Hex Artifact Content
Not logged in

Artifact 3edcc9b8ccf62b49dd0910129a1cba3ea827761f:

File src/sqlite3.c part of check-in [3f6edbc779] - Update to the latest SQLite. Add a Rebuild button on the Shun webpage. Add the test-detach CLI method. by drh on 2008-11-10 00:40:10.

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 36 2e 34 2e 20 20 42  ersion 3.6.4.  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 36 35 36 39 20 6c 69 6e 65 73  st.** 6569 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 31 31 2d 31 30 20 30 30 3a 31 34  2008-11-10 00:14
04c0: 3a 33 36 20 55 54 43 2e 0a 2a 2f 0a 23 64 65 66  :36 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 38 38 20 32 30 30 38 2f 31 31 2f 30 35 20 31  788 2008/11/05 1
0790: 36 3a 33 37 3a 33 35 20 64 72 68 20 45 78 70 20  6:37:35 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 69 66 6e 64 65 66  ns... */.#ifndef
2060: 20 5f 47 4e 55 5f 53 4f 55 52 43 45 0a 23 20 64   _GNU_SOURCE.# d
2070: 65 66 69 6e 65 20 5f 47 4e 55 5f 53 4f 55 52 43  efine _GNU_SOURC
2080: 45 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  E.#endif../*.** 
2090: 49 6e 63 6c 75 64 65 20 73 74 61 6e 64 61 72 64  Include standard
20a0: 20 68 65 61 64 65 72 20 66 69 6c 65 73 20 61 73   header files as
20b0: 20 6e 65 63 65 73 73 61 72 79 0a 2a 2f 0a 23 69   necessary.*/.#i
20c0: 66 64 65 66 20 48 41 56 45 5f 53 54 44 49 4e 54  fdef HAVE_STDINT
20d0: 5f 48 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64  _H.#include <std
20e0: 69 6e 74 2e 68 3e 0a 23 65 6e 64 69 66 0a 23 69  int.h>.#endif.#i
20f0: 66 64 65 66 20 48 41 56 45 5f 49 4e 54 54 59 50  fdef HAVE_INTTYP
2100: 45 53 5f 48 0a 23 69 6e 63 6c 75 64 65 20 3c 69  ES_H.#include <i
2110: 6e 74 74 79 70 65 73 2e 68 3e 0a 23 65 6e 64 69  nttypes.h>.#endi
2120: 66 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61 63 72 6f  f../*.** A macro
2130: 20 75 73 65 64 20 74 6f 20 61 69 64 20 69 6e 20   used to aid in 
2140: 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67  coverage testing
2150: 2e 20 20 57 68 65 6e 20 64 6f 69 6e 67 20 63 6f  .  When doing co
2160: 76 65 72 61 67 65 0a 2a 2a 20 74 65 73 74 69 6e  verage.** testin
2170: 67 2c 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e  g, the condition
2180: 20 69 6e 73 69 64 65 20 74 68 65 20 61 72 67 75   inside the argu
2190: 6d 65 6e 74 20 6d 75 73 74 20 62 65 20 65 76 61  ment must be eva
21a0: 6c 75 61 74 65 64 20 0a 2a 2a 20 62 6f 74 68 20  luated .** both 
21b0: 74 72 75 65 20 61 6e 64 20 66 61 6c 73 65 20 69  true and false i
21c0: 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 74 20 66  n order to get f
21d0: 75 6c 6c 20 62 72 61 6e 63 68 20 63 6f 76 65 72  ull branch cover
21e0: 61 67 65 2e 0a 2a 2a 20 54 68 69 73 20 6d 61 63  age..** This mac
21f0: 72 6f 20 63 61 6e 20 62 65 20 69 6e 73 65 72 74  ro can be insert
2200: 65 64 20 74 6f 20 65 6e 73 75 72 65 20 61 64 65  ed to ensure ade
2210: 71 75 61 74 65 20 74 65 73 74 20 63 6f 76 65 72  quate test cover
2220: 61 67 65 0a 2a 2a 20 69 6e 20 70 6c 61 63 65 73  age.** in places
2230: 20 77 68 65 72 65 20 73 69 6d 70 6c 65 20 63 6f   where simple co
2240: 6e 64 69 74 69 6f 6e 2f 64 65 63 69 73 69 6f 6e  ndition/decision
2250: 20 63 6f 76 65 72 61 67 65 20 69 73 20 69 6e 61   coverage is ina
2260: 64 65 71 75 61 74 65 2e 0a 2a 2f 0a 23 69 66 64  dequate..*/.#ifd
2270: 65 66 20 53 51 4c 49 54 45 5f 43 4f 56 45 52 41  ef SQLITE_COVERA
2280: 47 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50  GE_TEST.SQLITE_P
2290: 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
22a0: 6c 69 74 65 33 43 6f 76 65 72 61 67 65 28 69 6e  lite3Coverage(in
22b0: 74 29 3b 0a 23 20 64 65 66 69 6e 65 20 74 65 73  t);.# define tes
22c0: 74 63 61 73 65 28 58 29 20 20 69 66 28 20 58 20  tcase(X)  if( X 
22d0: 29 7b 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61  ){ sqlite3Covera
22e0: 67 65 28 5f 5f 4c 49 4e 45 5f 5f 29 3b 20 7d 0a  ge(__LINE__); }.
22f0: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 74  #else.# define t
2300: 65 73 74 63 61 73 65 28 58 29 0a 23 65 6e 64 69  estcase(X).#endi
2310: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 41 4c 57  f../*.** The ALW
2320: 41 59 53 20 61 6e 64 20 4e 45 56 45 52 20 6d 61  AYS and NEVER ma
2330: 63 72 6f 73 20 73 75 72 72 6f 75 6e 64 20 62 6f  cros surround bo
2340: 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  olean expression
2350: 73 20 77 68 69 63 68 20 0a 2a 2a 20 61 72 65 20  s which .** are 
2360: 69 6e 74 65 6e 64 65 64 20 74 6f 20 61 6c 77 61  intended to alwa
2370: 79 73 20 62 65 20 74 72 75 65 20 6f 72 20 66 61  ys be true or fa
2380: 6c 73 65 2c 20 72 65 73 70 65 63 74 69 76 65 6c  lse, respectivel
2390: 79 2e 20 20 53 75 63 68 0a 2a 2a 20 65 78 70 72  y.  Such.** expr
23a0: 65 73 73 69 6f 6e 73 20 63 6f 75 6c 64 20 62 65  essions could be
23b0: 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 74 68   omitted from th
23c0: 65 20 63 6f 64 65 20 63 6f 6d 70 6c 65 74 65 6c  e code completel
23d0: 79 2e 20 20 42 75 74 20 74 68 65 79 0a 2a 2a 20  y.  But they.** 
23e0: 61 72 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 20  are included in 
23f0: 61 20 66 65 77 20 63 61 73 65 73 20 69 6e 20 6f  a few cases in o
2400: 72 64 65 72 20 74 6f 20 65 6e 68 61 6e 63 65 20  rder to enhance 
2410: 74 68 65 20 72 65 73 69 6c 69 65 6e 63 65 0a 2a  the resilience.*
2420: 2a 20 6f 66 20 53 51 4c 69 74 65 20 74 6f 20 75  * of SQLite to u
2430: 6e 65 78 70 65 63 74 65 64 20 62 65 68 61 76 69  nexpected behavi
2440: 6f 72 20 2d 20 74 6f 20 6d 61 6b 65 20 74 68 65  or - to make the
2450: 20 63 6f 64 65 20 22 73 65 6c 66 2d 68 65 61 6c   code "self-heal
2460: 69 6e 67 22 0a 2a 2a 20 6f 72 20 22 64 75 63 74  ing".** or "duct
2470: 69 6c 65 22 20 72 61 74 68 65 72 20 74 68 61 6e  ile" rather than
2480: 20 62 65 69 6e 67 20 22 62 72 69 74 74 6c 65 22   being "brittle"
2490: 20 61 6e 64 20 63 72 61 73 68 69 6e 67 20 61 74   and crashing at
24a0: 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 68 69   the first.** hi
24b0: 6e 74 20 6f 66 20 75 6e 70 6c 61 6e 6e 65 64 20  nt of unplanned 
24c0: 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20  behavior..**.** 
24d0: 57 68 65 6e 20 64 6f 69 6e 67 20 63 6f 76 65 72  When doing cover
24e0: 61 67 65 20 74 65 73 74 69 6e 67 20 41 4c 57 41  age testing ALWA
24f0: 59 53 20 61 6e 64 20 4e 45 56 45 52 20 61 72 65  YS and NEVER are
2500: 20 68 61 72 64 2d 63 6f 64 65 64 20 74 6f 0a 2a   hard-coded to.*
2510: 2a 20 62 65 20 74 72 75 65 20 61 6e 64 20 66 61  * be true and fa
2520: 6c 73 65 20 73 6f 20 74 68 61 74 20 74 68 65 20  lse so that the 
2530: 75 6e 72 65 61 63 68 61 62 6c 65 20 63 6f 64 65  unreachable code
2540: 20 74 68 65 6e 20 73 70 65 63 69 66 79 20 77 69   then specify wi
2550: 6c 6c 0a 2a 2a 20 6e 6f 74 20 62 65 20 63 6f 75  ll.** not be cou
2560: 6e 74 65 64 20 61 73 20 75 6e 74 65 73 74 65 64  nted as untested
2570: 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 66 64 65 66   code..*/.#ifdef
2580: 20 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45   SQLITE_COVERAGE
2590: 5f 54 45 53 54 0a 23 20 64 65 66 69 6e 65 20 41  _TEST.# define A
25a0: 4c 57 41 59 53 28 58 29 20 20 20 20 20 20 28 31  LWAYS(X)      (1
25b0: 29 0a 23 20 64 65 66 69 6e 65 20 4e 45 56 45 52  ).# define NEVER
25c0: 28 58 29 20 20 20 20 20 20 20 28 30 29 0a 23 65  (X)       (0).#e
25d0: 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 41 4c 57  lse.# define ALW
25e0: 41 59 53 28 58 29 20 20 20 20 20 20 28 58 29 0a  AYS(X)      (X).
25f0: 23 20 64 65 66 69 6e 65 20 4e 45 56 45 52 28 58  # define NEVER(X
2600: 29 20 20 20 20 20 20 20 28 58 29 0a 23 65 6e 64  )       (X).#end
2610: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61  if../*.** The ma
2620: 63 72 6f 20 75 6e 6c 69 6b 65 6c 79 28 29 20 69  cro unlikely() i
2630: 73 20 61 20 68 69 6e 74 20 74 68 61 74 20 73 75  s a hint that su
2640: 72 72 6f 75 6e 64 73 20 61 20 62 6f 6f 6c 65 61  rrounds a boolea
2650: 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20  n.** expression 
2660: 74 68 61 74 20 69 73 20 75 73 75 61 6c 6c 79 20  that is usually 
2670: 66 61 6c 73 65 2e 20 20 4d 61 63 72 6f 20 6c 69  false.  Macro li
2680: 6b 65 6c 79 28 29 20 73 75 72 72 6f 75 6e 64 73  kely() surrounds
2690: 0a 2a 2a 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78  .** a boolean ex
26a0: 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73  pression that is
26b0: 20 75 73 75 61 6c 6c 79 20 74 72 75 65 2e 20 20   usually true.  
26c0: 47 43 43 20 69 73 20 61 62 6c 65 20 74 6f 0a 2a  GCC is able to.*
26d0: 2a 20 75 73 65 20 74 68 65 73 65 20 68 69 6e 74  * use these hint
26e0: 73 20 74 6f 20 67 65 6e 65 72 61 74 65 20 62 65  s to generate be
26f0: 74 74 65 72 20 63 6f 64 65 2c 20 73 6f 6d 65 74  tter code, somet
2700: 69 6d 65 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66  imes..*/.#if def
2710: 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26  ined(__GNUC__) &
2720: 26 20 30 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b  & 0.# define lik
2730: 65 6c 79 28 58 29 20 20 20 20 5f 5f 62 75 69 6c  ely(X)    __buil
2740: 74 69 6e 5f 65 78 70 65 63 74 28 28 58 29 2c 31  tin_expect((X),1
2750: 29 0a 23 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b  ).# define unlik
2760: 65 6c 79 28 58 29 20 20 5f 5f 62 75 69 6c 74 69  ely(X)  __builti
2770: 6e 5f 65 78 70 65 63 74 28 28 58 29 2c 30 29 0a  n_expect((X),0).
2780: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 6c  #else.# define l
2790: 69 6b 65 6c 79 28 58 29 20 20 20 20 21 21 28 58  ikely(X)    !!(X
27a0: 29 0a 23 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b  ).# define unlik
27b0: 65 6c 79 28 58 29 20 20 21 21 28 58 29 0a 23 65  ely(X)  !!(X).#e
27c0: 6e 64 69 66 0a 0a 2f 2a 0a 20 2a 20 54 68 69 73  ndif../*. * This
27d0: 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20 74   macro is used t
27e0: 6f 20 22 68 69 64 65 22 20 73 6f 6d 65 20 75 67  o "hide" some ug
27f0: 6c 69 6e 65 73 73 20 69 6e 20 63 61 73 74 69 6e  liness in castin
2800: 67 20 61 6e 20 69 6e 74 0a 20 2a 20 76 61 6c 75  g an int. * valu
2810: 65 20 74 6f 20 61 20 70 74 72 20 76 61 6c 75 65  e to a ptr value
2820: 20 75 6e 64 65 72 20 74 68 65 20 4d 53 56 43 20   under the MSVC 
2830: 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c 65 72 2e  64-bit compiler.
2840: 20 20 20 43 61 73 74 69 6e 67 0a 20 2a 20 6e 6f     Casting. * no
2850: 6e 20 36 34 2d 62 69 74 20 76 61 6c 75 65 73 20  n 64-bit values 
2860: 74 6f 20 70 74 72 20 74 79 70 65 73 20 72 65 73  to ptr types res
2870: 75 6c 74 73 20 69 6e 20 61 20 22 68 61 72 64 22  ults in a "hard"
2880: 20 65 72 72 6f 72 20 77 69 74 68 20 0a 20 2a 20   error with . * 
2890: 74 68 65 20 4d 53 56 43 20 36 34 2d 62 69 74 20  the MSVC 64-bit 
28a0: 63 6f 6d 70 69 6c 65 72 20 77 68 69 63 68 20 74  compiler which t
28b0: 68 69 73 20 61 74 74 65 6d 70 74 73 20 74 6f 20  his attempts to 
28c0: 61 76 6f 69 64 2e 20 20 0a 20 2a 0a 20 2a 20 41  avoid.  . *. * A
28d0: 20 73 69 6d 70 6c 65 20 63 6f 6d 70 69 6c 65 72   simple compiler
28e0: 20 70 72 61 67 6d 61 20 6f 72 20 63 61 73 74 69   pragma or casti
28f0: 6e 67 20 73 65 71 75 65 6e 63 65 20 63 6f 75 6c  ng sequence coul
2900: 64 20 6e 6f 74 20 62 65 20 66 6f 75 6e 64 0a 20  d not be found. 
2910: 2a 20 74 6f 20 63 6f 72 72 65 63 74 20 74 68 69  * to correct thi
2920: 73 20 69 6e 20 61 6c 6c 20 73 69 74 75 61 74 69  s in all situati
2930: 6f 6e 73 2c 20 73 6f 20 74 68 69 73 20 6d 61 63  ons, so this mac
2940: 72 6f 20 77 61 73 20 69 6e 74 72 6f 64 75 63 65  ro was introduce
2950: 64 2e 0a 20 2a 0a 20 2a 20 49 74 20 63 6f 75 6c  d.. *. * It coul
2960: 64 20 62 65 20 61 72 67 75 65 64 20 74 68 61 74  d be argued that
2970: 20 74 68 65 20 69 6e 74 70 74 72 5f 74 20 74 79   the intptr_t ty
2980: 70 65 20 63 6f 75 6c 64 20 62 65 20 75 73 65 64  pe could be used
2990: 20 69 6e 20 74 68 69 73 0a 20 2a 20 63 61 73 65   in this. * case
29a0: 2c 20 62 75 74 20 74 68 61 74 20 74 79 70 65 20  , but that type 
29b0: 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65  is not available
29c0: 20 6f 6e 20 61 6c 6c 20 63 6f 6d 70 69 6c 65 72   on all compiler
29d0: 73 2c 20 6f 72 20 0a 20 2a 20 72 65 71 75 69 72  s, or . * requir
29e0: 65 73 20 74 68 65 20 23 69 6e 63 6c 75 64 65 20  es the #include 
29f0: 6f 66 20 73 70 65 63 69 66 69 63 20 68 65 61 64  of specific head
2a00: 65 72 73 20 77 68 69 63 68 20 64 69 66 66 65 72  ers which differ
2a10: 73 20 62 65 74 77 65 65 6e 0a 20 2a 20 70 6c 61  s between. * pla
2a20: 74 66 6f 72 6d 73 2e 0a 20 2a 2f 0a 23 64 65 66  tforms.. */.#def
2a30: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54  ine SQLITE_INT_T
2a40: 4f 5f 50 54 52 28 58 29 20 20 20 28 28 76 6f 69  O_PTR(X)   ((voi
2a50: 64 2a 29 26 28 28 63 68 61 72 2a 29 30 29 5b 58  d*)&((char*)0)[X
2a60: 5d 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  ]).#define SQLIT
2a70: 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 58 29 20  E_PTR_TO_INT(X) 
2a80: 20 20 28 28 69 6e 74 29 28 28 28 63 68 61 72 2a    ((int)(((char*
2a90: 29 58 29 2d 28 63 68 61 72 2a 29 30 29 29 0a 0a  )X)-(char*)0))..
2aa0: 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64 65 66  /*.** These #def
2ab0: 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e 61 62  ines should enab
2ac0: 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73 75 70  le >2GB file sup
2ad0: 70 6f 72 74 20 6f 6e 20 50 6f 73 69 78 20 69 66  port on Posix if
2ae0: 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79 69   the.** underlyi
2af0: 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  ng operating sys
2b00: 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69 74 2e  tem supports it.
2b10: 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61 63 6b    If the OS lack
2b20: 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c 65 20  s.** large file 
2b30: 73 75 70 70 6f 72 74 2c 20 6f 72 20 69 66 20 74  support, or if t
2b40: 68 65 20 4f 53 20 69 73 20 77 69 6e 64 6f 77 73  he OS is windows
2b50: 2c 20 74 68 65 73 65 20 73 68 6f 75 6c 64 20 62  , these should b
2b60: 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20  e no-ops..**.** 
2b70: 54 69 63 6b 65 74 20 23 32 37 33 39 3a 20 20 54  Ticket #2739:  T
2b80: 68 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f  he _LARGEFILE_SO
2b90: 55 52 43 45 20 6d 61 63 72 6f 20 6d 75 73 74 20  URCE macro must 
2ba0: 61 70 70 65 61 72 20 62 65 66 6f 72 65 20 61 6e  appear before an
2bb0: 79 0a 2a 2a 20 73 79 73 74 65 6d 20 23 69 6e 63  y.** system #inc
2bc0: 6c 75 64 65 73 2e 20 20 48 65 6e 63 65 2c 20 74  ludes.  Hence, t
2bd0: 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64  his block of cod
2be0: 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 76 65  e must be the ve
2bf0: 72 79 20 66 69 72 73 74 0a 2a 2a 20 63 6f 64 65  ry first.** code
2c00: 20 69 6e 20 61 6c 6c 20 73 6f 75 72 63 65 20 66   in all source f
2c10: 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 67  iles..**.** Larg
2c20: 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20 63  e file support c
2c30: 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64 20 75  an be disabled u
2c40: 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c 49 54  sing the -DSQLIT
2c50: 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 73 77  E_DISABLE_LFS sw
2c60: 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 20 63  itch.** on the c
2c70: 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20  ompiler command 
2c80: 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73 20 6e  line.  This is n
2c90: 65 63 65 73 73 61 72 79 20 69 66 20 79 6f 75 20  ecessary if you 
2ca0: 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a  are compiling.**
2cb0: 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d 61 63   on a recent mac
2cc0: 68 69 6e 65 20 28 65 78 3a 20 52 65 64 48 61 74  hine (ex: RedHat
2cd0: 20 37 2e 32 29 20 62 75 74 20 79 6f 75 20 77 61   7.2) but you wa
2ce0: 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 74 6f 20  nt your code to 
2cf0: 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f 6c  work.** on an ol
2d00: 64 65 72 20 6d 61 63 68 69 6e 65 20 28 65 78 3a  der machine (ex:
2d10: 20 52 65 64 48 61 74 20 36 2e 30 29 2e 20 20 49   RedHat 6.0).  I
2d20: 66 20 79 6f 75 20 63 6f 6d 70 69 6c 65 20 6f 6e  f you compile on
2d30: 20 52 65 64 48 61 74 20 37 2e 32 0a 2a 2a 20 77   RedHat 7.2.** w
2d40: 69 74 68 6f 75 74 20 74 68 69 73 20 6f 70 74 69  ithout this opti
2d50: 6f 6e 2c 20 4c 46 53 20 69 73 20 65 6e 61 62 6c  on, LFS is enabl
2d60: 65 2e 20 20 42 75 74 20 4c 46 53 20 64 6f 65 73  e.  But LFS does
2d70: 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 20 74 68   not exist in th
2d80: 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20 69 6e 20 52  e kernel.** in R
2d90: 65 64 48 61 74 20 36 2e 30 2c 20 73 6f 20 74 68  edHat 6.0, so th
2da0: 65 20 63 6f 64 65 20 77 6f 6e 27 74 20 77 6f 72  e code won't wor
2db0: 6b 2e 20 20 48 65 6e 63 65 2c 20 66 6f 72 20 6d  k.  Hence, for m
2dc0: 61 78 69 6d 75 6d 20 62 69 6e 61 72 79 0a 2a 2a  aximum binary.**
2dd0: 20 70 6f 72 74 61 62 69 6c 69 74 79 20 79 6f 75   portability you
2de0: 20 73 68 6f 75 6c 64 20 6f 6d 69 74 20 4c 46 53   should omit LFS
2df0: 2e 0a 2a 2a 0a 2a 2a 20 53 69 6d 69 6c 61 72 20  ..**.** Similar 
2e00: 69 73 20 74 72 75 65 20 66 6f 72 20 4d 61 63 4f  is true for MacO
2e10: 53 2e 20 20 4c 46 53 20 69 73 20 6f 6e 6c 79 20  S.  LFS is only 
2e20: 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 4d 61 63  supported on Mac
2e30: 4f 53 20 39 20 61 6e 64 20 6c 61 74 65 72 2e 0a  OS 9 and later..
2e40: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
2e50: 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 0a 23 20  E_DISABLE_LFS.# 
2e60: 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 5f 46 49  define _LARGE_FI
2e70: 4c 45 20 20 20 20 20 20 20 31 0a 23 20 69 66 6e  LE       1.# ifn
2e80: 64 65 66 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54  def _FILE_OFFSET
2e90: 5f 42 49 54 53 0a 23 20 20 20 64 65 66 69 6e 65  _BITS.#   define
2ea0: 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49   _FILE_OFFSET_BI
2eb0: 54 53 20 36 34 0a 23 20 65 6e 64 69 66 0a 23 20  TS 64.# endif.# 
2ec0: 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 46 49 4c  define _LARGEFIL
2ed0: 45 5f 53 4f 55 52 43 45 20 31 0a 23 65 6e 64 69  E_SOURCE 1.#endi
2ee0: 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51  f.../*.** The SQ
2ef0: 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20  LITE_THREADSAFE 
2f00: 6d 61 63 72 6f 20 6d 75 73 74 20 62 65 20 64 65  macro must be de
2f10: 66 69 6e 65 64 20 61 73 20 65 69 74 68 65 72 20  fined as either 
2f20: 30 20 6f 72 20 31 2e 0a 2a 2a 20 4f 6c 64 65 72  0 or 1..** Older
2f30: 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c   versions of SQL
2f40: 69 74 65 20 75 73 65 64 20 61 6e 20 6f 70 74 69  ite used an opti
2f50: 6f 6e 61 6c 20 54 48 52 45 41 44 53 41 46 45 20  onal THREADSAFE 
2f60: 6d 61 63 72 6f 2e 0a 2a 2a 20 57 65 20 73 75 70  macro..** We sup
2f70: 70 6f 72 74 20 74 68 61 74 20 66 6f 72 20 6c 65  port that for le
2f80: 67 61 63 79 0a 2a 2f 0a 23 69 66 20 21 64 65 66  gacy.*/.#if !def
2f90: 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 48 52 45  ined(SQLITE_THRE
2fa0: 41 44 53 41 46 45 29 0a 23 69 66 20 64 65 66 69  ADSAFE).#if defi
2fb0: 6e 65 64 28 54 48 52 45 41 44 53 41 46 45 29 0a  ned(THREADSAFE).
2fc0: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
2fd0: 54 48 52 45 41 44 53 41 46 45 20 54 48 52 45 41  THREADSAFE THREA
2fe0: 44 53 41 46 45 0a 23 65 6c 73 65 0a 23 20 64 65  DSAFE.#else.# de
2ff0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45  fine SQLITE_THRE
3000: 41 44 53 41 46 45 20 31 0a 23 65 6e 64 69 66 0a  ADSAFE 1.#endif.
3010: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
3020: 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  e SQLITE_DEFAULT
3030: 5f 4d 45 4d 53 54 41 54 55 53 20 6d 61 63 72 6f  _MEMSTATUS macro
3040: 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 64   must be defined
3050: 20 61 73 20 65 69 74 68 65 72 20 30 20 6f 72 20   as either 0 or 
3060: 31 2e 0a 2a 2a 20 49 74 20 64 65 74 65 72 6d 69  1..** It determi
3070: 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e  nes whether or n
3080: 6f 74 20 74 68 65 20 66 65 61 74 75 72 65 73 20  ot the features 
3090: 72 65 6c 61 74 65 64 20 74 6f 20 0a 2a 2a 20 53  related to .** S
30a0: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d  QLITE_CONFIG_MEM
30b0: 53 54 41 54 55 53 20 61 72 65 20 61 76 61 69 6c  STATUS are avail
30c0: 61 62 65 20 62 79 20 64 65 66 61 75 6c 74 20 6f  abe by default o
30d0: 72 20 6e 6f 74 2e 20 54 68 69 73 20 76 61 6c 75  r not. This valu
30e0: 65 20 63 61 6e 0a 2a 2a 20 62 65 20 6f 76 65 72  e can.** be over
30f0: 72 69 64 64 65 6e 20 61 74 20 72 75 6e 74 69 6d  ridden at runtim
3100: 65 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69  e using the sqli
3110: 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 41 50 49  te3_config() API
3120: 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65  ..*/.#if !define
3130: 64 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  d(SQLITE_DEFAULT
3140: 5f 4d 45 4d 53 54 41 54 55 53 29 0a 23 20 64 65  _MEMSTATUS).# de
3150: 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41  fine SQLITE_DEFA
3160: 55 4c 54 5f 4d 45 4d 53 54 41 54 55 53 20 31 0a  ULT_MEMSTATUS 1.
3170: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78  #endif../*.** Ex
3180: 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68 65  actly one of the
3190: 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f   following macro
31a0: 73 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65  s must be define
31b0: 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a  d in order to.**
31c0: 20 73 70 65 63 69 66 79 20 77 68 69 63 68 20 6d   specify which m
31d0: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
31e0: 20 73 75 62 73 79 73 74 65 6d 20 74 6f 20 75 73   subsystem to us
31f0: 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c  e..**.**     SQL
3200: 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f  ITE_SYSTEM_MALLO
3210: 43 20 20 20 20 20 20 20 20 20 20 2f 2f 20 55 73  C          // Us
3220: 65 20 6e 6f 72 6d 61 6c 20 73 79 73 74 65 6d 20  e normal system 
3230: 6d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 20 20 20 20  malloc().**     
3240: 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 20  SQLITE_MEMDEBUG 
3250: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2f                //
3260: 20 44 65 62 75 67 67 69 6e 67 20 76 65 72 73 69   Debugging versi
3270: 6f 6e 20 6f 66 20 73 79 73 74 65 6d 20 6d 61 6c  on of system mal
3280: 6c 6f 63 28 29 0a 2a 2a 20 20 20 20 20 53 51 4c  loc().**     SQL
3290: 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20  ITE_MEMORY_SIZE 
32a0: 20 20 20 20 20 20 20 20 20 20 20 2f 2f 20 69 6e             // in
32b0: 74 65 72 6e 61 6c 20 61 6c 6c 6f 63 61 74 6f 72  ternal allocator
32c0: 20 23 31 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54   #1.**     SQLIT
32d0: 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45  E_MMAP_HEAP_SIZE
32e0: 20 20 20 20 20 20 20 20 20 2f 2f 20 69 6e 74 65           // inte
32f0: 72 6e 61 6c 20 6d 6d 61 70 28 29 20 61 6c 6c 6f  rnal mmap() allo
3300: 63 61 74 6f 72 0a 2a 2a 20 20 20 20 20 53 51 4c  cator.**     SQL
3310: 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f  ITE_POW2_MEMORY_
3320: 53 49 5a 45 20 20 20 20 20 20 20 2f 2f 20 69 6e  SIZE       // in
3330: 74 65 72 6e 61 6c 20 70 6f 77 65 72 2d 6f 66 2d  ternal power-of-
3340: 74 77 6f 20 61 6c 6c 6f 63 61 74 6f 72 0a 2a 2a  two allocator.**
3350: 0a 2a 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74  .** If none of t
3360: 68 65 20 61 62 6f 76 65 20 61 72 65 20 64 65 66  he above are def
3370: 69 6e 65 64 2c 20 74 68 65 6e 20 73 65 74 20 53  ined, then set S
3380: 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c  QLITE_SYSTEM_MAL
3390: 4c 4f 43 20 61 73 0a 2a 2a 20 74 68 65 20 64 65  LOC as.** the de
33a0: 66 61 75 6c 74 2e 0a 2a 2f 0a 23 69 66 20 64 65  fault..*/.#if de
33b0: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 53 59 53  fined(SQLITE_SYS
33c0: 54 45 4d 5f 4d 41 4c 4c 4f 43 29 2b 64 65 66 69  TEM_MALLOC)+defi
33d0: 6e 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d 44 45  ned(SQLITE_MEMDE
33e0: 42 55 47 29 2b 5c 0a 20 20 20 20 64 65 66 69 6e  BUG)+\.    defin
33f0: 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59  ed(SQLITE_MEMORY
3400: 5f 53 49 5a 45 29 2b 64 65 66 69 6e 65 64 28 53  _SIZE)+defined(S
3410: 51 4c 49 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f  QLITE_MMAP_HEAP_
3420: 53 49 5a 45 29 2b 5c 0a 20 20 20 20 64 65 66 69  SIZE)+\.    defi
3430: 6e 65 64 28 53 51 4c 49 54 45 5f 50 4f 57 32 5f  ned(SQLITE_POW2_
3440: 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 3e 31 0a 23  MEMORY_SIZE)>1.#
3450: 20 65 72 72 6f 72 20 22 41 74 20 6d 6f 73 74 20   error "At most 
3460: 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  one of the follo
3470: 77 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  wing compile-tim
3480: 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  e configuration 
3490: 6f 70 74 69 6f 6e 73 5c 0a 20 69 73 20 61 6c 6c  options\. is all
34a0: 6f 77 73 3a 20 53 51 4c 49 54 45 5f 53 59 53 54  ows: SQLITE_SYST
34b0: 45 4d 5f 4d 41 4c 4c 4f 43 2c 20 53 51 4c 49 54  EM_MALLOC, SQLIT
34c0: 45 5f 4d 45 4d 44 45 42 55 47 2c 20 53 51 4c 49  E_MEMDEBUG, SQLI
34d0: 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 2c 5c  TE_MEMORY_SIZE,\
34e0: 0a 20 53 51 4c 49 54 45 5f 4d 4d 41 50 5f 48 45  . SQLITE_MMAP_HE
34f0: 41 50 5f 53 49 5a 45 2c 20 53 51 4c 49 54 45 5f  AP_SIZE, SQLITE_
3500: 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45  POW2_MEMORY_SIZE
3510: 22 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66  ".#endif.#if def
3520: 69 6e 65 64 28 53 51 4c 49 54 45 5f 53 59 53 54  ined(SQLITE_SYST
3530: 45 4d 5f 4d 41 4c 4c 4f 43 29 2b 64 65 66 69 6e  EM_MALLOC)+defin
3540: 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42  ed(SQLITE_MEMDEB
3550: 55 47 29 2b 5c 0a 20 20 20 20 64 65 66 69 6e 65  UG)+\.    define
3560: 64 28 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f  d(SQLITE_MEMORY_
3570: 53 49 5a 45 29 2b 64 65 66 69 6e 65 64 28 53 51  SIZE)+defined(SQ
3580: 4c 49 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53  LITE_MMAP_HEAP_S
3590: 49 5a 45 29 2b 5c 0a 20 20 20 20 64 65 66 69 6e  IZE)+\.    defin
35a0: 65 64 28 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d  ed(SQLITE_POW2_M
35b0: 45 4d 4f 52 59 5f 53 49 5a 45 29 3d 3d 30 0a 23  EMORY_SIZE)==0.#
35c0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53   define SQLITE_S
35d0: 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 31 0a 23  YSTEM_MALLOC 1.#
35e0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  endif../*.** If 
35f0: 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f  SQLITE_MALLOC_SO
3600: 46 54 5f 4c 49 4d 49 54 20 69 73 20 64 65 66 69  FT_LIMIT is defi
3610: 6e 65 64 2c 20 74 68 65 6e 20 74 72 79 20 74 6f  ned, then try to
3620: 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 69 7a   keep the.** siz
3630: 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61 6c 6c  es of memory all
3640: 6f 63 61 74 69 6f 6e 73 20 62 65 6c 6f 77 20 74  ocations below t
3650: 68 69 73 20 76 61 6c 75 65 20 77 68 65 72 65 20  his value where 
3660: 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a 23 69 66  possible..*/.#if
3670: 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
3680: 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45  POW2_MEMORY_SIZE
3690: 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
36a0: 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54  LITE_MALLOC_SOFT
36b0: 5f 4c 49 4d 49 54 29 0a 23 20 64 65 66 69 6e 65  _LIMIT).# define
36c0: 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53   SQLITE_MALLOC_S
36d0: 4f 46 54 5f 4c 49 4d 49 54 20 31 30 32 34 0a 23  OFT_LIMIT 1024.#
36e0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 65 20  endif../*.** We 
36f0: 6e 65 65 64 20 74 6f 20 64 65 66 69 6e 65 20 5f  need to define _
3700: 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 61 73 20  XOPEN_SOURCE as 
3710: 66 6f 6c 6c 6f 77 73 20 69 6e 20 6f 72 64 65 72  follows in order
3720: 20 74 6f 20 65 6e 61 62 6c 65 0a 2a 2a 20 72 65   to enable.** re
3730: 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 20  cursive mutexes 
3740: 6f 6e 20 6d 6f 73 74 20 75 6e 69 78 20 73 79 73  on most unix sys
3750: 74 65 6d 73 2e 20 20 42 75 74 20 4d 61 63 20 4f  tems.  But Mac O
3760: 53 20 58 20 69 73 20 64 69 66 66 65 72 65 6e 74  S X is different
3770: 2e 0a 2a 2a 20 54 68 65 20 5f 58 4f 50 45 4e 5f  ..** The _XOPEN_
3780: 53 4f 55 52 43 45 20 64 65 66 69 6e 65 20 63 61  SOURCE define ca
3790: 75 73 65 73 20 70 72 6f 62 6c 65 6d 73 20 66 6f  uses problems fo
37a0: 72 20 4d 61 63 20 4f 53 20 58 20 77 65 20 61 72  r Mac OS X we ar
37b0: 65 20 74 6f 6c 64 2c 0a 2a 2a 20 73 6f 20 69 74  e told,.** so it
37c0: 20 69 73 20 6f 6d 69 74 74 65 64 20 74 68 65 72   is omitted ther
37d0: 65 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20 23  e.  See ticket #
37e0: 32 36 37 33 2e 0a 2a 2a 0a 2a 2a 20 4c 61 74 65  2673..**.** Late
37f0: 72 20 77 65 20 6c 65 61 72 6e 20 74 68 61 74 20  r we learn that 
3800: 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 69 73  _XOPEN_SOURCE is
3810: 20 70 6f 6f 72 6c 79 20 6f 72 20 69 6e 63 6f 72   poorly or incor
3820: 72 65 63 74 6c 79 0a 2a 2a 20 69 6d 70 6c 65 6d  rectly.** implem
3830: 65 6e 74 65 64 20 6f 6e 20 73 6f 6d 65 20 73 79  ented on some sy
3840: 73 74 65 6d 73 2e 20 20 53 6f 20 77 65 20 61 76  stems.  So we av
3850: 6f 69 64 20 64 65 66 69 6e 69 6e 67 20 69 74 20  oid defining it 
3860: 61 74 20 61 6c 6c 0a 2a 2a 20 69 66 20 69 74 20  at all.** if it 
3870: 69 73 20 61 6c 72 65 61 64 79 20 64 65 66 69 6e  is already defin
3880: 65 64 20 6f 72 20 69 66 20 69 74 20 69 73 20 75  ed or if it is u
3890: 6e 6e 65 65 64 65 64 20 62 65 63 61 75 73 65 20  nneeded because 
38a0: 77 65 20 61 72 65 0a 2a 2a 20 6e 6f 74 20 64 6f  we are.** not do
38b0: 69 6e 67 20 61 20 74 68 72 65 61 64 73 61 66 65  ing a threadsafe
38c0: 20 62 75 69 6c 64 2e 20 20 54 69 63 6b 65 74 20   build.  Ticket 
38d0: 23 32 36 38 31 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  #2681..**.** See
38e0: 20 61 6c 73 6f 20 74 69 63 6b 65 74 20 23 32 37   also ticket #27
38f0: 34 31 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69  41..*/.#if !defi
3900: 6e 65 64 28 5f 58 4f 50 45 4e 5f 53 4f 55 52 43  ned(_XOPEN_SOURC
3910: 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f  E) && !defined(_
3920: 5f 44 41 52 57 49 4e 5f 5f 29 20 26 26 20 21 64  _DARWIN__) && !d
3930: 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f  efined(__APPLE__
3940: 29 20 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45  ) && SQLITE_THRE
3950: 41 44 53 41 46 45 0a 23 20 20 64 65 66 69 6e 65  ADSAFE.#  define
3960: 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 35   _XOPEN_SOURCE 5
3970: 30 30 20 20 2f 2a 20 4e 65 65 64 65 64 20 74 6f  00  /* Needed to
3980: 20 65 6e 61 62 6c 65 20 70 74 68 72 65 61 64 20   enable pthread 
3990: 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65  recursive mutexe
39a0: 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  s */.#endif../*.
39b0: 2a 2a 20 54 68 65 20 54 43 4c 20 68 65 61 64 65  ** The TCL heade
39c0: 72 73 20 61 72 65 20 6f 6e 6c 79 20 6e 65 65 64  rs are only need
39d0: 65 64 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e  ed when compilin
39e0: 67 20 74 68 65 20 54 43 4c 20 62 69 6e 64 69 6e  g the TCL bindin
39f0: 67 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e  gs..*/.#if defin
3a00: 65 64 28 53 51 4c 49 54 45 5f 54 43 4c 29 20 7c  ed(SQLITE_TCL) |
3a10: 7c 20 64 65 66 69 6e 65 64 28 54 43 4c 53 48 29  | defined(TCLSH)
3a20: 0a 23 20 69 6e 63 6c 75 64 65 20 3c 74 63 6c 2e  .# include <tcl.
3a30: 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  h>.#endif../*.**
3a40: 20 4d 61 6e 79 20 70 65 6f 70 6c 65 20 61 72 65   Many people are
3a50: 20 66 61 69 6c 69 6e 67 20 74 6f 20 73 65 74 20   failing to set 
3a60: 2d 44 4e 44 45 42 55 47 3d 31 20 77 68 65 6e 20  -DNDEBUG=1 when 
3a70: 63 6f 6d 70 69 6c 69 6e 67 20 53 51 4c 69 74 65  compiling SQLite
3a80: 2e 0a 2a 2a 20 53 65 74 74 69 6e 67 20 4e 44 45  ..** Setting NDE
3a90: 42 55 47 20 6d 61 6b 65 73 20 74 68 65 20 63 6f  BUG makes the co
3aa0: 64 65 20 73 6d 61 6c 6c 65 72 20 61 6e 64 20 72  de smaller and r
3ab0: 75 6e 20 66 61 73 74 65 72 2e 20 20 53 6f 20 74  un faster.  So t
3ac0: 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  he following.** 
3ad0: 6c 69 6e 65 73 20 61 72 65 20 61 64 64 65 64 20  lines are added 
3ae0: 74 6f 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  to automatically
3af0: 20 73 65 74 20 4e 44 45 42 55 47 20 75 6e 6c 65   set NDEBUG unle
3b00: 73 73 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f  ss the -DSQLITE_
3b10: 44 45 42 55 47 3d 31 0a 2a 2a 20 6f 70 74 69 6f  DEBUG=1.** optio
3b20: 6e 20 69 73 20 73 65 74 2e 20 20 54 68 75 73 20  n is set.  Thus 
3b30: 4e 44 45 42 55 47 20 62 65 63 6f 6d 65 73 20 61  NDEBUG becomes a
3b40: 6e 20 6f 70 74 2d 69 6e 20 72 61 74 68 65 72 20  n opt-in rather 
3b50: 74 68 61 6e 20 61 6e 20 6f 70 74 2d 6f 75 74 0a  than an opt-out.
3b60: 2a 2a 20 66 65 61 74 75 72 65 2e 0a 2a 2f 0a 23  ** feature..*/.#
3b70: 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42  if !defined(NDEB
3b80: 55 47 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  UG) && !defined(
3b90: 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 0a 23  SQLITE_DEBUG) .#
3ba0: 20 64 65 66 69 6e 65 20 4e 44 45 42 55 47 20 31   define NDEBUG 1
3bb0: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
3bc0: 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
3bd0: 20 73 71 6c 69 74 65 33 2e 68 20 69 6e 20 74 68   sqlite3.h in th
3be0: 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69  e middle of sqli
3bf0: 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
3c00: 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
3c10: 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
3c20: 69 6c 65 20 73 71 6c 69 74 65 33 2e 68 20 2a 2a  ile sqlite3.h **
3c30: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3c40: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3c50: 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
3c60: 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
3c70: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
3c80: 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
3c90: 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
3ca0: 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
3cb0: 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
3cc0: 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
3cd0: 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
3ce0: 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
3cf0: 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
3d00: 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
3d10: 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
3d20: 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
3d30: 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
3d40: 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
3d50: 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
3d60: 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
3d70: 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
3d80: 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
3d90: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3da0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3db0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dc0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
3dd0: 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66  ** This header f
3de0: 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20  ile defines the 
3df0: 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74  interface that t
3e00: 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72  he SQLite librar
3e10: 79 0a 2a 2a 20 70 72 65 73 65 6e 74 73 20 74 6f  y.** presents to
3e20: 20 63 6c 69 65 6e 74 20 70 72 6f 67 72 61 6d 73   client programs
3e30: 2e 20 20 49 66 20 61 20 43 2d 66 75 6e 63 74 69  .  If a C-functi
3e40: 6f 6e 2c 20 73 74 72 75 63 74 75 72 65 2c 20 64  on, structure, d
3e50: 61 74 61 74 79 70 65 2c 0a 2a 2a 20 6f 72 20 63  atatype,.** or c
3e60: 6f 6e 73 74 61 6e 74 20 64 65 66 69 6e 69 74 69  onstant definiti
3e70: 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 61 70 70 65  on does not appe
3e80: 61 72 20 69 6e 20 74 68 69 73 20 66 69 6c 65 2c  ar in this file,
3e90: 20 74 68 65 6e 20 69 74 20 69 73 0a 2a 2a 20 6e   then it is.** n
3ea0: 6f 74 20 61 20 70 75 62 6c 69 73 68 65 64 20 41  ot a published A
3eb0: 50 49 20 6f 66 20 53 51 4c 69 74 65 2c 20 69 73  PI of SQLite, is
3ec0: 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e   subject to chan
3ed0: 67 65 20 77 69 74 68 6f 75 74 0a 2a 2a 20 6e 6f  ge without.** no
3ee0: 74 69 63 65 2c 20 61 6e 64 20 73 68 6f 75 6c 64  tice, and should
3ef0: 20 6e 6f 74 20 62 65 20 72 65 66 65 72 65 6e 63   not be referenc
3f00: 65 64 20 62 79 20 70 72 6f 67 72 61 6d 73 20 74  ed by programs t
3f10: 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 2e 0a  hat use SQLite..
3f20: 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68  **.** Some of th
3f30: 65 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 74 68  e definitions th
3f40: 61 74 20 61 72 65 20 69 6e 20 74 68 69 73 20 66  at are in this f
3f50: 69 6c 65 20 61 72 65 20 6d 61 72 6b 65 64 20 61  ile are marked a
3f60: 73 0a 2a 2a 20 22 65 78 70 65 72 69 6d 65 6e 74  s.** "experiment
3f70: 61 6c 22 2e 20 20 45 78 70 65 72 69 6d 65 6e 74  al".  Experiment
3f80: 61 6c 20 69 6e 74 65 72 66 61 63 65 73 20 61 72  al interfaces ar
3f90: 65 20 6e 6f 72 6d 61 6c 6c 79 20 6e 65 77 0a 2a  e normally new.*
3fa0: 2a 20 66 65 61 74 75 72 65 73 20 72 65 63 65 6e  * features recen
3fb0: 74 6c 79 20 61 64 64 65 64 20 74 6f 20 53 51 4c  tly added to SQL
3fc0: 69 74 65 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20  ite.  We do not 
3fd0: 61 6e 74 69 63 69 70 61 74 65 20 63 68 61 6e 67  anticipate chang
3fe0: 65 73 0a 2a 2a 20 74 6f 20 65 78 70 65 72 69 6d  es.** to experim
3ff0: 65 6e 74 61 6c 20 69 6e 74 65 72 66 61 63 65 73  ental interfaces
4000: 20 62 75 74 20 72 65 73 65 72 76 65 20 74 6f 20   but reserve to 
4010: 6d 61 6b 65 20 6d 69 6e 6f 72 20 63 68 61 6e 67  make minor chang
4020: 65 73 20 69 66 0a 2a 2a 20 65 78 70 65 72 69 65  es if.** experie
4030: 6e 63 65 20 66 72 6f 6d 20 75 73 65 20 22 69 6e  nce from use "in
4040: 20 74 68 65 20 77 69 6c 64 22 20 73 75 67 67 65   the wild" sugge
4050: 73 74 20 73 75 63 68 20 63 68 61 6e 67 65 73 20  st such changes 
4060: 61 72 65 20 70 72 75 64 65 6e 74 2e 0a 2a 2a 0a  are prudent..**.
4070: 2a 2a 20 54 68 65 20 6f 66 66 69 63 69 61 6c 20  ** The official 
4080: 43 2d 6c 61 6e 67 75 61 67 65 20 41 50 49 20 64  C-language API d
4090: 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72  ocumentation for
40a0: 20 53 51 4c 69 74 65 20 69 73 20 64 65 72 69 76   SQLite is deriv
40b0: 65 64 0a 2a 2a 20 66 72 6f 6d 20 63 6f 6d 6d 65  ed.** from comme
40c0: 6e 74 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65  nts in this file
40d0: 2e 20 20 54 68 69 73 20 66 69 6c 65 20 69 73 20  .  This file is 
40e0: 74 68 65 20 61 75 74 68 6f 72 69 74 61 74 69 76  the authoritativ
40f0: 65 20 73 6f 75 72 63 65 0a 2a 2a 20 6f 6e 20 68  e source.** on h
4100: 6f 77 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66  ow SQLite interf
4110: 61 63 65 73 20 61 72 65 20 73 75 70 70 6f 73 65  aces are suppose
4120: 20 74 6f 20 6f 70 65 72 61 74 65 2e 0a 2a 2a 0a   to operate..**.
4130: 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74  ** The name of t
4140: 68 69 73 20 66 69 6c 65 20 75 6e 64 65 72 20 63  his file under c
4150: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6d 61 6e  onfiguration man
4160: 61 67 65 6d 65 6e 74 20 69 73 20 22 73 71 6c 69  agement is "sqli
4170: 74 65 2e 68 2e 69 6e 22 2e 0a 2a 2a 20 54 68 65  te.h.in"..** The
4180: 20 6d 61 6b 65 66 69 6c 65 20 6d 61 6b 65 73 20   makefile makes 
4190: 73 6f 6d 65 20 6d 69 6e 6f 72 20 63 68 61 6e 67  some minor chang
41a0: 65 73 20 74 6f 20 74 68 69 73 20 66 69 6c 65 20  es to this file 
41b0: 28 73 75 63 68 20 61 73 20 69 6e 73 65 72 74 69  (such as inserti
41c0: 6e 67 0a 2a 2a 20 74 68 65 20 76 65 72 73 69 6f  ng.** the versio
41d0: 6e 20 6e 75 6d 62 65 72 29 20 61 6e 64 20 63 68  n number) and ch
41e0: 61 6e 67 65 73 20 69 74 73 20 6e 61 6d 65 20 74  anges its name t
41f0: 6f 20 22 73 71 6c 69 74 65 33 2e 68 22 20 61 73  o "sqlite3.h" as
4200: 0a 2a 2a 20 70 61 72 74 20 6f 66 20 74 68 65 20  .** part of the 
4210: 62 75 69 6c 64 20 70 72 6f 63 65 73 73 2e 0a 2a  build process..*
4220: 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 73  *.** @(#) $Id: s
4230: 71 6c 69 74 65 2e 68 2e 69 6e 2c 76 20 31 2e 34  qlite.h.in,v 1.4
4240: 30 39 20 32 30 30 38 2f 31 31 2f 30 37 20 30 30  09 2008/11/07 00
4250: 3a 30 36 3a 31 38 20 64 72 68 20 45 78 70 20 24  :06:18 drh Exp $
4260: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c  .*/.#ifndef _SQL
4270: 49 54 45 33 5f 48 5f 0a 23 64 65 66 69 6e 65 20  ITE3_H_.#define 
4280: 5f 53 51 4c 49 54 45 33 5f 48 5f 0a 23 69 6e 63  _SQLITE3_H_.#inc
4290: 6c 75 64 65 20 3c 73 74 64 61 72 67 2e 68 3e 20  lude <stdarg.h> 
42a0: 20 20 20 20 2f 2a 20 4e 65 65 64 65 64 20 66 6f      /* Needed fo
42b0: 72 20 74 68 65 20 64 65 66 69 6e 69 74 69 6f 6e  r the definition
42c0: 20 6f 66 20 76 61 5f 6c 69 73 74 20 2a 2f 0a 0a   of va_list */..
42d0: 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  /*.** Make sure 
42e0: 77 65 20 63 61 6e 20 63 61 6c 6c 20 74 68 69 73  we can call this
42f0: 20 73 74 75 66 66 20 66 72 6f 6d 20 43 2b 2b 2e   stuff from C++.
4300: 0a 2a 2f 0a 23 69 66 20 30 0a 65 78 74 65 72 6e  .*/.#if 0.extern
4310: 20 22 43 22 20 7b 0a 23 65 6e 64 69 66 0a 0a 0a   "C" {.#endif...
4320: 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 61 62  /*.** Add the ab
4330: 69 6c 69 74 79 20 74 6f 20 6f 76 65 72 72 69 64  ility to overrid
4340: 65 20 27 65 78 74 65 72 6e 27 0a 2a 2f 0a 23 69  e 'extern'.*/.#i
4350: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 45 58 54  fndef SQLITE_EXT
4360: 45 52 4e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  ERN.# define SQL
4370: 49 54 45 5f 45 58 54 45 52 4e 20 65 78 74 65 72  ITE_EXTERN exter
4380: 6e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  n.#endif../*.** 
4390: 54 68 65 73 65 20 6e 6f 2d 6f 70 20 6d 61 63 72  These no-op macr
43a0: 6f 73 20 61 72 65 20 75 73 65 64 20 69 6e 20 66  os are used in f
43b0: 72 6f 6e 74 20 6f 66 20 69 6e 74 65 72 66 61 63  ront of interfac
43c0: 65 73 20 74 6f 20 6d 61 72 6b 20 74 68 6f 73 65  es to mark those
43d0: 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 61  .** interfaces a
43e0: 73 20 65 69 74 68 65 72 20 64 65 70 72 65 63 61  s either depreca
43f0: 74 65 64 20 6f 72 20 65 78 70 65 72 69 6d 65 6e  ted or experimen
4400: 74 61 6c 2e 20 20 4e 65 77 20 61 70 70 6c 69 63  tal.  New applic
4410: 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64  ations.** should
4420: 20 6e 6f 74 20 75 73 65 20 64 65 70 72 65 63 61   not use depreca
4430: 74 65 64 20 69 6e 74 72 66 61 63 65 73 20 2d 20  ted intrfaces - 
4440: 74 68 65 79 20 61 72 65 20 73 75 70 70 6f 72 74  they are support
4450: 20 66 6f 72 20 62 61 63 6b 77 61 72 64 73 0a 2a   for backwards.*
4460: 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20  * compatibility 
4470: 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 69  only.  Applicati
4480: 6f 6e 20 77 72 69 74 65 72 73 20 73 68 6f 75 6c  on writers shoul
4490: 64 20 62 65 20 61 77 61 72 65 20 74 68 61 74 0a  d be aware that.
44a0: 2a 2a 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20  ** experimental 
44b0: 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 73  interfaces are s
44c0: 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65  ubject to change
44d0: 20 69 6e 20 70 6f 69 6e 74 20 72 65 6c 65 61 73   in point releas
44e0: 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  es..**.** These 
44f0: 6d 61 63 72 6f 73 20 75 73 65 64 20 74 6f 20 72  macros used to r
4500: 65 73 6f 6c 76 65 20 74 6f 20 76 61 72 69 6f 75  esolve to variou
4510: 73 20 6b 69 6e 64 73 20 6f 66 20 63 6f 6d 70 69  s kinds of compi
4520: 6c 65 72 20 6d 61 67 69 63 20 74 68 61 74 0a 2a  ler magic that.*
4530: 2a 20 77 6f 75 6c 64 20 67 65 6e 65 72 61 74 65  * would generate
4540: 20 77 61 72 6e 69 6e 67 20 6d 65 73 73 61 67 65   warning message
4550: 73 20 77 68 65 6e 20 74 68 65 79 20 77 65 72 65  s when they were
4560: 20 75 73 65 64 2e 20 20 42 75 74 20 74 68 61 74   used.  But that
4570: 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 6d 61 67  .** compiler mag
4580: 69 63 20 65 6e 64 65 64 20 75 70 20 67 65 6e 65  ic ended up gene
4590: 72 61 74 69 6e 67 20 73 75 63 68 20 61 20 66 6c  rating such a fl
45a0: 75 72 72 79 20 6f 66 20 62 75 67 20 72 65 70 6f  urry of bug repo
45b0: 72 74 73 0a 2a 2a 20 74 68 61 74 20 77 65 20 68  rts.** that we h
45c0: 61 76 65 20 74 61 6b 65 6e 20 69 74 20 61 6c 6c  ave taken it all
45d0: 20 6f 75 74 20 61 6e 64 20 67 6f 6e 65 20 62 61   out and gone ba
45e0: 63 6b 20 74 6f 20 75 73 69 6e 67 20 73 69 6d 70  ck to using simp
45f0: 6c 65 0a 2a 2a 20 6e 6f 6f 70 20 6d 61 63 72 6f  le.** noop macro
4600: 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  s..*/.#define SQ
4610: 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 0a  LITE_DEPRECATED.
4620: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45  #define SQLITE_E
4630: 58 50 45 52 49 4d 45 4e 54 41 4c 0a 0a 2f 2a 0a  XPERIMENTAL../*.
4640: 2a 2a 20 45 6e 73 75 72 65 20 74 68 65 73 65 20  ** Ensure these 
4650: 73 79 6d 62 6f 6c 73 20 77 65 72 65 20 6e 6f 74  symbols were not
4660: 20 64 65 66 69 6e 65 64 20 62 79 20 73 6f 6d 65   defined by some
4670: 20 70 72 65 76 69 6f 75 73 20 68 65 61 64 65 72   previous header
4680: 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 64 65 66   file..*/.#ifdef
4690: 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 0a   SQLITE_VERSION.
46a0: 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 56  # undef SQLITE_V
46b0: 45 52 53 49 4f 4e 0a 23 65 6e 64 69 66 0a 23 69  ERSION.#endif.#i
46c0: 66 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52 53  fdef SQLITE_VERS
46d0: 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23 20 75 6e 64  ION_NUMBER.# und
46e0: 65 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f  ef SQLITE_VERSIO
46f0: 4e 5f 4e 55 4d 42 45 52 0a 23 65 6e 64 69 66 0a  N_NUMBER.#endif.
4700: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
4710: 20 43 6f 6d 70 69 6c 65 2d 54 69 6d 65 20 4c 69   Compile-Time Li
4720: 62 72 61 72 79 20 56 65 72 73 69 6f 6e 20 4e 75  brary Version Nu
4730: 6d 62 65 72 73 20 7b 48 31 30 30 31 30 7d 20 3c  mbers {H10010} <
4740: 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S60100>.**.** Th
4750: 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e  e SQLITE_VERSION
4760: 20 61 6e 64 20 53 51 4c 49 54 45 5f 56 45 52 53   and SQLITE_VERS
4770: 49 4f 4e 5f 4e 55 4d 42 45 52 20 23 64 65 66 69  ION_NUMBER #defi
4780: 6e 65 73 20 69 6e 0a 2a 2a 20 74 68 65 20 73 71  nes in.** the sq
4790: 6c 69 74 65 33 2e 68 20 66 69 6c 65 20 73 70 65  lite3.h file spe
47a0: 63 69 66 79 20 74 68 65 20 76 65 72 73 69 6f 6e  cify the version
47b0: 20 6f 66 20 53 51 4c 69 74 65 20 77 69 74 68 20   of SQLite with 
47c0: 77 68 69 63 68 0a 2a 2a 20 74 68 61 74 20 68 65  which.** that he
47d0: 61 64 65 72 20 66 69 6c 65 20 69 73 20 61 73 73  ader file is ass
47e0: 6f 63 69 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  ociated..**.** T
47f0: 68 65 20 22 76 65 72 73 69 6f 6e 22 20 6f 66 20  he "version" of 
4800: 53 51 4c 69 74 65 20 69 73 20 61 20 73 74 72 69  SQLite is a stri
4810: 6e 67 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  ng of the form "
4820: 58 2e 59 2e 5a 22 2e 0a 2a 2a 20 54 68 65 20 70  X.Y.Z"..** The p
4830: 68 72 61 73 65 20 22 61 6c 70 68 61 22 20 6f 72  hrase "alpha" or
4840: 20 22 62 65 74 61 22 20 6d 69 67 68 74 20 62 65   "beta" might be
4850: 20 61 70 70 65 6e 64 65 64 20 61 66 74 65 72 20   appended after 
4860: 74 68 65 20 5a 2e 0a 2a 2a 20 54 68 65 20 58 20  the Z..** The X 
4870: 76 61 6c 75 65 20 69 73 20 6d 61 6a 6f 72 20 76  value is major v
4880: 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 61 6c  ersion number al
4890: 77 61 79 73 20 33 20 69 6e 20 53 51 4c 69 74 65  ways 3 in SQLite
48a0: 33 2e 0a 2a 2a 20 54 68 65 20 58 20 76 61 6c 75  3..** The X valu
48b0: 65 20 6f 6e 6c 79 20 63 68 61 6e 67 65 73 20 77  e only changes w
48c0: 68 65 6e 20 62 61 63 6b 77 61 72 64 73 20 63 6f  hen backwards co
48d0: 6d 70 61 74 69 62 69 6c 69 74 79 20 69 73 0a 2a  mpatibility is.*
48e0: 2a 20 62 72 6f 6b 65 6e 20 61 6e 64 20 77 65 20  * broken and we 
48f0: 69 6e 74 65 6e 64 20 74 6f 20 6e 65 76 65 72 20  intend to never 
4900: 62 72 65 61 6b 20 62 61 63 6b 77 61 72 64 73 20  break backwards 
4910: 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a  compatibility..*
4920: 2a 20 54 68 65 20 59 20 76 61 6c 75 65 20 69 73  * The Y value is
4930: 20 74 68 65 20 6d 69 6e 6f 72 20 76 65 72 73 69   the minor versi
4940: 6f 6e 20 6e 75 6d 62 65 72 20 61 6e 64 20 6f 6e  on number and on
4950: 6c 79 20 63 68 61 6e 67 65 73 20 77 68 65 6e 0a  ly changes when.
4960: 2a 2a 20 74 68 65 72 65 20 61 72 65 20 6d 61 6a  ** there are maj
4970: 6f 72 20 66 65 61 74 75 72 65 20 65 6e 68 61 6e  or feature enhan
4980: 63 65 6d 65 6e 74 73 20 74 68 61 74 20 61 72 65  cements that are
4990: 20 66 6f 72 77 61 72 64 73 20 63 6f 6d 70 61 74   forwards compat
49a0: 69 62 6c 65 0a 2a 2a 20 62 75 74 20 6e 6f 74 20  ible.** but not 
49b0: 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74  backwards compat
49c0: 69 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 5a 20 76  ible..** The Z v
49d0: 61 6c 75 65 20 69 73 20 74 68 65 20 72 65 6c 65  alue is the rele
49e0: 61 73 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 69  ase number and i
49f0: 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 69  s incremented wi
4a00: 74 68 0a 2a 2a 20 65 61 63 68 20 72 65 6c 65 61  th.** each relea
4a10: 73 65 20 62 75 74 20 72 65 73 65 74 73 20 62 61  se but resets ba
4a20: 63 6b 20 74 6f 20 30 20 77 68 65 6e 65 76 65 72  ck to 0 whenever
4a30: 20 59 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65   Y is incremente
4a40: 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  d..**.** See als
4a50: 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 6c 69 62 76  o: [sqlite3_libv
4a60: 65 72 73 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73  ersion()] and [s
4a70: 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
4a80: 6e 5f 6e 75 6d 62 65 72 28 29 5d 2e 0a 2a 2a 0a  n_number()]..**.
4a90: 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a  ** INVARIANTS:.*
4aa0: 2a 0a 2a 2a 20 7b 48 31 30 30 31 31 7d 20 54 68  *.** {H10011} Th
4ab0: 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e  e SQLITE_VERSION
4ac0: 20 23 64 65 66 69 6e 65 20 69 6e 20 74 68 65 20   #define in the 
4ad0: 73 71 6c 69 74 65 33 2e 68 20 68 65 61 64 65 72  sqlite3.h header
4ae0: 20 66 69 6c 65 20 73 68 61 6c 6c 0a 2a 2a 20 20   file shall.**  
4af0: 20 20 20 20 20 20 20 20 65 76 61 6c 75 61 74 65          evaluate
4b00: 20 74 6f 20 61 20 73 74 72 69 6e 67 20 6c 69 74   to a string lit
4b10: 65 72 61 6c 20 74 68 61 74 20 69 73 20 74 68 65  eral that is the
4b20: 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 0a   SQLite version.
4b30: 2a 2a 20 20 20 20 20 20 20 20 20 20 77 69 74 68  **          with
4b40: 20 77 68 69 63 68 20 74 68 65 20 68 65 61 64 65   which the heade
4b50: 72 20 66 69 6c 65 20 69 73 20 61 73 73 6f 63 69  r file is associ
4b60: 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30  ated..**.** {H10
4b70: 30 31 34 7d 20 54 68 65 20 53 51 4c 49 54 45 5f  014} The SQLITE_
4b80: 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 23  VERSION_NUMBER #
4b90: 64 65 66 69 6e 65 20 73 68 61 6c 6c 20 72 65 73  define shall res
4ba0: 6f 6c 76 65 20 74 6f 20 61 6e 20 69 6e 74 65 67  olve to an integ
4bb0: 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77  er.**          w
4bc0: 69 74 68 20 74 68 65 20 76 61 6c 75 65 20 28 58  ith the value (X
4bd0: 2a 31 30 30 30 30 30 30 20 2b 20 59 2a 31 30 30  *1000000 + Y*100
4be0: 30 20 2b 20 5a 29 20 77 68 65 72 65 20 58 2c 20  0 + Z) where X, 
4bf0: 59 2c 20 61 6e 64 20 5a 0a 2a 2a 20 20 20 20 20  Y, and Z.**     
4c00: 20 20 20 20 20 61 72 65 20 74 68 65 20 6d 61 6a       are the maj
4c10: 6f 72 20 76 65 72 73 69 6f 6e 2c 20 6d 69 6e 6f  or version, mino
4c20: 72 20 76 65 72 73 69 6f 6e 2c 20 61 6e 64 20 72  r version, and r
4c30: 65 6c 65 61 73 65 20 6e 75 6d 62 65 72 2e 0a 2a  elease number..*
4c40: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
4c50: 5f 56 45 52 53 49 4f 4e 20 20 20 20 20 20 20 20  _VERSION        
4c60: 20 22 33 2e 36 2e 34 22 0a 23 64 65 66 69 6e 65   "3.6.4".#define
4c70: 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f   SQLITE_VERSION_
4c80: 4e 55 4d 42 45 52 20 20 33 30 30 36 30 30 34 0a  NUMBER  3006004.
4c90: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
4ca0: 20 52 75 6e 2d 54 69 6d 65 20 4c 69 62 72 61 72   Run-Time Librar
4cb0: 79 20 56 65 72 73 69 6f 6e 20 4e 75 6d 62 65 72  y Version Number
4cc0: 73 20 7b 48 31 30 30 32 30 7d 20 3c 53 36 30 31  s {H10020} <S601
4cd0: 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a  00>.** KEYWORDS:
4ce0: 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e   sqlite3_version
4cf0: 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66 65 61  .**.** These fea
4d00: 74 75 72 65 73 20 70 72 6f 76 69 64 65 20 74 68  tures provide th
4d10: 65 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69  e same informati
4d20: 6f 6e 20 61 73 20 74 68 65 20 5b 53 51 4c 49 54  on as the [SQLIT
4d30: 45 5f 56 45 52 53 49 4f 4e 5d 0a 2a 2a 20 61 6e  E_VERSION].** an
4d40: 64 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f  d [SQLITE_VERSIO
4d50: 4e 5f 4e 55 4d 42 45 52 5d 20 23 64 65 66 69 6e  N_NUMBER] #defin
4d60: 65 73 20 69 6e 20 74 68 65 20 68 65 61 64 65 72  es in the header
4d70: 2c 20 62 75 74 20 61 72 65 20 61 73 73 6f 63 69  , but are associ
4d80: 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65  ated.** with the
4d90: 20 6c 69 62 72 61 72 79 20 69 6e 73 74 65 61 64   library instead
4da0: 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 20 66   of the header f
4db0: 69 6c 65 2e 20 20 43 61 75 74 69 6f 75 73 20 70  ile.  Cautious p
4dc0: 72 6f 67 72 61 6d 6d 65 72 73 20 6d 69 67 68 74  rogrammers might
4dd0: 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 61 20 63 68  .** include a ch
4de0: 65 63 6b 20 69 6e 20 74 68 65 69 72 20 61 70 70  eck in their app
4df0: 6c 69 63 61 74 69 6f 6e 20 74 6f 20 76 65 72 69  lication to veri
4e00: 66 79 20 74 68 61 74 0a 2a 2a 20 73 71 6c 69 74  fy that.** sqlit
4e10: 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75  e3_libversion_nu
4e20: 6d 62 65 72 28 29 20 61 6c 77 61 79 73 20 72 65  mber() always re
4e30: 74 75 72 6e 73 20 74 68 65 20 76 61 6c 75 65 0a  turns the value.
4e40: 2a 2a 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49  ** [SQLITE_VERSI
4e50: 4f 4e 5f 4e 55 4d 42 45 52 5d 2e 0a 2a 2a 0a 2a  ON_NUMBER]..**.*
4e60: 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 69  * The sqlite3_li
4e70: 62 76 65 72 73 69 6f 6e 28 29 20 66 75 6e 63 74  bversion() funct
4e80: 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20  ion returns the 
4e90: 73 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  same information
4ea0: 20 61 73 20 69 73 0a 2a 2a 20 69 6e 20 74 68 65   as is.** in the
4eb0: 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e   sqlite3_version
4ec0: 5b 5d 20 73 74 72 69 6e 67 20 63 6f 6e 73 74 61  [] string consta
4ed0: 6e 74 2e 20 20 54 68 65 20 66 75 6e 63 74 69 6f  nt.  The functio
4ee0: 6e 20 69 73 20 70 72 6f 76 69 64 65 64 0a 2a 2a  n is provided.**
4ef0: 20 66 6f 72 20 75 73 65 20 69 6e 20 44 4c 4c 73   for use in DLLs
4f00: 20 73 69 6e 63 65 20 44 4c 4c 20 75 73 65 72 73   since DLL users
4f10: 20 75 73 75 61 6c 6c 79 20 64 6f 20 6e 6f 74 20   usually do not 
4f20: 68 61 76 65 20 64 69 72 65 63 74 20 61 63 63 65  have direct acce
4f30: 73 73 20 74 6f 20 73 74 72 69 6e 67 0a 2a 2a 20  ss to string.** 
4f40: 63 6f 6e 73 74 61 6e 74 73 20 77 69 74 68 69 6e  constants within
4f50: 20 74 68 65 20 44 4c 4c 2e 0a 2a 2a 0a 2a 2a 20   the DLL..**.** 
4f60: 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a  INVARIANTS:.**.*
4f70: 2a 20 7b 48 31 30 30 32 31 7d 20 54 68 65 20 5b  * {H10021} The [
4f80: 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69  sqlite3_libversi
4f90: 6f 6e 5f 6e 75 6d 62 65 72 28 29 5d 20 69 6e 74  on_number()] int
4fa0: 65 72 66 61 63 65 20 73 68 61 6c 6c 20 72 65 74  erface shall ret
4fb0: 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  urn.**          
4fc0: 61 6e 20 69 6e 74 65 67 65 72 20 65 71 75 61 6c  an integer equal
4fd0: 20 74 6f 20 5b 53 51 4c 49 54 45 5f 56 45 52 53   to [SQLITE_VERS
4fe0: 49 4f 4e 5f 4e 55 4d 42 45 52 5d 2e 0a 2a 2a 0a  ION_NUMBER]..**.
4ff0: 2a 2a 20 7b 48 31 30 30 32 32 7d 20 54 68 65 20  ** {H10022} The 
5000: 5b 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e  [sqlite3_version
5010: 5d 20 73 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e  ] string constan
5020: 74 20 73 68 61 6c 6c 20 63 6f 6e 74 61 69 6e 0a  t shall contain.
5030: 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20  **          the 
5040: 74 65 78 74 20 6f 66 20 74 68 65 20 5b 53 51 4c  text of the [SQL
5050: 49 54 45 5f 56 45 52 53 49 4f 4e 5d 20 73 74 72  ITE_VERSION] str
5060: 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 30  ing..**.** {H100
5070: 32 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  23} The [sqlite3
5080: 5f 6c 69 62 76 65 72 73 69 6f 6e 28 29 5d 20 66  _libversion()] f
5090: 75 6e 63 74 69 6f 6e 20 73 68 61 6c 6c 20 72 65  unction shall re
50a0: 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  turn.**         
50b0: 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
50c0: 65 20 5b 73 71 6c 69 74 65 33 5f 76 65 72 73 69  e [sqlite3_versi
50d0: 6f 6e 5d 20 73 74 72 69 6e 67 20 63 6f 6e 73 74  on] string const
50e0: 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ant..*/.SQLITE_A
50f0: 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71  PI const char sq
5100: 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 3b  lite3_version[];
5110: 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
5120: 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  t char *sqlite3_
5130: 6c 69 62 76 65 72 73 69 6f 6e 28 76 6f 69 64 29  libversion(void)
5140: 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
5150: 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73   sqlite3_libvers
5160: 69 6f 6e 5f 6e 75 6d 62 65 72 28 76 6f 69 64 29  ion_number(void)
5170: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
5180: 46 3a 20 54 65 73 74 20 54 6f 20 53 65 65 20 49  F: Test To See I
5190: 66 20 54 68 65 20 4c 69 62 72 61 72 79 20 49 73  f The Library Is
51a0: 20 54 68 72 65 61 64 73 61 66 65 20 7b 48 31 30   Threadsafe {H10
51b0: 31 30 30 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a  100} <S60100>.**
51c0: 0a 2a 2a 20 53 51 4c 69 74 65 20 63 61 6e 20 62  .** SQLite can b
51d0: 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20  e compiled with 
51e0: 6f 72 20 77 69 74 68 6f 75 74 20 6d 75 74 65 78  or without mutex
51f0: 65 73 2e 20 20 57 68 65 6e 0a 2a 2a 20 74 68 65  es.  When.** the
5200: 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53   [SQLITE_THREADS
5210: 41 46 45 5d 20 43 20 70 72 65 70 72 6f 63 65 73  AFE] C preproces
5220: 73 6f 72 20 6d 61 63 72 6f 20 31 20 6f 72 20 32  sor macro 1 or 2
5230: 2c 20 6d 75 74 65 78 65 73 0a 2a 2a 20 61 72 65  , mutexes.** are
5240: 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 53 51 4c   enabled and SQL
5250: 69 74 65 20 69 73 20 74 68 72 65 61 64 73 61 66  ite is threadsaf
5260: 65 2e 20 20 57 68 65 6e 20 74 68 65 0a 2a 2a 20  e.  When the.** 
5270: 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41  [SQLITE_THREADSA
5280: 46 45 5d 20 6d 61 63 72 6f 20 69 73 20 30 2c 20  FE] macro is 0, 
5290: 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 65 73 20  .** the mutexes 
52a0: 61 72 65 20 6f 6d 69 74 74 65 64 2e 20 20 57 69  are omitted.  Wi
52b0: 74 68 6f 75 74 20 74 68 65 20 6d 75 74 65 78 65  thout the mutexe
52c0: 73 2c 20 69 74 20 69 73 20 6e 6f 74 20 73 61 66  s, it is not saf
52d0: 65 0a 2a 2a 20 74 6f 20 75 73 65 20 53 51 4c 69  e.** to use SQLi
52e0: 74 65 20 63 6f 6e 63 75 72 72 65 6e 74 6c 79 20  te concurrently 
52f0: 66 72 6f 6d 20 6d 6f 72 65 20 74 68 61 6e 20 6f  from more than o
5300: 6e 65 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a  ne thread..**.**
5310: 20 45 6e 61 62 6c 69 6e 67 20 6d 75 74 65 78 65   Enabling mutexe
5320: 73 20 69 6e 63 75 72 73 20 61 20 6d 65 61 73 75  s incurs a measu
5330: 72 61 62 6c 65 20 70 65 72 66 6f 72 6d 61 6e 63  rable performanc
5340: 65 20 70 65 6e 61 6c 74 79 2e 0a 2a 2a 20 53 6f  e penalty..** So
5350: 20 69 66 20 73 70 65 65 64 20 69 73 20 6f 66 20   if speed is of 
5360: 75 74 6d 6f 73 74 20 69 6d 70 6f 72 74 61 6e 63  utmost importanc
5370: 65 2c 20 69 74 20 6d 61 6b 65 73 20 73 65 6e 73  e, it makes sens
5380: 65 20 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20  e to disable.** 
5390: 74 68 65 20 6d 75 74 65 78 65 73 2e 20 20 42 75  the mutexes.  Bu
53a0: 74 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 73 61  t for maximum sa
53b0: 66 65 74 79 2c 20 6d 75 74 65 78 65 73 20 73 68  fety, mutexes sh
53c0: 6f 75 6c 64 20 62 65 20 65 6e 61 62 6c 65 64 2e  ould be enabled.
53d0: 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20  .** The default 
53e0: 62 65 68 61 76 69 6f 72 20 69 73 20 66 6f 72 20  behavior is for 
53f0: 6d 75 74 65 78 65 73 20 74 6f 20 62 65 20 65 6e  mutexes to be en
5400: 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  abled..**.** Thi
5410: 73 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20  s interface can 
5420: 62 65 20 75 73 65 64 20 62 79 20 61 20 70 72 6f  be used by a pro
5430: 67 72 61 6d 20 74 6f 20 6d 61 6b 65 20 73 75 72  gram to make sur
5440: 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 76 65  e that the.** ve
5450: 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 20  rsion of SQLite 
5460: 74 68 61 74 20 69 74 20 69 73 20 6c 69 6e 6b 69  that it is linki
5470: 6e 67 20 61 67 61 69 6e 73 74 20 77 61 73 20 63  ng against was c
5480: 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20  ompiled with.** 
5490: 74 68 65 20 64 65 73 69 72 65 64 20 73 65 74 74  the desired sett
54a0: 69 6e 67 20 6f 66 20 74 68 65 20 5b 53 51 4c 49  ing of the [SQLI
54b0: 54 45 5f 54 48 52 45 41 44 53 41 46 45 5d 20 6d  TE_THREADSAFE] m
54c0: 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  acro..**.** This
54d0: 20 69 6e 74 65 72 66 61 63 65 20 6f 6e 6c 79 20   interface only 
54e0: 72 65 70 6f 72 74 73 20 6f 6e 20 74 68 65 20 63  reports on the c
54f0: 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6d 75 74 65  ompile-time mute
5500: 78 20 73 65 74 74 69 6e 67 0a 2a 2a 20 6f 66 20  x setting.** of 
5510: 74 68 65 20 5b 53 51 4c 49 54 45 5f 54 48 52 45  the [SQLITE_THRE
5520: 41 44 53 41 46 45 5d 20 66 6c 61 67 2e 20 20 49  ADSAFE] flag.  I
5530: 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70  f SQLite is comp
5540: 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20 53 51 4c  iled with.** SQL
5550: 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 31  ITE_THREADSAFE=1
5560: 20 74 68 65 6e 20 6d 75 74 65 78 65 73 20 61 72   then mutexes ar
5570: 65 20 65 6e 61 62 6c 65 64 20 62 79 20 64 65 66  e enabled by def
5580: 61 75 6c 74 20 62 75 74 0a 2a 2a 20 63 61 6e 20  ault but.** can 
5590: 62 65 20 66 75 6c 6c 79 20 6f 72 20 70 61 72 74  be fully or part
55a0: 69 61 6c 6c 79 20 64 69 73 61 62 6c 65 64 20 75  ially disabled u
55b0: 73 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 5b  sing a call to [
55c0: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
55d0: 5d 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 76 65  ].** with the ve
55e0: 72 62 73 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46  rbs [SQLITE_CONF
55f0: 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 5d  IG_SINGLETHREAD]
5600: 2c 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  , [SQLITE_CONFIG
5610: 5f 4d 55 4c 54 49 54 48 52 45 41 44 5d 2c 0a 2a  _MULTITHREAD],.*
5620: 2a 20 6f 72 20 5b 53 51 4c 49 54 45 5f 43 4f 4e  * or [SQLITE_CON
5630: 46 49 47 5f 4d 55 54 45 58 5d 2e 20 20 54 68 65  FIG_MUTEX].  The
5640: 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66   return value of
5650: 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73   this function s
5660: 68 6f 77 73 0a 2a 2a 20 6f 6e 6c 79 20 74 68 65  hows.** only the
5670: 20 64 65 66 61 75 6c 74 20 63 6f 6d 70 69 6c 65   default compile
5680: 2d 74 69 6d 65 20 73 65 74 74 69 6e 67 2c 20 6e  -time setting, n
5690: 6f 74 20 61 6e 79 20 72 75 6e 2d 74 69 6d 65 20  ot any run-time 
56a0: 63 68 61 6e 67 65 73 0a 2a 2a 20 74 6f 20 74 68  changes.** to th
56b0: 61 74 20 73 65 74 74 69 6e 67 2e 0a 2a 2a 0a 2a  at setting..**.*
56c0: 2a 20 53 65 65 20 74 68 65 20 5b 74 68 72 65 61  * See the [threa
56d0: 64 69 6e 67 20 6d 6f 64 65 5d 20 64 6f 63 75 6d  ding mode] docum
56e0: 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64  entation for add
56f0: 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
5700: 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52  ion..**.** INVAR
5710: 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31  IANTS:.**.** {H1
5720: 30 31 30 31 7d 20 54 68 65 20 5b 73 71 6c 69 74  0101} The [sqlit
5730: 65 33 5f 74 68 72 65 61 64 73 61 66 65 28 29 5d  e3_threadsafe()]
5740: 20 66 75 6e 63 74 69 6f 6e 20 73 68 61 6c 6c 20   function shall 
5750: 72 65 74 75 72 6e 20 7a 65 72 6f 20 69 66 0a 2a  return zero if.*
5760: 2a 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 6f  *          and o
5770: 6e 6c 79 20 69 66 20 53 51 4c 69 74 65 20 77 61  nly if SQLite wa
5780: 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20  s compiled with 
5790: 6d 75 74 65 78 69 6e 67 20 63 6f 64 65 20 6f 6d  mutexing code om
57a0: 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  itted..**.** {H1
57b0: 30 31 30 32 7d 20 54 68 65 20 76 61 6c 75 65 20  0102} The value 
57c0: 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20  returned by the 
57d0: 5b 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73  [sqlite3_threads
57e0: 61 66 65 28 29 5d 20 66 75 6e 63 74 69 6f 6e 0a  afe()] function.
57f0: 2a 2a 20 20 20 20 20 20 20 20 20 20 73 68 61 6c  **          shal
5800: 6c 20 72 65 6d 61 69 6e 20 74 68 65 20 73 61 6d  l remain the sam
5810: 65 20 61 63 72 6f 73 73 20 63 61 6c 6c 73 20 74  e across calls t
5820: 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  o [sqlite3_confi
5830: 67 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  g()]..*/.SQLITE_
5840: 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
5850: 74 68 72 65 61 64 73 61 66 65 28 76 6f 69 64 29  threadsafe(void)
5860: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
5870: 46 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e  F: Database Conn
5880: 65 63 74 69 6f 6e 20 48 61 6e 64 6c 65 20 7b 48  ection Handle {H
5890: 31 32 30 30 30 7d 20 3c 53 34 30 32 30 30 3e 0a  12000} <S40200>.
58a0: 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 64 61  ** KEYWORDS: {da
58b0: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
58c0: 6e 7d 20 7b 64 61 74 61 62 61 73 65 20 63 6f 6e  n} {database con
58d0: 6e 65 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20  nections}.**.** 
58e0: 45 61 63 68 20 6f 70 65 6e 20 53 51 4c 69 74 65  Each open SQLite
58f0: 20 64 61 74 61 62 61 73 65 20 69 73 20 72 65 70   database is rep
5900: 72 65 73 65 6e 74 65 64 20 62 79 20 61 20 70 6f  resented by a po
5910: 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74  inter to an inst
5920: 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6f  ance of.** the o
5930: 70 61 71 75 65 20 73 74 72 75 63 74 75 72 65 20  paque structure 
5940: 6e 61 6d 65 64 20 22 73 71 6c 69 74 65 33 22 2e  named "sqlite3".
5950: 20 20 49 74 20 69 73 20 75 73 65 66 75 6c 20 74    It is useful t
5960: 6f 20 74 68 69 6e 6b 20 6f 66 20 61 6e 20 73 71  o think of an sq
5970: 6c 69 74 65 33 0a 2a 2a 20 70 6f 69 6e 74 65 72  lite3.** pointer
5980: 20 61 73 20 61 6e 20 6f 62 6a 65 63 74 2e 20 20   as an object.  
5990: 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  The [sqlite3_ope
59a0: 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f  n()], [sqlite3_o
59b0: 70 65 6e 31 36 28 29 5d 2c 20 61 6e 64 0a 2a 2a  pen16()], and.**
59c0: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76   [sqlite3_open_v
59d0: 32 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20  2()] interfaces 
59e0: 61 72 65 20 69 74 73 20 63 6f 6e 73 74 72 75 63  are its construc
59f0: 74 6f 72 73 2c 20 61 6e 64 20 5b 73 71 6c 69 74  tors, and [sqlit
5a00: 65 33 5f 63 6c 6f 73 65 28 29 5d 0a 2a 2a 20 69  e3_close()].** i
5a10: 73 20 69 74 73 20 64 65 73 74 72 75 63 74 6f 72  s its destructor
5a20: 2e 20 20 54 68 65 72 65 20 61 72 65 20 6d 61 6e  .  There are man
5a30: 79 20 6f 74 68 65 72 20 69 6e 74 65 72 66 61 63  y other interfac
5a40: 65 73 20 28 73 75 63 68 20 61 73 0a 2a 2a 20 5b  es (such as.** [
5a50: 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
5a60: 76 32 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f  v2()], [sqlite3_
5a70: 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
5a80: 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69  )], and.** [sqli
5a90: 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74  te3_busy_timeout
5aa0: 28 29 5d 20 74 6f 20 6e 61 6d 65 20 62 75 74 20  ()] to name but 
5ab0: 74 68 72 65 65 29 20 74 68 61 74 20 61 72 65 20  three) that are 
5ac0: 6d 65 74 68 6f 64 73 20 6f 6e 20 61 6e 0a 2a 2a  methods on an.**
5ad0: 20 73 71 6c 69 74 65 33 20 6f 62 6a 65 63 74 2e   sqlite3 object.
5ae0: 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
5af0: 63 74 20 73 71 6c 69 74 65 33 20 73 71 6c 69 74  ct sqlite3 sqlit
5b00: 65 33 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  e3;../*.** CAPI3
5b10: 52 45 46 3a 20 36 34 2d 42 69 74 20 49 6e 74 65  REF: 64-Bit Inte
5b20: 67 65 72 20 54 79 70 65 73 20 7b 48 31 30 32 30  ger Types {H1020
5b30: 30 7d 20 3c 53 31 30 31 31 30 3e 0a 2a 2a 20 4b  0} <S10110>.** K
5b40: 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 5f  EYWORDS: sqlite_
5b50: 69 6e 74 36 34 20 73 71 6c 69 74 65 5f 75 69 6e  int64 sqlite_uin
5b60: 74 36 34 0a 2a 2a 0a 2a 2a 20 42 65 63 61 75 73  t64.**.** Becaus
5b70: 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 72  e there is no cr
5b80: 6f 73 73 2d 70 6c 61 74 66 6f 72 6d 20 77 61 79  oss-platform way
5b90: 20 74 6f 20 73 70 65 63 69 66 79 20 36 34 2d 62   to specify 64-b
5ba0: 69 74 20 69 6e 74 65 67 65 72 20 74 79 70 65 73  it integer types
5bb0: 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6e 63 6c 75  .** SQLite inclu
5bc0: 64 65 73 20 74 79 70 65 64 65 66 73 20 66 6f 72  des typedefs for
5bd0: 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 61   64-bit signed a
5be0: 6e 64 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65  nd unsigned inte
5bf0: 67 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  gers..**.** The 
5c00: 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 61 6e  sqlite3_int64 an
5c10: 64 20 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34  d sqlite3_uint64
5c20: 20 61 72 65 20 74 68 65 20 70 72 65 66 65 72 72   are the preferr
5c30: 65 64 20 74 79 70 65 20 64 65 66 69 6e 69 74 69  ed type definiti
5c40: 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ons..** The sqli
5c50: 74 65 5f 69 6e 74 36 34 20 61 6e 64 20 73 71 6c  te_int64 and sql
5c60: 69 74 65 5f 75 69 6e 74 36 34 20 74 79 70 65 73  ite_uint64 types
5c70: 20 61 72 65 20 73 75 70 70 6f 72 74 65 64 20 66   are supported f
5c80: 6f 72 20 62 61 63 6b 77 61 72 64 73 0a 2a 2a 20  or backwards.** 
5c90: 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 6f 6e  compatibility on
5ca0: 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49  ly..**.** INVARI
5cb0: 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 30  ANTS:.**.** {H10
5cc0: 32 30 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  201} The [sqlite
5cd0: 5f 69 6e 74 36 34 5d 20 61 6e 64 20 5b 73 71 6c  _int64] and [sql
5ce0: 69 74 65 33 5f 69 6e 74 36 34 5d 20 74 79 70 65  ite3_int64] type
5cf0: 20 73 68 61 6c 6c 20 73 70 65 63 69 66 79 0a 2a   shall specify.*
5d00: 2a 20 20 20 20 20 20 20 20 20 20 61 20 36 34 2d  *          a 64-
5d10: 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67  bit signed integ
5d20: 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 30  er..**.** {H1020
5d30: 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 5f 75  2} The [sqlite_u
5d40: 69 6e 74 36 34 5d 20 61 6e 64 20 5b 73 71 6c 69  int64] and [sqli
5d50: 74 65 33 5f 75 69 6e 74 36 34 5d 20 74 79 70 65  te3_uint64] type
5d60: 20 73 68 61 6c 6c 20 73 70 65 63 69 66 79 0a 2a   shall specify.*
5d70: 2a 20 20 20 20 20 20 20 20 20 20 61 20 36 34 2d  *          a 64-
5d80: 62 69 74 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  bit unsigned int
5d90: 65 67 65 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  eger..*/.#ifdef 
5da0: 53 51 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 50  SQLITE_INT64_TYP
5db0: 45 0a 20 20 74 79 70 65 64 65 66 20 53 51 4c 49  E.  typedef SQLI
5dc0: 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71  TE_INT64_TYPE sq
5dd0: 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74 79  lite_int64;.  ty
5de0: 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20 53  pedef unsigned S
5df0: 51 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45  QLITE_INT64_TYPE
5e00: 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a   sqlite_uint64;.
5e10: 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d  #elif defined(_M
5e20: 53 43 5f 56 45 52 29 20 7c 7c 20 64 65 66 69 6e  SC_VER) || defin
5e30: 65 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29  ed(__BORLANDC__)
5e40: 0a 20 20 74 79 70 65 64 65 66 20 5f 5f 69 6e 74  .  typedef __int
5e50: 36 34 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b  64 sqlite_int64;
5e60: 0a 20 20 74 79 70 65 64 65 66 20 75 6e 73 69 67  .  typedef unsig
5e70: 6e 65 64 20 5f 5f 69 6e 74 36 34 20 73 71 6c 69  ned __int64 sqli
5e80: 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65 6c 73 65  te_uint64;.#else
5e90: 0a 20 20 74 79 70 65 64 65 66 20 6c 6f 6e 67 20  .  typedef long 
5ea0: 6c 6f 6e 67 20 69 6e 74 20 73 71 6c 69 74 65 5f  long int sqlite_
5eb0: 69 6e 74 36 34 3b 0a 20 20 74 79 70 65 64 65 66  int64;.  typedef
5ec0: 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c   unsigned long l
5ed0: 6f 6e 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 75  ong int sqlite_u
5ee0: 69 6e 74 36 34 3b 0a 23 65 6e 64 69 66 0a 74 79  int64;.#endif.ty
5ef0: 70 65 64 65 66 20 73 71 6c 69 74 65 5f 69 6e 74  pedef sqlite_int
5f00: 36 34 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  64 sqlite3_int64
5f10: 3b 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 65  ;.typedef sqlite
5f20: 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f  _uint64 sqlite3_
5f30: 75 69 6e 74 36 34 3b 0a 0a 2f 2a 0a 2a 2a 20 49  uint64;../*.** I
5f40: 66 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20  f compiling for 
5f50: 61 20 70 72 6f 63 65 73 73 6f 72 20 74 68 61 74  a processor that
5f60: 20 6c 61 63 6b 73 20 66 6c 6f 61 74 69 6e 67 20   lacks floating 
5f70: 70 6f 69 6e 74 20 73 75 70 70 6f 72 74 2c 0a 2a  point support,.*
5f80: 2a 20 73 75 62 73 74 69 74 75 74 65 20 69 6e 74  * substitute int
5f90: 65 67 65 72 20 66 6f 72 20 66 6c 6f 61 74 69 6e  eger for floatin
5fa0: 67 2d 70 6f 69 6e 74 2e 0a 2a 2f 0a 23 69 66 64  g-point..*/.#ifd
5fb0: 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
5fc0: 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20  LOATING_POINT.# 
5fd0: 64 65 66 69 6e 65 20 64 6f 75 62 6c 65 20 73 71  define double sq
5fe0: 6c 69 74 65 33 5f 69 6e 74 36 34 0a 23 65 6e 64  lite3_int64.#end
5ff0: 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  if../*.** CAPI3R
6000: 45 46 3a 20 43 6c 6f 73 69 6e 67 20 41 20 44 61  EF: Closing A Da
6010: 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f  tabase Connectio
6020: 6e 20 7b 48 31 32 30 31 30 7d 20 3c 53 33 30 31  n {H12010} <S301
6030: 30 30 3e 3c 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a  00><S40200>.**.*
6040: 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
6050: 73 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72  s the destructor
6060: 20 66 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65   for the [sqlite
6070: 33 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a  3] object..**.**
6080: 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68   Applications sh
6090: 6f 75 6c 64 20 5b 73 71 6c 69 74 65 33 5f 66 69  ould [sqlite3_fi
60a0: 6e 61 6c 69 7a 65 20 7c 20 66 69 6e 61 6c 69 7a  nalize | finaliz
60b0: 65 5d 20 61 6c 6c 20 5b 70 72 65 70 61 72 65 64  e] all [prepared
60c0: 20 73 74 61 74 65 6d 65 6e 74 73 5d 0a 2a 2a 20   statements].** 
60d0: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f  and [sqlite3_blo
60e0: 62 5f 63 6c 6f 73 65 20 7c 20 63 6c 6f 73 65 5d  b_close | close]
60f0: 20 61 6c 6c 20 5b 42 4c 4f 42 20 68 61 6e 64 6c   all [BLOB handl
6100: 65 73 5d 20 61 73 73 6f 63 69 61 74 65 64 20 77  es] associated w
6110: 69 74 68 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69  ith.** the [sqli
6120: 74 65 33 5d 20 6f 62 6a 65 63 74 20 70 72 69 6f  te3] object prio
6130: 72 20 74 6f 20 61 74 74 65 6d 70 74 69 6e 67 20  r to attempting 
6140: 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 6f 62 6a  to close the obj
6150: 65 63 74 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c  ect..** The [sql
6160: 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 29  ite3_next_stmt()
6170: 5d 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20  ] interface can 
6180: 62 65 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74  be used to locat
6190: 65 20 61 6c 6c 0a 2a 2a 20 5b 70 72 65 70 61 72  e all.** [prepar
61a0: 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 20 61  ed statements] a
61b0: 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
61c0: 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
61d0: 63 74 69 6f 6e 5d 20 69 66 20 64 65 73 69 72 65  ction] if desire
61e0: 64 2e 0a 2a 2a 20 54 79 70 69 63 61 6c 20 63 6f  d..** Typical co
61f0: 64 65 20 6d 69 67 68 74 20 6c 6f 6f 6b 20 6c 69  de might look li
6200: 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c  ke this:.**.** <
6210: 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e  blockquote><pre>
6220: 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  .** sqlite3_stmt
6230: 20 2a 70 53 74 6d 74 3b 0a 2a 2a 20 77 68 69 6c   *pStmt;.** whil
6240: 65 28 20 28 70 53 74 6d 74 20 3d 20 73 71 6c 69  e( (pStmt = sqli
6250: 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 64 62  te3_next_stmt(db
6260: 2c 20 30 29 29 21 3d 30 20 29 7b 0a 2a 2a 20 26  , 0))!=0 ){.** &
6270: 6e 62 73 70 3b 20 20 20 73 71 6c 69 74 65 33 5f  nbsp;   sqlite3_
6280: 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b  finalize(pStmt);
6290: 0a 2a 2a 20 7d 0a 2a 2a 20 3c 2f 70 72 65 3e 3c  .** }.** </pre><
62a0: 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a  /blockquote>.**.
62b0: 2a 2a 20 49 66 20 5b 73 71 6c 69 74 65 33 5f 63  ** If [sqlite3_c
62c0: 6c 6f 73 65 28 29 5d 20 69 73 20 69 6e 76 6f 6b  lose()] is invok
62d0: 65 64 20 77 68 69 6c 65 20 61 20 74 72 61 6e 73  ed while a trans
62e0: 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 2c 0a  action is open,.
62f0: 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ** the transacti
6300: 6f 6e 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61  on is automatica
6310: 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e  lly rolled back.
6320: 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54  .**.** INVARIANT
6330: 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 30 31 31  S:.**.** {H12011
6340: 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63  } A successful c
6350: 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
6360: 63 6c 6f 73 65 28 43 29 5d 20 73 68 61 6c 6c 20  close(C)] shall 
6370: 64 65 73 74 72 6f 79 20 74 68 65 0a 2a 2a 20 20  destroy the.**  
6380: 20 20 20 20 20 20 20 20 5b 64 61 74 61 62 61 73          [databas
6390: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6f 62  e connection] ob
63a0: 6a 65 63 74 20 43 2e 0a 2a 2a 0a 2a 2a 20 7b 48  ject C..**.** {H
63b0: 31 32 30 31 32 7d 20 41 20 73 75 63 63 65 73 73  12012} A success
63c0: 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c  ful call to [sql
63d0: 69 74 65 33 5f 63 6c 6f 73 65 28 43 29 5d 20 73  ite3_close(C)] s
63e0: 68 61 6c 6c 20 72 65 74 75 72 6e 20 53 51 4c 49  hall return SQLI
63f0: 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  TE_OK..**.** {H1
6400: 32 30 31 33 7d 20 41 20 73 75 63 63 65 73 73 66  2013} A successf
6410: 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69  ul call to [sqli
6420: 74 65 33 5f 63 6c 6f 73 65 28 43 29 5d 20 73 68  te3_close(C)] sh
6430: 61 6c 6c 20 72 65 6c 65 61 73 65 20 61 6c 6c 0a  all release all.
6440: 2a 2a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 6f  **          memo
6450: 72 79 20 61 6e 64 20 73 79 73 74 65 6d 20 72 65  ry and system re
6460: 73 6f 75 72 63 65 73 20 61 73 73 6f 63 69 61 74  sources associat
6470: 65 64 20 77 69 74 68 20 5b 64 61 74 61 62 61 73  ed with [databas
6480: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a  e connection].**
6490: 20 20 20 20 20 20 20 20 20 20 43 2e 0a 2a 2a 0a            C..**.
64a0: 2a 2a 20 7b 48 31 32 30 31 34 7d 20 41 20 63 61  ** {H12014} A ca
64b0: 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63  ll to [sqlite3_c
64c0: 6c 6f 73 65 28 43 29 5d 20 6f 6e 20 61 20 5b 64  lose(C)] on a [d
64d0: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
64e0: 6f 6e 5d 20 43 20 74 68 61 74 0a 2a 2a 20 20 20  on] C that.**   
64f0: 20 20 20 20 20 20 20 68 61 73 20 6f 6e 65 20 6f         has one o
6500: 72 20 6d 6f 72 65 20 6f 70 65 6e 20 5b 70 72 65  r more open [pre
6510: 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73  pared statements
6520: 5d 20 73 68 61 6c 6c 20 66 61 69 6c 20 77 69 74  ] shall fail wit
6530: 68 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e  h.**          an
6540: 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 65   [SQLITE_BUSY] e
6550: 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a  rror code..**.**
6560: 20 7b 48 31 32 30 31 35 7d 20 41 20 63 61 6c 6c   {H12015} A call
6570: 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f   to [sqlite3_clo
6580: 73 65 28 43 29 5d 20 77 68 65 72 65 20 43 20 69  se(C)] where C i
6590: 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  s a NULL pointer
65a0: 20 73 68 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20   shall.**       
65b0: 20 20 20 62 65 20 61 20 68 61 72 6d 6c 65 73 73     be a harmless
65c0: 20 6e 6f 2d 6f 70 20 72 65 74 75 72 6e 69 6e 67   no-op returning
65d0: 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a   SQLITE_OK..**.*
65e0: 2a 20 7b 48 31 32 30 31 39 7d 20 57 68 65 6e 20  * {H12019} When 
65f0: 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 43  [sqlite3_close(C
6600: 29 5d 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e  )] is invoked on
6610: 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e   a [database con
6620: 6e 65 63 74 69 6f 6e 5d 20 43 0a 2a 2a 20 20 20  nection] C.**   
6630: 20 20 20 20 20 20 20 74 68 61 74 20 68 61 73 20         that has 
6640: 61 20 70 65 6e 64 69 6e 67 20 74 72 61 6e 73 61  a pending transa
6650: 63 74 69 6f 6e 2c 20 74 68 65 20 74 72 61 6e 73  ction, the trans
6660: 61 63 74 69 6f 6e 20 73 68 61 6c 6c 20 62 65 0a  action shall be.
6670: 2a 2a 20 20 20 20 20 20 20 20 20 20 72 6f 6c 6c  **          roll
6680: 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 41  ed back..**.** A
6690: 53 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a  SSUMPTIONS:.**.*
66a0: 2a 20 7b 41 31 32 30 31 36 7d 20 54 68 65 20 43  * {A12016} The C
66b0: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73   parameter to [s
66c0: 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 43 29 5d  qlite3_close(C)]
66d0: 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20   must be either 
66e0: 61 20 4e 55 4c 4c 0a 2a 2a 20 20 20 20 20 20 20  a NULL.**       
66f0: 20 20 20 70 6f 69 6e 74 65 72 20 6f 72 20 61 6e     pointer or an
6700: 20 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63   [sqlite3] objec
6710: 74 20 70 6f 69 6e 74 65 72 20 6f 62 74 61 69 6e  t pointer obtain
6720: 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66  ed.**          f
6730: 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  rom [sqlite3_ope
6740: 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f  n()], [sqlite3_o
6750: 70 65 6e 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20  pen16()], or.** 
6760: 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
6770: 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2c 20 61 6e  3_open_v2()], an
6780: 64 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79  d not previously
6790: 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49   closed..*/.SQLI
67a0: 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
67b0: 65 33 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33  e3_close(sqlite3
67c0: 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20   *);../*.** The 
67d0: 74 79 70 65 20 66 6f 72 20 61 20 63 61 6c 6c 62  type for a callb
67e0: 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a  ack function..**
67f0: 20 54 68 69 73 20 69 73 20 6c 65 67 61 63 79 20   This is legacy 
6800: 61 6e 64 20 64 65 70 72 65 63 61 74 65 64 2e 20  and deprecated. 
6810: 20 49 74 20 69 73 20 69 6e 63 6c 75 64 65 64 20   It is included 
6820: 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 0a 2a  for historical.*
6830: 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20  * compatibility 
6840: 61 6e 64 20 69 73 20 6e 6f 74 20 64 6f 63 75 6d  and is not docum
6850: 65 6e 74 65 64 2e 0a 2a 2f 0a 74 79 70 65 64 65  ented..*/.typede
6860: 66 20 69 6e 74 20 28 2a 73 71 6c 69 74 65 33 5f  f int (*sqlite3_
6870: 63 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c  callback)(void*,
6880: 69 6e 74 2c 63 68 61 72 2a 2a 2c 20 63 68 61 72  int,char**, char
6890: 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  **);../*.** CAPI
68a0: 33 52 45 46 3a 20 4f 6e 65 2d 53 74 65 70 20 51  3REF: One-Step Q
68b0: 75 65 72 79 20 45 78 65 63 75 74 69 6f 6e 20 49  uery Execution I
68c0: 6e 74 65 72 66 61 63 65 20 7b 48 31 32 31 30 30  nterface {H12100
68d0: 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a  } <S10000>.**.**
68e0: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65   The sqlite3_exe
68f0: 63 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73  c() interface is
6900: 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20 77 61   a convenient wa
6910: 79 20 6f 66 20 72 75 6e 6e 69 6e 67 20 6f 6e 65  y of running one
6920: 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 53 51 4c 20   or more.** SQL 
6930: 73 74 61 74 65 6d 65 6e 74 73 20 77 69 74 68 6f  statements witho
6940: 75 74 20 68 61 76 69 6e 67 20 74 6f 20 77 72 69  ut having to wri
6950: 74 65 20 61 20 6c 6f 74 20 6f 66 20 43 20 63 6f  te a lot of C co
6960: 64 65 2e 20 20 54 68 65 20 55 54 46 2d 38 20 65  de.  The UTF-8 e
6970: 6e 63 6f 64 65 64 0a 2a 2a 20 53 51 4c 20 73 74  ncoded.** SQL st
6980: 61 74 65 6d 65 6e 74 73 20 61 72 65 20 70 61 73  atements are pas
6990: 73 65 64 20 69 6e 20 61 73 20 74 68 65 20 73 65  sed in as the se
69a0: 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74  cond parameter t
69b0: 6f 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29  o sqlite3_exec()
69c0: 2e 0a 2a 2a 20 54 68 65 20 73 74 61 74 65 6d 65  ..** The stateme
69d0: 6e 74 73 20 61 72 65 20 65 76 61 6c 75 61 74 65  nts are evaluate
69e0: 64 20 6f 6e 65 20 62 79 20 6f 6e 65 20 75 6e 74  d one by one unt
69f0: 69 6c 20 65 69 74 68 65 72 20 61 6e 20 65 72 72  il either an err
6a00: 6f 72 20 6f 72 0a 2a 2a 20 61 6e 20 69 6e 74 65  or or.** an inte
6a10: 72 72 75 70 74 20 69 73 20 65 6e 63 6f 75 6e 74  rrupt is encount
6a20: 65 72 65 64 2c 20 6f 72 20 75 6e 74 69 6c 20 74  ered, or until t
6a30: 68 65 79 20 61 72 65 20 61 6c 6c 20 64 6f 6e 65  hey are all done
6a40: 2e 20 20 54 68 65 20 33 72 64 20 70 61 72 61 6d  .  The 3rd param
6a50: 65 74 65 72 0a 2a 2a 20 69 73 20 61 6e 20 6f 70  eter.** is an op
6a60: 74 69 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 20  tional callback 
6a70: 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 20  that is invoked 
6a80: 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 72 6f  once for each ro
6a90: 77 20 6f 66 20 61 6e 79 20 71 75 65 72 79 0a 2a  w of any query.*
6aa0: 2a 20 72 65 73 75 6c 74 73 20 70 72 6f 64 75 63  * results produc
6ab0: 65 64 20 62 79 20 74 68 65 20 53 51 4c 20 73 74  ed by the SQL st
6ac0: 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 65 20 35  atements.  The 5
6ad0: 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 65 6c  th parameter tel
6ae0: 6c 73 20 77 68 65 72 65 0a 2a 2a 20 74 6f 20 77  ls where.** to w
6af0: 72 69 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d  rite any error m
6b00: 65 73 73 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 54  essages..**.** T
6b10: 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
6b20: 20 70 61 73 73 65 64 20 62 61 63 6b 20 74 68 72   passed back thr
6b30: 6f 75 67 68 20 74 68 65 20 35 74 68 20 70 61 72  ough the 5th par
6b40: 61 6d 65 74 65 72 20 69 73 20 68 65 6c 64 0a 2a  ameter is held.*
6b50: 2a 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61  * in memory obta
6b60: 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74  ined from [sqlit
6b70: 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 54  e3_malloc()].  T
6b80: 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d 6f 72 79  o avoid a memory
6b90: 20 6c 65 61 6b 2c 0a 2a 2a 20 74 68 65 20 63 61   leak,.** the ca
6ba0: 6c 6c 69 6e 67 20 61 70 70 6c 69 63 61 74 69 6f  lling applicatio
6bb0: 6e 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 5b 73  n should call [s
6bc0: 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20 6f  qlite3_free()] o
6bd0: 6e 20 61 6e 79 20 65 72 72 6f 72 0a 2a 2a 20 6d  n any error.** m
6be0: 65 73 73 61 67 65 20 72 65 74 75 72 6e 65 64 20  essage returned 
6bf0: 74 68 72 6f 75 67 68 20 74 68 65 20 35 74 68 20  through the 5th 
6c00: 70 61 72 61 6d 65 74 65 72 20 77 68 65 6e 20 69  parameter when i
6c10: 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 75  t has finished u
6c20: 73 69 6e 67 0a 2a 2a 20 74 68 65 20 65 72 72 6f  sing.** the erro
6c30: 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a  r message..**.**
6c40: 20 49 66 20 74 68 65 20 53 51 4c 20 73 74 61 74   If the SQL stat
6c50: 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 32 6e 64  ement in the 2nd
6c60: 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 4e 55   parameter is NU
6c70: 4c 4c 20 6f 72 20 61 6e 20 65 6d 70 74 79 20 73  LL or an empty s
6c80: 74 72 69 6e 67 0a 2a 2a 20 6f 72 20 61 20 73 74  tring.** or a st
6c90: 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ring containing 
6ca0: 6f 6e 6c 79 20 77 68 69 74 65 73 70 61 63 65 20  only whitespace 
6cb0: 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 2c 20 74 68  and comments, th
6cc0: 65 6e 20 6e 6f 20 53 51 4c 0a 2a 2a 20 73 74 61  en no SQL.** sta
6cd0: 74 65 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c  tements are eval
6ce0: 75 61 74 65 64 20 61 6e 64 20 74 68 65 20 64 61  uated and the da
6cf0: 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 63 68  tabase is not ch
6d00: 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  anged..**.** The
6d10: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20   sqlite3_exec() 
6d20: 69 6e 74 65 72 66 61 63 65 20 69 73 20 69 6d 70  interface is imp
6d30: 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74 65 72 6d  lemented in term
6d40: 73 20 6f 66 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  s of.** [sqlite3
6d50: 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 20  _prepare_v2()], 
6d60: 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
6d70: 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 66  , and [sqlite3_f
6d80: 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 20 54  inalize()]..** T
6d90: 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  he sqlite3_exec(
6da0: 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e  ) routine does n
6db0: 6f 74 68 69 6e 67 20 74 6f 20 74 68 65 20 64 61  othing to the da
6dc0: 74 61 62 61 73 65 20 74 68 61 74 20 63 61 6e 6e  tabase that cann
6dd0: 6f 74 20 62 65 20 64 6f 6e 65 0a 2a 2a 20 62 79  ot be done.** by
6de0: 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
6df0: 65 5f 76 32 28 29 5d 2c 20 5b 73 71 6c 69 74 65  e_v2()], [sqlite
6e00: 33 5f 73 74 65 70 28 29 5d 2c 20 61 6e 64 20 5b  3_step()], and [
6e10: 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
6e20: 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52  ()]..**.** INVAR
6e30: 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31  IANTS:.**.** {H1
6e40: 32 31 30 31 7d 20 41 20 73 75 63 63 65 73 73 66  2101} A successf
6e50: 75 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66  ul invocation of
6e60: 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 44   [sqlite3_exec(D
6e70: 2c 53 2c 43 2c 41 2c 45 29 5d 0a 2a 2a 20 20 20  ,S,C,A,E)].**   
6e80: 20 20 20 20 20 20 20 73 68 61 6c 6c 20 73 65 71         shall seq
6e90: 75 65 6e 74 69 61 6c 6c 79 20 65 76 61 6c 75 61  uentially evalua
6ea0: 74 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 55 54  te all of the UT
6eb0: 46 2d 38 20 65 6e 63 6f 64 65 64 2c 0a 2a 2a 20  F-8 encoded,.** 
6ec0: 20 20 20 20 20 20 20 20 20 73 65 6d 69 63 6f 6c           semicol
6ed0: 6f 6e 2d 73 65 70 61 72 61 74 65 64 20 53 51 4c  on-separated SQL
6ee0: 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74   statements in t
6ef0: 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74  he zero-terminat
6f00: 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73  ed.**          s
6f10: 74 72 69 6e 67 20 53 20 77 69 74 68 69 6e 20 74  tring S within t
6f20: 68 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 68  he context of th
6f30: 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
6f40: 65 63 74 69 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a  ection] D..**.**
6f50: 20 7b 48 31 32 31 30 32 7d 20 49 66 20 74 68 65   {H12102} If the
6f60: 20 53 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20   S parameter to 
6f70: 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 44 2c  [sqlite3_exec(D,
6f80: 53 2c 43 2c 41 2c 45 29 5d 20 69 73 20 4e 55 4c  S,C,A,E)] is NUL
6f90: 4c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20  L then.**       
6fa0: 20 20 20 74 68 65 20 61 63 74 69 6f 6e 73 20 6f     the actions o
6fb0: 66 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20  f the interface 
6fc0: 73 68 61 6c 6c 20 62 65 20 74 68 65 20 73 61 6d  shall be the sam
6fd0: 65 20 61 73 20 69 66 20 74 68 65 0a 2a 2a 20 20  e as if the.**  
6fe0: 20 20 20 20 20 20 20 20 53 20 70 61 72 61 6d 65          S parame
6ff0: 74 65 72 20 77 65 72 65 20 61 6e 20 65 6d 70 74  ter were an empt
7000: 79 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  y string..**.** 
7010: 7b 48 31 32 31 30 34 7d 20 54 68 65 20 72 65 74  {H12104} The ret
7020: 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 5b 73 71  urn value of [sq
7030: 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 73 68  lite3_exec()] sh
7040: 61 6c 6c 20 62 65 20 5b 53 51 4c 49 54 45 5f 4f  all be [SQLITE_O
7050: 4b 5d 20 69 66 20 61 6c 6c 0a 2a 2a 20 20 20 20  K] if all.**    
7060: 20 20 20 20 20 20 53 51 4c 20 73 74 61 74 65 6d        SQL statem
7070: 65 6e 74 73 20 72 75 6e 20 73 75 63 63 65 73 73  ents run success
7080: 66 75 6c 6c 79 20 61 6e 64 20 74 6f 20 63 6f 6d  fully and to com
7090: 70 6c 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b  pletion..**.** {
70a0: 48 31 32 31 30 35 7d 20 54 68 65 20 72 65 74 75  H12105} The retu
70b0: 72 6e 20 76 61 6c 75 65 20 6f 66 20 5b 73 71 6c  rn value of [sql
70c0: 69 74 65 33 5f 65 78 65 63 28 29 5d 20 73 68 61  ite3_exec()] sha
70d0: 6c 6c 20 62 65 20 61 6e 20 61 70 70 72 6f 70 72  ll be an appropr
70e0: 69 61 74 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  iate.**         
70f0: 20 6e 6f 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f 72   non-zero [error
7100: 20 63 6f 64 65 5d 20 69 66 20 61 6e 79 20 53 51   code] if any SQ
7110: 4c 20 73 74 61 74 65 6d 65 6e 74 20 66 61 69 6c  L statement fail
7120: 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 31 30 37  s..**.** {H12107
7130: 7d 20 49 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  } If one or more
7140: 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61 74   of the SQL stat
7150: 65 6d 65 6e 74 73 20 68 61 6e 64 65 64 20 74 6f  ements handed to
7160: 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29   [sqlite3_exec()
7170: 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65  ].**          re
7180: 74 75 72 6e 20 72 65 73 75 6c 74 73 20 61 6e 64  turn results and
7190: 20 74 68 65 20 33 72 64 20 70 61 72 61 6d 65 74   the 3rd paramet
71a0: 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  er is not NULL, 
71b0: 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  then.**         
71c0: 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75   the callback fu
71d0: 6e 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64  nction specified
71e0: 20 62 79 20 74 68 65 20 33 72 64 20 70 61 72 61   by the 3rd para
71f0: 6d 65 74 65 72 20 73 68 61 6c 6c 20 62 65 0a 2a  meter shall be.*
7200: 2a 20 20 20 20 20 20 20 20 20 20 69 6e 76 6f 6b  *          invok
7210: 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ed once for each
7220: 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 2e 0a   row of result..
7230: 2a 2a 0a 2a 2a 20 7b 48 31 32 31 31 30 7d 20 49  **.** {H12110} I
7240: 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72  f the callback r
7250: 65 74 75 72 6e 73 20 61 20 6e 6f 6e 2d 7a 65 72  eturns a non-zer
7260: 6f 20 76 61 6c 75 65 20 74 68 65 6e 20 5b 73 71  o value then [sq
7270: 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 0a 2a 2a  lite3_exec()].**
7280: 20 20 20 20 20 20 20 20 20 20 73 68 61 6c 6c 20            shall 
7290: 61 62 6f 72 74 20 74 68 65 20 53 51 4c 20 73 74  abort the SQL st
72a0: 61 74 65 6d 65 6e 74 20 69 74 20 69 73 20 63 75  atement it is cu
72b0: 72 72 65 6e 74 6c 79 20 65 76 61 6c 75 61 74 69  rrently evaluati
72c0: 6e 67 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ng,.**          
72d0: 73 6b 69 70 20 61 6c 6c 20 73 75 62 73 65 71 75  skip all subsequ
72e0: 65 6e 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ent SQL statemen
72f0: 74 73 2c 20 61 6e 64 20 72 65 74 75 72 6e 20 5b  ts, and return [
7300: 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a  SQLITE_ABORT]..*
7310: 2a 0a 2a 2a 20 7b 48 31 32 31 31 33 7d 20 54 68  *.** {H12113} Th
7320: 65 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28  e [sqlite3_exec(
7330: 29 5d 20 72 6f 75 74 69 6e 65 20 73 68 61 6c 6c  )] routine shall
7340: 20 70 61 73 73 20 69 74 73 20 34 74 68 20 70 61   pass its 4th pa
7350: 72 61 6d 65 74 65 72 20 74 68 72 6f 75 67 68 0a  rameter through.
7360: 2a 2a 20 20 20 20 20 20 20 20 20 20 61 73 20 74  **          as t
7370: 68 65 20 31 73 74 20 70 61 72 61 6d 65 74 65 72  he 1st parameter
7380: 20 6f 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   of the callback
7390: 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 31 31 36 7d  ..**.** {H12116}
73a0: 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 65 78   The [sqlite3_ex
73b0: 65 63 28 29 5d 20 72 6f 75 74 69 6e 65 20 73 68  ec()] routine sh
73c0: 61 6c 6c 20 73 65 74 20 74 68 65 20 32 6e 64 20  all set the 2nd 
73d0: 70 61 72 61 6d 65 74 65 72 20 6f 66 20 69 74 73  parameter of its
73e0: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 61 6c  .**          cal
73f0: 6c 62 61 63 6b 20 74 6f 20 62 65 20 74 68 65 20  lback to be the 
7400: 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
7410: 73 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74  s in the current
7420: 20 72 6f 77 20 6f 66 0a 2a 2a 20 20 20 20 20 20   row of.**      
7430: 20 20 20 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a      result..**.*
7440: 2a 20 7b 48 31 32 31 31 39 7d 20 54 68 65 20 5b  * {H12119} The [
7450: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20  sqlite3_exec()] 
7460: 72 6f 75 74 69 6e 65 20 73 68 61 6c 6c 20 73 65  routine shall se
7470: 74 20 74 68 65 20 33 72 64 20 70 61 72 61 6d 65  t the 3rd parame
7480: 74 65 72 20 6f 66 20 69 74 73 0a 2a 2a 20 20 20  ter of its.**   
7490: 20 20 20 20 20 20 20 63 61 6c 6c 62 61 63 6b 20         callback 
74a0: 74 6f 20 62 65 20 61 6e 20 61 72 72 61 79 20 6f  to be an array o
74b0: 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 73 74  f pointers to st
74c0: 72 69 6e 67 73 20 68 6f 6c 64 69 6e 67 20 74 68  rings holding th
74d0: 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 76 61  e.**          va
74e0: 6c 75 65 73 20 66 6f 72 20 65 61 63 68 20 63 6f  lues for each co
74f0: 6c 75 6d 6e 20 69 6e 20 74 68 65 20 63 75 72 72  lumn in the curr
7500: 65 6e 74 20 72 65 73 75 6c 74 20 73 65 74 20 72  ent result set r
7510: 6f 77 20 61 73 0a 2a 2a 20 20 20 20 20 20 20 20  ow as.**        
7520: 20 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20    obtained from 
7530: 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  [sqlite3_column_
7540: 74 65 78 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b  text()]..**.** {
7550: 48 31 32 31 32 32 7d 20 54 68 65 20 5b 73 71 6c  H12122} The [sql
7560: 69 74 65 33 5f 65 78 65 63 28 29 5d 20 72 6f 75  ite3_exec()] rou
7570: 74 69 6e 65 20 73 68 61 6c 6c 20 73 65 74 20 74  tine shall set t
7580: 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72  he 4th parameter
7590: 20 6f 66 20 69 74 73 0a 2a 2a 20 20 20 20 20 20   of its.**      
75a0: 20 20 20 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20      callback to 
75b0: 62 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70  be an array of p
75c0: 6f 69 6e 74 65 72 73 20 74 6f 20 73 74 72 69 6e  ointers to strin
75d0: 67 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a 2a  gs holding the.*
75e0: 2a 20 20 20 20 20 20 20 20 20 20 6e 61 6d 65 73  *          names
75f0: 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d   of result colum
7600: 6e 73 20 61 73 20 6f 62 74 61 69 6e 65 64 20 66  ns as obtained f
7610: 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c  rom [sqlite3_col
7620: 75 6d 6e 5f 6e 61 6d 65 28 29 5d 2e 0a 2a 2a 0a  umn_name()]..**.
7630: 2a 2a 20 7b 48 31 32 31 32 35 7d 20 49 66 20 74  ** {H12125} If t
7640: 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72  he 3rd parameter
7650: 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65   to [sqlite3_exe
7660: 63 28 29 5d 20 69 73 20 4e 55 4c 4c 20 74 68 65  c()] is NULL the
7670: 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73  n.**          [s
7680: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 73  qlite3_exec()] s
7690: 68 61 6c 6c 20 73 69 6c 65 6e 74 6c 79 20 64 69  hall silently di
76a0: 73 63 61 72 64 20 71 75 65 72 79 20 72 65 73 75  scard query resu
76b0: 6c 74 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 31  lts..**.** {H121
76c0: 33 31 7d 20 49 66 20 61 6e 20 65 72 72 6f 72 20  31} If an error 
76d0: 6f 63 63 75 72 73 20 77 68 69 6c 65 20 70 61 72  occurs while par
76e0: 73 69 6e 67 20 6f 72 20 65 76 61 6c 75 61 74 69  sing or evaluati
76f0: 6e 67 20 61 6e 79 20 6f 66 20 74 68 65 20 53 51  ng any of the SQ
7700: 4c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74  L.**          st
7710: 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20  atements in the 
7720: 53 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 5b  S parameter of [
7730: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 44 2c 53  sqlite3_exec(D,S
7740: 2c 43 2c 41 2c 45 29 5d 20 61 6e 64 20 69 66 0a  ,C,A,E)] and if.
7750: 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20  **          the 
7760: 45 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e  E parameter is n
7770: 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 5b 73  ot NULL, then [s
7780: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 73  qlite3_exec()] s
7790: 68 61 6c 6c 20 73 74 6f 72 65 0a 2a 2a 20 20 20  hall store.**   
77a0: 20 20 20 20 20 20 20 69 6e 20 2a 45 20 61 6e 20         in *E an 
77b0: 61 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f  appropriate erro
77c0: 72 20 6d 65 73 73 61 67 65 20 77 72 69 74 74 65  r message writte
77d0: 6e 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62  n into memory ob
77e0: 74 61 69 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20  tained.**       
77f0: 20 20 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33     from [sqlite3
7800: 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2a 0a 2a  _malloc()]..**.*
7810: 2a 20 7b 48 31 32 31 33 34 7d 20 54 68 65 20 5b  * {H12134} The [
7820: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 44 2c 53  sqlite3_exec(D,S
7830: 2c 43 2c 41 2c 45 29 5d 20 72 6f 75 74 69 6e 65  ,C,A,E)] routine
7840: 20 73 68 61 6c 6c 20 73 65 74 20 74 68 65 20 76   shall set the v
7850: 61 6c 75 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20  alue of.**      
7860: 20 20 20 20 2a 45 20 74 6f 20 4e 55 4c 4c 20 69      *E to NULL i
7870: 66 20 45 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20  f E is not NULL 
7880: 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 6e 6f  and there are no
7890: 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 7b   errors..**.** {
78a0: 48 31 32 31 33 37 7d 20 54 68 65 20 5b 73 71 6c  H12137} The [sql
78b0: 69 74 65 33 5f 65 78 65 63 28 44 2c 53 2c 43 2c  ite3_exec(D,S,C,
78c0: 41 2c 45 29 5d 20 66 75 6e 63 74 69 6f 6e 20 73  A,E)] function s
78d0: 68 61 6c 6c 20 73 65 74 20 74 68 65 20 5b 65 72  hall set the [er
78e0: 72 6f 72 20 63 6f 64 65 5d 0a 2a 2a 20 20 20 20  ror code].**    
78f0: 20 20 20 20 20 20 61 6e 64 20 6d 65 73 73 61 67        and messag
7900: 65 20 61 63 63 65 73 73 69 62 6c 65 20 76 69 61  e accessible via
7910: 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64   [sqlite3_errcod
7920: 65 28 29 5d 2c 20 0a 2a 2a 20 20 20 20 20 20 20  e()], .**       
7930: 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 65     [sqlite3_exte
7940: 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 29 5d 2c  nded_errcode()],
7950: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71  .**          [sq
7960: 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2c  lite3_errmsg()],
7970: 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65 72   and [sqlite3_er
7980: 72 6d 73 67 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a  rmsg16()]..**.**
7990: 20 7b 48 31 32 31 33 38 7d 20 49 66 20 74 68 65   {H12138} If the
79a0: 20 53 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20   S parameter to 
79b0: 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 44 2c  [sqlite3_exec(D,
79c0: 53 2c 43 2c 41 2c 45 29 5d 20 69 73 20 4e 55 4c  S,C,A,E)] is NUL
79d0: 4c 20 6f 72 20 61 6e 0a 2a 2a 20 20 20 20 20 20  L or an.**      
79e0: 20 20 20 20 65 6d 70 74 79 20 73 74 72 69 6e 67      empty string
79f0: 20 6f 72 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 74   or contains not
7a00: 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20  hing other than 
7a10: 77 68 69 74 65 73 70 61 63 65 2c 20 63 6f 6d 6d  whitespace, comm
7a20: 65 6e 74 73 2c 0a 2a 2a 20 20 20 20 20 20 20 20  ents,.**        
7a30: 20 20 61 6e 64 2f 6f 72 20 73 65 6d 69 63 6f 6c    and/or semicol
7a40: 6f 6e 73 2c 20 74 68 65 6e 20 72 65 73 75 6c 74  ons, then result
7a50: 73 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 65 72  s of [sqlite3_er
7a60: 72 63 6f 64 65 28 29 5d 2c 0a 2a 2a 20 20 20 20  rcode()],.**    
7a70: 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65        [sqlite3_e
7a80: 78 74 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 28  xtended_errcode(
7a90: 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  )],.**          
7aa0: 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28  [sqlite3_errmsg(
7ab0: 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  )], and [sqlite3
7ac0: 5f 65 72 72 6d 73 67 31 36 28 29 5d 0a 2a 2a 20  _errmsg16()].** 
7ad0: 20 20 20 20 20 20 20 20 20 73 68 61 6c 6c 20 72           shall r
7ae0: 65 73 65 74 20 74 6f 20 69 6e 64 69 63 61 74 65  eset to indicate
7af0: 20 6e 6f 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a   no errors..**.*
7b00: 2a 20 41 53 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a  * ASSUMPTIONS:.*
7b10: 2a 0a 2a 2a 20 7b 41 31 32 31 34 31 7d 20 54 68  *.** {A12141} Th
7b20: 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
7b30: 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78  r to [sqlite3_ex
7b40: 65 63 28 29 5d 20 6d 75 73 74 20 62 65 20 61 6e  ec()] must be an
7b50: 20 76 61 6c 69 64 20 61 6e 64 20 6f 70 65 6e 0a   valid and open.
7b60: 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 64 61 74  **          [dat
7b70: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
7b80: 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 31 34 32  ]..**.** {A12142
7b90: 7d 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  } The database c
7ba0: 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e  onnection must n
7bb0: 6f 74 20 62 65 20 63 6c 6f 73 65 64 20 77 68 69  ot be closed whi
7bc0: 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  le.**          [
7bd0: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20  sqlite3_exec()] 
7be0: 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a  is running..**.*
7bf0: 2a 20 7b 41 31 32 31 34 33 7d 20 54 68 65 20 63  * {A12143} The c
7c00: 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  alling function 
7c10: 73 68 6f 75 6c 64 20 75 73 65 20 5b 73 71 6c 69  should use [sqli
7c20: 74 65 33 5f 66 72 65 65 28 29 5d 20 74 6f 20 66  te3_free()] to f
7c30: 72 65 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ree.**          
7c40: 74 68 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20  the memory that 
7c50: 2a 65 72 72 6d 73 67 20 69 73 20 6c 65 66 74 20  *errmsg is left 
7c60: 70 6f 69 6e 74 69 6e 67 20 61 74 20 6f 6e 63 65  pointing at once
7c70: 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 20 20   the error.**   
7c80: 20 20 20 20 20 20 20 6d 65 73 73 61 67 65 20 69         message i
7c90: 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6e 65 65 64  s no longer need
7ca0: 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 31 34  ed..**.** {A1214
7cb0: 35 7d 20 54 68 65 20 53 51 4c 20 73 74 61 74 65  5} The SQL state
7cc0: 6d 65 6e 74 20 74 65 78 74 20 69 6e 20 74 68 65  ment text in the
7cd0: 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74   2nd parameter t
7ce0: 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28  o [sqlite3_exec(
7cf0: 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6d  )].**          m
7d00: 75 73 74 20 72 65 6d 61 69 6e 20 75 6e 63 68 61  ust remain uncha
7d10: 6e 67 65 64 20 77 68 69 6c 65 20 5b 73 71 6c 69  nged while [sqli
7d20: 74 65 33 5f 65 78 65 63 28 29 5d 20 69 73 20 72  te3_exec()] is r
7d30: 75 6e 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54  unning..*/.SQLIT
7d40: 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
7d50: 33 5f 65 78 65 63 28 0a 20 20 73 71 6c 69 74 65  3_exec(.  sqlite
7d60: 33 2a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  3*,             
7d70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7d80: 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65 6e 20       /* An open 
7d90: 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f  database */.  co
7da0: 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 2c 20 20  nst char *sql,  
7db0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7dc0: 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20           /* SQL 
7dd0: 74 6f 20 62 65 20 65 76 61 6c 75 61 74 65 64 20  to be evaluated 
7de0: 2a 2f 0a 20 20 69 6e 74 20 28 2a 63 61 6c 6c 62  */.  int (*callb
7df0: 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  ack)(void*,int,c
7e00: 68 61 72 2a 2a 2c 63 68 61 72 2a 2a 29 2c 20 20  har**,char**),  
7e10: 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 75 6e 63  /* Callback func
7e20: 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  tion */.  void *
7e30: 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
7e40: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7e50: 20 20 20 20 20 2f 2a 20 31 73 74 20 61 72 67 75       /* 1st argu
7e60: 6d 65 6e 74 20 74 6f 20 63 61 6c 6c 62 61 63 6b  ment to callback
7e70: 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 65 72 72   */.  char **err
7e80: 6d 73 67 20 20 20 20 20 20 20 20 20 20 20 20 20  msg             
7e90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7ea0: 20 2f 2a 20 45 72 72 6f 72 20 6d 73 67 20 77 72   /* Error msg wr
7eb0: 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 3b  itten here */.);
7ec0: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
7ed0: 3a 20 52 65 73 75 6c 74 20 43 6f 64 65 73 20 7b  : Result Codes {
7ee0: 48 31 30 32 31 30 7d 20 3c 53 31 30 37 30 30 3e  H10210} <S10700>
7ef0: 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 53 51  .** KEYWORDS: SQ
7f00: 4c 49 54 45 5f 4f 4b 20 7b 65 72 72 6f 72 20 63  LITE_OK {error c
7f10: 6f 64 65 7d 20 7b 65 72 72 6f 72 20 63 6f 64 65  ode} {error code
7f20: 73 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  s}.** KEYWORDS: 
7f30: 7b 72 65 73 75 6c 74 20 63 6f 64 65 7d 20 7b 72  {result code} {r
7f40: 65 73 75 6c 74 20 63 6f 64 65 73 7d 0a 2a 2a 0a  esult codes}.**.
7f50: 2a 2a 20 4d 61 6e 79 20 53 51 4c 69 74 65 20 66  ** Many SQLite f
7f60: 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20  unctions return 
7f70: 61 6e 20 69 6e 74 65 67 65 72 20 72 65 73 75 6c  an integer resul
7f80: 74 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 65 20  t code from the 
7f90: 73 65 74 20 73 68 6f 77 6e 0a 2a 2a 20 68 65 72  set shown.** her
7fa0: 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 6e  e in order to in
7fb0: 64 69 63 61 74 65 73 20 73 75 63 63 65 73 73 20  dicates success 
7fc0: 6f 72 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a  or failure..**.*
7fd0: 2a 20 4e 65 77 20 65 72 72 6f 72 20 63 6f 64 65  * New error code
7fe0: 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69  s may be added i
7ff0: 6e 20 66 75 74 75 72 65 20 76 65 72 73 69 6f 6e  n future version
8000: 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a  s of SQLite..**.
8010: 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51  ** See also: [SQ
8020: 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20  LITE_IOERR_READ 
8030: 7c 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c  | extended resul
8040: 74 20 63 6f 64 65 73 5d 0a 2a 2f 0a 23 64 65 66  t codes].*/.#def
8050: 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4b 20 20 20  ine SQLITE_OK   
8060: 20 20 20 20 20 20 20 20 30 20 20 20 2f 2a 20 53          0   /* S
8070: 75 63 63 65 73 73 66 75 6c 20 72 65 73 75 6c 74  uccessful result
8080: 20 2a 2f 0a 2f 2a 20 62 65 67 69 6e 6e 69 6e 67   */./* beginning
8090: 2d 6f 66 2d 65 72 72 6f 72 2d 63 6f 64 65 73 20  -of-error-codes 
80a0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
80b0: 45 5f 45 52 52 4f 52 20 20 20 20 20 20 20 20 31  E_ERROR        1
80c0: 20 20 20 2f 2a 20 53 51 4c 20 65 72 72 6f 72 20     /* SQL error 
80d0: 6f 72 20 6d 69 73 73 69 6e 67 20 64 61 74 61 62  or missing datab
80e0: 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ase */.#define S
80f0: 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c 20 20  QLITE_INTERNAL  
8100: 20 20 20 32 20 20 20 2f 2a 20 49 6e 74 65 72 6e     2   /* Intern
8110: 61 6c 20 6c 6f 67 69 63 20 65 72 72 6f 72 20 69  al logic error i
8120: 6e 20 53 51 4c 69 74 65 20 2a 2f 0a 23 64 65 66  n SQLite */.#def
8130: 69 6e 65 20 53 51 4c 49 54 45 5f 50 45 52 4d 20  ine SQLITE_PERM 
8140: 20 20 20 20 20 20 20 20 33 20 20 20 2f 2a 20 41          3   /* A
8150: 63 63 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e  ccess permission
8160: 20 64 65 6e 69 65 64 20 2a 2f 0a 23 64 65 66 69   denied */.#defi
8170: 6e 65 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20  ne SQLITE_ABORT 
8180: 20 20 20 20 20 20 20 34 20 20 20 2f 2a 20 43 61         4   /* Ca
8190: 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 20 72  llback routine r
81a0: 65 71 75 65 73 74 65 64 20 61 6e 20 61 62 6f 72  equested an abor
81b0: 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  t */.#define SQL
81c0: 49 54 45 5f 42 55 53 59 20 20 20 20 20 20 20 20  ITE_BUSY        
81d0: 20 35 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61   5   /* The data
81e0: 62 61 73 65 20 66 69 6c 65 20 69 73 20 6c 6f 63  base file is loc
81f0: 6b 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ked */.#define S
8200: 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 20 20 20  QLITE_LOCKED    
8210: 20 20 20 36 20 20 20 2f 2a 20 41 20 74 61 62 6c     6   /* A tabl
8220: 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  e in the databas
8230: 65 20 69 73 20 6c 6f 63 6b 65 64 20 2a 2f 0a 23  e is locked */.#
8240: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f  define SQLITE_NO
8250: 4d 45 4d 20 20 20 20 20 20 20 20 37 20 20 20 2f  MEM        7   /
8260: 2a 20 41 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  * A malloc() fai
8270: 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  led */.#define S
8280: 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 20  QLITE_READONLY  
8290: 20 20 20 38 20 20 20 2f 2a 20 41 74 74 65 6d 70     8   /* Attemp
82a0: 74 20 74 6f 20 77 72 69 74 65 20 61 20 72 65 61  t to write a rea
82b0: 64 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 20 2a  donly database *
82c0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
82d0: 5f 49 4e 54 45 52 52 55 50 54 20 20 20 20 39 20  _INTERRUPT    9 
82e0: 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 74    /* Operation t
82f0: 65 72 6d 69 6e 61 74 65 64 20 62 79 20 73 71 6c  erminated by sql
8300: 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29  ite3_interrupt()
8310: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
8320: 45 5f 49 4f 45 52 52 20 20 20 20 20 20 20 31 30  E_IOERR       10
8330: 20 20 20 2f 2a 20 53 6f 6d 65 20 6b 69 6e 64 20     /* Some kind 
8340: 6f 66 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f  of disk I/O erro
8350: 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 23 64  r occurred */.#d
8360: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52  efine SQLITE_COR
8370: 52 55 50 54 20 20 20 20 20 31 31 20 20 20 2f 2a  RUPT     11   /*
8380: 20 54 68 65 20 64 61 74 61 62 61 73 65 20 64 69   The database di
8390: 73 6b 20 69 6d 61 67 65 20 69 73 20 6d 61 6c 66  sk image is malf
83a0: 6f 72 6d 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  ormed */.#define
83b0: 20 53 51 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44   SQLITE_NOTFOUND
83c0: 20 20 20 20 31 32 20 20 20 2f 2a 20 4e 4f 54 20      12   /* NOT 
83d0: 55 53 45 44 2e 20 54 61 62 6c 65 20 6f 72 20 72  USED. Table or r
83e0: 65 63 6f 72 64 20 6e 6f 74 20 66 6f 75 6e 64 20  ecord not found 
83f0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
8400: 45 5f 46 55 4c 4c 20 20 20 20 20 20 20 20 31 33  E_FULL        13
8410: 20 20 20 2f 2a 20 49 6e 73 65 72 74 69 6f 6e 20     /* Insertion 
8420: 66 61 69 6c 65 64 20 62 65 63 61 75 73 65 20 64  failed because d
8430: 61 74 61 62 61 73 65 20 69 73 20 66 75 6c 6c 20  atabase is full 
8440: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
8450: 45 5f 43 41 4e 54 4f 50 45 4e 20 20 20 20 31 34  E_CANTOPEN    14
8460: 20 20 20 2f 2a 20 55 6e 61 62 6c 65 20 74 6f 20     /* Unable to 
8470: 6f 70 65 6e 20 74 68 65 20 64 61 74 61 62 61 73  open the databas
8480: 65 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e  e file */.#defin
8490: 65 20 53 51 4c 49 54 45 5f 50 52 4f 54 4f 43 4f  e SQLITE_PROTOCO
84a0: 4c 20 20 20 20 31 35 20 20 20 2f 2a 20 4e 4f 54  L    15   /* NOT
84b0: 20 55 53 45 44 2e 20 44 61 74 61 62 61 73 65 20   USED. Database 
84c0: 6c 6f 63 6b 20 70 72 6f 74 6f 63 6f 6c 20 65 72  lock protocol er
84d0: 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ror */.#define S
84e0: 51 4c 49 54 45 5f 45 4d 50 54 59 20 20 20 20 20  QLITE_EMPTY     
84f0: 20 20 31 36 20 20 20 2f 2a 20 44 61 74 61 62 61    16   /* Databa
8500: 73 65 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a 23  se is empty */.#
8510: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 43  define SQLITE_SC
8520: 48 45 4d 41 20 20 20 20 20 20 31 37 20 20 20 2f  HEMA      17   /
8530: 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 73  * The database s
8540: 63 68 65 6d 61 20 63 68 61 6e 67 65 64 20 2a 2f  chema changed */
8550: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
8560: 54 4f 4f 42 49 47 20 20 20 20 20 20 31 38 20 20  TOOBIG      18  
8570: 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 42 4c   /* String or BL
8580: 4f 42 20 65 78 63 65 65 64 73 20 73 69 7a 65 20  OB exceeds size 
8590: 6c 69 6d 69 74 20 2a 2f 0a 23 64 65 66 69 6e 65  limit */.#define
85a0: 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49   SQLITE_CONSTRAI
85b0: 4e 54 20 20 31 39 20 20 20 2f 2a 20 41 62 6f 72  NT  19   /* Abor
85c0: 74 20 64 75 65 20 74 6f 20 63 6f 6e 73 74 72 61  t due to constra
85d0: 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 2a 2f  int violation */
85e0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
85f0: 4d 49 53 4d 41 54 43 48 20 20 20 20 32 30 20 20  MISMATCH    20  
8600: 20 2f 2a 20 44 61 74 61 20 74 79 70 65 20 6d 69   /* Data type mi
8610: 73 6d 61 74 63 68 20 2a 2f 0a 23 64 65 66 69 6e  smatch */.#defin
8620: 65 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20  e SQLITE_MISUSE 
8630: 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4c 69 62       21   /* Lib
8640: 72 61 72 79 20 75 73 65 64 20 69 6e 63 6f 72 72  rary used incorr
8650: 65 63 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65  ectly */.#define
8660: 20 53 51 4c 49 54 45 5f 4e 4f 4c 46 53 20 20 20   SQLITE_NOLFS   
8670: 20 20 20 20 32 32 20 20 20 2f 2a 20 55 73 65 73      22   /* Uses
8680: 20 4f 53 20 66 65 61 74 75 72 65 73 20 6e 6f 74   OS features not
8690: 20 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 68 6f   supported on ho
86a0: 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  st */.#define SQ
86b0: 4c 49 54 45 5f 41 55 54 48 20 20 20 20 20 20 20  LITE_AUTH       
86c0: 20 32 33 20 20 20 2f 2a 20 41 75 74 68 6f 72 69   23   /* Authori
86d0: 7a 61 74 69 6f 6e 20 64 65 6e 69 65 64 20 2a 2f  zation denied */
86e0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
86f0: 46 4f 52 4d 41 54 20 20 20 20 20 20 32 34 20 20  FORMAT      24  
8700: 20 2f 2a 20 41 75 78 69 6c 69 61 72 79 20 64 61   /* Auxiliary da
8710: 74 61 62 61 73 65 20 66 6f 72 6d 61 74 20 65 72  tabase format er
8720: 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ror */.#define S
8730: 51 4c 49 54 45 5f 52 41 4e 47 45 20 20 20 20 20  QLITE_RANGE     
8740: 20 20 32 35 20 20 20 2f 2a 20 32 6e 64 20 70 61    25   /* 2nd pa
8750: 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
8760: 65 33 5f 62 69 6e 64 20 6f 75 74 20 6f 66 20 72  e3_bind out of r
8770: 61 6e 67 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ange */.#define 
8780: 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 20 20 20  SQLITE_NOTADB   
8790: 20 20 20 32 36 20 20 20 2f 2a 20 46 69 6c 65 20     26   /* File 
87a0: 6f 70 65 6e 65 64 20 74 68 61 74 20 69 73 20 6e  opened that is n
87b0: 6f 74 20 61 20 64 61 74 61 62 61 73 65 20 66 69  ot a database fi
87c0: 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  le */.#define SQ
87d0: 4c 49 54 45 5f 52 4f 57 20 20 20 20 20 20 20 20  LITE_ROW        
87e0: 20 31 30 30 20 20 2f 2a 20 73 71 6c 69 74 65 33   100  /* sqlite3
87f0: 5f 73 74 65 70 28 29 20 68 61 73 20 61 6e 6f 74  _step() has anot
8800: 68 65 72 20 72 6f 77 20 72 65 61 64 79 20 2a 2f  her row ready */
8810: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
8820: 44 4f 4e 45 20 20 20 20 20 20 20 20 31 30 31 20  DONE        101 
8830: 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70   /* sqlite3_step
8840: 28 29 20 68 61 73 20 66 69 6e 69 73 68 65 64 20  () has finished 
8850: 65 78 65 63 75 74 69 6e 67 20 2a 2f 0a 2f 2a 20  executing */./* 
8860: 65 6e 64 2d 6f 66 2d 65 72 72 6f 72 2d 63 6f 64  end-of-error-cod
8870: 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  es */../*.** CAP
8880: 49 33 52 45 46 3a 20 45 78 74 65 6e 64 65 64 20  I3REF: Extended 
8890: 52 65 73 75 6c 74 20 43 6f 64 65 73 20 7b 48 31  Result Codes {H1
88a0: 30 32 32 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a  0220} <S10700>.*
88b0: 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 65 78 74  * KEYWORDS: {ext
88c0: 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65  ended error code
88d0: 7d 20 7b 65 78 74 65 6e 64 65 64 20 65 72 72 6f  } {extended erro
88e0: 72 20 63 6f 64 65 73 7d 0a 2a 2a 20 4b 45 59 57  r codes}.** KEYW
88f0: 4f 52 44 53 3a 20 7b 65 78 74 65 6e 64 65 64 20  ORDS: {extended 
8900: 72 65 73 75 6c 74 20 63 6f 64 65 7d 20 7b 65 78  result code} {ex
8910: 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f  tended result co
8920: 64 65 73 7d 0a 2a 2a 0a 2a 2a 20 49 6e 20 69 74  des}.**.** In it
8930: 73 20 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67  s default config
8940: 75 72 61 74 69 6f 6e 2c 20 53 51 4c 69 74 65 20  uration, SQLite 
8950: 41 50 49 20 72 6f 75 74 69 6e 65 73 20 72 65 74  API routines ret
8960: 75 72 6e 20 6f 6e 65 20 6f 66 20 32 36 20 69 6e  urn one of 26 in
8970: 74 65 67 65 72 0a 2a 2a 20 5b 53 51 4c 49 54 45  teger.** [SQLITE
8980: 5f 4f 4b 20 7c 20 72 65 73 75 6c 74 20 63 6f 64  _OK | result cod
8990: 65 73 5d 2e 20 20 48 6f 77 65 76 65 72 2c 20 65  es].  However, e
89a0: 78 70 65 72 69 65 6e 63 65 20 68 61 73 20 73 68  xperience has sh
89b0: 6f 77 6e 20 74 68 61 74 20 6d 61 6e 79 20 6f 66  own that many of
89c0: 0a 2a 2a 20 74 68 65 73 65 20 72 65 73 75 6c 74  .** these result
89d0: 20 63 6f 64 65 73 20 61 72 65 20 74 6f 6f 20 63   codes are too c
89e0: 6f 61 72 73 65 2d 67 72 61 69 6e 65 64 2e 20 20  oarse-grained.  
89f0: 54 68 65 79 20 64 6f 20 6e 6f 74 20 70 72 6f 76  They do not prov
8a00: 69 64 65 20 61 73 0a 2a 2a 20 6d 75 63 68 20 69  ide as.** much i
8a10: 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
8a20: 20 70 72 6f 62 6c 65 6d 73 20 61 73 20 70 72 6f   problems as pro
8a30: 67 72 61 6d 6d 65 72 73 20 6d 69 67 68 74 20 6c  grammers might l
8a40: 69 6b 65 2e 20 20 49 6e 20 61 6e 20 65 66 66 6f  ike.  In an effo
8a50: 72 74 20 74 6f 0a 2a 2a 20 61 64 64 72 65 73 73  rt to.** address
8a60: 20 74 68 69 73 2c 20 6e 65 77 65 72 20 76 65 72   this, newer ver
8a70: 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20  sions of SQLite 
8a80: 28 76 65 72 73 69 6f 6e 20 33 2e 33 2e 38 20 61  (version 3.3.8 a
8a90: 6e 64 20 6c 61 74 65 72 29 20 69 6e 63 6c 75 64  nd later) includ
8aa0: 65 0a 2a 2a 20 73 75 70 70 6f 72 74 20 66 6f 72  e.** support for
8ab0: 20 61 64 64 69 74 69 6f 6e 61 6c 20 72 65 73 75   additional resu
8ac0: 6c 74 20 63 6f 64 65 73 20 74 68 61 74 20 70 72  lt codes that pr
8ad0: 6f 76 69 64 65 20 6d 6f 72 65 20 64 65 74 61 69  ovide more detai
8ae0: 6c 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a  led information.
8af0: 2a 2a 20 61 62 6f 75 74 20 65 72 72 6f 72 73 2e  ** about errors.
8b00: 20 54 68 65 20 65 78 74 65 6e 64 65 64 20 72 65   The extended re
8b10: 73 75 6c 74 20 63 6f 64 65 73 20 61 72 65 20 65  sult codes are e
8b20: 6e 61 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c  nabled or disabl
8b30: 65 64 0a 2a 2a 20 6f 6e 20 61 20 70 65 72 20 64  ed.** on a per d
8b40: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
8b50: 6f 6e 20 62 61 73 69 73 20 75 73 69 6e 67 20 74  on basis using t
8b60: 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65  he.** [sqlite3_e
8b70: 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63  xtended_result_c
8b80: 6f 64 65 73 28 29 5d 20 41 50 49 2e 0a 2a 2a 0a  odes()] API..**.
8b90: 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 61  ** Some of the a
8ba0: 76 61 69 6c 61 62 6c 65 20 65 78 74 65 6e 64 65  vailable extende
8bb0: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61  d result codes a
8bc0: 72 65 20 6c 69 73 74 65 64 20 68 65 72 65 2e 0a  re listed here..
8bd0: 2a 2a 20 4f 6e 65 20 6d 61 79 20 65 78 70 65 63  ** One may expec
8be0: 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
8bf0: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
8c00: 63 6f 64 65 73 20 77 69 6c 6c 20 62 65 20 65 78  codes will be ex
8c10: 70 61 6e 64 0a 2a 2a 20 6f 76 65 72 20 74 69 6d  pand.** over tim
8c20: 65 2e 20 20 53 6f 66 74 77 61 72 65 20 74 68 61  e.  Software tha
8c30: 74 20 75 73 65 73 20 65 78 74 65 6e 64 65 64 20  t uses extended 
8c40: 72 65 73 75 6c 74 20 63 6f 64 65 73 20 73 68 6f  result codes sho
8c50: 75 6c 64 20 65 78 70 65 63 74 0a 2a 2a 20 74 6f  uld expect.** to
8c60: 20 73 65 65 20 6e 65 77 20 72 65 73 75 6c 74 20   see new result 
8c70: 63 6f 64 65 73 20 69 6e 20 66 75 74 75 72 65 20  codes in future 
8c80: 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69  releases of SQLi
8c90: 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51  te..**.** The SQ
8ca0: 4c 49 54 45 5f 4f 4b 20 72 65 73 75 6c 74 20 63  LITE_OK result c
8cb0: 6f 64 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62  ode will never b
8cc0: 65 20 65 78 74 65 6e 64 65 64 2e 20 20 49 74 20  e extended.  It 
8cd0: 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 62  will always.** b
8ce0: 65 20 65 78 61 63 74 6c 79 20 7a 65 72 6f 2e 0a  e exactly zero..
8cf0: 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53  **.** INVARIANTS
8d00: 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 32 33 7d  :.**.** {H10223}
8d10: 20 54 68 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61   The symbolic na
8d20: 6d 65 20 66 6f 72 20 61 6e 20 65 78 74 65 6e 64  me for an extend
8d30: 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 20 73  ed result code s
8d40: 68 61 6c 6c 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a  hall contains.**
8d50: 20 20 20 20 20 20 20 20 20 20 61 20 72 65 6c 61            a rela
8d60: 74 65 64 20 70 72 69 6d 61 72 79 20 72 65 73 75  ted primary resu
8d70: 6c 74 20 63 6f 64 65 20 61 73 20 61 20 70 72 65  lt code as a pre
8d80: 66 69 78 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32  fix..**.** {H102
8d90: 32 34 7d 20 50 72 69 6d 61 72 79 20 72 65 73 75  24} Primary resu
8da0: 6c 74 20 63 6f 64 65 20 6e 61 6d 65 73 20 73 68  lt code names sh
8db0: 61 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 20 73 69  all contain a si
8dc0: 6e 67 6c 65 20 22 5f 22 20 63 68 61 72 61 63 74  ngle "_" charact
8dd0: 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 32  er..**.** {H1022
8de0: 35 7d 20 45 78 74 65 6e 64 65 64 20 72 65 73 75  5} Extended resu
8df0: 6c 74 20 63 6f 64 65 20 6e 61 6d 65 73 20 73 68  lt code names sh
8e00: 61 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 77 6f 20  all contain two 
8e10: 6f 72 20 6d 6f 72 65 20 22 5f 22 20 63 68 61 72  or more "_" char
8e20: 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48  acters..**.** {H
8e30: 31 30 32 32 36 7d 20 54 68 65 20 6e 75 6d 65 72  10226} The numer
8e40: 69 63 20 76 61 6c 75 65 20 6f 66 20 61 6e 20 65  ic value of an e
8e50: 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63  xtended result c
8e60: 6f 64 65 20 73 68 61 6c 6c 20 63 6f 6e 74 61 69  ode shall contai
8e70: 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  n the.**        
8e80: 20 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20    numeric value 
8e90: 6f 66 20 69 74 73 20 63 6f 72 72 65 73 70 6f 6e  of its correspon
8ea0: 64 69 6e 67 20 70 72 69 6d 61 72 79 20 72 65 73  ding primary res
8eb0: 75 6c 74 20 63 6f 64 65 20 69 6e 0a 2a 2a 20 20  ult code in.**  
8ec0: 20 20 20 20 20 20 20 20 69 74 73 20 6c 65 61 73          its leas
8ed0: 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20 38 20  t significant 8 
8ee0: 62 69 74 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  bits..*/.#define
8ef0: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45   SQLITE_IOERR_RE
8f00: 41 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20  AD              
8f10: 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20  (SQLITE_IOERR | 
8f20: 28 31 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  (1<<8)).#define 
8f30: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f  SQLITE_IOERR_SHO
8f40: 52 54 5f 52 45 41 44 20 20 20 20 20 20 20 20 28  RT_READ        (
8f50: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
8f60: 32 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53  2<<8)).#define S
8f70: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54  QLITE_IOERR_WRIT
8f80: 45 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53  E             (S
8f90: 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 33  QLITE_IOERR | (3
8fa0: 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51  <<8)).#define SQ
8fb0: 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e 43  LITE_IOERR_FSYNC
8fc0: 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51               (SQ
8fd0: 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 34 3c  LITE_IOERR | (4<
8fe0: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  <8)).#define SQL
8ff0: 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53  ITE_IOERR_DIR_FS
9000: 59 4e 43 20 20 20 20 20 20 20 20 20 28 53 51 4c  YNC         (SQL
9010: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 35 3c 3c  ITE_IOERR | (5<<
9020: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  8)).#define SQLI
9030: 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54  TE_IOERR_TRUNCAT
9040: 45 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49  E          (SQLI
9050: 54 45 5f 49 4f 45 52 52 20 7c 20 28 36 3c 3c 38  TE_IOERR | (6<<8
9060: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
9070: 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 20 20 20  E_IOERR_FSTAT   
9080: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
9090: 45 5f 49 4f 45 52 52 20 7c 20 28 37 3c 3c 38 29  E_IOERR | (7<<8)
90a0: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ).#define SQLITE
90b0: 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 20 20 20  _IOERR_UNLOCK   
90c0: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45           (SQLITE
90d0: 5f 49 4f 45 52 52 20 7c 20 28 38 3c 3c 38 29 29  _IOERR | (8<<8))
90e0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
90f0: 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 20 20 20 20  IOERR_RDLOCK    
9100: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f          (SQLITE_
9110: 49 4f 45 52 52 20 7c 20 28 39 3c 3c 38 29 29 0a  IOERR | (9<<8)).
9120: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
9130: 4f 45 52 52 5f 44 45 4c 45 54 45 20 20 20 20 20  OERR_DELETE     
9140: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49         (SQLITE_I
9150: 4f 45 52 52 20 7c 20 28 31 30 3c 3c 38 29 29 0a  OERR | (10<<8)).
9160: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
9170: 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 20 20 20  OERR_BLOCKED    
9180: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49         (SQLITE_I
9190: 4f 45 52 52 20 7c 20 28 31 31 3c 3c 38 29 29 0a  OERR | (11<<8)).
91a0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
91b0: 4f 45 52 52 5f 4e 4f 4d 45 4d 20 20 20 20 20 20  OERR_NOMEM      
91c0: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49         (SQLITE_I
91d0: 4f 45 52 52 20 7c 20 28 31 32 3c 3c 38 29 29 0a  OERR | (12<<8)).
91e0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
91f0: 4f 45 52 52 5f 41 43 43 45 53 53 20 20 20 20 20  OERR_ACCESS     
9200: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49         (SQLITE_I
9210: 4f 45 52 52 20 7c 20 28 31 33 3c 3c 38 29 29 0a  OERR | (13<<8)).
9220: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
9230: 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56  OERR_CHECKRESERV
9240: 45 44 4c 4f 43 4b 20 28 53 51 4c 49 54 45 5f 49  EDLOCK (SQLITE_I
9250: 4f 45 52 52 20 7c 20 28 31 34 3c 3c 38 29 29 0a  OERR | (14<<8)).
9260: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
9270: 4f 45 52 52 5f 4c 4f 43 4b 20 20 20 20 20 20 20  OERR_LOCK       
9280: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49         (SQLITE_I
9290: 4f 45 52 52 20 7c 20 28 31 35 3c 3c 38 29 29 0a  OERR | (15<<8)).
92a0: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
92b0: 20 46 6c 61 67 73 20 46 6f 72 20 46 69 6c 65 20   Flags For File 
92c0: 4f 70 65 6e 20 4f 70 65 72 61 74 69 6f 6e 73 20  Open Operations 
92d0: 7b 48 31 30 32 33 30 7d 20 3c 48 31 31 31 32 30  {H10230} <H11120
92e0: 3e 20 3c 48 31 32 37 30 30 3e 0a 2a 2a 0a 2a 2a  > <H12700>.**.**
92f0: 20 54 68 65 73 65 20 62 69 74 20 76 61 6c 75 65   These bit value
9300: 73 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66  s are intended f
9310: 6f 72 20 75 73 65 20 69 6e 20 74 68 65 0a 2a 2a  or use in the.**
9320: 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74   3rd parameter t
9330: 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6f  o the [sqlite3_o
9340: 70 65 6e 5f 76 32 28 29 5d 20 69 6e 74 65 72 66  pen_v2()] interf
9350: 61 63 65 20 61 6e 64 0a 2a 2a 20 69 6e 20 74 68  ace and.** in th
9360: 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20  e 4th parameter 
9370: 74 6f 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74  to the xOpen met
9380: 68 6f 64 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73  hod of the.** [s
9390: 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65  qlite3_vfs] obje
93a0: 63 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ct..*/.#define S
93b0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f  QLITE_OPEN_READO
93c0: 4e 4c 59 20 20 20 20 20 20 20 20 20 30 78 30 30  NLY         0x00
93d0: 30 30 30 30 30 31 0a 23 64 65 66 69 6e 65 20 53  000001.#define S
93e0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
93f0: 52 49 54 45 20 20 20 20 20 20 20 20 30 78 30 30  RITE        0x00
9400: 30 30 30 30 30 32 0a 23 64 65 66 69 6e 65 20 53  000002.#define S
9410: 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
9420: 45 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30  E           0x00
9430: 30 30 30 30 30 34 0a 23 64 65 66 69 6e 65 20 53  000004.#define S
9440: 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54  QLITE_OPEN_DELET
9450: 45 4f 4e 43 4c 4f 53 45 20 20 20 20 30 78 30 30  EONCLOSE    0x00
9460: 30 30 30 30 30 38 0a 23 64 65 66 69 6e 65 20 53  000008.#define S
9470: 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55  QLITE_OPEN_EXCLU
9480: 53 49 56 45 20 20 20 20 20 20 20 20 30 78 30 30  SIVE        0x00
9490: 30 30 30 30 31 30 0a 23 64 65 66 69 6e 65 20 53  000010.#define S
94a0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
94b0: 44 42 20 20 20 20 20 20 20 20 20 20 30 78 30 30  DB          0x00
94c0: 30 30 30 31 30 30 0a 23 64 65 66 69 6e 65 20 53  000100.#define S
94d0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f  QLITE_OPEN_TEMP_
94e0: 44 42 20 20 20 20 20 20 20 20 20 20 30 78 30 30  DB          0x00
94f0: 30 30 30 32 30 30 0a 23 64 65 66 69 6e 65 20 53  000200.#define S
9500: 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53  QLITE_OPEN_TRANS
9510: 49 45 4e 54 5f 44 42 20 20 20 20 20 30 78 30 30  IENT_DB     0x00
9520: 30 30 30 34 30 30 0a 23 64 65 66 69 6e 65 20 53  000400.#define S
9530: 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
9540: 4a 4f 55 52 4e 41 4c 20 20 20 20 20 30 78 30 30  JOURNAL     0x00
9550: 30 30 30 38 30 30 0a 23 64 65 66 69 6e 65 20 53  000800.#define S
9560: 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f  QLITE_OPEN_TEMP_
9570: 4a 4f 55 52 4e 41 4c 20 20 20 20 20 30 78 30 30  JOURNAL     0x00
9580: 30 30 31 30 30 30 0a 23 64 65 66 69 6e 65 20 53  001000.#define S
9590: 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f  QLITE_OPEN_SUBJO
95a0: 55 52 4e 41 4c 20 20 20 20 20 20 20 30 78 30 30  URNAL       0x00
95b0: 30 30 32 30 30 30 0a 23 64 65 66 69 6e 65 20 53  002000.#define S
95c0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45  QLITE_OPEN_MASTE
95d0: 52 5f 4a 4f 55 52 4e 41 4c 20 20 20 30 78 30 30  R_JOURNAL   0x00
95e0: 30 30 34 30 30 30 0a 23 64 65 66 69 6e 65 20 53  004000.#define S
95f0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54  QLITE_OPEN_NOMUT
9600: 45 58 20 20 20 20 20 20 20 20 20 20 30 78 30 30  EX          0x00
9610: 30 30 38 30 30 30 0a 23 64 65 66 69 6e 65 20 53  008000.#define S
9620: 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d  QLITE_OPEN_FULLM
9630: 55 54 45 58 20 20 20 20 20 20 20 20 30 78 30 30  UTEX        0x00
9640: 30 31 30 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 43 41  010000../*.** CA
9650: 50 49 33 52 45 46 3a 20 44 65 76 69 63 65 20 43  PI3REF: Device C
9660: 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 7b  haracteristics {
9670: 48 31 30 32 34 30 7d 20 3c 48 31 31 31 32 30 3e  H10240} <H11120>
9680: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 44 65 76 69  .**.** The xDevi
9690: 63 65 43 61 70 61 62 69 6c 69 74 69 65 73 20 6d  ceCapabilities m
96a0: 65 74 68 6f 64 20 6f 66 20 74 68 65 20 5b 73 71  ethod of the [sq
96b0: 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
96c0: 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 72 65 74 75  ].** object retu
96d0: 72 6e 73 20 61 6e 20 69 6e 74 65 67 65 72 20 77  rns an integer w
96e0: 68 69 63 68 20 69 73 20 61 20 76 65 63 74 6f 72  hich is a vector
96f0: 20 6f 66 20 74 68 65 20 74 68 65 73 65 0a 2a 2a   of the these.**
9700: 20 62 69 74 20 76 61 6c 75 65 73 20 65 78 70 72   bit values expr
9710: 65 73 73 69 6e 67 20 49 2f 4f 20 63 68 61 72 61  essing I/O chara
9720: 63 74 65 72 69 73 74 69 63 73 20 6f 66 20 74 68  cteristics of th
9730: 65 20 6d 61 73 73 20 73 74 6f 72 61 67 65 0a 2a  e mass storage.*
9740: 2a 20 64 65 76 69 63 65 20 74 68 61 74 20 68 6f  * device that ho
9750: 6c 64 73 20 74 68 65 20 66 69 6c 65 20 74 68 61  lds the file tha
9760: 74 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69  t the [sqlite3_i
9770: 6f 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 72 65  o_methods].** re
9780: 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54  fers to..**.** T
9790: 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  he SQLITE_IOCAP_
97a0: 41 54 4f 4d 49 43 20 70 72 6f 70 65 72 74 79 20  ATOMIC property 
97b0: 6d 65 61 6e 73 20 74 68 61 74 20 61 6c 6c 20 77  means that all w
97c0: 72 69 74 65 73 20 6f 66 0a 2a 2a 20 61 6e 79 20  rites of.** any 
97d0: 73 69 7a 65 20 61 72 65 20 61 74 6f 6d 69 63 2e  size are atomic.
97e0: 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43    The SQLITE_IOC
97f0: 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e 20 76 61 6c  AP_ATOMICnnn val
9800: 75 65 73 0a 2a 2a 20 6d 65 61 6e 20 74 68 61 74  ues.** mean that
9810: 20 77 72 69 74 65 73 20 6f 66 20 62 6c 6f 63 6b   writes of block
9820: 73 20 74 68 61 74 20 61 72 65 20 6e 6e 6e 20 62  s that are nnn b
9830: 79 74 65 73 20 69 6e 20 73 69 7a 65 20 61 6e 64  ytes in size and
9840: 0a 2a 2a 20 61 72 65 20 61 6c 69 67 6e 65 64 20  .** are aligned 
9850: 74 6f 20 61 6e 20 61 64 64 72 65 73 73 20 77 68  to an address wh
9860: 69 63 68 20 69 73 20 61 6e 20 69 6e 74 65 67 65  ich is an intege
9870: 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 0a 2a 2a  r multiple of.**
9880: 20 6e 6e 6e 20 61 72 65 20 61 74 6f 6d 69 63 2e   nnn are atomic.
9890: 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43    The SQLITE_IOC
98a0: 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 76  AP_SAFE_APPEND v
98b0: 61 6c 75 65 20 6d 65 61 6e 73 0a 2a 2a 20 74 68  alue means.** th
98c0: 61 74 20 77 68 65 6e 20 64 61 74 61 20 69 73 20  at when data is 
98d0: 61 70 70 65 6e 64 65 64 20 74 6f 20 61 20 66 69  appended to a fi
98e0: 6c 65 2c 20 74 68 65 20 64 61 74 61 20 69 73 20  le, the data is 
98f0: 61 70 70 65 6e 64 65 64 0a 2a 2a 20 66 69 72 73  appended.** firs
9900: 74 20 74 68 65 6e 20 74 68 65 20 73 69 7a 65 20  t then the size 
9910: 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 65  of the file is e
9920: 78 74 65 6e 64 65 64 2c 20 6e 65 76 65 72 20 74  xtended, never t
9930: 68 65 20 6f 74 68 65 72 0a 2a 2a 20 77 61 79 20  he other.** way 
9940: 61 72 6f 75 6e 64 2e 20 20 54 68 65 20 53 51 4c  around.  The SQL
9950: 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e  ITE_IOCAP_SEQUEN
9960: 54 49 41 4c 20 70 72 6f 70 65 72 74 79 20 6d 65  TIAL property me
9970: 61 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e 66 6f  ans that.** info
9980: 72 6d 61 74 69 6f 6e 20 69 73 20 77 72 69 74 74  rmation is writt
9990: 65 6e 20 74 6f 20 64 69 73 6b 20 69 6e 20 74 68  en to disk in th
99a0: 65 20 73 61 6d 65 20 6f 72 64 65 72 20 61 73 20  e same order as 
99b0: 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 78 57 72 69  calls.** to xWri
99c0: 74 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  te()..*/.#define
99d0: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
99e0: 4f 4d 49 43 20 20 20 20 20 20 20 20 20 20 30 78  OMIC          0x
99f0: 30 30 30 30 30 30 30 31 0a 23 64 65 66 69 6e 65  00000001.#define
9a00: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
9a10: 4f 4d 49 43 35 31 32 20 20 20 20 20 20 20 30 78  OMIC512       0x
9a20: 30 30 30 30 30 30 30 32 0a 23 64 65 66 69 6e 65  00000002.#define
9a30: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
9a40: 4f 4d 49 43 31 4b 20 20 20 20 20 20 20 20 30 78  OMIC1K        0x
9a50: 30 30 30 30 30 30 30 34 0a 23 64 65 66 69 6e 65  00000004.#define
9a60: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
9a70: 4f 4d 49 43 32 4b 20 20 20 20 20 20 20 20 30 78  OMIC2K        0x
9a80: 30 30 30 30 30 30 30 38 0a 23 64 65 66 69 6e 65  00000008.#define
9a90: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
9aa0: 4f 4d 49 43 34 4b 20 20 20 20 20 20 20 20 30 78  OMIC4K        0x
9ab0: 30 30 30 30 30 30 31 30 0a 23 64 65 66 69 6e 65  00000010.#define
9ac0: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
9ad0: 4f 4d 49 43 38 4b 20 20 20 20 20 20 20 20 30 78  OMIC8K        0x
9ae0: 30 30 30 30 30 30 32 30 0a 23 64 65 66 69 6e 65  00000020.#define
9af0: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
9b00: 4f 4d 49 43 31 36 4b 20 20 20 20 20 20 20 30 78  OMIC16K       0x
9b10: 30 30 30 30 30 30 34 30 0a 23 64 65 66 69 6e 65  00000040.#define
9b20: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
9b30: 4f 4d 49 43 33 32 4b 20 20 20 20 20 20 20 30 78  OMIC32K       0x
9b40: 30 30 30 30 30 30 38 30 0a 23 64 65 66 69 6e 65  00000080.#define
9b50: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
9b60: 4f 4d 49 43 36 34 4b 20 20 20 20 20 20 20 30 78  OMIC64K       0x
9b70: 30 30 30 30 30 31 30 30 0a 23 64 65 66 69 6e 65  00000100.#define
9b80: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41   SQLITE_IOCAP_SA
9b90: 46 45 5f 41 50 50 45 4e 44 20 20 20 20 20 30 78  FE_APPEND     0x
9ba0: 30 30 30 30 30 32 30 30 0a 23 64 65 66 69 6e 65  00000200.#define
9bb0: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45   SQLITE_IOCAP_SE
9bc0: 51 55 45 4e 54 49 41 4c 20 20 20 20 20 20 30 78  QUENTIAL      0x
9bd0: 30 30 30 30 30 34 30 30 0a 0a 2f 2a 0a 2a 2a 20  00000400../*.** 
9be0: 43 41 50 49 33 52 45 46 3a 20 46 69 6c 65 20 4c  CAPI3REF: File L
9bf0: 6f 63 6b 69 6e 67 20 4c 65 76 65 6c 73 20 7b 48  ocking Levels {H
9c00: 31 30 32 35 30 7d 20 3c 48 31 31 31 32 30 3e 20  10250} <H11120> 
9c10: 3c 48 31 31 33 31 30 3e 0a 2a 2a 0a 2a 2a 20 53  <H11310>.**.** S
9c20: 51 4c 69 74 65 20 75 73 65 73 20 6f 6e 65 20 6f  QLite uses one o
9c30: 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20  f these integer 
9c40: 76 61 6c 75 65 73 20 61 73 20 74 68 65 20 73 65  values as the se
9c50: 63 6f 6e 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  cond.** argument
9c60: 20 74 6f 20 63 61 6c 6c 73 20 69 74 20 6d 61 6b   to calls it mak
9c70: 65 73 20 74 6f 20 74 68 65 20 78 4c 6f 63 6b 28  es to the xLock(
9c80: 29 20 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 20  ) and xUnlock() 
9c90: 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 61 6e  methods.** of an
9ca0: 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74   [sqlite3_io_met
9cb0: 68 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2f  hods] object..*/
9cc0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
9cd0: 4c 4f 43 4b 5f 4e 4f 4e 45 20 20 20 20 20 20 20  LOCK_NONE       
9ce0: 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c     0.#define SQL
9cf0: 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 20  ITE_LOCK_SHARED 
9d00: 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65         1.#define
9d10: 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 53   SQLITE_LOCK_RES
9d20: 45 52 56 45 44 20 20 20 20 20 20 32 0a 23 64 65  ERVED      2.#de
9d30: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  fine SQLITE_LOCK
9d40: 5f 50 45 4e 44 49 4e 47 20 20 20 20 20 20 20 33  _PENDING       3
9d50: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
9d60: 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 20 20  LOCK_EXCLUSIVE  
9d70: 20 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49     4../*.** CAPI
9d80: 33 52 45 46 3a 20 53 79 6e 63 68 72 6f 6e 69 7a  3REF: Synchroniz
9d90: 61 74 69 6f 6e 20 54 79 70 65 20 46 6c 61 67 73  ation Type Flags
9da0: 20 7b 48 31 30 32 36 30 7d 20 3c 48 31 31 31 32   {H10260} <H1112
9db0: 30 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53 51  0>.**.** When SQ
9dc0: 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 74 68 65  Lite invokes the
9dd0: 20 78 53 79 6e 63 28 29 20 6d 65 74 68 6f 64 20   xSync() method 
9de0: 6f 66 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65  of an.** [sqlite
9df0: 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62  3_io_methods] ob
9e00: 6a 65 63 74 20 69 74 20 75 73 65 73 20 61 20 63  ject it uses a c
9e10: 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 0a 2a 2a  ombination of.**
9e20: 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 76   these integer v
9e30: 61 6c 75 65 73 20 61 73 20 74 68 65 20 73 65 63  alues as the sec
9e40: 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a  ond argument..**
9e50: 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 53 51 4c  .** When the SQL
9e60: 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c  ITE_SYNC_DATAONL
9e70: 59 20 66 6c 61 67 20 69 73 20 75 73 65 64 2c 20  Y flag is used, 
9e80: 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  it means that th
9e90: 65 0a 2a 2a 20 73 79 6e 63 20 6f 70 65 72 61 74  e.** sync operat
9ea0: 69 6f 6e 20 6f 6e 6c 79 20 6e 65 65 64 73 20 74  ion only needs t
9eb0: 6f 20 66 6c 75 73 68 20 64 61 74 61 20 74 6f 20  o flush data to 
9ec0: 6d 61 73 73 20 73 74 6f 72 61 67 65 2e 20 20 49  mass storage.  I
9ed0: 6e 6f 64 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74  node.** informat
9ee0: 69 6f 6e 20 6e 65 65 64 20 6e 6f 74 20 62 65 20  ion need not be 
9ef0: 66 6c 75 73 68 65 64 2e 20 54 68 65 20 53 51 4c  flushed. The SQL
9f00: 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20  ITE_SYNC_NORMAL 
9f10: 66 6c 61 67 20 6d 65 61 6e 73 0a 2a 2a 20 74 6f  flag means.** to
9f20: 20 75 73 65 20 6e 6f 72 6d 61 6c 20 66 73 79 6e   use normal fsyn
9f30: 63 28 29 20 73 65 6d 61 6e 74 69 63 73 2e 20 54  c() semantics. T
9f40: 68 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46  he SQLITE_SYNC_F
9f50: 55 4c 4c 20 66 6c 61 67 20 6d 65 61 6e 73 0a 2a  ULL flag means.*
9f60: 2a 20 74 6f 20 75 73 65 20 4d 61 63 20 4f 53 2d  * to use Mac OS-
9f70: 58 20 73 74 79 6c 65 20 66 75 6c 6c 73 79 6e 63  X style fullsync
9f80: 20 69 6e 73 74 65 61 64 20 6f 66 20 66 73 79 6e   instead of fsyn
9f90: 63 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  c()..*/.#define 
9fa0: 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d  SQLITE_SYNC_NORM
9fb0: 41 4c 20 20 20 20 20 20 20 20 30 78 30 30 30 30  AL        0x0000
9fc0: 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  2.#define SQLITE
9fd0: 5f 53 59 4e 43 5f 46 55 4c 4c 20 20 20 20 20 20  _SYNC_FULL      
9fe0: 20 20 20 20 30 78 30 30 30 30 33 0a 23 64 65 66      0x00003.#def
9ff0: 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f  ine SQLITE_SYNC_
a000: 44 41 54 41 4f 4e 4c 59 20 20 20 20 20 20 30 78  DATAONLY      0x
a010: 30 30 30 31 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  00010../*.** CAP
a020: 49 33 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66  I3REF: OS Interf
a030: 61 63 65 20 4f 70 65 6e 20 46 69 6c 65 20 48 61  ace Open File Ha
a040: 6e 64 6c 65 20 7b 48 31 31 31 31 30 7d 20 3c 53  ndle {H11110} <S
a050: 32 30 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 41 6e 20  20110>.**.** An 
a060: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f  [sqlite3_file] o
a070: 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 73  bject represents
a080: 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 69 6e   an open file in
a090: 20 74 68 65 20 4f 53 0a 2a 2a 20 69 6e 74 65 72   the OS.** inter
a0a0: 66 61 63 65 20 6c 61 79 65 72 2e 20 20 49 6e 64  face layer.  Ind
a0b0: 69 76 69 64 75 61 6c 20 4f 53 20 69 6e 74 65 72  ividual OS inter
a0c0: 66 61 63 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  face implementat
a0d0: 69 6f 6e 73 20 77 69 6c 6c 0a 2a 2a 20 77 61 6e  ions will.** wan
a0e0: 74 20 74 6f 20 73 75 62 63 6c 61 73 73 20 74 68  t to subclass th
a0f0: 69 73 20 6f 62 6a 65 63 74 20 62 79 20 61 70 70  is object by app
a100: 65 6e 64 69 6e 67 20 61 64 64 69 74 69 6f 6e 61  ending additiona
a110: 6c 20 66 69 65 6c 64 73 0a 2a 2a 20 66 6f 72 20  l fields.** for 
a120: 74 68 65 69 72 20 6f 77 6e 20 75 73 65 2e 20 20  their own use.  
a130: 54 68 65 20 70 4d 65 74 68 6f 64 73 20 65 6e 74  The pMethods ent
a140: 72 79 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  ry is a pointer 
a150: 74 6f 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65  to an.** [sqlite
a160: 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62  3_io_methods] ob
a170: 6a 65 63 74 20 74 68 61 74 20 64 65 66 69 6e 65  ject that define
a180: 73 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 70 65  s methods for pe
a190: 72 66 6f 72 6d 69 6e 67 0a 2a 2a 20 49 2f 4f 20  rforming.** I/O 
a1a0: 6f 70 65 72 61 74 69 6f 6e 73 20 6f 6e 20 74 68  operations on th
a1b0: 65 20 6f 70 65 6e 20 66 69 6c 65 2e 0a 2a 2f 0a  e open file..*/.
a1c0: 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
a1d0: 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 71 6c 69  qlite3_file sqli
a1e0: 74 65 33 5f 66 69 6c 65 3b 0a 73 74 72 75 63 74  te3_file;.struct
a1f0: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 7b 0a   sqlite3_file {.
a200: 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73    const struct s
a210: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
a220: 73 20 2a 70 4d 65 74 68 6f 64 73 3b 20 20 2f 2a  s *pMethods;  /*
a230: 20 4d 65 74 68 6f 64 73 20 66 6f 72 20 61 6e 20   Methods for an 
a240: 6f 70 65 6e 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a  open file */.};.
a250: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
a260: 20 4f 53 20 49 6e 74 65 72 66 61 63 65 20 46 69   OS Interface Fi
a270: 6c 65 20 56 69 72 74 75 61 6c 20 4d 65 74 68 6f  le Virtual Metho
a280: 64 73 20 4f 62 6a 65 63 74 20 7b 48 31 31 31 32  ds Object {H1112
a290: 30 7d 20 3c 53 32 30 31 31 30 3e 0a 2a 2a 0a 2a  0} <S20110>.**.*
a2a0: 2a 20 45 76 65 72 79 20 66 69 6c 65 20 6f 70 65  * Every file ope
a2b0: 6e 65 64 20 62 79 20 74 68 65 20 5b 73 71 6c 69  ned by the [sqli
a2c0: 74 65 33 5f 76 66 73 5d 20 78 4f 70 65 6e 20 6d  te3_vfs] xOpen m
a2d0: 65 74 68 6f 64 20 70 6f 70 75 6c 61 74 65 73 20  ethod populates 
a2e0: 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66  an.** [sqlite3_f
a2f0: 69 6c 65 5d 20 6f 62 6a 65 63 74 20 28 6f 72 2c  ile] object (or,
a300: 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 6c 79 2c 20   more commonly, 
a310: 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68  a subclass of th
a320: 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69  e.** [sqlite3_fi
a330: 6c 65 5d 20 6f 62 6a 65 63 74 29 20 77 69 74 68  le] object) with
a340: 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
a350: 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
a360: 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 54 68 69  s object..** Thi
a370: 73 20 6f 62 6a 65 63 74 20 64 65 66 69 6e 65 73  s object defines
a380: 20 74 68 65 20 6d 65 74 68 6f 64 73 20 75 73 65   the methods use
a390: 64 20 74 6f 20 70 65 72 66 6f 72 6d 20 76 61 72  d to perform var
a3a0: 69 6f 75 73 20 6f 70 65 72 61 74 69 6f 6e 73 0a  ious operations.
a3b0: 2a 2a 20 61 67 61 69 6e 73 74 20 74 68 65 20 6f  ** against the o
a3c0: 70 65 6e 20 66 69 6c 65 20 72 65 70 72 65 73 65  pen file represe
a3d0: 6e 74 65 64 20 62 79 20 74 68 65 20 5b 73 71 6c  nted by the [sql
a3e0: 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 63  ite3_file] objec
a3f0: 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61  t..**.** The fla
a400: 67 73 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78  gs argument to x
a410: 53 79 6e 63 20 6d 61 79 20 62 65 20 6f 6e 65 20  Sync may be one 
a420: 6f 66 20 5b 53 51 4c 49 54 45 5f 53 59 4e 43 5f  of [SQLITE_SYNC_
a430: 4e 4f 52 4d 41 4c 5d 20 6f 72 0a 2a 2a 20 5b 53  NORMAL] or.** [S
a440: 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 5d  QLITE_SYNC_FULL]
a450: 2e 20 20 54 68 65 20 66 69 72 73 74 20 63 68 6f  .  The first cho
a460: 69 63 65 20 69 73 20 74 68 65 20 6e 6f 72 6d 61  ice is the norma
a470: 6c 20 66 73 79 6e 63 28 29 2e 0a 2a 2a 20 54 68  l fsync()..** Th
a480: 65 20 73 65 63 6f 6e 64 20 63 68 6f 69 63 65 20  e second choice 
a490: 69 73 20 61 20 4d 61 63 20 4f 53 2d 58 20 73 74  is a Mac OS-X st
a4a0: 79 6c 65 20 66 75 6c 6c 73 79 6e 63 2e 20 20 54  yle fullsync.  T
a4b0: 68 65 20 5b 53 51 4c 49 54 45 5f 53 59 4e 43 5f  he [SQLITE_SYNC_
a4c0: 44 41 54 41 4f 4e 4c 59 5d 0a 2a 2a 20 66 6c 61  DATAONLY].** fla
a4d0: 67 20 6d 61 79 20 62 65 20 4f 52 65 64 20 69 6e  g may be ORed in
a4e0: 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61   to indicate tha
a4f0: 74 20 6f 6e 6c 79 20 74 68 65 20 64 61 74 61 20  t only the data 
a500: 6f 66 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 61  of the file.** a
a510: 6e 64 20 6e 6f 74 20 69 74 73 20 69 6e 6f 64 65  nd not its inode
a520: 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e   needs to be syn
a530: 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ced..**.** The i
a540: 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20 74 6f  nteger values to
a550: 20 78 4c 6f 63 6b 28 29 20 61 6e 64 20 78 55 6e   xLock() and xUn
a560: 6c 6f 63 6b 28 29 20 61 72 65 20 6f 6e 65 20 6f  lock() are one o
a570: 66 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69  f.** <ul>.** <li
a580: 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 4e  > [SQLITE_LOCK_N
a590: 4f 4e 45 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53  ONE],.** <li> [S
a5a0: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45  QLITE_LOCK_SHARE
a5b0: 44 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c  D],.** <li> [SQL
a5c0: 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 52 56 45  ITE_LOCK_RESERVE
a5d0: 44 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c  D],.** <li> [SQL
a5e0: 49 54 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47  ITE_LOCK_PENDING
a5f0: 5d 2c 20 6f 72 0a 2a 2a 20 3c 6c 69 3e 20 5b 53  ], or.** <li> [S
a600: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55  QLITE_LOCK_EXCLU
a610: 53 49 56 45 5d 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a  SIVE]..** </ul>.
a620: 2a 2a 20 78 4c 6f 63 6b 28 29 20 69 6e 63 72 65  ** xLock() incre
a630: 61 73 65 73 20 74 68 65 20 6c 6f 63 6b 2e 20 78  ases the lock. x
a640: 55 6e 6c 6f 63 6b 28 29 20 64 65 63 72 65 61 73  Unlock() decreas
a650: 65 73 20 74 68 65 20 6c 6f 63 6b 2e 0a 2a 2a 20  es the lock..** 
a660: 54 68 65 20 78 43 68 65 63 6b 52 65 73 65 72 76  The xCheckReserv
a670: 65 64 4c 6f 63 6b 28 29 20 6d 65 74 68 6f 64 20  edLock() method 
a680: 63 68 65 63 6b 73 20 77 68 65 74 68 65 72 20 61  checks whether a
a690: 6e 79 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ny database conn
a6a0: 65 63 74 69 6f 6e 2c 0a 2a 2a 20 65 69 74 68 65  ection,.** eithe
a6b0: 72 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73  r in this proces
a6c0: 73 20 6f 72 20 69 6e 20 73 6f 6d 65 20 6f 74 68  s or in some oth
a6d0: 65 72 20 70 72 6f 63 65 73 73 2c 20 69 73 20 68  er process, is h
a6e0: 6f 6c 64 69 6e 67 20 61 20 52 45 53 45 52 56 45  olding a RESERVE
a6f0: 44 2c 0a 2a 2a 20 50 45 4e 44 49 4e 47 2c 20 6f  D,.** PENDING, o
a700: 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  r EXCLUSIVE lock
a710: 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 49   on the file.  I
a720: 74 20 72 65 74 75 72 6e 73 20 74 72 75 65 0a 2a  t returns true.*
a730: 2a 20 69 66 20 73 75 63 68 20 61 20 6c 6f 63 6b  * if such a lock
a740: 20 65 78 69 73 74 73 20 61 6e 64 20 66 61 6c 73   exists and fals
a750: 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a  e otherwise..**.
a760: 2a 2a 20 54 68 65 20 78 46 69 6c 65 43 6f 6e 74  ** The xFileCont
a770: 72 6f 6c 28 29 20 6d 65 74 68 6f 64 20 69 73 20  rol() method is 
a780: 61 20 67 65 6e 65 72 69 63 20 69 6e 74 65 72 66  a generic interf
a790: 61 63 65 20 74 68 61 74 20 61 6c 6c 6f 77 73 20  ace that allows 
a7a0: 63 75 73 74 6f 6d 0a 2a 2a 20 56 46 53 20 69 6d  custom.** VFS im
a7b0: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 74 6f  plementations to
a7c0: 20 64 69 72 65 63 74 6c 79 20 63 6f 6e 74 72 6f   directly contro
a7d0: 6c 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 75  l an open file u
a7e0: 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c  sing the.** [sql
a7f0: 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f  ite3_file_contro
a800: 6c 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 20  l()] interface. 
a810: 20 54 68 65 20 73 65 63 6f 6e 64 20 22 6f 70 22   The second "op"
a820: 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 0a   argument is an.
a830: 2a 2a 20 69 6e 74 65 67 65 72 20 6f 70 63 6f 64  ** integer opcod
a840: 65 2e 20 20 54 68 65 20 74 68 69 72 64 20 61 72  e.  The third ar
a850: 67 75 6d 65 6e 74 20 69 73 20 61 20 67 65 6e 65  gument is a gene
a860: 72 69 63 20 70 6f 69 6e 74 65 72 20 69 6e 74 65  ric pointer inte
a870: 6e 64 65 64 20 74 6f 0a 2a 2a 20 70 6f 69 6e 74  nded to.** point
a880: 20 74 6f 20 61 20 73 74 72 75 63 74 75 72 65 20   to a structure 
a890: 74 68 61 74 20 6d 61 79 20 63 6f 6e 74 61 69 6e  that may contain
a8a0: 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20 73 70   arguments or sp
a8b0: 61 63 65 20 69 6e 20 77 68 69 63 68 20 74 6f 0a  ace in which to.
a8c0: 2a 2a 20 77 72 69 74 65 20 72 65 74 75 72 6e 20  ** write return 
a8d0: 76 61 6c 75 65 73 2e 20 20 50 6f 74 65 6e 74 69  values.  Potenti
a8e0: 61 6c 20 75 73 65 73 20 66 6f 72 20 78 46 69 6c  al uses for xFil
a8f0: 65 43 6f 6e 74 72 6f 6c 28 29 20 6d 69 67 68 74  eControl() might
a900: 20 62 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73   be.** functions
a910: 20 74 6f 20 65 6e 61 62 6c 65 20 62 6c 6f 63 6b   to enable block
a920: 69 6e 67 20 6c 6f 63 6b 73 20 77 69 74 68 20 74  ing locks with t
a930: 69 6d 65 6f 75 74 73 2c 20 74 6f 20 63 68 61 6e  imeouts, to chan
a940: 67 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 69 6e  ge the.** lockin
a950: 67 20 73 74 72 61 74 65 67 79 20 28 66 6f 72 20  g strategy (for 
a960: 65 78 61 6d 70 6c 65 20 74 6f 20 75 73 65 20 64  example to use d
a970: 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 73 29 2c 20  ot-file locks), 
a980: 74 6f 20 69 6e 71 75 69 72 65 0a 2a 2a 20 61 62  to inquire.** ab
a990: 6f 75 74 20 74 68 65 20 73 74 61 74 75 73 20 6f  out the status o
a9a0: 66 20 61 20 6c 6f 63 6b 2c 20 6f 72 20 74 6f 20  f a lock, or to 
a9b0: 62 72 65 61 6b 20 73 74 61 6c 65 20 6c 6f 63 6b  break stale lock
a9c0: 73 2e 20 20 54 68 65 20 53 51 4c 69 74 65 0a 2a  s.  The SQLite.*
a9d0: 2a 20 63 6f 72 65 20 72 65 73 65 72 76 65 73 20  * core reserves 
a9e0: 61 6c 6c 20 6f 70 63 6f 64 65 73 20 6c 65 73 73  all opcodes less
a9f0: 20 74 68 61 6e 20 31 30 30 20 66 6f 72 20 69 74   than 100 for it
aa00: 73 20 6f 77 6e 20 75 73 65 2e 0a 2a 2a 20 41 20  s own use..** A 
aa10: 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f  [SQLITE_FCNTL_LO
aa20: 43 4b 53 54 41 54 45 20 7c 20 6c 69 73 74 20 6f  CKSTATE | list o
aa30: 66 20 6f 70 63 6f 64 65 73 5d 20 6c 65 73 73 20  f opcodes] less 
aa40: 74 68 61 6e 20 31 30 30 20 69 73 20 61 76 61 69  than 100 is avai
aa50: 6c 61 62 6c 65 2e 0a 2a 2a 20 41 70 70 6c 69 63  lable..** Applic
aa60: 61 74 69 6f 6e 73 20 74 68 61 74 20 64 65 66 69  ations that defi
aa70: 6e 65 20 61 20 63 75 73 74 6f 6d 20 78 46 69 6c  ne a custom xFil
aa80: 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20  eControl method 
aa90: 73 68 6f 75 6c 64 20 75 73 65 20 6f 70 63 6f 64  should use opcod
aaa0: 65 73 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68  es.** greater th
aab0: 61 6e 20 31 30 30 20 74 6f 20 61 76 6f 69 64 20  an 100 to avoid 
aac0: 63 6f 6e 66 6c 69 63 74 73 2e 0a 2a 2a 0a 2a 2a  conflicts..**.**
aad0: 20 54 68 65 20 78 53 65 63 74 6f 72 53 69 7a 65   The xSectorSize
aae0: 28 29 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e  () method return
aaf0: 73 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a  s the sector siz
ab00: 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 65 76 69  e of the.** devi
ab10: 63 65 20 74 68 61 74 20 75 6e 64 65 72 6c 69 65  ce that underlie
ab20: 73 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68 65  s the file.  The
ab30: 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 73 20   sector size is 
ab40: 74 68 65 0a 2a 2a 20 6d 69 6e 69 6d 75 6d 20 77  the.** minimum w
ab50: 72 69 74 65 20 74 68 61 74 20 63 61 6e 20 62 65  rite that can be
ab60: 20 70 65 72 66 6f 72 6d 65 64 20 77 69 74 68 6f   performed witho
ab70: 75 74 20 64 69 73 74 75 72 62 69 6e 67 0a 2a 2a  ut disturbing.**
ab80: 20 6f 74 68 65 72 20 62 79 74 65 73 20 69 6e 20   other bytes in 
ab90: 74 68 65 20 66 69 6c 65 2e 20 20 54 68 65 20 78  the file.  The x
aba0: 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
abb0: 73 74 69 63 73 28 29 0a 2a 2a 20 6d 65 74 68 6f  stics().** metho
abc0: 64 20 72 65 74 75 72 6e 73 20 61 20 62 69 74 20  d returns a bit 
abd0: 76 65 63 74 6f 72 20 64 65 73 63 72 69 62 69 6e  vector describin
abe0: 67 20 62 65 68 61 76 69 6f 72 73 20 6f 66 20 74  g behaviors of t
abf0: 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67  he.** underlying
ac00: 20 64 65 76 69 63 65 3a 0a 2a 2a 0a 2a 2a 20 3c   device:.**.** <
ac10: 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c  ul>.** <li> [SQL
ac20: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
ac30: 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54  ].** <li> [SQLIT
ac40: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31  E_IOCAP_ATOMIC51
ac50: 32 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49  2].** <li> [SQLI
ac60: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31  TE_IOCAP_ATOMIC1
ac70: 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49  K].** <li> [SQLI
ac80: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 32  TE_IOCAP_ATOMIC2
ac90: 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49  K].** <li> [SQLI
aca0: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 34  TE_IOCAP_ATOMIC4
acb0: 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49  K].** <li> [SQLI
acc0: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 38  TE_IOCAP_ATOMIC8
acd0: 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49  K].** <li> [SQLI
ace0: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31  TE_IOCAP_ATOMIC1
acf0: 36 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c  6K].** <li> [SQL
ad00: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
ad10: 33 32 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51  32K].** <li> [SQ
ad20: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
ad30: 43 36 34 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53  C64K].** <li> [S
ad40: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45  QLITE_IOCAP_SAFE
ad50: 5f 41 50 50 45 4e 44 5d 0a 2a 2a 20 3c 6c 69 3e  _APPEND].** <li>
ad60: 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53   [SQLITE_IOCAP_S
ad70: 45 51 55 45 4e 54 49 41 4c 5d 0a 2a 2a 20 3c 2f  EQUENTIAL].** </
ad80: 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51  ul>.**.** The SQ
ad90: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
ada0: 43 20 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73  C property means
adb0: 20 74 68 61 74 20 61 6c 6c 20 77 72 69 74 65 73   that all writes
adc0: 20 6f 66 0a 2a 2a 20 61 6e 79 20 73 69 7a 65 20   of.** any size 
add0: 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65  are atomic.  The
ade0: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
adf0: 4f 4d 49 43 6e 6e 6e 20 76 61 6c 75 65 73 0a 2a  OMICnnn values.*
ae00: 2a 20 6d 65 61 6e 20 74 68 61 74 20 77 72 69 74  * mean that writ
ae10: 65 73 20 6f 66 20 62 6c 6f 63 6b 73 20 74 68 61  es of blocks tha
ae20: 74 20 61 72 65 20 6e 6e 6e 20 62 79 74 65 73 20  t are nnn bytes 
ae30: 69 6e 20 73 69 7a 65 20 61 6e 64 0a 2a 2a 20 61  in size and.** a
ae40: 72 65 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e  re aligned to an
ae50: 20 61 64 64 72 65 73 73 20 77 68 69 63 68 20 69   address which i
ae60: 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6d 75 6c  s an integer mul
ae70: 74 69 70 6c 65 20 6f 66 0a 2a 2a 20 6e 6e 6e 20  tiple of.** nnn 
ae80: 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65  are atomic.  The
ae90: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41   SQLITE_IOCAP_SA
aea0: 46 45 5f 41 50 50 45 4e 44 20 76 61 6c 75 65 20  FE_APPEND value 
aeb0: 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 77 68  means.** that wh
aec0: 65 6e 20 64 61 74 61 20 69 73 20 61 70 70 65 6e  en data is appen
aed0: 64 65 64 20 74 6f 20 61 20 66 69 6c 65 2c 20 74  ded to a file, t
aee0: 68 65 20 64 61 74 61 20 69 73 20 61 70 70 65 6e  he data is appen
aef0: 64 65 64 0a 2a 2a 20 66 69 72 73 74 20 74 68 65  ded.** first the
af00: 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  n the size of th
af10: 65 20 66 69 6c 65 20 69 73 20 65 78 74 65 6e 64  e file is extend
af20: 65 64 2c 20 6e 65 76 65 72 20 74 68 65 20 6f 74  ed, never the ot
af30: 68 65 72 0a 2a 2a 20 77 61 79 20 61 72 6f 75 6e  her.** way aroun
af40: 64 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49  d.  The SQLITE_I
af50: 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20  OCAP_SEQUENTIAL 
af60: 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74  property means t
af70: 68 61 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  hat.** informati
af80: 6f 6e 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f  on is written to
af90: 20 64 69 73 6b 20 69 6e 20 74 68 65 20 73 61 6d   disk in the sam
afa0: 65 20 6f 72 64 65 72 20 61 73 20 63 61 6c 6c 73  e order as calls
afb0: 0a 2a 2a 20 74 6f 20 78 57 72 69 74 65 28 29 2e  .** to xWrite().
afc0: 0a 2a 2a 0a 2a 2a 20 49 66 20 78 52 65 61 64 28  .**.** If xRead(
afd0: 29 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  ) returns SQLITE
afe0: 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41  _IOERR_SHORT_REA
aff0: 44 20 69 74 20 6d 75 73 74 20 61 6c 73 6f 20 66  D it must also f
b000: 69 6c 6c 0a 2a 2a 20 69 6e 20 74 68 65 20 75 6e  ill.** in the un
b010: 72 65 61 64 20 70 6f 72 74 69 6f 6e 73 20 6f 66  read portions of
b020: 20 74 68 65 20 62 75 66 66 65 72 20 77 69 74 68   the buffer with
b030: 20 7a 65 72 6f 73 2e 20 20 41 20 56 46 53 20 74   zeros.  A VFS t
b040: 68 61 74 0a 2a 2a 20 66 61 69 6c 73 20 74 6f 20  hat.** fails to 
b050: 7a 65 72 6f 2d 66 69 6c 6c 20 73 68 6f 72 74 20  zero-fill short 
b060: 72 65 61 64 73 20 6d 69 67 68 74 20 73 65 65 6d  reads might seem
b070: 20 74 6f 20 77 6f 72 6b 2e 20 20 48 6f 77 65 76   to work.  Howev
b080: 65 72 2c 0a 2a 2a 20 66 61 69 6c 75 72 65 20 74  er,.** failure t
b090: 6f 20 7a 65 72 6f 2d 66 69 6c 6c 20 73 68 6f 72  o zero-fill shor
b0a0: 74 20 72 65 61 64 73 20 77 69 6c 6c 20 65 76 65  t reads will eve
b0b0: 6e 74 75 61 6c 6c 79 20 6c 65 61 64 20 74 6f 0a  ntually lead to.
b0c0: 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 72 72  ** database corr
b0d0: 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64  uption..*/.typed
b0e0: 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
b0f0: 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 73 71 6c  3_io_methods sql
b100: 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 3b  ite3_io_methods;
b110: 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
b120: 69 6f 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 20 69  io_methods {.  i
b130: 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20 20 69  nt iVersion;.  i
b140: 6e 74 20 28 2a 78 43 6c 6f 73 65 29 28 73 71 6c  nt (*xClose)(sql
b150: 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 20 69  ite3_file*);.  i
b160: 6e 74 20 28 2a 78 52 65 61 64 29 28 73 71 6c 69  nt (*xRead)(sqli
b170: 74 65 33 5f 66 69 6c 65 2a 2c 20 76 6f 69 64 2a  te3_file*, void*
b180: 2c 20 69 6e 74 20 69 41 6d 74 2c 20 73 71 6c 69  , int iAmt, sqli
b190: 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 73 74 29  te3_int64 iOfst)
b1a0: 3b 0a 20 20 69 6e 74 20 28 2a 78 57 72 69 74 65  ;.  int (*xWrite
b1b0: 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c  )(sqlite3_file*,
b1c0: 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e   const void*, in
b1d0: 74 20 69 41 6d 74 2c 20 73 71 6c 69 74 65 33 5f  t iAmt, sqlite3_
b1e0: 69 6e 74 36 34 20 69 4f 66 73 74 29 3b 0a 20 20  int64 iOfst);.  
b1f0: 69 6e 74 20 28 2a 78 54 72 75 6e 63 61 74 65 29  int (*xTruncate)
b200: 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20  (sqlite3_file*, 
b210: 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 69  sqlite3_int64 si
b220: 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 79  ze);.  int (*xSy
b230: 6e 63 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  nc)(sqlite3_file
b240: 2a 2c 20 69 6e 74 20 66 6c 61 67 73 29 3b 0a 20  *, int flags);. 
b250: 20 69 6e 74 20 28 2a 78 46 69 6c 65 53 69 7a 65   int (*xFileSize
b260: 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c  )(sqlite3_file*,
b270: 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a   sqlite3_int64 *
b280: 70 53 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a  pSize);.  int (*
b290: 78 4c 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66  xLock)(sqlite3_f
b2a0: 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e  ile*, int);.  in
b2b0: 74 20 28 2a 78 55 6e 6c 6f 63 6b 29 28 73 71 6c  t (*xUnlock)(sql
b2c0: 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29  ite3_file*, int)
b2d0: 3b 0a 20 20 69 6e 74 20 28 2a 78 43 68 65 63 6b  ;.  int (*xCheck
b2e0: 52 65 73 65 72 76 65 64 4c 6f 63 6b 29 28 73 71  ReservedLock)(sq
b2f0: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74  lite3_file*, int
b300: 20 2a 70 52 65 73 4f 75 74 29 3b 0a 20 20 69 6e   *pResOut);.  in
b310: 74 20 28 2a 78 46 69 6c 65 43 6f 6e 74 72 6f 6c  t (*xFileControl
b320: 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c  )(sqlite3_file*,
b330: 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70   int op, void *p
b340: 41 72 67 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53  Arg);.  int (*xS
b350: 65 63 74 6f 72 53 69 7a 65 29 28 73 71 6c 69 74  ectorSize)(sqlit
b360: 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 20 69 6e 74  e3_file*);.  int
b370: 20 28 2a 78 44 65 76 69 63 65 43 68 61 72 61 63   (*xDeviceCharac
b380: 74 65 72 69 73 74 69 63 73 29 28 73 71 6c 69 74  teristics)(sqlit
b390: 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 20 2f 2a 20  e3_file*);.  /* 
b3a0: 41 64 64 69 74 69 6f 6e 61 6c 20 6d 65 74 68 6f  Additional metho
b3b0: 64 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20  ds may be added 
b3c0: 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73  in future releas
b3d0: 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  es */.};../*.** 
b3e0: 43 41 50 49 33 52 45 46 3a 20 53 74 61 6e 64 61  CAPI3REF: Standa
b3f0: 72 64 20 46 69 6c 65 20 43 6f 6e 74 72 6f 6c 20  rd File Control 
b400: 4f 70 63 6f 64 65 73 20 7b 48 31 31 33 31 30 7d  Opcodes {H11310}
b410: 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S30800>.**.** 
b420: 54 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f  These integer co
b430: 6e 73 74 61 6e 74 73 20 61 72 65 20 6f 70 63 6f  nstants are opco
b440: 64 65 73 20 66 6f 72 20 74 68 65 20 78 46 69 6c  des for the xFil
b450: 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 0a  eControl method.
b460: 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74  ** of the [sqlit
b470: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f  e3_io_methods] o
b480: 62 6a 65 63 74 20 61 6e 64 20 66 6f 72 20 74 68  bject and for th
b490: 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f  e [sqlite3_file_
b4a0: 63 6f 6e 74 72 6f 6c 28 29 5d 0a 2a 2a 20 69 6e  control()].** in
b4b0: 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54  terface..**.** T
b4c0: 68 65 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c  he [SQLITE_FCNTL
b4d0: 5f 4c 4f 43 4b 53 54 41 54 45 5d 20 6f 70 63 6f  _LOCKSTATE] opco
b4e0: 64 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 64  de is used for d
b4f0: 65 62 75 67 67 69 6e 67 2e 20 20 54 68 69 73 0a  ebugging.  This.
b500: 2a 2a 20 6f 70 63 6f 64 65 20 63 61 75 73 65 73  ** opcode causes
b510: 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f   the xFileContro
b520: 6c 20 6d 65 74 68 6f 64 20 74 6f 20 77 72 69 74  l method to writ
b530: 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74  e the current st
b540: 61 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6c 6f  ate of.** the lo
b550: 63 6b 20 28 6f 6e 65 20 6f 66 20 5b 53 51 4c 49  ck (one of [SQLI
b560: 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c 20 5b  TE_LOCK_NONE], [
b570: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52  SQLITE_LOCK_SHAR
b580: 45 44 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  ED],.** [SQLITE_
b590: 4c 4f 43 4b 5f 52 45 53 45 52 56 45 44 5d 2c 20  LOCK_RESERVED], 
b5a0: 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50 45 4e  [SQLITE_LOCK_PEN
b5b0: 44 49 4e 47 5d 2c 20 6f 72 20 5b 53 51 4c 49 54  DING], or [SQLIT
b5c0: 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45  E_LOCK_EXCLUSIVE
b5d0: 5d 29 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20 69 6e  ]).** into an in
b5e0: 74 65 67 65 72 20 74 68 61 74 20 74 68 65 20 70  teger that the p
b5f0: 41 72 67 20 61 72 67 75 6d 65 6e 74 20 70 6f 69  Arg argument poi
b600: 6e 74 73 20 74 6f 2e 20 54 68 69 73 20 63 61 70  nts to. This cap
b610: 61 62 69 6c 69 74 79 0a 2a 2a 20 69 73 20 75 73  ability.** is us
b620: 65 64 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e  ed during testin
b630: 67 20 61 6e 64 20 6f 6e 6c 79 20 6e 65 65 64 73  g and only needs
b640: 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74 65 64   to be supported
b650: 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 54 45 53   when SQLITE_TES
b660: 54 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 2e  T.** is defined.
b670: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
b680: 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41  TE_FCNTL_LOCKSTA
b690: 54 45 20 20 20 20 20 20 20 20 31 0a 0a 2f 2a 0a  TE        1../*.
b6a0: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74  ** CAPI3REF: Mut
b6b0: 65 78 20 48 61 6e 64 6c 65 20 7b 48 31 37 31 31  ex Handle {H1711
b6c0: 30 7d 20 3c 53 32 30 31 33 30 3e 0a 2a 2a 0a 2a  0} <S20130>.**.*
b6d0: 2a 20 54 68 65 20 6d 75 74 65 78 20 6d 6f 64 75  * The mutex modu
b6e0: 6c 65 20 77 69 74 68 69 6e 20 53 51 4c 69 74 65  le within SQLite
b6f0: 20 64 65 66 69 6e 65 73 20 5b 73 71 6c 69 74 65   defines [sqlite
b700: 33 5f 6d 75 74 65 78 5d 20 74 6f 20 62 65 20 61  3_mutex] to be a
b710: 6e 0a 2a 2a 20 61 62 73 74 72 61 63 74 20 74 79  n.** abstract ty
b720: 70 65 20 66 6f 72 20 61 20 6d 75 74 65 78 20 6f  pe for a mutex o
b730: 62 6a 65 63 74 2e 20 20 54 68 65 20 53 51 4c 69  bject.  The SQLi
b740: 74 65 20 63 6f 72 65 20 6e 65 76 65 72 20 6c 6f  te core never lo
b750: 6f 6b 73 0a 2a 2a 20 61 74 20 74 68 65 20 69 6e  oks.** at the in
b760: 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74  ternal represent
b770: 61 74 69 6f 6e 20 6f 66 20 61 6e 20 5b 73 71 6c  ation of an [sql
b780: 69 74 65 33 5f 6d 75 74 65 78 5d 2e 20 20 49 74  ite3_mutex].  It
b790: 20 6f 6e 6c 79 0a 2a 2a 20 64 65 61 6c 73 20 77   only.** deals w
b7a0: 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  ith pointers to 
b7b0: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74  the [sqlite3_mut
b7c0: 65 78 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a  ex] object..**.*
b7d0: 2a 20 4d 75 74 65 78 65 73 20 61 72 65 20 63 72  * Mutexes are cr
b7e0: 65 61 74 65 64 20 75 73 69 6e 67 20 5b 73 71 6c  eated using [sql
b7f0: 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
b800: 28 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ()]..*/.typedef 
b810: 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d  struct sqlite3_m
b820: 75 74 65 78 20 73 71 6c 69 74 65 33 5f 6d 75 74  utex sqlite3_mut
b830: 65 78 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  ex;../*.** CAPI3
b840: 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 61 63  REF: OS Interfac
b850: 65 20 4f 62 6a 65 63 74 20 7b 48 31 31 31 34 30  e Object {H11140
b860: 7d 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a 2a  } <S20100>.**.**
b870: 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
b880: 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20  the sqlite3_vfs 
b890: 6f 62 6a 65 63 74 20 64 65 66 69 6e 65 73 20 74  object defines t
b8a0: 68 65 20 69 6e 74 65 72 66 61 63 65 20 62 65 74  he interface bet
b8b0: 77 65 65 6e 0a 2a 2a 20 74 68 65 20 53 51 4c 69  ween.** the SQLi
b8c0: 74 65 20 63 6f 72 65 20 61 6e 64 20 74 68 65 20  te core and the 
b8d0: 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61  underlying opera
b8e0: 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 54 68  ting system.  Th
b8f0: 65 20 22 76 66 73 22 0a 2a 2a 20 69 6e 20 74 68  e "vfs".** in th
b900: 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6f 62  e name of the ob
b910: 6a 65 63 74 20 73 74 61 6e 64 73 20 66 6f 72 20  ject stands for 
b920: 22 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79  "virtual file sy
b930: 73 74 65 6d 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  stem"..**.** The
b940: 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 56   value of the iV
b950: 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 69 73 20  ersion field is 
b960: 69 6e 69 74 69 61 6c 6c 79 20 31 20 62 75 74 20  initially 1 but 
b970: 6d 61 79 20 62 65 20 6c 61 72 67 65 72 20 69 6e  may be larger in
b980: 0a 2a 2a 20 66 75 74 75 72 65 20 76 65 72 73 69  .** future versi
b990: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20  ons of SQLite.  
b9a0: 41 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64  Additional field
b9b0: 73 20 6d 61 79 20 62 65 20 61 70 70 65 6e 64 65  s may be appende
b9c0: 64 20 74 6f 20 74 68 69 73 0a 2a 2a 20 6f 62 6a  d to this.** obj
b9d0: 65 63 74 20 77 68 65 6e 20 74 68 65 20 69 56 65  ect when the iVe
b9e0: 72 73 69 6f 6e 20 76 61 6c 75 65 20 69 73 20 69  rsion value is i
b9f0: 6e 63 72 65 61 73 65 64 2e 20 20 4e 6f 74 65 20  ncreased.  Note 
ba00: 74 68 61 74 20 74 68 65 20 73 74 72 75 63 74 75  that the structu
ba10: 72 65 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c  re.** of the sql
ba20: 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 20  ite3_vfs object 
ba30: 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 74  changes in the t
ba40: 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 74 77 65  ransaction betwe
ba50: 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 76 65 72  en.** SQLite ver
ba60: 73 69 6f 6e 20 33 2e 35 2e 39 20 61 6e 64 20 33  sion 3.5.9 and 3
ba70: 2e 36 2e 30 20 61 6e 64 20 79 65 74 20 74 68 65  .6.0 and yet the
ba80: 20 69 56 65 72 73 69 6f 6e 20 66 69 65 6c 64 20   iVersion field 
ba90: 77 61 73 20 6e 6f 74 0a 2a 2a 20 6d 6f 64 69 66  was not.** modif
baa0: 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ied..**.** The s
bab0: 7a 4f 73 46 69 6c 65 20 66 69 65 6c 64 20 69 73  zOsFile field is
bac0: 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
bad0: 20 73 75 62 63 6c 61 73 73 65 64 20 5b 73 71 6c   subclassed [sql
bae0: 69 74 65 33 5f 66 69 6c 65 5d 0a 2a 2a 20 73 74  ite3_file].** st
baf0: 72 75 63 74 75 72 65 20 75 73 65 64 20 62 79 20  ructure used by 
bb00: 74 68 69 73 20 56 46 53 2e 20 20 6d 78 50 61 74  this VFS.  mxPat
bb10: 68 6e 61 6d 65 20 69 73 20 74 68 65 20 6d 61 78  hname is the max
bb20: 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 0a 2a  imum length of.*
bb30: 2a 20 61 20 70 61 74 68 6e 61 6d 65 20 69 6e 20  * a pathname in 
bb40: 74 68 69 73 20 56 46 53 2e 0a 2a 2a 0a 2a 2a 20  this VFS..**.** 
bb50: 52 65 67 69 73 74 65 72 65 64 20 73 71 6c 69 74  Registered sqlit
bb60: 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 73 20 61  e3_vfs objects a
bb70: 72 65 20 6b 65 70 74 20 6f 6e 20 61 20 6c 69 6e  re kept on a lin
bb80: 6b 65 64 20 6c 69 73 74 20 66 6f 72 6d 65 64 20  ked list formed 
bb90: 62 79 0a 2a 2a 20 74 68 65 20 70 4e 65 78 74 20  by.** the pNext 
bba0: 70 6f 69 6e 74 65 72 2e 20 20 54 68 65 20 5b 73  pointer.  The [s
bbb0: 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73  qlite3_vfs_regis
bbc0: 74 65 72 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73  ter()].** and [s
bbd0: 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67  qlite3_vfs_unreg
bbe0: 69 73 74 65 72 28 29 5d 20 69 6e 74 65 72 66 61  ister()] interfa
bbf0: 63 65 73 20 6d 61 6e 61 67 65 20 74 68 69 73 20  ces manage this 
bc00: 6c 69 73 74 0a 2a 2a 20 69 6e 20 61 20 74 68 72  list.** in a thr
bc10: 65 61 64 2d 73 61 66 65 20 77 61 79 2e 20 20 54  ead-safe way.  T
bc20: 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f  he [sqlite3_vfs_
bc30: 66 69 6e 64 28 29 5d 20 69 6e 74 65 72 66 61 63  find()] interfac
bc40: 65 0a 2a 2a 20 73 65 61 72 63 68 65 73 20 74 68  e.** searches th
bc50: 65 20 6c 69 73 74 2e 20 20 4e 65 69 74 68 65 72  e list.  Neither
bc60: 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
bc70: 20 63 6f 64 65 20 6e 6f 72 20 74 68 65 20 56 46   code nor the VF
bc80: 53 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  S.** implementat
bc90: 69 6f 6e 20 73 68 6f 75 6c 64 20 75 73 65 20 74  ion should use t
bca0: 68 65 20 70 4e 65 78 74 20 70 6f 69 6e 74 65 72  he pNext pointer
bcb0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4e 65 78  ..**.** The pNex
bcc0: 74 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 6f  t field is the o
bcd0: 6e 6c 79 20 66 69 65 6c 64 20 69 6e 20 74 68 65  nly field in the
bce0: 20 73 71 6c 69 74 65 33 5f 76 66 73 0a 2a 2a 20   sqlite3_vfs.** 
bcf0: 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 53  structure that S
bd00: 51 4c 69 74 65 20 77 69 6c 6c 20 65 76 65 72 20  QLite will ever 
bd10: 6d 6f 64 69 66 79 2e 20 20 53 51 4c 69 74 65 20  modify.  SQLite 
bd20: 77 69 6c 6c 20 6f 6e 6c 79 20 61 63 63 65 73 73  will only access
bd30: 0a 2a 2a 20 6f 72 20 6d 6f 64 69 66 79 20 74 68  .** or modify th
bd40: 69 73 20 66 69 65 6c 64 20 77 68 69 6c 65 20 68  is field while h
bd50: 6f 6c 64 69 6e 67 20 61 20 70 61 72 74 69 63 75  olding a particu
bd60: 6c 61 72 20 73 74 61 74 69 63 20 6d 75 74 65 78  lar static mutex
bd70: 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61  ..** The applica
bd80: 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65  tion should neve
bd90: 72 20 6d 6f 64 69 66 79 20 61 6e 79 74 68 69 6e  r modify anythin
bda0: 67 20 77 69 74 68 69 6e 20 74 68 65 20 73 71 6c  g within the sql
bdb0: 69 74 65 33 5f 76 66 73 0a 2a 2a 20 6f 62 6a 65  ite3_vfs.** obje
bdc0: 63 74 20 6f 6e 63 65 20 74 68 65 20 6f 62 6a 65  ct once the obje
bdd0: 63 74 20 68 61 73 20 62 65 65 6e 20 72 65 67 69  ct has been regi
bde0: 73 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  stered..**.** Th
bdf0: 65 20 7a 4e 61 6d 65 20 66 69 65 6c 64 20 68 6f  e zName field ho
be00: 6c 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  lds the name of 
be10: 74 68 65 20 56 46 53 20 6d 6f 64 75 6c 65 2e 20  the VFS module. 
be20: 20 54 68 65 20 6e 61 6d 65 20 6d 75 73 74 0a 2a   The name must.*
be30: 2a 20 62 65 20 75 6e 69 71 75 65 20 61 63 72 6f  * be unique acro
be40: 73 73 20 61 6c 6c 20 56 46 53 20 6d 6f 64 75 6c  ss all VFS modul
be50: 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 31 34  es..**.** {H1114
be60: 31 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 67  1} SQLite will g
be70: 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68  uarantee that th
be80: 65 20 7a 46 69 6c 65 6e 61 6d 65 20 70 61 72 61  e zFilename para
be90: 6d 65 74 65 72 20 74 6f 20 78 4f 70 65 6e 0a 2a  meter to xOpen.*
bea0: 2a 20 69 73 20 65 69 74 68 65 72 20 61 20 4e 55  * is either a NU
beb0: 4c 4c 20 70 6f 69 6e 74 65 72 20 6f 72 20 73 74  LL pointer or st
bec0: 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 0a 2a 2a  ring obtained.**
bed0: 20 66 72 6f 6d 20 78 46 75 6c 6c 50 61 74 68 6e   from xFullPathn
bee0: 61 6d 65 28 29 2e 20 20 53 51 4c 69 74 65 20 66  ame().  SQLite f
bef0: 75 72 74 68 65 72 20 67 75 61 72 61 6e 74 65 65  urther guarantee
bf00: 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 73 74  s that.** the st
bf10: 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 76 61 6c  ring will be val
bf20: 69 64 20 61 6e 64 20 75 6e 63 68 61 6e 67 65 64  id and unchanged
bf30: 20 75 6e 74 69 6c 20 78 43 6c 6f 73 65 28 29 20   until xClose() 
bf40: 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e 20 7b 45  is.** called. {E
bf50: 4e 44 7d 20 20 42 65 63 61 75 73 65 20 6f 66 20  ND}  Because of 
bf60: 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e  the previous sen
bf70: 74 65 6e 73 65 2c 0a 2a 2a 20 74 68 65 20 5b 73  tense,.** the [s
bf80: 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 63 61 6e  qlite3_file] can
bf90: 20 73 61 66 65 6c 79 20 73 74 6f 72 65 20 61 20   safely store a 
bfa0: 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a  pointer to the.*
bfb0: 2a 20 66 69 6c 65 6e 61 6d 65 20 69 66 20 69 74  * filename if it
bfc0: 20 6e 65 65 64 73 20 74 6f 20 72 65 6d 65 6d 62   needs to rememb
bfd0: 65 72 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20  er the filename 
bfe0: 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e 2e  for some reason.
bff0: 0a 2a 2a 20 49 66 20 74 68 65 20 7a 46 69 6c 65  .** If the zFile
c000: 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 69  name parameter i
c010: 73 20 78 4f 70 65 6e 20 69 73 20 61 20 4e 55 4c  s xOpen is a NUL
c020: 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 78  L pointer then x
c030: 4f 70 65 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e 76  Open.** must inv
c040: 69 74 65 20 69 74 73 20 6f 77 6e 20 74 65 6d 70  ite its own temp
c050: 6f 72 61 72 79 20 6e 61 6d 65 20 66 6f 72 20 74  orary name for t
c060: 68 65 20 66 69 6c 65 2e 20 20 57 68 65 6e 65 76  he file.  Whenev
c070: 65 72 20 74 68 65 20 0a 2a 2a 20 78 46 69 6c 65  er the .** xFile
c080: 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 69  name parameter i
c090: 73 20 4e 55 4c 4c 20 69 74 20 77 69 6c 6c 20 61  s NULL it will a
c0a0: 6c 73 6f 20 62 65 20 74 68 65 20 63 61 73 65 20  lso be the case 
c0b0: 74 68 61 74 20 74 68 65 0a 2a 2a 20 66 6c 61 67  that the.** flag
c0c0: 73 20 70 61 72 61 6d 65 74 65 72 20 77 69 6c 6c  s parameter will
c0d0: 20 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54 45   include [SQLITE
c0e0: 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c  _OPEN_DELETEONCL
c0f0: 4f 53 45 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31  OSE]..**.** {H11
c100: 31 34 32 7d 20 54 68 65 20 66 6c 61 67 73 20 61  142} The flags a
c110: 72 67 75 6d 65 6e 74 20 74 6f 20 78 4f 70 65 6e  rgument to xOpen
c120: 28 29 20 69 6e 63 6c 75 64 65 73 20 61 6c 6c 20  () includes all 
c130: 62 69 74 73 20 73 65 74 20 69 6e 0a 2a 2a 20 74  bits set in.** t
c140: 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e  he flags argumen
c150: 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70  t to [sqlite3_op
c160: 65 6e 5f 76 32 28 29 5d 2e 20 20 4f 72 20 69 66  en_v2()].  Or if
c170: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29   [sqlite3_open()
c180: 5d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33  ].** or [sqlite3
c190: 5f 6f 70 65 6e 31 36 28 29 5d 20 69 73 20 75 73  _open16()] is us
c1a0: 65 64 2c 20 74 68 65 6e 20 66 6c 61 67 73 20 69  ed, then flags i
c1b0: 6e 63 6c 75 64 65 73 20 61 74 20 6c 65 61 73 74  ncludes at least
c1c0: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  .** [SQLITE_OPEN
c1d0: 5f 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b 53  _READWRITE] | [S
c1e0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
c1f0: 45 5d 2e 20 7b 45 4e 44 7d 0a 2a 2a 20 49 66 20  E]. {END}.** If 
c200: 78 4f 70 65 6e 28 29 20 6f 70 65 6e 73 20 61 20  xOpen() opens a 
c210: 66 69 6c 65 20 72 65 61 64 2d 6f 6e 6c 79 20 74  file read-only t
c220: 68 65 6e 20 69 74 20 73 65 74 73 20 2a 70 4f 75  hen it sets *pOu
c230: 74 46 6c 61 67 73 20 74 6f 0a 2a 2a 20 69 6e 63  tFlags to.** inc
c240: 6c 75 64 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45  lude [SQLITE_OPE
c250: 4e 5f 52 45 41 44 4f 4e 4c 59 5d 2e 20 20 4f 74  N_READONLY].  Ot
c260: 68 65 72 20 62 69 74 73 20 69 6e 20 2a 70 4f 75  her bits in *pOu
c270: 74 46 6c 61 67 73 20 6d 61 79 20 62 65 20 73 65  tFlags may be se
c280: 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 31 34 33  t..**.** {H11143
c290: 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c  } SQLite will al
c2a0: 73 6f 20 61 64 64 20 6f 6e 65 20 6f 66 20 74 68  so add one of th
c2b0: 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67  e following flag
c2c0: 73 20 74 6f 20 74 68 65 20 78 4f 70 65 6e 28 29  s to the xOpen()
c2d0: 0a 2a 2a 20 63 61 6c 6c 2c 20 64 65 70 65 6e 64  .** call, depend
c2e0: 69 6e 67 20 6f 6e 20 74 68 65 20 6f 62 6a 65 63  ing on the objec
c2f0: 74 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 3a 0a  t being opened:.
c300: 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c  **.** <ul>.** <l
c310: 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  i>  [SQLITE_OPEN
c320: 5f 4d 41 49 4e 5f 44 42 5d 0a 2a 2a 20 3c 6c 69  _MAIN_DB].** <li
c330: 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  >  [SQLITE_OPEN_
c340: 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a  MAIN_JOURNAL].**
c350: 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f   <li>  [SQLITE_O
c360: 50 45 4e 5f 54 45 4d 50 5f 44 42 5d 0a 2a 2a 20  PEN_TEMP_DB].** 
c370: 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50  <li>  [SQLITE_OP
c380: 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 5d  EN_TEMP_JOURNAL]
c390: 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54  .** <li>  [SQLIT
c3a0: 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54  E_OPEN_TRANSIENT
c3b0: 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53  _DB].** <li>  [S
c3c0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f  QLITE_OPEN_SUBJO
c3d0: 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20  URNAL].** <li>  
c3e0: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53  [SQLITE_OPEN_MAS
c3f0: 54 45 52 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20  TER_JOURNAL].** 
c400: 3c 2f 75 6c 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a  </ul> {END}.**.*
c410: 2a 20 54 68 65 20 66 69 6c 65 20 49 2f 4f 20 69  * The file I/O i
c420: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 63 61  mplementation ca
c430: 6e 20 75 73 65 20 74 68 65 20 6f 62 6a 65 63 74  n use the object
c440: 20 74 79 70 65 20 66 6c 61 67 73 20 74 6f 0a 2a   type flags to.*
c450: 2a 20 63 68 61 6e 67 65 20 74 68 65 20 77 61 79  * change the way
c460: 20 69 74 20 64 65 61 6c 73 20 77 69 74 68 20 66   it deals with f
c470: 69 6c 65 73 2e 20 20 46 6f 72 20 65 78 61 6d 70  iles.  For examp
c480: 6c 65 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69  le, an applicati
c490: 6f 6e 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20  on.** that does 
c4a0: 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74 20 63  not care about c
c4b0: 72 61 73 68 20 72 65 63 6f 76 65 72 79 20 6f 72  rash recovery or
c4c0: 20 72 6f 6c 6c 62 61 63 6b 20 6d 69 67 68 74 20   rollback might 
c4d0: 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6f 70 65 6e  make.** the open
c4e0: 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69   of a journal fi
c4f0: 6c 65 20 61 20 6e 6f 2d 6f 70 2e 20 20 57 72 69  le a no-op.  Wri
c500: 74 65 73 20 74 6f 20 74 68 69 73 20 6a 6f 75 72  tes to this jour
c510: 6e 61 6c 20 77 6f 75 6c 64 0a 2a 2a 20 61 6c 73  nal would.** als
c520: 6f 20 62 65 20 6e 6f 2d 6f 70 73 2c 20 61 6e 64  o be no-ops, and
c530: 20 61 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20   any attempt to 
c540: 72 65 61 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c  read the journal
c550: 20 77 6f 75 6c 64 20 72 65 74 75 72 6e 0a 2a 2a   would return.**
c560: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2e 20 20   SQLITE_IOERR.  
c570: 4f 72 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  Or the implement
c580: 61 74 69 6f 6e 20 6d 69 67 68 74 20 72 65 63 6f  ation might reco
c590: 67 6e 69 7a 65 20 74 68 61 74 20 61 20 64 61 74  gnize that a dat
c5a0: 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 77 69  abase.** file wi
c5b0: 6c 6c 20 62 65 20 64 6f 69 6e 67 20 70 61 67 65  ll be doing page
c5c0: 2d 61 6c 69 67 6e 65 64 20 73 65 63 74 6f 72 20  -aligned sector 
c5d0: 72 65 61 64 73 20 61 6e 64 20 77 72 69 74 65 73  reads and writes
c5e0: 20 69 6e 20 61 20 72 61 6e 64 6f 6d 0a 2a 2a 20   in a random.** 
c5f0: 6f 72 64 65 72 20 61 6e 64 20 73 65 74 20 75 70  order and set up
c600: 20 69 74 73 20 49 2f 4f 20 73 75 62 73 79 73 74   its I/O subsyst
c610: 65 6d 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a  em accordingly..
c620: 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 6d 69 67  **.** SQLite mig
c630: 68 74 20 61 6c 73 6f 20 61 64 64 20 6f 6e 65 20  ht also add one 
c640: 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
c650: 20 66 6c 61 67 73 20 74 6f 20 74 68 65 20 78 4f   flags to the xO
c660: 70 65 6e 20 6d 65 74 68 6f 64 3a 0a 2a 2a 0a 2a  pen method:.**.*
c670: 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b  * <ul>.** <li> [
c680: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45  SQLITE_OPEN_DELE
c690: 54 45 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 3c 6c  TEONCLOSE].** <l
c6a0: 69 3e 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  i> [SQLITE_OPEN_
c6b0: 45 58 43 4c 55 53 49 56 45 5d 0a 2a 2a 20 3c 2f  EXCLUSIVE].** </
c6c0: 75 6c 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 31 34  ul>.**.** {H1114
c6d0: 35 7d 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f  5} The [SQLITE_O
c6e0: 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53  PEN_DELETEONCLOS
c6f0: 45 5d 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68  E] flag means th
c700: 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65  e file should be
c710: 0a 2a 2a 20 64 65 6c 65 74 65 64 20 77 68 65 6e  .** deleted when
c720: 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 20 20   it is closed.  
c730: 7b 48 31 31 31 34 36 7d 20 54 68 65 20 5b 53 51  {H11146} The [SQ
c740: 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45  LITE_OPEN_DELETE
c750: 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 77 69 6c 6c  ONCLOSE].** will
c760: 20 62 65 20 73 65 74 20 66 6f 72 20 54 45 4d 50   be set for TEMP
c770: 20 20 64 61 74 61 62 61 73 65 73 2c 20 6a 6f 75    databases, jou
c780: 72 6e 61 6c 73 20 61 6e 64 20 66 6f 72 20 73 75  rnals and for su
c790: 62 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a  bjournals..**.**
c7a0: 20 7b 48 31 31 31 34 37 7d 20 54 68 65 20 5b 53   {H11147} The [S
c7b0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55  QLITE_OPEN_EXCLU
c7c0: 53 49 56 45 5d 20 66 6c 61 67 20 6d 65 61 6e 73  SIVE] flag means
c7d0: 20 74 68 65 20 66 69 6c 65 20 73 68 6f 75 6c 64   the file should
c7e0: 20 62 65 20 6f 70 65 6e 65 64 0a 2a 2a 20 66 6f   be opened.** fo
c7f0: 72 20 65 78 63 6c 75 73 69 76 65 20 61 63 63 65  r exclusive acce
c800: 73 73 2e 20 20 54 68 69 73 20 66 6c 61 67 20 69  ss.  This flag i
c810: 73 20 73 65 74 20 66 6f 72 20 61 6c 6c 20 66 69  s set for all fi
c820: 6c 65 73 20 65 78 63 65 70 74 0a 2a 2a 20 66 6f  les except.** fo
c830: 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  r the main datab
c840: 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  ase file..**.** 
c850: 7b 48 31 31 31 34 38 7d 20 41 74 20 6c 65 61 73  {H11148} At leas
c860: 74 20 73 7a 4f 73 46 69 6c 65 20 62 79 74 65 73  t szOsFile bytes
c870: 20 6f 66 20 6d 65 6d 6f 72 79 20 61 72 65 20 61   of memory are a
c880: 6c 6c 6f 63 61 74 65 64 20 62 79 20 53 51 4c 69  llocated by SQLi
c890: 74 65 0a 2a 2a 20 74 6f 20 68 6f 6c 64 20 74 68  te.** to hold th
c8a0: 65 20 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65  e  [sqlite3_file
c8b0: 5d 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73  ] structure pass
c8c0: 65 64 20 61 73 20 74 68 65 20 74 68 69 72 64 0a  ed as the third.
c8d0: 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78  ** argument to x
c8e0: 4f 70 65 6e 2e 20 7b 45 4e 44 7d 20 20 54 68 65  Open. {END}  The
c8f0: 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 64 6f   xOpen method do
c900: 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 0a 2a  es not have to.*
c910: 2a 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 73  * allocate the s
c920: 74 72 75 63 74 75 72 65 3b 20 69 74 20 73 68 6f  tructure; it sho
c930: 75 6c 64 20 6a 75 73 74 20 66 69 6c 6c 20 69 74  uld just fill it
c940: 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 31   in..**.** {H111
c950: 34 39 7d 20 54 68 65 20 66 6c 61 67 73 20 61 72  49} The flags ar
c960: 67 75 6d 65 6e 74 20 74 6f 20 78 41 63 63 65 73  gument to xAcces
c970: 73 28 29 20 6d 61 79 20 62 65 20 5b 53 51 4c 49  s() may be [SQLI
c980: 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53  TE_ACCESS_EXISTS
c990: 5d 0a 2a 2a 20 74 6f 20 74 65 73 74 20 66 6f 72  ].** to test for
c9a0: 20 74 68 65 20 65 78 69 73 74 65 6e 63 65 20 6f   the existence o
c9b0: 66 20 61 20 66 69 6c 65 2c 20 6f 72 20 5b 53 51  f a file, or [SQ
c9c0: 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44  LITE_ACCESS_READ
c9d0: 57 52 49 54 45 5d 20 74 6f 0a 2a 2a 20 74 65 73  WRITE] to.** tes
c9e0: 74 20 77 68 65 74 68 65 72 20 61 20 66 69 6c 65  t whether a file
c9f0: 20 69 73 20 72 65 61 64 61 62 6c 65 20 61 6e 64   is readable and
ca00: 20 77 72 69 74 61 62 6c 65 2c 20 6f 72 20 5b 53   writable, or [S
ca10: 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41  QLITE_ACCESS_REA
ca20: 44 5d 0a 2a 2a 20 74 6f 20 74 65 73 74 20 77 68  D].** to test wh
ca30: 65 74 68 65 72 20 61 20 66 69 6c 65 20 69 73 20  ether a file is 
ca40: 61 74 20 6c 65 61 73 74 20 72 65 61 64 61 62 6c  at least readabl
ca50: 65 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 66 69  e. {END}  The fi
ca60: 6c 65 20 63 61 6e 20 62 65 20 61 0a 2a 2a 20 64  le can be a.** d
ca70: 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20  irectory..**.** 
ca80: 7b 48 31 31 31 35 30 7d 20 53 51 4c 69 74 65 20  {H11150} SQLite 
ca90: 77 69 6c 6c 20 61 6c 77 61 79 73 20 61 6c 6c 6f  will always allo
caa0: 63 61 74 65 20 61 74 20 6c 65 61 73 74 20 6d 78  cate at least mx
cab0: 50 61 74 68 6e 61 6d 65 2b 31 20 62 79 74 65 73  Pathname+1 bytes
cac0: 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6f 75 74 70   for the.** outp
cad0: 75 74 20 62 75 66 66 65 72 20 78 46 75 6c 6c 50  ut buffer xFullP
cae0: 61 74 68 6e 61 6d 65 2e 20 7b 48 31 31 31 35 31  athname. {H11151
caf0: 7d 20 54 68 65 20 65 78 61 63 74 20 73 69 7a 65  } The exact size
cb00: 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 20 62   of the output b
cb10: 75 66 66 65 72 0a 2a 2a 20 69 73 20 61 6c 73 6f  uffer.** is also
cb20: 20 70 61 73 73 65 64 20 61 73 20 61 20 70 61 72   passed as a par
cb30: 61 6d 65 74 65 72 20 74 6f 20 62 6f 74 68 20 20  ameter to both  
cb40: 6d 65 74 68 6f 64 73 2e 20 7b 45 4e 44 7d 20 20  methods. {END}  
cb50: 49 66 20 74 68 65 20 6f 75 74 70 75 74 20 62 75  If the output bu
cb60: 66 66 65 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c  ffer.** is not l
cb70: 61 72 67 65 20 65 6e 6f 75 67 68 2c 20 5b 53 51  arge enough, [SQ
cb80: 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 73  LITE_CANTOPEN] s
cb90: 68 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65  hould be returne
cba0: 64 2e 20 53 69 6e 63 65 20 74 68 69 73 20 69 73  d. Since this is
cbb0: 0a 2a 2a 20 68 61 6e 64 6c 65 64 20 61 73 20 61  .** handled as a
cbc0: 20 66 61 74 61 6c 20 65 72 72 6f 72 20 62 79 20   fatal error by 
cbd0: 53 51 4c 69 74 65 2c 20 76 66 73 20 69 6d 70 6c  SQLite, vfs impl
cbe0: 65 6d 65 6e 74 61 74 69 6f 6e 73 20 73 68 6f 75  ementations shou
cbf0: 6c 64 20 65 6e 64 65 61 76 6f 72 0a 2a 2a 20 74  ld endeavor.** t
cc00: 6f 20 70 72 65 76 65 6e 74 20 74 68 69 73 20 62  o prevent this b
cc10: 79 20 73 65 74 74 69 6e 67 20 6d 78 50 61 74 68  y setting mxPath
cc20: 6e 61 6d 65 20 74 6f 20 61 20 73 75 66 66 69 63  name to a suffic
cc30: 69 65 6e 74 6c 79 20 6c 61 72 67 65 20 76 61 6c  iently large val
cc40: 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 52  ue..**.** The xR
cc50: 61 6e 64 6f 6d 6e 65 73 73 28 29 2c 20 78 53 6c  andomness(), xSl
cc60: 65 65 70 28 29 2c 20 61 6e 64 20 78 43 75 72 72  eep(), and xCurr
cc70: 65 6e 74 54 69 6d 65 28 29 20 69 6e 74 65 72 66  entTime() interf
cc80: 61 63 65 73 0a 2a 2a 20 61 72 65 20 6e 6f 74 20  aces.** are not 
cc90: 73 74 72 69 63 74 6c 79 20 61 20 70 61 72 74 20  strictly a part 
cca0: 6f 66 20 74 68 65 20 66 69 6c 65 73 79 73 74 65  of the filesyste
ccb0: 6d 2c 20 62 75 74 20 74 68 65 79 20 61 72 65 0a  m, but they are.
ccc0: 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74  ** included in t
ccd0: 68 65 20 56 46 53 20 73 74 72 75 63 74 75 72 65  he VFS structure
cce0: 20 66 6f 72 20 63 6f 6d 70 6c 65 74 65 6e 65 73   for completenes
ccf0: 73 2e 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64 6f  s..** The xRando
cd00: 6d 6e 65 73 73 28 29 20 66 75 6e 63 74 69 6f 6e  mness() function
cd10: 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 74   attempts to ret
cd20: 75 72 6e 20 6e 42 79 74 65 73 20 62 79 74 65 73  urn nBytes bytes
cd30: 0a 2a 2a 20 6f 66 20 67 6f 6f 64 2d 71 75 61 6c  .** of good-qual
cd40: 69 74 79 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69  ity randomness i
cd50: 6e 74 6f 20 7a 4f 75 74 2e 20 20 54 68 65 20 72  nto zOut.  The r
cd60: 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a  eturn value is.*
cd70: 2a 20 74 68 65 20 61 63 74 75 61 6c 20 6e 75 6d  * the actual num
cd80: 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
cd90: 72 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61 69  randomness obtai
cda0: 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 78 53 6c 65  ned..** The xSle
cdb0: 65 70 28 29 20 6d 65 74 68 6f 64 20 63 61 75 73  ep() method caus
cdc0: 65 73 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 74  es the calling t
cdd0: 68 72 65 61 64 20 74 6f 20 73 6c 65 65 70 20 66  hread to sleep f
cde0: 6f 72 20 61 74 0a 2a 2a 20 6c 65 61 73 74 20 74  or at.** least t
cdf0: 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63  he number of mic
ce00: 72 6f 73 65 63 6f 6e 64 73 20 67 69 76 65 6e 2e  roseconds given.
ce10: 20 20 54 68 65 20 78 43 75 72 72 65 6e 74 54 69    The xCurrentTi
ce20: 6d 65 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 72  me().** method r
ce30: 65 74 75 72 6e 73 20 61 20 4a 75 6c 69 61 6e 20  eturns a Julian 
ce40: 44 61 79 20 4e 75 6d 62 65 72 20 66 6f 72 20 74  Day Number for t
ce50: 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 65 20  he current date 
ce60: 61 6e 64 20 74 69 6d 65 2e 0a 2a 2f 0a 74 79 70  and time..*/.typ
ce70: 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
ce80: 74 65 33 5f 76 66 73 20 73 71 6c 69 74 65 33 5f  te3_vfs sqlite3_
ce90: 76 66 73 3b 0a 73 74 72 75 63 74 20 73 71 6c 69  vfs;.struct sqli
cea0: 74 65 33 5f 76 66 73 20 7b 0a 20 20 69 6e 74 20  te3_vfs {.  int 
ceb0: 69 56 65 72 73 69 6f 6e 3b 20 20 20 20 20 20 20  iVersion;       
cec0: 20 20 20 20 20 2f 2a 20 53 74 72 75 63 74 75 72       /* Structur
ced0: 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72  e version number
cee0: 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4f 73 46 69   */.  int szOsFi
cef0: 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  le;            /
cf00: 2a 20 53 69 7a 65 20 6f 66 20 73 75 62 63 6c 61  * Size of subcla
cf10: 73 73 65 64 20 73 71 6c 69 74 65 33 5f 66 69 6c  ssed sqlite3_fil
cf20: 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 61 74  e */.  int mxPat
cf30: 68 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20  hname;          
cf40: 2f 2a 20 4d 61 78 69 6d 75 6d 20 66 69 6c 65 20  /* Maximum file 
cf50: 70 61 74 68 6e 61 6d 65 20 6c 65 6e 67 74 68 20  pathname length 
cf60: 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  */.  sqlite3_vfs
cf70: 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a   *pNext;      /*
cf80: 20 4e 65 78 74 20 72 65 67 69 73 74 65 72 65 64   Next registered
cf90: 20 56 46 53 20 2a 2f 0a 20 20 63 6f 6e 73 74 20   VFS */.  const 
cfa0: 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
cfb0: 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
cfc0: 69 73 20 76 69 72 74 75 61 6c 20 66 69 6c 65 20  is virtual file 
cfd0: 73 79 73 74 65 6d 20 2a 2f 0a 20 20 76 6f 69 64  system */.  void
cfe0: 20 2a 70 41 70 70 44 61 74 61 3b 20 20 20 20 20   *pAppData;     
cff0: 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
d000: 74 6f 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73  to application-s
d010: 70 65 63 69 66 69 63 20 64 61 74 61 20 2a 2f 0a  pecific data */.
d020: 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73    int (*xOpen)(s
d030: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e  qlite3_vfs*, con
d040: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
d050: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 0a 20  sqlite3_file*,. 
d060: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
d070: 74 20 66 6c 61 67 73 2c 20 69 6e 74 20 2a 70 4f  t flags, int *pO
d080: 75 74 46 6c 61 67 73 29 3b 0a 20 20 69 6e 74 20  utFlags);.  int 
d090: 28 2a 78 44 65 6c 65 74 65 29 28 73 71 6c 69 74  (*xDelete)(sqlit
d0a0: 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63  e3_vfs*, const c
d0b0: 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20  har *zName, int 
d0c0: 73 79 6e 63 44 69 72 29 3b 0a 20 20 69 6e 74 20  syncDir);.  int 
d0d0: 28 2a 78 41 63 63 65 73 73 29 28 73 71 6c 69 74  (*xAccess)(sqlit
d0e0: 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63  e3_vfs*, const c
d0f0: 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20  har *zName, int 
d100: 66 6c 61 67 73 2c 20 69 6e 74 20 2a 70 52 65 73  flags, int *pRes
d110: 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46  Out);.  int (*xF
d120: 75 6c 6c 50 61 74 68 6e 61 6d 65 29 28 73 71 6c  ullPathname)(sql
d130: 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74  ite3_vfs*, const
d140: 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e   char *zName, in
d150: 74 20 6e 4f 75 74 2c 20 63 68 61 72 20 2a 7a 4f  t nOut, char *zO
d160: 75 74 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 78  ut);.  void *(*x
d170: 44 6c 4f 70 65 6e 29 28 73 71 6c 69 74 65 33 5f  DlOpen)(sqlite3_
d180: 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  vfs*, const char
d190: 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20   *zFilename);.  
d1a0: 76 6f 69 64 20 28 2a 78 44 6c 45 72 72 6f 72 29  void (*xDlError)
d1b0: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69  (sqlite3_vfs*, i
d1c0: 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a  nt nByte, char *
d1d0: 7a 45 72 72 4d 73 67 29 3b 0a 20 20 76 6f 69 64  zErrMsg);.  void
d1e0: 20 2a 28 2a 78 44 6c 53 79 6d 29 28 73 71 6c 69   *(*xDlSym)(sqli
d1f0: 74 65 33 5f 76 66 73 2a 2c 76 6f 69 64 2a 2c 20  te3_vfs*,void*, 
d200: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d  const char *zSym
d210: 62 6f 6c 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78  bol);.  void (*x
d220: 44 6c 43 6c 6f 73 65 29 28 73 71 6c 69 74 65 33  DlClose)(sqlite3
d230: 5f 76 66 73 2a 2c 20 76 6f 69 64 2a 29 3b 0a 20  _vfs*, void*);. 
d240: 20 69 6e 74 20 28 2a 78 52 61 6e 64 6f 6d 6e 65   int (*xRandomne
d250: 73 73 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a  ss)(sqlite3_vfs*
d260: 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 61  , int nByte, cha
d270: 72 20 2a 7a 4f 75 74 29 3b 0a 20 20 69 6e 74 20  r *zOut);.  int 
d280: 28 2a 78 53 6c 65 65 70 29 28 73 71 6c 69 74 65  (*xSleep)(sqlite
d290: 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6d 69 63 72  3_vfs*, int micr
d2a0: 6f 73 65 63 6f 6e 64 73 29 3b 0a 20 20 69 6e 74  oseconds);.  int
d2b0: 20 28 2a 78 43 75 72 72 65 6e 74 54 69 6d 65 29   (*xCurrentTime)
d2c0: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 64  (sqlite3_vfs*, d
d2d0: 6f 75 62 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28  ouble*);.  int (
d2e0: 2a 78 47 65 74 4c 61 73 74 45 72 72 6f 72 29 28  *xGetLastError)(
d2f0: 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e  sqlite3_vfs*, in
d300: 74 2c 20 63 68 61 72 20 2a 29 3b 0a 20 20 2f 2a  t, char *);.  /*
d310: 20 4e 65 77 20 66 69 65 6c 64 73 20 6d 61 79 20   New fields may 
d320: 62 65 20 61 70 70 65 6e 64 65 64 20 69 6e 20 66  be appended in f
d330: 69 67 75 72 65 20 76 65 72 73 69 6f 6e 73 2e 20  igure versions. 
d340: 20 54 68 65 20 69 56 65 72 73 69 6f 6e 0a 20 20   The iVersion.  
d350: 2a 2a 20 76 61 6c 75 65 20 77 69 6c 6c 20 69 6e  ** value will in
d360: 63 72 65 6d 65 6e 74 20 77 68 65 6e 65 76 65 72  crement whenever
d370: 20 74 68 69 73 20 68 61 70 70 65 6e 73 2e 20 2a   this happens. *
d380: 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  /.};../*.** CAPI
d390: 33 52 45 46 3a 20 46 6c 61 67 73 20 66 6f 72 20  3REF: Flags for 
d3a0: 74 68 65 20 78 41 63 63 65 73 73 20 56 46 53 20  the xAccess VFS 
d3b0: 6d 65 74 68 6f 64 20 7b 48 31 31 31 39 30 7d 20  method {H11190} 
d3c0: 3c 48 31 31 31 34 30 3e 0a 2a 2a 0a 2a 2a 20 7b  <H11140>.**.** {
d3d0: 48 31 31 31 39 31 7d 20 54 68 65 73 65 20 69 6e  H11191} These in
d3e0: 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 20  teger constants 
d3f0: 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 74  can be used as t
d400: 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74  he third paramet
d410: 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 78 41 63  er to.** the xAc
d420: 63 65 73 73 20 6d 65 74 68 6f 64 20 6f 66 20 61  cess method of a
d430: 6e 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20  n [sqlite3_vfs] 
d440: 6f 62 6a 65 63 74 2e 20 7b 45 4e 44 7d 20 20 54  object. {END}  T
d450: 68 65 79 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a  hey determine.**
d460: 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 70 65   what kind of pe
d470: 72 6d 69 73 73 69 6f 6e 73 20 74 68 65 20 78 41  rmissions the xA
d480: 63 63 65 73 73 20 6d 65 74 68 6f 64 20 69 73 20  ccess method is 
d490: 6c 6f 6f 6b 69 6e 67 20 66 6f 72 2e 0a 2a 2a 20  looking for..** 
d4a0: 7b 48 31 31 31 39 32 7d 20 57 69 74 68 20 53 51  {H11192} With SQ
d4b0: 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53  LITE_ACCESS_EXIS
d4c0: 54 53 2c 20 74 68 65 20 78 41 63 63 65 73 73 20  TS, the xAccess 
d4d0: 6d 65 74 68 6f 64 0a 2a 2a 20 73 69 6d 70 6c 79  method.** simply
d4e0: 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 72 20   checks whether 
d4f0: 74 68 65 20 66 69 6c 65 20 65 78 69 73 74 73 2e  the file exists.
d500: 0a 2a 2a 20 7b 48 31 31 31 39 33 7d 20 57 69 74  .** {H11193} Wit
d510: 68 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  h SQLITE_ACCESS_
d520: 52 45 41 44 57 52 49 54 45 2c 20 74 68 65 20 78  READWRITE, the x
d530: 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a 2a  Access method.**
d540: 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 72 20   checks whether 
d550: 74 68 65 20 66 69 6c 65 20 69 73 20 62 6f 74 68  the file is both
d560: 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77 72   readable and wr
d570: 69 74 61 62 6c 65 2e 0a 2a 2a 20 7b 48 31 31 31  itable..** {H111
d580: 39 34 7d 20 57 69 74 68 20 53 51 4c 49 54 45 5f  94} With SQLITE_
d590: 41 43 43 45 53 53 5f 52 45 41 44 2c 20 74 68 65  ACCESS_READ, the
d5a0: 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a   xAccess method.
d5b0: 2a 2a 20 63 68 65 63 6b 73 20 77 68 65 74 68 65  ** checks whethe
d5c0: 72 20 74 68 65 20 66 69 6c 65 20 69 73 20 72 65  r the file is re
d5d0: 61 64 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69  adable..*/.#defi
d5e0: 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  ne SQLITE_ACCESS
d5f0: 5f 45 58 49 53 54 53 20 20 20 20 30 0a 23 64 65  _EXISTS    0.#de
d600: 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 45  fine SQLITE_ACCE
d610: 53 53 5f 52 45 41 44 57 52 49 54 45 20 31 0a 23  SS_READWRITE 1.#
d620: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 43  define SQLITE_AC
d630: 43 45 53 53 5f 52 45 41 44 20 20 20 20 20 20 32  CESS_READ      2
d640: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
d650: 3a 20 49 6e 69 74 69 61 6c 69 7a 65 20 54 68 65  : Initialize The
d660: 20 53 51 4c 69 74 65 20 4c 69 62 72 61 72 79 20   SQLite Library 
d670: 7b 48 31 30 31 33 30 7d 20 3c 53 32 30 30 30 30  {H10130} <S20000
d680: 3e 3c 53 33 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20  ><S30100>.**.** 
d690: 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  The sqlite3_init
d6a0: 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e 65  ialize() routine
d6b0: 20 69 6e 69 74 69 61 6c 69 7a 65 73 20 74 68 65   initializes the
d6c0: 0a 2a 2a 20 53 51 4c 69 74 65 20 6c 69 62 72 61  .** SQLite libra
d6d0: 72 79 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  ry.  The sqlite3
d6e0: 5f 73 68 75 74 64 6f 77 6e 28 29 20 72 6f 75 74  _shutdown() rout
d6f0: 69 6e 65 0a 2a 2a 20 64 65 61 6c 6c 6f 63 61 74  ine.** deallocat
d700: 65 73 20 61 6e 79 20 72 65 73 6f 75 72 63 65 73  es any resources
d710: 20 74 68 61 74 20 77 65 72 65 20 61 6c 6c 6f 63   that were alloc
d720: 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  ated by sqlite3_
d730: 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e 0a 2a 2a  initialize()..**
d740: 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71  .** A call to sq
d750: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
d760: 28 29 20 69 73 20 61 6e 20 22 65 66 66 65 63 74  () is an "effect
d770: 69 76 65 22 20 63 61 6c 6c 20 69 66 20 69 74 20  ive" call if it 
d780: 69 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20  is.** the first 
d790: 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69 6e 69  time sqlite3_ini
d7a0: 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 6e 76  tialize() is inv
d7b0: 6f 6b 65 64 20 64 75 72 69 6e 67 20 74 68 65 20  oked during the 
d7c0: 6c 69 66 65 74 69 6d 65 20 6f 66 0a 2a 2a 20 74  lifetime of.** t
d7d0: 68 65 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 69  he process, or i
d7e0: 66 20 69 74 20 69 73 20 74 68 65 20 66 69 72 73  f it is the firs
d7f0: 74 20 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69  t time sqlite3_i
d800: 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69  nitialize() is i
d810: 6e 76 6f 6b 65 64 0a 2a 2a 20 66 6f 6c 6c 6f 77  nvoked.** follow
d820: 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71  ing a call to sq
d830: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29  lite3_shutdown()
d840: 2e 20 20 4f 6e 6c 79 20 61 6e 20 65 66 66 65 63  .  Only an effec
d850: 74 69 76 65 20 63 61 6c 6c 0a 2a 2a 20 6f 66 20  tive call.** of 
d860: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
d870: 7a 65 28 29 20 64 6f 65 73 20 61 6e 79 20 69 6e  ze() does any in
d880: 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 20 20 41  itialization.  A
d890: 6c 6c 20 6f 74 68 65 72 20 63 61 6c 6c 73 0a 2a  ll other calls.*
d8a0: 2a 20 61 72 65 20 68 61 72 6d 6c 65 73 73 20 6e  * are harmless n
d8b0: 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 41 6d 6f  o-ops..**.** Amo
d8c0: 6e 67 20 6f 74 68 65 72 20 74 68 69 6e 67 73 2c  ng other things,
d8d0: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
d8e0: 69 7a 65 28 29 20 73 68 61 6c 6c 20 69 6e 76 6f  ize() shall invo
d8f0: 6b 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73  ke.** sqlite3_os
d900: 5f 69 6e 69 74 28 29 2e 20 20 53 69 6d 69 6c 61  _init().  Simila
d910: 72 6c 79 2c 20 73 71 6c 69 74 65 33 5f 73 68 75  rly, sqlite3_shu
d920: 74 64 6f 77 6e 28 29 0a 2a 2a 20 73 68 61 6c 6c  tdown().** shall
d930: 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f   invoke sqlite3_
d940: 6f 73 5f 65 6e 64 28 29 2e 0a 2a 2a 0a 2a 2a 20  os_end()..**.** 
d950: 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  The sqlite3_init
d960: 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e 65  ialize() routine
d970: 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
d980: 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 2e  _OK] on success.
d990: 0a 2a 2a 20 49 66 20 66 6f 72 20 73 6f 6d 65 20  .** If for some 
d9a0: 72 65 61 73 6f 6e 2c 20 73 71 6c 69 74 65 33 5f  reason, sqlite3_
d9b0: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20  initialize() is 
d9c0: 75 6e 61 62 6c 65 20 74 6f 20 69 6e 69 74 69 61  unable to initia
d9d0: 6c 69 7a 65 0a 2a 2a 20 74 68 65 20 6c 69 62 72  lize.** the libr
d9e0: 61 72 79 20 28 70 65 72 68 61 70 73 20 69 74 20  ary (perhaps it 
d9f0: 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c  is unable to all
da00: 6f 63 61 74 65 20 61 20 6e 65 65 64 65 64 20 72  ocate a needed r
da10: 65 73 6f 75 72 63 65 20 73 75 63 68 0a 2a 2a 20  esource such.** 
da20: 61 73 20 61 20 6d 75 74 65 78 29 20 69 74 20 72  as a mutex) it r
da30: 65 74 75 72 6e 73 20 61 6e 20 5b 65 72 72 6f 72  eturns an [error
da40: 20 63 6f 64 65 5d 20 6f 74 68 65 72 20 74 68 61   code] other tha
da50: 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a  n [SQLITE_OK]..*
da60: 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
da70: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 72 6f  _initialize() ro
da80: 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
da90: 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79 20 6d 61  internally by ma
daa0: 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 53 51 4c 69  ny other.** SQLi
dab0: 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 73 6f  te interfaces so
dac0: 20 74 68 61 74 20 61 6e 20 61 70 70 6c 69 63 61   that an applica
dad0: 74 69 6f 6e 20 75 73 75 61 6c 6c 79 20 64 6f 65  tion usually doe
dae0: 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 0a 2a 2a  s not need to.**
daf0: 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f   invoke sqlite3_
db00: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 64 69 72  initialize() dir
db10: 65 63 74 6c 79 2e 20 20 46 6f 72 20 65 78 61 6d  ectly.  For exam
db20: 70 6c 65 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70  ple, [sqlite3_op
db30: 65 6e 28 29 5d 0a 2a 2a 20 63 61 6c 6c 73 20 73  en()].** calls s
db40: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
db50: 65 28 29 20 73 6f 20 74 68 65 20 53 51 4c 69 74  e() so the SQLit
db60: 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 62  e library will b
db70: 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a  e automatically.
db80: 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 77  ** initialized w
db90: 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  hen [sqlite3_ope
dba0: 6e 28 29 5d 20 69 73 20 63 61 6c 6c 65 64 20 69  n()] is called i
dbb0: 66 20 69 74 20 68 61 73 20 6e 6f 74 20 62 65 20  f it has not be 
dbc0: 69 6e 69 74 69 61 6c 69 7a 65 64 0a 2a 2a 20 61  initialized.** a
dbd0: 6c 72 65 61 64 79 2e 20 20 48 6f 77 65 76 65 72  lready.  However
dbe0: 2c 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 63  , if SQLite is c
dbf0: 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65  ompiled with the
dc00: 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55   [SQLITE_OMIT_AU
dc10: 54 4f 49 4e 49 54 5d 0a 2a 2a 20 63 6f 6d 70 69  TOINIT].** compi
dc20: 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 2c 20  le-time option, 
dc30: 74 68 65 6e 20 74 68 65 20 61 75 74 6f 6d 61 74  then the automat
dc40: 69 63 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69  ic calls to sqli
dc50: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
dc60: 0a 2a 2a 20 61 72 65 20 6f 6d 69 74 74 65 64 20  .** are omitted 
dc70: 61 6e 64 20 74 68 65 20 61 70 70 6c 69 63 61 74  and the applicat
dc80: 69 6f 6e 20 6d 75 73 74 20 63 61 6c 6c 20 73 71  ion must call sq
dc90: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
dca0: 28 29 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 70  () directly.** p
dcb0: 72 69 6f 72 20 74 6f 20 75 73 69 6e 67 20 61 6e  rior to using an
dcc0: 79 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 69  y other SQLite i
dcd0: 6e 74 65 72 66 61 63 65 2e 20 20 46 6f 72 20 6d  nterface.  For m
dce0: 61 78 69 6d 75 6d 20 70 6f 72 74 61 62 69 6c 69  aximum portabili
dcf0: 74 79 2c 0a 2a 2a 20 69 74 20 69 73 20 72 65 63  ty,.** it is rec
dd00: 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 61 70  ommended that ap
dd10: 70 6c 69 63 61 74 69 6f 6e 73 20 61 6c 77 61 79  plications alway
dd20: 73 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33  s invoke sqlite3
dd30: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 0a 2a 2a  _initialize().**
dd40: 20 64 69 72 65 63 74 6c 79 20 70 72 69 6f 72 20   directly prior 
dd50: 74 6f 20 75 73 69 6e 67 20 61 6e 79 20 6f 74 68  to using any oth
dd60: 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66  er SQLite interf
dd70: 61 63 65 2e 20 20 46 75 74 75 72 65 20 72 65 6c  ace.  Future rel
dd80: 65 61 73 65 73 0a 2a 2a 20 6f 66 20 53 51 4c 69  eases.** of SQLi
dd90: 74 65 20 6d 61 79 20 72 65 71 75 69 72 65 20 74  te may require t
dda0: 68 69 73 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  his.  In other w
ddb0: 6f 72 64 73 2c 20 74 68 65 20 62 65 68 61 76 69  ords, the behavi
ddc0: 6f 72 20 65 78 68 69 62 69 74 65 64 0a 2a 2a 20  or exhibited.** 
ddd0: 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63  when SQLite is c
dde0: 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 5b 53 51  ompiled with [SQ
ddf0: 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
de00: 49 54 5d 20 6d 69 67 68 74 20 62 65 63 6f 6d 65  IT] might become
de10: 20 74 68 65 0a 2a 2a 20 64 65 66 61 75 6c 74 20   the.** default 
de20: 62 65 68 61 76 69 6f 72 20 69 6e 20 73 6f 6d 65  behavior in some
de30: 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 20   future release 
de40: 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a  of SQLite..**.**
de50: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f   The sqlite3_os_
de60: 69 6e 69 74 28 29 20 72 6f 75 74 69 6e 65 20 64  init() routine d
de70: 6f 65 73 20 6f 70 65 72 61 74 69 6e 67 2d 73 79  oes operating-sy
de80: 73 74 65 6d 20 73 70 65 63 69 66 69 63 0a 2a 2a  stem specific.**
de90: 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
dea0: 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69  of the SQLite li
deb0: 62 72 61 72 79 2e 20 20 54 68 65 20 73 71 6c 69  brary.  The sqli
dec0: 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a 2a 2a 20  te3_os_end().** 
ded0: 72 6f 75 74 69 6e 65 20 75 6e 64 6f 65 73 20 74  routine undoes t
dee0: 68 65 20 65 66 66 65 63 74 20 6f 66 20 73 71 6c  he effect of sql
def0: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 2e 20  ite3_os_init(). 
df00: 20 54 79 70 69 63 61 6c 20 74 61 73 6b 73 0a 2a   Typical tasks.*
df10: 2a 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74  * performed by t
df20: 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 69 6e  hese routines in
df30: 63 6c 75 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  clude allocation
df40: 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 6f 6e   or deallocation
df50: 0a 2a 2a 20 6f 66 20 73 74 61 74 69 63 20 72 65  .** of static re
df60: 73 6f 75 72 63 65 73 2c 20 69 6e 69 74 69 61 6c  sources, initial
df70: 69 7a 61 74 69 6f 6e 20 6f 66 20 67 6c 6f 62 61  ization of globa
df80: 6c 20 76 61 72 69 61 62 6c 65 73 2c 0a 2a 2a 20  l variables,.** 
df90: 73 65 74 74 69 6e 67 20 75 70 20 61 20 64 65 66  setting up a def
dfa0: 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66  ault [sqlite3_vf
dfb0: 73 5d 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 73 65  s] module, or se
dfc0: 74 74 69 6e 67 20 75 70 0a 2a 2a 20 61 20 64 65  tting up.** a de
dfd0: 66 61 75 6c 74 20 63 6f 6e 66 69 67 75 72 61 74  fault configurat
dfe0: 69 6f 6e 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  ion using [sqlit
dff0: 65 33 5f 63 6f 6e 66 69 67 28 29 5d 2e 0a 2a 2a  e3_config()]..**
e000: 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74  .** The applicat
e010: 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65 72  ion should never
e020: 20 69 6e 76 6f 6b 65 20 65 69 74 68 65 72 20 73   invoke either s
e030: 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29  qlite3_os_init()
e040: 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f  .** or sqlite3_o
e050: 73 5f 65 6e 64 28 29 20 64 69 72 65 63 74 6c 79  s_end() directly
e060: 2e 20 20 54 68 65 20 61 70 70 6c 69 63 61 74 69  .  The applicati
e070: 6f 6e 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 69  on should only i
e080: 6e 76 6f 6b 65 0a 2a 2a 20 73 71 6c 69 74 65 33  nvoke.** sqlite3
e090: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 61 6e  _initialize() an
e0a0: 64 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f  d sqlite3_shutdo
e0b0: 77 6e 28 29 2e 20 20 54 68 65 20 73 71 6c 69 74  wn().  The sqlit
e0c0: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 0a 2a 2a 20  e3_os_init().** 
e0d0: 69 6e 74 65 72 66 61 63 65 20 69 73 20 63 61 6c  interface is cal
e0e0: 6c 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  led automaticall
e0f0: 79 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e 69  y by sqlite3_ini
e100: 74 69 61 6c 69 7a 65 28 29 20 61 6e 64 0a 2a 2a  tialize() and.**
e110: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28   sqlite3_os_end(
e120: 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 73  ) is called by s
e130: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28  qlite3_shutdown(
e140: 29 2e 20 20 41 70 70 72 6f 70 72 69 61 74 65 0a  ).  Appropriate.
e150: 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
e160: 6e 73 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f  ns for sqlite3_o
e170: 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 71 6c  s_init() and sql
e180: 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a 2a 2a  ite3_os_end().**
e190: 20 61 72 65 20 62 75 69 6c 74 20 69 6e 74 6f 20   are built into 
e1a0: 53 51 4c 69 74 65 20 77 68 65 6e 20 69 74 20 69  SQLite when it i
e1b0: 73 20 63 6f 6d 70 69 6c 65 64 20 66 6f 72 20 75  s compiled for u
e1c0: 6e 69 78 2c 20 77 69 6e 64 6f 77 73 2c 20 6f 72  nix, windows, or
e1d0: 20 6f 73 2f 32 2e 0a 2a 2a 20 57 68 65 6e 20 62   os/2..** When b
e1e0: 75 69 6c 74 20 66 6f 72 20 6f 74 68 65 72 20 70  uilt for other p
e1f0: 6c 61 74 66 6f 72 6d 73 20 28 75 73 69 6e 67 20  latforms (using 
e200: 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f 53 5f 4f  the [SQLITE_OS_O
e210: 54 48 45 52 3d 31 5d 20 63 6f 6d 70 69 6c 65 2d  THER=1] compile-
e220: 74 69 6d 65 0a 2a 2a 20 6f 70 74 69 6f 6e 29 20  time.** option) 
e230: 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
e240: 6d 75 73 74 20 73 75 70 70 6c 79 20 61 20 73 75  must supply a su
e250: 69 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  itable implement
e260: 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 73 71 6c  ation for.** sql
e270: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61  ite3_os_init() a
e280: 6e 64 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e  nd sqlite3_os_en
e290: 64 28 29 2e 20 20 41 6e 20 61 70 70 6c 69 63 61  d().  An applica
e2a0: 74 69 6f 6e 2d 73 75 70 70 6c 69 65 64 0a 2a 2a  tion-supplied.**
e2b0: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
e2c0: 6f 66 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  of sqlite3_os_in
e2d0: 69 74 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f  it() or sqlite3_
e2e0: 6f 73 5f 65 6e 64 28 29 0a 2a 2a 20 6d 75 73 74  os_end().** must
e2f0: 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f   return [SQLITE_
e300: 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 20 61  OK] on success a
e310: 6e 64 20 73 6f 6d 65 20 6f 74 68 65 72 20 5b 65  nd some other [e
e320: 72 72 6f 72 20 63 6f 64 65 5d 20 75 70 6f 6e 0a  rror code] upon.
e330: 2a 2a 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 53  ** failure..*/.S
e340: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
e350: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
e360: 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41  (void);.SQLITE_A
e370: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73  PI int sqlite3_s
e380: 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 3b 0a 53  hutdown(void);.S
e390: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
e3a0: 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f  lite3_os_init(vo
e3b0: 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  id);.SQLITE_API 
e3c0: 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65  int sqlite3_os_e
e3d0: 6e 64 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a  nd(void);../*.**
e3e0: 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69   CAPI3REF: Confi
e3f0: 67 75 72 69 6e 67 20 54 68 65 20 53 51 4c 69 74  guring The SQLit
e400: 65 20 4c 69 62 72 61 72 79 20 7b 48 31 34 31 30  e Library {H1410
e410: 30 7d 20 3c 53 32 30 30 30 30 3e 3c 53 33 30 32  0} <S20000><S302
e420: 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  00>.** EXPERIMEN
e430: 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  TAL.**.** The sq
e440: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 69  lite3_config() i
e450: 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64  nterface is used
e460: 20 74 6f 20 6d 61 6b 65 20 67 6c 6f 62 61 6c 20   to make global 
e470: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 0a 2a 2a  configuration.**
e480: 20 63 68 61 6e 67 65 73 20 74 6f 20 53 51 4c 69   changes to SQLi
e490: 74 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 74  te in order to t
e4a0: 75 6e 65 20 53 51 4c 69 74 65 20 74 6f 20 74 68  une SQLite to th
e4b0: 65 20 73 70 65 63 69 66 69 63 20 6e 65 65 64 73  e specific needs
e4c0: 20 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69   of.** the appli
e4d0: 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 64 65 66  cation.  The def
e4e0: 61 75 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 69  ault configurati
e4f0: 6f 6e 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65  on is recommende
e500: 64 20 66 6f 72 20 6d 6f 73 74 0a 2a 2a 20 61 70  d for most.** ap
e510: 70 6c 69 63 61 74 69 6f 6e 73 20 61 6e 64 20 73  plications and s
e520: 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  o this routine i
e530: 73 20 75 73 75 61 6c 6c 79 20 6e 6f 74 20 6e 65  s usually not ne
e540: 63 65 73 73 61 72 79 2e 20 20 49 74 20 69 73 0a  cessary.  It is.
e550: 2a 2a 20 70 72 6f 76 69 64 65 64 20 74 6f 20 73  ** provided to s
e560: 75 70 70 6f 72 74 20 72 61 72 65 20 61 70 70 6c  upport rare appl
e570: 69 63 61 74 69 6f 6e 73 20 77 69 74 68 20 75 6e  ications with un
e580: 75 73 75 61 6c 20 6e 65 65 64 73 2e 0a 2a 2a 0a  usual needs..**.
e590: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63  ** The sqlite3_c
e5a0: 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 63  onfig() interfac
e5b0: 65 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73  e is not threads
e5c0: 61 66 65 2e 20 20 54 68 65 20 61 70 70 6c 69 63  afe.  The applic
e5d0: 61 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e  ation.** must in
e5e0: 73 75 72 65 20 74 68 61 74 20 6e 6f 20 6f 74 68  sure that no oth
e5f0: 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66  er SQLite interf
e600: 61 63 65 73 20 61 72 65 20 69 6e 76 6f 6b 65 64  aces are invoked
e610: 20 62 79 20 6f 74 68 65 72 0a 2a 2a 20 74 68 72   by other.** thr
e620: 65 61 64 73 20 77 68 69 6c 65 20 73 71 6c 69 74  eads while sqlit
e630: 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 72  e3_config() is r
e640: 75 6e 6e 69 6e 67 2e 20 20 46 75 72 74 68 65 72  unning.  Further
e650: 6d 6f 72 65 2c 20 73 71 6c 69 74 65 33 5f 63 6f  more, sqlite3_co
e660: 6e 66 69 67 28 29 0a 2a 2a 20 6d 61 79 20 6f 6e  nfig().** may on
e670: 6c 79 20 62 65 20 69 6e 76 6f 6b 65 64 20 70 72  ly be invoked pr
e680: 69 6f 72 20 74 6f 20 6c 69 62 72 61 72 79 20 69  ior to library i
e690: 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 75 73  nitialization us
e6a0: 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  ing.** [sqlite3_
e6b0: 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 20 6f 72  initialize()] or
e6c0: 20 61 66 74 65 72 20 73 68 75 74 64 6f 77 6e 20   after shutdown 
e6d0: 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74  by [sqlite3_shut
e6e0: 64 6f 77 6e 28 29 5d 2e 0a 2a 2a 20 4e 6f 74 65  down()]..** Note
e6f0: 2c 20 68 6f 77 65 76 65 72 2c 20 74 68 61 74 20  , however, that 
e700: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
e710: 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 61   can be called a
e720: 73 20 70 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a  s part of the.**
e730: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
e740: 6f 66 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f  of an applicatio
e750: 6e 2d 64 65 66 69 6e 65 64 20 5b 73 71 6c 69 74  n-defined [sqlit
e760: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 5d 2e 0a 2a  e3_os_init()]..*
e770: 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61  *.** The first a
e780: 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74  rgument to sqlit
e790: 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 61  e3_config() is a
e7a0: 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 5b 53 51  n integer.** [SQ
e7b0: 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47  LITE_CONFIG_SING
e7c0: 4c 45 54 48 52 45 41 44 20 7c 20 63 6f 6e 66 69  LETHREAD | confi
e7d0: 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d  guration option]
e7e0: 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73   that determines
e7f0: 0a 2a 2a 20 77 68 61 74 20 70 72 6f 70 65 72 74  .** what propert
e800: 79 20 6f 66 20 53 51 4c 69 74 65 20 69 73 20 74  y of SQLite is t
e810: 6f 20 62 65 20 63 6f 6e 66 69 67 75 72 65 64 2e  o be configured.
e820: 20 20 53 75 62 73 65 71 75 65 6e 74 20 61 72 67    Subsequent arg
e830: 75 6d 65 6e 74 73 0a 2a 2a 20 76 61 72 79 20 64  uments.** vary d
e840: 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20  epending on the 
e850: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53  [SQLITE_CONFIG_S
e860: 49 4e 47 4c 45 54 48 52 45 41 44 20 7c 20 63 6f  INGLETHREAD | co
e870: 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69  nfiguration opti
e880: 6f 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69  on].** in the fi
e890: 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a  rst argument..**
e8a0: 0a 2a 2a 20 57 68 65 6e 20 61 20 63 6f 6e 66 69  .** When a confi
e8b0: 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20  guration option 
e8c0: 69 73 20 73 65 74 2c 20 73 71 6c 69 74 65 33 5f  is set, sqlite3_
e8d0: 63 6f 6e 66 69 67 28 29 20 72 65 74 75 72 6e 73  config() returns
e8e0: 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a   [SQLITE_OK]..**
e8f0: 20 49 66 20 74 68 65 20 6f 70 74 69 6f 6e 20 69   If the option i
e900: 73 20 75 6e 6b 6e 6f 77 6e 20 6f 72 20 53 51 4c  s unknown or SQL
e910: 69 74 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f  ite is unable to
e920: 20 73 65 74 20 74 68 65 20 6f 70 74 69 6f 6e 0a   set the option.
e930: 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  ** then this rou
e940: 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 6e  tine returns a n
e950: 6f 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63  on-zero [error c
e960: 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41  ode]..**.** INVA
e970: 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48  RIANTS:.**.** {H
e980: 31 34 31 30 33 7d 20 41 20 73 75 63 63 65 73 73  14103} A success
e990: 66 75 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  ful invocation o
e9a0: 66 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  f [sqlite3_confi
e9b0: 67 28 29 5d 20 73 68 61 6c 6c 20 72 65 74 75 72  g()] shall retur
e9c0: 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53  n.**          [S
e9d0: 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a  QLITE_OK]..**.**
e9e0: 20 7b 48 31 34 31 30 36 7d 20 54 68 65 20 5b 73   {H14106} The [s
e9f0: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d  qlite3_config()]
ea00: 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c   interface shall
ea10: 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f   return [SQLITE_
ea20: 4d 49 53 55 53 45 5d 0a 2a 2a 20 20 20 20 20 20  MISUSE].**      
ea30: 20 20 20 20 69 66 20 69 74 20 69 73 20 69 6e 76      if it is inv
ea40: 6f 6b 65 64 20 69 6e 20 62 65 74 77 65 65 6e 20  oked in between 
ea50: 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65  calls to [sqlite
ea60: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 20  3_initialize()] 
ea70: 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  and.**          
ea80: 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77  [sqlite3_shutdow
ea90: 6e 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34  n()]..**.** {H14
eaa0: 31 32 30 7d 20 41 20 73 75 63 63 65 73 73 66 75  120} A successfu
eab0: 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  l call to [sqlit
eac0: 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49  e3_config]([SQLI
ead0: 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45  TE_CONFIG_SINGLE
eae0: 54 48 52 45 41 44 5d 29 0a 2a 2a 20 20 20 20 20  THREAD]).**     
eaf0: 20 20 20 20 20 73 68 61 6c 6c 20 73 65 74 20 74       shall set t
eb00: 68 65 20 64 65 66 61 75 6c 74 20 5b 74 68 72 65  he default [thre
eb10: 61 64 69 6e 67 20 6d 6f 64 65 5d 20 74 6f 20 53  ading mode] to S
eb20: 69 6e 67 6c 65 2d 74 68 72 65 61 64 2e 0a 2a 2a  ingle-thread..**
eb30: 0a 2a 2a 20 7b 48 31 34 31 32 33 7d 20 41 20 73  .** {H14123} A s
eb40: 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74  uccessful call t
eb50: 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  o [sqlite3_confi
eb60: 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  g]([SQLITE_CONFI
eb70: 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 5d 29 0a  G_MULTITHREAD]).
eb80: 2a 2a 20 20 20 20 20 20 20 20 20 20 73 68 61 6c  **          shal
eb90: 6c 20 73 65 74 20 74 68 65 20 64 65 66 61 75 6c  l set the defaul
eba0: 74 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64  t [threading mod
ebb0: 65 5d 20 74 6f 20 4d 75 6c 74 69 2d 74 68 72 65  e] to Multi-thre
ebc0: 61 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 32  ad..**.** {H1412
ebd0: 36 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20  6} A successful 
ebe0: 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
ebf0: 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45  _config]([SQLITE
ec00: 5f 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a  _CONFIG_SERIALIZ
ec10: 45 44 5d 29 0a 2a 2a 20 20 20 20 20 20 20 20 20  ED]).**         
ec20: 20 73 68 61 6c 6c 20 73 65 74 20 74 68 65 20 64   shall set the d
ec30: 65 66 61 75 6c 74 20 5b 74 68 72 65 61 64 69 6e  efault [threadin
ec40: 67 20 6d 6f 64 65 5d 20 74 6f 20 53 65 72 69 61  g mode] to Seria
ec50: 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  lized..**.** {H1
ec60: 34 31 32 39 7d 20 41 20 73 75 63 63 65 73 73 66  4129} A successf
ec70: 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69  ul call to [sqli
ec80: 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c  te3_config]([SQL
ec90: 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58  ITE_CONFIG_MUTEX
eca0: 5d 2c 58 29 0a 2a 2a 20 20 20 20 20 20 20 20 20  ],X).**         
ecb0: 20 77 68 65 72 65 20 58 20 69 73 20 61 20 70 6f   where X is a po
ecc0: 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 69 74  inter to an init
ecd0: 69 61 6c 69 7a 65 64 20 5b 73 71 6c 69 74 65 33  ialized [sqlite3
ece0: 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 0a  _mutex_methods].
ecf0: 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 62 6a 65  **          obje
ed00: 63 74 20 73 68 61 6c 6c 20 63 61 75 73 65 20 61  ct shall cause a
ed10: 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 6d 75  ll subsequent mu
ed20: 74 65 78 20 6f 70 65 72 61 74 69 6f 6e 73 20 70  tex operations p
ed30: 65 72 66 6f 72 6d 65 64 0a 2a 2a 20 20 20 20 20  erformed.**     
ed40: 20 20 20 20 20 62 79 20 53 51 4c 69 74 65 20 74       by SQLite t
ed50: 6f 20 75 73 65 20 74 68 65 20 6d 75 74 65 78 20  o use the mutex 
ed60: 6d 65 74 68 6f 64 73 20 74 68 61 74 20 77 65 72  methods that wer
ed70: 65 20 70 72 65 73 65 6e 74 20 69 6e 20 58 0a 2a  e present in X.*
ed80: 2a 20 20 20 20 20 20 20 20 20 20 64 75 72 69 6e  *          durin
ed90: 67 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 5b 73  g the call to [s
eda0: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d  qlite3_config()]
edb0: 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 33 32 7d  ..**.** {H14132}
edc0: 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 61   A successful ca
edd0: 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63  ll to [sqlite3_c
ede0: 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43  onfig]([SQLITE_C
edf0: 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 5d 2c  ONFIG_GETMUTEX],
ee00: 58 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77  X).**          w
ee10: 68 65 72 65 20 58 20 69 73 20 61 20 70 6f 69 6e  here X is a poin
ee20: 74 65 72 20 74 6f 20 61 6e 20 5b 73 71 6c 69 74  ter to an [sqlit
ee30: 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
ee40: 5d 20 6f 62 6a 65 63 74 20 0a 2a 2a 20 20 20 20  ] object .**    
ee50: 20 20 20 20 20 20 73 68 61 6c 6c 20 6f 76 65 72        shall over
ee60: 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e  write the conten
ee70: 74 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 6d 75  t of [sqlite3_mu
ee80: 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a  tex_methods] obj
ee90: 65 63 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ect.**          
eea0: 77 69 74 68 20 74 68 65 20 6d 75 74 65 78 20 6d  with the mutex m
eeb0: 65 74 68 6f 64 73 20 63 75 72 72 65 6e 74 6c 79  ethods currently
eec0: 20 69 6e 20 75 73 65 20 62 79 20 53 51 4c 69 74   in use by SQLit
eed0: 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 33 35  e..**.** {H14135
eee0: 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63  } A successful c
eef0: 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
ef00: 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f  config]([SQLITE_
ef10: 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 2c 4d  CONFIG_MALLOC],M
ef20: 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68  ).**          wh
ef30: 65 72 65 20 4d 20 69 73 20 61 20 70 6f 69 6e 74  ere M is a point
ef40: 65 72 20 74 6f 20 61 6e 20 69 6e 69 74 69 61 6c  er to an initial
ef50: 69 7a 65 64 20 5b 73 71 6c 69 74 65 33 5f 6d 65  ized [sqlite3_me
ef60: 6d 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 20 20  m_methods].**   
ef70: 20 20 20 20 20 20 20 6f 62 6a 65 63 74 20 73 68         object sh
ef80: 61 6c 6c 20 63 61 75 73 65 20 61 6c 6c 20 73 75  all cause all su
ef90: 62 73 65 71 75 65 6e 74 20 6d 65 6d 6f 72 79 20  bsequent memory 
efa0: 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 70 65 72 61  allocation opera
efb0: 74 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20  tions.**        
efc0: 20 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 53    performed by S
efd0: 51 4c 69 74 65 20 74 6f 20 75 73 65 20 74 68 65  QLite to use the
efe0: 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 77 65   methods that we
eff0: 72 65 20 70 72 65 73 65 6e 74 20 69 6e 20 0a 2a  re present in .*
f000: 2a 20 20 20 20 20 20 20 20 20 20 4d 20 64 75 72  *          M dur
f010: 69 6e 67 20 74 68 65 20 63 61 6c 6c 20 74 6f 20  ing the call to 
f020: 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28  [sqlite3_config(
f030: 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 33  )]..**.** {H1413
f040: 38 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20  8} A successful 
f050: 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
f060: 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45  _config]([SQLITE
f070: 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c 4f  _CONFIG_GETMALLO
f080: 43 5d 2c 4d 29 0a 2a 2a 20 20 20 20 20 20 20 20  C],M).**        
f090: 20 20 77 68 65 72 65 20 4d 20 69 73 20 61 20 70    where M is a p
f0a0: 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 5b 73 71  ointer to an [sq
f0b0: 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
f0c0: 73 5d 20 6f 62 6a 65 63 74 20 73 68 61 6c 6c 0a  s] object shall.
f0d0: 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 76 65 72  **          over
f0e0: 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e  write the conten
f0f0: 74 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 6d 65  t of [sqlite3_me
f100: 6d 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63  m_methods] objec
f110: 74 20 77 69 74 68 20 0a 2a 2a 20 20 20 20 20 20  t with .**      
f120: 20 20 20 20 74 68 65 20 6d 65 6d 6f 72 79 20 61      the memory a
f130: 6c 6c 6f 63 61 74 69 6f 6e 20 6d 65 74 68 6f 64  llocation method
f140: 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75  s currently in u
f150: 73 65 20 62 79 0a 2a 2a 20 20 20 20 20 20 20 20  se by.**        
f160: 20 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20    SQLite..**.** 
f170: 7b 48 31 34 31 34 31 7d 20 41 20 73 75 63 63 65  {H14141} A succe
f180: 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73  ssful call to [s
f190: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b  qlite3_config]([
f1a0: 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45  SQLITE_CONFIG_ME
f1b0: 4d 53 54 41 54 55 53 5d 2c 31 29 0a 2a 2a 20 20  MSTATUS],1).**  
f1c0: 20 20 20 20 20 20 20 20 73 68 61 6c 6c 20 65 6e          shall en
f1d0: 61 62 6c 65 20 74 68 65 20 6d 65 6d 6f 72 79 20  able the memory 
f1e0: 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 74 61 74 75  allocation statu
f1f0: 73 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 6c 6f 67  s collection log
f200: 69 63 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 34  ic..**.** {H1414
f210: 34 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20  4} A successful 
f220: 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
f230: 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45  _config]([SQLITE
f240: 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55  _CONFIG_MEMSTATU
f250: 53 5d 2c 30 29 0a 2a 2a 20 20 20 20 20 20 20 20  S],0).**        
f260: 20 20 73 68 61 6c 6c 20 64 69 73 61 62 6c 65 20    shall disable 
f270: 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
f280: 61 74 69 6f 6e 20 73 74 61 74 75 73 20 63 6f 6c  ation status col
f290: 6c 65 63 74 69 6f 6e 20 6c 6f 67 69 63 2e 0a 2a  lection logic..*
f2a0: 2a 0a 2a 2a 20 7b 48 31 34 31 34 37 7d 20 54 68  *.** {H14147} Th
f2b0: 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
f2c0: 69 6f 6e 20 73 74 61 74 75 73 20 63 6f 6c 6c 65  ion status colle
f2d0: 63 74 69 6f 6e 20 6c 6f 67 69 63 20 73 68 61 6c  ction logic shal
f2e0: 6c 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  l be.**         
f2f0: 20 65 6e 61 62 6c 65 64 20 62 79 20 64 65 66 61   enabled by defa
f300: 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31  ult..**.** {H141
f310: 35 30 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c  50} A successful
f320: 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
f330: 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54  3_config]([SQLIT
f340: 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48  E_CONFIG_SCRATCH
f350: 5d 2c 53 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20  ],S,Z,N).**     
f360: 20 20 20 20 20 77 68 65 72 65 20 5a 20 61 6e 64       where Z and
f370: 20 4e 20 61 72 65 20 6e 6f 6e 2d 6e 65 67 61 74   N are non-negat
f380: 69 76 65 20 69 6e 74 65 67 65 72 73 20 61 6e 64  ive integers and
f390: 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 53 20   .**          S 
f3a0: 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
f3b0: 61 6e 20 61 6c 69 67 6e 65 64 20 6d 65 6d 6f 72  an aligned memor
f3c0: 79 20 62 75 66 66 65 72 20 6e 6f 74 20 6c 65 73  y buffer not les
f3d0: 73 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20  s than.**       
f3e0: 20 20 20 5a 2a 4e 20 62 79 74 65 73 20 69 6e 20     Z*N bytes in 
f3f0: 73 69 7a 65 20 73 68 61 6c 6c 20 63 61 75 73 65  size shall cause
f400: 20 53 20 74 6f 20 62 65 20 75 73 65 64 20 62 79   S to be used by
f410: 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
f420: 20 5b 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79   [scratch memory
f430: 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 66 6f 72 20   allocator] for 
f440: 61 73 20 6d 61 6e 79 20 61 73 20 4e 20 73 69 6d  as many as N sim
f450: 75 6c 61 74 61 6e 65 6f 75 73 0a 2a 2a 20 20 20  ulataneous.**   
f460: 20 20 20 20 20 20 20 61 6c 6c 6f 63 61 74 69 6f         allocatio
f470: 6e 73 20 65 61 63 68 20 6f 66 20 73 69 7a 65 20  ns each of size 
f480: 5a 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 35 33  Z..**.** {H14153
f490: 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63  } A successful c
f4a0: 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
f4b0: 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f  config]([SQLITE_
f4c0: 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 2c  CONFIG_SCRATCH],
f4d0: 53 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 20 20  S,Z,N).**       
f4e0: 20 20 20 77 68 65 72 65 20 53 20 69 73 20 61 20     where S is a 
f4f0: 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 73 68 61  NULL pointer sha
f500: 6c 6c 20 64 69 73 61 62 6c 65 20 74 68 65 0a 2a  ll disable the.*
f510: 2a 20 20 20 20 20 20 20 20 20 20 5b 73 63 72 61  *          [scra
f520: 74 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  tch memory alloc
f530: 61 74 6f 72 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  ator]..**.** {H1
f540: 34 31 35 36 7d 20 41 20 73 75 63 63 65 73 73 66  4156} A successf
f550: 75 6c 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20  ul call to.**   
f560: 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
f570: 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f  config]([SQLITE_
f580: 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45  CONFIG_PAGECACHE
f590: 5d 2c 53 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20  ],S,Z,N).**     
f5a0: 20 20 20 20 20 77 68 65 72 65 20 5a 20 61 6e 64       where Z and
f5b0: 20 4e 20 61 72 65 20 6e 6f 6e 2d 6e 65 67 61 74   N are non-negat
f5c0: 69 76 65 20 69 6e 74 65 67 65 72 73 20 61 6e 64  ive integers and
f5d0: 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 53 20   .**          S 
f5e0: 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
f5f0: 61 6e 20 61 6c 69 67 6e 65 64 20 6d 65 6d 6f 72  an aligned memor
f600: 79 20 62 75 66 66 65 72 20 6e 6f 74 20 6c 65 73  y buffer not les
f610: 73 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20  s than.**       
f620: 20 20 20 5a 2a 4e 20 62 79 74 65 73 20 69 6e 20     Z*N bytes in 
f630: 73 69 7a 65 20 73 68 61 6c 6c 20 63 61 75 73 65  size shall cause
f640: 20 53 20 74 6f 20 62 65 20 75 73 65 64 20 62 79   S to be used by
f650: 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
f660: 20 5b 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f   [pagecache memo
f670: 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 66 6f  ry allocator] fo
f680: 72 20 61 73 20 6d 61 6e 79 20 61 73 20 4e 20 73  r as many as N s
f690: 69 6d 75 6c 61 74 61 6e 65 6f 75 73 0a 2a 2a 20  imulataneous.** 
f6a0: 20 20 20 20 20 20 20 20 20 61 6c 6c 6f 63 61 74           allocat
f6b0: 69 6f 6e 73 20 65 61 63 68 20 6f 66 20 73 69 7a  ions each of siz
f6c0: 65 20 5a 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31  e Z..**.** {H141
f6d0: 35 39 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c  59} A successful
f6e0: 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 20 20   call to.**     
f6f0: 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f       [sqlite3_co
f700: 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f  nfig]([SQLITE_CO
f710: 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2c  NFIG_PAGECACHE],
f720: 53 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 20 20  S,Z,N).**       
f730: 20 20 20 77 68 65 72 65 20 53 20 69 73 20 61 20     where S is a 
f740: 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 73 68 61  NULL pointer sha
f750: 6c 6c 20 64 69 73 61 62 6c 65 20 74 68 65 0a 2a  ll disable the.*
f760: 2a 20 20 20 20 20 20 20 20 20 20 5b 70 61 67 65  *          [page
f770: 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  cache memory all
f780: 6f 63 61 74 6f 72 5d 2e 0a 2a 2a 0a 2a 2a 20 7b  ocator]..**.** {
f790: 48 31 34 31 36 32 7d 20 41 20 73 75 63 63 65 73  H14162} A succes
f7a0: 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71  sful call to [sq
f7b0: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53  lite3_config]([S
f7c0: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48 45 41  QLITE_CONFIG_HEA
f7d0: 50 5d 2c 48 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20  P],H,Z,N).**    
f7e0: 20 20 20 20 20 20 77 68 65 72 65 20 5a 20 61 6e        where Z an
f7f0: 64 20 4e 20 61 72 65 20 6e 6f 6e 2d 6e 65 67 61  d N are non-nega
f800: 74 69 76 65 20 69 6e 74 65 67 65 72 73 20 61 6e  tive integers an
f810: 64 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 48  d .**          H
f820: 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
f830: 20 61 6e 20 61 6c 69 67 6e 65 64 20 6d 65 6d 6f   an aligned memo
f840: 72 79 20 62 75 66 66 65 72 20 6e 6f 74 20 6c 65  ry buffer not le
f850: 73 73 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20  ss than.**      
f860: 20 20 20 20 5a 20 62 79 74 65 73 20 69 6e 20 73      Z bytes in s
f870: 69 7a 65 20 73 68 61 6c 6c 20 65 6e 61 62 6c 65  ize shall enable
f880: 20 74 68 65 20 5b 6d 65 6d 73 79 73 35 5d 20 6d   the [memsys5] m
f890: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 0a  emory allocator.
f8a0: 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e 64 20  **          and 
f8b0: 63 61 75 73 65 20 69 74 20 74 6f 20 75 73 65 20  cause it to use 
f8c0: 62 75 66 66 65 72 20 53 20 61 73 20 69 74 73 20  buffer S as its 
f8d0: 6d 65 6d 6f 72 79 20 73 6f 75 72 63 65 20 61 6e  memory source an
f8e0: 64 20 74 6f 20 75 73 65 0a 2a 2a 20 20 20 20 20  d to use.**     
f8f0: 20 20 20 20 20 61 20 6d 69 6e 69 6d 75 6d 20 61       a minimum a
f900: 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 6f  llocation size o
f910: 66 20 4e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31  f N..**.** {H141
f920: 36 35 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c  65} A successful
f930: 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
f940: 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54  3_config]([SQLIT
f950: 45 5f 43 4f 4e 46 49 47 5f 48 45 41 50 5d 2c 48  E_CONFIG_HEAP],H
f960: 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 20 20 20  ,Z,N).**        
f970: 20 20 77 68 65 72 65 20 48 20 69 73 20 61 20 4e    where H is a N
f980: 55 4c 4c 20 70 6f 69 6e 74 65 72 20 73 68 61 6c  ULL pointer shal
f990: 6c 20 64 69 73 61 62 6c 65 20 74 68 65 0a 2a 2a  l disable the.**
f9a0: 20 20 20 20 20 20 20 20 20 20 5b 6d 65 6d 73 79            [memsy
f9b0: 73 35 5d 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  s5] memory alloc
f9c0: 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34  ator..**.** {H14
f9d0: 31 36 38 7d 20 41 20 73 75 63 63 65 73 73 66 75  168} A successfu
f9e0: 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  l call to [sqlit
f9f0: 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49  e3_config]([SQLI
fa00: 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53  TE_CONFIG_LOOKAS
fa10: 49 44 45 5d 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20  IDE],Z,N).**    
fa20: 20 20 20 20 20 20 73 68 61 6c 6c 20 63 61 75 73        shall caus
fa30: 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 6c  e the default [l
fa40: 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20  ookaside memory 
fa50: 61 6c 6c 6f 63 61 74 6f 72 5d 20 63 6f 6e 66 69  allocator] confi
fa60: 67 75 72 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 20  guration.**     
fa70: 20 20 20 20 20 66 6f 72 20 6e 65 77 20 5b 64 61       for new [da
fa80: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
fa90: 6e 73 5d 20 74 6f 20 62 65 20 4e 20 73 6c 6f 74  ns] to be N slot
faa0: 73 20 6f 66 20 5a 20 62 79 74 65 73 20 65 61 63  s of Z bytes eac
fab0: 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  h..*/.SQLITE_API
fac0: 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45   SQLITE_EXPERIME
fad0: 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33  NTAL int sqlite3
fae0: 5f 63 6f 6e 66 69 67 28 69 6e 74 2c 20 2e 2e 2e  _config(int, ...
faf0: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
fb00: 45 46 3a 20 43 6f 6e 66 69 67 75 72 65 20 64 61  EF: Configure da
fb10: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
fb20: 6e 73 20 20 7b 48 31 34 32 30 30 7d 20 3c 53 32  ns  {H14200} <S2
fb30: 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d  0000>.** EXPERIM
fb40: 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ENTAL.**.** The 
fb50: 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69  sqlite3_db_confi
fb60: 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73  g() interface is
fb70: 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 63 6f   used to make co
fb80: 6e 66 69 67 75 72 61 74 69 6f 6e 0a 2a 2a 20 63  nfiguration.** c
fb90: 68 61 6e 67 65 73 20 74 6f 20 61 20 5b 64 61 74  hanges to a [dat
fba0: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
fbb0: 5d 2e 20 20 54 68 65 20 69 6e 74 65 72 66 61 63  ].  The interfac
fbc0: 65 20 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 0a  e is similar to.
fbd0: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66  ** [sqlite3_conf
fbe0: 69 67 28 29 5d 20 65 78 63 65 70 74 20 74 68 61  ig()] except tha
fbf0: 74 20 74 68 65 20 63 68 61 6e 67 65 73 20 61 70  t the changes ap
fc00: 70 6c 79 20 74 6f 20 61 20 73 69 6e 67 6c 65 0a  ply to a single.
fc10: 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  ** [database con
fc20: 6e 65 63 74 69 6f 6e 5d 20 28 73 70 65 63 69 66  nection] (specif
fc30: 69 65 64 20 69 6e 20 74 68 65 20 66 69 72 73 74  ied in the first
fc40: 20 61 72 67 75 6d 65 6e 74 29 2e 20 20 54 68 65   argument).  The
fc50: 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 64 62 5f 63  .** sqlite3_db_c
fc60: 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 63  onfig() interfac
fc70: 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73  e can only be us
fc80: 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61  ed immediately a
fc90: 66 74 65 72 0a 2a 2a 20 74 68 65 20 64 61 74 61  fter.** the data
fca0: 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
fcb0: 69 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  is created using
fcc0: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29   [sqlite3_open()
fcd0: 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6f  ],.** [sqlite3_o
fce0: 70 65 6e 31 36 28 29 5d 2c 20 6f 72 20 5b 73 71  pen16()], or [sq
fcf0: 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d  lite3_open_v2()]
fd00: 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65  .  .**.** The se
fd10: 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f  cond argument to
fd20: 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66   sqlite3_db_conf
fd30: 69 67 28 44 2c 56 2c 2e 2e 2e 29 20 20 69 73 20  ig(D,V,...)  is 
fd40: 74 68 65 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 61  the.** configura
fd50: 74 69 6f 6e 20 76 65 72 62 20 2d 20 61 6e 20 69  tion verb - an i
fd60: 6e 74 65 67 65 72 20 63 6f 64 65 20 74 68 61 74  nteger code that
fd70: 20 69 6e 64 69 63 61 74 65 73 20 77 68 61 74 0a   indicates what.
fd80: 2a 2a 20 61 73 70 65 63 74 20 6f 66 20 74 68 65  ** aspect of the
fd90: 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
fda0: 63 74 69 6f 6e 5d 20 69 73 20 62 65 69 6e 67 20  ction] is being 
fdb0: 63 6f 6e 66 69 67 75 72 65 64 2e 0a 2a 2a 20 54  configured..** T
fdc0: 68 65 20 6f 6e 6c 79 20 63 68 6f 69 63 65 20 66  he only choice f
fdd0: 6f 72 20 74 68 69 73 20 76 61 6c 75 65 20 69 73  or this value is
fde0: 20 5b 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49   [SQLITE_DBCONFI
fdf0: 47 5f 4c 4f 4f 4b 41 53 49 44 45 5d 2e 0a 2a 2a  G_LOOKASIDE]..**
fe00: 20 4e 65 77 20 76 65 72 62 73 20 61 72 65 20 6c   New verbs are l
fe10: 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 64 64 65  ikely to be adde
fe20: 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65  d in future rele
fe30: 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a  ases of SQLite..
fe40: 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 61 72  ** Additional ar
fe50: 67 75 6d 65 6e 74 73 20 64 65 70 65 6e 64 20 6f  guments depend o
fe60: 6e 20 74 68 65 20 76 65 72 62 2e 0a 2a 2a 0a 2a  n the verb..**.*
fe70: 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a  * INVARIANTS:.**
fe80: 0a 2a 2a 20 7b 48 31 34 32 30 33 7d 20 41 20 63  .** {H14203} A c
fe90: 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
fea0: 64 62 5f 63 6f 6e 66 69 67 28 44 2c 56 2c 2e 2e  db_config(D,V,..
feb0: 2e 29 5d 20 73 68 61 6c 6c 20 72 65 74 75 72 6e  .)] shall return
fec0: 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20   [SQLITE_OK].** 
fed0: 20 20 20 20 20 20 20 20 20 69 66 20 61 6e 64 20           if and 
fee0: 6f 6e 6c 79 20 69 66 20 74 68 65 20 63 61 6c 6c  only if the call
fef0: 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2e 0a   is successful..
ff00: 2a 2a 0a 2a 2a 20 7b 48 31 34 32 30 36 7d 20 49  **.** {H14206} I
ff10: 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 6c  f one or more sl
ff20: 6f 74 73 20 6f 66 20 74 68 65 20 5b 6c 6f 6f 6b  ots of the [look
ff30: 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  aside memory all
ff40: 6f 63 61 74 6f 72 5d 20 66 6f 72 0a 2a 2a 20 20  ocator] for.**  
ff50: 20 20 20 20 20 20 20 20 5b 64 61 74 61 62 61 73          [databas
ff60: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 20  e connection] D 
ff70: 61 72 65 20 69 6e 20 75 73 65 2c 20 74 68 65 6e  are in use, then
ff80: 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20   a call to.**   
ff90: 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
ffa0: 64 62 5f 63 6f 6e 66 69 67 5d 28 44 2c 5b 53 51  db_config](D,[SQ
ffb0: 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f  LITE_DBCONFIG_LO
ffc0: 4f 4b 41 53 49 44 45 5d 2c 2e 2e 2e 29 20 73 68  OKASIDE],...) sh
ffd0: 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  all.**          
ffe0: 66 61 69 6c 20 77 69 74 68 20 61 6e 20 5b 53 51  fail with an [SQ
fff0: 4c 49 54 45 5f 42 55 53 59 5d 20 72 65 74 75 72  LITE_BUSY] retur
10000 6e 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48  n code..**.** {H
10010 31 34 32 30 39 7d 20 41 20 73 75 63 63 65 73 73  14209} A success
10020 66 75 6c 20 63 61 6c 6c 20 74 6f 20 0a 2a 2a 20  ful call to .** 
10030 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
10040 33 5f 64 62 5f 63 6f 6e 66 69 67 5d 28 44 2c 5b  3_db_config](D,[
10050 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f  SQLITE_DBCONFIG_
10060 4c 4f 4f 4b 41 53 49 44 45 5d 2c 42 2c 5a 2c 4e  LOOKASIDE],B,Z,N
10070 29 20 77 68 65 72 65 0a 2a 2a 20 20 20 20 20 20  ) where.**      
10080 20 20 20 20 44 20 69 73 20 61 6e 20 6f 70 65 6e      D is an open
10090 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
100a0 63 74 69 6f 6e 5d 20 61 6e 64 20 5a 20 61 6e 64  ction] and Z and
100b0 20 4e 20 61 72 65 20 70 6f 73 69 74 69 76 65 0a   N are positive.
100c0 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e 74 65  **          inte
100d0 67 65 72 73 20 61 6e 64 20 42 20 69 73 20 61 6e  gers and B is an
100e0 20 61 6c 69 67 6e 65 64 20 62 75 66 66 65 72 20   aligned buffer 
100f0 61 74 20 6c 65 61 73 74 20 5a 2a 4e 20 62 79 74  at least Z*N byt
10100 65 73 20 69 6e 20 73 69 7a 65 0a 2a 2a 20 20 20  es in size.**   
10110 20 20 20 20 20 20 20 73 68 61 6c 6c 20 63 61 75         shall cau
10120 73 65 20 74 68 65 20 5b 6c 6f 6f 6b 61 73 69 64  se the [lookasid
10130 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
10140 6f 72 5d 20 66 6f 72 20 44 20 74 6f 20 75 73 65  or] for D to use
10150 20 62 75 66 66 65 72 20 42 20 0a 2a 2a 20 20 20   buffer B .**   
10160 20 20 20 20 20 20 20 77 69 74 68 20 4e 20 73 6c         with N sl
10170 6f 74 73 20 6f 66 20 5a 20 62 79 74 65 73 20 65  ots of Z bytes e
10180 61 63 68 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 32  ach..**.** {H142
10190 31 32 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c  12} A successful
101a0 20 63 61 6c 6c 20 74 6f 20 0a 2a 2a 20 20 20 20   call to .**    
101b0 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 64        [sqlite3_d
101c0 62 5f 63 6f 6e 66 69 67 5d 28 44 2c 5b 53 51 4c  b_config](D,[SQL
101d0 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f  ITE_DBCONFIG_LOO
101e0 4b 41 53 49 44 45 5d 2c 42 2c 5a 2c 4e 29 20 77  KASIDE],B,Z,N) w
101f0 68 65 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  here.**         
10200 20 44 20 69 73 20 61 6e 20 6f 70 65 6e 20 5b 64   D is an open [d
10210 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
10220 6f 6e 5d 20 61 6e 64 20 5a 20 61 6e 64 20 4e 20  on] and Z and N 
10230 61 72 65 20 70 6f 73 69 74 69 76 65 0a 2a 2a 20  are positive.** 
10240 20 20 20 20 20 20 20 20 20 69 6e 74 65 67 65 72           integer
10250 73 20 61 6e 64 20 42 20 69 73 20 4e 55 4c 4c 20  s and B is NULL 
10260 70 6f 69 6e 74 65 72 20 73 68 61 6c 6c 20 63 61  pointer shall ca
10270 75 73 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  use the.**      
10280 20 20 20 20 5b 6c 6f 6f 6b 61 73 69 64 65 20 6d      [lookaside m
10290 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d  emory allocator]
102a0 20 66 6f 72 20 44 20 74 6f 20 61 20 6f 62 74 61   for D to a obta
102b0 69 6e 20 5a 2a 4e 20 62 79 74 65 20 62 75 66 66  in Z*N byte buff
102c0 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66  er.**          f
102d0 72 6f 6d 20 74 68 65 20 70 72 69 6d 61 72 79 20  rom the primary 
102e0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
102f0 20 61 6e 64 20 75 73 65 20 74 68 61 74 20 62 75   and use that bu
10300 66 66 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  ffer.**         
10310 20 77 69 74 68 20 4e 20 6c 6f 6f 6b 61 73 69 64   with N lookasid
10320 65 20 73 6c 6f 74 73 20 6f 66 20 5a 20 62 79 74  e slots of Z byt
10330 65 73 20 65 61 63 68 2e 0a 2a 2a 0a 2a 2a 20 7b  es each..**.** {
10340 48 31 34 32 31 35 7d 20 41 20 73 75 63 63 65 73  H14215} A succes
10350 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 0a 2a 2a  sful call to .**
10360 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
10370 65 33 5f 64 62 5f 63 6f 6e 66 69 67 5d 28 44 2c  e3_db_config](D,
10380 5b 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47  [SQLITE_DBCONFIG
10390 5f 4c 4f 4f 4b 41 53 49 44 45 5d 2c 42 2c 5a 2c  _LOOKASIDE],B,Z,
103a0 4e 29 20 77 68 65 72 65 0a 2a 2a 20 20 20 20 20  N) where.**     
103b0 20 20 20 20 20 44 20 69 73 20 61 6e 20 6f 70 65       D is an ope
103c0 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  n [database conn
103d0 65 63 74 69 6f 6e 5d 20 61 6e 64 20 5a 20 61 6e  ection] and Z an
103e0 64 20 4e 20 61 72 65 20 7a 65 72 6f 20 73 68 61  d N are zero sha
103f0 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 64  ll.**          d
10400 69 73 61 62 6c 65 20 74 68 65 20 5b 6c 6f 6f 6b  isable the [look
10410 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  aside memory all
10420 6f 63 61 74 6f 72 5d 20 66 6f 72 20 44 2e 0a 2a  ocator] for D..*
10430 2a 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  *.**.*/.SQLITE_A
10440 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49  PI SQLITE_EXPERI
10450 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74  MENTAL int sqlit
10460 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c  e3_db_config(sql
10470 69 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 2e  ite3*, int op, .
10480 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  ..);../*.** CAPI
10490 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c  3REF: Memory All
104a0 6f 63 61 74 69 6f 6e 20 52 6f 75 74 69 6e 65 73  ocation Routines
104b0 20 7b 48 31 30 31 35 35 7d 20 3c 53 32 30 31 32   {H10155} <S2012
104c0 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  0>.** EXPERIMENT
104d0 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  AL.**.** An inst
104e0 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a  ance of this obj
104f0 65 63 74 20 64 65 66 69 6e 65 73 20 74 68 65 20  ect defines the 
10500 69 6e 74 65 72 66 61 63 65 20 62 65 74 77 65 65  interface betwee
10510 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 61 6e 64 20  n SQLite.** and 
10520 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79  low-level memory
10530 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74   allocation rout
10540 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ines..**.** This
10550 20 6f 62 6a 65 63 74 20 69 73 20 75 73 65 64 20   object is used 
10560 69 6e 20 6f 6e 6c 79 20 6f 6e 65 20 70 6c 61 63  in only one plac
10570 65 20 69 6e 20 74 68 65 20 53 51 4c 69 74 65 20  e in the SQLite 
10580 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20 41 20  interface..** A 
10590 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e  pointer to an in
105a0 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f  stance of this o
105b0 62 6a 65 63 74 20 69 73 20 74 68 65 20 61 72 67  bject is the arg
105c0 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 5b 73 71 6c  ument to.** [sql
105d0 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 77  ite3_config()] w
105e0 68 65 6e 20 74 68 65 20 63 6f 6e 66 69 67 75 72  hen the configur
105f0 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 0a  ation option is.
10600 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ** [SQLITE_CONFI
10610 47 5f 4d 41 4c 4c 4f 43 5d 2e 20 20 42 79 20 63  G_MALLOC].  By c
10620 72 65 61 74 69 6e 67 20 61 6e 20 69 6e 73 74 61  reating an insta
10630 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65  nce of this obje
10640 63 74 0a 2a 2a 20 61 6e 64 20 70 61 73 73 69 6e  ct.** and passin
10650 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33  g it to [sqlite3
10660 5f 63 6f 6e 66 69 67 28 29 5d 20 64 75 72 69 6e  _config()] durin
10670 67 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c  g configuration,
10680 20 61 6e 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69   an.** applicati
10690 6f 6e 20 63 61 6e 20 73 70 65 63 69 66 79 20 61  on can specify a
106a0 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6d 65  n alternative me
106b0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
106c0 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 66 6f 72  subsystem.** for
106d0 20 53 51 4c 69 74 65 20 74 6f 20 75 73 65 20 66   SQLite to use f
106e0 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 79  or all of its dy
106f0 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 6e 65 65  namic memory nee
10700 64 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74  ds..**.** Note t
10710 68 61 74 20 53 51 4c 69 74 65 20 63 6f 6d 65 73  hat SQLite comes
10720 20 77 69 74 68 20 61 20 62 75 69 6c 74 2d 69 6e   with a built-in
10730 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
10740 72 20 74 68 61 74 20 69 73 0a 2a 2a 20 70 65 72  r that is.** per
10750 66 65 63 74 6c 79 20 61 64 65 71 75 61 74 65 20  fectly adequate 
10760 66 6f 72 20 74 68 65 20 6f 76 65 72 77 68 65 6c  for the overwhel
10770 6d 69 6e 67 20 6d 61 6a 6f 72 69 74 79 20 6f 66  ming majority of
10780 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a   applications.**
10790 20 61 6e 64 20 74 68 61 74 20 74 68 69 73 20 6f   and that this o
107a0 62 6a 65 63 74 20 69 73 20 6f 6e 6c 79 20 75 73  bject is only us
107b0 65 66 75 6c 20 74 6f 20 61 20 74 69 6e 79 20 6d  eful to a tiny m
107c0 69 6e 6f 72 69 74 79 20 6f 66 20 61 70 70 6c 69  inority of appli
107d0 63 61 74 69 6f 6e 73 0a 2a 2a 20 77 69 74 68 20  cations.** with 
107e0 73 70 65 63 69 61 6c 69 7a 65 64 20 6d 65 6d 6f  specialized memo
107f0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65  ry allocation re
10800 71 75 69 72 65 6d 65 6e 74 73 2e 20 20 54 68 69  quirements.  Thi
10810 73 20 6f 62 6a 65 63 74 20 69 73 0a 2a 2a 20 61  s object is.** a
10820 6c 73 6f 20 75 73 65 64 20 64 75 72 69 6e 67 20  lso used during 
10830 74 65 73 74 69 6e 67 20 6f 66 20 53 51 4c 69 74  testing of SQLit
10840 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 70  e in order to sp
10850 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e 61  ecify an alterna
10860 74 69 76 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61  tive.** memory a
10870 6c 6c 6f 63 61 74 6f 72 20 74 68 61 74 20 73 69  llocator that si
10880 6d 75 6c 61 74 65 73 20 6d 65 6d 6f 72 79 20 6f  mulates memory o
10890 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f 6e  ut-of-memory con
108a0 64 69 74 69 6f 6e 73 20 69 6e 0a 2a 2a 20 6f 72  ditions in.** or
108b0 64 65 72 20 74 6f 20 76 65 72 69 66 79 20 74 68  der to verify th
108c0 61 74 20 53 51 4c 69 74 65 20 72 65 63 6f 76 65  at SQLite recove
108d0 72 73 20 67 72 61 63 65 66 75 6c 6c 79 20 66 72  rs gracefully fr
108e0 6f 6d 20 73 75 63 68 0a 2a 2a 20 63 6f 6e 64 69  om such.** condi
108f0 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tions..**.** The
10900 20 78 4d 61 6c 6c 6f 63 2c 20 78 46 72 65 65 2c   xMalloc, xFree,
10910 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 20 6d 65   and xRealloc me
10920 74 68 6f 64 73 20 6d 75 73 74 20 77 6f 72 6b 20  thods must work 
10930 6c 69 6b 65 20 74 68 65 0a 2a 2a 20 6d 61 6c 6c  like the.** mall
10940 6f 63 28 29 2c 20 66 72 65 65 28 29 2c 20 61 6e  oc(), free(), an
10950 64 20 72 65 61 6c 6c 6f 63 28 29 20 66 75 6e 63  d realloc() func
10960 74 69 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20 73  tions from the s
10970 74 61 6e 64 61 72 64 20 6c 69 62 72 61 72 79 2e  tandard library.
10980 0a 2a 2a 0a 2a 2a 20 78 53 69 7a 65 20 73 68 6f  .**.** xSize sho
10990 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 61  uld return the a
109a0 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66  llocated size of
109b0 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
109c0 74 69 6f 6e 0a 2a 2a 20 70 72 65 76 69 6f 75 73  tion.** previous
109d0 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ly obtained from
109e0 20 78 4d 61 6c 6c 6f 63 20 6f 72 20 78 52 65 61   xMalloc or xRea
109f0 6c 6c 6f 63 2e 20 20 54 68 65 20 61 6c 6c 6f 63  lloc.  The alloc
10a00 61 74 65 64 20 73 69 7a 65 0a 2a 2a 20 69 73 20  ated size.** is 
10a10 61 6c 77 61 79 73 20 61 74 20 6c 65 61 73 74 20  always at least 
10a20 61 73 20 62 69 67 20 61 73 20 74 68 65 20 72 65  as big as the re
10a30 71 75 65 73 74 65 64 20 73 69 7a 65 20 62 75 74  quested size but
10a40 20 6d 61 79 20 62 65 20 6c 61 72 67 65 72 2e 0a   may be larger..
10a50 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 6f 75 6e 64  **.** The xRound
10a60 75 70 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e  up method return
10a70 73 20 77 68 61 74 20 77 6f 75 6c 64 20 62 65 20  s what would be 
10a80 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69  the allocated si
10a90 7a 65 20 6f 66 0a 2a 2a 20 61 20 6d 65 6d 6f 72  ze of.** a memor
10aa0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 67 69 76  y allocation giv
10ab0 65 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  en a particular 
10ac0 72 65 71 75 65 73 74 65 64 20 73 69 7a 65 2e 20  requested size. 
10ad0 20 4d 6f 73 74 20 6d 65 6d 6f 72 79 0a 2a 2a 20   Most memory.** 
10ae0 61 6c 6c 6f 63 61 74 6f 72 73 20 72 6f 75 6e 64  allocators round
10af0 20 75 70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63   up memory alloc
10b00 61 74 69 6f 6e 73 20 61 74 20 6c 65 61 73 74 20  ations at least 
10b10 74 6f 20 74 68 65 20 6e 65 78 74 20 6d 75 6c 74  to the next mult
10b20 69 70 6c 65 0a 2a 2a 20 6f 66 20 38 2e 20 20 53  iple.** of 8.  S
10b30 6f 6d 65 20 61 6c 6c 6f 63 61 74 6f 72 73 20 72  ome allocators r
10b40 6f 75 6e 64 20 75 70 20 74 6f 20 61 20 6c 61 72  ound up to a lar
10b50 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 72 20  ger multiple or 
10b60 74 6f 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e  to a power of 2.
10b70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 6e 69 74  .**.** The xInit
10b80 20 6d 65 74 68 6f 64 20 69 6e 69 74 69 61 6c 69   method initiali
10b90 7a 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 61  zes the memory a
10ba0 6c 6c 6f 63 61 74 6f 72 2e 20 20 28 46 6f 72 20  llocator.  (For 
10bb0 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 69 74 20 6d  example,.** it m
10bc0 69 67 68 74 20 61 6c 6c 6f 63 61 74 65 20 61 6e  ight allocate an
10bd0 79 20 72 65 71 75 69 72 65 20 6d 75 74 65 78 65  y require mutexe
10be0 73 20 6f 72 20 69 6e 69 74 69 61 6c 69 7a 65 20  s or initialize 
10bf0 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 0a 2a 2a  internal data.**
10c00 20 73 74 72 75 63 74 75 72 65 73 2e 20 20 54 68   structures.  Th
10c10 65 20 78 53 68 75 74 64 6f 77 6e 20 6d 65 74 68  e xShutdown meth
10c20 6f 64 20 69 73 20 69 6e 76 6f 6b 65 64 20 28 69  od is invoked (i
10c30 6e 64 69 72 65 63 74 6c 79 29 20 62 79 0a 2a 2a  ndirectly) by.**
10c40 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f   [sqlite3_shutdo
10c50 77 6e 28 29 5d 20 61 6e 64 20 73 68 6f 75 6c 64  wn()] and should
10c60 20 64 65 61 6c 6c 6f 63 61 74 65 20 61 6e 79 20   deallocate any 
10c70 72 65 73 6f 75 72 63 65 73 20 61 63 71 75 69 72  resources acquir
10c80 65 64 0a 2a 2a 20 62 79 20 78 49 6e 69 74 2e 20  ed.** by xInit. 
10c90 20 54 68 65 20 70 41 70 70 44 61 74 61 20 70 6f   The pAppData po
10ca0 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 73  inter is used as
10cb0 20 74 68 65 20 6f 6e 6c 79 20 70 61 72 61 6d 65   the only parame
10cc0 74 65 72 20 74 6f 0a 2a 2a 20 78 49 6e 69 74 20  ter to.** xInit 
10cd0 61 6e 64 20 78 53 68 75 74 64 6f 77 6e 2e 0a 2a  and xShutdown..*
10ce0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
10cf0 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74   sqlite3_mem_met
10d00 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 6d 65 6d  hods sqlite3_mem
10d10 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74  _methods;.struct
10d20 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74   sqlite3_mem_met
10d30 68 6f 64 73 20 7b 0a 20 20 76 6f 69 64 20 2a 28  hods {.  void *(
10d40 2a 78 4d 61 6c 6c 6f 63 29 28 69 6e 74 29 3b 20  *xMalloc)(int); 
10d50 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72          /* Memor
10d60 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e  y allocation fun
10d70 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20  ction */.  void 
10d80 28 2a 78 46 72 65 65 29 28 76 6f 69 64 2a 29 3b  (*xFree)(void*);
10d90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65            /* Fre
10da0 65 20 61 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61  e a prior alloca
10db0 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  tion */.  void *
10dc0 28 2a 78 52 65 61 6c 6c 6f 63 29 28 76 6f 69 64  (*xRealloc)(void
10dd0 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 52 65 73 69  *,int);  /* Resi
10de0 7a 65 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e  ze an allocation
10df0 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 69 7a   */.  int (*xSiz
10e00 65 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20  e)(void*);      
10e10 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74       /* Return t
10e20 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 61 6c  he size of an al
10e30 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  location */.  in
10e40 74 20 28 2a 78 52 6f 75 6e 64 75 70 29 28 69 6e  t (*xRoundup)(in
10e50 74 29 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t);          /* 
10e60 52 6f 75 6e 64 20 75 70 20 72 65 71 75 65 73 74  Round up request
10e70 20 73 69 7a 65 20 74 6f 20 61 6c 6c 6f 63 61 74   size to allocat
10e80 69 6f 6e 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e  ion size */.  in
10e90 74 20 28 2a 78 49 6e 69 74 29 28 76 6f 69 64 2a  t (*xInit)(void*
10ea0 29 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  );           /* 
10eb0 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d  Initialize the m
10ec0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20  emory allocator 
10ed0 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 53 68 75  */.  void (*xShu
10ee0 74 64 6f 77 6e 29 28 76 6f 69 64 2a 29 3b 20 20  tdown)(void*);  
10ef0 20 20 20 20 2f 2a 20 44 65 69 6e 69 74 69 61 6c      /* Deinitial
10f00 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61  ize the memory a
10f10 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 76 6f  llocator */.  vo
10f20 69 64 20 2a 70 41 70 70 44 61 74 61 3b 20 20 20  id *pAppData;   
10f30 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10f40 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 49 6e 69  Argument to xIni
10f50 74 28 29 20 61 6e 64 20 78 53 68 75 74 64 6f 77  t() and xShutdow
10f60 6e 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  n() */.};../*.**
10f70 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69   CAPI3REF: Confi
10f80 67 75 72 61 74 69 6f 6e 20 4f 70 74 69 6f 6e 73  guration Options
10f90 20 7b 48 31 30 31 36 30 7d 20 3c 53 32 30 30 30   {H10160} <S2000
10fa0 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  0>.** EXPERIMENT
10fb0 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63  AL.**.** These c
10fc0 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 74 68 65  onstants are the
10fd0 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 74 65 67   available integ
10fe0 65 72 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  er configuration
10ff0 20 6f 70 74 69 6f 6e 73 20 74 68 61 74 0a 2a 2a   options that.**
11000 20 63 61 6e 20 62 65 20 70 61 73 73 65 64 20 61   can be passed a
11010 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  s the first argu
11020 6d 65 6e 74 20 74 6f 20 74 68 65 20 5b 73 71 6c  ment to the [sql
11030 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 69  ite3_config()] i
11040 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20  nterface..**.** 
11050 4e 65 77 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  New configuratio
11060 6e 20 6f 70 74 69 6f 6e 73 20 6d 61 79 20 62 65  n options may be
11070 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65   added in future
11080 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c   releases of SQL
11090 69 74 65 2e 0a 2a 2a 20 45 78 69 73 74 69 6e 67  ite..** Existing
110a0 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f   configuration o
110b0 70 74 69 6f 6e 73 20 6d 69 67 68 74 20 62 65 20  ptions might be 
110c0 64 69 73 63 6f 6e 74 69 6e 75 65 64 2e 20 20 41  discontinued.  A
110d0 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73  pplications.** s
110e0 68 6f 75 6c 64 20 63 68 65 63 6b 20 74 68 65 20  hould check the 
110f0 72 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d  return code from
11100 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67   [sqlite3_config
11110 28 29 5d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  ()] to make sure
11120 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 61 6c   that.** the cal
11130 6c 20 77 6f 72 6b 65 64 2e 20 20 54 68 65 20 5b  l worked.  The [
11140 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
11150 5d 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c  ] interface will
11160 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e   return a.** non
11170 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64  -zero [error cod
11180 65 5d 20 69 66 20 61 20 64 69 73 63 6f 6e 74 69  e] if a disconti
11190 6e 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f 72  nued or unsuppor
111a0 74 65 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  ted configuratio
111b0 6e 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 69  n option.** is i
111c0 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64  nvoked..**.** <d
111d0 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  l>.** <dt>SQLITE
111e0 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48  _CONFIG_SINGLETH
111f0 52 45 41 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  READ</dt>.** <dd
11200 3e 54 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72  >There are no ar
11210 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20  guments to this 
11220 6f 70 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70  option.  This op
11230 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 0a 2a 2a  tion disables.**
11240 20 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20 61 6e   all mutexing an
11250 64 20 70 75 74 73 20 53 51 4c 69 74 65 20 69 6e  d puts SQLite in
11260 74 6f 20 61 20 6d 6f 64 65 20 77 68 65 72 65 20  to a mode where 
11270 69 74 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75  it can only be u
11280 73 65 64 0a 2a 2a 20 62 79 20 61 20 73 69 6e 67  sed.** by a sing
11290 6c 65 20 74 68 72 65 61 64 2e 3c 2f 64 64 3e 0a  le thread.</dd>.
112a0 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
112b0 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52  _CONFIG_MULTITHR
112c0 45 41 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  EAD</dt>.** <dd>
112d0 54 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72 67  There are no arg
112e0 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 6f  uments to this o
112f0 70 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74  ption.  This opt
11300 69 6f 6e 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20  ion disables.** 
11310 6d 75 74 65 78 69 6e 67 20 6f 6e 20 5b 64 61 74  mutexing on [dat
11320 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
11330 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20  ] and [prepared 
11340 73 74 61 74 65 6d 65 6e 74 5d 20 6f 62 6a 65 63  statement] objec
11350 74 73 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69  ts..** The appli
11360 63 61 74 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e  cation is respon
11370 73 69 62 6c 65 20 66 6f 72 20 73 65 72 69 61 6c  sible for serial
11380 69 7a 69 6e 67 20 61 63 63 65 73 73 20 74 6f 0a  izing access to.
11390 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  ** [database con
113a0 6e 65 63 74 69 6f 6e 73 5d 20 61 6e 64 20 5b 70  nections] and [p
113b0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
113c0 74 73 5d 2e 20 20 42 75 74 20 6f 74 68 65 72 20  ts].  But other 
113d0 6d 75 74 65 78 65 73 0a 2a 2a 20 61 72 65 20 65  mutexes.** are e
113e0 6e 61 62 6c 65 64 20 73 6f 20 74 68 61 74 20 53  nabled so that S
113f0 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 73 61  QLite will be sa
11400 66 65 20 74 6f 20 75 73 65 20 69 6e 20 61 20 6d  fe to use in a m
11410 75 6c 74 69 2d 74 68 72 65 61 64 65 64 0a 2a 2a  ulti-threaded.**
11420 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 61 73 20   environment as 
11430 6c 6f 6e 67 20 61 73 20 6e 6f 20 74 77 6f 20 74  long as no two t
11440 68 72 65 61 64 73 20 61 74 74 65 6d 70 74 20 74  hreads attempt t
11450 6f 20 75 73 65 20 74 68 65 20 73 61 6d 65 0a 2a  o use the same.*
11460 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  * [database conn
11470 65 63 74 69 6f 6e 5d 20 61 74 20 74 68 65 20 73  ection] at the s
11480 61 6d 65 20 74 69 6d 65 2e 20 20 53 65 65 20 74  ame time.  See t
11490 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f  he [threading mo
114a0 64 65 5d 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 61  de].** documenta
114b0 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f  tion for additio
114c0 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
114d0 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
114e0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 45  SQLITE_CONFIG_SE
114f0 52 49 41 4c 49 5a 45 44 3c 2f 64 74 3e 0a 2a 2a  RIALIZED</dt>.**
11500 20 3c 64 64 3e 54 68 65 72 65 20 61 72 65 20 6e   <dd>There are n
11510 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74  o arguments to t
11520 68 69 73 20 6f 70 74 69 6f 6e 2e 20 20 54 68 69  his option.  Thi
11530 73 20 6f 70 74 69 6f 6e 20 65 6e 61 62 6c 65 73  s option enables
11540 0a 2a 2a 20 61 6c 6c 20 6d 75 74 65 78 65 73 20  .** all mutexes 
11550 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 72 65  including the re
11560 63 75 72 73 69 76 65 0a 2a 2a 20 6d 75 74 65 78  cursive.** mutex
11570 65 73 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 20  es on [database 
11580 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64 20  connection] and 
11590 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
115a0 65 6e 74 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a  ent] objects..**
115b0 20 49 6e 20 74 68 69 73 20 6d 6f 64 65 20 28 77   In this mode (w
115c0 68 69 63 68 20 69 73 20 74 68 65 20 64 65 66 61  hich is the defa
115d0 75 6c 74 20 77 68 65 6e 20 53 51 4c 69 74 65 20  ult when SQLite 
115e0 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  is compiled with
115f0 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 54 48 52 45  .** [SQLITE_THRE
11600 41 44 53 41 46 45 3d 31 5d 29 20 74 68 65 20 53  ADSAFE=1]) the S
11610 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 77 69  QLite library wi
11620 6c 6c 20 69 74 73 65 6c 66 20 73 65 72 69 61 6c  ll itself serial
11630 69 7a 65 20 61 63 63 65 73 73 0a 2a 2a 20 74 6f  ize access.** to
11640 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
11650 63 74 69 6f 6e 73 5d 20 61 6e 64 20 5b 70 72 65  ctions] and [pre
11660 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73  pared statements
11670 5d 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a 2a  ] so that the.**
11680 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 73 20   application is 
11690 66 72 65 65 20 74 6f 20 75 73 65 20 74 68 65 20  free to use the 
116a0 73 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63  same [database c
116b0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6f 72 20 74 68  onnection] or th
116c0 65 0a 2a 2a 20 73 61 6d 65 20 5b 70 72 65 70 61  e.** same [prepa
116d0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69  red statement] i
116e0 6e 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65  n different thre
116f0 61 64 73 20 61 74 20 74 68 65 20 73 61 6d 65 20  ads at the same 
11700 74 69 6d 65 2e 0a 2a 2a 20 53 65 65 20 74 68 65  time..** See the
11710 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65   [threading mode
11720 5d 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20  ] documentation 
11730 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  for additional i
11740 6e 66 6f 72 6d 61 74 69 6f 6e 2e 3c 2f 64 64 3e  nformation.</dd>
11750 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  .**.** <dt>SQLIT
11760 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 3c  E_CONFIG_MALLOC<
11770 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
11780 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20   option takes a 
11790 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20  single argument 
117a0 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74  which is a point
117b0 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74  er to an.** inst
117c0 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c  ance of the [sql
117d0 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
117e0 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68  ] structure.  Th
117f0 65 20 61 72 67 75 6d 65 6e 74 20 73 70 65 63 69  e argument speci
11800 66 69 65 73 0a 2a 2a 20 61 6c 74 65 72 6e 61 74  fies.** alternat
11810 69 76 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65  ive low-level me
11820 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
11830 72 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 75  routines to be u
11840 73 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 0a  sed in place of.
11850 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  ** the memory al
11860 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65  location routine
11870 73 20 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c  s built into SQL
11880 69 74 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ite.</dd>.**.** 
11890 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49  <dt>SQLITE_CONFI
118a0 47 5f 47 45 54 4d 41 4c 4c 4f 43 3c 2f 64 74 3e  G_GETMALLOC</dt>
118b0 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74  .** <dd>This opt
118c0 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67  ion takes a sing
118d0 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63  le argument whic
118e0 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  h is a pointer t
118f0 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65  o an.** instance
11900 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33   of the [sqlite3
11910 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 20 73 74  _mem_methods] st
11920 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 5b 73  ructure.  The [s
11930 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
11940 64 73 5d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  ds].** structure
11950 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20   is filled with 
11960 74 68 65 20 63 75 72 72 65 6e 74 6c 79 20 64 65  the currently de
11970 66 69 6e 65 64 20 6d 65 6d 6f 72 79 20 61 6c 6c  fined memory all
11980 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73  ocation routines
11990 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69 6f 6e  ..** This option
119a0 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
119b0 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 64 65 66  overload the def
119c0 61 75 6c 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  ault memory allo
119d0 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e  cation.** routin
119e0 65 73 20 77 69 74 68 20 61 20 77 72 61 70 70 65  es with a wrappe
119f0 72 20 74 68 61 74 20 73 69 6d 75 6c 61 74 69 6f  r that simulatio
11a00 6e 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ns memory alloca
11a10 74 69 6f 6e 20 66 61 69 6c 75 72 65 20 6f 72 0a  tion failure or.
11a20 2a 2a 20 74 72 61 63 6b 73 20 6d 65 6d 6f 72 79  ** tracks memory
11a30 20 75 73 61 67 65 2c 20 66 6f 72 20 65 78 61 6d   usage, for exam
11a40 70 6c 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ple.</dd>.**.** 
11a50 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49  <dt>SQLITE_CONFI
11a60 47 5f 4d 45 4d 53 54 41 54 55 53 3c 2f 64 74 3e  G_MEMSTATUS</dt>
11a70 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74  .** <dd>This opt
11a80 69 6f 6e 20 74 61 6b 65 73 20 73 69 6e 67 6c 65  ion takes single
11a90 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 74 79 70   argument of typ
11aa0 65 20 69 6e 74 2c 20 69 6e 74 65 72 70 72 65 74  e int, interpret
11ab0 65 64 20 61 73 20 61 20 0a 2a 2a 20 62 6f 6f 6c  ed as a .** bool
11ac0 65 61 6e 2c 20 77 68 69 63 68 20 65 6e 61 62 6c  ean, which enabl
11ad0 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20 74  es or disables t
11ae0 68 65 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66  he collection of
11af0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
11b00 6f 6e 20 0a 2a 2a 20 73 74 61 74 69 73 74 69 63  on .** statistic
11b10 73 2e 20 57 68 65 6e 20 64 69 73 61 62 6c 65 64  s. When disabled
11b20 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  , the following 
11b30 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65  SQLite interface
11b40 73 20 62 65 63 6f 6d 65 20 0a 2a 2a 20 6e 6f 6e  s become .** non
11b50 2d 6f 70 65 72 61 74 69 6f 6e 61 6c 3a 0a 2a 2a  -operational:.**
11b60 20 20 20 3c 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69     <ul>.**   <li
11b70 3e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72  > [sqlite3_memor
11b80 79 5f 75 73 65 64 28 29 5d 0a 2a 2a 20 20 20 3c  y_used()].**   <
11b90 6c 69 3e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d  li> [sqlite3_mem
11ba0 6f 72 79 5f 68 69 67 68 77 61 74 65 72 28 29 5d  ory_highwater()]
11bb0 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69  .**   <li> [sqli
11bc0 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69  te3_soft_heap_li
11bd0 6d 69 74 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e  mit()].**   <li>
11be0 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73   [sqlite3_status
11bf0 28 29 5d 0a 2a 2a 20 20 20 3c 2f 75 6c 3e 0a 2a  ()].**   </ul>.*
11c00 2a 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64  * </dd>.**.** <d
11c10 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  t>SQLITE_CONFIG_
11c20 53 43 52 41 54 43 48 3c 2f 64 74 3e 0a 2a 2a 20  SCRATCH</dt>.** 
11c30 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20  <dd>This option 
11c40 73 70 65 63 69 66 69 65 73 20 61 20 73 74 61 74  specifies a stat
11c50 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72  ic memory buffer
11c60 20 74 68 61 74 20 53 51 4c 69 74 65 20 63 61 6e   that SQLite can
11c70 20 75 73 65 20 66 6f 72 0a 2a 2a 20 73 63 72 61   use for.** scra
11c80 74 63 68 20 6d 65 6d 6f 72 79 2e 20 20 54 68 65  tch memory.  The
11c90 72 65 20 61 72 65 20 74 68 72 65 65 20 61 72 67  re are three arg
11ca0 75 6d 65 6e 74 73 3a 20 20 41 20 70 6f 69 6e 74  uments:  A point
11cb0 65 72 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79  er to the memory
11cc0 2c 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66  , the.** size of
11cd0 20 65 61 63 68 20 73 63 72 61 74 63 68 20 62 75   each scratch bu
11ce0 66 66 65 72 20 28 73 7a 29 2c 20 61 6e 64 20 74  ffer (sz), and t
11cf0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 75 66  he number of buf
11d00 66 65 72 73 20 28 4e 29 2e 20 20 54 68 65 20 73  fers (N).  The s
11d10 7a 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6d 75  z.** argument mu
11d20 73 74 20 62 65 20 61 20 6d 75 6c 74 69 70 6c 65  st be a multiple
11d30 20 6f 66 20 31 36 2e 20 54 68 65 20 73 7a 20 70   of 16. The sz p
11d40 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20  arameter should 
11d50 62 65 20 61 20 66 65 77 20 62 79 74 65 73 0a 2a  be a few bytes.*
11d60 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68  * larger than th
11d70 65 20 61 63 74 75 61 6c 20 73 63 72 61 74 63 68  e actual scratch
11d80 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20   space required 
11d90 64 75 65 20 69 6e 74 65 72 6e 61 6c 20 6f 76 65  due internal ove
11da0 72 68 65 61 64 2e 0a 2a 2a 20 54 68 65 20 66 69  rhead..** The fi
11db0 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20  rst.** argument 
11dc0 73 68 6f 75 6c 64 20 70 6f 69 6e 74 20 74 6f 20  should point to 
11dd0 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66  an allocation of
11de0 20 61 74 20 6c 65 61 73 74 20 73 7a 2a 4e 20 62   at least sz*N b
11df0 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a  ytes of memory..
11e00 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75  ** SQLite will u
11e10 73 65 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20  se no more than 
11e20 6f 6e 65 20 73 63 72 61 74 63 68 20 62 75 66 66  one scratch buff
11e30 65 72 20 61 74 20 6f 6e 63 65 20 70 65 72 20 74  er at once per t
11e40 68 72 65 61 64 2c 20 73 6f 0a 2a 2a 20 4e 20 73  hread, so.** N s
11e50 68 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20  hould be set to 
11e60 74 68 65 20 65 78 70 65 63 74 65 64 20 6d 61 78  the expected max
11e70 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74  imum number of t
11e80 68 72 65 61 64 73 2e 20 20 54 68 65 20 73 7a 0a  hreads.  The sz.
11e90 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 73 68 6f  ** parameter sho
11ea0 75 6c 64 20 62 65 20 36 20 74 69 6d 65 73 20 74  uld be 6 times t
11eb0 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c  he size of the l
11ec0 61 72 67 65 73 74 20 64 61 74 61 62 61 73 65 20  argest database 
11ed0 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 53 63  page size..** Sc
11ee0 72 61 74 63 68 20 62 75 66 66 65 72 73 20 61 72  ratch buffers ar
11ef0 65 20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f  e used as part o
11f00 66 20 74 68 65 20 62 74 72 65 65 20 62 61 6c 61  f the btree bala
11f10 6e 63 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20  nce operation.  
11f20 49 66 0a 2a 2a 20 54 68 65 20 62 74 72 65 65 20  If.** The btree 
11f30 62 61 6c 61 6e 63 65 72 20 6e 65 65 64 73 20 61  balancer needs a
11f40 64 64 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79  dditional memory
11f50 20 62 65 79 6f 6e 64 20 77 68 61 74 20 69 73 20   beyond what is 
11f60 70 72 6f 76 69 64 65 64 20 62 79 0a 2a 2a 20 73  provided by.** s
11f70 63 72 61 74 63 68 20 62 75 66 66 65 72 73 20 6f  cratch buffers o
11f80 72 20 69 66 20 6e 6f 20 73 63 72 61 74 63 68 20  r if no scratch 
11f90 62 75 66 66 65 72 20 73 70 61 63 65 20 69 73 20  buffer space is 
11fa0 73 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e 20  specified, then 
11fb0 53 51 4c 69 74 65 0a 2a 2a 20 67 6f 65 73 20 74  SQLite.** goes t
11fc0 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  o [sqlite3_mallo
11fd0 63 28 29 5d 20 74 6f 20 6f 62 74 61 69 6e 20 74  c()] to obtain t
11fe0 68 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65  he memory it nee
11ff0 64 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  ds.</dd>.**.** <
12000 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  dt>SQLITE_CONFIG
12010 5f 50 41 47 45 43 41 43 48 45 3c 2f 64 74 3e 0a  _PAGECACHE</dt>.
12020 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69  ** <dd>This opti
12030 6f 6e 20 73 70 65 63 69 66 69 65 73 20 61 20 73  on specifies a s
12040 74 61 74 69 63 20 6d 65 6d 6f 72 79 20 62 75 66  tatic memory buf
12050 66 65 72 20 74 68 61 74 20 53 51 4c 69 74 65 20  fer that SQLite 
12060 63 61 6e 20 75 73 65 20 66 6f 72 0a 2a 2a 20 74  can use for.** t
12070 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65  he database page
12080 20 63 61 63 68 65 2e 20 20 54 68 65 72 65 20 61   cache.  There a
12090 72 65 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e  re three argumen
120a0 74 73 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  ts: A pointer to
120b0 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20   the.** memory, 
120c0 74 68 65 20 73 69 7a 65 20 6f 66 20 65 61 63 68  the size of each
120d0 20 70 61 67 65 20 62 75 66 66 65 72 20 28 73 7a   page buffer (sz
120e0 29 2c 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65  ), and the numbe
120f0 72 20 6f 66 20 70 61 67 65 73 20 28 4e 29 2e 0a  r of pages (N)..
12100 2a 2a 20 54 68 65 20 73 7a 20 61 72 67 75 6d 65  ** The sz argume
12110 6e 74 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77  nt must be a pow
12120 65 72 20 6f 66 20 74 77 6f 20 62 65 74 77 65 65  er of two betwee
12130 6e 20 35 31 32 20 61 6e 64 20 33 32 37 36 38 2e  n 512 and 32768.
12140 20 20 54 68 65 20 66 69 72 73 74 0a 2a 2a 20 61    The first.** a
12150 72 67 75 6d 65 6e 74 20 73 68 6f 75 6c 64 20 70  rgument should p
12160 6f 69 6e 74 20 74 6f 20 61 6e 20 61 6c 6c 6f 63  oint to an alloc
12170 61 74 69 6f 6e 20 6f 66 20 61 74 20 6c 65 61 73  ation of at leas
12180 74 20 73 7a 2a 4e 20 62 79 74 65 73 20 6f 66 20  t sz*N bytes of 
12190 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 53 51 4c 69 74  memory..** SQLit
121a0 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20 6d  e will use the m
121b0 65 6d 6f 72 79 20 70 72 6f 76 69 64 65 64 20 62  emory provided b
121c0 79 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  y the first argu
121d0 6d 65 6e 74 20 74 6f 20 73 61 74 69 73 66 79 20  ment to satisfy 
121e0 69 74 73 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6e 65  its.** memory ne
121f0 65 64 73 20 66 6f 72 20 74 68 65 20 66 69 72 73  eds for the firs
12200 74 20 4e 20 70 61 67 65 73 20 74 68 61 74 20 69  t N pages that i
12210 74 20 61 64 64 73 20 74 6f 20 63 61 63 68 65 2e  t adds to cache.
12220 20 20 49 66 20 61 64 64 69 74 69 6f 6e 61 6c 0a    If additional.
12230 2a 2a 20 70 61 67 65 20 63 61 63 68 65 20 6d 65  ** page cache me
12240 6d 6f 72 79 20 69 73 20 6e 65 65 64 65 64 20 62  mory is needed b
12250 65 79 6f 6e 64 20 77 68 61 74 20 69 73 20 70 72  eyond what is pr
12260 6f 76 69 64 65 64 20 62 79 20 74 68 69 73 20 6f  ovided by this o
12270 70 74 69 6f 6e 2c 20 74 68 65 6e 0a 2a 2a 20 53  ption, then.** S
12280 51 4c 69 74 65 20 67 6f 65 73 20 74 6f 20 5b 73  QLite goes to [s
12290 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
122a0 20 66 6f 72 20 74 68 65 20 61 64 64 69 74 69 6f   for the additio
122b0 6e 61 6c 20 73 74 6f 72 61 67 65 20 73 70 61 63  nal storage spac
122c0 65 2e 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d  e..** The implem
122d0 65 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74 20 75  entation might u
122e0 73 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f  se one or more o
122f0 66 20 74 68 65 20 4e 20 62 75 66 66 65 72 73 20  f the N buffers 
12300 74 6f 20 68 6f 6c 64 20 0a 2a 2a 20 6d 65 6d 6f  to hold .** memo
12310 72 79 20 61 63 63 6f 75 6e 74 69 6e 67 20 69 6e  ry accounting in
12320 66 6f 72 6d 61 74 69 6f 6e 2e 20 3c 2f 64 64 3e  formation. </dd>
12330 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  .**.** <dt>SQLIT
12340 45 5f 43 4f 4e 46 49 47 5f 48 45 41 50 3c 2f 64  E_CONFIG_HEAP</d
12350 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f  t>.** <dd>This o
12360 70 74 69 6f 6e 20 73 70 65 63 69 66 69 65 73 20  ption specifies 
12370 61 20 73 74 61 74 69 63 20 6d 65 6d 6f 72 79 20  a static memory 
12380 62 75 66 66 65 72 20 74 68 61 74 20 53 51 4c 69  buffer that SQLi
12390 74 65 20 77 69 6c 6c 20 75 73 65 0a 2a 2a 20 66  te will use.** f
123a0 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 79  or all of its dy
123b0 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 61 6c 6c  namic memory all
123c0 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 20 62 65  ocation needs be
123d0 79 6f 6e 64 20 74 68 6f 73 65 20 70 72 6f 76 69  yond those provi
123e0 64 65 64 0a 2a 2a 20 66 6f 72 20 62 79 20 5b 53  ded.** for by [S
123f0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52  QLITE_CONFIG_SCR
12400 41 54 43 48 5d 20 61 6e 64 20 5b 53 51 4c 49 54  ATCH] and [SQLIT
12410 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43  E_CONFIG_PAGECAC
12420 48 45 5d 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72  HE]..** There ar
12430 65 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74  e three argument
12440 73 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  s: A pointer to 
12450 74 68 65 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20  the memory, the 
12460 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 79 74  number of.** byt
12470 65 73 20 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79  es in the memory
12480 20 62 75 66 66 65 72 2c 20 61 6e 64 20 74 68 65   buffer, and the
12490 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61 74   minimum allocat
124a0 69 6f 6e 20 73 69 7a 65 2e 20 20 49 66 0a 2a 2a  ion size.  If.**
124b0 20 74 68 65 20 66 69 72 73 74 20 70 6f 69 6e 74   the first point
124c0 65 72 20 28 74 68 65 20 6d 65 6d 6f 72 79 20 70  er (the memory p
124d0 6f 69 6e 74 65 72 29 20 69 73 20 4e 55 4c 4c 2c  ointer) is NULL,
124e0 20 74 68 65 6e 20 53 51 4c 69 74 65 20 72 65 76   then SQLite rev
124f0 65 72 74 73 0a 2a 2a 20 74 6f 20 75 73 69 6e 67  erts.** to using
12500 20 69 74 73 20 64 65 66 61 75 6c 74 20 6d 65 6d   its default mem
12510 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 28 74  ory allocator (t
12520 68 65 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63  he system malloc
12530 28 29 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  () implementatio
12540 6e 29 2c 0a 2a 2a 20 75 6e 64 6f 69 6e 67 20 61  n),.** undoing a
12550 6e 79 20 70 72 69 6f 72 20 69 6e 76 6f 63 61 74  ny prior invocat
12560 69 6f 6e 20 6f 66 20 5b 53 51 4c 49 54 45 5f 43  ion of [SQLITE_C
12570 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 2e 20 20  ONFIG_MALLOC].  
12580 49 66 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79  If the.** memory
12590 20 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74 20   pointer is not 
125a0 4e 55 4c 4c 20 61 6e 64 20 65 69 74 68 65 72 20  NULL and either 
125b0 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d  [SQLITE_ENABLE_M
125c0 45 4d 53 59 53 33 5d 20 6f 72 0a 2a 2a 20 5b 53  EMSYS3] or.** [S
125d0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
125e0 53 59 53 35 5d 20 61 72 65 20 64 65 66 69 6e 65  SYS5] are define
125f0 64 2c 20 74 68 65 6e 20 74 68 65 20 61 6c 74 65  d, then the alte
12600 72 6e 61 74 69 76 65 20 6d 65 6d 6f 72 79 0a 2a  rnative memory.*
12610 2a 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 65  * allocator is e
12620 6e 67 61 67 65 64 20 74 6f 20 68 61 6e 64 6c 65  ngaged to handle
12630 20 61 6c 6c 20 6f 66 20 53 51 4c 69 74 65 73 20   all of SQLites 
12640 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
12650 6e 20 6e 65 65 64 73 2e 3c 2f 64 64 3e 0a 2a 2a  n needs.</dd>.**
12660 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43  .** <dt>SQLITE_C
12670 4f 4e 46 49 47 5f 4d 55 54 45 58 3c 2f 64 74 3e  ONFIG_MUTEX</dt>
12680 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74  .** <dd>This opt
12690 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67  ion takes a sing
126a0 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63  le argument whic
126b0 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  h is a pointer t
126c0 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65  o an.** instance
126d0 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33   of the [sqlite3
126e0 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20  _mutex_methods] 
126f0 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20  structure.  The 
12700 61 72 67 75 6d 65 6e 74 20 73 70 65 63 69 66 69  argument specifi
12710 65 73 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 69 76  es.** alternativ
12720 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 75 74 65  e low-level mute
12730 78 20 72 6f 75 74 69 6e 65 73 20 74 6f 20 62 65  x routines to be
12740 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 0a 2a   used in place.*
12750 2a 20 74 68 65 20 6d 75 74 65 78 20 72 6f 75 74  * the mutex rout
12760 69 6e 65 73 20 62 75 69 6c 74 20 69 6e 74 6f 20  ines built into 
12770 53 51 4c 69 74 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a  SQLite.</dd>.**.
12780 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f  ** <dt>SQLITE_CO
12790 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 3c 2f 64  NFIG_GETMUTEX</d
127a0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f  t>.** <dd>This o
127b0 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69  ption takes a si
127c0 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68  ngle argument wh
127d0 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ich is a pointer
127e0 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e   to an.** instan
127f0 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74  ce of the [sqlit
12800 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
12810 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68  ] structure.  Th
12820 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 75  e.** [sqlite3_mu
12830 74 65 78 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20  tex_methods].** 
12840 73 74 72 75 63 74 75 72 65 20 69 73 20 66 69 6c  structure is fil
12850 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 75 72  led with the cur
12860 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 6d  rently defined m
12870 75 74 65 78 20 72 6f 75 74 69 6e 65 73 2e 0a 2a  utex routines..*
12880 2a 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 63 61  * This option ca
12890 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6f 76 65  n be used to ove
128a0 72 6c 6f 61 64 20 74 68 65 20 64 65 66 61 75 6c  rload the defaul
128b0 74 20 6d 75 74 65 78 20 61 6c 6c 6f 63 61 74 69  t mutex allocati
128c0 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77  on.** routines w
128d0 69 74 68 20 61 20 77 72 61 70 70 65 72 20 75 73  ith a wrapper us
128e0 65 64 20 74 6f 20 74 72 61 63 6b 20 6d 75 74 65  ed to track mute
128f0 78 20 75 73 61 67 65 20 66 6f 72 20 70 65 72 66  x usage for perf
12900 6f 72 6d 61 6e 63 65 0a 2a 2a 20 70 72 6f 66 69  ormance.** profi
12910 6c 69 6e 67 20 6f 72 20 74 65 73 74 69 6e 67 2c  ling or testing,
12920 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e 3c 2f 64   for example.</d
12930 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c  d>.**.** <dt>SQL
12940 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41  ITE_CONFIG_LOOKA
12950 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  SIDE</dt>.** <dd
12960 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b  >This option tak
12970 65 73 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73  es two arguments
12980 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 20   that determine 
12990 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 6d  the default.** m
129a0 65 6d 6f 72 79 20 61 6c 6c 63 61 74 69 6f 6e 20  emory allcation 
129b0 6c 6f 6f 6b 61 73 69 64 65 20 6f 70 74 69 6d 69  lookaside optimi
129c0 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20 66 69 72  zation.  The fir
129d0 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  st argument is t
129e0 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 65 61  he.** size of ea
129f0 63 68 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66  ch lookaside buf
12a00 66 65 72 20 73 6c 6f 74 20 61 6e 64 20 74 68 65  fer slot and the
12a10 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65 20 6e   second is the n
12a20 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f 74  umber of.** slot
12a30 73 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 65  s allocated to e
12a40 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ach database con
12a50 6e 65 63 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a  nection.</dd>.**
12a60 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65  .** </dl>.*/.#de
12a70 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  fine SQLITE_CONF
12a80 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 20  IG_SINGLETHREAD 
12a90 20 31 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 64   1  /* nil */.#d
12aa0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e  efine SQLITE_CON
12ab0 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 20  FIG_MULTITHREAD 
12ac0 20 20 32 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23    2  /* nil */.#
12ad0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
12ae0 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44 20  NFIG_SERIALIZED 
12af0 20 20 20 33 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a     3  /* nil */.
12b00 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
12b10 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 20 20 20 20  ONFIG_MALLOC    
12b20 20 20 20 20 34 20 20 2f 2a 20 73 71 6c 69 74 65      4  /* sqlite
12b30 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 20 2a  3_mem_methods* *
12b40 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
12b50 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c 4f  _CONFIG_GETMALLO
12b60 43 20 20 20 20 20 35 20 20 2f 2a 20 73 71 6c 69  C     5  /* sqli
12b70 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a  te3_mem_methods*
12b80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
12b90 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43  TE_CONFIG_SCRATC
12ba0 48 20 20 20 20 20 20 20 36 20 20 2f 2a 20 76 6f  H       6  /* vo
12bb0 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74  id*, int sz, int
12bc0 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51   N */.#define SQ
12bd0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45  LITE_CONFIG_PAGE
12be0 43 41 43 48 45 20 20 20 20 20 37 20 20 2f 2a 20  CACHE     7  /* 
12bf0 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69  void*, int sz, i
12c00 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  nt N */.#define 
12c10 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48 45  SQLITE_CONFIG_HE
12c20 41 50 20 20 20 20 20 20 20 20 20 20 38 20 20 2f  AP          8  /
12c30 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e 42 79  * void*, int nBy
12c40 74 65 2c 20 69 6e 74 20 6d 69 6e 20 2a 2f 0a 23  te, int min */.#
12c50 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
12c60 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 20 20  NFIG_MEMSTATUS  
12c70 20 20 20 39 20 20 2f 2a 20 62 6f 6f 6c 65 61 6e     9  /* boolean
12c80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
12c90 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 20  TE_CONFIG_MUTEX 
12ca0 20 20 20 20 20 20 20 31 30 20 20 2f 2a 20 73 71         10  /* sq
12cb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
12cc0 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ods* */.#define 
12cd0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45  SQLITE_CONFIG_GE
12ce0 54 4d 55 54 45 58 20 20 20 20 20 31 31 20 20 2f  TMUTEX     11  /
12cf0 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  * sqlite3_mutex_
12d00 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66  methods* */.#def
12d10 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ine SQLITE_CONFI
12d20 47 5f 43 48 55 4e 4b 41 4c 4c 4f 43 20 20 20 31  G_CHUNKALLOC   1
12d30 32 20 20 2f 2a 20 69 6e 74 20 74 68 72 65 73 68  2  /* int thresh
12d40 6f 6c 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  old */.#define S
12d50 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f  QLITE_CONFIG_LOO
12d60 4b 41 53 49 44 45 20 20 20 20 31 33 20 20 2f 2a  KASIDE    13  /*
12d70 20 69 6e 74 20 69 6e 74 20 2a 2f 0a 0a 2f 2a 0a   int int */../*.
12d80 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e  ** CAPI3REF: Con
12d90 66 69 67 75 72 61 74 69 6f 6e 20 4f 70 74 69 6f  figuration Optio
12da0 6e 73 20 7b 48 31 30 31 37 30 7d 20 3c 53 32 30  ns {H10170} <S20
12db0 30 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45  000>.** EXPERIME
12dc0 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  NTAL.**.** These
12dd0 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 74   constants are t
12de0 68 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 74  he available int
12df0 65 67 65 72 20 63 6f 6e 66 69 67 75 72 61 74 69  eger configurati
12e00 6f 6e 20 6f 70 74 69 6f 6e 73 20 74 68 61 74 0a  on options that.
12e10 2a 2a 20 63 61 6e 20 62 65 20 70 61 73 73 65 64  ** can be passed
12e20 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61   as the second a
12e30 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 5b  rgument to the [
12e40 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69  sqlite3_db_confi
12e50 67 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a  g()] interface..
12e60 2a 2a 0a 2a 2a 20 4e 65 77 20 63 6f 6e 66 69 67  **.** New config
12e70 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20  uration options 
12e80 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e 20  may be added in 
12e90 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20  future releases 
12ea0 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 45 78  of SQLite..** Ex
12eb0 69 73 74 69 6e 67 20 63 6f 6e 66 69 67 75 72 61  isting configura
12ec0 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 6d 69 67  tion options mig
12ed0 68 74 20 62 65 20 64 69 73 63 6f 6e 74 69 6e 75  ht be discontinu
12ee0 65 64 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e  ed.  Application
12ef0 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 68 65 63  s.** should chec
12f00 6b 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64  k the return cod
12f10 65 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f  e from [sqlite3_
12f20 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 74 6f 20  db_config()] to 
12f30 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a 2a  make sure that.*
12f40 2a 20 74 68 65 20 63 61 6c 6c 20 77 6f 72 6b 65  * the call worke
12f50 64 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 33  d.  The [sqlite3
12f60 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e  _db_config()] in
12f70 74 65 72 66 61 63 65 20 77 69 6c 6c 20 72 65 74  terface will ret
12f80 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72  urn a.** non-zer
12f90 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69  o [error code] i
12fa0 66 20 61 20 64 69 73 63 6f 6e 74 69 6e 75 65 64  f a discontinued
12fb0 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20   or unsupported 
12fc0 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70  configuration op
12fd0 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b  tion.** is invok
12fe0 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a  ed..**.** <dl>.*
12ff0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 44 42 43  * <dt>SQLITE_DBC
13000 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 3c  ONFIG_LOOKASIDE<
13010 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
13020 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 74 68   option takes th
13030 72 65 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 61  ree additional a
13040 72 67 75 6d 65 6e 74 73 20 74 68 61 74 20 64 65  rguments that de
13050 74 65 72 6d 69 6e 65 20 74 68 65 20 0a 2a 2a 20  termine the .** 
13060 5b 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72  [lookaside memor
13070 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 63 6f 6e  y allocator] con
13080 66 69 67 75 72 61 74 69 6f 6e 20 66 6f 72 20 74  figuration for t
13090 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
130a0 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 20 54 68 65  nection]..** The
130b0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
130c0 28 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d  (the third param
130d0 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33  eter to [sqlite3
130e0 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 69 73  _db_config()] is
130f0 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f   a.** pointer to
13100 20 61 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72   a memory buffer
13110 20 74 6f 20 75 73 65 20 66 6f 72 20 6c 6f 6f 6b   to use for look
13120 61 73 69 64 65 20 6d 65 6d 6f 72 79 2e 20 20 54  aside memory.  T
13130 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75  he first.** argu
13140 6d 65 6e 74 20 6d 61 79 20 62 65 20 4e 55 4c 4c  ment may be NULL
13150 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53   in which case S
13160 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 6c 6f 63  QLite will alloc
13170 61 74 65 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64  ate the lookasid
13180 65 0a 2a 2a 20 62 75 66 66 65 72 20 69 74 73 65  e.** buffer itse
13190 6c 66 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65  lf using [sqlite
131a0 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 54 68  3_malloc()].  Th
131b0 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
131c0 74 20 69 73 20 74 68 65 0a 2a 2a 20 73 69 7a 65  t is the.** size
131d0 20 6f 66 20 65 61 63 68 20 6c 6f 6f 6b 61 73 69   of each lookasi
131e0 64 65 20 62 75 66 66 65 72 20 73 6c 6f 74 20 61  de buffer slot a
131f0 6e 64 20 74 68 65 20 74 68 69 72 64 20 61 72 67  nd the third arg
13200 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 75 6d  ument is the num
13210 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f 74 73 2e  ber of.** slots.
13220 20 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68    The size of th
13230 65 20 62 75 66 66 65 72 20 69 6e 20 74 68 65 20  e buffer in the 
13240 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 6d  first argument m
13250 75 73 74 20 62 65 20 67 72 65 61 74 65 72 20 74  ust be greater t
13260 68 61 6e 0a 2a 2a 20 6f 72 20 65 71 75 61 6c 20  han.** or equal 
13270 74 6f 20 74 68 65 20 70 72 6f 64 75 63 74 20 6f  to the product o
13280 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 6e 64  f the second and
13290 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 73   third arguments
132a0 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f 64  .</dd>.**.** </d
132b0 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  l>.*/.#define SQ
132c0 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f  LITE_DBCONFIG_LO
132d0 4f 4b 41 53 49 44 45 20 20 20 20 31 30 30 31 20  OKASIDE    1001 
132e0 20 2f 2a 20 76 6f 69 64 2a 20 69 6e 74 20 69 6e   /* void* int in
132f0 74 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  t */.../*.** CAP
13300 49 33 52 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72  I3REF: Enable Or
13310 20 44 69 73 61 62 6c 65 20 45 78 74 65 6e 64 65   Disable Extende
13320 64 20 52 65 73 75 6c 74 20 43 6f 64 65 73 20 7b  d Result Codes {
13330 48 31 32 32 30 30 7d 20 3c 53 31 30 37 30 30 3e  H12200} <S10700>
13340 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
13350 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75  e3_extended_resu
13360 6c 74 5f 63 6f 64 65 73 28 29 20 72 6f 75 74 69  lt_codes() routi
13370 6e 65 20 65 6e 61 62 6c 65 73 20 6f 72 20 64 69  ne enables or di
13380 73 61 62 6c 65 73 20 74 68 65 0a 2a 2a 20 5b 65  sables the.** [e
13390 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63  xtended result c
133a0 6f 64 65 73 5d 20 66 65 61 74 75 72 65 20 6f 66  odes] feature of
133b0 20 53 51 4c 69 74 65 2e 20 54 68 65 20 65 78 74   SQLite. The ext
133c0 65 6e 64 65 64 20 72 65 73 75 6c 74 0a 2a 2a 20  ended result.** 
133d0 63 6f 64 65 73 20 61 72 65 20 64 69 73 61 62 6c  codes are disabl
133e0 65 64 20 62 79 20 64 65 66 61 75 6c 74 20 66 6f  ed by default fo
133f0 72 20 68 69 73 74 6f 72 69 63 61 6c 20 63 6f 6d  r historical com
13400 70 61 74 69 62 69 6c 69 74 79 20 63 6f 6e 73 69  patibility consi
13410 64 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  derations..**.**
13420 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a   INVARIANTS:.**.
13430 2a 2a 20 7b 48 31 32 32 30 31 7d 20 45 61 63 68  ** {H12201} Each
13440 20 6e 65 77 20 5b 64 61 74 61 62 61 73 65 20 63   new [database c
13450 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 68 61 6c 6c  onnection] shall
13460 20 68 61 76 65 20 74 68 65 0a 2a 2a 20 20 20 20   have the.**    
13470 20 20 20 20 20 20 5b 65 78 74 65 6e 64 65 64 20        [extended 
13480 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 66 65  result codes] fe
13490 61 74 75 72 65 20 64 69 73 61 62 6c 65 64 20 62  ature disabled b
134a0 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 0a 2a 2a  y default..**.**
134b0 20 7b 48 31 32 32 30 32 7d 20 54 68 65 20 5b 73   {H12202} The [s
134c0 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f  qlite3_extended_
134d0 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 44 2c 46  result_codes(D,F
134e0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 73 68 61  )] interface sha
134f0 6c 6c 20 65 6e 61 62 6c 65 0a 2a 2a 20 20 20 20  ll enable.**    
13500 20 20 20 20 20 20 5b 65 78 74 65 6e 64 65 64 20        [extended 
13510 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 66 6f  result codes] fo
13520 72 20 74 68 65 20 20 5b 64 61 74 61 62 61 73 65  r the  [database
13530 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 0a 2a   connection] D.*
13540 2a 20 20 20 20 20 20 20 20 20 20 69 66 20 74 68  *          if th
13550 65 20 46 20 70 61 72 61 6d 65 74 65 72 20 69 73  e F parameter is
13560 20 74 72 75 65 2c 20 6f 72 20 64 69 73 61 62 6c   true, or disabl
13570 65 20 74 68 65 6d 20 69 66 20 46 20 69 73 20 66  e them if F is f
13580 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  alse..*/.SQLITE_
13590 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
135a0 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f  extended_result_
135b0 63 6f 64 65 73 28 73 71 6c 69 74 65 33 2a 2c 20  codes(sqlite3*, 
135c0 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a  int onoff);../*.
135d0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4c 61 73  ** CAPI3REF: Las
135e0 74 20 49 6e 73 65 72 74 20 52 6f 77 69 64 20 7b  t Insert Rowid {
135f0 48 31 32 32 32 30 7d 20 3c 53 31 30 37 30 30 3e  H12220} <S10700>
13600 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 65 6e 74 72  .**.** Each entr
13610 79 20 69 6e 20 61 6e 20 53 51 4c 69 74 65 20 74  y in an SQLite t
13620 61 62 6c 65 20 68 61 73 20 61 20 75 6e 69 71 75  able has a uniqu
13630 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 0a  e 64-bit signed.
13640 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 79 20 63  ** integer key c
13650 61 6c 6c 65 64 20 74 68 65 20 22 72 6f 77 69 64  alled the "rowid
13660 22 2e 20 54 68 65 20 72 6f 77 69 64 20 69 73 20  ". The rowid is 
13670 61 6c 77 61 79 73 20 61 76 61 69 6c 61 62 6c 65  always available
13680 0a 2a 2a 20 61 73 20 61 6e 20 75 6e 64 65 63 6c  .** as an undecl
13690 61 72 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  ared column name
136a0 64 20 52 4f 57 49 44 2c 20 4f 49 44 2c 20 6f 72  d ROWID, OID, or
136b0 20 5f 52 4f 57 49 44 5f 20 61 73 20 6c 6f 6e 67   _ROWID_ as long
136c0 20 61 73 20 74 68 6f 73 65 0a 2a 2a 20 6e 61 6d   as those.** nam
136d0 65 73 20 61 72 65 20 6e 6f 74 20 61 6c 73 6f 20  es are not also 
136e0 75 73 65 64 20 62 79 20 65 78 70 6c 69 63 69 74  used by explicit
136f0 6c 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c 75  ly declared colu
13700 6d 6e 73 2e 20 49 66 0a 2a 2a 20 74 68 65 20 74  mns. If.** the t
13710 61 62 6c 65 20 68 61 73 20 61 20 63 6f 6c 75 6d  able has a colum
13720 6e 20 6f 66 20 74 79 70 65 20 49 4e 54 45 47 45  n of type INTEGE
13730 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 74 68  R PRIMARY KEY th
13740 65 6e 20 74 68 61 74 20 63 6f 6c 75 6d 6e 0a 2a  en that column.*
13750 2a 20 69 73 20 61 6e 6f 74 68 65 72 20 61 6c 69  * is another ali
13760 61 73 20 66 6f 72 20 74 68 65 20 72 6f 77 69 64  as for the rowid
13770 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
13780 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65  tine returns the
13790 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 6d 6f   rowid of the mo
137a0 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 73 75 63  st recent.** suc
137b0 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54 5d  cessful [INSERT]
137c0 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61   into the databa
137d0 73 65 20 66 72 6f 6d 20 74 68 65 20 5b 64 61 74  se from the [dat
137e0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
137f0 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 72 73  ].** in the firs
13800 74 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20  t argument.  If 
13810 6e 6f 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49  no successful [I
13820 4e 53 45 52 54 5d 73 0a 2a 2a 20 68 61 76 65 20  NSERT]s.** have 
13830 65 76 65 72 20 6f 63 63 75 72 72 65 64 20 6f 6e  ever occurred on
13840 20 74 68 61 74 20 64 61 74 61 62 61 73 65 20 63   that database c
13850 6f 6e 6e 65 63 74 69 6f 6e 2c 20 7a 65 72 6f 20  onnection, zero 
13860 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
13870 2a 2a 20 49 66 20 61 6e 20 5b 49 4e 53 45 52 54  ** If an [INSERT
13880 5d 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20  ] occurs within 
13890 61 20 74 72 69 67 67 65 72 2c 20 74 68 65 6e 20  a trigger, then 
138a0 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65  the rowid of the
138b0 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 72 6f 77   inserted.** row
138c0 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 20   is returned by 
138d0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 73 20  this routine as 
138e0 6c 6f 6e 67 20 61 73 20 74 68 65 20 74 72 69 67  long as the trig
138f0 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a  ger is running..
13900 2a 2a 20 42 75 74 20 6f 6e 63 65 20 74 68 65 20  ** But once the 
13910 74 72 69 67 67 65 72 20 74 65 72 6d 69 6e 61 74  trigger terminat
13920 65 73 2c 20 74 68 65 20 76 61 6c 75 65 20 72 65  es, the value re
13930 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 20 72  turned by this r
13940 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 76 65 72 74  outine.** revert
13950 73 20 74 6f 20 74 68 65 20 6c 61 73 74 20 76 61  s to the last va
13960 6c 75 65 20 69 6e 73 65 72 74 65 64 20 62 65 66  lue inserted bef
13970 6f 72 65 20 74 68 65 20 74 72 69 67 67 65 72 20  ore the trigger 
13980 66 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20  fired..**.** An 
13990 5b 49 4e 53 45 52 54 5d 20 74 68 61 74 20 66 61  [INSERT] that fa
139a0 69 6c 73 20 64 75 65 20 74 6f 20 61 20 63 6f 6e  ils due to a con
139b0 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f  straint violatio
139c0 6e 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 73 75  n is not a.** su
139d0 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54  ccessful [INSERT
139e0 5d 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 63  ] and does not c
139f0 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20  hange the value 
13a00 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73  returned by this
13a10 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20 20 54 68  .** routine.  Th
13a20 75 73 20 49 4e 53 45 52 54 20 4f 52 20 46 41 49  us INSERT OR FAI
13a30 4c 2c 20 49 4e 53 45 52 54 20 4f 52 20 49 47 4e  L, INSERT OR IGN
13a40 4f 52 45 2c 20 49 4e 53 45 52 54 20 4f 52 20 52  ORE, INSERT OR R
13a50 4f 4c 4c 42 41 43 4b 2c 0a 2a 2a 20 61 6e 64 20  OLLBACK,.** and 
13a60 49 4e 53 45 52 54 20 4f 52 20 41 42 4f 52 54 20  INSERT OR ABORT 
13a70 6d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 20  make no changes 
13a80 74 6f 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  to the return va
13a90 6c 75 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 72  lue of this.** r
13aa0 6f 75 74 69 6e 65 20 77 68 65 6e 20 74 68 65 69  outine when thei
13ab0 72 20 69 6e 73 65 72 74 69 6f 6e 20 66 61 69 6c  r insertion fail
13ac0 73 2e 20 20 57 68 65 6e 20 49 4e 53 45 52 54 20  s.  When INSERT 
13ad0 4f 52 20 52 45 50 4c 41 43 45 0a 2a 2a 20 65 6e  OR REPLACE.** en
13ae0 63 6f 75 6e 74 65 72 73 20 61 20 63 6f 6e 73 74  counters a const
13af0 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 2c  raint violation,
13b00 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 66 61 69   it does not fai
13b10 6c 2e 20 20 54 68 65 0a 2a 2a 20 49 4e 53 45 52  l.  The.** INSER
13b20 54 20 63 6f 6e 74 69 6e 75 65 73 20 74 6f 20 63  T continues to c
13b30 6f 6d 70 6c 65 74 69 6f 6e 20 61 66 74 65 72 20  ompletion after 
13b40 64 65 6c 65 74 69 6e 67 20 72 6f 77 73 20 74 68  deleting rows th
13b50 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 74 68 65  at caused.** the
13b60 20 63 6f 6e 73 74 72 61 69 6e 74 20 70 72 6f 62   constraint prob
13b70 6c 65 6d 20 73 6f 20 49 4e 53 45 52 54 20 4f 52  lem so INSERT OR
13b80 20 52 45 50 4c 41 43 45 20 77 69 6c 6c 20 61 6c   REPLACE will al
13b90 77 61 79 73 20 63 68 61 6e 67 65 0a 2a 2a 20 74  ways change.** t
13ba0 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
13bb0 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66 61 63  of this interfac
13bc0 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65  e..**.** For the
13bd0 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69   purposes of thi
13be0 73 20 72 6f 75 74 69 6e 65 2c 20 61 6e 20 5b 49  s routine, an [I
13bf0 4e 53 45 52 54 5d 20 69 73 20 63 6f 6e 73 69 64  NSERT] is consid
13c00 65 72 65 64 20 74 6f 0a 2a 2a 20 62 65 20 73 75  ered to.** be su
13c10 63 63 65 73 73 66 75 6c 20 65 76 65 6e 20 69 66  ccessful even if
13c20 20 69 74 20 69 73 20 73 75 62 73 65 71 75 65 6e   it is subsequen
13c30 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e  tly rolled back.
13c40 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54  .**.** INVARIANT
13c50 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 32 31  S:.**.** {H12221
13c60 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c  } The [sqlite3_l
13c70 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64  ast_insert_rowid
13c80 28 29 5d 20 66 75 6e 63 74 69 6f 6e 20 73 68 61  ()] function sha
13c90 6c 6c 20 72 65 74 75 72 6e 20 74 68 65 20 72 6f  ll return the ro
13ca0 77 69 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  wid.**          
13cb0 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  of the most rece
13cc0 6e 74 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49  nt successful [I
13cd0 4e 53 45 52 54 5d 20 70 65 72 66 6f 72 6d 65 64  NSERT] performed
13ce0 20 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20   on the same.** 
13cf0 20 20 20 20 20 20 20 20 20 5b 64 61 74 61 62 61           [databa
13d00 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61  se connection] a
13d10 6e 64 20 77 69 74 68 69 6e 20 74 68 65 20 73 61  nd within the sa
13d20 6d 65 20 6f 72 20 68 69 67 68 65 72 20 6c 65 76  me or higher lev
13d30 65 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  el.**          t
13d40 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2c 20  rigger context, 
13d50 6f 72 20 7a 65 72 6f 20 69 66 20 74 68 65 72 65  or zero if there
13d60 20 68 61 76 65 20 62 65 65 6e 20 6e 6f 20 71 75   have been no qu
13d70 61 6c 69 66 79 69 6e 67 0a 2a 2a 20 20 20 20 20  alifying.**     
13d80 20 20 20 20 20 5b 49 4e 53 45 52 54 5d 20 73 74       [INSERT] st
13d90 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20  atements..**.** 
13da0 7b 48 31 32 32 32 33 7d 20 54 68 65 20 5b 73 71  {H12223} The [sq
13db0 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72  lite3_last_inser
13dc0 74 5f 72 6f 77 69 64 28 29 5d 20 66 75 6e 63 74  t_rowid()] funct
13dd0 69 6f 6e 20 73 68 61 6c 6c 20 72 65 74 75 72 6e  ion shall return
13de0 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
13df0 20 73 61 6d 65 20 76 61 6c 75 65 20 77 68 65 6e   same value when
13e00 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65   called from the
13e10 20 73 61 6d 65 20 74 72 69 67 67 65 72 20 63 6f   same trigger co
13e20 6e 74 65 78 74 0a 2a 2a 20 20 20 20 20 20 20 20  ntext.**        
13e30 20 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 62 65    immediately be
13e40 66 6f 72 65 20 61 6e 64 20 61 66 74 65 72 20 61  fore and after a
13e50 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a 2a 0a   [ROLLBACK]..**.
13e60 2a 2a 20 41 53 53 55 4d 50 54 49 4f 4e 53 3a 0a  ** ASSUMPTIONS:.
13e70 2a 2a 0a 2a 2a 20 7b 41 31 32 32 33 32 7d 20 49  **.** {A12232} I
13e80 66 20 61 20 73 65 70 61 72 61 74 65 20 74 68 72  f a separate thr
13e90 65 61 64 20 70 65 72 66 6f 72 6d 73 20 61 20 6e  ead performs a n
13ea0 65 77 20 5b 49 4e 53 45 52 54 5d 20 6f 6e 20 74  ew [INSERT] on t
13eb0 68 65 20 73 61 6d 65 0a 2a 2a 20 20 20 20 20 20  he same.**      
13ec0 20 20 20 20 64 61 74 61 62 61 73 65 20 63 6f 6e      database con
13ed0 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68  nection while th
13ee0 65 20 5b 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f  e [sqlite3_last_
13ef0 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 5d 0a  insert_rowid()].
13f00 2a 2a 20 20 20 20 20 20 20 20 20 20 66 75 6e 63  **          func
13f10 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 20  tion is running 
13f20 61 6e 64 20 74 68 75 73 20 63 68 61 6e 67 65 73  and thus changes
13f30 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 72 74   the last insert
13f40 20 72 6f 77 69 64 2c 0a 2a 2a 20 20 20 20 20 20   rowid,.**      
13f50 20 20 20 20 74 68 65 6e 20 74 68 65 20 76 61 6c      then the val
13f60 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b  ue returned by [
13f70 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73  sqlite3_last_ins
13f80 65 72 74 5f 72 6f 77 69 64 28 29 5d 20 69 73 0a  ert_rowid()] is.
13f90 2a 2a 20 20 20 20 20 20 20 20 20 20 75 6e 70 72  **          unpr
13fa0 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 6d 69  edictable and mi
13fb0 67 68 74 20 6e 6f 74 20 65 71 75 61 6c 20 65 69  ght not equal ei
13fc0 74 68 65 72 20 74 68 65 20 6f 6c 64 20 6f 72 20  ther the old or 
13fd0 74 68 65 20 6e 65 77 0a 2a 2a 20 20 20 20 20 20  the new.**      
13fe0 20 20 20 20 6c 61 73 74 20 69 6e 73 65 72 74 20      last insert 
13ff0 72 6f 77 69 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  rowid..*/.SQLITE
14000 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74  _API sqlite3_int
14010 36 34 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f  64 sqlite3_last_
14020 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 73 71 6c  insert_rowid(sql
14030 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  ite3*);../*.** C
14040 41 50 49 33 52 45 46 3a 20 43 6f 75 6e 74 20 54  API3REF: Count T
14050 68 65 20 4e 75 6d 62 65 72 20 4f 66 20 52 6f 77  he Number Of Row
14060 73 20 4d 6f 64 69 66 69 65 64 20 7b 48 31 32 32  s Modified {H122
14070 34 30 7d 20 3c 53 31 30 36 30 30 3e 0a 2a 2a 0a  40} <S10600>.**.
14080 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
14090 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
140a0 62 65 72 20 6f 66 20 64 61 74 61 62 61 73 65 20  ber of database 
140b0 72 6f 77 73 20 74 68 61 74 20 77 65 72 65 20 63  rows that were c
140c0 68 61 6e 67 65 64 0a 2a 2a 20 6f 72 20 69 6e 73  hanged.** or ins
140d0 65 72 74 65 64 20 6f 72 20 64 65 6c 65 74 65 64  erted or deleted
140e0 20 62 79 20 74 68 65 20 6d 6f 73 74 20 72 65 63   by the most rec
140f0 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65 64 20  ently completed 
14100 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a  SQL statement.**
14110 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73   on the [databas
14120 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70  e connection] sp
14130 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 66  ecified by the f
14140 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e 0a  irst parameter..
14150 2a 2a 20 4f 6e 6c 79 20 63 68 61 6e 67 65 73 20  ** Only changes 
14160 74 68 61 74 20 61 72 65 20 64 69 72 65 63 74 6c  that are directl
14170 79 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74  y specified by t
14180 68 65 20 5b 49 4e 53 45 52 54 5d 2c 20 5b 55 50  he [INSERT], [UP
14190 44 41 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 44 45  DATE],.** or [DE
141a0 4c 45 54 45 5d 20 73 74 61 74 65 6d 65 6e 74 20  LETE] statement 
141b0 61 72 65 20 63 6f 75 6e 74 65 64 2e 20 20 41 75  are counted.  Au
141c0 78 69 6c 69 61 72 79 20 63 68 61 6e 67 65 73 20  xiliary changes 
141d0 63 61 75 73 65 64 20 62 79 0a 2a 2a 20 74 72 69  caused by.** tri
141e0 67 67 65 72 73 20 61 72 65 20 6e 6f 74 20 63 6f  ggers are not co
141f0 75 6e 74 65 64 2e 20 55 73 65 20 74 68 65 20 5b  unted. Use the [
14200 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68  sqlite3_total_ch
14210 61 6e 67 65 73 28 29 5d 20 66 75 6e 63 74 69 6f  anges()] functio
14220 6e 0a 2a 2a 20 74 6f 20 66 69 6e 64 20 74 68 65  n.** to find the
14230 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
14240 20 63 68 61 6e 67 65 73 20 69 6e 63 6c 75 64 69   changes includi
14250 6e 67 20 63 68 61 6e 67 65 73 20 63 61 75 73 65  ng changes cause
14260 64 20 62 79 20 74 72 69 67 67 65 72 73 2e 0a 2a  d by triggers..*
14270 2a 0a 2a 2a 20 41 20 22 72 6f 77 20 63 68 61 6e  *.** A "row chan
14280 67 65 22 20 69 73 20 61 20 63 68 61 6e 67 65 20  ge" is a change 
14290 74 6f 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20  to a single row 
142a0 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c  of a single tabl
142b0 65 0a 2a 2a 20 63 61 75 73 65 64 20 62 79 20 61  e.** caused by a
142c0 6e 20 49 4e 53 45 52 54 2c 20 44 45 4c 45 54 45  n INSERT, DELETE
142d0 2c 20 6f 72 20 55 50 44 41 54 45 20 73 74 61 74  , or UPDATE stat
142e0 65 6d 65 6e 74 2e 20 20 52 6f 77 73 20 74 68 61  ement.  Rows tha
142f0 74 0a 2a 2a 20 61 72 65 20 63 68 61 6e 67 65 64  t.** are changed
14300 20 61 73 20 73 69 64 65 20 65 66 66 65 63 74 73   as side effects
14310 20 6f 66 20 52 45 50 4c 41 43 45 20 63 6f 6e 73   of REPLACE cons
14320 74 72 61 69 6e 74 20 72 65 73 6f 6c 75 74 69 6f  traint resolutio
14330 6e 2c 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c 20  n,.** rollback, 
14340 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e 67  ABORT processing
14350 2c 20 44 52 4f 50 20 54 41 42 4c 45 2c 20 6f 72  , DROP TABLE, or
14360 20 62 79 20 61 6e 79 20 6f 74 68 65 72 0a 2a 2a   by any other.**
14370 20 6d 65 63 68 61 6e 69 73 6d 73 20 64 6f 20 6e   mechanisms do n
14380 6f 74 20 63 6f 75 6e 74 20 61 73 20 64 69 72 65  ot count as dire
14390 63 74 20 72 6f 77 20 63 68 61 6e 67 65 73 2e 0a  ct row changes..
143a0 2a 2a 0a 2a 2a 20 41 20 22 74 72 69 67 67 65 72  **.** A "trigger
143b0 20 63 6f 6e 74 65 78 74 22 20 69 73 20 61 20 73   context" is a s
143c0 63 6f 70 65 20 6f 66 20 65 78 65 63 75 74 69 6f  cope of executio
143d0 6e 20 74 68 61 74 20 62 65 67 69 6e 73 20 61 6e  n that begins an
143e0 64 0a 2a 2a 20 65 6e 64 73 20 77 69 74 68 20 74  d.** ends with t
143f0 68 65 20 73 63 72 69 70 74 20 6f 66 20 61 20 74  he script of a t
14400 72 69 67 67 65 72 2e 20 20 4d 6f 73 74 20 53 51  rigger.  Most SQ
14410 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65  L statements are
14420 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 6f 75  .** evaluated ou
14430 74 73 69 64 65 20 6f 66 20 61 6e 79 20 74 72 69  tside of any tri
14440 67 67 65 72 2e 20 20 54 68 69 73 20 69 73 20 74  gger.  This is t
14450 68 65 20 22 74 6f 70 20 6c 65 76 65 6c 22 0a 2a  he "top level".*
14460 2a 20 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78  * trigger contex
14470 74 2e 20 20 49 66 20 61 20 74 72 69 67 67 65 72  t.  If a trigger
14480 20 66 69 72 65 73 20 66 72 6f 6d 20 74 68 65 20   fires from the 
14490 74 6f 70 20 6c 65 76 65 6c 2c 20 61 0a 2a 2a 20  top level, a.** 
144a0 6e 65 77 20 74 72 69 67 67 65 72 20 63 6f 6e 74  new trigger cont
144b0 65 78 74 20 69 73 20 65 6e 74 65 72 65 64 20 66  ext is entered f
144c0 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e 20  or the duration 
144d0 6f 66 20 74 68 61 74 20 6f 6e 65 0a 2a 2a 20 74  of that one.** t
144e0 72 69 67 67 65 72 2e 20 20 53 75 62 74 72 69 67  rigger.  Subtrig
144f0 67 65 72 73 20 63 72 65 61 74 65 20 73 75 62 63  gers create subc
14500 6f 6e 74 65 78 74 73 20 66 6f 72 20 74 68 65 69  ontexts for thei
14510 72 20 64 75 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  r duration..**.*
14520 2a 20 43 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74  * Calling [sqlit
14530 65 33 5f 65 78 65 63 28 29 5d 20 6f 72 20 5b 73  e3_exec()] or [s
14540 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 72  qlite3_step()] r
14550 65 63 75 72 73 69 76 65 6c 79 20 64 6f 65 73 0a  ecursively does.
14560 2a 2a 20 6e 6f 74 20 63 72 65 61 74 65 20 61 20  ** not create a 
14570 6e 65 77 20 74 72 69 67 67 65 72 20 63 6f 6e 74  new trigger cont
14580 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ext..**.** This 
14590 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
145a0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64   the number of d
145b0 69 72 65 63 74 20 72 6f 77 20 63 68 61 6e 67 65  irect row change
145c0 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6d 6f 73 74  s in the.** most
145d0 20 72 65 63 65 6e 74 20 49 4e 53 45 52 54 2c 20   recent INSERT, 
145e0 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45 54  UPDATE, or DELET
145f0 45 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68  E statement with
14600 69 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74  in the same.** t
14610 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2e 0a  rigger context..
14620 2a 2a 0a 2a 2a 20 54 68 75 73 2c 20 77 68 65 6e  **.** Thus, when
14630 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65   called from the
14640 20 74 6f 70 20 6c 65 76 65 6c 2c 20 74 68 69 73   top level, this
14650 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
14660 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20  s the.** number 
14670 6f 66 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68  of changes in th
14680 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 49 4e  e most recent IN
14690 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72  SERT, UPDATE, or
146a0 20 44 45 4c 45 54 45 0a 2a 2a 20 74 68 61 74 20   DELETE.** that 
146b0 61 6c 73 6f 20 6f 63 63 75 72 72 65 64 20 61 74  also occurred at
146c0 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2e 20   the top level. 
146d0 20 57 69 74 68 69 6e 20 74 68 65 20 62 6f 64 79   Within the body
146e0 20 6f 66 20 61 20 74 72 69 67 67 65 72 2c 0a 2a   of a trigger,.*
146f0 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 68  * the sqlite3_ch
14700 61 6e 67 65 73 28 29 20 69 6e 74 65 72 66 61 63  anges() interfac
14710 65 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20  e can be called 
14720 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 75 6d 62  to find the numb
14730 65 72 20 6f 66 0a 2a 2a 20 63 68 61 6e 67 65 73  er of.** changes
14740 20 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63   in the most rec
14750 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65 64 20  ently completed 
14760 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20  INSERT, UPDATE, 
14770 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 73 74 61  or DELETE.** sta
14780 74 65 6d 65 6e 74 20 77 69 74 68 69 6e 20 74 68  tement within th
14790 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 73 61  e body of the sa
147a0 6d 65 20 74 72 69 67 67 65 72 2e 0a 2a 2a 20 48  me trigger..** H
147b0 6f 77 65 76 65 72 2c 20 74 68 65 20 6e 75 6d 62  owever, the numb
147c0 65 72 20 72 65 74 75 72 6e 65 64 20 64 6f 65 73  er returned does
147d0 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 63 68 61   not include cha
147e0 6e 67 65 73 0a 2a 2a 20 63 61 75 73 65 64 20 62  nges.** caused b
147f0 79 20 73 75 62 74 72 69 67 67 65 72 73 20 73 69  y subtriggers si
14800 6e 63 65 20 74 68 6f 73 65 20 68 61 76 65 20 74  nce those have t
14810 68 65 69 72 20 6f 77 6e 20 63 6f 6e 74 65 78 74  heir own context
14820 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69  ..**.** SQLite i
14830 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 63 6f  mplements the co
14840 6d 6d 61 6e 64 20 22 44 45 4c 45 54 45 20 46 52  mmand "DELETE FR
14850 4f 4d 20 74 61 62 6c 65 22 20 77 69 74 68 6f 75  OM table" withou
14860 74 20 61 20 57 48 45 52 45 20 63 6c 61 75 73 65  t a WHERE clause
14870 0a 2a 2a 20 62 79 20 64 72 6f 70 70 69 6e 67 20  .** by dropping 
14880 61 6e 64 20 72 65 63 72 65 61 74 69 6e 67 20 74  and recreating t
14890 68 65 20 74 61 62 6c 65 2e 20 20 44 6f 69 6e 67  he table.  Doing
148a0 20 73 6f 20 69 73 20 6d 75 63 68 20 66 61 73 74   so is much fast
148b0 65 72 20 74 68 61 6e 20 67 6f 69 6e 67 0a 2a 2a  er than going.**
148c0 20 74 68 72 6f 75 67 68 20 61 6e 64 20 64 65 6c   through and del
148d0 65 74 69 6e 67 20 69 6e 64 69 76 69 64 75 61 6c  eting individual
148e0 20 65 6c 65 6d 65 6e 74 73 20 66 72 6f 6d 20 74   elements from t
148f0 68 65 20 74 61 62 6c 65 2e 20 20 42 65 63 61 75  he table.  Becau
14900 73 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 6f 70  se of this.** op
14910 74 69 6d 69 7a 61 74 69 6f 6e 2c 20 74 68 65 20  timization, the 
14920 64 65 6c 65 74 69 6f 6e 73 20 69 6e 20 22 44 45  deletions in "DE
14930 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 22  LETE FROM table"
14940 20 61 72 65 20 6e 6f 74 20 72 6f 77 20 63 68 61   are not row cha
14950 6e 67 65 73 20 61 6e 64 0a 2a 2a 20 77 69 6c 6c  nges and.** will
14960 20 6e 6f 74 20 62 65 20 63 6f 75 6e 74 65 64 20   not be counted 
14970 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63  by the sqlite3_c
14980 68 61 6e 67 65 73 28 29 20 6f 72 20 5b 73 71 6c  hanges() or [sql
14990 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67  ite3_total_chang
149a0 65 73 28 29 5d 0a 2a 2a 20 66 75 6e 63 74 69 6f  es()].** functio
149b0 6e 73 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f  ns, regardless o
149c0 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  f the number of 
149d0 65 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 77 65  elements that we
149e0 72 65 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 2a 2a  re originally.**
149f0 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20   in the table.  
14a00 54 6f 20 67 65 74 20 61 6e 20 61 63 63 75 72 61  To get an accura
14a10 74 65 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20  te count of the 
14a20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 64  number of rows d
14a30 65 6c 65 74 65 64 2c 20 75 73 65 0a 2a 2a 20 22  eleted, use.** "
14a40 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c  DELETE FROM tabl
14a50 65 20 57 48 45 52 45 20 31 22 20 69 6e 73 74 65  e WHERE 1" inste
14a60 61 64 2e 20 20 4f 72 20 72 65 63 6f 6d 70 69 6c  ad.  Or recompil
14a70 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b  e using the.** [
14a80 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 55 4e  SQLITE_OMIT_TRUN
14a90 43 41 54 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  CATE_OPTIMIZATIO
14aa0 4e 5d 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20  N] compile-time 
14ab0 6f 70 74 69 6f 6e 20 74 6f 20 64 69 73 61 62 6c  option to disabl
14ac0 65 20 74 68 65 0a 2a 2a 20 6f 70 74 69 6d 69 7a  e the.** optimiz
14ad0 61 74 69 6f 6e 20 6f 6e 20 61 6c 6c 20 71 75 65  ation on all que
14ae0 72 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41  ries..**.** INVA
14af0 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48  RIANTS:.**.** {H
14b00 31 32 32 34 31 7d 20 54 68 65 20 5b 73 71 6c 69  12241} The [sqli
14b10 74 65 33 5f 63 68 61 6e 67 65 73 28 29 5d 20 66  te3_changes()] f
14b20 75 6e 63 74 69 6f 6e 20 73 68 61 6c 6c 20 72 65  unction shall re
14b30 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
14b40 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72  of.**          r
14b50 6f 77 20 63 68 61 6e 67 65 73 20 63 61 75 73 65  ow changes cause
14b60 64 20 62 79 20 74 68 65 20 6d 6f 73 74 20 72 65  d by the most re
14b70 63 65 6e 74 20 49 4e 53 45 52 54 2c 20 55 50 44  cent INSERT, UPD
14b80 41 54 45 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20  ATE,.**         
14b90 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74 65   or DELETE state
14ba0 6d 65 6e 74 20 6f 6e 20 74 68 65 20 73 61 6d 65  ment on the same
14bb0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
14bc0 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 20 20 20 20  tion and.**     
14bd0 20 20 20 20 20 77 69 74 68 69 6e 20 74 68 65 20       within the 
14be0 73 61 6d 65 20 6f 72 20 68 69 67 68 65 72 20 74  same or higher t
14bf0 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2c 20  rigger context, 
14c00 6f 72 20 7a 65 72 6f 20 69 66 20 74 68 65 72 65  or zero if there
14c10 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 20 20 20   have.**        
14c20 20 20 6e 6f 74 20 62 65 65 6e 20 61 6e 79 20 71    not been any q
14c30 75 61 6c 69 66 79 69 6e 67 20 72 6f 77 20 63 68  ualifying row ch
14c40 61 6e 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  anges..**.** {H1
14c50 32 32 34 33 7d 20 53 74 61 74 65 6d 65 6e 74 73  2243} Statements
14c60 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 44 45   of the form "DE
14c70 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 6e  LETE FROM tablen
14c80 61 6d 65 22 20 77 69 74 68 20 6e 6f 0a 2a 2a 20  ame" with no.** 
14c90 20 20 20 20 20 20 20 20 20 57 48 45 52 45 20 63           WHERE c
14ca0 6c 61 75 73 65 20 73 68 61 6c 6c 20 63 61 75 73  lause shall caus
14cb0 65 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c  e subsequent cal
14cc0 6c 73 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20  ls to.**        
14cd0 20 20 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67    [sqlite3_chang
14ce0 65 73 28 29 5d 20 74 6f 20 72 65 74 75 72 6e 20  es()] to return 
14cf0 7a 65 72 6f 2c 20 72 65 67 61 72 64 6c 65 73 73  zero, regardless
14d00 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   of the.**      
14d10 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f      number of ro
14d20 77 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 69 6e  ws originally in
14d30 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a   the table..**.*
14d40 2a 20 41 53 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a  * ASSUMPTIONS:.*
14d50 2a 0a 2a 2a 20 7b 41 31 32 32 35 32 7d 20 49 66  *.** {A12252} If
14d60 20 61 20 73 65 70 61 72 61 74 65 20 74 68 72 65   a separate thre
14d70 61 64 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73  ad makes changes
14d80 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74   on the same dat
14d90 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
14da0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 69  .**          whi
14db0 6c 65 20 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e  le [sqlite3_chan
14dc0 67 65 73 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e  ges()] is runnin
14dd0 67 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65  g then the value
14de0 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 20 20 20   returned.**    
14df0 20 20 20 20 20 20 69 73 20 75 6e 70 72 65 64 69        is unpredi
14e00 63 74 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20 6d  ctable and not m
14e10 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51  eaningful..*/.SQ
14e20 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
14e30 69 74 65 33 5f 63 68 61 6e 67 65 73 28 73 71 6c  ite3_changes(sql
14e40 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  ite3*);../*.** C
14e50 41 50 49 33 52 45 46 3a 20 54 6f 74 61 6c 20 4e  API3REF: Total N
14e60 75 6d 62 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f  umber Of Rows Mo
14e70 64 69 66 69 65 64 20 7b 48 31 32 32 36 30 7d 20  dified {H12260} 
14e80 3c 53 31 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S10600>.**.** T
14e90 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
14ea0 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  urns the number 
14eb0 6f 66 20 72 6f 77 20 63 68 61 6e 67 65 73 20 63  of row changes c
14ec0 61 75 73 65 64 20 62 79 20 49 4e 53 45 52 54 2c  aused by INSERT,
14ed0 0a 2a 2a 20 55 50 44 41 54 45 20 6f 72 20 44 45  .** UPDATE or DE
14ee0 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 73 20  LETE statements 
14ef0 73 69 6e 63 65 20 74 68 65 20 5b 64 61 74 61 62  since the [datab
14f00 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
14f10 77 61 73 20 6f 70 65 6e 65 64 2e 0a 2a 2a 20 54  was opened..** T
14f20 68 65 20 63 6f 75 6e 74 20 69 6e 63 6c 75 64 65  he count include
14f30 73 20 61 6c 6c 20 63 68 61 6e 67 65 73 20 66 72  s all changes fr
14f40 6f 6d 20 61 6c 6c 20 74 72 69 67 67 65 72 20 63  om all trigger c
14f50 6f 6e 74 65 78 74 73 2e 20 20 48 6f 77 65 76 65  ontexts.  Howeve
14f60 72 2c 0a 2a 2a 20 74 68 65 20 63 6f 75 6e 74 20  r,.** the count 
14f70 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65  does not include
14f80 20 63 68 61 6e 67 65 73 20 75 73 65 64 20 74 6f   changes used to
14f90 20 69 6d 70 6c 65 6d 65 6e 74 20 52 45 50 4c 41   implement REPLA
14fa0 43 45 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 0a  CE constraints,.
14fb0 2a 2a 20 64 6f 20 72 6f 6c 6c 62 61 63 6b 73 20  ** do rollbacks 
14fc0 6f 72 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73  or ABORT process
14fd0 69 6e 67 2c 20 6f 72 20 44 52 4f 50 20 74 61 62  ing, or DROP tab
14fe0 6c 65 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a  le processing..*
14ff0 2a 20 54 68 65 20 63 68 61 6e 67 65 73 20 61 72  * The changes ar
15000 65 20 63 6f 75 6e 74 65 64 20 61 73 20 73 6f 6f  e counted as soo
15010 6e 20 61 73 20 74 68 65 20 73 74 61 74 65 6d 65  n as the stateme
15020 6e 74 20 74 68 61 74 20 6d 61 6b 65 73 20 74 68  nt that makes th
15030 65 6d 20 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65 74  em is.** complet
15040 65 64 20 28 77 68 65 6e 20 74 68 65 20 73 74 61  ed (when the sta
15050 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 69 73  tement handle is
15060 20 70 61 73 73 65 64 20 74 6f 20 5b 73 71 6c 69   passed to [sqli
15070 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a  te3_reset()] or.
15080 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61  ** [sqlite3_fina
15090 6c 69 7a 65 28 29 5d 29 2e 0a 2a 2a 0a 2a 2a 20  lize()])..**.** 
150a0 53 51 4c 69 74 65 20 69 6d 70 6c 65 6d 65 6e 74  SQLite implement
150b0 73 20 74 68 65 20 63 6f 6d 6d 61 6e 64 20 22 44  s the command "D
150c0 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65  ELETE FROM table
150d0 22 20 77 69 74 68 6f 75 74 20 61 20 57 48 45 52  " without a WHER
150e0 45 20 63 6c 61 75 73 65 0a 2a 2a 20 62 79 20 64  E clause.** by d
150f0 72 6f 70 70 69 6e 67 20 61 6e 64 20 72 65 63 72  ropping and recr
15100 65 61 74 69 6e 67 20 74 68 65 20 74 61 62 6c 65  eating the table
15110 2e 20 20 28 54 68 69 73 20 69 73 20 6d 75 63 68  .  (This is much
15120 20 66 61 73 74 65 72 20 74 68 61 6e 20 67 6f 69   faster than goi
15130 6e 67 0a 2a 2a 20 74 68 72 6f 75 67 68 20 61 6e  ng.** through an
15140 64 20 64 65 6c 65 74 69 6e 67 20 69 6e 64 69 76  d deleting indiv
15150 69 64 75 61 6c 20 65 6c 65 6d 65 6e 74 73 20 66  idual elements f
15160 72 6f 6d 20 74 68 65 20 74 61 62 6c 65 2e 29 20  rom the table.) 
15170 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 69 73   Because of this
15180 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  .** optimization
15190 2c 20 74 68 65 20 64 65 6c 65 74 69 6f 6e 73 20  , the deletions 
151a0 69 6e 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20  in "DELETE FROM 
151b0 74 61 62 6c 65 22 20 61 72 65 20 6e 6f 74 20 72  table" are not r
151c0 6f 77 20 63 68 61 6e 67 65 73 20 61 6e 64 0a 2a  ow changes and.*
151d0 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 63 6f  * will not be co
151e0 75 6e 74 65 64 20 62 79 20 74 68 65 20 73 71 6c  unted by the sql
151f0 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20 6f  ite3_changes() o
15200 72 20 5b 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c  r [sqlite3_total
15210 5f 63 68 61 6e 67 65 73 28 29 5d 0a 2a 2a 20 66  _changes()].** f
15220 75 6e 63 74 69 6f 6e 73 2c 20 72 65 67 61 72 64  unctions, regard
15230 6c 65 73 73 20 6f 66 20 74 68 65 20 6e 75 6d 62  less of the numb
15240 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 74  er of elements t
15250 68 61 74 20 77 65 72 65 20 6f 72 69 67 69 6e 61  hat were origina
15260 6c 6c 79 0a 2a 2a 20 69 6e 20 74 68 65 20 74 61  lly.** in the ta
15270 62 6c 65 2e 20 20 54 6f 20 67 65 74 20 61 6e 20  ble.  To get an 
15280 61 63 63 75 72 61 74 65 20 63 6f 75 6e 74 20 6f  accurate count o
15290 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  f the number of 
152a0 72 6f 77 73 20 64 65 6c 65 74 65 64 2c 20 75 73  rows deleted, us
152b0 65 0a 2a 2a 20 22 44 45 4c 45 54 45 20 46 52 4f  e.** "DELETE FRO
152c0 4d 20 74 61 62 6c 65 20 57 48 45 52 45 20 31 22  M table WHERE 1"
152d0 20 69 6e 73 74 65 61 64 2e 20 20 20 4f 72 20 72   instead.   Or r
152e0 65 63 6f 6d 70 69 6c 65 20 75 73 69 6e 67 20 74  ecompile using t
152f0 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4d  he.** [SQLITE_OM
15300 49 54 5f 54 52 55 4e 43 41 54 45 5f 4f 50 54 49  IT_TRUNCATE_OPTI
15310 4d 49 5a 41 54 49 4f 4e 5d 20 63 6f 6d 70 69 6c  MIZATION] compil
15320 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 20 74 6f  e-time option to
15330 20 64 69 73 61 62 6c 65 20 74 68 65 0a 2a 2a 20   disable the.** 
15340 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6f 6e 20  optimization on 
15350 61 6c 6c 20 71 75 65 72 69 65 73 2e 0a 2a 2a 0a  all queries..**.
15360 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20  ** See also the 
15370 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73  [sqlite3_changes
15380 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a  ()] interface..*
15390 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
153a0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 36 31 7d 20  .**.** {H12261} 
153b0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 74 6f 74  The [sqlite3_tot
153c0 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 20 72 65  al_changes()] re
153d0 74 75 72 6e 73 20 74 68 65 20 74 6f 74 61 6c 20  turns the total 
153e0 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 20 20 20 20  number.**       
153f0 20 20 20 6f 66 20 72 6f 77 20 63 68 61 6e 67 65     of row change
15400 73 20 63 61 75 73 65 64 20 62 79 20 49 4e 53 45  s caused by INSE
15410 52 54 2c 20 55 50 44 41 54 45 2c 20 61 6e 64 2f  RT, UPDATE, and/
15420 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 20 20 20  or DELETE.**    
15430 20 20 20 20 20 20 73 74 61 74 65 6d 65 6e 74 73        statements
15440 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 5b 64 61   on the same [da
15450 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
15460 6e 5d 2c 20 69 6e 20 61 6e 79 0a 2a 2a 20 20 20  n], in any.**   
15470 20 20 20 20 20 20 20 74 72 69 67 67 65 72 20 63         trigger c
15480 6f 6e 74 65 78 74 2c 20 73 69 6e 63 65 20 74 68  ontext, since th
15490 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
154a0 63 74 69 6f 6e 20 77 61 73 20 63 72 65 61 74 65  ction was create
154b0 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 36 33  d..**.** {H12263
154c0 7d 20 53 74 61 74 65 6d 65 6e 74 73 20 6f 66 20  } Statements of 
154d0 74 68 65 20 66 6f 72 6d 20 22 44 45 4c 45 54 45  the form "DELETE
154e0 20 46 52 4f 4d 20 74 61 62 6c 65 6e 61 6d 65 22   FROM tablename"
154f0 20 77 69 74 68 20 6e 6f 0a 2a 2a 20 20 20 20 20   with no.**     
15500 20 20 20 20 20 57 48 45 52 45 20 63 6c 61 75 73       WHERE claus
15510 65 20 73 68 61 6c 6c 20 6e 6f 74 20 63 68 61 6e  e shall not chan
15520 67 65 20 74 68 65 20 76 61 6c 75 65 20 72 65 74  ge the value ret
15530 75 72 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 20  urned.**        
15540 20 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 74 6f    by [sqlite3_to
15550 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 2e 0a  tal_changes()]..
15560 2a 2a 0a 2a 2a 20 41 53 53 55 4d 50 54 49 4f 4e  **.** ASSUMPTION
15570 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 32 36 34  S:.**.** {A12264
15580 7d 20 49 66 20 61 20 73 65 70 61 72 61 74 65 20  } If a separate 
15590 74 68 72 65 61 64 20 6d 61 6b 65 73 20 63 68 61  thread makes cha
155a0 6e 67 65 73 20 6f 6e 20 74 68 65 20 73 61 6d 65  nges on the same
155b0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
155c0 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  tion.**         
155d0 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f   while [sqlite3_
155e0 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d  total_changes()]
155f0 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e   is running then
15600 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 20 20   the value.**   
15610 20 20 20 20 20 20 20 72 65 74 75 72 6e 65 64 20         returned 
15620 69 73 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65  is unpredictable
15630 20 61 6e 64 20 6e 6f 74 20 6d 65 61 6e 69 6e 67   and not meaning
15640 66 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ful..*/.SQLITE_A
15650 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74  PI int sqlite3_t
15660 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 73 71 6c  otal_changes(sql
15670 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  ite3*);../*.** C
15680 41 50 49 33 52 45 46 3a 20 49 6e 74 65 72 72 75  API3REF: Interru
15690 70 74 20 41 20 4c 6f 6e 67 2d 52 75 6e 6e 69 6e  pt A Long-Runnin
156a0 67 20 51 75 65 72 79 20 7b 48 31 32 32 37 30 7d  g Query {H12270}
156b0 20 3c 53 33 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S30500>.**.** 
156c0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61  This function ca
156d0 75 73 65 73 20 61 6e 79 20 70 65 6e 64 69 6e 67  uses any pending
156e0 20 64 61 74 61 62 61 73 65 20 6f 70 65 72 61 74   database operat
156f0 69 6f 6e 20 74 6f 20 61 62 6f 72 74 20 61 6e 64  ion to abort and
15700 0a 2a 2a 20 72 65 74 75 72 6e 20 61 74 20 69 74  .** return at it
15710 73 20 65 61 72 6c 69 65 73 74 20 6f 70 70 6f 72  s earliest oppor
15720 74 75 6e 69 74 79 2e 20 54 68 69 73 20 72 6f 75  tunity. This rou
15730 74 69 6e 65 20 69 73 20 74 79 70 69 63 61 6c 6c  tine is typicall
15740 79 0a 2a 2a 20 63 61 6c 6c 65 64 20 69 6e 20 72  y.** called in r
15750 65 73 70 6f 6e 73 65 20 74 6f 20 61 20 75 73 65  esponse to a use
15760 72 20 61 63 74 69 6f 6e 20 73 75 63 68 20 61 73  r action such as
15770 20 70 72 65 73 73 69 6e 67 20 22 43 61 6e 63 65   pressing "Cance
15780 6c 22 0a 2a 2a 20 6f 72 20 43 74 72 6c 2d 43 20  l".** or Ctrl-C 
15790 77 68 65 72 65 20 74 68 65 20 75 73 65 72 20 77  where the user w
157a0 61 6e 74 73 20 61 20 6c 6f 6e 67 20 71 75 65 72  ants a long quer
157b0 79 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 68  y operation to h
157c0 61 6c 74 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65  alt.** immediate
157d0 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20  ly..**.** It is 
157e0 73 61 66 65 20 74 6f 20 63 61 6c 6c 20 74 68 69  safe to call thi
157f0 73 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d 20 61  s routine from a
15800 20 74 68 72 65 61 64 20 64 69 66 66 65 72 65 6e   thread differen
15810 74 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 74 68  t from the.** th
15820 72 65 61 64 20 74 68 61 74 20 69 73 20 63 75 72  read that is cur
15830 72 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 20 74  rently running t
15840 68 65 20 64 61 74 61 62 61 73 65 20 6f 70 65 72  he database oper
15850 61 74 69 6f 6e 2e 20 20 42 75 74 20 69 74 0a 2a  ation.  But it.*
15860 2a 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f  * is not safe to
15870 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69   call this routi
15880 6e 65 20 77 69 74 68 20 61 20 5b 64 61 74 61 62  ne with a [datab
15890 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
158a0 74 68 61 74 0a 2a 2a 20 69 73 20 63 6c 6f 73 65  that.** is close
158b0 64 20 6f 72 20 6d 69 67 68 74 20 63 6c 6f 73 65  d or might close
158c0 20 62 65 66 6f 72 65 20 73 71 6c 69 74 65 33 5f   before sqlite3_
158d0 69 6e 74 65 72 72 75 70 74 28 29 20 72 65 74 75  interrupt() retu
158e0 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  rns..**.** If an
158f0 20 53 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 69   SQL operation i
15900 73 20 76 65 72 79 20 6e 65 61 72 6c 79 20 66 69  s very nearly fi
15910 6e 69 73 68 65 64 20 61 74 20 74 68 65 20 74 69  nished at the ti
15920 6d 65 20 77 68 65 6e 0a 2a 2a 20 73 71 6c 69 74  me when.** sqlit
15930 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 69  e3_interrupt() i
15940 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 69  s called, then i
15950 74 20 6d 69 67 68 74 20 6e 6f 74 20 68 61 76 65  t might not have
15960 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 0a   an opportunity.
15970 2a 2a 20 74 6f 20 62 65 20 69 6e 74 65 72 72 75  ** to be interru
15980 70 74 65 64 20 61 6e 64 20 6d 69 67 68 74 20 63  pted and might c
15990 6f 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6d 70 6c  ontinue to compl
159a0 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20  etion..**.** An 
159b0 53 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 74 68  SQL operation th
159c0 61 74 20 69 73 20 69 6e 74 65 72 72 75 70 74 65  at is interrupte
159d0 64 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 5b 53  d will return [S
159e0 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d  QLITE_INTERRUPT]
159f0 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 74 65  ..** If the inte
15a00 72 72 75 70 74 65 64 20 53 51 4c 20 6f 70 65 72  rrupted SQL oper
15a10 61 74 69 6f 6e 20 69 73 20 61 6e 20 49 4e 53 45  ation is an INSE
15a20 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44  RT, UPDATE, or D
15a30 45 4c 45 54 45 0a 2a 2a 20 74 68 61 74 20 69 73  ELETE.** that is
15a40 20 69 6e 73 69 64 65 20 61 6e 20 65 78 70 6c 69   inside an expli
15a50 63 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  cit transaction,
15a60 20 74 68 65 6e 20 74 68 65 20 65 6e 74 69 72 65   then the entire
15a70 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20   transaction.** 
15a80 77 69 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 20 62  will be rolled b
15a90 61 63 6b 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  ack automaticall
15aa0 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20  y..**.** A call 
15ab0 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  to sqlite3_inter
15ac0 72 75 70 74 28 29 20 68 61 73 20 6e 6f 20 65 66  rupt() has no ef
15ad0 66 65 63 74 20 6f 6e 20 53 51 4c 20 73 74 61 74  fect on SQL stat
15ae0 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61  ements.** that a
15af0 72 65 20 73 74 61 72 74 65 64 20 61 66 74 65 72  re started after
15b00 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
15b10 70 74 28 29 20 72 65 74 75 72 6e 73 2e 0a 2a 2a  pt() returns..**
15b20 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a  .** INVARIANTS:.
15b30 2a 2a 0a 2a 2a 20 7b 48 31 32 32 37 31 7d 20 54  **.** {H12271} T
15b40 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74 65  he [sqlite3_inte
15b50 72 72 75 70 74 28 29 5d 20 69 6e 74 65 72 66 61  rrupt()] interfa
15b60 63 65 20 77 69 6c 6c 20 66 6f 72 63 65 20 61 6c  ce will force al
15b70 6c 20 72 75 6e 6e 69 6e 67 0a 2a 2a 20 20 20 20  l running.**    
15b80 20 20 20 20 20 20 53 51 4c 20 73 74 61 74 65 6d        SQL statem
15b90 65 6e 74 73 20 61 73 73 6f 63 69 61 74 65 64 20  ents associated 
15ba0 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 64 61  with the same da
15bb0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
15bc0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f  n.**          to
15bd0 20 68 61 6c 74 20 61 66 74 65 72 20 70 72 6f 63   halt after proc
15be0 65 73 73 69 6e 67 20 61 74 20 6d 6f 73 74 20 6f  essing at most o
15bf0 6e 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 72 6f  ne additional ro
15c00 77 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a  w of data..**.**
15c10 20 7b 48 31 32 32 37 32 7d 20 41 6e 79 20 53 51   {H12272} Any SQ
15c20 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  L statement that
15c30 20 69 73 20 69 6e 74 65 72 72 75 70 74 65 64 20   is interrupted 
15c40 62 79 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74 65  by [sqlite3_inte
15c50 72 72 75 70 74 28 29 5d 0a 2a 2a 20 20 20 20 20  rrupt()].**     
15c60 20 20 20 20 20 77 69 6c 6c 20 72 65 74 75 72 6e       will return
15c70 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55   [SQLITE_INTERRU
15c80 50 54 5d 2e 0a 2a 2a 0a 2a 2a 20 41 53 53 55 4d  PT]..**.** ASSUM
15c90 50 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41  PTIONS:.**.** {A
15ca0 31 32 32 37 39 7d 20 49 66 20 74 68 65 20 64 61  12279} If the da
15cb0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
15cc0 6e 20 63 6c 6f 73 65 73 20 77 68 69 6c 65 20 5b  n closes while [
15cd0 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
15ce0 74 28 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20  t()].**         
15cf0 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e   is running then
15d00 20 62 61 64 20 74 68 69 6e 67 73 20 77 69 6c 6c   bad things will
15d10 20 6c 69 6b 65 6c 79 20 68 61 70 70 65 6e 2e 0a   likely happen..
15d20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
15d30 69 64 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  id sqlite3_inter
15d40 72 75 70 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a  rupt(sqlite3*);.
15d50 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
15d60 20 44 65 74 65 72 6d 69 6e 65 20 49 66 20 41 6e   Determine If An
15d70 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 49   SQL Statement I
15d80 73 20 43 6f 6d 70 6c 65 74 65 20 7b 48 31 30 35  s Complete {H105
15d90 31 30 7d 20 3c 53 37 30 32 30 30 3e 0a 2a 2a 0a  10} <S70200>.**.
15da0 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
15db0 73 20 61 72 65 20 75 73 65 66 75 6c 20 66 6f 72  s are useful for
15dc0 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 69 6e   command-line in
15dd0 70 75 74 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  put to determine
15de0 20 69 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65   if the.** curre
15df0 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 74 65 78  ntly entered tex
15e00 74 20 73 65 65 6d 73 20 74 6f 20 66 6f 72 6d 20  t seems to form 
15e10 63 6f 6d 70 6c 65 74 65 20 61 20 53 51 4c 20 73  complete a SQL s
15e20 74 61 74 65 6d 65 6e 74 20 6f 72 0a 2a 2a 20 69  tatement or.** i
15e30 66 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 70  f additional inp
15e40 75 74 20 69 73 20 6e 65 65 64 65 64 20 62 65 66  ut is needed bef
15e50 6f 72 65 20 73 65 6e 64 69 6e 67 20 74 68 65 20  ore sending the 
15e60 74 65 78 74 20 69 6e 74 6f 0a 2a 2a 20 53 51 4c  text into.** SQL
15e70 69 74 65 20 66 6f 72 20 70 61 72 73 69 6e 67 2e  ite for parsing.
15e80 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73    These routines
15e90 20 72 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   return true if 
15ea0 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67  the input string
15eb0 0a 2a 2a 20 61 70 70 65 61 72 73 20 74 6f 20 62  .** appears to b
15ec0 65 20 61 20 63 6f 6d 70 6c 65 74 65 20 53 51 4c  e a complete SQL
15ed0 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41 20 73   statement.  A s
15ee0 74 61 74 65 6d 65 6e 74 20 69 73 20 6a 75 64 67  tatement is judg
15ef0 65 64 20 74 6f 20 62 65 0a 2a 2a 20 63 6f 6d 70  ed to be.** comp
15f00 6c 65 74 65 20 69 66 20 69 74 20 65 6e 64 73 20  lete if it ends 
15f10 77 69 74 68 20 61 20 73 65 6d 69 63 6f 6c 6f 6e  with a semicolon
15f20 20 74 6f 6b 65 6e 20 61 6e 64 20 69 73 20 6e 6f   token and is no
15f30 74 20 61 20 66 72 61 67 6d 65 6e 74 20 6f 66 20  t a fragment of 
15f40 61 0a 2a 2a 20 43 52 45 41 54 45 20 54 52 49 47  a.** CREATE TRIG
15f50 47 45 52 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  GER statement.  
15f60 53 65 6d 69 63 6f 6c 6f 6e 73 20 74 68 61 74 20  Semicolons that 
15f70 61 72 65 20 65 6d 62 65 64 64 65 64 20 77 69 74  are embedded wit
15f80 68 69 6e 0a 2a 2a 20 73 74 72 69 6e 67 20 6c 69  hin.** string li
15f90 74 65 72 61 6c 73 20 6f 72 20 71 75 6f 74 65 64  terals or quoted
15fa0 20 69 64 65 6e 74 69 66 69 65 72 20 6e 61 6d 65   identifier name
15fb0 73 20 6f 72 20 63 6f 6d 6d 65 6e 74 73 20 61 72  s or comments ar
15fc0 65 20 6e 6f 74 0a 2a 2a 20 69 6e 64 65 70 65 6e  e not.** indepen
15fd0 64 65 6e 74 20 74 6f 6b 65 6e 73 20 28 74 68 65  dent tokens (the
15fe0 79 20 61 72 65 20 70 61 72 74 20 6f 66 20 74 68  y are part of th
15ff0 65 20 74 6f 6b 65 6e 20 69 6e 20 77 68 69 63 68  e token in which
16000 20 74 68 65 79 20 61 72 65 0a 2a 2a 20 65 6d 62   they are.** emb
16010 65 64 64 65 64 29 20 61 6e 64 20 74 68 75 73 20  edded) and thus 
16020 64 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73 20  do not count as 
16030 61 20 73 74 61 74 65 6d 65 6e 74 20 74 65 72 6d  a statement term
16040 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  inator..**.** Th
16050 65 73 65 20 72 6f 75 74 69 6e 65 73 20 64 6f 20  ese routines do 
16060 6e 6f 74 20 70 61 72 73 65 20 74 68 65 20 53 51  not parse the SQ
16070 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68 75  L statements thu
16080 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 64 65  s.** will not de
16090 74 65 63 74 20 73 79 6e 74 61 63 74 69 63 61 6c  tect syntactical
160a0 6c 79 20 69 6e 63 6f 72 72 65 63 74 20 53 51 4c  ly incorrect SQL
160b0 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e  ..**.** INVARIAN
160c0 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 35 31  TS:.**.** {H1051
160d0 31 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20  1} A successful 
160e0 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 5b 73  evaluation of [s
160f0 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28  qlite3_complete(
16100 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20  )] or.**        
16110 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c    [sqlite3_compl
16120 65 74 65 31 36 28 29 5d 20 66 75 6e 63 74 69 6f  ete16()] functio
16130 6e 73 20 73 68 61 6c 6c 0a 2a 2a 20 20 20 20 20  ns shall.**     
16140 20 20 20 20 20 72 65 74 75 72 6e 20 61 20 6e 75       return a nu
16150 6d 65 72 69 63 20 31 20 69 66 20 61 6e 64 20 6f  meric 1 if and o
16160 6e 6c 79 20 69 66 20 74 68 65 20 6c 61 73 74 20  nly if the last 
16170 6e 6f 6e 2d 77 68 69 74 65 73 70 61 63 65 0a 2a  non-whitespace.*
16180 2a 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e  *          token
16190 20 69 6e 20 74 68 65 69 72 20 69 6e 70 75 74 20   in their input 
161a0 69 73 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 20 74  is a semicolon t
161b0 68 61 74 20 69 73 20 6e 6f 74 20 69 6e 20 62 65  hat is not in be
161c0 74 77 65 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20  tween.**        
161d0 20 20 74 68 65 20 42 45 47 49 4e 20 61 6e 64 20    the BEGIN and 
161e0 45 4e 44 20 6f 66 20 61 20 43 52 45 41 54 45 20  END of a CREATE 
161f0 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e  TRIGGER statemen
16200 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 35 31 32  t..**.** {H10512
16210 7d 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c  } If a memory al
16220 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f  location error o
16230 63 63 75 72 73 20 64 75 72 69 6e 67 20 61 6e 20  ccurs during an 
16240 69 6e 76 6f 63 61 74 69 6f 6e 0a 2a 2a 20 20 20  invocation.**   
16250 20 20 20 20 20 20 20 6f 66 20 5b 73 71 6c 69 74         of [sqlit
16260 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29 5d 20 6f  e3_complete()] o
16270 72 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  r [sqlite3_compl
16280 65 74 65 31 36 28 29 5d 20 74 68 65 6e 20 74 68  ete16()] then th
16290 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 6f  e.**          ro
162a0 75 74 69 6e 65 20 73 68 61 6c 6c 20 72 65 74 75  utine shall retu
162b0 72 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rn [SQLITE_NOMEM
162c0 5d 2e 0a 2a 2a 0a 2a 2a 20 41 53 53 55 4d 50 54  ]..**.** ASSUMPT
162d0 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31 30  IONS:.**.** {A10
162e0 35 31 32 7d 20 54 68 65 20 69 6e 70 75 74 20 74  512} The input t
162f0 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  o [sqlite3_compl
16300 65 74 65 28 29 5d 20 6d 75 73 74 20 62 65 20 61  ete()] must be a
16310 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
16320 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 55 54 46  .**          UTF
16330 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a  -8 string..**.**
16340 20 7b 41 31 30 35 31 33 7d 20 54 68 65 20 69 6e   {A10513} The in
16350 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  put to [sqlite3_
16360 63 6f 6d 70 6c 65 74 65 31 36 28 29 5d 20 6d 75  complete16()] mu
16370 73 74 20 62 65 20 61 20 7a 65 72 6f 2d 74 65 72  st be a zero-ter
16380 6d 69 6e 61 74 65 64 0a 2a 2a 20 20 20 20 20 20  minated.**      
16390 20 20 20 20 55 54 46 2d 31 36 20 73 74 72 69 6e      UTF-16 strin
163a0 67 20 69 6e 20 6e 61 74 69 76 65 20 62 79 74 65  g in native byte
163b0 20 6f 72 64 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54   order..*/.SQLIT
163c0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
163d0 33 5f 63 6f 6d 70 6c 65 74 65 28 63 6f 6e 73 74  3_complete(const
163e0 20 63 68 61 72 20 2a 73 71 6c 29 3b 0a 53 51 4c   char *sql);.SQL
163f0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
16400 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 63  te3_complete16(c
16410 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 29 3b  onst void *sql);
16420 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
16430 3a 20 52 65 67 69 73 74 65 72 20 41 20 43 61 6c  : Register A Cal
16440 6c 62 61 63 6b 20 54 6f 20 48 61 6e 64 6c 65 20  lback To Handle 
16450 53 51 4c 49 54 45 5f 42 55 53 59 20 45 72 72 6f  SQLITE_BUSY Erro
16460 72 73 20 7b 48 31 32 33 31 30 7d 20 3c 53 34 30  rs {H12310} <S40
16470 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  400>.**.** This 
16480 72 6f 75 74 69 6e 65 20 73 65 74 73 20 61 20 63  routine sets a c
16490 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
164a0 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 69   that might be i
164b0 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 0a  nvoked whenever.
164c0 2a 2a 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73  ** an attempt is
164d0 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e 20 61 20   made to open a 
164e0 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 74  database table t
164f0 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68 72 65  hat another thre
16500 61 64 0a 2a 2a 20 6f 72 20 70 72 6f 63 65 73 73  ad.** or process
16510 20 68 61 73 20 6c 6f 63 6b 65 64 2e 0a 2a 2a 0a   has locked..**.
16520 2a 2a 20 49 66 20 74 68 65 20 62 75 73 79 20 63  ** If the busy c
16530 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55 4c 4c 2c  allback is NULL,
16540 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f 42 55   then [SQLITE_BU
16550 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49  SY] or [SQLITE_I
16560 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 0a 2a 2a  OERR_BLOCKED].**
16570 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6d 6d   is returned imm
16580 65 64 69 61 74 65 6c 79 20 75 70 6f 6e 20 65 6e  ediately upon en
16590 63 6f 75 6e 74 65 72 69 6e 67 20 74 68 65 20 6c  countering the l
165a0 6f 63 6b 2e 20 49 66 20 74 68 65 20 62 75 73 79  ock. If the busy
165b0 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20   callback.** is 
165c0 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74  not NULL, then t
165d0 68 65 20 63 61 6c 6c 62 61 63 6b 20 77 69 6c 6c  he callback will
165e0 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 69 74 68   be invoked with
165f0 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e 0a   two arguments..
16600 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  **.** The first 
16610 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
16620 68 61 6e 64 6c 65 72 20 69 73 20 61 20 63 6f 70  handler is a cop
16630 79 20 6f 66 20 74 68 65 20 76 6f 69 64 2a 20 70  y of the void* p
16640 6f 69 6e 74 65 72 20 77 68 69 63 68 0a 2a 2a 20  ointer which.** 
16650 69 73 20 74 68 65 20 74 68 69 72 64 20 61 72 67  is the third arg
16660 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33  ument to sqlite3
16670 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 2e  _busy_handler().
16680 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67    The second arg
16690 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20  ument to.** the 
166a0 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b  handler callback
166b0 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
166c0 66 20 74 69 6d 65 73 20 74 68 61 74 20 74 68 65  f times that the
166d0 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 68 61   busy handler ha
166e0 73 0a 2a 2a 20 62 65 65 6e 20 69 6e 76 6f 6b 65  s.** been invoke
166f0 64 20 66 6f 72 20 74 68 69 73 20 6c 6f 63 6b 69  d for this locki
16700 6e 67 20 65 76 65 6e 74 2e 20 20 49 66 20 74 68  ng event.  If th
16710 65 0a 2a 2a 20 62 75 73 79 20 63 61 6c 6c 62 61  e.** busy callba
16720 63 6b 20 72 65 74 75 72 6e 73 20 30 2c 20 74 68  ck returns 0, th
16730 65 6e 20 6e 6f 20 61 64 64 69 74 69 6f 6e 61 6c  en no additional
16740 20 61 74 74 65 6d 70 74 73 20 61 72 65 20 6d 61   attempts are ma
16750 64 65 20 74 6f 0a 2a 2a 20 61 63 63 65 73 73 20  de to.** access 
16760 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e 64  the database and
16770 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f   [SQLITE_BUSY] o
16780 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  r [SQLITE_IOERR_
16790 42 4c 4f 43 4b 45 44 5d 20 69 73 20 72 65 74 75  BLOCKED] is retu
167a0 72 6e 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20  rned..** If the 
167b0 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73  callback returns
167c0 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20   non-zero, then 
167d0 61 6e 6f 74 68 65 72 20 61 74 74 65 6d 70 74 0a  another attempt.
167e0 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 70  ** is made to op
167f0 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  en the database 
16800 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64 20  for reading and 
16810 74 68 65 20 63 79 63 6c 65 20 72 65 70 65 61 74  the cycle repeat
16820 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 65  s..**.** The pre
16830 73 65 6e 63 65 20 6f 66 20 61 20 62 75 73 79 20  sence of a busy 
16840 68 61 6e 64 6c 65 72 20 64 6f 65 73 20 6e 6f 74  handler does not
16850 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20   guarantee that 
16860 69 74 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b  it will be invok
16870 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 72 65  ed.** when there
16880 20 69 73 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74   is lock content
16890 69 6f 6e 2e 20 49 66 20 53 51 4c 69 74 65 20 64  ion. If SQLite d
168a0 65 74 65 72 6d 69 6e 65 73 20 74 68 61 74 20 69  etermines that i
168b0 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 79  nvoking the busy
168c0 0a 2a 2a 20 68 61 6e 64 6c 65 72 20 63 6f 75 6c  .** handler coul
168d0 64 20 72 65 73 75 6c 74 20 69 6e 20 61 20 64 65  d result in a de
168e0 61 64 6c 6f 63 6b 2c 20 69 74 20 77 69 6c 6c 20  adlock, it will 
168f0 67 6f 20 61 68 65 61 64 20 61 6e 64 20 72 65 74  go ahead and ret
16900 75 72 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 59  urn [SQLITE_BUSY
16910 5d 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 45 5f  ].** or [SQLITE_
16920 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 20 69  IOERR_BLOCKED] i
16930 6e 73 74 65 61 64 20 6f 66 20 69 6e 76 6f 6b 69  nstead of invoki
16940 6e 67 20 74 68 65 20 62 75 73 79 20 68 61 6e 64  ng the busy hand
16950 6c 65 72 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72  ler..** Consider
16960 20 61 20 73 63 65 6e 61 72 69 6f 20 77 68 65 72   a scenario wher
16970 65 20 6f 6e 65 20 70 72 6f 63 65 73 73 20 69 73  e one process is
16980 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 61 64 20   holding a read 
16990 6c 6f 63 6b 20 74 68 61 74 0a 2a 2a 20 69 74 20  lock that.** it 
169a0 69 73 20 74 72 79 69 6e 67 20 74 6f 20 70 72 6f  is trying to pro
169b0 6d 6f 74 65 20 74 6f 20 61 20 72 65 73 65 72 76  mote to a reserv
169c0 65 64 20 6c 6f 63 6b 20 61 6e 64 0a 2a 2a 20 61  ed lock and.** a
169d0 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 20   second process 
169e0 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 73  is holding a res
169f0 65 72 76 65 64 20 6c 6f 63 6b 20 74 68 61 74 20  erved lock that 
16a00 69 74 20 69 73 20 74 72 79 69 6e 67 0a 2a 2a 20  it is trying.** 
16a10 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 61 6e  to promote to an
16a20 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e   exclusive lock.
16a30 20 20 54 68 65 20 66 69 72 73 74 20 70 72 6f 63    The first proc
16a40 65 73 73 20 63 61 6e 6e 6f 74 20 70 72 6f 63 65  ess cannot proce
16a50 65 64 0a 2a 2a 20 62 65 63 61 75 73 65 20 69 74  ed.** because it
16a60 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 74   is blocked by t
16a70 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 74 68  he second and th
16a80 65 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 73  e second process
16a90 20 63 61 6e 6e 6f 74 0a 2a 2a 20 70 72 6f 63 65   cannot.** proce
16aa0 65 64 20 62 65 63 61 75 73 65 20 69 74 20 69 73  ed because it is
16ab0 20 62 6c 6f 63 6b 65 64 20 62 79 20 74 68 65 20   blocked by the 
16ac0 66 69 72 73 74 2e 20 20 49 66 20 62 6f 74 68 20  first.  If both 
16ad0 70 72 6f 63 65 73 73 65 73 0a 2a 2a 20 69 6e 76  processes.** inv
16ae0 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 61 6e  oke the busy han
16af0 64 6c 65 72 73 2c 20 6e 65 69 74 68 65 72 20 77  dlers, neither w
16b00 69 6c 6c 20 6d 61 6b 65 20 61 6e 79 20 70 72 6f  ill make any pro
16b10 67 72 65 73 73 2e 20 20 54 68 65 72 65 66 6f 72  gress.  Therefor
16b20 65 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 72 65 74  e,.** SQLite ret
16b30 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 42 55 53  urns [SQLITE_BUS
16b40 59 5d 20 66 6f 72 20 74 68 65 20 66 69 72 73 74  Y] for the first
16b50 20 70 72 6f 63 65 73 73 2c 20 68 6f 70 69 6e 67   process, hoping
16b60 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 77 69   that this.** wi
16b70 6c 6c 20 69 6e 64 75 63 65 20 74 68 65 20 66 69  ll induce the fi
16b80 72 73 74 20 70 72 6f 63 65 73 73 20 74 6f 20 72  rst process to r
16b90 65 6c 65 61 73 65 20 69 74 73 20 72 65 61 64 20  elease its read 
16ba0 6c 6f 63 6b 20 61 6e 64 20 61 6c 6c 6f 77 0a 2a  lock and allow.*
16bb0 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72 6f  * the second pro
16bc0 63 65 73 73 20 74 6f 20 70 72 6f 63 65 65 64 2e  cess to proceed.
16bd0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75  .**.** The defau
16be0 6c 74 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b  lt busy callback
16bf0 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20   is NULL..**.** 
16c00 54 68 65 20 5b 53 51 4c 49 54 45 5f 42 55 53 59  The [SQLITE_BUSY
16c10 5d 20 65 72 72 6f 72 20 69 73 20 63 6f 6e 76 65  ] error is conve
16c20 72 74 65 64 20 74 6f 20 5b 53 51 4c 49 54 45 5f  rted to [SQLITE_
16c30 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 0a 2a  IOERR_BLOCKED].*
16c40 2a 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73  * when SQLite is
16c50 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
16c60 66 20 61 20 6c 61 72 67 65 20 74 72 61 6e 73 61  f a large transa
16c70 63 74 69 6f 6e 20 77 68 65 72 65 20 61 6c 6c 20  ction where all 
16c80 74 68 65 0a 2a 2a 20 63 68 61 6e 67 65 73 20 77  the.** changes w
16c90 69 6c 6c 20 6e 6f 74 20 66 69 74 20 69 6e 74 6f  ill not fit into
16ca0 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63   the in-memory c
16cb0 61 63 68 65 2e 20 20 53 51 4c 69 74 65 20 77 69  ache.  SQLite wi
16cc0 6c 6c 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 6f  ll.** already ho
16cd0 6c 64 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  ld a RESERVED lo
16ce0 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ck on the databa
16cf0 73 65 20 66 69 6c 65 2c 20 62 75 74 20 69 74 20  se file, but it 
16d00 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 70 72 6f 6d  needs.** to prom
16d10 6f 74 65 20 74 68 69 73 20 6c 6f 63 6b 20 74 6f  ote this lock to
16d20 20 45 58 43 4c 55 53 49 56 45 20 73 6f 20 74 68   EXCLUSIVE so th
16d30 61 74 20 69 74 20 63 61 6e 20 73 70 69 6c 6c 20  at it can spill 
16d40 63 61 63 68 65 0a 2a 2a 20 70 61 67 65 73 20 69  cache.** pages i
16d50 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65  nto the database
16d60 20 66 69 6c 65 20 77 69 74 68 6f 75 74 20 68 61   file without ha
16d70 72 6d 20 74 6f 20 63 6f 6e 63 75 72 72 65 6e 74  rm to concurrent
16d80 0a 2a 2a 20 72 65 61 64 65 72 73 2e 20 20 49 66  .** readers.  If
16d90 20 69 74 20 69 73 20 75 6e 61 62 6c 65 20 74 6f   it is unable to
16da0 20 70 72 6f 6d 6f 74 65 20 74 68 65 20 6c 6f 63   promote the loc
16db0 6b 2c 20 74 68 65 6e 20 74 68 65 20 69 6e 2d 6d  k, then the in-m
16dc0 65 6d 6f 72 79 0a 2a 2a 20 63 61 63 68 65 20 77  emory.** cache w
16dd0 69 6c 6c 20 62 65 20 6c 65 66 74 20 69 6e 20 61  ill be left in a
16de0 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73  n inconsistent s
16df0 74 61 74 65 20 61 6e 64 20 73 6f 20 74 68 65 20  tate and so the 
16e00 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 73  error.** code is
16e10 20 70 72 6f 6d 6f 74 65 64 20 66 72 6f 6d 20 74   promoted from t
16e20 68 65 20 72 65 6c 61 74 69 76 65 6c 79 20 62 65  he relatively be
16e30 6e 69 67 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53  nign [SQLITE_BUS
16e40 59 5d 20 74 6f 0a 2a 2a 20 74 68 65 20 6d 6f 72  Y] to.** the mor
16e50 65 20 73 65 76 65 72 65 20 5b 53 51 4c 49 54 45  e severe [SQLITE
16e60 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 2e  _IOERR_BLOCKED].
16e70 20 20 54 68 69 73 20 65 72 72 6f 72 20 63 6f 64    This error cod
16e80 65 20 70 72 6f 6d 6f 74 69 6f 6e 0a 2a 2a 20 66  e promotion.** f
16e90 6f 72 63 65 73 20 61 6e 20 61 75 74 6f 6d 61 74  orces an automat
16ea0 69 63 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 74  ic rollback of t
16eb0 68 65 20 63 68 61 6e 67 65 73 2e 20 20 53 65 65  he changes.  See
16ec0 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 65 66 3d   the.** <a href=
16ed0 22 2f 63 76 73 74 72 61 63 2f 77 69 6b 69 3f 70  "/cvstrac/wiki?p
16ee0 3d 43 6f 72 72 75 70 74 69 6f 6e 46 6f 6c 6c 6f  =CorruptionFollo
16ef0 77 69 6e 67 42 75 73 79 45 72 72 6f 72 22 3e 0a  wingBusyError">.
16f00 2a 2a 20 43 6f 72 72 75 70 74 69 6f 6e 46 6f 6c  ** CorruptionFol
16f10 6c 6f 77 69 6e 67 42 75 73 79 45 72 72 6f 72 3c  lowingBusyError<
16f20 2f 61 3e 20 77 69 6b 69 20 70 61 67 65 20 66 6f  /a> wiki page fo
16f30 72 20 61 20 64 69 73 63 75 73 73 69 6f 6e 20 6f  r a discussion o
16f40 66 20 77 68 79 0a 2a 2a 20 74 68 69 73 20 69 73  f why.** this is
16f50 20 69 6d 70 6f 72 74 61 6e 74 2e 0a 2a 2a 0a 2a   important..**.*
16f60 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79  * There can only
16f70 20 62 65 20 61 20 73 69 6e 67 6c 65 20 62 75 73   be a single bus
16f80 79 20 68 61 6e 64 6c 65 72 20 64 65 66 69 6e 65  y handler define
16f90 64 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 5b 64  d for each.** [d
16fa0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
16fb0 6f 6e 5d 2e 20 20 53 65 74 74 69 6e 67 20 61 20  on].  Setting a 
16fc0 6e 65 77 20 62 75 73 79 20 68 61 6e 64 6c 65 72  new busy handler
16fd0 20 63 6c 65 61 72 73 20 61 6e 79 0a 2a 2a 20 70   clears any.** p
16fe0 72 65 76 69 6f 75 73 6c 79 20 73 65 74 20 68 61  reviously set ha
16ff0 6e 64 6c 65 72 2e 20 20 4e 6f 74 65 20 74 68 61  ndler.  Note tha
17000 74 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74  t calling [sqlit
17010 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28  e3_busy_timeout(
17020 29 5d 0a 2a 2a 20 77 69 6c 6c 20 61 6c 73 6f 20  )].** will also 
17030 73 65 74 20 6f 72 20 63 6c 65 61 72 20 74 68 65  set or clear the
17040 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a 2a   busy handler..*
17050 2a 0a 2a 2a 20 54 68 65 20 62 75 73 79 20 63 61  *.** The busy ca
17060 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 20 6e 6f  llback should no
17070 74 20 74 61 6b 65 20 61 6e 79 20 61 63 74 69 6f  t take any actio
17080 6e 73 20 77 68 69 63 68 20 6d 6f 64 69 66 79 20  ns which modify 
17090 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
170a0 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20  connection that 
170b0 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75 73 79  invoked the busy
170c0 20 68 61 6e 64 6c 65 72 2e 20 20 41 6e 79 20 73   handler.  Any s
170d0 75 63 68 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 72  uch actions.** r
170e0 65 73 75 6c 74 20 69 6e 20 75 6e 64 65 66 69 6e  esult in undefin
170f0 65 64 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 20  ed behavior..** 
17100 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a  .** INVARIANTS:.
17110 2a 2a 0a 2a 2a 20 7b 48 31 32 33 31 31 7d 20 54  **.** {H12311} T
17120 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79  he [sqlite3_busy
17130 5f 68 61 6e 64 6c 65 72 28 44 2c 43 2c 41 29 5d  _handler(D,C,A)]
17140 20 66 75 6e 63 74 69 6f 6e 20 73 68 61 6c 6c 20   function shall 
17150 72 65 70 6c 61 63 65 0a 2a 2a 20 20 20 20 20 20  replace.**      
17160 20 20 20 20 62 75 73 79 20 63 61 6c 6c 62 61 63      busy callbac
17170 6b 20 69 6e 20 74 68 65 20 5b 64 61 74 61 62 61  k in the [databa
17180 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44  se connection] D
17190 20 77 69 74 68 20 61 20 6e 65 77 0a 2a 2a 20 20   with a new.**  
171a0 20 20 20 20 20 20 20 20 61 20 6e 65 77 20 62 75          a new bu
171b0 73 79 20 68 61 6e 64 6c 65 72 20 43 20 61 6e 64  sy handler C and
171c0 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 61 74   application dat
171d0 61 20 70 6f 69 6e 74 65 72 20 41 2e 0a 2a 2a 0a  a pointer A..**.
171e0 2a 2a 20 7b 48 31 32 33 31 32 7d 20 4e 65 77 6c  ** {H12312} Newl
171f0 79 20 63 72 65 61 74 65 64 20 5b 64 61 74 61 62  y created [datab
17200 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d  ase connections]
17210 20 73 68 61 6c 6c 20 68 61 76 65 20 61 20 62 75   shall have a bu
17220 73 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 68  sy.**          h
17230 61 6e 64 6c 65 72 20 6f 66 20 4e 55 4c 4c 2e 0a  andler of NULL..
17240 2a 2a 0a 2a 2a 20 7b 48 31 32 33 31 34 7d 20 57  **.** {H12314} W
17250 68 65 6e 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20  hen two or more 
17260 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
17270 74 69 6f 6e 73 5d 20 73 68 61 72 65 20 61 0a 2a  tions] share a.*
17280 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
17290 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65  te3_enable_share
172a0 64 5f 63 61 63 68 65 20 7c 20 63 6f 6d 6d 6f 6e  d_cache | common
172b0 20 63 61 63 68 65 5d 2c 0a 2a 2a 20 20 20 20 20   cache],.**     
172c0 20 20 20 20 20 74 68 65 20 62 75 73 79 20 68 61       the busy ha
172d0 6e 64 6c 65 72 20 66 6f 72 20 74 68 65 20 64 61  ndler for the da
172e0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
172f0 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 73 69 6e  n currently usin
17300 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  g.**          th
17310 65 20 63 61 63 68 65 20 73 68 61 6c 6c 20 62 65  e cache shall be
17320 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 74 68   invoked when th
17330 65 20 63 61 63 68 65 20 65 6e 63 6f 75 6e 74 65  e cache encounte
17340 72 73 20 61 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a  rs a lock..**.**
17350 20 7b 48 31 32 33 31 36 7d 20 49 66 20 61 20 62   {H12316} If a b
17360 75 73 79 20 68 61 6e 64 6c 65 72 20 63 61 6c 6c  usy handler call
17370 62 61 63 6b 20 72 65 74 75 72 6e 73 20 7a 65 72  back returns zer
17380 6f 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c 69  o, then the SQLi
17390 74 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20  te interface.** 
173a0 20 20 20 20 20 20 20 20 20 74 68 61 74 20 70 72           that pr
173b0 6f 76 6f 6b 65 64 20 74 68 65 20 6c 6f 63 6b 69  ovoked the locki
173c0 6e 67 20 65 76 65 6e 74 20 73 68 61 6c 6c 20 72  ng event shall r
173d0 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 42 55  eturn [SQLITE_BU
173e0 53 59 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33  SY]..**.** {H123
173f0 31 38 7d 20 53 51 4c 69 74 65 20 73 68 61 6c 6c  18} SQLite shall
17400 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 62 75 73   invokes the bus
17410 79 20 68 61 6e 64 6c 65 72 20 77 69 74 68 20 74  y handler with t
17420 77 6f 20 61 72 67 75 6d 65 6e 74 73 20 77 68 69  wo arguments whi
17430 63 68 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61  ch.**          a
17440 72 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  re a copy of the
17450 20 70 6f 69 6e 74 65 72 20 73 75 70 70 6c 69 65   pointer supplie
17460 64 20 62 79 20 74 68 65 20 33 72 64 20 70 61 72  d by the 3rd par
17470 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 20 20 20  ameter to.**    
17480 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 62        [sqlite3_b
17490 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 5d 20 61  usy_handler()] a
174a0 6e 64 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68  nd a count of th
174b0 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 72 69 6f  e number of prio
174c0 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e  r.**          in
174d0 76 6f 63 61 74 69 6f 6e 73 20 6f 66 20 74 68 65  vocations of the
174e0 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66 6f   busy handler fo
174f0 72 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69  r the same locki
17500 6e 67 20 65 76 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  ng event..**.** 
17510 41 53 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a 2a 0a  ASSUMPTIONS:.**.
17520 2a 2a 20 7b 41 31 32 33 31 39 7d 20 41 20 62 75  ** {A12319} A bu
17530 73 79 20 68 61 6e 64 6c 65 72 20 6d 75 73 74 20  sy handler must 
17540 6e 6f 74 20 63 6c 6f 73 65 20 74 68 65 20 64 61  not close the da
17550 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
17560 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72  n.**          or
17570 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
17580 6d 65 6e 74 5d 20 74 68 61 74 20 69 6e 76 6f 6b  ment] that invok
17590 65 64 20 74 68 65 20 62 75 73 79 20 68 61 6e 64  ed the busy hand
175a0 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ler..*/.SQLITE_A
175b0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
175c0 75 73 79 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69  usy_handler(sqli
175d0 74 65 33 2a 2c 20 69 6e 74 28 2a 29 28 76 6f 69  te3*, int(*)(voi
175e0 64 2a 2c 69 6e 74 29 2c 20 76 6f 69 64 2a 29 3b  d*,int), void*);
175f0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
17600 3a 20 53 65 74 20 41 20 42 75 73 79 20 54 69 6d  : Set A Busy Tim
17610 65 6f 75 74 20 7b 48 31 32 33 34 30 7d 20 3c 53  eout {H12340} <S
17620 34 30 34 31 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69  40410>.**.** Thi
17630 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 61  s routine sets a
17640 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68   [sqlite3_busy_h
17650 61 6e 64 6c 65 72 20 7c 20 62 75 73 79 20 68 61  andler | busy ha
17660 6e 64 6c 65 72 5d 20 74 68 61 74 20 73 6c 65 65  ndler] that slee
17670 70 73 0a 2a 2a 20 66 6f 72 20 61 20 73 70 65 63  ps.** for a spec
17680 69 66 69 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20  ified amount of 
17690 74 69 6d 65 20 77 68 65 6e 20 61 20 74 61 62 6c  time when a tabl
176a0 65 20 69 73 20 6c 6f 63 6b 65 64 2e 20 20 54 68  e is locked.  Th
176b0 65 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 77 69 6c  e handler.** wil
176c0 6c 20 73 6c 65 65 70 20 6d 75 6c 74 69 70 6c 65  l sleep multiple
176d0 20 74 69 6d 65 73 20 75 6e 74 69 6c 20 61 74 20   times until at 
176e0 6c 65 61 73 74 20 22 6d 73 22 20 6d 69 6c 6c 69  least "ms" milli
176f0 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70  seconds of sleep
17700 69 6e 67 0a 2a 2a 20 68 61 76 65 20 61 63 63 75  ing.** have accu
17710 6d 75 6c 61 74 65 64 2e 20 7b 48 31 32 33 34 33  mulated. {H12343
17720 7d 20 41 66 74 65 72 20 22 6d 73 22 20 6d 69 6c  } After "ms" mil
17730 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65  liseconds of sle
17740 65 70 69 6e 67 2c 0a 2a 2a 20 74 68 65 20 68 61  eping,.** the ha
17750 6e 64 6c 65 72 20 72 65 74 75 72 6e 73 20 30 20  ndler returns 0 
17760 77 68 69 63 68 20 63 61 75 73 65 73 20 5b 73 71  which causes [sq
17770 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 74 6f  lite3_step()] to
17780 20 72 65 74 75 72 6e 0a 2a 2a 20 5b 53 51 4c 49   return.** [SQLI
17790 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c  TE_BUSY] or [SQL
177a0 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45  ITE_IOERR_BLOCKE
177b0 44 5d 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e  D]..**.** Callin
177c0 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  g this routine w
177d0 69 74 68 20 61 6e 20 61 72 67 75 6d 65 6e 74 20  ith an argument 
177e0 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75  less than or equ
177f0 61 6c 20 74 6f 20 7a 65 72 6f 0a 2a 2a 20 74 75  al to zero.** tu
17800 72 6e 73 20 6f 66 66 20 61 6c 6c 20 62 75 73 79  rns off all busy
17810 20 68 61 6e 64 6c 65 72 73 2e 0a 2a 2a 0a 2a 2a   handlers..**.**
17820 20 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20   There can only 
17830 62 65 20 61 20 73 69 6e 67 6c 65 20 62 75 73 79  be a single busy
17840 20 68 61 6e 64 6c 65 72 20 66 6f 72 20 61 20 70   handler for a p
17850 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 5b 64 61  articular.** [da
17860 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
17870 6e 5d 20 61 6e 79 20 61 6e 79 20 67 69 76 65 6e  n] any any given
17880 20 6d 6f 6d 65 6e 74 2e 20 20 49 66 20 61 6e 6f   moment.  If ano
17890 74 68 65 72 20 62 75 73 79 20 68 61 6e 64 6c 65  ther busy handle
178a0 72 0a 2a 2a 20 77 61 73 20 64 65 66 69 6e 65 64  r.** was defined
178b0 20 20 28 75 73 69 6e 67 20 5b 73 71 6c 69 74 65    (using [sqlite
178c0 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 29  3_busy_handler()
178d0 5d 29 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c  ]) prior to call
178e0 69 6e 67 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74  ing.** this rout
178f0 69 6e 65 2c 20 74 68 61 74 20 6f 74 68 65 72 20  ine, that other 
17900 62 75 73 79 20 68 61 6e 64 6c 65 72 20 69 73 20  busy handler is 
17910 63 6c 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  cleared..**.** I
17920 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a  NVARIANTS:.**.**
17930 20 7b 48 31 32 33 34 31 7d 20 54 68 65 20 5b 73   {H12341} The [s
17940 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65  qlite3_busy_time
17950 6f 75 74 28 29 5d 20 66 75 6e 63 74 69 6f 6e 20  out()] function 
17960 73 68 61 6c 6c 20 6f 76 65 72 72 69 64 65 20 61  shall override a
17970 6e 79 20 70 72 69 6f 72 0a 2a 2a 20 20 20 20 20  ny prior.**     
17980 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 62 75       [sqlite3_bu
17990 73 79 5f 74 69 6d 65 6f 75 74 28 29 5d 20 6f 72  sy_timeout()] or
179a0 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68   [sqlite3_busy_h
179b0 61 6e 64 6c 65 72 28 29 5d 20 73 65 74 74 69 6e  andler()] settin
179c0 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 6e  g.**          on
179d0 20 74 68 65 20 73 61 6d 65 20 5b 64 61 74 61 62   the same [datab
179e0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e  ase connection].
179f0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 34 33 7d 20  .**.** {H12343} 
17a00 49 66 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d  If the 2nd param
17a10 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33  eter to [sqlite3
17a20 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 29 5d  _busy_timeout()]
17a30 20 69 73 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a   is less than.**
17a40 20 20 20 20 20 20 20 20 20 20 6f 72 20 65 71 75            or equ
17a50 61 6c 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 6e  al to zero, then
17a60 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65   the busy handle
17a70 72 20 73 68 61 6c 6c 20 62 65 20 63 6c 65 61 72  r shall be clear
17a80 65 64 20 73 6f 20 74 68 61 74 0a 2a 2a 20 20 20  ed so that.**   
17a90 20 20 20 20 20 20 20 61 6c 6c 20 73 75 62 73 65         all subse
17aa0 71 75 65 6e 74 20 6c 6f 63 6b 69 6e 67 20 65 76  quent locking ev
17ab0 65 6e 74 73 20 69 6d 6d 65 64 69 61 74 65 6c 79  ents immediately
17ac0 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f   return [SQLITE_
17ad0 42 55 53 59 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  BUSY]..**.** {H1
17ae0 32 33 34 34 7d 20 49 66 20 74 68 65 20 32 6e 64  2344} If the 2nd
17af0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73   parameter to [s
17b00 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65  qlite3_busy_time
17b10 6f 75 74 28 29 5d 20 69 73 20 61 20 70 6f 73 69  out()] is a posi
17b20 74 69 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  tive.**         
17b30 20 6e 75 6d 62 65 72 20 4e 2c 20 74 68 65 6e 20   number N, then 
17b40 61 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 73  a busy handler s
17b50 68 61 6c 6c 20 62 65 20 73 65 74 20 74 68 61 74  hall be set that
17b60 20 72 65 70 65 61 74 65 64 6c 79 20 63 61 6c 6c   repeatedly call
17b70 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  s.**          th
17b80 65 20 78 53 6c 65 65 70 28 29 20 6d 65 74 68 6f  e xSleep() metho
17b90 64 20 69 6e 20 74 68 65 20 5b 73 71 6c 69 74 65  d in the [sqlite
17ba0 33 5f 76 66 73 20 7c 20 56 46 53 20 69 6e 74 65  3_vfs | VFS inte
17bb0 72 66 61 63 65 5d 20 75 6e 74 69 6c 0a 2a 2a 20  rface] until.** 
17bc0 20 20 20 20 20 20 20 20 20 65 69 74 68 65 72 20           either 
17bd0 74 68 65 20 6c 6f 63 6b 20 63 6c 65 61 72 73 20  the lock clears 
17be0 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 63 75 6d  or until the cum
17bf0 75 6c 61 74 69 76 65 20 73 6c 65 65 70 20 74 69  ulative sleep ti
17c00 6d 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72  me.**          r
17c10 65 70 6f 72 74 65 64 20 62 61 63 6b 20 62 79 20  eported back by 
17c20 78 53 6c 65 65 70 28 29 20 65 78 63 65 65 64 73  xSleep() exceeds
17c30 20 4e 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 2e   N milliseconds.
17c40 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
17c50 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f  nt sqlite3_busy_
17c60 74 69 6d 65 6f 75 74 28 73 71 6c 69 74 65 33 2a  timeout(sqlite3*
17c70 2c 20 69 6e 74 20 6d 73 29 3b 0a 0a 2f 2a 0a 2a  , int ms);../*.*
17c80 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 76  * CAPI3REF: Conv
17c90 65 6e 69 65 6e 63 65 20 52 6f 75 74 69 6e 65 73  enience Routines
17ca0 20 46 6f 72 20 52 75 6e 6e 69 6e 67 20 51 75 65   For Running Que
17cb0 72 69 65 73 20 7b 48 31 32 33 37 30 7d 20 3c 53  ries {H12370} <S
17cc0 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 44 65 66  10000>.**.** Def
17cd0 69 6e 69 74 69 6f 6e 3a 20 41 20 3c 62 3e 72 65  inition: A <b>re
17ce0 73 75 6c 74 20 74 61 62 6c 65 3c 2f 62 3e 20 69  sult table</b> i
17cf0 73 20 6d 65 6d 6f 72 79 20 64 61 74 61 20 73 74  s memory data st
17d00 72 75 63 74 75 72 65 20 63 72 65 61 74 65 64 20  ructure created 
17d10 62 79 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74  by the.** [sqlit
17d20 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 20  e3_get_table()] 
17d30 69 6e 74 65 72 66 61 63 65 2e 20 20 41 20 72 65  interface.  A re
17d40 73 75 6c 74 20 74 61 62 6c 65 20 72 65 63 6f 72  sult table recor
17d50 64 73 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65  ds the.** comple
17d60 74 65 20 71 75 65 72 79 20 72 65 73 75 6c 74 73  te query results
17d70 20 66 72 6f 6d 20 6f 6e 65 20 6f 72 20 6d 6f 72   from one or mor
17d80 65 20 71 75 65 72 69 65 73 2e 0a 2a 2a 0a 2a 2a  e queries..**.**
17d90 20 54 68 65 20 74 61 62 6c 65 20 63 6f 6e 63 65   The table conce
17da0 70 74 75 61 6c 6c 79 20 68 61 73 20 61 20 6e 75  ptually has a nu
17db0 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 61 6e 64  mber of rows and
17dc0 20 63 6f 6c 75 6d 6e 73 2e 20 20 42 75 74 0a 2a   columns.  But.*
17dd0 2a 20 74 68 65 73 65 20 6e 75 6d 62 65 72 73 20  * these numbers 
17de0 61 72 65 20 6e 6f 74 20 70 61 72 74 20 6f 66 20  are not part of 
17df0 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65  the result table
17e00 20 69 74 73 65 6c 66 2e 20 20 54 68 65 73 65 0a   itself.  These.
17e10 2a 2a 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6f  ** numbers are o
17e20 62 74 61 69 6e 65 64 20 73 65 70 61 72 61 74 65  btained separate
17e30 6c 79 2e 20 20 4c 65 74 20 4e 20 62 65 20 74 68  ly.  Let N be th
17e40 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
17e50 0a 2a 2a 20 61 6e 64 20 4d 20 62 65 20 74 68 65  .** and M be the
17e60 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
17e70 6e 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73 75  ns..**.** A resu
17e80 6c 74 20 74 61 62 6c 65 20 69 73 20 61 6e 20 61  lt table is an a
17e90 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 73  rray of pointers
17ea0 20 74 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61   to zero-termina
17eb0 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67  ted UTF-8 string
17ec0 73 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20  s..** There are 
17ed0 28 4e 2b 31 29 2a 4d 20 65 6c 65 6d 65 6e 74 73  (N+1)*M elements
17ee0 20 69 6e 20 74 68 65 20 61 72 72 61 79 2e 20 20   in the array.  
17ef0 54 68 65 20 66 69 72 73 74 20 4d 20 70 6f 69 6e  The first M poin
17f00 74 65 72 73 20 70 6f 69 6e 74 0a 2a 2a 20 74 6f  ters point.** to
17f10 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
17f20 20 73 74 72 69 6e 67 73 20 74 68 61 74 20 20 63   strings that  c
17f30 6f 6e 74 61 69 6e 20 74 68 65 20 6e 61 6d 65 73  ontain the names
17f40 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 2e   of the columns.
17f50 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 6e  .** The remainin
17f60 67 20 65 6e 74 72 69 65 73 20 61 6c 6c 20 70 6f  g entries all po
17f70 69 6e 74 20 74 6f 20 71 75 65 72 79 20 72 65 73  int to query res
17f80 75 6c 74 73 2e 20 20 4e 55 4c 4c 20 76 61 6c 75  ults.  NULL valu
17f90 65 73 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20  es result.** in 
17fa0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 2e 20 20  NULL pointers.  
17fb0 41 6c 6c 20 6f 74 68 65 72 20 76 61 6c 75 65 73  All other values
17fc0 20 61 72 65 20 69 6e 20 74 68 65 69 72 20 55 54   are in their UT
17fd0 46 2d 38 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61  F-8 zero-termina
17fe0 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 72 65  ted.** string re
17ff0 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 61 73 20  presentation as 
18000 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c  returned by [sql
18010 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
18020 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73  ()]..**.** A res
18030 75 6c 74 20 74 61 62 6c 65 20 6d 69 67 68 74 20  ult table might 
18040 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20 6f  consist of one o
18050 72 20 6d 6f 72 65 20 6d 65 6d 6f 72 79 20 61 6c  r more memory al
18060 6c 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2a 20 49 74  locations..** It
18070 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20   is not safe to 
18080 70 61 73 73 20 61 20 72 65 73 75 6c 74 20 74 61  pass a result ta
18090 62 6c 65 20 64 69 72 65 63 74 6c 79 20 74 6f 20  ble directly to 
180a0 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d  [sqlite3_free()]
180b0 2e 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 74 61  ..** A result ta
180c0 62 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 64 65  ble should be de
180d0 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20  allocated using 
180e0 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61  [sqlite3_free_ta
180f0 62 6c 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 73  ble()]..**.** As
18100 20 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 74   an example of t
18110 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20  he result table 
18120 66 6f 72 6d 61 74 2c 20 73 75 70 70 6f 73 65 20  format, suppose 
18130 61 20 71 75 65 72 79 20 72 65 73 75 6c 74 0a 2a  a query result.*
18140 2a 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  * is as follows:
18150 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f  .**.** <blockquo
18160 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20  te><pre>.**     
18170 20 20 20 4e 61 6d 65 20 20 20 20 20 20 20 20 7c     Name        |
18180 20 41 67 65 0a 2a 2a 20 20 20 20 20 20 20 20 2d   Age.**        -
18190 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
181a0 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 20  ------.**       
181b0 20 41 6c 69 63 65 20 20 20 20 20 20 20 7c 20 34   Alice       | 4
181c0 33 0a 2a 2a 20 20 20 20 20 20 20 20 42 6f 62 20  3.**        Bob 
181d0 20 20 20 20 20 20 20 20 7c 20 32 38 0a 2a 2a 20          | 28.** 
181e0 20 20 20 20 20 20 20 43 69 6e 64 79 20 20 20 20         Cindy    
181f0 20 20 20 7c 20 32 31 0a 2a 2a 20 3c 2f 70 72 65     | 21.** </pre
18200 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a  ></blockquote>.*
18210 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74  *.** There are t
18220 77 6f 20 63 6f 6c 75 6d 6e 20 28 4d 3d 3d 32 29  wo column (M==2)
18230 20 61 6e 64 20 74 68 72 65 65 20 72 6f 77 73 20   and three rows 
18240 28 4e 3d 3d 33 29 2e 20 20 54 68 75 73 20 74 68  (N==3).  Thus th
18250 65 0a 2a 2a 20 72 65 73 75 6c 74 20 74 61 62 6c  e.** result tabl
18260 65 20 68 61 73 20 38 20 65 6e 74 72 69 65 73 2e  e has 8 entries.
18270 20 20 53 75 70 70 6f 73 65 20 74 68 65 20 72 65    Suppose the re
18280 73 75 6c 74 20 74 61 62 6c 65 20 69 73 20 73 74  sult table is st
18290 6f 72 65 64 0a 2a 2a 20 69 6e 20 61 6e 20 61 72  ored.** in an ar
182a0 72 61 79 20 6e 61 6d 65 73 20 61 7a 52 65 73 75  ray names azResu
182b0 6c 74 2e 20 20 54 68 65 6e 20 61 7a 52 65 73 75  lt.  Then azResu
182c0 6c 74 20 68 6f 6c 64 73 20 74 68 69 73 20 63 6f  lt holds this co
182d0 6e 74 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c  ntent:.**.** <bl
182e0 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a  ockquote><pre>.*
182f0 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c  *        azResul
18300 74 26 23 39 31 3b 30 5d 20 3d 20 22 4e 61 6d 65  t&#91;0] = "Name
18310 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52  ";.**        azR
18320 65 73 75 6c 74 26 23 39 31 3b 31 5d 20 3d 20 22  esult&#91;1] = "
18330 41 67 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20  Age";.**        
18340 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 32 5d 20  azResult&#91;2] 
18350 3d 20 22 41 6c 69 63 65 22 3b 0a 2a 2a 20 20 20  = "Alice";.**   
18360 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39       azResult&#9
18370 31 3b 33 5d 20 3d 20 22 34 33 22 3b 0a 2a 2a 20  1;3] = "43";.** 
18380 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26         azResult&
18390 23 39 31 3b 34 5d 20 3d 20 22 42 6f 62 22 3b 0a  #91;4] = "Bob";.
183a0 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75  **        azResu
183b0 6c 74 26 23 39 31 3b 35 5d 20 3d 20 22 32 38 22  lt&#91;5] = "28"
183c0 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65  ;.**        azRe
183d0 73 75 6c 74 26 23 39 31 3b 36 5d 20 3d 20 22 43  sult&#91;6] = "C
183e0 69 6e 64 79 22 3b 0a 2a 2a 20 20 20 20 20 20 20  indy";.**       
183f0 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 37 5d   azResult&#91;7]
18400 20 3d 20 22 32 31 22 3b 0a 2a 2a 20 3c 2f 70 72   = "21";.** </pr
18410 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a  e></blockquote>.
18420 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
18430 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 66 75  3_get_table() fu
18440 6e 63 74 69 6f 6e 20 65 76 61 6c 75 61 74 65 73  nction evaluates
18450 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20   one or more.** 
18460 73 65 6d 69 63 6f 6c 6f 6e 2d 73 65 70 61 72 61  semicolon-separa
18470 74 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ted SQL statemen
18480 74 73 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d 74  ts in the zero-t
18490 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 0a  erminated UTF-8.
184a0 2a 2a 20 73 74 72 69 6e 67 20 6f 66 20 69 74 73  ** string of its
184b0 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20   2nd parameter. 
184c0 20 49 74 20 72 65 74 75 72 6e 73 20 61 20 72 65   It returns a re
184d0 73 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 74 68  sult table to th
184e0 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 67 69 76  e.** pointer giv
184f0 65 6e 20 69 6e 20 69 74 73 20 33 72 64 20 70 61  en in its 3rd pa
18500 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41  rameter..**.** A
18510 66 74 65 72 20 74 68 65 20 63 61 6c 6c 69 6e 67  fter the calling
18520 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 66 69   function has fi
18530 6e 69 73 68 65 64 20 75 73 69 6e 67 20 74 68 65  nished using the
18540 20 72 65 73 75 6c 74 2c 20 69 74 20 73 68 6f 75   result, it shou
18550 6c 64 0a 2a 2a 20 70 61 73 73 20 74 68 65 20 70  ld.** pass the p
18560 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 65  ointer to the re
18570 73 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 73 71  sult table to sq
18580 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65  lite3_free_table
18590 28 29 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a  () in order to.*
185a0 2a 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d 65  * release the me
185b0 6d 6f 72 79 20 74 68 61 74 20 77 61 73 20 6d 61  mory that was ma
185c0 6c 6c 6f 63 65 64 2e 20 20 42 65 63 61 75 73 65  lloced.  Because
185d0 20 6f 66 20 74 68 65 20 77 61 79 20 74 68 65 0a   of the way the.
185e0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  ** [sqlite3_mall
185f0 6f 63 28 29 5d 20 68 61 70 70 65 6e 73 20 77 69  oc()] happens wi
18600 74 68 69 6e 20 73 71 6c 69 74 65 33 5f 67 65 74  thin sqlite3_get
18610 5f 74 61 62 6c 65 28 29 2c 20 74 68 65 20 63 61  _table(), the ca
18620 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f  lling.** functio
18630 6e 20 6d 75 73 74 20 6e 6f 74 20 74 72 79 20 74  n must not try t
18640 6f 20 63 61 6c 6c 20 5b 73 71 6c 69 74 65 33 5f  o call [sqlite3_
18650 66 72 65 65 28 29 5d 20 64 69 72 65 63 74 6c 79  free()] directly
18660 2e 20 20 4f 6e 6c 79 0a 2a 2a 20 5b 73 71 6c 69  .  Only.** [sqli
18670 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29  te3_free_table()
18680 5d 20 69 73 20 61 62 6c 65 20 74 6f 20 72 65 6c  ] is able to rel
18690 65 61 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20  ease the memory 
186a0 70 72 6f 70 65 72 6c 79 20 61 6e 64 20 73 61 66  properly and saf
186b0 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ely..**.** The s
186c0 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65  qlite3_get_table
186d0 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  () interface is 
186e0 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61  implemented as a
186f0 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 0a   wrapper around.
18700 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63  ** [sqlite3_exec
18710 28 29 5d 2e 20 20 54 68 65 20 73 71 6c 69 74 65  ()].  The sqlite
18720 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 72 6f  3_get_table() ro
18730 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 68  utine does not h
18740 61 76 65 20 61 63 63 65 73 73 0a 2a 2a 20 74 6f  ave access.** to
18750 20 61 6e 79 20 69 6e 74 65 72 6e 61 6c 20 64 61   any internal da
18760 74 61 20 73 74 72 75 63 74 75 72 65 73 20 6f 66  ta structures of
18770 20 53 51 4c 69 74 65 2e 20 20 49 74 20 75 73 65   SQLite.  It use
18780 73 20 6f 6e 6c 79 20 74 68 65 20 70 75 62 6c 69  s only the publi
18790 63 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 64  c.** interface d
187a0 65 66 69 6e 65 64 20 68 65 72 65 2e 20 20 41 73  efined here.  As
187b0 20 61 20 63 6f 6e 73 65 71 75 65 6e 63 65 2c 20   a consequence, 
187c0 65 72 72 6f 72 73 20 74 68 61 74 20 6f 63 63 75  errors that occu
187d0 72 20 69 6e 20 74 68 65 0a 2a 2a 20 77 72 61 70  r in the.** wrap
187e0 70 65 72 20 6c 61 79 65 72 20 6f 75 74 73 69 64  per layer outsid
187f0 65 20 6f 66 20 74 68 65 20 69 6e 74 65 72 6e 61  e of the interna
18800 6c 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28  l [sqlite3_exec(
18810 29 5d 20 63 61 6c 6c 20 61 72 65 20 6e 6f 74 0a  )] call are not.
18820 2a 2a 20 72 65 66 6c 65 63 74 65 64 20 69 6e 20  ** reflected in 
18830 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73  subsequent calls
18840 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72   to [sqlite3_err
18850 63 6f 64 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69  code()] or [sqli
18860 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a  te3_errmsg()]..*
18870 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
18880 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 37 31 7d 20  .**.** {H12371} 
18890 49 66 20 61 20 5b 73 71 6c 69 74 65 33 5f 67 65  If a [sqlite3_ge
188a0 74 5f 74 61 62 6c 65 28 29 5d 20 66 61 69 6c 73  t_table()] fails
188b0 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
188c0 74 69 6f 6e 2c 20 74 68 65 6e 0a 2a 2a 20 20 20  tion, then.**   
188d0 20 20 20 20 20 20 20 69 74 20 73 68 61 6c 6c 20         it shall 
188e0 66 72 65 65 20 74 68 65 20 72 65 73 75 6c 74 20  free the result 
188f0 74 61 62 6c 65 20 75 6e 64 65 72 20 63 6f 6e 73  table under cons
18900 74 72 75 63 74 69 6f 6e 2c 20 61 62 6f 72 74 20  truction, abort 
18910 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
18920 71 75 65 72 79 20 69 6e 20 70 72 6f 63 65 73 73  query in process
18930 2c 20 73 6b 69 70 20 61 6e 79 20 73 75 62 73 65  , skip any subse
18940 71 75 65 6e 74 20 71 75 65 72 69 65 73 2c 20 73  quent queries, s
18950 65 74 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  et the.**       
18960 20 20 20 2a 70 61 7a 52 65 73 75 6c 74 20 6f 75     *pazResult ou
18970 74 70 75 74 20 70 6f 69 6e 74 65 72 20 74 6f 20  tput pointer to 
18980 4e 55 4c 4c 20 61 6e 64 20 72 65 74 75 72 6e 20  NULL and return 
18990 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a  [SQLITE_NOMEM]..
189a0 2a 2a 0a 2a 2a 20 7b 48 31 32 33 37 33 7d 20 49  **.** {H12373} I
189b0 66 20 74 68 65 20 70 6e 43 6f 6c 75 6d 6e 20 70  f the pnColumn p
189c0 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c  arameter to [sql
189d0 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29  ite3_get_table()
189e0 5d 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 0a 2a 2a  ] is not NULL.**
189f0 20 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 61            then a
18a00 20 73 75 63 63 65 73 73 66 75 6c 20 69 6e 76 6f   successful invo
18a10 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74  cation of [sqlit
18a20 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 20  e3_get_table()] 
18a30 73 68 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20  shall.**        
18a40 20 20 77 72 69 74 65 20 74 68 65 20 6e 75 6d 62    write the numb
18a50 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
18a60 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
18a70 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74   result set of t
18a80 68 65 20 71 75 65 72 79 20 69 6e 74 6f 20 2a 70  he query into *p
18a90 6e 43 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 7b  nColumn..**.** {
18aa0 48 31 32 33 37 34 7d 20 49 66 20 74 68 65 20 70  H12374} If the p
18ab0 6e 52 6f 77 20 70 61 72 61 6d 65 74 65 72 20 74  nRow parameter t
18ac0 6f 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  o [sqlite3_get_t
18ad0 61 62 6c 65 28 29 5d 20 69 73 20 6e 6f 74 20 4e  able()] is not N
18ae0 55 4c 4c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ULL.**          
18af0 74 68 65 6e 20 61 20 73 75 63 63 65 73 73 66 75  then a successfu
18b00 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20  l invocation of 
18b10 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62  [sqlite3_get_tab
18b20 6c 65 28 29 5d 20 73 68 61 6c 6c 0a 2a 2a 20 20  le()] shall.**  
18b30 20 20 20 20 20 20 20 20 77 72 69 74 65 73 20 74          writes t
18b40 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
18b50 73 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20  s in the.**     
18b60 20 20 20 20 20 72 65 73 75 6c 74 20 73 65 74 20       result set 
18b70 6f 66 20 74 68 65 20 71 75 65 72 79 20 69 6e 74  of the query int
18b80 6f 20 2a 70 6e 52 6f 77 2e 0a 2a 2a 0a 2a 2a 20  o *pnRow..**.** 
18b90 7b 48 31 32 33 37 36 7d 20 41 20 73 75 63 63 65  {H12376} A succe
18ba0 73 73 66 75 6c 20 69 6e 76 6f 63 61 74 69 6f 6e  ssful invocation
18bb0 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 67 65 74   of [sqlite3_get
18bc0 5f 74 61 62 6c 65 28 29 5d 20 74 68 61 74 20 63  _table()] that c
18bd0 6f 6d 70 75 74 65 73 0a 2a 2a 20 20 20 20 20 20  omputes.**      
18be0 20 20 20 20 4e 20 72 6f 77 73 20 6f 66 20 72 65      N rows of re
18bf0 73 75 6c 74 20 77 69 74 68 20 43 20 63 6f 6c 75  sult with C colu
18c00 6d 6e 73 20 70 65 72 20 72 6f 77 20 73 68 61 6c  mns per row shal
18c10 6c 20 6d 61 6b 65 20 2a 70 61 7a 52 65 73 75 6c  l make *pazResul
18c20 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 6f  t.**          po
18c30 69 6e 74 20 74 6f 20 61 6e 20 61 72 72 61 79 20  int to an array 
18c40 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 28  of pointers to (
18c50 4e 2b 31 29 2a 43 20 73 74 72 69 6e 67 73 20 77  N+1)*C strings w
18c60 68 65 72 65 20 74 68 65 20 66 69 72 73 74 0a 2a  here the first.*
18c70 2a 20 20 20 20 20 20 20 20 20 20 43 20 73 74 72  *          C str
18c80 69 6e 67 73 20 61 72 65 20 63 6f 6c 75 6d 6e 20  ings are column 
18c90 6e 61 6d 65 73 20 61 73 20 6f 62 74 61 69 6e 65  names as obtaine
18ca0 64 20 66 72 6f 6d 0a 2a 2a 20 20 20 20 20 20 20  d from.**       
18cb0 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75     [sqlite3_colu
18cc0 6d 6e 5f 6e 61 6d 65 28 29 5d 20 61 6e 64 20 74  mn_name()] and t
18cd0 68 65 20 72 65 73 74 20 61 72 65 20 63 6f 6c 75  he rest are colu
18ce0 6d 6e 20 72 65 73 75 6c 74 20 76 61 6c 75 65 73  mn result values
18cf0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 62 74  .**          obt
18d00 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69  ained from [sqli
18d10 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28  te3_column_text(
18d20 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 37  )]..**.** {H1237
18d30 39 7d 20 54 68 65 20 76 61 6c 75 65 73 20 69 6e  9} The values in
18d40 20 74 68 65 20 70 61 7a 52 65 73 75 6c 74 20 61   the pazResult a
18d50 72 72 61 79 20 72 65 74 75 72 6e 65 64 20 62 79  rray returned by
18d60 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61   [sqlite3_get_ta
18d70 62 6c 65 28 29 5d 0a 2a 2a 20 20 20 20 20 20 20  ble()].**       
18d80 20 20 20 73 68 61 6c 6c 20 72 65 6d 61 69 6e 20     shall remain 
18d90 76 61 6c 69 64 20 75 6e 74 69 6c 20 63 6c 65 61  valid until clea
18da0 72 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f  red by [sqlite3_
18db0 66 72 65 65 5f 74 61 62 6c 65 28 29 5d 2e 0a 2a  free_table()]..*
18dc0 2a 0a 2a 2a 20 7b 48 31 32 33 38 32 7d 20 57 68  *.** {H12382} Wh
18dd0 65 6e 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  en an error occu
18de0 72 73 20 64 75 72 69 6e 67 20 65 76 61 6c 75 61  rs during evalua
18df0 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33  tion of [sqlite3
18e00 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 0a 2a 2a  _get_table()].**
18e10 20 20 20 20 20 20 20 20 20 20 74 68 65 20 66 75            the fu
18e20 6e 63 74 69 6f 6e 20 73 68 61 6c 6c 20 73 65 74  nction shall set
18e30 20 2a 70 61 7a 52 65 73 75 6c 74 20 74 6f 20 4e   *pazResult to N
18e40 55 4c 4c 2c 20 77 72 69 74 65 20 61 6e 20 65 72  ULL, write an er
18e50 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 20  ror message.**  
18e60 20 20 20 20 20 20 20 20 69 6e 74 6f 20 6d 65 6d          into mem
18e70 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
18e80 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  m [sqlite3_mallo
18e90 63 28 29 5d 2c 20 6d 61 6b 65 0a 2a 2a 20 20 20  c()], make.**   
18ea0 20 20 20 20 20 20 20 2a 2a 70 7a 45 72 72 6d 73         **pzErrms
18eb0 67 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20  g point to that 
18ec0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2c 20 61  error message, a
18ed0 6e 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 20  nd return a.**  
18ee0 20 20 20 20 20 20 20 20 61 70 70 72 6f 70 72 69          appropri
18ef0 61 74 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d  ate [error code]
18f00 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
18f10 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  int sqlite3_get_
18f20 74 61 62 6c 65 28 0a 20 20 73 71 6c 69 74 65 33  table(.  sqlite3
18f30 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f   *db,          /
18f40 2a 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 62 61  * An open databa
18f50 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  se */.  const ch
18f60 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 2f 2a  ar *zSql,     /*
18f70 20 53 51 4c 20 74 6f 20 62 65 20 65 76 61 6c 75   SQL to be evalu
18f80 61 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a  ated */.  char *
18f90 2a 2a 70 61 7a 52 65 73 75 6c 74 2c 20 20 20 20  **pazResult,    
18fa0 2f 2a 20 52 65 73 75 6c 74 73 20 6f 66 20 74 68  /* Results of th
18fb0 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e 74  e query */.  int
18fc0 20 2a 70 6e 52 6f 77 2c 20 20 20 20 20 20 20 20   *pnRow,        
18fd0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
18fe0 72 65 73 75 6c 74 20 72 6f 77 73 20 77 72 69 74  result rows writ
18ff0 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e  ten here */.  in
19000 74 20 2a 70 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20  t *pnColumn,    
19010 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
19020 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20   result columns 
19030 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a  written here */.
19040 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 6d 73    char **pzErrms
19050 67 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72  g       /* Error
19060 20 6d 73 67 20 77 72 69 74 74 65 6e 20 68 65 72   msg written her
19070 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41  e */.);.SQLITE_A
19080 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
19090 66 72 65 65 5f 74 61 62 6c 65 28 63 68 61 72 20  free_table(char 
190a0 2a 2a 72 65 73 75 6c 74 29 3b 0a 0a 2f 2a 0a 2a  **result);../*.*
190b0 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6f 72 6d  * CAPI3REF: Form
190c0 61 74 74 65 64 20 53 74 72 69 6e 67 20 50 72 69  atted String Pri
190d0 6e 74 69 6e 67 20 46 75 6e 63 74 69 6f 6e 73 20  nting Functions 
190e0 7b 48 31 37 34 30 30 7d 20 3c 53 37 30 30 30 30  {H17400} <S70000
190f0 3e 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20  ><S20000>.**.** 
19100 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  These routines a
19110 72 65 20 77 6f 72 6b 61 6c 69 6b 65 73 20 6f 66  re workalikes of
19120 20 74 68 65 20 22 70 72 69 6e 74 66 28 29 22 20   the "printf()" 
19130 66 61 6d 69 6c 79 20 6f 66 20 66 75 6e 63 74 69  family of functi
19140 6f 6e 73 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20  ons.** from the 
19150 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61  standard C libra
19160 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  ry..**.** The sq
19170 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 20  lite3_mprintf() 
19180 61 6e 64 20 73 71 6c 69 74 65 33 5f 76 6d 70 72  and sqlite3_vmpr
19190 69 6e 74 66 28 29 20 72 6f 75 74 69 6e 65 73 20  intf() routines 
191a0 77 72 69 74 65 20 74 68 65 69 72 0a 2a 2a 20 72  write their.** r
191b0 65 73 75 6c 74 73 20 69 6e 74 6f 20 6d 65 6d 6f  esults into memo
191c0 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ry obtained from
191d0 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
191e0 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 74 72 69  ()]..** The stri
191f0 6e 67 73 20 72 65 74 75 72 6e 65 64 20 62 79 20  ngs returned by 
19200 74 68 65 73 65 20 74 77 6f 20 72 6f 75 74 69 6e  these two routin
19210 65 73 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20  es should be.** 
19220 72 65 6c 65 61 73 65 64 20 62 79 20 5b 73 71 6c  released by [sql
19230 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 20 20 42  ite3_free()].  B
19240 6f 74 68 20 72 6f 75 74 69 6e 65 73 20 72 65 74  oth routines ret
19250 75 72 6e 20 61 0a 2a 2a 20 4e 55 4c 4c 20 70 6f  urn a.** NULL po
19260 69 6e 74 65 72 20 69 66 20 5b 73 71 6c 69 74 65  inter if [sqlite
19270 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 69 73 20 75  3_malloc()] is u
19280 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74  nable to allocat
19290 65 20 65 6e 6f 75 67 68 0a 2a 2a 20 6d 65 6d 6f  e enough.** memo
192a0 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72  ry to hold the r
192b0 65 73 75 6c 74 69 6e 67 20 73 74 72 69 6e 67 2e  esulting string.
192c0 0a 2a 2a 0a 2a 2a 20 49 6e 20 73 71 6c 69 74 65  .**.** In sqlite
192d0 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 72 6f 75  3_snprintf() rou
192e0 74 69 6e 65 20 69 73 20 73 69 6d 69 6c 61 72 20  tine is similar 
192f0 74 6f 20 22 73 6e 70 72 69 6e 74 66 28 29 22 20  to "snprintf()" 
19300 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 73 74 61 6e  from.** the stan
19310 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e 20  dard C library. 
19320 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 77   The result is w
19330 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 0a  ritten into the.
19340 2a 2a 20 62 75 66 66 65 72 20 73 75 70 70 6c 69  ** buffer suppli
19350 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64  ed as the second
19360 20 70 61 72 61 6d 65 74 65 72 20 77 68 6f 73 65   parameter whose
19370 20 73 69 7a 65 20 69 73 20 67 69 76 65 6e 20 62   size is given b
19380 79 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 70  y.** the first p
19390 61 72 61 6d 65 74 65 72 2e 20 4e 6f 74 65 20 74  arameter. Note t
193a0 68 61 74 20 74 68 65 20 6f 72 64 65 72 20 6f 66  hat the order of
193b0 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 74 77   the.** first tw
193c0 6f 20 70 61 72 61 6d 65 74 65 72 73 20 69 73 20  o parameters is 
193d0 72 65 76 65 72 73 65 64 20 66 72 6f 6d 20 73 6e  reversed from sn
193e0 70 72 69 6e 74 66 28 29 2e 20 20 54 68 69 73 20  printf().  This 
193f0 69 73 20 61 6e 0a 2a 2a 20 68 69 73 74 6f 72 69  is an.** histori
19400 63 61 6c 20 61 63 63 69 64 65 6e 74 20 74 68 61  cal accident tha
19410 74 20 63 61 6e 6e 6f 74 20 62 65 20 66 69 78 65  t cannot be fixe
19420 64 20 77 69 74 68 6f 75 74 20 62 72 65 61 6b 69  d without breaki
19430 6e 67 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73 20  ng.** backwards 
19440 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 20 20  compatibility.  
19450 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20 73  Note also that s
19460 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
19470 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 70  ).** returns a p
19480 6f 69 6e 74 65 72 20 74 6f 20 69 74 73 20 62 75  ointer to its bu
19490 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20  ffer instead of 
194a0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  the number of.**
194b0 20 63 68 61 72 61 63 74 65 72 73 20 61 63 74 75   characters actu
194c0 61 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 6e 74  ally written int
194d0 6f 20 74 68 65 20 62 75 66 66 65 72 2e 20 20 57  o the buffer.  W
194e0 65 20 61 64 6d 69 74 20 74 68 61 74 0a 2a 2a 20  e admit that.** 
194f0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68  the number of ch
19500 61 72 61 63 74 65 72 73 20 77 72 69 74 74 65 6e  aracters written
19510 20 77 6f 75 6c 64 20 62 65 20 61 20 6d 6f 72 65   would be a more
19520 20 75 73 65 66 75 6c 20 72 65 74 75 72 6e 0a 2a   useful return.*
19530 2a 20 76 61 6c 75 65 20 62 75 74 20 77 65 20 63  * value but we c
19540 61 6e 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65  annot change the
19550 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
19560 6f 66 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  of sqlite3_snpri
19570 6e 74 66 28 29 0a 2a 2a 20 6e 6f 77 20 77 69 74  ntf().** now wit
19580 68 6f 75 74 20 62 72 65 61 6b 69 6e 67 20 63 6f  hout breaking co
19590 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a  mpatibility..**.
195a0 2a 2a 20 41 73 20 6c 6f 6e 67 20 61 73 20 74 68  ** As long as th
195b0 65 20 62 75 66 66 65 72 20 73 69 7a 65 20 69 73  e buffer size is
195c0 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65   greater than ze
195d0 72 6f 2c 20 73 71 6c 69 74 65 33 5f 73 6e 70 72  ro, sqlite3_snpr
195e0 69 6e 74 66 28 29 0a 2a 2a 20 67 75 61 72 61 6e  intf().** guaran
195f0 74 65 65 73 20 74 68 61 74 20 74 68 65 20 62 75  tees that the bu
19600 66 66 65 72 20 69 73 20 61 6c 77 61 79 73 20 7a  ffer is always z
19610 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20  ero-terminated. 
19620 20 54 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 61   The first.** pa
19630 72 61 6d 65 74 65 72 20 22 6e 22 20 69 73 20 74  rameter "n" is t
19640 68 65 20 74 6f 74 61 6c 20 73 69 7a 65 20 6f 66  he total size of
19650 20 74 68 65 20 62 75 66 66 65 72 2c 20 69 6e 63   the buffer, inc
19660 6c 75 64 69 6e 67 20 73 70 61 63 65 20 66 6f 72  luding space for
19670 0a 2a 2a 20 74 68 65 20 7a 65 72 6f 20 74 65 72  .** the zero ter
19680 6d 69 6e 61 74 6f 72 2e 20 20 53 6f 20 74 68 65  minator.  So the
19690 20 6c 6f 6e 67 65 73 74 20 73 74 72 69 6e 67 20   longest string 
196a0 74 68 61 74 20 63 61 6e 20 62 65 20 63 6f 6d 70  that can be comp
196b0 6c 65 74 65 6c 79 0a 2a 2a 20 77 72 69 74 74 65  letely.** writte
196c0 6e 20 77 69 6c 6c 20 62 65 20 6e 2d 31 20 63 68  n will be n-1 ch
196d0 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20  aracters..**.** 
196e0 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  These routines a
196f0 6c 6c 20 69 6d 70 6c 65 6d 65 6e 74 20 73 6f 6d  ll implement som
19700 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 6f 72  e additional for
19710 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 70 74 69 6f  matting.** optio
19720 6e 73 20 74 68 61 74 20 61 72 65 20 75 73 65 66  ns that are usef
19730 75 6c 20 66 6f 72 20 63 6f 6e 73 74 72 75 63 74  ul for construct
19740 69 6e 67 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ing SQL statemen
19750 74 73 2e 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68  ts..** All of th
19760 65 20 75 73 75 61 6c 20 70 72 69 6e 74 66 28 29  e usual printf()
19770 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 70 74 69   formatting opti
19780 6f 6e 73 20 61 70 70 6c 79 2e 20 20 49 6e 20 61  ons apply.  In a
19790 64 64 69 74 69 6f 6e 2c 20 74 68 65 72 65 0a 2a  ddition, there.*
197a0 2a 20 69 73 20 61 72 65 20 22 25 71 22 2c 20 22  * is are "%q", "
197b0 25 51 22 2c 20 61 6e 64 20 22 25 7a 22 20 6f 70  %Q", and "%z" op
197c0 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tions..**.** The
197d0 20 25 71 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73   %q option works
197e0 20 6c 69 6b 65 20 25 73 20 69 6e 20 74 68 61 74   like %s in that
197f0 20 69 74 20 73 75 62 73 74 69 74 75 74 65 73 20   it substitutes 
19800 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65  a null-terminate
19810 64 0a 2a 2a 20 73 74 72 69 6e 67 20 66 72 6f 6d  d.** string from
19820 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 6c 69   the argument li
19830 73 74 2e 20 20 42 75 74 20 25 71 20 61 6c 73 6f  st.  But %q also
19840 20 64 6f 75 62 6c 65 73 20 65 76 65 72 79 20 27   doubles every '
19850 5c 27 27 20 63 68 61 72 61 63 74 65 72 2e 0a 2a  \'' character..*
19860 2a 20 25 71 20 69 73 20 64 65 73 69 67 6e 65 64  * %q is designed
19870 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65 20   for use inside 
19880 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c  a string literal
19890 2e 20 20 42 79 20 64 6f 75 62 6c 69 6e 67 20 65  .  By doubling e
198a0 61 63 68 20 27 5c 27 27 0a 2a 2a 20 63 68 61 72  ach '\''.** char
198b0 61 63 74 65 72 20 69 74 20 65 73 63 61 70 65 73  acter it escapes
198c0 20 74 68 61 74 20 63 68 61 72 61 63 74 65 72 20   that character 
198d0 61 6e 64 20 61 6c 6c 6f 77 73 20 69 74 20 74 6f  and allows it to
198e0 20 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74   be inserted int
198f0 6f 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 2e  o.** the string.
19900 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70  .**.** For examp
19910 6c 65 2c 20 61 73 73 75 6d 65 20 74 68 65 20 73  le, assume the s
19920 74 72 69 6e 67 20 76 61 72 69 61 62 6c 65 20 7a  tring variable z
19930 54 65 78 74 20 63 6f 6e 74 61 69 6e 73 20 74 65  Text contains te
19940 78 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  xt as follows:.*
19950 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65  *.** <blockquote
19960 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 61 72 20  ><pre>.**  char 
19970 2a 7a 54 65 78 74 20 3d 20 22 49 74 27 73 20 61  *zText = "It's a
19980 20 68 61 70 70 79 20 64 61 79 21 22 3b 0a 2a 2a   happy day!";.**
19990 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75   </pre></blockqu
199a0 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 63  ote>.**.** One c
199b0 61 6e 20 75 73 65 20 74 68 69 73 20 74 65 78 74  an use this text
199c0 20 69 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 65   in an SQL state
199d0 6d 65 6e 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  ment as follows:
199e0 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f  .**.** <blockquo
199f0 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 61  te><pre>.**  cha
19a00 72 20 2a 7a 53 51 4c 20 3d 20 73 71 6c 69 74 65  r *zSQL = sqlite
19a10 33 5f 6d 70 72 69 6e 74 66 28 22 49 4e 53 45 52  3_mprintf("INSER
19a20 54 20 49 4e 54 4f 20 74 61 62 6c 65 20 56 41 4c  T INTO table VAL
19a30 55 45 53 28 27 25 71 27 29 22 2c 20 7a 54 65 78  UES('%q')", zTex
19a40 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f  t);.**  sqlite3_
19a50 65 78 65 63 28 64 62 2c 20 7a 53 51 4c 2c 20 30  exec(db, zSQL, 0
19a60 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20 73 71 6c  , 0, 0);.**  sql
19a70 69 74 65 33 5f 66 72 65 65 28 7a 53 51 4c 29 3b  ite3_free(zSQL);
19a80 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63  .** </pre></bloc
19a90 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 42 65  kquote>.**.** Be
19aa0 63 61 75 73 65 20 74 68 65 20 25 71 20 66 6f 72  cause the %q for
19ab0 6d 61 74 20 73 74 72 69 6e 67 20 69 73 20 75 73  mat string is us
19ac0 65 64 2c 20 74 68 65 20 27 5c 27 27 20 63 68 61  ed, the '\'' cha
19ad0 72 61 63 74 65 72 20 69 6e 20 7a 54 65 78 74 0a  racter in zText.
19ae0 2a 2a 20 69 73 20 65 73 63 61 70 65 64 20 61 6e  ** is escaped an
19af0 64 20 74 68 65 20 53 51 4c 20 67 65 6e 65 72 61  d the SQL genera
19b00 74 65 64 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77  ted is as follow
19b10 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71  s:.**.** <blockq
19b20 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 49  uote><pre>.**  I
19b30 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65  NSERT INTO table
19b40 31 20 56 41 4c 55 45 53 28 27 49 74 27 27 73 20  1 VALUES('It''s 
19b50 61 20 68 61 70 70 79 20 64 61 79 21 27 29 0a 2a  a happy day!').*
19b60 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71  * </pre></blockq
19b70 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  uote>.**.** This
19b80 20 69 73 20 63 6f 72 72 65 63 74 2e 20 20 48 61   is correct.  Ha
19b90 64 20 77 65 20 75 73 65 64 20 25 73 20 69 6e 73  d we used %s ins
19ba0 74 65 61 64 20 6f 66 20 25 71 2c 20 74 68 65 20  tead of %q, the 
19bb0 67 65 6e 65 72 61 74 65 64 20 53 51 4c 0a 2a 2a  generated SQL.**
19bc0 20 77 6f 75 6c 64 20 68 61 76 65 20 6c 6f 6f 6b   would have look
19bd0 65 64 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  ed like this:.**
19be0 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e  .** <blockquote>
19bf0 3c 70 72 65 3e 0a 2a 2a 20 20 49 4e 53 45 52 54  <pre>.**  INSERT
19c00 20 49 4e 54 4f 20 74 61 62 6c 65 31 20 56 41 4c   INTO table1 VAL
19c10 55 45 53 28 27 49 74 27 73 20 61 20 68 61 70 70  UES('It's a happ
19c20 79 20 64 61 79 21 27 29 3b 0a 2a 2a 20 3c 2f 70  y day!');.** </p
19c30 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e  re></blockquote>
19c40 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 65 63 6f  .**.** This seco
19c50 6e 64 20 65 78 61 6d 70 6c 65 20 69 73 20 61 6e  nd example is an
19c60 20 53 51 4c 20 73 79 6e 74 61 78 20 65 72 72 6f   SQL syntax erro
19c70 72 2e 20 20 41 73 20 61 20 67 65 6e 65 72 61 6c  r.  As a general
19c80 20 72 75 6c 65 20 79 6f 75 20 73 68 6f 75 6c 64   rule you should
19c90 0a 2a 2a 20 61 6c 77 61 79 73 20 75 73 65 20 25  .** always use %
19ca0 71 20 69 6e 73 74 65 61 64 20 6f 66 20 25 73 20  q instead of %s 
19cb0 77 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 74  when inserting t
19cc0 65 78 74 20 69 6e 74 6f 20 61 20 73 74 72 69 6e  ext into a strin
19cd0 67 20 6c 69 74 65 72 61 6c 2e 0a 2a 2a 0a 2a 2a  g literal..**.**
19ce0 20 54 68 65 20 25 51 20 6f 70 74 69 6f 6e 20 77   The %Q option w
19cf0 6f 72 6b 73 20 6c 69 6b 65 20 25 71 20 65 78 63  orks like %q exc
19d00 65 70 74 20 69 74 20 61 6c 73 6f 20 61 64 64 73  ept it also adds
19d10 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 73 20 61   single quotes a
19d20 72 6f 75 6e 64 0a 2a 2a 20 74 68 65 20 6f 75 74  round.** the out
19d30 73 69 64 65 20 6f 66 20 74 68 65 20 74 6f 74 61  side of the tota
19d40 6c 20 73 74 72 69 6e 67 2e 20 20 41 64 64 69 74  l string.  Addit
19d50 69 6f 6e 61 6c 6c 79 2c 20 69 66 20 74 68 65 20  ionally, if the 
19d60 70 61 72 61 6d 65 74 65 72 20 69 6e 20 74 68 65  parameter in the
19d70 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6c 69 73  .** argument lis
19d80 74 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  t is a NULL poin
19d90 74 65 72 2c 20 25 51 20 73 75 62 73 74 69 74 75  ter, %Q substitu
19da0 74 65 73 20 74 68 65 20 74 65 78 74 20 22 4e 55  tes the text "NU
19db0 4c 4c 22 20 28 77 69 74 68 6f 75 74 0a 2a 2a 20  LL" (without.** 
19dc0 73 69 6e 67 6c 65 20 71 75 6f 74 65 73 29 20 69  single quotes) i
19dd0 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 65 20 25  n place of the %
19de0 51 20 6f 70 74 69 6f 6e 2e 20 20 53 6f 2c 20 66  Q option.  So, f
19df0 6f 72 20 65 78 61 6d 70 6c 65 2c 20 6f 6e 65 20  or example, one 
19e00 63 6f 75 6c 64 20 73 61 79 3a 0a 2a 2a 0a 2a 2a  could say:.**.**
19e10 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72   <blockquote><pr
19e20 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 53 51  e>.**  char *zSQ
19e30 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  L = sqlite3_mpri
19e40 6e 74 66 28 22 49 4e 53 45 52 54 20 49 4e 54 4f  ntf("INSERT INTO
19e50 20 74 61 62 6c 65 20 56 41 4c 55 45 53 28 25 51   table VALUES(%Q
19e60 29 22 2c 20 7a 54 65 78 74 29 3b 0a 2a 2a 20 20  )", zText);.**  
19e70 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c  sqlite3_exec(db,
19e80 20 7a 53 51 4c 2c 20 30 2c 20 30 2c 20 30 29 3b   zSQL, 0, 0, 0);
19e90 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .**  sqlite3_fre
19ea0 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20 3c 2f 70 72  e(zSQL);.** </pr
19eb0 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a  e></blockquote>.
19ec0 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 61  **.** The code a
19ed0 62 6f 76 65 20 77 69 6c 6c 20 72 65 6e 64 65 72  bove will render
19ee0 20 61 20 63 6f 72 72 65 63 74 20 53 51 4c 20 73   a correct SQL s
19ef0 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20  tatement in the 
19f00 7a 53 51 4c 0a 2a 2a 20 76 61 72 69 61 62 6c 65  zSQL.** variable
19f10 20 65 76 65 6e 20 69 66 20 74 68 65 20 7a 54 65   even if the zTe
19f20 78 74 20 76 61 72 69 61 62 6c 65 20 69 73 20 61  xt variable is a
19f30 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a   NULL pointer..*
19f40 2a 0a 2a 2a 20 54 68 65 20 22 25 7a 22 20 66 6f  *.** The "%z" fo
19f50 72 6d 61 74 74 69 6e 67 20 6f 70 74 69 6f 6e 20  rmatting option 
19f60 77 6f 72 6b 73 20 65 78 61 63 74 6c 79 20 6c 69  works exactly li
19f70 6b 65 20 22 25 73 22 20 77 69 74 68 20 74 68 65  ke "%s" with the
19f80 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 20 74 68 61  .** addition tha
19f90 74 20 61 66 74 65 72 20 74 68 65 20 73 74 72 69  t after the stri
19fa0 6e 67 20 68 61 73 20 62 65 65 6e 20 72 65 61 64  ng has been read
19fb0 20 61 6e 64 20 63 6f 70 69 65 64 20 69 6e 74 6f   and copied into
19fc0 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 2c 20  .** the result, 
19fd0 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d  [sqlite3_free()]
19fe0 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68   is called on th
19ff0 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 2e 20  e input string. 
1a000 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49 4e 56 41  {END}.**.** INVA
1a010 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48  RIANTS:.**.** {H
1a020 31 37 34 30 33 7d 20 20 54 68 65 20 5b 73 71 6c  17403}  The [sql
1a030 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 5d 20  ite3_mprintf()] 
1a040 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 76 6d 70  and [sqlite3_vmp
1a050 72 69 6e 74 66 28 29 5d 20 69 6e 74 65 72 66 61  rintf()] interfa
1a060 63 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ces.**          
1a070 20 72 65 74 75 72 6e 20 65 69 74 68 65 72 20 70   return either p
1a080 6f 69 6e 74 65 72 73 20 74 6f 20 7a 65 72 6f 2d  ointers to zero-
1a090 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38  terminated UTF-8
1a0a0 20 73 74 72 69 6e 67 73 20 68 65 6c 64 20 69 6e   strings held in
1a0b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6d 65  .**           me
1a0c0 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
1a0d0 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  om [sqlite3_mall
1a0e0 6f 63 28 29 5d 20 6f 72 20 4e 55 4c 4c 20 70 6f  oc()] or NULL po
1a0f0 69 6e 74 65 72 73 20 69 66 0a 2a 2a 20 20 20 20  inters if.**    
1a100 20 20 20 20 20 20 20 61 20 63 61 6c 6c 20 74 6f         a call to
1a110 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
1a120 28 29 5d 20 66 61 69 6c 73 2e 0a 2a 2a 0a 2a 2a  ()] fails..**.**
1a130 20 7b 48 31 37 34 30 36 7d 20 20 54 68 65 20 5b   {H17406}  The [
1a140 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1a150 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 77 72  ()] interface wr
1a160 69 74 65 73 20 61 20 7a 65 72 6f 2d 74 65 72 6d  ites a zero-term
1a170 69 6e 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 20  inated.**       
1a180 20 20 20 20 55 54 46 2d 38 20 73 74 72 69 6e 67      UTF-8 string
1a190 20 69 6e 74 6f 20 74 68 65 20 62 75 66 66 65 72   into the buffer
1a1a0 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74   pointed to by t
1a1b0 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  he second parame
1a1c0 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ter.**          
1a1d0 20 70 72 6f 76 69 64 65 64 20 74 68 61 74 20 74   provided that t
1a1e0 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  he first paramet
1a1f0 65 72 20 69 73 20 67 72 65 61 74 65 72 20 74 68  er is greater th
1a200 61 6e 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 7b  an zero..**.** {
1a210 48 31 37 34 30 37 7d 20 20 54 68 65 20 5b 73 71  H17407}  The [sq
1a220 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29  lite3_snprintf()
1a230 5d 20 69 6e 74 65 72 66 61 63 65 20 64 6f 65 73  ] interface does
1a240 20 6e 6f 74 20 77 72 69 74 65 20 73 6c 6f 74 73   not write slots
1a250 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   of.**          
1a260 20 69 74 73 20 6f 75 74 70 75 74 20 62 75 66 66   its output buff
1a270 65 72 20 28 74 68 65 20 73 65 63 6f 6e 64 20 70  er (the second p
1a280 61 72 61 6d 65 74 65 72 29 20 6f 75 74 73 69 64  arameter) outsid
1a290 65 20 74 68 65 20 72 61 6e 67 65 0a 2a 2a 20 20  e the range.**  
1a2a0 20 20 20 20 20 20 20 20 20 6f 66 20 30 20 74 68           of 0 th
1a2b0 72 6f 75 67 68 20 4e 2d 31 20 28 77 68 65 72 65  rough N-1 (where
1a2c0 20 4e 20 69 73 20 74 68 65 20 66 69 72 73 74 20   N is the first 
1a2d0 70 61 72 61 6d 65 74 65 72 29 0a 2a 2a 20 20 20  parameter).**   
1a2e0 20 20 20 20 20 20 20 20 72 65 67 61 72 64 6c 65          regardle
1a2f0 73 73 20 6f 66 20 74 68 65 20 6c 65 6e 67 74 68  ss of the length
1a300 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 0a 2a   of the string.*
1a310 2a 20 20 20 20 20 20 20 20 20 20 20 72 65 71 75  *           requ
1a320 65 73 74 65 64 20 62 79 20 74 68 65 20 66 6f 72  ested by the for
1a330 6d 61 74 20 73 70 65 63 69 66 69 63 61 74 69 6f  mat specificatio
1a340 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  n..*/.SQLITE_API
1a350 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6d   char *sqlite3_m
1a360 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61  printf(const cha
1a370 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f  r*,...);.SQLITE_
1a380 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65  API char *sqlite
1a390 33 5f 76 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74  3_vmprintf(const
1a3a0 20 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29   char*, va_list)
1a3b0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61  ;.SQLITE_API cha
1a3c0 72 20 2a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  r *sqlite3_snpri
1a3d0 6e 74 66 28 69 6e 74 2c 63 68 61 72 2a 2c 63 6f  ntf(int,char*,co
1a3e0 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b  nst char*, ...);
1a3f0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
1a400 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74  : Memory Allocat
1a410 69 6f 6e 20 53 75 62 73 79 73 74 65 6d 20 7b 48  ion Subsystem {H
1a420 31 37 33 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a  17300} <S20000>.
1a430 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65  **.** The SQLite
1a440 20 63 6f 72 65 20 20 75 73 65 73 20 74 68 65 73   core  uses thes
1a450 65 20 74 68 72 65 65 20 72 6f 75 74 69 6e 65 73  e three routines
1a460 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20   for all of its 
1a470 6f 77 6e 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 20  own.** internal 
1a480 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
1a490 6e 20 6e 65 65 64 73 2e 20 22 43 6f 72 65 22 20  n needs. "Core" 
1a4a0 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  in the previous 
1a4b0 73 65 6e 74 65 6e 63 65 0a 2a 2a 20 64 6f 65 73  sentence.** does
1a4c0 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 6f 70 65   not include ope
1a4d0 72 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 73 70  rating-system sp
1a4e0 65 63 69 66 69 63 20 56 46 53 20 69 6d 70 6c 65  ecific VFS imple
1a4f0 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 65 0a  mentation.  The.
1a500 2a 2a 20 57 69 6e 64 6f 77 73 20 56 46 53 20 75  ** Windows VFS u
1a510 73 65 73 20 6e 61 74 69 76 65 20 6d 61 6c 6c 6f  ses native mallo
1a520 63 28 29 20 61 6e 64 20 66 72 65 65 28 29 20 66  c() and free() f
1a530 6f 72 20 73 6f 6d 65 20 6f 70 65 72 61 74 69 6f  or some operatio
1a540 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  ns..**.** The sq
1a550 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72  lite3_malloc() r
1a560 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61  outine returns a
1a570 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c   pointer to a bl
1a580 6f 63 6b 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79  ock.** of memory
1a590 20 61 74 20 6c 65 61 73 74 20 4e 20 62 79 74 65   at least N byte
1a5a0 73 20 69 6e 20 6c 65 6e 67 74 68 2c 20 77 68 65  s in length, whe
1a5b0 72 65 20 4e 20 69 73 20 74 68 65 20 70 61 72 61  re N is the para
1a5c0 6d 65 74 65 72 2e 0a 2a 2a 20 49 66 20 73 71 6c  meter..** If sql
1a5d0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 73  ite3_malloc() is
1a5e0 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61 69   unable to obtai
1a5f0 6e 20 73 75 66 66 69 63 69 65 6e 74 20 66 72 65  n sufficient fre
1a600 65 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 69 74 20  e.** memory, it 
1a610 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70  returns a NULL p
1a620 6f 69 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20  ointer.  If the 
1a630 70 61 72 61 6d 65 74 65 72 20 4e 20 74 6f 0a 2a  parameter N to.*
1a640 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  * sqlite3_malloc
1a650 28 29 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65  () is zero or ne
1a660 67 61 74 69 76 65 20 74 68 65 6e 20 73 71 6c 69  gative then sqli
1a670 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 65 74  te3_malloc() ret
1a680 75 72 6e 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70  urns.** a NULL p
1a690 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 61  ointer..**.** Ca
1a6a0 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 72  lling sqlite3_fr
1a6b0 65 65 28 29 20 77 69 74 68 20 61 20 70 6f 69 6e  ee() with a poin
1a6c0 74 65 72 20 70 72 65 76 69 6f 75 73 6c 79 20 72  ter previously r
1a6d0 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71  eturned.** by sq
1a6e0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 6f  lite3_malloc() o
1a6f0 72 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  r sqlite3_reallo
1a700 63 28 29 20 72 65 6c 65 61 73 65 73 20 74 68 61  c() releases tha
1a710 74 20 6d 65 6d 6f 72 79 20 73 6f 0a 2a 2a 20 74  t memory so.** t
1a720 68 61 74 20 69 74 20 6d 69 67 68 74 20 62 65 20  hat it might be 
1a730 72 65 75 73 65 64 2e 20 20 54 68 65 20 73 71 6c  reused.  The sql
1a740 69 74 65 33 5f 66 72 65 65 28 29 20 72 6f 75 74  ite3_free() rout
1a750 69 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f  ine is.** a no-o
1a760 70 20 69 66 20 69 73 20 63 61 6c 6c 65 64 20 77  p if is called w
1a770 69 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  ith a NULL point
1a780 65 72 2e 20 20 50 61 73 73 69 6e 67 20 61 20 4e  er.  Passing a N
1a790 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74  ULL pointer.** t
1a7a0 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  o sqlite3_free()
1a7b0 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 20 41   is harmless.  A
1a7c0 66 74 65 72 20 62 65 69 6e 67 20 66 72 65 65 64  fter being freed
1a7d0 2c 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 68 6f 75  , memory.** shou
1a7e0 6c 64 20 6e 65 69 74 68 65 72 20 62 65 20 72 65  ld neither be re
1a7f0 61 64 20 6e 6f 72 20 77 72 69 74 74 65 6e 2e 20  ad nor written. 
1a800 20 45 76 65 6e 20 72 65 61 64 69 6e 67 20 70 72   Even reading pr
1a810 65 76 69 6f 75 73 6c 79 20 66 72 65 65 64 0a 2a  eviously freed.*
1a820 2a 20 6d 65 6d 6f 72 79 20 6d 69 67 68 74 20 72  * memory might r
1a830 65 73 75 6c 74 20 69 6e 20 61 20 73 65 67 6d 65  esult in a segme
1a840 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 20 6f 72  ntation fault or
1a850 20 6f 74 68 65 72 20 73 65 76 65 72 65 20 65 72   other severe er
1a860 72 6f 72 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 63  ror..** Memory c
1a870 6f 72 72 75 70 74 69 6f 6e 2c 20 61 20 73 65 67  orruption, a seg
1a880 6d 65 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 2c  mentation fault,
1a890 20 6f 72 20 6f 74 68 65 72 20 73 65 76 65 72 65   or other severe
1a8a0 20 65 72 72 6f 72 0a 2a 2a 20 6d 69 67 68 74 20   error.** might 
1a8b0 72 65 73 75 6c 74 20 69 66 20 73 71 6c 69 74 65  result if sqlite
1a8c0 33 5f 66 72 65 65 28 29 20 69 73 20 63 61 6c 6c  3_free() is call
1a8d0 65 64 20 77 69 74 68 20 61 20 6e 6f 6e 2d 4e 55  ed with a non-NU
1a8e0 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 61 74 0a  LL pointer that.
1a8f0 2a 2a 20 77 61 73 20 6e 6f 74 20 6f 62 74 61 69  ** was not obtai
1a900 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
1a910 5f 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c  _malloc() or sql
1a920 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a  ite3_realloc()..
1a930 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
1a940 33 5f 72 65 61 6c 6c 6f 63 28 29 20 69 6e 74 65  3_realloc() inte
1a950 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 74  rface attempts t
1a960 6f 20 72 65 73 69 7a 65 20 61 0a 2a 2a 20 70 72  o resize a.** pr
1a970 69 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ior memory alloc
1a980 61 74 69 6f 6e 20 74 6f 20 62 65 20 61 74 20 6c  ation to be at l
1a990 65 61 73 74 20 4e 20 62 79 74 65 73 2c 20 77 68  east N bytes, wh
1a9a0 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a 20  ere N is the.** 
1a9b0 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
1a9c0 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c  .  The memory al
1a9d0 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 62 65 20 72  location to be r
1a9e0 65 73 69 7a 65 64 20 69 73 20 74 68 65 20 66 69  esized is the fi
1a9f0 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  rst.** parameter
1aa00 2e 20 20 49 66 20 74 68 65 20 66 69 72 73 74 20  .  If the first 
1aa10 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c  parameter to sql
1aa20 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a 2a  ite3_realloc().*
1aa30 2a 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  * is a NULL poin
1aa40 74 65 72 20 74 68 65 6e 20 69 74 73 20 62 65 68  ter then its beh
1aa50 61 76 69 6f 72 20 69 73 20 69 64 65 6e 74 69 63  avior is identic
1aa60 61 6c 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a  al to calling.**
1aa70 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
1aa80 4e 29 20 77 68 65 72 65 20 4e 20 69 73 20 74 68  N) where N is th
1aa90 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
1aaa0 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65  er to sqlite3_re
1aab0 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 49 66 20 74  alloc()..** If t
1aac0 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  he second parame
1aad0 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72  ter to sqlite3_r
1aae0 65 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65 72 6f  ealloc() is zero
1aaf0 20 6f 72 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20   or.** negative 
1ab00 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f  then the behavio
1ab10 72 20 69 73 20 65 78 61 63 74 6c 79 20 74 68 65  r is exactly the
1ab20 20 73 61 6d 65 20 61 73 20 63 61 6c 6c 69 6e 67   same as calling
1ab30 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 72 65 65  .** sqlite3_free
1ab40 28 50 29 20 77 68 65 72 65 20 50 20 69 73 20 74  (P) where P is t
1ab50 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  he first paramet
1ab60 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65  er to sqlite3_re
1ab70 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 73 71 6c 69  alloc()..** sqli
1ab80 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65  te3_realloc() re
1ab90 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
1aba0 74 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  to a memory allo
1abb0 63 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 61 74 20  cation.** of at 
1abc0 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20 69 6e  least N bytes in
1abd0 20 73 69 7a 65 20 6f 72 20 4e 55 4c 4c 20 69 66   size or NULL if
1abe0 20 73 75 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f   sufficient memo
1abf0 72 79 20 69 73 20 75 6e 61 76 61 69 6c 61 62 6c  ry is unavailabl
1ac00 65 2e 0a 2a 2a 20 49 66 20 4d 20 69 73 20 74 68  e..** If M is th
1ac10 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70 72  e size of the pr
1ac20 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20  ior allocation, 
1ac30 74 68 65 6e 20 6d 69 6e 28 4e 2c 4d 29 20 62 79  then min(N,M) by
1ac40 74 65 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70 72  tes.** of the pr
1ac50 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61  ior allocation a
1ac60 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74  re copied into t
1ac70 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
1ac80 62 75 66 66 65 72 20 72 65 74 75 72 6e 65 64 0a  buffer returned.
1ac90 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 5f 72 65  ** by sqlite3_re
1aca0 61 6c 6c 6f 63 28 29 20 61 6e 64 20 74 68 65 20  alloc() and the 
1acb0 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e  prior allocation
1acc0 20 69 73 20 66 72 65 65 64 2e 0a 2a 2a 20 49 66   is freed..** If
1acd0 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
1ace0 28 29 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2c  () returns NULL,
1acf0 20 74 68 65 6e 20 74 68 65 20 70 72 69 6f 72 20   then the prior 
1ad00 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 73  allocation.** is
1ad10 20 6e 6f 74 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a   not freed..**.*
1ad20 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 72 65 74  * The memory ret
1ad30 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33  urned by sqlite3
1ad40 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 73 71  _malloc() and sq
1ad50 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a  lite3_realloc().
1ad60 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61 6c 69  ** is always ali
1ad70 67 6e 65 64 20 74 6f 20 61 74 20 6c 65 61 73 74  gned to at least
1ad80 20 61 6e 20 38 20 62 79 74 65 20 62 6f 75 6e 64   an 8 byte bound
1ad90 61 72 79 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a  ary. {END}.**.**
1ada0 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 6d 70   The default imp
1adb0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
1adc0 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
1add0 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 75  tion subsystem u
1ade0 73 65 73 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f  ses.** the mallo
1adf0 63 28 29 2c 20 72 65 61 6c 6c 6f 63 28 29 20 61  c(), realloc() a
1ae00 6e 64 20 66 72 65 65 28 29 20 70 72 6f 76 69 64  nd free() provid
1ae10 65 64 20 62 79 20 74 68 65 20 73 74 61 6e 64 61  ed by the standa
1ae20 72 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a  rd C library..**
1ae30 20 7b 48 31 37 33 38 32 7d 20 48 6f 77 65 76 65   {H17382} Howeve
1ae40 72 2c 20 69 66 20 53 51 4c 69 74 65 20 69 73 20  r, if SQLite is 
1ae50 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68  compiled with th
1ae60 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 4f  e.** SQLITE_MEMO
1ae70 52 59 5f 53 49 5a 45 3d 3c 69 3e 4e 4e 4e 3c 2f  RY_SIZE=<i>NNN</
1ae80 69 3e 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f  i> C preprocesso
1ae90 72 20 6d 61 63 72 6f 20 28 77 68 65 72 65 20 3c  r macro (where <
1aea0 69 3e 4e 4e 4e 3c 2f 69 3e 0a 2a 2a 20 69 73 20  i>NNN</i>.** is 
1aeb0 61 6e 20 69 6e 74 65 67 65 72 29 2c 20 74 68 65  an integer), the
1aec0 6e 20 53 51 4c 69 74 65 20 63 72 65 61 74 65 20  n SQLite create 
1aed0 61 20 73 74 61 74 69 63 20 61 72 72 61 79 20 6f  a static array o
1aee0 66 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 3c 69  f at least.** <i
1aef0 3e 4e 4e 4e 3c 2f 69 3e 20 62 79 74 65 73 20 69  >NNN</i> bytes i
1af00 6e 20 73 69 7a 65 20 61 6e 64 20 75 73 65 73 20  n size and uses 
1af10 74 68 61 74 20 61 72 72 61 79 20 66 6f 72 20 61  that array for a
1af20 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d 69  ll of its dynami
1af30 63 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  c.** memory allo
1af40 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e 20 7b 45  cation needs. {E
1af50 4e 44 7d 20 20 41 64 64 69 74 69 6f 6e 61 6c 20  ND}  Additional 
1af60 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
1af70 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 6d 61 79 20   options.** may 
1af80 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75  be added in futu
1af90 72 65 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a  re releases..**.
1afa0 2a 2a 20 49 6e 20 53 51 4c 69 74 65 20 76 65 72  ** In SQLite ver
1afb0 73 69 6f 6e 20 33 2e 35 2e 30 20 61 6e 64 20 33  sion 3.5.0 and 3
1afc0 2e 35 2e 31 2c 20 69 74 20 77 61 73 20 70 6f 73  .5.1, it was pos
1afd0 73 69 62 6c 65 20 74 6f 20 64 65 66 69 6e 65 0a  sible to define.
1afe0 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 4f 4d  ** the SQLITE_OM
1aff0 49 54 5f 4d 45 4d 4f 52 59 5f 41 4c 4c 4f 43 41  IT_MEMORY_ALLOCA
1b000 54 49 4f 4e 20 77 68 69 63 68 20 77 6f 75 6c 64  TION which would
1b010 20 63 61 75 73 65 20 74 68 65 20 62 75 69 6c 74   cause the built
1b020 2d 69 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  -in.** implement
1b030 61 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 72  ation of these r
1b040 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 6f 6d  outines to be om
1b050 69 74 74 65 64 2e 20 20 54 68 61 74 20 63 61 70  itted.  That cap
1b060 61 62 69 6c 69 74 79 0a 2a 2a 20 69 73 20 6e 6f  ability.** is no
1b070 20 6c 6f 6e 67 65 72 20 70 72 6f 76 69 64 65 64   longer provided
1b080 2e 20 20 4f 6e 6c 79 20 62 75 69 6c 74 2d 69 6e  .  Only built-in
1b090 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
1b0a0 72 73 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a  rs can be used..
1b0b0 2a 2a 0a 2a 2a 20 54 68 65 20 57 69 6e 64 6f 77  **.** The Window
1b0c0 73 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20 6c  s OS interface l
1b0d0 61 79 65 72 20 63 61 6c 6c 73 0a 2a 2a 20 74 68  ayer calls.** th
1b0e0 65 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28  e system malloc(
1b0f0 29 20 61 6e 64 20 66 72 65 65 28 29 20 64 69 72  ) and free() dir
1b100 65 63 74 6c 79 20 77 68 65 6e 20 63 6f 6e 76 65  ectly when conve
1b110 72 74 69 6e 67 0a 2a 2a 20 66 69 6c 65 6e 61 6d  rting.** filenam
1b120 65 73 20 62 65 74 77 65 65 6e 20 74 68 65 20 55  es between the U
1b130 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 75 73  TF-8 encoding us
1b140 65 64 20 62 79 20 53 51 4c 69 74 65 0a 2a 2a 20  ed by SQLite.** 
1b150 61 6e 64 20 77 68 61 74 65 76 65 72 20 66 69 6c  and whatever fil
1b160 65 6e 61 6d 65 20 65 6e 63 6f 64 69 6e 67 20 69  ename encoding i
1b170 73 20 75 73 65 64 20 62 79 20 74 68 65 20 70 61  s used by the pa
1b180 72 74 69 63 75 6c 61 72 20 57 69 6e 64 6f 77 73  rticular Windows
1b190 0a 2a 2a 20 69 6e 73 74 61 6c 6c 61 74 69 6f 6e  .** installation
1b1a0 2e 20 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  .  Memory alloca
1b1b0 74 69 6f 6e 20 65 72 72 6f 72 73 20 61 72 65 20  tion errors are 
1b1c0 64 65 74 65 63 74 65 64 2c 20 62 75 74 0a 2a 2a  detected, but.**
1b1d0 20 74 68 65 79 20 61 72 65 20 72 65 70 6f 72 74   they are report
1b1e0 65 64 20 62 61 63 6b 20 61 73 20 5b 53 51 4c 49  ed back as [SQLI
1b1f0 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 6f 72 0a  TE_CANTOPEN] or.
1b200 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52  ** [SQLITE_IOERR
1b210 5d 20 72 61 74 68 65 72 20 74 68 61 6e 20 5b 53  ] rather than [S
1b220 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a 2a  QLITE_NOMEM]..**
1b230 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a  .** INVARIANTS:.
1b240 2a 2a 0a 2a 2a 20 7b 48 31 37 33 30 33 7d 20 20  **.** {H17303}  
1b250 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  The [sqlite3_mal
1b260 6c 6f 63 28 4e 29 5d 20 69 6e 74 65 72 66 61 63  loc(N)] interfac
1b270 65 20 72 65 74 75 72 6e 73 20 65 69 74 68 65 72  e returns either
1b280 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a   a pointer to.**
1b290 20 20 20 20 20 20 20 20 20 20 20 61 20 6e 65 77             a new
1b2a0 6c 79 20 63 68 65 63 6b 65 64 2d 6f 75 74 20 62  ly checked-out b
1b2b0 6c 6f 63 6b 20 6f 66 20 61 74 20 6c 65 61 73 74  lock of at least
1b2c0 20 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f   N bytes of memo
1b2d0 72 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ry.**           
1b2e0 74 68 61 74 20 69 73 20 38 2d 62 79 74 65 20 61  that is 8-byte a
1b2f0 6c 69 67 6e 65 64 2c 20 6f 72 20 69 74 20 72 65  ligned, or it re
1b300 74 75 72 6e 73 20 4e 55 4c 4c 20 69 66 20 69 74  turns NULL if it
1b310 20 69 73 20 75 6e 61 62 6c 65 0a 2a 2a 20 20 20   is unable.**   
1b320 20 20 20 20 20 20 20 20 74 6f 20 66 75 6c 66 69          to fulfi
1b330 6c 6c 20 74 68 65 20 72 65 71 75 65 73 74 2e 0a  ll the request..
1b340 2a 2a 0a 2a 2a 20 7b 48 31 37 33 30 34 7d 20 20  **.** {H17304}  
1b350 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  The [sqlite3_mal
1b360 6c 6f 63 28 4e 29 5d 20 69 6e 74 65 72 66 61 63  loc(N)] interfac
1b370 65 20 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c  e returns a NULL
1b380 20 70 6f 69 6e 74 65 72 20 69 66 0a 2a 2a 20 20   pointer if.**  
1b390 20 20 20 20 20 20 20 20 20 4e 20 69 73 20 6c 65           N is le
1b3a0 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  ss than or equal
1b3b0 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20   to zero..**.** 
1b3c0 7b 48 31 37 33 30 35 7d 20 20 54 68 65 20 5b 73  {H17305}  The [s
1b3d0 71 6c 69 74 65 33 5f 66 72 65 65 28 50 29 5d 20  qlite3_free(P)] 
1b3e0 69 6e 74 65 72 66 61 63 65 20 72 65 6c 65 61 73  interface releas
1b3f0 65 73 20 6d 65 6d 6f 72 79 20 70 72 65 76 69 6f  es memory previo
1b400 75 73 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20 20  usly.**         
1b410 20 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20    returned from 
1b420 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
1b430 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72  )] or [sqlite3_r
1b440 65 61 6c 6c 6f 63 28 29 5d 2c 0a 2a 2a 20 20 20  ealloc()],.**   
1b450 20 20 20 20 20 20 20 20 6d 61 6b 69 6e 67 20 69          making i
1b460 74 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20  t available for 
1b470 72 65 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  reuse..**.** {H1
1b480 37 33 30 36 7d 20 20 41 20 63 61 6c 6c 20 74 6f  7306}  A call to
1b490 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 4e   [sqlite3_free(N
1b4a0 55 4c 4c 29 5d 20 69 73 20 61 20 68 61 72 6d 6c  ULL)] is a harml
1b4b0 65 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a  ess no-op..**.**
1b4c0 20 7b 48 31 37 33 31 30 7d 20 20 41 20 63 61 6c   {H17310}  A cal
1b4d0 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65  l to [sqlite3_re
1b4e0 61 6c 6c 6f 63 28 30 2c 4e 29 5d 20 69 73 20 65  alloc(0,N)] is e
1b4f0 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 61 20 63  quivalent to a c
1b500 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  all.**          
1b510 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c   to [sqlite3_mal
1b520 6c 6f 63 28 4e 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b  loc(N)]..**.** {
1b530 48 31 37 33 31 32 7d 20 20 41 20 63 61 6c 6c 20  H17312}  A call 
1b540 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c  to [sqlite3_real
1b550 6c 6f 63 28 50 2c 30 29 5d 20 69 73 20 65 71 75  loc(P,0)] is equ
1b560 69 76 61 6c 65 6e 74 20 74 6f 20 61 20 63 61 6c  ivalent to a cal
1b570 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74  l.**           t
1b580 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28  o [sqlite3_free(
1b590 50 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 33  P)]..**.** {H173
1b5a0 31 35 7d 20 20 54 68 65 20 53 51 4c 69 74 65 20  15}  The SQLite 
1b5b0 63 6f 72 65 20 75 73 65 73 20 5b 73 71 6c 69 74  core uses [sqlit
1b5c0 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 20 5b 73  e3_malloc()], [s
1b5d0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29  qlite3_realloc()
1b5e0 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ],.**           
1b5f0 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 66 72 65  and [sqlite3_fre
1b600 65 28 29 5d 20 66 6f 72 20 61 6c 6c 20 6f 66 20  e()] for all of 
1b610 69 74 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  its memory alloc
1b620 61 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 20 20 20  ation and.**    
1b630 20 20 20 20 20 20 20 64 65 61 6c 6c 6f 63 61 74         deallocat
1b640 69 6f 6e 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a  ion needs..**.**
1b650 20 7b 48 31 37 33 31 38 7d 20 20 54 68 65 20 5b   {H17318}  The [
1b660 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
1b670 50 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20  P,N)] interface 
1b680 72 65 74 75 72 6e 73 20 65 69 74 68 65 72 20 61  returns either a
1b690 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 20 20 20 20   pointer.**     
1b6a0 20 20 20 20 20 20 74 6f 20 61 20 62 6c 6f 63 6b        to a block
1b6b0 20 6f 66 20 63 68 65 63 6b 65 64 2d 6f 75 74 20   of checked-out 
1b6c0 6d 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61  memory of at lea
1b6d0 73 74 20 4e 20 62 79 74 65 73 20 69 6e 20 73 69  st N bytes in si
1b6e0 7a 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ze.**           
1b6f0 74 68 61 74 20 69 73 20 38 2d 62 79 74 65 20 61  that is 8-byte a
1b700 6c 69 67 6e 65 64 2c 20 6f 72 20 61 20 4e 55 4c  ligned, or a NUL
1b710 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a  L pointer..**.**
1b720 20 7b 48 31 37 33 32 31 7d 20 20 57 68 65 6e 20   {H17321}  When 
1b730 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63  [sqlite3_realloc
1b740 28 50 2c 4e 29 5d 20 72 65 74 75 72 6e 73 20 61  (P,N)] returns a
1b750 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74 65   non-NULL pointe
1b760 72 2c 20 69 74 20 66 69 72 73 74 0a 2a 2a 20 20  r, it first.**  
1b770 20 20 20 20 20 20 20 20 20 63 6f 70 69 65 73 20           copies 
1b780 74 68 65 20 66 69 72 73 74 20 4b 20 62 79 74 65  the first K byte
1b790 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20 66 72 6f  s of content fro
1b7a0 6d 20 50 20 69 6e 74 6f 20 74 68 65 20 6e 65 77  m P into the new
1b7b0 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ly.**           
1b7c0 61 6c 6c 6f 63 61 74 65 64 20 62 6c 6f 63 6b 2c  allocated block,
1b7d0 20 77 68 65 72 65 20 4b 20 69 73 20 74 68 65 20   where K is the 
1b7e0 6c 65 73 73 65 72 20 6f 66 20 4e 20 61 6e 64 20  lesser of N and 
1b7f0 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 20  the size of.**  
1b800 20 20 20 20 20 20 20 20 20 74 68 65 20 62 75 66           the buf
1b810 66 65 72 20 50 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  fer P..**.** {H1
1b820 37 33 32 32 7d 20 20 57 68 65 6e 20 5b 73 71 6c  7322}  When [sql
1b830 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 50 2c 4e  ite3_realloc(P,N
1b840 29 5d 20 72 65 74 75 72 6e 73 20 61 20 6e 6f 6e  )] returns a non
1b850 2d 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 69  -NULL pointer, i
1b860 74 20 66 69 72 73 74 0a 2a 2a 20 20 20 20 20 20  t first.**      
1b870 20 20 20 20 20 72 65 6c 65 61 73 65 73 20 74 68       releases th
1b880 65 20 62 75 66 66 65 72 20 50 2e 0a 2a 2a 0a 2a  e buffer P..**.*
1b890 2a 20 7b 48 31 37 33 32 33 7d 20 20 57 68 65 6e  * {H17323}  When
1b8a0 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f   [sqlite3_reallo
1b8b0 63 28 50 2c 4e 29 5d 20 72 65 74 75 72 6e 73 20  c(P,N)] returns 
1b8c0 4e 55 4c 4c 2c 20 74 68 65 20 62 75 66 66 65 72  NULL, the buffer
1b8d0 20 50 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20   P is.**        
1b8e0 20 20 20 6e 6f 74 20 6d 6f 64 69 66 69 65 64 20     not modified 
1b8f0 6f 72 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a  or released..**.
1b900 2a 2a 20 41 53 53 55 4d 50 54 49 4f 4e 53 3a 0a  ** ASSUMPTIONS:.
1b910 2a 2a 0a 2a 2a 20 7b 41 31 37 33 35 30 7d 20 20  **.** {A17350}  
1b920 54 68 65 20 70 6f 69 6e 74 65 72 20 61 72 67 75  The pointer argu
1b930 6d 65 6e 74 73 20 74 6f 20 5b 73 71 6c 69 74 65  ments to [sqlite
1b940 33 5f 66 72 65 65 28 29 5d 20 61 6e 64 20 5b 73  3_free()] and [s
1b950 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29  qlite3_realloc()
1b960 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6d  ].**           m
1b970 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 55  ust be either NU
1b980 4c 4c 20 6f 72 20 65 6c 73 65 20 70 6f 69 6e 74  LL or else point
1b990 65 72 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ers obtained fro
1b9a0 6d 20 61 20 70 72 69 6f 72 0a 2a 2a 20 20 20 20  m a prior.**    
1b9b0 20 20 20 20 20 20 20 69 6e 76 6f 63 61 74 69 6f         invocatio
1b9c0 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 6d 61  n of [sqlite3_ma
1b9d0 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69  lloc()] or [sqli
1b9e0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 74  te3_realloc()] t
1b9f0 68 61 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 20  hat have.**     
1ba00 20 20 20 20 20 20 6e 6f 74 20 79 65 74 20 62 65        not yet be
1ba10 65 6e 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a  en released..**.
1ba20 2a 2a 20 7b 41 31 37 33 35 31 7d 20 20 54 68 65  ** {A17351}  The
1ba30 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73   application mus
1ba40 74 20 6e 6f 74 20 72 65 61 64 20 6f 72 20 77 72  t not read or wr
1ba50 69 74 65 20 61 6e 79 20 70 61 72 74 20 6f 66 0a  ite any part of.
1ba60 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 20 62  **           a b
1ba70 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 61  lock of memory a
1ba80 66 74 65 72 20 69 74 20 68 61 73 20 62 65 65 6e  fter it has been
1ba90 20 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67 0a   released using.
1baa0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b 73 71  **           [sq
1bab0 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20 6f 72  lite3_free()] or
1bac0 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f   [sqlite3_reallo
1bad0 63 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  c()]..*/.SQLITE_
1bae0 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  API void *sqlite
1baf0 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53  3_malloc(int);.S
1bb00 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
1bb10 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
1bb20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  void*, int);.SQL
1bb30 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
1bb40 69 74 65 33 5f 66 72 65 65 28 76 6f 69 64 2a 29  ite3_free(void*)
1bb50 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
1bb60 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61  F: Memory Alloca
1bb70 74 6f 72 20 53 74 61 74 69 73 74 69 63 73 20 7b  tor Statistics {
1bb80 48 31 37 33 37 30 7d 20 3c 53 33 30 32 31 30 3e  H17370} <S30210>
1bb90 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 70 72  .**.** SQLite pr
1bba0 6f 76 69 64 65 73 20 74 68 65 73 65 20 74 77 6f  ovides these two
1bbb0 20 69 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20   interfaces for 
1bbc0 72 65 70 6f 72 74 69 6e 67 20 6f 6e 20 74 68 65  reporting on the
1bbd0 20 73 74 61 74 75 73 0a 2a 2a 20 6f 66 20 74 68   status.** of th
1bbe0 65 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  e [sqlite3_mallo
1bbf0 63 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 66  c()], [sqlite3_f
1bc00 72 65 65 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c  ree()], and [sql
1bc10 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 0a  ite3_realloc()].
1bc20 2a 2a 20 72 6f 75 74 69 6e 65 73 2c 20 77 68 69  ** routines, whi
1bc30 63 68 20 66 6f 72 6d 20 74 68 65 20 62 75 69 6c  ch form the buil
1bc40 74 2d 69 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  t-in memory allo
1bc50 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d  cation subsystem
1bc60 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e  ..**.** INVARIAN
1bc70 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 33 37  TS:.**.** {H1737
1bc80 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  1} The [sqlite3_
1bc90 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 5d 20 72  memory_used()] r
1bca0 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74  outine returns t
1bcb0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
1bcc0 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  es.**          o
1bcd0 66 20 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e 74  f memory current
1bce0 6c 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 28  ly outstanding (
1bcf0 6d 61 6c 6c 6f 63 65 64 20 62 75 74 20 6e 6f 74  malloced but not
1bd00 20 66 72 65 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 7b   freed)..**.** {
1bd10 48 31 37 33 37 33 7d 20 54 68 65 20 5b 73 71 6c  H17373} The [sql
1bd20 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68  ite3_memory_high
1bd30 77 61 74 65 72 28 29 5d 20 72 6f 75 74 69 6e 65  water()] routine
1bd40 20 72 65 74 75 72 6e 73 20 74 68 65 20 6d 61 78   returns the max
1bd50 69 6d 75 6d 0a 2a 2a 20 20 20 20 20 20 20 20 20  imum.**         
1bd60 20 76 61 6c 75 65 20 6f 66 20 5b 73 71 6c 69 74   value of [sqlit
1bd70 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29  e3_memory_used()
1bd80 5d 20 73 69 6e 63 65 20 74 68 65 20 68 69 67 68  ] since the high
1bd90 2d 77 61 74 65 72 20 6d 61 72 6b 0a 2a 2a 20 20  -water mark.**  
1bda0 20 20 20 20 20 20 20 20 77 61 73 20 6c 61 73 74          was last
1bdb0 20 72 65 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48   reset..**.** {H
1bdc0 31 37 33 37 34 7d 20 54 68 65 20 76 61 6c 75 65  17374} The value
1bdd0 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73  s returned by [s
1bde0 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73  qlite3_memory_us
1bdf0 65 64 28 29 5d 20 61 6e 64 0a 2a 2a 20 20 20 20  ed()] and.**    
1be00 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 6d        [sqlite3_m
1be10 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 28  emory_highwater(
1be20 29 5d 20 69 6e 63 6c 75 64 65 20 61 6e 79 20 6f  )] include any o
1be30 76 65 72 68 65 61 64 0a 2a 2a 20 20 20 20 20 20  verhead.**      
1be40 20 20 20 20 61 64 64 65 64 20 62 79 20 53 51 4c      added by SQL
1be50 69 74 65 20 69 6e 20 69 74 73 20 69 6d 70 6c 65  ite in its imple
1be60 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 5b 73 71  mentation of [sq
1be70 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2c  lite3_malloc()],
1be80 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62 75 74  .**          but
1be90 20 6e 6f 74 20 6f 76 65 72 68 65 61 64 20 61 64   not overhead ad
1bea0 64 65 64 20 62 79 20 74 68 65 20 61 6e 79 20 75  ded by the any u
1beb0 6e 64 65 72 6c 79 69 6e 67 20 73 79 73 74 65 6d  nderlying system
1bec0 20 6c 69 62 72 61 72 79 0a 2a 2a 20 20 20 20 20   library.**     
1bed0 20 20 20 20 20 72 6f 75 74 69 6e 65 73 20 74 68       routines th
1bee0 61 74 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  at [sqlite3_mall
1bef0 6f 63 28 29 5d 20 6d 61 79 20 63 61 6c 6c 2e 0a  oc()] may call..
1bf00 2a 2a 0a 2a 2a 20 7b 48 31 37 33 37 35 7d 20 54  **.** {H17375} T
1bf10 68 65 20 6d 65 6d 6f 72 79 20 68 69 67 68 2d 77  he memory high-w
1bf20 61 74 65 72 20 6d 61 72 6b 20 69 73 20 72 65 73  ater mark is res
1bf30 65 74 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e  et to the curren
1bf40 74 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 20 20  t value of.**   
1bf50 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
1bf60 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 5d 20 69  memory_used()] i
1bf70 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 20 74 68  f and only if th
1bf80 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a  e parameter to.*
1bf90 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
1bfa0 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77  te3_memory_highw
1bfb0 61 74 65 72 28 29 5d 20 69 73 20 74 72 75 65 2e  ater()] is true.
1bfc0 20 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75    The value retu
1bfd0 72 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20  rned.**         
1bfe0 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d   by [sqlite3_mem
1bff0 6f 72 79 5f 68 69 67 68 77 61 74 65 72 28 31 29  ory_highwater(1)
1c000 5d 20 69 73 20 74 68 65 20 68 69 67 68 2d 77 61  ] is the high-wa
1c010 74 65 72 20 6d 61 72 6b 0a 2a 2a 20 20 20 20 20  ter mark.**     
1c020 20 20 20 20 20 70 72 69 6f 72 20 74 6f 20 74 68       prior to th
1c030 65 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49  e reset..*/.SQLI
1c040 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69  TE_API sqlite3_i
1c050 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d  nt64 sqlite3_mem
1c060 6f 72 79 5f 75 73 65 64 28 76 6f 69 64 29 3b 0a  ory_used(void);.
1c070 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
1c080 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e3_int64 sqlite3
1c090 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65  _memory_highwate
1c0a0 72 28 69 6e 74 20 72 65 73 65 74 46 6c 61 67 29  r(int resetFlag)
1c0b0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
1c0c0 46 3a 20 50 73 65 75 64 6f 2d 52 61 6e 64 6f 6d  F: Pseudo-Random
1c0d0 20 4e 75 6d 62 65 72 20 47 65 6e 65 72 61 74 6f   Number Generato
1c0e0 72 20 7b 48 31 37 33 39 30 7d 20 3c 53 32 30 30  r {H17390} <S200
1c0f0 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65  00>.**.** SQLite
1c100 20 63 6f 6e 74 61 69 6e 73 20 61 20 68 69 67 68   contains a high
1c110 2d 71 75 61 6c 69 74 79 20 70 73 65 75 64 6f 2d  -quality pseudo-
1c120 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65  random number ge
1c130 6e 65 72 61 74 6f 72 20 28 50 52 4e 47 29 20 75  nerator (PRNG) u
1c140 73 65 64 20 74 6f 0a 2a 2a 20 73 65 6c 65 63 74  sed to.** select
1c150 20 72 61 6e 64 6f 6d 20 52 4f 57 49 44 73 20 77   random ROWIDs w
1c160 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 6e 65  hen inserting ne
1c170 77 20 72 65 63 6f 72 64 73 20 69 6e 74 6f 20 61  w records into a
1c180 20 74 61 62 6c 65 20 74 68 61 74 0a 2a 2a 20 61   table that.** a
1c190 6c 72 65 61 64 79 20 75 73 65 73 20 74 68 65 20  lready uses the 
1c1a0 6c 61 72 67 65 73 74 20 70 6f 73 73 69 62 6c 65  largest possible
1c1b0 20 52 4f 57 49 44 2e 20 20 54 68 65 20 50 52 4e   ROWID.  The PRN
1c1c0 47 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20 66  G is also used f
1c1d0 6f 72 0a 2a 2a 20 74 68 65 20 62 75 69 6c 64 2d  or.** the build-
1c1e0 69 6e 20 72 61 6e 64 6f 6d 28 29 20 61 6e 64 20  in random() and 
1c1f0 72 61 6e 64 6f 6d 62 6c 6f 62 28 29 20 53 51 4c  randomblob() SQL
1c200 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 69   functions.  Thi
1c210 73 20 69 6e 74 65 72 66 61 63 65 20 61 6c 6c 6f  s interface allo
1c220 77 73 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f  ws.** applicatio
1c230 6e 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 65  ns to access the
1c240 20 73 61 6d 65 20 50 52 4e 47 20 66 6f 72 20 6f   same PRNG for o
1c250 74 68 65 72 20 70 75 72 70 6f 73 65 73 2e 0a 2a  ther purposes..*
1c260 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 74  *.** A call to t
1c270 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74 6f 72  his routine stor
1c280 65 73 20 4e 20 62 79 74 65 73 20 6f 66 20 72 61  es N bytes of ra
1c290 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 62 75  ndomness into bu
1c2a0 66 66 65 72 20 50 2e 0a 2a 2a 0a 2a 2a 20 54 68  ffer P..**.** Th
1c2b0 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 69  e first time thi
1c2c0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e 76  s routine is inv
1c2d0 6f 6b 65 64 20 28 65 69 74 68 65 72 20 69 6e 74  oked (either int
1c2e0 65 72 6e 61 6c 6c 79 20 6f 72 20 62 79 0a 2a 2a  ernally or by.**
1c2f0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
1c300 29 20 74 68 65 20 50 52 4e 47 20 69 73 20 73 65  ) the PRNG is se
1c310 65 64 65 64 20 75 73 69 6e 67 20 72 61 6e 64 6f  eded using rando
1c320 6d 6e 65 73 73 20 6f 62 74 61 69 6e 65 64 0a 2a  mness obtained.*
1c330 2a 20 66 72 6f 6d 20 74 68 65 20 78 52 61 6e 64  * from the xRand
1c340 6f 6d 6e 65 73 73 20 6d 65 74 68 6f 64 20 6f 66  omness method of
1c350 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71   the default [sq
1c360 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63  lite3_vfs] objec
1c370 74 2e 0a 2a 2a 20 4f 6e 20 61 6c 6c 20 73 75 62  t..** On all sub
1c380 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 69  sequent invocati
1c390 6f 6e 73 2c 20 74 68 65 20 70 73 65 75 64 6f 2d  ons, the pseudo-
1c3a0 72 61 6e 64 6f 6d 6e 65 73 73 20 69 73 20 67 65  randomness is ge
1c3b0 6e 65 72 61 74 65 64 0a 2a 2a 20 69 6e 74 65 72  nerated.** inter
1c3c0 6e 61 6c 6c 79 20 61 6e 64 20 77 69 74 68 6f 75  nally and withou
1c3d0 74 20 72 65 63 6f 75 72 73 65 20 74 6f 20 74 68  t recourse to th
1c3e0 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20  e [sqlite3_vfs] 
1c3f0 78 52 61 6e 64 6f 6d 6e 65 73 73 0a 2a 2a 20 6d  xRandomness.** m
1c400 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56  ethod..**.** INV
1c410 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b  ARIANTS:.**.** {
1c420 48 31 37 33 39 32 7d 20 54 68 65 20 5b 73 71 6c  H17392} The [sql
1c430 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28  ite3_randomness(
1c440 4e 2c 50 29 5d 20 69 6e 74 65 72 66 61 63 65 20  N,P)] interface 
1c450 77 72 69 74 65 73 20 4e 20 62 79 74 65 73 20 6f  writes N bytes o
1c460 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 68 69  f.**          hi
1c470 67 68 2d 71 75 61 6c 69 74 79 20 70 73 65 75 64  gh-quality pseud
1c480 6f 2d 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74  o-randomness int
1c490 6f 20 62 75 66 66 65 72 20 50 2e 0a 2a 2f 0a 53  o buffer P..*/.S
1c4a0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
1c4b0 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73  qlite3_randomnes
1c4c0 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a 50  s(int N, void *P
1c4d0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
1c4e0 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d 65  EF: Compile-Time
1c4f0 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 43   Authorization C
1c500 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 35 30 30  allbacks {H12500
1c510 7d 20 3c 53 37 30 31 30 30 3e 0a 2a 2a 0a 2a 2a  } <S70100>.**.**
1c520 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
1c530 67 69 73 74 65 72 73 20 61 20 61 75 74 68 6f 72  gisters a author
1c540 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 77 69  izer callback wi
1c550 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a  th a particular.
1c560 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  ** [database con
1c570 6e 65 63 74 69 6f 6e 5d 2c 20 73 75 70 70 6c 69  nection], suppli
1c580 65 64 20 69 6e 20 74 68 65 20 66 69 72 73 74 20  ed in the first 
1c590 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65  argument..** The
1c5a0 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c   authorizer call
1c5b0 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20  back is invoked 
1c5c0 61 73 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  as SQL statement
1c5d0 73 20 61 72 65 20 62 65 69 6e 67 20 63 6f 6d 70  s are being comp
1c5e0 69 6c 65 64 0a 2a 2a 20 62 79 20 5b 73 71 6c 69  iled.** by [sqli
1c5f0 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f  te3_prepare()] o
1c600 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 20 5b  r its variants [
1c610 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
1c620 76 32 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74  v2()],.** [sqlit
1c630 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20  e3_prepare16()] 
1c640 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  and [sqlite3_pre
1c650 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 20 20 41  pare16_v2()].  A
1c660 74 20 76 61 72 69 6f 75 73 0a 2a 2a 20 70 6f 69  t various.** poi
1c670 6e 74 73 20 64 75 72 69 6e 67 20 74 68 65 20 63  nts during the c
1c680 6f 6d 70 69 6c 61 74 69 6f 6e 20 70 72 6f 63 65  ompilation proce
1c690 73 73 2c 20 61 73 20 6c 6f 67 69 63 20 69 73 20  ss, as logic is 
1c6a0 62 65 69 6e 67 20 63 72 65 61 74 65 64 0a 2a 2a  being created.**
1c6b0 20 74 6f 20 70 65 72 66 6f 72 6d 20 76 61 72 69   to perform vari
1c6c0 6f 75 73 20 61 63 74 69 6f 6e 73 2c 20 74 68 65  ous actions, the
1c6d0 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c   authorizer call
1c6e0 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20  back is invoked 
1c6f0 74 6f 0a 2a 2a 20 73 65 65 20 69 66 20 74 68 6f  to.** see if tho
1c700 73 65 20 61 63 74 69 6f 6e 73 20 61 72 65 20 61  se actions are a
1c710 6c 6c 6f 77 65 64 2e 20 20 54 68 65 20 61 75 74  llowed.  The aut
1c720 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
1c730 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 72   should.** retur
1c740 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 74 6f  n [SQLITE_OK] to
1c750 20 61 6c 6c 6f 77 20 74 68 65 20 61 63 74 69 6f   allow the actio
1c760 6e 2c 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52  n, [SQLITE_IGNOR
1c770 45 5d 20 74 6f 20 64 69 73 61 6c 6c 6f 77 20 74  E] to disallow t
1c780 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 61  he.** specific a
1c790 63 74 69 6f 6e 20 62 75 74 20 61 6c 6c 6f 77 20  ction but allow 
1c7a0 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
1c7b0 74 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74 6f  t to continue to
1c7c0 20 62 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 2c   be.** compiled,
1c7d0 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59   or [SQLITE_DENY
1c7e0 5d 20 74 6f 20 63 61 75 73 65 20 74 68 65 20 65  ] to cause the e
1c7f0 6e 74 69 72 65 20 53 51 4c 20 73 74 61 74 65 6d  ntire SQL statem
1c800 65 6e 74 20 74 6f 20 62 65 0a 2a 2a 20 72 65 6a  ent to be.** rej
1c810 65 63 74 65 64 20 77 69 74 68 20 61 6e 20 65 72  ected with an er
1c820 72 6f 72 2e 20 20 49 66 20 74 68 65 20 61 75 74  ror.  If the aut
1c830 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
1c840 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 79 20   returns.** any 
1c850 76 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61 6e  value other than
1c860 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d   [SQLITE_IGNORE]
1c870 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 6f  , [SQLITE_OK], o
1c880 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d 0a  r [SQLITE_DENY].
1c890 2a 2a 20 74 68 65 6e 20 74 68 65 20 5b 73 71 6c  ** then the [sql
1c8a0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
1c8b0 29 5d 20 6f 72 20 65 71 75 69 76 61 6c 65 6e 74  )] or equivalent
1c8c0 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69 67 67   call that trigg
1c8d0 65 72 65 64 0a 2a 2a 20 74 68 65 20 61 75 74 68  ered.** the auth
1c8e0 6f 72 69 7a 65 72 20 77 69 6c 6c 20 66 61 69 6c  orizer will fail
1c8f0 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d   with an error m
1c900 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 57 68  essage..**.** Wh
1c910 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  en the callback 
1c920 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f  returns [SQLITE_
1c930 4f 4b 5d 2c 20 74 68 61 74 20 6d 65 61 6e 73 20  OK], that means 
1c940 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a  the operation.**
1c950 20 72 65 71 75 65 73 74 65 64 20 69 73 20 6f 6b   requested is ok
1c960 2e 20 20 57 68 65 6e 20 74 68 65 20 63 61 6c 6c  .  When the call
1c970 62 61 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 51  back returns [SQ
1c980 4c 49 54 45 5f 44 45 4e 59 5d 2c 20 74 68 65 0a  LITE_DENY], the.
1c990 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  ** [sqlite3_prep
1c9a0 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71 75  are_v2()] or equ
1c9b0 69 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68 61  ivalent call tha
1c9c0 74 20 74 72 69 67 67 65 72 65 64 20 74 68 65 0a  t triggered the.
1c9d0 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 77 69  ** authorizer wi
1c9e0 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 20  ll fail with an 
1c9f0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 65 78  error message ex
1ca00 70 6c 61 69 6e 69 6e 67 20 74 68 61 74 0a 2a 2a  plaining that.**
1ca10 20 61 63 63 65 73 73 20 69 73 20 64 65 6e 69 65   access is denie
1ca20 64 2e 20 20 49 66 20 74 68 65 20 61 75 74 68 6f  d.  If the autho
1ca30 72 69 7a 65 72 20 63 6f 64 65 20 69 73 20 5b 53  rizer code is [S
1ca40 51 4c 49 54 45 5f 52 45 41 44 5d 0a 2a 2a 20 61  QLITE_READ].** a
1ca50 6e 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  nd the callback 
1ca60 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f  returns [SQLITE_
1ca70 49 47 4e 4f 52 45 5d 20 74 68 65 6e 20 74 68 65  IGNORE] then the
1ca80 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74  .** [prepared st
1ca90 61 74 65 6d 65 6e 74 5d 20 73 74 61 74 65 6d 65  atement] stateme
1caa0 6e 74 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65  nt is constructe
1cab0 64 20 74 6f 20 73 75 62 73 74 69 74 75 74 65 0a  d to substitute.
1cac0 2a 2a 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20  ** a NULL value 
1cad0 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 65 20  in place of the 
1cae0 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61  table column tha
1caf0 74 20 77 6f 75 6c 64 20 68 61 76 65 0a 2a 2a 20  t would have.** 
1cb00 62 65 65 6e 20 72 65 61 64 20 69 66 20 5b 53 51  been read if [SQ
1cb10 4c 49 54 45 5f 4f 4b 5d 20 68 61 64 20 62 65 65  LITE_OK] had bee
1cb20 6e 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65  n returned.  The
1cb30 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d   [SQLITE_IGNORE]
1cb40 0a 2a 2a 20 72 65 74 75 72 6e 20 63 61 6e 20 62  .** return can b
1cb50 65 20 75 73 65 64 20 74 6f 20 64 65 6e 79 20 61  e used to deny a
1cb60 6e 20 75 6e 74 72 75 73 74 65 64 20 75 73 65 72  n untrusted user
1cb70 20 61 63 63 65 73 73 20 74 6f 20 69 6e 64 69 76   access to indiv
1cb80 69 64 75 61 6c 0a 2a 2a 20 63 6f 6c 75 6d 6e 73  idual.** columns
1cb90 20 6f 66 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 0a   of a table..**.
1cba0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72  ** The first par
1cbb0 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 75  ameter to the au
1cbc0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
1cbd0 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74  k is a copy of t
1cbe0 68 65 20 74 68 69 72 64 0a 2a 2a 20 70 61 72 61  he third.** para
1cbf0 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c  meter to the sql
1cc00 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69  ite3_set_authori
1cc10 7a 65 72 28 29 20 69 6e 74 65 72 66 61 63 65 2e  zer() interface.
1cc20 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61   The second para
1cc30 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20  meter.** to the 
1cc40 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 69  callback is an i
1cc50 6e 74 65 67 65 72 20 5b 53 51 4c 49 54 45 5f 43  nteger [SQLITE_C
1cc60 4f 50 59 20 7c 20 61 63 74 69 6f 6e 20 63 6f 64  OPY | action cod
1cc70 65 5d 20 74 68 61 74 20 73 70 65 63 69 66 69 65  e] that specifie
1cc80 73 0a 2a 2a 20 74 68 65 20 70 61 72 74 69 63 75  s.** the particu
1cc90 6c 61 72 20 61 63 74 69 6f 6e 20 74 6f 20 62 65  lar action to be
1cca0 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 54 68 65   authorized. The
1ccb0 20 74 68 69 72 64 20 74 68 72 6f 75 67 68 20 73   third through s
1ccc0 69 78 74 68 20 70 61 72 61 6d 65 74 65 72 73 0a  ixth parameters.
1ccd0 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61  ** to the callba
1cce0 63 6b 20 61 72 65 20 7a 65 72 6f 2d 74 65 72 6d  ck are zero-term
1ccf0 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73 20 74  inated strings t
1cd00 68 61 74 20 63 6f 6e 74 61 69 6e 20 61 64 64 69  hat contain addi
1cd10 74 69 6f 6e 61 6c 0a 2a 2a 20 64 65 74 61 69 6c  tional.** detail
1cd20 73 20 61 62 6f 75 74 20 74 68 65 20 61 63 74 69  s about the acti
1cd30 6f 6e 20 74 6f 20 62 65 20 61 75 74 68 6f 72 69  on to be authori
1cd40 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 75  zed..**.** An au
1cd50 74 68 6f 72 69 7a 65 72 20 69 73 20 75 73 65 64  thorizer is used
1cd60 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70   when [sqlite3_p
1cd70 72 65 70 61 72 65 20 7c 20 70 72 65 70 61 72 69  repare | prepari
1cd80 6e 67 5d 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65  ng].** SQL state
1cd90 6d 65 6e 74 73 20 66 72 6f 6d 20 61 6e 20 75 6e  ments from an un
1cda0 74 72 75 73 74 65 64 20 73 6f 75 72 63 65 2c 20  trusted source, 
1cdb0 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74  to ensure that t
1cdc0 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
1cdd0 73 0a 2a 2a 20 64 6f 20 6e 6f 74 20 74 72 79 20  s.** do not try 
1cde0 74 6f 20 61 63 63 65 73 73 20 64 61 74 61 20 74  to access data t
1cdf0 68 65 79 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f  hey are not allo
1ce00 77 65 64 20 74 6f 20 73 65 65 2c 20 6f 72 20 74  wed to see, or t
1ce10 68 61 74 20 74 68 65 79 20 64 6f 20 6e 6f 74 0a  hat they do not.
1ce20 2a 2a 20 74 72 79 20 74 6f 20 65 78 65 63 75 74  ** try to execut
1ce30 65 20 6d 61 6c 69 63 69 6f 75 73 20 73 74 61 74  e malicious stat
1ce40 65 6d 65 6e 74 73 20 74 68 61 74 20 64 61 6d 61  ements that dama
1ce50 67 65 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  ge the database.
1ce60 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65    For.** example
1ce70 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e  , an application
1ce80 20 6d 61 79 20 61 6c 6c 6f 77 20 61 20 75 73 65   may allow a use
1ce90 72 20 74 6f 20 65 6e 74 65 72 20 61 72 62 69 74  r to enter arbit
1cea0 72 61 72 79 0a 2a 2a 20 53 51 4c 20 71 75 65 72  rary.** SQL quer
1ceb0 69 65 73 20 66 6f 72 20 65 76 61 6c 75 61 74 69  ies for evaluati
1cec0 6f 6e 20 62 79 20 61 20 64 61 74 61 62 61 73 65  on by a database
1ced0 2e 20 20 42 75 74 20 74 68 65 20 61 70 70 6c 69  .  But the appli
1cee0 63 61 74 69 6f 6e 20 64 6f 65 73 0a 2a 2a 20 6e  cation does.** n
1cef0 6f 74 20 77 61 6e 74 20 74 68 65 20 75 73 65 72  ot want the user
1cf00 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 6d   to be able to m
1cf10 61 6b 65 20 61 72 62 69 74 72 61 72 79 20 63 68  ake arbitrary ch
1cf20 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20  anges to the.** 
1cf30 64 61 74 61 62 61 73 65 2e 20 20 41 6e 20 61 75  database.  An au
1cf40 74 68 6f 72 69 7a 65 72 20 63 6f 75 6c 64 20 74  thorizer could t
1cf50 68 65 6e 20 62 65 20 70 75 74 20 69 6e 20 70 6c  hen be put in pl
1cf60 61 63 65 20 77 68 69 6c 65 20 74 68 65 0a 2a 2a  ace while the.**
1cf70 20 75 73 65 72 2d 65 6e 74 65 72 65 64 20 53 51   user-entered SQ
1cf80 4c 20 69 73 20 62 65 69 6e 67 20 5b 73 71 6c 69  L is being [sqli
1cf90 74 65 33 5f 70 72 65 70 61 72 65 20 7c 20 70 72  te3_prepare | pr
1cfa0 65 70 61 72 65 64 5d 20 74 68 61 74 0a 2a 2a 20  epared] that.** 
1cfb0 64 69 73 61 6c 6c 6f 77 73 20 65 76 65 72 79 74  disallows everyt
1cfc0 68 69 6e 67 20 65 78 63 65 70 74 20 5b 53 45 4c  hing except [SEL
1cfd0 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 73 2e  ECT] statements.
1cfe0 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69  .**.** Applicati
1cff0 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f  ons that need to
1d000 20 70 72 6f 63 65 73 73 20 53 51 4c 20 66 72 6f   process SQL fro
1d010 6d 20 75 6e 74 72 75 73 74 65 64 20 73 6f 75 72  m untrusted sour
1d020 63 65 73 0a 2a 2a 20 6d 69 67 68 74 20 61 6c 73  ces.** might als
1d030 6f 20 63 6f 6e 73 69 64 65 72 20 6c 6f 77 65 72  o consider lower
1d040 69 6e 67 20 72 65 73 6f 75 72 63 65 20 6c 69 6d  ing resource lim
1d050 69 74 73 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  its using [sqlit
1d060 65 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 61  e3_limit()].** a
1d070 6e 64 20 6c 69 6d 69 74 69 6e 67 20 64 61 74 61  nd limiting data
1d080 62 61 73 65 20 73 69 7a 65 20 75 73 69 6e 67 20  base size using 
1d090 74 68 65 20 5b 6d 61 78 5f 70 61 67 65 5f 63 6f  the [max_page_co
1d0a0 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 0a 2a 2a  unt] [PRAGMA].**
1d0b0 20 69 6e 20 61 64 64 69 74 69 6f 6e 20 74 6f 20   in addition to 
1d0c0 75 73 69 6e 67 20 61 6e 20 61 75 74 68 6f 72 69  using an authori
1d0d0 7a 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20  zer..**.** Only 
1d0e0 61 20 73 69 6e 67 6c 65 20 61 75 74 68 6f 72 69  a single authori
1d0f0 7a 65 72 20 63 61 6e 20 62 65 20 69 6e 20 70 6c  zer can be in pl
1d100 61 63 65 20 6f 6e 20 61 20 64 61 74 61 62 61 73  ace on a databas
1d110 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20  e connection.** 
1d120 61 74 20 61 20 74 69 6d 65 2e 20 20 45 61 63 68  at a time.  Each
1d130 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
1d140 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20  _set_authorizer 
1d150 6f 76 65 72 72 69 64 65 73 20 74 68 65 0a 2a 2a  overrides the.**
1d160 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 2e 20   previous call. 
1d170 20 44 69 73 61 62 6c 65 20 74 68 65 20 61 75 74   Disable the aut
1d180 68 6f 72 69 7a 65 72 20 62 79 20 69 6e 73 74 61  horizer by insta
1d190 6c 6c 69 6e 67 20 61 20 4e 55 4c 4c 20 63 61 6c  lling a NULL cal
1d1a0 6c 62 61 63 6b 2e 0a 2a 2a 20 54 68 65 20 61 75  lback..** The au
1d1b0 74 68 6f 72 69 7a 65 72 20 69 73 20 64 69 73 61  thorizer is disa
1d1c0 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e  bled by default.
1d1d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
1d1e0 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 6d  rizer callback m
1d1f0 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68  ust not do anyth
1d200 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f  ing that will mo
1d210 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61  dify.** the data
1d220 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
1d230 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65  that invoked the
1d240 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c   authorizer call
1d250 62 61 63 6b 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68  back..** Note th
1d260 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  at [sqlite3_prep
1d270 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73  are_v2()] and [s
1d280 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 62  qlite3_step()] b
1d290 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 69 72  oth modify their
1d2a0 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e  .** database con
1d2b0 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 65  nections for the
1d2c0 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f 64   meaning of "mod
1d2d0 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 61 72  ify" in this par
1d2e0 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 57 68  agraph..**.** Wh
1d2f0 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  en [sqlite3_prep
1d300 61 72 65 5f 76 32 28 29 5d 20 69 73 20 75 73 65  are_v2()] is use
1d310 64 20 74 6f 20 70 72 65 70 61 72 65 20 61 20 73  d to prepare a s
1d320 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 0a 2a 2a  tatement, the.**
1d330 20 73 74 61 74 65 6d 65 6e 74 20 6d 69 67 68 74   statement might
1d340 20 62 65 20 72 65 70 72 65 70 61 72 65 64 20 64   be reprepared d
1d350 75 72 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73  uring [sqlite3_s
1d360 74 65 70 28 29 5d 20 64 75 65 20 74 6f 20 61 20  tep()] due to a 
1d370 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e 67  .** schema chang
1d380 65 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 61  e.  Hence, the a
1d390 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c  pplication shoul
1d3a0 64 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68  d ensure that th
1d3b0 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 75 74  e.** correct aut
1d3c0 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
1d3d0 20 72 65 6d 61 69 6e 73 20 69 6e 20 70 6c 61 63   remains in plac
1d3e0 65 20 64 75 72 69 6e 67 20 74 68 65 20 5b 73 71  e during the [sq
1d3f0 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 0a 2a  lite3_step()]..*
1d400 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74  *.** Note that t
1d410 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61  he authorizer ca
1d420 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65  llback is invoke
1d430 64 20 6f 6e 6c 79 20 64 75 72 69 6e 67 0a 2a 2a  d only during.**
1d440 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
1d450 65 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72 69  e()] or its vari
1d460 61 6e 74 73 2e 20 20 41 75 74 68 6f 72 69 7a 61  ants.  Authoriza
1d470 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20 70  tion is not.** p
1d480 65 72 66 6f 72 6d 65 64 20 64 75 72 69 6e 67 20  erformed during 
1d490 73 74 61 74 65 6d 65 6e 74 20 65 76 61 6c 75 61  statement evalua
1d4a0 74 69 6f 6e 20 69 6e 20 5b 73 71 6c 69 74 65 33  tion in [sqlite3
1d4b0 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  _step()]..**.** 
1d4c0 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a  INVARIANTS:.**.*
1d4d0 2a 20 7b 48 31 32 35 30 31 7d 20 54 68 65 20 5b  * {H12501} The [
1d4e0 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68  sqlite3_set_auth
1d4f0 6f 72 69 7a 65 72 28 44 2c 2e 2e 2e 29 5d 20 69  orizer(D,...)] i
1d500 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74 65  nterface registe
1d510 72 73 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 20  rs a.**         
1d520 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c   authorizer call
1d530 62 61 63 6b 20 77 69 74 68 20 64 61 74 61 62 61  back with databa
1d540 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 44 2e  se connection D.
1d550 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30 32 7d 20  .**.** {H12502} 
1d560 54 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63  The authorizer c
1d570 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b  allback is invok
1d580 65 64 20 61 73 20 53 51 4c 20 73 74 61 74 65 6d  ed as SQL statem
1d590 65 6e 74 73 20 61 72 65 0a 2a 2a 20 20 20 20 20  ents are.**     
1d5a0 20 20 20 20 20 62 65 69 6e 67 20 70 61 72 73 65       being parse
1d5b0 65 64 20 61 6e 64 20 63 6f 6d 70 69 6c 65 64 2e  ed and compiled.
1d5c0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30 33 7d 20  .**.** {H12503} 
1d5d0 49 66 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65  If the authorize
1d5e0 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72  r callback retur
1d5f0 6e 73 20 61 6e 79 20 76 61 6c 75 65 20 6f 74 68  ns any value oth
1d600 65 72 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20  er than.**      
1d610 20 20 20 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f      [SQLITE_IGNO
1d620 52 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d  RE], [SQLITE_OK]
1d630 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e  , or [SQLITE_DEN
1d640 59 5d 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20  Y], then.**     
1d650 20 20 20 20 20 74 68 65 20 61 70 70 6c 69 63 61       the applica
1d660 74 69 6f 6e 20 69 6e 74 65 72 66 61 63 65 20 63  tion interface c
1d670 61 6c 6c 20 74 68 61 74 20 63 61 75 73 65 64 0a  all that caused.
1d680 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20  **          the 
1d690 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62  authorizer callb
1d6a0 61 63 6b 20 74 6f 20 72 75 6e 20 73 68 61 6c 6c  ack to run shall
1d6b0 20 66 61 69 6c 20 77 69 74 68 20 61 6e 0a 2a 2a   fail with an.**
1d6c0 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54            [SQLIT
1d6d0 45 5f 45 52 52 4f 52 5d 20 65 72 72 6f 72 20 63  E_ERROR] error c
1d6e0 6f 64 65 20 61 6e 64 20 61 6e 20 61 70 70 72 6f  ode and an appro
1d6f0 70 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65 73  priate error mes
1d700 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  sage..**.** {H12
1d710 35 30 34 7d 20 57 68 65 6e 20 74 68 65 20 61 75  504} When the au
1d720 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
1d730 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54  k returns [SQLIT
1d740 45 5f 4f 4b 5d 2c 20 74 68 65 20 6f 70 65 72 61  E_OK], the opera
1d750 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  tion.**         
1d760 20 64 65 73 63 72 69 62 65 64 20 69 73 20 70 72   described is pr
1d770 6f 63 65 73 73 65 64 20 6e 6f 72 6d 61 6c 6c 79  ocessed normally
1d780 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30 35 7d  ..**.** {H12505}
1d790 20 57 68 65 6e 20 74 68 65 20 61 75 74 68 6f 72   When the author
1d7a0 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65  izer callback re
1d7b0 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 44 45  turns [SQLITE_DE
1d7c0 4e 59 5d 2c 20 74 68 65 0a 2a 2a 20 20 20 20 20  NY], the.**     
1d7d0 20 20 20 20 20 61 70 70 6c 69 63 61 74 69 6f 6e       application
1d7e0 20 69 6e 74 65 72 66 61 63 65 20 63 61 6c 6c 20   interface call 
1d7f0 74 68 61 74 20 63 61 75 73 65 64 20 74 68 65 0a  that caused the.
1d800 2a 2a 20 20 20 20 20 20 20 20 20 20 61 75 74 68  **          auth
1d810 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20  orizer callback 
1d820 74 6f 20 72 75 6e 20 73 68 61 6c 6c 20 66 61 69  to run shall fai
1d830 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 69  l.**          wi
1d840 74 68 20 61 6e 20 5b 53 51 4c 49 54 45 5f 45 52  th an [SQLITE_ER
1d850 52 4f 52 5d 20 65 72 72 6f 72 20 63 6f 64 65 20  ROR] error code 
1d860 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  and an error mes
1d870 73 61 67 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  sage.**         
1d880 20 65 78 70 6c 61 69 6e 69 6e 67 20 74 68 61 74   explaining that
1d890 20 61 63 63 65 73 73 20 69 73 20 64 65 6e 69 65   access is denie
1d8a0 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30 36  d..**.** {H12506
1d8b0 7d 20 49 66 20 74 68 65 20 61 75 74 68 6f 72 69  } If the authori
1d8c0 7a 65 72 20 63 6f 64 65 20 28 74 68 65 20 32 6e  zer code (the 2n
1d8d0 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  d parameter to t
1d8e0 68 65 20 61 75 74 68 6f 72 69 7a 65 72 0a 2a 2a  he authorizer.**
1d8f0 20 20 20 20 20 20 20 20 20 20 63 61 6c 6c 62 61            callba
1d900 63 6b 29 20 69 73 20 5b 53 51 4c 49 54 45 5f 52  ck) is [SQLITE_R
1d910 45 41 44 5d 20 61 6e 64 20 74 68 65 20 61 75 74  EAD] and the aut
1d920 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
1d930 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20   returns.**     
1d940 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 49 47 4e       [SQLITE_IGN
1d950 4f 52 45 5d 2c 20 74 68 65 6e 20 74 68 65 20 70  ORE], then the p
1d960 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
1d970 74 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65 64  t is constructed
1d980 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   to.**          
1d990 69 6e 73 65 72 74 20 61 20 4e 55 4c 4c 20 76 61  insert a NULL va
1d9a0 6c 75 65 20 69 6e 20 70 6c 61 63 65 20 6f 66 20  lue in place of 
1d9b0 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e  the table column
1d9c0 20 74 68 61 74 20 77 6f 75 6c 64 20 68 61 76 65   that would have
1d9d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62 65 65  .**          bee
1d9e0 6e 20 72 65 61 64 20 69 66 20 5b 53 51 4c 49 54  n read if [SQLIT
1d9f0 45 5f 4f 4b 5d 20 68 61 64 20 62 65 65 6e 20 72  E_OK] had been r
1da00 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b  eturned..**.** {
1da10 48 31 32 35 30 37 7d 20 49 66 20 74 68 65 20 61  H12507} If the a
1da20 75 74 68 6f 72 69 7a 65 72 20 63 6f 64 65 20 28  uthorizer code (
1da30 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65  the 2nd paramete
1da40 72 20 74 6f 20 74 68 65 20 61 75 74 68 6f 72 69  r to the authori
1da50 7a 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  zer.**          
1da60 63 61 6c 6c 62 61 63 6b 29 20 69 73 20 61 6e 79  callback) is any
1da70 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e  thing other than
1da80 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 5d 2c 20   [SQLITE_READ], 
1da90 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  then.**         
1daa0 20 61 20 72 65 74 75 72 6e 20 6f 66 20 5b 53 51   a return of [SQ
1dab0 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 68 61 73  LITE_IGNORE] has
1dac0 20 74 68 65 20 73 61 6d 65 20 65 66 66 65 63 74   the same effect
1dad0 20 61 73 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59   as [SQLITE_DENY
1dae0 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 31 30  ]..**.** {H12510
1daf0 7d 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61  } The first para
1db00 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 75 74  meter to the aut
1db10 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
1db20 20 69 73 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a   is a copy of.**
1db30 20 20 20 20 20 20 20 20 20 20 74 68 65 20 74 68            the th
1db40 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ird parameter to
1db50 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65   the [sqlite3_se
1db60 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 5d 20  t_authorizer()] 
1db70 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a  interface..**.**
1db80 20 7b 48 31 32 35 31 31 7d 20 54 68 65 20 73 65   {H12511} The se
1db90 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74  cond parameter t
1dba0 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69  o the callback i
1dbb0 73 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20  s an integer.** 
1dbc0 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45           [SQLITE
1dbd0 5f 43 4f 50 59 20 7c 20 61 63 74 69 6f 6e 20 63  _COPY | action c
1dbe0 6f 64 65 5d 20 74 68 61 74 20 73 70 65 63 69 66  ode] that specif
1dbf0 69 65 73 20 74 68 65 20 70 61 72 74 69 63 75 6c  ies the particul
1dc00 61 72 20 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20  ar action.**    
1dc10 20 20 20 20 20 20 74 6f 20 62 65 20 61 75 74 68        to be auth
1dc20 6f 72 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48  orized..**.** {H
1dc30 31 32 35 31 32 7d 20 54 68 65 20 74 68 69 72 64  12512} The third
1dc40 20 74 68 72 6f 75 67 68 20 73 69 78 74 68 20 70   through sixth p
1dc50 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 65  arameters to the
1dc60 20 63 61 6c 6c 62 61 63 6b 20 61 72 65 0a 2a 2a   callback are.**
1dc70 20 20 20 20 20 20 20 20 20 20 7a 65 72 6f 2d 74            zero-t
1dc80 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67  erminated string
1dc90 73 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 0a 2a  s that contain.*
1dca0 2a 20 20 20 20 20 20 20 20 20 20 61 64 64 69 74  *          addit
1dcb0 69 6f 6e 61 6c 20 64 65 74 61 69 6c 73 20 61 62  ional details ab
1dcc0 6f 75 74 20 74 68 65 20 61 63 74 69 6f 6e 20 74  out the action t
1dcd0 6f 20 62 65 20 61 75 74 68 6f 72 69 7a 65 64 2e  o be authorized.
1dce0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 32 30 7d 20  .**.** {H12520} 
1dcf0 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 71  Each call to [sq
1dd00 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72  lite3_set_author
1dd10 69 7a 65 72 28 29 5d 20 6f 76 65 72 72 69 64 65  izer()] override
1dd20 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e  s.**          an
1dd30 79 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 73  y previously ins
1dd40 74 61 6c 6c 65 64 20 61 75 74 68 6f 72 69 7a 65  talled authorize
1dd50 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 32 31  r..**.** {H12521
1dd60 7d 20 41 20 4e 55 4c 4c 20 61 75 74 68 6f 72 69  } A NULL authori
1dd70 7a 65 72 20 6d 65 61 6e 73 20 74 68 61 74 20 6e  zer means that n
1dd80 6f 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 0a  o authorization.
1dd90 2a 2a 20 20 20 20 20 20 20 20 20 20 63 61 6c 6c  **          call
1dda0 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 2e  back is invoked.
1ddb0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 32 32 7d 20  .**.** {H12522} 
1ddc0 54 68 65 20 64 65 66 61 75 6c 74 20 61 75 74 68  The default auth
1ddd0 6f 72 69 7a 65 72 20 69 73 20 4e 55 4c 4c 2e 0a  orizer is NULL..
1dde0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
1ddf0 74 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75  t sqlite3_set_au
1de00 74 68 6f 72 69 7a 65 72 28 0a 20 20 73 71 6c 69  thorizer(.  sqli
1de10 74 65 33 2a 2c 0a 20 20 69 6e 74 20 28 2a 78 41  te3*,.  int (*xA
1de20 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  uth)(void*,int,c
1de30 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
1de40 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61   char*,const cha
1de50 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c  r*,const char*),
1de60 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 72 44 61  .  void *pUserDa
1de70 74 61 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  ta.);../*.** CAP
1de80 49 33 52 45 46 3a 20 41 75 74 68 6f 72 69 7a 65  I3REF: Authorize
1de90 72 20 52 65 74 75 72 6e 20 43 6f 64 65 73 20 7b  r Return Codes {
1dea0 48 31 32 35 39 30 7d 20 3c 48 31 32 35 30 30 3e  H12590} <H12500>
1deb0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69  .**.** The [sqli
1dec0 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a  te3_set_authoriz
1ded0 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65 72 20  er | authorizer 
1dee0 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f  callback functio
1def0 6e 5d 20 6d 75 73 74 0a 2a 2a 20 72 65 74 75 72  n] must.** retur
1df00 6e 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54 45  n either [SQLITE
1df10 5f 4f 4b 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 74  _OK] or one of t
1df20 68 65 73 65 20 74 77 6f 20 63 6f 6e 73 74 61 6e  hese two constan
1df30 74 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74  ts in order.** t
1df40 6f 20 73 69 67 6e 61 6c 20 53 51 4c 69 74 65 20  o signal SQLite 
1df50 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
1df60 68 65 20 61 63 74 69 6f 6e 20 69 73 20 70 65 72  he action is per
1df70 6d 69 74 74 65 64 2e 20 20 53 65 65 20 74 68 65  mitted.  See the
1df80 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 65 74  .** [sqlite3_set
1df90 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 75  _authorizer | au
1dfa0 74 68 6f 72 69 7a 65 72 20 64 6f 63 75 6d 65 6e  thorizer documen
1dfb0 74 61 74 69 6f 6e 5d 20 66 6f 72 20 61 64 64 69  tation] for addi
1dfc0 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d  tional.** inform
1dfd0 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ation..*/.#defin
1dfe0 65 20 53 51 4c 49 54 45 5f 44 45 4e 59 20 20 20  e SQLITE_DENY   
1dff0 31 20 20 20 2f 2a 20 41 62 6f 72 74 20 74 68 65  1   /* Abort the
1e000 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 77   SQL statement w
1e010 69 74 68 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a  ith an error */.
1e020 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
1e030 47 4e 4f 52 45 20 32 20 20 20 2f 2a 20 44 6f 6e  GNORE 2   /* Don
1e040 27 74 20 61 6c 6c 6f 77 20 61 63 63 65 73 73 2c  't allow access,
1e050 20 62 75 74 20 64 6f 6e 27 74 20 67 65 6e 65 72   but don't gener
1e060 61 74 65 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a  ate an error */.
1e070 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
1e080 20 41 75 74 68 6f 72 69 7a 65 72 20 41 63 74 69   Authorizer Acti
1e090 6f 6e 20 43 6f 64 65 73 20 7b 48 31 32 35 35 30  on Codes {H12550
1e0a0 7d 20 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a 2a 2a  } <H12500>.**.**
1e0b0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65   The [sqlite3_se
1e0c0 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 5d 20  t_authorizer()] 
1e0d0 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74  interface regist
1e0e0 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 66  ers a callback f
1e0f0 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20  unction.** that 
1e100 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 20 61 75  is invoked to au
1e110 74 68 6f 72 69 7a 65 20 63 65 72 74 61 69 6e 20  thorize certain 
1e120 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 63  SQL statement ac
1e130 74 69 6f 6e 73 2e 20 20 54 68 65 0a 2a 2a 20 73  tions.  The.** s
1e140 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
1e150 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  to the callback 
1e160 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f  is an integer co
1e170 64 65 20 74 68 61 74 20 73 70 65 63 69 66 69 65  de that specifie
1e180 73 0a 2a 2a 20 77 68 61 74 20 61 63 74 69 6f 6e  s.** what action
1e190 20 69 73 20 62 65 69 6e 67 20 61 75 74 68 6f 72   is being author
1e1a0 69 7a 65 64 2e 20 20 54 68 65 73 65 20 61 72 65  ized.  These are
1e1b0 20 74 68 65 20 69 6e 74 65 67 65 72 20 61 63 74   the integer act
1e1c0 69 6f 6e 20 63 6f 64 65 73 20 74 68 61 74 0a 2a  ion codes that.*
1e1d0 2a 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72  * the authorizer
1e1e0 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 62 65   callback may be
1e1f0 20 70 61 73 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54   passed..**.** T
1e200 68 65 73 65 20 61 63 74 69 6f 6e 20 63 6f 64 65  hese action code
1e210 20 76 61 6c 75 65 73 20 73 69 67 6e 69 66 79 20   values signify 
1e220 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 6f 70 65  what kind of ope
1e230 72 61 74 69 6f 6e 20 69 73 20 74 6f 20 62 65 0a  ration is to be.
1e240 2a 2a 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 20  ** authorized.  
1e250 54 68 65 20 33 72 64 20 61 6e 64 20 34 74 68 20  The 3rd and 4th 
1e260 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68  parameters to th
1e270 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 0a  e authorization.
1e280 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  ** callback func
1e290 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 70 61 72  tion will be par
1e2a0 61 6d 65 74 65 72 73 20 6f 72 20 4e 55 4c 4c 20  ameters or NULL 
1e2b0 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 69  depending on whi
1e2c0 63 68 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 63  ch of these.** c
1e2d0 6f 64 65 73 20 69 73 20 75 73 65 64 20 61 73 20  odes is used as 
1e2e0 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  the second param
1e2f0 65 74 65 72 2e 20 20 54 68 65 20 35 74 68 20 70  eter.  The 5th p
1e300 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 0a  arameter to the.
1e310 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61  ** authorizer ca
1e320 6c 6c 62 61 63 6b 20 69 73 20 74 68 65 20 6e 61  llback is the na
1e330 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
1e340 73 65 20 28 22 6d 61 69 6e 22 2c 20 22 74 65 6d  se ("main", "tem
1e350 70 22 2c 0a 2a 2a 20 65 74 63 2e 29 20 69 66 20  p",.** etc.) if 
1e360 61 70 70 6c 69 63 61 62 6c 65 2e 20 20 54 68 65  applicable.  The
1e370 20 36 74 68 20 70 61 72 61 6d 65 74 65 72 20 74   6th parameter t
1e380 6f 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72  o the authorizer
1e390 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20   callback.** is 
1e3a0 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
1e3b0 69 6e 6e 65 72 2d 6d 6f 73 74 20 74 72 69 67 67  inner-most trigg
1e3c0 65 72 20 6f 72 20 76 69 65 77 20 74 68 61 74 20  er or view that 
1e3d0 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
1e3e0 6f 72 0a 2a 2a 20 74 68 65 20 61 63 63 65 73 73  or.** the access
1e3f0 20 61 74 74 65 6d 70 74 20 6f 72 20 4e 55 4c 4c   attempt or NULL
1e400 20 69 66 20 74 68 69 73 20 61 63 63 65 73 73 20   if this access 
1e410 61 74 74 65 6d 70 74 20 69 73 20 64 69 72 65 63  attempt is direc
1e420 74 6c 79 20 66 72 6f 6d 0a 2a 2a 20 74 6f 70 2d  tly from.** top-
1e430 6c 65 76 65 6c 20 53 51 4c 20 63 6f 64 65 2e 0a  level SQL code..
1e440 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53  **.** INVARIANTS
1e450 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 35 31 7d  :.**.** {H12551}
1e460 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61   The second para
1e470 6d 65 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 20  meter to an.**  
1e480 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33          [sqlite3
1e490 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20  _set_authorizer 
1e4a0 7c 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c  | authorizer cal
1e4b0 6c 62 61 63 6b 5d 20 73 68 61 6c 6c 20 62 65 20  lback] shall be 
1e4c0 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20  an integer.**   
1e4d0 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 43         [SQLITE_C
1e4e0 4f 50 59 20 7c 20 61 75 74 68 6f 72 69 7a 65 72  OPY | authorizer
1e4f0 20 63 6f 64 65 5d 20 74 68 61 74 20 73 70 65 63   code] that spec
1e500 69 66 69 65 73 20 77 68 61 74 20 61 63 74 69 6f  ifies what actio
1e510 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73  n.**          is
1e520 20 62 65 69 6e 67 20 61 75 74 68 6f 72 69 7a 65   being authorize
1e530 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 35 32  d..**.** {H12552
1e540 7d 20 54 68 65 20 33 72 64 20 61 6e 64 20 34 74  } The 3rd and 4t
1e550 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20  h parameters to 
1e560 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
1e570 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74  [sqlite3_set_aut
1e580 68 6f 72 69 7a 65 72 20 7c 20 61 75 74 68 6f 72  horizer | author
1e590 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b  ization callback
1e5a0 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 68  ].**          sh
1e5b0 61 6c 6c 20 62 65 20 70 61 72 61 6d 65 74 65 72  all be parameter
1e5c0 73 20 6f 72 20 4e 55 4c 4c 20 64 65 70 65 6e 64  s or NULL depend
1e5d0 69 6e 67 20 6f 6e 20 77 68 69 63 68 0a 2a 2a 20  ing on which.** 
1e5e0 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45           [SQLITE
1e5f0 5f 43 4f 50 59 20 7c 20 61 75 74 68 6f 72 69 7a  _COPY | authoriz
1e600 65 72 20 63 6f 64 65 5d 20 69 73 20 75 73 65 64  er code] is used
1e610 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70   as the second p
1e620 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20  arameter..**.** 
1e630 7b 48 31 32 35 35 33 7d 20 54 68 65 20 35 74 68  {H12553} The 5th
1e640 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68   parameter to th
1e650 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73  e.**          [s
1e660 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f  qlite3_set_autho
1e670 72 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a  rizer | authoriz
1e680 65 72 20 63 61 6c 6c 62 61 63 6b 5d 20 73 68 61  er callback] sha
1e690 6c 6c 20 62 65 20 74 68 65 20 6e 61 6d 65 0a 2a  ll be the name.*
1e6a0 2a 20 20 20 20 20 20 20 20 20 20 6f 66 20 74 68  *          of th
1e6b0 65 20 64 61 74 61 62 61 73 65 20 28 65 78 61 6d  e database (exam
1e6c0 70 6c 65 3a 20 22 6d 61 69 6e 22 2c 20 22 74 65  ple: "main", "te
1e6d0 6d 70 22 2c 20 65 74 63 2e 29 20 69 66 20 61 70  mp", etc.) if ap
1e6e0 70 6c 69 63 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  plicable..**.** 
1e6f0 7b 48 31 32 35 35 34 7d 20 54 68 65 20 36 74 68  {H12554} The 6th
1e700 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68   parameter to th
1e710 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73  e.**          [s
1e720 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f  qlite3_set_autho
1e730 72 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a  rizer | authoriz
1e740 65 72 20 63 61 6c 6c 62 61 63 6b 5d 20 73 68 61  er callback] sha
1e750 6c 6c 20 62 65 20 74 68 65 20 6e 61 6d 65 0a 2a  ll be the name.*
1e760 2a 20 20 20 20 20 20 20 20 20 20 6f 66 20 74 68  *          of th
1e770 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 74 72 69  e inner-most tri
1e780 67 67 65 72 20 6f 72 20 76 69 65 77 20 74 68 61  gger or view tha
1e790 74 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65  t is responsible
1e7a0 20 66 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20   for.**         
1e7b0 20 74 68 65 20 61 63 63 65 73 73 20 61 74 74 65   the access atte
1e7c0 6d 70 74 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74  mpt or NULL if t
1e7d0 68 69 73 20 61 63 63 65 73 73 20 61 74 74 65 6d  his access attem
1e7e0 70 74 20 69 73 20 64 69 72 65 63 74 6c 79 20 66  pt is directly f
1e7f0 72 6f 6d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  rom.**          
1e800 74 6f 70 2d 6c 65 76 65 6c 20 53 51 4c 20 63 6f  top-level SQL co
1e810 64 65 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  de..*/./********
1e820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e840 2a 2a 2a 20 33 72 64 20 2a 2a 2a 2a 2a 2a 2a 2a  *** 3rd ********
1e850 2a 2a 2a 2a 20 34 74 68 20 2a 2a 2a 2a 2a 2a 2a  **** 4th *******
1e860 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ****/.#define SQ
1e870 4c 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45  LITE_CREATE_INDE
1e880 58 20 20 20 20 20 20 20 20 20 20 31 20 20 20 2f  X          1   /
1e890 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20  * Index Name    
1e8a0 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20    Table Name    
1e8b0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
1e8c0 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c 45  ITE_CREATE_TABLE
1e8d0 20 20 20 20 20 20 20 20 20 20 32 20 20 20 2f 2a            2   /*
1e8e0 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
1e8f0 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20   NULL           
1e900 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1e910 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49  TE_CREATE_TEMP_I
1e920 4e 44 45 58 20 20 20 20 20 33 20 20 20 2f 2a 20  NDEX     3   /* 
1e930 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20  Index Name      
1e940 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
1e950 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1e960 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 41  E_CREATE_TEMP_TA
1e970 42 4c 45 20 20 20 20 20 34 20 20 20 2f 2a 20 54  BLE     4   /* T
1e980 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e  able Name      N
1e990 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a  ULL            *
1e9a0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1e9b0 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52 49  _CREATE_TEMP_TRI
1e9c0 47 47 45 52 20 20 20 35 20 20 20 2f 2a 20 54 72  GGER   5   /* Tr
1e9d0 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61  igger Name    Ta
1e9e0 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f  ble Name      */
1e9f0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1ea00 43 52 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 57  CREATE_TEMP_VIEW
1ea10 20 20 20 20 20 20 36 20 20 20 2f 2a 20 56 69 65        6   /* Vie
1ea20 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c  w Name       NUL
1ea30 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  L            */.
1ea40 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
1ea50 52 45 41 54 45 5f 54 52 49 47 47 45 52 20 20 20  REATE_TRIGGER   
1ea60 20 20 20 20 20 37 20 20 20 2f 2a 20 54 72 69 67       7   /* Trig
1ea70 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c  ger Name    Tabl
1ea80 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23  e Name      */.#
1ea90 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52  define SQLITE_CR
1eaa0 45 41 54 45 5f 56 49 45 57 20 20 20 20 20 20 20  EATE_VIEW       
1eab0 20 20 20 20 38 20 20 20 2f 2a 20 56 69 65 77 20      8   /* View 
1eac0 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20  Name       NULL 
1ead0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64             */.#d
1eae0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 4c  efine SQLITE_DEL
1eaf0 45 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ETE             
1eb00 20 20 20 39 20 20 20 2f 2a 20 54 61 62 6c 65 20     9   /* Table 
1eb10 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20  Name      NULL  
1eb20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65            */.#de
1eb30 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50  fine SQLITE_DROP
1eb40 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20  _INDEX          
1eb50 20 31 30 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e   10   /* Index N
1eb60 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e  ame      Table N
1eb70 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  ame      */.#def
1eb80 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f  ine SQLITE_DROP_
1eb90 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20  TABLE           
1eba0 31 31 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61  11   /* Table Na
1ebb0 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20  me      NULL    
1ebc0 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
1ebd0 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54  ne SQLITE_DROP_T
1ebe0 45 4d 50 5f 49 4e 44 45 58 20 20 20 20 20 20 31  EMP_INDEX      1
1ebf0 32 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d  2   /* Index Nam
1ec00 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d  e      Table Nam
1ec10 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  e      */.#defin
1ec20 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45  e SQLITE_DROP_TE
1ec30 4d 50 5f 54 41 42 4c 45 20 20 20 20 20 20 31 33  MP_TABLE      13
1ec40 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65     /* Table Name
1ec50 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20        NULL      
1ec60 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
1ec70 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d   SQLITE_DROP_TEM
1ec80 50 5f 54 52 49 47 47 45 52 20 20 20 20 31 34 20  P_TRIGGER    14 
1ec90 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d    /* Trigger Nam
1eca0 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20  e    Table Name 
1ecb0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
1ecc0 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50  SQLITE_DROP_TEMP
1ecd0 5f 56 49 45 57 20 20 20 20 20 20 20 31 35 20 20  _VIEW       15  
1ece0 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20   /* View Name   
1ecf0 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20      NULL        
1ed00 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
1ed10 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 47  QLITE_DROP_TRIGG
1ed20 45 52 20 20 20 20 20 20 20 20 20 31 36 20 20 20  ER         16   
1ed30 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20  /* Trigger Name 
1ed40 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20     Table Name   
1ed50 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
1ed60 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 20 20  LITE_DROP_VIEW  
1ed70 20 20 20 20 20 20 20 20 20 20 31 37 20 20 20 2f            17   /
1ed80 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20  * View Name     
1ed90 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20    NULL          
1eda0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
1edb0 49 54 45 5f 49 4e 53 45 52 54 20 20 20 20 20 20  ITE_INSERT      
1edc0 20 20 20 20 20 20 20 20 20 31 38 20 20 20 2f 2a           18   /*
1edd0 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
1ede0 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20   NULL           
1edf0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1ee00 54 45 5f 50 52 41 47 4d 41 20 20 20 20 20 20 20  TE_PRAGMA       
1ee10 20 20 20 20 20 20 20 20 31 39 20 20 20 2f 2a 20          19   /* 
1ee20 50 72 61 67 6d 61 20 4e 61 6d 65 20 20 20 20 20  Pragma Name     
1ee30 31 73 74 20 61 72 67 20 6f 72 20 4e 55 4c 4c 20  1st arg or NULL 
1ee40 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1ee50 45 5f 52 45 41 44 20 20 20 20 20 20 20 20 20 20  E_READ          
1ee60 20 20 20 20 20 20 20 32 30 20 20 20 2f 2a 20 54         20   /* T
1ee70 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 43  able Name      C
1ee80 6f 6c 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a  olumn Name     *
1ee90 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1eea0 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 20 20  _SELECT         
1eeb0 20 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4e 55        21   /* NU
1eec0 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 4e 55  LL            NU
1eed0 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  LL            */
1eee0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1eef0 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 20  TRANSACTION     
1ef00 20 20 20 20 20 32 32 20 20 20 2f 2a 20 4e 55 4c       22   /* NUL
1ef10 4c 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c  L            NUL
1ef20 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  L            */.
1ef30 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55  #define SQLITE_U
1ef40 50 44 41 54 45 20 20 20 20 20 20 20 20 20 20 20  PDATE           
1ef50 20 20 20 20 32 33 20 20 20 2f 2a 20 54 61 62 6c      23   /* Tabl
1ef60 65 20 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c 75  e Name      Colu
1ef70 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23  mn Name     */.#
1ef80 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 54  define SQLITE_AT
1ef90 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20  TACH            
1efa0 20 20 20 32 34 20 20 20 2f 2a 20 46 69 6c 65 6e     24   /* Filen
1efb0 61 6d 65 20 20 20 20 20 20 20 20 4e 55 4c 4c 20  ame        NULL 
1efc0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64             */.#d
1efd0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 54  efine SQLITE_DET
1efe0 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20  ACH             
1eff0 20 20 32 35 20 20 20 2f 2a 20 44 61 74 61 62 61    25   /* Databa
1f000 73 65 20 4e 61 6d 65 20 20 20 4e 55 4c 4c 20 20  se Name   NULL  
1f010 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65            */.#de
1f020 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 4c 54 45  fine SQLITE_ALTE
1f030 52 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 20  R_TABLE         
1f040 20 32 36 20 20 20 2f 2a 20 44 61 74 61 62 61 73   26   /* Databas
1f050 65 20 4e 61 6d 65 20 20 20 54 61 62 6c 65 20 4e  e Name   Table N
1f060 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  ame      */.#def
1f070 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 49 4e 44  ine SQLITE_REIND
1f080 45 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20  EX              
1f090 32 37 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61  27   /* Index Na
1f0a0 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20  me      NULL    
1f0b0 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
1f0c0 6e 65 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59 5a  ne SQLITE_ANALYZ
1f0d0 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32  E              2
1f0e0 38 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d  8   /* Table Nam
1f0f0 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20  e      NULL     
1f100 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
1f110 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f  e SQLITE_CREATE_
1f120 56 54 41 42 4c 45 20 20 20 20 20 20 20 20 32 39  VTABLE        29
1f130 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65     /* Table Name
1f140 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d        Module Nam
1f150 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  e     */.#define
1f160 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54 41   SQLITE_DROP_VTA
1f170 42 4c 45 20 20 20 20 20 20 20 20 20 20 33 30 20  BLE          30 
1f180 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20    /* Table Name 
1f190 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d 65       Module Name
1f1a0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
1f1b0 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e 20  SQLITE_FUNCTION 
1f1c0 20 20 20 20 20 20 20 20 20 20 20 20 33 31 20 20              31  
1f1d0 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 4e 61 6d   /* Function Nam
1f1e0 65 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20  e   NULL        
1f1f0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
1f200 51 4c 49 54 45 5f 43 4f 50 59 20 20 20 20 20 20  QLITE_COPY      
1f210 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20              0   
1f220 2f 2a 20 4e 6f 20 6c 6f 6e 67 65 72 20 75 73 65  /* No longer use
1f230 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  d */../*.** CAPI
1f240 33 52 45 46 3a 20 54 72 61 63 69 6e 67 20 41 6e  3REF: Tracing An
1f250 64 20 50 72 6f 66 69 6c 69 6e 67 20 46 75 6e 63  d Profiling Func
1f260 74 69 6f 6e 73 20 7b 48 31 32 32 38 30 7d 20 3c  tions {H12280} <
1f270 53 36 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52  S60400>.** EXPER
1f280 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68  IMENTAL.**.** Th
1f290 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 67  ese routines reg
1f2a0 69 73 74 65 72 20 63 61 6c 6c 62 61 63 6b 20 66  ister callback f
1f2b0 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 63 61  unctions that ca
1f2c0 6e 20 62 65 20 75 73 65 64 20 66 6f 72 0a 2a 2a  n be used for.**
1f2d0 20 74 72 61 63 69 6e 67 20 61 6e 64 20 70 72 6f   tracing and pro
1f2e0 66 69 6c 69 6e 67 20 74 68 65 20 65 78 65 63 75  filing the execu
1f2f0 74 69 6f 6e 20 6f 66 20 53 51 4c 20 73 74 61 74  tion of SQL stat
1f300 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  ements..**.** Th
1f310 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  e callback funct
1f320 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 62  ion registered b
1f330 79 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28  y sqlite3_trace(
1f340 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 74 0a  ) is invoked at.
1f350 2a 2a 20 76 61 72 69 6f 75 73 20 74 69 6d 65 73  ** various times
1f360 20 77 68 65 6e 20 61 6e 20 53 51 4c 20 73 74 61   when an SQL sta
1f370 74 65 6d 65 6e 74 20 69 73 20 62 65 69 6e 67 20  tement is being 
1f380 72 75 6e 20 62 79 20 5b 73 71 6c 69 74 65 33 5f  run by [sqlite3_
1f390 73 74 65 70 28 29 5d 2e 0a 2a 2a 20 54 68 65 20  step()]..** The 
1f3a0 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73  callback returns
1f3b0 20 61 20 55 54 46 2d 38 20 72 65 6e 64 65 72 69   a UTF-8 renderi
1f3c0 6e 67 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74  ng of the SQL st
1f3d0 61 74 65 6d 65 6e 74 20 74 65 78 74 0a 2a 2a 20  atement text.** 
1f3e0 61 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  as the statement
1f3f0 20 66 69 72 73 74 20 62 65 67 69 6e 73 20 65 78   first begins ex
1f400 65 63 75 74 69 6e 67 2e 20 20 41 64 64 69 74 69  ecuting.  Additi
1f410 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 6f  onal callbacks o
1f420 63 63 75 72 0a 2a 2a 20 61 73 20 65 61 63 68 20  ccur.** as each 
1f430 74 72 69 67 67 65 72 65 64 20 73 75 62 70 72 6f  triggered subpro
1f440 67 72 61 6d 20 69 73 20 65 6e 74 65 72 65 64 2e  gram is entered.
1f450 20 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 73 20    The callbacks 
1f460 66 6f 72 20 74 72 69 67 67 65 72 73 0a 2a 2a 20  for triggers.** 
1f470 63 6f 6e 74 61 69 6e 20 61 20 55 54 46 2d 38 20  contain a UTF-8 
1f480 53 51 4c 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74  SQL comment that
1f490 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20   identifies the 
1f4a0 74 72 69 67 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54  trigger..**.** T
1f4b0 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  he callback func
1f4c0 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20  tion registered 
1f4d0 62 79 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69  by sqlite3_profi
1f4e0 6c 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a  le() is invoked.
1f4f0 2a 2a 20 61 73 20 65 61 63 68 20 53 51 4c 20 73  ** as each SQL s
1f500 74 61 74 65 6d 65 6e 74 20 66 69 6e 69 73 68 65  tatement finishe
1f510 73 2e 20 20 54 68 65 20 70 72 6f 66 69 6c 65 20  s.  The profile 
1f520 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e  callback contain
1f530 73 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61  s.** the origina
1f540 6c 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78 74  l statement text
1f550 20 61 6e 64 20 61 6e 20 65 73 74 69 6d 61 74 65   and an estimate
1f560 20 6f 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 74   of wall-clock t
1f570 69 6d 65 0a 2a 2a 20 6f 66 20 68 6f 77 20 6c 6f  ime.** of how lo
1f580 6e 67 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e  ng that statemen
1f590 74 20 74 6f 6f 6b 20 74 6f 20 72 75 6e 2e 0a 2a  t took to run..*
1f5a0 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
1f5b0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 38 31 7d 20  .**.** {H12281} 
1f5c0 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  The callback fun
1f5d0 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64  ction registered
1f5e0 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 74 72 61   by [sqlite3_tra
1f5f0 63 65 28 29 5d 20 0a 2a 2a 20 20 20 20 20 20 20  ce()] .**       
1f600 20 20 20 73 68 61 6c 6c 20 62 65 20 69 6e 76 6f     shall be invo
1f610 6b 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ked.**          
1f620 77 68 65 6e 65 76 65 72 20 61 6e 20 53 51 4c 20  whenever an SQL 
1f630 73 74 61 74 65 6d 65 6e 74 20 66 69 72 73 74 20  statement first 
1f640 62 65 67 69 6e 73 20 74 6f 20 65 78 65 63 75 74  begins to execut
1f650 65 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20  e and.**        
1f660 20 20 77 68 65 6e 65 76 65 72 20 61 20 74 72 69    whenever a tri
1f670 67 67 65 72 20 73 75 62 70 72 6f 67 72 61 6d 20  gger subprogram 
1f680 66 69 72 73 74 20 62 65 67 69 6e 73 20 74 6f 20  first begins to 
1f690 72 75 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32  run..**.** {H122
1f6a0 38 32 7d 20 45 61 63 68 20 63 61 6c 6c 20 74 6f  82} Each call to
1f6b0 20 5b 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28   [sqlite3_trace(
1f6c0 29 5d 20 73 68 61 6c 6c 20 6f 76 65 72 72 69 64  )] shall overrid
1f6d0 65 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c 79  e the previously
1f6e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 67  .**          reg
1f6f0 69 73 74 65 72 65 64 20 74 72 61 63 65 20 63 61  istered trace ca
1f700 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 7b 48  llback..**.** {H
1f710 31 32 32 38 33 7d 20 41 20 4e 55 4c 4c 20 74 72  12283} A NULL tr
1f720 61 63 65 20 63 61 6c 6c 62 61 63 6b 20 73 68 61  ace callback sha
1f730 6c 6c 20 64 69 73 61 62 6c 65 20 74 72 61 63 69  ll disable traci
1f740 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 38  ng..**.** {H1228
1f750 34 7d 20 54 68 65 20 66 69 72 73 74 20 61 72 67  4} The first arg
1f760 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 74 72 61  ument to the tra
1f770 63 65 20 63 61 6c 6c 62 61 63 6b 20 73 68 61 6c  ce callback shal
1f780 6c 20 62 65 20 61 20 63 6f 70 79 20 6f 66 0a 2a  l be a copy of.*
1f790 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 70  *          the p
1f7a0 6f 69 6e 74 65 72 20 77 68 69 63 68 20 77 61 73  ointer which was
1f7b0 20 74 68 65 20 33 72 64 20 61 72 67 75 6d 65 6e   the 3rd argumen
1f7c0 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 74 72  t to [sqlite3_tr
1f7d0 61 63 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48  ace()]..**.** {H
1f7e0 31 32 32 38 35 7d 20 54 68 65 20 73 65 63 6f 6e  12285} The secon
1f7f0 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  d argument to th
1f800 65 20 74 72 61 63 65 20 63 61 6c 6c 62 61 63 6b  e trace callback
1f810 20 69 73 20 61 0a 2a 2a 20 20 20 20 20 20 20 20   is a.**        
1f820 20 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65    zero-terminate
1f830 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 63  d UTF-8 string c
1f840 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6f 72  ontaining the or
1f850 69 67 69 6e 61 6c 20 74 65 78 74 0a 2a 2a 20 20  iginal text.**  
1f860 20 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 53          of the S
1f870 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 73 20  QL statement as 
1f880 69 74 20 77 61 73 20 70 61 73 73 65 64 20 69 6e  it was passed in
1f890 74 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  to [sqlite3_prep
1f8a0 61 72 65 5f 76 32 28 29 5d 0a 2a 2a 20 20 20 20  are_v2()].**    
1f8b0 20 20 20 20 20 20 6f 72 20 74 68 65 20 65 71 75        or the equ
1f8c0 69 76 61 6c 65 6e 74 2c 20 6f 72 20 61 6e 20 53  ivalent, or an S
1f8d0 51 4c 20 63 6f 6d 6d 65 6e 74 20 69 6e 64 69 63  QL comment indic
1f8e0 61 74 69 6e 67 20 74 68 65 20 62 65 67 69 6e 6e  ating the beginn
1f8f0 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ing.**          
1f900 6f 66 20 61 20 74 72 69 67 67 65 72 20 73 75 62  of a trigger sub
1f910 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 7b  program..**.** {
1f920 48 31 32 32 38 37 7d 20 54 68 65 20 63 61 6c 6c  H12287} The call
1f930 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65  back function re
1f940 67 69 73 74 65 72 65 64 20 62 79 20 5b 73 71 6c  gistered by [sql
1f950 69 74 65 33 5f 70 72 6f 66 69 6c 65 28 29 5d 20  ite3_profile()] 
1f960 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 20 20  is invoked.**   
1f970 20 20 20 20 20 20 20 61 73 20 65 61 63 68 20 53         as each S
1f980 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 66 69 6e  QL statement fin
1f990 69 73 68 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  ishes..**.** {H1
1f9a0 32 32 38 38 7d 20 54 68 65 20 66 69 72 73 74 20  2288} The first 
1f9b0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65  parameter to the
1f9c0 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63   profile callbac
1f9d0 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 0a 2a  k is a copy of.*
1f9e0 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 33  *          the 3
1f9f0 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  rd parameter to 
1fa00 5b 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65  [sqlite3_profile
1fa10 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32  ()]..**.** {H122
1fa20 38 39 7d 20 54 68 65 20 73 65 63 6f 6e 64 20 70  89} The second p
1fa30 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20  arameter to the 
1fa40 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b  profile callback
1fa50 20 69 73 20 61 0a 2a 2a 20 20 20 20 20 20 20 20   is a.**        
1fa60 20 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65    zero-terminate
1fa70 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 74  d UTF-8 string t
1fa80 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  hat contains the
1fa90 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f   complete text o
1faa0 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  f.**          th
1fab0 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
1fac0 61 73 20 69 74 20 77 61 73 20 70 72 6f 63 65 73  as it was proces
1fad0 73 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f  sed by [sqlite3_
1fae0 70 72 65 70 61 72 65 5f 76 32 28 29 5d 0a 2a 2a  prepare_v2()].**
1faf0 20 20 20 20 20 20 20 20 20 20 6f 72 20 74 68 65            or the
1fb00 20 65 71 75 69 76 61 6c 65 6e 74 2e 0a 2a 2a 0a   equivalent..**.
1fb10 2a 2a 20 7b 48 31 32 32 39 30 7d 20 54 68 65 20  ** {H12290} The 
1fb20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20  third parameter 
1fb30 74 6f 20 74 68 65 20 70 72 6f 66 69 6c 65 20 63  to the profile c
1fb40 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 65 73  allback is an es
1fb50 74 69 6d 61 74 65 0a 2a 2a 20 20 20 20 20 20 20  timate.**       
1fb60 20 20 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72     of the number
1fb70 20 6f 66 20 6e 61 6e 6f 73 65 63 6f 6e 64 73 20   of nanoseconds 
1fb80 6f 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 74 69  of wall-clock ti
1fb90 6d 65 20 72 65 71 75 69 72 65 64 20 74 6f 0a 2a  me required to.*
1fba0 2a 20 20 20 20 20 20 20 20 20 20 72 75 6e 20 74  *          run t
1fbb0 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
1fbc0 20 66 72 6f 6d 20 73 74 61 72 74 20 74 6f 20 66   from start to f
1fbd0 69 6e 69 73 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45  inish..*/.SQLITE
1fbe0 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45  _API SQLITE_EXPE
1fbf0 52 49 4d 45 4e 54 41 4c 20 76 6f 69 64 20 2a 73  RIMENTAL void *s
1fc00 71 6c 69 74 65 33 5f 74 72 61 63 65 28 73 71 6c  qlite3_trace(sql
1fc10 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a 78 54 72  ite3*, void(*xTr
1fc20 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74  ace)(void*,const
1fc30 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 2a 29 3b   char*), void*);
1fc40 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49  .SQLITE_API SQLI
1fc50 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20  TE_EXPERIMENTAL 
1fc60 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 70 72  void *sqlite3_pr
1fc70 6f 66 69 6c 65 28 73 71 6c 69 74 65 33 2a 2c 0a  ofile(sqlite3*,.
1fc80 20 20 20 76 6f 69 64 28 2a 78 50 72 6f 66 69 6c     void(*xProfil
1fc90 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63  e)(void*,const c
1fca0 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 75 69 6e  har*,sqlite3_uin
1fcb0 74 36 34 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f  t64), void*);../
1fcc0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 51  *.** CAPI3REF: Q
1fcd0 75 65 72 79 20 50 72 6f 67 72 65 73 73 20 43 61  uery Progress Ca
1fce0 6c 6c 62 61 63 6b 73 20 7b 48 31 32 39 31 30 7d  llbacks {H12910}
1fcf0 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S60400>.**.** 
1fd00 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e  This routine con
1fd10 66 69 67 75 72 65 73 20 61 20 63 61 6c 6c 62 61  figures a callba
1fd20 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2d 20 74 68  ck function - th
1fd30 65 0a 2a 2a 20 70 72 6f 67 72 65 73 73 20 63 61  e.** progress ca
1fd40 6c 6c 62 61 63 6b 20 2d 20 74 68 61 74 20 69 73  llback - that is
1fd50 20 69 6e 76 6f 6b 65 64 20 70 65 72 69 6f 64 69   invoked periodi
1fd60 63 61 6c 6c 79 20 64 75 72 69 6e 67 20 6c 6f 6e  cally during lon
1fd70 67 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 63 61 6c  g.** running cal
1fd80 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65  ls to [sqlite3_e
1fd90 78 65 63 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33  xec()], [sqlite3
1fda0 5f 73 74 65 70 28 29 5d 20 61 6e 64 0a 2a 2a 20  _step()] and.** 
1fdb0 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62  [sqlite3_get_tab
1fdc0 6c 65 28 29 5d 2e 20 20 41 6e 20 65 78 61 6d 70  le()].  An examp
1fdd0 6c 65 20 75 73 65 20 66 6f 72 20 74 68 69 73 0a  le use for this.
1fde0 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  ** interface is 
1fdf0 74 6f 20 6b 65 65 70 20 61 20 47 55 49 20 75 70  to keep a GUI up
1fe00 64 61 74 65 64 20 64 75 72 69 6e 67 20 61 20 6c  dated during a l
1fe10 61 72 67 65 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a  arge query..**.*
1fe20 2a 20 49 66 20 74 68 65 20 70 72 6f 67 72 65 73  * If the progres
1fe30 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72  s callback retur
1fe40 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  ns non-zero, the
1fe50 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a 2a 2a   operation is.**
1fe60 20 69 6e 74 65 72 72 75 70 74 65 64 2e 20 20 54   interrupted.  T
1fe70 68 69 73 20 66 65 61 74 75 72 65 20 63 61 6e 20  his feature can 
1fe80 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  be used to imple
1fe90 6d 65 6e 74 20 61 0a 2a 2a 20 22 43 61 6e 63 65  ment a.** "Cance
1fea0 6c 22 20 62 75 74 74 6f 6e 20 6f 6e 20 61 20 47  l" button on a G
1feb0 55 49 20 70 72 6f 67 72 65 73 73 20 64 69 61 6c  UI progress dial
1fec0 6f 67 20 62 6f 78 2e 0a 2a 2a 0a 2a 2a 20 54 68  og box..**.** Th
1fed0 65 20 70 72 6f 67 72 65 73 73 20 68 61 6e 64 6c  e progress handl
1fee0 65 72 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61  er must not do a
1fef0 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c  nything that wil
1ff00 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20  l modify.** the 
1ff10 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1ff20 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64  ion that invoked
1ff30 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 68 61   the progress ha
1ff40 6e 64 6c 65 72 2e 0a 2a 2a 20 4e 6f 74 65 20 74  ndler..** Note t
1ff50 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  hat [sqlite3_pre
1ff60 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b  pare_v2()] and [
1ff70 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
1ff80 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 69  both modify thei
1ff90 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f  r.** database co
1ffa0 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 68  nnections for th
1ffb0 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f  e meaning of "mo
1ffc0 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 61  dify" in this pa
1ffd0 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 49  ragraph..**.** I
1ffe0 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a  NVARIANTS:.**.**
1fff0 20 7b 48 31 32 39 31 31 7d 20 54 68 65 20 63 61   {H12911} The ca
20000 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
20010 72 65 67 69 73 74 65 72 65 64 20 62 79 20 73 71  registered by sq
20020 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68  lite3_progress_h
20030 61 6e 64 6c 65 72 28 29 0a 2a 2a 20 20 20 20 20  andler().**     
20040 20 20 20 20 20 69 73 20 69 6e 76 6f 6b 65 64 20       is invoked 
20050 70 65 72 69 6f 64 69 63 61 6c 6c 79 20 64 75 72  periodically dur
20060 69 6e 67 20 6c 6f 6e 67 20 72 75 6e 6e 69 6e 67  ing long running
20070 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 20 20 20   calls to.**    
20080 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 73        [sqlite3_s
20090 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48  tep()]..**.** {H
200a0 31 32 39 31 32 7d 20 54 68 65 20 70 72 6f 67 72  12912} The progr
200b0 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  ess callback is 
200c0 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72  invoked once for
200d0 20 65 76 65 72 79 20 4e 20 76 69 72 74 75 61 6c   every N virtual
200e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6d 61 63  .**          mac
200f0 68 69 6e 65 20 6f 70 63 6f 64 65 73 2c 20 77 68  hine opcodes, wh
20100 65 72 65 20 4e 20 69 73 20 74 68 65 20 73 65 63  ere N is the sec
20110 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a  ond argument to.
20120 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20  **          the 
20130 5b 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73  [sqlite3_progres
20140 73 5f 68 61 6e 64 6c 65 72 28 29 5d 20 63 61 6c  s_handler()] cal
20150 6c 20 74 68 61 74 20 72 65 67 69 73 74 65 72 65  l that registere
20160 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  d.**          th
20170 65 20 63 61 6c 6c 62 61 63 6b 2e 20 20 49 66 20  e callback.  If 
20180 4e 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 31  N is less than 1
20190 2c 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65  , sqlite3_progre
201a0 73 73 5f 68 61 6e 64 6c 65 72 28 29 0a 2a 2a 20  ss_handler().** 
201b0 20 20 20 20 20 20 20 20 20 61 63 74 73 20 61 73           acts as
201c0 20 69 66 20 61 20 4e 55 4c 4c 20 70 72 6f 67 72   if a NULL progr
201d0 65 73 73 20 68 61 6e 64 6c 65 72 20 68 61 64 20  ess handler had 
201e0 62 65 65 6e 20 73 70 65 63 69 66 69 65 64 2e 0a  been specified..
201f0 2a 2a 0a 2a 2a 20 7b 48 31 32 39 31 33 7d 20 54  **.** {H12913} T
20200 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c  he progress call
20210 62 61 63 6b 20 69 74 73 65 6c 66 20 69 73 20 69  back itself is i
20220 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65  dentified by the
20230 20 74 68 69 72 64 0a 2a 2a 20 20 20 20 20 20 20   third.**       
20240 20 20 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73     argument to s
20250 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f  qlite3_progress_
20260 68 61 6e 64 6c 65 72 28 29 2e 0a 2a 2a 0a 2a 2a  handler()..**.**
20270 20 7b 48 31 32 39 31 34 7d 20 54 68 65 20 66 6f   {H12914} The fo
20280 75 72 74 68 20 61 72 67 75 6d 65 6e 74 20 74 6f  urth argument to
20290 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73   sqlite3_progres
202a0 73 5f 68 61 6e 64 6c 65 72 28 29 20 69 73 20 61  s_handler() is a
202b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 76 6f 69  .**          voi
202c0 64 20 70 6f 69 6e 74 65 72 20 70 61 73 73 65 64  d pointer passed
202d0 20 74 6f 20 74 68 65 20 70 72 6f 67 72 65 73 73   to the progress
202e0 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20 20   callback.**    
202f0 20 20 20 20 20 20 66 75 6e 63 74 69 6f 6e 20 65        function e
20300 61 63 68 20 74 69 6d 65 20 69 74 20 69 73 20 69  ach time it is i
20310 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48  nvoked..**.** {H
20320 31 32 39 31 35 7d 20 49 66 20 61 20 63 61 6c 6c  12915} If a call
20330 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65   to [sqlite3_ste
20340 70 28 29 5d 20 72 65 73 75 6c 74 73 20 69 6e 20  p()] results in 
20350 66 65 77 65 72 20 74 68 61 6e 20 4e 20 6f 70 63  fewer than N opc
20360 6f 64 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  odes.**         
20370 20 62 65 69 6e 67 20 65 78 65 63 75 74 65 64 2c   being executed,
20380 20 74 68 65 6e 20 74 68 65 20 70 72 6f 67 72 65   then the progre
20390 73 73 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e  ss callback is n
203a0 65 76 65 72 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a  ever invoked..**
203b0 0a 2a 2a 20 7b 48 31 32 39 31 36 7d 20 45 76 65  .** {H12916} Eve
203c0 72 79 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69  ry call to [sqli
203d0 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e  te3_progress_han
203e0 64 6c 65 72 28 29 5d 0a 2a 2a 20 20 20 20 20 20  dler()].**      
203f0 20 20 20 20 6f 76 65 72 77 72 69 74 65 73 20 61      overwrites a
20400 6e 79 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65  ny previously re
20410 67 69 73 74 65 72 65 64 20 70 72 6f 67 72 65 73  gistered progres
20420 73 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 0a 2a 2a  s handler..**.**
20430 20 7b 48 31 32 39 31 37 7d 20 49 66 20 74 68 65   {H12917} If the
20440 20 70 72 6f 67 72 65 73 73 20 68 61 6e 64 6c 65   progress handle
20450 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55  r callback is NU
20460 4c 4c 20 74 68 65 6e 20 6e 6f 20 70 72 6f 67 72  LL then no progr
20470 65 73 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ess.**          
20480 68 61 6e 64 6c 65 72 20 69 73 20 69 6e 76 6f 6b  handler is invok
20490 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 31  ed..**.** {H1291
204a0 38 7d 20 49 66 20 74 68 65 20 70 72 6f 67 72 65  8} If the progre
204b0 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75  ss callback retu
204c0 72 6e 73 20 61 20 72 65 73 75 6c 74 20 6f 74 68  rns a result oth
204d0 65 72 20 74 68 61 6e 20 30 2c 20 74 68 65 6e 0a  er than 0, then.
204e0 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20  **          the 
204f0 62 65 68 61 76 69 6f 72 20 69 73 20 61 20 69 66  behavior is a if
20500 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72   [sqlite3_interr
20510 75 70 74 28 29 5d 20 68 61 64 20 62 65 65 6e 20  upt()] had been 
20520 63 61 6c 6c 65 64 2e 0a 2a 2a 20 20 20 20 20 20  called..**      
20530 20 20 20 20 3c 53 33 30 35 30 30 3e 0a 2a 2f 0a      <S30500>.*/.
20540 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
20550 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73  sqlite3_progress
20560 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65 33  _handler(sqlite3
20570 2a 2c 20 69 6e 74 2c 20 69 6e 74 28 2a 29 28 76  *, int, int(*)(v
20580 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a  oid*), void*);..
20590 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
205a0 4f 70 65 6e 69 6e 67 20 41 20 4e 65 77 20 44 61  Opening A New Da
205b0 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f  tabase Connectio
205c0 6e 20 7b 48 31 32 37 30 30 7d 20 3c 53 34 30 32  n {H12700} <S402
205d0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  00>.**.** These 
205e0 72 6f 75 74 69 6e 65 73 20 6f 70 65 6e 20 61 6e  routines open an
205f0 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65   SQLite database
20600 20 66 69 6c 65 20 77 68 6f 73 65 20 6e 61 6d 65   file whose name
20610 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68 65   is given by the
20620 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 61 72 67  .** filename arg
20630 75 6d 65 6e 74 2e 20 54 68 65 20 66 69 6c 65 6e  ument. The filen
20640 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ame argument is 
20650 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 55  interpreted as U
20660 54 46 2d 38 20 66 6f 72 0a 2a 2a 20 73 71 6c 69  TF-8 for.** sqli
20670 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73  te3_open() and s
20680 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
20690 20 61 6e 64 20 61 73 20 55 54 46 2d 31 36 20 69   and as UTF-16 i
206a0 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 74  n the native byt
206b0 65 0a 2a 2a 20 6f 72 64 65 72 20 66 6f 72 20 73  e.** order for s
206c0 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 2e  qlite3_open16().
206d0 20 41 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e   A [database con
206e0 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 20  nection] handle 
206f0 69 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 72 65  is usually.** re
20700 74 75 72 6e 65 64 20 69 6e 20 2a 70 70 44 62 2c  turned in *ppDb,
20710 20 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f   even if an erro
20720 72 20 6f 63 63 75 72 73 2e 20 20 54 68 65 20 6f  r occurs.  The o
20730 6e 6c 79 20 65 78 63 65 70 74 69 6f 6e 20 69 73  nly exception is
20740 20 74 68 61 74 0a 2a 2a 20 69 66 20 53 51 4c 69   that.** if SQLi
20750 74 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20  te is unable to 
20760 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20  allocate memory 
20770 74 6f 20 68 6f 6c 64 20 74 68 65 20 5b 73 71 6c  to hold the [sql
20780 69 74 65 33 5d 20 6f 62 6a 65 63 74 2c 0a 2a 2a  ite3] object,.**
20790 20 61 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 65 20   a NULL will be 
207a0 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 70  written into *pp
207b0 44 62 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20  Db instead of a 
207c0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b  pointer to the [
207d0 73 71 6c 69 74 65 33 5d 0a 2a 2a 20 6f 62 6a 65  sqlite3].** obje
207e0 63 74 2e 20 49 66 20 74 68 65 20 64 61 74 61 62  ct. If the datab
207f0 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 28 61  ase is opened (a
20800 6e 64 2f 6f 72 20 63 72 65 61 74 65 64 29 20 73  nd/or created) s
20810 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 74 68 65  uccessfully, the
20820 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d  n.** [SQLITE_OK]
20830 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f   is returned.  O
20840 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72  therwise an [err
20850 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75  or code] is retu
20860 72 6e 65 64 2e 20 20 54 68 65 0a 2a 2a 20 5b 73  rned.  The.** [s
20870 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d  qlite3_errmsg()]
20880 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72   or [sqlite3_err
20890 6d 73 67 31 36 28 29 5d 20 72 6f 75 74 69 6e 65  msg16()] routine
208a0 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  s can be used to
208b0 20 6f 62 74 61 69 6e 0a 2a 2a 20 61 6e 20 45 6e   obtain.** an En
208c0 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 20 64  glish language d
208d0 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68  escription of th
208e0 65 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54  e error..**.** T
208f0 68 65 20 64 65 66 61 75 6c 74 20 65 6e 63 6f 64  he default encod
20900 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74 61  ing for the data
20910 62 61 73 65 20 77 69 6c 6c 20 62 65 20 55 54 46  base will be UTF
20920 2d 38 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33  -8 if.** sqlite3
20930 5f 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74  _open() or sqlit
20940 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20  e3_open_v2() is 
20950 63 61 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 55 54  called and.** UT
20960 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74 69  F-16 in the nati
20970 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 69 66  ve byte order if
20980 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28   sqlite3_open16(
20990 29 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a  ) is used..**.**
209a0 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   Whether or not 
209b0 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  an error occurs 
209c0 77 68 65 6e 20 69 74 20 69 73 20 6f 70 65 6e 65  when it is opene
209d0 64 2c 20 72 65 73 6f 75 72 63 65 73 0a 2a 2a 20  d, resources.** 
209e0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
209f0 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  the [database co
20a00 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65  nnection] handle
20a10 20 73 68 6f 75 6c 64 20 62 65 20 72 65 6c 65 61   should be relea
20a20 73 65 64 20 62 79 0a 2a 2a 20 70 61 73 73 69 6e  sed by.** passin
20a30 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33  g it to [sqlite3
20a40 5f 63 6c 6f 73 65 28 29 5d 20 77 68 65 6e 20 69  _close()] when i
20a50 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 72  t is no longer r
20a60 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  equired..**.** T
20a70 68 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  he sqlite3_open_
20a80 76 32 28 29 20 69 6e 74 65 72 66 61 63 65 20 77  v2() interface w
20a90 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 74 65  orks like sqlite
20aa0 33 5f 6f 70 65 6e 28 29 0a 2a 2a 20 65 78 63 65  3_open().** exce
20ab0 70 74 20 74 68 61 74 20 69 74 20 61 63 63 65 70  pt that it accep
20ac0 74 73 20 74 77 6f 20 61 64 64 69 74 69 6f 6e 61  ts two additiona
20ad0 6c 20 70 61 72 61 6d 65 74 65 72 73 20 66 6f 72  l parameters for
20ae0 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6e 74   additional cont
20af0 72 6f 6c 0a 2a 2a 20 6f 76 65 72 20 74 68 65 20  rol.** over the 
20b00 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 6f 6e  new database con
20b10 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 66 6c  nection.  The fl
20b20 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 63 61  ags parameter ca
20b30 6e 20 74 61 6b 65 20 6f 6e 65 20 6f 66 0a 2a 2a  n take one of.**
20b40 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   the following t
20b50 68 72 65 65 20 76 61 6c 75 65 73 2c 20 6f 70 74  hree values, opt
20b60 69 6f 6e 61 6c 6c 79 20 63 6f 6d 62 69 6e 65 64  ionally combined
20b70 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 5b 53   with the .** [S
20b80 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54  QLITE_OPEN_NOMUT
20b90 45 58 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4f  EX] or [SQLITE_O
20ba0 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20 66  PEN_FULLMUTEX] f
20bb0 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e  lags:.**.** <dl>
20bc0 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f  .** <dt>[SQLITE_
20bd0 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 3c 2f  OPEN_READONLY]</
20be0 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64  dt>.** <dd>The d
20bf0 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65  atabase is opene
20c00 64 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d  d in read-only m
20c10 6f 64 65 2e 20 20 49 66 20 74 68 65 20 64 61 74  ode.  If the dat
20c20 61 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a 2a  abase does not.*
20c30 2a 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 2c  * already exist,
20c40 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74   an error is ret
20c50 75 72 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  urned.</dd>.**.*
20c60 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50  * <dt>[SQLITE_OP
20c70 45 4e 5f 52 45 41 44 57 52 49 54 45 5d 3c 2f 64  EN_READWRITE]</d
20c80 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61  t>.** <dd>The da
20c90 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64  tabase is opened
20ca0 20 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64   for reading and
20cb0 20 77 72 69 74 69 6e 67 20 69 66 20 70 6f 73 73   writing if poss
20cc0 69 62 6c 65 2c 20 6f 72 20 72 65 61 64 69 6e 67  ible, or reading
20cd0 0a 2a 2a 20 6f 6e 6c 79 20 69 66 20 74 68 65 20  .** only if the 
20ce0 66 69 6c 65 20 69 73 20 77 72 69 74 65 20 70 72  file is write pr
20cf0 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 20 6f  otected by the o
20d00 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e  perating system.
20d10 20 20 49 6e 20 65 69 74 68 65 72 0a 2a 2a 20 63    In either.** c
20d20 61 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ase the database
20d30 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20 65 78   must already ex
20d40 69 73 74 2c 20 6f 74 68 65 72 77 69 73 65 20 61  ist, otherwise a
20d50 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72  n error is retur
20d60 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ned.</dd>.**.** 
20d70 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  <dt>[SQLITE_OPEN
20d80 5f 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b 53  _READWRITE] | [S
20d90 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
20da0 45 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  E]</dt>.** <dd>T
20db0 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6f  he database is o
20dc0 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69 6e  pened for readin
20dd0 67 20 61 6e 64 20 77 72 69 74 69 6e 67 2c 20 61  g and writing, a
20de0 6e 64 20 69 73 20 63 72 65 61 74 65 73 20 69 74  nd is creates it
20df0 20 69 66 0a 2a 2a 20 69 74 20 64 6f 65 73 20 6e   if.** it does n
20e00 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  ot already exist
20e10 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 62 65  . This is the be
20e20 68 61 76 69 6f 72 20 74 68 61 74 20 69 73 20 61  havior that is a
20e30 6c 77 61 79 73 20 75 73 65 64 20 66 6f 72 0a 2a  lways used for.*
20e40 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29  * sqlite3_open()
20e50 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65   and sqlite3_ope
20e60 6e 31 36 28 29 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c  n16().</dd>.** <
20e70 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  /dl>.**.** If th
20e80 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20  e 3rd parameter 
20e90 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  to sqlite3_open_
20ea0 76 32 28 29 20 69 73 20 6e 6f 74 20 6f 6e 65 20  v2() is not one 
20eb0 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6d 62 69 6e  of the.** combin
20ec0 61 74 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f  ations shown abo
20ed0 76 65 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65  ve or one of the
20ee0 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73 68   combinations sh
20ef0 6f 77 6e 20 61 62 6f 76 65 20 63 6f 6d 62 69 6e  own above combin
20f00 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b  ed.** with the [
20f10 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55  SQLITE_OPEN_NOMU
20f20 54 45 58 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f  TEX] or [SQLITE_
20f30 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20  OPEN_FULLMUTEX] 
20f40 66 6c 61 67 73 2c 0a 2a 2a 20 74 68 65 6e 20 74  flags,.** then t
20f50 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75  he behavior is u
20f60 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  ndefined..**.** 
20f70 49 66 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f  If the [SQLITE_O
20f80 50 45 4e 5f 4e 4f 4d 55 54 45 58 5d 20 66 6c 61  PEN_NOMUTEX] fla
20f90 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74  g is set, then t
20fa0 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
20fb0 65 63 74 69 6f 6e 0a 2a 2a 20 6f 70 65 6e 73 20  ection.** opens 
20fc0 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72  in the multi-thr
20fd0 65 61 64 20 5b 74 68 72 65 61 64 69 6e 67 20 6d  ead [threading m
20fe0 6f 64 65 5d 20 61 73 20 6c 6f 6e 67 20 61 73 20  ode] as long as 
20ff0 74 68 65 20 73 69 6e 67 6c 65 2d 74 68 72 65 61  the single-threa
21000 64 0a 2a 2a 20 6d 6f 64 65 20 68 61 73 20 6e 6f  d.** mode has no
21010 74 20 62 65 65 6e 20 73 65 74 20 61 74 20 63 6f  t been set at co
21020 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 72 20 73 74  mpile-time or st
21030 61 72 74 2d 74 69 6d 65 2e 20 20 49 66 20 74 68  art-time.  If th
21040 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45  e.** [SQLITE_OPE
21050 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20 66 6c 61  N_FULLMUTEX] fla
21060 67 20 69 73 20 73 65 74 20 74 68 65 6e 20 74 68  g is set then th
21070 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
21080 63 74 69 6f 6e 20 6f 70 65 6e 73 0a 2a 2a 20 69  ction opens.** i
21090 6e 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64  n the serialized
210a0 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65   [threading mode
210b0 5d 20 75 6e 6c 65 73 73 20 73 69 6e 67 6c 65 2d  ] unless single-
210c0 74 68 72 65 61 64 20 77 61 73 0a 2a 2a 20 70 72  thread was.** pr
210d0 65 76 69 6f 75 73 6c 79 20 73 65 6c 65 63 74 65  eviously selecte
210e0 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  d at compile-tim
210f0 65 20 6f 72 20 73 74 61 72 74 2d 74 69 6d 65 2e  e or start-time.
21100 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69  .**.** If the fi
21110 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f  lename is ":memo
21120 72 79 3a 22 2c 20 74 68 65 6e 20 61 20 70 72 69  ry:", then a pri
21130 76 61 74 65 2c 20 74 65 6d 70 6f 72 61 72 79 20  vate, temporary 
21140 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
21150 73 65 0a 2a 2a 20 69 73 20 63 72 65 61 74 65 64  se.** is created
21160 20 66 6f 72 20 74 68 65 20 63 6f 6e 6e 65 63 74   for the connect
21170 69 6f 6e 2e 20 20 54 68 69 73 20 69 6e 2d 6d 65  ion.  This in-me
21180 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 77 69  mory database wi
21190 6c 6c 20 76 61 6e 69 73 68 20 77 68 65 6e 0a 2a  ll vanish when.*
211a0 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  * the database c
211b0 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f  onnection is clo
211c0 73 65 64 2e 20 20 46 75 74 75 72 65 20 76 65 72  sed.  Future ver
211d0 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20  sions of SQLite 
211e0 6d 69 67 68 74 0a 2a 2a 20 6d 61 6b 65 20 75 73  might.** make us
211f0 65 20 6f 66 20 61 64 64 69 74 69 6f 6e 61 6c 20  e of additional 
21200 73 70 65 63 69 61 6c 20 66 69 6c 65 6e 61 6d 65  special filename
21210 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 69 74  s that begin wit
21220 68 20 74 68 65 20 22 3a 22 20 63 68 61 72 61 63  h the ":" charac
21230 74 65 72 2e 0a 2a 2a 20 49 74 20 69 73 20 72 65  ter..** It is re
21240 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 77  commended that w
21250 68 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 66  hen a database f
21260 69 6c 65 6e 61 6d 65 20 61 63 74 75 61 6c 6c 79  ilename actually
21270 20 64 6f 65 73 20 62 65 67 69 6e 20 77 69 74 68   does begin with
21280 0a 2a 2a 20 61 20 22 3a 22 20 63 68 61 72 61 63  .** a ":" charac
21290 74 65 72 20 79 6f 75 20 73 68 6f 75 6c 64 20 70  ter you should p
212a0 72 65 66 69 78 20 74 68 65 20 66 69 6c 65 6e 61  refix the filena
212b0 6d 65 20 77 69 74 68 20 61 20 70 61 74 68 6e 61  me with a pathna
212c0 6d 65 20 73 75 63 68 20 61 73 0a 2a 2a 20 22 2e  me such as.** ".
212d0 2f 22 20 74 6f 20 61 76 6f 69 64 20 61 6d 62 69  /" to avoid ambi
212e0 67 75 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  guity..**.** If 
212f0 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 69 73 20  the filename is 
21300 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c  an empty string,
21310 20 74 68 65 6e 20 61 20 70 72 69 76 61 74 65 2c   then a private,
21320 20 74 65 6d 70 6f 72 61 72 79 0a 2a 2a 20 6f 6e   temporary.** on
21330 2d 64 69 73 6b 20 64 61 74 61 62 61 73 65 20 77  -disk database w
21340 69 6c 6c 20 62 65 20 63 72 65 61 74 65 64 2e 20  ill be created. 
21350 20 54 68 69 73 20 70 72 69 76 61 74 65 20 64 61   This private da
21360 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 0a 2a  tabase will be.*
21370 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  * automatically 
21380 64 65 6c 65 74 65 64 20 61 73 20 73 6f 6f 6e 20  deleted as soon 
21390 61 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  as the database 
213a0 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c  connection is cl
213b0 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  osed..**.** The 
213c0 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72  fourth parameter
213d0 20 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e   to sqlite3_open
213e0 5f 76 32 28 29 20 69 73 20 74 68 65 20 6e 61 6d  _v2() is the nam
213f0 65 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c  e of the.** [sql
21400 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74  ite3_vfs] object
21410 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68   that defines th
21420 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  e operating syst
21430 65 6d 20 69 6e 74 65 72 66 61 63 65 20 74 68 61  em interface tha
21440 74 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74  t.** the new dat
21450 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
21460 20 73 68 6f 75 6c 64 20 75 73 65 2e 20 20 49 66   should use.  If
21470 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 61   the fourth para
21480 6d 65 74 65 72 20 69 73 0a 2a 2a 20 61 20 4e 55  meter is.** a NU
21490 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20  LL pointer then 
214a0 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71 6c  the default [sql
214b0 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74  ite3_vfs] object
214c0 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20   is used..**.** 
214d0 3c 62 3e 4e 6f 74 65 20 74 6f 20 57 69 6e 64 6f  <b>Note to Windo
214e0 77 73 20 75 73 65 72 73 3a 3c 2f 62 3e 20 20 54  ws users:</b>  T
214f0 68 65 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64  he encoding used
21500 20 66 6f 72 20 74 68 65 20 66 69 6c 65 6e 61 6d   for the filenam
21510 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 6f 66  e argument.** of
21520 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20   sqlite3_open() 
21530 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  and sqlite3_open
21540 5f 76 32 28 29 20 6d 75 73 74 20 62 65 20 55 54  _v2() must be UT
21550 46 2d 38 2c 20 6e 6f 74 20 77 68 61 74 65 76 65  F-8, not whateve
21560 72 0a 2a 2a 20 63 6f 64 65 70 61 67 65 20 69 73  r.** codepage is
21570 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e   currently defin
21580 65 64 2e 20 20 46 69 6c 65 6e 61 6d 65 73 20 63  ed.  Filenames c
21590 6f 6e 74 61 69 6e 69 6e 67 20 69 6e 74 65 72 6e  ontaining intern
215a0 61 74 69 6f 6e 61 6c 0a 2a 2a 20 63 68 61 72 61  ational.** chara
215b0 63 74 65 72 73 20 6d 75 73 74 20 62 65 20 63 6f  cters must be co
215c0 6e 76 65 72 74 65 64 20 74 6f 20 55 54 46 2d 38  nverted to UTF-8
215d0 20 70 72 69 6f 72 20 74 6f 20 70 61 73 73 69 6e   prior to passin
215e0 67 20 74 68 65 6d 20 69 6e 74 6f 0a 2a 2a 20 73  g them into.** s
215f0 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 6f 72  qlite3_open() or
21600 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32   sqlite3_open_v2
21610 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49  ()..**.** INVARI
21620 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  ANTS:.**.** {H12
21630 37 30 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  701} The [sqlite
21640 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69  3_open()], [sqli
21650 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 61  te3_open16()], a
21660 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  nd.**          [
21670 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
21680 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 63 72  )] interfaces cr
21690 65 61 74 65 20 61 20 6e 65 77 0a 2a 2a 20 20 20  eate a new.**   
216a0 20 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 65         [database
216b0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 73 73   connection] ass
216c0 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20  ociated with.** 
216d0 20 20 20 20 20 20 20 20 20 74 68 65 20 64 61 74           the dat
216e0 61 62 61 73 65 20 66 69 6c 65 20 67 69 76 65 6e  abase file given
216f0 20 69 6e 20 74 68 65 69 72 20 66 69 72 73 74 20   in their first 
21700 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a  parameter..**.**
21710 20 7b 48 31 32 37 30 32 7d 20 54 68 65 20 66 69   {H12702} The fi
21720 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20  lename argument 
21730 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61  is interpreted a
21740 73 20 55 54 46 2d 38 0a 2a 2a 20 20 20 20 20 20  s UTF-8.**      
21750 20 20 20 20 66 6f 72 20 5b 73 71 6c 69 74 65 33      for [sqlite3
21760 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 5b 73 71  _open()] and [sq
21770 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d  lite3_open_v2()]
21780 20 61 6e 64 20 61 73 20 55 54 46 2d 31 36 0a 2a   and as UTF-16.*
21790 2a 20 20 20 20 20 20 20 20 20 20 69 6e 20 74 68  *          in th
217a0 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72  e native byte or
217b0 64 65 72 20 66 6f 72 20 5b 73 71 6c 69 74 65 33  der for [sqlite3
217c0 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 0a 2a  _open16()]..**.*
217d0 2a 20 7b 48 31 32 37 30 33 7d 20 41 20 73 75 63  * {H12703} A suc
217e0 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 74 69  cessful invocati
217f0 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 6f  on of [sqlite3_o
21800 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33  pen()], [sqlite3
21810 5f 6f 70 65 6e 31 36 28 29 5d 2c 0a 2a 2a 20 20  _open16()],.**  
21820 20 20 20 20 20 20 20 20 6f 72 20 5b 73 71 6c 69          or [sqli
21830 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 77  te3_open_v2()] w
21840 72 69 74 65 73 20 61 20 70 6f 69 6e 74 65 72 20  rites a pointer 
21850 74 6f 20 61 20 6e 65 77 0a 2a 2a 20 20 20 20 20  to a new.**     
21860 20 20 20 20 20 5b 64 61 74 61 62 61 73 65 20 63       [database c
21870 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 6e 74 6f 20  onnection] into 
21880 2a 70 70 44 62 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  *ppDb..**.** {H1
21890 32 37 30 34 7d 20 54 68 65 20 5b 73 71 6c 69 74  2704} The [sqlit
218a0 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c  e3_open()], [sql
218b0 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20  ite3_open16()], 
218c0 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  and.**          
218d0 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32  [sqlite3_open_v2
218e0 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 72  ()] interfaces r
218f0 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b  eturn [SQLITE_OK
21900 5d 20 75 70 6f 6e 20 73 75 63 63 65 73 73 2c 0a  ] upon success,.
21910 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 61  **          or a
21920 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b 65  n appropriate [e
21930 72 72 6f 72 20 63 6f 64 65 5d 20 6f 6e 20 66 61  rror code] on fa
21940 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  ilure..**.** {H1
21950 32 37 30 36 7d 20 54 68 65 20 64 65 66 61 75 6c  2706} The defaul
21960 74 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20  t text encoding 
21970 66 6f 72 20 61 20 6e 65 77 20 64 61 74 61 62 61  for a new databa
21980 73 65 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  se created using
21990 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71  .**          [sq
219a0 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 20 6f 72  lite3_open()] or
219b0 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76   [sqlite3_open_v
219c0 32 28 29 5d 20 77 69 6c 6c 20 62 65 20 55 54 46  2()] will be UTF
219d0 2d 38 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 37 30  -8..**.** {H1270
219e0 37 7d 20 54 68 65 20 64 65 66 61 75 6c 74 20 74  7} The default t
219f0 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72  ext encoding for
21a00 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20   a new database 
21a10 63 72 65 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a  created using.**
21a20 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
21a30 65 33 5f 6f 70 65 6e 31 36 28 29 5d 20 77 69 6c  e3_open16()] wil
21a40 6c 20 62 65 20 55 54 46 2d 31 36 2e 0a 2a 2a 0a  l be UTF-16..**.
21a50 2a 2a 20 7b 48 31 32 37 30 39 7d 20 54 68 65 20  ** {H12709} The 
21a60 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 46 2c  [sqlite3_open(F,
21a70 44 29 5d 20 69 6e 74 65 72 66 61 63 65 20 69 73  D)] interface is
21a80 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 0a 2a   equivalent to.*
21a90 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
21aa0 74 65 33 5f 6f 70 65 6e 5f 76 32 28 46 2c 44 2c  te3_open_v2(F,D,
21ab0 47 2c 30 29 5d 20 77 68 65 72 65 20 74 68 65 20  G,0)] where the 
21ac0 47 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a  G parameter is.*
21ad0 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49  *          [SQLI
21ae0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
21af0 45 5d 7c 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  E]|[SQLITE_OPEN_
21b00 43 52 45 41 54 45 5d 2e 0a 2a 2a 0a 2a 2a 20 7b  CREATE]..**.** {
21b10 48 31 32 37 31 31 7d 20 49 66 20 74 68 65 20 47  H12711} If the G
21b20 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73   parameter to [s
21b30 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 46  qlite3_open_v2(F
21b40 2c 44 2c 47 2c 56 29 5d 20 63 6f 6e 74 61 69 6e  ,D,G,V)] contain
21b50 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  s the.**        
21b60 20 20 62 69 74 20 76 61 6c 75 65 20 5b 53 51 4c    bit value [SQL
21b70 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c  ITE_OPEN_READONL
21b80 59 5d 20 74 68 65 6e 20 74 68 65 20 64 61 74 61  Y] then the data
21b90 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 0a 2a  base is opened.*
21ba0 2a 20 20 20 20 20 20 20 20 20 20 66 6f 72 20 72  *          for r
21bb0 65 61 64 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2a 0a  eading only..**.
21bc0 2a 2a 20 7b 48 31 32 37 31 32 7d 20 49 66 20 74  ** {H12712} If t
21bd0 68 65 20 47 20 70 61 72 61 6d 65 74 65 72 20 74  he G parameter t
21be0 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  o [sqlite3_open_
21bf0 76 32 28 46 2c 44 2c 47 2c 56 29 5d 20 63 6f 6e  v2(F,D,G,V)] con
21c00 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20 20 20 20  tains the.**    
21c10 20 20 20 20 20 20 62 69 74 20 76 61 6c 75 65 20        bit value 
21c20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41  [SQLITE_OPEN_REA
21c30 44 57 52 49 54 45 5d 20 74 68 65 6e 20 74 68 65  DWRITE] then the
21c40 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65   database is ope
21c50 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ned.**          
21c60 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72 69 74  reading and writ
21c70 69 6e 67 20 69 66 20 70 6f 73 73 69 62 6c 65 2c  ing if possible,
21c80 20 6f 72 20 66 6f 72 20 72 65 61 64 69 6e 67 20   or for reading 
21c90 6f 6e 6c 79 20 69 66 20 74 68 65 0a 2a 2a 20 20  only if the.**  
21ca0 20 20 20 20 20 20 20 20 66 69 6c 65 20 69 73 20          file is 
21cb0 77 72 69 74 65 20 70 72 6f 74 65 63 74 65 64 20  write protected 
21cc0 62 79 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67  by the operating
21cd0 20 73 79 73 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 7b   system..**.** {
21ce0 48 31 32 37 31 33 7d 20 49 66 20 74 68 65 20 47  H12713} If the G
21cf0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73   parameter to [s
21d00 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 46  qlite3_open_v2(F
21d10 2c 44 2c 47 2c 56 29 5d 20 6f 6d 69 74 73 20 74  ,D,G,V)] omits t
21d20 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62  he.**          b
21d30 69 74 20 76 61 6c 75 65 20 5b 53 51 4c 49 54 45  it value [SQLITE
21d40 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d 20 61 6e  _OPEN_CREATE] an
21d50 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 64  d the database d
21d60 6f 65 73 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20  oes not.**      
21d70 20 20 20 20 70 72 65 76 69 6f 75 73 6c 79 20 65      previously e
21d80 78 69 73 74 2c 20 61 6e 20 65 72 72 6f 72 20 69  xist, an error i
21d90 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
21da0 2a 20 7b 48 31 32 37 31 34 7d 20 49 66 20 74 68  * {H12714} If th
21db0 65 20 47 20 70 61 72 61 6d 65 74 65 72 20 74 6f  e G parameter to
21dc0 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76   [sqlite3_open_v
21dd0 32 28 46 2c 44 2c 47 2c 56 29 5d 20 63 6f 6e 74  2(F,D,G,V)] cont
21de0 61 69 6e 73 20 74 68 65 0a 2a 2a 20 20 20 20 20  ains the.**     
21df0 20 20 20 20 20 62 69 74 20 76 61 6c 75 65 20 5b       bit value [
21e00 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
21e10 54 45 5d 20 61 6e 64 20 74 68 65 20 64 61 74 61  TE] and the data
21e20 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a  base does not.**
21e30 20 20 20 20 20 20 20 20 20 20 70 72 65 76 69 6f            previo
21e40 75 73 6c 79 20 65 78 69 73 74 2c 20 74 68 65 6e  usly exist, then
21e50 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d   an attempt is m
21e60 61 64 65 20 74 6f 20 63 72 65 61 74 65 20 61 6e  ade to create an
21e70 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e  d.**          in
21e80 69 74 69 61 6c 69 7a 65 20 74 68 65 20 64 61 74  itialize the dat
21e90 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  abase..**.** {H1
21ea0 32 37 31 37 7d 20 49 66 20 74 68 65 20 66 69 6c  2717} If the fil
21eb0 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 74  ename argument t
21ec0 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  o [sqlite3_open(
21ed0 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  )], [sqlite3_ope
21ee0 6e 31 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20  n16()],.**      
21ef0 20 20 20 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f      or [sqlite3_
21f00 6f 70 65 6e 5f 76 32 28 29 5d 20 69 73 20 22 3a  open_v2()] is ":
21f10 6d 65 6d 6f 72 79 3a 22 2c 20 74 68 65 6e 20 61  memory:", then a
21f20 6e 20 70 72 69 76 61 74 65 2c 0a 2a 2a 20 20 20  n private,.**   
21f30 20 20 20 20 20 20 20 65 70 68 65 6d 65 72 61 6c         ephemeral
21f40 2c 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  , in-memory data
21f50 62 61 73 65 20 69 73 20 63 72 65 61 74 65 64 20  base is created 
21f60 66 6f 72 20 74 68 65 20 63 6f 6e 6e 65 63 74 69  for the connecti
21f70 6f 6e 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  on..**          
21f80 3c 74 6f 64 6f 3e 49 73 20 53 51 4c 49 54 45 5f  <todo>Is SQLITE_
21f90 4f 50 45 4e 5f 43 52 45 41 54 45 7c 53 51 4c 49  OPEN_CREATE|SQLI
21fa0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
21fb0 45 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 20 20  E required.**   
21fc0 20 20 20 20 20 20 20 69 6e 20 73 71 6c 69 74 65         in sqlite
21fd0 33 5f 6f 70 65 6e 5f 76 32 28 29 3f 3c 2f 74 6f  3_open_v2()?</to
21fe0 64 6f 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 37 31  do>.**.** {H1271
21ff0 39 7d 20 49 66 20 74 68 65 20 66 69 6c 65 6e 61  9} If the filena
22000 6d 65 20 69 73 20 4e 55 4c 4c 20 6f 72 20 61 6e  me is NULL or an
22010 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c 20 74   empty string, t
22020 68 65 6e 20 61 20 70 72 69 76 61 74 65 2c 0a 2a  hen a private,.*
22030 2a 20 20 20 20 20 20 20 20 20 20 65 70 68 65 6d  *          ephem
22040 65 72 61 6c 20 6f 6e 2d 64 69 73 6b 20 64 61 74  eral on-disk dat
22050 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20 63 72  abase will be cr
22060 65 61 74 65 64 2e 0a 2a 2a 20 20 20 20 20 20 20  eated..**       
22070 20 20 20 3c 74 6f 64 6f 3e 49 73 20 53 51 4c 49     <todo>Is SQLI
22080 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 53  TE_OPEN_CREATE|S
22090 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
220a0 52 49 54 45 20 72 65 71 75 69 72 65 64 0a 2a 2a  RITE required.**
220b0 20 20 20 20 20 20 20 20 20 20 69 6e 20 73 71 6c            in sql
220c0 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 3f 3c  ite3_open_v2()?<
220d0 2f 74 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31  /todo>.**.** {H1
220e0 32 37 32 31 7d 20 54 68 65 20 5b 64 61 74 61 62  2721} The [datab
220f0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
22100 63 72 65 61 74 65 64 20 62 79 20 5b 73 71 6c 69  created by [sqli
22110 74 65 33 5f 6f 70 65 6e 5f 76 32 28 46 2c 44 2c  te3_open_v2(F,D,
22120 47 2c 56 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20  G,V)].**        
22130 20 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20 5b    will use the [
22140 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a  sqlite3_vfs] obj
22150 65 63 74 20 69 64 65 6e 74 69 66 69 65 64 20 62  ect identified b
22160 79 20 74 68 65 20 56 20 70 61 72 61 6d 65 74 65  y the V paramete
22170 72 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  r,.**          o
22180 72 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73  r the default [s
22190 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65  qlite3_vfs] obje
221a0 63 74 20 69 66 20 56 20 69 73 20 61 20 4e 55 4c  ct if V is a NUL
221b0 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a  L pointer..**.**
221c0 20 7b 48 31 32 37 32 33 7d 20 54 77 6f 20 5b 64   {H12723} Two [d
221d0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
221e0 6f 6e 73 5d 20 77 69 6c 6c 20 73 68 61 72 65 20  ons] will share 
221f0 61 20 63 6f 6d 6d 6f 6e 20 63 61 63 68 65 20 69  a common cache i
22200 66 20 62 6f 74 68 20 77 65 72 65 0a 2a 2a 20 20  f both were.**  
22210 20 20 20 20 20 20 20 20 6f 70 65 6e 65 64 20 77          opened w
22220 69 74 68 20 74 68 65 20 73 61 6d 65 20 56 46 53  ith the same VFS
22230 20 77 68 69 6c 65 20 5b 73 68 61 72 65 64 20 63   while [shared c
22240 61 63 68 65 20 6d 6f 64 65 5d 20 77 61 73 20 65  ache mode] was e
22250 6e 61 62 6c 65 64 20 61 6e 64 0a 2a 2a 20 20 20  nabled and.**   
22260 20 20 20 20 20 20 20 69 66 20 62 6f 74 68 20 66         if both f
22270 69 6c 65 6e 61 6d 65 73 20 63 6f 6d 70 61 72 65  ilenames compare
22280 20 65 71 75 61 6c 20 75 73 69 6e 67 20 6d 65 6d   equal using mem
22290 63 6d 70 28 29 20 61 66 74 65 72 20 68 61 76 69  cmp() after havi
222a0 6e 67 20 62 65 65 6e 0a 2a 2a 20 20 20 20 20 20  ng been.**      
222b0 20 20 20 20 70 72 6f 63 65 73 73 65 64 20 62 79      processed by
222c0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66   the [sqlite3_vf
222d0 73 20 7c 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d  s | xFullPathnam
222e0 65 5d 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65  e] method of the
222f0 20 56 46 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   VFS..*/.SQLITE_
22300 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
22310 6f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68  open(.  const ch
22320 61 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20  ar *filename,   
22330 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c 65  /* Database file
22340 6e 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a  name (UTF-8) */.
22350 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62    sqlite3 **ppDb
22360 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
22370 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e 64  : SQLite db hand
22380 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f  le */.);.SQLITE_
22390 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
223a0 6f 70 65 6e 31 36 28 0a 20 20 63 6f 6e 73 74 20  open16(.  const 
223b0 76 6f 69 64 20 2a 66 69 6c 65 6e 61 6d 65 2c 20  void *filename, 
223c0 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69    /* Database fi
223d0 6c 65 6e 61 6d 65 20 28 55 54 46 2d 31 36 29 20  lename (UTF-16) 
223e0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70  */.  sqlite3 **p
223f0 70 44 62 20 20 20 20 20 20 20 20 20 20 2f 2a 20  pDb          /* 
22400 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 62 20 68  OUT: SQLite db h
22410 61 6e 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49  andle */.);.SQLI
22420 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
22430 65 33 5f 6f 70 65 6e 5f 76 32 28 0a 20 20 63 6f  e3_open_v2(.  co
22440 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e 61  nst char *filena
22450 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61 73  me,   /* Databas
22460 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d  e filename (UTF-
22470 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  8) */.  sqlite3 
22480 2a 2a 70 70 44 62 2c 20 20 20 20 20 20 20 20 20  **ppDb,         
22490 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 64  /* OUT: SQLite d
224a0 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e  b handle */.  in
224b0 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20  t flags,        
224c0 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a        /* Flags *
224d0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
224e0 7a 56 66 73 20 20 20 20 20 20 20 20 2f 2a 20 4e  zVfs        /* N
224f0 61 6d 65 20 6f 66 20 56 46 53 20 6d 6f 64 75 6c  ame of VFS modul
22500 65 20 74 6f 20 75 73 65 20 2a 2f 0a 29 3b 0a 0a  e to use */.);..
22510 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
22520 45 72 72 6f 72 20 43 6f 64 65 73 20 41 6e 64 20  Error Codes And 
22530 4d 65 73 73 61 67 65 73 20 7b 48 31 32 38 30 30  Messages {H12800
22540 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 0a 2a 2a  } <S60200>.**.**
22550 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 72 72   The sqlite3_err
22560 63 6f 64 65 28 29 20 69 6e 74 65 72 66 61 63 65  code() interface
22570 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
22580 65 72 69 63 20 5b 72 65 73 75 6c 74 20 63 6f 64  eric [result cod
22590 65 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e 64  e] or.** [extend
225a0 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20  ed result code] 
225b0 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63  for the most rec
225c0 65 6e 74 20 66 61 69 6c 65 64 20 73 71 6c 69 74  ent failed sqlit
225d0 65 33 5f 2a 20 41 50 49 20 63 61 6c 6c 0a 2a 2a  e3_* API call.**
225e0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
225f0 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e   a [database con
22600 6e 65 63 74 69 6f 6e 5d 2e 20 49 66 20 61 20 70  nection]. If a p
22610 72 69 6f 72 20 41 50 49 20 63 61 6c 6c 20 66 61  rior API call fa
22620 69 6c 65 64 0a 2a 2a 20 62 75 74 20 74 68 65 20  iled.** but the 
22630 6d 6f 73 74 20 72 65 63 65 6e 74 20 41 50 49 20  most recent API 
22640 63 61 6c 6c 20 73 75 63 63 65 65 64 65 64 2c 20  call succeeded, 
22650 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
22660 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33   from.** sqlite3
22670 5f 65 72 72 63 6f 64 65 28 29 20 69 73 20 75 6e  _errcode() is un
22680 64 65 66 69 6e 65 64 2e 20 20 54 68 65 20 73 71  defined.  The sq
22690 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 65  lite3_extended_e
226a0 72 72 63 6f 64 65 28 29 0a 2a 2a 20 69 6e 74 65  rrcode().** inte
226b0 72 66 61 63 65 20 69 73 20 74 68 65 20 73 61 6d  rface is the sam
226c0 65 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74  e except that it
226d0 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20   always returns 
226e0 74 68 65 20 0a 2a 2a 20 5b 65 78 74 65 6e 64 65  the .** [extende
226f0 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 65  d result code] e
22700 76 65 6e 20 77 68 65 6e 20 65 78 74 65 6e 64 65  ven when extende
22710 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61  d result codes a
22720 72 65 0a 2a 2a 20 64 69 73 61 62 6c 65 64 2e 0a  re.** disabled..
22730 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
22740 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73  3_errmsg() and s
22750 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28  qlite3_errmsg16(
22760 29 20 72 65 74 75 72 6e 20 45 6e 67 6c 69 73 68  ) return English
22770 2d 6c 61 6e 67 75 61 67 65 0a 2a 2a 20 74 65 78  -language.** tex
22780 74 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73  t that describes
22790 20 74 68 65 20 65 72 72 6f 72 2c 20 61 73 20 65   the error, as e
227a0 69 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20 55  ither UTF-8 or U
227b0 54 46 2d 31 36 20 72 65 73 70 65 63 74 69 76 65  TF-16 respective
227c0 6c 79 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74 6f  ly..** Memory to
227d0 20 68 6f 6c 64 20 74 68 65 20 65 72 72 6f 72 20   hold the error 
227e0 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 20 69  message string i
227f0 73 20 6d 61 6e 61 67 65 64 20 69 6e 74 65 72 6e  s managed intern
22800 61 6c 6c 79 2e 0a 2a 2a 20 54 68 65 20 61 70 70  ally..** The app
22810 6c 69 63 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f  lication does no
22820 74 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 79 20  t need to worry 
22830 61 62 6f 75 74 20 66 72 65 65 69 6e 67 20 74 68  about freeing th
22840 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 48 6f 77  e result..** How
22850 65 76 65 72 2c 20 74 68 65 20 65 72 72 6f 72 20  ever, the error 
22860 73 74 72 69 6e 67 20 6d 69 67 68 74 20 62 65 20  string might be 
22870 6f 76 65 72 77 72 69 74 74 65 6e 20 6f 72 20 64  overwritten or d
22880 65 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a 2a  eallocated by.**
22890 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
228a0 73 20 74 6f 20 6f 74 68 65 72 20 53 51 4c 69 74  s to other SQLit
228b0 65 20 69 6e 74 65 72 66 61 63 65 20 66 75 6e 63  e interface func
228c0 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  tions..**.** Whe
228d0 6e 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64  n the serialized
228e0 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65   [threading mode
228f0 5d 20 69 73 20 69 6e 20 75 73 65 2c 20 69 74 20  ] is in use, it 
22900 6d 69 67 68 74 20 62 65 20 74 68 65 0a 2a 2a 20  might be the.** 
22910 63 61 73 65 20 74 68 61 74 20 61 20 73 65 63 6f  case that a seco
22920 6e 64 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  nd error occurs 
22930 6f 6e 20 61 20 73 65 70 61 72 61 74 65 20 74 68  on a separate th
22940 72 65 61 64 20 69 6e 20 62 65 74 77 65 65 6e 0a  read in between.
22950 2a 2a 20 74 68 65 20 74 69 6d 65 20 6f 66 20 74  ** the time of t
22960 68 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 61  he first error a
22970 6e 64 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 74  nd the call to t
22980 68 65 73 65 20 69 6e 74 65 72 66 61 63 65 73 2e  hese interfaces.
22990 0a 2a 2a 20 57 68 65 6e 20 74 68 61 74 20 68 61  .** When that ha
229a0 70 70 65 6e 73 2c 20 74 68 65 20 73 65 63 6f 6e  ppens, the secon
229b0 64 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20  d error will be 
229c0 72 65 70 6f 72 74 65 64 20 73 69 6e 63 65 20 74  reported since t
229d0 68 65 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63  hese.** interfac
229e0 65 73 20 61 6c 77 61 79 73 20 72 65 70 6f 72 74  es always report
229f0 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
22a00 20 72 65 73 75 6c 74 2e 20 20 54 6f 20 61 76 6f   result.  To avo
22a10 69 64 0a 2a 2a 20 74 68 69 73 2c 20 65 61 63 68  id.** this, each
22a20 20 74 68 72 65 61 64 20 63 61 6e 20 6f 62 74 61   thread can obta
22a30 69 6e 20 65 78 63 6c 75 73 69 76 65 20 75 73 65  in exclusive use
22a40 20 6f 66 20 74 68 65 20 5b 64 61 74 61 62 61 73   of the [databas
22a50 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 0a  e connection] D.
22a60 2a 2a 20 62 79 20 69 6e 76 6f 6b 69 6e 67 20 5b  ** by invoking [
22a70 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
22a80 74 65 72 5d 28 5b 73 71 6c 69 74 65 33 5f 64 62  ter]([sqlite3_db
22a90 5f 6d 75 74 65 78 5d 28 44 29 29 20 62 65 66 6f  _mutex](D)) befo
22aa0 72 65 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20  re beginning.** 
22ab0 74 6f 20 75 73 65 20 44 20 61 6e 64 20 69 6e 76  to use D and inv
22ac0 6f 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d  oking [sqlite3_m
22ad0 75 74 65 78 5f 6c 65 61 76 65 5d 28 5b 73 71 6c  utex_leave]([sql
22ae0 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d 28 44  ite3_db_mutex](D
22af0 29 29 20 61 66 74 65 72 0a 2a 2a 20 61 6c 6c 20  )) after.** all 
22b00 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 69 6e 74  calls to the int
22b10 65 72 66 61 63 65 73 20 6c 69 73 74 65 64 20 68  erfaces listed h
22b20 65 72 65 20 61 72 65 20 63 6f 6d 70 6c 65 74 65  ere are complete
22b30 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69  d..**.** If an i
22b40 6e 74 65 72 66 61 63 65 20 66 61 69 6c 73 20 77  nterface fails w
22b50 69 74 68 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  ith SQLITE_MISUS
22b60 45 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  E, that means th
22b70 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 77  e interface.** w
22b80 61 73 20 69 6e 76 6f 6b 65 64 20 69 6e 63 6f 72  as invoked incor
22b90 72 65 63 74 6c 79 20 62 79 20 74 68 65 20 61 70  rectly by the ap
22ba0 70 6c 69 63 61 74 69 6f 6e 2e 20 20 49 6e 20 74  plication.  In t
22bb0 68 61 74 20 63 61 73 65 2c 20 74 68 65 0a 2a 2a  hat case, the.**
22bc0 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20   error code and 
22bd0 6d 65 73 73 61 67 65 20 6d 61 79 20 6f 72 20 6d  message may or m
22be0 61 79 20 6e 6f 74 20 62 65 20 73 65 74 2e 0a 2a  ay not be set..*
22bf0 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
22c00 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 38 30 31 7d 20  .**.** {H12801} 
22c10 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 65 72 72  The [sqlite3_err
22c20 63 6f 64 65 28 44 29 5d 20 69 6e 74 65 72 66 61  code(D)] interfa
22c30 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  ce returns the n
22c40 75 6d 65 72 69 63 0a 2a 2a 20 20 20 20 20 20 20  umeric.**       
22c50 20 20 20 5b 72 65 73 75 6c 74 20 63 6f 64 65 5d     [result code]
22c60 20 6f 72 20 5b 65 78 74 65 6e 64 65 64 20 72 65   or [extended re
22c70 73 75 6c 74 20 63 6f 64 65 5d 20 66 6f 72 20 74  sult code] for t
22c80 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  he most recently
22c90 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 61 69  .**          fai
22ca0 6c 65 64 20 69 6e 74 65 72 66 61 63 65 20 63 61  led interface ca
22cb0 6c 6c 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ll associated wi
22cc0 74 68 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  th the [database
22cd0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 2e 0a   connection] D..
22ce0 2a 2a 0a 2a 2a 20 7b 48 31 32 38 30 32 7d 20 54  **.** {H12802} T
22cf0 68 65 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 65  he [sqlite3_exte
22d00 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 44 29 5d  nded_errcode(D)]
22d10 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72   interface retur
22d20 6e 73 20 74 68 65 20 6e 75 6d 65 72 69 63 0a 2a  ns the numeric.*
22d30 2a 20 20 20 20 20 20 20 20 20 20 5b 65 78 74 65  *          [exte
22d40 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65  nded result code
22d50 5d 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72  ] for the most r
22d60 65 63 65 6e 74 6c 79 0a 2a 2a 20 20 20 20 20 20  ecently.**      
22d70 20 20 20 20 66 61 69 6c 65 64 20 69 6e 74 65 72      failed inter
22d80 66 61 63 65 20 63 61 6c 6c 20 61 73 73 6f 63 69  face call associ
22d90 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64  ated with the [d
22da0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
22db0 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  on] D..**.** {H1
22dc0 32 38 30 33 7d 20 54 68 65 20 5b 73 71 6c 69 74  2803} The [sqlit
22dd0 65 33 5f 65 72 72 6d 73 67 28 44 29 5d 20 61 6e  e3_errmsg(D)] an
22de0 64 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  d [sqlite3_errms
22df0 67 31 36 28 44 29 5d 0a 2a 2a 20 20 20 20 20 20  g16(D)].**      
22e00 20 20 20 20 69 6e 74 65 72 66 61 63 65 73 20 72      interfaces r
22e10 65 74 75 72 6e 20 45 6e 67 6c 69 73 68 2d 6c 61  eturn English-la
22e20 6e 67 75 61 67 65 20 74 65 78 74 20 74 68 61 74  nguage text that
22e30 20 64 65 73 63 72 69 62 65 73 0a 2a 2a 20 20 20   describes.**   
22e40 20 20 20 20 20 20 20 74 68 65 20 65 72 72 6f 72         the error
22e50 20 69 6e 20 74 68 65 20 6d 6f 73 74 6c 79 20 72   in the mostly r
22e60 65 63 65 6e 74 6c 79 20 66 61 69 6c 65 64 20 69  ecently failed i
22e70 6e 74 65 72 66 61 63 65 20 63 61 6c 6c 2c 0a 2a  nterface call,.*
22e80 2a 20 20 20 20 20 20 20 20 20 20 65 6e 63 6f 64  *          encod
22e90 65 64 20 61 73 20 65 69 74 68 65 72 20 55 54 46  ed as either UTF
22ea0 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 72 65 73  -8 or UTF-16 res
22eb0 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a  pectively..**.**
22ec0 20 7b 48 31 32 38 30 37 7d 20 54 68 65 20 73 74   {H12807} The st
22ed0 72 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 62  rings returned b
22ee0 79 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  y [sqlite3_errms
22ef0 67 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  g()] and [sqlite
22f00 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 0a 2a 2a  3_errmsg16()].**
22f10 20 20 20 20 20 20 20 20 20 20 61 72 65 20 76 61            are va
22f20 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 6e 65  lid until the ne
22f30 78 74 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66  xt SQLite interf
22f40 61 63 65 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20  ace call..**.** 
22f50 7b 48 31 32 38 30 38 7d 20 43 61 6c 6c 73 20 74  {H12808} Calls t
22f60 6f 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20 74  o API routines t
22f70 68 61 74 20 64 6f 20 6e 6f 74 20 72 65 74 75 72  hat do not retur
22f80 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 0a  n an error code.
22f90 2a 2a 20 20 20 20 20 20 20 20 20 20 28 65 78 61  **          (exa
22fa0 6d 70 6c 65 3a 20 5b 73 71 6c 69 74 65 33 5f 64  mple: [sqlite3_d
22fb0 61 74 61 5f 63 6f 75 6e 74 28 29 5d 29 20 64 6f  ata_count()]) do
22fc0 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 20   not.**         
22fd0 20 63 68 61 6e 67 65 20 74 68 65 20 65 72 72 6f   change the erro
22fe0 72 20 63 6f 64 65 20 6f 72 20 6d 65 73 73 61 67  r code or messag
22ff0 65 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a  e returned by.**
23000 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
23010 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 2c 20 5b  e3_errcode()], [
23020 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64  sqlite3_extended
23030 5f 65 72 72 63 6f 64 65 28 29 5d 2c 0a 2a 2a 20  _errcode()],.** 
23040 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
23050 33 5f 65 72 72 6d 73 67 28 29 5d 2c 20 6f 72 20  3_errmsg()], or 
23060 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31  [sqlite3_errmsg1
23070 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  6()]..**.** {H12
23080 38 30 39 7d 20 49 6e 74 65 72 66 61 63 65 73 20  809} Interfaces 
23090 74 68 61 74 20 61 72 65 20 6e 6f 74 20 61 73 73  that are not ass
230a0 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 73  ociated with a s
230b0 70 65 63 69 66 69 63 0a 2a 2a 20 20 20 20 20 20  pecific.**      
230c0 20 20 20 20 5b 64 61 74 61 62 61 73 65 20 63 6f      [database co
230d0 6e 6e 65 63 74 69 6f 6e 5d 20 28 65 78 61 6d 70  nnection] (examp
230e0 6c 65 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20  les:.**         
230f0 20 5b 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74   [sqlite3_mprint
23100 66 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  f()] or [sqlite3
23110 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63  _enable_shared_c
23120 61 63 68 65 28 29 5d 0a 2a 2a 20 20 20 20 20 20  ache()].**      
23130 20 20 20 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67      do not chang
23140 65 20 74 68 65 20 76 61 6c 75 65 73 20 72 65 74  e the values ret
23150 75 72 6e 65 64 20 62 79 0a 2a 2a 20 20 20 20 20  urned by.**     
23160 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 72       [sqlite3_er
23170 72 63 6f 64 65 28 29 5d 2c 20 5b 73 71 6c 69 74  rcode()], [sqlit
23180 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 63  e3_extended_errc
23190 6f 64 65 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20  ode()],.**      
231a0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 72 72      [sqlite3_err
231b0 6d 73 67 28 29 5d 2c 20 6f 72 20 5b 73 71 6c 69  msg()], or [sqli
231c0 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 2e  te3_errmsg16()].
231d0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
231e0 6e 74 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  nt sqlite3_errco
231f0 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b  de(sqlite3 *db);
23200 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
23210 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64  sqlite3_extended
23220 5f 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65 33  _errcode(sqlite3
23230 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f 41 50   *db);.SQLITE_AP
23240 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  I const char *sq
23250 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 73 71 6c  lite3_errmsg(sql
23260 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 41  ite3*);.SQLITE_A
23270 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
23280 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28  qlite3_errmsg16(
23290 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a  sqlite3*);../*.*
232a0 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c 20  * CAPI3REF: SQL 
232b0 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a 65 63 74  Statement Object
232c0 20 7b 48 31 33 30 30 30 7d 20 3c 48 31 33 30 31   {H13000} <H1301
232d0 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  0>.** KEYWORDS: 
232e0 7b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  {prepared statem
232f0 65 6e 74 7d 20 7b 70 72 65 70 61 72 65 64 20 73  ent} {prepared s
23300 74 61 74 65 6d 65 6e 74 73 7d 0a 2a 2a 0a 2a 2a  tatements}.**.**
23310 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
23320 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70 72  this object repr
23330 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c 65 20  esents a single 
23340 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  SQL statement..*
23350 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 20 69 73  * This object is
23360 20 76 61 72 69 6f 75 73 6c 79 20 6b 6e 6f 77 6e   variously known
23370 20 61 73 20 61 20 22 70 72 65 70 61 72 65 64 20   as a "prepared 
23380 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20 61 0a  statement" or a.
23390 2a 2a 20 22 63 6f 6d 70 69 6c 65 64 20 53 51 4c  ** "compiled SQL
233a0 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20 73   statement" or s
233b0 69 6d 70 6c 79 20 61 73 20 61 20 22 73 74 61 74  imply as a "stat
233c0 65 6d 65 6e 74 22 2e 0a 2a 2a 0a 2a 2a 20 54 68  ement"..**.** Th
233d0 65 20 6c 69 66 65 20 6f 66 20 61 20 73 74 61 74  e life of a stat
233e0 65 6d 65 6e 74 20 6f 62 6a 65 63 74 20 67 6f 65  ement object goe
233f0 73 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69 6b 65  s something like
23400 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f 6c   this:.**.** <ol
23410 3e 0a 2a 2a 20 3c 6c 69 3e 20 43 72 65 61 74 65  >.** <li> Create
23420 20 74 68 65 20 6f 62 6a 65 63 74 20 75 73 69 6e   the object usin
23430 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  g [sqlite3_prepa
23440 72 65 5f 76 32 28 29 5d 20 6f 72 20 61 20 72 65  re_v2()] or a re
23450 6c 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 66 75  lated.**      fu
23460 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 3c 6c 69 3e 20  nction..** <li> 
23470 42 69 6e 64 20 76 61 6c 75 65 73 20 74 6f 20 5b  Bind values to [
23480 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73 5d  host parameters]
23490 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74   using the sqlit
234a0 65 33 5f 62 69 6e 64 5f 2a 28 29 0a 2a 2a 20 20  e3_bind_*().**  
234b0 20 20 20 20 69 6e 74 65 72 66 61 63 65 73 2e 0a      interfaces..
234c0 2a 2a 20 3c 6c 69 3e 20 52 75 6e 20 74 68 65 20  ** <li> Run the 
234d0 53 51 4c 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b  SQL by calling [
234e0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
234f0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69 6d 65  one or more time
23500 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 65 73 65 74  s..** <li> Reset
23510 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 75   the statement u
23520 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 72 65  sing [sqlite3_re
23530 73 65 74 28 29 5d 20 74 68 65 6e 20 67 6f 20 62  set()] then go b
23540 61 63 6b 0a 2a 2a 20 20 20 20 20 20 74 6f 20 73  ack.**      to s
23550 74 65 70 20 32 2e 20 20 44 6f 20 74 68 69 73 20  tep 2.  Do this 
23560 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 74 69 6d  zero or more tim
23570 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 44 65 73 74  es..** <li> Dest
23580 72 6f 79 20 74 68 65 20 6f 62 6a 65 63 74 20 75  roy the object u
23590 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69  sing [sqlite3_fi
235a0 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 20 3c 2f  nalize()]..** </
235b0 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72 20  ol>.**.** Refer 
235c0 74 6f 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e  to documentation
235d0 20 6f 6e 20 69 6e 64 69 76 69 64 75 61 6c 20 6d   on individual m
235e0 65 74 68 6f 64 73 20 61 62 6f 76 65 20 66 6f 72  ethods above for
235f0 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69   additional.** i
23600 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74  nformation..*/.t
23610 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
23620 6c 69 74 65 33 5f 73 74 6d 74 20 73 71 6c 69 74  lite3_stmt sqlit
23630 65 33 5f 73 74 6d 74 3b 0a 0a 2f 2a 0a 2a 2a 20  e3_stmt;../*.** 
23640 43 41 50 49 33 52 45 46 3a 20 52 75 6e 2d 74 69  CAPI3REF: Run-ti
23650 6d 65 20 4c 69 6d 69 74 73 20 7b 48 31 32 37 36  me Limits {H1276
23660 30 7d 20 3c 53 32 30 36 30 30 3e 0a 2a 2a 0a 2a  0} <S20600>.**.*
23670 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65  * This interface
23680 20 61 6c 6c 6f 77 73 20 74 68 65 20 73 69 7a 65   allows the size
23690 20 6f 66 20 76 61 72 69 6f 75 73 20 63 6f 6e 73   of various cons
236a0 74 72 75 63 74 73 20 74 6f 20 62 65 20 6c 69 6d  tructs to be lim
236b0 69 74 65 64 0a 2a 2a 20 6f 6e 20 61 20 63 6f 6e  ited.** on a con
236c0 6e 65 63 74 69 6f 6e 20 62 79 20 63 6f 6e 6e 65  nection by conne
236d0 63 74 69 6f 6e 20 62 61 73 69 73 2e 20 20 54 68  ction basis.  Th
236e0 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
236f0 72 20 69 73 20 74 68 65 0a 2a 2a 20 5b 64 61 74  r is the.** [dat
23700 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
23710 5d 20 77 68 6f 73 65 20 6c 69 6d 69 74 20 69 73  ] whose limit is
23720 20 74 6f 20 62 65 20 73 65 74 20 6f 72 20 71 75   to be set or qu
23730 65 72 69 65 64 2e 20 20 54 68 65 0a 2a 2a 20 73  eried.  The.** s
23740 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
23750 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 5b 6c  is one of the [l
23760 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65 73 5d  imit categories]
23770 20 74 68 61 74 20 64 65 66 69 6e 65 20 61 0a 2a   that define a.*
23780 2a 20 63 6c 61 73 73 20 6f 66 20 63 6f 6e 73 74  * class of const
23790 72 75 63 74 73 20 74 6f 20 62 65 20 73 69 7a 65  ructs to be size
237a0 20 6c 69 6d 69 74 65 64 2e 20 20 54 68 65 20 74   limited.  The t
237b0 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 69  hird parameter i
237c0 73 20 74 68 65 0a 2a 2a 20 6e 65 77 20 6c 69 6d  s the.** new lim
237d0 69 74 20 66 6f 72 20 74 68 61 74 20 63 6f 6e 73  it for that cons
237e0 74 72 75 63 74 2e 20 20 54 68 65 20 66 75 6e 63  truct.  The func
237f0 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65  tion returns the
23800 20 6f 6c 64 20 6c 69 6d 69 74 2e 0a 2a 2a 0a 2a   old limit..**.*
23810 2a 20 49 66 20 74 68 65 20 6e 65 77 20 6c 69 6d  * If the new lim
23820 69 74 20 69 73 20 61 20 6e 65 67 61 74 69 76 65  it is a negative
23830 20 6e 75 6d 62 65 72 2c 20 74 68 65 20 6c 69 6d   number, the lim
23840 69 74 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e  it is unchanged.
23850 0a 2a 2a 20 46 6f 72 20 74 68 65 20 6c 69 6d 69  .** For the limi
23860 74 20 63 61 74 65 67 6f 72 79 20 6f 66 20 53 51  t category of SQ
23870 4c 49 54 45 5f 4c 49 4d 49 54 5f 58 59 5a 20 74  LITE_LIMIT_XYZ t
23880 68 65 72 65 20 69 73 20 61 20 68 61 72 64 20 75  here is a hard u
23890 70 70 65 72 0a 2a 2a 20 62 6f 75 6e 64 20 73 65  pper.** bound se
238a0 74 20 62 79 20 61 20 63 6f 6d 70 69 6c 65 2d 74  t by a compile-t
238b0 69 6d 65 20 43 20 70 72 65 70 72 6f 63 65 73 73  ime C preprocess
238c0 6f 72 20 6d 61 63 72 6f 20 6e 61 6d 65 64 20 53  or macro named S
238d0 51 4c 49 54 45 5f 4d 41 58 5f 58 59 5a 2e 0a 2a  QLITE_MAX_XYZ..*
238e0 2a 20 28 54 68 65 20 22 5f 4c 49 4d 49 54 5f 22  * (The "_LIMIT_"
238f0 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 69 73 20   in the name is 
23900 63 68 61 6e 67 65 64 20 74 6f 20 22 5f 4d 41 58  changed to "_MAX
23910 5f 22 2e 29 0a 2a 2a 20 41 74 74 65 6d 70 74 73  _".).** Attempts
23920 20 74 6f 20 69 6e 63 72 65 61 73 65 20 61 20 6c   to increase a l
23930 69 6d 69 74 20 61 62 6f 76 65 20 69 74 73 20 68  imit above its h
23940 61 72 64 20 75 70 70 65 72 20 62 6f 75 6e 64 20  ard upper bound 
23950 61 72 65 0a 2a 2a 20 73 69 6c 65 6e 74 6c 79 20  are.** silently 
23960 74 72 75 6e 63 61 74 65 64 20 74 6f 20 74 68 65  truncated to the
23970 20 68 61 72 64 20 75 70 70 65 72 20 6c 69 6d 69   hard upper limi
23980 74 2e 0a 2a 2a 0a 2a 2a 20 52 75 6e 20 74 69 6d  t..**.** Run tim
23990 65 20 6c 69 6d 69 74 73 20 61 72 65 20 69 6e 74  e limits are int
239a0 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e  ended for use in
239b0 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68   applications th
239c0 61 74 20 6d 61 6e 61 67 65 0a 2a 2a 20 62 6f 74  at manage.** bot
239d0 68 20 74 68 65 69 72 20 6f 77 6e 20 69 6e 74 65  h their own inte
239e0 72 6e 61 6c 20 64 61 74 61 62 61 73 65 20 61 6e  rnal database an
239f0 64 20 61 6c 73 6f 20 64 61 74 61 62 61 73 65 73  d also databases
23a00 20 74 68 61 74 20 61 72 65 20 63 6f 6e 74 72 6f   that are contro
23a10 6c 6c 65 64 0a 2a 2a 20 62 79 20 75 6e 74 72 75  lled.** by untru
23a20 73 74 65 64 20 65 78 74 65 72 6e 61 6c 20 73 6f  sted external so
23a30 75 72 63 65 73 2e 20 20 41 6e 20 65 78 61 6d 70  urces.  An examp
23a40 6c 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d  le application m
23a50 69 67 68 74 20 62 65 20 61 0a 2a 2a 20 77 65 62  ight be a.** web
23a60 62 72 6f 77 73 65 72 20 74 68 61 74 20 68 61 73  browser that has
23a70 20 69 74 73 20 6f 77 6e 20 64 61 74 61 62 61 73   its own databas
23a80 65 73 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 68  es for storing h
23a90 69 73 74 6f 72 79 20 61 6e 64 0a 2a 2a 20 73 65  istory and.** se
23aa0 70 61 72 61 74 65 20 64 61 74 61 62 61 73 65 73  parate databases
23ab0 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 4a   controlled by J
23ac0 61 76 61 53 63 72 69 70 74 20 61 70 70 6c 69 63  avaScript applic
23ad0 61 74 69 6f 6e 73 20 64 6f 77 6e 6c 6f 61 64 65  ations downloade
23ae0 64 0a 2a 2a 20 6f 66 66 20 74 68 65 20 49 6e 74  d.** off the Int
23af0 65 72 6e 65 74 2e 20 20 54 68 65 20 69 6e 74 65  ernet.  The inte
23b00 72 6e 61 6c 20 64 61 74 61 62 61 73 65 73 20 63  rnal databases c
23b10 61 6e 20 62 65 20 67 69 76 65 6e 20 74 68 65 0a  an be given the.
23b20 2a 2a 20 6c 61 72 67 65 2c 20 64 65 66 61 75 6c  ** large, defaul
23b30 74 20 6c 69 6d 69 74 73 2e 20 20 44 61 74 61 62  t limits.  Datab
23b40 61 73 65 73 20 6d 61 6e 61 67 65 64 20 62 79 20  ases managed by 
23b50 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 65 73  external sources
23b60 20 63 61 6e 0a 2a 2a 20 62 65 20 67 69 76 65 6e   can.** be given
23b70 20 6d 75 63 68 20 73 6d 61 6c 6c 65 72 20 6c 69   much smaller li
23b80 6d 69 74 73 20 64 65 73 69 67 6e 65 64 20 74 6f  mits designed to
23b90 20 70 72 65 76 65 6e 74 20 61 20 64 65 6e 69 61   prevent a denia
23ba0 6c 20 6f 66 20 73 65 72 76 69 63 65 0a 2a 2a 20  l of service.** 
23bb0 61 74 74 61 63 6b 2e 20 20 44 65 76 65 6c 6f 70  attack.  Develop
23bc0 65 72 73 20 6d 69 67 68 74 20 61 6c 73 6f 20 77  ers might also w
23bd0 61 6e 74 20 74 6f 20 75 73 65 20 74 68 65 20 5b  ant to use the [
23be0 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68  sqlite3_set_auth
23bf0 6f 72 69 7a 65 72 28 29 5d 0a 2a 2a 20 69 6e 74  orizer()].** int
23c00 65 72 66 61 63 65 20 74 6f 20 66 75 72 74 68 65  erface to furthe
23c10 72 20 63 6f 6e 74 72 6f 6c 20 75 6e 74 72 75 73  r control untrus
23c20 74 65 64 20 53 51 4c 2e 20 20 54 68 65 20 73 69  ted SQL.  The si
23c30 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ze of the databa
23c40 73 65 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79  se.** created by
23c50 20 61 6e 20 75 6e 74 72 75 73 74 65 64 20 73 63   an untrusted sc
23c60 72 69 70 74 20 63 61 6e 20 62 65 20 63 6f 6e 74  ript can be cont
23c70 61 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65 0a  ained using the.
23c80 2a 2a 20 5b 6d 61 78 5f 70 61 67 65 5f 63 6f 75  ** [max_page_cou
23c90 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 2e 0a 2a 2a  nt] [PRAGMA]..**
23ca0 0a 2a 2a 20 4e 65 77 20 72 75 6e 2d 74 69 6d 65  .** New run-time
23cb0 20 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65   limit categorie
23cc0 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69  s may be added i
23cd0 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65  n future release
23ce0 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  s..**.** INVARIA
23cf0 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 37  NTS:.**.** {H127
23d00 36 32 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c  62} A successful
23d10 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
23d20 33 5f 6c 69 6d 69 74 28 44 2c 43 2c 56 29 5d 20  3_limit(D,C,V)] 
23d30 77 68 65 72 65 20 56 20 69 73 0a 2a 2a 20 20 20  where V is.**   
23d40 20 20 20 20 20 20 20 70 6f 73 69 74 69 76 65 20         positive 
23d50 63 68 61 6e 67 65 73 20 74 68 65 20 6c 69 6d 69  changes the limi
23d60 74 20 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f 66  t on the size of
23d70 20 63 6f 6e 73 74 72 75 63 74 20 43 20 69 6e 20   construct C in 
23d80 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
23d90 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
23da0 74 69 6f 6e 5d 20 44 20 74 6f 20 74 68 65 20 6c  tion] D to the l
23db0 65 73 73 65 72 20 6f 66 20 56 20 61 6e 64 20 74  esser of V and t
23dc0 68 65 20 68 61 72 64 20 75 70 70 65 72 0a 2a 2a  he hard upper.**
23dd0 20 20 20 20 20 20 20 20 20 20 62 6f 75 6e 64 20            bound 
23de0 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 43  on the size of C
23df0 20 74 68 61 74 20 69 73 20 73 65 74 20 61 74 20   that is set at 
23e00 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2a  compile-time..**
23e10 0a 2a 2a 20 7b 48 31 32 37 36 36 7d 20 41 20 73  .** {H12766} A s
23e20 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74  uccessful call t
23e30 6f 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74  o [sqlite3_limit
23e40 28 44 2c 43 2c 56 29 5d 20 77 68 65 72 65 20 56  (D,C,V)] where V
23e50 20 69 73 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20   is negative.** 
23e60 20 20 20 20 20 20 20 20 20 6c 65 61 76 65 73 20           leaves 
23e70 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65  the state of the
23e80 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
23e90 63 74 69 6f 6e 5d 20 44 20 75 6e 63 68 61 6e 67  ction] D unchang
23ea0 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 37 36  ed..**.** {H1276
23eb0 39 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20  9} A successful 
23ec0 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
23ed0 5f 6c 69 6d 69 74 28 44 2c 43 2c 56 29 5d 20 72  _limit(D,C,V)] r
23ee0 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 20 20  eturns the.**   
23ef0 20 20 20 20 20 20 20 76 61 6c 75 65 20 6f 66 20         value of 
23f00 74 68 65 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65  the limit on the
23f10 20 73 69 7a 65 20 6f 66 20 63 6f 6e 73 74 72 75   size of constru
23f20 63 74 20 43 20 69 6e 20 74 68 65 0a 2a 2a 20 20  ct C in the.**  
23f30 20 20 20 20 20 20 20 20 5b 64 61 74 61 62 61 73          [databas
23f40 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 20  e connection] D 
23f50 61 73 20 69 74 20 77 61 73 20 70 72 69 6f 72 20  as it was prior 
23f60 74 6f 20 74 68 65 20 63 61 6c 6c 2e 0a 2a 2f 0a  to the call..*/.
23f70 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
23f80 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 73 71 6c  qlite3_limit(sql
23f90 69 74 65 33 2a 2c 20 69 6e 74 20 69 64 2c 20 69  ite3*, int id, i
23fa0 6e 74 20 6e 65 77 56 61 6c 29 3b 0a 0a 2f 2a 0a  nt newVal);../*.
23fb0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75 6e  ** CAPI3REF: Run
23fc0 2d 54 69 6d 65 20 4c 69 6d 69 74 20 43 61 74 65  -Time Limit Cate
23fd0 67 6f 72 69 65 73 20 7b 48 31 32 37 39 30 7d 20  gories {H12790} 
23fe0 3c 48 31 32 37 36 30 3e 0a 2a 2a 20 4b 45 59 57  <H12760>.** KEYW
23ff0 4f 52 44 53 3a 20 7b 6c 69 6d 69 74 20 63 61 74  ORDS: {limit cat
24000 65 67 6f 72 79 7d 20 7b 6c 69 6d 69 74 20 63 61  egory} {limit ca
24010 74 65 67 6f 72 69 65 73 7d 0a 2a 2a 0a 2a 2a 20  tegories}.**.** 
24020 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20  These constants 
24030 64 65 66 69 6e 65 20 76 61 72 69 6f 75 73 20 61  define various a
24040 73 70 65 63 74 73 20 6f 66 20 61 20 5b 64 61 74  spects of a [dat
24050 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
24060 5d 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65  ].** that can be
24070 20 6c 69 6d 69 74 65 64 20 69 6e 20 73 69 7a 65   limited in size
24080 20 62 79 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71   by calls to [sq
24090 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 5d 2e 0a  lite3_limit()]..
240a0 2a 2a 20 54 68 65 20 6d 65 61 6e 69 6e 67 73 20  ** The meanings 
240b0 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 6c  of the various l
240c0 69 6d 69 74 73 20 61 72 65 20 61 73 20 66 6f 6c  imits are as fol
240d0 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e  lows:.**.** <dl>
240e0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c  .** <dt>SQLITE_L
240f0 49 4d 49 54 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e  IMIT_LENGTH</dt>
24100 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69  .** <dd>The maxi
24110 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20  mum size of any 
24120 73 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 6f  string or BLOB o
24130 72 20 74 61 62 6c 65 20 72 6f 77 2e 3c 64 64 3e  r table row.<dd>
24140 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  .**.** <dt>SQLIT
24150 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47  E_LIMIT_SQL_LENG
24160 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  TH</dt>.** <dd>T
24170 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  he maximum lengt
24180 68 20 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74  h of an SQL stat
24190 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  ement.</dd>.**.*
241a0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d  * <dt>SQLITE_LIM
241b0 49 54 5f 43 4f 4c 55 4d 4e 3c 2f 64 74 3e 0a 2a  IT_COLUMN</dt>.*
241c0 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75  * <dd>The maximu
241d0 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  m number of colu
241e0 6d 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 20 64  mns in a table d
241f0 65 66 69 6e 69 74 69 6f 6e 20 6f 72 20 69 6e 20  efinition or in 
24200 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65  the.** result se
24210 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 6f 72  t of a SELECT or
24220 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   the maximum num
24230 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
24240 6e 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 6f 72  n an index.** or
24250 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42 59 20   in an ORDER BY 
24260 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75  or GROUP BY clau
24270 73 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  se.</dd>.**.** <
24280 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  dt>SQLITE_LIMIT_
24290 45 58 50 52 5f 44 45 50 54 48 3c 2f 64 74 3e 0a  EXPR_DEPTH</dt>.
242a0 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d  ** <dd>The maxim
242b0 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 65 20  um depth of the 
242c0 70 61 72 73 65 20 74 72 65 65 20 6f 6e 20 61 6e  parse tree on an
242d0 79 20 65 78 70 72 65 73 73 69 6f 6e 2e 3c 2f 64  y expression.</d
242e0 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c  d>.**.** <dt>SQL
242f0 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55  ITE_LIMIT_COMPOU
24300 4e 44 5f 53 45 4c 45 43 54 3c 2f 64 74 3e 0a 2a  ND_SELECT</dt>.*
24310 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75  * <dd>The maximu
24320 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d  m number of term
24330 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20  s in a compound 
24340 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
24350 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
24360 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 44  >SQLITE_LIMIT_VD
24370 42 45 5f 4f 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  BE_OP</dt>.** <d
24380 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75  d>The maximum nu
24390 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75 63 74  mber of instruct
243a0 69 6f 6e 73 20 69 6e 20 61 20 76 69 72 74 75 61  ions in a virtua
243b0 6c 20 6d 61 63 68 69 6e 65 20 70 72 6f 67 72 61  l machine progra
243c0 6d 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6d 70  m.** used to imp
243d0 6c 65 6d 65 6e 74 20 61 6e 20 53 51 4c 20 73 74  lement an SQL st
243e0 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a  atement.</dd>.**
243f0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c  .** <dt>SQLITE_L
24400 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52  IMIT_FUNCTION_AR
24410 47 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  G</dt>.** <dd>Th
24420 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
24430 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 6f 6e   of arguments on
24440 20 61 20 66 75 6e 63 74 69 6f 6e 2e 3c 2f 64 64   a function.</dd
24450 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
24460 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 45  TE_LIMIT_ATTACHE
24470 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  D</dt>.** <dd>Th
24480 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
24490 20 6f 66 20 61 74 74 61 63 68 65 64 20 64 61 74   of attached dat
244a0 61 62 61 73 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a  abases.</dd>.**.
244b0 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49  ** <dt>SQLITE_LI
244c0 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e  MIT_LIKE_PATTERN
244d0 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20  _LENGTH</dt>.** 
244e0 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20  <dd>The maximum 
244f0 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 70 61  length of the pa
24500 74 74 65 72 6e 20 61 72 67 75 6d 65 6e 74 20 74  ttern argument t
24510 6f 20 74 68 65 20 4c 49 4b 45 20 6f 72 0a 2a 2a  o the LIKE or.**
24520 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 73 2e   GLOB operators.
24530 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
24540 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52  SQLITE_LIMIT_VAR
24550 49 41 42 4c 45 5f 4e 55 4d 42 45 52 3c 2f 64 74  IABLE_NUMBER</dt
24560 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78  >.** <dd>The max
24570 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 76  imum number of v
24580 61 72 69 61 62 6c 65 73 20 69 6e 20 61 6e 20 53  ariables in an S
24590 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61  QL statement tha
245a0 74 20 63 61 6e 0a 2a 2a 20 62 65 20 62 6f 75 6e  t can.** be boun
245b0 64 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e  d.</dd>.** </dl>
245c0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
245d0 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 20  TE_LIMIT_LENGTH 
245e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
245f0 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c     0.#define SQL
24600 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45  ITE_LIMIT_SQL_LE
24610 4e 47 54 48 20 20 20 20 20 20 20 20 20 20 20 20  NGTH            
24620 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51      1.#define SQ
24630 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d  LITE_LIMIT_COLUM
24640 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  N               
24650 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53       2.#define S
24660 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52  QLITE_LIMIT_EXPR
24670 5f 44 45 50 54 48 20 20 20 20 20 20 20 20 20 20  _DEPTH          
24680 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20        3.#define 
24690 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d  SQLITE_LIMIT_COM
246a0 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 20 20 20  POUND_SELECT    
246b0 20 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65         4.#define
246c0 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 44   SQLITE_LIMIT_VD
246d0 42 45 5f 4f 50 20 20 20 20 20 20 20 20 20 20 20  BE_OP           
246e0 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e          5.#defin
246f0 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 46  e SQLITE_LIMIT_F
24700 55 4e 43 54 49 4f 4e 5f 41 52 47 20 20 20 20 20  UNCTION_ARG     
24710 20 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 69           6.#defi
24720 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  ne SQLITE_LIMIT_
24730 41 54 54 41 43 48 45 44 20 20 20 20 20 20 20 20  ATTACHED        
24740 20 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 66            7.#def
24750 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54  ine SQLITE_LIMIT
24760 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45  _LIKE_PATTERN_LE
24770 4e 47 54 48 20 20 20 20 20 20 20 38 0a 23 64 65  NGTH       8.#de
24780 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49  fine SQLITE_LIMI
24790 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45  T_VARIABLE_NUMBE
247a0 52 20 20 20 20 20 20 20 20 20 20 20 39 0a 0a 2f  R           9../
247b0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43  *.** CAPI3REF: C
247c0 6f 6d 70 69 6c 69 6e 67 20 41 6e 20 53 51 4c 20  ompiling An SQL 
247d0 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 30 31  Statement {H1301
247e0 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 20 4b  0} <S10000>.** K
247f0 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c 20 73 74  EYWORDS: {SQL st
24800 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 65 72  atement compiler
24810 7d 0a 2a 2a 0a 2a 2a 20 54 6f 20 65 78 65 63 75  }.**.** To execu
24820 74 65 20 61 6e 20 53 51 4c 20 71 75 65 72 79 2c  te an SQL query,
24830 20 69 74 20 6d 75 73 74 20 66 69 72 73 74 20 62   it must first b
24840 65 20 63 6f 6d 70 69 6c 65 64 20 69 6e 74 6f 20  e compiled into 
24850 61 20 62 79 74 65 2d 63 6f 64 65 0a 2a 2a 20 70  a byte-code.** p
24860 72 6f 67 72 61 6d 20 75 73 69 6e 67 20 6f 6e 65  rogram using one
24870 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e   of these routin
24880 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  es..**.** The fi
24890 72 73 74 20 61 72 67 75 6d 65 6e 74 2c 20 22 64  rst argument, "d
248a0 62 22 2c 20 69 73 20 61 20 5b 64 61 74 61 62 61  b", is a [databa
248b0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6f  se connection] o
248c0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 0a 2a  btained from a.*
248d0 2a 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20  * prior call to 
248e0 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d  [sqlite3_open()]
248f0 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  , [sqlite3_open_
24900 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65  v2()] or [sqlite
24910 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 0a  3_open16()]..**.
24920 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72  ** The second ar
24930 67 75 6d 65 6e 74 2c 20 22 7a 53 71 6c 22 2c 20  gument, "zSql", 
24940 69 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  is the statement
24950 20 74 6f 20 62 65 20 63 6f 6d 70 69 6c 65 64 2c   to be compiled,
24960 20 65 6e 63 6f 64 65 64 0a 2a 2a 20 61 73 20 65   encoded.** as e
24970 69 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20 55  ither UTF-8 or U
24980 54 46 2d 31 36 2e 20 20 54 68 65 20 73 71 6c 69  TF-16.  The sqli
24990 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 61 6e  te3_prepare() an
249a0 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  d sqlite3_prepar
249b0 65 5f 76 32 28 29 0a 2a 2a 20 69 6e 74 65 72 66  e_v2().** interf
249c0 61 63 65 73 20 75 73 65 20 55 54 46 2d 38 2c 20  aces use UTF-8, 
249d0 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70  and sqlite3_prep
249e0 61 72 65 31 36 28 29 20 61 6e 64 20 73 71 6c 69  are16() and sqli
249f0 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32  te3_prepare16_v2
24a00 28 29 0a 2a 2a 20 75 73 65 20 55 54 46 2d 31 36  ().** use UTF-16
24a10 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e  ..**.** If the n
24a20 42 79 74 65 20 61 72 67 75 6d 65 6e 74 20 69 73  Byte argument is
24a30 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c   less than zero,
24a40 20 74 68 65 6e 20 7a 53 71 6c 20 69 73 20 72 65   then zSql is re
24a50 61 64 20 75 70 20 74 6f 20 74 68 65 0a 2a 2a 20  ad up to the.** 
24a60 66 69 72 73 74 20 7a 65 72 6f 20 74 65 72 6d 69  first zero termi
24a70 6e 61 74 6f 72 2e 20 49 66 20 6e 42 79 74 65 20  nator. If nByte 
24a80 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c  is non-negative,
24a90 20 74 68 65 6e 20 69 74 20 69 73 20 74 68 65 20   then it is the 
24aa0 6d 61 78 69 6d 75 6d 0a 2a 2a 20 6e 75 6d 62 65  maximum.** numbe
24ab0 72 20 6f 66 20 20 62 79 74 65 73 20 72 65 61 64  r of  bytes read
24ac0 20 66 72 6f 6d 20 7a 53 71 6c 2e 20 20 57 68 65   from zSql.  Whe
24ad0 6e 20 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e  n nByte is non-n
24ae0 65 67 61 74 69 76 65 2c 20 74 68 65 0a 2a 2a 20  egative, the.** 
24af0 7a 53 71 6c 20 73 74 72 69 6e 67 20 65 6e 64 73  zSql string ends
24b00 20 61 74 20 65 69 74 68 65 72 20 74 68 65 20 66   at either the f
24b10 69 72 73 74 20 27 5c 30 30 30 27 20 6f 72 20 27  irst '\000' or '
24b20 5c 75 30 30 30 30 27 20 63 68 61 72 61 63 74 65  \u0000' characte
24b30 72 20 6f 72 0a 2a 2a 20 74 68 65 20 6e 42 79 74  r or.** the nByt
24b40 65 2d 74 68 20 62 79 74 65 2c 20 77 68 69 63 68  e-th byte, which
24b50 65 76 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74  ever comes first
24b60 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20  . If the caller 
24b70 6b 6e 6f 77 73 0a 2a 2a 20 74 68 61 74 20 74 68  knows.** that th
24b80 65 20 73 75 70 70 6c 69 65 64 20 73 74 72 69 6e  e supplied strin
24b90 67 20 69 73 20 6e 75 6c 2d 74 65 72 6d 69 6e 61  g is nul-termina
24ba0 74 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20  ted, then there 
24bb0 69 73 20 61 20 73 6d 61 6c 6c 0a 2a 2a 20 70 65  is a small.** pe
24bc0 72 66 6f 72 6d 61 6e 63 65 20 61 64 76 61 6e 74  rformance advant
24bd0 61 67 65 20 74 6f 20 62 65 20 67 61 69 6e 65 64  age to be gained
24be0 20 62 79 20 70 61 73 73 69 6e 67 20 61 6e 20 6e   by passing an n
24bf0 42 79 74 65 20 70 61 72 61 6d 65 74 65 72 20 74  Byte parameter t
24c00 68 61 74 0a 2a 2a 20 69 73 20 65 71 75 61 6c 20  hat.** is equal 
24c10 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  to the number of
24c20 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 69 6e   bytes in the in
24c30 70 75 74 20 73 74 72 69 6e 67 20 3c 69 3e 69 6e  put string <i>in
24c40 63 6c 75 64 69 6e 67 3c 2f 69 3e 0a 2a 2a 20 74  cluding</i>.** t
24c50 68 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f  he nul-terminato
24c60 72 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 2a  r bytes..**.** *
24c70 70 7a 54 61 69 6c 20 69 73 20 6d 61 64 65 20 74  pzTail is made t
24c80 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66  o point to the f
24c90 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74  irst byte past t
24ca0 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a  he end of the.**
24cb0 20 66 69 72 73 74 20 53 51 4c 20 73 74 61 74 65   first SQL state
24cc0 6d 65 6e 74 20 69 6e 20 7a 53 71 6c 2e 20 20 54  ment in zSql.  T
24cd0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f 6e  hese routines on
24ce0 6c 79 20 63 6f 6d 70 69 6c 65 20 74 68 65 20 66  ly compile the f
24cf0 69 72 73 74 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  irst.** statemen
24d00 74 20 69 6e 20 7a 53 71 6c 2c 20 73 6f 20 2a 70  t in zSql, so *p
24d10 7a 54 61 69 6c 20 69 73 20 6c 65 66 74 20 70 6f  zTail is left po
24d20 69 6e 74 69 6e 67 20 74 6f 20 77 68 61 74 20 72  inting to what r
24d30 65 6d 61 69 6e 73 0a 2a 2a 20 75 6e 63 6f 6d 70  emains.** uncomp
24d40 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 70 53  iled..**.** *ppS
24d50 74 6d 74 20 69 73 20 6c 65 66 74 20 70 6f 69 6e  tmt is left poin
24d60 74 69 6e 67 20 74 6f 20 61 20 63 6f 6d 70 69 6c  ting to a compil
24d70 65 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  ed [prepared sta
24d80 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 63 61 6e  tement] that can
24d90 20 62 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20   be.** executed 
24da0 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73  using [sqlite3_s
24db0 74 65 70 28 29 5d 2e 20 20 49 66 20 74 68 65 72  tep()].  If ther
24dc0 65 20 69 73 20 61 6e 20 65 72 72 6f 72 2c 20 2a  e is an error, *
24dd0 70 70 53 74 6d 74 20 69 73 20 73 65 74 0a 2a 2a  ppStmt is set.**
24de0 20 74 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 74 68   to NULL.  If th
24df0 65 20 69 6e 70 75 74 20 74 65 78 74 20 63 6f 6e  e input text con
24e00 74 61 69 6e 73 20 6e 6f 20 53 51 4c 20 28 69 66  tains no SQL (if
24e10 20 74 68 65 20 69 6e 70 75 74 20 69 73 20 61 6e   the input is an
24e20 20 65 6d 70 74 79 0a 2a 2a 20 73 74 72 69 6e 67   empty.** string
24e30 20 6f 72 20 61 20 63 6f 6d 6d 65 6e 74 29 20 74   or a comment) t
24e40 68 65 6e 20 2a 70 70 53 74 6d 74 20 69 73 20 73  hen *ppStmt is s
24e50 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 20 7b  et to NULL..** {
24e60 41 31 33 30 31 38 7d 20 54 68 65 20 63 61 6c 6c  A13018} The call
24e70 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20 69 73  ing procedure is
24e80 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72   responsible for
24e90 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 63 6f   deleting the co
24ea0 6d 70 69 6c 65 64 0a 2a 2a 20 53 51 4c 20 73 74  mpiled.** SQL st
24eb0 61 74 65 6d 65 6e 74 20 75 73 69 6e 67 20 5b 73  atement using [s
24ec0 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
24ed0 29 5d 20 61 66 74 65 72 20 69 74 20 68 61 73 20  )] after it has 
24ee0 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 69 74  finished with it
24ef0 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65  ..**.** On succe
24f00 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20  ss, [SQLITE_OK] 
24f10 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 74 68  is returned, oth
24f20 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 6f 72  erwise an [error
24f30 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e   code] is return
24f40 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  ed..**.** The sq
24f50 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
24f60 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70  () and sqlite3_p
24f70 72 65 70 61 72 65 31 36 5f 76 32 28 29 20 69 6e  repare16_v2() in
24f80 74 65 72 66 61 63 65 73 20 61 72 65 0a 2a 2a 20  terfaces are.** 
24f90 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20  recommended for 
24fa0 61 6c 6c 20 6e 65 77 20 70 72 6f 67 72 61 6d 73  all new programs
24fb0 2e 20 54 68 65 20 74 77 6f 20 6f 6c 64 65 72 20  . The two older 
24fc0 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 72  interfaces are r
24fd0 65 74 61 69 6e 65 64 0a 2a 2a 20 66 6f 72 20 62  etained.** for b
24fe0 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69  ackwards compati
24ff0 62 69 6c 69 74 79 2c 20 62 75 74 20 74 68 65 69  bility, but thei
25000 72 20 75 73 65 20 69 73 20 64 69 73 63 6f 75 72  r use is discour
25010 61 67 65 64 2e 0a 2a 2a 20 49 6e 20 74 68 65 20  aged..** In the 
25020 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 73 2c  "v2" interfaces,
25030 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74   the prepared st
25040 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 68 61 74 20  atement.** that 
25050 69 73 20 72 65 74 75 72 6e 65 64 20 28 74 68 65  is returned (the
25060 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5d 20   [sqlite3_stmt] 
25070 6f 62 6a 65 63 74 29 20 63 6f 6e 74 61 69 6e 73  object) contains
25080 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a   a copy of the.*
25090 2a 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74  * original SQL t
250a0 65 78 74 2e 20 54 68 69 73 20 63 61 75 73 65 73  ext. This causes
250b0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74   the [sqlite3_st
250c0 65 70 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20  ep()] interface 
250d0 74 6f 0a 2a 2a 20 62 65 68 61 76 65 20 61 20 64  to.** behave a d
250e0 69 66 66 65 72 65 6e 74 6c 79 20 69 6e 20 74 77  ifferently in tw
250f0 6f 20 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f  o ways:.**.** <o
25100 6c 3e 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 49 66  l>.** <li>.** If
25110 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
25120 68 65 6d 61 20 63 68 61 6e 67 65 73 2c 20 69 6e  hema changes, in
25130 73 74 65 61 64 20 6f 66 20 72 65 74 75 72 6e 69  stead of returni
25140 6e 67 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d  ng [SQLITE_SCHEM
25150 41 5d 20 61 73 20 69 74 0a 2a 2a 20 61 6c 77 61  A] as it.** alwa
25160 79 73 20 75 73 65 64 20 74 6f 20 64 6f 2c 20 5b  ys used to do, [
25170 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
25180 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c  will automatical
25190 6c 79 20 72 65 63 6f 6d 70 69 6c 65 20 74 68 65  ly recompile the
251a0 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e   SQL.** statemen
251b0 74 20 61 6e 64 20 74 72 79 20 74 6f 20 72 75 6e  t and try to run
251c0 20 69 74 20 61 67 61 69 6e 2e 20 20 49 66 20 74   it again.  If t
251d0 68 65 20 73 63 68 65 6d 61 20 68 61 73 20 63 68  he schema has ch
251e0 61 6e 67 65 64 20 69 6e 0a 2a 2a 20 61 20 77 61  anged in.** a wa
251f0 79 20 74 68 61 74 20 6d 61 6b 65 73 20 74 68 65  y that makes the
25200 20 73 74 61 74 65 6d 65 6e 74 20 6e 6f 20 6c 6f   statement no lo
25210 6e 67 65 72 20 76 61 6c 69 64 2c 20 5b 73 71 6c  nger valid, [sql
25220 69 74 65 33 5f 73 74 65 70 28 29 5d 20 77 69 6c  ite3_step()] wil
25230 6c 20 73 74 69 6c 6c 0a 2a 2a 20 72 65 74 75 72  l still.** retur
25240 6e 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  n [SQLITE_SCHEMA
25250 5d 2e 20 20 42 75 74 20 75 6e 6c 69 6b 65 20 74  ].  But unlike t
25260 68 65 20 6c 65 67 61 63 79 20 62 65 68 61 76 69  he legacy behavi
25270 6f 72 2c 20 5b 53 51 4c 49 54 45 5f 53 43 48 45  or, [SQLITE_SCHE
25280 4d 41 5d 20 69 73 0a 2a 2a 20 6e 6f 77 20 61 20  MA] is.** now a 
25290 66 61 74 61 6c 20 65 72 72 6f 72 2e 20 20 43 61  fatal error.  Ca
252a0 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70  lling [sqlite3_p
252b0 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 67 61  repare_v2()] aga
252c0 69 6e 20 77 69 6c 6c 20 6e 6f 74 20 6d 61 6b 65  in will not make
252d0 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 67 6f   the.** error go
252e0 20 61 77 61 79 2e 20 20 4e 6f 74 65 3a 20 75 73   away.  Note: us
252f0 65 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  e [sqlite3_errms
25300 67 28 29 5d 20 74 6f 20 66 69 6e 64 20 74 68 65  g()] to find the
25310 20 74 65 78 74 0a 2a 2a 20 6f 66 20 74 68 65 20   text.** of the 
25320 70 61 72 73 69 6e 67 20 65 72 72 6f 72 20 74 68  parsing error th
25330 61 74 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e  at results in an
25340 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d   [SQLITE_SCHEMA]
25350 20 72 65 74 75 72 6e 2e 0a 2a 2a 20 3c 2f 6c 69   return..** </li
25360 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20  >.**.** <li>.** 
25370 57 68 65 6e 20 61 6e 20 65 72 72 6f 72 20 6f 63  When an error oc
25380 63 75 72 73 2c 20 5b 73 71 6c 69 74 65 33 5f 73  curs, [sqlite3_s
25390 74 65 70 28 29 5d 20 77 69 6c 6c 20 72 65 74 75  tep()] will retu
253a0 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 65  rn one of the de
253b0 74 61 69 6c 65 64 0a 2a 2a 20 5b 65 72 72 6f 72  tailed.** [error
253c0 20 63 6f 64 65 73 5d 20 6f 72 20 5b 65 78 74 65   codes] or [exte
253d0 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 73  nded error codes
253e0 5d 2e 20 20 54 68 65 20 6c 65 67 61 63 79 20 62  ].  The legacy b
253f0 65 68 61 76 69 6f 72 20 77 61 73 20 74 68 61 74  ehavior was that
25400 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  .** [sqlite3_ste
25410 70 28 29 5d 20 77 6f 75 6c 64 20 6f 6e 6c 79 20  p()] would only 
25420 72 65 74 75 72 6e 20 61 20 67 65 6e 65 72 69 63  return a generic
25430 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20   [SQLITE_ERROR] 
25440 72 65 73 75 6c 74 20 63 6f 64 65 0a 2a 2a 20 61  result code.** a
25450 6e 64 20 79 6f 75 20 77 6f 75 6c 64 20 68 61 76  nd you would hav
25460 65 20 74 6f 20 6d 61 6b 65 20 61 20 73 65 63 6f  e to make a seco
25470 6e 64 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69  nd call to [sqli
25480 74 65 33 5f 72 65 73 65 74 28 29 5d 20 69 6e 20  te3_reset()] in 
25490 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 66 69 6e 64  order.** to find
254a0 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   the underlying 
254b0 63 61 75 73 65 20 6f 66 20 74 68 65 20 70 72 6f  cause of the pro
254c0 62 6c 65 6d 2e 20 57 69 74 68 20 74 68 65 20 22  blem. With the "
254d0 76 32 22 20 70 72 65 70 61 72 65 0a 2a 2a 20 69  v2" prepare.** i
254e0 6e 74 65 72 66 61 63 65 73 2c 20 74 68 65 20 75  nterfaces, the u
254f0 6e 64 65 72 6c 79 69 6e 67 20 72 65 61 73 6f 6e  nderlying reason
25500 20 66 6f 72 20 74 68 65 20 65 72 72 6f 72 20 69   for the error i
25510 73 20 72 65 74 75 72 6e 65 64 20 69 6d 6d 65 64  s returned immed
25520 69 61 74 65 6c 79 2e 0a 2a 2a 20 3c 2f 6c 69 3e  iately..** </li>
25530 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20  .** </ol>.**.** 
25540 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a  INVARIANTS:.**.*
25550 2a 20 7b 48 31 33 30 31 31 7d 20 54 68 65 20 5b  * {H13011} The [
25560 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
25570 64 62 2c 7a 53 71 6c 2c 2e 2e 2e 29 5d 20 61 6e  db,zSql,...)] an
25580 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73  d.**          [s
25590 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
255a0 32 28 64 62 2c 7a 53 71 6c 2c 2e 2e 2e 29 5d 20  2(db,zSql,...)] 
255b0 69 6e 74 65 72 66 61 63 65 73 20 69 6e 74 65 72  interfaces inter
255c0 70 72 65 74 20 74 68 65 0a 2a 2a 20 20 20 20 20  pret the.**     
255d0 20 20 20 20 20 74 65 78 74 20 69 6e 20 74 68 65       text in the
255e0 69 72 20 7a 53 71 6c 20 70 61 72 61 6d 65 74 65  ir zSql paramete
255f0 72 20 61 73 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a  r as UTF-8..**.*
25600 2a 20 7b 48 31 33 30 31 32 7d 20 54 68 65 20 5b  * {H13012} The [
25610 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
25620 36 28 64 62 2c 7a 53 71 6c 2c 2e 2e 2e 29 5d 20  6(db,zSql,...)] 
25630 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  and.**          
25640 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
25650 31 36 5f 76 32 28 64 62 2c 7a 53 71 6c 2c 2e 2e  16_v2(db,zSql,..
25660 2e 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 69  .)] interfaces i
25670 6e 74 65 72 70 72 65 74 20 74 68 65 0a 2a 2a 20  nterpret the.** 
25680 20 20 20 20 20 20 20 20 20 74 65 78 74 20 69 6e           text in
25690 20 74 68 65 69 72 20 7a 53 71 6c 20 70 61 72 61   their zSql para
256a0 6d 65 74 65 72 20 61 73 20 55 54 46 2d 31 36 20  meter as UTF-16 
256b0 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79  in the native by
256c0 74 65 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a 2a 20  te order..**.** 
256d0 7b 48 31 33 30 31 33 7d 20 49 66 20 74 68 65 20  {H13013} If the 
256e0 6e 42 79 74 65 20 61 72 67 75 6d 65 6e 74 20 74  nByte argument t
256f0 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  o [sqlite3_prepa
25700 72 65 5f 76 32 28 64 62 2c 7a 53 71 6c 2c 6e 42  re_v2(db,zSql,nB
25710 79 74 65 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 20 20  yte,...)].**    
25720 20 20 20 20 20 20 61 6e 64 20 69 74 73 20 76 61        and its va
25730 72 69 61 6e 74 73 20 69 73 20 6c 65 73 73 20 74  riants is less t
25740 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 20 53 51  han zero, the SQ
25750 4c 20 74 65 78 74 20 69 73 0a 2a 2a 20 20 20 20  L text is.**    
25760 20 20 20 20 20 20 72 65 61 64 20 66 72 6f 6d 20        read from 
25770 7a 53 71 6c 20 69 73 20 72 65 61 64 20 75 70 20  zSql is read up 
25780 74 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65 72  to the first zer
25790 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a  o terminator..**
257a0 0a 2a 2a 20 7b 48 31 33 30 31 34 7d 20 49 66 20  .** {H13014} If 
257b0 74 68 65 20 6e 42 79 74 65 20 61 72 67 75 6d 65  the nByte argume
257c0 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 70  nt to [sqlite3_p
257d0 72 65 70 61 72 65 5f 76 32 28 64 62 2c 7a 53 71  repare_v2(db,zSq
257e0 6c 2c 6e 42 79 74 65 2c 2e 2e 2e 29 5d 0a 2a 2a  l,nByte,...)].**
257f0 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 69 74            and it
25800 73 20 76 61 72 69 61 6e 74 73 20 69 73 20 6e 6f  s variants is no
25810 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e  n-negative, then
25820 20 61 74 20 6d 6f 73 74 20 6e 42 79 74 65 73 20   at most nBytes 
25830 62 79 74 65 73 20 6f 66 0a 2a 2a 20 20 20 20 20  bytes of.**     
25840 20 20 20 20 20 53 51 4c 20 74 65 78 74 20 69 73       SQL text is
25850 20 72 65 61 64 20 66 72 6f 6d 20 7a 53 71 6c 2e   read from zSql.
25860 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 30 31 35 7d 20  .**.** {H13015} 
25870 49 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  In [sqlite3_prep
25880 61 72 65 5f 76 32 28 64 62 2c 7a 53 71 6c 2c 4e  are_v2(db,zSql,N
25890 2c 50 2c 70 7a 54 61 69 6c 29 5d 20 61 6e 64 20  ,P,pzTail)] and 
258a0 69 74 73 20 76 61 72 69 61 6e 74 73 0a 2a 2a 20  its variants.** 
258b0 20 20 20 20 20 20 20 20 20 69 66 20 74 68 65 20           if the 
258c0 7a 53 71 6c 20 69 6e 70 75 74 20 74 65 78 74 20  zSql input text 
258d0 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74 68  contains more th
258e0 61 6e 20 6f 6e 65 20 53 51 4c 20 73 74 61 74 65  an one SQL state
258f0 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  ment.**         
25900 20 61 6e 64 20 70 7a 54 61 69 6c 20 69 73 20 6e   and pzTail is n
25910 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 2a 70  ot NULL, then *p
25920 7a 54 61 69 6c 20 69 73 20 6d 61 64 65 20 74 6f  zTail is made to
25930 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a   point to the.**
25940 20 20 20 20 20 20 20 20 20 20 66 69 72 73 74 20            first 
25950 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65 6e  byte past the en
25960 64 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 53  d of the first S
25970 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20  QL statement in 
25980 7a 53 71 6c 2e 0a 2a 2a 20 20 20 20 20 20 20 20  zSql..**        
25990 20 20 3c 74 6f 64 6f 3e 57 68 61 74 20 64 6f 65    <todo>What doe
259a0 73 20 2a 70 7a 54 61 69 6c 20 70 6f 69 6e 74 20  s *pzTail point 
259b0 74 6f 20 69 66 20 74 68 65 72 65 20 69 73 20 6f  to if there is o
259c0 6e 65 20 73 74 61 74 65 6d 65 6e 74 3f 3c 2f 74  ne statement?</t
259d0 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 30  odo>.**.** {H130
259e0 31 36 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c  16} A successful
259f0 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
25a00 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c  3_prepare_v2(db,
25a10 7a 53 71 6c 2c 4e 2c 70 70 53 74 6d 74 2c 2e 2e  zSql,N,ppStmt,..
25a20 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .)].**          
25a30 6f 72 20 6f 6e 65 20 6f 66 20 69 74 73 20 76 61  or one of its va
25a40 72 69 61 6e 74 73 20 77 72 69 74 65 73 20 69 6e  riants writes in
25a50 74 6f 20 2a 70 70 53 74 6d 74 20 61 20 70 6f 69  to *ppStmt a poi
25a60 6e 74 65 72 20 74 6f 20 61 20 6e 65 77 0a 2a 2a  nter to a new.**
25a70 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61            [prepa
25a80 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 6f  red statement] o
25a90 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 4e  r a pointer to N
25aa0 55 4c 4c 20 69 66 20 7a 53 71 6c 20 63 6f 6e 74  ULL if zSql cont
25ab0 61 69 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  ains.**         
25ac0 20 6e 6f 74 68 69 6e 67 20 6f 74 68 65 72 20 74   nothing other t
25ad0 68 61 6e 20 77 68 69 74 65 73 70 61 63 65 20 6f  han whitespace o
25ae0 72 20 63 6f 6d 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a  r comments..**.*
25af0 2a 20 7b 48 31 33 30 31 39 7d 20 54 68 65 20 5b  * {H13019} The [
25b00 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
25b10 76 32 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20  v2()] interface 
25b20 61 6e 64 20 69 74 73 20 76 61 72 69 61 6e 74 73  and its variants
25b30 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20   return.**      
25b40 20 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20      [SQLITE_OK] 
25b50 6f 72 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74  or an appropriat
25b60 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 75  e [error code] u
25b70 70 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a  pon failure..**.
25b80 2a 2a 20 7b 48 31 33 30 32 31 7d 20 42 65 66 6f  ** {H13021} Befo
25b90 72 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  re [sqlite3_prep
25ba0 61 72 65 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74  are(db,zSql,nByt
25bb0 65 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29  e,ppStmt,pzTail)
25bc0 5d 20 6f 72 20 69 74 73 0a 2a 2a 20 20 20 20 20  ] or its.**     
25bd0 20 20 20 20 20 76 61 72 69 61 6e 74 73 20 72 65       variants re
25be0 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 28  turns an error (
25bf0 61 6e 79 20 76 61 6c 75 65 20 6f 74 68 65 72 20  any value other 
25c00 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d  than [SQLITE_OK]
25c10 29 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  ),.**          t
25c20 68 65 79 20 66 69 72 73 74 20 73 65 74 20 2a 70  hey first set *p
25c30 70 53 74 6d 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a  pStmt to NULL..*
25c40 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
25c50 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
25c60 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
25c70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
25c80 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a  atabase handle *
25c90 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
25ca0 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53  zSql,       /* S
25cb0 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54  QL statement, UT
25cc0 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20  F-8 encoded */. 
25cd0 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20   int nByte,     
25ce0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69           /* Maxi
25cf0 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53  mum length of zS
25d00 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a  ql in bytes. */.
25d10 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
25d20 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54  *ppStmt,  /* OUT
25d30 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64  : Statement hand
25d40 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  le */.  const ch
25d50 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20  ar **pzTail     
25d60 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20  /* OUT: Pointer 
25d70 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f  to unused portio
25d80 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a  n of zSql */.);.
25d90 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
25da0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
25db0 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  2(.  sqlite3 *db
25dc0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
25dd0 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  Database handle 
25de0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
25df0 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20  *zSql,       /* 
25e00 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55  SQL statement, U
25e10 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a  TF-8 encoded */.
25e20 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20    int nByte,    
25e30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
25e40 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a  imum length of z
25e50 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f  Sql in bytes. */
25e60 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
25e70 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55  **ppStmt,  /* OU
25e80 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e  T: Statement han
25e90 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  dle */.  const c
25ea0 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20  har **pzTail    
25eb0 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72   /* OUT: Pointer
25ec0 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69   to unused porti
25ed0 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b  on of zSql */.);
25ee0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
25ef0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
25f00 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  6(.  sqlite3 *db
25f10 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
25f20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  Database handle 
25f30 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
25f40 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20  *zSql,       /* 
25f50 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55  SQL statement, U
25f60 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 2a 2f  TF-16 encoded */
25f70 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20  .  int nByte,   
25f80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
25f90 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20  ximum length of 
25fa0 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a  zSql in bytes. *
25fb0 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
25fc0 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f   **ppStmt,  /* O
25fd0 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61  UT: Statement ha
25fe0 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ndle */.  const 
25ff0 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20  void **pzTail   
26000 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65    /* OUT: Pointe
26010 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74  r to unused port
26020 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29  ion of zSql */.)
26030 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
26040 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
26050 31 36 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33  16_v2(.  sqlite3
26060 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
26070 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e   /* Database han
26080 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  dle */.  const v
26090 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20  oid *zSql,      
260a0 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   /* SQL statemen
260b0 74 2c 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65  t, UTF-16 encode
260c0 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  d */.  int nByte
260d0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
260e0 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68  * Maximum length
260f0 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65   of zSql in byte
26100 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  s. */.  sqlite3_
26110 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20  stmt **ppStmt,  
26120 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e  /* OUT: Statemen
26130 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f  t handle */.  co
26140 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69  nst void **pzTai
26150 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f  l     /* OUT: Po
26160 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20  inter to unused 
26170 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20  portion of zSql 
26180 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  */.);../*.** CAP
26190 49 33 52 45 46 3a 20 52 65 74 72 69 65 76 69 6e  I3REF: Retrievin
261a0 67 20 53 74 61 74 65 6d 65 6e 74 20 53 51 4c 20  g Statement SQL 
261b0 7b 48 31 33 31 30 30 7d 20 3c 48 31 33 30 30 30  {H13100} <H13000
261c0 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74  >.**.** This int
261d0 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 75 73  erface can be us
261e0 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 61  ed to retrieve a
261f0 20 73 61 76 65 64 20 63 6f 70 79 20 6f 66 20 74   saved copy of t
26200 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 53  he original.** S
26210 51 4c 20 74 65 78 74 20 75 73 65 64 20 74 6f 20  QL text used to 
26220 63 72 65 61 74 65 20 61 20 5b 70 72 65 70 61 72  create a [prepar
26230 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69 66  ed statement] if
26240 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 20   that statement 
26250 77 61 73 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20  was.** compiled 
26260 75 73 69 6e 67 20 65 69 74 68 65 72 20 5b 73 71  using either [sq
26270 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
26280 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ()] or [sqlite3_
26290 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e  prepare16_v2()].
262a0 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54  .**.** INVARIANT
262b0 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 31 30 31  S:.**.** {H13101
262c0 7d 20 49 66 20 74 68 65 20 5b 70 72 65 70 61 72  } If the [prepar
262d0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 70 61  ed statement] pa
262e0 73 73 65 64 20 61 73 20 74 68 65 20 61 72 67 75  ssed as the argu
262f0 6d 65 6e 74 20 74 6f 0a 2a 2a 20 20 20 20 20 20  ment to.**      
26300 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 73 71 6c      [sqlite3_sql
26310 28 29 5d 20 77 61 73 20 63 6f 6d 70 69 6c 65 64  ()] was compiled
26320 20 75 73 69 6e 67 20 65 69 74 68 65 72 20 5b 73   using either [s
26330 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
26340 32 28 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20  2()] or.**      
26350 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 70 72 65      [sqlite3_pre
26360 70 61 72 65 31 36 5f 76 32 28 29 5d 2c 20 74 68  pare16_v2()], th
26370 65 6e 20 5b 73 71 6c 69 74 65 33 5f 73 71 6c 28  en [sqlite3_sql(
26380 29 5d 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20  )] returns.**   
26390 20 20 20 20 20 20 20 61 20 70 6f 69 6e 74 65 72         a pointer
263a0 20 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69   to a zero-termi
263b0 6e 61 74 65 64 20 73 74 72 69 6e 67 20 63 6f 6e  nated string con
263c0 74 61 69 6e 69 6e 67 20 61 20 55 54 46 2d 38 20  taining a UTF-8 
263d0 72 65 6e 64 65 72 69 6e 67 0a 2a 2a 20 20 20 20  rendering.**    
263e0 20 20 20 20 20 20 6f 66 20 74 68 65 20 6f 72 69        of the ori
263f0 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 74 65 6d  ginal SQL statem
26400 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 31  ent..**.** {H131
26410 30 32 7d 20 49 66 20 74 68 65 20 5b 70 72 65 70  02} If the [prep
26420 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
26430 70 61 73 73 65 64 20 61 73 20 74 68 65 20 61 72  passed as the ar
26440 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 20 20 20  gument to.**    
26450 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 73        [sqlite3_s
26460 71 6c 28 29 5d 20 77 61 73 20 63 6f 6d 70 69 6c  ql()] was compil
26470 65 64 20 75 73 69 6e 67 20 65 69 74 68 65 72 20  ed using either 
26480 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
26490 28 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20  ()] or.**       
264a0 20 20 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70     [sqlite3_prep
264b0 61 72 65 31 36 28 29 5d 2c 20 74 68 65 6e 20 5b  are16()], then [
264c0 73 71 6c 69 74 65 33 5f 73 71 6c 28 29 5d 20 72  sqlite3_sql()] r
264d0 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f  eturns a NULL po
264e0 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  inter..**.** {H1
264f0 33 31 30 33 7d 20 54 68 65 20 73 74 72 69 6e 67  3103} The string
26500 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71   returned by [sq
26510 6c 69 74 65 33 5f 73 71 6c 28 53 29 5d 20 69 73  lite3_sql(S)] is
26520 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65   valid until the
26530 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72  .**          [pr
26540 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
26550 5d 20 53 20 69 73 20 64 65 6c 65 74 65 64 20 75  ] S is deleted u
26560 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69  sing [sqlite3_fi
26570 6e 61 6c 69 7a 65 28 53 29 5d 2e 0a 2a 2f 0a 53  nalize(S)]..*/.S
26580 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
26590 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 71  char *sqlite3_sq
265a0 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  l(sqlite3_stmt *
265b0 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  pStmt);../*.** C
265c0 41 50 49 33 52 45 46 3a 20 44 79 6e 61 6d 69 63  API3REF: Dynamic
265d0 61 6c 6c 79 20 54 79 70 65 64 20 56 61 6c 75 65  ally Typed Value
265e0 20 4f 62 6a 65 63 74 20 7b 48 31 35 30 30 30 7d   Object {H15000}
265f0 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 20 4b 45 59   <S20200>.** KEY
26600 57 4f 52 44 53 3a 20 7b 70 72 6f 74 65 63 74 65  WORDS: {protecte
26610 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 7d  d sqlite3_value}
26620 20 7b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71   {unprotected sq
26630 6c 69 74 65 33 5f 76 61 6c 75 65 7d 0a 2a 2a 0a  lite3_value}.**.
26640 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 74  ** SQLite uses t
26650 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  he sqlite3_value
26660 20 6f 62 6a 65 63 74 20 74 6f 20 72 65 70 72 65   object to repre
26670 73 65 6e 74 20 61 6c 6c 20 76 61 6c 75 65 73 0a  sent all values.
26680 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 73  ** that can be s
26690 74 6f 72 65 64 20 69 6e 20 61 20 64 61 74 61 62  tored in a datab
266a0 61 73 65 20 74 61 62 6c 65 2e 20 53 51 4c 69 74  ase table. SQLit
266b0 65 20 75 73 65 73 20 64 79 6e 61 6d 69 63 20 74  e uses dynamic t
266c0 79 70 69 6e 67 0a 2a 2a 20 66 6f 72 20 74 68 65  yping.** for the
266d0 20 76 61 6c 75 65 73 20 69 74 20 73 74 6f 72 65   values it store
266e0 73 2e 20 56 61 6c 75 65 73 20 73 74 6f 72 65 64  s. Values stored
266f0 20 69 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   in sqlite3_valu
26700 65 20 6f 62 6a 65 63 74 73 0a 2a 2a 20 63 61 6e  e objects.** can
26710 20 62 65 20 69 6e 74 65 67 65 72 73 2c 20 66 6c   be integers, fl
26720 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c  oating point val
26730 75 65 73 2c 20 73 74 72 69 6e 67 73 2c 20 42 4c  ues, strings, BL
26740 4f 42 73 2c 20 6f 72 20 4e 55 4c 4c 2e 0a 2a 2a  OBs, or NULL..**
26750 0a 2a 2a 20 41 6e 20 73 71 6c 69 74 65 33 5f 76  .** An sqlite3_v
26760 61 6c 75 65 20 6f 62 6a 65 63 74 20 6d 61 79 20  alue object may 
26770 62 65 20 65 69 74 68 65 72 20 22 70 72 6f 74 65  be either "prote
26780 63 74 65 64 22 20 6f 72 20 22 75 6e 70 72 6f 74  cted" or "unprot
26790 65 63 74 65 64 22 2e 0a 2a 2a 20 53 6f 6d 65 20  ected"..** Some 
267a0 69 6e 74 65 72 66 61 63 65 73 20 72 65 71 75 69  interfaces requi
267b0 72 65 20 61 20 70 72 6f 74 65 63 74 65 64 20 73  re a protected s
267c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 20 20 4f  qlite3_value.  O
267d0 74 68 65 72 20 69 6e 74 65 72 66 61 63 65 73 0a  ther interfaces.
267e0 2a 2a 20 77 69 6c 6c 20 61 63 63 65 70 74 20 65  ** will accept e
267f0 69 74 68 65 72 20 61 20 70 72 6f 74 65 63 74 65  ither a protecte
26800 64 20 6f 72 20 61 6e 20 75 6e 70 72 6f 74 65 63  d or an unprotec
26810 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ted sqlite3_valu
26820 65 2e 0a 2a 2a 20 45 76 65 72 79 20 69 6e 74 65  e..** Every inte
26830 72 66 61 63 65 20 74 68 61 74 20 61 63 63 65 70  rface that accep
26840 74 73 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ts sqlite3_value
26850 20 61 72 67 75 6d 65 6e 74 73 20 73 70 65 63 69   arguments speci
26860 66 69 65 73 0a 2a 2a 20 77 68 65 74 68 65 72 20  fies.** whether 
26870 6f 72 20 6e 6f 74 20 69 74 20 72 65 71 75 69 72  or not it requir
26880 65 73 20 61 20 70 72 6f 74 65 63 74 65 64 20 73  es a protected s
26890 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 0a 2a 2a  qlite3_value..**
268a0 0a 2a 2a 20 54 68 65 20 74 65 72 6d 73 20 22 70  .** The terms "p
268b0 72 6f 74 65 63 74 65 64 22 20 61 6e 64 20 22 75  rotected" and "u
268c0 6e 70 72 6f 74 65 63 74 65 64 22 20 72 65 66 65  nprotected" refe
268d0 72 20 74 6f 20 77 68 65 74 68 65 72 20 6f 72 20  r to whether or 
268e0 6e 6f 74 0a 2a 2a 20 61 20 6d 75 74 65 78 20 69  not.** a mutex i
268f0 73 20 68 65 6c 64 2e 20 20 41 20 69 6e 74 65 72  s held.  A inter
26900 6e 61 6c 20 6d 75 74 65 78 20 69 73 20 68 65 6c  nal mutex is hel
26910 64 20 66 6f 72 20 61 20 70 72 6f 74 65 63 74 65  d for a protecte
26920 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c  d.** sqlite3_val
26930 75 65 20 6f 62 6a 65 63 74 20 62 75 74 20 6e 6f  ue object but no
26940 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20 66   mutex is held f
26950 6f 72 20 61 6e 20 75 6e 70 72 6f 74 65 63 74 65  or an unprotecte
26960 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c  d.** sqlite3_val
26970 75 65 20 6f 62 6a 65 63 74 2e 20 20 49 66 20 53  ue object.  If S
26980 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65  QLite is compile
26990 64 20 74 6f 20 62 65 20 73 69 6e 67 6c 65 2d 74  d to be single-t
269a0 68 72 65 61 64 65 64 0a 2a 2a 20 28 77 69 74 68  hreaded.** (with
269b0 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53   [SQLITE_THREADS
269c0 41 46 45 3d 30 5d 20 61 6e 64 20 77 69 74 68 20  AFE=0] and with 
269d0 5b 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73  [sqlite3_threads
269e0 61 66 65 28 29 5d 20 72 65 74 75 72 6e 69 6e 67  afe()] returning
269f0 20 30 29 0a 2a 2a 20 6f 72 20 69 66 20 53 51 4c   0).** or if SQL
26a00 69 74 65 20 69 73 20 72 75 6e 20 69 6e 20 6f 6e  ite is run in on
26a10 65 20 6f 66 20 72 65 64 75 63 65 64 20 6d 75 74  e of reduced mut
26a20 65 78 20 6d 6f 64 65 73 20 0a 2a 2a 20 5b 53 51  ex modes .** [SQ
26a30 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47  LITE_CONFIG_SING
26a40 4c 45 54 48 52 45 41 44 5d 20 6f 72 20 5b 53 51  LETHREAD] or [SQ
26a50 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54  LITE_CONFIG_MULT
26a60 49 54 48 52 45 41 44 5d 0a 2a 2a 20 74 68 65 6e  ITHREAD].** then
26a70 20 74 68 65 72 65 20 69 73 20 6e 6f 20 64 69 73   there is no dis
26a80 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65 65 6e  tinction between
26a90 20 70 72 6f 74 65 63 74 65 64 20 61 6e 64 20 75   protected and u
26aa0 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71  nprotected.** sq
26ab0 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65  lite3_value obje
26ac0 63 74 73 20 61 6e 64 20 74 68 65 79 20 63 61 6e  cts and they can
26ad0 20 62 65 20 75 73 65 64 20 69 6e 74 65 72 63 68   be used interch
26ae0 61 6e 67 65 61 62 6c 79 2e 20 20 48 6f 77 65 76  angeably.  Howev
26af0 65 72 2c 0a 2a 2a 20 66 6f 72 20 6d 61 78 69 6d  er,.** for maxim
26b00 75 6d 20 63 6f 64 65 20 70 6f 72 74 61 62 69 6c  um code portabil
26b10 69 74 79 20 69 74 20 69 73 20 72 65 63 6f 6d 6d  ity it is recomm
26b20 65 6e 64 65 64 20 74 68 61 74 20 61 70 70 6c 69  ended that appli
26b30 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 74 69 6c 6c  cations.** still
26b40 20 6d 61 6b 65 20 74 68 65 20 64 69 73 74 69 6e   make the distin
26b50 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 62 65  ction between be
26b60 74 77 65 65 6e 20 70 72 6f 74 65 63 74 65 64 20  tween protected 
26b70 61 6e 64 20 75 6e 70 72 6f 74 65 63 74 65 64 0a  and unprotected.
26b80 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ** sqlite3_value
26b90 20 6f 62 6a 65 63 74 73 20 65 76 65 6e 20 77 68   objects even wh
26ba0 65 6e 20 6e 6f 74 20 73 74 72 69 63 74 6c 79 20  en not strictly 
26bb0 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20  required..**.** 
26bc0 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  The sqlite3_valu
26bd0 65 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61  e objects that a
26be0 72 65 20 70 61 73 73 65 64 20 61 73 20 70 61 72  re passed as par
26bf0 61 6d 65 74 65 72 73 20 69 6e 74 6f 20 74 68 65  ameters into the
26c00 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
26c10 6f 6e 20 6f 66 20 5b 61 70 70 6c 69 63 61 74 69  on of [applicati
26c20 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66  on-defined SQL f
26c30 75 6e 63 74 69 6f 6e 73 5d 20 61 72 65 20 70 72  unctions] are pr
26c40 6f 74 65 63 74 65 64 2e 0a 2a 2a 20 54 68 65 20  otected..** The 
26c50 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62  sqlite3_value ob
26c60 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20 62 79  ject returned by
26c70 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c  .** [sqlite3_col
26c80 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73 20  umn_value()] is 
26c90 75 6e 70 72 6f 74 65 63 74 65 64 2e 0a 2a 2a 20  unprotected..** 
26ca0 55 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69  Unprotected sqli
26cb0 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74  te3_value object
26cc0 73 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73  s may only be us
26cd0 65 64 20 77 69 74 68 0a 2a 2a 20 5b 73 71 6c 69  ed with.** [sqli
26ce0 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65  te3_result_value
26cf0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
26d00 5f 62 69 6e 64 5f 76 61 6c 75 65 28 29 5d 2e 0a  _bind_value()]..
26d10 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  ** The [sqlite3_
26d20 76 61 6c 75 65 5f 62 6c 6f 62 20 7c 20 73 71 6c  value_blob | sql
26d30 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
26d40 29 5d 20 66 61 6d 69 6c 79 20 6f 66 0a 2a 2a 20  )] family of.** 
26d50 69 6e 74 65 72 66 61 63 65 73 20 72 65 71 75 69  interfaces requi
26d60 72 65 20 70 72 6f 74 65 63 74 65 64 20 73 71 6c  re protected sql
26d70 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63  ite3_value objec
26d80 74 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ts..*/.typedef s
26d90 74 72 75 63 74 20 4d 65 6d 20 73 71 6c 69 74 65  truct Mem sqlite
26da0 33 5f 76 61 6c 75 65 3b 0a 0a 2f 2a 0a 2a 2a 20  3_value;../*.** 
26db0 43 41 50 49 33 52 45 46 3a 20 53 51 4c 20 46 75  CAPI3REF: SQL Fu
26dc0 6e 63 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20 4f  nction Context O
26dd0 62 6a 65 63 74 20 7b 48 31 36 30 30 31 7d 20 3c  bject {H16001} <
26de0 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S20200>.**.** Th
26df0 65 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69  e context in whi
26e00 63 68 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69  ch an SQL functi
26e10 6f 6e 20 65 78 65 63 75 74 65 73 20 69 73 20 73  on executes is s
26e20 74 6f 72 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 73  tored in an.** s
26e30 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 6f  qlite3_context o
26e40 62 6a 65 63 74 2e 20 20 41 20 70 6f 69 6e 74 65  bject.  A pointe
26e50 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f  r to an sqlite3_
26e60 63 6f 6e 74 65 78 74 20 6f 62 6a 65 63 74 0a 2a  context object.*
26e70 2a 20 69 73 20 61 6c 77 61 79 73 20 66 69 72 73  * is always firs
26e80 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b  t parameter to [
26e90 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69  application-defi
26ea0 6e 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ned SQL function
26eb0 73 5d 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69  s]..** The appli
26ec0 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53  cation-defined S
26ed0 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c  QL function impl
26ee0 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 6c 6c 20  ementation will 
26ef0 70 61 73 73 20 74 68 69 73 0a 2a 2a 20 70 6f 69  pass this.** poi
26f00 6e 74 65 72 20 74 68 72 6f 75 67 68 20 69 6e 74  nter through int
26f10 6f 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69  o calls to [sqli
26f20 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 7c  te3_result_int |
26f30 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 28   sqlite3_result(
26f40 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  )],.** [sqlite3_
26f50 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78  aggregate_contex
26f60 74 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 75  t()], [sqlite3_u
26f70 73 65 72 5f 64 61 74 61 28 29 5d 2c 0a 2a 2a 20  ser_data()],.** 
26f80 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  [sqlite3_context
26f90 5f 64 62 5f 68 61 6e 64 6c 65 28 29 5d 2c 20 5b  _db_handle()], [
26fa0 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64  sqlite3_get_auxd
26fb0 61 74 61 28 29 5d 2c 0a 2a 2a 20 61 6e 64 2f 6f  ata()],.** and/o
26fc0 72 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  r [sqlite3_set_a
26fd0 75 78 64 61 74 61 28 29 5d 2e 0a 2a 2f 0a 74 79  uxdata()]..*/.ty
26fe0 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
26ff0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 71 6c  ite3_context sql
27000 69 74 65 33 5f 63 6f 6e 74 65 78 74 3b 0a 0a 2f  ite3_context;../
27010 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 42  *.** CAPI3REF: B
27020 69 6e 64 69 6e 67 20 56 61 6c 75 65 73 20 54 6f  inding Values To
27030 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d   Prepared Statem
27040 65 6e 74 73 20 7b 48 31 33 35 30 30 7d 20 3c 53  ents {H13500} <S
27050 37 30 33 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  70300>.** KEYWOR
27060 44 53 3a 20 7b 68 6f 73 74 20 70 61 72 61 6d 65  DS: {host parame
27070 74 65 72 7d 20 7b 68 6f 73 74 20 70 61 72 61 6d  ter} {host param
27080 65 74 65 72 73 7d 20 7b 68 6f 73 74 20 70 61 72  eters} {host par
27090 61 6d 65 74 65 72 20 6e 61 6d 65 7d 0a 2a 2a 20  ameter name}.** 
270a0 4b 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c 20 70  KEYWORDS: {SQL p
270b0 61 72 61 6d 65 74 65 72 7d 20 7b 53 51 4c 20 70  arameter} {SQL p
270c0 61 72 61 6d 65 74 65 72 73 7d 20 7b 70 61 72 61  arameters} {para
270d0 6d 65 74 65 72 20 62 69 6e 64 69 6e 67 7d 0a 2a  meter binding}.*
270e0 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 53 51 4c 20  *.** In the SQL 
270f0 73 74 72 69 6e 67 73 20 69 6e 70 75 74 20 74 6f  strings input to
27100 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
27110 65 5f 76 32 28 29 5d 20 61 6e 64 20 69 74 73 20  e_v2()] and its 
27120 76 61 72 69 61 6e 74 73 2c 0a 2a 2a 20 6c 69 74  variants,.** lit
27130 65 72 61 6c 73 20 6d 61 79 20 62 65 20 72 65 70  erals may be rep
27140 6c 61 63 65 64 20 62 79 20 61 20 70 61 72 61 6d  laced by a param
27150 65 74 65 72 20 69 6e 20 6f 6e 65 20 6f 66 20 74  eter in one of t
27160 68 65 73 65 20 66 6f 72 6d 73 3a 0a 2a 2a 0a 2a  hese forms:.**.*
27170 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20  * <ul>.** <li>  
27180 3f 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 4e 4e 4e 0a  ?.** <li>  ?NNN.
27190 2a 2a 20 3c 6c 69 3e 20 20 3a 56 56 56 0a 2a 2a  ** <li>  :VVV.**
271a0 20 3c 6c 69 3e 20 20 40 56 56 56 0a 2a 2a 20 3c   <li>  @VVV.** <
271b0 6c 69 3e 20 20 24 56 56 56 0a 2a 2a 20 3c 2f 75  li>  $VVV.** </u
271c0 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20  l>.**.** In the 
271d0 70 61 72 61 6d 65 74 65 72 20 66 6f 72 6d 73 20  parameter forms 
271e0 73 68 6f 77 6e 20 61 62 6f 76 65 20 4e 4e 4e 20  shown above NNN 
271f0 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6c 69  is an integer li
27200 74 65 72 61 6c 2c 0a 2a 2a 20 61 6e 64 20 56 56  teral,.** and VV
27210 56 20 69 73 20 61 6e 20 61 6c 70 68 61 2d 6e 75  V is an alpha-nu
27220 6d 65 72 69 63 20 70 61 72 61 6d 65 74 65 72 20  meric parameter 
27230 6e 61 6d 65 2e 20 54 68 65 20 76 61 6c 75 65 73  name. The values
27240 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 70 61 72   of these.** par
27250 61 6d 65 74 65 72 73 20 28 61 6c 73 6f 20 63 61  ameters (also ca
27260 6c 6c 65 64 20 22 68 6f 73 74 20 70 61 72 61 6d  lled "host param
27270 65 74 65 72 20 6e 61 6d 65 73 22 20 6f 72 20 22  eter names" or "
27280 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 22 29  SQL parameters")
27290 0a 2a 2a 20 63 61 6e 20 62 65 20 73 65 74 20 75  .** can be set u
272a0 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33  sing the sqlite3
272b0 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e  _bind_*() routin
272c0 65 73 20 64 65 66 69 6e 65 64 20 68 65 72 65 2e  es defined here.
272d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
272e0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
272f0 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28   sqlite3_bind_*(
27300 29 20 72 6f 75 74 69 6e 65 73 20 69 73 20 61 6c  ) routines is al
27310 77 61 79 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65  ways.** a pointe
27320 72 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65  r to the [sqlite
27330 33 5f 73 74 6d 74 5d 20 6f 62 6a 65 63 74 20 72  3_stmt] object r
27340 65 74 75 72 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20  eturned from.** 
27350 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
27360 5f 76 32 28 29 5d 20 6f 72 20 69 74 73 20 76 61  _v2()] or its va
27370 72 69 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  riants..**.** Th
27380 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
27390 74 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f  t is the index o
273a0 66 20 74 68 65 20 53 51 4c 20 70 61 72 61 6d 65  f the SQL parame
273b0 74 65 72 20 74 6f 20 62 65 20 73 65 74 2e 0a 2a  ter to be set..*
273c0 2a 20 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 53  * The leftmost S
273d0 51 4c 20 70 61 72 61 6d 65 74 65 72 20 68 61 73  QL parameter has
273e0 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 2e 20   an index of 1. 
273f0 20 57 68 65 6e 20 74 68 65 20 73 61 6d 65 20 6e   When the same n
27400 61 6d 65 64 0a 2a 2a 20 53 51 4c 20 70 61 72 61  amed.** SQL para
27410 6d 65 74 65 72 20 69 73 20 75 73 65 64 20 6d 6f  meter is used mo
27420 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 73 65  re than once, se
27430 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75  cond and subsequ
27440 65 6e 74 0a 2a 2a 20 6f 63 63 75 72 72 65 6e 63  ent.** occurrenc
27450 65 73 20 68 61 76 65 20 74 68 65 20 73 61 6d 65  es have the same
27460 20 69 6e 64 65 78 20 61 73 20 74 68 65 20 66 69   index as the fi
27470 72 73 74 20 6f 63 63 75 72 72 65 6e 63 65 2e 0a  rst occurrence..
27480 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 66 6f 72  ** The index for
27490 20 6e 61 6d 65 64 20 70 61 72 61 6d 65 74 65 72   named parameter
274a0 73 20 63 61 6e 20 62 65 20 6c 6f 6f 6b 65 64 20  s can be looked 
274b0 75 70 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  up using the.** 
274c0 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61  [sqlite3_bind_pa
274d0 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d  rameter_index()]
274e0 20 41 50 49 20 69 66 20 64 65 73 69 72 65 64 2e   API if desired.
274f0 20 20 54 68 65 20 69 6e 64 65 78 0a 2a 2a 20 66    The index.** f
27500 6f 72 20 22 3f 4e 4e 4e 22 20 70 61 72 61 6d 65  or "?NNN" parame
27510 74 65 72 73 20 69 73 20 74 68 65 20 76 61 6c 75  ters is the valu
27520 65 20 6f 66 20 4e 4e 4e 2e 0a 2a 2a 20 54 68 65  e of NNN..** The
27530 20 4e 4e 4e 20 76 61 6c 75 65 20 6d 75 73 74 20   NNN value must 
27540 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64  be between 1 and
27550 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 69   the [sqlite3_li
27560 6d 69 74 28 29 5d 0a 2a 2a 20 70 61 72 61 6d 65  mit()].** parame
27570 74 65 72 20 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  ter [SQLITE_LIMI
27580 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45  T_VARIABLE_NUMBE
27590 52 5d 20 28 64 65 66 61 75 6c 74 20 76 61 6c 75  R] (default valu
275a0 65 3a 20 39 39 39 29 2e 0a 2a 2a 0a 2a 2a 20 54  e: 999)..**.** T
275b0 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e  he third argumen
275c0 74 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 74  t is the value t
275d0 6f 20 62 69 6e 64 20 74 6f 20 74 68 65 20 70 61  o bind to the pa
275e0 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49  rameter..**.** I
275f0 6e 20 74 68 6f 73 65 20 72 6f 75 74 69 6e 65 73  n those routines
27600 20 74 68 61 74 20 68 61 76 65 20 61 20 66 6f 75   that have a fou
27610 72 74 68 20 61 72 67 75 6d 65 6e 74 2c 20 69 74  rth argument, it
27620 73 20 76 61 6c 75 65 20 69 73 20 74 68 65 0a 2a  s value is the.*
27630 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * number of byte
27640 73 20 69 6e 20 74 68 65 20 70 61 72 61 6d 65 74  s in the paramet
27650 65 72 2e 20 20 54 6f 20 62 65 20 63 6c 65 61 72  er.  To be clear
27660 3a 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 74  : the value is t
27670 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  he.** number of 
27680 3c 75 3e 62 79 74 65 73 3c 2f 75 3e 20 69 6e 20  <u>bytes</u> in 
27690 74 68 65 20 76 61 6c 75 65 2c 20 6e 6f 74 20 74  the value, not t
276a0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61  he number of cha
276b0 72 61 63 74 65 72 73 2e 0a 2a 2a 20 49 66 20 74  racters..** If t
276c0 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65  he fourth parame
276d0 74 65 72 20 69 73 20 6e 65 67 61 74 69 76 65 2c  ter is negative,
276e0 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74   the length of t
276f0 68 65 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20  he string is.** 
27700 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
27710 74 65 73 20 75 70 20 74 6f 20 74 68 65 20 66 69  tes up to the fi
27720 72 73 74 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61  rst zero termina
27730 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  tor..**.** The f
27740 69 66 74 68 20 61 72 67 75 6d 65 6e 74 20 74 6f  ifth argument to
27750 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c   sqlite3_bind_bl
27760 6f 62 28 29 2c 20 73 71 6c 69 74 65 33 5f 62 69  ob(), sqlite3_bi
27770 6e 64 5f 74 65 78 74 28 29 2c 20 61 6e 64 0a 2a  nd_text(), and.*
27780 2a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74  * sqlite3_bind_t
27790 65 78 74 31 36 28 29 20 69 73 20 61 20 64 65 73  ext16() is a des
277a0 74 72 75 63 74 6f 72 20 75 73 65 64 20 74 6f 20  tructor used to 
277b0 64 69 73 70 6f 73 65 20 6f 66 20 74 68 65 20 42  dispose of the B
277c0 4c 4f 42 20 6f 72 0a 2a 2a 20 73 74 72 69 6e 67  LOB or.** string
277d0 20 61 66 74 65 72 20 53 51 4c 69 74 65 20 68 61   after SQLite ha
277e0 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20  s finished with 
277f0 69 74 2e 20 49 66 20 74 68 65 20 66 69 66 74 68  it. If the fifth
27800 20 61 72 67 75 6d 65 6e 74 20 69 73 0a 2a 2a 20   argument is.** 
27810 74 68 65 20 73 70 65 63 69 61 6c 20 76 61 6c 75  the special valu
27820 65 20 5b 53 51 4c 49 54 45 5f 53 54 41 54 49 43  e [SQLITE_STATIC
27830 5d 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 61  ], then SQLite a
27840 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 0a  ssumes that the.
27850 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  ** information i
27860 73 20 69 6e 20 73 74 61 74 69 63 2c 20 75 6e 6d  s in static, unm
27870 61 6e 61 67 65 64 20 73 70 61 63 65 20 61 6e 64  anaged space and
27880 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
27890 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2a 20 49  o be freed..** I
278a0 66 20 74 68 65 20 66 69 66 74 68 20 61 72 67 75  f the fifth argu
278b0 6d 65 6e 74 20 68 61 73 20 74 68 65 20 76 61 6c  ment has the val
278c0 75 65 20 5b 53 51 4c 49 54 45 5f 54 52 41 4e 53  ue [SQLITE_TRANS
278d0 49 45 4e 54 5d 2c 20 74 68 65 6e 0a 2a 2a 20 53  IENT], then.** S
278e0 51 4c 69 74 65 20 6d 61 6b 65 73 20 69 74 73 20  QLite makes its 
278f0 6f 77 6e 20 70 72 69 76 61 74 65 20 63 6f 70 79  own private copy
27900 20 6f 66 20 74 68 65 20 64 61 74 61 20 69 6d 6d   of the data imm
27910 65 64 69 61 74 65 6c 79 2c 20 62 65 66 6f 72 65  ediately, before
27920 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f  .** the sqlite3_
27930 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65  bind_*() routine
27940 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20   returns..**.** 
27950 54 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  The sqlite3_bind
27960 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 72 6f 75 74  _zeroblob() rout
27970 69 6e 65 20 62 69 6e 64 73 20 61 20 42 4c 4f 42  ine binds a BLOB
27980 20 6f 66 20 6c 65 6e 67 74 68 20 4e 20 74 68 61   of length N tha
27990 74 0a 2a 2a 20 69 73 20 66 69 6c 6c 65 64 20 77  t.** is filled w
279a0 69 74 68 20 7a 65 72 6f 65 73 2e 20 20 41 20 7a  ith zeroes.  A z
279b0 65 72 6f 62 6c 6f 62 20 75 73 65 73 20 61 20 66  eroblob uses a f
279c0 69 78 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 6d  ixed amount of m
279d0 65 6d 6f 72 79 0a 2a 2a 20 28 6a 75 73 74 20 61  emory.** (just a
279e0 6e 20 69 6e 74 65 67 65 72 20 74 6f 20 68 6f 6c  n integer to hol
279f0 64 20 69 74 73 20 73 69 7a 65 29 20 77 68 69 6c  d its size) whil
27a00 65 20 69 74 20 69 73 20 62 65 69 6e 67 20 70 72  e it is being pr
27a10 6f 63 65 73 73 65 64 2e 0a 2a 2a 20 5a 65 72 6f  ocessed..** Zero
27a20 62 6c 6f 62 73 20 61 72 65 20 69 6e 74 65 6e 64  blobs are intend
27a30 65 64 20 74 6f 20 73 65 72 76 65 20 61 73 20 70  ed to serve as p
27a40 6c 61 63 65 68 6f 6c 64 65 72 73 20 66 6f 72 20  laceholders for 
27a50 42 4c 4f 42 73 20 77 68 6f 73 65 0a 2a 2a 20 63  BLOBs whose.** c
27a60 6f 6e 74 65 6e 74 20 69 73 20 6c 61 74 65 72 20  ontent is later 
27a70 77 72 69 74 74 65 6e 20 75 73 69 6e 67 0a 2a 2a  written using.**
27a80 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f   [sqlite3_blob_o
27a90 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65 6e 74 61  pen | incrementa
27aa0 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 72 6f 75 74  l BLOB I/O] rout
27ab0 69 6e 65 73 2e 0a 2a 2a 20 41 20 6e 65 67 61 74  ines..** A negat
27ac0 69 76 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68  ive value for th
27ad0 65 20 7a 65 72 6f 62 6c 6f 62 20 72 65 73 75 6c  e zeroblob resul
27ae0 74 73 20 69 6e 20 61 20 7a 65 72 6f 2d 6c 65 6e  ts in a zero-len
27af0 67 74 68 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20  gth BLOB..**.** 
27b00 54 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  The sqlite3_bind
27b10 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 6d 75  _*() routines mu
27b20 73 74 20 62 65 20 63 61 6c 6c 65 64 20 61 66 74  st be called aft
27b30 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70  er.** [sqlite3_p
27b40 72 65 70 61 72 65 5f 76 32 28 29 5d 20 28 61 6e  repare_v2()] (an
27b50 64 20 69 74 73 20 76 61 72 69 61 6e 74 73 29 20  d its variants) 
27b60 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65  or [sqlite3_rese
27b70 74 28 29 5d 20 61 6e 64 0a 2a 2a 20 62 65 66 6f  t()] and.** befo
27b80 72 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  re [sqlite3_step
27b90 28 29 5d 2e 0a 2a 2a 20 42 69 6e 64 69 6e 67 73  ()]..** Bindings
27ba0 20 61 72 65 20 6e 6f 74 20 63 6c 65 61 72 65 64   are not cleared
27bb0 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33   by the [sqlite3
27bc0 5f 72 65 73 65 74 28 29 5d 20 72 6f 75 74 69 6e  _reset()] routin
27bd0 65 2e 0a 2a 2a 20 55 6e 62 6f 75 6e 64 20 70 61  e..** Unbound pa
27be0 72 61 6d 65 74 65 72 73 20 61 72 65 20 69 6e 74  rameters are int
27bf0 65 72 70 72 65 74 65 64 20 61 73 20 4e 55 4c 4c  erpreted as NULL
27c00 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  ..**.** These ro
27c10 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 5b 53  utines return [S
27c20 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63  QLITE_OK] on suc
27c30 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72  cess or an error
27c40 20 63 6f 64 65 20 69 66 0a 2a 2a 20 61 6e 79 74   code if.** anyt
27c50 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e  hing goes wrong.
27c60 20 20 5b 53 51 4c 49 54 45 5f 52 41 4e 47 45 5d    [SQLITE_RANGE]
27c70 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
27c80 74 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a  the parameter.**
27c90 20 69 6e 64 65 78 20 69 73 20 6f 75 74 20 6f 66   index is out of
27ca0 20 72 61 6e 67 65 2e 20 20 5b 53 51 4c 49 54 45   range.  [SQLITE
27cb0 5f 4e 4f 4d 45 4d 5d 20 69 73 20 72 65 74 75 72  _NOMEM] is retur
27cc0 6e 65 64 20 69 66 20 6d 61 6c 6c 6f 63 28 29 20  ned if malloc() 
27cd0 66 61 69 6c 73 2e 0a 2a 2a 20 5b 53 51 4c 49 54  fails..** [SQLIT
27ce0 45 5f 4d 49 53 55 53 45 5d 20 6d 69 67 68 74 20  E_MISUSE] might 
27cf0 62 65 20 72 65 74 75 72 6e 65 64 20 69 66 20 74  be returned if t
27d00 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72  hese routines ar
27d10 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 0a 2a 2a  e called on a.**
27d20 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
27d30 20 74 68 61 74 20 69 73 20 74 68 65 20 77 72 6f   that is the wro
27d40 6e 67 20 73 74 61 74 65 20 6f 72 20 77 68 69 63  ng state or whic
27d50 68 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  h has already be
27d60 65 6e 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a  en finalized..**
27d70 20 44 65 74 65 63 74 69 6f 6e 20 6f 66 20 6d 69   Detection of mi
27d80 73 75 73 65 20 69 73 20 75 6e 72 65 6c 69 61 62  suse is unreliab
27d90 6c 65 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e  le.  Application
27da0 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 64 65 70  s should not dep
27db0 65 6e 64 0a 2a 2a 20 6f 6e 20 53 51 4c 49 54 45  end.** on SQLITE
27dc0 5f 4d 49 53 55 53 45 20 72 65 74 75 72 6e 73 2e  _MISUSE returns.
27dd0 20 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20    SQLITE_MISUSE 
27de0 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 69  is intended to i
27df0 6e 64 69 63 61 74 65 20 61 0a 2a 2a 20 61 20 6c  ndicate a.** a l
27e00 6f 67 69 63 20 65 72 72 6f 72 20 69 6e 20 74 68  ogic error in th
27e10 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20  e application.  
27e20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20  Future versions 
27e30 6f 66 20 53 51 4c 69 74 65 20 6d 69 67 68 74 0a  of SQLite might.
27e40 2a 2a 20 70 61 6e 69 63 20 72 61 74 68 65 72 20  ** panic rather 
27e50 74 68 61 6e 20 72 65 74 75 72 6e 20 53 51 4c 49  than return SQLI
27e60 54 45 5f 4d 49 53 55 53 45 2e 0a 2a 2a 0a 2a 2a  TE_MISUSE..**.**
27e70 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69   See also: [sqli
27e80 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
27e90 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 0a 2a 2a 20  er_count()],.** 
27ea0 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61  [sqlite3_bind_pa
27eb0 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 5d 2c  rameter_name()],
27ec0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 69   and [sqlite3_bi
27ed0 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64  nd_parameter_ind
27ee0 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56  ex()]..**.** INV
27ef0 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b  ARIANTS:.**.** {
27f00 48 31 33 35 30 36 7d 20 54 68 65 20 5b 53 51 4c  H13506} The [SQL
27f10 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69   statement compi
27f20 6c 65 72 5d 20 72 65 63 6f 67 6e 69 7a 65 73 20  ler] recognizes 
27f30 74 6f 6b 65 6e 73 20 6f 66 20 74 68 65 20 66 6f  tokens of the fo
27f40 72 6d 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  rms.**          
27f50 22 3f 22 2c 20 22 3f 4e 4e 4e 22 2c 20 22 24 56  "?", "?NNN", "$V
27f60 56 56 22 2c 20 22 3a 56 56 56 22 2c 20 61 6e 64  VV", ":VVV", and
27f70 20 22 40 56 56 56 22 20 61 73 20 53 51 4c 20 70   "@VVV" as SQL p
27f80 61 72 61 6d 65 74 65 72 73 2c 0a 2a 2a 20 20 20  arameters,.**   
27f90 20 20 20 20 20 20 20 77 68 65 72 65 20 4e 4e 4e         where NNN
27fa0 20 69 73 20 61 6e 79 20 73 65 71 75 65 6e 63 65   is any sequence
27fb0 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20   of one or more 
27fc0 64 69 67 69 74 73 0a 2a 2a 20 20 20 20 20 20 20  digits.**       
27fd0 20 20 20 61 6e 64 20 77 68 65 72 65 20 56 56 56     and where VVV
27fe0 20 69 73 20 61 6e 79 20 73 65 71 75 65 6e 63 65   is any sequence
27ff0 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20   of one or more 
28000 61 6c 70 68 61 6e 75 6d 65 72 69 63 0a 2a 2a 20  alphanumeric.** 
28010 20 20 20 20 20 20 20 20 20 63 68 61 72 61 63 74           charact
28020 65 72 73 20 6f 72 20 22 3a 3a 22 20 6f 70 74 69  ers or "::" opti
28030 6f 6e 61 6c 6c 79 20 66 6f 6c 6c 6f 77 65 64 20  onally followed 
28040 62 79 20 61 20 73 74 72 69 6e 67 20 63 6f 6e 74  by a string cont
28050 61 69 6e 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20  aining.**       
28060 20 20 20 6e 6f 20 73 70 61 63 65 73 20 61 6e 64     no spaces and
28070 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69   contained withi
28080 6e 20 70 61 72 65 6e 74 68 65 73 65 73 2e 0a 2a  n parentheses..*
28090 2a 0a 2a 2a 20 7b 48 31 33 35 30 39 7d 20 54 68  *.** {H13509} Th
280a0 65 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20  e initial value 
280b0 6f 66 20 61 6e 20 53 51 4c 20 70 61 72 61 6d 65  of an SQL parame
280c0 74 65 72 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a  ter is NULL..**.
280d0 2a 2a 20 7b 48 31 33 35 31 32 7d 20 54 68 65 20  ** {H13512} The 
280e0 69 6e 64 65 78 20 6f 66 20 61 6e 20 22 3f 22 20  index of an "?" 
280f0 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 69 73  SQL parameter is
28100 20 6f 6e 65 20 6c 61 72 67 65 72 20 74 68 61 6e   one larger than
28110 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
28120 20 6c 61 72 67 65 73 74 20 69 6e 64 65 78 20 6f   largest index o
28130 66 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20  f SQL parameter 
28140 74 6f 20 74 68 65 20 6c 65 66 74 2c 20 6f 72 20  to the left, or 
28150 31 20 69 66 0a 2a 2a 20 20 20 20 20 20 20 20 20  1 if.**         
28160 20 74 68 65 20 22 3f 22 20 69 73 20 74 68 65 20   the "?" is the 
28170 6c 65 66 74 6d 6f 73 74 20 53 51 4c 20 70 61 72  leftmost SQL par
28180 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48  ameter..**.** {H
28190 31 33 35 31 35 7d 20 54 68 65 20 69 6e 64 65 78  13515} The index
281a0 20 6f 66 20 61 6e 20 22 3f 4e 4e 4e 22 20 53 51   of an "?NNN" SQ
281b0 4c 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74  L parameter is t
281c0 68 65 20 69 6e 74 65 67 65 72 20 4e 4e 4e 2e 0a  he integer NNN..
281d0 2a 2a 0a 2a 2a 20 7b 48 31 33 35 31 38 7d 20 54  **.** {H13518} T
281e0 68 65 20 69 6e 64 65 78 20 6f 66 20 61 6e 20 22  he index of an "
281f0 3a 56 56 56 22 2c 20 22 24 56 56 56 22 2c 20 6f  :VVV", "$VVV", o
28200 72 20 22 40 56 56 56 22 20 53 51 4c 20 70 61 72  r "@VVV" SQL par
28210 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 20 20 20  ameter is.**    
28220 20 20 20 20 20 20 74 68 65 20 73 61 6d 65 20 61        the same a
28230 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 6c  s the index of l
28240 65 66 74 6d 6f 73 74 20 6f 63 63 75 72 72 65 6e  eftmost occurren
28250 63 65 73 20 6f 66 20 74 68 65 20 73 61 6d 65 0a  ces of the same.
28260 2a 2a 20 20 20 20 20 20 20 20 20 20 70 61 72 61  **          para
28270 6d 65 74 65 72 2c 20 6f 72 20 6f 6e 65 20 6d 6f  meter, or one mo
28280 72 65 20 74 68 61 6e 20 74 68 65 20 6c 61 72 67  re than the larg
28290 65 73 74 20 69 6e 64 65 78 20 6f 76 65 72 20 61  est index over a
282a0 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70  ll.**          p
282b0 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 65  arameters to the
282c0 20 6c 65 66 74 20 69 66 20 74 68 69 73 20 69 73   left if this is
282d0 20 74 68 65 20 66 69 72 73 74 20 6f 63 63 75 72   the first occur
282e0 72 65 6e 63 65 0a 2a 2a 20 20 20 20 20 20 20 20  rence.**        
282f0 20 20 6f 66 20 74 68 69 73 20 70 61 72 61 6d 65    of this parame
28300 74 65 72 2c 20 6f 72 20 31 20 69 66 20 74 68 69  ter, or 1 if thi
28310 73 20 69 73 20 74 68 65 20 6c 65 66 74 6d 6f 73  s is the leftmos
28320 74 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a  t parameter..**.
28330 2a 2a 20 7b 48 31 33 35 32 31 7d 20 54 68 65 20  ** {H13521} The 
28340 5b 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63  [SQL statement c
28350 6f 6d 70 69 6c 65 72 5d 20 66 61 69 6c 73 20 77  ompiler] fails w
28360 69 74 68 20 61 6e 20 5b 53 51 4c 49 54 45 5f 52  ith an [SQLITE_R
28370 41 4e 47 45 5d 0a 2a 2a 20 20 20 20 20 20 20 20  ANGE].**        
28380 20 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 69    error if the i
28390 6e 64 65 78 20 6f 66 20 61 6e 20 53 51 4c 20 70  ndex of an SQL p
283a0 61 72 61 6d 65 74 65 72 20 69 73 20 6c 65 73 73  arameter is less
283b0 20 74 68 61 6e 20 31 0a 2a 2a 20 20 20 20 20 20   than 1.**      
283c0 20 20 20 20 6f 72 20 67 72 65 61 74 65 72 20 74      or greater t
283d0 68 61 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 2d  han the compile-
283e0 74 69 6d 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  time SQLITE_MAX_
283f0 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 0a  VARIABLE_NUMBER.
28400 2a 2a 20 20 20 20 20 20 20 20 20 20 70 61 72 61  **          para
28410 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  meter..**.** {H1
28420 33 35 32 34 7d 20 43 61 6c 6c 73 20 74 6f 20 5b  3524} Calls to [
28430 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78  sqlite3_bind_tex
28440 74 20 7c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  t | sqlite3_bind
28450 28 53 2c 4e 2c 56 2c 2e 2e 2e 29 5d 0a 2a 2a 20  (S,N,V,...)].** 
28460 20 20 20 20 20 20 20 20 20 61 73 73 6f 63 69 61           associa
28470 74 65 20 74 68 65 20 76 61 6c 75 65 20 56 20 77  te the value V w
28480 69 74 68 20 61 6c 6c 20 53 51 4c 20 70 61 72 61  ith all SQL para
28490 6d 65 74 65 72 73 20 68 61 76 69 6e 67 20 61 6e  meters having an
284a0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e 64  .**          ind
284b0 65 78 20 6f 66 20 4e 20 69 6e 20 74 68 65 20 5b  ex of N in the [
284c0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
284d0 6e 74 5d 20 53 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  nt] S..**.** {H1
284e0 33 35 32 37 7d 20 43 61 6c 6c 73 20 74 6f 20 5b  3527} Calls to [
284f0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78  sqlite3_bind_tex
28500 74 20 7c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  t | sqlite3_bind
28510 28 53 2c 4e 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 20  (S,N,...)].**   
28520 20 20 20 20 20 20 20 6f 76 65 72 72 69 64 65 20         override 
28530 70 72 69 6f 72 20 63 61 6c 6c 73 20 77 69 74 68  prior calls with
28540 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 73   the same values
28550 20 6f 66 20 53 20 61 6e 64 20 4e 2e 0a 2a 2a 0a   of S and N..**.
28560 2a 2a 20 7b 48 31 33 35 33 30 7d 20 42 69 6e 64  ** {H13530} Bind
28570 69 6e 67 73 20 65 73 74 61 62 6c 69 73 68 65 64  ings established
28580 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e   by [sqlite3_bin
28590 64 5f 74 65 78 74 20 7c 20 73 71 6c 69 74 65 33  d_text | sqlite3
285a0 5f 62 69 6e 64 28 53 2c 2e 2e 2e 29 5d 0a 2a 2a  _bind(S,...)].**
285b0 20 20 20 20 20 20 20 20 20 20 70 65 72 73 69 73            persis
285c0 74 20 61 63 72 6f 73 73 20 63 61 6c 6c 73 20 74  t across calls t
285d0 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74  o [sqlite3_reset
285e0 28 53 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33  (S)]..**.** {H13
285f0 35 33 33 7d 20 49 6e 20 63 61 6c 6c 73 20 74 6f  533} In calls to
28600 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62   [sqlite3_bind_b
28610 6c 6f 62 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c  lob(S,N,V,L,D)],
28620 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71  .**          [sq
28630 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28  lite3_bind_text(
28640 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c 20 6f 72 0a  S,N,V,L,D)], or.
28650 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
28660 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36  ite3_bind_text16
28670 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 53 51 4c  (S,N,V,L,D)] SQL
28680 69 74 65 20 62 69 6e 64 73 20 74 68 65 20 66 69  ite binds the fi
28690 72 73 74 20 4c 0a 2a 2a 20 20 20 20 20 20 20 20  rst L.**        
286a0 20 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 42    bytes of the B
286b0 4c 4f 42 20 6f 72 20 73 74 72 69 6e 67 20 70 6f  LOB or string po
286c0 69 6e 74 65 64 20 74 6f 20 62 79 20 56 2c 20 77  inted to by V, w
286d0 68 65 6e 20 4c 0a 2a 2a 20 20 20 20 20 20 20 20  hen L.**        
286e0 20 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76    is non-negativ
286f0 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 35 33 36  e..**.** {H13536
28700 7d 20 49 6e 20 63 61 6c 6c 73 20 74 6f 20 5b 73  } In calls to [s
28710 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
28720 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 6f 72 0a  (S,N,V,L,D)] or.
28730 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
28740 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36  ite3_bind_text16
28750 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 53 51 4c  (S,N,V,L,D)] SQL
28760 69 74 65 20 62 69 6e 64 73 20 63 68 61 72 61 63  ite binds charac
28770 74 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  ters.**         
28780 20 66 72 6f 6d 20 56 20 74 68 72 6f 75 67 68 20   from V through 
28790 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f 20 63  the first zero c
287a0 68 61 72 61 63 74 65 72 20 77 68 65 6e 20 4c 20  haracter when L 
287b0 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a  is negative..**.
287c0 2a 2a 20 7b 48 31 33 35 33 39 7d 20 49 6e 20 63  ** {H13539} In c
287d0 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33  alls to [sqlite3
287e0 5f 62 69 6e 64 5f 62 6c 6f 62 28 53 2c 4e 2c 56  _bind_blob(S,N,V
287f0 2c 4c 2c 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 20  ,L,D)],.**      
28800 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e      [sqlite3_bin
28810 64 5f 74 65 78 74 28 53 2c 4e 2c 56 2c 4c 2c 44  d_text(S,N,V,L,D
28820 29 5d 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20  )], or.**       
28830 20 20 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64     [sqlite3_bind
28840 5f 74 65 78 74 31 36 28 53 2c 4e 2c 56 2c 4c 2c  _text16(S,N,V,L,
28850 44 29 5d 20 77 68 65 6e 20 44 20 69 73 20 74 68  D)] when D is th
28860 65 20 73 70 65 63 69 61 6c 0a 2a 2a 20 20 20 20  e special.**    
28870 20 20 20 20 20 20 63 6f 6e 73 74 61 6e 74 20 5b        constant [
28880 53 51 4c 49 54 45 5f 53 54 41 54 49 43 5d 2c 20  SQLITE_STATIC], 
28890 53 51 4c 69 74 65 20 61 73 73 75 6d 65 73 20 74  SQLite assumes t
288a0 68 61 74 20 74 68 65 20 76 61 6c 75 65 20 56 0a  hat the value V.
288b0 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73 20 68  **          is h
288c0 65 6c 64 20 69 6e 20 73 74 61 74 69 63 20 75 6e  eld in static un
288d0 6d 61 6e 61 67 65 64 20 73 70 61 63 65 20 74 68  managed space th
288e0 61 74 20 77 69 6c 6c 20 6e 6f 74 20 63 68 61 6e  at will not chan
288f0 67 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 64  ge.**          d
28900 75 72 69 6e 67 20 74 68 65 20 6c 69 66 65 74 69  uring the lifeti
28910 6d 65 20 6f 66 20 74 68 65 20 62 69 6e 64 69 6e  me of the bindin
28920 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 35 34 32  g..**.** {H13542
28930 7d 20 49 6e 20 63 61 6c 6c 73 20 74 6f 20 5b 73  } In calls to [s
28940 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
28950 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c 0a 2a 2a  (S,N,V,L,D)],.**
28960 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
28970 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 53 2c 4e  e3_bind_text(S,N
28980 2c 56 2c 4c 2c 44 29 5d 2c 20 6f 72 0a 2a 2a 20  ,V,L,D)], or.** 
28990 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
289a0 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 28 53 2c  3_bind_text16(S,
289b0 4e 2c 56 2c 4c 2c 44 29 5d 20 77 68 65 6e 20 44  N,V,L,D)] when D
289c0 20 69 73 20 74 68 65 20 73 70 65 63 69 61 6c 0a   is the special.
289d0 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73  **          cons
289e0 74 61 6e 74 20 5b 53 51 4c 49 54 45 5f 54 52 41  tant [SQLITE_TRA
289f0 4e 53 49 45 4e 54 5d 2c 20 74 68 65 20 72 6f 75  NSIENT], the rou
28a00 74 69 6e 65 20 6d 61 6b 65 73 20 61 0a 2a 2a 20  tine makes a.** 
28a10 20 20 20 20 20 20 20 20 20 70 72 69 76 61 74 65           private
28a20 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c   copy of the val
28a30 75 65 20 56 20 62 65 66 6f 72 65 20 69 74 20 72  ue V before it r
28a40 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48  eturns..**.** {H
28a50 31 33 35 34 35 7d 20 49 6e 20 63 61 6c 6c 73 20  13545} In calls 
28a60 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  to [sqlite3_bind
28a70 5f 62 6c 6f 62 28 53 2c 4e 2c 56 2c 4c 2c 44 29  _blob(S,N,V,L,D)
28a80 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  ],.**          [
28a90 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78  sqlite3_bind_tex
28aa0 74 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c 20 6f  t(S,N,V,L,D)], o
28ab0 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73  r.**          [s
28ac0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
28ad0 31 36 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 77  16(S,N,V,L,D)] w
28ae0 68 65 6e 20 44 20 69 73 20 61 20 70 6f 69 6e 74  hen D is a point
28af0 65 72 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20  er to.**        
28b00 20 20 61 20 66 75 6e 63 74 69 6f 6e 2c 20 53 51    a function, SQ
28b10 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 74 68 61  Lite invokes tha
28b20 74 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 64 65  t function to de
28b30 73 74 72 6f 79 20 74 68 65 0a 2a 2a 20 20 20 20  stroy the.**    
28b40 20 20 20 20 20 20 76 61 6c 75 65 20 56 20 61 66        value V af
28b50 74 65 72 20 69 74 20 68 61 73 20 66 69 6e 69 73  ter it has finis
28b60 68 65 64 20 75 73 69 6e 67 20 74 68 65 20 76 61  hed using the va
28b70 6c 75 65 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  lue V..**.** {H1
28b80 33 35 34 38 7d 20 49 6e 20 63 61 6c 6c 73 20 74  3548} In calls t
28b90 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  o [sqlite3_bind_
28ba0 7a 65 72 6f 62 6c 6f 62 28 53 2c 4e 2c 56 2c 4c  zeroblob(S,N,V,L
28bb0 29 5d 20 74 68 65 20 76 61 6c 75 65 20 62 6f 75  )] the value bou
28bc0 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69  nd.**          i
28bd0 73 20 61 20 42 4c 4f 42 20 6f 66 20 4c 20 62 79  s a BLOB of L by
28be0 74 65 73 2c 20 6f 72 20 61 20 7a 65 72 6f 2d 6c  tes, or a zero-l
28bf0 65 6e 67 74 68 20 42 4c 4f 42 20 69 66 20 4c 20  ength BLOB if L 
28c00 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a  is negative..**.
28c10 2a 2a 20 7b 48 31 33 35 35 31 7d 20 49 6e 20 63  ** {H13551} In c
28c20 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33  alls to [sqlite3
28c30 5f 62 69 6e 64 5f 76 61 6c 75 65 28 53 2c 4e 2c  _bind_value(S,N,
28c40 56 29 5d 20 74 68 65 20 56 20 61 72 67 75 6d 65  V)] the V argume
28c50 6e 74 20 6d 61 79 0a 2a 2a 20 20 20 20 20 20 20  nt may.**       
28c60 20 20 20 62 65 20 65 69 74 68 65 72 20 61 20 5b     be either a [
28c70 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
28c80 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20  3_value] object 
28c90 6f 72 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20  or an.**        
28ca0 20 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73    [unprotected s
28cb0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62  qlite3_value] ob
28cc0 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ject..*/.SQLITE_
28cd0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
28ce0 62 69 6e 64 5f 62 6c 6f 62 28 73 71 6c 69 74 65  bind_blob(sqlite
28cf0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f  3_stmt*, int, co
28d00 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e  nst void*, int n
28d10 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  , void(*)(void*)
28d20 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
28d30 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64  t sqlite3_bind_d
28d40 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 74  ouble(sqlite3_st
28d50 6d 74 2a 2c 20 69 6e 74 2c 20 64 6f 75 62 6c 65  mt*, int, double
28d60 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
28d70 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  t sqlite3_bind_i
28d80 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  nt(sqlite3_stmt*
28d90 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  , int, int);.SQL
28da0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
28db0 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73  te3_bind_int64(s
28dc0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
28dd0 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  t, sqlite3_int64
28de0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
28df0 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e  t sqlite3_bind_n
28e00 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ull(sqlite3_stmt
28e10 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
28e20 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
28e30 62 69 6e 64 5f 74 65 78 74 28 73 71 6c 69 74 65  bind_text(sqlite
28e40 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f  3_stmt*, int, co
28e50 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 20 6e  nst char*, int n
28e60 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  , void(*)(void*)
28e70 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
28e80 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74  t sqlite3_bind_t
28e90 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73 74  ext16(sqlite3_st
28ea0 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20  mt*, int, const 
28eb0 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64  void*, int, void
28ec0 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c  (*)(void*));.SQL
28ed0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
28ee0 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73  te3_bind_value(s
28ef0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
28f00 74 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  t, const sqlite3
28f10 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45  _value*);.SQLITE
28f20 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
28f30 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73  _bind_zeroblob(s
28f40 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
28f50 74 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a  t, int n);../*.*
28f60 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d 62  * CAPI3REF: Numb
28f70 65 72 20 4f 66 20 53 51 4c 20 50 61 72 61 6d 65  er Of SQL Parame
28f80 74 65 72 73 20 7b 48 31 33 36 30 30 7d 20 3c 53  ters {H13600} <S
28f90 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69  70300>.**.** Thi
28fa0 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65  s routine can be
28fb0 20 75 73 65 64 20 74 6f 20 66 69 6e 64 20 74 68   used to find th
28fc0 65 20 6e 75 6d 62 65 72 20 6f 66 20 5b 53 51 4c  e number of [SQL
28fd0 20 70 61 72 61 6d 65 74 65 72 73 5d 0a 2a 2a 20   parameters].** 
28fe0 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 73  in a [prepared s
28ff0 74 61 74 65 6d 65 6e 74 5d 2e 20 20 53 51 4c 20  tatement].  SQL 
29000 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 74  parameters are t
29010 6f 6b 65 6e 73 20 6f 66 20 74 68 65 0a 2a 2a 20  okens of the.** 
29020 66 6f 72 6d 20 22 3f 22 2c 20 22 3f 4e 4e 4e 22  form "?", "?NNN"
29030 2c 20 22 3a 41 41 41 22 2c 20 22 24 41 41 41 22  , ":AAA", "$AAA"
29040 2c 20 6f 72 20 22 40 41 41 41 22 20 74 68 61 74  , or "@AAA" that
29050 20 73 65 72 76 65 20 61 73 0a 2a 2a 20 70 6c 61   serve as.** pla
29060 63 65 68 6f 6c 64 65 72 73 20 66 6f 72 20 76 61  ceholders for va
29070 6c 75 65 73 20 74 68 61 74 20 61 72 65 20 5b 73  lues that are [s
29080 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
29090 20 7c 20 62 6f 75 6e 64 5d 0a 2a 2a 20 74 6f 20   | bound].** to 
290a0 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 61  the parameters a
290b0 74 20 61 20 6c 61 74 65 72 20 74 69 6d 65 2e 0a  t a later time..
290c0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
290d0 6e 65 20 61 63 74 75 61 6c 6c 79 20 72 65 74 75  ne actually retu
290e0 72 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66  rns the index of
290f0 20 74 68 65 20 6c 61 72 67 65 73 74 20 28 72 69   the largest (ri
29100 67 68 74 6d 6f 73 74 29 0a 2a 2a 20 70 61 72 61  ghtmost).** para
29110 6d 65 74 65 72 2e 20 46 6f 72 20 61 6c 6c 20 66  meter. For all f
29120 6f 72 6d 73 20 65 78 63 65 70 74 20 3f 4e 4e 4e  orms except ?NNN
29130 2c 20 74 68 69 73 20 77 69 6c 6c 20 63 6f 72 72  , this will corr
29140 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 0a 2a 2a  espond to the.**
29150 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 71 75   number of uniqu
29160 65 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20 49  e parameters.  I
29170 66 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 20  f parameters of 
29180 74 68 65 20 3f 4e 4e 4e 20 61 72 65 20 75 73 65  the ?NNN are use
29190 64 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 61 79 20  d,.** there may 
291a0 62 65 20 67 61 70 73 20 69 6e 20 74 68 65 20 6c  be gaps in the l
291b0 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  ist..**.** See a
291c0 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62 69  lso: [sqlite3_bi
291d0 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f  nd_blob|sqlite3_
291e0 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c  bind()],.** [sql
291f0 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
29200 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e 64  ter_name()], and
29210 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e  .** [sqlite3_bin
29220 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65  d_parameter_inde
29230 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41  x()]..**.** INVA
29240 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48  RIANTS:.**.** {H
29250 31 33 36 30 31 7d 20 54 68 65 20 5b 73 71 6c 69  13601} The [sqli
29260 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
29270 65 72 5f 63 6f 75 6e 74 28 53 29 5d 20 69 6e 74  er_count(S)] int
29280 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 0a 2a  erface returns.*
29290 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 6c  *          the l
292a0 61 72 67 65 73 74 20 69 6e 64 65 78 20 6f 66 20  argest index of 
292b0 61 6c 6c 20 53 51 4c 20 70 61 72 61 6d 65 74 65  all SQL paramete
292c0 72 73 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20  rs in the.**    
292d0 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 20        [prepared 
292e0 73 74 61 74 65 6d 65 6e 74 5d 20 53 2c 20 6f 72  statement] S, or
292f0 20 30 20 69 66 20 53 20 63 6f 6e 74 61 69 6e 73   0 if S contains
29300 20 6e 6f 20 53 51 4c 20 70 61 72 61 6d 65 74 65   no SQL paramete
29310 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  rs..*/.SQLITE_AP
29320 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
29330 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75  nd_parameter_cou
29340 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  nt(sqlite3_stmt*
29350 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
29360 45 46 3a 20 4e 61 6d 65 20 4f 66 20 41 20 48 6f  EF: Name Of A Ho
29370 73 74 20 50 61 72 61 6d 65 74 65 72 20 7b 48 31  st Parameter {H1
29380 33 36 32 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a  3620} <S70300>.*
29390 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
293a0 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  e returns a poin
293b0 74 65 72 20 74 6f 20 74 68 65 20 6e 61 6d 65 20  ter to the name 
293c0 6f 66 20 74 68 65 20 6e 2d 74 68 0a 2a 2a 20 5b  of the n-th.** [
293d0 53 51 4c 20 70 61 72 61 6d 65 74 65 72 5d 20 69  SQL parameter] i
293e0 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74  n a [prepared st
293f0 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 53 51 4c  atement]..** SQL
29400 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 20 74   parameters of t
29410 68 65 20 66 6f 72 6d 20 22 3f 4e 4e 4e 22 20 6f  he form "?NNN" o
29420 72 20 22 3a 41 41 41 22 20 6f 72 20 22 40 41 41  r ":AAA" or "@AA
29430 41 22 20 6f 72 20 22 24 41 41 41 22 0a 2a 2a 20  A" or "$AAA".** 
29440 68 61 76 65 20 61 20 6e 61 6d 65 20 77 68 69 63  have a name whic
29450 68 20 69 73 20 74 68 65 20 73 74 72 69 6e 67 20  h is the string 
29460 22 3f 4e 4e 4e 22 20 6f 72 20 22 3a 41 41 41 22  "?NNN" or ":AAA"
29470 20 6f 72 20 22 40 41 41 41 22 20 6f 72 20 22 24   or "@AAA" or "$
29480 41 41 41 22 0a 2a 2a 20 72 65 73 70 65 63 74 69  AAA".** respecti
29490 76 65 6c 79 2e 0a 2a 2a 20 49 6e 20 6f 74 68 65  vely..** In othe
294a0 72 20 77 6f 72 64 73 2c 20 74 68 65 20 69 6e 69  r words, the ini
294b0 74 69 61 6c 20 22 3a 22 20 6f 72 20 22 24 22 20  tial ":" or "$" 
294c0 6f 72 20 22 40 22 20 6f 72 20 22 3f 22 0a 2a 2a  or "@" or "?".**
294d0 20 69 73 20 69 6e 63 6c 75 64 65 64 20 61 73 20   is included as 
294e0 70 61 72 74 20 6f 66 20 74 68 65 20 6e 61 6d 65  part of the name
294f0 2e 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 73 20  ..** Parameters 
29500 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 22 20  of the form "?" 
29510 77 69 74 68 6f 75 74 20 61 20 66 6f 6c 6c 6f 77  without a follow
29520 69 6e 67 20 69 6e 74 65 67 65 72 20 68 61 76 65  ing integer have
29530 20 6e 6f 20 6e 61 6d 65 0a 2a 2a 20 61 6e 64 20   no name.** and 
29540 61 72 65 20 61 6c 73 6f 20 72 65 66 65 72 72 65  are also referre
29550 64 20 74 6f 20 61 73 20 22 61 6e 6f 6e 79 6d 6f  d to as "anonymo
29560 75 73 20 70 61 72 61 6d 65 74 65 72 73 22 2e 0a  us parameters"..
29570 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  **.** The first 
29580 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 20 68  host parameter h
29590 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31  as an index of 1
295a0 2c 20 6e 6f 74 20 30 2e 0a 2a 2a 0a 2a 2a 20 49  , not 0..**.** I
295b0 66 20 74 68 65 20 76 61 6c 75 65 20 6e 20 69 73  f the value n is
295c0 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 6f 72   out of range or
295d0 20 69 66 20 74 68 65 20 6e 2d 74 68 20 70 61 72   if the n-th par
295e0 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 6e 61 6d  ameter is.** nam
295f0 65 6c 65 73 73 2c 20 74 68 65 6e 20 4e 55 4c 4c  eless, then NULL
29600 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54   is returned.  T
29610 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69  he returned stri
29620 6e 67 20 69 73 0a 2a 2a 20 61 6c 77 61 79 73 20  ng is.** always 
29630 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e  in UTF-8 encodin
29640 67 20 65 76 65 6e 20 69 66 20 74 68 65 20 6e 61  g even if the na
29650 6d 65 64 20 70 61 72 61 6d 65 74 65 72 20 77 61  med parameter wa
29660 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 20  s.** originally 
29670 73 70 65 63 69 66 69 65 64 20 61 73 20 55 54 46  specified as UTF
29680 2d 31 36 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f  -16 in [sqlite3_
29690 70 72 65 70 61 72 65 31 36 28 29 5d 20 6f 72 0a  prepare16()] or.
296a0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  ** [sqlite3_prep
296b0 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a  are16_v2()]..**.
296c0 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71  ** See also: [sq
296d0 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c  lite3_bind_blob|
296e0 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2c  sqlite3_bind()],
296f0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e  .** [sqlite3_bin
29700 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e  d_parameter_coun
29710 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71  t()], and.** [sq
29720 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
29730 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a  eter_index()]..*
29740 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
29750 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 36 32 31 7d 20  .**.** {H13621} 
29760 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e  The [sqlite3_bin
29770 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65  d_parameter_name
29780 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65  (S,N)] interface
29790 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20   returns.**     
297a0 20 20 20 20 20 61 20 55 54 46 2d 38 20 72 65 6e       a UTF-8 ren
297b0 64 65 72 69 6e 67 20 6f 66 20 74 68 65 20 6e 61  dering of the na
297c0 6d 65 20 6f 66 20 74 68 65 20 53 51 4c 20 70 61  me of the SQL pa
297d0 72 61 6d 65 74 65 72 20 69 6e 0a 2a 2a 20 20 20  rameter in.**   
297e0 20 20 20 20 20 20 20 74 68 65 20 5b 70 72 65 70         the [prep
297f0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
29800 53 20 68 61 76 69 6e 67 20 69 6e 64 65 78 20 4e  S having index N
29810 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  , or.**         
29820 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69   NULL if there i
29830 73 20 6e 6f 20 53 51 4c 20 70 61 72 61 6d 65 74  s no SQL paramet
29840 65 72 20 77 69 74 68 20 69 6e 64 65 78 20 4e 20  er with index N 
29850 6f 72 20 69 66 20 74 68 65 0a 2a 2a 20 20 20 20  or if the.**    
29860 20 20 20 20 20 20 70 61 72 61 6d 65 74 65 72 20        parameter 
29870 77 69 74 68 20 69 6e 64 65 78 20 4e 20 69 73 20  with index N is 
29880 61 6e 20 61 6e 6f 6e 79 6d 6f 75 73 20 70 61 72  an anonymous par
29890 61 6d 65 74 65 72 20 22 3f 22 2e 0a 2a 2f 0a 53  ameter "?"..*/.S
298a0 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
298b0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 62 69  char *sqlite3_bi
298c0 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d  nd_parameter_nam
298d0 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  e(sqlite3_stmt*,
298e0 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   int);../*.** CA
298f0 50 49 33 52 45 46 3a 20 49 6e 64 65 78 20 4f 66  PI3REF: Index Of
29900 20 41 20 50 61 72 61 6d 65 74 65 72 20 57 69 74   A Parameter Wit
29910 68 20 41 20 47 69 76 65 6e 20 4e 61 6d 65 20 7b  h A Given Name {
29920 48 31 33 36 34 30 7d 20 3c 53 37 30 33 30 30 3e  H13640} <S70300>
29930 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  .**.** Return th
29940 65 20 69 6e 64 65 78 20 6f 66 20 61 6e 20 53 51  e index of an SQ
29950 4c 20 70 61 72 61 6d 65 74 65 72 20 67 69 76 65  L parameter give
29960 6e 20 69 74 73 20 6e 61 6d 65 2e 20 20 54 68 65  n its name.  The
29970 0a 2a 2a 20 69 6e 64 65 78 20 76 61 6c 75 65 20  .** index value 
29980 72 65 74 75 72 6e 65 64 20 69 73 20 73 75 69 74  returned is suit
29990 61 62 6c 65 20 66 6f 72 20 75 73 65 20 61 73 20  able for use as 
299a0 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 70 61  the second.** pa
299b0 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69  rameter to [sqli
299c0 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71  te3_bind_blob|sq
299d0 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2e 20 20  lite3_bind()].  
299e0 41 20 7a 65 72 6f 0a 2a 2a 20 69 73 20 72 65 74  A zero.** is ret
299f0 75 72 6e 65 64 20 69 66 20 6e 6f 20 6d 61 74 63  urned if no matc
29a00 68 69 6e 67 20 70 61 72 61 6d 65 74 65 72 20 69  hing parameter i
29a10 73 20 66 6f 75 6e 64 2e 20 20 54 68 65 20 70 61  s found.  The pa
29a20 72 61 6d 65 74 65 72 0a 2a 2a 20 6e 61 6d 65 20  rameter.** name 
29a30 6d 75 73 74 20 62 65 20 67 69 76 65 6e 20 69 6e  must be given in
29a40 20 55 54 46 2d 38 20 65 76 65 6e 20 69 66 20 74   UTF-8 even if t
29a50 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74  he original stat
29a60 65 6d 65 6e 74 0a 2a 2a 20 77 61 73 20 70 72 65  ement.** was pre
29a70 70 61 72 65 64 20 66 72 6f 6d 20 55 54 46 2d 31  pared from UTF-1
29a80 36 20 74 65 78 74 20 75 73 69 6e 67 20 5b 73 71  6 text using [sq
29a90 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f  lite3_prepare16_
29aa0 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  v2()]..**.** See
29ab0 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f   also: [sqlite3_
29ac0 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65  bind_blob|sqlite
29ad0 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73  3_bind()],.** [s
29ae0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61  qlite3_bind_para
29af0 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 20  meter_count()], 
29b00 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  and.** [sqlite3_
29b10 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69  bind_parameter_i
29b20 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49  ndex()]..**.** I
29b30 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a  NVARIANTS:.**.**
29b40 20 7b 48 31 33 36 34 31 7d 20 54 68 65 20 5b 73   {H13641} The [s
29b50 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61  qlite3_bind_para
29b60 6d 65 74 65 72 5f 69 6e 64 65 78 28 53 2c 4e 29  meter_index(S,N)
29b70 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75  ] interface retu
29b80 72 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  rns.**          
29b90 74 68 65 20 69 6e 64 65 78 20 6f 66 20 53 51 4c  the index of SQL
29ba0 20 70 61 72 61 6d 65 74 65 72 20 69 6e 20 74 68   parameter in th
29bb0 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  e [prepared stat
29bc0 65 6d 65 6e 74 5d 0a 2a 2a 20 20 20 20 20 20 20  ement].**       
29bd0 20 20 20 53 20 77 68 6f 73 65 20 6e 61 6d 65 20     S whose name 
29be0 6d 61 74 63 68 65 73 20 74 68 65 20 55 54 46 2d  matches the UTF-
29bf0 38 20 73 74 72 69 6e 67 20 4e 2c 20 6f 72 20 30  8 string N, or 0
29c00 20 69 66 20 74 68 65 72 65 20 69 73 0a 2a 2a 20   if there is.** 
29c10 20 20 20 20 20 20 20 20 20 6e 6f 20 6d 61 74 63           no matc
29c20 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  h..*/.SQLITE_API
29c30 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
29c40 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65  d_parameter_inde
29c50 78 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  x(sqlite3_stmt*,
29c60 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
29c70 6d 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  me);../*.** CAPI
29c80 33 52 45 46 3a 20 52 65 73 65 74 20 41 6c 6c 20  3REF: Reset All 
29c90 42 69 6e 64 69 6e 67 73 20 4f 6e 20 41 20 50 72  Bindings On A Pr
29ca0 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74  epared Statement
29cb0 20 7b 48 31 33 36 36 30 7d 20 3c 53 37 30 33 30   {H13660} <S7030
29cc0 30 3e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 74 72 61 72  0>.**.** Contrar
29cd0 79 20 74 6f 20 74 68 65 20 69 6e 74 75 69 74 69  y to the intuiti
29ce0 6f 6e 20 6f 66 20 6d 61 6e 79 2c 20 5b 73 71 6c  on of many, [sql
29cf0 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 64 6f  ite3_reset()] do
29d00 65 73 20 6e 6f 74 20 72 65 73 65 74 0a 2a 2a 20  es not reset.** 
29d10 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e  the [sqlite3_bin
29d20 64 5f 62 6c 6f 62 20 7c 20 62 69 6e 64 69 6e 67  d_blob | binding
29d30 73 5d 20 6f 6e 20 61 20 5b 70 72 65 70 61 72 65  s] on a [prepare
29d40 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a  d statement]..**
29d50 20 55 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e   Use this routin
29d60 65 20 74 6f 20 72 65 73 65 74 20 61 6c 6c 20 68  e to reset all h
29d70 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73 20 74  ost parameters t
29d80 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 4e  o NULL..**.** IN
29d90 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20  VARIANTS:.**.** 
29da0 7b 48 31 33 36 36 31 7d 20 54 68 65 20 5b 73 71  {H13661} The [sq
29db0 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64  lite3_clear_bind
29dc0 69 6e 67 73 28 53 29 5d 20 69 6e 74 65 72 66 61  ings(S)] interfa
29dd0 63 65 20 72 65 73 65 74 73 20 61 6c 6c 20 53 51  ce resets all SQ
29de0 4c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 61  L.**          pa
29df0 72 61 6d 65 74 65 72 20 62 69 6e 64 69 6e 67 73  rameter bindings
29e00 20 69 6e 20 74 68 65 20 5b 70 72 65 70 61 72 65   in the [prepare
29e10 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 62  d statement] S b
29e20 61 63 6b 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a  ack to NULL..*/.
29e30 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
29e40 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e  qlite3_clear_bin
29e50 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74  dings(sqlite3_st
29e60 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  mt*);../*.** CAP
29e70 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 4f 66  I3REF: Number Of
29e80 20 43 6f 6c 75 6d 6e 73 20 49 6e 20 41 20 52 65   Columns In A Re
29e90 73 75 6c 74 20 53 65 74 20 7b 48 31 33 37 31 30  sult Set {H13710
29ea0 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a  } <S10700>.**.**
29eb0 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
29ec0 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
29ed0 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
29ee0 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 0a  returned by the.
29ef0 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  ** [prepared sta
29f00 74 65 6d 65 6e 74 5d 2e 20 54 68 69 73 20 72 6f  tement]. This ro
29f10 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 30 20  utine returns 0 
29f20 69 66 20 70 53 74 6d 74 20 69 73 20 61 6e 20 53  if pStmt is an S
29f30 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20  QL.** statement 
29f40 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 72 65  that does not re
29f50 74 75 72 6e 20 64 61 74 61 20 28 66 6f 72 20 65  turn data (for e
29f60 78 61 6d 70 6c 65 20 61 6e 20 5b 55 50 44 41 54  xample an [UPDAT
29f70 45 5d 29 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52  E])..**.** INVAR
29f80 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31  IANTS:.**.** {H1
29f90 33 37 31 31 7d 20 54 68 65 20 5b 73 71 6c 69 74  3711} The [sqlit
29fa0 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28  e3_column_count(
29fb0 53 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65  S)] interface re
29fc0 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
29fd0 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   of.**          
29fe0 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72  columns in the r
29ff0 65 73 75 6c 74 20 73 65 74 20 67 65 6e 65 72 61  esult set genera
2a000 74 65 64 20 62 79 20 74 68 65 20 5b 70 72 65 70  ted by the [prep
2a010 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
2a020 53 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  S,.**          o
2a030 72 20 30 20 69 66 20 53 20 64 6f 65 73 20 6e 6f  r 0 if S does no
2a040 74 20 67 65 6e 65 72 61 74 65 20 61 20 72 65 73  t generate a res
2a050 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49  ult set..*/.SQLI
2a060 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2a070 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28  e3_column_count(
2a080 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
2a090 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  tmt);../*.** CAP
2a0a0 49 33 52 45 46 3a 20 43 6f 6c 75 6d 6e 20 4e 61  I3REF: Column Na
2a0b0 6d 65 73 20 49 6e 20 41 20 52 65 73 75 6c 74 20  mes In A Result 
2a0c0 53 65 74 20 7b 48 31 33 37 32 30 7d 20 3c 53 31  Set {H13720} <S1
2a0d0 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  0700>.**.** Thes
2a0e0 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72  e routines retur
2a0f0 6e 20 74 68 65 20 6e 61 6d 65 20 61 73 73 69 67  n the name assig
2a100 6e 65 64 20 74 6f 20 61 20 70 61 72 74 69 63 75  ned to a particu
2a110 6c 61 72 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e  lar column.** in
2a120 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
2a130 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 73 74  of a [SELECT] st
2a140 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 73 71  atement.  The sq
2a150 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d  lite3_column_nam
2a160 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  e().** interface
2a170 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
2a180 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72  er to a zero-ter
2a190 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74  minated UTF-8 st
2a1a0 72 69 6e 67 0a 2a 2a 20 61 6e 64 20 73 71 6c 69  ring.** and sqli
2a1b0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31  te3_column_name1
2a1c0 36 28 29 20 72 65 74 75 72 6e 73 20 61 20 70 6f  6() returns a po
2a1d0 69 6e 74 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d  inter to a zero-
2a1e0 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 55 54  terminated.** UT
2a1f0 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 54 68  F-16 string.  Th
2a200 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
2a210 72 20 69 73 20 74 68 65 20 5b 70 72 65 70 61 72  r is the [prepar
2a220 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a  ed statement].**
2a230 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
2a240 20 74 68 65 20 5b 53 45 4c 45 43 54 5d 20 73 74   the [SELECT] st
2a250 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 73 65 63  atement. The sec
2a260 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73  ond parameter is
2a270 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e   the.** column n
2a280 75 6d 62 65 72 2e 20 20 54 68 65 20 6c 65 66 74  umber.  The left
2a290 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 69 73 20 6e  most column is n
2a2a0 75 6d 62 65 72 20 30 2e 0a 2a 2a 0a 2a 2a 20 54  umber 0..**.** T
2a2b0 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69  he returned stri
2a2c0 6e 67 20 70 6f 69 6e 74 65 72 20 69 73 20 76 61  ng pointer is va
2a2d0 6c 69 64 20 75 6e 74 69 6c 20 65 69 74 68 65 72  lid until either
2a2e0 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73   the [prepared s
2a2f0 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 69 73 20  tatement].** is 
2a300 64 65 73 74 72 6f 79 65 64 20 62 79 20 5b 73 71  destroyed by [sq
2a310 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29  lite3_finalize()
2a320 5d 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 6e  ] or until the n
2a330 65 78 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73  ext call to.** s
2a340 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61  qlite3_column_na
2a350 6d 65 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f  me() or sqlite3_
2a360 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 29 20  column_name16() 
2a370 6f 6e 20 74 68 65 20 73 61 6d 65 20 63 6f 6c 75  on the same colu
2a380 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71 6c  mn..**.** If sql
2a390 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 66 61  ite3_malloc() fa
2a3a0 69 6c 73 20 64 75 72 69 6e 67 20 74 68 65 20 70  ils during the p
2a3b0 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 65 69 74  rocessing of eit
2a3c0 68 65 72 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 28  her routine.** (
2a3d0 66 6f 72 20 65 78 61 6d 70 6c 65 20 64 75 72 69  for example duri
2a3e0 6e 67 20 61 20 63 6f 6e 76 65 72 73 69 6f 6e 20  ng a conversion 
2a3f0 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 55 54  from UTF-8 to UT
2a400 46 2d 31 36 29 20 74 68 65 6e 20 61 0a 2a 2a 20  F-16) then a.** 
2a410 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 20  NULL pointer is 
2a420 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
2a430 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 72 65  The name of a re
2a440 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 74  sult column is t
2a450 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
2a460 22 41 53 22 20 63 6c 61 75 73 65 20 66 6f 72 0a  "AS" clause for.
2a470 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2c 20  ** that column, 
2a480 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 41  if there is an A
2a490 53 20 63 6c 61 75 73 65 2e 20 20 49 66 20 74 68  S clause.  If th
2a4a0 65 72 65 20 69 73 20 6e 6f 20 41 53 20 63 6c 61  ere is no AS cla
2a4b0 75 73 65 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20  use.** then the 
2a4c0 6e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75  name of the colu
2a4d0 6d 6e 20 69 73 20 75 6e 73 70 65 63 69 66 69 65  mn is unspecifie
2a4e0 64 20 61 6e 64 20 6d 61 79 20 63 68 61 6e 67 65  d and may change
2a4f0 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20 72 65 6c   from.** one rel
2a500 65 61 73 65 20 6f 66 20 53 51 4c 69 74 65 20 74  ease of SQLite t
2a510 6f 20 74 68 65 20 6e 65 78 74 2e 0a 2a 2a 0a 2a  o the next..**.*
2a520 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a  * INVARIANTS:.**
2a530 0a 2a 2a 20 7b 48 31 33 37 32 31 7d 20 41 20 73  .** {H13721} A s
2a540 75 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61  uccessful invoca
2a550 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c  tion of the [sql
2a560 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  ite3_column_name
2a570 28 53 2c 4e 29 5d 0a 2a 2a 20 20 20 20 20 20 20  (S,N)].**       
2a580 20 20 20 69 6e 74 65 72 66 61 63 65 20 72 65 74     interface ret
2a590 75 72 6e 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  urns the name of
2a5a0 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20   the Nth column 
2a5b0 28 77 68 65 72 65 20 30 20 69 73 0a 2a 2a 20 20  (where 0 is.**  
2a5c0 20 20 20 20 20 20 20 20 74 68 65 20 6c 65 66 74          the left
2a5d0 6d 6f 73 74 20 63 6f 6c 75 6d 6e 29 20 66 6f 72  most column) for
2a5e0 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
2a5f0 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  of the.**       
2a600 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 74 61     [prepared sta
2a610 74 65 6d 65 6e 74 5d 20 53 20 61 73 20 61 20 7a  tement] S as a z
2a620 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55  ero-terminated U
2a630 54 46 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a  TF-8 string..**.
2a640 2a 2a 20 7b 48 31 33 37 32 33 7d 20 41 20 73 75  ** {H13723} A su
2a650 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 74  ccessful invocat
2a660 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c 69  ion of the [sqli
2a670 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31  te3_column_name1
2a680 36 28 53 2c 4e 29 5d 0a 2a 2a 20 20 20 20 20 20  6(S,N)].**      
2a690 20 20 20 20 69 6e 74 65 72 66 61 63 65 20 72 65      interface re
2a6a0 74 75 72 6e 73 20 74 68 65 20 6e 61 6d 65 20 6f  turns the name o
2a6b0 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e  f the Nth column
2a6c0 20 28 77 68 65 72 65 20 30 20 69 73 0a 2a 2a 20   (where 0 is.** 
2a6d0 20 20 20 20 20 20 20 20 20 74 68 65 20 6c 65 66           the lef
2a6e0 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 29 20 66 6f  tmost column) fo
2a6f0 72 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  r the result set
2a700 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   of the.**      
2a710 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 74      [prepared st
2a720 61 74 65 6d 65 6e 74 5d 20 53 20 61 73 20 61 20  atement] S as a 
2a730 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20  zero-terminated 
2a740 55 54 46 2d 31 36 20 73 74 72 69 6e 67 0a 2a 2a  UTF-16 string.**
2a750 20 20 20 20 20 20 20 20 20 20 69 6e 20 74 68 65            in the
2a760 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64   native byte ord
2a770 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 32  er..**.** {H1372
2a780 34 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  4} The [sqlite3_
2a790 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 5d 20 61  column_name()] a
2a7a0 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75  nd [sqlite3_colu
2a7b0 6d 6e 5f 6e 61 6d 65 31 36 28 29 5d 0a 2a 2a 20  mn_name16()].** 
2a7c0 20 20 20 20 20 20 20 20 20 69 6e 74 65 72 66 61           interfa
2a7d0 63 65 73 20 72 65 74 75 72 6e 20 61 20 4e 55 4c  ces return a NUL
2a7e0 4c 20 70 6f 69 6e 74 65 72 20 69 66 20 74 68 65  L pointer if the
2a7f0 79 20 61 72 65 20 75 6e 61 62 6c 65 20 74 6f 0a  y are unable to.
2a800 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6c 6c 6f  **          allo
2a810 63 61 74 65 20 6d 65 6d 6f 72 79 20 74 6f 20 68  cate memory to h
2a820 6f 6c 64 20 74 68 65 69 72 20 6e 6f 72 6d 61 6c  old their normal
2a830 20 72 65 74 75 72 6e 20 73 74 72 69 6e 67 73 2e   return strings.
2a840 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 32 35 7d 20  .**.** {H13725} 
2a850 49 66 20 74 68 65 20 4e 20 70 61 72 61 6d 65 74  If the N paramet
2a860 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63  er to [sqlite3_c
2a870 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 53 2c 4e 29 5d  olumn_name(S,N)]
2a880 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   or.**          
2a890 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  [sqlite3_column_
2a8a0 6e 61 6d 65 31 36 28 53 2c 4e 29 5d 20 69 73 20  name16(S,N)] is 
2a8b0 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 68  out of range, th
2a8c0 65 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  en the.**       
2a8d0 20 20 20 69 6e 74 65 72 66 61 63 65 73 20 72 65     interfaces re
2a8e0 74 75 72 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  turn a NULL poin
2a8f0 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37  ter..**.** {H137
2a900 32 36 7d 20 54 68 65 20 73 74 72 69 6e 67 73 20  26} The strings 
2a910 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c  returned by [sql
2a920 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  ite3_column_name
2a930 28 53 2c 4e 29 5d 20 61 6e 64 0a 2a 2a 20 20 20  (S,N)] and.**   
2a940 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
2a950 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 53 2c  column_name16(S,
2a960 4e 29 5d 20 61 72 65 20 76 61 6c 69 64 20 75 6e  N)] are valid un
2a970 74 69 6c 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20  til the next.** 
2a980 20 20 20 20 20 20 20 20 20 63 61 6c 6c 20 74 6f           call to
2a990 20 65 69 74 68 65 72 20 72 6f 75 74 69 6e 65 20   either routine 
2a9a0 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 53 20  with the same S 
2a9b0 61 6e 64 20 4e 20 70 61 72 61 6d 65 74 65 72 73  and N parameters
2a9c0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20  .**          or 
2a9d0 75 6e 74 69 6c 20 5b 73 71 6c 69 74 65 33 5f 66  until [sqlite3_f
2a9e0 69 6e 61 6c 69 7a 65 28 53 29 5d 20 69 73 20 63  inalize(S)] is c
2a9f0 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  alled..**.** {H1
2aa00 33 37 32 37 7d 20 57 68 65 6e 20 61 20 72 65 73  3727} When a res
2aa10 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20  ult column of a 
2aa20 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65  [SELECT] stateme
2aa30 6e 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 20  nt contains.**  
2aa40 20 20 20 20 20 20 20 20 61 6e 20 41 53 20 63 6c          an AS cl
2aa50 61 75 73 65 2c 20 74 68 65 20 6e 61 6d 65 20 6f  ause, the name o
2aa60 66 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 69 73  f that column is
2aa70 20 74 68 65 20 69 64 65 6e 74 69 66 69 65 72 0a   the identifier.
2aa80 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f 20 74  **          to t
2aa90 68 65 20 72 69 67 68 74 20 6f 66 20 74 68 65 20  he right of the 
2aaa0 41 53 20 6b 65 79 77 6f 72 64 2e 0a 2a 2f 0a 53  AS keyword..*/.S
2aab0 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
2aac0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f  char *sqlite3_co
2aad0 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 65  lumn_name(sqlite
2aae0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b  3_stmt*, int N);
2aaf0 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
2ab00 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
2ab10 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73 71  column_name16(sq
2ab20 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
2ab30 20 4e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49   N);../*.** CAPI
2ab40 33 52 45 46 3a 20 53 6f 75 72 63 65 20 4f 66 20  3REF: Source Of 
2ab50 44 61 74 61 20 49 6e 20 41 20 51 75 65 72 79 20  Data In A Query 
2ab60 52 65 73 75 6c 74 20 7b 48 31 33 37 34 30 7d 20  Result {H13740} 
2ab70 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S10700>.**.** T
2ab80 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 70 72  hese routines pr
2ab90 6f 76 69 64 65 20 61 20 6d 65 61 6e 73 20 74 6f  ovide a means to
2aba0 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20   determine what 
2abb0 63 6f 6c 75 6d 6e 20 6f 66 20 77 68 61 74 0a 2a  column of what.*
2abc0 2a 20 74 61 62 6c 65 20 69 6e 20 77 68 69 63 68  * table in which
2abd0 20 64 61 74 61 62 61 73 65 20 61 20 72 65 73 75   database a resu
2abe0 6c 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d  lt of a [SELECT]
2abf0 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65 73   statement comes
2ac00 20 66 72 6f 6d 2e 0a 2a 2a 20 54 68 65 20 6e 61   from..** The na
2ac10 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
2ac20 73 65 20 6f 72 20 74 61 62 6c 65 20 6f 72 20 63  se or table or c
2ac30 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20 72 65 74  olumn can be ret
2ac40 75 72 6e 65 64 20 61 73 0a 2a 2a 20 65 69 74 68  urned as.** eith
2ac50 65 72 20 61 20 55 54 46 2d 38 20 6f 72 20 55 54  er a UTF-8 or UT
2ac60 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 54 68  F-16 string.  Th
2ac70 65 20 5f 64 61 74 61 62 61 73 65 5f 20 72 6f 75  e _database_ rou
2ac80 74 69 6e 65 73 20 72 65 74 75 72 6e 0a 2a 2a 20  tines return.** 
2ac90 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d  the database nam
2aca0 65 2c 20 74 68 65 20 5f 74 61 62 6c 65 5f 20 72  e, the _table_ r
2acb0 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74  outines return t
2acc0 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2c 20 61  he table name, a
2acd0 6e 64 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e  nd.** the origin
2ace0 5f 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72  _ routines retur
2acf0 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  n the column nam
2ad00 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  e..** The return
2ad10 65 64 20 73 74 72 69 6e 67 20 69 73 20 76 61 6c  ed string is val
2ad20 69 64 20 75 6e 74 69 6c 20 74 68 65 20 5b 70 72  id until the [pr
2ad30 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
2ad40 5d 20 69 73 20 64 65 73 74 72 6f 79 65 64 0a 2a  ] is destroyed.*
2ad50 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  * using [sqlite3
2ad60 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20  _finalize()] or 
2ad70 75 6e 74 69 6c 20 74 68 65 20 73 61 6d 65 20 69  until the same i
2ad80 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 72 65  nformation is re
2ad90 71 75 65 73 74 65 64 0a 2a 2a 20 61 67 61 69 6e  quested.** again
2ada0 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20   in a different 
2adb0 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  encoding..**.** 
2adc0 54 68 65 20 6e 61 6d 65 73 20 72 65 74 75 72 6e  The names return
2add0 65 64 20 61 72 65 20 74 68 65 20 6f 72 69 67 69  ed are the origi
2ade0 6e 61 6c 20 75 6e 2d 61 6c 69 61 73 65 64 20 6e  nal un-aliased n
2adf0 61 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64  ames of the.** d
2ae00 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 2c 20  atabase, table, 
2ae10 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a  and column..**.*
2ae20 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75  * The first argu
2ae30 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c  ment to the foll
2ae40 6f 77 69 6e 67 20 63 61 6c 6c 73 20 69 73 20 61  owing calls is a
2ae50 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
2ae60 6d 65 6e 74 5d 2e 0a 2a 2a 20 54 68 65 73 65 20  ment]..** These 
2ae70 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e  functions return
2ae80 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
2ae90 75 74 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d  ut the Nth colum
2aea0 6e 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a  n returned by.**
2aeb0 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20   the statement, 
2aec0 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 73  where N is the s
2aed0 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61  econd function a
2aee0 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49  rgument..**.** I
2aef0 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e  f the Nth column
2af00 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65   returned by the
2af10 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 6e   statement is an
2af20 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 0a 2a   expression or.*
2af30 2a 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 69  * subquery and i
2af40 73 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20 76  s not a column v
2af50 61 6c 75 65 2c 20 74 68 65 6e 20 61 6c 6c 20 6f  alue, then all o
2af60 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e  f these function
2af70 73 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c  s return.** NULL
2af80 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  .  These routine
2af90 20 6d 69 67 68 74 20 61 6c 73 6f 20 72 65 74 75   might also retu
2afa0 72 6e 20 4e 55 4c 4c 20 69 66 20 61 20 6d 65 6d  rn NULL if a mem
2afb0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65  ory allocation e
2afc0 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2e 20  rror.** occurs. 
2afd0 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 79   Otherwise, they
2afe0 20 72 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65   return the name
2aff0 20 6f 66 20 74 68 65 20 61 74 74 61 63 68 65 64   of the attached
2b000 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65   database, table
2b010 0a 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 74  .** and column t
2b020 68 61 74 20 71 75 65 72 79 20 72 65 73 75 6c 74  hat query result
2b030 20 63 6f 6c 75 6d 6e 20 77 61 73 20 65 78 74 72   column was extr
2b040 61 63 74 65 64 20 66 72 6f 6d 2e 0a 2a 2a 0a 2a  acted from..**.*
2b050 2a 20 41 73 20 77 69 74 68 20 61 6c 6c 20 6f 74  * As with all ot
2b060 68 65 72 20 53 51 4c 69 74 65 20 41 50 49 73 2c  her SQLite APIs,
2b070 20 74 68 6f 73 65 20 70 6f 73 74 66 69 78 65 64   those postfixed
2b080 20 77 69 74 68 20 22 31 36 22 20 72 65 74 75 72   with "16" retur
2b090 6e 0a 2a 2a 20 55 54 46 2d 31 36 20 65 6e 63 6f  n.** UTF-16 enco
2b0a0 64 65 64 20 73 74 72 69 6e 67 73 2c 20 74 68 65  ded strings, the
2b0b0 20 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 73   other functions
2b0c0 20 72 65 74 75 72 6e 20 55 54 46 2d 38 2e 20 7b   return UTF-8. {
2b0d0 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  END}.**.** These
2b0e0 20 41 50 49 73 20 61 72 65 20 6f 6e 6c 79 20 61   APIs are only a
2b0f0 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 20  vailable if the 
2b100 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70  library was comp
2b110 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a  iled with the.**
2b120 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f   [SQLITE_ENABLE_
2b130 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 5d  COLUMN_METADATA]
2b140 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 20   C-preprocessor 
2b150 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a  symbol defined..
2b160 2a 2a 0a 2a 2a 20 7b 41 31 33 37 35 31 7d 0a 2a  **.** {A13751}.*
2b170 2a 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65  * If two or more
2b180 20 74 68 72 65 61 64 73 20 63 61 6c 6c 20 6f 6e   threads call on
2b190 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65  e or more of the
2b1a0 73 65 20 72 6f 75 74 69 6e 65 73 20 61 67 61 69  se routines agai
2b1b0 6e 73 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20  nst the same.** 
2b1c0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
2b1d0 6e 74 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 61 74  nt and column at
2b1e0 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 74   the same time t
2b1f0 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20  hen the results 
2b200 61 72 65 0a 2a 2a 20 75 6e 64 65 66 69 6e 65 64  are.** undefined
2b210 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e  ..**.** INVARIAN
2b220 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 34  TS:.**.** {H1374
2b230 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  1} The [sqlite3_
2b240 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f  column_database_
2b250 6e 61 6d 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72  name(S,N)] inter
2b260 66 61 63 65 20 72 65 74 75 72 6e 73 20 65 69 74  face returns eit
2b270 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  her.**          
2b280 74 68 65 20 55 54 46 2d 38 20 7a 65 72 6f 2d 74  the UTF-8 zero-t
2b290 65 72 6d 69 6e 61 74 65 64 20 6e 61 6d 65 20 6f  erminated name o
2b2a0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
2b2b0 72 6f 6d 20 77 68 69 63 68 20 74 68 65 0a 2a 2a  rom which the.**
2b2c0 20 20 20 20 20 20 20 20 20 20 4e 74 68 20 72 65            Nth re
2b2d0 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  sult column of t
2b2e0 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  he [prepared sta
2b2f0 74 65 6d 65 6e 74 5d 20 53 20 69 73 20 65 78 74  tement] S is ext
2b300 72 61 63 74 65 64 2c 0a 2a 2a 20 20 20 20 20 20  racted,.**      
2b310 20 20 20 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74      or NULL if t
2b320 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  he Nth column of
2b330 20 53 20 69 73 20 61 20 67 65 6e 65 72 61 6c 20   S is a general 
2b340 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 20 20  expression.**   
2b350 20 20 20 20 20 20 20 6f 72 20 69 66 20 75 6e 61         or if una
2b360 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ble to allocate 
2b370 6d 65 6d 6f 72 79 20 74 6f 20 73 74 6f 72 65 20  memory to store 
2b380 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20  the name..**.** 
2b390 7b 48 31 33 37 34 32 7d 20 54 68 65 20 5b 73 71  {H13742} The [sq
2b3a0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74  lite3_column_dat
2b3b0 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 53 2c 4e  abase_name16(S,N
2b3c0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  )] interface ret
2b3d0 75 72 6e 73 20 65 69 74 68 65 72 0a 2a 2a 20 20  urns either.**  
2b3e0 20 20 20 20 20 20 20 20 74 68 65 20 55 54 46 2d          the UTF-
2b3f0 31 36 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f  16 native byte o
2b400 72 64 65 72 20 7a 65 72 6f 2d 74 65 72 6d 69 6e  rder zero-termin
2b410 61 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65  ated name of the
2b420 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20   database.**    
2b430 20 20 20 20 20 20 66 72 6f 6d 20 77 68 69 63 68        from which
2b440 20 74 68 65 20 4e 74 68 20 72 65 73 75 6c 74 20   the Nth result 
2b450 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 5b 70  column of the [p
2b460 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
2b470 74 5d 20 53 20 69 73 0a 2a 2a 20 20 20 20 20 20  t] S is.**      
2b480 20 20 20 20 65 78 74 72 61 63 74 65 64 2c 20 6f      extracted, o
2b490 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 4e 74  r NULL if the Nt
2b4a0 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 53 20 69 73  h column of S is
2b4b0 20 61 20 67 65 6e 65 72 61 6c 20 65 78 70 72 65   a general expre
2b4c0 73 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20  ssion.**        
2b4d0 20 20 6f 72 20 69 66 20 75 6e 61 62 6c 65 20 74    or if unable t
2b4e0 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72  o allocate memor
2b4f0 79 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 6e  y to store the n
2b500 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37  ame..**.** {H137
2b510 34 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  43} The [sqlite3
2b520 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  _column_table_na
2b530 6d 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61  me(S,N)] interfa
2b540 63 65 20 72 65 74 75 72 6e 73 20 65 69 74 68 65  ce returns eithe
2b550 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  r.**          th
2b560 65 20 55 54 46 2d 38 20 7a 65 72 6f 2d 74 65 72  e UTF-8 zero-ter
2b570 6d 69 6e 61 74 65 64 20 6e 61 6d 65 20 6f 66 20  minated name of 
2b580 74 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77  the table from w
2b590 68 69 63 68 20 74 68 65 0a 2a 2a 20 20 20 20 20  hich the.**     
2b5a0 20 20 20 20 20 4e 74 68 20 72 65 73 75 6c 74 20       Nth result 
2b5b0 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 5b 70  column of the [p
2b5c0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
2b5d0 74 5d 20 53 20 69 73 20 65 78 74 72 61 63 74 65  t] S is extracte
2b5e0 64 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  d,.**          o
2b5f0 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 4e 74  r NULL if the Nt
2b600 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 53 20 69 73  h column of S is
2b610 20 61 20 67 65 6e 65 72 61 6c 20 65 78 70 72 65   a general expre
2b620 73 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20  ssion.**        
2b630 20 20 6f 72 20 69 66 20 75 6e 61 62 6c 65 20 74    or if unable t
2b640 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72  o allocate memor
2b650 79 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 6e  y to store the n
2b660 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37  ame..**.** {H137
2b670 34 34 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  44} The [sqlite3
2b680 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  _column_table_na
2b690 6d 65 31 36 28 53 2c 4e 29 5d 20 69 6e 74 65 72  me16(S,N)] inter
2b6a0 66 61 63 65 20 72 65 74 75 72 6e 73 20 65 69 74  face returns eit
2b6b0 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  her.**          
2b6c0 74 68 65 20 55 54 46 2d 31 36 20 6e 61 74 69 76  the UTF-16 nativ
2b6d0 65 20 62 79 74 65 20 6f 72 64 65 72 20 7a 65 72  e byte order zer
2b6e0 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 6e 61 6d  o-terminated nam
2b6f0 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 0a 2a  e of the table.*
2b700 2a 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d 20  *          from 
2b710 77 68 69 63 68 20 74 68 65 20 4e 74 68 20 72 65  which the Nth re
2b720 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  sult column of t
2b730 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  he [prepared sta
2b740 74 65 6d 65 6e 74 5d 20 53 20 69 73 0a 2a 2a 20  tement] S is.** 
2b750 20 20 20 20 20 20 20 20 20 65 78 74 72 61 63 74           extract
2b760 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74  ed, or NULL if t
2b770 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  he Nth column of
2b780 20 53 20 69 73 20 61 20 67 65 6e 65 72 61 6c 20   S is a general 
2b790 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 20 20  expression.**   
2b7a0 20 20 20 20 20 20 20 6f 72 20 69 66 20 75 6e 61         or if una
2b7b0 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ble to allocate 
2b7c0 6d 65 6d 6f 72 79 20 74 6f 20 73 74 6f 72 65 20  memory to store 
2b7d0 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20  the name..**.** 
2b7e0 7b 48 31 33 37 34 35 7d 20 54 68 65 20 5b 73 71  {H13745} The [sq
2b7f0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69  lite3_column_ori
2b800 67 69 6e 5f 6e 61 6d 65 28 53 2c 4e 29 5d 20 69  gin_name(S,N)] i
2b810 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
2b820 20 65 69 74 68 65 72 0a 2a 2a 20 20 20 20 20 20   either.**      
2b830 20 20 20 20 74 68 65 20 55 54 46 2d 38 20 7a 65      the UTF-8 ze
2b840 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 6e 61  ro-terminated na
2b850 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
2b860 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 77 68 69 63  column from whic
2b870 68 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  h the.**        
2b880 20 20 4e 74 68 20 72 65 73 75 6c 74 20 63 6f 6c    Nth result col
2b890 75 6d 6e 20 6f 66 20 74 68 65 20 5b 70 72 65 70  umn of the [prep
2b8a0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
2b8b0 53 20 69 73 20 65 78 74 72 61 63 74 65 64 2c 0a  S is extracted,.
2b8c0 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 4e  **          or N
2b8d0 55 4c 4c 20 69 66 20 74 68 65 20 4e 74 68 20 63  ULL if the Nth c
2b8e0 6f 6c 75 6d 6e 20 6f 66 20 53 20 69 73 20 61 20  olumn of S is a 
2b8f0 67 65 6e 65 72 61 6c 20 65 78 70 72 65 73 73 69  general expressi
2b900 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  on.**          o
2b910 72 20 69 66 20 75 6e 61 62 6c 65 20 74 6f 20 61  r if unable to a
2b920 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 74  llocate memory t
2b930 6f 20 73 74 6f 72 65 20 74 68 65 20 6e 61 6d 65  o store the name
2b940 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 34 36 7d  ..**.** {H13746}
2b950 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f   The [sqlite3_co
2b960 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
2b970 31 36 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61  16(S,N)] interfa
2b980 63 65 20 72 65 74 75 72 6e 73 20 65 69 74 68 65  ce returns eithe
2b990 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  r.**          th
2b9a0 65 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20  e UTF-16 native 
2b9b0 62 79 74 65 20 6f 72 64 65 72 20 7a 65 72 6f 2d  byte order zero-
2b9c0 74 65 72 6d 69 6e 61 74 65 64 20 6e 61 6d 65 20  terminated name 
2b9d0 6f 66 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20  of the table.** 
2b9e0 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 20           column 
2b9f0 66 72 6f 6d 20 77 68 69 63 68 20 74 68 65 20 4e  from which the N
2ba00 74 68 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  th result column
2ba10 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   of the.**      
2ba20 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 74      [prepared st
2ba30 61 74 65 6d 65 6e 74 5d 20 53 20 69 73 20 65 78  atement] S is ex
2ba40 74 72 61 63 74 65 64 2c 20 6f 72 20 4e 55 4c 4c  tracted, or NULL
2ba50 20 69 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75   if the Nth colu
2ba60 6d 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  mn.**          o
2ba70 66 20 53 20 69 73 20 61 20 67 65 6e 65 72 61 6c  f S is a general
2ba80 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 69   expression or i
2ba90 66 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f  f unable to allo
2baa0 63 61 74 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20  cate memory.**  
2bab0 20 20 20 20 20 20 20 20 74 6f 20 73 74 6f 72 65          to store
2bac0 20 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a   the name..**.**
2bad0 20 7b 48 31 33 37 34 38 7d 20 54 68 65 20 72 65   {H13748} The re
2bae0 74 75 72 6e 20 76 61 6c 75 65 73 20 66 72 6f 6d  turn values from
2baf0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71  .**          [sq
2bb00 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74  lite3_column_dat
2bb10 61 62 61 73 65 5f 6e 61 6d 65 20 7c 20 63 6f 6c  abase_name | col
2bb20 75 6d 6e 20 6d 65 74 61 64 61 74 61 20 69 6e 74  umn metadata int
2bb30 65 72 66 61 63 65 73 5d 0a 2a 2a 20 20 20 20 20  erfaces].**     
2bb40 20 20 20 20 20 61 72 65 20 76 61 6c 69 64 20 66       are valid f
2bb50 6f 72 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20  or the lifetime 
2bb60 6f 66 20 74 68 65 20 5b 70 72 65 70 61 72 65 64  of the [prepared
2bb70 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 20   statement].**  
2bb80 20 20 20 20 20 20 20 20 6f 72 20 75 6e 74 69 6c          or until
2bb90 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 69 73   the encoding is
2bba0 20 63 68 61 6e 67 65 64 20 62 79 20 61 6e 6f 74   changed by anot
2bbb0 68 65 72 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20  her metadata.** 
2bbc0 20 20 20 20 20 20 20 20 20 69 6e 74 65 72 66 61           interfa
2bbd0 63 65 20 63 61 6c 6c 20 66 6f 72 20 74 68 65 20  ce call for the 
2bbe0 73 61 6d 65 20 70 72 65 70 61 72 65 64 20 73 74  same prepared st
2bbf0 61 74 65 6d 65 6e 74 20 61 6e 64 20 63 6f 6c 75  atement and colu
2bc00 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 41 53 53 55 4d 50  mn..**.** ASSUMP
2bc10 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31  TIONS:.**.** {A1
2bc20 33 37 35 31 7d 20 49 66 20 74 77 6f 20 6f 72 20  3751} If two or 
2bc30 6d 6f 72 65 20 74 68 72 65 61 64 73 20 63 61 6c  more threads cal
2bc40 6c 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a  l one or more.**
2bc50 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
2bc60 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61  e3_column_databa
2bc70 73 65 5f 6e 61 6d 65 20 7c 20 63 6f 6c 75 6d 6e  se_name | column
2bc80 20 6d 65 74 61 64 61 74 61 20 69 6e 74 65 72 66   metadata interf
2bc90 61 63 65 73 5d 0a 2a 2a 20 20 20 20 20 20 20 20  aces].**        
2bca0 20 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 5b    for the same [
2bcb0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
2bcc0 6e 74 5d 20 61 6e 64 20 72 65 73 75 6c 74 20 63  nt] and result c
2bcd0 6f 6c 75 6d 6e 0a 2a 2a 20 20 20 20 20 20 20 20  olumn.**        
2bce0 20 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69    at the same ti
2bcf0 6d 65 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  me then the resu
2bd00 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65  lts are undefine
2bd10 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  d..*/.SQLITE_API
2bd20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
2bd30 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61  ite3_column_data
2bd40 62 61 73 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65  base_name(sqlite
2bd50 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51  3_stmt*,int);.SQ
2bd60 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
2bd70 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  oid *sqlite3_col
2bd80 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d  umn_database_nam
2bd90 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  e16(sqlite3_stmt
2bda0 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41  *,int);.SQLITE_A
2bdb0 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  PI const char *s
2bdc0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61  qlite3_column_ta
2bdd0 62 6c 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33  ble_name(sqlite3
2bde0 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c  _stmt*,int);.SQL
2bdf0 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
2be00 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  id *sqlite3_colu
2be10 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28  mn_table_name16(
2be20 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
2be30 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  t);.SQLITE_API c
2be40 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
2be50 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e  e3_column_origin
2be60 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74  _name(sqlite3_st
2be70 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  mt*,int);.SQLITE
2be80 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
2be90 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
2bea0 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 28 73 71  origin_name16(sq
2beb0 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29  lite3_stmt*,int)
2bec0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
2bed0 46 3a 20 44 65 63 6c 61 72 65 64 20 44 61 74 61  F: Declared Data
2bee0 74 79 70 65 20 4f 66 20 41 20 51 75 65 72 79 20  type Of A Query 
2bef0 52 65 73 75 6c 74 20 7b 48 31 33 37 36 30 7d 20  Result {H13760} 
2bf00 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S10700>.**.** T
2bf10 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  he first paramet
2bf20 65 72 20 69 73 20 61 20 5b 70 72 65 70 61 72 65  er is a [prepare
2bf30 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a  d statement]..**
2bf40 20 49 66 20 74 68 69 73 20 73 74 61 74 65 6d 65   If this stateme
2bf50 6e 74 20 69 73 20 61 20 5b 53 45 4c 45 43 54 5d  nt is a [SELECT]
2bf60 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 74   statement and t
2bf70 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  he Nth column of
2bf80 20 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64   the.** returned
2bf90 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74   result set of t
2bfa0 68 61 74 20 5b 53 45 4c 45 43 54 5d 20 69 73 20  hat [SELECT] is 
2bfb0 61 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 28  a table column (
2bfc0 6e 6f 74 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73  not an.** expres
2bfd0 73 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 79  sion or subquery
2bfe0 29 20 74 68 65 6e 20 74 68 65 20 64 65 63 6c 61  ) then the decla
2bff0 72 65 64 20 74 79 70 65 20 6f 66 20 74 68 65 20  red type of the 
2c000 74 61 62 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20  table.** column 
2c010 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66  is returned.  If
2c020 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20   the Nth column 
2c030 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  of the result se
2c040 74 20 69 73 20 61 6e 0a 2a 2a 20 65 78 70 72 65  t is an.** expre
2c050 73 73 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72  ssion or subquer
2c060 79 2c 20 74 68 65 6e 20 61 20 4e 55 4c 4c 20 70  y, then a NULL p
2c070 6f 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e  ointer is return
2c080 65 64 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  ed..** The retur
2c090 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 61 6c  ned string is al
2c0a0 77 61 79 73 20 55 54 46 2d 38 20 65 6e 63 6f 64  ways UTF-8 encod
2c0b0 65 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20  ed. {END}.**.** 
2c0c0 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 67 69 76  For example, giv
2c0d0 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  en the database 
2c0e0 73 63 68 65 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43 52  schema:.**.** CR
2c0f0 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 63 31  EATE TABLE t1(c1
2c100 20 56 41 52 49 41 4e 54 29 3b 0a 2a 2a 0a 2a 2a   VARIANT);.**.**
2c110 20 61 6e 64 20 74 68 65 20 66 6f 6c 6c 6f 77 69   and the followi
2c120 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20  ng statement to 
2c130 62 65 20 63 6f 6d 70 69 6c 65 64 3a 0a 2a 2a 0a  be compiled:.**.
2c140 2a 2a 20 53 45 4c 45 43 54 20 63 31 20 2b 20 31  ** SELECT c1 + 1
2c150 2c 20 63 31 20 46 52 4f 4d 20 74 31 3b 0a 2a 2a  , c1 FROM t1;.**
2c160 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
2c170 20 77 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68   would return th
2c180 65 20 73 74 72 69 6e 67 20 22 56 41 52 49 41 4e  e string "VARIAN
2c190 54 22 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e  T" for the secon
2c1a0 64 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75  d result.** colu
2c1b0 6d 6e 20 28 69 3d 3d 31 29 2c 20 61 6e 64 20 61  mn (i==1), and a
2c1c0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 66 6f   NULL pointer fo
2c1d0 72 20 74 68 65 20 66 69 72 73 74 20 72 65 73 75  r the first resu
2c1e0 6c 74 20 63 6f 6c 75 6d 6e 20 28 69 3d 3d 30 29  lt column (i==0)
2c1f0 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 75  ..**.** SQLite u
2c200 73 65 73 20 64 79 6e 61 6d 69 63 20 72 75 6e 2d  ses dynamic run-
2c210 74 69 6d 65 20 74 79 70 69 6e 67 2e 20 20 53 6f  time typing.  So
2c220 20 6a 75 73 74 20 62 65 63 61 75 73 65 20 61 20   just because a 
2c230 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20 64 65 63  column.** is dec
2c240 6c 61 72 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e  lared to contain
2c250 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 79   a particular ty
2c260 70 65 20 64 6f 65 73 20 6e 6f 74 20 6d 65 61 6e  pe does not mean
2c270 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 64 61 74   that the.** dat
2c280 61 20 73 74 6f 72 65 64 20 69 6e 20 74 68 61 74  a stored in that
2c290 20 63 6f 6c 75 6d 6e 20 69 73 20 6f 66 20 74 68   column is of th
2c2a0 65 20 64 65 63 6c 61 72 65 64 20 74 79 70 65 2e  e declared type.
2c2b0 20 20 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20 73    SQLite is.** s
2c2c0 74 72 6f 6e 67 6c 79 20 74 79 70 65 64 2c 20 62  trongly typed, b
2c2d0 75 74 20 74 68 65 20 74 79 70 69 6e 67 20 69 73  ut the typing is
2c2e0 20 64 79 6e 61 6d 69 63 20 6e 6f 74 20 73 74 61   dynamic not sta
2c2f0 74 69 63 2e 20 20 54 79 70 65 0a 2a 2a 20 69 73  tic.  Type.** is
2c300 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
2c310 20 69 6e 64 69 76 69 64 75 61 6c 20 76 61 6c 75   individual valu
2c320 65 73 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 65  es, not with the
2c330 20 63 6f 6e 74 61 69 6e 65 72 73 0a 2a 2a 20 75   containers.** u
2c340 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 6f 73  sed to hold thos
2c350 65 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20  e values..**.** 
2c360 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a  INVARIANTS:.**.*
2c370 2a 20 7b 48 31 33 37 36 31 7d 20 20 41 20 73 75  * {H13761}  A su
2c380 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f  ccessful call to
2c390 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   [sqlite3_column
2c3a0 5f 64 65 63 6c 74 79 70 65 28 53 2c 4e 29 5d 20  _decltype(S,N)] 
2c3b0 72 65 74 75 72 6e 73 20 61 0a 2a 2a 20 20 20 20  returns a.**    
2c3c0 20 20 20 20 20 20 20 7a 65 72 6f 2d 74 65 72 6d         zero-term
2c3d0 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 72  inated UTF-8 str
2c3e0 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ing containing t
2c3f0 68 65 20 64 65 63 6c 61 72 65 64 20 64 61 74 61  he declared data
2c400 74 79 70 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  type.**         
2c410 20 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63    of the table c
2c420 6f 6c 75 6d 6e 20 74 68 61 74 20 61 70 70 65 61  olumn that appea
2c430 72 73 20 61 73 20 74 68 65 20 4e 74 68 20 63 6f  rs as the Nth co
2c440 6c 75 6d 6e 20 28 6e 75 6d 62 65 72 65 64 0a 2a  lumn (numbered.*
2c450 2a 20 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d  *           from
2c460 20 30 29 20 6f 66 20 74 68 65 20 72 65 73 75 6c   0) of the resul
2c470 74 20 73 65 74 20 74 6f 20 74 68 65 20 5b 70 72  t set to the [pr
2c480 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
2c490 5d 20 53 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37  ] S..**.** {H137
2c4a0 36 32 7d 20 20 41 20 73 75 63 63 65 73 73 66 75  62}  A successfu
2c4b0 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  l call to [sqlit
2c4c0 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79  e3_column_declty
2c4d0 70 65 31 36 28 53 2c 4e 29 5d 0a 2a 2a 20 20 20  pe16(S,N)].**   
2c4e0 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 73 20          returns 
2c4f0 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  a zero-terminate
2c500 64 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20  d UTF-16 native 
2c510 62 79 74 65 20 6f 72 64 65 72 20 73 74 72 69 6e  byte order strin
2c520 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 63  g.**           c
2c530 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 64 65  ontaining the de
2c540 63 6c 61 72 65 64 20 64 61 74 61 74 79 70 65 20  clared datatype 
2c550 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c  of the table col
2c560 75 6d 6e 20 74 68 61 74 20 61 70 70 65 61 72 73  umn that appears
2c570 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 73  .**           as
2c580 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20   the Nth column 
2c590 28 6e 75 6d 62 65 72 65 64 20 66 72 6f 6d 20 30  (numbered from 0
2c5a0 29 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20  ) of the result 
2c5b0 73 65 74 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20  set to the.**   
2c5c0 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 65          [prepare
2c5d0 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 2e 0a  d statement] S..
2c5e0 2a 2a 0a 2a 2a 20 7b 48 31 33 37 36 33 7d 20 20  **.** {H13763}  
2c5f0 49 66 20 4e 20 69 73 20 6c 65 73 73 20 74 68 61  If N is less tha
2c600 6e 20 30 20 6f 72 20 4e 20 69 73 20 67 72 65 61  n 0 or N is grea
2c610 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ter than or equa
2c620 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20  l to.**         
2c630 20 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20    the number of 
2c640 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 5b  columns in the [
2c650 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
2c660 6e 74 5d 20 53 2c 0a 2a 2a 20 20 20 20 20 20 20  nt] S,.**       
2c670 20 20 20 20 6f 72 20 69 66 20 74 68 65 20 4e 74      or if the Nt
2c680 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 53 20 69 73  h column of S is
2c690 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f   an expression o
2c6a0 72 20 73 75 62 71 75 65 72 79 20 72 61 74 68 65  r subquery rathe
2c6b0 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74  r.**           t
2c6c0 68 61 6e 20 61 20 74 61 62 6c 65 20 63 6f 6c 75  han a table colu
2c6d0 6d 6e 2c 20 6f 72 20 69 66 20 61 20 6d 65 6d 6f  mn, or if a memo
2c6e0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61  ry allocation fa
2c6f0 69 6c 75 72 65 0a 2a 2a 20 20 20 20 20 20 20 20  ilure.**        
2c700 20 20 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67     occurs during
2c710 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72   encoding conver
2c720 73 69 6f 6e 73 2c 20 74 68 65 6e 0a 2a 2a 20 20  sions, then.**  
2c730 20 20 20 20 20 20 20 20 20 63 61 6c 6c 73 20 74           calls t
2c740 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  o [sqlite3_colum
2c750 6e 5f 64 65 63 6c 74 79 70 65 28 53 2c 4e 29 5d  n_decltype(S,N)]
2c760 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   or.**          
2c770 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   [sqlite3_column
2c780 5f 64 65 63 6c 74 79 70 65 31 36 28 53 2c 4e 29  _decltype16(S,N)
2c790 5d 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a  ] return NULL..*
2c7a0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
2c7b0 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
2c7c0 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65  _column_decltype
2c7d0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
2c7e0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  nt);.SQLITE_API 
2c7f0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
2c800 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74  te3_column_declt
2c810 79 70 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74  ype16(sqlite3_st
2c820 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a  mt*,int);../*.**
2c830 20 43 41 50 49 33 52 45 46 3a 20 45 76 61 6c 75   CAPI3REF: Evalu
2c840 61 74 65 20 41 6e 20 53 51 4c 20 53 74 61 74 65  ate An SQL State
2c850 6d 65 6e 74 20 7b 48 31 33 32 30 30 7d 20 3c 53  ment {H13200} <S
2c860 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 66 74  10000>.**.** Aft
2c870 65 72 20 61 20 5b 70 72 65 70 61 72 65 64 20 73  er a [prepared s
2c880 74 61 74 65 6d 65 6e 74 5d 20 68 61 73 20 62 65  tatement] has be
2c890 65 6e 20 70 72 65 70 61 72 65 64 20 75 73 69 6e  en prepared usin
2c8a0 67 20 65 69 74 68 65 72 0a 2a 2a 20 5b 73 71 6c  g either.** [sql
2c8b0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
2c8c0 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70  )] or [sqlite3_p
2c8d0 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20 6f  repare16_v2()] o
2c8e0 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 65 67  r one of the leg
2c8f0 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  acy.** interface
2c900 73 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  s [sqlite3_prepa
2c910 72 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65  re()] or [sqlite
2c920 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 2c 20  3_prepare16()], 
2c930 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  this function.**
2c940 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20   must be called 
2c950 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69 6d 65  one or more time
2c960 73 20 74 6f 20 65 76 61 6c 75 61 74 65 20 74 68  s to evaluate th
2c970 65 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a  e statement..**.
2c980 2a 2a 20 54 68 65 20 64 65 74 61 69 6c 73 20 6f  ** The details o
2c990 66 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f  f the behavior o
2c9a0 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74  f the sqlite3_st
2c9b0 65 70 28 29 20 69 6e 74 65 72 66 61 63 65 20 64  ep() interface d
2c9c0 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 77 68 65 74  epend.** on whet
2c9d0 68 65 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e  her the statemen
2c9e0 74 20 77 61 73 20 70 72 65 70 61 72 65 64 20 75  t was prepared u
2c9f0 73 69 6e 67 20 74 68 65 20 6e 65 77 65 72 20 22  sing the newer "
2ca00 76 32 22 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a  v2" interface.**
2ca10 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
2ca20 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c  e_v2()] and [sql
2ca30 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76  ite3_prepare16_v
2ca40 32 28 29 5d 20 6f 72 20 74 68 65 20 6f 6c 64 65  2()] or the olde
2ca50 72 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65  r legacy.** inte
2ca60 72 66 61 63 65 20 5b 73 71 6c 69 74 65 33 5f 70  rface [sqlite3_p
2ca70 72 65 70 61 72 65 28 29 5d 20 61 6e 64 20 5b 73  repare()] and [s
2ca80 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
2ca90 28 29 5d 2e 20 20 54 68 65 20 75 73 65 20 6f 66  ()].  The use of
2caa0 20 74 68 65 0a 2a 2a 20 6e 65 77 20 22 76 32 22   the.** new "v2"
2cab0 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 72 65   interface is re
2cac0 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 6e 65  commended for ne
2cad0 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 62  w applications b
2cae0 75 74 20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a  ut the legacy.**
2caf0 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 20   interface will 
2cb00 63 6f 6e 74 69 6e 75 65 20 74 6f 20 62 65 20 73  continue to be s
2cb10 75 70 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  upported..**.** 
2cb20 49 6e 20 74 68 65 20 6c 65 67 61 63 79 20 69 6e  In the legacy in
2cb30 74 65 72 66 61 63 65 2c 20 74 68 65 20 72 65 74  terface, the ret
2cb40 75 72 6e 20 76 61 6c 75 65 20 77 69 6c 6c 20 62  urn value will b
2cb50 65 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54 45  e either [SQLITE
2cb60 5f 42 55 53 59 5d 2c 0a 2a 2a 20 5b 53 51 4c 49  _BUSY],.** [SQLI
2cb70 54 45 5f 44 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54  TE_DONE], [SQLIT
2cb80 45 5f 52 4f 57 5d 2c 20 5b 53 51 4c 49 54 45 5f  E_ROW], [SQLITE_
2cb90 45 52 52 4f 52 5d 2c 20 6f 72 20 5b 53 51 4c 49  ERROR], or [SQLI
2cba0 54 45 5f 4d 49 53 55 53 45 5d 2e 0a 2a 2a 20 57  TE_MISUSE]..** W
2cbb0 69 74 68 20 74 68 65 20 22 76 32 22 20 69 6e 74  ith the "v2" int
2cbc0 65 72 66 61 63 65 2c 20 61 6e 79 20 6f 66 20 74  erface, any of t
2cbd0 68 65 20 6f 74 68 65 72 20 5b 72 65 73 75 6c 74  he other [result
2cbe0 20 63 6f 64 65 73 5d 20 6f 72 0a 2a 2a 20 5b 65   codes] or.** [e
2cbf0 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63  xtended result c
2cc00 6f 64 65 73 5d 20 6d 69 67 68 74 20 62 65 20 72  odes] might be r
2cc10 65 74 75 72 6e 65 64 20 61 73 20 77 65 6c 6c 2e  eturned as well.
2cc20 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 42  .**.** [SQLITE_B
2cc30 55 53 59 5d 20 6d 65 61 6e 73 20 74 68 61 74 20  USY] means that 
2cc40 74 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 67  the database eng
2cc50 69 6e 65 20 77 61 73 20 75 6e 61 62 6c 65 20 74  ine was unable t
2cc60 6f 20 61 63 71 75 69 72 65 20 74 68 65 0a 2a 2a  o acquire the.**
2cc70 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 73 20   database locks 
2cc80 69 74 20 6e 65 65 64 73 20 74 6f 20 64 6f 20 69  it needs to do i
2cc90 74 73 20 6a 6f 62 2e 20 20 49 66 20 74 68 65 20  ts job.  If the 
2cca0 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 20 5b  statement is a [
2ccb0 43 4f 4d 4d 49 54 5d 0a 2a 2a 20 6f 72 20 6f 63  COMMIT].** or oc
2ccc0 63 75 72 73 20 6f 75 74 73 69 64 65 20 6f 66 20  curs outside of 
2ccd0 61 6e 20 65 78 70 6c 69 63 69 74 20 74 72 61 6e  an explicit tran
2cce0 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 79 6f  saction, then yo
2ccf0 75 20 63 61 6e 20 72 65 74 72 79 20 74 68 65 0a  u can retry the.
2cd00 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49  ** statement.  I
2cd10 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  f the statement 
2cd20 69 73 20 6e 6f 74 20 61 20 5b 43 4f 4d 4d 49 54  is not a [COMMIT
2cd30 5d 20 61 6e 64 20 6f 63 63 75 72 73 20 77 69 74  ] and occurs wit
2cd40 68 69 6e 20 61 0a 2a 2a 20 65 78 70 6c 69 63 69  hin a.** explici
2cd50 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 68  t transaction th
2cd60 65 6e 20 79 6f 75 20 73 68 6f 75 6c 64 20 72 6f  en you should ro
2cd70 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73  llback the trans
2cd80 61 63 74 69 6f 6e 20 62 65 66 6f 72 65 0a 2a 2a  action before.**
2cd90 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 0a 2a 2a 0a   continuing..**.
2cda0 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d  ** [SQLITE_DONE]
2cdb0 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
2cdc0 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 66 69  statement has fi
2cdd0 6e 69 73 68 65 64 20 65 78 65 63 75 74 69 6e 67  nished executing
2cde0 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79  .** successfully
2cdf0 2e 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  .  sqlite3_step(
2ce00 29 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ) should not be 
2ce10 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 6f 6e 20  called again on 
2ce20 74 68 69 73 20 76 69 72 74 75 61 6c 0a 2a 2a 20  this virtual.** 
2ce30 6d 61 63 68 69 6e 65 20 77 69 74 68 6f 75 74 20  machine without 
2ce40 66 69 72 73 74 20 63 61 6c 6c 69 6e 67 20 5b 73  first calling [s
2ce50 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20  qlite3_reset()] 
2ce60 74 6f 20 72 65 73 65 74 20 74 68 65 20 76 69 72  to reset the vir
2ce70 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 20  tual.** machine 
2ce80 62 61 63 6b 20 74 6f 20 69 74 73 20 69 6e 69 74  back to its init
2ce90 69 61 6c 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a  ial state..**.**
2cea0 20 49 66 20 74 68 65 20 53 51 4c 20 73 74 61 74   If the SQL stat
2ceb0 65 6d 65 6e 74 20 62 65 69 6e 67 20 65 78 65 63  ement being exec
2cec0 75 74 65 64 20 72 65 74 75 72 6e 73 20 61 6e 79  uted returns any
2ced0 20 64 61 74 61 2c 20 74 68 65 6e 20 5b 53 51 4c   data, then [SQL
2cee0 49 54 45 5f 52 4f 57 5d 0a 2a 2a 20 69 73 20 72  ITE_ROW].** is r
2cef0 65 74 75 72 6e 65 64 20 65 61 63 68 20 74 69 6d  eturned each tim
2cf00 65 20 61 20 6e 65 77 20 72 6f 77 20 6f 66 20 64  e a new row of d
2cf10 61 74 61 20 69 73 20 72 65 61 64 79 20 66 6f 72  ata is ready for
2cf20 20 70 72 6f 63 65 73 73 69 6e 67 20 62 79 20 74   processing by t
2cf30 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 2e 20 54 68  he.** caller. Th
2cf40 65 20 76 61 6c 75 65 73 20 6d 61 79 20 62 65 20  e values may be 
2cf50 61 63 63 65 73 73 65 64 20 75 73 69 6e 67 20 74  accessed using t
2cf60 68 65 20 5b 63 6f 6c 75 6d 6e 20 61 63 63 65 73  he [column acces
2cf70 73 20 66 75 6e 63 74 69 6f 6e 73 5d 2e 0a 2a 2a  s functions]..**
2cf80 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20   sqlite3_step() 
2cf90 69 73 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 20  is called again 
2cfa0 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 20  to retrieve the 
2cfb0 6e 65 78 74 20 72 6f 77 20 6f 66 20 64 61 74 61  next row of data
2cfc0 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  ..**.** [SQLITE_
2cfd0 45 52 52 4f 52 5d 20 6d 65 61 6e 73 20 74 68 61  ERROR] means tha
2cfe0 74 20 61 20 72 75 6e 2d 74 69 6d 65 20 65 72 72  t a run-time err
2cff0 6f 72 20 28 73 75 63 68 20 61 73 20 61 20 63 6f  or (such as a co
2d000 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 76 69 6f 6c  nstraint.** viol
2d010 61 74 69 6f 6e 29 20 68 61 73 20 6f 63 63 75 72  ation) has occur
2d020 72 65 64 2e 20 20 73 71 6c 69 74 65 33 5f 73 74  red.  sqlite3_st
2d030 65 70 28 29 20 73 68 6f 75 6c 64 20 6e 6f 74 20  ep() should not 
2d040 62 65 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 20  be called again 
2d050 6f 6e 0a 2a 2a 20 74 68 65 20 56 4d 2e 20 4d 6f  on.** the VM. Mo
2d060 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6d  re information m
2d070 61 79 20 62 65 20 66 6f 75 6e 64 20 62 79 20 63  ay be found by c
2d080 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f  alling [sqlite3_
2d090 65 72 72 6d 73 67 28 29 5d 2e 0a 2a 2a 20 57 69  errmsg()]..** Wi
2d0a0 74 68 20 74 68 65 20 6c 65 67 61 63 79 20 69 6e  th the legacy in
2d0b0 74 65 72 66 61 63 65 2c 20 61 20 6d 6f 72 65 20  terface, a more 
2d0c0 73 70 65 63 69 66 69 63 20 65 72 72 6f 72 20 63  specific error c
2d0d0 6f 64 65 20 28 66 6f 72 20 65 78 61 6d 70 6c 65  ode (for example
2d0e0 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54  ,.** [SQLITE_INT
2d0f0 45 52 52 55 50 54 5d 2c 20 5b 53 51 4c 49 54 45  ERRUPT], [SQLITE
2d100 5f 53 43 48 45 4d 41 5d 2c 20 5b 53 51 4c 49 54  _SCHEMA], [SQLIT
2d110 45 5f 43 4f 52 52 55 50 54 5d 2c 20 61 6e 64 20  E_CORRUPT], and 
2d120 73 6f 20 66 6f 72 74 68 29 0a 2a 2a 20 63 61 6e  so forth).** can
2d130 20 62 65 20 6f 62 74 61 69 6e 65 64 20 62 79 20   be obtained by 
2d140 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33  calling [sqlite3
2d150 5f 72 65 73 65 74 28 29 5d 20 6f 6e 20 74 68 65  _reset()] on the
2d160 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74  .** [prepared st
2d170 61 74 65 6d 65 6e 74 5d 2e 20 20 49 6e 20 74 68  atement].  In th
2d180 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65  e "v2" interface
2d190 2c 0a 2a 2a 20 74 68 65 20 6d 6f 72 65 20 73 70  ,.** the more sp
2d1a0 65 63 69 66 69 63 20 65 72 72 6f 72 20 63 6f 64  ecific error cod
2d1b0 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 64 69  e is returned di
2d1c0 72 65 63 74 6c 79 20 62 79 20 73 71 6c 69 74 65  rectly by sqlite
2d1d0 33 5f 73 74 65 70 28 29 2e 0a 2a 2a 0a 2a 2a 20  3_step()..**.** 
2d1e0 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 20  [SQLITE_MISUSE] 
2d1f0 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 74  means that the t
2d200 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20  his routine was 
2d210 63 61 6c 6c 65 64 20 69 6e 61 70 70 72 6f 70 72  called inappropr
2d220 69 61 74 65 6c 79 2e 0a 2a 2a 20 50 65 72 68 61  iately..** Perha
2d230 70 73 20 69 74 20 77 61 73 20 63 61 6c 6c 65 64  ps it was called
2d240 20 6f 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20   on a [prepared 
2d250 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20  statement] that 
2d260 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62  has.** already b
2d270 65 65 6e 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e  een [sqlite3_fin
2d280 61 6c 69 7a 65 20 7c 20 66 69 6e 61 6c 69 7a 65  alize | finalize
2d290 64 5d 20 6f 72 20 6f 6e 20 6f 6e 65 20 74 68 61  d] or on one tha
2d2a0 74 20 68 61 64 0a 2a 2a 20 70 72 65 76 69 6f 75  t had.** previou
2d2b0 73 6c 79 20 72 65 74 75 72 6e 65 64 20 5b 53 51  sly returned [SQ
2d2c0 4c 49 54 45 5f 45 52 52 4f 52 5d 20 6f 72 20 5b  LITE_ERROR] or [
2d2d0 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2e 20 20 4f  SQLITE_DONE].  O
2d2e0 72 20 69 74 20 63 6f 75 6c 64 0a 2a 2a 20 62 65  r it could.** be
2d2f0 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20 74   the case that t
2d300 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
2d310 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 62   connection is b
2d320 65 69 6e 67 20 75 73 65 64 20 62 79 20 74 77 6f  eing used by two
2d330 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20 74 68 72 65   or.** more thre
2d340 61 64 73 20 61 74 20 74 68 65 20 73 61 6d 65 20  ads at the same 
2d350 6d 6f 6d 65 6e 74 20 69 6e 20 74 69 6d 65 2e 0a  moment in time..
2d360 2a 2a 0a 2a 2a 20 3c 62 3e 47 6f 6f 66 79 20 49  **.** <b>Goofy I
2d370 6e 74 65 72 66 61 63 65 20 41 6c 65 72 74 3a 3c  nterface Alert:<
2d380 2f 62 3e 20 49 6e 20 74 68 65 20 6c 65 67 61 63  /b> In the legac
2d390 79 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65  y interface, the
2d3a0 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0a   sqlite3_step().
2d3b0 2a 2a 20 41 50 49 20 61 6c 77 61 79 73 20 72 65  ** API always re
2d3c0 74 75 72 6e 73 20 61 20 67 65 6e 65 72 69 63 20  turns a generic 
2d3d0 65 72 72 6f 72 20 63 6f 64 65 2c 20 5b 53 51 4c  error code, [SQL
2d3e0 49 54 45 5f 45 52 52 4f 52 5d 2c 20 66 6f 6c 6c  ITE_ERROR], foll
2d3f0 6f 77 69 6e 67 20 61 6e 79 0a 2a 2a 20 65 72 72  owing any.** err
2d400 6f 72 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53  or other than [S
2d410 51 4c 49 54 45 5f 42 55 53 59 5d 20 61 6e 64 20  QLITE_BUSY] and 
2d420 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 2e  [SQLITE_MISUSE].
2d430 20 20 59 6f 75 20 6d 75 73 74 20 63 61 6c 6c 0a    You must call.
2d440 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65  ** [sqlite3_rese
2d450 74 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  t()] or [sqlite3
2d460 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 69 6e 20  _finalize()] in 
2d470 6f 72 64 65 72 20 74 6f 20 66 69 6e 64 20 6f 6e  order to find on
2d480 65 20 6f 66 20 74 68 65 0a 2a 2a 20 73 70 65 63  e of the.** spec
2d490 69 66 69 63 20 5b 65 72 72 6f 72 20 63 6f 64 65  ific [error code
2d4a0 73 5d 20 74 68 61 74 20 62 65 74 74 65 72 20 64  s] that better d
2d4b0 65 73 63 72 69 62 65 73 20 74 68 65 20 65 72 72  escribes the err
2d4c0 6f 72 2e 0a 2a 2a 20 57 65 20 61 64 6d 69 74 20  or..** We admit 
2d4d0 74 68 61 74 20 74 68 69 73 20 69 73 20 61 20 67  that this is a g
2d4e0 6f 6f 66 79 20 64 65 73 69 67 6e 2e 20 20 54 68  oofy design.  Th
2d4f0 65 20 70 72 6f 62 6c 65 6d 20 68 61 73 20 62 65  e problem has be
2d500 65 6e 20 66 69 78 65 64 0a 2a 2a 20 77 69 74 68  en fixed.** with
2d510 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66   the "v2" interf
2d520 61 63 65 2e 20 20 49 66 20 79 6f 75 20 70 72 65  ace.  If you pre
2d530 70 61 72 65 20 61 6c 6c 20 6f 66 20 79 6f 75 72  pare all of your
2d540 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a   SQL statements.
2d550 2a 2a 20 75 73 69 6e 67 20 65 69 74 68 65 72 20  ** using either 
2d560 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
2d570 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74  _v2()] or [sqlit
2d580 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28  e3_prepare16_v2(
2d590 29 5d 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f 66  )] instead.** of
2d5a0 20 74 68 65 20 6c 65 67 61 63 79 20 5b 73 71 6c   the legacy [sql
2d5b0 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20  ite3_prepare()] 
2d5c0 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  and [sqlite3_pre
2d5d0 70 61 72 65 31 36 28 29 5d 20 69 6e 74 65 72 66  pare16()] interf
2d5e0 61 63 65 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68  aces,.** then th
2d5f0 65 20 6d 6f 72 65 20 73 70 65 63 69 66 69 63 20  e more specific 
2d600 5b 65 72 72 6f 72 20 63 6f 64 65 73 5d 20 61 72  [error codes] ar
2d610 65 20 72 65 74 75 72 6e 65 64 20 64 69 72 65 63  e returned direc
2d620 74 6c 79 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65  tly.** by sqlite
2d630 33 5f 73 74 65 70 28 29 2e 20 20 54 68 65 20 75  3_step().  The u
2d640 73 65 20 6f 66 20 74 68 65 20 22 76 32 22 20 69  se of the "v2" i
2d650 6e 74 65 72 66 61 63 65 20 69 73 20 72 65 63 6f  nterface is reco
2d660 6d 6d 65 6e 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  mmended..**.** I
2d670 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a  NVARIANTS:.**.**
2d680 20 7b 48 31 33 32 30 32 7d 20 20 49 66 20 74 68   {H13202}  If th
2d690 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  e [prepared stat
2d6a0 65 6d 65 6e 74 5d 20 53 20 69 73 20 72 65 61 64  ement] S is read
2d6b0 79 20 74 6f 20 62 65 20 72 75 6e 2c 20 74 68 65  y to be run, the
2d6c0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b  n.**           [
2d6d0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 5d  sqlite3_step(S)]
2d6e0 20 61 64 76 61 6e 63 65 73 20 74 68 61 74 20 70   advances that p
2d6f0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
2d700 74 20 75 6e 74 69 6c 0a 2a 2a 20 20 20 20 20 20  t until.**      
2d710 20 20 20 20 20 63 6f 6d 70 6c 65 74 69 6f 6e 20       completion 
2d720 6f 72 20 75 6e 74 69 6c 20 69 74 20 69 73 20 72  or until it is r
2d730 65 61 64 79 20 74 6f 20 72 65 74 75 72 6e 20 61  eady to return a
2d740 6e 6f 74 68 65 72 20 72 6f 77 20 6f 66 20 74 68  nother row of th
2d750 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 72  e.**           r
2d760 65 73 75 6c 74 20 73 65 74 2c 20 6f 72 20 75 6e  esult set, or un
2d770 74 69 6c 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f  til an [sqlite3_
2d780 69 6e 74 65 72 72 75 70 74 20 7c 20 69 6e 74 65  interrupt | inte
2d790 72 72 75 70 74 5d 0a 2a 2a 20 20 20 20 20 20 20  rrupt].**       
2d7a0 20 20 20 20 6f 72 20 61 20 72 75 6e 2d 74 69 6d      or a run-tim
2d7b0 65 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a  e error occurs..
2d7c0 2a 2a 0a 2a 2a 20 7b 48 31 35 33 30 34 7d 20 20  **.** {H15304}  
2d7d0 57 68 65 6e 20 61 20 63 61 6c 6c 20 74 6f 20 5b  When a call to [
2d7e0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 5d  sqlite3_step(S)]
2d7f0 20 63 61 75 73 65 73 20 74 68 65 20 5b 70 72 65   causes the [pre
2d800 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
2d810 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 53 20  .**           S 
2d820 74 6f 20 72 75 6e 20 74 6f 20 63 6f 6d 70 6c 65  to run to comple
2d830 74 69 6f 6e 2c 20 74 68 65 20 66 75 6e 63 74 69  tion, the functi
2d840 6f 6e 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49  on returns [SQLI
2d850 54 45 5f 44 4f 4e 45 5d 2e 0a 2a 2a 0a 2a 2a 20  TE_DONE]..**.** 
2d860 7b 48 31 35 33 30 36 7d 20 20 57 68 65 6e 20 61  {H15306}  When a
2d870 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
2d880 33 5f 73 74 65 70 28 53 29 5d 20 73 74 6f 70 73  3_step(S)] stops
2d890 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20 72   because it is r
2d8a0 65 61 64 79 20 74 6f 0a 2a 2a 20 20 20 20 20 20  eady to.**      
2d8b0 20 20 20 20 20 72 65 74 75 72 6e 20 61 6e 6f 74       return anot
2d8c0 68 65 72 20 72 6f 77 20 6f 66 20 74 68 65 20 72  her row of the r
2d8d0 65 73 75 6c 74 20 73 65 74 2c 20 69 74 20 72 65  esult set, it re
2d8e0 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 52 4f  turns [SQLITE_RO
2d8f0 57 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 33 30  W]..**.** {H1530
2d900 38 7d 20 20 49 66 20 61 20 63 61 6c 6c 20 74 6f  8}  If a call to
2d910 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53   [sqlite3_step(S
2d920 29 5d 20 65 6e 63 6f 75 6e 74 65 72 73 20 61 6e  )] encounters an
2d930 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b 73  .**           [s
2d940 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
2d950 20 7c 20 69 6e 74 65 72 72 75 70 74 5d 20 6f 72   | interrupt] or
2d960 20 61 20 72 75 6e 2d 74 69 6d 65 20 65 72 72 6f   a run-time erro
2d970 72 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  r,.**           
2d980 69 74 20 72 65 74 75 72 6e 73 20 61 6e 20 61 70  it returns an ap
2d990 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20  propriate error 
2d9a0 63 6f 64 65 20 74 68 61 74 20 69 73 20 6e 6f 74  code that is not
2d9b0 20 6f 6e 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20   one of.**      
2d9c0 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d       [SQLITE_OK]
2d9d0 2c 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 2c 20  , [SQLITE_ROW], 
2d9e0 6f 72 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d  or [SQLITE_DONE]
2d9f0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 33 31 30 7d  ..**.** {H15310}
2da00 20 20 49 66 20 61 6e 20 5b 73 71 6c 69 74 65 33    If an [sqlite3
2da10 5f 69 6e 74 65 72 72 75 70 74 20 7c 20 69 6e 74  _interrupt | int
2da20 65 72 72 75 70 74 5d 20 6f 72 20 61 20 72 75 6e  errupt] or a run
2da30 2d 74 69 6d 65 20 65 72 72 6f 72 0a 2a 2a 20 20  -time error.**  
2da40 20 20 20 20 20 20 20 20 20 6f 63 63 75 72 73 20           occurs 
2da50 64 75 72 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f  during a call to
2da60 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53   [sqlite3_step(S
2da70 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  )].**           
2da80 66 6f 72 20 61 20 5b 70 72 65 70 61 72 65 64 20  for a [prepared 
2da90 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 63 72 65  statement] S cre
2daa0 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 20 20  ated using.**   
2dab0 20 20 20 20 20 20 20 20 6c 65 67 61 63 79 20 69          legacy i
2dac0 6e 74 65 72 66 61 63 65 73 20 5b 73 71 6c 69 74  nterfaces [sqlit
2dad0 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f 72  e3_prepare()] or
2dae0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b 73  .**           [s
2daf0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
2db00 28 29 5d 2c 20 74 68 65 6e 20 74 68 65 20 66 75  ()], then the fu
2db10 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 65  nction returns e
2db20 69 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20  ither.**        
2db30 20 20 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52     [SQLITE_ERROR
2db40 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d  ], [SQLITE_BUSY]
2db50 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4d 49 53  , or [SQLITE_MIS
2db60 55 53 45 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  USE]..*/.SQLITE_
2db70 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2db80 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73 74 6d  step(sqlite3_stm
2db90 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  t*);../*.** CAPI
2dba0 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 6f 66 20  3REF: Number of 
2dbb0 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 72 65 73  columns in a res
2dbc0 75 6c 74 20 73 65 74 20 7b 48 31 33 37 37 30 7d  ult set {H13770}
2dbd0 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S10700>.**.** 
2dbe0 52 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  Returns the numb
2dbf0 65 72 20 6f 66 20 76 61 6c 75 65 73 20 69 6e 20  er of values in 
2dc00 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20  the current row 
2dc10 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  of the result se
2dc20 74 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  t..**.** INVARIA
2dc30 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37  NTS:.**.** {H137
2dc40 37 31 7d 20 20 41 66 74 65 72 20 61 20 63 61 6c  71}  After a cal
2dc50 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74  l to [sqlite3_st
2dc60 65 70 28 53 29 5d 20 74 68 61 74 20 72 65 74 75  ep(S)] that retu
2dc70 72 6e 73 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d  rns [SQLITE_ROW]
2dc80 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74  ,.**           t
2dc90 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 61 74 61  he [sqlite3_data
2dca0 5f 63 6f 75 6e 74 28 53 29 5d 20 72 6f 75 74 69  _count(S)] routi
2dcb0 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 74  ne will return t
2dcc0 68 65 20 73 61 6d 65 20 76 61 6c 75 65 0a 2a 2a  he same value.**
2dcd0 20 20 20 20 20 20 20 20 20 20 20 61 73 20 74 68             as th
2dce0 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  e [sqlite3_colum
2dcf0 6e 5f 63 6f 75 6e 74 28 53 29 5d 20 66 75 6e 63  n_count(S)] func
2dd00 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33  tion..**.** {H13
2dd10 37 37 32 7d 20 20 41 66 74 65 72 20 5b 73 71 6c  772}  After [sql
2dd20 69 74 65 33 5f 73 74 65 70 28 53 29 5d 20 68 61  ite3_step(S)] ha
2dd30 73 20 72 65 74 75 72 6e 65 64 20 61 6e 79 20 76  s returned any v
2dd40 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61 6e 0a  alue other than.
2dd50 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b 53 51  **           [SQ
2dd60 4c 49 54 45 5f 52 4f 57 5d 20 6f 72 20 62 65 66  LITE_ROW] or bef
2dd70 6f 72 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  ore [sqlite3_ste
2dd80 70 28 53 29 5d 20 68 61 73 20 62 65 65 6e 20 63  p(S)] has been c
2dd90 61 6c 6c 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20  alled on the.** 
2dda0 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61            [prepa
2ddb0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 66  red statement] f
2ddc0 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 69 6d  or the first tim
2ddd0 65 20 73 69 6e 63 65 20 69 74 20 77 61 73 0a 2a  e since it was.*
2dde0 2a 20 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  *           [sql
2ddf0 69 74 65 33 5f 70 72 65 70 61 72 65 20 7c 20 70  ite3_prepare | p
2de00 72 65 70 61 72 65 64 5d 20 6f 72 20 5b 73 71 6c  repared] or [sql
2de10 69 74 65 33 5f 72 65 73 65 74 20 7c 20 72 65 73  ite3_reset | res
2de20 65 74 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20  et],.**         
2de30 20 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 64    the [sqlite3_d
2de40 61 74 61 5f 63 6f 75 6e 74 28 53 29 5d 20 72 6f  ata_count(S)] ro
2de50 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 7a 65  utine returns ze
2de60 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ro..*/.SQLITE_AP
2de70 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 61  I int sqlite3_da
2de80 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33  ta_count(sqlite3
2de90 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a  _stmt *pStmt);..
2dea0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
2deb0 46 75 6e 64 61 6d 65 6e 74 61 6c 20 44 61 74 61  Fundamental Data
2dec0 74 79 70 65 73 20 7b 48 31 30 32 36 35 7d 20 3c  types {H10265} <
2ded0 53 31 30 31 31 30 3e 3c 53 31 30 31 32 30 3e 0a  S10110><S10120>.
2dee0 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 53 51 4c  ** KEYWORDS: SQL
2def0 49 54 45 5f 54 45 58 54 0a 2a 2a 0a 2a 2a 20 7b  ITE_TEXT.**.** {
2df00 48 31 30 32 36 36 7d 20 45 76 65 72 79 20 76 61  H10266} Every va
2df10 6c 75 65 20 69 6e 20 53 51 4c 69 74 65 20 68 61  lue in SQLite ha
2df20 73 20 6f 6e 65 20 6f 66 20 66 69 76 65 20 66 75  s one of five fu
2df30 6e 64 61 6d 65 6e 74 61 6c 20 64 61 74 61 74 79  ndamental dataty
2df40 70 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a  pes:.**.** <ul>.
2df50 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20 73  ** <li> 64-bit s
2df60 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a  igned integer.**
2df70 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20 49 45 45   <li> 64-bit IEE
2df80 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  E floating point
2df90 20 6e 75 6d 62 65 72 0a 2a 2a 20 3c 6c 69 3e 20   number.** <li> 
2dfa0 73 74 72 69 6e 67 0a 2a 2a 20 3c 6c 69 3e 20 42  string.** <li> B
2dfb0 4c 4f 42 0a 2a 2a 20 3c 6c 69 3e 20 4e 55 4c 4c  LOB.** <li> NULL
2dfc0 0a 2a 2a 20 3c 2f 75 6c 3e 20 7b 45 4e 44 7d 0a  .** </ul> {END}.
2dfd0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73  **.** These cons
2dfe0 74 61 6e 74 73 20 61 72 65 20 63 6f 64 65 73 20  tants are codes 
2dff0 66 6f 72 20 65 61 63 68 20 6f 66 20 74 68 6f 73  for each of thos
2e000 65 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e  e types..**.** N
2e010 6f 74 65 20 74 68 61 74 20 74 68 65 20 53 51 4c  ote that the SQL
2e020 49 54 45 5f 54 45 58 54 20 63 6f 6e 73 74 61 6e  ITE_TEXT constan
2e030 74 20 77 61 73 20 61 6c 73 6f 20 75 73 65 64 20  t was also used 
2e040 69 6e 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f  in SQLite versio
2e050 6e 20 32 0a 2a 2a 20 66 6f 72 20 61 20 63 6f 6d  n 2.** for a com
2e060 70 6c 65 74 65 6c 79 20 64 69 66 66 65 72 65 6e  pletely differen
2e070 74 20 6d 65 61 6e 69 6e 67 2e 20 20 53 6f 66 74  t meaning.  Soft
2e080 77 61 72 65 20 74 68 61 74 20 6c 69 6e 6b 73 20  ware that links 
2e090 61 67 61 69 6e 73 74 20 62 6f 74 68 0a 2a 2a 20  against both.** 
2e0a0 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 32  SQLite version 2
2e0b0 20 61 6e 64 20 53 51 4c 69 74 65 20 76 65 72 73   and SQLite vers
2e0c0 69 6f 6e 20 33 20 73 68 6f 75 6c 64 20 75 73 65  ion 3 should use
2e0d0 20 53 51 4c 49 54 45 33 5f 54 45 58 54 2c 20 6e   SQLITE3_TEXT, n
2e0e0 6f 74 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 45 58  ot.** SQLITE_TEX
2e0f0 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  T..*/.#define SQ
2e100 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 20 31 0a  LITE_INTEGER  1.
2e110 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46  #define SQLITE_F
2e120 4c 4f 41 54 20 20 20 20 32 0a 23 64 65 66 69 6e  LOAT    2.#defin
2e130 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 20 20 20  e SQLITE_BLOB   
2e140 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    4.#define SQLI
2e150 54 45 5f 4e 55 4c 4c 20 20 20 20 20 35 0a 23 69  TE_NULL     5.#i
2e160 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 58 54  fdef SQLITE_TEXT
2e170 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f  .# undef SQLITE_
2e180 54 45 58 54 0a 23 65 6c 73 65 0a 23 20 64 65 66  TEXT.#else.# def
2e190 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 58 54 20  ine SQLITE_TEXT 
2e1a0 20 20 20 20 33 0a 23 65 6e 64 69 66 0a 23 64 65      3.#endif.#de
2e1b0 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 54 45 58  fine SQLITE3_TEX
2e1c0 54 20 20 20 20 20 33 0a 0a 2f 2a 0a 2a 2a 20 43  T     3../*.** C
2e1d0 41 50 49 33 52 45 46 3a 20 52 65 73 75 6c 74 20  API3REF: Result 
2e1e0 56 61 6c 75 65 73 20 46 72 6f 6d 20 41 20 51 75  Values From A Qu
2e1f0 65 72 79 20 7b 48 31 33 38 30 30 7d 20 3c 53 31  ery {H13800} <S1
2e200 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44  0700>.** KEYWORD
2e210 53 3a 20 7b 63 6f 6c 75 6d 6e 20 61 63 63 65 73  S: {column acces
2e220 73 20 66 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a  s functions}.**.
2e230 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
2e240 73 20 66 6f 72 6d 20 74 68 65 20 22 72 65 73 75  s form the "resu
2e250 6c 74 20 73 65 74 20 71 75 65 72 79 22 20 69 6e  lt set query" in
2e260 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54  terface..**.** T
2e270 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65  hese routines re
2e280 74 75 72 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  turn information
2e290 20 61 62 6f 75 74 20 61 20 73 69 6e 67 6c 65 20   about a single 
2e2a0 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 63 75  column of the cu
2e2b0 72 72 65 6e 74 0a 2a 2a 20 72 65 73 75 6c 74 20  rrent.** result 
2e2c0 72 6f 77 20 6f 66 20 61 20 71 75 65 72 79 2e 20  row of a query. 
2e2d0 20 49 6e 20 65 76 65 72 79 20 63 61 73 65 20 74   In every case t
2e2e0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
2e2f0 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a  t is a pointer.*
2e300 2a 20 74 6f 20 74 68 65 20 5b 70 72 65 70 61 72  * to the [prepar
2e310 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 68  ed statement] th
2e320 61 74 20 69 73 20 62 65 69 6e 67 20 65 76 61 6c  at is being eval
2e330 75 61 74 65 64 20 28 74 68 65 20 5b 73 71 6c 69  uated (the [sqli
2e340 74 65 33 5f 73 74 6d 74 2a 5d 0a 2a 2a 20 74 68  te3_stmt*].** th
2e350 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 64 20  at was returned 
2e360 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 70 72  from [sqlite3_pr
2e370 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 6f  epare_v2()] or o
2e380 6e 65 20 6f 66 20 69 74 73 20 76 61 72 69 61 6e  ne of its varian
2e390 74 73 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73  ts).** and the s
2e3a0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69  econd argument i
2e3b0 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  s the index of t
2e3c0 68 65 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 68  he column for wh
2e3d0 69 63 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a  ich information.
2e3e0 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 72 65 74  ** should be ret
2e3f0 75 72 6e 65 64 2e 20 20 54 68 65 20 6c 65 66 74  urned.  The left
2e400 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  most column of t
2e410 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 68 61  he result set ha
2e420 73 20 74 68 65 20 69 6e 64 65 78 20 30 2e 0a 2a  s the index 0..*
2e430 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 20  *.** If the SQL 
2e440 73 74 61 74 65 6d 65 6e 74 20 64 6f 65 73 20 6e  statement does n
2e450 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  ot currently poi
2e460 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 20 72 6f  nt to a valid ro
2e470 77 2c 20 6f 72 20 69 66 20 74 68 65 0a 2a 2a 20  w, or if the.** 
2e480 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 69 73 20  column index is 
2e490 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 68  out of range, th
2e4a0 65 20 72 65 73 75 6c 74 20 69 73 20 75 6e 64 65  e result is unde
2e4b0 66 69 6e 65 64 2e 0a 2a 2a 20 54 68 65 73 65 20  fined..** These 
2e4c0 72 6f 75 74 69 6e 65 73 20 6d 61 79 20 6f 6e 6c  routines may onl
2e4d0 79 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e  y be called when
2e4e0 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
2e4f0 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c   call to.** [sql
2e500 69 74 65 33 5f 73 74 65 70 28 29 5d 20 68 61 73  ite3_step()] has
2e510 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54   returned [SQLIT
2e520 45 5f 52 4f 57 5d 20 61 6e 64 20 6e 65 69 74 68  E_ROW] and neith
2e530 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72  er.** [sqlite3_r
2e540 65 73 65 74 28 29 5d 20 6e 6f 72 20 5b 73 71 6c  eset()] nor [sql
2e550 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d  ite3_finalize()]
2e560 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c 6c 65   have been calle
2e570 64 20 73 75 62 73 65 71 75 65 6e 74 6c 79 2e 0a  d subsequently..
2e580 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68 65  ** If any of the
2e590 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  se routines are 
2e5a0 63 61 6c 6c 65 64 20 61 66 74 65 72 20 5b 73 71  called after [sq
2e5b0 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f  lite3_reset()] o
2e5c0 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69  r.** [sqlite3_fi
2e5d0 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20 61 66 74  nalize()] or aft
2e5e0 65 72 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  er [sqlite3_step
2e5f0 28 29 5d 20 68 61 73 20 72 65 74 75 72 6e 65 64  ()] has returned
2e600 0a 2a 2a 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74  .** something ot
2e610 68 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45  her than [SQLITE
2e620 5f 52 4f 57 5d 2c 20 74 68 65 20 72 65 73 75 6c  _ROW], the resul
2e630 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64  ts are undefined
2e640 2e 0a 2a 2a 20 49 66 20 5b 73 71 6c 69 74 65 33  ..** If [sqlite3
2e650 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b 73 71 6c  _step()] or [sql
2e660 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72  ite3_reset()] or
2e670 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
2e680 7a 65 28 29 5d 0a 2a 2a 20 61 72 65 20 63 61 6c  ze()].** are cal
2e690 6c 65 64 20 66 72 6f 6d 20 61 20 64 69 66 66 65  led from a diffe
2e6a0 72 65 6e 74 20 74 68 72 65 61 64 20 77 68 69 6c  rent thread whil
2e6b0 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 72  e any of these r
2e6c0 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72 65 20 70  outines.** are p
2e6d0 65 6e 64 69 6e 67 2c 20 74 68 65 6e 20 74 68 65  ending, then the
2e6e0 20 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e 64   results are und
2e6f0 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  efined..**.** Th
2e700 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
2e710 5f 74 79 70 65 28 29 20 72 6f 75 74 69 6e 65 20  _type() routine 
2e720 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 5b  returns the.** [
2e730 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 7c  SQLITE_INTEGER |
2e740 20 64 61 74 61 74 79 70 65 20 63 6f 64 65 5d 20   datatype code] 
2e750 66 6f 72 20 74 68 65 20 69 6e 69 74 69 61 6c 20  for the initial 
2e760 64 61 74 61 20 74 79 70 65 0a 2a 2a 20 6f 66 20  data type.** of 
2e770 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d  the result colum
2e780 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e 65 64  n.  The returned
2e790 20 76 61 6c 75 65 20 69 73 20 6f 6e 65 20 6f 66   value is one of
2e7a0 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52   [SQLITE_INTEGER
2e7b0 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 46 4c  ],.** [SQLITE_FL
2e7c0 4f 41 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 54 45  OAT], [SQLITE_TE
2e7d0 58 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 4c 4f  XT], [SQLITE_BLO
2e7e0 42 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4e  B], or [SQLITE_N
2e7f0 55 4c 4c 5d 2e 20 20 54 68 65 20 76 61 6c 75 65  ULL].  The value
2e800 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 62 79 20  .** returned by 
2e810 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
2e820 79 70 65 28 29 20 69 73 20 6f 6e 6c 79 20 6d 65  ype() is only me
2e830 61 6e 69 6e 67 66 75 6c 20 69 66 20 6e 6f 20 74  aningful if no t
2e840 79 70 65 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f  ype.** conversio
2e850 6e 73 20 68 61 76 65 20 6f 63 63 75 72 72 65 64  ns have occurred
2e860 20 61 73 20 64 65 73 63 72 69 62 65 64 20 62 65   as described be
2e870 6c 6f 77 2e 20 20 41 66 74 65 72 20 61 20 74 79  low.  After a ty
2e880 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2c 0a 2a  pe conversion,.*
2e890 2a 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75  * the value retu
2e8a0 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  rned by sqlite3_
2e8b0 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 20 69 73  column_type() is
2e8c0 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 46 75 74   undefined.  Fut
2e8d0 75 72 65 0a 2a 2a 20 76 65 72 73 69 6f 6e 73 20  ure.** versions 
2e8e0 6f 66 20 53 51 4c 69 74 65 20 6d 61 79 20 63 68  of SQLite may ch
2e8f0 61 6e 67 65 20 74 68 65 20 62 65 68 61 76 69 6f  ange the behavio
2e900 72 20 6f 66 20 73 71 6c 69 74 65 33 5f 63 6f 6c  r of sqlite3_col
2e910 75 6d 6e 5f 74 79 70 65 28 29 0a 2a 2a 20 66 6f  umn_type().** fo
2e920 6c 6c 6f 77 69 6e 67 20 61 20 74 79 70 65 20 63  llowing a type c
2e930 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  onversion..**.**
2e940 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 69   If the result i
2e950 73 20 61 20 42 4c 4f 42 20 6f 72 20 55 54 46 2d  s a BLOB or UTF-
2e960 38 20 73 74 72 69 6e 67 20 74 68 65 6e 20 74 68  8 string then th
2e970 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
2e980 5f 62 79 74 65 73 28 29 0a 2a 2a 20 72 6f 75 74  _bytes().** rout
2e990 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ine returns the 
2e9a0 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
2e9b0 69 6e 20 74 68 61 74 20 42 4c 4f 42 20 6f 72 20  in that BLOB or 
2e9c0 73 74 72 69 6e 67 2e 0a 2a 2a 20 49 66 20 74 68  string..** If th
2e9d0 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 55 54  e result is a UT
2e9e0 46 2d 31 36 20 73 74 72 69 6e 67 2c 20 74 68 65  F-16 string, the
2e9f0 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  n sqlite3_column
2ea00 5f 62 79 74 65 73 28 29 20 63 6f 6e 76 65 72 74  _bytes() convert
2ea10 73 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20  s.** the string 
2ea20 74 6f 20 55 54 46 2d 38 20 61 6e 64 20 74 68 65  to UTF-8 and the
2ea30 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75  n returns the nu
2ea40 6d 62 65 72 20 6f 66 20 62 79 74 65 73 2e 0a 2a  mber of bytes..*
2ea50 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20  * If the result 
2ea60 69 73 20 61 20 6e 75 6d 65 72 69 63 20 76 61 6c  is a numeric val
2ea70 75 65 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f  ue then sqlite3_
2ea80 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 75  column_bytes() u
2ea90 73 65 73 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  ses.** [sqlite3_
2eaa0 73 6e 70 72 69 6e 74 66 28 29 5d 20 74 6f 20 63  snprintf()] to c
2eab0 6f 6e 76 65 72 74 20 74 68 61 74 20 76 61 6c 75  onvert that valu
2eac0 65 20 74 6f 20 61 20 55 54 46 2d 38 20 73 74 72  e to a UTF-8 str
2ead0 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73 0a  ing and returns.
2eae0 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ** the number of
2eaf0 20 62 79 74 65 73 20 69 6e 20 74 68 61 74 20 73   bytes in that s
2eb00 74 72 69 6e 67 2e 0a 2a 2a 20 54 68 65 20 76 61  tring..** The va
2eb10 6c 75 65 20 72 65 74 75 72 6e 65 64 20 64 6f 65  lue returned doe
2eb20 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 74 68  s not include th
2eb30 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f  e zero terminato
2eb40 72 20 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20  r at the end.** 
2eb50 6f 66 20 74 68 65 20 73 74 72 69 6e 67 2e 20 20  of the string.  
2eb60 46 6f 72 20 63 6c 61 72 69 74 79 3a 20 74 68 65  For clarity: the
2eb70 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
2eb80 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
2eb90 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 74 68 65  .** bytes in the
2eba0 20 73 74 72 69 6e 67 2c 20 6e 6f 74 20 74 68 65   string, not the
2ebb0 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61   number of chara
2ebc0 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 53 74 72  cters..**.** Str
2ebd0 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 62 79  ings returned by
2ebe0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
2ebf0 74 65 78 74 28 29 20 61 6e 64 20 73 71 6c 69 74  text() and sqlit
2ec00 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
2ec10 28 29 2c 0a 2a 2a 20 65 76 65 6e 20 65 6d 70 74  (),.** even empt
2ec20 79 20 73 74 72 69 6e 67 73 2c 20 61 72 65 20 61  y strings, are a
2ec30 6c 77 61 79 73 20 7a 65 72 6f 20 74 65 72 6d 69  lways zero termi
2ec40 6e 61 74 65 64 2e 20 20 54 68 65 20 72 65 74 75  nated.  The retu
2ec50 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 66 72 6f 6d  rn.** value from
2ec60 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
2ec70 62 6c 6f 62 28 29 20 66 6f 72 20 61 20 7a 65 72  blob() for a zer
2ec80 6f 2d 6c 65 6e 67 74 68 20 42 4c 4f 42 20 69 73  o-length BLOB is
2ec90 20 61 6e 20 61 72 62 69 74 72 61 72 79 0a 2a 2a   an arbitrary.**
2eca0 20 70 6f 69 6e 74 65 72 2c 20 70 6f 73 73 69 62   pointer, possib
2ecb0 6c 79 20 65 76 65 6e 20 61 20 4e 55 4c 4c 20 70  ly even a NULL p
2ecc0 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  ointer..**.** Th
2ecd0 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
2ece0 5f 62 79 74 65 73 31 36 28 29 20 72 6f 75 74 69  _bytes16() routi
2ecf0 6e 65 20 69 73 20 73 69 6d 69 6c 61 72 20 74 6f  ne is similar to
2ed00 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
2ed10 62 79 74 65 73 28 29 0a 2a 2a 20 62 75 74 20 6c  bytes().** but l
2ed20 65 61 76 65 73 20 74 68 65 20 72 65 73 75 6c 74  eaves the result
2ed30 20 69 6e 20 55 54 46 2d 31 36 20 69 6e 20 6e 61   in UTF-16 in na
2ed40 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20  tive byte order 
2ed50 69 6e 73 74 65 61 64 20 6f 66 20 55 54 46 2d 38  instead of UTF-8
2ed60 2e 0a 2a 2a 20 54 68 65 20 7a 65 72 6f 20 74 65  ..** The zero te
2ed70 72 6d 69 6e 61 74 6f 72 20 69 73 20 6e 6f 74 20  rminator is not 
2ed80 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 69 73  included in this
2ed90 20 63 6f 75 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68   count..**.** Th
2eda0 65 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 65  e object returne
2edb0 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 63 6f  d by [sqlite3_co
2edc0 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73  lumn_value()] is
2edd0 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65 63   an.** [unprotec
2ede0 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ted sqlite3_valu
2edf0 65 5d 20 6f 62 6a 65 63 74 2e 20 20 41 6e 20 75  e] object.  An u
2ee00 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  nprotected sqlit
2ee10 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a  e3_value object.
2ee20 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75  ** may only be u
2ee30 73 65 64 20 77 69 74 68 20 5b 73 71 6c 69 74 65  sed with [sqlite
2ee40 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 29 5d 20  3_bind_value()] 
2ee50 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72 65 73  and [sqlite3_res
2ee60 75 6c 74 5f 76 61 6c 75 65 28 29 5d 2e 0a 2a 2a  ult_value()]..**
2ee70 20 49 66 20 74 68 65 20 5b 75 6e 70 72 6f 74 65   If the [unprote
2ee80 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c  cted sqlite3_val
2ee90 75 65 5d 20 6f 62 6a 65 63 74 20 72 65 74 75 72  ue] object retur
2eea0 6e 65 64 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74  ned by.** [sqlit
2eeb0 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28  e3_column_value(
2eec0 29 5d 20 69 73 20 75 73 65 64 20 69 6e 20 61 6e  )] is used in an
2eed0 79 20 6f 74 68 65 72 20 77 61 79 2c 20 69 6e 63  y other way, inc
2eee0 6c 75 64 69 6e 67 20 63 61 6c 6c 73 0a 2a 2a 20  luding calls.** 
2eef0 74 6f 20 72 6f 75 74 69 6e 65 73 20 6c 69 6b 65  to routines like
2ef00 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   [sqlite3_value_
2ef10 69 6e 74 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33  int()], [sqlite3
2ef20 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c 0a  _value_text()],.
2ef30 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 76  ** or [sqlite3_v
2ef40 61 6c 75 65 5f 62 79 74 65 73 28 29 5d 2c 20 74  alue_bytes()], t
2ef50 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72  hen the behavior
2ef60 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a   is undefined..*
2ef70 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
2ef80 6e 65 73 20 61 74 74 65 6d 70 74 20 74 6f 20 63  nes attempt to c
2ef90 6f 6e 76 65 72 74 20 74 68 65 20 76 61 6c 75 65  onvert the value
2efa0 20 77 68 65 72 65 20 61 70 70 72 6f 70 72 69 61   where appropria
2efb0 74 65 2e 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d  te.  For.** exam
2efc0 70 6c 65 2c 20 69 66 20 74 68 65 20 69 6e 74 65  ple, if the inte
2efd0 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74  rnal representat
2efe0 69 6f 6e 20 69 73 20 46 4c 4f 41 54 20 61 6e 64  ion is FLOAT and
2eff0 20 61 20 74 65 78 74 20 72 65 73 75 6c 74 0a 2a   a text result.*
2f000 2a 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20  * is requested, 
2f010 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74  [sqlite3_snprint
2f020 66 28 29 5d 20 69 73 20 75 73 65 64 20 69 6e 74  f()] is used int
2f030 65 72 6e 61 6c 6c 79 20 74 6f 20 70 65 72 66 6f  ernally to perfo
2f040 72 6d 20 74 68 65 0a 2a 2a 20 63 6f 6e 76 65 72  rm the.** conver
2f050 73 69 6f 6e 20 61 75 74 6f 6d 61 74 69 63 61 6c  sion automatical
2f060 6c 79 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ly.  The followi
2f070 6e 67 20 74 61 62 6c 65 20 64 65 74 61 69 6c 73  ng table details
2f080 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73   the conversions
2f090 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 61 70 70  .** that are app
2f0a0 6c 69 65 64 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f  lied:.**.** <blo
2f0b0 63 6b 71 75 6f 74 65 3e 0a 2a 2a 20 3c 74 61 62  ckquote>.** <tab
2f0c0 6c 65 20 62 6f 72 64 65 72 3d 22 31 22 3e 0a 2a  le border="1">.*
2f0d0 2a 20 3c 74 72 3e 3c 74 68 3e 20 49 6e 74 65 72  * <tr><th> Inter
2f0e0 6e 61 6c 3c 62 72 3e 54 79 70 65 20 3c 74 68 3e  nal<br>Type <th>
2f0f0 20 52 65 71 75 65 73 74 65 64 3c 62 72 3e 54 79   Requested<br>Ty
2f100 70 65 20 3c 74 68 3e 20 20 43 6f 6e 76 65 72 73  pe <th>  Convers
2f110 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c 74  ion.**.** <tr><t
2f120 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e  d>  NULL    <td>
2f130 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20   INTEGER   <td> 
2f140 52 65 73 75 6c 74 20 69 73 20 30 0a 2a 2a 20 3c  Result is 0.** <
2f150 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20  tr><td>  NULL   
2f160 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20   <td>  FLOAT    
2f170 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20 30  <td> Result is 0
2f180 2e 30 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  .0.** <tr><td>  
2f190 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20 20 54  NULL    <td>   T
2f1a0 45 58 54 20 20 20 20 3c 74 64 3e 20 52 65 73 75  EXT    <td> Resu
2f1b0 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74  lt is NULL point
2f1c0 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  er.** <tr><td>  
2f1d0 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20 20 42  NULL    <td>   B
2f1e0 4c 4f 42 20 20 20 20 3c 74 64 3e 20 52 65 73 75  LOB    <td> Resu
2f1f0 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74  lt is NULL point
2f200 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49  er.** <tr><td> I
2f210 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 20 46 4c  NTEGER  <td>  FL
2f220 4f 41 54 20 20 20 20 3c 74 64 3e 20 43 6f 6e 76  OAT    <td> Conv
2f230 65 72 74 20 66 72 6f 6d 20 69 6e 74 65 67 65 72  ert from integer
2f240 20 74 6f 20 66 6c 6f 61 74 0a 2a 2a 20 3c 74 72   to float.** <tr
2f250 3e 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 3c  ><td> INTEGER  <
2f260 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74  td>   TEXT    <t
2f270 64 3e 20 41 53 43 49 49 20 72 65 6e 64 65 72 69  d> ASCII renderi
2f280 6e 67 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65  ng of the intege
2f290 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e  r.** <tr><td> IN
2f2a0 54 45 47 45 52 20 20 3c 74 64 3e 20 20 20 42 4c  TEGER  <td>   BL
2f2b0 4f 42 20 20 20 20 3c 74 64 3e 20 53 61 6d 65 20  OB    <td> Same 
2f2c0 61 73 20 49 4e 54 45 47 45 52 2d 3e 54 45 58 54  as INTEGER->TEXT
2f2d0 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c  .** <tr><td>  FL
2f2e0 4f 41 54 20 20 20 3c 74 64 3e 20 49 4e 54 45 47  OAT   <td> INTEG
2f2f0 45 52 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72  ER   <td> Conver
2f300 74 20 66 72 6f 6d 20 66 6c 6f 61 74 20 74 6f 20  t from float to 
2f310 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c  integer.** <tr><
2f320 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 3c 74 64  td>  FLOAT   <td
2f330 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e  >   TEXT    <td>
2f340 20 41 53 43 49 49 20 72 65 6e 64 65 72 69 6e 67   ASCII rendering
2f350 20 6f 66 20 74 68 65 20 66 6c 6f 61 74 0a 2a 2a   of the float.**
2f360 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54   <tr><td>  FLOAT
2f370 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20     <td>   BLOB  
2f380 20 20 3c 74 64 3e 20 53 61 6d 65 20 61 73 20 46    <td> Same as F
2f390 4c 4f 41 54 2d 3e 54 45 58 54 0a 2a 2a 20 3c 74  LOAT->TEXT.** <t
2f3a0 72 3e 3c 74 64 3e 20 20 54 45 58 54 20 20 20 20  r><td>  TEXT    
2f3b0 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c  <td> INTEGER   <
2f3c0 74 64 3e 20 55 73 65 20 61 74 6f 69 28 29 0a 2a  td> Use atoi().*
2f3d0 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58 54  * <tr><td>  TEXT
2f3e0 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20      <td>  FLOAT 
2f3f0 20 20 20 3c 74 64 3e 20 55 73 65 20 61 74 6f 66     <td> Use atof
2f400 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  ().** <tr><td>  
2f410 54 45 58 54 20 20 20 20 3c 74 64 3e 20 20 20 42  TEXT    <td>   B
2f420 4c 4f 42 20 20 20 20 3c 74 64 3e 20 4e 6f 20 63  LOB    <td> No c
2f430 68 61 6e 67 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64  hange.** <tr><td
2f440 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20  >  BLOB    <td> 
2f450 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20 43  INTEGER   <td> C
2f460 6f 6e 76 65 72 74 20 74 6f 20 54 45 58 54 20 74  onvert to TEXT t
2f470 68 65 6e 20 75 73 65 20 61 74 6f 69 28 29 0a 2a  hen use atoi().*
2f480 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42  * <tr><td>  BLOB
2f490 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20      <td>  FLOAT 
2f4a0 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20     <td> Convert 
2f4b0 74 6f 20 54 45 58 54 20 74 68 65 6e 20 75 73 65  to TEXT then use
2f4c0 20 61 74 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c   atof().** <tr><
2f4d0 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74 64  td>  BLOB    <td
2f4e0 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e  >   TEXT    <td>
2f4f0 20 41 64 64 20 61 20 7a 65 72 6f 20 74 65 72 6d   Add a zero term
2f500 69 6e 61 74 6f 72 20 69 66 20 6e 65 65 64 65 64  inator if needed
2f510 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 20  .** </table>.** 
2f520 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a  </blockquote>.**
2f530 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 61 62  .** The table ab
2f540 6f 76 65 20 6d 61 6b 65 73 20 72 65 66 65 72 65  ove makes refere
2f550 6e 63 65 20 74 6f 20 73 74 61 6e 64 61 72 64 20  nce to standard 
2f560 43 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69  C library functi
2f570 6f 6e 73 20 61 74 6f 69 28 29 0a 2a 2a 20 61 6e  ons atoi().** an
2f580 64 20 61 74 6f 66 28 29 2e 20 20 53 51 4c 69 74  d atof().  SQLit
2f590 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 61 6c 6c  e does not reall
2f5a0 79 20 75 73 65 20 74 68 65 73 65 20 66 75 6e 63  y use these func
2f5b0 74 69 6f 6e 73 2e 20 20 49 74 20 68 61 73 20 69  tions.  It has i
2f5c0 74 73 0a 2a 2a 20 6f 77 6e 20 65 71 75 69 76 61  ts.** own equiva
2f5d0 6c 65 6e 74 20 69 6e 74 65 72 6e 61 6c 20 72 6f  lent internal ro
2f5e0 75 74 69 6e 65 73 2e 20 20 54 68 65 20 61 74 6f  utines.  The ato
2f5f0 69 28 29 20 61 6e 64 20 61 74 6f 66 28 29 20 6e  i() and atof() n
2f600 61 6d 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 64  ames are.** used
2f610 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 66 6f   in the table fo
2f620 72 20 62 72 65 76 69 74 79 20 61 6e 64 20 62 65  r brevity and be
2f630 63 61 75 73 65 20 74 68 65 79 20 61 72 65 20 66  cause they are f
2f640 61 6d 69 6c 69 61 72 20 74 6f 20 6d 6f 73 74 0a  amiliar to most.
2f650 2a 2a 20 43 20 70 72 6f 67 72 61 6d 6d 65 72 73  ** C programmers
2f660 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
2f670 74 20 77 68 65 6e 20 74 79 70 65 20 63 6f 6e 76  t when type conv
2f680 65 72 73 69 6f 6e 73 20 6f 63 63 75 72 2c 20 70  ersions occur, p
2f690 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 64  ointers returned
2f6a0 20 62 79 20 70 72 69 6f 72 0a 2a 2a 20 63 61 6c   by prior.** cal
2f6b0 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f  ls to sqlite3_co
2f6c0 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 73 71 6c  lumn_blob(), sql
2f6d0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
2f6e0 28 29 2c 20 61 6e 64 2f 6f 72 0a 2a 2a 20 73 71  (), and/or.** sq
2f6f0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
2f700 74 31 36 28 29 20 6d 61 79 20 62 65 20 69 6e 76  t16() may be inv
2f710 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 20 54 79 70  alidated..** Typ
2f720 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 6e  e conversions an
2f730 64 20 70 6f 69 6e 74 65 72 20 69 6e 76 61 6c 69  d pointer invali
2f740 64 61 74 69 6f 6e 73 20 6d 69 67 68 74 20 6f 63  dations might oc
2f750 63 75 72 0a 2a 2a 20 69 6e 20 74 68 65 20 66 6f  cur.** in the fo
2f760 6c 6c 6f 77 69 6e 67 20 63 61 73 65 73 3a 0a 2a  llowing cases:.*
2f770 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69  *.** <ul>.** <li
2f780 3e 20 54 68 65 20 69 6e 69 74 69 61 6c 20 63 6f  > The initial co
2f790 6e 74 65 6e 74 20 69 73 20 61 20 42 4c 4f 42 20  ntent is a BLOB 
2f7a0 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  and sqlite3_colu
2f7b0 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 2a 2a 20  mn_text() or.** 
2f7c0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c       sqlite3_col
2f7d0 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 69 73 20  umn_text16() is 
2f7e0 63 61 6c 6c 65 64 2e 20 20 41 20 7a 65 72 6f 2d  called.  A zero-
2f7f0 74 65 72 6d 69 6e 61 74 6f 72 20 6d 69 67 68 74  terminator might
2f800 0a 2a 2a 20 20 20 20 20 20 6e 65 65 64 20 74 6f  .**      need to
2f810 20 62 65 20 61 64 64 65 64 20 74 6f 20 74 68 65   be added to the
2f820 20 73 74 72 69 6e 67 2e 3c 2f 6c 69 3e 0a 2a 2a   string.</li>.**
2f830 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 61   <li> The initia
2f840 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 55 54 46  l content is UTF
2f850 2d 38 20 74 65 78 74 20 61 6e 64 20 73 71 6c 69  -8 text and sqli
2f860 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
2f870 31 36 28 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20  16() or.**      
2f880 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
2f890 65 78 74 31 36 28 29 20 69 73 20 63 61 6c 6c 65  ext16() is calle
2f8a0 64 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20  d.  The content 
2f8b0 6d 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74 65  must be converte
2f8c0 64 0a 2a 2a 20 20 20 20 20 20 74 6f 20 55 54 46  d.**      to UTF
2f8d0 2d 31 36 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 6c 69  -16.</li>.** <li
2f8e0 3e 20 54 68 65 20 69 6e 69 74 69 61 6c 20 63 6f  > The initial co
2f8f0 6e 74 65 6e 74 20 69 73 20 55 54 46 2d 31 36 20  ntent is UTF-16 
2f900 74 65 78 74 20 61 6e 64 20 73 71 6c 69 74 65 33  text and sqlite3
2f910 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20  _column_bytes() 
2f920 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74  or.**      sqlit
2f930 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29  e3_column_text()
2f940 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 65   is called.  The
2f950 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62 65   content must be
2f960 20 63 6f 6e 76 65 72 74 65 64 0a 2a 2a 20 20 20   converted.**   
2f970 20 20 20 74 6f 20 55 54 46 2d 38 2e 3c 2f 6c 69     to UTF-8.</li
2f980 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a  >.** </ul>.**.**
2f990 20 43 6f 6e 76 65 72 73 69 6f 6e 73 20 62 65 74   Conversions bet
2f9a0 77 65 65 6e 20 55 54 46 2d 31 36 62 65 20 61 6e  ween UTF-16be an
2f9b0 64 20 55 54 46 2d 31 36 6c 65 20 61 72 65 20 61  d UTF-16le are a
2f9c0 6c 77 61 79 73 20 64 6f 6e 65 20 69 6e 20 70 6c  lways done in pl
2f9d0 61 63 65 20 61 6e 64 20 64 6f 0a 2a 2a 20 6e 6f  ace and do.** no
2f9e0 74 20 69 6e 76 61 6c 69 64 61 74 65 20 61 20 70  t invalidate a p
2f9f0 72 69 6f 72 20 70 6f 69 6e 74 65 72 2c 20 74 68  rior pointer, th
2fa00 6f 75 67 68 20 6f 66 20 63 6f 75 72 73 65 20 74  ough of course t
2fa10 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68  he content of th
2fa20 65 20 62 75 66 66 65 72 0a 2a 2a 20 74 68 61 74  e buffer.** that
2fa30 20 74 68 65 20 70 72 69 6f 72 20 70 6f 69 6e 74   the prior point
2fa40 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 77 69 6c  er points to wil
2fa50 6c 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69  l have been modi
2fa60 66 69 65 64 2e 20 20 4f 74 68 65 72 20 6b 69 6e  fied.  Other kin
2fa70 64 73 0a 2a 2a 20 6f 66 20 63 6f 6e 76 65 72 73  ds.** of convers
2fa80 69 6f 6e 20 61 72 65 20 64 6f 6e 65 20 69 6e 20  ion are done in 
2fa90 70 6c 61 63 65 20 77 68 65 6e 20 69 74 20 69 73  place when it is
2faa0 20 70 6f 73 73 69 62 6c 65 2c 20 62 75 74 20 73   possible, but s
2fab0 6f 6d 65 74 69 6d 65 73 20 74 68 65 79 0a 2a 2a  ometimes they.**
2fac0 20 61 72 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c   are not possibl
2fad0 65 20 61 6e 64 20 69 6e 20 74 68 6f 73 65 20 63  e and in those c
2fae0 61 73 65 73 20 70 72 69 6f 72 20 70 6f 69 6e 74  ases prior point
2faf0 65 72 73 20 61 72 65 20 69 6e 76 61 6c 69 64 61  ers are invalida
2fb00 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ted..**.** The s
2fb10 61 66 65 73 74 20 61 6e 64 20 65 61 73 69 65 73  afest and easies
2fb20 74 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 70 6f  t to remember po
2fb30 6c 69 63 79 20 69 73 20 74 6f 20 69 6e 76 6f 6b  licy is to invok
2fb40 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  e these routines
2fb50 0a 2a 2a 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68  .** in one of th
2fb60 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 61 79 73  e following ways
2fb70 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  :.**.** <ul>.** 
2fb80 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f 6c   <li>sqlite3_col
2fb90 75 6d 6e 5f 74 65 78 74 28 29 20 66 6f 6c 6c 6f  umn_text() follo
2fba0 77 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63  wed by sqlite3_c
2fbb0 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 3c 2f 6c  olumn_bytes()</l
2fbc0 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74  i>.**  <li>sqlit
2fbd0 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29  e3_column_blob()
2fbe0 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c   followed by sql
2fbf0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
2fc00 73 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69  s()</li>.**  <li
2fc10 3e 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  >sqlite3_column_
2fc20 74 65 78 74 31 36 28 29 20 66 6f 6c 6c 6f 77 65  text16() followe
2fc30 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c  d by sqlite3_col
2fc40 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 3c 2f 6c  umn_bytes16()</l
2fc50 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a  i>.** </ul>.**.*
2fc60 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  * In other words
2fc70 2c 20 79 6f 75 20 73 68 6f 75 6c 64 20 63 61 6c  , you should cal
2fc80 6c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  l sqlite3_column
2fc90 5f 74 65 78 74 28 29 2c 0a 2a 2a 20 73 71 6c 69  _text(),.** sqli
2fca0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
2fcb0 29 2c 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f  ), or sqlite3_co
2fcc0 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 69  lumn_text16() fi
2fcd0 72 73 74 20 74 6f 20 66 6f 72 63 65 20 74 68 65  rst to force the
2fce0 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 74 6f 20   result.** into 
2fcf0 74 68 65 20 64 65 73 69 72 65 64 20 66 6f 72 6d  the desired form
2fd00 61 74 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65 20  at, then invoke 
2fd10 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
2fd20 79 74 65 73 28 29 20 6f 72 0a 2a 2a 20 73 71 6c  ytes() or.** sql
2fd30 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
2fd40 73 31 36 28 29 20 74 6f 20 66 69 6e 64 20 74 68  s16() to find th
2fd50 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 72 65  e size of the re
2fd60 73 75 6c 74 2e 20 20 44 6f 20 6e 6f 74 20 6d 69  sult.  Do not mi
2fd70 78 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 73 71  x calls.** to sq
2fd80 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
2fd90 74 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63  t() or sqlite3_c
2fda0 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 77 69 74  olumn_blob() wit
2fdb0 68 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71  h calls to.** sq
2fdc0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
2fdd0 65 73 31 36 28 29 2c 20 61 6e 64 20 64 6f 20 6e  es16(), and do n
2fde0 6f 74 20 6d 69 78 20 63 61 6c 6c 73 20 74 6f 20  ot mix calls to 
2fdf0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
2fe00 65 78 74 31 36 28 29 0a 2a 2a 20 77 69 74 68 20  ext16().** with 
2fe10 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33  calls to sqlite3
2fe20 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 2e  _column_bytes().
2fe30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74  .**.** The point
2fe40 65 72 73 20 72 65 74 75 72 6e 65 64 20 61 72 65  ers returned are
2fe50 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 61 20 74   valid until a t
2fe60 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f  ype conversion o
2fe70 63 63 75 72 73 20 61 73 0a 2a 2a 20 64 65 73 63  ccurs as.** desc
2fe80 72 69 62 65 64 20 61 62 6f 76 65 2c 20 6f 72 20  ribed above, or 
2fe90 75 6e 74 69 6c 20 5b 73 71 6c 69 74 65 33 5f 73  until [sqlite3_s
2fea0 74 65 70 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74  tep()] or [sqlit
2feb0 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a  e3_reset()] or.*
2fec0 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  * [sqlite3_final
2fed0 69 7a 65 28 29 5d 20 69 73 20 63 61 6c 6c 65 64  ize()] is called
2fee0 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 73 70  .  The memory sp
2fef0 61 63 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64  ace used to hold
2ff00 20 73 74 72 69 6e 67 73 0a 2a 2a 20 61 6e 64 20   strings.** and 
2ff10 42 4c 4f 42 73 20 69 73 20 66 72 65 65 64 20 61  BLOBs is freed a
2ff20 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 44  utomatically.  D
2ff30 6f 20 3c 62 3e 6e 6f 74 3c 2f 62 3e 20 70 61 73  o <b>not</b> pas
2ff40 73 20 74 68 65 20 70 6f 69 6e 74 65 72 73 20 72  s the pointers r
2ff50 65 74 75 72 6e 65 64 0a 2a 2a 20 5b 73 71 6c 69  eturned.** [sqli
2ff60 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
2ff70 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c  )], [sqlite3_col
2ff80 75 6d 6e 5f 74 65 78 74 28 29 5d 2c 20 65 74 63  umn_text()], etc
2ff90 2e 20 69 6e 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74  . into.** [sqlit
2ffa0 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a 2a 0a 2a  e3_free()]..**.*
2ffb0 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c  * If a memory al
2ffc0 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f  location error o
2ffd0 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 65  ccurs during the
2ffe0 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 61   evaluation of a
2fff0 6e 79 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20 72  ny.** of these r
30000 6f 75 74 69 6e 65 73 2c 20 61 20 64 65 66 61 75  outines, a defau
30010 6c 74 20 76 61 6c 75 65 20 69 73 20 72 65 74 75  lt value is retu
30020 72 6e 65 64 2e 20 20 54 68 65 20 64 65 66 61 75  rned.  The defau
30030 6c 74 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 65  lt value.** is e
30040 69 74 68 65 72 20 74 68 65 20 69 6e 74 65 67 65  ither the intege
30050 72 20 30 2c 20 74 68 65 20 66 6c 6f 61 74 69 6e  r 0, the floatin
30060 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 20 30  g point number 0
30070 2e 30 2c 20 6f 72 20 61 20 4e 55 4c 4c 0a 2a 2a  .0, or a NULL.**
30080 20 70 6f 69 6e 74 65 72 2e 20 20 53 75 62 73 65   pointer.  Subse
30090 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 5b  quent calls to [
300a0 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28  sqlite3_errcode(
300b0 29 5d 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a  )] will return.*
300c0 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d  * [SQLITE_NOMEM]
300d0 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e  ..**.** INVARIAN
300e0 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 38 30  TS:.**.** {H1380
300f0 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  3} The [sqlite3_
30100 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 53 2c 4e 29  column_blob(S,N)
30110 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e 76  ] interface conv
30120 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 20  erts the.**     
30130 20 20 20 20 20 4e 74 68 20 63 6f 6c 75 6d 6e 20       Nth column 
30140 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  in the current r
30150 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  ow of the result
30160 20 73 65 74 20 66 6f 72 0a 2a 2a 20 20 20 20 20   set for.**     
30170 20 20 20 20 20 74 68 65 20 5b 70 72 65 70 61 72       the [prepar
30180 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20  ed statement] S 
30190 69 6e 74 6f 20 61 20 42 4c 4f 42 20 61 6e 64 20  into a BLOB and 
301a0 74 68 65 6e 20 72 65 74 75 72 6e 73 20 61 0a 2a  then returns a.*
301b0 2a 20 20 20 20 20 20 20 20 20 20 70 6f 69 6e 74  *          point
301c0 65 72 20 74 6f 20 74 68 65 20 63 6f 6e 76 65 72  er to the conver
301d0 74 65 64 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a  ted value..**.**
301e0 20 7b 48 31 33 38 30 36 7d 20 54 68 65 20 5b 73   {H13806} The [s
301f0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
30200 74 65 73 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66  tes(S,N)] interf
30210 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a  ace returns the.
30220 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 75 6d 62  **          numb
30230 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74  er of bytes in t
30240 68 65 20 42 4c 4f 42 20 6f 72 20 73 74 72 69 6e  he BLOB or strin
30250 67 20 28 65 78 63 6c 75 73 69 76 65 20 6f 66 20  g (exclusive of 
30260 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
30270 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20  zero terminator 
30280 6f 6e 20 74 68 65 20 73 74 72 69 6e 67 29 20 74  on the string) t
30290 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 64  hat was returned
302a0 20 62 79 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   by the.**      
302b0 20 20 20 20 6d 6f 73 74 20 72 65 63 65 6e 74 20      most recent 
302c0 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
302d0 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 53 2c 4e  _column_blob(S,N
302e0 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20  )] or.**        
302f0 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d    [sqlite3_colum
30300 6e 5f 74 65 78 74 28 53 2c 4e 29 5d 2e 0a 2a 2a  n_text(S,N)]..**
30310 0a 2a 2a 20 7b 48 31 33 38 30 39 7d 20 54 68 65  .** {H13809} The
30320 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   [sqlite3_column
30330 5f 62 79 74 65 73 31 36 28 53 2c 4e 29 5d 20 69  _bytes16(S,N)] i
30340 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
30350 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
30360 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
30370 20 69 6e 20 74 68 65 20 73 74 72 69 6e 67 20 28   in the string (
30380 65 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68 65  exclusive of the
30390 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 7a 65 72  .**          zer
303a0 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 6f 6e 20  o terminator on 
303b0 74 68 65 20 73 74 72 69 6e 67 29 20 74 68 61 74  the string) that
303c0 20 77 61 73 20 72 65 74 75 72 6e 65 64 20 62 79   was returned by
303d0 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
303e0 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c   most recent cal
303f0 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f  l to [sqlite3_co
30400 6c 75 6d 6e 5f 74 65 78 74 31 36 28 53 2c 4e 29  lumn_text16(S,N)
30410 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 38 31 32  ]..**.** {H13812
30420 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63  } The [sqlite3_c
30430 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 53 2c 4e  olumn_double(S,N
30440 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e  )] interface con
30450 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20  verts the.**    
30460 20 20 20 20 20 20 4e 74 68 20 63 6f 6c 75 6d 6e        Nth column
30470 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20   in the current 
30480 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c  row of the resul
30490 74 20 73 65 74 20 66 6f 72 20 74 68 65 0a 2a 2a  t set for the.**
304a0 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61            [prepa
304b0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53  red statement] S
304c0 20 69 6e 74 6f 20 61 20 66 6c 6f 61 74 69 6e 67   into a floating
304d0 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 61 6e 64   point value and
304e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74  .**          ret
304f0 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 20 74  urns a copy of t
30500 68 61 74 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a  hat value..**.**
30510 20 7b 48 31 33 38 31 35 7d 20 54 68 65 20 5b 73   {H13815} The [s
30520 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
30530 74 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63  t(S,N)] interfac
30540 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65 0a 2a  e converts the.*
30550 2a 20 20 20 20 20 20 20 20 20 20 4e 74 68 20 63  *          Nth c
30560 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 63 75 72  olumn in the cur
30570 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20  rent row of the 
30580 72 65 73 75 6c 74 20 73 65 74 20 66 6f 72 20 74  result set for t
30590 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  he.**          [
305a0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
305b0 6e 74 5d 20 53 20 69 6e 74 6f 20 61 20 36 34 2d  nt] S into a 64-
305c0 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67  bit signed integ
305d0 65 72 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20  er and.**       
305e0 20 20 20 72 65 74 75 72 6e 73 20 74 68 65 20 6c     returns the l
305f0 6f 77 65 72 20 33 32 20 62 69 74 73 20 6f 66 20  ower 32 bits of 
30600 74 68 61 74 20 69 6e 74 65 67 65 72 2e 0a 2a 2a  that integer..**
30610 0a 2a 2a 20 7b 48 31 33 38 31 38 7d 20 54 68 65  .** {H13818} The
30620 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   [sqlite3_column
30630 5f 69 6e 74 36 34 28 53 2c 4e 29 5d 20 69 6e 74  _int64(S,N)] int
30640 65 72 66 61 63 65 20 63 6f 6e 76 65 72 74 73 20  erface converts 
30650 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
30660 4e 74 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68  Nth column in th
30670 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66  e current row of
30680 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
30690 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  for the.**      
306a0 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 74      [prepared st
306b0 61 74 65 6d 65 6e 74 5d 20 53 20 69 6e 74 6f 20  atement] S into 
306c0 61 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20  a 64-bit signed 
306d0 69 6e 74 65 67 65 72 20 61 6e 64 0a 2a 2a 20 20  integer and.**  
306e0 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 73 20          returns 
306f0 61 20 63 6f 70 79 20 6f 66 20 74 68 61 74 20 69  a copy of that i
30700 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48  nteger..**.** {H
30710 31 33 38 32 31 7d 20 54 68 65 20 5b 73 71 6c 69  13821} The [sqli
30720 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28  te3_column_text(
30730 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20  S,N)] interface 
30740 63 6f 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a 20  converts the.** 
30750 20 20 20 20 20 20 20 20 20 4e 74 68 20 63 6f 6c           Nth col
30760 75 6d 6e 20 69 6e 20 74 68 65 20 63 75 72 72 65  umn in the curre
30770 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 72 65  nt row of the re
30780 73 75 6c 74 20 73 65 74 20 66 6f 72 0a 2a 2a 20  sult set for.** 
30790 20 20 20 20 20 20 20 20 20 74 68 65 20 5b 70 72           the [pr
307a0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
307b0 5d 20 53 20 69 6e 74 6f 20 61 20 7a 65 72 6f 2d  ] S into a zero-
307c0 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38  terminated UTF-8
307d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74 72  .**          str
307e0 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73 20  ing and returns 
307f0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61  a pointer to tha
30800 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  t string..**.** 
30810 7b 48 31 33 38 32 34 7d 20 54 68 65 20 5b 73 71  {H13824} The [sq
30820 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
30830 74 31 36 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66  t16(S,N)] interf
30840 61 63 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65  ace converts the
30850 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 4e 74 68  .**          Nth
30860 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 63   column in the c
30870 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68  urrent row of th
30880 65 20 72 65 73 75 6c 74 20 73 65 74 20 66 6f 72  e result set for
30890 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
308a0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
308b0 6d 65 6e 74 5d 20 53 20 69 6e 74 6f 20 61 20 7a  ment] S into a z
308c0 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 32  ero-terminated 2
308d0 2d 62 79 74 65 0a 2a 2a 20 20 20 20 20 20 20 20  -byte.**        
308e0 20 20 61 6c 69 67 6e 65 64 20 55 54 46 2d 31 36    aligned UTF-16
308f0 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64   native byte ord
30900 65 72 20 73 74 72 69 6e 67 20 61 6e 64 20 72 65  er string and re
30910 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20  turns.**        
30920 20 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74    a pointer to t
30930 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a  hat string..**.*
30940 2a 20 7b 48 31 33 38 32 37 7d 20 54 68 65 20 5b  * {H13827} The [
30950 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
30960 79 70 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66  ype(S,N)] interf
30970 61 63 65 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20  ace returns.**  
30980 20 20 20 20 20 20 20 20 6f 6e 65 20 6f 66 20 5b          one of [
30990 53 51 4c 49 54 45 5f 4e 55 4c 4c 5d 2c 20 5b 53  SQLITE_NULL], [S
309a0 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 5d 2c 20  QLITE_INTEGER], 
309b0 5b 53 51 4c 49 54 45 5f 46 4c 4f 41 54 5d 2c 0a  [SQLITE_FLOAT],.
309c0 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c  **          [SQL
309d0 49 54 45 5f 54 45 58 54 5d 2c 20 6f 72 20 5b 53  ITE_TEXT], or [S
309e0 51 4c 49 54 45 5f 42 4c 4f 42 5d 20 61 73 20 61  QLITE_BLOB] as a
309f0 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 0a 2a  ppropriate for.*
30a00 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 4e  *          the N
30a10 74 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65  th column in the
30a20 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20   current row of 
30a30 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 66  the result set f
30a40 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  or.**          t
30a50 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  he [prepared sta
30a60 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a 2a 0a 2a 2a  tement] S..**.**
30a70 20 7b 48 31 33 38 33 30 7d 20 54 68 65 20 5b 73   {H13830} The [s
30a80 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61  qlite3_column_va
30a90 6c 75 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66  lue(S,N)] interf
30aa0 61 63 65 20 72 65 74 75 72 6e 73 20 61 0a 2a 2a  ace returns a.**
30ab0 20 20 20 20 20 20 20 20 20 20 70 6f 69 6e 74 65            pointe
30ac0 72 20 74 6f 20 61 6e 20 5b 75 6e 70 72 6f 74 65  r to an [unprote
30ad0 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c  cted sqlite3_val
30ae0 75 65 5d 20 6f 62 6a 65 63 74 20 66 6f 72 20 74  ue] object for t
30af0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 4e  he.**          N
30b00 74 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65  th column in the
30b10 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20   current row of 
30b20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 66  the result set f
30b30 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  or.**          t
30b40 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  he [prepared sta
30b50 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a 2f 0a 53 51  tement] S..*/.SQ
30b60 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
30b70 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  oid *sqlite3_col
30b80 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33  umn_blob(sqlite3
30b90 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c  _stmt*, int iCol
30ba0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
30bb0 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  t sqlite3_column
30bc0 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 73  _bytes(sqlite3_s
30bd0 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b  tmt*, int iCol);
30be0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
30bf0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
30c00 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73  ytes16(sqlite3_s
30c10 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b  tmt*, int iCol);
30c20 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62  .SQLITE_API doub
30c30 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  le sqlite3_colum
30c40 6e 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33  n_double(sqlite3
30c50 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c  _stmt*, int iCol
30c60 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
30c70 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  t sqlite3_column
30c80 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d  _int(sqlite3_stm
30c90 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53  t*, int iCol);.S
30ca0 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
30cb0 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f  3_int64 sqlite3_
30cc0 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c  column_int64(sql
30cd0 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20  ite3_stmt*, int 
30ce0 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50  iCol);.SQLITE_AP
30cf0 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  I const unsigned
30d00 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
30d10 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74  olumn_text(sqlit
30d20 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43  e3_stmt*, int iC
30d30 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  ol);.SQLITE_API 
30d40 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
30d50 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31  te3_column_text1
30d60 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  6(sqlite3_stmt*,
30d70 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49   int iCol);.SQLI
30d80 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
30d90 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73  e3_column_type(s
30da0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
30db0 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f  t iCol);.SQLITE_
30dc0 41 50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  API sqlite3_valu
30dd0 65 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  e *sqlite3_colum
30de0 6e 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f  n_value(sqlite3_
30df0 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29  stmt*, int iCol)
30e00 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
30e10 46 3a 20 44 65 73 74 72 6f 79 20 41 20 50 72 65  F: Destroy A Pre
30e20 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20  pared Statement 
30e30 4f 62 6a 65 63 74 20 7b 48 31 33 33 30 30 7d 20  Object {H13300} 
30e40 3c 53 37 30 33 30 30 3e 3c 53 33 30 31 30 30 3e  <S70300><S30100>
30e50 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
30e60 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 20 66 75  e3_finalize() fu
30e70 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
30e80 20 74 6f 20 64 65 6c 65 74 65 20 61 20 5b 70 72   to delete a [pr
30e90 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
30ea0 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 61  ]..** If the sta
30eb0 74 65 6d 65 6e 74 20 77 61 73 20 65 78 65 63 75  tement was execu
30ec0 74 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79  ted successfully
30ed0 20 6f 72 20 6e 6f 74 20 65 78 65 63 75 74 65 64   or not executed
30ee0 20 61 74 20 61 6c 6c 2c 20 74 68 65 6e 0a 2a 2a   at all, then.**
30ef0 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
30f00 74 75 72 6e 65 64 2e 20 49 66 20 65 78 65 63 75  turned. If execu
30f10 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 74  tion of the stat
30f20 65 6d 65 6e 74 20 66 61 69 6c 65 64 20 74 68 65  ement failed the
30f30 6e 20 61 6e 0a 2a 2a 20 5b 65 72 72 6f 72 20 63  n an.** [error c
30f40 6f 64 65 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65  ode] or [extende
30f50 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73  d error code] is
30f60 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
30f70 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61   This routine ca
30f80 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61  n be called at a
30f90 6e 79 20 70 6f 69 6e 74 20 64 75 72 69 6e 67 20  ny point during 
30fa0 74 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66  the execution of
30fb0 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65   the.** [prepare
30fc0 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49  d statement].  I
30fd0 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61  f the virtual ma
30fe0 63 68 69 6e 65 20 68 61 73 20 6e 6f 74 0a 2a 2a  chine has not.**
30ff0 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 63 75   completed execu
31000 74 69 6f 6e 20 77 68 65 6e 20 74 68 69 73 20 72  tion when this r
31010 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
31020 2c 20 74 68 61 74 20 69 73 20 6c 69 6b 65 0a 2a  , that is like.*
31030 2a 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 61  * encountering a
31040 6e 20 65 72 72 6f 72 20 6f 72 20 61 6e 20 5b 73  n error or an [s
31050 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
31060 20 7c 20 69 6e 74 65 72 72 75 70 74 5d 2e 0a 2a   | interrupt]..*
31070 2a 20 49 6e 63 6f 6d 70 6c 65 74 65 20 75 70 64  * Incomplete upd
31080 61 74 65 73 20 6d 61 79 20 62 65 20 72 6f 6c 6c  ates may be roll
31090 65 64 20 62 61 63 6b 20 61 6e 64 20 74 72 61 6e  ed back and tran
310a0 73 61 63 74 69 6f 6e 73 20 63 61 6e 63 65 6c 65  sactions cancele
310b0 64 2c 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20  d,.** depending 
310c0 6f 6e 20 74 68 65 20 63 69 72 63 75 6d 73 74 61  on the circumsta
310d0 6e 63 65 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a  nces, and the.**
310e0 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 72 65   [error code] re
310f0 74 75 72 6e 65 64 20 77 69 6c 6c 20 62 65 20 5b  turned will be [
31100 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a  SQLITE_ABORT]..*
31110 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
31120 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 32 7d 20  .**.** {H11302} 
31130 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e  The [sqlite3_fin
31140 61 6c 69 7a 65 28 53 29 5d 20 69 6e 74 65 72 66  alize(S)] interf
31150 61 63 65 20 64 65 73 74 72 6f 79 73 20 74 68 65  ace destroys the
31160 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72  .**          [pr
31170 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
31180 5d 20 53 20 61 6e 64 20 72 65 6c 65 61 73 65 73  ] S and releases
31190 20 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20   all.**         
311a0 20 6d 65 6d 6f 72 79 20 61 6e 64 20 66 69 6c 65   memory and file
311b0 20 72 65 73 6f 75 72 63 65 73 20 68 65 6c 64 20   resources held 
311c0 62 79 20 74 68 61 74 20 6f 62 6a 65 63 74 2e 0a  by that object..
311d0 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 34 7d 20 49  **.** {H11304} I
311e0 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  f the most recen
311f0 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  t call to [sqlit
31200 65 33 5f 73 74 65 70 28 53 29 5d 20 66 6f 72 20  e3_step(S)] for 
31210 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
31220 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
31230 65 6e 74 5d 20 53 20 72 65 74 75 72 6e 65 64 20  ent] S returned 
31240 61 6e 20 65 72 72 6f 72 2c 0a 2a 2a 20 20 20 20  an error,.**    
31250 20 20 20 20 20 20 74 68 65 6e 20 5b 73 71 6c 69        then [sqli
31260 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 53 29 5d  te3_finalize(S)]
31270 20 72 65 74 75 72 6e 73 20 74 68 61 74 20 73 61   returns that sa
31280 6d 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c  me error..*/.SQL
31290 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
312a0 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71 6c  te3_finalize(sql
312b0 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
312c0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
312d0 45 46 3a 20 52 65 73 65 74 20 41 20 50 72 65 70  EF: Reset A Prep
312e0 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 4f  ared Statement O
312f0 62 6a 65 63 74 20 7b 48 31 33 33 33 30 7d 20 3c  bject {H13330} <
31300 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S70300>.**.** Th
31310 65 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  e sqlite3_reset(
31320 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  ) function is ca
31330 6c 6c 65 64 20 74 6f 20 72 65 73 65 74 20 61 20  lled to reset a 
31340 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
31350 65 6e 74 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 62  ent].** object b
31360 61 63 6b 20 74 6f 20 69 74 73 20 69 6e 69 74 69  ack to its initi
31370 61 6c 20 73 74 61 74 65 2c 20 72 65 61 64 79 20  al state, ready 
31380 74 6f 20 62 65 20 72 65 2d 65 78 65 63 75 74 65  to be re-execute
31390 64 2e 0a 2a 2a 20 41 6e 79 20 53 51 4c 20 73 74  d..** Any SQL st
313a0 61 74 65 6d 65 6e 74 20 76 61 72 69 61 62 6c 65  atement variable
313b0 73 20 74 68 61 74 20 68 61 64 20 76 61 6c 75 65  s that had value
313c0 73 20 62 6f 75 6e 64 20 74 6f 20 74 68 65 6d 20  s bound to them 
313d0 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 5b 73 71  using.** the [sq
313e0 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20  lite3_bind_blob 
313f0 7c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a  | sqlite3_bind_*
31400 28 29 20 41 50 49 5d 20 72 65 74 61 69 6e 20 74  () API] retain t
31410 68 65 69 72 20 76 61 6c 75 65 73 2e 0a 2a 2a 20  heir values..** 
31420 55 73 65 20 5b 73 71 6c 69 74 65 33 5f 63 6c 65  Use [sqlite3_cle
31430 61 72 5f 62 69 6e 64 69 6e 67 73 28 29 5d 20 74  ar_bindings()] t
31440 6f 20 72 65 73 65 74 20 74 68 65 20 62 69 6e 64  o reset the bind
31450 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31  ings..**.** {H11
31460 33 33 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  332} The [sqlite
31470 33 5f 72 65 73 65 74 28 53 29 5d 20 69 6e 74 65  3_reset(S)] inte
31480 72 66 61 63 65 20 72 65 73 65 74 73 20 74 68 65  rface resets the
31490 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
314a0 6d 65 6e 74 5d 20 53 0a 2a 2a 20 20 20 20 20 20  ment] S.**      
314b0 20 20 20 20 62 61 63 6b 20 74 6f 20 74 68 65 20      back to the 
314c0 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 69 74 73  beginning of its
314d0 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20   program..**.** 
314e0 7b 48 31 31 33 33 34 7d 20 49 66 20 74 68 65 20  {H11334} If the 
314f0 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c  most recent call
31500 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65   to [sqlite3_ste
31510 70 28 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a  p(S)] for the.**
31520 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61            [prepa
31530 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53  red statement] S
31540 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54   returned [SQLIT
31550 45 5f 52 4f 57 5d 20 6f 72 20 5b 53 51 4c 49 54  E_ROW] or [SQLIT
31560 45 5f 44 4f 4e 45 5d 2c 0a 2a 2a 20 20 20 20 20  E_DONE],.**     
31570 20 20 20 20 20 6f 72 20 69 66 20 5b 73 71 6c 69       or if [sqli
31580 74 65 33 5f 73 74 65 70 28 53 29 5d 20 68 61 73  te3_step(S)] has
31590 20 6e 65 76 65 72 20 62 65 66 6f 72 65 20 62 65   never before be
315a0 65 6e 20 63 61 6c 6c 65 64 20 6f 6e 20 53 2c 0a  en called on S,.
315b0 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 6e  **          then
315c0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28   [sqlite3_reset(
315d0 53 29 5d 20 72 65 74 75 72 6e 73 20 5b 53 51 4c  S)] returns [SQL
315e0 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 7b  ITE_OK]..**.** {
315f0 48 31 31 33 33 36 7d 20 49 66 20 74 68 65 20 6d  H11336} If the m
31600 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20  ost recent call 
31610 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  to [sqlite3_step
31620 28 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20  (S)] for the.** 
31630 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 72           [prepar
31640 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20  ed statement] S 
31650 69 6e 64 69 63 61 74 65 64 20 61 6e 20 65 72 72  indicated an err
31660 6f 72 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20  or, then.**     
31670 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 65       [sqlite3_re
31680 73 65 74 28 53 29 5d 20 72 65 74 75 72 6e 73 20  set(S)] returns 
31690 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b  an appropriate [
316a0 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a  error code]..**.
316b0 2a 2a 20 7b 48 31 31 33 33 38 7d 20 54 68 65 20  ** {H11338} The 
316c0 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53  [sqlite3_reset(S
316d0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 64 6f 65  )] interface doe
316e0 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65  s not change the
316f0 20 76 61 6c 75 65 73 0a 2a 2a 20 20 20 20 20 20   values.**      
31700 20 20 20 20 6f 66 20 61 6e 79 20 5b 73 71 6c 69      of any [sqli
31710 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 62 69  te3_bind_blob|bi
31720 6e 64 69 6e 67 73 5d 20 6f 6e 20 74 68 65 20 5b  ndings] on the [
31730 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
31740 6e 74 5d 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45  nt] S..*/.SQLITE
31750 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
31760 5f 72 65 73 65 74 28 73 71 6c 69 74 65 33 5f 73  _reset(sqlite3_s
31770 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a  tmt *pStmt);../*
31780 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 72  .** CAPI3REF: Cr
31790 65 61 74 65 20 4f 72 20 52 65 64 65 66 69 6e 65  eate Or Redefine
317a0 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 73 20 7b   SQL Functions {
317b0 48 31 36 31 30 30 7d 20 3c 53 32 30 32 30 30 3e  H16100} <S20200>
317c0 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 66  .** KEYWORDS: {f
317d0 75 6e 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e  unction creation
317e0 20 72 6f 75 74 69 6e 65 73 7d 0a 2a 2a 20 4b 45   routines}.** KE
317f0 59 57 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 61  YWORDS: {applica
31800 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c  tion-defined SQL
31810 20 66 75 6e 63 74 69 6f 6e 7d 0a 2a 2a 20 4b 45   function}.** KE
31820 59 57 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 61  YWORDS: {applica
31830 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c  tion-defined SQL
31840 20 66 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a   functions}.**.*
31850 2a 20 54 68 65 73 65 20 74 77 6f 20 66 75 6e 63  * These two func
31860 74 69 6f 6e 73 20 28 63 6f 6c 6c 65 63 74 69 76  tions (collectiv
31870 65 6c 79 20 6b 6e 6f 77 6e 20 61 73 20 22 66 75  ely known as "fu
31880 6e 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20  nction creation 
31890 72 6f 75 74 69 6e 65 73 22 29 0a 2a 2a 20 61 72  routines").** ar
318a0 65 20 75 73 65 64 20 74 6f 20 61 64 64 20 53 51  e used to add SQ
318b0 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 61  L functions or a
318c0 67 67 72 65 67 61 74 65 73 20 6f 72 20 74 6f 20  ggregates or to 
318d0 72 65 64 65 66 69 6e 65 20 74 68 65 20 62 65 68  redefine the beh
318e0 61 76 69 6f 72 0a 2a 2a 20 6f 66 20 65 78 69 73  avior.** of exis
318f0 74 69 6e 67 20 53 51 4c 20 66 75 6e 63 74 69 6f  ting SQL functio
31900 6e 73 20 6f 72 20 61 67 67 72 65 67 61 74 65 73  ns or aggregates
31910 2e 20 20 54 68 65 20 6f 6e 6c 79 20 64 69 66 66  .  The only diff
31920 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74  erence between t
31930 68 65 0a 2a 2a 20 74 77 6f 20 69 73 20 74 68 61  he.** two is tha
31940 74 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72  t the second par
31950 61 6d 65 74 65 72 2c 20 74 68 65 20 6e 61 6d 65  ameter, the name
31960 20 6f 66 20 74 68 65 20 28 73 63 61 6c 61 72 29   of the (scalar)
31970 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20   function or.** 
31980 61 67 67 72 65 67 61 74 65 2c 20 69 73 20 65 6e  aggregate, is en
31990 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 20 66  coded in UTF-8 f
319a0 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  or sqlite3_creat
319b0 65 5f 66 75 6e 63 74 69 6f 6e 28 29 20 61 6e 64  e_function() and
319c0 20 55 54 46 2d 31 36 0a 2a 2a 20 66 6f 72 20 73   UTF-16.** for s
319d0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
319e0 6e 63 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a  nction16()..**.*
319f0 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61  * The first para
31a00 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 64 61  meter is the [da
31a10 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
31a20 6e 5d 20 74 6f 20 77 68 69 63 68 20 74 68 65 20  n] to which the 
31a30 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  SQL.** function 
31a40 69 73 20 74 6f 20 62 65 20 61 64 64 65 64 2e 20  is to be added. 
31a50 20 49 66 20 61 20 73 69 6e 67 6c 65 20 70 72 6f   If a single pro
31a60 67 72 61 6d 20 75 73 65 73 20 6d 6f 72 65 20 74  gram uses more t
31a70 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65  han one database
31a80 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  .** connection i
31a90 6e 74 65 72 6e 61 6c 6c 79 2c 20 74 68 65 6e 20  nternally, then 
31aa0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6d 75  SQL functions mu
31ab0 73 74 20 62 65 20 61 64 64 65 64 20 69 6e 64 69  st be added indi
31ac0 76 69 64 75 61 6c 6c 79 20 74 6f 0a 2a 2a 20 65  vidually to.** e
31ad0 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ach database con
31ae0 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  nection..**.** T
31af0 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  he second parame
31b00 74 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20  ter is the name 
31b10 6f 66 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74  of the SQL funct
31b20 69 6f 6e 20 74 6f 20 62 65 20 63 72 65 61 74 65  ion to be create
31b30 64 20 6f 72 0a 2a 2a 20 72 65 64 65 66 69 6e 65  d or.** redefine
31b40 64 2e 20 20 54 68 65 20 6c 65 6e 67 74 68 20 6f  d.  The length o
31b50 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20 6c 69  f the name is li
31b60 6d 69 74 65 64 20 74 6f 20 32 35 35 20 62 79 74  mited to 255 byt
31b70 65 73 2c 20 65 78 63 6c 75 73 69 76 65 20 6f 66  es, exclusive of
31b80 0a 2a 2a 20 74 68 65 20 7a 65 72 6f 2d 74 65 72  .** the zero-ter
31b90 6d 69 6e 61 74 6f 72 2e 20 20 4e 6f 74 65 20 74  minator.  Note t
31ba0 68 61 74 20 74 68 65 20 6e 61 6d 65 20 6c 65 6e  hat the name len
31bb0 67 74 68 20 6c 69 6d 69 74 20 69 73 20 69 6e 20  gth limit is in 
31bc0 62 79 74 65 73 2c 20 6e 6f 74 0a 2a 2a 20 63 68  bytes, not.** ch
31bd0 61 72 61 63 74 65 72 73 2e 20 20 41 6e 79 20 61  aracters.  Any a
31be0 74 74 65 6d 70 74 20 74 6f 20 63 72 65 61 74 65  ttempt to create
31bf0 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68   a function with
31c00 20 61 20 6c 6f 6e 67 65 72 20 6e 61 6d 65 0a 2a   a longer name.*
31c10 2a 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e  * will result in
31c20 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20   [SQLITE_ERROR] 
31c30 62 65 69 6e 67 20 72 65 74 75 72 6e 65 64 2e 0a  being returned..
31c40 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20  **.** The third 
31c50 70 61 72 61 6d 65 74 65 72 20 28 6e 41 72 67 29  parameter (nArg)
31c60 0a 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 65  .** is the numbe
31c70 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74  r of arguments t
31c80 68 61 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63  hat the SQL func
31c90 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65  tion or.** aggre
31ca0 67 61 74 65 20 74 61 6b 65 73 2e 20 49 66 20 74  gate takes. If t
31cb0 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 69 73  his parameter is
31cc0 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20   negative, then 
31cd0 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  the SQL function
31ce0 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65   or.** aggregate
31cf0 20 6d 61 79 20 74 61 6b 65 20 61 6e 79 20 6e 75   may take any nu
31d00 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
31d10 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 75  s..**.** The fou
31d20 72 74 68 20 70 61 72 61 6d 65 74 65 72 2c 20 65  rth parameter, e
31d30 54 65 78 74 52 65 70 2c 20 73 70 65 63 69 66 69  TextRep, specifi
31d40 65 73 20 77 68 61 74 0a 2a 2a 20 5b 53 51 4c 49  es what.** [SQLI
31d50 54 45 5f 55 54 46 38 20 7c 20 74 65 78 74 20 65  TE_UTF8 | text e
31d60 6e 63 6f 64 69 6e 67 5d 20 74 68 69 73 20 53 51  ncoding] this SQ
31d70 4c 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 66 65  L function prefe
31d80 72 73 20 66 6f 72 0a 2a 2a 20 69 74 73 20 70 61  rs for.** its pa
31d90 72 61 6d 65 74 65 72 73 2e 20 20 41 6e 79 20 53  rameters.  Any S
31da0 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c  QL function impl
31db0 65 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75 6c  ementation shoul
31dc0 64 20 62 65 20 61 62 6c 65 20 74 6f 20 77 6f 72  d be able to wor
31dd0 6b 0a 2a 2a 20 77 6f 72 6b 20 77 69 74 68 20 55  k.** work with U
31de0 54 46 2d 38 2c 20 55 54 46 2d 31 36 6c 65 2c 20  TF-8, UTF-16le, 
31df0 6f 72 20 55 54 46 2d 31 36 62 65 2e 20 20 42 75  or UTF-16be.  Bu
31e00 74 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e 74  t some implement
31e10 61 74 69 6f 6e 73 20 6d 61 79 20 62 65 0a 2a 2a  ations may be.**
31e20 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 20   more efficient 
31e30 77 69 74 68 20 6f 6e 65 20 65 6e 63 6f 64 69 6e  with one encodin
31e40 67 20 74 68 61 6e 20 61 6e 6f 74 68 65 72 2e 20  g than another. 
31e50 20 49 74 20 69 73 20 61 6c 6c 6f 77 65 64 20 74   It is allowed t
31e60 6f 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73 71 6c 69  o.** invoke sqli
31e70 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
31e80 69 6f 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 33  ion() or sqlite3
31e90 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
31ea0 31 36 28 29 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a  16() multiple.**
31eb0 20 74 69 6d 65 73 20 77 69 74 68 20 74 68 65 20   times with the 
31ec0 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 62 75  same function bu
31ed0 74 20 77 69 74 68 20 64 69 66 66 65 72 65 6e 74  t with different
31ee0 20 76 61 6c 75 65 73 20 6f 66 20 65 54 65 78 74   values of eText
31ef0 52 65 70 2e 0a 2a 2a 20 57 68 65 6e 20 6d 75 6c  Rep..** When mul
31f00 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  tiple implementa
31f10 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d  tions of the sam
31f20 65 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 61  e function are a
31f30 76 61 69 6c 61 62 6c 65 2c 20 53 51 4c 69 74 65  vailable, SQLite
31f40 0a 2a 2a 20 77 69 6c 6c 20 70 69 63 6b 20 74 68  .** will pick th
31f50 65 20 6f 6e 65 20 74 68 61 74 20 69 6e 76 6f 6c  e one that invol
31f60 76 65 73 20 74 68 65 20 6c 65 61 73 74 20 61 6d  ves the least am
31f70 6f 75 6e 74 20 6f 66 20 64 61 74 61 20 63 6f 6e  ount of data con
31f80 76 65 72 73 69 6f 6e 2e 0a 2a 2a 20 49 66 20 74  version..** If t
31f90 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61 20 73  here is only a s
31fa0 69 6e 67 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  ingle implementa
31fb0 74 69 6f 6e 20 77 68 69 63 68 20 64 6f 65 73 20  tion which does 
31fc0 6e 6f 74 20 63 61 72 65 20 77 68 61 74 20 74 65  not care what te
31fd0 78 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 20 69  xt.** encoding i
31fe0 73 20 75 73 65 64 2c 20 74 68 65 6e 20 74 68 65  s used, then the
31ff0 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74   fourth argument
32000 20 73 68 6f 75 6c 64 20 62 65 20 5b 53 51 4c 49   should be [SQLI
32010 54 45 5f 41 4e 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54  TE_ANY]..**.** T
32020 68 65 20 66 69 66 74 68 20 70 61 72 61 6d 65 74  he fifth paramet
32030 65 72 20 69 73 20 61 6e 20 61 72 62 69 74 72 61  er is an arbitra
32040 72 79 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 65  ry pointer.  The
32050 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
32060 6f 66 20 74 68 65 0a 2a 2a 20 66 75 6e 63 74 69  of the.** functi
32070 6f 6e 20 63 61 6e 20 67 61 69 6e 20 61 63 63 65  on can gain acce
32080 73 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74  ss to this point
32090 65 72 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65  er using [sqlite
320a0 33 5f 75 73 65 72 5f 64 61 74 61 28 29 5d 2e 0a  3_user_data()]..
320b0 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 76 65 6e 74  **.** The sevent
320c0 68 2c 20 65 69 67 68 74 68 20 61 6e 64 20 6e 69  h, eighth and ni
320d0 6e 74 68 20 70 61 72 61 6d 65 74 65 72 73 2c 20  nth parameters, 
320e0 78 46 75 6e 63 2c 20 78 53 74 65 70 20 61 6e 64  xFunc, xStep and
320f0 20 78 46 69 6e 61 6c 2c 20 61 72 65 0a 2a 2a 20   xFinal, are.** 
32100 70 6f 69 6e 74 65 72 73 20 74 6f 20 43 2d 6c 61  pointers to C-la
32110 6e 67 75 61 67 65 20 66 75 6e 63 74 69 6f 6e 73  nguage functions
32120 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
32130 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  the SQL function
32140 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65   or.** aggregate
32150 2e 20 41 20 73 63 61 6c 61 72 20 53 51 4c 20 66  . A scalar SQL f
32160 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65 73  unction requires
32170 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69   an implementati
32180 6f 6e 20 6f 66 20 74 68 65 20 78 46 75 6e 63 0a  on of the xFunc.
32190 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 6c 79  ** callback only
321a0 2c 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 20  , NULL pointers 
321b0 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64  should be passed
321c0 20 61 73 20 74 68 65 20 78 53 74 65 70 20 61 6e   as the xStep an
321d0 64 20 78 46 69 6e 61 6c 0a 2a 2a 20 70 61 72 61  d xFinal.** para
321e0 6d 65 74 65 72 73 2e 20 41 6e 20 61 67 67 72 65  meters. An aggre
321f0 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69 6f  gate SQL functio
32200 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20 69 6d  n requires an im
32210 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
32220 78 53 74 65 70 0a 2a 2a 20 61 6e 64 20 78 46 69  xStep.** and xFi
32230 6e 61 6c 20 61 6e 64 20 4e 55 4c 4c 20 73 68 6f  nal and NULL sho
32240 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 66 6f  uld be passed fo
32250 72 20 78 46 75 6e 63 2e 20 54 6f 20 64 65 6c 65  r xFunc. To dele
32260 74 65 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 2a  te an existing.*
32270 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f  * SQL function o
32280 72 20 61 67 67 72 65 67 61 74 65 2c 20 70 61 73  r aggregate, pas
32290 73 20 4e 55 4c 4c 20 66 6f 72 20 61 6c 6c 20 74  s NULL for all t
322a0 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 20 63 61  hree function ca
322b0 6c 6c 62 61 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49  llbacks..**.** I
322c0 74 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 74  t is permitted t
322d0 6f 20 72 65 67 69 73 74 65 72 20 6d 75 6c 74 69  o register multi
322e0 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ple implementati
322f0 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 0a  ons of the same.
32300 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 77 69 74  ** functions wit
32310 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 20  h the same name 
32320 62 75 74 20 77 69 74 68 20 65 69 74 68 65 72 20  but with either 
32330 64 69 66 66 65 72 69 6e 67 20 6e 75 6d 62 65 72  differing number
32340 73 20 6f 66 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  s of.** argument
32350 73 20 6f 72 20 64 69 66 66 65 72 69 6e 67 20 70  s or differing p
32360 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65 6e  referred text en
32370 63 6f 64 69 6e 67 73 2e 20 20 53 51 4c 69 74 65  codings.  SQLite
32380 20 77 69 6c 6c 20 75 73 65 0a 2a 2a 20 74 68 65   will use.** the
32390 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
323a0 6d 6f 73 74 20 63 6c 6f 73 65 6c 79 20 6d 61 74  most closely mat
323b0 63 68 65 73 20 74 68 65 20 77 61 79 20 69 6e 20  ches the way in 
323c0 77 68 69 63 68 20 74 68 65 0a 2a 2a 20 53 51 4c  which the.** SQL
323d0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
323e0 64 2e 20 20 41 20 66 75 6e 63 74 69 6f 6e 20 69  d.  A function i
323f0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69  mplementation wi
32400 74 68 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69 76  th a non-negativ
32410 65 0a 2a 2a 20 6e 41 72 67 20 70 61 72 61 6d 65  e.** nArg parame
32420 74 65 72 20 69 73 20 61 20 62 65 74 74 65 72 20  ter is a better 
32430 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e  match than a fun
32440 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61  ction implementa
32450 74 69 6f 6e 20 77 69 74 68 0a 2a 2a 20 61 20 6e  tion with.** a n
32460 65 67 61 74 69 76 65 20 6e 41 72 67 2e 20 20 41  egative nArg.  A
32470 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20   function where 
32480 74 68 65 20 70 72 65 66 65 72 72 65 64 20 74 65  the preferred te
32490 78 74 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 6d  xt encoding.** m
324a0 61 74 63 68 65 73 20 74 68 65 20 64 61 74 61 62  atches the datab
324b0 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20  ase encoding is 
324c0 61 20 62 65 74 74 65 72 0a 2a 2a 20 6d 61 74 63  a better.** matc
324d0 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74 69 6f  h than a functio
324e0 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 6f  n where the enco
324f0 64 69 6e 67 20 69 73 20 64 69 66 66 65 72 65 6e  ding is differen
32500 74 2e 20 20 0a 2a 2a 20 41 20 66 75 6e 63 74 69  t.  .** A functi
32510 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63  on where the enc
32520 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e 63 65  oding difference
32530 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 46 31   is between UTF1
32540 36 6c 65 20 61 6e 64 20 55 54 46 31 36 62 65 0a  6le and UTF16be.
32550 2a 2a 20 69 73 20 61 20 63 6c 6f 73 65 72 20 6d  ** is a closer m
32560 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63  atch than a func
32570 74 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65  tion where the e
32580 6e 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e  ncoding differen
32590 63 65 20 69 73 0a 2a 2a 20 62 65 74 77 65 65 6e  ce is.** between
325a0 20 55 54 46 38 20 61 6e 64 20 55 54 46 31 36 2e   UTF8 and UTF16.
325b0 0a 2a 2a 0a 2a 2a 20 42 75 69 6c 74 2d 69 6e 20  .**.** Built-in 
325c0 66 75 6e 63 74 69 6f 6e 73 20 6d 61 79 20 62 65  functions may be
325d0 20 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20 6e   overloaded by n
325e0 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  ew application-d
325f0 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73  efined functions
32600 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61  ..** The first a
32610 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
32620 65 64 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68  ed function with
32630 20 61 20 67 69 76 65 6e 20 6e 61 6d 65 20 6f 76   a given name ov
32640 65 72 72 69 64 65 73 20 61 6c 6c 0a 2a 2a 20 62  errides all.** b
32650 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e  uilt-in function
32660 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 5b 64  s in the same [d
32670 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
32680 6f 6e 5d 20 77 69 74 68 20 74 68 65 20 73 61 6d  on] with the sam
32690 65 20 6e 61 6d 65 2e 0a 2a 2a 20 53 75 62 73 65  e name..** Subse
326a0 71 75 65 6e 74 20 61 70 70 6c 69 63 61 74 69 6f  quent applicatio
326b0 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  n-defined functi
326c0 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20  ons of the same 
326d0 6e 61 6d 65 20 6f 6e 6c 79 20 6f 76 65 72 72 69  name only overri
326e0 64 65 20 0a 2a 2a 20 70 72 69 6f 72 20 61 70 70  de .** prior app
326f0 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
32700 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20   functions that 
32710 61 72 65 20 61 6e 20 65 78 61 63 74 20 6d 61 74  are an exact mat
32720 63 68 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6e 75  ch for the.** nu
32730 6d 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74 65  mber of paramete
32740 72 73 20 61 6e 64 20 70 72 65 66 65 72 72 65 64  rs and preferred
32750 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a   encoding..**.**
32760 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d   An application-
32770 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
32780 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 74 6f   is permitted to
32790 20 63 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 53   call other.** S
327a0 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73  QLite interfaces
327b0 2e 20 20 48 6f 77 65 76 65 72 2c 20 73 75 63 68  .  However, such
327c0 20 63 61 6c 6c 73 20 6d 75 73 74 20 6e 6f 74 0a   calls must not.
327d0 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74  ** close the dat
327e0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
327f0 20 6e 6f 72 20 66 69 6e 61 6c 69 7a 65 20 6f 72   nor finalize or
32800 20 72 65 73 65 74 20 74 68 65 20 70 72 65 70 61   reset the prepa
32810 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  red.** statement
32820 20 69 6e 20 77 68 69 63 68 20 74 68 65 20 66 75   in which the fu
32830 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e  nction is runnin
32840 67 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  g..**.** INVARIA
32850 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31  NTS:.**.** {H161
32860 30 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  03} The [sqlite3
32870 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
32880 31 36 28 44 2c 58 2c 2e 2e 2e 29 5d 20 69 6e 74  16(D,X,...)] int
32890 65 72 66 61 63 65 20 73 68 61 6c 6c 20 62 65 68  erface shall beh
328a0 61 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ave.**          
328b0 61 73 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61  as [sqlite3_crea
328c0 74 65 5f 66 75 6e 63 74 69 6f 6e 28 44 2c 58 2c  te_function(D,X,
328d0 2e 2e 2e 29 5d 20 69 6e 20 65 76 65 72 79 20 77  ...)] in every w
328e0 61 79 20 65 78 63 65 70 74 20 74 68 61 74 20 69  ay except that i
328f0 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e  t.**          in
32900 74 65 72 70 72 65 74 73 20 74 68 65 20 58 20 61  terprets the X a
32910 72 67 75 6d 65 6e 74 20 61 73 20 7a 65 72 6f 2d  rgument as zero-
32920 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 31  terminated UTF-1
32930 36 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 61  6.**          na
32940 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20  tive byte order 
32950 69 6e 73 74 65 61 64 20 6f 66 20 61 73 20 7a 65  instead of as ze
32960 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54  ro-terminated UT
32970 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31  F-8..**.** {H161
32980 30 36 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c  06} A successful
32990 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74   invocation of t
329a0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  he.**          [
329b0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
329c0 75 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c  unction(D,X,N,E,
329d0 2e 2e 2e 29 5d 20 69 6e 74 65 72 66 61 63 65 20  ...)] interface 
329e0 73 68 61 6c 6c 20 72 65 67 69 73 74 65 72 0a 2a  shall register.*
329f0 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 72 65  *          or re
32a00 70 6c 61 63 65 73 20 63 61 6c 6c 62 61 63 6b 20  places callback 
32a10 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 65  functions in the
32a20 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
32a30 63 74 69 6f 6e 5d 20 44 0a 2a 2a 20 20 20 20 20  ction] D.**     
32a40 20 20 20 20 20 75 73 65 64 20 74 6f 20 69 6d 70       used to imp
32a50 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20 66  lement the SQL f
32a60 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20 58 20  unction named X 
32a70 77 69 74 68 20 4e 20 70 61 72 61 6d 65 74 65 72  with N parameter
32a80 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e  s.**          an
32a90 64 20 68 61 76 69 6e 67 20 61 20 70 72 65 66 65  d having a prefe
32aa0 72 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69  rred text encodi
32ab0 6e 67 20 6f 66 20 45 2e 0a 2a 2a 0a 2a 2a 20 7b  ng of E..**.** {
32ac0 48 31 36 31 30 39 7d 20 41 20 73 75 63 63 65 73  H16109} A succes
32ad0 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71  sful call to [sq
32ae0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
32af0 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 50 2c  ction(D,X,N,E,P,
32b00 46 2c 53 2c 4c 29 5d 0a 2a 2a 20 20 20 20 20 20  F,S,L)].**      
32b10 20 20 20 20 73 68 61 6c 6c 20 72 65 70 6c 61 63      shall replac
32b20 65 20 74 68 65 20 50 2c 20 46 2c 20 53 2c 20 61  e the P, F, S, a
32b30 6e 64 20 4c 20 76 61 6c 75 65 73 20 66 72 6f 6d  nd L values from
32b40 20 61 6e 79 20 70 72 69 6f 72 20 63 61 6c 6c 73   any prior calls
32b50 20 77 69 74 68 0a 2a 2a 20 20 20 20 20 20 20 20   with.**        
32b60 20 20 74 68 65 20 73 61 6d 65 20 44 2c 20 58 2c    the same D, X,
32b70 20 4e 2c 20 61 6e 64 20 45 20 76 61 6c 75 65 73   N, and E values
32b80 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 31 32 7d  ..**.** {H16112}
32b90 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72   The [sqlite3_cr
32ba0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 44 2c  eate_function(D,
32bb0 58 2c 2e 2e 2e 29 5d 20 69 6e 74 65 72 66 61 63  X,...)] interfac
32bc0 65 20 73 68 61 6c 6c 20 66 61 69 6c 0a 2a 2a 20  e shall fail.** 
32bd0 20 20 20 20 20 20 20 20 20 69 66 20 74 68 65 20           if the 
32be0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d  SQL function nam
32bf0 65 20 58 20 69 73 0a 2a 2a 20 20 20 20 20 20 20  e X is.**       
32c00 20 20 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 32     longer than 2
32c10 35 35 20 62 79 74 65 73 20 65 78 63 6c 75 73 69  55 bytes exclusi
32c20 76 65 20 6f 66 20 74 68 65 20 7a 65 72 6f 20 74  ve of the zero t
32c30 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a  erminator..**.**
32c40 20 7b 48 31 36 31 31 38 7d 20 54 68 65 20 5b 73   {H16118} The [s
32c50 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
32c60 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 50  nction(D,X,N,E,P
32c70 2c 46 2c 53 2c 4c 29 5d 20 69 6e 74 65 72 66 61  ,F,S,L)] interfa
32c80 63 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73  ce.**          s
32c90 68 61 6c 6c 20 66 61 69 6c 20 75 6e 6c 65 73 73  hall fail unless
32ca0 20 65 69 74 68 65 72 20 46 20 69 73 20 4e 55 4c   either F is NUL
32cb0 4c 20 61 6e 64 20 53 20 61 6e 64 20 4c 20 61 72  L and S and L ar
32cc0 65 20 6e 6f 6e 2d 4e 55 4c 4c 20 6f 72 0a 2a 2a  e non-NULL or.**
32cd0 2a 20 20 20 20 20 20 20 20 20 46 20 69 73 20 6e  *         F is n
32ce0 6f 6e 2d 4e 55 4c 4c 20 61 6e 64 20 53 20 61 6e  on-NULL and S an
32cf0 64 20 4c 20 61 72 65 20 4e 55 4c 4c 2e 0a 2a 2a  d L are NULL..**
32d00 0a 2a 2a 20 7b 48 31 36 31 32 31 7d 20 54 68 65  .** {H16121} The
32d10 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
32d20 5f 66 75 6e 63 74 69 6f 6e 28 44 2c 2e 2e 2e 29  _function(D,...)
32d30 5d 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c  ] interface shal
32d40 6c 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 0a  l fails with an.
32d50 2a 2a 20 20 20 20 20 20 20 20 20 20 65 72 72 6f  **          erro
32d60 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54  r code of [SQLIT
32d70 45 5f 42 55 53 59 5d 20 69 66 20 74 68 65 72 65  E_BUSY] if there
32d80 20 65 78 69 73 74 20 5b 70 72 65 70 61 72 65 64   exist [prepared
32d90 20 73 74 61 74 65 6d 65 6e 74 73 5d 0a 2a 2a 20   statements].** 
32da0 20 20 20 20 20 20 20 20 20 61 73 73 6f 63 69 61           associa
32db0 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 61  ted with the [da
32dc0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
32dd0 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36  n] D..**.** {H16
32de0 31 32 34 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  124} The [sqlite
32df0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
32e00 6e 28 44 2c 58 2c 4e 2c 2e 2e 2e 29 5d 20 69 6e  n(D,X,N,...)] in
32e10 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 66 61  terface shall fa
32e20 69 6c 20 77 69 74 68 0a 2a 2a 20 20 20 20 20 20  il with.**      
32e30 20 20 20 20 61 6e 20 65 72 72 6f 72 20 63 6f 64      an error cod
32e40 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 45 52 52  e of [SQLITE_ERR
32e50 4f 52 5d 20 69 66 20 70 61 72 61 6d 65 74 65 72  OR] if parameter
32e60 20 4e 20 69 73 20 6c 65 73 73 0a 2a 2a 20 20 20   N is less.**   
32e70 20 20 20 20 20 20 20 74 68 61 6e 20 2d 31 20 6f         than -1 o
32e80 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 31  r greater than 1
32e90 32 37 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 32  27..**.** {H1612
32ea0 37 7d 20 57 68 65 6e 20 4e 20 69 73 20 6e 6f 6e  7} When N is non
32eb0 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20 5b  -negative, the [
32ec0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
32ed0 75 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 2e 2e  unction(D,X,N,..
32ee0 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .)].**          
32ef0 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20  interface shall 
32f00 72 65 67 69 73 74 65 72 20 63 61 6c 6c 62 61 63  register callbac
32f10 6b 73 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64  ks to be invoked
32f20 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20   for the.**     
32f30 20 20 20 20 20 53 51 4c 20 66 75 6e 63 74 69 6f       SQL functio
32f40 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 61  n.**          na
32f50 6d 65 64 20 58 20 77 68 65 6e 20 74 68 65 20 6e  med X when the n
32f60 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
32f70 74 73 20 74 6f 20 74 68 65 20 53 51 4c 20 66 75  ts to the SQL fu
32f80 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 20 20 20  nction is.**    
32f90 20 20 20 20 20 20 65 78 61 63 74 6c 79 20 4e 2e        exactly N.
32fa0 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 33 30 7d 20  .**.** {H16130} 
32fb0 57 68 65 6e 20 4e 20 69 73 20 2d 31 2c 20 74 68  When N is -1, th
32fc0 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74  e [sqlite3_creat
32fd0 65 5f 66 75 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e  e_function(D,X,N
32fe0 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20  ,...)].**       
32ff0 20 20 20 69 6e 74 65 72 66 61 63 65 20 73 68 61     interface sha
33000 6c 6c 20 72 65 67 69 73 74 65 72 20 63 61 6c 6c  ll register call
33010 62 61 63 6b 73 20 74 6f 20 62 65 20 69 6e 76 6f  backs to be invo
33020 6b 65 64 20 66 6f 72 20 74 68 65 20 53 51 4c 0a  ked for the SQL.
33030 2a 2a 20 20 20 20 20 20 20 20 20 20 66 75 6e 63  **          func
33040 74 69 6f 6e 20 6e 61 6d 65 64 20 58 20 77 69 74  tion named X wit
33050 68 20 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20  h any number of 
33060 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a  arguments..**.**
33070 20 7b 48 31 36 31 33 33 7d 20 57 68 65 6e 20 63   {H16133} When c
33080 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33  alls to [sqlite3
33090 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
330a0 28 44 2c 58 2c 4e 2c 2e 2e 2e 29 5d 0a 2a 2a 20  (D,X,N,...)].** 
330b0 20 20 20 20 20 20 20 20 20 73 70 65 63 69 66 79           specify
330c0 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d   multiple implem
330d0 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65  entations of the
330e0 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 58   same function X
330f0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e 64  .**          and
33100 20 77 68 65 6e 20 6f 6e 65 20 69 6d 70 6c 65 6d   when one implem
33110 65 6e 74 61 74 69 6f 6e 20 68 61 73 20 4e 3e 3d  entation has N>=
33120 30 20 61 6e 64 20 74 68 65 20 6f 74 68 65 72 20  0 and the other 
33130 68 61 73 20 4e 3d 28 2d 31 29 0a 2a 2a 20 20 20  has N=(-1).**   
33140 20 20 20 20 20 20 20 74 68 65 20 69 6d 70 6c 65         the imple
33150 6d 65 6e 74 61 74 69 6f 6e 20 77 69 74 68 20 61  mentation with a
33160 20 6e 6f 6e 2d 7a 65 72 6f 20 4e 20 73 68 61 6c   non-zero N shal
33170 6c 20 62 65 20 70 72 65 66 65 72 72 65 64 2e 0a  l be preferred..
33180 2a 2a 0a 2a 2a 20 7b 48 31 36 31 33 36 7d 20 57  **.** {H16136} W
33190 68 65 6e 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71  hen calls to [sq
331a0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
331b0 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 2e 2e  ction(D,X,N,E,..
331c0 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .)].**          
331d0 73 70 65 63 69 66 79 20 6d 75 6c 74 69 70 6c 65  specify multiple
331e0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
331f0 20 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e   of the same fun
33200 63 74 69 6f 6e 20 58 20 77 69 74 68 0a 2a 2a 20  ction X with.** 
33210 20 20 20 20 20 20 20 20 20 74 68 65 20 73 61 6d           the sam
33220 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  e number of argu
33230 6d 65 6e 74 73 20 4e 20 62 75 74 20 77 69 74 68  ments N but with
33240 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 20 20   different.**   
33250 20 20 20 20 20 20 20 65 6e 63 6f 64 69 6e 67 73         encodings
33260 20 45 2c 20 74 68 65 6e 20 74 68 65 20 69 6d 70   E, then the imp
33270 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 68 65 72  lementation wher
33280 65 20 45 20 6d 61 74 63 68 65 73 20 74 68 65 0a  e E matches the.
33290 2a 2a 20 20 20 20 20 20 20 20 20 20 64 61 74 61  **          data
332a0 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 73 68  base encoding sh
332b0 61 6c 6c 20 70 72 65 66 65 72 72 65 64 2e 0a 2a  all preferred..*
332c0 2a 0a 2a 2a 20 7b 48 31 36 31 33 39 7d 20 46 6f  *.** {H16139} Fo
332d0 72 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 53  r an aggregate S
332e0 51 4c 20 66 75 6e 63 74 69 6f 6e 20 63 72 65 61  QL function crea
332f0 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 20 20 20  ted using.**    
33300 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 63        [sqlite3_c
33310 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 44  reate_function(D
33320 2c 58 2c 4e 2c 45 2c 50 2c 30 2c 53 2c 4c 29 5d  ,X,N,E,P,0,S,L)]
33330 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 0a 2a   the finalizer.*
33340 2a 20 20 20 20 20 20 20 20 20 20 66 75 6e 63 74  *          funct
33350 69 6f 6e 20 4c 20 73 68 61 6c 6c 20 61 6c 77 61  ion L shall alwa
33360 79 73 20 62 65 20 69 6e 76 6f 6b 65 64 20 65 78  ys be invoked ex
33370 61 63 74 6c 79 20 6f 6e 63 65 20 69 66 20 74 68  actly once if th
33380 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74  e.**          st
33390 65 70 20 66 75 6e 63 74 69 6f 6e 20 53 20 69 73  ep function S is
333a0 20 63 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20 6d   called one or m
333b0 6f 72 65 20 74 69 6d 65 73 2e 0a 2a 2a 0a 2a 2a  ore times..**.**
333c0 20 7b 48 31 36 31 34 32 7d 20 57 68 65 6e 20 53   {H16142} When S
333d0 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 65 69  QLite invokes ei
333e0 74 68 65 72 20 74 68 65 20 78 46 75 6e 63 20 6f  ther the xFunc o
333f0 72 20 78 53 74 65 70 20 66 75 6e 63 74 69 6f 6e  r xStep function
33400 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   of.**          
33410 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  an application-d
33420 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 74  efined SQL funct
33430 69 6f 6e 20 6f 72 20 61 67 67 72 65 67 61 74 65  ion or aggregate
33440 20 63 72 65 61 74 65 64 0a 2a 2a 20 20 20 20 20   created.**     
33450 20 20 20 20 20 62 79 20 5b 73 71 6c 69 74 65 33       by [sqlite3
33460 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
33470 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ()] or [sqlite3_
33480 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31  create_function1
33490 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20  6()],.**        
334a0 20 20 74 68 65 6e 20 74 68 65 20 61 72 72 61 79    then the array
334b0 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c   of [sqlite3_val
334c0 75 65 5d 20 6f 62 6a 65 63 74 73 20 70 61 73 73  ue] objects pass
334d0 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 20 20 20  ed as the.**    
334e0 20 20 20 20 20 20 74 68 69 72 64 20 70 61 72 61        third para
334f0 6d 65 74 65 72 20 73 68 61 6c 6c 20 62 65 20 5b  meter shall be [
33500 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
33510 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73  3_value] objects
33520 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
33530 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
33540 74 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 73  te_function(.  s
33550 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f  qlite3 *db,.  co
33560 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 74  nst char *zFunct
33570 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e  ionName,.  int n
33580 41 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78 74  Arg,.  int eText
33590 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 41 70  Rep,.  void *pAp
335a0 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e  p,.  void (*xFun
335b0 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  c)(sqlite3_conte
335c0 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
335d0 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64  value**),.  void
335e0 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65   (*xStep)(sqlite
335f0 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
33600 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c  qlite3_value**),
33610 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c  .  void (*xFinal
33620 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
33630 74 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  t*).);.SQLITE_AP
33640 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72  I int sqlite3_cr
33650 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28  eate_function16(
33660 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a  .  sqlite3 *db,.
33670 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46    const void *zF
33680 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69  unctionName,.  i
33690 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65  nt nArg,.  int e
336a0 54 65 78 74 52 65 70 2c 0a 20 20 76 6f 69 64 20  TextRep,.  void 
336b0 2a 70 41 70 70 2c 0a 20 20 76 6f 69 64 20 28 2a  *pApp,.  void (*
336c0 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63  xFunc)(sqlite3_c
336d0 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
336e0 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20  te3_value**),.  
336f0 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71  void (*xStep)(sq
33700 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
33710 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
33720 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46  **),.  void (*xF
33730 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f  inal)(sqlite3_co
33740 6e 74 65 78 74 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a  ntext*).);../*.*
33750 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65 78 74  * CAPI3REF: Text
33760 20 45 6e 63 6f 64 69 6e 67 73 20 7b 48 31 30 32   Encodings {H102
33770 36 37 7d 20 3c 53 35 30 32 30 30 3e 20 3c 48 31  67} <S50200> <H1
33780 36 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  6100>.**.** Thes
33790 65 20 63 6f 6e 73 74 61 6e 74 20 64 65 66 69 6e  e constant defin
337a0 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73 20  e integer codes 
337b0 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 20 74  that represent t
337c0 68 65 20 76 61 72 69 6f 75 73 0a 2a 2a 20 74 65  he various.** te
337d0 78 74 20 65 6e 63 6f 64 69 6e 67 73 20 73 75 70  xt encodings sup
337e0 70 6f 72 74 65 64 20 62 79 20 53 51 4c 69 74 65  ported by SQLite
337f0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
33800 49 54 45 5f 55 54 46 38 20 20 20 20 20 20 20 20  ITE_UTF8        
33810 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c     1.#define SQL
33820 49 54 45 5f 55 54 46 31 36 4c 45 20 20 20 20 20  ITE_UTF16LE     
33830 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c     2.#define SQL
33840 49 54 45 5f 55 54 46 31 36 42 45 20 20 20 20 20  ITE_UTF16BE     
33850 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c     3.#define SQL
33860 49 54 45 5f 55 54 46 31 36 20 20 20 20 20 20 20  ITE_UTF16       
33870 20 20 20 34 20 20 20 20 2f 2a 20 55 73 65 20 6e     4    /* Use n
33880 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72  ative byte order
33890 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
338a0 54 45 5f 41 4e 59 20 20 20 20 20 20 20 20 20 20  TE_ANY          
338b0 20 20 35 20 20 20 20 2f 2a 20 73 71 6c 69 74 65    5    /* sqlite
338c0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
338d0 6e 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e  n only */.#defin
338e0 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41  e SQLITE_UTF16_A
338f0 4c 49 47 4e 45 44 20 20 38 20 20 20 20 2f 2a 20  LIGNED  8    /* 
33900 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
33910 6f 6c 6c 61 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f  ollation only */
33920 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
33930 3a 20 44 65 70 72 65 63 61 74 65 64 20 46 75 6e  : Deprecated Fun
33940 63 74 69 6f 6e 73 0a 2a 2a 20 44 45 50 52 45 43  ctions.** DEPREC
33950 41 54 45 44 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  ATED.**.** These
33960 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 5b   functions are [
33970 64 65 70 72 65 63 61 74 65 64 5d 2e 20 20 49 6e  deprecated].  In
33980 20 6f 72 64 65 72 20 74 6f 20 6d 61 69 6e 74 61   order to mainta
33990 69 6e 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73 20  in.** backwards 
339a0 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69  compatibility wi
339b0 74 68 20 6f 6c 64 65 72 20 63 6f 64 65 2c 20 74  th older code, t
339c0 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 63  hese functions c
339d0 6f 6e 74 69 6e 75 65 20 0a 2a 2a 20 74 6f 20 62  ontinue .** to b
339e0 65 20 73 75 70 70 6f 72 74 65 64 2e 20 20 48 6f  e supported.  Ho
339f0 77 65 76 65 72 2c 20 6e 65 77 20 61 70 70 6c 69  wever, new appli
33a00 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 61  cations should a
33a10 76 6f 69 64 0a 2a 2a 20 74 68 65 20 75 73 65 20  void.** the use 
33a20 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f  of these functio
33a30 6e 73 2e 20 20 54 6f 20 68 65 6c 70 20 65 6e 63  ns.  To help enc
33a40 6f 75 72 61 67 65 20 70 65 6f 70 6c 65 20 74 6f  ourage people to
33a50 20 61 76 6f 69 64 0a 2a 2a 20 75 73 69 6e 67 20   avoid.** using 
33a60 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 2c  these functions,
33a70 20 77 65 20 61 72 65 20 6e 6f 74 20 67 6f 69 6e   we are not goin
33a80 67 20 74 6f 20 74 65 6c 6c 20 79 6f 75 20 77 61  g to tell you wa
33a90 6e 74 20 74 68 65 79 20 64 6f 2e 0a 2a 2f 0a 23  nt they do..*/.#
33aa0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
33ab0 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 53 51  IT_DEPRECATED.SQ
33ac0 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f  LITE_API SQLITE_
33ad0 44 45 50 52 45 43 41 54 45 44 20 69 6e 74 20 73  DEPRECATED int s
33ae0 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
33af0 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 63  _count(sqlite3_c
33b00 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45  ontext*);.SQLITE
33b10 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52  _API SQLITE_DEPR
33b20 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74  ECATED int sqlit
33b30 65 33 5f 65 78 70 69 72 65 64 28 73 71 6c 69 74  e3_expired(sqlit
33b40 65 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49 54  e3_stmt*);.SQLIT
33b50 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50  E_API SQLITE_DEP
33b60 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69  RECATED int sqli
33b70 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e  te3_transfer_bin
33b80 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74  dings(sqlite3_st
33b90 6d 74 2a 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d  mt*, sqlite3_stm
33ba0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  t*);.SQLITE_API 
33bb0 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45  SQLITE_DEPRECATE
33bc0 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 6c  D int sqlite3_gl
33bd0 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28 76 6f 69  obal_recover(voi
33be0 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53  d);.SQLITE_API S
33bf0 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44  QLITE_DEPRECATED
33c00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 74 68   void sqlite3_th
33c10 72 65 61 64 5f 63 6c 65 61 6e 75 70 28 76 6f 69  read_cleanup(voi
33c20 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53  d);.SQLITE_API S
33c30 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44  QLITE_DEPRECATED
33c40 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d   int sqlite3_mem
33c50 6f 72 79 5f 61 6c 61 72 6d 28 76 6f 69 64 28 2a  ory_alarm(void(*
33c60 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f  )(void*,sqlite3_
33c70 69 6e 74 36 34 2c 69 6e 74 29 2c 76 6f 69 64 2a  int64,int),void*
33c80 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b  ,sqlite3_int64);
33c90 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43  .#endif../*.** C
33ca0 41 50 49 33 52 45 46 3a 20 4f 62 74 61 69 6e 69  API3REF: Obtaini
33cb0 6e 67 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 20  ng SQL Function 
33cc0 50 61 72 61 6d 65 74 65 72 20 56 61 6c 75 65 73  Parameter Values
33cd0 20 7b 48 31 35 31 30 30 7d 20 3c 53 32 30 32 30   {H15100} <S2020
33ce0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 2d 6c  0>.**.** The C-l
33cf0 61 6e 67 75 61 67 65 20 69 6d 70 6c 65 6d 65 6e  anguage implemen
33d00 74 61 74 69 6f 6e 20 6f 66 20 53 51 4c 20 66 75  tation of SQL fu
33d10 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72  nctions and aggr
33d20 65 67 61 74 65 73 20 75 73 65 73 0a 2a 2a 20 74  egates uses.** t
33d30 68 69 73 20 73 65 74 20 6f 66 20 69 6e 74 65 72  his set of inter
33d40 66 61 63 65 20 72 6f 75 74 69 6e 65 73 20 74 6f  face routines to
33d50 20 61 63 63 65 73 73 20 74 68 65 20 70 61 72 61   access the para
33d60 6d 65 74 65 72 20 76 61 6c 75 65 73 20 6f 6e 0a  meter values on.
33d70 2a 2a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  ** the function 
33d80 6f 72 20 61 67 67 72 65 67 61 74 65 2e 0a 2a 2a  or aggregate..**
33d90 0a 2a 2a 20 54 68 65 20 78 46 75 6e 63 20 28 66  .** The xFunc (f
33da0 6f 72 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69  or scalar functi
33db0 6f 6e 73 29 20 6f 72 20 78 53 74 65 70 20 28 66  ons) or xStep (f
33dc0 6f 72 20 61 67 67 72 65 67 61 74 65 73 29 20 70  or aggregates) p
33dd0 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 6f 20  arameters.** to 
33de0 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
33df0 66 75 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64 20  function()] and 
33e00 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
33e10 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a  function16()].**
33e20 20 64 65 66 69 6e 65 20 63 61 6c 6c 62 61 63 6b   define callback
33e30 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  s that implement
33e40 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f   the SQL functio
33e50 6e 73 20 61 6e 64 20 61 67 67 72 65 67 61 74 65  ns and aggregate
33e60 73 2e 0a 2a 2a 20 54 68 65 20 34 74 68 20 70 61  s..** The 4th pa
33e70 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 73 65  rameter to these
33e80 20 63 61 6c 6c 62 61 63 6b 73 20 69 73 20 61 6e   callbacks is an
33e90 20 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65   array of pointe
33ea0 72 73 20 74 6f 0a 2a 2a 20 5b 70 72 6f 74 65 63  rs to.** [protec
33eb0 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ted sqlite3_valu
33ec0 65 5d 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 65  e] objects.  The
33ed0 72 65 20 69 73 20 6f 6e 65 20 5b 73 71 6c 69 74  re is one [sqlit
33ee0 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74  e3_value] object
33ef0 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20 70 61 72   for.** each par
33f00 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 53 51  ameter to the SQ
33f10 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65  L function.  The
33f20 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  se routines are 
33f30 75 73 65 64 20 74 6f 0a 2a 2a 20 65 78 74 72 61  used to.** extra
33f40 63 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74  ct values from t
33f50 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  he [sqlite3_valu
33f60 65 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a 2a  e] objects..**.*
33f70 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
33f80 20 77 6f 72 6b 20 6f 6e 6c 79 20 77 69 74 68 20   work only with 
33f90 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  [protected sqlit
33fa0 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74  e3_value] object
33fb0 73 2e 0a 2a 2a 20 41 6e 79 20 61 74 74 65 6d 70  s..** Any attemp
33fc0 74 20 74 6f 20 75 73 65 20 74 68 65 73 65 20 72  t to use these r
33fd0 6f 75 74 69 6e 65 73 20 6f 6e 20 61 6e 20 5b 75  outines on an [u
33fe0 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  nprotected sqlit
33ff0 65 33 5f 76 61 6c 75 65 5d 0a 2a 2a 20 6f 62 6a  e3_value].** obj
34000 65 63 74 20 72 65 73 75 6c 74 73 20 69 6e 20 75  ect results in u
34010 6e 64 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f  ndefined behavio
34020 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  r..**.** These r
34030 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6a 75 73  outines work jus
34040 74 20 6c 69 6b 65 20 74 68 65 20 63 6f 72 72 65  t like the corre
34050 73 70 6f 6e 64 69 6e 67 20 5b 63 6f 6c 75 6d 6e  sponding [column
34060 20 61 63 63 65 73 73 20 66 75 6e 63 74 69 6f 6e   access function
34070 73 5d 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61  s].** except tha
34080 74 20 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  t  these routine
34090 73 20 74 61 6b 65 20 61 20 73 69 6e 67 6c 65 20  s take a single 
340a0 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  [protected sqlit
340b0 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74  e3_value] object
340c0 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 69 6e 73 74  .** pointer inst
340d0 65 61 64 20 6f 66 20 61 20 5b 73 71 6c 69 74 65  ead of a [sqlite
340e0 33 5f 73 74 6d 74 2a 5d 20 70 6f 69 6e 74 65 72  3_stmt*] pointer
340f0 20 61 6e 64 20 61 6e 20 69 6e 74 65 67 65 72 20   and an integer 
34100 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a  column number..*
34110 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
34120 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29 20  _value_text16() 
34130 69 6e 74 65 72 66 61 63 65 20 65 78 74 72 61 63  interface extrac
34140 74 73 20 61 20 55 54 46 2d 31 36 20 73 74 72 69  ts a UTF-16 stri
34150 6e 67 0a 2a 2a 20 69 6e 20 74 68 65 20 6e 61 74  ng.** in the nat
34160 69 76 65 20 62 79 74 65 2d 6f 72 64 65 72 20 6f  ive byte-order o
34170 66 20 74 68 65 20 68 6f 73 74 20 6d 61 63 68 69  f the host machi
34180 6e 65 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69  ne.  The.** sqli
34190 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
341a0 62 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  be() and sqlite3
341b0 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28  _value_text16le(
341c0 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20  ) interfaces.** 
341d0 65 78 74 72 61 63 74 20 55 54 46 2d 31 36 20 73  extract UTF-16 s
341e0 74 72 69 6e 67 73 20 61 73 20 62 69 67 2d 65 6e  trings as big-en
341f0 64 69 61 6e 20 61 6e 64 20 6c 69 74 74 6c 65 2d  dian and little-
34200 65 6e 64 69 61 6e 20 72 65 73 70 65 63 74 69 76  endian respectiv
34210 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ely..**.** The s
34220 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d  qlite3_value_num
34230 65 72 69 63 5f 74 79 70 65 28 29 20 69 6e 74 65  eric_type() inte
34240 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 74  rface attempts t
34250 6f 20 61 70 70 6c 79 0a 2a 2a 20 6e 75 6d 65 72  o apply.** numer
34260 69 63 20 61 66 66 69 6e 69 74 79 20 74 6f 20 74  ic affinity to t
34270 68 65 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20  he value.  This 
34280 6d 65 61 6e 73 20 74 68 61 74 20 61 6e 20 61 74  means that an at
34290 74 65 6d 70 74 20 69 73 0a 2a 2a 20 6d 61 64 65  tempt is.** made
342a0 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20   to convert the 
342b0 76 61 6c 75 65 20 74 6f 20 61 6e 20 69 6e 74 65  value to an inte
342c0 67 65 72 20 6f 72 20 66 6c 6f 61 74 69 6e 67 20  ger or floating 
342d0 70 6f 69 6e 74 2e 20 20 49 66 0a 2a 2a 20 73 75  point.  If.** su
342e0 63 68 20 61 20 63 6f 6e 76 65 72 73 69 6f 6e 20  ch a conversion 
342f0 69 73 20 70 6f 73 73 69 62 6c 65 20 77 69 74 68  is possible with
34300 6f 75 74 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f  out loss of info
34310 72 6d 61 74 69 6f 6e 20 28 69 6e 20 6f 74 68 65  rmation (in othe
34320 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 69 66 20 74  r.** words, if t
34330 68 65 20 76 61 6c 75 65 20 69 73 20 61 20 73 74  he value is a st
34340 72 69 6e 67 20 74 68 61 74 20 6c 6f 6f 6b 73 20  ring that looks 
34350 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72 29 0a 2a  like a number).*
34360 2a 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 76 65  * then the conve
34370 72 73 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d  rsion is perform
34380 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 6e  ed.  Otherwise n
34390 6f 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 63  o conversion occ
343a0 75 72 73 2e 0a 2a 2a 20 54 68 65 20 5b 53 51 4c  urs..** The [SQL
343b0 49 54 45 5f 49 4e 54 45 47 45 52 20 7c 20 64 61  ITE_INTEGER | da
343c0 74 61 74 79 70 65 5d 20 61 66 74 65 72 20 63 6f  tatype] after co
343d0 6e 76 65 72 73 69 6f 6e 20 69 73 20 72 65 74 75  nversion is retu
343e0 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 6c 65 61  rned..**.** Plea
343f0 73 65 20 70 61 79 20 70 61 72 74 69 63 75 6c 61  se pay particula
34400 72 20 61 74 74 65 6e 74 69 6f 6e 20 74 6f 20 74  r attention to t
34410 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65  he fact that the
34420 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65   pointer returne
34430 64 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c 69 74  d.** from [sqlit
34440 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 29 5d  e3_value_blob()]
34450 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  , [sqlite3_value
34460 5f 74 65 78 74 28 29 5d 2c 20 6f 72 0a 2a 2a 20  _text()], or.** 
34470 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  [sqlite3_value_t
34480 65 78 74 31 36 28 29 5d 20 63 61 6e 20 62 65 20  ext16()] can be 
34490 69 6e 76 61 6c 69 64 61 74 65 64 20 62 79 20 61  invalidated by a
344a0 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
344b0 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   to.** [sqlite3_
344c0 76 61 6c 75 65 5f 62 79 74 65 73 28 29 5d 2c 20  value_bytes()], 
344d0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62  [sqlite3_value_b
344e0 79 74 65 73 31 36 28 29 5d 2c 20 5b 73 71 6c 69  ytes16()], [sqli
344f0 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29  te3_value_text()
34500 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65  ],.** or [sqlite
34510 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29  3_value_text16()
34520 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  ]..**.** These r
34530 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65 20  outines must be 
34540 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20  called from the 
34550 73 61 6d 65 20 74 68 72 65 61 64 20 61 73 0a 2a  same thread as.*
34560 2a 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69  * the SQL functi
34570 6f 6e 20 74 68 61 74 20 73 75 70 70 6c 69 65 64  on that supplied
34580 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61   the [sqlite3_va
34590 6c 75 65 2a 5d 20 70 61 72 61 6d 65 74 65 72 73  lue*] parameters
345a0 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e  ..**.** INVARIAN
345b0 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31 30  TS:.**.** {H1510
345c0 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  3} The [sqlite3_
345d0 76 61 6c 75 65 5f 62 6c 6f 62 28 56 29 5d 20 69  value_blob(V)] i
345e0 6e 74 65 72 66 61 63 65 20 63 6f 6e 76 65 72 74  nterface convert
345f0 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  s the.**        
34600 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c    [protected sql
34610 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65  ite3_value] obje
34620 63 74 20 56 20 69 6e 74 6f 20 61 20 42 4c 4f 42  ct V into a BLOB
34630 20 61 6e 64 20 74 68 65 6e 0a 2a 2a 20 20 20 20   and then.**    
34640 20 20 20 20 20 20 72 65 74 75 72 6e 73 20 61 20        returns a 
34650 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63  pointer to the c
34660 6f 6e 76 65 72 74 65 64 20 76 61 6c 75 65 2e 0a  onverted value..
34670 2a 2a 0a 2a 2a 20 7b 48 31 35 31 30 36 7d 20 54  **.** {H15106} T
34680 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  he [sqlite3_valu
34690 65 5f 62 79 74 65 73 28 56 29 5d 20 69 6e 74 65  e_bytes(V)] inte
346a0 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68  rface returns th
346b0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 75  e.**          nu
346c0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
346d0 20 74 68 65 20 42 4c 4f 42 20 6f 72 20 73 74 72   the BLOB or str
346e0 69 6e 67 20 28 65 78 63 6c 75 73 69 76 65 20 6f  ing (exclusive o
346f0 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  f the.**        
34700 20 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f    zero terminato
34710 72 20 6f 6e 20 74 68 65 20 73 74 72 69 6e 67 29  r on the string)
34720 20 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e   that was return
34730 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 20 20 20  ed by the.**    
34740 20 20 20 20 20 20 6d 6f 73 74 20 72 65 63 65 6e        most recen
34750 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  t call to [sqlit
34760 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 56 29  e3_value_blob(V)
34770 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  ] or.**         
34780 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   [sqlite3_value_
34790 74 65 78 74 28 56 29 5d 2e 0a 2a 2a 0a 2a 2a 20  text(V)]..**.** 
347a0 7b 48 31 35 31 30 39 7d 20 54 68 65 20 5b 73 71  {H15109} The [sq
347b0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
347c0 73 31 36 28 56 29 5d 20 69 6e 74 65 72 66 61 63  s16(V)] interfac
347d0 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a  e returns the.**
347e0 20 20 20 20 20 20 20 20 20 20 6e 75 6d 62 65 72            number
347f0 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65   of bytes in the
34800 20 73 74 72 69 6e 67 20 28 65 78 63 6c 75 73 69   string (exclusi
34810 76 65 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20  ve of the.**    
34820 20 20 20 20 20 20 7a 65 72 6f 20 74 65 72 6d 69        zero termi
34830 6e 61 74 6f 72 20 6f 6e 20 74 68 65 20 73 74 72  nator on the str
34840 69 6e 67 29 20 74 68 61 74 20 77 61 73 20 72 65  ing) that was re
34850 74 75 72 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a  turned by the.**
34860 20 20 20 20 20 20 20 20 20 20 6d 6f 73 74 20 72            most r
34870 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 5b 73  ecent call to [s
34880 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
34890 74 31 36 28 56 29 5d 2c 0a 2a 2a 20 20 20 20 20  t16(V)],.**     
348a0 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 76 61       [sqlite3_va
348b0 6c 75 65 5f 74 65 78 74 31 36 62 65 28 56 29 5d  lue_text16be(V)]
348c0 2c 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 76 61  , or [sqlite3_va
348d0 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 56 29 5d  lue_text16le(V)]
348e0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31 31 32 7d  ..**.** {H15112}
348f0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61   The [sqlite3_va
34900 6c 75 65 5f 64 6f 75 62 6c 65 28 56 29 5d 20 69  lue_double(V)] i
34910 6e 74 65 72 66 61 63 65 20 63 6f 6e 76 65 72 74  nterface convert
34920 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  s the.**        
34930 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c    [protected sql
34940 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65  ite3_value] obje
34950 63 74 20 56 20 69 6e 74 6f 20 61 20 66 6c 6f 61  ct V into a floa
34960 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65  ting point value
34970 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20   and.**         
34980 20 72 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20   returns a copy 
34990 6f 66 20 74 68 61 74 20 76 61 6c 75 65 2e 0a 2a  of that value..*
349a0 2a 0a 2a 2a 20 7b 48 31 35 31 31 35 7d 20 54 68  *.** {H15115} Th
349b0 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  e [sqlite3_value
349c0 5f 69 6e 74 28 56 29 5d 20 69 6e 74 65 72 66 61  _int(V)] interfa
349d0 63 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65 0a  ce converts the.
349e0 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 6f  **          [pro
349f0 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76  tected sqlite3_v
34a00 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 56 20 69  alue] object V i
34a10 6e 74 6f 20 61 20 36 34 2d 62 69 74 20 73 69 67  nto a 64-bit sig
34a20 6e 65 64 20 69 6e 74 65 67 65 72 20 61 6e 64 0a  ned integer and.
34a30 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  **          retu
34a40 72 6e 73 20 74 68 65 20 6c 6f 77 65 72 20 33 32  rns the lower 32
34a50 20 62 69 74 73 20 6f 66 20 74 68 61 74 20 69 6e   bits of that in
34a60 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  teger..**.** {H1
34a70 35 31 31 38 7d 20 54 68 65 20 5b 73 71 6c 69 74  5118} The [sqlit
34a80 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 56  e3_value_int64(V
34a90 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e  )] interface con
34aa0 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20  verts the.**    
34ab0 20 20 20 20 20 20 5b 70 72 6f 74 65 63 74 65 64        [protected
34ac0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20   sqlite3_value] 
34ad0 6f 62 6a 65 63 74 20 56 20 69 6e 74 6f 20 61 20  object V into a 
34ae0 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e  64-bit signed in
34af0 74 65 67 65 72 20 61 6e 64 0a 2a 2a 20 20 20 20  teger and.**    
34b00 20 20 20 20 20 20 72 65 74 75 72 6e 73 20 61 20        returns a 
34b10 63 6f 70 79 20 6f 66 20 74 68 61 74 20 69 6e 74  copy of that int
34b20 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35  eger..**.** {H15
34b30 31 32 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  121} The [sqlite
34b40 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 56 29 5d  3_value_text(V)]
34b50 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e 76 65   interface conve
34b60 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  rts the.**      
34b70 20 20 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73      [protected s
34b80 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62  qlite3_value] ob
34b90 6a 65 63 74 20 56 20 69 6e 74 6f 20 61 20 7a 65  ject V into a ze
34ba0 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54  ro-terminated UT
34bb0 46 2d 38 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  F-8.**          
34bc0 73 74 72 69 6e 67 20 61 6e 64 20 72 65 74 75 72  string and retur
34bd0 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
34be0 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a  that string..**.
34bf0 2a 2a 20 7b 48 31 35 31 32 34 7d 20 54 68 65 20  ** {H15124} The 
34c00 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  [sqlite3_value_t
34c10 65 78 74 31 36 28 56 29 5d 20 69 6e 74 65 72 66  ext16(V)] interf
34c20 61 63 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65  ace converts the
34c30 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72  .**          [pr
34c40 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
34c50 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 56 20  value] object V 
34c60 69 6e 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 6d  into a zero-term
34c70 69 6e 61 74 65 64 20 32 2d 62 79 74 65 0a 2a 2a  inated 2-byte.**
34c80 20 20 20 20 20 20 20 20 20 20 61 6c 69 67 6e 65            aligne
34c90 64 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20  d UTF-16 native 
34ca0 62 79 74 65 20 6f 72 64 65 72 0a 2a 2a 20 20 20  byte order.**   
34cb0 20 20 20 20 20 20 20 73 74 72 69 6e 67 20 61 6e         string an
34cc0 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  d returns a poin
34cd0 74 65 72 20 74 6f 20 74 68 61 74 20 73 74 72 69  ter to that stri
34ce0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31 32  ng..**.** {H1512
34cf0 37 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  7} The [sqlite3_
34d00 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28 56  value_text16be(V
34d10 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e  )] interface con
34d20 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20  verts the.**    
34d30 20 20 20 20 20 20 5b 70 72 6f 74 65 63 74 65 64        [protected
34d40 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20   sqlite3_value] 
34d50 6f 62 6a 65 63 74 20 56 20 69 6e 74 6f 20 61 20  object V into a 
34d60 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20  zero-terminated 
34d70 32 2d 62 79 74 65 0a 2a 2a 20 20 20 20 20 20 20  2-byte.**       
34d80 20 20 20 61 6c 69 67 6e 65 64 20 55 54 46 2d 31     aligned UTF-1
34d90 36 20 62 69 67 2d 65 6e 64 69 61 6e 0a 2a 2a 20  6 big-endian.** 
34da0 20 20 20 20 20 20 20 20 20 73 74 72 69 6e 67 20           string 
34db0 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f  and returns a po
34dc0 69 6e 74 65 72 20 74 6f 20 74 68 61 74 20 73 74  inter to that st
34dd0 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35  ring..**.** {H15
34de0 31 33 30 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  130} The [sqlite
34df0 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65  3_value_text16le
34e00 28 56 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63  (V)] interface c
34e10 6f 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20  onverts the.**  
34e20 20 20 20 20 20 20 20 20 5b 70 72 6f 74 65 63 74          [protect
34e30 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
34e40 5d 20 6f 62 6a 65 63 74 20 56 20 69 6e 74 6f 20  ] object V into 
34e50 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  a zero-terminate
34e60 64 20 32 2d 62 79 74 65 0a 2a 2a 20 20 20 20 20  d 2-byte.**     
34e70 20 20 20 20 20 61 6c 69 67 6e 65 64 20 55 54 46       aligned UTF
34e80 2d 31 36 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61  -16 little-endia
34e90 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74  n.**          st
34ea0 72 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73  ring and returns
34eb0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
34ec0 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a  at string..**.**
34ed0 20 7b 48 31 35 31 33 33 7d 20 54 68 65 20 5b 73   {H15133} The [s
34ee0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
34ef0 65 28 56 29 5d 20 69 6e 74 65 72 66 61 63 65 20  e(V)] interface 
34f00 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20 20  returns.**      
34f10 20 20 20 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49      one of [SQLI
34f20 54 45 5f 4e 55 4c 4c 5d 2c 20 5b 53 51 4c 49 54  TE_NULL], [SQLIT
34f30 45 5f 49 4e 54 45 47 45 52 5d 2c 20 5b 53 51 4c  E_INTEGER], [SQL
34f40 49 54 45 5f 46 4c 4f 41 54 5d 2c 0a 2a 2a 20 20  ITE_FLOAT],.**  
34f50 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f          [SQLITE_
34f60 54 45 58 54 5d 2c 20 6f 72 20 5b 53 51 4c 49 54  TEXT], or [SQLIT
34f70 45 5f 42 4c 4f 42 5d 20 61 73 20 61 70 70 72 6f  E_BLOB] as appro
34f80 70 72 69 61 74 65 20 66 6f 72 0a 2a 2a 20 20 20  priate for.**   
34f90 20 20 20 20 20 20 20 74 68 65 20 5b 73 71 6c 69         the [sqli
34fa0 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63  te3_value] objec
34fb0 74 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31  t V..**.** {H151
34fc0 33 36 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  36} The [sqlite3
34fd0 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74  _value_numeric_t
34fe0 79 70 65 28 56 29 5d 20 69 6e 74 65 72 66 61 63  ype(V)] interfac
34ff0 65 20 63 6f 6e 76 65 72 74 73 0a 2a 2a 20 20 20  e converts.**   
35000 20 20 20 20 20 20 20 74 68 65 20 5b 70 72 6f 74         the [prot
35010 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61  ected sqlite3_va
35020 6c 75 65 5d 20 6f 62 6a 65 63 74 20 56 20 69 6e  lue] object V in
35030 74 6f 20 65 69 74 68 65 72 20 61 6e 20 69 6e 74  to either an int
35040 65 67 65 72 20 6f 72 0a 2a 2a 20 20 20 20 20 20  eger or.**      
35050 20 20 20 20 61 20 66 6c 6f 61 74 69 6e 67 20 70      a floating p
35060 6f 69 6e 74 20 76 61 6c 75 65 20 69 66 20 69 74  oint value if it
35070 20 63 61 6e 20 64 6f 20 73 6f 20 77 69 74 68 6f   can do so witho
35080 75 74 20 6c 6f 73 73 20 6f 66 0a 2a 2a 20 20 20  ut loss of.**   
35090 20 20 20 20 20 20 20 69 6e 66 6f 72 6d 61 74 69         informati
350a0 6f 6e 2c 20 61 6e 64 20 72 65 74 75 72 6e 73 20  on, and returns 
350b0 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 4e  one of [SQLITE_N
350c0 55 4c 4c 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20  ULL],.**        
350d0 20 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45    [SQLITE_INTEGE
350e0 52 5d 2c 20 5b 53 51 4c 49 54 45 5f 46 4c 4f 41  R], [SQLITE_FLOA
350f0 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 54 45 58 54  T], [SQLITE_TEXT
35100 5d 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20  ], or.**        
35110 20 20 5b 53 51 4c 49 54 45 5f 42 4c 4f 42 5d 20    [SQLITE_BLOB] 
35120 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66  as appropriate f
35130 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  or the.**       
35140 20 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71     [protected sq
35150 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a  lite3_value] obj
35160 65 63 74 20 56 20 61 66 74 65 72 20 74 68 65 20  ect V after the 
35170 63 6f 6e 76 65 72 73 69 6f 6e 20 61 74 74 65 6d  conversion attem
35180 70 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  pt..*/.SQLITE_AP
35190 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
351a0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
351b0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
351c0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
351d0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
351e0 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c  ytes(sqlite3_val
351f0 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ue*);.SQLITE_API
35200 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c   int sqlite3_val
35210 75 65 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74  ue_bytes16(sqlit
35220 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49  e3_value*);.SQLI
35230 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 71  TE_API double sq
35240 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62  lite3_value_doub
35250 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  le(sqlite3_value
35260 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  *);.SQLITE_API i
35270 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt sqlite3_value
35280 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 76 61 6c  _int(sqlite3_val
35290 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ue*);.SQLITE_API
352a0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73   sqlite3_int64 s
352b0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
352c0 36 34 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  64(sqlite3_value
352d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  *);.SQLITE_API c
352e0 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
352f0 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar *sqlite3_valu
35300 65 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f 76  e_text(sqlite3_v
35310 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41  alue*);.SQLITE_A
35320 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
35330 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
35340 74 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  t16(sqlite3_valu
35350 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  e*);.SQLITE_API 
35360 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
35370 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
35380 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  le(sqlite3_value
35390 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  *);.SQLITE_API c
353a0 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
353b0 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62  e3_value_text16b
353c0 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  e(sqlite3_value*
353d0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
353e0 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  t sqlite3_value_
353f0 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c  type(sqlite3_val
35400 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ue*);.SQLITE_API
35410 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c   int sqlite3_val
35420 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28  ue_numeric_type(
35430 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
35440 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
35450 3a 20 4f 62 74 61 69 6e 20 41 67 67 72 65 67 61  : Obtain Aggrega
35460 74 65 20 46 75 6e 63 74 69 6f 6e 20 43 6f 6e 74  te Function Cont
35470 65 78 74 20 7b 48 31 36 32 31 30 7d 20 3c 53 32  ext {H16210} <S2
35480 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  0200>.**.** The 
35490 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
354a0 66 20 61 67 67 72 65 67 61 74 65 20 53 51 4c 20  f aggregate SQL 
354b0 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 20 74 68  functions use th
354c0 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 61 6c  is routine to al
354d0 6c 6f 63 61 74 65 0a 2a 2a 20 61 20 73 74 72 75  locate.** a stru
354e0 63 74 75 72 65 20 66 6f 72 20 73 74 6f 72 69 6e  cture for storin
354f0 67 20 74 68 65 69 72 20 73 74 61 74 65 2e 0a 2a  g their state..*
35500 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74  *.** The first t
35510 69 6d 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ime the sqlite3_
35520 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78  aggregate_contex
35530 74 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 63  t() routine is c
35540 61 6c 6c 65 64 20 66 6f 72 20 61 0a 2a 2a 20 70  alled for a.** p
35550 61 72 74 69 63 75 6c 61 72 20 61 67 67 72 65 67  articular aggreg
35560 61 74 65 2c 20 53 51 4c 69 74 65 20 61 6c 6c 6f  ate, SQLite allo
35570 63 61 74 65 73 20 6e 42 79 74 65 73 20 6f 66 20  cates nBytes of 
35580 6d 65 6d 6f 72 79 2c 20 7a 65 72 6f 65 73 20 6f  memory, zeroes o
35590 75 74 20 74 68 61 74 0a 2a 2a 20 6d 65 6d 6f 72  ut that.** memor
355a0 79 2c 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61  y, and returns a
355b0 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20   pointer to it. 
355c0 4f 6e 20 73 65 63 6f 6e 64 20 61 6e 64 20 73 75  On second and su
355d0 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74  bsequent calls t
355e0 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 61 67 67  o.** sqlite3_agg
355f0 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29  regate_context()
35600 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 61 67   for the same ag
35610 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
35620 20 69 6e 64 65 78 2c 0a 2a 2a 20 74 68 65 20 73   index,.** the s
35630 61 6d 65 20 62 75 66 66 65 72 20 69 73 20 72 65  ame buffer is re
35640 74 75 72 6e 65 64 2e 20 54 68 65 20 69 6d 70 6c  turned. The impl
35650 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
35660 65 20 61 67 67 72 65 67 61 74 65 20 63 61 6e 20  e aggregate can 
35670 75 73 65 0a 2a 2a 20 74 68 65 20 72 65 74 75 72  use.** the retur
35680 6e 65 64 20 62 75 66 66 65 72 20 74 6f 20 61 63  ned buffer to ac
35690 63 75 6d 75 6c 61 74 65 20 64 61 74 61 2e 0a 2a  cumulate data..*
356a0 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 61 75 74 6f  *.** SQLite auto
356b0 6d 61 74 69 63 61 6c 6c 79 20 66 72 65 65 73 20  matically frees 
356c0 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 75  the allocated bu
356d0 66 66 65 72 20 77 68 65 6e 20 74 68 65 20 61 67  ffer when the ag
356e0 67 72 65 67 61 74 65 0a 2a 2a 20 71 75 65 72 79  gregate.** query
356f0 20 63 6f 6e 63 6c 75 64 65 73 2e 0a 2a 2a 0a 2a   concludes..**.*
35700 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61  * The first para
35710 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 20  meter should be 
35720 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a  a copy of the.**
35730 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   [sqlite3_contex
35740 74 20 7c 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  t | SQL function
35750 20 63 6f 6e 74 65 78 74 5d 20 74 68 61 74 20 69   context] that i
35760 73 20 74 68 65 20 66 69 72 73 74 20 70 61 72 61  s the first para
35770 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20  meter.** to the 
35780 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65  callback routine
35790 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
357a0 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 66   the aggregate f
357b0 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  unction..**.** T
357c0 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74  his routine must
357d0 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20   be called from 
357e0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20  the same thread 
357f0 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20  in which.** the 
35800 61 67 67 72 65 67 61 74 65 20 53 51 4c 20 66 75  aggregate SQL fu
35810 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e  nction is runnin
35820 67 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  g..**.** INVARIA
35830 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32  NTS:.**.** {H162
35840 31 31 7d 20 54 68 65 20 66 69 72 73 74 20 69 6e  11} The first in
35850 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c  vocation of [sql
35860 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
35870 6f 6e 74 65 78 74 28 43 2c 4e 29 5d 20 66 6f 72  ontext(C,N)] for
35880 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 20 70  .**          a p
35890 61 72 74 69 63 75 6c 61 72 20 69 6e 73 74 61 6e  articular instan
358a0 63 65 20 6f 66 20 61 6e 20 61 67 67 72 65 67 61  ce of an aggrega
358b0 74 65 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72  te function (for
358c0 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a   a particular.**
358d0 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65 78            contex
358e0 74 20 43 29 20 63 61 75 73 65 73 20 53 51 4c 69  t C) causes SQLi
358f0 74 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 4e  te to allocate N
35900 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79   bytes of memory
35910 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 7a 65  ,.**          ze
35920 72 6f 20 74 68 61 74 20 6d 65 6d 6f 72 79 2c 20  ro that memory, 
35930 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69  and return a poi
35940 6e 74 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f  nter to the allo
35950 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a 2a  cated memory..**
35960 0a 2a 2a 20 7b 48 31 36 32 31 33 7d 20 49 66 20  .** {H16213} If 
35970 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
35980 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73  ion error occurs
35990 20 64 75 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20   during.**      
359a0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 61 67 67      [sqlite3_agg
359b0 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 43  regate_context(C
359c0 2c 4e 29 5d 20 74 68 65 6e 20 74 68 65 20 66 75  ,N)] then the fu
359d0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 30  nction returns 0
359e0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 31 35 7d  ..**.** {H16215}
359f0 20 53 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 73   Second and subs
35a00 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f  equent invocatio
35a10 6e 73 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20  ns of.**        
35a20 20 20 5b 73 71 6c 69 74 65 33 5f 61 67 67 72 65    [sqlite3_aggre
35a30 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 43 2c 4e  gate_context(C,N
35a40 29 5d 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20  )] for the same 
35a50 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20  context pointer 
35a60 43 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 67  C.**          ig
35a70 6e 6f 72 65 20 74 68 65 20 4e 20 70 61 72 61 6d  nore the N param
35a80 65 74 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20  eter and return 
35a90 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
35aa0 20 73 61 6d 65 0a 2a 2a 20 20 20 20 20 20 20 20   same.**        
35ab0 20 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72    block of memor
35ac0 79 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  y returned by th
35ad0 65 20 66 69 72 73 74 20 69 6e 76 6f 63 61 74 69  e first invocati
35ae0 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 31  on..**.** {H1621
35af0 37 7d 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c  7} The memory al
35b00 6c 6f 63 61 74 65 64 20 62 79 20 5b 73 71 6c 69  located by [sqli
35b10 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
35b20 6e 74 65 78 74 28 43 2c 4e 29 5d 20 69 73 0a 2a  ntext(C,N)] is.*
35b30 2a 20 20 20 20 20 20 20 20 20 20 61 75 74 6f 6d  *          autom
35b40 61 74 69 63 61 6c 6c 79 20 66 72 65 65 64 20 6f  atically freed o
35b50 6e 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20  n the next call 
35b60 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65  to [sqlite3_rese
35b70 74 28 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20  t()].**         
35b80 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e   or [sqlite3_fin
35b90 61 6c 69 7a 65 28 29 5d 20 66 6f 72 20 74 68 65  alize()] for the
35ba0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
35bb0 6d 65 6e 74 5d 20 63 6f 6e 74 61 69 6e 69 6e 67  ment] containing
35bc0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65  .**          the
35bd0 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
35be0 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77  ion associated w
35bf0 69 74 68 20 63 6f 6e 74 65 78 74 20 43 2e 0a 2a  ith context C..*
35c00 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
35c10 64 20 2a 73 71 6c 69 74 65 33 5f 61 67 67 72 65  d *sqlite3_aggre
35c20 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 73 71 6c  gate_context(sql
35c30 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69  ite3_context*, i
35c40 6e 74 20 6e 42 79 74 65 73 29 3b 0a 0a 2f 2a 0a  nt nBytes);../*.
35c50 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 55 73 65  ** CAPI3REF: Use
35c60 72 20 44 61 74 61 20 46 6f 72 20 46 75 6e 63 74  r Data For Funct
35c70 69 6f 6e 73 20 7b 48 31 36 32 34 30 7d 20 3c 53  ions {H16240} <S
35c80 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  20200>.**.** The
35c90 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61   sqlite3_user_da
35ca0 74 61 28 29 20 69 6e 74 65 72 66 61 63 65 20 72  ta() interface r
35cb0 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66  eturns a copy of
35cc0 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20  .** the pointer 
35cd0 74 68 61 74 20 77 61 73 20 74 68 65 20 70 55 73  that was the pUs
35ce0 65 72 44 61 74 61 20 70 61 72 61 6d 65 74 65 72  erData parameter
35cf0 20 28 74 68 65 20 35 74 68 20 70 61 72 61 6d 65   (the 5th parame
35d00 74 65 72 29 0a 2a 2a 20 6f 66 20 74 68 65 20 5b  ter).** of the [
35d10 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
35d20 75 6e 63 74 69 6f 6e 28 29 5d 0a 2a 2a 20 61 6e  unction()].** an
35d30 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74  d [sqlite3_creat
35d40 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 20  e_function16()] 
35d50 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 6f 72  routines that or
35d60 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20 72 65 67 69  iginally.** regi
35d70 73 74 65 72 65 64 20 74 68 65 20 61 70 70 6c 69  stered the appli
35d80 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 66  cation defined f
35d90 75 6e 63 74 69 6f 6e 2e 20 7b 45 4e 44 7d 0a 2a  unction. {END}.*
35da0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
35db0 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64  e must be called
35dc0 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 74   from the same t
35dd0 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 0a 2a  hread in which.*
35de0 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  * the applicatio
35df0 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  n-defined functi
35e00 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a  on is running..*
35e10 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
35e20 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 34 33 7d 20  .**.** {H16243} 
35e30 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 75 73 65  The [sqlite3_use
35e40 72 5f 64 61 74 61 28 43 29 5d 20 69 6e 74 65 72  r_data(C)] inter
35e50 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 63  face returns a c
35e60 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20  opy of the.**   
35e70 20 20 20 20 20 20 20 50 20 70 6f 69 6e 74 65 72         P pointer
35e80 20 66 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74   from the [sqlit
35e90 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
35ea0 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 50 2c 46 2c 53  on(D,X,N,E,P,F,S
35eb0 2c 4c 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20  ,L)].**         
35ec0 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 63 72 65   or [sqlite3_cre
35ed0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 44  ate_function16(D
35ee0 2c 58 2c 4e 2c 45 2c 50 2c 46 2c 53 2c 4c 29 5d  ,X,N,E,P,F,S,L)]
35ef0 20 63 61 6c 6c 20 74 68 61 74 0a 2a 2a 20 20 20   call that.**   
35f00 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 65         registere
35f10 64 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69  d the SQL functi
35f20 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  on associated wi
35f30 74 68 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74  th [sqlite3_cont
35f40 65 78 74 5d 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54  ext] C..*/.SQLIT
35f50 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
35f60 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71  te3_user_data(sq
35f70 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b  lite3_context*);
35f80 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
35f90 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65  : Database Conne
35fa0 63 74 69 6f 6e 20 46 6f 72 20 46 75 6e 63 74 69  ction For Functi
35fb0 6f 6e 73 20 7b 48 31 36 32 35 30 7d 20 3c 53 36  ons {H16250} <S6
35fc0 30 36 30 30 3e 3c 53 32 30 32 30 30 3e 0a 2a 2a  0600><S20200>.**
35fd0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
35fe0 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
35ff0 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65  e() interface re
36000 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 0a  turns a copy of.
36010 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74  ** the pointer t
36020 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  o the [database 
36030 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 68 65  connection] (the
36040 20 31 73 74 20 70 61 72 61 6d 65 74 65 72 29 0a   1st parameter).
36050 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74  ** of the [sqlit
36060 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
36070 6f 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71  on()].** and [sq
36080 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
36090 63 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69  ction16()] routi
360a0 6e 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e 61  nes that origina
360b0 6c 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 65  lly.** registere
360c0 64 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  d the applicatio
360d0 6e 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  n defined functi
360e0 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49  on..**.** INVARI
360f0 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 36  ANTS:.**.** {H16
36100 32 35 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  253} The [sqlite
36110 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
36120 64 6c 65 28 43 29 5d 20 69 6e 74 65 72 66 61 63  dle(C)] interfac
36130 65 20 72 65 74 75 72 6e 73 20 61 20 63 6f 70 79  e returns a copy
36140 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   of the.**      
36150 20 20 20 20 44 20 70 6f 69 6e 74 65 72 20 66 72      D pointer fr
36160 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  om the [sqlite3_
36170 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
36180 44 2c 58 2c 4e 2c 45 2c 50 2c 46 2c 53 2c 4c 29  D,X,N,E,P,F,S,L)
36190 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72  ].**          or
361a0 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
361b0 5f 66 75 6e 63 74 69 6f 6e 31 36 28 44 2c 58 2c  _function16(D,X,
361c0 4e 2c 45 2c 50 2c 46 2c 53 2c 4c 29 5d 20 63 61  N,E,P,F,S,L)] ca
361d0 6c 6c 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20  ll that.**      
361e0 20 20 20 20 72 65 67 69 73 74 65 72 65 64 20 74      registered t
361f0 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  he SQL function 
36200 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
36210 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  [sqlite3_context
36220 5d 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ] C..*/.SQLITE_A
36230 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69  PI sqlite3 *sqli
36240 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
36250 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f  andle(sqlite3_co
36260 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  ntext*);../*.** 
36270 43 41 50 49 33 52 45 46 3a 20 46 75 6e 63 74 69  CAPI3REF: Functi
36280 6f 6e 20 41 75 78 69 6c 69 61 72 79 20 44 61 74  on Auxiliary Dat
36290 61 20 7b 48 31 36 32 37 30 7d 20 3c 53 32 30 32  a {H16270} <S202
362a0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  00>.**.** The fo
362b0 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e 63  llowing two func
362c0 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 75 73 65  tions may be use
362d0 64 20 62 79 20 73 63 61 6c 61 72 20 53 51 4c 20  d by scalar SQL 
362e0 66 75 6e 63 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20  functions to.** 
362f0 61 73 73 6f 63 69 61 74 65 20 6d 65 74 61 64 61  associate metada
36300 74 61 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74  ta with argument
36310 20 76 61 6c 75 65 73 2e 20 49 66 20 74 68 65 20   values. If the 
36320 73 61 6d 65 20 76 61 6c 75 65 20 69 73 20 70 61  same value is pa
36330 73 73 65 64 20 74 6f 0a 2a 2a 20 6d 75 6c 74 69  ssed to.** multi
36340 70 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20  ple invocations 
36350 6f 66 20 74 68 65 20 73 61 6d 65 20 53 51 4c 20  of the same SQL 
36360 66 75 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67 20  function during 
36370 71 75 65 72 79 20 65 78 65 63 75 74 69 6f 6e 2c  query execution,
36380 20 75 6e 64 65 72 0a 2a 2a 20 73 6f 6d 65 20 63   under.** some c
36390 69 72 63 75 6d 73 74 61 6e 63 65 73 20 74 68 65  ircumstances the
363a0 20 61 73 73 6f 63 69 61 74 65 64 20 6d 65 74 61   associated meta
363b0 64 61 74 61 20 6d 61 79 20 62 65 20 70 72 65 73  data may be pres
363c0 65 72 76 65 64 2e 20 54 68 69 73 20 6d 61 79 0a  erved. This may.
363d0 2a 2a 20 62 65 20 75 73 65 64 2c 20 66 6f 72 20  ** be used, for 
363e0 65 78 61 6d 70 6c 65 2c 20 74 6f 20 61 64 64 20  example, to add 
363f0 61 20 72 65 67 75 6c 61 72 2d 65 78 70 72 65 73  a regular-expres
36400 73 69 6f 6e 20 6d 61 74 63 68 69 6e 67 20 73 63  sion matching sc
36410 61 6c 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  alar.** function
36420 2e 20 54 68 65 20 63 6f 6d 70 69 6c 65 64 20 76  . The compiled v
36430 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 72 65  ersion of the re
36440 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e  gular expression
36450 20 69 73 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a   is stored as.**
36460 20 6d 65 74 61 64 61 74 61 20 61 73 73 6f 63 69   metadata associ
36470 61 74 65 64 20 77 69 74 68 20 74 68 65 20 53 51  ated with the SQ
36480 4c 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61  L value passed a
36490 73 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78  s the regular ex
364a0 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 70 61 74 74  pression.** patt
364b0 65 72 6e 2e 20 20 54 68 65 20 63 6f 6d 70 69 6c  ern.  The compil
364c0 65 64 20 72 65 67 75 6c 61 72 20 65 78 70 72 65  ed regular expre
364d0 73 73 69 6f 6e 20 63 61 6e 20 62 65 20 72 65 75  ssion can be reu
364e0 73 65 64 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 0a  sed on multiple.
364f0 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f  ** invocations o
36500 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74  f the same funct
36510 69 6f 6e 20 73 6f 20 74 68 61 74 20 74 68 65 20  ion so that the 
36520 6f 72 69 67 69 6e 61 6c 20 70 61 74 74 65 72 6e  original pattern
36530 20 73 74 72 69 6e 67 0a 2a 2a 20 64 6f 65 73 20   string.** does 
36540 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 72  not need to be r
36550 65 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 65 61 63  ecompiled on eac
36560 68 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 0a 2a 2a  h invocation..**
36570 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
36580 67 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e  get_auxdata() in
36590 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
365a0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
365b0 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 61 73 73   metadata.** ass
365c0 6f 63 69 61 74 65 64 20 62 79 20 74 68 65 20 73  ociated by the s
365d0 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61  qlite3_set_auxda
365e0 74 61 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 69  ta() function wi
365f0 74 68 20 74 68 65 20 4e 74 68 20 61 72 67 75 6d  th the Nth argum
36600 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 74 6f 20  ent.** value to 
36610 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d  the application-
36620 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
36630 2e 20 49 66 20 6e 6f 20 6d 65 74 61 64 61 74 61  . If no metadata
36640 20 68 61 73 20 62 65 65 6e 20 65 76 65 72 0a 2a   has been ever.*
36650 2a 20 62 65 65 6e 20 73 65 74 20 66 6f 72 20 74  * been set for t
36660 68 65 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 20  he Nth argument 
36670 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2c  of the function,
36680 20 6f 72 20 69 66 20 74 68 65 20 63 6f 72 72 65   or if the corre
36690 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 66 75 6e 63  sponding.** func
366a0 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 68  tion parameter h
366b0 61 73 20 63 68 61 6e 67 65 64 20 73 69 6e 63 65  as changed since
366c0 20 74 68 65 20 6d 65 74 61 2d 64 61 74 61 20 77   the meta-data w
366d0 61 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20  as set,.** then 
366e0 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64  sqlite3_get_auxd
366f0 61 74 61 28 29 20 72 65 74 75 72 6e 73 20 61 20  ata() returns a 
36700 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a  NULL pointer..**
36710 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
36720 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e  set_auxdata() in
36730 74 65 72 66 61 63 65 20 73 61 76 65 73 20 74 68  terface saves th
36740 65 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 70 6f  e metadata.** po
36750 69 6e 74 65 64 20 74 6f 20 62 79 20 69 74 73 20  inted to by its 
36760 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 61 73  3rd parameter as
36770 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 66 6f   the metadata fo
36780 72 20 74 68 65 20 4e 2d 74 68 0a 2a 2a 20 61 72  r the N-th.** ar
36790 67 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 70  gument of the ap
367a0 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
367b0 64 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 75 62  d function.  Sub
367c0 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73  sequent.** calls
367d0 20 74 6f 20 73 71 6c 69 74 65 33 5f 67 65 74 5f   to sqlite3_get_
367e0 61 75 78 64 61 74 61 28 29 20 6d 69 67 68 74 20  auxdata() might 
367f0 72 65 74 75 72 6e 20 74 68 69 73 20 64 61 74 61  return this data
36800 2c 20 69 66 20 69 74 20 68 61 73 0a 2a 2a 20 6e  , if it has.** n
36810 6f 74 20 62 65 65 6e 20 64 65 73 74 72 6f 79 65  ot been destroye
36820 64 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e  d..** If it is n
36830 6f 74 20 4e 55 4c 4c 2c 20 53 51 4c 69 74 65 20  ot NULL, SQLite 
36840 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20  will invoke the 
36850 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 66 75  destructor.** fu
36860 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 62 79 20  nction given by 
36870 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65  the 4th paramete
36880 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 65 74  r to sqlite3_set
36890 5f 61 75 78 64 61 74 61 28 29 20 6f 6e 0a 2a 2a  _auxdata() on.**
368a0 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 77 68   the metadata wh
368b0 65 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  en the correspon
368c0 64 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 61  ding function pa
368d0 72 61 6d 65 74 65 72 20 63 68 61 6e 67 65 73 0a  rameter changes.
368e0 2a 2a 20 6f 72 20 77 68 65 6e 20 74 68 65 20 53  ** or when the S
368f0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d  QL statement com
36900 70 6c 65 74 65 73 2c 20 77 68 69 63 68 65 76 65  pletes, whicheve
36910 72 20 63 6f 6d 65 73 20 66 69 72 73 74 2e 0a 2a  r comes first..*
36920 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 73 20 66  *.** SQLite is f
36930 72 65 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20  ree to call the 
36940 64 65 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64  destructor and d
36950 72 6f 70 20 6d 65 74 61 64 61 74 61 20 6f 6e 20  rop metadata on 
36960 61 6e 79 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  any.** parameter
36970 20 6f 66 20 61 6e 79 20 66 75 6e 63 74 69 6f 6e   of any function
36980 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 20 20 54   at any time.  T
36990 68 65 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 65  he only guarante
369a0 65 20 69 73 20 74 68 61 74 0a 2a 2a 20 74 68 65  e is that.** the
369b0 20 64 65 73 74 72 75 63 74 6f 72 20 77 69 6c 6c   destructor will
369c0 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72   be called befor
369d0 65 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 69  e the metadata i
369e0 73 20 64 72 6f 70 70 65 64 2e 0a 2a 2a 0a 2a 2a  s dropped..**.**
369f0 20 49 6e 20 70 72 61 63 74 69 63 65 2c 20 6d 65   In practice, me
36a00 74 61 64 61 74 61 20 69 73 20 70 72 65 73 65 72  tadata is preser
36a10 76 65 64 20 62 65 74 77 65 65 6e 20 66 75 6e 63  ved between func
36a20 74 69 6f 6e 20 63 61 6c 6c 73 20 66 6f 72 0a 2a  tion calls for.*
36a30 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68  * expressions th
36a40 61 74 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20  at are constant 
36a50 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e  at compile time.
36a60 20 54 68 69 73 20 69 6e 63 6c 75 64 65 73 20 6c   This includes l
36a70 69 74 65 72 61 6c 0a 2a 2a 20 76 61 6c 75 65 73  iteral.** values
36a80 20 61 6e 64 20 53 51 4c 20 76 61 72 69 61 62 6c   and SQL variabl
36a90 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  es..**.** These 
36aa0 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65  routines must be
36ab0 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65   called from the
36ac0 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20   same thread in 
36ad0 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 53 51 4c  which.** the SQL
36ae0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e   function is run
36af0 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41  ning..**.** INVA
36b00 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48  RIANTS:.**.** {H
36b10 31 36 32 37 32 7d 20 54 68 65 20 5b 73 71 6c 69  16272} The [sqli
36b20 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28  te3_get_auxdata(
36b30 43 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20  C,N)] interface 
36b40 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
36b50 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f  r.**          to
36b60 20 6d 65 74 61 64 61 74 61 20 61 73 73 6f 63 69   metadata associ
36b70 61 74 65 64 20 77 69 74 68 20 74 68 65 20 4e 74  ated with the Nt
36b80 68 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74  h parameter of t
36b90 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 0a  he SQL function.
36ba0 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 6f 73  **          whos
36bb0 65 20 63 6f 6e 74 65 78 74 20 69 73 20 43 2c 20  e context is C, 
36bc0 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65  or NULL if there
36bd0 20 69 73 20 6e 6f 20 6d 65 74 61 64 61 74 61 20   is no metadata 
36be0 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 20 20  associated.**   
36bf0 20 20 20 20 20 20 20 77 69 74 68 20 74 68 61 74         with that
36c00 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a   parameter..**.*
36c10 2a 20 7b 48 31 36 32 37 34 7d 20 54 68 65 20 5b  * {H16274} The [
36c20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64  sqlite3_set_auxd
36c30 61 74 61 28 43 2c 4e 2c 50 2c 44 29 5d 20 69 6e  ata(C,N,P,D)] in
36c40 74 65 72 66 61 63 65 20 61 73 73 69 67 6e 73 20  terface assigns 
36c50 61 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 20 20  a metadata.**   
36c60 20 20 20 20 20 20 20 70 6f 69 6e 74 65 72 20 50         pointer P
36c70 20 74 6f 20 74 68 65 20 4e 74 68 20 70 61 72 61   to the Nth para
36c80 6d 65 74 65 72 20 6f 66 20 74 68 65 20 53 51 4c  meter of the SQL
36c90 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 63   function with c
36ca0 6f 6e 74 65 78 74 20 43 2e 0a 2a 2a 0a 2a 2a 20  ontext C..**.** 
36cb0 7b 48 31 36 32 37 36 7d 20 53 51 4c 69 74 65 20  {H16276} SQLite 
36cc0 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20  will invoke the 
36cd0 64 65 73 74 72 75 63 74 6f 72 20 44 20 77 69 74  destructor D wit
36ce0 68 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d  h a single argum
36cf0 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ent.**          
36d00 77 68 69 63 68 20 69 73 20 74 68 65 20 6d 65 74  which is the met
36d10 61 64 61 74 61 20 70 6f 69 6e 74 65 72 20 50 20  adata pointer P 
36d20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 63 61 6c 6c  following a call
36d30 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   to.**          
36d40 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78  [sqlite3_set_aux
36d50 64 61 74 61 28 43 2c 4e 2c 50 2c 44 29 5d 20 77  data(C,N,P,D)] w
36d60 68 65 6e 20 53 51 4c 69 74 65 20 63 65 61 73 65  hen SQLite cease
36d70 73 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 20 20 20  s to hold.**    
36d80 20 20 20 20 20 20 74 68 65 20 6d 65 74 61 64 61        the metada
36d90 74 61 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 37  ta..**.** {H1627
36da0 37 7d 20 53 51 4c 69 74 65 20 63 65 61 73 65 73  7} SQLite ceases
36db0 20 74 6f 20 68 6f 6c 64 20 6d 65 74 61 64 61 74   to hold metadat
36dc0 61 20 66 6f 72 20 61 6e 20 53 51 4c 20 66 75 6e  a for an SQL fun
36dd0 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 0a  ction parameter.
36de0 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 65 6e  **          when
36df0 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
36e00 61 74 20 70 61 72 61 6d 65 74 65 72 20 63 68 61  at parameter cha
36e10 6e 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36  nges..**.** {H16
36e20 32 37 38 7d 20 57 68 65 6e 20 5b 73 71 6c 69 74  278} When [sqlit
36e30 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 43  e3_set_auxdata(C
36e40 2c 4e 2c 50 2c 44 29 5d 20 69 73 20 69 6e 76 6f  ,N,P,D)] is invo
36e50 6b 65 64 2c 20 74 68 65 20 64 65 73 74 72 75 63  ked, the destruc
36e60 74 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  tor.**          
36e70 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 61 6e  is called for an
36e80 79 20 70 72 69 6f 72 20 6d 65 74 61 64 61 74 61  y prior metadata
36e90 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
36ea0 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69   the same functi
36eb0 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63  on.**          c
36ec0 6f 6e 74 65 78 74 20 43 20 61 6e 64 20 70 61 72  ontext C and par
36ed0 61 6d 65 74 65 72 20 4e 2e 0a 2a 2a 0a 2a 2a 20  ameter N..**.** 
36ee0 7b 48 31 36 32 37 39 7d 20 53 51 4c 69 74 65 20  {H16279} SQLite 
36ef0 77 69 6c 6c 20 63 61 6c 6c 20 64 65 73 74 72 75  will call destru
36f00 63 74 6f 72 73 20 66 6f 72 20 61 6e 79 20 6d 65  ctors for any me
36f10 74 61 64 61 74 61 20 69 74 20 69 73 20 68 6f 6c  tadata it is hol
36f20 64 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20  ding.**         
36f30 20 69 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72   in a particular
36f40 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
36f50 6d 65 6e 74 5d 20 53 20 77 68 65 6e 20 65 69 74  ment] S when eit
36f60 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  her.**          
36f70 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53  [sqlite3_reset(S
36f80 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66  )] or [sqlite3_f
36f90 69 6e 61 6c 69 7a 65 28 53 29 5d 20 69 73 20 63  inalize(S)] is c
36fa0 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  alled..*/.SQLITE
36fb0 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
36fc0 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 73  e3_get_auxdata(s
36fd0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
36fe0 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f   int N);.SQLITE_
36ff0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
37000 5f 73 65 74 5f 61 75 78 64 61 74 61 28 73 71 6c  _set_auxdata(sql
37010 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69  ite3_context*, i
37020 6e 74 20 4e 2c 20 76 6f 69 64 2a 2c 20 76 6f 69  nt N, void*, voi
37030 64 20 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 0a  d (*)(void*));..
37040 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
37050 20 43 6f 6e 73 74 61 6e 74 73 20 44 65 66 69 6e   Constants Defin
37060 69 6e 67 20 53 70 65 63 69 61 6c 20 44 65 73 74  ing Special Dest
37070 72 75 63 74 6f 72 20 42 65 68 61 76 69 6f 72 20  ructor Behavior 
37080 7b 48 31 30 32 38 30 7d 20 3c 53 33 30 31 30 30  {H10280} <S30100
37090 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61 72  >.**.** These ar
370a0 65 20 73 70 65 63 69 61 6c 20 76 61 6c 75 65 73  e special values
370b0 20 66 6f 72 20 74 68 65 20 64 65 73 74 72 75 63   for the destruc
370c0 74 6f 72 20 74 68 61 74 20 69 73 20 70 61 73 73  tor that is pass
370d0 65 64 20 69 6e 20 61 73 20 74 68 65 0a 2a 2a 20  ed in as the.** 
370e0 66 69 6e 61 6c 20 61 72 67 75 6d 65 6e 74 20 74  final argument t
370f0 6f 20 72 6f 75 74 69 6e 65 73 20 6c 69 6b 65 20  o routines like 
37100 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  [sqlite3_result_
37110 62 6c 6f 62 28 29 5d 2e 20 20 49 66 20 74 68 65  blob()].  If the
37120 20 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 61   destructor.** a
37130 72 67 75 6d 65 6e 74 20 69 73 20 53 51 4c 49 54  rgument is SQLIT
37140 45 5f 53 54 41 54 49 43 2c 20 69 74 20 6d 65 61  E_STATIC, it mea
37150 6e 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74  ns that the cont
37160 65 6e 74 20 70 6f 69 6e 74 65 72 20 69 73 20 63  ent pointer is c
37170 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61 6e 64 20 77  onstant.** and w
37180 69 6c 6c 20 6e 65 76 65 72 20 63 68 61 6e 67 65  ill never change
37190 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 6e  .  It does not n
371a0 65 65 64 20 74 6f 20 62 65 20 64 65 73 74 72 6f  eed to be destro
371b0 79 65 64 2e 20 20 54 68 65 0a 2a 2a 20 53 51 4c  yed.  The.** SQL
371c0 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 76 61  ITE_TRANSIENT va
371d0 6c 75 65 20 6d 65 61 6e 73 20 74 68 61 74 20 74  lue means that t
371e0 68 65 20 63 6f 6e 74 65 6e 74 20 77 69 6c 6c 20  he content will 
371f0 6c 69 6b 65 6c 79 20 63 68 61 6e 67 65 20 69 6e  likely change in
37200 0a 2a 2a 20 74 68 65 20 6e 65 61 72 20 66 75 74  .** the near fut
37210 75 72 65 20 61 6e 64 20 74 68 61 74 20 53 51 4c  ure and that SQL
37220 69 74 65 20 73 68 6f 75 6c 64 20 6d 61 6b 65 20  ite should make 
37230 69 74 73 20 6f 77 6e 20 70 72 69 76 61 74 65 20  its own private 
37240 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 63  copy of.** the c
37250 6f 6e 74 65 6e 74 20 62 65 66 6f 72 65 20 72 65  ontent before re
37260 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54  turning..**.** T
37270 68 65 20 74 79 70 65 64 65 66 20 69 73 20 6e 65  he typedef is ne
37280 63 65 73 73 61 72 79 20 74 6f 20 77 6f 72 6b 20  cessary to work 
37290 61 72 6f 75 6e 64 20 70 72 6f 62 6c 65 6d 73 20  around problems 
372a0 69 6e 20 63 65 72 74 61 69 6e 0a 2a 2a 20 43 2b  in certain.** C+
372b0 2b 20 63 6f 6d 70 69 6c 65 72 73 2e 20 20 53 65  + compilers.  Se
372c0 65 20 74 69 63 6b 65 74 20 23 32 31 39 31 2e 0a  e ticket #2191..
372d0 2a 2f 0a 74 79 70 65 64 65 66 20 76 6f 69 64 20  */.typedef void 
372e0 28 2a 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75  (*sqlite3_destru
372f0 63 74 6f 72 5f 74 79 70 65 29 28 76 6f 69 64 2a  ctor_type)(void*
37300 29 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  );.#define SQLIT
37310 45 5f 53 54 41 54 49 43 20 20 20 20 20 20 28 28  E_STATIC      ((
37320 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75 63 74  sqlite3_destruct
37330 6f 72 5f 74 79 70 65 29 30 29 0a 23 64 65 66 69  or_type)0).#defi
37340 6e 65 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  ne SQLITE_TRANSI
37350 45 4e 54 20 20 20 28 28 73 71 6c 69 74 65 33 5f  ENT   ((sqlite3_
37360 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65 29  destructor_type)
37370 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  -1)../*.** CAPI3
37380 52 45 46 3a 20 53 65 74 74 69 6e 67 20 54 68 65  REF: Setting The
37390 20 52 65 73 75 6c 74 20 4f 66 20 41 6e 20 53 51   Result Of An SQ
373a0 4c 20 46 75 6e 63 74 69 6f 6e 20 7b 48 31 36 34  L Function {H164
373b0 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a  00} <S20200>.**.
373c0 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
373d0 73 20 61 72 65 20 75 73 65 64 20 62 79 20 74 68  s are used by th
373e0 65 20 78 46 75 6e 63 20 6f 72 20 78 46 69 6e 61  e xFunc or xFina
373f0 6c 20 63 61 6c 6c 62 61 63 6b 73 20 74 68 61 74  l callbacks that
37400 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 53 51  .** implement SQ
37410 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20  L functions and 
37420 61 67 67 72 65 67 61 74 65 73 2e 20 20 53 65 65  aggregates.  See
37430 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 72 65  .** [sqlite3_cre
37440 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 20  ate_function()] 
37450 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 65  and [sqlite3_cre
37460 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29  ate_function16()
37470 5d 0a 2a 2a 20 66 6f 72 20 61 64 64 69 74 69 6f  ].** for additio
37480 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
37490 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e  .**.** These fun
374a0 63 74 69 6f 6e 73 20 77 6f 72 6b 20 76 65 72 79  ctions work very
374b0 20 6d 75 63 68 20 6c 69 6b 65 20 74 68 65 20 5b   much like the [
374c0 70 61 72 61 6d 65 74 65 72 20 62 69 6e 64 69 6e  parameter bindin
374d0 67 5d 20 66 61 6d 69 6c 79 20 6f 66 0a 2a 2a 20  g] family of.** 
374e0 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74  functions used t
374f0 6f 20 62 69 6e 64 20 76 61 6c 75 65 73 20 74 6f  o bind values to
37500 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73   host parameters
37510 20 69 6e 20 70 72 65 70 61 72 65 64 20 73 74 61   in prepared sta
37520 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 52 65 66 65  tements..** Refe
37530 72 20 74 6f 20 74 68 65 20 5b 53 51 4c 20 70 61  r to the [SQL pa
37540 72 61 6d 65 74 65 72 5d 20 64 6f 63 75 6d 65 6e  rameter] documen
37550 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74  tation for addit
37560 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
37570 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  n..**.** The sql
37580 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
37590 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74  () interface set
375a0 73 20 74 68 65 20 72 65 73 75 6c 74 20 66 72 6f  s the result fro
375b0 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63 61 74  m.** an applicat
375c0 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
375d0 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 42  tion to be the B
375e0 4c 4f 42 20 77 68 6f 73 65 20 63 6f 6e 74 65 6e  LOB whose conten
375f0 74 20 69 73 20 70 6f 69 6e 74 65 64 0a 2a 2a 20  t is pointed.** 
37600 74 6f 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64  to by the second
37610 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64 20 77   parameter and w
37620 68 69 63 68 20 69 73 20 4e 20 62 79 74 65 73 20  hich is N bytes 
37630 6c 6f 6e 67 20 77 68 65 72 65 20 4e 20 69 73 20  long where N is 
37640 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 70 61 72  the.** third par
37650 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  ameter..**.** Th
37660 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
37670 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 69 6e 74 65  _zeroblob() inte
37680 72 66 61 63 65 73 20 73 65 74 20 74 68 65 20 72  rfaces set the r
37690 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20  esult of.** the 
376a0 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69  application-defi
376b0 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  ned function to 
376c0 62 65 20 61 20 42 4c 4f 42 20 63 6f 6e 74 61 69  be a BLOB contai
376d0 6e 69 6e 67 20 61 6c 6c 20 7a 65 72 6f 0a 2a 2a  ning all zero.**
376e0 20 62 79 74 65 73 20 61 6e 64 20 4e 20 62 79 74   bytes and N byt
376f0 65 73 20 69 6e 20 73 69 7a 65 2c 20 77 68 65 72  es in size, wher
37700 65 20 4e 20 69 73 20 74 68 65 20 76 61 6c 75 65  e N is the value
37710 20 6f 66 20 74 68 65 20 32 6e 64 20 70 61 72 61   of the 2nd para
37720 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  meter..**.** The
37730 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
37740 64 6f 75 62 6c 65 28 29 20 69 6e 74 65 72 66 61  double() interfa
37750 63 65 20 73 65 74 73 20 74 68 65 20 72 65 73 75  ce sets the resu
37760 6c 74 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 61 70  lt from.** an ap
37770 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
37780 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65  d function to be
37790 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e   a floating poin
377a0 74 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 65  t value specifie
377b0 64 0a 2a 2a 20 62 79 20 69 74 73 20 32 6e 64 20  d.** by its 2nd 
377c0 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  argument..**.** 
377d0 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  The sqlite3_resu
377e0 6c 74 5f 65 72 72 6f 72 28 29 20 61 6e 64 20 73  lt_error() and s
377f0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
37800 72 6f 72 31 36 28 29 20 66 75 6e 63 74 69 6f 6e  ror16() function
37810 73 0a 2a 2a 20 63 61 75 73 65 20 74 68 65 20 69  s.** cause the i
37820 6d 70 6c 65 6d 65 6e 74 65 64 20 53 51 4c 20 66  mplemented SQL f
37830 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 72 6f 77  unction to throw
37840 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a   an exception..*
37850 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 74 68  * SQLite uses th
37860 65 20 73 74 72 69 6e 67 20 70 6f 69 6e 74 65 64  e string pointed
37870 20 74 6f 20 62 79 20 74 68 65 0a 2a 2a 20 32 6e   to by the.** 2n
37880 64 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 73  d parameter of s
37890 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
378a0 72 6f 72 28 29 20 6f 72 20 73 71 6c 69 74 65 33  ror() or sqlite3
378b0 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28  _result_error16(
378c0 29 0a 2a 2a 20 61 73 20 74 68 65 20 74 65 78 74  ).** as the text
378d0 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 6d 65 73   of an error mes
378e0 73 61 67 65 2e 20 20 53 51 4c 69 74 65 20 69 6e  sage.  SQLite in
378f0 74 65 72 70 72 65 74 73 20 74 68 65 20 65 72 72  terprets the err
37900 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 73 74  or.** message st
37910 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69 74 65  ring from sqlite
37920 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29  3_result_error()
37930 20 61 73 20 55 54 46 2d 38 2e 20 53 51 4c 69 74   as UTF-8. SQLit
37940 65 0a 2a 2a 20 69 6e 74 65 72 70 72 65 74 73 20  e.** interprets 
37950 74 68 65 20 73 74 72 69 6e 67 20 66 72 6f 6d 20  the string from 
37960 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
37970 72 72 6f 72 31 36 28 29 20 61 73 20 55 54 46 2d  rror16() as UTF-
37980 31 36 20 69 6e 20 6e 61 74 69 76 65 0a 2a 2a 20  16 in native.** 
37990 62 79 74 65 20 6f 72 64 65 72 2e 20 20 49 66 20  byte order.  If 
379a0 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65  the third parame
379b0 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72  ter to sqlite3_r
379c0 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 0a 2a 2a  esult_error().**
379d0 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75   or sqlite3_resu
379e0 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 69 73 20  lt_error16() is 
379f0 6e 65 67 61 74 69 76 65 20 74 68 65 6e 20 53 51  negative then SQ
37a00 4c 69 74 65 20 74 61 6b 65 73 20 61 73 20 74 68  Lite takes as th
37a10 65 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61  e error.** messa
37a20 67 65 20 61 6c 6c 20 74 65 78 74 20 75 70 20 74  ge all text up t
37a30 68 72 6f 75 67 68 20 74 68 65 20 66 69 72 73 74  hrough the first
37a40 20 7a 65 72 6f 20 63 68 61 72 61 63 74 65 72 2e   zero character.
37a50 0a 2a 2a 20 49 66 20 74 68 65 20 74 68 69 72 64  .** If the third
37a60 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71   parameter to sq
37a70 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
37a80 6f 72 28 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74  or() or.** sqlit
37a90 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31  e3_result_error1
37aa0 36 28 29 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74  6() is non-negat
37ab0 69 76 65 20 74 68 65 6e 20 53 51 4c 69 74 65 20  ive then SQLite 
37ac0 74 61 6b 65 73 20 74 68 61 74 20 6d 61 6e 79 0a  takes that many.
37ad0 2a 2a 20 62 79 74 65 73 20 28 6e 6f 74 20 63 68  ** bytes (not ch
37ae0 61 72 61 63 74 65 72 73 29 20 66 72 6f 6d 20 74  aracters) from t
37af0 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72  he 2nd parameter
37b00 20 61 73 20 74 68 65 20 65 72 72 6f 72 20 6d 65   as the error me
37b10 73 73 61 67 65 2e 0a 2a 2a 20 54 68 65 20 73 71  ssage..** The sq
37b20 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
37b30 6f 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  or() and sqlite3
37b40 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28  _result_error16(
37b50 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 6d 61  ).** routines ma
37b60 6b 65 20 61 20 70 72 69 76 61 74 65 20 63 6f 70  ke a private cop
37b70 79 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 6d  y of the error m
37b80 65 73 73 61 67 65 20 74 65 78 74 20 62 65 66 6f  essage text befo
37b90 72 65 0a 2a 2a 20 74 68 65 79 20 72 65 74 75 72  re.** they retur
37ba0 6e 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 63  n.  Hence, the c
37bb0 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  alling function 
37bc0 63 61 6e 20 64 65 61 6c 6c 6f 63 61 74 65 20 6f  can deallocate o
37bd0 72 0a 2a 2a 20 6d 6f 64 69 66 79 20 74 68 65 20  r.** modify the 
37be0 74 65 78 74 20 61 66 74 65 72 20 74 68 65 79 20  text after they 
37bf0 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 68  return without h
37c00 61 72 6d 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69  arm..** The sqli
37c10 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
37c20 5f 63 6f 64 65 28 29 20 66 75 6e 63 74 69 6f 6e  _code() function
37c30 20 63 68 61 6e 67 65 73 20 74 68 65 20 65 72 72   changes the err
37c40 6f 72 20 63 6f 64 65 0a 2a 2a 20 72 65 74 75 72  or code.** retur
37c50 6e 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 73  ned by SQLite as
37c60 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61 6e 20   a result of an 
37c70 65 72 72 6f 72 20 69 6e 20 61 20 66 75 6e 63 74  error in a funct
37c80 69 6f 6e 2e 20 20 42 79 20 64 65 66 61 75 6c 74  ion.  By default
37c90 2c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 63  ,.** the error c
37ca0 6f 64 65 20 69 73 20 53 51 4c 49 54 45 5f 45 52  ode is SQLITE_ER
37cb0 52 4f 52 2e 20 20 41 20 73 75 62 73 65 71 75 65  ROR.  A subseque
37cc0 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  nt call to sqlit
37cd0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
37ce0 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f  ).** or sqlite3_
37cf0 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29  result_error16()
37d00 20 72 65 73 65 74 73 20 74 68 65 20 65 72 72 6f   resets the erro
37d10 72 20 63 6f 64 65 20 74 6f 20 53 51 4c 49 54 45  r code to SQLITE
37d20 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 54 68  _ERROR..**.** Th
37d30 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
37d40 5f 74 6f 6f 62 69 67 28 29 20 69 6e 74 65 72 66  _toobig() interf
37d50 61 63 65 20 63 61 75 73 65 73 20 53 51 4c 69 74  ace causes SQLit
37d60 65 20 74 6f 20 74 68 72 6f 77 20 61 6e 20 65 72  e to throw an er
37d70 72 6f 72 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e  ror.** indicatin
37d80 67 20 74 68 61 74 20 61 20 73 74 72 69 6e 67 20  g that a string 
37d90 6f 72 20 42 4c 4f 42 20 69 73 20 74 6f 20 6c 6f  or BLOB is to lo
37da0 6e 67 20 74 6f 20 72 65 70 72 65 73 65 6e 74 2e  ng to represent.
37db0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
37dc0 65 33 5f 72 65 73 75 6c 74 5f 6e 6f 6d 65 6d 28  e3_result_nomem(
37dd0 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 75 73  ) interface caus
37de0 65 73 20 53 51 4c 69 74 65 20 74 6f 20 74 68 72  es SQLite to thr
37df0 6f 77 20 61 6e 20 65 72 72 6f 72 0a 2a 2a 20 69  ow an error.** i
37e00 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 61  ndicating that a
37e10 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
37e20 6f 6e 20 66 61 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a  on failed..**.**
37e30 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   The sqlite3_res
37e40 75 6c 74 5f 69 6e 74 28 29 20 69 6e 74 65 72 66  ult_int() interf
37e50 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65 74  ace sets the ret
37e60 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20  urn value.** of 
37e70 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d  the application-
37e80 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
37e90 20 74 6f 20 62 65 20 74 68 65 20 33 32 2d 62 69   to be the 32-bi
37ea0 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
37eb0 0a 2a 2a 20 76 61 6c 75 65 20 67 69 76 65 6e 20  .** value given 
37ec0 69 6e 20 74 68 65 20 32 6e 64 20 61 72 67 75 6d  in the 2nd argum
37ed0 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ent..** The sqli
37ee0 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34  te3_result_int64
37ef0 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74  () interface set
37f00 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  s the return val
37f10 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61 70 70  ue.** of the app
37f20 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
37f30 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20   function to be 
37f40 74 68 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65  the 64-bit signe
37f50 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 76 61 6c  d integer.** val
37f60 75 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20  ue given in the 
37f70 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a  2nd argument..**
37f80 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
37f90 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 29 20 69 6e  result_null() in
37fa0 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65  terface sets the
37fb0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a   return value.**
37fc0 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 74   of the applicat
37fd0 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
37fe0 74 69 6f 6e 20 74 6f 20 62 65 20 4e 55 4c 4c 2e  tion to be NULL.
37ff0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
38000 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 29  e3_result_text()
38010 2c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  , sqlite3_result
38020 5f 74 65 78 74 31 36 28 29 2c 0a 2a 2a 20 73 71  _text16(),.** sq
38030 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
38040 74 31 36 6c 65 28 29 2c 20 61 6e 64 20 73 71 6c  t16le(), and sql
38050 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
38060 31 36 62 65 28 29 20 69 6e 74 65 72 66 61 63 65  16be() interface
38070 73 0a 2a 2a 20 73 65 74 20 74 68 65 20 72 65 74  s.** set the ret
38080 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 65  urn value of the
38090 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66   application-def
380a0 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f  ined function to
380b0 20 62 65 0a 2a 2a 20 61 20 74 65 78 74 20 73 74   be.** a text st
380c0 72 69 6e 67 20 77 68 69 63 68 20 69 73 20 72 65  ring which is re
380d0 70 72 65 73 65 6e 74 65 64 20 61 73 20 55 54 46  presented as UTF
380e0 2d 38 2c 20 55 54 46 2d 31 36 20 6e 61 74 69 76  -8, UTF-16 nativ
380f0 65 20 62 79 74 65 20 6f 72 64 65 72 2c 0a 2a 2a  e byte order,.**
38100 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 20 65   UTF-16 little e
38110 6e 64 69 61 6e 2c 20 6f 72 20 55 54 46 2d 31 36  ndian, or UTF-16
38120 20 62 69 67 20 65 6e 64 69 61 6e 2c 20 72 65 73   big endian, res
38130 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 53 51  pectively..** SQ
38140 4c 69 74 65 20 74 61 6b 65 73 20 74 68 65 20 74  Lite takes the t
38150 65 78 74 20 72 65 73 75 6c 74 20 66 72 6f 6d 20  ext result from 
38160 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
38170 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 32 6e 64 20  from.** the 2nd 
38180 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74 68 65  parameter of the
38190 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
381a0 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73  text* interfaces
381b0 2e 0a 2a 2a 20 49 66 20 74 68 65 20 33 72 64 20  ..** If the 3rd 
381c0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65  parameter to the
381d0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
381e0 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73  text* interfaces
381f0 0a 2a 2a 20 69 73 20 6e 65 67 61 74 69 76 65 2c  .** is negative,
38200 20 74 68 65 6e 20 53 51 4c 69 74 65 20 74 61 6b   then SQLite tak
38210 65 73 20 72 65 73 75 6c 74 20 74 65 78 74 20 66  es result text f
38220 72 6f 6d 20 74 68 65 20 32 6e 64 20 70 61 72 61  rom the 2nd para
38230 6d 65 74 65 72 0a 2a 2a 20 74 68 72 6f 75 67 68  meter.** through
38240 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f 20   the first zero 
38250 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49 66  character..** If
38260 20 74 68 65 20 33 72 64 20 70 61 72 61 6d 65 74   the 3rd paramet
38270 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65  er to the sqlite
38280 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 69  3_result_text* i
38290 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 69 73 20  nterfaces.** is 
382a0 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 68  non-negative, th
382b0 65 6e 20 61 73 20 6d 61 6e 79 20 62 79 74 65 73  en as many bytes
382c0 20 28 6e 6f 74 20 63 68 61 72 61 63 74 65 72 73   (not characters
382d0 29 20 6f 66 20 74 68 65 20 74 65 78 74 0a 2a 2a  ) of the text.**
382e0 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74   pointed to by t
382f0 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72  he 2nd parameter
38300 20 61 72 65 20 74 61 6b 65 6e 20 61 73 20 74 68   are taken as th
38310 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  e application-de
38320 66 69 6e 65 64 0a 2a 2a 20 66 75 6e 63 74 69 6f  fined.** functio
38330 6e 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20  n result..** If 
38340 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65  the 4th paramete
38350 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  r to the sqlite3
38360 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e  _result_text* in
38370 74 65 72 66 61 63 65 73 0a 2a 2a 20 6f 72 20 73  terfaces.** or s
38380 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c  qlite3_result_bl
38390 6f 62 20 69 73 20 61 20 6e 6f 6e 2d 4e 55 4c 4c  ob is a non-NULL
383a0 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 53   pointer, then S
383b0 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 61 74  QLite calls that
383c0 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 73 20  .** function as 
383d0 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 6f  the destructor o
383e0 6e 20 74 68 65 20 74 65 78 74 20 6f 72 20 42 4c  n the text or BL
383f0 4f 42 20 72 65 73 75 6c 74 20 77 68 65 6e 20 69  OB result when i
38400 74 20 68 61 73 0a 2a 2a 20 66 69 6e 69 73 68 65  t has.** finishe
38410 64 20 75 73 69 6e 67 20 74 68 61 74 20 72 65 73  d using that res
38420 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 34  ult..** If the 4
38430 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  th parameter to 
38440 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  the sqlite3_resu
38450 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61  lt_text* interfa
38460 63 65 73 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65  ces or.** sqlite
38470 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73  3_result_blob is
38480 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f 6e   the special con
38490 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 53 54 41  stant SQLITE_STA
384a0 54 49 43 2c 20 74 68 65 6e 20 53 51 4c 69 74 65  TIC, then SQLite
384b0 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 61 74  .** assumes that
384c0 20 74 68 65 20 74 65 78 74 20 6f 72 20 42 4c 4f   the text or BLO
384d0 42 20 72 65 73 75 6c 74 20 69 73 20 69 6e 20 63  B result is in c
384e0 6f 6e 73 74 61 6e 74 20 73 70 61 63 65 20 61 6e  onstant space an
384f0 64 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 6f  d does not.** co
38500 70 79 20 74 68 65 20 69 74 20 6f 72 20 63 61 6c  py the it or cal
38510 6c 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 77  l a destructor w
38520 68 65 6e 20 69 74 20 68 61 73 20 66 69 6e 69 73  hen it has finis
38530 68 65 64 20 75 73 69 6e 67 20 74 68 61 74 20 72  hed using that r
38540 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65  esult..** If the
38550 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74   4th parameter t
38560 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65  o the sqlite3_re
38570 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72  sult_text* inter
38580 66 61 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69  faces.** or sqli
38590 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20  te3_result_blob 
385a0 69 73 20 74 68 65 20 73 70 65 63 69 61 6c 20 63  is the special c
385b0 6f 6e 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 54  onstant SQLITE_T
385c0 52 41 4e 53 49 45 4e 54 0a 2a 2a 20 74 68 65 6e  RANSIENT.** then
385d0 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 61 20   SQLite makes a 
385e0 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 73 75  copy of the resu
385f0 6c 74 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62  lt into space ob
38600 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 66  tained from.** f
38610 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  rom [sqlite3_mal
38620 6c 6f 63 28 29 5d 20 62 65 66 6f 72 65 20 69 74  loc()] before it
38630 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20   returns..**.** 
38640 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  The sqlite3_resu
38650 6c 74 5f 76 61 6c 75 65 28 29 20 69 6e 74 65 72  lt_value() inter
38660 66 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65  face sets the re
38670 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61  sult of.** the a
38680 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
38690 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62  ed function to b
386a0 65 20 61 20 63 6f 70 79 20 74 68 65 0a 2a 2a 20  e a copy the.** 
386b0 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c  [unprotected sql
386c0 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65  ite3_value] obje
386d0 63 74 20 73 70 65 63 69 66 69 65 64 20 62 79 20  ct specified by 
386e0 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65  the 2nd paramete
386f0 72 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74  r.  The.** sqlit
38700 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28  e3_result_value(
38710 29 20 69 6e 74 65 72 66 61 63 65 20 6d 61 6b 65  ) interface make
38720 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  s a copy of the 
38730 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 0a  [sqlite3_value].
38740 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20 5b  ** so that the [
38750 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 73  sqlite3_value] s
38760 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20  pecified in the 
38770 70 61 72 61 6d 65 74 65 72 20 6d 61 79 20 63 68  parameter may ch
38780 61 6e 67 65 20 6f 72 0a 2a 2a 20 62 65 20 64 65  ange or.** be de
38790 61 6c 6c 6f 63 61 74 65 64 20 61 66 74 65 72 20  allocated after 
387a0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76  sqlite3_result_v
387b0 61 6c 75 65 28 29 20 72 65 74 75 72 6e 73 20 77  alue() returns w
387c0 69 74 68 6f 75 74 20 68 61 72 6d 2e 0a 2a 2a 20  ithout harm..** 
387d0 41 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c  A [protected sql
387e0 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65  ite3_value] obje
387f0 63 74 20 6d 61 79 20 61 6c 77 61 79 73 20 62 65  ct may always be
38800 20 75 73 65 64 20 77 68 65 72 65 20 61 6e 0a 2a   used where an.*
38810 2a 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73  * [unprotected s
38820 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62  qlite3_value] ob
38830 6a 65 63 74 20 69 73 20 72 65 71 75 69 72 65 64  ject is required
38840 2c 20 73 6f 20 65 69 74 68 65 72 0a 2a 2a 20 6b  , so either.** k
38850 69 6e 64 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f  ind of [sqlite3_
38860 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 63 61  value] object ca
38870 6e 20 62 65 20 75 73 65 64 20 77 69 74 68 20 74  n be used with t
38880 68 69 73 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a  his interface..*
38890 2a 0a 2a 2a 20 49 66 20 74 68 65 73 65 20 72 6f  *.** If these ro
388a0 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c 65  utines are calle
388b0 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68  d from within th
388c0 65 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65  e different thre
388d0 61 64 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 6f  ad.** than the o
388e0 6e 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ne containing th
388f0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  e application-de
38900 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74  fined function t
38910 68 61 74 20 72 65 63 65 69 76 65 64 0a 2a 2a 20  hat received.** 
38920 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e  the [sqlite3_con
38930 74 65 78 74 5d 20 70 6f 69 6e 74 65 72 2c 20 74  text] pointer, t
38940 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 75  he results are u
38950 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  ndefined..**.** 
38960 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a  INVARIANTS:.**.*
38970 2a 20 7b 48 31 36 34 30 33 7d 20 54 68 65 20 64  * {H16403} The d
38980 65 66 61 75 6c 74 20 72 65 74 75 72 6e 20 76 61  efault return va
38990 6c 75 65 20 66 72 6f 6d 20 61 6e 79 20 53 51 4c  lue from any SQL
389a0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c   function is NUL
389b0 4c 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 30 36  L..**.** {H16406
389c0 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72  } The [sqlite3_r
389d0 65 73 75 6c 74 5f 62 6c 6f 62 28 43 2c 56 2c 4e  esult_blob(C,V,N
389e0 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63  ,D)] interface c
389f0 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 20 20  hanges the.**   
38a00 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61         return va
38a10 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20  lue of function 
38a20 43 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 20 74  C to be a BLOB t
38a30 68 61 74 20 69 73 20 4e 20 62 79 74 65 73 0a 2a  hat is N bytes.*
38a40 2a 20 20 20 20 20 20 20 20 20 20 69 6e 20 6c 65  *          in le
38a50 6e 67 74 68 20 61 6e 64 20 77 69 74 68 20 63 6f  ngth and with co
38a60 6e 74 65 6e 74 20 70 6f 69 6e 74 65 64 20 74 6f  ntent pointed to
38a70 20 62 79 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31   by V..**.** {H1
38a80 36 34 30 39 7d 20 54 68 65 20 5b 73 71 6c 69 74  6409} The [sqlit
38a90 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65  e3_result_double
38aa0 28 43 2c 56 29 5d 20 69 6e 74 65 72 66 61 63 65  (C,V)] interface
38ab0 20 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20   changes the.** 
38ac0 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
38ad0 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f  value of functio
38ae0 6e 20 43 20 74 6f 20 62 65 20 74 68 65 20 66 6c  n C to be the fl
38af0 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c  oating point val
38b00 75 65 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36  ue V..**.** {H16
38b10 34 31 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  412} The [sqlite
38b20 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 43  3_result_error(C
38b30 2c 56 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65  ,V,N)] interface
38b40 20 63 68 61 6e 67 65 73 20 74 68 65 20 72 65 74   changes the ret
38b50 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  urn.**          
38b60 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f  value of functio
38b70 6e 20 43 20 74 6f 20 62 65 20 61 6e 20 65 78 63  n C to be an exc
38b80 65 70 74 69 6f 6e 20 77 69 74 68 20 65 72 72 6f  eption with erro
38b90 72 20 63 6f 64 65 0a 2a 2a 20 20 20 20 20 20 20  r code.**       
38ba0 20 20 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52     [SQLITE_ERROR
38bb0 5d 20 61 6e 64 20 61 20 55 54 46 2d 38 20 65 72  ] and a UTF-8 er
38bc0 72 6f 72 20 6d 65 73 73 61 67 65 20 63 6f 70 69  ror message copi
38bd0 65 64 20 66 72 6f 6d 20 56 20 75 70 20 74 6f 20  ed from V up to 
38be0 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
38bf0 66 69 72 73 74 20 7a 65 72 6f 20 62 79 74 65 20  first zero byte 
38c00 6f 72 20 75 6e 74 69 6c 20 4e 20 62 79 74 65 73  or until N bytes
38c10 20 61 72 65 20 72 65 61 64 20 69 66 20 4e 20 69   are read if N i
38c20 73 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2a 0a 2a  s positive..**.*
38c30 2a 20 7b 48 31 36 34 31 35 7d 20 54 68 65 20 5b  * {H16415} The [
38c40 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
38c50 72 72 6f 72 31 36 28 43 2c 56 2c 4e 29 5d 20 69  rror16(C,V,N)] i
38c60 6e 74 65 72 66 61 63 65 20 63 68 61 6e 67 65 73  nterface changes
38c70 20 74 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 20   the return.**  
38c80 20 20 20 20 20 20 20 20 76 61 6c 75 65 20 6f 66          value of
38c90 20 66 75 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62   function C to b
38ca0 65 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 77  e an exception w
38cb0 69 74 68 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a  ith error code.*
38cc0 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49  *          [SQLI
38cd0 54 45 5f 45 52 52 4f 52 5d 20 61 6e 64 20 61 20  TE_ERROR] and a 
38ce0 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79  UTF-16 native by
38cf0 74 65 20 6f 72 64 65 72 20 65 72 72 6f 72 20 6d  te order error m
38d00 65 73 73 61 67 65 0a 2a 2a 20 20 20 20 20 20 20  essage.**       
38d10 20 20 20 63 6f 70 69 65 64 20 66 72 6f 6d 20 56     copied from V
38d20 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 73 74   up to the first
38d30 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72   zero terminator
38d40 20 6f 72 20 75 6e 74 69 6c 20 4e 20 62 79 74 65   or until N byte
38d50 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 72  s.**          ar
38d60 65 20 72 65 61 64 20 69 66 20 4e 20 69 73 20 70  e read if N is p
38d70 6f 73 69 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b  ositive..**.** {
38d80 48 31 36 34 31 38 7d 20 54 68 65 20 5b 73 71 6c  H16418} The [sql
38d90 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
38da0 72 5f 74 6f 6f 62 69 67 28 43 29 5d 20 69 6e 74  r_toobig(C)] int
38db0 65 72 66 61 63 65 20 63 68 61 6e 67 65 73 20 74  erface changes t
38dc0 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20  he return.**    
38dd0 20 20 20 20 20 20 76 61 6c 75 65 20 6f 66 20 74        value of t
38de0 68 65 20 66 75 6e 63 74 69 6f 6e 20 43 20 74 6f  he function C to
38df0 20 62 65 20 61 6e 20 65 78 63 65 70 74 69 6f 6e   be an exception
38e00 20 77 69 74 68 20 65 72 72 6f 72 20 63 6f 64 65   with error code
38e10 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51  .**          [SQ
38e20 4c 49 54 45 5f 54 4f 4f 42 49 47 5d 20 61 6e 64  LITE_TOOBIG] and
38e30 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20   an appropriate 
38e40 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a  error message..*
38e50 2a 0a 2a 2a 20 7b 48 31 36 34 32 31 7d 20 54 68  *.** {H16421} Th
38e60 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  e [sqlite3_resul
38e70 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 43 29  t_error_nomem(C)
38e80 5d 20 69 6e 74 65 72 66 61 63 65 20 63 68 61 6e  ] interface chan
38e90 67 65 73 20 74 68 65 20 72 65 74 75 72 6e 0a 2a  ges the return.*
38ea0 2a 20 20 20 20 20 20 20 20 20 20 76 61 6c 75 65  *          value
38eb0 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
38ec0 20 43 20 74 6f 20 62 65 20 61 6e 20 65 78 63 65   C to be an exce
38ed0 70 74 69 6f 6e 20 77 69 74 68 20 65 72 72 6f 72  ption with error
38ee0 20 63 6f 64 65 0a 2a 2a 20 20 20 20 20 20 20 20   code.**        
38ef0 20 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d    [SQLITE_NOMEM]
38f00 20 61 6e 64 20 61 6e 20 61 70 70 72 6f 70 72 69   and an appropri
38f10 61 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  ate error messag
38f20 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 32 34  e..**.** {H16424
38f30 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72  } The [sqlite3_r
38f40 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65  esult_error_code
38f50 28 43 2c 45 29 5d 20 69 6e 74 65 72 66 61 63 65  (C,E)] interface
38f60 20 63 68 61 6e 67 65 73 20 74 68 65 20 72 65 74   changes the ret
38f70 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  urn.**          
38f80 76 61 6c 75 65 20 6f 66 20 74 68 65 20 66 75 6e  value of the fun
38f90 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 61 6e  ction C to be an
38fa0 20 65 78 63 65 70 74 69 6f 6e 20 77 69 74 68 20   exception with 
38fb0 65 72 72 6f 72 20 63 6f 64 65 20 45 2e 0a 2a 2a  error code E..**
38fc0 20 20 20 20 20 20 20 20 20 20 54 68 65 20 65 72            The er
38fd0 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74  ror message text
38fe0 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a   is unchanged..*
38ff0 2a 0a 2a 2a 20 7b 48 31 36 34 32 37 7d 20 54 68  *.** {H16427} Th
39000 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  e [sqlite3_resul
39010 74 5f 69 6e 74 28 43 2c 56 29 5d 20 69 6e 74 65  t_int(C,V)] inte
39020 72 66 61 63 65 20 63 68 61 6e 67 65 73 20 74 68  rface changes th
39030 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65  e.**          re
39040 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 75  turn value of fu
39050 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 74  nction C to be t
39060 68 65 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65  he 32-bit intege
39070 72 20 76 61 6c 75 65 20 56 2e 0a 2a 2a 0a 2a 2a  r value V..**.**
39080 20 7b 48 31 36 34 33 30 7d 20 54 68 65 20 5b 73   {H16430} The [s
39090 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
390a0 74 36 34 28 43 2c 56 29 5d 20 69 6e 74 65 72 66  t64(C,V)] interf
390b0 61 63 65 20 63 68 61 6e 67 65 73 20 74 68 65 0a  ace changes the.
390c0 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  **          retu
390d0 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 75 6e 63  rn value of func
390e0 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 74 68 65  tion C to be the
390f0 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20   64-bit integer 
39100 76 61 6c 75 65 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b  value V..**.** {
39110 48 31 36 34 33 33 7d 20 54 68 65 20 5b 73 71 6c  H16433} The [sql
39120 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
39130 28 43 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63  (C)] interface c
39140 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 20 20  hanges the.**   
39150 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61         return va
39160 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20  lue of function 
39170 43 20 74 6f 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a  C to be NULL..**
39180 0a 2a 2a 20 7b 48 31 36 34 33 36 7d 20 54 68 65  .** {H16436} The
39190 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74   [sqlite3_result
391a0 5f 74 65 78 74 28 43 2c 56 2c 4e 2c 44 29 5d 20  _text(C,V,N,D)] 
391b0 69 6e 74 65 72 66 61 63 65 20 63 68 61 6e 67 65  interface change
391c0 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  s the.**        
391d0 20 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f    return value o
391e0 66 20 66 75 6e 63 74 69 6f 6e 20 43 20 74 6f 20  f function C to 
391f0 62 65 20 74 68 65 20 55 54 46 2d 38 20 73 74 72  be the UTF-8 str
39200 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ing.**          
39210 56 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 73  V up to the firs
39220 74 20 7a 65 72 6f 20 69 66 20 4e 20 69 73 20 6e  t zero if N is n
39230 65 67 61 74 69 76 65 0a 2a 2a 20 20 20 20 20 20  egative.**      
39240 20 20 20 20 6f 72 20 74 68 65 20 66 69 72 73 74      or the first
39250 20 4e 20 62 79 74 65 73 20 6f 66 20 56 20 69 66   N bytes of V if
39260 20 4e 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69   N is non-negati
39270 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 33  ve..**.** {H1643
39280 39 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  9} The [sqlite3_
39290 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 43 2c  result_text16(C,
392a0 56 2c 4e 2c 44 29 5d 20 69 6e 74 65 72 66 61 63  V,N,D)] interfac
392b0 65 20 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a  e changes the.**
392c0 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
392d0 20 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69   value of functi
392e0 6f 6e 20 43 20 74 6f 20 62 65 20 74 68 65 20 55  on C to be the U
392f0 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79 74  TF-16 native byt
39300 65 20 6f 72 64 65 72 0a 2a 2a 20 20 20 20 20 20  e order.**      
39310 20 20 20 20 73 74 72 69 6e 67 20 56 20 75 70 20      string V up 
39320 74 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65 72  to the first zer
39330 6f 20 69 66 20 4e 20 69 73 20 6e 65 67 61 74 69  o if N is negati
39340 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  ve.**          o
39350 72 20 74 68 65 20 66 69 72 73 74 20 4e 20 62 79  r the first N by
39360 74 65 73 20 6f 66 20 56 20 69 66 20 4e 20 69 73  tes of V if N is
39370 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a   non-negative..*
39380 2a 0a 2a 2a 20 7b 48 31 36 34 34 32 7d 20 54 68  *.** {H16442} Th
39390 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  e [sqlite3_resul
393a0 74 5f 74 65 78 74 31 36 62 65 28 43 2c 56 2c 4e  t_text16be(C,V,N
393b0 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63  ,D)] interface c
393c0 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 20 20  hanges the.**   
393d0 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61         return va
393e0 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20  lue of function 
393f0 43 20 74 6f 20 62 65 20 74 68 65 20 55 54 46 2d  C to be the UTF-
39400 31 36 20 62 69 67 2d 65 6e 64 69 61 6e 0a 2a 2a  16 big-endian.**
39410 20 20 20 20 20 20 20 20 20 20 73 74 72 69 6e 67            string
39420 20 56 20 75 70 20 74 6f 20 74 68 65 20 66 69 72   V up to the fir
39430 73 74 20 7a 65 72 6f 20 69 66 20 4e 20 69 73 20  st zero if N is 
39440 6e 65 67 61 74 69 76 65 0a 2a 2a 20 20 20 20 20  negative.**     
39450 20 20 20 20 20 6f 72 20 74 68 65 20 66 69 72 73       or the firs
39460 74 20 4e 20 62 79 74 65 73 20 6f 72 20 56 20 69  t N bytes or V i
39470 66 20 4e 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74  f N is non-negat
39480 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34  ive..**.** {H164
39490 34 35 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  45} The [sqlite3
394a0 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65  _result_text16le
394b0 28 43 2c 56 2c 4e 2c 44 29 5d 20 69 6e 74 65 72  (C,V,N,D)] inter
394c0 66 61 63 65 20 63 68 61 6e 67 65 73 20 74 68 65  face changes the
394d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74  .**          ret
394e0 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 75 6e  urn value of fun
394f0 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 74 68  ction C to be th
39500 65 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 2d  e UTF-16 little-
39510 65 6e 64 69 61 6e 0a 2a 2a 20 20 20 20 20 20 20  endian.**       
39520 20 20 20 73 74 72 69 6e 67 20 56 20 75 70 20 74     string V up t
39530 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f  o the first zero
39540 20 69 66 20 4e 20 69 73 20 6e 65 67 61 74 69 76   if N is negativ
39550 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72  e.**          or
39560 20 74 68 65 20 66 69 72 73 74 20 4e 20 62 79 74   the first N byt
39570 65 73 20 6f 66 20 56 20 69 66 20 4e 20 69 73 20  es of V if N is 
39580 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a 2a  non-negative..**
39590 0a 2a 2a 20 7b 48 31 36 34 34 38 7d 20 54 68 65  .** {H16448} The
395a0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74   [sqlite3_result
395b0 5f 76 61 6c 75 65 28 43 2c 56 29 5d 20 69 6e 74  _value(C,V)] int
395c0 65 72 66 61 63 65 20 63 68 61 6e 67 65 73 20 74  erface changes t
395d0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72  he.**          r
395e0 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66  eturn value of f
395f0 75 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20  unction C to be 
39600 74 68 65 20 5b 75 6e 70 72 6f 74 65 63 74 65 64  the [unprotected
39610 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 0a   sqlite3_value].
39620 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 62 6a 65  **          obje
39630 63 74 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36  ct V..**.** {H16
39640 34 35 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  451} The [sqlite
39650 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f  3_result_zeroblo
39660 62 28 43 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63  b(C,N)] interfac
39670 65 20 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a  e changes the.**
39680 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
39690 20 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69   value of functi
396a0 6f 6e 20 43 20 74 6f 20 62 65 20 61 6e 20 4e 2d  on C to be an N-
396b0 62 79 74 65 20 42 4c 4f 42 20 6f 66 20 61 6c 6c  byte BLOB of all
396c0 20 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48   zeros..**.** {H
396d0 31 36 34 35 34 7d 20 54 68 65 20 5b 73 71 6c 69  16454} The [sqli
396e0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
396f0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
39700 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28  _result_error16(
39710 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69  )].**          i
39720 6e 74 65 72 66 61 63 65 73 20 6d 61 6b 65 20 61  nterfaces make a
39730 20 63 6f 70 79 20 6f 66 20 74 68 65 69 72 20 65   copy of their e
39740 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 72  rror message str
39750 69 6e 67 73 20 62 65 66 6f 72 65 0a 2a 2a 20 20  ings before.**  
39760 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 69 6e          returnin
39770 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 35 37  g..**.** {H16457
39780 7d 20 49 66 20 74 68 65 20 44 20 64 65 73 74 72  } If the D destr
39790 75 63 74 6f 72 20 70 61 72 61 6d 65 74 65 72 20  uctor parameter 
397a0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75  to [sqlite3_resu
397b0 6c 74 5f 62 6c 6f 62 28 43 2c 56 2c 4e 2c 44 29  lt_blob(C,V,N,D)
397c0 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  ],.**          [
397d0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
397e0 65 78 74 28 43 2c 56 2c 4e 2c 44 29 5d 2c 20 5b  ext(C,V,N,D)], [
397f0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
39800 65 78 74 31 36 28 43 2c 56 2c 4e 2c 44 29 5d 2c  ext16(C,V,N,D)],
39810 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71  .**          [sq
39820 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
39830 74 31 36 62 65 28 43 2c 56 2c 4e 2c 44 29 5d 2c  t16be(C,V,N,D)],
39840 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   or.**          
39850 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  [sqlite3_result_
39860 74 65 78 74 31 36 6c 65 28 43 2c 56 2c 4e 2c 44  text16le(C,V,N,D
39870 29 5d 20 69 73 20 74 68 65 20 63 6f 6e 73 74 61  )] is the consta
39880 6e 74 20 5b 53 51 4c 49 54 45 5f 53 54 41 54 49  nt [SQLITE_STATI
39890 43 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  C].**          t
398a0 68 65 6e 20 6e 6f 20 64 65 73 74 72 75 63 74 6f  hen no destructo
398b0 72 20 69 73 20 65 76 65 72 20 63 61 6c 6c 65 64  r is ever called
398c0 20 6f 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20   on the pointer 
398d0 56 20 61 6e 64 20 53 51 4c 69 74 65 0a 2a 2a 20  V and SQLite.** 
398e0 20 20 20 20 20 20 20 20 20 61 73 73 75 6d 65 73           assumes
398f0 20 74 68 61 74 20 56 20 69 73 20 69 6d 6d 75 74   that V is immut
39900 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36  able..**.** {H16
39910 34 36 30 7d 20 49 66 20 74 68 65 20 44 20 64 65  460} If the D de
39920 73 74 72 75 63 74 6f 72 20 70 61 72 61 6d 65 74  structor paramet
39930 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72  er to [sqlite3_r
39940 65 73 75 6c 74 5f 62 6c 6f 62 28 43 2c 56 2c 4e  esult_blob(C,V,N
39950 2c 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20  ,D)],.**        
39960 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c    [sqlite3_resul
39970 74 5f 74 65 78 74 28 43 2c 56 2c 4e 2c 44 29 5d  t_text(C,V,N,D)]
39980 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  , [sqlite3_resul
39990 74 5f 74 65 78 74 31 36 28 43 2c 56 2c 4e 2c 44  t_text16(C,V,N,D
399a0 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  )],.**          
399b0 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  [sqlite3_result_
399c0 74 65 78 74 31 36 62 65 28 43 2c 56 2c 4e 2c 44  text16be(C,V,N,D
399d0 29 5d 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20  )], or.**       
399e0 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75     [sqlite3_resu
399f0 6c 74 5f 74 65 78 74 31 36 6c 65 28 43 2c 56 2c  lt_text16le(C,V,
39a00 4e 2c 44 29 5d 20 69 73 20 74 68 65 20 63 6f 6e  N,D)] is the con
39a10 73 74 61 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20  stant.**        
39a20 20 20 5b 53 51 4c 49 54 45 5f 54 52 41 4e 53 49    [SQLITE_TRANSI
39a30 45 4e 54 5d 20 74 68 65 6e 20 74 68 65 20 69 6e  ENT] then the in
39a40 74 65 72 66 61 63 65 73 20 6d 61 6b 65 73 20 61  terfaces makes a
39a50 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20   copy of the.** 
39a60 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65 6e 74           content
39a70 20 6f 66 20 56 20 61 6e 64 20 72 65 74 61 69 6e   of V and retain
39a80 73 20 74 68 65 20 63 6f 70 79 2e 0a 2a 2a 0a 2a  s the copy..**.*
39a90 2a 20 7b 48 31 36 34 36 33 7d 20 49 66 20 74 68  * {H16463} If th
39aa0 65 20 44 20 64 65 73 74 72 75 63 74 6f 72 20 70  e D destructor p
39ab0 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c  arameter to [sql
39ac0 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
39ad0 28 43 2c 56 2c 4e 2c 44 29 5d 2c 0a 2a 2a 20 20  (C,V,N,D)],.**  
39ae0 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33          [sqlite3
39af0 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 43 2c 56  _result_text(C,V
39b00 2c 4e 2c 44 29 5d 2c 20 5b 73 71 6c 69 74 65 33  ,N,D)], [sqlite3
39b10 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 43  _result_text16(C
39b20 2c 56 2c 4e 2c 44 29 5d 2c 0a 2a 2a 20 20 20 20  ,V,N,D)],.**    
39b30 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 72        [sqlite3_r
39b40 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 43  esult_text16be(C
39b50 2c 56 2c 4e 2c 44 29 5d 2c 20 6f 72 0a 2a 2a 20  ,V,N,D)], or.** 
39b60 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
39b70 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c  3_result_text16l
39b80 65 28 43 2c 56 2c 4e 2c 44 29 5d 20 69 73 20 73  e(C,V,N,D)] is s
39b90 6f 6d 65 20 76 61 6c 75 65 20 6f 74 68 65 72 20  ome value other 
39ba0 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  than.**         
39bb0 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 5b   the constants [
39bc0 53 51 4c 49 54 45 5f 53 54 41 54 49 43 5d 20 61  SQLITE_STATIC] a
39bd0 6e 64 20 5b 53 51 4c 49 54 45 5f 54 52 41 4e 53  nd [SQLITE_TRANS
39be0 49 45 4e 54 5d 20 74 68 65 6e 0a 2a 2a 20 20 20  IENT] then.**   
39bf0 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 77 69         SQLite wi
39c00 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 64 65  ll invoke the de
39c10 73 74 72 75 63 74 6f 72 20 44 20 77 69 74 68 20  structor D with 
39c20 56 20 61 73 20 69 74 73 20 6f 6e 6c 79 20 61 72  V as its only ar
39c30 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20  gument.**       
39c40 20 20 20 77 68 65 6e 20 69 74 20 68 61 73 20 66     when it has f
39c50 69 6e 69 73 68 65 64 20 77 69 74 68 20 74 68 65  inished with the
39c60 20 56 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c   V value..*/.SQL
39c70 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
39c80 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
39c90 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
39ca0 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20  *, const void*, 
39cb0 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69  int, void(*)(voi
39cc0 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  d*));.SQLITE_API
39cd0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
39ce0 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71 6c 69  sult_double(sqli
39cf0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 64 6f  te3_context*, do
39d00 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50  uble);.SQLITE_AP
39d10 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
39d20 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 6c 69  esult_error(sqli
39d30 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f  te3_context*, co
39d40 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b  nst char*, int);
39d50 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
39d60 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
39d70 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 33 5f  error16(sqlite3_
39d80 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20  context*, const 
39d90 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  void*, int);.SQL
39da0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
39db0 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
39dc0 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 65 33  r_toobig(sqlite3
39dd0 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49  _context*);.SQLI
39de0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
39df0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
39e00 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33 5f 63  _nomem(sqlite3_c
39e10 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45  ontext*);.SQLITE
39e20 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
39e30 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63  3_result_error_c
39e40 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ode(sqlite3_cont
39e50 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ext*, int);.SQLI
39e60 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
39e70 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 73  te3_result_int(s
39e80 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
39e90 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50   int);.SQLITE_AP
39ea0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
39eb0 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 71 6c 69  esult_int64(sqli
39ec0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 73 71  te3_context*, sq
39ed0 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51  lite3_int64);.SQ
39ee0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
39ef0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
39f00 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  l(sqlite3_contex
39f10 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  t*);.SQLITE_API 
39f20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
39f30 75 6c 74 5f 74 65 78 74 28 73 71 6c 69 74 65 33  ult_text(sqlite3
39f40 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74  _context*, const
39f50 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 76 6f 69   char*, int, voi
39f60 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51  d(*)(void*));.SQ
39f70 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
39f80 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
39f90 74 31 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  t16(sqlite3_cont
39fa0 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64  ext*, const void
39fb0 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28  *, int, void(*)(
39fc0 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f  void*));.SQLITE_
39fd0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
39fe0 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65  _result_text16le
39ff0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
3a000 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20  *, const void*, 
3a010 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  int,void(*)(void
3a020 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  *));.SQLITE_API 
3a030 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
3a040 75 6c 74 5f 74 65 78 74 31 36 62 65 28 73 71 6c  ult_text16be(sql
3a050 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63  ite3_context*, c
3a060 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c  onst void*, int,
3a070 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
3a080 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
3a090 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
3a0a0 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 63 6f  value(sqlite3_co
3a0b0 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 65 33 5f  ntext*, sqlite3_
3a0c0 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  value*);.SQLITE_
3a0d0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
3a0e0 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62  _result_zeroblob
3a0f0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
3a100 2a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a  *, int n);../*.*
3a110 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 66 69  * CAPI3REF: Defi
3a120 6e 65 20 4e 65 77 20 43 6f 6c 6c 61 74 69 6e 67  ne New Collating
3a130 20 53 65 71 75 65 6e 63 65 73 20 7b 48 31 36 36   Sequences {H166
3a140 30 30 7d 20 3c 53 32 30 33 30 30 3e 0a 2a 2a 0a  00} <S20300>.**.
3a150 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f  ** These functio
3a160 6e 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 61  ns are used to a
3a170 64 64 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e  dd new collation
3a180 20 73 65 71 75 65 6e 63 65 73 20 74 6f 20 74 68   sequences to th
3a190 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63  e.** [database c
3a1a0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69  onnection] speci
3a1b0 66 69 65 64 20 61 73 20 74 68 65 20 66 69 72 73  fied as the firs
3a1c0 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a  t argument..**.*
3a1d0 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  * The name of th
3a1e0 65 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e 20  e new collation 
3a1f0 73 65 71 75 65 6e 63 65 20 69 73 20 73 70 65 63  sequence is spec
3a200 69 66 69 65 64 20 61 73 20 61 20 55 54 46 2d 38  ified as a UTF-8
3a210 20 73 74 72 69 6e 67 0a 2a 2a 20 66 6f 72 20 73   string.** for s
3a220 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
3a230 6c 6c 61 74 69 6f 6e 28 29 20 61 6e 64 20 73 71  llation() and sq
3a240 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
3a250 6c 61 74 69 6f 6e 5f 76 32 28 29 0a 2a 2a 20 61  lation_v2().** a
3a260 6e 64 20 61 20 55 54 46 2d 31 36 20 73 74 72 69  nd a UTF-16 stri
3a270 6e 67 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63  ng for sqlite3_c
3a280 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31  reate_collation1
3a290 36 28 29 2e 20 49 6e 20 61 6c 6c 20 63 61 73 65  6(). In all case
3a2a0 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 69 73  s.** the name is
3a2b0 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73   passed as the s
3a2c0 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61  econd function a
3a2d0 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  rgument..**.** T
3a2e0 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e  he third argumen
3a2f0 74 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f 66 20  t may be one of 
3a300 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 5b 53  the constants [S
3a310 51 4c 49 54 45 5f 55 54 46 38 5d 2c 0a 2a 2a 20  QLITE_UTF8],.** 
3a320 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d  [SQLITE_UTF16LE]
3a330 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 54 46 31   or [SQLITE_UTF1
3a340 36 42 45 5d 2c 20 69 6e 64 69 63 61 74 69 6e 67  6BE], indicating
3a350 20 74 68 61 74 20 74 68 65 20 75 73 65 72 2d 73   that the user-s
3a360 75 70 70 6c 69 65 64 0a 2a 2a 20 72 6f 75 74 69  upplied.** routi
3a370 6e 65 20 65 78 70 65 63 74 73 20 74 6f 20 62 65  ne expects to be
3a380 20 70 61 73 73 65 64 20 70 6f 69 6e 74 65 72 73   passed pointers
3a390 20 74 6f 20 73 74 72 69 6e 67 73 20 65 6e 63 6f   to strings enco
3a3a0 64 65 64 20 75 73 69 6e 67 20 55 54 46 2d 38 2c  ded using UTF-8,
3a3b0 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69 74 74 6c  .** UTF-16 littl
3a3c0 65 2d 65 6e 64 69 61 6e 2c 20 6f 72 20 55 54 46  e-endian, or UTF
3a3d0 2d 31 36 20 62 69 67 2d 65 6e 64 69 61 6e 2c 20  -16 big-endian, 
3a3e0 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 54 68  respectively. Th
3a3f0 65 0a 2a 2a 20 74 68 69 72 64 20 61 72 67 75 6d  e.** third argum
3a400 65 6e 74 20 6d 69 67 68 74 20 61 6c 73 6f 20 62  ent might also b
3a410 65 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 5f  e [SQLITE_UTF16_
3a420 41 4c 49 47 4e 45 44 5d 20 74 6f 20 69 6e 64 69  ALIGNED] to indi
3a430 63 61 74 65 20 74 68 61 74 0a 2a 2a 20 74 68 65  cate that.** the
3a440 20 72 6f 75 74 69 6e 65 20 65 78 70 65 63 74 73   routine expects
3a450 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 31 36 2d   pointers to 16-
3a460 62 69 74 20 77 6f 72 64 20 61 6c 69 67 6e 65 64  bit word aligned
3a470 20 73 74 72 69 6e 67 73 0a 2a 2a 20 6f 66 20 55   strings.** of U
3a480 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74  TF-16 in the nat
3a490 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 6f  ive byte order o
3a4a0 66 20 74 68 65 20 68 6f 73 74 20 63 6f 6d 70 75  f the host compu
3a4b0 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69  ter..**.** A poi
3a4c0 6e 74 65 72 20 74 6f 20 74 68 65 20 75 73 65 72  nter to the user
3a4d0 20 73 75 70 70 6c 69 65 64 20 72 6f 75 74 69 6e   supplied routin
3a4e0 65 20 6d 75 73 74 20 62 65 20 70 61 73 73 65 64  e must be passed
3a4f0 20 61 73 20 74 68 65 20 66 69 66 74 68 0a 2a 2a   as the fifth.**
3a500 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 69   argument.  If i
3a510 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 69 73 20  t is NULL, this 
3a520 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 64  is the same as d
3a530 65 6c 65 74 69 6e 67 20 74 68 65 20 63 6f 6c 6c  eleting the coll
3a540 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63  ation.** sequenc
3a550 65 20 28 73 6f 20 74 68 61 74 20 53 51 4c 69 74  e (so that SQLit
3a560 65 20 63 61 6e 6e 6f 74 20 63 61 6c 6c 20 69 74  e cannot call it
3a570 20 61 6e 79 6d 6f 72 65 29 2e 0a 2a 2a 20 45 61   anymore)..** Ea
3a580 63 68 20 74 69 6d 65 20 74 68 65 20 61 70 70 6c  ch time the appl
3a590 69 63 61 74 69 6f 6e 20 73 75 70 70 6c 69 65 64  ication supplied
3a5a0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76   function is inv
3a5b0 6f 6b 65 64 2c 20 69 74 20 69 73 20 70 61 73 73  oked, it is pass
3a5c0 65 64 0a 2a 2a 20 61 73 20 69 74 73 20 66 69 72  ed.** as its fir
3a5d0 73 74 20 70 61 72 61 6d 65 74 65 72 20 61 20 63  st parameter a c
3a5e0 6f 70 79 20 6f 66 20 74 68 65 20 76 6f 69 64 2a  opy of the void*
3a5f0 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66   passed as the f
3a600 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 0a 2a  ourth argument.*
3a610 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 72 65  * to sqlite3_cre
3a620 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 20  ate_collation() 
3a630 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  or sqlite3_creat
3a640 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 29 2e  e_collation16().
3a650 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69  .**.** The remai
3a660 6e 69 6e 67 20 61 72 67 75 6d 65 6e 74 73 20 74  ning arguments t
3a670 6f 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  o the applicatio
3a680 6e 2d 73 75 70 70 6c 69 65 64 20 72 6f 75 74 69  n-supplied routi
3a690 6e 65 20 61 72 65 20 74 77 6f 20 73 74 72 69 6e  ne are two strin
3a6a0 67 73 2c 0a 2a 2a 20 65 61 63 68 20 72 65 70 72  gs,.** each repr
3a6b0 65 73 65 6e 74 65 64 20 62 79 20 61 20 28 6c 65  esented by a (le
3a6c0 6e 67 74 68 2c 20 64 61 74 61 29 20 70 61 69 72  ngth, data) pair
3a6d0 20 61 6e 64 20 65 6e 63 6f 64 65 64 20 69 6e 20   and encoded in 
3a6e0 74 68 65 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20  the encoding.** 
3a6f0 74 68 61 74 20 77 61 73 20 70 61 73 73 65 64 20  that was passed 
3a700 61 73 20 74 68 65 20 74 68 69 72 64 20 61 72 67  as the third arg
3a710 75 6d 65 6e 74 20 77 68 65 6e 20 74 68 65 20 63  ument when the c
3a720 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
3a730 65 20 77 61 73 0a 2a 2a 20 72 65 67 69 73 74 65  e was.** registe
3a740 72 65 64 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20  red. {END}  The 
3a750 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 65 66 69  application defi
3a760 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 72 6f  ned collation ro
3a770 75 74 69 6e 65 20 73 68 6f 75 6c 64 0a 2a 2a 20  utine should.** 
3a780 72 65 74 75 72 6e 20 6e 65 67 61 74 69 76 65 2c  return negative,
3a790 20 7a 65 72 6f 20 6f 72 20 70 6f 73 69 74 69 76   zero or positiv
3a7a0 65 20 69 66 20 74 68 65 20 66 69 72 73 74 20 73  e if the first s
3a7b0 74 72 69 6e 67 20 69 73 20 6c 65 73 73 20 74 68  tring is less th
3a7c0 61 6e 2c 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 2c  an,.** equal to,
3a7d0 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e   or greater than
3a7e0 20 74 68 65 20 73 65 63 6f 6e 64 20 73 74 72 69   the second stri
3a7f0 6e 67 2e 20 69 2e 65 2e 20 28 53 54 52 49 4e 47  ng. i.e. (STRING
3a800 31 20 2d 20 53 54 52 49 4e 47 32 29 2e 0a 2a 2a  1 - STRING2)..**
3a810 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
3a820 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
3a830 5f 76 32 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65  _v2() works like
3a840 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
3a850 63 6f 6c 6c 61 74 69 6f 6e 28 29 0a 2a 2a 20 65  collation().** e
3a860 78 63 65 70 74 20 74 68 61 74 20 69 74 20 74 61  xcept that it ta
3a870 6b 65 73 20 61 6e 20 65 78 74 72 61 20 61 72 67  kes an extra arg
3a880 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 61  ument which is a
3a890 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72 0a   destructor for.
3a8a0 2a 2a 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  ** the collation
3a8b0 2e 20 20 54 68 65 20 64 65 73 74 72 75 63 74 6f  .  The destructo
3a8c0 72 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  r is called when
3a8d0 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 69   the collation i
3a8e0 73 0a 2a 2a 20 64 65 73 74 72 6f 79 65 64 20 61  s.** destroyed a
3a8f0 6e 64 20 69 73 20 70 61 73 73 65 64 20 61 20 63  nd is passed a c
3a900 6f 70 79 20 6f 66 20 74 68 65 20 66 6f 75 72 74  opy of the fourt
3a910 68 20 70 61 72 61 6d 65 74 65 72 20 76 6f 69 64  h parameter void
3a920 2a 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 6f 66 20  * pointer.** of 
3a930 74 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61  the sqlite3_crea
3a940 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28  te_collation_v2(
3a950 29 2e 0a 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e 73  )..** Collations
3a960 20 61 72 65 20 64 65 73 74 72 6f 79 65 64 20 77   are destroyed w
3a970 68 65 6e 20 74 68 65 79 20 61 72 65 20 6f 76 65  hen they are ove
3a980 72 72 69 64 64 65 6e 20 62 79 20 6c 61 74 65 72  rridden by later
3a990 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 0a 2a 2a   calls to the.**
3a9a0 20 63 6f 6c 6c 61 74 69 6f 6e 20 63 72 65 61 74   collation creat
3a9b0 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72  ion functions or
3a9c0 20 77 68 65 6e 20 74 68 65 20 5b 64 61 74 61 62   when the [datab
3a9d0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
3a9e0 69 73 20 63 6c 6f 73 65 64 0a 2a 2a 20 75 73 69  is closed.** usi
3a9f0 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73  ng [sqlite3_clos
3aa00 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41  e()]..**.** INVA
3aa10 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48  RIANTS:.**.** {H
3aa20 31 36 36 30 33 7d 20 41 20 73 75 63 63 65 73 73  16603} A success
3aa30 66 75 6c 20 63 61 6c 6c 20 74 6f 20 74 68 65 0a  ful call to the.
3aa40 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
3aa50 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
3aa60 61 74 69 6f 6e 5f 76 32 28 42 2c 58 2c 45 2c 50  ation_v2(B,X,E,P
3aa70 2c 46 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 65  ,F,D)] interface
3aa80 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 67  .**          reg
3aa90 69 73 74 65 72 73 20 66 75 6e 63 74 69 6f 6e 20  isters function 
3aaa0 46 20 61 73 20 74 68 65 20 63 6f 6d 70 61 72 69  F as the compari
3aab0 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 75 73 65  son function use
3aac0 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20  d to.**         
3aad0 20 69 6d 70 6c 65 6d 65 6e 74 20 63 6f 6c 6c 61   implement colla
3aae0 74 69 6f 6e 20 58 20 6f 6e 20 74 68 65 20 5b 64  tion X on the [d
3aaf0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
3ab00 6f 6e 5d 20 42 20 66 6f 72 0a 2a 2a 20 20 20 20  on] B for.**    
3ab10 20 20 20 20 20 20 64 61 74 61 62 61 73 65 73 20        databases 
3ab20 68 61 76 69 6e 67 20 65 6e 63 6f 64 69 6e 67 20  having encoding 
3ab30 45 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36 30 34  E..**.** {H16604
3ab40 7d 20 53 51 4c 69 74 65 20 75 6e 64 65 72 73 74  } SQLite underst
3ab50 61 6e 64 73 20 74 68 65 20 58 20 70 61 72 61 6d  ands the X param
3ab60 65 74 65 72 20 74 6f 0a 2a 2a 20 20 20 20 20 20  eter to.**      
3ab70 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 72 65      [sqlite3_cre
3ab80 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32  ate_collation_v2
3ab90 28 42 2c 58 2c 45 2c 50 2c 46 2c 44 29 5d 20 61  (B,X,E,P,F,D)] a
3aba0 73 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61  s a zero-termina
3abb0 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ted.**          
3abc0 55 54 46 2d 38 20 73 74 72 69 6e 67 20 69 6e 20  UTF-8 string in 
3abd0 77 68 69 63 68 20 63 61 73 65 20 69 73 20 69 67  which case is ig
3abe0 6e 6f 72 65 64 20 66 6f 72 20 41 53 43 49 49 20  nored for ASCII 
3abf0 63 68 61 72 61 63 74 65 72 73 20 61 6e 64 0a 2a  characters and.*
3ac00 2a 20 20 20 20 20 20 20 20 20 20 69 73 20 73 69  *          is si
3ac10 67 6e 69 66 69 63 61 6e 74 20 66 6f 72 20 6e 6f  gnificant for no
3ac20 6e 2d 41 53 43 49 49 20 63 68 61 72 61 63 74 65  n-ASCII characte
3ac30 72 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36 30  rs..**.** {H1660
3ac40 36 7d 20 53 75 63 63 65 73 73 69 76 65 20 63 61  6} Successive ca
3ac50 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  lls to [sqlite3_
3ac60 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
3ac70 5f 76 32 28 42 2c 58 2c 45 2c 50 2c 46 2c 44 29  _v2(B,X,E,P,F,D)
3ac80 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 69  ].**          wi
3ac90 74 68 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75  th the same valu
3aca0 65 73 20 66 6f 72 20 42 2c 20 58 2c 20 61 6e 64  es for B, X, and
3acb0 20 45 2c 20 6f 76 65 72 72 69 64 65 20 70 72 69   E, override pri
3acc0 6f 72 20 76 61 6c 75 65 73 0a 2a 2a 20 20 20 20  or values.**    
3acd0 20 20 20 20 20 20 6f 66 20 50 2c 20 46 2c 20 61        of P, F, a
3ace0 6e 64 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36  nd D..**.** {H16
3acf0 36 30 39 7d 20 49 66 20 74 68 65 20 64 65 73 74  609} If the dest
3ad00 72 75 63 74 6f 72 20 44 20 69 6e 20 5b 73 71 6c  ructor D in [sql
3ad10 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
3ad20 61 74 69 6f 6e 5f 76 32 28 42 2c 58 2c 45 2c 50  ation_v2(B,X,E,P
3ad30 2c 46 2c 44 29 5d 0a 2a 2a 20 20 20 20 20 20 20  ,F,D)].**       
3ad40 20 20 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74     is not NULL t
3ad50 68 65 6e 20 69 74 20 69 73 20 63 61 6c 6c 65 64  hen it is called
3ad60 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74 20 50   with argument P
3ad70 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 20 20 20   when the.**    
3ad80 20 20 20 20 20 20 63 6f 6c 6c 61 74 69 6e 67 20        collating 
3ad90 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 72 6f 70  function is drop
3ada0 70 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a  ped by SQLite..*
3adb0 2a 0a 2a 2a 20 7b 48 31 36 36 31 32 7d 20 41 20  *.** {H16612} A 
3adc0 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69  collating functi
3add0 6f 6e 20 69 73 20 64 72 6f 70 70 65 64 20 77 68  on is dropped wh
3ade0 65 6e 20 69 74 20 69 73 20 6f 76 65 72 6c 6f 61  en it is overloa
3adf0 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36  ded..**.** {H166
3ae00 31 35 7d 20 41 20 63 6f 6c 6c 61 74 69 6e 67 20  15} A collating 
3ae10 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 72 6f 70  function is drop
3ae20 70 65 64 20 77 68 65 6e 20 74 68 65 20 64 61 74  ped when the dat
3ae30 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
3ae40 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73 20  .**          is 
3ae50 63 6c 6f 73 65 64 20 75 73 69 6e 67 20 5b 73 71  closed using [sq
3ae60 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 2e 0a  lite3_close()]..
3ae70 2a 2a 0a 2a 2a 20 7b 48 31 36 36 31 38 7d 20 54  **.** {H16618} T
3ae80 68 65 20 70 6f 69 6e 74 65 72 20 50 20 69 6e 20  he pointer P in 
3ae90 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
3aea0 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 42 2c 58  collation_v2(B,X
3aeb0 2c 45 2c 50 2c 46 2c 44 29 5d 0a 2a 2a 20 20 20  ,E,P,F,D)].**   
3aec0 20 20 20 20 20 20 20 69 73 20 70 61 73 73 65 64         is passed
3aed0 20 74 68 72 6f 75 67 68 20 61 73 20 74 68 65 20   through as the 
3aee0 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20  first parameter 
3aef0 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  to the compariso
3af00 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 75  n.**          fu
3af10 6e 63 74 69 6f 6e 20 46 20 66 6f 72 20 61 6c 6c  nction F for all
3af20 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f   subsequent invo
3af30 63 61 74 69 6f 6e 73 20 6f 66 20 46 2e 0a 2a 2a  cations of F..**
3af40 0a 2a 2a 20 7b 48 31 36 36 32 31 7d 20 41 20 63  .** {H16621} A c
3af50 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
3af60 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
3af70 28 42 2c 58 2c 45 2c 50 2c 46 29 5d 20 69 73 20  (B,X,E,P,F)] is 
3af80 65 78 61 63 74 6c 79 0a 2a 2a 20 20 20 20 20 20  exactly.**      
3af90 20 20 20 20 74 68 65 20 73 61 6d 65 20 61 73 20      the same as 
3afa0 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  a call to [sqlit
3afb0 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
3afc0 69 6f 6e 5f 76 32 28 29 5d 20 77 69 74 68 0a 2a  ion_v2()] with.*
3afd0 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 73  *          the s
3afe0 61 6d 65 20 70 61 72 61 6d 65 74 65 72 73 20 61  ame parameters a
3aff0 6e 64 20 61 20 4e 55 4c 4c 20 64 65 73 74 72 75  nd a NULL destru
3b000 63 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36  ctor..**.** {H16
3b010 36 32 34 7d 20 46 6f 6c 6c 6f 77 69 6e 67 20 61  624} Following a
3b020 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
3b030 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 42 2c  _collation_v2(B,
3b040 58 2c 45 2c 50 2c 46 2c 44 29 5d 2c 0a 2a 2a 20  X,E,P,F,D)],.** 
3b050 20 20 20 20 20 20 20 20 20 53 51 4c 69 74 65 20           SQLite 
3b060 75 73 65 73 20 74 68 65 20 63 6f 6d 70 61 72 69  uses the compari
3b070 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 46 20 66  son function F f
3b080 6f 72 20 61 6c 6c 20 74 65 78 74 20 63 6f 6d 70  or all text comp
3b090 61 72 69 73 6f 6e 0a 2a 2a 20 20 20 20 20 20 20  arison.**       
3b0a0 20 20 20 6f 70 65 72 61 74 69 6f 6e 73 20 6f 6e     operations on
3b0b0 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
3b0c0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 42 20 6f 6e 20  onnection] B on 
3b0d0 74 65 78 74 20 76 61 6c 75 65 73 20 74 68 61 74  text values that
3b0e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 75 73 65  .**          use
3b0f0 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73   the collating s
3b100 65 71 75 65 6e 63 65 20 6e 61 6d 65 64 20 58 2e  equence named X.
3b110 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36 32 37 7d 20  .**.** {H16627} 
3b120 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65  The [sqlite3_cre
3b130 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28  ate_collation16(
3b140 42 2c 58 2c 45 2c 50 2c 46 29 5d 20 77 6f 72 6b  B,X,E,P,F)] work
3b150 73 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 20 20  s the same.**   
3b160 20 20 20 20 20 20 20 61 73 20 5b 73 71 6c 69 74         as [sqlit
3b170 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
3b180 69 6f 6e 28 42 2c 58 2c 45 2c 50 2c 46 29 5d 20  ion(B,X,E,P,F)] 
3b190 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 0a  except that the.
3b1a0 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6c 6c  **          coll
3b1b0 61 74 69 6f 6e 20 6e 61 6d 65 20 58 20 69 73 20  ation name X is 
3b1c0 75 6e 64 65 72 73 74 6f 6f 64 20 61 73 20 55 54  understood as UT
3b1d0 46 2d 31 36 20 69 6e 20 6e 61 74 69 76 65 20 62  F-16 in native b
3b1e0 79 74 65 20 6f 72 64 65 72 0a 2a 2a 20 20 20 20  yte order.**    
3b1f0 20 20 20 20 20 20 69 6e 73 74 65 61 64 20 6f 66        instead of
3b200 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 7b 48   UTF-8..**.** {H
3b210 31 36 36 33 30 7d 20 57 68 65 6e 20 6d 75 6c 74  16630} When mult
3b220 69 70 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20  iple comparison 
3b230 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 61 76  functions are av
3b240 61 69 6c 61 62 6c 65 20 66 6f 72 20 74 68 65 20  ailable for the 
3b250 73 61 6d 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  same.**         
3b260 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
3b270 6e 63 65 2c 20 53 51 4c 69 74 65 20 63 68 6f 6f  nce, SQLite choo
3b280 73 65 73 20 74 68 65 20 6f 6e 65 20 77 68 6f 73  ses the one whos
3b290 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 0a  e text encoding.
3b2a0 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 71 75  **          requ
3b2b0 69 72 65 73 20 74 68 65 20 6c 65 61 73 74 20 61  ires the least a
3b2c0 6d 6f 75 6e 74 20 6f 66 20 63 6f 6e 76 65 72 73  mount of convers
3b2d0 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 64 65 66  ion from the def
3b2e0 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  ault.**         
3b2f0 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 6f   text encoding o
3b300 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a  f the database..
3b310 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
3b320 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  t sqlite3_create
3b330 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 71  _collation(.  sq
3b340 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 74  lite3*, .  const
3b350 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20   char *zName, . 
3b360 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a   int eTextRep, .
3b370 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a    void*,.  int(*
3b380 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c  xCompare)(void*,
3b390 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
3b3a0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29  int,const void*)
3b3b0 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .);.SQLITE_API i
3b3c0 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  nt sqlite3_creat
3b3d0 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 0a  e_collation_v2(.
3b3e0 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63    sqlite3*, .  c
3b3f0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
3b400 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65  , .  int eTextRe
3b410 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69  p, .  void*,.  i
3b420 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f  nt(*xCompare)(vo
3b430 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
3b440 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
3b450 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78 44  id*),.  void(*xD
3b460 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 0a 29  estroy)(void*).)
3b470 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
3b480 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
3b490 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 20 73  collation16(.  s
3b4a0 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73  qlite3*, .  cons
3b4b0 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c 0a 20  t void *zName,. 
3b4c0 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a   int eTextRep, .
3b4d0 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a    void*,.  int(*
3b4e0 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c  xCompare)(void*,
3b4f0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
3b500 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29  int,const void*)
3b510 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .);../*.** CAPI3
3b520 52 45 46 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 4e  REF: Collation N
3b530 65 65 64 65 64 20 43 61 6c 6c 62 61 63 6b 73 20  eeded Callbacks 
3b540 7b 48 31 36 37 30 30 7d 20 3c 53 32 30 33 30 30  {H16700} <S20300
3b550 3e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64  >.**.** To avoid
3b560 20 68 61 76 69 6e 67 20 74 6f 20 72 65 67 69 73   having to regis
3b570 74 65 72 20 61 6c 6c 20 63 6f 6c 6c 61 74 69 6f  ter all collatio
3b580 6e 20 73 65 71 75 65 6e 63 65 73 20 62 65 66 6f  n sequences befo
3b590 72 65 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a  re a database.**
3b5a0 20 63 61 6e 20 62 65 20 75 73 65 64 2c 20 61 20   can be used, a 
3b5b0 73 69 6e 67 6c 65 20 63 61 6c 6c 62 61 63 6b 20  single callback 
3b5c0 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20  function may be 
3b5d0 72 65 67 69 73 74 65 72 65 64 20 77 69 74 68 20  registered with 
3b5e0 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65  the.** [database
3b5f0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 6f 20   connection] to 
3b600 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 65 76  be called whenev
3b610 65 72 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 20  er an undefined 
3b620 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71  collation.** seq
3b630 75 65 6e 63 65 20 69 73 20 72 65 71 75 69 72 65  uence is require
3b640 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
3b650 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 67 69  function is regi
3b660 73 74 65 72 65 64 20 75 73 69 6e 67 20 74 68 65  stered using the
3b670 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69   sqlite3_collati
3b680 6f 6e 5f 6e 65 65 64 65 64 28 29 20 41 50 49 2c  on_needed() API,
3b690 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20 70  .** then it is p
3b6a0 61 73 73 65 64 20 74 68 65 20 6e 61 6d 65 73 20  assed the names 
3b6b0 6f 66 20 75 6e 64 65 66 69 6e 65 64 20 63 6f 6c  of undefined col
3b6c0 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73  lation sequences
3b6d0 20 61 73 20 73 74 72 69 6e 67 73 0a 2a 2a 20 65   as strings.** e
3b6e0 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 2e  ncoded in UTF-8.
3b6f0 20 7b 48 31 36 37 30 33 7d 20 49 66 20 73 71 6c   {H16703} If sql
3b700 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
3b710 65 65 64 65 64 31 36 28 29 20 69 73 20 75 73 65  eeded16() is use
3b720 64 2c 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 73 20  d,.** the names 
3b730 61 72 65 20 70 61 73 73 65 64 20 61 73 20 55 54  are passed as UT
3b740 46 2d 31 36 20 69 6e 20 6d 61 63 68 69 6e 65 20  F-16 in machine 
3b750 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65  native byte orde
3b760 72 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20  r..** A call to 
3b770 65 69 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20  either function 
3b780 72 65 70 6c 61 63 65 73 20 61 6e 79 20 65 78 69  replaces any exi
3b790 73 74 69 6e 67 20 63 61 6c 6c 62 61 63 6b 2e 0a  sting callback..
3b7a0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63  **.** When the c
3b7b0 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b  allback is invok
3b7c0 65 64 2c 20 74 68 65 20 66 69 72 73 74 20 61 72  ed, the first ar
3b7d0 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 69 73  gument passed is
3b7e0 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66 20 74 68   a copy.** of th
3b7f0 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
3b800 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c  t to sqlite3_col
3b810 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20  lation_needed() 
3b820 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f  or.** sqlite3_co
3b830 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36  llation_needed16
3b840 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20  ().  The second 
3b850 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
3b860 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e  database.** conn
3b870 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 74 68 69  ection.  The thi
3b880 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f  rd argument is o
3b890 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 55 54  ne of [SQLITE_UT
3b8a0 46 38 5d 2c 20 5b 53 51 4c 49 54 45 5f 55 54 46  F8], [SQLITE_UTF
3b8b0 31 36 42 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51  16BE],.** or [SQ
3b8c0 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d 2c 20 69  LITE_UTF16LE], i
3b8d0 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 6d 6f  ndicating the mo
3b8e0 73 74 20 64 65 73 69 72 61 62 6c 65 20 66 6f 72  st desirable for
3b8f0 6d 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69  m of the collati
3b900 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 66  on.** sequence f
3b910 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65 64  unction required
3b920 2e 20 20 54 68 65 20 66 6f 75 72 74 68 20 70 61  .  The fourth pa
3b930 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 6e  rameter is the n
3b940 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65  ame of the.** re
3b950 71 75 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e  quired collation
3b960 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a   sequence..**.**
3b970 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75   The callback fu
3b980 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 72 65  nction should re
3b990 67 69 73 74 65 72 20 74 68 65 20 64 65 73 69 72  gister the desir
3b9a0 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 75 73 69  ed collation usi
3b9b0 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63  ng.** [sqlite3_c
3b9c0 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28  reate_collation(
3b9d0 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72 65  )], [sqlite3_cre
3b9e0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28  ate_collation16(
3b9f0 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74  )], or.** [sqlit
3ba00 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
3ba10 69 6f 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a  ion_v2()]..**.**
3ba20 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a   INVARIANTS:.**.
3ba30 2a 2a 20 7b 48 31 36 37 30 32 7d 20 41 20 73 75  ** {H16702} A su
3ba40 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f  ccessful call to
3ba50 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74   [sqlite3_collat
3ba60 69 6f 6e 5f 6e 65 65 64 65 64 28 44 2c 50 2c 46  ion_needed(D,P,F
3ba70 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  )].**          o
3ba80 72 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61  r [sqlite3_colla
3ba90 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 44 2c  tion_needed16(D,
3baa0 50 2c 46 29 5d 20 63 61 75 73 65 73 0a 2a 2a 20  P,F)] causes.** 
3bab0 20 20 20 20 20 20 20 20 20 74 68 65 20 5b 64 61           the [da
3bac0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3bad0 6e 5d 20 44 20 74 6f 20 69 6e 76 6f 6b 65 20 63  n] D to invoke c
3bae0 61 6c 6c 62 61 63 6b 20 46 20 77 69 74 68 20 66  allback F with f
3baf0 69 72 73 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  irst.**         
3bb00 20 70 61 72 61 6d 65 74 65 72 20 50 20 77 68 65   parameter P whe
3bb10 6e 65 76 65 72 20 69 74 20 6e 65 65 64 73 20 61  never it needs a
3bb20 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63   comparison func
3bb30 74 69 6f 6e 20 66 6f 72 20 61 0a 2a 2a 20 20 20  tion for a.**   
3bb40 20 20 20 20 20 20 20 63 6f 6c 6c 61 74 69 6e 67         collating
3bb50 20 73 65 71 75 65 6e 63 65 20 74 68 61 74 20 69   sequence that i
3bb60 74 20 64 6f 65 73 20 6e 6f 74 20 6b 6e 6f 77 20  t does not know 
3bb70 61 62 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  about..**.** {H1
3bb80 36 37 30 34 7d 20 45 61 63 68 20 73 75 63 63 65  6704} Each succe
3bb90 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73  ssful call to [s
3bba0 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e  qlite3_collation
3bbb0 5f 6e 65 65 64 65 64 28 29 5d 20 6f 72 0a 2a 2a  _needed()] or.**
3bbc0 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
3bbd0 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  e3_collation_nee
3bbe0 64 65 64 31 36 28 29 5d 20 6f 76 65 72 72 69 64  ded16()] overrid
3bbf0 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  es the callback 
3bc00 72 65 67 69 73 74 65 72 65 64 0a 2a 2a 20 20 20  registered.**   
3bc10 20 20 20 20 20 20 20 6f 6e 20 74 68 65 20 73 61         on the sa
3bc20 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  me [database con
3bc30 6e 65 63 74 69 6f 6e 5d 20 62 79 20 70 72 69 6f  nection] by prio
3bc40 72 20 63 61 6c 6c 73 20 74 6f 20 65 69 74 68 65  r calls to eithe
3bc50 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e  r.**          in
3bc60 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 7b  terface..**.** {
3bc70 48 31 36 37 30 36 7d 20 54 68 65 20 6e 61 6d 65  H16706} The name
3bc80 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65   of the requeste
3bc90 64 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63  d collating func
3bca0 74 69 6f 6e 20 70 61 73 73 65 64 20 69 6e 20 74  tion passed in t
3bcb0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 34  he.**          4
3bcc0 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  th parameter to 
3bcd0 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  the callback is 
3bce0 69 6e 20 55 54 46 2d 38 20 69 66 20 74 68 65 20  in UTF-8 if the 
3bcf0 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20 20 20  callback.**     
3bd00 20 20 20 20 20 77 61 73 20 72 65 67 69 73 74 65       was registe
3bd10 72 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  red using [sqlit
3bd20 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  e3_collation_nee
3bd30 64 65 64 28 29 5d 20 61 6e 64 0a 2a 2a 20 20 20  ded()] and.**   
3bd40 20 20 20 20 20 20 20 69 73 20 69 6e 20 55 54 46         is in UTF
3bd50 2d 31 36 20 6e 61 74 69 76 65 20 62 79 74 65 20  -16 native byte 
3bd60 6f 72 64 65 72 20 69 66 20 74 68 65 20 63 61 6c  order if the cal
3bd70 6c 62 61 63 6b 20 77 61 73 0a 2a 2a 20 20 20 20  lback was.**    
3bd80 20 20 20 20 20 20 72 65 67 69 73 74 65 72 65 64        registered
3bd90 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
3bda0 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
3bdb0 31 36 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45  16()]..*/.SQLITE
3bdc0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
3bdd0 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  _collation_neede
3bde0 64 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a  d(.  sqlite3*, .
3bdf0 20 20 76 6f 69 64 2a 2c 20 0a 20 20 76 6f 69 64    void*, .  void
3be00 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65  (*)(void*,sqlite
3be10 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c  3*,int eTextRep,
3be20 63 6f 6e 73 74 20 63 68 61 72 2a 29 0a 29 3b 0a  const char*).);.
3be30 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3be40 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e  qlite3_collation
3be50 5f 6e 65 65 64 65 64 31 36 28 0a 20 20 73 71 6c  _needed16(.  sql
3be60 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a 2c  ite3*, .  void*,
3be70 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  .  void(*)(void*
3be80 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54  ,sqlite3*,int eT
3be90 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69  extRep,const voi
3bea0 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 70  d*).);../*.** Sp
3beb0 65 63 69 66 79 20 74 68 65 20 6b 65 79 20 66 6f  ecify the key fo
3bec0 72 20 61 6e 20 65 6e 63 72 79 70 74 65 64 20 64  r an encrypted d
3bed0 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 72  atabase.  This r
3bee0 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 65  outine should be
3bef0 0a 2a 2a 20 63 61 6c 6c 65 64 20 72 69 67 68 74  .** called right
3bf00 20 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f 6f   after sqlite3_o
3bf10 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  pen()..**.** The
3bf20 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65   code to impleme
3bf30 6e 74 20 74 68 69 73 20 41 50 49 20 69 73 20 6e  nt this API is n
3bf40 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20  ot available in 
3bf50 74 68 65 20 70 75 62 6c 69 63 20 72 65 6c 65 61  the public relea
3bf60 73 65 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e  se.** of SQLite.
3bf70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
3bf80 6e 74 20 73 71 6c 69 74 65 33 5f 6b 65 79 28 0a  nt sqlite3_key(.
3bf90 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
3bfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3bfb0 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20   /* Database to 
3bfc0 62 65 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20 20  be rekeyed */.  
3bfd0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
3bfe0 2c 20 69 6e 74 20 6e 4b 65 79 20 20 20 20 20 2f  , int nKey     /
3bff0 2a 20 54 68 65 20 6b 65 79 20 2a 2f 0a 29 3b 0a  * The key */.);.
3c000 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
3c010 65 20 6b 65 79 20 6f 6e 20 61 6e 20 6f 70 65 6e  e key on an open
3c020 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 20 74   database.  If t
3c030 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 62  he current datab
3c040 61 73 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 65 6e  ase is not.** en
3c050 63 72 79 70 74 65 64 2c 20 74 68 69 73 20 72 6f  crypted, this ro
3c060 75 74 69 6e 65 20 77 69 6c 6c 20 65 6e 63 72 79  utine will encry
3c070 70 74 20 69 74 2e 20 20 49 66 20 70 4e 65 77 3d  pt it.  If pNew=
3c080 3d 30 20 6f 72 20 6e 4e 65 77 3d 3d 30 2c 20 74  =0 or nNew==0, t
3c090 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 69  he.** database i
3c0a0 73 20 64 65 63 72 79 70 74 65 64 2e 0a 2a 2a 0a  s decrypted..**.
3c0b0 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 6f 20 69  ** The code to i
3c0c0 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73 20 41 50  mplement this AP
3c0d0 49 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62  I is not availab
3c0e0 6c 65 20 69 6e 20 74 68 65 20 70 75 62 6c 69 63  le in the public
3c0f0 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 20 53   release.** of S
3c100 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  QLite..*/.SQLITE
3c110 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
3c120 5f 72 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 65  _rekey(.  sqlite
3c130 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
3c140 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
3c150 62 61 73 65 20 74 6f 20 62 65 20 72 65 6b 65 79  base to be rekey
3c160 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  ed */.  const vo
3c170 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b  id *pKey, int nK
3c180 65 79 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65  ey     /* The ne
3c190 77 20 6b 65 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a  w key */.);../*.
3c1a0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 75 73  ** CAPI3REF: Sus
3c1b0 70 65 6e 64 20 45 78 65 63 75 74 69 6f 6e 20 46  pend Execution F
3c1c0 6f 72 20 41 20 53 68 6f 72 74 20 54 69 6d 65 20  or A Short Time 
3c1d0 7b 48 31 30 35 33 30 7d 20 3c 53 34 30 34 31 30  {H10530} <S40410
3c1e0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  >.**.** The sqli
3c1f0 74 65 33 5f 73 6c 65 65 70 28 29 20 66 75 6e 63  te3_sleep() func
3c200 74 69 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20  tion causes the 
3c210 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 74  current thread t
3c220 6f 20 73 75 73 70 65 6e 64 20 65 78 65 63 75 74  o suspend execut
3c230 69 6f 6e 0a 2a 2a 20 66 6f 72 20 61 74 20 6c 65  ion.** for at le
3c240 61 73 74 20 61 20 6e 75 6d 62 65 72 20 6f 66 20  ast a number of 
3c250 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 73 70 65  milliseconds spe
3c260 63 69 66 69 65 64 20 69 6e 20 69 74 73 20 70 61  cified in its pa
3c270 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49  rameter..**.** I
3c280 66 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20  f the operating 
3c290 73 79 73 74 65 6d 20 64 6f 65 73 20 6e 6f 74 20  system does not 
3c2a0 73 75 70 70 6f 72 74 20 73 6c 65 65 70 20 72 65  support sleep re
3c2b0 71 75 65 73 74 73 20 77 69 74 68 0a 2a 2a 20 6d  quests with.** m
3c2c0 69 6c 6c 69 73 65 63 6f 6e 64 20 74 69 6d 65 20  illisecond time 
3c2d0 72 65 73 6f 6c 75 74 69 6f 6e 2c 20 74 68 65 6e  resolution, then
3c2e0 20 74 68 65 20 74 69 6d 65 20 77 69 6c 6c 20 62   the time will b
3c2f0 65 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 0a  e rounded up to.
3c300 2a 2a 20 74 68 65 20 6e 65 61 72 65 73 74 20 73  ** the nearest s
3c310 65 63 6f 6e 64 2e 20 54 68 65 20 6e 75 6d 62 65  econd. The numbe
3c320 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e 64  r of millisecond
3c330 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74 75 61  s of sleep actua
3c340 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 65 64  lly.** requested
3c350 20 66 72 6f 6d 20 74 68 65 20 6f 70 65 72 61 74   from the operat
3c360 69 6e 67 20 73 79 73 74 65 6d 20 69 73 20 72 65  ing system is re
3c370 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 51  turned..**.** SQ
3c380 4c 69 74 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  Lite implements 
3c390 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 62  this interface b
3c3a0 79 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 78 53  y calling the xS
3c3b0 6c 65 65 70 28 29 0a 2a 2a 20 6d 65 74 68 6f 64  leep().** method
3c3c0 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   of the default 
3c3d0 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62  [sqlite3_vfs] ob
3c3e0 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41  ject..**.** INVA
3c3f0 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48  RIANTS:.**.** {H
3c400 31 30 35 33 33 7d 20 54 68 65 20 5b 73 71 6c 69  10533} The [sqli
3c410 74 65 33 5f 73 6c 65 65 70 28 4d 29 5d 20 69 6e  te3_sleep(M)] in
3c420 74 65 72 66 61 63 65 20 69 6e 76 6f 6b 65 73 20  terface invokes 
3c430 74 68 65 20 78 53 6c 65 65 70 0a 2a 2a 20 20 20  the xSleep.**   
3c440 20 20 20 20 20 20 20 6d 65 74 68 6f 64 20 6f 66         method of
3c450 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71   the default [sq
3c460 6c 69 74 65 33 5f 76 66 73 7c 56 46 53 5d 20 69  lite3_vfs|VFS] i
3c470 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 20 20  n order to.**   
3c480 20 20 20 20 20 20 20 73 75 73 70 65 6e 64 20 65         suspend e
3c490 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 20  xecution of the 
3c4a0 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 66  current thread f
3c4b0 6f 72 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 20  or at least.**  
3c4c0 20 20 20 20 20 20 20 20 4d 20 6d 69 6c 6c 69 73          M millis
3c4d0 65 63 6f 6e 64 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48  econds..**.** {H
3c4e0 31 30 35 33 36 7d 20 54 68 65 20 5b 73 71 6c 69  10536} The [sqli
3c4f0 74 65 33 5f 73 6c 65 65 70 28 4d 29 5d 20 69 6e  te3_sleep(M)] in
3c500 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
3c510 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  the number of.**
3c520 20 20 20 20 20 20 20 20 20 20 6d 69 6c 6c 69 73            millis
3c530 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 20  econds of sleep 
3c540 61 63 74 75 61 6c 6c 79 20 72 65 71 75 65 73 74  actually request
3c550 65 64 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74  ed of the operat
3c560 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ing.**          
3c570 73 79 73 74 65 6d 2c 20 77 68 69 63 68 20 6d 69  system, which mi
3c580 67 68 74 20 62 65 20 6c 61 72 67 65 72 20 74 68  ght be larger th
3c590 61 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  an the parameter
3c5a0 20 4d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50   M..*/.SQLITE_AP
3c5b0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6c  I int sqlite3_sl
3c5c0 65 65 70 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a  eep(int);../*.**
3c5d0 20 43 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20   CAPI3REF: Name 
3c5e0 4f 66 20 54 68 65 20 46 6f 6c 64 65 72 20 48 6f  Of The Folder Ho
3c5f0 6c 64 69 6e 67 20 54 65 6d 70 6f 72 61 72 79 20  lding Temporary 
3c600 46 69 6c 65 73 20 7b 48 31 30 33 31 30 7d 20 3c  Files {H10310} <
3c610 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 49 66  S20000>.**.** If
3c620 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72   this global var
3c630 69 61 62 6c 65 20 69 73 20 6d 61 64 65 20 74 6f  iable is made to
3c640 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 72 69   point to a stri
3c650 6e 67 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 74  ng which is.** t
3c660 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 66 6f 6c  he name of a fol
3c670 64 65 72 20 28 61 2e 6b 2e 61 2e 20 64 69 72 65  der (a.k.a. dire
3c680 63 74 6f 72 79 29 2c 20 74 68 65 6e 20 61 6c 6c  ctory), then all
3c690 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73   temporary files
3c6a0 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 53  .** created by S
3c6b0 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 70 6c  QLite will be pl
3c6c0 61 63 65 64 20 69 6e 20 74 68 61 74 20 64 69 72  aced in that dir
3c6d0 65 63 74 6f 72 79 2e 20 20 49 66 20 74 68 69 73  ectory.  If this
3c6e0 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20   variable.** is 
3c6f0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20  a NULL pointer, 
3c700 74 68 65 6e 20 53 51 4c 69 74 65 20 70 65 72 66  then SQLite perf
3c710 6f 72 6d 73 20 61 20 73 65 61 72 63 68 20 66 6f  orms a search fo
3c720 72 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65  r an appropriate
3c730 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69  .** temporary fi
3c740 6c 65 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a  le directory..**
3c750 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61  .** It is not sa
3c760 66 65 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 69  fe to modify thi
3c770 73 20 76 61 72 69 61 62 6c 65 20 6f 6e 63 65 20  s variable once 
3c780 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  a [database conn
3c790 65 63 74 69 6f 6e 5d 0a 2a 2a 20 68 61 73 20 62  ection].** has b
3c7a0 65 65 6e 20 6f 70 65 6e 65 64 2e 20 20 49 74 20  een opened.  It 
3c7b0 69 73 20 69 6e 74 65 6e 64 65 64 20 74 68 61 74  is intended that
3c7c0 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 62   this variable b
3c7d0 65 20 73 65 74 20 6f 6e 63 65 0a 2a 2a 20 61 73  e set once.** as
3c7e0 20 70 61 72 74 20 6f 66 20 70 72 6f 63 65 73 73   part of process
3c7f0 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
3c800 61 6e 64 20 62 65 66 6f 72 65 20 61 6e 79 20 53  and before any S
3c810 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 0a  QLite interface.
3c820 2a 2a 20 72 6f 75 74 69 6e 65 73 20 68 61 76 65  ** routines have
3c830 20 62 65 65 6e 20 63 61 6c 6c 20 61 6e 64 20 72   been call and r
3c840 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65 64 20  emain unchanged 
3c850 74 68 65 72 65 61 66 74 65 72 2e 0a 2a 2f 0a 53  thereafter..*/.S
3c860 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a  QLITE_API char *
3c870 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72  sqlite3_temp_dir
3c880 65 63 74 6f 72 79 3b 0a 0a 2f 2a 0a 2a 2a 20 43  ectory;../*.** C
3c890 41 50 49 33 52 45 46 3a 20 54 65 73 74 20 46 6f  API3REF: Test Fo
3c8a0 72 20 41 75 74 6f 2d 43 6f 6d 6d 69 74 20 4d 6f  r Auto-Commit Mo
3c8b0 64 65 20 7b 48 31 32 39 33 30 7d 20 3c 53 36 30  de {H12930} <S60
3c8c0 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  200>.** KEYWORDS
3c8d0 3a 20 7b 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f  : {autocommit mo
3c8e0 64 65 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  de}.**.** The sq
3c8f0 6c 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f  lite3_get_autoco
3c900 6d 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63 65  mmit() interface
3c910 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72   returns non-zer
3c920 6f 20 6f 72 0a 2a 2a 20 7a 65 72 6f 20 69 66 20  o or.** zero if 
3c930 74 68 65 20 67 69 76 65 6e 20 64 61 74 61 62 61  the given databa
3c940 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  se connection is
3c950 20 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 61 75   or is not in au
3c960 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 0a 2a  tocommit mode,.*
3c970 2a 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20  * respectively. 
3c980 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65   Autocommit mode
3c990 20 69 73 20 6f 6e 20 62 79 20 64 65 66 61 75 6c   is on by defaul
3c9a0 74 2e 0a 2a 2a 20 41 75 74 6f 63 6f 6d 6d 69 74  t..** Autocommit
3c9b0 20 6d 6f 64 65 20 69 73 20 64 69 73 61 62 6c 65   mode is disable
3c9c0 64 20 62 79 20 61 20 5b 42 45 47 49 4e 5d 20 73  d by a [BEGIN] s
3c9d0 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 41 75 74  tatement..** Aut
3c9e0 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20  ocommit mode is 
3c9f0 72 65 2d 65 6e 61 62 6c 65 64 20 62 79 20 61 20  re-enabled by a 
3ca00 5b 43 4f 4d 4d 49 54 5d 20 6f 72 20 5b 52 4f 4c  [COMMIT] or [ROL
3ca10 4c 42 41 43 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66  LBACK]..**.** If
3ca20 20 63 65 72 74 61 69 6e 20 6b 69 6e 64 73 20 6f   certain kinds o
3ca30 66 20 65 72 72 6f 72 73 20 6f 63 63 75 72 20 6f  f errors occur o
3ca40 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 77 69  n a statement wi
3ca50 74 68 69 6e 20 61 20 6d 75 6c 74 69 2d 73 74 61  thin a multi-sta
3ca60 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61  tement.** transa
3ca70 63 74 69 6f 6e 20 28 65 72 72 6f 72 73 20 69 6e  ction (errors in
3ca80 63 6c 75 64 69 6e 67 20 5b 53 51 4c 49 54 45 5f  cluding [SQLITE_
3ca90 46 55 4c 4c 5d 2c 20 5b 53 51 4c 49 54 45 5f 49  FULL], [SQLITE_I
3caa0 4f 45 52 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54  OERR],.** [SQLIT
3cab0 45 5f 4e 4f 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54  E_NOMEM], [SQLIT
3cac0 45 5f 42 55 53 59 5d 2c 20 61 6e 64 20 5b 53 51  E_BUSY], and [SQ
3cad0 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 29  LITE_INTERRUPT])
3cae0 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 74 72 61   then the.** tra
3caf0 6e 73 61 63 74 69 6f 6e 20 6d 69 67 68 74 20 62  nsaction might b
3cb00 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 75  e rolled back au
3cb10 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 54 68  tomatically.  Th
3cb20 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f 0a 2a 2a  e only way to.**
3cb30 20 66 69 6e 64 20 6f 75 74 20 77 68 65 74 68 65   find out whethe
3cb40 72 20 53 51 4c 69 74 65 20 61 75 74 6f 6d 61 74  r SQLite automat
3cb50 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61  ically rolled ba
3cb60 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ck the transacti
3cb70 6f 6e 20 61 66 74 65 72 0a 2a 2a 20 61 6e 20 65  on after.** an e
3cb80 72 72 6f 72 20 69 73 20 74 6f 20 75 73 65 20 74  rror is to use t
3cb90 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a  his function..**
3cba0 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a  .** INVARIANTS:.
3cbb0 2a 2a 0a 2a 2a 20 7b 48 31 32 39 33 31 7d 20 54  **.** {H12931} T
3cbc0 68 65 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f  he [sqlite3_get_
3cbd0 61 75 74 6f 63 6f 6d 6d 69 74 28 44 29 5d 20 69  autocommit(D)] i
3cbe0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
3cbf0 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 0a 2a 2a 20   non-zero or.** 
3cc00 20 20 20 20 20 20 20 20 20 7a 65 72 6f 20 69 66           zero if
3cc10 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
3cc20 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 20 69 73 20  onnection] D is 
3cc30 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 61 75 74  or is not in aut
3cc40 6f 63 6f 6d 6d 69 74 0a 2a 2a 20 20 20 20 20 20  ocommit.**      
3cc50 20 20 20 20 6d 6f 64 65 2c 20 72 65 73 70 65 63      mode, respec
3cc60 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48  tively..**.** {H
3cc70 31 32 39 33 32 7d 20 41 75 74 6f 63 6f 6d 6d 69  12932} Autocommi
3cc80 74 20 6d 6f 64 65 20 69 73 20 6f 6e 20 62 79 20  t mode is on by 
3cc90 64 65 66 61 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 7b  default..**.** {
3cca0 48 31 32 39 33 33 7d 20 41 75 74 6f 63 6f 6d 6d  H12933} Autocomm
3ccb0 69 74 20 6d 6f 64 65 20 69 73 20 64 69 73 61 62  it mode is disab
3ccc0 6c 65 64 20 62 79 20 61 20 73 75 63 63 65 73 73  led by a success
3ccd0 66 75 6c 20 5b 42 45 47 49 4e 5d 20 73 74 61 74  ful [BEGIN] stat
3cce0 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  ement..**.** {H1
3ccf0 32 39 33 34 7d 20 41 75 74 6f 63 6f 6d 6d 69 74  2934} Autocommit
3cd00 20 6d 6f 64 65 20 69 73 20 65 6e 61 62 6c 65 64   mode is enabled
3cd10 20 62 79 20 61 20 73 75 63 63 65 73 73 66 75 6c   by a successful
3cd20 20 5b 43 4f 4d 4d 49 54 5d 20 6f 72 20 5b 52 4f   [COMMIT] or [RO
3cd30 4c 4c 42 41 43 4b 5d 0a 2a 2a 20 20 20 20 20 20  LLBACK].**      
3cd40 20 20 20 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a      statement..*
3cd50 2a 0a 2a 2a 20 41 53 53 55 4d 50 54 49 4f 4e 53  *.** ASSUMPTIONS
3cd60 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 39 33 36 7d  :.**.** {A12936}
3cd70 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65   If another thre
3cd80 61 64 20 63 68 61 6e 67 65 73 20 74 68 65 20 61  ad changes the a
3cd90 75 74 6f 63 6f 6d 6d 69 74 20 73 74 61 74 75 73  utocommit status
3cda0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
3cdb0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e  .**          con
3cdc0 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68  nection while th
3cdd0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 75  is routine is ru
3cde0 6e 6e 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 20  nning, then the 
3cdf0 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20  return value.** 
3ce00 20 20 20 20 20 20 20 20 20 69 73 20 75 6e 64 65           is unde
3ce10 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  fined..*/.SQLITE
3ce20 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
3ce30 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28  _get_autocommit(
3ce40 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a  sqlite3*);../*.*
3ce50 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69 6e 64  * CAPI3REF: Find
3ce60 20 54 68 65 20 44 61 74 61 62 61 73 65 20 48 61   The Database Ha
3ce70 6e 64 6c 65 20 4f 66 20 41 20 50 72 65 70 61 72  ndle Of A Prepar
3ce80 65 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31  ed Statement {H1
3ce90 33 31 32 30 7d 20 3c 53 36 30 36 30 30 3e 0a 2a  3120} <S60600>.*
3cea0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
3ceb0 5f 64 62 5f 68 61 6e 64 6c 65 20 69 6e 74 65 72  _db_handle inter
3cec0 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65  face returns the
3ced0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
3cee0 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a  ction] handle.**
3cef0 20 74 6f 20 77 68 69 63 68 20 61 20 5b 70 72 65   to which a [pre
3cf00 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
3cf10 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68 65 20 64   belongs.  The d
3cf20 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 72  atabase handle r
3cf30 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71  eturned by.** sq
3cf40 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 20  lite3_db_handle 
3cf50 69 73 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  is the same data
3cf60 62 61 73 65 20 68 61 6e 64 6c 65 20 74 68 61 74  base handle that
3cf70 20 77 61 73 20 74 68 65 20 66 69 72 73 74 20 61   was the first a
3cf80 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68  rgument.** to th
3cf90 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  e [sqlite3_prepa
3cfa0 72 65 5f 76 32 28 29 5d 20 63 61 6c 6c 20 28 6f  re_v2()] call (o
3cfb0 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 29 20  r its variants) 
3cfc0 74 68 61 74 20 77 61 73 20 75 73 65 64 20 74 6f  that was used to
3cfd0 0a 2a 2a 20 63 72 65 61 74 65 20 74 68 65 20 73  .** create the s
3cfe0 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20  tatement in the 
3cff0 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2a 0a  first place..**.
3d000 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a  ** INVARIANTS:.*
3d010 2a 0a 2a 2a 20 7b 48 31 33 31 32 33 7d 20 54 68  *.** {H13123} Th
3d020 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 68 61  e [sqlite3_db_ha
3d030 6e 64 6c 65 28 53 29 5d 20 69 6e 74 65 72 66 61  ndle(S)] interfa
3d040 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  ce returns a poi
3d050 6e 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  nter.**         
3d060 20 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73   to the [databas
3d070 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 73  e connection] as
3d080 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
3d090 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70  e.**          [p
3d0a0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
3d0b0 74 5d 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  t] S..*/.SQLITE_
3d0c0 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c  API sqlite3 *sql
3d0d0 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73  ite3_db_handle(s
3d0e0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a  qlite3_stmt*);..
3d0f0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
3d100 46 69 6e 64 20 74 68 65 20 6e 65 78 74 20 70 72  Find the next pr
3d110 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
3d120 20 7b 48 31 33 31 34 30 7d 20 3c 53 36 30 36 30   {H13140} <S6060
3d130 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  0>.**.** This in
3d140 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
3d150 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
3d160 20 6e 65 78 74 20 5b 70 72 65 70 61 72 65 64 20   next [prepared 
3d170 73 74 61 74 65 6d 65 6e 74 5d 20 61 66 74 65 72  statement] after
3d180 0a 2a 2a 20 70 53 74 6d 74 20 61 73 73 6f 63 69  .** pStmt associ
3d190 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64  ated with the [d
3d1a0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
3d1b0 6f 6e 5d 20 70 44 62 2e 20 20 49 66 20 70 53 74  on] pDb.  If pSt
3d1c0 6d 74 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68  mt is NULL.** th
3d1d0 65 6e 20 74 68 69 73 20 69 6e 74 65 72 66 61 63  en this interfac
3d1e0 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  e returns a poin
3d1f0 74 65 72 20 74 6f 20 74 68 65 20 66 69 72 73 74  ter to the first
3d200 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
3d210 65 6e 74 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65  ent.** associate
3d220 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62  d with the datab
3d230 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70  ase connection p
3d240 44 62 2e 20 20 49 66 20 6e 6f 20 70 72 65 70 61  Db.  If no prepa
3d250 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a  red statement.**
3d260 20 73 61 74 69 73 66 69 65 73 20 74 68 65 20 63   satisfies the c
3d270 6f 6e 64 69 74 69 6f 6e 73 20 6f 66 20 74 68 69  onditions of thi
3d280 73 20 72 6f 75 74 69 6e 65 2c 20 69 74 20 72 65  s routine, it re
3d290 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  turns NULL..**.*
3d2a0 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a  * INVARIANTS:.**
3d2b0 0a 2a 2a 20 7b 48 31 33 31 34 33 7d 20 49 66 20  .** {H13143} If 
3d2c0 44 20 69 73 20 61 20 5b 64 61 74 61 62 61 73 65  D is a [database
3d2d0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 68 61   connection] tha
3d2e0 74 20 68 6f 6c 64 73 20 6f 6e 65 20 6f 72 20 6d  t holds one or m
3d2f0 6f 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ore.**          
3d300 75 6e 66 69 6e 61 6c 69 7a 65 64 20 5b 70 72 65  unfinalized [pre
3d310 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73  pared statements
3d320 5d 20 61 6e 64 20 53 20 69 73 20 61 20 4e 55 4c  ] and S is a NUL
3d330 4c 20 70 6f 69 6e 74 65 72 2c 0a 2a 2a 20 20 20  L pointer,.**   
3d340 20 20 20 20 20 20 20 74 68 65 6e 20 5b 73 71 6c         then [sql
3d350 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 44  ite3_next_stmt(D
3d360 2c 20 53 29 5d 20 72 6f 75 74 69 6e 65 20 73 68  , S)] routine sh
3d370 61 6c 6c 20 72 65 74 75 72 6e 20 61 20 70 6f 69  all return a poi
3d380 6e 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  nter.**         
3d390 20 74 6f 20 6f 6e 65 20 6f 66 20 74 68 65 20 70   to one of the p
3d3a0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
3d3b0 74 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ts associated wi
3d3c0 74 68 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33  th D..**.** {H13
3d3d0 31 34 36 7d 20 49 66 20 44 20 69 73 20 61 20 5b  146} If D is a [
3d3e0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3d3f0 69 6f 6e 5d 20 74 68 61 74 20 68 6f 6c 64 73 20  ion] that holds 
3d400 6e 6f 20 75 6e 66 69 6e 61 6c 69 7a 65 64 0a 2a  no unfinalized.*
3d410 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70  *          [prep
3d420 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d  ared statements]
3d430 20 61 6e 64 20 53 20 69 73 20 61 20 4e 55 4c 4c   and S is a NULL
3d440 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 0a 2a   pointer, then.*
3d450 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
3d460 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 44 2c  te3_next_stmt(D,
3d470 20 53 29 5d 20 72 6f 75 74 69 6e 65 20 73 68 61   S)] routine sha
3d480 6c 6c 20 72 65 74 75 72 6e 20 61 20 4e 55 4c 4c  ll return a NULL
3d490 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   pointer..**.** 
3d4a0 7b 48 31 33 31 34 39 7d 20 49 66 20 53 20 69 73  {H13149} If S is
3d4b0 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61   a [prepared sta
3d4c0 74 65 6d 65 6e 74 5d 20 69 6e 20 74 68 65 20 5b  tement] in the [
3d4d0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3d4e0 69 6f 6e 5d 20 44 0a 2a 2a 20 20 20 20 20 20 20  ion] D.**       
3d4f0 20 20 20 61 6e 64 20 53 20 69 73 20 6e 6f 74 20     and S is not 
3d500 74 68 65 20 6c 61 73 74 20 70 72 65 70 61 72 65  the last prepare
3d510 64 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 44  d statement in D
3d520 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20  , then.**       
3d530 20 20 20 5b 73 71 6c 69 74 65 33 5f 6e 65 78 74     [sqlite3_next
3d540 5f 73 74 6d 74 28 44 2c 20 53 29 5d 20 72 6f 75  _stmt(D, S)] rou
3d550 74 69 6e 65 20 73 68 61 6c 6c 20 72 65 74 75 72  tine shall retur
3d560 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 20  n a pointer.**  
3d570 20 20 20 20 20 20 20 20 74 6f 20 74 68 65 20 6e          to the n
3d580 65 78 74 20 70 72 65 70 61 72 65 64 20 73 74 61  ext prepared sta
3d590 74 65 6d 65 6e 74 20 69 6e 20 44 20 61 66 74 65  tement in D afte
3d5a0 72 20 53 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 31  r S..**.** {H131
3d5b0 35 32 7d 20 49 66 20 53 20 69 73 20 74 68 65 20  52} If S is the 
3d5c0 6c 61 73 74 20 5b 70 72 65 70 61 72 65 64 20 73  last [prepared s
3d5d0 74 61 74 65 6d 65 6e 74 5d 20 69 6e 20 74 68 65  tatement] in the
3d5e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 64 61  .**          [da
3d5f0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3d600 6e 5d 20 44 20 74 68 65 6e 20 74 68 65 20 5b 73  n] D then the [s
3d610 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74  qlite3_next_stmt
3d620 28 44 2c 20 53 29 5d 0a 2a 2a 20 20 20 20 20 20  (D, S)].**      
3d630 20 20 20 20 72 6f 75 74 69 6e 65 20 73 68 61 6c      routine shal
3d640 6c 20 72 65 74 75 72 6e 20 61 20 4e 55 4c 4c 20  l return a NULL 
3d650 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41  pointer..**.** A
3d660 53 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a  SSUMPTIONS:.**.*
3d670 2a 20 7b 41 31 33 31 35 34 7d 20 54 68 65 20 5b  * {A13154} The [
3d680 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3d690 69 6f 6e 5d 20 70 6f 69 6e 74 65 72 20 44 20 69  ion] pointer D i
3d6a0 6e 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20  n a call to.**  
3d6b0 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33          [sqlite3
3d6c0 5f 6e 65 78 74 5f 73 74 6d 74 28 44 2c 53 29 5d  _next_stmt(D,S)]
3d6d0 20 6d 75 73 74 20 72 65 66 65 72 20 74 6f 20 61   must refer to a
3d6e0 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 0a  n open database.
3d6f0 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 6e  **          conn
3d700 65 63 74 69 6f 6e 20 61 6e 64 20 69 6e 20 70 61  ection and in pa
3d710 72 74 69 63 75 6c 61 72 20 6d 75 73 74 20 6e 6f  rticular must no
3d720 74 20 62 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  t be a NULL poin
3d730 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ter..*/.SQLITE_A
3d740 50 49 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  PI sqlite3_stmt 
3d750 2a 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74  *sqlite3_next_st
3d760 6d 74 28 73 71 6c 69 74 65 33 20 2a 70 44 62 2c  mt(sqlite3 *pDb,
3d770 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
3d780 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  Stmt);../*.** CA
3d790 50 49 33 52 45 46 3a 20 43 6f 6d 6d 69 74 20 41  PI3REF: Commit A
3d7a0 6e 64 20 52 6f 6c 6c 62 61 63 6b 20 4e 6f 74 69  nd Rollback Noti
3d7b0 66 69 63 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63  fication Callbac
3d7c0 6b 73 20 7b 48 31 32 39 35 30 7d 20 3c 53 36 30  ks {H12950} <S60
3d7d0 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  400>.**.** The s
3d7e0 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f  qlite3_commit_ho
3d7f0 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72  ok() interface r
3d800 65 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62  egisters a callb
3d810 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  ack.** function 
3d820 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68  to be invoked wh
3d830 65 6e 65 76 65 72 20 61 20 74 72 61 6e 73 61 63  enever a transac
3d840 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65  tion is committe
3d850 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61  d..** Any callba
3d860 63 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 76  ck set by a prev
3d870 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c  ious call to sql
3d880 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b  ite3_commit_hook
3d890 28 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61  ().** for the sa
3d8a0 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  me database conn
3d8b0 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69  ection is overri
3d8c0 64 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 73 71 6c  dden..** The sql
3d8d0 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f  ite3_rollback_ho
3d8e0 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72  ok() interface r
3d8f0 65 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62  egisters a callb
3d900 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  ack.** function 
3d910 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68  to be invoked wh
3d920 65 6e 65 76 65 72 20 61 20 74 72 61 6e 73 61 63  enever a transac
3d930 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65  tion is committe
3d940 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61  d..** Any callba
3d950 63 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 76  ck set by a prev
3d960 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c  ious call to sql
3d970 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b  ite3_commit_hook
3d980 28 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61  ().** for the sa
3d990 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  me database conn
3d9a0 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69  ection is overri
3d9b0 64 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 70 41 72  dden..** The pAr
3d9c0 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20 70 61  g argument is pa
3d9d0 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20  ssed through to 
3d9e0 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a  the callback..**
3d9f0 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   If the callback
3da00 20 6f 6e 20 61 20 63 6f 6d 6d 69 74 20 68 6f 6f   on a commit hoo
3da10 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  k function retur
3da20 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20  ns non-zero,.** 
3da30 74 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20  then the commit 
3da40 69 73 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74  is converted int
3da50 6f 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a  o a rollback..**
3da60 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 66  .** If another f
3da70 75 6e 63 74 69 6f 6e 20 77 61 73 20 70 72 65 76  unction was prev
3da80 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65  iously registere
3da90 64 2c 20 69 74 73 0a 2a 2a 20 70 41 72 67 20 76  d, its.** pArg v
3daa0 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64  alue is returned
3dab0 2e 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c  .  Otherwise NUL
3dac0 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  L is returned..*
3dad0 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63  *.** The callbac
3dae0 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  k implementation
3daf0 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79   must not do any
3db00 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20  thing that will 
3db10 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61  modify.** the da
3db20 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3db30 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74  n that invoked t
3db40 68 65 20 63 61 6c 6c 62 61 63 6b 2e 20 20 41 6e  he callback.  An
3db50 79 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20  y actions.** to 
3db60 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62  modify the datab
3db70 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d  ase connection m
3db80 75 73 74 20 62 65 20 64 65 66 65 72 72 65 64 20  ust be deferred 
3db90 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 0a  until after the.
3dba0 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66  ** completion of
3dbb0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74   the [sqlite3_st
3dbc0 65 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 20  ep()] call that 
3dbd0 74 72 69 67 67 65 72 65 64 20 74 68 65 20 63 6f  triggered the co
3dbe0 6d 6d 69 74 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 62  mmit.** or rollb
3dbf0 61 63 6b 20 68 6f 6f 6b 20 69 6e 20 74 68 65 20  ack hook in the 
3dc00 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2a 20  first place..** 
3dc10 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69 74  Note that [sqlit
3dc20 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
3dc30 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74   and [sqlite3_st
3dc40 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66  ep()] both modif
3dc50 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 61 62  y their.** datab
3dc60 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ase connections 
3dc70 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 20  for the meaning 
3dc80 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 74  of "modify" in t
3dc90 68 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a 2a  his paragraph..*
3dca0 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 69 6e 67  *.** Registering
3dcb0 20 61 20 4e 55 4c 4c 20 66 75 6e 63 74 69 6f 6e   a NULL function
3dcc0 20 64 69 73 61 62 6c 65 73 20 74 68 65 20 63 61   disables the ca
3dcd0 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 6f  llback..**.** Fo
3dce0 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f  r the purposes o
3dcf0 66 20 74 68 69 73 20 41 50 49 2c 20 61 20 74 72  f this API, a tr
3dd00 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 61 69  ansaction is sai
3dd10 64 20 74 6f 20 68 61 76 65 20 62 65 65 6e 0a 2a  d to have been.*
3dd20 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69 66  * rolled back if
3dd30 20 61 6e 20 65 78 70 6c 69 63 69 74 20 22 52 4f   an explicit "RO
3dd40 4c 4c 42 41 43 4b 22 20 73 74 61 74 65 6d 65 6e  LLBACK" statemen
3dd50 74 20 69 73 20 65 78 65 63 75 74 65 64 2c 20 6f  t is executed, o
3dd60 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 72  r.** an error or
3dd70 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 61 75 73   constraint caus
3dd80 65 73 20 61 6e 20 69 6d 70 6c 69 63 69 74 20 72  es an implicit r
3dd90 6f 6c 6c 62 61 63 6b 20 74 6f 20 6f 63 63 75 72  ollback to occur
3dda0 2e 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63  ..** The rollbac
3ddb0 6b 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f  k callback is no
3ddc0 74 20 69 6e 76 6f 6b 65 64 20 69 66 20 61 20 74  t invoked if a t
3ddd0 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a  ransaction is.**
3dde0 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72   automatically r
3ddf0 6f 6c 6c 65 64 20 62 61 63 6b 20 62 65 63 61 75  olled back becau
3de00 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  se the database 
3de10 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c  connection is cl
3de20 6f 73 65 64 2e 0a 2a 2a 20 54 68 65 20 72 6f 6c  osed..** The rol
3de30 6c 62 61 63 6b 20 63 61 6c 6c 62 61 63 6b 20 69  lback callback i
3de40 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 69 66  s not invoked if
3de50 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
3de60 73 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b  s.** rolled back
3de70 20 62 65 63 61 75 73 65 20 61 20 63 6f 6d 6d 69   because a commi
3de80 74 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72  t callback retur
3de90 6e 65 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a  ned non-zero..**
3dea0 20 3c 74 6f 64 6f 3e 20 43 68 65 63 6b 20 6f 6e   <todo> Check on
3deb0 20 74 68 69 73 20 3c 2f 74 6f 64 6f 3e 0a 2a 2a   this </todo>.**
3dec0 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a  .** INVARIANTS:.
3ded0 2a 2a 0a 2a 2a 20 7b 48 31 32 39 35 31 7d 20 54  **.** {H12951} T
3dee0 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 6d  he [sqlite3_comm
3def0 69 74 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d 20  it_hook(D,F,P)] 
3df00 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74  interface regist
3df10 65 72 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  ers the.**      
3df20 20 20 20 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e      callback fun
3df30 63 74 69 6f 6e 20 46 20 74 6f 20 62 65 20 69 6e  ction F to be in
3df40 76 6f 6b 65 64 20 77 69 74 68 20 61 72 67 75 6d  voked with argum
3df50 65 6e 74 20 50 20 77 68 65 6e 65 76 65 72 0a 2a  ent P whenever.*
3df60 2a 20 20 20 20 20 20 20 20 20 20 61 20 74 72 61  *          a tra
3df70 6e 73 61 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73  nsaction commits
3df80 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73   on the [databas
3df90 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 2e  e connection] D.
3dfa0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 35 32 7d 20  .**.** {H12952} 
3dfb0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d  The [sqlite3_com
3dfc0 6d 69 74 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d  mit_hook(D,F,P)]
3dfd0 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72   interface retur
3dfe0 6e 73 20 74 68 65 20 50 20 61 72 67 75 6d 65 6e  ns the P argumen
3dff0 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 72  t.**          fr
3e000 6f 6d 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  om the previous 
3e010 63 61 6c 6c 20 77 69 74 68 20 74 68 65 20 73 61  call with the sa
3e020 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  me [database con
3e030 6e 65 63 74 69 6f 6e 5d 20 44 2c 0a 2a 2a 20 20  nection] D,.**  
3e040 20 20 20 20 20 20 20 20 6f 72 20 4e 55 4c 4c 20          or NULL 
3e050 6f 6e 20 74 68 65 20 66 69 72 73 74 20 63 61 6c  on the first cal
3e060 6c 20 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c  l for a particul
3e070 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ar database conn
3e080 65 63 74 69 6f 6e 20 44 2e 0a 2a 2a 0a 2a 2a 20  ection D..**.** 
3e090 7b 48 31 32 39 35 33 7d 20 45 61 63 68 20 63 61  {H12953} Each ca
3e0a0 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63  ll to [sqlite3_c
3e0b0 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 5d 20 6f 76  ommit_hook()] ov
3e0c0 65 72 77 72 69 74 65 73 20 74 68 65 20 63 61 6c  erwrites the cal
3e0d0 6c 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 20 20  lback.**        
3e0e0 20 20 72 65 67 69 73 74 65 72 65 64 20 62 79 20    registered by 
3e0f0 70 72 69 6f 72 20 63 61 6c 6c 73 2e 0a 2a 2a 0a  prior calls..**.
3e100 2a 2a 20 7b 48 31 32 39 35 34 7d 20 49 66 20 74  ** {H12954} If t
3e110 68 65 20 46 20 61 72 67 75 6d 65 6e 74 20 74 6f  he F argument to
3e120 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74   [sqlite3_commit
3e130 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d 20 69 73  _hook(D,F,P)] is
3e140 20 4e 55 4c 4c 0a 2a 2a 20 20 20 20 20 20 20 20   NULL.**        
3e150 20 20 74 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69    then the commi
3e160 74 20 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b 20  t hook callback 
3e170 69 73 20 63 61 6e 63 65 6c 65 64 20 61 6e 64 20  is canceled and 
3e180 6e 6f 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20  no callback.**  
3e190 20 20 20 20 20 20 20 20 69 73 20 69 6e 76 6f 6b          is invok
3e1a0 65 64 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61  ed when a transa
3e1b0 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73 2e 0a 2a  ction commits..*
3e1c0 2a 0a 2a 2a 20 7b 48 31 32 39 35 35 7d 20 49 66  *.** {H12955} If
3e1d0 20 74 68 65 20 63 6f 6d 6d 69 74 20 63 61 6c 6c   the commit call
3e1e0 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e  back returns non
3e1f0 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68 65 20 63  -zero then the c
3e200 6f 6d 6d 69 74 20 69 73 0a 2a 2a 20 20 20 20 20  ommit is.**     
3e210 20 20 20 20 20 63 6f 6e 76 65 72 74 65 64 20 69       converted i
3e220 6e 74 6f 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a  nto a rollback..
3e230 2a 2a 0a 2a 2a 20 7b 48 31 32 39 36 31 7d 20 54  **.** {H12961} T
3e240 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 6f 6c 6c  he [sqlite3_roll
3e250 62 61 63 6b 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29  back_hook(D,F,P)
3e260 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69  ] interface regi
3e270 73 74 65 72 73 20 74 68 65 0a 2a 2a 20 20 20 20  sters the.**    
3e280 20 20 20 20 20 20 63 61 6c 6c 62 61 63 6b 20 66        callback f
3e290 75 6e 63 74 69 6f 6e 20 46 20 74 6f 20 62 65 20  unction F to be 
3e2a0 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 61 72 67  invoked with arg
3e2b0 75 6d 65 6e 74 20 50 20 77 68 65 6e 65 76 65 72  ument P whenever
3e2c0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 20 74  .**          a t
3e2d0 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c 73  ransaction rolls
3e2e0 20 62 61 63 6b 20 6f 6e 20 74 68 65 20 5b 64 61   back on the [da
3e2f0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3e300 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  n] D..**.** {H12
3e310 39 36 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  962} The [sqlite
3e320 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28  3_rollback_hook(
3e330 44 2c 46 2c 50 29 5d 20 69 6e 74 65 72 66 61 63  D,F,P)] interfac
3e340 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 50 0a  e returns the P.
3e350 2a 2a 20 20 20 20 20 20 20 20 20 20 61 72 67 75  **          argu
3e360 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 70 72  ment from the pr
3e370 65 76 69 6f 75 73 20 63 61 6c 6c 20 77 69 74 68  evious call with
3e380 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 20 20 20   the same.**    
3e390 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 65 20        [database 
3e3a0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 2c 20 6f  connection] D, o
3e3b0 72 20 4e 55 4c 4c 20 6f 6e 20 74 68 65 20 66 69  r NULL on the fi
3e3c0 72 73 74 20 63 61 6c 6c 0a 2a 2a 20 20 20 20 20  rst call.**     
3e3d0 20 20 20 20 20 66 6f 72 20 61 20 70 61 72 74 69       for a parti
3e3e0 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63  cular database c
3e3f0 6f 6e 6e 65 63 74 69 6f 6e 20 44 2e 0a 2a 2a 0a  onnection D..**.
3e400 2a 2a 20 7b 48 31 32 39 36 33 7d 20 45 61 63 68  ** {H12963} Each
3e410 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
3e420 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28  3_rollback_hook(
3e430 29 5d 20 6f 76 65 72 77 72 69 74 65 73 20 74 68  )] overwrites th
3e440 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20  e callback.**   
3e450 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 65         registere
3e460 64 20 62 79 20 70 72 69 6f 72 20 63 61 6c 6c 73  d by prior calls
3e470 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 36 34 7d  ..**.** {H12964}
3e480 20 49 66 20 74 68 65 20 46 20 61 72 67 75 6d 65   If the F argume
3e490 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72  nt to [sqlite3_r
3e4a0 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 44 2c 46  ollback_hook(D,F
3e4b0 2c 50 29 5d 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20  ,P)] is NULL.** 
3e4c0 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 74 68           then th
3e4d0 65 20 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f 6b 20  e rollback hook 
3e4e0 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 61 6e 63  callback is canc
3e4f0 65 6c 65 64 20 61 6e 64 20 6e 6f 20 63 61 6c 6c  eled and no call
3e500 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 20 20 20  back.**         
3e510 20 69 73 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e   is invoked when
3e520 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72   a transaction r
3e530 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2f 0a 53 51  olls back..*/.SQ
3e540 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73  LITE_API void *s
3e550 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f  qlite3_commit_ho
3e560 6f 6b 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  ok(sqlite3*, int
3e570 28 2a 29 28 76 6f 69 64 2a 29 2c 20 76 6f 69 64  (*)(void*), void
3e580 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  *);.SQLITE_API v
3e590 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 6f 6c  oid *sqlite3_rol
3e5a0 6c 62 61 63 6b 5f 68 6f 6f 6b 28 73 71 6c 69 74  lback_hook(sqlit
3e5b0 65 33 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69  e3*, void(*)(voi
3e5c0 64 20 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f  d *), void*);../
3e5d0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44  *.** CAPI3REF: D
3e5e0 61 74 61 20 43 68 61 6e 67 65 20 4e 6f 74 69 66  ata Change Notif
3e5f0 69 63 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b  ication Callback
3e600 73 20 7b 48 31 32 39 37 30 7d 20 3c 53 36 30 34  s {H12970} <S604
3e610 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  00>.**.** The sq
3e620 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f  lite3_update_hoo
3e630 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65  k() interface re
3e640 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61  gisters a callba
3e650 63 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 77  ck function.** w
3e660 69 74 68 20 74 68 65 20 5b 64 61 74 61 62 61 73  ith the [databas
3e670 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 64  e connection] id
3e680 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20  entified by the 
3e690 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 0a 2a  first argument.*
3e6a0 2a 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20  * to be invoked 
3e6b0 77 68 65 6e 65 76 65 72 20 61 20 72 6f 77 20 69  whenever a row i
3e6c0 73 20 75 70 64 61 74 65 64 2c 20 69 6e 73 65 72  s updated, inser
3e6d0 74 65 64 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a  ted or deleted..
3e6e0 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 63 6b 20  ** Any callback 
3e6f0 73 65 74 20 62 79 20 61 20 70 72 65 76 69 6f 75  set by a previou
3e700 73 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 66  s call to this f
3e710 75 6e 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74  unction.** for t
3e720 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
3e730 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f   connection is o
3e740 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a 0a 2a 2a  verridden..**.**
3e750 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   The second argu
3e760 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65  ment is a pointe
3e770 72 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f  r to the functio
3e780 6e 20 74 6f 20 69 6e 76 6f 6b 65 20 77 68 65 6e  n to invoke when
3e790 20 61 0a 2a 2a 20 72 6f 77 20 69 73 20 75 70 64   a.** row is upd
3e7a0 61 74 65 64 2c 20 69 6e 73 65 72 74 65 64 20 6f  ated, inserted o
3e7b0 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 54 68  r deleted..** Th
3e7c0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
3e7d0 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   to the callback
3e7e0 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68   is a copy of th
3e7f0 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  e third argument
3e800 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 75  .** to sqlite3_u
3e810 70 64 61 74 65 5f 68 6f 6f 6b 28 29 2e 0a 2a 2a  pdate_hook()..**
3e820 20 54 68 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c   The second call
3e830 62 61 63 6b 20 61 72 67 75 6d 65 6e 74 20 69 73  back argument is
3e840 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f   one of [SQLITE_
3e850 49 4e 53 45 52 54 5d 2c 20 5b 53 51 4c 49 54 45  INSERT], [SQLITE
3e860 5f 44 45 4c 45 54 45 5d 2c 0a 2a 2a 20 6f 72 20  _DELETE],.** or 
3e870 5b 53 51 4c 49 54 45 5f 55 50 44 41 54 45 5d 2c  [SQLITE_UPDATE],
3e880 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68   depending on th
3e890 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74  e operation that
3e8a0 20 63 61 75 73 65 64 20 74 68 65 20 63 61 6c 6c   caused the call
3e8b0 62 61 63 6b 0a 2a 2a 20 74 6f 20 62 65 20 69 6e  back.** to be in
3e8c0 76 6f 6b 65 64 2e 0a 2a 2a 20 54 68 65 20 74 68  voked..** The th
3e8d0 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 61  ird and fourth a
3e8e0 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20  rguments to the 
3e8f0 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e  callback contain
3e900 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65   pointers to the
3e910 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 61 6e 64  .** database and
3e920 20 74 61 62 6c 65 20 6e 61 6d 65 20 63 6f 6e 74   table name cont
3e930 61 69 6e 69 6e 67 20 74 68 65 20 61 66 66 65 63  aining the affec
3e940 74 65 64 20 72 6f 77 2e 0a 2a 2a 20 54 68 65 20  ted row..** The 
3e950 66 69 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 20 70  final callback p
3e960 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20  arameter is the 
3e970 72 6f 77 69 64 20 6f 66 20 74 68 65 20 72 6f 77  rowid of the row
3e980 2e 20 49 6e 20 74 68 65 20 63 61 73 65 20 6f 66  . In the case of
3e990 0a 2a 2a 20 61 6e 20 75 70 64 61 74 65 2c 20 74  .** an update, t
3e9a0 68 69 73 20 69 73 20 74 68 65 20 72 6f 77 69 64  his is the rowid
3e9b0 20 61 66 74 65 72 20 74 68 65 20 75 70 64 61 74   after the updat
3e9c0 65 20 74 61 6b 65 73 20 70 6c 61 63 65 2e 0a 2a  e takes place..*
3e9d0 2a 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65 20  *.** The update 
3e9e0 68 6f 6f 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f  hook is not invo
3e9f0 6b 65 64 20 77 68 65 6e 20 69 6e 74 65 72 6e 61  ked when interna
3ea00 6c 20 73 79 73 74 65 6d 20 74 61 62 6c 65 73 20  l system tables 
3ea10 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 20  are.** modified 
3ea20 28 69 2e 65 2e 20 73 71 6c 69 74 65 5f 6d 61 73  (i.e. sqlite_mas
3ea30 74 65 72 20 61 6e 64 20 73 71 6c 69 74 65 5f 73  ter and sqlite_s
3ea40 65 71 75 65 6e 63 65 29 2e 0a 2a 2a 0a 2a 2a 20  equence)..**.** 
3ea50 54 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20  The update hook 
3ea60 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d  implementation m
3ea70 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68  ust not do anyth
3ea80 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f  ing that will mo
3ea90 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61  dify.** the data
3eaa0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
3eab0 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65  that invoked the
3eac0 20 75 70 64 61 74 65 20 68 6f 6f 6b 2e 20 20 41   update hook.  A
3ead0 6e 79 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f  ny actions.** to
3eae0 20 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 61   modify the data
3eaf0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
3eb00 6d 75 73 74 20 62 65 20 64 65 66 65 72 72 65 64  must be deferred
3eb10 20 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65   until after the
3eb20 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f  .** completion o
3eb30 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73  f the [sqlite3_s
3eb40 74 65 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 74  tep()] call that
3eb50 20 74 72 69 67 67 65 72 65 64 20 74 68 65 20 75   triggered the u
3eb60 70 64 61 74 65 20 68 6f 6f 6b 2e 0a 2a 2a 20 4e  pdate hook..** N
3eb70 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69 74 65  ote that [sqlite
3eb80 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20  3_prepare_v2()] 
3eb90 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  and [sqlite3_ste
3eba0 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79  p()] both modify
3ebb0 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 61   their.** databa
3ebc0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66  se connections f
3ebd0 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f  or the meaning o
3ebe0 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 68  f "modify" in th
3ebf0 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a  is paragraph..**
3ec00 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 66  .** If another f
3ec10 75 6e 63 74 69 6f 6e 20 77 61 73 20 70 72 65 76  unction was prev
3ec20 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65  iously registere
3ec30 64 2c 20 69 74 73 20 70 41 72 67 20 76 61 6c 75  d, its pArg valu
3ec40 65 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64  e.** is returned
3ec50 2e 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c  .  Otherwise NUL
3ec60 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  L is returned..*
3ec70 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
3ec80 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 37 31 7d 20  .**.** {H12971} 
3ec90 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 75 70 64  The [sqlite3_upd
3eca0 61 74 65 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d  ate_hook(D,F,P)]
3ecb0 20 69 6e 74 65 72 66 61 63 65 20 63 61 75 73 65   interface cause
3ecc0 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a  s the callback.*
3ecd0 2a 20 20 20 20 20 20 20 20 20 20 66 75 6e 63 74  *          funct
3ece0 69 6f 6e 20 46 20 74 6f 20 62 65 20 69 6e 76 6f  ion F to be invo
3ecf0 6b 65 64 20 77 69 74 68 20 66 69 72 73 74 20 70  ked with first p
3ed00 61 72 61 6d 65 74 65 72 20 50 20 77 68 65 6e 65  arameter P whene
3ed10 76 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ver.**          
3ed20 61 20 74 61 62 6c 65 20 72 6f 77 20 69 73 20 6d  a table row is m
3ed30 6f 64 69 66 69 65 64 2c 20 69 6e 73 65 72 74 65  odified, inserte
3ed40 64 2c 20 6f 72 20 64 65 6c 65 74 65 64 20 6f 6e  d, or deleted on
3ed50 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65  .**          the
3ed60 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
3ed70 63 74 69 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20  ction] D..**.** 
3ed80 7b 48 31 32 39 37 33 7d 20 54 68 65 20 5b 73 71  {H12973} The [sq
3ed90 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f  lite3_update_hoo
3eda0 6b 28 44 2c 46 2c 50 29 5d 20 69 6e 74 65 72 66  k(D,F,P)] interf
3edb0 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ace returns the 
3edc0 76 61 6c 75 65 0a 2a 2a 20 20 20 20 20 20 20 20  value.**        
3edd0 20 20 6f 66 20 50 20 66 6f 72 20 74 68 65 20 70    of P for the p
3ede0 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 6f 6e 20  revious call on 
3edf0 74 68 65 20 73 61 6d 65 20 5b 64 61 74 61 62 61  the same [databa
3ee00 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44  se connection] D
3ee10 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72  ,.**          or
3ee20 20 4e 55 4c 4c 20 66 6f 72 20 74 68 65 20 66 69   NULL for the fi
3ee30 72 73 74 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20  rst call..**.** 
3ee40 7b 48 31 32 39 37 35 7d 20 49 66 20 74 68 65 20  {H12975} If the 
3ee50 75 70 64 61 74 65 20 68 6f 6f 6b 20 63 61 6c 6c  update hook call
3ee60 62 61 63 6b 20 46 20 69 6e 20 5b 73 71 6c 69 74  back F in [sqlit
3ee70 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 44  e3_update_hook(D
3ee80 2c 46 2c 50 29 5d 0a 2a 2a 20 20 20 20 20 20 20  ,F,P)].**       
3ee90 20 20 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20     is NULL then 
3eea0 74 68 65 20 6e 6f 20 75 70 64 61 74 65 20 63 61  the no update ca
3eeb0 6c 6c 62 61 63 6b 73 20 61 72 65 20 6d 61 64 65  llbacks are made
3eec0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 37 37 7d  ..**.** {H12977}
3eed0 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73   Each call to [s
3eee0 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f  qlite3_update_ho
3eef0 6f 6b 28 44 2c 46 2c 50 29 5d 20 6f 76 65 72 72  ok(D,F,P)] overr
3ef00 69 64 65 73 20 70 72 69 6f 72 20 63 61 6c 6c 73  ides prior calls
3ef10 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f 20  .**          to 
3ef20 74 68 65 20 73 61 6d 65 20 69 6e 74 65 72 66 61  the same interfa
3ef30 63 65 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 5b  ce on the same [
3ef40 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3ef50 69 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48  ion] D..**.** {H
3ef60 31 32 39 37 39 7d 20 54 68 65 20 75 70 64 61 74  12979} The updat
3ef70 65 20 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b 20  e hook callback 
3ef80 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 77  is not invoked w
3ef90 68 65 6e 20 69 6e 74 65 72 6e 61 6c 20 73 79 73  hen internal sys
3efa0 74 65 6d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  tem.**          
3efb0 74 61 62 6c 65 73 20 73 75 63 68 20 61 73 20 73  tables such as s
3efc0 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 61 6e 64  qlite_master and
3efd0 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65   sqlite_sequence
3efe0 20 61 72 65 20 6d 6f 64 69 66 69 65 64 2e 0a 2a   are modified..*
3eff0 2a 0a 2a 2a 20 7b 48 31 32 39 38 31 7d 20 54 68  *.** {H12981} Th
3f000 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
3f010 65 72 20 74 6f 20 74 68 65 20 75 70 64 61 74 65  er to the update
3f020 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20 20   callback.**    
3f030 20 20 20 20 20 20 69 73 20 6f 6e 65 20 6f 66 20        is one of 
3f040 5b 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 5d 2c  [SQLITE_INSERT],
3f050 20 5b 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 5d   [SQLITE_DELETE]
3f060 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 50 44 41   or [SQLITE_UPDA
3f070 54 45 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20  TE],.**         
3f080 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68   depending on th
3f090 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74  e operation that
3f0a0 20 63 61 75 73 65 64 20 74 68 65 20 63 61 6c 6c   caused the call
3f0b0 62 61 63 6b 20 74 6f 20 62 65 20 69 6e 76 6f 6b  back to be invok
3f0c0 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 38  ed..**.** {H1298
3f0d0 33 7d 20 54 68 65 20 74 68 69 72 64 20 61 6e 64  3} The third and
3f0e0 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74   fourth argument
3f0f0 73 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63  s to the callbac
3f100 6b 20 63 6f 6e 74 61 69 6e 20 70 6f 69 6e 74 65  k contain pointe
3f110 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  rs.**          t
3f120 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  o zero-terminate
3f130 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 73 20  d UTF-8 strings 
3f140 77 68 69 63 68 20 61 72 65 20 74 68 65 20 6e 61  which are the na
3f150 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20  mes of the.**   
3f160 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20         database 
3f170 61 6e 64 20 74 61 62 6c 65 20 74 68 61 74 20 69  and table that i
3f180 73 20 62 65 69 6e 67 20 75 70 64 61 74 65 64 2e  s being updated.
3f190 0a 0a 2a 2a 20 7b 48 31 32 39 38 35 7d 20 54 68  ..** {H12985} Th
3f1a0 65 20 66 69 6e 61 6c 20 63 61 6c 6c 62 61 63 6b  e final callback
3f1b0 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68   parameter is th
3f1c0 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 72  e rowid of the r
3f1d0 6f 77 20 61 66 74 65 72 0a 2a 2a 20 20 20 20 20  ow after.**     
3f1e0 20 20 20 20 20 74 68 65 20 63 68 61 6e 67 65 20       the change 
3f1f0 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54  occurs..*/.SQLIT
3f200 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
3f210 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28  te3_update_hook(
3f220 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20  .  sqlite3*, .  
3f230 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a 2c 69  void(*)(void *,i
3f240 6e 74 20 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a  nt ,char const *
3f250 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c 73 71  ,char const *,sq
3f260 6c 69 74 65 33 5f 69 6e 74 36 34 29 2c 0a 20 20  lite3_int64),.  
3f270 76 6f 69 64 2a 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  void*.);../*.** 
3f280 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62 6c 65  CAPI3REF: Enable
3f290 20 4f 72 20 44 69 73 61 62 6c 65 20 53 68 61 72   Or Disable Shar
3f2a0 65 64 20 50 61 67 65 72 20 43 61 63 68 65 20 7b  ed Pager Cache {
3f2b0 48 31 30 33 33 30 7d 20 3c 53 33 30 39 30 30 3e  H10330} <S30900>
3f2c0 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 73  .** KEYWORDS: {s
3f2d0 68 61 72 65 64 20 63 61 63 68 65 7d 20 7b 73 68  hared cache} {sh
3f2e0 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 7d  ared cache mode}
3f2f0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
3f300 69 6e 65 20 65 6e 61 62 6c 65 73 20 6f 72 20 64  ine enables or d
3f310 69 73 61 62 6c 65 73 20 74 68 65 20 73 68 61 72  isables the shar
3f320 69 6e 67 20 6f 66 20 74 68 65 20 64 61 74 61 62  ing of the datab
3f330 61 73 65 20 63 61 63 68 65 0a 2a 2a 20 61 6e 64  ase cache.** and
3f340 20 73 63 68 65 6d 61 20 64 61 74 61 20 73 74 72   schema data str
3f350 75 63 74 75 72 65 73 20 62 65 74 77 65 65 6e 20  uctures between 
3f360 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
3f370 74 69 6f 6e 20 7c 20 63 6f 6e 6e 65 63 74 69 6f  tion | connectio
3f380 6e 73 5d 0a 2a 2a 20 74 6f 20 74 68 65 20 73 61  ns].** to the sa
3f390 6d 65 20 64 61 74 61 62 61 73 65 2e 20 53 68 61  me database. Sha
3f3a0 72 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 20  ring is enabled 
3f3b0 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  if the argument 
3f3c0 69 73 20 74 72 75 65 0a 2a 2a 20 61 6e 64 20 64  is true.** and d
3f3d0 69 73 61 62 6c 65 64 20 69 66 20 74 68 65 20 61  isabled if the a
3f3e0 72 67 75 6d 65 6e 74 20 69 73 20 66 61 6c 73 65  rgument is false
3f3f0 2e 0a 2a 2a 0a 2a 2a 20 43 61 63 68 65 20 73 68  ..**.** Cache sh
3f400 61 72 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64  aring is enabled
3f410 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20 66 6f   and disabled fo
3f420 72 20 61 6e 20 65 6e 74 69 72 65 20 70 72 6f 63  r an entire proc
3f430 65 73 73 2e 20 7b 45 4e 44 7d 0a 2a 2a 20 54 68  ess. {END}.** Th
3f440 69 73 20 69 73 20 61 20 63 68 61 6e 67 65 20 61  is is a change a
3f450 73 20 6f 66 20 53 51 4c 69 74 65 20 76 65 72 73  s of SQLite vers
3f460 69 6f 6e 20 33 2e 35 2e 30 2e 20 49 6e 20 70 72  ion 3.5.0. In pr
3f470 69 6f 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  ior versions of 
3f480 53 51 4c 69 74 65 2c 0a 2a 2a 20 73 68 61 72 69  SQLite,.** shari
3f490 6e 67 20 77 61 73 20 65 6e 61 62 6c 65 64 20 6f  ng was enabled o
3f4a0 72 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 65  r disabled for e
3f4b0 61 63 68 20 74 68 72 65 61 64 20 73 65 70 61 72  ach thread separ
3f4c0 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ately..**.** The
3f4d0 20 63 61 63 68 65 20 73 68 61 72 69 6e 67 20 6d   cache sharing m
3f4e0 6f 64 65 20 73 65 74 20 62 79 20 74 68 69 73 20  ode set by this 
3f4f0 69 6e 74 65 72 66 61 63 65 20 65 66 66 65 63 74  interface effect
3f500 73 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74  s all subsequent
3f510 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71  .** calls to [sq
3f520 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b  lite3_open()], [
3f530 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
3f540 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  )], and [sqlite3
3f550 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 20 45  _open16()]..** E
3f560 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 73 65  xisting database
3f570 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 6f 6e   connections con
3f580 74 69 6e 75 65 20 75 73 65 20 74 68 65 20 73 68  tinue use the sh
3f590 61 72 69 6e 67 20 6d 6f 64 65 0a 2a 2a 20 74 68  aring mode.** th
3f5a0 61 74 20 77 61 73 20 69 6e 20 65 66 66 65 63 74  at was in effect
3f5b0 20 61 74 20 74 68 65 20 74 69 6d 65 20 74 68 65   at the time the
3f5c0 79 20 77 65 72 65 20 6f 70 65 6e 65 64 2e 0a 2a  y were opened..*
3f5d0 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 61 62  *.** Virtual tab
3f5e0 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65 20 75 73  les cannot be us
3f5f0 65 64 20 77 69 74 68 20 61 20 73 68 61 72 65 64  ed with a shared
3f600 20 63 61 63 68 65 2e 20 20 57 68 65 6e 20 73 68   cache.  When sh
3f610 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 69 73  ared.** cache is
3f620 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 20 5b 73   enabled, the [s
3f630 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f  qlite3_create_mo
3f640 64 75 6c 65 28 29 5d 20 41 50 49 20 75 73 65 64  dule()] API used
3f650 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a 2a 20   to register.** 
3f660 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 77  virtual tables w
3f670 69 6c 6c 20 61 6c 77 61 79 73 20 72 65 74 75 72  ill always retur
3f680 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a  n an error..**.*
3f690 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
3f6a0 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f  eturns [SQLITE_O
3f6b0 4b 5d 20 69 66 20 73 68 61 72 65 64 20 63 61 63  K] if shared cac
3f6c0 68 65 20 77 61 73 20 65 6e 61 62 6c 65 64 20 6f  he was enabled o
3f6d0 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 73 75  r disabled.** su
3f6e0 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 41 6e 20  ccessfully.  An 
3f6f0 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20  [error code] is 
3f700 72 65 74 75 72 6e 65 64 20 6f 74 68 65 72 77 69  returned otherwi
3f710 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65 64  se..**.** Shared
3f720 20 63 61 63 68 65 20 69 73 20 64 69 73 61 62 6c   cache is disabl
3f730 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e 20 42  ed by default. B
3f740 75 74 20 74 68 69 73 20 6d 69 67 68 74 20 63 68  ut this might ch
3f750 61 6e 67 65 20 69 6e 0a 2a 2a 20 66 75 74 75 72  ange in.** futur
3f760 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51  e releases of SQ
3f770 4c 69 74 65 2e 20 20 41 70 70 6c 69 63 61 74 69  Lite.  Applicati
3f780 6f 6e 73 20 74 68 61 74 20 63 61 72 65 20 61 62  ons that care ab
3f790 6f 75 74 20 73 68 61 72 65 64 0a 2a 2a 20 63 61  out shared.** ca
3f7a0 63 68 65 20 73 65 74 74 69 6e 67 20 73 68 6f 75  che setting shou
3f7b0 6c 64 20 73 65 74 20 69 74 20 65 78 70 6c 69 63  ld set it explic
3f7c0 69 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41  itly..**.** INVA
3f7d0 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48  RIANTS:.**.** {H
3f7e0 31 30 33 33 31 7d 20 41 20 73 75 63 63 65 73 73  10331} A success
3f7f0 66 75 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  ful invocation o
3f800 66 20 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c  f [sqlite3_enabl
3f810 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 28 42  e_shared_cache(B
3f820 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77  )].**          w
3f830 69 6c 6c 20 65 6e 61 62 6c 65 20 6f 72 20 64 69  ill enable or di
3f840 73 61 62 6c 65 20 73 68 61 72 65 64 20 63 61 63  sable shared cac
3f850 68 65 20 6d 6f 64 65 20 66 6f 72 20 61 6e 79 20  he mode for any 
3f860 73 75 62 73 65 71 75 65 6e 74 6c 79 0a 2a 2a 20  subsequently.** 
3f870 20 20 20 20 20 20 20 20 20 63 72 65 61 74 65 64           created
3f880 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
3f890 63 74 69 6f 6e 5d 20 69 6e 20 74 68 65 20 73 61  ction] in the sa
3f8a0 6d 65 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a  me process..**.*
3f8b0 2a 20 7b 48 31 30 33 33 36 7d 20 57 68 65 6e 20  * {H10336} When 
3f8c0 73 68 61 72 65 64 20 63 61 63 68 65 20 69 73 20  shared cache is 
3f8d0 65 6e 61 62 6c 65 64 2c 20 74 68 65 20 5b 73 71  enabled, the [sq
3f8e0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
3f8f0 75 6c 65 28 29 5d 0a 2a 2a 20 20 20 20 20 20 20  ule()].**       
3f900 20 20 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c     interface wil
3f910 6c 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20  l always return 
3f920 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20  an error..**.** 
3f930 7b 48 31 30 33 33 37 7d 20 54 68 65 20 5b 73 71  {H10337} The [sq
3f940 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61  lite3_enable_sha
3f950 72 65 64 5f 63 61 63 68 65 28 42 29 5d 20 69 6e  red_cache(B)] in
3f960 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 0a  terface returns.
3f970 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c  **          [SQL
3f980 49 54 45 5f 4f 4b 5d 20 69 66 20 73 68 61 72 65  ITE_OK] if share
3f990 64 20 63 61 63 68 65 20 77 61 73 20 65 6e 61 62  d cache was enab
3f9a0 6c 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64 20  led or disabled 
3f9b0 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 0a 2a 2a  successfully..**
3f9c0 0a 2a 2a 20 7b 48 31 30 33 33 39 7d 20 53 68 61  .** {H10339} Sha
3f9d0 72 65 64 20 63 61 63 68 65 20 69 73 20 64 69 73  red cache is dis
3f9e0 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74  abled by default
3f9f0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
3fa00 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62  int sqlite3_enab
3fa10 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 28  le_shared_cache(
3fa20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  int);../*.** CAP
3fa30 49 33 52 45 46 3a 20 41 74 74 65 6d 70 74 20 54  I3REF: Attempt T
3fa40 6f 20 46 72 65 65 20 48 65 61 70 20 4d 65 6d 6f  o Free Heap Memo
3fa50 72 79 20 7b 48 31 37 33 34 30 7d 20 3c 53 33 30  ry {H17340} <S30
3fa60 32 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  220>.**.** The s
3fa70 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d  qlite3_release_m
3fa80 65 6d 6f 72 79 28 29 20 69 6e 74 65 72 66 61 63  emory() interfac
3fa90 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 66 72  e attempts to fr
3faa0 65 65 20 4e 20 62 79 74 65 73 0a 2a 2a 20 6f 66  ee N bytes.** of
3fab0 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 62 79 20   heap memory by 
3fac0 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 6e 6f 6e  deallocating non
3fad0 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d 6f 72  -essential memor
3fae0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a  y allocations.**
3faf0 20 68 65 6c 64 20 62 79 20 74 68 65 20 64 61 74   held by the dat
3fb00 61 62 61 73 65 20 6c 69 62 72 61 72 79 2e 20 7b  abase library. {
3fb10 45 4e 44 7d 20 20 4d 65 6d 6f 72 79 20 75 73 65  END}  Memory use
3fb20 64 20 74 6f 20 63 61 63 68 65 20 64 61 74 61 62  d to cache datab
3fb30 61 73 65 0a 2a 2a 20 70 61 67 65 73 20 74 6f 20  ase.** pages to 
3fb40 69 6d 70 72 6f 76 65 20 70 65 72 66 6f 72 6d 61  improve performa
3fb50 6e 63 65 20 69 73 20 61 6e 20 65 78 61 6d 70 6c  nce is an exampl
3fb60 65 20 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69  e of non-essenti
3fb70 61 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 73 71  al memory..** sq
3fb80 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65  lite3_release_me
3fb90 6d 6f 72 79 28 29 20 72 65 74 75 72 6e 73 20 74  mory() returns t
3fba0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
3fbb0 65 73 20 61 63 74 75 61 6c 6c 79 20 66 72 65 65  es actually free
3fbc0 64 2c 0a 2a 2a 20 77 68 69 63 68 20 6d 69 67 68  d,.** which migh
3fbd0 74 20 62 65 20 6d 6f 72 65 20 6f 72 20 6c 65 73  t be more or les
3fbe0 73 20 74 68 61 6e 20 74 68 65 20 61 6d 6f 75 6e  s than the amoun
3fbf0 74 20 72 65 71 75 65 73 74 65 64 2e 0a 2a 2a 0a  t requested..**.
3fc00 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a  ** INVARIANTS:.*
3fc10 2a 0a 2a 2a 20 7b 48 31 37 33 34 31 7d 20 54 68  *.** {H17341} Th
3fc20 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65 61  e [sqlite3_relea
3fc30 73 65 5f 6d 65 6d 6f 72 79 28 4e 29 5d 20 69 6e  se_memory(N)] in
3fc40 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73  terface attempts
3fc50 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   to.**          
3fc60 66 72 65 65 20 4e 20 62 79 74 65 73 20 6f 66 20  free N bytes of 
3fc70 68 65 61 70 20 6d 65 6d 6f 72 79 20 62 79 20 64  heap memory by d
3fc80 65 61 6c 6c 6f 63 61 74 69 6e 67 20 6e 6f 6e 2d  eallocating non-
3fc90 65 73 73 65 6e 74 69 61 6c 0a 2a 2a 20 20 20 20  essential.**    
3fca0 20 20 20 20 20 20 6d 65 6d 6f 72 79 20 61 6c 6c        memory all
3fcb0 6f 63 61 74 69 6f 6e 73 20 68 65 6c 64 20 62 79  ocations held by
3fcc0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c 69   the database li
3fcd0 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  brary..**.** {H1
3fce0 36 33 34 32 7d 20 54 68 65 20 5b 73 71 6c 69 74  6342} The [sqlit
3fcf0 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72  e3_release_memor
3fd00 79 28 4e 29 5d 20 72 65 74 75 72 6e 73 20 74 68  y(N)] returns th
3fd10 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 20 20  e number.**     
3fd20 20 20 20 20 20 6f 66 20 62 79 74 65 73 20 61 63       of bytes ac
3fd30 74 75 61 6c 6c 79 20 66 72 65 65 64 2c 20 77 68  tually freed, wh
3fd40 69 63 68 20 6d 69 67 68 74 20 62 65 20 6d 6f 72  ich might be mor
3fd50 65 20 6f 72 20 6c 65 73 73 0a 2a 2a 20 20 20 20  e or less.**    
3fd60 20 20 20 20 20 20 74 68 61 6e 20 74 68 65 20 61        than the a
3fd70 6d 6f 75 6e 74 20 72 65 71 75 65 73 74 65 64 2e  mount requested.
3fd80 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
3fd90 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61  nt sqlite3_relea
3fda0 73 65 5f 6d 65 6d 6f 72 79 28 69 6e 74 29 3b 0a  se_memory(int);.
3fdb0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
3fdc0 20 49 6d 70 6f 73 65 20 41 20 4c 69 6d 69 74 20   Impose A Limit 
3fdd0 4f 6e 20 48 65 61 70 20 53 69 7a 65 20 7b 48 31  On Heap Size {H1
3fde0 37 33 35 30 7d 20 3c 53 33 30 32 32 30 3e 0a 2a  7350} <S30220>.*
3fdf0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
3fe00 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74  _soft_heap_limit
3fe10 28 29 20 69 6e 74 65 72 66 61 63 65 20 70 6c 61  () interface pla
3fe20 63 65 73 20 61 20 22 73 6f 66 74 22 20 6c 69 6d  ces a "soft" lim
3fe30 69 74 0a 2a 2a 20 6f 6e 20 74 68 65 20 61 6d 6f  it.** on the amo
3fe40 75 6e 74 20 6f 66 20 68 65 61 70 20 6d 65 6d 6f  unt of heap memo
3fe50 72 79 20 74 68 61 74 20 6d 61 79 20 62 65 20 61  ry that may be a
3fe60 6c 6c 6f 63 61 74 65 64 20 62 79 20 53 51 4c 69  llocated by SQLi
3fe70 74 65 2e 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 74  te..** If an int
3fe80 65 72 6e 61 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e  ernal allocation
3fe90 20 69 73 20 72 65 71 75 65 73 74 65 64 20 74 68   is requested th
3fea0 61 74 20 77 6f 75 6c 64 20 65 78 63 65 65 64 20  at would exceed 
3feb0 74 68 65 0a 2a 2a 20 73 6f 66 74 20 68 65 61 70  the.** soft heap
3fec0 20 6c 69 6d 69 74 2c 20 5b 73 71 6c 69 74 65 33   limit, [sqlite3
3fed0 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28  _release_memory(
3fee0 29 5d 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e  )] is invoked on
3fef0 65 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20 74 69 6d  e or.** more tim
3ff00 65 73 20 74 6f 20 66 72 65 65 20 75 70 20 73 6f  es to free up so
3ff10 6d 65 20 73 70 61 63 65 20 62 65 66 6f 72 65 20  me space before 
3ff20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  the allocation i
3ff30 73 20 70 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a 0a  s performed..**.
3ff40 2a 2a 20 54 68 65 20 6c 69 6d 69 74 20 69 73 20  ** The limit is 
3ff50 63 61 6c 6c 65 64 20 22 73 6f 66 74 22 2c 20 62  called "soft", b
3ff60 65 63 61 75 73 65 20 69 66 20 5b 73 71 6c 69 74  ecause if [sqlit
3ff70 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72  e3_release_memor
3ff80 79 28 29 5d 0a 2a 2a 20 63 61 6e 6e 6f 74 20 66  y()].** cannot f
3ff90 72 65 65 20 73 75 66 66 69 63 69 65 6e 74 20 6d  ree sufficient m
3ffa0 65 6d 6f 72 79 20 74 6f 20 70 72 65 76 65 6e 74  emory to prevent
3ffb0 20 74 68 65 20 6c 69 6d 69 74 20 66 72 6f 6d 20   the limit from 
3ffc0 62 65 69 6e 67 20 65 78 63 65 65 64 65 64 2c 0a  being exceeded,.
3ffd0 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 73  ** the memory is
3ffe0 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 79 77 61   allocated anywa
3fff0 79 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e  y and the curren
40000 74 20 6f 70 65 72 61 74 69 6f 6e 20 70 72 6f 63  t operation proc
40010 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65  eeds..**.** A ne
40020 67 61 74 69 76 65 20 6f 72 20 7a 65 72 6f 20 76  gative or zero v
40030 61 6c 75 65 20 66 6f 72 20 4e 20 6d 65 61 6e 73  alue for N means
40040 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 6e   that there is n
40050 6f 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69  o soft heap limi
40060 74 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65  t and.** [sqlite
40070 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
40080 28 29 5d 20 77 69 6c 6c 20 6f 6e 6c 79 20 62 65  ()] will only be
40090 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 6d 65 6d   called when mem
400a0 6f 72 79 20 69 73 20 65 78 68 61 75 73 74 65 64  ory is exhausted
400b0 2e 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74  ..** The default
400c0 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 73   value for the s
400d0 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 69  oft heap limit i
400e0 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53 51  s zero..**.** SQ
400f0 4c 69 74 65 20 6d 61 6b 65 73 20 61 20 62 65 73  Lite makes a bes
40100 74 20 65 66 66 6f 72 74 20 74 6f 20 68 6f 6e 6f  t effort to hono
40110 72 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20  r the soft heap 
40120 6c 69 6d 69 74 2e 0a 2a 2a 20 42 75 74 20 69 66  limit..** But if
40130 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c   the soft heap l
40140 69 6d 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 68  imit cannot be h
40150 6f 6e 6f 72 65 64 2c 20 65 78 65 63 75 74 69 6f  onored, executio
40160 6e 20 77 69 6c 6c 0a 2a 2a 20 63 6f 6e 74 69 6e  n will.** contin
40170 75 65 20 77 69 74 68 6f 75 74 20 65 72 72 6f 72  ue without error
40180 20 6f 72 20 6e 6f 74 69 66 69 63 61 74 69 6f 6e   or notification
40190 2e 20 20 54 68 69 73 20 69 73 20 77 68 79 20 74  .  This is why t
401a0 68 65 20 6c 69 6d 69 74 20 69 73 0a 2a 2a 20 63  he limit is.** c
401b0 61 6c 6c 65 64 20 61 20 22 73 6f 66 74 22 20 6c  alled a "soft" l
401c0 69 6d 69 74 2e 20 20 49 74 20 69 73 20 61 64 76  imit.  It is adv
401d0 69 73 6f 72 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a  isory only..**.*
401e0 2a 20 50 72 69 6f 72 20 74 6f 20 53 51 4c 69 74  * Prior to SQLit
401f0 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 2c  e version 3.5.0,
40200 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e   this routine on
40210 6c 79 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 74  ly constrained t
40220 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c  he memory.** all
40230 6f 63 61 74 65 64 20 62 79 20 61 20 73 69 6e 67  ocated by a sing
40240 6c 65 20 74 68 72 65 61 64 20 2d 20 74 68 65 20  le thread - the 
40250 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20 77  same thread in w
40260 68 69 63 68 20 74 68 69 73 20 72 6f 75 74 69 6e  hich this routin
40270 65 0a 2a 2a 20 72 75 6e 73 2e 20 20 42 65 67 69  e.** runs.  Begi
40280 6e 6e 69 6e 67 20 77 69 74 68 20 53 51 4c 69 74  nning with SQLit
40290 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 2c  e version 3.5.0,
402a0 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c   the soft heap l
402b0 69 6d 69 74 20 69 73 0a 2a 2a 20 61 70 70 6c 69  imit is.** appli
402c0 65 64 20 74 6f 20 61 6c 6c 20 74 68 72 65 61 64  ed to all thread
402d0 73 2e 20 54 68 65 20 76 61 6c 75 65 20 73 70 65  s. The value spe
402e0 63 69 66 69 65 64 20 66 6f 72 20 74 68 65 20 73  cified for the s
402f0 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 0a 2a  oft heap limit.*
40300 2a 20 69 73 20 61 6e 20 75 70 70 65 72 20 62 6f  * is an upper bo
40310 75 6e 64 20 6f 6e 20 74 68 65 20 74 6f 74 61 6c  und on the total
40320 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
40330 6f 6e 20 66 6f 72 20 61 6c 6c 20 74 68 72 65 61  on for all threa
40340 64 73 2e 20 49 6e 0a 2a 2a 20 76 65 72 73 69 6f  ds. In.** versio
40350 6e 20 33 2e 35 2e 30 20 74 68 65 72 65 20 69 73  n 3.5.0 there is
40360 20 6e 6f 20 6d 65 63 68 61 6e 69 73 6d 20 66 6f   no mechanism fo
40370 72 20 6c 69 6d 69 74 69 6e 67 20 74 68 65 20 68  r limiting the h
40380 65 61 70 20 75 73 61 67 65 20 66 6f 72 0a 2a 2a  eap usage for.**
40390 20 69 6e 64 69 76 69 64 75 61 6c 20 74 68 72 65   individual thre
403a0 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52  ads..**.** INVAR
403b0 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31  IANTS:.**.** {H1
403c0 36 33 35 31 7d 20 54 68 65 20 5b 73 71 6c 69 74  6351} The [sqlit
403d0 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d  e3_soft_heap_lim
403e0 69 74 28 4e 29 5d 20 69 6e 74 65 72 66 61 63 65  it(N)] interface
403f0 20 70 6c 61 63 65 73 20 61 20 73 6f 66 74 20 6c   places a soft l
40400 69 6d 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  imit.**         
40410 20 6f 66 20 4e 20 62 79 74 65 73 20 6f 6e 20 74   of N bytes on t
40420 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 68 65 61  he amount of hea
40430 70 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 6d 61  p memory that ma
40440 79 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a  y be allocated.*
40450 2a 20 20 20 20 20 20 20 20 20 20 75 73 69 6e 67  *          using
40460 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
40470 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ()] or [sqlite3_
40480 72 65 61 6c 6c 6f 63 28 29 5d 20 61 74 20 61 6e  realloc()] at an
40490 79 20 70 6f 69 6e 74 0a 2a 2a 20 20 20 20 20 20  y point.**      
404a0 20 20 20 20 69 6e 20 74 69 6d 65 2e 0a 2a 2a 0a      in time..**.
404b0 2a 2a 20 7b 48 31 36 33 35 32 7d 20 49 66 20 61  ** {H16352} If a
404c0 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
404d0 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b  3_malloc()] or [
404e0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
404f0 29 5d 20 77 6f 75 6c 64 0a 2a 2a 20 20 20 20 20  )] would.**     
40500 20 20 20 20 20 63 61 75 73 65 20 74 68 65 20 74       cause the t
40510 6f 74 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 61  otal amount of a
40520 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 20  llocated memory 
40530 74 6f 20 65 78 63 65 65 64 20 74 68 65 0a 2a 2a  to exceed the.**
40540 20 20 20 20 20 20 20 20 20 20 73 6f 66 74 20 68            soft h
40550 65 61 70 20 6c 69 6d 69 74 2c 20 74 68 65 6e 20  eap limit, then 
40560 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65  [sqlite3_release
40570 5f 6d 65 6d 6f 72 79 28 29 5d 20 69 73 20 69 6e  _memory()] is in
40580 76 6f 6b 65 64 0a 2a 2a 20 20 20 20 20 20 20 20  voked.**        
40590 20 20 69 6e 20 61 6e 20 61 74 74 65 6d 70 74 20    in an attempt 
405a0 74 6f 20 72 65 64 75 63 65 20 74 68 65 20 6d 65  to reduce the me
405b0 6d 6f 72 79 20 75 73 61 67 65 20 70 72 69 6f 72  mory usage prior
405c0 20 74 6f 20 70 72 6f 63 65 65 64 69 6e 67 0a 2a   to proceeding.*
405d0 2a 20 20 20 20 20 20 20 20 20 20 77 69 74 68 20  *          with 
405e0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
405f0 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 2e 0a 2a  ation attempt..*
40600 2a 0a 2a 2a 20 7b 48 31 36 33 35 33 7d 20 43 61  *.** {H16353} Ca
40610 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  lls to [sqlite3_
40620 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 71  malloc()] or [sq
40630 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d  lite3_realloc()]
40640 20 74 68 61 74 20 74 72 69 67 67 65 72 0a 2a 2a   that trigger.**
40650 20 20 20 20 20 20 20 20 20 20 61 74 74 65 6d 70            attemp
40660 74 73 20 74 6f 20 72 65 64 75 63 65 20 6d 65 6d  ts to reduce mem
40670 6f 72 79 20 75 73 61 67 65 20 74 68 72 6f 75 67  ory usage throug
40680 68 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20  h the soft heap 
40690 6c 69 6d 69 74 0a 2a 2a 20 20 20 20 20 20 20 20  limit.**        
406a0 20 20 6d 65 63 68 61 6e 69 73 6d 20 63 6f 6e 74    mechanism cont
406b0 69 6e 75 65 20 65 76 65 6e 20 69 66 20 74 68 65  inue even if the
406c0 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 64 75   attempt to redu
406d0 63 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 20  ce memory.**    
406e0 20 20 20 20 20 20 75 73 61 67 65 20 69 73 20 75        usage is u
406f0 6e 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2a 0a  nsuccessful..**.
40700 2a 2a 20 7b 48 31 36 33 35 34 7d 20 41 20 6e 65  ** {H16354} A ne
40710 67 61 74 69 76 65 20 6f 72 20 7a 65 72 6f 20 76  gative or zero v
40720 61 6c 75 65 20 66 6f 72 20 4e 20 69 6e 20 61 20  alue for N in a 
40730 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 20  call to.**      
40740 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 73 6f 66      [sqlite3_sof
40750 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 4e 29 5d  t_heap_limit(N)]
40760 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 72   means that ther
40770 65 20 69 73 20 6e 6f 20 73 6f 66 74 0a 2a 2a 20  e is no soft.** 
40780 20 20 20 20 20 20 20 20 20 68 65 61 70 20 6c 69           heap li
40790 6d 69 74 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  mit and [sqlite3
407a0 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28  _release_memory(
407b0 29 5d 20 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 0a  )] will only be.
407c0 2a 2a 20 20 20 20 20 20 20 20 20 20 63 61 6c 6c  **          call
407d0 65 64 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20 69  ed when memory i
407e0 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 78 68  s completely exh
407f0 61 75 73 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48  austed..**.** {H
40800 31 36 33 35 35 7d 20 54 68 65 20 64 65 66 61 75  16355} The defau
40810 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  lt value for the
40820 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74   soft heap limit
40830 20 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20   is zero..**.** 
40840 7b 48 31 36 33 35 38 7d 20 45 61 63 68 20 63 61  {H16358} Each ca
40850 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73  ll to [sqlite3_s
40860 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 4e  oft_heap_limit(N
40870 29 5d 20 6f 76 65 72 72 69 64 65 73 20 74 68 65  )] overrides the
40880 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 76 61 6c  .**          val
40890 75 65 73 20 73 65 74 20 62 79 20 61 6c 6c 20 70  ues set by all p
408a0 72 69 6f 72 20 63 61 6c 6c 73 2e 0a 2a 2f 0a 53  rior calls..*/.S
408b0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
408c0 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70  qlite3_soft_heap
408d0 5f 6c 69 6d 69 74 28 69 6e 74 29 3b 0a 0a 2f 2a  _limit(int);../*
408e0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 78  .** CAPI3REF: Ex
408f0 74 72 61 63 74 20 4d 65 74 61 64 61 74 61 20 41  tract Metadata A
40900 62 6f 75 74 20 41 20 43 6f 6c 75 6d 6e 20 4f 66  bout A Column Of
40910 20 41 20 54 61 62 6c 65 20 7b 48 31 32 38 35 30   A Table {H12850
40920 7d 20 3c 53 36 30 33 30 30 3e 0a 2a 2a 0a 2a 2a  } <S60300>.**.**
40930 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
40940 74 75 72 6e 73 20 6d 65 74 61 64 61 74 61 20 61  turns metadata a
40950 62 6f 75 74 20 61 20 73 70 65 63 69 66 69 63 20  bout a specific 
40960 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 73 70 65 63  column of a spec
40970 69 66 69 63 0a 2a 2a 20 64 61 74 61 62 61 73 65  ific.** database
40980 20 74 61 62 6c 65 20 61 63 63 65 73 73 69 62 6c   table accessibl
40990 65 20 75 73 69 6e 67 20 74 68 65 20 5b 64 61 74  e using the [dat
409a0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
409b0 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 70 61 73 73  ] handle.** pass
409c0 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ed as the first 
409d0 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e  function argumen
409e0 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c  t..**.** The col
409f0 75 6d 6e 20 69 73 20 69 64 65 6e 74 69 66 69 65  umn is identifie
40a00 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 2c  d by the second,
40a10 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74   third and fourt
40a20 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 0a  h parameters to.
40a30 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** this function
40a40 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72  . The second par
40a50 61 6d 65 74 65 72 20 69 73 20 65 69 74 68 65 72  ameter is either
40a60 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
40a70 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 28 69 2e   database.** (i.
40a80 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65 6d 70  e. "main", "temp
40a90 22 20 6f 72 20 61 6e 20 61 74 74 61 63 68 65 64  " or an attached
40aa0 20 64 61 74 61 62 61 73 65 29 20 63 6f 6e 74 61   database) conta
40ab0 69 6e 69 6e 67 20 74 68 65 20 73 70 65 63 69 66  ining the specif
40ac0 69 65 64 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20  ied.** table or 
40ad0 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 73 20 4e  NULL. If it is N
40ae0 55 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c 20 61 74  ULL, then all at
40af0 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73  tached databases
40b00 20 61 72 65 20 73 65 61 72 63 68 65 64 0a 2a 2a   are searched.**
40b10 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 75   for the table u
40b20 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 61 6c  sing the same al
40b30 67 6f 72 69 74 68 6d 20 75 73 65 64 20 62 79 20  gorithm used by 
40b40 74 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 67  the database eng
40b50 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 73 6f 6c 76  ine to.** resolv
40b60 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74 61  e unqualified ta
40b70 62 6c 65 20 72 65 66 65 72 65 6e 63 65 73 2e 0a  ble references..
40b80 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20  **.** The third 
40b90 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61 6d  and fourth param
40ba0 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66 75  eters to this fu
40bb0 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20 74  nction are the t
40bc0 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a  able and column.
40bd0 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64  ** name of the d
40be0 65 73 69 72 65 64 20 63 6f 6c 75 6d 6e 2c 20 72  esired column, r
40bf0 65 73 70 65 63 74 69 76 65 6c 79 2e 20 4e 65 69  espectively. Nei
40c00 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 70 61  ther of these pa
40c10 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6d 61 79 20  rameters.** may 
40c20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 4d  be NULL..**.** M
40c30 65 74 61 64 61 74 61 20 69 73 20 72 65 74 75 72  etadata is retur
40c40 6e 65 64 20 62 79 20 77 72 69 74 69 6e 67 20 74  ned by writing t
40c50 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63  o the memory loc
40c60 61 74 69 6f 6e 73 20 70 61 73 73 65 64 20 61 73  ations passed as
40c70 20 74 68 65 20 35 74 68 0a 2a 2a 20 61 6e 64 20   the 5th.** and 
40c80 73 75 62 73 65 71 75 65 6e 74 20 70 61 72 61 6d  subsequent param
40c90 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66 75  eters to this fu
40ca0 6e 63 74 69 6f 6e 2e 20 41 6e 79 20 6f 66 20 74  nction. Any of t
40cb0 68 65 73 65 20 61 72 67 75 6d 65 6e 74 73 20 6d  hese arguments m
40cc0 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2c 20 69  ay be.** NULL, i
40cd0 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65  n which case the
40ce0 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 65   corresponding e
40cf0 6c 65 6d 65 6e 74 20 6f 66 20 6d 65 74 61 64 61  lement of metada
40d00 74 61 20 69 73 20 6f 6d 69 74 74 65 64 2e 0a 2a  ta is omitted..*
40d10 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65  *.** <blockquote
40d20 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72 64  >.** <table bord
40d30 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74 72 3e 3c  er="1">.** <tr><
40d40 74 68 3e 20 50 61 72 61 6d 65 74 65 72 20 3c 74  th> Parameter <t
40d50 68 3e 20 4f 75 74 70 75 74 3c 62 72 3e 54 79 70  h> Output<br>Typ
40d60 65 20 3c 74 68 3e 20 20 44 65 73 63 72 69 70 74  e <th>  Descript
40d70 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c 74  ion.**.** <tr><t
40d80 64 3e 20 35 74 68 20 3c 74 64 3e 20 63 6f 6e 73  d> 5th <td> cons
40d90 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 44 61 74  t char* <td> Dat
40da0 61 20 74 79 70 65 0a 2a 2a 20 3c 74 72 3e 3c 74  a type.** <tr><t
40db0 64 3e 20 36 74 68 20 3c 74 64 3e 20 63 6f 6e 73  d> 6th <td> cons
40dc0 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 4e 61 6d  t char* <td> Nam
40dd0 65 20 6f 66 20 64 65 66 61 75 6c 74 20 63 6f 6c  e of default col
40de0 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 0a  lation sequence.
40df0 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 37 74 68 20  ** <tr><td> 7th 
40e00 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20  <td> int        
40e10 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f   <td> True if co
40e20 6c 75 6d 6e 20 68 61 73 20 61 20 4e 4f 54 20 4e  lumn has a NOT N
40e30 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a  ULL constraint.*
40e40 2a 20 3c 74 72 3e 3c 74 64 3e 20 38 74 68 20 3c  * <tr><td> 8th <
40e50 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20 20  td> int         
40e60 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f 6c  <td> True if col
40e70 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66 20 74  umn is part of t
40e80 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 0a 2a  he PRIMARY KEY.*
40e90 2a 20 3c 74 72 3e 3c 74 64 3e 20 39 74 68 20 3c  * <tr><td> 9th <
40ea0 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20 20  td> int         
40eb0 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f 6c  <td> True if col
40ec0 75 6d 6e 20 69 73 20 41 55 54 4f 49 4e 43 52 45  umn is AUTOINCRE
40ed0 4d 45 4e 54 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e  MENT.** </table>
40ee0 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65  .** </blockquote
40ef0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f  >.**.** The memo
40f00 72 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ry pointed to by
40f10 20 74 68 65 20 63 68 61 72 61 63 74 65 72 20 70   the character p
40f20 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 64  ointers returned
40f30 20 66 6f 72 20 74 68 65 0a 2a 2a 20 64 65 63 6c   for the.** decl
40f40 61 72 61 74 69 6f 6e 20 74 79 70 65 20 61 6e 64  aration type and
40f50 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
40f60 6e 63 65 20 69 73 20 76 61 6c 69 64 20 6f 6e 6c  nce is valid onl
40f70 79 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74  y until the next
40f80 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 61 6e 79 20  .** call to any 
40f90 53 51 4c 69 74 65 20 41 50 49 20 66 75 6e 63 74  SQLite API funct
40fa0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ion..**.** If th
40fb0 65 20 73 70 65 63 69 66 69 65 64 20 74 61 62 6c  e specified tabl
40fc0 65 20 69 73 20 61 63 74 75 61 6c 6c 79 20 61 20  e is actually a 
40fd0 76 69 65 77 2c 20 61 6e 20 5b 65 72 72 6f 72 20  view, an [error 
40fe0 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65  code] is returne
40ff0 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
41000 73 70 65 63 69 66 69 65 64 20 63 6f 6c 75 6d 6e  specified column
41010 20 69 73 20 22 72 6f 77 69 64 22 2c 20 22 6f 69   is "rowid", "oi
41020 64 22 20 6f 72 20 22 5f 72 6f 77 69 64 5f 22 20  d" or "_rowid_" 
41030 61 6e 64 20 61 6e 0a 2a 2a 20 49 4e 54 45 47 45  and an.** INTEGE
41040 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f  R PRIMARY KEY co
41050 6c 75 6d 6e 20 68 61 73 20 62 65 65 6e 20 65 78  lumn has been ex
41060 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65  plicitly declare
41070 64 2c 20 74 68 65 6e 20 74 68 65 20 6f 75 74 70  d, then the outp
41080 75 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73  ut.** parameters
41090 20 61 72 65 20 73 65 74 20 66 6f 72 20 74 68 65   are set for the
410a0 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c   explicitly decl
410b0 61 72 65 64 20 63 6f 6c 75 6d 6e 2e 20 49 66 20  ared column. If 
410c0 74 68 65 72 65 20 69 73 20 6e 6f 0a 2a 2a 20 65  there is no.** e
410d0 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72  xplicitly declar
410e0 65 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  ed INTEGER PRIMA
410f0 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 2c 20 74  RY KEY column, t
41100 68 65 6e 20 74 68 65 20 6f 75 74 70 75 74 0a 2a  hen the output.*
41110 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65  * parameters are
41120 20 73 65 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a   set as follows:
41130 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 3e 0a 2a 2a 20  .**.** <pre>.** 
41140 20 20 20 20 64 61 74 61 20 74 79 70 65 3a 20 22      data type: "
41150 49 4e 54 45 47 45 52 22 0a 2a 2a 20 20 20 20 20  INTEGER".**     
41160 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
41170 63 65 3a 20 22 42 49 4e 41 52 59 22 0a 2a 2a 20  ce: "BINARY".** 
41180 20 20 20 20 6e 6f 74 20 6e 75 6c 6c 3a 20 30 0a      not null: 0.
41190 2a 2a 20 20 20 20 20 70 72 69 6d 61 72 79 20 6b  **     primary k
411a0 65 79 3a 20 31 0a 2a 2a 20 20 20 20 20 61 75 74  ey: 1.**     aut
411b0 6f 20 69 6e 63 72 65 6d 65 6e 74 3a 20 30 0a 2a  o increment: 0.*
411c0 2a 20 3c 2f 70 72 65 3e 0a 2a 2a 0a 2a 2a 20 54  * </pre>.**.** T
411d0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79  his function may
411e0 20 6c 6f 61 64 20 6f 6e 65 20 6f 72 20 6d 6f 72   load one or mor
411f0 65 20 73 63 68 65 6d 61 73 20 66 72 6f 6d 20 64  e schemas from d
41200 61 74 61 62 61 73 65 20 66 69 6c 65 73 2e 20 49  atabase files. I
41210 66 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 6f 63  f an.** error oc
41220 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 69 73  curs during this
41230 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 69 66 20   process, or if 
41240 74 68 65 20 72 65 71 75 65 73 74 65 64 20 74 61  the requested ta
41250 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 0a 2a 2a  ble or column.**
41260 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64   cannot be found
41270 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65  , an [error code
41280 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  ] is returned an
41290 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  d an error messa
412a0 67 65 20 6c 65 66 74 0a 2a 2a 20 69 6e 20 74 68  ge left.** in th
412b0 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
412c0 65 63 74 69 6f 6e 5d 20 28 74 6f 20 62 65 20 72  ection] (to be r
412d0 65 74 72 69 65 76 65 64 20 75 73 69 6e 67 20 73  etrieved using s
412e0 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 29  qlite3_errmsg())
412f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49  ..**.** This API
41300 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62   is only availab
41310 6c 65 20 69 66 20 74 68 65 20 6c 69 62 72 61 72  le if the librar
41320 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77  y was compiled w
41330 69 74 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49  ith the.** [SQLI
41340 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e  TE_ENABLE_COLUMN
41350 5f 4d 45 54 41 44 41 54 41 5d 20 43 2d 70 72 65  _METADATA] C-pre
41360 70 72 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f 6c  processor symbol
41370 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c   defined..*/.SQL
41380 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
41390 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e  te3_table_column
413a0 5f 6d 65 74 61 64 61 74 61 28 0a 20 20 73 71 6c  _metadata(.  sql
413b0 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
413c0 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e           /* Conn
413d0 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 2a 2f  ection handle */
413e0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
413f0 44 62 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 2f  DbName,        /
41400 2a 20 44 61 74 61 62 61 73 65 20 6e 61 6d 65 20  * Database name 
41410 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e  or NULL */.  con
41420 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 4e  st char *zTableN
41430 61 6d 65 2c 20 20 20 20 20 2f 2a 20 54 61 62 6c  ame,     /* Tabl
41440 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73  e name */.  cons
41450 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 4e  t char *zColumnN
41460 61 6d 65 2c 20 20 20 20 2f 2a 20 43 6f 6c 75 6d  ame,    /* Colum
41470 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 61 72  n name */.  char
41480 20 63 6f 6e 73 74 20 2a 2a 70 7a 44 61 74 61 54   const **pzDataT
41490 79 70 65 2c 20 20 20 20 2f 2a 20 4f 55 54 50 55  ype,    /* OUTPU
414a0 54 3a 20 44 65 63 6c 61 72 65 64 20 64 61 74 61  T: Declared data
414b0 20 74 79 70 65 20 2a 2f 0a 20 20 63 68 61 72 20   type */.  char 
414c0 63 6f 6e 73 74 20 2a 2a 70 7a 43 6f 6c 6c 53 65  const **pzCollSe
414d0 71 2c 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54  q,     /* OUTPUT
414e0 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  : Collation sequ
414f0 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 69  ence name */.  i
41500 6e 74 20 2a 70 4e 6f 74 4e 75 6c 6c 2c 20 20 20  nt *pNotNull,   
41510 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
41520 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 4e 4f  TPUT: True if NO
41530 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e  T NULL constrain
41540 74 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 69 6e  t exists */.  in
41550 74 20 2a 70 50 72 69 6d 61 72 79 4b 65 79 2c 20  t *pPrimaryKey, 
41560 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
41570 50 55 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c  PUT: True if col
41580 75 6d 6e 20 70 61 72 74 20 6f 66 20 50 4b 20 2a  umn part of PK *
41590 2f 0a 20 20 69 6e 74 20 2a 70 41 75 74 6f 69 6e  /.  int *pAutoin
415a0 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c               
415b0 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65 20  /* OUTPUT: True 
415c0 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 61 75 74  if column is aut
415d0 6f 2d 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a 29  o-increment */.)
415e0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
415f0 46 3a 20 4c 6f 61 64 20 41 6e 20 45 78 74 65 6e  F: Load An Exten
41600 73 69 6f 6e 20 7b 48 31 32 36 30 30 7d 20 3c 53  sion {H12600} <S
41610 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69  20500>.**.** Thi
41620 73 20 69 6e 74 65 72 66 61 63 65 20 6c 6f 61 64  s interface load
41630 73 20 61 6e 20 53 51 4c 69 74 65 20 65 78 74 65  s an SQLite exte
41640 6e 73 69 6f 6e 20 6c 69 62 72 61 72 79 20 66 72  nsion library fr
41650 6f 6d 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c  om the named fil
41660 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 31  e..**.** {H12601
41670 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 6f  } The sqlite3_lo
41680 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 69  ad_extension() i
41690 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 74  nterface attempt
416a0 73 20 74 6f 20 6c 6f 61 64 20 61 6e 0a 2a 2a 20  s to load an.** 
416b0 20 20 20 20 20 20 20 20 20 53 51 4c 69 74 65 20           SQLite 
416c0 65 78 74 65 6e 73 69 6f 6e 20 6c 69 62 72 61 72  extension librar
416d0 79 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74  y contained in t
416e0 68 65 20 66 69 6c 65 20 7a 46 69 6c 65 2e 0a 2a  he file zFile..*
416f0 2a 0a 2a 2a 20 7b 48 31 32 36 30 32 7d 20 54 68  *.** {H12602} Th
41700 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 69 73  e entry point is
41710 20 7a 50 72 6f 63 2e 0a 2a 2a 0a 2a 2a 20 7b 48   zProc..**.** {H
41720 31 32 36 30 33 7d 20 7a 50 72 6f 63 20 6d 61 79  12603} zProc may
41730 20 62 65 20 30 2c 20 69 6e 20 77 68 69 63 68 20   be 0, in which 
41740 63 61 73 65 20 74 68 65 20 6e 61 6d 65 20 6f 66  case the name of
41750 20 74 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74   the entry point
41760 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 64 65 66  .**          def
41770 61 75 6c 74 73 20 74 6f 20 22 73 71 6c 69 74 65  aults to "sqlite
41780 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74  3_extension_init
41790 22 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 34  "..**.** {H12604
417a0 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 6f  } The sqlite3_lo
417b0 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 69  ad_extension() i
417c0 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 72  nterface shall r
417d0 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20  eturn.**        
417e0 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e    [SQLITE_OK] on
417f0 20 73 75 63 63 65 73 73 20 61 6e 64 20 5b 53 51   success and [SQ
41800 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 66 20 73  LITE_ERROR] if s
41810 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72  omething goes wr
41820 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36  ong..**.** {H126
41830 30 35 7d 20 49 66 20 61 6e 20 65 72 72 6f 72 20  05} If an error 
41840 6f 63 63 75 72 73 20 61 6e 64 20 70 7a 45 72 72  occurs and pzErr
41850 4d 73 67 20 69 73 20 6e 6f 74 20 30 2c 20 74 68  Msg is not 0, th
41860 65 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  en the.**       
41870 20 20 20 5b 73 71 6c 69 74 65 33 5f 6c 6f 61 64     [sqlite3_load
41880 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 69 6e  _extension()] in
41890 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 61 74  terface shall at
418a0 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 20 20  tempt to.**     
418b0 20 20 20 20 20 66 69 6c 6c 20 2a 70 7a 45 72 72       fill *pzErr
418c0 4d 73 67 20 77 69 74 68 20 65 72 72 6f 72 20 6d  Msg with error m
418d0 65 73 73 61 67 65 20 74 65 78 74 20 73 74 6f 72  essage text stor
418e0 65 64 20 69 6e 20 6d 65 6d 6f 72 79 0a 2a 2a 20  ed in memory.** 
418f0 20 20 20 20 20 20 20 20 20 6f 62 74 61 69 6e 65           obtaine
41900 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f  d from [sqlite3_
41910 6d 61 6c 6c 6f 63 28 29 5d 2e 20 7b 45 4e 44 7d  malloc()]. {END}
41920 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75    The calling fu
41930 6e 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20  nction.**       
41940 20 20 20 73 68 6f 75 6c 64 20 66 72 65 65 20 74     should free t
41950 68 69 73 20 6d 65 6d 6f 72 79 20 62 79 20 63 61  his memory by ca
41960 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66  lling [sqlite3_f
41970 72 65 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48  ree()]..**.** {H
41980 31 32 36 30 36 7d 20 45 78 74 65 6e 73 69 6f 6e  12606} Extension
41990 20 6c 6f 61 64 69 6e 67 20 6d 75 73 74 20 62 65   loading must be
419a0 20 65 6e 61 62 6c 65 64 20 75 73 69 6e 67 0a 2a   enabled using.*
419b0 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
419c0 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f  te3_enable_load_
419d0 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 70 72 69  extension()] pri
419e0 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 68  or to calling th
419f0 69 73 20 41 50 49 2c 0a 2a 2a 20 20 20 20 20 20  is API,.**      
41a00 20 20 20 20 6f 74 68 65 72 77 69 73 65 20 61 6e      otherwise an
41a10 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20 72   error will be r
41a20 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  eturned..*/.SQLI
41a30 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
41a40 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f  e3_load_extensio
41a50 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  n(.  sqlite3 *db
41a60 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f  ,          /* Lo
41a70 61 64 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e  ad the extension
41a80 20 69 6e 74 6f 20 74 68 69 73 20 64 61 74 61 62   into this datab
41a90 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
41aa0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
41ab0 7a 46 69 6c 65 2c 20 20 20 20 2f 2a 20 4e 61 6d  zFile,    /* Nam
41ac0 65 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 20  e of the shared 
41ad0 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 69  library containi
41ae0 6e 67 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a  ng extension */.
41af0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50    const char *zP
41b00 72 6f 63 2c 20 20 20 20 2f 2a 20 45 6e 74 72 79  roc,    /* Entry
41b10 20 70 6f 69 6e 74 2e 20 20 44 65 72 69 76 65 64   point.  Derived
41b20 20 66 72 6f 6d 20 7a 46 69 6c 65 20 69 66 20 30   from zFile if 0
41b30 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45   */.  char **pzE
41b40 72 72 4d 73 67 20 20 20 20 20 20 20 2f 2a 20 50  rrMsg       /* P
41b50 75 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  ut error message
41b60 20 68 65 72 65 20 69 66 20 6e 6f 74 20 30 20 2a   here if not 0 *
41b70 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  /.);../*.** CAPI
41b80 33 52 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72 20  3REF: Enable Or 
41b90 44 69 73 61 62 6c 65 20 45 78 74 65 6e 73 69 6f  Disable Extensio
41ba0 6e 20 4c 6f 61 64 69 6e 67 20 7b 48 31 32 36 32  n Loading {H1262
41bb0 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a  0} <S20500>.**.*
41bc0 2a 20 53 6f 20 61 73 20 6e 6f 74 20 74 6f 20 6f  * So as not to o
41bd0 70 65 6e 20 73 65 63 75 72 69 74 79 20 68 6f 6c  pen security hol
41be0 65 73 20 69 6e 20 6f 6c 64 65 72 20 61 70 70 6c  es in older appl
41bf0 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 61 72  ications that ar
41c00 65 0a 2a 2a 20 75 6e 70 72 65 70 61 72 65 64 20  e.** unprepared 
41c10 74 6f 20 64 65 61 6c 20 77 69 74 68 20 65 78 74  to deal with ext
41c20 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 2c 20  ension loading, 
41c30 61 6e 64 20 61 73 20 61 20 6d 65 61 6e 73 20 6f  and as a means o
41c40 66 20 64 69 73 61 62 6c 69 6e 67 0a 2a 2a 20 65  f disabling.** e
41c50 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67  xtension loading
41c60 20 77 68 69 6c 65 20 65 76 61 6c 75 61 74 69 6e   while evaluatin
41c70 67 20 75 73 65 72 2d 65 6e 74 65 72 65 64 20 53  g user-entered S
41c80 51 4c 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  QL, the followin
41c90 67 20 41 50 49 0a 2a 2a 20 69 73 20 70 72 6f 76  g API.** is prov
41ca0 69 64 65 64 20 74 6f 20 74 75 72 6e 20 74 68 65  ided to turn the
41cb0 20 5b 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65   [sqlite3_load_e
41cc0 78 74 65 6e 73 69 6f 6e 28 29 5d 20 6d 65 63 68  xtension()] mech
41cd0 61 6e 69 73 6d 20 6f 6e 20 61 6e 64 20 6f 66 66  anism on and off
41ce0 2e 0a 2a 2a 0a 2a 2a 20 45 78 74 65 6e 73 69 6f  ..**.** Extensio
41cf0 6e 20 6c 6f 61 64 69 6e 67 20 69 73 20 6f 66 66  n loading is off
41d00 20 62 79 20 64 65 66 61 75 6c 74 2e 20 53 65 65   by default. See
41d10 20 74 69 63 6b 65 74 20 23 31 38 36 33 2e 0a 2a   ticket #1863..*
41d20 2a 0a 2a 2a 20 7b 48 31 32 36 32 31 7d 20 43 61  *.** {H12621} Ca
41d30 6c 6c 20 74 68 65 20 73 71 6c 69 74 65 33 5f 65  ll the sqlite3_e
41d40 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e  nable_load_exten
41d50 73 69 6f 6e 28 29 20 72 6f 75 74 69 6e 65 20 77  sion() routine w
41d60 69 74 68 20 6f 6e 6f 66 66 3d 3d 31 0a 2a 2a 20  ith onoff==1.** 
41d70 20 20 20 20 20 20 20 20 20 74 6f 20 74 75 72 6e           to turn
41d80 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69   extension loadi
41d90 6e 67 20 6f 6e 20 61 6e 64 20 63 61 6c 6c 20 69  ng on and call i
41da0 74 20 77 69 74 68 20 6f 6e 6f 66 66 3d 3d 30 20  t with onoff==0 
41db0 74 6f 20 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20  to turn.**      
41dc0 20 20 20 20 69 74 20 62 61 63 6b 20 6f 66 66 20      it back off 
41dd0 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  again..**.** {H1
41de0 32 36 32 32 7d 20 45 78 74 65 6e 73 69 6f 6e 20  2622} Extension 
41df0 6c 6f 61 64 69 6e 67 20 69 73 20 6f 66 66 20 62  loading is off b
41e00 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2f 0a 53 51  y default..*/.SQ
41e10 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
41e20 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64  ite3_enable_load
41e30 5f 65 78 74 65 6e 73 69 6f 6e 28 73 71 6c 69 74  _extension(sqlit
41e40 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f 6e 6f 66  e3 *db, int onof
41e50 66 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  f);../*.** CAPI3
41e60 52 45 46 3a 20 41 75 74 6f 6d 61 74 69 63 61 6c  REF: Automatical
41e70 6c 79 20 4c 6f 61 64 20 41 6e 20 45 78 74 65 6e  ly Load An Exten
41e80 73 69 6f 6e 73 20 7b 48 31 32 36 34 30 7d 20 3c  sions {H12640} <
41e90 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S20500>.**.** Th
41ea0 69 73 20 41 50 49 20 63 61 6e 20 62 65 20 69 6e  is API can be in
41eb0 76 6f 6b 65 64 20 61 74 20 70 72 6f 67 72 61 6d  voked at program
41ec0 20 73 74 61 72 74 75 70 20 69 6e 20 6f 72 64 65   startup in orde
41ed0 72 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a 2a  r to register.**
41ee0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 74 61   one or more sta
41ef0 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 65 64 20 65  tically linked e
41f00 78 74 65 6e 73 69 6f 6e 73 20 74 68 61 74 20 77  xtensions that w
41f10 69 6c 6c 20 62 65 20 61 76 61 69 6c 61 62 6c 65  ill be available
41f20 0a 2a 2a 20 74 6f 20 61 6c 6c 20 6e 65 77 20 5b  .** to all new [
41f30 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
41f40 69 6f 6e 73 5d 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a  ions]. {END}.**.
41f50 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
41f60 73 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72  stores a pointer
41f70 20 74 6f 20 74 68 65 20 65 78 74 65 6e 73 69 6f   to the extensio
41f80 6e 20 69 6e 20 61 6e 20 61 72 72 61 79 20 74 68  n in an array th
41f90 61 74 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e 65  at is.** obtaine
41fa0 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f  d from [sqlite3_
41fb0 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 49 66 20 79  malloc()].  If y
41fc0 6f 75 20 72 75 6e 20 61 20 6d 65 6d 6f 72 79 20  ou run a memory 
41fd0 6c 65 61 6b 20 63 68 65 63 6b 65 72 0a 2a 2a 20  leak checker.** 
41fe0 6f 6e 20 79 6f 75 72 20 70 72 6f 67 72 61 6d 20  on your program 
41ff0 61 6e 64 20 69 74 20 72 65 70 6f 72 74 73 20 61  and it reports a
42000 20 6c 65 61 6b 20 62 65 63 61 75 73 65 20 6f 66   leak because of
42010 20 74 68 69 73 20 61 72 72 61 79 2c 20 69 6e 76   this array, inv
42020 6f 6b 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  oke.** [sqlite3_
42030 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74 65 6e  reset_auto_exten
42040 73 69 6f 6e 28 29 5d 20 70 72 69 6f 72 20 74 6f  sion()] prior to
42050 20 73 68 75 74 64 6f 77 6e 20 74 6f 20 66 72 65   shutdown to fre
42060 65 20 74 68 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2a  e the memory..**
42070 0a 2a 2a 20 7b 48 31 32 36 34 31 7d 20 54 68 69  .** {H12641} Thi
42080 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73  s function regis
42090 74 65 72 73 20 61 6e 20 65 78 74 65 6e 73 69 6f  ters an extensio
420a0 6e 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 74 68  n entry point th
420b0 61 74 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20  at is.**        
420c0 20 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20    automatically 
420d0 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72  invoked whenever
420e0 20 61 20 6e 65 77 20 5b 64 61 74 61 62 61 73 65   a new [database
420f0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20   connection].** 
42100 20 20 20 20 20 20 20 20 20 69 73 20 6f 70 65 6e           is open
42110 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65  ed using [sqlite
42120 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69  3_open()], [sqli
42130 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 0a 2a  te3_open16()],.*
42140 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 5b 73  *          or [s
42150 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
42160 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 32  ]..**.** {H12642
42170 7d 20 44 75 70 6c 69 63 61 74 65 20 65 78 74 65  } Duplicate exte
42180 6e 73 69 6f 6e 73 20 61 72 65 20 64 65 74 65 63  nsions are detec
42190 74 65 64 20 73 6f 20 63 61 6c 6c 69 6e 67 20 74  ted so calling t
421a0 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 20  his routine.**  
421b0 20 20 20 20 20 20 20 20 6d 75 6c 74 69 70 6c 65          multiple
421c0 20 74 69 6d 65 73 20 77 69 74 68 20 74 68 65 20   times with the 
421d0 73 61 6d 65 20 65 78 74 65 6e 73 69 6f 6e 20 69  same extension i
421e0 73 20 68 61 72 6d 6c 65 73 73 2e 0a 2a 2a 0a 2a  s harmless..**.*
421f0 2a 20 7b 48 31 32 36 34 33 7d 20 54 68 69 73 20  * {H12643} This 
42200 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20 61  routine stores a
42210 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
42220 65 78 74 65 6e 73 69 6f 6e 20 69 6e 20 61 6e 20  extension in an 
42230 61 72 72 61 79 0a 2a 2a 20 20 20 20 20 20 20 20  array.**        
42240 20 20 74 68 61 74 20 69 73 20 6f 62 74 61 69 6e    that is obtain
42250 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33  ed from [sqlite3
42260 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2a 0a 2a  _malloc()]..**.*
42270 2a 20 7b 48 31 32 36 34 34 7d 20 41 75 74 6f 6d  * {H12644} Autom
42280 61 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 73 20  atic extensions 
42290 61 70 70 6c 79 20 61 63 72 6f 73 73 20 61 6c 6c  apply across all
422a0 20 74 68 72 65 61 64 73 2e 0a 2a 2f 0a 53 51 4c   threads..*/.SQL
422b0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
422c0 74 65 33 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69  te3_auto_extensi
422d0 6f 6e 28 76 6f 69 64 20 2a 78 45 6e 74 72 79 50  on(void *xEntryP
422e0 6f 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  oint);../*.** CA
422f0 50 49 33 52 45 46 3a 20 52 65 73 65 74 20 41 75  PI3REF: Reset Au
42300 74 6f 6d 61 74 69 63 20 45 78 74 65 6e 73 69 6f  tomatic Extensio
42310 6e 20 4c 6f 61 64 69 6e 67 20 7b 48 31 32 36 36  n Loading {H1266
42320 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a  0} <S20500>.**.*
42330 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
42340 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 70 72 65  disables all pre
42350 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72  viously register
42360 65 64 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a 20  ed automatic.** 
42370 65 78 74 65 6e 73 69 6f 6e 73 2e 20 7b 45 4e 44  extensions. {END
42380 7d 20 20 49 74 20 75 6e 64 6f 65 73 20 74 68 65  }  It undoes the
42390 20 65 66 66 65 63 74 20 6f 66 20 61 6c 6c 20 70   effect of all p
423a0 72 69 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  rior.** [sqlite3
423b0 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28  _auto_extension(
423c0 29 5d 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20  )] calls..**.** 
423d0 7b 48 31 32 36 36 31 7d 20 54 68 69 73 20 66 75  {H12661} This fu
423e0 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20  nction disables 
423f0 61 6c 6c 20 70 72 65 76 69 6f 75 73 6c 79 20 72  all previously r
42400 65 67 69 73 74 65 72 65 64 0a 2a 2a 20 20 20 20  egistered.**    
42410 20 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 20        automatic 
42420 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a  extensions..**.*
42430 2a 20 7b 48 31 32 36 36 32 7d 20 54 68 69 73 20  * {H12662} This 
42440 66 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65  function disable
42450 73 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65  s automatic exte
42460 6e 73 69 6f 6e 73 20 69 6e 20 61 6c 6c 20 74 68  nsions in all th
42470 72 65 61 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  reads..*/.SQLITE
42480 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
42490 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74  3_reset_auto_ext
424a0 65 6e 73 69 6f 6e 28 76 6f 69 64 29 3b 0a 0a 2f  ension(void);../
424b0 2a 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d  *.****** EXPERIM
424c0 45 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20  ENTAL - subject 
424d0 74 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75  to change withou
424e0 74 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a  t notice *******
424f0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
42500 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74  e interface to t
42510 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  he virtual-table
42520 20 6d 65 63 68 61 6e 69 73 6d 20 69 73 20 63 75   mechanism is cu
42530 72 72 65 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72  rrently consider
42540 65 64 0a 2a 2a 20 74 6f 20 62 65 20 65 78 70 65  ed.** to be expe
42550 72 69 6d 65 6e 74 61 6c 2e 20 20 54 68 65 20 69  rimental.  The i
42560 6e 74 65 72 66 61 63 65 20 6d 69 67 68 74 20 63  nterface might c
42570 68 61 6e 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61  hange in incompa
42580 74 69 62 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 49  tible ways..** I
42590 66 20 74 68 69 73 20 69 73 20 61 20 70 72 6f 62  f this is a prob
425a0 6c 65 6d 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20  lem for you, do 
425b0 6e 6f 74 20 75 73 65 20 74 68 65 20 69 6e 74 65  not use the inte
425c0 72 66 61 63 65 20 61 74 20 74 68 69 73 20 74 69  rface at this ti
425d0 6d 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74  me..**.** When t
425e0 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  he virtual-table
425f0 20 6d 65 63 68 61 6e 69 73 6d 20 73 74 61 62 69   mechanism stabi
42600 6c 69 7a 65 73 2c 20 77 65 20 77 69 6c 6c 20 64  lizes, we will d
42610 65 63 6c 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e  eclare the.** in
42620 74 65 72 66 61 63 65 20 66 69 78 65 64 2c 20 73  terface fixed, s
42630 75 70 70 6f 72 74 20 69 74 20 69 6e 64 65 66 69  upport it indefi
42640 6e 69 74 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f  nitely, and remo
42650 76 65 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e  ve this comment.
42660 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63  .*/../*.** Struc
42670 74 75 72 65 73 20 75 73 65 64 20 62 79 20 74 68  tures used by th
42680 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
42690 69 6e 74 65 72 66 61 63 65 0a 2a 2f 0a 74 79 70  interface.*/.typ
426a0 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
426b0 74 65 33 5f 76 74 61 62 20 73 71 6c 69 74 65 33  te3_vtab sqlite3
426c0 5f 76 74 61 62 3b 0a 74 79 70 65 64 65 66 20 73  _vtab;.typedef s
426d0 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
426e0 64 65 78 5f 69 6e 66 6f 20 73 71 6c 69 74 65 33  dex_info sqlite3
426f0 5f 69 6e 64 65 78 5f 69 6e 66 6f 3b 0a 74 79 70  _index_info;.typ
42700 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
42710 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
42720 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
42730 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72  sor;.typedef str
42740 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  uct sqlite3_modu
42750 6c 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  le sqlite3_modul
42760 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  e;../*.** CAPI3R
42770 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c  EF: Virtual Tabl
42780 65 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 30 30  e Object {H18000
42790 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45  } <S20400>.** KE
427a0 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f  YWORDS: sqlite3_
427b0 6d 6f 64 75 6c 65 0a 2a 2a 20 45 58 50 45 52 49  module.** EXPERI
427c0 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 41 20 6d  MENTAL.**.** A m
427d0 6f 64 75 6c 65 20 69 73 20 61 20 63 6c 61 73 73  odule is a class
427e0 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61 62 6c   of virtual tabl
427f0 65 73 2e 20 20 45 61 63 68 20 6d 6f 64 75 6c 65  es.  Each module
42800 20 69 73 20 64 65 66 69 6e 65 64 0a 2a 2a 20 62   is defined.** b
42810 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  y an instance of
42820 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
42830 74 72 75 63 74 75 72 65 2e 20 20 54 68 69 73 20  tructure.  This 
42840 73 74 72 75 63 74 75 72 65 20 63 6f 6e 73 69 73  structure consis
42850 74 73 0a 2a 2a 20 6d 6f 73 74 6c 79 20 6f 66 20  ts.** mostly of 
42860 6d 65 74 68 6f 64 73 20 66 6f 72 20 74 68 65 20  methods for the 
42870 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  module..**.** Th
42880 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  is interface is 
42890 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e 64  experimental and
428a0 20 69 73 20 73 75 62 6a 65 63 74 20 74 6f 20 63   is subject to c
428b0 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f  hange or.** remo
428c0 76 61 6c 20 69 6e 20 66 75 74 75 72 65 20 72 65  val in future re
428d0 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65  leases of SQLite
428e0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69  ..*/.struct sqli
428f0 74 65 33 5f 6d 6f 64 75 6c 65 20 7b 0a 20 20 69  te3_module {.  i
42900 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20 20 69  nt iVersion;.  i
42910 6e 74 20 28 2a 78 43 72 65 61 74 65 29 28 73 71  nt (*xCreate)(sq
42920 6c 69 74 65 33 2a 2c 20 76 6f 69 64 20 2a 70 41  lite3*, void *pA
42930 75 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ux,.            
42940 20 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e     int argc, con
42950 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61  st char *const*a
42960 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20  rgv,.           
42970 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62      sqlite3_vtab
42980 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61 72 2a   **ppVTab, char*
42990 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e  *);.  int (*xCon
429a0 6e 65 63 74 29 28 73 71 6c 69 74 65 33 2a 2c 20  nect)(sqlite3*, 
429b0 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 20 20  void *pAux,.    
429c0 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 61             int a
429d0 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  rgc, const char 
429e0 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 20  *const*argv,.   
429f0 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
42a00 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 61  te3_vtab **ppVTa
42a10 62 2c 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e  b, char**);.  in
42a20 74 20 28 2a 78 42 65 73 74 49 6e 64 65 78 29 28  t (*xBestIndex)(
42a30 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
42a40 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64  Tab, sqlite3_ind
42a50 65 78 5f 69 6e 66 6f 2a 29 3b 0a 20 20 69 6e 74  ex_info*);.  int
42a60 20 28 2a 78 44 69 73 63 6f 6e 6e 65 63 74 29 28   (*xDisconnect)(
42a70 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
42a80 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44  Tab);.  int (*xD
42a90 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f  estroy)(sqlite3_
42aa0 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20  vtab *pVTab);.  
42ab0 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c  int (*xOpen)(sql
42ac0 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62  ite3_vtab *pVTab
42ad0 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  , sqlite3_vtab_c
42ae0 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72  ursor **ppCursor
42af0 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f 73  );.  int (*xClos
42b00 65 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  e)(sqlite3_vtab_
42b10 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20  cursor*);.  int 
42b20 28 2a 78 46 69 6c 74 65 72 29 28 73 71 6c 69 74  (*xFilter)(sqlit
42b30 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 2c  e3_vtab_cursor*,
42b40 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f 6e   int idxNum, con
42b50 73 74 20 63 68 61 72 20 2a 69 64 78 53 74 72 2c  st char *idxStr,
42b60 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
42b70 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
42b80 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29  e3_value **argv)
42b90 3b 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78 74 29  ;.  int (*xNext)
42ba0 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75  (sqlite3_vtab_cu
42bb0 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  rsor*);.  int (*
42bc0 78 45 6f 66 29 28 73 71 6c 69 74 65 33 5f 76 74  xEof)(sqlite3_vt
42bd0 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69  ab_cursor*);.  i
42be0 6e 74 20 28 2a 78 43 6f 6c 75 6d 6e 29 28 73 71  nt (*xColumn)(sq
42bf0 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
42c00 72 2a 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  r*, sqlite3_cont
42c10 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e  ext*, int);.  in
42c20 74 20 28 2a 78 52 6f 77 69 64 29 28 73 71 6c 69  t (*xRowid)(sqli
42c30 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a  te3_vtab_cursor*
42c40 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
42c50 2a 70 52 6f 77 69 64 29 3b 0a 20 20 69 6e 74 20  *pRowid);.  int 
42c60 28 2a 78 55 70 64 61 74 65 29 28 73 71 6c 69 74  (*xUpdate)(sqlit
42c70 65 33 5f 76 74 61 62 20 2a 2c 20 69 6e 74 2c 20  e3_vtab *, int, 
42c80 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
42c90 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
42ca0 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 42 65 67  *);.  int (*xBeg
42cb0 69 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  in)(sqlite3_vtab
42cc0 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20   *pVTab);.  int 
42cd0 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74 65 33  (*xSync)(sqlite3
42ce0 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20  _vtab *pVTab);. 
42cf0 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 29 28   int (*xCommit)(
42d00 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
42d10 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52  Tab);.  int (*xR
42d20 6f 6c 6c 62 61 63 6b 29 28 73 71 6c 69 74 65 33  ollback)(sqlite3
42d30 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20  _vtab *pVTab);. 
42d40 20 69 6e 74 20 28 2a 78 46 69 6e 64 46 75 6e 63   int (*xFindFunc
42d50 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 5f 76 74  tion)(sqlite3_vt
42d60 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74 20 6e  ab *pVtab, int n
42d70 41 72 67 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  Arg, const char 
42d80 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  *zName,.        
42d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76                 v
42da0 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29 28 73  oid (**pxFunc)(s
42db0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
42dc0 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
42dd0 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20 20 20 20  e**),.          
42de0 20 20 20 20 20 20 20 20 20 20 20 20 20 76 6f 69               voi
42df0 64 20 2a 2a 70 70 41 72 67 29 3b 0a 20 20 69 6e  d **ppArg);.  in
42e00 74 20 28 2a 78 52 65 6e 61 6d 65 29 28 73 71 6c  t (*xRename)(sql
42e10 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
42e20 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
42e30 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43  ew);.};../*.** C
42e40 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c  API3REF: Virtual
42e50 20 54 61 62 6c 65 20 49 6e 64 65 78 69 6e 67 20   Table Indexing 
42e60 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 7b 48 31 38  Information {H18
42e70 31 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a  100} <S20400>.**
42e80 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74   KEYWORDS: sqlit
42e90 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a 2a  e3_index_info.**
42ea0 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
42eb0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
42ec0 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63  index_info struc
42ed0 74 75 72 65 20 61 6e 64 20 69 74 73 20 73 75 62  ture and its sub
42ee0 73 74 72 75 63 74 75 72 65 73 20 69 73 20 75 73  structures is us
42ef0 65 64 20 74 6f 0a 2a 2a 20 70 61 73 73 20 69 6e  ed to.** pass in
42f00 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 61  formation into a
42f10 6e 64 20 72 65 63 65 69 76 65 20 74 68 65 20 72  nd receive the r
42f20 65 70 6c 79 20 66 72 6f 6d 20 74 68 65 20 78 42  eply from the xB
42f30 65 73 74 49 6e 64 65 78 0a 2a 2a 20 6d 65 74 68  estIndex.** meth
42f40 6f 64 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33  od of an sqlite3
42f50 5f 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20 66 69  _module.  The fi
42f60 65 6c 64 73 20 75 6e 64 65 72 20 2a 2a 49 6e 70  elds under **Inp
42f70 75 74 73 2a 2a 20 61 72 65 20 74 68 65 0a 2a 2a  uts** are the.**
42f80 20 69 6e 70 75 74 73 20 74 6f 20 78 42 65 73 74   inputs to xBest
42f90 49 6e 64 65 78 20 61 6e 64 20 61 72 65 20 72 65  Index and are re
42fa0 61 64 2d 6f 6e 6c 79 2e 20 20 78 42 65 73 74 49  ad-only.  xBestI
42fb0 6e 64 65 78 20 69 6e 73 65 72 74 73 20 69 74 73  ndex inserts its
42fc0 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e 74 6f  .** results into
42fd0 20 74 68 65 20 2a 2a 4f 75 74 70 75 74 73 2a 2a   the **Outputs**
42fe0 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 54   fields..**.** T
42ff0 68 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d  he aConstraint[]
43000 20 61 72 72 61 79 20 72 65 63 6f 72 64 73 20 57   array records W
43010 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f 6e 73  HERE clause cons
43020 74 72 61 69 6e 74 73 20 6f 66 20 74 68 65 20 66  traints of the f
43030 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 3e  orm:.**.** <pre>
43040 63 6f 6c 75 6d 6e 20 4f 50 20 65 78 70 72 3c 2f  column OP expr</
43050 70 72 65 3e 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65  pre>.**.** where
43060 20 4f 50 20 69 73 20 3d 2c 20 26 6c 74 3b 2c 20   OP is =, &lt;, 
43070 26 6c 74 3b 3d 2c 20 26 67 74 3b 2c 20 6f 72 20  &lt;=, &gt;, or 
43080 26 67 74 3b 3d 2e 20 20 54 68 65 20 70 61 72 74  &gt;=.  The part
43090 69 63 75 6c 61 72 20 6f 70 65 72 61 74 6f 72 20  icular operator 
430a0 69 73 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20  is.** stored in 
430b0 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 6f 70  aConstraint[].op
430c0 2e 20 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20  .  The index of 
430d0 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 73 74  the column is st
430e0 6f 72 65 64 20 69 6e 0a 2a 2a 20 61 43 6f 6e 73  ored in.** aCons
430f0 74 72 61 69 6e 74 5b 5d 2e 69 43 6f 6c 75 6d 6e  traint[].iColumn
43100 2e 20 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d  .  aConstraint[]
43110 2e 75 73 61 62 6c 65 20 69 73 20 54 52 55 45 20  .usable is TRUE 
43120 69 66 20 74 68 65 0a 2a 2a 20 65 78 70 72 20 6f  if the.** expr o
43130 6e 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64  n the right-hand
43140 20 73 69 64 65 20 63 61 6e 20 62 65 20 65 76 61   side can be eva
43150 6c 75 61 74 65 64 20 28 61 6e 64 20 74 68 75 73  luated (and thus
43160 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 0a   the constraint.
43170 2a 2a 20 69 73 20 75 73 61 62 6c 65 29 20 61 6e  ** is usable) an
43180 64 20 66 61 6c 73 65 20 69 66 20 69 74 20 63 61  d false if it ca
43190 6e 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  nnot..**.** The 
431a0 6f 70 74 69 6d 69 7a 65 72 20 61 75 74 6f 6d 61  optimizer automa
431b0 74 69 63 61 6c 6c 79 20 69 6e 76 65 72 74 73 20  tically inverts 
431c0 74 65 72 6d 73 20 6f 66 20 74 68 65 20 66 6f 72  terms of the for
431d0 6d 20 22 65 78 70 72 20 4f 50 20 63 6f 6c 75 6d  m "expr OP colum
431e0 6e 22 0a 2a 2a 20 61 6e 64 20 6d 61 6b 65 73 20  n".** and makes 
431f0 6f 74 68 65 72 20 73 69 6d 70 6c 69 66 69 63 61  other simplifica
43200 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 57 48 45  tions to the WHE
43210 52 45 20 63 6c 61 75 73 65 20 69 6e 20 61 6e 20  RE clause in an 
43220 61 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 67 65  attempt to.** ge
43230 74 20 61 73 20 6d 61 6e 79 20 57 48 45 52 45 20  t as many WHERE 
43240 63 6c 61 75 73 65 20 74 65 72 6d 73 20 69 6e 74  clause terms int
43250 6f 20 74 68 65 20 66 6f 72 6d 20 73 68 6f 77 6e  o the form shown
43260 20 61 62 6f 76 65 20 61 73 20 70 6f 73 73 69 62   above as possib
43270 6c 65 2e 0a 2a 2a 20 54 68 65 20 61 43 6f 6e 73  le..** The aCons
43280 74 72 61 69 6e 74 5b 5d 20 61 72 72 61 79 20 6f  traint[] array o
43290 6e 6c 79 20 72 65 70 6f 72 74 73 20 57 48 45 52  nly reports WHER
432a0 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 69  E clause terms i
432b0 6e 20 74 68 65 20 63 6f 72 72 65 63 74 0a 2a 2a  n the correct.**
432c0 20 66 6f 72 6d 20 74 68 61 74 20 72 65 66 65 72   form that refer
432d0 20 74 6f 20 74 68 65 20 70 61 72 74 69 63 75 6c   to the particul
432e0 61 72 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ar virtual table
432f0 20 62 65 69 6e 67 20 71 75 65 72 69 65 64 2e 0a   being queried..
43300 2a 2a 0a 2a 2a 20 49 6e 66 6f 72 6d 61 74 69 6f  **.** Informatio
43310 6e 20 61 62 6f 75 74 20 74 68 65 20 4f 52 44 45  n about the ORDE
43320 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 73  R BY clause is s
43330 74 6f 72 65 64 20 69 6e 20 61 4f 72 64 65 72 42  tored in aOrderB
43340 79 5b 5d 2e 0a 2a 2a 20 45 61 63 68 20 74 65 72  y[]..** Each ter
43350 6d 20 6f 66 20 61 4f 72 64 65 72 42 79 20 72 65  m of aOrderBy re
43360 63 6f 72 64 73 20 61 20 63 6f 6c 75 6d 6e 20 6f  cords a column o
43370 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  f the ORDER BY c
43380 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  lause..**.** The
43390 20 78 42 65 73 74 49 6e 64 65 78 20 6d 65 74 68   xBestIndex meth
433a0 6f 64 20 6d 75 73 74 20 66 69 6c 6c 20 61 43 6f  od must fill aCo
433b0 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 5d 20  nstraintUsage[] 
433c0 77 69 74 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  with information
433d0 0a 2a 2a 20 61 62 6f 75 74 20 77 68 61 74 20 70  .** about what p
433e0 61 72 61 6d 65 74 65 72 73 20 74 6f 20 70 61 73  arameters to pas
433f0 73 20 74 6f 20 78 46 69 6c 74 65 72 2e 20 20 49  s to xFilter.  I
43400 66 20 61 72 67 76 49 6e 64 65 78 3e 30 20 74 68  f argvIndex>0 th
43410 65 6e 0a 2a 2a 20 74 68 65 20 72 69 67 68 74 2d  en.** the right-
43420 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65  hand side of the
43430 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 61   corresponding a
43440 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 69 73 20  Constraint[] is 
43450 65 76 61 6c 75 61 74 65 64 0a 2a 2a 20 61 6e 64  evaluated.** and
43460 20 62 65 63 6f 6d 65 73 20 74 68 65 20 61 72 67   becomes the arg
43470 76 49 6e 64 65 78 2d 74 68 20 65 6e 74 72 79 20  vIndex-th entry 
43480 69 6e 20 61 72 67 76 2e 20 20 49 66 20 61 43 6f  in argv.  If aCo
43490 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 5d 2e  nstraintUsage[].
434a0 6f 6d 69 74 0a 2a 2a 20 69 73 20 74 72 75 65 2c  omit.** is true,
434b0 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 73 74 72   then the constr
434c0 61 69 6e 74 20 69 73 20 61 73 73 75 6d 65 64 20  aint is assumed 
434d0 74 6f 20 62 65 20 66 75 6c 6c 79 20 68 61 6e 64  to be fully hand
434e0 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 76 69  led by the.** vi
434f0 72 74 75 61 6c 20 74 61 62 6c 65 20 61 6e 64 20  rtual table and 
43500 69 73 20 6e 6f 74 20 63 68 65 63 6b 65 64 20 61  is not checked a
43510 67 61 69 6e 20 62 79 20 53 51 4c 69 74 65 2e 0a  gain by SQLite..
43520 2a 2a 0a 2a 2a 20 54 68 65 20 69 64 78 4e 75 6d  **.** The idxNum
43530 20 61 6e 64 20 69 64 78 50 74 72 20 76 61 6c 75   and idxPtr valu
43540 65 73 20 61 72 65 20 72 65 63 6f 72 64 65 64 20  es are recorded 
43550 61 6e 64 20 70 61 73 73 65 64 20 69 6e 74 6f 20  and passed into 
43560 78 46 69 6c 74 65 72 2e 0a 2a 2a 20 73 71 6c 69  xFilter..** sqli
43570 74 65 33 5f 66 72 65 65 28 29 20 69 73 20 75 73  te3_free() is us
43580 65 64 20 74 6f 20 66 72 65 65 20 69 64 78 50 74  ed to free idxPt
43590 72 20 69 66 20 6e 65 65 64 54 6f 46 72 65 65 49  r if needToFreeI
435a0 64 78 50 74 72 20 69 73 20 74 72 75 65 2e 0a 2a  dxPtr is true..*
435b0 2a 0a 2a 2a 20 54 68 65 20 6f 72 64 65 72 42 79  *.** The orderBy
435c0 43 6f 6e 73 75 6d 65 64 20 6d 65 61 6e 73 20 74  Consumed means t
435d0 68 61 74 20 6f 75 74 70 75 74 20 66 72 6f 6d 20  hat output from 
435e0 78 46 69 6c 74 65 72 20 77 69 6c 6c 20 6f 63 63  xFilter will occ
435f0 75 72 20 69 6e 0a 2a 2a 20 74 68 65 20 63 6f 72  ur in.** the cor
43600 72 65 63 74 20 6f 72 64 65 72 20 74 6f 20 73 61  rect order to sa
43610 74 69 73 66 79 20 74 68 65 20 4f 52 44 45 52 20  tisfy the ORDER 
43620 42 59 20 63 6c 61 75 73 65 20 73 6f 20 74 68 61  BY clause so tha
43630 74 20 6e 6f 20 73 65 70 61 72 61 74 65 0a 2a 2a  t no separate.**
43640 20 73 6f 72 74 69 6e 67 20 73 74 65 70 20 69 73   sorting step is
43650 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a   required..**.**
43660 20 54 68 65 20 65 73 74 69 6d 61 74 65 64 43 6f   The estimatedCo
43670 73 74 20 76 61 6c 75 65 20 69 73 20 61 6e 20 65  st value is an e
43680 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20 63  stimate of the c
43690 6f 73 74 20 6f 66 20 64 6f 69 6e 67 20 74 68 65  ost of doing the
436a0 0a 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20 6c  .** particular l
436b0 6f 6f 6b 75 70 2e 20 20 41 20 66 75 6c 6c 20 73  ookup.  A full s
436c0 63 61 6e 20 6f 66 20 61 20 74 61 62 6c 65 20 77  can of a table w
436d0 69 74 68 20 4e 20 65 6e 74 72 69 65 73 20 73 68  ith N entries sh
436e0 6f 75 6c 64 20 68 61 76 65 0a 2a 2a 20 61 20 63  ould have.** a c
436f0 6f 73 74 20 6f 66 20 4e 2e 20 20 41 20 62 69 6e  ost of N.  A bin
43700 61 72 79 20 73 65 61 72 63 68 20 6f 66 20 61 20  ary search of a 
43710 74 61 62 6c 65 20 6f 66 20 4e 20 65 6e 74 72 69  table of N entri
43720 65 73 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61  es should have a
43730 0a 2a 2a 20 63 6f 73 74 20 6f 66 20 61 70 70 72  .** cost of appr
43740 6f 78 69 6d 61 74 65 6c 79 20 6c 6f 67 28 4e 29  oximately log(N)
43750 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74  ..**.** This int
43760 65 72 66 61 63 65 20 69 73 20 65 78 70 65 72 69  erface is experi
43770 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 73 75  mental and is su
43780 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20  bject to change 
43790 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 69 6e  or.** removal in
437a0 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73   future releases
437b0 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 73   of SQLite..*/.s
437c0 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
437d0 64 65 78 5f 69 6e 66 6f 20 7b 0a 20 20 2f 2a 20  dex_info {.  /* 
437e0 49 6e 70 75 74 73 20 2a 2f 0a 20 20 69 6e 74 20  Inputs */.  int 
437f0 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20  nConstraint;    
43800 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
43810 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
43820 43 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20  Constraint */.  
43830 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
43840 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20  ndex_constraint 
43850 7b 0a 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 75  {.     int iColu
43860 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  mn;             
43870 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6f 6e 20 6c 65   /* Column on le
43880 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20  ft-hand side of 
43890 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20  constraint */.  
438a0 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72     unsigned char
438b0 20 6f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20   op;         /* 
438c0 43 6f 6e 73 74 72 61 69 6e 74 20 6f 70 65 72 61  Constraint opera
438d0 74 6f 72 20 2a 2f 0a 20 20 20 20 20 75 6e 73 69  tor */.     unsi
438e0 67 6e 65 64 20 63 68 61 72 20 75 73 61 62 6c 65  gned char usable
438f0 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  ;     /* True if
43900 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74   this constraint
43910 20 69 73 20 75 73 61 62 6c 65 20 2a 2f 0a 20 20   is usable */.  
43920 20 20 20 69 6e 74 20 69 54 65 72 6d 4f 66 66 73     int iTermOffs
43930 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  et;          /* 
43940 55 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20  Used internally 
43950 2d 20 78 42 65 73 74 49 6e 64 65 78 20 73 68 6f  - xBestIndex sho
43960 75 6c 64 20 69 67 6e 6f 72 65 20 2a 2f 0a 20 20  uld ignore */.  
43970 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 3b 20  } *aConstraint; 
43980 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61             /* Ta
43990 62 6c 65 20 6f 66 20 57 48 45 52 45 20 63 6c 61  ble of WHERE cla
439a0 75 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  use constraints 
439b0 2a 2f 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42  */.  int nOrderB
439c0 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y;              
439d0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72  /* Number of ter
439e0 6d 73 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20  ms in the ORDER 
439f0 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73  BY clause */.  s
43a00 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
43a10 64 65 78 5f 6f 72 64 65 72 62 79 20 7b 0a 20 20  dex_orderby {.  
43a20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20     int iColumn; 
43a30 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
43a40 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f  Column number */
43a50 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  .     unsigned c
43a60 68 61 72 20 64 65 73 63 3b 20 20 20 20 20 20 20  har desc;       
43a70 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53 43  /* True for DESC
43a80 2e 20 20 46 61 6c 73 65 20 66 6f 72 20 41 53 43  .  False for ASC
43a90 2e 20 2a 2f 0a 20 20 7d 20 2a 61 4f 72 64 65 72  . */.  } *aOrder
43aa0 42 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  By;             
43ab0 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42    /* The ORDER B
43ac0 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 2f 2a  Y clause */.  /*
43ad0 20 4f 75 74 70 75 74 73 20 2a 2f 0a 20 20 73 74   Outputs */.  st
43ae0 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
43af0 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73  ex_constraint_us
43b00 61 67 65 20 7b 0a 20 20 20 20 69 6e 74 20 61 72  age {.    int ar
43b10 67 76 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20  gvIndex;        
43b20 20 20 20 2f 2a 20 69 66 20 3e 30 2c 20 63 6f 6e     /* if >0, con
43b30 73 74 72 61 69 6e 74 20 69 73 20 70 61 72 74 20  straint is part 
43b40 6f 66 20 61 72 67 76 20 74 6f 20 78 46 69 6c 74  of argv to xFilt
43b50 65 72 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e  er */.    unsign
43b60 65 64 20 63 68 61 72 20 6f 6d 69 74 3b 20 20 20  ed char omit;   
43b70 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f 64     /* Do not cod
43b80 65 20 61 20 74 65 73 74 20 66 6f 72 20 74 68 69  e a test for thi
43b90 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a  s constraint */.
43ba0 20 20 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74    } *aConstraint
43bb0 55 73 61 67 65 3b 0a 20 20 69 6e 74 20 69 64 78  Usage;.  int idx
43bc0 4e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  Num;            
43bd0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 75 73      /* Number us
43be0 65 64 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74  ed to identify t
43bf0 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63 68  he index */.  ch
43c00 61 72 20 2a 69 64 78 53 74 72 3b 20 20 20 20 20  ar *idxStr;     
43c10 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69           /* Stri
43c20 6e 67 2c 20 70 6f 73 73 69 62 6c 79 20 6f 62 74  ng, possibly obt
43c30 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
43c40 65 33 5f 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 69  e3_malloc */.  i
43c50 6e 74 20 6e 65 65 64 54 6f 46 72 65 65 49 64 78  nt needToFreeIdx
43c60 53 74 72 3b 20 20 20 20 20 20 2f 2a 20 46 72 65  Str;      /* Fre
43c70 65 20 69 64 78 53 74 72 20 75 73 69 6e 67 20 73  e idxStr using s
43c80 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69 66  qlite3_free() if
43c90 20 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6f   true */.  int o
43ca0 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 3b 20  rderByConsumed; 
43cb0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
43cc0 20 6f 75 74 70 75 74 20 69 73 20 61 6c 72 65 61   output is alrea
43cd0 64 79 20 6f 72 64 65 72 65 64 20 2a 2f 0a 20 20  dy ordered */.  
43ce0 64 6f 75 62 6c 65 20 65 73 74 69 6d 61 74 65 64  double estimated
43cf0 43 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20 45 73  Cost;      /* Es
43d00 74 69 6d 61 74 65 64 20 63 6f 73 74 20 6f 66 20  timated cost of 
43d10 75 73 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78  using this index
43d20 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 53   */.};.#define S
43d30 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
43d40 54 52 41 49 4e 54 5f 45 51 20 20 20 20 32 0a 23  TRAINT_EQ    2.#
43d50 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e  define SQLITE_IN
43d60 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47  DEX_CONSTRAINT_G
43d70 54 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53  T    4.#define S
43d80 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
43d90 54 52 41 49 4e 54 5f 4c 45 20 20 20 20 38 0a 23  TRAINT_LE    8.#
43da0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e  define SQLITE_IN
43db0 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c  DEX_CONSTRAINT_L
43dc0 54 20 20 20 20 31 36 0a 23 64 65 66 69 6e 65 20  T    16.#define 
43dd0 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
43de0 53 54 52 41 49 4e 54 5f 47 45 20 20 20 20 33 32  STRAINT_GE    32
43df0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
43e00 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54  INDEX_CONSTRAINT
43e10 5f 4d 41 54 43 48 20 36 34 0a 0a 2f 2a 0a 2a 2a  _MATCH 64../*.**
43e20 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 69 73   CAPI3REF: Regis
43e30 74 65 72 20 41 20 56 69 72 74 75 61 6c 20 54 61  ter A Virtual Ta
43e40 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  ble Implementati
43e50 6f 6e 20 7b 48 31 38 32 30 30 7d 20 3c 53 32 30  on {H18200} <S20
43e60 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45  400>.** EXPERIME
43e70 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  NTAL.**.** This 
43e80 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
43e90 74 6f 20 72 65 67 69 73 74 65 72 20 61 20 6e 65  to register a ne
43ea0 77 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 77 69  w module name wi
43eb0 74 68 20 61 0a 2a 2a 20 5b 64 61 74 61 62 61 73  th a.** [databas
43ec0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20  e connection].  
43ed0 4d 6f 64 75 6c 65 20 6e 61 6d 65 73 20 6d 75 73  Module names mus
43ee0 74 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20  t be registered 
43ef0 62 65 66 6f 72 65 0a 2a 2a 20 63 72 65 61 74 69  before.** creati
43f00 6e 67 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74  ng new virtual t
43f10 61 62 6c 65 73 20 6f 6e 20 74 68 65 20 6d 6f 64  ables on the mod
43f20 75 6c 65 2c 20 6f 72 20 62 65 66 6f 72 65 20 75  ule, or before u
43f30 73 69 6e 67 0a 2a 2a 20 70 72 65 65 78 69 73 74  sing.** preexist
43f40 69 6e 67 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ing virtual tabl
43f50 65 73 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65  es of the module
43f60 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74  ..**.** This int
43f70 65 72 66 61 63 65 20 69 73 20 65 78 70 65 72 69  erface is experi
43f80 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 73 75  mental and is su
43f90 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20  bject to change 
43fa0 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 69 6e  or.** removal in
43fb0 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73   future releases
43fc0 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53   of SQLite..*/.S
43fd0 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45  QLITE_API SQLITE
43fe0 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e  _EXPERIMENTAL in
43ff0 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  t sqlite3_create
44000 5f 6d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74  _module(.  sqlit
44010 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
44020 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20        /* SQLite 
44030 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65  connection to re
44040 67 69 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69  gister module wi
44050 74 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  th */.  const ch
44060 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20  ar *zName,      
44070 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
44080 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f  e module */.  co
44090 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
440a0 6c 65 20 2a 2c 20 20 20 20 2f 2a 20 4d 65 74 68  le *,    /* Meth
440b0 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75  ods for the modu
440c0 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 20 20  le */.  void *  
440d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
440e0 20 20 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74     /* Client dat
440f0 61 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43  a for xCreate/xC
44100 6f 6e 6e 65 63 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a  onnect */.);../*
44110 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65  .** CAPI3REF: Re
44120 67 69 73 74 65 72 20 41 20 56 69 72 74 75 61 6c  gister A Virtual
44130 20 54 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74   Table Implement
44140 61 74 69 6f 6e 20 7b 48 31 38 32 31 30 7d 20 3c  ation {H18210} <
44150 53 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52  S20400>.** EXPER
44160 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68  IMENTAL.**.** Th
44170 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69 64  is routine is id
44180 65 6e 74 69 63 61 6c 20 74 6f 20 74 68 65 20 5b  entical to the [
44190 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
441a0 6f 64 75 6c 65 28 29 5d 20 6d 65 74 68 6f 64 20  odule()] method 
441b0 61 62 6f 76 65 2c 0a 2a 2a 20 65 78 63 65 70 74  above,.** except
441c0 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 77 73 20   that it allows 
441d0 61 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e  a destructor fun
441e0 63 74 69 6f 6e 20 74 6f 20 62 65 20 73 70 65 63  ction to be spec
441f0 69 66 69 65 64 2e 20 49 74 20 69 73 0a 2a 2a 20  ified. It is.** 
44200 65 76 65 6e 20 6d 6f 72 65 20 65 78 70 65 72 69  even more experi
44210 6d 65 6e 74 61 6c 20 74 68 61 6e 20 74 68 65 20  mental than the 
44220 72 65 73 74 20 6f 66 20 74 68 65 20 76 69 72 74  rest of the virt
44230 75 61 6c 20 74 61 62 6c 65 73 20 41 50 49 2e 0a  ual tables API..
44240 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51  */.SQLITE_API SQ
44250 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41  LITE_EXPERIMENTA
44260 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72  L int sqlite3_cr
44270 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 0a  eate_module_v2(.
44280 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
44290 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
442a0 53 51 4c 69 74 65 20 63 6f 6e 6e 65 63 74 69 6f  SQLite connectio
442b0 6e 20 74 6f 20 72 65 67 69 73 74 65 72 20 6d 6f  n to register mo
442c0 64 75 6c 65 20 77 69 74 68 20 2a 2f 0a 20 20 63  dule with */.  c
442d0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
442e0 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  ,         /* Nam
442f0 65 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20  e of the module 
44300 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  */.  const sqlit
44310 65 33 5f 6d 6f 64 75 6c 65 20 2a 2c 20 20 20 20  e3_module *,    
44320 2f 2a 20 4d 65 74 68 6f 64 73 20 66 6f 72 20 74  /* Methods for t
44330 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76  he module */.  v
44340 6f 69 64 20 2a 2c 20 20 20 20 20 20 20 20 20 20  oid *,          
44350 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6c 69            /* Cli
44360 65 6e 74 20 64 61 74 61 20 66 6f 72 20 78 43 72  ent data for xCr
44370 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f  eate/xConnect */
44380 0a 20 20 76 6f 69 64 28 2a 78 44 65 73 74 72 6f  .  void(*xDestro
44390 79 29 28 76 6f 69 64 2a 29 20 20 20 20 20 2f 2a  y)(void*)     /*
443a0 20 4d 6f 64 75 6c 65 20 64 65 73 74 72 75 63 74   Module destruct
443b0 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29  or function */.)
443c0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
443d0 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65  F: Virtual Table
443e0 20 49 6e 73 74 61 6e 63 65 20 4f 62 6a 65 63 74   Instance Object
443f0 20 7b 48 31 38 30 31 30 7d 20 3c 53 32 30 34 30   {H18010} <S2040
44400 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  0>.** KEYWORDS: 
44410 73 71 6c 69 74 65 33 5f 76 74 61 62 0a 2a 2a 20  sqlite3_vtab.** 
44420 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a  EXPERIMENTAL.**.
44430 2a 2a 20 45 76 65 72 79 20 6d 6f 64 75 6c 65 20  ** Every module 
44440 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 75  implementation u
44450 73 65 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f  ses a subclass o
44460 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
44470 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 74 6f 20  structure.** to 
44480 64 65 73 63 72 69 62 65 20 61 20 70 61 72 74 69  describe a parti
44490 63 75 6c 61 72 20 69 6e 73 74 61 6e 63 65 20 6f  cular instance o
444a0 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20 45  f the module.  E
444b0 61 63 68 20 73 75 62 63 6c 61 73 73 20 77 69 6c  ach subclass wil
444c0 6c 0a 2a 2a 20 62 65 20 74 61 69 6c 6f 72 65 64  l.** be tailored
444d0 20 74 6f 20 74 68 65 20 73 70 65 63 69 66 69 63   to the specific
444e0 20 6e 65 65 64 73 20 6f 66 20 74 68 65 20 6d 6f   needs of the mo
444f0 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  dule implementat
44500 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 70 75 72 70  ion..** The purp
44510 6f 73 65 20 6f 66 20 74 68 69 73 20 73 75 70 65  ose of this supe
44520 72 63 6c 61 73 73 20 69 73 20 74 6f 20 64 65 66  rclass is to def
44530 69 6e 65 20 63 65 72 74 61 69 6e 20 66 69 65 6c  ine certain fiel
44540 64 73 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 63  ds that are.** c
44550 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6d 6f 64  ommon to all mod
44560 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ule implementati
44570 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75  ons..**.** Virtu
44580 61 6c 20 74 61 62 6c 65 73 20 6d 65 74 68 6f 64  al tables method
44590 73 20 63 61 6e 20 73 65 74 20 61 6e 20 65 72 72  s can set an err
445a0 6f 72 20 6d 65 73 73 61 67 65 20 62 79 20 61 73  or message by as
445b0 73 69 67 6e 69 6e 67 20 61 0a 2a 2a 20 73 74 72  signing a.** str
445c0 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ing obtained fro
445d0 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e  m [sqlite3_mprin
445e0 74 66 28 29 5d 20 74 6f 20 7a 45 72 72 4d 73 67  tf()] to zErrMsg
445f0 2e 20 20 54 68 65 20 6d 65 74 68 6f 64 20 73 68  .  The method sh
44600 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65 20 63 61 72  ould.** take car
44610 65 20 74 68 61 74 20 61 6e 79 20 70 72 69 6f 72  e that any prior
44620 20 73 74 72 69 6e 67 20 69 73 20 66 72 65 65 64   string is freed
44630 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73   by a call to [s
44640 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 0a 2a  qlite3_free()].*
44650 2a 20 70 72 69 6f 72 20 74 6f 20 61 73 73 69 67  * prior to assig
44660 6e 69 6e 67 20 61 20 6e 65 77 20 73 74 72 69 6e  ning a new strin
44670 67 20 74 6f 20 7a 45 72 72 4d 73 67 2e 20 20 41  g to zErrMsg.  A
44680 66 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 6d  fter the error m
44690 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 64 65 6c  essage.** is del
446a0 69 76 65 72 65 64 20 75 70 20 74 6f 20 74 68 65  ivered up to the
446b0 20 63 6c 69 65 6e 74 20 61 70 70 6c 69 63 61 74   client applicat
446c0 69 6f 6e 2c 20 74 68 65 20 73 74 72 69 6e 67 20  ion, the string 
446d0 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69  will be automati
446e0 63 61 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 20 62  cally.** freed b
446f0 79 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  y sqlite3_free()
44700 20 61 6e 64 20 74 68 65 20 7a 45 72 72 4d 73 67   and the zErrMsg
44710 20 66 69 65 6c 64 20 77 69 6c 6c 20 62 65 20 7a   field will be z
44720 65 72 6f 65 64 2e 20 20 4e 6f 74 65 0a 2a 2a 20  eroed.  Note.** 
44730 74 68 61 74 20 73 71 6c 69 74 65 33 5f 6d 70 72  that sqlite3_mpr
44740 69 6e 74 66 28 29 20 61 6e 64 20 73 71 6c 69 74  intf() and sqlit
44750 65 33 5f 66 72 65 65 28 29 20 61 72 65 20 75 73  e3_free() are us
44760 65 64 20 6f 6e 20 74 68 65 20 7a 45 72 72 4d 73  ed on the zErrMs
44770 67 20 66 69 65 6c 64 0a 2a 2a 20 73 69 6e 63 65  g field.** since
44780 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20   virtual tables 
44790 61 72 65 20 63 6f 6d 6d 6f 6e 6c 79 20 69 6d 70  are commonly imp
447a0 6c 65 6d 65 6e 74 65 64 20 69 6e 20 6c 6f 61 64  lemented in load
447b0 61 62 6c 65 20 65 78 74 65 6e 73 69 6f 6e 73 20  able extensions 
447c0 77 68 69 63 68 0a 2a 2a 20 64 6f 20 6e 6f 74 20  which.** do not 
447d0 68 61 76 65 20 61 63 63 65 73 73 20 74 6f 20 73  have access to s
447e0 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 29 20  qlite3MPrintf() 
447f0 6f 72 20 73 71 6c 69 74 65 33 46 72 65 65 28 29  or sqlite3Free()
44800 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74  ..**.** This int
44810 65 72 66 61 63 65 20 69 73 20 65 78 70 65 72 69  erface is experi
44820 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 73 75  mental and is su
44830 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20  bject to change 
44840 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 69 6e  or.** removal in
44850 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73   future releases
44860 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 73   of SQLite..*/.s
44870 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76 74  truct sqlite3_vt
44880 61 62 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c  ab {.  const sql
44890 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
448a0 64 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20 6d 6f  dule;  /* The mo
448b0 64 75 6c 65 20 66 6f 72 20 74 68 69 73 20 76 69  dule for this vi
448c0 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20  rtual table */. 
448d0 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20   int nRef;      
448e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
448f0 20 2f 2a 20 55 73 65 64 20 69 6e 74 65 72 6e 61   /* Used interna
44900 6c 6c 79 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  lly */.  char *z
44910 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20 20 20  ErrMsg;         
44920 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f           /* Erro
44930 72 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 73  r message from s
44940 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29  qlite3_mprintf()
44950 20 2a 2f 0a 20 20 2f 2a 20 56 69 72 74 75 61 6c   */.  /* Virtual
44960 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74   table implement
44970 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69  ations will typi
44980 63 61 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69  cally add additi
44990 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d  onal fields */.}
449a0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
449b0 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65  F: Virtual Table
449c0 20 43 75 72 73 6f 72 20 4f 62 6a 65 63 74 20 20   Cursor Object  
449d0 7b 48 31 38 30 32 30 7d 20 3c 53 32 30 34 30 30  {H18020} <S20400
449e0 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73  >.** KEYWORDS: s
449f0 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
44a00 6f 72 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  or.** EXPERIMENT
44a10 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 6d  AL.**.** Every m
44a20 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  odule implementa
44a30 74 69 6f 6e 20 75 73 65 73 20 61 20 73 75 62 63  tion uses a subc
44a40 6c 61 73 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c  lass of the foll
44a50 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a  owing structure.
44a60 2a 2a 20 74 6f 20 64 65 73 63 72 69 62 65 20 63  ** to describe c
44a70 75 72 73 6f 72 73 20 74 68 61 74 20 70 6f 69 6e  ursors that poin
44a80 74 20 69 6e 74 6f 20 74 68 65 20 76 69 72 74 75  t into the virtu
44a90 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 61 72 65  al table and are
44aa0 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 6f 70   used.** to loop
44ab0 20 74 68 72 6f 75 67 68 20 74 68 65 20 76 69 72   through the vir
44ac0 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 43 75 72  tual table.  Cur
44ad0 73 6f 72 73 20 61 72 65 20 63 72 65 61 74 65 64  sors are created
44ae0 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 78 4f   using the.** xO
44af0 70 65 6e 20 6d 65 74 68 6f 64 20 6f 66 20 74 68  pen method of th
44b00 65 20 6d 6f 64 75 6c 65 2e 20 20 45 61 63 68 20  e module.  Each 
44b10 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  module implement
44b20 61 74 69 6f 6e 20 77 69 6c 6c 20 64 65 66 69 6e  ation will defin
44b30 65 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74  e.** the content
44b40 20 6f 66 20 61 20 63 75 72 73 6f 72 20 73 74 72   of a cursor str
44b50 75 63 74 75 72 65 20 74 6f 20 73 75 69 74 20 69  ucture to suit i
44b60 74 73 20 6f 77 6e 20 6e 65 65 64 73 2e 0a 2a 2a  ts own needs..**
44b70 0a 2a 2a 20 54 68 69 73 20 73 75 70 65 72 63 6c  .** This supercl
44b80 61 73 73 20 65 78 69 73 74 73 20 69 6e 20 6f 72  ass exists in or
44b90 64 65 72 20 74 6f 20 64 65 66 69 6e 65 20 66 69  der to define fi
44ba0 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75 72 73  elds of the curs
44bb0 6f 72 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 63  or that.** are c
44bc0 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d 70  ommon to all imp
44bd0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a  lementations..**
44be0 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61  .** This interfa
44bf0 63 65 20 69 73 20 65 78 70 65 72 69 6d 65 6e 74  ce is experiment
44c00 61 6c 20 61 6e 64 20 69 73 20 73 75 62 6a 65 63  al and is subjec
44c10 74 20 74 6f 20 63 68 61 6e 67 65 20 6f 72 0a 2a  t to change or.*
44c20 2a 20 72 65 6d 6f 76 61 6c 20 69 6e 20 66 75 74  * removal in fut
44c30 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20  ure releases of 
44c40 53 51 4c 69 74 65 2e 0a 2a 2f 0a 73 74 72 75 63  SQLite..*/.struc
44c50 74 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  t sqlite3_vtab_c
44c60 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65  ursor {.  sqlite
44c70 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 20 20  3_vtab *pVtab;  
44c80 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74      /* Virtual t
44c90 61 62 6c 65 20 6f 66 20 74 68 69 73 20 63 75 72  able of this cur
44ca0 73 6f 72 20 2a 2f 0a 20 20 2f 2a 20 56 69 72 74  sor */.  /* Virt
44cb0 75 61 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d  ual table implem
44cc0 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74  entations will t
44cd0 79 70 69 63 61 6c 6c 79 20 61 64 64 20 61 64 64  ypically add add
44ce0 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a  itional fields *
44cf0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  /.};../*.** CAPI
44d00 33 52 45 46 3a 20 44 65 63 6c 61 72 65 20 54 68  3REF: Declare Th
44d10 65 20 53 63 68 65 6d 61 20 4f 66 20 41 20 56 69  e Schema Of A Vi
44d20 72 74 75 61 6c 20 54 61 62 6c 65 20 7b 48 31 38  rtual Table {H18
44d30 32 38 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a  280} <S20400>.**
44d40 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
44d50 0a 2a 2a 20 54 68 65 20 78 43 72 65 61 74 65 20  .** The xCreate 
44d60 61 6e 64 20 78 43 6f 6e 6e 65 63 74 20 6d 65 74  and xConnect met
44d70 68 6f 64 73 20 6f 66 20 61 20 6d 6f 64 75 6c 65  hods of a module
44d80 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69   use the followi
44d90 6e 67 20 41 50 49 0a 2a 2a 20 74 6f 20 64 65 63  ng API.** to dec
44da0 6c 61 72 65 20 74 68 65 20 66 6f 72 6d 61 74 20  lare the format 
44db0 28 74 68 65 20 6e 61 6d 65 73 20 61 6e 64 20 64  (the names and d
44dc0 61 74 61 74 79 70 65 73 20 6f 66 20 74 68 65 20  atatypes of the 
44dd0 63 6f 6c 75 6d 6e 73 29 20 6f 66 0a 2a 2a 20 74  columns) of.** t
44de0 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
44df0 73 20 74 68 65 79 20 69 6d 70 6c 65 6d 65 6e 74  s they implement
44e00 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74  ..**.** This int
44e10 65 72 66 61 63 65 20 69 73 20 65 78 70 65 72 69  erface is experi
44e20 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 73 75  mental and is su
44e30 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20  bject to change 
44e40 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 69 6e  or.** removal in
44e50 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73   future releases
44e60 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53   of SQLite..*/.S
44e70 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45  QLITE_API SQLITE
44e80 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e  _EXPERIMENTAL in
44e90 74 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72  t sqlite3_declar
44ea0 65 5f 76 74 61 62 28 73 71 6c 69 74 65 33 2a 2c  e_vtab(sqlite3*,
44eb0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 72   const char *zCr
44ec0 65 61 74 65 54 61 62 6c 65 29 3b 0a 0a 2f 2a 0a  eateTable);../*.
44ed0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 76 65  ** CAPI3REF: Ove
44ee0 72 6c 6f 61 64 20 41 20 46 75 6e 63 74 69 6f 6e  rload A Function
44ef0 20 46 6f 72 20 41 20 56 69 72 74 75 61 6c 20 54   For A Virtual T
44f00 61 62 6c 65 20 7b 48 31 38 33 30 30 7d 20 3c 53  able {H18300} <S
44f10 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49  20400>.** EXPERI
44f20 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 56 69 72  MENTAL.**.** Vir
44f30 74 75 61 6c 20 74 61 62 6c 65 73 20 63 61 6e 20  tual tables can 
44f40 70 72 6f 76 69 64 65 20 61 6c 74 65 72 6e 61 74  provide alternat
44f50 69 76 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ive implementati
44f60 6f 6e 73 20 6f 66 20 66 75 6e 63 74 69 6f 6e 73  ons of functions
44f70 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 78 46  .** using the xF
44f80 69 6e 64 46 75 6e 63 74 69 6f 6e 20 6d 65 74 68  indFunction meth
44f90 6f 64 2e 20 20 42 75 74 20 67 6c 6f 62 61 6c 20  od.  But global 
44fa0 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 6f 73  versions of thos
44fb0 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6d  e functions.** m
44fc0 75 73 74 20 65 78 69 73 74 20 69 6e 20 6f 72 64  ust exist in ord
44fd0 65 72 20 74 6f 20 62 65 20 6f 76 65 72 6c 6f 61  er to be overloa
44fe0 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ded..**.** This 
44ff0 41 50 49 20 6d 61 6b 65 73 20 73 75 72 65 20 61  API makes sure a
45000 20 67 6c 6f 62 61 6c 20 76 65 72 73 69 6f 6e 20   global version 
45010 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69  of a function wi
45020 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a  th a particular.
45030 2a 2a 20 6e 61 6d 65 20 61 6e 64 20 6e 75 6d 62  ** name and numb
45040 65 72 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73  er of parameters
45050 20 65 78 69 73 74 73 2e 20 20 49 66 20 6e 6f 20   exists.  If no 
45060 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 20 65 78  such function ex
45070 69 73 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 74  ists.** before t
45080 68 69 73 20 41 50 49 20 69 73 20 63 61 6c 6c 65  his API is calle
45090 64 2c 20 61 20 6e 65 77 20 66 75 6e 63 74 69 6f  d, a new functio
450a0 6e 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 54  n is created.  T
450b0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
450c0 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77 20  n.** of the new 
450d0 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20  function always 
450e0 63 61 75 73 65 73 20 61 6e 20 65 78 63 65 70 74  causes an except
450f0 69 6f 6e 20 74 6f 20 62 65 20 74 68 72 6f 77 6e  ion to be thrown
45100 2e 20 20 53 6f 0a 2a 2a 20 74 68 65 20 6e 65 77  .  So.** the new
45110 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74   function is not
45120 20 67 6f 6f 64 20 66 6f 72 20 61 6e 79 74 68 69   good for anythi
45130 6e 67 20 62 79 20 69 74 73 65 6c 66 2e 20 20 49  ng by itself.  I
45140 74 73 20 6f 6e 6c 79 0a 2a 2a 20 70 75 72 70 6f  ts only.** purpo
45150 73 65 20 69 73 20 74 6f 20 62 65 20 61 20 70 6c  se is to be a pl
45160 61 63 65 68 6f 6c 64 65 72 20 66 75 6e 63 74 69  aceholder functi
45170 6f 6e 20 74 68 61 74 20 63 61 6e 20 62 65 20 6f  on that can be o
45180 76 65 72 6c 6f 61 64 65 64 0a 2a 2a 20 62 79 20  verloaded.** by 
45190 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a  virtual tables..
451a0 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 73  **.** This API s
451b0 68 6f 75 6c 64 20 62 65 20 63 6f 6e 73 69 64 65  hould be conside
451c0 72 65 64 20 70 61 72 74 20 6f 66 20 74 68 65 20  red part of the 
451d0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e  virtual table in
451e0 74 65 72 66 61 63 65 2c 0a 2a 2a 20 77 68 69 63  terface,.** whic
451f0 68 20 69 73 20 65 78 70 65 72 69 6d 65 6e 74 61  h is experimenta
45200 6c 20 61 6e 64 20 73 75 62 6a 65 63 74 20 74 6f  l and subject to
45210 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a 53 51 4c 49   change..*/.SQLI
45220 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58  TE_API SQLITE_EX
45230 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73  PERIMENTAL int s
45240 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f  qlite3_overload_
45250 66 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33  function(sqlite3
45260 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  *, const char *z
45270 46 75 6e 63 4e 61 6d 65 2c 20 69 6e 74 20 6e 41  FuncName, int nA
45280 72 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  rg);../*.** The 
45290 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 65  interface to the
452a0 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d   virtual-table m
452b0 65 63 68 61 6e 69 73 6d 20 64 65 66 69 6e 65 64  echanism defined
452c0 20 61 62 6f 76 65 20 28 62 61 63 6b 20 75 70 0a   above (back up.
452d0 2a 2a 20 74 6f 20 61 20 63 6f 6d 6d 65 6e 74 20  ** to a comment 
452e0 72 65 6d 61 72 6b 61 62 6c 79 20 73 69 6d 69 6c  remarkably simil
452f0 61 72 20 74 6f 20 74 68 69 73 20 6f 6e 65 29 20  ar to this one) 
45300 69 73 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e  is currently con
45310 73 69 64 65 72 65 64 0a 2a 2a 20 74 6f 20 62 65  sidered.** to be
45320 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20 20   experimental.  
45330 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 6d 69  The interface mi
45340 67 68 74 20 63 68 61 6e 67 65 20 69 6e 20 69 6e  ght change in in
45350 63 6f 6d 70 61 74 69 62 6c 65 20 77 61 79 73 2e  compatible ways.
45360 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61  .** If this is a
45370 20 70 72 6f 62 6c 65 6d 20 66 6f 72 20 79 6f 75   problem for you
45380 2c 20 64 6f 20 6e 6f 74 20 75 73 65 20 74 68 65  , do not use the
45390 20 69 6e 74 65 72 66 61 63 65 20 61 74 20 74 68   interface at th
453a0 69 73 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 57  is time..**.** W
453b0 68 65 6e 20 74 68 65 20 76 69 72 74 75 61 6c 2d  hen the virtual-
453c0 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20  table mechanism 
453d0 73 74 61 62 69 6c 69 7a 65 73 2c 20 77 65 20 77  stabilizes, we w
453e0 69 6c 6c 20 64 65 63 6c 61 72 65 20 74 68 65 0a  ill declare the.
453f0 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 66 69 78  ** interface fix
45400 65 64 2c 20 73 75 70 70 6f 72 74 20 69 74 20 69  ed, support it i
45410 6e 64 65 66 69 6e 69 74 65 6c 79 2c 20 61 6e 64  ndefinitely, and
45420 20 72 65 6d 6f 76 65 20 74 68 69 73 20 63 6f 6d   remove this com
45430 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 20  ment..**.****** 
45440 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 2d 20 73  EXPERIMENTAL - s
45450 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65  ubject to change
45460 20 77 69 74 68 6f 75 74 20 6e 6f 74 69 63 65 20   without notice 
45470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
45480 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  /../*.** CAPI3RE
45490 46 3a 20 41 20 48 61 6e 64 6c 65 20 54 6f 20 41  F: A Handle To A
454a0 6e 20 4f 70 65 6e 20 42 4c 4f 42 20 7b 48 31 37  n Open BLOB {H17
454b0 38 30 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a  800} <S30230>.**
454c0 20 4b 45 59 57 4f 52 44 53 3a 20 7b 42 4c 4f 42   KEYWORDS: {BLOB
454d0 20 68 61 6e 64 6c 65 7d 20 7b 42 4c 4f 42 20 68   handle} {BLOB h
454e0 61 6e 64 6c 65 73 7d 0a 2a 2a 0a 2a 2a 20 41 6e  andles}.**.** An
454f0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
45500 73 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73 65  s object represe
45510 6e 74 73 20 61 6e 20 6f 70 65 6e 20 42 4c 4f 42  nts an open BLOB
45520 20 6f 6e 20 77 68 69 63 68 0a 2a 2a 20 5b 73 71   on which.** [sq
45530 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20  lite3_blob_open 
45540 7c 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 42 4c  | incremental BL
45550 4f 42 20 49 2f 4f 5d 20 63 61 6e 20 62 65 20 70  OB I/O] can be p
45560 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a 20 4f 62 6a  erformed..** Obj
45570 65 63 74 73 20 6f 66 20 74 68 69 73 20 74 79 70  ects of this typ
45580 65 20 61 72 65 20 63 72 65 61 74 65 64 20 62 79  e are created by
45590 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f   [sqlite3_blob_o
455a0 70 65 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 64 65  pen()].** and de
455b0 73 74 72 6f 79 65 64 20 62 79 20 5b 73 71 6c 69  stroyed by [sqli
455c0 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29  te3_blob_close()
455d0 5d 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74  ]..** The [sqlit
455e0 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 5d 20  e3_blob_read()] 
455f0 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f  and [sqlite3_blo
45600 62 5f 77 72 69 74 65 28 29 5d 20 69 6e 74 65 72  b_write()] inter
45610 66 61 63 65 73 0a 2a 2a 20 63 61 6e 20 62 65 20  faces.** can be 
45620 75 73 65 64 20 74 6f 20 72 65 61 64 20 6f 72 20  used to read or 
45630 77 72 69 74 65 20 73 6d 61 6c 6c 20 73 75 62 73  write small subs
45640 65 63 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 42  ections of the B
45650 4c 4f 42 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c  LOB..** The [sql
45660 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28  ite3_blob_bytes(
45670 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  )] interface ret
45680 75 72 6e 73 20 74 68 65 20 73 69 7a 65 20 6f 66  urns the size of
45690 20 74 68 65 20 42 4c 4f 42 20 69 6e 20 62 79 74   the BLOB in byt
456a0 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  es..*/.typedef s
456b0 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 62 6c  truct sqlite3_bl
456c0 6f 62 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 3b  ob sqlite3_blob;
456d0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
456e0 3a 20 4f 70 65 6e 20 41 20 42 4c 4f 42 20 46 6f  : Open A BLOB Fo
456f0 72 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 49 2f  r Incremental I/
45700 4f 20 7b 48 31 37 38 31 30 7d 20 3c 53 33 30 32  O {H17810} <S302
45710 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  30>.**.** This i
45720 6e 74 65 72 66 61 63 65 73 20 6f 70 65 6e 73 20  nterfaces opens 
45730 61 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 20 7c  a [BLOB handle |
45740 20 68 61 6e 64 6c 65 5d 20 74 6f 20 74 68 65 20   handle] to the 
45750 42 4c 4f 42 20 6c 6f 63 61 74 65 64 0a 2a 2a 20  BLOB located.** 
45760 69 6e 20 72 6f 77 20 69 52 6f 77 2c 20 63 6f 6c  in row iRow, col
45770 75 6d 6e 20 7a 43 6f 6c 75 6d 6e 2c 20 74 61 62  umn zColumn, tab
45780 6c 65 20 7a 54 61 62 6c 65 20 69 6e 20 64 61 74  le zTable in dat
45790 61 62 61 73 65 20 7a 44 62 3b 0a 2a 2a 20 69 6e  abase zDb;.** in
457a0 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68   other words, th
457b0 65 20 73 61 6d 65 20 42 4c 4f 42 20 74 68 61 74  e same BLOB that
457c0 20 77 6f 75 6c 64 20 62 65 20 73 65 6c 65 63 74   would be select
457d0 65 64 20 62 79 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72  ed by:.**.** <pr
457e0 65 3e 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54  e>.**     SELECT
457f0 20 7a 43 6f 6c 75 6d 6e 20 46 52 4f 4d 20 7a 44   zColumn FROM zD
45800 62 2e 7a 54 61 62 6c 65 20 57 48 45 52 45 20 72  b.zTable WHERE r
45810 6f 77 69 64 20 3d 20 69 52 6f 77 3b 0a 2a 2a 20  owid = iRow;.** 
45820 3c 2f 70 72 65 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a  </pre> {END}.**.
45830 2a 2a 20 49 66 20 74 68 65 20 66 6c 61 67 73 20  ** If the flags 
45840 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e  parameter is non
45850 2d 7a 65 72 6f 2c 20 74 68 65 20 74 68 65 20 42  -zero, the the B
45860 4c 4f 42 20 69 73 20 6f 70 65 6e 65 64 20 66 6f  LOB is opened fo
45870 72 20 72 65 61 64 0a 2a 2a 20 61 6e 64 20 77 72  r read.** and wr
45880 69 74 65 20 61 63 63 65 73 73 2e 20 49 66 20 69  ite access. If i
45890 74 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 20 42  t is zero, the B
458a0 4c 4f 42 20 69 73 20 6f 70 65 6e 65 64 20 66 6f  LOB is opened fo
458b0 72 20 72 65 61 64 20 61 63 63 65 73 73 2e 0a 2a  r read access..*
458c0 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74  *.** Note that t
458d0 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  he database name
458e0 20 69 73 20 6e 6f 74 20 74 68 65 20 66 69 6c 65   is not the file
458f0 6e 61 6d 65 20 74 68 61 74 20 63 6f 6e 74 61 69  name that contai
45900 6e 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61  ns.** the databa
45910 73 65 20 62 75 74 20 72 61 74 68 65 72 20 74 68  se but rather th
45920 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20  e symbolic name 
45930 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
45940 74 68 61 74 0a 2a 2a 20 69 73 20 61 73 73 69 67  that.** is assig
45950 6e 65 64 20 77 68 65 6e 20 74 68 65 20 64 61 74  ned when the dat
45960 61 62 61 73 65 20 69 73 20 63 6f 6e 6e 65 63 74  abase is connect
45970 65 64 20 75 73 69 6e 67 20 5b 41 54 54 41 43 48  ed using [ATTACH
45980 5d 2e 0a 2a 2a 20 46 6f 72 20 74 68 65 20 6d 61  ]..** For the ma
45990 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
459a0 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e  , the database n
459b0 61 6d 65 20 69 73 20 22 6d 61 69 6e 22 2e 0a 2a  ame is "main"..*
459c0 2a 20 46 6f 72 20 54 45 4d 50 20 74 61 62 6c 65  * For TEMP table
459d0 73 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20  s, the database 
459e0 6e 61 6d 65 20 69 73 20 22 74 65 6d 70 22 2e 0a  name is "temp"..
459f0 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73  **.** On success
45a00 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 73  , [SQLITE_OK] is
45a10 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68   returned and th
45a20 65 20 6e 65 77 20 5b 42 4c 4f 42 20 68 61 6e 64  e new [BLOB hand
45a30 6c 65 5d 20 69 73 20 77 72 69 74 74 65 6e 0a 2a  le] is written.*
45a40 2a 20 74 6f 20 2a 70 70 42 6c 6f 62 2e 20 4f 74  * to *ppBlob. Ot
45a50 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 6f  herwise an [erro
45a60 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72  r code] is retur
45a70 6e 65 64 20 61 6e 64 20 61 6e 79 20 76 61 6c 75  ned and any valu
45a80 65 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20  e written.** to 
45a90 2a 70 70 42 6c 6f 62 20 73 68 6f 75 6c 64 20 6e  *ppBlob should n
45aa0 6f 74 20 62 65 20 75 73 65 64 20 62 79 20 74 68  ot be used by th
45ab0 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a 20 54 68 69  e caller..** Thi
45ac0 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 20  s function sets 
45ad0 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  the [database co
45ae0 6e 6e 65 63 74 69 6f 6e 5d 20 65 72 72 6f 72 20  nnection] error 
45af0 63 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67 65  code and message
45b00 0a 2a 2a 20 61 63 63 65 73 73 69 62 6c 65 20 76  .** accessible v
45b10 69 61 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63  ia [sqlite3_errc
45b20 6f 64 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69  ode()] and [sqli
45b30 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a  te3_errmsg()]..*
45b40 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 6f 77 20  *.** If the row 
45b50 74 68 61 74 20 61 20 42 4c 4f 42 20 68 61 6e 64  that a BLOB hand
45b60 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20  le points to is 
45b70 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e 0a 2a  modified by an.*
45b80 2a 20 5b 55 50 44 41 54 45 5d 2c 20 5b 44 45 4c  * [UPDATE], [DEL
45b90 45 54 45 5d 2c 20 6f 72 20 62 79 20 5b 4f 4e 20  ETE], or by [ON 
45ba0 43 4f 4e 46 4c 49 43 54 5d 20 73 69 64 65 2d 65  CONFLICT] side-e
45bb0 66 66 65 63 74 73 0a 2a 2a 20 74 68 65 6e 20 74  ffects.** then t
45bc0 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65 20 69  he BLOB handle i
45bd0 73 20 6d 61 72 6b 65 64 20 61 73 20 22 65 78 70  s marked as "exp
45be0 69 72 65 64 22 2e 0a 2a 2a 20 54 68 69 73 20 69  ired"..** This i
45bf0 73 20 74 72 75 65 20 69 66 20 61 6e 79 20 63 6f  s true if any co
45c00 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 6f 77 20  lumn of the row 
45c10 69 73 20 63 68 61 6e 67 65 64 2c 20 65 76 65 6e  is changed, even
45c20 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 74 68   a column.** oth
45c30 65 72 20 74 68 61 6e 20 74 68 65 20 6f 6e 65 20  er than the one 
45c40 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65 20  the BLOB handle 
45c50 69 73 20 6f 70 65 6e 20 6f 6e 2e 0a 2a 2a 20 43  is open on..** C
45c60 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33  alls to [sqlite3
45c70 5f 62 6c 6f 62 5f 72 65 61 64 28 29 5d 20 61 6e  _blob_read()] an
45c80 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  d [sqlite3_blob_
45c90 77 72 69 74 65 28 29 5d 20 66 6f 72 0a 2a 2a 20  write()] for.** 
45ca0 61 20 65 78 70 69 72 65 64 20 42 4c 4f 42 20 68  a expired BLOB h
45cb0 61 6e 64 6c 65 20 66 61 69 6c 20 77 69 74 68 20  andle fail with 
45cc0 61 6e 20 72 65 74 75 72 6e 20 63 6f 64 65 20 6f  an return code o
45cd0 66 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d  f [SQLITE_ABORT]
45ce0 2e 0a 2a 2a 20 43 68 61 6e 67 65 73 20 77 72 69  ..** Changes wri
45cf0 74 74 65 6e 20 69 6e 74 6f 20 61 20 42 4c 4f 42  tten into a BLOB
45d00 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 42 4c   prior to the BL
45d10 4f 42 20 65 78 70 69 72 69 6e 67 20 61 72 65 20  OB expiring are 
45d20 6e 6f 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20  not.** rollback 
45d30 62 79 20 74 68 65 20 65 78 70 69 72 61 74 69 6f  by the expiratio
45d40 6e 20 6f 66 20 74 68 65 20 42 4c 4f 42 2e 20 20  n of the BLOB.  
45d50 53 75 63 68 20 63 68 61 6e 67 65 73 20 77 69 6c  Such changes wil
45d60 6c 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20  l eventually.** 
45d70 63 6f 6d 6d 69 74 20 69 66 20 74 68 65 20 74 72  commit if the tr
45d80 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6e 74 69 6e  ansaction contin
45d90 75 65 73 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f  ues to completio
45da0 6e 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  n..**.** INVARIA
45db0 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38  NTS:.**.** {H178
45dc0 31 33 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c  13} A successful
45dd0 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74   invocation of t
45de0 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  he [sqlite3_blob
45df0 5f 6f 70 65 6e 28 44 2c 42 2c 54 2c 43 2c 52 2c  _open(D,B,T,C,R,
45e00 46 2c 50 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20  F,P)].**        
45e10 20 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c    interface shal
45e20 6c 20 6f 70 65 6e 20 61 6e 20 5b 73 71 6c 69 74  l open an [sqlit
45e30 65 33 5f 62 6c 6f 62 5d 20 6f 62 6a 65 63 74 20  e3_blob] object 
45e40 50 20 6f 6e 20 74 68 65 20 42 4c 4f 42 0a 2a 2a  P on the BLOB.**
45e50 20 20 20 20 20 20 20 20 20 20 69 6e 20 63 6f 6c            in col
45e60 75 6d 6e 20 43 20 6f 66 20 74 68 65 20 74 61 62  umn C of the tab
45e70 6c 65 20 54 20 69 6e 20 74 68 65 20 64 61 74 61  le T in the data
45e80 62 61 73 65 20 42 20 6f 6e 0a 2a 2a 20 20 20 20  base B on.**    
45e90 20 20 20 20 20 20 74 68 65 20 5b 64 61 74 61 62        the [datab
45ea0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
45eb0 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 31 34  D..**.** {H17814
45ec0 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 69  } A successful i
45ed0 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71  nvocation of [sq
45ee0 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28  lite3_blob_open(
45ef0 44 2c 2e 2e 2e 29 5d 20 73 68 61 6c 6c 20 73 74  D,...)] shall st
45f00 61 72 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  art.**          
45f10 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69 6f  a new transactio
45f20 6e 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61  n on the [databa
45f30 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44  se connection] D
45f40 20 69 66 20 74 68 61 74 0a 2a 2a 20 20 20 20 20   if that.**     
45f50 20 20 20 20 20 63 6f 6e 6e 65 63 74 69 6f 6e 20       connection 
45f60 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69  is not already i
45f70 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  n a transaction.
45f80 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 31 36 7d 20  .**.** {H17816} 
45f90 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f  The [sqlite3_blo
45fa0 62 5f 6f 70 65 6e 28 44 2c 42 2c 54 2c 43 2c 52  b_open(D,B,T,C,R
45fb0 2c 46 2c 50 29 5d 20 69 6e 74 65 72 66 61 63 65  ,F,P)] interface
45fc0 20 73 68 61 6c 6c 20 6f 70 65 6e 0a 2a 2a 20 20   shall open.**  
45fd0 20 20 20 20 20 20 20 20 74 68 65 20 42 4c 4f 42          the BLOB
45fe0 20 66 6f 72 20 72 65 61 64 20 61 6e 64 20 77 72   for read and wr
45ff0 69 74 65 20 61 63 63 65 73 73 20 69 66 20 61 6e  ite access if an
46000 64 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 46 0a  d only if the F.
46010 2a 2a 20 20 20 20 20 20 20 20 20 20 70 61 72 61  **          para
46020 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72  meter is non-zer
46030 6f 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 31 39  o..**.** {H17819
46040 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62  } The [sqlite3_b
46050 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 69 6e 74 65  lob_open()] inte
46060 72 66 61 63 65 20 73 68 61 6c 6c 20 72 65 74 75  rface shall retu
46070 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f  rn [SQLITE_OK] o
46080 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 75  n.**          su
46090 63 63 65 73 73 20 61 6e 64 20 61 6e 20 61 70 70  ccess and an app
460a0 72 6f 70 72 69 61 74 65 20 5b 65 72 72 6f 72 20  ropriate [error 
460b0 63 6f 64 65 5d 20 6f 6e 20 66 61 69 6c 75 72 65  code] on failure
460c0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 32 31 7d  ..**.** {H17821}
460d0 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
460e0 75 72 73 20 64 75 72 69 6e 67 20 65 76 61 6c 75  urs during evalu
460f0 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65  ation of [sqlite
46100 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 44 2c 2e 2e  3_blob_open(D,..
46110 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .)].**          
46120 74 68 65 6e 20 73 75 62 73 65 71 75 65 6e 74 20  then subsequent 
46130 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65  calls to [sqlite
46140 33 5f 65 72 72 63 6f 64 65 28 44 29 5d 2c 0a 2a  3_errcode(D)],.*
46150 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
46160 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72  te3_extended_err
46170 63 6f 64 65 28 29 5d 2c 20 0a 2a 2a 20 20 20 20  code()], .**    
46180 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65        [sqlite3_e
46190 72 72 6d 73 67 28 44 29 5d 2c 20 61 6e 64 20 5b  rrmsg(D)], and [
461a0 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36  sqlite3_errmsg16
461b0 28 44 29 5d 20 73 68 61 6c 6c 20 72 65 74 75 72  (D)] shall retur
461c0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e  n.**          in
461d0 66 6f 72 6d 61 74 69 6f 6e 20 61 70 70 72 6f 70  formation approp
461e0 72 69 61 74 65 20 66 6f 72 20 74 68 61 74 20 65  riate for that e
461f0 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37  rror..**.** {H17
46200 38 32 34 7d 20 49 66 20 61 6e 79 20 63 6f 6c 75  824} If any colu
46210 6d 6e 20 69 6e 20 74 68 65 20 72 6f 77 20 74 68  mn in the row th
46220 61 74 20 61 20 5b 73 71 6c 69 74 65 33 5f 62 6c  at a [sqlite3_bl
46230 6f 62 5d 20 68 61 73 20 6f 70 65 6e 20 69 73 0a  ob] has open is.
46240 2a 2a 20 20 20 20 20 20 20 20 20 20 63 68 61 6e  **          chan
46250 67 65 64 20 62 79 20 61 20 73 65 70 61 72 61 74  ged by a separat
46260 65 20 5b 55 50 44 41 54 45 5d 20 6f 72 20 5b 44  e [UPDATE] or [D
46270 45 4c 45 54 45 5d 20 73 74 61 74 65 6d 65 6e 74  ELETE] statement
46280 20 6f 72 20 62 79 0a 2a 2a 20 20 20 20 20 20 20   or by.**       
46290 20 20 20 61 6e 20 5b 4f 4e 20 43 4f 4e 46 4c 49     an [ON CONFLI
462a0 43 54 5d 20 73 69 64 65 20 65 66 66 65 63 74 2c  CT] side effect,
462b0 20 74 68 65 6e 20 74 68 65 20 5b 73 71 6c 69 74   then the [sqlit
462c0 65 33 5f 62 6c 6f 62 5d 20 73 68 61 6c 6c 0a 2a  e3_blob] shall.*
462d0 2a 20 20 20 20 20 20 20 20 20 20 62 65 20 6d 61  *          be ma
462e0 72 6b 65 64 20 61 73 20 69 6e 76 61 6c 69 64 2e  rked as invalid.
462f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
46300 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  nt sqlite3_blob_
46310 6f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a  open(.  sqlite3*
46320 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
46330 7a 44 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  zDb,.  const cha
46340 72 20 2a 7a 54 61 62 6c 65 2c 0a 20 20 63 6f 6e  r *zTable,.  con
46350 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e  st char *zColumn
46360 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ,.  sqlite3_int6
46370 34 20 69 52 6f 77 2c 0a 20 20 69 6e 74 20 66 6c  4 iRow,.  int fl
46380 61 67 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  ags,.  sqlite3_b
46390 6c 6f 62 20 2a 2a 70 70 42 6c 6f 62 0a 29 3b 0a  lob **ppBlob.);.
463a0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
463b0 20 43 6c 6f 73 65 20 41 20 42 4c 4f 42 20 48 61   Close A BLOB Ha
463c0 6e 64 6c 65 20 7b 48 31 37 38 33 30 7d 20 3c 53  ndle {H17830} <S
463d0 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 43 6c 6f  30230>.**.** Clo
463e0 73 65 73 20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f  ses an open [BLO
463f0 42 20 68 61 6e 64 6c 65 5d 2e 0a 2a 2a 0a 2a 2a  B handle]..**.**
46400 20 43 6c 6f 73 69 6e 67 20 61 20 42 4c 4f 42 20   Closing a BLOB 
46410 73 68 61 6c 6c 20 63 61 75 73 65 20 74 68 65 20  shall cause the 
46420 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
46430 69 6f 6e 20 74 6f 20 63 6f 6d 6d 69 74 0a 2a 2a  ion to commit.**
46440 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f   if there are no
46450 20 6f 74 68 65 72 20 42 4c 4f 42 73 2c 20 6e 6f   other BLOBs, no
46460 20 70 65 6e 64 69 6e 67 20 70 72 65 70 61 72 65   pending prepare
46470 64 20 73 74 61 74 65 6d 65 6e 74 73 2c 20 61 6e  d statements, an
46480 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  d the.** databas
46490 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
464a0 69 6e 20 5b 61 75 74 6f 63 6f 6d 6d 69 74 20 6d  in [autocommit m
464b0 6f 64 65 5d 2e 0a 2a 2a 20 49 66 20 61 6e 79 20  ode]..** If any 
464c0 77 72 69 74 65 73 20 77 65 72 65 20 6d 61 64 65  writes were made
464d0 20 74 6f 20 74 68 65 20 42 4c 4f 42 2c 20 74 68   to the BLOB, th
464e0 65 79 20 6d 69 67 68 74 20 62 65 20 68 65 6c 64  ey might be held
464f0 20 69 6e 20 63 61 63 68 65 0a 2a 2a 20 75 6e 74   in cache.** unt
46500 69 6c 20 74 68 65 20 63 6c 6f 73 65 20 6f 70 65  il the close ope
46510 72 61 74 69 6f 6e 20 69 66 20 74 68 65 79 20 77  ration if they w
46520 69 6c 6c 20 66 69 74 2e 20 7b 45 4e 44 7d 0a 2a  ill fit. {END}.*
46530 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 74 68 65  *.** Closing the
46540 20 42 4c 4f 42 20 6f 66 74 65 6e 20 66 6f 72 63   BLOB often forc
46550 65 73 20 74 68 65 20 63 68 61 6e 67 65 73 0a 2a  es the changes.*
46560 2a 20 6f 75 74 20 74 6f 20 64 69 73 6b 20 61 6e  * out to disk an
46570 64 20 73 6f 20 69 66 20 61 6e 79 20 49 2f 4f 20  d so if any I/O 
46580 65 72 72 6f 72 73 20 6f 63 63 75 72 2c 20 74 68  errors occur, th
46590 65 79 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 6f  ey will likely o
465a0 63 63 75 72 0a 2a 2a 20 61 74 20 74 68 65 20 74  ccur.** at the t
465b0 69 6d 65 20 77 68 65 6e 20 74 68 65 20 42 4c 4f  ime when the BLO
465c0 42 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 7b 48  B is closed.  {H
465d0 31 37 38 33 33 7d 20 41 6e 79 20 65 72 72 6f 72  17833} Any error
465e0 73 20 74 68 61 74 20 6f 63 63 75 72 20 64 75 72  s that occur dur
465f0 69 6e 67 0a 2a 2a 20 63 6c 6f 73 69 6e 67 20 61  ing.** closing a
46600 72 65 20 72 65 70 6f 72 74 65 64 20 61 73 20 61  re reported as a
46610 20 6e 6f 6e 2d 7a 65 72 6f 20 72 65 74 75 72 6e   non-zero return
46620 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   value..**.** Th
46630 65 20 42 4c 4f 42 20 69 73 20 63 6c 6f 73 65 64  e BLOB is closed
46640 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c 6c 79   unconditionally
46650 2e 20 20 45 76 65 6e 20 69 66 20 74 68 69 73 20  .  Even if this 
46660 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 0a  routine returns.
46670 2a 2a 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  ** an error code
46680 2c 20 74 68 65 20 42 4c 4f 42 20 69 73 20 73 74  , the BLOB is st
46690 69 6c 6c 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a  ill closed..**.*
466a0 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a  * INVARIANTS:.**
466b0 0a 2a 2a 20 7b 48 31 37 38 33 33 7d 20 54 68 65  .** {H17833} The
466c0 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63   [sqlite3_blob_c
466d0 6c 6f 73 65 28 50 29 5d 20 69 6e 74 65 72 66 61  lose(P)] interfa
466e0 63 65 20 63 6c 6f 73 65 73 20 61 6e 20 5b 73 71  ce closes an [sq
466f0 6c 69 74 65 33 5f 62 6c 6f 62 5d 0a 2a 2a 20 20  lite3_blob].**  
46700 20 20 20 20 20 20 20 20 6f 62 6a 65 63 74 20 50          object P
46710 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e   previously open
46720 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65  ed using [sqlite
46730 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 2e 0a  3_blob_open()]..
46740 2a 2a 0a 2a 2a 20 7b 48 31 37 38 33 36 7d 20 43  **.** {H17836} C
46750 6c 6f 73 69 6e 67 20 61 6e 20 5b 73 71 6c 69 74  losing an [sqlit
46760 65 33 5f 62 6c 6f 62 5d 20 6f 62 6a 65 63 74 20  e3_blob] object 
46770 75 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20  using.**        
46780 20 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f    [sqlite3_blob_
46790 63 6c 6f 73 65 28 29 5d 20 73 68 61 6c 6c 20 63  close()] shall c
467a0 61 75 73 65 20 74 68 65 20 63 75 72 72 65 6e 74  ause the current
467b0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 0a   transaction to.
467c0 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6d 6d  **          comm
467d0 69 74 20 69 66 20 74 68 65 72 65 20 61 72 65 20  it if there are 
467e0 6e 6f 20 6f 74 68 65 72 20 6f 70 65 6e 20 5b 73  no other open [s
467f0 71 6c 69 74 65 33 5f 62 6c 6f 62 5d 20 6f 62 6a  qlite3_blob] obj
46800 65 63 74 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  ects.**         
46810 20 6f 72 20 5b 70 72 65 70 61 72 65 64 20 73 74   or [prepared st
46820 61 74 65 6d 65 6e 74 73 5d 20 6f 6e 20 74 68 65  atements] on the
46830 20 73 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20   same [database 
46840 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64 0a  connection] and.
46850 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20  **          the 
46860 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
46870 69 6f 6e 20 69 73 20 69 6e 20 5b 61 75 74 6f 63  ion is in [autoc
46880 6f 6d 6d 69 74 20 6d 6f 64 65 5d 2e 0a 2a 2a 0a  ommit mode]..**.
46890 2a 2a 20 7b 48 31 37 38 33 39 7d 20 54 68 65 20  ** {H17839} The 
468a0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c  [sqlite3_blob_cl
468b0 6f 73 65 28 50 29 5d 20 69 6e 74 65 72 66 61 63  ose(P)] interfac
468c0 65 73 20 73 68 61 6c 6c 20 63 6c 6f 73 65 20 74  es shall close t
468d0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  he.**          [
468e0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5d 20 6f 62  sqlite3_blob] ob
468f0 6a 65 63 74 20 50 20 75 6e 63 6f 6e 64 69 74 69  ject P unconditi
46900 6f 6e 61 6c 6c 79 2c 20 65 76 65 6e 20 69 66 0a  onally, even if.
46910 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
46920 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28  ite3_blob_close(
46930 50 29 5d 20 72 65 74 75 72 6e 73 20 73 6f 6d 65  P)] returns some
46940 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e  thing other than
46950 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2f   [SQLITE_OK]..*/
46960 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
46970 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f  sqlite3_blob_clo
46980 73 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20  se(sqlite3_blob 
46990 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  *);../*.** CAPI3
469a0 52 45 46 3a 20 52 65 74 75 72 6e 20 54 68 65 20  REF: Return The 
469b0 53 69 7a 65 20 4f 66 20 41 6e 20 4f 70 65 6e 20  Size Of An Open 
469c0 42 4c 4f 42 20 7b 48 31 37 38 34 30 7d 20 3c 53  BLOB {H17840} <S
469d0 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74  30230>.**.** Ret
469e0 75 72 6e 73 20 74 68 65 20 73 69 7a 65 20 69 6e  urns the size in
469f0 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 42 4c   bytes of the BL
46a00 4f 42 20 61 63 63 65 73 73 69 62 6c 65 20 76 69  OB accessible vi
46a10 61 20 74 68 65 20 6f 70 65 6e 0a 2a 2a 20 5b 5d  a the open.** []
46a20 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 6e 20  BLOB handle] in 
46a30 69 74 73 20 6f 6e 6c 79 20 61 72 67 75 6d 65 6e  its only argumen
46a40 74 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  t..**.** INVARIA
46a50 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38  NTS:.**.** {H178
46a60 34 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  43} The [sqlite3
46a70 5f 62 6c 6f 62 5f 62 79 74 65 73 28 50 29 5d 20  _blob_bytes(P)] 
46a80 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
46a90 73 20 74 68 65 20 73 69 7a 65 0a 2a 2a 20 20 20  s the size.**   
46aa0 20 20 20 20 20 20 20 69 6e 20 62 79 74 65 73 20         in bytes 
46ab0 6f 66 20 74 68 65 20 42 4c 4f 42 20 74 68 61 74  of the BLOB that
46ac0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c   the [sqlite3_bl
46ad0 6f 62 5d 20 6f 62 6a 65 63 74 20 50 0a 2a 2a 20  ob] object P.** 
46ae0 20 20 20 20 20 20 20 20 20 72 65 66 65 72 73 20           refers 
46af0 74 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  to..*/.SQLITE_AP
46b00 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c  I int sqlite3_bl
46b10 6f 62 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33  ob_bytes(sqlite3
46b20 5f 62 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  _blob *);../*.**
46b30 20 43 41 50 49 33 52 45 46 3a 20 52 65 61 64 20   CAPI3REF: Read 
46b40 44 61 74 61 20 46 72 6f 6d 20 41 20 42 4c 4f 42  Data From A BLOB
46b50 20 49 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 7b   Incrementally {
46b60 48 31 37 38 35 30 7d 20 3c 53 33 30 32 33 30 3e  H17850} <S30230>
46b70 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
46b80 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
46b90 72 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61  read data from a
46ba0 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e  n open [BLOB han
46bb0 64 6c 65 5d 20 69 6e 74 6f 20 61 0a 2a 2a 20 63  dle] into a.** c
46bc0 61 6c 6c 65 72 2d 73 75 70 70 6c 69 65 64 20 62  aller-supplied b
46bd0 75 66 66 65 72 2e 20 4e 20 62 79 74 65 73 20 6f  uffer. N bytes o
46be0 66 20 64 61 74 61 20 61 72 65 20 63 6f 70 69 65  f data are copie
46bf0 64 20 69 6e 74 6f 20 62 75 66 66 65 72 20 5a 0a  d into buffer Z.
46c00 2a 2a 20 66 72 6f 6d 20 74 68 65 20 6f 70 65 6e  ** from the open
46c10 20 42 4c 4f 42 2c 20 73 74 61 72 74 69 6e 67 20   BLOB, starting 
46c20 61 74 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65  at offset iOffse
46c30 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6f 66 66 73  t..**.** If offs
46c40 65 74 20 69 4f 66 66 73 65 74 20 69 73 20 6c 65  et iOffset is le
46c50 73 73 20 74 68 61 6e 20 4e 20 62 79 74 65 73 20  ss than N bytes 
46c60 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66 20  from the end of 
46c70 74 68 65 20 42 4c 4f 42 2c 0a 2a 2a 20 5b 53 51  the BLOB,.** [SQ
46c80 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 72  LITE_ERROR] is r
46c90 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64  eturned and no d
46ca0 61 74 61 20 69 73 20 72 65 61 64 2e 20 20 49 66  ata is read.  If
46cb0 20 4e 20 6f 72 20 69 4f 66 66 73 65 74 20 69 73   N or iOffset is
46cc0 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 7a 65  .** less than ze
46cd0 72 6f 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f  ro, [SQLITE_ERRO
46ce0 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  R] is returned a
46cf0 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 72 65  nd no data is re
46d00 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74 74  ad..**.** An att
46d10 65 6d 70 74 20 74 6f 20 72 65 61 64 20 66 72 6f  empt to read fro
46d20 6d 20 61 6e 20 65 78 70 69 72 65 64 20 5b 42 4c  m an expired [BL
46d30 4f 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c 73  OB handle] fails
46d40 20 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72 6f   with an.** erro
46d50 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54  r code of [SQLIT
46d60 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20  E_ABORT]..**.** 
46d70 4f 6e 20 73 75 63 63 65 73 73 2c 20 53 51 4c 49  On success, SQLI
46d80 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
46d90 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  d..** Otherwise,
46da0 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d   an [error code]
46db0 20 6f 72 20 61 6e 20 5b 65 78 74 65 6e 64 65 64   or an [extended
46dc0 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20   error code] is 
46dd0 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
46de0 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a  INVARIANTS:.**.*
46df0 2a 20 7b 48 31 37 38 35 33 7d 20 41 20 73 75 63  * {H17853} A suc
46e00 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 74 69  cessful invocati
46e10 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 62  on of [sqlite3_b
46e20 6c 6f 62 5f 72 65 61 64 28 50 2c 5a 2c 4e 2c 58  lob_read(P,Z,N,X
46e30 29 5d 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  )] .**          
46e40 73 68 61 6c 6c 20 72 65 61 64 73 20 4e 20 62 79  shall reads N by
46e50 74 65 73 20 6f 66 20 64 61 74 61 20 6f 75 74 20  tes of data out 
46e60 6f 66 20 74 68 65 20 42 4c 4f 42 20 72 65 66 65  of the BLOB refe
46e70 72 65 6e 63 65 64 20 62 79 0a 2a 2a 20 20 20 20  renced by.**    
46e80 20 20 20 20 20 20 5b 42 4c 4f 42 20 68 61 6e 64        [BLOB hand
46e90 6c 65 5d 20 50 20 62 65 67 69 6e 6e 69 6e 67 20  le] P beginning 
46ea0 61 74 20 6f 66 66 73 65 74 20 58 20 61 6e 64 20  at offset X and 
46eb0 73 74 6f 72 65 20 74 68 6f 73 65 20 62 79 74 65  store those byte
46ec0 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e  s.**          in
46ed0 74 6f 20 62 75 66 66 65 72 20 5a 2e 0a 2a 2a 0a  to buffer Z..**.
46ee0 2a 2a 20 7b 48 31 37 38 35 36 7d 20 49 6e 20 5b  ** {H17856} In [
46ef0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61  sqlite3_blob_rea
46f00 64 28 50 2c 5a 2c 4e 2c 58 29 5d 20 69 66 20 74  d(P,Z,N,X)] if t
46f10 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 42  he size of the B
46f20 4c 4f 42 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  LOB.**          
46f30 69 73 20 6c 65 73 73 20 74 68 61 6e 20 4e 2b 58  is less than N+X
46f40 20 62 79 74 65 73 2c 20 74 68 65 6e 20 74 68 65   bytes, then the
46f50 20 66 75 6e 63 74 69 6f 6e 20 73 68 61 6c 6c 20   function shall 
46f60 6c 65 61 76 65 20 74 68 65 0a 2a 2a 20 20 20 20  leave the.**    
46f70 20 20 20 20 20 20 5a 20 62 75 66 66 65 72 20 75        Z buffer u
46f80 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 72 65 74  nchanged and ret
46f90 75 72 6e 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f  urn [SQLITE_ERRO
46fa0 52 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 35  R]..**.** {H1785
46fb0 39 7d 20 49 6e 20 5b 73 71 6c 69 74 65 33 5f 62  9} In [sqlite3_b
46fc0 6c 6f 62 5f 72 65 61 64 28 50 2c 5a 2c 4e 2c 58  lob_read(P,Z,N,X
46fd0 29 5d 20 69 66 20 58 20 6f 72 20 4e 20 69 73 20  )] if X or N is 
46fe0 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 0a 2a  less than zero.*
46ff0 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 6e 20  *          then 
47000 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 73 68 61  the function sha
47010 6c 6c 20 6c 65 61 76 65 20 74 68 65 20 5a 20 62  ll leave the Z b
47020 75 66 66 65 72 20 75 6e 63 68 61 6e 67 65 64 0a  uffer unchanged.
47030 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e 64 20  **          and 
47040 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 45  return [SQLITE_E
47050 52 52 4f 52 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  RROR]..**.** {H1
47060 37 38 36 32 7d 20 54 68 65 20 5b 73 71 6c 69 74  7862} The [sqlit
47070 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 50 2c 5a  e3_blob_read(P,Z
47080 2c 4e 2c 58 29 5d 20 69 6e 74 65 72 66 61 63 65  ,N,X)] interface
47090 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 20 5b 53   shall return [S
470a0 51 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 20 20 20  QLITE_OK].**    
470b0 20 20 20 20 20 20 69 66 20 4e 20 62 79 74 65 73        if N bytes
470c0 20 61 72 65 20 73 75 63 63 65 73 73 66 75 6c 6c   are successfull
470d0 79 20 72 65 61 64 20 69 6e 74 6f 20 62 75 66 66  y read into buff
470e0 65 72 20 5a 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37  er Z..**.** {H17
470f0 38 36 33 7d 20 49 66 20 74 68 65 20 5b 42 4c 4f  863} If the [BLO
47100 42 20 68 61 6e 64 6c 65 5d 20 50 20 69 73 20 65  B handle] P is e
47110 78 70 69 72 65 64 20 61 6e 64 20 58 20 61 6e 64  xpired and X and
47120 20 4e 20 61 72 65 20 77 69 74 68 69 6e 20 62 6f   N are within bo
47130 75 6e 64 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  unds.**         
47140 20 74 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 62   then [sqlite3_b
47150 6c 6f 62 5f 72 65 61 64 28 50 2c 5a 2c 4e 2c 58  lob_read(P,Z,N,X
47160 29 5d 20 73 68 61 6c 6c 20 6c 65 61 76 65 20 74  )] shall leave t
47170 68 65 20 5a 20 62 75 66 66 65 72 0a 2a 2a 20 20  he Z buffer.**  
47180 20 20 20 20 20 20 20 20 75 6e 63 68 61 6e 67 65          unchange
47190 64 20 61 6e 64 20 72 65 74 75 72 6e 20 5b 53 51  d and return [SQ
471a0 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a  LITE_ABORT]..**.
471b0 2a 2a 20 7b 48 31 37 38 36 35 7d 20 49 66 20 74  ** {H17865} If t
471c0 68 65 20 72 65 71 75 65 73 74 65 64 20 72 65 61  he requested rea
471d0 64 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63  d could not be c
471e0 6f 6d 70 6c 65 74 65 64 2c 0a 2a 2a 20 20 20 20  ompleted,.**    
471f0 20 20 20 20 20 20 74 68 65 20 5b 73 71 6c 69 74        the [sqlit
47200 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 50 2c 5a  e3_blob_read(P,Z
47210 2c 4e 2c 58 29 5d 20 69 6e 74 65 72 66 61 63 65  ,N,X)] interface
47220 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 20 61 6e   shall return an
47230 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 70 70  .**          app
47240 72 6f 70 72 69 61 74 65 20 5b 65 72 72 6f 72 20  ropriate [error 
47250 63 6f 64 65 5d 20 6f 72 20 5b 65 78 74 65 6e 64  code] or [extend
47260 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a  ed error code]..
47270 2a 2a 0a 2a 2a 20 7b 48 31 37 38 36 38 7d 20 49  **.** {H17868} I
47280 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
47290 73 20 64 75 72 69 6e 67 20 65 76 61 6c 75 61 74  s during evaluat
472a0 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f  ion of [sqlite3_
472b0 62 6c 6f 62 5f 72 65 61 64 28 50 2c 2e 2e 2e 29  blob_read(P,...)
472c0 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  ].**          th
472d0 65 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 61  en subsequent ca
472e0 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  lls to [sqlite3_
472f0 65 72 72 63 6f 64 65 28 44 29 5d 2c 0a 2a 2a 20  errcode(D)],.** 
47300 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
47310 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 63 6f  3_extended_errco
47320 64 65 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20  de()],.**       
47330 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d     [sqlite3_errm
47340 73 67 28 44 29 5d 2c 20 61 6e 64 20 5b 73 71 6c  sg(D)], and [sql
47350 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 44 29  ite3_errmsg16(D)
47360 5d 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a 2a  ] shall return.*
47370 2a 20 20 20 20 20 20 20 20 20 20 69 6e 66 6f 72  *          infor
47380 6d 61 74 69 6f 6e 20 61 70 70 72 6f 70 72 69 61  mation appropria
47390 74 65 20 66 6f 72 20 74 68 61 74 20 65 72 72 6f  te for that erro
473a0 72 2c 20 77 68 65 72 65 20 44 20 69 73 20 74 68  r, where D is th
473b0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 64  e.**          [d
473c0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
473d0 6f 6e 5d 20 74 68 61 74 20 77 61 73 20 75 73 65  on] that was use
473e0 64 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 5b 42  d to open the [B
473f0 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 50 2e 0a 2a  LOB handle] P..*
47400 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
47410 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65   sqlite3_blob_re
47420 61 64 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20  ad(sqlite3_blob 
47430 2a 2c 20 76 6f 69 64 20 2a 5a 2c 20 69 6e 74 20  *, void *Z, int 
47440 4e 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 29 3b  N, int iOffset);
47450 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
47460 3a 20 57 72 69 74 65 20 44 61 74 61 20 49 6e 74  : Write Data Int
47470 6f 20 41 20 42 4c 4f 42 20 49 6e 63 72 65 6d 65  o A BLOB Increme
47480 6e 74 61 6c 6c 79 20 7b 48 31 37 38 37 30 7d 20  ntally {H17870} 
47490 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S30230>.**.** T
474a0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
474b0 75 73 65 64 20 74 6f 20 77 72 69 74 65 20 64 61  used to write da
474c0 74 61 20 69 6e 74 6f 20 61 6e 20 6f 70 65 6e 20  ta into an open 
474d0 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66 72  [BLOB handle] fr
474e0 6f 6d 20 61 0a 2a 2a 20 63 61 6c 6c 65 72 2d 73  om a.** caller-s
474f0 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 2e 20  upplied buffer. 
47500 4e 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20  N bytes of data 
47510 61 72 65 20 63 6f 70 69 65 64 20 66 72 6f 6d 20  are copied from 
47520 74 68 65 20 62 75 66 66 65 72 20 5a 0a 2a 2a 20  the buffer Z.** 
47530 69 6e 74 6f 20 74 68 65 20 6f 70 65 6e 20 42 4c  into the open BL
47540 4f 42 2c 20 73 74 61 72 74 69 6e 67 20 61 74 20  OB, starting at 
47550 6f 66 66 73 65 74 20 69 4f 66 66 73 65 74 2e 0a  offset iOffset..
47560 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 5b 42 4c  **.** If the [BL
47570 4f 42 20 68 61 6e 64 6c 65 5d 20 70 61 73 73 65  OB handle] passe
47580 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61  d as the first a
47590 72 67 75 6d 65 6e 74 20 77 61 73 20 6e 6f 74 20  rgument was not 
475a0 6f 70 65 6e 65 64 20 66 6f 72 0a 2a 2a 20 77 72  opened for.** wr
475b0 69 74 69 6e 67 20 28 74 68 65 20 66 6c 61 67 73  iting (the flags
475c0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73   parameter to [s
475d0 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e  qlite3_blob_open
475e0 28 29 5d 20 77 61 73 20 7a 65 72 6f 29 2c 0a 2a  ()] was zero),.*
475f0 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * this function 
47600 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f  returns [SQLITE_
47610 52 45 41 44 4f 4e 4c 59 5d 2e 0a 2a 2a 0a 2a 2a  READONLY]..**.**
47620 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d   This function m
47630 61 79 20 6f 6e 6c 79 20 6d 6f 64 69 66 79 20 74  ay only modify t
47640 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
47650 68 65 20 42 4c 4f 42 3b 20 69 74 20 69 73 0a 2a  he BLOB; it is.*
47660 2a 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74  * not possible t
47670 6f 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 73  o increase the s
47680 69 7a 65 20 6f 66 20 61 20 42 4c 4f 42 20 75 73  ize of a BLOB us
47690 69 6e 67 20 74 68 69 73 20 41 50 49 2e 0a 2a 2a  ing this API..**
476a0 20 49 66 20 6f 66 66 73 65 74 20 69 4f 66 66 73   If offset iOffs
476b0 65 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20  et is less than 
476c0 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65  N bytes from the
476d0 20 65 6e 64 20 6f 66 20 74 68 65 20 42 4c 4f 42   end of the BLOB
476e0 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52  ,.** [SQLITE_ERR
476f0 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20  OR] is returned 
47700 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 77  and no data is w
47710 72 69 74 74 65 6e 2e 20 20 49 66 20 4e 20 69 73  ritten.  If N is
47720 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 7a 65  .** less than ze
47730 72 6f 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52  ro [SQLITE_ERROR
47740 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  ] is returned an
47750 64 20 6e 6f 20 64 61 74 61 20 69 73 20 77 72 69  d no data is wri
47760 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61  tten..**.** An a
47770 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20  ttempt to write 
47780 74 6f 20 61 6e 20 65 78 70 69 72 65 64 20 5b 42  to an expired [B
47790 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c  LOB handle] fail
477a0 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72  s with an.** err
477b0 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49  or code of [SQLI
477c0 54 45 5f 41 42 4f 52 54 5d 2e 20 20 57 72 69 74  TE_ABORT].  Writ
477d0 65 73 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 74  es to the BLOB t
477e0 68 61 74 20 6f 63 63 75 72 72 65 64 0a 2a 2a 20  hat occurred.** 
477f0 62 65 66 6f 72 65 20 74 68 65 20 5b 42 4c 4f 42  before the [BLOB
47800 20 68 61 6e 64 6c 65 5d 20 65 78 70 69 72 65 64   handle] expired
47810 20 61 72 65 20 6e 6f 74 20 72 6f 6c 6c 65 64 20   are not rolled 
47820 62 61 63 6b 20 62 79 20 74 68 65 0a 2a 2a 20 65  back by the.** e
47830 78 70 69 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  xpiration of the
47840 20 68 61 6e 64 6c 65 2c 20 74 68 6f 75 67 68 20   handle, though 
47850 6f 66 20 63 6f 75 72 73 65 20 74 68 6f 73 65 20  of course those 
47860 63 68 61 6e 67 65 73 20 6d 69 67 68 74 0a 2a 2a  changes might.**
47870 20 68 61 76 65 20 62 65 65 6e 20 6f 76 65 72 77   have been overw
47880 72 69 74 74 65 6e 20 62 79 20 74 68 65 20 73 74  ritten by the st
47890 61 74 65 6d 65 6e 74 20 74 68 61 74 20 65 78 70  atement that exp
478a0 69 72 65 64 20 74 68 65 20 42 4c 4f 42 20 68 61  ired the BLOB ha
478b0 6e 64 6c 65 0a 2a 2a 20 6f 72 20 62 79 20 6f 74  ndle.** or by ot
478c0 68 65 72 20 69 6e 64 65 70 65 6e 64 65 6e 74 20  her independent 
478d0 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a  statements..**.*
478e0 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 53 51  * On success, SQ
478f0 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
47900 6e 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ned..** Otherwis
47910 65 2c 20 61 6e 20 20 5b 65 72 72 6f 72 20 63 6f  e, an  [error co
47920 64 65 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65 6e  de] or an [exten
47930 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20  ded error code] 
47940 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
47950 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a  ** INVARIANTS:.*
47960 2a 0a 2a 2a 20 7b 48 31 37 38 37 33 7d 20 41 20  *.** {H17873} A 
47970 73 75 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63  successful invoc
47980 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65  ation of [sqlite
47990 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 50 2c 5a  3_blob_write(P,Z
479a0 2c 4e 2c 58 29 5d 0a 2a 2a 20 20 20 20 20 20 20  ,N,X)].**       
479b0 20 20 20 73 68 61 6c 6c 20 77 72 69 74 65 20 4e     shall write N
479c0 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 66   bytes of data f
479d0 72 6f 6d 20 62 75 66 66 65 72 20 5a 20 69 6e 74  rom buffer Z int
479e0 6f 20 74 68 65 20 42 4c 4f 42 20 0a 2a 2a 20 20  o the BLOB .**  
479f0 20 20 20 20 20 20 20 20 72 65 66 65 72 65 6e 63          referenc
47a00 65 64 20 62 79 20 5b 42 4c 4f 42 20 68 61 6e 64  ed by [BLOB hand
47a10 6c 65 5d 20 50 20 62 65 67 69 6e 6e 69 6e 67 20  le] P beginning 
47a20 61 74 20 6f 66 66 73 65 74 20 58 20 69 6e 74 6f  at offset X into
47a30 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65  .**          the
47a40 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31   BLOB..**.** {H1
47a50 37 38 37 34 7d 20 49 6e 20 74 68 65 20 61 62 73  7874} In the abs
47a60 65 6e 63 65 20 6f 66 20 6f 74 68 65 72 20 6f 76  ence of other ov
47a70 65 72 72 69 64 64 69 6e 67 20 63 68 61 6e 67 65  erridding change
47a80 73 2c 20 74 68 65 20 63 68 61 6e 67 65 73 0a 2a  s, the changes.*
47a90 2a 20 20 20 20 20 20 20 20 20 20 77 72 69 74 74  *          writt
47aa0 65 6e 20 74 6f 20 61 20 42 4c 4f 42 20 62 79 20  en to a BLOB by 
47ab0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72  [sqlite3_blob_wr
47ac0 69 74 65 28 29 5d 20 73 68 61 6c 6c 0a 2a 2a 20  ite()] shall.** 
47ad0 20 20 20 20 20 20 20 20 20 72 65 6d 61 69 6e 20           remain 
47ae0 69 6e 20 65 66 66 65 63 74 20 61 66 74 65 72 20  in effect after 
47af0 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 5b  the associated [
47b00 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 65 78 70  BLOB handle] exp
47b10 69 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37  ires..**.** {H17
47b20 38 37 35 7d 20 49 66 20 74 68 65 20 5b 42 4c 4f  875} If the [BLO
47b30 42 20 68 61 6e 64 6c 65 5d 20 50 20 77 61 73 20  B handle] P was 
47b40 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69  opened for readi
47b50 6e 67 20 6f 6e 6c 79 20 74 68 65 6e 0a 2a 2a 20  ng only then.** 
47b60 20 20 20 20 20 20 20 20 20 61 6e 20 69 6e 76 6f           an invo
47b70 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74  cation of [sqlit
47b80 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 50 2c  e3_blob_write(P,
47b90 5a 2c 4e 2c 58 29 5d 20 73 68 61 6c 6c 20 6c 65  Z,N,X)] shall le
47ba0 61 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ave.**          
47bb0 74 68 65 20 72 65 66 65 72 65 6e 63 65 64 20 42  the referenced B
47bc0 4c 4f 42 20 75 6e 63 68 61 6e 67 65 64 20 61 6e  LOB unchanged an
47bd0 64 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45  d return [SQLITE
47be0 5f 52 45 41 44 4f 4e 4c 59 5d 2e 0a 2a 2a 0a 2a  _READONLY]..**.*
47bf0 2a 20 7b 48 31 37 38 37 36 7d 20 49 66 20 74 68  * {H17876} If th
47c00 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 42 4c  e size of the BL
47c10 4f 42 20 72 65 66 65 72 65 6e 63 65 64 20 62 79  OB referenced by
47c20 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 50   [BLOB handle] P
47c30 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   is.**          
47c40 6c 65 73 73 20 74 68 61 6e 20 4e 2b 58 20 62 79  less than N+X by
47c50 74 65 73 20 74 68 65 6e 20 5b 73 71 6c 69 74 65  tes then [sqlite
47c60 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 50 2c 5a  3_blob_write(P,Z
47c70 2c 4e 2c 58 29 5d 20 73 68 61 6c 6c 0a 2a 2a 20  ,N,X)] shall.** 
47c80 20 20 20 20 20 20 20 20 20 6c 65 61 76 65 20 74           leave t
47c90 68 65 20 42 4c 4f 42 20 75 6e 63 68 61 6e 67 65  he BLOB unchange
47ca0 64 20 61 6e 64 20 72 65 74 75 72 6e 20 5b 53 51  d and return [SQ
47cb0 4c 49 54 45 5f 45 52 52 4f 52 5d 2e 0a 2a 2a 0a  LITE_ERROR]..**.
47cc0 2a 2a 20 7b 48 31 37 38 37 37 7d 20 49 66 20 74  ** {H17877} If t
47cd0 68 65 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d  he [BLOB handle]
47ce0 20 50 20 69 73 20 65 78 70 69 72 65 64 20 61 6e   P is expired an
47cf0 64 20 58 20 61 6e 64 20 4e 20 61 72 65 20 77 69  d X and N are wi
47d00 74 68 69 6e 20 62 6f 75 6e 64 73 0a 2a 2a 20 20  thin bounds.**  
47d10 20 20 20 20 20 20 20 20 74 68 65 6e 20 5b 73 71          then [sq
47d20 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28  lite3_blob_read(
47d30 50 2c 5a 2c 4e 2c 58 29 5d 20 73 68 61 6c 6c 20  P,Z,N,X)] shall 
47d40 6c 65 61 76 65 20 74 68 65 20 42 4c 4f 42 0a 2a  leave the BLOB.*
47d50 2a 20 20 20 20 20 20 20 20 20 20 75 6e 63 68 61  *          uncha
47d60 6e 67 65 64 20 61 6e 64 20 72 65 74 75 72 6e 20  nged and return 
47d70 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a  [SQLITE_ABORT]..
47d80 2a 2a 0a 2a 2a 20 7b 48 31 37 38 37 39 7d 20 49  **.** {H17879} I
47d90 66 20 58 20 6f 72 20 4e 20 61 72 65 20 6c 65 73  f X or N are les
47da0 73 20 74 68 61 6e 20 7a 65 72 6f 20 74 68 65 6e  s than zero then
47db0 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77   [sqlite3_blob_w
47dc0 72 69 74 65 28 50 2c 5a 2c 4e 2c 58 29 5d 0a 2a  rite(P,Z,N,X)].*
47dd0 2a 20 20 20 20 20 20 20 20 20 20 73 68 61 6c 6c  *          shall
47de0 20 6c 65 61 76 65 20 74 68 65 20 42 4c 4f 42 20   leave the BLOB 
47df0 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 5b 42  referenced by [B
47e00 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 50 20 75 6e  LOB handle] P un
47e10 63 68 61 6e 67 65 64 0a 2a 2a 20 20 20 20 20 20  changed.**      
47e20 20 20 20 20 61 6e 64 20 72 65 74 75 72 6e 20 5b      and return [
47e30 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 2e 0a 2a  SQLITE_ERROR]..*
47e40 2a 0a 2a 2a 20 7b 48 31 37 38 38 32 7d 20 54 68  *.** {H17882} Th
47e50 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  e [sqlite3_blob_
47e60 77 72 69 74 65 28 50 2c 5a 2c 4e 2c 58 29 5d 20  write(P,Z,N,X)] 
47e70 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20  interface shall 
47e80 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20  return.**       
47e90 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69     [SQLITE_OK] i
47ea0 66 20 4e 20 62 79 74 65 73 20 77 68 65 72 65 20  f N bytes where 
47eb0 73 75 63 63 65 73 73 66 75 6c 6c 79 20 77 72 69  successfully wri
47ec0 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 42 4c  tten into the BL
47ed0 4f 42 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 38  OB..**.** {H1788
47ee0 35 7d 20 49 66 20 74 68 65 20 72 65 71 75 65 73  5} If the reques
47ef0 74 65 64 20 77 72 69 74 65 20 63 6f 75 6c 64 20  ted write could 
47f00 6e 6f 74 20 62 65 20 63 6f 6d 70 6c 65 74 65 64  not be completed
47f10 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  ,.**          th
47f20 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  e [sqlite3_blob_
47f30 77 72 69 74 65 28 50 2c 5a 2c 4e 2c 58 29 5d 20  write(P,Z,N,X)] 
47f40 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20  interface shall 
47f50 72 65 74 75 72 6e 20 61 6e 0a 2a 2a 20 20 20 20  return an.**    
47f60 20 20 20 20 20 20 61 70 70 72 6f 70 72 69 61 74        appropriat
47f70 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f  e [error code] o
47f80 72 20 5b 65 78 74 65 6e 64 65 64 20 65 72 72 6f  r [extended erro
47f90 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a 20 7b  r code]..**.** {
47fa0 48 31 37 38 38 38 7d 20 49 66 20 61 6e 20 65 72  H17888} If an er
47fb0 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e  ror occurs durin
47fc0 67 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20  g evaluation of 
47fd0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72  [sqlite3_blob_wr
47fe0 69 74 65 28 44 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20  ite(D,...)].**  
47ff0 20 20 20 20 20 20 20 20 74 68 65 6e 20 73 75 62          then sub
48000 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f  sequent calls to
48010 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64   [sqlite3_errcod
48020 65 28 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20  e(D)],.**       
48030 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 65     [sqlite3_exte
48040 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 29 5d 2c  nded_errcode()],
48050 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71  .**          [sq
48060 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 44 29 5d  lite3_errmsg(D)]
48070 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65  , and [sqlite3_e
48080 72 72 6d 73 67 31 36 28 44 29 5d 20 73 68 61 6c  rrmsg16(D)] shal
48090 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20  l return.**     
480a0 20 20 20 20 20 69 6e 66 6f 72 6d 61 74 69 6f 6e       information
480b0 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72   appropriate for
480c0 20 74 68 61 74 20 65 72 72 6f 72 2e 0a 2a 2f 0a   that error..*/.
480d0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
480e0 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74  qlite3_blob_writ
480f0 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a  e(sqlite3_blob *
48100 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c  , const void *z,
48110 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 4f 66 66   int n, int iOff
48120 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  set);../*.** CAP
48130 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 46  I3REF: Virtual F
48140 69 6c 65 20 53 79 73 74 65 6d 20 4f 62 6a 65 63  ile System Objec
48150 74 73 20 7b 48 31 31 32 30 30 7d 20 3c 53 32 30  ts {H11200} <S20
48160 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72  100>.**.** A vir
48170 74 75 61 6c 20 66 69 6c 65 73 79 73 74 65 6d 20  tual filesystem 
48180 28 56 46 53 29 20 69 73 20 61 6e 20 5b 73 71 6c  (VFS) is an [sql
48190 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74  ite3_vfs] object
481a0 0a 2a 2a 20 74 68 61 74 20 53 51 4c 69 74 65 20  .** that SQLite 
481b0 75 73 65 73 20 74 6f 20 69 6e 74 65 72 61 63 74  uses to interact
481c0 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 75 6e 64  .** with the und
481d0 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e  erlying operatin
481e0 67 20 73 79 73 74 65 6d 2e 20 20 4d 6f 73 74 20  g system.  Most 
481f0 53 51 4c 69 74 65 20 62 75 69 6c 64 73 20 63 6f  SQLite builds co
48200 6d 65 20 77 69 74 68 20 61 0a 2a 2a 20 73 69 6e  me with a.** sin
48210 67 6c 65 20 64 65 66 61 75 6c 74 20 56 46 53 20  gle default VFS 
48220 74 68 61 74 20 69 73 20 61 70 70 72 6f 70 72 69  that is appropri
48230 61 74 65 20 66 6f 72 20 74 68 65 20 68 6f 73 74  ate for the host
48240 20 63 6f 6d 70 75 74 65 72 2e 0a 2a 2a 20 4e 65   computer..** Ne
48250 77 20 56 46 53 65 73 20 63 61 6e 20 62 65 20 72  w VFSes can be r
48260 65 67 69 73 74 65 72 65 64 20 61 6e 64 20 65 78  egistered and ex
48270 69 73 74 69 6e 67 20 56 46 53 65 73 20 63 61 6e  isting VFSes can
48280 20 62 65 20 75 6e 72 65 67 69 73 74 65 72 65 64   be unregistered
48290 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ..** The followi
482a0 6e 67 20 69 6e 74 65 72 66 61 63 65 73 20 61 72  ng interfaces ar
482b0 65 20 70 72 6f 76 69 64 65 64 2e 0a 2a 2a 0a 2a  e provided..**.*
482c0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76 66  * The sqlite3_vf
482d0 73 5f 66 69 6e 64 28 29 20 69 6e 74 65 72 66 61  s_find() interfa
482e0 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  ce returns a poi
482f0 6e 74 65 72 20 74 6f 20 61 20 56 46 53 20 67 69  nter to a VFS gi
48300 76 65 6e 20 69 74 73 20 6e 61 6d 65 2e 0a 2a 2a  ven its name..**
48310 20 4e 61 6d 65 73 20 61 72 65 20 63 61 73 65 20   Names are case 
48320 73 65 6e 73 69 74 69 76 65 2e 0a 2a 2a 20 4e 61  sensitive..** Na
48330 6d 65 73 20 61 72 65 20 7a 65 72 6f 2d 74 65 72  mes are zero-ter
48340 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74  minated UTF-8 st
48350 72 69 6e 67 73 2e 0a 2a 2a 20 49 66 20 74 68 65  rings..** If the
48360 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2c 20  re is no match, 
48370 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69  a NULL pointer i
48380 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 49  s returned..** I
48390 66 20 7a 56 66 73 4e 61 6d 65 20 69 73 20 4e 55  f zVfsName is NU
483a0 4c 4c 20 74 68 65 6e 20 74 68 65 20 64 65 66 61  LL then the defa
483b0 75 6c 74 20 56 46 53 20 69 73 20 72 65 74 75 72  ult VFS is retur
483c0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 56  ned..**.** New V
483d0 46 53 65 73 20 61 72 65 20 72 65 67 69 73 74 65  FSes are registe
483e0 72 65 64 20 77 69 74 68 20 73 71 6c 69 74 65 33  red with sqlite3
483f0 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 29 2e  _vfs_register().
48400 0a 2a 2a 20 45 61 63 68 20 6e 65 77 20 56 46 53  .** Each new VFS
48410 20 62 65 63 6f 6d 65 73 20 74 68 65 20 64 65 66   becomes the def
48420 61 75 6c 74 20 56 46 53 20 69 66 20 74 68 65 20  ault VFS if the 
48430 6d 61 6b 65 44 66 6c 74 20 66 6c 61 67 20 69 73  makeDflt flag is
48440 20 73 65 74 2e 0a 2a 2a 20 54 68 65 20 73 61 6d   set..** The sam
48450 65 20 56 46 53 20 63 61 6e 20 62 65 20 72 65 67  e VFS can be reg
48460 69 73 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65  istered multiple
48470 20 74 69 6d 65 73 20 77 69 74 68 6f 75 74 20 69   times without i
48480 6e 6a 75 72 79 2e 0a 2a 2a 20 54 6f 20 6d 61 6b  njury..** To mak
48490 65 20 61 6e 20 65 78 69 73 74 69 6e 67 20 56 46  e an existing VF
484a0 53 20 69 6e 74 6f 20 74 68 65 20 64 65 66 61 75  S into the defau
484b0 6c 74 20 56 46 53 2c 20 72 65 67 69 73 74 65 72  lt VFS, register
484c0 20 69 74 20 61 67 61 69 6e 0a 2a 2a 20 77 69 74   it again.** wit
484d0 68 20 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 66  h the makeDflt f
484e0 6c 61 67 20 73 65 74 2e 20 20 49 66 20 74 77 6f  lag set.  If two
484f0 20 64 69 66 66 65 72 65 6e 74 20 56 46 53 65 73   different VFSes
48500 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 73 61 6d   with the.** sam
48510 65 20 6e 61 6d 65 20 61 72 65 20 72 65 67 69 73  e name are regis
48520 74 65 72 65 64 2c 20 74 68 65 20 62 65 68 61 76  tered, the behav
48530 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64  ior is undefined
48540 2e 20 20 49 66 20 61 0a 2a 2a 20 56 46 53 20 69  .  If a.** VFS i
48550 73 20 72 65 67 69 73 74 65 72 65 64 20 77 69 74  s registered wit
48560 68 20 61 20 6e 61 6d 65 20 74 68 61 74 20 69 73  h a name that is
48570 20 4e 55 4c 4c 20 6f 72 20 61 6e 20 65 6d 70 74   NULL or an empt
48580 79 20 73 74 72 69 6e 67 2c 0a 2a 2a 20 74 68 65  y string,.** the
48590 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69  n the behavior i
485a0 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a  s undefined..**.
485b0 2a 2a 20 55 6e 72 65 67 69 73 74 65 72 20 61 20  ** Unregister a 
485c0 56 46 53 20 77 69 74 68 20 74 68 65 20 73 71 6c  VFS with the sql
485d0 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73  ite3_vfs_unregis
485e0 74 65 72 28 29 20 69 6e 74 65 72 66 61 63 65 2e  ter() interface.
485f0 0a 2a 2a 20 49 66 20 74 68 65 20 64 65 66 61 75  .** If the defau
48600 6c 74 20 56 46 53 20 69 73 20 75 6e 72 65 67 69  lt VFS is unregi
48610 73 74 65 72 65 64 2c 20 61 6e 6f 74 68 65 72 20  stered, another 
48620 56 46 53 20 69 73 20 63 68 6f 73 65 6e 20 61 73  VFS is chosen as
48630 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74 2e  .** the default.
48640 20 20 54 68 65 20 63 68 6f 69 63 65 20 66 6f 72    The choice for
48650 20 74 68 65 20 6e 65 77 20 56 46 53 20 69 73 20   the new VFS is 
48660 61 72 62 69 74 72 61 72 79 2e 0a 2a 2a 0a 2a 2a  arbitrary..**.**
48670 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a   INVARIANTS:.**.
48680 2a 2a 20 7b 48 31 31 32 30 33 7d 20 54 68 65 20  ** {H11203} The 
48690 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e  [sqlite3_vfs_fin
486a0 64 28 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20  d(N)] interface 
486b0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
486c0 72 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20 20  r to the.**     
486d0 20 20 20 20 20 72 65 67 69 73 74 65 72 65 64 20       registered 
486e0 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62  [sqlite3_vfs] ob
486f0 6a 65 63 74 20 77 68 6f 73 65 20 6e 61 6d 65 20  ject whose name 
48700 65 78 61 63 74 6c 79 20 6d 61 74 63 68 65 73 0a  exactly matches.
48710 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20  **          the 
48720 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20  zero-terminated 
48730 55 54 46 2d 38 20 73 74 72 69 6e 67 20 4e 2c 20  UTF-8 string N, 
48740 6f 72 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55  or it returns NU
48750 4c 4c 20 69 66 0a 2a 2a 20 20 20 20 20 20 20 20  LL if.**        
48760 20 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61    there is no ma
48770 74 63 68 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 32  tch..**.** {H112
48780 30 36 7d 20 49 66 20 74 68 65 20 4e 20 70 61 72  06} If the N par
48790 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74  ameter to [sqlit
487a0 65 33 5f 76 66 73 5f 66 69 6e 64 28 4e 29 5d 20  e3_vfs_find(N)] 
487b0 69 73 20 4e 55 4c 4c 20 74 68 65 6e 0a 2a 2a 20  is NULL then.** 
487c0 20 20 20 20 20 20 20 20 20 74 68 65 20 66 75 6e           the fun
487d0 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20  ction returns a 
487e0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 64  pointer to the d
487f0 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f  efault [sqlite3_
48800 76 66 73 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20  vfs].**         
48810 20 6f 62 6a 65 63 74 20 69 66 20 74 68 65 72 65   object if there
48820 20 69 73 20 6f 6e 65 2c 20 6f 72 20 4e 55 4c 4c   is one, or NULL
48830 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   if there is no 
48840 64 65 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20  default.**      
48850 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 76 66 73      [sqlite3_vfs
48860 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20  ] object..**.** 
48870 7b 48 31 31 32 30 39 7d 20 54 68 65 20 5b 73 71  {H11209} The [sq
48880 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74  lite3_vfs_regist
48890 65 72 28 50 2c 46 29 5d 20 69 6e 74 65 72 66 61  er(P,F)] interfa
488a0 63 65 20 72 65 67 69 73 74 65 72 73 20 74 68 65  ce registers the
488b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 65 6c  .**          wel
488c0 6c 2d 66 6f 72 6d 65 64 20 5b 73 71 6c 69 74 65  l-formed [sqlite
488d0 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 20 50 20  3_vfs] object P 
488e0 75 73 69 6e 67 20 74 68 65 20 6e 61 6d 65 20 67  using the name g
488f0 69 76 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  iven.**         
48900 20 62 79 20 74 68 65 20 7a 4e 61 6d 65 20 66 69   by the zName fi
48910 65 6c 64 20 6f 66 20 74 68 65 20 6f 62 6a 65 63  eld of the objec
48920 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 32 31 32  t..**.** {H11212
48930 7d 20 55 73 69 6e 67 20 74 68 65 20 5b 73 71 6c  } Using the [sql
48940 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65  ite3_vfs_registe
48950 72 28 50 2c 46 29 5d 20 69 6e 74 65 72 66 61 63  r(P,F)] interfac
48960 65 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a 2a  e to register.**
48970 20 20 20 20 20 20 20 20 20 20 74 68 65 20 73 61            the sa
48980 6d 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d  me [sqlite3_vfs]
48990 20 6f 62 6a 65 63 74 20 6d 75 6c 74 69 70 6c 65   object multiple
489a0 20 74 69 6d 65 73 20 69 73 20 61 20 68 61 72 6d   times is a harm
489b0 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  less no-op..**.*
489c0 2a 20 7b 48 31 31 32 31 35 7d 20 54 68 65 20 5b  * {H11215} The [
489d0 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69  sqlite3_vfs_regi
489e0 73 74 65 72 28 50 2c 46 29 5d 20 69 6e 74 65 72  ster(P,F)] inter
489f0 66 61 63 65 20 6d 61 6b 65 73 20 74 68 65 20 5b  face makes the [
48a00 73 71 6c 69 74 65 33 5f 76 66 73 5d 0a 2a 2a 20  sqlite3_vfs].** 
48a10 20 20 20 20 20 20 20 20 20 6f 62 6a 65 63 74 20           object 
48a20 50 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73  P the default [s
48a30 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65  qlite3_vfs] obje
48a40 63 74 20 69 66 20 46 20 69 73 20 6e 6f 6e 2d 7a  ct if F is non-z
48a50 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 32  ero..**.** {H112
48a60 31 38 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  18} The [sqlite3
48a70 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 28  _vfs_unregister(
48a80 50 29 5d 20 69 6e 74 65 72 66 61 63 65 20 75 6e  P)] interface un
48a90 72 65 67 69 73 74 65 72 73 20 74 68 65 0a 2a 2a  registers the.**
48aa0 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
48ab0 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 20 50  e3_vfs] object P
48ac0 20 73 6f 20 74 68 61 74 20 69 74 20 69 73 20 6e   so that it is n
48ad0 6f 20 6c 6f 6e 67 65 72 20 72 65 74 75 72 6e 65  o longer returne
48ae0 64 20 62 79 0a 2a 2a 20 20 20 20 20 20 20 20 20  d by.**         
48af0 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
48b00 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 76 66  s to [sqlite3_vf
48b10 73 5f 66 69 6e 64 28 29 5d 2e 0a 2a 2f 0a 53 51  s_find()]..*/.SQ
48b20 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
48b30 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 5f 76 66  _vfs *sqlite3_vf
48b40 73 5f 66 69 6e 64 28 63 6f 6e 73 74 20 63 68 61  s_find(const cha
48b50 72 20 2a 7a 56 66 73 4e 61 6d 65 29 3b 0a 53 51  r *zVfsName);.SQ
48b60 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
48b70 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65  ite3_vfs_registe
48b80 72 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20  r(sqlite3_vfs*, 
48b90 69 6e 74 20 6d 61 6b 65 44 66 6c 74 29 3b 0a 53  int makeDflt);.S
48ba0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
48bb0 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69  lite3_vfs_unregi
48bc0 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73  ster(sqlite3_vfs
48bd0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  *);../*.** CAPI3
48be0 52 45 46 3a 20 4d 75 74 65 78 65 73 20 7b 48 31  REF: Mutexes {H1
48bf0 37 30 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a  7000} <S20000>.*
48c00 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20  *.** The SQLite 
48c10 63 6f 72 65 20 75 73 65 73 20 74 68 65 73 65 20  core uses these 
48c20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 74 68 72  routines for thr
48c30 65 61 64 0a 2a 2a 20 73 79 6e 63 68 72 6f 6e 69  ead.** synchroni
48c40 7a 61 74 69 6f 6e 2e 20 54 68 6f 75 67 68 20 74  zation. Though t
48c50 68 65 79 20 61 72 65 20 69 6e 74 65 6e 64 65 64  hey are intended
48c60 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a   for internal.**
48c70 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 2c 20   use by SQLite, 
48c80 63 6f 64 65 20 74 68 61 74 20 6c 69 6e 6b 73 20  code that links 
48c90 61 67 61 69 6e 73 74 20 53 51 4c 69 74 65 20 69  against SQLite i
48ca0 73 0a 2a 2a 20 70 65 72 6d 69 74 74 65 64 20 74  s.** permitted t
48cb0 6f 20 75 73 65 20 61 6e 79 20 6f 66 20 74 68 65  o use any of the
48cc0 73 65 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a  se routines..**.
48cd0 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20 73 6f  ** The SQLite so
48ce0 75 72 63 65 20 63 6f 64 65 20 63 6f 6e 74 61 69  urce code contai
48cf0 6e 73 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c  ns multiple impl
48d00 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 6f  ementations.** o
48d10 66 20 74 68 65 73 65 20 6d 75 74 65 78 20 72 6f  f these mutex ro
48d20 75 74 69 6e 65 73 2e 20 20 41 6e 20 61 70 70 72  utines.  An appr
48d30 6f 70 72 69 61 74 65 20 69 6d 70 6c 65 6d 65 6e  opriate implemen
48d40 74 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 65 6c  tation.** is sel
48d50 65 63 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61  ected automatica
48d60 6c 6c 79 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74  lly at compile-t
48d70 69 6d 65 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  ime.  The follow
48d80 69 6e 67 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  ing.** implement
48d90 61 74 69 6f 6e 73 20 61 72 65 20 61 76 61 69 6c  ations are avail
48da0 61 62 6c 65 20 69 6e 20 74 68 65 20 53 51 4c 69  able in the SQLi
48db0 74 65 20 63 6f 72 65 3a 0a 2a 2a 0a 2a 2a 20 3c  te core:.**.** <
48dc0 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51  ul>.** <li>   SQ
48dd0 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 2a  LITE_MUTEX_OS2.*
48de0 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f  * <li>   SQLITE_
48df0 4d 55 54 45 58 5f 50 54 48 52 45 41 44 0a 2a 2a  MUTEX_PTHREAD.**
48e00 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d   <li>   SQLITE_M
48e10 55 54 45 58 5f 57 33 32 0a 2a 2a 20 3c 6c 69 3e  UTEX_W32.** <li>
48e20 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f     SQLITE_MUTEX_
48e30 4e 4f 4f 50 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a  NOOP.** </ul>.**
48e40 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4d  .** The SQLITE_M
48e50 55 54 45 58 5f 4e 4f 4f 50 20 69 6d 70 6c 65 6d  UTEX_NOOP implem
48e60 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 20 73 65  entation is a se
48e70 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 0a 2a 2a  t of routines.**
48e80 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 20 72 65   that does no re
48e90 61 6c 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 69  al locking and i
48ea0 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f  s appropriate fo
48eb0 72 20 75 73 65 20 69 6e 0a 2a 2a 20 61 20 73 69  r use in.** a si
48ec0 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 61 70  ngle-threaded ap
48ed0 70 6c 69 63 61 74 69 6f 6e 2e 20 20 54 68 65 20  plication.  The 
48ee0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32  SQLITE_MUTEX_OS2
48ef0 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45  ,.** SQLITE_MUTE
48f00 58 5f 50 54 48 52 45 41 44 2c 20 61 6e 64 20 53  X_PTHREAD, and S
48f10 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 20  QLITE_MUTEX_W32 
48f20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a  implementations.
48f30 2a 2a 20 61 72 65 20 61 70 70 72 6f 70 72 69 61  ** are appropria
48f40 74 65 20 66 6f 72 20 75 73 65 20 6f 6e 20 4f 53  te for use on OS
48f50 2f 32 2c 20 55 6e 69 78 2c 20 61 6e 64 20 57 69  /2, Unix, and Wi
48f60 6e 64 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ndows..**.** If 
48f70 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c  SQLite is compil
48f80 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c 49  ed with the SQLI
48f90 54 45 5f 4d 55 54 45 58 5f 41 50 50 44 45 46 20  TE_MUTEX_APPDEF 
48fa0 70 72 65 70 72 6f 63 65 73 73 6f 72 0a 2a 2a 20  preprocessor.** 
48fb0 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 20 28 77  macro defined (w
48fc0 69 74 68 20 22 2d 44 53 51 4c 49 54 45 5f 4d 55  ith "-DSQLITE_MU
48fd0 54 45 58 5f 41 50 50 44 45 46 3d 31 22 29 2c 20  TEX_APPDEF=1"), 
48fe0 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 0a 2a 2a  then no mutex.**
48ff0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
49000 69 73 20 69 6e 63 6c 75 64 65 64 20 77 69 74 68  is included with
49010 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 49 6e   the library. In
49020 20 74 68 69 73 20 63 61 73 65 20 74 68 65 0a 2a   this case the.*
49030 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75  * application mu
49040 73 74 20 73 75 70 70 6c 79 20 61 20 63 75 73 74  st supply a cust
49050 6f 6d 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65  om mutex impleme
49060 6e 74 61 74 69 6f 6e 20 75 73 69 6e 67 20 74 68  ntation using th
49070 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e  e.** [SQLITE_CON
49080 46 49 47 5f 4d 55 54 45 58 5d 20 6f 70 74 69 6f  FIG_MUTEX] optio
49090 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  n of the sqlite3
490a0 5f 63 6f 6e 66 69 67 28 29 20 66 75 6e 63 74 69  _config() functi
490b0 6f 6e 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61 6c  on.** before cal
490c0 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e 69  ling sqlite3_ini
490d0 74 69 61 6c 69 7a 65 28 29 20 6f 72 20 61 6e 79  tialize() or any
490e0 20 6f 74 68 65 72 20 70 75 62 6c 69 63 20 73 71   other public sq
490f0 6c 69 74 65 33 5f 0a 2a 2a 20 66 75 6e 63 74 69  lite3_.** functi
49100 6f 6e 20 74 68 61 74 20 63 61 6c 6c 73 20 73 71  on that calls sq
49110 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
49120 28 29 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31  ()..**.** {H1701
49130 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  1} The sqlite3_m
49140 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75  utex_alloc() rou
49150 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61  tine allocates a
49160 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e   new.** mutex an
49170 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  d returns a poin
49180 74 65 72 20 74 6f 20 69 74 2e 20 7b 48 31 37 30  ter to it. {H170
49190 31 32 7d 20 49 66 20 69 74 20 72 65 74 75 72 6e  12} If it return
491a0 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d  s NULL.** that m
491b0 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65  eans that a mute
491c0 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61  x could not be a
491d0 6c 6c 6f 63 61 74 65 64 2e 20 7b 48 31 37 30 31  llocated. {H1701
491e0 33 7d 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c  3} SQLite.** wil
491f0 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73 74 61  l unwind its sta
49200 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e  ck and return an
49210 20 65 72 72 6f 72 2e 20 7b 48 31 37 30 31 34 7d   error. {H17014}
49220 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a   The argument.**
49230 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   to sqlite3_mute
49240 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65  x_alloc() is one
49250 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65   of these intege
49260 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a  r constants:.**.
49270 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20  ** <ul>.** <li> 
49280 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
49290 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  ST.** <li>  SQLI
492a0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
492b0 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  VE.** <li>  SQLI
492c0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
492d0 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20  MASTER.** <li>  
492e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
492f0 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20  TIC_MEM.** <li> 
49300 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
49310 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69  ATIC_MEM2.** <li
49320 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
49330 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c  STATIC_PRNG.** <
49340 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
49350 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a 20  X_STATIC_LRU.** 
49360 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
49370 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a 2a  EX_STATIC_LRU2.*
49380 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 7b 48  * </ul>.**.** {H
49390 31 37 30 31 35 7d 20 54 68 65 20 66 69 72 73 74  17015} The first
493a0 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63   two constants c
493b0 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 74  ause sqlite3_mut
493c0 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72  ex_alloc() to cr
493d0 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75  eate.** a new mu
493e0 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d 75  tex.  The new mu
493f0 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76 65  tex is recursive
49400 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54   when SQLITE_MUT
49410 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20  EX_RECURSIVE.** 
49420 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74 20  is used but not 
49430 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20 77  necessarily so w
49440 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  hen SQLITE_MUTEX
49450 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 20 7b  _FAST is used. {
49460 45 4e 44 7d 0a 2a 2a 20 54 68 65 20 6d 75 74 65  END}.** The mute
49470 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  x implementation
49480 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
49490 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63  o make a distinc
494a0 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20  tion.** between 
494b0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
494c0 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54  URSIVE and SQLIT
494d0 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20  E_MUTEX_FAST if 
494e0 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77  it does.** not w
494f0 61 6e 74 20 74 6f 2e 20 20 7b 48 31 37 30 31 36  ant to.  {H17016
49500 7d 20 42 75 74 20 53 51 4c 69 74 65 20 77 69 6c  } But SQLite wil
49510 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20 61  l only request a
49520 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78   recursive mutex
49530 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68 65   in.** cases whe
49540 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65 65  re it really nee
49550 64 73 20 6f 6e 65 2e 20 20 7b 45 4e 44 7d 20 49  ds one.  {END} I
49560 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72  f a faster non-r
49570 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a 2a  ecursive mutex.*
49580 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
49590 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e   is available on
495a0 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66 6f   the host platfo
495b0 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73 75  rm, the mutex su
495c0 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74  bsystem.** might
495d0 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d   return such a m
495e0 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65  utex in response
495f0 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58   to SQLITE_MUTEX
49600 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  _FAST..**.** {H1
49610 37 30 31 37 7d 20 54 68 65 20 6f 74 68 65 72 20  7017} The other 
49620 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65  allowed paramete
49630 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75  rs to sqlite3_mu
49640 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68  tex_alloc() each
49650 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69   return.** a poi
49660 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63  nter to a static
49670 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74   preexisting mut
49680 65 78 2e 20 7b 45 4e 44 7d 20 20 46 6f 75 72 20  ex. {END}  Four 
49690 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61  static mutexes a
496a0 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 68  re.** used by th
496b0 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 69 6f  e current versio
496c0 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 46 75  n of SQLite.  Fu
496d0 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66  ture versions of
496e0 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61   SQLite.** may a
496f0 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74  dd additional st
49700 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 20 53  atic mutexes.  S
49710 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 72  tatic mutexes ar
49720 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a  e for internal.*
49730 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 20  * use by SQLite 
49740 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 69  only.  Applicati
49750 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 51 4c  ons that use SQL
49760 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 6f 75  ite mutexes shou
49770 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74  ld.** use only t
49780 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78  he dynamic mutex
49790 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 53  es returned by S
497a0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
497b0 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55   or.** SQLITE_MU
497c0 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a  TEX_RECURSIVE..*
497d0 2a 0a 2a 2a 20 7b 48 31 37 30 31 38 7d 20 4e 6f  *.** {H17018} No
497e0 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f  te that if one o
497f0 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75  f the dynamic mu
49800 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 28  tex parameters (
49810 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
49820 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d  T.** or SQLITE_M
49830 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 20  UTEX_RECURSIVE) 
49840 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c  is used then sql
49850 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
49860 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20  ().** returns a 
49870 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20  different mutex 
49880 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20  on every call.  
49890 7b 48 31 37 30 33 34 7d 20 42 75 74 20 66 6f 72  {H17034} But for
498a0 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a 20 6d   the static.** m
498b0 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65 20  utex types, the 
498c0 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72 65  same mutex is re
498d0 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 20  turned on every 
498e0 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a  call that has.**
498f0 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20 6e   the same type n
49900 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  umber..**.** {H1
49910 37 30 31 39 7d 20 54 68 65 20 73 71 6c 69 74 65  7019} The sqlite
49920 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 29 20 72  3_mutex_free() r
49930 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74  outine deallocat
49940 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 0a  es a previously.
49950 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 64 79 6e  ** allocated dyn
49960 61 6d 69 63 20 6d 75 74 65 78 2e 20 7b 48 31 37  amic mutex. {H17
49970 30 32 30 7d 20 53 51 4c 69 74 65 20 69 73 20 63  020} SQLite is c
49980 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f  areful to deallo
49990 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 64 79  cate every.** dy
499a0 6e 61 6d 69 63 20 6d 75 74 65 78 20 74 68 61 74  namic mutex that
499b0 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 20 7b   it allocates. {
499c0 41 31 37 30 32 31 7d 20 54 68 65 20 64 79 6e 61  A17021} The dyna
499d0 6d 69 63 20 6d 75 74 65 78 65 73 20 6d 75 73 74  mic mutexes must
499e0 20 6e 6f 74 20 62 65 20 69 6e 0a 2a 2a 20 75 73   not be in.** us
499f0 65 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 20  e when they are 
49a00 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 41 31  deallocated. {A1
49a10 37 30 32 32 7d 20 41 74 74 65 6d 70 74 69 6e 67  7022} Attempting
49a20 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 61   to deallocate a
49a30 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 65 78   static.** mutex
49a40 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 65   results in unde
49a50 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 20  fined behavior. 
49a60 7b 48 31 37 30 32 33 7d 20 53 51 4c 69 74 65 20  {H17023} SQLite 
49a70 6e 65 76 65 72 20 64 65 61 6c 6c 6f 63 61 74 65  never deallocate
49a80 73 0a 2a 2a 20 61 20 73 74 61 74 69 63 20 6d 75  s.** a static mu
49a90 74 65 78 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a  tex. {END}.**.**
49aa0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
49ab0 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73  ex_enter() and s
49ac0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
49ad0 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65  () routines atte
49ae0 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20  mpt.** to enter 
49af0 61 20 6d 75 74 65 78 2e 20 7b 48 31 37 30 32 34  a mutex. {H17024
49b00 7d 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72  } If another thr
49b10 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77  ead is already w
49b20 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c  ithin the mutex,
49b30 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  .** sqlite3_mute
49b40 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62  x_enter() will b
49b50 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33  lock and sqlite3
49b60 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c  _mutex_try() wil
49b70 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49  l return.** SQLI
49b80 54 45 5f 42 55 53 59 2e 20 7b 48 31 37 30 32 35  TE_BUSY. {H17025
49b90 7d 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  }  The sqlite3_m
49ba0 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72  utex_try() inter
49bb0 66 61 63 65 20 72 65 74 75 72 6e 73 20 5b 53 51  face returns [SQ
49bc0 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 75 70 6f 6e  LITE_OK].** upon
49bd0 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72   successful entr
49be0 79 2e 20 20 7b 48 31 37 30 32 36 7d 20 4d 75 74  y.  {H17026} Mut
49bf0 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73 69  exes created usi
49c00 6e 67 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54  ng.** SQLITE_MUT
49c10 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e  EX_RECURSIVE can
49c20 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74   be entered mult
49c30 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68  iple times by th
49c40 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 0a 2a  e same thread..*
49c50 2a 20 7b 48 31 37 30 32 37 7d 20 49 6e 20 73 75  * {H17027} In su
49c60 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a  ch cases the,.**
49c70 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 65   mutex must be e
49c80 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 6e  xited an equal n
49c90 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 62  umber of times b
49ca0 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 68  efore another th
49cb0 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65  read.** can ente
49cc0 72 2e 20 20 7b 41 31 37 30 32 38 7d 20 49 66 20  r.  {A17028} If 
49cd0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20  the same thread 
49ce0 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61  tries to enter a
49cf0 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 6b 69 6e 64  ny other.** kind
49d00 20 6f 66 20 6d 75 74 65 78 20 6d 6f 72 65 20 74   of mutex more t
49d10 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65  han once, the be
49d20 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69  havior is undefi
49d30 6e 65 64 2e 0a 2a 2a 20 7b 48 31 37 30 32 39 7d  ned..** {H17029}
49d40 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76   SQLite will nev
49d50 65 72 20 65 78 68 69 62 69 74 0a 2a 2a 20 73 75  er exhibit.** su
49d60 63 68 20 62 65 68 61 76 69 6f 72 20 69 6e 20 69  ch behavior in i
49d70 74 73 20 6f 77 6e 20 75 73 65 20 6f 66 20 6d 75  ts own use of mu
49d80 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d  texes..**.** Som
49d90 65 20 73 79 73 74 65 6d 73 20 28 66 6f 72 20 65  e systems (for e
49da0 78 61 6d 70 6c 65 2c 20 57 69 6e 64 6f 77 73 20  xample, Windows 
49db0 39 35 29 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f  95) do not suppo
49dc0 72 74 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  rt the operation
49dd0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20  .** implemented 
49de0 62 79 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  by sqlite3_mutex
49df0 5f 74 72 79 28 29 2e 20 20 4f 6e 20 74 68 6f 73  _try().  On thos
49e00 65 20 73 79 73 74 65 6d 73 2c 20 73 71 6c 69 74  e systems, sqlit
49e10 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 0a 2a  e3_mutex_try().*
49e20 2a 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 72 65  * will always re
49e30 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
49e40 2e 20 20 7b 48 31 37 30 33 30 7d 20 54 68 65 20  .  {H17030} The 
49e50 53 51 4c 69 74 65 20 63 6f 72 65 20 6f 6e 6c 79  SQLite core only
49e60 20 65 76 65 72 20 75 73 65 73 0a 2a 2a 20 73 71   ever uses.** sq
49e70 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
49e80 29 20 61 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61  ) as an optimiza
49e90 74 69 6f 6e 20 73 6f 20 74 68 69 73 20 69 73 20  tion so this is 
49ea0 61 63 63 65 70 74 61 62 6c 65 20 62 65 68 61 76  acceptable behav
49eb0 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30  ior..**.** {H170
49ec0 33 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f  31} The sqlite3_
49ed0 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f  mutex_leave() ro
49ee0 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75  utine exits a mu
49ef0 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20  tex that was.** 
49f00 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72  previously enter
49f10 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74  ed by the same t
49f20 68 72 65 61 64 2e 20 20 7b 41 31 37 30 33 32 7d  hread.  {A17032}
49f30 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a   The behavior.**
49f40 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 66   is undefined if
49f50 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f   the mutex is no
49f60 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65  t currently ente
49f70 72 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63 61  red by the.** ca
49f80 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 6f 72 20  lling thread or 
49f90 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79  is not currently
49fa0 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 7b 48 31   allocated.  {H1
49fb0 37 30 33 33 7d 20 53 51 4c 69 74 65 20 77 69 6c  7033} SQLite wil
49fc0 6c 0a 2a 2a 20 6e 65 76 65 72 20 64 6f 20 65 69  l.** never do ei
49fd0 74 68 65 72 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a  ther. {END}.**.*
49fe0 2a 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e  * If the argumen
49ff0 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74  t to sqlite3_mut
4a000 65 78 5f 65 6e 74 65 72 28 29 2c 20 73 71 6c 69  ex_enter(), sqli
4a010 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 2c  te3_mutex_try(),
4a020 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d   or.** sqlite3_m
4a030 75 74 65 78 5f 6c 65 61 76 65 28 29 20 69 73 20  utex_leave() is 
4a040 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20  a NULL pointer, 
4a050 74 68 65 6e 20 61 6c 6c 20 74 68 72 65 65 20 72  then all three r
4a060 6f 75 74 69 6e 65 73 0a 2a 2a 20 62 65 68 61 76  outines.** behav
4a070 65 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a  e as no-ops..**.
4a080 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71  ** See also: [sq
4a090 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
4a0a0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
4a0b0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29  _mutex_notheld()
4a0c0 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  ]..*/.SQLITE_API
4a0d0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
4a0e0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
4a0f0 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45  loc(int);.SQLITE
4a100 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
4a110 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 71 6c  3_mutex_free(sql
4a120 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51  ite3_mutex*);.SQ
4a130 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
4a140 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
4a150 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a  r(sqlite3_mutex*
4a160 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
4a170 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  t sqlite3_mutex_
4a180 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  try(sqlite3_mute
4a190 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  x*);.SQLITE_API 
4a1a0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74  void sqlite3_mut
4a1b0 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33  ex_leave(sqlite3
4a1c0 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  _mutex*);../*.**
4a1d0 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78   CAPI3REF: Mutex
4a1e0 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65 63 74 20   Methods Object 
4a1f0 7b 48 31 37 31 32 30 7d 20 3c 53 32 30 31 33 30  {H17120} <S20130
4a200 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  >.** EXPERIMENTA
4a210 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  L.**.** An insta
4a220 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75  nce of this stru
4a230 63 74 75 72 65 20 64 65 66 69 6e 65 73 20 74 68  cture defines th
4a240 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74  e low-level rout
4a250 69 6e 65 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20  ines.** used to 
4a260 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 75 73 65  allocate and use
4a270 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20   mutexes..**.** 
4a280 55 73 75 61 6c 6c 79 2c 20 74 68 65 20 64 65 66  Usually, the def
4a290 61 75 6c 74 20 6d 75 74 65 78 20 69 6d 70 6c 65  ault mutex imple
4a2a0 6d 65 6e 74 61 74 69 6f 6e 73 20 70 72 6f 76 69  mentations provi
4a2b0 64 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 72  ded by SQLite ar
4a2c0 65 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 2c  e.** sufficient,
4a2d0 20 68 6f 77 65 76 65 72 20 74 68 65 20 75 73 65   however the use
4a2e0 72 20 68 61 73 20 74 68 65 20 6f 70 74 69 6f 6e  r has the option
4a2f0 20 6f 66 20 73 75 62 73 74 69 74 75 74 69 6e 67   of substituting
4a300 20 61 20 63 75 73 74 6f 6d 0a 2a 2a 20 69 6d 70   a custom.** imp
4a310 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20  lementation for 
4a320 73 70 65 63 69 61 6c 69 7a 65 64 20 64 65 70 6c  specialized depl
4a330 6f 79 6d 65 6e 74 73 20 6f 72 20 73 79 73 74 65  oyments or syste
4a340 6d 73 20 66 6f 72 20 77 68 69 63 68 20 53 51 4c  ms for which SQL
4a350 69 74 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20  ite.** does not 
4a360 70 72 6f 76 69 64 65 20 61 20 73 75 69 74 61 62  provide a suitab
4a370 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  le implementatio
4a380 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c  n. In this case,
4a390 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 63 72 65   the user.** cre
4a3a0 61 74 65 73 20 61 6e 64 20 70 6f 70 75 6c 61 74  ates and populat
4a3b0 65 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  es an instance o
4a3c0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
4a3d0 20 74 6f 20 70 61 73 73 0a 2a 2a 20 74 6f 20 73   to pass.** to s
4a3e0 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20  qlite3_config() 
4a3f0 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 5b  along with the [
4a400 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55  SQLITE_CONFIG_MU
4a410 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 20  TEX] option..** 
4a420 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 61 6e  Additionally, an
4a430 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
4a440 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e 20  s structure can 
4a450 62 65 20 75 73 65 64 20 61 73 20 61 6e 0a 2a 2a  be used as an.**
4a460 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65   output variable
4a470 20 77 68 65 6e 20 71 75 65 72 79 69 6e 67 20 74   when querying t
4a480 68 65 20 73 79 73 74 65 6d 20 66 6f 72 20 74 68  he system for th
4a490 65 20 63 75 72 72 65 6e 74 20 6d 75 74 65 78 0a  e current mutex.
4a4a0 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
4a4b0 6e 2c 20 75 73 69 6e 67 20 74 68 65 20 5b 53 51  n, using the [SQ
4a4c0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d  LITE_CONFIG_GETM
4a4d0 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a  UTEX] option..**
4a4e0 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 78 49 6e  .** The xMutexIn
4a4f0 69 74 20 6d 65 74 68 6f 64 20 64 65 66 69 6e 65  it method define
4a500 64 20 62 79 20 74 68 69 73 20 73 74 72 75 63 74  d by this struct
4a510 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 61  ure is invoked a
4a520 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73 79 73  s.** part of sys
4a530 74 65 6d 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  tem initializati
4a540 6f 6e 20 62 79 20 74 68 65 20 73 71 6c 69 74 65  on by the sqlite
4a550 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 66  3_initialize() f
4a560 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 7b 48 31 37  unction..** {H17
4a570 30 30 31 7d 20 54 68 65 20 78 4d 75 74 65 78 49  001} The xMutexI
4a580 6e 69 74 20 72 6f 75 74 69 6e 65 20 73 68 61 6c  nit routine shal
4a590 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 79 20 53  l be called by S
4a5a0 51 4c 69 74 65 20 6f 6e 63 65 20 66 6f 72 20 65  QLite once for e
4a5b0 61 63 68 0a 2a 2a 20 65 66 66 65 63 74 69 76 65  ach.** effective
4a5c0 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
4a5d0 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 2e  3_initialize()].
4a5e0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65  .**.** The xMute
4a5f0 78 45 6e 64 20 6d 65 74 68 6f 64 20 64 65 66 69  xEnd method defi
4a600 6e 65 64 20 62 79 20 74 68 69 73 20 73 74 72 75  ned by this stru
4a610 63 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 64  cture is invoked
4a620 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73   as.** part of s
4a630 79 73 74 65 6d 20 73 68 75 74 64 6f 77 6e 20 62  ystem shutdown b
4a640 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 68  y the sqlite3_sh
4a650 75 74 64 6f 77 6e 28 29 20 66 75 6e 63 74 69 6f  utdown() functio
4a660 6e 2e 20 54 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d  n. The.** implem
4a670 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 69 73  entation of this
4a680 20 6d 65 74 68 6f 64 20 69 73 20 65 78 70 65 63   method is expec
4a690 74 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20 61  ted to release a
4a6a0 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 0a 2a  ll outstanding.*
4a6b0 2a 20 72 65 73 6f 75 72 63 65 73 20 6f 62 74 61  * resources obta
4a6c0 69 6e 65 64 20 62 79 20 74 68 65 20 6d 75 74 65  ined by the mute
4a6d0 78 20 6d 65 74 68 6f 64 73 20 69 6d 70 6c 65 6d  x methods implem
4a6e0 65 6e 74 61 74 69 6f 6e 2c 20 65 73 70 65 63 69  entation, especi
4a6f0 61 6c 6c 79 0a 2a 2a 20 74 68 6f 73 65 20 6f 62  ally.** those ob
4a700 74 61 69 6e 65 64 20 62 79 20 74 68 65 20 78 4d  tained by the xM
4a710 75 74 65 78 49 6e 69 74 20 6d 65 74 68 6f 64 2e  utexInit method.
4a720 20 7b 48 31 37 30 30 33 7d 20 54 68 65 20 78 4d   {H17003} The xM
4a730 75 74 65 78 45 6e 64 28 29 0a 2a 2a 20 69 6e 74  utexEnd().** int
4a740 65 72 66 61 63 65 20 73 68 61 6c 6c 20 62 65 20  erface shall be 
4a750 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72  invoked once for
4a760 20 65 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73   each call to [s
4a770 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28  qlite3_shutdown(
4a780 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  )]..**.** The re
4a790 6d 61 69 6e 69 6e 67 20 73 65 76 65 6e 20 6d 65  maining seven me
4a7a0 74 68 6f 64 73 20 64 65 66 69 6e 65 64 20 62 79  thods defined by
4a7b0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
4a7c0 28 78 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 2a 2a  (xMutexAlloc,.**
4a7d0 20 78 4d 75 74 65 78 46 72 65 65 2c 20 78 4d 75   xMutexFree, xMu
4a7e0 74 65 78 45 6e 74 65 72 2c 20 78 4d 75 74 65 78  texEnter, xMutex
4a7f0 54 72 79 2c 20 78 4d 75 74 65 78 4c 65 61 76 65  Try, xMutexLeave
4a800 2c 20 78 4d 75 74 65 78 48 65 6c 64 20 61 6e 64  , xMutexHeld and
4a810 0a 2a 2a 20 78 4d 75 74 65 78 4e 6f 74 68 65 6c  .** xMutexNothel
4a820 64 29 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  d) implement the
4a830 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 72   following inter
4a840 66 61 63 65 73 20 28 72 65 73 70 65 63 74 69 76  faces (respectiv
4a850 65 6c 79 29 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e  ely):.**.** <ul>
4a860 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c  .**   <li>  [sql
4a870 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
4a880 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c  ()] </li>.**   <
4a890 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75  li>  [sqlite3_mu
4a8a0 74 65 78 5f 66 72 65 65 28 29 5d 20 3c 2f 6c 69  tex_free()] </li
4a8b0 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71  >.**   <li>  [sq
4a8c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
4a8d0 72 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20  r()] </li>.**   
4a8e0 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d  <li>  [sqlite3_m
4a8f0 75 74 65 78 5f 74 72 79 28 29 5d 20 3c 2f 6c 69  utex_try()] </li
4a900 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71  >.**   <li>  [sq
4a910 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
4a920 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20  e()] </li>.**   
4a930 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d  <li>  [sqlite3_m
4a940 75 74 65 78 5f 68 65 6c 64 28 29 5d 20 3c 2f 6c  utex_held()] </l
4a950 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73  i>.**   <li>  [s
4a960 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
4a970 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a  held()] </li>.**
4a980 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65   </ul>.**.** The
4a990 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e 63 65   only difference
4a9a0 20 69 73 20 74 68 61 74 20 74 68 65 20 70 75 62   is that the pub
4a9b0 6c 69 63 20 73 71 6c 69 74 65 33 5f 58 58 58 20  lic sqlite3_XXX 
4a9c0 66 75 6e 63 74 69 6f 6e 73 20 65 6e 75 6d 65 72  functions enumer
4a9d0 61 74 65 64 0a 2a 2a 20 61 62 6f 76 65 20 73 69  ated.** above si
4a9e0 6c 65 6e 74 6c 79 20 69 67 6e 6f 72 65 20 61 6e  lently ignore an
4a9f0 79 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 74 68  y invocations th
4aa00 61 74 20 70 61 73 73 20 61 20 4e 55 4c 4c 20 70  at pass a NULL p
4aa10 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64 0a 2a  ointer instead.*
4aa20 2a 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75 74  * of a valid mut
4aa30 65 78 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 69  ex handle. The i
4aa40 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f  mplementations o
4aa50 66 20 74 68 65 20 6d 65 74 68 6f 64 73 20 64 65  f the methods de
4aa60 66 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68 69 73  fined.** by this
4aa70 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 6e   structure are n
4aa80 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 68  ot required to h
4aa90 61 6e 64 6c 65 20 74 68 69 73 20 63 61 73 65 2c  andle this case,
4aaa0 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20   the results.** 
4aab0 6f 66 20 70 61 73 73 69 6e 67 20 61 20 4e 55 4c  of passing a NUL
4aac0 4c 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 61  L pointer instea
4aad0 64 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75 74  d of a valid mut
4aae0 65 78 20 68 61 6e 64 6c 65 20 61 72 65 20 75 6e  ex handle are un
4aaf0 64 65 66 69 6e 65 64 0a 2a 2a 20 28 69 2e 65 2e  defined.** (i.e.
4ab00 20 69 74 20 69 73 20 61 63 63 65 70 74 61 62 6c   it is acceptabl
4ab10 65 20 74 6f 20 70 72 6f 76 69 64 65 20 61 6e 20  e to provide an 
4ab20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74  implementation t
4ab30 68 61 74 20 73 65 67 66 61 75 6c 74 73 20 69 66  hat segfaults if
4ab40 0a 2a 2a 20 69 74 20 69 73 20 70 61 73 73 65 64  .** it is passed
4ab50 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 29   a NULL pointer)
4ab60 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
4ab70 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  uct sqlite3_mute
4ab80 78 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74 65  x_methods sqlite
4ab90 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 3b  3_mutex_methods;
4aba0 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
4abb0 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 7b 0a  mutex_methods {.
4abc0 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 49 6e    int (*xMutexIn
4abd0 69 74 29 28 76 6f 69 64 29 3b 0a 20 20 69 6e 74  it)(void);.  int
4abe0 20 28 2a 78 4d 75 74 65 78 45 6e 64 29 28 76 6f   (*xMutexEnd)(vo
4abf0 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  id);.  sqlite3_m
4ac00 75 74 65 78 20 2a 28 2a 78 4d 75 74 65 78 41 6c  utex *(*xMutexAl
4ac10 6c 6f 63 29 28 69 6e 74 29 3b 0a 20 20 76 6f 69  loc)(int);.  voi
4ac20 64 20 28 2a 78 4d 75 74 65 78 46 72 65 65 29 28  d (*xMutexFree)(
4ac30 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29  sqlite3_mutex *)
4ac40 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 65  ;.  void (*xMute
4ac50 78 45 6e 74 65 72 29 28 73 71 6c 69 74 65 33 5f  xEnter)(sqlite3_
4ac60 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69 6e 74 20  mutex *);.  int 
4ac70 28 2a 78 4d 75 74 65 78 54 72 79 29 28 73 71 6c  (*xMutexTry)(sql
4ac80 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20  ite3_mutex *);. 
4ac90 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 78 4c 65   void (*xMutexLe
4aca0 61 76 65 29 28 73 71 6c 69 74 65 33 5f 6d 75 74  ave)(sqlite3_mut
4acb0 65 78 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78  ex *);.  int (*x
4acc0 4d 75 74 65 78 48 65 6c 64 29 28 73 71 6c 69 74  MutexHeld)(sqlit
4acd0 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69  e3_mutex *);.  i
4ace0 6e 74 20 28 2a 78 4d 75 74 65 78 4e 6f 74 68 65  nt (*xMutexNothe
4acf0 6c 64 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  ld)(sqlite3_mute
4ad00 78 20 2a 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  x *);.};../*.** 
4ad10 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78 20  CAPI3REF: Mutex 
4ad20 56 65 72 69 66 69 63 61 74 69 6f 6e 20 52 6f 75  Verification Rou
4ad30 74 69 6e 65 73 20 7b 48 31 37 30 38 30 7d 20 3c  tines {H17080} <
4ad40 53 32 30 31 33 30 3e 20 3c 53 33 30 38 30 30 3e  S20130> <S30800>
4ad50 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
4ad60 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20  e3_mutex_held() 
4ad70 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  and sqlite3_mute
4ad80 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74  x_notheld() rout
4ad90 69 6e 65 73 0a 2a 2a 20 61 72 65 20 69 6e 74 65  ines.** are inte
4ada0 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e 73  nded for use ins
4adb0 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61  ide assert() sta
4adc0 74 65 6d 65 6e 74 73 2e 20 7b 48 31 37 30 38 31  tements. {H17081
4add0 7d 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72  } The SQLite cor
4ade0 65 0a 2a 2a 20 6e 65 76 65 72 20 75 73 65 73 20  e.** never uses 
4adf0 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 65  these routines e
4ae00 78 63 65 70 74 20 69 6e 73 69 64 65 20 61 6e 20  xcept inside an 
4ae10 61 73 73 65 72 74 28 29 20 61 6e 64 20 61 70 70  assert() and app
4ae20 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65  lications.** are
4ae30 20 61 64 76 69 73 65 64 20 74 6f 20 66 6f 6c 6c   advised to foll
4ae40 6f 77 20 74 68 65 20 6c 65 61 64 20 6f 66 20 74  ow the lead of t
4ae50 68 65 20 63 6f 72 65 2e 20 20 7b 48 31 37 30 38  he core.  {H1708
4ae60 32 7d 20 54 68 65 20 63 6f 72 65 20 6f 6e 6c 79  2} The core only
4ae70 0a 2a 2a 20 70 72 6f 76 69 64 65 73 20 69 6d 70  .** provides imp
4ae80 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72  lementations for
4ae90 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   these routines 
4aea0 77 68 65 6e 20 69 74 20 69 73 20 63 6f 6d 70 69  when it is compi
4aeb0 6c 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20  led.** with the 
4aec0 53 51 4c 49 54 45 5f 44 45 42 55 47 20 66 6c 61  SQLITE_DEBUG fla
4aed0 67 2e 20 20 7b 41 31 37 30 38 37 7d 20 45 78 74  g.  {A17087} Ext
4aee0 65 72 6e 61 6c 20 6d 75 74 65 78 20 69 6d 70 6c  ernal mutex impl
4aef0 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 61  ementations.** a
4af00 72 65 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64  re only required
4af10 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 73   to provide thes
4af20 65 20 72 6f 75 74 69 6e 65 73 20 69 66 20 53 51  e routines if SQ
4af30 4c 49 54 45 5f 44 45 42 55 47 20 69 73 0a 2a 2a  LITE_DEBUG is.**
4af40 20 64 65 66 69 6e 65 64 20 61 6e 64 20 69 66 20   defined and if 
4af50 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 20 64 65  NDEBUG is not de
4af60 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  fined..**.** {H1
4af70 37 30 38 33 7d 20 54 68 65 73 65 20 72 6f 75 74  7083} These rout
4af80 69 6e 65 73 20 73 68 6f 75 6c 64 20 72 65 74 75  ines should retu
4af90 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 6d  rn true if the m
4afa0 75 74 65 78 20 69 6e 20 74 68 65 69 72 20 61 72  utex in their ar
4afb0 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 68 65 6c  gument.** is hel
4afc0 64 20 6f 72 20 6e 6f 74 20 68 65 6c 64 2c 20 72  d or not held, r
4afd0 65 73 70 65 63 74 69 76 65 6c 79 2c 20 62 79 20  espectively, by 
4afe0 74 68 65 20 63 61 6c 6c 69 6e 67 20 74 68 72 65  the calling thre
4aff0 61 64 2e 0a 2a 2a 0a 2a 2a 20 7b 58 31 37 30 38  ad..**.** {X1708
4b000 34 7d 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74  4} The implement
4b010 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 65 71  ation is not req
4b020 75 69 72 65 64 20 74 6f 20 70 72 6f 76 69 64 65  uired to provide
4b030 64 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68  d versions of th
4b040 65 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20  ese.** routines 
4b050 74 68 61 74 20 61 63 74 75 61 6c 6c 79 20 77 6f  that actually wo
4b060 72 6b 2e 20 49 66 20 74 68 65 20 69 6d 70 6c 65  rk. If the imple
4b070 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e  mentation does n
4b080 6f 74 20 70 72 6f 76 69 64 65 20 77 6f 72 6b 69  ot provide worki
4b090 6e 67 0a 2a 2a 20 76 65 72 73 69 6f 6e 73 20 6f  ng.** versions o
4b0a0 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  f these routines
4b0b0 2c 20 69 74 20 73 68 6f 75 6c 64 20 61 74 20 6c  , it should at l
4b0c0 65 61 73 74 20 70 72 6f 76 69 64 65 20 73 74 75  east provide stu
4b0d0 62 73 20 74 68 61 74 20 61 6c 77 61 79 73 0a 2a  bs that always.*
4b0e0 2a 20 72 65 74 75 72 6e 20 74 72 75 65 20 73 6f  * return true so
4b0f0 20 74 68 61 74 20 6f 6e 65 20 64 6f 65 73 20 6e   that one does n
4b100 6f 74 20 67 65 74 20 73 70 75 72 69 6f 75 73 20  ot get spurious 
4b110 61 73 73 65 72 74 69 6f 6e 20 66 61 69 6c 75 72  assertion failur
4b120 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 38  es..**.** {H1708
4b130 35 7d 20 49 66 20 74 68 65 20 61 72 67 75 6d 65  5} If the argume
4b140 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75  nt to sqlite3_mu
4b150 74 65 78 5f 68 65 6c 64 28 29 20 69 73 20 61 20  tex_held() is a 
4b160 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65  NULL pointer the
4b170 6e 0a 2a 2a 20 74 68 65 20 72 6f 75 74 69 6e 65  n.** the routine
4b180 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 31   should return 1
4b190 2e 20 20 7b 45 4e 44 7d 20 54 68 69 73 20 73 65  .  {END} This se
4b1a0 65 6d 73 20 63 6f 75 6e 74 65 72 2d 69 6e 74 75  ems counter-intu
4b1b0 69 74 69 76 65 20 73 69 6e 63 65 0a 2a 2a 20 63  itive since.** c
4b1c0 6c 65 61 72 6c 79 20 74 68 65 20 6d 75 74 65 78  learly the mutex
4b1d0 20 63 61 6e 6e 6f 74 20 62 65 20 68 65 6c 64 20   cannot be held 
4b1e0 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65  if it does not e
4b1f0 78 69 73 74 2e 20 20 42 75 74 20 74 68 65 0a 2a  xist.  But the.*
4b200 2a 20 74 68 65 20 72 65 61 73 6f 6e 20 74 68 65  * the reason the
4b210 20 6d 75 74 65 78 20 64 6f 65 73 20 6e 6f 74 20   mutex does not 
4b220 65 78 69 73 74 20 69 73 20 62 65 63 61 75 73 65  exist is because
4b230 20 74 68 65 20 62 75 69 6c 64 20 69 73 20 6e 6f   the build is no
4b240 74 0a 2a 2a 20 75 73 69 6e 67 20 6d 75 74 65 78  t.** using mutex
4b250 65 73 2e 20 20 41 6e 64 20 77 65 20 64 6f 20 6e  es.  And we do n
4b260 6f 74 20 77 61 6e 74 20 74 68 65 20 61 73 73 65  ot want the asse
4b270 72 74 28 29 20 63 6f 6e 74 61 69 6e 69 6e 67 20  rt() containing 
4b280 74 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73  the.** call to s
4b290 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
4b2a0 64 28 29 20 74 6f 20 66 61 69 6c 2c 20 73 6f 20  d() to fail, so 
4b2b0 61 20 6e 6f 6e 2d 7a 65 72 6f 20 72 65 74 75 72  a non-zero retur
4b2c0 6e 20 69 73 0a 2a 2a 20 74 68 65 20 61 70 70 72  n is.** the appr
4b2d0 6f 70 72 69 61 74 65 20 74 68 69 6e 67 20 74 6f  opriate thing to
4b2e0 20 64 6f 2e 20 20 7b 48 31 37 30 38 36 7d 20 54   do.  {H17086} T
4b2f0 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
4b300 5f 6e 6f 74 68 65 6c 64 28 29 0a 2a 2a 20 69 6e  _notheld().** in
4b310 74 65 72 66 61 63 65 20 73 68 6f 75 6c 64 20 61  terface should a
4b320 6c 73 6f 20 72 65 74 75 72 6e 20 31 20 77 68 65  lso return 1 whe
4b330 6e 20 67 69 76 65 6e 20 61 20 4e 55 4c 4c 20 70  n given a NULL p
4b340 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  ointer..*/.SQLIT
4b350 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
4b360 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c  3_mutex_held(sql
4b370 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51  ite3_mutex*);.SQ
4b380 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
4b390 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65  ite3_mutex_nothe
4b3a0 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ld(sqlite3_mutex
4b3b0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  *);../*.** CAPI3
4b3c0 52 45 46 3a 20 4d 75 74 65 78 20 54 79 70 65 73  REF: Mutex Types
4b3d0 20 7b 48 31 37 30 30 31 7d 20 3c 48 31 37 30 30   {H17001} <H1700
4b3e0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71  0>.**.** The [sq
4b3f0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
4b400 63 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 74  c()] interface t
4b410 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72  akes a single ar
4b420 67 75 6d 65 6e 74 0a 2a 2a 20 77 68 69 63 68 20  gument.** which 
4b430 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20  is one of these 
4b440 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74  integer constant
4b450 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 74  s..**.** The set
4b460 20 6f 66 20 73 74 61 74 69 63 20 6d 75 74 65 78   of static mutex
4b470 65 73 20 6d 61 79 20 63 68 61 6e 67 65 20 66 72  es may change fr
4b480 6f 6d 20 6f 6e 65 20 53 51 4c 69 74 65 20 72 65  om one SQLite re
4b490 6c 65 61 73 65 20 74 6f 20 74 68 65 0a 2a 2a 20  lease to the.** 
4b4a0 6e 65 78 74 2e 20 20 41 70 70 6c 69 63 61 74 69  next.  Applicati
4b4b0 6f 6e 73 20 74 68 61 74 20 6f 76 65 72 72 69 64  ons that overrid
4b4c0 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 6d  e the built-in m
4b4d0 75 74 65 78 20 6c 6f 67 69 63 20 6d 75 73 74 20  utex logic must 
4b4e0 62 65 0a 2a 2a 20 70 72 65 70 61 72 65 64 20 74  be.** prepared t
4b4f0 6f 20 61 63 63 6f 6d 6d 6f 64 61 74 65 20 61 64  o accommodate ad
4b500 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20  ditional static 
4b510 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a 23 64 65 66  mutexes..*/.#def
4b520 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ine SQLITE_MUTEX
4b530 5f 46 41 53 54 20 20 20 20 20 20 20 20 20 20 20  _FAST           
4b540 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    0.#define SQLI
4b550 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
4b560 56 45 20 20 20 20 20 20 20 20 31 0a 23 64 65 66  VE        1.#def
4b570 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ine SQLITE_MUTEX
4b580 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 20  _STATIC_MASTER  
4b590 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    2.#define SQLI
4b5a0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
4b5b0 4d 45 4d 20 20 20 20 20 20 20 33 20 20 2f 2a 20  MEM       3  /* 
4b5c0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
4b5d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
4b5e0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
4b5f0 4d 45 4d 32 20 20 20 20 20 20 34 20 20 2f 2a 20  MEM2      4  /* 
4b600 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
4b610 6d 65 6d 6f 72 79 28 29 20 2a 2f 0a 23 64 65 66  memory() */.#def
4b620 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ine SQLITE_MUTEX
4b630 5f 53 54 41 54 49 43 5f 50 52 4e 47 20 20 20 20  _STATIC_PRNG    
4b640 20 20 35 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f    5  /* sqlite3_
4b650 72 61 6e 64 6f 6d 28 29 20 2a 2f 0a 23 64 65 66  random() */.#def
4b660 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ine SQLITE_MUTEX
4b670 5f 53 54 41 54 49 43 5f 4c 52 55 20 20 20 20 20  _STATIC_LRU     
4b680 20 20 36 20 20 2f 2a 20 6c 72 75 20 70 61 67 65    6  /* lru page
4b690 20 6c 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65   list */.#define
4b6a0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
4b6b0 41 54 49 43 5f 4c 52 55 32 20 20 20 20 20 20 37  ATIC_LRU2      7
4b6c0 20 20 2f 2a 20 6c 72 75 20 70 61 67 65 20 6c 69    /* lru page li
4b6d0 73 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  st */../*.** CAP
4b6e0 49 33 52 45 46 3a 20 52 65 74 72 69 65 76 65 20  I3REF: Retrieve 
4b6f0 74 68 65 20 6d 75 74 65 78 20 66 6f 72 20 61 20  the mutex for a 
4b700 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
4b710 69 6f 6e 20 7b 48 31 37 30 30 32 7d 20 3c 48 31  ion {H17002} <H1
4b720 37 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  7000>.**.** This
4b730 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72   interface retur
4b740 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 68 65  ns a pointer the
4b750 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d   [sqlite3_mutex]
4b760 20 6f 62 6a 65 63 74 20 74 68 61 74 20 0a 2a 2a   object that .**
4b770 20 73 65 72 69 61 6c 69 7a 65 73 20 61 63 63 65   serializes acce
4b780 73 73 20 74 6f 20 74 68 65 20 5b 64 61 74 61 62  ss to the [datab
4b790 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
4b7a0 67 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67  given in the arg
4b7b0 75 6d 65 6e 74 0a 2a 2a 20 77 68 65 6e 20 74 68  ument.** when th
4b7c0 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64  e [threading mod
4b7d0 65 5d 20 69 73 20 53 65 72 69 61 6c 69 7a 65 64  e] is Serialized
4b7e0 2e 0a 2a 2a 20 49 66 20 74 68 65 20 5b 74 68 72  ..** If the [thr
4b7f0 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20  eading mode] is 
4b800 53 69 6e 67 6c 65 2d 74 68 72 65 61 64 20 6f 72  Single-thread or
4b810 20 4d 75 6c 74 69 2d 74 68 72 65 61 64 20 74 68   Multi-thread th
4b820 65 6e 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69  en this.** routi
4b830 6e 65 20 72 65 74 75 72 6e 73 20 61 20 4e 55 4c  ne returns a NUL
4b840 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51  L pointer..*/.SQ
4b850 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
4b860 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 5f  _mutex *sqlite3_
4b870 64 62 5f 6d 75 74 65 78 28 73 71 6c 69 74 65 33  db_mutex(sqlite3
4b880 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  *);../*.** CAPI3
4b890 52 45 46 3a 20 4c 6f 77 2d 4c 65 76 65 6c 20 43  REF: Low-Level C
4b8a0 6f 6e 74 72 6f 6c 20 4f 66 20 44 61 74 61 62 61  ontrol Of Databa
4b8b0 73 65 20 46 69 6c 65 73 20 7b 48 31 31 33 30 30  se Files {H11300
4b8c0 7d 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a  } <S30800>.**.**
4b8d0 20 7b 48 31 31 33 30 31 7d 20 54 68 65 20 5b 73   {H11301} The [s
4b8e0 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74  qlite3_file_cont
4b8f0 72 6f 6c 28 29 5d 20 69 6e 74 65 72 66 61 63 65  rol()] interface
4b900 20 6d 61 6b 65 73 20 61 20 64 69 72 65 63 74 20   makes a direct 
4b910 63 61 6c 6c 20 74 6f 20 74 68 65 0a 2a 2a 20 78  call to the.** x
4b920 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68  FileControl meth
4b930 6f 64 20 66 6f 72 20 74 68 65 20 5b 73 71 6c 69  od for the [sqli
4b940 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20  te3_io_methods] 
4b950 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 61 74 65  object associate
4b960 64 0a 2a 2a 20 77 69 74 68 20 61 20 70 61 72 74  d.** with a part
4b970 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20  icular database 
4b980 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68  identified by th
4b990 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
4b9a0 74 2e 20 7b 48 31 31 33 30 32 7d 20 54 68 65 0a  t. {H11302} The.
4b9b0 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64  ** name of the d
4b9c0 61 74 61 62 61 73 65 20 69 73 20 74 68 65 20 6e  atabase is the n
4b9d0 61 6d 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20  ame assigned to 
4b9e0 74 68 65 20 64 61 74 61 62 61 73 65 20 62 79 20  the database by 
4b9f0 74 68 65 0a 2a 2a 20 3c 61 20 68 72 65 66 3d 22  the.** <a href="
4ba00 6c 61 6e 67 5f 61 74 74 61 63 68 2e 68 74 6d 6c  lang_attach.html
4ba10 22 3e 41 54 54 41 43 48 3c 2f 61 3e 20 53 51 4c  ">ATTACH</a> SQL
4ba20 20 63 6f 6d 6d 61 6e 64 20 74 68 61 74 20 6f 70   command that op
4ba30 65 6e 65 64 20 74 68 65 0a 2a 2a 20 64 61 74 61  ened the.** data
4ba40 62 61 73 65 2e 20 7b 48 31 31 33 30 33 7d 20 54  base. {H11303} T
4ba50 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 6d 61  o control the ma
4ba60 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
4ba70 2c 20 75 73 65 20 74 68 65 20 6e 61 6d 65 20 22  , use the name "
4ba80 6d 61 69 6e 22 0a 2a 2a 20 6f 72 20 61 20 4e 55  main".** or a NU
4ba90 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 7b 48 31 31  LL pointer. {H11
4baa0 33 30 34 7d 20 54 68 65 20 74 68 69 72 64 20 61  304} The third a
4bab0 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65  nd fourth parame
4bac0 74 65 72 73 20 74 6f 20 74 68 69 73 20 72 6f 75  ters to this rou
4bad0 74 69 6e 65 0a 2a 2a 20 61 72 65 20 70 61 73 73  tine.** are pass
4bae0 65 64 20 64 69 72 65 63 74 6c 79 20 74 68 72 6f  ed directly thro
4baf0 75 67 68 20 74 6f 20 74 68 65 20 73 65 63 6f 6e  ugh to the secon
4bb00 64 20 61 6e 64 20 74 68 69 72 64 20 70 61 72 61  d and third para
4bb10 6d 65 74 65 72 73 20 6f 66 0a 2a 2a 20 74 68 65  meters of.** the
4bb20 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65   xFileControl me
4bb30 74 68 6f 64 2e 20 20 7b 48 31 31 33 30 35 7d 20  thod.  {H11305} 
4bb40 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
4bb50 20 6f 66 20 74 68 65 20 78 46 69 6c 65 43 6f 6e   of the xFileCon
4bb60 74 72 6f 6c 0a 2a 2a 20 6d 65 74 68 6f 64 20 62  trol.** method b
4bb70 65 63 6f 6d 65 73 20 74 68 65 20 72 65 74 75 72  ecomes the retur
4bb80 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20  n value of this 
4bb90 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 7b  routine..**.** {
4bba0 48 31 31 33 30 36 7d 20 49 66 20 74 68 65 20 73  H11306} If the s
4bbb0 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
4bbc0 28 7a 44 62 4e 61 6d 65 29 20 64 6f 65 73 20 6e  (zDbName) does n
4bbd0 6f 74 20 6d 61 74 63 68 20 74 68 65 20 6e 61 6d  ot match the nam
4bbe0 65 20 6f 66 20 61 6e 79 0a 2a 2a 20 6f 70 65 6e  e of any.** open
4bbf0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
4bc00 74 68 65 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  then SQLITE_ERRO
4bc10 52 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 7b  R is returned. {
4bc20 48 31 31 33 30 37 7d 20 54 68 69 73 20 65 72 72  H11307} This err
4bc30 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 73 20 6e 6f  or.** code is no
4bc40 74 20 72 65 6d 65 6d 62 65 72 65 64 20 61 6e 64  t remembered and
4bc50 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 72 65 63   will not be rec
4bc60 61 6c 6c 65 64 20 62 79 20 5b 73 71 6c 69 74 65  alled by [sqlite
4bc70 33 5f 65 72 72 63 6f 64 65 28 29 5d 0a 2a 2a 20  3_errcode()].** 
4bc80 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d  or [sqlite3_errm
4bc90 73 67 28 29 5d 2e 20 7b 41 31 31 33 30 38 7d 20  sg()]. {A11308} 
4bca0 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 78  The underlying x
4bcb0 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68  FileControl meth
4bcc0 6f 64 20 6d 69 67 68 74 0a 2a 2a 20 61 6c 73 6f  od might.** also
4bcd0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
4bce0 52 52 4f 52 2e 20 20 7b 41 31 31 33 30 39 7d 20  RROR.  {A11309} 
4bcf0 54 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20  There is no way 
4bd00 74 6f 20 64 69 73 74 69 6e 67 75 69 73 68 20 62  to distinguish b
4bd10 65 74 77 65 65 6e 0a 2a 2a 20 61 6e 20 69 6e 63  etween.** an inc
4bd20 6f 72 72 65 63 74 20 7a 44 62 4e 61 6d 65 20 61  orrect zDbName a
4bd30 6e 64 20 61 6e 20 53 51 4c 49 54 45 5f 45 52 52  nd an SQLITE_ERR
4bd40 4f 52 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 74  OR return from t
4bd50 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a  he underlying.**
4bd60 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65   xFileControl me
4bd70 74 68 6f 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a  thod. {END}.**.*
4bd80 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51 4c  * See also: [SQL
4bd90 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54  ITE_FCNTL_LOCKST
4bda0 41 54 45 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ATE].*/.SQLITE_A
4bdb0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66  PI int sqlite3_f
4bdc0 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 73 71 6c 69  ile_control(sqli
4bdd0 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  te3*, const char
4bde0 20 2a 7a 44 62 4e 61 6d 65 2c 20 69 6e 74 20 6f   *zDbName, int o
4bdf0 70 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a  p, void*);../*.*
4be00 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65 73 74  * CAPI3REF: Test
4be10 69 6e 67 20 49 6e 74 65 72 66 61 63 65 20 7b 48  ing Interface {H
4be20 31 31 34 30 30 7d 20 3c 53 33 30 38 30 30 3e 0a  11400} <S30800>.
4be30 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
4be40 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29  3_test_control()
4be50 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 73   interface is us
4be60 65 64 20 74 6f 20 72 65 61 64 20 6f 75 74 20 69  ed to read out i
4be70 6e 74 65 72 6e 61 6c 0a 2a 2a 20 73 74 61 74 65  nternal.** state
4be80 20 6f 66 20 53 51 4c 69 74 65 20 61 6e 64 20 74   of SQLite and t
4be90 6f 20 69 6e 6a 65 63 74 20 66 61 75 6c 74 73 20  o inject faults 
4bea0 69 6e 74 6f 20 53 51 4c 69 74 65 20 66 6f 72 20  into SQLite for 
4beb0 74 65 73 74 69 6e 67 0a 2a 2a 20 70 75 72 70 6f  testing.** purpo
4bec0 73 65 73 2e 20 20 54 68 65 20 66 69 72 73 74 20  ses.  The first 
4bed0 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 6e 20  parameter is an 
4bee0 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64 65 20 74  operation code t
4bef0 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73 0a 2a  hat determines.*
4bf00 2a 20 74 68 65 20 6e 75 6d 62 65 72 2c 20 6d 65  * the number, me
4bf10 61 6e 69 6e 67 2c 20 61 6e 64 20 6f 70 65 72 61  aning, and opera
4bf20 74 69 6f 6e 20 6f 66 20 61 6c 6c 20 73 75 62 73  tion of all subs
4bf30 65 71 75 65 6e 74 20 70 61 72 61 6d 65 74 65 72  equent parameter
4bf40 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  s..**.** This in
4bf50 74 65 72 66 61 63 65 20 69 73 20 6e 6f 74 20 66  terface is not f
4bf60 6f 72 20 75 73 65 20 62 79 20 61 70 70 6c 69 63  or use by applic
4bf70 61 74 69 6f 6e 73 2e 20 20 49 74 20 65 78 69 73  ations.  It exis
4bf80 74 73 20 73 6f 6c 65 6c 79 0a 2a 2a 20 66 6f 72  ts solely.** for
4bf90 20 76 65 72 69 66 79 69 6e 67 20 74 68 65 20 63   verifying the c
4bfa0 6f 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f 6e  orrect operation
4bfb0 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20 6c   of the SQLite l
4bfc0 69 62 72 61 72 79 2e 20 20 44 65 70 65 6e 64 69  ibrary.  Dependi
4bfd0 6e 67 0a 2a 2a 20 6f 6e 20 68 6f 77 20 74 68 65  ng.** on how the
4bfe0 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20   SQLite library 
4bff0 69 73 20 63 6f 6d 70 69 6c 65 64 2c 20 74 68 69  is compiled, thi
4c000 73 20 69 6e 74 65 72 66 61 63 65 20 6d 69 67 68  s interface migh
4c010 74 20 6e 6f 74 20 65 78 69 73 74 2e 0a 2a 2a 0a  t not exist..**.
4c020 2a 2a 20 54 68 65 20 64 65 74 61 69 6c 73 20 6f  ** The details o
4c030 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20  f the operation 
4c040 63 6f 64 65 73 2c 20 74 68 65 69 72 20 6d 65 61  codes, their mea
4c050 6e 69 6e 67 73 2c 20 74 68 65 20 70 61 72 61 6d  nings, the param
4c060 65 74 65 72 73 0a 2a 2a 20 74 68 65 79 20 74 61  eters.** they ta
4c070 6b 65 2c 20 61 6e 64 20 77 68 61 74 20 74 68 65  ke, and what the
4c080 79 20 64 6f 20 61 72 65 20 61 6c 6c 20 73 75 62  y do are all sub
4c090 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77  ject to change w
4c0a0 69 74 68 6f 75 74 20 6e 6f 74 69 63 65 2e 0a 2a  ithout notice..*
4c0b0 2a 20 55 6e 6c 69 6b 65 20 6d 6f 73 74 20 6f 66  * Unlike most of
4c0c0 20 74 68 65 20 53 51 4c 69 74 65 20 41 50 49 2c   the SQLite API,
4c0d0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
4c0e0 73 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 64  s not guaranteed
4c0f0 20 74 6f 0a 2a 2a 20 6f 70 65 72 61 74 65 20 63   to.** operate c
4c100 6f 6e 73 69 73 74 65 6e 74 6c 79 20 66 72 6f 6d  onsistently from
4c110 20 6f 6e 65 20 72 65 6c 65 61 73 65 20 74 6f 20   one release to 
4c120 74 68 65 20 6e 65 78 74 2e 0a 2a 2f 0a 53 51 4c  the next..*/.SQL
4c130 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
4c140 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c  te3_test_control
4c150 28 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 3b 0a 0a  (int op, ...);..
4c160 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
4c170 54 65 73 74 69 6e 67 20 49 6e 74 65 72 66 61 63  Testing Interfac
4c180 65 20 4f 70 65 72 61 74 69 6f 6e 20 43 6f 64 65  e Operation Code
4c190 73 20 7b 48 31 31 34 31 30 7d 20 3c 48 31 31 34  s {H11410} <H114
4c1a0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  00>.**.** These 
4c1b0 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 74 68  constants are th
4c1c0 65 20 76 61 6c 69 64 20 6f 70 65 72 61 74 69 6f  e valid operatio
4c1d0 6e 20 63 6f 64 65 20 70 61 72 61 6d 65 74 65 72  n code parameter
4c1e0 73 20 75 73 65 64 0a 2a 2a 20 61 73 20 74 68 65  s used.** as the
4c1f0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
4c200 74 6f 20 5b 73 71 6c 69 74 65 33 5f 74 65 73 74  to [sqlite3_test
4c210 5f 63 6f 6e 74 72 6f 6c 28 29 5d 2e 0a 2a 2a 0a  _control()]..**.
4c220 2a 2a 20 54 68 65 73 65 20 70 61 72 61 6d 65 74  ** These paramet
4c230 65 72 73 20 61 6e 64 20 74 68 65 69 72 20 6d 65  ers and their me
4c240 61 6e 69 6e 67 73 20 61 72 65 20 73 75 62 6a 65  anings are subje
4c250 63 74 20 74 6f 20 63 68 61 6e 67 65 0a 2a 2a 20  ct to change.** 
4c260 77 69 74 68 6f 75 74 20 6e 6f 74 69 63 65 2e 20  without notice. 
4c270 20 54 68 65 73 65 20 76 61 6c 75 65 73 20 61 72   These values ar
4c280 65 20 66 6f 72 20 74 65 73 74 69 6e 67 20 70 75  e for testing pu
4c290 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 0a 2a 2a 20  rposes only..** 
4c2a0 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f  Applications sho
4c2b0 75 6c 64 20 6e 6f 74 20 75 73 65 20 61 6e 79 20  uld not use any 
4c2c0 6f 66 20 74 68 65 73 65 20 70 61 72 61 6d 65 74  of these paramet
4c2d0 65 72 73 20 6f 72 20 74 68 65 0a 2a 2a 20 5b 73  ers or the.** [s
4c2e0 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74  qlite3_test_cont
4c2f0 72 6f 6c 28 29 5d 20 69 6e 74 65 72 66 61 63 65  rol()] interface
4c300 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
4c310 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e  ITE_TESTCTRL_PRN
4c320 47 5f 53 41 56 45 20 20 20 20 20 20 20 20 20 20  G_SAVE          
4c330 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20        5.#define 
4c340 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
4c350 50 52 4e 47 5f 52 45 53 54 4f 52 45 20 20 20 20  PRNG_RESTORE    
4c360 20 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 69           6.#defi
4c370 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54  ne SQLITE_TESTCT
4c380 52 4c 5f 50 52 4e 47 5f 52 45 53 45 54 20 20 20  RL_PRNG_RESET   
4c390 20 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 64              7.#d
4c3a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53  efine SQLITE_TES
4c3b0 54 43 54 52 4c 5f 42 49 54 56 45 43 5f 54 45 53  TCTRL_BITVEC_TES
4c3c0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38  T              8
4c3d0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
4c3e0 54 45 53 54 43 54 52 4c 5f 46 41 55 4c 54 5f 49  TESTCTRL_FAULT_I
4c3f0 4e 53 54 41 4c 4c 20 20 20 20 20 20 20 20 20 20  NSTALL          
4c400 20 20 39 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    9.#define SQLI
4c410 54 45 5f 54 45 53 54 43 54 52 4c 5f 42 45 4e 49  TE_TESTCTRL_BENI
4c420 47 4e 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53 20  GN_MALLOC_HOOKS 
4c430 20 20 20 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 43 41      10../*.** CA
4c440 50 49 33 52 45 46 3a 20 53 51 4c 69 74 65 20 52  PI3REF: SQLite R
4c450 75 6e 74 69 6d 65 20 53 74 61 74 75 73 20 7b 48  untime Status {H
4c460 31 37 32 30 30 7d 20 3c 53 36 30 32 30 30 3e 0a  17200} <S60200>.
4c470 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
4c480 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72  **.** This inter
4c490 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20  face is used to 
4c4a0 72 65 74 72 69 65 76 65 20 72 75 6e 74 69 6d 65  retrieve runtime
4c4b0 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74   status informat
4c4c0 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 74 68 65  ion.** about the
4c4d0 20 70 72 65 66 6f 72 6d 61 6e 63 65 20 6f 66 20   preformance of 
4c4e0 53 51 4c 69 74 65 2c 20 61 6e 64 20 6f 70 74 69  SQLite, and opti
4c4f0 6f 6e 61 6c 6c 79 20 74 6f 20 72 65 73 65 74 20  onally to reset 
4c500 76 61 72 69 6f 75 73 0a 2a 2a 20 68 69 67 68 77  various.** highw
4c510 61 74 65 72 20 6d 61 72 6b 73 2e 20 20 54 68 65  ater marks.  The
4c520 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
4c530 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f  is an integer co
4c540 64 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70  de for.** the sp
4c550 65 63 69 66 69 63 20 70 61 72 61 6d 65 74 65 72  ecific parameter
4c560 20 74 6f 20 6d 65 61 73 75 72 65 2e 20 20 52 65   to measure.  Re
4c570 63 6f 67 6e 69 7a 65 64 20 69 6e 74 65 67 65 72  cognized integer
4c580 20 63 6f 64 65 73 0a 2a 2a 20 61 72 65 20 6f 66   codes.** are of
4c590 20 74 68 65 20 66 6f 72 6d 20 5b 53 51 4c 49 54   the form [SQLIT
4c5a0 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f  E_STATUS_MEMORY_
4c5b0 55 53 45 44 20 7c 20 53 51 4c 49 54 45 5f 53 54  USED | SQLITE_ST
4c5c0 41 54 55 53 5f 2e 2e 2e 5d 2e 0a 2a 2a 20 54 68  ATUS_...]..** Th
4c5d0 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20  e current value 
4c5e0 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  of the parameter
4c5f0 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 74   is returned int
4c600 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a 2a 2a 20  o *pCurrent..** 
4c610 54 68 65 20 68 69 67 68 65 73 74 20 72 65 63 6f  The highest reco
4c620 72 64 65 64 20 76 61 6c 75 65 20 69 73 20 72 65  rded value is re
4c630 74 75 72 6e 65 64 20 69 6e 20 2a 70 48 69 67 68  turned in *pHigh
4c640 77 61 74 65 72 2e 20 20 49 66 20 74 68 65 0a 2a  water.  If the.*
4c650 2a 20 72 65 73 65 74 46 6c 61 67 20 69 73 20 74  * resetFlag is t
4c660 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 68 69  rue, then the hi
4c670 67 68 65 73 74 20 72 65 63 6f 72 64 20 76 61 6c  ghest record val
4c680 75 65 20 69 73 20 72 65 73 65 74 20 61 66 74 65  ue is reset afte
4c690 72 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72  r.** *pHighwater
4c6a0 20 69 73 20 77 72 69 74 74 65 6e 2e 20 53 6f 6d   is written. Som
4c6b0 65 20 70 61 72 61 6d 65 74 65 72 73 20 64 6f 20  e parameters do 
4c6c0 6e 6f 74 20 72 65 63 6f 72 64 20 74 68 65 20 68  not record the h
4c6d0 69 67 68 65 73 74 0a 2a 2a 20 76 61 6c 75 65 2e  ighest.** value.
4c6e0 20 20 46 6f 72 20 74 68 6f 73 65 20 70 61 72 61    For those para
4c6f0 6d 65 74 65 72 73 0a 2a 2a 20 6e 6f 74 68 69 6e  meters.** nothin
4c700 67 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74  g is written int
4c710 6f 20 2a 70 48 69 67 68 77 61 74 65 72 20 61 6e  o *pHighwater an
4c720 64 20 74 68 65 20 72 65 73 65 74 46 6c 61 67 20  d the resetFlag 
4c730 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20 4f  is ignored..** O
4c740 74 68 65 72 20 70 61 72 61 6d 65 74 65 72 73 20  ther parameters 
4c750 72 65 63 6f 72 64 20 6f 6e 6c 79 20 74 68 65 20  record only the 
4c760 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 61  highwater mark a
4c770 6e 64 20 6e 6f 74 20 74 68 65 20 63 75 72 72 65  nd not the curre
4c780 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 46 6f  nt.** value.  Fo
4c790 72 20 74 68 65 73 65 20 6c 61 74 74 65 72 20 70  r these latter p
4c7a0 61 72 61 6d 65 74 65 72 73 20 6e 6f 74 68 69 6e  arameters nothin
4c7b0 67 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74  g is written int
4c7c0 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a 2a 2a 0a  o *pCurrent..**.
4c7d0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
4c7e0 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f  returns SQLITE_O
4c7f0 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64  K on success and
4c800 20 61 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 5b   a non-zero.** [
4c810 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 6e 20 66  error code] on f
4c820 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  ailure..**.** Th
4c830 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
4c840 72 65 61 64 73 61 66 65 20 62 75 74 20 69 73 20  readsafe but is 
4c850 6e 6f 74 20 61 74 6f 6d 69 63 2e 20 20 54 68 69  not atomic.  Thi
4c860 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 0a 2a 2a  s routine can.**
4c870 20 63 61 6c 6c 65 64 20 77 68 69 6c 65 20 6f 74   called while ot
4c880 68 65 72 20 74 68 72 65 61 64 73 20 61 72 65 20  her threads are 
4c890 72 75 6e 6e 69 6e 67 20 74 68 65 20 73 61 6d 65  running the same
4c8a0 20 6f 72 20 64 69 66 66 65 72 65 6e 74 20 53 51   or different SQ
4c8b0 4c 69 74 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63  Lite.** interfac
4c8c0 65 73 2e 20 20 48 6f 77 65 76 65 72 20 74 68 65  es.  However the
4c8d0 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64   values returned
4c8e0 20 69 6e 20 2a 70 43 75 72 72 65 6e 74 20 61 6e   in *pCurrent an
4c8f0 64 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72  d.** *pHighwater
4c900 20 72 65 66 6c 65 63 74 20 74 68 65 20 73 74 61   reflect the sta
4c910 74 75 73 20 6f 66 20 53 51 4c 69 74 65 20 61 74  tus of SQLite at
4c920 20 64 69 66 66 65 72 65 6e 74 20 70 6f 69 6e 74   different point
4c930 73 20 69 6e 20 74 69 6d 65 0a 2a 2a 20 61 6e 64  s in time.** and
4c940 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20   it is possible 
4c950 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68 72  that another thr
4c960 65 61 64 20 6d 69 67 68 74 20 63 68 61 6e 67 65  ead might change
4c970 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a   the parameter.*
4c980 2a 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65  * in between the
4c990 20 74 69 6d 65 73 20 77 68 65 6e 20 2a 70 43 75   times when *pCu
4c9a0 72 72 65 6e 74 20 61 6e 64 20 2a 70 48 69 67 68  rrent and *pHigh
4c9b0 77 61 74 65 72 20 61 72 65 20 77 72 69 74 74 65  water are writte
4c9c0 6e 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  n..**.** See als
4c9d0 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73  o: [sqlite3_db_s
4c9e0 74 61 74 75 73 28 29 5d 0a 2a 2f 0a 53 51 4c 49  tatus()].*/.SQLI
4c9f0 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58  TE_API SQLITE_EX
4ca00 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73  PERIMENTAL int s
4ca10 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 69 6e  qlite3_status(in
4ca20 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 72 72  t op, int *pCurr
4ca30 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69 67 68 77  ent, int *pHighw
4ca40 61 74 65 72 2c 20 69 6e 74 20 72 65 73 65 74 46  ater, int resetF
4ca50 6c 61 67 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41  lag);.../*.** CA
4ca60 50 49 33 52 45 46 3a 20 53 74 61 74 75 73 20 50  PI3REF: Status P
4ca70 61 72 61 6d 65 74 65 72 73 20 7b 48 31 37 32 35  arameters {H1725
4ca80 30 7d 20 3c 48 31 37 32 30 30 3e 0a 2a 2a 20 45  0} <H17200>.** E
4ca90 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a  XPERIMENTAL.**.*
4caa0 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 72 20  * These integer 
4cab0 63 6f 6e 73 74 61 6e 74 73 20 64 65 73 69 67 6e  constants design
4cac0 61 74 65 20 76 61 72 69 6f 75 73 20 72 75 6e 2d  ate various run-
4cad0 74 69 6d 65 20 73 74 61 74 75 73 20 70 61 72 61  time status para
4cae0 6d 65 74 65 72 73 0a 2a 2a 20 74 68 61 74 20 63  meters.** that c
4caf0 61 6e 20 62 65 20 72 65 74 75 72 6e 65 64 20 62  an be returned b
4cb00 79 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75  y [sqlite3_statu
4cb10 73 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e  s()]..**.** <dl>
4cb20 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53  .** <dt>SQLITE_S
4cb30 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45  TATUS_MEMORY_USE
4cb40 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  D</dt>.** <dd>Th
4cb50 69 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  is parameter is 
4cb60 74 68 65 20 63 75 72 72 65 6e 74 20 61 6d 6f 75  the current amou
4cb70 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63 68 65  nt of memory che
4cb80 63 6b 65 64 20 6f 75 74 0a 2a 2a 20 75 73 69 6e  cked out.** usin
4cb90 67 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  g [sqlite3_mallo
4cba0 63 28 29 5d 2c 20 65 69 74 68 65 72 20 64 69 72  c()], either dir
4cbb0 65 63 74 6c 79 20 6f 72 20 69 6e 64 69 72 65 63  ectly or indirec
4cbc0 74 6c 79 2e 20 20 54 68 65 0a 2a 2a 20 66 69 67  tly.  The.** fig
4cbd0 75 72 65 20 69 6e 63 6c 75 64 65 73 20 63 61 6c  ure includes cal
4cbe0 6c 73 20 6d 61 64 65 20 74 6f 20 5b 73 71 6c 69  ls made to [sqli
4cbf0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 62 79  te3_malloc()] by
4cc00 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
4cc10 0a 2a 2a 20 61 6e 64 20 69 6e 74 65 72 6e 61 6c  .** and internal
4cc20 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 62 79   memory usage by
4cc30 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
4cc40 61 72 79 2e 20 20 53 63 72 61 74 63 68 20 6d 65  ary.  Scratch me
4cc50 6d 6f 72 79 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 6c  mory.** controll
4cc60 65 64 20 62 79 20 5b 53 51 4c 49 54 45 5f 43 4f  ed by [SQLITE_CO
4cc70 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 20 61 6e  NFIG_SCRATCH] an
4cc80 64 20 61 75 78 69 6c 69 61 72 79 20 70 61 67 65  d auxiliary page
4cc90 2d 63 61 63 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79  -cache.** memory
4cca0 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 5b   controlled by [
4ccb0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41  SQLITE_CONFIG_PA
4ccc0 47 45 43 41 43 48 45 5d 20 69 73 20 6e 6f 74 20  GECACHE] is not 
4ccd0 69 6e 63 6c 75 64 65 64 20 69 6e 0a 2a 2a 20 74  included in.** t
4cce0 68 69 73 20 70 61 72 61 6d 65 74 65 72 2e 20 20  his parameter.  
4ccf0 54 68 65 20 61 6d 6f 75 6e 74 20 72 65 74 75 72  The amount retur
4cd00 6e 65 64 20 69 73 20 74 68 65 20 73 75 6d 20 6f  ned is the sum o
4cd10 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  f the allocation
4cd20 0a 2a 2a 20 73 69 7a 65 73 20 61 73 20 72 65 70  .** sizes as rep
4cd30 6f 72 74 65 64 20 62 79 20 74 68 65 20 78 53 69  orted by the xSi
4cd40 7a 65 20 6d 65 74 68 6f 64 20 69 6e 20 5b 73 71  ze method in [sq
4cd50 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
4cd60 73 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  s].</dd>.**.** <
4cd70 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53  dt>SQLITE_STATUS
4cd80 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 3c 2f 64 74  _MALLOC_SIZE</dt
4cd90 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61  >.** <dd>This pa
4cda0 72 61 6d 65 74 65 72 20 72 65 63 6f 72 64 73 20  rameter records 
4cdb0 74 68 65 20 6c 61 72 67 65 73 74 20 6d 65 6d 6f  the largest memo
4cdc0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65  ry allocation re
4cdd0 71 75 65 73 74 0a 2a 2a 20 68 61 6e 64 65 64 20  quest.** handed 
4cde0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  to [sqlite3_mall
4cdf0 6f 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65  oc()] or [sqlite
4ce00 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 28 6f 72  3_realloc()] (or
4ce10 20 74 68 65 69 72 0a 2a 2a 20 69 6e 74 65 72 6e   their.** intern
4ce20 61 6c 20 65 71 75 69 76 61 6c 65 6e 74 73 29 2e  al equivalents).
4ce30 20 20 4f 6e 6c 79 20 74 68 65 20 76 61 6c 75 65    Only the value
4ce40 20 72 65 74 75 72 6e 65 64 20 69 6e 20 74 68 65   returned in the
4ce50 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72 20  .** *pHighwater 
4ce60 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71  parameter to [sq
4ce70 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 5d 20  lite3_status()] 
4ce80 69 73 20 6f 66 20 69 6e 74 65 72 65 73 74 2e 20  is of interest. 
4ce90 20 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 77   .** The value w
4cea0 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20  ritten into the 
4ceb0 2a 70 43 75 72 72 65 6e 74 20 70 61 72 61 6d 65  *pCurrent parame
4cec0 74 65 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64  ter is undefined
4ced0 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
4cee0 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50  >SQLITE_STATUS_P
4cef0 41 47 45 43 41 43 48 45 5f 55 53 45 44 3c 2f 64  AGECACHE_USED</d
4cf00 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70  t>.** <dd>This p
4cf10 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73  arameter returns
4cf20 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
4cf30 61 67 65 73 20 75 73 65 64 20 6f 75 74 20 6f 66  ages used out of
4cf40 20 74 68 65 0a 2a 2a 20 5b 70 61 67 65 63 61 63   the.** [pagecac
4cf50 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
4cf60 74 6f 72 5d 20 74 68 61 74 20 77 61 73 20 63 6f  tor] that was co
4cf70 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67 20 0a  nfigured using .
4cf80 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ** [SQLITE_CONFI
4cf90 47 5f 50 41 47 45 43 41 43 48 45 5d 2e 20 20 54  G_PAGECACHE].  T
4cfa0 68 65 0a 2a 2a 20 76 61 6c 75 65 20 72 65 74 75  he.** value retu
4cfb0 72 6e 65 64 20 69 73 20 69 6e 20 70 61 67 65 73  rned is in pages
4cfc0 2c 20 6e 6f 74 20 69 6e 20 62 79 74 65 73 2e 3c  , not in bytes.<
4cfd0 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53  /dd>.**.** <dt>S
4cfe0 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47  QLITE_STATUS_PAG
4cff0 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 3c  ECACHE_OVERFLOW<
4d000 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
4d010 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72   parameter retur
4d020 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
4d030 20 62 79 74 65 73 20 6f 66 20 70 61 67 65 20 63   bytes of page c
4d040 61 63 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69  ache.** allocati
4d050 6f 6e 20 77 68 69 63 68 20 63 6f 75 6c 64 20 6e  on which could n
4d060 6f 74 20 62 65 20 73 74 61 74 69 73 66 69 65 64  ot be statisfied
4d070 20 62 79 20 74 68 65 20 5b 53 51 4c 49 54 45 5f   by the [SQLITE_
4d080 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45  CONFIG_PAGECACHE
4d090 5d 0a 2a 2a 20 62 75 66 66 65 72 20 61 6e 64 20  ].** buffer and 
4d0a0 77 68 65 72 65 20 66 6f 72 63 65 64 20 74 6f 20  where forced to 
4d0b0 6f 76 65 72 66 6c 6f 77 20 74 6f 20 5b 73 71 6c  overflow to [sql
4d0c0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20  ite3_malloc()]. 
4d0d0 20 54 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64   The.** returned
4d0e0 20 76 61 6c 75 65 20 69 6e 63 6c 75 64 65 73 20   value includes 
4d0f0 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 68 61 74  allocations that
4d100 20 6f 76 65 72 66 6c 6f 77 65 64 20 62 65 63 61   overflowed beca
4d110 75 73 65 20 74 68 65 79 0a 2a 2a 20 77 68 65 72  use they.** wher
4d120 65 20 74 6f 6f 20 6c 61 72 67 65 20 28 74 68 65  e too large (the
4d130 79 20 77 65 72 65 20 6c 61 72 67 65 72 20 74 68  y were larger th
4d140 61 6e 20 74 68 65 20 22 73 7a 22 20 70 61 72 61  an the "sz" para
4d150 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 5b 53 51 4c  meter to.** [SQL
4d160 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43  ITE_CONFIG_PAGEC
4d170 41 43 48 45 5d 29 20 61 6e 64 20 61 6c 6c 6f 63  ACHE]) and alloc
4d180 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 76 65 72  ations that over
4d190 66 6c 6f 77 65 64 20 62 65 63 61 75 73 65 0a 2a  flowed because.*
4d1a0 2a 20 6e 6f 20 73 70 61 63 65 20 77 61 73 20 6c  * no space was l
4d1b0 65 66 74 20 69 6e 20 74 68 65 20 70 61 67 65 20  eft in the page 
4d1c0 63 61 63 68 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  cache.</dd>.**.*
4d1d0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41  * <dt>SQLITE_STA
4d1e0 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 53 49  TUS_PAGECACHE_SI
4d1f0 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  ZE</dt>.** <dd>T
4d200 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65  his parameter re
4d210 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65 73  cords the larges
4d220 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  t memory allocat
4d230 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 68  ion request.** h
4d240 61 6e 64 65 64 20 74 6f 20 5b 70 61 67 65 63 61  anded to [pageca
4d250 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  che memory alloc
4d260 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20 74 68 65  ator].  Only the
4d270 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
4d280 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68  in the.** *pHigh
4d290 77 61 74 65 72 20 70 61 72 61 6d 65 74 65 72 20  water parameter 
4d2a0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74  to [sqlite3_stat
4d2b0 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 65  us()] is of inte
4d2c0 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76  rest.  .** The v
4d2d0 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69 6e 74  alue written int
4d2e0 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e 74 20  o the *pCurrent 
4d2f0 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 6e 64  parameter is und
4d300 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a  efined.</dd>.**.
4d310 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54  ** <dt>SQLITE_ST
4d320 41 54 55 53 5f 53 43 52 41 54 43 48 5f 55 53 45  ATUS_SCRATCH_USE
4d330 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  D</dt>.** <dd>Th
4d340 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74  is parameter ret
4d350 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  urns the number 
4d360 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 75  of allocations u
4d370 73 65 64 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a  sed out of the.*
4d380 2a 20 5b 73 63 72 61 74 63 68 20 6d 65 6d 6f 72  * [scratch memor
4d390 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 63 6f 6e  y allocator] con
4d3a0 66 69 67 75 72 65 64 20 75 73 69 6e 67 0a 2a 2a  figured using.**
4d3b0 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f   [SQLITE_CONFIG_
4d3c0 53 43 52 41 54 43 48 5d 2e 20 20 54 68 65 20 76  SCRATCH].  The v
4d3d0 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73  alue returned is
4d3e0 20 69 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2c   in allocations,
4d3f0 20 6e 6f 74 0a 2a 2a 20 69 6e 20 62 79 74 65 73   not.** in bytes
4d400 2e 20 20 53 69 6e 63 65 20 61 20 73 69 6e 67 6c  .  Since a singl
4d410 65 20 74 68 72 65 61 64 20 6d 61 79 20 6f 6e 6c  e thread may onl
4d420 79 20 68 61 76 65 20 6f 6e 65 20 73 63 72 61 74  y have one scrat
4d430 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a  ch allocation.**
4d440 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74 20   outstanding at 
4d450 74 69 6d 65 2c 20 74 68 69 73 20 70 61 72 61 6d  time, this param
4d460 65 74 65 72 20 61 6c 73 6f 20 72 65 70 6f 72 74  eter also report
4d470 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
4d480 74 68 72 65 61 64 73 0a 2a 2a 20 75 73 69 6e 67  threads.** using
4d490 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20   scratch memory 
4d4a0 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65  at the same time
4d4b0 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
4d4c0 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53  >SQLITE_STATUS_S
4d4d0 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 3c  CRATCH_OVERFLOW<
4d4e0 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
4d4f0 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72   parameter retur
4d500 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
4d510 20 62 79 74 65 73 20 6f 66 20 73 63 72 61 74 63   bytes of scratc
4d520 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f  h memory.** allo
4d530 63 61 74 69 6f 6e 20 77 68 69 63 68 20 63 6f 75  cation which cou
4d540 6c 64 20 6e 6f 74 20 62 65 20 73 74 61 74 69 73  ld not be statis
4d550 66 69 65 64 20 62 79 20 74 68 65 20 5b 53 51 4c  fied by the [SQL
4d560 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54  ITE_CONFIG_SCRAT
4d570 43 48 5d 0a 2a 2a 20 62 75 66 66 65 72 20 61 6e  CH].** buffer an
4d580 64 20 77 68 65 72 65 20 66 6f 72 63 65 64 20 74  d where forced t
4d590 6f 20 6f 76 65 72 66 6c 6f 77 20 74 6f 20 5b 73  o overflow to [s
4d5a0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
4d5b0 2e 20 20 54 68 65 20 76 61 6c 75 65 73 0a 2a 2a  .  The values.**
4d5c0 20 72 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 64   returned includ
4d5d0 65 20 6f 76 65 72 66 6c 6f 77 73 20 62 65 63 61  e overflows beca
4d5e0 75 73 65 20 74 68 65 20 72 65 71 75 65 73 74 65  use the requeste
4d5f0 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73  d allocation was
4d600 20 74 6f 6f 0a 2a 2a 20 6c 61 72 67 65 72 20 28   too.** larger (
4d610 74 68 61 74 20 69 73 2c 20 62 65 63 61 75 73 65  that is, because
4d620 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 61   the requested a
4d630 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 20 6c 61  llocation was la
4d640 72 67 65 72 20 74 68 61 6e 20 74 68 65 0a 2a 2a  rger than the.**
4d650 20 22 73 7a 22 20 70 61 72 61 6d 65 74 65 72 20   "sz" parameter 
4d660 74 6f 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  to [SQLITE_CONFI
4d670 47 5f 53 43 52 41 54 43 48 5d 29 20 61 6e 64 20  G_SCRATCH]) and 
4d680 62 65 63 61 75 73 65 20 6e 6f 20 73 63 72 61 74  because no scrat
4d690 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 73 6c 6f  ch buffer.** slo
4d6a0 74 73 20 77 65 72 65 20 61 76 61 69 6c 61 62 6c  ts were availabl
4d6b0 65 2e 0a 2a 2a 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a  e..** </dd>.**.*
4d6c0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41  * <dt>SQLITE_STA
4d6d0 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45  TUS_SCRATCH_SIZE
4d6e0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69  </dt>.** <dd>Thi
4d6f0 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f  s parameter reco
4d700 72 64 73 20 74 68 65 20 6c 61 72 67 65 73 74 20  rds the largest 
4d710 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
4d720 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 68 61 6e  n request.** han
4d730 64 65 64 20 74 6f 20 5b 73 63 72 61 74 63 68 20  ded to [scratch 
4d740 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
4d750 5d 2e 20 20 4f 6e 6c 79 20 74 68 65 20 76 61 6c  ].  Only the val
4d760 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e 20 74  ue returned in t
4d770 68 65 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65  he.** *pHighwate
4d780 72 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b  r parameter to [
4d790 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29  sqlite3_status()
4d7a0 5d 20 69 73 20 6f 66 20 69 6e 74 65 72 65 73 74  ] is of interest
4d7b0 2e 20 20 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65  .  .** The value
4d7c0 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
4d7d0 65 20 2a 70 43 75 72 72 65 6e 74 20 70 61 72 61  e *pCurrent para
4d7e0 6d 65 74 65 72 20 69 73 20 75 6e 64 65 66 69 6e  meter is undefin
4d7f0 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  ed.</dd>.**.** <
4d800 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53  dt>SQLITE_STATUS
4d810 5f 50 41 52 53 45 52 5f 53 54 41 43 4b 3c 2f 64  _PARSER_STACK</d
4d820 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70  t>.** <dd>This p
4d830 61 72 61 6d 65 74 65 72 20 72 65 63 6f 72 64 73  arameter records
4d840 20 74 68 65 20 64 65 65 70 65 73 74 20 70 61 72   the deepest par
4d850 73 65 72 20 73 74 61 63 6b 2e 20 20 49 74 20 69  ser stack.  It i
4d860 73 20 6f 6e 6c 79 0a 2a 2a 20 6d 65 61 6e 69 6e  s only.** meanin
4d870 67 66 75 6c 20 69 66 20 53 51 4c 69 74 65 20 69  gful if SQLite i
4d880 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20  s compiled with 
4d890 5b 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b  [YYTRACKMAXSTACK
4d8a0 44 45 50 54 48 5d 2e 3c 2f 64 64 3e 0a 2a 2a 20  DEPTH].</dd>.** 
4d8b0 3c 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20  </dl>.**.** New 
4d8c0 73 74 61 74 75 73 20 70 61 72 61 6d 65 74 65 72  status parameter
4d8d0 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 66  s may be added f
4d8e0 72 6f 6d 20 74 69 6d 65 20 74 6f 20 74 69 6d 65  rom time to time
4d8f0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
4d900 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52  ITE_STATUS_MEMOR
4d910 59 5f 55 53 45 44 20 20 20 20 20 20 20 20 20 20  Y_USED          
4d920 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  0.#define SQLITE
4d930 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48  _STATUS_PAGECACH
4d940 45 5f 55 53 45 44 20 20 20 20 20 20 20 31 0a 23  E_USED       1.#
4d950 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54  define SQLITE_ST
4d960 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f  ATUS_PAGECACHE_O
4d970 56 45 52 46 4c 4f 57 20 20 20 32 0a 23 64 65 66  VERFLOW   2.#def
4d980 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55  ine SQLITE_STATU
4d990 53 5f 53 43 52 41 54 43 48 5f 55 53 45 44 20 20  S_SCRATCH_USED  
4d9a0 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65         3.#define
4d9b0 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53   SQLITE_STATUS_S
4d9c0 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 20  CRATCH_OVERFLOW 
4d9d0 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51      4.#define SQ
4d9e0 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c  LITE_STATUS_MALL
4d9f0 4f 43 5f 53 49 5a 45 20 20 20 20 20 20 20 20 20  OC_SIZE         
4da00 20 35 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   5.#define SQLIT
4da10 45 5f 53 54 41 54 55 53 5f 50 41 52 53 45 52 5f  E_STATUS_PARSER_
4da20 53 54 41 43 4b 20 20 20 20 20 20 20 20 20 36 0a  STACK         6.
4da30 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
4da40 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f  TATUS_PAGECACHE_
4da50 53 49 5a 45 20 20 20 20 20 20 20 37 0a 23 64 65  SIZE       7.#de
4da60 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54  fine SQLITE_STAT
4da70 55 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 20  US_SCRATCH_SIZE 
4da80 20 20 20 20 20 20 20 20 38 0a 0a 2f 2a 0a 2a 2a          8../*.**
4da90 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61 62   CAPI3REF: Datab
4daa0 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 53  ase Connection S
4dab0 74 61 74 75 73 20 7b 48 31 37 35 30 30 7d 20 3c  tatus {H17500} <
4dac0 53 36 30 32 30 30 3e 0a 2a 2a 20 45 58 50 45 52  S60200>.** EXPER
4dad0 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68  IMENTAL.**.** Th
4dae0 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  is interface is 
4daf0 75 73 65 64 20 74 6f 20 72 65 74 72 69 65 76 65  used to retrieve
4db00 20 72 75 6e 74 69 6d 65 20 73 74 61 74 75 73 20   runtime status 
4db10 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 0a 2a 2a 20  information .** 
4db20 61 62 6f 75 74 20 61 20 73 69 6e 67 6c 65 20 5b  about a single [
4db30 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
4db40 69 6f 6e 5d 2e 20 20 54 68 65 20 66 69 72 73 74  ion].  The first
4db50 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
4db60 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e  .** database con
4db70 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63 74 20 74  nection object t
4db80 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65  o be interrogate
4db90 64 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61  d.  The second a
4dba0 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 74 68  rgument.** is th
4dbb0 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 69  e parameter to i
4dbc0 6e 74 65 72 72 6f 67 61 74 65 2e 20 20 43 75 72  nterrogate.  Cur
4dbd0 72 65 6e 74 6c 79 2c 20 74 68 65 20 6f 6e 6c 79  rently, the only
4dbe0 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 0a 2a   allowed value.*
4dbf0 2a 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64  * for the second
4dc00 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 5b 53   parameter is [S
4dc10 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c  QLITE_DBSTATUS_L
4dc20 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 5d 2e 0a  OOKASIDE_USED]..
4dc30 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 6f 70  ** Additional op
4dc40 74 69 6f 6e 73 20 77 69 6c 6c 20 6c 69 6b 65 6c  tions will likel
4dc50 79 20 61 70 70 65 61 72 20 69 6e 20 66 75 74 75  y appear in futu
4dc60 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53  re releases of S
4dc70 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  QLite..**.** The
4dc80 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f   current value o
4dc90 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  f the requested 
4dca0 70 61 72 61 6d 65 74 65 72 20 69 73 20 77 72 69  parameter is wri
4dcb0 74 74 65 6e 20 69 6e 74 6f 20 2a 70 43 75 72 0a  tten into *pCur.
4dcc0 2a 2a 20 61 6e 64 20 74 68 65 20 68 69 67 68 65  ** and the highe
4dcd0 73 74 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73  st instantaneous
4dce0 20 76 61 6c 75 65 20 69 73 20 77 72 69 74 74 65   value is writte
4dcf0 6e 20 69 6e 74 6f 20 2a 70 48 69 77 74 72 2e 20  n into *pHiwtr. 
4dd00 20 49 66 0a 2a 2a 20 74 68 65 20 72 65 73 65 74   If.** the reset
4dd10 46 6c 67 20 69 73 20 74 72 75 65 2c 20 74 68 65  Flg is true, the
4dd20 6e 20 74 68 65 20 68 69 67 68 65 73 74 20 69 6e  n the highest in
4dd30 73 74 61 6e 74 61 6e 65 6f 75 73 20 76 61 6c 75  stantaneous valu
4dd40 65 20 69 73 0a 2a 2a 20 72 65 73 65 74 20 62 61  e is.** reset ba
4dd50 63 6b 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 63  ck down to the c
4dd60 75 72 72 65 6e 74 20 76 61 6c 75 65 2e 0a 2a 2a  urrent value..**
4dd70 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73  .** See also: [s
4dd80 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 5d  qlite3_status()]
4dd90 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74   and [sqlite3_st
4dda0 6d 74 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2f  mt_status()]..*/
4ddb0 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49  .SQLITE_API SQLI
4ddc0 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20  TE_EXPERIMENTAL 
4ddd0 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 62 5f 73  int sqlite3_db_s
4dde0 74 61 74 75 73 28 73 71 6c 69 74 65 33 2a 2c 20  tatus(sqlite3*, 
4ddf0 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75  int op, int *pCu
4de00 72 2c 20 69 6e 74 20 2a 70 48 69 77 74 72 2c 20  r, int *pHiwtr, 
4de10 69 6e 74 20 72 65 73 65 74 46 6c 67 29 3b 0a 0a  int resetFlg);..
4de20 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
4de30 53 74 61 74 75 73 20 50 61 72 61 6d 65 74 65 72  Status Parameter
4de40 73 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 63  s for database c
4de50 6f 6e 6e 65 63 74 69 6f 6e 73 20 7b 48 31 37 35  onnections {H175
4de60 32 30 7d 20 3c 48 31 37 35 30 30 3e 0a 2a 2a 20  20} <H17500>.** 
4de70 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a  EXPERIMENTAL.**.
4de80 2a 2a 20 53 74 61 74 75 73 20 76 65 72 62 73 20  ** Status verbs 
4de90 66 6f 72 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f  for [sqlite3_db_
4dea0 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2a 0a 2a 2a  status()]..**.**
4deb0 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c   <dl>.** <dt>SQL
4dec0 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f  ITE_DBSTATUS_LOO
4ded0 4b 41 53 49 44 45 5f 55 53 45 44 3c 2f 64 74 3e  KASIDE_USED</dt>
4dee0 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72  .** <dd>This par
4def0 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 74  ameter returns t
4df00 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 6f  he number of loo
4df10 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 73 6c  kaside memory sl
4df20 6f 74 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a  ots currently.**
4df30 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 3c 2f 64   checked out.</d
4df40 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23  d>.** </dl>.*/.#
4df50 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42  define SQLITE_DB
4df60 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45  STATUS_LOOKASIDE
4df70 5f 55 53 45 44 20 20 20 20 20 30 0a 0a 0a 2f 2a  _USED     0.../*
4df80 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 50 72  .** CAPI3REF: Pr
4df90 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74  epared Statement
4dfa0 20 53 74 61 74 75 73 20 7b 48 31 37 35 35 30 7d   Status {H17550}
4dfb0 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 45 58 50   <S60200>.** EXP
4dfc0 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20  ERIMENTAL.**.** 
4dfd0 45 61 63 68 20 70 72 65 70 61 72 65 64 20 73 74  Each prepared st
4dfe0 61 74 65 6d 65 6e 74 20 6d 61 69 6e 74 61 69 6e  atement maintain
4dff0 73 20 76 61 72 69 6f 75 73 0a 2a 2a 20 5b 53 51  s various.** [SQ
4e000 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f  LITE_STMTSTATUS_
4e010 53 4f 52 54 20 7c 20 63 6f 75 6e 74 65 72 73 5d  SORT | counters]
4e020 20 74 68 61 74 20 6d 65 61 73 75 72 65 20 74 68   that measure th
4e030 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 74  e number.** of t
4e040 69 6d 65 73 20 69 74 20 68 61 73 20 70 65 72 66  imes it has perf
4e050 6f 72 6d 65 64 20 73 70 65 63 69 66 69 63 20 6f  ormed specific o
4e060 70 65 72 61 74 69 6f 6e 73 2e 20 20 54 68 65 73  perations.  Thes
4e070 65 20 63 6f 75 6e 74 65 72 73 20 63 61 6e 0a 2a  e counters can.*
4e080 2a 20 62 65 20 75 73 65 64 20 74 6f 20 6d 6f 6e  * be used to mon
4e090 69 74 6f 72 20 74 68 65 20 70 65 72 66 6f 72 6d  itor the perform
4e0a0 61 6e 63 65 20 63 68 61 72 61 63 74 65 72 69 73  ance characteris
4e0b0 74 69 63 73 20 6f 66 20 74 68 65 20 70 72 65 70  tics of the prep
4e0c0 61 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  ared.** statemen
4e0d0 74 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65  ts.  For example
4e0e0 2c 20 69 66 20 74 68 65 20 6e 75 6d 62 65 72 20  , if the number 
4e0f0 6f 66 20 74 61 62 6c 65 20 73 74 65 70 73 20 67  of table steps g
4e100 72 65 61 74 6c 79 20 65 78 63 65 65 64 73 0a 2a  reatly exceeds.*
4e110 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * the number of 
4e120 74 61 62 6c 65 20 73 65 61 72 63 68 65 73 20 6f  table searches o
4e130 72 20 72 65 73 75 6c 74 20 72 6f 77 73 2c 20 74  r result rows, t
4e140 68 61 74 20 77 6f 75 6c 64 20 74 65 6e 64 20 74  hat would tend t
4e150 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 74 68  o indicate.** th
4e160 61 74 20 74 68 65 20 70 72 65 70 61 72 65 64 20  at the prepared 
4e170 73 74 61 74 65 6d 65 6e 74 20 69 73 20 75 73 69  statement is usi
4e180 6e 67 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20  ng a full table 
4e190 73 63 61 6e 20 72 61 74 68 65 72 20 74 68 61 6e  scan rather than
4e1a0 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 2e 20 20 0a  .** an index.  .
4e1b0 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72  **.** This inter
4e1c0 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20  face is used to 
4e1d0 72 65 74 72 69 65 76 65 20 61 6e 64 20 72 65 73  retrieve and res
4e1e0 65 74 20 63 6f 75 6e 74 65 72 20 76 61 6c 75 65  et counter value
4e1f0 73 20 66 72 6f 6d 0a 2a 2a 20 61 20 5b 70 72 65  s from.** a [pre
4e200 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
4e210 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67  .  The first arg
4e220 75 6d 65 6e 74 20 69 73 20 74 68 65 20 70 72 65  ument is the pre
4e230 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a  pared statement.
4e240 2a 2a 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20  ** object to be 
4e250 69 6e 74 65 72 72 6f 67 61 74 65 64 2e 20 20 54  interrogated.  T
4e260 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
4e270 6e 74 0a 2a 2a 20 69 73 20 61 6e 20 69 6e 74 65  nt.** is an inte
4e280 67 65 72 20 63 6f 64 65 20 66 6f 72 20 61 20 73  ger code for a s
4e290 70 65 63 69 66 69 63 20 5b 53 51 4c 49 54 45 5f  pecific [SQLITE_
4e2a0 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 20  STMTSTATUS_SORT 
4e2b0 7c 20 63 6f 75 6e 74 65 72 5d 0a 2a 2a 20 74 6f  | counter].** to
4e2c0 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65 64   be interrogated
4e2d0 2e 20 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e  . .** The curren
4e2e0 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72  t value of the r
4e2f0 65 71 75 65 73 74 65 64 20 63 6f 75 6e 74 65 72  equested counter
4e300 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
4e310 20 49 66 20 74 68 65 20 72 65 73 65 74 46 6c 67   If the resetFlg
4e320 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74   is true, then t
4e330 68 65 20 63 6f 75 6e 74 65 72 20 69 73 20 72 65  he counter is re
4e340 73 65 74 20 74 6f 20 7a 65 72 6f 20 61 66 74 65  set to zero afte
4e350 72 20 74 68 69 73 0a 2a 2a 20 69 6e 74 65 72 66  r this.** interf
4e360 61 63 65 20 63 61 6c 6c 20 72 65 74 75 72 6e 73  ace call returns
4e370 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
4e380 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75  : [sqlite3_statu
4e390 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  s()] and [sqlite
4e3a0 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 2e 0a  3_db_status()]..
4e3b0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51  */.SQLITE_API SQ
4e3c0 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41  LITE_EXPERIMENTA
4e3d0 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74  L int sqlite3_st
4e3e0 6d 74 5f 73 74 61 74 75 73 28 73 71 6c 69 74 65  mt_status(sqlite
4e3f0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 6f 70 2c  3_stmt*, int op,
4e400 69 6e 74 20 72 65 73 65 74 46 6c 67 29 3b 0a 0a  int resetFlg);..
4e410 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
4e420 53 74 61 74 75 73 20 50 61 72 61 6d 65 74 65 72  Status Parameter
4e430 73 20 66 6f 72 20 70 72 65 70 61 72 65 64 20 73  s for prepared s
4e440 74 61 74 65 6d 65 6e 74 73 20 7b 48 31 37 35 37  tatements {H1757
4e450 30 7d 20 3c 48 31 37 35 35 30 3e 0a 2a 2a 20 45  0} <H17550>.** E
4e460 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a  XPERIMENTAL.**.*
4e470 2a 20 54 68 65 73 65 20 70 72 65 70 72 6f 63 65  * These preproce
4e480 73 73 6f 72 20 6d 61 63 72 6f 73 20 64 65 66 69  ssor macros defi
4e490 6e 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73  ne integer codes
4e4a0 20 74 68 61 74 20 6e 61 6d 65 20 63 6f 75 6e 74   that name count
4e4b0 65 72 0a 2a 2a 20 76 61 6c 75 65 73 20 61 73 73  er.** values ass
4e4c0 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
4e4d0 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f 73   [sqlite3_stmt_s
4e4e0 74 61 74 75 73 28 29 5d 20 69 6e 74 65 72 66 61  tatus()] interfa
4e4f0 63 65 2e 0a 2a 2a 20 54 68 65 20 6d 65 61 6e 69  ce..** The meani
4e500 6e 67 73 20 6f 66 20 74 68 65 20 76 61 72 69 6f  ngs of the vario
4e510 75 73 20 63 6f 75 6e 74 65 72 73 20 61 72 65 20  us counters are 
4e520 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
4e530 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51  * <dl>.** <dt>SQ
4e540 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f  LITE_STMTSTATUS_
4e550 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50 3c 2f 64  FULLSCAN_STEP</d
4e560 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 69  t>.** <dd>This i
4e570 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
4e580 74 69 6d 65 73 20 74 68 61 74 20 53 51 4c 69 74  times that SQLit
4e590 65 20 68 61 73 20 73 74 65 70 70 65 64 20 66 6f  e has stepped fo
4e5a0 72 77 61 72 64 20 69 6e 0a 2a 2a 20 61 20 74 61  rward in.** a ta
4e5b0 62 6c 65 20 61 73 20 70 61 72 74 20 6f 66 20 61  ble as part of a
4e5c0 20 66 75 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e   full table scan
4e5d0 2e 20 20 4c 61 72 67 65 20 6e 75 6d 62 65 72 73  .  Large numbers
4e5e0 20 66 6f 72 20 74 68 69 73 20 63 6f 75 6e 74 65   for this counte
4e5f0 72 0a 2a 2a 20 6d 61 79 20 69 6e 64 69 63 61 74  r.** may indicat
4e600 65 20 6f 70 70 6f 72 74 75 6e 69 74 69 65 73 20  e opportunities 
4e610 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  for performance 
4e620 69 6d 70 72 6f 76 65 6d 65 6e 74 20 74 68 72 6f  improvement thro
4e630 75 67 68 20 0a 2a 2a 20 63 61 72 65 66 75 6c 20  ugh .** careful 
4e640 75 73 65 20 6f 66 20 69 6e 64 69 63 65 73 2e 3c  use of indices.<
4e650 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53  /dd>.**.** <dt>S
4e660 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53  QLITE_STMTSTATUS
4e670 5f 53 4f 52 54 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  _SORT</dt>.** <d
4e680 64 3e 54 68 69 73 20 69 73 20 74 68 65 20 6e 75  d>This is the nu
4e690 6d 62 65 72 20 6f 66 20 73 6f 72 74 20 6f 70 65  mber of sort ope
4e6a0 72 61 74 69 6f 6e 73 20 74 68 61 74 20 68 61 76  rations that hav
4e6b0 65 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 20 41  e occurred..** A
4e6c0 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20   non-zero value 
4e6d0 69 6e 20 74 68 69 73 20 63 6f 75 6e 74 65 72 20  in this counter 
4e6e0 6d 61 79 20 69 6e 64 69 63 61 74 65 20 61 6e 20  may indicate an 
4e6f0 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 0a 2a  opportunity to.*
4e700 2a 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 70 65  * improvement pe
4e710 72 66 6f 72 6d 61 6e 63 65 20 74 68 72 6f 75 67  rformance throug
4e720 68 20 63 61 72 65 66 75 6c 20 75 73 65 20 6f 66  h careful use of
4e730 20 69 6e 64 69 63 65 73 2e 3c 2f 64 64 3e 0a 2a   indices.</dd>.*
4e740 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64  *.** </dl>.*/.#d
4e750 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 4d  efine SQLITE_STM
4e760 54 53 54 41 54 55 53 5f 46 55 4c 4c 53 43 41 4e  TSTATUS_FULLSCAN
4e770 5f 53 54 45 50 20 20 20 20 20 31 0a 23 64 65 66  _STEP     1.#def
4e780 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 4d 54 53  ine SQLITE_STMTS
4e790 54 41 54 55 53 5f 53 4f 52 54 20 20 20 20 20 20  TATUS_SORT      
4e7a0 20 20 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a          2../*.**
4e7b0 20 55 6e 64 6f 20 74 68 65 20 68 61 63 6b 20 74   Undo the hack t
4e7c0 68 61 74 20 63 6f 6e 76 65 72 74 73 20 66 6c 6f  hat converts flo
4e7d0 61 74 69 6e 67 20 70 6f 69 6e 74 20 74 79 70 65  ating point type
4e7e0 73 20 74 6f 20 69 6e 74 65 67 65 72 20 66 6f 72  s to integer for
4e7f0 0a 2a 2a 20 62 75 69 6c 64 73 20 6f 6e 20 70 72  .** builds on pr
4e800 6f 63 65 73 73 6f 72 73 20 77 69 74 68 6f 75 74  ocessors without
4e810 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20   floating point 
4e820 73 75 70 70 6f 72 74 2e 0a 2a 2f 0a 23 69 66 64  support..*/.#ifd
4e830 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
4e840 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20  LOATING_POINT.# 
4e850 75 6e 64 65 66 20 64 6f 75 62 6c 65 0a 23 65 6e  undef double.#en
4e860 64 69 66 0a 0a 23 69 66 20 30 0a 7d 20 20 2f 2a  dif..#if 0.}  /*
4e870 20 45 6e 64 20 6f 66 20 74 68 65 20 27 65 78 74   End of the 'ext
4e880 65 72 6e 20 22 43 22 27 20 62 6c 6f 63 6b 20 2a  ern "C"' block *
4e890 2f 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  /.#endif.#endif.
4e8a0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
4e8b0 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 2e   End of sqlite3.
4e8c0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
4e8d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e8e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
4e8f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
4e900 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
4e910 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
4e920 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
4e930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
4e940 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
4e950 20 49 6e 63 6c 75 64 65 20 68 61 73 68 2e 68 20   Include hash.h 
4e960 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
4e970 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
4e980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
4e990 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
4e9a0 20 42 65 67 69 6e 20 66 69 6c 65 20 68 61 73 68   Begin file hash
4e9b0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
4e9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
4e9e0 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
4e9f0 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54  ember 22.**.** T
4ea00 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
4ea10 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
4ea20 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
4ea30 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
4ea40 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
4ea50 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
4ea60 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
4ea70 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
4ea80 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
4ea90 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
4eaa0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
4eab0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
4eac0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
4ead0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
4eae0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
4eaf0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
4eb00 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
4eb10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4eb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4eb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4eb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4eb50 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
4eb60 69 73 20 74 68 65 20 68 65 61 64 65 72 20 66 69  is the header fi
4eb70 6c 65 20 66 6f 72 20 74 68 65 20 67 65 6e 65 72  le for the gener
4eb80 69 63 20 68 61 73 68 2d 74 61 62 6c 65 20 69 6d  ic hash-table im
4eb90 70 6c 65 6d 65 6e 61 74 69 6f 6e 0a 2a 2a 20 75  plemenation.** u
4eba0 73 65 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a  sed in SQLite..*
4ebb0 2a 0a 2a 2a 20 24 49 64 3a 20 68 61 73 68 2e 68  *.** $Id: hash.h
4ebc0 2c 76 20 31 2e 31 32 20 32 30 30 38 2f 31 30 2f  ,v 1.12 2008/10/
4ebd0 31 30 20 31 37 3a 34 31 3a 32 39 20 64 72 68 20  10 17:41:29 drh 
4ebe0 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66  Exp $.*/.#ifndef
4ebf0 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f   _SQLITE_HASH_H_
4ec00 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45  .#define _SQLITE
4ec10 5f 48 41 53 48 5f 48 5f 0a 0a 2f 2a 20 46 6f 72  _HASH_H_../* For
4ec20 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e  ward declaration
4ec30 73 20 6f 66 20 73 74 72 75 63 74 75 72 65 73 2e  s of structures.
4ec40 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75   */.typedef stru
4ec50 63 74 20 48 61 73 68 20 48 61 73 68 3b 0a 74 79  ct Hash Hash;.ty
4ec60 70 65 64 65 66 20 73 74 72 75 63 74 20 48 61 73  pedef struct Has
4ec70 68 45 6c 65 6d 20 48 61 73 68 45 6c 65 6d 3b 0a  hElem HashElem;.
4ec80 0a 2f 2a 20 41 20 63 6f 6d 70 6c 65 74 65 20 68  ./* A complete h
4ec90 61 73 68 20 74 61 62 6c 65 20 69 73 20 61 6e 20  ash table is an 
4eca0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
4ecb0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
4ecc0 75 72 65 2e 0a 2a 2a 20 54 68 65 20 69 6e 74 65  ure..** The inte
4ecd0 72 6e 61 6c 73 20 6f 66 20 74 68 69 73 20 73 74  rnals of this st
4ece0 72 75 63 74 75 72 65 20 61 72 65 20 69 6e 74 65  ructure are inte
4ecf0 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71 75  nded to be opaqu
4ed00 65 20 2d 2d 20 63 6c 69 65 6e 74 0a 2a 2a 20 63  e -- client.** c
4ed10 6f 64 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 61  ode should not a
4ed20 74 74 65 6d 70 74 20 74 6f 20 61 63 63 65 73 73  ttempt to access
4ed30 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 65 20 66   or modify the f
4ed40 69 65 6c 64 73 20 6f 66 20 74 68 69 73 20 73 74  ields of this st
4ed50 72 75 63 74 75 72 65 0a 2a 2a 20 64 69 72 65 63  ructure.** direc
4ed60 74 6c 79 2e 20 20 43 68 61 6e 67 65 20 74 68 69  tly.  Change thi
4ed70 73 20 73 74 72 75 63 74 75 72 65 20 6f 6e 6c 79  s structure only
4ed80 20 62 79 20 75 73 69 6e 67 20 74 68 65 20 72 6f   by using the ro
4ed90 75 74 69 6e 65 73 20 62 65 6c 6f 77 2e 0a 2a 2a  utines below..**
4eda0 20 48 6f 77 65 76 65 72 2c 20 6d 61 6e 79 20 6f   However, many o
4edb0 66 20 74 68 65 20 22 70 72 6f 63 65 64 75 72 65  f the "procedure
4edc0 73 22 20 61 6e 64 20 22 66 75 6e 63 74 69 6f 6e  s" and "function
4edd0 73 22 20 66 6f 72 20 6d 6f 64 69 66 79 69 6e 67  s" for modifying
4ede0 20 61 6e 64 0a 2a 2a 20 61 63 63 65 73 73 69 6e   and.** accessin
4edf0 67 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  g this structure
4ee00 20 61 72 65 20 72 65 61 6c 6c 79 20 6d 61 63 72   are really macr
4ee10 6f 73 2c 20 73 6f 20 77 65 20 63 61 6e 27 74 20  os, so we can't 
4ee20 72 65 61 6c 6c 79 20 6d 61 6b 65 0a 2a 2a 20 74  really make.** t
4ee30 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6f 70  his structure op
4ee40 61 71 75 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  aque..*/.struct 
4ee50 48 61 73 68 20 7b 0a 20 20 75 6e 73 69 67 6e 65  Hash {.  unsigne
4ee60 64 20 69 6e 74 20 63 6f 70 79 4b 65 79 3a 20 31  d int copyKey: 1
4ee70 3b 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 6f  ;  /* True if co
4ee80 70 79 20 6f 66 20 6b 65 79 20 6d 61 64 65 20 6f  py of key made o
4ee90 6e 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20 75 6e  n insert */.  un
4eea0 73 69 67 6e 65 64 20 69 6e 74 20 68 74 73 69 7a  signed int htsiz
4eeb0 65 20 3a 20 33 31 3b 20 2f 2a 20 4e 75 6d 62 65  e : 31; /* Numbe
4eec0 72 20 6f 66 20 62 75 63 6b 65 74 73 20 69 6e 20  r of buckets in 
4eed0 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a  the hash table *
4eee0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  /.  unsigned int
4eef0 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 2f 2a   count;       /*
4ef00 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
4ef10 65 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65  es in this table
4ef20 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a   */.  HashElem *
4ef30 66 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 20  first;          
4ef40 2f 2a 20 54 68 65 20 66 69 72 73 74 20 65 6c 65  /* The first ele
4ef50 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 72 72 61  ment of the arra
4ef60 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 68  y */.  struct _h
4ef70 74 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  t {             
4ef80 20 2f 2a 20 74 68 65 20 68 61 73 68 20 74 61 62   /* the hash tab
4ef90 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63 6f  le */.    int co
4efa0 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  unt;            
4efb0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
4efc0 66 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 74  f entries with t
4efd0 68 69 73 20 68 61 73 68 20 2a 2f 0a 20 20 20 20  his hash */.    
4efe0 48 61 73 68 45 6c 65 6d 20 2a 63 68 61 69 6e 3b  HashElem *chain;
4eff0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
4f000 69 6e 74 65 72 20 74 6f 20 66 69 72 73 74 20 65  inter to first e
4f010 6e 74 72 79 20 77 69 74 68 20 74 68 69 73 20 68  ntry with this h
4f020 61 73 68 20 2a 2f 0a 20 20 7d 20 2a 68 74 3b 0a  ash */.  } *ht;.
4f030 7d 3b 0a 0a 2f 2a 20 45 61 63 68 20 65 6c 65 6d  };../* Each elem
4f040 65 6e 74 20 69 6e 20 74 68 65 20 68 61 73 68 20  ent in the hash 
4f050 74 61 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 74  table is an inst
4f060 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
4f070 6f 77 69 6e 67 20 0a 2a 2a 20 73 74 72 75 63 74  owing .** struct
4f080 75 72 65 2e 20 20 41 6c 6c 20 65 6c 65 6d 65 6e  ure.  All elemen
4f090 74 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e  ts are stored on
4f0a0 20 61 20 73 69 6e 67 6c 65 20 64 6f 75 62 6c 79   a single doubly
4f0b0 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a  -linked list..**
4f0c0 0a 2a 2a 20 41 67 61 69 6e 2c 20 74 68 69 73 20  .** Again, this 
4f0d0 73 74 72 75 63 74 75 72 65 20 69 73 20 69 6e 74  structure is int
4f0e0 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71  ended to be opaq
4f0f0 75 65 2c 20 62 75 74 20 69 74 20 63 61 6e 27 74  ue, but it can't
4f100 20 72 65 61 6c 6c 79 0a 2a 2a 20 62 65 20 6f 70   really.** be op
4f110 61 71 75 65 20 62 65 63 61 75 73 65 20 69 74 20  aque because it 
4f120 69 73 20 75 73 65 64 20 62 79 20 6d 61 63 72 6f  is used by macro
4f130 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 48 61 73  s..*/.struct Has
4f140 68 45 6c 65 6d 20 7b 0a 20 20 48 61 73 68 45 6c  hElem {.  HashEl
4f150 65 6d 20 2a 6e 65 78 74 2c 20 2a 70 72 65 76 3b  em *next, *prev;
4f160 20 20 20 2f 2a 20 4e 65 78 74 20 61 6e 64 20 70     /* Next and p
4f170 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74 73  revious elements
4f180 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 2a 2f   in the table */
4f190 0a 20 20 76 6f 69 64 20 2a 64 61 74 61 3b 20 20  .  void *data;  
4f1a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
4f1b0 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77  ata associated w
4f1c0 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65 6e 74  ith this element
4f1d0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 4b 65 79   */.  void *pKey
4f1e0 3b 20 69 6e 74 20 6e 4b 65 79 3b 20 20 20 20 2f  ; int nKey;    /
4f1f0 2a 20 4b 65 79 20 61 73 73 6f 63 69 61 74 65 64  * Key associated
4f200 20 77 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65   with this eleme
4f210 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  nt */.};../*.** 
4f220 41 63 63 65 73 73 20 72 6f 75 74 69 6e 65 73 2e  Access routines.
4f230 20 20 54 6f 20 64 65 6c 65 74 65 2c 20 69 6e 73    To delete, ins
4f240 65 72 74 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  ert a NULL point
4f250 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
4f260 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
4f270 65 33 48 61 73 68 49 6e 69 74 28 48 61 73 68 2a  e3HashInit(Hash*
4f280 2c 20 69 6e 74 20 63 6f 70 79 4b 65 79 29 3b 0a  , int copyKey);.
4f290 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
4f2a0 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73 68  oid *sqlite3Hash
4f2b0 49 6e 73 65 72 74 28 48 61 73 68 2a 2c 20 63 6f  Insert(Hash*, co
4f2c0 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
4f2d0 69 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 64 20 2a  int nKey, void *
4f2e0 70 44 61 74 61 29 3b 0a 53 51 4c 49 54 45 5f 50  pData);.SQLITE_P
4f2f0 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
4f300 69 74 65 33 48 61 73 68 46 69 6e 64 28 63 6f 6e  ite3HashFind(con
4f310 73 74 20 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20  st Hash*, const 
4f320 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20  void *pKey, int 
4f330 6e 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52  nKey);.SQLITE_PR
4f340 49 56 41 54 45 20 48 61 73 68 45 6c 65 6d 20 2a  IVATE HashElem *
4f350 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 45  sqlite3HashFindE
4f360 6c 65 6d 28 63 6f 6e 73 74 20 48 61 73 68 2a 2c  lem(const Hash*,
4f370 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
4f380 79 2c 20 69 6e 74 20 6e 4b 65 79 29 3b 0a 53 51  y, int nKey);.SQ
4f390 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
4f3a0 64 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65  d sqlite3HashCle
4f3b0 61 72 28 48 61 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a  ar(Hash*);../*.*
4f3c0 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 6c 6f 6f  * Macros for loo
4f3d0 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c  ping over all el
4f3e0 65 6d 65 6e 74 73 20 6f 66 20 61 20 68 61 73 68  ements of a hash
4f3f0 20 74 61 62 6c 65 2e 20 20 54 68 65 20 69 64 69   table.  The idi
4f400 6f 6d 20 69 73 0a 2a 2a 20 6c 69 6b 65 20 74 68  om is.** like th
4f410 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 48 61 73 68  is:.**.**   Hash
4f420 20 68 3b 0a 2a 2a 20 20 20 48 61 73 68 45 6c 65   h;.**   HashEle
4f430 6d 20 2a 70 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a  m *p;.**   ....*
4f440 2a 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65  *   for(p=sqlite
4f450 48 61 73 68 46 69 72 73 74 28 26 68 29 3b 20 70  HashFirst(&h); p
4f460 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65  ; p=sqliteHashNe
4f470 78 74 28 70 29 29 7b 0a 2a 2a 20 20 20 20 20 53  xt(p)){.**     S
4f480 6f 6d 65 53 74 72 75 63 74 75 72 65 20 2a 70 44  omeStructure *pD
4f490 61 74 61 20 3d 20 73 71 6c 69 74 65 48 61 73 68  ata = sqliteHash
4f4a0 44 61 74 61 28 70 29 3b 0a 2a 2a 20 20 20 20 20  Data(p);.**     
4f4b0 2f 2f 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20  // do something 
4f4c0 77 69 74 68 20 70 44 61 74 61 0a 2a 2a 20 20 20  with pData.**   
4f4d0 7d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c  }.*/.#define sql
4f4e0 69 74 65 48 61 73 68 46 69 72 73 74 28 48 29 20  iteHashFirst(H) 
4f4f0 20 28 28 48 29 2d 3e 66 69 72 73 74 29 0a 23 64   ((H)->first).#d
4f500 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68  efine sqliteHash
4f510 4e 65 78 74 28 45 29 20 20 20 28 28 45 29 2d 3e  Next(E)   ((E)->
4f520 6e 65 78 74 29 0a 23 64 65 66 69 6e 65 20 73 71  next).#define sq
4f530 6c 69 74 65 48 61 73 68 44 61 74 61 28 45 29 20  liteHashData(E) 
4f540 20 20 28 28 45 29 2d 3e 64 61 74 61 29 0a 23 64    ((E)->data).#d
4f550 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68  efine sqliteHash
4f560 4b 65 79 28 45 29 20 20 20 20 28 28 45 29 2d 3e  Key(E)    ((E)->
4f570 70 4b 65 79 29 0a 23 64 65 66 69 6e 65 20 73 71  pKey).#define sq
4f580 6c 69 74 65 48 61 73 68 4b 65 79 73 69 7a 65 28  liteHashKeysize(
4f590 45 29 20 28 28 45 29 2d 3e 6e 4b 65 79 29 0a 0a  E) ((E)->nKey)..
4f5a0 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20  /*.** Number of 
4f5b0 65 6e 74 72 69 65 73 20 69 6e 20 61 20 68 61 73  entries in a has
4f5c0 68 20 74 61 62 6c 65 0a 2a 2f 0a 23 64 65 66 69  h table.*/.#defi
4f5d0 6e 65 20 73 71 6c 69 74 65 48 61 73 68 43 6f 75  ne sqliteHashCou
4f5e0 6e 74 28 48 29 20 20 28 28 48 29 2d 3e 63 6f 75  nt(H)  ((H)->cou
4f5f0 6e 74 29 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f  nt)..#endif /* _
4f600 53 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f 20 2a  SQLITE_HASH_H_ *
4f610 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
4f620 2a 2a 20 45 6e 64 20 6f 66 20 68 61 73 68 2e 68  ** End of hash.h
4f630 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
4f640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f660 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
4f670 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
4f680 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
4f690 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  in sqliteInt.h *
4f6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f6b0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
4f6c0 2a 2a 20 49 6e 63 6c 75 64 65 20 70 61 72 73 65  ** Include parse
4f6d0 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
4f6e0 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   of sqliteInt.h 
4f6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f700 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
4f710 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 61  ** Begin file pa
4f720 72 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  rse.h **********
4f730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f750 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45  */.#define TK_SE
4f760 4d 49 20 20 20 20 20 20 20 20 20 20 20 20 20 20  MI              
4f770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a                1.
4f780 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 50 4c 41  #define TK_EXPLA
4f790 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  IN              
4f7a0 20 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65             2.#de
4f7b0 66 69 6e 65 20 54 4b 5f 51 55 45 52 59 20 20 20  fine TK_QUERY   
4f7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f7d0 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e          3.#defin
4f7e0 65 20 54 4b 5f 50 4c 41 4e 20 20 20 20 20 20 20  e TK_PLAN       
4f7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f800 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 54       4.#define T
4f810 4b 5f 42 45 47 49 4e 20 20 20 20 20 20 20 20 20  K_BEGIN         
4f820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f830 20 20 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54    5.#define TK_T
4f840 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 20 20  RANSACTION      
4f850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
4f860 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 45  .#define TK_DEFE
4f870 52 52 45 44 20 20 20 20 20 20 20 20 20 20 20 20  RRED            
4f880 20 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 64              7.#d
4f890 65 66 69 6e 65 20 54 4b 5f 49 4d 4d 45 44 49 41  efine TK_IMMEDIA
4f8a0 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TE              
4f8b0 20 20 20 20 20 20 20 20 20 38 0a 23 64 65 66 69           8.#defi
4f8c0 6e 65 20 54 4b 5f 45 58 43 4c 55 53 49 56 45 20  ne TK_EXCLUSIVE 
4f8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f8e0 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e 65 20        9.#define 
4f8f0 54 4b 5f 43 4f 4d 4d 49 54 20 20 20 20 20 20 20  TK_COMMIT       
4f900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f910 20 20 31 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f    10.#define TK_
4f920 45 4e 44 20 20 20 20 20 20 20 20 20 20 20 20 20  END             
4f930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4f940 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 4f 4c  1.#define TK_ROL
4f950 4c 42 41 43 4b 20 20 20 20 20 20 20 20 20 20 20  LBACK           
4f960 20 20 20 20 20 20 20 20 20 20 20 20 31 32 0a 23              12.#
4f970 64 65 66 69 6e 65 20 54 4b 5f 43 52 45 41 54 45  define TK_CREATE
4f980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f990 20 20 20 20 20 20 20 20 20 31 33 0a 23 64 65 66           13.#def
4f9a0 69 6e 65 20 54 4b 5f 54 41 42 4c 45 20 20 20 20  ine TK_TABLE    
4f9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f9c0 20 20 20 20 20 20 31 34 0a 23 64 65 66 69 6e 65        14.#define
4f9d0 20 54 4b 5f 49 46 20 20 20 20 20 20 20 20 20 20   TK_IF          
4f9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f9f0 20 20 20 31 35 0a 23 64 65 66 69 6e 65 20 54 4b     15.#define TK
4fa00 5f 4e 4f 54 20 20 20 20 20 20 20 20 20 20 20 20  _NOT            
4fa10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fa20 31 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58  16.#define TK_EX
4fa30 49 53 54 53 20 20 20 20 20 20 20 20 20 20 20 20  ISTS            
4fa40 20 20 20 20 20 20 20 20 20 20 20 20 20 31 37 0a               17.
4fa50 23 64 65 66 69 6e 65 20 54 4b 5f 54 45 4d 50 20  #define TK_TEMP 
4fa60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fa70 20 20 20 20 20 20 20 20 20 20 31 38 0a 23 64 65            18.#de
4fa80 66 69 6e 65 20 54 4b 5f 4c 50 20 20 20 20 20 20  fine TK_LP      
4fa90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4faa0 20 20 20 20 20 20 20 31 39 0a 23 64 65 66 69 6e         19.#defin
4fab0 65 20 54 4b 5f 52 50 20 20 20 20 20 20 20 20 20  e TK_RP         
4fac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fad0 20 20 20 20 32 30 0a 23 64 65 66 69 6e 65 20 54      20.#define T
4fae0 4b 5f 41 53 20 20 20 20 20 20 20 20 20 20 20 20  K_AS            
4faf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fb00 20 32 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43   21.#define TK_C
4fb10 4f 4d 4d 41 20 20 20 20 20 20 20 20 20 20 20 20  OMMA            
4fb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 32                22
4fb30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 44 20 20  .#define TK_ID  
4fb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fb50 20 20 20 20 20 20 20 20 20 20 20 32 33 0a 23 64             23.#d
4fb60 65 66 69 6e 65 20 54 4b 5f 41 42 4f 52 54 20 20  efine TK_ABORT  
4fb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fb80 20 20 20 20 20 20 20 20 32 34 0a 23 64 65 66 69          24.#defi
4fb90 6e 65 20 54 4b 5f 41 46 54 45 52 20 20 20 20 20  ne TK_AFTER     
4fba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fbb0 20 20 20 20 20 32 35 0a 23 64 65 66 69 6e 65 20       25.#define 
4fbc0 54 4b 5f 41 4e 41 4c 59 5a 45 20 20 20 20 20 20  TK_ANALYZE      
4fbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fbe0 20 20 32 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f    26.#define TK_
4fbf0 41 53 43 20 20 20 20 20 20 20 20 20 20 20 20 20  ASC             
4fc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
4fc10 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 54 54  7.#define TK_ATT
4fc20 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20  ACH             
4fc30 20 20 20 20 20 20 20 20 20 20 20 20 32 38 0a 23              28.#
4fc40 64 65 66 69 6e 65 20 54 4b 5f 42 45 46 4f 52 45  define TK_BEFORE
4fc50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fc60 20 20 20 20 20 20 20 20 20 32 39 0a 23 64 65 66           29.#def
4fc70 69 6e 65 20 54 4b 5f 43 41 53 43 41 44 45 20 20  ine TK_CASCADE  
4fc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fc90 20 20 20 20 20 20 33 30 0a 23 64 65 66 69 6e 65        30.#define
4fca0 20 54 4b 5f 43 41 53 54 20 20 20 20 20 20 20 20   TK_CAST        
4fcb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fcc0 20 20 20 33 31 0a 23 64 65 66 69 6e 65 20 54 4b     31.#define TK
4fcd0 5f 43 4f 4e 46 4c 49 43 54 20 20 20 20 20 20 20  _CONFLICT       
4fce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fcf0 33 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 41  32.#define TK_DA
4fd00 54 41 42 41 53 45 20 20 20 20 20 20 20 20 20 20  TABASE          
4fd10 20 20 20 20 20 20 20 20 20 20 20 20 20 33 33 0a               33.
4fd20 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 53 43 20  #define TK_DESC 
4fd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fd40 20 20 20 20 20 20 20 20 20 20 33 34 0a 23 64 65            34.#de
4fd50 66 69 6e 65 20 54 4b 5f 44 45 54 41 43 48 20 20  fine TK_DETACH  
4fd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fd70 20 20 20 20 20 20 20 33 35 0a 23 64 65 66 69 6e         35.#defin
4fd80 65 20 54 4b 5f 45 41 43 48 20 20 20 20 20 20 20  e TK_EACH       
4fd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fda0 20 20 20 20 33 36 0a 23 64 65 66 69 6e 65 20 54      36.#define T
4fdb0 4b 5f 46 41 49 4c 20 20 20 20 20 20 20 20 20 20  K_FAIL          
4fdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fdd0 20 33 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46   37.#define TK_F
4fde0 4f 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OR              
4fdf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 38                38
4fe00 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 47 4e 4f  .#define TK_IGNO
4fe10 52 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  RE              
4fe20 20 20 20 20 20 20 20 20 20 20 20 33 39 0a 23 64             39.#d
4fe30 65 66 69 6e 65 20 54 4b 5f 49 4e 49 54 49 41 4c  efine TK_INITIAL
4fe40 4c 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LY              
4fe50 20 20 20 20 20 20 20 20 34 30 0a 23 64 65 66 69          40.#defi
4fe60 6e 65 20 54 4b 5f 49 4e 53 54 45 41 44 20 20 20  ne TK_INSTEAD   
4fe70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fe80 20 20 20 20 20 34 31 0a 23 64 65 66 69 6e 65 20       41.#define 
4fe90 54 4b 5f 4c 49 4b 45 5f 4b 57 20 20 20 20 20 20  TK_LIKE_KW      
4fea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4feb0 20 20 34 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f    42.#define TK_
4fec0 4d 41 54 43 48 20 20 20 20 20 20 20 20 20 20 20  MATCH           
4fed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
4fee0 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4b 45 59  3.#define TK_KEY
4fef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ff00 20 20 20 20 20 20 20 20 20 20 20 20 34 34 0a 23              44.#
4ff10 64 65 66 69 6e 65 20 54 4b 5f 4f 46 20 20 20 20  define TK_OF    
4ff20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ff30 20 20 20 20 20 20 20 20 20 34 35 0a 23 64 65 66           45.#def
4ff40 69 6e 65 20 54 4b 5f 4f 46 46 53 45 54 20 20 20  ine TK_OFFSET   
4ff50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ff60 20 20 20 20 20 20 34 36 0a 23 64 65 66 69 6e 65        46.#define
4ff70 20 54 4b 5f 50 52 41 47 4d 41 20 20 20 20 20 20   TK_PRAGMA      
4ff80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ff90 20 20 20 34 37 0a 23 64 65 66 69 6e 65 20 54 4b     47.#define TK
4ffa0 5f 52 41 49 53 45 20 20 20 20 20 20 20 20 20 20  _RAISE          
4ffb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ffc0 34 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45  48.#define TK_RE
4ffd0 50 4c 41 43 45 20 20 20 20 20 20 20 20 20 20 20  PLACE           
4ffe0 20 20 20 20 20 20 20 20 20 20 20 20 20 34 39 0a               49.
4fff0 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 53 54 52  #define TK_RESTR
50000 49 43 54 20 20 20 20 20 20 20 20 20 20 20 20 20  ICT             
50010 20 20 20 20 20 20 20 20 20 20 35 30 0a 23 64 65            50.#de
50020 66 69 6e 65 20 54 4b 5f 52 4f 57 20 20 20 20 20  fine TK_ROW     
50030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50040 20 20 20 20 20 20 20 35 31 0a 23 64 65 66 69 6e         51.#defin
50050 65 20 54 4b 5f 54 52 49 47 47 45 52 20 20 20 20  e TK_TRIGGER    
50060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50070 20 20 20 20 35 32 0a 23 64 65 66 69 6e 65 20 54      52.#define T
50080 4b 5f 56 41 43 55 55 4d 20 20 20 20 20 20 20 20  K_VACUUM        
50090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
500a0 20 35 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56   53.#define TK_V
500b0 49 45 57 20 20 20 20 20 20 20 20 20 20 20 20 20  IEW             
500c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 34                54
500d0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 49 52 54  .#define TK_VIRT
500e0 55 41 4c 20 20 20 20 20 20 20 20 20 20 20 20 20  UAL             
500f0 20 20 20 20 20 20 20 20 20 20 20 35 35 0a 23 64             55.#d
50100 65 66 69 6e 65 20 54 4b 5f 52 45 49 4e 44 45 58  efine TK_REINDEX
50110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50120 20 20 20 20 20 20 20 20 35 36 0a 23 64 65 66 69          56.#defi
50130 6e 65 20 54 4b 5f 52 45 4e 41 4d 45 20 20 20 20  ne TK_RENAME    
50140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50150 20 20 20 20 20 35 37 0a 23 64 65 66 69 6e 65 20       57.#define 
50160 54 4b 5f 43 54 49 4d 45 5f 4b 57 20 20 20 20 20  TK_CTIME_KW     
50170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50180 20 20 35 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f    58.#define TK_
50190 41 4e 59 20 20 20 20 20 20 20 20 20 20 20 20 20  ANY             
501a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
501b0 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 52 20  9.#define TK_OR 
501c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
501d0 20 20 20 20 20 20 20 20 20 20 20 20 36 30 0a 23              60.#
501e0 64 65 66 69 6e 65 20 54 4b 5f 41 4e 44 20 20 20  define TK_AND   
501f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50200 20 20 20 20 20 20 20 20 20 36 31 0a 23 64 65 66           61.#def
50210 69 6e 65 20 54 4b 5f 49 53 20 20 20 20 20 20 20  ine TK_IS       
50220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50230 20 20 20 20 20 20 36 32 0a 23 64 65 66 69 6e 65        62.#define
50240 20 54 4b 5f 42 45 54 57 45 45 4e 20 20 20 20 20   TK_BETWEEN     
50250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50260 20 20 20 36 33 0a 23 64 65 66 69 6e 65 20 54 4b     63.#define TK
50270 5f 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 20  _IN             
50280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50290 36 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 53  64.#define TK_IS
502a0 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20  NULL            
502b0 20 20 20 20 20 20 20 20 20 20 20 20 20 36 35 0a               65.
502c0 23 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 54 4e 55  #define TK_NOTNU
502d0 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LL              
502e0 20 20 20 20 20 20 20 20 20 20 36 36 0a 23 64 65            66.#de
502f0 66 69 6e 65 20 54 4b 5f 4e 45 20 20 20 20 20 20  fine TK_NE      
50300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50310 20 20 20 20 20 20 20 36 37 0a 23 64 65 66 69 6e         67.#defin
50320 65 20 54 4b 5f 45 51 20 20 20 20 20 20 20 20 20  e TK_EQ         
50330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50340 20 20 20 20 36 38 0a 23 64 65 66 69 6e 65 20 54      68.#define T
50350 4b 5f 47 54 20 20 20 20 20 20 20 20 20 20 20 20  K_GT            
50360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50370 20 36 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c   69.#define TK_L
50380 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
50390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 30                70
503a0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 54 20 20  .#define TK_LT  
503b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
503c0 20 20 20 20 20 20 20 20 20 20 20 37 31 0a 23 64             71.#d
503d0 65 66 69 6e 65 20 54 4b 5f 47 45 20 20 20 20 20  efine TK_GE     
503e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
503f0 20 20 20 20 20 20 20 20 37 32 0a 23 64 65 66 69          72.#defi
50400 6e 65 20 54 4b 5f 45 53 43 41 50 45 20 20 20 20  ne TK_ESCAPE    
50410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50420 20 20 20 20 20 37 33 0a 23 64 65 66 69 6e 65 20       73.#define 
50430 54 4b 5f 42 49 54 41 4e 44 20 20 20 20 20 20 20  TK_BITAND       
50440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50450 20 20 37 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f    74.#define TK_
50460 42 49 54 4f 52 20 20 20 20 20 20 20 20 20 20 20  BITOR           
50470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
50480 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 53 48  5.#define TK_LSH
50490 49 46 54 20 20 20 20 20 20 20 20 20 20 20 20 20  IFT             
504a0 20 20 20 20 20 20 20 20 20 20 20 20 37 36 0a 23              76.#
504b0 64 65 66 69 6e 65 20 54 4b 5f 52 53 48 49 46 54  define TK_RSHIFT
504c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
504d0 20 20 20 20 20 20 20 20 20 37 37 0a 23 64 65 66           77.#def
504e0 69 6e 65 20 54 4b 5f 50 4c 55 53 20 20 20 20 20  ine TK_PLUS     
504f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50500 20 20 20 20 20 20 37 38 0a 23 64 65 66 69 6e 65        78.#define
50510 20 54 4b 5f 4d 49 4e 55 53 20 20 20 20 20 20 20   TK_MINUS       
50520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50530 20 20 20 37 39 0a 23 64 65 66 69 6e 65 20 54 4b     79.#define TK
50540 5f 53 54 41 52 20 20 20 20 20 20 20 20 20 20 20  _STAR           
50550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50560 38 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 4c  80.#define TK_SL
50570 41 53 48 20 20 20 20 20 20 20 20 20 20 20 20 20  ASH             
50580 20 20 20 20 20 20 20 20 20 20 20 20 20 38 31 0a               81.
50590 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 4d 20 20  #define TK_REM  
505a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
505b0 20 20 20 20 20 20 20 20 20 20 38 32 0a 23 64 65            82.#de
505c0 66 69 6e 65 20 54 4b 5f 43 4f 4e 43 41 54 20 20  fine TK_CONCAT  
505d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
505e0 20 20 20 20 20 20 20 38 33 0a 23 64 65 66 69 6e         83.#defin
505f0 65 20 54 4b 5f 43 4f 4c 4c 41 54 45 20 20 20 20  e TK_COLLATE    
50600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50610 20 20 20 20 38 34 0a 23 64 65 66 69 6e 65 20 54      84.#define T
50620 4b 5f 55 4d 49 4e 55 53 20 20 20 20 20 20 20 20  K_UMINUS        
50630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50640 20 38 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55   85.#define TK_U
50650 50 4c 55 53 20 20 20 20 20 20 20 20 20 20 20 20  PLUS            
50660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 36                86
50670 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 4e  .#define TK_BITN
50680 4f 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OT              
50690 20 20 20 20 20 20 20 20 20 20 20 38 37 0a 23 64             87.#d
506a0 65 66 69 6e 65 20 54 4b 5f 53 54 52 49 4e 47 20  efine TK_STRING 
506b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
506c0 20 20 20 20 20 20 20 20 38 38 0a 23 64 65 66 69          88.#defi
506d0 6e 65 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 20 20 20  ne TK_JOIN_KW   
506e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
506f0 20 20 20 20 20 38 39 0a 23 64 65 66 69 6e 65 20       89.#define 
50700 54 4b 5f 43 4f 4e 53 54 52 41 49 4e 54 20 20 20  TK_CONSTRAINT   
50710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50720 20 20 39 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f    90.#define TK_
50730 44 45 46 41 55 4c 54 20 20 20 20 20 20 20 20 20  DEFAULT         
50740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
50750 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 55 4c  1.#define TK_NUL
50760 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  L               
50770 20 20 20 20 20 20 20 20 20 20 20 20 39 32 0a 23              92.#
50780 64 65 66 69 6e 65 20 54 4b 5f 50 52 49 4d 41 52  define TK_PRIMAR
50790 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Y               
507a0 20 20 20 20 20 20 20 20 20 39 33 0a 23 64 65 66           93.#def
507b0 69 6e 65 20 54 4b 5f 55 4e 49 51 55 45 20 20 20  ine TK_UNIQUE   
507c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
507d0 20 20 20 20 20 20 39 34 0a 23 64 65 66 69 6e 65        94.#define
507e0 20 54 4b 5f 43 48 45 43 4b 20 20 20 20 20 20 20   TK_CHECK       
507f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50800 20 20 20 39 35 0a 23 64 65 66 69 6e 65 20 54 4b     95.#define TK
50810 5f 52 45 46 45 52 45 4e 43 45 53 20 20 20 20 20  _REFERENCES     
50820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50830 39 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 55  96.#define TK_AU
50840 54 4f 49 4e 43 52 20 20 20 20 20 20 20 20 20 20  TOINCR          
50850 20 20 20 20 20 20 20 20 20 20 20 20 20 39 37 0a               97.
50860 23 64 65 66 69 6e 65 20 54 4b 5f 4f 4e 20 20 20  #define TK_ON   
50870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50880 20 20 20 20 20 20 20 20 20 20 39 38 0a 23 64 65            98.#de
50890 66 69 6e 65 20 54 4b 5f 44 45 4c 45 54 45 20 20  fine TK_DELETE  
508a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
508b0 20 20 20 20 20 20 20 39 39 0a 23 64 65 66 69 6e         99.#defin
508c0 65 20 54 4b 5f 55 50 44 41 54 45 20 20 20 20 20  e TK_UPDATE     
508d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
508e0 20 20 20 20 31 30 30 0a 23 64 65 66 69 6e 65 20      100.#define 
508f0 54 4b 5f 49 4e 53 45 52 54 20 20 20 20 20 20 20  TK_INSERT       
50900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50910 20 20 31 30 31 0a 23 64 65 66 69 6e 65 20 54 4b    101.#define TK
50920 5f 53 45 54 20 20 20 20 20 20 20 20 20 20 20 20  _SET            
50930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50940 31 30 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44  102.#define TK_D
50950 45 46 45 52 52 41 42 4c 45 20 20 20 20 20 20 20  EFERRABLE       
50960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30                10
50970 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 4f 52  3.#define TK_FOR
50980 45 49 47 4e 20 20 20 20 20 20 20 20 20 20 20 20  EIGN            
50990 20 20 20 20 20 20 20 20 20 20 20 20 31 30 34 0a              104.
509a0 23 64 65 66 69 6e 65 20 54 4b 5f 44 52 4f 50 20  #define TK_DROP 
509b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
509c0 20 20 20 20 20 20 20 20 20 20 31 30 35 0a 23 64            105.#d
509d0 65 66 69 6e 65 20 54 4b 5f 55 4e 49 4f 4e 20 20  efine TK_UNION  
509e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
509f0 20 20 20 20 20 20 20 20 31 30 36 0a 23 64 65 66          106.#def
50a00 69 6e 65 20 54 4b 5f 41 4c 4c 20 20 20 20 20 20  ine TK_ALL      
50a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50a20 20 20 20 20 20 20 31 30 37 0a 23 64 65 66 69 6e        107.#defin
50a30 65 20 54 4b 5f 45 58 43 45 50 54 20 20 20 20 20  e TK_EXCEPT     
50a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50a50 20 20 20 20 31 30 38 0a 23 64 65 66 69 6e 65 20      108.#define 
50a60 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 20 20 20  TK_INTERSECT    
50a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50a80 20 20 31 30 39 0a 23 64 65 66 69 6e 65 20 54 4b    109.#define TK
50a90 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 20 20  _SELECT         
50aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ab0 31 31 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44  110.#define TK_D
50ac0 49 53 54 49 4e 43 54 20 20 20 20 20 20 20 20 20  ISTINCT         
50ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31                11
50ae0 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 4f 54  1.#define TK_DOT
50af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50b00 20 20 20 20 20 20 20 20 20 20 20 20 31 31 32 0a              112.
50b10 23 64 65 66 69 6e 65 20 54 4b 5f 46 52 4f 4d 20  #define TK_FROM 
50b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50b30 20 20 20 20 20 20 20 20 20 20 31 31 33 0a 23 64            113.#d
50b40 65 66 69 6e 65 20 54 4b 5f 4a 4f 49 4e 20 20 20  efine TK_JOIN   
50b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50b60 20 20 20 20 20 20 20 20 31 31 34 0a 23 64 65 66          114.#def
50b70 69 6e 65 20 54 4b 5f 49 4e 44 45 58 45 44 20 20  ine TK_INDEXED  
50b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50b90 20 20 20 20 20 20 31 31 35 0a 23 64 65 66 69 6e        115.#defin
50ba0 65 20 54 4b 5f 42 59 20 20 20 20 20 20 20 20 20  e TK_BY         
50bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50bc0 20 20 20 20 31 31 36 0a 23 64 65 66 69 6e 65 20      116.#define 
50bd0 54 4b 5f 55 53 49 4e 47 20 20 20 20 20 20 20 20  TK_USING        
50be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50bf0 20 20 31 31 37 0a 23 64 65 66 69 6e 65 20 54 4b    117.#define TK
50c00 5f 4f 52 44 45 52 20 20 20 20 20 20 20 20 20 20  _ORDER          
50c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c20 31 31 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47  118.#define TK_G
50c30 52 4f 55 50 20 20 20 20 20 20 20 20 20 20 20 20  ROUP            
50c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31                11
50c50 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 48 41 56  9.#define TK_HAV
50c60 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 20  ING             
50c70 20 20 20 20 20 20 20 20 20 20 20 20 31 32 30 0a              120.
50c80 23 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4d 49 54  #define TK_LIMIT
50c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ca0 20 20 20 20 20 20 20 20 20 20 31 32 31 0a 23 64            121.#d
50cb0 65 66 69 6e 65 20 54 4b 5f 57 48 45 52 45 20 20  efine TK_WHERE  
50cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50cd0 20 20 20 20 20 20 20 20 31 32 32 0a 23 64 65 66          122.#def
50ce0 69 6e 65 20 54 4b 5f 49 4e 54 4f 20 20 20 20 20  ine TK_INTO     
50cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d00 20 20 20 20 20 20 31 32 33 0a 23 64 65 66 69 6e        123.#defin
50d10 65 20 54 4b 5f 56 41 4c 55 45 53 20 20 20 20 20  e TK_VALUES     
50d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d30 20 20 20 20 31 32 34 0a 23 64 65 66 69 6e 65 20      124.#define 
50d40 54 4b 5f 49 4e 54 45 47 45 52 20 20 20 20 20 20  TK_INTEGER      
50d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d60 20 20 31 32 35 0a 23 64 65 66 69 6e 65 20 54 4b    125.#define TK
50d70 5f 46 4c 4f 41 54 20 20 20 20 20 20 20 20 20 20  _FLOAT          
50d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d90 31 32 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42  126.#define TK_B
50da0 4c 4f 42 20 20 20 20 20 20 20 20 20 20 20 20 20  LOB             
50db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
50dc0 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 47  7.#define TK_REG
50dd0 49 53 54 45 52 20 20 20 20 20 20 20 20 20 20 20  ISTER           
50de0 20 20 20 20 20 20 20 20 20 20 20 20 31 32 38 0a              128.
50df0 23 64 65 66 69 6e 65 20 54 4b 5f 56 41 52 49 41  #define TK_VARIA
50e00 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20  BLE             
50e10 20 20 20 20 20 20 20 20 20 20 31 32 39 0a 23 64            129.#d
50e20 65 66 69 6e 65 20 54 4b 5f 43 41 53 45 20 20 20  efine TK_CASE   
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 20 31 33 30 0a 23 64 65 66          130.#def
50e50 69 6e 65 20 54 4b 5f 57 48 45 4e 20 20 20 20 20  ine TK_WHEN     
50e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e70 20 20 20 20 20 20 31 33 31 0a 23 64 65 66 69 6e        131.#defin
50e80 65 20 54 4b 5f 54 48 45 4e 20 20 20 20 20 20 20  e TK_THEN       
50e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ea0 20 20 20 20 31 33 32 0a 23 64 65 66 69 6e 65 20      132.#define 
50eb0 54 4b 5f 45 4c 53 45 20 20 20 20 20 20 20 20 20  TK_ELSE         
50ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ed0 20 20 31 33 33 0a 23 64 65 66 69 6e 65 20 54 4b    133.#define TK
50ee0 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20  _INDEX          
50ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f00 31 33 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41  134.#define TK_A
50f10 4c 54 45 52 20 20 20 20 20 20 20 20 20 20 20 20  LTER            
50f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33                13
50f30 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 20  5.#define TK_TO 
50f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f50 20 20 20 20 20 20 20 20 20 20 20 20 31 33 36 0a              136.
50f60 23 64 65 66 69 6e 65 20 54 4b 5f 41 44 44 20 20  #define TK_ADD  
50f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f80 20 20 20 20 20 20 20 20 20 20 31 33 37 0a 23 64            137.#d
50f90 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 55 4d 4e 4b  efine TK_COLUMNK
50fa0 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  W               
50fb0 20 20 20 20 20 20 20 20 31 33 38 0a 23 64 65 66          138.#def
50fc0 69 6e 65 20 54 4b 5f 54 4f 5f 54 45 58 54 20 20  ine TK_TO_TEXT  
50fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50fe0 20 20 20 20 20 20 31 33 39 0a 23 64 65 66 69 6e        139.#defin
50ff0 65 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20 20 20  e TK_TO_BLOB    
51000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51010 20 20 20 20 31 34 30 0a 23 64 65 66 69 6e 65 20      140.#define 
51020 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 20 20 20  TK_TO_NUMERIC   
51030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51040 20 20 31 34 31 0a 23 64 65 66 69 6e 65 20 54 4b    141.#define TK
51050 5f 54 4f 5f 49 4e 54 20 20 20 20 20 20 20 20 20  _TO_INT         
51060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51070 31 34 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54  142.#define TK_T
51080 4f 5f 52 45 41 4c 20 20 20 20 20 20 20 20 20 20  O_REAL          
51090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34                14
510a0 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4e 44  3.#define TK_END
510b0 5f 4f 46 5f 46 49 4c 45 20 20 20 20 20 20 20 20  _OF_FILE        
510c0 20 20 20 20 20 20 20 20 20 20 20 20 31 34 34 0a              144.
510d0 23 64 65 66 69 6e 65 20 54 4b 5f 49 4c 4c 45 47  #define TK_ILLEG
510e0 41 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  AL              
510f0 20 20 20 20 20 20 20 20 20 20 31 34 35 0a 23 64            145.#d
51100 65 66 69 6e 65 20 54 4b 5f 53 50 41 43 45 20 20  efine TK_SPACE  
51110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51120 20 20 20 20 20 20 20 20 31 34 36 0a 23 64 65 66          146.#def
51130 69 6e 65 20 54 4b 5f 55 4e 43 4c 4f 53 45 44 5f  ine TK_UNCLOSED_
51140 53 54 52 49 4e 47 20 20 20 20 20 20 20 20 20 20  STRING          
51150 20 20 20 20 20 20 31 34 37 0a 23 64 65 66 69 6e        147.#defin
51160 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 20 20  e TK_FUNCTION   
51170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51180 20 20 20 20 31 34 38 0a 23 64 65 66 69 6e 65 20      148.#define 
51190 54 4b 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20 20  TK_COLUMN       
511a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
511b0 20 20 31 34 39 0a 23 64 65 66 69 6e 65 20 54 4b    149.#define TK
511c0 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 20 20  _AGG_FUNCTION   
511d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
511e0 31 35 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41  150.#define TK_A
511f0 47 47 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20 20  GG_COLUMN       
51200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35                15
51210 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e  1.#define TK_CON
51220 53 54 5f 46 55 4e 43 20 20 20 20 20 20 20 20 20  ST_FUNC         
51230 20 20 20 20 20 20 20 20 20 20 20 20 31 35 32 0a              152.
51240 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
51250 20 45 6e 64 20 6f 66 20 70 61 72 73 65 2e 68 20   End of parse.h 
51260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
51290 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
512a0 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
512b0 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
512c0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
512d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
512e0 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f  .#include <stdio
512f0 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74  .h>.#include <st
51300 64 6c 69 62 2e 68 3e 0a 23 69 6e 63 6c 75 64 65  dlib.h>.#include
51310 20 3c 73 74 72 69 6e 67 2e 68 3e 0a 23 69 6e 63   <string.h>.#inc
51320 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e 0a  lude <assert.h>.
51330 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 64 65 66  #include <stddef
51340 2e 68 3e 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 6f  .h>../*.** If co
51350 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61 20 70 72  mpiling for a pr
51360 6f 63 65 73 73 6f 72 20 74 68 61 74 20 6c 61 63  ocessor that lac
51370 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  ks floating poin
51380 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a 20 73 75  t support,.** su
51390 62 73 74 69 74 75 74 65 20 69 6e 74 65 67 65 72  bstitute integer
513a0 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67 2d 70 6f   for floating-po
513b0 69 6e 74 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  int.*/.#ifdef SQ
513c0 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
513d0 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 65 66 69 6e  NG_POINT.# defin
513e0 65 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 5f  e double sqlite_
513f0 69 6e 74 36 34 0a 23 20 64 65 66 69 6e 65 20 4c  int64.# define L
51400 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 73  ONGDOUBLE_TYPE s
51410 71 6c 69 74 65 5f 69 6e 74 36 34 0a 23 20 69 66  qlite_int64.# if
51420 6e 64 65 66 20 53 51 4c 49 54 45 5f 42 49 47 5f  ndef SQLITE_BIG_
51430 44 42 4c 0a 23 20 20 20 64 65 66 69 6e 65 20 53  DBL.#   define S
51440 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 20 28 30  QLITE_BIG_DBL (0
51450 78 37 66 66 66 66 66 66 66 66 66 66 66 66 66 66  x7ffffffffffffff
51460 66 29 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66  f).# endif.# def
51470 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ine SQLITE_OMIT_
51480 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 20 31  DATETIME_FUNCS 1
51490 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
514a0 5f 4f 4d 49 54 5f 54 52 41 43 45 20 31 0a 23 20  _OMIT_TRACE 1.# 
514b0 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 49 58  undef SQLITE_MIX
514c0 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f  ED_ENDIAN_64BIT_
514d0 46 4c 4f 41 54 0a 23 65 6e 64 69 66 0a 23 69 66  FLOAT.#endif.#if
514e0 6e 64 65 66 20 53 51 4c 49 54 45 5f 42 49 47 5f  ndef SQLITE_BIG_
514f0 44 42 4c 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  DBL.# define SQL
51500 49 54 45 5f 42 49 47 5f 44 42 4c 20 28 31 65 39  ITE_BIG_DBL (1e9
51510 39 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  9).#endif../*.**
51520 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 69 73 20   OMIT_TEMPDB is 
51530 73 65 74 20 74 6f 20 31 20 69 66 20 53 51 4c 49  set to 1 if SQLI
51540 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 20 69  TE_OMIT_TEMPDB i
51550 73 20 64 65 66 69 6e 65 64 2c 20 6f 72 20 30 0a  s defined, or 0.
51560 2a 2a 20 61 66 74 65 72 77 61 72 64 2e 20 48 61  ** afterward. Ha
51570 76 69 6e 67 20 74 68 69 73 20 6d 61 63 72 6f 20  ving this macro 
51580 61 6c 6c 6f 77 73 20 75 73 20 74 6f 20 63 61 75  allows us to cau
51590 73 65 20 74 68 65 20 43 20 63 6f 6d 70 69 6c 65  se the C compile
515a0 72 20 0a 2a 2a 20 74 6f 20 6f 6d 69 74 20 63 6f  r .** to omit co
515b0 64 65 20 75 73 65 64 20 62 79 20 54 45 4d 50 20  de used by TEMP 
515c0 74 61 62 6c 65 73 20 77 69 74 68 6f 75 74 20 6d  tables without m
515d0 65 73 73 79 20 23 69 66 6e 64 65 66 20 73 74 61  essy #ifndef sta
515e0 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 23 69 66 64  tements..*/.#ifd
515f0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
51600 45 4d 50 44 42 0a 23 64 65 66 69 6e 65 20 4f 4d  EMPDB.#define OM
51610 49 54 5f 54 45 4d 50 44 42 20 31 0a 23 65 6c 73  IT_TEMPDB 1.#els
51620 65 0a 23 64 65 66 69 6e 65 20 4f 4d 49 54 5f 54  e.#define OMIT_T
51630 45 4d 50 44 42 20 30 0a 23 65 6e 64 69 66 0a 0a  EMPDB 0.#endif..
51640 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c  /*.** If the fol
51650 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 69 73 20  lowing macro is 
51660 73 65 74 20 74 6f 20 31 2c 20 74 68 65 6e 20 4e  set to 1, then N
51670 55 4c 4c 20 76 61 6c 75 65 73 20 61 72 65 20 63  ULL values are c
51680 6f 6e 73 69 64 65 72 65 64 0a 2a 2a 20 64 69 73  onsidered.** dis
51690 74 69 6e 63 74 20 77 68 65 6e 20 64 65 74 65 72  tinct when deter
516a0 6d 69 6e 69 6e 67 20 77 68 65 74 68 65 72 20 6f  mining whether o
516b0 72 20 6e 6f 74 20 74 77 6f 20 65 6e 74 72 69 65  r not two entrie
516c0 73 20 61 72 65 20 74 68 65 20 73 61 6d 65 0a 2a  s are the same.*
516d0 2a 20 69 6e 20 61 20 55 4e 49 51 55 45 20 69 6e  * in a UNIQUE in
516e0 64 65 78 2e 20 20 54 68 69 73 20 69 73 20 74 68  dex.  This is th
516f0 65 20 77 61 79 20 50 6f 73 74 67 72 65 53 51 4c  e way PostgreSQL
51700 2c 20 4f 72 61 63 6c 65 2c 20 44 42 32 2c 20 4d  , Oracle, DB2, M
51710 79 53 51 4c 2c 0a 2a 2a 20 4f 43 45 4c 4f 54 2c  ySQL,.** OCELOT,
51720 20 61 6e 64 20 46 69 72 65 62 69 72 64 20 61 6c   and Firebird al
51730 6c 20 77 6f 72 6b 2e 20 20 54 68 65 20 53 51 4c  l work.  The SQL
51740 39 32 20 73 70 65 63 20 65 78 70 6c 69 63 69 74  92 spec explicit
51750 6c 79 20 73 61 79 73 20 74 68 69 73 0a 2a 2a 20  ly says this.** 
51760 69 73 20 74 68 65 20 77 61 79 20 74 68 69 6e 67  is the way thing
51770 73 20 61 72 65 20 73 75 70 70 6f 73 65 20 74 6f  s are suppose to
51780 20 77 6f 72 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   work..**.** If 
51790 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61  the following ma
517a0 63 72 6f 20 69 73 20 73 65 74 20 74 6f 20 30 2c  cro is set to 0,
517b0 20 74 68 65 20 4e 55 4c 4c 73 20 61 72 65 20 69   the NULLs are i
517c0 6e 64 69 73 74 69 6e 63 74 20 66 6f 72 0a 2a 2a  ndistinct for.**
517d0 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78 2e   a UNIQUE index.
517e0 20 20 49 6e 20 74 68 69 73 20 6d 6f 64 65 2c 20    In this mode, 
517f0 79 6f 75 20 63 61 6e 20 6f 6e 6c 79 20 68 61 76  you can only hav
51800 65 20 61 20 73 69 6e 67 6c 65 20 4e 55 4c 4c 20  e a single NULL 
51810 65 6e 74 72 79 0a 2a 2a 20 66 6f 72 20 61 20 63  entry.** for a c
51820 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 65 64 20 55  olumn declared U
51830 4e 49 51 55 45 2e 20 20 54 68 69 73 20 69 73 20  NIQUE.  This is 
51840 74 68 65 20 77 61 79 20 49 6e 66 6f 72 6d 69 78  the way Informix
51850 20 61 6e 64 20 53 51 4c 20 53 65 72 76 65 72 0a   and SQL Server.
51860 2a 2a 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 64 65 66  ** work..*/.#def
51870 69 6e 65 20 4e 55 4c 4c 5f 44 49 53 54 49 4e 43  ine NULL_DISTINC
51880 54 5f 46 4f 52 5f 55 4e 49 51 55 45 20 31 0a 0a  T_FOR_UNIQUE 1..
51890 2f 2a 0a 2a 2a 20 54 68 65 20 22 66 69 6c 65 20  /*.** The "file 
518a0 66 6f 72 6d 61 74 22 20 6e 75 6d 62 65 72 20 69  format" number i
518b0 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61  s an integer tha
518c0 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  t is incremented
518d0 20 77 68 65 6e 65 76 65 72 0a 2a 2a 20 74 68 65   whenever.** the
518e0 20 56 44 42 45 2d 6c 65 76 65 6c 20 66 69 6c 65   VDBE-level file
518f0 20 66 6f 72 6d 61 74 20 63 68 61 6e 67 65 73 2e   format changes.
51900 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
51910 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 74 68  macros define th
51920 65 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74  e.** the default
51930 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 66 6f 72   file format for
51940 20 6e 65 77 20 64 61 74 61 62 61 73 65 73 20 61   new databases a
51950 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 66  nd the maximum f
51960 69 6c 65 20 66 6f 72 6d 61 74 0a 2a 2a 20 74 68  ile format.** th
51970 61 74 20 74 68 65 20 6c 69 62 72 61 72 79 20 63  at the library c
51980 61 6e 20 72 65 61 64 2e 0a 2a 2f 0a 23 64 65 66  an read..*/.#def
51990 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46  ine SQLITE_MAX_F
519a0 49 4c 45 5f 46 4f 52 4d 41 54 20 34 0a 23 69 66  ILE_FORMAT 4.#if
519b0 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41  ndef SQLITE_DEFA
519c0 55 4c 54 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 0a  ULT_FILE_FORMAT.
519d0 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
519e0 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46 4f 52  DEFAULT_FILE_FOR
519f0 4d 41 54 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a  MAT 1.#endif../*
51a00 0a 2a 2a 20 50 72 6f 76 69 64 65 20 61 20 64 65  .** Provide a de
51a10 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20  fault value for 
51a20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52  SQLITE_TEMP_STOR
51a30 45 20 69 6e 20 63 61 73 65 20 69 74 20 69 73 20  E in case it is 
51a40 6e 6f 74 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  not specified.**
51a50 20 6f 6e 20 74 68 65 20 63 6f 6d 6d 61 6e 64 2d   on the command-
51a60 6c 69 6e 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  line.*/.#ifndef 
51a70 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52  SQLITE_TEMP_STOR
51a80 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  E.# define SQLIT
51a90 45 5f 54 45 4d 50 5f 53 54 4f 52 45 20 31 0a 23  E_TEMP_STORE 1.#
51aa0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 43 43  endif../*.** GCC
51ab0 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e 65   does not define
51ac0 20 74 68 65 20 6f 66 66 73 65 74 6f 66 28 29 20   the offsetof() 
51ad0 6d 61 63 72 6f 20 73 6f 20 77 65 27 6c 6c 20 68  macro so we'll h
51ae0 61 76 65 20 74 6f 20 64 6f 20 69 74 0a 2a 2a 20  ave to do it.** 
51af0 6f 75 72 73 65 6c 76 65 73 2e 0a 2a 2f 0a 23 69  ourselves..*/.#i
51b00 66 6e 64 65 66 20 6f 66 66 73 65 74 6f 66 0a 23  fndef offsetof.#
51b10 64 65 66 69 6e 65 20 6f 66 66 73 65 74 6f 66 28  define offsetof(
51b20 53 54 52 55 43 54 55 52 45 2c 46 49 45 4c 44 29  STRUCTURE,FIELD)
51b30 20 28 28 69 6e 74 29 28 28 63 68 61 72 2a 29 26   ((int)((char*)&
51b40 28 28 53 54 52 55 43 54 55 52 45 2a 29 30 29 2d  ((STRUCTURE*)0)-
51b50 3e 46 49 45 4c 44 29 29 0a 23 65 6e 64 69 66 0a  >FIELD)).#endif.
51b60 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
51b70 73 65 65 20 69 66 20 74 68 69 73 20 6d 61 63 68  see if this mach
51b80 69 6e 65 20 75 73 65 73 20 45 42 43 44 49 43 2e  ine uses EBCDIC.
51b90 20 20 28 59 65 73 2c 20 62 65 6c 69 65 76 65 20    (Yes, believe 
51ba0 69 74 20 6f 72 0a 2a 2a 20 6e 6f 74 2c 20 74 68  it or.** not, th
51bb0 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6d 61  ere are still ma
51bc0 63 68 69 6e 65 73 20 6f 75 74 20 74 68 65 72 65  chines out there
51bd0 20 74 68 61 74 20 75 73 65 20 45 42 43 44 49 43   that use EBCDIC
51be0 2e 29 0a 2a 2f 0a 23 69 66 20 27 41 27 20 3d 3d  .).*/.#if 'A' ==
51bf0 20 27 5c 33 30 31 27 0a 23 20 64 65 66 69 6e 65   '\301'.# define
51c00 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 20 31   SQLITE_EBCDIC 1
51c10 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
51c20 53 51 4c 49 54 45 5f 41 53 43 49 49 20 31 0a 23  SQLITE_ASCII 1.#
51c30 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74  endif../*.** Int
51c40 65 67 65 72 73 20 6f 66 20 6b 6e 6f 77 6e 20 73  egers of known s
51c50 69 7a 65 73 2e 20 20 54 68 65 73 65 20 74 79 70  izes.  These typ
51c60 65 64 65 66 73 20 6d 69 67 68 74 20 63 68 61 6e  edefs might chan
51c70 67 65 20 66 6f 72 20 61 72 63 68 69 74 65 63 74  ge for architect
51c80 75 72 65 73 0a 2a 2a 20 77 68 65 72 65 20 74 68  ures.** where th
51c90 65 20 73 69 7a 65 73 20 76 65 72 79 2e 20 20 50  e sizes very.  P
51ca0 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72  reprocessor macr
51cb0 6f 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65  os are available
51cc0 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a 2a 20   so that the.** 
51cd0 74 79 70 65 73 20 63 61 6e 20 62 65 20 63 6f 6e  types can be con
51ce0 76 65 6e 69 65 6e 74 6c 79 20 72 65 64 65 66 69  veniently redefi
51cf0 6e 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74  ned at compile-t
51d00 79 70 65 2e 20 20 4c 69 6b 65 20 74 68 69 73 3a  ype.  Like this:
51d10 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 63  .**.**         c
51d20 63 20 27 2d 44 55 49 4e 54 50 54 52 5f 54 59 50  c '-DUINTPTR_TYP
51d30 45 3d 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 27  E=long long int'
51d40 20 2e 2e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20   ....*/.#ifndef 
51d50 55 49 4e 54 33 32 5f 54 59 50 45 0a 23 20 69 66  UINT32_TYPE.# if
51d60 64 65 66 20 48 41 56 45 5f 55 49 4e 54 33 32 5f  def HAVE_UINT32_
51d70 54 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54  T.#  define UINT
51d80 33 32 5f 54 59 50 45 20 75 69 6e 74 33 32 5f 74  32_TYPE uint32_t
51d90 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e  .# else.#  defin
51da0 65 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 6e  e UINT32_TYPE un
51db0 73 69 67 6e 65 64 20 69 6e 74 0a 23 20 65 6e 64  signed int.# end
51dc0 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65  if.#endif.#ifnde
51dd0 66 20 55 49 4e 54 31 36 5f 54 59 50 45 0a 23 20  f UINT16_TYPE.# 
51de0 69 66 64 65 66 20 48 41 56 45 5f 55 49 4e 54 31  ifdef HAVE_UINT1
51df0 36 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 55 49  6_T.#  define UI
51e00 4e 54 31 36 5f 54 59 50 45 20 75 69 6e 74 31 36  NT16_TYPE uint16
51e10 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66  _t.# else.#  def
51e20 69 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 45 20  ine UINT16_TYPE 
51e30 75 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20 69  unsigned short i
51e40 6e 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69  nt.# endif.#endi
51e50 66 0a 23 69 66 6e 64 65 66 20 49 4e 54 31 36 5f  f.#ifndef INT16_
51e60 54 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56  TYPE.# ifdef HAV
51e70 45 5f 49 4e 54 31 36 5f 54 0a 23 20 20 64 65 66  E_INT16_T.#  def
51e80 69 6e 65 20 49 4e 54 31 36 5f 54 59 50 45 20 69  ine INT16_TYPE i
51e90 6e 74 31 36 5f 74 0a 23 20 65 6c 73 65 0a 23 20  nt16_t.# else.# 
51ea0 20 64 65 66 69 6e 65 20 49 4e 54 31 36 5f 54 59   define INT16_TY
51eb0 50 45 20 73 68 6f 72 74 20 69 6e 74 0a 23 20 65  PE short int.# e
51ec0 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e  ndif.#endif.#ifn
51ed0 64 65 66 20 55 49 4e 54 38 5f 54 59 50 45 0a 23  def UINT8_TYPE.#
51ee0 20 69 66 64 65 66 20 48 41 56 45 5f 55 49 4e 54   ifdef HAVE_UINT
51ef0 38 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 55 49  8_T.#  define UI
51f00 4e 54 38 5f 54 59 50 45 20 75 69 6e 74 38 5f 74  NT8_TYPE uint8_t
51f10 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e  .# else.#  defin
51f20 65 20 55 49 4e 54 38 5f 54 59 50 45 20 75 6e 73  e UINT8_TYPE uns
51f30 69 67 6e 65 64 20 63 68 61 72 0a 23 20 65 6e 64  igned char.# end
51f40 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65  if.#endif.#ifnde
51f50 66 20 49 4e 54 38 5f 54 59 50 45 0a 23 20 69 66  f INT8_TYPE.# if
51f60 64 65 66 20 48 41 56 45 5f 49 4e 54 38 5f 54 0a  def HAVE_INT8_T.
51f70 23 20 20 64 65 66 69 6e 65 20 49 4e 54 38 5f 54  #  define INT8_T
51f80 59 50 45 20 69 6e 74 38 5f 74 0a 23 20 65 6c 73  YPE int8_t.# els
51f90 65 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54 38  e.#  define INT8
51fa0 5f 54 59 50 45 20 73 69 67 6e 65 64 20 63 68 61  _TYPE signed cha
51fb0 72 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66  r.# endif.#endif
51fc0 0a 23 69 66 6e 64 65 66 20 4c 4f 4e 47 44 4f 55  .#ifndef LONGDOU
51fd0 42 4c 45 5f 54 59 50 45 0a 23 20 64 65 66 69 6e  BLE_TYPE.# defin
51fe0 65 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50  e LONGDOUBLE_TYP
51ff0 45 20 6c 6f 6e 67 20 64 6f 75 62 6c 65 0a 23 65  E long double.#e
52000 6e 64 69 66 0a 74 79 70 65 64 65 66 20 73 71 6c  ndif.typedef sql
52010 69 74 65 5f 69 6e 74 36 34 20 69 36 34 3b 20 20  ite_int64 i64;  
52020 20 20 20 20 20 20 20 20 2f 2a 20 38 2d 62 79 74          /* 8-byt
52030 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  e signed integer
52040 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 71 6c 69   */.typedef sqli
52050 74 65 5f 75 69 6e 74 36 34 20 75 36 34 3b 20 20  te_uint64 u64;  
52060 20 20 20 20 20 20 20 2f 2a 20 38 2d 62 79 74 65         /* 8-byte
52070 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65   unsigned intege
52080 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49 4e  r */.typedef UIN
52090 54 33 32 5f 54 59 50 45 20 75 33 32 3b 20 20 20  T32_TYPE u32;   
520a0 20 20 20 20 20 20 20 20 2f 2a 20 34 2d 62 79 74          /* 4-byt
520b0 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67  e unsigned integ
520c0 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49  er */.typedef UI
520d0 4e 54 31 36 5f 54 59 50 45 20 75 31 36 3b 20 20  NT16_TYPE u16;  
520e0 20 20 20 20 20 20 20 20 20 2f 2a 20 32 2d 62 79           /* 2-by
520f0 74 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65  te unsigned inte
52100 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 49  ger */.typedef I
52110 4e 54 31 36 5f 54 59 50 45 20 69 31 36 3b 20 20  NT16_TYPE i16;  
52120 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 2d 62            /* 2-b
52130 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67  yte signed integ
52140 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49  er */.typedef UI
52150 4e 54 38 5f 54 59 50 45 20 75 38 3b 20 20 20 20  NT8_TYPE u8;    
52160 20 20 20 20 20 20 20 20 20 2f 2a 20 31 2d 62 79           /* 1-by
52170 74 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65  te unsigned inte
52180 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 49  ger */.typedef I
52190 4e 54 38 5f 54 59 50 45 20 69 38 3b 20 20 20 20  NT8_TYPE i8;    
521a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 2d 62            /* 1-b
521b0 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67  yte signed integ
521c0 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63  er */../*.** Mac
521d0 72 6f 73 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  ros to determine
521e0 20 77 68 65 74 68 65 72 20 74 68 65 20 6d 61 63   whether the mac
521f0 68 69 6e 65 20 69 73 20 62 69 67 20 6f 72 20 6c  hine is big or l
52200 69 74 74 6c 65 20 65 6e 64 69 61 6e 2c 0a 2a 2a  ittle endian,.**
52210 20 65 76 61 6c 75 61 74 65 64 20 61 74 20 72 75   evaluated at ru
52220 6e 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66  ntime..*/.#ifdef
52230 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41   SQLITE_AMALGAMA
52240 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56  TION.SQLITE_PRIV
52250 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73 71  ATE const int sq
52260 6c 69 74 65 33 6f 6e 65 3b 0a 23 65 6c 73 65 0a  lite3one;.#else.
52270 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
52280 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 65 33  onst int sqlite3
52290 6f 6e 65 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20  one;.#endif.#if 
522a0 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c  defined(i386) ||
522b0 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f   defined(__i386_
522c0 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d  _) || defined(_M
522d0 5f 49 58 38 36 29 5c 0a 20 20 20 20 20 20 20 20  _IX86)\.        
522e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
522f0 20 20 20 20 20 7c 7c 20 64 65 66 69 6e 65 64 28       || defined(
52300 5f 5f 78 38 36 5f 36 34 29 20 7c 7c 20 64 65 66  __x86_64) || def
52310 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29  ined(__x86_64__)
52320 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
52330 5f 42 49 47 45 4e 44 49 41 4e 20 20 20 20 30 0a  _BIGENDIAN    0.
52340 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
52350 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 31 0a 23  LITTLEENDIAN 1.#
52360 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55   define SQLITE_U
52370 54 46 31 36 4e 41 54 49 56 45 20 20 53 51 4c 49  TF16NATIVE  SQLI
52380 54 45 5f 55 54 46 31 36 4c 45 0a 23 65 6c 73 65  TE_UTF16LE.#else
52390 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
523a0 5f 42 49 47 45 4e 44 49 41 4e 20 20 20 20 28 2a  _BIGENDIAN    (*
523b0 28 63 68 61 72 20 2a 29 28 26 73 71 6c 69 74 65  (char *)(&sqlite
523c0 33 6f 6e 65 29 3d 3d 30 29 0a 23 20 64 65 66 69  3one)==0).# defi
523d0 6e 65 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45  ne SQLITE_LITTLE
523e0 45 4e 44 49 41 4e 20 28 2a 28 63 68 61 72 20 2a  ENDIAN (*(char *
523f0 29 28 26 73 71 6c 69 74 65 33 6f 6e 65 29 3d 3d  )(&sqlite3one)==
52400 31 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  1).# define SQLI
52410 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20 28  TE_UTF16NATIVE (
52420 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e  SQLITE_BIGENDIAN
52430 3f 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3a  ?SQLITE_UTF16BE:
52440 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 29 0a  SQLITE_UTF16LE).
52450 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f  #endif../*.** Co
52460 6e 73 74 61 6e 74 73 20 66 6f 72 20 74 68 65 20  nstants for the 
52470 6c 61 72 67 65 73 74 20 61 6e 64 20 73 6d 61 6c  largest and smal
52480 6c 65 73 74 20 70 6f 73 73 69 62 6c 65 20 36 34  lest possible 64
52490 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65  -bit signed inte
524a0 67 65 72 73 2e 0a 2a 2a 20 54 68 65 73 65 20 6d  gers..** These m
524b0 61 63 72 6f 73 20 61 72 65 20 64 65 73 69 67 6e  acros are design
524c0 65 64 20 74 6f 20 77 6f 72 6b 20 63 6f 72 72 65  ed to work corre
524d0 63 74 6c 79 20 6f 6e 20 62 6f 74 68 20 33 32 2d  ctly on both 32-
524e0 62 69 74 20 61 6e 64 20 36 34 2d 62 69 74 0a 2a  bit and 64-bit.*
524f0 2a 20 63 6f 6d 70 69 6c 65 72 73 2e 0a 2a 2f 0a  * compilers..*/.
52500 23 64 65 66 69 6e 65 20 4c 41 52 47 45 53 54 5f  #define LARGEST_
52510 49 4e 54 36 34 20 20 28 30 78 66 66 66 66 66 66  INT64  (0xffffff
52520 66 66 7c 28 28 28 69 36 34 29 30 78 37 66 66 66  ff|(((i64)0x7fff
52530 66 66 66 66 29 3c 3c 33 32 29 29 0a 23 64 65 66  ffff)<<32)).#def
52540 69 6e 65 20 53 4d 41 4c 4c 45 53 54 5f 49 4e 54  ine SMALLEST_INT
52550 36 34 20 28 28 28 69 36 34 29 2d 31 29 20 2d 20  64 (((i64)-1) - 
52560 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 29 0a 0a  LARGEST_INT64)..
52570 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
52580 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
52590 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20  ng structure is 
525a0 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68  used to store th
525b0 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 2a  e busy-handler.*
525c0 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61  * callback for a
525d0 20 67 69 76 65 6e 20 73 71 6c 69 74 65 20 68 61   given sqlite ha
525e0 6e 64 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65  ndle. .**.** The
525f0 20 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e 64   sqlite.busyHand
52600 6c 65 72 20 6d 65 6d 62 65 72 20 6f 66 20 74 68  ler member of th
52610 65 20 73 71 6c 69 74 65 20 73 74 72 75 63 74 20  e sqlite struct 
52620 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 62 75 73  contains the bus
52630 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f  y.** callback fo
52640 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  r the database h
52650 61 6e 64 6c 65 2e 20 45 61 63 68 20 70 61 67 65  andle. Each page
52660 72 20 6f 70 65 6e 65 64 20 76 69 61 20 74 68 65  r opened via the
52670 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 6c   sqlite.** handl
52680 65 20 69 73 20 70 61 73 73 65 64 20 61 20 70 6f  e is passed a po
52690 69 6e 74 65 72 20 74 6f 20 73 71 6c 69 74 65 2e  inter to sqlite.
526a0 62 75 73 79 48 61 6e 64 6c 65 72 2e 20 54 68 65  busyHandler. The
526b0 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 2a 2a   busy-handler.**
526c0 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 75 72   callback is cur
526d0 72 65 6e 74 6c 79 20 69 6e 76 6f 6b 65 64 20 6f  rently invoked o
526e0 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  nly from within 
526f0 70 61 67 65 72 2e 63 2e 0a 2a 2f 0a 74 79 70 65  pager.c..*/.type
52700 64 65 66 20 73 74 72 75 63 74 20 42 75 73 79 48  def struct BusyH
52710 61 6e 64 6c 65 72 20 42 75 73 79 48 61 6e 64 6c  andler BusyHandl
52720 65 72 3b 0a 73 74 72 75 63 74 20 42 75 73 79 48  er;.struct BusyH
52730 61 6e 64 6c 65 72 20 7b 0a 20 20 69 6e 74 20 28  andler {.  int (
52740 2a 78 46 75 6e 63 29 28 76 6f 69 64 20 2a 2c 69  *xFunc)(void *,i
52750 6e 74 29 3b 20 20 2f 2a 20 54 68 65 20 62 75 73  nt);  /* The bus
52760 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20  y callback */.  
52770 76 6f 69 64 20 2a 70 41 72 67 3b 20 20 20 20 20  void *pArg;     
52780 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
52790 72 73 74 20 61 72 67 20 74 6f 20 62 75 73 79 20  rst arg to busy 
527a0 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e  callback */.  in
527b0 74 20 6e 42 75 73 79 3b 20 20 20 20 20 20 20 20  t nBusy;        
527c0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 72           /* Incr
527d0 65 6d 65 6e 74 65 64 20 77 69 74 68 20 65 61 63  emented with eac
527e0 68 20 62 75 73 79 20 63 61 6c 6c 20 2a 2f 0a 7d  h busy call */.}
527f0 3b 0a 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f 66  ;../*.** Name of
52800 20 74 68 65 20 6d 61 73 74 65 72 20 64 61 74 61   the master data
52810 62 61 73 65 20 74 61 62 6c 65 2e 20 20 54 68 65  base table.  The
52820 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 65   master database
52830 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 61 20 73   table.** is a s
52840 70 65 63 69 61 6c 20 74 61 62 6c 65 20 74 68 61  pecial table tha
52850 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65  t holds the name
52860 73 20 61 6e 64 20 61 74 74 72 69 62 75 74 65 73  s and attributes
52870 20 6f 66 20 61 6c 6c 0a 2a 2a 20 75 73 65 72 20   of all.** user 
52880 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63  tables and indic
52890 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  es..*/.#define M
528a0 41 53 54 45 52 5f 4e 41 4d 45 20 20 20 20 20 20  ASTER_NAME      
528b0 20 22 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22   "sqlite_master"
528c0 0a 23 64 65 66 69 6e 65 20 54 45 4d 50 5f 4d 41  .#define TEMP_MA
528d0 53 54 45 52 5f 4e 41 4d 45 20 20 22 73 71 6c 69  STER_NAME  "sqli
528e0 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 22 0a  te_temp_master".
528f0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 2d  ./*.** The root-
52900 70 61 67 65 20 6f 66 20 74 68 65 20 6d 61 73 74  page of the mast
52910 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62 6c  er database tabl
52920 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41  e..*/.#define MA
52930 53 54 45 52 5f 52 4f 4f 54 20 20 20 20 20 20 20  STER_ROOT       
52940 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d  1../*.** The nam
52950 65 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20  e of the schema 
52960 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e  table..*/.#defin
52970 65 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 78  e SCHEMA_TABLE(x
52980 29 20 20 28 28 21 4f 4d 49 54 5f 54 45 4d 50 44  )  ((!OMIT_TEMPD
52990 42 29 26 26 28 78 3d 3d 31 29 3f 54 45 4d 50 5f  B)&&(x==1)?TEMP_
529a0 4d 41 53 54 45 52 5f 4e 41 4d 45 3a 4d 41 53 54  MASTER_NAME:MAST
529b0 45 52 5f 4e 41 4d 45 29 0a 0a 2f 2a 0a 2a 2a 20  ER_NAME)../*.** 
529c0 41 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 6d 61  A convenience ma
529d0 63 72 6f 20 74 68 61 74 20 72 65 74 75 72 6e 73  cro that returns
529e0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
529f0 6c 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 61 6e  lements in.** an
52a00 20 61 72 72 61 79 2e 0a 2a 2f 0a 23 64 65 66 69   array..*/.#defi
52a10 6e 65 20 41 72 72 61 79 53 69 7a 65 28 58 29 20  ne ArraySize(X) 
52a20 20 20 20 28 73 69 7a 65 6f 66 28 58 29 2f 73 69     (sizeof(X)/si
52a30 7a 65 6f 66 28 58 5b 30 5d 29 29 0a 0a 2f 2a 0a  zeof(X[0]))../*.
52a40 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
52a50 20 76 61 6c 75 65 20 61 73 20 61 20 64 65 73 74   value as a dest
52a60 72 75 63 74 6f 72 20 6d 65 61 6e 73 20 74 6f 20  ructor means to 
52a70 75 73 65 20 73 71 6c 69 74 65 33 44 62 46 72 65  use sqlite3DbFre
52a80 65 28 29 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20  e()..** This is 
52a90 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65 78 74 65  an internal exte
52aa0 6e 73 69 6f 6e 20 74 6f 20 53 51 4c 49 54 45 5f  nsion to SQLITE_
52ab0 53 54 41 54 49 43 20 61 6e 64 20 53 51 4c 49 54  STATIC and SQLIT
52ac0 45 5f 54 52 41 4e 53 49 45 4e 54 2e 0a 2a 2f 0a  E_TRANSIENT..*/.
52ad0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
52ae0 59 4e 41 4d 49 43 20 20 20 28 28 73 71 6c 69 74  YNAMIC   ((sqlit
52af0 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79  e3_destructor_ty
52b00 70 65 29 73 71 6c 69 74 65 33 44 62 46 72 65 65  pe)sqlite3DbFree
52b10 29 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 53 51  )../*.** When SQ
52b20 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 20 69 73  LITE_OMIT_WSD is
52b30 20 64 65 66 69 6e 65 64 2c 20 69 74 20 6d 65 61   defined, it mea
52b40 6e 73 20 74 68 61 74 20 74 68 65 20 74 61 72 67  ns that the targ
52b50 65 74 20 70 6c 61 74 66 6f 72 6d 20 64 6f 65 73  et platform does
52b60 0a 2a 2a 20 6e 6f 74 20 73 75 70 70 6f 72 74 20  .** not support 
52b70 57 72 69 74 61 62 6c 65 20 53 74 61 74 69 63 20  Writable Static 
52b80 44 61 74 61 20 28 57 53 44 29 20 73 75 63 68 20  Data (WSD) such 
52b90 61 73 20 67 6c 6f 62 61 6c 20 61 6e 64 20 73 74  as global and st
52ba0 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 2e 0a  atic variables..
52bb0 2a 2a 20 41 6c 6c 20 76 61 72 69 61 62 6c 65 73  ** All variables
52bc0 20 6d 75 73 74 20 65 69 74 68 65 72 20 62 65 20   must either be 
52bd0 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 6f 72 20  on the stack or 
52be0 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f  dynamically allo
52bf0 63 61 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68  cated from.** th
52c00 65 20 68 65 61 70 2e 20 20 57 68 65 6e 20 57 53  e heap.  When WS
52c10 44 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64  D is unsupported
52c20 2c 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 64  , the variable d
52c30 65 63 6c 61 72 61 74 69 6f 6e 73 20 73 63 61 74  eclarations scat
52c40 74 65 72 65 64 0a 2a 2a 20 74 68 72 6f 75 67 68  tered.** through
52c50 6f 75 74 20 74 68 65 20 53 51 4c 69 74 65 20 63  out the SQLite c
52c60 6f 64 65 20 6d 75 73 74 20 62 65 63 6f 6d 65 20  ode must become 
52c70 63 6f 6e 73 74 61 6e 74 73 20 69 6e 73 74 65 61  constants instea
52c80 64 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 57  d.  The SQLITE_W
52c90 53 44 0a 2a 2a 20 6d 61 63 72 6f 20 69 73 20 75  SD.** macro is u
52ca0 73 65 64 20 66 6f 72 20 74 68 69 73 20 70 75 72  sed for this pur
52cb0 70 6f 73 65 2e 20 20 41 6e 64 20 69 6e 73 74 65  pose.  And inste
52cc0 61 64 20 6f 66 20 72 65 66 65 72 65 6e 63 69 6e  ad of referencin
52cd0 67 20 74 68 65 20 76 61 72 69 61 62 6c 65 0a 2a  g the variable.*
52ce0 2a 20 64 69 72 65 63 74 6c 79 2c 20 77 65 20 75  * directly, we u
52cf0 73 65 20 69 74 73 20 63 6f 6e 73 74 61 6e 74 20  se its constant 
52d00 61 73 20 61 20 6b 65 79 20 74 6f 20 6c 6f 6f 6b  as a key to look
52d10 75 70 20 74 68 65 20 72 75 6e 2d 74 69 6d 65 20  up the run-time 
52d20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 75 66  allocated.** buf
52d30 66 65 72 20 74 68 61 74 20 68 6f 6c 64 73 20 72  fer that holds r
52d40 65 61 6c 20 76 61 72 69 61 62 6c 65 2e 20 20 54  eal variable.  T
52d50 68 65 20 63 6f 6e 73 74 61 6e 74 20 69 73 20 61  he constant is a
52d60 6c 73 6f 20 74 68 65 20 69 6e 69 74 69 61 6c 69  lso the initiali
52d70 7a 65 72 0a 2a 2a 20 66 6f 72 20 74 68 65 20 72  zer.** for the r
52d80 75 6e 2d 74 69 6d 65 20 61 6c 6c 6f 63 61 74 65  un-time allocate
52d90 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20  d buffer..**.** 
52da0 49 6e 20 74 68 65 20 75 73 75 61 6c 6c 79 20 63  In the usually c
52db0 61 73 65 20 77 68 65 72 65 20 57 53 44 20 69 73  ase where WSD is
52dc0 20 73 75 70 70 6f 72 74 65 64 2c 20 74 68 65 20   supported, the 
52dd0 53 51 4c 49 54 45 5f 57 53 44 20 61 6e 64 20 47  SQLITE_WSD and G
52de0 4c 4f 42 41 4c 0a 2a 2a 20 6d 61 63 72 6f 73 20  LOBAL.** macros 
52df0 62 65 63 6f 6d 65 20 6e 6f 2d 6f 70 73 20 61 6e  become no-ops an
52e00 64 20 68 61 76 65 20 7a 65 72 6f 20 70 65 72 66  d have zero perf
52e10 6f 72 6d 61 6e 63 65 20 69 6d 70 61 63 74 2e 0a  ormance impact..
52e20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
52e30 5f 4f 4d 49 54 5f 57 53 44 0a 20 20 23 64 65 66  _OMIT_WSD.  #def
52e40 69 6e 65 20 53 51 4c 49 54 45 5f 57 53 44 20 63  ine SQLITE_WSD c
52e50 6f 6e 73 74 0a 20 20 23 64 65 66 69 6e 65 20 47  onst.  #define G
52e60 4c 4f 42 41 4c 28 74 2c 76 29 20 28 2a 28 74 2a  LOBAL(t,v) (*(t*
52e70 29 73 71 6c 69 74 65 33 5f 77 73 64 5f 66 69 6e  )sqlite3_wsd_fin
52e80 64 28 28 76 6f 69 64 2a 29 26 28 76 29 2c 20 73  d((void*)&(v), s
52e90 69 7a 65 6f 66 28 76 29 29 29 0a 20 20 23 64 65  izeof(v))).  #de
52ea0 66 69 6e 65 20 73 71 6c 69 74 65 33 47 6c 6f 62  fine sqlite3Glob
52eb0 61 6c 43 6f 6e 66 69 67 20 47 4c 4f 42 41 4c 28  alConfig GLOBAL(
52ec0 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f  struct Sqlite3Co
52ed0 6e 66 69 67 2c 20 73 71 6c 69 74 65 33 43 6f 6e  nfig, sqlite3Con
52ee0 66 69 67 29 0a 53 51 4c 49 54 45 5f 41 50 49 20  fig).SQLITE_API 
52ef0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 77 73    int sqlite3_ws
52f00 64 5f 69 6e 69 74 28 69 6e 74 20 4e 2c 20 69 6e  d_init(int N, in
52f10 74 20 4a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  t J);.SQLITE_API
52f20 20 20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33     void *sqlite3
52f30 5f 77 73 64 5f 66 69 6e 64 28 76 6f 69 64 20 2a  _wsd_find(void *
52f40 4b 2c 20 69 6e 74 20 4c 29 3b 0a 23 65 6c 73 65  K, int L);.#else
52f50 0a 20 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54  .  #define SQLIT
52f60 45 5f 57 53 44 20 0a 20 20 23 64 65 66 69 6e 65  E_WSD .  #define
52f70 20 47 4c 4f 42 41 4c 28 74 2c 76 29 20 76 0a 20   GLOBAL(t,v) v. 
52f80 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33   #define sqlite3
52f90 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 73 71 6c  GlobalConfig sql
52fa0 69 74 65 33 43 6f 6e 66 69 67 0a 23 65 6e 64 69  ite3Config.#endi
52fb0 66 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64  f../*.** Forward
52fc0 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 73   references to s
52fd0 74 72 75 63 74 75 72 65 73 0a 2a 2f 0a 74 79 70  tructures.*/.typ
52fe0 65 64 65 66 20 73 74 72 75 63 74 20 41 67 67 49  edef struct AggI
52ff0 6e 66 6f 20 41 67 67 49 6e 66 6f 3b 0a 74 79 70  nfo AggInfo;.typ
53000 65 64 65 66 20 73 74 72 75 63 74 20 41 75 74 68  edef struct Auth
53010 43 6f 6e 74 65 78 74 20 41 75 74 68 43 6f 6e 74  Context AuthCont
53020 65 78 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ext;.typedef str
53030 75 63 74 20 42 69 74 76 65 63 20 42 69 74 76 65  uct Bitvec Bitve
53040 63 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  c;.typedef struc
53050 74 20 43 6f 6c 6c 53 65 71 20 43 6f 6c 6c 53 65  t CollSeq CollSe
53060 71 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  q;.typedef struc
53070 74 20 43 6f 6c 75 6d 6e 20 43 6f 6c 75 6d 6e 3b  t Column Column;
53080 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
53090 44 62 20 44 62 3b 0a 74 79 70 65 64 65 66 20 73  Db Db;.typedef s
530a0 74 72 75 63 74 20 53 63 68 65 6d 61 20 53 63 68  truct Schema Sch
530b0 65 6d 61 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ema;.typedef str
530c0 75 63 74 20 45 78 70 72 20 45 78 70 72 3b 0a 74  uct Expr Expr;.t
530d0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 45 78  ypedef struct Ex
530e0 70 72 4c 69 73 74 20 45 78 70 72 4c 69 73 74 3b  prList ExprList;
530f0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
53100 46 4b 65 79 20 46 4b 65 79 3b 0a 74 79 70 65 64  FKey FKey;.typed
53110 65 66 20 73 74 72 75 63 74 20 46 75 6e 63 44 65  ef struct FuncDe
53120 66 20 46 75 6e 63 44 65 66 3b 0a 74 79 70 65 64  f FuncDef;.typed
53130 65 66 20 73 74 72 75 63 74 20 46 75 6e 63 44 65  ef struct FuncDe
53140 66 48 61 73 68 20 46 75 6e 63 44 65 66 48 61 73  fHash FuncDefHas
53150 68 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  h;.typedef struc
53160 74 20 49 64 4c 69 73 74 20 49 64 4c 69 73 74 3b  t IdList IdList;
53170 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
53180 49 6e 64 65 78 20 49 6e 64 65 78 3b 0a 74 79 70  Index Index;.typ
53190 65 64 65 66 20 73 74 72 75 63 74 20 4b 65 79 43  edef struct KeyC
531a0 6c 61 73 73 20 4b 65 79 43 6c 61 73 73 3b 0a 74  lass KeyClass;.t
531b0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4b 65  ypedef struct Ke
531c0 79 49 6e 66 6f 20 4b 65 79 49 6e 66 6f 3b 0a 74  yInfo KeyInfo;.t
531d0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4c 6f  ypedef struct Lo
531e0 6f 6b 61 73 69 64 65 20 4c 6f 6f 6b 61 73 69 64  okaside Lookasid
531f0 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  e;.typedef struc
53200 74 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20  t LookasideSlot 
53210 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 3b 0a 74  LookasideSlot;.t
53220 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 6f  ypedef struct Mo
53230 64 75 6c 65 20 4d 6f 64 75 6c 65 3b 0a 74 79 70  dule Module;.typ
53240 65 64 65 66 20 73 74 72 75 63 74 20 4e 61 6d 65  edef struct Name
53250 43 6f 6e 74 65 78 74 20 4e 61 6d 65 43 6f 6e 74  Context NameCont
53260 65 78 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ext;.typedef str
53270 75 63 74 20 50 61 72 73 65 20 50 61 72 73 65 3b  uct Parse Parse;
53280 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
53290 53 65 6c 65 63 74 20 53 65 6c 65 63 74 3b 0a 74  Select Select;.t
532a0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 72  ypedef struct Sr
532b0 63 4c 69 73 74 20 53 72 63 4c 69 73 74 3b 0a 74  cList SrcList;.t
532c0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 74  ypedef struct St
532d0 72 41 63 63 75 6d 20 53 74 72 41 63 63 75 6d 3b  rAccum StrAccum;
532e0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
532f0 54 61 62 6c 65 20 54 61 62 6c 65 3b 0a 74 79 70  Table Table;.typ
53300 65 64 65 66 20 73 74 72 75 63 74 20 54 61 62 6c  edef struct Tabl
53310 65 4c 6f 63 6b 20 54 61 62 6c 65 4c 6f 63 6b 3b  eLock TableLock;
53320 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
53330 54 6f 6b 65 6e 20 54 6f 6b 65 6e 3b 0a 74 79 70  Token Token;.typ
53340 65 64 65 66 20 73 74 72 75 63 74 20 54 72 69 67  edef struct Trig
53350 67 65 72 53 74 61 63 6b 20 54 72 69 67 67 65 72  gerStack Trigger
53360 53 74 61 63 6b 3b 0a 74 79 70 65 64 65 66 20 73  Stack;.typedef s
53370 74 72 75 63 74 20 54 72 69 67 67 65 72 53 74 65  truct TriggerSte
53380 70 20 54 72 69 67 67 65 72 53 74 65 70 3b 0a 74  p TriggerStep;.t
53390 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 72  ypedef struct Tr
533a0 69 67 67 65 72 20 54 72 69 67 67 65 72 3b 0a 74  igger Trigger;.t
533b0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 55 6e  ypedef struct Un
533c0 70 61 63 6b 65 64 52 65 63 6f 72 64 20 55 6e 70  packedRecord Unp
533d0 61 63 6b 65 64 52 65 63 6f 72 64 3b 0a 74 79 70  ackedRecord;.typ
533e0 65 64 65 66 20 73 74 72 75 63 74 20 57 61 6c 6b  edef struct Walk
533f0 65 72 20 57 61 6c 6b 65 72 3b 0a 74 79 70 65 64  er Walker;.typed
53400 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65 49  ef struct WhereI
53410 6e 66 6f 20 57 68 65 72 65 49 6e 66 6f 3b 0a 74  nfo WhereInfo;.t
53420 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57 68  ypedef struct Wh
53430 65 72 65 4c 65 76 65 6c 20 57 68 65 72 65 4c 65  ereLevel WhereLe
53440 76 65 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 65  vel;../*.** Defe
53450 72 20 73 6f 75 72 63 69 6e 67 20 76 64 62 65 2e  r sourcing vdbe.
53460 68 20 61 6e 64 20 62 74 72 65 65 2e 68 20 75 6e  h and btree.h un
53470 74 69 6c 20 61 66 74 65 72 20 74 68 65 20 22 75  til after the "u
53480 38 22 20 61 6e 64 20 0a 2a 2a 20 22 42 75 73 79  8" and .** "Busy
53490 48 61 6e 64 6c 65 72 22 20 74 79 70 65 64 65 66  Handler" typedef
534a0 73 2e 20 76 64 62 65 2e 68 20 61 6c 73 6f 20 72  s. vdbe.h also r
534b0 65 71 75 69 72 65 73 20 61 20 66 65 77 20 6f 66  equires a few of
534c0 20 74 68 65 20 6f 70 61 71 75 65 0a 2a 2a 20 70   the opaque.** p
534d0 6f 69 6e 74 65 72 20 74 79 70 65 73 20 28 69 2e  ointer types (i.
534e0 65 2e 20 46 75 6e 63 44 65 66 29 20 64 65 66 69  e. FuncDef) defi
534f0 6e 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 2f 2a  ned above..*/./*
53500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
53510 63 6c 75 64 65 20 62 74 72 65 65 2e 68 20 69 6e  clude btree.h in
53520 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73   the middle of s
53530 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a  qliteInt.h *****
53540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
53550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
53560 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65 2e 68  gin file btree.h
53570 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
53580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
53590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
535a0 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
535b0 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
535c0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
535d0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
535e0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
535f0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
53600 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
53610 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
53620 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
53630 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
53640 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
53650 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
53660 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
53670 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
53680 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
53690 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
536a0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
536b0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
536c0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
536d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
536e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
536f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
53700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
53710 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61  ****.** This hea
53720 64 65 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73  der file defines
53730 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74   the interface t
53740 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 20 42  hat the sqlite B
53750 2d 54 72 65 65 20 66 69 6c 65 0a 2a 2a 20 73 75  -Tree file.** su
53760 62 73 79 73 74 65 6d 2e 20 20 53 65 65 20 63 6f  bsystem.  See co
53770 6d 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 73 6f  mments in the so
53780 75 72 63 65 20 63 6f 64 65 20 66 6f 72 20 61 20  urce code for a 
53790 64 65 74 61 69 6c 65 64 20 64 65 73 63 72 69 70  detailed descrip
537a0 74 69 6f 6e 0a 2a 2a 20 6f 66 20 77 68 61 74 20  tion.** of what 
537b0 65 61 63 68 20 69 6e 74 65 72 66 61 63 65 20 72  each interface r
537c0 6f 75 74 69 6e 65 20 64 6f 65 73 2e 0a 2a 2a 0a  outine does..**.
537d0 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 62 74 72  ** @(#) $Id: btr
537e0 65 65 2e 68 2c 76 20 31 2e 31 30 35 20 32 30 30  ee.h,v 1.105 200
537f0 38 2f 31 30 2f 32 37 20 31 33 3a 35 39 3a 33 34  8/10/27 13:59:34
53800 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70   danielk1977 Exp
53810 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 42   $.*/.#ifndef _B
53820 54 52 45 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20  TREE_H_.#define 
53830 5f 42 54 52 45 45 5f 48 5f 0a 0a 2f 2a 20 54 4f  _BTREE_H_../* TO
53840 44 4f 3a 20 54 68 69 73 20 64 65 66 69 6e 69 74  DO: This definit
53850 69 6f 6e 20 69 73 20 6a 75 73 74 20 69 6e 63 6c  ion is just incl
53860 75 64 65 64 20 73 6f 20 6f 74 68 65 72 20 6d 6f  uded so other mo
53870 64 75 6c 65 73 20 63 6f 6d 70 69 6c 65 2e 20 49  dules compile. I
53880 74 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 65  t.** needs to be
53890 20 72 65 76 69 73 69 74 65 64 2e 0a 2a 2f 0a 23   revisited..*/.#
538a0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f  define SQLITE_N_
538b0 42 54 52 45 45 5f 4d 45 54 41 20 31 30 0a 0a 2f  BTREE_META 10../
538c0 2a 0a 2a 2a 20 49 66 20 64 65 66 69 6e 65 64 20  *.** If defined 
538d0 61 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 75 74  as non-zero, aut
538e0 6f 2d 76 61 63 75 75 6d 20 69 73 20 65 6e 61 62  o-vacuum is enab
538f0 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e 20  led by default. 
53900 4f 74 68 65 72 77 69 73 65 0a 2a 2a 20 69 74 20  Otherwise.** it 
53910 6d 75 73 74 20 62 65 20 74 75 72 6e 65 64 20 6f  must be turned o
53920 6e 20 66 6f 72 20 65 61 63 68 20 64 61 74 61 62  n for each datab
53930 61 73 65 20 75 73 69 6e 67 20 22 50 52 41 47 4d  ase using "PRAGM
53940 41 20 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20  A auto_vacuum = 
53950 31 22 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  1"..*/.#ifndef S
53960 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55  QLITE_DEFAULT_AU
53970 54 4f 56 41 43 55 55 4d 0a 20 20 23 64 65 66 69  TOVACUUM.  #defi
53980 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ne SQLITE_DEFAUL
53990 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 30 0a 23  T_AUTOVACUUM 0.#
539a0 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 42  endif..#define B
539b0 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f  TREE_AUTOVACUUM_
539c0 4e 4f 4e 45 20 30 20 20 20 20 20 20 20 20 2f 2a  NONE 0        /*
539d0 20 44 6f 20 6e 6f 74 20 64 6f 20 61 75 74 6f 2d   Do not do auto-
539e0 76 61 63 75 75 6d 20 2a 2f 0a 23 64 65 66 69 6e  vacuum */.#defin
539f0 65 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55  e BTREE_AUTOVACU
53a00 55 4d 5f 46 55 4c 4c 20 31 20 20 20 20 20 20 20  UM_FULL 1       
53a10 20 2f 2a 20 44 6f 20 66 75 6c 6c 20 61 75 74 6f   /* Do full auto
53a20 2d 76 61 63 75 75 6d 20 2a 2f 0a 23 64 65 66 69  -vacuum */.#defi
53a30 6e 65 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43  ne BTREE_AUTOVAC
53a40 55 55 4d 5f 49 4e 43 52 20 32 20 20 20 20 20 20  UUM_INCR 2      
53a50 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 61 6c    /* Incremental
53a60 20 76 61 63 75 75 6d 20 2a 2f 0a 0a 2f 2a 0a 2a   vacuum */../*.*
53a70 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72  * Forward declar
53a80 61 74 69 6f 6e 73 20 6f 66 20 73 74 72 75 63 74  ations of struct
53a90 75 72 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ure.*/.typedef s
53aa0 74 72 75 63 74 20 42 74 72 65 65 20 42 74 72 65  truct Btree Btre
53ab0 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  e;.typedef struc
53ac0 74 20 42 74 43 75 72 73 6f 72 20 42 74 43 75 72  t BtCursor BtCur
53ad0 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72  sor;.typedef str
53ae0 75 63 74 20 42 74 53 68 61 72 65 64 20 42 74 53  uct BtShared BtS
53af0 68 61 72 65 64 3b 0a 74 79 70 65 64 65 66 20 73  hared;.typedef s
53b00 74 72 75 63 74 20 42 74 72 65 65 4d 75 74 65 78  truct BtreeMutex
53b10 41 72 72 61 79 20 42 74 72 65 65 4d 75 74 65 78  Array BtreeMutex
53b20 41 72 72 61 79 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68  Array;../*.** Th
53b30 69 73 20 73 74 72 75 63 74 75 72 65 20 72 65 63  is structure rec
53b40 6f 72 64 73 20 61 6c 6c 20 6f 66 20 74 68 65 20  ords all of the 
53b50 42 74 72 65 65 73 20 74 68 61 74 20 6e 65 65 64  Btrees that need
53b60 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 61 20 6d 75   to hold.** a mu
53b70 74 65 78 20 62 65 66 6f 72 65 20 77 65 20 65 6e  tex before we en
53b80 74 65 72 20 73 71 6c 69 74 65 33 56 64 62 65 45  ter sqlite3VdbeE
53b90 78 65 63 28 29 2e 20 20 54 68 65 20 42 74 72 65  xec().  The Btre
53ba0 65 73 20 61 72 65 0a 2a 2a 20 61 72 65 20 70 6c  es are.** are pl
53bb0 61 63 65 64 20 69 6e 20 61 42 74 72 65 65 5b 5d  aced in aBtree[]
53bc0 20 69 6e 20 6f 72 64 65 72 20 6f 66 20 61 42 74   in order of aBt
53bd0 72 65 65 5b 5d 2d 3e 70 42 74 2e 20 20 54 68 61  ree[]->pBt.  Tha
53be0 74 20 77 61 79 2c 0a 2a 2a 20 77 65 20 63 61 6e  t way,.** we can
53bf0 20 61 6c 77 61 79 73 20 6c 6f 63 6b 20 61 6e 64   always lock and
53c00 20 75 6e 6c 6f 63 6b 20 74 68 65 6d 20 61 6c 6c   unlock them all
53c10 20 71 75 69 63 6b 6c 79 2e 0a 2a 2f 0a 73 74 72   quickly..*/.str
53c20 75 63 74 20 42 74 72 65 65 4d 75 74 65 78 41 72  uct BtreeMutexAr
53c30 72 61 79 20 7b 0a 20 20 69 6e 74 20 6e 4d 75 74  ray {.  int nMut
53c40 65 78 3b 0a 20 20 42 74 72 65 65 20 2a 61 42 74  ex;.  Btree *aBt
53c50 72 65 65 5b 53 51 4c 49 54 45 5f 4d 41 58 5f 41  ree[SQLITE_MAX_A
53c60 54 54 41 43 48 45 44 2b 31 5d 3b 0a 7d 3b 0a 0a  TTACHED+1];.};..
53c70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
53c80 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
53c90 4f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68  Open(.  const ch
53ca0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20  ar *zFilename,  
53cb0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61   /* Name of data
53cc0 62 61 73 65 20 66 69 6c 65 20 74 6f 20 6f 70 65  base file to ope
53cd0 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  n */.  sqlite3 *
53ce0 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
53cf0 2f 2a 20 41 73 73 6f 63 69 61 74 65 64 20 64 61  /* Associated da
53d00 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
53d10 6e 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a 2c  n */.  Btree **,
53d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53d30 2f 2a 20 52 65 74 75 72 6e 20 6f 70 65 6e 20 42  /* Return open B
53d40 74 72 65 65 2a 20 68 65 72 65 20 2a 2f 0a 20 20  tree* here */.  
53d50 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20  int flags,      
53d60 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67           /* Flag
53d70 73 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c  s */.  int vfsFl
53d80 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 20  ags             
53d90 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20  /* Flags passed 
53da0 74 68 72 6f 75 67 68 20 74 6f 20 56 46 53 20 6f  through to VFS o
53db0 70 65 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 20 54 68  pen */.);../* Th
53dc0 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65  e flags paramete
53dd0 72 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65  r to sqlite3Btre
53de0 65 4f 70 65 6e 20 63 61 6e 20 62 65 20 74 68 65  eOpen can be the
53df0 20 62 69 74 77 69 73 65 20 6f 72 20 6f 66 20 74   bitwise or of t
53e00 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20  he.** following 
53e10 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e 4f  values..**.** NO
53e20 54 45 3a 20 20 54 68 65 73 65 20 76 61 6c 75 65  TE:  These value
53e30 73 20 6d 75 73 74 20 6d 61 74 63 68 20 74 68 65  s must match the
53e40 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 50   corresponding P
53e50 41 47 45 52 5f 20 76 61 6c 75 65 73 20 69 6e 0a  AGER_ values in.
53e60 2a 2a 20 70 61 67 65 72 2e 68 2e 0a 2a 2f 0a 23  ** pager.h..*/.#
53e70 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4f 4d 49  define BTREE_OMI
53e80 54 5f 4a 4f 55 52 4e 41 4c 20 20 31 20 20 2f 2a  T_JOURNAL  1  /*
53e90 20 44 6f 20 6e 6f 74 20 75 73 65 20 6a 6f 75 72   Do not use jour
53ea0 6e 61 6c 2e 20 20 4e 6f 20 61 72 67 75 6d 65 6e  nal.  No argumen
53eb0 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52  t */.#define BTR
53ec0 45 45 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20  EE_NO_READLOCK  
53ed0 20 32 20 20 2f 2a 20 4f 6d 69 74 20 72 65 61 64   2  /* Omit read
53ee0 6c 6f 63 6b 73 20 6f 6e 20 72 65 61 64 6f 6e 6c  locks on readonl
53ef0 79 20 66 69 6c 65 73 20 2a 2f 0a 23 64 65 66 69  y files */.#defi
53f00 6e 65 20 42 54 52 45 45 5f 4d 45 4d 4f 52 59 20  ne BTREE_MEMORY 
53f10 20 20 20 20 20 20 20 34 20 20 2f 2a 20 49 6e 2d         4  /* In-
53f20 6d 65 6d 6f 72 79 20 44 42 2e 20 20 4e 6f 20 61  memory DB.  No a
53f30 72 67 75 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69  rgument */.#defi
53f40 6e 65 20 42 54 52 45 45 5f 52 45 41 44 4f 4e 4c  ne BTREE_READONL
53f50 59 20 20 20 20 20 20 38 20 20 2f 2a 20 4f 70 65  Y      8  /* Ope
53f60 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  n the database i
53f70 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65  n read-only mode
53f80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45   */.#define BTRE
53f90 45 5f 52 45 41 44 57 52 49 54 45 20 20 20 20 31  E_READWRITE    1
53fa0 36 20 20 2f 2a 20 4f 70 65 6e 20 66 6f 72 20 62  6  /* Open for b
53fb0 6f 74 68 20 72 65 61 64 69 6e 67 20 61 6e 64 20  oth reading and 
53fc0 77 72 69 74 69 6e 67 20 2a 2f 0a 23 64 65 66 69  writing */.#defi
53fd0 6e 65 20 42 54 52 45 45 5f 43 52 45 41 54 45 20  ne BTREE_CREATE 
53fe0 20 20 20 20 20 20 33 32 20 20 2f 2a 20 43 72 65        32  /* Cre
53ff0 61 74 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ate the database
54000 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20   if it does not 
54010 65 78 69 73 74 20 2a 2f 0a 0a 53 51 4c 49 54 45  exist */..SQLITE
54020 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
54030 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 42  ite3BtreeClose(B
54040 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  tree*);.SQLITE_P
54050 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
54060 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53  e3BtreeSetCacheS
54070 69 7a 65 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b  ize(Btree*,int);
54080 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
54090 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
540a0 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 42  SetSafetyLevel(B
540b0 74 72 65 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a  tree*,int,int);.
540c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
540d0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  nt sqlite3BtreeS
540e0 79 6e 63 44 69 73 61 62 6c 65 64 28 42 74 72 65  yncDisabled(Btre
540f0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
54100 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
54110 74 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28  treeSetPageSize(
54120 42 74 72 65 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b  Btree*,int,int);
54130 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
54140 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
54150 47 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65  GetPageSize(Btre
54160 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
54170 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
54180 74 72 65 65 4d 61 78 50 61 67 65 43 6f 75 6e 74  treeMaxPageCount
54190 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51  (Btree*,int);.SQ
541a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
541b0 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
541c0 52 65 73 65 72 76 65 28 42 74 72 65 65 2a 29 3b  Reserve(Btree*);
541d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
541e0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
541f0 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74  SetAutoVacuum(Bt
54200 72 65 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  ree *, int);.SQL
54210 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
54220 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 41  sqlite3BtreeGetA
54230 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20  utoVacuum(Btree 
54240 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
54250 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
54260 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 42 74  reeBeginTrans(Bt
54270 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ree*,int);.SQLIT
54280 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
54290 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74  lite3BtreeCommit
542a0 50 68 61 73 65 4f 6e 65 28 42 74 72 65 65 2a 2c  PhaseOne(Btree*,
542b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61   const char *zMa
542c0 73 74 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ster);.SQLITE_PR
542d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
542e0 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73  3BtreeCommitPhas
542f0 65 54 77 6f 28 42 74 72 65 65 2a 29 3b 0a 53 51  eTwo(Btree*);.SQ
54300 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
54310 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
54320 6d 69 74 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c  mit(Btree*);.SQL
54330 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
54340 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c  sqlite3BtreeRoll
54350 62 61 63 6b 28 42 74 72 65 65 2a 29 3b 0a 53 51  back(Btree*);.SQ
54360 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
54370 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67   sqlite3BtreeBeg
54380 69 6e 53 74 6d 74 28 42 74 72 65 65 2a 29 3b 0a  inStmt(Btree*);.
54390 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
543a0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
543b0 6f 6d 6d 69 74 53 74 6d 74 28 42 74 72 65 65 2a  ommitStmt(Btree*
543c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
543d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
543e0 65 65 52 6f 6c 6c 62 61 63 6b 53 74 6d 74 28 42  eeRollbackStmt(B
543f0 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  tree*);.SQLITE_P
54400 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
54410 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62  e3BtreeCreateTab
54420 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 2a 2c  le(Btree*, int*,
54430 20 69 6e 74 20 66 6c 61 67 73 29 3b 0a 53 51 4c   int flags);.SQL
54440 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
54450 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e  sqlite3BtreeIsIn
54460 54 72 61 6e 73 28 42 74 72 65 65 2a 29 3b 0a 53  Trans(Btree*);.S
54470 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
54480 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73  t sqlite3BtreeIs
54490 49 6e 53 74 6d 74 28 42 74 72 65 65 2a 29 3b 0a  InStmt(Btree*);.
544a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
544b0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  nt sqlite3BtreeI
544c0 73 49 6e 52 65 61 64 54 72 61 6e 73 28 42 74 72  sInReadTrans(Btr
544d0 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ee*);.SQLITE_PRI
544e0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
544f0 65 33 42 74 72 65 65 53 63 68 65 6d 61 28 42 74  e3BtreeSchema(Bt
54500 72 65 65 20 2a 2c 20 69 6e 74 2c 20 76 6f 69 64  ree *, int, void
54510 28 2a 29 28 76 6f 69 64 20 2a 29 29 3b 0a 53 51  (*)(void *));.SQ
54520 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
54530 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68   sqlite3BtreeSch
54540 65 6d 61 4c 6f 63 6b 65 64 28 42 74 72 65 65 20  emaLocked(Btree 
54550 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
54560 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
54570 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42 74 72  reeLockTable(Btr
54580 65 65 20 2a 2c 20 69 6e 74 2c 20 75 38 29 3b 0a  ee *, int, u8);.
54590 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
545a0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
545b0 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e  te3BtreeGetFilen
545c0 61 6d 65 28 42 74 72 65 65 20 2a 29 3b 0a 53 51  ame(Btree *);.SQ
545d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
545e0 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
545f0 42 74 72 65 65 47 65 74 44 69 72 6e 61 6d 65 28  BtreeGetDirname(
54600 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45  Btree *);.SQLITE
54610 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
54620 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65  har *sqlite3Btre
54630 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28  eGetJournalname(
54640 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45  Btree *);.SQLITE
54650 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
54660 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c  ite3BtreeCopyFil
54670 65 28 42 74 72 65 65 20 2a 2c 20 42 74 72 65 65  e(Btree *, Btree
54680 20 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49   *);..SQLITE_PRI
54690 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
546a0 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d 28  BtreeIncrVacuum(
546b0 42 74 72 65 65 20 2a 29 3b 0a 0a 2f 2a 20 54 68  Btree *);../* Th
546c0 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65  e flags paramete
546d0 72 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65  r to sqlite3Btre
546e0 65 43 72 65 61 74 65 54 61 62 6c 65 20 63 61 6e  eCreateTable can
546f0 20 62 65 20 74 68 65 20 62 69 74 77 69 73 65 20   be the bitwise 
54700 4f 52 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c  OR.** of the fol
54710 6c 6f 77 69 6e 67 20 66 6c 61 67 73 3a 0a 2a 2f  lowing flags:.*/
54720 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 49  .#define BTREE_I
54730 4e 54 4b 45 59 20 20 20 20 20 31 20 20 20 20 2f  NTKEY     1    /
54740 2a 20 54 61 62 6c 65 20 68 61 73 20 6f 6e 6c 79  * Table has only
54750 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69   64-bit signed i
54760 6e 74 65 67 65 72 20 6b 65 79 73 20 2a 2f 0a 23  nteger keys */.#
54770 64 65 66 69 6e 65 20 42 54 52 45 45 5f 5a 45 52  define BTREE_ZER
54780 4f 44 41 54 41 20 20 20 32 20 20 20 20 2f 2a 20  ODATA   2    /* 
54790 54 61 62 6c 65 20 68 61 73 20 6b 65 79 73 20 6f  Table has keys o
547a0 6e 6c 79 20 2d 20 6e 6f 20 64 61 74 61 20 2a 2f  nly - no data */
547b0 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4c  .#define BTREE_L
547c0 45 41 46 44 41 54 41 20 20 20 34 20 20 20 20 2f  EAFDATA   4    /
547d0 2a 20 44 61 74 61 20 73 74 6f 72 65 64 20 69 6e  * Data stored in
547e0 20 6c 65 61 76 65 73 20 6f 6e 6c 79 2e 20 20 49   leaves only.  I
547f0 6d 70 6c 69 65 73 20 49 4e 54 4b 45 59 20 2a 2f  mplies INTKEY */
54800 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
54810 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
54820 65 44 72 6f 70 54 61 62 6c 65 28 42 74 72 65 65  eDropTable(Btree
54830 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53  *, int, int*);.S
54840 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
54850 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c  t sqlite3BtreeCl
54860 65 61 72 54 61 62 6c 65 28 42 74 72 65 65 2a 2c  earTable(Btree*,
54870 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c   int, int*);.SQL
54880 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
54890 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d  sqlite3BtreeGetM
548a0 65 74 61 28 42 74 72 65 65 2a 2c 20 69 6e 74 20  eta(Btree*, int 
548b0 69 64 78 2c 20 75 33 32 20 2a 70 56 61 6c 75 65  idx, u32 *pValue
548c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
548d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
548e0 65 65 55 70 64 61 74 65 4d 65 74 61 28 42 74 72  eeUpdateMeta(Btr
548f0 65 65 2a 2c 20 69 6e 74 20 69 64 78 2c 20 75 33  ee*, int idx, u3
54900 32 20 76 61 6c 75 65 29 3b 0a 53 51 4c 49 54 45  2 value);.SQLITE
54910 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
54920 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 41 6c  lite3BtreeTripAl
54930 6c 43 75 72 73 6f 72 73 28 42 74 72 65 65 2a 2c  lCursors(Btree*,
54940 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50   int);..SQLITE_P
54950 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
54960 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 0a 20  e3BtreeCursor(. 
54970 20 42 74 72 65 65 2a 2c 20 20 20 20 20 20 20 20   Btree*,        
54980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54990 20 20 20 20 20 20 2f 2a 20 42 54 72 65 65 20 63        /* BTree c
549a0 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 20  ontaining table 
549b0 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74  to open */.  int
549c0 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20   iTable,        
549d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
549e0 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 6f    /* Index of ro
549f0 6f 74 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74  ot page */.  int
54a00 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20   wrFlag,        
54a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54a20 20 20 2f 2a 20 31 20 66 6f 72 20 77 72 69 74 69    /* 1 for writi
54a30 6e 67 2e 20 20 30 20 66 6f 72 20 72 65 61 64 2d  ng.  0 for read-
54a40 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 74  only */.  struct
54a50 20 4b 65 79 49 6e 66 6f 2a 2c 20 20 20 20 20 20   KeyInfo*,      
54a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
54a70 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74  * First argument
54a80 20 74 6f 20 63 6f 6d 70 61 72 65 20 66 75 6e 63   to compare func
54a90 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72 73  tion */.  BtCurs
54aa0 6f 72 20 2a 70 43 75 72 73 6f 72 20 20 20 20 20  or *pCursor     
54ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
54ac0 2a 20 53 70 61 63 65 20 74 6f 20 77 72 69 74 65  * Space to write
54ad0 20 63 75 72 73 6f 72 20 73 74 72 75 63 74 75 72   cursor structur
54ae0 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 50  e */.);.SQLITE_P
54af0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
54b00 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a  e3BtreeCursorSiz
54b10 65 28 76 6f 69 64 29 3b 0a 0a 53 51 4c 49 54 45  e(void);..SQLITE
54b20 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
54b30 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75  ite3BtreeCloseCu
54b40 72 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 3b  rsor(BtCursor*);
54b50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
54b60 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
54b70 4d 6f 76 65 74 6f 28 0a 20 20 42 74 43 75 72 73  Moveto(.  BtCurs
54b80 6f 72 2a 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69  or*,.  const voi
54b90 64 20 2a 70 4b 65 79 2c 0a 20 20 69 36 34 20 6e  d *pKey,.  i64 n
54ba0 4b 65 79 2c 0a 20 20 69 6e 74 20 62 69 61 73 2c  Key,.  int bias,
54bb0 0a 20 20 69 6e 74 20 2a 70 52 65 73 0a 29 3b 0a  .  int *pRes.);.
54bc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
54bd0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  nt sqlite3BtreeM
54be0 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 0a 20  ovetoUnpacked(. 
54bf0 20 42 74 43 75 72 73 6f 72 2a 2c 0a 20 20 55 6e   BtCursor*,.  Un
54c00 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 55  packedRecord *pU
54c10 6e 4b 65 79 2c 0a 20 20 69 36 34 20 69 6e 74 4b  nKey,.  i64 intK
54c20 65 79 2c 0a 20 20 69 6e 74 20 62 69 61 73 2c 0a  ey,.  int bias,.
54c30 20 20 69 6e 74 20 2a 70 52 65 73 0a 29 3b 0a 53    int *pRes.);.S
54c40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
54c50 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  t sqlite3BtreeCu
54c60 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 42 74 43  rsorHasMoved(BtC
54c70 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53  ursor*, int*);.S
54c80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
54c90 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65  t sqlite3BtreeDe
54ca0 6c 65 74 65 28 42 74 43 75 72 73 6f 72 2a 29 3b  lete(BtCursor*);
54cb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
54cc0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
54cd0 49 6e 73 65 72 74 28 42 74 43 75 72 73 6f 72 2a  Insert(BtCursor*
54ce0 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b  , const void *pK
54cf0 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c 0a 20 20  ey, i64 nKey,.  
54d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54d20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74  const void *pDat
54d30 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20  a, int nData,.  
54d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54d60 69 6e 74 20 6e 5a 65 72 6f 2c 20 69 6e 74 20 62  int nZero, int b
54d70 69 61 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ias);.SQLITE_PRI
54d80 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
54d90 42 74 72 65 65 46 69 72 73 74 28 42 74 43 75 72  BtreeFirst(BtCur
54da0 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29  sor*, int *pRes)
54db0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
54dc0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
54dd0 65 4c 61 73 74 28 42 74 43 75 72 73 6f 72 2a 2c  eLast(BtCursor*,
54de0 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c   int *pRes);.SQL
54df0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
54e00 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74  sqlite3BtreeNext
54e10 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20  (BtCursor*, int 
54e20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50  *pRes);.SQLITE_P
54e30 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
54e40 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 75 72  e3BtreeEof(BtCur
54e50 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  sor*);.SQLITE_PR
54e60 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
54e70 33 42 74 72 65 65 46 6c 61 67 73 28 42 74 43 75  3BtreeFlags(BtCu
54e80 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  rsor*);.SQLITE_P
54e90 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
54ea0 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28  e3BtreePrevious(
54eb0 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a  BtCursor*, int *
54ec0 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52  pRes);.SQLITE_PR
54ed0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
54ee0 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 42 74  3BtreeKeySize(Bt
54ef0 43 75 72 73 6f 72 2a 2c 20 69 36 34 20 2a 70 53  Cursor*, i64 *pS
54f00 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ize);.SQLITE_PRI
54f10 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
54f20 42 74 72 65 65 4b 65 79 28 42 74 43 75 72 73 6f  BtreeKey(BtCurso
54f30 72 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20  r*, u32 offset, 
54f40 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b  u32 amt, void*);
54f50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
54f60 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33  sqlite3 *sqlite3
54f70 42 74 72 65 65 43 75 72 73 6f 72 44 62 28 63 6f  BtreeCursorDb(co
54f80 6e 73 74 20 42 74 43 75 72 73 6f 72 2a 29 3b 0a  nst BtCursor*);.
54f90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
54fa0 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
54fb0 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28  e3BtreeKeyFetch(
54fc0 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a  BtCursor*, int *
54fd0 70 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  pAmt);.SQLITE_PR
54fe0 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64  IVATE const void
54ff0 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 44 61   *sqlite3BtreeDa
55000 74 61 46 65 74 63 68 28 42 74 43 75 72 73 6f 72  taFetch(BtCursor
55010 2a 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0a 53  *, int *pAmt);.S
55020 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
55030 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61  t sqlite3BtreeDa
55040 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72 2a  taSize(BtCursor*
55050 2c 20 75 33 32 20 2a 70 53 69 7a 65 29 3b 0a 53  , u32 *pSize);.S
55060 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
55070 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61  t sqlite3BtreeDa
55080 74 61 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33  ta(BtCursor*, u3
55090 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d  2 offset, u32 am
550a0 74 2c 20 76 6f 69 64 2a 29 3b 0a 0a 53 51 4c 49  t, void*);..SQLI
550b0 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20  TE_PRIVATE char 
550c0 2a 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74  *sqlite3BtreeInt
550d0 65 67 72 69 74 79 43 68 65 63 6b 28 42 74 72 65  egrityCheck(Btre
550e0 65 2a 2c 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20  e*, int *aRoot, 
550f0 69 6e 74 20 6e 52 6f 6f 74 2c 20 69 6e 74 2c 20  int nRoot, int, 
55100 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  int*);.SQLITE_PR
55110 49 56 41 54 45 20 73 74 72 75 63 74 20 50 61 67  IVATE struct Pag
55120 65 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  er *sqlite3Btree
55130 50 61 67 65 72 28 42 74 72 65 65 2a 29 3b 0a 0a  Pager(Btree*);..
55140 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
55150 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  nt sqlite3BtreeP
55160 75 74 44 61 74 61 28 42 74 43 75 72 73 6f 72 2a  utData(BtCursor*
55170 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33  , u32 offset, u3
55180 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53  2 amt, void*);.S
55190 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
551a0 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  id sqlite3BtreeC
551b0 61 63 68 65 4f 76 65 72 66 6c 6f 77 28 42 74 43  acheOverflow(BtC
551c0 75 72 73 6f 72 20 2a 29 3b 0a 53 51 4c 49 54 45  ursor *);.SQLITE
551d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
551e0 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43  lite3BtreeClearC
551f0 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a  ursor(BtCursor *
55200 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  );..#ifdef SQLIT
55210 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52  E_TEST.SQLITE_PR
55220 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
55230 33 42 74 72 65 65 43 75 72 73 6f 72 49 6e 66 6f  3BtreeCursorInfo
55240 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 2a  (BtCursor*, int*
55250 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
55260 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
55270 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 4c 69  te3BtreeCursorLi
55280 73 74 28 42 74 72 65 65 2a 29 3b 0a 23 65 6e 64  st(Btree*);.#end
55290 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20  if../*.** If we 
552a0 61 72 65 20 6e 6f 74 20 75 73 69 6e 67 20 73 68  are not using sh
552b0 61 72 65 64 20 63 61 63 68 65 2c 20 74 68 65 6e  ared cache, then
552c0 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65   there is no nee
552d0 64 20 74 6f 0a 2a 2a 20 75 73 65 20 6d 75 74 65  d to.** use mute
552e0 78 65 73 20 74 6f 20 61 63 63 65 73 73 20 74 68  xes to access th
552f0 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63  e BtShared struc
55300 74 75 72 65 73 2e 20 20 53 6f 20 6d 61 6b 65 20  tures.  So make 
55310 74 68 65 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64  the.** Enter and
55320 20 4c 65 61 76 65 20 70 72 6f 63 65 64 75 72 65   Leave procedure
55330 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 69 66  s no-ops..*/.#if
55340 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
55350 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
55360 48 45 29 20 26 26 20 53 51 4c 49 54 45 5f 54 48  HE) && SQLITE_TH
55370 52 45 41 44 53 41 46 45 0a 53 51 4c 49 54 45 5f  READSAFE.SQLITE_
55380 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
55390 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
553a0 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
553b0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
553c0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
553d0 65 28 42 74 72 65 65 2a 29 3b 0a 23 69 66 6e 64  e(Btree*);.#ifnd
553e0 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 54  ef NDEBUG.  /* T
553f0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
55400 73 65 64 20 69 6e 73 69 64 65 20 61 73 73 65 72  sed inside asser
55410 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 6f  t() statements o
55420 6e 6c 79 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  nly. */.SQLITE_P
55430 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c  RIVATE   int sql
55440 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75  ite3BtreeHoldsMu
55450 74 65 78 28 42 74 72 65 65 2a 29 3b 0a 23 65 6e  tex(Btree*);.#en
55460 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  dif.SQLITE_PRIVA
55470 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
55480 33 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f  3BtreeEnterCurso
55490 72 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51  r(BtCursor*);.SQ
554a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
554b0 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
554c0 4c 65 61 76 65 43 75 72 73 6f 72 28 42 74 43 75  LeaveCursor(BtCu
554d0 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  rsor*);.SQLITE_P
554e0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
554f0 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41  lite3BtreeEnterA
55500 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51  ll(sqlite3*);.SQ
55510 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
55520 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
55530 4c 65 61 76 65 41 6c 6c 28 73 71 6c 69 74 65 33  LeaveAll(sqlite3
55540 2a 29 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  *);.#ifndef NDEB
55550 55 47 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75  UG.  /* This rou
55560 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73  tine is used ins
55570 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61  ide assert() sta
55580 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f  tements only. */
55590 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
555a0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72    int sqlite3Btr
555b0 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65  eeHoldsAllMutexe
555c0 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6e  s(sqlite3*);.#en
555d0 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  dif.SQLITE_PRIVA
555e0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
555f0 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79  3BtreeMutexArray
55600 45 6e 74 65 72 28 42 74 72 65 65 4d 75 74 65 78  Enter(BtreeMutex
55610 41 72 72 61 79 2a 29 3b 0a 53 51 4c 49 54 45 5f  Array*);.SQLITE_
55620 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
55630 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78  qlite3BtreeMutex
55640 41 72 72 61 79 4c 65 61 76 65 28 42 74 72 65 65  ArrayLeave(Btree
55650 4d 75 74 65 78 41 72 72 61 79 2a 29 3b 0a 53 51  MutexArray*);.SQ
55660 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
55670 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
55680 4d 75 74 65 78 41 72 72 61 79 49 6e 73 65 72 74  MutexArrayInsert
55690 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79  (BtreeMutexArray
556a0 2a 2c 20 42 74 72 65 65 2a 29 3b 0a 23 65 6c 73  *, Btree*);.#els
556b0 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  e.# define sqlit
556c0 65 33 42 74 72 65 65 45 6e 74 65 72 28 58 29 0a  e3BtreeEnter(X).
556d0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
556e0 42 74 72 65 65 4c 65 61 76 65 28 58 29 0a 23 69  BtreeLeave(X).#i
556f0 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f  fndef NDEBUG.  /
55700 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
55710 73 20 75 73 65 64 20 69 6e 73 69 64 65 20 61 73  s used inside as
55720 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
55730 73 20 6f 6e 6c 79 2e 20 2a 2f 0a 23 20 64 65 66  s only. */.# def
55740 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65  ine sqlite3Btree
55750 48 6f 6c 64 73 4d 75 74 65 78 28 58 29 20 31 0a  HoldsMutex(X) 1.
55760 23 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 20  #endif.# define 
55770 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
55780 72 43 75 72 73 6f 72 28 58 29 0a 23 20 64 65 66  rCursor(X).# def
55790 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65  ine sqlite3Btree
557a0 4c 65 61 76 65 43 75 72 73 6f 72 28 58 29 0a 23  LeaveCursor(X).#
557b0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42   define sqlite3B
557c0 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 58 29 0a  treeEnterAll(X).
557d0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
557e0 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 58 29  BtreeLeaveAll(X)
557f0 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
55800 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e    /* This routin
55810 65 20 69 73 20 75 73 65 64 20 69 6e 73 69 64 65  e is used inside
55820 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
55830 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f 0a 23 20  ents only. */.# 
55840 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74  define sqlite3Bt
55850 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78  reeHoldsAllMutex
55860 65 73 28 58 29 20 31 0a 23 65 6e 64 69 66 0a 23  es(X) 1.#endif.#
55870 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42   define sqlite3B
55880 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e  treeMutexArrayEn
55890 74 65 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20  ter(X).# define 
558a0 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65  sqlite3BtreeMute
558b0 78 41 72 72 61 79 4c 65 61 76 65 28 58 29 0a 23  xArrayLeave(X).#
558c0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42   define sqlite3B
558d0 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e  treeMutexArrayIn
558e0 73 65 72 74 28 58 2c 59 29 0a 23 65 6e 64 69 66  sert(X,Y).#endif
558f0 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 42 54  ...#endif /* _BT
55900 52 45 45 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  REE_H_ */../****
55910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
55920 66 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a  f btree.h ******
55930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
55960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
55970 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
55980 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74  eft off in sqlit
55990 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
559a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
559b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
559c0 64 65 20 76 64 62 65 2e 68 20 69 6e 20 74 68 65  de vdbe.h in the
559d0 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74   middle of sqlit
559e0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
559f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
55a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
55a10 20 66 69 6c 65 20 76 64 62 65 2e 68 20 2a 2a 2a   file vdbe.h ***
55a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
55a50 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
55a60 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
55a70 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
55a80 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
55a90 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
55aa0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
55ab0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
55ac0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
55ad0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
55ae0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
55af0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
55b00 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
55b10 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
55b20 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
55b30 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
55b40 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
55b50 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
55b60 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
55b70 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
55b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55bc0 2a 0a 2a 2a 20 48 65 61 64 65 72 20 66 69 6c 65  *.** Header file
55bd0 20 66 6f 72 20 74 68 65 20 56 69 72 74 75 61 6c   for the Virtual
55be0 20 44 61 74 61 42 61 73 65 20 45 6e 67 69 6e 65   DataBase Engine
55bf0 20 28 56 44 42 45 29 0a 2a 2a 0a 2a 2a 20 54 68   (VDBE).**.** Th
55c00 69 73 20 68 65 61 64 65 72 20 64 65 66 69 6e 65  is header define
55c10 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20  s the interface 
55c20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 20 64  to the virtual d
55c30 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 0a 2a  atabase engine.*
55c40 2a 20 6f 72 20 56 44 42 45 2e 20 20 54 68 65 20  * or VDBE.  The 
55c50 56 44 42 45 20 69 6d 70 6c 65 6d 65 6e 74 73 20  VDBE implements 
55c60 61 6e 20 61 62 73 74 72 61 63 74 20 6d 61 63 68  an abstract mach
55c70 69 6e 65 20 74 68 61 74 20 72 75 6e 73 20 61 0a  ine that runs a.
55c80 2a 2a 20 73 69 6d 70 6c 65 20 70 72 6f 67 72 61  ** simple progra
55c90 6d 20 74 6f 20 61 63 63 65 73 73 20 61 6e 64 20  m to access and 
55ca0 6d 6f 64 69 66 79 20 74 68 65 20 75 6e 64 65 72  modify the under
55cb0 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 2e 0a  lying database..
55cc0 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 2e  **.** $Id: vdbe.
55cd0 68 2c 76 20 31 2e 31 33 39 20 32 30 30 38 2f 31  h,v 1.139 2008/1
55ce0 30 2f 33 31 20 31 30 3a 35 33 3a 32 33 20 64 61  0/31 10:53:23 da
55cf0 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a  nielk1977 Exp $.
55d00 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49  */.#ifndef _SQLI
55d10 54 45 5f 56 44 42 45 5f 48 5f 0a 23 64 65 66 69  TE_VDBE_H_.#defi
55d20 6e 65 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f  ne _SQLITE_VDBE_
55d30 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 69 6e 67  H_../*.** A sing
55d40 6c 65 20 56 44 42 45 20 69 73 20 61 6e 20 6f 70  le VDBE is an op
55d50 61 71 75 65 20 73 74 72 75 63 74 75 72 65 20 6e  aque structure n
55d60 61 6d 65 64 20 22 56 64 62 65 22 2e 20 20 4f 6e  amed "Vdbe".  On
55d70 6c 79 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69  ly routines.** i
55d80 6e 20 74 68 65 20 73 6f 75 72 63 65 20 66 69 6c  n the source fil
55d90 65 20 73 71 6c 69 74 65 56 64 62 65 2e 63 20 61  e sqliteVdbe.c a
55da0 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 73 65  re allowed to se
55db0 65 20 74 68 65 20 69 6e 73 69 64 65 73 0a 2a 2a  e the insides.**
55dc0 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
55dd0 72 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  re..*/.typedef s
55de0 74 72 75 63 74 20 56 64 62 65 20 56 64 62 65 3b  truct Vdbe Vdbe;
55df0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65  ../*.** The name
55e00 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  s of the followi
55e10 6e 67 20 74 79 70 65 73 20 64 65 63 6c 61 72 65  ng types declare
55e20 64 20 69 6e 20 76 64 62 65 49 6e 74 2e 68 20 61  d in vdbeInt.h a
55e30 72 65 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 66  re required.** f
55e40 6f 72 20 74 68 65 20 56 64 62 65 4f 70 20 64 65  or the VdbeOp de
55e50 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70  finition..*/.typ
55e60 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65  edef struct Vdbe
55e70 46 75 6e 63 20 56 64 62 65 46 75 6e 63 3b 0a 74  Func VdbeFunc;.t
55e80 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65  ypedef struct Me
55e90 6d 20 4d 65 6d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20  m Mem;../*.** A 
55ea0 73 69 6e 67 6c 65 20 69 6e 73 74 72 75 63 74 69  single instructi
55eb0 6f 6e 20 6f 66 20 74 68 65 20 76 69 72 74 75 61  on of the virtua
55ec0 6c 20 6d 61 63 68 69 6e 65 20 68 61 73 20 61 6e  l machine has an
55ed0 20 6f 70 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 61   opcode.** and a
55ee0 73 20 6d 61 6e 79 20 61 73 20 74 68 72 65 65 20  s many as three 
55ef0 6f 70 65 72 61 6e 64 73 2e 20 20 54 68 65 20 69  operands.  The i
55f00 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 72 65  nstruction is re
55f10 63 6f 72 64 65 64 0a 2a 2a 20 61 73 20 61 6e 20  corded.** as an 
55f20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
55f30 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
55f40 75 72 65 3a 0a 2a 2f 0a 73 74 72 75 63 74 20 56  ure:.*/.struct V
55f50 64 62 65 4f 70 20 7b 0a 20 20 75 38 20 6f 70 63  dbeOp {.  u8 opc
55f60 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ode;          /*
55f70 20 57 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20   What operation 
55f80 74 6f 20 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20  to perform */.  
55f90 73 69 67 6e 65 64 20 63 68 61 72 20 70 34 74 79  signed char p4ty
55fa0 70 65 3b 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68  pe; /* One of th
55fb0 65 20 50 34 5f 78 78 78 20 63 6f 6e 73 74 61 6e  e P4_xxx constan
55fc0 74 73 20 66 6f 72 20 70 34 20 2a 2f 0a 20 20 75  ts for p4 */.  u
55fd0 38 20 6f 70 66 6c 61 67 73 3b 20 20 20 20 20 20  8 opflags;      
55fe0 20 20 20 2f 2a 20 4e 6f 74 20 63 75 72 72 65 6e     /* Not curren
55ff0 74 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20 75 38  tly used */.  u8
56000 20 70 35 3b 20 20 20 20 20 20 20 20 20 20 20 20   p5;            
56010 20 20 2f 2a 20 46 69 66 74 68 20 70 61 72 61 6d    /* Fifth param
56020 65 74 65 72 20 69 73 20 61 6e 20 75 6e 73 69 67  eter is an unsig
56030 6e 65 64 20 63 68 61 72 61 63 74 65 72 20 2a 2f  ned character */
56040 0a 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20  .  int p1;      
56050 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
56060 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74  operand */.  int
56070 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20   p2;            
56080 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 61 6d   /* Second param
56090 65 74 65 72 20 28 6f 66 74 65 6e 20 74 68 65 20  eter (often the 
560a0 6a 75 6d 70 20 64 65 73 74 69 6e 61 74 69 6f 6e  jump destination
560b0 29 20 2a 2f 0a 20 20 69 6e 74 20 70 33 3b 20 20  ) */.  int p3;  
560c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
560d0 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65  e third paramete
560e0 72 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 20  r */.  union {  
560f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6f             /* fo
56100 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 2a 2f  rth parameter */
56110 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20  .    int i;     
56120 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
56130 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 66 20  nteger value if 
56140 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32  p4type==P4_INT32
56150 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 3b   */.    void *p;
56160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
56170 2a 20 47 65 6e 65 72 69 63 20 70 6f 69 6e 74 65  * Generic pointe
56180 72 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a  r */.    char *z
56190 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
561a0 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61  /* Pointer to da
561b0 74 61 20 66 6f 72 20 73 74 72 69 6e 67 20 28 63  ta for string (c
561c0 68 61 72 20 61 72 72 61 79 29 20 74 79 70 65 73  har array) types
561d0 20 2a 2f 0a 20 20 20 20 69 36 34 20 2a 70 49 36   */.    i64 *pI6
561e0 34 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  4;             /
561f0 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79  * Used when p4ty
56200 70 65 20 69 73 20 50 34 5f 49 4e 54 36 34 20 2a  pe is P4_INT64 *
56210 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 2a 70 52  /.    double *pR
56220 65 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  eal;         /* 
56230 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65  Used when p4type
56240 20 69 73 20 50 34 5f 52 45 41 4c 20 2a 2f 0a 20   is P4_REAL */. 
56250 20 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e     FuncDef *pFun
56260 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65  c;        /* Use
56270 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73  d when p4type is
56280 20 50 34 5f 46 55 4e 43 44 45 46 20 2a 2f 0a 20   P4_FUNCDEF */. 
56290 20 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64     VdbeFunc *pVd
562a0 62 65 46 75 6e 63 3b 20 20 20 2f 2a 20 55 73 65  beFunc;   /* Use
562b0 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73  d when p4type is
562c0 20 50 34 5f 56 44 42 45 46 55 4e 43 20 2a 2f 0a   P4_VDBEFUNC */.
562d0 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
562e0 6c 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73  ll;        /* Us
562f0 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69  ed when p4type i
56300 73 20 50 34 5f 43 4f 4c 4c 53 45 51 20 2a 2f 0a  s P4_COLLSEQ */.
56310 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20      Mem *pMem;  
56320 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
56330 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69  ed when p4type i
56340 73 20 50 34 5f 4d 45 4d 20 2a 2f 0a 20 20 20 20  s P4_MEM */.    
56350 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
56360 74 61 62 3b 20 20 20 2f 2a 20 55 73 65 64 20 77  tab;   /* Used w
56370 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34  hen p4type is P4
56380 5f 56 54 41 42 20 2a 2f 0a 20 20 20 20 4b 65 79  _VTAB */.    Key
56390 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20  Info *pKeyInfo; 
563a0 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e      /* Used when
563b0 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 4b 45   p4type is P4_KE
563c0 59 49 4e 46 4f 20 2a 2f 0a 20 20 20 20 69 6e 74  YINFO */.    int
563d0 20 2a 61 69 3b 20 20 20 20 20 20 20 20 20 20 20   *ai;           
563e0 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e      /* Used when
563f0 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 49 4e   p4type is P4_IN
56400 54 41 52 52 41 59 20 2a 2f 0a 20 20 7d 20 70 34  TARRAY */.  } p4
56410 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
56420 44 45 42 55 47 0a 20 20 63 68 61 72 20 2a 7a 43  DEBUG.  char *zC
56430 6f 6d 6d 65 6e 74 3b 20 20 20 20 20 20 20 20 20  omment;         
56440 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 74 6f 20 69   /* Comment to i
56450 6d 70 72 6f 76 65 20 72 65 61 64 61 62 69 6c 69  mprove readabili
56460 74 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66  ty */.#endif.#if
56470 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45  def VDBE_PROFILE
56480 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20  .  int cnt;     
56490 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
564a0 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74  umber of times t
564b0 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
564c0 77 61 73 20 65 78 65 63 75 74 65 64 20 2a 2f 0a  was executed */.
564d0 20 20 75 36 34 20 63 79 63 6c 65 73 3b 20 20 20    u64 cycles;   
564e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f             /* To
564f0 74 61 6c 20 74 69 6d 65 20 73 70 65 6e 74 20 65  tal time spent e
56500 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 69 6e  xecuting this in
56510 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e  struction */.#en
56520 64 69 66 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73  dif.};.typedef s
56530 74 72 75 63 74 20 56 64 62 65 4f 70 20 56 64 62  truct VdbeOp Vdb
56540 65 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 6d  eOp;../*.** A sm
56550 61 6c 6c 65 72 20 76 65 72 73 69 6f 6e 20 6f 66  aller version of
56560 20 56 64 62 65 4f 70 20 75 73 65 64 20 66 6f 72   VdbeOp used for
56570 20 74 68 65 20 56 64 62 65 41 64 64 4f 70 4c 69   the VdbeAddOpLi
56580 73 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 65  st() function be
56590 63 61 75 73 65 0a 2a 2a 20 69 74 20 74 61 6b 65  cause.** it take
565a0 73 20 75 70 20 6c 65 73 73 20 73 70 61 63 65 2e  s up less space.
565b0 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 4f  .*/.struct VdbeO
565c0 70 4c 69 73 74 20 7b 0a 20 20 75 38 20 6f 70 63  pList {.  u8 opc
565d0 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ode;          /*
565e0 20 57 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20   What operation 
565f0 74 6f 20 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20  to perform */.  
56600 73 69 67 6e 65 64 20 63 68 61 72 20 70 31 3b 20  signed char p1; 
56610 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6f 70 65      /* First ope
56620 72 61 6e 64 20 2a 2f 0a 20 20 73 69 67 6e 65 64  rand */.  signed
56630 20 63 68 61 72 20 70 32 3b 20 20 20 20 20 2f 2a   char p2;     /*
56640 20 53 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   Second paramete
56650 72 20 28 6f 66 74 65 6e 20 74 68 65 20 6a 75 6d  r (often the jum
56660 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 29 20 2a  p destination) *
56670 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72 20  /.  signed char 
56680 70 33 3b 20 20 20 20 20 2f 2a 20 54 68 69 72 64  p3;     /* Third
56690 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 7d 3b   parameter */.};
566a0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
566b0 56 64 62 65 4f 70 4c 69 73 74 20 56 64 62 65 4f  VdbeOpList VdbeO
566c0 70 4c 69 73 74 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c  pList;../*.** Al
566d0 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66 20  lowed values of 
566e0 56 64 62 65 4f 70 2e 70 33 74 79 70 65 0a 2a 2f  VdbeOp.p3type.*/
566f0 0a 23 64 65 66 69 6e 65 20 50 34 5f 4e 4f 54 55  .#define P4_NOTU
56700 53 45 44 20 20 20 20 30 20 20 20 2f 2a 20 54 68  SED    0   /* Th
56710 65 20 50 34 20 70 61 72 61 6d 65 74 65 72 20 69  e P4 parameter i
56720 73 20 6e 6f 74 20 75 73 65 64 20 2a 2f 0a 23 64  s not used */.#d
56730 65 66 69 6e 65 20 50 34 5f 44 59 4e 41 4d 49 43  efine P4_DYNAMIC
56740 20 20 28 2d 31 29 20 20 2f 2a 20 50 6f 69 6e 74    (-1)  /* Point
56750 65 72 20 74 6f 20 61 20 73 74 72 69 6e 67 20 6f  er to a string o
56760 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c  btained from sql
56770 69 74 65 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23  iteMalloc() */.#
56780 64 65 66 69 6e 65 20 50 34 5f 53 54 41 54 49 43  define P4_STATIC
56790 20 20 20 28 2d 32 29 20 20 2f 2a 20 50 6f 69 6e     (-2)  /* Poin
567a0 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20  ter to a static 
567b0 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e  string */.#defin
567c0 65 20 50 34 5f 43 4f 4c 4c 53 45 51 20 20 28 2d  e P4_COLLSEQ  (-
567d0 34 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70  4)  /* P4 is a p
567e0 6f 69 6e 74 65 72 20 74 6f 20 61 20 43 6f 6c 6c  ointer to a Coll
567f0 53 65 71 20 73 74 72 75 63 74 75 72 65 20 2a 2f  Seq structure */
56800 0a 23 64 65 66 69 6e 65 20 50 34 5f 46 55 4e 43  .#define P4_FUNC
56810 44 45 46 20 20 28 2d 35 29 20 20 2f 2a 20 50 34  DEF  (-5)  /* P4
56820 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
56830 20 61 20 46 75 6e 63 44 65 66 20 73 74 72 75 63   a FuncDef struc
56840 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ture */.#define 
56850 50 34 5f 4b 45 59 49 4e 46 4f 20 20 28 2d 36 29  P4_KEYINFO  (-6)
56860 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69    /* P4 is a poi
56870 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66  nter to a KeyInf
56880 6f 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23  o structure */.#
56890 64 65 66 69 6e 65 20 50 34 5f 56 44 42 45 46 55  define P4_VDBEFU
568a0 4e 43 20 28 2d 37 29 20 20 2f 2a 20 50 34 20 69  NC (-7)  /* P4 i
568b0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
568c0 20 56 64 62 65 46 75 6e 63 20 73 74 72 75 63 74   VdbeFunc struct
568d0 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ure */.#define P
568e0 34 5f 4d 45 4d 20 20 20 20 20 20 28 2d 38 29 20  4_MEM      (-8) 
568f0 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e   /* P4 is a poin
56900 74 65 72 20 74 6f 20 61 20 4d 65 6d 2a 20 20 20  ter to a Mem*   
56910 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64   structure */.#d
56920 65 66 69 6e 65 20 50 34 5f 54 52 41 4e 53 49 45  efine P4_TRANSIE
56930 4e 54 20 28 2d 39 29 20 2f 2a 20 50 34 20 69 73  NT (-9) /* P4 is
56940 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
56950 74 72 61 6e 73 69 65 6e 74 20 73 74 72 69 6e 67  transient string
56960 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 56   */.#define P4_V
56970 54 41 42 20 20 20 20 20 28 2d 31 30 29 20 2f 2a  TAB     (-10) /*
56980 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72   P4 is a pointer
56990 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 76   to an sqlite3_v
569a0 74 61 62 20 73 74 72 75 63 74 75 72 65 20 2a 2f  tab structure */
569b0 0a 23 64 65 66 69 6e 65 20 50 34 5f 4d 50 52 49  .#define P4_MPRI
569c0 4e 54 46 20 20 28 2d 31 31 29 20 2f 2a 20 50 34  NTF  (-11) /* P4
569d0 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 62 74   is a string obt
569e0 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
569f0 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 2a 2f 0a  e3_mprintf() */.
56a00 23 64 65 66 69 6e 65 20 50 34 5f 52 45 41 4c 20  #define P4_REAL 
56a10 20 20 20 20 28 2d 31 32 29 20 2f 2a 20 50 34 20      (-12) /* P4 
56a20 69 73 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61  is a 64-bit floa
56a30 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65  ting point value
56a40 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 49   */.#define P4_I
56a50 4e 54 36 34 20 20 20 20 28 2d 31 33 29 20 2f 2a  NT64    (-13) /*
56a60 20 50 34 20 69 73 20 61 20 36 34 2d 62 69 74 20   P4 is a 64-bit 
56a70 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a  signed integer *
56a80 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 49 4e 54  /.#define P4_INT
56a90 33 32 20 20 20 20 28 2d 31 34 29 20 2f 2a 20 50  32    (-14) /* P
56aa0 34 20 69 73 20 61 20 33 32 2d 62 69 74 20 73 69  4 is a 32-bit si
56ab0 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a  gned integer */.
56ac0 23 64 65 66 69 6e 65 20 50 34 5f 49 4e 54 41 52  #define P4_INTAR
56ad0 52 41 59 20 28 2d 31 35 29 20 2f 2a 20 50 34 20  RAY (-15) /* P4 
56ae0 69 73 20 61 20 76 65 63 74 6f 72 20 6f 66 20 33  is a vector of 3
56af0 32 2d 62 69 74 20 69 6e 74 65 67 65 72 73 20 2a  2-bit integers *
56b00 2f 0a 0a 2f 2a 20 57 68 65 6e 20 61 64 64 69 6e  /../* When addin
56b10 67 20 61 20 50 34 20 61 72 67 75 6d 65 6e 74 20  g a P4 argument 
56b20 75 73 69 6e 67 20 50 34 5f 4b 45 59 49 4e 46 4f  using P4_KEYINFO
56b30 2c 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  , a copy of the 
56b40 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
56b50 65 0a 2a 2a 20 69 73 20 6d 61 64 65 2e 20 20 54  e.** is made.  T
56b60 68 61 74 20 63 6f 70 79 20 69 73 20 66 72 65 65  hat copy is free
56b70 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20  d when the Vdbe 
56b80 69 73 20 66 69 6e 61 6c 69 7a 65 64 2e 20 20 42  is finalized.  B
56b90 75 74 20 69 66 20 74 68 65 0a 2a 2a 20 61 72 67  ut if the.** arg
56ba0 75 6d 65 6e 74 20 69 73 20 50 34 5f 4b 45 59 49  ument is P4_KEYI
56bb0 4e 46 4f 5f 48 41 4e 44 4f 46 46 2c 20 74 68 65  NFO_HANDOFF, the
56bc0 20 70 61 73 73 65 64 20 69 6e 20 70 6f 69 6e 74   passed in point
56bd0 65 72 20 69 73 20 75 73 65 64 2e 20 20 49 74 20  er is used.  It 
56be0 73 74 69 6c 6c 0a 2a 2a 20 67 65 74 73 20 66 72  still.** gets fr
56bf0 65 65 64 20 77 68 65 6e 20 74 68 65 20 56 64 62  eed when the Vdb
56c00 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 20 73  e is finalized s
56c10 6f 20 69 74 20 73 74 69 6c 6c 20 73 68 6f 75 6c  o it still shoul
56c20 64 20 62 65 20 6f 62 74 61 69 6e 65 64 0a 2a 2a  d be obtained.**
56c30 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 73   from a single s
56c40 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20  qliteMalloc().  
56c50 42 75 74 20 6e 6f 20 63 6f 70 79 20 69 73 20 6d  But no copy is m
56c60 61 64 65 20 61 6e 64 20 74 68 65 20 63 61 6c 6c  ade and the call
56c70 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  ing.** function 
56c80 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a 20 74 72 79  should *not* try
56c90 20 74 6f 20 66 72 65 65 20 74 68 65 20 4b 65 79   to free the Key
56ca0 49 6e 66 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  Info..*/.#define
56cb0 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
56cc0 4f 46 46 20 28 2d 31 36 29 0a 23 64 65 66 69 6e  OFF (-16).#defin
56cd0 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 41  e P4_KEYINFO_STA
56ce0 54 49 43 20 20 28 2d 31 37 29 0a 0a 2f 2a 0a 2a  TIC  (-17)../*.*
56cf0 2a 20 54 68 65 20 56 64 62 65 2e 61 43 6f 6c 4e  * The Vdbe.aColN
56d00 61 6d 65 20 61 72 72 61 79 20 63 6f 6e 74 61 69  ame array contai
56d10 6e 73 20 35 6e 20 4d 65 6d 20 73 74 72 75 63 74  ns 5n Mem struct
56d20 75 72 65 73 2c 20 77 68 65 72 65 20 6e 20 69 73  ures, where n is
56d30 20 74 68 65 20 0a 2a 2a 20 6e 75 6d 62 65 72 20   the .** number 
56d40 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 61  of columns of da
56d50 74 61 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  ta returned by t
56d60 68 65 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f  he statement..*/
56d70 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45  .#define COLNAME
56d80 5f 4e 41 4d 45 20 20 20 20 20 30 0a 23 64 65 66  _NAME     0.#def
56d90 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c  ine COLNAME_DECL
56da0 54 59 50 45 20 31 0a 23 64 65 66 69 6e 65 20 43  TYPE 1.#define C
56db0 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 20  OLNAME_DATABASE 
56dc0 32 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d  2.#define COLNAM
56dd0 45 5f 54 41 42 4c 45 20 20 20 20 33 0a 23 64 65  E_TABLE    3.#de
56de0 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c  fine COLNAME_COL
56df0 55 4d 4e 20 20 20 34 0a 23 69 66 64 65 66 20 53  UMN   4.#ifdef S
56e00 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c  QLITE_ENABLE_COL
56e10 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 23 20 64  UMN_METADATA.# d
56e20 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20  efine COLNAME_N 
56e30 20 20 20 20 20 20 20 35 20 20 20 20 20 20 2f 2a         5      /*
56e40 20 4e 75 6d 62 65 72 20 6f 66 20 43 4f 4c 4e 41   Number of COLNA
56e50 4d 45 5f 78 78 78 20 73 79 6d 62 6f 6c 73 20 2a  ME_xxx symbols *
56e60 2f 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20  /.#else.# ifdef 
56e70 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c  SQLITE_OMIT_DECL
56e80 54 59 50 45 0a 23 20 20 20 64 65 66 69 6e 65 20  TYPE.#   define 
56e90 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20 20 31  COLNAME_N      1
56ea0 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 6f        /* Store o
56eb0 6e 6c 79 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a  nly the name */.
56ec0 23 20 65 6c 73 65 0a 23 20 20 20 64 65 66 69 6e  # else.#   defin
56ed0 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20  e COLNAME_N     
56ee0 20 32 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65   2      /* Store
56ef0 20 74 68 65 20 6e 61 6d 65 20 61 6e 64 20 64 65   the name and de
56f00 63 6c 74 79 70 65 20 2a 2f 0a 23 20 65 6e 64 69  cltype */.# endi
56f10 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  f.#endif../*.** 
56f20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61  The following ma
56f30 63 72 6f 20 63 6f 6e 76 65 72 74 73 20 61 20 72  cro converts a r
56f40 65 6c 61 74 69 76 65 20 61 64 64 72 65 73 73 20  elative address 
56f50 69 6e 20 74 68 65 20 70 32 20 66 69 65 6c 64 0a  in the p2 field.
56f60 2a 2a 20 6f 66 20 61 20 56 64 62 65 4f 70 20 73  ** of a VdbeOp s
56f70 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 61 20  tructure into a 
56f80 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 20  negative number 
56f90 73 6f 20 74 68 61 74 20 0a 2a 2a 20 73 71 6c 69  so that .** sqli
56fa0 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
56fb0 28 29 20 6b 6e 6f 77 73 20 74 68 61 74 20 74 68  () knows that th
56fc0 65 20 61 64 64 72 65 73 73 20 69 73 20 72 65 6c  e address is rel
56fd0 61 74 69 76 65 2e 20 20 43 61 6c 6c 69 6e 67 0a  ative.  Calling.
56fe0 2a 2a 20 74 68 65 20 6d 61 63 72 6f 20 61 67 61  ** the macro aga
56ff0 69 6e 20 72 65 73 74 6f 72 65 73 20 74 68 65 20  in restores the 
57000 61 64 64 72 65 73 73 2e 0a 2a 2f 0a 23 64 65 66  address..*/.#def
57010 69 6e 65 20 41 44 44 52 28 58 29 20 20 28 2d 31  ine ADDR(X)  (-1
57020 2d 28 58 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  -(X))../*.** The
57030 20 6d 61 6b 65 66 69 6c 65 20 73 63 61 6e 73 20   makefile scans 
57040 74 68 65 20 76 64 62 65 2e 63 20 73 6f 75 72 63  the vdbe.c sourc
57050 65 20 66 69 6c 65 20 61 6e 64 20 63 72 65 61 74  e file and creat
57060 65 73 20 74 68 65 20 22 6f 70 63 6f 64 65 73 2e  es the "opcodes.
57070 68 22 0a 2a 2a 20 68 65 61 64 65 72 20 66 69 6c  h".** header fil
57080 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 61  e that defines a
57090 20 6e 75 6d 62 65 72 20 66 6f 72 20 65 61 63 68   number for each
570a0 20 6f 70 63 6f 64 65 20 75 73 65 64 20 62 79 20   opcode used by 
570b0 74 68 65 20 56 44 42 45 2e 0a 2a 2f 0a 2f 2a 2a  the VDBE..*/./**
570c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
570d0 6c 75 64 65 20 6f 70 63 6f 64 65 73 2e 68 20 69  lude opcodes.h i
570e0 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
570f0 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  vdbe.h *********
57100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
57110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
57120 69 6e 20 66 69 6c 65 20 6f 70 63 6f 64 65 73 2e  in file opcodes.
57130 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
57140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20  ***********/./* 
57160 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65  Automatically ge
57170 6e 65 72 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74  nerated.  Do not
57180 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 53 65 65 20   edit */./* See 
57190 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77  the mkopcodeh.aw
571a0 6b 20 73 63 72 69 70 74 20 66 6f 72 20 64 65 74  k script for det
571b0 61 69 6c 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ails */.#define 
571c0 4f 50 5f 56 4e 65 78 74 20 20 20 20 20 20 20 20  OP_VNext        
571d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
571e0 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e          1.#defin
571f0 65 20 4f 50 5f 41 66 66 69 6e 69 74 79 20 20 20  e OP_Affinity   
57200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57210 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 66            2.#def
57220 69 6e 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 20 20  ine OP_Column   
57230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57240 20 20 20 20 20 20 20 20 20 20 20 20 33 0a 23 64              3.#d
57250 65 66 69 6e 65 20 4f 50 5f 53 65 74 43 6f 6f 6b  efine OP_SetCook
57260 69 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ie              
57270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 0a                4.
57280 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 61 6c 20  #define OP_Real 
57290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
572a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
572b0 36 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  6   /* same as T
572c0 4b 5f 46 4c 4f 41 54 20 20 20 20 2a 2f 0a 23 64  K_FLOAT    */.#d
572d0 65 66 69 6e 65 20 4f 50 5f 53 65 71 75 65 6e 63  efine OP_Sequenc
572e0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
572f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a                5.
57300 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f 76 65 47  #define OP_MoveG
57310 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
57320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57330 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 47 65 20  6.#define OP_Ge 
57340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57360 20 37 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   72   /* same as
57370 20 54 4b 5f 47 45 20 20 20 20 20 20 20 2a 2f 0a   TK_GE       */.
57380 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 4b 65  #define OP_RowKe
57390 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y               
573a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
573b0 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 43 6f  7.#define OP_SCo
573c0 70 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  py              
573d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
573e0 20 20 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 45    8.#define OP_E
573f0 71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  q               
57400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57410 20 20 20 36 38 20 20 20 2f 2a 20 73 61 6d 65 20     68   /* same 
57420 61 73 20 54 4b 5f 45 51 20 20 20 20 20 20 20 2a  as TK_EQ       *
57430 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65  /.#define OP_Ope
57440 6e 57 72 69 74 65 20 20 20 20 20 20 20 20 20 20  nWrite          
57450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57460 20 20 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e    9.#define OP_N
57470 6f 74 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20  otNull          
57480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57490 20 20 20 36 36 20 20 20 2f 2a 20 73 61 6d 65 20     66   /* same 
574a0 61 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 2a  as TK_NOTNULL  *
574b0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 20  /.#define OP_If 
574c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
574d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
574e0 20 31 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54   10.#define OP_T
574f0 6f 49 6e 74 20 20 20 20 20 20 20 20 20 20 20 20  oInt            
57500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57510 20 20 31 34 32 20 20 20 2f 2a 20 73 61 6d 65 20    142   /* same 
57520 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 20 20 20 2a  as TK_TO_INT   *
57530 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 74 72  /.#define OP_Str
57540 69 6e 67 38 20 20 20 20 20 20 20 20 20 20 20 20  ing8            
57550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57560 20 38 38 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   88   /* same as
57570 20 54 4b 5f 53 54 52 49 4e 47 20 20 20 2a 2f 0a   TK_STRING   */.
57580 23 64 65 66 69 6e 65 20 4f 50 5f 56 52 6f 77 69  #define OP_VRowi
57590 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
575a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
575b0 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6c  1.#define OP_Col
575c0 6c 53 65 71 20 20 20 20 20 20 20 20 20 20 20 20  lSeq            
575d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
575e0 20 31 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f   12.#define OP_O
575f0 70 65 6e 52 65 61 64 20 20 20 20 20 20 20 20 20  penRead         
57600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57610 20 20 20 31 33 0a 23 64 65 66 69 6e 65 20 4f 50     13.#define OP
57620 5f 45 78 70 69 72 65 20 20 20 20 20 20 20 20 20  _Expire         
57630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57640 20 20 20 20 20 31 34 0a 23 64 65 66 69 6e 65 20       14.#define 
57650 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 20 20 20  OP_AutoCommit   
57660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57670 20 20 20 20 20 20 20 31 35 0a 23 64 65 66 69 6e         15.#defin
57680 65 20 4f 50 5f 47 74 20 20 20 20 20 20 20 20 20  e OP_Gt         
57690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
576a0 20 20 20 20 20 20 20 20 20 36 39 20 20 20 2f 2a           69   /*
576b0 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 54 20 20   same as TK_GT  
576c0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
576d0 4f 50 5f 50 61 67 65 63 6f 75 6e 74 20 20 20 20  OP_Pagecount    
576e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
576f0 20 20 20 20 20 20 20 31 37 0a 23 64 65 66 69 6e         17.#defin
57700 65 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b  e OP_IntegrityCk
57710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57720 20 20 20 20 20 20 20 20 20 31 38 0a 23 64 65 66           18.#def
57730 69 6e 65 20 4f 50 5f 53 6f 72 74 20 20 20 20 20  ine OP_Sort     
57740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57750 20 20 20 20 20 20 20 20 20 20 20 31 39 0a 23 64             19.#d
57760 65 66 69 6e 65 20 4f 50 5f 43 6f 70 79 20 20 20  efine OP_Copy   
57770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57780 20 20 20 20 20 20 20 20 20 20 20 20 20 32 30 0a               20.
57790 23 64 65 66 69 6e 65 20 4f 50 5f 54 72 61 63 65  #define OP_Trace
577a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
577b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
577c0 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 75 6e  1.#define OP_Fun
577d0 63 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20  ction           
577e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
577f0 20 32 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49   22.#define OP_I
57800 66 4e 65 67 20 20 20 20 20 20 20 20 20 20 20 20  fNeg            
57810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57820 20 20 20 32 33 0a 23 64 65 66 69 6e 65 20 4f 50     23.#define OP
57830 5f 41 6e 64 20 20 20 20 20 20 20 20 20 20 20 20  _And            
57840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57850 20 20 20 20 20 36 31 20 20 20 2f 2a 20 73 61 6d       61   /* sam
57860 65 20 61 73 20 54 4b 5f 41 4e 44 20 20 20 20 20  e as TK_AND     
57870 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53   */.#define OP_S
57880 75 62 74 72 61 63 74 20 20 20 20 20 20 20 20 20  ubtract         
57890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
578a0 20 20 20 37 39 20 20 20 2f 2a 20 73 61 6d 65 20     79   /* same 
578b0 61 73 20 54 4b 5f 4d 49 4e 55 53 20 20 20 20 2a  as TK_MINUS    *
578c0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 6f  /.#define OP_Noo
578d0 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p               
578e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
578f0 20 32 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52   24.#define OP_R
57900 65 74 75 72 6e 20 20 20 20 20 20 20 20 20 20 20  eturn           
57910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57920 20 20 20 32 35 0a 23 64 65 66 69 6e 65 20 4f 50     25.#define OP
57930 5f 52 65 6d 61 69 6e 64 65 72 20 20 20 20 20 20  _Remainder      
57940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57950 20 20 20 20 20 38 32 20 20 20 2f 2a 20 73 61 6d       82   /* sam
57960 65 20 61 73 20 54 4b 5f 52 45 4d 20 20 20 20 20  e as TK_REM     
57970 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e   */.#define OP_N
57980 65 77 52 6f 77 69 64 20 20 20 20 20 20 20 20 20  ewRowid         
57990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
579a0 20 20 20 32 36 0a 23 64 65 66 69 6e 65 20 4f 50     26.#define OP
579b0 5f 4d 75 6c 74 69 70 6c 79 20 20 20 20 20 20 20  _Multiply       
579c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
579d0 20 20 20 20 20 38 30 20 20 20 2f 2a 20 73 61 6d       80   /* sam
579e0 65 20 61 73 20 54 4b 5f 53 54 41 52 20 20 20 20  e as TK_STAR    
579f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56   */.#define OP_V
57a00 61 72 69 61 62 6c 65 20 20 20 20 20 20 20 20 20  ariable         
57a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57a20 20 20 20 32 37 0a 23 64 65 66 69 6e 65 20 4f 50     27.#define OP
57a30 5f 53 74 72 69 6e 67 20 20 20 20 20 20 20 20 20  _String         
57a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57a50 20 20 20 20 20 32 38 0a 23 64 65 66 69 6e 65 20       28.#define 
57a60 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 20  OP_RealAffinity 
57a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57a80 20 20 20 20 20 20 20 32 39 0a 23 64 65 66 69 6e         29.#defin
57a90 65 20 4f 50 5f 56 52 65 6e 61 6d 65 20 20 20 20  e OP_VRename    
57aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57ab0 20 20 20 20 20 20 20 20 20 33 30 0a 23 64 65 66           30.#def
57ac0 69 6e 65 20 4f 50 5f 50 61 72 73 65 53 63 68 65  ine OP_ParseSche
57ad0 6d 61 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ma              
57ae0 20 20 20 20 20 20 20 20 20 20 20 33 31 0a 23 64             31.#d
57af0 65 66 69 6e 65 20 4f 50 5f 56 4f 70 65 6e 20 20  efine OP_VOpen  
57b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57b10 20 20 20 20 20 20 20 20 20 20 20 20 20 33 32 0a               32.
57b20 23 64 65 66 69 6e 65 20 4f 50 5f 43 6c 6f 73 65  #define OP_Close
57b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
57b50 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 72 65  3.#define OP_Cre
57b60 61 74 65 49 6e 64 65 78 20 20 20 20 20 20 20 20  ateIndex        
57b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57b80 20 33 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49   34.#define OP_I
57b90 73 55 6e 69 71 75 65 20 20 20 20 20 20 20 20 20  sUnique         
57ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57bb0 20 20 20 33 35 0a 23 64 65 66 69 6e 65 20 4f 50     35.#define OP
57bc0 5f 4e 6f 74 46 6f 75 6e 64 20 20 20 20 20 20 20  _NotFound       
57bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57be0 20 20 20 20 20 33 36 0a 23 64 65 66 69 6e 65 20       36.#define 
57bf0 4f 50 5f 49 6e 74 36 34 20 20 20 20 20 20 20 20  OP_Int64        
57c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57c10 20 20 20 20 20 20 20 33 37 0a 23 64 65 66 69 6e         37.#defin
57c20 65 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 20 20  e OP_MustBeInt  
57c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57c40 20 20 20 20 20 20 20 20 20 33 38 0a 23 64 65 66           38.#def
57c50 69 6e 65 20 4f 50 5f 48 61 6c 74 20 20 20 20 20  ine OP_Halt     
57c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57c70 20 20 20 20 20 20 20 20 20 20 20 33 39 0a 23 64             39.#d
57c80 65 66 69 6e 65 20 4f 50 5f 52 6f 77 69 64 20 20  efine OP_Rowid  
57c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 34 30 0a               40.
57cb0 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 4c 54  #define OP_IdxLT
57cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
57ce0 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 64 64  1.#define OP_Add
57cf0 49 6d 6d 20 20 20 20 20 20 20 20 20 20 20 20 20  Imm             
57d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57d10 20 34 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53   42.#define OP_S
57d20 74 61 74 65 6d 65 6e 74 20 20 20 20 20 20 20 20  tatement        
57d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57d40 20 20 20 34 33 0a 23 64 65 66 69 6e 65 20 4f 50     43.#define OP
57d50 5f 52 6f 77 44 61 74 61 20 20 20 20 20 20 20 20  _RowData        
57d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57d70 20 20 20 20 20 34 34 0a 23 64 65 66 69 6e 65 20       44.#define 
57d80 4f 50 5f 4d 65 6d 4d 61 78 20 20 20 20 20 20 20  OP_MemMax       
57d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57da0 20 20 20 20 20 20 20 34 35 0a 23 64 65 66 69 6e         45.#defin
57db0 65 20 4f 50 5f 4f 72 20 20 20 20 20 20 20 20 20  e OP_Or         
57dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57dd0 20 20 20 20 20 20 20 20 20 36 30 20 20 20 2f 2a           60   /*
57de0 20 73 61 6d 65 20 61 73 20 54 4b 5f 4f 52 20 20   same as TK_OR  
57df0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
57e00 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 20 20 20  OP_NotExists    
57e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57e20 20 20 20 20 20 20 20 34 36 0a 23 64 65 66 69 6e         46.#defin
57e30 65 20 4f 50 5f 47 6f 73 75 62 20 20 20 20 20 20  e OP_Gosub      
57e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57e50 20 20 20 20 20 20 20 20 20 34 37 0a 23 64 65 66           47.#def
57e60 69 6e 65 20 4f 50 5f 44 69 76 69 64 65 20 20 20  ine OP_Divide   
57e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57e80 20 20 20 20 20 20 20 20 20 20 20 38 31 20 20 20             81   
57e90 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c  /* same as TK_SL
57ea0 41 53 48 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  ASH    */.#defin
57eb0 65 20 4f 50 5f 49 6e 74 65 67 65 72 20 20 20 20  e OP_Integer    
57ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57ed0 20 20 20 20 20 20 20 20 20 34 38 0a 23 64 65 66           48.#def
57ee0 69 6e 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 69 63  ine OP_ToNumeric
57ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57f00 20 20 20 20 20 20 20 20 20 20 31 34 31 20 20 20            141   
57f10 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f  /* same as TK_TO
57f20 5f 4e 55 4d 45 52 49 43 2a 2f 0a 23 64 65 66 69  _NUMERIC*/.#defi
57f30 6e 65 20 4f 50 5f 50 72 65 76 20 20 20 20 20 20  ne OP_Prev      
57f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57f50 20 20 20 20 20 20 20 20 20 20 34 39 0a 23 64 65            49.#de
57f60 66 69 6e 65 20 4f 50 5f 43 6f 6e 63 61 74 20 20  fine OP_Concat  
57f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57f80 20 20 20 20 20 20 20 20 20 20 20 20 38 33 20 20              83  
57f90 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 43   /* same as TK_C
57fa0 4f 4e 43 41 54 20 20 20 2a 2f 0a 23 64 65 66 69  ONCAT   */.#defi
57fb0 6e 65 20 4f 50 5f 42 69 74 41 6e 64 20 20 20 20  ne OP_BitAnd    
57fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57fd0 20 20 20 20 20 20 20 20 20 20 37 34 20 20 20 2f            74   /
57fe0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54  * same as TK_BIT
57ff0 41 4e 44 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  AND   */.#define
58000 20 4f 50 5f 56 43 6f 6c 75 6d 6e 20 20 20 20 20   OP_VColumn     
58010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58020 20 20 20 20 20 20 20 20 35 30 0a 23 64 65 66 69          50.#defi
58030 6e 65 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c  ne OP_CreateTabl
58040 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
58050 20 20 20 20 20 20 20 20 20 20 35 31 0a 23 64 65            51.#de
58060 66 69 6e 65 20 4f 50 5f 4c 61 73 74 20 20 20 20  fine OP_Last    
58070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58080 20 20 20 20 20 20 20 20 20 20 20 20 35 32 0a 23              52.#
58090 64 65 66 69 6e 65 20 4f 50 5f 49 73 4e 75 6c 6c  define OP_IsNull
580a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
580b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 35                65
580c0 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
580d0 5f 49 53 4e 55 4c 4c 20 20 20 2a 2f 0a 23 64 65  _ISNULL   */.#de
580e0 66 69 6e 65 20 4f 50 5f 49 6e 63 72 56 61 63 75  fine OP_IncrVacu
580f0 75 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20  um              
58100 20 20 20 20 20 20 20 20 20 20 20 20 35 33 0a 23              53.#
58110 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 52 6f 77  define OP_IdxRow
58120 69 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  id              
58130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 34                54
58140 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 68 69 66  .#define OP_Shif
58150 74 52 69 67 68 74 20 20 20 20 20 20 20 20 20 20  tRight          
58160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58170 37 37 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  77   /* same as 
58180 54 4b 5f 52 53 48 49 46 54 20 20 20 2a 2f 0a 23  TK_RSHIFT   */.#
58190 64 65 66 69 6e 65 20 4f 50 5f 52 65 73 65 74 43  define OP_ResetC
581a0 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 20  ount            
581b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 35                55
581c0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 69 66 6f  .#define OP_Fifo
581d0 57 72 69 74 65 20 20 20 20 20 20 20 20 20 20 20  Write           
581e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
581f0 35 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f  56.#define OP_Co
58200 6e 74 65 78 74 50 75 73 68 20 20 20 20 20 20 20  ntextPush       
58210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58220 20 20 35 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f    57.#define OP_
58230 59 69 65 6c 64 20 20 20 20 20 20 20 20 20 20 20  Yield           
58240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58250 20 20 20 20 35 38 0a 23 64 65 66 69 6e 65 20 4f      58.#define O
58260 50 5f 44 72 6f 70 54 72 69 67 67 65 72 20 20 20  P_DropTrigger   
58270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58280 20 20 20 20 20 20 35 39 0a 23 64 65 66 69 6e 65        59.#define
58290 20 4f 50 5f 44 72 6f 70 49 6e 64 65 78 20 20 20   OP_DropIndex   
582a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
582b0 20 20 20 20 20 20 20 20 36 32 0a 23 64 65 66 69          62.#defi
582c0 6e 65 20 4f 50 5f 49 64 78 47 45 20 20 20 20 20  ne OP_IdxGE     
582d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
582e0 20 20 20 20 20 20 20 20 20 20 36 33 0a 23 64 65            63.#de
582f0 66 69 6e 65 20 4f 50 5f 49 64 78 44 65 6c 65 74  fine OP_IdxDelet
58300 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
58310 20 20 20 20 20 20 20 20 20 20 20 20 36 34 0a 23              64.#
58320 64 65 66 69 6e 65 20 4f 50 5f 56 61 63 75 75 6d  define OP_Vacuum
58330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 33                73
58350 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f 76 65  .#define OP_Move
58360 4c 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Le              
58370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58380 38 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66  84.#define OP_If
58390 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20  Not             
583a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
583b0 20 20 38 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f    85.#define OP_
583c0 44 72 6f 70 54 61 62 6c 65 20 20 20 20 20 20 20  DropTable       
583d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
583e0 20 20 20 20 38 36 0a 23 64 65 66 69 6e 65 20 4f      86.#define O
583f0 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 20 20 20  P_MakeRecord    
58400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58410 20 20 20 20 20 20 38 39 0a 23 64 65 66 69 6e 65        89.#define
58420 20 4f 50 5f 54 6f 42 6c 6f 62 20 20 20 20 20 20   OP_ToBlob      
58430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58440 20 20 20 20 20 20 20 31 34 30 20 20 20 2f 2a 20         140   /* 
58450 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 42 4c  same as TK_TO_BL
58460 4f 42 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  OB  */.#define O
58470 50 5f 52 65 73 75 6c 74 52 6f 77 20 20 20 20 20  P_ResultRow     
58480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58490 20 20 20 20 20 20 39 30 0a 23 64 65 66 69 6e 65        90.#define
584a0 20 4f 50 5f 44 65 6c 65 74 65 20 20 20 20 20 20   OP_Delete      
584b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
584c0 20 20 20 20 20 20 20 20 39 31 0a 23 64 65 66 69          91.#defi
584d0 6e 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c 20 20  ne OP_AggFinal  
584e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
584f0 20 20 20 20 20 20 20 20 20 20 39 32 0a 23 64 65            92.#de
58500 66 69 6e 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20  fine OP_Compare 
58510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58520 20 20 20 20 20 20 20 20 20 20 20 20 39 33 0a 23              93.#
58530 64 65 66 69 6e 65 20 4f 50 5f 53 68 69 66 74 4c  define OP_ShiftL
58540 65 66 74 20 20 20 20 20 20 20 20 20 20 20 20 20  eft             
58550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 36                76
58560 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
58570 5f 4c 53 48 49 46 54 20 20 20 2a 2f 0a 23 64 65  _LSHIFT   */.#de
58580 66 69 6e 65 20 4f 50 5f 47 6f 74 6f 20 20 20 20  fine OP_Goto    
58590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
585a0 20 20 20 20 20 20 20 20 20 20 20 20 39 34 0a 23              94.#
585b0 64 65 66 69 6e 65 20 4f 50 5f 54 61 62 6c 65 4c  define OP_TableL
585c0 6f 63 6b 20 20 20 20 20 20 20 20 20 20 20 20 20  ock             
585d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 35                95
585e0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 69 66 6f  .#define OP_Fifo
585f0 52 65 61 64 20 20 20 20 20 20 20 20 20 20 20 20  Read            
58600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58610 39 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6c  96.#define OP_Cl
58620 65 61 72 20 20 20 20 20 20 20 20 20 20 20 20 20  ear             
58630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58640 20 20 39 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f    97.#define OP_
58650 4d 6f 76 65 4c 74 20 20 20 20 20 20 20 20 20 20  MoveLt          
58660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58670 20 20 20 20 39 38 0a 23 64 65 66 69 6e 65 20 4f      98.#define O
58680 50 5f 4c 65 20 20 20 20 20 20 20 20 20 20 20 20  P_Le            
58690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
586a0 20 20 20 20 20 20 37 30 20 20 20 2f 2a 20 73 61        70   /* sa
586b0 6d 65 20 61 73 20 54 4b 5f 4c 45 20 20 20 20 20  me as TK_LE     
586c0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
586d0 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 20 20 20  VerifyCookie    
586e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
586f0 20 20 20 20 39 39 0a 23 64 65 66 69 6e 65 20 4f      99.#define O
58700 50 5f 41 67 67 53 74 65 70 20 20 20 20 20 20 20  P_AggStep       
58710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58720 20 20 20 20 20 31 30 30 0a 23 64 65 66 69 6e 65       100.#define
58730 20 4f 50 5f 54 6f 54 65 78 74 20 20 20 20 20 20   OP_ToText      
58740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58750 20 20 20 20 20 20 20 31 33 39 20 20 20 2f 2a 20         139   /* 
58760 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 54 45  same as TK_TO_TE
58770 58 54 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  XT  */.#define O
58780 50 5f 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20  P_Not           
58790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
587a0 20 20 20 20 20 20 31 36 20 20 20 2f 2a 20 73 61        16   /* sa
587b0 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 20 20 20 20  me as TK_NOT    
587c0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
587d0 54 6f 52 65 61 6c 20 20 20 20 20 20 20 20 20 20  ToReal          
587e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
587f0 20 20 20 31 34 33 20 20 20 2f 2a 20 73 61 6d 65     143   /* same
58800 20 61 73 20 54 4b 5f 54 4f 5f 52 45 41 4c 20 20   as TK_TO_REAL  
58810 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65  */.#define OP_Se
58820 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 20 20 20 20  tNumColumns     
58830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58840 20 31 30 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f   101.#define OP_
58850 54 72 61 6e 73 61 63 74 69 6f 6e 20 20 20 20 20  Transaction     
58860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58870 20 20 20 31 30 32 0a 23 64 65 66 69 6e 65 20 4f     102.#define O
58880 50 5f 56 46 69 6c 74 65 72 20 20 20 20 20 20 20  P_VFilter       
58890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
588a0 20 20 20 20 20 31 30 33 0a 23 64 65 66 69 6e 65       103.#define
588b0 20 4f 50 5f 4e 65 20 20 20 20 20 20 20 20 20 20   OP_Ne          
588c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
588d0 20 20 20 20 20 20 20 20 36 37 20 20 20 2f 2a 20          67   /* 
588e0 73 61 6d 65 20 61 73 20 54 4b 5f 4e 45 20 20 20  same as TK_NE   
588f0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f      */.#define O
58900 50 5f 56 44 65 73 74 72 6f 79 20 20 20 20 20 20  P_VDestroy      
58910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58920 20 20 20 20 20 31 30 34 0a 23 64 65 66 69 6e 65       104.#define
58930 20 4f 50 5f 43 6f 6e 74 65 78 74 50 6f 70 20 20   OP_ContextPop  
58940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58950 20 20 20 20 20 20 20 31 30 35 0a 23 64 65 66 69         105.#defi
58960 6e 65 20 4f 50 5f 42 69 74 4f 72 20 20 20 20 20  ne OP_BitOr     
58970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58980 20 20 20 20 20 20 20 20 20 20 37 35 20 20 20 2f            75   /
58990 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54  * same as TK_BIT
589a0 4f 52 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  OR    */.#define
589b0 20 4f 50 5f 4e 65 78 74 20 20 20 20 20 20 20 20   OP_Next        
589c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
589d0 20 20 20 20 20 20 20 31 30 36 0a 23 64 65 66 69         106.#defi
589e0 6e 65 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 20  ne OP_IdxInsert 
589f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58a00 20 20 20 20 20 20 20 20 20 31 30 37 0a 23 64 65           107.#de
58a10 66 69 6e 65 20 4f 50 5f 4c 74 20 20 20 20 20 20  fine OP_Lt      
58a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58a30 20 20 20 20 20 20 20 20 20 20 20 20 37 31 20 20              71  
58a40 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c   /* same as TK_L
58a50 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  T       */.#defi
58a60 6e 65 20 4f 50 5f 49 6e 73 65 72 74 20 20 20 20  ne OP_Insert    
58a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58a80 20 20 20 20 20 20 20 20 20 31 30 38 0a 23 64 65           108.#de
58a90 66 69 6e 65 20 4f 50 5f 44 65 73 74 72 6f 79 20  fine OP_Destroy 
58aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58ab0 20 20 20 20 20 20 20 20 20 20 20 31 30 39 0a 23             109.#
58ac0 64 65 66 69 6e 65 20 4f 50 5f 52 65 61 64 43 6f  define OP_ReadCo
58ad0 6f 6b 69 65 20 20 20 20 20 20 20 20 20 20 20 20  okie            
58ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 30               110
58af0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 6f 72 63  .#define OP_Forc
58b00 65 49 6e 74 20 20 20 20 20 20 20 20 20 20 20 20  eInt            
58b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
58b20 31 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 6f  11.#define OP_Lo
58b30 61 64 41 6e 61 6c 79 73 69 73 20 20 20 20 20 20  adAnalysis      
58b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58b50 20 31 31 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f   112.#define OP_
58b60 45 78 70 6c 61 69 6e 20 20 20 20 20 20 20 20 20  Explain         
58b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58b80 20 20 20 31 31 33 0a 23 64 65 66 69 6e 65 20 4f     113.#define O
58b90 50 5f 4f 70 65 6e 50 73 65 75 64 6f 20 20 20 20  P_OpenPseudo    
58ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58bb0 20 20 20 20 20 31 31 34 0a 23 64 65 66 69 6e 65       114.#define
58bc0 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
58bd0 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l               
58be0 20 20 20 20 20 20 20 31 31 35 0a 23 64 65 66 69         115.#defi
58bf0 6e 65 20 4f 50 5f 4e 75 6c 6c 20 20 20 20 20 20  ne OP_Null      
58c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58c10 20 20 20 20 20 20 20 20 20 31 31 36 0a 23 64 65           116.#de
58c20 66 69 6e 65 20 4f 50 5f 4d 6f 76 65 20 20 20 20  fine OP_Move    
58c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58c40 20 20 20 20 20 20 20 20 20 20 20 31 31 37 0a 23             117.#
58c50 64 65 66 69 6e 65 20 4f 50 5f 42 6c 6f 62 20 20  define OP_Blob  
58c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58c70 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 38               118
58c80 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 64 64 20  .#define OP_Add 
58c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58cb0 37 38 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  78   /* same as 
58cc0 54 4b 5f 50 4c 55 53 20 20 20 20 20 2a 2f 0a 23  TK_PLUS     */.#
58cd0 64 65 66 69 6e 65 20 4f 50 5f 52 65 77 69 6e 64  define OP_Rewind
58ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 39               119
58d00 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f 76 65  .#define OP_Move
58d10 47 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Ge              
58d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
58d30 32 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 42  20.#define OP_VB
58d40 65 67 69 6e 20 20 20 20 20 20 20 20 20 20 20 20  egin            
58d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58d60 20 31 32 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f   121.#define OP_
58d70 56 55 70 64 61 74 65 20 20 20 20 20 20 20 20 20  VUpdate         
58d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58d90 20 20 20 31 32 32 0a 23 64 65 66 69 6e 65 20 4f     122.#define O
58da0 50 5f 49 66 5a 65 72 6f 20 20 20 20 20 20 20 20  P_IfZero        
58db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58dc0 20 20 20 20 20 31 32 33 0a 23 64 65 66 69 6e 65       123.#define
58dd0 20 4f 50 5f 42 69 74 4e 6f 74 20 20 20 20 20 20   OP_BitNot      
58de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58df0 20 20 20 20 20 20 20 20 38 37 20 20 20 2f 2a 20          87   /* 
58e00 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f  same as TK_BITNO
58e10 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  T   */.#define O
58e20 50 5f 56 43 72 65 61 74 65 20 20 20 20 20 20 20  P_VCreate       
58e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58e40 20 20 20 20 20 31 32 34 0a 23 64 65 66 69 6e 65       124.#define
58e50 20 4f 50 5f 46 6f 75 6e 64 20 20 20 20 20 20 20   OP_Found       
58e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58e70 20 20 20 20 20 20 20 31 32 35 0a 23 64 65 66 69         125.#defi
58e80 6e 65 20 4f 50 5f 49 66 50 6f 73 20 20 20 20 20  ne OP_IfPos     
58e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58ea0 20 20 20 20 20 20 20 20 20 31 32 37 0a 23 64 65           127.#de
58eb0 66 69 6e 65 20 4f 50 5f 4e 75 6c 6c 52 6f 77 20  fine OP_NullRow 
58ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58ed0 20 20 20 20 20 20 20 20 20 20 20 31 32 38 0a 23             128.#
58ee0 64 65 66 69 6e 65 20 4f 50 5f 4a 75 6d 70 20 20  define OP_Jump  
58ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58f00 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 39               129
58f10 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 65 72 6d  .#define OP_Perm
58f20 75 74 61 74 69 6f 6e 20 20 20 20 20 20 20 20 20  utation         
58f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
58f40 33 30 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f  30../* The follo
58f50 77 69 6e 67 20 6f 70 63 6f 64 65 20 76 61 6c 75  wing opcode valu
58f60 65 73 20 61 72 65 20 6e 65 76 65 72 20 75 73 65  es are never use
58f70 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f  d */.#define OP_
58f80 4e 6f 74 55 73 65 64 5f 31 33 31 20 20 20 20 20  NotUsed_131     
58f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58fa0 20 20 20 31 33 31 0a 23 64 65 66 69 6e 65 20 4f     131.#define O
58fb0 50 5f 4e 6f 74 55 73 65 64 5f 31 33 32 20 20 20  P_NotUsed_132   
58fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58fd0 20 20 20 20 20 31 33 32 0a 23 64 65 66 69 6e 65       132.#define
58fe0 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 33 20   OP_NotUsed_133 
58ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59000 20 20 20 20 20 20 20 31 33 33 0a 23 64 65 66 69         133.#defi
59010 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33  ne OP_NotUsed_13
59020 34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  4               
59030 20 20 20 20 20 20 20 20 20 31 33 34 0a 23 64 65           134.#de
59040 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f  fine OP_NotUsed_
59050 31 33 35 20 20 20 20 20 20 20 20 20 20 20 20 20  135             
59060 20 20 20 20 20 20 20 20 20 20 20 31 33 35 0a 23             135.#
59070 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65  define OP_NotUse
59080 64 5f 31 33 36 20 20 20 20 20 20 20 20 20 20 20  d_136           
59090 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 36               136
590a0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55  .#define OP_NotU
590b0 73 65 64 5f 31 33 37 20 20 20 20 20 20 20 20 20  sed_137         
590c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
590d0 33 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f  37.#define OP_No
590e0 74 55 73 65 64 5f 31 33 38 20 20 20 20 20 20 20  tUsed_138       
590f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59100 20 31 33 38 0a 0a 0a 2f 2a 20 50 72 6f 70 65 72   138.../* Proper
59110 74 69 65 73 20 73 75 63 68 20 61 73 20 22 6f 75  ties such as "ou
59120 74 32 22 20 6f 72 20 22 6a 75 6d 70 22 20 74 68  t2" or "jump" th
59130 61 74 20 61 72 65 20 73 70 65 63 69 66 69 65 64  at are specified
59140 20 69 6e 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20   in.** comments 
59150 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 63  following the "c
59160 61 73 65 22 20 66 6f 72 20 65 61 63 68 20 6f 70  ase" for each op
59170 63 6f 64 65 20 69 6e 20 74 68 65 20 76 64 62 65  code in the vdbe
59180 2e 63 0a 2a 2a 20 61 72 65 20 65 6e 63 6f 64 65  .c.** are encode
59190 64 20 69 6e 74 6f 20 62 69 74 76 65 63 74 6f 72  d into bitvector
591a0 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2f  s as follows:.*/
591b0 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 4a  .#define OPFLG_J
591c0 55 4d 50 20 20 20 20 20 20 20 20 20 20 20 20 30  UMP            0
591d0 78 30 30 30 31 20 20 2f 2a 20 6a 75 6d 70 3a 20  x0001  /* jump: 
591e0 20 50 32 20 68 6f 6c 64 73 20 6a 6d 70 20 74 61   P2 holds jmp ta
591f0 72 67 65 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  rget */.#define 
59200 4f 50 46 4c 47 5f 4f 55 54 32 5f 50 52 45 52 45  OPFLG_OUT2_PRERE
59210 4c 45 41 53 45 20 30 78 30 30 30 32 20 20 2f 2a  LEASE 0x0002  /*
59220 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65   out2-prerelease
59230 3a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46  : */.#define OPF
59240 4c 47 5f 49 4e 31 20 20 20 20 20 20 20 20 20 20  LG_IN1          
59250 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 69 6e     0x0004  /* in
59260 31 3a 20 20 20 50 31 20 69 73 20 61 6e 20 69 6e  1:   P1 is an in
59270 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  put */.#define O
59280 50 46 4c 47 5f 49 4e 32 20 20 20 20 20 20 20 20  PFLG_IN2        
59290 20 20 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20       0x0008  /* 
592a0 69 6e 32 3a 20 20 20 50 32 20 69 73 20 61 6e 20  in2:   P2 is an 
592b0 69 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65  input */.#define
592c0 20 4f 50 46 4c 47 5f 49 4e 33 20 20 20 20 20 20   OPFLG_IN3      
592d0 20 20 20 20 20 20 20 30 78 30 30 31 30 20 20 2f         0x0010  /
592e0 2a 20 69 6e 33 3a 20 20 20 50 33 20 69 73 20 61  * in3:   P3 is a
592f0 6e 20 69 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69  n input */.#defi
59300 6e 65 20 4f 50 46 4c 47 5f 4f 55 54 33 20 20 20  ne OPFLG_OUT3   
59310 20 20 20 20 20 20 20 20 20 30 78 30 30 32 30 20           0x0020 
59320 20 2f 2a 20 6f 75 74 33 3a 20 20 50 33 20 69 73   /* out3:  P3 is
59330 20 61 6e 20 6f 75 74 70 75 74 20 2a 2f 0a 23 64   an output */.#d
59340 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 49 54  efine OPFLG_INIT
59350 49 41 4c 49 5a 45 52 20 7b 5c 0a 2f 2a 20 20 20  IALIZER {\./*   
59360 30 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 31 2c  0 */ 0x00, 0x01,
59370 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 31   0x00, 0x00, 0x1
59380 30 2c 20 30 78 30 32 2c 20 30 78 31 31 2c 20 30  0, 0x02, 0x11, 0
59390 78 30 30 2c 5c 0a 2f 2a 20 20 20 38 20 2a 2f 20  x00,\./*   8 */ 
593a0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 35  0x00, 0x00, 0x05
593b0 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78  , 0x02, 0x00, 0x
593c0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 5c  00, 0x00, 0x00,\
593d0 0a 2f 2a 20 20 31 36 20 2a 2f 20 30 78 30 34 2c  ./*  16 */ 0x04,
593e0 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30   0x02, 0x00, 0x0
593f0 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  1, 0x00, 0x00, 0
59400 78 30 30 2c 20 30 78 30 35 2c 5c 0a 2f 2a 20 20  x00, 0x05,\./*  
59410 32 34 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 34  24 */ 0x00, 0x04
59420 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78  , 0x02, 0x02, 0x
59430 30 32 2c 20 30 78 30 34 2c 20 30 78 30 30 2c 20  02, 0x04, 0x00, 
59440 30 78 30 30 2c 5c 0a 2f 2a 20 20 33 32 20 2a 2f  0x00,\./*  32 */
59450 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
59460 32 2c 20 30 78 31 31 2c 20 30 78 31 31 2c 20 30  2, 0x11, 0x11, 0
59470 78 30 32 2c 20 30 78 30 35 2c 20 30 78 30 30 2c  x02, 0x05, 0x00,
59480 5c 0a 2f 2a 20 20 34 30 20 2a 2f 20 30 78 30 32  \./*  40 */ 0x02
59490 2c 20 30 78 31 31 2c 20 30 78 30 34 2c 20 30 78  , 0x11, 0x04, 0x
594a0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 63 2c 20  00, 0x00, 0x0c, 
594b0 30 78 31 31 2c 20 30 78 30 31 2c 5c 0a 2f 2a 20  0x11, 0x01,\./* 
594c0 20 34 38 20 2a 2f 20 30 78 30 32 2c 20 30 78 30   48 */ 0x02, 0x0
594d0 31 2c 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30  1, 0x00, 0x02, 0
594e0 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 32 2c  x01, 0x01, 0x02,
594f0 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 35 36 20 2a   0x00,\./*  56 *
59500 2f 20 30 78 30 34 2c 20 30 78 30 30 2c 20 30 78  / 0x04, 0x00, 0x
59510 30 30 2c 20 30 78 30 30 2c 20 30 78 32 63 2c 20  00, 0x00, 0x2c, 
59520 30 78 32 63 2c 20 30 78 30 30 2c 20 30 78 31 31  0x2c, 0x00, 0x11
59530 2c 5c 0a 2f 2a 20 20 36 34 20 2a 2f 20 30 78 30  ,\./*  64 */ 0x0
59540 30 2c 20 30 78 30 35 2c 20 30 78 30 35 2c 20 30  0, 0x05, 0x05, 0
59550 78 31 35 2c 20 30 78 31 35 2c 20 30 78 31 35 2c  x15, 0x15, 0x15,
59560 20 30 78 31 35 2c 20 30 78 31 35 2c 5c 0a 2f 2a   0x15, 0x15,\./*
59570 20 20 37 32 20 2a 2f 20 30 78 31 35 2c 20 30 78    72 */ 0x15, 0x
59580 30 30 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20  00, 0x2c, 0x2c, 
59590 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32 63  0x2c, 0x2c, 0x2c
595a0 2c 20 30 78 32 63 2c 5c 0a 2f 2a 20 20 38 30 20  , 0x2c,\./*  80 
595b0 2a 2f 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30  */ 0x2c, 0x2c, 0
595c0 78 32 63 2c 20 30 78 32 63 2c 20 30 78 31 31 2c  x2c, 0x2c, 0x11,
595d0 20 30 78 30 35 2c 20 30 78 30 30 2c 20 30 78 30   0x05, 0x00, 0x0
595e0 34 2c 5c 0a 2f 2a 20 20 38 38 20 2a 2f 20 30 78  4,\./*  88 */ 0x
595f0 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  02, 0x00, 0x00, 
59600 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
59610 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 5c 0a 2f  , 0x01, 0x00,\./
59620 2a 20 20 39 36 20 2a 2f 20 30 78 30 31 2c 20 30  *  96 */ 0x01, 0
59630 78 30 30 2c 20 30 78 31 31 2c 20 30 78 30 30 2c  x00, 0x11, 0x00,
59640 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
59650 30 2c 20 30 78 30 31 2c 5c 0a 2f 2a 20 31 30 34  0, 0x01,\./* 104
59660 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 20   */ 0x00, 0x00, 
59670 30 78 30 31 2c 20 30 78 30 38 2c 20 30 78 30 30  0x01, 0x08, 0x00
59680 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78  , 0x02, 0x02, 0x
59690 30 35 2c 5c 0a 2f 2a 20 31 31 32 20 2a 2f 20 30  05,\./* 112 */ 0
596a0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
596b0 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 30   0x00, 0x02, 0x0
596c0 30 2c 20 30 78 30 32 2c 20 30 78 30 31 2c 5c 0a  0, 0x02, 0x01,\.
596d0 2f 2a 20 31 32 30 20 2a 2f 20 30 78 31 31 2c 20  /* 120 */ 0x11, 
596e0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 35  0x00, 0x00, 0x05
596f0 2c 20 30 78 30 30 2c 20 30 78 31 31 2c 20 30 78  , 0x00, 0x11, 0x
59700 30 32 2c 20 30 78 30 35 2c 5c 0a 2f 2a 20 31 32  02, 0x05,\./* 12
59710 38 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 31 2c  8 */ 0x00, 0x01,
59720 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
59730 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
59740 78 30 30 2c 5c 0a 2f 2a 20 31 33 36 20 2a 2f 20  x00,\./* 136 */ 
59750 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
59760 2c 20 30 78 30 34 2c 20 30 78 30 34 2c 20 30 78  , 0x04, 0x04, 0x
59770 30 34 2c 20 30 78 30 34 2c 20 30 78 30 34 2c 5c  04, 0x04, 0x04,\
59780 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
59790 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64  *** End of opcod
597a0 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  es.h ***********
597b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
597c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
597d0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
597e0 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
597f0 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
59800 20 69 6e 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a   in vdbe.h *****
59810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59820 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 74 6f  **/../*.** Proto
59830 74 79 70 65 73 20 66 6f 72 20 74 68 65 20 56 44  types for the VD
59840 42 45 20 69 6e 74 65 72 66 61 63 65 2e 20 20 53  BE interface.  S
59850 65 65 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74  ee comments on t
59860 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
59870 6e 0a 2a 2a 20 66 6f 72 20 61 20 64 65 73 63 72  n.** for a descr
59880 69 70 74 69 6f 6e 20 6f 66 20 77 68 61 74 20 65  iption of what e
59890 61 63 68 20 6f 66 20 74 68 65 73 65 20 72 6f 75  ach of these rou
598a0 74 69 6e 65 73 20 64 6f 65 73 2e 0a 2a 2f 0a 53  tines does..*/.S
598b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64  QLITE_PRIVATE Vd
598c0 62 65 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43  be *sqlite3VdbeC
598d0 72 65 61 74 65 28 73 71 6c 69 74 65 33 2a 29 3b  reate(sqlite3*);
598e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
598f0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
59900 64 64 4f 70 30 28 56 64 62 65 2a 2c 69 6e 74 29  ddOp0(Vdbe*,int)
59910 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
59920 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
59930 41 64 64 4f 70 31 28 56 64 62 65 2a 2c 69 6e 74  AddOp1(Vdbe*,int
59940 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
59950 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
59960 33 56 64 62 65 41 64 64 4f 70 32 28 56 64 62 65  3VdbeAddOp2(Vdbe
59970 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a  *,int,int,int);.
59980 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
59990 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
599a0 64 4f 70 33 28 56 64 62 65 2a 2c 69 6e 74 2c 69  dOp3(Vdbe*,int,i
599b0 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c  nt,int,int);.SQL
599c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
599d0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
599e0 34 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c  4(Vdbe*,int,int,
599f0 69 6e 74 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68  int,int,const ch
59a00 61 72 20 2a 7a 50 34 2c 69 6e 74 29 3b 0a 53 51  ar *zP4,int);.SQ
59a10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
59a20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
59a30 70 4c 69 73 74 28 56 64 62 65 2a 2c 20 69 6e 74  pList(Vdbe*, int
59a40 20 6e 4f 70 2c 20 56 64 62 65 4f 70 4c 69 73 74   nOp, VdbeOpList
59a50 20 63 6f 6e 73 74 20 2a 61 4f 70 29 3b 0a 53 51   const *aOp);.SQ
59a60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
59a70 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61  d sqlite3VdbeCha
59a80 6e 67 65 50 31 28 56 64 62 65 2a 2c 20 69 6e 74  ngeP1(Vdbe*, int
59a90 20 61 64 64 72 2c 20 69 6e 74 20 50 31 29 3b 0a   addr, int P1);.
59aa0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
59ab0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43  oid sqlite3VdbeC
59ac0 68 61 6e 67 65 50 32 28 56 64 62 65 2a 2c 20 69  hangeP2(Vdbe*, i
59ad0 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 50 32 29  nt addr, int P2)
59ae0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
59af0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
59b00 65 43 68 61 6e 67 65 50 33 28 56 64 62 65 2a 2c  eChangeP3(Vdbe*,
59b10 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 50   int addr, int P
59b20 33 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  3);.SQLITE_PRIVA
59b30 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
59b40 64 62 65 43 68 61 6e 67 65 50 35 28 56 64 62 65  dbeChangeP5(Vdbe
59b50 2a 2c 20 75 38 20 50 35 29 3b 0a 53 51 4c 49 54  *, u8 P5);.SQLIT
59b60 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
59b70 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
59b80 72 65 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64  re(Vdbe*, int ad
59b90 64 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  dr);.SQLITE_PRIV
59ba0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
59bb0 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70  VdbeChangeToNoop
59bc0 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72  (Vdbe*, int addr
59bd0 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45  , int N);.SQLITE
59be0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
59bf0 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
59c00 34 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64  4(Vdbe*, int add
59c10 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  r, const char *z
59c20 50 34 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49  P4, int N);.SQLI
59c30 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
59c40 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42  sqlite3VdbeUsesB
59c50 74 72 65 65 28 56 64 62 65 2a 2c 20 69 6e 74 29  tree(Vdbe*, int)
59c60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
59c70 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33   VdbeOp *sqlite3
59c80 56 64 62 65 47 65 74 4f 70 28 56 64 62 65 2a 2c  VdbeGetOp(Vdbe*,
59c90 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
59ca0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
59cb0 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56  3VdbeMakeLabel(V
59cc0 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  dbe*);.SQLITE_PR
59cd0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
59ce0 65 33 56 64 62 65 44 65 6c 65 74 65 28 56 64 62  e3VdbeDelete(Vdb
59cf0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
59d00 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
59d10 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 56 64  VdbeMakeReady(Vd
59d20 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c  be*,int,int,int,
59d30 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
59d40 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
59d50 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 56 64 62  VdbeFinalize(Vdb
59d60 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
59d70 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
59d80 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
59d90 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51  (Vdbe*, int);.SQ
59da0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
59db0 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
59dc0 65 6e 74 41 64 64 72 28 56 64 62 65 2a 29 3b 0a  entAddr(Vdbe*);.
59dd0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
59de0 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  BUG.SQLITE_PRIVA
59df0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
59e00 33 56 64 62 65 54 72 61 63 65 28 56 64 62 65 2a  3VdbeTrace(Vdbe*
59e10 2c 46 49 4c 45 2a 29 3b 0a 23 65 6e 64 69 66 0a  ,FILE*);.#endif.
59e20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
59e30 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 52  oid sqlite3VdbeR
59e40 65 73 65 74 53 74 65 70 52 65 73 75 6c 74 28 56  esetStepResult(V
59e50 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  dbe*);.SQLITE_PR
59e60 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
59e70 33 56 64 62 65 52 65 73 65 74 28 56 64 62 65 2a  3VdbeReset(Vdbe*
59e80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
59e90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
59ea0 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 56 64 62  beSetNumCols(Vdb
59eb0 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  e*,int);.SQLITE_
59ec0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
59ed0 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
59ee0 65 28 56 64 62 65 2a 2c 20 69 6e 74 2c 20 69 6e  e(Vdbe*, int, in
59ef0 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  t, const char *,
59f00 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29   void(*)(void*))
59f10 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
59f20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
59f30 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 56 64  eCountChanges(Vd
59f40 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  be*);.SQLITE_PRI
59f50 56 41 54 45 20 73 71 6c 69 74 65 33 20 2a 73 71  VATE sqlite3 *sq
59f60 6c 69 74 65 33 56 64 62 65 44 62 28 56 64 62 65  lite3VdbeDb(Vdbe
59f70 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
59f80 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
59f90 64 62 65 53 65 74 53 71 6c 28 56 64 62 65 2a 2c  dbeSetSql(Vdbe*,
59fa0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
59fb0 69 6e 74 20 6e 29 3b 0a 53 51 4c 49 54 45 5f 50  int n);.SQLITE_P
59fc0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
59fd0 74 65 33 56 64 62 65 53 77 61 70 28 56 64 62 65  te3VdbeSwap(Vdbe
59fe0 2a 2c 56 64 62 65 2a 29 3b 0a 0a 23 69 66 64 65  *,Vdbe*);..#ifde
59ff0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
5a000 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e  MEMORY_MANAGEMEN
5a010 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
5a020 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
5a030 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e  ReleaseMemory(in
5a040 74 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54  t);.#endif.SQLIT
5a050 45 5f 50 52 49 56 41 54 45 20 55 6e 70 61 63 6b  E_PRIVATE Unpack
5a060 65 64 52 65 63 6f 72 64 20 2a 73 71 6c 69 74 65  edRecord *sqlite
5a070 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63  3VdbeRecordUnpac
5a080 6b 28 4b 65 79 49 6e 66 6f 2a 2c 69 6e 74 2c 63  k(KeyInfo*,int,c
5a090 6f 6e 73 74 20 76 6f 69 64 2a 2c 0a 20 20 20 20  onst void*,.    
5a0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a0c0 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f      UnpackedReco
5a0d0 72 64 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  rd*,int);.SQLITE
5a0e0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
5a0f0 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55  lite3VdbeDeleteU
5a100 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 55 6e  npackedRecord(Un
5a110 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0a  packedRecord*);.
5a120 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5a130 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  nt sqlite3VdbeRe
5a140 63 6f 72 64 43 6f 6d 70 61 72 65 28 69 6e 74 2c  cordCompare(int,
5a150 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 55 6e 70 61  const void*,Unpa
5a160 63 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0a 0a 0a  ckedRecord*);...
5a170 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53  #ifndef NDEBUG.S
5a180 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
5a190 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
5a1a0 43 6f 6d 6d 65 6e 74 28 56 64 62 65 2a 2c 20 63  Comment(Vdbe*, c
5a1b0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29  onst char*, ...)
5a1c0 3b 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 43  ;.# define VdbeC
5a1d0 6f 6d 6d 65 6e 74 28 58 29 20 20 73 71 6c 69 74  omment(X)  sqlit
5a1e0 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74 20 58 0a  e3VdbeComment X.
5a1f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
5a200 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
5a210 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56 64 62  eNoopComment(Vdb
5a220 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e*, const char*,
5a230 20 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65 20   ...);.# define 
5a240 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28  VdbeNoopComment(
5a250 58 29 20 20 73 71 6c 69 74 65 33 56 64 62 65 4e  X)  sqlite3VdbeN
5a260 6f 6f 70 43 6f 6d 6d 65 6e 74 20 58 0a 23 65 6c  oopComment X.#el
5a270 73 65 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65  se.# define Vdbe
5a280 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 20 64 65 66  Comment(X).# def
5a290 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d  ine VdbeNoopComm
5a2a0 65 6e 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23  ent(X).#endif..#
5a2b0 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  endif../********
5a2c0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64  ****** End of vd
5a2d0 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  be.h ***********
5a2e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5a2f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5a300 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
5a310 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
5a320 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
5a330 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74  off in sqliteInt
5a340 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
5a350 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
5a360 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70  ****** Include p
5a370 61 67 65 72 2e 68 20 69 6e 20 74 68 65 20 6d 69  ager.h in the mi
5a380 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e  ddle of sqliteIn
5a390 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
5a3a0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
5a3b0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
5a3c0 65 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a  e pager.h ******
5a3d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5a3e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5a3f0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
5a400 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a  1 September 15.*
5a410 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
5a420 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
5a430 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
5a440 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
5a450 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
5a460 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
5a470 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
5a480 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
5a490 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
5a4a0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
5a4b0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
5a4c0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
5a4d0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
5a4e0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
5a4f0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
5a500 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
5a510 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
5a520 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
5a530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5a540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5a550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5a560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
5a570 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c   This header fil
5a580 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e  e defines the in
5a590 74 65 72 66 61 63 65 20 74 68 61 74 20 74 68 65  terface that the
5a5a0 20 73 71 6c 69 74 65 20 70 61 67 65 20 63 61 63   sqlite page cac
5a5b0 68 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e  he.** subsystem.
5a5c0 20 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65    The page cache
5a5d0 20 73 75 62 73 79 73 74 65 6d 20 72 65 61 64 73   subsystem reads
5a5e0 20 61 6e 64 20 77 72 69 74 65 73 20 61 20 66 69   and writes a fi
5a5f0 6c 65 20 61 20 70 61 67 65 0a 2a 2a 20 61 74 20  le a page.** at 
5a600 61 20 74 69 6d 65 20 61 6e 64 20 70 72 6f 76 69  a time and provi
5a610 64 65 73 20 61 20 6a 6f 75 72 6e 61 6c 20 66 6f  des a journal fo
5a620 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a  r rollback..**.*
5a630 2a 20 40 28 23 29 20 24 49 64 3a 20 70 61 67 65  * @(#) $Id: page
5a640 72 2e 68 2c 76 20 31 2e 38 36 20 32 30 30 38 2f  r.h,v 1.86 2008/
5a650 31 30 2f 31 37 20 31 38 3a 35 31 3a 35 33 20 64  10/17 18:51:53 d
5a660 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
5a670 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50 41  .*/..#ifndef _PA
5a680 47 45 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f  GER_H_.#define _
5a690 50 41 47 45 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20  PAGER_H_../*.** 
5a6a0 49 66 20 64 65 66 69 6e 65 64 20 61 73 20 6e 6f  If defined as no
5a6b0 6e 2d 7a 65 72 6f 2c 20 61 75 74 6f 2d 76 61 63  n-zero, auto-vac
5a6c0 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 62  uum is enabled b
5a6d0 79 20 64 65 66 61 75 6c 74 2e 20 4f 74 68 65 72  y default. Other
5a6e0 77 69 73 65 0a 2a 2a 20 69 74 20 6d 75 73 74 20  wise.** it must 
5a6f0 62 65 20 74 75 72 6e 65 64 20 6f 6e 20 66 6f 72  be turned on for
5a700 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20 75   each database u
5a710 73 69 6e 67 20 22 50 52 41 47 4d 41 20 61 75 74  sing "PRAGMA aut
5a720 6f 5f 76 61 63 75 75 6d 20 3d 20 31 22 2e 0a 2a  o_vacuum = 1"..*
5a730 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
5a740 5f 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c  _DEFAULT_JOURNAL
5a750 5f 53 49 5a 45 5f 4c 49 4d 49 54 0a 20 20 23 64  _SIZE_LIMIT.  #d
5a760 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46  efine SQLITE_DEF
5a770 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a  AULT_JOURNAL_SIZ
5a780 45 5f 4c 49 4d 49 54 20 2d 31 0a 23 65 6e 64 69  E_LIMIT -1.#endi
5a790 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70  f../*.** The typ
5a7a0 65 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73  e used to repres
5a7b0 65 6e 74 20 61 20 70 61 67 65 20 6e 75 6d 62 65  ent a page numbe
5a7c0 72 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 61  r.  The first pa
5a7d0 67 65 20 69 6e 20 61 20 66 69 6c 65 0a 2a 2a 20  ge in a file.** 
5a7e0 69 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20 31  is called page 1
5a7f0 2e 20 20 30 20 69 73 20 75 73 65 64 20 74 6f 20  .  0 is used to 
5a800 72 65 70 72 65 73 65 6e 74 20 22 6e 6f 74 20 61  represent "not a
5a810 20 70 61 67 65 22 2e 0a 2a 2f 0a 74 79 70 65 64   page"..*/.typed
5a820 65 66 20 75 33 32 20 50 67 6e 6f 3b 0a 0a 2f 2a  ef u32 Pgno;../*
5a830 0a 2a 2a 20 45 61 63 68 20 6f 70 65 6e 20 66 69  .** Each open fi
5a840 6c 65 20 69 73 20 6d 61 6e 61 67 65 64 20 62 79  le is managed by
5a850 20 61 20 73 65 70 61 72 61 74 65 20 69 6e 73 74   a separate inst
5a860 61 6e 63 65 20 6f 66 20 74 68 65 20 22 50 61 67  ance of the "Pag
5a870 65 72 22 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  er" structure..*
5a880 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
5a890 20 50 61 67 65 72 20 50 61 67 65 72 3b 0a 0a 2f   Pager Pager;../
5a8a0 2a 0a 2a 2a 20 48 61 6e 64 6c 65 20 74 79 70 65  *.** Handle type
5a8b0 20 66 6f 72 20 70 61 67 65 73 2e 0a 2a 2f 0a 74   for pages..*/.t
5a8c0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67  ypedef struct Pg
5a8d0 48 64 72 20 44 62 50 61 67 65 3b 0a 0a 2f 2a 0a  Hdr DbPage;../*.
5a8e0 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65  ** Allowed value
5a8f0 73 20 66 6f 72 20 74 68 65 20 66 6c 61 67 73 20  s for the flags 
5a900 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c  parameter to sql
5a910 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 29 2e  ite3PagerOpen().
5a920 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 54 68 69  .**.** NOTE: Thi
5a930 73 20 76 61 6c 75 65 73 20 6d 75 73 74 20 6d 61  s values must ma
5a940 74 63 68 20 74 68 65 20 63 6f 72 72 65 73 70 6f  tch the correspo
5a950 6e 64 69 6e 67 20 42 54 52 45 45 5f 20 76 61 6c  nding BTREE_ val
5a960 75 65 73 20 69 6e 20 62 74 72 65 65 2e 68 2e 0a  ues in btree.h..
5a970 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  */.#define PAGER
5a980 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 20 30  _OMIT_JOURNAL  0
5a990 78 30 30 30 31 20 20 20 20 2f 2a 20 44 6f 20 6e  x0001    /* Do n
5a9a0 6f 74 20 75 73 65 20 61 20 72 6f 6c 6c 62 61 63  ot use a rollbac
5a9b0 6b 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64 65  k journal */.#de
5a9c0 66 69 6e 65 20 50 41 47 45 52 5f 4e 4f 5f 52 45  fine PAGER_NO_RE
5a9d0 41 44 4c 4f 43 4b 20 20 20 30 78 30 30 30 32 20  ADLOCK   0x0002 
5a9e0 20 20 20 2f 2a 20 4f 6d 69 74 20 72 65 61 64 6c     /* Omit readl
5a9f0 6f 63 6b 73 20 6f 6e 20 72 65 61 64 6f 6e 6c 79  ocks on readonly
5aa00 20 66 69 6c 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a   files */../*.**
5aa10 20 56 61 6c 69 64 20 76 61 6c 75 65 73 20 66 6f   Valid values fo
5aa20 72 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  r the second arg
5aa30 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33  ument to sqlite3
5aa40 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65  PagerLockingMode
5aa50 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ()..*/.#define P
5aa60 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
5aa70 5f 51 55 45 52 59 20 20 20 20 20 20 2d 31 0a 23  _QUERY      -1.#
5aa80 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43  define PAGER_LOC
5aa90 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20  KINGMODE_NORMAL 
5aaa0 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 50       0.#define P
5aab0 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
5aac0 5f 45 58 43 4c 55 53 49 56 45 20 20 20 31 0a 0a  _EXCLUSIVE   1..
5aad0 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61 6c 75  /*.** Valid valu
5aae0 65 73 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e  es for the secon
5aaf0 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71  d argument to sq
5ab00 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61  lite3PagerJourna
5ab10 6c 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 65 66  lMode()..*/.#def
5ab20 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41  ine PAGER_JOURNA
5ab30 4c 4d 4f 44 45 5f 51 55 45 52 59 20 20 20 20 20  LMODE_QUERY     
5ab40 20 2d 31 0a 23 64 65 66 69 6e 65 20 50 41 47 45   -1.#define PAGE
5ab50 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45  R_JOURNALMODE_DE
5ab60 4c 45 54 45 20 20 20 20 20 20 30 20 20 20 2f 2a  LETE      0   /*
5ab70 20 43 6f 6d 6d 69 74 20 62 79 20 64 65 6c 65 74   Commit by delet
5ab80 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ing journal file
5ab90 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45   */.#define PAGE
5aba0 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45  R_JOURNALMODE_PE
5abb0 52 53 49 53 54 20 20 20 20 20 31 20 20 20 2f 2a  RSIST     1   /*
5abc0 20 43 6f 6d 6d 69 74 20 62 79 20 7a 65 72 6f 69   Commit by zeroi
5abd0 6e 67 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  ng journal heade
5abe0 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47  r */.#define PAG
5abf0 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f  ER_JOURNALMODE_O
5ac00 46 46 20 20 20 20 20 20 20 20 20 32 20 20 20 2f  FF         2   /
5ac10 2a 20 4a 6f 75 72 6e 61 6c 20 6f 6d 69 74 74 65  * Journal omitte
5ac20 64 2e 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  d.  */.#define P
5ac30 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
5ac40 5f 54 52 55 4e 43 41 54 45 20 20 20 20 33 20 20  _TRUNCATE    3  
5ac50 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20 74 72   /* Commit by tr
5ac60 75 6e 63 61 74 69 6e 67 20 6a 6f 75 72 6e 61 6c  uncating journal
5ac70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45   */.#define PAGE
5ac80 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45  R_JOURNALMODE_ME
5ac90 4d 4f 52 59 20 20 20 20 20 20 34 20 20 20 2f 2a  MORY      4   /*
5aca0 20 49 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e   In-memory journ
5acb0 61 6c 20 66 69 6c 65 20 2a 2f 0a 0a 2f 2a 0a 2a  al file */../*.*
5acc0 2a 20 53 65 65 20 73 6f 75 72 63 65 20 63 6f 64  * See source cod
5acd0 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 61  e comments for a
5ace0 20 64 65 74 61 69 6c 65 64 20 64 65 73 63 72 69   detailed descri
5acf0 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 6c  ption of the fol
5ad00 6c 6f 77 69 6e 67 0a 2a 2a 20 72 6f 75 74 69 6e  lowing.** routin
5ad10 65 73 3a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  es:.*/.SQLITE_PR
5ad20 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
5ad30 33 50 61 67 65 72 4f 70 65 6e 28 73 71 6c 69 74  3PagerOpen(sqlit
5ad40 65 33 5f 76 66 73 20 2a 2c 20 50 61 67 65 72 20  e3_vfs *, Pager 
5ad50 2a 2a 70 70 50 61 67 65 72 2c 20 63 6f 6e 73 74  **ppPager, const
5ad60 20 63 68 61 72 2a 2c 20 69 6e 74 2c 69 6e 74 2c   char*, int,int,
5ad70 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
5ad80 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
5ad90 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e  3PagerSetBusyhan
5ada0 64 6c 65 72 28 50 61 67 65 72 2a 2c 20 42 75 73  dler(Pager*, Bus
5adb0 79 48 61 6e 64 6c 65 72 20 2a 70 42 75 73 79 48  yHandler *pBusyH
5adc0 61 6e 64 6c 65 72 29 3b 0a 53 51 4c 49 54 45 5f  andler);.SQLITE_
5add0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
5ade0 69 74 65 33 50 61 67 65 72 53 65 74 52 65 69 6e  ite3PagerSetRein
5adf0 69 74 65 72 28 50 61 67 65 72 2a 2c 20 76 6f 69  iter(Pager*, voi
5ae00 64 28 2a 29 28 44 62 50 61 67 65 2a 29 29 3b 0a  d(*)(DbPage*));.
5ae10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5ae20 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  nt sqlite3PagerS
5ae30 65 74 50 61 67 65 73 69 7a 65 28 50 61 67 65 72  etPagesize(Pager
5ae40 2a 2c 20 75 31 36 2a 29 3b 0a 53 51 4c 49 54 45  *, u16*);.SQLITE
5ae50 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
5ae60 69 74 65 33 50 61 67 65 72 4d 61 78 50 61 67 65  ite3PagerMaxPage
5ae70 43 6f 75 6e 74 28 50 61 67 65 72 2a 2c 20 69 6e  Count(Pager*, in
5ae80 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
5ae90 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
5aea0 67 65 72 52 65 61 64 46 69 6c 65 68 65 61 64 65  gerReadFileheade
5aeb0 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 2c 20 75  r(Pager*, int, u
5aec0 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a  nsigned char*);.
5aed0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
5aee0 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  oid sqlite3Pager
5aef0 53 65 74 43 61 63 68 65 73 69 7a 65 28 50 61 67  SetCachesize(Pag
5af00 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  er*, int);.SQLIT
5af10 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
5af20 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28  lite3PagerClose(
5af30 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 3b 0a  Pager *pPager);.
5af40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5af50 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 41  nt sqlite3PagerA
5af60 63 71 75 69 72 65 28 50 61 67 65 72 20 2a 70 50  cquire(Pager *pP
5af70 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c  ager, Pgno pgno,
5af80 20 44 62 50 61 67 65 20 2a 2a 70 70 50 61 67 65   DbPage **ppPage
5af90 2c 20 69 6e 74 20 63 6c 72 46 6c 61 67 29 3b 0a  , int clrFlag);.
5afa0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50  #define sqlite3P
5afb0 61 67 65 72 47 65 74 28 41 2c 42 2c 43 29 20 73  agerGet(A,B,C) s
5afc0 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69  qlite3PagerAcqui
5afd0 72 65 28 41 2c 42 2c 43 2c 30 29 0a 53 51 4c 49  re(A,B,C,0).SQLI
5afe0 54 45 5f 50 52 49 56 41 54 45 20 44 62 50 61 67  TE_PRIVATE DbPag
5aff0 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4c  e *sqlite3PagerL
5b000 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61  ookup(Pager *pPa
5b010 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 3b  ger, Pgno pgno);
5b020 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5b030 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
5b040 50 61 67 65 52 65 66 63 6f 75 6e 74 28 44 62 50  PageRefcount(DbP
5b050 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  age*);.SQLITE_PR
5b060 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
5b070 33 50 61 67 65 72 52 65 66 28 44 62 50 61 67 65  3PagerRef(DbPage
5b080 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
5b090 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
5b0a0 67 65 72 55 6e 72 65 66 28 44 62 50 61 67 65 2a  gerUnref(DbPage*
5b0b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
5b0c0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
5b0d0 65 72 57 72 69 74 65 28 44 62 50 61 67 65 2a 29  erWrite(DbPage*)
5b0e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
5b0f0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
5b100 72 50 61 67 65 63 6f 75 6e 74 28 50 61 67 65 72  rPagecount(Pager
5b110 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45  *, int*);.SQLITE
5b120 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
5b130 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74  ite3PagerTruncat
5b140 65 28 50 61 67 65 72 2a 2c 50 67 6e 6f 29 3b 0a  e(Pager*,Pgno);.
5b150 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5b160 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 42  nt sqlite3PagerB
5b170 65 67 69 6e 28 44 62 50 61 67 65 2a 2c 20 69 6e  egin(DbPage*, in
5b180 74 20 65 78 46 6c 61 67 29 3b 0a 53 51 4c 49 54  t exFlag);.SQLIT
5b190 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
5b1a0 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74  lite3PagerCommit
5b1b0 50 68 61 73 65 4f 6e 65 28 50 61 67 65 72 2a 2c  PhaseOne(Pager*,
5b1c0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73  const char *zMas
5b1d0 74 65 72 2c 20 50 67 6e 6f 2c 20 69 6e 74 29 3b  ter, Pgno, int);
5b1e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5b1f0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
5b200 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 50  CommitPhaseTwo(P
5b210 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ager*);.SQLITE_P
5b220 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
5b230 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28  e3PagerRollback(
5b240 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  Pager*);.SQLITE_
5b250 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
5b260 74 65 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e  te3PagerIsreadon
5b270 6c 79 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49  ly(Pager*);.SQLI
5b280 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
5b290 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d 74 42  qlite3PagerStmtB
5b2a0 65 67 69 6e 28 50 61 67 65 72 2a 29 3b 0a 53 51  egin(Pager*);.SQ
5b2b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
5b2c0 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d   sqlite3PagerStm
5b2d0 74 43 6f 6d 6d 69 74 28 50 61 67 65 72 2a 29 3b  tCommit(Pager*);
5b2e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5b2f0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
5b300 53 74 6d 74 52 6f 6c 6c 62 61 63 6b 28 50 61 67  StmtRollback(Pag
5b310 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  er*);.SQLITE_PRI
5b320 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
5b330 33 50 61 67 65 72 44 6f 6e 74 52 6f 6c 6c 62 61  3PagerDontRollba
5b340 63 6b 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c  ck(DbPage*);.SQL
5b350 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
5b360 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74  sqlite3PagerDont
5b370 57 72 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0a  Write(DbPage*);.
5b380 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5b390 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52  nt sqlite3PagerR
5b3a0 65 66 63 6f 75 6e 74 28 50 61 67 65 72 2a 29 3b  efcount(Pager*);
5b3b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5b3c0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
5b3d0 72 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28  rSetSafetyLevel(
5b3e0 50 61 67 65 72 2a 2c 69 6e 74 2c 69 6e 74 29 3b  Pager*,int,int);
5b3f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5b400 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
5b410 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d 65  te3PagerFilename
5b420 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  (Pager*);.SQLITE
5b430 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73  _PRIVATE const s
5b440 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69  qlite3_vfs *sqli
5b450 74 65 33 50 61 67 65 72 56 66 73 28 50 61 67 65  te3PagerVfs(Page
5b460 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
5b470 41 54 45 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ATE sqlite3_file
5b480 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69   *sqlite3PagerFi
5b490 6c 65 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49  le(Pager*);.SQLI
5b4a0 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
5b4b0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61   char *sqlite3Pa
5b4c0 67 65 72 44 69 72 6e 61 6d 65 28 50 61 67 65 72  gerDirname(Pager
5b4d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
5b4e0 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  TE const char *s
5b4f0 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e  qlite3PagerJourn
5b500 61 6c 6e 61 6d 65 28 50 61 67 65 72 2a 29 3b 0a  alname(Pager*);.
5b510 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5b520 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4e  nt sqlite3PagerN
5b530 6f 73 79 6e 63 28 50 61 67 65 72 2a 29 3b 0a 53  osync(Pager*);.S
5b540 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
5b550 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f  t sqlite3PagerMo
5b560 76 65 70 61 67 65 28 50 61 67 65 72 2a 2c 44 62  vepage(Pager*,Db
5b570 50 61 67 65 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b  Page*,Pgno,int);
5b580 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5b590 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67  void *sqlite3Pag
5b5a0 65 72 47 65 74 44 61 74 61 28 44 62 50 61 67 65  erGetData(DbPage
5b5b0 20 2a 29 3b 20 0a 53 51 4c 49 54 45 5f 50 52 49   *); .SQLITE_PRI
5b5c0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
5b5d0 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28  e3PagerGetExtra(
5b5e0 44 62 50 61 67 65 20 2a 29 3b 20 0a 53 51 4c 49  DbPage *); .SQLI
5b5f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
5b600 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69  qlite3PagerLocki
5b610 6e 67 4d 6f 64 65 28 50 61 67 65 72 20 2a 2c 20  ngMode(Pager *, 
5b620 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
5b630 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
5b640 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65  PagerJournalMode
5b650 28 50 61 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a  (Pager *, int);.
5b660 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5b670 36 34 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a  64 sqlite3PagerJ
5b680 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28  ournalSizeLimit(
5b690 50 61 67 65 72 20 2a 2c 20 69 36 34 29 3b 0a 53  Pager *, i64);.S
5b6a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
5b6b0 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  id *sqlite3Pager
5b6c0 54 65 6d 70 53 70 61 63 65 28 50 61 67 65 72 2a  TempSpace(Pager*
5b6d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
5b6e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
5b6f0 65 72 53 79 6e 63 28 50 61 67 65 72 20 2a 70 50  erSync(Pager *pP
5b700 61 67 65 72 29 3b 0a 0a 23 69 66 64 65 66 20 53  ager);..#ifdef S
5b710 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a  QLITE_HAS_CODEC.
5b720 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
5b730 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67   void sqlite3Pag
5b740 65 72 53 65 74 43 6f 64 65 63 28 50 61 67 65 72  erSetCodec(Pager
5b750 2a 2c 76 6f 69 64 2a 28 2a 29 28 76 6f 69 64 2a  *,void*(*)(void*
5b760 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29  ,void*,Pgno,int)
5b770 2c 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 69 66 0a  ,void*);.#endif.
5b780 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44  .#if !defined(ND
5b790 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64  EBUG) || defined
5b7a0 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 53 51  (SQLITE_TEST).SQ
5b7b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 50  LITE_PRIVATE   P
5b7c0 67 6e 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72  gno sqlite3Pager
5b7d0 50 61 67 65 6e 75 6d 62 65 72 28 44 62 50 61 67  Pagenumber(DbPag
5b7e0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
5b7f0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
5b800 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
5b810 65 28 44 62 50 61 67 65 2a 29 3b 0a 23 65 6e 64  e(DbPage*);.#end
5b820 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
5b830 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52  E_TEST.SQLITE_PR
5b840 49 56 41 54 45 20 20 20 69 6e 74 20 2a 73 71 6c  IVATE   int *sql
5b850 69 74 65 33 50 61 67 65 72 53 74 61 74 73 28 50  ite3PagerStats(P
5b860 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ager*);.SQLITE_P
5b870 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
5b880 6c 69 74 65 33 50 61 67 65 72 52 65 66 64 75 6d  lite3PagerRefdum
5b890 70 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54  p(Pager*);.SQLIT
5b8a0 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
5b8b0 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 4d 65  sqlite3PagerIsMe
5b8c0 6d 64 62 28 50 61 67 65 72 2a 29 3b 0a 23 65 6e  mdb(Pager*);.#en
5b8d0 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
5b8e0 54 45 5f 54 45 53 54 0a 76 6f 69 64 20 64 69 73  TE_TEST.void dis
5b8f0 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69  able_simulated_i
5b900 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 3b 0a  o_errors(void);.
5b910 76 6f 69 64 20 65 6e 61 62 6c 65 5f 73 69 6d 75  void enable_simu
5b920 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28  lated_io_errors(
5b930 76 6f 69 64 29 3b 0a 23 65 6c 73 65 0a 23 20 64  void);.#else.# d
5b940 65 66 69 6e 65 20 64 69 73 61 62 6c 65 5f 73 69  efine disable_si
5b950 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72  mulated_io_error
5b960 73 28 29 0a 23 20 64 65 66 69 6e 65 20 65 6e 61  s().# define ena
5b970 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f  ble_simulated_io
5b980 5f 65 72 72 6f 72 73 28 29 0a 23 65 6e 64 69 66  _errors().#endif
5b990 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 50 41 47  ..#endif /* _PAG
5b9a0 45 52 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  ER_H_ */../*****
5b9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
5b9c0 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a   pager.h *******
5b9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5b9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5b9f0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
5ba00 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
5ba10 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
5ba20 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65  ft off in sqlite
5ba30 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
5ba40 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
5ba50 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
5ba60 65 20 70 63 61 63 68 65 2e 68 20 69 6e 20 74 68  e pcache.h in th
5ba70 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69  e middle of sqli
5ba80 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
5ba90 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
5baa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
5bab0 66 69 6c 65 20 70 63 61 63 68 65 2e 68 20 2a 2a  file pcache.h **
5bac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5bad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5bae0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
5baf0 32 30 30 38 20 41 75 67 75 73 74 20 30 35 0a 2a  2008 August 05.*
5bb00 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
5bb10 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
5bb20 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
5bb30 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
5bb40 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
5bb50 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
5bb60 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
5bb70 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
5bb80 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
5bb90 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
5bba0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
5bbb0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
5bbc0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
5bbd0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
5bbe0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
5bbf0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
5bc00 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
5bc10 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
5bc20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5bc30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5bc40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5bc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
5bc60 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c   This header fil
5bc70 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e  e defines the in
5bc80 74 65 72 66 61 63 65 20 74 68 61 74 20 74 68 65  terface that the
5bc90 20 73 71 6c 69 74 65 20 70 61 67 65 20 63 61 63   sqlite page cac
5bca0 68 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e  he.** subsystem.
5bcb0 20 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64   .**.** @(#) $Id
5bcc0 3a 20 70 63 61 63 68 65 2e 68 2c 76 20 31 2e 31  : pcache.h,v 1.1
5bcd0 34 20 32 30 30 38 2f 31 30 2f 31 37 20 31 38 3a  4 2008/10/17 18:
5bce0 35 31 3a 35 33 20 64 61 6e 69 65 6c 6b 31 39 37  51:53 danielk197
5bcf0 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e  7 Exp $.*/..#ifn
5bd00 64 65 66 20 5f 50 43 41 43 48 45 5f 48 5f 0a 0a  def _PCACHE_H_..
5bd10 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50  typedef struct P
5bd20 67 48 64 72 20 50 67 48 64 72 3b 0a 74 79 70 65  gHdr PgHdr;.type
5bd30 64 65 66 20 73 74 72 75 63 74 20 50 43 61 63 68  def struct PCach
5bd40 65 20 50 43 61 63 68 65 3b 0a 0a 2f 2a 0a 2a 2a  e PCache;../*.**
5bd50 20 45 76 65 72 79 20 70 61 67 65 20 69 6e 20 74   Every page in t
5bd60 68 65 20 63 61 63 68 65 20 69 73 20 63 6f 6e 74  he cache is cont
5bd70 72 6f 6c 6c 65 64 20 62 79 20 61 6e 20 69 6e 73  rolled by an ins
5bd80 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
5bd90 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74  lowing.** struct
5bda0 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50  ure..*/.struct P
5bdb0 67 48 64 72 20 7b 0a 20 20 76 6f 69 64 20 2a 70  gHdr {.  void *p
5bdc0 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  Data;           
5bdd0 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65          /* Conte
5bde0 6e 74 20 6f 66 20 74 68 69 73 20 70 61 67 65 20  nt of this page 
5bdf0 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 45 78 74 72  */.  void *pExtr
5be00 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  a;              
5be10 20 20 20 20 2f 2a 20 45 78 74 72 61 20 63 6f 6e      /* Extra con
5be20 74 65 6e 74 20 2a 2f 0a 20 20 50 67 48 64 72 20  tent */.  PgHdr 
5be30 2a 70 44 69 72 74 79 3b 20 20 20 20 20 20 20 20  *pDirty;        
5be40 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 61 6e           /* Tran
5be50 73 69 65 6e 74 20 6c 69 73 74 20 6f 66 20 64 69  sient list of di
5be60 72 74 79 20 70 61 67 65 73 20 2a 2f 0a 20 20 50  rty pages */.  P
5be70 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20  gno pgno;       
5be80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5be90 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72   Page number for
5bea0 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20   this page */.  
5beb0 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20  Pager *pPager;  
5bec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5bed0 2a 20 54 68 65 20 70 61 67 65 72 20 74 68 69 73  * The pager this
5bee0 20 70 61 67 65 20 69 73 20 70 61 72 74 20 6f 66   page is part of
5bef0 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
5bf00 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20  E_CHECK_PAGES.  
5bf10 75 33 32 20 70 61 67 65 48 61 73 68 3b 20 20 20  u32 pageHash;   
5bf20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5bf30 2a 20 48 61 73 68 20 6f 66 20 70 61 67 65 20 63  * Hash of page c
5bf40 6f 6e 74 65 6e 74 20 2a 2f 0a 23 65 6e 64 69 66  ontent */.#endif
5bf50 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20  .  u16 flags;   
5bf60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5bf70 20 20 2f 2a 20 50 47 48 44 52 20 66 6c 61 67 73    /* PGHDR flags
5bf80 20 64 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 2a   defined below *
5bf90 2f 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /.  /***********
5bfa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5bfb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5bfc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5bfd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a  ***********.  **
5bfe0 20 45 6c 65 6d 65 6e 74 73 20 61 62 6f 76 65 20   Elements above 
5bff0 61 72 65 20 70 75 62 6c 69 63 2e 20 20 41 6c 6c  are public.  All
5c000 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 73   that follows is
5c010 20 70 72 69 76 61 74 65 20 74 6f 20 70 63 61 63   private to pcac
5c020 68 65 2e 63 0a 20 20 2a 2a 20 61 6e 64 20 73 68  he.c.  ** and sh
5c030 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 63 63 65  ould not be acce
5c040 73 73 65 64 20 62 79 20 6f 74 68 65 72 20 6d 6f  ssed by other mo
5c050 64 75 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 31  dules..  */.  i1
5c060 36 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20  6 nRef;         
5c070 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5c080 4e 75 6d 62 65 72 20 6f 66 20 75 73 65 72 73 20  Number of users 
5c090 6f 66 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a  of this page */.
5c0a0 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65    PCache *pCache
5c0b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5c0c0 20 2f 2a 20 43 61 63 68 65 20 74 68 61 74 20 6f   /* Cache that o
5c0d0 77 6e 73 20 74 68 69 73 20 70 61 67 65 20 2a 2f  wns this page */
5c0e0 0a 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..  /***********
5c0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5c100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5c110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5c120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a  ***********.  **
5c130 20 45 6c 65 6d 65 6e 74 73 20 61 62 6f 76 65 20   Elements above 
5c140 61 72 65 20 61 63 63 65 73 73 69 62 6c 65 20 61  are accessible a
5c150 74 20 61 6e 79 20 74 69 6d 65 20 62 79 20 74 68  t any time by th
5c160 65 20 6f 77 6e 65 72 20 6f 66 20 74 68 65 20 63  e owner of the c
5c170 61 63 68 65 0a 20 20 2a 2a 20 77 69 74 68 6f 75  ache.  ** withou
5c180 74 20 74 68 65 20 6e 65 65 64 20 66 6f 72 20 61  t the need for a
5c190 20 6d 75 74 65 78 2e 20 20 54 68 65 20 65 6c 65   mutex.  The ele
5c1a0 6d 65 6e 74 73 20 74 68 61 74 20 66 6f 6c 6c 6f  ments that follo
5c1b0 77 20 63 61 6e 20 6f 6e 6c 79 20 62 65 0a 20 20  w can only be.  
5c1c0 2a 2a 20 61 63 63 65 73 73 65 64 20 77 68 69 6c  ** accessed whil
5c1d0 65 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 53 51  e holding the SQ
5c1e0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
5c1f0 43 5f 4c 52 55 20 6d 75 74 65 78 2e 0a 20 20 2a  C_LRU mutex..  *
5c200 2f 0a 20 20 50 67 48 64 72 20 2a 70 4e 65 78 74  /.  PgHdr *pNext
5c210 48 61 73 68 2c 20 2a 70 50 72 65 76 48 61 73 68  Hash, *pPrevHash
5c220 3b 20 20 2f 2a 20 48 61 73 68 20 63 6f 6c 6c 69  ;  /* Hash colli
5c230 73 69 6f 6e 20 63 68 61 69 6e 20 66 6f 72 20 50  sion chain for P
5c240 67 48 64 72 2e 70 67 6e 6f 20 2a 2f 0a 20 20 50  gHdr.pgno */.  P
5c250 67 48 64 72 20 2a 70 4e 65 78 74 2c 20 2a 70 50  gHdr *pNext, *pP
5c260 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  rev;          /*
5c270 20 4c 69 73 74 20 6f 66 20 63 6c 65 61 6e 20 6f   List of clean o
5c280 72 20 64 69 72 74 79 20 70 61 67 65 73 20 2a 2f  r dirty pages */
5c290 0a 20 20 50 67 48 64 72 20 2a 70 4e 65 78 74 4c  .  PgHdr *pNextL
5c2a0 72 75 2c 20 2a 70 50 72 65 76 4c 72 75 3b 20 20  ru, *pPrevLru;  
5c2b0 20 20 2f 2a 20 50 61 72 74 20 6f 66 20 67 6c 6f    /* Part of glo
5c2c0 62 61 6c 20 4c 52 55 20 6c 69 73 74 20 2a 2f 0a  bal LRU list */.
5c2d0 7d 3b 0a 0a 2f 2a 20 42 69 74 20 76 61 6c 75 65  };../* Bit value
5c2e0 73 20 66 6f 72 20 50 67 48 64 72 2e 66 6c 61 67  s for PgHdr.flag
5c2f0 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48  s */.#define PGH
5c300 44 52 5f 49 4e 5f 4a 4f 55 52 4e 41 4c 20 20 20  DR_IN_JOURNAL   
5c310 20 20 20 20 20 30 78 30 30 31 20 20 2f 2a 20 50       0x001  /* P
5c320 61 67 65 20 69 73 20 69 6e 20 72 6f 6c 6c 62 61  age is in rollba
5c330 63 6b 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64  ck journal */.#d
5c340 65 66 69 6e 65 20 50 47 48 44 52 5f 44 49 52 54  efine PGHDR_DIRT
5c350 59 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78  Y             0x
5c360 30 30 32 20 20 2f 2a 20 50 61 67 65 20 68 61 73  002  /* Page has
5c370 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65 66   changed */.#def
5c380 69 6e 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53  ine PGHDR_NEED_S
5c390 59 4e 43 20 20 20 20 20 20 20 20 20 30 78 30 30  YNC         0x00
5c3a0 34 20 20 2f 2a 20 46 73 79 6e 63 20 74 68 65 20  4  /* Fsync the 
5c3b0 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
5c3c0 20 62 65 66 6f 72 65 0a 20 20 20 20 20 20 20 20   before.        
5c3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
5c3f0 2a 20 77 72 69 74 69 6e 67 20 74 68 69 73 20 70  * writing this p
5c400 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 62  age to the datab
5c410 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ase */.#define P
5c420 47 48 44 52 5f 4e 45 45 44 5f 52 45 41 44 20 20  GHDR_NEED_READ  
5c430 20 20 20 20 20 20 20 30 78 30 30 38 20 20 2f 2a         0x008  /*
5c440 20 43 6f 6e 74 65 6e 74 20 69 73 20 75 6e 72 65   Content is unre
5c450 61 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47  ad */.#define PG
5c460 48 44 52 5f 52 45 55 53 45 5f 55 4e 4c 49 4b 45  HDR_REUSE_UNLIKE
5c470 4c 59 20 20 20 20 30 78 30 31 30 20 20 2f 2a 20  LY    0x010  /* 
5c480 41 20 68 69 6e 74 20 74 68 61 74 20 72 65 75 73  A hint that reus
5c490 65 20 69 73 20 75 6e 6c 69 6b 65 6c 79 20 2a 2f  e is unlikely */
5c4a0 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f 44  .#define PGHDR_D
5c4b0 4f 4e 54 5f 57 52 49 54 45 20 20 20 20 20 20 20  ONT_WRITE       
5c4c0 20 30 78 30 32 30 20 20 2f 2a 20 44 6f 20 6e 6f   0x020  /* Do no
5c4d0 74 20 77 72 69 74 65 20 63 6f 6e 74 65 6e 74 20  t write content 
5c4e0 74 6f 20 64 69 73 6b 20 2a 2f 0a 0a 2f 2a 20 49  to disk */../* I
5c4f0 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 73 68  nitialize and sh
5c500 75 74 64 6f 77 6e 20 74 68 65 20 70 61 67 65 20  utdown the page 
5c510 63 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 20  cache subsystem 
5c520 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
5c530 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61  E int sqlite3Pca
5c540 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 76 6f  cheInitialize(vo
5c550 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  id);.SQLITE_PRIV
5c560 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
5c570 50 63 61 63 68 65 53 68 75 74 64 6f 77 6e 28 76  PcacheShutdown(v
5c580 6f 69 64 29 3b 0a 0a 2f 2a 20 50 61 67 65 20 63  oid);../* Page c
5c590 61 63 68 65 20 62 75 66 66 65 72 20 6d 61 6e 61  ache buffer mana
5c5a0 67 65 6d 65 6e 74 3a 0a 2a 2a 20 54 68 65 73 65  gement:.** These
5c5b0 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d   routines implem
5c5c0 65 6e 74 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ent SQLITE_CONFI
5c5d0 47 5f 50 41 47 45 43 41 43 48 45 2e 0a 2a 2f 0a  G_PAGECACHE..*/.
5c5e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
5c5f0 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 68  oid sqlite3PCach
5c600 65 42 75 66 66 65 72 53 65 74 75 70 28 76 6f 69  eBufferSetup(voi
5c610 64 20 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74  d *, int sz, int
5c620 20 6e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   n);.SQLITE_PRIV
5c630 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
5c640 33 50 43 61 63 68 65 4d 61 6c 6c 6f 63 28 69 6e  3PCacheMalloc(in
5c650 74 20 73 7a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  t sz);.SQLITE_PR
5c660 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
5c670 65 33 50 43 61 63 68 65 46 72 65 65 28 76 6f 69  e3PCacheFree(voi
5c680 64 2a 29 3b 0a 0a 2f 2a 20 43 72 65 61 74 65 20  d*);../* Create 
5c690 61 20 6e 65 77 20 70 61 67 65 72 20 63 61 63 68  a new pager cach
5c6a0 65 2e 0a 2a 2a 20 55 6e 64 65 72 20 6d 65 6d 6f  e..** Under memo
5c6b0 72 79 20 73 74 72 65 73 73 2c 20 69 6e 76 6f 6b  ry stress, invok
5c6c0 65 20 78 53 74 72 65 73 73 20 74 6f 20 74 72 79  e xStress to try
5c6d0 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 63   to make pages c
5c6e0 6c 65 61 6e 2e 0a 2a 2a 20 4f 6e 6c 79 20 63 6c  lean..** Only cl
5c6f0 65 61 6e 20 61 6e 64 20 75 6e 70 69 6e 6e 65 64  ean and unpinned
5c700 20 70 61 67 65 73 20 63 61 6e 20 62 65 20 72 65   pages can be re
5c710 63 6c 61 69 6d 65 64 2e 0a 2a 2f 0a 53 51 4c 49  claimed..*/.SQLI
5c720 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
5c730 73 71 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65  sqlite3PcacheOpe
5c740 6e 28 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 2c  n(.  int szPage,
5c750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c760 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65      /* Size of e
5c770 76 65 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69  very page */.  i
5c780 6e 74 20 73 7a 45 78 74 72 61 2c 20 20 20 20 20  nt szExtra,     
5c790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5c7a0 20 45 78 74 72 61 20 73 70 61 63 65 20 61 73 73   Extra space ass
5c7b0 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63  ociated with eac
5c7c0 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  h page */.  int 
5c7d0 62 50 75 72 67 65 61 62 6c 65 2c 20 20 20 20 20  bPurgeable,     
5c7e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
5c7f0 75 65 20 69 66 20 70 61 67 65 73 20 61 72 65 20  ue if pages are 
5c800 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f 72 65  on backing store
5c810 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 74 72   */.  int (*xStr
5c820 65 73 73 29 28 76 6f 69 64 2a 2c 20 50 67 48 64  ess)(void*, PgHd
5c830 72 2a 29 2c 20 2f 2a 20 43 61 6c 6c 20 74 6f 20  r*), /* Call to 
5c840 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65  try to make page
5c850 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f 69  s clean */.  voi
5c860 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 20 20  d *pStress,     
5c870 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
5c880 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65  rgument to xStre
5c890 73 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a  ss */.  PCache *
5c8a0 70 54 6f 49 6e 69 74 20 20 20 20 20 20 20 20 20  pToInit         
5c8b0 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c         /* Preall
5c8c0 6f 63 61 74 65 64 20 73 70 61 63 65 20 66 6f 72  ocated space for
5c8d0 20 74 68 65 20 50 43 61 63 68 65 20 2a 2f 0a 29   the PCache */.)
5c8e0 3b 0a 0a 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65  ;../* Modify the
5c8f0 20 70 61 67 65 2d 73 69 7a 65 20 61 66 74 65 72   page-size after
5c900 20 74 68 65 20 63 61 63 68 65 20 68 61 73 20 62   the cache has b
5c910 65 65 6e 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a  een created. */.
5c920 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
5c930 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
5c940 65 53 65 74 50 61 67 65 53 69 7a 65 28 50 43 61  eSetPageSize(PCa
5c950 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a  che *, int);../*
5c960 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65   Return the size
5c970 20 69 6e 20 62 79 74 65 73 20 6f 66 20 61 20 50   in bytes of a P
5c980 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 20 55  Cache object.  U
5c990 73 65 64 20 74 6f 20 70 72 65 61 6c 6c 6f 63 61  sed to prealloca
5c9a0 74 65 0a 2a 2a 20 73 74 6f 72 61 67 65 20 73 70  te.** storage sp
5c9b0 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ace..*/.SQLITE_P
5c9c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
5c9d0 65 33 50 63 61 63 68 65 53 69 7a 65 28 76 6f 69  e3PcacheSize(voi
5c9e0 64 29 3b 0a 0a 2f 2a 20 4f 6e 65 20 72 65 6c 65  d);../* One rele
5c9f0 61 73 65 20 70 65 72 20 73 75 63 63 65 73 73 66  ase per successf
5ca00 75 6c 20 66 65 74 63 68 2e 20 20 50 61 67 65 20  ul fetch.  Page 
5ca10 69 73 20 70 69 6e 6e 65 64 20 75 6e 74 69 6c 20  is pinned until 
5ca20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 20 52 65 66  released..** Ref
5ca30 65 72 65 6e 63 65 20 63 6f 75 6e 74 65 64 2e 20  erence counted. 
5ca40 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
5ca50 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63  TE int sqlite3Pc
5ca60 61 63 68 65 46 65 74 63 68 28 50 43 61 63 68 65  acheFetch(PCache
5ca70 2a 2c 20 50 67 6e 6f 2c 20 69 6e 74 20 63 72 65  *, Pgno, int cre
5ca80 61 74 65 46 6c 61 67 2c 20 50 67 48 64 72 2a 2a  ateFlag, PgHdr**
5ca90 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
5caa0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
5cab0 61 63 68 65 52 65 6c 65 61 73 65 28 50 67 48 64  acheRelease(PgHd
5cac0 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49  r*);..SQLITE_PRI
5cad0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
5cae0 33 50 63 61 63 68 65 44 72 6f 70 28 50 67 48 64  3PcacheDrop(PgHd
5caf0 72 2a 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  r*);         /* 
5cb00 52 65 6d 6f 76 65 20 70 61 67 65 20 66 72 6f 6d  Remove page from
5cb10 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45   cache */.SQLITE
5cb20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
5cb30 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44  lite3PcacheMakeD
5cb40 69 72 74 79 28 50 67 48 64 72 2a 29 3b 20 20 20  irty(PgHdr*);   
5cb50 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 61   /* Make sure pa
5cb60 67 65 20 69 73 20 6d 61 72 6b 65 64 20 64 69 72  ge is marked dir
5cb70 74 79 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  ty */.SQLITE_PRI
5cb80 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
5cb90 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e  3PcacheMakeClean
5cba0 28 50 67 48 64 72 2a 29 3b 20 20 20 20 2f 2a 20  (PgHdr*);    /* 
5cbb0 4d 61 72 6b 20 61 20 73 69 6e 67 6c 65 20 70 61  Mark a single pa
5cbc0 67 65 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a 53  ge as clean */.S
5cbd0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
5cbe0 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
5cbf0 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 2a  CleanAll(PCache*
5cc00 29 3b 20 20 20 20 2f 2a 20 4d 61 72 6b 20 61 6c  );    /* Mark al
5cc10 6c 20 64 69 72 74 79 20 6c 69 73 74 20 70 61 67  l dirty list pag
5cc20 65 73 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a 0a  es as clean */..
5cc30 2f 2a 20 43 68 61 6e 67 65 20 61 20 70 61 67 65  /* Change a page
5cc40 20 6e 75 6d 62 65 72 2e 20 20 55 73 65 64 20 62   number.  Used b
5cc50 79 20 69 6e 63 72 2d 76 61 63 75 75 6d 2e 20 2a  y incr-vacuum. *
5cc60 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
5cc70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
5cc80 63 68 65 4d 6f 76 65 28 50 67 48 64 72 2a 2c 20  cheMove(PgHdr*, 
5cc90 50 67 6e 6f 29 3b 0a 0a 2f 2a 20 52 65 6d 6f 76  Pgno);../* Remov
5cca0 65 20 61 6c 6c 20 70 61 67 65 73 20 77 69 74 68  e all pages with
5ccb0 20 70 67 6e 6f 3e 78 2e 20 20 52 65 73 65 74 20   pgno>x.  Reset 
5ccc0 74 68 65 20 63 61 63 68 65 20 69 66 20 78 3d 3d  the cache if x==
5ccd0 30 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  0 */.SQLITE_PRIV
5cce0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
5ccf0 50 63 61 63 68 65 54 72 75 6e 63 61 74 65 28 50  PcacheTruncate(P
5cd00 43 61 63 68 65 2a 2c 20 50 67 6e 6f 20 78 29 3b  Cache*, Pgno x);
5cd10 0a 0a 2f 2a 20 47 65 74 20 61 20 6c 69 73 74 20  ../* Get a list 
5cd20 6f 66 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67  of all dirty pag
5cd30 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c  es in the cache,
5cd40 20 73 6f 72 74 65 64 20 62 79 20 70 61 67 65 20   sorted by page 
5cd50 6e 75 6d 62 65 72 20 2a 2f 0a 53 51 4c 49 54 45  number */.SQLITE
5cd60 5f 50 52 49 56 41 54 45 20 50 67 48 64 72 20 2a  _PRIVATE PgHdr *
5cd70 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 69 72  sqlite3PcacheDir
5cd80 74 79 4c 69 73 74 28 50 43 61 63 68 65 2a 29 3b  tyList(PCache*);
5cd90 0a 0a 2f 2a 20 52 65 73 65 74 20 61 6e 64 20 63  ../* Reset and c
5cda0 6c 6f 73 65 20 74 68 65 20 63 61 63 68 65 20 6f  lose the cache o
5cdb0 62 6a 65 63 74 20 2a 2f 0a 53 51 4c 49 54 45 5f  bject */.SQLITE_
5cdc0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
5cdd0 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28  ite3PcacheClose(
5cde0 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 43 6c  PCache*);../* Cl
5cdf0 65 61 72 20 66 6c 61 67 73 20 66 72 6f 6d 20 70  ear flags from p
5ce00 61 67 65 73 20 6f 66 20 74 68 65 20 70 61 67 65  ages of the page
5ce10 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45   cache */.SQLITE
5ce20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
5ce30 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72  lite3PcacheClear
5ce40 46 6c 61 67 73 28 50 43 61 63 68 65 2a 2c 20 69  Flags(PCache*, i
5ce50 6e 74 20 6d 61 73 6b 29 3b 0a 0a 2f 2a 20 41 73  nt mask);../* As
5ce60 73 65 72 74 20 66 6c 61 67 73 20 73 65 74 74 69  sert flags setti
5ce70 6e 67 73 20 6f 6e 20 61 6c 6c 20 70 61 67 65 73  ngs on all pages
5ce80 2e 20 20 44 65 62 75 67 67 69 6e 67 20 6f 6e 6c  .  Debugging onl
5ce90 79 20 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45  y */.#ifndef NDE
5cea0 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  BUG.SQLITE_PRIVA
5ceb0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
5cec0 33 50 63 61 63 68 65 41 73 73 65 72 74 46 6c 61  3PcacheAssertFla
5ced0 67 73 28 50 43 61 63 68 65 2a 2c 20 69 6e 74 20  gs(PCache*, int 
5cee0 74 72 75 65 4d 61 73 6b 2c 20 69 6e 74 20 66 61  trueMask, int fa
5cef0 6c 73 65 4d 61 73 6b 29 3b 0a 23 65 6c 73 65 0a  lseMask);.#else.
5cf00 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
5cf10 50 63 61 63 68 65 41 73 73 65 72 74 46 6c 61 67  PcacheAssertFlag
5cf20 73 28 41 2c 42 2c 43 29 0a 23 65 6e 64 69 66 0a  s(A,B,C).#endif.
5cf30 0a 2f 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  ./* Return true 
5cf40 69 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  if the number of
5cf50 20 64 69 72 74 79 20 70 61 67 65 73 20 69 73 20   dirty pages is 
5cf60 30 20 6f 72 20 31 20 2a 2f 0a 53 51 4c 49 54 45  0 or 1 */.SQLITE
5cf70 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
5cf80 69 74 65 33 50 63 61 63 68 65 5a 65 72 6f 4f 72  ite3PcacheZeroOr
5cf90 4f 6e 65 44 69 72 74 79 50 61 67 65 73 28 50 43  OneDirtyPages(PC
5cfa0 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 44 69 73 63  ache*);../* Disc
5cfb0 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ard the contents
5cfc0 20 6f 66 20 74 68 65 20 63 61 63 68 65 20 2a 2f   of the cache */
5cfd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5cfe0 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68  int sqlite3Pcach
5cff0 65 43 6c 65 61 72 28 50 43 61 63 68 65 2a 29 3b  eClear(PCache*);
5d000 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ../* Return the 
5d010 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
5d020 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65  outstanding page
5d030 20 72 65 66 65 72 65 6e 63 65 73 20 2a 2f 0a 53   references */.S
5d040 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
5d050 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52  t sqlite3PcacheR
5d060 65 66 43 6f 75 6e 74 28 50 43 61 63 68 65 2a 29  efCount(PCache*)
5d070 3b 0a 0a 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20  ;../* Increment 
5d080 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f  the reference co
5d090 75 6e 74 20 6f 66 20 61 6e 20 65 78 69 73 74 69  unt of an existi
5d0a0 6e 67 20 70 61 67 65 20 2a 2f 0a 53 51 4c 49 54  ng page */.SQLIT
5d0b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
5d0c0 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 28  qlite3PcacheRef(
5d0d0 50 67 48 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45  PgHdr*);..SQLITE
5d0e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
5d0f0 69 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65  ite3PcachePageRe
5d100 66 63 6f 75 6e 74 28 50 67 48 64 72 2a 29 3b 0a  fcount(PgHdr*);.
5d110 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74  ./* Return the t
5d120 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70  otal number of p
5d130 61 67 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74  ages stored in t
5d140 68 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49  he cache */.SQLI
5d150 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
5d160 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65  qlite3PcachePage
5d170 63 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a  count(PCache*);.
5d180 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43  .#ifdef SQLITE_C
5d190 48 45 43 4b 5f 50 41 47 45 53 0a 2f 2a 20 49 74  HECK_PAGES./* It
5d1a0 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 61 6c  erate through al
5d1b0 6c 20 70 61 67 65 73 20 63 75 72 72 65 6e 74 6c  l pages currentl
5d1c0 79 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  y stored in the 
5d1d0 63 61 63 68 65 2e 20 54 68 69 73 20 69 6e 74 65  cache. This inte
5d1e0 72 66 61 63 65 0a 2a 2a 20 69 73 20 6f 6e 6c 79  rface.** is only
5d1f0 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 53 51   available if SQ
5d200 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53  LITE_CHECK_PAGES
5d210 20 69 73 20 64 65 66 69 6e 65 64 20 77 68 65 6e   is defined when
5d220 20 74 68 65 20 6c 69 62 72 61 72 79 20 69 73 20   the library is 
5d230 0a 2a 2a 20 62 75 69 6c 74 2e 0a 2a 2f 0a 53 51  .** built..*/.SQ
5d240 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
5d250 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49  d sqlite3PcacheI
5d260 74 65 72 61 74 65 28 50 43 61 63 68 65 20 2a 70  terate(PCache *p
5d270 43 61 63 68 65 2c 20 76 6f 69 64 20 28 2a 78 49  Cache, void (*xI
5d280 74 65 72 29 28 50 67 48 64 72 20 2a 29 29 3b 0a  ter)(PgHdr *));.
5d290 23 65 6e 64 69 66 0a 0a 2f 2a 20 53 65 74 20 61  #endif../* Set a
5d2a0 6e 64 20 67 65 74 20 74 68 65 20 73 75 67 67 65  nd get the sugge
5d2b0 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20  sted cache-size 
5d2c0 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65  for the specifie
5d2d0 64 20 70 61 67 65 72 2d 63 61 63 68 65 2e 0a 2a  d pager-cache..*
5d2e0 2a 0a 2a 2a 20 49 66 20 6e 6f 20 67 6c 6f 62 61  *.** If no globa
5d2f0 6c 20 6d 61 78 69 6d 75 6d 20 69 73 20 63 6f 6e  l maximum is con
5d300 66 69 67 75 72 65 64 2c 20 74 68 65 6e 20 74 68  figured, then th
5d310 65 20 73 79 73 74 65 6d 20 61 74 74 65 6d 70 74  e system attempt
5d320 73 20 74 6f 20 6c 69 6d 69 74 0a 2a 2a 20 74 68  s to limit.** th
5d330 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
5d340 66 20 70 61 67 65 73 20 63 61 63 68 65 64 20 62  f pages cached b
5d350 79 20 70 75 72 67 65 61 62 6c 65 20 70 61 67 65  y purgeable page
5d360 72 2d 63 61 63 68 65 73 20 74 6f 20 74 68 65 20  r-caches to the 
5d370 73 75 6d 0a 2a 2a 20 6f 66 20 74 68 65 20 73 75  sum.** of the su
5d380 67 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 69  ggested cache-si
5d390 7a 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  zes..*/.SQLITE_P
5d3a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
5d3b0 65 33 50 63 61 63 68 65 47 65 74 43 61 63 68 65  e3PcacheGetCache
5d3c0 73 69 7a 65 28 50 43 61 63 68 65 20 2a 29 3b 0a  size(PCache *);.
5d3d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
5d3e0 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
5d3f0 65 53 65 74 43 61 63 68 65 73 69 7a 65 28 50 43  eSetCachesize(PC
5d400 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 0a 23  ache *, int);..#
5d410 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
5d420 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47  BLE_MEMORY_MANAG
5d430 45 4d 45 4e 54 0a 2f 2a 20 54 72 79 20 74 6f 20  EMENT./* Try to 
5d440 72 65 74 75 72 6e 20 6d 65 6d 6f 72 79 20 75 73  return memory us
5d450 65 64 20 62 79 20 74 68 65 20 70 63 61 63 68 65  ed by the pcache
5d460 20 6d 6f 64 75 6c 65 20 74 6f 20 74 68 65 20 6d   module to the m
5d470 61 69 6e 20 6d 65 6d 6f 72 79 20 68 65 61 70 20  ain memory heap 
5d480 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
5d490 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61  E int sqlite3Pca
5d4a0 63 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79  cheReleaseMemory
5d4b0 28 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 0a 23  (int);.#endif..#
5d4c0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
5d4d0 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
5d4e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
5d4f0 63 68 65 53 74 61 74 73 28 69 6e 74 2a 2c 69 6e  cheStats(int*,in
5d500 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a 23  t*,int*,int*);.#
5d510 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a  endif..#endif /*
5d520 20 5f 50 43 41 43 48 45 5f 48 5f 20 2a 2f 0a 0a   _PCACHE_H_ */..
5d530 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
5d540 45 6e 64 20 6f 66 20 70 63 61 63 68 65 2e 68 20  End of pcache.h 
5d550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5d560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5d570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
5d580 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
5d590 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
5d5a0 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
5d5b0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
5d5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
5d5d0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
5d5e0 20 49 6e 63 6c 75 64 65 20 6f 73 2e 68 20 69 6e   Include os.h in
5d5f0 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73   the middle of s
5d600 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a  qliteInt.h *****
5d610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
5d620 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
5d630 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 2e 68   Begin file os.h
5d640 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
5d650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5d660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
5d670 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
5d680 65 6d 62 65 72 20 31 36 0a 2a 2a 0a 2a 2a 20 54  ember 16.**.** T
5d690 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
5d6a0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
5d6b0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
5d6c0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
5d6d0 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
5d6e0 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
5d6f0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
5d700 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
5d710 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
5d720 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
5d730 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
5d740 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
5d750 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
5d760 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
5d770 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
5d780 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
5d790 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
5d7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5d7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5d7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5d7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5d7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
5d7f0 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66  ** This header f
5d800 69 6c 65 20 28 74 6f 67 65 74 68 65 72 20 77 69  ile (together wi
5d810 74 68 20 69 73 20 63 6f 6d 70 61 6e 69 6f 6e 20  th is companion 
5d820 43 20 73 6f 75 72 63 65 2d 63 6f 64 65 20 66 69  C source-code fi
5d830 6c 65 0a 2a 2a 20 22 6f 73 2e 63 22 29 20 61 74  le.** "os.c") at
5d840 74 65 6d 70 74 20 74 6f 20 61 62 73 74 72 61 63  tempt to abstrac
5d850 74 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  t the underlying
5d860 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
5d870 6d 20 73 6f 20 74 68 61 74 0a 2a 2a 20 74 68 65  m so that.** the
5d880 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20   SQLite library 
5d890 77 69 6c 6c 20 77 6f 72 6b 20 6f 6e 20 62 6f 74  will work on bot
5d8a0 68 20 50 4f 53 49 58 20 61 6e 64 20 77 69 6e 64  h POSIX and wind
5d8b0 6f 77 73 20 73 79 73 74 65 6d 73 2e 0a 2a 2a 0a  ows systems..**.
5d8c0 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66  ** This header f
5d8d0 69 6c 65 20 69 73 20 23 69 6e 63 6c 75 64 65 2d  ile is #include-
5d8e0 65 64 20 62 79 20 73 71 6c 69 74 65 49 6e 74 2e  ed by sqliteInt.
5d8f0 68 20 61 6e 64 20 74 68 75 73 20 65 6e 64 73 20  h and thus ends 
5d900 75 70 0a 2a 2a 20 62 65 69 6e 67 20 69 6e 63 6c  up.** being incl
5d910 75 64 65 64 20 62 79 20 65 76 65 72 79 20 73 6f  uded by every so
5d920 75 72 63 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  urce file..**.**
5d930 20 24 49 64 3a 20 6f 73 2e 68 2c 76 20 31 2e 31   $Id: os.h,v 1.1
5d940 30 35 20 32 30 30 38 2f 30 36 2f 32 36 20 31 30  05 2008/06/26 10
5d950 3a 34 31 3a 31 39 20 64 61 6e 69 65 6c 6b 31 39  :41:19 danielk19
5d960 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e  77 Exp $.*/.#ifn
5d970 64 65 66 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 48  def _SQLITE_OS_H
5d980 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54  _.#define _SQLIT
5d990 45 5f 4f 53 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 46  E_OS_H_../*.** F
5d9a0 69 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20  igure out if we 
5d9b0 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68  are dealing with
5d9c0 20 55 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c 20   Unix, Windows, 
5d9d0 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a  or some other.**
5d9e0 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
5d9f0 6d 2e 20 20 41 66 74 65 72 20 74 68 65 20 66 6f  m.  After the fo
5da00 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f 66  llowing block of
5da10 20 70 72 65 70 72 6f 63 65 73 73 20 6d 61 63 72   preprocess macr
5da20 6f 73 2c 0a 2a 2a 20 61 6c 6c 20 6f 66 20 53 51  os,.** all of SQ
5da30 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 2c 20 53 51  LITE_OS_UNIX, SQ
5da40 4c 49 54 45 5f 4f 53 5f 57 49 4e 2c 20 53 51 4c  LITE_OS_WIN, SQL
5da50 49 54 45 5f 4f 53 5f 4f 53 32 2c 20 61 6e 64 20  ITE_OS_OS2, and 
5da60 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 20  SQLITE_OS_OTHER 
5da70 0a 2a 2a 20 77 69 6c 6c 20 64 65 66 69 6e 65 64  .** will defined
5da80 20 74 6f 20 65 69 74 68 65 72 20 31 20 6f 72 20   to either 1 or 
5da90 30 2e 20 20 4f 6e 65 20 6f 66 20 74 68 65 20 66  0.  One of the f
5daa0 6f 75 72 20 77 69 6c 6c 20 62 65 20 31 2e 20 20  our will be 1.  
5dab0 54 68 65 20 6f 74 68 65 72 20 0a 2a 2a 20 74 68  The other .** th
5dac0 72 65 65 20 77 69 6c 6c 20 62 65 20 30 2e 0a 2a  ree will be 0..*
5dad0 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  /.#if defined(SQ
5dae0 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0a 23  LITE_OS_OTHER).#
5daf0 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54   if SQLITE_OS_OT
5db00 48 45 52 3d 3d 31 0a 23 20 20 20 75 6e 64 65 66  HER==1.#   undef
5db10 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 0a   SQLITE_OS_UNIX.
5db20 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54  #   define SQLIT
5db30 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20  E_OS_UNIX 0.#   
5db40 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f  undef SQLITE_OS_
5db50 57 49 4e 0a 23 20 20 20 64 65 66 69 6e 65 20 53  WIN.#   define S
5db60 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23  QLITE_OS_WIN 0.#
5db70 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f     undef SQLITE_
5db80 4f 53 5f 4f 53 32 0a 23 20 20 20 64 65 66 69 6e  OS_OS2.#   defin
5db90 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20  e SQLITE_OS_OS2 
5dba0 30 0a 23 20 65 6c 73 65 0a 23 20 20 20 75 6e 64  0.# else.#   und
5dbb0 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48  ef SQLITE_OS_OTH
5dbc0 45 52 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69  ER.# endif.#endi
5dbd0 66 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  f.#if !defined(S
5dbe0 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 29 20 26  QLITE_OS_UNIX) &
5dbf0 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
5dc00 45 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 20 64 65  E_OS_OTHER).# de
5dc10 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f  fine SQLITE_OS_O
5dc20 54 48 45 52 20 30 0a 23 20 69 66 6e 64 65 66 20  THER 0.# ifndef 
5dc30 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20  SQLITE_OS_WIN.# 
5dc40 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 57 49    if defined(_WI
5dc50 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  N32) || defined(
5dc60 57 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65  WIN32) || define
5dc70 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 20 7c 7c  d(__CYGWIN__) ||
5dc80 20 64 65 66 69 6e 65 64 28 5f 5f 4d 49 4e 47 57   defined(__MINGW
5dc90 33 32 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64  32__) || defined
5dca0 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 23  (__BORLANDC__).#
5dcb0 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49       define SQLI
5dcc0 54 45 5f 4f 53 5f 57 49 4e 20 31 0a 23 20 20 20  TE_OS_WIN 1.#   
5dcd0 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
5dce0 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 20 20  OS_UNIX 0.#     
5dcf0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
5dd00 5f 4f 53 32 20 30 0a 23 20 20 20 65 6c 69 66 20  _OS2 0.#   elif 
5dd10 64 65 66 69 6e 65 64 28 5f 5f 45 4d 58 5f 5f 29  defined(__EMX__)
5dd20 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53 32   || defined(_OS2
5dd30 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 4f 53 32  ) || defined(OS2
5dd40 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53  ) || defined(_OS
5dd50 32 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  2_) || defined(_
5dd60 5f 4f 53 32 5f 5f 29 0a 23 20 20 20 20 20 64 65  _OS2__).#     de
5dd70 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57  fine SQLITE_OS_W
5dd80 49 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e  IN 0.#     defin
5dd90 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58  e SQLITE_OS_UNIX
5dda0 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20   0.#     define 
5ddb0 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 31 0a  SQLITE_OS_OS2 1.
5ddc0 23 20 20 20 65 6c 73 65 0a 23 20 20 20 20 20 64  #   else.#     d
5ddd0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f  efine SQLITE_OS_
5dde0 57 49 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69  WIN 0.#     defi
5ddf0 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49  ne SQLITE_OS_UNI
5de00 58 20 31 0a 23 20 20 20 20 20 64 65 66 69 6e 65  X 1.#     define
5de10 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 30   SQLITE_OS_OS2 0
5de20 0a 23 20 20 65 6e 64 69 66 0a 23 20 65 6c 73 65  .#  endif.# else
5de30 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54  .#  define SQLIT
5de40 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 64  E_OS_UNIX 0.#  d
5de50 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f  efine SQLITE_OS_
5de60 4f 53 32 20 30 0a 23 20 65 6e 64 69 66 0a 23 65  OS2 0.# endif.#e
5de70 6c 73 65 0a 23 20 69 66 6e 64 65 66 20 53 51 4c  lse.# ifndef SQL
5de80 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 64 65  ITE_OS_WIN.#  de
5de90 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57  fine SQLITE_OS_W
5dea0 49 4e 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 6e  IN 0.# endif.#en
5deb0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72  dif../*.** Deter
5dec0 6d 69 6e 65 20 69 66 20 77 65 20 61 72 65 20 64  mine if we are d
5ded0 65 61 6c 69 6e 67 20 77 69 74 68 20 57 69 6e 64  ealing with Wind
5dee0 6f 77 73 43 45 20 2d 20 77 68 69 63 68 20 68 61  owsCE - which ha
5def0 73 20 61 20 6d 75 63 68 0a 2a 2a 20 72 65 64 75  s a much.** redu
5df00 63 65 64 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 20  ced API..*/.#if 
5df10 64 65 66 69 6e 65 64 28 5f 57 49 4e 33 32 5f 57  defined(_WIN32_W
5df20 43 45 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  CE).# define SQL
5df30 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 31 0a 23  ITE_OS_WINCE 1.#
5df40 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51  else.# define SQ
5df50 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 30 0a  LITE_OS_WINCE 0.
5df60 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44  #endif.../*.** D
5df70 65 66 69 6e 65 20 74 68 65 20 6d 61 78 69 6d 75  efine the maximu
5df80 6d 20 73 69 7a 65 20 6f 66 20 61 20 74 65 6d 70  m size of a temp
5df90 6f 72 61 72 79 20 66 69 6c 65 6e 61 6d 65 0a 2a  orary filename.*
5dfa0 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  /.#if SQLITE_OS_
5dfb0 57 49 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 77  WIN.# include <w
5dfc0 69 6e 64 6f 77 73 2e 68 3e 0a 23 20 64 65 66 69  indows.h>.# defi
5dfd0 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41  ne SQLITE_TEMPNA
5dfe0 4d 45 5f 53 49 5a 45 20 28 4d 41 58 5f 50 41 54  ME_SIZE (MAX_PAT
5dff0 48 2b 35 30 29 0a 23 65 6c 69 66 20 53 51 4c 49  H+50).#elif SQLI
5e000 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 69 66 20 28  TE_OS_OS2.# if (
5e010 5f 5f 47 4e 55 43 5f 5f 20 3e 20 33 20 7c 7c 20  __GNUC__ > 3 || 
5e020 5f 5f 47 4e 55 43 5f 5f 20 3d 3d 20 33 20 26 26  __GNUC__ == 3 &&
5e030 20 5f 5f 47 4e 55 43 5f 4d 49 4e 4f 52 5f 5f 20   __GNUC_MINOR__ 
5e040 3e 3d 20 33 29 20 26 26 20 64 65 66 69 6e 65 64  >= 3) && defined
5e050 28 4f 53 32 5f 48 49 47 48 5f 4d 45 4d 4f 52 59  (OS2_HIGH_MEMORY
5e060 29 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 6f 73  ).#  include <os
5e070 32 73 61 66 65 2e 68 3e 20 2f 2a 20 68 61 73 20  2safe.h> /* has 
5e080 74 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 62  to be included b
5e090 65 66 6f 72 65 20 6f 73 32 2e 68 20 66 6f 72 20  efore os2.h for 
5e0a0 6c 69 6e 6b 69 6e 67 20 74 6f 20 77 6f 72 6b 20  linking to work 
5e0b0 2a 2f 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66  */.# endif.# def
5e0c0 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 44 41 54 45  ine INCL_DOSDATE
5e0d0 54 49 4d 45 0a 23 20 64 65 66 69 6e 65 20 49 4e  TIME.# define IN
5e0e0 43 4c 5f 44 4f 53 46 49 4c 45 4d 47 52 0a 23 20  CL_DOSFILEMGR.# 
5e0f0 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 45  define INCL_DOSE
5e100 52 52 4f 52 53 0a 23 20 64 65 66 69 6e 65 20 49  RRORS.# define I
5e110 4e 43 4c 5f 44 4f 53 4d 49 53 43 0a 23 20 64 65  NCL_DOSMISC.# de
5e120 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 50 52 4f  fine INCL_DOSPRO
5e130 43 45 53 53 0a 23 20 64 65 66 69 6e 65 20 49 4e  CESS.# define IN
5e140 43 4c 5f 44 4f 53 4d 4f 44 55 4c 45 4d 47 52 0a  CL_DOSMODULEMGR.
5e150 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f  # define INCL_DO
5e160 53 53 45 4d 41 50 48 4f 52 45 53 0a 23 20 69 6e  SSEMAPHORES.# in
5e170 63 6c 75 64 65 20 3c 6f 73 32 2e 68 3e 0a 23 20  clude <os2.h>.# 
5e180 69 6e 63 6c 75 64 65 20 3c 75 63 6f 6e 76 2e 68  include <uconv.h
5e190 3e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  >.# define SQLIT
5e1a0 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20  E_TEMPNAME_SIZE 
5e1b0 28 43 43 48 4d 41 58 50 41 54 48 43 4f 4d 50 29  (CCHMAXPATHCOMP)
5e1c0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
5e1d0 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f  SQLITE_TEMPNAME_
5e1e0 53 49 5a 45 20 32 30 30 0a 23 65 6e 64 69 66 0a  SIZE 200.#endif.
5e1f0 0a 2f 2a 20 49 66 20 74 68 65 20 53 45 54 5f 46  ./* If the SET_F
5e200 55 4c 4c 53 59 4e 43 20 6d 61 63 72 6f 20 69 73  ULLSYNC macro is
5e210 20 6e 6f 74 20 64 65 66 69 6e 65 64 20 61 62 6f   not defined abo
5e220 76 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 69 74  ve, then make it
5e230 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 0a 2a 2f 0a 23  .** a no-op.*/.#
5e240 69 66 6e 64 65 66 20 53 45 54 5f 46 55 4c 4c 53  ifndef SET_FULLS
5e250 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 53 45 54  YNC.# define SET
5e260 5f 46 55 4c 4c 53 59 4e 43 28 78 2c 79 29 0a 23  _FULLSYNC(x,y).#
5e270 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
5e280 20 64 65 66 61 75 6c 74 20 73 69 7a 65 20 6f 66   default size of
5e290 20 61 20 64 69 73 6b 20 73 65 63 74 6f 72 0a 2a   a disk sector.*
5e2a0 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
5e2b0 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f  _DEFAULT_SECTOR_
5e2c0 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51  SIZE.# define SQ
5e2d0 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43  LITE_DEFAULT_SEC
5e2e0 54 4f 52 5f 53 49 5a 45 20 35 31 32 0a 23 65 6e  TOR_SIZE 512.#en
5e2f0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 6d 70 6f  dif../*.** Tempo
5e300 72 61 72 79 20 66 69 6c 65 73 20 61 72 65 20 6e  rary files are n
5e310 61 6d 65 64 20 73 74 61 72 74 69 6e 67 20 77 69  amed starting wi
5e320 74 68 20 74 68 69 73 20 70 72 65 66 69 78 20 66  th this prefix f
5e330 6f 6c 6c 6f 77 65 64 20 62 79 20 31 36 20 72 61  ollowed by 16 ra
5e340 6e 64 6f 6d 0a 2a 2a 20 61 6c 70 68 61 6e 75 6d  ndom.** alphanum
5e350 65 72 69 63 20 63 68 61 72 61 63 74 65 72 73 2c  eric characters,
5e360 20 61 6e 64 20 6e 6f 20 66 69 6c 65 20 65 78 74   and no file ext
5e370 65 6e 73 69 6f 6e 2e 20 54 68 65 79 20 61 72 65  ension. They are
5e380 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 0a 2a   stored in the.*
5e390 2a 20 4f 53 27 73 20 73 74 61 6e 64 61 72 64 20  * OS's standard 
5e3a0 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 64  temporary file d
5e3b0 69 72 65 63 74 6f 72 79 2c 20 61 6e 64 20 61 72  irectory, and ar
5e3c0 65 20 64 65 6c 65 74 65 64 20 70 72 69 6f 72 20  e deleted prior 
5e3d0 74 6f 20 65 78 69 74 2e 0a 2a 2a 20 49 66 20 73  to exit..** If s
5e3e0 71 6c 69 74 65 20 69 73 20 62 65 69 6e 67 20 65  qlite is being e
5e3f0 6d 62 65 64 64 65 64 20 69 6e 20 61 6e 6f 74 68  mbedded in anoth
5e400 65 72 20 70 72 6f 67 72 61 6d 2c 20 79 6f 75 20  er program, you 
5e410 6d 61 79 20 77 69 73 68 20 74 6f 20 63 68 61 6e  may wish to chan
5e420 67 65 20 74 68 65 0a 2a 2a 20 70 72 65 66 69 78  ge the.** prefix
5e430 20 74 6f 20 72 65 66 6c 65 63 74 20 79 6f 75 72   to reflect your
5e440 20 70 72 6f 67 72 61 6d 27 73 20 6e 61 6d 65 2c   program's name,
5e450 20 73 6f 20 74 68 61 74 20 69 66 20 79 6f 75 72   so that if your
5e460 20 70 72 6f 67 72 61 6d 20 65 78 69 74 73 0a 2a   program exits.*
5e470 2a 20 70 72 65 6d 61 74 75 72 65 6c 79 2c 20 6f  * prematurely, o
5e480 6c 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c  ld temporary fil
5e490 65 73 20 63 61 6e 20 62 65 20 65 61 73 69 6c 79  es can be easily
5e4a0 20 69 64 65 6e 74 69 66 69 65 64 2e 20 54 68 69   identified. Thi
5e4b0 73 20 63 61 6e 20 62 65 20 64 6f 6e 65 0a 2a 2a  s can be done.**
5e4c0 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 5f   using -DSQLITE_
5e4d0 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58  TEMP_FILE_PREFIX
5e4e0 3d 6d 79 70 72 65 66 69 78 5f 20 6f 6e 20 74 68  =myprefix_ on th
5e4f0 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61  e compiler comma
5e500 6e 64 20 6c 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 32  nd line..**.** 2
5e510 30 30 36 2d 31 30 2d 33 31 3a 20 20 54 68 65 20  006-10-31:  The 
5e520 64 65 66 61 75 6c 74 20 70 72 65 66 69 78 20 75  default prefix u
5e530 73 65 64 20 74 6f 20 62 65 20 22 73 71 6c 69 74  sed to be "sqlit
5e540 65 5f 22 2e 20 20 42 75 74 20 74 68 65 6e 0a 2a  e_".  But then.*
5e550 2a 20 4d 63 61 66 65 65 20 73 74 61 72 74 65 64  * Mcafee started
5e560 20 75 73 69 6e 67 20 53 51 4c 69 74 65 20 69 6e   using SQLite in
5e570 20 74 68 65 69 72 20 61 6e 74 69 2d 76 69 72 75   their anti-viru
5e580 73 20 70 72 6f 64 75 63 74 20 61 6e 64 20 69 74  s product and it
5e590 0a 2a 2a 20 73 74 61 72 74 65 64 20 70 75 74 74  .** started putt
5e5a0 69 6e 67 20 66 69 6c 65 73 20 77 69 74 68 20 74  ing files with t
5e5b0 68 65 20 22 73 71 6c 69 74 65 22 20 6e 61 6d 65  he "sqlite" name
5e5c0 20 69 6e 20 74 68 65 20 63 3a 2f 74 65 6d 70 20   in the c:/temp 
5e5d0 66 6f 6c 64 65 72 2e 0a 2a 2a 20 54 68 69 73 20  folder..** This 
5e5e0 61 6e 6e 6f 79 65 64 20 6d 61 6e 79 20 77 69 6e  annoyed many win
5e5f0 64 6f 77 73 20 75 73 65 72 73 2e 20 20 54 68 6f  dows users.  Tho
5e600 73 65 20 75 73 65 72 73 20 77 6f 75 6c 64 20 74  se users would t
5e610 68 65 6e 20 64 6f 20 61 20 0a 2a 2a 20 47 6f 6f  hen do a .** Goo
5e620 67 6c 65 20 73 65 61 72 63 68 20 66 6f 72 20 22  gle search for "
5e630 73 71 6c 69 74 65 22 2c 20 66 69 6e 64 20 74 68  sqlite", find th
5e640 65 20 74 65 6c 65 70 68 6f 6e 65 20 6e 75 6d 62  e telephone numb
5e650 65 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 65  ers of the.** de
5e660 76 65 6c 6f 70 65 72 73 20 61 6e 64 20 63 61 6c  velopers and cal
5e670 6c 20 74 6f 20 77 61 6b 65 20 74 68 65 6d 20 75  l to wake them u
5e680 70 20 61 74 20 6e 69 67 68 74 20 61 6e 64 20 63  p at night and c
5e690 6f 6d 70 6c 61 69 6e 2e 0a 2a 2a 20 46 6f 72 20  omplain..** For 
5e6a0 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68 65  this reason, the
5e6b0 20 64 65 66 61 75 6c 74 20 6e 61 6d 65 20 70 72   default name pr
5e6c0 65 66 69 78 20 69 73 20 63 68 61 6e 67 65 64 20  efix is changed 
5e6d0 74 6f 20 62 65 20 22 73 71 6c 69 74 65 22 20 0a  to be "sqlite" .
5e6e0 2a 2a 20 73 70 65 6c 6c 65 64 20 62 61 63 6b 77  ** spelled backw
5e6f0 61 72 64 73 2e 20 20 53 6f 20 74 68 65 20 74 65  ards.  So the te
5e700 6d 70 20 66 69 6c 65 73 20 61 72 65 20 73 74 69  mp files are sti
5e710 6c 6c 20 69 64 65 6e 74 69 66 69 65 64 2c 20 62  ll identified, b
5e720 75 74 0a 2a 2a 20 61 6e 79 62 6f 64 79 20 73 6d  ut.** anybody sm
5e730 61 72 74 20 65 6e 6f 75 67 68 20 74 6f 20 66 69  art enough to fi
5e740 67 75 72 65 20 6f 75 74 20 74 68 65 20 63 6f 64  gure out the cod
5e750 65 20 69 73 20 61 6c 73 6f 20 6c 69 6b 65 6c 79  e is also likely
5e760 20 73 6d 61 72 74 0a 2a 2a 20 65 6e 6f 75 67 68   smart.** enough
5e770 20 74 6f 20 6b 6e 6f 77 20 74 68 61 74 20 63 61   to know that ca
5e780 6c 6c 69 6e 67 20 74 68 65 20 64 65 76 65 6c 6f  lling the develo
5e790 70 65 72 20 77 69 6c 6c 20 6e 6f 74 20 68 65 6c  per will not hel
5e7a0 70 20 67 65 74 20 72 69 64 0a 2a 2a 20 6f 66 20  p get rid.** of 
5e7b0 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66  the file..*/.#if
5e7c0 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d 50  ndef SQLITE_TEMP
5e7d0 5f 46 49 4c 45 5f 50 52 45 46 49 58 0a 23 20 64  _FILE_PREFIX.# d
5e7e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d  efine SQLITE_TEM
5e7f0 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 20 22 65  P_FILE_PREFIX "e
5e800 74 69 6c 71 73 5f 22 0a 23 65 6e 64 69 66 0a 0a  tilqs_".#endif..
5e810 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
5e820 69 6e 67 20 76 61 6c 75 65 73 20 6d 61 79 20 62  ing values may b
5e830 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
5e840 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
5e850 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 4c  to.** sqlite3OsL
5e860 6f 63 6b 28 29 2e 20 54 68 65 20 76 61 72 69 6f  ock(). The vario
5e870 75 73 20 6c 6f 63 6b 73 20 65 78 68 69 62 69 74  us locks exhibit
5e880 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
5e890 65 6d 61 6e 74 69 63 73 3a 0a 2a 2a 0a 2a 2a 20  emantics:.**.** 
5e8a0 53 48 41 52 45 44 3a 20 20 20 20 41 6e 79 20 6e  SHARED:    Any n
5e8b0 75 6d 62 65 72 20 6f 66 20 70 72 6f 63 65 73 73  umber of process
5e8c0 65 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 53 48  es may hold a SH
5e8d0 41 52 45 44 20 6c 6f 63 6b 20 73 69 6d 75 6c 74  ARED lock simult
5e8e0 61 6e 65 6f 75 73 6c 79 2e 0a 2a 2a 20 52 45 53  aneously..** RES
5e8f0 45 52 56 45 44 3a 20 20 41 20 73 69 6e 67 6c 65  ERVED:  A single
5e900 20 70 72 6f 63 65 73 73 20 6d 61 79 20 68 6f 6c   process may hol
5e910 64 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  d a RESERVED loc
5e920 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 74 0a 2a  k on a file at.*
5e930 2a 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 79  *            any
5e940 20 74 69 6d 65 2e 20 4f 74 68 65 72 20 70 72 6f   time. Other pro
5e950 63 65 73 73 65 73 20 6d 61 79 20 68 6f 6c 64 20  cesses may hold 
5e960 61 6e 64 20 6f 62 74 61 69 6e 20 6e 65 77 20 53  and obtain new S
5e970 48 41 52 45 44 20 6c 6f 63 6b 73 2e 0a 2a 2a 20  HARED locks..** 
5e980 50 45 4e 44 49 4e 47 3a 20 20 20 41 20 73 69 6e  PENDING:   A sin
5e990 67 6c 65 20 70 72 6f 63 65 73 73 20 6d 61 79 20  gle process may 
5e9a0 68 6f 6c 64 20 61 20 50 45 4e 44 49 4e 47 20 6c  hold a PENDING l
5e9b0 6f 63 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 74  ock on a file at
5e9c0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 61  .**            a
5e9d0 6e 79 20 6f 6e 65 20 74 69 6d 65 2e 20 45 78 69  ny one time. Exi
5e9e0 73 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63  sting SHARED loc
5e9f0 6b 73 20 6d 61 79 20 70 65 72 73 69 73 74 2c 20  ks may persist, 
5ea00 62 75 74 20 6e 6f 20 6e 65 77 0a 2a 2a 20 20 20  but no new.**   
5ea10 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44 20           SHARED 
5ea20 6c 6f 63 6b 73 20 6d 61 79 20 62 65 20 6f 62 74  locks may be obt
5ea30 61 69 6e 65 64 20 62 79 20 6f 74 68 65 72 20 70  ained by other p
5ea40 72 6f 63 65 73 73 65 73 2e 0a 2a 2a 20 45 58 43  rocesses..** EXC
5ea50 4c 55 53 49 56 45 3a 20 41 6e 20 45 58 43 4c 55  LUSIVE: An EXCLU
5ea60 53 49 56 45 20 6c 6f 63 6b 20 70 72 65 63 6c 75  SIVE lock preclu
5ea70 64 65 73 20 61 6c 6c 20 6f 74 68 65 72 20 6c 6f  des all other lo
5ea80 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 50 45 4e 44 49  cks..**.** PENDI
5ea90 4e 47 5f 4c 4f 43 4b 20 6d 61 79 20 6e 6f 74 20  NG_LOCK may not 
5eaa0 62 65 20 70 61 73 73 65 64 20 64 69 72 65 63 74  be passed direct
5eab0 6c 79 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 4c  ly to sqlite3OsL
5eac0 6f 63 6b 28 29 2e 20 49 6e 73 74 65 61 64 2c 20  ock(). Instead, 
5ead0 61 0a 2a 2a 20 70 72 6f 63 65 73 73 20 74 68 61  a.** process tha
5eae0 74 20 72 65 71 75 65 73 74 73 20 61 6e 20 45 58  t requests an EX
5eaf0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6d 61 79  CLUSIVE lock may
5eb00 20 61 63 74 75 61 6c 6c 79 20 6f 62 74 61 69 6e   actually obtain
5eb10 20 61 20 50 45 4e 44 49 4e 47 0a 2a 2a 20 6c 6f   a PENDING.** lo
5eb20 63 6b 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20  ck. This can be 
5eb30 75 70 67 72 61 64 65 64 20 74 6f 20 61 6e 20 45  upgraded to an E
5eb40 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 62 79  XCLUSIVE lock by
5eb50 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 63 61   a subsequent ca
5eb60 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33  ll to.** sqlite3
5eb70 4f 73 4c 6f 63 6b 28 29 2e 0a 2a 2f 0a 23 64 65  OsLock()..*/.#de
5eb80 66 69 6e 65 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20  fine NO_LOCK    
5eb90 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53       0.#define S
5eba0 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20 31  HARED_LOCK     1
5ebb0 0a 23 64 65 66 69 6e 65 20 52 45 53 45 52 56 45  .#define RESERVE
5ebc0 44 5f 4c 4f 43 4b 20 20 20 32 0a 23 64 65 66 69  D_LOCK   2.#defi
5ebd0 6e 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20  ne PENDING_LOCK 
5ebe0 20 20 20 33 0a 23 64 65 66 69 6e 65 20 45 58 43     3.#define EXC
5ebf0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 34 0a 0a  LUSIVE_LOCK  4..
5ec00 2f 2a 0a 2a 2a 20 46 69 6c 65 20 4c 6f 63 6b 69  /*.** File Locki
5ec10 6e 67 20 4e 6f 74 65 73 3a 20 20 28 4d 6f 73 74  ng Notes:  (Most
5ec20 6c 79 20 61 62 6f 75 74 20 77 69 6e 64 6f 77 73  ly about windows
5ec30 20 62 75 74 20 61 6c 73 6f 20 73 6f 6d 65 20 69   but also some i
5ec40 6e 66 6f 20 66 6f 72 20 55 6e 69 78 29 0a 2a 2a  nfo for Unix).**
5ec50 0a 2a 2a 20 57 65 20 63 61 6e 6e 6f 74 20 75 73  .** We cannot us
5ec60 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 6f  e LockFileEx() o
5ec70 72 20 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 29  r UnlockFileEx()
5ec80 20 6f 6e 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20   on Win95/98/ME 
5ec90 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 6f 73 65  because.** those
5eca0 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 6e   functions are n
5ecb0 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 53  ot available.  S
5ecc0 6f 20 77 65 20 75 73 65 20 6f 6e 6c 79 20 4c 6f  o we use only Lo
5ecd0 63 6b 46 69 6c 65 28 29 20 61 6e 64 0a 2a 2a 20  ckFile() and.** 
5ece0 55 6e 6c 6f 63 6b 46 69 6c 65 28 29 2e 0a 2a 2a  UnlockFile()..**
5ecf0 0a 2a 2a 20 4c 6f 63 6b 46 69 6c 65 28 29 20 70  .** LockFile() p
5ed00 72 65 76 65 6e 74 73 20 6e 6f 74 20 6a 75 73 74  revents not just
5ed10 20 77 72 69 74 69 6e 67 20 62 75 74 20 61 6c 73   writing but als
5ed20 6f 20 72 65 61 64 69 6e 67 20 62 79 20 6f 74 68  o reading by oth
5ed30 65 72 20 70 72 6f 63 65 73 73 65 73 2e 0a 2a 2a  er processes..**
5ed40 20 41 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 69   A SHARED_LOCK i
5ed50 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f  s obtained by lo
5ed60 63 6b 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 72  cking a single r
5ed70 61 6e 64 6f 6d 6c 79 2d 63 68 6f 73 65 6e 20 0a  andomly-chosen .
5ed80 2a 2a 20 62 79 74 65 20 6f 75 74 20 6f 66 20 61  ** byte out of a
5ed90 20 73 70 65 63 69 66 69 63 20 72 61 6e 67 65 20   specific range 
5eda0 6f 66 20 62 79 74 65 73 2e 20 54 68 65 20 6c 6f  of bytes. The lo
5edb0 63 6b 20 62 79 74 65 20 69 73 20 6f 62 74 61 69  ck byte is obtai
5edc0 6e 65 64 20 61 74 20 0a 2a 2a 20 72 61 6e 64 6f  ned at .** rando
5edd0 6d 20 73 6f 20 74 77 6f 20 73 65 70 61 72 61 74  m so two separat
5ede0 65 20 72 65 61 64 65 72 73 20 63 61 6e 20 70 72  e readers can pr
5edf0 6f 62 61 62 6c 79 20 61 63 63 65 73 73 20 74 68  obably access th
5ee00 65 20 66 69 6c 65 20 61 74 20 74 68 65 20 0a 2a  e file at the .*
5ee10 2a 20 73 61 6d 65 20 74 69 6d 65 2c 20 75 6e 6c  * same time, unl
5ee20 65 73 73 20 74 68 65 79 20 61 72 65 20 75 6e 6c  ess they are unl
5ee30 75 63 6b 79 20 61 6e 64 20 63 68 6f 6f 73 65 20  ucky and choose 
5ee40 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b 20 62 79  the same lock by
5ee50 74 65 2e 0a 2a 2a 20 41 6e 20 45 58 43 4c 55 53  te..** An EXCLUS
5ee60 49 56 45 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61  IVE_LOCK is obta
5ee70 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20  ined by locking 
5ee80 61 6c 6c 20 62 79 74 65 73 20 69 6e 20 74 68 65  all bytes in the
5ee90 20 72 61 6e 67 65 2e 0a 2a 2a 20 54 68 65 72 65   range..** There
5eea0 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 6f 6e 65   can only be one
5eeb0 20 77 72 69 74 65 72 2e 20 20 41 20 52 45 53 45   writer.  A RESE
5eec0 52 56 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62 74  RVED_LOCK is obt
5eed0 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67  ained by locking
5eee0 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 62 79 74  .** a single byt
5eef0 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 68  e of the file th
5ef00 61 74 20 69 73 20 64 65 73 69 67 6e 61 74 65 64  at is designated
5ef10 20 61 73 20 74 68 65 20 72 65 73 65 72 76 65 64   as the reserved
5ef20 20 6c 6f 63 6b 20 62 79 74 65 2e 0a 2a 2a 20 41   lock byte..** A
5ef30 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 69 73   PENDING_LOCK is
5ef40 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f 63   obtained by loc
5ef50 6b 69 6e 67 20 61 20 64 65 73 69 67 6e 61 74 65  king a designate
5ef60 64 20 62 79 74 65 20 64 69 66 66 65 72 65 6e 74  d byte different
5ef70 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 52 45 53   from.** the RES
5ef80 45 52 56 45 44 5f 4c 4f 43 4b 20 62 79 74 65 2e  ERVED_LOCK byte.
5ef90 0a 2a 2a 0a 2a 2a 20 4f 6e 20 57 69 6e 4e 54 2f  .**.** On WinNT/
5efa0 32 4b 2f 58 50 20 73 79 73 74 65 6d 73 2c 20 4c  2K/XP systems, L
5efb0 6f 63 6b 46 69 6c 65 45 78 28 29 20 61 6e 64 20  ockFileEx() and 
5efc0 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 29 20 61  UnlockFileEx() a
5efd0 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 0a 2a 2a  re available,.**
5efe0 20 77 68 69 63 68 20 6d 65 61 6e 73 20 77 65 20   which means we 
5eff0 63 61 6e 20 75 73 65 20 72 65 61 64 65 72 2f 77  can use reader/w
5f000 72 69 74 65 72 20 6c 6f 63 6b 73 2e 20 20 57 68  riter locks.  Wh
5f010 65 6e 20 72 65 61 64 65 72 2f 77 72 69 74 65 72  en reader/writer
5f020 20 6c 6f 63 6b 73 0a 2a 2a 20 61 72 65 20 75 73   locks.** are us
5f030 65 64 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 20  ed, the lock is 
5f040 70 6c 61 63 65 64 20 6f 6e 20 74 68 65 20 73 61  placed on the sa
5f050 6d 65 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65  me range of byte
5f060 73 20 74 68 61 74 20 69 73 20 75 73 65 64 0a 2a  s that is used.*
5f070 2a 20 66 6f 72 20 70 72 6f 62 61 62 69 6c 69 73  * for probabilis
5f080 74 69 63 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 57  tic locking in W
5f090 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 48 65 6e  in95/98/ME.  Hen
5f0a0 63 65 2c 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  ce, the locking 
5f0b0 73 63 68 65 6d 65 0a 2a 2a 20 77 69 6c 6c 20 73  scheme.** will s
5f0c0 75 70 70 6f 72 74 20 74 77 6f 20 6f 72 20 6d 6f  upport two or mo
5f0d0 72 65 20 57 69 6e 39 35 20 72 65 61 64 65 72 73  re Win95 readers
5f0e0 20 6f 72 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20   or two or more 
5f0f0 57 69 6e 4e 54 20 72 65 61 64 65 72 73 2e 0a 2a  WinNT readers..*
5f100 2a 20 42 75 74 20 61 20 73 69 6e 67 6c 65 20 57  * But a single W
5f110 69 6e 39 35 20 72 65 61 64 65 72 20 77 69 6c 6c  in95 reader will
5f120 20 6c 6f 63 6b 20 6f 75 74 20 61 6c 6c 20 57 69   lock out all Wi
5f130 6e 4e 54 20 72 65 61 64 65 72 73 20 61 6e 64 20  nNT readers and 
5f140 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 57 69 6e 4e  a single.** WinN
5f150 54 20 72 65 61 64 65 72 20 77 69 6c 6c 20 6c 6f  T reader will lo
5f160 63 6b 20 6f 75 74 20 61 6c 6c 20 6f 74 68 65 72  ck out all other
5f170 20 57 69 6e 39 35 20 72 65 61 64 65 72 73 2e 0a   Win95 readers..
5f180 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  **.** The follow
5f190 69 6e 67 20 23 64 65 66 69 6e 65 73 20 73 70 65  ing #defines spe
5f1a0 63 69 66 79 20 74 68 65 20 72 61 6e 67 65 20 6f  cify the range o
5f1b0 66 20 62 79 74 65 73 20 75 73 65 64 20 66 6f 72  f bytes used for
5f1c0 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 53 48 41   locking..** SHA
5f1d0 52 45 44 5f 53 49 5a 45 20 69 73 20 74 68 65 20  RED_SIZE is the 
5f1e0 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
5f1f0 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65  available in the
5f200 20 70 6f 6f 6c 20 66 72 6f 6d 20 77 68 69 63 68   pool from which
5f210 0a 2a 2a 20 61 20 72 61 6e 64 6f 6d 20 62 79 74  .** a random byt
5f220 65 20 69 73 20 73 65 6c 65 63 74 65 64 20 66 6f  e is selected fo
5f230 72 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e  r a shared lock.
5f240 20 20 54 68 65 20 70 6f 6f 6c 20 6f 66 20 62 79    The pool of by
5f250 74 65 73 20 66 6f 72 0a 2a 2a 20 73 68 61 72 65  tes for.** share
5f260 64 20 6c 6f 63 6b 73 20 62 65 67 69 6e 73 20 61  d locks begins a
5f270 74 20 53 48 41 52 45 44 5f 46 49 52 53 54 2e 20  t SHARED_FIRST. 
5f280 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64 65  .**.** These #de
5f290 66 69 6e 65 73 20 61 72 65 20 61 76 61 69 6c 61  fines are availa
5f2a0 62 6c 65 20 69 6e 20 73 71 6c 69 74 65 5f 61 75  ble in sqlite_au
5f2b0 78 2e 68 20 73 6f 20 74 68 61 74 20 61 64 61 70  x.h so that adap
5f2c0 74 6f 72 73 20 66 6f 72 0a 2a 2a 20 63 6f 6e 6e  tors for.** conn
5f2d0 65 63 74 69 6e 67 20 53 51 4c 69 74 65 20 74 6f  ecting SQLite to
5f2e0 20 6f 74 68 65 72 20 6f 70 65 72 61 74 69 6e 67   other operating
5f2f0 20 73 79 73 74 65 6d 73 20 63 61 6e 20 75 73 65   systems can use
5f300 20 74 68 65 20 73 61 6d 65 20 62 79 74 65 0a 2a   the same byte.*
5f310 2a 20 72 61 6e 67 65 73 20 66 6f 72 20 6c 6f 63  * ranges for loc
5f320 6b 69 6e 67 2e 20 20 49 6e 20 70 61 72 74 69 63  king.  In partic
5f330 75 6c 61 72 2c 20 74 68 65 20 73 61 6d 65 20 6c  ular, the same l
5f340 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20  ocking strategy 
5f350 61 6e 64 0a 2a 2a 20 62 79 74 65 20 72 61 6e 67  and.** byte rang
5f360 65 73 20 61 72 65 20 75 73 65 64 20 66 6f 72 20  es are used for 
5f370 55 6e 69 78 2e 20 20 54 68 69 73 20 6c 65 61 76  Unix.  This leav
5f380 65 73 20 6f 70 65 6e 20 74 68 65 20 70 6f 73 73  es open the poss
5f390 69 62 6c 69 74 79 20 6f 66 20 68 61 76 69 6e 67  iblity of having
5f3a0 0a 2a 2a 20 63 6c 69 65 6e 74 73 20 6f 6e 20 77  .** clients on w
5f3b0 69 6e 39 35 2c 20 77 69 6e 4e 54 2c 20 61 6e 64  in95, winNT, and
5f3c0 20 75 6e 69 78 20 61 6c 6c 20 74 61 6c 6b 69 6e   unix all talkin
5f3d0 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20 73 68  g to the same sh
5f3e0 61 72 65 64 20 66 69 6c 65 0a 2a 2a 20 61 6e 64  ared file.** and
5f3f0 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 72   all locking cor
5f400 72 65 63 74 6c 79 2e 20 20 54 6f 20 64 6f 20 73  rectly.  To do s
5f410 6f 20 77 6f 75 6c 64 20 72 65 71 75 69 72 65 20  o would require 
5f420 74 68 61 74 20 73 61 6d 62 61 20 28 6f 72 20 77  that samba (or w
5f430 68 61 74 65 76 65 72 0a 2a 2a 20 74 6f 6f 6c 20  hatever.** tool 
5f440 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 66 6f  is being used fo
5f450 72 20 66 69 6c 65 20 73 68 61 72 69 6e 67 29 20  r file sharing) 
5f460 69 6d 70 6c 65 6d 65 6e 74 73 20 6c 6f 63 6b 73  implements locks
5f470 20 63 6f 72 72 65 63 74 6c 79 20 62 65 74 77 65   correctly betwe
5f480 65 6e 0a 2a 2a 20 77 69 6e 64 6f 77 73 20 61 6e  en.** windows an
5f490 64 20 75 6e 69 78 2e 20 20 49 27 6d 20 67 75 65  d unix.  I'm gue
5f4a0 73 73 69 6e 67 20 74 68 61 74 20 69 73 6e 27 74  ssing that isn't
5f4b0 20 6c 69 6b 65 6c 79 20 74 6f 20 68 61 70 70 65   likely to happe
5f4c0 6e 2c 20 62 75 74 20 62 79 0a 2a 2a 20 75 73 69  n, but by.** usi
5f4d0 6e 67 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b  ng the same lock
5f4e0 69 6e 67 20 72 61 6e 67 65 20 77 65 20 61 72 65  ing range we are
5f4f0 20 61 74 20 6c 65 61 73 74 20 6f 70 65 6e 20 74   at least open t
5f500 6f 20 74 68 65 20 70 6f 73 73 69 62 69 6c 69 74  o the possibilit
5f510 79 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 69 6e 67  y..**.** Locking
5f520 20 69 6e 20 77 69 6e 64 6f 77 73 20 69 73 20 6d   in windows is m
5f530 61 6e 64 69 74 6f 72 79 2e 20 20 46 6f 72 20 74  anditory.  For t
5f540 68 69 73 20 72 65 61 73 6f 6e 2c 20 77 65 20 63  his reason, we c
5f550 61 6e 6e 6f 74 20 73 74 6f 72 65 0a 2a 2a 20 61  annot store.** a
5f560 63 74 75 61 6c 20 64 61 74 61 20 69 6e 20 74 68  ctual data in th
5f570 65 20 62 79 74 65 73 20 75 73 65 64 20 66 6f 72  e bytes used for
5f580 20 6c 6f 63 6b 69 6e 67 2e 20 20 54 68 65 20 70   locking.  The p
5f590 61 67 65 72 20 6e 65 76 65 72 20 61 6c 6c 6f 63  ager never alloc
5f5a0 61 74 65 73 0a 2a 2a 20 74 68 65 20 70 61 67 65  ates.** the page
5f5b0 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 6c 6f  s involved in lo
5f5c0 63 6b 69 6e 67 20 74 68 65 72 65 66 6f 72 65 2e  cking therefore.
5f5d0 20 20 53 48 41 52 45 44 5f 53 49 5a 45 20 69 73    SHARED_SIZE is
5f5e0 20 73 65 6c 65 63 74 65 64 20 73 6f 0a 2a 2a 20   selected so.** 
5f5f0 74 68 61 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 77  that all locks w
5f600 69 6c 6c 20 66 69 74 20 6f 6e 20 61 20 73 69 6e  ill fit on a sin
5f610 67 6c 65 20 70 61 67 65 20 65 76 65 6e 20 61 74  gle page even at
5f620 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 70 61 67   the minimum pag
5f630 65 20 73 69 7a 65 2e 0a 2a 2a 20 50 45 4e 44 49  e size..** PENDI
5f640 4e 47 5f 42 59 54 45 20 64 65 66 69 6e 65 73 20  NG_BYTE defines 
5f650 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
5f660 20 74 68 65 20 6c 6f 63 6b 73 2e 20 20 42 79 20   the locks.  By 
5f670 64 65 66 61 75 6c 74 20 50 45 4e 44 49 4e 47 5f  default PENDING_
5f680 42 59 54 45 0a 2a 2a 20 69 73 20 73 65 74 20 68  BYTE.** is set h
5f690 69 67 68 20 73 6f 20 74 68 61 74 20 77 65 20 64  igh so that we d
5f6a0 6f 6e 27 74 20 68 61 76 65 20 74 6f 20 61 6c 6c  on't have to all
5f6b0 6f 63 61 74 65 20 61 6e 20 75 6e 75 73 65 64 20  ocate an unused 
5f6c0 70 61 67 65 20 65 78 63 65 70 74 0a 2a 2a 20 66  page except.** f
5f6d0 6f 72 20 76 65 72 79 20 6c 61 72 67 65 20 64 61  or very large da
5f6e0 74 61 62 61 73 65 73 2e 20 20 42 75 74 20 6f 6e  tabases.  But on
5f6f0 65 20 73 68 6f 75 6c 64 20 74 65 73 74 20 74 68  e should test th
5f700 65 20 70 61 67 65 20 73 6b 69 70 70 69 6e 67 20  e page skipping 
5f710 6c 6f 67 69 63 20 0a 2a 2a 20 62 79 20 73 65 74  logic .** by set
5f720 74 69 6e 67 20 50 45 4e 44 49 4e 47 5f 42 59 54  ting PENDING_BYT
5f730 45 20 6c 6f 77 20 61 6e 64 20 72 75 6e 6e 69 6e  E low and runnin
5f740 67 20 74 68 65 20 65 6e 74 69 72 65 20 72 65 67  g the entire reg
5f750 72 65 73 73 69 6f 6e 20 73 75 69 74 65 2e 0a 2a  ression suite..*
5f760 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68  *.** Changing th
5f770 65 20 76 61 6c 75 65 20 6f 66 20 50 45 4e 44 49  e value of PENDI
5f780 4e 47 5f 42 59 54 45 20 72 65 73 75 6c 74 73 20  NG_BYTE results 
5f790 69 6e 20 61 20 73 75 62 74 6c 79 20 69 6e 63 6f  in a subtly inco
5f7a0 6d 70 61 74 69 62 6c 65 0a 2a 2a 20 66 69 6c 65  mpatible.** file
5f7b0 20 66 6f 72 6d 61 74 2e 20 20 44 65 70 65 6e 64   format.  Depend
5f7c0 69 6e 67 20 6f 6e 20 68 6f 77 20 69 74 20 69 73  ing on how it is
5f7d0 20 63 68 61 6e 67 65 64 2c 20 79 6f 75 20 6d 69   changed, you mi
5f7e0 67 68 74 20 6e 6f 74 20 6e 6f 74 69 63 65 0a 2a  ght not notice.*
5f7f0 2a 20 74 68 65 20 69 6e 63 6f 6d 70 61 74 69 62  * the incompatib
5f800 69 6c 69 74 79 20 72 69 67 68 74 20 61 77 61 79  ility right away
5f810 2c 20 65 76 65 6e 20 72 75 6e 6e 69 6e 67 20 61  , even running a
5f820 20 66 75 6c 6c 20 72 65 67 72 65 73 73 69 6f 6e   full regression
5f830 20 74 65 73 74 2e 0a 2a 2a 20 54 68 65 20 64 65   test..** The de
5f840 66 61 75 6c 74 20 6c 6f 63 61 74 69 6f 6e 20 6f  fault location o
5f850 66 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 69  f PENDING_BYTE i
5f860 73 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65  s the first byte
5f870 20 70 61 73 74 20 74 68 65 0a 2a 2a 20 31 47 42   past the.** 1GB
5f880 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2a 0a 2a 2f   boundary..**.*/
5f890 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
5f8a0 54 45 53 54 0a 23 64 65 66 69 6e 65 20 50 45 4e  TEST.#define PEN
5f8b0 44 49 4e 47 5f 42 59 54 45 20 20 20 20 20 20 30  DING_BYTE      0
5f8c0 78 34 30 30 30 30 30 30 30 20 20 2f 2a 20 46 69  x40000000  /* Fi
5f8d0 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74 68  rst byte past th
5f8e0 65 20 31 47 42 20 62 6f 75 6e 64 61 72 79 20 2a  e 1GB boundary *
5f8f0 2f 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 41  /.#else.SQLITE_A
5f900 50 49 20 65 78 74 65 72 6e 20 75 6e 73 69 67 6e  PI extern unsign
5f910 65 64 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70  ed int sqlite3_p
5f920 65 6e 64 69 6e 67 5f 62 79 74 65 3b 0a 23 64 65  ending_byte;.#de
5f930 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f 42 59 54  fine PENDING_BYT
5f940 45 20 73 71 6c 69 74 65 33 5f 70 65 6e 64 69 6e  E sqlite3_pendin
5f950 67 5f 62 79 74 65 0a 23 65 6e 64 69 66 0a 0a 23  g_byte.#endif..#
5f960 64 65 66 69 6e 65 20 52 45 53 45 52 56 45 44 5f  define RESERVED_
5f970 42 59 54 45 20 20 20 20 20 28 50 45 4e 44 49 4e  BYTE     (PENDIN
5f980 47 5f 42 59 54 45 2b 31 29 0a 23 64 65 66 69 6e  G_BYTE+1).#defin
5f990 65 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 20  e SHARED_FIRST  
5f9a0 20 20 20 20 28 50 45 4e 44 49 4e 47 5f 42 59 54      (PENDING_BYT
5f9b0 45 2b 32 29 0a 23 64 65 66 69 6e 65 20 53 48 41  E+2).#define SHA
5f9c0 52 45 44 5f 53 49 5a 45 20 20 20 20 20 20 20 35  RED_SIZE       5
5f9d0 31 30 0a 0a 2f 2a 20 0a 2a 2a 20 46 75 6e 63 74  10../* .** Funct
5f9e0 69 6f 6e 73 20 66 6f 72 20 61 63 63 65 73 73 69  ions for accessi
5f9f0 6e 67 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ng sqlite3_file 
5fa00 6d 65 74 68 6f 64 73 20 0a 2a 2f 0a 53 51 4c 49  methods .*/.SQLI
5fa10 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
5fa20 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 73 71  qlite3OsClose(sq
5fa30 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 53 51  lite3_file*);.SQ
5fa40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
5fa50 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 73   sqlite3OsRead(s
5fa60 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 76 6f  qlite3_file*, vo
5fa70 69 64 2a 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36  id*, int amt, i6
5fa80 34 20 6f 66 66 73 65 74 29 3b 0a 53 51 4c 49 54  4 offset);.SQLIT
5fa90 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
5faa0 6c 69 74 65 33 4f 73 57 72 69 74 65 28 73 71 6c  lite3OsWrite(sql
5fab0 69 74 65 33 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73  ite3_file*, cons
5fac0 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 61 6d 74  t void*, int amt
5fad0 2c 20 69 36 34 20 6f 66 66 73 65 74 29 3b 0a 53  , i64 offset);.S
5fae0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
5faf0 74 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63  t sqlite3OsTrunc
5fb00 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ate(sqlite3_file
5fb10 2a 2c 20 69 36 34 20 73 69 7a 65 29 3b 0a 53 51  *, i64 size);.SQ
5fb20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
5fb30 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 73   sqlite3OsSync(s
5fb40 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e  qlite3_file*, in
5fb50 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
5fb60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
5fb70 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33  FileSize(sqlite3
5fb80 5f 66 69 6c 65 2a 2c 20 69 36 34 20 2a 70 53 69  _file*, i64 *pSi
5fb90 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ze);.SQLITE_PRIV
5fba0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
5fbb0 73 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  sLock(sqlite3_fi
5fbc0 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  le*, int);.SQLIT
5fbd0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
5fbe0 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 71  lite3OsUnlock(sq
5fbf0 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74  lite3_file*, int
5fc00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
5fc10 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43  E int sqlite3OsC
5fc20 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
5fc30 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
5fc40 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29  d, int *pResOut)
5fc50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
5fc60 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 69   int sqlite3OsFi
5fc70 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65  leControl(sqlite
5fc80 33 5f 66 69 6c 65 2a 2c 69 6e 74 2c 76 6f 69 64  3_file*,int,void
5fc90 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
5fca0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
5fcb0 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 74  SectorSize(sqlit
5fcc0 65 33 5f 66 69 6c 65 20 2a 69 64 29 3b 0a 53 51  e3_file *id);.SQ
5fcd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
5fce0 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65   sqlite3OsDevice
5fcf0 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28  Characteristics(
5fd00 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
5fd10 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 46 75 6e 63 74  );../* .** Funct
5fd20 69 6f 6e 73 20 66 6f 72 20 61 63 63 65 73 73 69  ions for accessi
5fd30 6e 67 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6d  ng sqlite3_vfs m
5fd40 65 74 68 6f 64 73 20 0a 2a 2f 0a 53 51 4c 49 54  ethods .*/.SQLIT
5fd50 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
5fd60 6c 69 74 65 33 4f 73 4f 70 65 6e 28 73 71 6c 69  lite3OsOpen(sqli
5fd70 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74  te3_vfs *, const
5fd80 20 63 68 61 72 20 2a 2c 20 73 71 6c 69 74 65 33   char *, sqlite3
5fd90 5f 66 69 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74  _file*, int, int
5fda0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
5fdb0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
5fdc0 73 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 5f  sDelete(sqlite3_
5fdd0 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61  vfs *, const cha
5fde0 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  r *, int);.SQLIT
5fdf0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
5fe00 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 73 71  lite3OsAccess(sq
5fe10 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e  lite3_vfs *, con
5fe20 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20  st char *, int, 
5fe30 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 3b 0a 53  int *pResOut);.S
5fe40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
5fe50 74 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50  t sqlite3OsFullP
5fe60 61 74 68 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f  athname(sqlite3_
5fe70 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61  vfs *, const cha
5fe80 72 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a  r *, int, char *
5fe90 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
5fea0 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45  E_OMIT_LOAD_EXTE
5feb0 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49  NSION.SQLITE_PRI
5fec0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
5fed0 65 33 4f 73 44 6c 4f 70 65 6e 28 73 71 6c 69 74  e3OsDlOpen(sqlit
5fee0 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20  e3_vfs *, const 
5fef0 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  char *);.SQLITE_
5ff00 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
5ff10 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 28 73 71  ite3OsDlError(sq
5ff20 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74  lite3_vfs *, int
5ff30 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54  , char *);.SQLIT
5ff40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
5ff50 73 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28 73  sqlite3OsDlSym(s
5ff60 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 76 6f  qlite3_vfs *, vo
5ff70 69 64 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  id *, const char
5ff80 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
5ff90 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
5ffa0 4f 73 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65  OsDlClose(sqlite
5ffb0 33 5f 76 66 73 20 2a 2c 20 76 6f 69 64 20 2a 29  3_vfs *, void *)
5ffc0 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ;.#endif /* SQLI
5ffd0 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
5ffe0 45 4e 53 49 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45  ENSION */.SQLITE
5fff0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
60000 69 74 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73  ite3OsRandomness
60010 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20  (sqlite3_vfs *, 
60020 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51  int, char *);.SQ
60030 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
60040 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28   sqlite3OsSleep(
60050 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69  sqlite3_vfs *, i
60060 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
60070 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
60080 73 43 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c  sCurrentTime(sql
60090 69 74 65 33 5f 76 66 73 20 2a 2c 20 64 6f 75 62  ite3_vfs *, doub
600a0 6c 65 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  le*);../*.** Con
600b0 76 65 6e 69 65 6e 63 65 20 66 75 6e 63 74 69 6f  venience functio
600c0 6e 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61  ns for opening a
600d0 6e 64 20 63 6c 6f 73 69 6e 67 20 66 69 6c 65 73  nd closing files
600e0 20 75 73 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74   using .** sqlit
600f0 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 74 6f 20 6f  e3_malloc() to o
60100 62 74 61 69 6e 20 73 70 61 63 65 20 66 6f 72 20  btain space for 
60110 74 68 65 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20  the file-handle 
60120 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51  structure..*/.SQ
60130 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
60140 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61   sqlite3OsOpenMa
60150 6c 6c 6f 63 28 73 71 6c 69 74 65 33 5f 76 66 73  lloc(sqlite3_vfs
60160 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   *, const char *
60170 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  , sqlite3_file *
60180 2a 2c 20 69 6e 74 2c 69 6e 74 2a 29 3b 0a 53 51  *, int,int*);.SQ
60190 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
601a0 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46   sqlite3OsCloseF
601b0 72 65 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ree(sqlite3_file
601c0 20 2a 29 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20   *);..#endif /* 
601d0 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 20 2a 2f  _SQLITE_OS_H_ */
601e0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
601f0 2a 20 45 6e 64 20 6f 66 20 6f 73 2e 68 20 2a 2a  * End of os.h **
60200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60230 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
60240 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
60250 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
60260 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  n sqliteInt.h **
60270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60280 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
60290 2a 20 49 6e 63 6c 75 64 65 20 6d 75 74 65 78 2e  * Include mutex.
602a0 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
602b0 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  of sqliteInt.h *
602c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
602d0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
602e0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74  * Begin file mut
602f0 65 78 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ex.h ***********
60300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60320 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67  /./*.** 2007 Aug
60330 75 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65  ust 28.**.** The
60340 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
60350 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
60360 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
60370 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
60380 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
60390 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
603a0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
603b0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
603c0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
603d0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
603e0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
603f0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
60400 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
60410 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
60420 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
60430 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
60440 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
60450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60490 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
604a0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74   file contains t
604b0 68 65 20 63 6f 6d 6d 6f 6e 20 68 65 61 64 65 72  he common header
604c0 20 66 6f 72 20 61 6c 6c 20 6d 75 74 65 78 20 69   for all mutex i
604d0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a  mplementations..
604e0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 49 6e 74  ** The sqliteInt
604f0 2e 68 20 68 65 61 64 65 72 20 23 69 6e 63 6c 75  .h header #inclu
60500 64 65 73 20 74 68 69 73 20 66 69 6c 65 20 73 6f  des this file so
60510 20 74 68 61 74 20 69 74 20 69 73 20 61 76 61 69   that it is avai
60520 6c 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20  lable.** to all 
60530 73 6f 75 72 63 65 20 66 69 6c 65 73 2e 20 20 57  source files.  W
60540 65 20 62 72 65 61 6b 20 69 74 20 6f 75 74 20 69  e break it out i
60550 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b  n an effort to k
60560 65 65 70 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20  eep the code.** 
60570 62 65 74 74 65 72 20 6f 72 67 61 6e 69 7a 65 64  better organized
60580 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20 73  ..**.** NOTE:  s
60590 6f 75 72 63 65 20 66 69 6c 65 73 20 73 68 6f 75  ource files shou
605a0 6c 64 20 2a 6e 6f 74 2a 20 23 69 6e 63 6c 75 64  ld *not* #includ
605b0 65 20 74 68 69 73 20 68 65 61 64 65 72 20 66 69  e this header fi
605c0 6c 65 20 64 69 72 65 63 74 6c 79 2e 0a 2a 2a 20  le directly..** 
605d0 53 6f 75 72 63 65 20 66 69 6c 65 73 20 73 68 6f  Source files sho
605e0 75 6c 64 20 23 69 6e 63 6c 75 64 65 20 74 68 65  uld #include the
605f0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 66 69 6c   sqliteInt.h fil
60600 65 20 61 6e 64 20 6c 65 74 20 74 68 61 74 20 66  e and let that f
60610 69 6c 65 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 74  ile.** include t
60620 68 69 73 20 6f 6e 65 20 69 6e 64 69 72 65 63 74  his one indirect
60630 6c 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d  ly..**.** $Id: m
60640 75 74 65 78 2e 68 2c 76 20 31 2e 39 20 32 30 30  utex.h,v 1.9 200
60650 38 2f 31 30 2f 30 37 20 31 35 3a 32 35 3a 34 38  8/10/07 15:25:48
60660 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a   drh Exp $.*/...
60670 2f 2a 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75 74  /*.** Figure out
60680 20 77 68 61 74 20 76 65 72 73 69 6f 6e 20 6f 66   what version of
60690 20 74 68 65 20 63 6f 64 65 20 74 6f 20 75 73 65   the code to use
606a0 2e 20 20 54 68 65 20 63 68 6f 69 63 65 73 20 61  .  The choices a
606b0 72 65 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54  re.**.**   SQLIT
606c0 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 20 20 20 20  E_MUTEX_OMIT    
606d0 20 20 20 20 20 4e 6f 20 6d 75 74 65 78 20 6c 6f       No mutex lo
606e0 67 69 63 2e 20 20 4e 6f 74 20 65 76 65 6e 20 73  gic.  Not even s
606f0 74 75 62 73 2e 20 20 54 68 65 0a 2a 2a 20 20 20  tubs.  The.**   
60700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
60710 20 20 20 20 20 20 20 20 20 20 6d 75 74 65 78 65            mutexe
60720 73 20 69 6d 70 6c 65 6d 65 6e 74 69 6f 6e 20 63  s implemention c
60730 61 6e 6e 6f 74 20 62 65 20 6f 76 65 72 72 69 64  annot be overrid
60740 64 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  den.**          
60750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
60760 20 20 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65     at start-time
60770 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45  ..**.**   SQLITE
60780 5f 4d 55 54 45 58 5f 4e 4f 4f 50 20 20 20 20 20  _MUTEX_NOOP     
60790 20 20 20 20 46 6f 72 20 73 69 6e 67 6c 65 2d 74      For single-t
607a0 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 74  hreaded applicat
607b0 69 6f 6e 73 2e 20 20 4e 6f 0a 2a 2a 20 20 20 20  ions.  No.**    
607c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
607d0 20 20 20 20 20 20 20 20 20 6d 75 74 75 61 6c 20           mutual 
607e0 65 78 63 6c 75 73 69 6f 6e 20 69 73 20 70 72 6f  exclusion is pro
607f0 76 69 64 65 64 2e 20 20 42 75 74 20 74 68 69 73  vided.  But this
60800 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
60810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
60820 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 63  implementation c
60830 61 6e 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e  an be overridden
60840 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   at.**          
60850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
60860 20 20 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a     start-time..*
60870 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55  *.**   SQLITE_MU
60880 54 45 58 5f 50 54 48 52 45 41 44 53 20 20 20 20  TEX_PTHREADS    
60890 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65 61   For multi-threa
608a0 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73  ded applications
608b0 20 6f 6e 20 55 6e 69 78 2e 0a 2a 2a 0a 2a 2a 20   on Unix..**.** 
608c0 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57    SQLITE_MUTEX_W
608d0 33 32 20 20 20 20 20 20 20 20 20 20 46 6f 72 20  32          For 
608e0 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61  multi-threaded a
608f0 70 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20 57  pplications on W
60900 69 6e 33 32 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51  in32..**.**   SQ
60910 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 20 20  LITE_MUTEX_OS2  
60920 20 20 20 20 20 20 20 20 46 6f 72 20 6d 75 6c 74          For mult
60930 69 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c 69  i-threaded appli
60940 63 61 74 69 6f 6e 73 20 6f 6e 20 4f 53 2f 32 2e  cations on OS/2.
60950 0a 2a 2f 0a 23 69 66 20 21 53 51 4c 49 54 45 5f  .*/.#if !SQLITE_
60960 54 48 52 45 41 44 53 41 46 45 0a 23 20 64 65 66  THREADSAFE.# def
60970 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ine SQLITE_MUTEX
60980 5f 4f 4d 49 54 0a 23 65 6e 64 69 66 0a 23 69 66  _OMIT.#endif.#if
60990 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
609a0 46 45 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  FE && !defined(S
609b0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50  QLITE_MUTEX_NOOP
609c0 29 0a 23 20 20 69 66 20 53 51 4c 49 54 45 5f 4f  ).#  if SQLITE_O
609d0 53 5f 55 4e 49 58 0a 23 20 20 20 20 64 65 66 69  S_UNIX.#    defi
609e0 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ne SQLITE_MUTEX_
609f0 50 54 48 52 45 41 44 53 0a 23 20 20 65 6c 69 66  PTHREADS.#  elif
60a00 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23   SQLITE_OS_WIN.#
60a10 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54      define SQLIT
60a20 45 5f 4d 55 54 45 58 5f 57 33 32 0a 23 20 20 65  E_MUTEX_W32.#  e
60a30 6c 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53  lif SQLITE_OS_OS
60a40 32 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51  2.#    define SQ
60a50 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 23  LITE_MUTEX_OS2.#
60a60 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69    else.#    defi
60a70 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ne SQLITE_MUTEX_
60a80 4e 4f 4f 50 0a 23 20 20 65 6e 64 69 66 0a 23 65  NOOP.#  endif.#e
60a90 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
60aa0 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 2f  ITE_MUTEX_OMIT./
60ab0 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20  *.** If this is 
60ac0 61 20 6e 6f 2d 6f 70 20 69 6d 70 6c 65 6d 65 6e  a no-op implemen
60ad0 74 61 74 69 6f 6e 2c 20 69 6d 70 6c 65 6d 65 6e  tation, implemen
60ae0 74 20 65 76 65 72 79 74 68 69 6e 67 20 61 73 20  t everything as 
60af0 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69  macros..*/.#defi
60b00 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
60b10 5f 61 6c 6c 6f 63 28 58 29 20 20 20 20 28 28 73  _alloc(X)    ((s
60b20 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 29  qlite3_mutex*)8)
60b30 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
60b40 5f 6d 75 74 65 78 5f 66 72 65 65 28 58 29 0a 23  _mutex_free(X).#
60b50 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d  define sqlite3_m
60b60 75 74 65 78 5f 65 6e 74 65 72 28 58 29 0a 23 64  utex_enter(X).#d
60b70 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75  efine sqlite3_mu
60b80 74 65 78 5f 74 72 79 28 58 29 20 20 20 20 20 20  tex_try(X)      
60b90 53 51 4c 49 54 45 5f 4f 4b 0a 23 64 65 66 69 6e  SQLITE_OK.#defin
60ba0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
60bb0 6c 65 61 76 65 28 58 29 0a 23 64 65 66 69 6e 65  leave(X).#define
60bc0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
60bd0 65 6c 64 28 58 29 20 20 20 20 20 31 0a 23 64 65  eld(X)     1.#de
60be0 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74  fine sqlite3_mut
60bf0 65 78 5f 6e 6f 74 68 65 6c 64 28 58 29 20 20 31  ex_notheld(X)  1
60c00 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
60c10 4d 75 74 65 78 41 6c 6c 6f 63 28 58 29 20 20 20  MutexAlloc(X)   
60c20 20 20 20 28 28 73 71 6c 69 74 65 33 5f 6d 75 74     ((sqlite3_mut
60c30 65 78 2a 29 38 29 0a 23 64 65 66 69 6e 65 20 73  ex*)8).#define s
60c40 71 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28  qlite3MutexInit(
60c50 29 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  )        SQLITE_
60c60 4f 4b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  OK.#define sqlit
60c70 65 33 4d 75 74 65 78 45 6e 64 28 29 0a 23 65 6e  e3MutexEnd().#en
60c80 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53  dif /* defined(S
60c90 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 55 54 45 58  QLITE_OMIT_MUTEX
60ca0 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
60cb0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74  ***** End of mut
60cc0 65 78 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ex.h ***********
60cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60cf0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
60d00 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
60d10 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
60d20 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e  ff in sqliteInt.
60d30 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
60d40 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61  ****/.../*.** Ea
60d50 63 68 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ch database file
60d60 20 74 6f 20 62 65 20 61 63 63 65 73 73 65 64 20   to be accessed 
60d70 62 79 20 74 68 65 20 73 79 73 74 65 6d 20 69 73  by the system is
60d80 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20   an instance.** 
60d90 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
60da0 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65   structure.  The
60db0 72 65 20 61 72 65 20 6e 6f 72 6d 61 6c 6c 79 20  re are normally 
60dc0 74 77 6f 20 6f 66 20 74 68 65 73 65 20 73 74 72  two of these str
60dd0 75 63 74 75 72 65 73 0a 2a 2a 20 69 6e 20 74 68  uctures.** in th
60de0 65 20 73 71 6c 69 74 65 2e 61 44 62 5b 5d 20 61  e sqlite.aDb[] a
60df0 72 72 61 79 2e 20 20 61 44 62 5b 30 5d 20 69 73  rray.  aDb[0] is
60e00 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
60e10 73 65 20 66 69 6c 65 20 61 6e 64 0a 2a 2a 20 61  se file and.** a
60e20 44 62 5b 31 5d 20 69 73 20 74 68 65 20 64 61 74  Db[1] is the dat
60e30 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 64 20  abase file used 
60e40 74 6f 20 68 6f 6c 64 20 74 65 6d 70 6f 72 61 72  to hold temporar
60e50 79 20 74 61 62 6c 65 73 2e 20 20 41 64 64 69 74  y tables.  Addit
60e60 69 6f 6e 61 6c 0a 2a 2a 20 64 61 74 61 62 61 73  ional.** databas
60e70 65 73 20 6d 61 79 20 62 65 20 61 74 74 61 63 68  es may be attach
60e80 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 44 62  ed..*/.struct Db
60e90 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65   {.  char *zName
60ea0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  ;         /* Nam
60eb0 65 20 6f 66 20 74 68 69 73 20 64 61 74 61 62 61  e of this databa
60ec0 73 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70  se */.  Btree *p
60ed0 42 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  Bt;          /* 
60ee0 54 68 65 20 42 2a 54 72 65 65 20 73 74 72 75 63  The B*Tree struc
60ef0 74 75 72 65 20 66 6f 72 20 74 68 69 73 20 64 61  ture for this da
60f00 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20  tabase file */. 
60f10 20 75 38 20 69 6e 54 72 61 6e 73 3b 20 20 20 20   u8 inTrans;    
60f20 20 20 20 20 20 20 2f 2a 20 30 3a 20 6e 6f 74 20        /* 0: not 
60f30 77 72 69 74 61 62 6c 65 2e 20 20 31 3a 20 54 72  writable.  1: Tr
60f40 61 6e 73 61 63 74 69 6f 6e 2e 20 20 32 3a 20 43  ansaction.  2: C
60f50 68 65 63 6b 70 6f 69 6e 74 20 2a 2f 0a 20 20 75  heckpoint */.  u
60f60 38 20 73 61 66 65 74 79 5f 6c 65 76 65 6c 3b 20  8 safety_level; 
60f70 20 20 20 20 2f 2a 20 48 6f 77 20 61 67 67 72 65      /* How aggre
60f80 73 73 69 76 65 20 61 74 20 73 79 6e 63 68 69 6e  ssive at synchin
60f90 67 20 64 61 74 61 20 74 6f 20 64 69 73 6b 20 2a  g data to disk *
60fa0 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20  /.  void *pAux; 
60fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
60fc0 20 41 75 78 69 6c 69 61 72 79 20 64 61 74 61 2e   Auxiliary data.
60fd0 20 20 55 73 75 61 6c 6c 79 20 4e 55 4c 4c 20 2a    Usually NULL *
60fe0 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 72 65 65  /.  void (*xFree
60ff0 41 75 78 29 28 76 6f 69 64 2a 29 3b 20 20 2f 2a  Aux)(void*);  /*
61000 20 52 6f 75 74 69 6e 65 20 74 6f 20 66 72 65 65   Routine to free
61010 20 70 41 75 78 20 2a 2f 0a 20 20 53 63 68 65 6d   pAux */.  Schem
61020 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 20  a *pSchema;     
61030 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61  /* Pointer to da
61040 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 28 70  tabase schema (p
61050 6f 73 73 69 62 6c 79 20 73 68 61 72 65 64 29 20  ossibly shared) 
61060 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  */.};../*.** An 
61070 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
61080 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
61090 75 72 65 20 73 74 6f 72 65 73 20 61 20 64 61 74  ure stores a dat
610a0 61 62 61 73 65 20 73 63 68 65 6d 61 2e 0a 2a 2a  abase schema..**
610b0 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65  .** If there are
610c0 20 6e 6f 20 76 69 72 74 75 61 6c 20 74 61 62 6c   no virtual tabl
610d0 65 73 20 63 6f 6e 66 69 67 75 72 65 64 20 69 6e  es configured in
610e0 20 74 68 69 73 20 73 63 68 65 6d 61 2c 20 74 68   this schema, th
610f0 65 0a 2a 2a 20 53 63 68 65 6d 61 2e 64 62 20 76  e.** Schema.db v
61100 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74  ariable is set t
61110 6f 20 4e 55 4c 4c 2e 20 41 66 74 65 72 20 74 68  o NULL. After th
61120 65 20 66 69 72 73 74 20 76 69 72 74 75 61 6c 20  e first virtual 
61130 74 61 62 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65  table.** has bee
61140 6e 20 61 64 64 65 64 2c 20 69 74 20 69 73 20 73  n added, it is s
61150 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  et to point to t
61160 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
61170 65 63 74 69 6f 6e 20 0a 2a 2a 20 75 73 65 64 20  ection .** used 
61180 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 63 6f  to create the co
61190 6e 6e 65 63 74 69 6f 6e 2e 20 4f 6e 63 65 20 61  nnection. Once a
611a0 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68   virtual table h
611b0 61 73 20 62 65 65 6e 0a 2a 2a 20 61 64 64 65 64  as been.** added
611c0 20 74 6f 20 74 68 65 20 53 63 68 65 6d 61 20 73   to the Schema s
611d0 74 72 75 63 74 75 72 65 20 61 6e 64 20 74 68 65  tructure and the
611e0 20 53 63 68 65 6d 61 2e 64 62 20 76 61 72 69 61   Schema.db varia
611f0 62 6c 65 20 70 6f 70 75 6c 61 74 65 64 2c 20 0a  ble populated, .
61200 2a 2a 20 6f 6e 6c 79 20 74 68 61 74 20 64 61 74  ** only that dat
61210 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
61220 20 6d 61 79 20 75 73 65 20 74 68 65 20 53 63 68   may use the Sch
61230 65 6d 61 20 74 6f 20 70 72 65 70 61 72 65 20 0a  ema to prepare .
61240 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  ** statements..*
61250 2f 0a 73 74 72 75 63 74 20 53 63 68 65 6d 61 20  /.struct Schema 
61260 7b 0a 20 20 69 6e 74 20 73 63 68 65 6d 61 5f 63  {.  int schema_c
61270 6f 6f 6b 69 65 3b 20 20 20 2f 2a 20 44 61 74 61  ookie;   /* Data
61280 62 61 73 65 20 73 63 68 65 6d 61 20 76 65 72 73  base schema vers
61290 69 6f 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  ion number for t
612a0 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 48 61  his file */.  Ha
612b0 73 68 20 74 62 6c 48 61 73 68 3b 20 20 20 20 20  sh tblHash;     
612c0 20 20 20 2f 2a 20 41 6c 6c 20 74 61 62 6c 65 73     /* All tables
612d0 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d 65   indexed by name
612e0 20 2a 2f 0a 20 20 48 61 73 68 20 69 64 78 48 61   */.  Hash idxHa
612f0 73 68 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 6c  sh;        /* Al
61300 6c 20 28 6e 61 6d 65 64 29 20 69 6e 64 69 63 65  l (named) indice
61310 73 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d  s indexed by nam
61320 65 20 2a 2f 0a 20 20 48 61 73 68 20 74 72 69 67  e */.  Hash trig
61330 48 61 73 68 3b 20 20 20 20 20 20 20 2f 2a 20 41  Hash;       /* A
61340 6c 6c 20 74 72 69 67 67 65 72 73 20 69 6e 64 65  ll triggers inde
61350 78 65 64 20 62 79 20 6e 61 6d 65 20 2a 2f 0a 20  xed by name */. 
61360 20 48 61 73 68 20 61 46 4b 65 79 3b 20 20 20 20   Hash aFKey;    
61370 20 20 20 20 20 20 2f 2a 20 46 6f 72 65 69 67 6e        /* Foreign
61380 20 6b 65 79 73 20 69 6e 64 65 78 65 64 20 62 79   keys indexed by
61390 20 74 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20 20 54   to-table */.  T
613a0 61 62 6c 65 20 2a 70 53 65 71 54 61 62 3b 20 20  able *pSeqTab;  
613b0 20 20 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74      /* The sqlit
613c0 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65  e_sequence table
613d0 20 75 73 65 64 20 62 79 20 41 55 54 4f 49 4e 43   used by AUTOINC
613e0 52 45 4d 45 4e 54 20 2a 2f 0a 20 20 75 38 20 66  REMENT */.  u8 f
613f0 69 6c 65 5f 66 6f 72 6d 61 74 3b 20 20 20 20 20  ile_format;     
61400 20 2f 2a 20 53 63 68 65 6d 61 20 66 6f 72 6d 61   /* Schema forma
61410 74 20 76 65 72 73 69 6f 6e 20 66 6f 72 20 74 68  t version for th
61420 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20  is file */.  u8 
61430 65 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  enc;            
61440 20 20 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69    /* Text encodi
61450 6e 67 20 75 73 65 64 20 62 79 20 74 68 69 73 20  ng used by this 
61460 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 31  database */.  u1
61470 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20  6 flags;        
61480 20 20 20 2f 2a 20 46 6c 61 67 73 20 61 73 73 6f     /* Flags asso
61490 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73  ciated with this
614a0 20 73 63 68 65 6d 61 20 2a 2f 0a 20 20 69 6e 74   schema */.  int
614b0 20 63 61 63 68 65 5f 73 69 7a 65 3b 20 20 20 20   cache_size;    
614c0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70    /* Number of p
614d0 61 67 65 73 20 74 6f 20 75 73 65 20 69 6e 20 74  ages to use in t
614e0 68 65 20 63 61 63 68 65 20 2a 2f 0a 23 69 66 6e  he cache */.#ifn
614f0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
61500 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 73  VIRTUALTABLE.  s
61510 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
61520 20 20 20 20 2f 2a 20 22 4f 77 6e 65 72 22 20 63      /* "Owner" c
61530 6f 6e 6e 65 63 74 69 6f 6e 2e 20 53 65 65 20 63  onnection. See c
61540 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 20 2a 2f 0a  omment above */.
61550 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  #endif.};../*.**
61560 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 63 61   These macros ca
61570 6e 20 62 65 20 75 73 65 64 20 74 6f 20 74 65 73  n be used to tes
61580 74 2c 20 73 65 74 2c 20 6f 72 20 63 6c 65 61 72  t, set, or clear
61590 20 62 69 74 73 20 69 6e 20 74 68 65 20 0a 2a 2a   bits in the .**
615a0 20 44 62 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e   Db.flags field.
615b0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 62 48 61  .*/.#define DbHa
615c0 73 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29  sProperty(D,I,P)
615d0 20 20 20 20 20 28 28 28 44 29 2d 3e 61 44 62 5b       (((D)->aDb[
615e0 49 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67  I].pSchema->flag
615f0 73 26 28 50 29 29 3d 3d 28 50 29 29 0a 23 64 65  s&(P))==(P)).#de
61600 66 69 6e 65 20 44 62 48 61 73 41 6e 79 50 72 6f  fine DbHasAnyPro
61610 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20 28 28  perty(D,I,P)  ((
61620 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68  (D)->aDb[I].pSch
61630 65 6d 61 2d 3e 66 6c 61 67 73 26 28 50 29 29 21  ema->flags&(P))!
61640 3d 30 29 0a 23 64 65 66 69 6e 65 20 44 62 53 65  =0).#define DbSe
61650 74 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29  tProperty(D,I,P)
61660 20 20 20 20 20 28 44 29 2d 3e 61 44 62 5b 49 5d       (D)->aDb[I]
61670 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 7c  .pSchema->flags|
61680 3d 28 50 29 0a 23 64 65 66 69 6e 65 20 44 62 43  =(P).#define DbC
61690 6c 65 61 72 50 72 6f 70 65 72 74 79 28 44 2c 49  learProperty(D,I
616a0 2c 50 29 20 20 20 28 44 29 2d 3e 61 44 62 5b 49  ,P)   (D)->aDb[I
616b0 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73  ].pSchema->flags
616c0 26 3d 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c  &=~(P)../*.** Al
616d0 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72  lowed values for
616e0 20 74 68 65 20 44 42 2e 66 6c 61 67 73 20 66 69   the DB.flags fi
616f0 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 44  eld..**.** The D
61700 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 66  B_SchemaLoaded f
61710 6c 61 67 20 69 73 20 73 65 74 20 61 66 74 65 72  lag is set after
61720 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
61730 68 65 6d 61 20 68 61 73 20 62 65 65 6e 0a 2a 2a  hema has been.**
61740 20 72 65 61 64 20 69 6e 74 6f 20 69 6e 74 65 72   read into inter
61750 6e 61 6c 20 68 61 73 68 20 74 61 62 6c 65 73 2e  nal hash tables.
61760 0a 2a 2a 0a 2a 2a 20 44 42 5f 55 6e 72 65 73 65  .**.** DB_Unrese
61770 74 56 69 65 77 73 20 6d 65 61 6e 73 20 74 68 61  tViews means tha
61780 74 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 76 69  t one or more vi
61790 65 77 73 20 68 61 76 65 20 63 6f 6c 75 6d 6e 20  ews have column 
617a0 6e 61 6d 65 73 20 74 68 61 74 0a 2a 2a 20 68 61  names that.** ha
617b0 76 65 20 62 65 65 6e 20 66 69 6c 6c 65 64 20 6f  ve been filled o
617c0 75 74 2e 20 20 49 66 20 74 68 65 20 73 63 68 65  ut.  If the sche
617d0 6d 61 20 63 68 61 6e 67 65 73 2c 20 74 68 65 73  ma changes, thes
617e0 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6d  e column names m
617f0 69 67 68 74 0a 2a 2a 20 63 68 61 6e 67 65 73 20  ight.** changes 
61800 61 6e 64 20 73 6f 20 74 68 65 20 76 69 65 77 20  and so the view 
61810 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20 62 65 20  will need to be 
61820 72 65 73 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e  reset..*/.#defin
61830 65 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65  e DB_SchemaLoade
61840 64 20 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20  d    0x0001  /* 
61850 54 68 65 20 73 63 68 65 6d 61 20 68 61 73 20 62  The schema has b
61860 65 65 6e 20 6c 6f 61 64 65 64 20 2a 2f 0a 23 64  een loaded */.#d
61870 65 66 69 6e 65 20 44 42 5f 55 6e 72 65 73 65 74  efine DB_Unreset
61880 56 69 65 77 73 20 20 20 20 30 78 30 30 30 32 20  Views    0x0002 
61890 20 2f 2a 20 53 6f 6d 65 20 76 69 65 77 73 20 68   /* Some views h
618a0 61 76 65 20 64 65 66 69 6e 65 64 20 63 6f 6c 75  ave defined colu
618b0 6d 6e 20 6e 61 6d 65 73 20 2a 2f 0a 23 64 65 66  mn names */.#def
618c0 69 6e 65 20 44 42 5f 45 6d 70 74 79 20 20 20 20  ine DB_Empty    
618d0 20 20 20 20 20 20 20 30 78 30 30 30 34 20 20 2f         0x0004  /
618e0 2a 20 54 68 65 20 66 69 6c 65 20 69 73 20 65 6d  * The file is em
618f0 70 74 79 20 28 6c 65 6e 67 74 68 20 30 20 62 79  pty (length 0 by
61900 74 65 73 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  tes) */../*.** T
61910 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 66  he number of dif
61920 66 65 72 65 6e 74 20 6b 69 6e 64 73 20 6f 66 20  ferent kinds of 
61930 74 68 69 6e 67 73 20 74 68 61 74 20 63 61 6e 20  things that can 
61940 62 65 20 6c 69 6d 69 74 65 64 0a 2a 2a 20 75 73  be limited.** us
61950 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ing the sqlite3_
61960 6c 69 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63  limit() interfac
61970 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  e..*/.#define SQ
61980 4c 49 54 45 5f 4e 5f 4c 49 4d 49 54 20 28 53 51  LITE_N_LIMIT (SQ
61990 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41  LITE_LIMIT_VARIA
619a0 42 4c 45 5f 4e 55 4d 42 45 52 2b 31 29 0a 0a 2f  BLE_NUMBER+1)../
619b0 2a 0a 2a 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 6d  *.** Lookaside m
619c0 61 6c 6c 6f 63 20 69 73 20 61 20 73 65 74 20 6f  alloc is a set o
619d0 66 20 66 69 78 65 64 2d 73 69 7a 65 20 62 75 66  f fixed-size buf
619e0 66 65 72 73 20 74 68 61 74 20 63 61 6e 20 62 65  fers that can be
619f0 20 75 73 65 64 0a 2a 2a 20 74 6f 20 73 61 74 69   used.** to sati
61a00 73 69 66 79 20 73 6d 61 6c 6c 20 74 72 61 6e 73  sify small trans
61a10 69 65 6e 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  ient memory allo
61a20 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 73 20  cation requests 
61a30 66 6f 72 20 6f 62 6a 65 63 74 73 0a 2a 2a 20 61  for objects.** a
61a40 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
61a50 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61   particular data
61a60 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  base connection.
61a70 20 20 54 68 65 20 75 73 65 20 6f 66 0a 2a 2a 20    The use of.** 
61a80 6c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63  lookaside malloc
61a90 20 70 72 6f 76 69 64 65 73 20 61 20 73 69 67 6e   provides a sign
61aa0 69 66 69 63 61 6e 74 20 70 65 72 66 6f 72 6d 61  ificant performa
61ab0 6e 63 65 20 65 6e 68 61 6e 63 65 6d 65 6e 74 0a  nce enhancement.
61ac0 2a 2a 20 28 61 70 70 72 6f 78 20 31 30 25 29 20  ** (approx 10%) 
61ad0 62 79 20 61 76 6f 69 64 69 6e 67 20 6e 75 6d 65  by avoiding nume
61ae0 72 6f 75 73 20 6d 61 6c 6c 6f 63 2f 66 72 65 65  rous malloc/free
61af0 20 72 65 71 75 65 73 74 73 20 77 68 69 6c 65 20   requests while 
61b00 70 61 72 73 69 6e 67 0a 2a 2a 20 53 51 4c 20 73  parsing.** SQL s
61b10 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a  tatements..**.**
61b20 20 54 68 65 20 4c 6f 6f 6b 61 73 69 64 65 20 73   The Lookaside s
61b30 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 63  tructure holds c
61b40 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 69 6e 66  onfiguration inf
61b50 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
61b60 68 65 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20  he.** lookaside 
61b70 6d 61 6c 6c 6f 63 20 73 75 62 73 79 73 74 65 6d  malloc subsystem
61b80 2e 20 20 45 61 63 68 20 61 76 61 69 6c 61 62 6c  .  Each availabl
61b90 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
61ba0 69 6f 6e 20 69 6e 0a 2a 2a 20 74 68 65 20 6c 6f  ion in.** the lo
61bb0 6f 6b 61 73 69 64 65 20 73 75 62 73 79 73 74 65  okaside subsyste
61bc0 6d 20 69 73 20 73 74 6f 72 65 64 20 6f 6e 20 61  m is stored on a
61bd0 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20   linked list of 
61be0 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 0a 2a 2a  LookasideSlot.**
61bf0 20 6f 62 6a 65 63 74 73 2e 0a 2a 2f 0a 73 74 72   objects..*/.str
61c00 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 20 7b 0a  uct Lookaside {.
61c10 20 20 75 31 36 20 73 7a 3b 20 20 20 20 20 20 20    u16 sz;       
61c20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
61c30 65 20 6f 66 20 65 61 63 68 20 62 75 66 66 65 72  e of each buffer
61c40 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75   in bytes */.  u
61c50 38 20 62 45 6e 61 62 6c 65 64 3b 20 20 20 20 20  8 bEnabled;     
61c60 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
61c70 66 20 75 73 65 20 6c 6f 6f 6b 61 73 69 64 65 2e  f use lookaside.
61c80 20 20 46 61 6c 73 65 20 74 6f 20 69 67 6e 6f 72    False to ignor
61c90 65 20 69 74 20 2a 2f 0a 20 20 75 38 20 62 4d 61  e it */.  u8 bMa
61ca0 6c 6c 6f 63 65 64 3b 20 20 20 20 20 20 20 20 20  lloced;         
61cb0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 53 74    /* True if pSt
61cc0 61 72 74 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  art obtained fro
61cd0 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  m sqlite3_malloc
61ce0 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 75 74  () */.  int nOut
61cf0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
61d00 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 75 66  /* Number of buf
61d10 66 65 72 73 20 63 75 72 72 65 6e 74 6c 79 20 63  fers currently c
61d20 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a 20 20  hecked out */.  
61d30 69 6e 74 20 6d 78 4f 75 74 3b 20 20 20 20 20 20  int mxOut;      
61d40 20 20 20 20 20 20 20 20 2f 2a 20 48 69 67 68 77          /* Highw
61d50 61 74 65 72 20 6d 61 72 6b 20 66 6f 72 20 6e 4f  ater mark for nO
61d60 75 74 20 2a 2f 0a 20 20 4c 6f 6f 6b 61 73 69 64  ut */.  Lookasid
61d70 65 53 6c 6f 74 20 2a 70 46 72 65 65 3b 20 20 20  eSlot *pFree;   
61d80 2f 2a 20 4c 69 73 74 20 6f 66 20 61 76 61 69 6c  /* List of avail
61d90 61 62 6c 65 20 62 75 66 66 65 72 73 20 2a 2f 0a  able buffers */.
61da0 20 20 76 6f 69 64 20 2a 70 53 74 61 72 74 3b 20    void *pStart; 
61db0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
61dc0 73 74 20 62 79 74 65 20 6f 66 20 61 76 61 69 6c  st byte of avail
61dd0 61 62 6c 65 20 6d 65 6d 6f 72 79 20 73 70 61 63  able memory spac
61de0 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 45 6e  e */.  void *pEn
61df0 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  d;             /
61e00 2a 20 46 69 72 73 74 20 62 79 74 65 20 70 61 73  * First byte pas
61e10 74 20 65 6e 64 20 6f 66 20 61 76 61 69 6c 61 62  t end of availab
61e20 6c 65 20 73 70 61 63 65 20 2a 2f 0a 7d 3b 0a 73  le space */.};.s
61e30 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 53  truct LookasideS
61e40 6c 6f 74 20 7b 0a 20 20 4c 6f 6f 6b 61 73 69 64  lot {.  Lookasid
61e50 65 53 6c 6f 74 20 2a 70 4e 65 78 74 3b 20 20 20  eSlot *pNext;   
61e60 20 2f 2a 20 4e 65 78 74 20 62 75 66 66 65 72 20   /* Next buffer 
61e70 69 6e 20 74 68 65 20 6c 69 73 74 20 6f 66 20 66  in the list of f
61e80 72 65 65 20 62 75 66 66 65 72 73 20 2a 2f 0a 7d  ree buffers */.}
61e90 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 68 61 73 68 20  ;../*.** A hash 
61ea0 74 61 62 6c 65 20 66 6f 72 20 66 75 6e 63 74 69  table for functi
61eb0 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a  on definitions..
61ec0 2a 2a 0a 2a 2a 20 48 61 73 68 20 65 61 63 68 20  **.** Hash each 
61ed0 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72  FuncDef structur
61ee0 65 20 69 6e 74 6f 20 6f 6e 65 20 6f 66 20 74 68  e into one of th
61ef0 65 20 46 75 6e 63 44 65 66 48 61 73 68 2e 61 5b  e FuncDefHash.a[
61f00 5d 20 73 6c 6f 74 73 2e 0a 2a 2a 20 43 6f 6c 6c  ] slots..** Coll
61f10 69 73 69 6f 6e 73 20 61 72 65 20 6f 6e 20 74 68  isions are on th
61f20 65 20 46 75 6e 63 44 65 66 2e 70 48 61 73 68 20  e FuncDef.pHash 
61f30 63 68 61 69 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74  chain..*/.struct
61f40 20 46 75 6e 63 44 65 66 48 61 73 68 20 7b 0a 20   FuncDefHash {. 
61f50 20 46 75 6e 63 44 65 66 20 2a 61 5b 32 33 5d 3b   FuncDef *a[23];
61f60 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74         /* Hash t
61f70 61 62 6c 65 20 66 6f 72 20 66 75 6e 63 74 69 6f  able for functio
61f80 6e 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ns */.};../*.** 
61f90 45 61 63 68 20 64 61 74 61 62 61 73 65 20 69 73  Each database is
61fa0 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
61fb0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
61fc0 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54  ructure..**.** T
61fd0 68 65 20 73 71 6c 69 74 65 2e 6c 61 73 74 52 6f  he sqlite.lastRo
61fe0 77 69 64 20 72 65 63 6f 72 64 73 20 74 68 65 20  wid records the 
61ff0 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77 69  last insert rowi
62000 64 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 61  d generated by a
62010 6e 0a 2a 2a 20 69 6e 73 65 72 74 20 73 74 61 74  n.** insert stat
62020 65 6d 65 6e 74 2e 20 20 49 6e 73 65 72 74 73 20  ement.  Inserts 
62030 6f 6e 20 76 69 65 77 73 20 64 6f 20 6e 6f 74 20  on views do not 
62040 61 66 66 65 63 74 20 69 74 73 20 76 61 6c 75 65  affect its value
62050 2e 20 20 45 61 63 68 0a 2a 2a 20 74 72 69 67 67  .  Each.** trigg
62060 65 72 20 68 61 73 20 69 74 73 20 6f 77 6e 20 63  er has its own c
62070 6f 6e 74 65 78 74 2c 20 73 6f 20 74 68 61 74 20  ontext, so that 
62080 6c 61 73 74 52 6f 77 69 64 20 63 61 6e 20 62 65  lastRowid can be
62090 20 75 70 64 61 74 65 64 20 69 6e 73 69 64 65 0a   updated inside.
620a0 2a 2a 20 74 72 69 67 67 65 72 73 20 61 73 20 75  ** triggers as u
620b0 73 75 61 6c 2e 20 20 54 68 65 20 70 72 65 76 69  sual.  The previ
620c0 6f 75 73 20 76 61 6c 75 65 20 77 69 6c 6c 20 62  ous value will b
620d0 65 20 72 65 73 74 6f 72 65 64 20 6f 6e 63 65 20  e restored once 
620e0 74 68 65 20 74 72 69 67 67 65 72 0a 2a 2a 20 65  the trigger.** e
620f0 78 69 74 73 2e 20 20 55 70 6f 6e 20 65 6e 74 65  xits.  Upon ente
62100 72 69 6e 67 20 61 20 62 65 66 6f 72 65 20 6f 72  ring a before or
62110 20 69 6e 73 74 65 61 64 20 6f 66 20 74 72 69 67   instead of trig
62120 67 65 72 2c 20 6c 61 73 74 52 6f 77 69 64 20 69  ger, lastRowid i
62130 73 20 6e 6f 0a 2a 2a 20 6c 6f 6e 67 65 72 20 28  s no.** longer (
62140 73 69 6e 63 65 20 61 66 74 65 72 20 76 65 72 73  since after vers
62150 69 6f 6e 20 32 2e 38 2e 31 32 29 20 72 65 73 65  ion 2.8.12) rese
62160 74 20 74 6f 20 2d 31 2e 0a 2a 2a 0a 2a 2a 20 54  t to -1..**.** T
62170 68 65 20 73 71 6c 69 74 65 2e 6e 43 68 61 6e 67  he sqlite.nChang
62180 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f 75 6e 74  e does not count
62190 20 63 68 61 6e 67 65 73 20 77 69 74 68 69 6e 20   changes within 
621a0 74 72 69 67 67 65 72 73 20 61 6e 64 20 6b 65 65  triggers and kee
621b0 70 73 20 6e 6f 0a 2a 2a 20 63 6f 6e 74 65 78 74  ps no.** context
621c0 2e 20 20 49 74 20 69 73 20 72 65 73 65 74 20 61  .  It is reset a
621d0 74 20 73 74 61 72 74 20 6f 66 20 73 71 6c 69 74  t start of sqlit
621e0 65 33 5f 65 78 65 63 2e 0a 2a 2a 20 54 68 65 20  e3_exec..** The 
621f0 73 71 6c 69 74 65 2e 6c 73 43 68 61 6e 67 65 20  sqlite.lsChange 
62200 72 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 6e  represents the n
62210 75 6d 62 65 72 20 6f 66 20 63 68 61 6e 67 65 73  umber of changes
62220 20 6d 61 64 65 20 62 79 20 74 68 65 20 6c 61 73   made by the las
62230 74 0a 2a 2a 20 69 6e 73 65 72 74 2c 20 75 70 64  t.** insert, upd
62240 61 74 65 2c 20 6f 72 20 64 65 6c 65 74 65 20 73  ate, or delete s
62250 74 61 74 65 6d 65 6e 74 2e 20 20 49 74 20 72 65  tatement.  It re
62260 6d 61 69 6e 73 20 63 6f 6e 73 74 61 6e 74 20 74  mains constant t
62270 68 72 6f 75 67 68 6f 75 74 20 74 68 65 0a 2a 2a  hroughout the.**
62280 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 73 74 61   length of a sta
62290 74 65 6d 65 6e 74 20 61 6e 64 20 69 73 20 74 68  tement and is th
622a0 65 6e 20 75 70 64 61 74 65 64 20 62 79 20 4f 50  en updated by OP
622b0 5f 53 65 74 43 6f 75 6e 74 73 2e 20 20 49 74 20  _SetCounts.  It 
622c0 6b 65 65 70 73 20 61 0a 2a 2a 20 63 6f 6e 74 65  keeps a.** conte
622d0 78 74 20 73 74 61 63 6b 20 6a 75 73 74 20 6c 69  xt stack just li
622e0 6b 65 20 6c 61 73 74 52 6f 77 69 64 20 73 6f 20  ke lastRowid so 
622f0 74 68 61 74 20 74 68 65 20 63 6f 75 6e 74 20 6f  that the count o
62300 66 20 63 68 61 6e 67 65 73 0a 2a 2a 20 77 69 74  f changes.** wit
62310 68 69 6e 20 61 20 74 72 69 67 67 65 72 20 69 73  hin a trigger is
62320 20 6e 6f 74 20 73 65 65 6e 20 6f 75 74 73 69 64   not seen outsid
62330 65 20 74 68 65 20 74 72 69 67 67 65 72 2e 20 20  e the trigger.  
62340 43 68 61 6e 67 65 73 20 74 6f 20 76 69 65 77 73  Changes to views
62350 20 64 6f 20 6e 6f 74 0a 2a 2a 20 61 66 66 65 63   do not.** affec
62360 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6c  t the value of l
62370 73 43 68 61 6e 67 65 2e 0a 2a 2a 20 54 68 65 20  sChange..** The 
62380 73 71 6c 69 74 65 2e 63 73 43 68 61 6e 67 65 20  sqlite.csChange 
62390 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66 20 74  keeps track of t
623a0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 75 72  he number of cur
623b0 72 65 6e 74 20 63 68 61 6e 67 65 73 20 28 73 69  rent changes (si
623c0 6e 63 65 0a 2a 2a 20 74 68 65 20 6c 61 73 74 20  nce.** the last 
623d0 73 74 61 74 65 6d 65 6e 74 29 20 61 6e 64 20 69  statement) and i
623e0 73 20 75 73 65 64 20 74 6f 20 75 70 64 61 74 65  s used to update
623f0 20 73 71 6c 69 74 65 5f 6c 73 43 68 61 6e 67 65   sqlite_lsChange
62400 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 62  ..**.** The memb
62410 65 72 20 76 61 72 69 61 62 6c 65 73 20 73 71 6c  er variables sql
62420 69 74 65 2e 65 72 72 43 6f 64 65 2c 20 73 71 6c  ite.errCode, sql
62430 69 74 65 2e 7a 45 72 72 4d 73 67 20 61 6e 64 20  ite.zErrMsg and 
62440 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73 67 31 36  sqlite.zErrMsg16
62450 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 6d 6f  .** store the mo
62460 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20  st recent error 
62470 63 6f 64 65 20 61 6e 64 2c 20 69 66 20 61 70 70  code and, if app
62480 6c 69 63 61 62 6c 65 2c 20 73 74 72 69 6e 67 2e  licable, string.
62490 20 54 68 65 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c   The.** internal
624a0 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65   function sqlite
624b0 33 45 72 72 6f 72 28 29 20 69 73 20 75 73 65 64  3Error() is used
624c0 20 74 6f 20 73 65 74 20 74 68 65 73 65 20 76 61   to set these va
624d0 72 69 61 62 6c 65 73 0a 2a 2a 20 63 6f 6e 73 69  riables.** consi
624e0 73 74 65 6e 74 6c 79 2e 0a 2a 2f 0a 73 74 72 75  stently..*/.stru
624f0 63 74 20 73 71 6c 69 74 65 33 20 7b 0a 20 20 73  ct sqlite3 {.  s
62500 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
62510 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
62520 4f 53 20 49 6e 74 65 72 66 61 63 65 20 2a 2f 0a  OS Interface */.
62530 20 20 69 6e 74 20 6e 44 62 3b 20 20 20 20 20 20    int nDb;      
62540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62550 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 61 63  /* Number of bac
62560 6b 65 6e 64 73 20 63 75 72 72 65 6e 74 6c 79 20  kends currently 
62570 69 6e 20 75 73 65 20 2a 2f 0a 20 20 44 62 20 2a  in use */.  Db *
62580 61 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  aDb;            
62590 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c            /* All
625a0 20 62 61 63 6b 65 6e 64 73 20 2a 2f 0a 20 20 69   backends */.  i
625b0 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20  nt flags;       
625c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
625d0 4d 69 73 63 65 6c 6c 61 6e 6f 75 73 20 66 6c 61  Miscellanous fla
625e0 67 73 2e 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f  gs. See below */
625f0 0a 20 20 69 6e 74 20 6f 70 65 6e 46 6c 61 67 73  .  int openFlags
62600 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
62610 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64   /* Flags passed
62620 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 66 73 2e   to sqlite3_vfs.
62630 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74  xOpen() */.  int
62640 20 65 72 72 43 6f 64 65 3b 20 20 20 20 20 20 20   errCode;       
62650 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f             /* Mo
62660 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20  st recent error 
62670 63 6f 64 65 20 28 53 51 4c 49 54 45 5f 2a 29 20  code (SQLITE_*) 
62680 2a 2f 0a 20 20 69 6e 74 20 65 72 72 4d 61 73 6b  */.  int errMask
62690 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
626a0 20 20 20 2f 2a 20 26 20 72 65 73 75 6c 74 20 63     /* & result c
626b0 6f 64 65 73 20 77 69 74 68 20 74 68 69 73 20 62  odes with this b
626c0 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20  efore returning 
626d0 2a 2f 0a 20 20 75 38 20 61 75 74 6f 43 6f 6d 6d  */.  u8 autoComm
626e0 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  it;             
626f0 20 20 20 2f 2a 20 54 68 65 20 61 75 74 6f 2d 63     /* The auto-c
62700 6f 6d 6d 69 74 20 66 6c 61 67 2e 20 2a 2f 0a 20  ommit flag. */. 
62710 20 75 38 20 74 65 6d 70 5f 73 74 6f 72 65 3b 20   u8 temp_store; 
62720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
62730 2a 20 31 3a 20 66 69 6c 65 20 32 3a 20 6d 65 6d  * 1: file 2: mem
62740 6f 72 79 20 30 3a 20 64 65 66 61 75 6c 74 20 2a  ory 0: default *
62750 2f 0a 20 20 75 38 20 6d 61 6c 6c 6f 63 46 61 69  /.  u8 mallocFai
62760 6c 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  led;            
62770 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20    /* True if we 
62780 68 61 76 65 20 73 65 65 6e 20 61 20 6d 61 6c 6c  have seen a mall
62790 6f 63 20 66 61 69 6c 75 72 65 20 2a 2f 0a 20 20  oc failure */.  
627a0 75 38 20 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 3b  u8 dfltLockMode;
627b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
627c0 20 44 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67   Default locking
627d0 2d 6d 6f 64 65 20 66 6f 72 20 61 74 74 61 63 68  -mode for attach
627e0 65 64 20 64 62 73 20 2a 2f 0a 20 20 75 38 20 64  ed dbs */.  u8 d
627f0 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 20  fltJournalMode; 
62800 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66            /* Def
62810 61 75 6c 74 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64  ault journal mod
62820 65 20 66 6f 72 20 61 74 74 61 63 68 65 64 20 64  e for attached d
62830 62 73 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63  bs */.  signed c
62840 68 61 72 20 6e 65 78 74 41 75 74 6f 76 61 63 3b  har nextAutovac;
62850 20 20 20 20 20 20 2f 2a 20 41 75 74 6f 76 61 63        /* Autovac
62860 20 73 65 74 74 69 6e 67 20 61 66 74 65 72 20 56   setting after V
62870 41 43 55 55 4d 20 69 66 20 3e 3d 30 20 2a 2f 0a  ACUUM if >=0 */.
62880 20 20 69 6e 74 20 6e 65 78 74 50 61 67 65 73 69    int nextPagesi
62890 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
628a0 2f 2a 20 50 61 67 65 73 69 7a 65 20 61 66 74 65  /* Pagesize afte
628b0 72 20 56 41 43 55 55 4d 20 69 66 20 3e 30 20 2a  r VACUUM if >0 *
628c0 2f 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65 3b 20  /.  int nTable; 
628d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
628e0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74    /* Number of t
628f0 61 62 6c 65 73 20 69 6e 20 74 68 65 20 64 61 74  ables in the dat
62900 61 62 61 73 65 20 2a 2f 0a 20 20 43 6f 6c 6c 53  abase */.  CollS
62910 65 71 20 2a 70 44 66 6c 74 43 6f 6c 6c 3b 20 20  eq *pDfltColl;  
62920 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
62930 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6e  default collatin
62940 67 20 73 65 71 75 65 6e 63 65 20 28 42 49 4e 41  g sequence (BINA
62950 52 59 29 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73  RY) */.  i64 las
62960 74 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20  tRowid;         
62970 20 20 20 20 20 20 20 2f 2a 20 52 4f 57 49 44 20         /* ROWID 
62980 6f 66 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 69  of most recent i
62990 6e 73 65 72 74 20 28 73 65 65 20 61 62 6f 76 65  nsert (see above
629a0 29 20 2a 2f 0a 20 20 69 36 34 20 70 72 69 6f 72  ) */.  i64 prior
629b0 4e 65 77 52 6f 77 69 64 3b 20 20 20 20 20 20 20  NewRowid;       
629c0 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 72 61 6e       /* Last ran
629d0 64 6f 6d 6c 79 20 67 65 6e 65 72 61 74 65 64 20  domly generated 
629e0 52 4f 57 49 44 20 2a 2f 0a 20 20 69 6e 74 20 6d  ROWID */.  int m
629f0 61 67 69 63 3b 20 20 20 20 20 20 20 20 20 20 20  agic;           
62a00 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 67 69           /* Magi
62a10 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 64 65 74  c number for det
62a20 65 63 74 20 6c 69 62 72 61 72 79 20 6d 69 73 75  ect library misu
62a30 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61  se */.  int nCha
62a40 6e 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  nge;            
62a50 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72        /* Value r
62a60 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74  eturned by sqlit
62a70 65 33 5f 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a  e3_changes() */.
62a80 20 20 69 6e 74 20 6e 54 6f 74 61 6c 43 68 61 6e    int nTotalChan
62a90 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
62aa0 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65  /* Value returne
62ab0 64 20 62 79 20 73 71 6c 69 74 65 33 5f 74 6f 74  d by sqlite3_tot
62ac0 61 6c 5f 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a  al_changes() */.
62ad0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
62ae0 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20  *mutex;         
62af0 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75  /* Connection mu
62b00 74 65 78 20 2a 2f 0a 20 20 69 6e 74 20 61 4c 69  tex */.  int aLi
62b10 6d 69 74 5b 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d  mit[SQLITE_N_LIM
62b20 49 54 5d 3b 20 20 20 2f 2a 20 4c 69 6d 69 74 73  IT];   /* Limits
62b30 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 71 6c   */.  struct sql
62b40 69 74 65 33 49 6e 69 74 49 6e 66 6f 20 7b 20 20  ite3InitInfo {  
62b50 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69      /* Informati
62b60 6f 6e 20 75 73 65 64 20 64 75 72 69 6e 67 20 69  on used during i
62b70 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f  nitialization */
62b80 0a 20 20 20 20 69 6e 74 20 69 44 62 3b 20 20 20  .    int iDb;   
62b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62ba0 20 2f 2a 20 57 68 65 6e 20 62 61 63 6b 20 69 73   /* When back is
62bb0 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a   being initializ
62bc0 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 65  ed */.    int ne
62bd0 77 54 6e 75 6d 3b 20 20 20 20 20 20 20 20 20 20  wTnum;          
62be0 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 70 61 67        /* Rootpag
62bf0 65 20 6f 66 20 74 61 62 6c 65 20 62 65 69 6e 67  e of table being
62c00 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a   initialized */.
62c10 20 20 20 20 75 38 20 62 75 73 79 3b 20 20 20 20      u8 busy;    
62c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62c30 2f 2a 20 54 52 55 45 20 69 66 20 63 75 72 72 65  /* TRUE if curre
62c40 6e 74 6c 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e  ntly initializin
62c50 67 20 2a 2f 0a 20 20 7d 20 69 6e 69 74 3b 0a 20  g */.  } init;. 
62c60 20 69 6e 74 20 6e 45 78 74 65 6e 73 69 6f 6e 3b   int nExtension;
62c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
62c80 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 61 64  * Number of load
62c90 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 20 2a 2f  ed extensions */
62ca0 0a 20 20 76 6f 69 64 20 2a 2a 61 45 78 74 65 6e  .  void **aExten
62cb0 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20  sion;           
62cc0 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 73 68 61   /* Array of sha
62cd0 72 65 64 20 6c 69 62 72 61 72 61 79 20 68 61 6e  red libraray han
62ce0 64 6c 65 73 20 2a 2f 0a 20 20 73 74 72 75 63 74  dles */.  struct
62cf0 20 56 64 62 65 20 2a 70 56 64 62 65 3b 20 20 20   Vdbe *pVdbe;   
62d00 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20          /* List 
62d10 6f 66 20 61 63 74 69 76 65 20 76 69 72 74 75 61  of active virtua
62d20 6c 20 6d 61 63 68 69 6e 65 73 20 2a 2f 0a 20 20  l machines */.  
62d30 69 6e 74 20 61 63 74 69 76 65 56 64 62 65 43 6e  int activeVdbeCn
62d40 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
62d50 20 4e 75 6d 62 65 72 20 6f 66 20 76 64 62 65 73   Number of vdbes
62d60 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75   currently execu
62d70 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 77 72  ting */.  int wr
62d80 69 74 65 56 64 62 65 43 6e 74 3b 20 20 20 20 20  iteVdbeCnt;     
62d90 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
62da0 72 20 6f 66 20 61 63 74 69 76 65 20 56 44 42 45  r of active VDBE
62db0 73 20 74 68 61 74 20 61 72 65 20 77 72 69 74 69  s that are writi
62dc0 6e 67 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  ng */.  void (*x
62dd0 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e  Trace)(void*,con
62de0 73 74 20 63 68 61 72 2a 29 3b 20 20 20 20 20 20  st char*);      
62df0 20 20 2f 2a 20 54 72 61 63 65 20 66 75 6e 63 74    /* Trace funct
62e00 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ion */.  void *p
62e10 54 72 61 63 65 41 72 67 3b 20 20 20 20 20 20 20  TraceArg;       
62e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62e30 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74     /* Argument t
62e40 6f 20 74 68 65 20 74 72 61 63 65 20 66 75 6e 63  o the trace func
62e50 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28  tion */.  void (
62e60 2a 78 50 72 6f 66 69 6c 65 29 28 76 6f 69 64 2a  *xProfile)(void*
62e70 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 36 34  ,const char*,u64
62e80 29 3b 20 20 2f 2a 20 50 72 6f 66 69 6c 69 6e 67  );  /* Profiling
62e90 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76   function */.  v
62ea0 6f 69 64 20 2a 70 50 72 6f 66 69 6c 65 41 72 67  oid *pProfileArg
62eb0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
62ec0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75           /* Argu
62ed0 6d 65 6e 74 20 74 6f 20 70 72 6f 66 69 6c 65 20  ment to profile 
62ee0 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f  function */.  vo
62ef0 69 64 20 2a 70 43 6f 6d 6d 69 74 41 72 67 3b 20  id *pCommitArg; 
62f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62f10 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78  /* Argument to x
62f20 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 28 29  CommitCallback()
62f30 20 2a 2f 20 20 20 0a 20 20 69 6e 74 20 28 2a 78   */   .  int (*x
62f40 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 29 28  CommitCallback)(
62f50 76 6f 69 64 2a 29 3b 20 20 20 20 2f 2a 20 49 6e  void*);    /* In
62f60 76 6f 6b 65 64 20 61 74 20 65 76 65 72 79 20 63  voked at every c
62f70 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 6f 69 64  ommit. */.  void
62f80 20 2a 70 52 6f 6c 6c 62 61 63 6b 41 72 67 3b 20   *pRollbackArg; 
62f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
62fa0 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 52 6f   Argument to xRo
62fb0 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 28 29  llbackCallback()
62fc0 20 2a 2f 20 20 20 0a 20 20 76 6f 69 64 20 28 2a   */   .  void (*
62fd0 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63  xRollbackCallbac
62fe0 6b 29 28 76 6f 69 64 2a 29 3b 20 2f 2a 20 49 6e  k)(void*); /* In
62ff0 76 6f 6b 65 64 20 61 74 20 65 76 65 72 79 20 63  voked at every c
63000 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 6f 69 64  ommit. */.  void
63010 20 2a 70 55 70 64 61 74 65 41 72 67 3b 0a 20 20   *pUpdateArg;.  
63020 76 6f 69 64 20 28 2a 78 55 70 64 61 74 65 43 61  void (*xUpdateCa
63030 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e  llback)(void*,in
63040 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  t, const char*,c
63050 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74  onst char*,sqlit
63060 65 5f 69 6e 74 36 34 29 3b 0a 20 20 76 6f 69 64  e_int64);.  void
63070 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 29 28 76  (*xCollNeeded)(v
63080 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e  oid*,sqlite3*,in
63090 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74  t eTextRep,const
630a0 20 63 68 61 72 2a 29 3b 0a 20 20 76 6f 69 64 28   char*);.  void(
630b0 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 29 28  *xCollNeeded16)(
630c0 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69  void*,sqlite3*,i
630d0 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73  nt eTextRep,cons
630e0 74 20 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64  t void*);.  void
630f0 20 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67   *pCollNeededArg
63100 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ;.  sqlite3_valu
63110 65 20 2a 70 45 72 72 3b 20 20 20 20 20 20 20 20  e *pErr;        
63120 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74    /* Most recent
63130 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 2a   error message *
63140 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73  /.  char *zErrMs
63150 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
63160 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74    /* Most recent
63170 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 28   error message (
63180 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 29 20 2a  UTF-8 encoded) *
63190 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73  /.  char *zErrMs
631a0 67 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20  g16;            
631b0 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74    /* Most recent
631c0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 28   error message (
631d0 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 29 20  UTF-16 encoded) 
631e0 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20  */.  union {.   
631f0 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 69 73   volatile int is
63200 49 6e 74 65 72 72 75 70 74 65 64 3b 20 2f 2a 20  Interrupted; /* 
63210 54 72 75 65 20 69 66 20 73 71 6c 69 74 65 33 5f  True if sqlite3_
63220 69 6e 74 65 72 72 75 70 74 20 68 61 73 20 62 65  interrupt has be
63230 65 6e 20 63 61 6c 6c 65 64 20 2a 2f 0a 20 20 20  en called */.   
63240 20 64 6f 75 62 6c 65 20 6e 6f 74 55 73 65 64 31   double notUsed1
63250 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
63260 53 70 61 63 65 72 20 2a 2f 0a 20 20 7d 20 75 31  Spacer */.  } u1
63270 3b 0a 20 20 4c 6f 6f 6b 61 73 69 64 65 20 6c 6f  ;.  Lookaside lo
63280 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20 20 20  okaside;        
63290 20 20 2f 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 6d    /* Lookaside m
632a0 61 6c 6c 6f 63 20 63 6f 6e 66 69 67 75 72 61 74  alloc configurat
632b0 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ion */.#ifndef S
632c0 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
632d0 52 49 5a 41 54 49 4f 4e 0a 20 20 69 6e 74 20 28  RIZATION.  int (
632e0 2a 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e  *xAuth)(void*,in
632f0 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  t,const char*,co
63300 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
63310 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
63320 2a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  *);.            
63330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63340 20 20 20 20 2f 2a 20 41 63 63 65 73 73 20 61 75      /* Access au
63350 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63  thorization func
63360 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  tion */.  void *
63370 70 41 75 74 68 41 72 67 3b 20 20 20 20 20 20 20  pAuthArg;       
63380 20 20 20 20 20 20 20 20 2f 2a 20 31 73 74 20 61          /* 1st a
63390 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 61  rgument to the a
633a0 63 63 65 73 73 20 61 75 74 68 20 66 75 6e 63 74  ccess auth funct
633b0 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69  ion */.#endif.#i
633c0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
633d0 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42  T_PROGRESS_CALLB
633e0 41 43 4b 0a 20 20 69 6e 74 20 28 2a 78 50 72 6f  ACK.  int (*xPro
633f0 67 72 65 73 73 29 28 76 6f 69 64 20 2a 29 3b 20  gress)(void *); 
63400 20 20 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72      /* The progr
63410 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a  ess callback */.
63420 20 20 76 6f 69 64 20 2a 70 50 72 6f 67 72 65 73    void *pProgres
63430 73 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20  sArg;           
63440 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74  /* Argument to t
63450 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c  he progress call
63460 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 50  back */.  int nP
63470 72 6f 67 72 65 73 73 4f 70 73 3b 20 20 20 20 20  rogressOps;     
63480 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
63490 72 20 6f 66 20 6f 70 63 6f 64 65 73 20 66 6f 72  r of opcodes for
634a0 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61   progress callba
634b0 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66  ck */.#endif.#if
634c0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
634d0 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
634e0 48 61 73 68 20 61 4d 6f 64 75 6c 65 3b 20 20 20  Hash aModule;   
634f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
63500 20 70 6f 70 75 6c 61 74 65 64 20 62 79 20 73 71   populated by sq
63510 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
63520 75 6c 65 28 29 20 2a 2f 0a 20 20 54 61 62 6c 65  ule() */.  Table
63530 20 2a 70 56 54 61 62 3b 20 20 20 20 20 20 20 20   *pVTab;        
63540 20 20 20 20 20 20 20 20 20 2f 2a 20 76 74 61 62           /* vtab
63550 20 77 69 74 68 20 61 63 74 69 76 65 20 43 6f 6e   with active Con
63560 6e 65 63 74 2f 43 72 65 61 74 65 20 6d 65 74 68  nect/Create meth
63570 6f 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  od */.  sqlite3_
63580 76 74 61 62 20 2a 2a 61 56 54 72 61 6e 73 3b 20  vtab **aVTrans; 
63590 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c        /* Virtual
635a0 20 74 61 62 6c 65 73 20 77 69 74 68 20 6f 70 65   tables with ope
635b0 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 2a  n transactions *
635c0 2f 0a 20 20 69 6e 74 20 6e 56 54 72 61 6e 73 3b  /.  int nVTrans;
635d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
635e0 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73    /* Allocated s
635f0 69 7a 65 20 6f 66 20 61 56 54 72 61 6e 73 20 2a  ize of aVTrans *
63600 2f 0a 23 65 6e 64 69 66 0a 20 20 46 75 6e 63 44  /.#endif.  FuncD
63610 65 66 48 61 73 68 20 61 46 75 6e 63 3b 20 20 20  efHash aFunc;   
63620 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68           /* Hash
63630 20 74 61 62 6c 65 20 6f 66 20 63 6f 6e 6e 65 63   table of connec
63640 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 2a  tion functions *
63650 2f 0a 20 20 48 61 73 68 20 61 43 6f 6c 6c 53 65  /.  Hash aCollSe
63660 71 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  q;              
63670 20 20 2f 2a 20 41 6c 6c 20 63 6f 6c 6c 61 74 69    /* All collati
63680 6e 67 20 73 65 71 75 65 6e 63 65 73 20 2a 2f 0a  ng sequences */.
63690 20 20 42 75 73 79 48 61 6e 64 6c 65 72 20 62 75    BusyHandler bu
636a0 73 79 48 61 6e 64 6c 65 72 3b 20 20 20 20 20 20  syHandler;      
636b0 2f 2a 20 42 75 73 79 20 63 61 6c 6c 62 61 63 6b  /* Busy callback
636c0 20 2a 2f 0a 20 20 69 6e 74 20 62 75 73 79 54 69   */.  int busyTi
636d0 6d 65 6f 75 74 3b 20 20 20 20 20 20 20 20 20 20  meout;          
636e0 20 20 20 20 2f 2a 20 42 75 73 79 20 68 61 6e 64      /* Busy hand
636f0 6c 65 72 20 74 69 6d 65 6f 75 74 2c 20 69 6e 20  ler timeout, in 
63700 6d 73 65 63 20 2a 2f 0a 20 20 44 62 20 61 44 62  msec */.  Db aDb
63710 53 74 61 74 69 63 5b 32 5d 3b 20 20 20 20 20 20  Static[2];      
63720 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 69          /* Stati
63730 63 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20  c space for the 
63740 32 20 64 65 66 61 75 6c 74 20 62 61 63 6b 65 6e  2 default backen
63750 64 73 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  ds */.#ifdef SQL
63760 49 54 45 5f 53 53 45 0a 20 20 73 71 6c 69 74 65  ITE_SSE.  sqlite
63770 33 5f 73 74 6d 74 20 2a 70 46 65 74 63 68 3b 20  3_stmt *pFetch; 
63780 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20          /* Used 
63790 62 79 20 53 53 45 20 74 6f 20 66 65 74 63 68 20  by SSE to fetch 
637a0 73 74 6f 72 65 64 20 73 74 61 74 65 6d 65 6e 74  stored statement
637b0 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a  s */.#endif.};..
637c0 2f 2a 0a 2a 2a 20 41 20 6d 61 63 72 6f 20 74 6f  /*.** A macro to
637d0 20 64 69 73 63 6f 76 65 72 20 74 68 65 20 65 6e   discover the en
637e0 63 6f 64 69 6e 67 20 6f 66 20 61 20 64 61 74 61  coding of a data
637f0 62 61 73 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  base..*/.#define
63800 20 45 4e 43 28 64 62 29 20 28 28 64 62 29 2d 3e   ENC(db) ((db)->
63810 61 44 62 5b 30 5d 2e 70 53 63 68 65 6d 61 2d 3e  aDb[0].pSchema->
63820 65 6e 63 29 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73  enc)../*.** Poss
63830 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72 20  ible values for 
63840 74 68 65 20 73 71 6c 69 74 65 2e 66 6c 61 67 73  the sqlite.flags
63850 20 61 6e 64 20 6f 72 20 44 62 2e 66 6c 61 67 73   and or Db.flags
63860 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f   fields..**.** O
63870 6e 20 73 71 6c 69 74 65 2e 66 6c 61 67 73 2c 20  n sqlite.flags, 
63880 74 68 65 20 53 51 4c 49 54 45 5f 49 6e 54 72 61  the SQLITE_InTra
63890 6e 73 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 74  ns value means t
638a0 68 61 74 20 77 65 20 68 61 76 65 0a 2a 2a 20 65  hat we have.** e
638b0 78 65 63 75 74 65 64 20 61 20 42 45 47 49 4e 2e  xecuted a BEGIN.
638c0 20 20 4f 6e 20 44 62 2e 66 6c 61 67 73 2c 20 53    On Db.flags, S
638d0 51 4c 49 54 45 5f 49 6e 54 72 61 6e 73 20 6d 65  QLITE_InTrans me
638e0 61 6e 73 20 61 20 73 74 61 74 65 6d 65 6e 74 0a  ans a statement.
638f0 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  ** transaction i
63900 73 20 61 63 74 69 76 65 20 6f 6e 20 74 68 61 74  s active on that
63910 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61   particular data
63920 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 64  base file..*/.#d
63930 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 64 62  efine SQLITE_Vdb
63940 65 54 72 61 63 65 20 20 20 20 20 20 30 78 30 30  eTrace      0x00
63950 30 30 30 30 30 31 20 20 2f 2a 20 54 72 75 65 20  000001  /* True 
63960 74 6f 20 74 72 61 63 65 20 56 44 42 45 20 65 78  to trace VDBE ex
63970 65 63 75 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69  ecution */.#defi
63980 6e 65 20 53 51 4c 49 54 45 5f 49 6e 54 72 61 6e  ne SQLITE_InTran
63990 73 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30  s        0x00000
639a0 30 30 38 20 20 2f 2a 20 54 72 75 65 20 69 66 20  008  /* True if 
639b0 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  in a transaction
639c0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
639d0 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73  TE_InternChanges
639e0 20 20 30 78 30 30 30 30 30 30 31 30 20 20 2f 2a    0x00000010  /*
639f0 20 55 6e 63 6f 6d 6d 69 74 74 65 64 20 48 61 73   Uncommitted Has
63a00 68 20 74 61 62 6c 65 20 63 68 61 6e 67 65 73 20  h table changes 
63a10 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
63a20 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 20 20  E_FullColNames  
63a30 20 30 78 30 30 30 30 30 30 32 30 20 20 2f 2a 20   0x00000020  /* 
63a40 53 68 6f 77 20 66 75 6c 6c 20 63 6f 6c 75 6d 6e  Show full column
63a50 20 6e 61 6d 65 73 20 6f 6e 20 53 45 4c 45 43 54   names on SELECT
63a60 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
63a70 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73  TE_ShortColNames
63a80 20 20 30 78 30 30 30 30 30 30 34 30 20 20 2f 2a    0x00000040  /*
63a90 20 53 68 6f 77 20 73 68 6f 72 74 20 63 6f 6c 75   Show short colu
63aa0 6d 6e 73 20 6e 61 6d 65 73 20 2a 2f 0a 23 64 65  mns names */.#de
63ab0 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 6f 75 6e  fine SQLITE_Coun
63ac0 74 52 6f 77 73 20 20 20 20 20 20 30 78 30 30 30  tRows      0x000
63ad0 30 30 30 38 30 20 20 2f 2a 20 43 6f 75 6e 74 20  00080  /* Count 
63ae0 72 6f 77 73 20 63 68 61 6e 67 65 64 20 62 79 20  rows changed by 
63af0 49 4e 53 45 52 54 2c 20 2a 2f 0a 20 20 20 20 20  INSERT, */.     
63b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63b20 20 20 20 20 20 2f 2a 20 20 20 44 45 4c 45 54 45       /*   DELETE
63b30 2c 20 6f 72 20 55 50 44 41 54 45 20 61 6e 64 20  , or UPDATE and 
63b40 72 65 74 75 72 6e 20 2a 2f 0a 20 20 20 20 20 20  return */.      
63b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63b70 20 20 20 20 2f 2a 20 20 20 74 68 65 20 63 6f 75      /*   the cou
63b80 6e 74 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 62  nt using a callb
63b90 61 63 6b 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ack. */.#define 
63ba0 53 51 4c 49 54 45 5f 4e 75 6c 6c 43 61 6c 6c 62  SQLITE_NullCallb
63bb0 61 63 6b 20 20 20 30 78 30 30 30 30 30 31 30 30  ack   0x00000100
63bc0 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20    /* Invoke the 
63bd0 63 61 6c 6c 62 61 63 6b 20 6f 6e 63 65 20 69 66  callback once if
63be0 20 74 68 65 20 2a 2f 0a 20 20 20 20 20 20 20 20   the */.        
63bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63c10 20 20 2f 2a 20 20 20 72 65 73 75 6c 74 20 73 65    /*   result se
63c20 74 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a 23 64  t is empty */.#d
63c30 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 71 6c  efine SQLITE_Sql
63c40 54 72 61 63 65 20 20 20 20 20 20 20 30 78 30 30  Trace       0x00
63c50 30 30 30 32 30 30 20 20 2f 2a 20 44 65 62 75 67  000200  /* Debug
63c60 20 70 72 69 6e 74 20 53 51 4c 20 61 73 20 69 74   print SQL as it
63c70 20 65 78 65 63 75 74 65 73 20 2a 2f 0a 23 64 65   executes */.#de
63c80 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 64 62 65  fine SQLITE_Vdbe
63c90 4c 69 73 74 69 6e 67 20 20 20 20 30 78 30 30 30  Listing    0x000
63ca0 30 30 34 30 30 20 20 2f 2a 20 44 65 62 75 67 20  00400  /* Debug 
63cb0 6c 69 73 74 69 6e 67 73 20 6f 66 20 56 44 42 45  listings of VDBE
63cc0 20 70 72 6f 67 72 61 6d 73 20 2a 2f 0a 23 64 65   programs */.#de
63cd0 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 72 69 74  fine SQLITE_Writ
63ce0 65 53 63 68 65 6d 61 20 20 20 20 30 78 30 30 30  eSchema    0x000
63cf0 30 30 38 30 30 20 20 2f 2a 20 4f 4b 20 74 6f 20  00800  /* OK to 
63d00 75 70 64 61 74 65 20 53 51 4c 49 54 45 5f 4d 41  update SQLITE_MA
63d10 53 54 45 52 20 2a 2f 0a 23 64 65 66 69 6e 65 20  STER */.#define 
63d20 53 51 4c 49 54 45 5f 4e 6f 52 65 61 64 6c 6f 63  SQLITE_NoReadloc
63d30 6b 20 20 20 20 20 30 78 30 30 30 30 31 30 30 30  k     0x00001000
63d40 20 20 2f 2a 20 52 65 61 64 6c 6f 63 6b 73 20 61    /* Readlocks a
63d50 72 65 20 6f 6d 69 74 74 65 64 20 77 68 65 6e 20  re omitted when 
63d60 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
63d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63d80 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 63             ** ac
63d90 63 65 73 73 69 6e 67 20 72 65 61 64 2d 6f 6e 6c  cessing read-onl
63da0 79 20 64 61 74 61 62 61 73 65 73 20 2a 2f 0a 23  y databases */.#
63db0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 67  define SQLITE_Ig
63dc0 6e 6f 72 65 43 68 65 63 6b 73 20 20 20 30 78 30  noreChecks   0x0
63dd0 30 30 30 32 30 30 30 20 20 2f 2a 20 44 6f 20 6e  0002000  /* Do n
63de0 6f 74 20 65 6e 66 6f 72 63 65 20 63 68 65 63 6b  ot enforce check
63df0 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a   constraints */.
63e00 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52  #define SQLITE_R
63e10 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 30  eadUncommitted 0
63e20 78 30 30 30 30 34 30 30 30 20 2f 2a 20 46 6f 72  x00004000 /* For
63e30 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f   shared-cache mo
63e40 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  de */.#define SQ
63e50 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c 65 46  LITE_LegacyFileF
63e60 6d 74 20 20 30 78 30 30 30 30 38 30 30 30 20 20  mt  0x00008000  
63e70 2f 2a 20 43 72 65 61 74 65 20 6e 65 77 20 64 61  /* Create new da
63e80 74 61 62 61 73 65 73 20 69 6e 20 66 6f 72 6d 61  tabases in forma
63e90 74 20 31 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  t 1 */.#define S
63ea0 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79 6e 63 20  QLITE_FullFSync 
63eb0 20 20 20 20 20 30 78 30 30 30 31 30 30 30 30 20       0x00010000 
63ec0 20 2f 2a 20 55 73 65 20 66 75 6c 6c 20 66 73 79   /* Use full fsy
63ed0 6e 63 20 6f 6e 20 74 68 65 20 62 61 63 6b 65 6e  nc on the backen
63ee0 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  d */.#define SQL
63ef0 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f  ITE_LoadExtensio
63f00 6e 20 20 30 78 30 30 30 32 30 30 30 30 20 20 2f  n  0x00020000  /
63f10 2a 20 45 6e 61 62 6c 65 20 6c 6f 61 64 5f 65 78  * Enable load_ex
63f20 74 65 6e 73 69 6f 6e 20 2a 2f 0a 0a 23 64 65 66  tension */..#def
63f30 69 6e 65 20 53 51 4c 49 54 45 5f 52 65 63 6f 76  ine SQLITE_Recov
63f40 65 72 79 4d 6f 64 65 20 20 20 30 78 30 30 30 34  eryMode   0x0004
63f50 30 30 30 30 20 20 2f 2a 20 49 67 6e 6f 72 65 20  0000  /* Ignore 
63f60 73 63 68 65 6d 61 20 65 72 72 6f 72 73 20 2a 2f  schema errors */
63f70 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
63f80 53 68 61 72 65 64 43 61 63 68 65 20 20 20 20 30  SharedCache    0
63f90 78 30 30 30 38 30 30 30 30 20 20 2f 2a 20 43 61  x00080000  /* Ca
63fa0 63 68 65 20 73 68 61 72 69 6e 67 20 69 73 20 65  che sharing is e
63fb0 6e 61 62 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e  nabled */.#defin
63fc0 65 20 53 51 4c 49 54 45 5f 56 74 61 62 20 20 20  e SQLITE_Vtab   
63fd0 20 20 20 20 20 20 20 20 30 78 30 30 31 30 30 30          0x001000
63fe0 30 30 20 20 2f 2a 20 54 68 65 72 65 20 65 78 69  00  /* There exi
63ff0 73 74 73 20 61 20 76 69 72 74 75 61 6c 20 74 61  sts a virtual ta
64000 62 6c 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 6f  ble */../*.** Po
64010 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f  ssible values fo
64020 72 20 74 68 65 20 73 71 6c 69 74 65 2e 6d 61 67  r the sqlite.mag
64030 69 63 20 66 69 65 6c 64 2e 0a 2a 2a 20 54 68 65  ic field..** The
64040 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6f 62 74   numbers are obt
64050 61 69 6e 65 64 20 61 74 20 72 61 6e 64 6f 6d 20  ained at random 
64060 61 6e 64 20 68 61 76 65 20 6e 6f 20 73 70 65 63  and have no spec
64070 69 61 6c 20 6d 65 61 6e 69 6e 67 2c 20 6f 74 68  ial meaning, oth
64080 65 72 0a 2a 2a 20 74 68 61 6e 20 62 65 69 6e 67  er.** than being
64090 20 64 69 73 74 69 6e 63 74 20 66 72 6f 6d 20 6f   distinct from o
640a0 6e 65 20 61 6e 6f 74 68 65 72 2e 0a 2a 2f 0a 23  ne another..*/.#
640b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
640c0 47 49 43 5f 4f 50 45 4e 20 20 20 20 20 30 78 61  GIC_OPEN     0xa
640d0 30 32 39 61 36 39 37 20 20 2f 2a 20 44 61 74 61  029a697  /* Data
640e0 62 61 73 65 20 69 73 20 6f 70 65 6e 20 2a 2f 0a  base is open */.
640f0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d  #define SQLITE_M
64100 41 47 49 43 5f 43 4c 4f 53 45 44 20 20 20 30 78  AGIC_CLOSED   0x
64110 39 66 33 63 32 64 33 33 20 20 2f 2a 20 44 61 74  9f3c2d33  /* Dat
64120 61 62 61 73 65 20 69 73 20 63 6c 6f 73 65 64 20  abase is closed 
64130 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
64140 45 5f 4d 41 47 49 43 5f 53 49 43 4b 20 20 20 20  E_MAGIC_SICK    
64150 20 30 78 34 62 37 37 31 32 39 30 20 20 2f 2a 20   0x4b771290  /* 
64160 45 72 72 6f 72 20 61 6e 64 20 61 77 61 69 74 69  Error and awaiti
64170 6e 67 20 63 6c 6f 73 65 20 2a 2f 0a 23 64 65 66  ng close */.#def
64180 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  ine SQLITE_MAGIC
64190 5f 42 55 53 59 20 20 20 20 20 30 78 66 30 33 62  _BUSY     0xf03b
641a0 37 39 30 36 20 20 2f 2a 20 44 61 74 61 62 61 73  7906  /* Databas
641b0 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75  e currently in u
641c0 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  se */.#define SQ
641d0 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52  LITE_MAGIC_ERROR
641e0 20 20 20 20 30 78 62 35 33 35 37 39 33 30 20 20      0xb5357930  
641f0 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f 4d 49 53  /* An SQLITE_MIS
64200 55 53 45 20 65 72 72 6f 72 20 6f 63 63 75 72 72  USE error occurr
64210 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63  ed */../*.** Eac
64220 68 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69  h SQL function i
64230 73 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e 20  s defined by an 
64240 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
64250 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72  following.** str
64260 75 63 74 75 72 65 2e 20 20 41 20 70 6f 69 6e 74  ucture.  A point
64270 65 72 20 74 6f 20 74 68 69 73 20 73 74 72 75 63  er to this struc
64280 74 75 72 65 20 69 73 20 73 74 6f 72 65 64 20 69  ture is stored i
64290 6e 20 74 68 65 20 73 71 6c 69 74 65 2e 61 46 75  n the sqlite.aFu
642a0 6e 63 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65  nc.** hash table
642b0 2e 20 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c 65  .  When multiple
642c0 20 66 75 6e 63 74 69 6f 6e 73 20 68 61 76 65 20   functions have 
642d0 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2c 20 74  the same name, t
642e0 68 65 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2a  he hash table.**
642f0 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 6c 69 6e   points to a lin
64300 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 68 65 73  ked list of thes
64310 65 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f  e structures..*/
64320 0a 73 74 72 75 63 74 20 46 75 6e 63 44 65 66 20  .struct FuncDef 
64330 7b 0a 20 20 69 31 36 20 6e 41 72 67 3b 20 20 20  {.  i16 nArg;   
64340 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
64350 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e  er of arguments.
64360 20 20 2d 31 20 6d 65 61 6e 73 20 75 6e 6c 69 6d    -1 means unlim
64370 69 74 65 64 20 2a 2f 0a 20 20 75 38 20 69 50 72  ited */.  u8 iPr
64380 65 66 45 6e 63 3b 20 20 20 20 20 20 20 20 20 2f  efEnc;         /
64390 2a 20 50 72 65 66 65 72 72 65 64 20 74 65 78 74  * Preferred text
643a0 20 65 6e 63 6f 64 69 6e 67 20 28 53 51 4c 49 54   encoding (SQLIT
643b0 45 5f 55 54 46 38 2c 20 31 36 4c 45 2c 20 31 36  E_UTF8, 16LE, 16
643c0 42 45 29 20 2a 2f 0a 20 20 75 38 20 66 6c 61 67  BE) */.  u8 flag
643d0 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  s;            /*
643e0 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f   Some combinatio
643f0 6e 20 6f 66 20 53 51 4c 49 54 45 5f 46 55 4e 43  n of SQLITE_FUNC
64400 5f 2a 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55  _* */.  void *pU
64410 73 65 72 44 61 74 61 3b 20 20 20 20 20 2f 2a 20  serData;     /* 
64420 55 73 65 72 20 64 61 74 61 20 70 61 72 61 6d 65  User data parame
64430 74 65 72 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66  ter */.  FuncDef
64440 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a   *pNext;      /*
64450 20 4e 65 78 74 20 66 75 6e 63 74 69 6f 6e 20 77   Next function w
64460 69 74 68 20 73 61 6d 65 20 6e 61 6d 65 20 2a 2f  ith same name */
64470 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29  .  void (*xFunc)
64480 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
64490 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
644a0 6c 75 65 2a 2a 29 3b 20 2f 2a 20 52 65 67 75 6c  lue**); /* Regul
644b0 61 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  ar function */. 
644c0 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73   void (*xStep)(s
644d0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
644e0 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
644f0 65 2a 2a 29 3b 20 2f 2a 20 41 67 67 72 65 67 61  e**); /* Aggrega
64500 74 65 20 73 74 65 70 20 2a 2f 0a 20 20 76 6f 69  te step */.  voi
64510 64 20 28 2a 78 46 69 6e 61 6c 69 7a 65 29 28 73  d (*xFinalize)(s
64520 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
64530 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
64540 20 2f 2a 20 41 67 67 72 65 67 61 74 65 20 66 69   /* Aggregate fi
64550 6e 69 61 6c 69 7a 65 72 20 2a 2f 0a 20 20 63 68  nializer */.  ch
64560 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20  ar *zName;      
64570 20 20 20 2f 2a 20 53 51 4c 20 6e 61 6d 65 20 6f     /* SQL name o
64580 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20  f the function. 
64590 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 48  */.  FuncDef *pH
645a0 61 73 68 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78  ash;      /* Nex
645b0 74 20 77 69 74 68 20 61 20 64 69 66 66 65 72 65  t with a differe
645c0 6e 74 20 6e 61 6d 65 20 62 75 74 20 74 68 65 20  nt name but the 
645d0 73 61 6d 65 20 68 61 73 68 20 2a 2f 0a 7d 3b 0a  same hash */.};.
645e0 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20  ./*.** Possible 
645f0 76 61 6c 75 65 73 20 66 6f 72 20 46 75 6e 63 44  values for FuncD
64600 65 66 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66  ef.flags.*/.#def
64610 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  ine SQLITE_FUNC_
64620 4c 49 4b 45 20 20 20 20 20 30 78 30 31 20 2f 2a  LIKE     0x01 /*
64630 20 43 61 6e 64 69 64 61 74 65 20 66 6f 72 20 74   Candidate for t
64640 68 65 20 4c 49 4b 45 20 6f 70 74 69 6d 69 7a 61  he LIKE optimiza
64650 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  tion */.#define 
64660 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45  SQLITE_FUNC_CASE
64670 20 20 20 20 20 30 78 30 32 20 2f 2a 20 43 61 73       0x02 /* Cas
64680 65 2d 73 65 6e 73 69 74 69 76 65 20 4c 49 4b 45  e-sensitive LIKE
64690 2d 74 79 70 65 20 66 75 6e 63 74 69 6f 6e 20 2a  -type function *
646a0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
646b0 5f 46 55 4e 43 5f 45 50 48 45 4d 20 20 20 20 30  _FUNC_EPHEM    0
646c0 78 30 34 20 2f 2a 20 45 70 68 65 72 6d 65 72 61  x04 /* Ephermera
646d0 6c 2e 20 20 44 65 6c 65 74 65 20 77 69 74 68 20  l.  Delete with 
646e0 56 44 42 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20  VDBE */.#define 
646f0 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44  SQLITE_FUNC_NEED
64700 43 4f 4c 4c 20 30 78 30 38 20 2f 2a 20 73 71 6c  COLL 0x08 /* sql
64710 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53  ite3GetFuncCollS
64720 65 71 28 29 20 6d 69 67 68 74 20 62 65 20 63 61  eq() might be ca
64730 6c 6c 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  lled */../*.** T
64740 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 72  he following thr
64750 65 65 20 6d 61 63 72 6f 73 2c 20 46 55 4e 43 54  ee macros, FUNCT
64760 49 4f 4e 28 29 2c 20 4c 49 4b 45 46 55 4e 43 28  ION(), LIKEFUNC(
64770 29 20 61 6e 64 20 41 47 47 52 45 47 41 54 45 28  ) and AGGREGATE(
64780 29 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f  ) are.** used to
64790 20 63 72 65 61 74 65 20 74 68 65 20 69 6e 69 74   create the init
647a0 69 61 6c 69 7a 65 72 73 20 66 6f 72 20 74 68 65  ializers for the
647b0 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75   FuncDef structu
647c0 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 46 55 4e  res..**.**   FUN
647d0 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72  CTION(zName, nAr
647e0 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 46  g, iArg, bNC, xF
647f0 75 6e 63 29 0a 2a 2a 20 20 20 20 20 55 73 65 64  unc).**     Used
64800 20 74 6f 20 63 72 65 61 74 65 20 61 20 73 63 61   to create a sca
64810 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 64 65 66  lar function def
64820 69 6e 69 74 69 6f 6e 20 6f 66 20 61 20 66 75 6e  inition of a fun
64830 63 74 69 6f 6e 20 7a 4e 61 6d 65 20 0a 2a 2a 20  ction zName .** 
64840 20 20 20 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20      implemented 
64850 62 79 20 43 20 66 75 6e 63 74 69 6f 6e 20 78 46  by C function xF
64860 75 6e 63 20 74 68 61 74 20 61 63 63 65 70 74 73  unc that accepts
64870 20 6e 41 72 67 20 61 72 67 75 6d 65 6e 74 73 2e   nArg arguments.
64880 20 54 68 65 0a 2a 2a 20 20 20 20 20 76 61 6c 75   The.**     valu
64890 65 20 70 61 73 73 65 64 20 61 73 20 69 41 72 67  e passed as iArg
648a0 20 69 73 20 63 61 73 74 20 74 6f 20 61 20 28 76   is cast to a (v
648b0 6f 69 64 2a 29 20 61 6e 64 20 6d 61 64 65 20 61  oid*) and made a
648c0 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 20 20 20 20  vailable.**     
648d0 61 73 20 74 68 65 20 75 73 65 72 2d 64 61 74 61  as the user-data
648e0 20 28 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64   (sqlite3_user_d
648f0 61 74 61 28 29 29 20 66 6f 72 20 74 68 65 20 66  ata()) for the f
64900 75 6e 63 74 69 6f 6e 2e 20 49 66 20 0a 2a 2a 20  unction. If .** 
64910 20 20 20 20 61 72 67 75 6d 65 6e 74 20 62 4e 43      argument bNC
64920 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74   is true, then t
64930 68 65 20 46 75 6e 63 44 65 66 2e 6e 65 65 64 43  he FuncDef.needC
64940 6f 6c 6c 61 74 65 20 66 6c 61 67 20 69 73 20 73  ollate flag is s
64950 65 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 41 47 47 52  et..**.**   AGGR
64960 45 47 41 54 45 28 7a 4e 61 6d 65 2c 20 6e 41 72  EGATE(zName, nAr
64970 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 53  g, iArg, bNC, xS
64980 74 65 70 2c 20 78 46 69 6e 61 6c 29 0a 2a 2a 20  tep, xFinal).** 
64990 20 20 20 20 55 73 65 64 20 74 6f 20 63 72 65 61      Used to crea
649a0 74 65 20 61 6e 20 61 67 67 72 65 67 61 74 65 20  te an aggregate 
649b0 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74  function definit
649c0 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20  ion implemented 
649d0 62 79 0a 2a 2a 20 20 20 20 20 74 68 65 20 43 20  by.**     the C 
649e0 66 75 6e 63 74 69 6f 6e 73 20 78 53 74 65 70 20  functions xStep 
649f0 61 6e 64 20 78 46 69 6e 61 6c 2e 20 54 68 65 20  and xFinal. The 
64a00 66 69 72 73 74 20 66 6f 75 72 20 70 61 72 61 6d  first four param
64a10 65 74 65 72 73 0a 2a 2a 20 20 20 20 20 61 72 65  eters.**     are
64a20 20 69 6e 74 65 72 70 72 65 74 65 64 20 69 6e 20   interpreted in 
64a30 74 68 65 20 73 61 6d 65 20 77 61 79 20 61 73 20  the same way as 
64a40 74 68 65 20 66 69 72 73 74 20 34 20 70 61 72 61  the first 4 para
64a50 6d 65 74 65 72 73 20 74 6f 0a 2a 2a 20 20 20 20  meters to.**    
64a60 20 46 55 4e 43 54 49 4f 4e 28 29 2e 0a 2a 2a 0a   FUNCTION()..**.
64a70 2a 2a 20 20 20 4c 49 4b 45 46 55 4e 43 28 7a 4e  **   LIKEFUNC(zN
64a80 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72 67 2c  ame, nArg, pArg,
64a90 20 66 6c 61 67 73 29 0a 2a 2a 20 20 20 20 20 55   flags).**     U
64aa0 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20  sed to create a 
64ab0 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20  scalar function 
64ac0 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 61 20  definition of a 
64ad0 66 75 6e 63 74 69 6f 6e 20 7a 4e 61 6d 65 20 0a  function zName .
64ae0 2a 2a 20 20 20 20 20 74 68 61 74 20 61 63 63 65  **     that acce
64af0 70 74 73 20 6e 41 72 67 20 61 72 67 75 6d 65 6e  pts nArg argumen
64b00 74 73 20 61 6e 64 20 69 73 20 69 6d 70 6c 65 6d  ts and is implem
64b10 65 6e 74 65 64 20 62 79 20 61 20 63 61 6c 6c 20  ented by a call 
64b20 74 6f 20 43 20 0a 2a 2a 20 20 20 20 20 66 75 6e  to C .**     fun
64b30 63 74 69 6f 6e 20 6c 69 6b 65 46 75 6e 63 2e 20  ction likeFunc. 
64b40 41 72 67 75 6d 65 6e 74 20 70 41 72 67 20 69 73  Argument pArg is
64b50 20 63 61 73 74 20 74 6f 20 61 20 28 76 6f 69 64   cast to a (void
64b60 20 2a 29 20 61 6e 64 20 6d 61 64 65 0a 2a 2a 20   *) and made.** 
64b70 20 20 20 20 61 76 61 69 6c 61 62 6c 65 20 61 73      available as
64b80 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 75 73   the function us
64b90 65 72 2d 64 61 74 61 20 28 73 71 6c 69 74 65 33  er-data (sqlite3
64ba0 5f 75 73 65 72 5f 64 61 74 61 28 29 29 2e 20 54  _user_data()). T
64bb0 68 65 0a 2a 2a 20 20 20 20 20 46 75 6e 63 44 65  he.**     FuncDe
64bc0 66 2e 66 6c 61 67 73 20 76 61 72 69 61 62 6c 65  f.flags variable
64bd0 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 76   is set to the v
64be0 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 74  alue passed as t
64bf0 68 65 20 66 6c 61 67 73 0a 2a 2a 20 20 20 20 20  he flags.**     
64c00 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 23 64  parameter..*/.#d
64c10 65 66 69 6e 65 20 46 55 4e 43 54 49 4f 4e 28 7a  efine FUNCTION(z
64c20 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72 67  Name, nArg, iArg
64c30 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 20 5c 0a  , bNC, xFunc) \.
64c40 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f    {nArg, SQLITE_
64c50 55 54 46 38 2c 20 62 4e 43 2a 38 2c 20 53 51 4c  UTF8, bNC*8, SQL
64c60 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 69  ITE_INT_TO_PTR(i
64c70 41 72 67 29 2c 20 30 2c 20 78 46 75 6e 63 2c 20  Arg), 0, xFunc, 
64c80 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 7d 0a 23 64  0, 0, #zName}.#d
64c90 65 66 69 6e 65 20 53 54 52 5f 46 55 4e 43 54 49  efine STR_FUNCTI
64ca0 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20  ON(zName, nArg, 
64cb0 70 41 72 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63  pArg, bNC, xFunc
64cc0 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c  ) \.  {nArg, SQL
64cd0 49 54 45 5f 55 54 46 38 2c 20 62 4e 43 2a 38 2c  ITE_UTF8, bNC*8,
64ce0 20 70 41 72 67 2c 20 30 2c 20 78 46 75 6e 63 2c   pArg, 0, xFunc,
64cf0 20 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 7d 0a 23   0, 0, #zName}.#
64d00 64 65 66 69 6e 65 20 4c 49 4b 45 46 55 4e 43 28  define LIKEFUNC(
64d10 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 61 72 67  zName, nArg, arg
64d20 2c 20 66 6c 61 67 73 29 20 5c 0a 20 20 7b 6e 41  , flags) \.  {nA
64d30 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  rg, SQLITE_UTF8,
64d40 20 66 6c 61 67 73 2c 20 28 76 6f 69 64 20 2a 29   flags, (void *)
64d50 61 72 67 2c 20 30 2c 20 6c 69 6b 65 46 75 6e 63  arg, 0, likeFunc
64d60 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 7d 0a  , 0, 0, #zName}.
64d70 23 64 65 66 69 6e 65 20 41 47 47 52 45 47 41 54  #define AGGREGAT
64d80 45 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 61  E(zName, nArg, a
64d90 72 67 2c 20 6e 63 2c 20 78 53 74 65 70 2c 20 78  rg, nc, xStep, x
64da0 46 69 6e 61 6c 29 20 5c 0a 20 20 7b 6e 41 72 67  Final) \.  {nArg
64db0 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 6e  , SQLITE_UTF8, n
64dc0 63 2a 38 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f  c*8, SQLITE_INT_
64dd0 54 4f 5f 50 54 52 28 61 72 67 29 2c 20 30 2c 20  TO_PTR(arg), 0, 
64de0 30 2c 20 78 53 74 65 70 2c 78 46 69 6e 61 6c 2c  0, xStep,xFinal,
64df0 20 23 7a 4e 61 6d 65 7d 0a 0a 0a 2f 2a 0a 2a 2a   #zName}.../*.**
64e00 20 45 61 63 68 20 53 51 4c 69 74 65 20 6d 6f 64   Each SQLite mod
64e10 75 6c 65 20 28 76 69 72 74 75 61 6c 20 74 61 62  ule (virtual tab
64e20 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 29 20 69  le definition) i
64e30 73 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e 0a  s defined by an.
64e40 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  ** instance of t
64e50 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
64e60 75 63 74 75 72 65 2c 20 73 74 6f 72 65 64 20 69  ucture, stored i
64e70 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 4d  n the sqlite3.aM
64e80 6f 64 75 6c 65 0a 2a 2a 20 68 61 73 68 20 74 61  odule.** hash ta
64e90 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d  ble..*/.struct M
64ea0 6f 64 75 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20  odule {.  const 
64eb0 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a  sqlite3_module *
64ec0 70 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20 20 2f  pModule;       /
64ed0 2a 20 43 61 6c 6c 62 61 63 6b 20 70 6f 69 6e 74  * Callback point
64ee0 65 72 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ers */.  const c
64ef0 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
64f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
64f10 20 4e 61 6d 65 20 70 61 73 73 65 64 20 74 6f 20   Name passed to 
64f20 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20  create_module() 
64f30 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 3b  */.  void *pAux;
64f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64f50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41 75            /* pAu
64f60 78 20 70 61 73 73 65 64 20 74 6f 20 63 72 65 61  x passed to crea
64f70 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20  te_module() */. 
64f80 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79   void (*xDestroy
64f90 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 20 20  )(void *);      
64fa0 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20        /* Module 
64fb0 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74  destructor funct
64fc0 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ion */.};../*.**
64fd0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
64fe0 75 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6f  ut each column o
64ff0 66 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 69  f an SQL table i
65000 73 20 68 65 6c 64 20 69 6e 20 61 6e 20 69 6e 73  s held in an ins
65010 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 69 73  tance.** of this
65020 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73   structure..*/.s
65030 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20 7b 0a 20  truct Column {. 
65040 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
65050 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69    /* Name of thi
65060 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45 78  s column */.  Ex
65070 70 72 20 2a 70 44 66 6c 74 3b 20 20 20 20 20 2f  pr *pDflt;     /
65080 2a 20 44 65 66 61 75 6c 74 20 76 61 6c 75 65 20  * Default value 
65090 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a  of this column *
650a0 2f 0a 20 20 63 68 61 72 20 2a 7a 54 79 70 65 3b  /.  char *zType;
650b0 20 20 20 20 20 2f 2a 20 44 61 74 61 20 74 79 70       /* Data typ
650c0 65 20 66 6f 72 20 74 68 69 73 20 63 6f 6c 75 6d  e for this colum
650d0 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f  n */.  char *zCo
650e0 6c 6c 3b 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61  ll;     /* Colla
650f0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 20 20  ting sequence.  
65100 49 66 20 4e 55 4c 4c 2c 20 75 73 65 20 74 68 65  If NULL, use the
65110 20 64 65 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38   default */.  u8
65120 20 6e 6f 74 4e 75 6c 6c 3b 20 20 20 20 20 20 2f   notNull;      /
65130 2a 20 54 72 75 65 20 69 66 20 74 68 65 72 65 20  * True if there 
65140 69 73 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f  is a NOT NULL co
65150 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 75 38  nstraint */.  u8
65160 20 69 73 50 72 69 6d 4b 65 79 3b 20 20 20 20 2f   isPrimKey;    /
65170 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 63  * True if this c
65180 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66  olumn is part of
65190 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59   the PRIMARY KEY
651a0 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e   */.  char affin
651b0 69 74 79 3b 20 20 20 2f 2a 20 4f 6e 65 20 6f 66  ity;   /* One of
651c0 20 74 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f   the SQLITE_AFF_
651d0 2e 2e 2e 20 76 61 6c 75 65 73 20 2a 2f 0a 23 69  ... values */.#i
651e0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
651f0 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
65200 20 75 38 20 69 73 48 69 64 64 65 6e 3b 20 20 20   u8 isHidden;   
65210 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69    /* True if thi
65220 73 20 63 6f 6c 75 6d 6e 20 69 73 20 27 68 69 64  s column is 'hid
65230 64 65 6e 27 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d  den' */.#endif.}
65240 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 22 43 6f 6c 6c  ;../*.** A "Coll
65250 61 74 69 6e 67 20 53 65 71 75 65 6e 63 65 22 20  ating Sequence" 
65260 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e  is defined by an
65270 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
65280 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74   following.** st
65290 72 75 63 74 75 72 65 2e 20 43 6f 6e 63 65 70 74  ructure. Concept
652a0 75 61 6c 6c 79 2c 20 61 20 63 6f 6c 6c 61 74 69  ually, a collati
652b0 6e 67 20 73 65 71 75 65 6e 63 65 20 63 6f 6e 73  ng sequence cons
652c0 69 73 74 73 20 6f 66 20 61 20 6e 61 6d 65 20 61  ists of a name a
652d0 6e 64 0a 2a 2a 20 61 20 63 6f 6d 70 61 72 69 73  nd.** a comparis
652e0 6f 6e 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20  on routine that 
652f0 64 65 66 69 6e 65 73 20 74 68 65 20 6f 72 64 65  defines the orde
65300 72 20 6f 66 20 74 68 61 74 20 73 65 71 75 65 6e  r of that sequen
65310 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  ce..**.** There 
65320 6d 61 79 20 74 77 6f 20 73 65 70 65 72 61 74 65  may two seperate
65330 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
65340 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f   of the collatio
65350 6e 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 6e 65 0a  n function, one.
65360 2a 2a 20 74 68 61 74 20 70 72 6f 63 65 73 73 65  ** that processe
65370 73 20 74 65 78 74 20 69 6e 20 55 54 46 2d 38 20  s text in UTF-8 
65380 65 6e 63 6f 64 69 6e 67 20 28 43 6f 6c 6c 53 65  encoding (CollSe
65390 71 2e 78 43 6d 70 29 20 61 6e 64 20 61 6e 6f 74  q.xCmp) and anot
653a0 68 65 72 20 74 68 61 74 0a 2a 2a 20 70 72 6f 63  her that.** proc
653b0 65 73 73 65 73 20 74 65 78 74 20 65 6e 63 6f 64  esses text encod
653c0 65 64 20 69 6e 20 55 54 46 2d 31 36 20 28 43 6f  ed in UTF-16 (Co
653d0 6c 6c 53 65 71 2e 78 43 6d 70 31 36 29 2c 20 75  llSeq.xCmp16), u
653e0 73 69 6e 67 20 74 68 65 20 6d 61 63 68 69 6e 65  sing the machine
653f0 0a 2a 2a 20 6e 61 74 69 76 65 20 62 79 74 65 20  .** native byte 
65400 6f 72 64 65 72 2e 20 57 68 65 6e 20 61 20 63 6f  order. When a co
65410 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
65420 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20 53 51 4c   is invoked, SQL
65430 69 74 65 20 73 65 6c 65 63 74 73 0a 2a 2a 20 74  ite selects.** t
65440 68 65 20 76 65 72 73 69 6f 6e 20 74 68 61 74 20  he version that 
65450 77 69 6c 6c 20 72 65 71 75 69 72 65 20 74 68 65  will require the
65460 20 6c 65 61 73 74 20 65 78 70 65 6e 73 69 76 65   least expensive
65470 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 72 61   encoding.** tra
65480 6e 73 6c 61 74 69 6f 6e 73 2c 20 69 66 20 61 6e  nslations, if an
65490 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 6f 6c  y..**.** The Col
654a0 6c 53 65 71 2e 70 55 73 65 72 20 6d 65 6d 62 65  lSeq.pUser membe
654b0 72 20 76 61 72 69 61 62 6c 65 20 69 73 20 61 6e  r variable is an
654c0 20 65 78 74 72 61 20 70 61 72 61 6d 65 74 65 72   extra parameter
654d0 20 74 68 61 74 20 70 61 73 73 65 64 20 69 6e 0a   that passed in.
654e0 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ** as the first 
654f0 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
65500 55 54 46 2d 38 20 63 6f 6d 70 61 72 69 73 6f 6e  UTF-8 comparison
65510 20 66 75 6e 63 74 69 6f 6e 2c 20 78 43 6d 70 2e   function, xCmp.
65520 0a 2a 2a 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65  .** CollSeq.pUse
65530 72 31 36 20 69 73 20 74 68 65 20 65 71 75 69 76  r16 is the equiv
65540 61 6c 65 6e 74 20 66 6f 72 20 74 68 65 20 55 54  alent for the UT
65550 46 2d 31 36 20 63 6f 6d 70 61 72 69 73 6f 6e 20  F-16 comparison 
65560 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 78 43 6d  function,.** xCm
65570 70 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f  p16..**.** If bo
65580 74 68 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 20  th CollSeq.xCmp 
65590 61 6e 64 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70  and CollSeq.xCmp
655a0 31 36 20 61 72 65 20 4e 55 4c 4c 2c 20 69 74 20  16 are NULL, it 
655b0 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a  means that the.*
655c0 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  * collating sequ
655d0 65 6e 63 65 20 69 73 20 75 6e 64 65 66 69 6e 65  ence is undefine
655e0 64 2e 20 20 49 6e 64 69 63 65 73 20 62 75 69 6c  d.  Indices buil
655f0 74 20 6f 6e 20 61 6e 20 75 6e 64 65 66 69 6e 65  t on an undefine
65600 64 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73  d.** collating s
65610 65 71 75 65 6e 63 65 20 6d 61 79 20 6e 6f 74 20  equence may not 
65620 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74  be read or writt
65630 65 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 43 6f  en..*/.struct Co
65640 6c 6c 53 65 71 20 7b 0a 20 20 63 68 61 72 20 2a  llSeq {.  char *
65650 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20  zName;          
65660 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63  /* Name of the c
65670 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
65680 65 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  e, UTF-8 encoded
65690 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20   */.  u8 enc;   
656a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
656b0 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 68 61 6e  ext encoding han
656c0 64 6c 65 64 20 62 79 20 78 43 6d 70 28 29 20 2a  dled by xCmp() *
656d0 2f 0a 20 20 75 38 20 74 79 70 65 3b 20 20 20 20  /.  u8 type;    
656e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
656f0 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 43   of the SQLITE_C
65700 4f 4c 4c 5f 2e 2e 2e 20 76 61 6c 75 65 73 20 62  OLL_... values b
65710 65 6c 6f 77 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  elow */.  void *
65720 70 55 73 65 72 3b 20 20 20 20 20 20 20 20 20 20  pUser;          
65730 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e  /* First argumen
65740 74 20 74 6f 20 78 43 6d 70 28 29 20 2a 2f 0a 20  t to xCmp() */. 
65750 20 69 6e 74 20 28 2a 78 43 6d 70 29 28 76 6f 69   int (*xCmp)(voi
65760 64 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f  d*,int, const vo
65770 69 64 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20  id*, int, const 
65780 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 28  void*);.  void (
65790 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 3b 20 20  *xDel)(void*);  
657a0 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f  /* Destructor fo
657b0 72 20 70 55 73 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f  r pUser */.};../
657c0 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c  *.** Allowed val
657d0 75 65 73 20 6f 66 20 43 6f 6c 6c 53 65 71 2e 74  ues of CollSeq.t
657e0 79 70 65 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ype:.*/.#define 
657f0 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41  SQLITE_COLL_BINA
65800 52 59 20 20 31 20 20 2f 2a 20 54 68 65 20 64 65  RY  1  /* The de
65810 66 61 75 6c 74 20 6d 65 6d 63 6d 70 28 29 20 63  fault memcmp() c
65820 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
65830 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  e */.#define SQL
65840 49 54 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45 20  ITE_COLL_NOCASE 
65850 20 32 20 20 2f 2a 20 54 68 65 20 62 75 69 6c 74   2  /* The built
65860 2d 69 6e 20 4e 4f 43 41 53 45 20 63 6f 6c 6c 61  -in NOCASE colla
65870 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f  ting sequence */
65880 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
65890 43 4f 4c 4c 5f 52 45 56 45 52 53 45 20 33 20 20  COLL_REVERSE 3  
658a0 2f 2a 20 54 68 65 20 62 75 69 6c 74 2d 69 6e 20  /* The built-in 
658b0 52 45 56 45 52 53 45 20 63 6f 6c 6c 61 74 69 6e  REVERSE collatin
658c0 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64  g sequence */.#d
658d0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c  efine SQLITE_COL
658e0 4c 5f 55 53 45 52 20 20 20 20 30 20 20 2f 2a 20  L_USER    0  /* 
658f0 41 6e 79 20 6f 74 68 65 72 20 75 73 65 72 2d 64  Any other user-d
65900 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6e 67  efined collating
65910 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 0a 2f 2a   sequence */../*
65920 0a 2a 2a 20 41 20 73 6f 72 74 20 6f 72 64 65 72  .** A sort order
65930 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 41   can be either A
65940 53 43 20 6f 72 20 44 45 53 43 2e 0a 2a 2f 0a 23  SC or DESC..*/.#
65950 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f  define SQLITE_SO
65960 5f 41 53 43 20 20 20 20 20 20 20 30 20 20 2f 2a  _ASC       0  /*
65970 20 53 6f 72 74 20 69 6e 20 61 73 63 65 6e 64 69   Sort in ascendi
65980 6e 67 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66  ng order */.#def
65990 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f 5f 44 45  ine SQLITE_SO_DE
659a0 53 43 20 20 20 20 20 20 31 20 20 2f 2a 20 53 6f  SC      1  /* So
659b0 72 74 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20  rt in ascending 
659c0 6f 72 64 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  order */../*.** 
659d0 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20  Column affinity 
659e0 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  types..**.** The
659f0 73 65 20 75 73 65 64 20 74 6f 20 68 61 76 65 20  se used to have 
65a00 6d 6e 65 6d 6f 6e 69 63 20 6e 61 6d 65 20 6c 69  mnemonic name li
65a10 6b 65 20 27 69 27 20 66 6f 72 20 53 51 4c 49 54  ke 'i' for SQLIT
65a20 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 61 6e  E_AFF_INTEGER an
65a30 64 0a 2a 2a 20 27 74 27 20 66 6f 72 20 53 51 4c  d.** 't' for SQL
65a40 49 54 45 5f 41 46 46 5f 54 45 58 54 2e 20 20 42  ITE_AFF_TEXT.  B
65a50 75 74 20 77 65 20 63 61 6e 20 73 61 76 65 20 61  ut we can save a
65a60 20 6c 69 74 74 6c 65 20 73 70 61 63 65 20 61 6e   little space an
65a70 64 20 69 6d 70 72 6f 76 65 0a 2a 2a 20 74 68 65  d improve.** the
65a80 20 73 70 65 65 64 20 61 20 6c 69 74 74 6c 65 20   speed a little 
65a90 62 79 20 6e 75 6d 62 65 72 69 6e 67 20 74 68 65  by numbering the
65aa0 20 76 61 6c 75 65 73 20 63 6f 6e 73 65 63 75 74   values consecut
65ab0 69 76 65 6c 79 2e 20 20 0a 2a 2a 0a 2a 2a 20 42  ively.  .**.** B
65ac0 75 74 20 72 61 74 68 65 72 20 74 68 61 6e 20 73  ut rather than s
65ad0 74 61 72 74 20 77 69 74 68 20 30 20 6f 72 20 31  tart with 0 or 1
65ae0 2c 20 77 65 20 62 65 67 69 6e 20 77 69 74 68 20  , we begin with 
65af0 27 61 27 2e 20 20 54 68 61 74 20 77 61 79 2c 0a  'a'.  That way,.
65b00 2a 2a 20 77 68 65 6e 20 6d 75 6c 74 69 70 6c 65  ** when multiple
65b10 20 61 66 66 69 6e 69 74 79 20 74 79 70 65 73 20   affinity types 
65b20 61 72 65 20 63 6f 6e 63 61 74 65 6e 61 74 65 64  are concatenated
65b30 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 61   into a string a
65b40 6e 64 0a 2a 2a 20 75 73 65 64 20 61 73 20 74 68  nd.** used as th
65b50 65 20 50 34 20 6f 70 65 72 61 6e 64 2c 20 74 68  e P4 operand, th
65b60 65 79 20 77 69 6c 6c 20 62 65 20 6d 6f 72 65 20  ey will be more 
65b70 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  readable..**.** 
65b80 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20 74  Note also that t
65b90 68 65 20 6e 75 6d 65 72 69 63 20 74 79 70 65 73  he numeric types
65ba0 20 61 72 65 20 67 72 6f 75 70 65 64 20 74 6f 67   are grouped tog
65bb0 65 74 68 65 72 20 73 6f 20 74 68 61 74 20 74 65  ether so that te
65bc0 73 74 69 6e 67 0a 2a 2a 20 66 6f 72 20 61 20 6e  sting.** for a n
65bd0 75 6d 65 72 69 63 20 74 79 70 65 20 69 73 20 61  umeric type is a
65be0 20 73 69 6e 67 6c 65 20 63 6f 6d 70 61 72 69 73   single comparis
65bf0 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  on..*/.#define S
65c00 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 20  QLITE_AFF_TEXT  
65c10 20 20 20 27 61 27 0a 23 64 65 66 69 6e 65 20 53     'a'.#define S
65c20 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 20  QLITE_AFF_NONE  
65c30 20 20 20 27 62 27 0a 23 64 65 66 69 6e 65 20 53     'b'.#define S
65c40 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49  QLITE_AFF_NUMERI
65c50 43 20 20 27 63 27 0a 23 64 65 66 69 6e 65 20 53  C  'c'.#define S
65c60 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45  QLITE_AFF_INTEGE
65c70 52 20 20 27 64 27 0a 23 64 65 66 69 6e 65 20 53  R  'd'.#define S
65c80 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 20 20  QLITE_AFF_REAL  
65c90 20 20 20 27 65 27 0a 0a 23 64 65 66 69 6e 65 20     'e'..#define 
65ca0 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 69 63  sqlite3IsNumeric
65cb0 41 66 66 69 6e 69 74 79 28 58 29 20 20 28 28 58  Affinity(X)  ((X
65cc0 29 3e 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55  )>=SQLITE_AFF_NU
65cd0 4d 45 52 49 43 29 0a 0a 2f 2a 0a 2a 2a 20 54 68  MERIC)../*.** Th
65ce0 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53  e SQLITE_AFF_MAS
65cf0 4b 20 76 61 6c 75 65 73 20 6d 61 73 6b 73 20 6f  K values masks o
65d00 66 66 20 74 68 65 20 73 69 67 6e 69 66 69 63 61  ff the significa
65d10 6e 74 20 62 69 74 73 20 6f 66 20 61 6e 0a 2a 2a  nt bits of an.**
65d20 20 61 66 66 69 6e 69 74 79 20 76 61 6c 75 65 2e   affinity value.
65d30 20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c   .*/.#define SQL
65d40 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 20 20 20  ITE_AFF_MASK    
65d50 20 30 78 36 37 0a 0a 2f 2a 0a 2a 2a 20 41 64 64   0x67../*.** Add
65d60 69 74 69 6f 6e 61 6c 20 62 69 74 20 76 61 6c 75  itional bit valu
65d70 65 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 4f  es that can be O
65d80 52 65 64 20 77 69 74 68 20 61 6e 20 61 66 66 69  Red with an affi
65d90 6e 69 74 79 20 77 69 74 68 6f 75 74 0a 2a 2a 20  nity without.** 
65da0 63 68 61 6e 67 69 6e 67 20 74 68 65 20 61 66 66  changing the aff
65db0 69 6e 69 74 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e  inity..*/.#defin
65dc0 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  e SQLITE_JUMPIFN
65dd0 55 4c 4c 20 20 20 30 78 30 38 20 20 2f 2a 20 6a  ULL   0x08  /* j
65de0 75 6d 70 73 20 69 66 20 65 69 74 68 65 72 20 6f  umps if either o
65df0 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 2a  perand is NULL *
65e00 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
65e10 5f 53 54 4f 52 45 50 32 20 20 20 20 20 20 30 78  _STOREP2      0x
65e20 31 30 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73  10  /* Store res
65e30 75 6c 74 20 69 6e 20 72 65 67 5b 50 32 5d 20 72  ult in reg[P2] r
65e40 61 74 68 65 72 20 74 68 61 6e 20 6a 75 6d 70 20  ather than jump 
65e50 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53  */../*.** Each S
65e60 51 4c 20 74 61 62 6c 65 20 69 73 20 72 65 70 72  QL table is repr
65e70 65 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72  esented in memor
65e80 79 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65  y by an instance
65e90 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f   of the.** follo
65ea0 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a  wing structure..
65eb0 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 7a 4e 61 6d  **.** Table.zNam
65ec0 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  e is the name of
65ed0 20 74 68 65 20 74 61 62 6c 65 2e 20 20 54 68 65   the table.  The
65ee0 20 63 61 73 65 20 6f 66 20 74 68 65 20 6f 72 69   case of the ori
65ef0 67 69 6e 61 6c 0a 2a 2a 20 43 52 45 41 54 45 20  ginal.** CREATE 
65f00 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20  TABLE statement 
65f10 69 73 20 73 74 6f 72 65 64 2c 20 62 75 74 20 63  is stored, but c
65f20 61 73 65 20 69 73 20 6e 6f 74 20 73 69 67 6e 69  ase is not signi
65f30 66 69 63 61 6e 74 20 66 6f 72 0a 2a 2a 20 63 6f  ficant for.** co
65f40 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  mparisons..**.**
65f50 20 54 61 62 6c 65 2e 6e 43 6f 6c 20 69 73 20 74   Table.nCol is t
65f60 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
65f70 75 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62  umns in this tab
65f80 6c 65 2e 20 20 54 61 62 6c 65 2e 61 43 6f 6c 20  le.  Table.aCol 
65f90 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20  is a.** pointer 
65fa0 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 43  to an array of C
65fb0 6f 6c 75 6d 6e 20 73 74 72 75 63 74 75 72 65 73  olumn structures
65fc0 2c 20 6f 6e 65 20 66 6f 72 20 65 61 63 68 20 63  , one for each c
65fd0 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  olumn..**.** If 
65fe0 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e  the table has an
65ff0 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
66000 20 4b 45 59 2c 20 74 68 65 6e 20 54 61 62 6c 65   KEY, then Table
66010 2e 69 50 4b 65 79 20 69 73 20 74 68 65 20 69 6e  .iPKey is the in
66020 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f  dex of.** the co
66030 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 74 68 61  lumn that is tha
66040 74 20 6b 65 79 2e 20 20 20 4f 74 68 65 72 77 69  t key.   Otherwi
66050 73 65 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 69  se Table.iPKey i
66060 73 20 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f 74  s negative.  Not
66070 65 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 64 61  e.** that the da
66080 74 61 74 79 70 65 20 6f 66 20 74 68 65 20 50 52  tatype of the PR
66090 49 4d 41 52 59 20 4b 45 59 20 6d 75 73 74 20 62  IMARY KEY must b
660a0 65 20 49 4e 54 45 47 45 52 20 66 6f 72 20 74 68  e INTEGER for th
660b0 69 73 20 66 69 65 6c 64 20 74 6f 0a 2a 2a 20 62  is field to.** b
660c0 65 20 73 65 74 2e 20 20 41 6e 20 49 4e 54 45 47  e set.  An INTEG
660d0 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69  ER PRIMARY KEY i
660e0 73 20 75 73 65 64 20 61 73 20 74 68 65 20 72 6f  s used as the ro
660f0 77 69 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77  wid for each row
66100 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65   of.** the table
66110 2e 20 20 49 66 20 61 20 74 61 62 6c 65 20 68 61  .  If a table ha
66120 73 20 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49  s no INTEGER PRI
66130 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 61  MARY KEY, then a
66140 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 0a 2a 2a   random rowid.**
66150 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 66 6f   is generated fo
66160 72 20 65 61 63 68 20 72 6f 77 20 6f 66 20 74 68  r each row of th
66170 65 20 74 61 62 6c 65 2e 20 20 54 46 5f 48 61 73  e table.  TF_Has
66180 50 72 69 6d 61 72 79 4b 65 79 20 69 73 20 73 65  PrimaryKey is se
66190 74 20 69 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c  t if.** the tabl
661a0 65 20 68 61 73 20 61 6e 79 20 50 52 49 4d 41 52  e has any PRIMAR
661b0 59 20 4b 45 59 2c 20 49 4e 54 45 47 45 52 20 6f  Y KEY, INTEGER o
661c0 72 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a  r otherwise..**.
661d0 2a 2a 20 54 61 62 6c 65 2e 74 6e 75 6d 20 69 73  ** Table.tnum is
661e0 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
661f0 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 20 42 54   for the root BT
66200 72 65 65 20 70 61 67 65 20 6f 66 20 74 68 65 20  ree page of the 
66210 74 61 62 6c 65 20 69 6e 20 74 68 65 0a 2a 2a 20  table in the.** 
66220 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
66230 49 66 20 54 61 62 6c 65 2e 69 44 62 20 69 73 20  If Table.iDb is 
66240 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
66250 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20   database table 
66260 62 61 63 6b 65 6e 64 0a 2a 2a 20 69 6e 20 73 71  backend.** in sq
66270 6c 69 74 65 2e 61 44 62 5b 5d 2e 20 20 30 20 69  lite.aDb[].  0 i
66280 73 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64  s for the main d
66290 61 74 61 62 61 73 65 20 61 6e 64 20 31 20 69 73  atabase and 1 is
662a0 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 74 68   for the file th
662b0 61 74 0a 2a 2a 20 68 6f 6c 64 73 20 74 65 6d 70  at.** holds temp
662c0 6f 72 61 72 79 20 74 61 62 6c 65 73 20 61 6e 64  orary tables and
662d0 20 69 6e 64 69 63 65 73 2e 20 20 49 66 20 54 46   indices.  If TF
662e0 5f 45 70 68 65 6d 65 72 61 6c 20 69 73 20 73 65  _Ephemeral is se
662f0 74 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 74 61  t.** then the ta
66300 62 6c 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ble is stored in
66310 20 61 20 66 69 6c 65 20 74 68 61 74 20 69 73 20   a file that is 
66320 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65  automatically de
66330 6c 65 74 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68  leted.** when th
66340 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 74 6f  e VDBE cursor to
66350 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 63 6c   the table is cl
66360 6f 73 65 64 2e 20 20 49 6e 20 74 68 69 73 20 63  osed.  In this c
66370 61 73 65 20 54 61 62 6c 65 2e 74 6e 75 6d 20 0a  ase Table.tnum .
66380 2a 2a 20 72 65 66 65 72 73 20 56 44 42 45 20 63  ** refers VDBE c
66390 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 74 68 61  ursor number tha
663a0 74 20 68 6f 6c 64 73 20 74 68 65 20 74 61 62 6c  t holds the tabl
663b0 65 20 6f 70 65 6e 2c 20 6e 6f 74 20 74 6f 20 74  e open, not to t
663c0 68 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20  he root.** page 
663d0 6e 75 6d 62 65 72 2e 20 20 54 72 61 6e 73 69 65  number.  Transie
663e0 6e 74 20 74 61 62 6c 65 73 20 61 72 65 20 75 73  nt tables are us
663f0 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72  ed to hold the r
66400 65 73 75 6c 74 73 20 6f 66 20 61 0a 2a 2a 20 73  esults of a.** s
66410 75 62 2d 71 75 65 72 79 20 74 68 61 74 20 61 70  ub-query that ap
66420 70 65 61 72 73 20 69 6e 73 74 65 61 64 20 6f 66  pears instead of
66430 20 61 20 72 65 61 6c 20 74 61 62 6c 65 20 6e 61   a real table na
66440 6d 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  me in the FROM c
66450 6c 61 75 73 65 20 0a 2a 2a 20 6f 66 20 61 20 53  lause .** of a S
66460 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
66470 0a 2a 2f 0a 73 74 72 75 63 74 20 54 61 62 6c 65  .*/.struct Table
66480 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62   {.  sqlite3 *db
66490 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73  ;         /* Ass
664a0 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65  ociated database
664b0 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 4d 69   connection.  Mi
664c0 67 68 74 20 62 65 20 4e 55 4c 4c 2e 20 2a 2f 0a  ght be NULL. */.
664d0 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
664e0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
664f0 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 76  f the table or v
66500 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 69 50 4b  iew */.  int iPK
66510 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ey;           /*
66520 20 49 66 20 6e 6f 74 20 6e 65 67 61 74 69 76 65   If not negative
66530 2c 20 75 73 65 20 61 43 6f 6c 5b 69 50 4b 65 79  , use aCol[iPKey
66540 5d 20 61 73 20 74 68 65 20 70 72 69 6d 61 72 79  ] as the primary
66550 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 43   key */.  int nC
66560 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ol;            /
66570 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  * Number of colu
66580 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c  mns in this tabl
66590 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61  e */.  Column *a
665a0 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 49  Col;        /* I
665b0 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
665c0 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a   each column */.
665d0 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b    Index *pIndex;
665e0 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f         /* List o
665f0 66 20 53 51 4c 20 69 6e 64 65 78 65 73 20 6f 6e  f SQL indexes on
66600 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f 0a   this table. */.
66610 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20 20 20 20    int tnum;     
66620 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 42         /* Root B
66630 54 72 65 65 20 6e 6f 64 65 20 66 6f 72 20 74 68  Tree node for th
66640 69 73 20 74 61 62 6c 65 20 28 73 65 65 20 6e 6f  is table (see no
66650 74 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 53  te above) */.  S
66660 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20  elect *pSelect; 
66670 20 20 20 20 2f 2a 20 4e 55 4c 4c 20 66 6f 72 20      /* NULL for 
66680 74 61 62 6c 65 73 2e 20 20 50 6f 69 6e 74 73 20  tables.  Points 
66690 74 6f 20 64 65 66 69 6e 69 74 69 6f 6e 20 69 66  to definition if
666a0 20 61 20 76 69 65 77 2e 20 2a 2f 0a 20 20 75 31   a view. */.  u1
666b0 36 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20  6 nRef;         
666c0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
666d0 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73  pointers to this
666e0 20 54 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 74   Table */.  u8 t
666f0 61 62 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20  abFlags;        
66700 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 54 46 5f 2a   /* Mask of TF_*
66710 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20   values */.  u8 
66720 6b 65 79 43 6f 6e 66 3b 20 20 20 20 20 20 20 20  keyConf;        
66730 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20    /* What to do 
66740 69 6e 20 63 61 73 65 20 6f 66 20 75 6e 69 71 75  in case of uniqu
66750 65 6e 65 73 73 20 63 6f 6e 66 6c 69 63 74 20 6f  eness conflict o
66760 6e 20 69 50 4b 65 79 20 2a 2f 0a 20 20 54 72 69  n iPKey */.  Tri
66770 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20  gger *pTrigger; 
66780 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 53 51 4c    /* List of SQL
66790 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74 68 69   triggers on thi
667a0 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 46 4b 65  s table */.  FKe
667b0 79 20 2a 70 46 4b 65 79 3b 20 20 20 20 20 20 20  y *pFKey;       
667c0 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74    /* Linked list
667d0 20 6f 66 20 61 6c 6c 20 66 6f 72 65 69 67 6e 20   of all foreign 
667e0 6b 65 79 73 20 69 6e 20 74 68 69 73 20 74 61 62  keys in this tab
667f0 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43  le */.  char *zC
66800 6f 6c 41 66 66 3b 20 20 20 20 20 20 20 2f 2a 20  olAff;       /* 
66810 53 74 72 69 6e 67 20 64 65 66 69 6e 69 6e 67 20  String defining 
66820 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20  the affinity of 
66830 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23  each column */.#
66840 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
66850 49 54 5f 43 48 45 43 4b 0a 20 20 45 78 70 72 20  IT_CHECK.  Expr 
66860 2a 70 43 68 65 63 6b 3b 20 20 20 20 20 20 20 20  *pCheck;        
66870 2f 2a 20 54 68 65 20 41 4e 44 20 6f 66 20 61 6c  /* The AND of al
66880 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69  l CHECK constrai
66890 6e 74 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69  nts */.#endif.#i
668a0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
668b0 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20 20 69  T_ALTERTABLE.  i
668c0 6e 74 20 61 64 64 43 6f 6c 4f 66 66 73 65 74 3b  nt addColOffset;
668d0 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e      /* Offset in
668e0 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
668f0 6d 74 20 74 6f 20 61 64 64 20 61 20 6e 65 77 20  mt to add a new 
66900 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 65 6e 64 69 66  column */.#endif
66910 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
66920 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
66930 45 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64  E.  Module *pMod
66940 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  ;        /* Poin
66950 74 65 72 20 74 6f 20 74 68 65 20 69 6d 70 6c 65  ter to the imple
66960 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
66970 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 73 71 6c   module */.  sql
66980 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
66990 3b 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20  ; /* Pointer to 
669a0 74 68 65 20 6d 6f 64 75 6c 65 20 69 6e 73 74 61  the module insta
669b0 6e 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 6f  nce */.  int nMo
669c0 64 75 6c 65 41 72 67 3b 20 20 20 20 20 20 2f 2a  duleArg;      /*
669d0 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   Number of argum
669e0 65 6e 74 73 20 74 6f 20 74 68 65 20 6d 6f 64 75  ents to the modu
669f0 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61  le */.  char **a
66a00 7a 4d 6f 64 75 6c 65 41 72 67 3b 20 20 2f 2a 20  zModuleArg;  /* 
66a10 54 65 78 74 20 6f 66 20 61 6c 6c 20 6d 6f 64 75  Text of all modu
66a20 6c 65 20 61 72 67 73 2e 20 5b 30 5d 20 69 73 20  le args. [0] is 
66a30 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 2a 2f 0a 23  module name */.#
66a40 65 6e 64 69 66 0a 20 20 53 63 68 65 6d 61 20 2a  endif.  Schema *
66a50 70 53 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20  pSchema;     /* 
66a60 53 63 68 65 6d 61 20 74 68 61 74 20 63 6f 6e 74  Schema that cont
66a70 61 69 6e 73 20 74 68 69 73 20 74 61 62 6c 65 20  ains this table 
66a80 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 78  */.  Table *pNex
66a90 74 5a 6f 6d 62 69 65 3b 20 20 2f 2a 20 4e 65 78  tZombie;  /* Nex
66aa0 74 20 6f 6e 20 74 68 65 20 50 61 72 73 65 2e 70  t on the Parse.p
66ab0 5a 6f 6d 62 69 65 54 61 62 20 6c 69 73 74 20 2a  ZombieTab list *
66ac0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  /.};../*.** Allo
66ad0 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 54  wed values for T
66ae0 61 62 65 2e 74 61 62 46 6c 61 67 73 2e 0a 2a 2f  abe.tabFlags..*/
66af0 0a 23 64 65 66 69 6e 65 20 54 46 5f 52 65 61 64  .#define TF_Read
66b00 6f 6e 6c 79 20 20 20 20 20 20 20 20 30 78 30 31  only        0x01
66b10 20 20 20 20 2f 2a 20 52 65 61 64 2d 6f 6e 6c 79      /* Read-only
66b20 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20 2a 2f   system table */
66b30 0a 23 64 65 66 69 6e 65 20 54 46 5f 45 70 68 65  .#define TF_Ephe
66b40 6d 65 72 61 6c 20 20 20 20 20 20 20 30 78 30 32  meral       0x02
66b50 20 20 20 20 2f 2a 20 41 6e 20 65 6d 70 68 65 72      /* An empher
66b60 6d 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65  mal table */.#de
66b70 66 69 6e 65 20 54 46 5f 48 61 73 50 72 69 6d 61  fine TF_HasPrima
66b80 72 79 4b 65 79 20 20 20 30 78 30 34 20 20 20 20  ryKey   0x04    
66b90 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 61 20 70  /* Table has a p
66ba0 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a 23 64  rimary key */.#d
66bb0 65 66 69 6e 65 20 54 46 5f 41 75 74 6f 69 6e 63  efine TF_Autoinc
66bc0 72 65 6d 65 6e 74 20 20 20 30 78 30 38 20 20 20  rement   0x08   
66bd0 20 2f 2a 20 49 6e 74 65 67 65 72 20 70 72 69 6d   /* Integer prim
66be0 61 72 79 20 6b 65 79 20 69 73 20 61 75 74 6f 69  ary key is autoi
66bf0 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a 23 64 65 66  ncrement */.#def
66c00 69 6e 65 20 54 46 5f 56 69 72 74 75 61 6c 20 20  ine TF_Virtual  
66c10 20 20 20 20 20 20 20 30 78 31 30 20 20 20 20 2f         0x10    /
66c20 2a 20 49 73 20 61 20 76 69 72 74 75 61 6c 20 74  * Is a virtual t
66c30 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  able */.#define 
66c40 54 46 5f 4e 65 65 64 4d 65 74 61 64 61 74 61 20  TF_NeedMetadata 
66c50 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20 61 43     0x20    /* aC
66c60 6f 6c 5b 5d 2e 7a 54 79 70 65 20 61 6e 64 20 61  ol[].zType and a
66c70 43 6f 6c 5b 5d 2e 70 43 6f 6c 6c 20 6d 69 73 73  Col[].pColl miss
66c80 69 6e 67 20 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20  ing */..../*.** 
66c90 54 65 73 74 20 74 6f 20 73 65 65 20 77 68 65 74  Test to see whet
66ca0 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 74 61 62  her or not a tab
66cb0 6c 65 20 69 73 20 61 20 76 69 72 74 75 61 6c 20  le is a virtual 
66cc0 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 0a  table.  This is.
66cd0 2a 2a 20 64 6f 6e 65 20 61 73 20 61 20 6d 61 63  ** done as a mac
66ce0 72 6f 20 73 6f 20 74 68 61 74 20 69 74 20 77 69  ro so that it wi
66cf0 6c 6c 20 62 65 20 6f 70 74 69 6d 69 7a 65 64 20  ll be optimized 
66d00 6f 75 74 20 77 68 65 6e 20 76 69 72 74 75 61 6c  out when virtual
66d10 0a 2a 2a 20 74 61 62 6c 65 20 73 75 70 70 6f 72  .** table suppor
66d20 74 20 69 73 20 6f 6d 69 74 74 65 64 20 66 72 6f  t is omitted fro
66d30 6d 20 74 68 65 20 62 75 69 6c 64 2e 0a 2a 2f 0a  m the build..*/.
66d40 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
66d50 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
66d60 0a 23 20 20 64 65 66 69 6e 65 20 49 73 56 69 72  .#  define IsVir
66d70 74 75 61 6c 28 58 29 20 20 20 20 20 20 28 28 28  tual(X)      (((
66d80 58 29 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54  X)->tabFlags & T
66d90 46 5f 56 69 72 74 75 61 6c 29 21 3d 30 29 0a 23  F_Virtual)!=0).#
66da0 20 20 64 65 66 69 6e 65 20 49 73 48 69 64 64 65    define IsHidde
66db0 6e 43 6f 6c 75 6d 6e 28 58 29 20 28 28 58 29 2d  nColumn(X) ((X)-
66dc0 3e 69 73 48 69 64 64 65 6e 29 0a 23 65 6c 73 65  >isHidden).#else
66dd0 0a 23 20 20 64 65 66 69 6e 65 20 49 73 56 69 72  .#  define IsVir
66de0 74 75 61 6c 28 58 29 20 20 20 20 20 20 30 0a 23  tual(X)      0.#
66df0 20 20 64 65 66 69 6e 65 20 49 73 48 69 64 64 65    define IsHidde
66e00 6e 43 6f 6c 75 6d 6e 28 58 29 20 30 0a 23 65 6e  nColumn(X) 0.#en
66e10 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20  dif../*.** Each 
66e20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
66e30 74 72 61 69 6e 74 20 69 73 20 61 6e 20 69 6e 73  traint is an ins
66e40 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
66e50 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
66e60 2e 0a 2a 2a 0a 2a 2a 20 41 20 66 6f 72 65 69 67  ..**.** A foreig
66e70 6e 20 6b 65 79 20 69 73 20 61 73 73 6f 63 69 61  n key is associa
66e80 74 65 64 20 77 69 74 68 20 74 77 6f 20 74 61 62  ted with two tab
66e90 6c 65 73 2e 20 20 54 68 65 20 22 66 72 6f 6d 22  les.  The "from"
66ea0 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 74 68 65   table is.** the
66eb0 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f 6e 74   table that cont
66ec0 61 69 6e 73 20 74 68 65 20 52 45 46 45 52 45 4e  ains the REFEREN
66ed0 43 45 53 20 63 6c 61 75 73 65 20 74 68 61 74 20  CES clause that 
66ee0 63 72 65 61 74 65 73 20 74 68 65 20 66 6f 72 65  creates the fore
66ef0 69 67 6e 0a 2a 2a 20 6b 65 79 2e 20 20 54 68 65  ign.** key.  The
66f00 20 22 74 6f 22 20 74 61 62 6c 65 20 69 73 20 74   "to" table is t
66f10 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 69 73  he table that is
66f20 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20 52 45   named in the RE
66f30 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 2e  FERENCES clause.
66f40 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 69  .** Consider thi
66f50 73 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a  s example:.**.**
66f60 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c       CREATE TABL
66f70 45 20 65 78 31 28 0a 2a 2a 20 20 20 20 20 20 20  E ex1(.**       
66f80 61 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  a INTEGER PRIMAR
66f90 59 20 4b 45 59 2c 0a 2a 2a 20 20 20 20 20 20 20  Y KEY,.**       
66fa0 62 20 49 4e 54 45 47 45 52 20 43 4f 4e 53 54 52  b INTEGER CONSTR
66fb0 41 49 4e 54 20 66 6b 31 20 52 45 46 45 52 45 4e  AINT fk1 REFEREN
66fc0 43 45 53 20 65 78 32 28 78 29 0a 2a 2a 20 20 20  CES ex2(x).**   
66fd0 20 20 29 3b 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 66    );.**.** For f
66fe0 6f 72 65 69 67 6e 20 6b 65 79 20 22 66 6b 31 22  oreign key "fk1"
66ff0 2c 20 74 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65  , the from-table
67000 20 69 73 20 22 65 78 31 22 20 61 6e 64 20 74 68   is "ex1" and th
67010 65 20 74 6f 2d 74 61 62 6c 65 20 69 73 20 22 65  e to-table is "e
67020 78 32 22 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20  x2"..**.** Each 
67030 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73  REFERENCES claus
67040 65 20 67 65 6e 65 72 61 74 65 73 20 61 6e 20 69  e generates an i
67050 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
67060 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
67070 72 65 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 61  re.** which is a
67080 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 66  ttached to the f
67090 72 6f 6d 2d 74 61 62 6c 65 2e 20 20 54 68 65 20  rom-table.  The 
670a0 74 6f 2d 74 61 62 6c 65 20 6e 65 65 64 20 6e 6f  to-table need no
670b0 74 20 65 78 69 73 74 20 77 68 65 6e 0a 2a 2a 20  t exist when.** 
670c0 74 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65 20 69  the from-table i
670d0 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20  s created.  The 
670e0 65 78 69 73 74 61 6e 63 65 20 6f 66 20 74 68 65  existance of the
670f0 20 74 6f 2d 74 61 62 6c 65 20 69 73 20 6e 6f 74   to-table is not
67100 20 63 68 65 63 6b 65 64 0a 2a 2a 20 75 6e 74 69   checked.** unti
67110 6c 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20  l an attempt is 
67120 6d 61 64 65 20 74 6f 20 69 6e 73 65 72 74 20 64  made to insert d
67130 61 74 61 20 69 6e 74 6f 20 74 68 65 20 66 72 6f  ata into the fro
67140 6d 2d 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  m-table..**.** T
67150 68 65 20 73 71 6c 69 74 65 2e 61 46 4b 65 79 20  he sqlite.aFKey 
67160 68 61 73 68 20 74 61 62 6c 65 20 73 74 6f 72 65  hash table store
67170 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68  s pointers to th
67180 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  is structure.** 
67190 67 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f  given the name o
671a0 66 20 61 20 74 6f 2d 74 61 62 6c 65 2e 20 20 46  f a to-table.  F
671b0 6f 72 20 65 61 63 68 20 74 6f 2d 74 61 62 6c 65  or each to-table
671c0 2c 20 61 6c 6c 20 66 6f 72 65 69 67 6e 20 6b 65  , all foreign ke
671d0 79 73 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64  ys.** associated
671e0 20 77 69 74 68 20 74 68 61 74 20 74 61 62 6c 65   with that table
671f0 20 61 72 65 20 6f 6e 20 61 20 6c 69 6e 6b 65 64   are on a linked
67200 20 6c 69 73 74 20 75 73 69 6e 67 20 74 68 65 20   list using the 
67210 46 4b 65 79 2e 70 4e 65 78 74 54 6f 0a 2a 2a 20  FKey.pNextTo.** 
67220 66 69 65 6c 64 2e 0a 2a 2f 0a 73 74 72 75 63 74  field..*/.struct
67230 20 46 4b 65 79 20 7b 0a 20 20 54 61 62 6c 65 20   FKey {.  Table 
67240 2a 70 46 72 6f 6d 3b 20 20 20 20 20 2f 2a 20 54  *pFrom;     /* T
67250 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f  he table that co
67260 6e 73 74 61 69 6e 73 20 74 68 65 20 52 45 46 45  nstains the REFE
67270 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 2a 2f  RENCES clause */
67280 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 74 46 72  .  FKey *pNextFr
67290 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74 20 66 6f 72  om;  /* Next for
672a0 65 69 67 6e 20 6b 65 79 20 69 6e 20 70 46 72 6f  eign key in pFro
672b0 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 6f  m */.  char *zTo
672c0 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ;        /* Name
672d0 20 6f 66 20 74 61 62 6c 65 20 74 68 61 74 20 74   of table that t
672e0 68 65 20 6b 65 79 20 70 6f 69 6e 74 73 20 74 6f  he key points to
672f0 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78   */.  FKey *pNex
67300 74 54 6f 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20  tTo;    /* Next 
67310 66 6f 72 65 69 67 6e 20 6b 65 79 20 74 68 61 74  foreign key that
67320 20 70 6f 69 6e 74 73 20 74 6f 20 7a 54 6f 20 2a   points to zTo *
67330 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20  /.  int nCol;   
67340 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
67350 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
67360 69 73 20 6b 65 79 20 2a 2f 0a 20 20 73 74 72 75  is key */.  stru
67370 63 74 20 73 43 6f 6c 4d 61 70 20 7b 20 20 2f 2a  ct sColMap {  /*
67380 20 4d 61 70 70 69 6e 67 20 6f 66 20 63 6f 6c 75   Mapping of colu
67390 6d 6e 73 20 69 6e 20 70 46 72 6f 6d 20 74 6f 20  mns in pFrom to 
673a0 63 6f 6c 75 6d 6e 73 20 69 6e 20 7a 54 6f 20 2a  columns in zTo *
673b0 2f 0a 20 20 20 20 69 6e 74 20 69 46 72 6f 6d 3b  /.    int iFrom;
673c0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
673d0 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 70  x of column in p
673e0 46 72 6f 6d 20 2a 2f 0a 20 20 20 20 63 68 61 72  From */.    char
673f0 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f   *zCol;        /
67400 2a 20 4e 61 6d 65 20 6f 66 20 63 6f 6c 75 6d 6e  * Name of column
67410 20 69 6e 20 7a 54 6f 2e 20 20 49 66 20 30 20 75   in zTo.  If 0 u
67420 73 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a  se PRIMARY KEY *
67430 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 20 20 20 20  /.  } *aCol;    
67440 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74        /* One ent
67450 72 79 20 66 6f 72 20 65 61 63 68 20 6f 66 20 6e  ry for each of n
67460 43 6f 6c 20 63 6f 6c 75 6d 6e 20 73 20 2a 2f 0a  Col column s */.
67470 20 20 75 38 20 69 73 44 65 66 65 72 72 65 64 3b    u8 isDeferred;
67480 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63      /* True if c
67490 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63 6b 69  onstraint checki
674a0 6e 67 20 69 73 20 64 65 66 65 72 72 65 64 20 74  ng is deferred t
674b0 69 6c 6c 20 43 4f 4d 4d 49 54 20 2a 2f 0a 20 20  ill COMMIT */.  
674c0 75 38 20 75 70 64 61 74 65 43 6f 6e 66 3b 20 20  u8 updateConf;  
674d0 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65 73 6f    /* How to reso
674e0 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20 74 68  lve conflicts th
674f0 61 74 20 6f 63 63 75 72 20 6f 6e 20 55 50 44 41  at occur on UPDA
67500 54 45 20 2a 2f 0a 20 20 75 38 20 64 65 6c 65 74  TE */.  u8 delet
67510 65 43 6f 6e 66 3b 20 20 20 20 2f 2a 20 48 6f 77  eConf;    /* How
67520 20 74 6f 20 72 65 73 6f 6c 76 65 20 63 6f 6e 66   to resolve conf
67530 6c 69 63 74 73 20 74 68 61 74 20 6f 63 63 75 72  licts that occur
67540 20 6f 6e 20 44 45 4c 45 54 45 20 2a 2f 0a 20 20   on DELETE */.  
67550 75 38 20 69 6e 73 65 72 74 43 6f 6e 66 3b 20 20  u8 insertConf;  
67560 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65 73 6f    /* How to reso
67570 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20 74 68  lve conflicts th
67580 61 74 20 6f 63 63 75 72 20 6f 6e 20 49 4e 53 45  at occur on INSE
67590 52 54 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  RT */.};../*.** 
675a0 53 51 4c 69 74 65 20 73 75 70 70 6f 72 74 73 20  SQLite supports 
675b0 6d 61 6e 79 20 64 69 66 66 65 72 65 6e 74 20 77  many different w
675c0 61 79 73 20 74 6f 20 72 65 73 6f 6c 76 65 20 61  ays to resolve a
675d0 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65   constraint.** e
675e0 72 72 6f 72 2e 20 20 52 4f 4c 4c 42 41 43 4b 20  rror.  ROLLBACK 
675f0 70 72 6f 63 65 73 73 69 6e 67 20 6d 65 61 6e 73  processing means
67600 20 74 68 61 74 20 61 20 63 6f 6e 73 74 72 61 69   that a constrai
67610 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 0a 2a 2a 20  nt violation.** 
67620 63 61 75 73 65 73 20 74 68 65 20 6f 70 65 72 61  causes the opera
67630 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73 20  tion in process 
67640 74 6f 20 66 61 69 6c 20 61 6e 64 20 66 6f 72 20  to fail and for 
67650 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e  the current tran
67660 73 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65  saction.** to be
67670 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 41   rolled back.  A
67680 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e 67 20  BORT processing 
67690 6d 65 61 6e 73 20 74 68 65 20 6f 70 65 72 61 74  means the operat
676a0 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73 0a 2a  ion in process.*
676b0 2a 20 66 61 69 6c 73 20 61 6e 64 20 61 6e 79 20  * fails and any 
676c0 70 72 69 6f 72 20 63 68 61 6e 67 65 73 20 66 72  prior changes fr
676d0 6f 6d 20 74 68 61 74 20 6f 6e 65 20 6f 70 65 72  om that one oper
676e0 61 74 69 6f 6e 20 61 72 65 20 62 61 63 6b 65 64  ation are backed
676f0 20 6f 75 74 2c 0a 2a 2a 20 62 75 74 20 74 68 65   out,.** but the
67700 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
67710 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e  not rolled back.
67720 20 20 46 41 49 4c 20 70 72 6f 63 65 73 73 69 6e    FAIL processin
67730 67 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20  g means that.** 
67740 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 6e  the operation in
67750 20 70 72 6f 67 72 65 73 73 20 73 74 6f 70 73 20   progress stops 
67760 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e 20 65  and returns an e
67770 72 72 6f 72 20 63 6f 64 65 2e 20 20 42 75 74 20  rror code.  But 
67780 70 72 69 6f 72 0a 2a 2a 20 63 68 61 6e 67 65 73  prior.** changes
67790 20 64 75 65 20 74 6f 20 74 68 65 20 73 61 6d 65   due to the same
677a0 20 6f 70 65 72 61 74 69 6f 6e 20 61 72 65 20 6e   operation are n
677b0 6f 74 20 62 61 63 6b 65 64 20 6f 75 74 20 61 6e  ot backed out an
677c0 64 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a  d no rollback.**
677d0 20 6f 63 63 75 72 73 2e 20 20 49 47 4e 4f 52 45   occurs.  IGNORE
677e0 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
677f0 70 61 72 74 69 63 75 6c 61 72 20 72 6f 77 20 74  particular row t
67800 68 61 74 20 63 61 75 73 65 64 20 74 68 65 20 63  hat caused the c
67810 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72  onstraint.** err
67820 6f 72 20 69 73 20 6e 6f 74 20 69 6e 73 65 72 74  or is not insert
67830 65 64 20 6f 72 20 75 70 64 61 74 65 64 2e 20 20  ed or updated.  
67840 50 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74 69  Processing conti
67850 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f  nues and no erro
67860 72 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64  r.** is returned
67870 2e 20 20 52 45 50 4c 41 43 45 20 6d 65 61 6e 73  .  REPLACE means
67880 20 74 68 61 74 20 70 72 65 65 78 69 73 74 69 6e   that preexistin
67890 67 20 64 61 74 61 62 61 73 65 20 72 6f 77 73 20  g database rows 
678a0 74 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 61  that caused.** a
678b0 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69   UNIQUE constrai
678c0 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 61 72 65  nt violation are
678d0 20 72 65 6d 6f 76 65 64 20 73 6f 20 74 68 61 74   removed so that
678e0 20 74 68 65 20 6e 65 77 20 69 6e 73 65 72 74 20   the new insert 
678f0 6f 72 0a 2a 2a 20 75 70 64 61 74 65 20 63 61 6e  or.** update can
67900 20 70 72 6f 63 65 65 64 2e 20 20 50 72 6f 63 65   proceed.  Proce
67910 73 73 69 6e 67 20 63 6f 6e 74 69 6e 75 65 73 20  ssing continues 
67920 61 6e 64 20 6e 6f 20 65 72 72 6f 72 20 69 73 20  and no error is 
67930 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  reported..**.** 
67940 52 45 53 54 52 49 43 54 2c 20 53 45 54 4e 55 4c  RESTRICT, SETNUL
67950 4c 2c 20 61 6e 64 20 43 41 53 43 41 44 45 20 61  L, and CASCADE a
67960 63 74 69 6f 6e 73 20 61 70 70 6c 79 20 6f 6e 6c  ctions apply onl
67970 79 20 74 6f 20 66 6f 72 65 69 67 6e 20 6b 65 79  y to foreign key
67980 73 2e 0a 2a 2a 20 52 45 53 54 52 49 43 54 20 69  s..** RESTRICT i
67990 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 41 42  s the same as AB
679a0 4f 52 54 20 66 6f 72 20 49 4d 4d 45 44 49 41 54  ORT for IMMEDIAT
679b0 45 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61  E foreign keys a
679c0 6e 64 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61  nd the.** same a
679d0 73 20 52 4f 4c 4c 42 41 43 4b 20 66 6f 72 20 44  s ROLLBACK for D
679e0 45 46 45 52 52 45 44 20 6b 65 79 73 2e 20 20 53  EFERRED keys.  S
679f0 45 54 4e 55 4c 4c 20 6d 65 61 6e 73 20 74 68 61  ETNULL means tha
67a00 74 20 74 68 65 20 66 6f 72 65 69 67 6e 0a 2a 2a  t the foreign.**
67a10 20 6b 65 79 20 69 73 20 73 65 74 20 74 6f 20 4e   key is set to N
67a20 55 4c 4c 2e 20 20 43 41 53 43 41 44 45 20 6d 65  ULL.  CASCADE me
67a30 61 6e 73 20 74 68 61 74 20 61 20 44 45 4c 45 54  ans that a DELET
67a40 45 20 6f 72 20 55 50 44 41 54 45 20 6f 66 20 74  E or UPDATE of t
67a50 68 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 64  he.** referenced
67a60 20 74 61 62 6c 65 20 72 6f 77 20 69 73 20 70 72   table row is pr
67a70 6f 70 61 67 61 74 65 64 20 69 6e 74 6f 20 74 68  opagated into th
67a80 65 20 72 6f 77 20 74 68 61 74 20 68 6f 6c 64 73  e row that holds
67a90 20 74 68 65 0a 2a 2a 20 66 6f 72 65 69 67 6e 20   the.** foreign 
67aa0 6b 65 79 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20  key..** .** The 
67ab0 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d 62 6f 6c  following symbol
67ac0 69 63 20 76 61 6c 75 65 73 20 61 72 65 20 75 73  ic values are us
67ad0 65 64 20 74 6f 20 72 65 63 6f 72 64 20 77 68 69  ed to record whi
67ae0 63 68 20 74 79 70 65 0a 2a 2a 20 6f 66 20 61 63  ch type.** of ac
67af0 74 69 6f 6e 20 74 6f 20 74 61 6b 65 2e 0a 2a 2f  tion to take..*/
67b00 0a 23 64 65 66 69 6e 65 20 4f 45 5f 4e 6f 6e 65  .#define OE_None
67b10 20 20 20 20 20 30 20 20 20 2f 2a 20 54 68 65 72       0   /* Ther
67b20 65 20 69 73 20 6e 6f 20 63 6f 6e 73 74 72 61 69  e is no constrai
67b30 6e 74 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 23  nt to check */.#
67b40 64 65 66 69 6e 65 20 4f 45 5f 52 6f 6c 6c 62 61  define OE_Rollba
67b50 63 6b 20 31 20 20 20 2f 2a 20 46 61 69 6c 20 74  ck 1   /* Fail t
67b60 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e 64  he operation and
67b70 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72   rollback the tr
67b80 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65  ansaction */.#de
67b90 66 69 6e 65 20 4f 45 5f 41 62 6f 72 74 20 20 20  fine OE_Abort   
67ba0 20 32 20 20 20 2f 2a 20 42 61 63 6b 20 6f 75 74   2   /* Back out
67bb0 20 63 68 61 6e 67 65 73 20 62 75 74 20 64 6f 20   changes but do 
67bc0 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61 6e  no rollback tran
67bd0 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69  saction */.#defi
67be0 6e 65 20 4f 45 5f 46 61 69 6c 20 20 20 20 20 33  ne OE_Fail     3
67bf0 20 20 20 2f 2a 20 53 74 6f 70 20 74 68 65 20 6f     /* Stop the o
67c00 70 65 72 61 74 69 6f 6e 20 62 75 74 20 6c 65 61  peration but lea
67c10 76 65 20 61 6c 6c 20 70 72 69 6f 72 20 63 68 61  ve all prior cha
67c20 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  nges */.#define 
67c30 4f 45 5f 49 67 6e 6f 72 65 20 20 20 34 20 20 20  OE_Ignore   4   
67c40 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 20 65 72  /* Ignore the er
67c50 72 6f 72 2e 20 44 6f 20 6e 6f 74 20 64 6f 20 74  ror. Do not do t
67c60 68 65 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44  he INSERT or UPD
67c70 41 54 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  ATE */.#define O
67c80 45 5f 52 65 70 6c 61 63 65 20 20 35 20 20 20 2f  E_Replace  5   /
67c90 2a 20 44 65 6c 65 74 65 20 65 78 69 73 74 69 6e  * Delete existin
67ca0 67 20 72 65 63 6f 72 64 2c 20 74 68 65 6e 20 64  g record, then d
67cb0 6f 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41  o INSERT or UPDA
67cc0 54 45 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 4f  TE */..#define O
67cd0 45 5f 52 65 73 74 72 69 63 74 20 36 20 20 20 2f  E_Restrict 6   /
67ce0 2a 20 4f 45 5f 41 62 6f 72 74 20 66 6f 72 20 49  * OE_Abort for I
67cf0 4d 4d 45 44 49 41 54 45 2c 20 4f 45 5f 52 6f 6c  MMEDIATE, OE_Rol
67d00 6c 62 61 63 6b 20 66 6f 72 20 44 45 46 45 52 52  lback for DEFERR
67d10 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45  ED */.#define OE
67d20 5f 53 65 74 4e 75 6c 6c 20 20 37 20 20 20 2f 2a  _SetNull  7   /*
67d30 20 53 65 74 20 74 68 65 20 66 6f 72 65 69 67 6e   Set the foreign
67d40 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 4e 55   key value to NU
67d50 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45  LL */.#define OE
67d60 5f 53 65 74 44 66 6c 74 20 20 38 20 20 20 2f 2a  _SetDflt  8   /*
67d70 20 53 65 74 20 74 68 65 20 66 6f 72 65 69 67 6e   Set the foreign
67d80 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 69 74   key value to it
67d90 73 20 64 65 66 61 75 6c 74 20 2a 2f 0a 23 64 65  s default */.#de
67da0 66 69 6e 65 20 4f 45 5f 43 61 73 63 61 64 65 20  fine OE_Cascade 
67db0 20 39 20 20 20 2f 2a 20 43 61 73 63 61 64 65 20   9   /* Cascade 
67dc0 74 68 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 0a  the changes */..
67dd0 23 64 65 66 69 6e 65 20 4f 45 5f 44 65 66 61 75  #define OE_Defau
67de0 6c 74 20 20 39 39 20 20 2f 2a 20 44 6f 20 77 68  lt  99  /* Do wh
67df0 61 74 65 76 65 72 20 74 68 65 20 64 65 66 61 75  atever the defau
67e00 6c 74 20 61 63 74 69 6f 6e 20 69 73 20 2a 2f 0a  lt action is */.
67e10 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
67e20 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
67e30 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69  wing structure i
67e40 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  s passed as the 
67e50 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e  first.** argumen
67e60 74 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 65  t to sqlite3Vdbe
67e70 4b 65 79 43 6f 6d 70 61 72 65 20 61 6e 64 20 69  KeyCompare and i
67e80 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 74 72 6f  s used to contro
67e90 6c 20 74 68 65 20 0a 2a 2a 20 63 6f 6d 70 61 72  l the .** compar
67ea0 69 73 6f 6e 20 6f 66 20 74 68 65 20 74 77 6f 20  ison of the two 
67eb0 69 6e 64 65 78 20 6b 65 79 73 2e 0a 2a 2f 0a 73  index keys..*/.s
67ec0 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 7b 0a  truct KeyInfo {.
67ed0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
67ee0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
67ef0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
67f00 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20   */.  u8 enc;   
67f10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78            /* Tex
67f20 74 20 65 6e 63 6f 64 69 6e 67 20 2d 20 6f 6e 65  t encoding - one
67f30 20 6f 66 20 74 68 65 20 54 45 58 54 5f 55 74 66   of the TEXT_Utf
67f40 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 31  * values */.  u1
67f50 36 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20  6 nField;       
67f60 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
67f70 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6c 6c 5b  ntries in aColl[
67f80 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 53 6f 72 74  ] */.  u8 *aSort
67f90 4f 72 64 65 72 3b 20 20 20 20 20 2f 2a 20 49 66  Order;     /* If
67fa0 20 64 65 66 69 6e 65 64 20 61 6e 20 61 53 6f 72   defined an aSor
67fb0 74 4f 72 64 65 72 5b 69 5d 20 69 73 20 74 72 75  tOrder[i] is tru
67fc0 65 2c 20 73 6f 72 74 20 44 45 53 43 20 2a 2f 0a  e, sort DESC */.
67fd0 20 20 43 6f 6c 6c 53 65 71 20 2a 61 43 6f 6c 6c    CollSeq *aColl
67fe0 5b 31 5d 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69  [1];  /* Collati
67ff0 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20  ng sequence for 
68000 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74 68 65  each term of the
68010 20 6b 65 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a   key */.};../*.*
68020 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
68030 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
68040 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69  tructure holds i
68050 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
68060 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 69 6e 64   a.** single ind
68070 65 78 20 72 65 63 6f 72 64 20 74 68 61 74 20 68  ex record that h
68080 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
68090 70 61 72 73 65 64 20 6f 75 74 20 69 6e 74 6f 20  parsed out into 
680a0 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20 76 61  individual.** va
680b0 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65  lues..**.** A re
680c0 63 6f 72 64 20 69 73 20 61 6e 20 6f 62 6a 65 63  cord is an objec
680d0 74 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  t that contains 
680e0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 66 69 65 6c  one or more fiel
680f0 64 73 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 20 52  ds of data..** R
68100 65 63 6f 72 64 73 20 61 72 65 20 75 73 65 64 20  ecords are used 
68110 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6e  to store the con
68120 74 65 6e 74 20 6f 66 20 61 20 74 61 62 6c 65 20  tent of a table 
68130 72 6f 77 20 61 6e 64 20 74 6f 20 73 74 6f 72 65  row and to store
68140 0a 2a 2a 20 74 68 65 20 6b 65 79 20 6f 66 20 61  .** the key of a
68150 6e 20 69 6e 64 65 78 2e 20 20 41 20 62 6c 6f 62  n index.  A blob
68160 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 72   encoding of a r
68170 65 63 6f 72 64 20 69 73 20 63 72 65 61 74 65 64  ecord is created
68180 20 62 79 0a 2a 2a 20 74 68 65 20 4f 50 5f 4d 61   by.** the OP_Ma
68190 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 20  keRecord opcode 
681a0 6f 66 20 74 68 65 20 56 44 42 45 20 61 6e 64 20  of the VDBE and 
681b0 69 73 20 64 69 73 61 73 73 65 6d 62 6c 69 65 64  is disassemblied
681c0 20 62 79 20 74 68 65 0a 2a 2a 20 4f 50 5f 43 6f   by the.** OP_Co
681d0 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a  lumn opcode..**.
681e0 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72  ** This structur
681f0 65 20 68 6f 6c 64 73 20 61 20 72 65 63 6f 72 64  e holds a record
68200 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61 64   that has alread
68210 79 20 62 65 65 6e 20 64 69 73 61 73 73 65 6d 62  y been disassemb
68220 6c 65 64 0a 2a 2a 20 69 6e 74 6f 20 69 74 73 20  led.** into its 
68230 63 6f 6e 73 74 69 74 75 74 65 6e 74 20 66 69 65  constitutent fie
68240 6c 64 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 55  lds..*/.struct U
68250 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 7b 0a  npackedRecord {.
68260 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49    KeyInfo *pKeyI
68270 6e 66 6f 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69  nfo;  /* Collati
68280 6f 6e 20 61 6e 64 20 73 6f 72 74 2d 6f 72 64 65  on and sort-orde
68290 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f  r information */
682a0 0a 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 20  .  u16 nField;  
682b0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
682c0 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
682d0 70 4d 65 6d 5b 5d 20 2a 2f 0a 20 20 75 31 36 20  pMem[] */.  u16 
682e0 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  flags;          
682f0 2f 2a 20 42 6f 6f 6c 65 61 6e 20 73 65 74 74 69  /* Boolean setti
68300 6e 67 73 2e 20 20 55 4e 50 41 43 4b 45 44 5f 2e  ngs.  UNPACKED_.
68310 2e 2e 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 4d 65  .. below */.  Me
68320 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20 20  m *aMem;        
68330 20 20 2f 2a 20 56 61 6c 75 65 73 20 2a 2f 0a 7d    /* Values */.}
68340 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64  ;../*.** Allowed
68350 20 76 61 6c 75 65 73 20 6f 66 20 55 6e 70 61 63   values of Unpac
68360 6b 65 64 52 65 63 6f 72 64 2e 66 6c 61 67 73 0a  kedRecord.flags.
68370 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43  */.#define UNPAC
68380 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 20 20  KED_NEED_FREE   
68390 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4d 65 6d    0x0001  /* Mem
683a0 6f 72 79 20 69 73 20 66 72 6f 6d 20 73 71 6c 69  ory is from sqli
683b0 74 65 33 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23  te3Malloc() */.#
683c0 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f  define UNPACKED_
683d0 4e 45 45 44 5f 44 45 53 54 52 4f 59 20 20 30 78  NEED_DESTROY  0x
683e0 30 30 30 32 20 20 2f 2a 20 61 70 4d 65 6d 5b 5d  0002  /* apMem[]
683f0 73 20 73 68 6f 75 6c 64 20 61 6c 6c 20 62 65 20  s should all be 
68400 64 65 73 74 72 6f 79 65 64 20 2a 2f 0a 23 64 65  destroyed */.#de
68410 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 49 47  fine UNPACKED_IG
68420 4e 4f 52 45 5f 52 4f 57 49 44 20 20 30 78 30 30  NORE_ROWID  0x00
68430 30 34 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 72  04  /* Ignore tr
68440 61 69 6c 69 6e 67 20 72 6f 77 69 64 20 6f 6e 20  ailing rowid on 
68450 6b 65 79 31 20 2a 2f 0a 23 64 65 66 69 6e 65 20  key1 */.#define 
68460 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59  UNPACKED_INCRKEY
68470 20 20 20 20 20 20 20 30 78 30 30 30 38 20 20 2f         0x0008  /
68480 2a 20 4d 61 6b 65 20 74 68 69 73 20 6b 65 79 20  * Make this key 
68490 61 6e 20 65 70 73 69 6c 6f 6e 20 6c 61 72 67 65  an epsilon large
684a0 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50  r */.#define UNP
684b0 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54  ACKED_PREFIX_MAT
684c0 43 48 20 20 30 78 30 30 31 30 20 20 2f 2a 20 41  CH  0x0010  /* A
684d0 20 70 72 65 66 69 78 20 6d 61 74 63 68 20 69 73   prefix match is
684e0 20 63 6f 6e 73 69 64 65 72 65 64 20 4f 4b 20 2a   considered OK *
684f0 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51  /../*.** Each SQ
68500 4c 20 69 6e 64 65 78 20 69 73 20 72 65 70 72 65  L index is repre
68510 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79  sented in memory
68520 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e   by an.** instan
68530 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
68540 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ing structure..*
68550 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73  *.** The columns
68560 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 68   of the table th
68570 61 74 20 61 72 65 20 74 6f 20 62 65 20 69 6e 64  at are to be ind
68580 65 78 65 64 20 61 72 65 20 64 65 73 63 72 69 62  exed are describ
68590 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 61 69 43  ed.** by the aiC
685a0 6f 6c 75 6d 6e 5b 5d 20 66 69 65 6c 64 20 6f 66  olumn[] field of
685b0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e   this structure.
685c0 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 73    For example, s
685d0 75 70 70 6f 73 65 0a 2a 2a 20 77 65 20 68 61 76  uppose.** we hav
685e0 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  e the following 
685f0 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 3a  table and index:
68600 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54  .**.**     CREAT
68610 45 20 54 41 42 4c 45 20 45 78 31 28 63 31 20 69  E TABLE Ex1(c1 i
68620 6e 74 2c 20 63 32 20 69 6e 74 2c 20 63 33 20 74  nt, c2 int, c3 t
68630 65 78 74 29 3b 0a 2a 2a 20 20 20 20 20 43 52 45  ext);.**     CRE
68640 41 54 45 20 49 4e 44 45 58 20 45 78 32 20 4f 4e  ATE INDEX Ex2 ON
68650 20 45 78 31 28 63 33 2c 63 31 29 3b 0a 2a 2a 0a   Ex1(c3,c1);.**.
68660 2a 2a 20 49 6e 20 74 68 65 20 54 61 62 6c 65 20  ** In the Table 
68670 73 74 72 75 63 74 75 72 65 20 64 65 73 63 72 69  structure descri
68680 62 69 6e 67 20 45 78 31 2c 20 6e 43 6f 6c 3d 3d  bing Ex1, nCol==
68690 33 20 62 65 63 61 75 73 65 20 74 68 65 72 65 20  3 because there 
686a0 61 72 65 0a 2a 2a 20 74 68 72 65 65 20 63 6f 6c  are.** three col
686b0 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c  umns in the tabl
686c0 65 2e 20 20 49 6e 20 74 68 65 20 49 6e 64 65 78  e.  In the Index
686d0 20 73 74 72 75 63 74 75 72 65 20 64 65 73 63 72   structure descr
686e0 69 62 69 6e 67 0a 2a 2a 20 45 78 32 2c 20 6e 43  ibing.** Ex2, nC
686f0 6f 6c 75 6d 6e 3d 3d 32 20 73 69 6e 63 65 20 32  olumn==2 since 2
68700 20 6f 66 20 74 68 65 20 33 20 63 6f 6c 75 6d 6e   of the 3 column
68710 73 20 6f 66 20 45 78 31 20 61 72 65 20 69 6e 64  s of Ex1 are ind
68720 65 78 65 64 2e 0a 2a 2a 20 54 68 65 20 76 61 6c  exed..** The val
68730 75 65 20 6f 66 20 61 69 43 6f 6c 75 6d 6e 20 69  ue of aiColumn i
68740 73 20 7b 32 2c 20 30 7d 2e 20 20 61 69 43 6f 6c  s {2, 0}.  aiCol
68750 75 6d 6e 5b 30 5d 3d 3d 32 20 62 65 63 61 75 73  umn[0]==2 becaus
68760 65 20 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 20  e the .** first 
68770 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 69 6e 64  column to be ind
68780 65 78 65 64 20 28 63 33 29 20 68 61 73 20 61 6e  exed (c3) has an
68790 20 69 6e 64 65 78 20 6f 66 20 32 20 69 6e 20 45   index of 2 in E
687a0 78 31 2e 61 43 6f 6c 5b 5d 2e 0a 2a 2a 20 54 68  x1.aCol[]..** Th
687b0 65 20 73 65 63 6f 6e 64 20 63 6f 6c 75 6d 6e 20  e second column 
687c0 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 28 63  to be indexed (c
687d0 31 29 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20  1) has an index 
687e0 6f 66 20 30 20 69 6e 0a 2a 2a 20 45 78 31 2e 61  of 0 in.** Ex1.a
687f0 43 6f 6c 5b 5d 2c 20 68 65 6e 63 65 20 45 78 32  Col[], hence Ex2
68800 2e 61 69 43 6f 6c 75 6d 6e 5b 31 5d 3d 3d 30 2e  .aiColumn[1]==0.
68810 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 6e 64 65 78  .**.** The Index
68820 2e 6f 6e 45 72 72 6f 72 20 66 69 65 6c 64 20 64  .onError field d
68830 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65  etermines whethe
68840 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e 64  r or not the ind
68850 65 78 65 64 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20  exed columns.** 
68860 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65 20 61  must be unique a
68870 6e 64 20 77 68 61 74 20 74 6f 20 64 6f 20 69 66  nd what to do if
68880 20 74 68 65 79 20 61 72 65 20 6e 6f 74 2e 20 20   they are not.  
68890 57 68 65 6e 20 49 6e 64 65 78 2e 6f 6e 45 72 72  When Index.onErr
688a0 6f 72 3d 4f 45 5f 4e 6f 6e 65 2c 0a 2a 2a 20 69  or=OE_None,.** i
688b0 74 20 6d 65 61 6e 73 20 74 68 69 73 20 69 73 20  t means this is 
688c0 6e 6f 74 20 61 20 75 6e 69 71 75 65 20 69 6e 64  not a unique ind
688d0 65 78 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69  ex.  Otherwise i
688e0 74 20 69 73 20 61 20 75 6e 69 71 75 65 20 69 6e  t is a unique in
688f0 64 65 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20 76  dex.** and the v
68900 61 6c 75 65 20 6f 66 20 49 6e 64 65 78 2e 6f 6e  alue of Index.on
68910 45 72 72 6f 72 20 69 6e 64 69 63 61 74 65 20 74  Error indicate t
68920 68 65 20 77 68 69 63 68 20 63 6f 6e 66 6c 69 63  he which conflic
68930 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 0a 2a 2a  t resolution .**
68940 20 61 6c 67 6f 72 69 74 68 6d 20 74 6f 20 65 6d   algorithm to em
68950 70 6c 6f 79 20 77 68 65 6e 65 76 65 72 20 61 6e  ploy whenever an
68960 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
68970 20 74 6f 20 69 6e 73 65 72 74 20 61 20 6e 6f 6e   to insert a non
68980 2d 75 6e 69 71 75 65 0a 2a 2a 20 65 6c 65 6d 65  -unique.** eleme
68990 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49 6e  nt..*/.struct In
689a0 64 65 78 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e  dex {.  char *zN
689b0 61 6d 65 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ame;     /* Name
689c0 20 6f 66 20 74 68 69 73 20 69 6e 64 65 78 20 2a   of this index *
689d0 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b  /.  int nColumn;
689e0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
689f0 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
68a00 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20 74   table used by t
68a10 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69  his index */.  i
68a20 6e 74 20 2a 61 69 43 6f 6c 75 6d 6e 3b 20 20 20  nt *aiColumn;   
68a30 2f 2a 20 57 68 69 63 68 20 63 6f 6c 75 6d 6e 73  /* Which columns
68a40 20 61 72 65 20 75 73 65 64 20 62 79 20 74 68 69   are used by thi
68a50 73 20 69 6e 64 65 78 2e 20 20 31 73 74 20 69 73  s index.  1st is
68a60 20 30 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64   0 */.  unsigned
68a70 20 2a 61 69 52 6f 77 45 73 74 3b 20 2f 2a 20 52   *aiRowEst; /* R
68a80 65 73 75 6c 74 20 6f 66 20 41 4e 41 4c 59 5a 45  esult of ANALYZE
68a90 3a 20 45 73 74 2e 20 72 6f 77 73 20 73 65 6c 65  : Est. rows sele
68aa0 63 74 65 64 20 62 79 20 65 61 63 68 20 63 6f 6c  cted by each col
68ab0 75 6d 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  umn */.  Table *
68ac0 70 54 61 62 6c 65 3b 20 20 20 2f 2a 20 54 68 65  pTable;   /* The
68ad0 20 53 51 4c 20 74 61 62 6c 65 20 62 65 69 6e 67   SQL table being
68ae0 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e   indexed */.  in
68af0 74 20 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 2f  t tnum;        /
68b00 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e  * Page containin
68b10 67 20 72 6f 6f 74 20 6f 66 20 74 68 69 73 20 69  g root of this i
68b20 6e 64 65 78 20 69 6e 20 64 61 74 61 62 61 73 65  ndex in database
68b30 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 6f 6e   file */.  u8 on
68b40 45 72 72 6f 72 3b 20 20 20 20 20 20 2f 2a 20 4f  Error;      /* O
68b50 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f  E_Abort, OE_Igno
68b60 72 65 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20  re, OE_Replace, 
68b70 6f 72 20 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a 20 20  or OE_None */.  
68b80 75 38 20 61 75 74 6f 49 6e 64 65 78 3b 20 20 20  u8 autoIndex;   
68b90 20 2f 2a 20 54 72 75 65 20 69 66 20 69 73 20 61   /* True if is a
68ba0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65  utomatically cre
68bb0 61 74 65 64 20 28 65 78 3a 20 62 79 20 55 4e 49  ated (ex: by UNI
68bc0 51 55 45 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a  QUE) */.  char *
68bd0 7a 43 6f 6c 41 66 66 3b 20 20 20 2f 2a 20 53 74  zColAff;   /* St
68be0 72 69 6e 67 20 64 65 66 69 6e 69 6e 67 20 74 68  ring defining th
68bf0 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 65 61  e affinity of ea
68c00 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49  ch column */.  I
68c10 6e 64 65 78 20 2a 70 4e 65 78 74 3b 20 20 20 20  ndex *pNext;    
68c20 2f 2a 20 54 68 65 20 6e 65 78 74 20 69 6e 64 65  /* The next inde
68c30 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  x associated wit
68c40 68 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65  h the same table
68c50 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53   */.  Schema *pS
68c60 63 68 65 6d 61 3b 20 2f 2a 20 53 63 68 65 6d 61  chema; /* Schema
68c70 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69 73   containing this
68c80 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 2a   index */.  u8 *
68c90 61 53 6f 72 74 4f 72 64 65 72 3b 20 20 2f 2a 20  aSortOrder;  /* 
68ca0 41 72 72 61 79 20 6f 66 20 73 69 7a 65 20 49 6e  Array of size In
68cb0 64 65 78 2e 6e 43 6f 6c 75 6d 6e 2e 20 54 72 75  dex.nColumn. Tru
68cc0 65 3d 3d 44 45 53 43 2c 20 46 61 6c 73 65 3d 3d  e==DESC, False==
68cd0 41 53 43 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  ASC */.  char **
68ce0 61 7a 43 6f 6c 6c 3b 20 20 20 2f 2a 20 41 72 72  azColl;   /* Arr
68cf0 61 79 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20  ay of collation 
68d00 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 73 20 66  sequence names f
68d10 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 7d 3b 0a 0a  or index */.};..
68d20 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 6f 6b 65 6e  /*.** Each token
68d30 20 63 6f 6d 69 6e 67 20 6f 75 74 20 6f 66 20 74   coming out of t
68d40 68 65 20 6c 65 78 65 72 20 69 73 20 61 6e 20 69  he lexer is an i
68d50 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68  nstance of.** th
68d60 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  is structure.  T
68d70 6f 6b 65 6e 73 20 61 72 65 20 61 6c 73 6f 20 75  okens are also u
68d80 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61  sed as part of a
68d90 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a  n expression..**
68da0 0a 2a 2a 20 4e 6f 74 65 20 69 66 20 54 6f 6b 65  .** Note if Toke
68db0 6e 2e 7a 3d 3d 30 20 74 68 65 6e 20 54 6f 6b 65  n.z==0 then Toke
68dc0 6e 2e 64 79 6e 20 61 6e 64 20 54 6f 6b 65 6e 2e  n.dyn and Token.
68dd0 6e 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20  n are undefined 
68de0 61 6e 64 0a 2a 2a 20 6d 61 79 20 63 6f 6e 74 61  and.** may conta
68df0 69 6e 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 73  in random values
68e00 2e 20 20 44 6f 20 6e 6f 74 20 6d 61 6b 65 20 61  .  Do not make a
68e10 6e 79 20 61 73 73 75 70 74 69 6f 6e 73 20 61 62  ny assuptions ab
68e20 6f 75 74 20 54 6f 6b 65 6e 2e 64 79 6e 0a 2a 2a  out Token.dyn.**
68e30 20 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 77 68 65   and Token.n whe
68e40 6e 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 2e 0a 2a 2f  n Token.z==0..*/
68e50 0a 73 74 72 75 63 74 20 54 6f 6b 65 6e 20 7b 0a  .struct Token {.
68e60 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
68e70 20 63 68 61 72 20 2a 7a 3b 20 2f 2a 20 54 65 78   char *z; /* Tex
68e80 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 2e 20  t of the token. 
68e90 20 4e 6f 74 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e   Not NULL-termin
68ea0 61 74 65 64 21 20 2a 2f 0a 20 20 75 6e 73 69 67  ated! */.  unsig
68eb0 6e 65 64 20 64 79 6e 20 20 3a 20 31 3b 20 20 20  ned dyn  : 1;   
68ec0 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 6d     /* True for m
68ed0 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 2c 20  alloced memory, 
68ee0 66 61 6c 73 65 20 66 6f 72 20 73 74 61 74 69 63  false for static
68ef0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6e   */.  unsigned n
68f00 20 20 20 20 3a 20 33 31 3b 20 20 20 20 20 2f 2a      : 31;     /*
68f10 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61   Number of chara
68f20 63 74 65 72 73 20 69 6e 20 74 68 69 73 20 74 6f  cters in this to
68f30 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ken */.};../*.**
68f40 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
68f50 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63  this structure c
68f60 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74  ontains informat
68f70 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65  ion needed to ge
68f80 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66  nerate.** code f
68f90 6f 72 20 61 20 53 45 4c 45 43 54 20 74 68 61 74  or a SELECT that
68fa0 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67   contains aggreg
68fb0 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a  ate functions..*
68fc0 2a 0a 2a 2a 20 49 66 20 45 78 70 72 2e 6f 70 3d  *.** If Expr.op=
68fd0 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 6f  =TK_AGG_COLUMN o
68fe0 72 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f  r TK_AGG_FUNCTIO
68ff0 4e 20 74 68 65 6e 20 45 78 70 72 2e 70 41 67 67  N then Expr.pAgg
69000 49 6e 66 6f 20 69 73 20 61 0a 2a 2a 20 70 6f 69  Info is a.** poi
69010 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 72  nter to this str
69020 75 63 74 75 72 65 2e 20 20 54 68 65 20 45 78 70  ucture.  The Exp
69030 72 2e 69 43 6f 6c 75 6d 6e 20 66 69 65 6c 64 20  r.iColumn field 
69040 69 73 20 74 68 65 20 69 6e 64 65 78 20 69 6e 0a  is the index in.
69050 2a 2a 20 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b  ** AggInfo.aCol[
69060 5d 20 6f 72 20 41 67 67 49 6e 66 6f 2e 61 46 75  ] or AggInfo.aFu
69070 6e 63 5b 5d 20 6f 66 20 69 6e 66 6f 72 6d 61 74  nc[] of informat
69080 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65  ion needed to ge
69090 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66  nerate.** code f
690a0 6f 72 20 74 68 61 74 20 6e 6f 64 65 2e 0a 2a 2a  or that node..**
690b0 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 70 47 72 6f  .** AggInfo.pGro
690c0 75 70 42 79 20 61 6e 64 20 41 67 67 49 6e 66 6f  upBy and AggInfo
690d0 2e 61 46 75 6e 63 2e 70 45 78 70 72 20 70 6f 69  .aFunc.pExpr poi
690e0 6e 74 20 74 6f 20 66 69 65 6c 64 73 20 77 69 74  nt to fields wit
690f0 68 69 6e 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69  hin the.** origi
69100 6e 61 6c 20 53 65 6c 65 63 74 20 73 74 72 75 63  nal Select struc
69110 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69  ture that descri
69120 62 65 73 20 74 68 65 20 53 45 4c 45 43 54 20 73  bes the SELECT s
69130 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 73 65  tatement.  These
69140 0a 2a 2a 20 66 69 65 6c 64 73 20 64 6f 20 6e 6f  .** fields do no
69150 74 20 6e 65 65 64 20 74 6f 20 62 65 20 66 72 65  t need to be fre
69160 65 64 20 77 68 65 6e 20 64 65 61 6c 6c 6f 63 61  ed when dealloca
69170 74 69 6e 67 20 74 68 65 20 41 67 67 49 6e 66 6f  ting the AggInfo
69180 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73   structure..*/.s
69190 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20 7b 0a  truct AggInfo {.
691a0 20 20 75 38 20 64 69 72 65 63 74 4d 6f 64 65 3b    u8 directMode;
691b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72            /* Dir
691c0 65 63 74 20 72 65 6e 64 65 72 69 6e 67 20 6d 6f  ect rendering mo
691d0 64 65 20 6d 65 61 6e 73 20 74 61 6b 65 20 64 61  de means take da
691e0 74 61 20 64 69 72 65 63 74 6c 79 0a 20 20 20 20  ta directly.    
691f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
69200 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 73 6f        ** from so
69210 75 72 63 65 20 74 61 62 6c 65 73 20 72 61 74 68  urce tables rath
69220 65 72 20 74 68 61 6e 20 66 72 6f 6d 20 61 63 63  er than from acc
69230 75 6d 75 6c 61 74 6f 72 73 20 2a 2f 0a 20 20 75  umulators */.  u
69240 38 20 75 73 65 53 6f 72 74 69 6e 67 49 64 78 3b  8 useSortingIdx;
69250 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 69 72         /* In dir
69260 65 63 74 20 6d 6f 64 65 2c 20 72 65 66 65 72 65  ect mode, refere
69270 6e 63 65 20 74 68 65 20 73 6f 72 74 69 6e 67 20  nce the sorting 
69280 69 6e 64 65 78 20 72 61 74 68 65 72 0a 20 20 20  index rather.   
69290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
692a0 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e 20 74         ** than t
692b0 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20  he source table 
692c0 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 69 6e 67  */.  int sorting
692d0 49 64 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Idx;         /* 
692e0 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66  Cursor number of
692f0 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64   the sorting ind
69300 65 78 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ex */.  ExprList
69310 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20 20   *pGroupBy;     
69320 2f 2a 20 54 68 65 20 67 72 6f 75 70 20 62 79 20  /* The group by 
69330 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20  clause */.  int 
69340 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 3b 20  nSortingColumn; 
69350 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
69360 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
69370 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f  sorting index */
69380 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66  .  struct AggInf
69390 6f 5f 63 6f 6c 20 7b 20 20 20 20 2f 2a 20 46 6f  o_col {    /* Fo
693a0 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 75 73  r each column us
693b0 65 64 20 69 6e 20 73 6f 75 72 63 65 20 74 61 62  ed in source tab
693c0 6c 65 73 20 2a 2f 0a 20 20 20 20 54 61 62 6c 65  les */.    Table
693d0 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20   *pTab;         
693e0 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 74 61      /* Source ta
693f0 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ble */.    int i
69400 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20  Table;          
69410 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75      /* Cursor nu
69420 6d 62 65 72 20 6f 66 20 74 68 65 20 73 6f 75 72  mber of the sour
69430 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  ce table */.    
69440 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20  int iColumn;    
69450 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75           /* Colu
69460 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 69 6e  mn number within
69470 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c   the source tabl
69480 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53 6f  e */.    int iSo
69490 72 74 65 72 43 6f 6c 75 6d 6e 3b 20 20 20 20 20  rterColumn;     
694a0 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62    /* Column numb
694b0 65 72 20 69 6e 20 74 68 65 20 73 6f 72 74 69 6e  er in the sortin
694c0 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69  g index */.    i
694d0 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20  nt iMem;        
694e0 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72          /* Memor
694f0 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 20  y location that 
69500 61 63 74 73 20 61 73 20 61 63 63 75 6d 75 6c 61  acts as accumula
69510 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70 72 20  tor */.    Expr 
69520 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20  *pExpr;         
69530 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69 67 69      /* The origi
69540 6e 61 6c 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  nal expression *
69550 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 0a 20 20 69  /.  } *aCol;.  i
69560 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20  nt nColumn;     
69570 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
69580 20 6f 66 20 75 73 65 64 20 65 6e 74 72 69 65 73   of used entries
69590 20 69 6e 20 61 43 6f 6c 5b 5d 20 2a 2f 0a 20 20   in aCol[] */.  
695a0 69 6e 74 20 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63  int nColumnAlloc
695b0 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ;       /* Numbe
695c0 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63  r of slots alloc
695d0 61 74 65 64 20 66 6f 72 20 61 43 6f 6c 5b 5d 20  ated for aCol[] 
695e0 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63 75 6d 75  */.  int nAccumu
695f0 6c 61 74 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20  lator;       /* 
69600 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  Number of column
69610 73 20 74 68 61 74 20 73 68 6f 77 20 74 68 72 6f  s that show thro
69620 75 67 68 20 74 6f 20 74 68 65 20 6f 75 74 70 75  ugh to the outpu
69630 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t..             
69640 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
69650 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d  Additional colum
69660 6e 73 20 61 72 65 20 75 73 65 64 20 6f 6e 6c 79  ns are used only
69670 20 61 73 20 70 61 72 61 6d 65 74 65 72 73 20 74   as parameters t
69680 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o.              
69690 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61              ** a
696a0 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
696b0 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41  ns */.  struct A
696c0 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b 20 20 20  ggInfo_func {   
696d0 2f 2a 20 46 6f 72 20 65 61 63 68 20 61 67 67 72  /* For each aggr
696e0 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a  egate function *
696f0 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70  /.    Expr *pExp
69700 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  r;             /
69710 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 65 6e 63  * Expression enc
69720 6f 64 69 6e 67 20 74 68 65 20 66 75 6e 63 74 69  oding the functi
69730 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65  on */.    FuncDe
69740 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20  f *pFunc;       
69750 20 20 20 2f 2a 20 54 68 65 20 61 67 67 72 65 67     /* The aggreg
69760 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  ate function imp
69770 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20  lementation */. 
69780 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20     int iMem;    
69790 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
697a0 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74  emory location t
697b0 68 61 74 20 61 63 74 73 20 61 73 20 61 63 63 75  hat acts as accu
697c0 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 69  mulator */.    i
697d0 6e 74 20 69 44 69 73 74 69 6e 63 74 3b 20 20 20  nt iDistinct;   
697e0 20 20 20 20 20 20 20 20 2f 2a 20 45 70 68 65 72          /* Epher
697f0 6d 65 72 61 6c 20 74 61 62 6c 65 20 75 73 65 64  meral table used
69800 20 74 6f 20 65 6e 66 6f 72 63 65 20 44 49 53 54   to enforce DIST
69810 49 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a 61 46 75  INCT */.  } *aFu
69820 6e 63 3b 0a 20 20 69 6e 74 20 6e 46 75 6e 63 3b  nc;.  int nFunc;
69830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
69840 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
69850 65 73 20 69 6e 20 61 46 75 6e 63 5b 5d 20 2a 2f  es in aFunc[] */
69860 0a 20 20 69 6e 74 20 6e 46 75 6e 63 41 6c 6c 6f  .  int nFuncAllo
69870 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  c;         /* Nu
69880 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c  mber of slots al
69890 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 46 75 6e  located for aFun
698a0 63 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  c[] */.};../*.**
698b0 20 45 61 63 68 20 6e 6f 64 65 20 6f 66 20 61 6e   Each node of an
698c0 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74   expression in t
698d0 68 65 20 70 61 72 73 65 20 74 72 65 65 20 69 73  he parse tree is
698e0 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20   an instance.** 
698f0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
69900 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 6f 70  e..**.** Expr.op
69910 20 69 73 20 74 68 65 20 6f 70 63 6f 64 65 2e 20   is the opcode. 
69920 20 54 68 65 20 69 6e 74 65 67 65 72 20 70 61 72   The integer par
69930 73 65 72 20 74 6f 6b 65 6e 20 63 6f 64 65 73 20  ser token codes 
69940 61 72 65 20 72 65 75 73 65 64 0a 2a 2a 20 61 73  are reused.** as
69950 20 6f 70 63 6f 64 65 73 20 68 65 72 65 2e 20 20   opcodes here.  
69960 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65  For example, the
69970 20 70 61 72 73 65 72 20 64 65 66 69 6e 65 73 20   parser defines 
69980 54 4b 5f 47 45 20 74 6f 20 62 65 20 61 6e 20 69  TK_GE to be an i
69990 6e 74 65 67 65 72 0a 2a 2a 20 63 6f 64 65 20 72  nteger.** code r
699a0 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20  epresenting the 
699b0 22 3e 3d 22 20 6f 70 65 72 61 74 6f 72 2e 20 20  ">=" operator.  
699c0 54 68 69 73 20 73 61 6d 65 20 69 6e 74 65 67 65  This same intege
699d0 72 20 63 6f 64 65 20 69 73 20 72 65 75 73 65 64  r code is reused
699e0 0a 2a 2a 20 74 6f 20 72 65 70 72 65 73 65 6e 74  .** to represent
699f0 20 74 68 65 20 67 72 65 61 74 65 72 2d 74 68 61   the greater-tha
69a00 6e 2d 6f 72 2d 65 71 75 61 6c 2d 74 6f 20 6f 70  n-or-equal-to op
69a10 65 72 61 74 6f 72 20 69 6e 20 74 68 65 20 65 78  erator in the ex
69a20 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65  pression.** tree
69a30 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 70 52 69  ..**.** Expr.pRi
69a40 67 68 74 20 61 6e 64 20 45 78 70 72 2e 70 4c 65  ght and Expr.pLe
69a50 66 74 20 61 72 65 20 73 75 62 65 78 70 72 65 73  ft are subexpres
69a60 73 69 6f 6e 73 2e 20 20 45 78 70 72 2e 70 4c 69  sions.  Expr.pLi
69a70 73 74 20 69 73 20 61 20 6c 69 73 74 0a 2a 2a 20  st is a list.** 
69a80 6f 66 20 61 72 67 75 6d 65 6e 74 20 69 66 20 74  of argument if t
69a90 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
69aa0 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a   a function..**.
69ab0 2a 2a 20 45 78 70 72 2e 74 6f 6b 65 6e 20 69 73  ** Expr.token is
69ac0 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74 6f   the operator to
69ad0 6b 65 6e 20 66 6f 72 20 74 68 69 73 20 6e 6f 64  ken for this nod
69ae0 65 2e 20 20 46 6f 72 20 73 6f 6d 65 20 65 78 70  e.  For some exp
69af0 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 74 68 61 74  ressions.** that
69b00 20 68 61 76 65 20 73 75 62 65 78 70 72 65 73 73   have subexpress
69b10 69 6f 6e 73 2c 20 45 78 70 72 2e 74 6f 6b 65 6e  ions, Expr.token
69b20 20 63 61 6e 20 62 65 20 74 68 65 20 63 6f 6d 70   can be the comp
69b30 6c 65 74 65 20 74 65 78 74 20 74 68 61 74 20 67  lete text that g
69b40 61 76 65 0a 2a 2a 20 72 69 73 65 20 74 6f 20 74  ave.** rise to t
69b50 68 65 20 45 78 70 72 2e 20 20 49 6e 20 74 68 65  he Expr.  In the
69b60 20 6c 61 74 74 65 72 20 63 61 73 65 2c 20 74 68   latter case, th
69b70 65 20 74 6f 6b 65 6e 20 69 73 20 6d 61 72 6b 65  e token is marke
69b80 64 20 61 73 20 62 65 69 6e 67 0a 2a 2a 20 61 20  d as being.** a 
69b90 63 6f 6d 70 6f 75 6e 64 20 74 6f 6b 65 6e 2e 0a  compound token..
69ba0 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 72 65 73 73  **.** An express
69bb0 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ion of the form 
69bc0 49 44 20 6f 72 20 49 44 2e 49 44 20 72 65 66 65  ID or ID.ID refe
69bd0 72 73 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 69  rs to a column i
69be0 6e 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20 46 6f  n a table..** Fo
69bf0 72 20 73 75 63 68 20 65 78 70 72 65 73 73 69 6f  r such expressio
69c00 6e 73 2c 20 45 78 70 72 2e 6f 70 20 69 73 20 73  ns, Expr.op is s
69c10 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20  et to TK_COLUMN 
69c20 61 6e 64 20 45 78 70 72 2e 69 54 61 62 6c 65 20  and Expr.iTable 
69c30 69 73 0a 2a 2a 20 74 68 65 20 69 6e 74 65 67 65  is.** the intege
69c40 72 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  r cursor number 
69c50 6f 66 20 61 20 56 44 42 45 20 63 75 72 73 6f 72  of a VDBE cursor
69c60 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 61   pointing to tha
69c70 74 20 74 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 45  t table and.** E
69c80 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 69 73 20 74  xpr.iColumn is t
69c90 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  he column number
69ca0 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69   for the specifi
69cb0 63 20 63 6f 6c 75 6d 6e 2e 20 20 49 66 20 74 68  c column.  If th
69cc0 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20  e.** expression 
69cd0 69 73 20 75 73 65 64 20 61 73 20 61 20 72 65 73  is used as a res
69ce0 75 6c 74 20 69 6e 20 61 6e 20 61 67 67 72 65 67  ult in an aggreg
69cf0 61 74 65 20 53 45 4c 45 43 54 2c 20 74 68 65 6e  ate SELECT, then
69d00 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 69 73   the.** value is
69d10 20 61 6c 73 6f 20 73 74 6f 72 65 64 20 69 6e 20   also stored in 
69d20 74 68 65 20 45 78 70 72 2e 69 41 67 67 20 63 6f  the Expr.iAgg co
69d30 6c 75 6d 6e 20 69 6e 20 74 68 65 20 61 67 67 72  lumn in the aggr
69d40 65 67 61 74 65 20 73 6f 20 74 68 61 74 0a 2a 2a  egate so that.**
69d50 20 69 74 20 63 61 6e 20 62 65 20 61 63 63 65 73   it can be acces
69d60 73 65 64 20 61 66 74 65 72 20 61 6c 6c 20 61 67  sed after all ag
69d70 67 72 65 67 61 74 65 73 20 61 72 65 20 63 6f 6d  gregates are com
69d80 70 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  puted..**.** If 
69d90 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
69da0 73 20 61 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68  s a function, th
69db0 65 20 45 78 70 72 2e 69 54 61 62 6c 65 20 69 73  e Expr.iTable is
69dc0 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 64 65   an integer code
69dd0 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 69 6e 67  .** representing
69de0 20 77 68 69 63 68 20 66 75 6e 63 74 69 6f 6e 2e   which function.
69df0 20 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73    If the express
69e00 69 6f 6e 20 69 73 20 61 6e 20 75 6e 62 6f 75 6e  ion is an unboun
69e10 64 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61  d variable.** ma
69e20 72 6b 65 72 20 28 61 20 71 75 65 73 74 69 6f 6e  rker (a question
69e30 20 6d 61 72 6b 20 63 68 61 72 61 63 74 65 72 20   mark character 
69e40 27 3f 27 20 69 6e 20 74 68 65 20 6f 72 69 67 69  '?' in the origi
69e50 6e 61 6c 20 53 51 4c 29 20 74 68 65 6e 20 74 68  nal SQL) then th
69e60 65 0a 2a 2a 20 45 78 70 72 2e 69 54 61 62 6c 65  e.** Expr.iTable
69e70 20 68 6f 6c 64 73 20 74 68 65 20 69 6e 64 65 78   holds the index
69e80 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74   number for that
69e90 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a   variable..**.**
69ea0 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69   If the expressi
69eb0 6f 6e 20 69 73 20 61 20 73 75 62 71 75 65 72 79  on is a subquery
69ec0 20 74 68 65 6e 20 45 78 70 72 2e 69 43 6f 6c 75   then Expr.iColu
69ed0 6d 6e 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65  mn holds an inte
69ee0 67 65 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20  ger.** register 
69ef0 6e 75 6d 62 65 72 20 63 6f 6e 74 61 69 6e 69 6e  number containin
69f00 67 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  g the result of 
69f10 74 68 65 20 73 75 62 71 75 65 72 79 2e 20 20 49  the subquery.  I
69f20 66 20 74 68 65 0a 2a 2a 20 73 75 62 71 75 65 72  f the.** subquer
69f30 79 20 67 69 76 65 73 20 61 20 63 6f 6e 73 74 61  y gives a consta
69f40 6e 74 20 72 65 73 75 6c 74 2c 20 74 68 65 6e 20  nt result, then 
69f50 69 54 61 62 6c 65 20 69 73 20 2d 31 2e 20 20 49  iTable is -1.  I
69f60 66 20 74 68 65 20 73 75 62 71 75 65 72 79 0a 2a  f the subquery.*
69f70 2a 20 67 69 76 65 73 20 61 20 64 69 66 66 65 72  * gives a differ
69f80 65 6e 74 20 61 6e 73 77 65 72 20 61 74 20 64 69  ent answer at di
69f90 66 66 65 72 65 6e 74 20 74 69 6d 65 73 20 64 75  fferent times du
69fa0 72 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 70  ring statement p
69fb0 72 6f 63 65 73 73 69 6e 67 0a 2a 2a 20 74 68 65  rocessing.** the
69fc0 6e 20 69 54 61 62 6c 65 20 69 73 20 74 68 65 20  n iTable is the 
69fd0 61 64 64 72 65 73 73 20 6f 66 20 61 20 73 75 62  address of a sub
69fe0 72 6f 75 74 69 6e 65 20 74 68 61 74 20 63 6f 6d  routine that com
69ff0 70 75 74 65 73 20 74 68 65 20 73 75 62 71 75 65  putes the subque
6a000 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 45 78  ry..**.** The Ex
6a010 70 72 2e 70 53 65 6c 65 63 74 20 66 69 65 6c 64  pr.pSelect field
6a020 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 53 45 4c   points to a SEL
6a030 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ECT statement.  
6a040 54 68 65 20 53 45 4c 45 43 54 20 6d 69 67 68 74  The SELECT might
6a050 0a 2a 2a 20 62 65 20 74 68 65 20 72 69 67 68 74  .** be the right
6a060 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 6e 20 49   operand of an I
6a070 4e 20 6f 70 65 72 61 74 6f 72 2e 20 20 4f 72 2c  N operator.  Or,
6a080 20 69 66 20 61 20 73 63 61 6c 61 72 20 53 45 4c   if a scalar SEL
6a090 45 43 54 20 61 70 70 65 61 72 73 0a 2a 2a 20 69  ECT appears.** i
6a0a0 6e 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  n an expression 
6a0b0 74 68 65 20 6f 70 63 6f 64 65 20 69 73 20 54 4b  the opcode is TK
6a0c0 5f 53 45 4c 45 43 54 20 61 6e 64 20 45 78 70 72  _SELECT and Expr
6a0d0 2e 70 53 65 6c 65 63 74 20 69 73 20 74 68 65 20  .pSelect is the 
6a0e0 6f 6e 6c 79 0a 2a 2a 20 6f 70 65 72 61 6e 64 2e  only.** operand.
6a0f0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 78  .**.** If the Ex
6a100 70 72 20 69 73 20 6f 66 20 74 79 70 65 20 4f 50  pr is of type OP
6a110 5f 43 6f 6c 75 6d 6e 2c 20 61 6e 64 20 74 68 65  _Column, and the
6a120 20 74 61 62 6c 65 20 69 74 20 69 73 20 73 65 6c   table it is sel
6a130 65 63 74 69 6e 67 20 66 72 6f 6d 0a 2a 2a 20 69  ecting from.** i
6a140 73 20 61 20 64 69 73 6b 20 74 61 62 6c 65 20 6f  s a disk table o
6a150 72 20 74 68 65 20 22 6f 6c 64 2e 2a 22 20 70 73  r the "old.*" ps
6a160 65 75 64 6f 2d 74 61 62 6c 65 2c 20 74 68 65 6e  eudo-table, then
6a170 20 70 54 61 62 20 70 6f 69 6e 74 73 20 74 6f 20   pTab points to 
6a180 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e  the.** correspon
6a190 64 69 6e 67 20 74 61 62 6c 65 20 64 65 66 69 6e  ding table defin
6a1a0 69 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74  ition..*/.struct
6a1b0 20 45 78 70 72 20 7b 0a 20 20 75 38 20 6f 70 3b   Expr {.  u8 op;
6a1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6a1d0 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 70 65   /* Operation pe
6a1e0 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69 73 20  rformed by this 
6a1f0 6e 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 61  node */.  char a
6a200 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20 20  ffinity;        
6a210 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79   /* The affinity
6a220 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f   of the column o
6a230 72 20 30 20 69 66 20 6e 6f 74 20 61 20 63 6f 6c  r 0 if not a col
6a240 75 6d 6e 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61  umn */.  u16 fla
6a250 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  gs;             
6a260 2f 2a 20 56 61 72 69 6f 75 73 20 66 6c 61 67 73  /* Various flags
6a270 2e 20 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0a  .  See below */.
6a280 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
6a290 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
6a2a0 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 6f  collation type o
6a2b0 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20  f the column or 
6a2c0 30 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65  0 */.  Expr *pLe
6a2d0 66 74 2c 20 2a 70 52 69 67 68 74 3b 20 20 2f 2a  ft, *pRight;  /*
6a2e0 20 4c 65 66 74 20 61 6e 64 20 72 69 67 68 74 20   Left and right 
6a2f0 73 75 62 6e 6f 64 65 73 20 2a 2f 0a 20 20 45 78  subnodes */.  Ex
6a300 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 20 20  prList *pList;  
6a310 20 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f       /* A list o
6a320 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 75 73  f expressions us
6a330 65 64 20 61 73 20 66 75 6e 63 74 69 6f 6e 20 61  ed as function a
6a340 72 67 75 6d 65 6e 74 73 0a 20 20 20 20 20 20 20  rguments.       
6a350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6a360 20 20 2a 2a 20 6f 72 20 69 6e 20 22 3c 65 78 70    ** or in "<exp
6a370 72 3e 20 49 4e 20 28 3c 65 78 70 72 2d 6c 69 73  r> IN (<expr-lis
6a380 74 29 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 74  t)" */.  Token t
6a390 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20  oken;           
6a3a0 2f 2a 20 41 6e 20 6f 70 65 72 61 6e 64 20 74 6f  /* An operand to
6a3b0 6b 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73  ken */.  Token s
6a3c0 70 61 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  pan;            
6a3d0 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74  /* Complete text
6a3e0 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
6a3f0 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62  on */.  int iTab
6a400 6c 65 2c 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 2f  le, iColumn;   /
6a410 2a 20 57 68 65 6e 20 6f 70 3d 3d 54 4b 5f 43 4f  * When op==TK_CO
6a420 4c 55 4d 4e 2c 20 74 68 65 6e 20 74 68 69 73 20  LUMN, then this 
6a430 65 78 70 72 20 6e 6f 64 65 20 6d 65 61 6e 73 20  expr node means 
6a440 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 20 20  the.            
6a450 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
6a460 69 43 6f 6c 75 6d 6e 2d 74 68 20 66 69 65 6c 64  iColumn-th field
6a470 20 6f 66 20 74 68 65 20 69 54 61 62 6c 65 2d 74   of the iTable-t
6a480 68 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 41 67  h table. */.  Ag
6a490 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 3b  gInfo *pAggInfo;
6a4a0 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20       /* Used by 
6a4b0 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e  TK_AGG_COLUMN an
6a4c0 64 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f  d TK_AGG_FUNCTIO
6a4d0 4e 20 2a 2f 0a 20 20 69 6e 74 20 69 41 67 67 3b  N */.  int iAgg;
6a4e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6a4f0 20 57 68 69 63 68 20 65 6e 74 72 79 20 69 6e 20   Which entry in 
6a500 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d  pAggInfo->aCol[]
6a510 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d 20 2a 2f   or ->aFunc[] */
6a520 0a 20 20 69 6e 74 20 69 52 69 67 68 74 4a 6f 69  .  int iRightJoi
6a530 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20 49 66 20  nTable;   /* If 
6a540 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20 74 68 65  EP_FromJoin, the
6a550 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20   right table of 
6a560 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 53 65  the join */.  Se
6a570 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20  lect *pSelect;  
6a580 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65       /* When the
6a590 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61   expression is a
6a5a0 20 73 75 62 2d 73 65 6c 65 63 74 2e 20 20 41 6c   sub-select.  Al
6a5b0 73 6f 20 74 68 65 0a 20 20 20 20 20 20 20 20 20  so the.         
6a5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6a5d0 2a 2a 20 72 69 67 68 74 20 73 69 64 65 20 6f 66  ** right side of
6a5e0 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 3c 73 65   "<expr> IN (<se
6a5f0 6c 65 63 74 3e 29 22 20 2a 2f 0a 20 20 54 61 62  lect>)" */.  Tab
6a600 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20  le *pTab;       
6a610 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 66 6f 72      /* Table for
6a620 20 54 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70 72 65   TK_COLUMN expre
6a630 73 73 69 6f 6e 73 2e 20 2a 2f 0a 23 69 66 20 53  ssions. */.#if S
6a640 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44  QLITE_MAX_EXPR_D
6a650 45 50 54 48 3e 30 0a 20 20 69 6e 74 20 6e 48 65  EPTH>0.  int nHe
6a660 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20  ight;           
6a670 2f 2a 20 48 65 69 67 68 74 20 6f 66 20 74 68 65  /* Height of the
6a680 20 74 72 65 65 20 68 65 61 64 65 64 20 62 79 20   tree headed by 
6a690 74 68 69 73 20 6e 6f 64 65 20 2a 2f 0a 23 65 6e  this node */.#en
6a6a0 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68  dif.};../*.** Th
6a6b0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20  e following are 
6a6c0 74 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20  the meanings of 
6a6d0 62 69 74 73 20 69 6e 20 74 68 65 20 45 78 70 72  bits in the Expr
6a6e0 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f  .flags field..*/
6a6f0 0a 23 64 65 66 69 6e 65 20 45 50 5f 46 72 6f 6d  .#define EP_From
6a700 4a 6f 69 6e 20 20 20 30 78 30 30 30 31 20 20 2f  Join   0x0001  /
6a710 2a 20 4f 72 69 67 69 6e 61 74 65 64 20 69 6e 20  * Originated in 
6a720 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75  ON or USING clau
6a730 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a  se of a join */.
6a740 23 64 65 66 69 6e 65 20 45 50 5f 41 67 67 20 20  #define EP_Agg  
6a750 20 20 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a        0x0002  /*
6a760 20 43 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72   Contains one or
6a770 20 6d 6f 72 65 20 61 67 67 72 65 67 61 74 65 20   more aggregate 
6a780 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 64 65  functions */.#de
6a790 66 69 6e 65 20 45 50 5f 52 65 73 6f 6c 76 65 64  fine EP_Resolved
6a7a0 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 49 44     0x0004  /* ID
6a7b0 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f  s have been reso
6a7c0 6c 76 65 64 20 74 6f 20 43 4f 4c 55 4d 4e 73 20  lved to COLUMNs 
6a7d0 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 45 72  */.#define EP_Er
6a7e0 72 6f 72 20 20 20 20 20 20 30 78 30 30 30 38 20  ror      0x0008 
6a7f0 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 63   /* Expression c
6a800 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d  ontains one or m
6a810 6f 72 65 20 65 72 72 6f 72 73 20 2a 2f 0a 23 64  ore errors */.#d
6a820 65 66 69 6e 65 20 45 50 5f 44 69 73 74 69 6e 63  efine EP_Distinc
6a830 74 20 20 20 30 78 30 30 31 30 20 20 2f 2a 20 41  t   0x0010  /* A
6a840 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
6a850 6e 20 77 69 74 68 20 44 49 53 54 49 4e 43 54 20  n with DISTINCT 
6a860 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65 66 69  keyword */.#defi
6a870 6e 65 20 45 50 5f 56 61 72 53 65 6c 65 63 74 20  ne EP_VarSelect 
6a880 20 30 78 30 30 32 30 20 20 2f 2a 20 70 53 65 6c   0x0020  /* pSel
6a890 65 63 74 20 69 73 20 63 6f 72 72 65 6c 61 74 65  ect is correlate
6a8a0 64 2c 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 20  d, not constant 
6a8b0 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 44 65  */.#define EP_De
6a8c0 71 75 6f 74 65 64 20 20 20 30 78 30 30 34 30 20  quoted   0x0040 
6a8d0 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
6a8e0 73 74 72 69 6e 67 20 68 61 73 20 62 65 65 6e 20  string has been 
6a8f0 64 65 71 75 6f 74 65 64 20 2a 2f 0a 23 64 65 66  dequoted */.#def
6a900 69 6e 65 20 45 50 5f 49 6e 66 69 78 46 75 6e 63  ine EP_InfixFunc
6a910 20 20 30 78 30 30 38 30 20 20 2f 2a 20 54 72 75    0x0080  /* Tru
6a920 65 20 66 6f 72 20 61 6e 20 69 6e 66 69 78 20 66  e for an infix f
6a930 75 6e 63 74 69 6f 6e 3a 20 4c 49 4b 45 2c 20 47  unction: LIKE, G
6a940 4c 4f 42 2c 20 65 74 63 20 2a 2f 0a 23 64 65 66  LOB, etc */.#def
6a950 69 6e 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74  ine EP_ExpCollat
6a960 65 20 30 78 30 31 30 30 20 20 2f 2a 20 43 6f 6c  e 0x0100  /* Col
6a970 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
6a980 73 70 65 63 69 66 69 65 64 20 65 78 70 6c 69 63  specified explic
6a990 69 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  itly */.#define 
6a9a0 45 50 5f 41 6e 79 41 66 66 20 20 20 20 20 30 78  EP_AnyAff     0x
6a9b0 30 32 30 30 20 20 2f 2a 20 43 61 6e 20 74 61 6b  0200  /* Can tak
6a9c0 65 20 61 20 63 61 63 68 65 64 20 63 6f 6c 75 6d  e a cached colum
6a9d0 6e 20 6f 66 20 61 6e 79 20 61 66 66 69 6e 69 74  n of any affinit
6a9e0 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f  y */.#define EP_
6a9f0 46 69 78 65 64 44 65 73 74 20 20 30 78 30 34 30  FixedDest  0x040
6aa00 30 20 20 2f 2a 20 52 65 73 75 6c 74 20 6e 65 65  0  /* Result nee
6aa10 64 65 64 20 69 6e 20 61 20 73 70 65 63 69 66 69  ded in a specifi
6aa20 63 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 23 64  c register */.#d
6aa30 65 66 69 6e 65 20 45 50 5f 49 6e 74 56 61 6c 75  efine EP_IntValu
6aa40 65 20 20 20 30 78 30 38 30 30 20 20 2f 2a 20 49  e   0x0800  /* I
6aa50 6e 74 65 67 65 72 20 76 61 6c 75 65 20 63 6f 6e  nteger value con
6aa60 74 61 69 6e 65 64 20 69 6e 20 69 54 61 62 6c 65  tained in iTable
6aa70 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20   */./*.** These 
6aa80 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73  macros can be us
6aa90 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65 74 2c  ed to test, set,
6aaa0 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73 20 69   or clear bits i
6aab0 6e 20 74 68 65 20 0a 2a 2a 20 45 78 70 72 2e 66  n the .** Expr.f
6aac0 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23  lags field..*/.#
6aad0 64 65 66 69 6e 65 20 45 78 70 72 48 61 73 50 72  define ExprHasPr
6aae0 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 20  operty(E,P)     
6aaf0 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 28 50 29  (((E)->flags&(P)
6ab00 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 20  )==(P)).#define 
6ab10 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72  ExprHasAnyProper
6ab20 74 79 28 45 2c 50 29 20 20 28 28 28 45 29 2d 3e  ty(E,P)  (((E)->
6ab30 66 6c 61 67 73 26 28 50 29 29 21 3d 30 29 0a 23  flags&(P))!=0).#
6ab40 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 50 72  define ExprSetPr
6ab50 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 20  operty(E,P)     
6ab60 28 45 29 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a  (E)->flags|=(P).
6ab70 23 64 65 66 69 6e 65 20 45 78 70 72 43 6c 65 61  #define ExprClea
6ab80 72 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20  rProperty(E,P)  
6ab90 20 28 45 29 2d 3e 66 6c 61 67 73 26 3d 7e 28 50   (E)->flags&=~(P
6aba0 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 20  )../*.** A list 
6abb0 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20  of expressions. 
6abc0 20 45 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e   Each expression
6abd0 20 6d 61 79 20 6f 70 74 69 6f 6e 61 6c 6c 79 20   may optionally 
6abe0 68 61 76 65 20 61 0a 2a 2a 20 6e 61 6d 65 2e 20  have a.** name. 
6abf0 20 41 6e 20 65 78 70 72 2f 6e 61 6d 65 20 63 6f   An expr/name co
6ac00 6d 62 69 6e 61 74 69 6f 6e 20 63 61 6e 20 62 65  mbination can be
6ac10 20 75 73 65 64 20 69 6e 20 73 65 76 65 72 61 6c   used in several
6ac20 20 77 61 79 73 2c 20 73 75 63 68 0a 2a 2a 20 61   ways, such.** a
6ac30 73 20 74 68 65 20 6c 69 73 74 20 6f 66 20 22 65  s the list of "e
6ac40 78 70 72 20 41 53 20 49 44 22 20 66 69 65 6c 64  xpr AS ID" field
6ac50 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 22 53  s following a "S
6ac60 45 4c 45 43 54 22 20 6f 72 20 69 6e 20 74 68 65  ELECT" or in the
6ac70 0a 2a 2a 20 6c 69 73 74 20 6f 66 20 22 49 44 20  .** list of "ID 
6ac80 3d 20 65 78 70 72 22 20 69 74 65 6d 73 20 69 6e  = expr" items in
6ac90 20 61 6e 20 55 50 44 41 54 45 2e 20 20 41 20 6c   an UPDATE.  A l
6aca0 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f  ist of expressio
6acb0 6e 73 20 63 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62  ns can.** also b
6acc0 65 20 75 73 65 64 20 61 73 20 74 68 65 20 61 72  e used as the ar
6acd0 67 75 6d 65 6e 74 20 74 6f 20 61 20 66 75 6e 63  gument to a func
6ace0 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 63  tion, in which c
6acf0 61 73 65 20 74 68 65 20 61 2e 7a 4e 61 6d 65 0a  ase the a.zName.
6ad00 2a 2a 20 66 69 65 6c 64 20 69 73 20 6e 6f 74 20  ** field is not 
6ad10 75 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  used..*/.struct 
6ad20 45 78 70 72 4c 69 73 74 20 7b 0a 20 20 69 6e 74  ExprList {.  int
6ad30 20 6e 45 78 70 72 3b 20 20 20 20 20 20 20 20 20   nExpr;         
6ad40 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
6ad50 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e 20   expressions on 
6ad60 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e  the list */.  in
6ad70 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20  t nAlloc;       
6ad80 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
6ad90 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61  f entries alloca
6ada0 74 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69  ted below */.  i
6adb0 6e 74 20 69 45 43 75 72 73 6f 72 3b 20 20 20 20  nt iECursor;    
6adc0 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75        /* VDBE Cu
6add0 72 73 6f 72 20 61 73 73 6f 63 69 61 74 65 64 20  rsor associated 
6ade0 77 69 74 68 20 74 68 69 73 20 45 78 70 72 4c 69  with this ExprLi
6adf0 73 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45  st */.  struct E
6ae00 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20  xprList_item {. 
6ae10 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20     Expr *pExpr; 
6ae20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
6ae30 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73   list of express
6ae40 69 6f 6e 73 20 2a 2f 0a 20 20 20 20 63 68 61 72  ions */.    char
6ae50 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20   *zName;        
6ae60 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 73 73 6f     /* Token asso
6ae70 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73  ciated with this
6ae80 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20   expression */. 
6ae90 20 20 20 75 38 20 73 6f 72 74 4f 72 64 65 72 3b     u8 sortOrder;
6aea0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66            /* 1 f
6aeb0 6f 72 20 44 45 53 43 20 6f 72 20 30 20 66 6f 72  or DESC or 0 for
6aec0 20 41 53 43 20 2a 2f 0a 20 20 20 20 75 38 20 64   ASC */.    u8 d
6aed0 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  one;            
6aee0 20 20 20 2f 2a 20 41 20 66 6c 61 67 20 74 6f 20     /* A flag to 
6aef0 69 6e 64 69 63 61 74 65 20 77 68 65 6e 20 70 72  indicate when pr
6af00 6f 63 65 73 73 69 6e 67 20 69 73 20 66 69 6e 69  ocessing is fini
6af10 73 68 65 64 20 2a 2f 0a 20 20 20 20 75 31 36 20  shed */.    u16 
6af20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  iCol;           
6af30 20 20 20 2f 2a 20 46 6f 72 20 4f 52 44 45 52 20     /* For ORDER 
6af40 42 59 2c 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65  BY, column numbe
6af50 72 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74 20  r in result set 
6af60 2a 2f 0a 20 20 20 20 75 31 36 20 69 41 6c 69 61  */.    u16 iAlia
6af70 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  s;            /*
6af80 20 49 6e 64 65 78 20 69 6e 74 6f 20 50 61 72 73   Index into Pars
6af90 65 2e 61 41 6c 69 61 73 5b 5d 20 66 6f 72 20 7a  e.aAlias[] for z
6afa0 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 20  Name */.  } *a; 
6afb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6afc0 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f   /* One entry fo
6afd0 72 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f  r each expressio
6afe0 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  n */.};../*.** A
6aff0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
6b000 69 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e  is structure can
6b010 20 68 6f 6c 64 20 61 20 73 69 6d 70 6c 65 20 6c   hold a simple l
6b020 69 73 74 20 6f 66 20 69 64 65 6e 74 69 66 69 65  ist of identifie
6b030 72 73 2c 0a 2a 2a 20 73 75 63 68 20 61 73 20 74  rs,.** such as t
6b040 68 65 20 6c 69 73 74 20 22 61 2c 62 2c 63 22 20  he list "a,b,c" 
6b050 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  in the following
6b060 20 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a   statements:.**.
6b070 2a 2a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49  **      INSERT I
6b080 4e 54 4f 20 74 28 61 2c 62 2c 63 29 20 56 41 4c  NTO t(a,b,c) VAL
6b090 55 45 53 20 2e 2e 2e 3b 0a 2a 2a 20 20 20 20 20  UES ...;.**     
6b0a0 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 64   CREATE INDEX id
6b0b0 78 20 4f 4e 20 74 28 61 2c 62 2c 63 29 3b 0a 2a  x ON t(a,b,c);.*
6b0c0 2a 20 20 20 20 20 20 43 52 45 41 54 45 20 54 52  *      CREATE TR
6b0d0 49 47 47 45 52 20 74 72 69 67 20 42 45 46 4f 52  IGGER trig BEFOR
6b0e0 45 20 55 50 44 41 54 45 20 4f 4e 20 74 28 61 2c  E UPDATE ON t(a,
6b0f0 62 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20  b,c) ...;.**.** 
6b100 54 68 65 20 49 64 4c 69 73 74 2e 61 2e 69 64 78  The IdList.a.idx
6b110 20 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 77   field is used w
6b120 68 65 6e 20 74 68 65 20 49 64 4c 69 73 74 20 72  hen the IdList r
6b130 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 6c 69  epresents the li
6b140 73 74 20 6f 66 0a 2a 2a 20 63 6f 6c 75 6d 6e 20  st of.** column 
6b150 6e 61 6d 65 73 20 61 66 74 65 72 20 61 20 74 61  names after a ta
6b160 62 6c 65 20 6e 61 6d 65 20 69 6e 20 61 6e 20 49  ble name in an I
6b170 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e  NSERT statement.
6b180 20 20 49 6e 20 74 68 65 20 73 74 61 74 65 6d 65    In the stateme
6b190 6e 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 4e 53  nt.**.**     INS
6b1a0 45 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63  ERT INTO t(a,b,c
6b1b0 29 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 22  ) ....**.** If "
6b1c0 61 22 20 69 73 20 74 68 65 20 6b 2d 74 68 20 63  a" is the k-th c
6b1d0 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 22  olumn of table "
6b1e0 74 22 2c 20 74 68 65 6e 20 49 64 4c 69 73 74 2e  t", then IdList.
6b1f0 61 5b 30 5d 2e 69 64 78 3d 3d 6b 2e 0a 2a 2f 0a  a[0].idx==k..*/.
6b200 73 74 72 75 63 74 20 49 64 4c 69 73 74 20 7b 0a  struct IdList {.
6b210 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f    struct IdList_
6b220 69 74 65 6d 20 7b 0a 20 20 20 20 63 68 61 72 20  item {.    char 
6b230 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20  *zName;      /* 
6b240 4e 61 6d 65 20 6f 66 20 74 68 65 20 69 64 65 6e  Name of the iden
6b250 74 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 6e  tifier */.    in
6b260 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20  t idx;          
6b270 2f 2a 20 49 6e 64 65 78 20 69 6e 20 73 6f 6d 65  /* Index in some
6b280 20 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d 20 6f 66   Table.aCol[] of
6b290 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20   a column named 
6b2a0 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b  zName */.  } *a;
6b2b0 0a 20 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20  .  int nId;     
6b2c0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
6b2d0 20 69 64 65 6e 74 69 66 69 65 72 73 20 6f 6e 20   identifiers on 
6b2e0 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e  the list */.  in
6b2f0 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 2f  t nAlloc;      /
6b300 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
6b310 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f  ies allocated fo
6b320 72 20 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f 0a 7d  r a[] below */.}
6b330 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 69 74  ;../*.** The bit
6b340 6d 61 73 6b 20 64 61 74 61 74 79 70 65 20 64 65  mask datatype de
6b350 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 73 20 75  fined below is u
6b360 73 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73 20  sed for various 
6b370 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2e 0a 2a  optimizations..*
6b380 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68  *.** Changing th
6b390 69 73 20 66 72 6f 6d 20 61 20 36 34 2d 62 69 74  is from a 64-bit
6b3a0 20 74 6f 20 61 20 33 32 2d 62 69 74 20 74 79 70   to a 32-bit typ
6b3b0 65 20 6c 69 6d 69 74 73 20 74 68 65 20 6e 75 6d  e limits the num
6b3c0 62 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 73  ber of.** tables
6b3d0 20 69 6e 20 61 20 6a 6f 69 6e 20 74 6f 20 33 32   in a join to 32
6b3e0 20 69 6e 73 74 65 61 64 20 6f 66 20 36 34 2e 20   instead of 64. 
6b3f0 20 42 75 74 20 69 74 20 61 6c 73 6f 20 72 65 64   But it also red
6b400 75 63 65 73 20 74 68 65 20 73 69 7a 65 0a 2a 2a  uces the size.**
6b410 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 20   of the library 
6b420 62 79 20 37 33 38 20 62 79 74 65 73 20 6f 6e 20  by 738 bytes on 
6b430 69 78 38 36 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ix86..*/.typedef
6b440 20 75 36 34 20 42 69 74 6d 61 73 6b 3b 0a 0a 2f   u64 Bitmask;../
6b450 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
6b460 6e 67 20 73 74 72 75 63 74 75 72 65 20 64 65 73  ng structure des
6b470 63 72 69 62 65 73 20 74 68 65 20 46 52 4f 4d 20  cribes the FROM 
6b480 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45  clause of a SELE
6b490 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  CT statement..**
6b4a0 20 45 61 63 68 20 74 61 62 6c 65 20 6f 72 20 73   Each table or s
6b4b0 75 62 71 75 65 72 79 20 69 6e 20 74 68 65 20 46  ubquery in the F
6b4c0 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 61 20  ROM clause is a 
6b4d0 73 65 70 61 72 61 74 65 20 65 6c 65 6d 65 6e 74  separate element
6b4e0 20 6f 66 0a 2a 2a 20 74 68 65 20 53 72 63 4c 69   of.** the SrcLi
6b4f0 73 74 2e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a  st.a[] array..**
6b500 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 61 64 64  .** With the add
6b510 69 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 69 70 6c  ition of multipl
6b520 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f  e database suppo
6b530 72 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  rt, the followin
6b540 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 63  g structure.** c
6b550 61 6e 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20  an also be used 
6b560 74 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61  to describe a pa
6b570 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 20 73  rticular table s
6b580 75 63 68 20 61 73 20 74 68 65 20 74 61 62 6c 65  uch as the table
6b590 20 74 68 61 74 0a 2a 2a 20 69 73 20 6d 6f 64 69   that.** is modi
6b5a0 66 69 65 64 20 62 79 20 61 6e 20 49 4e 53 45 52  fied by an INSER
6b5b0 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50  T, DELETE, or UP
6b5c0 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20  DATE statement. 
6b5d0 20 49 6e 20 73 74 61 6e 64 61 72 64 20 53 51 4c   In standard SQL
6b5e0 2c 0a 2a 2a 20 73 75 63 68 20 61 20 74 61 62 6c  ,.** such a tabl
6b5f0 65 20 6d 75 73 74 20 62 65 20 61 20 73 69 6d 70  e must be a simp
6b600 6c 65 20 6e 61 6d 65 3a 20 49 44 2e 20 20 42 75  le name: ID.  Bu
6b610 74 20 69 6e 20 53 51 4c 69 74 65 2c 20 74 68 65  t in SQLite, the
6b620 20 74 61 62 6c 65 20 63 61 6e 0a 2a 2a 20 6e 6f   table can.** no
6b630 77 20 62 65 20 69 64 65 6e 74 69 66 69 65 64 20  w be identified 
6b640 62 79 20 61 20 64 61 74 61 62 61 73 65 20 6e 61  by a database na
6b650 6d 65 2c 20 61 20 64 6f 74 2c 20 74 68 65 6e 20  me, a dot, then 
6b660 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 3a 20  the table name: 
6b670 49 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ID.ID..**.** The
6b680 20 6a 6f 69 6e 74 79 70 65 20 73 74 61 72 74 73   jointype starts
6b690 20 6f 75 74 20 73 68 6f 77 69 6e 67 20 74 68 65   out showing the
6b6a0 20 6a 6f 69 6e 20 74 79 70 65 20 62 65 74 77 65   join type betwe
6b6b0 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  en the current t
6b6c0 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20  able.** and the 
6b6d0 6e 65 78 74 20 74 61 62 6c 65 20 6f 6e 20 74 68  next table on th
6b6e0 65 20 6c 69 73 74 2e 20 20 54 68 65 20 70 61 72  e list.  The par
6b6f0 73 65 72 20 62 75 69 6c 64 73 20 74 68 65 20 6c  ser builds the l
6b700 69 73 74 20 74 68 69 73 20 77 61 79 2e 0a 2a 2a  ist this way..**
6b710 20 42 75 74 20 73 71 6c 69 74 65 33 53 72 63 4c   But sqlite3SrcL
6b720 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65  istShiftJoinType
6b730 28 29 20 6c 61 74 65 72 20 73 68 69 66 74 73 20  () later shifts 
6b740 74 68 65 20 6a 6f 69 6e 74 79 70 65 73 20 73 6f  the jointypes so
6b750 20 74 68 61 74 20 65 61 63 68 0a 2a 2a 20 6a 6f   that each.** jo
6b760 69 6e 74 79 70 65 20 65 78 70 72 65 73 73 65 73  intype expresses
6b770 20 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65 65   the join betwee
6b780 6e 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20  n the table and 
6b790 74 68 65 20 70 72 65 76 69 6f 75 73 20 74 61 62  the previous tab
6b7a0 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 72  le..*/.struct Sr
6b7b0 63 4c 69 73 74 20 7b 0a 20 20 69 31 36 20 6e 53  cList {.  i16 nS
6b7c0 72 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  rc;        /* Nu
6b7d0 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 6f  mber of tables o
6b7e0 72 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20  r subqueries in 
6b7f0 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
6b800 2a 2f 0a 20 20 69 31 36 20 6e 41 6c 6c 6f 63 3b  */.  i16 nAlloc;
6b810 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
6b820 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63  of entries alloc
6b830 61 74 65 64 20 69 6e 20 61 5b 5d 20 62 65 6c 6f  ated in a[] belo
6b840 77 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72  w */.  struct Sr
6b850 63 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20  cList_item {.   
6b860 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65   char *zDatabase
6b870 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61  ;  /* Name of da
6b880 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74  tabase holding t
6b890 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20  his table */.   
6b8a0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
6b8b0 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
6b8c0 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 63  e table */.    c
6b8d0 68 61 72 20 2a 7a 41 6c 69 61 73 3b 20 20 20 20  har *zAlias;    
6b8e0 20 2f 2a 20 54 68 65 20 22 42 22 20 70 61 72 74   /* The "B" part
6b8f0 20 6f 66 20 61 20 22 41 20 41 53 20 42 22 20 70   of a "A AS B" p
6b900 68 72 61 73 65 2e 20 20 7a 4e 61 6d 65 20 69 73  hrase.  zName is
6b910 20 74 68 65 20 22 41 22 20 2a 2f 0a 20 20 20 20   the "A" */.    
6b920 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20  Table *pTab;    
6b930 20 20 2f 2a 20 41 6e 20 53 51 4c 20 74 61 62 6c    /* An SQL tabl
6b940 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
6b950 74 6f 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20  to zName */.    
6b960 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b  Select *pSelect;
6b970 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74    /* A SELECT st
6b980 61 74 65 6d 65 6e 74 20 75 73 65 64 20 69 6e 20  atement used in 
6b990 70 6c 61 63 65 20 6f 66 20 61 20 74 61 62 6c 65  place of a table
6b9a0 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 75 38 20   name */.    u8 
6b9b0 69 73 50 6f 70 75 6c 61 74 65 64 3b 20 20 20 2f  isPopulated;   /
6b9c0 2a 20 54 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  * Temporary tabl
6b9d0 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
6b9e0 68 20 53 45 4c 45 43 54 20 69 73 20 70 6f 70 75  h SELECT is popu
6b9f0 6c 61 74 65 64 20 2a 2f 0a 20 20 20 20 75 38 20  lated */.    u8 
6ba00 6a 6f 69 6e 74 79 70 65 3b 20 20 20 20 20 20 2f  jointype;      /
6ba10 2a 20 54 79 70 65 20 6f 66 20 6a 6f 69 6e 20 62  * Type of join b
6ba20 65 74 77 65 65 6e 20 74 68 69 73 20 61 62 6c 65  etween this able
6ba30 20 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75   and the previou
6ba40 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 75  s */.    int iCu
6ba50 72 73 6f 72 3b 20 20 20 20 20 20 2f 2a 20 54 68  rsor;      /* Th
6ba60 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75  e VDBE cursor nu
6ba70 6d 62 65 72 20 75 73 65 64 20 74 6f 20 61 63 63  mber used to acc
6ba80 65 73 73 20 74 68 69 73 20 74 61 62 6c 65 20 2a  ess this table *
6ba90 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 4f 6e 3b  /.    Expr *pOn;
6baa0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f          /* The O
6bab0 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f  N clause of a jo
6bac0 69 6e 20 2a 2f 0a 20 20 20 20 49 64 4c 69 73 74  in */.    IdList
6bad0 20 2a 70 55 73 69 6e 67 3b 20 20 20 2f 2a 20 54   *pUsing;   /* T
6bae0 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20  he USING clause 
6baf0 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 20  of a join */.   
6bb00 20 42 69 74 6d 61 73 6b 20 63 6f 6c 55 73 65 64   Bitmask colUsed
6bb10 3b 20 20 2f 2a 20 42 69 74 20 4e 20 28 31 3c 3c  ;  /* Bit N (1<<
6bb20 4e 29 20 73 65 74 20 69 66 20 63 6f 6c 75 6d 6e  N) set if column
6bb30 20 4e 20 6f 72 20 70 54 61 62 20 69 73 20 75 73   N or pTab is us
6bb40 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 6e 6f 74  ed */.    u8 not
6bb50 49 6e 64 65 78 65 64 3b 20 20 20 20 2f 2a 20 54  Indexed;    /* T
6bb60 72 75 65 20 69 66 20 74 68 65 72 65 20 69 73 20  rue if there is 
6bb70 61 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c  a NOT INDEXED cl
6bb80 61 75 73 65 20 2a 2f 0a 20 20 20 20 63 68 61 72  ause */.    char
6bb90 20 2a 7a 49 6e 64 65 78 3b 20 20 20 20 20 2f 2a   *zIndex;     /*
6bba0 20 49 64 65 6e 74 69 66 69 65 72 20 66 72 6f 6d   Identifier from
6bbb0 20 22 49 4e 44 45 58 45 44 20 42 59 20 3c 7a 49   "INDEXED BY <zI
6bbc0 6e 64 65 78 3e 22 20 63 6c 61 75 73 65 20 2a 2f  ndex>" clause */
6bbd0 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e 64  .    Index *pInd
6bbe0 65 78 3b 20 20 20 20 2f 2a 20 49 6e 64 65 78 20  ex;    /* Index 
6bbf0 73 74 72 75 63 74 75 72 65 20 63 6f 72 72 65 73  structure corres
6bc00 70 6f 6e 64 69 6e 67 20 74 6f 20 7a 49 6e 64 65  ponding to zInde
6bc10 78 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 7d  x, if any */.  }
6bc20 20 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 20   a[1];          
6bc30 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20     /* One entry 
6bc40 66 6f 72 20 65 61 63 68 20 69 64 65 6e 74 69 66  for each identif
6bc50 69 65 72 20 6f 6e 20 74 68 65 20 6c 69 73 74 20  ier on the list 
6bc60 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72  */.};../*.** Per
6bc70 6d 69 74 74 65 64 20 76 61 6c 75 65 73 20 6f 66  mitted values of
6bc80 20 74 68 65 20 53 72 63 4c 69 73 74 2e 61 2e 6a   the SrcList.a.j
6bc90 6f 69 6e 74 79 70 65 20 66 69 65 6c 64 0a 2a 2f  ointype field.*/
6bca0 0a 23 64 65 66 69 6e 65 20 4a 54 5f 49 4e 4e 45  .#define JT_INNE
6bcb0 52 20 20 20 20 20 30 78 30 30 30 31 20 20 20 20  R     0x0001    
6bcc0 2f 2a 20 41 6e 79 20 6b 69 6e 64 20 6f 66 20 69  /* Any kind of i
6bcd0 6e 6e 65 72 20 6f 72 20 63 72 6f 73 73 20 6a 6f  nner or cross jo
6bce0 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54  in */.#define JT
6bcf0 5f 43 52 4f 53 53 20 20 20 20 20 30 78 30 30 30  _CROSS     0x000
6bd00 32 20 20 20 20 2f 2a 20 45 78 70 6c 69 63 69 74  2    /* Explicit
6bd10 20 75 73 65 20 6f 66 20 74 68 65 20 43 52 4f 53   use of the CROS
6bd20 53 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65  S keyword */.#de
6bd30 66 69 6e 65 20 4a 54 5f 4e 41 54 55 52 41 4c 20  fine JT_NATURAL 
6bd40 20 20 30 78 30 30 30 34 20 20 20 20 2f 2a 20 54    0x0004    /* T
6bd50 72 75 65 20 66 6f 72 20 61 20 22 6e 61 74 75 72  rue for a "natur
6bd60 61 6c 22 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66  al" join */.#def
6bd70 69 6e 65 20 4a 54 5f 4c 45 46 54 20 20 20 20 20  ine JT_LEFT     
6bd80 20 30 78 30 30 30 38 20 20 20 20 2f 2a 20 4c 65   0x0008    /* Le
6bd90 66 74 20 6f 75 74 65 72 20 6a 6f 69 6e 20 2a 2f  ft outer join */
6bda0 0a 23 64 65 66 69 6e 65 20 4a 54 5f 52 49 47 48  .#define JT_RIGH
6bdb0 54 20 20 20 20 20 30 78 30 30 31 30 20 20 20 20  T     0x0010    
6bdc0 2f 2a 20 52 69 67 68 74 20 6f 75 74 65 72 20 6a  /* Right outer j
6bdd0 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a  oin */.#define J
6bde0 54 5f 4f 55 54 45 52 20 20 20 20 20 30 78 30 30  T_OUTER     0x00
6bdf0 32 30 20 20 20 20 2f 2a 20 54 68 65 20 22 4f 55  20    /* The "OU
6be00 54 45 52 22 20 6b 65 79 77 6f 72 64 20 69 73 20  TER" keyword is 
6be10 70 72 65 73 65 6e 74 20 2a 2f 0a 23 64 65 66 69  present */.#defi
6be20 6e 65 20 4a 54 5f 45 52 52 4f 52 20 20 20 20 20  ne JT_ERROR     
6be30 30 78 30 30 34 30 20 20 20 20 2f 2a 20 75 6e 6b  0x0040    /* unk
6be40 6e 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70 6f 72  nown or unsuppor
6be50 74 65 64 20 6a 6f 69 6e 20 74 79 70 65 20 2a 2f  ted join type */
6be60 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 65 61 63 68  ../*.** For each
6be70 20 6e 65 73 74 65 64 20 6c 6f 6f 70 20 69 6e 20   nested loop in 
6be80 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 69  a WHERE clause i
6be90 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 74  mplementation, t
6bea0 68 65 20 57 68 65 72 65 49 6e 66 6f 0a 2a 2a 20  he WhereInfo.** 
6beb0 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
6bec0 6e 73 20 61 20 73 69 6e 67 6c 65 20 69 6e 73 74  ns a single inst
6bed0 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
6bee0 75 63 74 75 72 65 2e 20 20 54 68 69 73 20 73 74  ucture.  This st
6bef0 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 69 6e  ructure.** is in
6bf00 74 65 6e 64 65 64 20 74 6f 20 62 65 20 70 72 69  tended to be pri
6bf10 76 61 74 65 20 74 68 65 20 74 68 65 20 77 68 65  vate the the whe
6bf20 72 65 2e 63 20 6d 6f 64 75 6c 65 20 61 6e 64 20  re.c module and 
6bf30 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a  should not be.**
6bf40 20 61 63 63 65 73 73 20 6f 72 20 6d 6f 64 69 66   access or modif
6bf50 69 65 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64  ied by other mod
6bf60 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ules..**.** The 
6bf70 70 49 64 78 49 6e 66 6f 20 61 6e 64 20 70 42 65  pIdxInfo and pBe
6bf80 73 74 49 64 78 20 66 69 65 6c 64 73 20 61 72 65  stIdx fields are
6bf90 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 70 69   used to help pi
6bfa0 63 6b 20 74 68 65 20 62 65 73 74 0a 2a 2a 20 69  ck the best.** i
6bfb0 6e 64 65 78 20 6f 6e 20 61 20 76 69 72 74 75 61  ndex on a virtua
6bfc0 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20 70 49  l table.  The pI
6bfd0 64 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72 20 63  dxInfo pointer c
6bfe0 6f 6e 74 61 69 6e 73 20 69 6e 64 65 78 69 6e 67  ontains indexing
6bff0 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  .** information 
6c000 66 6f 72 20 74 68 65 20 69 2d 74 68 20 74 61 62  for the i-th tab
6c010 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  le in the FROM c
6c020 6c 61 75 73 65 20 62 65 66 6f 72 65 20 72 65 6f  lause before reo
6c030 72 64 65 72 69 6e 67 2e 0a 2a 2a 20 41 6c 6c 20  rdering..** All 
6c040 74 68 65 20 70 49 64 78 49 6e 66 6f 20 70 6f 69  the pIdxInfo poi
6c050 6e 74 65 72 73 20 61 72 65 20 66 72 65 65 64 20  nters are freed 
6c060 62 79 20 77 68 65 72 65 49 6e 66 6f 46 72 65 65  by whereInfoFree
6c070 28 29 20 69 6e 20 77 68 65 72 65 2e 63 2e 0a 2a  () in where.c..*
6c080 2a 20 54 68 65 20 70 42 65 73 74 49 64 78 20 70  * The pBestIdx p
6c090 6f 69 6e 74 65 72 20 69 73 20 61 20 63 6f 70 79  ointer is a copy
6c0a0 20 6f 66 20 70 49 64 78 49 6e 66 6f 20 66 6f 72   of pIdxInfo for
6c0b0 20 74 68 65 20 69 2d 74 68 20 74 61 62 6c 65 20   the i-th table 
6c0c0 61 66 74 65 72 0a 2a 2a 20 46 52 4f 4d 20 63 6c  after.** FROM cl
6c0d0 61 75 73 65 20 6f 72 64 65 72 69 6e 67 2e 20 20  ause ordering.  
6c0e0 54 68 69 73 20 69 73 20 61 20 6c 69 74 74 6c 65  This is a little
6c0f0 20 63 6f 6e 66 75 73 69 6e 67 20 73 6f 20 49 20   confusing so I 
6c100 77 69 6c 6c 20 72 65 70 65 61 74 0a 2a 2a 20 69  will repeat.** i
6c110 74 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 77  t in different w
6c120 6f 72 64 73 2e 20 20 57 68 65 72 65 49 6e 66 6f  ords.  WhereInfo
6c130 2e 61 5b 69 5d 2e 70 49 64 78 49 6e 66 6f 20 69  .a[i].pIdxInfo i
6c140 73 20 69 6e 64 65 78 20 69 6e 66 6f 72 6d 61 74  s index informat
6c150 69 6f 6e 20 0a 2a 2a 20 66 6f 72 20 57 68 65 72  ion .** for Wher
6c160 65 49 6e 66 6f 2e 70 54 61 62 4c 69 73 74 2e 61  eInfo.pTabList.a
6c170 5b 69 5d 2e 20 20 57 68 65 72 65 49 6e 66 6f 2e  [i].  WhereInfo.
6c180 61 5b 69 5d 2e 70 42 65 73 74 49 6e 66 6f 20 69  a[i].pBestInfo i
6c190 73 20 74 68 65 0a 2a 2a 20 69 6e 64 65 78 20 69  s the.** index i
6c1a0 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74  nformation for t
6c1b0 68 65 20 69 2d 74 68 20 6c 6f 6f 70 20 6f 66 20  he i-th loop of 
6c1c0 74 68 65 20 6a 6f 69 6e 2e 20 20 70 42 65 73 74  the join.  pBest
6c1d0 49 6e 66 6f 20 69 73 20 61 6c 77 61 79 73 0a 2a  Info is always.*
6c1e0 2a 20 65 69 74 68 65 72 20 4e 55 4c 4c 20 6f 72  * either NULL or
6c1f0 20 61 20 63 6f 70 79 20 6f 66 20 73 6f 6d 65 20   a copy of some 
6c200 70 49 64 78 49 6e 66 6f 2e 20 20 53 6f 20 66 6f  pIdxInfo.  So fo
6c210 72 20 63 6c 65 61 6e 75 70 20 69 74 20 69 73 20  r cleanup it is 
6c220 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 20 74  .** sufficient t
6c230 6f 20 66 72 65 65 20 61 6c 6c 20 6f 66 20 74 68  o free all of th
6c240 65 20 70 49 64 78 49 6e 66 6f 20 70 6f 69 6e 74  e pIdxInfo point
6c250 65 72 73 2e 0a 2a 2a 20 0a 2a 2f 0a 73 74 72 75  ers..** .*/.stru
6c260 63 74 20 57 68 65 72 65 4c 65 76 65 6c 20 7b 0a  ct WhereLevel {.
6c270 20 20 69 6e 74 20 69 46 72 6f 6d 3b 20 20 20 20    int iFrom;    
6c280 20 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63 68          /* Which
6c290 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 46 52   entry in the FR
6c2a0 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69  OM clause */.  i
6c2b0 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20  nt flags;       
6c2c0 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 61 73       /* Flags as
6c2d0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
6c2e0 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 69 6e  is level */.  in
6c2f0 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 20  t iMem;         
6c300 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6d 65 6d      /* First mem
6c310 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 62 79  ory cell used by
6c320 20 74 68 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20   this level */. 
6c330 20 69 6e 74 20 69 4c 65 66 74 4a 6f 69 6e 3b 20   int iLeftJoin; 
6c340 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79         /* Memory
6c350 20 63 65 6c 6c 20 75 73 65 64 20 74 6f 20 69 6d   cell used to im
6c360 70 6c 65 6d 65 6e 74 20 4c 45 46 54 20 4f 55 54  plement LEFT OUT
6c370 45 52 20 4a 4f 49 4e 20 2a 2f 0a 20 20 49 6e 64  ER JOIN */.  Ind
6c380 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20  ex *pIdx;       
6c390 20 20 20 2f 2a 20 49 6e 64 65 78 20 75 73 65 64     /* Index used
6c3a0 2e 20 20 4e 55 4c 4c 20 69 66 20 6e 6f 20 69 6e  .  NULL if no in
6c3b0 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61  dex */.  int iTa
6c3c0 62 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 2f  bCur;          /
6c3d0 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f  * The VDBE curso
6c3e0 72 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73  r used to access
6c3f0 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20   the table */.  
6c400 69 6e 74 20 69 49 64 78 43 75 72 3b 20 20 20 20  int iIdxCur;    
6c410 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42        /* The VDB
6c420 45 20 63 75 72 73 6f 72 20 75 73 65 64 20 74 6f  E cursor used to
6c430 20 61 63 65 73 73 73 20 70 49 64 78 20 2a 2f 0a   acesss pIdx */.
6c440 20 20 69 6e 74 20 62 72 6b 3b 20 20 20 20 20 20    int brk;      
6c450 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20          /* Jump 
6c460 68 65 72 65 20 74 6f 20 62 72 65 61 6b 20 6f 75  here to break ou
6c470 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f  t of the loop */
6c480 0a 20 20 69 6e 74 20 6e 78 74 3b 20 20 20 20 20  .  int nxt;     
6c490 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70           /* Jump
6c4a0 20 68 65 72 65 20 74 6f 20 73 74 61 72 74 20 74   here to start t
6c4b0 68 65 20 6e 65 78 74 20 49 4e 20 63 6f 6d 62 69  he next IN combi
6c4c0 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  nation */.  int 
6c4d0 63 6f 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  cont;           
6c4e0 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
6c4f0 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20  o continue with 
6c500 74 68 65 20 6e 65 78 74 20 6c 6f 6f 70 20 63 79  the next loop cy
6c510 63 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70  cle */.  int top
6c520 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
6c530 2a 20 46 69 72 73 74 20 69 6e 73 74 72 75 63 74  * First instruct
6c540 69 6f 6e 20 6f 66 20 69 6e 74 65 72 69 6f 72 20  ion of interior 
6c550 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20  of the loop */. 
6c560 20 69 6e 74 20 6f 70 2c 20 70 31 2c 20 70 32 2c   int op, p1, p2,
6c570 20 70 35 3b 20 20 20 2f 2a 20 4f 70 63 6f 64 65   p5;   /* Opcode
6c580 20 75 73 65 64 20 74 6f 20 74 65 72 6d 69 6e 61   used to termina
6c590 74 65 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20  te the loop */. 
6c5a0 20 69 6e 74 20 6e 45 71 3b 20 20 20 20 20 20 20   int nEq;       
6c5b0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
6c5c0 20 6f 66 20 3d 3d 20 6f 72 20 49 4e 20 63 6f 6e   of == or IN con
6c5d0 73 74 72 61 69 6e 74 73 20 6f 6e 20 74 68 69 73  straints on this
6c5e0 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e   loop */.  int n
6c5f0 49 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  In;             
6c600 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 49 4e   /* Number of IN
6c610 20 6f 70 65 72 61 74 6f 72 73 20 63 6f 6e 73 74   operators const
6c620 72 61 69 6e 69 6e 67 20 74 68 69 73 20 6c 6f 6f  raining this loo
6c630 70 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 49 6e  p */.  struct In
6c640 4c 6f 6f 70 20 7b 0a 20 20 20 20 69 6e 74 20 69  Loop {.    int i
6c650 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  Cur;            
6c660 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75    /* The VDBE cu
6c670 72 73 6f 72 20 75 73 65 64 20 62 79 20 74 68 69  rsor used by thi
6c680 73 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f  s IN operator */
6c690 0a 20 20 20 20 69 6e 74 20 74 6f 70 41 64 64 72  .    int topAddr
6c6a0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
6c6b0 6f 70 20 6f 66 20 74 68 65 20 49 4e 20 6c 6f 6f  op of the IN loo
6c6c0 70 20 2a 2f 0a 20 20 7d 20 2a 61 49 6e 4c 6f 6f  p */.  } *aInLoo
6c6d0 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  p;           /* 
6c6e0 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  Information abou
6c6f0 74 20 65 61 63 68 20 6e 65 73 74 65 64 20 49 4e  t each nested IN
6c700 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 73   operator */.  s
6c710 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
6c720 6f 20 2a 70 42 65 73 74 49 64 78 3b 20 20 2f 2a  o *pBestIdx;  /*
6c730 20 49 6e 64 65 78 20 69 6e 66 6f 72 6d 61 74 69   Index informati
6c740 6f 6e 20 66 6f 72 20 74 68 69 73 20 6c 65 76 65  on for this leve
6c750 6c 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 66  l */..  /* The f
6c760 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 20 69  ollowing field i
6c770 73 20 72 65 61 6c 6c 79 20 6e 6f 74 20 70 61 72  s really not par
6c780 74 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74  t of the current
6c790 20 6c 65 76 65 6c 2e 20 20 42 75 74 0a 20 20 2a   level.  But.  *
6c7a0 2a 20 77 65 20 6e 65 65 64 20 61 20 70 6c 61 63  * we need a plac
6c7b0 65 20 74 6f 20 63 61 63 68 65 20 69 6e 64 65 78  e to cache index
6c7c0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72   information for
6c7d0 20 65 61 63 68 20 74 61 62 6c 65 20 69 6e 20 74   each table in t
6c7e0 68 65 0a 20 20 2a 2a 20 46 52 4f 4d 20 63 6c 61  he.  ** FROM cla
6c7f0 75 73 65 20 61 6e 64 20 74 68 65 20 57 68 65 72  use and the Wher
6c800 65 4c 65 76 65 6c 20 73 74 72 75 63 74 75 72 65  eLevel structure
6c810 20 69 73 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74   is a convenient
6c820 20 70 6c 61 63 65 2e 0a 20 20 2a 2f 0a 20 20 73   place..  */.  s
6c830 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
6c840 6f 20 2a 70 49 64 78 49 6e 66 6f 3b 20 20 2f 2a  o *pIdxInfo;  /*
6c850 20 49 6e 64 65 78 20 69 6e 66 6f 20 66 6f 72 20   Index info for 
6c860 6e 2d 74 68 20 73 6f 75 72 63 65 20 74 61 62 6c  n-th source tabl
6c870 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46  e */.};../*.** F
6c880 6c 61 67 73 20 61 70 70 72 6f 70 72 69 61 74 65  lags appropriate
6c890 20 66 6f 72 20 74 68 65 20 77 66 6c 61 67 73 20   for the wflags 
6c8a0 70 61 72 61 6d 65 74 65 72 20 6f 66 20 73 71 6c  parameter of sql
6c8b0 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
6c8c0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  ..*/.#define WHE
6c8d0 52 45 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41  RE_ORDERBY_NORMA
6c8e0 4c 20 20 20 20 20 30 20 20 20 2f 2a 20 4e 6f 2d  L     0   /* No-
6c8f0 6f 70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48  op */.#define WH
6c900 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 20  ERE_ORDERBY_MIN 
6c910 20 20 20 20 20 20 20 31 20 20 20 2f 2a 20 4f 52         1   /* OR
6c920 44 45 52 20 42 59 20 70 72 6f 63 65 73 73 69 6e  DER BY processin
6c930 67 20 66 6f 72 20 6d 69 6e 28 29 20 66 75 6e 63  g for min() func
6c940 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52   */.#define WHER
6c950 45 5f 4f 52 44 45 52 42 59 5f 4d 41 58 20 20 20  E_ORDERBY_MAX   
6c960 20 20 20 20 20 32 20 20 20 2f 2a 20 4f 52 44 45       2   /* ORDE
6c970 52 20 42 59 20 70 72 6f 63 65 73 73 69 6e 67 20  R BY processing 
6c980 66 6f 72 20 6d 61 78 28 29 20 66 75 6e 63 20 2a  for max() func *
6c990 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
6c9a0 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 45 44 20  ONEPASS_DESIRED 
6c9b0 20 20 20 34 20 20 20 2f 2a 20 57 61 6e 74 20 74     4   /* Want t
6c9c0 6f 20 64 6f 20 6f 6e 65 2d 70 61 73 73 20 55 50  o do one-pass UP
6c9d0 44 41 54 45 2f 44 45 4c 45 54 45 20 2a 2f 0a 0a  DATE/DELETE */..
6c9e0 2f 2a 0a 2a 2a 20 54 68 65 20 57 48 45 52 45 20  /*.** The WHERE 
6c9f0 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e  clause processin
6ca00 67 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 77  g routine has tw
6ca10 6f 20 68 61 6c 76 65 73 2e 20 20 54 68 65 0a 2a  o halves.  The.*
6ca20 2a 20 66 69 72 73 74 20 70 61 72 74 20 64 6f 65  * first part doe
6ca30 73 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  s the start of t
6ca40 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 61 6e  he WHERE loop an
6ca50 64 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20  d the second.** 
6ca60 68 61 6c 66 20 64 6f 65 73 20 74 68 65 20 74 61  half does the ta
6ca70 69 6c 20 6f 66 20 74 68 65 20 57 48 45 52 45 20  il of the WHERE 
6ca80 6c 6f 6f 70 2e 20 20 41 6e 20 69 6e 73 74 61 6e  loop.  An instan
6ca90 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 74  ce of.** this st
6caa0 72 75 63 74 75 72 65 20 69 73 20 72 65 74 75 72  ructure is retur
6cab0 6e 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74  ned by the first
6cac0 20 68 61 6c 66 20 61 6e 64 20 70 61 73 73 65 64   half and passed
6cad0 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 65 63  .** into the sec
6cae0 6f 6e 64 20 68 61 6c 66 20 74 6f 20 67 69 76 65  ond half to give
6caf0 20 73 6f 6d 65 20 63 6f 6e 74 69 6e 75 69 74 79   some continuity
6cb00 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72  ..*/.struct Wher
6cb10 65 49 6e 66 6f 20 7b 0a 20 20 50 61 72 73 65 20  eInfo {.  Parse 
6cb20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 2f  *pParse;       /
6cb30 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f  * Parsing and co
6cb40 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  de generating co
6cb50 6e 74 65 78 74 20 2a 2f 0a 20 20 75 38 20 6f 6b  ntext */.  u8 ok
6cb60 4f 6e 65 50 61 73 73 3b 20 20 20 20 20 20 20 20  OnePass;        
6cb70 2f 2a 20 4f 6b 20 74 6f 20 75 73 65 20 6f 6e 65  /* Ok to use one
6cb80 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 20  -pass algorithm 
6cb90 66 6f 72 20 55 50 44 41 54 45 20 6f 72 20 44 45  for UPDATE or DE
6cba0 4c 45 54 45 20 2a 2f 0a 20 20 53 72 63 4c 69 73  LETE */.  SrcLis
6cbb0 74 20 2a 70 54 61 62 4c 69 73 74 3b 20 20 20 2f  t *pTabList;   /
6cbc0 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73  * List of tables
6cbd0 20 69 6e 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a   in the join */.
6cbe0 20 20 69 6e 74 20 69 54 6f 70 3b 20 20 20 20 20    int iTop;     
6cbf0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 65         /* The ve
6cc00 72 79 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  ry beginning of 
6cc10 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 2a  the WHERE loop *
6cc20 2f 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75  /.  int iContinu
6cc30 65 3b 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70  e;       /* Jump
6cc40 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75   here to continu
6cc50 65 20 77 69 74 68 20 6e 65 78 74 20 72 65 63 6f  e with next reco
6cc60 72 64 20 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65  rd */.  int iBre
6cc70 61 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ak;          /* 
6cc80 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 62 72 65  Jump here to bre
6cc90 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f  ak out of the lo
6cca0 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 65 76  op */.  int nLev
6ccb0 65 6c 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  el;          /* 
6ccc0 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64  Number of nested
6ccd0 20 6c 6f 6f 70 20 2a 2f 0a 20 20 73 71 6c 69 74   loop */.  sqlit
6cce0 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 2a  e3_index_info **
6ccf0 61 70 49 6e 66 6f 3b 20 20 2f 2a 20 41 72 72 61  apInfo;  /* Arra
6cd00 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f  y of pointers to
6cd10 20 69 6e 64 65 78 20 69 6e 66 6f 20 73 74 72 75   index info stru
6cd20 63 74 75 72 65 73 20 2a 2f 0a 20 20 57 68 65 72  ctures */.  Wher
6cd30 65 4c 65 76 65 6c 20 61 5b 31 5d 3b 20 20 20 20  eLevel a[1];    
6cd40 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
6cd50 61 62 6f 75 74 20 65 61 63 68 20 6e 65 73 74 20  about each nest 
6cd60 6c 6f 6f 70 20 69 6e 20 74 68 65 20 57 48 45 52  loop in the WHER
6cd70 45 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  E */.};../*.** A
6cd80 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 64 65 66   NameContext def
6cd90 69 6e 65 73 20 61 20 63 6f 6e 74 65 78 74 20 69  ines a context i
6cda0 6e 20 77 68 69 63 68 20 74 6f 20 72 65 73 6f 6c  n which to resol
6cdb0 76 65 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c  ve table and col
6cdc0 75 6d 6e 0a 2a 2a 20 6e 61 6d 65 73 2e 20 20 54  umn.** names.  T
6cdd0 68 65 20 63 6f 6e 74 65 78 74 20 63 6f 6e 73 69  he context consi
6cde0 73 74 73 20 6f 66 20 61 20 6c 69 73 74 20 6f 66  sts of a list of
6cdf0 20 74 61 62 6c 65 73 20 28 74 68 65 20 70 53 72   tables (the pSr
6ce00 63 4c 69 73 74 29 20 66 69 65 6c 64 20 61 6e 64  cList) field and
6ce10 0a 2a 2a 20 61 20 6c 69 73 74 20 6f 66 20 6e 61  .** a list of na
6ce20 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 28  med expression (
6ce30 70 45 4c 69 73 74 29 2e 20 20 54 68 65 20 6e 61  pEList).  The na
6ce40 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 6c  med expression l
6ce50 69 73 74 20 6d 61 79 0a 2a 2a 20 62 65 20 4e 55  ist may.** be NU
6ce60 4c 4c 2e 20 20 54 68 65 20 70 53 72 63 20 63 6f  LL.  The pSrc co
6ce70 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65  rresponds to the
6ce80 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
6ce90 61 20 53 45 4c 45 43 54 20 6f 72 0a 2a 2a 20 74  a SELECT or.** t
6cea0 6f 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e  o the table bein
6ceb0 67 20 6f 70 65 72 61 74 65 64 20 6f 6e 20 62 79  g operated on by
6cec0 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c   INSERT, UPDATE,
6ced0 20 6f 72 20 44 45 4c 45 54 45 2e 20 20 54 68 65   or DELETE.  The
6cee0 0a 2a 2a 20 70 45 4c 69 73 74 20 63 6f 72 72 65  .** pEList corre
6cef0 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 72 65  sponds to the re
6cf00 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20 53 45  sult set of a SE
6cf10 4c 45 43 54 20 61 6e 64 20 69 73 20 4e 55 4c 4c  LECT and is NULL
6cf20 20 66 6f 72 0a 2a 2a 20 6f 74 68 65 72 20 73 74   for.** other st
6cf30 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20  atements..**.** 
6cf40 4e 61 6d 65 43 6f 6e 74 65 78 74 73 20 63 61 6e  NameContexts can
6cf50 20 62 65 20 6e 65 73 74 65 64 2e 20 20 57 68 65   be nested.  Whe
6cf60 6e 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65  n resolving name
6cf70 73 2c 20 74 68 65 20 69 6e 6e 65 72 2d 6d 6f 73  s, the inner-mos
6cf80 74 20 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73  t .** context is
6cf90 20 73 65 61 72 63 68 65 64 20 66 69 72 73 74 2e   searched first.
6cfa0 20 20 49 66 20 6e 6f 20 6d 61 74 63 68 20 69 73    If no match is
6cfb0 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e 65 78 74   found, the next
6cfc0 20 6f 75 74 65 72 0a 2a 2a 20 63 6f 6e 74 65 78   outer.** contex
6cfd0 74 20 69 73 20 63 68 65 63 6b 65 64 2e 20 20 49  t is checked.  I
6cfe0 66 20 74 68 65 72 65 20 69 73 20 73 74 69 6c 6c  f there is still
6cff0 20 6e 6f 20 6d 61 74 63 68 2c 20 74 68 65 20 6e   no match, the n
6d000 65 78 74 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 69  ext context.** i
6d010 73 20 63 68 65 63 6b 65 64 2e 20 20 54 68 69 73  s checked.  This
6d020 20 70 72 6f 63 65 73 73 20 63 6f 6e 74 69 6e 75   process continu
6d030 65 73 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20  es until either 
6d040 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64  a match is found
6d050 0a 2a 2a 20 6f 72 20 61 6c 6c 20 63 6f 6e 74 65  .** or all conte
6d060 78 74 73 20 61 72 65 20 63 68 65 63 6b 2e 20 20  xts are check.  
6d070 57 68 65 6e 20 61 20 6d 61 74 63 68 20 69 73 20  When a match is 
6d080 66 6f 75 6e 64 2c 20 74 68 65 20 6e 52 65 66 20  found, the nRef 
6d090 6d 65 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 68 65  member of.** the
6d0a0 20 63 6f 6e 74 65 78 74 20 63 6f 6e 74 61 69 6e   context contain
6d0b0 69 6e 67 20 74 68 65 20 6d 61 74 63 68 20 69 73  ing the match is
6d0c0 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e 20 0a 2a   incremented. .*
6d0d0 2a 0a 2a 2a 20 45 61 63 68 20 73 75 62 71 75 65  *.** Each subque
6d0e0 72 79 20 67 65 74 73 20 61 20 6e 65 77 20 4e 61  ry gets a new Na
6d0f0 6d 65 43 6f 6e 74 65 78 74 2e 20 20 54 68 65 20  meContext.  The 
6d100 70 4e 65 78 74 20 66 69 65 6c 64 20 70 6f 69 6e  pNext field poin
6d110 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 4e 61 6d  ts to the.** Nam
6d120 65 43 6f 6e 74 65 78 74 20 69 6e 20 74 68 65 20  eContext in the 
6d130 70 61 72 65 6e 74 20 71 75 65 72 79 2e 20 20 54  parent query.  T
6d140 68 75 73 20 74 68 65 20 70 72 6f 63 65 73 73 20  hus the process 
6d150 6f 66 20 73 63 61 6e 6e 69 6e 67 20 74 68 65 0a  of scanning the.
6d160 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 6c  ** NameContext l
6d170 69 73 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20  ist corresponds 
6d180 74 6f 20 73 65 61 72 63 68 69 6e 67 20 74 68 72  to searching thr
6d190 6f 75 67 68 20 73 75 63 63 65 73 73 69 76 65 6c  ough successivel
6d1a0 79 20 6f 75 74 65 72 0a 2a 2a 20 73 75 62 71 75  y outer.** subqu
6d1b0 65 72 69 65 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f  eries looking fo
6d1c0 72 20 61 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73 74  r a match..*/.st
6d1d0 72 75 63 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74  ruct NameContext
6d1e0 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72   {.  Parse *pPar
6d1f0 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  se;       /* The
6d200 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 53 72 63   parser */.  Src
6d210 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 3b 20  List *pSrcList; 
6d220 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65    /* One or more
6d230 20 74 61 62 6c 65 73 20 75 73 65 64 20 74 6f 20   tables used to 
6d240 72 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 2a 2f  resolve names */
6d250 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
6d260 69 73 74 3b 20 20 20 20 2f 2a 20 4f 70 74 69 6f  ist;    /* Optio
6d270 6e 61 6c 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65  nal list of name
6d280 64 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f  d expressions */
6d290 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20  .  int nRef;    
6d2a0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
6d2b0 72 20 6f 66 20 6e 61 6d 65 73 20 72 65 73 6f 6c  r of names resol
6d2c0 76 65 64 20 62 79 20 74 68 69 73 20 63 6f 6e 74  ved by this cont
6d2d0 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72  ext */.  int nEr
6d2e0 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  r;            /*
6d2f0 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72   Number of error
6d300 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 77 68  s encountered wh
6d310 69 6c 65 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61  ile resolving na
6d320 6d 65 73 20 2a 2f 0a 20 20 75 38 20 61 6c 6c 6f  mes */.  u8 allo
6d330 77 41 67 67 3b 20 20 20 20 20 20 20 20 20 2f 2a  wAgg;         /*
6d340 20 41 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   Aggregate funct
6d350 69 6f 6e 73 20 61 6c 6c 6f 77 65 64 20 68 65 72  ions allowed her
6d360 65 20 2a 2f 0a 20 20 75 38 20 68 61 73 41 67 67  e */.  u8 hasAgg
6d370 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
6d380 72 75 65 20 69 66 20 61 67 67 72 65 67 61 74 65  rue if aggregate
6d390 73 20 61 72 65 20 73 65 65 6e 20 2a 2f 0a 20 20  s are seen */.  
6d3a0 75 38 20 69 73 43 68 65 63 6b 3b 20 20 20 20 20  u8 isCheck;     
6d3b0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
6d3c0 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20  resolving names 
6d3d0 69 6e 20 61 20 43 48 45 43 4b 20 63 6f 6e 73 74  in a CHECK const
6d3e0 72 61 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e  raint */.  int n
6d3f0 44 65 70 74 68 3b 20 20 20 20 20 20 20 20 20 20  Depth;          
6d400 2f 2a 20 44 65 70 74 68 20 6f 66 20 73 75 62 71  /* Depth of subq
6d410 75 65 72 79 20 72 65 63 75 72 73 69 6f 6e 2e 20  uery recursion. 
6d420 31 20 66 6f 72 20 6e 6f 20 72 65 63 75 72 73 69  1 for no recursi
6d430 6f 6e 20 2a 2f 0a 20 20 41 67 67 49 6e 66 6f 20  on */.  AggInfo 
6d440 2a 70 41 67 67 49 6e 66 6f 3b 20 20 20 2f 2a 20  *pAggInfo;   /* 
6d450 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  Information abou
6d460 74 20 61 67 67 72 65 67 61 74 65 73 20 61 74 20  t aggregates at 
6d470 74 68 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 20  this level */.  
6d480 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 65  NameContext *pNe
6d490 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 6f 75 74  xt;  /* Next out
6d4a0 65 72 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 2e  er name context.
6d4b0 20 20 4e 55 4c 4c 20 66 6f 72 20 6f 75 74 65 72    NULL for outer
6d4c0 6d 6f 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  most */.};../*.*
6d4d0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
6d4e0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
6d4f0 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
6d500 73 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  s all informatio
6d510 6e 0a 2a 2a 20 6e 65 65 64 65 64 20 74 6f 20 67  n.** needed to g
6d520 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
6d530 20 61 20 73 69 6e 67 6c 65 20 53 45 4c 45 43 54   a single SELECT
6d540 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a   statement..**.*
6d550 2a 20 6e 4c 69 6d 69 74 20 69 73 20 73 65 74 20  * nLimit is set 
6d560 74 6f 20 2d 31 20 69 66 20 74 68 65 72 65 20 69  to -1 if there i
6d570 73 20 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75 73  s no LIMIT claus
6d580 65 2e 20 20 6e 4f 66 66 73 65 74 20 69 73 20 73  e.  nOffset is s
6d590 65 74 20 74 6f 20 30 2e 0a 2a 2a 20 49 66 20 74  et to 0..** If t
6d5a0 68 65 72 65 20 69 73 20 61 20 4c 49 4d 49 54 20  here is a LIMIT 
6d5b0 63 6c 61 75 73 65 2c 20 74 68 65 20 70 61 72 73  clause, the pars
6d5c0 65 72 20 73 65 74 73 20 6e 4c 69 6d 69 74 20 74  er sets nLimit t
6d5d0 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  o the value of t
6d5e0 68 65 0a 2a 2a 20 6c 69 6d 69 74 20 61 6e 64 20  he.** limit and 
6d5f0 6e 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 76  nOffset to the v
6d600 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 66 66 73  alue of the offs
6d610 65 74 20 28 6f 72 20 30 20 69 66 20 74 68 65 72  et (or 0 if ther
6d620 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 6f 66 66 73  e is not.** offs
6d630 65 74 29 2e 20 20 42 75 74 20 6c 61 74 65 72 20  et).  But later 
6d640 6f 6e 2c 20 6e 4c 69 6d 69 74 20 61 6e 64 20 6e  on, nLimit and n
6d650 4f 66 66 73 65 74 20 62 65 63 6f 6d 65 20 74 68  Offset become th
6d660 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  e memory locatio
6d670 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 56 44 42  ns.** in the VDB
6d680 45 20 74 68 61 74 20 72 65 63 6f 72 64 20 74 68  E that record th
6d690 65 20 6c 69 6d 69 74 20 61 6e 64 20 6f 66 66 73  e limit and offs
6d6a0 65 74 20 63 6f 75 6e 74 65 72 73 2e 0a 2a 2a 0a  et counters..**.
6d6b0 2a 2a 20 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b  ** addrOpenEphm[
6d6c0 5d 20 65 6e 74 72 69 65 73 20 63 6f 6e 74 61 69  ] entries contai
6d6d0 6e 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66  n the address of
6d6e0 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
6d6f0 6c 20 6f 70 63 6f 64 65 73 2e 0a 2a 2a 20 54 68  l opcodes..** Th
6d700 65 73 65 20 61 64 64 72 65 73 73 65 73 20 6d 75  ese addresses mu
6d710 73 74 20 62 65 20 73 74 6f 72 65 64 20 73 6f 20  st be stored so 
6d720 74 68 61 74 20 77 65 20 63 61 6e 20 67 6f 20 62  that we can go b
6d730 61 63 6b 20 61 6e 64 20 66 69 6c 6c 20 69 6e 0a  ack and fill in.
6d740 2a 2a 20 74 68 65 20 50 34 5f 4b 45 59 49 4e 46  ** the P4_KEYINF
6d750 4f 20 61 6e 64 20 50 32 20 70 61 72 61 6d 65 74  O and P2 paramet
6d760 65 72 73 20 6c 61 74 65 72 2e 20 20 4e 65 69 74  ers later.  Neit
6d770 68 65 72 20 74 68 65 20 4b 65 79 49 6e 66 6f 20  her the KeyInfo 
6d780 6e 6f 72 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65  nor.** the numbe
6d790 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
6d7a0 50 32 20 63 61 6e 20 62 65 20 63 6f 6d 70 75 74  P2 can be comput
6d7b0 65 64 20 61 74 20 74 68 65 20 73 61 6d 65 20 74  ed at the same t
6d7c0 69 6d 65 0a 2a 2a 20 61 73 20 74 68 65 20 4f 50  ime.** as the OP
6d7d0 5f 4f 70 65 6e 45 70 68 6d 20 69 6e 73 74 72 75  _OpenEphm instru
6d7e0 63 74 69 6f 6e 20 69 73 20 63 6f 64 65 64 20 62  ction is coded b
6d7f0 65 63 61 75 73 65 20 6e 6f 74 0a 2a 2a 20 65 6e  ecause not.** en
6d800 6f 75 67 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ough information
6d810 20 61 62 6f 75 74 20 74 68 65 20 63 6f 6d 70 6f   about the compo
6d820 75 6e 64 20 71 75 65 72 79 20 69 73 20 6b 6e 6f  und query is kno
6d830 77 6e 20 61 74 20 74 68 61 74 20 70 6f 69 6e 74  wn at that point
6d840 2e 0a 2a 2a 20 54 68 65 20 4b 65 79 49 6e 66 6f  ..** The KeyInfo
6d850 20 66 6f 72 20 61 64 64 72 4f 70 65 6e 54 72 61   for addrOpenTra
6d860 6e 5b 30 5d 20 61 6e 64 20 5b 31 5d 20 63 6f 6e  n[0] and [1] con
6d870 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 20  tains collating 
6d880 73 65 71 75 65 6e 63 65 73 0a 2a 2a 20 66 6f 72  sequences.** for
6d890 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e   the result set.
6d8a0 20 20 54 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f    The KeyInfo fo
6d8b0 72 20 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b 32  r addrOpenTran[2
6d8c0 5d 20 63 6f 6e 74 61 69 6e 73 20 63 6f 6c 6c 61  ] contains colla
6d8d0 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65  ting.** sequence
6d8e0 73 20 66 6f 72 20 74 68 65 20 4f 52 44 45 52 20  s for the ORDER 
6d8f0 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73 74  BY clause..*/.st
6d900 72 75 63 74 20 53 65 6c 65 63 74 20 7b 0a 20 20  ruct Select {.  
6d910 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
6d920 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69  ;      /* The fi
6d930 65 6c 64 73 20 6f 66 20 74 68 65 20 72 65 73 75  elds of the resu
6d940 6c 74 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20  lt */.  u8 op;  
6d950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6d960 2a 20 4f 6e 65 20 6f 66 3a 20 54 4b 5f 55 4e 49  * One of: TK_UNI
6d970 4f 4e 20 54 4b 5f 41 4c 4c 20 54 4b 5f 49 4e 54  ON TK_ALL TK_INT
6d980 45 52 53 45 43 54 20 54 4b 5f 45 58 43 45 50 54  ERSECT TK_EXCEPT
6d990 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e   */.  char affin
6d9a0 69 74 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ity;         /* 
6d9b0 4d 61 6b 65 52 65 63 6f 72 64 20 77 69 74 68 20  MakeRecord with 
6d9c0 74 68 69 73 20 61 66 66 69 6e 69 74 79 20 66 6f  this affinity fo
6d9d0 72 20 53 52 54 5f 53 65 74 20 2a 2f 0a 20 20 75  r SRT_Set */.  u
6d9e0 31 36 20 73 65 6c 46 6c 61 67 73 3b 20 20 20 20  16 selFlags;    
6d9f0 20 20 20 20 20 20 2f 2a 20 56 61 72 69 6f 75 73        /* Various
6da00 20 53 46 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a   SF_* values */.
6da10 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b    SrcList *pSrc;
6da20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
6da30 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20  FROM clause */. 
6da40 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20   Expr *pWhere;  
6da50 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57          /* The W
6da60 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
6da70 20 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f 75   ExprList *pGrou
6da80 70 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 47  pBy;    /* The G
6da90 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a  ROUP BY clause *
6daa0 2f 0a 20 20 45 78 70 72 20 2a 70 48 61 76 69 6e  /.  Expr *pHavin
6dab0 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  g;         /* Th
6dac0 65 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 20  e HAVING clause 
6dad0 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
6dae0 4f 72 64 65 72 42 79 3b 20 20 20 20 2f 2a 20 54  OrderBy;    /* T
6daf0 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
6db00 73 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  se */.  Select *
6db10 70 50 72 69 6f 72 3b 20 20 20 20 20 20 20 20 2f  pPrior;        /
6db20 2a 20 50 72 69 6f 72 20 73 65 6c 65 63 74 20 69  * Prior select i
6db30 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c  n a compound sel
6db40 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  ect statement */
6db50 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 78 74  .  Select *pNext
6db60 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78  ;         /* Nex
6db70 74 20 73 65 6c 65 63 74 20 74 6f 20 74 68 65 20  t select to the 
6db80 6c 65 66 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75  left in a compou
6db90 6e 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  nd */.  Select *
6dba0 70 52 69 67 68 74 6d 6f 73 74 3b 20 20 20 20 2f  pRightmost;    /
6dbb0 2a 20 52 69 67 68 74 2d 6d 6f 73 74 20 73 65 6c  * Right-most sel
6dbc0 65 63 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e  ect in a compoun
6dbd0 64 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65  d select stateme
6dbe0 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c  nt */.  Expr *pL
6dbf0 69 6d 69 74 3b 20 20 20 20 20 20 20 20 20 20 2f  imit;          /
6dc00 2a 20 4c 49 4d 49 54 20 65 78 70 72 65 73 73 69  * LIMIT expressi
6dc10 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e  on. NULL means n
6dc20 6f 74 20 75 73 65 64 2e 20 2a 2f 0a 20 20 45 78  ot used. */.  Ex
6dc30 70 72 20 2a 70 4f 66 66 73 65 74 3b 20 20 20 20  pr *pOffset;    
6dc40 20 20 20 20 20 2f 2a 20 4f 46 46 53 45 54 20 65       /* OFFSET e
6dc50 78 70 72 65 73 73 69 6f 6e 2e 20 4e 55 4c 4c 20  xpression. NULL 
6dc60 6d 65 61 6e 73 20 6e 6f 74 20 75 73 65 64 2e 20  means not used. 
6dc70 2a 2f 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 2c  */.  int iLimit,
6dc80 20 69 4f 66 66 73 65 74 3b 20 20 20 2f 2a 20 4d   iOffset;   /* M
6dc90 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73 20  emory registers 
6dca0 68 6f 6c 64 69 6e 67 20 4c 49 4d 49 54 20 26 20  holding LIMIT & 
6dcb0 4f 46 46 53 45 54 20 63 6f 75 6e 74 65 72 73 20  OFFSET counters 
6dcc0 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4f 70 65  */.  int addrOpe
6dcd0 6e 45 70 68 6d 5b 33 5d 3b 20 20 20 2f 2a 20 4f  nEphm[3];   /* O
6dce0 50 5f 4f 70 65 6e 45 70 68 65 6d 20 6f 70 63 6f  P_OpenEphem opco
6dcf0 64 65 73 20 72 65 6c 61 74 65 64 20 74 6f 20 74  des related to t
6dd00 68 69 73 20 73 65 6c 65 63 74 20 2a 2f 0a 7d 3b  his select */.};
6dd10 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20  ../*.** Allowed 
6dd20 76 61 6c 75 65 73 20 66 6f 72 20 53 65 6c 65 63  values for Selec
6dd30 74 2e 73 65 6c 46 6c 61 67 73 2e 20 20 54 68 65  t.selFlags.  The
6dd40 20 22 53 46 22 20 70 72 65 66 69 78 20 73 74 61   "SF" prefix sta
6dd50 6e 64 73 20 66 6f 72 0a 2a 2a 20 22 53 65 6c 65  nds for.** "Sele
6dd60 63 74 20 46 6c 61 67 22 2e 0a 2a 2f 0a 23 64 65  ct Flag"..*/.#de
6dd70 66 69 6e 65 20 53 46 5f 44 69 73 74 69 6e 63 74  fine SF_Distinct
6dd80 20 20 20 20 20 20 20 20 30 78 30 30 30 31 20 20          0x0001  
6dd90 2f 2a 20 4f 75 74 70 75 74 20 73 68 6f 75 6c 64  /* Output should
6dda0 20 62 65 20 44 49 53 54 49 4e 43 54 20 2a 2f 0a   be DISTINCT */.
6ddb0 23 64 65 66 69 6e 65 20 53 46 5f 52 65 73 6f 6c  #define SF_Resol
6ddc0 76 65 64 20 20 20 20 20 20 20 20 30 78 30 30 30  ved        0x000
6ddd0 32 20 20 2f 2a 20 49 64 65 6e 74 69 66 69 65 72  2  /* Identifier
6dde0 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f  s have been reso
6ddf0 6c 76 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  lved */.#define 
6de00 53 46 5f 41 67 67 72 65 67 61 74 65 20 20 20 20  SF_Aggregate    
6de10 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 43 6f     0x0004  /* Co
6de20 6e 74 61 69 6e 73 20 61 67 67 72 65 67 61 74 65  ntains aggregate
6de30 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 64   functions */.#d
6de40 65 66 69 6e 65 20 53 46 5f 55 73 65 73 45 70 68  efine SF_UsesEph
6de50 65 6d 65 72 61 6c 20 20 20 30 78 30 30 30 38 20  emeral   0x0008 
6de60 20 2f 2a 20 55 73 65 73 20 74 68 65 20 4f 70 65   /* Uses the Ope
6de70 6e 45 70 68 65 6d 65 72 61 6c 20 6f 70 63 6f 64  nEphemeral opcod
6de80 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f  e */.#define SF_
6de90 45 78 70 61 6e 64 65 64 20 20 20 20 20 20 20 20  Expanded        
6dea0 30 78 30 30 31 30 20 20 2f 2a 20 73 71 6c 69 74  0x0010  /* sqlit
6deb0 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29  e3SelectExpand()
6dec0 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 69 73 20   called on this 
6ded0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 48 61  */.#define SF_Ha
6dee0 73 54 79 70 65 49 6e 66 6f 20 20 20 20 20 30 78  sTypeInfo     0x
6def0 30 30 32 30 20 20 2f 2a 20 46 52 4f 4d 20 73 75  0020  /* FROM su
6df00 62 71 75 65 72 69 65 73 20 68 61 76 65 20 54 61  bqueries have Ta
6df10 62 6c 65 20 6d 65 74 61 64 61 74 61 20 2a 2f 0a  ble metadata */.
6df20 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75  ../*.** The resu
6df30 6c 74 73 20 6f 66 20 61 20 73 65 6c 65 63 74 20  lts of a select 
6df40 63 61 6e 20 62 65 20 64 69 73 74 72 69 62 75 74  can be distribut
6df50 65 64 20 69 6e 20 73 65 76 65 72 61 6c 20 77 61  ed in several wa
6df60 79 73 2e 20 20 54 68 65 0a 2a 2a 20 22 53 52 54  ys.  The.** "SRT
6df70 22 20 70 72 65 66 69 78 20 6d 65 61 6e 73 20 22  " prefix means "
6df80 53 45 4c 45 43 54 20 52 65 73 75 6c 74 20 54 79  SELECT Result Ty
6df90 70 65 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  pe"..*/.#define 
6dfa0 53 52 54 5f 55 6e 69 6f 6e 20 20 20 20 20 20 20  SRT_Union       
6dfb0 20 31 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73   1  /* Store res
6dfc0 75 6c 74 20 61 73 20 6b 65 79 73 20 69 6e 20 61  ult as keys in a
6dfd0 6e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69  n index */.#defi
6dfe0 6e 65 20 53 52 54 5f 45 78 63 65 70 74 20 20 20  ne SRT_Except   
6dff0 20 20 20 20 32 20 20 2f 2a 20 52 65 6d 6f 76 65      2  /* Remove
6e000 20 72 65 73 75 6c 74 20 66 72 6f 6d 20 61 20 55   result from a U
6e010 4e 49 4f 4e 20 69 6e 64 65 78 20 2a 2f 0a 23 64  NION index */.#d
6e020 65 66 69 6e 65 20 53 52 54 5f 45 78 69 73 74 73  efine SRT_Exists
6e030 20 20 20 20 20 20 20 33 20 20 2f 2a 20 53 74 6f         3  /* Sto
6e040 72 65 20 31 20 69 66 20 74 68 65 20 72 65 73 75  re 1 if the resu
6e050 6c 74 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 20  lt is not empty 
6e060 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 44  */.#define SRT_D
6e070 69 73 63 61 72 64 20 20 20 20 20 20 34 20 20 2f  iscard      4  /
6e080 2a 20 44 6f 20 6e 6f 74 20 73 61 76 65 20 74 68  * Do not save th
6e090 65 20 72 65 73 75 6c 74 73 20 61 6e 79 77 68 65  e results anywhe
6e0a0 72 65 20 2a 2f 0a 0a 2f 2a 20 54 68 65 20 4f 52  re */../* The OR
6e0b0 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 73  DER BY clause is
6e0c0 20 69 67 6e 6f 72 65 64 20 66 6f 72 20 61 6c 6c   ignored for all
6e0d0 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 2a 2f   of the above */
6e0e0 0a 23 64 65 66 69 6e 65 20 49 67 6e 6f 72 61 62  .#define Ignorab
6e0f0 6c 65 4f 72 64 65 72 62 79 28 58 29 20 28 28 58  leOrderby(X) ((X
6e100 2d 3e 65 44 65 73 74 29 3c 3d 53 52 54 5f 44 69  ->eDest)<=SRT_Di
6e110 73 63 61 72 64 29 0a 0a 23 64 65 66 69 6e 65 20  scard)..#define 
6e120 53 52 54 5f 4f 75 74 70 75 74 20 20 20 20 20 20  SRT_Output      
6e130 20 35 20 20 2f 2a 20 4f 75 74 70 75 74 20 65 61   5  /* Output ea
6e140 63 68 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74  ch row of result
6e150 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f   */.#define SRT_
6e160 4d 65 6d 20 20 20 20 20 20 20 20 20 20 36 20 20  Mem          6  
6e170 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20  /* Store result 
6e180 69 6e 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  in a memory cell
6e190 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f   */.#define SRT_
6e1a0 53 65 74 20 20 20 20 20 20 20 20 20 20 37 20 20  Set          7  
6e1b0 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 73  /* Store results
6e1c0 20 61 73 20 6b 65 79 73 20 69 6e 20 61 6e 20 69   as keys in an i
6e1d0 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ndex */.#define 
6e1e0 53 52 54 5f 54 61 62 6c 65 20 20 20 20 20 20 20  SRT_Table       
6e1f0 20 38 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73   8  /* Store res
6e200 75 6c 74 20 61 73 20 64 61 74 61 20 77 69 74 68  ult as data with
6e210 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f   an automatic ro
6e220 77 69 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  wid */.#define S
6e230 52 54 5f 45 70 68 65 6d 54 61 62 20 20 20 20 20  RT_EphemTab     
6e240 39 20 20 2f 2a 20 43 72 65 61 74 65 20 74 72 61  9  /* Create tra
6e250 6e 73 69 65 6e 74 20 74 61 62 20 61 6e 64 20 73  nsient tab and s
6e260 74 6f 72 65 20 6c 69 6b 65 20 53 52 54 5f 54 61  tore like SRT_Ta
6e270 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ble */.#define S
6e280 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 20 20 31  RT_Coroutine   1
6e290 30 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61  0  /* Generate a
6e2a0 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 72   single row of r
6e2b0 65 73 75 6c 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  esult */../*.** 
6e2c0 41 20 73 74 72 75 63 74 75 72 65 20 75 73 65 64  A structure used
6e2d0 20 74 6f 20 63 75 73 74 6f 6d 69 7a 65 20 74 68   to customize th
6e2e0 65 20 62 65 68 61 76 69 6f 75 72 20 6f 66 20 73  e behaviour of s
6e2f0 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29 2e 20  qlite3Select(). 
6e300 53 65 65 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20  See.** comments 
6e310 61 62 6f 76 65 20 73 71 6c 69 74 65 33 53 65 6c  above sqlite3Sel
6e320 65 63 74 28 29 20 66 6f 72 20 64 65 74 61 69 6c  ect() for detail
6e330 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  s..*/.typedef st
6e340 72 75 63 74 20 53 65 6c 65 63 74 44 65 73 74 20  ruct SelectDest 
6e350 53 65 6c 65 63 74 44 65 73 74 3b 0a 73 74 72 75  SelectDest;.stru
6e360 63 74 20 53 65 6c 65 63 74 44 65 73 74 20 7b 0a  ct SelectDest {.
6e370 20 20 75 38 20 65 44 65 73 74 3b 20 20 20 20 20    u8 eDest;     
6e380 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69      /* How to di
6e390 73 70 6f 73 65 20 6f 66 20 74 68 65 20 72 65 73  spose of the res
6e3a0 75 6c 74 73 20 2a 2f 0a 20 20 75 38 20 61 66 66  ults */.  u8 aff
6e3b0 69 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a 20 41  inity;      /* A
6e3c0 66 66 69 6e 69 74 79 20 75 73 65 64 20 77 68 65  ffinity used whe
6e3d0 6e 20 65 44 65 73 74 3d 3d 53 52 54 5f 53 65 74  n eDest==SRT_Set
6e3e0 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 6d 3b   */.  int iParm;
6e3f0 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70 61 72          /* A par
6e400 61 6d 65 74 65 72 20 75 73 65 64 20 62 79 20 74  ameter used by t
6e410 68 65 20 65 44 65 73 74 20 64 69 73 70 6f 73 61  he eDest disposa
6e420 6c 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 69 6e  l method */.  in
6e430 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 20  t iMem;         
6e440 2f 2a 20 42 61 73 65 20 72 65 67 69 73 74 65 72  /* Base register
6e450 20 77 68 65 72 65 20 72 65 73 75 6c 74 73 20 61   where results a
6e460 72 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20  re written */.  
6e470 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20  int nMem;       
6e480 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
6e490 65 67 69 73 74 65 72 73 20 61 6c 6c 6f 63 61 74  egisters allocat
6e4a0 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ed */.};../*.** 
6e4b0 41 6e 20 53 51 4c 20 70 61 72 73 65 72 20 63 6f  An SQL parser co
6e4c0 6e 74 65 78 74 2e 20 20 41 20 63 6f 70 79 20 6f  ntext.  A copy o
6e4d0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
6e4e0 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f 75   is passed throu
6e4f0 67 68 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 72  gh.** the parser
6e500 20 61 6e 64 20 64 6f 77 6e 20 69 6e 74 6f 20 61   and down into a
6e510 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20 61 63  ll the parser ac
6e520 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69 6e 20  tion routine in 
6e530 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 61 72 72  order to.** carr
6e540 79 20 61 72 6f 75 6e 64 20 69 6e 66 6f 72 6d 61  y around informa
6e550 74 69 6f 6e 20 74 68 61 74 20 69 73 20 67 6c 6f  tion that is glo
6e560 62 61 6c 20 74 6f 20 74 68 65 20 65 6e 74 69 72  bal to the entir
6e570 65 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54  e parse..**.** T
6e580 68 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20  he structure is 
6e590 64 69 76 69 64 65 64 20 69 6e 74 6f 20 74 77 6f  divided into two
6e5a0 20 70 61 72 74 73 2e 20 20 57 68 65 6e 20 74 68   parts.  When th
6e5b0 65 20 70 61 72 73 65 72 20 61 6e 64 20 63 6f 64  e parser and cod
6e5c0 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 20 63 61  e.** generate ca
6e5d0 6c 6c 20 74 68 65 6d 73 65 6c 76 65 73 20 72 65  ll themselves re
6e5e0 63 75 72 73 69 76 65 6c 79 2c 20 74 68 65 20 66  cursively, the f
6e5f0 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 68 65  irst part of the
6e600 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73   structure.** is
6e610 20 63 6f 6e 73 74 61 6e 74 20 62 75 74 20 74 68   constant but th
6e620 65 20 73 65 63 6f 6e 64 20 70 61 72 74 20 69 73  e second part is
6e630 20 72 65 73 65 74 20 61 74 20 74 68 65 20 62 65   reset at the be
6e640 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 20  ginning and end 
6e650 6f 66 0a 2a 2a 20 65 61 63 68 20 72 65 63 75 72  of.** each recur
6e660 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  sion..**.** The 
6e670 6e 54 61 62 6c 65 4c 6f 63 6b 20 61 6e 64 20 61  nTableLock and a
6e680 54 61 62 6c 65 4c 6f 63 6b 20 76 61 72 69 61 62  TableLock variab
6e690 6c 65 73 20 61 72 65 20 6f 6e 6c 79 20 75 73 65  les are only use
6e6a0 64 20 69 66 20 74 68 65 20 73 68 61 72 65 64 2d  d if the shared-
6e6b0 63 61 63 68 65 20 0a 2a 2a 20 66 65 61 74 75 72  cache .** featur
6e6c0 65 20 69 73 20 65 6e 61 62 6c 65 64 20 28 69 66  e is enabled (if
6e6d0 20 73 71 6c 69 74 65 33 54 73 64 28 29 2d 3e 75   sqlite3Tsd()->u
6e6e0 73 65 53 68 61 72 65 64 44 61 74 61 20 69 73 20  seSharedData is 
6e6f0 74 72 75 65 29 2e 20 54 68 65 79 20 61 72 65 0a  true). They are.
6e700 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  ** used to store
6e710 20 74 68 65 20 73 65 74 20 6f 66 20 74 61 62 6c   the set of tabl
6e720 65 2d 6c 6f 63 6b 73 20 72 65 71 75 69 72 65 64  e-locks required
6e730 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e   by the statemen
6e740 74 20 62 65 69 6e 67 0a 2a 2a 20 63 6f 6d 70 69  t being.** compi
6e750 6c 65 64 2e 20 46 75 6e 63 74 69 6f 6e 20 73 71  led. Function sq
6e760 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29  lite3TableLock()
6e770 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20   is used to add 
6e780 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65 0a 2a  entries to the.*
6e790 2a 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63  * list..*/.struc
6e7a0 74 20 50 61 72 73 65 20 7b 0a 20 20 73 71 6c 69  t Parse {.  sqli
6e7b0 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
6e7c0 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 61 74   /* The main dat
6e7d0 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 20  abase structure 
6e7e0 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
6e7f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
6e800 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 65 78  urn code from ex
6e810 65 63 75 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61  ecution */.  cha
6e820 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20  r *zErrMsg;     
6e830 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 6d 65    /* An error me
6e840 73 73 61 67 65 20 2a 2f 0a 20 20 56 64 62 65 20  ssage */.  Vdbe 
6e850 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20 20 20  *pVdbe;         
6e860 2f 2a 20 41 6e 20 65 6e 67 69 6e 65 20 66 6f 72  /* An engine for
6e870 20 65 78 65 63 75 74 69 6e 67 20 64 61 74 61 62   executing datab
6e880 61 73 65 20 62 79 74 65 63 6f 64 65 20 2a 2f 0a  ase bytecode */.
6e890 20 20 75 38 20 63 6f 6c 4e 61 6d 65 73 53 65 74    u8 colNamesSet
6e8a0 3b 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 61  ;      /* TRUE a
6e8b0 66 74 65 72 20 4f 50 5f 43 6f 6c 75 6d 6e 4e 61  fter OP_ColumnNa
6e8c0 6d 65 20 68 61 73 20 62 65 65 6e 20 69 73 73 75  me has been issu
6e8d0 65 64 20 74 6f 20 70 56 64 62 65 20 2a 2f 0a 20  ed to pVdbe */. 
6e8e0 20 75 38 20 6e 61 6d 65 43 6c 61 73 68 3b 20 20   u8 nameClash;  
6e8f0 20 20 20 20 20 20 2f 2a 20 41 20 70 65 72 6d 61        /* A perma
6e900 6e 65 6e 74 20 74 61 62 6c 65 20 6e 61 6d 65 20  nent table name 
6e910 63 6c 61 73 68 65 73 20 77 69 74 68 20 74 65 6d  clashes with tem
6e920 70 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a  p table name */.
6e930 20 20 75 38 20 63 68 65 63 6b 53 63 68 65 6d 61    u8 checkSchema
6e940 3b 20 20 20 20 20 20 2f 2a 20 43 61 75 73 65 73  ;      /* Causes
6e950 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63   schema cookie c
6e960 68 65 63 6b 20 61 66 74 65 72 20 61 6e 20 65 72  heck after an er
6e970 72 6f 72 20 2a 2f 0a 20 20 75 38 20 6e 65 73 74  ror */.  u8 nest
6e980 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ed;           /*
6e990 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65   Number of neste
6e9a0 64 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 70  d calls to the p
6e9b0 61 72 73 65 72 2f 63 6f 64 65 20 67 65 6e 65 72  arser/code gener
6e9c0 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20 70 61 72  ator */.  u8 par
6e9d0 73 65 45 72 72 6f 72 3b 20 20 20 20 20 20 20 2f  seError;       /
6e9e0 2a 20 54 72 75 65 20 61 66 74 65 72 20 61 20 70  * True after a p
6e9f0 61 72 73 69 6e 67 20 65 72 72 6f 72 2e 20 20 54  arsing error.  T
6ea00 69 63 6b 65 74 20 23 31 37 39 34 20 2a 2f 0a 20  icket #1794 */. 
6ea10 20 75 38 20 6e 54 65 6d 70 52 65 67 3b 20 20 20   u8 nTempReg;   
6ea20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
6ea30 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67  of temporary reg
6ea40 69 73 74 65 72 73 20 69 6e 20 61 54 65 6d 70 52  isters in aTempR
6ea50 65 67 5b 5d 20 2a 2f 0a 20 20 75 38 20 6e 54 65  eg[] */.  u8 nTe
6ea60 6d 70 49 6e 55 73 65 3b 20 20 20 20 20 20 20 2f  mpInUse;       /
6ea70 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 54 65 6d  * Number of aTem
6ea80 70 52 65 67 5b 5d 20 63 75 72 72 65 6e 74 6c 79  pReg[] currently
6ea90 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a   checked out */.
6eaa0 20 20 69 6e 74 20 61 54 65 6d 70 52 65 67 5b 38    int aTempReg[8
6eab0 5d 3b 20 20 20 20 20 2f 2a 20 48 6f 6c 64 69 6e  ];     /* Holdin
6eac0 67 20 61 72 65 61 20 66 6f 72 20 74 65 6d 70 6f  g area for tempo
6ead0 72 61 72 79 20 72 65 67 69 73 74 65 72 73 20 2a  rary registers *
6eae0 2f 0a 20 20 69 6e 74 20 6e 52 61 6e 67 65 52 65  /.  int nRangeRe
6eaf0 67 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65  g;       /* Size
6eb00 20 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 61 72   of the temporar
6eb10 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63 6b  y register block
6eb20 20 2a 2f 0a 20 20 69 6e 74 20 69 52 61 6e 67 65   */.  int iRange
6eb30 52 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 46 69  Reg;       /* Fi
6eb40 72 73 74 20 72 65 67 69 73 74 65 72 20 69 6e 20  rst register in 
6eb50 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74  temporary regist
6eb60 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e  er block */.  in
6eb70 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20  t nErr;         
6eb80 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
6eb90 65 72 72 6f 72 73 20 73 65 65 6e 20 2a 2f 0a 20  errors seen */. 
6eba0 20 69 6e 74 20 6e 54 61 62 3b 20 20 20 20 20 20   int nTab;      
6ebb0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
6ebc0 6f 66 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c  of previously al
6ebd0 6c 6f 63 61 74 65 64 20 56 44 42 45 20 63 75 72  located VDBE cur
6ebe0 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d  sors */.  int nM
6ebf0 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  em;            /
6ec00 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f  * Number of memo
6ec10 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20 73 6f  ry cells used so
6ec20 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53   far */.  int nS
6ec30 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  et;            /
6ec40 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 65 74 73  * Number of sets
6ec50 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a   used so far */.
6ec60 20 20 69 6e 74 20 63 6b 42 61 73 65 3b 20 20 20    int ckBase;   
6ec70 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72         /* Base r
6ec80 65 67 69 73 74 65 72 20 6f 66 20 64 61 74 61 20  egister of data 
6ec90 64 75 72 69 6e 67 20 63 68 65 63 6b 20 63 6f 6e  during check con
6eca0 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69 6e  straints */.  in
6ecb0 74 20 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68  t disableColCach
6ecc0 65 3b 20 2f 2a 20 54 72 75 65 20 74 6f 20 64 69  e; /* True to di
6ecd0 73 61 62 6c 65 20 61 64 64 69 6e 67 20 74 6f 20  sable adding to 
6ece0 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 2a 2f 0a  column cache */.
6ecf0 20 20 69 6e 74 20 6e 43 6f 6c 43 61 63 68 65 3b    int nColCache;
6ed00 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
6ed10 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74   of entries in t
6ed20 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20  he column cache 
6ed30 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 43 61 63  */.  int iColCac
6ed40 68 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 65 78  he;       /* Nex
6ed50 74 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 63  t entry of the c
6ed60 61 63 68 65 20 74 6f 20 72 65 70 6c 61 63 65 20  ache to replace 
6ed70 2a 2f 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c  */.  struct yCol
6ed80 43 61 63 68 65 20 7b 0a 20 20 20 20 69 6e 74 20  Cache {.    int 
6ed90 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20  iTable;         
6eda0 20 20 2f 2a 20 54 61 62 6c 65 20 63 75 72 73 6f    /* Table curso
6edb0 72 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20  r number */.    
6edc0 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20  int iColumn;    
6edd0 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63        /* Table c
6ede0 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a  olumn number */.
6edf0 20 20 20 20 63 68 61 72 20 61 66 66 43 68 61 6e      char affChan
6ee00 67 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ge;       /* Tru
6ee10 65 20 69 66 20 74 68 69 73 20 72 65 67 69 73 74  e if this regist
6ee20 65 72 20 68 61 73 20 68 61 64 20 61 6e 20 61 66  er has had an af
6ee30 66 69 6e 69 74 79 20 63 68 61 6e 67 65 20 2a 2f  finity change */
6ee40 0a 20 20 20 20 69 6e 74 20 69 52 65 67 3b 20 20  .    int iReg;  
6ee50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
6ee60 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 76  gister holding v
6ee70 61 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c  alue of this col
6ee80 75 6d 6e 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c 43  umn */.  } aColC
6ee90 61 63 68 65 5b 31 30 5d 3b 20 20 20 20 20 2f 2a  ache[10];     /*
6eea0 20 4f 6e 65 20 66 6f 72 20 65 61 63 68 20 76 61   One for each va
6eeb0 6c 69 64 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65  lid column cache
6eec0 20 65 6e 74 72 79 20 2a 2f 0a 20 20 75 33 32 20   entry */.  u32 
6eed0 77 72 69 74 65 4d 61 73 6b 3b 20 20 20 20 20 20  writeMask;      
6eee0 20 2f 2a 20 53 74 61 72 74 20 61 20 77 72 69 74   /* Start a writ
6eef0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e  e transaction on
6ef00 20 74 68 65 73 65 20 64 61 74 61 62 61 73 65 73   these databases
6ef10 20 2a 2f 0a 20 20 75 33 32 20 63 6f 6f 6b 69 65   */.  u32 cookie
6ef20 4d 61 73 6b 3b 20 20 20 20 20 20 2f 2a 20 42 69  Mask;      /* Bi
6ef30 74 6d 61 73 6b 20 6f 66 20 73 63 68 65 6d 61 20  tmask of schema 
6ef40 76 65 72 69 66 69 65 64 20 64 61 74 61 62 61 73  verified databas
6ef50 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b  es */.  int cook
6ef60 69 65 47 6f 74 6f 3b 20 20 20 20 20 20 2f 2a 20  ieGoto;      /* 
6ef70 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 47 6f  Address of OP_Go
6ef80 74 6f 20 74 6f 20 63 6f 6f 6b 69 65 20 76 65 72  to to cookie ver
6ef90 69 66 69 65 72 20 73 75 62 72 6f 75 74 69 6e 65  ifier subroutine
6efa0 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b 69 65   */.  int cookie
6efb0 56 61 6c 75 65 5b 53 51 4c 49 54 45 5f 4d 41 58  Value[SQLITE_MAX
6efc0 5f 41 54 54 41 43 48 45 44 2b 32 5d 3b 20 20 2f  _ATTACHED+2];  /
6efd0 2a 20 56 61 6c 75 65 73 20 6f 66 20 63 6f 6f 6b  * Values of cook
6efe0 69 65 73 20 74 6f 20 76 65 72 69 66 79 20 2a 2f  ies to verify */
6eff0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
6f000 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
6f010 45 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65 4c 6f  E.  int nTableLo
6f020 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ck;        /* Nu
6f030 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 69 6e  mber of locks in
6f040 20 61 54 61 62 6c 65 4c 6f 63 6b 20 2a 2f 0a 20   aTableLock */. 
6f050 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 61 54 61 62   TableLock *aTab
6f060 6c 65 4c 6f 63 6b 3b 20 2f 2a 20 52 65 71 75 69  leLock; /* Requi
6f070 72 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20  red table locks 
6f080 66 6f 72 20 73 68 61 72 65 64 2d 63 61 63 68 65  for shared-cache
6f090 20 6d 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a   mode */.#endif.
6f0a0 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 20    int regRowid; 
6f0b0 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
6f0c0 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69 64  er holding rowid
6f0d0 20 6f 66 20 43 52 45 41 54 45 20 54 41 42 4c 45   of CREATE TABLE
6f0e0 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20   entry */.  int 
6f0f0 72 65 67 52 6f 6f 74 3b 20 20 20 20 20 20 20 20  regRoot;        
6f100 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
6f110 64 69 6e 67 20 72 6f 6f 74 20 70 61 67 65 20 6e  ding root page n
6f120 75 6d 62 65 72 20 66 6f 72 20 6e 65 77 20 6f 62  umber for new ob
6f130 6a 65 63 74 73 20 2a 2f 0a 0a 20 20 2f 2a 20 41  jects */..  /* A
6f140 62 6f 76 65 20 69 73 20 63 6f 6e 73 74 61 6e 74  bove is constant
6f150 20 62 65 74 77 65 65 6e 20 72 65 63 75 72 73 69   between recursi
6f160 6f 6e 73 2e 20 20 42 65 6c 6f 77 20 69 73 20 72  ons.  Below is r
6f170 65 73 65 74 20 62 65 66 6f 72 65 20 61 6e 64 20  eset before and 
6f180 61 66 74 65 72 0a 20 20 2a 2a 20 65 61 63 68 20  after.  ** each 
6f190 72 65 63 75 72 73 69 6f 6e 20 2a 2f 0a 0a 20 20  recursion */..  
6f1a0 69 6e 74 20 6e 56 61 72 3b 20 20 20 20 20 20 20  int nVar;       
6f1b0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
6f1c0 66 20 27 3f 27 20 76 61 72 69 61 62 6c 65 73 20  f '?' variables 
6f1d0 73 65 65 6e 20 69 6e 20 74 68 65 20 53 51 4c 20  seen in the SQL 
6f1e0 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20  so far */.  int 
6f1f0 6e 56 61 72 45 78 70 72 3b 20 20 20 20 20 20 20  nVarExpr;       
6f200 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73   /* Number of us
6f210 65 64 20 73 6c 6f 74 73 20 69 6e 20 61 70 56 61  ed slots in apVa
6f220 72 45 78 70 72 5b 5d 20 2a 2f 0a 20 20 69 6e 74  rExpr[] */.  int
6f230 20 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 3b 20   nVarExprAlloc; 
6f240 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
6f250 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 69  llocated slots i
6f260 6e 20 61 70 56 61 72 45 78 70 72 5b 5d 20 2a 2f  n apVarExpr[] */
6f270 0a 20 20 45 78 70 72 20 2a 2a 61 70 56 61 72 45  .  Expr **apVarE
6f280 78 70 72 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74  xpr;    /* Point
6f290 65 72 73 20 74 6f 20 3a 61 61 61 20 61 6e 64 20  ers to :aaa and 
6f2a0 24 61 61 61 61 20 77 69 6c 64 63 61 72 64 20 65  $aaaa wildcard e
6f2b0 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20  xpressions */.  
6f2c0 69 6e 74 20 6e 41 6c 69 61 73 3b 20 20 20 20 20  int nAlias;     
6f2d0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
6f2e0 66 20 61 6c 69 61 73 65 64 20 72 65 73 75 6c 74  f aliased result
6f2f0 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a   set columns */.
6f300 20 20 69 6e 74 20 2a 61 41 6c 69 61 73 3b 20 20    int *aAlias;  
6f310 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
6f320 65 72 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20  er used to hold 
6f330 61 6c 69 61 73 65 64 20 72 65 73 75 6c 74 20 2a  aliased result *
6f340 2f 0a 20 20 75 38 20 65 78 70 6c 61 69 6e 3b 20  /.  u8 explain; 
6f350 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
6f360 20 69 66 20 74 68 65 20 45 58 50 4c 41 49 4e 20   if the EXPLAIN 
6f370 66 6c 61 67 20 69 73 20 66 6f 75 6e 64 20 6f 6e  flag is found on
6f380 20 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20   the query */.  
6f390 54 6f 6b 65 6e 20 73 45 72 72 54 6f 6b 65 6e 3b  Token sErrToken;
6f3a0 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65       /* The toke
6f3b0 6e 20 61 74 20 77 68 69 63 68 20 74 68 65 20 65  n at which the e
6f3c0 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20 2a 2f  rror occurred */
6f3d0 0a 20 20 54 6f 6b 65 6e 20 73 4e 61 6d 65 54 6f  .  Token sNameTo
6f3e0 6b 65 6e 3b 20 20 20 20 2f 2a 20 54 6f 6b 65 6e  ken;    /* Token
6f3f0 20 77 69 74 68 20 75 6e 71 75 61 6c 69 66 69 65   with unqualifie
6f400 64 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 20  d schema object 
6f410 6e 61 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  name */.  Token 
6f420 73 4c 61 73 74 54 6f 6b 65 6e 3b 20 20 20 20 2f  sLastToken;    /
6f430 2a 20 54 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e  * The last token
6f440 20 70 61 72 73 65 64 20 2a 2f 0a 20 20 63 6f 6e   parsed */.  con
6f450 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20 20  st char *zSql;  
6f460 20 20 2f 2a 20 41 6c 6c 20 53 51 4c 20 74 65 78    /* All SQL tex
6f470 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  t */.  const cha
6f480 72 20 2a 7a 54 61 69 6c 3b 20 20 20 2f 2a 20 41  r *zTail;   /* A
6f490 6c 6c 20 53 51 4c 20 74 65 78 74 20 70 61 73 74  ll SQL text past
6f4a0 20 74 68 65 20 6c 61 73 74 20 73 65 6d 69 63 6f   the last semico
6f4b0 6c 6f 6e 20 70 61 72 73 65 64 20 2a 2f 0a 20 20  lon parsed */.  
6f4c0 54 61 62 6c 65 20 2a 70 4e 65 77 54 61 62 6c 65  Table *pNewTable
6f4d0 3b 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20  ;    /* A table 
6f4e0 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65  being constructe
6f4f0 64 20 62 79 20 43 52 45 41 54 45 20 54 41 42 4c  d by CREATE TABL
6f500 45 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a  E */.  Trigger *
6f510 70 4e 65 77 54 72 69 67 67 65 72 3b 20 20 20 20  pNewTrigger;    
6f520 20 2f 2a 20 54 72 69 67 67 65 72 20 75 6e 64 65   /* Trigger unde
6f530 72 20 63 6f 6e 73 74 72 75 63 74 20 62 79 20 61  r construct by a
6f540 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20   CREATE TRIGGER 
6f550 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 61 63  */.  TriggerStac
6f560 6b 20 2a 74 72 69 67 53 74 61 63 6b 3b 20 20 2f  k *trigStack;  /
6f570 2a 20 54 72 69 67 67 65 72 20 61 63 74 69 6f 6e  * Trigger action
6f580 73 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f  s being coded */
6f590 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
6f5a0 41 75 74 68 43 6f 6e 74 65 78 74 3b 20 2f 2a 20  AuthContext; /* 
6f5b0 54 68 65 20 36 74 68 20 70 61 72 61 6d 65 74 65  The 6th paramete
6f5c0 72 20 74 6f 20 64 62 2d 3e 78 41 75 74 68 20 63  r to db->xAuth c
6f5d0 61 6c 6c 62 61 63 6b 73 20 2a 2f 0a 23 69 66 6e  allbacks */.#ifn
6f5e0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
6f5f0 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 54  VIRTUALTABLE.  T
6f600 6f 6b 65 6e 20 73 41 72 67 3b 20 20 20 20 20 20  oken sArg;      
6f610 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d            /* Com
6f620 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 61 20  plete text of a 
6f630 6d 6f 64 75 6c 65 20 61 72 67 75 6d 65 6e 74 20  module argument 
6f640 2a 2f 0a 20 20 75 38 20 64 65 63 6c 61 72 65 56  */.  u8 declareV
6f650 74 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  tab;            
6f660 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 73 69 64  /* True if insid
6f670 65 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72  e sqlite3_declar
6f680 65 5f 76 74 61 62 28 29 20 2a 2f 0a 20 20 69 6e  e_vtab() */.  in
6f690 74 20 6e 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20  t nVtabLock;    
6f6a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
6f6b0 65 72 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61  er of virtual ta
6f6c0 62 6c 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a  bles to lock */.
6f6d0 20 20 54 61 62 6c 65 20 2a 2a 61 70 56 74 61 62    Table **apVtab
6f6e0 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20  Lock;        /* 
6f6f0 50 6f 69 6e 74 65 72 20 74 6f 20 76 69 72 74 75  Pointer to virtu
6f700 61 6c 20 74 61 62 6c 65 73 20 6e 65 65 64 69 6e  al tables needin
6f710 67 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 23 65 6e  g locking */.#en
6f720 64 69 66 0a 20 20 69 6e 74 20 6e 48 65 69 67 68  dif.  int nHeigh
6f730 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
6f740 20 45 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65   Expression tree
6f750 20 68 65 69 67 68 74 20 6f 66 20 63 75 72 72 65   height of curre
6f760 6e 74 20 73 75 62 2d 73 65 6c 65 63 74 20 2a 2f  nt sub-select */
6f770 0a 20 20 54 61 62 6c 65 20 2a 70 5a 6f 6d 62 69  .  Table *pZombi
6f780 65 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 4c 69  eTab;      /* Li
6f790 73 74 20 6f 66 20 54 61 62 6c 65 20 6f 62 6a 65  st of Table obje
6f7a0 63 74 73 20 74 6f 20 64 65 6c 65 74 65 20 61 66  cts to delete af
6f7b0 74 65 72 20 63 6f 64 65 20 67 65 6e 20 2a 2f 0a  ter code gen */.
6f7c0 7d 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  };..#ifdef SQLIT
6f7d0 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
6f7e0 42 4c 45 0a 20 20 23 64 65 66 69 6e 65 20 49 4e  BLE.  #define IN
6f7f0 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 30 0a  _DECLARE_VTAB 0.
6f800 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20  #else.  #define 
6f810 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20  IN_DECLARE_VTAB 
6f820 28 70 50 61 72 73 65 2d 3e 64 65 63 6c 61 72 65  (pParse->declare
6f830 56 74 61 62 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  Vtab).#endif../*
6f840 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
6f850 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
6f860 20 73 74 72 75 63 74 75 72 65 20 63 61 6e 20 62   structure can b
6f870 65 20 64 65 63 6c 61 72 65 64 20 6f 6e 20 61 20  e declared on a 
6f880 73 74 61 63 6b 20 61 6e 64 20 75 73 65 64 0a 2a  stack and used.*
6f890 2a 20 74 6f 20 73 61 76 65 20 74 68 65 20 50 61  * to save the Pa
6f8a0 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74 65 78 74  rse.zAuthContext
6f8b0 20 76 61 6c 75 65 20 73 6f 20 74 68 61 74 20 69   value so that i
6f8c0 74 20 63 61 6e 20 62 65 20 72 65 73 74 6f 72 65  t can be restore
6f8d0 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 73 74 72 75  d later..*/.stru
6f8e0 63 74 20 41 75 74 68 43 6f 6e 74 65 78 74 20 7b  ct AuthContext {
6f8f0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
6f900 41 75 74 68 43 6f 6e 74 65 78 74 3b 20 20 20 2f  AuthContext;   /
6f910 2a 20 50 75 74 20 73 61 76 65 64 20 50 61 72 73  * Put saved Pars
6f920 65 2e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 68  e.zAuthContext h
6f930 65 72 65 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a  ere */.  Parse *
6f940 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20  pParse;         
6f950 20 20 20 20 20 2f 2a 20 54 68 65 20 50 61 72 73       /* The Pars
6f960 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 7d  e structure */.}
6f970 3b 0a 0a 2f 2a 0a 2a 2a 20 42 69 74 66 69 65 6c  ;../*.** Bitfiel
6f980 64 20 66 6c 61 67 73 20 66 6f 72 20 50 32 20 76  d flags for P2 v
6f990 61 6c 75 65 20 69 6e 20 4f 50 5f 49 6e 73 65 72  alue in OP_Inser
6f9a0 74 20 61 6e 64 20 4f 50 5f 44 65 6c 65 74 65 0a  t and OP_Delete.
6f9b0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41  */.#define OPFLA
6f9c0 47 5f 4e 43 48 41 4e 47 45 20 20 20 31 20 20 20  G_NCHANGE   1   
6f9d0 20 2f 2a 20 53 65 74 20 74 6f 20 75 70 64 61 74   /* Set to updat
6f9e0 65 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f  e db->nChange */
6f9f0 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f  .#define OPFLAG_
6fa00 4c 41 53 54 52 4f 57 49 44 20 32 20 20 20 20 2f  LASTROWID 2    /
6fa10 2a 20 53 65 74 20 74 6f 20 75 70 64 61 74 65 20  * Set to update 
6fa20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 2a 2f  db->lastRowid */
6fa30 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f  .#define OPFLAG_
6fa40 49 53 55 50 44 41 54 45 20 20 34 20 20 20 20 2f  ISUPDATE  4    /
6fa50 2a 20 54 68 69 73 20 4f 50 5f 49 6e 73 65 72 74  * This OP_Insert
6fa60 20 69 73 20 61 6e 20 73 71 6c 20 55 50 44 41 54   is an sql UPDAT
6fa70 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46  E */.#define OPF
6fa80 4c 41 47 5f 41 50 50 45 4e 44 20 20 20 20 38 20  LAG_APPEND    8 
6fa90 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 6c 69     /* This is li
6faa0 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70  kely to be an ap
6fab0 70 65 6e 64 20 2a 2f 0a 0a 2f 2a 0a 20 2a 20 45  pend */../*. * E
6fac0 61 63 68 20 74 72 69 67 67 65 72 20 70 72 65 73  ach trigger pres
6fad0 65 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 62  ent in the datab
6fae0 61 73 65 20 73 63 68 65 6d 61 20 69 73 20 73 74  ase schema is st
6faf0 6f 72 65 64 20 61 73 20 61 6e 20 69 6e 73 74 61  ored as an insta
6fb00 6e 63 65 20 6f 66 0a 20 2a 20 73 74 72 75 63 74  nce of. * struct
6fb10 20 54 72 69 67 67 65 72 2e 20 0a 20 2a 0a 20 2a   Trigger. . *. *
6fb20 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 69 6e 73   Pointers to ins
6fb30 74 61 6e 63 65 73 20 6f 66 20 73 74 72 75 63 74  tances of struct
6fb40 20 54 72 69 67 67 65 72 20 61 72 65 20 73 74 6f   Trigger are sto
6fb50 72 65 64 20 69 6e 20 74 77 6f 20 77 61 79 73 2e  red in two ways.
6fb60 0a 20 2a 20 31 2e 20 49 6e 20 74 68 65 20 22 74  . * 1. In the "t
6fb70 72 69 67 48 61 73 68 22 20 68 61 73 68 20 74 61  rigHash" hash ta
6fb80 62 6c 65 20 28 70 61 72 74 20 6f 66 20 74 68 65  ble (part of the
6fb90 20 73 71 6c 69 74 65 33 2a 20 74 68 61 74 20 72   sqlite3* that r
6fba0 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 0a 20  epresents the . 
6fbb0 2a 20 20 20 20 64 61 74 61 62 61 73 65 29 2e 20  *    database). 
6fbc0 54 68 69 73 20 61 6c 6c 6f 77 73 20 54 72 69 67  This allows Trig
6fbd0 67 65 72 20 73 74 72 75 63 74 75 72 65 73 20 74  ger structures t
6fbe0 6f 20 62 65 20 72 65 74 72 69 65 76 65 64 20 62  o be retrieved b
6fbf0 79 20 6e 61 6d 65 2e 0a 20 2a 20 32 2e 20 41 6c  y name.. * 2. Al
6fc00 6c 20 74 72 69 67 67 65 72 73 20 61 73 73 6f 63  l triggers assoc
6fc10 69 61 74 65 64 20 77 69 74 68 20 61 20 73 69 6e  iated with a sin
6fc20 67 6c 65 20 74 61 62 6c 65 20 66 6f 72 6d 20 61  gle table form a
6fc30 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2c 20 75 73   linked list, us
6fc40 69 6e 67 20 74 68 65 0a 20 2a 20 20 20 20 70 4e  ing the. *    pN
6fc50 65 78 74 20 6d 65 6d 62 65 72 20 6f 66 20 73 74  ext member of st
6fc60 72 75 63 74 20 54 72 69 67 67 65 72 2e 20 41 20  ruct Trigger. A 
6fc70 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66  pointer to the f
6fc80 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20  irst element of 
6fc90 74 68 65 0a 20 2a 20 20 20 20 6c 69 6e 6b 65 64  the. *    linked
6fca0 20 6c 69 73 74 20 69 73 20 73 74 6f 72 65 64 20   list is stored 
6fcb0 61 73 20 74 68 65 20 22 70 54 72 69 67 67 65 72  as the "pTrigger
6fcc0 22 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20  " member of the 
6fcd0 61 73 73 6f 63 69 61 74 65 64 0a 20 2a 20 20 20  associated. *   
6fce0 20 73 74 72 75 63 74 20 54 61 62 6c 65 2e 0a 20   struct Table.. 
6fcf0 2a 0a 20 2a 20 54 68 65 20 22 73 74 65 70 5f 6c  *. * The "step_l
6fd00 69 73 74 22 20 6d 65 6d 62 65 72 20 70 6f 69 6e  ist" member poin
6fd10 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  ts to the first 
6fd20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 6c 69 6e  element of a lin
6fd30 6b 65 64 20 6c 69 73 74 0a 20 2a 20 63 6f 6e 74  ked list. * cont
6fd40 61 69 6e 69 6e 67 20 74 68 65 20 53 51 4c 20 73  aining the SQL s
6fd50 74 61 74 65 6d 65 6e 74 73 20 73 70 65 63 69 66  tatements specif
6fd60 69 65 64 20 61 73 20 74 68 65 20 74 72 69 67 67  ied as the trigg
6fd70 65 72 20 70 72 6f 67 72 61 6d 2e 0a 20 2a 2f 0a  er program.. */.
6fd80 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20 7b  struct Trigger {
6fd90 0a 20 20 63 68 61 72 20 2a 6e 61 6d 65 3b 20 20  .  char *name;  
6fda0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
6fdb0 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 72  e name of the tr
6fdc0 69 67 67 65 72 20 20 20 20 20 20 20 20 20 20 20  igger           
6fdd0 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a               */.
6fde0 20 20 63 68 61 72 20 2a 74 61 62 6c 65 3b 20 20    char *table;  
6fdf0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
6fe00 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 74   table or view t
6fe10 6f 20 77 68 69 63 68 20 74 68 65 20 74 72 69 67  o which the trig
6fe20 67 65 72 20 61 70 70 6c 69 65 73 20 2a 2f 0a 20  ger applies */. 
6fe30 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20   u8 op;         
6fe40 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
6fe50 6f 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b  of TK_DELETE, TK
6fe60 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45  _UPDATE, TK_INSE
6fe70 52 54 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  RT         */.  
6fe80 75 38 20 74 72 5f 74 6d 3b 20 20 20 20 20 20 20  u8 tr_tm;       
6fe90 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
6fea0 66 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45  f TRIGGER_BEFORE
6feb0 2c 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 20  , TRIGGER_AFTER 
6fec0 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 6e  */.  Expr *pWhen
6fed0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
6fee0 54 68 65 20 57 48 45 4e 20 63 6c 61 75 73 65 20  The WHEN clause 
6fef0 6f 66 20 74 68 65 20 65 78 70 72 65 73 69 6f 6e  of the expresion
6ff00 20 28 6d 61 79 20 62 65 20 4e 55 4c 4c 29 20 2a   (may be NULL) *
6ff10 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c  /.  IdList *pCol
6ff20 75 6d 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20 49  umns;       /* I
6ff30 66 20 74 68 69 73 20 69 73 20 61 6e 20 55 50 44  f this is an UPD
6ff40 41 54 45 20 4f 46 20 3c 63 6f 6c 75 6d 6e 2d 6c  ATE OF <column-l
6ff50 69 73 74 3e 20 74 72 69 67 67 65 72 2c 0a 20 20  ist> trigger,.  
6ff60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6ff70 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 3c             the <
6ff80 63 6f 6c 75 6d 6e 2d 6c 69 73 74 3e 20 69 73 20  column-list> is 
6ff90 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20  stored here */. 
6ffa0 20 54 6f 6b 65 6e 20 6e 61 6d 65 54 6f 6b 65 6e   Token nameToken
6ffb0 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65  ;        /* Toke
6ffc0 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 7a 4e 61  n containing zNa
6ffd0 6d 65 2e 20 55 73 65 20 64 75 72 69 6e 67 20 70  me. Use during p
6ffe0 61 72 73 69 6e 67 20 6f 6e 6c 79 20 2a 2f 0a 20  arsing only */. 
6fff0 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61   Schema *pSchema
70000 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 63 68 65  ;        /* Sche
70010 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ma containing th
70020 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 53  e trigger */.  S
70030 63 68 65 6d 61 20 2a 70 54 61 62 53 63 68 65 6d  chema *pTabSchem
70040 61 3b 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61  a;     /* Schema
70050 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
70060 74 61 62 6c 65 20 2a 2f 0a 20 20 54 72 69 67 67  table */.  Trigg
70070 65 72 53 74 65 70 20 2a 73 74 65 70 5f 6c 69 73  erStep *step_lis
70080 74 3b 20 2f 2a 20 4c 69 6e 6b 20 6c 69 73 74 20  t; /* Link list 
70090 6f 66 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  of trigger progr
700a0 61 6d 20 73 74 65 70 73 20 20 20 20 20 20 20 20  am steps        
700b0 20 20 20 20 20 2a 2f 0a 20 20 54 72 69 67 67 65       */.  Trigge
700c0 72 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20  r *pNext;       
700d0 20 20 2f 2a 20 4e 65 78 74 20 74 72 69 67 67 65    /* Next trigge
700e0 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  r associated wit
700f0 68 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 7d  h the table */.}
70100 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 74 72 69 67 67  ;../*.** A trigg
70110 65 72 20 69 73 20 65 69 74 68 65 72 20 61 20 42  er is either a B
70120 45 46 4f 52 45 20 6f 72 20 61 6e 20 41 46 54 45  EFORE or an AFTE
70130 52 20 74 72 69 67 67 65 72 2e 20 20 54 68 65 20  R trigger.  The 
70140 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 61  following consta
70150 6e 74 73 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65  nts.** determine
70160 20 77 68 69 63 68 2e 20 0a 2a 2a 0a 2a 2a 20 49   which. .**.** I
70170 66 20 74 68 65 72 65 20 61 72 65 20 6d 75 6c 74  f there are mult
70180 69 70 6c 65 20 74 72 69 67 67 65 72 73 2c 20 79  iple triggers, y
70190 6f 75 20 6d 69 67 68 74 20 6f 66 20 73 6f 6d 65  ou might of some
701a0 20 42 45 46 4f 52 45 20 61 6e 64 20 73 6f 6d 65   BEFORE and some
701b0 20 41 46 54 45 52 2e 0a 2a 2a 20 49 6e 20 74 68   AFTER..** In th
701c0 61 74 20 63 61 73 65 73 2c 20 74 68 65 20 63 6f  at cases, the co
701d0 6e 73 74 61 6e 74 73 20 62 65 6c 6f 77 20 63 61  nstants below ca
701e0 6e 20 62 65 20 4f 52 65 64 20 74 6f 67 65 74 68  n be ORed togeth
701f0 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54  er..*/.#define T
70200 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 20 31  RIGGER_BEFORE  1
70210 0a 23 64 65 66 69 6e 65 20 54 52 49 47 47 45 52  .#define TRIGGER
70220 5f 41 46 54 45 52 20 20 20 32 0a 0a 2f 2a 0a 20  _AFTER   2../*. 
70230 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
70240 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 53   struct TriggerS
70250 74 65 70 20 69 73 20 75 73 65 64 20 74 6f 20 73  tep is used to s
70260 74 6f 72 65 20 61 20 73 69 6e 67 6c 65 20 53 51  tore a single SQ
70270 4c 20 73 74 61 74 65 6d 65 6e 74 0a 20 2a 20 74  L statement. * t
70280 68 61 74 20 69 73 20 61 20 70 61 72 74 20 6f 66  hat is a part of
70290 20 61 20 74 72 69 67 67 65 72 2d 70 72 6f 67 72   a trigger-progr
702a0 61 6d 2e 20 0a 20 2a 0a 20 2a 20 49 6e 73 74 61  am. . *. * Insta
702b0 6e 63 65 73 20 6f 66 20 73 74 72 75 63 74 20 54  nces of struct T
702c0 72 69 67 67 65 72 53 74 65 70 20 61 72 65 20 73  riggerStep are s
702d0 74 6f 72 65 64 20 69 6e 20 61 20 73 69 6e 67 6c  tored in a singl
702e0 79 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28 6c  y linked list (l
702f0 69 6e 6b 65 64 0a 20 2a 20 75 73 69 6e 67 20 74  inked. * using t
70300 68 65 20 22 70 4e 65 78 74 22 20 6d 65 6d 62 65  he "pNext" membe
70310 72 29 20 72 65 66 65 72 65 6e 63 65 64 20 62 79  r) referenced by
70320 20 74 68 65 20 22 73 74 65 70 5f 6c 69 73 74 22   the "step_list"
70330 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 0a   member of the .
70340 20 2a 20 61 73 73 6f 63 69 61 74 65 64 20 73 74   * associated st
70350 72 75 63 74 20 54 72 69 67 67 65 72 20 69 6e 73  ruct Trigger ins
70360 74 61 6e 63 65 2e 20 54 68 65 20 66 69 72 73 74  tance. The first
70370 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20   element of the 
70380 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 0a 20  linked list is. 
70390 2a 20 74 68 65 20 66 69 72 73 74 20 73 74 65 70  * the first step
703a0 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 2d   of the trigger-
703b0 70 72 6f 67 72 61 6d 2e 0a 20 2a 20 0a 20 2a 20  program.. * . * 
703c0 54 68 65 20 22 6f 70 22 20 6d 65 6d 62 65 72 20  The "op" member 
703d0 69 6e 64 69 63 61 74 65 73 20 77 68 65 74 68 65  indicates whethe
703e0 72 20 74 68 69 73 20 69 73 20 61 20 22 44 45 4c  r this is a "DEL
703f0 45 54 45 22 2c 20 22 49 4e 53 45 52 54 22 2c 20  ETE", "INSERT", 
70400 22 55 50 44 41 54 45 22 20 6f 72 0a 20 2a 20 22  "UPDATE" or. * "
70410 53 45 4c 45 43 54 22 20 73 74 61 74 65 6d 65 6e  SELECT" statemen
70420 74 2e 20 54 68 65 20 6d 65 61 6e 69 6e 67 73 20  t. The meanings 
70430 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6d 65 6d  of the other mem
70440 62 65 72 73 20 69 73 20 64 65 74 65 72 6d 69 6e  bers is determin
70450 65 64 20 62 79 20 74 68 65 20 0a 20 2a 20 76 61  ed by the . * va
70460 6c 75 65 20 6f 66 20 22 6f 70 22 20 61 73 20 66  lue of "op" as f
70470 6f 6c 6c 6f 77 73 3a 0a 20 2a 0a 20 2a 20 28 6f  ollows:. *. * (o
70480 70 20 3d 3d 20 54 4b 5f 49 4e 53 45 52 54 29 0a  p == TK_INSERT).
70490 20 2a 20 6f 72 63 6f 6e 66 20 20 20 20 2d 3e 20   * orconf    -> 
704a0 73 74 6f 72 65 73 20 74 68 65 20 4f 4e 20 43 4f  stores the ON CO
704b0 4e 46 4c 49 43 54 20 61 6c 67 6f 72 69 74 68 6d  NFLICT algorithm
704c0 0a 20 2a 20 70 53 65 6c 65 63 74 20 20 20 2d 3e  . * pSelect   ->
704d0 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 49   If this is an I
704e0 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53  NSERT INTO ... S
704f0 45 4c 45 43 54 20 2e 2e 2e 20 73 74 61 74 65 6d  ELECT ... statem
70500 65 6e 74 2c 20 74 68 65 6e 0a 20 2a 20 20 20 20  ent, then. *    
70510 20 20 20 20 20 20 20 20 20 20 74 68 69 73 20 73            this s
70520 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72 20  tores a pointer 
70530 74 6f 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  to the SELECT st
70540 61 74 65 6d 65 6e 74 2e 20 4f 74 68 65 72 77 69  atement. Otherwi
70550 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 74 61 72 67  se NULL.. * targ
70560 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 6e  et    -> A token
70570 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 6e 61 6d   holding the nam
70580 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  e of the table t
70590 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 2e 0a 20  o insert into.. 
705a0 2a 20 70 45 78 70 72 4c 69 73 74 20 2d 3e 20 49  * pExprList -> I
705b0 66 20 74 68 69 73 20 69 73 20 61 6e 20 49 4e 53  f this is an INS
705c0 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 56 41 4c  ERT INTO ... VAL
705d0 55 45 53 20 2e 2e 2e 20 73 74 61 74 65 6d 65 6e  UES ... statemen
705e0 74 2c 20 74 68 65 6e 0a 20 2a 20 20 20 20 20 20  t, then. *      
705f0 20 20 20 20 20 20 20 20 74 68 69 73 20 73 74 6f          this sto
70600 72 65 73 20 76 61 6c 75 65 73 20 74 6f 20 62 65  res values to be
70610 20 69 6e 73 65 72 74 65 64 2e 20 4f 74 68 65 72   inserted. Other
70620 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 49  wise NULL.. * pI
70630 64 4c 69 73 74 20 20 20 2d 3e 20 49 66 20 74 68  dList   -> If th
70640 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20  is is an INSERT 
70650 49 4e 54 4f 20 2e 2e 2e 20 28 3c 63 6f 6c 75 6d  INTO ... (<colum
70660 6e 2d 6e 61 6d 65 73 3e 29 20 56 41 4c 55 45 53  n-names>) VALUES
70670 20 2e 2e 2e 20 0a 20 2a 20 20 20 20 20 20 20 20   ... . *        
70680 20 20 20 20 20 20 73 74 61 74 65 6d 65 6e 74 2c        statement,
70690 20 74 68 65 6e 20 74 68 69 73 20 73 74 6f 72 65   then this store
706a0 73 20 74 68 65 20 63 6f 6c 75 6d 6e 2d 6e 61 6d  s the column-nam
706b0 65 73 20 74 6f 20 62 65 0a 20 2a 20 20 20 20 20  es to be. *     
706c0 20 20 20 20 20 20 20 20 20 69 6e 73 65 72 74 65           inserte
706d0 64 20 69 6e 74 6f 2e 0a 20 2a 0a 20 2a 20 28 6f  d into.. *. * (o
706e0 70 20 3d 3d 20 54 4b 5f 44 45 4c 45 54 45 29 0a  p == TK_DELETE).
706f0 20 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e 20   * target    -> 
70700 41 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20  A token holding 
70710 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
70720 74 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20  table to delete 
70730 66 72 6f 6d 2e 0a 20 2a 20 70 57 68 65 72 65 20  from.. * pWhere 
70740 20 20 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20     -> The WHERE 
70750 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 44 45  clause of the DE
70760 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69  LETE statement i
70770 66 20 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69  f one is specifi
70780 65 64 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20  ed.. *          
70790 20 20 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55      Otherwise NU
707a0 4c 4c 2e 0a 20 2a 20 0a 20 2a 20 28 6f 70 20 3d  LL.. * . * (op =
707b0 3d 20 54 4b 5f 55 50 44 41 54 45 29 0a 20 2a 20  = TK_UPDATE). * 
707c0 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74  target    -> A t
707d0 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65  oken holding the
707e0 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   name of the tab
707f0 6c 65 20 74 6f 20 75 70 64 61 74 65 20 72 6f 77  le to update row
70800 73 20 6f 66 2e 0a 20 2a 20 70 57 68 65 72 65 20  s of.. * pWhere 
70810 20 20 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20     -> The WHERE 
70820 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 55 50  clause of the UP
70830 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69  DATE statement i
70840 66 20 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69  f one is specifi
70850 65 64 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20  ed.. *          
70860 20 20 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55      Otherwise NU
70870 4c 4c 2e 0a 20 2a 20 70 45 78 70 72 4c 69 73 74  LL.. * pExprList
70880 20 2d 3e 20 41 20 6c 69 73 74 20 6f 66 20 74 68   -> A list of th
70890 65 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 75 70 64  e columns to upd
708a0 61 74 65 20 61 6e 64 20 74 68 65 20 65 78 70 72  ate and the expr
708b0 65 73 73 69 6f 6e 73 20 74 6f 20 75 70 64 61 74  essions to updat
708c0 65 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20  e. *            
708d0 20 20 74 68 65 6d 20 74 6f 2e 20 53 65 65 20 73    them to. See s
708e0 71 6c 69 74 65 33 55 70 64 61 74 65 28 29 20 64  qlite3Update() d
708f0 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  ocumentation of 
70900 22 70 43 68 61 6e 67 65 73 22 0a 20 2a 20 20 20  "pChanges". *   
70910 20 20 20 20 20 20 20 20 20 20 20 61 72 67 75 6d             argum
70920 65 6e 74 2e 0a 20 2a 20 0a 20 2a 2f 0a 73 74 72  ent.. * . */.str
70930 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70 20  uct TriggerStep 
70940 7b 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20  {.  int op;     
70950 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
70960 6f 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b  of TK_DELETE, TK
70970 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45  _UPDATE, TK_INSE
70980 52 54 2c 20 54 4b 5f 53 45 4c 45 43 54 20 2a 2f  RT, TK_SELECT */
70990 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 3b 20 20  .  int orconf;  
709a0 20 20 20 20 20 20 20 20 2f 2a 20 4f 45 5f 52 6f          /* OE_Ro
709b0 6c 6c 62 61 63 6b 20 65 74 63 2e 20 2a 2f 0a 20  llback etc. */. 
709c0 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b   Trigger *pTrig;
709d0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72 69        /* The tri
709e0 67 67 65 72 20 74 68 61 74 20 74 68 69 73 20 73  gger that this s
709f0 74 65 70 20 69 73 20 61 20 70 61 72 74 20 6f 66  tep is a part of
70a00 20 2a 2f 0a 0a 20 20 53 65 6c 65 63 74 20 2a 70   */..  Select *p
70a10 53 65 6c 65 63 74 3b 20 20 20 20 20 2f 2a 20 56  Select;     /* V
70a20 61 6c 69 64 20 66 6f 72 20 53 45 4c 45 43 54 20  alid for SELECT 
70a30 61 6e 64 20 73 6f 6d 65 74 69 6d 65 73 20 0a 20  and sometimes . 
70a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70a50 20 20 20 20 20 20 20 20 20 49 4e 53 45 52 54 20           INSERT 
70a60 73 74 65 70 73 20 28 77 68 65 6e 20 70 45 78 70  steps (when pExp
70a70 72 4c 69 73 74 20 3d 3d 20 30 29 20 2a 2f 0a 20  rList == 0) */. 
70a80 20 54 6f 6b 65 6e 20 74 61 72 67 65 74 3b 20 20   Token target;  
70a90 20 20 20 20 20 20 2f 2a 20 56 61 6c 69 64 20 66        /* Valid f
70aa0 6f 72 20 44 45 4c 45 54 45 2c 20 55 50 44 41 54  or DELETE, UPDAT
70ab0 45 2c 20 49 4e 53 45 52 54 20 73 74 65 70 73 20  E, INSERT steps 
70ac0 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72  */.  Expr *pWher
70ad0 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c  e;        /* Val
70ae0 69 64 20 66 6f 72 20 44 45 4c 45 54 45 2c 20 55  id for DELETE, U
70af0 50 44 41 54 45 20 73 74 65 70 73 20 2a 2f 0a 20  PDATE steps */. 
70b00 20 45 78 70 72 4c 69 73 74 20 2a 70 45 78 70 72   ExprList *pExpr
70b10 4c 69 73 74 3b 20 2f 2a 20 56 61 6c 69 64 20 66  List; /* Valid f
70b20 6f 72 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  or UPDATE statem
70b30 65 6e 74 73 20 61 6e 64 20 73 6f 6d 65 74 69 6d  ents and sometim
70b40 65 73 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  es .            
70b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 49                 I
70b60 4e 53 45 52 54 20 73 74 65 70 73 20 28 77 68 65  NSERT steps (whe
70b70 6e 20 70 53 65 6c 65 63 74 20 3d 3d 20 30 29 20  n pSelect == 0) 
70b80 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 49 64 4c          */.  IdL
70b90 69 73 74 20 2a 70 49 64 4c 69 73 74 3b 20 20 20  ist *pIdList;   
70ba0 20 20 2f 2a 20 56 61 6c 69 64 20 66 6f 72 20 49    /* Valid for I
70bb0 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 73  NSERT statements
70bc0 20 6f 6e 6c 79 20 2a 2f 0a 20 20 54 72 69 67 67   only */.  Trigg
70bd0 65 72 53 74 65 70 20 2a 70 4e 65 78 74 3b 20 20  erStep *pNext;  
70be0 2f 2a 20 4e 65 78 74 20 69 6e 20 74 68 65 20 6c  /* Next in the l
70bf0 69 6e 6b 2d 6c 69 73 74 20 2a 2f 0a 20 20 54 72  ink-list */.  Tr
70c00 69 67 67 65 72 53 74 65 70 20 2a 70 4c 61 73 74  iggerStep *pLast
70c10 3b 20 20 2f 2a 20 4c 61 73 74 20 65 6c 65 6d 65  ;  /* Last eleme
70c20 6e 74 20 69 6e 20 6c 69 6e 6b 2d 6c 69 73 74 2e  nt in link-list.
70c30 20 56 61 6c 69 64 20 66 6f 72 20 31 73 74 20 65   Valid for 1st e
70c40 6c 65 6d 20 6f 6e 6c 79 20 2a 2f 0a 7d 3b 0a 0a  lem only */.};..
70c50 2f 2a 0a 20 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*. * An instanc
70c60 65 20 6f 66 20 73 74 72 75 63 74 20 54 72 69 67  e of struct Trig
70c70 67 65 72 53 74 61 63 6b 20 73 74 6f 72 65 73 20  gerStack stores 
70c80 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 72 65 71 75  information requ
70c90 69 72 65 64 20 64 75 72 69 6e 67 20 63 6f 64 65  ired during code
70ca0 0a 20 2a 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f  . * generation o
70cb0 66 20 61 20 73 69 6e 67 6c 65 20 74 72 69 67 67  f a single trigg
70cc0 65 72 20 70 72 6f 67 72 61 6d 2e 20 57 68 69 6c  er program. Whil
70cd0 65 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72  e the trigger pr
70ce0 6f 67 72 61 6d 20 69 73 20 62 65 69 6e 67 0a 20  ogram is being. 
70cf0 2a 20 63 6f 64 65 64 2c 20 69 74 73 20 61 73 73  * coded, its ass
70d00 6f 63 69 61 74 65 64 20 54 72 69 67 67 65 72 53  ociated TriggerS
70d10 74 61 63 6b 20 69 6e 73 74 61 6e 63 65 20 69 73  tack instance is
70d20 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74   pointed to by t
70d30 68 65 0a 20 2a 20 22 70 54 72 69 67 67 65 72 53  he. * "pTriggerS
70d40 74 61 63 6b 22 20 6d 65 6d 62 65 72 20 6f 66 20  tack" member of 
70d50 74 68 65 20 50 61 72 73 65 20 73 74 72 75 63 74  the Parse struct
70d60 75 72 65 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 70  ure.. *. * The p
70d70 54 61 62 20 6d 65 6d 62 65 72 20 70 6f 69 6e 74  Tab member point
70d80 73 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 74  s to the table t
70d90 68 61 74 20 74 72 69 67 67 65 72 73 20 61 72 65  hat triggers are
70da0 20 62 65 69 6e 67 20 63 6f 64 65 64 20 6f 6e 2e   being coded on.
70db0 20 54 68 65 20 0a 20 2a 20 6e 65 77 49 64 78 20   The . * newIdx 
70dc0 6d 65 6d 62 65 72 20 63 6f 6e 74 61 69 6e 73 20  member contains 
70dd0 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
70de0 20 76 64 62 65 20 63 75 72 73 6f 72 20 74 68 61   vdbe cursor tha
70df0 74 20 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20  t points at the 
70e00 74 65 6d 70 0a 20 2a 20 74 61 62 6c 65 20 74 68  temp. * table th
70e10 61 74 20 73 74 6f 72 65 73 20 74 68 65 20 6e 65  at stores the ne
70e20 77 2e 2a 20 72 65 66 65 72 65 6e 63 65 73 2e 20  w.* references. 
70e30 49 66 20 6e 65 77 2e 2a 20 72 65 66 65 72 65 6e  If new.* referen
70e40 63 65 73 20 61 72 65 20 6e 6f 74 20 76 61 6c 69  ces are not vali
70e50 64 0a 20 2a 20 66 6f 72 20 74 68 65 20 74 72 69  d. * for the tri
70e60 67 67 65 72 20 62 65 69 6e 67 20 63 6f 64 65 64  gger being coded
70e70 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 61 6e   (for example an
70e80 20 4f 4e 20 44 45 4c 45 54 45 20 74 72 69 67 67   ON DELETE trigg
70e90 65 72 29 2c 20 74 68 65 6e 20 6e 65 77 49 64 78  er), then newIdx
70ea0 0a 20 2a 20 69 73 20 73 65 74 20 74 6f 20 2d 31  . * is set to -1
70eb0 2e 20 54 68 65 20 6f 6c 64 49 64 78 20 6d 65 6d  . The oldIdx mem
70ec0 62 65 72 20 69 73 20 61 6e 61 6c 6f 67 6f 75 73  ber is analogous
70ed0 20 74 6f 20 6e 65 77 49 64 78 2c 20 66 6f 72 20   to newIdx, for 
70ee0 6f 6c 64 2e 2a 20 72 65 66 65 72 65 6e 63 65 73  old.* references
70ef0 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 4f 4e 20 43  .. *. * The ON C
70f00 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 74  ONFLICT policy t
70f10 6f 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68  o be used for th
70f20 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61  e trigger progra
70f30 6d 20 73 74 65 70 73 20 69 73 20 73 74 6f 72 65  m steps is store
70f40 64 20 0a 20 2a 20 61 73 20 74 68 65 20 6f 72 63  d . * as the orc
70f50 6f 6e 66 20 6d 65 6d 62 65 72 2e 20 49 66 20 74  onf member. If t
70f60 68 69 73 20 69 73 20 4f 45 5f 44 65 66 61 75 6c  his is OE_Defaul
70f70 74 2c 20 74 68 65 6e 20 74 68 65 20 4f 4e 20 43  t, then the ON C
70f80 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73 65 20 0a  ONFLICT clause .
70f90 20 2a 20 73 70 65 63 69 66 69 65 64 20 66 6f 72   * specified for
70fa0 20 69 6e 64 69 76 69 64 75 61 6c 20 74 72 69 67   individual trig
70fb0 67 65 72 73 20 73 74 65 70 73 20 69 73 20 75 73  gers steps is us
70fc0 65 64 2e 0a 20 2a 0a 20 2a 20 73 74 72 75 63 74  ed.. *. * struct
70fd0 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 68 61   TriggerStack ha
70fe0 73 20 61 20 22 70 4e 65 78 74 22 20 6d 65 6d 62  s a "pNext" memb
70ff0 65 72 2c 20 74 6f 20 61 6c 6c 6f 77 20 6c 69 6e  er, to allow lin
71000 6b 65 64 20 6c 69 73 74 73 20 74 6f 20 62 65 0a  ked lists to be.
71010 20 2a 20 63 6f 6e 73 74 72 75 63 74 65 64 2e 20   * constructed. 
71020 57 68 65 6e 20 63 6f 64 69 6e 67 20 6e 65 73 74  When coding nest
71030 65 64 20 74 72 69 67 67 65 72 73 20 28 74 72 69  ed triggers (tri
71040 67 67 65 72 73 20 66 69 72 65 64 20 62 79 20 6f  ggers fired by o
71050 74 68 65 72 20 74 72 69 67 67 65 72 73 29 0a 20  ther triggers). 
71060 2a 20 65 61 63 68 20 6e 65 73 74 65 64 20 74 72  * each nested tr
71070 69 67 67 65 72 20 73 74 6f 72 65 73 20 69 74 73  igger stores its
71080 20 70 61 72 65 6e 74 20 74 72 69 67 67 65 72 27   parent trigger'
71090 73 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 61  s TriggerStack a
710a0 73 20 74 68 65 20 22 70 4e 65 78 74 22 20 0a 20  s the "pNext" . 
710b0 2a 20 70 6f 69 6e 74 65 72 2e 20 4f 6e 63 65 20  * pointer. Once 
710c0 74 68 65 20 6e 65 73 74 65 64 20 74 72 69 67 67  the nested trigg
710d0 65 72 20 68 61 73 20 62 65 65 6e 20 63 6f 64 65  er has been code
710e0 64 2c 20 74 68 65 20 70 4e 65 78 74 20 76 61 6c  d, the pNext val
710f0 75 65 20 69 73 20 72 65 73 74 6f 72 65 64 0a 20  ue is restored. 
71100 2a 20 74 6f 20 74 68 65 20 70 54 72 69 67 67 65  * to the pTrigge
71110 72 53 74 61 63 6b 20 6d 65 6d 62 65 72 20 6f 66  rStack member of
71120 20 74 68 65 20 50 61 72 73 65 20 73 74 75 63 74   the Parse stuct
71130 75 72 65 20 61 6e 64 20 63 6f 64 69 6e 67 20 6f  ure and coding o
71140 66 20 74 68 65 20 70 61 72 65 6e 74 0a 20 2a 20  f the parent. * 
71150 74 72 69 67 67 65 72 20 63 6f 6e 74 69 6e 75 65  trigger continue
71160 73 2e 0a 20 2a 0a 20 2a 20 42 65 66 6f 72 65 20  s.. *. * Before 
71170 61 20 6e 65 73 74 65 64 20 74 72 69 67 67 65 72  a nested trigger
71180 20 69 73 20 63 6f 64 65 64 2c 20 74 68 65 20 6c   is coded, the l
71190 69 6e 6b 65 64 20 6c 69 73 74 20 70 6f 69 6e 74  inked list point
711a0 65 64 20 74 6f 20 62 79 20 74 68 65 20 0a 20 2a  ed to by the . *
711b0 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 20 69   pTriggerStack i
711c0 73 20 73 63 61 6e 6e 65 64 20 74 6f 20 65 6e 73  s scanned to ens
711d0 75 72 65 20 74 68 61 74 20 74 68 65 20 74 72 69  ure that the tri
711e0 67 67 65 72 20 69 73 20 6e 6f 74 20 61 62 6f 75  gger is not abou
711f0 74 20 74 6f 20 62 65 20 63 6f 64 65 64 0a 20 2a  t to be coded. *
71200 20 72 65 63 75 72 73 69 76 65 6c 79 2e 20 49 66   recursively. If
71210 20 74 68 69 73 20 63 6f 6e 64 69 74 69 6f 6e 20   this condition 
71220 69 73 20 64 65 74 65 63 74 65 64 2c 20 74 68 65  is detected, the
71230 20 6e 65 73 74 65 64 20 74 72 69 67 67 65 72 20   nested trigger 
71240 69 73 20 6e 6f 74 20 63 6f 64 65 64 2e 0a 20 2a  is not coded.. *
71250 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67 65 72  /.struct Trigger
71260 53 74 61 63 6b 20 7b 0a 20 20 54 61 62 6c 65 20  Stack {.  Table 
71270 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 2f  *pTab;         /
71280 2a 20 54 61 62 6c 65 20 74 68 61 74 20 74 72 69  * Table that tri
71290 67 67 65 72 73 20 61 72 65 20 63 75 72 72 65 6e  ggers are curren
712a0 74 6c 79 20 62 65 69 6e 67 20 63 6f 64 65 64 20  tly being coded 
712b0 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 49  on */.  int newI
712c0 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  dx;          /* 
712d0 49 6e 64 65 78 20 6f 66 20 76 64 62 65 20 63 75  Index of vdbe cu
712e0 72 73 6f 72 20 74 6f 20 22 6e 65 77 22 20 74 65  rsor to "new" te
712f0 6d 70 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  mp table */.  in
71300 74 20 6f 6c 64 49 64 78 3b 20 20 20 20 20 20 20  t oldIdx;       
71310 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 76     /* Index of v
71320 64 62 65 20 63 75 72 73 6f 72 20 74 6f 20 22 6f  dbe cursor to "o
71330 6c 64 22 20 74 65 6d 70 20 74 61 62 6c 65 20 2a  ld" temp table *
71340 2f 0a 20 20 75 33 32 20 6e 65 77 43 6f 6c 4d 61  /.  u32 newColMa
71350 73 6b 3b 0a 20 20 75 33 32 20 6f 6c 64 43 6f 6c  sk;.  u32 oldCol
71360 4d 61 73 6b 3b 0a 20 20 69 6e 74 20 6f 72 63 6f  Mask;.  int orco
71370 6e 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  nf;          /* 
71380 43 75 72 72 65 6e 74 20 6f 72 63 6f 6e 66 20 70  Current orconf p
71390 6f 6c 69 63 79 20 2a 2f 0a 20 20 69 6e 74 20 69  olicy */.  int i
713a0 67 6e 6f 72 65 4a 75 6d 70 3b 20 20 20 20 20 20  gnoreJump;      
713b0 2f 2a 20 77 68 65 72 65 20 74 6f 20 6a 75 6d 70  /* where to jump
713c0 20 74 6f 20 66 6f 72 20 61 20 52 41 49 53 45 28   to for a RAISE(
713d0 49 47 4e 4f 52 45 29 20 2a 2f 0a 20 20 54 72 69  IGNORE) */.  Tri
713e0 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20  gger *pTrigger; 
713f0 20 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72    /* The trigger
71400 20 63 75 72 72 65 6e 74 6c 79 20 62 65 69 6e 67   currently being
71410 20 63 6f 64 65 64 20 2a 2f 0a 20 20 54 72 69 67   coded */.  Trig
71420 67 65 72 53 74 61 63 6b 20 2a 70 4e 65 78 74 3b  gerStack *pNext;
71430 20 2f 2a 20 4e 65 78 74 20 74 72 69 67 67 65 72   /* Next trigger
71440 20 64 6f 77 6e 20 6f 6e 20 74 68 65 20 74 72 69   down on the tri
71450 67 67 65 72 20 73 74 61 63 6b 20 2a 2f 0a 7d 3b  gger stack */.};
71460 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
71470 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
71480 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61  contains informa
71490 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68 65  tion used by the
714a0 20 73 71 6c 69 74 65 46 69 78 2e 2e 2e 0a 2a 2a   sqliteFix....**
714b0 20 72 6f 75 74 69 6e 65 73 20 61 73 20 74 68 65   routines as the
714c0 79 20 77 61 6c 6b 20 74 68 65 20 70 61 72 73 65  y walk the parse
714d0 20 74 72 65 65 20 74 6f 20 6d 61 6b 65 20 64 61   tree to make da
714e0 74 61 62 61 73 65 20 72 65 66 65 72 65 6e 63 65  tabase reference
714f0 73 0a 2a 2a 20 65 78 70 6c 69 63 69 74 2e 20 20  s.** explicit.  
71500 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
71510 63 74 20 44 62 46 69 78 65 72 20 44 62 46 69 78  ct DbFixer DbFix
71520 65 72 3b 0a 73 74 72 75 63 74 20 44 62 46 69 78  er;.struct DbFix
71530 65 72 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50  er {.  Parse *pP
71540 61 72 73 65 3b 20 20 20 20 20 20 2f 2a 20 54 68  arse;      /* Th
71550 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  e parsing contex
71560 74 2e 20 20 45 72 72 6f 72 20 6d 65 73 73 61 67  t.  Error messag
71570 65 73 20 77 72 69 74 74 65 6e 20 68 65 72 65 20  es written here 
71580 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
71590 2a 7a 44 62 3b 20 20 20 20 2f 2a 20 4d 61 6b 65  *zDb;    /* Make
715a0 20 73 75 72 65 20 61 6c 6c 20 6f 62 6a 65 63 74   sure all object
715b0 73 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20  s are contained 
715c0 69 6e 20 74 68 69 73 20 64 61 74 61 62 61 73 65  in this database
715d0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
715e0 20 2a 7a 54 79 70 65 3b 20 20 2f 2a 20 54 79 70   *zType;  /* Typ
715f0 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 61 69 6e  e of the contain
71600 65 72 20 2d 20 75 73 65 64 20 66 6f 72 20 65 72  er - used for er
71610 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a  ror messages */.
71620 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70    const Token *p
71630 4e 61 6d 65 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66  Name; /* Name of
71640 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 72 20 2d   the container -
71650 20 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20   used for error 
71660 6d 65 73 73 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a  messages */.};..
71670 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65 63 74 65  /*.** An objecte
71680 64 20 75 73 65 64 20 74 6f 20 61 63 63 75 6d 75  d used to accumu
71690 6c 61 74 65 20 74 68 65 20 74 65 78 74 20 6f 66  late the text of
716a0 20 61 20 73 74 72 69 6e 67 20 77 68 65 72 65 20   a string where 
716b0 77 65 0a 2a 2a 20 64 6f 20 6e 6f 74 20 6e 65 63  we.** do not nec
716c0 65 73 73 61 72 69 6c 79 20 6b 6e 6f 77 20 68 6f  essarily know ho
716d0 77 20 62 69 67 20 74 68 65 20 73 74 72 69 6e 67  w big the string
716e0 20 77 69 6c 6c 20 62 65 20 69 6e 20 74 68 65 20   will be in the 
716f0 65 6e 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53  end..*/.struct S
71700 74 72 41 63 63 75 6d 20 7b 0a 20 20 73 71 6c 69  trAccum {.  sqli
71710 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
71720 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 64 61 74   /* Optional dat
71730 61 62 61 73 65 20 66 6f 72 20 6c 6f 6f 6b 61 73  abase for lookas
71740 69 64 65 2e 20 20 43 61 6e 20 62 65 20 4e 55 4c  ide.  Can be NUL
71750 4c 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 42 61  L */.  char *zBa
71760 73 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41  se;         /* A
71770 20 62 61 73 65 20 61 6c 6c 6f 63 61 74 69 6f 6e   base allocation
71780 2e 20 20 4e 6f 74 20 66 72 6f 6d 20 6d 61 6c 6c  .  Not from mall
71790 6f 63 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  oc. */.  char *z
717a0 54 65 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  Text;         /*
717b0 20 54 68 65 20 73 74 72 69 6e 67 20 63 6f 6c 6c   The string coll
717c0 65 63 74 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a  ected so far */.
717d0 20 20 69 6e 74 20 20 6e 43 68 61 72 3b 20 20 20    int  nChar;   
717e0 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
717f0 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 73   of the string s
71800 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 20  o far */.  int  
71810 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20  nAlloc;         
71820 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 73 70 61  /* Amount of spa
71830 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20  ce allocated in 
71840 7a 54 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 20  zText */.  int  
71850 6d 78 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20  mxAlloc;        
71860 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77  /* Maximum allow
71870 65 64 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68  ed string length
71880 20 2a 2f 0a 20 20 75 38 20 20 20 6d 61 6c 6c 6f   */.  u8   mallo
71890 63 46 61 69 6c 65 64 3b 20 20 20 2f 2a 20 42 65  cFailed;   /* Be
718a0 63 6f 6d 65 73 20 74 72 75 65 20 69 66 20 61 6e  comes true if an
718b0 79 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  y memory allocat
718c0 69 6f 6e 20 66 61 69 6c 73 20 2a 2f 0a 20 20 75  ion fails */.  u
718d0 38 20 20 20 75 73 65 4d 61 6c 6c 6f 63 3b 20 20  8   useMalloc;  
718e0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 7a      /* True if z
718f0 54 65 78 74 20 69 73 20 65 6e 6c 61 72 67 61 62  Text is enlargab
71900 6c 65 20 75 73 69 6e 67 20 72 65 61 6c 6c 6f 63  le using realloc
71910 20 2a 2f 0a 20 20 75 38 20 20 20 74 6f 6f 42 69   */.  u8   tooBi
71920 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65  g;         /* Be
71930 63 6f 6d 65 73 20 74 72 75 65 20 69 66 20 73 74  comes true if st
71940 72 69 6e 67 20 73 69 7a 65 20 65 78 63 65 65 64  ring size exceed
71950 73 20 6c 69 6d 69 74 73 20 2a 2f 0a 7d 3b 0a 0a  s limits */.};..
71960 2f 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20  /*.** A pointer 
71970 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72  to this structur
71980 65 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d  e is used to com
71990 6d 75 6e 69 63 61 74 65 20 69 6e 66 6f 72 6d 61  municate informa
719a0 74 69 6f 6e 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c  tion.** from sql
719b0 69 74 65 33 49 6e 69 74 20 61 6e 64 20 4f 50 5f  ite3Init and OP_
719c0 50 61 72 73 65 53 63 68 65 6d 61 20 69 6e 74 6f  ParseSchema into
719d0 20 74 68 65 20 73 71 6c 69 74 65 33 49 6e 69 74   the sqlite3Init
719e0 43 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 74 79 70  Callback..*/.typ
719f0 65 64 65 66 20 73 74 72 75 63 74 20 7b 0a 20 20  edef struct {.  
71a00 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
71a10 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
71a20 61 73 65 20 62 65 69 6e 67 20 69 6e 69 74 69 61  ase being initia
71a30 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  lized */.  int i
71a40 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  Db;            /
71a50 2a 20 30 20 66 6f 72 20 6d 61 69 6e 20 64 61 74  * 0 for main dat
71a60 61 62 61 73 65 2e 20 20 31 20 66 6f 72 20 54 45  abase.  1 for TE
71a70 4d 50 2c 20 32 2e 2e 20 66 6f 72 20 41 54 54 41  MP, 2.. for ATTA
71a80 43 48 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a  CHed */.  char *
71a90 2a 70 7a 45 72 72 4d 73 67 3b 20 20 20 20 2f 2a  *pzErrMsg;    /*
71aa0 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73   Error message s
71ab0 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20  tored here */.  
71ac0 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
71ad0 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f      /* Result co
71ae0 64 65 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a  de stored here *
71af0 2f 0a 7d 20 49 6e 69 74 44 61 74 61 3b 0a 0a 2f  /.} InitData;../
71b00 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 20 63  *.** Structure c
71b10 6f 6e 74 61 69 6e 69 6e 67 20 67 6c 6f 62 61 6c  ontaining global
71b20 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 64   configuration d
71b30 61 74 61 20 66 6f 72 20 74 68 65 20 53 51 4c 69  ata for the SQLi
71b40 74 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a  te library..**.*
71b50 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65  * This structure
71b60 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20 73   also contains s
71b70 6f 6d 65 20 73 74 61 74 65 20 69 6e 66 6f 72 6d  ome state inform
71b80 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74  ation..*/.struct
71b90 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 7b   Sqlite3Config {
71ba0 0a 20 20 69 6e 74 20 62 4d 65 6d 73 74 61 74 3b  .  int bMemstat;
71bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71bc0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20       /* True to 
71bd0 65 6e 61 62 6c 65 20 6d 65 6d 6f 72 79 20 73 74  enable memory st
71be0 61 74 75 73 20 2a 2f 0a 20 20 69 6e 74 20 62 43  atus */.  int bC
71bf0 6f 72 65 4d 75 74 65 78 3b 20 20 20 20 20 20 20  oreMutex;       
71c00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
71c10 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 63 6f  rue to enable co
71c20 72 65 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20  re mutexing */. 
71c30 20 69 6e 74 20 62 46 75 6c 6c 4d 75 74 65 78 3b   int bFullMutex;
71c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71c50 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e     /* True to en
71c60 61 62 6c 65 20 66 75 6c 6c 20 6d 75 74 65 78 69  able full mutexi
71c70 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 53 74  ng */.  int mxSt
71c80 72 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20  rlen;           
71c90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
71ca0 69 6d 75 6d 20 73 74 72 69 6e 67 20 6c 65 6e 67  imum string leng
71cb0 74 68 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4c 6f  th */.  int szLo
71cc0 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20 20 20  okaside;        
71cd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66            /* Def
71ce0 61 75 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 20 62  ault lookaside b
71cf0 75 66 66 65 72 20 73 69 7a 65 20 2a 2f 0a 20 20  uffer size */.  
71d00 69 6e 74 20 6e 4c 6f 6f 6b 61 73 69 64 65 3b 20  int nLookaside; 
71d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71d20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f 6f    /* Default loo
71d30 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 63 6f  kaside buffer co
71d40 75 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  unt */.  sqlite3
71d50 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d 3b 20  _mem_methods m; 
71d60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
71d70 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61  w-level memory a
71d80 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 65 72 66  llocation interf
71d90 61 63 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ace */.  sqlite3
71da0 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 6d  _mutex_methods m
71db0 75 74 65 78 3b 20 20 20 20 20 20 2f 2a 20 4c 6f  utex;      /* Lo
71dc0 77 2d 6c 65 76 65 6c 20 6d 75 74 65 78 20 69 6e  w-level mutex in
71dd0 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 76 6f 69  terface */.  voi
71de0 64 20 2a 70 48 65 61 70 3b 20 20 20 20 20 20 20  d *pHeap;       
71df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
71e00 2a 20 48 65 61 70 20 73 74 6f 72 61 67 65 20 73  * Heap storage s
71e10 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48  pace */.  int nH
71e20 65 61 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  eap;            
71e30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
71e40 69 7a 65 20 6f 66 20 70 48 65 61 70 5b 5d 20 2a  ize of pHeap[] *
71e50 2f 0a 20 20 69 6e 74 20 6d 6e 52 65 71 2c 20 6d  /.  int mnReq, m
71e60 78 52 65 71 3b 20 20 20 20 20 20 20 20 20 20 20  xReq;           
71e70 20 20 20 20 20 20 2f 2a 20 4d 69 6e 20 61 6e 64        /* Min and
71e80 20 6d 61 78 20 68 65 61 70 20 72 65 71 75 65 73   max heap reques
71e90 74 73 20 73 69 7a 65 73 20 2a 2f 0a 20 20 76 6f  ts sizes */.  vo
71ea0 69 64 20 2a 70 53 63 72 61 74 63 68 3b 20 20 20  id *pScratch;   
71eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71ec0 2f 2a 20 53 63 72 61 74 63 68 20 6d 65 6d 6f 72  /* Scratch memor
71ed0 79 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72  y */.  int szScr
71ee0 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20  atch;           
71ef0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
71f00 20 6f 66 20 65 61 63 68 20 73 63 72 61 74 63 68   of each scratch
71f10 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74   buffer */.  int
71f20 20 6e 53 63 72 61 74 63 68 3b 20 20 20 20 20 20   nScratch;      
71f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
71f40 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 63 72 61  * Number of scra
71f50 74 63 68 20 62 75 66 66 65 72 73 20 2a 2f 0a 20  tch buffers */. 
71f60 20 76 6f 69 64 20 2a 70 50 61 67 65 3b 20 20 20   void *pPage;   
71f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71f80 20 20 20 2f 2a 20 50 61 67 65 20 63 61 63 68 65     /* Page cache
71f90 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e 74   memory */.  int
71fa0 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 20   szPage;        
71fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
71fc0 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 70  * Size of each p
71fd0 61 67 65 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a  age in pPage[] *
71fe0 2f 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 20 20  /.  int nPage;  
71ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72000 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
72010 6f 66 20 70 61 67 65 73 20 69 6e 20 70 50 61 67  of pages in pPag
72020 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49  e[] */.  int isI
72030 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  nit;            
72040 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
72050 75 65 20 61 66 74 65 72 20 69 6e 69 74 69 61 6c  ue after initial
72060 69 7a 61 74 69 6f 6e 20 68 61 73 20 66 69 6e 69  ization has fini
72070 73 68 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 6e  shed */.  int in
72080 50 72 6f 67 72 65 73 73 3b 20 20 20 20 20 20 20  Progress;       
72090 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
720a0 72 75 65 20 77 68 69 6c 65 20 69 6e 69 74 69 61  rue while initia
720b0 6c 69 7a 61 74 69 6f 6e 20 69 6e 20 70 72 6f 67  lization in prog
720c0 72 65 73 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73  ress */.  int is
720d0 4d 61 6c 6c 6f 63 49 6e 69 74 3b 20 20 20 20 20  MallocInit;     
720e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
720f0 72 75 65 20 61 66 74 65 72 20 6d 61 6c 6c 6f 63  rue after malloc
72100 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   is initialized 
72110 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  */.  sqlite3_mut
72120 65 78 20 2a 70 49 6e 69 74 4d 75 74 65 78 3b 20  ex *pInitMutex; 
72130 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20         /* Mutex 
72140 75 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  used by sqlite3_
72150 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 2a 2f 0a  initialize() */.
72160 20 20 69 6e 74 20 6e 52 65 66 49 6e 69 74 4d 75    int nRefInitMu
72170 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  tex;            
72180 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
72190 20 75 73 65 72 73 20 6f 66 20 70 49 6e 69 74 4d   users of pInitM
721a0 75 74 65 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 53  utex */.  int nS
721b0 6d 61 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  mall;           
721c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61              /* a
721d0 6c 6c 6f 63 20 73 69 7a 65 20 74 68 72 65 73 68  lloc size thresh
721e0 6f 6c 64 20 75 73 65 64 20 62 79 20 6d 65 6d 36  old used by mem6
721f0 2e 63 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 61  .c */.  int mxPa
72200 72 73 65 72 53 74 61 63 6b 3b 20 20 20 20 20 20  rserStack;      
72210 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 61 78            /* max
72220 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68  imum depth of th
72230 65 20 70 61 72 73 65 72 20 73 74 61 63 6b 20 2a  e parser stack *
72240 2f 0a 20 20 69 6e 74 20 73 68 61 72 65 64 43 61  /.  int sharedCa
72250 63 68 65 45 6e 61 62 6c 65 64 3b 20 20 20 20 20  cheEnabled;     
72260 20 20 20 20 20 20 2f 2a 20 74 72 75 65 20 69 66        /* true if
72270 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f   shared-cache mo
72280 64 65 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 7d 3b  de enabled */.};
72290 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 65 78 74 20  ../*.** Context 
722a0 70 6f 69 6e 74 65 72 20 70 61 73 73 65 64 20 64  pointer passed d
722b0 6f 77 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20  own through the 
722c0 74 72 65 65 2d 77 61 6c 6b 2e 0a 2a 2f 0a 73 74  tree-walk..*/.st
722d0 72 75 63 74 20 57 61 6c 6b 65 72 20 7b 0a 20 20  ruct Walker {.  
722e0 69 6e 74 20 28 2a 78 45 78 70 72 43 61 6c 6c 62  int (*xExprCallb
722f0 61 63 6b 29 28 57 61 6c 6b 65 72 2a 2c 20 45 78  ack)(Walker*, Ex
72300 70 72 2a 29 3b 20 20 20 20 20 2f 2a 20 43 61 6c  pr*);     /* Cal
72310 6c 62 61 63 6b 20 66 6f 72 20 65 78 70 72 65 73  lback for expres
72320 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 28  sions */.  int (
72330 2a 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b  *xSelectCallback
72340 29 28 57 61 6c 6b 65 72 2a 2c 53 65 6c 65 63 74  )(Walker*,Select
72350 2a 29 3b 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b  *);  /* Callback
72360 20 66 6f 72 20 53 45 4c 45 43 54 73 20 2a 2f 0a   for SELECTs */.
72370 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b    Parse *pParse;
72380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72390 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
723a0 61 72 73 65 72 20 63 6f 6e 74 65 78 74 2e 20 20  arser context.  
723b0 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 20 20  */.  union {    
723c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
723d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
723e0 2a 20 45 78 74 72 61 20 64 61 74 61 20 66 6f 72  * Extra data for
723f0 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 20   callback */.   
72400 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e   NameContext *pN
72410 43 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  C;              
72420 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
72430 61 6d 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  aming context */
72440 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20  .    int i;     
72450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72470 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65  /* Integer value
72480 20 2a 2f 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f   */.  } u;.};../
72490 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72  * Forward declar
724a0 61 74 69 6f 6e 73 20 2a 2f 0a 53 51 4c 49 54 45  ations */.SQLITE
724b0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
724c0 69 74 65 33 57 61 6c 6b 45 78 70 72 28 57 61 6c  ite3WalkExpr(Wal
724d0 6b 65 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51  ker*, Expr*);.SQ
724e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
724f0 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72   sqlite3WalkExpr
72500 4c 69 73 74 28 57 61 6c 6b 65 72 2a 2c 20 45 78  List(Walker*, Ex
72510 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45  prList*);.SQLITE
72520 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
72530 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 57  ite3WalkSelect(W
72540 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29  alker*, Select*)
72550 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
72560 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b   int sqlite3Walk
72570 53 65 6c 65 63 74 45 78 70 72 28 57 61 6c 6b 65  SelectExpr(Walke
72580 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51  r*, Select*);.SQ
72590 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
725a0 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65   sqlite3WalkSele
725b0 63 74 46 72 6f 6d 28 57 61 6c 6b 65 72 2a 2c 20  ctFrom(Walker*, 
725c0 53 65 6c 65 63 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  Select*);../*.**
725d0 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f   Return code fro
725e0 6d 20 74 68 65 20 70 61 72 73 65 2d 74 72 65 65  m the parse-tree
725f0 20 77 61 6c 6b 69 6e 67 20 70 72 69 6d 69 74 69   walking primiti
72600 76 65 73 20 61 6e 64 20 74 68 65 69 72 0a 2a 2a  ves and their.**
72610 20 63 61 6c 6c 62 61 63 6b 73 2e 0a 2a 2f 0a 23   callbacks..*/.#
72620 64 65 66 69 6e 65 20 57 52 43 5f 43 6f 6e 74 69  define WRC_Conti
72630 6e 75 65 20 20 20 20 30 0a 23 64 65 66 69 6e 65  nue    0.#define
72640 20 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20 20   WRC_Prune      
72650 20 31 0a 23 64 65 66 69 6e 65 20 57 52 43 5f 41   1.#define WRC_A
72660 62 6f 72 74 20 20 20 20 20 20 20 32 0a 0a 2f 2a  bort       2../*
72670 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 7a 49 6e  .** Assuming zIn
72680 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66   points to the f
72690 69 72 73 74 20 62 79 74 65 20 6f 66 20 61 20 55  irst byte of a U
726a0 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2c 0a  TF-8 character,.
726b0 2a 2a 20 61 64 76 61 6e 63 65 20 7a 49 6e 20 74  ** advance zIn t
726c0 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66  o point to the f
726d0 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65  irst byte of the
726e0 20 6e 65 78 74 20 55 54 46 2d 38 20 63 68 61 72   next UTF-8 char
726f0 61 63 74 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e  acter..*/.#defin
72700 65 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54  e SQLITE_SKIP_UT
72710 46 38 28 7a 49 6e 29 20 7b 20 20 20 20 20 20 20  F8(zIn) {       
72720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72730 20 5c 0a 20 20 69 66 28 20 28 2a 28 7a 49 6e 2b   \.  if( (*(zIn+
72740 2b 29 29 3e 3d 30 78 63 30 20 29 7b 20 20 20 20  +))>=0xc0 ){    
72750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72760 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
72770 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20 26 20 30  while( (*zIn & 0
72780 78 63 30 29 3d 3d 30 78 38 30 20 29 7b 20 7a 49  xc0)==0x80 ){ zI
72790 6e 2b 2b 3b 20 7d 20 20 20 20 20 20 20 20 20 20  n++; }          
727a0 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20     \.  }        
727b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
727c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
727d0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a              \.}.
727e0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54  ./*.** The SQLIT
727f0 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20 6d  E_CORRUPT_BKPT m
72800 61 63 72 6f 20 63 61 6e 20 62 65 20 65 69 74 68  acro can be eith
72810 65 72 20 61 20 63 6f 6e 73 74 61 6e 74 20 28 66  er a constant (f
72820 6f 72 20 70 72 6f 64 75 63 74 69 6f 6e 0a 2a 2a  or production.**
72830 20 62 75 69 6c 64 73 29 20 6f 72 20 61 20 66 75   builds) or a fu
72840 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 28 66 6f 72  nction call (for
72850 20 64 65 62 75 67 67 69 6e 67 29 2e 20 20 49 66   debugging).  If
72860 20 69 74 20 69 73 20 61 20 66 75 6e 63 74 69 6f   it is a functio
72870 6e 20 63 61 6c 6c 2c 0a 2a 2a 20 69 74 20 61 6c  n call,.** it al
72880 6c 6f 77 73 20 74 68 65 20 6f 70 65 72 61 74 6f  lows the operato
72890 72 20 74 6f 20 73 65 74 20 61 20 62 72 65 61 6b  r to set a break
728a0 70 6f 69 6e 74 20 61 74 20 74 68 65 20 73 70 6f  point at the spo
728b0 74 20 77 68 65 72 65 20 64 61 74 61 62 61 73 65  t where database
728c0 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 20 69  .** corruption i
728d0 73 20 66 69 72 73 74 20 64 65 74 65 63 74 65 64  s first detected
728e0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
728f0 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f  TE_DEBUG.SQLITE_
72900 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
72910 6c 69 74 65 33 43 6f 72 72 75 70 74 28 76 6f 69  lite3Corrupt(voi
72920 64 29 3b 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  d);.# define SQL
72930 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
72940 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28   sqlite3Corrupt(
72950 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ).#else.# define
72960 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
72970 42 4b 50 54 20 53 51 4c 49 54 45 5f 43 4f 52 52  BKPT SQLITE_CORR
72980 55 50 54 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  UPT.#endif../*.*
72990 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74  * Internal funct
729a0 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65 73 0a 2a  ion prototypes.*
729b0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
729c0 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 49   int sqlite3StrI
729d0 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  Cmp(const char *
729e0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b  , const char *);
729f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
72a00 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 4e 49  int sqlite3StrNI
72a10 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  Cmp(const char *
72a20 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20  , const char *, 
72a30 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
72a40 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
72a50 49 73 4e 75 6d 62 65 72 28 63 6f 6e 73 74 20 63  IsNumber(const c
72a60 68 61 72 2a 2c 20 69 6e 74 2a 2c 20 75 38 29 3b  har*, int*, u8);
72a70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
72a80 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 6c 65  int sqlite3Strle
72a90 6e 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73  n(sqlite3*, cons
72aa0 74 20 63 68 61 72 2a 29 3b 0a 0a 53 51 4c 49 54  t char*);..SQLIT
72ab0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
72ac0 6c 69 74 65 33 4d 61 6c 6c 6f 63 49 6e 69 74 28  lite3MallocInit(
72ad0 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52  void);.SQLITE_PR
72ae0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
72af0 65 33 4d 61 6c 6c 6f 63 45 6e 64 28 76 6f 69 64  e3MallocEnd(void
72b00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
72b10 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d  E void *sqlite3M
72b20 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49  alloc(int);.SQLI
72b30 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
72b40 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65  *sqlite3MallocZe
72b50 72 6f 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  ro(int);.SQLITE_
72b60 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
72b70 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
72b80 6f 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29  o(sqlite3*, int)
72b90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
72ba0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62   void *sqlite3Db
72bb0 4d 61 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74 65  MallocRaw(sqlite
72bc0 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  3*, int);.SQLITE
72bd0 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
72be0 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 73  qlite3DbStrDup(s
72bf0 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
72c00 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ar*);.SQLITE_PRI
72c10 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
72c20 65 33 44 62 53 74 72 4e 44 75 70 28 73 71 6c 69  e3DbStrNDup(sqli
72c30 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
72c40 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
72c50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
72c60 69 74 65 33 52 65 61 6c 6c 6f 63 28 76 6f 69 64  ite3Realloc(void
72c70 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
72c80 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
72c90 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72  lite3DbReallocOr
72ca0 46 72 65 65 28 73 71 6c 69 74 65 33 20 2a 2c 20  Free(sqlite3 *, 
72cb0 76 6f 69 64 20 2a 2c 20 69 6e 74 29 3b 0a 53 51  void *, int);.SQ
72cc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
72cd0 64 20 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c  d *sqlite3DbReal
72ce0 6c 6f 63 28 73 71 6c 69 74 65 33 20 2a 2c 20 76  loc(sqlite3 *, v
72cf0 6f 69 64 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  oid *, int);.SQL
72d00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
72d10 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 73   sqlite3DbFree(s
72d20 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29 3b  qlite3*, void*);
72d30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
72d40 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  int sqlite3Mallo
72d50 63 53 69 7a 65 28 76 6f 69 64 2a 29 3b 0a 53 51  cSize(void*);.SQ
72d60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
72d70 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
72d80 53 69 7a 65 28 73 71 6c 69 74 65 33 2a 2c 20 76  Size(sqlite3*, v
72d90 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  oid*);.SQLITE_PR
72da0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
72db0 74 65 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63  te3ScratchMalloc
72dc0 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  (int);.SQLITE_PR
72dd0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
72de0 65 33 53 63 72 61 74 63 68 46 72 65 65 28 76 6f  e3ScratchFree(vo
72df0 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  id*);.SQLITE_PRI
72e00 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
72e10 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e 74  e3PageMalloc(int
72e20 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
72e30 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
72e40 67 65 46 72 65 65 28 76 6f 69 64 2a 29 3b 0a 53  geFree(void*);.S
72e50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
72e60 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74  id sqlite3MemSet
72e70 44 65 66 61 75 6c 74 28 76 6f 69 64 29 3b 0a 53  Default(void);.S
72e80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
72e90 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  nst sqlite3_mem_
72ea0 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33  methods *sqlite3
72eb0 4d 65 6d 47 65 74 4d 65 6d 73 79 73 33 28 76 6f  MemGetMemsys3(vo
72ec0 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  id);.SQLITE_PRIV
72ed0 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ATE const sqlite
72ee0 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73  3_mem_methods *s
72ef0 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73  qlite3MemGetMems
72f00 79 73 35 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  ys5(void);.SQLIT
72f10 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
72f20 71 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c  qlite3BenignMall
72f30 6f 63 48 6f 6f 6b 73 28 76 6f 69 64 20 28 2a 29  ocHooks(void (*)
72f40 28 76 6f 69 64 29 2c 20 76 6f 69 64 20 28 2a 29  (void), void (*)
72f50 28 76 6f 69 64 29 29 3b 0a 53 51 4c 49 54 45 5f  (void));.SQLITE_
72f60 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
72f70 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 76  te3MemoryAlarm(v
72f80 6f 69 64 20 28 2a 29 28 76 6f 69 64 2a 2c 20 73  oid (*)(void*, s
72f90 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 69 6e  qlite3_int64, in
72fa0 74 29 2c 20 76 6f 69 64 2a 2c 20 73 71 6c 69 74  t), void*, sqlit
72fb0 65 33 5f 69 6e 74 36 34 29 3b 0a 0a 23 69 66 6e  e3_int64);..#ifn
72fc0 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  def SQLITE_MUTEX
72fd0 5f 4f 4d 49 54 0a 53 51 4c 49 54 45 5f 50 52 49  _OMIT.SQLITE_PRI
72fe0 56 41 54 45 20 20 20 73 71 6c 69 74 65 33 5f 6d  VATE   sqlite3_m
72ff0 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71  utex_methods *sq
73000 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65  lite3DefaultMute
73010 78 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f  x(void);.SQLITE_
73020 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65  PRIVATE   sqlite
73030 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33  3_mutex *sqlite3
73040 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 29 3b  MutexAlloc(int);
73050 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
73060 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 74    int sqlite3Mut
73070 65 78 49 6e 69 74 28 76 6f 69 64 29 3b 0a 53 51  exInit(void);.SQ
73080 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
73090 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65 78 45  nt sqlite3MutexE
730a0 6e 64 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69 66  nd(void);.#endif
730b0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
730c0 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 61 74   int sqlite3Stat
730d0 75 73 56 61 6c 75 65 28 69 6e 74 29 3b 0a 53 51  usValue(int);.SQ
730e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
730f0 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41  d sqlite3StatusA
73100 64 64 28 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  dd(int, int);.SQ
73110 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
73120 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53  d sqlite3StatusS
73130 65 74 28 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 53  et(int, int);..S
73140 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
73150 74 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 64  t sqlite3IsNaN(d
73160 6f 75 62 6c 65 29 3b 0a 0a 53 51 4c 49 54 45 5f  ouble);..SQLITE_
73170 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
73180 69 74 65 33 56 58 50 72 69 6e 74 66 28 53 74 72  ite3VXPrintf(Str
73190 41 63 63 75 6d 2a 2c 20 69 6e 74 2c 20 63 6f 6e  Accum*, int, con
731a0 73 74 20 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73  st char*, va_lis
731b0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
731c0 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
731d0 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33 2a  MPrintf(sqlite3*
731e0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e  ,const char*, ..
731f0 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .);.SQLITE_PRIVA
73200 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
73210 56 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33  VMPrintf(sqlite3
73220 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76  *,const char*, v
73230 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f  a_list);.SQLITE_
73240 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71  PRIVATE char *sq
73250 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 73 71  lite3MAppendf(sq
73260 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2c 63 6f 6e  lite3*,char*,con
73270 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 23  st char*,...);.#
73280 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
73290 45 5f 54 45 53 54 29 20 7c 7c 20 64 65 66 69 6e  E_TEST) || defin
732a0 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
732b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
732c0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65    void sqlite3De
732d0 62 75 67 50 72 69 6e 74 66 28 63 6f 6e 73 74 20  bugPrintf(const 
732e0 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 65 6e  char*, ...);.#en
732f0 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28  dif.#if defined(
73300 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 53 51 4c  SQLITE_TEST).SQL
73310 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
73320 69 64 20 2a 73 71 6c 69 74 65 33 54 65 73 74 54  id *sqlite3TestT
73330 65 78 74 54 6f 50 74 72 28 63 6f 6e 73 74 20 63  extToPtr(const c
73340 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51  har*);.#endif.SQ
73350 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
73360 64 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69  d sqlite3SetStri
73370 6e 67 28 63 68 61 72 20 2a 2a 2c 20 73 71 6c 69  ng(char **, sqli
73380 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  te3*, const char
73390 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f  *, ...);.SQLITE_
733a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
733b0 69 74 65 33 45 72 72 6f 72 4d 73 67 28 50 61 72  ite3ErrorMsg(Par
733c0 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  se*, const char*
733d0 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50  , ...);.SQLITE_P
733e0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
733f0 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 50 61  te3ErrorClear(Pa
73400 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  rse*);.SQLITE_PR
73410 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
73420 65 33 44 65 71 75 6f 74 65 28 63 68 61 72 2a 29  e3Dequote(char*)
73430 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
73440 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 71   void sqlite3Deq
73450 75 6f 74 65 45 78 70 72 28 73 71 6c 69 74 65 33  uoteExpr(sqlite3
73460 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54  *, Expr*);.SQLIT
73470 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
73480 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65  lite3KeywordCode
73490 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20  (const unsigned 
734a0 63 68 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  char*, int);.SQL
734b0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
734c0 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72  sqlite3RunParser
734d0 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63  (Parse*, const c
734e0 68 61 72 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a  har*, char **);.
734f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
73500 6f 69 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73  oid sqlite3Finis
73510 68 43 6f 64 69 6e 67 28 50 61 72 73 65 2a 29 3b  hCoding(Parse*);
73520 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
73530 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 65  int sqlite3GetTe
73540 6d 70 52 65 67 28 50 61 72 73 65 2a 29 3b 0a 53  mpReg(Parse*);.S
73550 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
73560 69 64 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  id sqlite3Releas
73570 65 54 65 6d 70 52 65 67 28 50 61 72 73 65 2a 2c  eTempReg(Parse*,
73580 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
73590 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
735a0 47 65 74 54 65 6d 70 52 61 6e 67 65 28 50 61 72  GetTempRange(Par
735b0 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  se*,int);.SQLITE
735c0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
735d0 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
735e0 52 61 6e 67 65 28 50 61 72 73 65 2a 2c 69 6e 74  Range(Parse*,int
735f0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
73600 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
73610 74 65 33 45 78 70 72 28 73 71 6c 69 74 65 33 2a  te3Expr(sqlite3*
73620 2c 20 69 6e 74 2c 20 45 78 70 72 2a 2c 20 45 78  , int, Expr*, Ex
73630 70 72 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b 65 6e  pr*, const Token
73640 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
73650 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
73660 50 45 78 70 72 28 50 61 72 73 65 2a 2c 20 69 6e  PExpr(Parse*, in
73670 74 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 2c  t, Expr*, Expr*,
73680 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a   const Token*);.
73690 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45  SQLITE_PRIVATE E
736a0 78 70 72 20 2a 73 71 6c 69 74 65 33 52 65 67 69  xpr *sqlite3Regi
736b0 73 74 65 72 45 78 70 72 28 50 61 72 73 65 2a 2c  sterExpr(Parse*,
736c0 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
736d0 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71  PRIVATE Expr *sq
736e0 6c 69 74 65 33 45 78 70 72 41 6e 64 28 73 71 6c  lite3ExprAnd(sql
736f0 69 74 65 33 2a 2c 45 78 70 72 2a 2c 20 45 78 70  ite3*,Expr*, Exp
73700 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
73710 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
73720 45 78 70 72 53 70 61 6e 28 45 78 70 72 2a 2c 54  ExprSpan(Expr*,T
73730 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53  oken*,Token*);.S
73740 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78  QLITE_PRIVATE Ex
73750 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 46  pr *sqlite3ExprF
73760 75 6e 63 74 69 6f 6e 28 50 61 72 73 65 2a 2c 45  unction(Parse*,E
73770 78 70 72 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a  xprList*, Token*
73780 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
73790 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
737a0 70 72 41 73 73 69 67 6e 56 61 72 4e 75 6d 62 65  prAssignVarNumbe
737b0 72 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29  r(Parse*, Expr*)
737c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
737d0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
737e0 72 43 6c 65 61 72 28 73 71 6c 69 74 65 33 2a 2c  rClear(sqlite3*,
737f0 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
73800 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
73810 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 73  ite3ExprDelete(s
73820 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 2a 29 3b  qlite3*, Expr*);
73830 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
73840 45 78 70 72 4c 69 73 74 20 2a 73 71 6c 69 74 65  ExprList *sqlite
73850 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
73860 50 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a  Parse*,ExprList*
73870 2c 45 78 70 72 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a  ,Expr*,Token*);.
73880 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
73890 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c  oid sqlite3ExprL
738a0 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65  istDelete(sqlite
738b0 33 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a  3*, ExprList*);.
738c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
738d0 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 28 73  nt sqlite3Init(s
738e0 71 6c 69 74 65 33 2a 2c 20 63 68 61 72 2a 2a 29  qlite3*, char**)
738f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
73900 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74   int sqlite3Init
73910 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64 2a 2c 20  Callback(void*, 
73920 69 6e 74 2c 20 63 68 61 72 2a 2a 2c 20 63 68 61  int, char**, cha
73930 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  r**);.SQLITE_PRI
73940 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
73950 33 50 72 61 67 6d 61 28 50 61 72 73 65 2a 2c 54  3Pragma(Parse*,T
73960 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b  oken*,Token*,Tok
73970 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  en*,int);.SQLITE
73980 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
73990 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e  lite3ResetIntern
739a0 61 6c 53 63 68 65 6d 61 28 73 71 6c 69 74 65 33  alSchema(sqlite3
739b0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
739c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
739d0 69 74 65 33 42 65 67 69 6e 50 61 72 73 65 28 50  ite3BeginParse(P
739e0 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  arse*,int);.SQLI
739f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
73a00 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74  sqlite3CommitInt
73a10 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 73 71 6c  ernalChanges(sql
73a20 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ite3*);.SQLITE_P
73a30 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71  RIVATE Table *sq
73a40 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66  lite3ResultSetOf
73a50 53 65 6c 65 63 74 28 50 61 72 73 65 2a 2c 53 65  Select(Parse*,Se
73a60 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  lect*);.SQLITE_P
73a70 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
73a80 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61 62  te3OpenMasterTab
73a90 6c 65 28 50 61 72 73 65 20 2a 2c 20 69 6e 74 29  le(Parse *, int)
73aa0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
73ab0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61   void sqlite3Sta
73ac0 72 74 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 54  rtTable(Parse*,T
73ad0 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74  oken*,Token*,int
73ae0 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53  ,int,int,int);.S
73af0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
73b00 69 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c  id sqlite3AddCol
73b10 75 6d 6e 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e  umn(Parse*,Token
73b20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
73b30 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
73b40 64 64 4e 6f 74 4e 75 6c 6c 28 50 61 72 73 65 2a  ddNotNull(Parse*
73b50 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
73b60 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
73b70 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65 79  te3AddPrimaryKey
73b80 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73  (Parse*, ExprLis
73b90 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e  t*, int, int, in
73ba0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
73bb0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
73bc0 64 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e  ddCheckConstrain
73bd0 74 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29  t(Parse*, Expr*)
73be0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
73bf0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64   void sqlite3Add
73c00 43 6f 6c 75 6d 6e 54 79 70 65 28 50 61 72 73 65  ColumnType(Parse
73c10 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  *,Token*);.SQLIT
73c20 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
73c30 71 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c 74  qlite3AddDefault
73c40 56 61 6c 75 65 28 50 61 72 73 65 2a 2c 45 78 70  Value(Parse*,Exp
73c50 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
73c60 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
73c70 41 64 64 43 6f 6c 6c 61 74 65 54 79 70 65 28 50  AddCollateType(P
73c80 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a  arse*, Token*);.
73c90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
73ca0 6f 69 64 20 73 71 6c 69 74 65 33 45 6e 64 54 61  oid sqlite3EndTa
73cb0 62 6c 65 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e  ble(Parse*,Token
73cc0 2a 2c 54 6f 6b 65 6e 2a 2c 53 65 6c 65 63 74 2a  *,Token*,Select*
73cd0 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
73ce0 54 45 20 42 69 74 76 65 63 20 2a 73 71 6c 69 74  TE Bitvec *sqlit
73cf0 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 75  e3BitvecCreate(u
73d00 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  32);.SQLITE_PRIV
73d10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
73d20 69 74 76 65 63 54 65 73 74 28 42 69 74 76 65 63  itvecTest(Bitvec
73d30 2a 2c 20 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f  *, u32);.SQLITE_
73d40 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
73d50 74 65 33 42 69 74 76 65 63 53 65 74 28 42 69 74  te3BitvecSet(Bit
73d60 76 65 63 2a 2c 20 75 33 32 29 3b 0a 53 51 4c 49  vec*, u32);.SQLI
73d70 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
73d80 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65  sqlite3BitvecCle
73d90 61 72 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29  ar(Bitvec*, u32)
73da0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
73db0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74   void sqlite3Bit
73dc0 76 65 63 44 65 73 74 72 6f 79 28 42 69 74 76 65  vecDestroy(Bitve
73dd0 63 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  c*);.SQLITE_PRIV
73de0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
73df0 69 74 76 65 63 42 75 69 6c 74 69 6e 54 65 73 74  itvecBuiltinTest
73e00 28 69 6e 74 2c 69 6e 74 2a 29 3b 0a 0a 53 51 4c  (int,int*);..SQL
73e10 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
73e20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 56 69   sqlite3CreateVi
73e30 65 77 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a  ew(Parse*,Token*
73e40 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53  ,Token*,Token*,S
73e50 65 6c 65 63 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b  elect*,int,int);
73e60 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
73e70 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29  QLITE_OMIT_VIEW)
73e80 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
73e90 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
73ea0 54 41 42 4c 45 29 0a 53 51 4c 49 54 45 5f 50 52  TABLE).SQLITE_PR
73eb0 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
73ec0 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e  te3ViewGetColumn
73ed0 4e 61 6d 65 73 28 50 61 72 73 65 2a 2c 54 61 62  Names(Parse*,Tab
73ee0 6c 65 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65  le*);.#else.# de
73ef0 66 69 6e 65 20 73 71 6c 69 74 65 33 56 69 65 77  fine sqlite3View
73f00 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 41  GetColumnNames(A
73f10 2c 42 29 20 30 0a 23 65 6e 64 69 66 0a 0a 53 51  ,B) 0.#endif..SQ
73f20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
73f30 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 61 62  d sqlite3DropTab
73f40 6c 65 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69  le(Parse*, SrcLi
73f50 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  st*, int, int);.
73f60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
73f70 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74  oid sqlite3Delet
73f80 65 54 61 62 6c 65 28 54 61 62 6c 65 2a 29 3b 0a  eTable(Table*);.
73f90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
73fa0 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 73 65 72  oid sqlite3Inser
73fb0 74 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73  t(Parse*, SrcLis
73fc0 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 53  t*, ExprList*, S
73fd0 65 6c 65 63 74 2a 2c 20 49 64 4c 69 73 74 2a 2c  elect*, IdList*,
73fe0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
73ff0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
74000 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74 65  te3ArrayAllocate
74010 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 2a 2c  (sqlite3*,void*,
74020 69 6e 74 2c 69 6e 74 2c 69 6e 74 2a 2c 69 6e 74  int,int,int*,int
74030 2a 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  *,int*);.SQLITE_
74040 50 52 49 56 41 54 45 20 49 64 4c 69 73 74 20 2a  PRIVATE IdList *
74050 73 71 6c 69 74 65 33 49 64 4c 69 73 74 41 70 70  sqlite3IdListApp
74060 65 6e 64 28 73 71 6c 69 74 65 33 2a 2c 20 49 64  end(sqlite3*, Id
74070 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a  List*, Token*);.
74080 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
74090 6e 74 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74  nt sqlite3IdList
740a0 49 6e 64 65 78 28 49 64 4c 69 73 74 2a 2c 63 6f  Index(IdList*,co
740b0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
740c0 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69  TE_PRIVATE SrcLi
740d0 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69  st *sqlite3SrcLi
740e0 73 74 41 70 70 65 6e 64 28 73 71 6c 69 74 65 33  stAppend(sqlite3
740f0 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b  *, SrcList*, Tok
74100 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51  en*, Token*);.SQ
74110 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 63  LITE_PRIVATE Src
74120 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63  List *sqlite3Src
74130 4c 69 73 74 41 70 70 65 6e 64 46 72 6f 6d 54 65  ListAppendFromTe
74140 72 6d 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69  rm(Parse*, SrcLi
74150 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b  st*, Token*, Tok
74160 65 6e 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20  en*,.           
74170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
74180 20 20 20 20 20 20 20 20 20 20 20 54 6f 6b 65 6e             Token
74190 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 45 78 70 72  *, Select*, Expr
741a0 2a 2c 20 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c  *, IdList*);.SQL
741b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
741c0 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 49   sqlite3SrcListI
741d0 6e 64 65 78 65 64 42 79 28 50 61 72 73 65 20 2a  ndexedBy(Parse *
741e0 2c 20 53 72 63 4c 69 73 74 20 2a 2c 20 54 6f 6b  , SrcList *, Tok
741f0 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  en *);.SQLITE_PR
74200 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
74210 33 49 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75 70  3IndexedByLookup
74220 28 50 61 72 73 65 20 2a 2c 20 73 74 72 75 63 74  (Parse *, struct
74230 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 29   SrcList_item *)
74240 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74250 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63   void sqlite3Src
74260 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70  ListShiftJoinTyp
74270 65 28 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c  e(SrcList*);.SQL
74280 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
74290 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
742a0 73 73 69 67 6e 43 75 72 73 6f 72 73 28 50 61 72  ssignCursors(Par
742b0 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a  se*, SrcList*);.
742c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
742d0 6f 69 64 20 73 71 6c 69 74 65 33 49 64 4c 69 73  oid sqlite3IdLis
742e0 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 2a  tDelete(sqlite3*
742f0 2c 20 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c 49  , IdList*);.SQLI
74300 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
74310 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65  sqlite3SrcListDe
74320 6c 65 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 53  lete(sqlite3*, S
74330 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45  rcList*);.SQLITE
74340 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
74350 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78  lite3CreateIndex
74360 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54  (Parse*,Token*,T
74370 6f 6b 65 6e 2a 2c 53 72 63 4c 69 73 74 2a 2c 45  oken*,SrcList*,E
74380 78 70 72 4c 69 73 74 2a 2c 69 6e 74 2c 54 6f 6b  xprList*,int,Tok
74390 65 6e 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20  en*,.           
743a0 20 20 20 20 20 20 20 20 20 20 20 20 20 54 6f 6b               Tok
743b0 65 6e 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  en*, int, int);.
743c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
743d0 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 49  oid sqlite3DropI
743e0 6e 64 65 78 28 50 61 72 73 65 2a 2c 20 53 72 63  ndex(Parse*, Src
743f0 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  List*, int);.SQL
74400 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
74410 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 50 61  sqlite3Select(Pa
74420 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 53  rse*, Select*, S
74430 65 6c 65 63 74 44 65 73 74 2a 29 3b 0a 53 51 4c  electDest*);.SQL
74440 49 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65  ITE_PRIVATE Sele
74450 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63  ct *sqlite3Selec
74460 74 4e 65 77 28 50 61 72 73 65 2a 2c 45 78 70 72  tNew(Parse*,Expr
74470 4c 69 73 74 2a 2c 53 72 63 4c 69 73 74 2a 2c 45  List*,SrcList*,E
74480 78 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 0a  xpr*,ExprList*,.
74490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
744a0 20 20 20 20 20 20 20 20 20 45 78 70 72 2a 2c 45           Expr*,E
744b0 78 70 72 4c 69 73 74 2a 2c 69 6e 74 2c 45 78 70  xprList*,int,Exp
744c0 72 2a 2c 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54  r*,Expr*);.SQLIT
744d0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
744e0 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
744f0 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 53 65 6c  te(sqlite3*, Sel
74500 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ect*);.SQLITE_PR
74510 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c  IVATE Table *sql
74520 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75  ite3SrcListLooku
74530 70 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73  p(Parse*, SrcLis
74540 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
74550 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
74560 73 52 65 61 64 4f 6e 6c 79 28 50 61 72 73 65 2a  sReadOnly(Parse*
74570 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 29 3b 0a  , Table*, int);.
74580 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
74590 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65 6e 54  oid sqlite3OpenT
745a0 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 69 6e 74  able(Parse*, int
745b0 20 69 43 75 72 2c 20 69 6e 74 20 69 44 62 2c 20   iCur, int iDb, 
745c0 54 61 62 6c 65 2a 2c 20 69 6e 74 29 3b 0a 23 69  Table*, int);.#i
745d0 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
745e0 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44  _ENABLE_UPDATE_D
745f0 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 20  ELETE_LIMIT) && 
74600 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
74610 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a 53  OMIT_SUBQUERY).S
74620 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78  QLITE_PRIVATE Ex
74630 70 72 20 2a 73 71 6c 69 74 65 33 4c 69 6d 69 74  pr *sqlite3Limit
74640 57 68 65 72 65 28 50 61 72 73 65 20 2a 2c 20 53  Where(Parse *, S
74650 72 63 4c 69 73 74 20 2a 2c 20 45 78 70 72 20 2a  rcList *, Expr *
74660 2c 20 45 78 70 72 4c 69 73 74 20 2a 2c 20 45 78  , ExprList *, Ex
74670 70 72 20 2a 2c 20 45 78 70 72 20 2a 2c 20 63 68  pr *, Expr *, ch
74680 61 72 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51  ar *);.#endif.SQ
74690 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
746a0 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 46  d sqlite3DeleteF
746b0 72 6f 6d 28 50 61 72 73 65 2a 2c 20 53 72 63 4c  rom(Parse*, SrcL
746c0 69 73 74 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51  ist*, Expr*);.SQ
746d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
746e0 64 20 73 71 6c 69 74 65 33 55 70 64 61 74 65 28  d sqlite3Update(
746f0 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a  Parse*, SrcList*
74700 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 45 78 70  , ExprList*, Exp
74710 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  r*, int);.SQLITE
74720 5f 50 52 49 56 41 54 45 20 57 68 65 72 65 49 6e  _PRIVATE WhereIn
74730 66 6f 20 2a 73 71 6c 69 74 65 33 57 68 65 72 65  fo *sqlite3Where
74740 42 65 67 69 6e 28 50 61 72 73 65 2a 2c 20 53 72  Begin(Parse*, Sr
74750 63 4c 69 73 74 2a 2c 20 45 78 70 72 2a 2c 20 45  cList*, Expr*, E
74760 78 70 72 4c 69 73 74 2a 2a 2c 20 75 38 29 3b 0a  xprList**, u8);.
74770 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
74780 6f 69 64 20 73 71 6c 69 74 65 33 57 68 65 72 65  oid sqlite3Where
74790 45 6e 64 28 57 68 65 72 65 49 6e 66 6f 2a 29 3b  End(WhereInfo*);
747a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
747b0 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43  int sqlite3ExprC
747c0 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 50 61 72  odeGetColumn(Par
747d0 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74  se*, Table*, int
747e0 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29  , int, int, int)
747f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74800 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
74810 72 43 6f 64 65 4d 6f 76 65 28 50 61 72 73 65 2a  rCodeMove(Parse*
74820 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29  , int, int, int)
74830 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74840 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
74850 72 43 6f 64 65 43 6f 70 79 28 50 61 72 73 65 2a  rCodeCopy(Parse*
74860 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29  , int, int, int)
74870 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74880 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
74890 72 43 6c 65 61 72 43 6f 6c 75 6d 6e 43 61 63 68  rClearColumnCach
748a0 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a  e(Parse*, int);.
748b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
748c0 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
748d0 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e  acheAffinityChan
748e0 67 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20  ge(Parse*, int, 
748f0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
74900 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
74910 45 78 70 72 57 72 69 74 61 62 6c 65 52 65 67 69  ExprWritableRegi
74920 73 74 65 72 28 50 61 72 73 65 2a 2c 69 6e 74 2c  ster(Parse*,int,
74930 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
74940 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
74950 33 45 78 70 72 48 61 72 64 43 6f 70 79 28 50 61  3ExprHardCopy(Pa
74960 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53  rse*,int,int);.S
74970 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
74980 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  t sqlite3ExprCod
74990 65 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c  e(Parse*, Expr*,
749a0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
749b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
749c0 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 50 61  3ExprCodeTemp(Pa
749d0 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74  rse*, Expr*, int
749e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
749f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78  TE int sqlite3Ex
74a00 70 72 43 6f 64 65 54 61 72 67 65 74 28 50 61 72  prCodeTarget(Par
74a10 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29  se*, Expr*, int)
74a20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74a30 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
74a40 43 6f 64 65 41 6e 64 43 61 63 68 65 28 50 61 72  CodeAndCache(Par
74a50 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29  se*, Expr*, int)
74a60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74a70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
74a80 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28 50  rCodeConstants(P
74a90 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53  arse*, Expr*);.S
74aa0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
74ab0 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  t sqlite3ExprCod
74ac0 65 45 78 70 72 4c 69 73 74 28 50 61 72 73 65 2a  eExprList(Parse*
74ad0 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74  , ExprList*, int
74ae0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
74af0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
74b00 74 65 33 45 78 70 72 49 66 54 72 75 65 28 50 61  te3ExprIfTrue(Pa
74b10 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74  rse*, Expr*, int
74b20 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
74b30 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
74b40 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 50  te3ExprIfFalse(P
74b50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e  arse*, Expr*, in
74b60 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
74b70 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73  PRIVATE Table *s
74b80 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
74b90 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
74ba0 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  har*, const char
74bb0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
74bc0 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65  TE Table *sqlite
74bd0 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 50 61 72  3LocateTable(Par
74be0 73 65 2a 2c 69 6e 74 20 69 73 56 69 65 77 2c 63  se*,int isView,c
74bf0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73  onst char*, cons
74c00 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45  t char*);.SQLITE
74c10 5f 50 52 49 56 41 54 45 20 49 6e 64 65 78 20 2a  _PRIVATE Index *
74c20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78  sqlite3FindIndex
74c30 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
74c40 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61  char*, const cha
74c50 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
74c60 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
74c70 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54  UnlinkAndDeleteT
74c80 61 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c 69 6e  able(sqlite3*,in
74c90 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  t,const char*);.
74ca0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
74cb0 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e  oid sqlite3Unlin
74cc0 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 28  kAndDeleteIndex(
74cd0 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e  sqlite3*,int,con
74ce0 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54  st char*);.SQLIT
74cf0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
74d00 71 6c 69 74 65 33 56 61 63 75 75 6d 28 50 61 72  qlite3Vacuum(Par
74d10 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  se*);.SQLITE_PRI
74d20 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
74d30 52 75 6e 56 61 63 75 75 6d 28 63 68 61 72 2a 2a  RunVacuum(char**
74d40 2c 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c  , sqlite3*);.SQL
74d50 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
74d60 20 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f   *sqlite3NameFro
74d70 6d 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33 2a 2c  mToken(sqlite3*,
74d80 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45   Token*);.SQLITE
74d90 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
74da0 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28  ite3ExprCompare(
74db0 45 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53  Expr*, Expr*);.S
74dc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
74dd0 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e  id sqlite3ExprAn
74de0 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 28  alyzeAggregates(
74df0 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 78  NameContext*, Ex
74e00 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
74e10 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
74e20 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c  3ExprAnalyzeAggL
74e30 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a  ist(NameContext*
74e40 2c 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c  ,ExprList*);.SQL
74e50 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65  ITE_PRIVATE Vdbe
74e60 20 2a 73 71 6c 69 74 65 33 47 65 74 56 64 62 65   *sqlite3GetVdbe
74e70 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45  (Parse*);.SQLITE
74e80 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73  _PRIVATE Expr *s
74e90 71 6c 69 74 65 33 43 72 65 61 74 65 49 64 45 78  qlite3CreateIdEx
74ea0 70 72 28 50 61 72 73 65 20 2a 2c 20 63 6f 6e 73  pr(Parse *, cons
74eb0 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45  t char*);.SQLITE
74ec0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
74ed0 6c 69 74 65 33 50 72 6e 67 53 61 76 65 53 74 61  lite3PrngSaveSta
74ee0 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45  te(void);.SQLITE
74ef0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
74f00 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65  lite3PrngRestore
74f10 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c  State(void);.SQL
74f20 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
74f30 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 65   sqlite3PrngRese
74f40 74 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51  tState(void);.SQ
74f50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
74f60 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63  d sqlite3Rollbac
74f70 6b 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a  kAll(sqlite3*);.
74f80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
74f90 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 56  oid sqlite3CodeV
74fa0 65 72 69 66 79 53 63 68 65 6d 61 28 50 61 72 73  erifySchema(Pars
74fb0 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  e*, int);.SQLITE
74fc0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
74fd0 6c 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73 61  lite3BeginTransa
74fe0 63 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 6e  ction(Parse*, in
74ff0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
75000 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
75010 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e  ommitTransaction
75020 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45  (Parse*);.SQLITE
75030 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
75040 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61  lite3RollbackTra
75050 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 29  nsaction(Parse*)
75060 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
75070 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
75080 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 70 72 2a  IsConstant(Expr*
75090 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
750a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
750b0 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f  rIsConstantNotJo
750c0 69 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54  in(Expr*);.SQLIT
750d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
750e0 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
750f0 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 45 78  antOrFunction(Ex
75100 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
75110 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
75120 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 45 78  ExprIsInteger(Ex
75130 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49  pr*, int*);.SQLI
75140 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
75150 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 63 6f  qlite3IsRowid(co
75160 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
75170 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
75180 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52  sqlite3GenerateR
75190 6f 77 44 65 6c 65 74 65 28 50 61 72 73 65 2a 2c  owDelete(Parse*,
751a0 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e   Table*, int, in
751b0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
751c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
751d0 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 49  ite3GenerateRowI
751e0 6e 64 65 78 44 65 6c 65 74 65 28 50 61 72 73 65  ndexDelete(Parse
751f0 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20  *, Table*, int, 
75200 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  int*);.SQLITE_PR
75210 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
75220 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78 4b 65  3GenerateIndexKe
75230 79 28 50 61 72 73 65 2a 2c 20 49 6e 64 65 78 2a  y(Parse*, Index*
75240 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29  , int, int, int)
75250 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
75260 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65 6e   void sqlite3Gen
75270 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43  erateConstraintC
75280 68 65 63 6b 73 28 50 61 72 73 65 2a 2c 54 61 62  hecks(Parse*,Tab
75290 6c 65 2a 2c 69 6e 74 2c 69 6e 74 2c 0a 20 20 20  le*,int,int,.   
752a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
752b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
752c0 20 20 69 6e 74 2a 2c 69 6e 74 2c 69 6e 74 2c 69    int*,int,int,i
752d0 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  nt,int);.SQLITE_
752e0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
752f0 69 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73 65  ite3CompleteInse
75300 72 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 54 61  rtion(Parse*, Ta
75310 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20  ble*, int, int, 
75320 69 6e 74 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  int*,int,int,int
75330 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
75340 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
75350 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64  3OpenTableAndInd
75360 69 63 65 73 28 50 61 72 73 65 2a 2c 20 54 61 62  ices(Parse*, Tab
75370 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  le*, int, int);.
75380 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
75390 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e  oid sqlite3Begin
753a0 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 50  WriteOperation(P
753b0 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29  arse*, int, int)
753c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
753d0 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78   Expr *sqlite3Ex
753e0 70 72 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 45  prDup(sqlite3*,E
753f0 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
75400 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
75410 65 33 54 6f 6b 65 6e 43 6f 70 79 28 73 71 6c 69  e3TokenCopy(sqli
75420 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 20 54 6f 6b  te3*,Token*, Tok
75430 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  en*);.SQLITE_PRI
75440 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73  VATE ExprList *s
75450 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75  qlite3ExprListDu
75460 70 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 4c  p(sqlite3*,ExprL
75470 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ist*);.SQLITE_PR
75480 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73  IVATE SrcList *s
75490 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70  qlite3SrcListDup
754a0 28 73 71 6c 69 74 65 33 2a 2c 53 72 63 4c 69 73  (sqlite3*,SrcLis
754b0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
754c0 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69  ATE IdList *sqli
754d0 74 65 33 49 64 4c 69 73 74 44 75 70 28 73 71 6c  te3IdListDup(sql
754e0 69 74 65 33 2a 2c 49 64 4c 69 73 74 2a 29 3b 0a  ite3*,IdList*);.
754f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53  SQLITE_PRIVATE S
75500 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53 65  elect *sqlite3Se
75510 6c 65 63 74 44 75 70 28 73 71 6c 69 74 65 33 2a  lectDup(sqlite3*
75520 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54  ,Select*);.SQLIT
75530 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
75540 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e 73  qlite3FuncDefIns
75550 65 72 74 28 46 75 6e 63 44 65 66 48 61 73 68 2a  ert(FuncDefHash*
75560 2c 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51 4c  , FuncDef*);.SQL
75570 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63  ITE_PRIVATE Func
75580 44 65 66 20 2a 73 71 6c 69 74 65 33 46 69 6e 64  Def *sqlite3Find
75590 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33  Function(sqlite3
755a0 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e  *,const char*,in
755b0 74 2c 69 6e 74 2c 75 38 2c 69 6e 74 29 3b 0a 53  t,int,u8,int);.S
755c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
755d0 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74  id sqlite3Regist
755e0 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74 69 6f  erBuiltinFunctio
755f0 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51  ns(sqlite3*);.SQ
75600 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
75610 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65  d sqlite3Registe
75620 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f  rDateTimeFunctio
75630 6e 73 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45  ns(void);.SQLITE
75640 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
75650 6c 69 74 65 33 52 65 67 69 73 74 65 72 47 6c 6f  lite3RegisterGlo
75660 62 61 6c 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69  balFunctions(voi
75670 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  d);.SQLITE_PRIVA
75680 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65  TE int sqlite3Ge
75690 74 42 75 69 6c 74 69 6e 46 75 6e 63 74 69 6f 6e  tBuiltinFunction
756a0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69  (const char *, i
756b0 6e 74 2c 20 46 75 6e 63 44 65 66 20 2a 2a 29 3b  nt, FuncDef **);
756c0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
756d0 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56  EBUG.SQLITE_PRIV
756e0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
756f0 33 53 61 66 65 74 79 4f 6e 28 73 71 6c 69 74 65  3SafetyOn(sqlite
75700 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  3*);.SQLITE_PRIV
75710 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
75720 33 53 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74  3SafetyOff(sqlit
75730 65 33 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65  e3*);.#else.# de
75740 66 69 6e 65 20 73 71 6c 69 74 65 33 53 61 66 65  fine sqlite3Safe
75750 74 79 4f 6e 28 41 29 20 30 0a 23 20 64 65 66 69  tyOn(A) 0.# defi
75760 6e 65 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  ne sqlite3Safety
75770 4f 66 66 28 41 29 20 30 0a 23 65 6e 64 69 66 0a  Off(A) 0.#endif.
75780 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
75790 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  nt sqlite3Safety
757a0 43 68 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33 2a  CheckOk(sqlite3*
757b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
757c0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66  E int sqlite3Saf
757d0 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b  etyCheckSickOrOk
757e0 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49  (sqlite3*);.SQLI
757f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
75800 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f  sqlite3ChangeCoo
75810 6b 69 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29  kie(Parse*, int)
75820 3b 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  ;..#if !defined(
75830 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
75840 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
75850 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
75860 52 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  R).SQLITE_PRIVAT
75870 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61  E void sqlite3Ma
75880 74 65 72 69 61 6c 69 7a 65 56 69 65 77 28 50 61  terializeView(Pa
75890 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78  rse*, Table*, Ex
758a0 70 72 2a 2c 20 69 6e 74 29 3b 0a 23 65 6e 64 69  pr*, int);.#endi
758b0 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
758c0 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 53  E_OMIT_TRIGGER.S
758d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
758e0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69  void sqlite3Begi
758f0 6e 54 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c  nTrigger(Parse*,
75900 20 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69   Token*,Token*,i
75910 6e 74 2c 69 6e 74 2c 49 64 4c 69 73 74 2a 2c 53  nt,int,IdList*,S
75920 72 63 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20  rcList*,.       
75930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
75940 20 20 20 20 45 78 70 72 2a 2c 69 6e 74 2c 20 69      Expr*,int, i
75950 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
75960 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
75970 65 33 46 69 6e 69 73 68 54 72 69 67 67 65 72 28  e3FinishTrigger(
75980 50 61 72 73 65 2a 2c 20 54 72 69 67 67 65 72 53  Parse*, TriggerS
75990 74 65 70 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53  tep*, Token*);.S
759a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
759b0 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70  void sqlite3Drop
759c0 54 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20  Trigger(Parse*, 
759d0 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a  SrcList*, int);.
759e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
759f0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f   void sqlite3Dro
75a00 70 54 72 69 67 67 65 72 50 74 72 28 50 61 72 73  pTriggerPtr(Pars
75a10 65 2a 2c 20 54 72 69 67 67 65 72 2a 29 3b 0a 53  e*, Trigger*);.S
75a20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
75a30 69 6e 74 20 73 71 6c 69 74 65 33 54 72 69 67 67  int sqlite3Trigg
75a40 65 72 73 45 78 69 73 74 28 50 61 72 73 65 2a 2c  ersExist(Parse*,
75a50 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 45 78   Table*, int, Ex
75a60 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45  prList*);.SQLITE
75a70 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
75a80 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69  qlite3CodeRowTri
75a90 67 67 65 72 28 50 61 72 73 65 2a 2c 20 69 6e 74  gger(Parse*, int
75aa0 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74  , ExprList*, int
75ab0 2c 20 54 61 62 6c 65 20 2a 2c 20 69 6e 74 2c 20  , Table *, int, 
75ac0 69 6e 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20  int, .          
75ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
75ae0 20 69 6e 74 2c 20 69 6e 74 2c 20 75 33 32 2a 2c   int, int, u32*,
75af0 20 75 33 32 2a 29 3b 0a 20 20 76 6f 69 64 20 73   u32*);.  void s
75b00 71 6c 69 74 65 56 69 65 77 54 72 69 67 67 65 72  qliteViewTrigger
75b10 73 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a  s(Parse*, Table*
75b20 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20 45 78  , Expr*, int, Ex
75b30 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45  prList*);.SQLITE
75b40 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
75b50 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
75b60 67 67 65 72 53 74 65 70 28 73 71 6c 69 74 65 33  ggerStep(sqlite3
75b70 2a 2c 20 54 72 69 67 67 65 72 53 74 65 70 2a 29  *, TriggerStep*)
75b80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
75b90 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a     TriggerStep *
75ba0 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 53 65  sqlite3TriggerSe
75bb0 6c 65 63 74 53 74 65 70 28 73 71 6c 69 74 65 33  lectStep(sqlite3
75bc0 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49  *,Select*);.SQLI
75bd0 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69  TE_PRIVATE   Tri
75be0 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65  ggerStep *sqlite
75bf0 33 54 72 69 67 67 65 72 49 6e 73 65 72 74 53 74  3TriggerInsertSt
75c00 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65  ep(sqlite3*,Toke
75c10 6e 2a 2c 20 49 64 4c 69 73 74 2a 2c 0a 20 20 20  n*, IdList*,.   
75c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
75c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
75c40 20 20 20 20 20 45 78 70 72 4c 69 73 74 2a 2c 53       ExprList*,S
75c50 65 6c 65 63 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c  elect*,int);.SQL
75c60 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72  ITE_PRIVATE   Tr
75c70 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74  iggerStep *sqlit
75c80 65 33 54 72 69 67 67 65 72 55 70 64 61 74 65 53  e3TriggerUpdateS
75c90 74 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b  tep(sqlite3*,Tok
75ca0 65 6e 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 45  en*,ExprList*, E
75cb0 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  xpr*, int);.SQLI
75cc0 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69  TE_PRIVATE   Tri
75cd0 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65  ggerStep *sqlite
75ce0 33 54 72 69 67 67 65 72 44 65 6c 65 74 65 53 74  3TriggerDeleteSt
75cf0 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65  ep(sqlite3*,Toke
75d00 6e 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49  n*, Expr*);.SQLI
75d10 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
75d20 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54  d sqlite3DeleteT
75d30 72 69 67 67 65 72 28 73 71 6c 69 74 65 33 2a 2c  rigger(sqlite3*,
75d40 20 54 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c 49   Trigger*);.SQLI
75d50 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
75d60 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41  d sqlite3UnlinkA
75d70 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 72 28  ndDeleteTrigger(
75d80 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e  sqlite3*,int,con
75d90 73 74 20 63 68 61 72 2a 29 3b 0a 23 65 6c 73 65  st char*);.#else
75da0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
75db0 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28 41  3TriggersExist(A
75dc0 2c 42 2c 43 2c 44 2c 45 2c 46 29 20 30 0a 23 20  ,B,C,D,E,F) 0.# 
75dd0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 65  define sqlite3De
75de0 6c 65 74 65 54 72 69 67 67 65 72 28 41 2c 42 29  leteTrigger(A,B)
75df0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
75e00 33 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28  3DropTriggerPtr(
75e10 41 2c 42 29 0a 23 20 64 65 66 69 6e 65 20 73 71  A,B).# define sq
75e20 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65  lite3UnlinkAndDe
75e30 6c 65 74 65 54 72 69 67 67 65 72 28 41 2c 42 2c  leteTrigger(A,B,
75e40 43 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  C).# define sqli
75e50 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65  te3CodeRowTrigge
75e60 72 28 41 2c 42 2c 43 2c 44 2c 45 2c 46 2c 47 2c  r(A,B,C,D,E,F,G,
75e70 48 2c 49 2c 4a 2c 4b 29 20 30 0a 23 65 6e 64 69  H,I,J,K) 0.#endi
75e80 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  f..SQLITE_PRIVAT
75e90 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 69  E int sqlite3Joi
75ea0 6e 54 79 70 65 28 50 61 72 73 65 2a 2c 20 54 6f  nType(Parse*, To
75eb0 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f  ken*, Token*, To
75ec0 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
75ed0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
75ee0 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e 4b  e3CreateForeignK
75ef0 65 79 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c  ey(Parse*, ExprL
75f00 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 45 78  ist*, Token*, Ex
75f10 70 72 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53  prList*, int);.S
75f20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
75f30 69 64 20 73 71 6c 69 74 65 33 44 65 66 65 72 46  id sqlite3DeferF
75f40 6f 72 65 69 67 6e 4b 65 79 28 50 61 72 73 65 2a  oreignKey(Parse*
75f50 2c 20 69 6e 74 29 3b 0a 23 69 66 6e 64 65 66 20  , int);.#ifndef 
75f60 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
75f70 4f 52 49 5a 41 54 49 4f 4e 0a 53 51 4c 49 54 45  ORIZATION.SQLITE
75f80 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
75f90 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64 28  sqlite3AuthRead(
75fa0 50 61 72 73 65 2a 2c 45 78 70 72 2a 2c 53 63 68  Parse*,Expr*,Sch
75fb0 65 6d 61 2a 2c 53 72 63 4c 69 73 74 2a 29 3b 0a  ema*,SrcList*);.
75fc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
75fd0 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75 74 68   int sqlite3Auth
75fe0 43 68 65 63 6b 28 50 61 72 73 65 2a 2c 69 6e 74  Check(Parse*,int
75ff0 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63  , const char*, c
76000 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73  onst char*, cons
76010 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45  t char*);.SQLITE
76020 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
76030 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65  sqlite3AuthConte
76040 78 74 50 75 73 68 28 50 61 72 73 65 2a 2c 20 41  xtPush(Parse*, A
76050 75 74 68 43 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e  uthContext*, con
76060 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54  st char*);.SQLIT
76070 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
76080 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74   sqlite3AuthCont
76090 65 78 74 50 6f 70 28 41 75 74 68 43 6f 6e 74 65  extPop(AuthConte
760a0 78 74 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65  xt*);.#else.# de
760b0 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68  fine sqlite3Auth
760c0 52 65 61 64 28 61 2c 62 2c 63 2c 64 29 0a 23 20  Read(a,b,c,d).# 
760d0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75  define sqlite3Au
760e0 74 68 43 68 65 63 6b 28 61 2c 62 2c 63 2c 64 2c  thCheck(a,b,c,d,
760f0 65 29 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a  e)    SQLITE_OK.
76100 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
76110 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28  AuthContextPush(
76120 61 2c 62 2c 63 29 0a 23 20 64 65 66 69 6e 65 20  a,b,c).# define 
76130 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65  sqlite3AuthConte
76140 78 74 50 6f 70 28 61 29 20 20 28 28 76 6f 69 64  xtPop(a)  ((void
76150 29 28 61 29 29 0a 23 65 6e 64 69 66 0a 53 51 4c  )(a)).#endif.SQL
76160 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
76170 20 73 71 6c 69 74 65 33 41 74 74 61 63 68 28 50   sqlite3Attach(P
76180 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 45 78  arse*, Expr*, Ex
76190 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c  pr*, Expr*);.SQL
761a0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
761b0 20 73 71 6c 69 74 65 33 44 65 74 61 63 68 28 50   sqlite3Detach(P
761c0 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53  arse*, Expr*);.S
761d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
761e0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 61  t sqlite3BtreeFa
761f0 63 74 6f 72 79 28 63 6f 6e 73 74 20 73 71 6c 69  ctory(const sqli
76200 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63  te3 *db, const c
76210 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 0a  har *zFilename,.
76220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76230 20 20 20 20 20 20 20 69 6e 74 20 6f 6d 69 74 4a         int omitJ
76240 6f 75 72 6e 61 6c 2c 20 69 6e 74 20 6e 43 61 63  ournal, int nCac
76250 68 65 2c 20 69 6e 74 20 66 6c 61 67 73 2c 20 42  he, int flags, B
76260 74 72 65 65 20 2a 2a 70 70 42 74 72 65 65 29 3b  tree **ppBtree);
76270 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
76280 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 49 6e  int sqlite3FixIn
76290 69 74 28 44 62 46 69 78 65 72 2a 2c 20 50 61 72  it(DbFixer*, Par
762a0 73 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20  se*, int, const 
762b0 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b  char*, const Tok
762c0 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  en*);.SQLITE_PRI
762d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
762e0 46 69 78 53 72 63 4c 69 73 74 28 44 62 46 69 78  FixSrcList(DbFix
762f0 65 72 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a  er*, SrcList*);.
76300 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
76310 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c  nt sqlite3FixSel
76320 65 63 74 28 44 62 46 69 78 65 72 2a 2c 20 53 65  ect(DbFixer*, Se
76330 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  lect*);.SQLITE_P
76340 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
76350 65 33 46 69 78 45 78 70 72 28 44 62 46 69 78 65  e3FixExpr(DbFixe
76360 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49  r*, Expr*);.SQLI
76370 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
76380 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69 73  qlite3FixExprLis
76390 74 28 44 62 46 69 78 65 72 2a 2c 20 45 78 70 72  t(DbFixer*, Expr
763a0 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  List*);.SQLITE_P
763b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
763c0 65 33 46 69 78 54 72 69 67 67 65 72 53 74 65 70  e3FixTriggerStep
763d0 28 44 62 46 69 78 65 72 2a 2c 20 54 72 69 67 67  (DbFixer*, Trigg
763e0 65 72 53 74 65 70 2a 29 3b 0a 53 51 4c 49 54 45  erStep*);.SQLITE
763f0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
76400 69 74 65 33 41 74 6f 46 28 63 6f 6e 73 74 20 63  ite3AtoF(const c
76410 68 61 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 2a 29  har *z, double*)
76420 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
76430 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 49   int sqlite3GetI
76440 6e 74 33 32 28 63 6f 6e 73 74 20 63 68 61 72 20  nt32(const char 
76450 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45  *, int*);.SQLITE
76460 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
76470 69 74 65 33 46 69 74 73 49 6e 36 34 42 69 74 73  ite3FitsIn64Bits
76480 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69  (const char *, i
76490 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
764a0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55  ATE int sqlite3U
764b0 74 66 31 36 42 79 74 65 4c 65 6e 28 63 6f 6e 73  tf16ByteLen(cons
764c0 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69  t void *pData, i
764d0 6e 74 20 6e 43 68 61 72 29 3b 0a 53 51 4c 49 54  nt nChar);.SQLIT
764e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
764f0 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e  lite3Utf8CharLen
76500 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61  (const char *pDa
76510 74 61 2c 20 69 6e 74 20 6e 42 79 74 65 29 3b 0a  ta, int nByte);.
76520 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
76530 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 52 65  nt sqlite3Utf8Re
76540 61 64 28 63 6f 6e 73 74 20 75 38 2a 2c 20 63 6f  ad(const u8*, co
76550 6e 73 74 20 75 38 2a 2c 20 63 6f 6e 73 74 20 75  nst u8*, const u
76560 38 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75  8**);../*.** Rou
76570 74 69 6e 65 73 20 74 6f 20 72 65 61 64 20 61 6e  tines to read an
76580 64 20 77 72 69 74 65 20 76 61 72 69 61 62 6c 65  d write variable
76590 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73  -length integers
765a0 2e 20 20 54 68 65 73 65 20 75 73 65 64 20 74 6f  .  These used to
765b0 0a 2a 2a 20 62 65 20 64 65 66 69 6e 65 64 20 6c  .** be defined l
765c0 6f 63 61 6c 6c 79 2c 20 62 75 74 20 6e 6f 77 20  ocally, but now 
765d0 77 65 20 75 73 65 20 74 68 65 20 76 61 72 69 6e  we use the varin
765e0 74 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68  t routines in th
765f0 65 20 75 74 69 6c 2e 63 0a 2a 2a 20 66 69 6c 65  e util.c.** file
76600 2e 20 20 43 6f 64 65 20 73 68 6f 75 6c 64 20 75  .  Code should u
76610 73 65 20 74 68 65 20 4d 41 43 52 4f 20 66 6f 72  se the MACRO for
76620 6d 73 20 62 65 6c 6f 77 2c 20 61 73 20 74 68 65  ms below, as the
76630 20 56 61 72 69 6e 74 33 32 20 76 65 72 73 69 6f   Varint32 versio
76640 6e 73 0a 2a 2a 20 61 72 65 20 63 6f 64 65 64 20  ns.** are coded 
76650 74 6f 20 61 73 73 75 6d 65 20 74 68 65 20 73 69  to assume the si
76660 6e 67 6c 65 20 62 79 74 65 20 63 61 73 65 20 69  ngle byte case i
76670 73 20 61 6c 72 65 61 64 79 20 68 61 6e 64 6c 65  s already handle
76680 64 20 28 77 68 69 63 68 20 0a 2a 2a 20 74 68 65  d (which .** the
76690 20 4d 41 43 52 4f 20 66 6f 72 6d 20 64 6f 65 73   MACRO form does
766a0 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
766b0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
766c0 50 75 74 56 61 72 69 6e 74 28 75 6e 73 69 67 6e  PutVarint(unsign
766d0 65 64 20 63 68 61 72 2a 2c 20 75 36 34 29 3b 0a  ed char*, u64);.
766e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
766f0 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56 61 72  nt sqlite3PutVar
76700 69 6e 74 33 32 28 75 6e 73 69 67 6e 65 64 20 63  int32(unsigned c
76710 68 61 72 2a 2c 20 75 33 32 29 3b 0a 53 51 4c 49  har*, u32);.SQLI
76720 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
76730 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28  qlite3GetVarint(
76740 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
76750 68 61 72 20 2a 2c 20 75 36 34 20 2a 29 3b 0a 53  har *, u64 *);.S
76760 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
76770 74 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69  t sqlite3GetVari
76780 6e 74 33 32 28 63 6f 6e 73 74 20 75 6e 73 69 67  nt32(const unsig
76790 6e 65 64 20 63 68 61 72 20 2a 2c 20 75 33 32 20  ned char *, u32 
767a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
767b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61  TE int sqlite3Va
767c0 72 69 6e 74 4c 65 6e 28 75 36 34 20 76 29 3b 0a  rintLen(u64 v);.
767d0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61 64 65  ./*.** The heade
767e0 72 20 6f 66 20 61 20 72 65 63 6f 72 64 20 63 6f  r of a record co
767f0 6e 73 69 73 74 73 20 6f 66 20 61 20 73 65 71 75  nsists of a sequ
76800 65 6e 63 65 20 76 61 72 69 61 62 6c 65 2d 6c 65  ence variable-le
76810 6e 67 74 68 20 69 6e 74 65 67 65 72 73 2e 0a 2a  ngth integers..*
76820 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 72 73  * These integers
76830 20 61 72 65 20 61 6c 6d 6f 73 74 20 61 6c 77 61   are almost alwa
76840 79 73 20 73 6d 61 6c 6c 20 61 6e 64 20 61 72 65  ys small and are
76850 20 65 6e 63 6f 64 65 64 20 61 73 20 61 20 73 69   encoded as a si
76860 6e 67 6c 65 20 62 79 74 65 2e 0a 2a 2a 20 54 68  ngle byte..** Th
76870 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72  e following macr
76880 6f 73 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67  os take advantag
76890 65 20 74 68 69 73 20 66 61 63 74 20 74 6f 20 70  e this fact to p
768a0 72 6f 76 69 64 65 20 61 20 66 61 73 74 20 65 6e  rovide a fast en
768b0 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 64 65 63 6f  code.** and deco
768c0 64 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65  de of the intege
768d0 72 73 20 69 6e 20 61 20 72 65 63 6f 72 64 20 68  rs in a record h
768e0 65 61 64 65 72 2e 20 20 49 74 20 69 73 20 66 61  eader.  It is fa
768f0 73 74 65 72 20 66 6f 72 20 74 68 65 20 63 6f 6d  ster for the com
76900 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 65 72  mon.** case wher
76910 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 69 73  e the integer is
76920 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 20   a single byte. 
76930 20 49 74 20 69 73 20 61 20 6c 69 74 74 6c 65 20   It is a little 
76940 73 6c 6f 77 65 72 20 77 68 65 6e 20 74 68 65 0a  slower when the.
76950 2a 2a 20 69 6e 74 65 67 65 72 20 69 73 20 74 77  ** integer is tw
76960 6f 20 6f 72 20 6d 6f 72 65 20 62 79 74 65 73 2e  o or more bytes.
76970 20 20 42 75 74 20 6f 76 65 72 61 6c 6c 20 69 74    But overall it
76980 20 69 73 20 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a   is faster..**.*
76990 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
769a0 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20  expressions are 
769b0 65 71 75 69 76 61 6c 65 6e 74 3a 0a 2a 2a 0a 2a  equivalent:.**.*
769c0 2a 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74 65  *     x = sqlite
769d0 33 47 65 74 56 61 72 69 6e 74 33 32 28 20 41 2c  3GetVarint32( A,
769e0 20 26 42 20 29 3b 0a 2a 2a 20 20 20 20 20 78 20   &B );.**     x 
769f0 3d 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69  = sqlite3PutVari
76a00 6e 74 33 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a  nt32( A, B );.**
76a10 0a 2a 2a 20 20 20 20 20 78 20 3d 20 67 65 74 56  .**     x = getV
76a20 61 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29 3b  arint32( A, B );
76a30 0a 2a 2a 20 20 20 20 20 78 20 3d 20 70 75 74 56  .**     x = putV
76a40 61 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29 3b  arint32( A, B );
76a50 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67  .**.*/.#define g
76a60 65 74 56 61 72 69 6e 74 33 32 28 41 2c 42 29 20  etVarint32(A,B) 
76a70 20 28 28 2a 28 41 29 3c 28 75 6e 73 69 67 6e 65   ((*(A)<(unsigne
76a80 64 20 63 68 61 72 29 30 78 38 30 29 20 3f 20 28  d char)0x80) ? (
76a90 28 42 29 20 3d 20 28 75 33 32 29 2a 28 41 29 29  (B) = (u32)*(A))
76aa0 2c 31 20 3a 20 73 71 6c 69 74 65 33 47 65 74 56  ,1 : sqlite3GetV
76ab0 61 72 69 6e 74 33 32 28 28 41 29 2c 20 26 28 42  arint32((A), &(B
76ac0 29 29 29 0a 23 64 65 66 69 6e 65 20 70 75 74 56  ))).#define putV
76ad0 61 72 69 6e 74 33 32 28 41 2c 42 29 20 20 28 28  arint32(A,B)  ((
76ae0 28 42 29 3c 28 75 33 32 29 30 78 38 30 29 20 3f  (B)<(u32)0x80) ?
76af0 20 28 2a 28 41 29 20 3d 20 28 75 6e 73 69 67 6e   (*(A) = (unsign
76b00 65 64 20 63 68 61 72 29 28 42 29 29 2c 31 20 3a  ed char)(B)),1 :
76b10 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e   sqlite3PutVarin
76b20 74 33 32 28 28 41 29 2c 20 28 42 29 29 29 0a 23  t32((A), (B))).#
76b30 64 65 66 69 6e 65 20 67 65 74 56 61 72 69 6e 74  define getVarint
76b40 20 20 20 20 73 71 6c 69 74 65 33 47 65 74 56 61      sqlite3GetVa
76b50 72 69 6e 74 0a 23 64 65 66 69 6e 65 20 70 75 74  rint.#define put
76b60 56 61 72 69 6e 74 20 20 20 20 73 71 6c 69 74 65  Varint    sqlite
76b70 33 50 75 74 56 61 72 69 6e 74 0a 0a 0a 53 51 4c  3PutVarint...SQL
76b80 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
76b90 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66   sqlite3IndexAff
76ba0 69 6e 69 74 79 53 74 72 28 56 64 62 65 20 2a 2c  inityStr(Vdbe *,
76bb0 20 49 6e 64 65 78 20 2a 29 3b 0a 53 51 4c 49 54   Index *);.SQLIT
76bc0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
76bd0 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69 6e  qlite3TableAffin
76be0 69 74 79 53 74 72 28 56 64 62 65 20 2a 2c 20 54  ityStr(Vdbe *, T
76bf0 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  able *);.SQLITE_
76c00 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c  PRIVATE char sql
76c10 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e  ite3CompareAffin
76c20 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 2c  ity(Expr *pExpr,
76c30 20 63 68 61 72 20 61 66 66 32 29 3b 0a 53 51 4c   char aff2);.SQL
76c40 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
76c50 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69  sqlite3IndexAffi
76c60 6e 69 74 79 4f 6b 28 45 78 70 72 20 2a 70 45 78  nityOk(Expr *pEx
76c70 70 72 2c 20 63 68 61 72 20 69 64 78 5f 61 66 66  pr, char idx_aff
76c80 69 6e 69 74 79 29 3b 0a 53 51 4c 49 54 45 5f 50  inity);.SQLITE_P
76c90 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69  RIVATE char sqli
76ca0 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28  te3ExprAffinity(
76cb0 45 78 70 72 20 2a 70 45 78 70 72 29 3b 0a 53 51  Expr *pExpr);.SQ
76cc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
76cd0 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 63   sqlite3Atoi64(c
76ce0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 36 34 2a  onst char*, i64*
76cf0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
76d00 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72  E void sqlite3Er
76d10 72 6f 72 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e  ror(sqlite3*, in
76d20 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e  t, const char*,.
76d30 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..);.SQLITE_PRIV
76d40 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
76d50 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74  3HexToBlob(sqlit
76d60 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  e3*, const char 
76d70 2a 7a 2c 20 69 6e 74 20 6e 29 3b 0a 53 51 4c 49  *z, int n);.SQLI
76d80 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
76d90 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d  qlite3TwoPartNam
76da0 65 28 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65 6e  e(Parse *, Token
76db0 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2c 20 54 6f 6b   *, Token *, Tok
76dc0 65 6e 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  en **);.SQLITE_P
76dd0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61  RIVATE const cha
76de0 72 20 2a 73 71 6c 69 74 65 33 45 72 72 53 74 72  r *sqlite3ErrStr
76df0 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  (int);.SQLITE_PR
76e00 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
76e10 33 52 65 61 64 53 63 68 65 6d 61 28 50 61 72 73  3ReadSchema(Pars
76e20 65 20 2a 70 50 61 72 73 65 29 3b 0a 53 51 4c 49  e *pParse);.SQLI
76e30 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53  TE_PRIVATE CollS
76e40 65 71 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 43  eq *sqlite3FindC
76e50 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 2a 2c  ollSeq(sqlite3*,
76e60 75 38 20 65 6e 63 2c 20 63 6f 6e 73 74 20 63 68  u8 enc, const ch
76e70 61 72 20 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53  ar *,int,int);.S
76e80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f  QLITE_PRIVATE Co
76e90 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 4c 6f  llSeq *sqlite3Lo
76ea0 63 61 74 65 43 6f 6c 6c 53 65 71 28 50 61 72 73  cateCollSeq(Pars
76eb0 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74  e *pParse, const
76ec0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e   char *zName, in
76ed0 74 20 6e 4e 61 6d 65 29 3b 0a 53 51 4c 49 54 45  t nName);.SQLITE
76ee0 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71  _PRIVATE CollSeq
76ef0 20 2a 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c   *sqlite3ExprCol
76f00 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50 61 72  lSeq(Parse *pPar
76f10 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29  se, Expr *pExpr)
76f20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
76f30 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78   Expr *sqlite3Ex
76f40 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 73 65 20  prSetColl(Parse 
76f50 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 2c  *pParse, Expr *,
76f60 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54   Token *);.SQLIT
76f70 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
76f80 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c 6c 53 65  lite3CheckCollSe
76f90 71 28 50 61 72 73 65 20 2a 2c 20 43 6f 6c 6c 53  q(Parse *, CollS
76fa0 65 71 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  eq *);.SQLITE_PR
76fb0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
76fc0 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65  3CheckObjectName
76fd0 28 50 61 72 73 65 20 2a 2c 20 63 6f 6e 73 74 20  (Parse *, const 
76fe0 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  char *);.SQLITE_
76ff0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
77000 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e 67  ite3VdbeSetChang
77010 65 73 28 73 71 6c 69 74 65 33 20 2a 2c 20 69 6e  es(sqlite3 *, in
77020 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  t);..SQLITE_PRIV
77030 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  ATE const void *
77040 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74  sqlite3ValueText
77050 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c  (sqlite3_value*,
77060 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49   u8);.SQLITE_PRI
77070 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
77080 56 61 6c 75 65 42 79 74 65 73 28 73 71 6c 69 74  ValueBytes(sqlit
77090 65 33 5f 76 61 6c 75 65 2a 2c 20 75 38 29 3b 0a  e3_value*, u8);.
770a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
770b0 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65  oid sqlite3Value
770c0 53 65 74 53 74 72 28 73 71 6c 69 74 65 33 5f 76  SetStr(sqlite3_v
770d0 61 6c 75 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73  alue*, int, cons
770e0 74 20 76 6f 69 64 20 2a 2c 75 38 2c 20 0a 20 20  t void *,u8, .  
770f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77100 20 20 20 20 20 20 76 6f 69 64 28 2a 29 28 76 6f        void(*)(vo
77110 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52  id*));.SQLITE_PR
77120 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
77130 65 33 56 61 6c 75 65 46 72 65 65 28 73 71 6c 69  e3ValueFree(sqli
77140 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c  te3_value*);.SQL
77150 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69  ITE_PRIVATE sqli
77160 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74  te3_value *sqlit
77170 65 33 56 61 6c 75 65 4e 65 77 28 73 71 6c 69 74  e3ValueNew(sqlit
77180 65 33 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  e3 *);.SQLITE_PR
77190 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69  IVATE char *sqli
771a0 74 65 33 55 74 66 31 36 74 6f 38 28 73 71 6c 69  te3Utf16to8(sqli
771b0 74 65 33 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69  te3 *, const voi
771c0 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  d*, int);.SQLITE
771d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
771e0 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70  ite3ValueFromExp
771f0 72 28 73 71 6c 69 74 65 33 20 2a 2c 20 45 78 70  r(sqlite3 *, Exp
77200 72 20 2a 2c 20 75 38 2c 20 75 38 2c 20 73 71 6c  r *, u8, u8, sql
77210 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 3b 0a  ite3_value **);.
77220 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
77230 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65  oid sqlite3Value
77240 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 73 71  ApplyAffinity(sq
77250 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2c 20 75  lite3_value *, u
77260 38 2c 20 75 38 29 3b 0a 23 69 66 6e 64 65 66 20  8, u8);.#ifndef 
77270 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54  SQLITE_AMALGAMAT
77280 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ION.SQLITE_PRIVA
77290 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  TE const unsigne
772a0 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 55 70  d char sqlite3Up
772b0 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 3b 0a 53 51  perToLower[];.SQ
772c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c  LITE_PRIVATE SQL
772d0 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 53  ITE_WSD struct S
772e0 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 73 71 6c  qlite3Config sql
772f0 69 74 65 33 43 6f 6e 66 69 67 3b 0a 53 51 4c 49  ite3Config;.SQLI
77300 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49 54  TE_PRIVATE SQLIT
77310 45 5f 57 53 44 20 46 75 6e 63 44 65 66 48 61 73  E_WSD FuncDefHas
77320 68 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46  h sqlite3GlobalF
77330 75 6e 63 74 69 6f 6e 73 3b 0a 23 65 6e 64 69 66  unctions;.#endif
77340 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
77350 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6f 74  void sqlite3Root
77360 50 61 67 65 4d 6f 76 65 64 28 44 62 2a 2c 20 69  PageMoved(Db*, i
77370 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
77380 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
77390 6c 69 74 65 33 52 65 69 6e 64 65 78 28 50 61 72  lite3Reindex(Par
773a0 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b  se*, Token*, Tok
773b0 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  en*);.SQLITE_PRI
773c0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
773d0 33 41 6c 74 65 72 46 75 6e 63 74 69 6f 6e 73 28  3AlterFunctions(
773e0 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54  sqlite3*);.SQLIT
773f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
77400 71 6c 69 74 65 33 41 6c 74 65 72 52 65 6e 61 6d  qlite3AlterRenam
77410 65 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 53  eTable(Parse*, S
77420 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29  rcList*, Token*)
77430 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
77440 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54   int sqlite3GetT
77450 6f 6b 65 6e 28 63 6f 6e 73 74 20 75 6e 73 69 67  oken(const unsig
77460 6e 65 64 20 63 68 61 72 20 2a 2c 20 69 6e 74 20  ned char *, int 
77470 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
77480 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4e  TE void sqlite3N
77490 65 73 74 65 64 50 61 72 73 65 28 50 61 72 73 65  estedParse(Parse
774a0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  *, const char*, 
774b0 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ...);.SQLITE_PRI
774c0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
774d0 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53  3ExpirePreparedS
774e0 74 61 74 65 6d 65 6e 74 73 28 73 71 6c 69 74 65  tatements(sqlite
774f0 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  3*);.SQLITE_PRIV
77500 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
77510 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28 50 61  CodeSubselect(Pa
77520 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c 20 69  rse *, Expr *, i
77530 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
77540 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
77550 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28  lite3SelectPrep(
77560 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c  Parse*, Select*,
77570 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 29 3b 0a   NameContext*);.
77580 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
77590 6e 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  nt sqlite3Resolv
775a0 65 45 78 70 72 4e 61 6d 65 73 28 4e 61 6d 65 43  eExprNames(NameC
775b0 6f 6e 74 65 78 74 2a 2c 20 45 78 70 72 2a 29 3b  ontext*, Expr*);
775c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
775d0 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 73 6f  void sqlite3Reso
775e0 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28 50  lveSelectNames(P
775f0 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20  arse*, Select*, 
77600 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 29 3b 0a 53  NameContext*);.S
77610 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
77620 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  t sqlite3Resolve
77630 4f 72 64 65 72 47 72 6f 75 70 42 79 28 50 61 72  OrderGroupBy(Par
77640 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 45 78  se*, Select*, Ex
77650 70 72 4c 69 73 74 2a 2c 20 63 6f 6e 73 74 20 63  prList*, const c
77660 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
77670 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
77680 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28  e3ColumnDefault(
77690 56 64 62 65 20 2a 2c 20 54 61 62 6c 65 20 2a 2c  Vdbe *, Table *,
776a0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
776b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
776c0 65 33 41 6c 74 65 72 46 69 6e 69 73 68 41 64 64  e3AlterFinishAdd
776d0 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 2c 20  Column(Parse *, 
776e0 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45  Token *);.SQLITE
776f0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
77700 6c 69 74 65 33 41 6c 74 65 72 42 65 67 69 6e 41  lite3AlterBeginA
77710 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a  ddColumn(Parse *
77720 2c 20 53 72 63 4c 69 73 74 20 2a 29 3b 0a 53 51  , SrcList *);.SQ
77730 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c  LITE_PRIVATE Col
77740 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 47 65 74  lSeq *sqlite3Get
77750 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 2a  CollSeq(sqlite3*
77760 2c 20 43 6f 6c 6c 53 65 71 20 2a 2c 20 63 6f 6e  , CollSeq *, con
77770 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b  st char *, int);
77780 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
77790 63 68 61 72 20 73 71 6c 69 74 65 33 41 66 66 69  char sqlite3Affi
777a0 6e 69 74 79 54 79 70 65 28 63 6f 6e 73 74 20 54  nityType(const T
777b0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
777c0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
777d0 74 65 33 41 6e 61 6c 79 7a 65 28 50 61 72 73 65  te3Analyze(Parse
777e0 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e  *, Token*, Token
777f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
77800 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e  TE int sqlite3In
77810 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28  vokeBusyHandler(
77820 42 75 73 79 48 61 6e 64 6c 65 72 2a 29 3b 0a 53  BusyHandler*);.S
77830 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
77840 74 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28  t sqlite3FindDb(
77850 73 71 6c 69 74 65 33 2a 2c 20 54 6f 6b 65 6e 2a  sqlite3*, Token*
77860 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
77870 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 6e 61  E int sqlite3Ana
77880 6c 79 73 69 73 4c 6f 61 64 28 73 71 6c 69 74 65  lysisLoad(sqlite
77890 33 2a 2c 69 6e 74 20 69 44 42 29 3b 0a 53 51 4c  3*,int iDB);.SQL
778a0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
778b0 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 52   sqlite3DefaultR
778c0 6f 77 45 73 74 28 49 6e 64 65 78 2a 29 3b 0a 53  owEst(Index*);.S
778d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
778e0 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74  id sqlite3Regist
778f0 65 72 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 73 28  erLikeFunctions(
77900 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a  sqlite3*, int);.
77910 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
77920 6e 74 20 73 71 6c 69 74 65 33 49 73 4c 69 6b 65  nt sqlite3IsLike
77930 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33  Function(sqlite3
77940 2a 2c 45 78 70 72 2a 2c 69 6e 74 2a 2c 63 68 61  *,Expr*,int*,cha
77950 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
77960 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
77970 4d 69 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61  MinimumFileForma
77980 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69  t(Parse*, int, i
77990 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
779a0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
779b0 53 63 68 65 6d 61 46 72 65 65 28 76 6f 69 64 20  SchemaFree(void 
779c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
779d0 54 45 20 53 63 68 65 6d 61 20 2a 73 71 6c 69 74  TE Schema *sqlit
779e0 65 33 53 63 68 65 6d 61 47 65 74 28 73 71 6c 69  e3SchemaGet(sqli
779f0 74 65 33 20 2a 2c 20 42 74 72 65 65 20 2a 29 3b  te3 *, Btree *);
77a00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
77a10 69 6e 74 20 73 71 6c 69 74 65 33 53 63 68 65 6d  int sqlite3Schem
77a20 61 54 6f 49 6e 64 65 78 28 73 71 6c 69 74 65 33  aToIndex(sqlite3
77a30 20 2a 64 62 2c 20 53 63 68 65 6d 61 20 2a 29 3b   *db, Schema *);
77a40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
77a50 4b 65 79 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33  KeyInfo *sqlite3
77a60 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 50 61 72  IndexKeyinfo(Par
77a70 73 65 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b 0a  se *, Index *);.
77a80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
77a90 6e 74 20 73 71 6c 69 74 65 33 43 72 65 61 74 65  nt sqlite3Create
77aa0 46 75 6e 63 28 73 71 6c 69 74 65 33 20 2a 2c 20  Func(sqlite3 *, 
77ab0 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e  const char *, in
77ac0 74 2c 20 69 6e 74 2c 20 76 6f 69 64 20 2a 2c 20  t, int, void *, 
77ad0 0a 20 20 76 6f 69 64 20 28 2a 29 28 73 71 6c 69  .  void (*)(sqli
77ae0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
77af0 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  ,sqlite3_value *
77b00 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 29 28 73  *),.  void (*)(s
77b10 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
77b20 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
77b30 65 20 2a 2a 29 2c 20 76 6f 69 64 20 28 2a 29 28  e **), void (*)(
77b40 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
77b50 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ));.SQLITE_PRIVA
77b60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 70  TE int sqlite3Ap
77b70 69 45 78 69 74 28 73 71 6c 69 74 65 33 20 2a 64  iExit(sqlite3 *d
77b80 62 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  b, int);.SQLITE_
77b90 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
77ba0 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62  te3OpenTempDatab
77bb0 61 73 65 28 50 61 72 73 65 20 2a 29 3b 0a 0a 53  ase(Parse *);..S
77bc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
77bd0 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 63  id sqlite3StrAcc
77be0 75 6d 49 6e 69 74 28 53 74 72 41 63 63 75 6d 2a  umInit(StrAccum*
77bf0 2c 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 69 6e  , char*, int, in
77c00 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
77c10 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
77c20 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 53 74  trAccumAppend(St
77c30 72 41 63 63 75 6d 2a 2c 63 6f 6e 73 74 20 63 68  rAccum*,const ch
77c40 61 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  ar*,int);.SQLITE
77c50 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
77c60 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69  qlite3StrAccumFi
77c70 6e 69 73 68 28 53 74 72 41 63 63 75 6d 2a 29 3b  nish(StrAccum*);
77c80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
77c90 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 41  void sqlite3StrA
77ca0 63 63 75 6d 52 65 73 65 74 28 53 74 72 41 63 63  ccumReset(StrAcc
77cb0 75 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  um*);.SQLITE_PRI
77cc0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
77cd0 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28  3SelectDestInit(
77ce0 53 65 6c 65 63 74 44 65 73 74 2a 2c 69 6e 74 2c  SelectDest*,int,
77cf0 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  int);../*.** The
77d00 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68   interface to th
77d10 65 20 4c 45 4d 4f 4e 2d 67 65 6e 65 72 61 74 65  e LEMON-generate
77d20 64 20 70 61 72 73 65 72 0a 2a 2f 0a 53 51 4c 49  d parser.*/.SQLI
77d30 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
77d40 2a 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 6c  *sqlite3ParserAl
77d50 6c 6f 63 28 76 6f 69 64 2a 28 2a 29 28 73 69 7a  loc(void*(*)(siz
77d60 65 5f 74 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52  e_t));.SQLITE_PR
77d70 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
77d80 65 33 50 61 72 73 65 72 46 72 65 65 28 76 6f 69  e3ParserFree(voi
77d90 64 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64  d*, void(*)(void
77da0 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  *));.SQLITE_PRIV
77db0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
77dc0 50 61 72 73 65 72 28 76 6f 69 64 2a 2c 20 69 6e  Parser(void*, in
77dd0 74 2c 20 54 6f 6b 65 6e 2c 20 50 61 72 73 65 2a  t, Token, Parse*
77de0 29 3b 0a 23 69 66 64 65 66 20 59 59 54 52 41 43  );.#ifdef YYTRAC
77df0 4b 4d 41 58 53 54 41 43 4b 44 45 50 54 48 0a 53  KMAXSTACKDEPTH.S
77e00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
77e10 69 6e 74 20 73 71 6c 69 74 65 33 50 61 72 73 65  int sqlite3Parse
77e20 72 53 74 61 63 6b 50 65 61 6b 28 76 6f 69 64 2a  rStackPeak(void*
77e30 29 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54  );.#endif..SQLIT
77e40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
77e50 6c 69 74 65 33 41 75 74 6f 4c 6f 61 64 45 78 74  lite3AutoLoadExt
77e60 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a  ensions(sqlite3*
77e70 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
77e80 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45  E_OMIT_LOAD_EXTE
77e90 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49  NSION.SQLITE_PRI
77ea0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
77eb0 74 65 33 43 6c 6f 73 65 45 78 74 65 6e 73 69 6f  te3CloseExtensio
77ec0 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 65  ns(sqlite3*);.#e
77ed0 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  lse.# define sql
77ee0 69 74 65 33 43 6c 6f 73 65 45 78 74 65 6e 73 69  ite3CloseExtensi
77ef0 6f 6e 73 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23  ons(X).#endif..#
77f00 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
77f10 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
77f20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
77f30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54 61 62   void sqlite3Tab
77f40 6c 65 4c 6f 63 6b 28 50 61 72 73 65 20 2a 2c 20  leLock(Parse *, 
77f50 69 6e 74 2c 20 69 6e 74 2c 20 75 38 2c 20 63 6f  int, int, u8, co
77f60 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 23 65 6c  nst char *);.#el
77f70 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c  se.  #define sql
77f80 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 76 2c  ite3TableLock(v,
77f90 77 2c 78 2c 79 2c 7a 29 0a 23 65 6e 64 69 66 0a  w,x,y,z).#endif.
77fa0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
77fb0 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  EST.SQLITE_PRIVA
77fc0 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33  TE   int sqlite3
77fd0 55 74 66 38 54 6f 38 28 75 6e 73 69 67 6e 65 64  Utf8To8(unsigned
77fe0 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 0a   char*);.#endif.
77ff0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
78000 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
78010 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74  .#  define sqlit
78020 65 33 56 74 61 62 43 6c 65 61 72 28 58 29 0a 23  e3VtabClear(X).#
78030 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33    define sqlite3
78040 56 74 61 62 53 79 6e 63 28 58 2c 59 29 20 53 51  VtabSync(X,Y) SQ
78050 4c 49 54 45 5f 4f 4b 0a 23 20 20 64 65 66 69 6e  LITE_OK.#  defin
78060 65 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c  e sqlite3VtabRol
78070 6c 62 61 63 6b 28 58 29 0a 23 20 20 64 65 66 69  lback(X).#  defi
78080 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f  ne sqlite3VtabCo
78090 6d 6d 69 74 28 58 29 0a 23 65 6c 73 65 0a 53 51  mmit(X).#else.SQ
780a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20  LITE_PRIVATE    
780b0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
780c0 43 6c 65 61 72 28 54 61 62 6c 65 2a 29 3b 0a 53  Clear(Table*);.S
780d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
780e0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62   int sqlite3Vtab
780f0 53 79 6e 63 28 73 71 6c 69 74 65 33 20 2a 64 62  Sync(sqlite3 *db
78100 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49  , char **);.SQLI
78110 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69 6e  TE_PRIVATE    in
78120 74 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c  t sqlite3VtabRol
78130 6c 62 61 63 6b 28 73 71 6c 69 74 65 33 20 2a 64  lback(sqlite3 *d
78140 62 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  b);.SQLITE_PRIVA
78150 54 45 20 20 20 20 69 6e 74 20 73 71 6c 69 74 65  TE    int sqlite
78160 33 56 74 61 62 43 6f 6d 6d 69 74 28 73 71 6c 69  3VtabCommit(sqli
78170 74 65 33 20 2a 64 62 29 3b 0a 23 65 6e 64 69 66  te3 *db);.#endif
78180 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
78190 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
781a0 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 50 61 72  MakeWritable(Par
781b0 73 65 2a 2c 54 61 62 6c 65 2a 29 3b 0a 53 51 4c  se*,Table*);.SQL
781c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
781d0 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b   sqlite3VtabLock
781e0 28 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 29 3b  (sqlite3_vtab*);
781f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
78200 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
78210 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 2a 2c  Unlock(sqlite3*,
78220 20 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 29 3b   sqlite3_vtab*);
78230 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
78240 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
78250 42 65 67 69 6e 50 61 72 73 65 28 50 61 72 73 65  BeginParse(Parse
78260 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e  *, Token*, Token
78270 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  *, Token*);.SQLI
78280 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
78290 73 71 6c 69 74 65 33 56 74 61 62 46 69 6e 69 73  sqlite3VtabFinis
782a0 68 50 61 72 73 65 28 50 61 72 73 65 2a 2c 20 54  hParse(Parse*, T
782b0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
782c0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
782d0 74 65 33 56 74 61 62 41 72 67 49 6e 69 74 28 50  te3VtabArgInit(P
782e0 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  arse*);.SQLITE_P
782f0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
78300 74 65 33 56 74 61 62 41 72 67 45 78 74 65 6e 64  te3VtabArgExtend
78310 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29  (Parse*, Token*)
78320 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
78330 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62   int sqlite3Vtab
78340 43 61 6c 6c 43 72 65 61 74 65 28 73 71 6c 69 74  CallCreate(sqlit
78350 65 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20  e3*, int, const 
78360 63 68 61 72 20 2a 2c 20 63 68 61 72 20 2a 2a 29  char *, char **)
78370 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
78380 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62   int sqlite3Vtab
78390 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 50 61 72 73  CallConnect(Pars
783a0 65 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51 4c  e*, Table*);.SQL
783b0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
783c0 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 44  sqlite3VtabCallD
783d0 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33 2a 2c  estroy(sqlite3*,
783e0 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72   int, const char
783f0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
78400 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
78410 74 61 62 42 65 67 69 6e 28 73 71 6c 69 74 65 33  tabBegin(sqlite3
78420 20 2a 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62   *, sqlite3_vtab
78430 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
78440 41 54 45 20 46 75 6e 63 44 65 66 20 2a 73 71 6c  ATE FuncDef *sql
78450 69 74 65 33 56 74 61 62 4f 76 65 72 6c 6f 61 64  ite3VtabOverload
78460 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33  Function(sqlite3
78470 20 2a 2c 46 75 6e 63 44 65 66 2a 2c 20 69 6e 74   *,FuncDef*, int
78480 20 6e 41 72 67 2c 20 45 78 70 72 2a 29 3b 0a 53   nArg, Expr*);.S
78490 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
784a0 69 64 20 73 71 6c 69 74 65 33 49 6e 76 61 6c 69  id sqlite3Invali
784b0 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65  dFunction(sqlite
784c0 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
784d0 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b  qlite3_value**);
784e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
784f0 69 6e 74 20 73 71 6c 69 74 65 33 54 72 61 6e 73  int sqlite3Trans
78500 66 65 72 42 69 6e 64 69 6e 67 73 28 73 71 6c 69  ferBindings(sqli
78510 74 65 33 5f 73 74 6d 74 20 2a 2c 20 73 71 6c 69  te3_stmt *, sqli
78520 74 65 33 5f 73 74 6d 74 20 2a 29 3b 0a 53 51 4c  te3_stmt *);.SQL
78530 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
78540 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65  sqlite3Reprepare
78550 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  (Vdbe*);.SQLITE_
78560 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
78570 69 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 63  ite3ExprListChec
78580 6b 4c 65 6e 67 74 68 28 50 61 72 73 65 2a 2c 20  kLength(Parse*, 
78590 45 78 70 72 4c 69 73 74 2a 2c 20 63 6f 6e 73 74  ExprList*, const
785a0 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   char*);.SQLITE_
785b0 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20  PRIVATE CollSeq 
785c0 2a 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f  *sqlite3BinaryCo
785d0 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 50 61 72  mpareCollSeq(Par
785e0 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78  se *, Expr *, Ex
785f0 70 72 20 2a 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41  pr *);.../*.** A
78600 76 61 69 6c 61 62 6c 65 20 66 61 75 6c 74 20 69  vailable fault i
78610 6e 6a 65 63 74 6f 72 73 2e 20 20 53 68 6f 75 6c  njectors.  Shoul
78620 64 20 62 65 20 6e 75 6d 62 65 72 65 64 20 62 65  d be numbered be
78630 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 30 2e 0a  ginning with 0..
78640 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
78650 45 5f 46 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f  E_FAULTINJECTOR_
78660 4d 41 4c 4c 4f 43 20 20 20 20 20 30 0a 23 64 65  MALLOC     0.#de
78670 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 41 55 4c  fine SQLITE_FAUL
78680 54 49 4e 4a 45 43 54 4f 52 5f 43 4f 55 4e 54 20  TINJECTOR_COUNT 
78690 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68       1../*.** Th
786a0 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74  e interface to t
786b0 68 65 20 63 6f 64 65 20 69 6e 20 66 61 75 6c 74  he code in fault
786c0 2e 63 20 75 73 65 64 20 66 6f 72 20 69 64 65 6e  .c used for iden
786d0 74 69 66 79 69 6e 67 20 22 62 65 6e 69 67 6e 22  tifying "benign"
786e0 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75  .** malloc failu
786f0 72 65 73 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c  res. This is onl
78700 79 20 70 72 65 73 65 6e 74 20 69 66 20 53 51 4c  y present if SQL
78710 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e  ITE_OMIT_BUILTIN
78720 5f 54 45 53 54 0a 2a 2a 20 69 73 20 6e 6f 74 20  _TEST.** is not 
78730 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e  defined..*/.#ifn
78740 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
78750 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 53 51 4c  BUILTIN_TEST.SQL
78760 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
78770 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42  id sqlite3BeginB
78780 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64  enignMalloc(void
78790 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
787a0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
787b0 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  EndBenignMalloc(
787c0 76 6f 69 64 29 3b 0a 23 65 6c 73 65 0a 20 20 23  void);.#else.  #
787d0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 65  define sqlite3Be
787e0 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
787f0 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69  ).  #define sqli
78800 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
78810 6f 63 28 29 0a 23 65 6e 64 69 66 0a 0a 23 64 65  oc().#endif..#de
78820 66 69 6e 65 20 49 4e 5f 49 4e 44 45 58 5f 52 4f  fine IN_INDEX_RO
78830 57 49 44 20 20 20 20 20 20 20 20 20 20 20 31 0a  WID           1.
78840 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e 44 45 58  #define IN_INDEX
78850 5f 45 50 48 20 20 20 20 20 20 20 20 20 20 20 20  _EPH            
78860 20 32 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e   2.#define IN_IN
78870 44 45 58 5f 49 4e 44 45 58 20 20 20 20 20 20 20  DEX_INDEX       
78880 20 20 20 20 33 0a 53 51 4c 49 54 45 5f 50 52 49      3.SQLITE_PRI
78890 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
788a0 46 69 6e 64 49 6e 49 6e 64 65 78 28 50 61 72 73  FindInIndex(Pars
788b0 65 20 2a 2c 20 45 78 70 72 20 2a 2c 20 69 6e 74  e *, Expr *, int
788c0 2a 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  *);..#ifdef SQLI
788d0 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43  TE_ENABLE_ATOMIC
788e0 5f 57 52 49 54 45 0a 53 51 4c 49 54 45 5f 50 52  _WRITE.SQLITE_PR
788f0 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
78900 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 73  te3JournalOpen(s
78910 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f  qlite3_vfs *, co
78920 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 71 6c 69  nst char *, sqli
78930 74 65 33 5f 66 69 6c 65 20 2a 2c 20 69 6e 74 2c  te3_file *, int,
78940 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
78950 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
78960 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 73  te3JournalSize(s
78970 71 6c 69 74 65 33 5f 76 66 73 20 2a 29 3b 0a 53  qlite3_vfs *);.S
78980 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
78990 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e  int sqlite3Journ
789a0 61 6c 43 72 65 61 74 65 28 73 71 6c 69 74 65 33  alCreate(sqlite3
789b0 5f 66 69 6c 65 20 2a 29 3b 0a 23 65 6c 73 65 0a  _file *);.#else.
789c0 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65    #define sqlite
789d0 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 70 56 66  3JournalSize(pVf
789e0 73 29 20 28 28 70 56 66 73 29 2d 3e 73 7a 4f 73  s) ((pVfs)->szOs
789f0 46 69 6c 65 29 0a 23 65 6e 64 69 66 0a 0a 53 51  File).#endif..SQ
78a00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
78a10 64 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72  d sqlite3MemJour
78a20 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f  nalOpen(sqlite3_
78a30 66 69 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  file *);.SQLITE_
78a40 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
78a50 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a  te3MemJournalSiz
78a60 65 28 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e();.SQLITE_PRIV
78a70 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
78a80 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69  sMemJournal(sqli
78a90 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a 23 69  te3_file *);..#i
78aa0 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50  f SQLITE_MAX_EXP
78ab0 52 5f 44 45 50 54 48 3e 30 0a 53 51 4c 49 54 45  R_DEPTH>0.SQLITE
78ac0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
78ad0 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65  sqlite3ExprSetHe
78ae0 69 67 68 74 28 50 61 72 73 65 20 2a 70 50 61 72  ight(Parse *pPar
78af0 73 65 2c 20 45 78 70 72 20 2a 70 29 3b 0a 53 51  se, Expr *p);.SQ
78b00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
78b10 6e 74 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  nt sqlite3Select
78b20 45 78 70 72 48 65 69 67 68 74 28 53 65 6c 65 63  ExprHeight(Selec
78b30 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  t *);.SQLITE_PRI
78b40 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
78b50 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68  e3ExprCheckHeigh
78b60 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a  t(Parse*, int);.
78b70 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20  #else.  #define 
78b80 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65  sqlite3ExprSetHe
78b90 69 67 68 74 28 78 2c 79 29 0a 20 20 23 64 65 66  ight(x,y).  #def
78ba0 69 6e 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ine sqlite3Selec
78bb0 74 45 78 70 72 48 65 69 67 68 74 28 78 29 20 30  tExprHeight(x) 0
78bc0 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74  .  #define sqlit
78bd0 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68  e3ExprCheckHeigh
78be0 74 28 78 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 53  t(x,y).#endif..S
78bf0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33  QLITE_PRIVATE u3
78c00 32 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74  2 sqlite3Get4byt
78c10 65 28 63 6f 6e 73 74 20 75 38 2a 29 3b 0a 53 51  e(const u8*);.SQ
78c20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
78c30 64 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74  d sqlite3Put4byt
78c40 65 28 75 38 2a 2c 20 75 33 32 29 3b 0a 0a 23 69  e(u8*, u32);..#i
78c50 66 64 65 66 20 53 51 4c 49 54 45 5f 53 53 45 0a  fdef SQLITE_SSE.
78c60 23 69 6e 63 6c 75 64 65 20 22 73 73 65 49 6e 74  #include "sseInt
78c70 2e 68 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  .h".#endif..#ifd
78c80 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
78c90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
78ca0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72   void sqlite3Par
78cb0 73 65 72 54 72 61 63 65 28 46 49 4c 45 2a 2c 20  serTrace(FILE*, 
78cc0 63 68 61 72 20 2a 29 3b 0a 23 65 6e 64 69 66 0a  char *);.#endif.
78cd0 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51  ./*.** If the SQ
78ce0 4c 49 54 45 5f 45 4e 41 42 4c 45 20 49 4f 54 52  LITE_ENABLE IOTR
78cf0 41 43 45 20 65 78 69 73 74 73 20 74 68 65 6e 20  ACE exists then 
78d00 74 68 65 20 67 6c 6f 62 61 6c 20 76 61 72 69 61  the global varia
78d10 62 6c 65 0a 2a 2a 20 73 71 6c 69 74 65 33 49 6f  ble.** sqlite3Io
78d20 54 72 61 63 65 20 69 73 20 61 20 70 6f 69 6e 74  Trace is a point
78d30 65 72 20 74 6f 20 61 20 70 72 69 6e 74 66 2d 6c  er to a printf-l
78d40 69 6b 65 20 72 6f 75 74 69 6e 65 20 75 73 65 64  ike routine used
78d50 20 74 6f 0a 2a 2a 20 70 72 69 6e 74 20 49 2f 4f   to.** print I/O
78d60 20 74 72 61 63 69 6e 67 20 6d 65 73 73 61 67 65   tracing message
78d70 73 2e 20 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  s. .*/.#ifdef SQ
78d80 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52  LITE_ENABLE_IOTR
78d90 41 43 45 0a 23 20 64 65 66 69 6e 65 20 49 4f 54  ACE.# define IOT
78da0 52 41 43 45 28 41 29 20 20 69 66 28 20 73 71 6c  RACE(A)  if( sql
78db0 69 74 65 33 49 6f 54 72 61 63 65 20 29 7b 20 73  ite3IoTrace ){ s
78dc0 71 6c 69 74 65 33 49 6f 54 72 61 63 65 20 41 3b  qlite3IoTrace A;
78dd0 20 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54   }.SQLITE_PRIVAT
78de0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
78df0 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c 28 56  VdbeIOTraceSql(V
78e00 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  dbe*);.SQLITE_PR
78e10 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73 71 6c  IVATE void (*sql
78e20 69 74 65 33 49 6f 54 72 61 63 65 29 28 63 6f 6e  ite3IoTrace)(con
78e30 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 23  st char*,...);.#
78e40 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 49 4f  else.# define IO
78e50 54 52 41 43 45 28 41 29 0a 23 20 64 65 66 69 6e  TRACE(A).# defin
78e60 65 20 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54  e sqlite3VdbeIOT
78e70 72 61 63 65 53 71 6c 28 58 29 0a 23 65 6e 64 69  raceSql(X).#endi
78e80 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a  f..#endif../****
78e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
78ea0 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  f sqliteInt.h **
78eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
78ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
78ef0 20 66 69 6c 65 20 67 6c 6f 62 61 6c 2e 63 20 2a   file global.c *
78f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
78f30 20 32 30 30 38 20 4a 75 6e 65 20 31 33 0a 2a 2a   2008 June 13.**
78f40 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
78f50 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
78f60 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
78f70 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
78f80 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
78f90 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
78fa0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
78fb0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
78fc0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
78fd0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
78fe0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
78ff0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
79000 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
79010 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
79020 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
79030 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
79040 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
79050 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
79060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
790a0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
790b0 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e  tains definition
790c0 73 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69  s of global vari
790d0 61 62 6c 65 73 20 61 6e 64 20 63 6f 6e 74 61 6e  ables and contan
790e0 74 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 67  ts..**.** $Id: g
790f0 6c 6f 62 61 6c 2e 63 2c 76 20 31 2e 38 20 32 30  lobal.c,v 1.8 20
79100 30 38 2f 30 39 2f 30 34 20 31 37 3a 31 37 3a 33  08/09/04 17:17:3
79110 39 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78  9 danielk1977 Ex
79120 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 20 41 6e 20 61  p $.*/.../* An a
79130 72 72 61 79 20 74 6f 20 6d 61 70 20 61 6c 6c 20  rray to map all 
79140 75 70 70 65 72 2d 63 61 73 65 20 63 68 61 72 61  upper-case chara
79150 63 74 65 72 73 20 69 6e 74 6f 20 74 68 65 69 72  cters into their
79160 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a   corresponding.*
79170 2a 20 6c 6f 77 65 72 2d 63 61 73 65 20 63 68 61  * lower-case cha
79180 72 61 63 74 65 72 2e 20 0a 2a 2a 0a 2a 2a 20 53  racter. .**.** S
79190 51 4c 69 74 65 20 6f 6e 6c 79 20 63 6f 6e 73 69  QLite only consi
791a0 64 65 72 73 20 55 53 2d 41 53 43 49 49 20 28 6f  ders US-ASCII (o
791b0 72 20 45 42 43 44 49 43 29 20 63 68 61 72 61 63  r EBCDIC) charac
791c0 74 65 72 73 2e 20 20 57 65 20 64 6f 20 6e 6f 74  ters.  We do not
791d0 0a 2a 2a 20 68 61 6e 64 6c 65 20 63 61 73 65 20  .** handle case 
791e0 63 6f 6e 76 65 72 73 69 6f 6e 73 20 66 6f 72 20  conversions for 
791f0 74 68 65 20 55 54 46 20 63 68 61 72 61 63 74 65  the UTF characte
79200 72 20 73 65 74 20 73 69 6e 63 65 20 74 68 65 20  r set since the 
79210 74 61 62 6c 65 73 0a 2a 2a 20 69 6e 76 6f 6c 76  tables.** involv
79220 65 64 20 61 72 65 20 6e 65 61 72 6c 79 20 61 73  ed are nearly as
79230 20 62 69 67 20 6f 72 20 62 69 67 67 65 72 20 74   big or bigger t
79240 68 61 6e 20 53 51 4c 69 74 65 20 69 74 73 65 6c  han SQLite itsel
79250 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  f..*/.SQLITE_PRI
79260 56 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 67  VATE const unsig
79270 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 33  ned char sqlite3
79280 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 20 3d  UpperToLower[] =
79290 20 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45   {.#ifdef SQLITE
792a0 5f 41 53 43 49 49 0a 20 20 20 20 20 20 30 2c 20  _ASCII.      0, 
792b0 20 31 2c 20 20 32 2c 20 20 33 2c 20 20 34 2c 20   1,  2,  3,  4, 
792c0 20 35 2c 20 20 36 2c 20 20 37 2c 20 20 38 2c 20   5,  6,  7,  8, 
792d0 20 39 2c 20 31 30 2c 20 31 31 2c 20 31 32 2c 20   9, 10, 11, 12, 
792e0 31 33 2c 20 31 34 2c 20 31 35 2c 20 31 36 2c 20  13, 14, 15, 16, 
792f0 31 37 2c 0a 20 20 20 20 20 31 38 2c 20 31 39 2c  17,.     18, 19,
79300 20 32 30 2c 20 32 31 2c 20 32 32 2c 20 32 33 2c   20, 21, 22, 23,
79310 20 32 34 2c 20 32 35 2c 20 32 36 2c 20 32 37 2c   24, 25, 26, 27,
79320 20 32 38 2c 20 32 39 2c 20 33 30 2c 20 33 31 2c   28, 29, 30, 31,
79330 20 33 32 2c 20 33 33 2c 20 33 34 2c 20 33 35 2c   32, 33, 34, 35,
79340 0a 20 20 20 20 20 33 36 2c 20 33 37 2c 20 33 38  .     36, 37, 38
79350 2c 20 33 39 2c 20 34 30 2c 20 34 31 2c 20 34 32  , 39, 40, 41, 42
79360 2c 20 34 33 2c 20 34 34 2c 20 34 35 2c 20 34 36  , 43, 44, 45, 46
79370 2c 20 34 37 2c 20 34 38 2c 20 34 39 2c 20 35 30  , 47, 48, 49, 50
79380 2c 20 35 31 2c 20 35 32 2c 20 35 33 2c 0a 20 20  , 51, 52, 53,.  
79390 20 20 20 35 34 2c 20 35 35 2c 20 35 36 2c 20 35     54, 55, 56, 5
793a0 37 2c 20 35 38 2c 20 35 39 2c 20 36 30 2c 20 36  7, 58, 59, 60, 6
793b0 31 2c 20 36 32 2c 20 36 33 2c 20 36 34 2c 20 39  1, 62, 63, 64, 9
793c0 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30  7, 98, 99,100,10
793d0 31 2c 31 30 32 2c 31 30 33 2c 0a 20 20 20 20 31  1,102,103,.    1
793e0 30 34 2c 31 30 35 2c 31 30 36 2c 31 30 37 2c 31  04,105,106,107,1
793f0 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31  08,109,110,111,1
79400 31 32 2c 31 31 33 2c 31 31 34 2c 31 31 35 2c 31  12,113,114,115,1
79410 31 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31  16,117,118,119,1
79420 32 30 2c 31 32 31 2c 0a 20 20 20 20 31 32 32 2c  20,121,.    122,
79430 20 39 31 2c 20 39 32 2c 20 39 33 2c 20 39 34 2c   91, 92, 93, 94,
79440 20 39 35 2c 20 39 36 2c 20 39 37 2c 20 39 38 2c   95, 96, 97, 98,
79450 20 39 39 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c   99,100,101,102,
79460 31 30 33 2c 31 30 34 2c 31 30 35 2c 31 30 36 2c  103,104,105,106,
79470 31 30 37 2c 0a 20 20 20 20 31 30 38 2c 31 30 39  107,.    108,109
79480 2c 31 31 30 2c 31 31 31 2c 31 31 32 2c 31 31 33  ,110,111,112,113
79490 2c 31 31 34 2c 31 31 35 2c 31 31 36 2c 31 31 37  ,114,115,116,117
794a0 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c 31 32 31  ,118,119,120,121
794b0 2c 31 32 32 2c 31 32 33 2c 31 32 34 2c 31 32 35  ,122,123,124,125
794c0 2c 0a 20 20 20 20 31 32 36 2c 31 32 37 2c 31 32  ,.    126,127,12
794d0 38 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 33  8,129,130,131,13
794e0 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 33  2,133,134,135,13
794f0 36 2c 31 33 37 2c 31 33 38 2c 31 33 39 2c 31 34  6,137,138,139,14
79500 30 2c 31 34 31 2c 31 34 32 2c 31 34 33 2c 0a 20  0,141,142,143,. 
79510 20 20 20 31 34 34 2c 31 34 35 2c 31 34 36 2c 31     144,145,146,1
79520 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c 31  47,148,149,150,1
79530 35 31 2c 31 35 32 2c 31 35 33 2c 31 35 34 2c 31  51,152,153,154,1
79540 35 35 2c 31 35 36 2c 31 35 37 2c 31 35 38 2c 31  55,156,157,158,1
79550 35 39 2c 31 36 30 2c 31 36 31 2c 0a 20 20 20 20  59,160,161,.    
79560 31 36 32 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c  162,163,164,165,
79570 31 36 36 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c  166,167,168,169,
79580 31 37 30 2c 31 37 31 2c 31 37 32 2c 31 37 33 2c  170,171,172,173,
79590 31 37 34 2c 31 37 35 2c 31 37 36 2c 31 37 37 2c  174,175,176,177,
795a0 31 37 38 2c 31 37 39 2c 0a 20 20 20 20 31 38 30  178,179,.    180
795b0 2c 31 38 31 2c 31 38 32 2c 31 38 33 2c 31 38 34  ,181,182,183,184
795c0 2c 31 38 35 2c 31 38 36 2c 31 38 37 2c 31 38 38  ,185,186,187,188
795d0 2c 31 38 39 2c 31 39 30 2c 31 39 31 2c 31 39 32  ,189,190,191,192
795e0 2c 31 39 33 2c 31 39 34 2c 31 39 35 2c 31 39 36  ,193,194,195,196
795f0 2c 31 39 37 2c 0a 20 20 20 20 31 39 38 2c 31 39  ,197,.    198,19
79600 39 2c 32 30 30 2c 32 30 31 2c 32 30 32 2c 32 30  9,200,201,202,20
79610 33 2c 32 30 34 2c 32 30 35 2c 32 30 36 2c 32 30  3,204,205,206,20
79620 37 2c 32 30 38 2c 32 30 39 2c 32 31 30 2c 32 31  7,208,209,210,21
79630 31 2c 32 31 32 2c 32 31 33 2c 32 31 34 2c 32 31  1,212,213,214,21
79640 35 2c 0a 20 20 20 20 32 31 36 2c 32 31 37 2c 32  5,.    216,217,2
79650 31 38 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32  18,219,220,221,2
79660 32 32 2c 32 32 33 2c 32 32 34 2c 32 32 35 2c 32  22,223,224,225,2
79670 32 36 2c 32 32 37 2c 32 32 38 2c 32 32 39 2c 32  26,227,228,229,2
79680 33 30 2c 32 33 31 2c 32 33 32 2c 32 33 33 2c 0a  30,231,232,233,.
79690 20 20 20 20 32 33 34 2c 32 33 35 2c 32 33 36 2c      234,235,236,
796a0 32 33 37 2c 32 33 38 2c 32 33 39 2c 32 34 30 2c  237,238,239,240,
796b0 32 34 31 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c  241,242,243,244,
796c0 32 34 35 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c  245,246,247,248,
796d0 32 34 39 2c 32 35 30 2c 32 35 31 2c 0a 20 20 20  249,250,251,.   
796e0 20 32 35 32 2c 32 35 33 2c 32 35 34 2c 32 35 35   252,253,254,255
796f0 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
79700 51 4c 49 54 45 5f 45 42 43 44 49 43 0a 20 20 20  QLITE_EBCDIC.   
79710 20 20 20 30 2c 20 20 31 2c 20 20 32 2c 20 20 33     0,  1,  2,  3
79720 2c 20 20 34 2c 20 20 35 2c 20 20 36 2c 20 20 37  ,  4,  5,  6,  7
79730 2c 20 20 38 2c 20 20 39 2c 20 31 30 2c 20 31 31  ,  8,  9, 10, 11
79740 2c 20 31 32 2c 20 31 33 2c 20 31 34 2c 20 31 35  , 12, 13, 14, 15
79750 2c 20 2f 2a 20 30 78 20 2a 2f 0a 20 20 20 20 20  , /* 0x */.     
79760 31 36 2c 20 31 37 2c 20 31 38 2c 20 31 39 2c 20  16, 17, 18, 19, 
79770 32 30 2c 20 32 31 2c 20 32 32 2c 20 32 33 2c 20  20, 21, 22, 23, 
79780 32 34 2c 20 32 35 2c 20 32 36 2c 20 32 37 2c 20  24, 25, 26, 27, 
79790 32 38 2c 20 32 39 2c 20 33 30 2c 20 33 31 2c 20  28, 29, 30, 31, 
797a0 2f 2a 20 31 78 20 2a 2f 0a 20 20 20 20 20 33 32  /* 1x */.     32
797b0 2c 20 33 33 2c 20 33 34 2c 20 33 35 2c 20 33 36  , 33, 34, 35, 36
797c0 2c 20 33 37 2c 20 33 38 2c 20 33 39 2c 20 34 30  , 37, 38, 39, 40
797d0 2c 20 34 31 2c 20 34 32 2c 20 34 33 2c 20 34 34  , 41, 42, 43, 44
797e0 2c 20 34 35 2c 20 34 36 2c 20 34 37 2c 20 2f 2a  , 45, 46, 47, /*
797f0 20 32 78 20 2a 2f 0a 20 20 20 20 20 34 38 2c 20   2x */.     48, 
79800 34 39 2c 20 35 30 2c 20 35 31 2c 20 35 32 2c 20  49, 50, 51, 52, 
79810 35 33 2c 20 35 34 2c 20 35 35 2c 20 35 36 2c 20  53, 54, 55, 56, 
79820 35 37 2c 20 35 38 2c 20 35 39 2c 20 36 30 2c 20  57, 58, 59, 60, 
79830 36 31 2c 20 36 32 2c 20 36 33 2c 20 2f 2a 20 33  61, 62, 63, /* 3
79840 78 20 2a 2f 0a 20 20 20 20 20 36 34 2c 20 36 35  x */.     64, 65
79850 2c 20 36 36 2c 20 36 37 2c 20 36 38 2c 20 36 39  , 66, 67, 68, 69
79860 2c 20 37 30 2c 20 37 31 2c 20 37 32 2c 20 37 33  , 70, 71, 72, 73
79870 2c 20 37 34 2c 20 37 35 2c 20 37 36 2c 20 37 37  , 74, 75, 76, 77
79880 2c 20 37 38 2c 20 37 39 2c 20 2f 2a 20 34 78 20  , 78, 79, /* 4x 
79890 2a 2f 0a 20 20 20 20 20 38 30 2c 20 38 31 2c 20  */.     80, 81, 
798a0 38 32 2c 20 38 33 2c 20 38 34 2c 20 38 35 2c 20  82, 83, 84, 85, 
798b0 38 36 2c 20 38 37 2c 20 38 38 2c 20 38 39 2c 20  86, 87, 88, 89, 
798c0 39 30 2c 20 39 31 2c 20 39 32 2c 20 39 33 2c 20  90, 91, 92, 93, 
798d0 39 34 2c 20 39 35 2c 20 2f 2a 20 35 78 20 2a 2f  94, 95, /* 5x */
798e0 0a 20 20 20 20 20 39 36 2c 20 39 37 2c 20 36 36  .     96, 97, 66
798f0 2c 20 36 37 2c 20 36 38 2c 20 36 39 2c 20 37 30  , 67, 68, 69, 70
79900 2c 20 37 31 2c 20 37 32 2c 20 37 33 2c 31 30 36  , 71, 72, 73,106
79910 2c 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30  ,107,108,109,110
79920 2c 31 31 31 2c 20 2f 2a 20 36 78 20 2a 2f 0a 20  ,111, /* 6x */. 
79930 20 20 20 31 31 32 2c 20 38 31 2c 20 38 32 2c 20     112, 81, 82, 
79940 38 33 2c 20 38 34 2c 20 38 35 2c 20 38 36 2c 20  83, 84, 85, 86, 
79950 38 37 2c 20 38 38 2c 20 38 39 2c 31 32 32 2c 31  87, 88, 89,122,1
79960 32 33 2c 31 32 34 2c 31 32 35 2c 31 32 36 2c 31  23,124,125,126,1
79970 32 37 2c 20 2f 2a 20 37 78 20 2a 2f 0a 20 20 20  27, /* 7x */.   
79980 20 31 32 38 2c 31 32 39 2c 31 33 30 2c 31 33 31   128,129,130,131
79990 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c 31 33 35  ,132,133,134,135
799a0 2c 31 33 36 2c 31 33 37 2c 31 33 38 2c 31 33 39  ,136,137,138,139
799b0 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 34 33  ,140,141,142,143
799c0 2c 20 2f 2a 20 38 78 20 2a 2f 0a 20 20 20 20 31  , /* 8x */.    1
799d0 34 34 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c 31  44,145,146,147,1
799e0 34 38 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c 31  48,149,150,151,1
799f0 35 32 2c 31 35 33 2c 31 35 34 2c 31 35 35 2c 31  52,153,154,155,1
79a00 35 36 2c 31 35 37 2c 31 35 36 2c 31 35 39 2c 20  56,157,156,159, 
79a10 2f 2a 20 39 78 20 2a 2f 0a 20 20 20 20 31 36 30  /* 9x */.    160
79a20 2c 31 36 31 2c 31 36 32 2c 31 36 33 2c 31 36 34  ,161,162,163,164
79a30 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38  ,165,166,167,168
79a40 2c 31 36 39 2c 31 37 30 2c 31 37 31 2c 31 34 30  ,169,170,171,140
79a50 2c 31 34 31 2c 31 34 32 2c 31 37 35 2c 20 2f 2a  ,141,142,175, /*
79a60 20 41 78 20 2a 2f 0a 20 20 20 20 31 37 36 2c 31   Ax */.    176,1
79a70 37 37 2c 31 37 38 2c 31 37 39 2c 31 38 30 2c 31  77,178,179,180,1
79a80 38 31 2c 31 38 32 2c 31 38 33 2c 31 38 34 2c 31  81,182,183,184,1
79a90 38 35 2c 31 38 36 2c 31 38 37 2c 31 38 38 2c 31  85,186,187,188,1
79aa0 38 39 2c 31 39 30 2c 31 39 31 2c 20 2f 2a 20 42  89,190,191, /* B
79ab0 78 20 2a 2f 0a 20 20 20 20 31 39 32 2c 31 32 39  x */.    192,129
79ac0 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33  ,130,131,132,133
79ad0 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37  ,134,135,136,137
79ae0 2c 32 30 32 2c 32 30 33 2c 32 30 34 2c 32 30 35  ,202,203,204,205
79af0 2c 32 30 36 2c 32 30 37 2c 20 2f 2a 20 43 78 20  ,206,207, /* Cx 
79b00 2a 2f 0a 20 20 20 20 32 30 38 2c 31 34 35 2c 31  */.    208,145,1
79b10 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 31  46,147,148,149,1
79b20 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 32  50,151,152,153,2
79b30 31 38 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32  18,219,220,221,2
79b40 32 32 2c 32 32 33 2c 20 2f 2a 20 44 78 20 2a 2f  22,223, /* Dx */
79b50 0a 20 20 20 20 32 32 34 2c 32 32 35 2c 31 36 32  .    224,225,162
79b60 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 31 36 36  ,163,164,165,166
79b70 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 32 33 32  ,167,168,169,232
79b80 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 32 30 36  ,203,204,205,206
79b90 2c 32 30 37 2c 20 2f 2a 20 45 78 20 2a 2f 0a 20  ,207, /* Ex */. 
79ba0 20 20 20 32 33 39 2c 32 34 30 2c 32 34 31 2c 32     239,240,241,2
79bb0 34 32 2c 32 34 33 2c 32 34 34 2c 32 34 35 2c 32  42,243,244,245,2
79bc0 34 36 2c 32 34 37 2c 32 34 38 2c 32 34 39 2c 32  46,247,248,249,2
79bd0 31 39 2c 32 32 30 2c 32 32 31 2c 32 32 32 2c 32  19,220,221,222,2
79be0 35 35 2c 20 2f 2a 20 46 78 20 2a 2f 0a 23 65 6e  55, /* Fx */.#en
79bf0 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68  dif.};../*.** Th
79c00 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 69 6e 67  e following sing
79c10 6c 65 74 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74  leton contains t
79c20 68 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67  he global config
79c30 75 72 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 74  uration for.** t
79c40 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72  he SQLite librar
79c50 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
79c60 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44 20  VATE SQLITE_WSD 
79c70 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f  struct Sqlite3Co
79c80 6e 66 69 67 20 73 71 6c 69 74 65 33 43 6f 6e 66  nfig sqlite3Conf
79c90 69 67 20 3d 20 7b 0a 20 20 20 53 51 4c 49 54 45  ig = {.   SQLITE
79ca0 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41 54  _DEFAULT_MEMSTAT
79cb0 55 53 2c 20 20 2f 2a 20 62 4d 65 6d 73 74 61 74  US,  /* bMemstat
79cc0 20 2a 2f 0a 20 20 20 31 2c 20 20 20 20 20 20 20   */.   1,       
79cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
79ce0 20 20 2f 2a 20 62 43 6f 72 65 4d 75 74 65 78 20    /* bCoreMutex 
79cf0 2a 2f 0a 20 20 20 53 51 4c 49 54 45 5f 54 48 52  */.   SQLITE_THR
79d00 45 41 44 53 41 46 45 3d 3d 31 2c 20 20 20 20 20  EADSAFE==1,     
79d10 20 2f 2a 20 62 46 75 6c 6c 4d 75 74 65 78 20 2a   /* bFullMutex *
79d20 2f 0a 20 20 20 30 78 37 66 66 66 66 66 66 65 2c  /.   0x7ffffffe,
79d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
79d40 2f 2a 20 6d 78 53 74 72 6c 65 6e 20 2a 2f 0a 20  /* mxStrlen */. 
79d50 20 20 31 30 30 2c 20 20 20 20 20 20 20 20 20 20    100,          
79d60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
79d70 73 7a 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20  szLookaside */. 
79d80 20 20 35 30 30 2c 20 20 20 20 20 20 20 20 20 20    500,          
79d90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
79da0 6e 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 20  nLookaside */.  
79db0 20 2f 2a 20 4f 74 68 65 72 20 66 69 65 6c 64 73   /* Other fields
79dc0 20 61 6c 6c 20 64 65 66 61 75 6c 74 20 74 6f 20   all default to 
79dd0 7a 65 72 6f 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a  zero */.};.../*.
79de0 2a 2a 20 48 61 73 68 20 74 61 62 6c 65 20 66 6f  ** Hash table fo
79df0 72 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f  r global functio
79e00 6e 73 20 2d 20 66 75 6e 63 74 69 6f 6e 73 20 63  ns - functions c
79e10 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a 20  ommon to all.** 
79e20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
79e30 69 6f 6e 73 2e 20 20 41 66 74 65 72 20 69 6e 69  ions.  After ini
79e40 74 69 61 6c 69 7a 61 74 69 6f 6e 2c 20 74 68 69  tialization, thi
79e50 73 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 72 65  s table is.** re
79e60 61 64 2d 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49  ad-only..*/.SQLI
79e70 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49 54  TE_PRIVATE SQLIT
79e80 45 5f 57 53 44 20 46 75 6e 63 44 65 66 48 61 73  E_WSD FuncDefHas
79e90 68 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46  h sqlite3GlobalF
79ea0 75 6e 63 74 69 6f 6e 73 3b 0a 0a 2f 2a 2a 2a 2a  unctions;../****
79eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
79ec0 66 20 67 6c 6f 62 61 6c 2e 63 20 2a 2a 2a 2a 2a  f global.c *****
79ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
79f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
79f10 20 66 69 6c 65 20 73 74 61 74 75 73 2e 63 20 2a   file status.c *
79f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
79f50 20 32 30 30 38 20 4a 75 6e 65 20 31 38 0a 2a 2a   2008 June 18.**
79f60 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
79f70 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
79f80 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
79f90 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
79fa0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
79fb0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
79fc0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
79fd0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
79fe0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
79ff0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
7a000 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
7a010 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
7a020 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
7a030 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
7a040 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
7a050 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
7a060 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
7a070 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
7a080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7a090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7a0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7a0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
7a0c0 2a 2a 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 69  ** This module i
7a0d0 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 71  mplements the sq
7a0e0 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 20 69  lite3_status() i
7a0f0 6e 74 65 72 66 61 63 65 20 61 6e 64 20 72 65 6c  nterface and rel
7a100 61 74 65 64 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  ated.** function
7a110 61 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  ality..**.** $Id
7a120 3a 20 73 74 61 74 75 73 2e 63 2c 76 20 31 2e 39  : status.c,v 1.9
7a130 20 32 30 30 38 2f 30 39 2f 30 32 20 30 30 3a 35   2008/09/02 00:5
7a140 32 3a 35 32 20 64 72 68 20 45 78 70 20 24 0a 2a  2:52 drh Exp $.*
7a150 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 72 69 61 62 6c  /../*.** Variabl
7a160 65 73 20 69 6e 20 77 68 69 63 68 20 74 6f 20 72  es in which to r
7a170 65 63 6f 72 64 20 73 74 61 74 75 73 20 69 6e 66  ecord status inf
7a180 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70  ormation..*/.typ
7a190 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
7a1a0 74 65 33 53 74 61 74 54 79 70 65 20 73 71 6c 69  te3StatType sqli
7a1b0 74 65 33 53 74 61 74 54 79 70 65 3b 0a 73 74 61  te3StatType;.sta
7a1c0 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73  tic SQLITE_WSD s
7a1d0 74 72 75 63 74 20 73 71 6c 69 74 65 33 53 74 61  truct sqlite3Sta
7a1e0 74 54 79 70 65 20 7b 0a 20 20 69 6e 74 20 6e 6f  tType {.  int no
7a1f0 77 56 61 6c 75 65 5b 39 5d 3b 20 20 20 20 20 20  wValue[9];      
7a200 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 76 61     /* Current va
7a210 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 56  lue */.  int mxV
7a220 61 6c 75 65 5b 39 5d 3b 20 20 20 20 20 20 20 20  alue[9];        
7a230 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 76 61 6c    /* Maximum val
7a240 75 65 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 53  ue */.} sqlite3S
7a250 74 61 74 20 3d 20 7b 20 7b 30 2c 7d 2c 20 7b 30  tat = { {0,}, {0
7a260 2c 7d 20 7d 3b 0a 0a 0a 2f 2a 20 54 68 65 20 22  ,} };.../* The "
7a270 77 73 64 53 74 61 74 22 20 6d 61 63 72 6f 20 77  wsdStat" macro w
7a280 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74  ill resolve to t
7a290 68 65 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d  he status inform
7a2a0 61 74 69 6f 6e 0a 2a 2a 20 73 74 61 74 65 20 76  ation.** state v
7a2b0 65 63 74 6f 72 2e 20 20 49 66 20 77 72 69 74 61  ector.  If writa
7a2c0 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20  ble static data 
7a2d0 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6f  is unsupported o
7a2e0 6e 20 74 68 65 20 74 61 72 67 65 74 2c 0a 2a 2a  n the target,.**
7a2f0 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61   we have to loca
7a300 74 65 20 74 68 65 20 73 74 61 74 65 20 76 65 63  te the state vec
7a310 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e  tor at run-time.
7a320 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f    In the more co
7a330 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 65  mmon.** case whe
7a340 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 74  re writable stat
7a350 69 63 20 64 61 74 61 20 69 73 20 73 75 70 70 6f  ic data is suppo
7a360 72 74 65 64 2c 20 77 73 64 53 74 61 74 20 63 61  rted, wsdStat ca
7a370 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c 79  n refer directly
7a380 0a 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 6c 69  .** to the "sqli
7a390 74 65 33 53 74 61 74 22 20 73 74 61 74 65 20 76  te3Stat" state v
7a3a0 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64 20 61  ector declared a
7a3b0 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  bove..*/.#ifdef 
7a3c0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a  SQLITE_OMIT_WSD.
7a3d0 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74  # define wsdStat
7a3e0 49 6e 69 74 20 20 73 71 6c 69 74 65 33 53 74 61  Init  sqlite3Sta
7a3f0 74 54 79 70 65 20 2a 78 20 3d 20 26 47 4c 4f 42  tType *x = &GLOB
7a400 41 4c 28 73 71 6c 69 74 65 33 53 74 61 74 54 79  AL(sqlite3StatTy
7a410 70 65 2c 73 71 6c 69 74 65 33 53 74 61 74 29 0a  pe,sqlite3Stat).
7a420 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74  # define wsdStat
7a430 20 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65   x[0].#else.# de
7a440 66 69 6e 65 20 77 73 64 53 74 61 74 49 6e 69 74  fine wsdStatInit
7a450 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61  .# define wsdSta
7a460 74 20 73 71 6c 69 74 65 33 53 74 61 74 0a 23 65  t sqlite3Stat.#e
7a470 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ndif../*.** Retu
7a480 72 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 76  rn the current v
7a490 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 73  alue of a status
7a4a0 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 53   parameter..*/.S
7a4b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
7a4c0 74 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 56  t sqlite3StatusV
7a4d0 61 6c 75 65 28 69 6e 74 20 6f 70 29 7b 0a 20 20  alue(int op){.  
7a4e0 77 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 61  wsdStatInit;.  a
7a4f0 73 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 20  ssert( op>=0 && 
7a500 6f 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73 64  op<ArraySize(wsd
7a510 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29  Stat.nowValue) )
7a520 3b 0a 20 20 72 65 74 75 72 6e 20 77 73 64 53 74  ;.  return wsdSt
7a530 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b  at.nowValue[op];
7a540 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4e 20  .}../*.** Add N 
7a550 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  to the value of 
7a560 61 20 73 74 61 74 75 73 20 72 65 63 6f 72 64 2e  a status record.
7a570 20 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20    It is assumed 
7a580 74 68 61 74 20 74 68 65 0a 2a 2a 20 63 61 6c 6c  that the.** call
7a590 65 72 20 68 6f 6c 64 73 20 61 70 70 72 6f 70 72  er holds appropr
7a5a0 69 61 74 65 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a 53  iate locks..*/.S
7a5b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
7a5c0 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 73  id sqlite3Status
7a5d0 41 64 64 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20  Add(int op, int 
7a5e0 4e 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e 69  N){.  wsdStatIni
7a5f0 74 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e  t;.  assert( op>
7a600 3d 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53 69  =0 && op<ArraySi
7a610 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61  ze(wsdStat.nowVa
7a620 6c 75 65 29 20 29 3b 0a 20 20 77 73 64 53 74 61  lue) );.  wsdSta
7a630 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 20 2b  t.nowValue[op] +
7a640 3d 20 4e 3b 0a 20 20 69 66 28 20 77 73 64 53 74  = N;.  if( wsdSt
7a650 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3e  at.nowValue[op]>
7a660 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b  wsdStat.mxValue[
7a670 6f 70 5d 20 29 7b 0a 20 20 20 20 77 73 64 53 74  op] ){.    wsdSt
7a680 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d  at.mxValue[op] =
7a690 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75   wsdStat.nowValu
7a6a0 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  e[op];.  }.}../*
7a6b0 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 61 6c 75  .** Set the valu
7a6c0 65 20 6f 66 20 61 20 73 74 61 74 75 73 20 74 6f  e of a status to
7a6d0 20 58 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52   X..*/.SQLITE_PR
7a6e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
7a6f0 65 33 53 74 61 74 75 73 53 65 74 28 69 6e 74 20  e3StatusSet(int 
7a700 6f 70 2c 20 69 6e 74 20 58 29 7b 0a 20 20 77 73  op, int X){.  ws
7a710 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 73 73  dStatInit;.  ass
7a720 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f 70  ert( op>=0 && op
7a730 3c 41 72 72 61 79 53 69 7a 65 28 77 73 64 53 74  <ArraySize(wsdSt
7a740 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b 0a  at.nowValue) );.
7a750 20 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c    wsdStat.nowVal
7a760 75 65 5b 6f 70 5d 20 3d 20 58 3b 0a 20 20 69 66  ue[op] = X;.  if
7a770 28 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c  ( wsdStat.nowVal
7a780 75 65 5b 6f 70 5d 3e 77 73 64 53 74 61 74 2e 6d  ue[op]>wsdStat.m
7a790 78 56 61 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20 20  xValue[op] ){.  
7a7a0 20 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75    wsdStat.mxValu
7a7b0 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61 74 2e  e[op] = wsdStat.
7a7c0 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20  nowValue[op];.  
7a7d0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79  }.}../*.** Query
7a7e0 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74   status informat
7a7f0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ion..**.** This 
7a800 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61  implementation a
7a810 73 73 75 6d 65 73 20 74 68 61 74 20 72 65 61 64  ssumes that read
7a820 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20 61  ing or writing a
7a830 6e 20 61 6c 69 67 6e 65 64 0a 2a 2a 20 33 32 2d  n aligned.** 32-
7a840 62 69 74 20 69 6e 74 65 67 65 72 20 69 73 20 61  bit integer is a
7a850 6e 20 61 74 6f 6d 69 63 20 6f 70 65 72 61 74 69  n atomic operati
7a860 6f 6e 2e 20 20 49 66 20 74 68 61 74 20 61 73 73  on.  If that ass
7a870 75 6d 70 74 69 6f 6e 20 69 73 20 6e 6f 74 20 74  umption is not t
7a880 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 69  rue,.** then thi
7a890 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 74  s routine is not
7a8a0 20 74 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f 0a   threadsafe..*/.
7a8b0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
7a8c0 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 69 6e  qlite3_status(in
7a8d0 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 72 72  t op, int *pCurr
7a8e0 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69 67 68 77  ent, int *pHighw
7a8f0 61 74 65 72 2c 20 69 6e 74 20 72 65 73 65 74 46  ater, int resetF
7a900 6c 61 67 29 7b 0a 20 20 77 73 64 53 74 61 74 49  lag){.  wsdStatI
7a910 6e 69 74 3b 0a 20 20 69 66 28 20 6f 70 3c 30 20  nit;.  if( op<0 
7a920 7c 7c 20 6f 70 3e 3d 41 72 72 61 79 53 69 7a 65  || op>=ArraySize
7a930 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75  (wsdStat.nowValu
7a940 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  e) ){.    return
7a950 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
7a960 20 20 7d 0a 20 20 2a 70 43 75 72 72 65 6e 74 20    }.  *pCurrent 
7a970 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c  = wsdStat.nowVal
7a980 75 65 5b 6f 70 5d 3b 0a 20 20 2a 70 48 69 67 68  ue[op];.  *pHigh
7a990 77 61 74 65 72 20 3d 20 77 73 64 53 74 61 74 2e  water = wsdStat.
7a9a0 6d 78 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 69  mxValue[op];.  i
7a9b0 66 28 20 72 65 73 65 74 46 6c 61 67 20 29 7b 0a  f( resetFlag ){.
7a9c0 20 20 20 20 77 73 64 53 74 61 74 2e 6d 78 56 61      wsdStat.mxVa
7a9d0 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61  lue[op] = wsdSta
7a9e0 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a  t.nowValue[op];.
7a9f0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
7aa00 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
7aa10 20 51 75 65 72 79 20 73 74 61 74 75 73 20 69 6e   Query status in
7aa20 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20  formation for a 
7aa30 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20  single database 
7aa40 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2f 0a 53 51  connection.*/.SQ
7aa50 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
7aa60 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 0a  ite3_db_status(.
7aa70 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
7aa80 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
7aa90 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
7aaa0 6f 6e 20 77 68 6f 73 65 20 73 74 61 74 75 73 20  on whose status 
7aab0 69 73 20 64 65 73 69 72 65 64 20 2a 2f 0a 20 20  is desired */.  
7aac0 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20  int op,         
7aad0 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20        /* Status 
7aae0 76 65 72 62 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  verb */.  int *p
7aaf0 43 75 72 72 65 6e 74 2c 20 20 20 20 20 20 20 20  Current,        
7ab00 2f 2a 20 57 72 69 74 65 20 63 75 72 72 65 6e 74  /* Write current
7ab10 20 76 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a 20   value here */. 
7ab20 20 69 6e 74 20 2a 70 48 69 67 68 77 61 74 65 72   int *pHighwater
7ab30 2c 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20  ,      /* Write 
7ab40 68 69 67 68 2d 77 61 74 65 72 20 6d 61 72 6b 20  high-water mark 
7ab50 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65  here */.  int re
7ab60 73 65 74 46 6c 61 67 20 20 20 20 20 20 20 20 20  setFlag         
7ab70 2f 2a 20 52 65 73 65 74 20 68 69 67 68 2d 77 61  /* Reset high-wa
7ab80 74 65 72 20 6d 61 72 6b 20 69 66 20 74 72 75 65  ter mark if true
7ab90 20 2a 2f 0a 29 7b 0a 20 20 73 77 69 74 63 68 28   */.){.  switch(
7aba0 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20   op ){.    case 
7abb0 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f  SQLITE_DBSTATUS_
7abc0 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3a 20  LOOKASIDE_USED: 
7abd0 7b 0a 20 20 20 20 20 20 2a 70 43 75 72 72 65 6e  {.      *pCurren
7abe0 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  t = db->lookasid
7abf0 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 2a 70  e.nOut;.      *p
7ac00 48 69 67 68 77 61 74 65 72 20 3d 20 64 62 2d 3e  Highwater = db->
7ac10 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 3b  lookaside.mxOut;
7ac20 0a 20 20 20 20 20 20 69 66 28 20 72 65 73 65 74  .      if( reset
7ac30 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20  Flag ){.        
7ac40 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78  db->lookaside.mx
7ac50 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73  Out = db->lookas
7ac60 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20  ide.nOut;.      
7ac70 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
7ac80 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74     }.    default
7ac90 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  : {.      return
7aca0 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
7acb0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
7acc0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
7acd0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
7ace0 45 6e 64 20 6f 66 20 73 74 61 74 75 73 2e 63 20  End of status.c 
7acf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7ad00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7ad10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
7ad20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
7ad30 42 65 67 69 6e 20 66 69 6c 65 20 64 61 74 65 2e  Begin file date.
7ad40 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
7ad50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7ad60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
7ad70 2f 2a 0a 2a 2a 20 32 30 30 33 20 4f 63 74 6f 62  /*.** 2003 Octob
7ad80 65 72 20 33 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 31.**.** The 
7ad90 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
7ada0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
7adb0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
7adc0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
7add0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
7ade0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
7adf0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
7ae00 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
7ae10 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
7ae20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
7ae30 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
7ae40 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
7ae50 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
7ae60 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
7ae70 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
7ae80 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
7ae90 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
7aea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7aeb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7aec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7aed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7aee0 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
7aef0 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43  e contains the C
7af00 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20   functions that 
7af10 69 6d 70 6c 65 6d 65 6e 74 20 64 61 74 65 20 61  implement date a
7af20 6e 64 20 74 69 6d 65 0a 2a 2a 20 66 75 6e 63 74  nd time.** funct
7af30 69 6f 6e 73 20 66 6f 72 20 53 51 4c 69 74 65 2e  ions for SQLite.
7af40 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69    .**.** There i
7af50 73 20 6f 6e 6c 79 20 6f 6e 65 20 65 78 70 6f 72  s only one expor
7af60 74 65 64 20 73 79 6d 62 6f 6c 20 69 6e 20 74 68  ted symbol in th
7af70 69 73 20 66 69 6c 65 20 2d 20 74 68 65 20 66 75  is file - the fu
7af80 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71 6c 69 74 65  nction.** sqlite
7af90 33 52 65 67 69 73 74 65 72 44 61 74 65 54 69 6d  3RegisterDateTim
7afa0 65 46 75 6e 63 74 69 6f 6e 73 28 29 20 66 6f 75  eFunctions() fou
7afb0 6e 64 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d  nd at the bottom
7afc0 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a   of the file..**
7afd0 20 41 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65 20   All other code 
7afe0 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65 2e 0a  has file scope..
7aff0 2a 2a 0a 2a 2a 20 24 49 64 3a 20 64 61 74 65 2e  **.** $Id: date.
7b000 63 2c 76 20 31 2e 39 32 20 32 30 30 38 2f 31 30  c,v 1.92 2008/10
7b010 2f 31 33 20 31 35 3a 33 35 3a 30 39 20 64 72 68  /13 15:35:09 drh
7b020 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 53 51 4c   Exp $.**.** SQL
7b030 69 74 65 20 70 72 6f 63 65 73 73 65 73 20 61 6c  ite processes al
7b040 6c 20 74 69 6d 65 73 20 61 6e 64 20 64 61 74 65  l times and date
7b050 73 20 61 73 20 4a 75 6c 69 61 6e 20 44 61 79 20  s as Julian Day 
7b060 6e 75 6d 62 65 72 73 2e 20 20 54 68 65 0a 2a 2a  numbers.  The.**
7b070 20 64 61 74 65 73 20 61 6e 64 20 74 69 6d 65 73   dates and times
7b080 20 61 72 65 20 73 74 6f 72 65 64 20 61 73 20 74   are stored as t
7b090 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 61 79  he number of day
7b0a0 73 20 73 69 6e 63 65 20 6e 6f 6f 6e 0a 2a 2a 20  s since noon.** 
7b0b0 69 6e 20 47 72 65 65 6e 77 69 63 68 20 6f 6e 20  in Greenwich on 
7b0c0 4e 6f 76 65 6d 62 65 72 20 32 34 2c 20 34 37 31  November 24, 471
7b0d0 34 20 42 2e 43 2e 20 61 63 63 6f 72 64 69 6e 67  4 B.C. according
7b0e0 20 74 6f 20 74 68 65 20 47 72 65 67 6f 72 69 61   to the Gregoria
7b0f0 6e 0a 2a 2a 20 63 61 6c 65 6e 64 61 72 20 73 79  n.** calendar sy
7b100 73 74 65 6d 2e 20 0a 2a 2a 0a 2a 2a 20 31 39 37  stem. .**.** 197
7b110 30 2d 30 31 2d 30 31 20 30 30 3a 30 30 3a 30 30  0-01-01 00:00:00
7b120 20 69 73 20 4a 44 20 32 34 34 30 35 38 37 2e 35   is JD 2440587.5
7b130 0a 2a 2a 20 32 30 30 30 2d 30 31 2d 30 31 20 30  .** 2000-01-01 0
7b140 30 3a 30 30 3a 30 30 20 69 73 20 4a 44 20 32 34  0:00:00 is JD 24
7b150 35 31 35 34 34 2e 35 0a 2a 2a 0a 2a 2a 20 54 68  51544.5.**.** Th
7b160 69 73 20 69 6d 70 6c 65 6d 65 6e 74 69 6f 6e 20  is implemention 
7b170 72 65 71 75 69 72 65 73 20 79 65 61 72 73 20 74  requires years t
7b180 6f 20 62 65 20 65 78 70 72 65 73 73 65 64 20 61  o be expressed a
7b190 73 20 61 20 34 2d 64 69 67 69 74 20 6e 75 6d 62  s a 4-digit numb
7b1a0 65 72 0a 2a 2a 20 77 68 69 63 68 20 6d 65 61 6e  er.** which mean
7b1b0 73 20 74 68 61 74 20 6f 6e 6c 79 20 64 61 74 65  s that only date
7b1c0 73 20 62 65 74 77 65 65 6e 20 30 30 30 30 2d 30  s between 0000-0
7b1d0 31 2d 30 31 20 61 6e 64 20 39 39 39 39 2d 31 32  1-01 and 9999-12
7b1e0 2d 33 31 20 63 61 6e 0a 2a 2a 20 62 65 20 72 65  -31 can.** be re
7b1f0 70 72 65 73 65 6e 74 65 64 2c 20 65 76 65 6e 20  presented, even 
7b200 74 68 6f 75 67 68 20 6a 75 6c 69 61 6e 20 64 61  though julian da
7b210 79 20 6e 75 6d 62 65 72 73 20 61 6c 6c 6f 77 20  y numbers allow 
7b220 61 20 6d 75 63 68 20 77 69 64 65 72 0a 2a 2a 20  a much wider.** 
7b230 72 61 6e 67 65 20 6f 66 20 64 61 74 65 73 2e 0a  range of dates..
7b240 2a 2a 0a 2a 2a 20 54 68 65 20 47 72 65 67 6f 72  **.** The Gregor
7b250 69 61 6e 20 63 61 6c 65 6e 64 61 72 20 73 79 73  ian calendar sys
7b260 74 65 6d 20 69 73 20 75 73 65 64 20 66 6f 72 20  tem is used for 
7b270 61 6c 6c 20 64 61 74 65 73 20 61 6e 64 20 74 69  all dates and ti
7b280 6d 65 73 2c 0a 2a 2a 20 65 76 65 6e 20 74 68 6f  mes,.** even tho
7b290 73 65 20 74 68 61 74 20 70 72 65 64 61 74 65 20  se that predate 
7b2a0 74 68 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61  the Gregorian ca
7b2b0 6c 65 6e 64 61 72 2e 20 20 48 69 73 74 6f 72 69  lendar.  Histori
7b2c0 61 6e 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 75  ans usually.** u
7b2d0 73 65 20 74 68 65 20 4a 75 6c 69 61 6e 20 63 61  se the Julian ca
7b2e0 6c 65 6e 64 61 72 20 66 6f 72 20 64 61 74 65 73  lendar for dates
7b2f0 20 70 72 69 6f 72 20 74 6f 20 31 35 38 32 2d 31   prior to 1582-1
7b300 30 2d 31 35 20 61 6e 64 20 66 6f 72 20 73 6f 6d  0-15 and for som
7b310 65 0a 2a 2a 20 64 61 74 65 73 20 61 66 74 65 72  e.** dates after
7b320 77 61 72 64 73 2c 20 64 65 70 65 6e 64 69 6e 67  wards, depending
7b330 20 6f 6e 20 6c 6f 63 61 6c 65 2e 20 20 42 65 77   on locale.  Bew
7b340 61 72 65 20 6f 66 20 74 68 69 73 20 64 69 66 66  are of this diff
7b350 65 72 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  erence..**.** Th
7b360 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 61 6c 67  e conversion alg
7b370 6f 72 69 74 68 6d 73 20 61 72 65 20 69 6d 70 6c  orithms are impl
7b380 65 6d 65 6e 74 65 64 20 62 61 73 65 64 20 6f 6e  emented based on
7b390 20 64 65 73 63 72 69 70 74 69 6f 6e 73 0a 2a 2a   descriptions.**
7b3a0 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   in the followin
7b3b0 67 20 74 65 78 74 3a 0a 2a 2a 0a 2a 2a 20 20 20  g text:.**.**   
7b3c0 20 20 20 4a 65 61 6e 20 4d 65 65 75 73 0a 2a 2a     Jean Meeus.**
7b3d0 20 20 20 20 20 20 41 73 74 72 6f 6e 6f 6d 69 63        Astronomic
7b3e0 61 6c 20 41 6c 67 6f 72 69 74 68 6d 73 2c 20 32  al Algorithms, 2
7b3f0 6e 64 20 45 64 69 74 69 6f 6e 2c 20 31 39 39 38  nd Edition, 1998
7b400 0a 2a 2a 20 20 20 20 20 20 49 53 42 4d 20 30 2d  .**      ISBM 0-
7b410 39 34 33 33 39 36 2d 36 31 2d 31 0a 2a 2a 20 20  943396-61-1.**  
7b420 20 20 20 20 57 69 6c 6c 6d 61 6e 6e 2d 42 65 6c      Willmann-Bel
7b430 6c 2c 20 49 6e 63 0a 2a 2a 20 20 20 20 20 20 52  l, Inc.**      R
7b440 69 63 68 6d 6f 6e 64 2c 20 56 69 72 67 69 6e 69  ichmond, Virgini
7b450 61 20 28 55 53 41 29 0a 2a 2f 0a 23 69 6e 63 6c  a (USA).*/.#incl
7b460 75 64 65 20 3c 63 74 79 70 65 2e 68 3e 0a 23 69  ude <ctype.h>.#i
7b470 6e 63 6c 75 64 65 20 3c 74 69 6d 65 2e 68 3e 0a  nclude <time.h>.
7b480 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
7b490 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55  OMIT_DATETIME_FU
7b4a0 4e 43 53 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 72 65  NCS../*.** On re
7b4b0 63 65 6e 74 20 57 69 6e 64 6f 77 73 20 70 6c 61  cent Windows pla
7b4c0 74 66 6f 72 6d 73 2c 20 74 68 65 20 6c 6f 63 61  tforms, the loca
7b4d0 6c 74 69 6d 65 5f 73 28 29 20 66 75 6e 63 74 69  ltime_s() functi
7b4e0 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 0a  on is available.
7b4f0 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20 74 68  ** as part of th
7b500 65 20 22 53 65 63 75 72 65 20 43 52 54 22 2e 20  e "Secure CRT". 
7b510 49 74 20 69 73 20 65 73 73 65 6e 74 69 61 6c 6c  It is essentiall
7b520 79 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20  y equivalent to 
7b530 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28  .** localtime_r(
7b540 29 20 61 76 61 69 6c 61 62 6c 65 20 75 6e 64 65  ) available unde
7b550 72 20 6d 6f 73 74 20 50 4f 53 49 58 20 70 6c 61  r most POSIX pla
7b560 74 66 6f 72 6d 73 2c 20 65 78 63 65 70 74 20 74  tforms, except t
7b570 68 61 74 20 74 68 65 20 0a 2a 2a 20 6f 72 64 65  hat the .** orde
7b580 72 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74  r of the paramet
7b590 65 72 73 20 69 73 20 72 65 76 65 72 73 65 64 2e  ers is reversed.
7b5a0 0a 2a 2a 0a 2a 2a 20 53 65 65 20 68 74 74 70 3a  .**.** See http:
7b5b0 2f 2f 6d 73 64 6e 2e 6d 69 63 72 6f 73 6f 66 74  //msdn.microsoft
7b5c0 2e 63 6f 6d 2f 65 6e 2d 75 73 2f 6c 69 62 72 61  .com/en-us/libra
7b5d0 72 79 2f 61 34 34 32 78 33 79 65 28 56 53 2e 38  ry/a442x3ye(VS.8
7b5e0 30 29 2e 61 73 70 78 2e 0a 2a 2a 0a 2a 2a 20 49  0).aspx..**.** I
7b5f0 66 20 74 68 65 20 75 73 65 72 20 68 61 73 20 6e  f the user has n
7b600 6f 74 20 69 6e 64 69 63 61 74 65 64 20 74 6f 20  ot indicated to 
7b610 75 73 65 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28  use localtime_r(
7b620 29 20 6f 72 20 6c 6f 63 61 6c 74 69 6d 65 5f 73  ) or localtime_s
7b630 28 29 0a 2a 2a 20 61 6c 72 65 61 64 79 2c 20 63  ().** already, c
7b640 68 65 63 6b 20 66 6f 72 20 61 6e 20 4d 53 56 43  heck for an MSVC
7b650 20 62 75 69 6c 64 20 65 6e 76 69 72 6f 6e 6d 65   build environme
7b660 6e 74 20 74 68 61 74 20 70 72 6f 76 69 64 65 73  nt that provides
7b670 20 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f 73   .** localtime_s
7b680 28 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69  ()..*/.#if !defi
7b690 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49  ned(HAVE_LOCALTI
7b6a0 4d 45 5f 52 29 20 26 26 20 21 64 65 66 69 6e 65  ME_R) && !define
7b6b0 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45  d(HAVE_LOCALTIME
7b6c0 5f 53 29 20 26 26 20 5c 0a 20 20 20 20 20 64 65  _S) && \.     de
7b6d0 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 20  fined(_MSC_VER) 
7b6e0 26 26 20 64 65 66 69 6e 65 64 28 5f 43 52 54 5f  && defined(_CRT_
7b6f0 49 4e 53 45 43 55 52 45 5f 44 45 50 52 45 43 41  INSECURE_DEPRECA
7b700 54 45 29 0a 23 64 65 66 69 6e 65 20 48 41 56 45  TE).#define HAVE
7b710 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 20 31 0a 23  _LOCALTIME_S 1.#
7b720 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 20 73  endif../*.** A s
7b730 74 72 75 63 74 75 72 65 20 66 6f 72 20 68 6f 6c  tructure for hol
7b740 64 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 64 61  ding a single da
7b750 74 65 20 61 6e 64 20 74 69 6d 65 2e 0a 2a 2f 0a  te and time..*/.
7b760 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 44  typedef struct D
7b770 61 74 65 54 69 6d 65 20 44 61 74 65 54 69 6d 65  ateTime DateTime
7b780 3b 0a 73 74 72 75 63 74 20 44 61 74 65 54 69 6d  ;.struct DateTim
7b790 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  e {.  sqlite3_in
7b7a0 74 36 34 20 69 4a 44 3b 20 2f 2a 20 54 68 65 20  t64 iJD; /* The 
7b7b0 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65  julian day numbe
7b7c0 72 20 74 69 6d 65 73 20 38 36 34 30 30 30 30 30  r times 86400000
7b7d0 20 2a 2f 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20   */.  int Y, M, 
7b7e0 44 3b 20 20 20 20 20 20 20 2f 2a 20 59 65 61 72  D;       /* Year
7b7f0 2c 20 6d 6f 6e 74 68 2c 20 61 6e 64 20 64 61 79  , month, and day
7b800 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 6d 3b 20   */.  int h, m; 
7b810 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 75 72           /* Hour
7b820 20 61 6e 64 20 6d 69 6e 75 74 65 73 20 2a 2f 0a   and minutes */.
7b830 20 20 69 6e 74 20 74 7a 3b 20 20 20 20 20 20 20    int tz;       
7b840 20 20 20 20 20 2f 2a 20 54 69 6d 65 7a 6f 6e 65       /* Timezone
7b850 20 6f 66 66 73 65 74 20 69 6e 20 6d 69 6e 75 74   offset in minut
7b860 65 73 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 73  es */.  double s
7b870 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65  ;          /* Se
7b880 63 6f 6e 64 73 20 2a 2f 0a 20 20 63 68 61 72 20  conds */.  char 
7b890 76 61 6c 69 64 59 4d 44 3b 20 20 20 20 20 2f 2a  validYMD;     /*
7b8a0 20 54 72 75 65 20 69 66 20 59 2c 4d 2c 44 20 61   True if Y,M,D a
7b8b0 72 65 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68  re valid */.  ch
7b8c0 61 72 20 76 61 6c 69 64 48 4d 53 3b 20 20 20 20  ar validHMS;    
7b8d0 20 2f 2a 20 54 72 75 65 20 69 66 20 68 2c 6d 2c   /* True if h,m,
7b8e0 73 20 61 72 65 20 76 61 6c 69 64 20 2a 2f 0a 20  s are valid */. 
7b8f0 20 63 68 61 72 20 76 61 6c 69 64 4a 44 3b 20 20   char validJD;  
7b900 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69      /* True if i
7b910 4a 44 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20  JD is valid */. 
7b920 20 63 68 61 72 20 76 61 6c 69 64 54 5a 3b 20 20   char validTZ;  
7b930 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
7b940 7a 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 7d 3b  z is valid */.};
7b950 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  .../*.** Convert
7b960 20 7a 44 61 74 65 20 69 6e 74 6f 20 6f 6e 65 20   zDate into one 
7b970 6f 72 20 6d 6f 72 65 20 69 6e 74 65 67 65 72 73  or more integers
7b980 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 61 72  .  Additional ar
7b990 67 75 6d 65 6e 74 73 0a 2a 2a 20 63 6f 6d 65 20  guments.** come 
7b9a0 69 6e 20 67 72 6f 75 70 73 20 6f 66 20 35 20 61  in groups of 5 a
7b9b0 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
7b9c0 20 20 20 20 20 20 20 4e 20 20 20 20 20 20 20 6e         N       n
7b9d0 75 6d 62 65 72 20 6f 66 20 64 69 67 69 74 73 20  umber of digits 
7b9e0 69 6e 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a  in the integer.*
7b9f0 2a 20 20 20 20 20 20 20 6d 69 6e 20 20 20 20 20  *       min     
7ba00 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20  minimum allowed 
7ba10 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74  value of the int
7ba20 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6d 61  eger.**       ma
7ba30 78 20 20 20 20 20 6d 61 78 69 6d 75 6d 20 61 6c  x     maximum al
7ba40 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 20 74  lowed value of t
7ba50 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20  he integer.**   
7ba60 20 20 20 20 6e 65 78 74 43 20 20 20 66 69 72 73      nextC   firs
7ba70 74 20 63 68 61 72 61 63 74 65 72 20 61 66 74 65  t character afte
7ba80 72 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a  r the integer.**
7ba90 20 20 20 20 20 20 20 70 56 61 6c 20 20 20 20 77         pVal    w
7baa0 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 74 68  here to write th
7bab0 65 20 69 6e 74 65 67 65 72 73 20 76 61 6c 75 65  e integers value
7bac0 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73 69  ..**.** Conversi
7bad0 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 75 6e 74  ons continue unt
7bae0 69 6c 20 6f 6e 65 20 77 69 74 68 20 6e 65 78 74  il one with next
7baf0 43 3d 3d 30 20 69 73 20 65 6e 63 6f 75 6e 74 65  C==0 is encounte
7bb00 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 75 6e 63  red..** The func
7bb10 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65  tion returns the
7bb20 20 6e 75 6d 62 65 72 20 6f 66 20 73 75 63 63 65   number of succe
7bb30 73 73 66 75 6c 20 63 6f 6e 76 65 72 73 69 6f 6e  ssful conversion
7bb40 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
7bb50 20 67 65 74 44 69 67 69 74 73 28 63 6f 6e 73 74   getDigits(const
7bb60 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 2e 2e   char *zDate, ..
7bb70 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70  .){.  va_list ap
7bb80 3b 0a 20 20 69 6e 74 20 76 61 6c 3b 0a 20 20 69  ;.  int val;.  i
7bb90 6e 74 20 4e 3b 0a 20 20 69 6e 74 20 6d 69 6e 3b  nt N;.  int min;
7bba0 0a 20 20 69 6e 74 20 6d 61 78 3b 0a 20 20 69 6e  .  int max;.  in
7bbb0 74 20 6e 65 78 74 43 3b 0a 20 20 69 6e 74 20 2a  t nextC;.  int *
7bbc0 70 56 61 6c 3b 0a 20 20 69 6e 74 20 63 6e 74 20  pVal;.  int cnt 
7bbd0 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 28  = 0;.  va_start(
7bbe0 61 70 2c 20 7a 44 61 74 65 29 3b 0a 20 20 64 6f  ap, zDate);.  do
7bbf0 7b 0a 20 20 20 20 4e 20 3d 20 76 61 5f 61 72 67  {.    N = va_arg
7bc00 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 6d  (ap, int);.    m
7bc10 69 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  in = va_arg(ap, 
7bc20 69 6e 74 29 3b 0a 20 20 20 20 6d 61 78 20 3d 20  int);.    max = 
7bc30 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b  va_arg(ap, int);
7bc40 0a 20 20 20 20 6e 65 78 74 43 20 3d 20 76 61 5f  .    nextC = va_
7bc50 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20  arg(ap, int);.  
7bc60 20 20 70 56 61 6c 20 3d 20 76 61 5f 61 72 67 28    pVal = va_arg(
7bc70 61 70 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20 76  ap, int*);.    v
7bc80 61 6c 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c  al = 0;.    whil
7bc90 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 20 20  e( N-- ){.      
7bca0 69 66 28 20 21 69 73 64 69 67 69 74 28 2a 28 75  if( !isdigit(*(u
7bcb0 38 2a 29 7a 44 61 74 65 29 20 29 7b 0a 20 20 20  8*)zDate) ){.   
7bcc0 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 67 65       goto end_ge
7bcd0 74 44 69 67 69 74 73 3b 0a 20 20 20 20 20 20 7d  tDigits;.      }
7bce0 0a 20 20 20 20 20 20 76 61 6c 20 3d 20 76 61 6c  .      val = val
7bcf0 2a 31 30 20 2b 20 2a 7a 44 61 74 65 20 2d 20 27  *10 + *zDate - '
7bd00 30 27 3b 0a 20 20 20 20 20 20 7a 44 61 74 65 2b  0';.      zDate+
7bd10 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  +;.    }.    if(
7bd20 20 76 61 6c 3c 6d 69 6e 20 7c 7c 20 76 61 6c 3e   val<min || val>
7bd30 6d 61 78 20 7c 7c 20 28 6e 65 78 74 43 21 3d 30  max || (nextC!=0
7bd40 20 26 26 20 6e 65 78 74 43 21 3d 2a 7a 44 61 74   && nextC!=*zDat
7bd50 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  e) ){.      goto
7bd60 20 65 6e 64 5f 67 65 74 44 69 67 69 74 73 3b 0a   end_getDigits;.
7bd70 20 20 20 20 7d 0a 20 20 20 20 2a 70 56 61 6c 20      }.    *pVal 
7bd80 3d 20 76 61 6c 3b 0a 20 20 20 20 7a 44 61 74 65  = val;.    zDate
7bd90 2b 2b 3b 0a 20 20 20 20 63 6e 74 2b 2b 3b 0a 20  ++;.    cnt++;. 
7bda0 20 7d 77 68 69 6c 65 28 20 6e 65 78 74 43 20 29   }while( nextC )
7bdb0 3b 0a 65 6e 64 5f 67 65 74 44 69 67 69 74 73 3a  ;.end_getDigits:
7bdc0 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20  .  va_end(ap);. 
7bdd0 20 72 65 74 75 72 6e 20 63 6e 74 3b 0a 7d 0a 0a   return cnt;.}..
7bde0 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 65 78 74 20  /*.** Read text 
7bdf0 66 72 6f 6d 20 7a 5b 5d 20 61 6e 64 20 63 6f 6e  from z[] and con
7be00 76 65 72 74 20 69 6e 74 6f 20 61 20 66 6c 6f 61  vert into a floa
7be10 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65  ting point numbe
7be20 72 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68  r.  Return.** th
7be30 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 67 69  e number of digi
7be40 74 73 20 63 6f 6e 76 65 72 74 65 64 2e 0a 2a 2f  ts converted..*/
7be50 0a 23 64 65 66 69 6e 65 20 67 65 74 56 61 6c 75  .#define getValu
7be60 65 20 73 71 6c 69 74 65 33 41 74 6f 46 0a 0a 2f  e sqlite3AtoF../
7be70 2a 0a 2a 2a 20 50 61 72 73 65 20 61 20 74 69 6d  *.** Parse a tim
7be80 65 7a 6f 6e 65 20 65 78 74 65 6e 73 69 6f 6e 20  ezone extension 
7be90 6f 6e 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20  on the end of a 
7bea0 64 61 74 65 2d 74 69 6d 65 2e 0a 2a 2a 20 54 68  date-time..** Th
7beb0 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 73 20 6f  e extension is o
7bec0 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a  f the form:.**.*
7bed0 2a 20 20 20 20 20 20 20 20 28 2b 2f 2d 29 48 48  *        (+/-)HH
7bee0 3a 4d 4d 0a 2a 2a 0a 2a 2a 20 4f 72 20 74 68 65  :MM.**.** Or the
7bef0 20 22 7a 75 6c 75 22 20 6e 6f 74 61 74 69 6f 6e   "zulu" notation
7bf00 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 5a  :.**.**        Z
7bf10 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61  .**.** If the pa
7bf20 72 73 65 20 69 73 20 73 75 63 63 65 73 73 66 75  rse is successfu
7bf30 6c 2c 20 77 72 69 74 65 20 74 68 65 20 6e 75 6d  l, write the num
7bf40 62 65 72 20 6f 66 20 6d 69 6e 75 74 65 73 0a 2a  ber of minutes.*
7bf50 2a 20 6f 66 20 63 68 61 6e 67 65 20 69 6e 20 70  * of change in p
7bf60 2d 3e 74 7a 20 61 6e 64 20 72 65 74 75 72 6e 20  ->tz and return 
7bf70 30 2e 20 20 49 66 20 61 20 70 61 72 73 65 72 20  0.  If a parser 
7bf80 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a  error occurs,.**
7bf90 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f   return non-zero
7bfa0 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 69 73 73 69 6e  ..**.** A missin
7bfb0 67 20 73 70 65 63 69 66 69 65 72 20 69 73 20 6e  g specifier is n
7bfc0 6f 74 20 63 6f 6e 73 69 64 65 72 65 64 20 61 6e  ot considered an
7bfd0 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69   error..*/.stati
7bfe0 63 20 69 6e 74 20 70 61 72 73 65 54 69 6d 65 7a  c int parseTimez
7bff0 6f 6e 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  one(const char *
7c000 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d 65 20  zDate, DateTime 
7c010 2a 70 29 7b 0a 20 20 69 6e 74 20 73 67 6e 20 3d  *p){.  int sgn =
7c020 20 30 3b 0a 20 20 69 6e 74 20 6e 48 72 2c 20 6e   0;.  int nHr, n
7c030 4d 6e 3b 0a 20 20 69 6e 74 20 63 3b 0a 20 20 77  Mn;.  int c;.  w
7c040 68 69 6c 65 28 20 69 73 73 70 61 63 65 28 2a 28  hile( isspace(*(
7c050 75 38 2a 29 7a 44 61 74 65 29 20 29 7b 20 7a 44  u8*)zDate) ){ zD
7c060 61 74 65 2b 2b 3b 20 7d 0a 20 20 70 2d 3e 74 7a  ate++; }.  p->tz
7c070 20 3d 20 30 3b 0a 20 20 63 20 3d 20 2a 7a 44 61   = 0;.  c = *zDa
7c080 74 65 3b 0a 20 20 69 66 28 20 63 3d 3d 27 2d 27  te;.  if( c=='-'
7c090 20 29 7b 0a 20 20 20 20 73 67 6e 20 3d 20 2d 31   ){.    sgn = -1
7c0a0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d  ;.  }else if( c=
7c0b0 3d 27 2b 27 20 29 7b 0a 20 20 20 20 73 67 6e 20  ='+' ){.    sgn 
7c0c0 3d 20 2b 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66  = +1;.  }else if
7c0d0 28 20 63 3d 3d 27 5a 27 20 7c 7c 20 63 3d 3d 27  ( c=='Z' || c=='
7c0e0 7a 27 20 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b  z' ){.    zDate+
7c0f0 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 7a 75 6c 75  +;.    goto zulu
7c100 5f 74 69 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a  _time;.  }else{.
7c110 20 20 20 20 72 65 74 75 72 6e 20 63 21 3d 30 3b      return c!=0;
7c120 0a 20 20 7d 0a 20 20 7a 44 61 74 65 2b 2b 3b 0a  .  }.  zDate++;.
7c130 20 20 69 66 28 20 67 65 74 44 69 67 69 74 73 28    if( getDigits(
7c140 7a 44 61 74 65 2c 20 32 2c 20 30 2c 20 31 34 2c  zDate, 2, 0, 14,
7c150 20 27 3a 27 2c 20 26 6e 48 72 2c 20 32 2c 20 30   ':', &nHr, 2, 0
7c160 2c 20 35 39 2c 20 30 2c 20 26 6e 4d 6e 29 21 3d  , 59, 0, &nMn)!=
7c170 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  2 ){.    return 
7c180 31 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 65 20 2b  1;.  }.  zDate +
7c190 3d 20 35 3b 0a 20 20 70 2d 3e 74 7a 20 3d 20 73  = 5;.  p->tz = s
7c1a0 67 6e 2a 28 6e 4d 6e 20 2b 20 6e 48 72 2a 36 30  gn*(nMn + nHr*60
7c1b0 29 3b 0a 7a 75 6c 75 5f 74 69 6d 65 3a 0a 20 20  );.zulu_time:.  
7c1c0 77 68 69 6c 65 28 20 69 73 73 70 61 63 65 28 2a  while( isspace(*
7c1d0 28 75 38 2a 29 7a 44 61 74 65 29 20 29 7b 20 7a  (u8*)zDate) ){ z
7c1e0 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 72 65 74 75  Date++; }.  retu
7c1f0 72 6e 20 2a 7a 44 61 74 65 21 3d 30 3b 0a 7d 0a  rn *zDate!=0;.}.
7c200 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 74 69 6d  ./*.** Parse tim
7c210 65 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 48  es of the form H
7c220 48 3a 4d 4d 20 6f 72 20 48 48 3a 4d 4d 3a 53 53  H:MM or HH:MM:SS
7c230 20 6f 72 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46   or HH:MM:SS.FFF
7c240 46 2e 0a 2a 2a 20 54 68 65 20 48 48 2c 20 4d 4d  F..** The HH, MM
7c250 2c 20 61 6e 64 20 53 53 20 6d 75 73 74 20 65 61  , and SS must ea
7c260 63 68 20 62 65 20 65 78 61 63 74 6c 79 20 32 20  ch be exactly 2 
7c270 64 69 67 69 74 73 2e 20 20 54 68 65 0a 2a 2a 20  digits.  The.** 
7c280 66 72 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e  fractional secon
7c290 64 73 20 46 46 46 46 20 63 61 6e 20 62 65 20 6f  ds FFFF can be o
7c2a0 6e 65 20 6f 72 20 6d 6f 72 65 20 64 69 67 69 74  ne or more digit
7c2b0 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  s..**.** Return 
7c2c0 31 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  1 if there is a 
7c2d0 70 61 72 73 69 6e 67 20 65 72 72 6f 72 20 61 6e  parsing error an
7c2e0 64 20 30 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a  d 0 on success..
7c2f0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
7c300 72 73 65 48 68 4d 6d 53 73 28 63 6f 6e 73 74 20  rseHhMmSs(const 
7c310 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 74  char *zDate, Dat
7c320 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74  eTime *p){.  int
7c330 20 68 2c 20 6d 2c 20 73 3b 0a 20 20 64 6f 75 62   h, m, s;.  doub
7c340 6c 65 20 6d 73 20 3d 20 30 2e 30 3b 0a 20 20 69  le ms = 0.0;.  i
7c350 66 28 20 67 65 74 44 69 67 69 74 73 28 7a 44 61  f( getDigits(zDa
7c360 74 65 2c 20 32 2c 20 30 2c 20 32 34 2c 20 27 3a  te, 2, 0, 24, ':
7c370 27 2c 20 26 68 2c 20 32 2c 20 30 2c 20 35 39 2c  ', &h, 2, 0, 59,
7c380 20 30 2c 20 26 6d 29 21 3d 32 20 29 7b 0a 20 20   0, &m)!=2 ){.  
7c390 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
7c3a0 20 20 7a 44 61 74 65 20 2b 3d 20 35 3b 0a 20 20    zDate += 5;.  
7c3b0 69 66 28 20 2a 7a 44 61 74 65 3d 3d 27 3a 27 20  if( *zDate==':' 
7c3c0 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a  ){.    zDate++;.
7c3d0 20 20 20 20 69 66 28 20 67 65 74 44 69 67 69 74      if( getDigit
7c3e0 73 28 7a 44 61 74 65 2c 20 32 2c 20 30 2c 20 35  s(zDate, 2, 0, 5
7c3f0 39 2c 20 30 2c 20 26 73 29 21 3d 31 20 29 7b 0a  9, 0, &s)!=1 ){.
7c400 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
7c410 20 20 20 20 7d 0a 20 20 20 20 7a 44 61 74 65 20      }.    zDate 
7c420 2b 3d 20 32 3b 0a 20 20 20 20 69 66 28 20 2a 7a  += 2;.    if( *z
7c430 44 61 74 65 3d 3d 27 2e 27 20 26 26 20 69 73 64  Date=='.' && isd
7c440 69 67 69 74 28 28 75 38 29 7a 44 61 74 65 5b 31  igit((u8)zDate[1
7c450 5d 29 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62  ]) ){.      doub
7c460 6c 65 20 72 53 63 61 6c 65 20 3d 20 31 2e 30 3b  le rScale = 1.0;
7c470 0a 20 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a  .      zDate++;.
7c480 20 20 20 20 20 20 77 68 69 6c 65 28 20 69 73 64        while( isd
7c490 69 67 69 74 28 2a 28 75 38 2a 29 7a 44 61 74 65  igit(*(u8*)zDate
7c4a0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 73 20  ) ){.        ms 
7c4b0 3d 20 6d 73 2a 31 30 2e 30 20 2b 20 2a 7a 44 61  = ms*10.0 + *zDa
7c4c0 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20  te - '0';.      
7c4d0 20 20 72 53 63 61 6c 65 20 2a 3d 20 31 30 2e 30    rScale *= 10.0
7c4e0 3b 0a 20 20 20 20 20 20 20 20 7a 44 61 74 65 2b  ;.        zDate+
7c4f0 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  +;.      }.     
7c500 20 6d 73 20 2f 3d 20 72 53 63 61 6c 65 3b 0a 20   ms /= rScale;. 
7c510 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
7c520 20 20 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70    s = 0;.  }.  p
7c530 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20  ->validJD = 0;. 
7c540 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31   p->validHMS = 1
7c550 3b 0a 20 20 70 2d 3e 68 20 3d 20 68 3b 0a 20 20  ;.  p->h = h;.  
7c560 70 2d 3e 6d 20 3d 20 6d 3b 0a 20 20 70 2d 3e 73  p->m = m;.  p->s
7c570 20 3d 20 73 20 2b 20 6d 73 3b 0a 20 20 69 66 28   = s + ms;.  if(
7c580 20 70 61 72 73 65 54 69 6d 65 7a 6f 6e 65 28 7a   parseTimezone(z
7c590 44 61 74 65 2c 20 70 29 20 29 20 72 65 74 75 72  Date, p) ) retur
7c5a0 6e 20 31 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 54  n 1;.  p->validT
7c5b0 5a 20 3d 20 70 2d 3e 74 7a 21 3d 30 3b 0a 20 20  Z = p->tz!=0;.  
7c5c0 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
7c5d0 2a 2a 20 43 6f 6e 76 65 72 74 20 66 72 6f 6d 20  ** Convert from 
7c5e0 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d  YYYY-MM-DD HH:MM
7c5f0 3a 53 53 20 74 6f 20 6a 75 6c 69 61 6e 20 64 61  :SS to julian da
7c600 79 2e 20 20 57 65 20 61 6c 77 61 79 73 20 61 73  y.  We always as
7c610 73 75 6d 65 0a 2a 2a 20 74 68 61 74 20 74 68 65  sume.** that the
7c620 20 59 59 59 59 2d 4d 4d 2d 44 44 20 69 73 20 61   YYYY-MM-DD is a
7c630 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20  ccording to the 
7c640 47 72 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64  Gregorian calend
7c650 61 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72 65  ar..**.** Refere
7c660 6e 63 65 3a 20 20 4d 65 65 75 73 20 70 61 67 65  nce:  Meeus page
7c670 20 36 31 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f   61.*/.static vo
7c680 69 64 20 63 6f 6d 70 75 74 65 4a 44 28 44 61 74  id computeJD(Dat
7c690 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74  eTime *p){.  int
7c6a0 20 59 2c 20 4d 2c 20 44 2c 20 41 2c 20 42 2c 20   Y, M, D, A, B, 
7c6b0 58 31 2c 20 58 32 3b 0a 0a 20 20 69 66 28 20 70  X1, X2;..  if( p
7c6c0 2d 3e 76 61 6c 69 64 4a 44 20 29 20 72 65 74 75  ->validJD ) retu
7c6d0 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c  rn;.  if( p->val
7c6e0 69 64 59 4d 44 20 29 7b 0a 20 20 20 20 59 20 3d  idYMD ){.    Y =
7c6f0 20 70 2d 3e 59 3b 0a 20 20 20 20 4d 20 3d 20 70   p->Y;.    M = p
7c700 2d 3e 4d 3b 0a 20 20 20 20 44 20 3d 20 70 2d 3e  ->M;.    D = p->
7c710 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  D;.  }else{.    
7c720 59 20 3d 20 32 30 30 30 3b 20 20 2f 2a 20 49 66  Y = 2000;  /* If
7c730 20 6e 6f 20 59 4d 44 20 73 70 65 63 69 66 69 65   no YMD specifie
7c740 64 2c 20 61 73 73 75 6d 65 20 32 30 30 30 2d 4a  d, assume 2000-J
7c750 61 6e 2d 30 31 20 2a 2f 0a 20 20 20 20 4d 20 3d  an-01 */.    M =
7c760 20 31 3b 0a 20 20 20 20 44 20 3d 20 31 3b 0a 20   1;.    D = 1;. 
7c770 20 7d 0a 20 20 69 66 28 20 4d 3c 3d 32 20 29 7b   }.  if( M<=2 ){
7c780 0a 20 20 20 20 59 2d 2d 3b 0a 20 20 20 20 4d 20  .    Y--;.    M 
7c790 2b 3d 20 31 32 3b 0a 20 20 7d 0a 20 20 41 20 3d  += 12;.  }.  A =
7c7a0 20 59 2f 31 30 30 3b 0a 20 20 42 20 3d 20 32 20   Y/100;.  B = 2 
7c7b0 2d 20 41 20 2b 20 28 41 2f 34 29 3b 0a 20 20 58  - A + (A/4);.  X
7c7c0 31 20 3d 20 33 36 35 2e 32 35 2a 28 59 2b 34 37  1 = 365.25*(Y+47
7c7d0 31 36 29 3b 0a 20 20 58 32 20 3d 20 33 30 2e 36  16);.  X2 = 30.6
7c7e0 30 30 31 2a 28 4d 2b 31 29 3b 0a 20 20 70 2d 3e  001*(M+1);.  p->
7c7f0 69 4a 44 20 3d 20 28 58 31 20 2b 20 58 32 20 2b  iJD = (X1 + X2 +
7c800 20 44 20 2b 20 42 20 2d 20 31 35 32 34 2e 35 29   D + B - 1524.5)
7c810 2a 38 36 34 30 30 30 30 30 3b 0a 20 20 70 2d 3e  *86400000;.  p->
7c820 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 69  validJD = 1;.  i
7c830 66 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 29  f( p->validHMS )
7c840 7b 0a 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20  {.    p->iJD += 
7c850 70 2d 3e 68 2a 33 36 30 30 30 30 30 20 2b 20 70  p->h*3600000 + p
7c860 2d 3e 6d 2a 36 30 30 30 30 20 2b 20 70 2d 3e 73  ->m*60000 + p->s
7c870 2a 31 30 30 30 3b 0a 20 20 20 20 69 66 28 20 70  *1000;.    if( p
7c880 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 20 20  ->validTZ ){.   
7c890 20 20 20 70 2d 3e 69 4a 44 20 2d 3d 20 70 2d 3e     p->iJD -= p->
7c8a0 74 7a 2a 36 30 30 30 30 3b 0a 20 20 20 20 20 20  tz*60000;.      
7c8b0 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 30 3b  p->validYMD = 0;
7c8c0 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 48  .      p->validH
7c8d0 4d 53 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d  MS = 0;.      p-
7c8e0 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20  >validTZ = 0;.  
7c8f0 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
7c900 20 50 61 72 73 65 20 64 61 74 65 73 20 6f 66 20   Parse dates of 
7c910 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20  the form.**.**  
7c920 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48     YYYY-MM-DD HH
7c930 3a 4d 4d 3a 53 53 2e 46 46 46 0a 2a 2a 20 20 20  :MM:SS.FFF.**   
7c940 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a    YYYY-MM-DD HH:
7c950 4d 4d 3a 53 53 0a 2a 2a 20 20 20 20 20 59 59 59  MM:SS.**     YYY
7c960 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 0a 2a 2a  Y-MM-DD HH:MM.**
7c970 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 0a       YYYY-MM-DD.
7c980 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20  **.** Write the 
7c990 72 65 73 75 6c 74 20 69 6e 74 6f 20 74 68 65 20  result into the 
7c9a0 44 61 74 65 54 69 6d 65 20 73 74 72 75 63 74 75  DateTime structu
7c9b0 72 65 20 61 6e 64 20 72 65 74 75 72 6e 20 30 0a  re and return 0.
7c9c0 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e  ** on success an
7c9d0 64 20 31 20 69 66 20 74 68 65 20 69 6e 70 75 74  d 1 if the input
7c9e0 20 73 74 72 69 6e 67 20 69 73 20 6e 6f 74 20 61   string is not a
7c9f0 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20   well-formed.** 
7ca00 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  date..*/.static 
7ca10 69 6e 74 20 70 61 72 73 65 59 79 79 79 4d 6d 44  int parseYyyyMmD
7ca20 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44  d(const char *zD
7ca30 61 74 65 2c 20 44 61 74 65 54 69 6d 65 20 2a 70  ate, DateTime *p
7ca40 29 7b 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44  ){.  int Y, M, D
7ca50 2c 20 6e 65 67 3b 0a 0a 20 20 69 66 28 20 7a 44  , neg;..  if( zD
7ca60 61 74 65 5b 30 5d 3d 3d 27 2d 27 20 29 7b 0a 20  ate[0]=='-' ){. 
7ca70 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20     zDate++;.    
7ca80 6e 65 67 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  neg = 1;.  }else
7ca90 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20  {.    neg = 0;. 
7caa0 20 7d 0a 20 20 69 66 28 20 67 65 74 44 69 67 69   }.  if( getDigi
7cab0 74 73 28 7a 44 61 74 65 2c 34 2c 30 2c 39 39 39  ts(zDate,4,0,999
7cac0 39 2c 27 2d 27 2c 26 59 2c 32 2c 31 2c 31 32 2c  9,'-',&Y,2,1,12,
7cad0 27 2d 27 2c 26 4d 2c 32 2c 31 2c 33 31 2c 30 2c  '-',&M,2,1,31,0,
7cae0 26 44 29 21 3d 33 20 29 7b 0a 20 20 20 20 72 65  &D)!=3 ){.    re
7caf0 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44  turn 1;.  }.  zD
7cb00 61 74 65 20 2b 3d 20 31 30 3b 0a 20 20 77 68 69  ate += 10;.  whi
7cb10 6c 65 28 20 69 73 73 70 61 63 65 28 2a 28 75 38  le( isspace(*(u8
7cb20 2a 29 7a 44 61 74 65 29 20 7c 7c 20 27 54 27 3d  *)zDate) || 'T'=
7cb30 3d 2a 28 75 38 2a 29 7a 44 61 74 65 20 29 7b 20  =*(u8*)zDate ){ 
7cb40 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 69 66 28  zDate++; }.  if(
7cb50 20 70 61 72 73 65 48 68 4d 6d 53 73 28 7a 44 61   parseHhMmSs(zDa
7cb60 74 65 2c 20 70 29 3d 3d 30 20 29 7b 0a 20 20 20  te, p)==0 ){.   
7cb70 20 2f 2a 20 57 65 20 67 6f 74 20 74 68 65 20 74   /* We got the t
7cb80 69 6d 65 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69  ime */.  }else i
7cb90 66 28 20 2a 7a 44 61 74 65 3d 3d 30 20 29 7b 0a  f( *zDate==0 ){.
7cba0 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20      p->validHMS 
7cbb0 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 0;.  }else{.  
7cbc0 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
7cbd0 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30    p->validJD = 0
7cbe0 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20  ;.  p->validYMD 
7cbf0 3d 20 31 3b 0a 20 20 70 2d 3e 59 20 3d 20 6e 65  = 1;.  p->Y = ne
7cc00 67 20 3f 20 2d 59 20 3a 20 59 3b 0a 20 20 70 2d  g ? -Y : Y;.  p-
7cc10 3e 4d 20 3d 20 4d 3b 0a 20 20 70 2d 3e 44 20 3d  >M = M;.  p->D =
7cc20 20 44 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c   D;.  if( p->val
7cc30 69 64 54 5a 20 29 7b 0a 20 20 20 20 63 6f 6d 70  idTZ ){.    comp
7cc40 75 74 65 4a 44 28 70 29 3b 0a 20 20 7d 0a 20 20  uteJD(p);.  }.  
7cc50 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
7cc60 2a 2a 20 53 65 74 20 74 68 65 20 74 69 6d 65 20  ** Set the time 
7cc70 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  to the current t
7cc80 69 6d 65 20 72 65 70 6f 72 74 65 64 20 62 79 20  ime reported by 
7cc90 74 68 65 20 56 46 53 0a 2a 2f 0a 73 74 61 74 69  the VFS.*/.stati
7cca0 63 20 76 6f 69 64 20 73 65 74 44 61 74 65 54 69  c void setDateTi
7ccb0 6d 65 54 6f 43 75 72 72 65 6e 74 28 73 71 6c 69  meToCurrent(sqli
7ccc0 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
7ccd0 74 65 78 74 2c 20 44 61 74 65 54 69 6d 65 20 2a  text, DateTime *
7cce0 70 29 7b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 0a  p){.  double r;.
7ccf0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
7cd00 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
7cd10 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78  db_handle(contex
7cd20 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 73 43  t);.  sqlite3OsC
7cd30 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70  urrentTime(db->p
7cd40 56 66 73 2c 20 26 72 29 3b 0a 20 20 70 2d 3e 69  Vfs, &r);.  p->i
7cd50 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e  JD = (sqlite3_in
7cd60 74 36 34 29 28 72 2a 38 36 34 30 30 30 30 30 2e  t64)(r*86400000.
7cd70 30 20 2b 20 30 2e 35 29 3b 0a 20 20 70 2d 3e 76  0 + 0.5);.  p->v
7cd80 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 7d 0a 0a 2f  alidJD = 1;.}../
7cd90 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20  *.** Attempt to 
7cda0 70 61 72 73 65 20 74 68 65 20 67 69 76 65 6e 20  parse the given 
7cdb0 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 4a 75  string into a Ju
7cdc0 6c 69 61 6e 20 44 61 79 20 4e 75 6d 62 65 72 2e  lian Day Number.
7cdd0 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20    Return.** the 
7cde0 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  number of errors
7cdf0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ..**.** The foll
7ce00 6f 77 69 6e 67 20 61 72 65 20 61 63 63 65 70 74  owing are accept
7ce10 61 62 6c 65 20 66 6f 72 6d 73 20 66 6f 72 20 74  able forms for t
7ce20 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 3a  he input string:
7ce30 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 59 59 59 59  .**.**      YYYY
7ce40 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 2e  -MM-DD HH:MM:SS.
7ce50 46 46 46 20 20 2b 2f 2d 48 48 3a 4d 4d 0a 2a 2a  FFF  +/-HH:MM.**
7ce60 20 20 20 20 20 20 44 44 44 44 2e 44 44 20 0a 2a        DDDD.DD .*
7ce70 2a 20 20 20 20 20 20 6e 6f 77 0a 2a 2a 0a 2a 2a  *      now.**.**
7ce80 20 49 6e 20 74 68 65 20 66 69 72 73 74 20 66 6f   In the first fo
7ce90 72 6d 2c 20 74 68 65 20 2b 2f 2d 48 48 3a 4d 4d  rm, the +/-HH:MM
7cea0 20 69 73 20 61 6c 77 61 79 73 20 6f 70 74 69 6f   is always optio
7ceb0 6e 61 6c 2e 20 20 54 68 65 20 66 72 61 63 74 69  nal.  The fracti
7cec0 6f 6e 61 6c 0a 2a 2a 20 73 65 63 6f 6e 64 73 20  onal.** seconds 
7ced0 65 78 74 65 6e 73 69 6f 6e 20 28 74 68 65 20 22  extension (the "
7cee0 2e 46 46 46 22 29 20 69 73 20 6f 70 74 69 6f 6e  .FFF") is option
7cef0 61 6c 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 73  al.  The seconds
7cf00 20 70 6f 72 74 69 6f 6e 0a 2a 2a 20 28 22 3a 53   portion.** (":S
7cf10 53 2e 46 46 46 22 29 20 69 73 20 6f 70 74 69 6f  S.FFF") is optio
7cf20 6e 2e 20 20 54 68 65 20 79 65 61 72 20 61 6e 64  n.  The year and
7cf30 20 64 61 74 65 20 63 61 6e 20 62 65 20 6f 6d 69   date can be omi
7cf40 74 74 65 64 20 61 73 20 6c 6f 6e 67 0a 2a 2a 20  tted as long.** 
7cf50 61 73 20 74 68 65 72 65 20 69 73 20 61 20 74 69  as there is a ti
7cf60 6d 65 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20  me string.  The 
7cf70 74 69 6d 65 20 73 74 72 69 6e 67 20 63 61 6e 20  time string can 
7cf80 62 65 20 6f 6d 69 74 74 65 64 20 61 73 20 6c 6f  be omitted as lo
7cf90 6e 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 20 69  ng.** as there i
7cfa0 73 20 61 20 79 65 61 72 20 61 6e 64 20 64 61 74  s a year and dat
7cfb0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
7cfc0 20 70 61 72 73 65 44 61 74 65 4f 72 54 69 6d 65   parseDateOrTime
7cfd0 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
7cfe0 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20  ext *context, . 
7cff0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61   const char *zDa
7d000 74 65 2c 20 0a 20 20 44 61 74 65 54 69 6d 65 20  te, .  DateTime 
7d010 2a 70 0a 29 7b 0a 20 20 69 66 28 20 70 61 72 73  *p.){.  if( pars
7d020 65 59 79 79 79 4d 6d 44 64 28 7a 44 61 74 65 2c  eYyyyMmDd(zDate,
7d030 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  p)==0 ){.    ret
7d040 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69  urn 0;.  }else i
7d050 66 28 20 70 61 72 73 65 48 68 4d 6d 53 73 28 7a  f( parseHhMmSs(z
7d060 44 61 74 65 2c 20 70 29 3d 3d 30 20 29 7b 0a 20  Date, p)==0 ){. 
7d070 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
7d080 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33  else if( sqlite3
7d090 53 74 72 49 43 6d 70 28 7a 44 61 74 65 2c 22 6e  StrICmp(zDate,"n
7d0a0 6f 77 22 29 3d 3d 30 29 7b 0a 20 20 20 20 73 65  ow")==0){.    se
7d0b0 74 44 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65  tDateTimeToCurre
7d0c0 6e 74 28 63 6f 6e 74 65 78 74 2c 20 70 29 3b 0a  nt(context, p);.
7d0d0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
7d0e0 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65  }else if( sqlite
7d0f0 33 49 73 4e 75 6d 62 65 72 28 7a 44 61 74 65 2c  3IsNumber(zDate,
7d100 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29   0, SQLITE_UTF8)
7d110 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 72   ){.    double r
7d120 3b 0a 20 20 20 20 67 65 74 56 61 6c 75 65 28 7a  ;.    getValue(z
7d130 44 61 74 65 2c 20 26 72 29 3b 0a 20 20 20 20 70  Date, &r);.    p
7d140 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33  ->iJD = (sqlite3
7d150 5f 69 6e 74 36 34 29 28 72 2a 38 36 34 30 30 30  _int64)(r*864000
7d160 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 20  00.0 + 0.5);.   
7d170 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b   p->validJD = 1;
7d180 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
7d190 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d   }.  return 1;.}
7d1a0 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20  ../*.** Compute 
7d1b0 74 68 65 20 59 65 61 72 2c 20 4d 6f 6e 74 68 2c  the Year, Month,
7d1c0 20 61 6e 64 20 44 61 79 20 66 72 6f 6d 20 74 68   and Day from th
7d1d0 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d  e julian day num
7d1e0 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ber..*/.static v
7d1f0 6f 69 64 20 63 6f 6d 70 75 74 65 59 4d 44 28 44  oid computeYMD(D
7d200 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69  ateTime *p){.  i
7d210 6e 74 20 5a 2c 20 41 2c 20 42 2c 20 43 2c 20 44  nt Z, A, B, C, D
7d220 2c 20 45 2c 20 58 31 3b 0a 20 20 69 66 28 20 70  , E, X1;.  if( p
7d230 2d 3e 76 61 6c 69 64 59 4d 44 20 29 20 72 65 74  ->validYMD ) ret
7d240 75 72 6e 3b 0a 20 20 69 66 28 20 21 70 2d 3e 76  urn;.  if( !p->v
7d250 61 6c 69 64 4a 44 20 29 7b 0a 20 20 20 20 70 2d  alidJD ){.    p-
7d260 3e 59 20 3d 20 32 30 30 30 3b 0a 20 20 20 20 70  >Y = 2000;.    p
7d270 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 20 70 2d 3e  ->M = 1;.    p->
7d280 44 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  D = 1;.  }else{.
7d290 20 20 20 20 5a 20 3d 20 28 70 2d 3e 69 4a 44 20      Z = (p->iJD 
7d2a0 2b 20 34 33 32 30 30 30 30 30 29 2f 38 36 34 30  + 43200000)/8640
7d2b0 30 30 30 30 3b 0a 20 20 20 20 41 20 3d 20 28 5a  0000;.    A = (Z
7d2c0 20 2d 20 31 38 36 37 32 31 36 2e 32 35 29 2f 33   - 1867216.25)/3
7d2d0 36 35 32 34 2e 32 35 3b 0a 20 20 20 20 41 20 3d  6524.25;.    A =
7d2e0 20 5a 20 2b 20 31 20 2b 20 41 20 2d 20 28 41 2f   Z + 1 + A - (A/
7d2f0 34 29 3b 0a 20 20 20 20 42 20 3d 20 41 20 2b 20  4);.    B = A + 
7d300 31 35 32 34 3b 0a 20 20 20 20 43 20 3d 20 28 42  1524;.    C = (B
7d310 20 2d 20 31 32 32 2e 31 29 2f 33 36 35 2e 32 35   - 122.1)/365.25
7d320 3b 0a 20 20 20 20 44 20 3d 20 33 36 35 2e 32 35  ;.    D = 365.25
7d330 2a 43 3b 0a 20 20 20 20 45 20 3d 20 28 42 2d 44  *C;.    E = (B-D
7d340 29 2f 33 30 2e 36 30 30 31 3b 0a 20 20 20 20 58  )/30.6001;.    X
7d350 31 20 3d 20 33 30 2e 36 30 30 31 2a 45 3b 0a 20  1 = 30.6001*E;. 
7d360 20 20 20 70 2d 3e 44 20 3d 20 42 20 2d 20 44 20     p->D = B - D 
7d370 2d 20 58 31 3b 0a 20 20 20 20 70 2d 3e 4d 20 3d  - X1;.    p->M =
7d380 20 45 3c 31 34 20 3f 20 45 2d 31 20 3a 20 45 2d   E<14 ? E-1 : E-
7d390 31 33 3b 0a 20 20 20 20 70 2d 3e 59 20 3d 20 70  13;.    p->Y = p
7d3a0 2d 3e 4d 3e 32 20 3f 20 43 20 2d 20 34 37 31 36  ->M>2 ? C - 4716
7d3b0 20 3a 20 43 20 2d 20 34 37 31 35 3b 0a 20 20 7d   : C - 4715;.  }
7d3c0 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d  .  p->validYMD =
7d3d0 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d   1;.}../*.** Com
7d3e0 70 75 74 65 20 74 68 65 20 48 6f 75 72 2c 20 4d  pute the Hour, M
7d3f0 69 6e 75 74 65 2c 20 61 6e 64 20 53 65 63 6f 6e  inute, and Secon
7d400 64 73 20 66 72 6f 6d 20 74 68 65 20 6a 75 6c 69  ds from the juli
7d410 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 2e 0a 2a  an day number..*
7d420 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f  /.static void co
7d430 6d 70 75 74 65 48 4d 53 28 44 61 74 65 54 69 6d  mputeHMS(DateTim
7d440 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 73 3b 0a  e *p){.  int s;.
7d450 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 48 4d    if( p->validHM
7d460 53 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 63 6f  S ) return;.  co
7d470 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 73 20  mputeJD(p);.  s 
7d480 3d 20 28 70 2d 3e 69 4a 44 20 2b 20 34 33 32 30  = (p->iJD + 4320
7d490 30 30 30 30 29 20 25 20 38 36 34 30 30 30 30 30  0000) % 86400000
7d4a0 3b 0a 20 20 70 2d 3e 73 20 3d 20 73 2f 31 30 30  ;.  p->s = s/100
7d4b0 30 2e 30 3b 0a 20 20 73 20 3d 20 70 2d 3e 73 3b  0.0;.  s = p->s;
7d4c0 0a 20 20 70 2d 3e 73 20 2d 3d 20 73 3b 0a 20 20  .  p->s -= s;.  
7d4d0 70 2d 3e 68 20 3d 20 73 2f 33 36 30 30 3b 0a 20  p->h = s/3600;. 
7d4e0 20 73 20 2d 3d 20 70 2d 3e 68 2a 33 36 30 30 3b   s -= p->h*3600;
7d4f0 0a 20 20 70 2d 3e 6d 20 3d 20 73 2f 36 30 3b 0a  .  p->m = s/60;.
7d500 20 20 70 2d 3e 73 20 2b 3d 20 73 20 2d 20 70 2d    p->s += s - p-
7d510 3e 6d 2a 36 30 3b 0a 20 20 70 2d 3e 76 61 6c 69  >m*60;.  p->vali
7d520 64 48 4d 53 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a  dHMS = 1;.}../*.
7d530 2a 2a 20 43 6f 6d 70 75 74 65 20 62 6f 74 68 20  ** Compute both 
7d540 59 4d 44 20 61 6e 64 20 48 4d 53 0a 2a 2f 0a 73  YMD and HMS.*/.s
7d550 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75  tatic void compu
7d560 74 65 59 4d 44 5f 48 4d 53 28 44 61 74 65 54 69  teYMD_HMS(DateTi
7d570 6d 65 20 2a 70 29 7b 0a 20 20 63 6f 6d 70 75 74  me *p){.  comput
7d580 65 59 4d 44 28 70 29 3b 0a 20 20 63 6f 6d 70 75  eYMD(p);.  compu
7d590 74 65 48 4d 53 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a  teHMS(p);.}../*.
7d5a0 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 59 4d 44  ** Clear the YMD
7d5b0 20 61 6e 64 20 48 4d 53 20 61 6e 64 20 74 68 65   and HMS and the
7d5c0 20 54 5a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f   TZ.*/.static vo
7d5d0 69 64 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f  id clearYMD_HMS_
7d5e0 54 5a 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b  TZ(DateTime *p){
7d5f0 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d  .  p->validYMD =
7d600 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 48 4d   0;.  p->validHM
7d610 53 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69  S = 0;.  p->vali
7d620 64 54 5a 20 3d 20 30 3b 0a 7d 0a 0a 23 69 66 6e  dTZ = 0;.}..#ifn
7d630 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
7d640 4c 4f 43 41 4c 54 49 4d 45 0a 2f 2a 0a 2a 2a 20  LOCALTIME./*.** 
7d650 43 6f 6d 70 75 74 65 20 74 68 65 20 64 69 66 66  Compute the diff
7d660 65 72 65 6e 63 65 20 28 69 6e 20 6d 69 6c 6c 69  erence (in milli
7d670 73 65 63 6f 6e 64 73 29 0a 2a 2a 20 62 65 74 77  seconds).** betw
7d680 65 65 6e 20 6c 6f 63 61 6c 74 69 6d 65 20 61 6e  een localtime an
7d690 64 20 55 54 43 20 28 61 2e 6b 2e 61 2e 20 47 4d  d UTC (a.k.a. GM
7d6a0 54 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74 69  T).** for the ti
7d6b0 6d 65 20 76 61 6c 75 65 20 70 20 77 68 65 72 65  me value p where
7d6c0 20 70 20 69 73 20 69 6e 20 55 54 43 2e 0a 2a 2f   p is in UTC..*/
7d6d0 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 61  .static int loca
7d6e0 6c 74 69 6d 65 4f 66 66 73 65 74 28 44 61 74 65  ltimeOffset(Date
7d6f0 54 69 6d 65 20 2a 70 29 7b 0a 20 20 44 61 74 65  Time *p){.  Date
7d700 54 69 6d 65 20 78 2c 20 79 3b 0a 20 20 74 69 6d  Time x, y;.  tim
7d710 65 5f 74 20 74 3b 0a 20 20 78 20 3d 20 2a 70 3b  e_t t;.  x = *p;
7d720 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d  .  computeYMD_HM
7d730 53 28 26 78 29 3b 0a 20 20 69 66 28 20 78 2e 59  S(&x);.  if( x.Y
7d740 3c 31 39 37 31 20 7c 7c 20 78 2e 59 3e 3d 32 30  <1971 || x.Y>=20
7d750 33 38 20 29 7b 0a 20 20 20 20 78 2e 59 20 3d 20  38 ){.    x.Y = 
7d760 32 30 30 30 3b 0a 20 20 20 20 78 2e 4d 20 3d 20  2000;.    x.M = 
7d770 31 3b 0a 20 20 20 20 78 2e 44 20 3d 20 31 3b 0a  1;.    x.D = 1;.
7d780 20 20 20 20 78 2e 68 20 3d 20 30 3b 0a 20 20 20      x.h = 0;.   
7d790 20 78 2e 6d 20 3d 20 30 3b 0a 20 20 20 20 78 2e   x.m = 0;.    x.
7d7a0 73 20 3d 20 30 2e 30 3b 0a 20 20 7d 20 65 6c 73  s = 0.0;.  } els
7d7b0 65 20 7b 0a 20 20 20 20 69 6e 74 20 73 20 3d 20  e {.    int s = 
7d7c0 78 2e 73 20 2b 20 30 2e 35 3b 0a 20 20 20 20 78  x.s + 0.5;.    x
7d7d0 2e 73 20 3d 20 73 3b 0a 20 20 7d 0a 20 20 78 2e  .s = s;.  }.  x.
7d7e0 74 7a 20 3d 20 30 3b 0a 20 20 78 2e 76 61 6c 69  tz = 0;.  x.vali
7d7f0 64 4a 44 20 3d 20 30 3b 0a 20 20 63 6f 6d 70 75  dJD = 0;.  compu
7d800 74 65 4a 44 28 26 78 29 3b 0a 20 20 74 20 3d 20  teJD(&x);.  t = 
7d810 78 2e 69 4a 44 2f 31 30 30 30 20 2d 20 32 34 34  x.iJD/1000 - 244
7d820 30 35 38 37 2e 35 2a 38 36 34 30 30 2e 30 3b 0a  0587.5*86400.0;.
7d830 23 69 66 64 65 66 20 48 41 56 45 5f 4c 4f 43 41  #ifdef HAVE_LOCA
7d840 4c 54 49 4d 45 5f 52 0a 20 20 7b 0a 20 20 20 20  LTIME_R.  {.    
7d850 73 74 72 75 63 74 20 74 6d 20 73 4c 6f 63 61 6c  struct tm sLocal
7d860 3b 0a 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65 5f  ;.    localtime_
7d870 72 28 26 74 2c 20 26 73 4c 6f 63 61 6c 29 3b 0a  r(&t, &sLocal);.
7d880 20 20 20 20 79 2e 59 20 3d 20 73 4c 6f 63 61 6c      y.Y = sLocal
7d890 2e 74 6d 5f 79 65 61 72 20 2b 20 31 39 30 30 3b  .tm_year + 1900;
7d8a0 0a 20 20 20 20 79 2e 4d 20 3d 20 73 4c 6f 63 61  .    y.M = sLoca
7d8b0 6c 2e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20  l.tm_mon + 1;.  
7d8c0 20 20 79 2e 44 20 3d 20 73 4c 6f 63 61 6c 2e 74    y.D = sLocal.t
7d8d0 6d 5f 6d 64 61 79 3b 0a 20 20 20 20 79 2e 68 20  m_mday;.    y.h 
7d8e0 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72  = sLocal.tm_hour
7d8f0 3b 0a 20 20 20 20 79 2e 6d 20 3d 20 73 4c 6f 63  ;.    y.m = sLoc
7d900 61 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79  al.tm_min;.    y
7d910 2e 73 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73  .s = sLocal.tm_s
7d920 65 63 3b 0a 20 20 7d 0a 23 65 6c 69 66 20 64 65  ec;.  }.#elif de
7d930 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c  fined(HAVE_LOCAL
7d940 54 49 4d 45 5f 53 29 0a 20 20 7b 0a 20 20 20 20  TIME_S).  {.    
7d950 73 74 72 75 63 74 20 74 6d 20 73 4c 6f 63 61 6c  struct tm sLocal
7d960 3b 0a 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65 5f  ;.    localtime_
7d970 73 28 26 73 4c 6f 63 61 6c 2c 20 26 74 29 3b 0a  s(&sLocal, &t);.
7d980 20 20 20 20 79 2e 59 20 3d 20 73 4c 6f 63 61 6c      y.Y = sLocal
7d990 2e 74 6d 5f 79 65 61 72 20 2b 20 31 39 30 30 3b  .tm_year + 1900;
7d9a0 0a 20 20 20 20 79 2e 4d 20 3d 20 73 4c 6f 63 61  .    y.M = sLoca
7d9b0 6c 2e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20  l.tm_mon + 1;.  
7d9c0 20 20 79 2e 44 20 3d 20 73 4c 6f 63 61 6c 2e 74    y.D = sLocal.t
7d9d0 6d 5f 6d 64 61 79 3b 0a 20 20 20 20 79 2e 68 20  m_mday;.    y.h 
7d9e0 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72  = sLocal.tm_hour
7d9f0 3b 0a 20 20 20 20 79 2e 6d 20 3d 20 73 4c 6f 63  ;.    y.m = sLoc
7da00 61 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79  al.tm_min;.    y
7da10 2e 73 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73  .s = sLocal.tm_s
7da20 65 63 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20  ec;.  }.#else.  
7da30 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74 6d 20  {.    struct tm 
7da40 2a 70 54 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65  *pTm;.    sqlite
7da50 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71  3_mutex_enter(sq
7da60 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
7da70 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
7da80 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20  TIC_MASTER));.  
7da90 20 20 70 54 6d 20 3d 20 6c 6f 63 61 6c 74 69 6d    pTm = localtim
7daa0 65 28 26 74 29 3b 0a 20 20 20 20 79 2e 59 20 3d  e(&t);.    y.Y =
7dab0 20 70 54 6d 2d 3e 74 6d 5f 79 65 61 72 20 2b 20   pTm->tm_year + 
7dac0 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d 20 3d 20  1900;.    y.M = 
7dad0 70 54 6d 2d 3e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b  pTm->tm_mon + 1;
7dae0 0a 20 20 20 20 79 2e 44 20 3d 20 70 54 6d 2d 3e  .    y.D = pTm->
7daf0 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 20 79 2e 68  tm_mday;.    y.h
7db00 20 3d 20 70 54 6d 2d 3e 74 6d 5f 68 6f 75 72 3b   = pTm->tm_hour;
7db10 0a 20 20 20 20 79 2e 6d 20 3d 20 70 54 6d 2d 3e  .    y.m = pTm->
7db20 74 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e 73 20  tm_min;.    y.s 
7db30 3d 20 70 54 6d 2d 3e 74 6d 5f 73 65 63 3b 0a 20  = pTm->tm_sec;. 
7db40 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
7db50 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75  _leave(sqlite3Mu
7db60 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
7db70 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
7db80 54 45 52 29 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  TER));.  }.#endi
7db90 66 0a 20 20 79 2e 76 61 6c 69 64 59 4d 44 20 3d  f.  y.validYMD =
7dba0 20 31 3b 0a 20 20 79 2e 76 61 6c 69 64 48 4d 53   1;.  y.validHMS
7dbb0 20 3d 20 31 3b 0a 20 20 79 2e 76 61 6c 69 64 4a   = 1;.  y.validJ
7dbc0 44 20 3d 20 30 3b 0a 20 20 79 2e 76 61 6c 69 64  D = 0;.  y.valid
7dbd0 54 5a 20 3d 20 30 3b 0a 20 20 63 6f 6d 70 75 74  TZ = 0;.  comput
7dbe0 65 4a 44 28 26 79 29 3b 0a 20 20 72 65 74 75 72  eJD(&y);.  retur
7dbf0 6e 20 79 2e 69 4a 44 20 2d 20 78 2e 69 4a 44 3b  n y.iJD - x.iJD;
7dc00 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
7dc10 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49  ITE_OMIT_LOCALTI
7dc20 4d 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f  ME */../*.** Pro
7dc30 63 65 73 73 20 61 20 6d 6f 64 69 66 69 65 72 20  cess a modifier 
7dc40 74 6f 20 61 20 64 61 74 65 2d 74 69 6d 65 20 73  to a date-time s
7dc50 74 61 6d 70 2e 20 20 54 68 65 20 6d 6f 64 69 66  tamp.  The modif
7dc60 69 65 72 73 20 61 72 65 0a 2a 2a 20 61 73 20 66  iers are.** as f
7dc70 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ollows:.**.**   
7dc80 20 20 4e 4e 4e 20 64 61 79 73 0a 2a 2a 20 20 20    NNN days.**   
7dc90 20 20 4e 4e 4e 20 68 6f 75 72 73 0a 2a 2a 20 20    NNN hours.**  
7dca0 20 20 20 4e 4e 4e 20 6d 69 6e 75 74 65 73 0a 2a     NNN minutes.*
7dcb0 2a 20 20 20 20 20 4e 4e 4e 2e 4e 4e 4e 4e 20 73  *     NNN.NNNN s
7dcc0 65 63 6f 6e 64 73 0a 2a 2a 20 20 20 20 20 4e 4e  econds.**     NN
7dcd0 4e 20 6d 6f 6e 74 68 73 0a 2a 2a 20 20 20 20 20  N months.**     
7dce0 4e 4e 4e 20 79 65 61 72 73 0a 2a 2a 20 20 20 20  NNN years.**    
7dcf0 20 73 74 61 72 74 20 6f 66 20 6d 6f 6e 74 68 0a   start of month.
7dd00 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66 20  **     start of 
7dd10 79 65 61 72 0a 2a 2a 20 20 20 20 20 73 74 61 72  year.**     star
7dd20 74 20 6f 66 20 77 65 65 6b 0a 2a 2a 20 20 20 20  t of week.**    
7dd30 20 73 74 61 72 74 20 6f 66 20 64 61 79 0a 2a 2a   start of day.**
7dd40 20 20 20 20 20 77 65 65 6b 64 61 79 20 4e 0a 2a       weekday N.*
7dd50 2a 20 20 20 20 20 75 6e 69 78 65 70 6f 63 68 0a  *     unixepoch.
7dd60 2a 2a 20 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65  **     localtime
7dd70 0a 2a 2a 20 20 20 20 20 75 74 63 0a 2a 2a 0a 2a  .**     utc.**.*
7dd80 2a 20 52 65 74 75 72 6e 20 30 20 6f 6e 20 73 75  * Return 0 on su
7dd90 63 63 65 73 73 20 61 6e 64 20 31 20 69 66 20 74  ccess and 1 if t
7dda0 68 65 72 65 20 69 73 20 61 6e 79 20 6b 69 6e 64  here is any kind
7ddb0 20 6f 66 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74   of error..*/.st
7ddc0 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 4d 6f  atic int parseMo
7ddd0 64 69 66 69 65 72 28 63 6f 6e 73 74 20 63 68 61  difier(const cha
7dde0 72 20 2a 7a 4d 6f 64 2c 20 44 61 74 65 54 69 6d  r *zMod, DateTim
7ddf0 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20  e *p){.  int rc 
7de00 3d 20 31 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20  = 1;.  int n;.  
7de10 64 6f 75 62 6c 65 20 72 3b 0a 20 20 63 68 61 72  double r;.  char
7de20 20 2a 7a 2c 20 7a 42 75 66 5b 33 30 5d 3b 0a 20   *z, zBuf[30];. 
7de30 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 66 6f 72   z = zBuf;.  for
7de40 28 6e 3d 30 3b 20 6e 3c 73 69 7a 65 6f 66 28 7a  (n=0; n<sizeof(z
7de50 42 75 66 29 2d 31 20 26 26 20 7a 4d 6f 64 5b 6e  Buf)-1 && zMod[n
7de60 5d 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 7a 5b 6e  ]; n++){.    z[n
7de70 5d 20 3d 20 74 6f 6c 6f 77 65 72 28 7a 4d 6f 64  ] = tolower(zMod
7de80 5b 6e 5d 29 3b 0a 20 20 7d 0a 20 20 7a 5b 6e 5d  [n]);.  }.  z[n]
7de90 20 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 20   = 0;.  switch( 
7dea0 7a 5b 30 5d 20 29 7b 0a 23 69 66 6e 64 65 66 20  z[0] ){.#ifndef 
7deb0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41  SQLITE_OMIT_LOCA
7dec0 4c 54 49 4d 45 0a 20 20 20 20 63 61 73 65 20 27  LTIME.    case '
7ded0 6c 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 20  l': {.      /*  
7dee0 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 20 20 20 20    localtime.    
7def0 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41 73    **.      ** As
7df00 73 75 6d 69 6e 67 20 74 68 65 20 63 75 72 72 65  suming the curre
7df10 6e 74 20 74 69 6d 65 20 76 61 6c 75 65 20 69 73  nt time value is
7df20 20 55 54 43 20 28 61 2e 6b 2e 61 2e 20 47 4d 54   UTC (a.k.a. GMT
7df30 29 2c 20 73 68 69 66 74 20 69 74 20 74 6f 0a 20  ), shift it to. 
7df40 20 20 20 20 20 2a 2a 20 73 68 6f 77 20 6c 6f 63       ** show loc
7df50 61 6c 20 74 69 6d 65 2e 0a 20 20 20 20 20 20 2a  al time..      *
7df60 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 63  /.      if( strc
7df70 6d 70 28 7a 2c 20 22 6c 6f 63 61 6c 74 69 6d 65  mp(z, "localtime
7df80 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ")==0 ){.       
7df90 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20   computeJD(p);. 
7dfa0 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d         p->iJD +=
7dfb0 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74   localtimeOffset
7dfc0 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63 6c 65  (p);.        cle
7dfd0 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b  arYMD_HMS_TZ(p);
7dfe0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b  .        rc = 0;
7dff0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
7e000 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  reak;.    }.#end
7e010 69 66 0a 20 20 20 20 63 61 73 65 20 27 75 27 3a  if.    case 'u':
7e020 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 20   {.      /*.    
7e030 20 20 2a 2a 20 20 20 20 75 6e 69 78 65 70 6f 63    **    unixepoc
7e040 68 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  h.      **.     
7e050 20 2a 2a 20 54 72 65 61 74 20 74 68 65 20 63 75   ** Treat the cu
7e060 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 70  rrent value of p
7e070 2d 3e 69 4a 44 20 61 73 20 74 68 65 20 6e 75 6d  ->iJD as the num
7e080 62 65 72 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20  ber of.      ** 
7e090 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 39  seconds since 19
7e0a0 37 30 2e 20 20 43 6f 6e 76 65 72 74 20 74 6f 20  70.  Convert to 
7e0b0 61 20 72 65 61 6c 20 6a 75 6c 69 61 6e 20 64 61  a real julian da
7e0c0 79 20 6e 75 6d 62 65 72 2e 0a 20 20 20 20 20 20  y number..      
7e0d0 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74 72  */.      if( str
7e0e0 63 6d 70 28 7a 2c 20 22 75 6e 69 78 65 70 6f 63  cmp(z, "unixepoc
7e0f0 68 22 29 3d 3d 30 20 26 26 20 70 2d 3e 76 61 6c  h")==0 && p->val
7e100 69 64 4a 44 20 29 7b 0a 20 20 20 20 20 20 20 20  idJD ){.        
7e110 70 2d 3e 69 4a 44 20 3d 20 70 2d 3e 69 4a 44 2f  p->iJD = p->iJD/
7e120 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35 38  86400.0 + 244058
7e130 37 2e 35 2a 38 36 34 30 30 30 30 30 2e 30 3b 0a  7.5*86400000.0;.
7e140 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44          clearYMD
7e150 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20  _HMS_TZ(p);.    
7e160 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20      rc = 0;.    
7e170 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
7e180 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d  TE_OMIT_LOCALTIM
7e190 45 0a 20 20 20 20 20 20 65 6c 73 65 20 69 66 28  E.      else if(
7e1a0 20 73 74 72 63 6d 70 28 7a 2c 20 22 75 74 63 22   strcmp(z, "utc"
7e1b0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
7e1c0 69 6e 74 20 63 31 3b 0a 20 20 20 20 20 20 20 20  int c1;.        
7e1d0 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20  computeJD(p);.  
7e1e0 20 20 20 20 20 20 63 31 20 3d 20 6c 6f 63 61 6c        c1 = local
7e1f0 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b 0a 20  timeOffset(p);. 
7e200 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d 3d         p->iJD -=
7e210 20 63 31 3b 0a 20 20 20 20 20 20 20 20 63 6c 65   c1;.        cle
7e220 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b  arYMD_HMS_TZ(p);
7e230 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20  .        p->iJD 
7e240 2b 3d 20 63 31 20 2d 20 6c 6f 63 61 6c 74 69 6d  += c1 - localtim
7e250 65 4f 66 66 73 65 74 28 70 29 3b 0a 20 20 20 20  eOffset(p);.    
7e260 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20      rc = 0;.    
7e270 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20    }.#endif.     
7e280 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
7e290 20 20 63 61 73 65 20 27 77 27 3a 20 7b 0a 20 20    case 'w': {.  
7e2a0 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a 20      /*.      ** 
7e2b0 20 20 20 77 65 65 6b 64 61 79 20 4e 0a 20 20 20     weekday N.   
7e2c0 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4d     **.      ** M
7e2d0 6f 76 65 20 74 68 65 20 64 61 74 65 20 74 6f 20  ove the date to 
7e2e0 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 6f 6e  the same time on
7e2f0 20 74 68 65 20 6e 65 78 74 20 6f 63 63 75 72 72   the next occurr
7e300 65 6e 63 65 20 6f 66 0a 20 20 20 20 20 20 2a 2a  ence of.      **
7e310 20 77 65 65 6b 64 61 79 20 4e 20 77 68 65 72 65   weekday N where
7e320 20 30 3d 3d 53 75 6e 64 61 79 2c 20 31 3d 3d 4d   0==Sunday, 1==M
7e330 6f 6e 64 61 79 2c 20 61 6e 64 20 73 6f 20 66 6f  onday, and so fo
7e340 72 74 68 2e 20 20 49 66 20 74 68 65 0a 20 20 20  rth.  If the.   
7e350 20 20 20 2a 2a 20 64 61 74 65 20 69 73 20 61 6c     ** date is al
7e360 72 65 61 64 79 20 6f 6e 20 74 68 65 20 61 70 70  ready on the app
7e370 72 6f 70 72 69 61 74 65 20 77 65 65 6b 64 61 79  ropriate weekday
7e380 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f  , this is a no-o
7e390 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  p..      */.    
7e3a0 20 20 69 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c    if( strncmp(z,
7e3b0 20 22 77 65 65 6b 64 61 79 20 22 2c 20 38 29 3d   "weekday ", 8)=
7e3c0 3d 30 20 26 26 20 67 65 74 56 61 6c 75 65 28 26  =0 && getValue(&
7e3d0 7a 5b 38 5d 2c 26 72 29 3e 30 0a 20 20 20 20 20  z[8],&r)>0.     
7e3e0 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28              && (
7e3f0 6e 3d 72 29 3d 3d 72 20 26 26 20 6e 3e 3d 30 20  n=r)==r && n>=0 
7e400 26 26 20 72 3c 37 20 29 7b 0a 20 20 20 20 20 20  && r<7 ){.      
7e410 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
7e420 5a 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75  Z;.        compu
7e430 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 20  teYMD_HMS(p);.  
7e440 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a        p->validTZ
7e450 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70 2d   = 0;.        p-
7e460 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20  >validJD = 0;.  
7e470 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28        computeJD(
7e480 70 29 3b 0a 20 20 20 20 20 20 20 20 5a 20 3d 20  p);.        Z = 
7e490 28 28 70 2d 3e 69 4a 44 20 2b 20 31 32 39 36 30  ((p->iJD + 12960
7e4a0 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 20  0000)/86400000) 
7e4b0 25 20 37 3b 0a 20 20 20 20 20 20 20 20 69 66 28  % 7;.        if(
7e4c0 20 5a 3e 6e 20 29 20 5a 20 2d 3d 20 37 3b 0a 20   Z>n ) Z -= 7;. 
7e4d0 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d         p->iJD +=
7e4e0 20 28 6e 20 2d 20 5a 29 2a 38 36 34 30 30 30 30   (n - Z)*8640000
7e4f0 30 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72  0;.        clear
7e500 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20  YMD_HMS_TZ(p);. 
7e510 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20         rc = 0;. 
7e520 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
7e530 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
7e540 73 65 20 27 73 27 3a 20 7b 0a 20 20 20 20 20 20  se 's': {.      
7e550 2f 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 73  /*.      **    s
7e560 74 61 72 74 20 6f 66 20 54 54 54 54 54 0a 20 20  tart of TTTTT.  
7e570 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
7e580 4d 6f 76 65 20 74 68 65 20 64 61 74 65 20 62 61  Move the date ba
7e590 63 6b 77 61 72 64 73 20 74 6f 20 74 68 65 20 62  ckwards to the b
7e5a0 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20  eginning of the 
7e5b0 63 75 72 72 65 6e 74 20 64 61 79 2c 0a 20 20 20  current day,.   
7e5c0 20 20 20 2a 2a 20 6f 72 20 6d 6f 6e 74 68 20 6f     ** or month o
7e5d0 72 20 79 65 61 72 2e 0a 20 20 20 20 20 20 2a 2f  r year..      */
7e5e0 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 6e 63  .      if( strnc
7e5f0 6d 70 28 7a 2c 20 22 73 74 61 72 74 20 6f 66 20  mp(z, "start of 
7e600 22 2c 20 39 29 21 3d 30 20 29 20 62 72 65 61 6b  ", 9)!=0 ) break
7e610 3b 0a 20 20 20 20 20 20 7a 20 2b 3d 20 39 3b 0a  ;.      z += 9;.
7e620 20 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44        computeYMD
7e630 28 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61  (p);.      p->va
7e640 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 20 20 20  lidHMS = 1;.    
7e650 20 20 70 2d 3e 68 20 3d 20 70 2d 3e 6d 20 3d 20    p->h = p->m = 
7e660 30 3b 0a 20 20 20 20 20 20 70 2d 3e 73 20 3d 20  0;.      p->s = 
7e670 30 2e 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61  0.0;.      p->va
7e680 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20 20  lidTZ = 0;.     
7e690 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b   p->validJD = 0;
7e6a0 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 63 6d  .      if( strcm
7e6b0 70 28 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20  p(z,"month")==0 
7e6c0 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 44 20  ){.        p->D 
7e6d0 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72 63 20  = 1;.        rc 
7e6e0 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = 0;.      }else
7e6f0 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 79   if( strcmp(z,"y
7e700 65 61 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ear")==0 ){.    
7e710 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70      computeYMD(p
7e720 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d 20  );.        p->M 
7e730 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  = 1;.        p->
7e740 44 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72  D = 1;.        r
7e750 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c  c = 0;.      }el
7e760 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c  se if( strcmp(z,
7e770 22 64 61 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20  "day")==0 ){.   
7e780 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20       rc = 0;.   
7e790 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
7e7a0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
7e7b0 20 27 2b 27 3a 0a 20 20 20 20 63 61 73 65 20 27   '+':.    case '
7e7c0 2d 27 3a 0a 20 20 20 20 63 61 73 65 20 27 30 27  -':.    case '0'
7e7d0 3a 0a 20 20 20 20 63 61 73 65 20 27 31 27 3a 0a  :.    case '1':.
7e7e0 20 20 20 20 63 61 73 65 20 27 32 27 3a 0a 20 20      case '2':.  
7e7f0 20 20 63 61 73 65 20 27 33 27 3a 0a 20 20 20 20    case '3':.    
7e800 63 61 73 65 20 27 34 27 3a 0a 20 20 20 20 63 61  case '4':.    ca
7e810 73 65 20 27 35 27 3a 0a 20 20 20 20 63 61 73 65  se '5':.    case
7e820 20 27 36 27 3a 0a 20 20 20 20 63 61 73 65 20 27   '6':.    case '
7e830 37 27 3a 0a 20 20 20 20 63 61 73 65 20 27 38 27  7':.    case '8'
7e840 3a 0a 20 20 20 20 63 61 73 65 20 27 39 27 3a 20  :.    case '9': 
7e850 7b 0a 20 20 20 20 20 20 6e 20 3d 20 67 65 74 56  {.      n = getV
7e860 61 6c 75 65 28 7a 2c 20 26 72 29 3b 0a 20 20 20  alue(z, &r);.   
7e870 20 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 31 20     assert( n>=1 
7e880 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 6e  );.      if( z[n
7e890 5d 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20 20 20  ]==':' ){.      
7e8a0 20 20 2f 2a 20 41 20 6d 6f 64 69 66 69 65 72 20    /* A modifier 
7e8b0 6f 66 20 74 68 65 20 66 6f 72 6d 20 28 2b 7c 2d  of the form (+|-
7e8c0 29 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 20 61 64  )HH:MM:SS.FFF ad
7e8d0 64 73 20 28 6f 72 20 73 75 62 74 72 61 63 74 73  ds (or subtracts
7e8e0 29 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a  ) the.        **
7e8f0 20 73 70 65 63 69 66 69 65 64 20 6e 75 6d 62 65   specified numbe
7e900 72 20 6f 66 20 68 6f 75 72 73 2c 20 6d 69 6e 75  r of hours, minu
7e910 74 65 73 2c 20 73 65 63 6f 6e 64 73 2c 20 61 6e  tes, seconds, an
7e920 64 20 66 72 61 63 74 69 6f 6e 61 6c 20 73 65 63  d fractional sec
7e930 6f 6e 64 73 0a 20 20 20 20 20 20 20 20 2a 2a 20  onds.        ** 
7e940 74 6f 20 74 68 65 20 74 69 6d 65 2e 20 20 54 68  to the time.  Th
7e950 65 20 22 2e 46 46 46 22 20 6d 61 79 20 62 65 20  e ".FFF" may be 
7e960 6f 6d 69 74 74 65 64 2e 20 20 54 68 65 20 22 3a  omitted.  The ":
7e970 53 53 2e 46 46 46 22 20 6d 61 79 20 62 65 0a 20  SS.FFF" may be. 
7e980 20 20 20 20 20 20 20 2a 2a 20 6f 6d 69 74 74 65         ** omitte
7e990 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  d..        */.  
7e9a0 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
7e9b0 20 2a 7a 32 20 3d 20 7a 3b 0a 20 20 20 20 20 20   *z2 = z;.      
7e9c0 20 20 44 61 74 65 54 69 6d 65 20 74 78 3b 0a 20    DateTime tx;. 
7e9d0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69         sqlite3_i
7e9e0 6e 74 36 34 20 64 61 79 3b 0a 20 20 20 20 20 20  nt64 day;.      
7e9f0 20 20 69 66 28 20 21 69 73 64 69 67 69 74 28 2a    if( !isdigit(*
7ea00 28 75 38 2a 29 7a 32 29 20 29 20 7a 32 2b 2b 3b  (u8*)z2) ) z2++;
7ea10 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28  .        memset(
7ea20 26 74 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 74  &tx, 0, sizeof(t
7ea30 78 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  x));.        if(
7ea40 20 70 61 72 73 65 48 68 4d 6d 53 73 28 7a 32 2c   parseHhMmSs(z2,
7ea50 20 26 74 78 29 20 29 20 62 72 65 61 6b 3b 0a 20   &tx) ) break;. 
7ea60 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44         computeJD
7ea70 28 26 74 78 29 3b 0a 20 20 20 20 20 20 20 20 74  (&tx);.        t
7ea80 78 2e 69 4a 44 20 2d 3d 20 34 33 32 30 30 30 30  x.iJD -= 4320000
7ea90 30 3b 0a 20 20 20 20 20 20 20 20 64 61 79 20 3d  0;.        day =
7eaa0 20 74 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 30   tx.iJD/86400000
7eab0 3b 0a 20 20 20 20 20 20 20 20 74 78 2e 69 4a 44  ;.        tx.iJD
7eac0 20 2d 3d 20 64 61 79 2a 38 36 34 30 30 30 30 30   -= day*86400000
7ead0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b  ;.        if( z[
7eae0 30 5d 3d 3d 27 2d 27 20 29 20 74 78 2e 69 4a 44  0]=='-' ) tx.iJD
7eaf0 20 3d 20 2d 74 78 2e 69 4a 44 3b 0a 20 20 20 20   = -tx.iJD;.    
7eb00 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29      computeJD(p)
7eb10 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 59  ;.        clearY
7eb20 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20  MD_HMS_TZ(p);.  
7eb30 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20        p->iJD += 
7eb40 74 78 2e 69 4a 44 3b 0a 20 20 20 20 20 20 20 20  tx.iJD;.        
7eb50 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  rc = 0;.        
7eb60 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
7eb70 20 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a 20 20 20       z += n;.   
7eb80 20 20 20 77 68 69 6c 65 28 20 69 73 73 70 61 63     while( isspac
7eb90 65 28 2a 28 75 38 2a 29 7a 29 20 29 20 7a 2b 2b  e(*(u8*)z) ) z++
7eba0 3b 0a 20 20 20 20 20 20 6e 20 3d 20 73 74 72 6c  ;.      n = strl
7ebb0 65 6e 28 7a 29 3b 0a 20 20 20 20 20 20 69 66 28  en(z);.      if(
7ebc0 20 6e 3e 31 30 20 7c 7c 20 6e 3c 33 20 29 20 62   n>10 || n<3 ) b
7ebd0 72 65 61 6b 3b 0a 20 20 20 20 20 20 69 66 28 20  reak;.      if( 
7ebe0 7a 5b 6e 2d 31 5d 3d 3d 27 73 27 20 29 7b 20 7a  z[n-1]=='s' ){ z
7ebf0 5b 6e 2d 31 5d 20 3d 20 30 3b 20 6e 2d 2d 3b 20  [n-1] = 0; n--; 
7ec00 7d 0a 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a  }.      computeJ
7ec10 44 28 70 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  D(p);.      rc =
7ec20 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3d   0;.      if( n=
7ec30 3d 33 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22  =3 && strcmp(z,"
7ec40 64 61 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  day")==0 ){.    
7ec50 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 72 2a      p->iJD += r*
7ec60 38 36 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35  86400000.0 + 0.5
7ec70 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
7ec80 28 20 6e 3d 3d 34 20 26 26 20 73 74 72 63 6d 70  ( n==4 && strcmp
7ec90 28 7a 2c 22 68 6f 75 72 22 29 3d 3d 30 20 29 7b  (z,"hour")==0 ){
7eca0 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20  .        p->iJD 
7ecb0 2b 3d 20 72 2a 28 38 36 34 30 30 30 30 30 2e 30  += r*(86400000.0
7ecc0 2f 32 34 2e 30 29 20 2b 20 30 2e 35 3b 0a 20 20  /24.0) + 0.5;.  
7ecd0 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d      }else if( n=
7ece0 3d 36 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22  =6 && strcmp(z,"
7ecf0 6d 69 6e 75 74 65 22 29 3d 3d 30 20 29 7b 0a 20  minute")==0 ){. 
7ed00 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d         p->iJD +=
7ed10 20 72 2a 28 38 36 34 30 30 30 30 30 2e 30 2f 28   r*(86400000.0/(
7ed20 32 34 2e 30 2a 36 30 2e 30 29 29 20 2b 20 30 2e  24.0*60.0)) + 0.
7ed30 35 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  5;.      }else i
7ed40 66 28 20 6e 3d 3d 36 20 26 26 20 73 74 72 63 6d  f( n==6 && strcm
7ed50 70 28 7a 2c 22 73 65 63 6f 6e 64 22 29 3d 3d 30  p(z,"second")==0
7ed60 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69   ){.        p->i
7ed70 4a 44 20 2b 3d 20 72 2a 28 38 36 34 30 30 30 30  JD += r*(8640000
7ed80 30 2e 30 2f 28 32 34 2e 30 2a 36 30 2e 30 2a 36  0.0/(24.0*60.0*6
7ed90 30 2e 30 29 29 20 2b 20 30 2e 35 3b 0a 20 20 20  0.0)) + 0.5;.   
7eda0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d     }else if( n==
7edb0 35 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d  5 && strcmp(z,"m
7edc0 6f 6e 74 68 22 29 3d 3d 30 20 29 7b 0a 20 20 20  onth")==0 ){.   
7edd0 20 20 20 20 20 69 6e 74 20 78 2c 20 79 3b 0a 20       int x, y;. 
7ede0 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d         computeYM
7edf0 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 20  D_HMS(p);.      
7ee00 20 20 70 2d 3e 4d 20 2b 3d 20 72 3b 0a 20 20 20    p->M += r;.   
7ee10 20 20 20 20 20 78 20 3d 20 70 2d 3e 4d 3e 30 20       x = p->M>0 
7ee20 3f 20 28 70 2d 3e 4d 2d 31 29 2f 31 32 20 3a 20  ? (p->M-1)/12 : 
7ee30 28 70 2d 3e 4d 2d 31 32 29 2f 31 32 3b 0a 20 20  (p->M-12)/12;.  
7ee40 20 20 20 20 20 20 70 2d 3e 59 20 2b 3d 20 78 3b        p->Y += x;
7ee50 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 2d 3d  .        p->M -=
7ee60 20 78 2a 31 32 3b 0a 20 20 20 20 20 20 20 20 70   x*12;.        p
7ee70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20  ->validJD = 0;. 
7ee80 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44         computeJD
7ee90 28 70 29 3b 0a 20 20 20 20 20 20 20 20 79 20 3d  (p);.        y =
7eea0 20 72 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   r;.        if( 
7eeb0 79 21 3d 72 20 29 7b 0a 20 20 20 20 20 20 20 20  y!=r ){.        
7eec0 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 72 20 2d    p->iJD += (r -
7eed0 20 79 29 2a 33 30 2e 30 2a 38 36 34 30 30 30 30   y)*30.0*8640000
7eee0 30 2e 30 20 2b 20 30 2e 35 3b 0a 20 20 20 20 20  0.0 + 0.5;.     
7eef0 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
7ef00 20 69 66 28 20 6e 3d 3d 34 20 26 26 20 73 74 72   if( n==4 && str
7ef10 63 6d 70 28 7a 2c 22 79 65 61 72 22 29 3d 3d 30  cmp(z,"year")==0
7ef20 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70   ){.        comp
7ef30 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20  uteYMD_HMS(p);. 
7ef40 20 20 20 20 20 20 20 70 2d 3e 59 20 2b 3d 20 72         p->Y += r
7ef50 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c  ;.        p->val
7ef60 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20  idJD = 0;.      
7ef70 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a    computeJD(p);.
7ef80 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
7ef90 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20       rc = 1;.   
7efa0 20 20 20 7d 0a 20 20 20 20 20 20 63 6c 65 61 72     }.      clear
7efb0 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20  YMD_HMS_TZ(p);. 
7efc0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
7efd0 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b  }.    default: {
7efe0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
7eff0 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
7f000 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72   rc;.}../*.** Pr
7f010 6f 63 65 73 73 20 74 69 6d 65 20 66 75 6e 63 74  ocess time funct
7f020 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 2e 20 20  ion arguments.  
7f030 61 72 67 76 5b 30 5d 20 69 73 20 61 20 64 61 74  argv[0] is a dat
7f040 65 2d 74 69 6d 65 20 73 74 61 6d 70 2e 0a 2a 2a  e-time stamp..**
7f050 20 61 72 67 76 5b 31 5d 20 61 6e 64 20 66 6f 6c   argv[1] and fol
7f060 6c 6f 77 69 6e 67 20 61 72 65 20 6d 6f 64 69 66  lowing are modif
7f070 69 65 72 73 2e 20 20 50 61 72 73 65 20 74 68 65  iers.  Parse the
7f080 6d 20 61 6c 6c 20 61 6e 64 20 77 72 69 74 65 0a  m all and write.
7f090 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67  ** the resulting
7f0a0 20 74 69 6d 65 20 69 6e 74 6f 20 74 68 65 20 44   time into the D
7f0b0 61 74 65 54 69 6d 65 20 73 74 72 75 63 74 75 72  ateTime structur
7f0c0 65 20 70 2e 20 20 52 65 74 75 72 6e 20 30 0a 2a  e p.  Return 0.*
7f0d0 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64  * on success and
7f0e0 20 31 20 69 66 20 74 68 65 72 65 20 61 72 65 20   1 if there are 
7f0f0 61 6e 79 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a  any errors..**.*
7f100 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 7a  * If there are z
7f110 65 72 6f 20 70 61 72 61 6d 65 74 65 72 73 20 28  ero parameters (
7f120 69 66 20 65 76 65 6e 20 61 72 67 76 5b 30 5d 20  if even argv[0] 
7f130 69 73 20 75 6e 64 65 66 69 6e 65 64 29 0a 2a 2a  is undefined).**
7f140 20 74 68 65 6e 20 61 73 73 75 6d 65 20 61 20 64   then assume a d
7f150 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20  efault value of 
7f160 22 6e 6f 77 22 20 66 6f 72 20 61 72 67 76 5b 30  "now" for argv[0
7f170 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  ]..*/.static int
7f180 20 69 73 44 61 74 65 28 0a 20 20 73 71 6c 69 74   isDate(.  sqlit
7f190 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
7f1a0 65 78 74 2c 20 0a 20 20 69 6e 74 20 61 72 67 63  ext, .  int argc
7f1b0 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  , .  sqlite3_val
7f1c0 75 65 20 2a 2a 61 72 67 76 2c 20 0a 20 20 44 61  ue **argv, .  Da
7f1d0 74 65 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69  teTime *p.){.  i
7f1e0 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74 20 75 6e  nt i;.  const un
7f1f0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a  signed char *z;.
7f200 20 20 69 6e 74 20 65 54 79 70 65 3b 0a 20 20 6d    int eType;.  m
7f210 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65  emset(p, 0, size
7f220 6f 66 28 2a 70 29 29 3b 0a 20 20 69 66 28 20 61  of(*p));.  if( a
7f230 72 67 63 3d 3d 30 20 29 7b 0a 20 20 20 20 73 65  rgc==0 ){.    se
7f240 74 44 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65  tDateTimeToCurre
7f250 6e 74 28 63 6f 6e 74 65 78 74 2c 20 70 29 3b 0a  nt(context, p);.
7f260 20 20 7d 65 6c 73 65 20 69 66 28 20 28 65 54 79    }else if( (eTy
7f270 70 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  pe = sqlite3_val
7f280 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29  ue_type(argv[0])
7f290 29 3d 3d 53 51 4c 49 54 45 5f 46 4c 4f 41 54 0a  )==SQLITE_FLOAT.
7f2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7f2b0 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c     || eType==SQL
7f2c0 49 54 45 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20  ITE_INTEGER ){. 
7f2d0 20 20 20 70 2d 3e 69 4a 44 20 3d 20 73 71 6c 69     p->iJD = sqli
7f2e0 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65  te3_value_double
7f2f0 28 61 72 67 76 5b 30 5d 29 2a 38 36 34 30 30 30  (argv[0])*864000
7f300 30 30 2e 30 20 2b 20 30 2e 35 3b 0a 20 20 20 20  00.0 + 0.5;.    
7f310 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a  p->validJD = 1;.
7f320 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d    }else{.    z =
7f330 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
7f340 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ext(argv[0]);.  
7f350 20 20 69 66 28 20 21 7a 20 7c 7c 20 70 61 72 73    if( !z || pars
7f360 65 44 61 74 65 4f 72 54 69 6d 65 28 63 6f 6e 74  eDateOrTime(cont
7f370 65 78 74 2c 20 28 63 68 61 72 2a 29 7a 2c 20 70  ext, (char*)z, p
7f380 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
7f390 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n 1;.    }.  }. 
7f3a0 20 66 6f 72 28 69 3d 31 3b 20 69 3c 61 72 67 63   for(i=1; i<argc
7f3b0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
7f3c0 28 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  (z = sqlite3_val
7f3d0 75 65 5f 74 65 78 74 28 61 72 67 76 5b 69 5d 29  ue_text(argv[i])
7f3e0 29 3d 3d 30 20 7c 7c 20 70 61 72 73 65 4d 6f 64  )==0 || parseMod
7f3f0 69 66 69 65 72 28 28 63 68 61 72 2a 29 7a 2c 20  ifier((char*)z, 
7f400 70 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  p) ){.      retu
7f410 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rn 1;.    }.  }.
7f420 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a    return 0;.}...
7f430 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
7f440 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 69 6d 70  ing routines imp
7f450 6c 65 6d 65 6e 74 20 74 68 65 20 76 61 72 69 6f  lement the vario
7f460 75 73 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65  us date and time
7f470 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6f 66   functions.** of
7f480 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a   SQLite..*/../*.
7f490 2a 2a 20 20 20 20 6a 75 6c 69 61 6e 64 61 79 28  **    julianday(
7f4a0 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44   TIMESTRING, MOD
7f4b0 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a  , MOD, ...).**.*
7f4c0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6a 75 6c  * Return the jul
7f4d0 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 20 6f  ian day number o
7f4e0 66 20 74 68 65 20 64 61 74 65 20 73 70 65 63 69  f the date speci
7f4f0 66 69 65 64 20 69 6e 20 74 68 65 20 61 72 67 75  fied in the argu
7f500 6d 65 6e 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20  ments.*/.static 
7f510 76 6f 69 64 20 6a 75 6c 69 61 6e 64 61 79 46 75  void juliandayFu
7f520 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
7f530 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
7f540 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
7f550 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
7f560 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65  gv.){.  DateTime
7f570 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65   x;.  if( isDate
7f580 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20  (context, argc, 
7f590 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a  argv, &x)==0 ){.
7f5a0 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78      computeJD(&x
7f5b0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
7f5c0 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e  esult_double(con
7f5d0 74 65 78 74 2c 20 78 2e 69 4a 44 2f 38 36 34 30  text, x.iJD/8640
7f5e0 30 30 30 30 2e 30 29 3b 0a 20 20 7d 0a 7d 0a 0a  0000.0);.  }.}..
7f5f0 2f 2a 0a 2a 2a 20 20 20 20 64 61 74 65 74 69 6d  /*.**    datetim
7f600 65 28 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d  e( TIMESTRING, M
7f610 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a  OD, MOD, ...).**
7f620 0a 2a 2a 20 52 65 74 75 72 6e 20 59 59 59 59 2d  .** Return YYYY-
7f630 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 0a 2a  MM-DD HH:MM:SS.*
7f640 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61  /.static void da
7f650 74 65 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 71  tetimeFunc(.  sq
7f660 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
7f670 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
7f680 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
7f690 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
7f6a0 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66  DateTime x;.  if
7f6b0 28 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74  ( isDate(context
7f6c0 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78  , argc, argv, &x
7f6d0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72  )==0 ){.    char
7f6e0 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20   zBuf[100];.    
7f6f0 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 26  computeYMD_HMS(&
7f700 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  x);.    sqlite3_
7f710 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
7f720 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 30  zBuf), zBuf, "%0
7f730 34 64 2d 25 30 32 64 2d 25 30 32 64 20 25 30 32  4d-%02d-%02d %02
7f740 64 3a 25 30 32 64 3a 25 30 32 64 22 2c 0a 20 20  d:%02d:%02d",.  
7f750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7f760 20 20 20 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44     x.Y, x.M, x.D
7f770 2c 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e 74  , x.h, x.m, (int
7f780 29 28 78 2e 73 29 29 3b 0a 20 20 20 20 73 71 6c  )(x.s));.    sql
7f790 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
7f7a0 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20  (context, zBuf, 
7f7b0 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  -1, SQLITE_TRANS
7f7c0 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  IENT);.  }.}../*
7f7d0 0a 2a 2a 20 20 20 20 74 69 6d 65 28 20 54 49 4d  .**    time( TIM
7f7e0 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f  ESTRING, MOD, MO
7f7f0 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65  D, ...).**.** Re
7f800 74 75 72 6e 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f  turn HH:MM:SS.*/
7f810 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 69 6d  .static void tim
7f820 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  eFunc(.  sqlite3
7f830 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
7f840 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
7f850 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
7f860 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54  *argv.){.  DateT
7f870 69 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44  ime x;.  if( isD
7f880 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67  ate(context, arg
7f890 63 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20  c, argv, &x)==0 
7f8a0 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66  ){.    char zBuf
7f8b0 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75  [100];.    compu
7f8c0 74 65 48 4d 53 28 26 78 29 3b 0a 20 20 20 20 73  teHMS(&x);.    s
7f8d0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
7f8e0 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42  sizeof(zBuf), zB
7f8f0 75 66 2c 20 22 25 30 32 64 3a 25 30 32 64 3a 25  uf, "%02d:%02d:%
7f900 30 32 64 22 2c 20 78 2e 68 2c 20 78 2e 6d 2c 20  02d", x.h, x.m, 
7f910 28 69 6e 74 29 78 2e 73 29 3b 0a 20 20 20 20 73  (int)x.s);.    s
7f920 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
7f930 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66  xt(context, zBuf
7f940 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41  , -1, SQLITE_TRA
7f950 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a  NSIENT);.  }.}..
7f960 2f 2a 0a 2a 2a 20 20 20 20 64 61 74 65 28 20 54  /*.**    date( T
7f970 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20  IMESTRING, MOD, 
7f980 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20  MOD, ...).**.** 
7f990 52 65 74 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44  Return YYYY-MM-D
7f9a0 44 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  D.*/.static void
7f9b0 20 64 61 74 65 46 75 6e 63 28 0a 20 20 73 71 6c   dateFunc(.  sql
7f9c0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
7f9d0 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
7f9e0 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
7f9f0 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44  ue **argv.){.  D
7fa00 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28  ateTime x;.  if(
7fa10 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c   isDate(context,
7fa20 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29   argc, argv, &x)
7fa30 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20  ==0 ){.    char 
7fa40 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 63  zBuf[100];.    c
7fa50 6f 6d 70 75 74 65 59 4d 44 28 26 78 29 3b 0a 20  omputeYMD(&x);. 
7fa60 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
7fa70 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29  ntf(sizeof(zBuf)
7fa80 2c 20 7a 42 75 66 2c 20 22 25 30 34 64 2d 25 30  , zBuf, "%04d-%0
7fa90 32 64 2d 25 30 32 64 22 2c 20 78 2e 59 2c 20 78  2d-%02d", x.Y, x
7faa0 2e 4d 2c 20 78 2e 44 29 3b 0a 20 20 20 20 73 71  .M, x.D);.    sq
7fab0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
7fac0 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c  t(context, zBuf,
7fad0 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e   -1, SQLITE_TRAN
7fae0 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  SIENT);.  }.}../
7faf0 2a 0a 2a 2a 20 20 20 20 73 74 72 66 74 69 6d 65  *.**    strftime
7fb00 28 20 46 4f 52 4d 41 54 2c 20 54 49 4d 45 53 54  ( FORMAT, TIMEST
7fb10 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20  RING, MOD, MOD, 
7fb20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ...).**.** Retur
7fb30 6e 20 61 20 73 74 72 69 6e 67 20 64 65 73 63 72  n a string descr
7fb40 69 62 65 64 20 62 79 20 46 4f 52 4d 41 54 2e 20  ibed by FORMAT. 
7fb50 20 43 6f 6e 76 65 72 73 69 6f 6e 73 20 61 73 20   Conversions as 
7fb60 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20  follows:.**.**  
7fb70 20 25 64 20 20 64 61 79 20 6f 66 20 6d 6f 6e 74   %d  day of mont
7fb80 68 0a 2a 2a 20 20 20 25 66 20 20 2a 2a 20 66 72  h.**   %f  ** fr
7fb90 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73  actional seconds
7fba0 20 20 53 53 2e 53 53 53 0a 2a 2a 20 20 20 25 48    SS.SSS.**   %H
7fbb0 20 20 68 6f 75 72 20 30 30 2d 32 34 0a 2a 2a 20    hour 00-24.** 
7fbc0 20 20 25 6a 20 20 64 61 79 20 6f 66 20 79 65 61    %j  day of yea
7fbd0 72 20 30 30 30 2d 33 36 36 0a 2a 2a 20 20 20 25  r 000-366.**   %
7fbe0 4a 20 20 2a 2a 20 4a 75 6c 69 61 6e 20 64 61 79  J  ** Julian day
7fbf0 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 25 6d 20   number.**   %m 
7fc00 20 6d 6f 6e 74 68 20 30 31 2d 31 32 0a 2a 2a 20   month 01-12.** 
7fc10 20 20 25 4d 20 20 6d 69 6e 75 74 65 20 30 30 2d    %M  minute 00-
7fc20 35 39 0a 2a 2a 20 20 20 25 73 20 20 73 65 63 6f  59.**   %s  seco
7fc30 6e 64 73 20 73 69 6e 63 65 20 31 39 37 30 2d 30  nds since 1970-0
7fc40 31 2d 30 31 0a 2a 2a 20 20 20 25 53 20 20 73 65  1-01.**   %S  se
7fc50 63 6f 6e 64 73 20 30 30 2d 35 39 0a 2a 2a 20 20  conds 00-59.**  
7fc60 20 25 77 20 20 64 61 79 20 6f 66 20 77 65 65 6b   %w  day of week
7fc70 20 30 2d 36 20 20 73 75 6e 64 61 79 3d 3d 30 0a   0-6  sunday==0.
7fc80 2a 2a 20 20 20 25 57 20 20 77 65 65 6b 20 6f 66  **   %W  week of
7fc90 20 79 65 61 72 20 30 30 2d 35 33 0a 2a 2a 20 20   year 00-53.**  
7fca0 20 25 59 20 20 79 65 61 72 20 30 30 30 30 2d 39   %Y  year 0000-9
7fcb0 39 39 39 0a 2a 2a 20 20 20 25 25 20 20 25 0a 2a  999.**   %%  %.*
7fcc0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 74  /.static void st
7fcd0 72 66 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 71  rftimeFunc(.  sq
7fce0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
7fcf0 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
7fd00 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
7fd10 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
7fd20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 75 36  DateTime x;.  u6
7fd30 34 20 6e 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  4 n;.  int i, j;
7fd40 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 73 71  .  char *z;.  sq
7fd50 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 63 6f 6e  lite3 *db;.  con
7fd60 73 74 20 63 68 61 72 20 2a 7a 46 6d 74 20 3d 20  st char *zFmt = 
7fd70 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c  (const char*)sql
7fd80 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
7fd90 61 72 67 76 5b 30 5d 29 3b 0a 20 20 63 68 61 72  argv[0]);.  char
7fda0 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 69 66   zBuf[100];.  if
7fdb0 28 20 7a 46 6d 74 3d 3d 30 20 7c 7c 20 69 73 44  ( zFmt==0 || isD
7fdc0 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67  ate(context, arg
7fdd0 63 2d 31 2c 20 61 72 67 76 2b 31 2c 20 26 78 29  c-1, argv+1, &x)
7fde0 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20   ) return;.  db 
7fdf0 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  = sqlite3_contex
7fe00 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74  t_db_handle(cont
7fe10 65 78 74 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c  ext);.  for(i=0,
7fe20 20 6e 3d 31 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69   n=1; zFmt[i]; i
7fe30 2b 2b 2c 20 6e 2b 2b 29 7b 0a 20 20 20 20 69 66  ++, n++){.    if
7fe40 28 20 7a 46 6d 74 5b 69 5d 3d 3d 27 25 27 20 29  ( zFmt[i]=='%' )
7fe50 7b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 20  {.      switch( 
7fe60 7a 46 6d 74 5b 69 2b 31 5d 20 29 7b 0a 20 20 20  zFmt[i+1] ){.   
7fe70 20 20 20 20 20 63 61 73 65 20 27 64 27 3a 0a 20       case 'd':. 
7fe80 20 20 20 20 20 20 20 63 61 73 65 20 27 48 27 3a         case 'H':
7fe90 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 6d  .        case 'm
7fea0 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  ':.        case 
7feb0 27 4d 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73  'M':.        cas
7fec0 65 20 27 53 27 3a 0a 20 20 20 20 20 20 20 20 63  e 'S':.        c
7fed0 61 73 65 20 27 57 27 3a 0a 20 20 20 20 20 20 20  ase 'W':.       
7fee0 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20     n++;.        
7fef0 20 20 2f 2a 20 66 61 6c 6c 20 74 68 72 75 20 2a    /* fall thru *
7ff00 2f 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  /.        case '
7ff10 77 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65  w':.        case
7ff20 20 27 25 27 3a 0a 20 20 20 20 20 20 20 20 20 20   '%':.          
7ff30 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63  break;.        c
7ff40 61 73 65 20 27 66 27 3a 0a 20 20 20 20 20 20 20  ase 'f':.       
7ff50 20 20 20 6e 20 2b 3d 20 38 3b 0a 20 20 20 20 20     n += 8;.     
7ff60 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
7ff70 20 20 20 20 63 61 73 65 20 27 6a 27 3a 0a 20 20      case 'j':.  
7ff80 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 33 3b 0a          n += 3;.
7ff90 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
7ffa0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 59  .        case 'Y
7ffb0 27 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b  ':.          n +
7ffc0 3d 20 38 3b 0a 20 20 20 20 20 20 20 20 20 20 62  = 8;.          b
7ffd0 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61  reak;.        ca
7ffe0 73 65 20 27 73 27 3a 0a 20 20 20 20 20 20 20 20  se 's':.        
7fff0 63 61 73 65 20 27 4a 27 3a 0a 20 20 20 20 20 20  case 'J':.      
80000 20 20 20 20 6e 20 2b 3d 20 35 30 3b 0a 20 20 20      n += 50;.   
80010 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
80020 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20        default:. 
80030 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b           return;
80040 20 20 2f 2a 20 45 52 52 4f 52 2e 20 20 72 65 74    /* ERROR.  ret
80050 75 72 6e 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20  urn a NULL */.  
80060 20 20 20 20 7d 0a 20 20 20 20 20 20 69 2b 2b 3b      }.      i++;
80070 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
80080 20 6e 3c 73 69 7a 65 6f 66 28 7a 42 75 66 29 20   n<sizeof(zBuf) 
80090 29 7b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b  ){.    z = zBuf;
800a0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3e 64  .  }else if( n>d
800b0 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
800c0 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29  _LIMIT_LENGTH] )
800d0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
800e0 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69  sult_error_toobi
800f0 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20  g(context);.    
80100 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b  return;.  }else{
80110 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33  .    z = sqlite3
80120 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
80130 6e 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30  n);.    if( z==0
80140 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
80150 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e  3_result_error_n
80160 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20  omem(context);. 
80170 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
80180 20 7d 0a 20 20 7d 0a 20 20 63 6f 6d 70 75 74 65   }.  }.  compute
80190 4a 44 28 26 78 29 3b 0a 20 20 63 6f 6d 70 75 74  JD(&x);.  comput
801a0 65 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a 20 20  eYMD_HMS(&x);.  
801b0 66 6f 72 28 69 3d 6a 3d 30 3b 20 7a 46 6d 74 5b  for(i=j=0; zFmt[
801c0 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  i]; i++){.    if
801d0 28 20 7a 46 6d 74 5b 69 5d 21 3d 27 25 27 20 29  ( zFmt[i]!='%' )
801e0 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d  {.      z[j++] =
801f0 20 7a 46 6d 74 5b 69 5d 3b 0a 20 20 20 20 7d 65   zFmt[i];.    }e
80200 6c 73 65 7b 0a 20 20 20 20 20 20 69 2b 2b 3b 0a  lse{.      i++;.
80210 20 20 20 20 20 20 73 77 69 74 63 68 28 20 7a 46        switch( zF
80220 6d 74 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 20  mt[i] ){.       
80230 20 63 61 73 65 20 27 64 27 3a 20 20 73 71 6c 69   case 'd':  sqli
80240 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20  te3_snprintf(3, 
80250 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 44  &z[j],"%02d",x.D
80260 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a  ); j+=2; break;.
80270 20 20 20 20 20 20 20 20 63 61 73 65 20 27 66 27          case 'f'
80280 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 64 6f  : {.          do
80290 75 62 6c 65 20 73 20 3d 20 78 2e 73 3b 0a 20 20  uble s = x.s;.  
802a0 20 20 20 20 20 20 20 20 69 66 28 20 73 3e 35 39          if( s>59
802b0 2e 39 39 39 20 29 20 73 20 3d 20 35 39 2e 39 39  .999 ) s = 59.99
802c0 39 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  9;.          sql
802d0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 37 2c  ite3_snprintf(7,
802e0 20 26 7a 5b 6a 5d 2c 22 25 30 36 2e 33 66 22 2c   &z[j],"%06.3f",
802f0 20 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a   s);.          j
80300 20 2b 3d 20 73 74 72 6c 65 6e 28 26 7a 5b 6a 5d   += strlen(&z[j]
80310 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  );.          bre
80320 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
80330 20 20 20 20 20 20 63 61 73 65 20 27 48 27 3a 20        case 'H': 
80340 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
80350 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64  f(3, &z[j],"%02d
80360 22 2c 78 2e 68 29 3b 20 6a 2b 3d 32 3b 20 62 72  ",x.h); j+=2; br
80370 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
80380 65 20 27 57 27 3a 20 2f 2a 20 46 61 6c 6c 20 74  e 'W': /* Fall t
80390 68 72 75 20 2a 2f 0a 20 20 20 20 20 20 20 20 63  hru */.        c
803a0 61 73 65 20 27 6a 27 3a 20 7b 0a 20 20 20 20 20  ase 'j': {.     
803b0 20 20 20 20 20 69 6e 74 20 6e 44 61 79 3b 20 20       int nDay;  
803c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
803d0 6d 62 65 72 20 6f 66 20 64 61 79 73 20 73 69 6e  mber of days sin
803e0 63 65 20 31 73 74 20 64 61 79 20 6f 66 20 79 65  ce 1st day of ye
803f0 61 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  ar */.          
80400 44 61 74 65 54 69 6d 65 20 79 20 3d 20 78 3b 0a  DateTime y = x;.
80410 20 20 20 20 20 20 20 20 20 20 79 2e 76 61 6c 69            y.vali
80420 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  dJD = 0;.       
80430 20 20 20 79 2e 4d 20 3d 20 31 3b 0a 20 20 20 20     y.M = 1;.    
80440 20 20 20 20 20 20 79 2e 44 20 3d 20 31 3b 0a 20        y.D = 1;. 
80450 20 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65           compute
80460 4a 44 28 26 79 29 3b 0a 20 20 20 20 20 20 20 20  JD(&y);.        
80470 20 20 6e 44 61 79 20 3d 20 28 78 2e 69 4a 44 20    nDay = (x.iJD 
80480 2d 20 79 2e 69 4a 44 29 2f 38 36 34 30 30 30 30  - y.iJD)/8640000
80490 30 2e 30 20 2b 20 30 2e 35 3b 0a 20 20 20 20 20  0.0 + 0.5;.     
804a0 20 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69 5d       if( zFmt[i]
804b0 3d 3d 27 57 27 20 29 7b 0a 20 20 20 20 20 20 20  =='W' ){.       
804c0 20 20 20 20 20 69 6e 74 20 77 64 3b 20 20 20 2f       int wd;   /
804d0 2a 20 30 3d 4d 6f 6e 64 61 79 2c 20 31 3d 54 75  * 0=Monday, 1=Tu
804e0 65 73 64 61 79 2c 20 2e 2e 2e 20 36 3d 53 75 6e  esday, ... 6=Sun
804f0 64 61 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  day */.         
80500 20 20 20 77 64 20 3d 20 28 28 78 2e 69 4a 44 2b     wd = ((x.iJD+
80510 34 33 32 30 30 30 30 30 29 2f 38 36 34 30 30 30  43200000)/864000
80520 30 30 29 20 25 20 37 3b 0a 20 20 20 20 20 20 20  00) % 7;.       
80530 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
80540 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22  rintf(3, &z[j],"
80550 25 30 32 64 22 2c 28 6e 44 61 79 2b 37 2d 77 64  %02d",(nDay+7-wd
80560 29 2f 37 29 3b 0a 20 20 20 20 20 20 20 20 20 20  )/7);.          
80570 20 20 6a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20    j += 2;.      
80580 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
80590 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73         sqlite3_s
805a0 6e 70 72 69 6e 74 66 28 34 2c 20 26 7a 5b 6a 5d  nprintf(4, &z[j]
805b0 2c 22 25 30 33 64 22 2c 6e 44 61 79 2b 31 29 3b  ,"%03d",nDay+1);
805c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a 20 2b  .            j +
805d0 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  = 3;.          }
805e0 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
805f0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
80600 20 20 20 20 63 61 73 65 20 27 4a 27 3a 20 7b 0a      case 'J': {.
80610 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
80620 33 5f 73 6e 70 72 69 6e 74 66 28 32 30 2c 20 26  3_snprintf(20, &
80630 7a 5b 6a 5d 2c 22 25 2e 31 36 67 22 2c 78 2e 69  z[j],"%.16g",x.i
80640 4a 44 2f 38 36 34 30 30 30 30 30 2e 30 29 3b 0a  JD/86400000.0);.
80650 20 20 20 20 20 20 20 20 20 20 6a 2b 3d 73 74 72            j+=str
80660 6c 65 6e 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20  len(&z[j]);.    
80670 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
80680 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63       }.        c
80690 61 73 65 20 27 6d 27 3a 20 20 73 71 6c 69 74 65  ase 'm':  sqlite
806a0 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a  3_snprintf(3, &z
806b0 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 4d 29 3b  [j],"%02d",x.M);
806c0 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20   j+=2; break;.  
806d0 20 20 20 20 20 20 63 61 73 65 20 27 4d 27 3a 20        case 'M': 
806e0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
806f0 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64  f(3, &z[j],"%02d
80700 22 2c 78 2e 6d 29 3b 20 6a 2b 3d 32 3b 20 62 72  ",x.m); j+=2; br
80710 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
80720 65 20 27 73 27 3a 20 7b 0a 20 20 20 20 20 20 20  e 's': {.       
80730 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
80740 6e 74 66 28 33 30 2c 26 7a 5b 6a 5d 2c 22 25 64  ntf(30,&z[j],"%d
80750 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
80760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69                (i
80770 6e 74 29 28 78 2e 69 4a 44 2f 31 30 30 30 2e 30  nt)(x.iJD/1000.0
80780 20 2d 20 32 31 30 38 36 36 37 36 30 30 30 30 2e   - 210866760000.
80790 30 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a  0));.          j
807a0 20 2b 3d 20 73 74 72 6c 65 6e 28 26 7a 5b 6a 5d   += strlen(&z[j]
807b0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  );.          bre
807c0 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
807d0 20 20 20 20 20 20 63 61 73 65 20 27 53 27 3a 20        case 'S': 
807e0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
807f0 66 28 33 2c 26 7a 5b 6a 5d 2c 22 25 30 32 64 22  f(3,&z[j],"%02d"
80800 2c 28 69 6e 74 29 78 2e 73 29 3b 20 6a 2b 3d 32  ,(int)x.s); j+=2
80810 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ; break;.       
80820 20 63 61 73 65 20 27 77 27 3a 20 20 7a 5b 6a 2b   case 'w':  z[j+
80830 2b 5d 20 3d 20 28 28 28 78 2e 69 4a 44 2b 31 32  +] = (((x.iJD+12
80840 39 36 30 30 30 30 30 29 2f 38 36 34 30 30 30 30  9600000)/8640000
80850 30 29 20 25 20 37 29 20 2b 20 27 30 27 3b 20 62  0) % 7) + '0'; b
80860 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61  reak;.        ca
80870 73 65 20 27 59 27 3a 20 20 73 71 6c 69 74 65 33  se 'Y':  sqlite3
80880 5f 73 6e 70 72 69 6e 74 66 28 35 2c 26 7a 5b 6a  _snprintf(5,&z[j
80890 5d 2c 22 25 30 34 64 22 2c 78 2e 59 29 3b 20 6a  ],"%04d",x.Y); j
808a0 2b 3d 73 74 72 6c 65 6e 28 26 7a 5b 6a 5d 29 3b  +=strlen(&z[j]);
808b0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 64  break;.        d
808c0 65 66 61 75 6c 74 3a 20 20 20 7a 5b 6a 2b 2b 5d  efault:   z[j++]
808d0 20 3d 20 27 25 27 3b 20 62 72 65 61 6b 3b 0a 20   = '%'; break;. 
808e0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
808f0 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73  .  z[j] = 0;.  s
80900 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
80910 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 2d  xt(context, z, -
80920 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1,.             
80930 20 20 20 20 20 20 20 20 20 7a 3d 3d 7a 42 75 66           z==zBuf
80940 20 3f 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49   ? SQLITE_TRANSI
80950 45 4e 54 20 3a 20 53 51 4c 49 54 45 5f 44 59 4e  ENT : SQLITE_DYN
80960 41 4d 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  AMIC);.}../*.** 
80970 63 75 72 72 65 6e 74 5f 74 69 6d 65 28 29 0a 2a  current_time().*
80980 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
80990 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73  on returns the s
809a0 61 6d 65 20 76 61 6c 75 65 20 61 73 20 74 69 6d  ame value as tim
809b0 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61  e('now')..*/.sta
809c0 74 69 63 20 76 6f 69 64 20 63 74 69 6d 65 46 75  tic void ctimeFu
809d0 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
809e0 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
809f0 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
80a00 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
80a10 67 76 0a 29 7b 0a 20 20 74 69 6d 65 46 75 6e 63  gv.){.  timeFunc
80a20 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b  (context, 0, 0);
80a30 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e  .}../*.** curren
80a40 74 5f 64 61 74 65 28 29 0a 2a 2a 0a 2a 2a 20 54  t_date().**.** T
80a50 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
80a60 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76 61  urns the same va
80a70 6c 75 65 20 61 73 20 64 61 74 65 28 27 6e 6f 77  lue as date('now
80a80 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ')..*/.static vo
80a90 69 64 20 63 64 61 74 65 46 75 6e 63 28 0a 20 20  id cdateFunc(.  
80aa0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
80ab0 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
80ac0 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
80ad0 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
80ae0 20 20 64 61 74 65 46 75 6e 63 28 63 6f 6e 74 65    dateFunc(conte
80af0 78 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a  xt, 0, 0);.}../*
80b00 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 74 69 6d 65  .** current_time
80b10 73 74 61 6d 70 28 29 0a 2a 2a 0a 2a 2a 20 54 68  stamp().**.** Th
80b20 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
80b30 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c  rns the same val
80b40 75 65 20 61 73 20 64 61 74 65 74 69 6d 65 28 27  ue as datetime('
80b50 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  now')..*/.static
80b60 20 76 6f 69 64 20 63 74 69 6d 65 73 74 61 6d 70   void ctimestamp
80b70 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
80b80 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
80b90 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
80ba0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
80bb0 61 72 67 76 0a 29 7b 0a 20 20 64 61 74 65 74 69  argv.){.  dateti
80bc0 6d 65 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20  meFunc(context, 
80bd0 30 2c 20 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20  0, 0);.}.#endif 
80be0 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
80bf0 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45  TE_OMIT_DATETIME
80c00 5f 46 55 4e 43 53 29 20 2a 2f 0a 0a 23 69 66 64  _FUNCS) */..#ifd
80c10 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
80c20 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 2f 2a  ATETIME_FUNCS./*
80c30 0a 2a 2a 20 49 66 20 74 68 65 20 6c 69 62 72 61  .** If the libra
80c40 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 74  ry is compiled t
80c50 6f 20 6f 6d 69 74 20 74 68 65 20 66 75 6c 6c 2d  o omit the full-
80c60 73 63 61 6c 65 20 64 61 74 65 20 61 6e 64 20 74  scale date and t
80c70 69 6d 65 0a 2a 2a 20 68 61 6e 64 6c 69 6e 67 20  ime.** handling 
80c80 28 74 6f 20 67 65 74 20 61 20 73 6d 61 6c 6c 65  (to get a smalle
80c90 72 20 62 69 6e 61 72 79 29 2c 20 74 68 65 20 66  r binary), the f
80ca0 6f 6c 6c 6f 77 69 6e 67 20 6d 69 6e 69 6d 61 6c  ollowing minimal
80cb0 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74   version.** of t
80cc0 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 63 75 72  he functions cur
80cd0 72 65 6e 74 5f 74 69 6d 65 28 29 2c 20 63 75 72  rent_time(), cur
80ce0 72 65 6e 74 5f 64 61 74 65 28 29 20 61 6e 64 20  rent_date() and 
80cf0 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d  current_timestam
80d00 70 28 29 0a 2a 2a 20 61 72 65 20 69 6e 63 6c 75  p().** are inclu
80d10 64 65 64 20 69 6e 73 74 65 61 64 2e 20 54 68 69  ded instead. Thi
80d20 73 20 69 73 20 74 6f 20 73 75 70 70 6f 72 74 20  s is to support 
80d30 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69  column declarati
80d40 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e 63 6c  ons that.** incl
80d50 75 64 65 20 22 44 45 46 41 55 4c 54 20 43 55 52  ude "DEFAULT CUR
80d60 52 45 4e 54 5f 54 49 4d 45 22 20 65 74 63 2e 0a  RENT_TIME" etc..
80d70 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
80d80 69 6f 6e 20 75 73 65 73 20 74 68 65 20 43 2d 6c  ion uses the C-l
80d90 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73  ibrary functions
80da0 20 74 69 6d 65 28 29 2c 20 67 6d 74 69 6d 65 28   time(), gmtime(
80db0 29 0a 2a 2a 20 61 6e 64 20 73 74 72 66 74 69 6d  ).** and strftim
80dc0 65 28 29 2e 20 54 68 65 20 66 6f 72 6d 61 74 20  e(). The format 
80dd0 73 74 72 69 6e 67 20 74 6f 20 70 61 73 73 20 74  string to pass t
80de0 6f 20 73 74 72 66 74 69 6d 65 28 29 20 69 73 20  o strftime() is 
80df0 73 75 70 70 6c 69 65 64 0a 2a 2a 20 61 73 20 74  supplied.** as t
80e00 68 65 20 75 73 65 72 2d 64 61 74 61 20 66 6f 72  he user-data for
80e10 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a   the function..*
80e20 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 75  /.static void cu
80e30 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 28 0a 20  rrentTimeFunc(. 
80e40 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
80e50 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
80e60 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
80e70 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
80e80 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 63  .  time_t t;.  c
80e90 68 61 72 20 2a 7a 46 6f 72 6d 61 74 20 3d 20 28  har *zFormat = (
80ea0 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 75  char *)sqlite3_u
80eb0 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74  ser_data(context
80ec0 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  );.  sqlite3 *db
80ed0 3b 0a 20 20 64 6f 75 62 6c 65 20 72 54 3b 0a 20  ;.  double rT;. 
80ee0 20 63 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a   char zBuf[20];.
80ef0 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65 33 5f  .  db = sqlite3_
80f00 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
80f10 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71  e(context);.  sq
80f20 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69  lite3OsCurrentTi
80f30 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 54  me(db->pVfs, &rT
80f40 29 3b 0a 20 20 74 20 3d 20 38 36 34 30 30 2e 30  );.  t = 86400.0
80f50 2a 28 72 54 20 2d 20 32 34 34 30 35 38 37 2e 35  *(rT - 2440587.5
80f60 29 20 2b 20 30 2e 35 3b 0a 23 69 66 64 65 66 20  ) + 0.5;.#ifdef 
80f70 48 41 56 45 5f 47 4d 54 49 4d 45 5f 52 0a 20 20  HAVE_GMTIME_R.  
80f80 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74 6d 20  {.    struct tm 
80f90 73 4e 6f 77 3b 0a 20 20 20 20 67 6d 74 69 6d 65  sNow;.    gmtime
80fa0 5f 72 28 26 74 2c 20 26 73 4e 6f 77 29 3b 0a 20  _r(&t, &sNow);. 
80fb0 20 20 20 73 74 72 66 74 69 6d 65 28 7a 42 75 66     strftime(zBuf
80fc0 2c 20 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 26  , 20, zFormat, &
80fd0 73 4e 6f 77 29 3b 0a 20 20 7d 0a 23 65 6c 73 65  sNow);.  }.#else
80fe0 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20  .  {.    struct 
80ff0 74 6d 20 2a 70 54 6d 3b 0a 20 20 20 20 73 71 6c  tm *pTm;.    sql
81000 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
81010 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c  (sqlite3MutexAll
81020 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
81030 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b  STATIC_MASTER));
81040 0a 20 20 20 20 70 54 6d 20 3d 20 67 6d 74 69 6d  .    pTm = gmtim
81050 65 28 26 74 29 3b 0a 20 20 20 20 73 74 72 66 74  e(&t);.    strft
81060 69 6d 65 28 7a 42 75 66 2c 20 32 30 2c 20 7a 46  ime(zBuf, 20, zF
81070 6f 72 6d 61 74 2c 20 70 54 6d 29 3b 0a 20 20 20  ormat, pTm);.   
81080 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
81090 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65  eave(sqlite3Mute
810a0 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
810b0 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
810c0 52 29 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  R));.  }.#endif.
810d0 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
810e0 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
810f0 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45  zBuf, -1, SQLITE
81100 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23  _TRANSIENT);.}.#
81110 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  endif../*.** Thi
81120 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73  s function regis
81130 74 65 72 65 64 20 61 6c 6c 20 6f 66 20 74 68 65  tered all of the
81140 20 61 62 6f 76 65 20 43 20 66 75 6e 63 74 69 6f   above C functio
81150 6e 73 20 61 73 20 53 51 4c 0a 2a 2a 20 66 75 6e  ns as SQL.** fun
81160 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 73 68  ctions.  This sh
81170 6f 75 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c 79  ould be the only
81180 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73   routine in this
81190 20 66 69 6c 65 20 77 69 74 68 0a 2a 2a 20 65 78   file with.** ex
811a0 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a  ternal linkage..
811b0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
811c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65  E void sqlite3Re
811d0 67 69 73 74 65 72 44 61 74 65 54 69 6d 65 46 75  gisterDateTimeFu
811e0 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 7b 0a 20  nctions(void){. 
811f0 20 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57   static SQLITE_W
81200 53 44 20 46 75 6e 63 44 65 66 20 61 44 61 74 65  SD FuncDef aDate
81210 54 69 6d 65 46 75 6e 63 73 5b 5d 20 3d 20 7b 0a  TimeFuncs[] = {.
81220 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
81230 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e  MIT_DATETIME_FUN
81240 43 53 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  CS.    FUNCTION(
81250 6a 75 6c 69 61 6e 64 61 79 2c 20 20 20 20 20 20  julianday,      
81260 20 20 2d 31 2c 20 30 2c 20 30 2c 20 6a 75 6c 69    -1, 0, 0, juli
81270 61 6e 64 61 79 46 75 6e 63 20 29 2c 0a 20 20 20  andayFunc ),.   
81280 20 46 55 4e 43 54 49 4f 4e 28 64 61 74 65 2c 20   FUNCTION(date, 
81290 20 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20              -1, 
812a0 30 2c 20 30 2c 20 64 61 74 65 46 75 6e 63 20 20  0, 0, dateFunc  
812b0 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
812c0 49 4f 4e 28 74 69 6d 65 2c 20 20 20 20 20 20 20  ION(time,       
812d0 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20        -1, 0, 0, 
812e0 74 69 6d 65 46 75 6e 63 20 20 20 20 20 20 29 2c  timeFunc      ),
812f0 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 64 61  .    FUNCTION(da
81300 74 65 74 69 6d 65 2c 20 20 20 20 20 20 20 20 20  tetime,         
81310 2d 31 2c 20 30 2c 20 30 2c 20 64 61 74 65 74 69  -1, 0, 0, dateti
81320 6d 65 46 75 6e 63 20 20 29 2c 0a 20 20 20 20 46  meFunc  ),.    F
81330 55 4e 43 54 49 4f 4e 28 73 74 72 66 74 69 6d 65  UNCTION(strftime
81340 2c 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c  ,         -1, 0,
81350 20 30 2c 20 73 74 72 66 74 69 6d 65 46 75 6e 63   0, strftimeFunc
81360 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
81370 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 2c 20  N(current_time, 
81380 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 74       0, 0, 0, ct
81390 69 6d 65 46 75 6e 63 20 20 20 20 20 29 2c 0a 20  imeFunc     ),. 
813a0 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 72     FUNCTION(curr
813b0 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20 30  ent_timestamp, 0
813c0 2c 20 30 2c 20 30 2c 20 63 74 69 6d 65 73 74 61  , 0, 0, ctimesta
813d0 6d 70 46 75 6e 63 29 2c 0a 20 20 20 20 46 55 4e  mpFunc),.    FUN
813e0 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64 61  CTION(current_da
813f0 74 65 2c 20 20 20 20 20 20 30 2c 20 30 2c 20 30  te,      0, 0, 0
81400 2c 20 63 64 61 74 65 46 75 6e 63 20 20 20 20 20  , cdateFunc     
81410 29 2c 0a 23 65 6c 73 65 0a 20 20 20 20 53 54 52  ),.#else.    STR
81420 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e  _FUNCTION(curren
81430 74 5f 74 69 6d 65 2c 20 20 20 20 20 20 30 2c 20  t_time,      0, 
81440 22 25 48 3a 25 4d 3a 25 53 22 2c 20 20 20 20 20  "%H:%M:%S",     
81450 20 20 20 20 20 30 2c 20 63 75 72 72 65 6e 74 54       0, currentT
81460 69 6d 65 46 75 6e 63 29 2c 0a 20 20 20 20 53 54  imeFunc),.    ST
81470 52 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65  R_FUNCTION(curre
81480 6e 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20 30 2c  nt_timestamp, 0,
81490 20 22 25 59 2d 25 6d 2d 25 64 22 2c 20 20 20 20   "%Y-%m-%d",    
814a0 20 20 20 20 20 20 30 2c 20 63 75 72 72 65 6e 74        0, current
814b0 54 69 6d 65 46 75 6e 63 29 2c 0a 20 20 20 20 53  TimeFunc),.    S
814c0 54 52 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72  TR_FUNCTION(curr
814d0 65 6e 74 5f 64 61 74 65 2c 20 20 20 20 20 20 30  ent_date,      0
814e0 2c 20 22 25 59 2d 25 6d 2d 25 64 20 25 48 3a 25  , "%Y-%m-%d %H:%
814f0 4d 3a 25 53 22 2c 20 30 2c 20 63 75 72 72 65 6e  M:%S", 0, curren
81500 74 54 69 6d 65 46 75 6e 63 29 2c 0a 23 65 6e 64  tTimeFunc),.#end
81510 69 66 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b  if.  };.  int i;
81520 0a 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a  .  FuncDefHash *
81530 70 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28  pHash = &GLOBAL(
81540 46 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71 6c  FuncDefHash, sql
81550 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69  ite3GlobalFuncti
81560 6f 6e 73 29 3b 0a 20 20 46 75 6e 63 44 65 66 20  ons);.  FuncDef 
81570 2a 61 46 75 6e 63 20 3d 20 28 46 75 6e 63 44 65  *aFunc = (FuncDe
81580 66 2a 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44  f*)&GLOBAL(FuncD
81590 65 66 2c 20 61 44 61 74 65 54 69 6d 65 46 75 6e  ef, aDateTimeFun
815a0 63 73 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b  cs);..  for(i=0;
815b0 20 69 3c 41 72 72 61 79 53 69 7a 65 28 61 44 61   i<ArraySize(aDa
815c0 74 65 54 69 6d 65 46 75 6e 63 73 29 3b 20 69 2b  teTimeFuncs); i+
815d0 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46  +){.    sqlite3F
815e0 75 6e 63 44 65 66 49 6e 73 65 72 74 28 70 48 61  uncDefInsert(pHa
815f0 73 68 2c 20 26 61 46 75 6e 63 5b 69 5d 29 3b 0a  sh, &aFunc[i]);.
81600 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a    }.}../********
81610 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 64 61  ****** End of da
81620 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  te.c ***********
81630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81650 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
81660 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
81670 65 20 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  e os.c *********
81680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
816a0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
816b0 35 20 4e 6f 76 65 6d 62 65 72 20 32 39 0a 2a 2a  5 November 29.**
816c0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
816d0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
816e0 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
816f0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
81700 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
81710 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
81720 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
81730 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
81740 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
81750 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
81760 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
81770 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
81780 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
81790 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
817a0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
817b0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
817c0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
817d0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
817e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
817f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81820 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
81830 65 20 63 6f 6e 74 61 69 6e 73 20 4f 53 20 69 6e  e contains OS in
81840 74 65 72 66 61 63 65 20 63 6f 64 65 20 74 68 61  terface code tha
81850 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61  t is common to a
81860 6c 6c 0a 2a 2a 20 61 72 63 68 69 74 65 63 74 75  ll.** architectu
81870 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  res..**.** $Id: 
81880 6f 73 2e 63 2c 76 20 31 2e 31 32 34 20 32 30 30  os.c,v 1.124 200
81890 38 2f 31 30 2f 30 37 20 31 35 3a 32 35 3a 34 38  8/10/07 15:25:48
818a0 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 64   drh Exp $.*/.#d
818b0 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 4f 53  efine _SQLITE_OS
818c0 5f 43 5f 20 31 0a 23 75 6e 64 65 66 20 5f 53 51  _C_ 1.#undef _SQ
818d0 4c 49 54 45 5f 4f 53 5f 43 5f 0a 0a 2f 2a 0a 2a  LITE_OS_C_../*.*
818e0 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 53 51  * The default SQ
818f0 4c 69 74 65 20 73 71 6c 69 74 65 33 5f 76 66 73  Lite sqlite3_vfs
81900 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
81910 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65   do not allocate
81920 0a 2a 2a 20 6d 65 6d 6f 72 79 20 28 61 63 74 75  .** memory (actu
81930 61 6c 6c 79 2c 20 6f 73 5f 75 6e 69 78 2e 63 20  ally, os_unix.c 
81940 61 6c 6c 6f 63 61 74 65 73 20 61 20 73 6d 61 6c  allocates a smal
81950 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f  l amount of memo
81960 72 79 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69  ry.** from withi
81970 6e 20 4f 73 4f 70 65 6e 28 29 29 2c 20 62 75 74  n OsOpen()), but
81980 20 73 6f 6d 65 20 74 68 69 72 64 2d 70 61 72 74   some third-part
81990 79 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  y implementation
819a0 73 20 6d 61 79 2e 0a 2a 2a 20 53 6f 20 77 65 20  s may..** So we 
819b0 74 65 73 74 20 74 68 65 20 65 66 66 65 63 74 73  test the effects
819c0 20 6f 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66   of a malloc() f
819d0 61 69 6c 69 6e 67 20 61 6e 64 20 74 68 65 20 73  ailing and the s
819e0 71 6c 69 74 65 33 4f 73 58 58 58 28 29 0a 2a 2a  qlite3OsXXX().**
819f0 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
81a00 69 6e 67 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ing SQLITE_IOERR
81a10 5f 4e 4f 4d 45 4d 20 75 73 69 6e 67 20 74 68 65  _NOMEM using the
81a20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45   DO_OS_MALLOC_TE
81a30 53 54 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20  ST macro..**.** 
81a40 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75  The following fu
81a50 6e 63 74 69 6f 6e 73 20 61 72 65 20 69 6e 73 74  nctions are inst
81a60 72 75 6d 65 6e 74 65 64 20 66 6f 72 20 6d 61 6c  rumented for mal
81a70 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20 0a 2a  loc() failure .*
81a80 2a 20 74 65 73 74 69 6e 67 3a 0a 2a 2a 0a 2a 2a  * testing:.**.**
81a90 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70       sqlite3OsOp
81aa0 65 6e 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69  en().**     sqli
81ab0 74 65 33 4f 73 52 65 61 64 28 29 0a 2a 2a 20 20  te3OsRead().**  
81ac0 20 20 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74     sqlite3OsWrit
81ad0 65 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  e().**     sqlit
81ae0 65 33 4f 73 53 79 6e 63 28 29 0a 2a 2a 20 20 20  e3OsSync().**   
81af0 20 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28    sqlite3OsLock(
81b00 29 0a 2a 2a 0a 2a 2f 0a 23 69 66 20 64 65 66 69  ).**.*/.#if defi
81b10 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29  ned(SQLITE_TEST)
81b20 20 26 26 20 28 53 51 4c 49 54 45 5f 4f 53 5f 57   && (SQLITE_OS_W
81b30 49 4e 3d 3d 30 29 0a 20 20 23 64 65 66 69 6e 65  IN==0).  #define
81b40 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45   DO_OS_MALLOC_TE
81b50 53 54 20 69 66 20 28 31 29 20 7b 20 20 20 20 20  ST if (1) {     
81b60 20 20 20 20 20 20 20 5c 0a 20 20 20 20 76 6f 69         \.    voi
81b70 64 20 2a 70 54 73 74 41 6c 6c 6f 63 20 3d 20 73  d *pTstAlloc = s
81b80 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 31 30 29  qlite3Malloc(10)
81b90 3b 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 66  ;       \.    if
81ba0 20 28 21 70 54 73 74 41 6c 6c 6f 63 29 20 72 65   (!pTstAlloc) re
81bb0 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
81bc0 52 5f 4e 4f 4d 45 4d 3b 20 20 5c 0a 20 20 20 20  R_NOMEM;  \.    
81bd0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 73  sqlite3_free(pTs
81be0 74 41 6c 6c 6f 63 29 3b 20 20 20 20 20 20 20 20  tAlloc);        
81bf0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
81c00 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e  }.#else.  #defin
81c10 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54  e DO_OS_MALLOC_T
81c20 45 53 54 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  EST.#endif../*.*
81c30 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
81c40 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 6f 6e  routines are con
81c50 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72  venience wrapper
81c60 73 20 61 72 6f 75 6e 64 20 6d 65 74 68 6f 64 73  s around methods
81c70 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74  .** of the sqlit
81c80 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 20  e3_file object. 
81c90 20 54 68 69 73 20 69 73 20 6d 6f 73 74 6c 79 20   This is mostly 
81ca0 6a 75 73 74 20 73 79 6e 74 61 63 74 69 63 20 73  just syntactic s
81cb0 75 67 61 72 2e 20 41 6c 6c 0a 2a 2a 20 6f 66 20  ugar. All.** of 
81cc0 74 68 69 73 20 77 6f 75 6c 64 20 62 65 20 63 6f  this would be co
81cd0 6d 70 6c 65 74 65 6c 79 20 61 75 74 6f 6d 61 74  mpletely automat
81ce0 69 63 20 69 66 20 53 51 4c 69 74 65 20 77 65 72  ic if SQLite wer
81cf0 65 20 63 6f 64 65 64 20 75 73 69 6e 67 0a 2a 2a  e coded using.**
81d00 20 43 2b 2b 20 69 6e 73 74 65 61 64 20 6f 66 20   C++ instead of 
81d10 70 6c 61 69 6e 20 6f 6c 64 20 43 2e 0a 2a 2f 0a  plain old C..*/.
81d20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
81d30 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73  nt sqlite3OsClos
81d40 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
81d50 70 49 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  pId){.  int rc =
81d60 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
81d70 28 20 70 49 64 2d 3e 70 4d 65 74 68 6f 64 73 20  ( pId->pMethods 
81d80 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 49 64 2d  ){.    rc = pId-
81d90 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 43 6c 6f 73  >pMethods->xClos
81da0 65 28 70 49 64 29 3b 0a 20 20 20 20 70 49 64 2d  e(pId);.    pId-
81db0 3e 70 4d 65 74 68 6f 64 73 20 3d 20 30 3b 0a 20  >pMethods = 0;. 
81dc0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
81dd0 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
81de0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 65   int sqlite3OsRe
81df0 61 64 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ad(sqlite3_file 
81e00 2a 69 64 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c  *id, void *pBuf,
81e10 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66   int amt, i64 of
81e20 66 73 65 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d  fset){.  DO_OS_M
81e30 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65  ALLOC_TEST;.  re
81e40 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64  turn id->pMethod
81e50 73 2d 3e 78 52 65 61 64 28 69 64 2c 20 70 42 75  s->xRead(id, pBu
81e60 66 2c 20 61 6d 74 2c 20 6f 66 66 73 65 74 29 3b  f, amt, offset);
81e70 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
81e80 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 57  E int sqlite3OsW
81e90 72 69 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  rite(sqlite3_fil
81ea0 65 20 2a 69 64 2c 20 63 6f 6e 73 74 20 76 6f 69  e *id, const voi
81eb0 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 61 6d 74  d *pBuf, int amt
81ec0 2c 20 69 36 34 20 6f 66 66 73 65 74 29 7b 0a 20  , i64 offset){. 
81ed0 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45   DO_OS_MALLOC_TE
81ee0 53 54 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d  ST;.  return id-
81ef0 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 57 72 69 74  >pMethods->xWrit
81f00 65 28 69 64 2c 20 70 42 75 66 2c 20 61 6d 74 2c  e(id, pBuf, amt,
81f10 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 53 51 4c 49   offset);.}.SQLI
81f20 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
81f30 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65  qlite3OsTruncate
81f40 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
81f50 64 2c 20 69 36 34 20 73 69 7a 65 29 7b 0a 20 20  d, i64 size){.  
81f60 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68  return id->pMeth
81f70 6f 64 73 2d 3e 78 54 72 75 6e 63 61 74 65 28 69  ods->xTruncate(i
81f80 64 2c 20 73 69 7a 65 29 3b 0a 7d 0a 53 51 4c 49  d, size);.}.SQLI
81f90 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
81fa0 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 73 71 6c  qlite3OsSync(sql
81fb0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
81fc0 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 44 4f 5f  nt flags){.  DO_
81fd0 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a  OS_MALLOC_TEST;.
81fe0 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65    return id->pMe
81ff0 74 68 6f 64 73 2d 3e 78 53 79 6e 63 28 69 64 2c  thods->xSync(id,
82000 20 66 6c 61 67 73 29 3b 0a 7d 0a 53 51 4c 49 54   flags);.}.SQLIT
82010 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
82020 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
82030 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
82040 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20  , i64 *pSize){. 
82050 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45   DO_OS_MALLOC_TE
82060 53 54 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d  ST;.  return id-
82070 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65  >pMethods->xFile
82080 53 69 7a 65 28 69 64 2c 20 70 53 69 7a 65 29 3b  Size(id, pSize);
82090 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
820a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4c  E int sqlite3OsL
820b0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
820c0 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 54 79   *id, int lockTy
820d0 70 65 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c  pe){.  DO_OS_MAL
820e0 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 74 75  LOC_TEST;.  retu
820f0 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d  rn id->pMethods-
82100 3e 78 4c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b 54  >xLock(id, lockT
82110 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  ype);.}.SQLITE_P
82120 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
82130 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74  e3OsUnlock(sqlit
82140 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
82150 20 6c 6f 63 6b 54 79 70 65 29 7b 0a 20 20 72 65   lockType){.  re
82160 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64  turn id->pMethod
82170 73 2d 3e 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 6c  s->xUnlock(id, l
82180 6f 63 6b 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49  ockType);.}.SQLI
82190 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
821a0 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73  qlite3OsCheckRes
821b0 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65  ervedLock(sqlite
821c0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
821d0 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 44 4f 5f  *pResOut){.  DO_
821e0 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a  OS_MALLOC_TEST;.
821f0 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65    return id->pMe
82200 74 68 6f 64 73 2d 3e 78 43 68 65 63 6b 52 65 73  thods->xCheckRes
82210 65 72 76 65 64 4c 6f 63 6b 28 69 64 2c 20 70 52  ervedLock(id, pR
82220 65 73 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45  esOut);.}.SQLITE
82230 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
82240 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f  ite3OsFileContro
82250 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  l(sqlite3_file *
82260 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64  id, int op, void
82270 20 2a 70 41 72 67 29 7b 0a 20 20 72 65 74 75 72   *pArg){.  retur
82280 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e  n id->pMethods->
82290 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 69 64 2c  xFileControl(id,
822a0 20 6f 70 2c 20 70 41 72 67 29 3b 0a 7d 0a 53 51   op, pArg);.}.SQ
822b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
822c0 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72   sqlite3OsSector
822d0 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  Size(sqlite3_fil
822e0 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 28 2a  e *id){.  int (*
822f0 78 53 65 63 74 6f 72 53 69 7a 65 29 28 73 71 6c  xSectorSize)(sql
82300 69 74 65 33 5f 66 69 6c 65 2a 29 20 3d 20 69 64  ite3_file*) = id
82310 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 65 63  ->pMethods->xSec
82320 74 6f 72 53 69 7a 65 3b 0a 20 20 72 65 74 75 72  torSize;.  retur
82330 6e 20 28 78 53 65 63 74 6f 72 53 69 7a 65 20 3f  n (xSectorSize ?
82340 20 78 53 65 63 74 6f 72 53 69 7a 65 28 69 64 29   xSectorSize(id)
82350 20 3a 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c   : SQLITE_DEFAUL
82360 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 29 3b 0a  T_SECTOR_SIZE);.
82370 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
82380 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65   int sqlite3OsDe
82390 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
823a0 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ics(sqlite3_file
823b0 20 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20   *id){.  return 
823c0 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 44  id->pMethods->xD
823d0 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73  eviceCharacteris
823e0 74 69 63 73 28 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a  tics(id);.}../*.
823f0 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 72 6f 75  ** The next grou
82400 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 61 72  p of routines ar
82410 65 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72  e convenience wr
82420 61 70 70 65 72 73 20 61 72 6f 75 6e 64 20 74 68  appers around th
82430 65 0a 2a 2a 20 56 46 53 20 6d 65 74 68 6f 64 73  e.** VFS methods
82440 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
82450 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
82460 73 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33  sOpen(.  sqlite3
82470 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63  _vfs *pVfs, .  c
82480 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68  onst char *zPath
82490 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  , .  sqlite3_fil
824a0 65 20 2a 70 46 69 6c 65 2c 20 0a 20 20 69 6e 74  e *pFile, .  int
824b0 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e 74 20 2a   flags, .  int *
824c0 70 46 6c 61 67 73 4f 75 74 0a 29 7b 0a 20 20 44  pFlagsOut.){.  D
824d0 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54  O_OS_MALLOC_TEST
824e0 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d  ;.  return pVfs-
824f0 3e 78 4f 70 65 6e 28 70 56 66 73 2c 20 7a 50 61  >xOpen(pVfs, zPa
82500 74 68 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73  th, pFile, flags
82510 2c 20 70 46 6c 61 67 73 4f 75 74 29 3b 0a 7d 0a  , pFlagsOut);.}.
82520 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
82530 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65  nt sqlite3OsDele
82540 74 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  te(sqlite3_vfs *
82550 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72  pVfs, const char
82560 20 2a 7a 50 61 74 68 2c 20 69 6e 74 20 64 69 72   *zPath, int dir
82570 53 79 6e 63 29 7b 0a 20 20 72 65 74 75 72 6e 20  Sync){.  return 
82580 70 56 66 73 2d 3e 78 44 65 6c 65 74 65 28 70 56  pVfs->xDelete(pV
82590 66 73 2c 20 7a 50 61 74 68 2c 20 64 69 72 53 79  fs, zPath, dirSy
825a0 6e 63 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  nc);.}.SQLITE_PR
825b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
825c0 33 4f 73 41 63 63 65 73 73 28 0a 20 20 73 71 6c  3OsAccess(.  sql
825d0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
825e0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
825f0 50 61 74 68 2c 20 0a 20 20 69 6e 74 20 66 6c 61  Path, .  int fla
82600 67 73 2c 20 0a 20 20 69 6e 74 20 2a 70 52 65 73  gs, .  int *pRes
82610 4f 75 74 0a 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d  Out.){.  DO_OS_M
82620 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65  ALLOC_TEST;.  re
82630 74 75 72 6e 20 70 56 66 73 2d 3e 78 41 63 63 65  turn pVfs->xAcce
82640 73 73 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20  ss(pVfs, zPath, 
82650 66 6c 61 67 73 2c 20 70 52 65 73 4f 75 74 29 3b  flags, pResOut);
82660 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
82670 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46  E int sqlite3OsF
82680 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73  ullPathname(.  s
82690 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
826a0 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
826b0 2a 7a 50 61 74 68 2c 20 0a 20 20 69 6e 74 20 6e  *zPath, .  int n
826c0 50 61 74 68 4f 75 74 2c 20 0a 20 20 63 68 61 72  PathOut, .  char
826d0 20 2a 7a 50 61 74 68 4f 75 74 0a 29 7b 0a 20 20   *zPathOut.){.  
826e0 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 46 75  return pVfs->xFu
826f0 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c  llPathname(pVfs,
82700 20 7a 50 61 74 68 2c 20 6e 50 61 74 68 4f 75 74   zPath, nPathOut
82710 2c 20 7a 50 61 74 68 4f 75 74 29 3b 0a 7d 0a 23  , zPathOut);.}.#
82720 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
82730 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f  IT_LOAD_EXTENSIO
82740 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  N.SQLITE_PRIVATE
82750 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f 73   void *sqlite3Os
82760 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76  DlOpen(sqlite3_v
82770 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20  fs *pVfs, const 
82780 63 68 61 72 20 2a 7a 50 61 74 68 29 7b 0a 20 20  char *zPath){.  
82790 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 6c  return pVfs->xDl
827a0 4f 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68  Open(pVfs, zPath
827b0 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
827c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
827d0 4f 73 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65  OsDlError(sqlite
827e0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74  3_vfs *pVfs, int
827f0 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 42   nByte, char *zB
82800 75 66 4f 75 74 29 7b 0a 20 20 70 56 66 73 2d 3e  ufOut){.  pVfs->
82810 78 44 6c 45 72 72 6f 72 28 70 56 66 73 2c 20 6e  xDlError(pVfs, n
82820 42 79 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b 0a  Byte, zBufOut);.
82830 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
82840 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f 73   void *sqlite3Os
82850 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66  DlSym(sqlite3_vf
82860 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70  s *pVfs, void *p
82870 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68  Handle, const ch
82880 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 7b 0a 20 20  ar *zSymbol){.  
82890 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 6c  return pVfs->xDl
828a0 53 79 6d 28 70 56 66 73 2c 20 70 48 61 6e 64 6c  Sym(pVfs, pHandl
828b0 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a 7d 0a 53  e, zSymbol);.}.S
828c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
828d0 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c  id sqlite3OsDlCl
828e0 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ose(sqlite3_vfs 
828f0 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61  *pVfs, void *pHa
82900 6e 64 6c 65 29 7b 0a 20 20 70 56 66 73 2d 3e 78  ndle){.  pVfs->x
82910 44 6c 43 6c 6f 73 65 28 70 56 66 73 2c 20 70 48  DlClose(pVfs, pH
82920 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6e 64 69 66  andle);.}.#endif
82930 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
82940 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 2a  LOAD_EXTENSION *
82950 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
82960 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 61   int sqlite3OsRa
82970 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33  ndomness(sqlite3
82980 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20  _vfs *pVfs, int 
82990 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 42 75  nByte, char *zBu
829a0 66 4f 75 74 29 7b 0a 20 20 72 65 74 75 72 6e 20  fOut){.  return 
829b0 70 56 66 73 2d 3e 78 52 61 6e 64 6f 6d 6e 65 73  pVfs->xRandomnes
829c0 73 28 70 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a  s(pVfs, nByte, z
829d0 42 75 66 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54  BufOut);.}.SQLIT
829e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
829f0 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 73 71 6c  lite3OsSleep(sql
82a00 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
82a10 69 6e 74 20 6e 4d 69 63 72 6f 29 7b 0a 20 20 72  int nMicro){.  r
82a20 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 53 6c 65  eturn pVfs->xSle
82a30 65 70 28 70 56 66 73 2c 20 6e 4d 69 63 72 6f 29  ep(pVfs, nMicro)
82a40 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
82a50 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
82a60 43 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69  CurrentTime(sqli
82a70 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 64  te3_vfs *pVfs, d
82a80 6f 75 62 6c 65 20 2a 70 54 69 6d 65 4f 75 74 29  ouble *pTimeOut)
82a90 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d  {.  return pVfs-
82aa0 3e 78 43 75 72 72 65 6e 74 54 69 6d 65 28 70 56  >xCurrentTime(pV
82ab0 66 73 2c 20 70 54 69 6d 65 4f 75 74 29 3b 0a 7d  fs, pTimeOut);.}
82ac0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
82ad0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70   int sqlite3OsOp
82ae0 65 6e 4d 61 6c 6c 6f 63 28 0a 20 20 73 71 6c 69  enMalloc(.  sqli
82af0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a  te3_vfs *pVfs, .
82b00 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
82b10 69 6c 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f  ile, .  sqlite3_
82b20 66 69 6c 65 20 2a 2a 70 70 46 69 6c 65 2c 20 0a  file **ppFile, .
82b30 20 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 69    int flags,.  i
82b40 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 0a 29 7b  nt *pOutFlags.){
82b50 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
82b60 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 71 6c 69  TE_NOMEM;.  sqli
82b70 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 3b  te3_file *pFile;
82b80 0a 20 20 70 46 69 6c 65 20 3d 20 28 73 71 6c 69  .  pFile = (sqli
82b90 74 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74  te3_file *)sqlit
82ba0 65 33 4d 61 6c 6c 6f 63 28 70 56 66 73 2d 3e 73  e3Malloc(pVfs->s
82bb0 7a 4f 73 46 69 6c 65 29 3b 0a 20 20 69 66 28 20  zOsFile);.  if( 
82bc0 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 72 63 20  pFile ){.    rc 
82bd0 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28  = sqlite3OsOpen(
82be0 70 56 66 73 2c 20 7a 46 69 6c 65 2c 20 70 46 69  pVfs, zFile, pFi
82bf0 6c 65 2c 20 66 6c 61 67 73 2c 20 70 4f 75 74 46  le, flags, pOutF
82c00 6c 61 67 73 29 3b 0a 20 20 20 20 69 66 28 20 72  lags);.    if( r
82c10 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
82c20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
82c30 65 65 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d  ee(pFile);.    }
82c40 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a 70 70 46  else{.      *ppF
82c50 69 6c 65 20 3d 20 70 46 69 6c 65 3b 0a 20 20 20  ile = pFile;.   
82c60 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
82c70 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  rc;.}.SQLITE_PRI
82c80 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
82c90 4f 73 43 6c 6f 73 65 46 72 65 65 28 73 71 6c 69  OsCloseFree(sqli
82ca0 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 29  te3_file *pFile)
82cb0 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
82cc0 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74  ITE_OK;.  assert
82cd0 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 72 63 20  ( pFile );.  rc 
82ce0 3d 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65  = sqlite3OsClose
82cf0 28 70 46 69 6c 65 29 3b 0a 20 20 73 71 6c 69 74  (pFile);.  sqlit
82d00 65 33 5f 66 72 65 65 28 70 46 69 6c 65 29 3b 0a  e3_free(pFile);.
82d10 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
82d20 2f 2a 0a 2a 2a 20 54 68 65 20 6c 69 73 74 20 6f  /*.** The list o
82d30 66 20 61 6c 6c 20 72 65 67 69 73 74 65 72 65 64  f all registered
82d40 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74   VFS implementat
82d50 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ions..*/.static 
82d60 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 53 51  sqlite3_vfs * SQ
82d70 4c 49 54 45 5f 57 53 44 20 76 66 73 4c 69 73 74  LITE_WSD vfsList
82d80 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 76 66   = 0;.#define vf
82d90 73 4c 69 73 74 20 47 4c 4f 42 41 4c 28 73 71 6c  sList GLOBAL(sql
82da0 69 74 65 33 5f 76 66 73 20 2a 2c 20 76 66 73 4c  ite3_vfs *, vfsL
82db0 69 73 74 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61  ist)../*.** Loca
82dc0 74 65 20 61 20 56 46 53 20 62 79 20 6e 61 6d 65  te a VFS by name
82dd0 2e 20 20 49 66 20 6e 6f 20 6e 61 6d 65 20 69 73  .  If no name is
82de0 20 67 69 76 65 6e 2c 20 73 69 6d 70 6c 79 20 72   given, simply r
82df0 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 66 69 72  eturn the.** fir
82e00 73 74 20 56 46 53 20 6f 6e 20 74 68 65 20 6c 69  st VFS on the li
82e10 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  st..*/.SQLITE_AP
82e20 49 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 73  I sqlite3_vfs *s
82e30 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28  qlite3_vfs_find(
82e40 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73  const char *zVfs
82e50 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  ){.  sqlite3_vfs
82e60 20 2a 70 56 66 73 20 3d 20 30 3b 0a 23 69 66 20   *pVfs = 0;.#if 
82e70 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
82e80 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  E.  sqlite3_mute
82e90 78 20 2a 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66  x *mutex;.#endif
82ea0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
82eb0 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20  OMIT_AUTOINIT.  
82ec0 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33  int rc = sqlite3
82ed0 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20  _initialize();. 
82ee0 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
82ef0 20 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53   0;.#endif.#if S
82f00 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
82f10 0a 20 20 6d 75 74 65 78 20 3d 20 73 71 6c 69 74  .  mutex = sqlit
82f20 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
82f30 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
82f40 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 69 66  _MASTER);.#endif
82f50 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
82f60 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20  _enter(mutex);. 
82f70 20 66 6f 72 28 70 56 66 73 20 3d 20 76 66 73 4c   for(pVfs = vfsL
82f80 69 73 74 3b 20 70 56 66 73 3b 20 70 56 66 73 3d  ist; pVfs; pVfs=
82f90 70 56 66 73 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pVfs->pNext){.  
82fa0 20 20 69 66 28 20 7a 56 66 73 3d 3d 30 20 29 20    if( zVfs==0 ) 
82fb0 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73  break;.    if( s
82fc0 74 72 63 6d 70 28 7a 56 66 73 2c 20 70 56 66 73  trcmp(zVfs, pVfs
82fd0 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72  ->zName)==0 ) br
82fe0 65 61 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  eak;.  }.  sqlit
82ff0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
83000 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
83010 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55  pVfs;.}../*.** U
83020 6e 6c 69 6e 6b 20 61 20 56 46 53 20 66 72 6f 6d  nlink a VFS from
83030 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74   the linked list
83040 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
83050 76 66 73 55 6e 6c 69 6e 6b 28 73 71 6c 69 74 65  vfsUnlink(sqlite
83060 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 20 20  3_vfs *pVfs){.  
83070 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
83080 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74  mutex_held(sqlit
83090 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
830a0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
830b0 5f 4d 41 53 54 45 52 29 29 20 29 3b 0a 20 20 69  _MASTER)) );.  i
830c0 66 28 20 70 56 66 73 3d 3d 30 20 29 7b 0a 20 20  f( pVfs==0 ){.  
830d0 20 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 20 20    /* No-op */.  
830e0 7d 65 6c 73 65 20 69 66 28 20 76 66 73 4c 69 73  }else if( vfsLis
830f0 74 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 76  t==pVfs ){.    v
83100 66 73 4c 69 73 74 20 3d 20 70 56 66 73 2d 3e 70  fsList = pVfs->p
83110 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66  Next;.  }else if
83120 28 20 76 66 73 4c 69 73 74 20 29 7b 0a 20 20 20  ( vfsList ){.   
83130 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 20   sqlite3_vfs *p 
83140 3d 20 76 66 73 4c 69 73 74 3b 0a 20 20 20 20 77  = vfsList;.    w
83150 68 69 6c 65 28 20 70 2d 3e 70 4e 65 78 74 20 26  hile( p->pNext &
83160 26 20 70 2d 3e 70 4e 65 78 74 21 3d 70 56 66 73  & p->pNext!=pVfs
83170 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 2d   ){.      p = p-
83180 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20  >pNext;.    }.  
83190 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 3d 3d    if( p->pNext==
831a0 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20 70 2d  pVfs ){.      p-
831b0 3e 70 4e 65 78 74 20 3d 20 70 56 66 73 2d 3e 70  >pNext = pVfs->p
831c0 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Next;.    }.  }.
831d0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65  }../*.** Registe
831e0 72 20 61 20 56 46 53 20 77 69 74 68 20 74 68 65  r a VFS with the
831f0 20 73 79 73 74 65 6d 2e 20 20 49 74 20 69 73 20   system.  It is 
83200 68 61 72 6d 6c 65 73 73 20 74 6f 20 72 65 67 69  harmless to regi
83210 73 74 65 72 20 74 68 65 20 73 61 6d 65 0a 2a 2a  ster the same.**
83220 20 56 46 53 20 6d 75 6c 74 69 70 6c 65 20 74 69   VFS multiple ti
83230 6d 65 73 2e 20 20 54 68 65 20 6e 65 77 20 56 46  mes.  The new VF
83240 53 20 62 65 63 6f 6d 65 73 20 74 68 65 20 64 65  S becomes the de
83250 66 61 75 6c 74 20 69 66 20 6d 61 6b 65 44 66 6c  fault if makeDfl
83260 74 20 69 73 0a 2a 2a 20 74 72 75 65 2e 0a 2a 2f  t is.** true..*/
83270 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
83280 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69  sqlite3_vfs_regi
83290 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73  ster(sqlite3_vfs
832a0 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d 61 6b 65   *pVfs, int make
832b0 44 66 6c 74 29 7b 0a 20 20 73 71 6c 69 74 65 33  Dflt){.  sqlite3
832c0 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20  _mutex *mutex = 
832d0 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  0;.#ifndef SQLIT
832e0 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a  E_OMIT_AUTOINIT.
832f0 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74    int rc = sqlit
83300 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b  e3_initialize();
83310 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75  .  if( rc ) retu
83320 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20 20  rn rc;.#endif.  
83330 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d  mutex = sqlite3M
83340 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
83350 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
83360 53 54 45 52 29 3b 0a 20 20 73 71 6c 69 74 65 33  STER);.  sqlite3
83370 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74  _mutex_enter(mut
83380 65 78 29 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b  ex);.  vfsUnlink
83390 28 70 56 66 73 29 3b 0a 20 20 69 66 28 20 6d 61  (pVfs);.  if( ma
833a0 6b 65 44 66 6c 74 20 7c 7c 20 76 66 73 4c 69 73  keDflt || vfsLis
833b0 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 56 66 73  t==0 ){.    pVfs
833c0 2d 3e 70 4e 65 78 74 20 3d 20 76 66 73 4c 69 73  ->pNext = vfsLis
833d0 74 3b 0a 20 20 20 20 76 66 73 4c 69 73 74 20 3d  t;.    vfsList =
833e0 20 70 56 66 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a   pVfs;.  }else{.
833f0 20 20 20 20 70 56 66 73 2d 3e 70 4e 65 78 74 20      pVfs->pNext 
83400 3d 20 76 66 73 4c 69 73 74 2d 3e 70 4e 65 78 74  = vfsList->pNext
83410 3b 0a 20 20 20 20 76 66 73 4c 69 73 74 2d 3e 70  ;.    vfsList->p
83420 4e 65 78 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d  Next = pVfs;.  }
83430 0a 20 20 61 73 73 65 72 74 28 76 66 73 4c 69 73  .  assert(vfsLis
83440 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  t);.  sqlite3_mu
83450 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29  tex_leave(mutex)
83460 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
83470 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55  E_OK;.}../*.** U
83480 6e 72 65 67 69 73 74 65 72 20 61 20 56 46 53 20  nregister a VFS 
83490 73 6f 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f  so that it is no
834a0 20 6c 6f 6e 67 65 72 20 61 63 63 65 73 73 69 62   longer accessib
834b0 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  le..*/.SQLITE_AP
834c0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66  I int sqlite3_vf
834d0 73 5f 75 6e 72 65 67 69 73 74 65 72 28 73 71 6c  s_unregister(sql
834e0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 29 7b  ite3_vfs *pVfs){
834f0 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
83500 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33  ADSAFE.  sqlite3
83510 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20  _mutex *mutex = 
83520 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
83530 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
83540 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23  TATIC_MASTER);.#
83550 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f  endif.  sqlite3_
83560 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65  mutex_enter(mute
83570 78 29 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28  x);.  vfsUnlink(
83580 70 56 66 73 29 3b 0a 20 20 73 71 6c 69 74 65 33  pVfs);.  sqlite3
83590 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74  _mutex_leave(mut
835a0 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ex);.  return SQ
835b0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a  LITE_OK;.}../***
835c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
835d0 6f 66 20 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  of os.c ********
835e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
835f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
83610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
83620 6e 20 66 69 6c 65 20 66 61 75 6c 74 2e 63 20 2a  n file fault.c *
83630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
83660 2a 20 32 30 30 38 20 4a 61 6e 20 32 32 0a 2a 2a  * 2008 Jan 22.**
83670 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
83680 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
83690 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
836a0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
836b0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
836c0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
836d0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
836e0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
836f0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
83700 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
83710 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
83720 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
83730 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
83740 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
83750 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
83760 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
83770 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
83780 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
83790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
837a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
837b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
837c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
837d0 2a 2a 20 24 49 64 3a 20 66 61 75 6c 74 2e 63 2c  ** $Id: fault.c,
837e0 76 20 31 2e 31 31 20 32 30 30 38 2f 30 39 2f 30  v 1.11 2008/09/0
837f0 32 20 30 30 3a 35 32 3a 35 32 20 64 72 68 20 45  2 00:52:52 drh E
83800 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  xp $.*/../*.** T
83810 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
83820 73 20 63 6f 64 65 20 74 6f 20 73 75 70 70 6f 72  s code to suppor
83830 74 20 74 68 65 20 63 6f 6e 63 65 70 74 20 6f 66  t the concept of
83840 20 22 62 65 6e 69 67 6e 22 20 0a 2a 2a 20 6d 61   "benign" .** ma
83850 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 28 77  lloc failures (w
83860 68 65 6e 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28  hen the xMalloc(
83870 29 20 6f 72 20 78 52 65 61 6c 6c 6f 63 28 29 20  ) or xRealloc() 
83880 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 0a 2a 2a  method of the.**
83890 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74   sqlite3_mem_met
838a0 68 6f 64 73 20 73 74 72 75 63 74 75 72 65 20 66  hods structure f
838b0 61 69 6c 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65  ails to allocate
838c0 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f   a block of memo
838d0 72 79 0a 2a 2a 20 61 6e 64 20 72 65 74 75 72 6e  ry.** and return
838e0 73 20 30 29 2e 20 0a 2a 2a 0a 2a 2a 20 4d 6f 73  s 0). .**.** Mos
838f0 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  t malloc failure
83900 73 20 61 72 65 20 6e 6f 6e 2d 62 65 6e 69 67 6e  s are non-benign
83910 2e 20 41 66 74 65 72 20 74 68 65 79 20 6f 63 63  . After they occ
83920 75 72 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 61 62  ur, SQLite.** ab
83930 61 6e 64 6f 6e 73 20 74 68 65 20 63 75 72 72 65  andons the curre
83940 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e 64  nt operation and
83950 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f   returns an erro
83960 72 20 63 6f 64 65 20 28 75 73 75 61 6c 6c 79 0a  r code (usually.
83970 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29  ** SQLITE_NOMEM)
83980 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20 48 6f   to the user. Ho
83990 77 65 76 65 72 2c 20 73 6f 6d 65 74 69 6d 65 73  wever, sometimes
839a0 20 61 20 66 61 75 6c 74 20 69 73 20 6e 6f 74 20   a fault is not 
839b0 6e 65 63 65 73 73 61 72 69 6c 79 0a 2a 2a 20 66  necessarily.** f
839c0 61 74 61 6c 2e 20 46 6f 72 20 65 78 61 6d 70 6c  atal. For exampl
839d0 65 2c 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66  e, if a malloc f
839e0 61 69 6c 73 20 77 68 69 6c 65 20 72 65 73 69 7a  ails while resiz
839f0 69 6e 67 20 61 20 68 61 73 68 20 74 61 62 6c 65  ing a hash table
83a00 2c 20 74 68 69 73 20 0a 2a 2a 20 69 73 20 63 6f  , this .** is co
83a10 6d 70 6c 65 74 65 6c 79 20 72 65 63 6f 76 65 72  mpletely recover
83a20 61 62 6c 65 20 73 69 6d 70 6c 79 20 62 79 20 6e  able simply by n
83a30 6f 74 20 63 61 72 72 79 69 6e 67 20 6f 75 74 20  ot carrying out 
83a40 74 68 65 20 72 65 73 69 7a 65 2e 20 54 68 65 20  the resize. The 
83a50 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20 77  .** hash table w
83a60 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20  ill continue to 
83a70 66 75 6e 63 74 69 6f 6e 20 6e 6f 72 6d 61 6c 6c  function normall
83a80 79 2e 20 20 53 6f 20 61 20 6d 61 6c 6c 6f 63 20  y.  So a malloc 
83a90 66 61 69 6c 75 72 65 20 0a 2a 2a 20 64 75 72 69  failure .** duri
83aa0 6e 67 20 61 20 68 61 73 68 20 74 61 62 6c 65 20  ng a hash table 
83ab0 72 65 73 69 7a 65 20 69 73 20 61 20 62 65 6e 69  resize is a beni
83ac0 67 6e 20 66 61 75 6c 74 2e 0a 2a 2f 0a 0a 0a 23  gn fault..*/...#
83ad0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
83ae0 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a  IT_BUILTIN_TEST.
83af0 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20 76 61  ./*.** Global va
83b00 72 69 61 62 6c 65 73 2e 0a 2a 2f 0a 74 79 70 65  riables..*/.type
83b10 64 65 66 20 73 74 72 75 63 74 20 42 65 6e 69 67  def struct Benig
83b20 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 42 65 6e  nMallocHooks Ben
83b30 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 3b 0a  ignMallocHooks;.
83b40 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53  static SQLITE_WS
83b50 44 20 73 74 72 75 63 74 20 42 65 6e 69 67 6e 4d  D struct BenignM
83b60 61 6c 6c 6f 63 48 6f 6f 6b 73 20 7b 0a 20 20 76  allocHooks {.  v
83b70 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 42 65 67  oid (*xBenignBeg
83b80 69 6e 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69  in)(void);.  voi
83b90 64 20 28 2a 78 42 65 6e 69 67 6e 45 6e 64 29 28  d (*xBenignEnd)(
83ba0 76 6f 69 64 29 3b 0a 7d 20 73 71 6c 69 74 65 33  void);.} sqlite3
83bb0 48 6f 6f 6b 73 20 3d 20 7b 20 30 2c 20 30 20 7d  Hooks = { 0, 0 }
83bc0 3b 0a 0a 2f 2a 20 54 68 65 20 22 77 73 64 48 6f  ;../* The "wsdHo
83bd0 6f 6b 73 22 20 6d 61 63 72 6f 20 77 69 6c 6c 20  oks" macro will 
83be0 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 20 61  resolve to the a
83bf0 70 70 72 6f 70 72 69 61 74 65 20 42 65 6e 69 67  ppropriate Benig
83c00 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 0a 2a 2a 20  nMallocHooks.** 
83c10 73 74 72 75 63 74 75 72 65 2e 20 20 49 66 20 77  structure.  If w
83c20 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64  ritable static d
83c30 61 74 61 20 69 73 20 75 6e 73 75 70 70 6f 72 74  ata is unsupport
83c40 65 64 20 6f 6e 20 74 68 65 20 74 61 72 67 65 74  ed on the target
83c50 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20  ,.** we have to 
83c60 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 65  locate the state
83c70 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74   vector at run-t
83c80 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72  ime.  In the mor
83c90 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65  e common.** case
83ca0 20 77 68 65 72 65 20 77 72 69 74 61 62 6c 65 20   where writable 
83cb0 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 73  static data is s
83cc0 75 70 70 6f 72 74 65 64 2c 20 77 73 64 48 6f 6f  upported, wsdHoo
83cd0 6b 73 20 63 61 6e 20 72 65 66 65 72 20 64 69 72  ks can refer dir
83ce0 65 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 65 20  ectly.** to the 
83cf0 22 73 71 6c 69 74 65 33 48 6f 6f 6b 73 22 20 73  "sqlite3Hooks" s
83d00 74 61 74 65 20 76 65 63 74 6f 72 20 64 65 63 6c  tate vector decl
83d10 61 72 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23  ared above..*/.#
83d20 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
83d30 54 5f 57 53 44 0a 23 20 64 65 66 69 6e 65 20 77  T_WSD.# define w
83d40 73 64 48 6f 6f 6b 73 49 6e 69 74 20 5c 0a 20 20  sdHooksInit \.  
83d50 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b  BenignMallocHook
83d60 73 20 2a 78 20 3d 20 26 47 4c 4f 42 41 4c 28 42  s *x = &GLOBAL(B
83d70 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73  enignMallocHooks
83d80 2c 73 71 6c 69 74 65 33 48 6f 6f 6b 73 29 0a 23  ,sqlite3Hooks).#
83d90 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73   define wsdHooks
83da0 20 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65   x[0].#else.# de
83db0 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 49 6e 69  fine wsdHooksIni
83dc0 74 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f  t.# define wsdHo
83dd0 6f 6b 73 20 73 71 6c 69 74 65 33 48 6f 6f 6b 73  oks sqlite3Hooks
83de0 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
83df0 52 65 67 69 73 74 65 72 20 68 6f 6f 6b 73 20 74  Register hooks t
83e00 6f 20 63 61 6c 6c 20 77 68 65 6e 20 73 71 6c 69  o call when sqli
83e10 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61  te3BeginBenignMa
83e20 6c 6c 6f 63 28 29 20 61 6e 64 0a 2a 2a 20 73 71  lloc() and.** sq
83e30 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61  lite3EndBenignMa
83e40 6c 6c 6f 63 28 29 20 61 72 65 20 63 61 6c 6c 65  lloc() are calle
83e50 64 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e  d, respectively.
83e60 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
83e70 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
83e80 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73  enignMallocHooks
83e90 28 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69  (.  void (*xBeni
83ea0 67 6e 42 65 67 69 6e 29 28 76 6f 69 64 29 2c 0a  gnBegin)(void),.
83eb0 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e    void (*xBenign
83ec0 45 6e 64 29 28 76 6f 69 64 29 0a 29 7b 0a 20 20  End)(void).){.  
83ed0 77 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20  wsdHooksInit;.  
83ee0 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e  wsdHooks.xBenign
83ef0 42 65 67 69 6e 20 3d 20 78 42 65 6e 69 67 6e 42  Begin = xBenignB
83f00 65 67 69 6e 3b 0a 20 20 77 73 64 48 6f 6f 6b 73  egin;.  wsdHooks
83f10 2e 78 42 65 6e 69 67 6e 45 6e 64 20 3d 20 78 42  .xBenignEnd = xB
83f20 65 6e 69 67 6e 45 6e 64 3b 0a 7d 0a 0a 2f 2a 0a  enignEnd;.}../*.
83f30 2a 2a 20 54 68 69 73 20 28 73 71 6c 69 74 65 33  ** This (sqlite3
83f40 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  EndBenignMalloc(
83f50 29 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  )) is called by 
83f60 53 51 4c 69 74 65 20 63 6f 64 65 20 74 6f 20 69  SQLite code to i
83f70 6e 64 69 63 61 74 65 20 74 68 61 74 0a 2a 2a 20  ndicate that.** 
83f80 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f  subsequent mallo
83f90 63 20 66 61 69 6c 75 72 65 73 20 61 72 65 20 62  c failures are b
83fa0 65 6e 69 67 6e 2e 20 41 20 63 61 6c 6c 20 74 6f  enign. A call to
83fb0 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67   sqlite3EndBenig
83fc0 6e 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 69 6e 64  nMalloc().** ind
83fd0 69 63 61 74 65 73 20 74 68 61 74 20 73 75 62 73  icates that subs
83fe0 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20 66 61  equent malloc fa
83ff0 69 6c 75 72 65 73 20 61 72 65 20 6e 6f 6e 2d 62  ilures are non-b
84000 65 6e 69 67 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  enign..*/.SQLITE
84010 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
84020 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e  lite3BeginBenign
84030 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 7b 0a 20 20  Malloc(void){.  
84040 77 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20  wsdHooksInit;.  
84050 69 66 28 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65  if( wsdHooks.xBe
84060 6e 69 67 6e 42 65 67 69 6e 20 29 7b 0a 20 20 20  nignBegin ){.   
84070 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67   wsdHooks.xBenig
84080 6e 42 65 67 69 6e 28 29 3b 0a 20 20 7d 0a 7d 0a  nBegin();.  }.}.
84090 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
840a0 6f 69 64 20 73 71 6c 69 74 65 33 45 6e 64 42 65  oid sqlite3EndBe
840b0 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29  nignMalloc(void)
840c0 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74  {.  wsdHooksInit
840d0 3b 0a 20 20 69 66 28 20 77 73 64 48 6f 6f 6b 73  ;.  if( wsdHooks
840e0 2e 78 42 65 6e 69 67 6e 45 6e 64 20 29 7b 0a 20  .xBenignEnd ){. 
840f0 20 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e     wsdHooks.xBen
84100 69 67 6e 45 6e 64 28 29 3b 0a 20 20 7d 0a 7d 0a  ignEnd();.  }.}.
84110 0a 23 65 6e 64 69 66 20 20 20 2f 2a 20 23 69 66  .#endif   /* #if
84120 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
84130 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a 2f  _BUILTIN_TEST */
84140 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
84150 2a 20 45 6e 64 20 6f 66 20 66 61 75 6c 74 2e 63  * End of fault.c
84160 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
84170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84190 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
841a0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d  * Begin file mem
841b0 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  0.c ************
841c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
841d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
841e0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74  /./*.** 2008 Oct
841f0 6f 62 65 72 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68  ober 28.**.** Th
84200 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
84210 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
84220 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
84230 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
84240 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
84250 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
84260 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
84270 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
84280 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
84290 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
842a0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
842b0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
842c0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
842d0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
842e0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
842f0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
84300 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
84310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84350 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
84360 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
84370 61 20 6e 6f 2d 6f 70 20 6d 65 6d 6f 72 79 20 61  a no-op memory a
84380 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72  llocation driver
84390 73 20 66 6f 72 20 75 73 65 20 77 68 65 6e 0a 2a  s for use when.*
843a0 2a 20 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41  * SQLITE_ZERO_MA
843b0 4c 4c 4f 43 20 69 73 20 64 65 66 69 6e 65 64 2e  LLOC is defined.
843c0 20 20 54 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e    The allocation
843d0 20 64 72 69 76 65 72 73 20 69 6d 70 6c 65 6d 65   drivers impleme
843e0 6e 74 65 64 0a 2a 2a 20 68 65 72 65 20 61 6c 77  nted.** here alw
843f0 61 79 73 20 66 61 69 6c 2e 20 20 53 51 4c 69 74  ays fail.  SQLit
84400 65 20 77 69 6c 6c 20 6e 6f 74 20 6f 70 65 72 61  e will not opera
84410 74 65 20 77 69 74 68 20 74 68 65 73 65 20 64 72  te with these dr
84420 69 76 65 72 73 2e 20 20 54 68 65 73 65 0a 2a 2a  ivers.  These.**
84430 20 61 72 65 20 6d 65 72 65 6c 79 20 70 6c 61 63   are merely plac
84440 65 68 6f 6c 64 65 72 73 2e 20 20 52 65 61 6c 20  eholders.  Real 
84450 64 72 69 76 65 72 73 20 6d 75 73 74 20 62 65 20  drivers must be 
84460 73 75 62 73 74 69 74 75 74 65 64 20 75 73 69 6e  substituted usin
84470 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e  g.** sqlite3_con
84480 66 69 67 28 29 20 62 65 66 6f 72 65 20 53 51 4c  fig() before SQL
84490 69 74 65 20 77 69 6c 6c 20 6f 70 65 72 61 74 65  ite will operate
844a0 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d  ..**.** $Id: mem
844b0 30 2e 63 2c 76 20 31 2e 31 20 32 30 30 38 2f 31  0.c,v 1.1 2008/1
844c0 30 2f 32 38 20 31 38 3a 35 38 3a 32 30 20 64 72  0/28 18:58:20 dr
844d0 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  h Exp $.*/../*.*
844e0 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f  * This version o
844f0 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  f the memory all
84500 6f 63 61 74 6f 72 20 69 73 20 74 68 65 20 64 65  ocator is the de
84510 66 61 75 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a  fault.  It is.**
84520 20 75 73 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74   used when no ot
84530 68 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  her memory alloc
84540 61 74 6f 72 20 69 73 20 73 70 65 63 69 66 69 65  ator is specifie
84550 64 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d  d using compile-
84560 74 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a  time.** macros..
84570 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
84580 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a  _ZERO_MALLOC../*
84590 0a 2a 2a 20 4e 6f 2d 6f 70 20 76 65 72 73 69 6f  .** No-op versio
845a0 6e 73 20 6f 66 20 61 6c 6c 20 6d 65 6d 6f 72 79  ns of all memory
845b0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74   allocation rout
845c0 69 6e 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ines.*/.static v
845d0 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d  oid *sqlite3MemM
845e0 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29  alloc(int nByte)
845f0 7b 20 72 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74  { return 0; }.st
84600 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65  atic void sqlite
84610 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70  3MemFree(void *p
84620 50 72 69 6f 72 29 7b 20 72 65 74 75 72 6e 3b 20  Prior){ return; 
84630 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73  }.static void *s
84640 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63  qlite3MemRealloc
84650 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69  (void *pPrior, i
84660 6e 74 20 6e 42 79 74 65 29 7b 20 72 65 74 75 72  nt nByte){ retur
84670 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e  n 0; }.static in
84680 74 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65  t sqlite3MemSize
84690 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 20  (void *pPrior){ 
846a0 72 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74  return 0; }.stat
846b0 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65  ic int sqlite3Me
846c0 6d 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b  mRoundup(int n){
846d0 20 72 65 74 75 72 6e 20 6e 3b 20 7d 0a 73 74 61   return n; }.sta
846e0 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  tic int sqlite3M
846f0 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74  emInit(void *Not
84700 55 73 65 64 29 7b 20 72 65 74 75 72 6e 20 53 51  Used){ return SQ
84710 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69  LITE_OK; }.stati
84720 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65  c void sqlite3Me
84730 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a  mShutdown(void *
84740 4e 6f 74 55 73 65 64 29 7b 20 72 65 74 75 72 6e  NotUsed){ return
84750 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ; }../*.** This 
84760 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f  routine is the o
84770 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74  nly routine in t
84780 68 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78  his file with ex
84790 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a  ternal linkage..
847a0 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74  **.** Populate t
847b0 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d  he low-level mem
847c0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  ory allocation f
847d0 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73  unction pointers
847e0 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c   in.** sqlite3Gl
847f0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74  obalConfig.m wit
84800 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68  h pointers to th
84810 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68  e routines in th
84820 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49  is file..*/.SQLI
84830 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
84840 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66  sqlite3MemSetDef
84850 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 74  ault(void){.  st
84860 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
84870 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 64  e3_mem_methods d
84880 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20  efaultMethods = 
84890 7b 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65  {.     sqlite3Me
848a0 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71  mMalloc,.     sq
848b0 6c 69 74 65 33 4d 65 6d 46 72 65 65 2c 0a 20 20  lite3MemFree,.  
848c0 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 61     sqlite3MemRea
848d0 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74  lloc,.     sqlit
848e0 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 20 20  e3MemSize,.     
848f0 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75  sqlite3MemRoundu
84900 70 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d  p,.     sqlite3M
84910 65 6d 49 6e 69 74 2c 0a 20 20 20 20 20 73 71 6c  emInit,.     sql
84920 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 2c  ite3MemShutdown,
84930 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 73  .     0.  };.  s
84940 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51  qlite3_config(SQ
84950 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c  LITE_CONFIG_MALL
84960 4f 43 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 68  OC, &defaultMeth
84970 6f 64 73 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  ods);.}..#endif 
84980 2f 2a 20 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d  /* SQLITE_ZERO_M
84990 41 4c 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  ALLOC */../*****
849a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
849b0 20 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   mem0.c ********
849c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
849d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
849e0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
849f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
84a00 66 69 6c 65 20 6d 65 6d 31 2e 63 20 2a 2a 2a 2a  file mem1.c ****
84a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84a30 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
84a40 32 30 30 37 20 41 75 67 75 73 74 20 31 34 0a 2a  2007 August 14.*
84a50 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
84a60 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
84a70 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
84a80 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
84a90 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
84aa0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
84ab0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
84ac0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
84ad0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
84ae0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
84af0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
84b00 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
84b10 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
84b20 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
84b30 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
84b40 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
84b50 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
84b60 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
84b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
84bb0 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
84bc0 6e 74 61 69 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c  ntains low-level
84bd0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
84be0 6f 6e 20 64 72 69 76 65 72 73 20 66 6f 72 20 77  on drivers for w
84bf0 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69  hen.** SQLite wi
84c00 6c 6c 20 75 73 65 20 74 68 65 20 73 74 61 6e 64  ll use the stand
84c10 61 72 64 20 43 2d 6c 69 62 72 61 72 79 20 6d 61  ard C-library ma
84c20 6c 6c 6f 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65  lloc/realloc/fre
84c30 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74  e interface.** t
84c40 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d  o obtain the mem
84c50 6f 72 79 20 69 74 20 6e 65 65 64 73 2e 0a 2a 2a  ory it needs..**
84c60 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
84c70 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74  ntains implement
84c80 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 6c 6f  ations of the lo
84c90 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61  w-level memory a
84ca0 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75  llocation.** rou
84cb0 74 69 6e 65 73 20 73 70 65 63 69 66 69 65 64 20  tines specified 
84cc0 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d  in the sqlite3_m
84cd0 65 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63  em_methods objec
84ce0 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65  t..**.** $Id: me
84cf0 6d 31 2e 63 2c 76 20 31 2e 32 37 20 32 30 30 38  m1.c,v 1.27 2008
84d00 2f 31 30 2f 32 38 20 31 38 3a 35 38 3a 32 30 20  /10/28 18:58:20 
84d10 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  drh Exp $.*/../*
84d20 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e  .** This version
84d30 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   of the memory a
84d40 6c 6c 6f 63 61 74 6f 72 20 69 73 20 74 68 65 20  llocator is the 
84d50 64 65 66 61 75 6c 74 2e 20 20 49 74 20 69 73 0a  default.  It is.
84d60 2a 2a 20 75 73 65 64 20 77 68 65 6e 20 6e 6f 20  ** used when no 
84d70 6f 74 68 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c  other memory all
84d80 6f 63 61 74 6f 72 20 69 73 20 73 70 65 63 69 66  ocator is specif
84d90 69 65 64 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c  ied using compil
84da0 65 2d 74 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73  e-time.** macros
84db0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
84dc0 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43  TE_SYSTEM_MALLOC
84dd0 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 6d 61 6c  ../*.** Like mal
84de0 6c 6f 63 28 29 2c 20 62 75 74 20 72 65 6d 65 6d  loc(), but remem
84df0 62 65 72 20 74 68 65 20 73 69 7a 65 20 6f 66 20  ber the size of 
84e00 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a  the allocation.*
84e10 2a 20 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e  * so that we can
84e20 20 66 69 6e 64 20 69 74 20 6c 61 74 65 72 20 75   find it later u
84e30 73 69 6e 67 20 73 71 6c 69 74 65 33 4d 65 6d 53  sing sqlite3MemS
84e40 69 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  ize()..**.** For
84e50 20 74 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20   this low-level 
84e60 72 6f 75 74 69 6e 65 2c 20 77 65 20 61 72 65 20  routine, we are 
84e70 67 75 61 72 61 6e 74 65 65 64 20 74 68 61 74 20  guaranteed that 
84e80 6e 42 79 74 65 3e 30 20 62 65 63 61 75 73 65 0a  nByte>0 because.
84e90 2a 2a 20 63 61 73 65 73 20 6f 66 20 6e 42 79 74  ** cases of nByt
84ea0 65 3c 3d 30 20 77 69 6c 6c 20 62 65 20 69 6e 74  e<=0 will be int
84eb0 65 72 63 65 70 74 65 64 20 61 6e 64 20 64 65 61  ercepted and dea
84ec0 6c 74 20 77 69 74 68 20 62 79 20 68 69 67 68 65  lt with by highe
84ed0 72 20 6c 65 76 65 6c 0a 2a 2a 20 72 6f 75 74 69  r level.** routi
84ee0 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  nes..*/.static v
84ef0 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d  oid *sqlite3MemM
84f00 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29  alloc(int nByte)
84f10 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  {.  sqlite3_int6
84f20 34 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20  4 *p;.  assert( 
84f30 6e 42 79 74 65 3e 30 20 29 3b 0a 20 20 6e 42 79  nByte>0 );.  nBy
84f40 74 65 20 3d 20 28 6e 42 79 74 65 2b 37 29 26 7e  te = (nByte+7)&~
84f50 37 3b 0a 20 20 70 20 3d 20 6d 61 6c 6c 6f 63 28  7;.  p = malloc(
84f60 20 6e 42 79 74 65 2b 38 20 29 3b 0a 20 20 69 66   nByte+8 );.  if
84f70 28 20 70 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20  ( p ){.    p[0] 
84f80 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 2b 2b  = nByte;.    p++
84f90 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  ;.  }.  return (
84fa0 76 6f 69 64 20 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a  void *)p;.}../*.
84fb0 2a 2a 20 4c 69 6b 65 20 66 72 65 65 28 29 20 62  ** Like free() b
84fc0 75 74 20 77 6f 72 6b 73 20 66 6f 72 20 61 6c 6c  ut works for all
84fd0 6f 63 61 74 69 6f 6e 73 20 6f 62 74 61 69 6e 65  ocations obtaine
84fe0 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 65  d from sqlite3Me
84ff0 6d 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20  mMalloc().** or 
85000 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f  sqlite3MemReallo
85010 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74  c()..**.** For t
85020 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f  his low-level ro
85030 75 74 69 6e 65 2c 20 77 65 20 61 6c 72 65 61 64  utine, we alread
85040 79 20 6b 6e 6f 77 20 74 68 61 74 20 70 50 72 69  y know that pPri
85050 6f 72 21 3d 30 20 73 69 6e 63 65 0a 2a 2a 20 63  or!=0 since.** c
85060 61 73 65 73 20 77 68 65 72 65 20 70 50 72 69 6f  ases where pPrio
85070 72 3d 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 62  r==0 will have b
85080 65 65 6e 20 69 6e 74 65 63 65 70 74 65 64 20 61  een intecepted a
85090 6e 64 20 64 65 61 6c 74 20 77 69 74 68 0a 2a 2a  nd dealt with.**
850a0 20 62 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c   by higher-level
850b0 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74   routines..*/.st
850c0 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65  atic void sqlite
850d0 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70  3MemFree(void *p
850e0 50 72 69 6f 72 29 7b 0a 20 20 73 71 6c 69 74 65  Prior){.  sqlite
850f0 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20 28 73 71  3_int64 *p = (sq
85100 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72  lite3_int64*)pPr
85110 69 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ior;.  assert( p
85120 50 72 69 6f 72 21 3d 30 20 29 3b 0a 20 20 70 2d  Prior!=0 );.  p-
85130 2d 3b 0a 20 20 66 72 65 65 28 70 29 3b 0a 7d 0a  -;.  free(p);.}.
85140 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 72 65 61 6c  ./*.** Like real
85150 6c 6f 63 28 29 2e 20 20 52 65 73 69 7a 65 20 61  loc().  Resize a
85160 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65  n allocation pre
85170 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64  viously obtained
85180 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33   from.** sqlite3
85190 4d 65 6d 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a  MemMalloc()..**.
851a0 2a 2a 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d  ** For this low-
851b0 6c 65 76 65 6c 20 69 6e 74 65 72 66 61 63 65 2c  level interface,
851c0 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 70 50   we know that pP
851d0 72 69 6f 72 21 3d 30 2e 20 20 43 61 73 65 73 20  rior!=0.  Cases 
851e0 77 68 65 72 65 0a 2a 2a 20 70 50 72 69 6f 72 3d  where.** pPrior=
851f0 3d 30 20 77 68 69 6c 65 20 68 61 76 65 20 62 65  =0 while have be
85200 65 6e 20 69 6e 74 65 72 63 65 70 74 65 64 20 62  en intercepted b
85210 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 72  y higher-level r
85220 6f 75 74 69 6e 65 20 61 6e 64 0a 2a 2a 20 72 65  outine and.** re
85230 64 69 72 65 63 74 65 64 20 74 6f 20 78 4d 61 6c  directed to xMal
85240 6c 6f 63 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c  loc.  Similarly,
85250 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 6e 42   we know that nB
85260 79 74 65 3e 30 20 62 65 63 61 75 73 65 73 0a 2a  yte>0 becauses.*
85270 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 6e 42  * cases where nB
85280 79 74 65 3c 3d 30 20 77 69 6c 6c 20 68 61 76 65  yte<=0 will have
85290 20 62 65 65 6e 20 69 6e 74 65 72 63 65 70 74 65   been intercepte
852a0 64 20 62 79 20 68 69 67 68 65 72 2d 6c 65 76 65  d by higher-leve
852b0 6c 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 61 6e  l.** routines an
852c0 64 20 72 65 64 69 72 65 63 74 65 64 20 74 6f 20  d redirected to 
852d0 78 46 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  xFree..*/.static
852e0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65   void *sqlite3Me
852f0 6d 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70  mRealloc(void *p
85300 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65  Prior, int nByte
85310 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  ){.  sqlite3_int
85320 36 34 20 2a 70 20 3d 20 28 73 71 6c 69 74 65 33  64 *p = (sqlite3
85330 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a  _int64*)pPrior;.
85340 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72    assert( pPrior
85350 21 3d 30 20 26 26 20 6e 42 79 74 65 3e 30 20 29  !=0 && nByte>0 )
85360 3b 0a 20 20 6e 42 79 74 65 20 3d 20 28 6e 42 79  ;.  nByte = (nBy
85370 74 65 2b 37 29 26 7e 37 3b 0a 20 20 70 20 3d 20  te+7)&~7;.  p = 
85380 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29  (sqlite3_int64*)
85390 70 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20  pPrior;.  p--;. 
853a0 20 70 20 3d 20 72 65 61 6c 6c 6f 63 28 70 2c 20   p = realloc(p, 
853b0 6e 42 79 74 65 2b 38 20 29 3b 0a 20 20 69 66 28  nByte+8 );.  if(
853c0 20 70 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d   p ){.    p[0] =
853d0 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b   nByte;.    p++;
853e0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76  .  }.  return (v
853f0 6f 69 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  oid*)p;.}../*.**
85400 20 52 65 70 6f 72 74 20 74 68 65 20 61 6c 6c 6f   Report the allo
85410 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 20  cated size of a 
85420 70 72 69 6f 72 20 72 65 74 75 72 6e 20 66 72 6f  prior return fro
85430 6d 20 78 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f  m xMalloc().** o
85440 72 20 78 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2f  r xRealloc()..*/
85450 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
85460 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20  te3MemSize(void 
85470 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 71 6c 69  *pPrior){.  sqli
85480 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20  te3_int64 *p;.  
85490 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 20  if( pPrior==0 ) 
854a0 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20  return 0;.  p = 
854b0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29  (sqlite3_int64*)
854c0 70 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20  pPrior;.  p--;. 
854d0 20 72 65 74 75 72 6e 20 70 5b 30 5d 3b 0a 7d 0a   return p[0];.}.
854e0 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20  ./*.** Round up 
854f0 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74  a request size t
85500 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64  o the next valid
85510 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65   allocation size
85520 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
85530 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75  sqlite3MemRoundu
85540 70 28 69 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75  p(int n){.  retu
85550 72 6e 20 28 6e 2b 37 29 20 26 20 7e 37 3b 0a 7d  rn (n+7) & ~7;.}
85560 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  ../*.** Initiali
85570 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a  ze this module..
85580 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  */.static int sq
85590 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28 76 6f 69  lite3MemInit(voi
855a0 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 72  d *NotUsed){.  r
855b0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
855c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74  .}../*.** Deinit
855d0 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75  ialize this modu
855e0 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  le..*/.static vo
855f0 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75  id sqlite3MemShu
85600 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55  tdown(void *NotU
85610 73 65 64 29 7b 0a 20 20 72 65 74 75 72 6e 3b 0a  sed){.  return;.
85620 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
85630 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c  utine is the onl
85640 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69  y routine in thi
85650 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 65  s file with exte
85660 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a  rnal linkage..**
85670 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65  .** Populate the
85680 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72   low-level memor
85690 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e  y allocation fun
856a0 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69  ction pointers i
856b0 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62  n.** sqlite3Glob
856c0 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20  alConfig.m with 
856d0 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20  pointers to the 
856e0 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73  routines in this
856f0 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   file..*/.SQLITE
85700 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
85710 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75  lite3MemSetDefau
85720 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74  lt(void){.  stat
85730 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
85740 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66  _mem_methods def
85750 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a  aultMethods = {.
85760 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d       sqlite3MemM
85770 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69  alloc,.     sqli
85780 74 65 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20  te3MemFree,.    
85790 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c   sqlite3MemReall
857a0 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  oc,.     sqlite3
857b0 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 71  MemSize,.     sq
857c0 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c  lite3MemRoundup,
857d0 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  .     sqlite3Mem
857e0 49 6e 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 74  Init,.     sqlit
857f0 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20  e3MemShutdown,. 
85800 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c      0.  };.  sql
85810 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49  ite3_config(SQLI
85820 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43  TE_CONFIG_MALLOC
85830 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64  , &defaultMethod
85840 73 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a  s);.}..#endif /*
85850 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d   SQLITE_SYSTEM_M
85860 41 4c 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  ALLOC */../*****
85870 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
85880 20 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   mem1.c ********
85890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
858a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
858b0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
858c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
858d0 66 69 6c 65 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a  file mem2.c ****
858e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
858f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85900 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
85910 32 30 30 37 20 41 75 67 75 73 74 20 31 35 0a 2a  2007 August 15.*
85920 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
85930 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
85940 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
85950 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
85960 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
85970 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
85980 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
85990 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
859a0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
859b0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
859c0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
859d0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
859e0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
859f0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
85a00 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
85a10 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
85a20 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
85a30 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
85a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
85a80 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
85a90 6e 74 61 69 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c  ntains low-level
85aa0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
85ab0 6f 6e 20 64 72 69 76 65 72 73 20 66 6f 72 20 77  on drivers for w
85ac0 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69  hen.** SQLite wi
85ad0 6c 6c 20 75 73 65 20 74 68 65 20 73 74 61 6e 64  ll use the stand
85ae0 61 72 64 20 43 2d 6c 69 62 72 61 72 79 20 6d 61  ard C-library ma
85af0 6c 6c 6f 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65  lloc/realloc/fre
85b00 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74  e interface.** t
85b10 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d  o obtain the mem
85b20 6f 72 79 20 69 74 20 6e 65 65 64 73 20 77 68 69  ory it needs whi
85b30 6c 65 20 61 64 64 69 6e 67 20 6c 6f 74 73 20 6f  le adding lots o
85b40 66 20 61 64 64 69 74 69 6f 6e 61 6c 20 64 65 62  f additional deb
85b50 75 67 67 69 6e 67 0a 2a 2a 20 69 6e 66 6f 72 6d  ugging.** inform
85b60 61 74 69 6f 6e 20 74 6f 20 65 61 63 68 20 61 6c  ation to each al
85b70 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 6f 72 64 65  location in orde
85b80 72 20 74 6f 20 68 65 6c 70 20 64 65 74 65 63 74  r to help detect
85b90 20 61 6e 64 20 66 69 78 20 6d 65 6d 6f 72 79 0a   and fix memory.
85ba0 2a 2a 20 6c 65 61 6b 73 20 61 6e 64 20 6d 65 6d  ** leaks and mem
85bb0 6f 72 79 20 75 73 61 67 65 20 65 72 72 6f 72 73  ory usage errors
85bc0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ..**.** This fil
85bd0 65 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65  e contains imple
85be0 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68  mentations of th
85bf0 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f  e low-level memo
85c00 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a  ry allocation.**
85c10 20 72 6f 75 74 69 6e 65 73 20 73 70 65 63 69 66   routines specif
85c20 69 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74  ied in the sqlit
85c30 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6f  e3_mem_methods o
85c40 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  bject..**.** $Id
85c50 3a 20 6d 65 6d 32 2e 63 2c 76 20 31 2e 34 30 20  : mem2.c,v 1.40 
85c60 32 30 30 38 2f 31 30 2f 32 38 20 31 38 3a 35 38  2008/10/28 18:58
85c70 3a 32 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :20 drh Exp $.*/
85c80 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72  ../*.** This ver
85c90 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f  sion of the memo
85ca0 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20  ry allocator is 
85cb0 75 73 65 64 20 6f 6e 6c 79 20 69 66 20 74 68 65  used only if the
85cc0 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45  .** SQLITE_MEMDE
85cd0 42 55 47 20 6d 61 63 72 6f 20 69 73 20 64 65 66  BUG macro is def
85ce0 69 6e 65 64 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ined.*/.#ifdef S
85cf0 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 0a 0a  QLITE_MEMDEBUG..
85d00 2f 2a 0a 2a 2a 20 54 68 65 20 62 61 63 6b 74 72  /*.** The backtr
85d10 61 63 65 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74  ace functionalit
85d20 79 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61  y is only availa
85d30 62 6c 65 20 77 69 74 68 20 47 4c 49 42 43 0a 2a  ble with GLIBC.*
85d40 2f 0a 23 69 66 64 65 66 20 5f 5f 47 4c 49 42 43  /.#ifdef __GLIBC
85d50 5f 5f 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 20  __.  extern int 
85d60 62 61 63 6b 74 72 61 63 65 28 76 6f 69 64 2a 2a  backtrace(void**
85d70 2c 69 6e 74 29 3b 0a 20 20 65 78 74 65 72 6e 20  ,int);.  extern 
85d80 76 6f 69 64 20 62 61 63 6b 74 72 61 63 65 5f 73  void backtrace_s
85d90 79 6d 62 6f 6c 73 5f 66 64 28 76 6f 69 64 2a 63  ymbols_fd(void*c
85da0 6f 6e 73 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a  onst*,int,int);.
85db0 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 62  #else.# define b
85dc0 61 63 6b 74 72 61 63 65 28 41 2c 42 29 20 31 0a  acktrace(A,B) 1.
85dd0 23 20 64 65 66 69 6e 65 20 62 61 63 6b 74 72 61  # define backtra
85de0 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 41 2c  ce_symbols_fd(A,
85df0 42 2c 43 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  B,C).#endif../*.
85e00 2a 2a 20 45 61 63 68 20 6d 65 6d 6f 72 79 20 61  ** Each memory a
85e10 6c 6c 6f 63 61 74 69 6f 6e 20 6c 6f 6f 6b 73 20  llocation looks 
85e20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a  like this:.**.**
85e30 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
85e40 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85e50 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85e60 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85e70 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 7c  ----------.**  |
85e80 20 54 69 74 6c 65 20 7c 20 20 62 61 63 6b 74 72   Title |  backtr
85e90 61 63 65 20 70 6f 69 6e 74 65 72 73 20 7c 20 20  ace pointers |  
85ea0 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 7c 20 20 61  MemBlockHdr |  a
85eb0 6c 6c 6f 63 61 74 69 6f 6e 20 7c 20 20 45 6e 64  llocation |  End
85ec0 47 75 61 72 64 20 7c 0a 2a 2a 20 20 2d 2d 2d 2d  Guard |.**  ----
85ed0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85ee0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85ef0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85f00 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85f10 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  ----.**.** The a
85f20 70 70 6c 69 63 61 74 69 6f 6e 20 63 6f 64 65 20  pplication code 
85f30 73 65 65 73 20 6f 6e 6c 79 20 61 20 70 6f 69 6e  sees only a poin
85f40 74 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63  ter to the alloc
85f50 61 74 69 6f 6e 2e 20 20 57 65 20 68 61 76 65 0a  ation.  We have.
85f60 2a 2a 20 74 6f 20 62 61 63 6b 20 75 70 20 66 72  ** to back up fr
85f70 6f 6d 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f  om the allocatio
85f80 6e 20 70 6f 69 6e 74 65 72 20 74 6f 20 66 69 6e  n pointer to fin
85f90 64 20 74 68 65 20 4d 65 6d 42 6c 6f 63 6b 48 64  d the MemBlockHd
85fa0 72 2e 20 20 54 68 65 0a 2a 2a 20 4d 65 6d 42 6c  r.  The.** MemBl
85fb0 6f 63 6b 48 64 72 20 74 65 6c 6c 73 20 75 73 20  ockHdr tells us 
85fc0 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
85fd0 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 20 74  allocation and t
85fe0 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20  he number of.** 
85ff0 62 61 63 6b 74 72 61 63 65 20 70 6f 69 6e 74 65  backtrace pointe
86000 72 73 2e 20 20 54 68 65 72 65 20 69 73 20 61 6c  rs.  There is al
86010 73 6f 20 61 20 67 75 61 72 64 20 77 6f 72 64 20  so a guard word 
86020 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  at the end of th
86030 65 0a 2a 2a 20 4d 65 6d 42 6c 6f 63 6b 48 64 72  e.** MemBlockHdr
86040 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 42  ..*/.struct MemB
86050 6c 6f 63 6b 48 64 72 20 7b 0a 20 20 69 36 34 20  lockHdr {.  i64 
86060 69 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20  iSize;          
86070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
86080 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 69 73 20  /* Size of this 
86090 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20  allocation */.  
860a0 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48  struct MemBlockH
860b0 64 72 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65  dr *pNext, *pPre
860c0 76 3b 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69  v;  /* Linked li
860d0 73 74 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65  st of all unfree
860e0 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 63 68  d memory */.  ch
860f0 61 72 20 6e 42 61 63 6b 74 72 61 63 65 3b 20 20  ar nBacktrace;  
86100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
86110 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
86120 61 63 6b 74 72 61 63 65 73 20 6f 6e 20 74 68 69  acktraces on thi
86130 73 20 61 6c 6c 6f 63 20 2a 2f 0a 20 20 63 68 61  s alloc */.  cha
86140 72 20 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74  r nBacktraceSlot
86150 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
86160 20 2f 2a 20 41 76 61 69 6c 61 62 6c 65 20 62 61   /* Available ba
86170 63 6b 74 72 61 63 65 20 73 6c 6f 74 73 20 2a 2f  cktrace slots */
86180 0a 20 20 73 68 6f 72 74 20 6e 54 69 74 6c 65 3b  .  short nTitle;
86190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
861a0 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20         /* Bytes 
861b0 6f 66 20 74 69 74 6c 65 3b 20 69 6e 63 6c 75 64  of title; includ
861c0 65 73 20 27 5c 30 27 20 2a 2f 0a 20 20 69 6e 74  es '\0' */.  int
861d0 20 69 46 6f 72 65 47 75 61 72 64 3b 20 20 20 20   iForeGuard;    
861e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
861f0 20 2f 2a 20 47 75 61 72 64 20 77 6f 72 64 20 66   /* Guard word f
86200 6f 72 20 73 61 6e 69 74 79 20 2a 2f 0a 7d 3b 0a  or sanity */.};.
86210 0a 2f 2a 0a 2a 2a 20 47 75 61 72 64 20 77 6f 72  ./*.** Guard wor
86220 64 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4f  ds.*/.#define FO
86230 52 45 47 55 41 52 44 20 30 78 38 30 46 35 45 31  REGUARD 0x80F5E1
86240 35 33 0a 23 64 65 66 69 6e 65 20 52 45 41 52 47  53.#define REARG
86250 55 41 52 44 20 30 78 45 34 36 37 36 42 35 33 0a  UARD 0xE4676B53.
86260 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66  ./*.** Number of
86270 20 6d 61 6c 6c 6f 63 20 73 69 7a 65 20 69 6e 63   malloc size inc
86280 72 65 6d 65 6e 74 73 20 74 6f 20 74 72 61 63 6b  rements to track
86290 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 43 53  ..*/.#define NCS
862a0 49 5a 45 20 20 31 30 30 30 0a 0a 2f 2a 0a 2a 2a  IZE  1000../*.**
862b0 20 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74   All of the stat
862c0 69 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 65  ic variables use
862d0 64 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65  d by this module
862e0 20 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a   are collected.*
862f0 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20  * into a single 
86300 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20  structure named 
86310 22 6d 65 6d 22 2e 20 20 54 68 69 73 20 69 73 20  "mem".  This is 
86320 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73  to keep the.** s
86330 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20  tatic variables 
86340 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f  organized and to
86350 20 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63   reduce namespac
86360 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77  e pollution.** w
86370 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20  hen this module 
86380 69 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68  is combined with
86390 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d   other in the am
863a0 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73  algamation..*/.s
863b0 74 61 74 69 63 20 73 74 72 75 63 74 20 7b 0a 20  tatic struct {. 
863c0 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65   .  /*.  ** Mute
863d0 78 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63  x to control acc
863e0 65 73 73 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72  ess to the memor
863f0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62  y allocation sub
86400 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73  system..  */.  s
86410 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
86420 74 65 78 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  tex;..  /*.  ** 
86430 48 65 61 64 20 61 6e 64 20 74 61 69 6c 20 6f 66  Head and tail of
86440 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f   a linked list o
86450 66 20 61 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e  f all outstandin
86460 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20  g allocations.  
86470 2a 2f 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42  */.  struct MemB
86480 6c 6f 63 6b 48 64 72 20 2a 70 46 69 72 73 74 3b  lockHdr *pFirst;
86490 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f  .  struct MemBlo
864a0 63 6b 48 64 72 20 2a 70 4c 61 73 74 3b 0a 20 20  ckHdr *pLast;.  
864b0 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6e  .  /*.  ** The n
864c0 75 6d 62 65 72 20 6f 66 20 6c 65 76 65 6c 73 20  umber of levels 
864d0 6f 66 20 62 61 63 6b 74 72 61 63 65 20 74 6f 20  of backtrace to 
864e0 73 61 76 65 20 69 6e 20 6e 65 77 20 61 6c 6c 6f  save in new allo
864f0 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20  cations..  */.  
86500 69 6e 74 20 6e 42 61 63 6b 74 72 61 63 65 3b 0a  int nBacktrace;.
86510 20 20 76 6f 69 64 20 28 2a 78 42 61 63 6b 74 72    void (*xBacktr
86520 61 63 65 29 28 69 6e 74 2c 20 69 6e 74 2c 20 76  ace)(int, int, v
86530 6f 69 64 20 2a 2a 29 3b 0a 0a 20 20 2f 2a 0a 20  oid **);..  /*. 
86540 20 2a 2a 20 54 69 74 6c 65 20 74 65 78 74 20 74   ** Title text t
86550 6f 20 69 6e 73 65 72 74 20 69 6e 20 66 72 6f 6e  o insert in fron
86560 74 20 6f 66 20 65 61 63 68 20 62 6c 6f 63 6b 0a  t of each block.
86570 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 69 74 6c    */.  int nTitl
86580 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74  e;        /* Byt
86590 65 73 20 6f 66 20 7a 54 69 74 6c 65 20 74 6f 20  es of zTitle to 
865a0 73 61 76 65 2e 20 20 49 6e 63 6c 75 64 65 73 20  save.  Includes 
865b0 27 5c 30 27 20 61 6e 64 20 70 61 64 64 69 6e 67  '\0' and padding
865c0 20 2a 2f 0a 20 20 63 68 61 72 20 7a 54 69 74 6c   */.  char zTitl
865d0 65 5b 31 30 30 5d 3b 20 20 2f 2a 20 54 68 65 20  e[100];  /* The 
865e0 74 69 74 6c 65 20 74 65 78 74 20 2a 2f 0a 0a 20  title text */.. 
865f0 20 2f 2a 20 0a 20 20 2a 2a 20 73 71 6c 69 74 65   /* .  ** sqlite
86600 33 4d 61 6c 6c 6f 63 44 69 73 61 6c 6c 6f 77 28  3MallocDisallow(
86610 29 20 69 6e 63 72 65 6d 65 6e 74 73 20 74 68 65  ) increments the
86620 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 75 6e 74   following count
86630 65 72 2e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33  er..  ** sqlite3
86640 4d 61 6c 6c 6f 63 41 6c 6c 6f 77 28 29 20 64 65  MallocAllow() de
86650 63 72 65 6d 65 6e 74 73 20 69 74 2e 0a 20 20 2a  crements it..  *
86660 2f 0a 20 20 69 6e 74 20 64 69 73 61 6c 6c 6f 77  /.  int disallow
86670 3b 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f  ; /* Do not allo
86680 77 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  w memory allocat
86690 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a  ion */..  /*.  *
866a0 2a 20 47 61 74 68 65 72 20 73 74 61 74 69 73 74  * Gather statist
866b0 69 63 73 20 6f 6e 20 74 68 65 20 73 69 7a 65 73  ics on the sizes
866c0 20 6f 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63   of memory alloc
866d0 61 74 69 6f 6e 73 2e 0a 20 20 2a 2a 20 6e 41 6c  ations..  ** nAl
866e0 6c 6f 63 5b 69 5d 20 69 73 20 74 68 65 20 6e 75  loc[i] is the nu
866f0 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69  mber of allocati
86700 6f 6e 20 61 74 74 65 6d 70 74 73 20 6f 66 20 69  on attempts of i
86710 2a 38 0a 20 20 2a 2a 20 62 79 74 65 73 2e 20 20  *8.  ** bytes.  
86720 69 3d 3d 4e 43 53 49 5a 45 20 69 73 20 74 68 65  i==NCSIZE is the
86730 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63   number of alloc
86740 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 66  ation attempts f
86750 6f 72 0a 20 20 2a 2a 20 73 69 7a 65 73 20 6d 6f  or.  ** sizes mo
86760 72 65 20 74 68 61 6e 20 4e 43 53 49 5a 45 2a 38  re than NCSIZE*8
86770 20 62 79 74 65 73 2e 0a 20 20 2a 2f 0a 20 20 69   bytes..  */.  i
86780 6e 74 20 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45  nt nAlloc[NCSIZE
86790 5d 3b 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c  ];      /* Total
867a0 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63   number of alloc
867b0 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20  ations */.  int 
867c0 6e 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d  nCurrent[NCSIZE]
867d0 3b 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20  ;    /* Current 
867e0 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61  number of alloca
867f0 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6d  tions */.  int m
86800 78 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d  xCurrent[NCSIZE]
86810 3b 20 20 20 2f 2a 20 48 69 67 68 77 61 74 65 72  ;   /* Highwater
86820 20 6d 61 72 6b 20 66 6f 72 20 6e 43 75 72 72 65   mark for nCurre
86830 6e 74 20 2a 2f 0a 0a 7d 20 6d 65 6d 3b 0a 0a 0a  nt */..} mem;...
86840 2f 2a 0a 2a 2a 20 41 64 6a 75 73 74 20 6d 65 6d  /*.** Adjust mem
86850 6f 72 79 20 75 73 61 67 65 20 73 74 61 74 69 73  ory usage statis
86860 74 69 63 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76  tics.*/.static v
86870 6f 69 64 20 61 64 6a 75 73 74 53 74 61 74 73 28  oid adjustStats(
86880 69 6e 74 20 69 53 69 7a 65 2c 20 69 6e 74 20 69  int iSize, int i
86890 6e 63 72 65 6d 65 6e 74 29 7b 0a 20 20 69 6e 74  ncrement){.  int
868a0 20 69 20 3d 20 28 28 69 53 69 7a 65 2b 37 29 26   i = ((iSize+7)&
868b0 7e 37 29 2f 38 3b 0a 20 20 69 66 28 20 69 3e 4e  ~7)/8;.  if( i>N
868c0 43 53 49 5a 45 2d 31 20 29 7b 0a 20 20 20 20 69  CSIZE-1 ){.    i
868d0 20 3d 20 4e 43 53 49 5a 45 20 2d 20 31 3b 0a 20   = NCSIZE - 1;. 
868e0 20 7d 0a 20 20 69 66 28 20 69 6e 63 72 65 6d 65   }.  if( increme
868f0 6e 74 3e 30 20 29 7b 0a 20 20 20 20 6d 65 6d 2e  nt>0 ){.    mem.
86900 6e 41 6c 6c 6f 63 5b 69 5d 2b 2b 3b 0a 20 20 20  nAlloc[i]++;.   
86910 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d   mem.nCurrent[i]
86920 2b 2b 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e  ++;.    if( mem.
86930 6e 43 75 72 72 65 6e 74 5b 69 5d 3e 6d 65 6d 2e  nCurrent[i]>mem.
86940 6d 78 43 75 72 72 65 6e 74 5b 69 5d 20 29 7b 0a  mxCurrent[i] ){.
86950 20 20 20 20 20 20 6d 65 6d 2e 6d 78 43 75 72 72        mem.mxCurr
86960 65 6e 74 5b 69 5d 20 3d 20 6d 65 6d 2e 6e 43 75  ent[i] = mem.nCu
86970 72 72 65 6e 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a  rrent[i];.    }.
86980 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d    }else{.    mem
86990 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2d 2d 3b 0a  .nCurrent[i]--;.
869a0 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e      assert( mem.
869b0 6e 43 75 72 72 65 6e 74 5b 69 5d 3e 3d 30 20 29  nCurrent[i]>=0 )
869c0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  ;.  }.}../*.** G
869d0 69 76 65 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 69  iven an allocati
869e0 6f 6e 2c 20 66 69 6e 64 20 74 68 65 20 4d 65 6d  on, find the Mem
869f0 42 6c 6f 63 6b 48 64 72 20 66 6f 72 20 74 68 61  BlockHdr for tha
86a00 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a  t allocation..**
86a10 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
86a20 20 63 68 65 63 6b 73 20 74 68 65 20 67 75 61 72   checks the guar
86a30 64 73 20 61 74 20 65 69 74 68 65 72 20 65 6e 64  ds at either end
86a40 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69   of the allocati
86a50 6f 6e 20 61 6e 64 0a 2a 2a 20 69 66 20 74 68 65  on and.** if the
86a60 79 20 61 72 65 20 69 6e 63 6f 72 72 65 63 74 20  y are incorrect 
86a70 69 74 20 61 73 73 65 72 74 73 2e 0a 2a 2f 0a 73  it asserts..*/.s
86a80 74 61 74 69 63 20 73 74 72 75 63 74 20 4d 65 6d  tatic struct Mem
86a90 42 6c 6f 63 6b 48 64 72 20 2a 73 71 6c 69 74 65  BlockHdr *sqlite
86aa0 33 4d 65 6d 73 79 73 47 65 74 48 65 61 64 65 72  3MemsysGetHeader
86ab0 28 76 6f 69 64 20 2a 70 41 6c 6c 6f 63 61 74 69  (void *pAllocati
86ac0 6f 6e 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65  on){.  struct Me
86ad0 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 3b 0a 20 20  mBlockHdr *p;.  
86ae0 69 6e 74 20 2a 70 49 6e 74 3b 0a 20 20 75 38 20  int *pInt;.  u8 
86af0 2a 70 55 38 3b 0a 20 20 69 6e 74 20 6e 52 65 73  *pU8;.  int nRes
86b00 65 72 76 65 3b 0a 0a 20 20 70 20 3d 20 28 73 74  erve;..  p = (st
86b10 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72  ruct MemBlockHdr
86b20 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20  *)pAllocation;. 
86b30 20 70 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20   p--;.  assert( 
86b40 70 2d 3e 69 46 6f 72 65 47 75 61 72 64 3d 3d 46  p->iForeGuard==F
86b50 4f 52 45 47 55 41 52 44 20 29 3b 0a 20 20 6e 52  OREGUARD );.  nR
86b60 65 73 65 72 76 65 20 3d 20 28 70 2d 3e 69 53 69  eserve = (p->iSi
86b70 7a 65 2b 37 29 26 7e 37 3b 0a 20 20 70 49 6e 74  ze+7)&~7;.  pInt
86b80 20 3d 20 28 69 6e 74 2a 29 70 41 6c 6c 6f 63 61   = (int*)pAlloca
86b90 74 69 6f 6e 3b 0a 20 20 70 55 38 20 3d 20 28 75  tion;.  pU8 = (u
86ba0 38 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a  8*)pAllocation;.
86bb0 20 20 61 73 73 65 72 74 28 20 70 49 6e 74 5b 6e    assert( pInt[n
86bc0 52 65 73 65 72 76 65 2f 73 69 7a 65 6f 66 28 69  Reserve/sizeof(i
86bd0 6e 74 29 5d 3d 3d 52 45 41 52 47 55 41 52 44 20  nt)]==REARGUARD 
86be0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6e 52  );.  assert( (nR
86bf0 65 73 65 72 76 65 2d 30 29 3c 3d 70 2d 3e 69 53  eserve-0)<=p->iS
86c00 69 7a 65 20 7c 7c 20 70 55 38 5b 6e 52 65 73 65  ize || pU8[nRese
86c10 72 76 65 2d 31 5d 3d 3d 30 78 36 35 20 29 3b 0a  rve-1]==0x65 );.
86c20 20 20 61 73 73 65 72 74 28 20 28 6e 52 65 73 65    assert( (nRese
86c30 72 76 65 2d 31 29 3c 3d 70 2d 3e 69 53 69 7a 65  rve-1)<=p->iSize
86c40 20 7c 7c 20 70 55 38 5b 6e 52 65 73 65 72 76 65   || pU8[nReserve
86c50 2d 32 5d 3d 3d 30 78 36 35 20 29 3b 0a 20 20 61  -2]==0x65 );.  a
86c60 73 73 65 72 74 28 20 28 6e 52 65 73 65 72 76 65  ssert( (nReserve
86c70 2d 32 29 3c 3d 70 2d 3e 69 53 69 7a 65 20 7c 7c  -2)<=p->iSize ||
86c80 20 70 55 38 5b 6e 52 65 73 65 72 76 65 2d 33 5d   pU8[nReserve-3]
86c90 3d 3d 30 78 36 35 20 29 3b 0a 20 20 72 65 74 75  ==0x65 );.  retu
86ca0 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  rn p;.}../*.** R
86cb0 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
86cc0 20 6f 66 20 62 79 74 65 73 20 63 75 72 72 65 6e   of bytes curren
86cd0 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 61 74  tly allocated at
86ce0 20 61 64 64 72 65 73 73 20 70 2e 0a 2a 2f 0a 73   address p..*/.s
86cf0 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65  tatic int sqlite
86d00 33 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70  3MemSize(void *p
86d10 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42  ){.  struct MemB
86d20 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20  lockHdr *pHdr;. 
86d30 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72   if( !p ){.    r
86d40 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70  eturn 0;.  }.  p
86d50 48 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d  Hdr = sqlite3Mem
86d60 73 79 73 47 65 74 48 65 61 64 65 72 28 70 29 3b  sysGetHeader(p);
86d70 0a 20 20 72 65 74 75 72 6e 20 70 48 64 72 2d 3e  .  return pHdr->
86d80 69 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  iSize;.}../*.** 
86d90 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d  Initialize the m
86da0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
86db0 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73   subsystem..*/.s
86dc0 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65  tatic int sqlite
86dd0 33 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e  3MemInit(void *N
86de0 6f 74 55 73 65 64 29 7b 0a 20 20 69 66 28 20 21  otUsed){.  if( !
86df0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
86e00 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a  fig.bMemstat ){.
86e10 20 20 20 20 2f 2a 20 49 66 20 6d 65 6d 6f 72 79      /* If memory
86e20 20 73 74 61 74 75 73 20 69 73 20 65 6e 61 62 6c   status is enabl
86e30 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6d 61 6c  ed, then the mal
86e40 6c 6f 63 2e 63 20 77 72 61 70 70 65 72 20 77 69  loc.c wrapper wi
86e50 6c 6c 20 61 6c 72 65 61 64 79 0a 20 20 20 20 2a  ll already.    *
86e60 2a 20 68 6f 6c 64 20 74 68 65 20 53 54 41 54 49  * hold the STATI
86e70 43 5f 4d 45 4d 20 6d 75 74 65 78 20 77 68 65 6e  C_MEM mutex when
86e80 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 68 65   the routines he
86e90 72 65 20 61 72 65 20 69 6e 76 6f 6b 65 64 2e 20  re are invoked. 
86ea0 2a 2f 0a 20 20 20 20 6d 65 6d 2e 6d 75 74 65 78  */.    mem.mutex
86eb0 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
86ec0 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
86ed0 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20  X_STATIC_MEM);. 
86ee0 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
86ef0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
86f00 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  Deinitialize the
86f10 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
86f20 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f  on subsystem..*/
86f30 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c  .static void sql
86f40 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28  ite3MemShutdown(
86f50 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a  void *NotUsed){.
86f60 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d 20 30 3b    mem.mutex = 0;
86f70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20  .}../*.** Round 
86f80 75 70 20 61 20 72 65 71 75 65 73 74 20 73 69 7a  up a request siz
86f90 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61  e to the next va
86fa0 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  lid allocation s
86fb0 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ize..*/.static i
86fc0 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75  nt sqlite3MemRou
86fd0 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 72  ndup(int n){.  r
86fe0 65 74 75 72 6e 20 28 6e 2b 37 29 20 26 20 7e 37  eturn (n+7) & ~7
86ff0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  ;.}../*.** Alloc
87000 61 74 65 20 6e 42 79 74 65 20 62 79 74 65 73 20  ate nByte bytes 
87010 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74  of memory..*/.st
87020 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74  atic void *sqlit
87030 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20  e3MemMalloc(int 
87040 6e 42 79 74 65 29 7b 0a 20 20 73 74 72 75 63 74  nByte){.  struct
87050 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48   MemBlockHdr *pH
87060 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74  dr;.  void **pBt
87070 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69  ;.  char *z;.  i
87080 6e 74 20 2a 70 49 6e 74 3b 0a 20 20 76 6f 69 64  nt *pInt;.  void
87090 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 74   *p = 0;.  int t
870a0 6f 74 61 6c 53 69 7a 65 3b 0a 20 20 69 6e 74 20  otalSize;.  int 
870b0 6e 52 65 73 65 72 76 65 3b 0a 20 20 73 71 6c 69  nReserve;.  sqli
870c0 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
870d0 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 61 73  mem.mutex);.  as
870e0 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61 6c 6c  sert( mem.disall
870f0 6f 77 3d 3d 30 20 29 3b 0a 20 20 6e 52 65 73 65  ow==0 );.  nRese
87100 72 76 65 20 3d 20 28 6e 42 79 74 65 2b 37 29 26  rve = (nByte+7)&
87110 7e 37 3b 0a 20 20 74 6f 74 61 6c 53 69 7a 65 20  ~7;.  totalSize 
87120 3d 20 6e 52 65 73 65 72 76 65 20 2b 20 73 69 7a  = nReserve + siz
87130 65 6f 66 28 2a 70 48 64 72 29 20 2b 20 73 69 7a  eof(*pHdr) + siz
87140 65 6f 66 28 69 6e 74 29 20 2b 0a 20 20 20 20 20  eof(int) +.     
87150 20 20 20 20 20 20 20 20 20 20 6d 65 6d 2e 6e 42            mem.nB
87160 61 63 6b 74 72 61 63 65 2a 73 69 7a 65 6f 66 28  acktrace*sizeof(
87170 76 6f 69 64 2a 29 20 2b 20 6d 65 6d 2e 6e 54 69  void*) + mem.nTi
87180 74 6c 65 3b 0a 20 20 70 20 3d 20 6d 61 6c 6c 6f  tle;.  p = mallo
87190 63 28 74 6f 74 61 6c 53 69 7a 65 29 3b 0a 20 20  c(totalSize);.  
871a0 69 66 28 20 70 20 29 7b 0a 20 20 20 20 7a 20 3d  if( p ){.    z =
871b0 20 70 3b 0a 20 20 20 20 70 42 74 20 3d 20 28 76   p;.    pBt = (v
871c0 6f 69 64 2a 2a 29 26 7a 5b 6d 65 6d 2e 6e 54 69  oid**)&z[mem.nTi
871d0 74 6c 65 5d 3b 0a 20 20 20 20 70 48 64 72 20 3d  tle];.    pHdr =
871e0 20 28 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63   (struct MemBloc
871f0 6b 48 64 72 2a 29 26 70 42 74 5b 6d 65 6d 2e 6e  kHdr*)&pBt[mem.n
87200 42 61 63 6b 74 72 61 63 65 5d 3b 0a 20 20 20 20  Backtrace];.    
87210 70 48 64 72 2d 3e 70 4e 65 78 74 20 3d 20 30 3b  pHdr->pNext = 0;
87220 0a 20 20 20 20 70 48 64 72 2d 3e 70 50 72 65 76  .    pHdr->pPrev
87230 20 3d 20 6d 65 6d 2e 70 4c 61 73 74 3b 0a 20 20   = mem.pLast;.  
87240 20 20 69 66 28 20 6d 65 6d 2e 70 4c 61 73 74 20    if( mem.pLast 
87250 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 70 4c 61  ){.      mem.pLa
87260 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 48 64 72  st->pNext = pHdr
87270 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
87280 20 20 20 6d 65 6d 2e 70 46 69 72 73 74 20 3d 20     mem.pFirst = 
87290 70 48 64 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20  pHdr;.    }.    
872a0 6d 65 6d 2e 70 4c 61 73 74 20 3d 20 70 48 64 72  mem.pLast = pHdr
872b0 3b 0a 20 20 20 20 70 48 64 72 2d 3e 69 46 6f 72  ;.    pHdr->iFor
872c0 65 47 75 61 72 64 20 3d 20 46 4f 52 45 47 55 41  eGuard = FOREGUA
872d0 52 44 3b 0a 20 20 20 20 70 48 64 72 2d 3e 6e 42  RD;.    pHdr->nB
872e0 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 20 3d 20  acktraceSlots = 
872f0 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 3b 0a  mem.nBacktrace;.
87300 20 20 20 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65      pHdr->nTitle
87310 20 3d 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a 20   = mem.nTitle;. 
87320 20 20 20 69 66 28 20 6d 65 6d 2e 6e 42 61 63 6b     if( mem.nBack
87330 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 76  trace ){.      v
87340 6f 69 64 20 2a 61 41 64 64 72 5b 34 30 5d 3b 0a  oid *aAddr[40];.
87350 20 20 20 20 20 20 70 48 64 72 2d 3e 6e 42 61 63        pHdr->nBac
87360 6b 74 72 61 63 65 20 3d 20 62 61 63 6b 74 72 61  ktrace = backtra
87370 63 65 28 61 41 64 64 72 2c 20 6d 65 6d 2e 6e 42  ce(aAddr, mem.nB
87380 61 63 6b 74 72 61 63 65 2b 31 29 2d 31 3b 0a 20  acktrace+1)-1;. 
87390 20 20 20 20 20 6d 65 6d 63 70 79 28 70 42 74 2c       memcpy(pBt,
873a0 20 26 61 41 64 64 72 5b 31 5d 2c 20 70 48 64 72   &aAddr[1], pHdr
873b0 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2a 73 69 7a  ->nBacktrace*siz
873c0 65 6f 66 28 76 6f 69 64 2a 29 29 3b 0a 20 20 20  eof(void*));.   
873d0 20 20 20 69 66 28 20 6d 65 6d 2e 78 42 61 63 6b     if( mem.xBack
873e0 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20  trace ){.       
873f0 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 28   mem.xBacktrace(
87400 6e 42 79 74 65 2c 20 70 48 64 72 2d 3e 6e 42 61  nByte, pHdr->nBa
87410 63 6b 74 72 61 63 65 2d 31 2c 20 26 61 41 64 64  cktrace-1, &aAdd
87420 72 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20  r[1]);.      }. 
87430 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
87440 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65  pHdr->nBacktrace
87450 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
87460 69 66 28 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 29  if( mem.nTitle )
87470 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a  {.      memcpy(z
87480 2c 20 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 6d 65  , mem.zTitle, me
87490 6d 2e 6e 54 69 74 6c 65 29 3b 0a 20 20 20 20 7d  m.nTitle);.    }
874a0 0a 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65  .    pHdr->iSize
874b0 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 64   = nByte;.    ad
874c0 6a 75 73 74 53 74 61 74 73 28 6e 42 79 74 65 2c  justStats(nByte,
874d0 20 2b 31 29 3b 0a 20 20 20 20 70 49 6e 74 20 3d   +1);.    pInt =
874e0 20 28 69 6e 74 2a 29 26 70 48 64 72 5b 31 5d 3b   (int*)&pHdr[1];
874f0 0a 20 20 20 20 70 49 6e 74 5b 6e 52 65 73 65 72  .    pInt[nReser
87500 76 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 20  ve/sizeof(int)] 
87510 3d 20 52 45 41 52 47 55 41 52 44 3b 0a 20 20 20  = REARGUARD;.   
87520 20 6d 65 6d 73 65 74 28 70 49 6e 74 2c 20 30 78   memset(pInt, 0x
87530 36 35 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a 20  65, nReserve);. 
87540 20 20 20 70 20 3d 20 28 76 6f 69 64 2a 29 70 49     p = (void*)pI
87550 6e 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  nt;.  }.  sqlite
87560 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
87570 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  m.mutex);.  retu
87580 72 6e 20 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn p; .}../*.** 
87590 46 72 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a  Free memory..*/.
875a0 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69  static void sqli
875b0 74 65 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 20  te3MemFree(void 
875c0 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 74 72 75  *pPrior){.  stru
875d0 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a  ct MemBlockHdr *
875e0 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70  pHdr;.  void **p
875f0 42 74 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20  Bt;.  char *z;. 
87600 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
87610 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65  GlobalConfig.bMe
87620 6d 73 74 61 74 20 7c 7c 20 6d 65 6d 2e 6d 75 74  mstat || mem.mut
87630 65 78 21 3d 30 20 29 3b 0a 20 20 70 48 64 72 20  ex!=0 );.  pHdr 
87640 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47  = sqlite3MemsysG
87650 65 74 48 65 61 64 65 72 28 70 50 72 69 6f 72 29  etHeader(pPrior)
87660 3b 0a 20 20 70 42 74 20 3d 20 28 76 6f 69 64 2a  ;.  pBt = (void*
87670 2a 29 70 48 64 72 3b 0a 20 20 70 42 74 20 2d 3d  *)pHdr;.  pBt -=
87680 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63   pHdr->nBacktrac
87690 65 53 6c 6f 74 73 3b 0a 20 20 73 71 6c 69 74 65  eSlots;.  sqlite
876a0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65  3_mutex_enter(me
876b0 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20  m.mutex);.  if( 
876c0 70 48 64 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20  pHdr->pPrev ){. 
876d0 20 20 20 61 73 73 65 72 74 28 20 70 48 64 72 2d     assert( pHdr-
876e0 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70  >pPrev->pNext==p
876f0 48 64 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d  Hdr );.    pHdr-
87700 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20  >pPrev->pNext = 
87710 70 48 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d  pHdr->pNext;.  }
87720 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
87730 28 20 6d 65 6d 2e 70 46 69 72 73 74 3d 3d 70 48  ( mem.pFirst==pH
87740 64 72 20 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 46  dr );.    mem.pF
87750 69 72 73 74 20 3d 20 70 48 64 72 2d 3e 70 4e 65  irst = pHdr->pNe
87760 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48  xt;.  }.  if( pH
87770 64 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20  dr->pNext ){.   
87780 20 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e 70   assert( pHdr->p
87790 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 48 64  Next->pPrev==pHd
877a0 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70  r );.    pHdr->p
877b0 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 48  Next->pPrev = pH
877c0 64 72 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 65 6c  dr->pPrev;.  }el
877d0 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
877e0 6d 65 6d 2e 70 4c 61 73 74 3d 3d 70 48 64 72 20  mem.pLast==pHdr 
877f0 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74  );.    mem.pLast
87800 20 3d 20 70 48 64 72 2d 3e 70 50 72 65 76 3b 0a   = pHdr->pPrev;.
87810 20 20 7d 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a    }.  z = (char*
87820 29 70 42 74 3b 0a 20 20 7a 20 2d 3d 20 70 48 64  )pBt;.  z -= pHd
87830 72 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 61 64 6a  r->nTitle;.  adj
87840 75 73 74 53 74 61 74 73 28 70 48 64 72 2d 3e 69  ustStats(pHdr->i
87850 53 69 7a 65 2c 20 2d 31 29 3b 0a 20 20 6d 65 6d  Size, -1);.  mem
87860 73 65 74 28 7a 2c 20 30 78 32 62 2c 20 73 69 7a  set(z, 0x2b, siz
87870 65 6f 66 28 76 6f 69 64 2a 29 2a 70 48 64 72 2d  eof(void*)*pHdr-
87880 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73  >nBacktraceSlots
87890 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48 64 72 29   + sizeof(*pHdr)
878a0 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   +.             
878b0 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65       pHdr->iSize
878c0 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b   + sizeof(int) +
878d0 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 29 3b 0a   pHdr->nTitle);.
878e0 20 20 66 72 65 65 28 7a 29 3b 0a 20 20 73 71 6c    free(z);.  sql
878f0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
87900 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 20 20 0a 7d  (mem.mutex);  .}
87910 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
87920 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78  he size of an ex
87930 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c  isting memory al
87940 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  location..**.** 
87950 46 6f 72 20 74 68 69 73 20 64 65 62 75 67 67 69  For this debuggi
87960 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ng implementatio
87970 6e 2c 20 77 65 20 2a 61 6c 77 61 79 73 2a 20 6d  n, we *always* m
87980 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68  ake a copy of th
87990 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  e.** allocation 
879a0 69 6e 74 6f 20 61 20 6e 65 77 20 70 6c 61 63 65  into a new place
879b0 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 49 6e 20   in memory.  In 
879c0 74 68 69 73 20 77 61 79 2c 20 69 66 20 74 68 65  this way, if the
879d0 20 0a 2a 2a 20 68 69 67 68 65 72 20 6c 65 76 65   .** higher leve
879e0 6c 20 63 6f 64 65 20 69 73 20 75 73 69 6e 67 20  l code is using 
879f0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6f  pointer to the o
87a00 6c 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69  ld allocation, i
87a10 74 20 69 73 20 0a 2a 2a 20 6d 75 63 68 20 6d 6f  t is .** much mo
87a20 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62 72 65  re likely to bre
87a30 61 6b 20 61 6e 64 20 77 65 20 61 72 65 20 6d 75  ak and we are mu
87a40 63 68 20 6d 6f 72 65 20 6c 69 6b 69 6e 67 20 74  ch more liking t
87a50 6f 20 66 69 6e 64 0a 2a 2a 20 74 68 65 20 65 72  o find.** the er
87a60 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ror..*/.static v
87a70 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52  oid *sqlite3MemR
87a80 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72  ealloc(void *pPr
87a90 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b  ior, int nByte){
87aa0 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f  .  struct MemBlo
87ab0 63 6b 48 64 72 20 2a 70 4f 6c 64 48 64 72 3b 0a  ckHdr *pOldHdr;.
87ac0 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20    void *pNew;.  
87ad0 61 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61  assert( mem.disa
87ae0 6c 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 70 4f 6c  llow==0 );.  pOl
87af0 64 48 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65  dHdr = sqlite3Me
87b00 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 70 50  msysGetHeader(pP
87b10 72 69 6f 72 29 3b 0a 20 20 70 4e 65 77 20 3d 20  rior);.  pNew = 
87b20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63  sqlite3MemMalloc
87b30 28 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 70  (nByte);.  if( p
87b40 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70  New ){.    memcp
87b50 79 28 70 4e 65 77 2c 20 70 50 72 69 6f 72 2c 20  y(pNew, pPrior, 
87b60 6e 42 79 74 65 3c 70 4f 6c 64 48 64 72 2d 3e 69  nByte<pOldHdr->i
87b70 53 69 7a 65 20 3f 20 6e 42 79 74 65 20 3a 20 70  Size ? nByte : p
87b80 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 29 3b 0a  OldHdr->iSize);.
87b90 20 20 20 20 69 66 28 20 6e 42 79 74 65 3e 70 4f      if( nByte>pO
87ba0 6c 64 48 64 72 2d 3e 69 53 69 7a 65 20 29 7b 0a  ldHdr->iSize ){.
87bb0 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28        memset(&((
87bc0 63 68 61 72 2a 29 70 4e 65 77 29 5b 70 4f 6c 64  char*)pNew)[pOld
87bd0 48 64 72 2d 3e 69 53 69 7a 65 5d 2c 20 30 78 32  Hdr->iSize], 0x2
87be0 62 2c 20 6e 42 79 74 65 20 2d 20 70 4f 6c 64 48  b, nByte - pOldH
87bf0 64 72 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20  dr->iSize);.    
87c00 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  }.    sqlite3Mem
87c10 46 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20  Free(pPrior);.  
87c20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b  }.  return pNew;
87c30 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 75 6c 61  .}../*.** Popula
87c40 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c  te the low-level
87c50 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
87c60 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e  on function poin
87c70 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74  ters in.** sqlit
87c80 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
87c90 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74   with pointers t
87ca0 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69  o the routines i
87cb0 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a  n this file..*/.
87cc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
87cd0 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 65  oid sqlite3MemSe
87ce0 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a  tDefault(void){.
87cf0 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73    static const s
87d00 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
87d10 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64  ds defaultMethod
87d20 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 74  s = {.     sqlit
87d30 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20  e3MemMalloc,.   
87d40 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65    sqlite3MemFree
87d50 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65  ,.     sqlite3Me
87d60 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73  mRealloc,.     s
87d70 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20  qlite3MemSize,. 
87d80 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f      sqlite3MemRo
87d90 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c 69  undup,.     sqli
87da0 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20  te3MemInit,.    
87db0 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64   sqlite3MemShutd
87dc0 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b  own,.     0.  };
87dd0 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  .  sqlite3_confi
87de0 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  g(SQLITE_CONFIG_
87df0 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74  MALLOC, &default
87e00 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 2f 2a 0a  Methods);.}../*.
87e10 2a 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65  ** Set the numbe
87e20 72 20 6f 66 20 62 61 63 6b 74 72 61 63 65 20 6c  r of backtrace l
87e30 65 76 65 6c 73 20 6b 65 70 74 20 66 6f 72 20 65  evels kept for e
87e40 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a  ach allocation..
87e50 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 7a 65  ** A value of ze
87e60 72 6f 20 74 75 72 6e 73 20 6f 66 66 20 62 61 63  ro turns off bac
87e70 6b 74 72 61 63 69 6e 67 2e 20 20 54 68 65 20 6e  ktracing.  The n
87e80 75 6d 62 65 72 20 69 73 20 61 6c 77 61 79 73 20  umber is always 
87e90 72 6f 75 6e 64 65 64 0a 2a 2a 20 75 70 20 74 6f  rounded.** up to
87ea0 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 32   a multiple of 2
87eb0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
87ec0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
87ed0 4d 65 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63  MemdebugBacktrac
87ee0 65 28 69 6e 74 20 64 65 70 74 68 29 7b 0a 20 20  e(int depth){.  
87ef0 69 66 28 20 64 65 70 74 68 3c 30 20 29 7b 20 64  if( depth<0 ){ d
87f00 65 70 74 68 20 3d 20 30 3b 20 7d 0a 20 20 69 66  epth = 0; }.  if
87f10 28 20 64 65 70 74 68 3e 32 30 20 29 7b 20 64 65  ( depth>20 ){ de
87f20 70 74 68 20 3d 20 32 30 3b 20 7d 0a 20 20 64 65  pth = 20; }.  de
87f30 70 74 68 20 3d 20 28 64 65 70 74 68 2b 31 29 26  pth = (depth+1)&
87f40 30 78 66 65 3b 0a 20 20 6d 65 6d 2e 6e 42 61 63  0xfe;.  mem.nBac
87f50 6b 74 72 61 63 65 20 3d 20 64 65 70 74 68 3b 0a  ktrace = depth;.
87f60 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  }..SQLITE_PRIVAT
87f70 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65  E void sqlite3Me
87f80 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63 65 43  mdebugBacktraceC
87f90 61 6c 6c 62 61 63 6b 28 76 6f 69 64 20 28 2a 78  allback(void (*x
87fa0 42 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20  Backtrace)(int, 
87fb0 69 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 29 7b 0a  int, void **)){.
87fc0 20 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65    mem.xBacktrace
87fd0 20 3d 20 78 42 61 63 6b 74 72 61 63 65 3b 0a 7d   = xBacktrace;.}
87fe0 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
87ff0 74 69 74 6c 65 20 73 74 72 69 6e 67 20 66 6f 72  title string for
88000 20 73 75 62 73 65 71 75 65 6e 74 20 61 6c 6c 6f   subsequent allo
88010 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49  cations..*/.SQLI
88020 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
88030 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53  sqlite3MemdebugS
88040 65 74 74 69 74 6c 65 28 63 6f 6e 73 74 20 63 68  ettitle(const ch
88050 61 72 20 2a 7a 54 69 74 6c 65 29 7b 0a 20 20 69  ar *zTitle){.  i
88060 6e 74 20 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 54  nt n = strlen(zT
88070 69 74 6c 65 29 20 2b 20 31 3b 0a 20 20 73 71 6c  itle) + 1;.  sql
88080 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
88090 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 69  (mem.mutex);.  i
880a0 66 28 20 6e 3e 3d 73 69 7a 65 6f 66 28 6d 65 6d  f( n>=sizeof(mem
880b0 2e 7a 54 69 74 6c 65 29 20 29 20 6e 20 3d 20 73  .zTitle) ) n = s
880c0 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74 6c 65  izeof(mem.zTitle
880d0 29 2d 31 3b 0a 20 20 6d 65 6d 63 70 79 28 6d 65  )-1;.  memcpy(me
880e0 6d 2e 7a 54 69 74 6c 65 2c 20 7a 54 69 74 6c 65  m.zTitle, zTitle
880f0 2c 20 6e 29 3b 0a 20 20 6d 65 6d 2e 7a 54 69 74  , n);.  mem.zTit
88100 6c 65 5b 6e 5d 20 3d 20 30 3b 0a 20 20 6d 65 6d  le[n] = 0;.  mem
88110 2e 6e 54 69 74 6c 65 20 3d 20 28 6e 2b 37 29 26  .nTitle = (n+7)&
88120 7e 37 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  ~7;.  sqlite3_mu
88130 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75  tex_leave(mem.mu
88140 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f  tex);.}..SQLITE_
88150 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
88160 69 74 65 33 4d 65 6d 64 65 62 75 67 53 79 6e 63  ite3MemdebugSync
88170 28 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d  (){.  struct Mem
88180 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a  BlockHdr *pHdr;.
88190 20 20 66 6f 72 28 70 48 64 72 3d 6d 65 6d 2e 70    for(pHdr=mem.p
881a0 46 69 72 73 74 3b 20 70 48 64 72 3b 20 70 48 64  First; pHdr; pHd
881b0 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74 29 7b 0a  r=pHdr->pNext){.
881c0 20 20 20 20 76 6f 69 64 20 2a 2a 70 42 74 20 3d      void **pBt =
881d0 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b 0a 20   (void**)pHdr;. 
881e0 20 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e     pBt -= pHdr->
881f0 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b  nBacktraceSlots;
88200 0a 20 20 20 20 6d 65 6d 2e 78 42 61 63 6b 74 72  .    mem.xBacktr
88210 61 63 65 28 70 48 64 72 2d 3e 69 53 69 7a 65 2c  ace(pHdr->iSize,
88220 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63   pHdr->nBacktrac
88230 65 2d 31 2c 20 26 70 42 74 5b 31 5d 29 3b 0a 20  e-1, &pBt[1]);. 
88240 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e   }.}../*.** Open
88250 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69 63 61   the file indica
88260 74 65 64 20 61 6e 64 20 77 72 69 74 65 20 61 20  ted and write a 
88270 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65  log of all unfre
88280 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c  ed memory .** al
88290 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f 20 74  locations into t
882a0 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49  hat log..*/.SQLI
882b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
882c0 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 44  sqlite3MemdebugD
882d0 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ump(const char *
882e0 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 46 49  zFilename){.  FI
882f0 4c 45 20 2a 6f 75 74 3b 0a 20 20 73 74 72 75 63  LE *out;.  struc
88300 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70  t MemBlockHdr *p
88310 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42  Hdr;.  void **pB
88320 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 6f 75  t;.  int i;.  ou
88330 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e  t = fopen(zFilen
88340 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 69 66 28  ame, "w");.  if(
88350 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 66   out==0 ){.    f
88360 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22  printf(stderr, "
88370 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74  ** Unable to out
88380 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 75 67  put memory debug
88390 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 73 20   output log: %s 
883a0 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  **\n",.         
883b0 20 20 20 20 20 20 20 20 20 20 20 7a 46 69 6c 65             zFile
883c0 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72  name);.    retur
883d0 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 70 48 64  n;.  }.  for(pHd
883e0 72 3d 6d 65 6d 2e 70 46 69 72 73 74 3b 20 70 48  r=mem.pFirst; pH
883f0 64 72 3b 20 70 48 64 72 3d 70 48 64 72 2d 3e 70  dr; pHdr=pHdr->p
88400 4e 65 78 74 29 7b 0a 20 20 20 20 63 68 61 72 20  Next){.    char 
88410 2a 7a 20 3d 20 28 63 68 61 72 2a 29 70 48 64 72  *z = (char*)pHdr
88420 3b 0a 20 20 20 20 7a 20 2d 3d 20 70 48 64 72 2d  ;.    z -= pHdr-
88430 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73  >nBacktraceSlots
88440 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 20 2b  *sizeof(void*) +
88450 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 3b 0a 20   pHdr->nTitle;. 
88460 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
88470 22 2a 2a 2a 2a 20 25 6c 6c 64 20 62 79 74 65 73  "**** %lld bytes
88480 20 61 74 20 25 70 20 66 72 6f 6d 20 25 73 20 2a   at %p from %s *
88490 2a 2a 2a 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20  ***\n", .       
884a0 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65       pHdr->iSize
884b0 2c 20 26 70 48 64 72 5b 31 5d 2c 20 70 48 64 72  , &pHdr[1], pHdr
884c0 2d 3e 6e 54 69 74 6c 65 20 3f 20 7a 20 3a 20 22  ->nTitle ? z : "
884d0 3f 3f 3f 22 29 3b 0a 20 20 20 20 69 66 28 20 70  ???");.    if( p
884e0 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20  Hdr->nBacktrace 
884f0 29 7b 0a 20 20 20 20 20 20 66 66 6c 75 73 68 28  ){.      fflush(
88500 6f 75 74 29 3b 0a 20 20 20 20 20 20 70 42 74 20  out);.      pBt 
88510 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b 0a  = (void**)pHdr;.
88520 20 20 20 20 20 20 70 42 74 20 2d 3d 20 70 48 64        pBt -= pHd
88530 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f  r->nBacktraceSlo
88540 74 73 3b 0a 20 20 20 20 20 20 62 61 63 6b 74 72  ts;.      backtr
88550 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 70  ace_symbols_fd(p
88560 42 74 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74  Bt, pHdr->nBackt
88570 72 61 63 65 2c 20 66 69 6c 65 6e 6f 28 6f 75 74  race, fileno(out
88580 29 29 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74  ));.      fprint
88590 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a 20 20  f(out, "\n");.  
885a0 20 20 7d 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74    }.  }.  fprint
885b0 66 28 6f 75 74 2c 20 22 43 4f 55 4e 54 53 3a 5c  f(out, "COUNTS:\
885c0 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  n");.  for(i=0; 
885d0 69 3c 4e 43 53 49 5a 45 2d 31 3b 20 69 2b 2b 29  i<NCSIZE-1; i++)
885e0 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 41  {.    if( mem.nA
885f0 6c 6c 6f 63 5b 69 5d 20 29 7b 0a 20 20 20 20 20  lloc[i] ){.     
88600 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20   fprintf(out, " 
88610 20 20 25 35 64 3a 20 25 31 30 64 20 25 31 30 64    %5d: %10d %10d
88620 20 25 31 30 64 5c 6e 22 2c 20 0a 20 20 20 20 20   %10d\n", .     
88630 20 20 20 20 20 20 20 69 2a 38 2c 20 6d 65 6d 2e         i*8, mem.
88640 6e 41 6c 6c 6f 63 5b 69 5d 2c 20 6d 65 6d 2e 6e  nAlloc[i], mem.n
88650 43 75 72 72 65 6e 74 5b 69 5d 2c 20 6d 65 6d 2e  Current[i], mem.
88660 6d 78 43 75 72 72 65 6e 74 5b 69 5d 29 3b 0a 20  mxCurrent[i]);. 
88670 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6d     }.  }.  if( m
88680 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45  em.nAlloc[NCSIZE
88690 2d 31 5d 20 29 7b 0a 20 20 20 20 66 70 72 69 6e  -1] ){.    fprin
886a0 74 66 28 6f 75 74 2c 20 22 20 20 20 25 35 64 3a  tf(out, "   %5d:
886b0 20 25 31 30 64 20 25 31 30 64 20 25 31 30 64 5c   %10d %10d %10d\
886c0 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
886d0 20 4e 43 53 49 5a 45 2a 38 2d 38 2c 20 6d 65 6d   NCSIZE*8-8, mem
886e0 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d 31  .nAlloc[NCSIZE-1
886f0 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ],.             
88700 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 4e 43 53  mem.nCurrent[NCS
88710 49 5a 45 2d 31 5d 2c 20 6d 65 6d 2e 6d 78 43 75  IZE-1], mem.mxCu
88720 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d 31 5d 29  rrent[NCSIZE-1])
88730 3b 0a 20 20 7d 0a 20 20 66 63 6c 6f 73 65 28 6f  ;.  }.  fclose(o
88740 75 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ut);.}../*.** Re
88750 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
88760 6f 66 20 74 69 6d 65 73 20 73 71 6c 69 74 65 33  of times sqlite3
88770 4d 65 6d 4d 61 6c 6c 6f 63 28 29 20 68 61 73 20  MemMalloc() has 
88780 62 65 65 6e 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a  been called..*/.
88790 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
887a0 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62  nt sqlite3Memdeb
887b0 75 67 4d 61 6c 6c 6f 63 43 6f 75 6e 74 28 29 7b  ugMallocCount(){
887c0 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
887d0 6e 54 6f 74 61 6c 20 3d 20 30 3b 0a 20 20 66 6f  nTotal = 0;.  fo
887e0 72 28 69 3d 30 3b 20 69 3c 4e 43 53 49 5a 45 3b  r(i=0; i<NCSIZE;
887f0 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 54 6f 74 61   i++){.    nTota
88800 6c 20 2b 3d 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b  l += mem.nAlloc[
88810 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  i];.  }.  return
88820 20 6e 54 6f 74 61 6c 3b 0a 7d 0a 0a 0a 23 65 6e   nTotal;.}...#en
88830 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 45  dif /* SQLITE_ME
88840 4d 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  MDEBUG */../****
88850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
88860 66 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f mem2.c *******
88870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
888a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
888b0 20 66 69 6c 65 20 6d 65 6d 33 2e 63 20 2a 2a 2a   file mem3.c ***
888c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
888d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
888e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
888f0 20 32 30 30 37 20 4f 63 74 6f 62 65 72 20 31 34   2007 October 14
88900 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
88910 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
88920 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
88930 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
88940 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
88950 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
88960 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
88970 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
88980 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
88990 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
889a0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
889b0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
889c0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
889d0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
889e0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
889f0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
88a00 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
88a10 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
88a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
88a60 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
88a70 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63  tains the C func
88a80 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65  tions that imple
88a90 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a  ment a memory.**
88aa0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73   allocation subs
88ab0 79 73 74 65 6d 20 66 6f 72 20 75 73 65 20 62 79  ystem for use by
88ac0 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a 2a 20   SQLite. .**.** 
88ad0 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20  This version of 
88ae0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
88af0 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20  ation subsystem 
88b00 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 75 73 65  omits all.** use
88b10 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e 20 54 68   of malloc(). Th
88b20 65 20 53 51 4c 69 74 65 20 75 73 65 72 20 73 75  e SQLite user su
88b30 70 70 6c 69 65 73 20 61 20 62 6c 6f 63 6b 20 6f  pplies a block o
88b40 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f  f memory.** befo
88b50 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74  re calling sqlit
88b60 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
88b70 66 72 6f 6d 20 77 68 69 63 68 20 61 6c 6c 6f 63  from which alloc
88b80 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61  ations.** are ma
88b90 64 65 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20  de and returned 
88ba0 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29  by the xMalloc()
88bb0 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 28 29 20   and xRealloc() 
88bc0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
88bd0 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c 69 74 65  ons. Once sqlite
88be0 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 68  3_initialize() h
88bf0 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 0a  as been called,.
88c00 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66  ** the amount of
88c10 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c   memory availabl
88c20 65 20 74 6f 20 53 51 4c 69 74 65 20 69 73 20 66  e to SQLite is f
88c30 69 78 65 64 20 61 6e 64 20 63 61 6e 6e 6f 74 0a  ixed and cannot.
88c40 2a 2a 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a  ** be changed..*
88c50 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f  *.** This versio
88c60 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20  n of the memory 
88c70 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
88c80 73 74 65 6d 20 69 73 20 69 6e 63 6c 75 64 65 64  stem is included
88c90 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75 69 6c 64  .** in the build
88ca0 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54 45 5f   only if SQLITE_
88cb0 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 69  ENABLE_MEMSYS3 i
88cc0 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a  s defined..**.**
88cd0 20 24 49 64 3a 20 6d 65 6d 33 2e 63 2c 76 20 31   $Id: mem3.c,v 1
88ce0 2e 32 33 20 32 30 30 38 2f 30 39 2f 30 32 20 31  .23 2008/09/02 1
88cf0 37 3a 35 32 3a 35 32 20 64 61 6e 69 65 6c 6b 31  7:52:52 danielk1
88d00 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  977 Exp $.*/../*
88d10 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e  .** This version
88d20 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   of the memory a
88d30 6c 6c 6f 63 61 74 6f 72 20 69 73 20 6f 6e 6c 79  llocator is only
88d40 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20   built into the 
88d50 6c 69 62 72 61 72 79 0a 2a 2a 20 53 51 4c 49 54  library.** SQLIT
88d60 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33  E_ENABLE_MEMSYS3
88d70 20 69 73 20 64 65 66 69 6e 65 64 2e 20 44 65 66   is defined. Def
88d80 69 6e 69 6e 67 20 74 68 69 73 20 73 79 6d 62 6f  ining this symbo
88d90 6c 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6d 65  l does not.** me
88da0 61 6e 20 74 68 61 74 20 74 68 65 20 6c 69 62 72  an that the libr
88db0 61 72 79 20 77 69 6c 6c 20 75 73 65 20 61 20 6d  ary will use a m
88dc0 65 6d 6f 72 79 2d 70 6f 6f 6c 20 62 79 20 64 65  emory-pool by de
88dd0 66 61 75 6c 74 2c 20 6a 75 73 74 20 74 68 61 74  fault, just that
88de0 0a 2a 2a 20 69 74 20 69 73 20 61 76 61 69 6c 61  .** it is availa
88df0 62 6c 65 2e 20 54 68 65 20 6d 65 6d 70 6f 6f 6c  ble. The mempool
88e00 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 61 63   allocator is ac
88e10 74 69 76 61 74 65 64 20 62 79 20 63 61 6c 6c 69  tivated by calli
88e20 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f  ng.** sqlite3_co
88e30 6e 66 69 67 28 29 2e 0a 2a 2f 0a 23 69 66 64 65  nfig()..*/.#ifde
88e40 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
88e50 4d 45 4d 53 59 53 33 0a 0a 2f 2a 0a 2a 2a 20 4d  MEMSYS3../*.** M
88e60 61 78 69 6d 75 6d 20 73 69 7a 65 20 28 69 6e 20  aximum size (in 
88e70 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 6f 66 20 61  Mem3Blocks) of a
88e80 20 22 73 6d 61 6c 6c 22 20 63 68 75 6e 6b 2e 0a   "small" chunk..
88e90 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 53 4d  */.#define MX_SM
88ea0 41 4c 4c 20 31 30 0a 0a 0a 2f 2a 0a 2a 2a 20 4e  ALL 10.../*.** N
88eb0 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c 69 73  umber of freelis
88ec0 74 20 68 61 73 68 20 73 6c 6f 74 73 0a 2a 2f 0a  t hash slots.*/.
88ed0 23 64 65 66 69 6e 65 20 4e 5f 48 41 53 48 20 20  #define N_HASH  
88ee0 36 31 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 65 6d 6f  61../*.** A memo
88ef0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28 61  ry allocation (a
88f00 6c 73 6f 20 63 61 6c 6c 65 64 20 61 20 22 63 68  lso called a "ch
88f10 75 6e 6b 22 29 20 63 6f 6e 73 69 73 74 73 20 6f  unk") consists o
88f20 66 20 74 77 6f 20 6f 72 20 0a 2a 2a 20 6d 6f 72  f two or .** mor
88f30 65 20 62 6c 6f 63 6b 73 20 77 68 65 72 65 20 65  e blocks where e
88f40 61 63 68 20 62 6c 6f 63 6b 20 69 73 20 38 20 62  ach block is 8 b
88f50 79 74 65 73 2e 20 20 54 68 65 20 66 69 72 73 74  ytes.  The first
88f60 20 38 20 62 79 74 65 73 20 61 72 65 20 0a 2a 2a   8 bytes are .**
88f70 20 61 20 68 65 61 64 65 72 20 74 68 61 74 20 69   a header that i
88f80 73 20 6e 6f 74 20 72 65 74 75 72 6e 65 64 20 74  s not returned t
88f90 6f 20 74 68 65 20 75 73 65 72 2e 0a 2a 2a 0a 2a  o the user..**.*
88fa0 2a 20 41 20 63 68 75 6e 6b 20 69 73 20 74 77 6f  * A chunk is two
88fb0 20 6f 72 20 6d 6f 72 65 20 62 6c 6f 63 6b 73 20   or more blocks 
88fc0 74 68 61 74 20 69 73 20 65 69 74 68 65 72 20 63  that is either c
88fd0 68 65 63 6b 65 64 20 6f 75 74 20 6f 72 0a 2a 2a  hecked out or.**
88fe0 20 66 72 65 65 2e 20 20 54 68 65 20 66 69 72 73   free.  The firs
88ff0 74 20 62 6c 6f 63 6b 20 68 61 73 20 66 6f 72 6d  t block has form
89000 61 74 20 75 2e 68 64 72 2e 20 20 75 2e 68 64 72  at u.hdr.  u.hdr
89010 2e 73 69 7a 65 34 78 20 69 73 20 34 20 74 69 6d  .size4x is 4 tim
89020 65 73 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f  es the.** size o
89030 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  f the allocation
89040 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20 74 68   in blocks if th
89050 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20  e allocation is 
89060 66 72 65 65 2e 0a 2a 2a 20 54 68 65 20 75 2e 68  free..** The u.h
89070 64 72 2e 73 69 7a 65 34 78 26 31 20 62 69 74 20  dr.size4x&1 bit 
89080 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20 63  is true if the c
89090 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20  hunk is checked 
890a0 6f 75 74 20 61 6e 64 0a 2a 2a 20 66 61 6c 73 65  out and.** false
890b0 20 69 66 20 74 68 65 20 63 68 75 6e 6b 20 69 73   if the chunk is
890c0 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74   on the freelist
890d0 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 73 69 7a  .  The u.hdr.siz
890e0 65 34 78 26 32 20 62 69 74 0a 2a 2a 20 69 73 20  e4x&2 bit.** is 
890f0 74 72 75 65 20 69 66 20 74 68 65 20 70 72 65 76  true if the prev
89100 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 20 63 68  ious chunk is ch
89110 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 20 66 61  ecked out and fa
89120 6c 73 65 20 69 66 20 74 68 65 0a 2a 2a 20 70 72  lse if the.** pr
89130 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 20  evious chunk is 
89140 66 72 65 65 2e 20 20 54 68 65 20 75 2e 68 64 72  free.  The u.hdr
89150 2e 70 72 65 76 53 69 7a 65 20 66 69 65 6c 64 20  .prevSize field 
89160 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a  is the size of.*
89170 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63  * the previous c
89180 68 75 6e 6b 20 69 6e 20 62 6c 6f 63 6b 73 20 69  hunk in blocks i
89190 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63  f the previous c
891a0 68 75 6e 6b 20 69 73 20 6f 6e 20 74 68 65 0a 2a  hunk is on the.*
891b0 2a 20 66 72 65 65 6c 69 73 74 2e 20 49 66 20 74  * freelist. If t
891c0 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e  he previous chun
891d0 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74  k is checked out
891e0 2c 20 74 68 65 6e 0a 2a 2a 20 75 2e 68 64 72 2e  , then.** u.hdr.
891f0 70 72 65 76 53 69 7a 65 20 63 61 6e 20 62 65 20  prevSize can be 
89200 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61  part of the data
89210 20 66 6f 72 20 74 68 61 74 20 63 68 75 6e 6b 20   for that chunk 
89220 61 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 6e 6f  and should.** no
89230 74 20 62 65 20 72 65 61 64 20 6f 72 20 77 72 69  t be read or wri
89240 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 6f  tten..**.** We o
89250 66 74 65 6e 20 69 64 65 6e 74 69 66 79 20 61 20  ften identify a 
89260 63 68 75 6e 6b 20 62 79 20 69 74 73 20 69 6e 64  chunk by its ind
89270 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c  ex in mem3.aPool
89280 5b 5d 2e 20 20 57 68 65 6e 0a 2a 2a 20 74 68 69  [].  When.** thi
89290 73 20 69 73 20 64 6f 6e 65 2c 20 74 68 65 20 63  s is done, the c
892a0 68 75 6e 6b 20 69 6e 64 65 78 20 72 65 66 65 72  hunk index refer
892b0 73 20 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20  s to the second 
892c0 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 74 68 65 20  block of.** the 
892d0 63 68 75 6e 6b 2e 20 20 49 6e 20 74 68 69 73 20  chunk.  In this 
892e0 77 61 79 2c 20 74 68 65 20 66 69 72 73 74 20 63  way, the first c
892f0 68 75 6e 6b 20 68 61 73 20 61 6e 20 69 6e 64 65  hunk has an inde
89300 78 20 6f 66 20 31 2e 0a 2a 2a 20 41 20 63 68 75  x of 1..** A chu
89310 6e 6b 20 69 6e 64 65 78 20 6f 66 20 30 20 6d 65  nk index of 0 me
89320 61 6e 73 20 22 6e 6f 20 73 75 63 68 20 63 68 75  ans "no such chu
89330 6e 6b 22 20 61 6e 64 20 69 73 20 74 68 65 20 65  nk" and is the e
89340 71 75 69 76 61 6c 65 6e 74 0a 2a 2a 20 6f 66 20  quivalent.** of 
89350 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a  a NULL pointer..
89360 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64  **.** The second
89370 20 62 6c 6f 63 6b 20 6f 66 20 66 72 65 65 20 63   block of free c
89380 68 75 6e 6b 73 20 69 73 20 6f 66 20 74 68 65 20  hunks is of the 
89390 66 6f 72 6d 20 75 2e 6c 69 73 74 2e 20 20 54 68  form u.list.  Th
893a0 65 0a 2a 2a 20 74 77 6f 20 66 69 65 6c 64 73 20  e.** two fields 
893b0 66 6f 72 6d 20 61 20 64 6f 75 62 6c 65 2d 6c 69  form a double-li
893c0 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 63 68 75  nked list of chu
893d0 6e 6b 73 20 6f 66 20 72 65 6c 61 74 65 64 20 73  nks of related s
893e0 69 7a 65 73 2e 0a 2a 2a 20 50 6f 69 6e 74 65 72  izes..** Pointer
893f0 73 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66  s to the head of
89400 20 74 68 65 20 6c 69 73 74 20 61 72 65 20 73 74   the list are st
89410 6f 72 65 64 20 69 6e 20 6d 65 6d 33 2e 61 69 53  ored in mem3.aiS
89420 6d 61 6c 6c 5b 5d 20 0a 2a 2a 20 66 6f 72 20 73  mall[] .** for s
89430 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73 20 61 6e  maller chunks an
89440 64 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 5d 20  d mem3.aiHash[] 
89450 66 6f 72 20 6c 61 72 67 65 72 20 63 68 75 6e 6b  for larger chunk
89460 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63  s..**.** The sec
89470 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 61 20 63  ond block of a c
89480 68 75 6e 6b 20 69 73 20 75 73 65 72 20 64 61 74  hunk is user dat
89490 61 20 69 66 20 74 68 65 20 63 68 75 6e 6b 20 69  a if the chunk i
894a0 73 20 63 68 65 63 6b 65 64 20 0a 2a 2a 20 6f 75  s checked .** ou
894b0 74 2e 20 20 49 66 20 61 20 63 68 75 6e 6b 20 69  t.  If a chunk i
894c0 73 20 63 68 65 63 6b 65 64 20 6f 75 74 2c 20 74  s checked out, t
894d0 68 65 20 75 73 65 72 20 64 61 74 61 20 6d 61 79  he user data may
894e0 20 65 78 74 65 6e 64 20 69 6e 74 6f 0a 2a 2a 20   extend into.** 
894f0 74 68 65 20 75 2e 68 64 72 2e 70 72 65 76 53 69  the u.hdr.prevSi
89500 7a 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  ze value of the 
89510 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 75 6e 6b 2e  following chunk.
89520 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
89530 63 74 20 4d 65 6d 33 42 6c 6f 63 6b 20 4d 65 6d  ct Mem3Block Mem
89540 33 42 6c 6f 63 6b 3b 0a 73 74 72 75 63 74 20 4d  3Block;.struct M
89550 65 6d 33 42 6c 6f 63 6b 20 7b 0a 20 20 75 6e 69  em3Block {.  uni
89560 6f 6e 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20  on {.    struct 
89570 7b 0a 20 20 20 20 20 20 75 33 32 20 70 72 65 76  {.      u32 prev
89580 53 69 7a 65 3b 20 20 20 2f 2a 20 53 69 7a 65 20  Size;   /* Size 
89590 6f 66 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e  of previous chun
895a0 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 20 65  k in Mem3Block e
895b0 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20  lements */.     
895c0 20 75 33 32 20 73 69 7a 65 34 78 3b 20 20 20 20   u32 size4x;    
895d0 20 2f 2a 20 34 78 20 74 68 65 20 73 69 7a 65 20   /* 4x the size 
895e0 6f 66 20 63 75 72 72 65 6e 74 20 63 68 75 6e 6b  of current chunk
895f0 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c   in Mem3Block el
89600 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 7d 20  ements */.    } 
89610 68 64 72 3b 0a 20 20 20 20 73 74 72 75 63 74 20  hdr;.    struct 
89620 7b 0a 20 20 20 20 20 20 75 33 32 20 6e 65 78 74  {.      u32 next
89630 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78  ;       /* Index
89640 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d   in mem3.aPool[]
89650 20 6f 66 20 6e 65 78 74 20 66 72 65 65 20 63 68   of next free ch
89660 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 75 33 32  unk */.      u32
89670 20 70 72 65 76 3b 20 20 20 20 20 20 20 2f 2a 20   prev;       /* 
89680 49 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50  Index in mem3.aP
89690 6f 6f 6c 5b 5d 20 6f 66 20 70 72 65 76 69 6f 75  ool[] of previou
896a0 73 20 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a  s free chunk */.
896b0 20 20 20 20 7d 20 6c 69 73 74 3b 0a 20 20 7d 20      } list;.  } 
896c0 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  u;.};../*.** All
896d0 20 6f 66 20 74 68 65 20 73 74 61 74 69 63 20 76   of the static v
896e0 61 72 69 61 62 6c 65 73 20 75 73 65 64 20 62 79  ariables used by
896f0 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 72 65   this module are
89700 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 69 6e   collected.** in
89710 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74 72 75  to a single stru
89720 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d 65 6d  cture named "mem
89730 33 22 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20  3".  This is to 
89740 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 61 74  keep the.** stat
89750 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f 72 67  ic variables org
89760 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 72 65  anized and to re
89770 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65 20 70  duce namespace p
89780 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e  ollution.** when
89790 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20   this module is 
897a0 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 6f 74  combined with ot
897b0 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61 6c 67  her in the amalg
897c0 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  amation..*/.stat
897d0 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74  ic SQLITE_WSD st
897e0 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62 61 6c 20  ruct Mem3Global 
897f0 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 65 6d 6f  {.  /*.  ** Memo
89800 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72  ry available for
89810 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 6e 50 6f   allocation. nPo
89820 6f 6c 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f  ol is the size o
89830 66 20 74 68 65 20 61 72 72 61 79 0a 20 20 2a 2a  f the array.  **
89840 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29   (in Mem3Blocks)
89850 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 61   pointed to by a
89860 50 6f 6f 6c 20 6c 65 73 73 20 32 2e 0a 20 20 2a  Pool less 2..  *
89870 2f 0a 20 20 75 33 32 20 6e 50 6f 6f 6c 3b 0a 20  /.  u32 nPool;. 
89880 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 61 50 6f 6f   Mem3Block *aPoo
89890 6c 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 72  l;..  /*.  ** Tr
898a0 75 65 20 69 66 20 77 65 20 61 72 65 20 65 76 61  ue if we are eva
898b0 6c 75 61 74 69 6e 67 20 61 6e 20 6f 75 74 2d 6f  luating an out-o
898c0 66 2d 6d 65 6d 6f 72 79 20 63 61 6c 6c 62 61 63  f-memory callbac
898d0 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 61 6c  k..  */.  int al
898e0 61 72 6d 42 75 73 79 3b 0a 20 20 0a 20 20 2f 2a  armBusy;.  .  /*
898f0 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63  .  ** Mutex to c
89900 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 74 6f  ontrol access to
89910 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
89920 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d  cation subsystem
89930 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
89940 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 20  _mutex *mutex;. 
89950 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20   .  /*.  ** The 
89960 6d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f  minimum amount o
89970 66 20 66 72 65 65 20 73 70 61 63 65 20 74 68 61  f free space tha
89980 74 20 77 65 20 68 61 76 65 20 73 65 65 6e 2e 0a  t we have seen..
89990 20 20 2a 2f 0a 20 20 75 33 32 20 6d 6e 4d 61 73    */.  u32 mnMas
899a0 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  ter;..  /*.  ** 
899b0 69 4d 61 73 74 65 72 20 69 73 20 74 68 65 20 69  iMaster is the i
899c0 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61 73 74  ndex of the mast
899d0 65 72 20 63 68 75 6e 6b 2e 20 20 4d 6f 73 74 20  er chunk.  Most 
899e0 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a  new allocations.
899f0 20 20 2a 2a 20 6f 63 63 75 72 20 6f 66 66 20 6f    ** occur off o
89a00 66 20 74 68 69 73 20 63 68 75 6e 6b 2e 20 20 73  f this chunk.  s
89a10 7a 4d 61 73 74 65 72 20 69 73 20 74 68 65 20 73  zMaster is the s
89a20 69 7a 65 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63  ize (in Mem3Bloc
89a30 6b 73 29 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  ks).  ** of the 
89a40 63 75 72 72 65 6e 74 20 6d 61 73 74 65 72 2e 20  current master. 
89a50 20 69 4d 61 73 74 65 72 20 69 73 20 30 20 69 66   iMaster is 0 if
89a60 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 6d 61   there is not ma
89a70 73 74 65 72 20 63 68 75 6e 6b 2e 0a 20 20 2a 2a  ster chunk..  **
89a80 20 54 68 65 20 6d 61 73 74 65 72 20 63 68 75 6e   The master chun
89a90 6b 20 69 73 20 6e 6f 74 20 69 6e 20 65 69 74 68  k is not in eith
89aa0 65 72 20 74 68 65 20 61 69 48 61 73 68 5b 5d 20  er the aiHash[] 
89ab0 6f 72 20 61 69 53 6d 61 6c 6c 5b 5d 2e 0a 20 20  or aiSmall[]..  
89ac0 2a 2f 0a 20 20 75 33 32 20 69 4d 61 73 74 65 72  */.  u32 iMaster
89ad0 3b 0a 20 20 75 33 32 20 73 7a 4d 61 73 74 65 72  ;.  u32 szMaster
89ae0 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 72 72  ;..  /*.  ** Arr
89af0 61 79 20 6f 66 20 6c 69 73 74 73 20 6f 66 20 66  ay of lists of f
89b00 72 65 65 20 62 6c 6f 63 6b 73 20 61 63 63 6f 72  ree blocks accor
89b10 64 69 6e 67 20 74 6f 20 74 68 65 20 62 6c 6f 63  ding to the bloc
89b20 6b 20 73 69 7a 65 20 0a 20 20 2a 2a 20 66 6f 72  k size .  ** for
89b30 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73 2c   smaller chunks,
89b40 20 6f 72 20 61 20 68 61 73 68 20 6f 6e 20 74 68   or a hash on th
89b50 65 20 62 6c 6f 63 6b 20 73 69 7a 65 20 66 6f 72  e block size for
89b60 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20 63 68 75   larger.  ** chu
89b70 6e 6b 73 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20  nks..  */.  u32 
89b80 61 69 53 6d 61 6c 6c 5b 4d 58 5f 53 4d 41 4c 4c  aiSmall[MX_SMALL
89b90 2d 31 5d 3b 20 20 20 2f 2a 20 46 6f 72 20 73 69  -1];   /* For si
89ba0 7a 65 73 20 32 20 74 68 72 6f 75 67 68 20 4d 58  zes 2 through MX
89bb0 5f 53 4d 41 4c 4c 2c 20 69 6e 63 6c 75 73 69 76  _SMALL, inclusiv
89bc0 65 20 2a 2f 0a 20 20 75 33 32 20 61 69 48 61 73  e */.  u32 aiHas
89bd0 68 5b 4e 5f 48 41 53 48 5d 3b 20 20 20 20 20 20  h[N_HASH];      
89be0 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 73 20 4d    /* For sizes M
89bf0 58 5f 53 4d 41 4c 4c 2b 31 20 61 6e 64 20 6c 61  X_SMALL+1 and la
89c00 72 67 65 72 20 2a 2f 0a 7d 20 6d 65 6d 33 20 3d  rger */.} mem3 =
89c10 20 7b 20 39 37 35 33 35 35 37 35 20 7d 3b 0a 0a   { 97535575 };..
89c20 23 64 65 66 69 6e 65 20 6d 65 6d 33 20 47 4c 4f  #define mem3 GLO
89c30 42 41 4c 28 73 74 72 75 63 74 20 4d 65 6d 33 47  BAL(struct Mem3G
89c40 6c 6f 62 61 6c 2c 20 6d 65 6d 33 29 0a 0a 2f 2a  lobal, mem3)../*
89c50 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63  .** Unlink the c
89c60 68 75 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 50 6f  hunk at mem3.aPo
89c70 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 73 74 20  ol[i] from list 
89c80 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a  it is currently.
89c90 2a 2a 20 6f 6e 2e 20 20 2a 70 52 6f 6f 74 20 69  ** on.  *pRoot i
89ca0 73 20 74 68 65 20 6c 69 73 74 20 74 68 61 74 20  s the list that 
89cb0 69 20 69 73 20 61 20 6d 65 6d 62 65 72 20 6f 66  i is a member of
89cc0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
89cd0 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72   memsys3UnlinkFr
89ce0 6f 6d 4c 69 73 74 28 75 33 32 20 69 2c 20 75 33  omList(u32 i, u3
89cf0 32 20 2a 70 52 6f 6f 74 29 7b 0a 20 20 75 33 32  2 *pRoot){.  u32
89d00 20 6e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50 6f   next = mem3.aPo
89d10 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78  ol[i].u.list.nex
89d20 74 3b 0a 20 20 75 33 32 20 70 72 65 76 20 3d 20  t;.  u32 prev = 
89d30 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e  mem3.aPool[i].u.
89d40 6c 69 73 74 2e 70 72 65 76 3b 0a 20 20 61 73 73  list.prev;.  ass
89d50 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
89d60 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74  ex_held(mem3.mut
89d70 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 72 65  ex) );.  if( pre
89d80 76 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 52 6f  v==0 ){.    *pRo
89d90 6f 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 65 6c  ot = next;.  }el
89da0 73 65 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f  se{.    mem3.aPo
89db0 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 74 2e  ol[prev].u.list.
89dc0 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d  next = next;.  }
89dd0 0a 20 20 69 66 28 20 6e 65 78 74 20 29 7b 0a 20  .  if( next ){. 
89de0 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65     mem3.aPool[ne
89df0 78 74 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20  xt].u.list.prev 
89e00 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 20 20 6d 65  = prev;.  }.  me
89e10 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69  m3.aPool[i].u.li
89e20 73 74 2e 6e 65 78 74 20 3d 20 30 3b 0a 20 20 6d  st.next = 0;.  m
89e30 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c  em3.aPool[i].u.l
89e40 69 73 74 2e 70 72 65 76 20 3d 20 30 3b 0a 7d 0a  ist.prev = 0;.}.
89e50 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68  ./*.** Unlink th
89e60 65 20 63 68 75 6e 6b 20 61 74 20 69 6e 64 65 78  e chunk at index
89e70 20 69 20 66 72 6f 6d 20 0a 2a 2a 20 77 68 61 74   i from .** what
89e80 65 76 65 72 20 6c 69 73 74 20 69 73 20 63 75 72  ever list is cur
89e90 72 65 6e 74 6c 79 20 61 20 6d 65 6d 62 65 72 20  rently a member 
89ea0 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  of..*/.static vo
89eb0 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b  id memsys3Unlink
89ec0 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32 20 73  (u32 i){.  u32 s
89ed0 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61 73 73  ize, hash;.  ass
89ee0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
89ef0 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74  ex_held(mem3.mut
89f00 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
89f10 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31   (mem3.aPool[i-1
89f20 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26  ].u.hdr.size4x &
89f30 20 31 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65   1)==0 );.  asse
89f40 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 73 69  rt( i>=1 );.  si
89f50 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  ze = mem3.aPool[
89f60 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  i-1].u.hdr.size4
89f70 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 20 73  x/4;.  assert( s
89f80 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  ize==mem3.aPool[
89f90 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e  i+size-1].u.hdr.
89fa0 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 61 73  prevSize );.  as
89fb0 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 20 29 3b  sert( size>=2 );
89fc0 0a 20 20 69 66 28 20 73 69 7a 65 20 3c 3d 20 4d  .  if( size <= M
89fd0 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 20 6d  X_SMALL ){.    m
89fe0 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d  emsys3UnlinkFrom
89ff0 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69  List(i, &mem3.ai
8a000 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a  Small[size-2]);.
8a010 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 61 73    }else{.    has
8a020 68 20 3d 20 73 69 7a 65 20 25 20 4e 5f 48 41 53  h = size % N_HAS
8a030 48 3b 0a 20 20 20 20 6d 65 6d 73 79 73 33 55 6e  H;.    memsys3Un
8a040 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20  linkFromList(i, 
8a050 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 61 73  &mem3.aiHash[has
8a060 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  h]);.  }.}../*.*
8a070 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b  * Link the chunk
8a080 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69   at mem3.aPool[i
8a090 5d 20 73 6f 20 74 68 61 74 20 69 73 20 6f 6e 20  ] so that is on 
8a0a0 74 68 65 20 6c 69 73 74 20 72 6f 6f 74 65 64 0a  the list rooted.
8a0b0 2a 2a 20 61 74 20 2a 70 52 6f 6f 74 2e 0a 2a 2f  ** at *pRoot..*/
8a0c0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
8a0d0 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74  sys3LinkIntoList
8a0e0 28 75 33 32 20 69 2c 20 75 33 32 20 2a 70 52 6f  (u32 i, u32 *pRo
8a0f0 6f 74 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73  ot){.  assert( s
8a100 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
8a110 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b  d(mem3.mutex) );
8a120 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d  .  mem3.aPool[i]
8a130 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 20 3d 20 2a  .u.list.next = *
8a140 70 52 6f 6f 74 3b 0a 20 20 6d 65 6d 33 2e 61 50  pRoot;.  mem3.aP
8a150 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 70 72  ool[i].u.list.pr
8a160 65 76 20 3d 20 30 3b 0a 20 20 69 66 28 20 2a 70  ev = 0;.  if( *p
8a170 52 6f 6f 74 20 29 7b 0a 20 20 20 20 6d 65 6d 33  Root ){.    mem3
8a180 2e 61 50 6f 6f 6c 5b 2a 70 52 6f 6f 74 5d 2e 75  .aPool[*pRoot].u
8a190 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 69 3b 0a  .list.prev = i;.
8a1a0 20 20 7d 0a 20 20 2a 70 52 6f 6f 74 20 3d 20 69    }.  *pRoot = i
8a1b0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20  ;.}../*.** Link 
8a1c0 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69 6e 64  the chunk at ind
8a1d0 65 78 20 69 20 69 6e 74 6f 20 65 69 74 68 65 72  ex i into either
8a1e0 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
8a1f0 0a 2a 2a 20 73 6d 61 6c 6c 20 63 68 75 6e 6b 20  .** small chunk 
8a200 6c 69 73 74 2c 20 6f 72 20 69 6e 74 6f 20 74 68  list, or into th
8a210 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20 68 61  e large chunk ha
8a220 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  sh table..*/.sta
8a230 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33  tic void memsys3
8a240 4c 69 6e 6b 28 75 33 32 20 69 29 7b 0a 20 20 75  Link(u32 i){.  u
8a250 33 32 20 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20  32 size, hash;. 
8a260 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
8a270 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33  _mutex_held(mem3
8a280 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  .mutex) );.  ass
8a290 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 61  ert( i>=1 );.  a
8a2a0 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61 50 6f  ssert( (mem3.aPo
8a2b0 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ol[i-1].u.hdr.si
8a2c0 7a 65 34 78 20 26 20 31 29 3d 3d 30 20 29 3b 0a  ze4x & 1)==0 );.
8a2d0 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50    size = mem3.aP
8a2e0 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73  ool[i-1].u.hdr.s
8a2f0 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72  ize4x/4;.  asser
8a300 74 28 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50  t( size==mem3.aP
8a310 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e  ool[i+size-1].u.
8a320 68 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a  hdr.prevSize );.
8a330 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 3e 3d    assert( size>=
8a340 32 20 29 3b 0a 20 20 69 66 28 20 73 69 7a 65 20  2 );.  if( size 
8a350 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20  <= MX_SMALL ){. 
8a360 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 49 6e     memsys3LinkIn
8a370 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e  toList(i, &mem3.
8a380 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29  aiSmall[size-2])
8a390 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68  ;.  }else{.    h
8a3a0 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e 5f 48  ash = size % N_H
8a3b0 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79 73 33  ASH;.    memsys3
8a3c0 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 69 2c 20  LinkIntoList(i, 
8a3d0 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 61 73  &mem3.aiHash[has
8a3e0 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  h]);.  }.}../*.*
8a3f0 2a 20 49 66 20 74 68 65 20 53 54 41 54 49 43 5f  * If the STATIC_
8a400 4d 45 4d 20 6d 75 74 65 78 20 69 73 20 6e 6f 74  MEM mutex is not
8a410 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 6f   already held, o
8a420 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e 20 54 68  btain it now. Th
8a430 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69 6c 6c 20  e mutex.** will 
8a440 61 6c 72 65 61 64 79 20 62 65 20 68 65 6c 64 20  already be held 
8a450 28 6f 62 74 61 69 6e 65 64 20 62 79 20 63 6f 64  (obtained by cod
8a460 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 29 20 69  e in malloc.c) i
8a470 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62  f.** sqlite3Glob
8a480 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 53 74 61  alConfig.bMemSta
8a490 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74  t is true..*/.st
8a4a0 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73  atic void memsys
8a4b0 33 45 6e 74 65 72 28 76 6f 69 64 29 7b 0a 20 20  3Enter(void){.  
8a4c0 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
8a4d0 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74  lConfig.bMemstat
8a4e0 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 6d 75 74 65  ==0 && mem3.mute
8a4f0 78 3d 3d 30 20 29 7b 0a 20 20 20 20 6d 65 6d 33  x==0 ){.    mem3
8a500 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33  .mutex = sqlite3
8a510 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
8a520 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
8a530 45 4d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  EM);.  }.  sqlit
8a540 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
8a550 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d 0a 73 74  em3.mutex);.}.st
8a560 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73  atic void memsys
8a570 33 4c 65 61 76 65 28 76 6f 69 64 29 7b 0a 20 20  3Leave(void){.  
8a580 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
8a590 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b  ave(mem3.mutex);
8a5a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64  .}../*.** Called
8a5b0 20 77 68 65 6e 20 77 65 20 61 72 65 20 75 6e 61   when we are una
8a5c0 62 6c 65 20 74 6f 20 73 61 74 69 73 66 79 20 61  ble to satisfy a
8a5d0 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20  n allocation of 
8a5e0 6e 42 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  nBytes..*/.stati
8a5f0 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4f 75  c void memsys3Ou
8a600 74 4f 66 4d 65 6d 6f 72 79 28 69 6e 74 20 6e 42  tOfMemory(int nB
8a610 79 74 65 29 7b 0a 20 20 69 66 28 20 21 6d 65 6d  yte){.  if( !mem
8a620 33 2e 61 6c 61 72 6d 42 75 73 79 20 29 7b 0a 20  3.alarmBusy ){. 
8a630 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73     mem3.alarmBus
8a640 79 20 3d 20 31 3b 0a 20 20 20 20 61 73 73 65 72  y = 1;.    asser
8a650 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
8a660 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78  _held(mem3.mutex
8a670 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) );.    sqlite3
8a680 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d  _mutex_leave(mem
8a690 33 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71  3.mutex);.    sq
8a6a0 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65  lite3_release_me
8a6b0 6d 6f 72 79 28 6e 42 79 74 65 29 3b 0a 20 20 20  mory(nByte);.   
8a6c0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
8a6d0 6e 74 65 72 28 6d 65 6d 33 2e 6d 75 74 65 78 29  nter(mem3.mutex)
8a6e0 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d  ;.    mem3.alarm
8a6f0 42 75 73 79 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a  Busy = 0;.  }.}.
8a700 0a 0a 2f 2a 0a 2a 2a 20 43 68 75 6e 6b 20 69 20  ../*.** Chunk i 
8a710 69 73 20 61 20 66 72 65 65 20 63 68 75 6e 6b 20  is a free chunk 
8a720 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 75 6e  that has been un
8a730 6c 69 6e 6b 65 64 2e 20 20 41 64 6a 75 73 74 20  linked.  Adjust 
8a740 69 74 73 20 0a 2a 2a 20 73 69 7a 65 20 70 61 72  its .** size par
8a750 61 6d 65 74 65 72 73 20 66 6f 72 20 63 68 65 63  ameters for chec
8a760 6b 2d 6f 75 74 20 61 6e 64 20 72 65 74 75 72 6e  k-out and return
8a770 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
8a780 65 20 0a 2a 2a 20 75 73 65 72 20 70 6f 72 74 69  e .** user porti
8a790 6f 6e 20 6f 66 20 74 68 65 20 63 68 75 6e 6b 2e  on of the chunk.
8a7a0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
8a7b0 2a 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75 74  *memsys3Checkout
8a7c0 28 75 33 32 20 69 2c 20 69 6e 74 20 6e 42 6c 6f  (u32 i, int nBlo
8a7d0 63 6b 29 7b 0a 20 20 75 33 32 20 78 3b 0a 20 20  ck){.  u32 x;.  
8a7e0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
8a7f0 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e  mutex_held(mem3.
8a800 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
8a810 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 61 73  rt( i>=1 );.  as
8a820 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c  sert( mem3.aPool
8a830 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  [i-1].u.hdr.size
8a840 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a  4x/4==nBlock );.
8a850 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 61    assert( mem3.a
8a860 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d  Pool[i+nBlock-1]
8a870 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3d  .u.hdr.prevSize=
8a880 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 78 20 3d  =nBlock );.  x =
8a890 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d   mem3.aPool[i-1]
8a8a0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20  .u.hdr.size4x;. 
8a8b0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d   mem3.aPool[i-1]
8a8c0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20  .u.hdr.size4x = 
8a8d0 6e 42 6c 6f 63 6b 2a 34 20 7c 20 31 20 7c 20 28  nBlock*4 | 1 | (
8a8e0 78 26 32 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f  x&2);.  mem3.aPo
8a8f0 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75  ol[i+nBlock-1].u
8a900 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20  .hdr.prevSize = 
8a910 6e 42 6c 6f 63 6b 3b 0a 20 20 6d 65 6d 33 2e 61  nBlock;.  mem3.a
8a920 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d  Pool[i+nBlock-1]
8a930 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d  .u.hdr.size4x |=
8a940 20 32 3b 0a 20 20 72 65 74 75 72 6e 20 26 6d 65   2;.  return &me
8a950 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 3b 0a 7d 0a 0a  m3.aPool[i];.}..
8a960 2f 2a 0a 2a 2a 20 43 61 72 76 65 20 61 20 70 69  /*.** Carve a pi
8a970 65 63 65 20 6f 66 66 20 6f 66 20 74 68 65 20 65  ece off of the e
8a980 6e 64 20 6f 66 20 74 68 65 20 6d 65 6d 33 2e 69  nd of the mem3.i
8a990 4d 61 73 74 65 72 20 66 72 65 65 20 63 68 75 6e  Master free chun
8a9a0 6b 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70  k..** Return a p
8a9b0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65  ointer to the ne
8a9c0 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 4f  w allocation.  O
8a9d0 72 2c 20 69 66 20 74 68 65 20 6d 61 73 74 65 72  r, if the master
8a9e0 20 63 68 75 6e 6b 0a 2a 2a 20 69 73 20 6e 6f 74   chunk.** is not
8a9f0 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2c 20 72   large enough, r
8aa00 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74  eturn 0..*/.stat
8aa10 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33  ic void *memsys3
8aa20 46 72 6f 6d 4d 61 73 74 65 72 28 69 6e 74 20 6e  FromMaster(int n
8aa30 42 6c 6f 63 6b 29 7b 0a 20 20 61 73 73 65 72 74  Block){.  assert
8aa40 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
8aa50 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29  held(mem3.mutex)
8aa60 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65   );.  assert( me
8aa70 6d 33 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c  m3.szMaster>=nBl
8aa80 6f 63 6b 20 29 3b 0a 20 20 69 66 28 20 6e 42 6c  ock );.  if( nBl
8aa90 6f 63 6b 3e 3d 6d 65 6d 33 2e 73 7a 4d 61 73 74  ock>=mem3.szMast
8aaa0 65 72 2d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 55  er-1 ){.    /* U
8aab0 73 65 20 74 68 65 20 65 6e 74 69 72 65 20 6d 61  se the entire ma
8aac0 73 74 65 72 20 2a 2f 0a 20 20 20 20 76 6f 69 64  ster */.    void
8aad0 20 2a 70 20 3d 20 6d 65 6d 73 79 73 33 43 68 65   *p = memsys3Che
8aae0 63 6b 6f 75 74 28 6d 65 6d 33 2e 69 4d 61 73 74  ckout(mem3.iMast
8aaf0 65 72 2c 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  er, mem3.szMaste
8ab00 72 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 69 4d 61  r);.    mem3.iMa
8ab10 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 6d 65  ster = 0;.    me
8ab20 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20 30 3b  m3.szMaster = 0;
8ab30 0a 20 20 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74  .    mem3.mnMast
8ab40 65 72 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75  er = 0;.    retu
8ab50 72 6e 20 70 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  rn p;.  }else{. 
8ab60 20 20 20 2f 2a 20 53 70 6c 69 74 20 74 68 65 20     /* Split the 
8ab70 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e 20 20 52  master block.  R
8ab80 65 74 75 72 6e 20 74 68 65 20 74 61 69 6c 2e 20  eturn the tail. 
8ab90 2a 2f 0a 20 20 20 20 75 33 32 20 6e 65 77 69 2c  */.    u32 newi,
8aba0 20 78 3b 0a 20 20 20 20 6e 65 77 69 20 3d 20 6d   x;.    newi = m
8abb0 65 6d 33 2e 69 4d 61 73 74 65 72 20 2b 20 6d 65  em3.iMaster + me
8abc0 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2d 20 6e 42  m3.szMaster - nB
8abd0 6c 6f 63 6b 3b 0a 20 20 20 20 61 73 73 65 72 74  lock;.    assert
8abe0 28 20 6e 65 77 69 20 3e 20 6d 65 6d 33 2e 69 4d  ( newi > mem3.iM
8abf0 61 73 74 65 72 2b 31 20 29 3b 0a 20 20 20 20 6d  aster+1 );.    m
8ac00 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69  em3.aPool[mem3.i
8ac10 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61  Master+mem3.szMa
8ac20 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72  ster-1].u.hdr.pr
8ac30 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f 63 6b 3b  evSize = nBlock;
8ac40 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  .    mem3.aPool[
8ac50 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d  mem3.iMaster+mem
8ac60 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e  3.szMaster-1].u.
8ac70 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20 32 3b  hdr.size4x |= 2;
8ac80 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  .    mem3.aPool[
8ac90 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  newi-1].u.hdr.si
8aca0 7a 65 34 78 20 3d 20 6e 42 6c 6f 63 6b 2a 34 20  ze4x = nBlock*4 
8acb0 2b 20 31 3b 0a 20 20 20 20 6d 65 6d 33 2e 73 7a  + 1;.    mem3.sz
8acc0 4d 61 73 74 65 72 20 2d 3d 20 6e 42 6c 6f 63 6b  Master -= nBlock
8acd0 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c  ;.    mem3.aPool
8ace0 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72 2e 70  [newi-1].u.hdr.p
8acf0 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73  revSize = mem3.s
8ad00 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 78 20 3d  zMaster;.    x =
8ad10 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33   mem3.aPool[mem3
8ad20 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64  .iMaster-1].u.hd
8ad30 72 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a 20 20  r.size4x & 2;.  
8ad40 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d    mem3.aPool[mem
8ad50 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68  3.iMaster-1].u.h
8ad60 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d 65 6d 33  dr.size4x = mem3
8ad70 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c 20 78 3b  .szMaster*4 | x;
8ad80 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a  .    if( mem3.sz
8ad90 4d 61 73 74 65 72 20 3c 20 6d 65 6d 33 2e 6d 6e  Master < mem3.mn
8ada0 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20  Master ){.      
8adb0 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 3d 20  mem3.mnMaster = 
8adc0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20  mem3.szMaster;. 
8add0 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
8ade0 28 76 6f 69 64 2a 29 26 6d 65 6d 33 2e 61 50 6f  (void*)&mem3.aPo
8adf0 6f 6c 5b 6e 65 77 69 5d 3b 0a 20 20 7d 0a 7d 0a  ol[newi];.  }.}.
8ae00 0a 2f 2a 0a 2a 2a 20 2a 70 52 6f 6f 74 20 69 73  ./*.** *pRoot is
8ae10 20 74 68 65 20 68 65 61 64 20 6f 66 20 61 20 6c   the head of a l
8ae20 69 73 74 20 6f 66 20 66 72 65 65 20 63 68 75 6e  ist of free chun
8ae30 6b 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 73  ks of the same s
8ae40 69 7a 65 0a 2a 2a 20 6f 72 20 73 61 6d 65 20 73  ize.** or same s
8ae50 69 7a 65 20 68 61 73 68 2e 20 20 49 6e 20 6f 74  ize hash.  In ot
8ae60 68 65 72 20 77 6f 72 64 73 2c 20 2a 70 52 6f 6f  her words, *pRoo
8ae70 74 20 69 73 20 61 6e 20 65 6e 74 72 79 20 69 6e  t is an entry in
8ae80 20 65 69 74 68 65 72 0a 2a 2a 20 6d 65 6d 33 2e   either.** mem3.
8ae90 61 69 53 6d 61 6c 6c 5b 5d 20 6f 72 20 6d 65 6d  aiSmall[] or mem
8aea0 33 2e 61 69 48 61 73 68 5b 5d 2e 20 20 0a 2a 2a  3.aiHash[].  .**
8aeb0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
8aec0 20 65 78 61 6d 69 6e 65 73 20 61 6c 6c 20 65 6e   examines all en
8aed0 74 72 69 65 73 20 6f 6e 20 74 68 65 20 67 69 76  tries on the giv
8aee0 65 6e 20 6c 69 73 74 20 61 6e 64 20 74 72 69 65  en list and trie
8aef0 73 0a 2a 2a 20 74 6f 20 63 6f 61 6c 65 73 63 65  s.** to coalesce
8af00 20 65 61 63 68 20 65 6e 74 72 69 65 73 20 77 69   each entries wi
8af10 74 68 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65  th adjacent free
8af20 20 63 68 75 6e 6b 73 2e 20 20 0a 2a 2a 0a 2a 2a   chunks.  .**.**
8af30 20 49 66 20 69 74 20 73 65 65 73 20 61 20 63 68   If it sees a ch
8af40 75 6e 6b 20 74 68 61 74 20 69 73 20 6c 61 72 67  unk that is larg
8af50 65 72 20 74 68 61 6e 20 6d 65 6d 33 2e 69 4d 61  er than mem3.iMa
8af60 73 74 65 72 2c 20 69 74 20 72 65 70 6c 61 63 65  ster, it replace
8af70 73 20 0a 2a 2a 20 74 68 65 20 63 75 72 72 65 6e  s .** the curren
8af80 74 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 77  t mem3.iMaster w
8af90 69 74 68 20 74 68 65 20 6e 65 77 20 6c 61 72 67  ith the new larg
8afa0 65 72 20 63 68 75 6e 6b 2e 20 20 49 6e 20 6f 72  er chunk.  In or
8afb0 64 65 72 20 66 6f 72 0a 2a 2a 20 74 68 69 73 20  der for.** this 
8afc0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 72 65 70  mem3.iMaster rep
8afd0 6c 61 63 65 6d 65 6e 74 20 74 6f 20 77 6f 72 6b  lacement to work
8afe0 2c 20 74 68 65 20 6d 61 73 74 65 72 20 63 68 75  , the master chu
8aff0 6e 6b 20 6d 75 73 74 20 62 65 0a 2a 2a 20 6c 69  nk must be.** li
8b000 6e 6b 65 64 20 69 6e 74 6f 20 74 68 65 20 68 61  nked into the ha
8b010 73 68 20 74 61 62 6c 65 73 2e 20 20 54 68 61 74  sh tables.  That
8b020 20 69 73 20 6e 6f 74 20 74 68 65 20 6e 6f 72 6d   is not the norm
8b030 61 6c 20 73 74 61 74 65 20 6f 66 0a 2a 2a 20 61  al state of.** a
8b040 66 66 61 69 72 73 2c 20 6f 66 20 63 6f 75 72 73  ffairs, of cours
8b050 65 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20  e.  The calling 
8b060 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 6c 69 6e  routine must lin
8b070 6b 20 74 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20  k the master.** 
8b080 63 68 75 6e 6b 20 62 65 66 6f 72 65 20 69 6e 76  chunk before inv
8b090 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69  oking this routi
8b0a0 6e 65 2c 20 74 68 65 6e 20 6d 75 73 74 20 75 6e  ne, then must un
8b0b0 6c 69 6e 6b 20 74 68 65 20 28 70 6f 73 73 69 62  link the (possib
8b0c0 6c 79 0a 2a 2a 20 63 68 61 6e 67 65 64 29 20 6d  ly.** changed) m
8b0d0 61 73 74 65 72 20 63 68 75 6e 6b 20 6f 6e 63 65  aster chunk once
8b0e0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61   this routine ha
8b0f0 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a 2f 0a 73  s finished..*/.s
8b100 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79  tatic void memsy
8b110 73 33 4d 65 72 67 65 28 75 33 32 20 2a 70 52 6f  s3Merge(u32 *pRo
8b120 6f 74 29 7b 0a 20 20 75 33 32 20 69 4e 65 78 74  ot){.  u32 iNext
8b130 2c 20 70 72 65 76 2c 20 73 69 7a 65 2c 20 69 2c  , prev, size, i,
8b140 20 78 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73   x;..  assert( s
8b150 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
8b160 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b  d(mem3.mutex) );
8b170 0a 20 20 66 6f 72 28 69 3d 2a 70 52 6f 6f 74 3b  .  for(i=*pRoot;
8b180 20 69 3e 30 3b 20 69 3d 69 4e 65 78 74 29 7b 0a   i>0; i=iNext){.
8b190 20 20 20 20 69 4e 65 78 74 20 3d 20 6d 65 6d 33      iNext = mem3
8b1a0 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74  .aPool[i].u.list
8b1b0 2e 6e 65 78 74 3b 0a 20 20 20 20 73 69 7a 65 20  .next;.    size 
8b1c0 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31  = mem3.aPool[i-1
8b1d0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0a  ].u.hdr.size4x;.
8b1e0 20 20 20 20 61 73 73 65 72 74 28 20 28 73 69 7a      assert( (siz
8b1f0 65 26 31 29 3d 3d 30 20 29 3b 0a 20 20 20 20 69  e&1)==0 );.    i
8b200 66 28 20 28 73 69 7a 65 26 32 29 3d 3d 30 20 29  f( (size&2)==0 )
8b210 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55  {.      memsys3U
8b220 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c  nlinkFromList(i,
8b230 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 61   pRoot);.      a
8b240 73 73 65 72 74 28 20 69 20 3e 20 6d 65 6d 33 2e  ssert( i > mem3.
8b250 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72  aPool[i-1].u.hdr
8b260 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 20  .prevSize );.   
8b270 20 20 20 70 72 65 76 20 3d 20 69 20 2d 20 6d 65     prev = i - me
8b280 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e  m3.aPool[i-1].u.
8b290 68 64 72 2e 70 72 65 76 53 69 7a 65 3b 0a 20 20  hdr.prevSize;.  
8b2a0 20 20 20 20 69 66 28 20 70 72 65 76 3d 3d 69 4e      if( prev==iN
8b2b0 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ext ){.        i
8b2c0 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f  Next = mem3.aPoo
8b2d0 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e  l[prev].u.list.n
8b2e0 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ext;.      }.   
8b2f0 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b     memsys3Unlink
8b300 28 70 72 65 76 29 3b 0a 20 20 20 20 20 20 73 69  (prev);.      si
8b310 7a 65 20 3d 20 69 20 2b 20 73 69 7a 65 2f 34 20  ze = i + size/4 
8b320 2d 20 70 72 65 76 3b 0a 20 20 20 20 20 20 78 20  - prev;.      x 
8b330 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65  = mem3.aPool[pre
8b340 76 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  v-1].u.hdr.size4
8b350 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d  x & 2;.      mem
8b360 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d 31 5d 2e  3.aPool[prev-1].
8b370 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 73  u.hdr.size4x = s
8b380 69 7a 65 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20  ize*4 | x;.     
8b390 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76   mem3.aPool[prev
8b3a0 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70  +size-1].u.hdr.p
8b3b0 72 65 76 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a  revSize = size;.
8b3c0 20 20 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e        memsys3Lin
8b3d0 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20 20 69  k(prev);.      i
8b3e0 20 3d 20 70 72 65 76 3b 0a 20 20 20 20 7d 65 6c   = prev;.    }el
8b3f0 73 65 7b 0a 20 20 20 20 20 20 73 69 7a 65 20 2f  se{.      size /
8b400 3d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  = 4;.    }.    i
8b410 66 28 20 73 69 7a 65 3e 6d 65 6d 33 2e 73 7a 4d  f( size>mem3.szM
8b420 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d  aster ){.      m
8b430 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 69 3b  em3.iMaster = i;
8b440 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61  .      mem3.szMa
8b450 73 74 65 72 20 3d 20 73 69 7a 65 3b 0a 20 20 20  ster = size;.   
8b460 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
8b470 52 65 74 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f  Return a block o
8b480 66 20 6d 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c  f memory of at l
8b490 65 61 73 74 20 6e 42 79 74 65 73 20 69 6e 20 73  east nBytes in s
8b4a0 69 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e  ize..** Return N
8b4b0 55 4c 4c 20 69 66 20 75 6e 61 62 6c 65 2e 0a 2a  ULL if unable..*
8b4c0 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
8b4d0 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20  on assumes that 
8b4e0 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 6d 75  the necessary mu
8b4f0 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c 20 61  texes, if any, a
8b500 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 65  re.** already he
8b510 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72  ld by the caller
8b520 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66 65 22  . Hence "Unsafe"
8b530 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
8b540 20 2a 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55   *memsys3MallocU
8b550 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 74 65 29  nsafe(int nByte)
8b560 7b 0a 20 20 75 33 32 20 69 3b 0a 20 20 69 6e 74  {.  u32 i;.  int
8b570 20 6e 42 6c 6f 63 6b 3b 0a 20 20 69 6e 74 20 74   nBlock;.  int t
8b580 6f 46 72 65 65 3b 0a 0a 20 20 61 73 73 65 72 74  oFree;..  assert
8b590 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
8b5a0 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29  held(mem3.mutex)
8b5b0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69   );.  assert( si
8b5c0 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d  zeof(Mem3Block)=
8b5d0 3d 38 20 29 3b 0a 20 20 69 66 28 20 6e 42 79 74  =8 );.  if( nByt
8b5e0 65 3c 3d 31 32 20 29 7b 0a 20 20 20 20 6e 42 6c  e<=12 ){.    nBl
8b5f0 6f 63 6b 20 3d 20 32 3b 0a 20 20 7d 65 6c 73 65  ock = 2;.  }else
8b600 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 20 3d 20 28  {.    nBlock = (
8b610 6e 42 79 74 65 20 2b 20 31 31 29 2f 38 3b 0a 20  nByte + 11)/8;. 
8b620 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 42 6c   }.  assert( nBl
8b630 6f 63 6b 3e 3d 32 20 29 3b 0a 0a 20 20 2f 2a 20  ock>=2 );..  /* 
8b640 53 54 45 50 20 31 3a 0a 20 20 2a 2a 20 4c 6f 6f  STEP 1:.  ** Loo
8b650 6b 20 66 6f 72 20 61 6e 20 65 6e 74 72 79 20 6f  k for an entry o
8b660 66 20 74 68 65 20 63 6f 72 72 65 63 74 20 73 69  f the correct si
8b670 7a 65 20 69 6e 20 65 69 74 68 65 72 20 74 68 65  ze in either the
8b680 20 73 6d 61 6c 6c 0a 20 20 2a 2a 20 63 68 75 6e   small.  ** chun
8b690 6b 20 74 61 62 6c 65 20 6f 72 20 69 6e 20 74 68  k table or in th
8b6a0 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20 68 61  e large chunk ha
8b6b0 73 68 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20  sh table.  This 
8b6c0 69 73 0a 20 20 2a 2a 20 73 75 63 63 65 73 73 66  is.  ** successf
8b6d0 75 6c 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 74  ul most of the t
8b6e0 69 6d 65 20 28 61 62 6f 75 74 20 39 20 74 69 6d  ime (about 9 tim
8b6f0 65 73 20 6f 75 74 20 6f 66 20 31 30 29 2e 0a 20  es out of 10).. 
8b700 20 2a 2f 0a 20 20 69 66 28 20 6e 42 6c 6f 63 6b   */.  if( nBlock
8b710 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a   <= MX_SMALL ){.
8b720 20 20 20 20 69 20 3d 20 6d 65 6d 33 2e 61 69 53      i = mem3.aiS
8b730 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32 5d 3b 0a  mall[nBlock-2];.
8b740 20 20 20 20 69 66 28 20 69 3e 30 20 29 7b 0a 20      if( i>0 ){. 
8b750 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69       memsys3Unli
8b760 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d  nkFromList(i, &m
8b770 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 6e 42 6c 6f  em3.aiSmall[nBlo
8b780 63 6b 2d 32 5d 29 3b 0a 20 20 20 20 20 20 72 65  ck-2]);.      re
8b790 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68 65 63  turn memsys3Chec
8b7a0 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b  kout(i, nBlock);
8b7b0 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
8b7c0 20 20 20 20 69 6e 74 20 68 61 73 68 20 3d 20 6e      int hash = n
8b7d0 42 6c 6f 63 6b 20 25 20 4e 5f 48 41 53 48 3b 0a  Block % N_HASH;.
8b7e0 20 20 20 20 66 6f 72 28 69 3d 6d 65 6d 33 2e 61      for(i=mem3.a
8b7f0 69 48 61 73 68 5b 68 61 73 68 5d 3b 20 69 3e 30  iHash[hash]; i>0
8b800 3b 20 69 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  ; i=mem3.aPool[i
8b810 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 7b 0a  ].u.list.next){.
8b820 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 61        if( mem3.a
8b830 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e  Pool[i-1].u.hdr.
8b840 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b  size4x/4==nBlock
8b850 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73   ){.        mems
8b860 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73  ys3UnlinkFromLis
8b870 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73  t(i, &mem3.aiHas
8b880 68 5b 68 61 73 68 5d 29 3b 0a 20 20 20 20 20 20  h[hash]);.      
8b890 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33    return memsys3
8b8a0 43 68 65 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f  Checkout(i, nBlo
8b8b0 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ck);.      }.   
8b8c0 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 54 45   }.  }..  /* STE
8b8d0 50 20 32 3a 0a 20 20 2a 2a 20 54 72 79 20 74 6f  P 2:.  ** Try to
8b8e0 20 73 61 74 69 73 66 79 20 74 68 65 20 61 6c 6c   satisfy the all
8b8f0 6f 63 61 74 69 6f 6e 20 62 79 20 63 61 72 76 69  ocation by carvi
8b900 6e 67 20 61 20 70 69 65 63 65 20 6f 66 66 20 6f  ng a piece off o
8b910 66 20 74 68 65 20 65 6e 64 0a 20 20 2a 2a 20 6f  f the end.  ** o
8b920 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 68 75  f the master chu
8b930 6e 6b 2e 20 20 54 68 69 73 20 73 74 65 70 20 75  nk.  This step u
8b940 73 75 61 6c 6c 79 20 77 6f 72 6b 73 20 69 66 20  sually works if 
8b950 73 74 65 70 20 31 20 66 61 69 6c 73 2e 0a 20 20  step 1 fails..  
8b960 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a  */.  if( mem3.sz
8b970 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29  Master>=nBlock )
8b980 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d  {.    return mem
8b990 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 6e  sys3FromMaster(n
8b9a0 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 0a 20 20  Block);.  }...  
8b9b0 2f 2a 20 53 54 45 50 20 33 3a 20 20 0a 20 20 2a  /* STEP 3:  .  *
8b9c0 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74  * Loop through t
8b9d0 68 65 20 65 6e 74 69 72 65 20 6d 65 6d 6f 72 79  he entire memory
8b9e0 20 70 6f 6f 6c 2e 20 20 43 6f 61 6c 65 73 63 65   pool.  Coalesce
8b9f0 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65 0a 20   adjacent free. 
8ba00 20 2a 2a 20 63 68 75 6e 6b 73 2e 20 20 52 65 63   ** chunks.  Rec
8ba10 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 73 74 65  ompute the maste
8ba20 72 20 63 68 75 6e 6b 20 61 73 20 74 68 65 20 6c  r chunk as the l
8ba30 61 72 67 65 73 74 20 66 72 65 65 20 63 68 75 6e  argest free chun
8ba40 6b 2e 0a 20 20 2a 2a 20 54 68 65 6e 20 74 72 79  k..  ** Then try
8ba50 20 61 67 61 69 6e 20 74 6f 20 73 61 74 69 73 66   again to satisf
8ba60 79 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  y the allocation
8ba70 20 62 79 20 63 61 72 76 69 6e 67 20 61 20 70 69   by carving a pi
8ba80 65 63 65 20 6f 66 66 0a 20 20 2a 2a 20 6f 66 20  ece off.  ** of 
8ba90 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d  the end of the m
8baa0 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 54 68  aster chunk.  Th
8bab0 69 73 20 73 74 65 70 20 68 61 70 70 65 6e 73 20  is step happens 
8bac0 76 65 72 79 0a 20 20 2a 2a 20 72 61 72 65 6c 79  very.  ** rarely
8bad0 20 28 77 65 20 68 6f 70 65 21 29 0a 20 20 2a 2f   (we hope!).  */
8bae0 0a 20 20 66 6f 72 28 74 6f 46 72 65 65 3d 6e 42  .  for(toFree=nB
8baf0 6c 6f 63 6b 2a 31 36 3b 20 74 6f 46 72 65 65 3c  lock*16; toFree<
8bb00 28 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 31 36 29 3b  (mem3.nPool*16);
8bb10 20 74 6f 46 72 65 65 20 2a 3d 20 32 29 7b 0a 20   toFree *= 2){. 
8bb20 20 20 20 6d 65 6d 73 79 73 33 4f 75 74 4f 66 4d     memsys3OutOfM
8bb30 65 6d 6f 72 79 28 74 6f 46 72 65 65 29 3b 0a 20  emory(toFree);. 
8bb40 20 20 20 69 66 28 20 6d 65 6d 33 2e 69 4d 61 73     if( mem3.iMas
8bb50 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  ter ){.      mem
8bb60 73 79 73 33 4c 69 6e 6b 28 6d 65 6d 33 2e 69 4d  sys3Link(mem3.iM
8bb70 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 6d 65  aster);.      me
8bb80 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 30 3b 0a  m3.iMaster = 0;.
8bb90 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73        mem3.szMas
8bba0 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  ter = 0;.    }. 
8bbb0 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f     for(i=0; i<N_
8bbc0 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  HASH; i++){.    
8bbd0 20 20 6d 65 6d 73 79 73 33 4d 65 72 67 65 28 26    memsys3Merge(&
8bbe0 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d 29 3b  mem3.aiHash[i]);
8bbf0 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69  .    }.    for(i
8bc00 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31  =0; i<MX_SMALL-1
8bc10 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6d 65  ; i++){.      me
8bc20 6d 73 79 73 33 4d 65 72 67 65 28 26 6d 65 6d 33  msys3Merge(&mem3
8bc30 2e 61 69 53 6d 61 6c 6c 5b 69 5d 29 3b 0a 20 20  .aiSmall[i]);.  
8bc40 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65 6d 33    }.    if( mem3
8bc50 2e 73 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20  .szMaster ){.   
8bc60 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b     memsys3Unlink
8bc70 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a  (mem3.iMaster);.
8bc80 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 73        if( mem3.s
8bc90 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20  zMaster>=nBlock 
8bca0 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
8bcb0 6e 20 6d 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73  n memsys3FromMas
8bcc0 74 65 72 28 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20  ter(nBlock);.   
8bcd0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
8bce0 20 20 2f 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20    /* If none of 
8bcf0 74 68 65 20 61 62 6f 76 65 20 77 6f 72 6b 65 64  the above worked
8bd00 2c 20 74 68 65 6e 20 77 65 20 66 61 69 6c 2e 20  , then we fail. 
8bd10 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  */.  return 0;.}
8bd20 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20  ../*.** Free an 
8bd30 6f 75 74 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f  outstanding memo
8bd40 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a  ry allocation..*
8bd50 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
8bd60 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20  on assumes that 
8bd70 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 6d 75  the necessary mu
8bd80 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c 20 61  texes, if any, a
8bd90 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 65  re.** already he
8bda0 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72  ld by the caller
8bdb0 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66 65 22  . Hence "Unsafe"
8bdc0 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 6d 73 79 73  ..*/.void memsys
8bdd0 33 46 72 65 65 55 6e 73 61 66 65 28 76 6f 69 64  3FreeUnsafe(void
8bde0 20 2a 70 4f 6c 64 29 7b 0a 20 20 4d 65 6d 33 42   *pOld){.  Mem3B
8bdf0 6c 6f 63 6b 20 2a 70 20 3d 20 28 4d 65 6d 33 42  lock *p = (Mem3B
8be00 6c 6f 63 6b 2a 29 70 4f 6c 64 3b 0a 20 20 69 6e  lock*)pOld;.  in
8be10 74 20 69 3b 0a 20 20 75 33 32 20 73 69 7a 65 2c  t i;.  u32 size,
8be20 20 78 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   x;.  assert( sq
8be30 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
8be40 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a  (mem3.mutex) );.
8be50 20 20 61 73 73 65 72 74 28 20 70 3e 6d 65 6d 33    assert( p>mem3
8be60 2e 61 50 6f 6f 6c 20 26 26 20 70 3c 26 6d 65 6d  .aPool && p<&mem
8be70 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f  3.aPool[mem3.nPo
8be80 6f 6c 5d 20 29 3b 0a 20 20 69 20 3d 20 70 20 2d  ol] );.  i = p -
8be90 20 6d 65 6d 33 2e 61 50 6f 6f 6c 3b 0a 20 20 61   mem3.aPool;.  a
8bea0 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61 50 6f  ssert( (mem3.aPo
8beb0 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ol[i-1].u.hdr.si
8bec0 7a 65 34 78 26 31 29 3d 3d 31 20 29 3b 0a 20 20  ze4x&1)==1 );.  
8bed0 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f  size = mem3.aPoo
8bee0 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  l[i-1].u.hdr.siz
8bef0 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28  e4x/4;.  assert(
8bf00 20 69 2b 73 69 7a 65 3c 3d 6d 65 6d 33 2e 6e 50   i+size<=mem3.nP
8bf10 6f 6f 6c 2b 31 20 29 3b 0a 20 20 6d 65 6d 33 2e  ool+1 );.  mem3.
8bf20 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72  aPool[i-1].u.hdr
8bf30 2e 73 69 7a 65 34 78 20 26 3d 20 7e 31 3b 0a 20  .size4x &= ~1;. 
8bf40 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69   mem3.aPool[i+si
8bf50 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76  ze-1].u.hdr.prev
8bf60 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 6d  Size = size;.  m
8bf70 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65  em3.aPool[i+size
8bf80 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
8bf90 20 26 3d 20 7e 32 3b 0a 20 20 6d 65 6d 73 79 73   &= ~2;.  memsys
8bfa0 33 4c 69 6e 6b 28 69 29 3b 0a 0a 20 20 2f 2a 20  3Link(i);..  /* 
8bfb0 54 72 79 20 74 6f 20 65 78 70 61 6e 64 20 74 68  Try to expand th
8bfc0 65 20 6d 61 73 74 65 72 20 75 73 69 6e 67 20 74  e master using t
8bfd0 68 65 20 6e 65 77 6c 79 20 66 72 65 65 64 20 63  he newly freed c
8bfe0 68 75 6e 6b 20 2a 2f 0a 20 20 69 66 28 20 6d 65  hunk */.  if( me
8bff0 6d 33 2e 69 4d 61 73 74 65 72 20 29 7b 0a 20 20  m3.iMaster ){.  
8c000 20 20 77 68 69 6c 65 28 20 28 6d 65 6d 33 2e 61    while( (mem3.a
8c010 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65  Pool[mem3.iMaste
8c020 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  r-1].u.hdr.size4
8c030 78 26 32 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  x&2)==0 ){.     
8c040 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f   size = mem3.aPo
8c050 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d  ol[mem3.iMaster-
8c060 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a  1].u.hdr.prevSiz
8c070 65 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 69 4d  e;.      mem3.iM
8c080 61 73 74 65 72 20 2d 3d 20 73 69 7a 65 3b 0a 20  aster -= size;. 
8c090 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74       mem3.szMast
8c0a0 65 72 20 2b 3d 20 73 69 7a 65 3b 0a 20 20 20 20  er += size;.    
8c0b0 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28    memsys3Unlink(
8c0c0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20  mem3.iMaster);. 
8c0d0 20 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e 61 50       x = mem3.aP
8c0e0 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72  ool[mem3.iMaster
8c0f0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
8c100 20 26 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d 33   & 2;.      mem3
8c110 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73  .aPool[mem3.iMas
8c120 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  ter-1].u.hdr.siz
8c130 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73  e4x = mem3.szMas
8c140 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20  ter*4 | x;.     
8c150 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33   mem3.aPool[mem3
8c160 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a  .iMaster+mem3.sz
8c170 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e  Master-1].u.hdr.
8c180 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e  prevSize = mem3.
8c190 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 7d 0a  szMaster;.    }.
8c1a0 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f      x = mem3.aPo
8c1b0 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d  ol[mem3.iMaster-
8c1c0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  1].u.hdr.size4x 
8c1d0 26 20 32 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  & 2;.    while( 
8c1e0 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33  (mem3.aPool[mem3
8c1f0 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a  .iMaster+mem3.sz
8c200 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e  Master-1].u.hdr.
8c210 73 69 7a 65 34 78 26 31 29 3d 3d 30 20 29 7b 0a  size4x&1)==0 ){.
8c220 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c        memsys3Unl
8c230 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72  ink(mem3.iMaster
8c240 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 29 3b  +mem3.szMaster);
8c250 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61  .      mem3.szMa
8c260 73 74 65 72 20 2b 3d 20 6d 65 6d 33 2e 61 50 6f  ster += mem3.aPo
8c270 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b  ol[mem3.iMaster+
8c280 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d  mem3.szMaster-1]
8c290 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b  .u.hdr.size4x/4;
8c2a0 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f  .      mem3.aPoo
8c2b0 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31  l[mem3.iMaster-1
8c2c0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d  ].u.hdr.size4x =
8c2d0 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34   mem3.szMaster*4
8c2e0 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65 6d 33   | x;.      mem3
8c2f0 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73  .aPool[mem3.iMas
8c300 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  ter+mem3.szMaste
8c310 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53  r-1].u.hdr.prevS
8c320 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73  ize = mem3.szMas
8c330 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  ter;.    }.  }.}
8c340 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
8c350 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 6f 75  he size of an ou
8c360 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61  tstanding alloca
8c370 74 69 6f 6e 2c 20 69 6e 20 62 79 74 65 73 2e 20  tion, in bytes. 
8c380 20 54 68 65 0a 2a 2a 20 73 69 7a 65 20 72 65 74   The.** size ret
8c390 75 72 6e 65 64 20 6f 6d 69 74 73 20 74 68 65 20  urned omits the 
8c3a0 38 2d 62 79 74 65 20 68 65 61 64 65 72 20 6f 76  8-byte header ov
8c3b0 65 72 68 65 61 64 2e 20 20 54 68 69 73 20 6f 6e  erhead.  This on
8c3c0 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20  ly.** works for 
8c3d0 63 68 75 6e 6b 73 20 74 68 61 74 20 61 72 65 20  chunks that are 
8c3e0 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65  currently checke
8c3f0 64 20 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  d out..*/.static
8c400 20 69 6e 74 20 6d 65 6d 73 79 73 33 53 69 7a 65   int memsys3Size
8c410 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 4d 65 6d  (void *p){.  Mem
8c420 33 42 6c 6f 63 6b 20 2a 70 42 6c 6f 63 6b 3b 0a  3Block *pBlock;.
8c430 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74    if( p==0 ) ret
8c440 75 72 6e 20 30 3b 0a 20 20 70 42 6c 6f 63 6b 20  urn 0;.  pBlock 
8c450 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 2a 29 70 3b  = (Mem3Block*)p;
8c460 0a 20 20 61 73 73 65 72 74 28 20 28 70 42 6c 6f  .  assert( (pBlo
8c470 63 6b 5b 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  ck[-1].u.hdr.siz
8c480 65 34 78 26 31 29 21 3d 30 20 29 3b 0a 20 20 72  e4x&1)!=0 );.  r
8c490 65 74 75 72 6e 20 28 70 42 6c 6f 63 6b 5b 2d 31  eturn (pBlock[-1
8c4a0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 7e  ].u.hdr.size4x&~
8c4b0 33 29 2a 32 20 2d 20 34 3b 0a 7d 0a 0a 2f 2a 0a  3)*2 - 4;.}../*.
8c4c0 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65  ** Round up a re
8c4d0 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68  quest size to th
8c4e0 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c  e next valid all
8c4f0 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f  ocation size..*/
8c500 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73  .static int mems
8c510 79 73 33 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e  ys3Roundup(int n
8c520 29 7b 0a 20 20 69 66 28 20 6e 3c 3d 31 32 20 29  ){.  if( n<=12 )
8c530 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 32 3b  {.    return 12;
8c540 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
8c550 74 75 72 6e 20 28 28 6e 2b 31 31 29 26 7e 37 29  turn ((n+11)&~7)
8c560 20 2d 20 34 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   - 4;.  }.}../*.
8c570 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74  ** Allocate nByt
8c580 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f  es of memory..*/
8c590 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65  .static void *me
8c5a0 6d 73 79 73 33 4d 61 6c 6c 6f 63 28 69 6e 74 20  msys3Malloc(int 
8c5b0 6e 42 79 74 65 73 29 7b 0a 20 20 73 71 6c 69 74  nBytes){.  sqlit
8c5c0 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 61  e3_int64 *p;.  a
8c5d0 73 73 65 72 74 28 20 6e 42 79 74 65 73 3e 30 20  ssert( nBytes>0 
8c5e0 29 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d  );          /* m
8c5f0 61 6c 6c 6f 63 2e 63 20 66 69 6c 74 65 72 73 20  alloc.c filters 
8c600 6f 75 74 20 30 20 62 79 74 65 20 72 65 71 75 65  out 0 byte reque
8c610 73 74 73 20 2a 2f 0a 20 20 6d 65 6d 73 79 73 33  sts */.  memsys3
8c620 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 3d 20 6d  Enter();.  p = m
8c630 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e 73 61  emsys3MallocUnsa
8c640 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 6d 65  fe(nBytes);.  me
8c650 6d 73 79 73 33 4c 65 61 76 65 28 29 3b 0a 20 20  msys3Leave();.  
8c660 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b  return (void*)p;
8c670 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20   .}../*.** Free 
8c680 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 76 6f 69 64 20  memory..*/.void 
8c690 6d 65 6d 73 79 73 33 46 72 65 65 28 76 6f 69 64  memsys3Free(void
8c6a0 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 61 73 73   *pPrior){.  ass
8c6b0 65 72 74 28 20 70 50 72 69 6f 72 20 29 3b 0a 20  ert( pPrior );. 
8c6c0 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b   memsys3Enter();
8c6d0 0a 20 20 6d 65 6d 73 79 73 33 46 72 65 65 55 6e  .  memsys3FreeUn
8c6e0 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20  safe(pPrior);.  
8c6f0 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 3b 0a  memsys3Leave();.
8c700 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  }../*.** Change 
8c710 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65  the size of an e
8c720 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61  xisting memory a
8c730 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 76 6f 69  llocation.*/.voi
8c740 64 20 2a 6d 65 6d 73 79 73 33 52 65 61 6c 6c 6f  d *memsys3Reallo
8c750 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20  c(void *pPrior, 
8c760 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 69  int nBytes){.  i
8c770 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 6f 69 64 20  nt nOld;.  void 
8c780 2a 70 3b 0a 20 20 69 66 28 20 70 50 72 69 6f 72  *p;.  if( pPrior
8c790 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
8c7a0 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  n sqlite3_malloc
8c7b0 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a 20 20  (nBytes);.  }.  
8c7c0 69 66 28 20 6e 42 79 74 65 73 3c 3d 30 20 29 7b  if( nBytes<=0 ){
8c7d0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
8c7e0 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 20 20 72  e(pPrior);.    r
8c7f0 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e  eturn 0;.  }.  n
8c800 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 33 53 69 7a  Old = memsys3Siz
8c810 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 69 66 28  e(pPrior);.  if(
8c820 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64 20 26 26   nBytes<=nOld &&
8c830 20 6e 42 79 74 65 73 3e 3d 6e 4f 6c 64 2d 31 32   nBytes>=nOld-12
8c840 38 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  8 ){.    return 
8c850 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65  pPrior;.  }.  me
8c860 6d 73 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20  msys3Enter();.  
8c870 70 20 3d 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f  p = memsys3Mallo
8c880 63 55 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b  cUnsafe(nBytes);
8c890 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
8c8a0 69 66 28 20 6e 4f 6c 64 3c 6e 42 79 74 65 73 20  if( nOld<nBytes 
8c8b0 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
8c8c0 70 2c 20 70 50 72 69 6f 72 2c 20 6e 4f 6c 64 29  p, pPrior, nOld)
8c8d0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
8c8e0 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 50 72     memcpy(p, pPr
8c8f0 69 6f 72 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20  ior, nBytes);.  
8c900 20 20 7d 0a 20 20 20 20 6d 65 6d 73 79 73 33 46    }.    memsys3F
8c910 72 65 65 55 6e 73 61 66 65 28 70 50 72 69 6f 72  reeUnsafe(pPrior
8c920 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 33  );.  }.  memsys3
8c930 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 74 75 72  Leave();.  retur
8c940 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  n p;.}../*.** In
8c950 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f  itialize this mo
8c960 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  dule..*/.static 
8c970 69 6e 74 20 6d 65 6d 73 79 73 33 49 6e 69 74 28  int memsys3Init(
8c980 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a  void *NotUsed){.
8c990 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c    if( !sqlite3Gl
8c9a0 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70  obalConfig.pHeap
8c9b0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
8c9c0 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
8c9d0 0a 0a 20 20 2f 2a 20 53 74 6f 72 65 20 61 20 70  ..  /* Store a p
8c9e0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65  ointer to the me
8c9f0 6d 6f 72 79 20 62 6c 6f 63 6b 20 69 6e 20 67 6c  mory block in gl
8ca00 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65 20 6d  obal structure m
8ca10 65 6d 33 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  em3. */.  assert
8ca20 28 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f  ( sizeof(Mem3Blo
8ca30 63 6b 29 3d 3d 38 20 29 3b 0a 20 20 6d 65 6d 33  ck)==8 );.  mem3
8ca40 2e 61 50 6f 6f 6c 20 3d 20 28 4d 65 6d 33 42 6c  .aPool = (Mem3Bl
8ca50 6f 63 6b 20 2a 29 73 71 6c 69 74 65 33 47 6c 6f  ock *)sqlite3Glo
8ca60 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b  balConfig.pHeap;
8ca70 0a 20 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 20 3d 20  .  mem3.nPool = 
8ca80 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  (sqlite3GlobalCo
8ca90 6e 66 69 67 2e 6e 48 65 61 70 20 2f 20 73 69 7a  nfig.nHeap / siz
8caa0 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 29 20  eof(Mem3Block)) 
8cab0 2d 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69  - 2;..  /* Initi
8cac0 61 6c 69 7a 65 20 74 68 65 20 6d 61 73 74 65 72  alize the master
8cad0 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 6d 65 6d   block. */.  mem
8cae0 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20 6d 65 6d  3.szMaster = mem
8caf0 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 2e  3.nPool;.  mem3.
8cb00 6d 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 2e  mnMaster = mem3.
8cb10 73 7a 4d 61 73 74 65 72 3b 0a 20 20 6d 65 6d 33  szMaster;.  mem3
8cb20 2e 69 4d 61 73 74 65 72 20 3d 20 31 3b 0a 20 20  .iMaster = 1;.  
8cb30 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 30 5d 2e 75 2e  mem3.aPool[0].u.
8cb40 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 28 6d 65  hdr.size4x = (me
8cb50 6d 33 2e 73 7a 4d 61 73 74 65 72 3c 3c 32 29 20  m3.szMaster<<2) 
8cb60 2b 20 32 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f  + 2;.  mem3.aPoo
8cb70 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e  l[mem3.nPool].u.
8cb80 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d  hdr.prevSize = m
8cb90 65 6d 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d  em3.nPool;.  mem
8cba0 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f  3.aPool[mem3.nPo
8cbb0 6f 6c 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  ol].u.hdr.size4x
8cbc0 20 3d 20 31 3b 0a 0a 20 20 72 65 74 75 72 6e 20   = 1;..  return 
8cbd0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
8cbe0 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65  .** Deinitialize
8cbf0 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f   this module..*/
8cc00 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
8cc10 73 79 73 33 53 68 75 74 64 6f 77 6e 28 76 6f 69  sys3Shutdown(voi
8cc20 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 72  d *NotUsed){.  r
8cc30 65 74 75 72 6e 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a  eturn;.}..../*.*
8cc40 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20  * Open the file 
8cc50 69 6e 64 69 63 61 74 65 64 20 61 6e 64 20 77 72  indicated and wr
8cc60 69 74 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c  ite a log of all
8cc70 20 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20   unfreed memory 
8cc80 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20  .** allocations 
8cc90 69 6e 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a  into that log..*
8cca0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
8ccb0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d   void sqlite3Mem
8ccc0 73 79 73 33 44 75 6d 70 28 63 6f 6e 73 74 20 63  sys3Dump(const c
8ccd0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b  har *zFilename){
8cce0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
8ccf0 45 42 55 47 0a 20 20 46 49 4c 45 20 2a 6f 75 74  EBUG.  FILE *out
8cd00 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20  ;.  int i, j;.  
8cd10 75 33 32 20 73 69 7a 65 3b 0a 20 20 69 66 28 20  u32 size;.  if( 
8cd20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20  zFilename==0 || 
8cd30 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20  zFilename[0]==0 
8cd40 29 7b 0a 20 20 20 20 6f 75 74 20 3d 20 73 74 64  ){.    out = std
8cd50 6f 75 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  out;.  }else{.  
8cd60 20 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46    out = fopen(zF
8cd70 69 6c 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20  ilename, "w");. 
8cd80 20 20 20 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b     if( out==0 ){
8cd90 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73  .      fprintf(s
8cda0 74 64 65 72 72 2c 20 22 2a 2a 20 55 6e 61 62 6c  tderr, "** Unabl
8cdb0 65 20 74 6f 20 6f 75 74 70 75 74 20 6d 65 6d 6f  e to output memo
8cdc0 72 79 20 64 65 62 75 67 20 6f 75 74 70 75 74 20  ry debug output 
8cdd0 6c 6f 67 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20  log: %s **\n",. 
8cde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8cdf0 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29 3b       zFilename);
8ce00 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
8ce10 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 79     }.  }.  memsy
8ce20 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 66 70 72  s3Enter();.  fpr
8ce30 69 6e 74 66 28 6f 75 74 2c 20 22 43 48 55 4e 4b  intf(out, "CHUNK
8ce40 53 3a 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d  S:\n");.  for(i=
8ce50 31 3b 20 69 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c  1; i<=mem3.nPool
8ce60 3b 20 69 2b 3d 73 69 7a 65 2f 34 29 7b 0a 20 20  ; i+=size/4){.  
8ce70 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50    size = mem3.aP
8ce80 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73  ool[i-1].u.hdr.s
8ce90 69 7a 65 34 78 3b 0a 20 20 20 20 69 66 28 20 73  ize4x;.    if( s
8cea0 69 7a 65 2f 34 3c 3d 31 20 29 7b 0a 20 20 20 20  ize/4<=1 ){.    
8ceb0 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
8cec0 25 70 20 73 69 7a 65 20 65 72 72 6f 72 5c 6e 22  %p size error\n"
8ced0 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d  , &mem3.aPool[i]
8cee0 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
8cef0 20 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61   0 );.      brea
8cf00 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  k;.    }.    if(
8cf10 20 28 73 69 7a 65 26 31 29 3d 3d 30 20 26 26 20   (size&1)==0 && 
8cf20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a  mem3.aPool[i+siz
8cf30 65 2f 34 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65  e/4-1].u.hdr.pre
8cf40 76 53 69 7a 65 21 3d 73 69 7a 65 2f 34 20 29 7b  vSize!=size/4 ){
8cf50 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f  .      fprintf(o
8cf60 75 74 2c 20 22 25 70 20 74 61 69 6c 20 73 69 7a  ut, "%p tail siz
8cf70 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68  e does not match
8cf80 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c  \n", &mem3.aPool
8cf90 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65  [i]);.      asse
8cfa0 72 74 28 20 30 20 29 3b 0a 20 20 20 20 20 20 62  rt( 0 );.      b
8cfb0 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
8cfc0 69 66 28 20 28 28 6d 65 6d 33 2e 61 50 6f 6f 6c  if( ((mem3.aPool
8cfd0 5b 69 2b 73 69 7a 65 2f 34 2d 31 5d 2e 75 2e 68  [i+size/4-1].u.h
8cfe0 64 72 2e 73 69 7a 65 34 78 26 32 29 3e 3e 31 29  dr.size4x&2)>>1)
8cff0 21 3d 28 73 69 7a 65 26 31 29 20 29 7b 0a 20 20  !=(size&1) ){.  
8d000 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c      fprintf(out,
8d010 20 22 25 70 20 74 61 69 6c 20 63 68 65 63 6b 6f   "%p tail checko
8d020 75 74 20 62 69 74 20 69 73 20 69 6e 63 6f 72 72  ut bit is incorr
8d030 65 63 74 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50  ect\n", &mem3.aP
8d040 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61  ool[i]);.      a
8d050 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20 20 20  ssert( 0 );.    
8d060 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
8d070 20 20 20 69 66 28 20 73 69 7a 65 26 31 20 29 7b     if( size&1 ){
8d080 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f  .      fprintf(o
8d090 75 74 2c 20 22 25 70 20 25 36 64 20 62 79 74 65  ut, "%p %6d byte
8d0a0 73 20 63 68 65 63 6b 65 64 20 6f 75 74 5c 6e 22  s checked out\n"
8d0b0 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d  , &mem3.aPool[i]
8d0c0 2c 20 28 73 69 7a 65 2f 34 29 2a 38 2d 38 29 3b  , (size/4)*8-8);
8d0d0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
8d0e0 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
8d0f0 25 70 20 25 36 64 20 62 79 74 65 73 20 66 72 65  %p %6d bytes fre
8d100 65 25 73 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50  e%s\n", &mem3.aP
8d110 6f 6f 6c 5b 69 5d 2c 20 28 73 69 7a 65 2f 34 29  ool[i], (size/4)
8d120 2a 38 2d 38 2c 0a 20 20 20 20 20 20 20 20 20 20  *8-8,.          
8d130 20 20 20 20 20 20 20 20 69 3d 3d 6d 65 6d 33 2e          i==mem3.
8d140 69 4d 61 73 74 65 72 20 3f 20 22 20 2a 2a 6d 61  iMaster ? " **ma
8d150 73 74 65 72 2a 2a 22 20 3a 20 22 22 29 3b 0a 20  ster**" : "");. 
8d160 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69     }.  }.  for(i
8d170 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31  =0; i<MX_SMALL-1
8d180 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
8d190 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3d  mem3.aiSmall[i]=
8d1a0 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  =0 ) continue;. 
8d1b0 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
8d1c0 22 73 6d 61 6c 6c 28 25 32 64 29 3a 22 2c 20 69  "small(%2d):", i
8d1d0 29 3b 0a 20 20 20 20 66 6f 72 28 6a 20 3d 20 6d  );.    for(j = m
8d1e0 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3b 20  em3.aiSmall[i]; 
8d1f0 6a 3e 30 3b 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f  j>0; j=mem3.aPoo
8d200 6c 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74  l[j].u.list.next
8d210 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66  ){.      fprintf
8d220 28 6f 75 74 2c 20 22 20 25 70 28 25 64 29 22 2c  (out, " %p(%d)",
8d230 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c   &mem3.aPool[j],
8d240 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28  .              (
8d250 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e  mem3.aPool[j-1].
8d260 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 29 2a  u.hdr.size4x/4)*
8d270 38 2d 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  8-8);.    }.    
8d280 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e  fprintf(out, "\n
8d290 22 29 3b 20 0a 20 20 7d 0a 20 20 66 6f 72 28 69  "); .  }.  for(i
8d2a0 3d 30 3b 20 69 3c 4e 5f 48 41 53 48 3b 20 69 2b  =0; i<N_HASH; i+
8d2b0 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33  +){.    if( mem3
8d2c0 2e 61 69 48 61 73 68 5b 69 5d 3d 3d 30 20 29 20  .aiHash[i]==0 ) 
8d2d0 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 66 70  continue;.    fp
8d2e0 72 69 6e 74 66 28 6f 75 74 2c 20 22 68 61 73 68  rintf(out, "hash
8d2f0 28 25 32 64 29 3a 22 2c 20 69 29 3b 0a 20 20 20  (%2d):", i);.   
8d300 20 66 6f 72 28 6a 20 3d 20 6d 65 6d 33 2e 61 69   for(j = mem3.ai
8d310 48 61 73 68 5b 69 5d 3b 20 6a 3e 30 3b 20 6a 3d  Hash[i]; j>0; j=
8d320 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e  mem3.aPool[j].u.
8d330 6c 69 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20  list.next){.    
8d340 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
8d350 20 25 70 28 25 64 29 22 2c 20 26 6d 65 6d 33 2e   %p(%d)", &mem3.
8d360 61 50 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20 20 20 20  aPool[j],.      
8d370 20 20 20 20 20 20 20 20 28 6d 65 6d 33 2e 61 50          (mem3.aP
8d380 6f 6f 6c 5b 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73  ool[j-1].u.hdr.s
8d390 69 7a 65 34 78 2f 34 29 2a 38 2d 38 29 3b 0a 20  ize4x/4)*8-8);. 
8d3a0 20 20 20 7d 0a 20 20 20 20 66 70 72 69 6e 74 66     }.    fprintf
8d3b0 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 20 0a 20 20  (out, "\n"); .  
8d3c0 7d 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c  }.  fprintf(out,
8d3d0 20 22 6d 61 73 74 65 72 3d 25 64 5c 6e 22 2c 20   "master=%d\n", 
8d3e0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20  mem3.iMaster);. 
8d3f0 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6e   fprintf(out, "n
8d400 6f 77 55 73 65 64 3d 25 64 5c 6e 22 2c 20 6d 65  owUsed=%d\n", me
8d410 6d 33 2e 6e 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d  m3.nPool*8 - mem
8d420 33 2e 73 7a 4d 61 73 74 65 72 2a 38 29 3b 0a 20  3.szMaster*8);. 
8d430 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d   fprintf(out, "m
8d440 78 55 73 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d  xUsed=%d\n", mem
8d450 33 2e 6e 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33  3.nPool*8 - mem3
8d460 2e 6d 6e 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20  .mnMaster*8);.  
8d470 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
8d480 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b  ave(mem3.mutex);
8d490 0a 20 20 69 66 28 20 6f 75 74 3d 3d 73 74 64 6f  .  if( out==stdo
8d4a0 75 74 20 29 7b 0a 20 20 20 20 66 66 6c 75 73 68  ut ){.    fflush
8d4b0 28 73 74 64 6f 75 74 29 3b 0a 20 20 7d 65 6c 73  (stdout);.  }els
8d4c0 65 7b 0a 20 20 20 20 66 63 6c 6f 73 65 28 6f 75  e{.    fclose(ou
8d4d0 74 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d  t);.  }.#endif.}
8d4e0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
8d4f0 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79  tine is the only
8d500 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73   routine in this
8d510 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 72   file with exter
8d520 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 67 65 2e  nal .** linkage.
8d530 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20  .**.** Populate 
8d540 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65  the low-level me
8d550 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
8d560 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72  function pointer
8d570 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47  s in.** sqlite3G
8d580 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69  lobalConfig.m wi
8d590 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74  th pointers to t
8d5a0 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74  he routines in t
8d5b0 68 69 73 20 66 69 6c 65 2e 20 54 68 65 0a 2a 2a  his file. The.**
8d5c0 20 61 72 67 75 6d 65 6e 74 73 20 73 70 65 63 69   arguments speci
8d5d0 66 79 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20  fy the block of 
8d5e0 6d 65 6d 6f 72 79 20 74 6f 20 6d 61 6e 61 67 65  memory to manage
8d5f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
8d600 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 63 61 6c  tine is only cal
8d610 6c 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63  led by sqlite3_c
8d620 6f 6e 66 69 67 28 29 2c 20 61 6e 64 20 74 68 65  onfig(), and the
8d630 72 65 66 6f 72 65 0a 2a 2a 20 69 73 20 6e 6f 74  refore.** is not
8d640 20 72 65 71 75 69 72 65 64 20 74 6f 20 62 65 20   required to be 
8d650 74 68 72 65 61 64 73 61 66 65 20 28 69 74 20 69  threadsafe (it i
8d660 73 20 6e 6f 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54  s not)..*/.SQLIT
8d670 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
8d680 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
8d690 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47  ods *sqlite3MemG
8d6a0 65 74 4d 65 6d 73 79 73 33 28 76 6f 69 64 29 7b  etMemsys3(void){
8d6b0 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
8d6c0 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
8d6d0 6f 64 73 20 6d 65 6d 70 6f 6f 6c 4d 65 74 68 6f  ods mempoolMetho
8d6e0 64 73 20 3d 20 7b 0a 20 20 20 20 20 6d 65 6d 73  ds = {.     mems
8d6f0 79 73 33 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20  ys3Malloc,.     
8d700 6d 65 6d 73 79 73 33 46 72 65 65 2c 0a 20 20 20  memsys3Free,.   
8d710 20 20 6d 65 6d 73 79 73 33 52 65 61 6c 6c 6f 63    memsys3Realloc
8d720 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 53 69  ,.     memsys3Si
8d730 7a 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33  ze,.     memsys3
8d740 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 6d 65  Roundup,.     me
8d750 6d 73 79 73 33 49 6e 69 74 2c 0a 20 20 20 20 20  msys3Init,.     
8d760 6d 65 6d 73 79 73 33 53 68 75 74 64 6f 77 6e 2c  memsys3Shutdown,
8d770 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 72  .     0.  };.  r
8d780 65 74 75 72 6e 20 26 6d 65 6d 70 6f 6f 6c 4d 65  eturn &mempoolMe
8d790 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66  thods;.}..#endif
8d7a0 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   /* SQLITE_ENABL
8d7b0 45 5f 4d 45 4d 53 59 53 33 20 2a 2f 0a 0a 2f 2a  E_MEMSYS3 */../*
8d7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
8d7d0 64 20 6f 66 20 6d 65 6d 33 2e 63 20 2a 2a 2a 2a  d of mem3.c ****
8d7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d7f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
8d810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
8d820 67 69 6e 20 66 69 6c 65 20 6d 65 6d 35 2e 63 20  gin file mem5.c 
8d830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
8d860 0a 2a 2a 20 32 30 30 37 20 4f 63 74 6f 62 65 72  .** 2007 October
8d870 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   14.**.** The au
8d880 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
8d890 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
8d8a0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
8d8b0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
8d8c0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
8d8d0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
8d8e0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
8d8f0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
8d900 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
8d910 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
8d920 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
8d930 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
8d940 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
8d950 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
8d960 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
8d970 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
8d980 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
8d990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d9a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d9d0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
8d9e0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66  contains the C f
8d9f0 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d  unctions that im
8da00 70 6c 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79  plement a memory
8da10 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  .** allocation s
8da20 75 62 73 79 73 74 65 6d 20 66 6f 72 20 75 73 65  ubsystem for use
8da30 20 62 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a   by SQLite. .**.
8da40 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20  ** This version 
8da50 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  of the memory al
8da60 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
8da70 65 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20  em omits all.** 
8da80 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e  use of malloc().
8da90 20 54 68 65 20 53 51 4c 69 74 65 20 75 73 65 72   The SQLite user
8daa0 20 73 75 70 70 6c 69 65 73 20 61 20 62 6c 6f 63   supplies a bloc
8dab0 6b 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62  k of memory.** b
8dac0 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 71  efore calling sq
8dad0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
8dae0 28 29 20 66 72 6f 6d 20 77 68 69 63 68 20 61 6c  () from which al
8daf0 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65  locations.** are
8db00 20 6d 61 64 65 20 61 6e 64 20 72 65 74 75 72 6e   made and return
8db10 65 64 20 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f  ed by the xMallo
8db20 63 28 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63  c() and xRealloc
8db30 28 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  () .** implement
8db40 61 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c  ations. Once sql
8db50 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
8db60 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65  ) has been calle
8db70 64 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74  d,.** the amount
8db80 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c   of memory avail
8db90 61 62 6c 65 20 74 6f 20 53 51 4c 69 74 65 20 69  able to SQLite i
8dba0 73 20 66 69 78 65 64 20 61 6e 64 20 63 61 6e 6e  s fixed and cann
8dbb0 6f 74 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65 64  ot.** be changed
8dbc0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72  ..**.** This ver
8dbd0 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f  sion of the memo
8dbe0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75  ry allocation su
8dbf0 62 73 79 73 74 65 6d 20 69 73 20 69 6e 63 6c 75  bsystem is inclu
8dc00 64 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75  ded.** in the bu
8dc10 69 6c 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49  ild only if SQLI
8dc20 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  TE_ENABLE_MEMSYS
8dc30 35 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a  5 is defined..**
8dc40 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 35 2e 63 2c  .** $Id: mem5.c,
8dc50 76 20 31 2e 31 35 20 32 30 30 38 2f 31 30 2f 32  v 1.15 2008/10/2
8dc60 38 20 31 38 3a 35 38 3a 32 30 20 64 72 68 20 45  8 18:58:20 drh E
8dc70 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  xp $.*/../*.** T
8dc80 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  his version of t
8dc90 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
8dca0 74 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c 79  tor is used only
8dcb0 20 77 68 65 6e 20 0a 2a 2a 20 53 51 4c 49 54 45   when .** SQLITE
8dcc0 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20  _ENABLE_MEMSYS5 
8dcd0 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23  is defined..*/.#
8dce0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
8dcf0 42 4c 45 5f 4d 45 4d 53 59 53 35 0a 0a 2f 2a 0a  BLE_MEMSYS5../*.
8dd00 2a 2a 20 41 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c  ** A minimum all
8dd10 6f 63 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e  ocation is an in
8dd20 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
8dd30 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
8dd40 65 2e 0a 2a 2a 20 4c 61 72 67 65 72 20 61 6c 6c  e..** Larger all
8dd50 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 61 6e 20  ocations are an 
8dd60 61 72 72 61 79 20 6f 66 20 74 68 65 73 65 20 73  array of these s
8dd70 74 72 75 63 74 75 72 65 73 20 77 68 65 72 65 20  tructures where 
8dd80 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74  the.** size of t
8dd90 68 65 20 61 72 72 61 79 20 69 73 20 61 20 70 6f  he array is a po
8dda0 77 65 72 20 6f 66 20 32 2e 0a 2a 2f 0a 74 79 70  wer of 2..*/.typ
8ddb0 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d 35  edef struct Mem5
8ddc0 4c 69 6e 6b 20 4d 65 6d 35 4c 69 6e 6b 3b 0a 73  Link Mem5Link;.s
8ddd0 74 72 75 63 74 20 4d 65 6d 35 4c 69 6e 6b 20 7b  truct Mem5Link {
8dde0 0a 20 20 69 6e 74 20 6e 65 78 74 3b 20 20 20 20  .  int next;    
8ddf0 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 6e     /* Index of n
8de00 65 78 74 20 66 72 65 65 20 63 68 75 6e 6b 20 2a  ext free chunk *
8de10 2f 0a 20 20 69 6e 74 20 70 72 65 76 3b 20 20 20  /.  int prev;   
8de20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
8de30 70 72 65 76 69 6f 75 73 20 66 72 65 65 20 63 68  previous free ch
8de40 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  unk */.};../*.**
8de50 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66   Maximum size of
8de60 20 61 6e 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   any allocation 
8de70 69 73 20 28 28 31 3c 3c 4c 4f 47 4d 41 58 29 2a  is ((1<<LOGMAX)*
8de80 6d 65 6d 35 2e 6e 41 74 6f 6d 29 2e 20 53 69 6e  mem5.nAtom). Sin
8de90 63 65 0a 2a 2a 20 6d 65 6d 35 2e 6e 41 74 6f 6d  ce.** mem5.nAtom
8dea0 20 69 73 20 61 6c 77 61 79 73 20 61 74 20 6c 65   is always at le
8deb0 61 73 74 20 38 2c 20 74 68 69 73 20 69 73 20 6e  ast 8, this is n
8dec0 6f 74 20 72 65 61 6c 6c 79 20 61 20 70 72 61 63  ot really a prac
8ded0 74 69 63 61 6c 0a 2a 2a 20 6c 69 6d 69 74 61 74  tical.** limitat
8dee0 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ion..*/.#define 
8def0 4c 4f 47 4d 41 58 20 33 30 0a 0a 2f 2a 0a 2a 2a  LOGMAX 30../*.**
8df00 20 4d 61 73 6b 73 20 75 73 65 64 20 66 6f 72 20   Masks used for 
8df10 6d 65 6d 35 2e 61 43 74 72 6c 5b 5d 20 65 6c 65  mem5.aCtrl[] ele
8df20 6d 65 6e 74 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ments..*/.#defin
8df30 65 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 20 20  e CTRL_LOGSIZE  
8df40 30 78 31 66 20 20 20 20 2f 2a 20 4c 6f 67 32 20  0x1f    /* Log2 
8df50 53 69 7a 65 20 6f 66 20 74 68 69 73 20 62 6c 6f  Size of this blo
8df60 63 6b 20 72 65 6c 61 74 69 76 65 20 74 6f 20 50  ck relative to P
8df70 4f 57 32 5f 4d 49 4e 20 2a 2f 0a 23 64 65 66 69  OW2_MIN */.#defi
8df80 6e 65 20 43 54 52 4c 5f 46 52 45 45 20 20 20 20  ne CTRL_FREE    
8df90 20 30 78 32 30 20 20 20 20 2f 2a 20 54 72 75 65   0x20    /* True
8dfa0 20 69 66 20 6e 6f 74 20 63 68 65 63 6b 65 64 20   if not checked 
8dfb0 6f 75 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c  out */../*.** Al
8dfc0 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 63 20  l of the static 
8dfd0 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 20 62  variables used b
8dfe0 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 72  y this module ar
8dff0 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 69  e collected.** i
8e000 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74 72  nto a single str
8e010 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d 65  ucture named "me
8e020 6d 35 22 2e 20 20 54 68 69 73 20 69 73 20 74 6f  m5".  This is to
8e030 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 61   keep the.** sta
8e040 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f 72  tic variables or
8e050 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 72  ganized and to r
8e060 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65 20  educe namespace 
8e070 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 65  pollution.** whe
8e080 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73  n this module is
8e090 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 6f   combined with o
8e0a0 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61 6c  ther in the amal
8e0b0 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  gamation..*/.sta
8e0c0 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73  tic SQLITE_WSD s
8e0d0 74 72 75 63 74 20 4d 65 6d 35 47 6c 6f 62 61 6c  truct Mem5Global
8e0e0 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 65 6d   {.  /*.  ** Mem
8e0f0 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66 6f  ory available fo
8e100 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 20 20 2a  r allocation.  *
8e110 2f 0a 20 20 69 6e 74 20 6e 41 74 6f 6d 3b 20 20  /.  int nAtom;  
8e120 20 20 20 20 20 2f 2a 20 53 6d 61 6c 6c 65 73 74       /* Smallest
8e130 20 70 6f 73 73 69 62 6c 65 20 61 6c 6c 6f 63 61   possible alloca
8e140 74 69 6f 6e 20 69 6e 20 62 79 74 65 73 20 2a 2f  tion in bytes */
8e150 0a 20 20 69 6e 74 20 6e 42 6c 6f 63 6b 3b 20 20  .  int nBlock;  
8e160 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
8e170 20 6e 41 74 6f 6d 20 73 69 7a 65 64 20 62 6c 6f   nAtom sized blo
8e180 63 6b 73 20 69 6e 20 7a 50 6f 6f 6c 20 2a 2f 0a  cks in zPool */.
8e190 20 20 75 38 20 2a 7a 50 6f 6f 6c 3b 0a 20 20 0a    u8 *zPool;.  .
8e1a0 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20    /*.  ** Mutex 
8e1b0 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73  to control acces
8e1c0 73 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20  s to the memory 
8e1d0 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
8e1e0 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  stem..  */.  sql
8e1f0 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65  ite3_mutex *mute
8e200 78 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 65  x;..  /*.  ** Pe
8e210 72 66 6f 72 6d 61 6e 63 65 20 73 74 61 74 69 73  rformance statis
8e220 74 69 63 73 0a 20 20 2a 2f 0a 20 20 75 36 34 20  tics.  */.  u64 
8e230 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20  nAlloc;         
8e240 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20  /* Total number 
8e250 6f 66 20 63 61 6c 6c 73 20 74 6f 20 6d 61 6c 6c  of calls to mall
8e260 6f 63 20 2a 2f 0a 20 20 75 36 34 20 74 6f 74 61  oc */.  u64 tota
8e270 6c 41 6c 6c 6f 63 3b 20 20 20 20 20 2f 2a 20 54  lAlloc;     /* T
8e280 6f 74 61 6c 20 6f 66 20 61 6c 6c 20 6d 61 6c 6c  otal of all mall
8e290 6f 63 20 63 61 6c 6c 73 20 2d 20 69 6e 63 6c 75  oc calls - inclu
8e2a0 64 65 73 20 69 6e 74 65 72 6e 61 6c 20 66 72 61  des internal fra
8e2b0 67 20 2a 2f 0a 20 20 75 36 34 20 74 6f 74 61 6c  g */.  u64 total
8e2c0 45 78 63 65 73 73 3b 20 20 20 20 2f 2a 20 54 6f  Excess;    /* To
8e2d0 74 61 6c 20 69 6e 74 65 72 6e 61 6c 20 66 72 61  tal internal fra
8e2e0 67 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20  gmentation */.  
8e2f0 75 33 32 20 63 75 72 72 65 6e 74 4f 75 74 3b 20  u32 currentOut; 
8e300 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 63      /* Current c
8e310 68 65 63 6b 6f 75 74 2c 20 69 6e 63 6c 75 64 69  heckout, includi
8e320 6e 67 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67  ng internal frag
8e330 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 75  mentation */.  u
8e340 33 32 20 63 75 72 72 65 6e 74 43 6f 75 6e 74 3b  32 currentCount;
8e350 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75     /* Current nu
8e360 6d 62 65 72 20 6f 66 20 64 69 73 74 69 6e 63 74  mber of distinct
8e370 20 63 68 65 63 6b 6f 75 74 73 20 2a 2f 0a 20 20   checkouts */.  
8e380 75 33 32 20 6d 61 78 4f 75 74 3b 20 20 20 20 20  u32 maxOut;     
8e390 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 69      /* Maximum i
8e3a0 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 63 75 72  nstantaneous cur
8e3b0 72 65 6e 74 4f 75 74 20 2a 2f 0a 20 20 75 33 32  rentOut */.  u32
8e3c0 20 6d 61 78 43 6f 75 6e 74 3b 20 20 20 20 20 20   maxCount;      
8e3d0 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 69 6e 73 74   /* Maximum inst
8e3e0 61 6e 74 61 6e 65 6f 75 73 20 63 75 72 72 65 6e  antaneous curren
8e3f0 74 43 6f 75 6e 74 20 2a 2f 0a 20 20 75 33 32 20  tCount */.  u32 
8e400 6d 61 78 52 65 71 75 65 73 74 3b 20 20 20 20 20  maxRequest;     
8e410 2f 2a 20 4c 61 72 67 65 73 74 20 61 6c 6c 6f 63  /* Largest alloc
8e420 61 74 69 6f 6e 20 28 65 78 63 6c 75 73 69 76 65  ation (exclusive
8e430 20 6f 66 20 69 6e 74 65 72 6e 61 6c 20 66 72 61   of internal fra
8e440 67 29 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20  g) */.  .  /*.  
8e450 2a 2a 20 4c 69 73 74 73 20 6f 66 20 66 72 65 65  ** Lists of free
8e460 20 62 6c 6f 63 6b 73 20 6f 66 20 76 61 72 69 6f   blocks of vario
8e470 75 73 20 73 69 7a 65 73 2e 0a 20 20 2a 2f 0a 20  us sizes..  */. 
8e480 20 69 6e 74 20 61 69 46 72 65 65 6c 69 73 74 5b   int aiFreelist[
8e490 4c 4f 47 4d 41 58 2b 31 5d 3b 0a 0a 20 20 2f 2a  LOGMAX+1];..  /*
8e4a0 0a 20 20 2a 2a 20 53 70 61 63 65 20 66 6f 72 20  .  ** Space for 
8e4b0 74 72 61 63 6b 69 6e 67 20 77 68 69 63 68 20 62  tracking which b
8e4c0 6c 6f 63 6b 73 20 61 72 65 20 63 68 65 63 6b 65  locks are checke
8e4d0 64 20 6f 75 74 20 61 6e 64 20 74 68 65 20 73 69  d out and the si
8e4e0 7a 65 0a 20 20 2a 2a 20 6f 66 20 65 61 63 68 20  ze.  ** of each 
8e4f0 62 6c 6f 63 6b 2e 20 20 4f 6e 65 20 62 79 74 65  block.  One byte
8e500 20 70 65 72 20 62 6c 6f 63 6b 2e 0a 20 20 2a 2f   per block..  */
8e510 0a 20 20 75 38 20 2a 61 43 74 72 6c 3b 0a 0a 7d  .  u8 *aCtrl;..}
8e520 20 6d 65 6d 35 20 3d 20 7b 20 31 39 38 30 34 31   mem5 = { 198041
8e530 36 37 20 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 6d  67 };..#define m
8e540 65 6d 35 20 47 4c 4f 42 41 4c 28 73 74 72 75 63  em5 GLOBAL(struc
8e550 74 20 4d 65 6d 35 47 6c 6f 62 61 6c 2c 20 6d 65  t Mem5Global, me
8e560 6d 35 29 0a 0a 23 64 65 66 69 6e 65 20 4d 45 4d  m5)..#define MEM
8e570 35 4c 49 4e 4b 28 69 64 78 29 20 28 28 4d 65 6d  5LINK(idx) ((Mem
8e580 35 4c 69 6e 6b 20 2a 29 28 26 6d 65 6d 35 2e 7a  5Link *)(&mem5.z
8e590 50 6f 6f 6c 5b 28 69 64 78 29 2a 6d 65 6d 35 2e  Pool[(idx)*mem5.
8e5a0 6e 41 74 6f 6d 5d 29 29 0a 0a 2f 2a 0a 2a 2a 20  nAtom]))../*.** 
8e5b0 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b  Unlink the chunk
8e5c0 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 69   at mem5.aPool[i
8e5d0 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69 74 20 69  ] from list it i
8e5e0 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f  s currently.** o
8e5f0 6e 2e 20 20 49 74 20 73 68 6f 75 6c 64 20 62 65  n.  It should be
8e600 20 66 6f 75 6e 64 20 6f 6e 20 6d 65 6d 35 2e 61   found on mem5.a
8e610 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69  iFreelist[iLogsi
8e620 7a 65 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ze]..*/.static v
8e630 6f 69 64 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e  oid memsys5Unlin
8e640 6b 28 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c 6f  k(int i, int iLo
8e650 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 6e 65  gsize){.  int ne
8e660 78 74 2c 20 70 72 65 76 3b 0a 20 20 61 73 73 65  xt, prev;.  asse
8e670 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d 65  rt( i>=0 && i<me
8e680 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61  m5.nBlock );.  a
8e690 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65 3e  ssert( iLogsize>
8e6a0 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c 3d  =0 && iLogsize<=
8e6b0 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 61 73 73 65  LOGMAX );.  asse
8e6c0 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b  rt( (mem5.aCtrl[
8e6d0 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49 5a  i] & CTRL_LOGSIZ
8e6e0 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20 29 3b 0a  E)==iLogsize );.
8e6f0 0a 20 20 6e 65 78 74 20 3d 20 4d 45 4d 35 4c 49  .  next = MEM5LI
8e700 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20 70  NK(i)->next;.  p
8e710 72 65 76 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69  rev = MEM5LINK(i
8e720 29 2d 3e 70 72 65 76 3b 0a 20 20 69 66 28 20 70  )->prev;.  if( p
8e730 72 65 76 3c 30 20 29 7b 0a 20 20 20 20 6d 65 6d  rev<0 ){.    mem
8e740 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f  5.aiFreelist[iLo
8e750 67 73 69 7a 65 5d 20 3d 20 6e 65 78 74 3b 0a 20  gsize] = next;. 
8e760 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 45 4d 35   }else{.    MEM5
8e770 4c 49 4e 4b 28 70 72 65 76 29 2d 3e 6e 65 78 74  LINK(prev)->next
8e780 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69   = next;.  }.  i
8e790 66 28 20 6e 65 78 74 3e 3d 30 20 29 7b 0a 20 20  f( next>=0 ){.  
8e7a0 20 20 4d 45 4d 35 4c 49 4e 4b 28 6e 65 78 74 29    MEM5LINK(next)
8e7b0 2d 3e 70 72 65 76 20 3d 20 70 72 65 76 3b 0a 20  ->prev = prev;. 
8e7c0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b   }.}../*.** Link
8e7d0 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 6d 65   the chunk at me
8e7e0 6d 35 2e 61 50 6f 6f 6c 5b 69 5d 20 73 6f 20 74  m5.aPool[i] so t
8e7f0 68 61 74 20 69 73 20 6f 6e 20 74 68 65 20 69 4c  hat is on the iL
8e800 6f 67 73 69 7a 65 0a 2a 2a 20 66 72 65 65 20 6c  ogsize.** free l
8e810 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ist..*/.static v
8e820 6f 69 64 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28  oid memsys5Link(
8e830 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c 6f 67 73  int i, int iLogs
8e840 69 7a 65 29 7b 0a 20 20 69 6e 74 20 78 3b 0a 20  ize){.  int x;. 
8e850 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
8e860 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 35  _mutex_held(mem5
8e870 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  .mutex) );.  ass
8e880 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d  ert( i>=0 && i<m
8e890 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20  em5.nBlock );.  
8e8a0 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65  assert( iLogsize
8e8b0 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c  >=0 && iLogsize<
8e8c0 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 61 73 73  =LOGMAX );.  ass
8e8d0 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c  ert( (mem5.aCtrl
8e8e0 5b 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49  [i] & CTRL_LOGSI
8e8f0 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20 29 3b  ZE)==iLogsize );
8e900 0a 0a 20 20 78 20 3d 20 4d 45 4d 35 4c 49 4e 4b  ..  x = MEM5LINK
8e910 28 69 29 2d 3e 6e 65 78 74 20 3d 20 6d 65 6d 35  (i)->next = mem5
8e920 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67  .aiFreelist[iLog
8e930 73 69 7a 65 5d 3b 0a 20 20 4d 45 4d 35 4c 49 4e  size];.  MEM5LIN
8e940 4b 28 69 29 2d 3e 70 72 65 76 20 3d 20 2d 31 3b  K(i)->prev = -1;
8e950 0a 20 20 69 66 28 20 78 3e 3d 30 20 29 7b 0a 20  .  if( x>=0 ){. 
8e960 20 20 20 61 73 73 65 72 74 28 20 78 3c 6d 65 6d     assert( x<mem
8e970 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 20 20  5.nBlock );.    
8e980 4d 45 4d 35 4c 49 4e 4b 28 78 29 2d 3e 70 72 65  MEM5LINK(x)->pre
8e990 76 20 3d 20 69 3b 0a 20 20 7d 0a 20 20 6d 65 6d  v = i;.  }.  mem
8e9a0 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f  5.aiFreelist[iLo
8e9b0 67 73 69 7a 65 5d 20 3d 20 69 3b 0a 7d 0a 0a 2f  gsize] = i;.}../
8e9c0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41 54  *.** If the STAT
8e9d0 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73 20  IC_MEM mutex is 
8e9e0 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64  not already held
8e9f0 2c 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e  , obtain it now.
8ea00 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69   The mutex.** wi
8ea10 6c 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68 65  ll already be he
8ea20 6c 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79 20  ld (obtained by 
8ea30 63 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63  code in malloc.c
8ea40 29 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47  ) if.** sqlite3G
8ea50 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d  lobalConfig.bMem
8ea60 53 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f  Stat is true..*/
8ea70 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
8ea80 73 79 73 35 45 6e 74 65 72 28 76 6f 69 64 29 7b  sys5Enter(void){
8ea90 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
8eaa0 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73  obalConfig.bMems
8eab0 74 61 74 3d 3d 30 20 26 26 20 6d 65 6d 35 2e 6d  tat==0 && mem5.m
8eac0 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 6d  utex==0 ){.    m
8ead0 65 6d 35 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69  em5.mutex = sqli
8eae0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
8eaf0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
8eb00 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 73 71  C_MEM);.  }.  sq
8eb10 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
8eb20 72 28 6d 65 6d 35 2e 6d 75 74 65 78 29 3b 0a 7d  r(mem5.mutex);.}
8eb30 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
8eb40 73 79 73 35 4c 65 61 76 65 28 76 6f 69 64 29 7b  sys5Leave(void){
8eb50 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
8eb60 5f 6c 65 61 76 65 28 6d 65 6d 35 2e 6d 75 74 65  _leave(mem5.mute
8eb70 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  x);.}../*.** Ret
8eb80 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20  urn the size of 
8eb90 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61  an outstanding a
8eba0 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 62 79  llocation, in by
8ebb0 74 65 73 2e 20 20 54 68 65 0a 2a 2a 20 73 69 7a  tes.  The.** siz
8ebc0 65 20 72 65 74 75 72 6e 65 64 20 6f 6d 69 74 73  e returned omits
8ebd0 20 74 68 65 20 38 2d 62 79 74 65 20 68 65 61 64   the 8-byte head
8ebe0 65 72 20 6f 76 65 72 68 65 61 64 2e 20 20 54 68  er overhead.  Th
8ebf0 69 73 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73  is only.** works
8ec00 20 66 6f 72 20 63 68 75 6e 6b 73 20 74 68 61 74   for chunks that
8ec10 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 63   are currently c
8ec20 68 65 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 73  hecked out..*/.s
8ec30 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73  tatic int memsys
8ec40 35 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a  5Size(void *p){.
8ec50 20 20 69 6e 74 20 69 53 69 7a 65 20 3d 20 30 3b    int iSize = 0;
8ec60 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
8ec70 69 6e 74 20 69 20 3d 20 28 28 75 38 20 2a 29 70  int i = ((u8 *)p
8ec80 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d  -mem5.zPool)/mem
8ec90 35 2e 6e 41 74 6f 6d 3b 0a 20 20 20 20 61 73 73  5.nAtom;.    ass
8eca0 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d  ert( i>=0 && i<m
8ecb0 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20  em5.nBlock );.  
8ecc0 20 20 69 53 69 7a 65 20 3d 20 6d 65 6d 35 2e 6e    iSize = mem5.n
8ecd0 41 74 6f 6d 20 2a 20 28 31 20 3c 3c 20 28 6d 65  Atom * (1 << (me
8ece0 6d 35 2e 61 43 74 72 6c 5b 69 5d 26 43 54 52 4c  m5.aCtrl[i]&CTRL
8ecf0 5f 4c 4f 47 53 49 5a 45 29 29 3b 0a 20 20 7d 0a  _LOGSIZE));.  }.
8ed00 20 20 72 65 74 75 72 6e 20 69 53 69 7a 65 3b 0a    return iSize;.
8ed10 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68  }../*.** Find th
8ed20 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 6f 6e  e first entry on
8ed30 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 4c   the freelist iL
8ed40 6f 67 73 69 7a 65 2e 20 20 55 6e 6c 69 6e 6b 20  ogsize.  Unlink 
8ed50 74 68 61 74 0a 2a 2a 20 65 6e 74 72 79 20 61 6e  that.** entry an
8ed60 64 20 72 65 74 75 72 6e 20 69 74 73 20 69 6e 64  d return its ind
8ed70 65 78 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ex. .*/.static i
8ed80 6e 74 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b  nt memsys5Unlink
8ed90 46 69 72 73 74 28 69 6e 74 20 69 4c 6f 67 73 69  First(int iLogsi
8eda0 7a 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  ze){.  int i;.  
8edb0 69 6e 74 20 69 46 69 72 73 74 3b 0a 0a 20 20 61  int iFirst;..  a
8edc0 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65 3e  ssert( iLogsize>
8edd0 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c 3d  =0 && iLogsize<=
8ede0 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 69 20 3d 20  LOGMAX );.  i = 
8edf0 69 46 69 72 73 74 20 3d 20 6d 65 6d 35 2e 61 69  iFirst = mem5.ai
8ee00 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a  Freelist[iLogsiz
8ee10 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 69 46  e];.  assert( iF
8ee20 69 72 73 74 3e 3d 30 20 29 3b 0a 20 20 77 68 69  irst>=0 );.  whi
8ee30 6c 65 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 69  le( i>0 ){.    i
8ee40 66 28 20 69 3c 69 46 69 72 73 74 20 29 20 69 46  f( i<iFirst ) iF
8ee50 69 72 73 74 20 3d 20 69 3b 0a 20 20 20 20 69 20  irst = i;.    i 
8ee60 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e  = MEM5LINK(i)->n
8ee70 65 78 74 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79  ext;.  }.  memsy
8ee80 73 35 55 6e 6c 69 6e 6b 28 69 46 69 72 73 74 2c  s5Unlink(iFirst,
8ee90 20 69 4c 6f 67 73 69 7a 65 29 3b 0a 20 20 72 65   iLogsize);.  re
8eea0 74 75 72 6e 20 69 46 69 72 73 74 3b 0a 7d 0a 0a  turn iFirst;.}..
8eeb0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 62  /*.** Return a b
8eec0 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 6f  lock of memory o
8eed0 66 20 61 74 20 6c 65 61 73 74 20 6e 42 79 74 65  f at least nByte
8eee0 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 52 65  s in size..** Re
8eef0 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 75 6e 61  turn NULL if una
8ef00 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ble..*/.static v
8ef10 6f 69 64 20 2a 6d 65 6d 73 79 73 35 4d 61 6c 6c  oid *memsys5Mall
8ef20 6f 63 55 6e 73 61 66 65 28 69 6e 74 20 6e 42 79  ocUnsafe(int nBy
8ef30 74 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20  te){.  int i;   
8ef40 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
8ef50 20 6f 66 20 61 20 6d 65 6d 35 2e 61 50 6f 6f 6c   of a mem5.aPool
8ef60 5b 5d 20 73 6c 6f 74 20 2a 2f 0a 20 20 69 6e 74  [] slot */.  int
8ef70 20 69 42 69 6e 3b 20 20 20 20 20 20 20 20 2f 2a   iBin;        /*
8ef80 20 49 6e 64 65 78 20 69 6e 74 6f 20 6d 65 6d 35   Index into mem5
8ef90 2e 61 69 46 72 65 65 6c 69 73 74 5b 5d 20 2a 2f  .aiFreelist[] */
8efa0 0a 20 20 69 6e 74 20 69 46 75 6c 6c 53 7a 3b 20  .  int iFullSz; 
8efb0 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61      /* Size of a
8efc0 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 6e 64 65  llocation rounde
8efd0 64 20 75 70 20 74 6f 20 70 6f 77 65 72 20 6f 66  d up to power of
8efe0 20 32 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 6f 67   2 */.  int iLog
8eff0 73 69 7a 65 3b 20 20 20 20 2f 2a 20 4c 6f 67 32  size;    /* Log2
8f000 20 6f 66 20 69 46 75 6c 6c 53 7a 2f 50 4f 57 32   of iFullSz/POW2
8f010 5f 4d 49 4e 20 2a 2f 0a 0a 20 20 2f 2a 20 4b 65  _MIN */..  /* Ke
8f020 65 70 20 74 72 61 63 6b 20 6f 66 20 74 68 65 20  ep track of the 
8f030 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69  maximum allocati
8f040 6f 6e 20 72 65 71 75 65 73 74 2e 20 20 45 76 65  on request.  Eve
8f050 6e 20 75 6e 66 75 6c 66 69 6c 6c 65 64 0a 20 20  n unfulfilled.  
8f060 2a 2a 20 72 65 71 75 65 73 74 73 20 61 72 65 20  ** requests are 
8f070 63 6f 75 6e 74 65 64 20 2a 2f 0a 20 20 69 66 28  counted */.  if(
8f080 20 6e 42 79 74 65 3e 6d 65 6d 35 2e 6d 61 78 52   nByte>mem5.maxR
8f090 65 71 75 65 73 74 20 29 7b 0a 20 20 20 20 6d 65  equest ){.    me
8f0a0 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 20 3d 20  m5.maxRequest = 
8f0b0 6e 42 79 74 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  nByte;.  }..  /*
8f0c0 20 52 6f 75 6e 64 20 6e 42 79 74 65 20 75 70 20   Round nByte up 
8f0d0 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69  to the next vali
8f0e0 64 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 2a  d power of two *
8f0f0 2f 0a 20 20 69 66 28 20 6e 42 79 74 65 3e 50 4f  /.  if( nByte>PO
8f100 57 32 5f 4d 41 58 20 29 20 72 65 74 75 72 6e 20  W2_MAX ) return 
8f110 30 3b 0a 20 20 66 6f 72 28 69 46 75 6c 6c 53 7a  0;.  for(iFullSz
8f120 3d 6d 65 6d 35 2e 6e 41 74 6f 6d 2c 20 69 4c 6f  =mem5.nAtom, iLo
8f130 67 73 69 7a 65 3d 30 3b 20 69 46 75 6c 6c 53 7a  gsize=0; iFullSz
8f140 3c 6e 42 79 74 65 3b 20 69 46 75 6c 6c 53 7a 20  <nByte; iFullSz 
8f150 2a 3d 20 32 2c 20 69 4c 6f 67 73 69 7a 65 2b 2b  *= 2, iLogsize++
8f160 29 7b 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73  ){}..  /* Make s
8f170 75 72 65 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c  ure mem5.aiFreel
8f180 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 63 6f  ist[iLogsize] co
8f190 6e 74 61 69 6e 73 20 61 74 20 6c 65 61 73 74 20  ntains at least 
8f1a0 6f 6e 65 20 66 72 65 65 0a 20 20 2a 2a 20 62 6c  one free.  ** bl
8f1b0 6f 63 6b 2e 20 20 49 66 20 6e 6f 74 2c 20 74 68  ock.  If not, th
8f1c0 65 6e 20 73 70 6c 69 74 20 61 20 62 6c 6f 63 6b  en split a block
8f1d0 20 6f 66 20 74 68 65 20 6e 65 78 74 20 6c 61 72   of the next lar
8f1e0 67 65 72 20 70 6f 77 65 72 20 6f 66 0a 20 20 2a  ger power of.  *
8f1f0 2a 20 74 77 6f 20 69 6e 20 6f 72 64 65 72 20 74  * two in order t
8f200 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 66  o create a new f
8f210 72 65 65 20 62 6c 6f 63 6b 20 6f 66 20 73 69 7a  ree block of siz
8f220 65 20 69 4c 6f 67 73 69 7a 65 2e 0a 20 20 2a 2f  e iLogsize..  */
8f230 0a 20 20 66 6f 72 28 69 42 69 6e 3d 69 4c 6f 67  .  for(iBin=iLog
8f240 73 69 7a 65 3b 20 6d 65 6d 35 2e 61 69 46 72 65  size; mem5.aiFre
8f250 65 6c 69 73 74 5b 69 42 69 6e 5d 3c 30 20 26 26  elist[iBin]<0 &&
8f260 20 69 42 69 6e 3c 3d 4c 4f 47 4d 41 58 3b 20 69   iBin<=LOGMAX; i
8f270 42 69 6e 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 69  Bin++){}.  if( i
8f280 42 69 6e 3e 4c 4f 47 4d 41 58 20 29 20 72 65 74  Bin>LOGMAX ) ret
8f290 75 72 6e 20 30 3b 0a 20 20 69 20 3d 20 6d 65 6d  urn 0;.  i = mem
8f2a0 73 79 73 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28  sys5UnlinkFirst(
8f2b0 69 42 69 6e 29 3b 0a 20 20 77 68 69 6c 65 28 20  iBin);.  while( 
8f2c0 69 42 69 6e 3e 69 4c 6f 67 73 69 7a 65 20 29 7b  iBin>iLogsize ){
8f2d0 0a 20 20 20 20 69 6e 74 20 6e 65 77 53 69 7a 65  .    int newSize
8f2e0 3b 0a 0a 20 20 20 20 69 42 69 6e 2d 2d 3b 0a 20  ;..    iBin--;. 
8f2f0 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 31 20 3c     newSize = 1 <
8f300 3c 20 69 42 69 6e 3b 0a 20 20 20 20 6d 65 6d 35  < iBin;.    mem5
8f310 2e 61 43 74 72 6c 5b 69 2b 6e 65 77 53 69 7a 65  .aCtrl[i+newSize
8f320 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20  ] = CTRL_FREE | 
8f330 69 42 69 6e 3b 0a 20 20 20 20 6d 65 6d 73 79 73  iBin;.    memsys
8f340 35 4c 69 6e 6b 28 69 2b 6e 65 77 53 69 7a 65 2c  5Link(i+newSize,
8f350 20 69 42 69 6e 29 3b 0a 20 20 7d 0a 20 20 6d 65   iBin);.  }.  me
8f360 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 3d 20 69 4c  m5.aCtrl[i] = iL
8f370 6f 67 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 55 70  ogsize;..  /* Up
8f380 64 61 74 65 20 61 6c 6c 6f 63 61 74 6f 72 20 70  date allocator p
8f390 65 72 66 6f 72 6d 61 6e 63 65 20 73 74 61 74 69  erformance stati
8f3a0 73 74 69 63 73 2e 20 2a 2f 0a 20 20 6d 65 6d 35  stics. */.  mem5
8f3b0 2e 6e 41 6c 6c 6f 63 2b 2b 3b 0a 20 20 6d 65 6d  .nAlloc++;.  mem
8f3c0 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 20 2b 3d 20  5.totalAlloc += 
8f3d0 69 46 75 6c 6c 53 7a 3b 0a 20 20 6d 65 6d 35 2e  iFullSz;.  mem5.
8f3e0 74 6f 74 61 6c 45 78 63 65 73 73 20 2b 3d 20 69  totalExcess += i
8f3f0 46 75 6c 6c 53 7a 20 2d 20 6e 42 79 74 65 3b 0a  FullSz - nByte;.
8f400 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f    mem5.currentCo
8f410 75 6e 74 2b 2b 3b 0a 20 20 6d 65 6d 35 2e 63 75  unt++;.  mem5.cu
8f420 72 72 65 6e 74 4f 75 74 20 2b 3d 20 69 46 75 6c  rrentOut += iFul
8f430 6c 53 7a 3b 0a 20 20 69 66 28 20 6d 65 6d 35 2e  lSz;.  if( mem5.
8f440 6d 61 78 43 6f 75 6e 74 3c 6d 65 6d 35 2e 63 75  maxCount<mem5.cu
8f450 72 72 65 6e 74 43 6f 75 6e 74 20 29 20 6d 65 6d  rrentCount ) mem
8f460 35 2e 6d 61 78 43 6f 75 6e 74 20 3d 20 6d 65 6d  5.maxCount = mem
8f470 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 3b 0a  5.currentCount;.
8f480 20 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78 4f 75    if( mem5.maxOu
8f490 74 3c 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75  t<mem5.currentOu
8f4a0 74 20 29 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 20  t ) mem5.maxOut 
8f4b0 3d 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75  = mem5.currentOu
8f4c0 74 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20  t;..  /* Return 
8f4d0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
8f4e0 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72   allocated memor
8f4f0 79 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28  y. */.  return (
8f500 76 6f 69 64 2a 29 26 6d 65 6d 35 2e 7a 50 6f 6f  void*)&mem5.zPoo
8f510 6c 5b 69 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 5d 3b  l[i*mem5.nAtom];
8f520 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61  .}../*.** Free a
8f530 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6d 65  n outstanding me
8f540 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e  mory allocation.
8f550 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
8f560 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73 61 66  memsys5FreeUnsaf
8f570 65 28 76 6f 69 64 20 2a 70 4f 6c 64 29 7b 0a 20  e(void *pOld){. 
8f580 20 75 33 32 20 73 69 7a 65 2c 20 69 4c 6f 67 73   u32 size, iLogs
8f590 69 7a 65 3b 0a 20 20 69 6e 74 20 69 42 6c 6f 63  ize;.  int iBloc
8f5a0 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0a  k;             .
8f5b0 0a 20 20 2f 2a 20 53 65 74 20 69 42 6c 6f 63 6b  .  /* Set iBlock
8f5c0 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66   to the index of
8f5d0 20 74 68 65 20 62 6c 6f 63 6b 20 70 6f 69 6e 74   the block point
8f5e0 65 64 20 74 6f 20 62 79 20 70 4f 6c 64 20 69 6e  ed to by pOld in
8f5f0 20 0a 20 20 2a 2a 20 74 68 65 20 61 72 72 61 79   .  ** the array
8f600 20 6f 66 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 62   of mem5.nAtom b
8f610 79 74 65 20 62 6c 6f 63 6b 73 20 70 6f 69 6e 74  yte blocks point
8f620 65 64 20 74 6f 20 62 79 20 6d 65 6d 35 2e 7a 50  ed to by mem5.zP
8f630 6f 6f 6c 2e 0a 20 20 2a 2f 0a 20 20 69 42 6c 6f  ool..  */.  iBlo
8f640 63 6b 20 3d 20 28 28 75 38 20 2a 29 70 4f 6c 64  ck = ((u8 *)pOld
8f650 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d  -mem5.zPool)/mem
8f660 35 2e 6e 41 74 6f 6d 3b 0a 0a 20 20 2f 2a 20 43  5.nAtom;..  /* C
8f670 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 6f  heck that the po
8f680 69 6e 74 65 72 20 70 4f 6c 64 20 70 6f 69 6e 74  inter pOld point
8f690 73 20 74 6f 20 61 20 76 61 6c 69 64 2c 20 6e 6f  s to a valid, no
8f6a0 6e 2d 66 72 65 65 20 62 6c 6f 63 6b 2e 20 2a 2f  n-free block. */
8f6b0 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f 63  .  assert( iBloc
8f6c0 6b 3e 3d 30 20 26 26 20 69 42 6c 6f 63 6b 3c 6d  k>=0 && iBlock<m
8f6d0 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20  em5.nBlock );.  
8f6e0 61 73 73 65 72 74 28 20 28 28 75 38 20 2a 29 70  assert( ((u8 *)p
8f6f0 4f 6c 64 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 25  Old-mem5.zPool)%
8f700 6d 65 6d 35 2e 6e 41 74 6f 6d 3d 3d 30 20 29 3b  mem5.nAtom==0 );
8f710 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d 35  .  assert( (mem5
8f720 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 26  .aCtrl[iBlock] &
8f730 20 43 54 52 4c 5f 46 52 45 45 29 3d 3d 30 20 29   CTRL_FREE)==0 )
8f740 3b 0a 0a 20 20 69 4c 6f 67 73 69 7a 65 20 3d 20  ;..  iLogsize = 
8f750 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63  mem5.aCtrl[iBloc
8f760 6b 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49 5a  k] & CTRL_LOGSIZ
8f770 45 3b 0a 20 20 73 69 7a 65 20 3d 20 31 3c 3c 69  E;.  size = 1<<i
8f780 4c 6f 67 73 69 7a 65 3b 0a 20 20 61 73 73 65 72  Logsize;.  asser
8f790 74 28 20 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31  t( iBlock+size-1
8f7a0 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a  <mem5.nBlock );.
8f7b0 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42  .  mem5.aCtrl[iB
8f7c0 6c 6f 63 6b 5d 20 7c 3d 20 43 54 52 4c 5f 46 52  lock] |= CTRL_FR
8f7d0 45 45 3b 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c  EE;.  mem5.aCtrl
8f7e0 5b 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31 5d 20  [iBlock+size-1] 
8f7f0 7c 3d 20 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20  |= CTRL_FREE;.  
8f800 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72  assert( mem5.cur
8f810 72 65 6e 74 43 6f 75 6e 74 3e 30 20 29 3b 0a 20  rentCount>0 );. 
8f820 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75   assert( mem5.cu
8f830 72 72 65 6e 74 4f 75 74 3e 3d 30 20 29 3b 0a 20  rrentOut>=0 );. 
8f840 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75   mem5.currentCou
8f850 6e 74 2d 2d 3b 0a 20 20 6d 65 6d 35 2e 63 75 72  nt--;.  mem5.cur
8f860 72 65 6e 74 4f 75 74 20 2d 3d 20 73 69 7a 65 2a  rentOut -= size*
8f870 6d 65 6d 35 2e 6e 41 74 6f 6d 3b 0a 20 20 61 73  mem5.nAtom;.  as
8f880 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65  sert( mem5.curre
8f890 6e 74 4f 75 74 3e 30 20 7c 7c 20 6d 65 6d 35 2e  ntOut>0 || mem5.
8f8a0 63 75 72 72 65 6e 74 43 6f 75 6e 74 3d 3d 30 20  currentCount==0 
8f8b0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d  );.  assert( mem
8f8c0 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 3e 30  5.currentCount>0
8f8d0 20 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74   || mem5.current
8f8e0 4f 75 74 3d 3d 30 20 29 3b 0a 0a 20 20 6d 65 6d  Out==0 );..  mem
8f8f0 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20  5.aCtrl[iBlock] 
8f900 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c  = CTRL_FREE | iL
8f910 6f 67 73 69 7a 65 3b 0a 20 20 77 68 69 6c 65 28  ogsize;.  while(
8f920 20 69 4c 6f 67 73 69 7a 65 3c 4c 4f 47 4d 41 58   iLogsize<LOGMAX
8f930 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 42 75 64   ){.    int iBud
8f940 64 79 3b 0a 20 20 20 20 69 66 28 20 28 69 42 6c  dy;.    if( (iBl
8f950 6f 63 6b 3e 3e 69 4c 6f 67 73 69 7a 65 29 20 26  ock>>iLogsize) &
8f960 20 31 20 29 7b 0a 20 20 20 20 20 20 69 42 75 64   1 ){.      iBud
8f970 64 79 20 3d 20 69 42 6c 6f 63 6b 20 2d 20 73 69  dy = iBlock - si
8f980 7a 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ze;.    }else{. 
8f990 20 20 20 20 20 69 42 75 64 64 79 20 3d 20 69 42       iBuddy = iB
8f9a0 6c 6f 63 6b 20 2b 20 73 69 7a 65 3b 0a 20 20 20  lock + size;.   
8f9b0 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 69   }.    assert( i
8f9c0 42 75 64 64 79 3e 3d 30 20 29 3b 0a 20 20 20 20  Buddy>=0 );.    
8f9d0 69 66 28 20 28 69 42 75 64 64 79 2b 28 31 3c 3c  if( (iBuddy+(1<<
8f9e0 69 4c 6f 67 73 69 7a 65 29 29 3e 6d 65 6d 35 2e  iLogsize))>mem5.
8f9f0 6e 42 6c 6f 63 6b 20 29 20 62 72 65 61 6b 3b 0a  nBlock ) break;.
8fa00 20 20 20 20 69 66 28 20 6d 65 6d 35 2e 61 43 74      if( mem5.aCt
8fa10 72 6c 5b 69 42 75 64 64 79 5d 21 3d 28 43 54 52  rl[iBuddy]!=(CTR
8fa20 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a  L_FREE | iLogsiz
8fa30 65 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  e) ) break;.    
8fa40 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 42  memsys5Unlink(iB
8fa50 75 64 64 79 2c 20 69 4c 6f 67 73 69 7a 65 29 3b  uddy, iLogsize);
8fa60 0a 20 20 20 20 69 4c 6f 67 73 69 7a 65 2b 2b 3b  .    iLogsize++;
8fa70 0a 20 20 20 20 69 66 28 20 69 42 75 64 64 79 3c  .    if( iBuddy<
8fa80 69 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20  iBlock ){.      
8fa90 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64 64  mem5.aCtrl[iBudd
8faa0 79 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c  y] = CTRL_FREE |
8fab0 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 20 20   iLogsize;.     
8fac0 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f   mem5.aCtrl[iBlo
8fad0 63 6b 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  ck] = 0;.      i
8fae0 42 6c 6f 63 6b 20 3d 20 69 42 75 64 64 79 3b 0a  Block = iBuddy;.
8faf0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
8fb00 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f   mem5.aCtrl[iBlo
8fb10 63 6b 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20  ck] = CTRL_FREE 
8fb20 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 20  | iLogsize;.    
8fb30 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75    mem5.aCtrl[iBu
8fb40 64 64 79 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  ddy] = 0;.    }.
8fb50 20 20 20 20 73 69 7a 65 20 2a 3d 20 32 3b 0a 20      size *= 2;. 
8fb60 20 7d 0a 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b   }.  memsys5Link
8fb70 28 69 42 6c 6f 63 6b 2c 20 69 4c 6f 67 73 69 7a  (iBlock, iLogsiz
8fb80 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  e);.}../*.** All
8fb90 6f 63 61 74 65 20 6e 42 79 74 65 73 20 6f 66 20  ocate nBytes of 
8fba0 6d 65 6d 6f 72 79 0a 2a 2f 0a 73 74 61 74 69 63  memory.*/.static
8fbb0 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 4d 61   void *memsys5Ma
8fbc0 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 73 29  lloc(int nBytes)
8fbd0 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  {.  sqlite3_int6
8fbe0 34 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28 20  4 *p = 0;.  if( 
8fbf0 6e 42 79 74 65 73 3e 30 20 29 7b 0a 20 20 20 20  nBytes>0 ){.    
8fc00 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b 0a  memsys5Enter();.
8fc10 20 20 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 4d      p = memsys5M
8fc20 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74  allocUnsafe(nByt
8fc30 65 73 29 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35  es);.    memsys5
8fc40 4c 65 61 76 65 28 29 3b 0a 20 20 7d 0a 20 20 72  Leave();.  }.  r
8fc50 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b 20  eturn (void*)p; 
8fc60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d  .}../*.** Free m
8fc70 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  emory..*/.static
8fc80 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 46 72 65   void memsys5Fre
8fc90 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b  e(void *pPrior){
8fca0 0a 20 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30  .  if( pPrior==0
8fcb0 20 29 7b 0a 61 73 73 65 72 74 28 30 29 3b 0a 20   ){.assert(0);. 
8fcc0 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
8fcd0 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b   memsys5Enter();
8fce0 0a 20 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e  .  memsys5FreeUn
8fcf0 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20  safe(pPrior);.  
8fd00 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29 3b 20  memsys5Leave(); 
8fd10 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67   .}../*.** Chang
8fd20 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e  e the size of an
8fd30 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79   existing memory
8fd40 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73   allocation.*/.s
8fd50 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73  tatic void *mems
8fd60 79 73 35 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20  ys5Realloc(void 
8fd70 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79  *pPrior, int nBy
8fd80 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64  tes){.  int nOld
8fd90 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 69  ;.  void *p;.  i
8fda0 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a  f( pPrior==0 ){.
8fdb0 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79      return memsy
8fdc0 73 35 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 73 29  s5Malloc(nBytes)
8fdd0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42 79 74  ;.  }.  if( nByt
8fde0 65 73 3c 3d 30 20 29 7b 0a 20 20 20 20 6d 65 6d  es<=0 ){.    mem
8fdf0 73 79 73 35 46 72 65 65 28 70 50 72 69 6f 72 29  sys5Free(pPrior)
8fe00 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
8fe10 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 6d 65 6d    }.  nOld = mem
8fe20 73 79 73 35 53 69 7a 65 28 70 50 72 69 6f 72 29  sys5Size(pPrior)
8fe30 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d  ;.  if( nBytes<=
8fe40 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 72 65 74 75  nOld ){.    retu
8fe50 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20  rn pPrior;.  }. 
8fe60 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b   memsys5Enter();
8fe70 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 4d 61  .  p = memsys5Ma
8fe80 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 65  llocUnsafe(nByte
8fe90 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  s);.  if( p ){. 
8fea0 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 50 72     memcpy(p, pPr
8feb0 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20 20 20  ior, nOld);.    
8fec0 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73 61 66  memsys5FreeUnsaf
8fed0 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20  e(pPrior);.  }. 
8fee0 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29 3b   memsys5Leave();
8fef0 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
8ff00 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61  /*.** Round up a
8ff10 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f   request size to
8ff20 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20   the next valid 
8ff30 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e  allocation size.
8ff40 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
8ff50 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 28 69 6e  emsys5Roundup(in
8ff60 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 46 75 6c  t n){.  int iFul
8ff70 6c 53 7a 3b 0a 20 20 66 6f 72 28 69 46 75 6c 6c  lSz;.  for(iFull
8ff80 53 7a 3d 6d 65 6d 35 2e 6e 41 74 6f 6d 3b 20 69  Sz=mem5.nAtom; i
8ff90 46 75 6c 6c 53 7a 3c 6e 3b 20 69 46 75 6c 6c 53  FullSz<n; iFullS
8ffa0 7a 20 2a 3d 20 32 29 3b 0a 20 20 72 65 74 75 72  z *= 2);.  retur
8ffb0 6e 20 69 46 75 6c 6c 53 7a 3b 0a 7d 0a 0a 73 74  n iFullSz;.}..st
8ffc0 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35  atic int memsys5
8ffd0 4c 6f 67 28 69 6e 74 20 69 56 61 6c 75 65 29 7b  Log(int iValue){
8ffe0 0a 20 20 69 6e 74 20 69 4c 6f 67 3b 0a 20 20 66  .  int iLog;.  f
8fff0 6f 72 28 69 4c 6f 67 3d 30 3b 20 28 31 3c 3c 69  or(iLog=0; (1<<i
90000 4c 6f 67 29 3c 69 56 61 6c 75 65 3b 20 69 4c 6f  Log)<iValue; iLo
90010 67 2b 2b 29 3b 0a 20 20 72 65 74 75 72 6e 20 69  g++);.  return i
90020 4c 6f 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  Log;.}../*.** In
90030 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f  itialize this mo
90040 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  dule..*/.static 
90050 69 6e 74 20 6d 65 6d 73 79 73 35 49 6e 69 74 28  int memsys5Init(
90060 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a  void *NotUsed){.
90070 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20    int ii;.  int 
90080 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 65 33 47  nByte = sqlite3G
90090 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 61  lobalConfig.nHea
900a0 70 3b 0a 20 20 75 38 20 2a 7a 42 79 74 65 20 3d  p;.  u8 *zByte =
900b0 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 47 6c   (u8 *)sqlite3Gl
900c0 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70  obalConfig.pHeap
900d0 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f 67 3b  ;.  int nMinLog;
900e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
900f0 20 2f 2a 20 4c 6f 67 20 6f 66 20 6d 69 6e 69 6d   /* Log of minim
90100 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69  um allocation si
90110 7a 65 20 69 6e 20 62 79 74 65 73 2a 2f 0a 20 20  ze in bytes*/.  
90120 69 6e 74 20 69 4f 66 66 73 65 74 3b 0a 0a 20 20  int iOffset;..  
90130 69 66 28 20 21 7a 42 79 74 65 20 29 7b 0a 20 20  if( !zByte ){.  
90140 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
90150 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 6e 4d  ERROR;.  }..  nM
90160 69 6e 4c 6f 67 20 3d 20 6d 65 6d 73 79 73 35 4c  inLog = memsys5L
90170 6f 67 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  og(sqlite3Global
90180 43 6f 6e 66 69 67 2e 6d 6e 52 65 71 29 3b 0a 20  Config.mnReq);. 
90190 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 3d 20 28 31   mem5.nAtom = (1
901a0 3c 3c 6e 4d 69 6e 4c 6f 67 29 3b 0a 20 20 77 68  <<nMinLog);.  wh
901b0 69 6c 65 28 20 73 69 7a 65 6f 66 28 4d 65 6d 35  ile( sizeof(Mem5
901c0 4c 69 6e 6b 29 3e 6d 65 6d 35 2e 6e 41 74 6f 6d  Link)>mem5.nAtom
901d0 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6e 41 74   ){.    mem5.nAt
901e0 6f 6d 20 3d 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20  om = mem5.nAtom 
901f0 3c 3c 20 31 3b 0a 20 20 7d 0a 0a 20 20 6d 65 6d  << 1;.  }..  mem
90200 35 2e 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42 79 74  5.nBlock = (nByt
90210 65 20 2f 20 28 6d 65 6d 35 2e 6e 41 74 6f 6d 2b  e / (mem5.nAtom+
90220 73 69 7a 65 6f 66 28 75 38 29 29 29 3b 0a 20 20  sizeof(u8)));.  
90230 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 3d 20 7a 42 79  mem5.zPool = zBy
90240 74 65 3b 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c  te;.  mem5.aCtrl
90250 20 3d 20 28 75 38 20 2a 29 26 6d 65 6d 35 2e 7a   = (u8 *)&mem5.z
90260 50 6f 6f 6c 5b 6d 65 6d 35 2e 6e 42 6c 6f 63 6b  Pool[mem5.nBlock
90270 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 5d 3b 0a 0a 20  *mem5.nAtom];.. 
90280 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 3d 4c   for(ii=0; ii<=L
90290 4f 47 4d 41 58 3b 20 69 69 2b 2b 29 7b 0a 20 20  OGMAX; ii++){.  
902a0 20 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73    mem5.aiFreelis
902b0 74 5b 69 69 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a  t[ii] = -1;.  }.
902c0 0a 20 20 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a  .  iOffset = 0;.
902d0 20 20 66 6f 72 28 69 69 3d 4c 4f 47 4d 41 58 3b    for(ii=LOGMAX;
902e0 20 69 69 3e 3d 30 3b 20 69 69 2d 2d 29 7b 0a 20   ii>=0; ii--){. 
902f0 20 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20     int nAlloc = 
90300 28 31 3c 3c 69 69 29 3b 0a 20 20 20 20 69 66 28  (1<<ii);.    if(
90310 20 28 69 4f 66 66 73 65 74 2b 6e 41 6c 6c 6f 63   (iOffset+nAlloc
90320 29 3c 3d 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29  )<=mem5.nBlock )
90330 7b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74  {.      mem5.aCt
90340 72 6c 5b 69 4f 66 66 73 65 74 5d 20 3d 20 69 69  rl[iOffset] = ii
90350 20 7c 20 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20   | CTRL_FREE;.  
90360 20 20 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28      memsys5Link(
90370 69 4f 66 66 73 65 74 2c 20 69 69 29 3b 0a 20 20  iOffset, ii);.  
90380 20 20 20 20 69 4f 66 66 73 65 74 20 2b 3d 20 6e      iOffset += n
90390 41 6c 6c 6f 63 3b 0a 20 20 20 20 7d 0a 20 20 20  Alloc;.    }.   
903a0 20 61 73 73 65 72 74 28 28 69 4f 66 66 73 65 74   assert((iOffset
903b0 2b 6e 41 6c 6c 6f 63 29 3e 6d 65 6d 35 2e 6e 42  +nAlloc)>mem5.nB
903c0 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 20 20 72 65  lock);.  }..  re
903d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
903e0 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69  }../*.** Deiniti
903f0 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c  alize this modul
90400 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
90410 64 20 6d 65 6d 73 79 73 35 53 68 75 74 64 6f 77  d memsys5Shutdow
90420 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29  n(void *NotUsed)
90430 7b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f  {.  return;.}../
90440 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 66 69  *.** Open the fi
90450 6c 65 20 69 6e 64 69 63 61 74 65 64 20 61 6e 64  le indicated and
90460 20 77 72 69 74 65 20 61 20 6c 6f 67 20 6f 66 20   write a log of 
90470 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65 6d 6f  all unfreed memo
90480 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f  ry .** allocatio
90490 6e 73 20 69 6e 74 6f 20 74 68 61 74 20 6c 6f 67  ns into that log
904a0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
904b0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
904c0 4d 65 6d 73 79 73 35 44 75 6d 70 28 63 6f 6e 73  Memsys5Dump(cons
904d0 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
904e0 65 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  e){.#ifdef SQLIT
904f0 45 5f 44 45 42 55 47 0a 20 20 46 49 4c 45 20 2a  E_DEBUG.  FILE *
90500 6f 75 74 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c  out;.  int i, j,
90510 20 6e 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f   n;.  int nMinLo
90520 67 3b 0a 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e  g;..  if( zFilen
90530 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e  ame==0 || zFilen
90540 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20  ame[0]==0 ){.   
90550 20 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20   out = stdout;. 
90560 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20   }else{.    out 
90570 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d  = fopen(zFilenam
90580 65 2c 20 22 77 22 29 3b 0a 20 20 20 20 69 66 28  e, "w");.    if(
90590 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20   out==0 ){.     
905a0 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
905b0 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f   "** Unable to o
905c0 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62  utput memory deb
905d0 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25  ug output log: %
905e0 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20  s **\n",.       
905f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a                 z
90600 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20  Filename);.     
90610 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
90620 20 7d 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65   }.  memsys5Ente
90630 72 28 29 3b 0a 20 20 6e 4d 69 6e 4c 6f 67 20 3d  r();.  nMinLog =
90640 20 6d 65 6d 73 79 73 35 4c 6f 67 28 6d 65 6d 35   memsys5Log(mem5
90650 2e 6e 41 74 6f 6d 29 3b 0a 20 20 66 6f 72 28 69  .nAtom);.  for(i
90660 3d 30 3b 20 69 3c 3d 4c 4f 47 4d 41 58 20 26 26  =0; i<=LOGMAX &&
90670 20 69 2b 6e 4d 69 6e 4c 6f 67 3c 33 32 3b 20 69   i+nMinLog<32; i
90680 2b 2b 29 7b 0a 20 20 20 20 66 6f 72 28 6e 3d 30  ++){.    for(n=0
90690 2c 20 6a 3d 6d 65 6d 35 2e 61 69 46 72 65 65 6c  , j=mem5.aiFreel
906a0 69 73 74 5b 69 5d 3b 20 6a 3e 3d 30 3b 20 6a 20  ist[i]; j>=0; j 
906b0 3d 20 4d 45 4d 35 4c 49 4e 4b 28 6a 29 2d 3e 6e  = MEM5LINK(j)->n
906c0 65 78 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20  ext, n++){}.    
906d0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 66 72  fprintf(out, "fr
906e0 65 65 6c 69 73 74 20 69 74 65 6d 73 20 6f 66 20  eelist items of 
906f0 73 69 7a 65 20 25 64 3a 20 25 64 5c 6e 22 2c 20  size %d: %d\n", 
90700 6d 65 6d 35 2e 6e 41 74 6f 6d 20 3c 3c 20 69 2c  mem5.nAtom << i,
90710 20 6e 29 3b 0a 20 20 7d 0a 20 20 66 70 72 69 6e   n);.  }.  fprin
90720 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6e 41  tf(out, "mem5.nA
90730 6c 6c 6f 63 20 20 20 20 20 20 20 3d 20 25 6c 6c  lloc       = %ll
90740 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6e 41 6c 6c 6f  u\n", mem5.nAllo
90750 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75  c);.  fprintf(ou
90760 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c  t, "mem5.totalAl
90770 6c 6f 63 20 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c  loc   = %llu\n",
90780 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63   mem5.totalAlloc
90790 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74  );.  fprintf(out
907a0 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63  , "mem5.totalExc
907b0 65 73 73 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20  ess  = %llu\n", 
907c0 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 73 73  mem5.totalExcess
907d0 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74  );.  fprintf(out
907e0 2c 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f  , "mem5.currentO
907f0 75 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65  ut   = %u\n", me
90800 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 29 3b 0a  m5.currentOut);.
90810 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
90820 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e  mem5.currentCoun
90830 74 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e  t = %u\n", mem5.
90840 63 75 72 72 65 6e 74 43 6f 75 6e 74 29 3b 0a 20  currentCount);. 
90850 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d   fprintf(out, "m
90860 65 6d 35 2e 6d 61 78 4f 75 74 20 20 20 20 20 20  em5.maxOut      
90870 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6d   = %u\n", mem5.m
90880 61 78 4f 75 74 29 3b 0a 20 20 66 70 72 69 6e 74  axOut);.  fprint
90890 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78  f(out, "mem5.max
908a0 43 6f 75 6e 74 20 20 20 20 20 3d 20 25 75 5c 6e  Count     = %u\n
908b0 22 2c 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e 74  ", mem5.maxCount
908c0 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74  );.  fprintf(out
908d0 2c 20 22 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65  , "mem5.maxReque
908e0 73 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65  st   = %u\n", me
908f0 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 29 3b 0a  m5.maxRequest);.
90900 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29    memsys5Leave()
90910 3b 0a 20 20 69 66 28 20 6f 75 74 3d 3d 73 74 64  ;.  if( out==std
90920 6f 75 74 20 29 7b 0a 20 20 20 20 66 66 6c 75 73  out ){.    fflus
90930 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 7d 65 6c  h(stdout);.  }el
90940 73 65 7b 0a 20 20 20 20 66 63 6c 6f 73 65 28 6f  se{.    fclose(o
90950 75 74 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ut);.  }.#endif.
90960 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
90970 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c  utine is the onl
90980 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69  y routine in thi
90990 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 65  s file with exte
909a0 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 67 65  rnal .** linkage
909b0 2e 20 49 74 20 72 65 74 75 72 6e 73 20 61 20 70  . It returns a p
909c0 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74  ointer to a stat
909d0 69 63 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d  ic sqlite3_mem_m
909e0 65 74 68 6f 64 73 0a 2a 2a 20 73 74 72 75 63 74  ethods.** struct
909f0 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20   populated with 
90a00 74 68 65 20 6d 65 6d 73 79 73 35 20 6d 65 74 68  the memsys5 meth
90a10 6f 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ods..*/.SQLITE_P
90a20 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c  RIVATE const sql
90a30 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
90a40 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d   *sqlite3MemGetM
90a50 65 6d 73 79 73 35 28 76 6f 69 64 29 7b 0a 20 20  emsys5(void){.  
90a60 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c  static const sql
90a70 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
90a80 20 6d 65 6d 73 79 73 35 4d 65 74 68 6f 64 73 20   memsys5Methods 
90a90 3d 20 7b 0a 20 20 20 20 20 6d 65 6d 73 79 73 35  = {.     memsys5
90aa0 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d  Malloc,.     mem
90ab0 73 79 73 35 46 72 65 65 2c 0a 20 20 20 20 20 6d  sys5Free,.     m
90ac0 65 6d 73 79 73 35 52 65 61 6c 6c 6f 63 2c 0a 20  emsys5Realloc,. 
90ad0 20 20 20 20 6d 65 6d 73 79 73 35 53 69 7a 65 2c      memsys5Size,
90ae0 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52 6f 75  .     memsys5Rou
90af0 6e 64 75 70 2c 0a 20 20 20 20 20 6d 65 6d 73 79  ndup,.     memsy
90b00 73 35 49 6e 69 74 2c 0a 20 20 20 20 20 6d 65 6d  s5Init,.     mem
90b10 73 79 73 35 53 68 75 74 64 6f 77 6e 2c 0a 20 20  sys5Shutdown,.  
90b20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 72 65 74 75     0.  };.  retu
90b30 72 6e 20 26 6d 65 6d 73 79 73 35 4d 65 74 68 6f  rn &memsys5Metho
90b40 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a  ds;.}..#endif /*
90b50 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
90b60 45 4d 53 59 53 35 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  EMSYS5 */../****
90b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
90b80 66 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f mem5.c *******
90b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
90bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
90bd0 20 66 69 6c 65 20 6d 75 74 65 78 2e 63 20 2a 2a   file mutex.c **
90be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
90c10 20 32 30 30 37 20 41 75 67 75 73 74 20 31 34 0a   2007 August 14.
90c20 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
90c30 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
90c40 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
90c50 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
90c60 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
90c70 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
90c80 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
90c90 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
90ca0 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
90cb0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
90cc0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
90cd0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
90ce0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
90cf0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
90d00 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
90d10 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
90d20 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
90d30 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
90d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
90d80 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
90d90 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74  ains the C funct
90da0 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d  ions that implem
90db0 65 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a  ent mutexes..**.
90dc0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
90dd0 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61 74 20  tains code that 
90de0 69 73 20 63 6f 6d 6d 6f 6e 20 61 63 72 6f 73 73  is common across
90df0 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70 6c 65   all mutex imple
90e00 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 0a 2a 2a 0a  mentations...**.
90e10 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 2e 63 2c  ** $Id: mutex.c,
90e20 76 20 31 2e 32 39 20 32 30 30 38 2f 31 30 2f 30  v 1.29 2008/10/0
90e30 37 20 31 35 3a 32 35 3a 34 38 20 64 72 68 20 45  7 15:25:48 drh E
90e40 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66  xp $.*/..#ifndef
90e50 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d   SQLITE_MUTEX_OM
90e60 49 54 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c  IT./*.** Initial
90e70 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73 79  ize the mutex sy
90e80 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  stem..*/.SQLITE_
90e90 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
90ea0 74 65 33 4d 75 74 65 78 49 6e 69 74 28 76 6f 69  te3MutexInit(voi
90eb0 64 29 7b 20 0a 20 20 69 6e 74 20 72 63 20 3d 20  d){ .  int rc = 
90ec0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
90ed0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
90ee0 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20  nfig.bCoreMutex 
90ef0 29 7b 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69  ){.    if( !sqli
90f00 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
90f10 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f  mutex.xMutexAllo
90f20 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66  c ){.      /* If
90f30 20 74 68 65 20 78 4d 75 74 65 78 41 6c 6c 6f 63   the xMutexAlloc
90f40 20 6d 65 74 68 6f 64 20 68 61 73 20 6e 6f 74 20   method has not 
90f50 62 65 65 6e 20 73 65 74 2c 20 74 68 65 6e 20 74  been set, then t
90f60 68 65 20 75 73 65 72 20 64 69 64 20 6e 6f 74 0a  he user did not.
90f70 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 61 6c 6c        ** install
90f80 20 61 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65   a mutex impleme
90f90 6e 74 61 74 69 6f 6e 20 76 69 61 20 73 71 6c 69  ntation via sqli
90fa0 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 70 72 69  te3_config() pri
90fb0 6f 72 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20  or to .      ** 
90fc0 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
90fd0 7a 65 28 29 20 62 65 69 6e 67 20 63 61 6c 6c 65  ze() being calle
90fe0 64 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f  d. This block co
90ff0 70 69 65 73 20 70 6f 69 6e 74 65 72 73 20 74 6f  pies pointers to
91000 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64 65  .      ** the de
91010 66 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 61  fault implementa
91020 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 73 71  tion into the sq
91030 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
91040 67 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 20  g structure..   
91050 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54     **.      ** T
91060 68 65 20 64 61 6e 67 65 72 20 69 73 20 74 68 61  he danger is tha
91070 74 20 61 6c 74 68 6f 75 67 68 20 73 71 6c 69 74  t although sqlit
91080 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 6e  e3_config() is n
91090 6f 74 20 61 20 74 68 72 65 61 64 73 61 66 65 0a  ot a threadsafe.
910a0 20 20 20 20 20 20 2a 2a 20 41 50 49 2c 20 73 71        ** API, sq
910b0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
910c0 28 29 20 69 73 2c 20 61 6e 64 20 73 6f 20 6d 75  () is, and so mu
910d0 6c 74 69 70 6c 65 20 74 68 72 65 61 64 73 20 6d  ltiple threads m
910e0 61 79 20 62 65 0a 20 20 20 20 20 20 2a 2a 20 61  ay be.      ** a
910f0 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 72 75 6e  ttempting to run
91100 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73   this function s
91110 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2e 20 54  imultaneously. T
91120 6f 20 67 75 61 72 64 20 77 72 69 74 65 0a 20 20  o guard write.  
91130 20 20 20 20 2a 2a 20 61 63 63 65 73 73 20 74 6f      ** access to
91140 20 74 68 65 20 73 71 6c 69 74 65 33 47 6c 6f 62   the sqlite3Glob
91150 61 6c 43 6f 6e 66 69 67 20 73 74 72 75 63 74 75  alConfig structu
91160 72 65 2c 20 74 68 65 20 27 4d 41 53 54 45 52 27  re, the 'MASTER'
91170 20 73 74 61 74 69 63 20 6d 75 74 65 78 0a 20 20   static mutex.  
91180 20 20 20 20 2a 2a 20 69 73 20 6f 62 74 61 69 6e      ** is obtain
91190 65 64 20 62 65 66 6f 72 65 20 6d 6f 64 69 66 79  ed before modify
911a0 69 6e 67 20 69 74 2e 0a 20 20 20 20 20 20 2a 2f  ing it..      */
911b0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
911c0 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 70 20  utex_methods *p 
911d0 3d 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74  = sqlite3Default
911e0 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 73  Mutex();.      s
911f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4d  qlite3_mutex *pM
91200 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 0a 20 20  aster = 0;.  .  
91210 20 20 20 20 72 63 20 3d 20 70 2d 3e 78 4d 75 74      rc = p->xMut
91220 65 78 49 6e 69 74 28 29 3b 0a 20 20 20 20 20 20  exInit();.      
91230 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
91240 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4d 61  K ){.        pMa
91250 73 74 65 72 20 3d 20 70 2d 3e 78 4d 75 74 65 78  ster = p->xMutex
91260 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
91270 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
91280 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
91290 74 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  t(pMaster);.    
912a0 20 20 20 20 70 2d 3e 78 4d 75 74 65 78 45 6e 74      p->xMutexEnt
912b0 65 72 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20  er(pMaster);.   
912c0 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c       assert( sql
912d0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
912e0 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c  .mutex.xMutexAll
912f0 6f 63 3d 3d 30 20 0a 20 20 20 20 20 20 20 20 20  oc==0 .         
91300 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c      || sqlite3Gl
91310 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78  obalConfig.mutex
91320 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 3d 3d 70 2d  .xMutexAlloc==p-
91330 3e 78 4d 75 74 65 78 41 6c 6c 6f 63 0a 20 20 20  >xMutexAlloc.   
91340 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20       );.        
91350 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62  if( !sqlite3Glob
91360 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
91370 4d 75 74 65 78 41 6c 6c 6f 63 20 29 7b 0a 20 20  MutexAlloc ){.  
91380 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47          sqlite3G
91390 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65  lobalConfig.mute
913a0 78 20 3d 20 2a 70 3b 0a 20 20 20 20 20 20 20 20  x = *p;.        
913b0 7d 0a 20 20 20 20 20 20 20 20 70 2d 3e 78 4d 75  }.        p->xMu
913c0 74 65 78 4c 65 61 76 65 28 70 4d 61 73 74 65 72  texLeave(pMaster
913d0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
913e0 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
913f0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
91400 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65  nfig.mutex.xMute
91410 78 49 6e 69 74 28 29 3b 0a 20 20 20 20 7d 0a 20  xInit();.    }. 
91420 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
91430 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f  .}../*.** Shutdo
91440 77 6e 20 74 68 65 20 6d 75 74 65 78 20 73 79 73  wn the mutex sys
91450 74 65 6d 2e 20 54 68 69 73 20 63 61 6c 6c 20 66  tem. This call f
91460 72 65 65 73 20 72 65 73 6f 75 72 63 65 73 20 61  rees resources a
91470 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a 2a 20 73  llocated by.** s
91480 71 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28  qlite3MutexInit(
91490 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
914a0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
914b0 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 0a  MutexEnd(void){.
914c0 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
914d0 45 5f 4f 4b 3b 0a 20 20 72 63 20 3d 20 73 71 6c  E_OK;.  rc = sql
914e0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
914f0 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 45 6e 64  .mutex.xMutexEnd
91500 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ();.  return rc;
91510 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 72 69 65  .}../*.** Retrie
91520 76 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ve a pointer to 
91530 61 20 73 74 61 74 69 63 20 6d 75 74 65 78 20 6f  a static mutex o
91540 72 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77  r allocate a new
91550 20 64 79 6e 61 6d 69 63 20 6f 6e 65 2e 0a 2a 2f   dynamic one..*/
91560 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
91570 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74  te3_mutex *sqlit
91580 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 69  e3_mutex_alloc(i
91590 6e 74 20 69 64 29 7b 0a 23 69 66 6e 64 65 66 20  nt id){.#ifndef 
915a0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
915b0 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74  INIT.  if( sqlit
915c0 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
915d0 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64  ) return 0;.#end
915e0 69 66 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  if.  return sqli
915f0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
91600 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f  mutex.xMutexAllo
91610 63 28 69 64 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45  c(id);.}..SQLITE
91620 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33  _PRIVATE sqlite3
91630 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 4d  _mutex *sqlite3M
91640 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64  utexAlloc(int id
91650 29 7b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65  ){.  if( !sqlite
91660 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43  3GlobalConfig.bC
91670 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20  oreMutex ){.    
91680 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
91690 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c  return sqlite3Gl
916a0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78  obalConfig.mutex
916b0 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69 64 29  .xMutexAlloc(id)
916c0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20  ;.}../*.** Free 
916d0 61 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 2e  a dynamic mutex.
916e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
916f0 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  oid sqlite3_mute
91700 78 5f 66 72 65 65 28 73 71 6c 69 74 65 33 5f 6d  x_free(sqlite3_m
91710 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20  utex *p){.  if( 
91720 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  p ){.    sqlite3
91730 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
91740 65 78 2e 78 4d 75 74 65 78 46 72 65 65 28 70 29  ex.xMutexFree(p)
91750 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f  ;.  }.}../*.** O
91760 62 74 61 69 6e 20 74 68 65 20 6d 75 74 65 78 20  btain the mutex 
91770 70 2e 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72  p. If some other
91780 20 74 68 72 65 61 64 20 61 6c 72 65 61 64 79 20   thread already 
91790 68 61 73 20 74 68 65 20 6d 75 74 65 78 2c 20 62  has the mutex, b
917a0 6c 6f 63 6b 0a 2a 2a 20 75 6e 74 69 6c 20 69 74  lock.** until it
917b0 20 63 61 6e 20 62 65 20 6f 62 74 61 69 6e 65 64   can be obtained
917c0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
917d0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74  void sqlite3_mut
917e0 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33  ex_enter(sqlite3
917f0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66  _mutex *p){.  if
91800 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ( p ){.    sqlit
91810 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
91820 75 74 65 78 2e 78 4d 75 74 65 78 45 6e 74 65 72  utex.xMutexEnter
91830 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  (p);.  }.}../*.*
91840 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 6d 75 74  * Obtain the mut
91850 65 78 20 70 2e 20 49 66 20 73 75 63 63 65 73 73  ex p. If success
91860 66 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  ful, return SQLI
91870 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65  TE_OK. Otherwise
91880 2c 20 69 66 20 61 6e 6f 74 68 65 72 0a 2a 2a 20  , if another.** 
91890 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68 65  thread holds the
918a0 20 6d 75 74 65 78 20 61 6e 64 20 69 74 20 63 61   mutex and it ca
918b0 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64  nnot be obtained
918c0 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
918d0 42 55 53 59 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  BUSY..*/.SQLITE_
918e0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
918f0 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69 74 65  mutex_try(sqlite
91900 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69  3_mutex *p){.  i
91910 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
91920 4b 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  K;.  if( p ){.  
91930 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
91940 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
91950 65 78 2e 78 4d 75 74 65 78 54 72 79 28 70 29 3b  ex.xMutexTry(p);
91960 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
91970 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  ;.}../*.** The s
91980 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
91990 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69  ve() routine exi
919a0 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20  ts a mutex that 
919b0 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a  was previously.*
919c0 2a 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65  * entered by the
919d0 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54   same thread.  T
919e0 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75  he behavior is u
919f0 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20  ndefined if the 
91a00 6d 75 74 65 78 20 0a 2a 2a 20 69 73 20 6e 6f 74  mutex .** is not
91a10 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72   currently enter
91a20 65 64 2e 20 49 66 20 61 20 4e 55 4c 4c 20 70 6f  ed. If a NULL po
91a30 69 6e 74 65 72 20 69 73 20 70 61 73 73 65 64 20  inter is passed 
91a40 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 0a 2a  as an argument.*
91a50 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * this function 
91a60 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53  is a no-op..*/.S
91a70 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
91a80 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
91a90 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ve(sqlite3_mutex
91aa0 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b   *p){.  if( p ){
91ab0 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
91ac0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
91ad0 4d 75 74 65 78 4c 65 61 76 65 28 70 29 3b 0a 20  MutexLeave(p);. 
91ae0 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44   }.}..#ifndef ND
91af0 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  EBUG./*.** The s
91b00 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
91b10 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  d() and sqlite3_
91b20 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20  mutex_notheld() 
91b30 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69  routine are.** i
91b40 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20  ntended for use 
91b50 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20  inside assert() 
91b60 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53  statements..*/.S
91b70 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
91b80 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
91b90 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
91ba0 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d  p){.  return p==
91bb0 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62  0 || sqlite3Glob
91bc0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
91bd0 4d 75 74 65 78 48 65 6c 64 28 70 29 3b 0a 7d 0a  MutexHeld(p);.}.
91be0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
91bf0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
91c00 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74  held(sqlite3_mut
91c10 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  ex *p){.  return
91c20 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33   p==0 || sqlite3
91c30 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
91c40 65 78 2e 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64  ex.xMutexNotheld
91c50 28 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  (p);.}.#endif..#
91c60 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
91c70 4f 4d 49 54 5f 4d 55 54 45 58 20 2a 2f 0a 0a 2f  OMIT_MUTEX */../
91c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
91c90 6e 64 20 6f 66 20 6d 75 74 65 78 2e 63 20 2a 2a  nd of mutex.c **
91ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
91cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
91ce0 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f  egin file mutex_
91cf0 6e 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  noop.c *********
91d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
91d20 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f 62 65  *.** 2008 Octobe
91d30 72 20 30 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 07.**.** The a
91d40 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
91d50 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
91d60 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
91d70 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
91d80 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
91d90 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
91da0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
91db0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
91dc0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
91dd0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
91de0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
91df0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
91e00 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
91e10 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
91e20 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
91e30 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
91e40 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
91e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91e90 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
91ea0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20   contains the C 
91eb0 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69  functions that i
91ec0 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73  mplement mutexes
91ed0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70  ..**.** This imp
91ee0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 20 74  lementation in t
91ef0 68 69 73 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f  his file does no
91f00 74 20 70 72 6f 76 69 64 65 20 61 6e 79 20 6d 75  t provide any mu
91f10 74 75 61 6c 0a 2a 2a 20 65 78 63 6c 75 73 69 6f  tual.** exclusio
91f20 6e 20 61 6e 64 20 69 73 20 74 68 75 73 20 73 75  n and is thus su
91f30 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20 6f  itable for use o
91f40 6e 6c 79 20 69 6e 20 61 70 70 6c 69 63 61 74 69  nly in applicati
91f50 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 75 73 65 20  ons.** that use 
91f60 53 51 4c 69 74 65 20 69 6e 20 61 20 73 69 6e 67  SQLite in a sing
91f70 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20  le thread.  The 
91f80 72 6f 75 74 69 6e 65 73 20 64 65 66 69 6e 65 64  routines defined
91f90 0a 2a 2a 20 68 65 72 65 20 61 72 65 20 70 6c 61  .** here are pla
91fa0 63 65 2d 68 6f 6c 64 65 72 73 2e 20 20 41 70 70  ce-holders.  App
91fb0 6c 69 63 61 74 69 6f 6e 73 20 63 61 6e 20 73 75  lications can su
91fc0 62 73 74 69 74 75 74 65 20 77 6f 72 6b 69 6e 67  bstitute working
91fd0 0a 2a 2a 20 6d 75 74 65 78 20 72 6f 75 74 69 6e  .** mutex routin
91fe0 65 73 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65  es at start-time
91ff0 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 0a 2a 2a   using the.**.**
92000 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e       sqlite3_con
92010 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49  fig(SQLITE_CONFI
92020 47 5f 4d 55 54 45 58 2c 2e 2e 2e 29 0a 2a 2a 0a  G_MUTEX,...).**.
92030 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a  ** interface..**
92040 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65 64 20  .** If compiled 
92050 77 69 74 68 20 53 51 4c 49 54 45 5f 44 45 42 55  with SQLITE_DEBU
92060 47 2c 20 74 68 65 6e 20 61 64 64 69 74 69 6f 6e  G, then addition
92070 61 6c 20 6c 6f 67 69 63 20 69 73 20 69 6e 73 65  al logic is inse
92080 72 74 65 64 0a 2a 2a 20 74 68 61 74 20 64 6f 65  rted.** that doe
92090 73 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67  s error checking
920a0 20 6f 6e 20 6d 75 74 65 78 65 73 20 74 6f 20 6d   on mutexes to m
920b0 61 6b 65 20 73 75 72 65 20 74 68 65 79 20 61 72  ake sure they ar
920c0 65 20 62 65 69 6e 67 0a 2a 2a 20 63 61 6c 6c 65  e being.** calle
920d0 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a  d correctly..**.
920e0 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f 6e 6f  ** $Id: mutex_no
920f0 6f 70 2e 63 2c 76 20 31 2e 32 20 32 30 30 38 2f  op.c,v 1.2 2008/
92100 31 30 2f 31 35 20 31 39 3a 30 33 3a 30 33 20 64  10/15 19:03:03 d
92110 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 23 69  rh Exp $.*/...#i
92120 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
92130 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20  _MUTEX_NOOP) && 
92140 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
92150 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 53 74 75  DEBUG)./*.** Stu
92160 62 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 61  b routines for a
92170 6c 6c 20 6d 75 74 65 78 20 6d 65 74 68 6f 64 73  ll mutex methods
92180 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
92190 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20 6e 6f  tines provide no
921a0 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f   mutual exclusio
921b0 6e 20 6f 72 20 65 72 72 6f 72 20 63 68 65 63 6b  n or error check
921c0 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ing..*/.static i
921d0 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 48 65 6c 64  nt noopMutexHeld
921e0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
921f0 70 29 7b 20 72 65 74 75 72 6e 20 31 3b 20 7d 0a  p){ return 1; }.
92200 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d  static int noopM
92210 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69  utexNotheld(sqli
92220 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72  te3_mutex *p){ r
92230 65 74 75 72 6e 20 31 3b 20 7d 0a 73 74 61 74 69  eturn 1; }.stati
92240 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 49  c int noopMutexI
92250 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72  nit(void){ retur
92260 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73  n SQLITE_OK; }.s
92270 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75  tatic int noopMu
92280 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65  texEnd(void){ re
92290 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  turn SQLITE_OK; 
922a0 7d 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33  }.static sqlite3
922b0 5f 6d 75 74 65 78 20 2a 6e 6f 6f 70 4d 75 74 65  _mutex *noopMute
922c0 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 20  xAlloc(int id){ 
922d0 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f  return (sqlite3_
922e0 6d 75 74 65 78 2a 29 38 3b 20 7d 0a 73 74 61 74  mutex*)8; }.stat
922f0 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65  ic void noopMute
92300 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75  xFree(sqlite3_mu
92310 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b  tex *p){ return;
92320 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e   }.static void n
92330 6f 6f 70 4d 75 74 65 78 45 6e 74 65 72 28 73 71  oopMutexEnter(sq
92340 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
92350 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61 74 69   return; }.stati
92360 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 54  c int noopMutexT
92370 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ry(sqlite3_mutex
92380 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 53 51 4c   *p){ return SQL
92390 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63  ITE_OK; }.static
923a0 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78 4c   void noopMutexL
923b0 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  eave(sqlite3_mut
923c0 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b 20  ex *p){ return; 
923d0 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  }..SQLITE_PRIVAT
923e0 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  E sqlite3_mutex_
923f0 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33  methods *sqlite3
92400 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69  DefaultMutex(voi
92410 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c  d){.  static sql
92420 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
92430 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20  ds sMutex = {.  
92440 20 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 74 2c    noopMutexInit,
92450 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 45 6e  .    noopMutexEn
92460 64 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78  d,.    noopMutex
92470 41 6c 6c 6f 63 2c 0a 20 20 20 20 6e 6f 6f 70 4d  Alloc,.    noopM
92480 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6e 6f  utexFree,.    no
92490 6f 70 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20  opMutexEnter,.  
924a0 20 20 6e 6f 6f 70 4d 75 74 65 78 54 72 79 2c 0a    noopMutexTry,.
924b0 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4c 65 61      noopMutexLea
924c0 76 65 2c 0a 0a 20 20 20 20 6e 6f 6f 70 4d 75 74  ve,..    noopMut
924d0 65 78 48 65 6c 64 2c 0a 20 20 20 20 6e 6f 6f 70  exHeld,.    noop
924e0 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20 20 7d  MutexNotheld.  }
924f0 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75  ;..  return &sMu
92500 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  tex;.}.#endif /*
92510 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
92520 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 21  MUTEX_NOOP) && !
92530 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
92540 45 42 55 47 29 20 2a 2f 0a 0a 23 69 66 20 64 65  EBUG) */..#if de
92550 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54  fined(SQLITE_MUT
92560 45 58 5f 4e 4f 4f 50 29 20 26 26 20 64 65 66 69  EX_NOOP) && defi
92570 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
92580 29 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20  )./*.** In this 
92590 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20  implementation, 
925a0 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 69  error checking i
925b0 73 20 70 72 6f 76 69 64 65 64 20 66 6f 72 20 74  s provided for t
925c0 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 65  esting.** and de
925d0 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73 65 73  bugging purposes
925e0 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73 20 73  .  The mutexes s
925f0 74 69 6c 6c 20 64 6f 20 6e 6f 74 20 70 72 6f 76  till do not prov
92600 69 64 65 20 61 6e 79 0a 2a 2a 20 6d 75 74 75 61  ide any.** mutua
92610 6c 20 65 78 63 6c 75 73 69 6f 6e 2e 0a 2a 2f 0a  l exclusion..*/.
92620 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78  ./*.** The mutex
92630 20 6f 62 6a 65 63 74 0a 2a 2f 0a 73 74 72 75 63   object.*/.struc
92640 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  t sqlite3_mutex 
92650 7b 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20 20  {.  int id;     
92660 2f 2a 20 54 68 65 20 6d 75 74 65 78 20 74 79 70  /* The mutex typ
92670 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20  e */.  int cnt; 
92680 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
92690 65 6e 74 72 69 65 73 20 77 69 74 68 6f 75 74 20  entries without 
926a0 61 20 6d 61 74 63 68 69 6e 67 20 6c 65 61 76 65  a matching leave
926b0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68   */.};../*.** Th
926c0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
926d0 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74  held() and sqlit
926e0 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64  e3_mutex_notheld
926f0 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a  () routine are.*
92700 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75  * intended for u
92710 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74  se inside assert
92720 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  () statements..*
92730 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 62  /.static int deb
92740 75 67 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69  ugMutexHeld(sqli
92750 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
92760 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20   return p==0 || 
92770 70 2d 3e 63 6e 74 3e 30 3b 0a 7d 0a 73 74 61 74  p->cnt>0;.}.stat
92780 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65  ic int debugMute
92790 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33  xNotheld(sqlite3
927a0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65  _mutex *p){.  re
927b0 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e  turn p==0 || p->
927c0 63 6e 74 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  cnt==0;.}../*.**
927d0 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20   Initialize and 
927e0 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  deinitialize the
927f0 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d   mutex subsystem
92800 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
92810 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74 28 76  debugMutexInit(v
92820 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c  oid){ return SQL
92830 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63  ITE_OK; }.static
92840 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 45   int debugMutexE
92850 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  nd(void){ return
92860 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f   SQLITE_OK; }../
92870 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
92880 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72  _mutex_alloc() r
92890 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73  outine allocates
928a0 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20   a new.** mutex 
928b0 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f  and returns a po
928c0 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66  inter to it.  If
928d0 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c   it returns NULL
928e0 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74  .** that means t
928f0 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c  hat a mutex coul
92900 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74  d not be allocat
92910 65 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 73  ed. .*/.static s
92920 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 64 65  qlite3_mutex *de
92930 62 75 67 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e  bugMutexAlloc(in
92940 74 20 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20  t id){.  static 
92950 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 61 53  sqlite3_mutex aS
92960 74 61 74 69 63 5b 36 5d 3b 0a 20 20 73 71 6c 69  tatic[6];.  sqli
92970 74 65 33 5f 6d 75 74 65 78 20 2a 70 4e 65 77 20  te3_mutex *pNew 
92980 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 20 69  = 0;.  switch( i
92990 64 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  d ){.    case SQ
929a0 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a  LITE_MUTEX_FAST:
929b0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
929c0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
929d0 3a 20 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d  : {.      pNew =
929e0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 73   sqlite3Malloc(s
929f0 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20  izeof(*pNew));. 
92a00 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b       if( pNew ){
92a10 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 69  .        pNew->i
92a20 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 20 20  d = id;.        
92a30 70 4e 65 77 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20  pNew->cnt = 0;. 
92a40 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
92a50 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65  ak;.    }.    de
92a60 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61  fault: {.      a
92a70 73 73 65 72 74 28 20 69 64 2d 32 20 3e 3d 20 30  ssert( id-2 >= 0
92a80 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
92a90 28 20 69 64 2d 32 20 3c 20 73 69 7a 65 6f 66 28  ( id-2 < sizeof(
92aa0 61 53 74 61 74 69 63 29 2f 73 69 7a 65 6f 66 28  aStatic)/sizeof(
92ab0 61 53 74 61 74 69 63 5b 30 5d 29 20 29 3b 0a 20  aStatic[0]) );. 
92ac0 20 20 20 20 20 70 4e 65 77 20 3d 20 26 61 53 74       pNew = &aSt
92ad0 61 74 69 63 5b 69 64 2d 32 5d 3b 0a 20 20 20 20  atic[id-2];.    
92ae0 20 20 70 4e 65 77 2d 3e 69 64 20 3d 20 69 64 3b    pNew->id = id;
92af0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
92b00 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
92b10 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pNew;.}../*.** 
92b20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 61  This routine dea
92b30 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65 76 69  llocates a previ
92b40 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  ously allocated 
92b50 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63  mutex..*/.static
92b60 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74 65 78   void debugMutex
92b70 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  Free(sqlite3_mut
92b80 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  ex *p){.  assert
92b90 28 20 70 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a 20  ( p->cnt==0 );. 
92ba0 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d   assert( p->id==
92bb0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
92bc0 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  T || p->id==SQLI
92bd0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
92be0 56 45 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  VE );.  sqlite3_
92bf0 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  free(p);.}../*.*
92c00 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
92c10 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20  tex_enter() and 
92c20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
92c30 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74  y() routines att
92c40 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72  empt.** to enter
92c50 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e   a mutex.  If an
92c60 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20  other thread is 
92c70 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 74  already within t
92c80 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c  he mutex,.** sql
92c90 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
92ca0 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e  () will block an
92cb0 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
92cc0 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72  try() will retur
92cd0 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59  n.** SQLITE_BUSY
92ce0 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  .  The sqlite3_m
92cf0 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72  utex_try() inter
92d00 66 61 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c  face returns SQL
92d10 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73  ITE_OK.** upon s
92d20 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e  uccessful entry.
92d30 20 20 4d 75 74 65 78 65 73 20 63 72 65 61 74 65    Mutexes create
92d40 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d  d using SQLITE_M
92d50 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63  UTEX_RECURSIVE c
92d60 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64  an.** be entered
92d70 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20   multiple times 
92d80 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  by the same thre
92d90 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73  ad.  In such cas
92da0 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78  es the,.** mutex
92db0 20 6d 75 73 74 20 62 65 20 65 78 69 74 65 64 20   must be exited 
92dc0 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20  an equal number 
92dd0 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20  of times before 
92de0 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a  another thread.*
92df0 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66  * can enter.  If
92e00 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
92e10 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20   tries to enter 
92e20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f  any other kind o
92e30 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20  f mutex.** more 
92e40 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62  than once, the b
92e50 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66  ehavior is undef
92e60 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ined..*/.static 
92e70 76 6f 69 64 20 64 65 62 75 67 4d 75 74 65 78 45  void debugMutexE
92e80 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74  nter(sqlite3_mut
92e90 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  ex *p){.  assert
92ea0 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f  ( p->id==SQLITE_
92eb0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20  MUTEX_RECURSIVE 
92ec0 7c 7c 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74  || debugMutexNot
92ed0 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e  held(p) );.  p->
92ee0 63 6e 74 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63 20  cnt++;.}.static 
92ef0 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 54 72  int debugMutexTr
92f00 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  y(sqlite3_mutex 
92f10 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
92f20 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54  ->id==SQLITE_MUT
92f30 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20  EX_RECURSIVE || 
92f40 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 6c  debugMutexNothel
92f50 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 63 6e 74  d(p) );.  p->cnt
92f60 2b 2b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  ++;.  return SQL
92f70 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
92f80 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
92f90 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69  ex_leave() routi
92fa0 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78  ne exits a mutex
92fb0 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65   that was.** pre
92fc0 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20  viously entered 
92fd0 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  by the same thre
92fe0 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f  ad.  The behavio
92ff0 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65  r.** is undefine
93000 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 69  d if the mutex i
93010 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  s not currently 
93020 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73  entered or.** is
93030 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61   not currently a
93040 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74  llocated.  SQLit
93050 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20  e will never do 
93060 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  either..*/.stati
93070 63 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74 65  c void debugMute
93080 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d  xLeave(sqlite3_m
93090 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65  utex *p){.  asse
930a0 72 74 28 20 64 65 62 75 67 4d 75 74 65 78 48 65  rt( debugMutexHe
930b0 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 63 6e  ld(p) );.  p->cn
930c0 74 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  t--;.  assert( p
930d0 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54  ->id==SQLITE_MUT
930e0 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20  EX_RECURSIVE || 
930f0 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 6c  debugMutexNothel
93100 64 28 70 29 20 29 3b 0a 7d 0a 0a 53 51 4c 49 54  d(p) );.}..SQLIT
93110 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65  E_PRIVATE sqlite
93120 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20  3_mutex_methods 
93130 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d  *sqlite3DefaultM
93140 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 74  utex(void){.  st
93150 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74  atic sqlite3_mut
93160 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65  ex_methods sMute
93170 78 20 3d 20 7b 0a 20 20 20 20 64 65 62 75 67 4d  x = {.    debugM
93180 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 64 65  utexInit,.    de
93190 62 75 67 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20  bugMutexEnd,.   
931a0 20 64 65 62 75 67 4d 75 74 65 78 41 6c 6c 6f 63   debugMutexAlloc
931b0 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78  ,.    debugMutex
931c0 46 72 65 65 2c 0a 20 20 20 20 64 65 62 75 67 4d  Free,.    debugM
931d0 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 64  utexEnter,.    d
931e0 65 62 75 67 4d 75 74 65 78 54 72 79 2c 0a 20 20  ebugMutexTry,.  
931f0 20 20 64 65 62 75 67 4d 75 74 65 78 4c 65 61 76    debugMutexLeav
93200 65 2c 0a 0a 20 20 20 20 64 65 62 75 67 4d 75 74  e,..    debugMut
93210 65 78 48 65 6c 64 2c 0a 20 20 20 20 64 65 62 75  exHeld,.    debu
93220 67 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20 20  gMutexNotheld.  
93230 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d  };..  return &sM
93240 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  utex;.}.#endif /
93250 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  * defined(SQLITE
93260 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20  _MUTEX_NOOP) && 
93270 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
93280 45 42 55 47 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  EBUG) */../*****
93290 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
932a0 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63 20 2a 2a   mutex_noop.c **
932b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
932c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
932d0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
932e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
932f0 66 69 6c 65 20 6d 75 74 65 78 5f 6f 73 32 2e 63  file mutex_os2.c
93300 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
93310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93320 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
93330 32 30 30 37 20 41 75 67 75 73 74 20 32 38 0a 2a  2007 August 28.*
93340 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
93350 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
93360 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
93370 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
93380 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
93390 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
933a0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
933b0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
933c0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
933d0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
933e0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
933f0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
93400 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
93410 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
93420 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
93430 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
93440 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
93450 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
93460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
934a0 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
934b0 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69  ins the C functi
934c0 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ons that impleme
934d0 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20 4f  nt mutexes for O
934e0 53 2f 32 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d  S/2.**.** $Id: m
934f0 75 74 65 78 5f 6f 73 32 2e 63 2c 76 20 31 2e 31  utex_os2.c,v 1.1
93500 30 20 32 30 30 38 2f 30 36 2f 32 33 20 32 32 3a  0 2008/06/23 22:
93510 31 33 3a 32 38 20 70 77 65 69 6c 62 61 63 68 65  13:28 pweilbache
93520 72 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  r Exp $.*/../*.*
93530 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68  * The code in th
93540 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20  is file is only 
93550 75 73 65 64 20 69 66 20 53 51 4c 49 54 45 5f 4d  used if SQLITE_M
93560 55 54 45 58 5f 4f 53 32 20 69 73 20 64 65 66 69  UTEX_OS2 is defi
93570 6e 65 64 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20  ned..** See the 
93580 6d 75 74 65 78 2e 68 20 66 69 6c 65 20 66 6f 72  mutex.h file for
93590 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 69 66   details..*/.#if
935a0 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  def SQLITE_MUTEX
935b0 5f 4f 53 32 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  _OS2../*********
935c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4f 53  ************* OS
935d0 2f 32 20 4d 75 74 65 78 20 49 6d 70 6c 65 6d 65  /2 Mutex Impleme
935e0 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a  ntation ********
935f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
93600 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d  *.** This implem
93610 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 75 74 65  entation of mute
93620 78 65 73 20 69 73 20 62 75 69 6c 74 20 75 73 69  xes is built usi
93630 6e 67 20 74 68 65 20 4f 53 2f 32 20 41 50 49 2e  ng the OS/2 API.
93640 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d  .*/../*.** The m
93650 75 74 65 78 20 6f 62 6a 65 63 74 0a 2a 2a 20 45  utex object.** E
93660 61 63 68 20 72 65 63 75 72 73 69 76 65 20 6d 75  ach recursive mu
93670 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61 6e  tex is an instan
93680 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
93690 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ing structure..*
936a0 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  /.struct sqlite3
936b0 5f 6d 75 74 65 78 20 7b 0a 20 20 48 4d 54 58 20  _mutex {.  HMTX 
936c0 6d 75 74 65 78 3b 20 20 20 20 20 20 20 2f 2a 20  mutex;       /* 
936d0 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e  Mutex controllin
936e0 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20  g the lock */.  
936f0 69 6e 74 20 20 69 64 3b 20 20 20 20 20 20 20 20  int  id;        
93700 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 65 20    /* Mutex type 
93710 2a 2f 0a 20 20 69 6e 74 20 20 6e 52 65 66 3b 20  */.  int  nRef; 
93720 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
93730 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 2a   of references *
93740 2f 0a 20 20 54 49 44 20 20 6f 77 6e 65 72 3b 20  /.  TID  owner; 
93750 20 20 20 20 20 20 2f 2a 20 54 68 72 65 61 64 20        /* Thread 
93760 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 6d 75 74  holding this mut
93770 65 78 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e  ex */.};..#defin
93780 65 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54  e OS2_MUTEX_INIT
93790 49 41 4c 49 5a 45 52 20 20 20 30 2c 30 2c 30 2c  IALIZER   0,0,0,
937a0 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c  0../*.** Initial
937b0 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61  ize and deinitia
937c0 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73  lize the mutex s
937d0 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61  ubsystem..*/.sta
937e0 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 78  tic int os2Mutex
937f0 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75  Init(void){ retu
93800 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a  rn SQLITE_OK; }.
93810 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75  static int os2Mu
93820 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65  texEnd(void){ re
93830 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  turn SQLITE_OK; 
93840 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  }../*.** The sql
93850 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
93860 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63  () routine alloc
93870 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75  ates a new.** mu
93880 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20  tex and returns 
93890 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e  a pointer to it.
938a0 20 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 20    If it returns 
938b0 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61  NULL.** that mea
938c0 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 78 20  ns that a mutex 
938d0 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c  could not be all
938e0 6f 63 61 74 65 64 2e 20 0a 2a 2a 20 53 51 4c 69  ocated. .** SQLi
938f0 74 65 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69  te will unwind i
93900 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72 65 74  ts stack and ret
93910 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20 54  urn an error.  T
93920 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74  he argument.** t
93930 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  o sqlite3_mutex_
93940 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f  alloc() is one o
93950 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20  f these integer 
93960 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a  constants:.**.**
93970 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53   <ul>.** <li>  S
93980 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
93990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
939a0 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45  .** <li>  SQLITE
939b0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
939c0 20 20 20 20 20 20 20 20 20 20 31 0a 2a 2a 20 3c            1.** <
939d0 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
939e0 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20  X_STATIC_MASTER 
939f0 20 20 20 20 20 32 0a 2a 2a 20 3c 6c 69 3e 20 20       2.** <li>  
93a00 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
93a10 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 20 20 20  TIC_MEM         
93a20 33 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  3.** <li>  SQLIT
93a30 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50  E_MUTEX_STATIC_P
93a40 52 4e 47 20 20 20 20 20 20 20 20 34 0a 2a 2a 20  RNG        4.** 
93a50 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  </ul>.**.** The 
93a60 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 61  first two consta
93a70 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 74 65  nts cause sqlite
93a80 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20  3_mutex_alloc() 
93a90 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e  to create.** a n
93aa0 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20 6e  ew mutex.  The n
93ab0 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63 75  ew mutex is recu
93ac0 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49 54  rsive when SQLIT
93ad0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
93ae0 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75 74  E.** is used but
93af0 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79   not necessarily
93b00 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 5f   so when SQLITE_
93b10 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75 73  MUTEX_FAST is us
93b20 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78  ed..** The mutex
93b30 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
93b40 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f  does not need to
93b50 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74   make a distinct
93b60 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53  ion.** between S
93b70 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
93b80 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45  RSIVE and SQLITE
93b90 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20 69  _MUTEX_FAST if i
93ba0 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61  t does.** not wa
93bb0 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 4c 69  nt to.  But SQLi
93bc0 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71  te will only req
93bd0 75 65 73 74 20 61 20 72 65 63 75 72 73 69 76 65  uest a recursive
93be0 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73   mutex in.** cas
93bf0 65 73 20 77 68 65 72 65 20 69 74 20 72 65 61 6c  es where it real
93c00 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 49  ly needs one.  I
93c10 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72  f a faster non-r
93c20 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a 2a  ecursive mutex.*
93c30 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
93c40 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e   is available on
93c50 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66 6f   the host platfo
93c60 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73 75  rm, the mutex su
93c70 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74  bsystem.** might
93c80 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d   return such a m
93c90 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65  utex in response
93ca0 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58   to SQLITE_MUTEX
93cb0 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  _FAST..**.** The
93cc0 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 70   other allowed p
93cd0 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71 6c  arameters to sql
93ce0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
93cf0 28 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a 2a  () each return.*
93d00 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  * a pointer to a
93d10 20 73 74 61 74 69 63 20 70 72 65 65 78 69 73 74   static preexist
93d20 69 6e 67 20 6d 75 74 65 78 2e 20 20 54 68 72 65  ing mutex.  Thre
93d30 65 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73  e static mutexes
93d40 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20   are.** used by 
93d50 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73  the current vers
93d60 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20  ion of SQLite.  
93d70 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20  Future versions 
93d80 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79  of SQLite.** may
93d90 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20   add additional 
93da0 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20  static mutexes. 
93db0 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20   Static mutexes 
93dc0 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c  are for internal
93dd0 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74  .** use by SQLit
93de0 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61  e only.  Applica
93df0 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53  tions that use S
93e00 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68  QLite mutexes sh
93e10 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79  ould.** use only
93e20 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74   the dynamic mut
93e30 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79  exes returned by
93e40 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
93e50 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f  ST or.** SQLITE_
93e60 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e  MUTEX_RECURSIVE.
93e70 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  .**.** Note that
93e80 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 64   if one of the d
93e90 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 72  ynamic mutex par
93ea0 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 5f  ameters (SQLITE_
93eb0 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72  MUTEX_FAST.** or
93ec0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
93ed0 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65 64  CURSIVE) is used
93ee0 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75   then sqlite3_mu
93ef0 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72  tex_alloc().** r
93f00 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72 65  eturns a differe
93f10 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72  nt mutex on ever
93f20 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f 72  y call.  But for
93f30 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a 20 6d   the static.** m
93f40 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65 20  utex types, the 
93f50 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72 65  same mutex is re
93f60 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 20  turned on every 
93f70 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a  call that has.**
93f80 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20 6e   the same type n
93f90 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  umber..*/.static
93fa0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
93fb0 6f 73 32 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e  os2MutexAlloc(in
93fc0 74 20 69 54 79 70 65 29 7b 0a 20 20 73 71 6c 69  t iType){.  sqli
93fd0 74 65 33 5f 6d 75 74 65 78 20 2a 70 20 3d 20 4e  te3_mutex *p = N
93fe0 55 4c 4c 3b 0a 20 20 73 77 69 74 63 68 28 20 69  ULL;.  switch( i
93ff0 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65  Type ){.    case
94000 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
94010 53 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c  ST:.    case SQL
94020 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
94030 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 20 3d  IVE: {.      p =
94040 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65   sqlite3MallocZe
94050 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29  ro( sizeof(*p) )
94060 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20 29 7b  ;.      if( p ){
94070 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20 3d  .        p->id =
94080 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20   iType;.        
94090 69 66 28 20 44 6f 73 43 72 65 61 74 65 4d 75 74  if( DosCreateMut
940a0 65 78 53 65 6d 28 20 30 2c 20 26 70 2d 3e 6d 75  exSem( 0, &p->mu
940b0 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 20 29 20  tex, 0, FALSE ) 
940c0 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20  != NO_ERROR ){. 
940d0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
940e0 5f 66 72 65 65 28 20 70 20 29 3b 0a 20 20 20 20  _free( p );.    
940f0 20 20 20 20 20 20 70 20 3d 20 4e 55 4c 4c 3b 0a        p = NULL;.
94100 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
94110 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
94120 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74     }.    default
94130 3a 20 7b 0a 20 20 20 20 20 20 73 74 61 74 69 63  : {.      static
94140 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 69 73   volatile int is
94150 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  Init = 0;.      
94160 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d  static sqlite3_m
94170 75 74 65 78 20 73 74 61 74 69 63 4d 75 74 65 78  utex staticMutex
94180 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20  es[] = {.       
94190 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49   { OS2_MUTEX_INI
941a0 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20  TIALIZER, },.   
941b0 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58       { OS2_MUTEX
941c0 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c  _INITIALIZER, },
941d0 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d  .        { OS2_M
941e0 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52  UTEX_INITIALIZER
941f0 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f  , },.        { O
94200 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c  S2_MUTEX_INITIAL
94210 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20  IZER, },.       
94220 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49   { OS2_MUTEX_INI
94230 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20  TIALIZER, },.   
94240 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58       { OS2_MUTEX
94250 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c  _INITIALIZER, },
94260 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20  .      };.      
94270 69 66 20 28 20 21 69 73 49 6e 69 74 20 29 7b 0a  if ( !isInit ){.
94280 20 20 20 20 20 20 20 20 41 50 49 52 45 54 20 72          APIRET r
94290 63 3b 0a 20 20 20 20 20 20 20 20 50 54 49 42 20  c;.        PTIB 
942a0 70 74 69 62 3b 0a 20 20 20 20 20 20 20 20 50 50  ptib;.        PP
942b0 49 42 20 70 70 69 62 3b 0a 20 20 20 20 20 20 20  IB ppib;.       
942c0 20 48 4d 54 58 20 6d 75 74 65 78 3b 0a 20 20 20   HMTX mutex;.   
942d0 20 20 20 20 20 63 68 61 72 20 6e 61 6d 65 5b 33       char name[3
942e0 32 5d 3b 0a 20 20 20 20 20 20 20 20 44 6f 73 47  2];.        DosG
942f0 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 20 26 70  etInfoBlocks( &p
94300 74 69 62 2c 20 26 70 70 69 62 20 29 3b 0a 20 20  tib, &ppib );.  
94310 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
94320 70 72 69 6e 74 66 28 20 73 69 7a 65 6f 66 28 6e  printf( sizeof(n
94330 61 6d 65 29 2c 20 6e 61 6d 65 2c 20 22 5c 5c 53  ame), name, "\\S
94340 45 4d 33 32 5c 5c 53 51 4c 49 54 45 25 30 34 78  EM32\\SQLITE%04x
94350 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
94360 20 20 20 20 20 20 20 20 20 20 20 20 20 70 70 69               ppi
94370 62 2d 3e 70 69 62 5f 75 6c 70 69 64 20 29 3b 0a  b->pib_ulpid );.
94380 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 21          while( !
94390 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20  isInit ){.      
943a0 20 20 20 20 6d 75 74 65 78 20 3d 20 30 3b 0a 20      mutex = 0;. 
943b0 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 44 6f           rc = Do
943c0 73 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d 28  sCreateMutexSem(
943d0 20 6e 61 6d 65 2c 20 26 6d 75 74 65 78 2c 20 30   name, &mutex, 0
943e0 2c 20 46 41 4c 53 45 29 3b 0a 20 20 20 20 20 20  , FALSE);.      
943f0 20 20 20 20 69 66 28 20 72 63 20 3d 3d 20 4e 4f      if( rc == NO
94400 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20  _ERROR ){.      
94410 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20        int i;.   
94420 20 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73           if( !is
94430 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Init ){.        
94440 20 20 20 20 20 20 66 6f 72 28 20 69 20 3d 20 30        for( i = 0
94450 3b 20 69 20 3c 20 73 69 7a 65 6f 66 28 73 74 61  ; i < sizeof(sta
94460 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69 7a 65  ticMutexes)/size
94470 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73  of(staticMutexes
94480 5b 30 5d 29 3b 20 69 2b 2b 20 29 7b 0a 20 20 20  [0]); i++ ){.   
94490 20 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73               Dos
944a0 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20  CreateMutexSem( 
944b0 30 2c 20 26 73 74 61 74 69 63 4d 75 74 65 78 65  0, &staticMutexe
944c0 73 5b 69 5d 2e 6d 75 74 65 78 2c 20 30 2c 20 46  s[i].mutex, 0, F
944d0 41 4c 53 45 20 29 3b 0a 20 20 20 20 20 20 20 20  ALSE );.        
944e0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
944f0 20 20 20 20 20 20 69 73 49 6e 69 74 20 3d 20 31        isInit = 1
94500 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
94510 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 43              DosC
94520 6c 6f 73 65 4d 75 74 65 78 53 65 6d 28 20 6d 75  loseMutexSem( mu
94530 74 65 78 20 29 3b 0a 20 20 20 20 20 20 20 20 20  tex );.         
94540 20 7d 65 6c 73 65 20 69 66 28 20 72 63 20 3d 3d   }else if( rc ==
94550 20 45 52 52 4f 52 5f 44 55 50 4c 49 43 41 54 45   ERROR_DUPLICATE
94560 5f 4e 41 4d 45 20 29 7b 0a 20 20 20 20 20 20 20  _NAME ){.       
94570 20 20 20 20 20 44 6f 73 53 6c 65 65 70 28 20 31       DosSleep( 1
94580 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65   );.          }e
94590 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
945a0 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20 20   return p;.     
945b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
945c0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61  .      }.      a
945d0 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3e  ssert( iType-2 >
945e0 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  = 0 );.      ass
945f0 65 72 74 28 20 69 54 79 70 65 2d 32 20 3c 20 73  ert( iType-2 < s
94600 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65  izeof(staticMute
94610 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 74  xes)/sizeof(stat
94620 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 20 29 3b  icMutexes[0]) );
94630 0a 20 20 20 20 20 20 70 20 3d 20 26 73 74 61 74  .      p = &stat
94640 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d  icMutexes[iType-
94650 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20  2];.      p->id 
94660 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62  = iType;.      b
94670 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
94680 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a    return p;.}...
94690 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
946a0 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61  ne deallocates a
946b0 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f   previously allo
946c0 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2a 20  cated mutex..** 
946d0 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75  SQLite is carefu
946e0 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20  l to deallocate 
946f0 65 76 65 72 79 20 6d 75 74 65 78 20 74 68 61 74  every mutex that
94700 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a   it allocates..*
94710 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73  /.static void os
94720 32 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 74  2MutexFree(sqlit
94730 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
94740 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
94750 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  n;.  assert( p->
94760 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73  nRef==0 );.  ass
94770 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  ert( p->id==SQLI
94780 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c  TE_MUTEX_FAST ||
94790 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d   p->id==SQLITE_M
947a0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29  UTEX_RECURSIVE )
947b0 3b 0a 20 20 44 6f 73 43 6c 6f 73 65 4d 75 74 65  ;.  DosCloseMute
947c0 78 53 65 6d 28 20 70 2d 3e 6d 75 74 65 78 20 29  xSem( p->mutex )
947d0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
947e0 28 20 70 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ( p );.}../*.** 
947f0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
94800 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71  x_enter() and sq
94810 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
94820 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d  ) routines attem
94830 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61  pt.** to enter a
94840 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74   mutex.  If anot
94850 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c  her thread is al
94860 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65  ready within the
94870 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74   mutex,.** sqlit
94880 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
94890 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20   will block and 
948a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
948b0 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a  y() will return.
948c0 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20  ** SQLITE_BUSY. 
948d0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
948e0 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61  ex_try() interfa
948f0 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  ce returns SQLIT
94900 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63  E_OK.** upon suc
94910 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20  cessful entry.  
94920 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20  Mutexes created 
94930 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54  using SQLITE_MUT
94940 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e  EX_RECURSIVE can
94950 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d  .** be entered m
94960 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79  ultiple times by
94970 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
94980 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73  .  In such cases
94990 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d   the,.** mutex m
949a0 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e  ust be exited an
949b0 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66   equal number of
949c0 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e   times before an
949d0 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20  other thread.** 
949e0 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74  can enter.  If t
949f0 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74  he same thread t
94a00 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e  ries to enter an
94a10 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20  y other kind of 
94a20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68  mutex.** more th
94a30 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68  an once, the beh
94a40 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e  avior is undefin
94a50 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
94a60 69 64 20 6f 73 32 4d 75 74 65 78 45 6e 74 65 72  id os2MutexEnter
94a70 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
94a80 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20  p){.  TID tid;. 
94a90 20 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a 20 20   PID holder1;.  
94aa0 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b 0a 20  ULONG holder2;. 
94ab0 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
94ac0 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  rn;.  assert( p-
94ad0 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45  >id==SQLITE_MUTE
94ae0 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 6f  X_RECURSIVE || o
94af0 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70  s2MutexNotheld(p
94b00 29 20 29 3b 0a 20 20 44 6f 73 52 65 71 75 65 73  ) );.  DosReques
94b10 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74  tMutexSem(p->mut
94b20 65 78 2c 20 53 45 4d 5f 49 4e 44 45 46 49 4e 49  ex, SEM_INDEFINI
94b30 54 45 5f 57 41 49 54 29 3b 0a 20 20 44 6f 73 51  TE_WAIT);.  DosQ
94b40 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e  ueryMutexSem(p->
94b50 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 31 2c  mutex, &holder1,
94b60 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 32 29   &tid, &holder2)
94b70 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 74  ;.  p->owner = t
94b80 69 64 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b  id;.  p->nRef++;
94b90 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  .}.static int os
94ba0 32 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65  2MutexTry(sqlite
94bb0 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69  3_mutex *p){.  i
94bc0 6e 74 20 72 63 3b 0a 20 20 54 49 44 20 74 69 64  nt rc;.  TID tid
94bd0 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 31 3b  ;.  PID holder1;
94be0 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32  .  ULONG holder2
94bf0 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
94c00 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
94c10 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64  .  assert( p->id
94c20 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  ==SQLITE_MUTEX_R
94c30 45 43 55 52 53 49 56 45 20 7c 7c 20 6f 73 32 4d  ECURSIVE || os2M
94c40 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29  utexNotheld(p) )
94c50 3b 0a 20 20 69 66 28 20 44 6f 73 52 65 71 75 65  ;.  if( DosReque
94c60 73 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75  stMutexSem(p->mu
94c70 74 65 78 2c 20 53 45 4d 5f 49 4d 4d 45 44 49 41  tex, SEM_IMMEDIA
94c80 54 45 5f 52 45 54 55 52 4e 29 20 3d 3d 20 4e 4f  TE_RETURN) == NO
94c90 5f 45 52 52 4f 52 29 20 7b 0a 20 20 20 20 44 6f  _ERROR) {.    Do
94ca0 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70  sQueryMutexSem(p
94cb0 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72  ->mutex, &holder
94cc0 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72  1, &tid, &holder
94cd0 32 29 3b 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72  2);.    p->owner
94ce0 20 3d 20 74 69 64 3b 0a 20 20 20 20 70 2d 3e 6e   = tid;.    p->n
94cf0 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20  Ref++;.    rc = 
94d00 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 20 65  SQLITE_OK;.  } e
94d10 6c 73 65 20 7b 0a 20 20 20 20 72 63 20 3d 20 53  lse {.    rc = S
94d20 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a  QLITE_BUSY;.  }.
94d30 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
94d40 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
94d50 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29  e3_mutex_leave()
94d60 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61   routine exits a
94d70 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a   mutex that was.
94d80 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e  ** previously en
94d90 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d  tered by the sam
94da0 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62  e thread.  The b
94db0 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e  ehavior.** is un
94dc0 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d  defined if the m
94dd0 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72  utex is not curr
94de0 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72  ently entered or
94df0 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65  .** is not curre
94e00 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20  ntly allocated. 
94e10 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76   SQLite will nev
94e20 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f  er do either..*/
94e30 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32  .static void os2
94e40 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69 74  MutexLeave(sqlit
94e50 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
94e60 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 68  TID tid;.  PID h
94e70 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e 47 20  older1;.  ULONG 
94e80 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 20 70  holder2;.  if( p
94e90 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
94ea0 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e  assert( p->nRef>
94eb0 30 20 29 3b 0a 20 20 44 6f 73 51 75 65 72 79 4d  0 );.  DosQueryM
94ec0 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78  utexSem(p->mutex
94ed0 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69 64  , &holder1, &tid
94ee0 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20 61  , &holder2);.  a
94ef0 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 3d  ssert( p->owner=
94f00 3d 74 69 64 20 29 3b 0a 20 20 70 2d 3e 6e 52 65  =tid );.  p->nRe
94f10 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  f--;.  assert( p
94f20 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e  ->nRef==0 || p->
94f30 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
94f40 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20  _RECURSIVE );.  
94f50 44 6f 73 52 65 6c 65 61 73 65 4d 75 74 65 78 53  DosReleaseMutexS
94f60 65 6d 28 70 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a  em(p->mutex);.}.
94f70 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
94f80 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  EBUG./*.** The s
94f90 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
94fa0 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  d() and sqlite3_
94fb0 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20  mutex_notheld() 
94fc0 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69  routine are.** i
94fd0 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20  ntended for use 
94fe0 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20  inside assert() 
94ff0 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73  statements..*/.s
95000 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74  tatic int os2Mut
95010 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d  exHeld(sqlite3_m
95020 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20  utex *p){.  TID 
95030 74 69 64 3b 0a 20 20 50 49 44 20 70 69 64 3b 0a  tid;.  PID pid;.
95040 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b    ULONG ulCount;
95050 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20  .  PTIB ptib;.  
95060 69 66 28 20 70 21 3d 30 20 29 20 7b 0a 20 20 20  if( p!=0 ) {.   
95070 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65   DosQueryMutexSe
95080 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 70 69 64  m(p->mutex, &pid
95090 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f 75 6e 74  , &tid, &ulCount
950a0 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20  );.  } else {.  
950b0 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63    DosGetInfoBloc
950c0 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b  ks(&ptib, NULL);
950d0 0a 20 20 20 20 74 69 64 20 3d 20 70 74 69 62 2d  .    tid = ptib-
950e0 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32  >tib_ptib2->tib2
950f0 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 20 72 65  _ultid;.  }.  re
95100 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 28 70 2d  turn p==0 || (p-
95110 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 2d 3e 6f  >nRef!=0 && p->o
95120 77 6e 65 72 3d 3d 74 69 64 29 3b 0a 7d 0a 73 74  wner==tid);.}.st
95130 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65  atic int os2Mute
95140 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33  xNotheld(sqlite3
95150 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49  _mutex *p){.  TI
95160 44 20 74 69 64 3b 0a 20 20 50 49 44 20 70 69 64  D tid;.  PID pid
95170 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e  ;.  ULONG ulCoun
95180 74 3b 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a  t;.  PTIB ptib;.
95190 20 20 69 66 28 20 70 21 3d 20 30 20 29 20 7b 0a    if( p!= 0 ) {.
951a0 20 20 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65      DosQueryMute
951b0 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26  xSem(p->mutex, &
951c0 70 69 64 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f  pid, &tid, &ulCo
951d0 75 6e 74 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b  unt);.  } else {
951e0 0a 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42  .    DosGetInfoB
951f0 6c 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c  locks(&ptib, NUL
95200 4c 29 3b 0a 20 20 20 20 74 69 64 20 3d 20 70 74  L);.    tid = pt
95210 69 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74  ib->tib_ptib2->t
95220 69 62 32 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20  ib2_ultid;.  }. 
95230 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20   return p==0 || 
95240 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d  p->nRef==0 || p-
95250 3e 6f 77 6e 65 72 21 3d 74 69 64 3b 0a 7d 0a 23  >owner!=tid;.}.#
95260 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52  endif..SQLITE_PR
95270 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75  IVATE sqlite3_mu
95280 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c  tex_methods *sql
95290 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78  ite3DefaultMutex
952a0 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63  (void){.  static
952b0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d   sqlite3_mutex_m
952c0 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20  ethods sMutex = 
952d0 7b 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 49 6e  {.    os2MutexIn
952e0 69 74 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78  it,.    os2Mutex
952f0 45 6e 64 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65  End,.    os2Mute
95300 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 6f 73 32 4d  xAlloc,.    os2M
95310 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6f 73  utexFree,.    os
95320 32 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20  2MutexEnter,.   
95330 20 6f 73 32 4d 75 74 65 78 54 72 79 2c 0a 20 20   os2MutexTry,.  
95340 20 20 6f 73 32 4d 75 74 65 78 4c 65 61 76 65 2c    os2MutexLeave,
95350 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
95360 45 42 55 47 0a 20 20 20 20 6f 73 32 4d 75 74 65  EBUG.    os2Mute
95370 78 48 65 6c 64 2c 0a 20 20 20 20 6f 73 32 4d 75  xHeld,.    os2Mu
95380 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6e 64 69  texNotheld.#endi
95390 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e  f.  };..  return
953a0 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64   &sMutex;.}.#end
953b0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54  if /* SQLITE_MUT
953c0 45 58 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  EX_OS2 */../****
953d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
953e0 66 20 6d 75 74 65 78 5f 6f 73 32 2e 63 20 2a 2a  f mutex_os2.c **
953f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
95420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
95430 20 66 69 6c 65 20 6d 75 74 65 78 5f 75 6e 69 78   file mutex_unix
95440 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
95450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
95470 20 32 30 30 37 20 41 75 67 75 73 74 20 32 38 0a   2007 August 28.
95480 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
95490 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
954a0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
954b0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
954c0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
954d0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
954e0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
954f0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
95500 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
95510 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
95520 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
95530 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
95540 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
95550 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
95560 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
95570 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
95580 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
95590 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
955a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
955b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
955c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
955d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
955e0 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
955f0 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74  ains the C funct
95600 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d  ions that implem
95610 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20  ent mutexes for 
95620 70 74 68 72 65 61 64 73 0a 2a 2a 0a 2a 2a 20 24  pthreads.**.** $
95630 49 64 3a 20 6d 75 74 65 78 5f 75 6e 69 78 2e 63  Id: mutex_unix.c
95640 2c 76 20 31 2e 31 33 20 32 30 30 38 2f 30 37 2f  ,v 1.13 2008/07/
95650 31 36 20 31 32 3a 33 33 3a 32 34 20 64 72 68 20  16 12:33:24 drh 
95660 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
95670 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73  The code in this
95680 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75 73   file is only us
95690 65 64 20 69 66 20 77 65 20 61 72 65 20 63 6f 6d  ed if we are com
956a0 70 69 6c 69 6e 67 20 74 68 72 65 61 64 73 61 66  piling threadsaf
956b0 65 0a 2a 2a 20 75 6e 64 65 72 20 75 6e 69 78 20  e.** under unix 
956c0 77 69 74 68 20 70 74 68 72 65 61 64 73 2e 0a 2a  with pthreads..*
956d0 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74  *.** Note that t
956e0 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  his implementati
956f0 6f 6e 20 72 65 71 75 69 72 65 73 20 61 20 76 65  on requires a ve
95700 72 73 69 6f 6e 20 6f 66 20 70 74 68 72 65 61 64  rsion of pthread
95710 73 20 74 68 61 74 0a 2a 2a 20 73 75 70 70 6f 72  s that.** suppor
95720 74 73 20 72 65 63 75 72 73 69 76 65 20 6d 75 74  ts recursive mut
95730 65 78 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  exes..*/.#ifdef 
95740 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48  SQLITE_MUTEX_PTH
95750 52 45 41 44 53 0a 0a 23 69 6e 63 6c 75 64 65 20  READS..#include 
95760 3c 70 74 68 72 65 61 64 2e 68 3e 0a 0a 0a 2f 2a  <pthread.h>.../*
95770 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72 73 69  .** Each recursi
95780 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e 20 69  ve mutex is an i
95790 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
957a0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
957b0 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71  re..*/.struct sq
957c0 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20  lite3_mutex {.  
957d0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 20  pthread_mutex_t 
957e0 6d 75 74 65 78 3b 20 20 20 20 20 2f 2a 20 4d 75  mutex;     /* Mu
957f0 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20  tex controlling 
95800 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e  the lock */.  in
95810 74 20 69 64 3b 20 20 20 20 20 20 20 20 20 20 20  t id;           
95820 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65           /* Mute
95830 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20  x type */.  int 
95840 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20  nRef;           
95850 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
95860 20 6f 66 20 65 6e 74 72 61 6e 63 65 73 20 2a 2f   of entrances */
95870 0a 20 20 70 74 68 72 65 61 64 5f 74 20 6f 77 6e  .  pthread_t own
95880 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  er;           /*
95890 20 54 68 72 65 61 64 20 74 68 61 74 20 69 73 20   Thread that is 
958a0 77 69 74 68 69 6e 20 74 68 69 73 20 6d 75 74 65  within this mute
958b0 78 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  x */.#ifdef SQLI
958c0 54 45 5f 44 45 42 55 47 0a 20 20 69 6e 74 20 74  TE_DEBUG.  int t
958d0 72 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20  race;           
958e0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f        /* True to
958f0 20 74 72 61 63 65 20 63 68 61 6e 67 65 73 20 2a   trace changes *
95900 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 23 69 66 64  /.#endif.};.#ifd
95910 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
95920 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f  #define SQLITE3_
95930 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45  MUTEX_INITIALIZE
95940 52 20 7b 20 50 54 48 52 45 41 44 5f 4d 55 54 45  R { PTHREAD_MUTE
95950 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30  X_INITIALIZER, 0
95960 2c 20 30 2c 20 28 70 74 68 72 65 61 64 5f 74 29  , 0, (pthread_t)
95970 30 2c 20 30 20 7d 0a 23 65 6c 73 65 0a 23 64 65  0, 0 }.#else.#de
95980 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d 55 54  fine SQLITE3_MUT
95990 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b  EX_INITIALIZER {
959a0 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 49   PTHREAD_MUTEX_I
959b0 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30  NITIALIZER, 0, 0
959c0 2c 20 28 70 74 68 72 65 61 64 5f 74 29 30 20 7d  , (pthread_t)0 }
959d0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
959e0 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
959f0 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69  _held() and sqli
95a00 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c  te3_mutex_nothel
95a10 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a  d() routine are.
95a20 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20  ** intended for 
95a30 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64 65 20  use only inside 
95a40 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
95a50 6e 74 73 2e 20 20 4f 6e 20 73 6f 6d 65 20 70 6c  nts.  On some pl
95a60 61 74 66 6f 72 6d 73 2c 0a 2a 2a 20 74 68 65 72  atforms,.** ther
95a70 65 20 6d 69 67 68 74 20 62 65 20 72 61 63 65 20  e might be race 
95a80 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74 20  conditions that 
95a90 63 61 6e 20 63 61 75 73 65 20 74 68 65 73 65 20  can cause these 
95aa0 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a 2a 20 64  routines to.** d
95ab0 65 6c 69 76 65 72 20 69 6e 63 6f 72 72 65 63 74  eliver incorrect
95ac0 20 72 65 73 75 6c 74 73 2e 20 20 49 6e 20 70 61   results.  In pa
95ad0 72 74 69 63 75 6c 61 72 2c 20 69 66 20 70 74 68  rticular, if pth
95ae0 72 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73 0a  read_equal() is.
95af0 2a 2a 20 6e 6f 74 20 61 6e 20 61 74 6f 6d 69 63  ** not an atomic
95b00 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e   operation, then
95b10 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   these routines 
95b20 6d 69 67 68 74 20 64 65 6c 69 76 65 72 79 0a 2a  might delivery.*
95b30 2a 20 69 6e 63 6f 72 72 65 63 74 20 72 65 73 75  * incorrect resu
95b40 6c 74 73 2e 20 20 4f 6e 20 6d 6f 73 74 20 70 6c  lts.  On most pl
95b50 61 74 66 6f 72 6d 73 2c 20 70 74 68 72 65 61 64  atforms, pthread
95b60 5f 65 71 75 61 6c 28 29 20 69 73 20 61 20 0a 2a  _equal() is a .*
95b70 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20  * comparison of 
95b80 74 77 6f 20 69 6e 74 65 67 65 72 73 20 61 6e 64  two integers and
95b90 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 61 74   is therefore at
95ba0 6f 6d 69 63 2e 20 20 42 75 74 20 77 65 20 61 72  omic.  But we ar
95bb0 65 0a 2a 2a 20 74 6f 6c 64 20 74 68 61 74 20 48  e.** told that H
95bc0 50 55 58 20 69 73 20 6e 6f 74 20 73 75 63 68 20  PUX is not such 
95bd0 61 20 70 6c 61 74 66 6f 72 6d 2e 20 20 49 66 20  a platform.  If 
95be0 73 6f 2c 20 74 68 65 6e 20 74 68 65 73 65 20 72  so, then these r
95bf0 6f 75 74 69 6e 65 73 0a 2a 2a 20 77 69 6c 6c 20  outines.** will 
95c00 6e 6f 74 20 61 6c 77 61 79 73 20 77 6f 72 6b 20  not always work 
95c10 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 48 50 55  correctly on HPU
95c20 58 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74 68 6f 73  X..**.** On thos
95c30 65 20 70 6c 61 74 66 6f 72 6d 73 20 77 68 65 72  e platforms wher
95c40 65 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28  e pthread_equal(
95c50 29 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2c  ) is not atomic,
95c60 20 53 51 4c 69 74 65 0a 2a 2a 20 73 68 6f 75 6c   SQLite.** shoul
95c70 64 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 77 69  d be compiled wi
95c80 74 68 6f 75 74 20 2d 44 53 51 4c 49 54 45 5f 44  thout -DSQLITE_D
95c90 45 42 55 47 20 61 6e 64 20 77 69 74 68 20 2d 44  EBUG and with -D
95ca0 4e 44 45 42 55 47 20 74 6f 0a 2a 2a 20 6d 61 6b  NDEBUG to.** mak
95cb0 65 20 73 75 72 65 20 6e 6f 20 61 73 73 65 72 74  e sure no assert
95cc0 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72  () statements ar
95cd0 65 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20  e evaluated and 
95ce0 68 65 6e 63 65 20 74 68 65 73 65 0a 2a 2a 20 72  hence these.** r
95cf0 6f 75 74 69 6e 65 73 20 61 72 65 20 6e 65 76 65  outines are neve
95d00 72 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 66  r called..*/.#if
95d10 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74  ndef NDEBUG.stat
95d20 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75  ic int pthreadMu
95d30 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f  texHeld(sqlite3_
95d40 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74  mutex *p){.  ret
95d50 75 72 6e 20 28 70 2d 3e 6e 52 65 66 21 3d 30 20  urn (p->nRef!=0 
95d60 26 26 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c  && pthread_equal
95d70 28 70 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72 65  (p->owner, pthre
95d80 61 64 5f 73 65 6c 66 28 29 29 29 3b 0a 7d 0a 73  ad_self()));.}.s
95d90 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61  tatic int pthrea
95da0 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71  dMutexNotheld(sq
95db0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
95dc0 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65  .  return p->nRe
95dd0 66 3d 3d 30 20 7c 7c 20 70 74 68 72 65 61 64 5f  f==0 || pthread_
95de0 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20  equal(p->owner, 
95df0 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 29 3d  pthread_self())=
95e00 3d 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  =0;.}.#endif../*
95e10 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61  .** Initialize a
95e20 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20  nd deinitialize 
95e30 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73  the mutex subsys
95e40 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  tem..*/.static i
95e50 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 49  nt pthreadMutexI
95e60 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72  nit(void){ retur
95e70 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73  n SQLITE_OK; }.s
95e80 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61  tatic int pthrea
95e90 64 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b  dMutexEnd(void){
95ea0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
95eb0 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  K; }../*.** The 
95ec0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
95ed0 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c  loc() routine al
95ee0 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a  locates a new.**
95ef0 20 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72   mutex and retur
95f00 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
95f10 69 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 72  it.  If it retur
95f20 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20  ns NULL.** that 
95f30 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74  means that a mut
95f40 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ex could not be 
95f50 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69  allocated.  SQLi
95f60 74 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e  te.** will unwin
95f70 64 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20  d its stack and 
95f80 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e  return an error.
95f90 20 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a    The argument.*
95fa0 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74  * to sqlite3_mut
95fb0 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e  ex_alloc() is on
95fc0 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67  e of these integ
95fd0 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a  er constants:.**
95fe0 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e  .** <ul>.** <li>
95ff0 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46    SQLITE_MUTEX_F
96000 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  AST.** <li>  SQL
96010 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
96020 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  IVE.** <li>  SQL
96030 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
96040 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20  _MASTER.** <li> 
96050 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
96060 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e  ATIC_MEM.** <li>
96070 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
96080 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c  TATIC_MEM2.** <l
96090 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
960a0 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20  _STATIC_PRNG.** 
960b0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
960c0 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a  EX_STATIC_LRU.**
960d0 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65   </ul>.**.** The
960e0 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74   first two const
960f0 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 74  ants cause sqlit
96100 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
96110 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20   to create.** a 
96120 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20  new mutex.  The 
96130 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63  new mutex is rec
96140 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49  ursive when SQLI
96150 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
96160 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75  VE.** is used bu
96170 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c  t not necessaril
96180 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45  y so when SQLITE
96190 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75  _MUTEX_FAST is u
961a0 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65  sed..** The mute
961b0 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  x implementation
961c0 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
961d0 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63  o make a distinc
961e0 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20  tion.** between 
961f0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
96200 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54  URSIVE and SQLIT
96210 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20  E_MUTEX_FAST if 
96220 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77  it does.** not w
96230 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 4c  ant to.  But SQL
96240 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65  ite will only re
96250 71 75 65 73 74 20 61 20 72 65 63 75 72 73 69 76  quest a recursiv
96260 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61  e mutex in.** ca
96270 73 65 73 20 77 68 65 72 65 20 69 74 20 72 65 61  ses where it rea
96280 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20  lly needs one.  
96290 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d  If a faster non-
962a0 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a  recursive mutex.
962b0 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
962c0 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f  n is available o
962d0 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66  n the host platf
962e0 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73  orm, the mutex s
962f0 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68  ubsystem.** migh
96300 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20  t return such a 
96310 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73  mutex in respons
96320 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45  e to SQLITE_MUTE
96330 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68  X_FAST..**.** Th
96340 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20  e other allowed 
96350 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71  parameters to sq
96360 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
96370 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a  c() each return.
96380 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
96390 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69 73  a static preexis
963a0 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 54 68 72  ting mutex.  Thr
963b0 65 65 20 73 74 61 74 69 63 20 6d 75 74 65 78 65  ee static mutexe
963c0 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79  s are.** used by
963d0 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72   the current ver
963e0 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20  sion of SQLite. 
963f0 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73   Future versions
96400 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61   of SQLite.** ma
96410 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c  y add additional
96420 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e   static mutexes.
96430 20 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73    Static mutexes
96440 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61   are for interna
96450 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69  l.** use by SQLi
96460 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63  te only.  Applic
96470 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20  ations that use 
96480 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73  SQLite mutexes s
96490 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c  hould.** use onl
964a0 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75  y the dynamic mu
964b0 74 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62  texes returned b
964c0 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  y SQLITE_MUTEX_F
964d0 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45  AST or.** SQLITE
964e0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
964f0 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
96500 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20  t if one of the 
96510 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61  dynamic mutex pa
96520 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45  rameters (SQLITE
96530 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f  _MUTEX_FAST.** o
96540 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  r SQLITE_MUTEX_R
96550 45 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65  ECURSIVE) is use
96560 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d  d then sqlite3_m
96570 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20  utex_alloc().** 
96580 72 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72  returns a differ
96590 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65  ent mutex on eve
965a0 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f  ry call.  But fo
965b0 72 20 74 68 65 20 73 74 61 74 69 63 20 0a 2a 2a  r the static .**
965c0 20 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68   mutex types, th
965d0 65 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20  e same mutex is 
965e0 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72  returned on ever
965f0 79 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a  y call that has.
96600 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65  ** the same type
96610 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74   number..*/.stat
96620 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ic sqlite3_mutex
96630 20 2a 70 74 68 72 65 61 64 4d 75 74 65 78 41 6c   *pthreadMutexAl
96640 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b 0a  loc(int iType){.
96650 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33    static sqlite3
96660 5f 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 74  _mutex staticMut
96670 65 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 53  exes[] = {.    S
96680 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49  QLITE3_MUTEX_INI
96690 54 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51  TIALIZER,.    SQ
966a0 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54  LITE3_MUTEX_INIT
966b0 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c  IALIZER,.    SQL
966c0 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49  ITE3_MUTEX_INITI
966d0 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49  ALIZER,.    SQLI
966e0 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41  TE3_MUTEX_INITIA
966f0 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54  LIZER,.    SQLIT
96700 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c  E3_MUTEX_INITIAL
96710 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45  IZER,.    SQLITE
96720 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  3_MUTEX_INITIALI
96730 5a 45 52 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74  ZER.  };.  sqlit
96740 65 33 5f 6d 75 74 65 78 20 2a 70 3b 0a 20 20 73  e3_mutex *p;.  s
96750 77 69 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a  witch( iType ){.
96760 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
96770 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a  MUTEX_RECURSIVE:
96780 20 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c   {.      p = sql
96790 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20  ite3MallocZero( 
967a0 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20  sizeof(*p) );.  
967b0 20 20 20 20 69 66 28 20 70 20 29 7b 0a 23 69 66      if( p ){.#if
967c0 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47  def SQLITE_HOMEG
967d0 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d  ROWN_RECURSIVE_M
967e0 55 54 45 58 0a 20 20 20 20 20 20 20 20 2f 2a 20  UTEX.        /* 
967f0 49 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 74  If recursive mut
96800 65 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61  exes are not ava
96810 69 6c 61 62 6c 65 2c 20 77 65 20 77 69 6c 6c 20  ilable, we will 
96820 68 61 76 65 20 74 6f 0a 20 20 20 20 20 20 20 20  have to.        
96830 2a 2a 20 62 75 69 6c 64 20 6f 75 72 20 6f 77 6e  ** build our own
96840 2e 20 20 53 65 65 20 62 65 6c 6f 77 2e 20 2a 2f  .  See below. */
96850 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64  .        pthread
96860 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e  _mutex_init(&p->
96870 6d 75 74 65 78 2c 20 30 29 3b 0a 23 65 6c 73 65  mutex, 0);.#else
96880 0a 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20  .        /* Use 
96890 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  a recursive mute
968a0 78 20 69 66 20 69 74 20 69 73 20 61 76 61 69 6c  x if it is avail
968b0 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20  able */.        
968c0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74  pthread_mutexatt
968d0 72 5f 74 20 72 65 63 75 72 73 69 76 65 41 74 74  r_t recursiveAtt
968e0 72 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65  r;.        pthre
968f0 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 69 6e 69  ad_mutexattr_ini
96900 74 28 26 72 65 63 75 72 73 69 76 65 41 74 74 72  t(&recursiveAttr
96910 29 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65  );.        pthre
96920 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 73 65 74  ad_mutexattr_set
96930 74 79 70 65 28 26 72 65 63 75 72 73 69 76 65 41  type(&recursiveA
96940 74 74 72 2c 20 50 54 48 52 45 41 44 5f 4d 55 54  ttr, PTHREAD_MUT
96950 45 58 5f 52 45 43 55 52 53 49 56 45 29 3b 0a 20  EX_RECURSIVE);. 
96960 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d         pthread_m
96970 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75  utex_init(&p->mu
96980 74 65 78 2c 20 26 72 65 63 75 72 73 69 76 65 41  tex, &recursiveA
96990 74 74 72 29 3b 0a 20 20 20 20 20 20 20 20 70 74  ttr);.        pt
969a0 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f  hread_mutexattr_
969b0 64 65 73 74 72 6f 79 28 26 72 65 63 75 72 73 69  destroy(&recursi
969c0 76 65 41 74 74 72 29 3b 0a 23 65 6e 64 69 66 0a  veAttr);.#endif.
969d0 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20          p->id = 
969e0 69 54 79 70 65 3b 0a 20 20 20 20 20 20 7d 0a 20  iType;.      }. 
969f0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
96a00 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  }.    case SQLIT
96a10 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a 20 7b 0a  E_MUTEX_FAST: {.
96a20 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65        p = sqlite
96a30 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a  3MallocZero( siz
96a40 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20  eof(*p) );.     
96a50 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20   if( p ){.      
96a60 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b    p->id = iType;
96a70 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64  .        pthread
96a80 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e  _mutex_init(&p->
96a90 6d 75 74 65 78 2c 20 30 29 3b 0a 20 20 20 20 20  mutex, 0);.     
96aa0 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
96ab0 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
96ac0 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72  t: {.      asser
96ad0 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20  t( iType-2 >= 0 
96ae0 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
96af0 20 69 54 79 70 65 2d 32 20 3c 20 73 69 7a 65 6f   iType-2 < sizeo
96b00 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 29  f(staticMutexes)
96b10 2f 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75  /sizeof(staticMu
96b20 74 65 78 65 73 5b 30 5d 29 20 29 3b 0a 20 20 20  texes[0]) );.   
96b30 20 20 20 70 20 3d 20 26 73 74 61 74 69 63 4d 75     p = &staticMu
96b40 74 65 78 65 73 5b 69 54 79 70 65 2d 32 5d 3b 0a  texes[iType-2];.
96b50 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54        p->id = iT
96b60 79 70 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ype;.      break
96b70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
96b80 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  turn p;.}.../*.*
96b90 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64  * This routine d
96ba0 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65  eallocates a pre
96bb0 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c 6c 6f 63  viously.** alloc
96bc0 61 74 65 64 20 6d 75 74 65 78 2e 20 20 53 51 4c  ated mutex.  SQL
96bd0 69 74 65 20 69 73 20 63 61 72 65 66 75 6c 20 74  ite is careful t
96be0 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 65 76 65  o deallocate eve
96bf0 72 79 0a 2a 2a 20 6d 75 74 65 78 20 74 68 61 74  ry.** mutex that
96c00 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a   it allocates..*
96c10 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 74  /.static void pt
96c20 68 72 65 61 64 4d 75 74 65 78 46 72 65 65 28 73  hreadMutexFree(s
96c30 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
96c40 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e  {.  assert( p->n
96c50 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  Ref==0 );.  asse
96c60 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54  rt( p->id==SQLIT
96c70 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c 20  E_MUTEX_FAST || 
96c80 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
96c90 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b  TEX_RECURSIVE );
96ca0 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78  .  pthread_mutex
96cb0 5f 64 65 73 74 72 6f 79 28 26 70 2d 3e 6d 75 74  _destroy(&p->mut
96cc0 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ex);.  sqlite3_f
96cd0 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ree(p);.}../*.**
96ce0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
96cf0 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73  ex_enter() and s
96d00 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
96d10 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65  () routines atte
96d20 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20  mpt.** to enter 
96d30 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f  a mutex.  If ano
96d40 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61  ther thread is a
96d50 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68  lready within th
96d60 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69  e mutex,.** sqli
96d70 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
96d80 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64  ) will block and
96d90 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74   sqlite3_mutex_t
96da0 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e  ry() will return
96db0 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e  .** SQLITE_BUSY.
96dc0 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75    The sqlite3_mu
96dd0 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66  tex_try() interf
96de0 61 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49  ace returns SQLI
96df0 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75  TE_OK.** upon su
96e00 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20  ccessful entry. 
96e10 20 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64   Mutexes created
96e20 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55   using SQLITE_MU
96e30 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61  TEX_RECURSIVE ca
96e40 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20  n.** be entered 
96e50 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62  multiple times b
96e60 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61  y the same threa
96e70 64 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65  d.  In such case
96e80 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20  s the,.** mutex 
96e90 6d 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61  must be exited a
96ea0 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f  n equal number o
96eb0 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61  f times before a
96ec0 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a  nother thread.**
96ed0 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20   can enter.  If 
96ee0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20  the same thread 
96ef0 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61  tries to enter a
96f00 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66  ny other kind of
96f10 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74   mutex.** more t
96f20 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65  han once, the be
96f30 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69  havior is undefi
96f40 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ned..*/.static v
96f50 6f 69 64 20 70 74 68 72 65 61 64 4d 75 74 65 78  oid pthreadMutex
96f60 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75  Enter(sqlite3_mu
96f70 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  tex *p){.  asser
96f80 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45  t( p->id==SQLITE
96f90 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
96fa0 20 7c 7c 20 70 74 68 72 65 61 64 4d 75 74 65 78   || pthreadMutex
96fb0 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 0a 23  Notheld(p) );..#
96fc0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d  ifdef SQLITE_HOM
96fd0 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45  EGROWN_RECURSIVE
96fe0 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49 66 20 72  _MUTEX.  /* If r
96ff0 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73  ecursive mutexes
97000 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62   are not availab
97010 6c 65 2c 20 74 68 65 6e 20 77 65 20 68 61 76 65  le, then we have
97020 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a 20 6f 75   to grow.  ** ou
97030 72 20 6f 77 6e 2e 20 20 54 68 69 73 20 69 6d 70  r own.  This imp
97040 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 75  lementation assu
97050 6d 65 73 20 74 68 61 74 20 70 74 68 72 65 61 64  mes that pthread
97060 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a 20 69 73  _equal().  ** is
97070 20 61 74 6f 6d 69 63 20 2d 20 74 68 61 74 20 69   atomic - that i
97080 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 65 63 65  t cannot be dece
97090 69 76 65 64 20 69 6e 74 6f 20 74 68 69 6e 6b 69  ived into thinki
970a0 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20 61 6e 64  ng self.  ** and
970b0 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65 20 65 71   p->owner are eq
970c0 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e 65 72 20  ual if p->owner 
970d0 63 68 61 6e 67 65 73 20 62 65 74 77 65 65 6e 20  changes between 
970e0 74 77 6f 20 76 61 6c 75 65 73 0a 20 20 2a 2a 20  two values.  ** 
970f0 74 68 61 74 20 61 72 65 20 6e 6f 74 20 65 71 75  that are not equ
97100 61 6c 20 74 6f 20 73 65 6c 66 20 77 68 69 6c 65  al to self while
97110 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20   the comparison 
97120 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 63 65 2e  is taking place.
97130 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65  .  ** This imple
97140 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73 6f 20 61  mentation also a
97150 73 73 75 6d 65 73 20 61 20 63 6f 68 65 72 65 6e  ssumes a coheren
97160 74 20 63 61 63 68 65 20 2d 20 74 68 61 74 20 0a  t cache - that .
97170 20 20 2a 2a 20 73 65 70 61 72 61 74 65 20 70 72    ** separate pr
97180 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f 74 20 72  ocesses cannot r
97190 65 61 64 20 64 69 66 66 65 72 65 6e 74 20 76 61  ead different va
971a0 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 20 73 61  lues from the sa
971b0 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65 73 73 20  me.  ** address 
971c0 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65  at the same time
971d0 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 66 20  .  If either of 
971e0 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 64 69 74  these two condit
971f0 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65 20 6e 6f  ions.  ** are no
97200 74 20 6d 65 74 2c 20 74 68 65 6e 20 74 68 65 20  t met, then the 
97210 6d 75 74 65 78 65 73 20 77 69 6c 6c 20 66 61 69  mutexes will fai
97220 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d 73 20 77  l and problems w
97230 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20 20 2a 2f  ill result..  */
97240 0a 20 20 7b 0a 20 20 20 20 70 74 68 72 65 61 64  .  {.    pthread
97250 5f 74 20 73 65 6c 66 20 3d 20 70 74 68 72 65 61  _t self = pthrea
97260 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 69 66  d_self();.    if
97270 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26 26 20 70  ( p->nRef>0 && p
97280 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e  thread_equal(p->
97290 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20 29 7b 0a  owner, self) ){.
972a0 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b        p->nRef++;
972b0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
972c0 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f    pthread_mutex_
972d0 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b  lock(&p->mutex);
972e0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
972f0 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 20  ->nRef==0 );.   
97300 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 73 65     p->owner = se
97310 6c 66 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65  lf;.      p->nRe
97320 66 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  f = 1;.    }.  }
97330 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 55 73 65 20  .#else.  /* Use 
97340 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 72 65 63  the built-in rec
97350 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 69  ursive mutexes i
97360 66 20 74 68 65 79 20 61 72 65 20 61 76 61 69 6c  f they are avail
97370 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 70 74 68  able..  */.  pth
97380 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28  read_mutex_lock(
97390 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d  &p->mutex);.  p-
973a0 3e 6f 77 6e 65 72 20 3d 20 70 74 68 72 65 61 64  >owner = pthread
973b0 5f 73 65 6c 66 28 29 3b 0a 20 20 70 2d 3e 6e 52  _self();.  p->nR
973c0 65 66 2b 2b 3b 0a 23 65 6e 64 69 66 0a 0a 23 69  ef++;.#endif..#i
973d0 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
973e0 47 0a 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65  G.  if( p->trace
973f0 20 29 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22   ){.    printf("
97400 65 6e 74 65 72 20 6d 75 74 65 78 20 25 70 20 28  enter mutex %p (
97410 25 64 29 20 77 69 74 68 20 6e 52 65 66 3d 25 64  %d) with nRef=%d
97420 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65  \n", p, p->trace
97430 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a  , p->nRef);.  }.
97440 23 65 6e 64 69 66 0a 7d 0a 73 74 61 74 69 63 20  #endif.}.static 
97450 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78  int pthreadMutex
97460 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  Try(sqlite3_mute
97470 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b  x *p){.  int rc;
97480 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64  .  assert( p->id
97490 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  ==SQLITE_MUTEX_R
974a0 45 43 55 52 53 49 56 45 20 7c 7c 20 70 74 68 72  ECURSIVE || pthr
974b0 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28  eadMutexNotheld(
974c0 70 29 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51  p) );..#ifdef SQ
974d0 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52  LITE_HOMEGROWN_R
974e0 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20  ECURSIVE_MUTEX. 
974f0 20 2f 2a 20 49 66 20 72 65 63 75 72 73 69 76 65   /* If recursive
97500 20 6d 75 74 65 78 65 73 20 61 72 65 20 6e 6f 74   mutexes are not
97510 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e   available, then
97520 20 77 65 20 68 61 76 65 20 74 6f 20 67 72 6f 77   we have to grow
97530 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e 2e 20 20  .  ** our own.  
97540 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74  This implementat
97550 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74  ion assumes that
97560 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29   pthread_equal()
97570 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d 69 63 20  .  ** is atomic 
97580 2d 20 74 68 61 74 20 69 74 20 63 61 6e 6e 6f 74  - that it cannot
97590 20 62 65 20 64 65 63 65 69 76 65 64 20 69 6e 74   be deceived int
975a0 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65 6c 66 0a  o thinking self.
975b0 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f 77 6e 65    ** and p->owne
975c0 72 20 61 72 65 20 65 71 75 61 6c 20 69 66 20 70  r are equal if p
975d0 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67 65 73 20  ->owner changes 
975e0 62 65 74 77 65 65 6e 20 74 77 6f 20 76 61 6c 75  between two valu
975f0 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 61 72 65  es.  ** that are
97600 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 73 65   not equal to se
97610 6c 66 20 77 68 69 6c 65 20 74 68 65 20 63 6f 6d  lf while the com
97620 70 61 72 69 73 6f 6e 20 69 73 20 74 61 6b 69 6e  parison is takin
97630 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a 20 54 68  g place..  ** Th
97640 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  is implementatio
97650 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 61  n also assumes a
97660 20 63 6f 68 65 72 65 6e 74 20 63 61 63 68 65 20   coherent cache 
97670 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20 73 65 70  - that .  ** sep
97680 61 72 61 74 65 20 70 72 6f 63 65 73 73 65 73 20  arate processes 
97690 63 61 6e 6e 6f 74 20 72 65 61 64 20 64 69 66 66  cannot read diff
976a0 65 72 65 6e 74 20 76 61 6c 75 65 73 20 66 72 6f  erent values fro
976b0 6d 20 74 68 65 20 73 61 6d 65 0a 20 20 2a 2a 20  m the same.  ** 
976c0 61 64 64 72 65 73 73 20 61 74 20 74 68 65 20 73  address at the s
976d0 61 6d 65 20 74 69 6d 65 2e 20 20 49 66 20 65 69  ame time.  If ei
976e0 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 74 77  ther of these tw
976f0 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a 20 20 2a  o conditions.  *
97700 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74 2c 20 74  * are not met, t
97710 68 65 6e 20 74 68 65 20 6d 75 74 65 78 65 73 20  hen the mutexes 
97720 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 20 70 72  will fail and pr
97730 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72 65 73 75  oblems will resu
97740 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20 20  lt..  */.  {.   
97750 20 70 74 68 72 65 61 64 5f 74 20 73 65 6c 66 20   pthread_t self 
97760 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29  = pthread_self()
97770 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52 65  ;.    if( p->nRe
97780 66 3e 30 20 26 26 20 70 74 68 72 65 61 64 5f 65  f>0 && pthread_e
97790 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 73  qual(p->owner, s
977a0 65 6c 66 29 20 29 7b 0a 20 20 20 20 20 20 70 2d  elf) ){.      p-
977b0 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20 72  >nRef++;.      r
977c0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
977d0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 74 68     }else if( pth
977e0 72 65 61 64 5f 6d 75 74 65 78 5f 74 72 79 6c 6f  read_mutex_trylo
977f0 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3d 3d 30  ck(&p->mutex)==0
97800 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
97810 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a  ( p->nRef==0 );.
97820 20 20 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d        p->owner =
97830 20 73 65 6c 66 3b 0a 20 20 20 20 20 20 70 2d 3e   self;.      p->
97840 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20 20  nRef = 1;.      
97850 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
97860 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
97870 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
97880 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c  Y;.    }.  }.#el
97890 73 65 0a 20 20 2f 2a 20 55 73 65 20 74 68 65 20  se.  /* Use the 
978a0 62 75 69 6c 74 2d 69 6e 20 72 65 63 75 72 73 69  built-in recursi
978b0 76 65 20 6d 75 74 65 78 65 73 20 69 66 20 74 68  ve mutexes if th
978c0 65 79 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65  ey are available
978d0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 74 68  ..  */.  if( pth
978e0 72 65 61 64 5f 6d 75 74 65 78 5f 74 72 79 6c 6f  read_mutex_trylo
978f0 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3d 3d 30  ck(&p->mutex)==0
97900 20 29 7b 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72   ){.    p->owner
97910 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28   = pthread_self(
97920 29 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b  );.    p->nRef++
97930 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
97940 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  E_OK;.  }else{. 
97950 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
97960 55 53 59 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  USY;.  }.#endif.
97970 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
97980 45 42 55 47 0a 20 20 69 66 28 20 72 63 3d 3d 53  EBUG.  if( rc==S
97990 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 2d 3e 74  QLITE_OK && p->t
979a0 72 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69 6e  race ){.    prin
979b0 74 66 28 22 65 6e 74 65 72 20 6d 75 74 65 78 20  tf("enter mutex 
979c0 25 70 20 28 25 64 29 20 77 69 74 68 20 6e 52 65  %p (%d) with nRe
979d0 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74  f=%d\n", p, p->t
979e0 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a  race, p->nRef);.
979f0 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74    }.#endif.  ret
97a00 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
97a10 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
97a20 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69  ex_leave() routi
97a30 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78  ne exits a mutex
97a40 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65   that was.** pre
97a50 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20  viously entered 
97a60 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  by the same thre
97a70 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f  ad.  The behavio
97a80 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65  r.** is undefine
97a90 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 69  d if the mutex i
97aa0 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  s not currently 
97ab0 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73  entered or.** is
97ac0 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61   not currently a
97ad0 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74  llocated.  SQLit
97ae0 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20  e will never do 
97af0 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  either..*/.stati
97b00 63 20 76 6f 69 64 20 70 74 68 72 65 61 64 4d 75  c void pthreadMu
97b10 74 65 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33  texLeave(sqlite3
97b20 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73  _mutex *p){.  as
97b30 73 65 72 74 28 20 70 74 68 72 65 61 64 4d 75 74  sert( pthreadMut
97b40 65 78 48 65 6c 64 28 70 29 20 29 3b 0a 20 20 70  exHeld(p) );.  p
97b50 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 61 73 73 65  ->nRef--;.  asse
97b60 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c  rt( p->nRef==0 |
97b70 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f  | p->id==SQLITE_
97b80 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20  MUTEX_RECURSIVE 
97b90 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  );..#ifdef SQLIT
97ba0 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55  E_HOMEGROWN_RECU
97bb0 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 20 69 66  RSIVE_MUTEX.  if
97bc0 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a  ( p->nRef==0 ){.
97bd0 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65      pthread_mute
97be0 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74  x_unlock(&p->mut
97bf0 65 78 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20  ex);.  }.#else. 
97c00 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75   pthread_mutex_u
97c10 6e 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29  nlock(&p->mutex)
97c20 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  ;.#endif..#ifdef
97c30 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
97c40 69 66 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a  if( p->trace ){.
97c50 20 20 20 20 70 72 69 6e 74 66 28 22 6c 65 61 76      printf("leav
97c60 65 20 6d 75 74 65 78 20 25 70 20 28 25 64 29 20  e mutex %p (%d) 
97c70 77 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c  with nRef=%d\n",
97c80 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d   p, p->trace, p-
97c90 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64  >nRef);.  }.#end
97ca0 69 66 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49  if.}..SQLITE_PRI
97cb0 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74  VATE sqlite3_mut
97cc0 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69  ex_methods *sqli
97cd0 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28  te3DefaultMutex(
97ce0 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20  void){.  static 
97cf0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
97d00 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b  thods sMutex = {
97d10 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65  .    pthreadMute
97d20 78 49 6e 69 74 2c 0a 20 20 20 20 70 74 68 72 65  xInit,.    pthre
97d30 61 64 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20 20  adMutexEnd,.    
97d40 70 74 68 72 65 61 64 4d 75 74 65 78 41 6c 6c 6f  pthreadMutexAllo
97d50 63 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75  c,.    pthreadMu
97d60 74 65 78 46 72 65 65 2c 0a 20 20 20 20 70 74 68  texFree,.    pth
97d70 72 65 61 64 4d 75 74 65 78 45 6e 74 65 72 2c 0a  readMutexEnter,.
97d80 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78      pthreadMutex
97d90 54 72 79 2c 0a 20 20 20 20 70 74 68 72 65 61 64  Try,.    pthread
97da0 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23 69 66 64  MutexLeave,.#ifd
97db0 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
97dc0 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78      pthreadMutex
97dd0 48 65 6c 64 2c 0a 20 20 20 20 70 74 68 72 65 61  Held,.    pthrea
97de0 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65  dMutexNotheld.#e
97df0 6e 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74  ndif.  };..  ret
97e00 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 0a  urn &sMutex;.}..
97e10 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
97e20 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 20 2a  _MUTEX_PTHREAD *
97e30 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
97e40 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f  ** End of mutex_
97e50 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  unix.c *********
97e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97e80 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
97e90 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75  ** Begin file mu
97ea0 74 65 78 5f 77 33 32 2e 63 20 2a 2a 2a 2a 2a 2a  tex_w32.c ******
97eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97ed0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75  */./*.** 2007 Au
97ee0 67 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68  gust 14.**.** Th
97ef0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
97f00 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
97f10 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
97f20 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
97f30 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
97f40 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
97f50 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
97f60 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
97f70 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
97f80 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
97f90 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
97fa0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
97fb0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
97fc0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
97fd0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
97fe0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
97ff0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
98000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98040 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
98050 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ile contains the
98060 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61   C functions tha
98070 74 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65  t implement mute
98080 78 65 73 20 66 6f 72 20 77 69 6e 33 32 0a 2a 2a  xes for win32.**
98090 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f 77  .** $Id: mutex_w
980a0 33 32 2e 63 2c 76 20 31 2e 31 31 20 32 30 30 38  32.c,v 1.11 2008
980b0 2f 30 36 2f 32 36 20 31 30 3a 34 31 3a 31 39 20  /06/26 10:41:19 
980c0 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
980d0 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  $.*/../*.** The 
980e0 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c  code in this fil
980f0 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69  e is only used i
98100 66 20 77 65 20 61 72 65 20 63 6f 6d 70 69 6c 69  f we are compili
98110 6e 67 20 6d 75 6c 74 69 74 68 72 65 61 64 65 64  ng multithreaded
98120 0a 2a 2a 20 6f 6e 20 61 20 77 69 6e 33 32 20 73  .** on a win32 s
98130 79 73 74 65 6d 2e 0a 2a 2f 0a 23 69 66 64 65 66  ystem..*/.#ifdef
98140 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33   SQLITE_MUTEX_W3
98150 32 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 72 65  2../*.** Each re
98160 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 73  cursive mutex is
98170 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
98180 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
98190 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75  ructure..*/.stru
981a0 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ct sqlite3_mutex
981b0 20 7b 0a 20 20 43 52 49 54 49 43 41 4c 5f 53 45   {.  CRITICAL_SE
981c0 43 54 49 4f 4e 20 6d 75 74 65 78 3b 20 20 20 20  CTION mutex;    
981d0 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c  /* Mutex control
981e0 6c 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f  ling the lock */
981f0 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20 20 20  .  int id;      
98200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
98210 20 4d 75 74 65 78 20 74 79 70 65 20 2a 2f 0a 20   Mutex type */. 
98220 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20   int nRef;      
98230 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
98240 75 6d 62 65 72 20 6f 66 20 65 6e 74 65 72 61 6e  umber of enteran
98250 63 65 73 20 2a 2f 0a 20 20 44 57 4f 52 44 20 6f  ces */.  DWORD o
98260 77 6e 65 72 3b 20 20 20 20 20 20 20 20 20 20 20  wner;           
98270 20 20 20 20 2f 2a 20 54 68 72 65 61 64 20 68 6f      /* Thread ho
98280 6c 64 69 6e 67 20 74 68 69 73 20 6d 75 74 65 78  lding this mutex
98290 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65   */.};../*.** Re
982a0 74 75 72 6e 20 74 72 75 65 20 28 6e 6f 6e 2d 7a  turn true (non-z
982b0 65 72 6f 29 20 69 66 20 77 65 20 61 72 65 20 72  ero) if we are r
982c0 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20 57 69 6e  unning under Win
982d0 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69 6e 58 50  NT, Win2K, WinXP
982e0 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45 2e 20 20  ,.** or WinCE.  
982f0 52 65 74 75 72 6e 20 66 61 6c 73 65 20 28 7a 65  Return false (ze
98300 72 6f 29 20 66 6f 72 20 57 69 6e 39 35 2c 20 57  ro) for Win95, W
98310 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d 45 2e 0a  in98, or WinME..
98320 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73 20 61 6e  **.** Here is an
98330 20 69 6e 74 65 72 65 73 74 69 6e 67 20 6f 62 73   interesting obs
98340 65 72 76 61 74 69 6f 6e 3a 20 20 57 69 6e 39 35  ervation:  Win95
98350 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20 57 69 6e  , Win98, and Win
98360 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68 65 20 4c  ME lack.** the L
98370 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e  ockFileEx() API.
98380 20 20 42 75 74 20 77 65 20 63 61 6e 20 73 74 69    But we can sti
98390 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69  ll statically li
983a0 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 61 74 0a  nk against that.
983b0 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e 67 20 61  ** API as long a
983c0 73 20 77 65 20 64 6f 6e 27 74 20 63 61 6c 6c 20  s we don't call 
983d0 69 74 20 77 69 6e 20 72 75 6e 6e 69 6e 67 20 57  it win running W
983e0 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 41 20 63  in95/98/ME.  A c
983f0 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 69 73 20 72  all to.** this r
98400 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74  outine is used t
98410 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74  o determine if t
98420 68 65 20 68 6f 73 74 20 69 73 20 57 69 6e 39 35  he host is Win95
98430 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a 20 57 69 6e  /98/ME or.** Win
98440 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 74 68 61 74  NT/2K/XP so that
98450 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68   we will know wh
98460 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 77 65 20  ether or not we 
98470 63 61 6e 20 73 61 66 65 6c 79 20 63 61 6c 6c 0a  can safely call.
98480 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45  ** the LockFileE
98490 78 28 29 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 20  x() API..*/.#if 
984a0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a  SQLITE_OS_WINCE.
984b0 23 20 64 65 66 69 6e 65 20 6d 75 74 65 78 49 73  # define mutexIs
984c0 4e 54 28 29 20 20 28 31 29 0a 23 65 6c 73 65 0a  NT()  (1).#else.
984d0 20 20 73 74 61 74 69 63 20 69 6e 74 20 6d 75 74    static int mut
984e0 65 78 49 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20  exIsNT(void){.  
984f0 20 20 73 74 61 74 69 63 20 69 6e 74 20 6f 73 54    static int osT
98500 79 70 65 20 3d 20 30 3b 0a 20 20 20 20 69 66 28  ype = 0;.    if(
98510 20 6f 73 54 79 70 65 3d 3d 30 20 29 7b 0a 20 20   osType==0 ){.  
98520 20 20 20 20 4f 53 56 45 52 53 49 4f 4e 49 4e 46      OSVERSIONINF
98530 4f 20 73 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73  O sInfo;.      s
98540 49 6e 66 6f 2e 64 77 4f 53 56 65 72 73 69 6f 6e  Info.dwOSVersion
98550 49 6e 66 6f 53 69 7a 65 20 3d 20 73 69 7a 65 6f  InfoSize = sizeo
98560 66 28 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20  f(sInfo);.      
98570 47 65 74 56 65 72 73 69 6f 6e 45 78 28 26 73 49  GetVersionEx(&sI
98580 6e 66 6f 29 3b 0a 20 20 20 20 20 20 6f 73 54 79  nfo);.      osTy
98590 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50 6c 61  pe = sInfo.dwPla
985a0 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50 4c 41  tformId==VER_PLA
985b0 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54 20 3f  TFORM_WIN32_NT ?
985c0 20 32 20 3a 20 31 3b 0a 20 20 20 20 7d 0a 20 20   2 : 1;.    }.  
985d0 20 20 72 65 74 75 72 6e 20 6f 73 54 79 70 65 3d    return osType=
985e0 3d 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f  =2;.  }.#endif /
985f0 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  * SQLITE_OS_WINC
98600 45 20 2a 2f 0a 0a 0a 23 69 66 64 65 66 20 53 51  E */...#ifdef SQ
98610 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a  LITE_DEBUG./*.**
98620 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
98630 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71  ex_held() and sq
98640 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68  lite3_mutex_noth
98650 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72  eld() routine ar
98660 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f  e.** intended fo
98670 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64  r use only insid
98680 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65  e assert() state
98690 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  ments..*/.static
986a0 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 48 65 6c   int winMutexHel
986b0 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  d(sqlite3_mutex 
986c0 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d  *p){.  return p-
986d0 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 2d 3e 6f  >nRef!=0 && p->o
986e0 77 6e 65 72 3d 3d 47 65 74 43 75 72 72 65 6e 74  wner==GetCurrent
986f0 54 68 72 65 61 64 49 64 28 29 3b 0a 7d 0a 73 74  ThreadId();.}.st
98700 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65  atic int winMute
98710 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33  xNotheld(sqlite3
98720 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65  _mutex *p){.  re
98730 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3d 3d 30 20  turn p->nRef==0 
98740 7c 7c 20 70 2d 3e 6f 77 6e 65 72 21 3d 47 65 74  || p->owner!=Get
98750 43 75 72 72 65 6e 74 54 68 72 65 61 64 49 64 28  CurrentThreadId(
98760 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  );.}.#endif.../*
98770 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61  .** Initialize a
98780 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20  nd deinitialize 
98790 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73  the mutex subsys
987a0 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  tem..*/.static i
987b0 6e 74 20 77 69 6e 4d 75 74 65 78 49 6e 69 74 28  nt winMutexInit(
987c0 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51  void){ return SQ
987d0 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69  LITE_OK; }.stati
987e0 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 45 6e  c int winMutexEn
987f0 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20  d(void){ return 
98800 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a  SQLITE_OK; }../*
98810 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
98820 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f  mutex_alloc() ro
98830 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20  utine allocates 
98840 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61  a new.** mutex a
98850 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  nd returns a poi
98860 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66 20  nter to it.  If 
98870 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a  it returns NULL.
98880 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  ** that means th
98890 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c 64  at a mutex could
988a0 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65   not be allocate
988b0 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69  d.  SQLite.** wi
988c0 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73 74  ll unwind its st
988d0 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 61  ack and return a
988e0 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20 61 72  n error.  The ar
988f0 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c  gument.** to sql
98900 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
98910 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65  () is one of the
98920 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74  se integer const
98930 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e  ants:.**.** <ul>
98940 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45  .** <li>  SQLITE
98950 5f 4d 55 54 45 58 5f 46 41 53 54 20 20 20 20 20  _MUTEX_FAST     
98960 20 20 20 20 20 20 20 20 20 20 30 0a 2a 2a 20 3c            0.** <
98970 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
98980 58 5f 52 45 43 55 52 53 49 56 45 20 20 20 20 20  X_RECURSIVE     
98990 20 20 20 20 20 31 0a 2a 2a 20 3c 6c 69 3e 20 20       1.** <li>  
989a0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
989b0 54 49 43 5f 4d 41 53 54 45 52 20 20 20 20 20 20  TIC_MASTER      
989c0 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  2.** <li>  SQLIT
989d0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
989e0 45 4d 20 20 20 20 20 20 20 20 20 33 0a 2a 2a 20  EM         3.** 
989f0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
98a00 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 20 20  EX_STATIC_PRNG  
98a10 20 20 20 20 20 20 34 0a 2a 2a 20 3c 2f 75 6c 3e        4.** </ul>
98a20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
98a30 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63   two constants c
98a40 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 74  ause sqlite3_mut
98a50 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72  ex_alloc() to cr
98a60 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75  eate.** a new mu
98a70 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d 75  tex.  The new mu
98a80 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76 65  tex is recursive
98a90 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54   when SQLITE_MUT
98aa0 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20  EX_RECURSIVE.** 
98ab0 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74 20  is used but not 
98ac0 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20 77  necessarily so w
98ad0 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  hen SQLITE_MUTEX
98ae0 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 0a 2a  _FAST is used..*
98af0 2a 20 54 68 65 20 6d 75 74 65 78 20 69 6d 70 6c  * The mutex impl
98b00 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20  ementation does 
98b10 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65  not need to make
98b20 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a 2a   a distinction.*
98b30 2a 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54 45  * between SQLITE
98b40 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
98b50 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 45   and SQLITE_MUTE
98b60 58 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f 65  X_FAST if it doe
98b70 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f  s.** not want to
98b80 2e 20 20 42 75 74 20 53 51 4c 69 74 65 20 77 69  .  But SQLite wi
98b90 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20  ll only request 
98ba0 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  a recursive mute
98bb0 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68  x in.** cases wh
98bc0 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65  ere it really ne
98bd0 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 61 20 66  eds one.  If a f
98be0 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73  aster non-recurs
98bf0 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70  ive mutex.** imp
98c00 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61  lementation is a
98c10 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20  vailable on the 
98c20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74  host platform, t
98c30 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74  he mutex subsyst
98c40 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75  em.** might retu
98c50 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78 20  rn such a mutex 
98c60 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 53  in response to S
98c70 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
98c80 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68 65  ..**.** The othe
98c90 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65  r allowed parame
98ca0 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f  ters to sqlite3_
98cb0 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61  mutex_alloc() ea
98cc0 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70  ch return.** a p
98cd0 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74  ointer to a stat
98ce0 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d  ic preexisting m
98cf0 75 74 65 78 2e 20 20 54 68 72 65 65 20 73 74 61  utex.  Three sta
98d00 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a  tic mutexes are.
98d10 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63  ** used by the c
98d20 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f  urrent version o
98d30 66 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72  f SQLite.  Futur
98d40 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  e versions of SQ
98d50 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20  Lite.** may add 
98d60 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69  additional stati
98d70 63 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74  c mutexes.  Stat
98d80 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66  ic mutexes are f
98d90 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75  or internal.** u
98da0 73 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c  se by SQLite onl
98db0 79 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73  y.  Applications
98dc0 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65   that use SQLite
98dd0 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a   mutexes should.
98de0 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20  ** use only the 
98df0 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20  dynamic mutexes 
98e00 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49  returned by SQLI
98e10 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72  TE_MUTEX_FAST or
98e20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  .** SQLITE_MUTEX
98e30 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a  _RECURSIVE..**.*
98e40 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f  * Note that if o
98e50 6e 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69  ne of the dynami
98e60 63 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65  c mutex paramete
98e70 72 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  rs (SQLITE_MUTEX
98e80 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49  _FAST.** or SQLI
98e90 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
98ea0 56 45 29 20 69 73 20 75 73 65 64 20 74 68 65 6e  VE) is used then
98eb0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
98ec0 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e  lloc().** return
98ed0 73 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75  s a different mu
98ee0 74 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c  tex on every cal
98ef0 6c 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65 20  l.  But for the 
98f00 73 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78  static .** mutex
98f10 20 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65   types, the same
98f20 20 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 6e   mutex is return
98f30 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c  ed on every call
98f40 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65   that has.** the
98f50 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65   same type numbe
98f60 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c  r..*/.static sql
98f70 69 74 65 33 5f 6d 75 74 65 78 20 2a 77 69 6e 4d  ite3_mutex *winM
98f80 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 54  utexAlloc(int iT
98f90 79 70 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  ype){.  sqlite3_
98fa0 6d 75 74 65 78 20 2a 70 3b 0a 0a 20 20 73 77 69  mutex *p;..  swi
98fb0 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 20  tch( iType ){.  
98fc0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55    case SQLITE_MU
98fd0 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 61  TEX_FAST:.    ca
98fe0 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  se SQLITE_MUTEX_
98ff0 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20  RECURSIVE: {.   
99000 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61     p = sqlite3Ma
99010 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66  llocZero( sizeof
99020 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66  (*p) );.      if
99030 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ( p ){.        p
99040 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20  ->id = iType;.  
99050 20 20 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65        Initialize
99060 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28  CriticalSection(
99070 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20  &p->mutex);.    
99080 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
99090 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75  .    }.    defau
990a0 6c 74 3a 20 7b 0a 20 20 20 20 20 20 73 74 61 74  lt: {.      stat
990b0 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ic sqlite3_mutex
990c0 20 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 36   staticMutexes[6
990d0 5d 3b 0a 20 20 20 20 20 20 73 74 61 74 69 63 20  ];.      static 
990e0 69 6e 74 20 69 73 49 6e 69 74 20 3d 20 30 3b 0a  int isInit = 0;.
990f0 20 20 20 20 20 20 77 68 69 6c 65 28 20 21 69 73        while( !is
99100 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Init ){.        
99110 73 74 61 74 69 63 20 6c 6f 6e 67 20 6c 6f 63 6b  static long lock
99120 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66   = 0;.        if
99130 28 20 49 6e 74 65 72 6c 6f 63 6b 65 64 49 6e 63  ( InterlockedInc
99140 72 65 6d 65 6e 74 28 26 6c 6f 63 6b 29 3d 3d 31  rement(&lock)==1
99150 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e   ){.          in
99160 74 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 66  t i;.          f
99170 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66  or(i=0; i<sizeof
99180 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 29 2f  (staticMutexes)/
99190 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74  sizeof(staticMut
991a0 65 78 65 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a  exes[0]); i++){.
991b0 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 69 74              Init
991c0 69 61 6c 69 7a 65 43 72 69 74 69 63 61 6c 53 65  ializeCriticalSe
991d0 63 74 69 6f 6e 28 26 73 74 61 74 69 63 4d 75 74  ction(&staticMut
991e0 65 78 65 73 5b 69 5d 2e 6d 75 74 65 78 29 3b 0a  exes[i].mutex);.
991f0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
99200 20 20 20 20 20 20 69 73 49 6e 69 74 20 3d 20 31        isInit = 1
99210 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
99220 0a 20 20 20 20 20 20 20 20 20 20 53 6c 65 65 70  .          Sleep
99230 28 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  (1);.        }. 
99240 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73       }.      ass
99250 65 72 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20  ert( iType-2 >= 
99260 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  0 );.      asser
99270 74 28 20 69 54 79 70 65 2d 32 20 3c 20 73 69 7a  t( iType-2 < siz
99280 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 65  eof(staticMutexe
99290 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 74 69 63  s)/sizeof(static
992a0 4d 75 74 65 78 65 73 5b 30 5d 29 20 29 3b 0a 20  Mutexes[0]) );. 
992b0 20 20 20 20 20 70 20 3d 20 26 73 74 61 74 69 63       p = &static
992c0 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d 32 5d  Mutexes[iType-2]
992d0 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20  ;.      p->id = 
992e0 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 72 65  iType;.      bre
992f0 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ak;.    }.  }.  
99300 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a  return p;.}.../*
99310 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
99320 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70   deallocates a p
99330 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c 6c  reviously.** all
99340 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 20 20 53  ocated mutex.  S
99350 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75 6c  QLite is careful
99360 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 65   to deallocate e
99370 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78 20 74 68  very.** mutex th
99380 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e  at it allocates.
99390 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
993a0 77 69 6e 4d 75 74 65 78 46 72 65 65 28 73 71 6c  winMutexFree(sql
993b0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
993c0 20 20 61 73 73 65 72 74 28 20 70 20 29 3b 0a 20    assert( p );. 
993d0 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66   assert( p->nRef
993e0 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
993f0 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d   p->id==SQLITE_M
99400 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e  UTEX_FAST || p->
99410 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
99420 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20  _RECURSIVE );.  
99430 44 65 6c 65 74 65 43 72 69 74 69 63 61 6c 53 65  DeleteCriticalSe
99440 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29  ction(&p->mutex)
99450 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
99460 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  (p);.}../*.** Th
99470 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
99480 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69  enter() and sqli
99490 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20  te3_mutex_try() 
994a0 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74  routines attempt
994b0 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d  .** to enter a m
994c0 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65  utex.  If anothe
994d0 72 20 74 68 72 65 61 64 20 69 73 20 61 6c 72 65  r thread is alre
994e0 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d  ady within the m
994f0 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33  utex,.** sqlite3
99500 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77  _mutex_enter() w
99510 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71  ill block and sq
99520 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
99530 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a  ) will return.**
99540 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54   SQLITE_BUSY.  T
99550 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
99560 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 65  _try() interface
99570 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
99580 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65  OK.** upon succe
99590 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75  ssful entry.  Mu
995a0 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73  texes created us
995b0 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ing SQLITE_MUTEX
995c0 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e 0a 2a  _RECURSIVE can.*
995d0 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c  * be entered mul
995e0 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74  tiple times by t
995f0 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20  he same thread. 
99600 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 20 74   In such cases t
99610 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73  he,.** mutex mus
99620 74 20 62 65 20 65 78 69 74 65 64 20 61 6e 20 65  t be exited an e
99630 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74  qual number of t
99640 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74  imes before anot
99650 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61  her thread.** ca
99660 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65  n enter.  If the
99670 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 69   same thread tri
99680 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20  es to enter any 
99690 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75  other kind of mu
996a0 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e  tex.** more than
996b0 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76   once, the behav
996c0 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64  ior is undefined
996d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
996e0 20 77 69 6e 4d 75 74 65 78 45 6e 74 65 72 28 73   winMutexEnter(s
996f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
99700 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  {.  assert( p->i
99710 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
99720 52 45 43 55 52 53 49 56 45 20 7c 7c 20 77 69 6e  RECURSIVE || win
99730 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20  MutexNotheld(p) 
99740 29 3b 0a 20 20 45 6e 74 65 72 43 72 69 74 69 63  );.  EnterCritic
99750 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75  alSection(&p->mu
99760 74 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72  tex);.  p->owner
99770 20 3d 20 47 65 74 43 75 72 72 65 6e 74 54 68 72   = GetCurrentThr
99780 65 61 64 49 64 28 29 3b 20 0a 20 20 70 2d 3e 6e  eadId(); .  p->n
99790 52 65 66 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63 20  Ref++;.}.static 
997a0 69 6e 74 20 77 69 6e 4d 75 74 65 78 54 72 79 28  int winMutexTry(
997b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
997c0 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
997d0 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 61 73 73  LITE_BUSY;.  ass
997e0 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  ert( p->id==SQLI
997f0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
99800 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78 4e 6f  VE || winMutexNo
99810 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 2f 2a  theld(p) );.  /*
99820 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  .  ** The sqlite
99830 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f  3_mutex_try() ro
99840 75 74 69 6e 65 20 69 73 20 76 65 72 79 20 72 61  utine is very ra
99850 72 65 6c 79 20 75 73 65 64 2c 20 61 6e 64 20 77  rely used, and w
99860 68 65 6e 20 69 74 0a 20 20 2a 2a 20 69 73 20 75  hen it.  ** is u
99870 73 65 64 20 69 74 20 69 73 20 6d 65 72 65 6c 79  sed it is merely
99880 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e   an optimization
99890 2e 20 20 53 6f 20 69 74 20 69 73 20 4f 4b 20 66  .  So it is OK f
998a0 6f 72 20 69 74 20 74 6f 20 61 6c 77 61 79 73 0a  or it to always.
998b0 20 20 2a 2a 20 66 61 69 6c 2e 20 20 0a 20 20 2a    ** fail.  .  *
998c0 2a 0a 20 20 2a 2a 20 54 68 65 20 54 72 79 45 6e  *.  ** The TryEn
998d0 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69  terCriticalSecti
998e0 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20 69  on() interface i
998f0 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65  s only available
99900 20 6f 6e 20 57 69 6e 4e 54 2e 0a 20 20 2a 2a 20   on WinNT..  ** 
99910 41 6e 64 20 73 6f 6d 65 20 77 69 6e 64 6f 77 73  And some windows
99920 20 63 6f 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c   compilers compl
99930 61 69 6e 20 69 66 20 79 6f 75 20 74 72 79 20 74  ain if you try t
99940 6f 20 75 73 65 20 69 74 20 77 69 74 68 6f 75 74  o use it without
99950 0a 20 20 2a 2a 20 66 69 72 73 74 20 64 6f 69 6e  .  ** first doin
99960 67 20 73 6f 6d 65 20 23 64 65 66 69 6e 65 73 20  g some #defines 
99970 74 68 61 74 20 70 72 65 76 65 6e 74 20 53 51 4c  that prevent SQL
99980 69 74 65 20 66 72 6f 6d 20 62 75 69 6c 64 69 6e  ite from buildin
99990 67 20 6f 6e 20 57 69 6e 39 38 2e 0a 20 20 2a 2a  g on Win98..  **
999a0 20 46 6f 72 20 74 68 61 74 20 72 65 61 73 6f 6e   For that reason
999b0 2c 20 77 65 20 77 69 6c 6c 20 6f 6d 69 74 20 74  , we will omit t
999c0 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  his optimization
999d0 20 66 6f 72 20 6e 6f 77 2e 20 20 53 65 65 0a 20   for now.  See. 
999e0 20 2a 2a 20 74 69 63 6b 65 74 20 23 32 36 38 35   ** ticket #2685
999f0 2e 0a 20 20 2a 2f 0a 23 69 66 20 30 0a 20 20 69  ..  */.#if 0.  i
99a00 66 28 20 6d 75 74 65 78 49 73 4e 54 28 29 20 26  f( mutexIsNT() &
99a10 26 20 54 72 79 45 6e 74 65 72 43 72 69 74 69 63  & TryEnterCritic
99a20 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75  alSection(&p->mu
99a30 74 65 78 29 20 29 7b 0a 20 20 20 20 70 2d 3e 6f  tex) ){.    p->o
99a40 77 6e 65 72 20 3d 20 47 65 74 43 75 72 72 65 6e  wner = GetCurren
99a50 74 54 68 72 65 61 64 49 64 28 29 3b 0a 20 20 20  tThreadId();.   
99a60 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20   p->nRef++;.    
99a70 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
99a80 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74    }.#endif.  ret
99a90 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
99aa0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
99ab0 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69  ex_leave() routi
99ac0 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78  ne exits a mutex
99ad0 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65   that was.** pre
99ae0 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20  viously entered 
99af0 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  by the same thre
99b00 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f  ad.  The behavio
99b10 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65  r.** is undefine
99b20 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 69  d if the mutex i
99b30 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  s not currently 
99b40 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73  entered or.** is
99b50 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61   not currently a
99b60 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74  llocated.  SQLit
99b70 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20  e will never do 
99b80 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  either..*/.stati
99b90 63 20 76 6f 69 64 20 77 69 6e 4d 75 74 65 78 4c  c void winMutexL
99ba0 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  eave(sqlite3_mut
99bb0 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  ex *p){.  assert
99bc0 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20  ( p->nRef>0 );. 
99bd0 20 61 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65   assert( p->owne
99be0 72 3d 3d 47 65 74 43 75 72 72 65 6e 74 54 68 72  r==GetCurrentThr
99bf0 65 61 64 49 64 28 29 20 29 3b 0a 20 20 70 2d 3e  eadId() );.  p->
99c00 6e 52 65 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74  nRef--;.  assert
99c10 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20  ( p->nRef==0 || 
99c20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
99c30 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b  TEX_RECURSIVE );
99c40 0a 20 20 4c 65 61 76 65 43 72 69 74 69 63 61 6c  .  LeaveCritical
99c50 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65  Section(&p->mute
99c60 78 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52  x);.}..SQLITE_PR
99c70 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75  IVATE sqlite3_mu
99c80 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c  tex_methods *sql
99c90 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78  ite3DefaultMutex
99ca0 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63  (void){.  static
99cb0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d   sqlite3_mutex_m
99cc0 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20  ethods sMutex = 
99cd0 7b 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 49 6e  {.    winMutexIn
99ce0 69 74 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78  it,.    winMutex
99cf0 45 6e 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65  End,.    winMute
99d00 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 77 69 6e 4d  xAlloc,.    winM
99d10 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 77 69  utexFree,.    wi
99d20 6e 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20  nMutexEnter,.   
99d30 20 77 69 6e 4d 75 74 65 78 54 72 79 2c 0a 20 20   winMutexTry,.  
99d40 20 20 77 69 6e 4d 75 74 65 78 4c 65 61 76 65 2c    winMutexLeave,
99d50 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
99d60 45 42 55 47 0a 20 20 20 20 77 69 6e 4d 75 74 65  EBUG.    winMute
99d70 78 48 65 6c 64 2c 0a 20 20 20 20 77 69 6e 4d 75  xHeld,.    winMu
99d80 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6e 64 69  texNotheld.#endi
99d90 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e  f.  };..  return
99da0 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64   &sMutex;.}.#end
99db0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54  if /* SQLITE_MUT
99dc0 45 58 5f 57 33 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  EX_W32 */../****
99dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
99de0 66 20 6d 75 74 65 78 5f 77 33 32 2e 63 20 2a 2a  f mutex_w32.c **
99df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
99e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
99e30 20 66 69 6c 65 20 6d 61 6c 6c 6f 63 2e 63 20 2a   file malloc.c *
99e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
99e70 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
99e80 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
99e90 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
99ea0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
99eb0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
99ec0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
99ed0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
99ee0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
99ef0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
99f00 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
99f10 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
99f20 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
99f30 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
99f40 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
99f50 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
99f60 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
99f70 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
99f80 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
99f90 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
99fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99fe0 2a 0a 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20 61  *.**.** Memory a
99ff0 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69  llocation functi
9a000 6f 6e 73 20 75 73 65 64 20 74 68 72 6f 75 67 68  ons used through
9a010 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a 2a 0a 2a  out sqlite..**.*
9a020 2a 20 24 49 64 3a 20 6d 61 6c 6c 6f 63 2e 63 2c  * $Id: malloc.c,
9a030 76 20 31 2e 34 35 20 32 30 30 38 2f 31 30 2f 31  v 1.45 2008/10/1
9a040 32 20 30 30 3a 32 37 3a 35 33 20 73 68 61 6e 65  2 00:27:53 shane
9a050 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   Exp $.*/../*.**
9a060 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75   This routine ru
9a070 6e 73 20 77 68 65 6e 20 74 68 65 20 6d 65 6d 6f  ns when the memo
9a080 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 73 65 65  ry allocator see
9a090 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 74 6f  s that the.** to
9a0a0 74 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  tal memory alloc
9a0b0 61 74 69 6f 6e 20 69 73 20 61 62 6f 75 74 20 74  ation is about t
9a0c0 6f 20 65 78 63 65 65 64 20 74 68 65 20 73 6f 66  o exceed the sof
9a0d0 74 20 68 65 61 70 0a 2a 2a 20 6c 69 6d 69 74 2e  t heap.** limit.
9a0e0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
9a0f0 73 6f 66 74 48 65 61 70 4c 69 6d 69 74 45 6e 66  softHeapLimitEnf
9a100 6f 72 63 65 72 28 0a 20 20 76 6f 69 64 20 2a 4e  orcer(.  void *N
9a110 6f 74 55 73 65 64 2c 20 0a 20 20 73 71 6c 69 74  otUsed, .  sqlit
9a120 65 33 5f 69 6e 74 36 34 20 69 6e 55 73 65 2c 0a  e3_int64 inUse,.
9a130 20 20 69 6e 74 20 61 6c 6c 6f 63 53 69 7a 65 0a    int allocSize.
9a140 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 6c  ){.  sqlite3_rel
9a150 65 61 73 65 5f 6d 65 6d 6f 72 79 28 61 6c 6c 6f  ease_memory(allo
9a160 63 53 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  cSize);.}../*.**
9a170 20 53 65 74 20 74 68 65 20 73 6f 66 74 20 68 65   Set the soft he
9a180 61 70 2d 73 69 7a 65 20 6c 69 6d 69 74 20 66 6f  ap-size limit fo
9a190 72 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 50  r the library. P
9a1a0 61 73 73 69 6e 67 20 61 20 7a 65 72 6f 20 6f 72  assing a zero or
9a1b0 20 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 76 61   .** negative va
9a1c0 6c 75 65 20 69 6e 64 69 63 61 74 65 73 20 6e 6f  lue indicates no
9a1d0 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54   limit..*/.SQLIT
9a1e0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
9a1f0 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d  e3_soft_heap_lim
9a200 69 74 28 69 6e 74 20 6e 29 7b 0a 20 20 73 71 6c  it(int n){.  sql
9a210 69 74 65 33 5f 75 69 6e 74 36 34 20 69 4c 69 6d  ite3_uint64 iLim
9a220 69 74 3b 0a 20 20 69 6e 74 20 6f 76 65 72 61 67  it;.  int overag
9a230 65 3b 0a 20 20 69 66 28 20 6e 3c 30 20 29 7b 0a  e;.  if( n<0 ){.
9a240 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 30 3b 0a      iLimit = 0;.
9a250 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 4c 69    }else{.    iLi
9a260 6d 69 74 20 3d 20 6e 3b 0a 20 20 7d 0a 20 20 73  mit = n;.  }.  s
9a270 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
9a280 65 28 29 3b 0a 20 20 69 66 28 20 69 4c 69 6d 69  e();.  if( iLimi
9a290 74 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  t>0 ){.    sqlit
9a2a0 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 73 6f  e3MemoryAlarm(so
9a2b0 66 74 48 65 61 70 4c 69 6d 69 74 45 6e 66 6f 72  ftHeapLimitEnfor
9a2c0 63 65 72 2c 20 30 2c 20 69 4c 69 6d 69 74 29 3b  cer, 0, iLimit);
9a2d0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
9a2e0 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d  lite3MemoryAlarm
9a2f0 28 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20  (0, 0, 0);.  }. 
9a300 20 6f 76 65 72 61 67 65 20 3d 20 73 71 6c 69 74   overage = sqlit
9a310 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29  e3_memory_used()
9a320 20 2d 20 6e 3b 0a 20 20 69 66 28 20 6f 76 65 72   - n;.  if( over
9a330 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c  age>0 ){.    sql
9a340 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d  ite3_release_mem
9a350 6f 72 79 28 6f 76 65 72 61 67 65 29 3b 0a 20 20  ory(overage);.  
9a360 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d  }.}../*.** Attem
9a370 70 74 20 74 6f 20 72 65 6c 65 61 73 65 20 75 70  pt to release up
9a380 20 74 6f 20 6e 20 62 79 74 65 73 20 6f 66 20 6e   to n bytes of n
9a390 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d  on-essential mem
9a3a0 6f 72 79 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a  ory currently.**
9a3b0 20 68 65 6c 64 20 62 79 20 53 51 4c 69 74 65 2e   held by SQLite.
9a3c0 20 41 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 6e   An example of n
9a3d0 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d  on-essential mem
9a3e0 6f 72 79 20 69 73 20 6d 65 6d 6f 72 79 20 75 73  ory is memory us
9a3f0 65 64 20 74 6f 0a 2a 2a 20 63 61 63 68 65 20 64  ed to.** cache d
9a400 61 74 61 62 61 73 65 20 70 61 67 65 73 20 74 68  atabase pages th
9a410 61 74 20 61 72 65 20 6e 6f 74 20 63 75 72 72 65  at are not curre
9a420 6e 74 6c 79 20 69 6e 20 75 73 65 2e 0a 2a 2f 0a  ntly in use..*/.
9a430 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
9a440 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d  qlite3_release_m
9a450 65 6d 6f 72 79 28 69 6e 74 20 6e 29 7b 0a 23 69  emory(int n){.#i
9a460 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
9a470 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45  LE_MEMORY_MANAGE
9a480 4d 45 4e 54 0a 20 20 69 6e 74 20 6e 52 65 74 20  MENT.  int nRet 
9a490 3d 20 30 3b 0a 23 69 66 20 30 0a 20 20 6e 52 65  = 0;.#if 0.  nRe
9a4a0 74 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65  t += sqlite3Vdbe
9a4b0 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 6e 29  ReleaseMemory(n)
9a4c0 3b 0a 23 65 6e 64 69 66 0a 20 20 6e 52 65 74 20  ;.#endif.  nRet 
9a4d0 2b 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  += sqlite3Pcache
9a4e0 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 6e 2d  ReleaseMemory(n-
9a4f0 6e 52 65 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  nRet);.  return 
9a500 6e 52 65 74 3b 0a 23 65 6c 73 65 0a 20 20 72 65  nRet;.#else.  re
9a510 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
9a520 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
9a530 53 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f  State informatio
9a540 6e 20 6c 6f 63 61 6c 20 74 6f 20 74 68 65 20 6d  n local to the m
9a550 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
9a560 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73   subsystem..*/.s
9a570 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44  tatic SQLITE_WSD
9a580 20 73 74 72 75 63 74 20 4d 65 6d 30 47 6c 6f 62   struct Mem0Glob
9a590 61 6c 20 7b 0a 20 20 2f 2a 20 4e 75 6d 62 65 72  al {.  /* Number
9a5a0 20 6f 66 20 66 72 65 65 20 70 61 67 65 73 20 66   of free pages f
9a5b0 6f 72 20 73 63 72 61 74 63 68 20 61 6e 64 20 70  or scratch and p
9a5c0 61 67 65 2d 63 61 63 68 65 20 6d 65 6d 6f 72 79  age-cache memory
9a5d0 20 2a 2f 0a 20 20 75 33 32 20 6e 53 63 72 61 74   */.  u32 nScrat
9a5e0 63 68 46 72 65 65 3b 0a 20 20 75 33 32 20 6e 50  chFree;.  u32 nP
9a5f0 61 67 65 46 72 65 65 3b 0a 0a 20 20 73 71 6c 69  ageFree;..  sqli
9a600 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
9a610 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74  ;         /* Mut
9a620 65 78 20 74 6f 20 73 65 72 69 61 6c 69 7a 65 20  ex to serialize 
9a630 61 63 63 65 73 73 20 2a 2f 0a 0a 20 20 2f 2a 0a  access */..  /*.
9a640 20 20 2a 2a 20 54 68 65 20 61 6c 61 72 6d 20 63    ** The alarm c
9a650 61 6c 6c 62 61 63 6b 20 61 6e 64 20 69 74 73 20  allback and its 
9a660 61 72 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 20  arguments.  The 
9a670 6d 65 6d 30 2e 6d 75 74 65 78 20 6c 6f 63 6b 20  mem0.mutex lock 
9a680 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 68 65 6c  will.  ** be hel
9a690 64 20 77 68 69 6c 65 20 74 68 65 20 63 61 6c 6c  d while the call
9a6a0 62 61 63 6b 20 69 73 20 72 75 6e 6e 69 6e 67 2e  back is running.
9a6b0 20 20 52 65 63 75 72 73 69 76 65 20 63 61 6c 6c    Recursive call
9a6c0 73 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 20  s into.  ** the 
9a6d0 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d  memory subsystem
9a6e0 20 61 72 65 20 61 6c 6c 6f 77 65 64 2c 20 62 75   are allowed, bu
9a6f0 74 20 6e 6f 20 6e 65 77 20 63 61 6c 6c 62 61 63  t no new callbac
9a700 6b 73 20 77 69 6c 6c 20 62 65 0a 20 20 2a 2a 20  ks will be.  ** 
9a710 69 73 73 75 65 64 2e 20 20 54 68 65 20 61 6c 61  issued.  The ala
9a720 72 6d 42 75 73 79 20 76 61 72 69 61 62 6c 65 20  rmBusy variable 
9a730 69 73 20 73 65 74 20 74 6f 20 70 72 65 76 65 6e  is set to preven
9a740 74 20 72 65 63 75 72 73 69 76 65 0a 20 20 2a 2a  t recursive.  **
9a750 20 63 61 6c 6c 62 61 63 6b 73 2e 0a 20 20 2a 2f   callbacks..  */
9a760 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
9a770 20 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 3b   alarmThreshold;
9a780 0a 20 20 76 6f 69 64 20 28 2a 61 6c 61 72 6d 43  .  void (*alarmC
9a790 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 20  allback)(void*, 
9a7a0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e  sqlite3_int64,in
9a7b0 74 29 3b 0a 20 20 76 6f 69 64 20 2a 61 6c 61 72  t);.  void *alar
9a7c0 6d 41 72 67 3b 0a 20 20 69 6e 74 20 61 6c 61 72  mArg;.  int alar
9a7d0 6d 42 75 73 79 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  mBusy;..  /*.  *
9a7e0 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68  * Pointers to th
9a7f0 65 20 65 6e 64 20 6f 66 20 73 71 6c 69 74 65 33  e end of sqlite3
9a800 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63  GlobalConfig.pSc
9a810 72 61 74 63 68 20 61 6e 64 0a 20 20 2a 2a 20 73  ratch and.  ** s
9a820 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9a830 69 67 2e 70 50 61 67 65 20 74 6f 20 61 20 62 6c  ig.pPage to a bl
9a840 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 74 68  ock of memory th
9a850 61 74 20 72 65 63 6f 72 64 73 0a 20 20 2a 2a 20  at records.  ** 
9a860 77 68 69 63 68 20 70 61 67 65 73 20 61 72 65 20  which pages are 
9a870 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a  available..  */.
9a880 20 20 75 33 32 20 2a 61 53 63 72 61 74 63 68 46    u32 *aScratchF
9a890 72 65 65 3b 0a 20 20 75 33 32 20 2a 61 50 61 67  ree;.  u32 *aPag
9a8a0 65 46 72 65 65 3b 0a 7d 20 6d 65 6d 30 20 3d 20  eFree;.} mem0 = 
9a8b0 7b 20 36 32 35 36 30 39 35 35 20 7d 3b 0a 0a 23  { 62560955 };..#
9a8c0 64 65 66 69 6e 65 20 6d 65 6d 30 20 47 4c 4f 42  define mem0 GLOB
9a8d0 41 4c 28 73 74 72 75 63 74 20 4d 65 6d 30 47 6c  AL(struct Mem0Gl
9a8e0 6f 62 61 6c 2c 20 6d 65 6d 30 29 0a 0a 2f 2a 0a  obal, mem0)../*.
9a8f0 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  ** Initialize th
9a900 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
9a910 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a  ion subsystem..*
9a920 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
9a930 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c   int sqlite3Mall
9a940 6f 63 49 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20  ocInit(void){.  
9a950 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
9a960 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f  lConfig.m.xMallo
9a970 63 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  c==0 ){.    sqli
9a980 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74  te3MemSetDefault
9a990 28 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74  ();.  }.  memset
9a9a0 28 26 6d 65 6d 30 2c 20 30 2c 20 73 69 7a 65 6f  (&mem0, 0, sizeo
9a9b0 66 28 6d 65 6d 30 29 29 3b 0a 20 20 69 66 28 20  f(mem0));.  if( 
9a9c0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9a9d0 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29  fig.bCoreMutex )
9a9e0 7b 0a 20 20 20 20 6d 65 6d 30 2e 6d 75 74 65 78  {.    mem0.mutex
9a9f0 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
9aa00 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
9aa10 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20  X_STATIC_MEM);. 
9aa20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
9aa30 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63  GlobalConfig.pSc
9aa40 72 61 74 63 68 20 26 26 20 73 71 6c 69 74 65 33  ratch && sqlite3
9aa50 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53  GlobalConfig.szS
9aa60 63 72 61 74 63 68 3e 3d 31 30 30 0a 20 20 20 20  cratch>=100.    
9aa70 20 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62    && sqlite3Glob
9aa80 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63  alConfig.nScratc
9aa90 68 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  h>=0 ){.    int 
9aaa0 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  i;.    sqlite3Gl
9aab0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72  obalConfig.szScr
9aac0 61 74 63 68 20 2d 3d 20 34 3b 0a 20 20 20 20 6d  atch -= 4;.    m
9aad0 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72 65 65  em0.aScratchFree
9aae0 20 3d 20 28 75 33 32 2a 29 26 28 28 63 68 61 72   = (u32*)&((char
9aaf0 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  *)sqlite3GlobalC
9ab00 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 29 0a  onfig.pScratch).
9ab10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9ab20 20 20 5b 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c    [sqlite3Global
9ab30 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68  Config.szScratch
9ab40 2a 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  *sqlite3GlobalCo
9ab50 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 5d 3b 0a  nfig.nScratch];.
9ab60 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73      for(i=0; i<s
9ab70 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9ab80 69 67 2e 6e 53 63 72 61 74 63 68 3b 20 69 2b 2b  ig.nScratch; i++
9ab90 29 7b 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68  ){ mem0.aScratch
9aba0 46 72 65 65 5b 69 5d 20 3d 20 69 3b 20 7d 0a 20  Free[i] = i; }. 
9abb0 20 20 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68     mem0.nScratch
9abc0 46 72 65 65 20 3d 20 73 71 6c 69 74 65 33 47 6c  Free = sqlite3Gl
9abd0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61  obalConfig.nScra
9abe0 74 63 68 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  tch;.  }else{.  
9abf0 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
9ac00 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 20 3d  onfig.pScratch =
9ac10 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47   0;.    sqlite3G
9ac20 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63  lobalConfig.szSc
9ac30 72 61 74 63 68 20 3d 20 30 3b 0a 20 20 7d 0a 20  ratch = 0;.  }. 
9ac40 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
9ac50 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 26  alConfig.pPage &
9ac60 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  & sqlite3GlobalC
9ac70 6f 6e 66 69 67 2e 73 7a 50 61 67 65 3e 3d 35 31  onfig.szPage>=51
9ac80 32 0a 20 20 20 20 20 20 26 26 20 73 71 6c 69 74  2.      && sqlit
9ac90 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
9aca0 50 61 67 65 3e 3d 31 20 29 7b 0a 20 20 20 20 69  Page>=1 ){.    i
9acb0 6e 74 20 69 3b 0a 20 20 20 20 69 6e 74 20 6f 76  nt i;.    int ov
9acc0 65 72 68 65 61 64 3b 0a 20 20 20 20 69 6e 74 20  erhead;.    int 
9acd0 73 7a 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62  sz = sqlite3Glob
9ace0 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 3b  alConfig.szPage;
9acf0 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c  .    int n = sql
9ad00 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9ad10 2e 6e 50 61 67 65 3b 0a 20 20 20 20 6f 76 65 72  .nPage;.    over
9ad20 68 65 61 64 20 3d 20 28 34 2a 6e 20 2b 20 73 7a  head = (4*n + sz
9ad30 20 2d 20 31 29 2f 73 7a 3b 0a 20 20 20 20 73 71   - 1)/sz;.    sq
9ad40 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9ad50 67 2e 6e 50 61 67 65 20 2d 3d 20 6f 76 65 72 68  g.nPage -= overh
9ad60 65 61 64 3b 0a 20 20 20 20 6d 65 6d 30 2e 61 50  ead;.    mem0.aP
9ad70 61 67 65 46 72 65 65 20 3d 20 28 75 33 32 2a 29  ageFree = (u32*)
9ad80 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33  &((char*)sqlite3
9ad90 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61  GlobalConfig.pPa
9ada0 67 65 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  ge).            
9adb0 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 47 6c        [sqlite3Gl
9adc0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67  obalConfig.szPag
9add0 65 2a 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  e*sqlite3GlobalC
9ade0 6f 6e 66 69 67 2e 6e 50 61 67 65 5d 3b 0a 20 20  onfig.nPage];.  
9adf0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 71 6c    for(i=0; i<sql
9ae00 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9ae10 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 6d 65  .nPage; i++){ me
9ae20 6d 30 2e 61 50 61 67 65 46 72 65 65 5b 69 5d 20  m0.aPageFree[i] 
9ae30 3d 20 69 3b 20 7d 0a 20 20 20 20 6d 65 6d 30 2e  = i; }.    mem0.
9ae40 6e 50 61 67 65 46 72 65 65 20 3d 20 73 71 6c 69  nPageFree = sqli
9ae50 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9ae60 6e 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a  nPage;.  }else{.
9ae70 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
9ae80 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 3d 20  lConfig.pPage = 
9ae90 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  0;.    sqlite3Gl
9aea0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67  obalConfig.szPag
9aeb0 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  e = 0;.  }.  ret
9aec0 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  urn sqlite3Globa
9aed0 6c 43 6f 6e 66 69 67 2e 6d 2e 78 49 6e 69 74 28  lConfig.m.xInit(
9aee0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9aef0 66 69 67 2e 6d 2e 70 41 70 70 44 61 74 61 29 3b  fig.m.pAppData);
9af00 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74  .}../*.** Deinit
9af10 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72  ialize the memor
9af20 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62  y allocation sub
9af30 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54  system..*/.SQLIT
9af40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
9af50 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64 28  qlite3MallocEnd(
9af60 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33  void){.  sqlite3
9af70 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
9af80 53 68 75 74 64 6f 77 6e 28 73 71 6c 69 74 65 33  Shutdown(sqlite3
9af90 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 70  GlobalConfig.m.p
9afa0 41 70 70 44 61 74 61 29 3b 0a 20 20 6d 65 6d 73  AppData);.  mems
9afb0 65 74 28 26 6d 65 6d 30 2c 20 30 2c 20 73 69 7a  et(&mem0, 0, siz
9afc0 65 6f 66 28 6d 65 6d 30 29 29 3b 0a 7d 0a 0a 2f  eof(mem0));.}../
9afd0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
9afe0 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79  amount of memory
9aff0 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b   currently check
9b000 65 64 20 6f 75 74 2e 0a 2a 2f 0a 53 51 4c 49 54  ed out..*/.SQLIT
9b010 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e  E_API sqlite3_in
9b020 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f  t64 sqlite3_memo
9b030 72 79 5f 75 73 65 64 28 76 6f 69 64 29 7b 0a 20  ry_used(void){. 
9b040 20 69 6e 74 20 6e 2c 20 6d 78 3b 0a 20 20 73 71   int n, mx;.  sq
9b050 6c 69 74 65 33 5f 69 6e 74 36 34 20 72 65 73 3b  lite3_int64 res;
9b060 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75  .  sqlite3_statu
9b070 73 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  s(SQLITE_STATUS_
9b080 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 26 6e 2c  MEMORY_USED, &n,
9b090 20 26 6d 78 2c 20 30 29 3b 0a 20 20 72 65 73 20   &mx, 0);.  res 
9b0a0 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  = (sqlite3_int64
9b0b0 29 6e 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f  )n;  /* Work aro
9b0c0 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 6c 61  und bug in Borla
9b0d0 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23 33 32  nd C. Ticket #32
9b0e0 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72  16 */.  return r
9b0f0 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  es;.}../*.** Ret
9b100 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  urn the maximum 
9b110 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79  amount of memory
9b120 20 74 68 61 74 20 68 61 73 20 65 76 65 72 20 62   that has ever b
9b130 65 65 6e 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f  een.** checked o
9b140 75 74 20 73 69 6e 63 65 20 65 69 74 68 65 72 20  ut since either 
9b150 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
9b160 20 74 68 69 73 20 70 72 6f 63 65 73 73 0a 2a 2a   this process.**
9b170 20 6f 72 20 73 69 6e 63 65 20 74 68 65 20 6d 6f   or since the mo
9b180 73 74 20 72 65 63 65 6e 74 20 72 65 73 65 74 2e  st recent reset.
9b190 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73  .*/.SQLITE_API s
9b1a0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c  qlite3_int64 sql
9b1b0 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68  ite3_memory_high
9b1c0 77 61 74 65 72 28 69 6e 74 20 72 65 73 65 74 46  water(int resetF
9b1d0 6c 61 67 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6d  lag){.  int n, m
9b1e0 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  x;.  sqlite3_int
9b1f0 36 34 20 72 65 73 3b 0a 20 20 73 71 6c 69 74 65  64 res;.  sqlite
9b200 33 5f 73 74 61 74 75 73 28 53 51 4c 49 54 45 5f  3_status(SQLITE_
9b210 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53  STATUS_MEMORY_US
9b220 45 44 2c 20 26 6e 2c 20 26 6d 78 2c 20 72 65 73  ED, &n, &mx, res
9b230 65 74 46 6c 61 67 29 3b 0a 20 20 72 65 73 20 3d  etFlag);.  res =
9b240 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29   (sqlite3_int64)
9b250 6d 78 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f  mx;  /* Work aro
9b260 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 6c 61  und bug in Borla
9b270 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23 33 32  nd C. Ticket #32
9b280 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72  16 */.  return r
9b290 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61  es;.}../*.** Cha
9b2a0 6e 67 65 20 74 68 65 20 61 6c 61 72 6d 20 63 61  nge the alarm ca
9b2b0 6c 6c 62 61 63 6b 0a 2a 2f 0a 53 51 4c 49 54 45  llback.*/.SQLITE
9b2c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
9b2d0 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28  ite3MemoryAlarm(
9b2e0 0a 20 20 76 6f 69 64 28 2a 78 43 61 6c 6c 62 61  .  void(*xCallba
9b2f0 63 6b 29 28 76 6f 69 64 20 2a 70 41 72 67 2c 20  ck)(void *pArg, 
9b300 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 75 73  sqlite3_int64 us
9b310 65 64 2c 69 6e 74 20 4e 29 2c 0a 20 20 76 6f 69  ed,int N),.  voi
9b320 64 20 2a 70 41 72 67 2c 0a 20 20 73 71 6c 69 74  d *pArg,.  sqlit
9b330 65 33 5f 69 6e 74 36 34 20 69 54 68 72 65 73 68  e3_int64 iThresh
9b340 6f 6c 64 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33  old.){.  sqlite3
9b350 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d  _mutex_enter(mem
9b360 30 2e 6d 75 74 65 78 29 3b 0a 20 20 6d 65 6d 30  0.mutex);.  mem0
9b370 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 3d  .alarmCallback =
9b380 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6d 65   xCallback;.  me
9b390 6d 30 2e 61 6c 61 72 6d 41 72 67 20 3d 20 70 41  m0.alarmArg = pA
9b3a0 72 67 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d  rg;.  mem0.alarm
9b3b0 54 68 72 65 73 68 6f 6c 64 20 3d 20 69 54 68 72  Threshold = iThr
9b3c0 65 73 68 6f 6c 64 3b 0a 20 20 73 71 6c 69 74 65  eshold;.  sqlite
9b3d0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
9b3e0 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  m0.mutex);.  ret
9b3f0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
9b400 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
9b410 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
9b420 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 61 74 65  ./*.** Deprecate
9b430 64 20 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 72  d external inter
9b440 66 61 63 65 2e 20 20 49 6e 74 65 72 6e 61 6c 2f  face.  Internal/
9b450 63 6f 72 65 20 53 51 4c 69 74 65 20 63 6f 64 65  core SQLite code
9b460 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20  .** should call 
9b470 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61  sqlite3MemoryAla
9b480 72 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  rm..*/.SQLITE_AP
9b490 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65  I int sqlite3_me
9b4a0 6d 6f 72 79 5f 61 6c 61 72 6d 28 0a 20 20 76 6f  mory_alarm(.  vo
9b4b0 69 64 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76  id(*xCallback)(v
9b4c0 6f 69 64 20 2a 70 41 72 67 2c 20 73 71 6c 69 74  oid *pArg, sqlit
9b4d0 65 33 5f 69 6e 74 36 34 20 75 73 65 64 2c 69 6e  e3_int64 used,in
9b4e0 74 20 4e 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41  t N),.  void *pA
9b4f0 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  rg,.  sqlite3_in
9b500 74 36 34 20 69 54 68 72 65 73 68 6f 6c 64 0a 29  t64 iThreshold.)
9b510 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
9b520 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 78 43  e3MemoryAlarm(xC
9b530 61 6c 6c 62 61 63 6b 2c 20 70 41 72 67 2c 20 69  allback, pArg, i
9b540 54 68 72 65 73 68 6f 6c 64 29 3b 0a 7d 0a 23 65  Threshold);.}.#e
9b550 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 72 69 67  ndif../*.** Trig
9b560 67 65 72 20 74 68 65 20 61 6c 61 72 6d 20 0a 2a  ger the alarm .*
9b570 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  /.static void sq
9b580 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d  lite3MallocAlarm
9b590 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 76  (int nByte){.  v
9b5a0 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29  oid (*xCallback)
9b5b0 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69  (void*,sqlite3_i
9b5c0 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 20 73 71 6c  nt64,int);.  sql
9b5d0 69 74 65 33 5f 69 6e 74 36 34 20 6e 6f 77 55 73  ite3_int64 nowUs
9b5e0 65 64 3b 0a 20 20 76 6f 69 64 20 2a 70 41 72 67  ed;.  void *pArg
9b5f0 3b 0a 20 20 69 66 28 20 6d 65 6d 30 2e 61 6c 61  ;.  if( mem0.ala
9b600 72 6d 43 61 6c 6c 62 61 63 6b 3d 3d 30 20 7c 7c  rmCallback==0 ||
9b610 20 6d 65 6d 30 2e 61 6c 61 72 6d 42 75 73 79 20   mem0.alarmBusy 
9b620 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d   ) return;.  mem
9b630 30 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20 31 3b  0.alarmBusy = 1;
9b640 0a 20 20 78 43 61 6c 6c 62 61 63 6b 20 3d 20 6d  .  xCallback = m
9b650 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63  em0.alarmCallbac
9b660 6b 3b 0a 20 20 6e 6f 77 55 73 65 64 20 3d 20 73  k;.  nowUsed = s
9b670 71 6c 69 74 65 33 53 74 61 74 75 73 56 61 6c 75  qlite3StatusValu
9b680 65 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  e(SQLITE_STATUS_
9b690 4d 45 4d 4f 52 59 5f 55 53 45 44 29 3b 0a 20 20  MEMORY_USED);.  
9b6a0 70 41 72 67 20 3d 20 6d 65 6d 30 2e 61 6c 61 72  pArg = mem0.alar
9b6b0 6d 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f  mArg;.  sqlite3_
9b6c0 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30  mutex_leave(mem0
9b6d0 2e 6d 75 74 65 78 29 3b 0a 20 20 78 43 61 6c 6c  .mutex);.  xCall
9b6e0 62 61 63 6b 28 70 41 72 67 2c 20 6e 6f 77 55 73  back(pArg, nowUs
9b6f0 65 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 73 71  ed, nByte);.  sq
9b700 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
9b710 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20  r(mem0.mutex);. 
9b720 20 6d 65 6d 30 2e 61 6c 61 72 6d 42 75 73 79 20   mem0.alarmBusy 
9b730 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f  = 0;.}../*.** Do
9b740 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
9b750 74 69 6f 6e 20 77 69 74 68 20 73 74 61 74 69 73  tion with statis
9b760 74 69 63 73 20 61 6e 64 20 61 6c 61 72 6d 73 2e  tics and alarms.
9b770 20 20 41 73 73 75 6d 65 20 74 68 65 0a 2a 2a 20    Assume the.** 
9b780 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20  lock is already 
9b790 68 65 6c 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  held..*/.static 
9b7a0 69 6e 74 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c  int mallocWithAl
9b7b0 61 72 6d 28 69 6e 74 20 6e 2c 20 76 6f 69 64 20  arm(int n, void 
9b7c0 2a 2a 70 70 29 7b 0a 20 20 69 6e 74 20 6e 46 75  **pp){.  int nFu
9b7d0 6c 6c 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20  ll;.  void *p;. 
9b7e0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
9b7f0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 30  _mutex_held(mem0
9b800 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6e 46 75  .mutex) );.  nFu
9b810 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62  ll = sqlite3Glob
9b820 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e  alConfig.m.xRoun
9b830 64 75 70 28 6e 29 3b 0a 20 20 73 71 6c 69 74 65  dup(n);.  sqlite
9b840 33 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54  3StatusSet(SQLIT
9b850 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f  E_STATUS_MALLOC_
9b860 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 69 66 28 20  SIZE, n);.  if( 
9b870 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61  mem0.alarmCallba
9b880 63 6b 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  ck!=0 ){.    int
9b890 20 6e 55 73 65 64 20 3d 20 73 71 6c 69 74 65 33   nUsed = sqlite3
9b8a0 53 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c 49  StatusValue(SQLI
9b8b0 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59  TE_STATUS_MEMORY
9b8c0 5f 55 53 45 44 29 3b 0a 20 20 20 20 69 66 28 20  _USED);.    if( 
9b8d0 6e 55 73 65 64 2b 6e 46 75 6c 6c 20 3e 3d 20 6d  nUsed+nFull >= m
9b8e0 65 6d 30 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f  em0.alarmThresho
9b8f0 6c 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ld ){.      sqli
9b900 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e  te3MallocAlarm(n
9b910 46 75 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Full);.    }.  }
9b920 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c  .  p = sqlite3Gl
9b930 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61  obalConfig.m.xMa
9b940 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b 0a 20 20 69  lloc(nFull);.  i
9b950 66 28 20 70 3d 3d 30 20 26 26 20 6d 65 6d 30 2e  f( p==0 && mem0.
9b960 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 29 7b  alarmCallback ){
9b970 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c  .    sqlite3Mall
9b980 6f 63 41 6c 61 72 6d 28 6e 46 75 6c 6c 29 3b 0a  ocAlarm(nFull);.
9b990 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47      p = sqlite3G
9b9a0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d  lobalConfig.m.xM
9b9b0 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b 0a 20 20  alloc(nFull);.  
9b9c0 7d 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  }.  if( p ){.   
9b9d0 20 6e 46 75 6c 6c 20 3d 20 73 71 6c 69 74 65 33   nFull = sqlite3
9b9e0 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20  MallocSize(p);. 
9b9f0 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73     sqlite3Status
9ba00 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55  Add(SQLITE_STATU
9ba10 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 6e  S_MEMORY_USED, n
9ba20 46 75 6c 6c 29 3b 0a 20 20 7d 0a 20 20 2a 70 70  Full);.  }.  *pp
9ba30 20 3d 20 70 3b 0a 20 20 72 65 74 75 72 6e 20 6e   = p;.  return n
9ba40 46 75 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  Full;.}../*.** A
9ba50 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 2e 20  llocate memory. 
9ba60 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
9ba70 20 6c 69 6b 65 20 73 71 6c 69 74 65 33 5f 6d 61   like sqlite3_ma
9ba80 6c 6c 6f 63 28 29 20 65 78 63 65 70 74 20 74 68  lloc() except th
9ba90 61 74 20 69 74 0a 2a 2a 20 61 73 73 75 6d 65 73  at it.** assumes
9baa0 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 75 62 73   the memory subs
9bab0 79 73 74 65 6d 20 68 61 73 20 61 6c 72 65 61 64  ystem has alread
9bac0 79 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a  y been initializ
9bad0 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
9bae0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
9baf0 74 65 33 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 29  te3Malloc(int n)
9bb00 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 69  {.  void *p;.  i
9bb10 66 28 20 6e 3c 3d 30 20 29 7b 0a 20 20 20 20 70  f( n<=0 ){.    p
9bb20 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66   = 0;.  }else if
9bb30 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
9bb40 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29  onfig.bMemstat )
9bb50 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  {.    sqlite3_mu
9bb60 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d  tex_enter(mem0.m
9bb70 75 74 65 78 29 3b 0a 20 20 20 20 6d 61 6c 6c 6f  utex);.    mallo
9bb80 63 57 69 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70  cWithAlarm(n, &p
9bb90 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
9bba0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e  utex_leave(mem0.
9bbb0 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b  mutex);.  }else{
9bbc0 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
9bbd0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
9bbe0 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 20  Malloc(n);.  }. 
9bbf0 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
9bc00 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e  .** This version
9bc10 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   of the memory a
9bc20 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 6f 72  llocation is for
9bc30 20 75 73 65 20 62 79 20 74 68 65 20 61 70 70 6c   use by the appl
9bc40 69 63 61 74 69 6f 6e 2e 0a 2a 2a 20 46 69 72 73  ication..** Firs
9bc50 74 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20  t make sure the 
9bc60 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d  memory subsystem
9bc70 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c   is initialized,
9bc80 20 74 68 65 6e 20 64 6f 20 74 68 65 0a 2a 2a 20   then do the.** 
9bc90 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53  allocation..*/.S
9bca0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
9bcb0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69  sqlite3_malloc(i
9bcc0 6e 74 20 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53  nt n){.#ifndef S
9bcd0 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
9bce0 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65  NIT.  if( sqlite
9bcf0 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29  3_initialize() )
9bd00 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69   return 0;.#endi
9bd10 66 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  f.  return sqlit
9bd20 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 7d 0a 0a  e3Malloc(n);.}..
9bd30 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 68 72 65 61  /*.** Each threa
9bd40 64 20 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20  d may only have 
9bd50 61 20 73 69 6e 67 6c 65 20 6f 75 74 73 74 61 6e  a single outstan
9bd60 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  ding allocation 
9bd70 66 72 6f 6d 0a 2a 2a 20 78 53 63 72 61 74 63 68  from.** xScratch
9bd80 4d 61 6c 6c 6f 63 28 29 2e 20 20 57 65 20 76 65  Malloc().  We ve
9bd90 72 69 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72  rify this constr
9bda0 61 69 6e 74 20 69 6e 20 74 68 65 20 73 69 6e 67  aint in the sing
9bdb0 6c 65 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 63  le-threaded.** c
9bdc0 61 73 65 20 62 79 20 73 65 74 74 69 6e 67 20 73  ase by setting s
9bdd0 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 74  cratchAllocOut t
9bde0 6f 20 31 20 77 68 65 6e 20 61 6e 20 61 6c 6c 6f  o 1 when an allo
9bdf0 63 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 6f 75 74  cation.** is out
9be00 73 74 61 6e 64 69 6e 67 20 63 6c 65 61 72 69 6e  standing clearin
9be10 67 20 69 74 20 77 68 65 6e 20 74 68 65 20 61 6c  g it when the al
9be20 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65  location is free
9be30 64 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  d..*/.#if SQLITE
9be40 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26  _THREADSAFE==0 &
9be50 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55  & !defined(NDEBU
9be60 47 29 0a 73 74 61 74 69 63 20 69 6e 74 20 73 63  G).static int sc
9be70 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20  ratchAllocOut = 
9be80 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  0;.#endif.../*.*
9be90 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72  * Allocate memor
9bea0 79 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20  y that is to be 
9beb0 75 73 65 64 20 61 6e 64 20 72 65 6c 65 61 73 65  used and release
9bec0 64 20 72 69 67 68 74 20 61 77 61 79 2e 0a 2a 2a  d right away..**
9bed0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
9bee0 20 73 69 6d 69 6c 61 72 20 74 6f 20 61 6c 6c 6f   similar to allo
9bef0 63 61 28 29 20 69 6e 20 74 68 61 74 20 69 74 20  ca() in that it 
9bf00 69 73 20 6e 6f 74 20 69 6e 74 65 6e 64 65 64 0a  is not intended.
9bf10 2a 2a 20 66 6f 72 20 73 69 74 75 61 74 69 6f 6e  ** for situation
9bf20 73 20 77 68 65 72 65 20 74 68 65 20 6d 65 6d 6f  s where the memo
9bf30 72 79 20 6d 69 67 68 74 20 62 65 20 68 65 6c 64  ry might be held
9bf40 20 6c 6f 6e 67 2d 74 65 72 6d 2e 20 20 54 68 69   long-term.  Thi
9bf50 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20  s.** routine is 
9bf60 69 6e 74 65 6e 64 65 64 20 74 6f 20 67 65 74 20  intended to get 
9bf70 6d 65 6d 6f 72 79 20 74 6f 20 6f 6c 64 20 6c 61  memory to old la
9bf80 72 67 65 20 74 72 61 6e 73 69 65 6e 74 20 64 61  rge transient da
9bf90 74 61 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73  ta.** structures
9bfa0 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 6f 74 20   that would not 
9bfb0 6e 6f 72 6d 61 6c 6c 79 20 66 69 74 20 6f 6e 20  normally fit on 
9bfc0 74 68 65 20 73 74 61 63 6b 20 6f 66 20 61 6e 0a  the stack of an.
9bfd0 2a 2a 20 65 6d 62 65 64 64 65 64 20 70 72 6f 63  ** embedded proc
9bfe0 65 73 73 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  essor..*/.SQLITE
9bff0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
9c000 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c  qlite3ScratchMal
9c010 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f  loc(int n){.  vo
9c020 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28  id *p;.  assert(
9c030 20 6e 3e 30 20 29 3b 0a 0a 23 69 66 20 53 51 4c   n>0 );..#if SQL
9c040 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d  ITE_THREADSAFE==
9c050 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44  0 && !defined(ND
9c060 45 42 55 47 29 0a 20 20 2f 2a 20 56 65 72 69 66  EBUG).  /* Verif
9c070 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74  y that no more t
9c080 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 68 20  han one scratch 
9c090 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72 20 74  allocation per t
9c0a0 68 72 65 61 64 0a 20 20 2a 2a 20 69 73 20 6f 75  hread.  ** is ou
9c0b0 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65  tstanding at one
9c0c0 20 74 69 6d 65 2e 20 20 28 54 68 69 73 20 69 73   time.  (This is
9c0d0 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 6e   only checked in
9c0e0 20 74 68 65 0a 20 20 2a 2a 20 73 69 6e 67 6c 65   the.  ** single
9c0f0 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 20 73  -threaded case s
9c100 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 20 69 6e  ince checking in
9c110 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61   the multi-threa
9c120 64 65 64 20 63 61 73 65 0a 20 20 2a 2a 20 77 6f  ded case.  ** wo
9c130 75 6c 64 20 62 65 20 6d 75 63 68 20 6d 6f 72 65  uld be much more
9c140 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 29 20 2a   complicated.) *
9c150 2f 0a 20 20 61 73 73 65 72 74 28 20 73 63 72 61  /.  assert( scra
9c160 74 63 68 41 6c 6c 6f 63 4f 75 74 3d 3d 30 20 29  tchAllocOut==0 )
9c170 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20  ;.#endif..  if( 
9c180 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9c190 66 69 67 2e 73 7a 53 63 72 61 74 63 68 3c 6e 20  fig.szScratch<n 
9c1a0 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 63 72 61  ){.    goto scra
9c1b0 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 20  tch_overflow;.  
9c1c0 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20 73 71 6c  }else{  .    sql
9c1d0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
9c1e0 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem0.mutex);.  
9c1f0 20 20 69 66 28 20 6d 65 6d 30 2e 6e 53 63 72 61    if( mem0.nScra
9c200 74 63 68 46 72 65 65 3d 3d 30 20 29 7b 0a 20 20  tchFree==0 ){.  
9c210 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
9c220 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74  x_leave(mem0.mut
9c230 65 78 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ex);.      goto 
9c240 73 63 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77  scratch_overflow
9c250 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
9c260 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
9c270 69 20 3d 20 6d 65 6d 30 2e 61 53 63 72 61 74 63  i = mem0.aScratc
9c280 68 46 72 65 65 5b 2d 2d 6d 65 6d 30 2e 6e 53 63  hFree[--mem0.nSc
9c290 72 61 74 63 68 46 72 65 65 5d 3b 0a 20 20 20 20  ratchFree];.    
9c2a0 20 20 69 20 2a 3d 20 73 71 6c 69 74 65 33 47 6c    i *= sqlite3Gl
9c2b0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72  obalConfig.szScr
9c2c0 61 74 63 68 3b 0a 20 20 20 20 20 20 73 71 6c 69  atch;.      sqli
9c2d0 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c  te3StatusAdd(SQL
9c2e0 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54  ITE_STATUS_SCRAT
9c2f0 43 48 5f 55 53 45 44 2c 20 31 29 3b 0a 20 20 20  CH_USED, 1);.   
9c300 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73     sqlite3Status
9c310 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55  Set(SQLITE_STATU
9c320 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 2c 20  S_SCRATCH_SIZE, 
9c330 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  n);.      sqlite
9c340 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
9c350 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20  m0.mutex);.     
9c360 20 70 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 63   p = (void*)&((c
9c370 68 61 72 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62  har*)sqlite3Glob
9c380 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63  alConfig.pScratc
9c390 68 29 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d  h)[i];.    }.  }
9c3a0 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
9c3b0 41 44 53 41 46 45 3d 3d 30 20 26 26 20 21 64 65  ADSAFE==0 && !de
9c3c0 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 20 20  fined(NDEBUG).  
9c3d0 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20  scratchAllocOut 
9c3e0 3d 20 70 21 3d 30 3b 0a 23 65 6e 64 69 66 0a 0a  = p!=0;.#endif..
9c3f0 20 20 72 65 74 75 72 6e 20 70 3b 0a 0a 73 63 72    return p;..scr
9c400 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3a 0a 20  atch_overflow:. 
9c410 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
9c420 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61  alConfig.bMemsta
9c430 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
9c440 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d  _mutex_enter(mem
9c450 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71  0.mutex);.    sq
9c460 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53  lite3StatusSet(S
9c470 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52  QLITE_STATUS_SCR
9c480 41 54 43 48 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20  ATCH_SIZE, n);. 
9c490 20 20 20 6e 20 3d 20 6d 61 6c 6c 6f 63 57 69 74     n = mallocWit
9c4a0 68 41 6c 61 72 6d 28 6e 2c 20 26 70 29 3b 0a 20  hAlarm(n, &p);. 
9c4b0 20 20 20 69 66 28 20 70 20 29 20 73 71 6c 69 74     if( p ) sqlit
9c4c0 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49  e3StatusAdd(SQLI
9c4d0 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43  TE_STATUS_SCRATC
9c4e0 48 5f 4f 56 45 52 46 4c 4f 57 2c 20 6e 29 3b 0a  H_OVERFLOW, n);.
9c4f0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
9c500 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74  x_leave(mem0.mut
9c510 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ex);.  }else{.  
9c520 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f    p = sqlite3Glo
9c530 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c  balConfig.m.xMal
9c540 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 23 69 66 20  loc(n);.  }.#if 
9c550 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
9c560 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64  E==0 && !defined
9c570 28 4e 44 45 42 55 47 29 0a 20 20 73 63 72 61 74  (NDEBUG).  scrat
9c580 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 70 21 3d  chAllocOut = p!=
9c590 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75  0;.#endif.  retu
9c5a0 72 6e 20 70 3b 20 20 20 20 0a 7d 0a 53 51 4c 49  rn p;    .}.SQLI
9c5b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
9c5c0 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 46 72  sqlite3ScratchFr
9c5d0 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69  ee(void *p){.  i
9c5e0 66 28 20 70 20 29 7b 0a 0a 23 69 66 20 53 51 4c  f( p ){..#if SQL
9c5f0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d  ITE_THREADSAFE==
9c600 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44  0 && !defined(ND
9c610 45 42 55 47 29 0a 20 20 20 20 2f 2a 20 56 65 72  EBUG).    /* Ver
9c620 69 66 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65  ify that no more
9c630 20 74 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63   than one scratc
9c640 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72  h allocation per
9c650 20 74 68 72 65 61 64 0a 20 20 20 20 2a 2a 20 69   thread.    ** i
9c660 73 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74  s outstanding at
9c670 20 6f 6e 65 20 74 69 6d 65 2e 20 20 28 54 68 69   one time.  (Thi
9c680 73 20 69 73 20 6f 6e 6c 79 20 63 68 65 63 6b 65  s is only checke
9c690 64 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20  d in the.    ** 
9c6a0 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20  single-threaded 
9c6b0 63 61 73 65 20 73 69 6e 63 65 20 63 68 65 63 6b  case since check
9c6c0 69 6e 67 20 69 6e 20 74 68 65 20 6d 75 6c 74 69  ing in the multi
9c6d0 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 0a 20  -threaded case. 
9c6e0 20 20 20 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6d     ** would be m
9c6f0 75 63 68 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63  uch more complic
9c700 61 74 65 64 2e 29 20 2a 2f 0a 20 20 20 20 61 73  ated.) */.    as
9c710 73 65 72 74 28 20 73 63 72 61 74 63 68 41 6c 6c  sert( scratchAll
9c720 6f 63 4f 75 74 3d 3d 31 20 29 3b 0a 20 20 20 20  ocOut==1 );.    
9c730 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20  scratchAllocOut 
9c740 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20  = 0;.#endif..   
9c750 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
9c760 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63  alConfig.pScratc
9c770 68 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20  h==0.           
9c780 7c 7c 20 70 3c 73 71 6c 69 74 65 33 47 6c 6f 62  || p<sqlite3Glob
9c790 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63  alConfig.pScratc
9c7a0 68 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  h.           || 
9c7b0 70 3e 3d 28 76 6f 69 64 2a 29 6d 65 6d 30 2e 61  p>=(void*)mem0.a
9c7c0 53 63 72 61 74 63 68 46 72 65 65 20 29 7b 0a 20  ScratchFree ){. 
9c7d0 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
9c7e0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65  GlobalConfig.bMe
9c7f0 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 20 20 20  mstat ){.       
9c800 20 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 71 6c   int iSize = sql
9c810 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70  ite3MallocSize(p
9c820 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
9c830 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
9c840 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20  em0.mutex);.    
9c850 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75      sqlite3Statu
9c860 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54  sAdd(SQLITE_STAT
9c870 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 46  US_SCRATCH_OVERF
9c880 4c 4f 57 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20  LOW, -iSize);.  
9c890 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61        sqlite3Sta
9c8a0 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
9c8b0 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44  ATUS_MEMORY_USED
9c8c0 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 20  , -iSize);.     
9c8d0 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
9c8e0 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70  Config.m.xFree(p
9c8f0 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
9c900 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
9c910 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20  em0.mutex);.    
9c920 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
9c930 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
9c940 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b  nfig.m.xFree(p);
9c950 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
9c960 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b  se{.      int i;
9c970 0a 20 20 20 20 20 20 69 20 3d 20 28 75 38 20 2a  .      i = (u8 *
9c980 29 70 20 2d 20 28 75 38 20 2a 29 73 71 6c 69 74  )p - (u8 *)sqlit
9c990 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
9c9a0 53 63 72 61 74 63 68 3b 0a 20 20 20 20 20 20 69  Scratch;.      i
9c9b0 20 2f 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61   /= sqlite3Globa
9c9c0 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63  lConfig.szScratc
9c9d0 68 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  h;.      assert(
9c9e0 20 69 3e 3d 30 20 26 26 20 69 3c 73 71 6c 69 74   i>=0 && i<sqlit
9c9f0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
9ca00 53 63 72 61 74 63 68 20 29 3b 0a 20 20 20 20 20  Scratch );.     
9ca10 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
9ca20 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29  nter(mem0.mutex)
9ca30 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
9ca40 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65  mem0.nScratchFre
9ca50 65 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  e<sqlite3GlobalC
9ca60 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 20 29  onfig.nScratch )
9ca70 3b 0a 20 20 20 20 20 20 6d 65 6d 30 2e 61 53 63  ;.      mem0.aSc
9ca80 72 61 74 63 68 46 72 65 65 5b 6d 65 6d 30 2e 6e  ratchFree[mem0.n
9ca90 53 63 72 61 74 63 68 46 72 65 65 2b 2b 5d 20 3d  ScratchFree++] =
9caa0 20 69 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   i;.      sqlite
9cab0 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54  3StatusAdd(SQLIT
9cac0 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48  E_STATUS_SCRATCH
9cad0 5f 55 53 45 44 2c 20 2d 31 29 3b 0a 20 20 20 20  _USED, -1);.    
9cae0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
9caf0 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78  leave(mem0.mutex
9cb00 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  );.    }.  }.}..
9cb10 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6d  /*.** Allocate m
9cb20 65 6d 6f 72 79 20 74 6f 20 62 65 20 75 73 65 64  emory to be used
9cb30 20 62 79 20 74 68 65 20 70 61 67 65 20 63 61 63   by the page cac
9cb40 68 65 2e 20 20 4d 61 6b 65 20 75 73 65 20 6f 66  he.  Make use of
9cb50 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 62   the.** memory b
9cb60 75 66 66 65 72 20 70 72 6f 76 69 64 65 64 20 62  uffer provided b
9cb70 79 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  y SQLITE_CONFIG_
9cb80 50 41 47 45 43 41 43 48 45 20 69 66 20 74 68 65  PAGECACHE if the
9cb90 72 65 20 69 73 20 6f 6e 65 0a 2a 2a 20 61 6e 64  re is one.** and
9cba0 20 74 68 61 74 20 6d 65 6d 6f 72 79 20 69 73 20   that memory is 
9cbb0 6f 66 20 74 68 65 20 72 69 67 68 74 20 73 69 7a  of the right siz
9cbc0 65 20 61 6e 64 20 69 73 20 6e 6f 74 20 63 6f 6d  e and is not com
9cbd0 70 6c 65 74 65 6c 79 0a 2a 2a 20 63 6f 6e 73 75  pletely.** consu
9cbe0 6d 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65 2c  med.  Otherwise,
9cbf0 20 66 61 69 6c 6f 76 65 72 20 74 6f 20 73 71 6c   failover to sql
9cc00 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f  ite3Malloc()..*/
9cc10 0a 23 69 66 20 30 0a 53 51 4c 49 54 45 5f 50 52  .#if 0.SQLITE_PR
9cc20 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
9cc30 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e  te3PageMalloc(in
9cc40 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b  t n){.  void *p;
9cc50 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29  .  assert( n>0 )
9cc60 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6e 20 26  ;.  assert( (n &
9cc70 20 28 6e 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20   (n-1))==0 );.  
9cc80 61 73 73 65 72 74 28 20 6e 3e 3d 35 31 32 20 26  assert( n>=512 &
9cc90 26 20 6e 3c 3d 33 32 37 36 38 20 29 3b 0a 0a 20  & n<=32768 );.. 
9cca0 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
9ccb0 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 3c  alConfig.szPage<
9ccc0 6e 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 70 61  n ){.    goto pa
9ccd0 67 65 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 20 7d  ge_overflow;.  }
9cce0 65 6c 73 65 7b 20 20 0a 20 20 20 20 73 71 6c 69  else{  .    sqli
9ccf0 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
9cd00 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20  mem0.mutex);.   
9cd10 20 69 66 28 20 6d 65 6d 30 2e 6e 50 61 67 65 46   if( mem0.nPageF
9cd20 72 65 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ree==0 ){.      
9cd30 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
9cd40 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b  ave(mem0.mutex);
9cd50 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65  .      goto page
9cd60 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 7d  _overflow;.    }
9cd70 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20  else{.      int 
9cd80 69 3b 0a 20 20 20 20 20 20 69 20 3d 20 6d 65 6d  i;.      i = mem
9cd90 30 2e 61 50 61 67 65 46 72 65 65 5b 2d 2d 6d 65  0.aPageFree[--me
9cda0 6d 30 2e 6e 50 61 67 65 46 72 65 65 5d 3b 0a 20  m0.nPageFree];. 
9cdb0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
9cdc0 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75  ex_leave(mem0.mu
9cdd0 74 65 78 29 3b 0a 20 20 20 20 20 20 69 20 2a 3d  tex);.      i *=
9cde0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
9cdf0 6e 66 69 67 2e 73 7a 50 61 67 65 3b 0a 20 20 20  nfig.szPage;.   
9ce00 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73     sqlite3Status
9ce10 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55  Set(SQLITE_STATU
9ce20 53 5f 50 41 47 45 43 41 43 48 45 5f 53 49 5a 45  S_PAGECACHE_SIZE
9ce30 2c 20 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , n);.      sqli
9ce40 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c  te3StatusAdd(SQL
9ce50 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43  ITE_STATUS_PAGEC
9ce60 41 43 48 45 5f 55 53 45 44 2c 20 31 29 3b 0a 20  ACHE_USED, 1);. 
9ce70 20 20 20 20 20 70 20 3d 20 28 76 6f 69 64 2a 29       p = (void*)
9ce80 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33  &((char*)sqlite3
9ce90 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61  GlobalConfig.pPa
9cea0 67 65 29 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20  ge)[i];.    }.  
9ceb0 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 0a 70  }.  return p;..p
9cec0 61 67 65 5f 6f 76 65 72 66 6c 6f 77 3a 0a 20 20  age_overflow:.  
9ced0 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
9cee0 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74  lConfig.bMemstat
9cef0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
9cf00 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30  mutex_enter(mem0
9cf10 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c  .mutex);.    sql
9cf20 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51  ite3StatusSet(SQ
9cf30 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45  LITE_STATUS_PAGE
9cf40 43 41 43 48 45 5f 53 49 5a 45 2c 20 6e 29 3b 0a  CACHE_SIZE, n);.
9cf50 20 20 20 20 6e 20 3d 20 6d 61 6c 6c 6f 63 57 69      n = mallocWi
9cf60 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70 29 3b 0a  thAlarm(n, &p);.
9cf70 20 20 20 20 69 66 28 20 70 20 29 20 73 71 6c 69      if( p ) sqli
9cf80 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c  te3StatusAdd(SQL
9cf90 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43  ITE_STATUS_PAGEC
9cfa0 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 2c 20 6e  ACHE_OVERFLOW, n
9cfb0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
9cfc0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e  utex_leave(mem0.
9cfd0 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b  mutex);.  }else{
9cfe0 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
9cff0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
9d000 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 20  Malloc(n);.  }. 
9d010 20 72 65 74 75 72 6e 20 70 3b 20 20 20 20 0a 7d   return p;    .}
9d020 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
9d030 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
9d040 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20  Free(void *p){. 
9d050 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 66   if( p ){.    if
9d060 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
9d070 6f 6e 66 69 67 2e 70 50 61 67 65 3d 3d 30 0a 20  onfig.pPage==0. 
9d080 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 3c 73            || p<s
9d090 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9d0a0 69 67 2e 70 50 61 67 65 0a 20 20 20 20 20 20 20  ig.pPage.       
9d0b0 20 20 20 20 7c 7c 20 70 3e 3d 28 76 6f 69 64 2a      || p>=(void*
9d0c0 29 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 65 20  )mem0.aPageFree 
9d0d0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20 74  ){.      /* In t
9d0e0 68 69 73 20 63 61 73 65 2c 20 74 68 65 20 70 61  his case, the pa
9d0f0 67 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61  ge allocation wa
9d100 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  s obtained from 
9d110 61 20 72 65 67 75 6c 61 72 20 0a 20 20 20 20 20  a regular .     
9d120 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69   ** call to sqli
9d130 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2e  te3_mem_methods.
9d140 78 4d 61 6c 6c 6f 63 28 29 20 28 61 20 70 61 67  xMalloc() (a pag
9d150 65 2d 63 61 63 68 65 2d 6d 65 6d 6f 72 79 20 0a  e-cache-memory .
9d160 20 20 20 20 20 20 2a 2a 20 22 6f 76 65 72 66 6c        ** "overfl
9d170 6f 77 22 29 2e 20 46 72 65 65 20 74 68 65 20 62  ow"). Free the b
9d180 6c 6f 63 6b 20 77 69 74 68 20 73 71 6c 69 74 65  lock with sqlite
9d190 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2e 78 46  3_mem_methods.xF
9d1a0 72 65 65 28 29 2e 0a 20 20 20 20 20 20 2a 2f 0a  ree()..      */.
9d1b0 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
9d1c0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d  3GlobalConfig.bM
9d1d0 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 20 20  emstat ){.      
9d1e0 20 20 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 71    int iSize = sq
9d1f0 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28  lite3MallocSize(
9d200 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  p);.        sqli
9d210 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
9d220 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20  mem0.mutex);.   
9d230 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74       sqlite3Stat
9d240 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41  usAdd(SQLITE_STA
9d250 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56  TUS_PAGECACHE_OV
9d260 45 52 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 29 3b  ERFLOW, -iSize);
9d270 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
9d280 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45  StatusAdd(SQLITE
9d290 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55  _STATUS_MEMORY_U
9d2a0 53 45 44 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20  SED, -iSize);.  
9d2b0 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f        sqlite3Glo
9d2c0 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65  balConfig.m.xFre
9d2d0 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71  e(p);.        sq
9d2e0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
9d2f0 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20  e(mem0.mutex);. 
9d300 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
9d310 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
9d320 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28  lConfig.m.xFree(
9d330 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  p);.      }.    
9d340 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20  }else{.      /* 
9d350 54 68 65 20 70 61 67 65 20 61 6c 6c 6f 63 61 74  The page allocat
9d360 69 6f 6e 20 77 61 73 20 61 6c 6c 6f 63 61 74 65  ion was allocate
9d370 64 20 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 74  d from the sqlit
9d380 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
9d390 50 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62 75  Page.      ** bu
9d3a0 66 66 65 72 2e 20 49 6e 20 74 68 69 73 20 63 61  ffer. In this ca
9d3b0 73 65 20 61 6c 6c 20 74 68 61 74 20 69 73 20 61  se all that is a
9d3c0 64 64 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  dd the index of 
9d3d0 74 68 65 20 70 61 67 65 20 69 6e 0a 20 20 20 20  the page in.    
9d3e0 20 20 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33    ** the sqlite3
9d3f0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61  GlobalConfig.pPa
9d400 67 65 20 61 72 72 61 79 20 74 6f 20 74 68 65 20  ge array to the 
9d410 73 65 74 20 6f 66 20 66 72 65 65 20 69 6e 64 65  set of free inde
9d420 78 65 73 20 73 74 6f 72 65 64 0a 20 20 20 20 20  xes stored.     
9d430 20 2a 2a 20 69 6e 20 74 68 65 20 6d 65 6d 30 2e   ** in the mem0.
9d440 61 50 61 67 65 46 72 65 65 5b 5d 20 61 72 72 61  aPageFree[] arra
9d450 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  y..      */.    
9d460 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 69    int i;.      i
9d470 20 3d 20 28 75 38 20 2a 29 70 20 2d 20 28 75 38   = (u8 *)p - (u8
9d480 20 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   *)sqlite3Global
9d490 43 6f 6e 66 69 67 2e 70 50 61 67 65 3b 0a 20 20  Config.pPage;.  
9d4a0 20 20 20 20 69 20 2f 3d 20 73 71 6c 69 74 65 33      i /= sqlite3
9d4b0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50  GlobalConfig.szP
9d4c0 61 67 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72  age;.      asser
9d4d0 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 73 71 6c  t( i>=0 && i<sql
9d4e0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9d4f0 2e 6e 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20  .nPage );.      
9d500 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
9d510 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b  ter(mem0.mutex);
9d520 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6d  .      assert( m
9d530 65 6d 30 2e 6e 50 61 67 65 46 72 65 65 3c 73 71  em0.nPageFree<sq
9d540 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9d550 67 2e 6e 50 61 67 65 20 29 3b 0a 20 20 20 20 20  g.nPage );.     
9d560 20 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 65 5b   mem0.aPageFree[
9d570 6d 65 6d 30 2e 6e 50 61 67 65 46 72 65 65 2b 2b  mem0.nPageFree++
9d580 5d 20 3d 20 69 3b 0a 20 20 20 20 20 20 73 71 6c  ] = i;.      sql
9d590 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51  ite3StatusAdd(SQ
9d5a0 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45  LITE_STATUS_PAGE
9d5b0 43 41 43 48 45 5f 55 53 45 44 2c 20 2d 31 29 3b  CACHE_USED, -1);
9d5c0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
9d5d0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e  utex_leave(mem0.
9d5e0 6d 75 74 65 78 29 3b 0a 23 69 66 20 21 64 65 66  mutex);.#if !def
9d5f0 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20  ined(NDEBUG) && 
9d600 30 0a 20 20 20 20 20 20 2f 2a 20 41 73 73 65 72  0.      /* Asser
9d610 74 20 74 68 61 74 20 61 20 64 75 70 6c 69 63 61  t that a duplica
9d620 74 65 20 77 61 73 20 6e 6f 74 20 6a 75 73 74 20  te was not just 
9d630 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 61 50  inserted into aP
9d640 61 67 65 46 72 65 65 5b 5d 2e 20 2a 2f 0a 20 20  ageFree[]. */.  
9d650 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6d      for(i=0; i<m
9d660 65 6d 30 2e 6e 50 61 67 65 46 72 65 65 2d 31 3b  em0.nPageFree-1;
9d670 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61   i++){.        a
9d680 73 73 65 72 74 28 20 6d 65 6d 30 2e 61 50 61 67  ssert( mem0.aPag
9d690 65 46 72 65 65 5b 69 5d 21 3d 6d 65 6d 30 2e 61  eFree[i]!=mem0.a
9d6a0 50 61 67 65 46 72 65 65 5b 6d 65 6d 30 2e 6e 50  PageFree[mem0.nP
9d6b0 61 67 65 46 72 65 65 2d 31 5d 20 29 3b 0a 20 20  ageFree-1] );.  
9d6c0 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
9d6d0 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a   }.  }.}.#endif.
9d6e0 0a 2f 2a 0a 2a 2a 20 54 52 55 45 20 69 66 20 70  ./*.** TRUE if p
9d6f0 20 69 73 20 61 20 6c 6f 6f 6b 61 73 69 64 65 20   is a lookaside 
9d700 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
9d710 6e 20 66 72 6f 6d 20 64 62 0a 2a 2f 0a 23 69 66  n from db.*/.#if
9d720 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
9d730 5f 4c 4f 4f 4b 41 53 49 44 45 0a 73 74 61 74 69  _LOOKASIDE.stati
9d740 63 20 69 6e 74 20 69 73 4c 6f 6f 6b 61 73 69 64  c int isLookasid
9d750 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76  e(sqlite3 *db, v
9d760 6f 69 64 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  oid *p){.  retur
9d770 6e 20 64 62 20 26 26 20 70 20 26 26 20 70 3e 3d  n db && p && p>=
9d780 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53  db->lookaside.pS
9d790 74 61 72 74 20 26 26 20 70 3c 64 62 2d 3e 6c 6f  tart && p<db->lo
9d7a0 6f 6b 61 73 69 64 65 2e 70 45 6e 64 3b 0a 7d 0a  okaside.pEnd;.}.
9d7b0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 69 73  #else.#define is
9d7c0 4c 6f 6f 6b 61 73 69 64 65 28 41 2c 42 29 20 30  Lookaside(A,B) 0
9d7d0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52  .#endif../*.** R
9d7e0 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f  eturn the size o
9d7f0 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  f a memory alloc
9d800 61 74 69 6f 6e 20 70 72 65 76 69 6f 75 73 6c 79  ation previously
9d810 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a   obtained from.*
9d820 2a 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  * sqlite3Malloc(
9d830 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 6d 61 6c  ) or sqlite3_mal
9d840 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  loc()..*/.SQLITE
9d850 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
9d860 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 76  ite3MallocSize(v
9d870 6f 69 64 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  oid *p){.  retur
9d880 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  n sqlite3GlobalC
9d890 6f 6e 66 69 67 2e 6d 2e 78 53 69 7a 65 28 70 29  onfig.m.xSize(p)
9d8a0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
9d8b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 44 62  TE int sqlite3Db
9d8c0 4d 61 6c 6c 6f 63 53 69 7a 65 28 73 71 6c 69 74  MallocSize(sqlit
9d8d0 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 29  e3 *db, void *p)
9d8e0 7b 0a 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73  {.  if( isLookas
9d8f0 69 64 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20  ide(db, p) ){.  
9d900 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 6c 6f 6f    return db->loo
9d910 6b 61 73 69 64 65 2e 73 7a 3b 0a 20 20 7d 65 6c  kaside.sz;.  }el
9d920 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73  se{.    return s
9d930 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9d940 69 67 2e 6d 2e 78 53 69 7a 65 28 70 29 3b 0a 20  ig.m.xSize(p);. 
9d950 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65   }.}../*.** Free
9d960 20 6d 65 6d 6f 72 79 20 70 72 65 76 69 6f 75 73   memory previous
9d970 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ly obtained from
9d980 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29   sqlite3Malloc()
9d990 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
9d9a0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 66 72 65  void sqlite3_fre
9d9b0 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 66  e(void *p){.  if
9d9c0 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( p==0 ) return;
9d9d0 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
9d9e0 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73  obalConfig.bMems
9d9f0 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  tat ){.    sqlit
9da00 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
9da10 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20  em0.mutex);.    
9da20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64  sqlite3StatusAdd
9da30 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d  (SQLITE_STATUS_M
9da40 45 4d 4f 52 59 5f 55 53 45 44 2c 20 2d 73 71 6c  EMORY_USED, -sql
9da50 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70  ite3MallocSize(p
9da60 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47  ));.    sqlite3G
9da70 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46  lobalConfig.m.xF
9da80 72 65 65 28 70 29 3b 0a 20 20 20 20 73 71 6c 69  ree(p);.    sqli
9da90 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
9daa0 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d  mem0.mutex);.  }
9dab0 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
9dac0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
9dad0 78 46 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a  xFree(p);.  }.}.
9dae0 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f  ./*.** Free memo
9daf0 72 79 20 74 68 61 74 20 6d 69 67 68 74 20 62 65  ry that might be
9db00 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
9db10 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61   a particular da
9db20 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63  tabase.** connec
9db30 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
9db40 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
9db50 69 74 65 33 44 62 46 72 65 65 28 73 71 6c 69 74  ite3DbFree(sqlit
9db60 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 29  e3 *db, void *p)
9db70 7b 0a 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73  {.  if( isLookas
9db80 69 64 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20  ide(db, p) ){.  
9db90 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20    LookasideSlot 
9dba0 2a 70 42 75 66 20 3d 20 28 4c 6f 6f 6b 61 73 69  *pBuf = (Lookasi
9dbb0 64 65 53 6c 6f 74 2a 29 70 3b 0a 20 20 20 20 70  deSlot*)p;.    p
9dbc0 42 75 66 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d  Buf->pNext = db-
9dbd0 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65  >lookaside.pFree
9dbe0 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73  ;.    db->lookas
9dbf0 69 64 65 2e 70 46 72 65 65 20 3d 20 70 42 75 66  ide.pFree = pBuf
9dc00 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73  ;.    db->lookas
9dc10 69 64 65 2e 6e 4f 75 74 2d 2d 3b 0a 20 20 7d 65  ide.nOut--;.  }e
9dc20 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
9dc30 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a  _free(p);.  }.}.
9dc40 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
9dc50 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69  e size of an exi
9dc60 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c  sting memory all
9dc70 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54  ocation.*/.SQLIT
9dc80 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
9dc90 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 76  sqlite3Realloc(v
9dca0 6f 69 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e  oid *pOld, int n
9dcb0 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f  Bytes){.  int nO
9dcc0 6c 64 2c 20 6e 4e 65 77 3b 0a 20 20 76 6f 69 64  ld, nNew;.  void
9dcd0 20 2a 70 4e 65 77 3b 0a 20 20 69 66 28 20 70 4f   *pNew;.  if( pO
9dce0 6c 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ld==0 ){.    ret
9dcf0 75 72 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  urn sqlite3Mallo
9dd00 63 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a 20  c(nBytes);.  }. 
9dd10 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 30 20 29   if( nBytes<=0 )
9dd20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
9dd30 65 65 28 70 4f 6c 64 29 3b 0a 20 20 20 20 72 65  ee(pOld);.    re
9dd40 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 4f  turn 0;.  }.  nO
9dd50 6c 64 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  ld = sqlite3Mall
9dd60 6f 63 53 69 7a 65 28 70 4f 6c 64 29 3b 0a 20 20  ocSize(pOld);.  
9dd70 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
9dd80 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74  lConfig.bMemstat
9dd90 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
9dda0 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30  mutex_enter(mem0
9ddb0 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c  .mutex);.    sql
9ddc0 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51  ite3StatusSet(SQ
9ddd0 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c  LITE_STATUS_MALL
9dde0 4f 43 5f 53 49 5a 45 2c 20 6e 42 79 74 65 73 29  OC_SIZE, nBytes)
9ddf0 3b 0a 20 20 20 20 6e 4e 65 77 20 3d 20 73 71 6c  ;.    nNew = sql
9de00 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9de10 2e 6d 2e 78 52 6f 75 6e 64 75 70 28 6e 42 79 74  .m.xRoundup(nByt
9de20 65 73 29 3b 0a 20 20 20 20 69 66 28 20 6e 4f 6c  es);.    if( nOl
9de30 64 3d 3d 6e 4e 65 77 20 29 7b 0a 20 20 20 20 20  d==nNew ){.     
9de40 20 70 4e 65 77 20 3d 20 70 4f 6c 64 3b 0a 20 20   pNew = pOld;.  
9de50 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
9de60 66 28 20 73 71 6c 69 74 65 33 53 74 61 74 75 73  f( sqlite3Status
9de70 56 61 6c 75 65 28 53 51 4c 49 54 45 5f 53 54 41  Value(SQLITE_STA
9de80 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 29  TUS_MEMORY_USED)
9de90 2b 6e 4e 65 77 2d 6e 4f 6c 64 20 3e 3d 20 0a 20  +nNew-nOld >= . 
9dea0 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 30 2e             mem0.
9deb0 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 20 29  alarmThreshold )
9dec0 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
9ded0 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 4e 65  3MallocAlarm(nNe
9dee0 77 2d 6e 4f 6c 64 29 3b 0a 20 20 20 20 20 20 7d  w-nOld);.      }
9def0 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71  .      pNew = sq
9df00 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9df10 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c  g.m.xRealloc(pOl
9df20 64 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 20 20  d, nNew);.      
9df30 69 66 28 20 70 4e 65 77 3d 3d 30 20 26 26 20 6d  if( pNew==0 && m
9df40 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63  em0.alarmCallbac
9df50 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  k ){.        sql
9df60 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28  ite3MallocAlarm(
9df70 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 20 20 20  nBytes);.       
9df80 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 47   pNew = sqlite3G
9df90 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52  lobalConfig.m.xR
9dfa0 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65  ealloc(pOld, nNe
9dfb0 77 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  w);.      }.    
9dfc0 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20    if( pNew ){.  
9dfd0 20 20 20 20 20 20 6e 4e 65 77 20 3d 20 73 71 6c        nNew = sql
9dfe0 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70  ite3MallocSize(p
9dff0 4e 65 77 29 3b 0a 20 20 20 20 20 20 20 20 73 71  New);.        sq
9e000 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53  lite3StatusAdd(S
9e010 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d  QLITE_STATUS_MEM
9e020 4f 52 59 5f 55 53 45 44 2c 20 6e 4e 65 77 2d 6e  ORY_USED, nNew-n
9e030 4f 6c 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Old);.      }.  
9e040 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
9e050 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30  mutex_leave(mem0
9e060 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65  .mutex);.  }else
9e070 7b 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c  {.    pNew = sql
9e080 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9e090 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c 64  .m.xRealloc(pOld
9e0a0 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a 20  , nBytes);.  }. 
9e0b0 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a   return pNew;.}.
9e0c0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 75 62 6c 69  ./*.** The publi
9e0d0 63 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 73  c interface to s
9e0e0 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 2e 20 20  qlite3Realloc.  
9e0f0 4d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74  Make sure that t
9e100 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 75 62  he memory.** sub
9e110 73 79 73 74 65 6d 20 69 73 20 69 6e 69 74 69 61  system is initia
9e120 6c 69 7a 65 64 20 70 72 69 6f 72 20 74 6f 20 69  lized prior to i
9e130 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74 65 52 65  nvoking sqliteRe
9e140 61 6c 6c 6f 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45  alloc..*/.SQLITE
9e150 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
9e160 65 33 5f 72 65 61 6c 6c 6f 63 28 76 6f 69 64 20  e3_realloc(void 
9e170 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e 29 7b 0a 23  *pOld, int n){.#
9e180 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
9e190 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66  IT_AUTOINIT.  if
9e1a0 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  ( sqlite3_initia
9e1b0 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20  lize() ) return 
9e1c0 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75  0;.#endif.  retu
9e1d0 72 6e 20 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f  rn sqlite3Reallo
9e1e0 63 28 70 4f 6c 64 2c 20 6e 29 3b 0a 7d 0a 0a 0a  c(pOld, n);.}...
9e1f0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61  /*.** Allocate a
9e200 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 0a  nd zero memory..
9e210 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */ .SQLITE_PRIVA
9e220 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
9e230 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e 74 20 6e  MallocZero(int n
9e240 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73  ){.  void *p = s
9e250 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b  qlite3Malloc(n);
9e260 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
9e270 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29 3b  memset(p, 0, n);
9e280 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
9e290 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  .}../*.** Alloca
9e2a0 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f  te and zero memo
9e2b0 72 79 2e 20 20 49 66 20 74 68 65 20 61 6c 6c 6f  ry.  If the allo
9e2c0 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 6d 61  cation fails, ma
9e2d0 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f 63  ke.** the malloc
9e2e0 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74  Failed flag in t
9e2f0 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f  he connection po
9e300 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  inter..*/.SQLITE
9e310 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
9e320 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
9e330 72 6f 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ro(sqlite3 *db, 
9e340 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a  int n){.  void *
9e350 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  p = sqlite3DbMal
9e360 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20  locRaw(db, n);. 
9e370 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6d 65   if( p ){.    me
9e380 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29 3b 0a 20  mset(p, 0, n);. 
9e390 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d   }.  return p;.}
9e3a0 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ../*.** Allocate
9e3b0 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79   and zero memory
9e3c0 2e 20 20 49 66 20 74 68 65 20 61 6c 6c 6f 63 61  .  If the alloca
9e3d0 74 69 6f 6e 20 66 61 69 6c 73 2c 20 6d 61 6b 65  tion fails, make
9e3e0 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61  .** the mallocFa
9e3f0 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65  iled flag in the
9e400 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 6e   connection poin
9e410 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64 62  ter..**.** If db
9e420 21 3d 30 20 61 6e 64 20 64 62 2d 3e 6d 61 6c 6c  !=0 and db->mall
9e430 6f 63 46 61 69 6c 65 64 20 69 73 20 74 72 75 65  ocFailed is true
9e440 20 28 69 6e 64 69 63 61 74 69 6e 67 20 61 20 70   (indicating a p
9e450 72 69 6f 72 20 6d 61 6c 6c 6f 63 0a 2a 2a 20 66  rior malloc.** f
9e460 61 69 6c 75 72 65 20 6f 6e 20 74 68 65 20 73 61  ailure on the sa
9e470 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  me database conn
9e480 65 63 74 69 6f 6e 29 20 74 68 65 6e 20 61 6c 77  ection) then alw
9e490 61 79 73 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a  ays return 0..**
9e4a0 20 48 65 6e 63 65 20 66 6f 72 20 61 20 70 61 72   Hence for a par
9e4b0 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65  ticular database
9e4c0 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 6f 6e 63   connection, onc
9e4d0 65 20 6d 61 6c 6c 6f 63 20 73 74 61 72 74 73 0a  e malloc starts.
9e4e0 2a 2a 20 66 61 69 6c 69 6e 67 2c 20 69 74 20 66  ** failing, it f
9e4f0 61 69 6c 73 20 63 6f 6e 73 69 73 74 65 6e 74 6c  ails consistentl
9e500 79 20 75 6e 74 69 6c 20 6d 61 6c 6c 6f 63 46 61  y until mallocFa
9e510 69 6c 65 64 20 69 73 20 72 65 73 65 74 2e 0a 2a  iled is reset..*
9e520 2a 20 54 68 69 73 20 69 73 20 61 6e 20 69 6d 70  * This is an imp
9e530 6f 72 74 61 6e 74 20 61 73 73 75 6d 70 74 69 6f  ortant assumptio
9e540 6e 2e 20 20 54 68 65 72 65 20 61 72 65 20 6d 61  n.  There are ma
9e550 6e 79 20 70 6c 61 63 65 73 20 69 6e 20 74 68 65  ny places in the
9e560 0a 2a 2a 20 63 6f 64 65 20 74 68 61 74 20 64 6f  .** code that do
9e570 20 74 68 69 6e 67 73 20 6c 69 6b 65 20 74 68 69   things like thi
9e580 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  s:.**.**        
9e590 20 69 6e 74 20 2a 61 20 3d 20 28 69 6e 74 2a 29   int *a = (int*)
9e5a0 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
9e5b0 61 77 28 64 62 2c 20 31 30 30 29 3b 0a 2a 2a 20  aw(db, 100);.** 
9e5c0 20 20 20 20 20 20 20 20 69 6e 74 20 2a 62 20 3d          int *b =
9e5d0 20 28 69 6e 74 2a 29 73 71 6c 69 74 65 33 44 62   (int*)sqlite3Db
9e5e0 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 32 30  MallocRaw(db, 20
9e5f0 30 29 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 69  0);.**         i
9e600 66 28 20 62 20 29 20 61 5b 31 30 5d 20 3d 20 39  f( b ) a[10] = 9
9e610 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72  ;.**.** In other
9e620 20 77 6f 72 64 73 2c 20 69 66 20 61 20 73 75 62   words, if a sub
9e630 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20 28  sequent malloc (
9e640 65 78 3a 20 22 62 22 29 20 77 6f 72 6b 65 64 2c  ex: "b") worked,
9e650 20 69 74 20 69 73 20 61 73 73 75 6d 65 64 0a 2a   it is assumed.*
9e660 2a 20 74 68 61 74 20 61 6c 6c 20 70 72 69 6f 72  * that all prior
9e670 20 6d 61 6c 6c 6f 63 73 20 28 65 78 3a 20 22 61   mallocs (ex: "a
9e680 22 29 20 77 6f 72 6b 65 64 20 74 6f 6f 2e 0a 2a  ") worked too..*
9e690 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
9e6a0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62   void *sqlite3Db
9e6b0 4d 61 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74 65  MallocRaw(sqlite
9e6c0 33 20 2a 64 62 2c 20 69 6e 74 20 6e 29 7b 0a 20  3 *db, int n){. 
9e6d0 20 76 6f 69 64 20 2a 70 3b 0a 23 69 66 6e 64 65   void *p;.#ifnde
9e6e0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
9e6f0 4f 4b 41 53 49 44 45 0a 20 20 69 66 28 20 64 62  OKASIDE.  if( db
9e700 20 29 7b 0a 20 20 20 20 4c 6f 6f 6b 61 73 69 64   ){.    Lookasid
9e710 65 53 6c 6f 74 20 2a 70 42 75 66 3b 0a 20 20 20  eSlot *pBuf;.   
9e720 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
9e730 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 72  ailed ){.      r
9e740 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20  eturn 0;.    }. 
9e750 20 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61     if( db->looka
9e760 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 26 26  side.bEnabled &&
9e770 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64   n<=db->lookasid
9e780 65 2e 73 7a 0a 20 20 20 20 20 20 20 20 20 26 26  e.sz.         &&
9e790 20 28 70 42 75 66 20 3d 20 64 62 2d 3e 6c 6f 6f   (pBuf = db->loo
9e7a0 6b 61 73 69 64 65 2e 70 46 72 65 65 29 21 3d 30  kaside.pFree)!=0
9e7b0 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f   ){.      db->lo
9e7c0 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 20 3d 20  okaside.pFree = 
9e7d0 70 42 75 66 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  pBuf->pNext;.   
9e7e0 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65     db->lookaside
9e7f0 2e 6e 4f 75 74 2b 2b 3b 0a 20 20 20 20 20 20 69  .nOut++;.      i
9e800 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65  f( db->lookaside
9e810 2e 6e 4f 75 74 3e 64 62 2d 3e 6c 6f 6f 6b 61 73  .nOut>db->lookas
9e820 69 64 65 2e 6d 78 4f 75 74 20 29 7b 0a 20 20 20  ide.mxOut ){.   
9e830 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69       db->lookasi
9e840 64 65 2e 6d 78 4f 75 74 20 3d 20 64 62 2d 3e 6c  de.mxOut = db->l
9e850 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20  ookaside.nOut;. 
9e860 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74       }.      ret
9e870 75 72 6e 20 28 76 6f 69 64 2a 29 70 42 75 66 3b  urn (void*)pBuf;
9e880 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65  .    }.  }.#else
9e890 0a 20 20 69 66 28 20 64 62 20 26 26 20 64 62 2d  .  if( db && db-
9e8a0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
9e8b0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
9e8c0 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 20 3d 20   }.#endif.  p = 
9e8d0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 29  sqlite3Malloc(n)
9e8e0 3b 0a 20 20 69 66 28 20 21 70 20 26 26 20 64 62  ;.  if( !p && db
9e8f0 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c   ){.    db->mall
9e900 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
9e910 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  }.  return p;.}.
9e920 0a 2f 2a 0a 2a 2a 20 52 65 73 69 7a 65 20 74 68  ./*.** Resize th
9e930 65 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72  e block of memor
9e940 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  y pointed to by 
9e950 70 20 74 6f 20 6e 20 62 79 74 65 73 2e 20 49 66  p to n bytes. If
9e960 20 74 68 65 0a 2a 2a 20 72 65 73 69 7a 65 20 66   the.** resize f
9e970 61 69 6c 73 2c 20 73 65 74 20 74 68 65 20 6d 61  ails, set the ma
9e980 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20  llocFailed flag 
9e990 69 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f  in the connectio
9e9a0 6e 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c  n object..*/.SQL
9e9b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
9e9c0 20 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c   *sqlite3DbReall
9e9d0 6f 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  oc(sqlite3 *db, 
9e9e0 76 6f 69 64 20 2a 70 2c 20 69 6e 74 20 6e 29 7b  void *p, int n){
9e9f0 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77 20 3d 20  .  void *pNew = 
9ea00 30 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  0;.  if( db->mal
9ea10 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 29 7b 0a  locFailed==0 ){.
9ea20 20 20 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a      if( p==0 ){.
9ea30 20 20 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c        return sql
9ea40 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
9ea50 64 62 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20  db, n);.    }.  
9ea60 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64    if( isLookasid
9ea70 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20  e(db, p) ){.    
9ea80 20 20 69 66 28 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f    if( n<=db->loo
9ea90 6b 61 73 69 64 65 2e 73 7a 20 29 7b 0a 20 20 20  kaside.sz ){.   
9eaa0 20 20 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20       return p;. 
9eab0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4e 65       }.      pNe
9eac0 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  w = sqlite3DbMal
9ead0 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20  locRaw(db, n);. 
9eae0 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b       if( pNew ){
9eaf0 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
9eb00 70 4e 65 77 2c 20 70 2c 20 64 62 2d 3e 6c 6f 6f  pNew, p, db->loo
9eb10 6b 61 73 69 64 65 2e 73 7a 29 3b 0a 20 20 20 20  kaside.sz);.    
9eb20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
9eb30 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20  e(db, p);.      
9eb40 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
9eb50 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65     pNew = sqlite
9eb60 33 5f 72 65 61 6c 6c 6f 63 28 70 2c 20 6e 29 3b  3_realloc(p, n);
9eb70 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e 65 77  .      if( !pNew
9eb80 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e   ){.        db->
9eb90 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
9eba0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
9ebb0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65    }.  return pNe
9ebc0 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65  w;.}../*.** Atte
9ebd0 6d 70 74 20 74 6f 20 72 65 61 6c 6c 6f 63 61 74  mpt to reallocat
9ebe0 65 20 70 2e 20 20 49 66 20 74 68 65 20 72 65 61  e p.  If the rea
9ebf0 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c  llocation fails,
9ec00 20 74 68 65 6e 20 66 72 65 65 20 70 0a 2a 2a 20   then free p.** 
9ec10 61 6e 64 20 73 65 74 20 74 68 65 20 6d 61 6c 6c  and set the mall
9ec20 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e  ocFailed flag in
9ec30 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
9ec40 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  nnection..*/.SQL
9ec50 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
9ec60 20 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c   *sqlite3DbReall
9ec70 6f 63 4f 72 46 72 65 65 28 73 71 6c 69 74 65 33  ocOrFree(sqlite3
9ec80 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 2c 20 69   *db, void *p, i
9ec90 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70  nt n){.  void *p
9eca0 4e 65 77 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71  New;.  pNew = sq
9ecb0 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64  lite3DbRealloc(d
9ecc0 62 2c 20 70 2c 20 6e 29 3b 0a 20 20 69 66 28 20  b, p, n);.  if( 
9ecd0 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 73 71 6c  !pNew ){.    sql
9ece0 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
9ecf0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
9ed00 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  pNew;.}../*.** M
9ed10 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 61 20  ake a copy of a 
9ed20 73 74 72 69 6e 67 20 69 6e 20 6d 65 6d 6f 72 79  string in memory
9ed30 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
9ed40 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 54  qliteMalloc(). T
9ed50 68 65 73 65 20 0a 2a 2a 20 66 75 6e 63 74 69 6f  hese .** functio
9ed60 6e 73 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 4d  ns call sqlite3M
9ed70 61 6c 6c 6f 63 52 61 77 28 29 20 64 69 72 65 63  allocRaw() direc
9ed80 74 6c 79 20 69 6e 73 74 65 61 64 20 6f 66 20 73  tly instead of s
9ed90 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 54  qliteMalloc(). T
9eda0 68 69 73 0a 2a 2a 20 69 73 20 62 65 63 61 75 73  his.** is becaus
9edb0 65 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20 64 65  e when memory de
9edc0 62 75 67 67 69 6e 67 20 69 73 20 74 75 72 6e 65  bugging is turne
9edd0 64 20 6f 6e 2c 20 74 68 65 73 65 20 74 77 6f 20  d on, these two 
9ede0 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 0a 2a  functions are .*
9edf0 2a 20 63 61 6c 6c 65 64 20 76 69 61 20 6d 61 63  * called via mac
9ee00 72 6f 73 20 74 68 61 74 20 72 65 63 6f 72 64 20  ros that record 
9ee10 74 68 65 20 63 75 72 72 65 6e 74 20 66 69 6c 65  the current file
9ee20 20 61 6e 64 20 6c 69 6e 65 20 6e 75 6d 62 65 72   and line number
9ee30 20 69 6e 20 74 68 65 0a 2a 2a 20 54 68 72 65 61   in the.** Threa
9ee40 64 44 61 74 61 20 73 74 72 75 63 74 75 72 65 2e  dData structure.
9ee50 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
9ee60 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
9ee70 44 62 53 74 72 44 75 70 28 73 71 6c 69 74 65 33  DbStrDup(sqlite3
9ee80 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   *db, const char
9ee90 20 2a 7a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e   *z){.  char *zN
9eea0 65 77 3b 0a 20 20 73 69 7a 65 5f 74 20 6e 3b 0a  ew;.  size_t n;.
9eeb0 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20    if( z==0 ){.  
9eec0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
9eed0 20 20 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 29 2b    n = strlen(z)+
9eee0 31 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6e 26  1;.  assert( (n&
9eef0 30 78 37 66 66 66 66 66 66 66 29 3d 3d 6e 20 29  0x7fffffff)==n )
9ef00 3b 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74  ;.  zNew = sqlit
9ef10 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
9ef20 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20 69 66 28  , (int)n);.  if(
9ef30 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d   zNew ){.    mem
9ef40 63 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29 3b  cpy(zNew, z, n);
9ef50 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e  .  }.  return zN
9ef60 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  ew;.}.SQLITE_PRI
9ef70 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
9ef80 65 33 44 62 53 74 72 4e 44 75 70 28 73 71 6c 69  e3DbStrNDup(sqli
9ef90 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63  te3 *db, const c
9efa0 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a  har *z, int n){.
9efb0 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20    char *zNew;.  
9efc0 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20  if( z==0 ){.    
9efd0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
9efe0 61 73 73 65 72 74 28 20 28 6e 26 30 78 37 66 66  assert( (n&0x7ff
9eff0 66 66 66 66 66 29 3d 3d 6e 20 29 3b 0a 20 20 7a  fffff)==n );.  z
9f000 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  New = sqlite3DbM
9f010 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 2b 31  allocRaw(db, n+1
9f020 29 3b 0a 20 20 69 66 28 20 7a 4e 65 77 20 29 7b  );.  if( zNew ){
9f030 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77  .    memcpy(zNew
9f040 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a 4e 65  , z, n);.    zNe
9f050 77 5b 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  w[n] = 0;.  }.  
9f060 72 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 0a  return zNew;.}..
9f070 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 73  /*.** Create a s
9f080 74 72 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 7a  tring from the z
9f090 46 72 6f 6d 61 74 20 61 72 67 75 6d 65 6e 74 20  Fromat argument 
9f0a0 61 6e 64 20 74 68 65 20 76 61 5f 6c 69 73 74 20  and the va_list 
9f0b0 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 0a 2a 2a  that follows..**
9f0c0 20 53 74 6f 72 65 20 74 68 65 20 73 74 72 69 6e   Store the strin
9f0d0 67 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61  g in memory obta
9f0e0 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
9f0f0 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 61 6b  Malloc() and mak
9f100 65 20 2a 70 7a 0a 2a 2a 20 70 6f 69 6e 74 20 74  e *pz.** point t
9f110 6f 20 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a  o that string..*
9f120 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
9f130 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 74   void sqlite3Set
9f140 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 70 7a  String(char **pz
9f150 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  , sqlite3 *db, c
9f160 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
9f170 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c  at, ...){.  va_l
9f180 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a  ist ap;.  char *
9f190 7a 3b 0a 0a 20 20 76 61 5f 73 74 61 72 74 28 61  z;..  va_start(a
9f1a0 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a  p, zFormat);.  z
9f1b0 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e   = sqlite3VMPrin
9f1c0 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20  tf(db, zFormat, 
9f1d0 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70  ap);.  va_end(ap
9f1e0 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
9f1f0 65 65 28 64 62 2c 20 2a 70 7a 29 3b 0a 20 20 2a  ee(db, *pz);.  *
9f200 70 7a 20 3d 20 7a 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  pz = z;.}.../*.*
9f210 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
9f220 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 62  must be called b
9f230 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 61 6e  efore exiting an
9f240 79 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 28  y API function (
9f250 69 2e 65 2e 20 0a 2a 2a 20 72 65 74 75 72 6e 69  i.e. .** returni
9f260 6e 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68  ng control to th
9f270 65 20 75 73 65 72 29 20 74 68 61 74 20 68 61 73  e user) that has
9f280 20 63 61 6c 6c 65 64 20 73 71 6c 69 74 65 33 5f   called sqlite3_
9f290 6d 61 6c 6c 6f 63 20 6f 72 0a 2a 2a 20 73 71 6c  malloc or.** sql
9f2a0 69 74 65 33 5f 72 65 61 6c 6c 6f 63 2e 0a 2a 2a  ite3_realloc..**
9f2b0 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64  .** The returned
9f2c0 20 76 61 6c 75 65 20 69 73 20 6e 6f 72 6d 61 6c   value is normal
9f2d0 6c 79 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  ly a copy of the
9f2e0 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
9f2f0 20 74 6f 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63   to this.** func
9f300 74 69 6f 6e 2e 20 48 6f 77 65 76 65 72 2c 20 69  tion. However, i
9f310 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  f a malloc() fai
9f320 6c 75 72 65 20 68 61 73 20 6f 63 63 75 72 65 64  lure has occured
9f330 20 73 69 6e 63 65 20 74 68 65 20 70 72 65 76 69   since the previ
9f340 6f 75 73 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f  ous.** invocatio
9f350 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69  n SQLITE_NOMEM i
9f360 73 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74 65  s returned inste
9f370 61 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ad. .**.** If th
9f380 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
9f390 2c 20 64 62 2c 20 69 73 20 6e 6f 74 20 4e 55 4c  , db, is not NUL
9f3a0 4c 20 61 6e 64 20 61 20 6d 61 6c 6c 6f 63 28 29  L and a malloc()
9f3b0 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72   error has occur
9f3c0 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20  ed,.** then the 
9f3d0 63 6f 6e 6e 65 63 74 69 6f 6e 20 65 72 72 6f 72  connection error
9f3e0 2d 63 6f 64 65 20 28 74 68 65 20 76 61 6c 75 65  -code (the value
9f3f0 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c   returned by sql
9f400 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 29 0a  ite3_errcode()).
9f410 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c  ** is set to SQL
9f420 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a 53 51  ITE_NOMEM..*/.SQ
9f430 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
9f440 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
9f450 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 69 6e 74  sqlite3* db, int
9f460 20 72 63 29 7b 0a 20 20 2f 2a 20 49 66 20 74 68   rc){.  /* If th
9f470 65 20 64 62 20 68 61 6e 64 6c 65 20 69 73 20 6e  e db handle is n
9f480 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 77 65  ot NULL, then we
9f490 20 6d 75 73 74 20 68 6f 6c 64 20 74 68 65 20 63   must hold the c
9f4a0 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65  onnection handle
9f4b0 0a 20 20 2a 2a 20 6d 75 74 65 78 20 68 65 72 65  .  ** mutex here
9f4c0 2e 20 4f 74 68 65 72 77 69 73 65 20 74 68 65 20  . Otherwise the 
9f4d0 72 65 61 64 20 28 61 6e 64 20 70 6f 73 73 69 62  read (and possib
9f4e0 6c 65 20 77 72 69 74 65 29 20 6f 66 20 64 62 2d  le write) of db-
9f4f0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 0a 20  >mallocFailed . 
9f500 20 2a 2a 20 69 73 20 75 6e 73 61 66 65 2c 20 61   ** is unsafe, a
9f510 73 20 69 73 20 74 68 65 20 63 61 6c 6c 20 74 6f  s is the call to
9f520 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 2e   sqlite3Error().
9f530 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
9f540 21 64 62 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d  !db || sqlite3_m
9f550 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75  utex_held(db->mu
9f560 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 64 62  tex) );.  if( db
9f570 20 26 26 20 28 64 62 2d 3e 6d 61 6c 6c 6f 63 46   && (db->mallocF
9f580 61 69 6c 65 64 20 7c 7c 20 72 63 3d 3d 53 51 4c  ailed || rc==SQL
9f590 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 29  ITE_IOERR_NOMEM)
9f5a0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
9f5b0 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
9f5c0 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20 20 64  NOMEM, 0);.    d
9f5d0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
9f5e0 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  = 0;.    rc = SQ
9f5f0 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
9f600 20 20 72 65 74 75 72 6e 20 72 63 20 26 20 28 64    return rc & (d
9f610 62 20 3f 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20  b ? db->errMask 
9f620 3a 20 30 78 66 66 29 3b 0a 7d 0a 0a 2f 2a 2a 2a  : 0xff);.}../***
9f630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
9f640 6f 66 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a 2a 2a  of malloc.c ****
9f650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
9f680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
9f690 6e 20 66 69 6c 65 20 70 72 69 6e 74 66 2e 63 20  n file printf.c 
9f6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
9f6d0 2a 20 54 68 65 20 22 70 72 69 6e 74 66 22 20 63  * The "printf" c
9f6e0 6f 64 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73  ode that follows
9f6f0 20 64 61 74 65 73 20 66 72 6f 6d 20 74 68 65 20   dates from the 
9f700 31 39 38 30 27 73 2e 20 20 49 74 20 69 73 20 69  1980's.  It is i
9f710 6e 0a 2a 2a 20 74 68 65 20 70 75 62 6c 69 63 20  n.** the public 
9f720 64 6f 6d 61 69 6e 2e 20 20 54 68 65 20 6f 72 69  domain.  The ori
9f730 67 69 6e 61 6c 20 63 6f 6d 6d 65 6e 74 73 20 61  ginal comments a
9f740 72 65 20 69 6e 63 6c 75 64 65 64 20 68 65 72 65  re included here
9f750 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65   for.** complete
9f760 6e 65 73 73 2e 20 20 54 68 65 79 20 61 72 65 20  ness.  They are 
9f770 76 65 72 79 20 6f 75 74 2d 6f 66 2d 64 61 74 65  very out-of-date
9f780 20 62 75 74 20 6d 69 67 68 74 20 62 65 20 75 73   but might be us
9f790 65 66 75 6c 20 61 73 0a 2a 2a 20 61 6e 20 68 69  eful as.** an hi
9f7a0 73 74 6f 72 69 63 61 6c 20 72 65 66 65 72 65 6e  storical referen
9f7b0 63 65 2e 20 20 4d 6f 73 74 20 6f 66 20 74 68 65  ce.  Most of the
9f7c0 20 22 65 6e 68 61 6e 63 65 6d 65 6e 74 73 22 20   "enhancements" 
9f7d0 68 61 76 65 20 62 65 65 6e 20 62 61 63 6b 65 64  have been backed
9f7e0 0a 2a 2a 20 6f 75 74 20 73 6f 20 74 68 61 74 20  .** out so that 
9f7f0 74 68 65 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74  the functionalit
9f800 79 20 69 73 20 6e 6f 77 20 74 68 65 20 73 61 6d  y is now the sam
9f810 65 20 61 73 20 73 74 61 6e 64 61 72 64 20 70 72  e as standard pr
9f820 69 6e 74 66 28 29 2e 0a 2a 2a 0a 2a 2a 20 24 49  intf()..**.** $I
9f830 64 3a 20 70 72 69 6e 74 66 2e 63 2c 76 20 31 2e  d: printf.c,v 1.
9f840 39 34 20 32 30 30 38 2f 30 38 2f 32 32 20 31 34  94 2008/08/22 14
9f850 3a 30 38 3a 33 36 20 64 72 68 20 45 78 70 20 24  :08:36 drh Exp $
9f860 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
9f870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f8a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
9f8b0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
9f8c0 6e 67 20 6d 6f 64 75 6c 65 73 20 69 73 20 61 6e  ng modules is an
9f8d0 20 65 6e 68 61 6e 63 65 64 20 72 65 70 6c 61 63   enhanced replac
9f8e0 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20 22 70  ement for the "p
9f8f0 72 69 6e 74 66 22 20 73 75 62 72 6f 75 74 69 6e  rintf" subroutin
9f900 65 73 0a 2a 2a 20 66 6f 75 6e 64 20 69 6e 20 74  es.** found in t
9f910 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69  he standard C li
9f920 62 72 61 72 79 2e 20 20 54 68 65 20 66 6f 6c 6c  brary.  The foll
9f930 6f 77 69 6e 67 20 65 6e 68 61 6e 63 65 6d 65 6e  owing enhancemen
9f940 74 73 20 61 72 65 0a 2a 2a 20 73 75 70 70 6f 72  ts are.** suppor
9f950 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  ted:.**.**      
9f960 2b 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 66 75  +  Additional fu
9f970 6e 63 74 69 6f 6e 73 2e 20 20 54 68 65 20 73 74  nctions.  The st
9f980 61 6e 64 61 72 64 20 73 65 74 20 6f 66 20 22 70  andard set of "p
9f990 72 69 6e 74 66 22 20 66 75 6e 63 74 69 6f 6e 73  rintf" functions
9f9a0 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 63 6c  .**         incl
9f9b0 75 64 65 73 20 70 72 69 6e 74 66 2c 20 66 70 72  udes printf, fpr
9f9c0 69 6e 74 66 2c 20 73 70 72 69 6e 74 66 2c 20 76  intf, sprintf, v
9f9d0 70 72 69 6e 74 66 2c 20 76 66 70 72 69 6e 74 66  printf, vfprintf
9f9e0 2c 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20  , and.**        
9f9f0 20 76 73 70 72 69 6e 74 66 2e 20 20 54 68 69 73   vsprintf.  This
9fa00 20 6d 6f 64 75 6c 65 20 61 64 64 73 20 74 68 65   module adds the
9fa10 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a   following:.**.*
9fa20 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 73  *           *  s
9fa30 6e 70 72 69 6e 74 66 20 2d 2d 20 57 6f 72 6b 73  nprintf -- Works
9fa40 20 6c 69 6b 65 20 73 70 72 69 6e 74 66 2c 20 62   like sprintf, b
9fa50 75 74 20 68 61 73 20 61 6e 20 65 78 74 72 61 20  ut has an extra 
9fa60 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 20  argument.**     
9fa70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9fa80 20 20 20 20 20 77 68 69 63 68 20 69 73 20 74 68       which is th
9fa90 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75  e size of the bu
9faa0 66 66 65 72 20 77 72 69 74 74 65 6e 20 74 6f 2e  ffer written to.
9fab0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .**.**          
9fac0 20 2a 20 20 6d 70 72 69 6e 74 66 20 2d 2d 20 20   *  mprintf --  
9fad0 53 69 6d 69 6c 61 72 20 74 6f 20 73 70 72 69 6e  Similar to sprin
9fae0 74 66 2e 20 20 57 72 69 74 65 73 20 6f 75 74 70  tf.  Writes outp
9faf0 75 74 20 74 6f 20 6d 65 6d 6f 72 79 0a 2a 2a 20  ut to memory.** 
9fb00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9fb10 20 20 20 20 20 20 20 20 20 6f 62 74 61 69 6e 65           obtaine
9fb20 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a  d from malloc..*
9fb30 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a  *.**           *
9fb40 20 20 78 70 72 69 6e 74 66 20 2d 2d 20 20 43 61    xprintf --  Ca
9fb50 6c 6c 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 74  lls a function t
9fb60 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 6f 75 74  o dispose of out
9fb70 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  put..**.**      
9fb80 20 20 20 20 20 2a 20 20 6e 70 72 69 6e 74 66 20       *  nprintf 
9fb90 2d 2d 20 20 4e 6f 20 6f 75 74 70 75 74 2c 20 62  --  No output, b
9fba0 75 74 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  ut returns the n
9fbb0 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
9fbc0 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ers.**          
9fbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9fbe0 74 68 61 74 20 77 6f 75 6c 64 20 68 61 76 65 20  that would have 
9fbf0 62 65 65 6e 20 6f 75 74 70 75 74 20 62 79 20 70  been output by p
9fc00 72 69 6e 74 66 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  rintf..**.**    
9fc10 20 20 20 20 20 20 20 2a 20 20 41 20 76 2d 20 76         *  A v- v
9fc20 65 72 73 69 6f 6e 20 28 65 78 3a 20 76 73 6e 70  ersion (ex: vsnp
9fc30 72 69 6e 74 66 29 20 6f 66 20 65 76 65 72 79 20  rintf) of every 
9fc40 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 6c 73 6f  function is also
9fc50 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
9fc60 20 73 75 70 70 6c 69 65 64 2e 0a 2a 2a 0a 2a 2a   supplied..**.**
9fc70 20 20 20 20 20 20 2b 20 20 41 20 66 65 77 20 65        +  A few e
9fc80 78 74 65 6e 73 69 6f 6e 73 20 74 6f 20 74 68 65  xtensions to the
9fc90 20 66 6f 72 6d 61 74 74 69 6e 67 20 6e 6f 74 61   formatting nota
9fca0 74 69 6f 6e 20 61 72 65 20 73 75 70 70 6f 72 74  tion are support
9fcb0 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  ed:.**.**       
9fcc0 20 20 20 20 2a 20 20 54 68 65 20 22 3d 22 20 66      *  The "=" f
9fcd0 6c 61 67 20 28 73 69 6d 69 6c 61 72 20 74 6f 20  lag (similar to 
9fce0 22 2d 22 29 20 63 61 75 73 65 73 20 74 68 65 20  "-") causes the 
9fcf0 6f 75 74 70 75 74 20 74 6f 20 62 65 0a 2a 2a 20  output to be.** 
9fd00 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65 20               be 
9fd10 63 65 6e 74 65 72 65 64 20 69 6e 20 74 68 65 20  centered in the 
9fd20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 20 73 69  appropriately si
9fd30 7a 65 64 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a 2a  zed field..**.**
9fd40 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 54 68             *  Th
9fd50 65 20 25 62 20 66 69 65 6c 64 20 6f 75 74 70 75  e %b field outpu
9fd60 74 73 20 61 6e 20 69 6e 74 65 67 65 72 20 69 6e  ts an integer in
9fd70 20 62 69 6e 61 72 79 20 6e 6f 74 61 74 69 6f 6e   binary notation
9fd80 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  ..**.**         
9fd90 20 20 2a 20 20 54 68 65 20 25 63 20 66 69 65 6c    *  The %c fiel
9fda0 64 20 6e 6f 77 20 61 63 63 65 70 74 73 20 61 20  d now accepts a 
9fdb0 70 72 65 63 69 73 69 6f 6e 2e 20 20 54 68 65 20  precision.  The 
9fdc0 63 68 61 72 61 63 74 65 72 20 6f 75 74 70 75 74  character output
9fdd0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
9fde0 20 69 73 20 72 65 70 65 61 74 65 64 20 62 79 20   is repeated by 
9fdf0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69  the number of ti
9fe00 6d 65 73 20 74 68 65 20 70 72 65 63 69 73 69 6f  mes the precisio
9fe10 6e 20 73 70 65 63 69 66 69 65 73 2e 0a 2a 2a 0a  n specifies..**.
9fe20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20  **           *  
9fe30 54 68 65 20 25 27 20 66 69 65 6c 64 20 77 6f 72  The %' field wor
9fe40 6b 73 20 6c 69 6b 65 20 25 63 2c 20 62 75 74 20  ks like %c, but 
9fe50 74 61 6b 65 73 20 61 73 20 69 74 73 20 63 68 61  takes as its cha
9fe60 72 61 63 74 65 72 20 74 68 65 0a 2a 2a 20 20 20  racter the.**   
9fe70 20 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 20             next 
9fe80 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 65  character of the
9fe90 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 2c 20   format string, 
9fea0 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 6e  instead of the n
9feb0 65 78 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ext.**          
9fec0 20 20 20 20 61 72 67 75 6d 65 6e 74 2e 20 20 46      argument.  F
9fed0 6f 72 20 65 78 61 6d 70 6c 65 2c 20 20 70 72 69  or example,  pri
9fee0 6e 74 66 28 22 25 2e 37 38 27 2d 22 29 20 20 70  ntf("%.78'-")  p
9fef0 72 69 6e 74 73 20 37 38 20 6d 69 6e 75 73 0a 2a  rints 78 minus.*
9ff00 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  *              s
9ff10 69 67 6e 73 2c 20 74 68 65 20 73 61 6d 65 20 61  igns, the same a
9ff20 73 20 20 70 72 69 6e 74 66 28 22 25 2e 37 38 63  s  printf("%.78c
9ff30 22 2c 27 2d 27 29 2e 0a 2a 2a 0a 2a 2a 20 20 20  ",'-')..**.**   
9ff40 20 20 20 2b 20 20 57 68 65 6e 20 63 6f 6d 70 69     +  When compi
9ff50 6c 65 64 20 75 73 69 6e 67 20 47 43 43 20 6f 6e  led using GCC on
9ff60 20 61 20 53 50 41 52 43 2c 20 74 68 69 73 20 76   a SPARC, this v
9ff70 65 72 73 69 6f 6e 20 6f 66 20 70 72 69 6e 74 66  ersion of printf
9ff80 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 66   is.**         f
9ff90 61 73 74 65 72 20 74 68 61 6e 20 74 68 65 20 6c  aster than the l
9ffa0 69 62 72 61 72 79 20 70 72 69 6e 74 66 20 66 6f  ibrary printf fo
9ffb0 72 20 53 55 4e 20 4f 53 20 34 2e 31 2e 0a 2a 2a  r SUN OS 4.1..**
9ffc0 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 6c 6c 20  .**      +  All 
9ffd0 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 66 75  functions are fu
9ffe0 6c 6c 79 20 72 65 65 6e 74 72 61 6e 74 2e 0a 2a  lly reentrant..*
9fff0 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  *.*/../*.** Conv
a0000 65 72 73 69 6f 6e 20 74 79 70 65 73 20 66 61 6c  ersion types fal
a0010 6c 20 69 6e 74 6f 20 76 61 72 69 6f 75 73 20 63  l into various c
a0020 61 74 65 67 6f 72 69 65 73 20 61 73 20 64 65 66  ategories as def
a0030 69 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 66  ined by the.** f
a0040 6f 6c 6c 6f 77 69 6e 67 20 65 6e 75 6d 65 72 61  ollowing enumera
a0050 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  tion..*/.#define
a0060 20 65 74 52 41 44 49 58 20 20 20 20 20 20 20 31   etRADIX       1
a0070 20 2f 2a 20 49 6e 74 65 67 65 72 20 74 79 70 65   /* Integer type
a0080 73 2e 20 20 25 64 2c 20 25 78 2c 20 25 6f 2c 20  s.  %d, %x, %o, 
a0090 61 6e 64 20 73 6f 20 66 6f 72 74 68 20 2a 2f 0a  and so forth */.
a00a0 23 64 65 66 69 6e 65 20 65 74 46 4c 4f 41 54 20  #define etFLOAT 
a00b0 20 20 20 20 20 20 32 20 2f 2a 20 46 6c 6f 61 74        2 /* Float
a00c0 69 6e 67 20 70 6f 69 6e 74 2e 20 20 25 66 20 2a  ing point.  %f *
a00d0 2f 0a 23 64 65 66 69 6e 65 20 65 74 45 58 50 20  /.#define etEXP 
a00e0 20 20 20 20 20 20 20 20 33 20 2f 2a 20 45 78 70          3 /* Exp
a00f0 6f 6e 65 6e 74 69 6f 6e 61 6c 20 6e 6f 74 61 74  onentional notat
a0100 69 6f 6e 2e 20 25 65 20 61 6e 64 20 25 45 20 2a  ion. %e and %E *
a0110 2f 0a 23 64 65 66 69 6e 65 20 65 74 47 45 4e 45  /.#define etGENE
a0120 52 49 43 20 20 20 20 20 34 20 2f 2a 20 46 6c 6f  RIC     4 /* Flo
a0130 61 74 69 6e 67 20 6f 72 20 65 78 70 6f 6e 65 6e  ating or exponen
a0140 74 69 61 6c 2c 20 64 65 70 65 6e 64 69 6e 67 20  tial, depending 
a0150 6f 6e 20 65 78 70 6f 6e 65 6e 74 2e 20 25 67 20  on exponent. %g 
a0160 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 49 5a  */.#define etSIZ
a0170 45 20 20 20 20 20 20 20 20 35 20 2f 2a 20 52 65  E        5 /* Re
a0180 74 75 72 6e 20 6e 75 6d 62 65 72 20 6f 66 20 63  turn number of c
a0190 68 61 72 61 63 74 65 72 73 20 70 72 6f 63 65 73  haracters proces
a01a0 73 65 64 20 73 6f 20 66 61 72 2e 20 25 6e 20 2a  sed so far. %n *
a01b0 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 54 52 49  /.#define etSTRI
a01c0 4e 47 20 20 20 20 20 20 36 20 2f 2a 20 53 74 72  NG      6 /* Str
a01d0 69 6e 67 73 2e 20 25 73 20 2a 2f 0a 23 64 65 66  ings. %s */.#def
a01e0 69 6e 65 20 65 74 44 59 4e 53 54 52 49 4e 47 20  ine etDYNSTRING 
a01f0 20 20 37 20 2f 2a 20 44 79 6e 61 6d 69 63 61 6c    7 /* Dynamical
a0200 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 72  ly allocated str
a0210 69 6e 67 73 2e 20 25 7a 20 2a 2f 0a 23 64 65 66  ings. %z */.#def
a0220 69 6e 65 20 65 74 50 45 52 43 45 4e 54 20 20 20  ine etPERCENT   
a0230 20 20 38 20 2f 2a 20 50 65 72 63 65 6e 74 20 73    8 /* Percent s
a0240 79 6d 62 6f 6c 2e 20 25 25 20 2a 2f 0a 23 64 65  ymbol. %% */.#de
a0250 66 69 6e 65 20 65 74 43 48 41 52 58 20 20 20 20  fine etCHARX    
a0260 20 20 20 39 20 2f 2a 20 43 68 61 72 61 63 74 65     9 /* Characte
a0270 72 73 2e 20 25 63 20 2a 2f 0a 2f 2a 20 54 68 65  rs. %c */./* The
a0280 20 72 65 73 74 20 61 72 65 20 65 78 74 65 6e 73   rest are extens
a0290 69 6f 6e 73 2c 20 6e 6f 74 20 6e 6f 72 6d 61 6c  ions, not normal
a02a0 6c 79 20 66 6f 75 6e 64 20 69 6e 20 70 72 69 6e  ly found in prin
a02b0 74 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20  tf() */.#define 
a02c0 65 74 53 51 4c 45 53 43 41 50 45 20 20 31 30 20  etSQLESCAPE  10 
a02d0 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74 68 20  /* Strings with 
a02e0 27 5c 27 27 20 64 6f 75 62 6c 65 64 2e 20 20 25  '\'' doubled.  %
a02f0 71 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53  q */.#define etS
a0300 51 4c 45 53 43 41 50 45 32 20 31 31 20 2f 2a 20  QLESCAPE2 11 /* 
a0310 53 74 72 69 6e 67 73 20 77 69 74 68 20 27 5c 27  Strings with '\'
a0320 27 20 64 6f 75 62 6c 65 64 20 61 6e 64 20 65 6e  ' doubled and en
a0330 63 6c 6f 73 65 64 20 69 6e 20 27 27 2c 0a 20 20  closed in '',.  
a0340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a0350 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 70 6f 69          NULL poi
a0360 6e 74 65 72 73 20 72 65 70 6c 61 63 65 64 20 62  nters replaced b
a0370 79 20 53 51 4c 20 4e 55 4c 4c 2e 20 20 25 51 20  y SQL NULL.  %Q 
a0380 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 54 4f 4b  */.#define etTOK
a0390 45 4e 20 20 20 20 20 20 31 32 20 2f 2a 20 61 20  EN      12 /* a 
a03a0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54 6f 6b  pointer to a Tok
a03b0 65 6e 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  en structure */.
a03c0 23 64 65 66 69 6e 65 20 65 74 53 52 43 4c 49 53  #define etSRCLIS
a03d0 54 20 20 20 20 31 33 20 2f 2a 20 61 20 70 6f 69  T    13 /* a poi
a03e0 6e 74 65 72 20 74 6f 20 61 20 53 72 63 4c 69 73  nter to a SrcLis
a03f0 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 50  t */.#define etP
a0400 4f 49 4e 54 45 52 20 20 20 20 31 34 20 2f 2a 20  OINTER    14 /* 
a0410 54 68 65 20 25 70 20 63 6f 6e 76 65 72 73 69 6f  The %p conversio
a0420 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53  n */.#define etS
a0430 51 4c 45 53 43 41 50 45 33 20 31 35 20 2f 2a 20  QLESCAPE3 15 /* 
a0440 25 77 20 2d 3e 20 53 74 72 69 6e 67 73 20 77 69  %w -> Strings wi
a0450 74 68 20 27 5c 22 27 20 64 6f 75 62 6c 65 64 20  th '\"' doubled 
a0460 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 4f 52 44  */.#define etORD
a0470 49 4e 41 4c 20 20 20 20 31 36 20 2f 2a 20 25 72  INAL    16 /* %r
a0480 20 2d 3e 20 31 73 74 2c 20 32 6e 64 2c 20 33 72   -> 1st, 2nd, 3r
a0490 64 2c 20 34 74 68 2c 20 65 74 63 2e 20 20 45 6e  d, 4th, etc.  En
a04a0 67 6c 69 73 68 20 6f 6e 6c 79 20 2a 2f 0a 0a 0a  glish only */...
a04b0 2f 2a 0a 2a 2a 20 41 6e 20 22 65 74 42 79 74 65  /*.** An "etByte
a04c0 22 20 69 73 20 61 6e 20 38 2d 62 69 74 20 75 6e  " is an 8-bit un
a04d0 73 69 67 6e 65 64 20 76 61 6c 75 65 2e 0a 2a 2f  signed value..*/
a04e0 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65  .typedef unsigne
a04f0 64 20 63 68 61 72 20 65 74 42 79 74 65 3b 0a 0a  d char etByte;..
a0500 2f 2a 0a 2a 2a 20 45 61 63 68 20 62 75 69 6c 74  /*.** Each built
a0510 69 6e 20 63 6f 6e 76 65 72 73 69 6f 6e 20 63 68  in conversion ch
a0520 61 72 61 63 74 65 72 20 28 65 78 3a 20 74 68 65  aracter (ex: the
a0530 20 27 64 27 20 69 6e 20 22 25 64 22 29 20 69 73   'd' in "%d") is
a0540 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 62 79   described.** by
a0550 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
a0560 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
a0570 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70 65 64  ructure.*/.typed
a0580 65 66 20 73 74 72 75 63 74 20 65 74 5f 69 6e 66  ef struct et_inf
a0590 6f 20 7b 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61  o {   /* Informa
a05a0 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20  tion about each 
a05b0 66 6f 72 6d 61 74 20 66 69 65 6c 64 20 2a 2f 0a  format field */.
a05c0 20 20 63 68 61 72 20 66 6d 74 74 79 70 65 3b 20    char fmttype; 
a05d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
a05e0 65 20 66 6f 72 6d 61 74 20 66 69 65 6c 64 20 63  e format field c
a05f0 6f 64 65 20 6c 65 74 74 65 72 20 2a 2f 0a 20 20  ode letter */.  
a0600 65 74 42 79 74 65 20 62 61 73 65 3b 20 20 20 20  etByte base;    
a0610 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
a0620 62 61 73 65 20 66 6f 72 20 72 61 64 69 78 20 63  base for radix c
a0630 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 65  onversion */.  e
a0640 74 42 79 74 65 20 66 6c 61 67 73 3b 20 20 20 20  tByte flags;    
a0650 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
a0660 72 20 6d 6f 72 65 20 6f 66 20 46 4c 41 47 5f 20  r more of FLAG_ 
a0670 63 6f 6e 73 74 61 6e 74 73 20 62 65 6c 6f 77 20  constants below 
a0680 2a 2f 0a 20 20 65 74 42 79 74 65 20 74 79 70 65  */.  etByte type
a0690 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
a06a0 20 43 6f 6e 76 65 72 73 69 6f 6e 20 70 61 72 61   Conversion para
a06b0 64 69 67 6d 20 2a 2f 0a 20 20 65 74 42 79 74 65  digm */.  etByte
a06c0 20 63 68 61 72 73 65 74 3b 20 20 20 20 20 20 20   charset;       
a06d0 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74     /* Offset int
a06e0 6f 20 61 44 69 67 69 74 73 5b 5d 20 6f 66 20 74  o aDigits[] of t
a06f0 68 65 20 64 69 67 69 74 73 20 73 74 72 69 6e 67  he digits string
a0700 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 70 72 65   */.  etByte pre
a0710 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f  fix;           /
a0720 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 50  * Offset into aP
a0730 72 65 66 69 78 5b 5d 20 6f 66 20 74 68 65 20 70  refix[] of the p
a0740 72 65 66 69 78 20 73 74 72 69 6e 67 20 2a 2f 0a  refix string */.
a0750 7d 20 65 74 5f 69 6e 66 6f 3b 0a 0a 2f 2a 0a 2a  } et_info;../*.*
a0760 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73  * Allowed values
a0770 20 66 6f 72 20 65 74 5f 69 6e 66 6f 2e 66 6c 61   for et_info.fla
a0780 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4c  gs.*/.#define FL
a0790 41 47 5f 53 49 47 4e 45 44 20 20 31 20 20 20 20  AG_SIGNED  1    
a07a0 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
a07b0 76 61 6c 75 65 20 74 6f 20 63 6f 6e 76 65 72 74  value to convert
a07c0 20 69 73 20 73 69 67 6e 65 64 20 2a 2f 0a 23 64   is signed */.#d
a07d0 65 66 69 6e 65 20 46 4c 41 47 5f 49 4e 54 45 52  efine FLAG_INTER
a07e0 4e 20 20 32 20 20 20 20 20 2f 2a 20 54 72 75 65  N  2     /* True
a07f0 20 69 66 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c   if for internal
a0800 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65   use only */.#de
a0810 66 69 6e 65 20 46 4c 41 47 5f 53 54 52 49 4e 47  fine FLAG_STRING
a0820 20 20 34 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 77    4     /* Allow
a0830 20 69 6e 66 69 6e 69 74 79 20 70 72 65 63 69 73   infinity precis
a0840 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54  ion */.../*.** T
a0850 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62  he following tab
a0860 6c 65 20 69 73 20 73 65 61 72 63 68 65 64 20 6c  le is searched l
a0870 69 6e 65 61 72 6c 79 2c 20 73 6f 20 69 74 20 69  inearly, so it i
a0880 73 20 67 6f 6f 64 20 74 6f 20 70 75 74 20 74 68  s good to put th
a0890 65 0a 2a 2a 20 6d 6f 73 74 20 66 72 65 71 75 65  e.** most freque
a08a0 6e 74 6c 79 20 75 73 65 64 20 63 6f 6e 76 65 72  ntly used conver
a08b0 73 69 6f 6e 20 74 79 70 65 73 20 66 69 72 73 74  sion types first
a08c0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
a08d0 74 20 63 68 61 72 20 61 44 69 67 69 74 73 5b 5d  t char aDigits[]
a08e0 20 3d 20 22 30 31 32 33 34 35 36 37 38 39 41 42   = "0123456789AB
a08f0 43 44 45 46 30 31 32 33 34 35 36 37 38 39 61 62  CDEF0123456789ab
a0900 63 64 65 66 22 3b 0a 73 74 61 74 69 63 20 63 6f  cdef";.static co
a0910 6e 73 74 20 63 68 61 72 20 61 50 72 65 66 69 78  nst char aPrefix
a0920 5b 5d 20 3d 20 22 2d 78 30 5c 30 30 30 58 30 22  [] = "-x0\000X0"
a0930 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 65  ;.static const e
a0940 74 5f 69 6e 66 6f 20 66 6d 74 69 6e 66 6f 5b 5d  t_info fmtinfo[]
a0950 20 3d 20 7b 0a 20 20 7b 20 20 27 64 27 2c 20 31   = {.  {  'd', 1
a0960 30 2c 20 31 2c 20 65 74 52 41 44 49 58 2c 20 20  0, 1, etRADIX,  
a0970 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b      0,  0 },.  {
a0980 20 20 27 73 27 2c 20 20 30 2c 20 34 2c 20 65 74    's',  0, 4, et
a0990 53 54 52 49 4e 47 2c 20 20 20 20 20 30 2c 20 20  STRING,     0,  
a09a0 30 20 7d 2c 0a 20 20 7b 20 20 27 67 27 2c 20 20  0 },.  {  'g',  
a09b0 30 2c 20 31 2c 20 65 74 47 45 4e 45 52 49 43 2c  0, 1, etGENERIC,
a09c0 20 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20 20 7b      30, 0 },.  {
a09d0 20 20 27 7a 27 2c 20 20 30 2c 20 34 2c 20 65 74    'z',  0, 4, et
a09e0 44 59 4e 53 54 52 49 4e 47 2c 20 20 30 2c 20 20  DYNSTRING,  0,  
a09f0 30 20 7d 2c 0a 20 20 7b 20 20 27 71 27 2c 20 20  0 },.  {  'q',  
a0a00 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 41 50  0, 4, etSQLESCAP
a0a10 45 2c 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b  E,  0,  0 },.  {
a0a20 20 20 27 51 27 2c 20 20 30 2c 20 34 2c 20 65 74    'Q',  0, 4, et
a0a30 53 51 4c 45 53 43 41 50 45 32 2c 20 30 2c 20 20  SQLESCAPE2, 0,  
a0a40 30 20 7d 2c 0a 20 20 7b 20 20 27 77 27 2c 20 20  0 },.  {  'w',  
a0a50 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 41 50  0, 4, etSQLESCAP
a0a60 45 33 2c 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b  E3, 0,  0 },.  {
a0a70 20 20 27 63 27 2c 20 20 30 2c 20 30 2c 20 65 74    'c',  0, 0, et
a0a80 43 48 41 52 58 2c 20 20 20 20 20 20 30 2c 20 20  CHARX,      0,  
a0a90 30 20 7d 2c 0a 20 20 7b 20 20 27 6f 27 2c 20 20  0 },.  {  'o',  
a0aa0 38 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 20 20  8, 0, etRADIX,  
a0ab0 20 20 20 20 30 2c 20 20 32 20 7d 2c 0a 20 20 7b      0,  2 },.  {
a0ac0 20 20 27 75 27 2c 20 31 30 2c 20 30 2c 20 65 74    'u', 10, 0, et
a0ad0 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c 20 20  RADIX,      0,  
a0ae0 30 20 7d 2c 0a 20 20 7b 20 20 27 78 27 2c 20 31  0 },.  {  'x', 1
a0af0 36 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 20 20  6, 0, etRADIX,  
a0b00 20 20 20 20 31 36 2c 20 31 20 7d 2c 0a 20 20 7b      16, 1 },.  {
a0b10 20 20 27 58 27 2c 20 31 36 2c 20 30 2c 20 65 74    'X', 16, 0, et
a0b20 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c 20 20  RADIX,      0,  
a0b30 34 20 7d 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c  4 },.#ifndef SQL
a0b40 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
a0b50 47 5f 50 4f 49 4e 54 0a 20 20 7b 20 20 27 66 27  G_POINT.  {  'f'
a0b60 2c 20 20 30 2c 20 31 2c 20 65 74 46 4c 4f 41 54  ,  0, 1, etFLOAT
a0b70 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a  ,      0,  0 },.
a0b80 20 20 7b 20 20 27 65 27 2c 20 20 30 2c 20 31 2c    {  'e',  0, 1,
a0b90 20 65 74 45 58 50 2c 20 20 20 20 20 20 20 20 33   etEXP,        3
a0ba0 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27 45 27  0, 0 },.  {  'E'
a0bb0 2c 20 20 30 2c 20 31 2c 20 65 74 45 58 50 2c 20  ,  0, 1, etEXP, 
a0bc0 20 20 20 20 20 20 20 31 34 2c 20 30 20 7d 2c 0a         14, 0 },.
a0bd0 20 20 7b 20 20 27 47 27 2c 20 20 30 2c 20 31 2c    {  'G',  0, 1,
a0be0 20 65 74 47 45 4e 45 52 49 43 2c 20 20 20 20 31   etGENERIC,    1
a0bf0 34 2c 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 20  4, 0 },.#endif. 
a0c00 20 7b 20 20 27 69 27 2c 20 31 30 2c 20 31 2c 20   {  'i', 10, 1, 
a0c10 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c  etRADIX,      0,
a0c20 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6e 27 2c    0 },.  {  'n',
a0c30 20 20 30 2c 20 30 2c 20 65 74 53 49 5a 45 2c 20    0, 0, etSIZE, 
a0c40 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20        0,  0 },. 
a0c50 20 7b 20 20 27 25 27 2c 20 20 30 2c 20 30 2c 20   {  '%',  0, 0, 
a0c60 65 74 50 45 52 43 45 4e 54 2c 20 20 20 20 30 2c  etPERCENT,    0,
a0c70 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 70 27 2c    0 },.  {  'p',
a0c80 20 31 36 2c 20 30 2c 20 65 74 50 4f 49 4e 54 45   16, 0, etPOINTE
a0c90 52 2c 20 20 20 20 30 2c 20 20 31 20 7d 2c 0a 20  R,    0,  1 },. 
a0ca0 20 7b 20 20 27 54 27 2c 20 20 30 2c 20 32 2c 20   {  'T',  0, 2, 
a0cb0 65 74 54 4f 4b 45 4e 2c 20 20 20 20 20 20 30 2c  etTOKEN,      0,
a0cc0 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 53 27 2c    0 },.  {  'S',
a0cd0 20 20 30 2c 20 32 2c 20 65 74 53 52 43 4c 49 53    0, 2, etSRCLIS
a0ce0 54 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20  T,    0,  0 },. 
a0cf0 20 7b 20 20 27 72 27 2c 20 31 30 2c 20 33 2c 20   {  'r', 10, 3, 
a0d00 65 74 4f 52 44 49 4e 41 4c 2c 20 20 20 20 30 2c  etORDINAL,    0,
a0d10 20 20 30 20 7d 2c 0a 7d 3b 0a 23 64 65 66 69 6e    0 },.};.#defin
a0d20 65 20 65 74 4e 49 4e 46 4f 20 20 28 73 69 7a 65  e etNINFO  (size
a0d30 6f 66 28 66 6d 74 69 6e 66 6f 29 2f 73 69 7a 65  of(fmtinfo)/size
a0d40 6f 66 28 66 6d 74 69 6e 66 6f 5b 30 5d 29 29 0a  of(fmtinfo[0])).
a0d50 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45  ./*.** If SQLITE
a0d60 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
a0d70 4f 49 4e 54 20 69 73 20 64 65 66 69 6e 65 64 2c  OINT is defined,
a0d80 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 68   then none of th
a0d90 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  e floating point
a0da0 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20  .** conversions 
a0db0 77 69 6c 6c 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 69  will work..*/.#i
a0dc0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
a0dd0 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
a0de0 0a 2f 2a 0a 2a 2a 20 22 2a 76 61 6c 22 20 69 73  ./*.** "*val" is
a0df0 20 61 20 64 6f 75 62 6c 65 20 73 75 63 68 20 74   a double such t
a0e00 68 61 74 20 30 2e 31 20 3c 3d 20 2a 76 61 6c 20  hat 0.1 <= *val 
a0e10 3c 20 31 30 2e 30 0a 2a 2a 20 52 65 74 75 72 6e  < 10.0.** Return
a0e20 20 74 68 65 20 61 73 63 69 69 20 63 6f 64 65 20   the ascii code 
a0e30 66 6f 72 20 74 68 65 20 6c 65 61 64 69 6e 67 20  for the leading 
a0e40 64 69 67 69 74 20 6f 66 20 2a 76 61 6c 2c 20 74  digit of *val, t
a0e50 68 65 6e 0a 2a 2a 20 6d 75 6c 74 69 70 6c 79 20  hen.** multiply 
a0e60 22 2a 76 61 6c 22 20 62 79 20 31 30 2e 30 20 74  "*val" by 10.0 t
a0e70 6f 20 72 65 6e 6f 72 6d 61 6c 69 7a 65 2e 0a 2a  o renormalize..*
a0e80 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a  *.** Example:.**
a0e90 20 20 20 20 20 69 6e 70 75 74 3a 20 20 20 20 20       input:     
a0ea0 2a 76 61 6c 20 3d 20 33 2e 31 34 31 35 39 0a 2a  *val = 3.14159.*
a0eb0 2a 20 20 20 20 20 6f 75 74 70 75 74 3a 20 20 20  *     output:   
a0ec0 20 2a 76 61 6c 20 3d 20 31 2e 34 31 35 39 20 20   *val = 1.4159  
a0ed0 20 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72    function retur
a0ee0 6e 20 3d 20 27 33 27 0a 2a 2a 0a 2a 2a 20 54 68  n = '3'.**.** Th
a0ef0 65 20 63 6f 75 6e 74 65 72 20 2a 63 6e 74 20 69  e counter *cnt i
a0f00 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 61  s incremented ea
a0f10 63 68 20 74 69 6d 65 2e 20 20 41 66 74 65 72 20  ch time.  After 
a0f20 63 6f 75 6e 74 65 72 20 65 78 63 65 65 64 73 0a  counter exceeds.
a0f30 2a 2a 20 31 36 20 28 74 68 65 20 6e 75 6d 62 65  ** 16 (the numbe
a0f40 72 20 6f 66 20 73 69 67 6e 69 66 69 63 61 6e 74  r of significant
a0f50 20 64 69 67 69 74 73 20 69 6e 20 61 20 36 34 2d   digits in a 64-
a0f60 62 69 74 20 66 6c 6f 61 74 29 20 27 30 27 20 69  bit float) '0' i
a0f70 73 0a 2a 2a 20 61 6c 77 61 79 73 20 72 65 74 75  s.** always retu
a0f80 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rned..*/.static 
a0f90 69 6e 74 20 65 74 5f 67 65 74 64 69 67 69 74 28  int et_getdigit(
a0fa0 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20  LONGDOUBLE_TYPE 
a0fb0 2a 76 61 6c 2c 20 69 6e 74 20 2a 63 6e 74 29 7b  *val, int *cnt){
a0fc0 0a 20 20 69 6e 74 20 64 69 67 69 74 3b 0a 20 20  .  int digit;.  
a0fd0 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20  LONGDOUBLE_TYPE 
a0fe0 64 3b 0a 20 20 69 66 28 20 28 2a 63 6e 74 29 2b  d;.  if( (*cnt)+
a0ff0 2b 20 3e 3d 20 31 36 20 29 20 72 65 74 75 72 6e  + >= 16 ) return
a1000 20 27 30 27 3b 0a 20 20 64 69 67 69 74 20 3d 20   '0';.  digit = 
a1010 28 69 6e 74 29 2a 76 61 6c 3b 0a 20 20 64 20 3d  (int)*val;.  d =
a1020 20 64 69 67 69 74 3b 0a 20 20 64 69 67 69 74 20   digit;.  digit 
a1030 2b 3d 20 27 30 27 3b 0a 20 20 2a 76 61 6c 20 3d  += '0';.  *val =
a1040 20 28 2a 76 61 6c 20 2d 20 64 29 2a 31 30 2e 30   (*val - d)*10.0
a1050 3b 0a 20 20 72 65 74 75 72 6e 20 64 69 67 69 74  ;.  return digit
a1060 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
a1070 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
a1080 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 0a 2f 2a 0a  NG_POINT */../*.
a1090 2a 2a 20 41 70 70 65 6e 64 20 4e 20 73 70 61 63  ** Append N spac
a10a0 65 20 63 68 61 72 61 63 74 65 72 73 20 74 6f 20  e characters to 
a10b0 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67  the given string
a10c0 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74   buffer..*/.stat
a10d0 69 63 20 76 6f 69 64 20 61 70 70 65 6e 64 53 70  ic void appendSp
a10e0 61 63 65 28 53 74 72 41 63 63 75 6d 20 2a 70 41  ace(StrAccum *pA
a10f0 63 63 75 6d 2c 20 69 6e 74 20 4e 29 7b 0a 20 20  ccum, int N){.  
a1100 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
a1110 72 20 7a 53 70 61 63 65 73 5b 5d 20 3d 20 22 20  r zSpaces[] = " 
a1120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1130 20 20 20 20 20 20 20 20 20 20 20 20 22 3b 0a 20              ";. 
a1140 20 77 68 69 6c 65 28 20 4e 3e 3d 73 69 7a 65 6f   while( N>=sizeo
a1150 66 28 7a 53 70 61 63 65 73 29 2d 31 20 29 7b 0a  f(zSpaces)-1 ){.
a1160 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63      sqlite3StrAc
a1170 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d  cumAppend(pAccum
a1180 2c 20 7a 53 70 61 63 65 73 2c 20 73 69 7a 65 6f  , zSpaces, sizeo
a1190 66 28 7a 53 70 61 63 65 73 29 2d 31 29 3b 0a 20  f(zSpaces)-1);. 
a11a0 20 20 20 4e 20 2d 3d 20 73 69 7a 65 6f 66 28 7a     N -= sizeof(z
a11b0 53 70 61 63 65 73 29 2d 31 3b 0a 20 20 7d 0a 20  Spaces)-1;.  }. 
a11c0 20 69 66 28 20 4e 3e 30 20 29 7b 0a 20 20 20 20   if( N>0 ){.    
a11d0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41  sqlite3StrAccumA
a11e0 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a 53  ppend(pAccum, zS
a11f0 70 61 63 65 73 2c 20 4e 29 3b 0a 20 20 7d 0a 7d  paces, N);.  }.}
a1200 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 6d 61 63 68 69  ../*.** On machi
a1210 6e 65 73 20 77 69 74 68 20 61 20 73 6d 61 6c 6c  nes with a small
a1220 20 73 74 61 63 6b 20 73 69 7a 65 2c 20 79 6f 75   stack size, you
a1230 20 63 61 6e 20 72 65 64 65 66 69 6e 65 20 74 68   can redefine th
a1240 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 50 52 49 4e  e.** SQLITE_PRIN
a1250 54 5f 42 55 46 5f 53 49 5a 45 20 74 6f 20 62 65  T_BUF_SIZE to be
a1260 20 6c 65 73 73 20 74 68 61 6e 20 33 35 30 2e 20   less than 350. 
a1270 20 42 75 74 20 62 65 77 61 72 65 20 2d 20 66 6f   But beware - fo
a1280 72 0a 2a 2a 20 73 6d 61 6c 6c 65 72 20 76 61 6c  r.** smaller val
a1290 75 65 73 20 73 6f 6d 65 20 25 66 20 63 6f 6e 76  ues some %f conv
a12a0 65 72 73 69 6f 6e 73 20 6d 61 79 20 67 6f 20 69  ersions may go i
a12b0 6e 74 6f 20 61 6e 20 69 6e 66 69 6e 69 74 65 20  nto an infinite 
a12c0 6c 6f 6f 70 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  loop..*/.#ifndef
a12d0 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55   SQLITE_PRINT_BU
a12e0 46 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20  F_SIZE.# define 
a12f0 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46  SQLITE_PRINT_BUF
a1300 5f 53 49 5a 45 20 33 35 30 0a 23 65 6e 64 69 66  _SIZE 350.#endif
a1310 0a 23 64 65 66 69 6e 65 20 65 74 42 55 46 53 49  .#define etBUFSI
a1320 5a 45 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f  ZE SQLITE_PRINT_
a1330 42 55 46 5f 53 49 5a 45 20 20 2f 2a 20 53 69 7a  BUF_SIZE  /* Siz
a1340 65 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 20  e of the output 
a1350 62 75 66 66 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  buffer */../*.**
a1360 20 54 68 65 20 72 6f 6f 74 20 70 72 6f 67 72 61   The root progra
a1370 6d 2e 20 20 41 6c 6c 20 76 61 72 69 61 74 69 6f  m.  All variatio
a1380 6e 73 20 63 61 6c 6c 20 74 68 69 73 20 63 6f 72  ns call this cor
a1390 65 2e 0a 2a 2a 0a 2a 2a 20 49 4e 50 55 54 53 3a  e..**.** INPUTS:
a13a0 0a 2a 2a 20 20 20 66 75 6e 63 20 20 20 54 68 69  .**   func   Thi
a13b0 73 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  s is a pointer t
a13c0 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b  o a function tak
a13d0 69 6e 67 20 74 68 72 65 65 20 61 72 67 75 6d 65  ing three argume
a13e0 6e 74 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  nts.**          
a13f0 20 20 31 2e 20 41 20 70 6f 69 6e 74 65 72 20 74    1. A pointer t
a1400 6f 20 61 6e 79 74 68 69 6e 67 2e 20 20 53 61 6d  o anything.  Sam
a1410 65 20 61 73 20 74 68 65 20 22 61 72 67 22 20 70  e as the "arg" p
a1420 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 20 20 20  arameter..**    
a1430 20 20 20 20 20 20 20 20 32 2e 20 41 20 70 6f 69          2. A poi
a1440 6e 74 65 72 20 74 6f 20 74 68 65 20 6c 69 73 74  nter to the list
a1450 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 74   of characters t
a1460 6f 20 62 65 20 6f 75 74 70 75 74 0a 2a 2a 20 20  o be output.**  
a1470 20 20 20 20 20 20 20 20 20 20 20 20 20 28 4e 6f               (No
a1480 74 65 2c 20 74 68 69 73 20 6c 69 73 74 20 69 73  te, this list is
a1490 20 4e 4f 54 20 6e 75 6c 6c 20 74 65 72 6d 69 6e   NOT null termin
a14a0 61 74 65 64 2e 29 0a 2a 2a 20 20 20 20 20 20 20  ated.).**       
a14b0 20 20 20 20 20 33 2e 20 41 6e 20 69 6e 74 65 67       3. An integ
a14c0 65 72 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61  er number of cha
a14d0 72 61 63 74 65 72 73 20 74 6f 20 62 65 20 6f 75  racters to be ou
a14e0 74 70 75 74 2e 0a 2a 2a 20 20 20 20 20 20 20 20  tput..**        
a14f0 20 20 20 20 20 20 20 28 4e 6f 74 65 3a 20 54 68         (Note: Th
a1500 69 73 20 6e 75 6d 62 65 72 20 6d 69 67 68 74 20  is number might 
a1510 62 65 20 7a 65 72 6f 2e 29 0a 2a 2a 0a 2a 2a 20  be zero.).**.** 
a1520 20 20 61 72 67 20 20 20 20 54 68 69 73 20 69 73    arg    This is
a1530 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20   the pointer to 
a1540 61 6e 79 74 68 69 6e 67 20 77 68 69 63 68 20 77  anything which w
a1550 69 6c 6c 20 62 65 20 70 61 73 73 65 64 20 61 73  ill be passed as
a1560 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
a1570 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
a1580 74 6f 20 22 66 75 6e 63 22 2e 20 20 55 73 65 20  to "func".  Use 
a1590 69 74 20 66 6f 72 20 77 68 61 74 65 76 65 72 20  it for whatever 
a15a0 79 6f 75 20 6c 69 6b 65 2e 0a 2a 2a 0a 2a 2a 20  you like..**.** 
a15b0 20 20 66 6d 74 20 20 20 20 54 68 69 73 20 69 73    fmt    This is
a15c0 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69   the format stri
a15d0 6e 67 2c 20 61 73 20 69 6e 20 74 68 65 20 75 73  ng, as in the us
a15e0 75 61 6c 20 70 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a  ual print..**.**
a15f0 20 20 20 61 70 20 20 20 20 20 54 68 69 73 20 69     ap     This i
a1600 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
a1610 20 6c 69 73 74 20 6f 66 20 61 72 67 75 6d 65 6e   list of argumen
a1620 74 73 2e 20 20 53 61 6d 65 20 61 73 20 69 6e 0a  ts.  Same as in.
a1630 2a 2a 20 20 20 20 20 20 20 20 20 20 76 66 70 72  **          vfpr
a1640 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 55 54 50 55  int..**.** OUTPU
a1650 54 53 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  TS:.**          
a1660 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
a1670 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75   is the total nu
a1680 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65  mber of characte
a1690 72 73 20 73 65 6e 74 20 74 6f 0a 2a 2a 20 20 20  rs sent to.**   
a16a0 20 20 20 20 20 20 20 74 68 65 20 66 75 6e 63 74         the funct
a16b0 69 6f 6e 20 22 66 75 6e 63 22 2e 20 20 52 65 74  ion "func".  Ret
a16c0 75 72 6e 73 20 2d 31 20 6f 6e 20 61 20 65 72 72  urns -1 on a err
a16d0 6f 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74  or..**.** Note t
a16e0 68 61 74 20 74 68 65 20 6f 72 64 65 72 20 69 6e  hat the order in
a16f0 20 77 68 69 63 68 20 61 75 74 6f 6d 61 74 69 63   which automatic
a1700 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 64   variables are d
a1710 65 63 6c 61 72 65 64 20 62 65 6c 6f 77 0a 2a 2a  eclared below.**
a1720 20 73 65 65 6d 73 20 74 6f 20 6d 61 6b 65 20 61   seems to make a
a1730 20 62 69 67 20 64 69 66 66 65 72 65 6e 63 65 20   big difference 
a1740 69 6e 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 68  in determining h
a1750 6f 77 20 66 61 73 74 20 74 68 69 73 20 62 65 61  ow fast this bea
a1760 73 74 0a 2a 2a 20 77 69 6c 6c 20 72 75 6e 2e 0a  st.** will run..
a1770 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
a1780 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 58  E void sqlite3VX
a1790 50 72 69 6e 74 66 28 0a 20 20 53 74 72 41 63 63  Printf(.  StrAcc
a17a0 75 6d 20 2a 70 41 63 63 75 6d 2c 20 20 20 20 20  um *pAccum,     
a17b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a17c0 41 63 63 75 6d 75 6c 61 74 65 20 72 65 73 75 6c  Accumulate resul
a17d0 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  ts here */.  int
a17e0 20 75 73 65 45 78 74 65 6e 64 65 64 2c 20 20 20   useExtended,   
a17f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1800 2f 2a 20 41 6c 6c 6f 77 20 65 78 74 65 6e 64 65  /* Allow extende
a1810 64 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 73 20  d %-conversions 
a1820 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
a1830 2a 66 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20  *fmt,           
a1840 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 6d 61          /* Forma
a1850 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 76 61  t string */.  va
a1860 5f 6c 69 73 74 20 61 70 20 20 20 20 20 20 20 20  _list ap        
a1870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1880 20 2f 2a 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f   /* arguments */
a1890 0a 29 7b 0a 20 20 69 6e 74 20 63 3b 20 20 20 20  .){.  int c;    
a18a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a18b0 20 2f 2a 20 4e 65 78 74 20 63 68 61 72 61 63 74   /* Next charact
a18c0 65 72 20 69 6e 20 74 68 65 20 66 6f 72 6d 61 74  er in the format
a18d0 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63 68 61   string */.  cha
a18e0 72 20 2a 62 75 66 70 74 3b 20 20 20 20 20 20 20  r *bufpt;       
a18f0 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
a1900 65 72 20 74 6f 20 74 68 65 20 63 6f 6e 76 65 72  er to the conver
a1910 73 69 6f 6e 20 62 75 66 66 65 72 20 2a 2f 0a 20  sion buffer */. 
a1920 20 69 6e 74 20 70 72 65 63 69 73 69 6f 6e 3b 20   int precision; 
a1930 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
a1940 72 65 63 69 73 69 6f 6e 20 6f 66 20 74 68 65 20  recision of the 
a1950 63 75 72 72 65 6e 74 20 66 69 65 6c 64 20 2a 2f  current field */
a1960 0a 20 20 69 6e 74 20 6c 65 6e 67 74 68 3b 20 20  .  int length;  
a1970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a1980 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 66   Length of the f
a1990 69 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 69 64  ield */.  int id
a19a0 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
a19b0 20 20 20 20 20 2f 2a 20 41 20 67 65 6e 65 72 61       /* A genera
a19c0 6c 20 70 75 72 70 6f 73 65 20 6c 6f 6f 70 20 63  l purpose loop c
a19d0 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  ounter */.  int 
a19e0 77 69 64 74 68 3b 20 20 20 20 20 20 20 20 20 20  width;          
a19f0 20 20 20 20 20 20 20 2f 2a 20 57 69 64 74 68 20         /* Width 
a1a00 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 66  of the current f
a1a10 69 65 6c 64 20 2a 2f 0a 20 20 65 74 42 79 74 65  ield */.  etByte
a1a20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66   flag_leftjustif
a1a30 79 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20  y;   /* True if 
a1a40 22 2d 22 20 66 6c 61 67 20 69 73 20 70 72 65 73  "-" flag is pres
a1a50 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20  ent */.  etByte 
a1a60 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 3b 20 20  flag_plussign;  
a1a70 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22      /* True if "
a1a80 2b 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65  +" flag is prese
a1a90 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66  nt */.  etByte f
a1aa0 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 3b 20 20  lag_blanksign;  
a1ab0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 20     /* True if " 
a1ac0 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e  " flag is presen
a1ad0 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c  t */.  etByte fl
a1ae0 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d  ag_alternateform
a1af0 3b 20 2f 2a 20 54 72 75 65 20 69 66 20 22 23 22  ; /* True if "#"
a1b00 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74   flag is present
a1b10 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61   */.  etByte fla
a1b20 67 5f 61 6c 74 66 6f 72 6d 32 3b 20 20 20 20 20  g_altform2;     
a1b30 20 2f 2a 20 54 72 75 65 20 69 66 20 22 21 22 20   /* True if "!" 
a1b40 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 20  flag is present 
a1b50 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67  */.  etByte flag
a1b60 5f 7a 65 72 6f 70 61 64 3b 20 20 20 20 20 20 20  _zeropad;       
a1b70 2f 2a 20 54 72 75 65 20 69 66 20 66 69 65 6c 64  /* True if field
a1b80 20 77 69 64 74 68 20 63 6f 6e 73 74 61 6e 74 20   width constant 
a1b90 73 74 61 72 74 73 20 77 69 74 68 20 7a 65 72 6f  starts with zero
a1ba0 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61   */.  etByte fla
a1bb0 67 5f 6c 6f 6e 67 3b 20 20 20 20 20 20 20 20 20  g_long;         
a1bc0 20 2f 2a 20 54 72 75 65 20 69 66 20 22 6c 22 20   /* True if "l" 
a1bd0 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 20  flag is present 
a1be0 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67  */.  etByte flag
a1bf0 5f 6c 6f 6e 67 6c 6f 6e 67 3b 20 20 20 20 20 20  _longlong;      
a1c00 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 22  /* True if the "
a1c10 6c 6c 22 20 66 6c 61 67 20 69 73 20 70 72 65 73  ll" flag is pres
a1c20 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20  ent */.  etByte 
a1c30 64 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20  done;           
a1c40 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 65 72 6d      /* Loop term
a1c50 69 6e 61 74 69 6f 6e 20 66 6c 61 67 20 2a 2f 0a  ination flag */.
a1c60 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20    sqlite_uint64 
a1c70 6c 6f 6e 67 76 61 6c 75 65 3b 20 20 20 2f 2a 20  longvalue;   /* 
a1c80 56 61 6c 75 65 20 66 6f 72 20 69 6e 74 65 67 65  Value for intege
a1c90 72 20 74 79 70 65 73 20 2a 2f 0a 20 20 4c 4f 4e  r types */.  LON
a1ca0 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 72 65 61  GDOUBLE_TYPE rea
a1cb0 6c 76 61 6c 75 65 3b 20 2f 2a 20 56 61 6c 75 65  lvalue; /* Value
a1cc0 20 66 6f 72 20 72 65 61 6c 20 74 79 70 65 73 20   for real types 
a1cd0 2a 2f 0a 20 20 63 6f 6e 73 74 20 65 74 5f 69 6e  */.  const et_in
a1ce0 66 6f 20 2a 69 6e 66 6f 70 3b 20 20 20 20 20 20  fo *infop;      
a1cf0 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68  /* Pointer to th
a1d00 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 69 6e  e appropriate in
a1d10 66 6f 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  fo structure */.
a1d20 20 20 63 68 61 72 20 62 75 66 5b 65 74 42 55 46    char buf[etBUF
a1d30 53 49 5a 45 5d 3b 20 20 20 20 20 20 20 2f 2a 20  SIZE];       /* 
a1d40 43 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66 66 65  Conversion buffe
a1d50 72 20 2a 2f 0a 20 20 63 68 61 72 20 70 72 65 66  r */.  char pref
a1d60 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ix;             
a1d70 20 20 2f 2a 20 50 72 65 66 69 78 20 63 68 61 72    /* Prefix char
a1d80 61 63 74 65 72 2e 20 20 22 2b 22 20 6f 72 20 22  acter.  "+" or "
a1d90 2d 22 20 6f 72 20 22 20 22 20 6f 72 20 27 5c 30  -" or " " or '\0
a1da0 27 2e 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 78  '. */.  etByte x
a1db0 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20  type;           
a1dc0 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e     /* Conversion
a1dd0 20 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20 20 63   paradigm */.  c
a1de0 68 61 72 20 2a 7a 45 78 74 72 61 3b 20 20 20 20  har *zExtra;    
a1df0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74            /* Ext
a1e00 72 61 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 66  ra memory used f
a1e10 6f 72 20 65 74 54 43 4c 45 53 43 41 50 45 20 63  or etTCLESCAPE c
a1e20 6f 6e 76 65 72 73 69 6f 6e 73 20 2a 2f 0a 23 69  onversions */.#i
a1e30 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
a1e40 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
a1e50 0a 20 20 69 6e 74 20 20 65 78 70 2c 20 65 32 3b  .  int  exp, e2;
a1e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a1e70 20 65 78 70 6f 6e 65 6e 74 20 6f 66 20 72 65 61   exponent of rea
a1e80 6c 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20 64  l numbers */.  d
a1e90 6f 75 62 6c 65 20 72 6f 75 6e 64 65 72 3b 20 20  ouble rounder;  
a1ea0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
a1eb0 64 20 66 6f 72 20 72 6f 75 6e 64 69 6e 67 20 66  d for rounding f
a1ec0 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61  loating point va
a1ed0 6c 75 65 73 20 2a 2f 0a 20 20 65 74 42 79 74 65  lues */.  etByte
a1ee0 20 66 6c 61 67 5f 64 70 3b 20 20 20 20 20 20 20   flag_dp;       
a1ef0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
a1f00 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 73 68  decimal point sh
a1f10 6f 75 6c 64 20 62 65 20 73 68 6f 77 6e 20 2a 2f  ould be shown */
a1f20 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 72  .  etByte flag_r
a1f30 74 7a 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  tz;           /*
a1f40 20 54 72 75 65 20 69 66 20 74 72 61 69 6c 69 6e   True if trailin
a1f50 67 20 7a 65 72 6f 73 20 73 68 6f 75 6c 64 20 62  g zeros should b
a1f60 65 20 72 65 6d 6f 76 65 64 20 2a 2f 0a 20 20 65  e removed */.  e
a1f70 74 42 79 74 65 20 66 6c 61 67 5f 65 78 70 3b 20  tByte flag_exp; 
a1f80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
a1f90 65 20 74 6f 20 66 6f 72 63 65 20 64 69 73 70 6c  e to force displ
a1fa0 61 79 20 6f 66 20 74 68 65 20 65 78 70 6f 6e 65  ay of the expone
a1fb0 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 73 64 3b  nt */.  int nsd;
a1fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1fd0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
a1fe0 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69  significant digi
a1ff0 74 73 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a 23  ts returned */.#
a2000 65 6e 64 69 66 0a 0a 20 20 6c 65 6e 67 74 68 20  endif..  length 
a2010 3d 20 30 3b 0a 20 20 62 75 66 70 74 20 3d 20 30  = 0;.  bufpt = 0
a2020 3b 0a 20 20 66 6f 72 28 3b 20 28 63 3d 28 2a 66  ;.  for(; (c=(*f
a2030 6d 74 29 29 21 3d 30 3b 20 2b 2b 66 6d 74 29 7b  mt))!=0; ++fmt){
a2040 0a 20 20 20 20 69 66 28 20 63 21 3d 27 25 27 20  .    if( c!='%' 
a2050 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 6d 74  ){.      int amt
a2060 3b 0a 20 20 20 20 20 20 62 75 66 70 74 20 3d 20  ;.      bufpt = 
a2070 28 63 68 61 72 20 2a 29 66 6d 74 3b 0a 20 20 20  (char *)fmt;.   
a2080 20 20 20 61 6d 74 20 3d 20 31 3b 0a 20 20 20 20     amt = 1;.    
a2090 20 20 77 68 69 6c 65 28 20 28 63 3d 28 2a 2b 2b    while( (c=(*++
a20a0 66 6d 74 29 29 21 3d 27 25 27 20 26 26 20 63 21  fmt))!='%' && c!
a20b0 3d 30 20 29 20 61 6d 74 2b 2b 3b 0a 20 20 20 20  =0 ) amt++;.    
a20c0 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75    sqlite3StrAccu
a20d0 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20  mAppend(pAccum, 
a20e0 62 75 66 70 74 2c 20 61 6d 74 29 3b 0a 20 20 20  bufpt, amt);.   
a20f0 20 20 20 69 66 28 20 63 3d 3d 30 20 29 20 62 72     if( c==0 ) br
a2100 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  eak;.    }.    i
a2110 66 28 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 3d  f( (c=(*++fmt))=
a2120 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
a2130 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
a2140 64 28 70 41 63 63 75 6d 2c 20 22 25 22 2c 20 31  d(pAccum, "%", 1
a2150 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
a2160 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 46 69 6e      }.    /* Fin
a2170 64 20 6f 75 74 20 77 68 61 74 20 66 6c 61 67 73  d out what flags
a2180 20 61 72 65 20 70 72 65 73 65 6e 74 20 2a 2f 0a   are present */.
a2190 20 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73      flag_leftjus
a21a0 74 69 66 79 20 3d 20 66 6c 61 67 5f 70 6c 75 73  tify = flag_plus
a21b0 73 69 67 6e 20 3d 20 66 6c 61 67 5f 62 6c 61 6e  sign = flag_blan
a21c0 6b 73 69 67 6e 20 3d 20 0a 20 20 20 20 20 66 6c  ksign = .     fl
a21d0 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d  ag_alternateform
a21e0 20 3d 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32   = flag_altform2
a21f0 20 3d 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20   = flag_zeropad 
a2200 3d 20 30 3b 0a 20 20 20 20 64 6f 6e 65 20 3d 20  = 0;.    done = 
a2210 30 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20  0;.    do{.     
a2220 20 73 77 69 74 63 68 28 20 63 20 29 7b 0a 20 20   switch( c ){.  
a2230 20 20 20 20 20 20 63 61 73 65 20 27 2d 27 3a 20        case '-': 
a2240 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69    flag_leftjusti
a2250 66 79 20 3d 20 31 3b 20 20 20 20 20 62 72 65 61  fy = 1;     brea
a2260 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k;.        case 
a2270 27 2b 27 3a 20 20 20 66 6c 61 67 5f 70 6c 75 73  '+':   flag_plus
a2280 73 69 67 6e 20 3d 20 31 3b 20 20 20 20 20 20 20  sign = 1;       
a2290 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
a22a0 63 61 73 65 20 27 20 27 3a 20 20 20 66 6c 61 67  case ' ':   flag
a22b0 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 31 3b 20  _blanksign = 1; 
a22c0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
a22d0 20 20 20 20 20 63 61 73 65 20 27 23 27 3a 20 20       case '#':  
a22e0 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66   flag_alternatef
a22f0 6f 72 6d 20 3d 20 31 3b 20 20 20 62 72 65 61 6b  orm = 1;   break
a2300 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  ;.        case '
a2310 21 27 3a 20 20 20 66 6c 61 67 5f 61 6c 74 66 6f  !':   flag_altfo
a2320 72 6d 32 20 3d 20 31 3b 20 20 20 20 20 20 20 20  rm2 = 1;        
a2330 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63  break;.        c
a2340 61 73 65 20 27 30 27 3a 20 20 20 66 6c 61 67 5f  ase '0':   flag_
a2350 7a 65 72 6f 70 61 64 20 3d 20 31 3b 20 20 20 20  zeropad = 1;    
a2360 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
a2370 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20      default:    
a2380 64 6f 6e 65 20 3d 20 31 3b 20 20 20 20 20 20 20  done = 1;       
a2390 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
a23a0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 77 68  .      }.    }wh
a23b0 69 6c 65 28 20 21 64 6f 6e 65 20 26 26 20 28 63  ile( !done && (c
a23c0 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d 30 20 29 3b  =(*++fmt))!=0 );
a23d0 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20  .    /* Get the 
a23e0 66 69 65 6c 64 20 77 69 64 74 68 20 2a 2f 0a 20  field width */. 
a23f0 20 20 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20     width = 0;.  
a2400 20 20 69 66 28 20 63 3d 3d 27 2a 27 20 29 7b 0a    if( c=='*' ){.
a2410 20 20 20 20 20 20 77 69 64 74 68 20 3d 20 76 61        width = va
a2420 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20  _arg(ap,int);.  
a2430 20 20 20 20 69 66 28 20 77 69 64 74 68 3c 30 20      if( width<0 
a2440 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f  ){.        flag_
a2450 6c 65 66 74 6a 75 73 74 69 66 79 20 3d 20 31 3b  leftjustify = 1;
a2460 0a 20 20 20 20 20 20 20 20 77 69 64 74 68 20 3d  .        width =
a2470 20 2d 77 69 64 74 68 3b 0a 20 20 20 20 20 20 7d   -width;.      }
a2480 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d  .      c = *++fm
a2490 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  t;.    }else{.  
a24a0 20 20 20 20 77 68 69 6c 65 28 20 63 3e 3d 27 30      while( c>='0
a24b0 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b 0a 20  ' && c<='9' ){. 
a24c0 20 20 20 20 20 20 20 77 69 64 74 68 20 3d 20 77         width = w
a24d0 69 64 74 68 2a 31 30 20 2b 20 63 20 2d 20 27 30  idth*10 + c - '0
a24e0 27 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 2a  ';.        c = *
a24f0 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d 0a 20  ++fmt;.      }. 
a2500 20 20 20 7d 0a 20 20 20 20 69 66 28 20 77 69 64     }.    if( wid
a2510 74 68 20 3e 20 65 74 42 55 46 53 49 5a 45 2d 31  th > etBUFSIZE-1
a2520 30 20 29 7b 0a 20 20 20 20 20 20 77 69 64 74 68  0 ){.      width
a2530 20 3d 20 65 74 42 55 46 53 49 5a 45 2d 31 30 3b   = etBUFSIZE-10;
a2540 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 47 65  .    }.    /* Ge
a2550 74 20 74 68 65 20 70 72 65 63 69 73 69 6f 6e 20  t the precision 
a2560 2a 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 2e  */.    if( c=='.
a2570 27 20 29 7b 0a 20 20 20 20 20 20 70 72 65 63 69  ' ){.      preci
a2580 73 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20  sion = 0;.      
a2590 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20  c = *++fmt;.    
a25a0 20 20 69 66 28 20 63 3d 3d 27 2a 27 20 29 7b 0a    if( c=='*' ){.
a25b0 20 20 20 20 20 20 20 20 70 72 65 63 69 73 69 6f          precisio
a25c0 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e  n = va_arg(ap,in
a25d0 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  t);.        if( 
a25e0 70 72 65 63 69 73 69 6f 6e 3c 30 20 29 20 70 72  precision<0 ) pr
a25f0 65 63 69 73 69 6f 6e 20 3d 20 2d 70 72 65 63 69  ecision = -preci
a2600 73 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20 63 20  sion;.        c 
a2610 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20  = *++fmt;.      
a2620 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 77  }else{.        w
a2630 68 69 6c 65 28 20 63 3e 3d 27 30 27 20 26 26 20  hile( c>='0' && 
a2640 63 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20 20 20  c<='9' ){.      
a2650 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20      precision = 
a2660 70 72 65 63 69 73 69 6f 6e 2a 31 30 20 2b 20 63  precision*10 + c
a2670 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20   - '0';.        
a2680 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20    c = *++fmt;.  
a2690 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
a26a0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
a26b0 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 2d 31 3b   precision = -1;
a26c0 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 47 65  .    }.    /* Ge
a26d0 74 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  t the conversion
a26e0 20 74 79 70 65 20 6d 6f 64 69 66 69 65 72 20 2a   type modifier *
a26f0 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 6c 27  /.    if( c=='l'
a2700 20 29 7b 0a 20 20 20 20 20 20 66 6c 61 67 5f 6c   ){.      flag_l
a2710 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 63  ong = 1;.      c
a2720 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20   = *++fmt;.     
a2730 20 69 66 28 20 63 3d 3d 27 6c 27 20 29 7b 0a 20   if( c=='l' ){. 
a2740 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67         flag_long
a2750 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20 20  long = 1;.      
a2760 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20    c = *++fmt;.  
a2770 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
a2780 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67     flag_longlong
a2790 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
a27a0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66    }else{.      f
a27b0 6c 61 67 5f 6c 6f 6e 67 20 3d 20 66 6c 61 67 5f  lag_long = flag_
a27c0 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20  longlong = 0;.  
a27d0 20 20 7d 0a 20 20 20 20 2f 2a 20 46 65 74 63 68    }.    /* Fetch
a27e0 20 74 68 65 20 69 6e 66 6f 20 65 6e 74 72 79 20   the info entry 
a27f0 66 6f 72 20 74 68 65 20 66 69 65 6c 64 20 2a 2f  for the field */
a2800 0a 20 20 20 20 69 6e 66 6f 70 20 3d 20 30 3b 0a  .    infop = 0;.
a2810 20 20 20 20 66 6f 72 28 69 64 78 3d 30 3b 20 69      for(idx=0; i
a2820 64 78 3c 65 74 4e 49 4e 46 4f 3b 20 69 64 78 2b  dx<etNINFO; idx+
a2830 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 63 3d  +){.      if( c=
a2840 3d 66 6d 74 69 6e 66 6f 5b 69 64 78 5d 2e 66 6d  =fmtinfo[idx].fm
a2850 74 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 20  ttype ){.       
a2860 20 69 6e 66 6f 70 20 3d 20 26 66 6d 74 69 6e 66   infop = &fmtinf
a2870 6f 5b 69 64 78 5d 3b 0a 20 20 20 20 20 20 20 20  o[idx];.        
a2880 69 66 28 20 75 73 65 45 78 74 65 6e 64 65 64 20  if( useExtended 
a2890 7c 7c 20 28 69 6e 66 6f 70 2d 3e 66 6c 61 67 73  || (infop->flags
a28a0 20 26 20 46 4c 41 47 5f 49 4e 54 45 52 4e 29 3d   & FLAG_INTERN)=
a28b0 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
a28c0 78 74 79 70 65 20 3d 20 69 6e 66 6f 70 2d 3e 74  xtype = infop->t
a28d0 79 70 65 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  ype;.        }el
a28e0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65  se{.          re
a28f0 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a  turn;.        }.
a2900 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
a2910 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
a2920 20 7a 45 78 74 72 61 20 3d 20 30 3b 0a 20 20 20   zExtra = 0;.   
a2930 20 69 66 28 20 69 6e 66 6f 70 3d 3d 30 20 29 7b   if( infop==0 ){
a2940 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
a2950 20 20 20 7d 0a 0a 0a 20 20 20 20 2f 2a 20 4c 69     }...    /* Li
a2960 6d 69 74 20 74 68 65 20 70 72 65 63 69 73 69 6f  mit the precisio
a2970 6e 20 74 6f 20 70 72 65 76 65 6e 74 20 6f 76 65  n to prevent ove
a2980 72 66 6c 6f 77 69 6e 67 20 62 75 66 5b 5d 20 64  rflowing buf[] d
a2990 75 72 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e  uring conversion
a29a0 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 72 65 63   */.    if( prec
a29b0 69 73 69 6f 6e 3e 65 74 42 55 46 53 49 5a 45 2d  ision>etBUFSIZE-
a29c0 34 30 20 26 26 20 28 69 6e 66 6f 70 2d 3e 66 6c  40 && (infop->fl
a29d0 61 67 73 20 26 20 46 4c 41 47 5f 53 54 52 49 4e  ags & FLAG_STRIN
a29e0 47 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  G)==0 ){.      p
a29f0 72 65 63 69 73 69 6f 6e 20 3d 20 65 74 42 55 46  recision = etBUF
a2a00 53 49 5a 45 2d 34 30 3b 0a 20 20 20 20 7d 0a 0a  SIZE-40;.    }..
a2a10 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 41 74      /*.    ** At
a2a20 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 76 61 72   this point, var
a2a30 69 61 62 6c 65 73 20 61 72 65 20 69 6e 69 74 69  iables are initi
a2a40 61 6c 69 7a 65 64 20 61 73 20 66 6f 6c 6c 6f 77  alized as follow
a2a50 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  s:.    **.    **
a2a60 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74     flag_alternat
a2a70 65 66 6f 72 6d 20 20 20 20 20 20 20 20 20 20 54  eform          T
a2a80 52 55 45 20 69 66 20 61 20 27 23 27 20 69 73 20  RUE if a '#' is 
a2a90 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20  present..    ** 
a2aa0 20 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20    flag_altform2 
a2ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52                TR
a2ac0 55 45 20 69 66 20 61 20 27 21 27 20 69 73 20 70  UE if a '!' is p
a2ad0 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 20  resent..    **  
a2ae0 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 20   flag_plussign  
a2af0 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55               TRU
a2b00 45 20 69 66 20 61 20 27 2b 27 20 69 73 20 70 72  E if a '+' is pr
a2b10 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 20  esent..    **   
a2b20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79  flag_leftjustify
a2b30 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55 45              TRUE
a2b40 20 69 66 20 61 20 27 2d 27 20 69 73 20 70 72 65   if a '-' is pre
a2b50 73 65 6e 74 20 6f 72 20 69 66 20 74 68 65 0a 20  sent or if the. 
a2b60 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20     **           
a2b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2b80 20 20 20 20 66 69 65 6c 64 20 77 69 64 74 68 20      field width 
a2b90 77 61 73 20 6e 65 67 61 74 69 76 65 2e 0a 20 20  was negative..  
a2ba0 20 20 2a 2a 20 20 20 66 6c 61 67 5f 7a 65 72 6f    **   flag_zero
a2bb0 70 61 64 20 20 20 20 20 20 20 20 20 20 20 20 20  pad             
a2bc0 20 20 20 54 52 55 45 20 69 66 20 74 68 65 20 77     TRUE if the w
a2bd0 69 64 74 68 20 62 65 67 61 6e 20 77 69 74 68 20  idth began with 
a2be0 30 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67  0..    **   flag
a2bf0 5f 6c 6f 6e 67 20 20 20 20 20 20 20 20 20 20 20  _long           
a2c00 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20          TRUE if 
a2c10 74 68 65 20 6c 65 74 74 65 72 20 27 6c 27 20 28  the letter 'l' (
a2c20 65 6c 6c 29 20 70 72 65 66 69 78 65 64 0a 20 20  ell) prefixed.  
a2c30 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20    **            
a2c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2c50 20 20 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f     the conversio
a2c60 6e 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 20  n character..   
a2c70 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c   **   flag_longl
a2c80 6f 6e 67 20 20 20 20 20 20 20 20 20 20 20 20 20  ong             
a2c90 20 20 54 52 55 45 20 69 66 20 74 68 65 20 6c 65    TRUE if the le
a2ca0 74 74 65 72 20 27 6c 6c 27 20 28 65 6c 6c 20 65  tter 'll' (ell e
a2cb0 6c 6c 29 20 70 72 65 66 69 78 65 64 0a 20 20 20  ll) prefixed.   
a2cc0 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20   **             
a2cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2ce0 20 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e    the conversion
a2cf0 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 20 20   character..    
a2d00 2a 2a 20 20 20 66 6c 61 67 5f 62 6c 61 6e 6b 73  **   flag_blanks
a2d10 69 67 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  ign             
a2d20 20 54 52 55 45 20 69 66 20 61 20 27 20 27 20 69   TRUE if a ' ' i
a2d30 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a  s present..    *
a2d40 2a 20 20 20 77 69 64 74 68 20 20 20 20 20 20 20  *   width       
a2d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2d60 54 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69  The specified fi
a2d70 65 6c 64 20 77 69 64 74 68 2e 20 20 54 68 69 73  eld width.  This
a2d80 20 69 73 0a 20 20 20 20 2a 2a 20 20 20 20 20 20   is.    **      
a2d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2da0 20 20 20 20 20 20 20 20 20 61 6c 77 61 79 73 20           always 
a2db0 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 20 5a  non-negative.  Z
a2dc0 65 72 6f 20 69 73 20 74 68 65 20 64 65 66 61 75  ero is the defau
a2dd0 6c 74 2e 0a 20 20 20 20 2a 2a 20 20 20 70 72 65  lt..    **   pre
a2de0 63 69 73 69 6f 6e 20 20 20 20 20 20 20 20 20 20  cision          
a2df0 20 20 20 20 20 20 20 20 20 54 68 65 20 73 70 65           The spe
a2e00 63 69 66 69 65 64 20 70 72 65 63 69 73 69 6f 6e  cified precision
a2e10 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a 20  .  The default. 
a2e20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20     **           
a2e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2e40 20 20 20 20 69 73 20 2d 31 2e 0a 20 20 20 20 2a      is -1..    *
a2e50 2a 20 20 20 78 74 79 70 65 20 20 20 20 20 20 20  *   xtype       
a2e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2e70 54 68 65 20 63 6c 61 73 73 20 6f 66 20 74 68 65  The class of the
a2e80 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 20 20 20   conversion..   
a2e90 20 2a 2a 20 20 20 69 6e 66 6f 70 20 20 20 20 20   **   infop     
a2ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2eb0 20 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65    Pointer to the
a2ec0 20 61 70 70 72 6f 70 72 69 61 74 65 20 69 6e 66   appropriate inf
a2ed0 6f 20 73 74 72 75 63 74 2e 0a 20 20 20 20 2a 2f  o struct..    */
a2ee0 0a 20 20 20 20 73 77 69 74 63 68 28 20 78 74 79  .    switch( xty
a2ef0 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65  pe ){.      case
a2f00 20 65 74 50 4f 49 4e 54 45 52 3a 0a 20 20 20 20   etPOINTER:.    
a2f10 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e      flag_longlon
a2f20 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 61 72 2a  g = sizeof(char*
a2f30 29 3d 3d 73 69 7a 65 6f 66 28 69 36 34 29 3b 0a  )==sizeof(i64);.
a2f40 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e          flag_lon
a2f50 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 61 72 2a  g = sizeof(char*
a2f60 29 3d 3d 73 69 7a 65 6f 66 28 6c 6f 6e 67 20 69  )==sizeof(long i
a2f70 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20  nt);.        /* 
a2f80 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e 74  Fall through int
a2f90 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73 65 20  o the next case 
a2fa0 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 65 74  */.      case et
a2fb0 4f 52 44 49 4e 41 4c 3a 0a 20 20 20 20 20 20 63  ORDINAL:.      c
a2fc0 61 73 65 20 65 74 52 41 44 49 58 3a 0a 20 20 20  ase etRADIX:.   
a2fd0 20 20 20 20 20 69 66 28 20 69 6e 66 6f 70 2d 3e       if( infop->
a2fe0 66 6c 61 67 73 20 26 20 46 4c 41 47 5f 53 49 47  flags & FLAG_SIG
a2ff0 4e 45 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20  NED ){.         
a3000 20 69 36 34 20 76 3b 0a 20 20 20 20 20 20 20 20   i64 v;.        
a3010 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 6c    if( flag_longl
a3020 6f 6e 67 20 29 20 20 20 76 20 3d 20 76 61 5f 61  ong )   v = va_a
a3030 72 67 28 61 70 2c 69 36 34 29 3b 0a 20 20 20 20  rg(ap,i64);.    
a3040 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 66        else if( f
a3050 6c 61 67 5f 6c 6f 6e 67 20 29 20 20 76 20 3d 20  lag_long )  v = 
a3060 76 61 5f 61 72 67 28 61 70 2c 6c 6f 6e 67 20 69  va_arg(ap,long i
a3070 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 65  nt);.          e
a3080 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20  lse             
a3090 20 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67 28       v = va_arg(
a30a0 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 20  ap,int);.       
a30b0 20 20 20 69 66 28 20 76 3c 30 20 29 7b 0a 20 20     if( v<0 ){.  
a30c0 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61            longva
a30d0 6c 75 65 20 3d 20 2d 76 3b 0a 20 20 20 20 20 20  lue = -v;.      
a30e0 20 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 27        prefix = '
a30f0 2d 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65  -';.          }e
a3100 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
a3110 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 3b 0a   longvalue = v;.
a3120 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
a3130 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 29 20  flag_plussign ) 
a3140 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d 20         prefix = 
a3150 27 2b 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20  '+';.           
a3160 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 62   else if( flag_b
a3170 6c 61 6e 6b 73 69 67 6e 20 29 20 20 70 72 65 66  lanksign )  pref
a3180 69 78 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 20  ix = ' ';.      
a3190 20 20 20 20 20 20 65 6c 73 65 20 20 20 20 20 20        else      
a31a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a31b0 20 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 20   prefix = 0;.   
a31c0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a31d0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
a31e0 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 6c    if( flag_longl
a31f0 6f 6e 67 20 29 20 20 20 6c 6f 6e 67 76 61 6c 75  ong )   longvalu
a3200 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 36  e = va_arg(ap,u6
a3210 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 65 6c  4);.          el
a3220 73 65 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67  se if( flag_long
a3230 20 29 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20   )  longvalue = 
a3240 76 61 5f 61 72 67 28 61 70 2c 75 6e 73 69 67 6e  va_arg(ap,unsign
a3250 65 64 20 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 20  ed long int);.  
a3260 20 20 20 20 20 20 20 20 65 6c 73 65 20 20 20 20          else    
a3270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f                lo
a3280 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 67  ngvalue = va_arg
a3290 28 61 70 2c 75 6e 73 69 67 6e 65 64 20 69 6e 74  (ap,unsigned int
a32a0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65  );.          pre
a32b0 66 69 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  fix = 0;.       
a32c0 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6c   }.        if( l
a32d0 6f 6e 67 76 61 6c 75 65 3d 3d 30 20 29 20 66 6c  ongvalue==0 ) fl
a32e0 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d  ag_alternateform
a32f0 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66   = 0;.        if
a3300 28 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 26  ( flag_zeropad &
a3310 26 20 70 72 65 63 69 73 69 6f 6e 3c 77 69 64 74  & precision<widt
a3320 68 2d 28 70 72 65 66 69 78 21 3d 30 29 20 29 7b  h-(prefix!=0) ){
a3330 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 63 69  .          preci
a3340 73 69 6f 6e 20 3d 20 77 69 64 74 68 2d 28 70 72  sion = width-(pr
a3350 65 66 69 78 21 3d 30 29 3b 0a 20 20 20 20 20 20  efix!=0);.      
a3360 20 20 7d 0a 20 20 20 20 20 20 20 20 62 75 66 70    }.        bufp
a3370 74 20 3d 20 26 62 75 66 5b 65 74 42 55 46 53 49  t = &buf[etBUFSI
a3380 5a 45 2d 31 5d 3b 0a 20 20 20 20 20 20 20 20 69  ZE-1];.        i
a3390 66 28 20 78 74 79 70 65 3d 3d 65 74 4f 52 44 49  f( xtype==etORDI
a33a0 4e 41 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  NAL ){.         
a33b0 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68   static const ch
a33c0 61 72 20 7a 4f 72 64 5b 5d 20 3d 20 22 74 68 73  ar zOrd[] = "ths
a33d0 74 6e 64 72 64 22 3b 0a 20 20 20 20 20 20 20 20  tndrd";.        
a33e0 20 20 69 6e 74 20 78 20 3d 20 6c 6f 6e 67 76 61    int x = longva
a33f0 6c 75 65 20 25 20 31 30 3b 0a 20 20 20 20 20 20  lue % 10;.      
a3400 20 20 20 20 69 66 28 20 78 3e 3d 34 20 7c 7c 20      if( x>=4 || 
a3410 28 6c 6f 6e 67 76 61 6c 75 65 2f 31 30 29 25 31  (longvalue/10)%1
a3420 30 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  0==1 ){.        
a3430 20 20 20 20 78 20 3d 20 30 3b 0a 20 20 20 20 20      x = 0;.     
a3440 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
a3450 20 62 75 66 5b 65 74 42 55 46 53 49 5a 45 2d 33   buf[etBUFSIZE-3
a3460 5d 20 3d 20 7a 4f 72 64 5b 78 2a 32 5d 3b 0a 20  ] = zOrd[x*2];. 
a3470 20 20 20 20 20 20 20 20 20 62 75 66 5b 65 74 42           buf[etB
a3480 55 46 53 49 5a 45 2d 32 5d 20 3d 20 7a 4f 72 64  UFSIZE-2] = zOrd
a3490 5b 78 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 20 20  [x*2+1];.       
a34a0 20 20 20 62 75 66 70 74 20 2d 3d 20 32 3b 0a 20     bufpt -= 2;. 
a34b0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a34c0 20 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 67   {.          reg
a34d0 69 73 74 65 72 20 63 6f 6e 73 74 20 63 68 61 72  ister const char
a34e0 20 2a 63 73 65 74 3b 20 20 20 20 20 20 2f 2a 20   *cset;      /* 
a34f0 55 73 65 20 72 65 67 69 73 74 65 72 73 20 66 6f  Use registers fo
a3500 72 20 73 70 65 65 64 20 2a 2f 0a 20 20 20 20 20  r speed */.     
a3510 20 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e       register in
a3520 74 20 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20  t base;.        
a3530 20 20 63 73 65 74 20 3d 20 26 61 44 69 67 69 74    cset = &aDigit
a3540 73 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74  s[infop->charset
a3550 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 62 61 73  ];.          bas
a3560 65 20 3d 20 69 6e 66 6f 70 2d 3e 62 61 73 65 3b  e = infop->base;
a3570 0a 20 20 20 20 20 20 20 20 20 20 64 6f 7b 20 20  .          do{  
a3580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a3590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a35a0 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76           /* Conv
a35b0 65 72 74 20 74 6f 20 61 73 63 69 69 20 2a 2f 0a  ert to ascii */.
a35c0 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d              *(--
a35d0 62 75 66 70 74 29 20 3d 20 63 73 65 74 5b 6c 6f  bufpt) = cset[lo
a35e0 6e 67 76 61 6c 75 65 25 62 61 73 65 5d 3b 0a 20  ngvalue%base];. 
a35f0 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76             longv
a3600 61 6c 75 65 20 3d 20 6c 6f 6e 67 76 61 6c 75 65  alue = longvalue
a3610 2f 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20  /base;.         
a3620 20 7d 77 68 69 6c 65 28 20 6c 6f 6e 67 76 61 6c   }while( longval
a3630 75 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20  ue>0 );.        
a3640 7d 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68  }.        length
a3650 20 3d 20 26 62 75 66 5b 65 74 42 55 46 53 49 5a   = &buf[etBUFSIZ
a3660 45 2d 31 5d 2d 62 75 66 70 74 3b 0a 20 20 20 20  E-1]-bufpt;.    
a3670 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72 65 63      for(idx=prec
a3680 69 73 69 6f 6e 2d 6c 65 6e 67 74 68 3b 20 69 64  ision-length; id
a3690 78 3e 30 3b 20 69 64 78 2d 2d 29 7b 0a 20 20 20  x>0; idx--){.   
a36a0 20 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70 74         *(--bufpt
a36b0 29 20 3d 20 27 30 27 3b 20 20 20 20 20 20 20 20  ) = '0';        
a36c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a36d0 20 20 20 20 20 2f 2a 20 5a 65 72 6f 20 70 61 64       /* Zero pad
a36e0 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   */.        }.  
a36f0 20 20 20 20 20 20 69 66 28 20 70 72 65 66 69 78        if( prefix
a3700 20 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20   ) *(--bufpt) = 
a3710 70 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 20  prefix;         
a3720 20 20 20 20 20 20 2f 2a 20 41 64 64 20 73 69 67        /* Add sig
a3730 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28  n */.        if(
a3740 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66   flag_alternatef
a3750 6f 72 6d 20 26 26 20 69 6e 66 6f 70 2d 3e 70 72  orm && infop->pr
a3760 65 66 69 78 20 29 7b 20 20 20 20 20 20 2f 2a 20  efix ){      /* 
a3770 41 64 64 20 22 30 22 20 6f 72 20 22 30 78 22 20  Add "0" or "0x" 
a3780 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e  */.          con
a3790 73 74 20 63 68 61 72 20 2a 70 72 65 3b 0a 20 20  st char *pre;.  
a37a0 20 20 20 20 20 20 20 20 63 68 61 72 20 78 3b 0a          char x;.
a37b0 20 20 20 20 20 20 20 20 20 20 70 72 65 20 3d 20            pre = 
a37c0 26 61 50 72 65 66 69 78 5b 69 6e 66 6f 70 2d 3e  &aPrefix[infop->
a37d0 70 72 65 66 69 78 5d 3b 0a 20 20 20 20 20 20 20  prefix];.       
a37e0 20 20 20 66 6f 72 28 3b 20 28 78 3d 28 2a 70 72     for(; (x=(*pr
a37f0 65 29 29 21 3d 30 3b 20 70 72 65 2b 2b 29 20 2a  e))!=0; pre++) *
a3800 28 2d 2d 62 75 66 70 74 29 20 3d 20 78 3b 0a 20  (--bufpt) = x;. 
a3810 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a3820 20 6c 65 6e 67 74 68 20 3d 20 26 62 75 66 5b 65   length = &buf[e
a3830 74 42 55 46 53 49 5a 45 2d 31 5d 2d 62 75 66 70  tBUFSIZE-1]-bufp
a3840 74 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  t;.        break
a3850 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 46  ;.      case etF
a3860 4c 4f 41 54 3a 0a 20 20 20 20 20 20 63 61 73 65  LOAT:.      case
a3870 20 65 74 45 58 50 3a 0a 20 20 20 20 20 20 63 61   etEXP:.      ca
a3880 73 65 20 65 74 47 45 4e 45 52 49 43 3a 0a 20 20  se etGENERIC:.  
a3890 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75 65 20        realvalue 
a38a0 3d 20 76 61 5f 61 72 67 28 61 70 2c 64 6f 75 62  = va_arg(ap,doub
a38b0 6c 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  le);.#ifndef SQL
a38c0 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
a38d0 47 5f 50 4f 49 4e 54 0a 20 20 20 20 20 20 20 20  G_POINT.        
a38e0 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3c 30 20  if( precision<0 
a38f0 29 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 36 3b  ) precision = 6;
a3900 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20           /* Set 
a3910 64 65 66 61 75 6c 74 20 70 72 65 63 69 73 69 6f  default precisio
a3920 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28  n */.        if(
a3930 20 70 72 65 63 69 73 69 6f 6e 3e 65 74 42 55 46   precision>etBUF
a3940 53 49 5a 45 2f 32 2d 31 30 20 29 20 70 72 65 63  SIZE/2-10 ) prec
a3950 69 73 69 6f 6e 20 3d 20 65 74 42 55 46 53 49 5a  ision = etBUFSIZ
a3960 45 2f 32 2d 31 30 3b 0a 20 20 20 20 20 20 20 20  E/2-10;.        
a3970 69 66 28 20 72 65 61 6c 76 61 6c 75 65 3c 30 2e  if( realvalue<0.
a3980 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  0 ){.          r
a3990 65 61 6c 76 61 6c 75 65 20 3d 20 2d 72 65 61 6c  ealvalue = -real
a39a0 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 20 20 20  value;.         
a39b0 20 70 72 65 66 69 78 20 3d 20 27 2d 27 3b 0a 20   prefix = '-';. 
a39c0 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
a39d0 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67          if( flag
a39e0 5f 70 6c 75 73 73 69 67 6e 20 29 20 20 20 20 20  _plussign )     
a39f0 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 2b       prefix = '+
a3a00 27 3b 0a 20 20 20 20 20 20 20 20 20 20 65 6c 73  ';.          els
a3a10 65 20 69 66 28 20 66 6c 61 67 5f 62 6c 61 6e 6b  e if( flag_blank
a3a20 73 69 67 6e 20 29 20 20 20 20 70 72 65 66 69 78  sign )    prefix
a3a30 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 20 20   = ' ';.        
a3a40 20 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 20    else          
a3a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
a3a60 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 20 20 20  refix = 0;.     
a3a70 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
a3a80 20 78 74 79 70 65 3d 3d 65 74 47 45 4e 45 52 49   xtype==etGENERI
a3a90 43 20 26 26 20 70 72 65 63 69 73 69 6f 6e 3e 30  C && precision>0
a3aa0 20 29 20 70 72 65 63 69 73 69 6f 6e 2d 2d 3b 0a   ) precision--;.
a3ab0 23 69 66 20 30 0a 20 20 20 20 20 20 20 20 2f 2a  #if 0.        /*
a3ac0 20 52 6f 75 6e 64 69 6e 67 20 77 6f 72 6b 73 20   Rounding works 
a3ad0 6c 69 6b 65 20 42 53 44 20 77 68 65 6e 20 74 68  like BSD when th
a3ae0 65 20 63 6f 6e 73 74 61 6e 74 20 30 2e 34 39 39  e constant 0.499
a3af0 39 20 69 73 20 75 73 65 64 2e 20 20 57 69 65 72  9 is used.  Wier
a3b00 64 21 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f  d! */.        fo
a3b10 72 28 69 64 78 3d 70 72 65 63 69 73 69 6f 6e 2c  r(idx=precision,
a3b20 20 72 6f 75 6e 64 65 72 3d 30 2e 34 39 39 39 3b   rounder=0.4999;
a3b30 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 2c 20 72   idx>0; idx--, r
a3b40 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 3b 0a 23 65  ounder*=0.1);.#e
a3b50 6c 73 65 0a 20 20 20 20 20 20 20 20 2f 2a 20 49  lse.        /* I
a3b60 74 20 6d 61 6b 65 73 20 6d 6f 72 65 20 73 65 6e  t makes more sen
a3b70 73 65 20 74 6f 20 75 73 65 20 30 2e 35 20 2a 2f  se to use 0.5 */
a3b80 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 64 78  .        for(idx
a3b90 3d 70 72 65 63 69 73 69 6f 6e 2c 20 72 6f 75 6e  =precision, roun
a3ba0 64 65 72 3d 30 2e 35 3b 20 69 64 78 3e 30 3b 20  der=0.5; idx>0; 
a3bb0 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65 72 2a 3d  idx--, rounder*=
a3bc0 30 2e 31 29 7b 7d 0a 23 65 6e 64 69 66 0a 20 20  0.1){}.#endif.  
a3bd0 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d        if( xtype=
a3be0 3d 65 74 46 4c 4f 41 54 20 29 20 72 65 61 6c 76  =etFLOAT ) realv
a3bf0 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 65 72 3b  alue += rounder;
a3c00 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 72 6d  .        /* Norm
a3c10 61 6c 69 7a 65 20 72 65 61 6c 76 61 6c 75 65 20  alize realvalue 
a3c20 74 6f 20 77 69 74 68 69 6e 20 31 30 2e 30 20 3e  to within 10.0 >
a3c30 20 72 65 61 6c 76 61 6c 75 65 20 3e 3d 20 31 2e   realvalue >= 1.
a3c40 30 20 2a 2f 0a 20 20 20 20 20 20 20 20 65 78 70  0 */.        exp
a3c50 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66   = 0;.        if
a3c60 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 72  ( sqlite3IsNaN(r
a3c70 65 61 6c 76 61 6c 75 65 29 20 29 7b 0a 20 20 20  ealvalue) ){.   
a3c80 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22         bufpt = "
a3c90 4e 61 4e 22 3b 0a 20 20 20 20 20 20 20 20 20 20  NaN";.          
a3ca0 6c 65 6e 67 74 68 20 3d 20 33 3b 0a 20 20 20 20  length = 3;.    
a3cb0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
a3cc0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
a3cd0 66 28 20 72 65 61 6c 76 61 6c 75 65 3e 30 2e 30  f( realvalue>0.0
a3ce0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68   ){.          wh
a3cf0 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d  ile( realvalue>=
a3d00 31 65 33 32 20 26 26 20 65 78 70 3c 3d 33 35 30  1e32 && exp<=350
a3d10 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d   ){ realvalue *=
a3d20 20 31 65 2d 33 32 3b 20 65 78 70 2b 3d 33 32 3b   1e-32; exp+=32;
a3d30 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69   }.          whi
a3d40 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31  le( realvalue>=1
a3d50 65 38 20 26 26 20 65 78 70 3c 3d 33 35 30 20 29  e8 && exp<=350 )
a3d60 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31  { realvalue *= 1
a3d70 65 2d 38 3b 20 65 78 70 2b 3d 38 3b 20 7d 0a 20  e-8; exp+=8; }. 
a3d80 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20           while( 
a3d90 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 20  realvalue>=10.0 
a3da0 26 26 20 65 78 70 3c 3d 33 35 30 20 29 7b 20 72  && exp<=350 ){ r
a3db0 65 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 3b  ealvalue *= 0.1;
a3dc0 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20 20   exp++; }.      
a3dd0 20 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76      while( realv
a3de0 61 6c 75 65 3c 31 65 2d 38 20 29 7b 20 72 65 61  alue<1e-8 ){ rea
a3df0 6c 76 61 6c 75 65 20 2a 3d 20 31 65 38 3b 20 65  lvalue *= 1e8; e
a3e00 78 70 2d 3d 38 3b 20 7d 0a 20 20 20 20 20 20 20  xp-=8; }.       
a3e10 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61     while( realva
a3e20 6c 75 65 3c 31 2e 30 20 29 7b 20 72 65 61 6c 76  lue<1.0 ){ realv
a3e30 61 6c 75 65 20 2a 3d 20 31 30 2e 30 3b 20 65 78  alue *= 10.0; ex
a3e40 70 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20 20  p--; }.         
a3e50 20 69 66 28 20 65 78 70 3e 33 35 30 20 29 7b 0a   if( exp>350 ){.
a3e60 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
a3e70 70 72 65 66 69 78 3d 3d 27 2d 27 20 29 7b 0a 20  prefix=='-' ){. 
a3e80 20 20 20 20 20 20 20 20 20 20 20 20 20 62 75 66               buf
a3e90 70 74 20 3d 20 22 2d 49 6e 66 22 3b 0a 20 20 20  pt = "-Inf";.   
a3ea0 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69           }else i
a3eb0 66 28 20 70 72 65 66 69 78 3d 3d 27 2b 27 20 29  f( prefix=='+' )
a3ec0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
a3ed0 62 75 66 70 74 20 3d 20 22 2b 49 6e 66 22 3b 0a  bufpt = "+Inf";.
a3ee0 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73              }els
a3ef0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e{.             
a3f00 20 62 75 66 70 74 20 3d 20 22 49 6e 66 22 3b 0a   bufpt = "Inf";.
a3f10 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
a3f20 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68            length
a3f30 20 3d 20 73 74 72 6c 65 6e 28 62 75 66 70 74 29   = strlen(bufpt)
a3f40 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72  ;.            br
a3f50 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  eak;.          }
a3f60 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a3f70 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a     bufpt = buf;.
a3f80 20 20 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20          /*.     
a3f90 20 20 20 2a 2a 20 49 66 20 74 68 65 20 66 69 65     ** If the fie
a3fa0 6c 64 20 74 79 70 65 20 69 73 20 65 74 47 45 4e  ld type is etGEN
a3fb0 45 52 49 43 2c 20 74 68 65 6e 20 63 6f 6e 76 65  ERIC, then conve
a3fc0 72 74 20 74 6f 20 65 69 74 68 65 72 20 65 74 45  rt to either etE
a3fd0 58 50 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 72  XP.        ** or
a3fe0 20 65 74 46 4c 4f 41 54 2c 20 61 73 20 61 70 70   etFLOAT, as app
a3ff0 72 6f 70 72 69 61 74 65 2e 0a 20 20 20 20 20 20  ropriate..      
a4000 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6c 61    */.        fla
a4010 67 5f 65 78 70 20 3d 20 78 74 79 70 65 3d 3d 65  g_exp = xtype==e
a4020 74 45 58 50 3b 0a 20 20 20 20 20 20 20 20 69 66  tEXP;.        if
a4030 28 20 78 74 79 70 65 21 3d 65 74 46 4c 4f 41 54  ( xtype!=etFLOAT
a4040 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65   ){.          re
a4050 61 6c 76 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64  alvalue += round
a4060 65 72 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  er;.          if
a4070 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e  ( realvalue>=10.
a4080 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a  0 ){ realvalue *
a4090 3d 20 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a  = 0.1; exp++; }.
a40a0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a40b0 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 47    if( xtype==etG
a40c0 45 4e 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20  ENERIC ){.      
a40d0 20 20 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 21      flag_rtz = !
a40e0 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f  flag_alternatefo
a40f0 72 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  rm;.          if
a4100 28 20 65 78 70 3c 2d 34 20 7c 7c 20 65 78 70 3e  ( exp<-4 || exp>
a4110 70 72 65 63 69 73 69 6f 6e 20 29 7b 0a 20 20 20  precision ){.   
a4120 20 20 20 20 20 20 20 20 20 78 74 79 70 65 20 3d           xtype =
a4130 20 65 74 45 58 50 3b 0a 20 20 20 20 20 20 20 20   etEXP;.        
a4140 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
a4150 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d       precision =
a4160 20 70 72 65 63 69 73 69 6f 6e 20 2d 20 65 78 70   precision - exp
a4170 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 78 74  ;.            xt
a4180 79 70 65 20 3d 20 65 74 46 4c 4f 41 54 3b 0a 20  ype = etFLOAT;. 
a4190 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
a41a0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
a41b0 20 20 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 30      flag_rtz = 0
a41c0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
a41d0 20 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65      if( xtype==e
a41e0 74 45 58 50 20 29 7b 0a 20 20 20 20 20 20 20 20  tEXP ){.        
a41f0 20 20 65 32 20 3d 20 30 3b 0a 20 20 20 20 20 20    e2 = 0;.      
a4200 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
a4210 20 20 20 65 32 20 3d 20 65 78 70 3b 0a 20 20 20     e2 = exp;.   
a4220 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e       }.        n
a4230 73 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  sd = 0;.        
a4240 66 6c 61 67 5f 64 70 20 3d 20 28 70 72 65 63 69  flag_dp = (preci
a4250 73 69 6f 6e 3e 30 29 20 7c 20 66 6c 61 67 5f 61  sion>0) | flag_a
a4260 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 7c 20 66  lternateform | f
a4270 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 0a 20 20  lag_altform2;.  
a4280 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 69 67        /* The sig
a4290 6e 20 69 6e 20 66 72 6f 6e 74 20 6f 66 20 74 68  n in front of th
a42a0 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20  e number */.    
a42b0 20 20 20 20 69 66 28 20 70 72 65 66 69 78 20 29      if( prefix )
a42c0 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75  {.          *(bu
a42d0 66 70 74 2b 2b 29 20 3d 20 70 72 65 66 69 78 3b  fpt++) = prefix;
a42e0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a42f0 20 20 20 2f 2a 20 44 69 67 69 74 73 20 70 72 69     /* Digits pri
a4300 6f 72 20 74 6f 20 74 68 65 20 64 65 63 69 6d 61  or to the decima
a4310 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20  l point */.     
a4320 20 20 20 69 66 28 20 65 32 3c 30 20 29 7b 0a 20     if( e2<0 ){. 
a4330 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74           *(bufpt
a4340 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20  ++) = '0';.     
a4350 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
a4360 20 20 20 20 66 6f 72 28 3b 20 65 32 3e 3d 30 3b      for(; e2>=0;
a4370 20 65 32 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20   e2--){.        
a4380 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d      *(bufpt++) =
a4390 20 65 74 5f 67 65 74 64 69 67 69 74 28 26 72 65   et_getdigit(&re
a43a0 61 6c 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20  alvalue,&nsd);. 
a43b0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
a43c0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20     }.        /* 
a43d0 54 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e  The decimal poin
a43e0 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28  t */.        if(
a43f0 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20 20 20   flag_dp ){.    
a4400 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29        *(bufpt++)
a4410 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 20 20   = '.';.        
a4420 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 22 30 22  }.        /* "0"
a4430 20 64 69 67 69 74 73 20 61 66 74 65 72 20 74 68   digits after th
a4440 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20  e decimal point 
a4450 62 75 74 20 62 65 66 6f 72 65 20 74 68 65 20 66  but before the f
a4460 69 72 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20  irst.        ** 
a4470 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69  significant digi
a4480 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20  t of the number 
a4490 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 65  */.        for(e
a44a0 32 2b 2b 3b 20 65 32 3c 30 3b 20 70 72 65 63 69  2++; e2<0; preci
a44b0 73 69 6f 6e 2d 2d 2c 20 65 32 2b 2b 29 7b 0a 20  sion--, e2++){. 
a44c0 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
a44d0 20 70 72 65 63 69 73 69 6f 6e 3e 30 20 29 3b 0a   precision>0 );.
a44e0 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70            *(bufp
a44f0 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20  t++) = '0';.    
a4500 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a      }.        /*
a4510 20 53 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67   Significant dig
a4520 69 74 73 20 61 66 74 65 72 20 74 68 65 20 64 65  its after the de
a4530 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20  cimal point */. 
a4540 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 70         while( (p
a4550 72 65 63 69 73 69 6f 6e 2d 2d 29 3e 30 20 29 7b  recision--)>0 ){
a4560 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66  .          *(buf
a4570 70 74 2b 2b 29 20 3d 20 65 74 5f 67 65 74 64 69  pt++) = et_getdi
a4580 67 69 74 28 26 72 65 61 6c 76 61 6c 75 65 2c 26  git(&realvalue,&
a4590 6e 73 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  nsd);.        }.
a45a0 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76          /* Remov
a45b0 65 20 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73  e trailing zeros
a45c0 20 61 6e 64 20 74 68 65 20 22 2e 22 20 69 66 20   and the "." if 
a45d0 6e 6f 20 64 69 67 69 74 73 20 66 6f 6c 6c 6f 77  no digits follow
a45e0 20 74 68 65 20 22 2e 22 20 2a 2f 0a 20 20 20 20   the "." */.    
a45f0 20 20 20 20 69 66 28 20 66 6c 61 67 5f 72 74 7a      if( flag_rtz
a4600 20 26 26 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20   && flag_dp ){. 
a4610 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20           while( 
a4620 62 75 66 70 74 5b 2d 31 5d 3d 3d 27 30 27 20 29  bufpt[-1]=='0' )
a4630 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b   *(--bufpt) = 0;
a4640 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
a4650 74 28 20 62 75 66 70 74 3e 62 75 66 20 29 3b 0a  t( bufpt>buf );.
a4660 20 20 20 20 20 20 20 20 20 20 69 66 28 20 62 75            if( bu
a4670 66 70 74 5b 2d 31 5d 3d 3d 27 2e 27 20 29 7b 0a  fpt[-1]=='.' ){.
a4680 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
a4690 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 29 7b  flag_altform2 ){
a46a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a  .              *
a46b0 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b  (bufpt++) = '0';
a46c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c  .            }el
a46d0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
a46e0 20 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 30    *(--bufpt) = 0
a46f0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
a4700 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
a4710 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a      }.        /*
a4720 20 41 64 64 20 74 68 65 20 22 65 4e 4e 4e 22 20   Add the "eNNN" 
a4730 73 75 66 66 69 78 20 2a 2f 0a 20 20 20 20 20 20  suffix */.      
a4740 20 20 69 66 28 20 66 6c 61 67 5f 65 78 70 20 7c    if( flag_exp |
a4750 7c 20 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29  | xtype==etEXP )
a4760 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75  {.          *(bu
a4770 66 70 74 2b 2b 29 20 3d 20 61 44 69 67 69 74 73  fpt++) = aDigits
a4780 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d  [infop->charset]
a4790 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
a47a0 65 78 70 3c 30 20 29 7b 0a 20 20 20 20 20 20 20  exp<0 ){.       
a47b0 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20       *(bufpt++) 
a47c0 3d 20 27 2d 27 3b 20 65 78 70 20 3d 20 2d 65 78  = '-'; exp = -ex
a47d0 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  p;.          }el
a47e0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
a47f0 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 2b 27  *(bufpt++) = '+'
a4800 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
a4810 20 20 20 20 20 20 20 20 69 66 28 20 65 78 70 3e          if( exp>
a4820 3d 31 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20  =100 ){.        
a4830 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d      *(bufpt++) =
a4840 20 28 65 78 70 2f 31 30 30 29 2b 27 30 27 3b 20   (exp/100)+'0'; 
a4850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
a4860 2a 20 31 30 30 27 73 20 64 69 67 69 74 20 2a 2f  * 100's digit */
a4870 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 78 70  .            exp
a4880 20 25 3d 20 31 30 30 3b 0a 20 20 20 20 20 20 20   %= 100;.       
a4890 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 2a     }.          *
a48a0 28 62 75 66 70 74 2b 2b 29 20 3d 20 65 78 70 2f  (bufpt++) = exp/
a48b0 31 30 2b 27 30 27 3b 20 20 20 20 20 20 20 20 20  10+'0';         
a48c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31              /* 1
a48d0 30 27 73 20 64 69 67 69 74 20 2a 2f 0a 20 20 20  0's digit */.   
a48e0 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b         *(bufpt++
a48f0 29 20 3d 20 65 78 70 25 31 30 2b 27 30 27 3b 20  ) = exp%10+'0'; 
a4900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a4910 20 20 20 20 2f 2a 20 31 27 73 20 64 69 67 69 74      /* 1's digit
a4920 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   */.        }.  
a4930 20 20 20 20 20 20 2a 62 75 66 70 74 20 3d 20 30        *bufpt = 0
a4940 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ;..        /* Th
a4950 65 20 63 6f 6e 76 65 72 74 65 64 20 6e 75 6d 62  e converted numb
a4960 65 72 20 69 73 20 69 6e 20 62 75 66 5b 5d 20 61  er is in buf[] a
a4970 6e 64 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74  nd zero terminat
a4980 65 64 2e 20 4f 75 74 70 75 74 20 69 74 2e 0a 20  ed. Output it.. 
a4990 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74         ** Note t
a49a0 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 69  hat the number i
a49b0 73 20 69 6e 20 74 68 65 20 75 73 75 61 6c 20 6f  s in the usual o
a49c0 72 64 65 72 2c 20 6e 6f 74 20 72 65 76 65 72 73  rder, not revers
a49d0 65 64 20 61 73 20 77 69 74 68 0a 20 20 20 20 20  ed as with.     
a49e0 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 63 6f     ** integer co
a49f0 6e 76 65 72 73 69 6f 6e 73 2e 20 2a 2f 0a 20 20  nversions. */.  
a4a00 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 62        length = b
a4a10 75 66 70 74 2d 62 75 66 3b 0a 20 20 20 20 20 20  ufpt-buf;.      
a4a20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 0a    bufpt = buf;..
a4a30 20 20 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69          /* Speci
a4a40 61 6c 20 63 61 73 65 3a 20 20 41 64 64 20 6c 65  al case:  Add le
a4a50 61 64 69 6e 67 20 7a 65 72 6f 73 20 69 66 20 74  ading zeros if t
a4a60 68 65 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20  he flag_zeropad 
a4a70 66 6c 61 67 20 69 73 0a 20 20 20 20 20 20 20 20  flag is.        
a4a80 2a 2a 20 73 65 74 20 61 6e 64 20 77 65 20 61 72  ** set and we ar
a4a90 65 20 6e 6f 74 20 6c 65 66 74 20 6a 75 73 74 69  e not left justi
a4aa0 66 69 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20  fied */.        
a4ab0 69 66 28 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64  if( flag_zeropad
a4ac0 20 26 26 20 21 66 6c 61 67 5f 6c 65 66 74 6a 75   && !flag_leftju
a4ad0 73 74 69 66 79 20 26 26 20 6c 65 6e 67 74 68 20  stify && length 
a4ae0 3c 20 77 69 64 74 68 29 7b 0a 20 20 20 20 20 20  < width){.      
a4af0 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20      int i;.     
a4b00 20 20 20 20 20 69 6e 74 20 6e 50 61 64 20 3d 20       int nPad = 
a4b10 77 69 64 74 68 20 2d 20 6c 65 6e 67 74 68 3b 0a  width - length;.
a4b20 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d            for(i=
a4b30 77 69 64 74 68 3b 20 69 3e 3d 6e 50 61 64 3b 20  width; i>=nPad; 
a4b40 69 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i--){.          
a4b50 20 20 62 75 66 70 74 5b 69 5d 20 3d 20 62 75 66    bufpt[i] = buf
a4b60 70 74 5b 69 2d 6e 50 61 64 5d 3b 0a 20 20 20 20  pt[i-nPad];.    
a4b70 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a4b80 20 20 69 20 3d 20 70 72 65 66 69 78 21 3d 30 3b    i = prefix!=0;
a4b90 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65  .          while
a4ba0 28 20 6e 50 61 64 2d 2d 20 29 20 62 75 66 70 74  ( nPad-- ) bufpt
a4bb0 5b 69 2b 2b 5d 20 3d 20 27 30 27 3b 0a 20 20 20  [i++] = '0';.   
a4bc0 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20         length = 
a4bd0 77 69 64 74 68 3b 0a 20 20 20 20 20 20 20 20 7d  width;.        }
a4be0 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20  .#endif.        
a4bf0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
a4c00 65 20 65 74 53 49 5a 45 3a 0a 20 20 20 20 20 20  e etSIZE:.      
a4c10 20 20 2a 28 76 61 5f 61 72 67 28 61 70 2c 69 6e    *(va_arg(ap,in
a4c20 74 2a 29 29 20 3d 20 70 41 63 63 75 6d 2d 3e 6e  t*)) = pAccum->n
a4c30 43 68 61 72 3b 0a 20 20 20 20 20 20 20 20 6c 65  Char;.        le
a4c40 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 30  ngth = width = 0
a4c50 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
a4c60 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 50 45  .      case etPE
a4c70 52 43 45 4e 54 3a 0a 20 20 20 20 20 20 20 20 62  RCENT:.        b
a4c80 75 66 5b 30 5d 20 3d 20 27 25 27 3b 0a 20 20 20  uf[0] = '%';.   
a4c90 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 75 66       bufpt = buf
a4ca0 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68  ;.        length
a4cb0 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 62 72   = 1;.        br
a4cc0 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
a4cd0 65 74 43 48 41 52 58 3a 0a 20 20 20 20 20 20 20  etCHARX:.       
a4ce0 20 63 20 3d 20 62 75 66 5b 30 5d 20 3d 20 76 61   c = buf[0] = va
a4cf0 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20  _arg(ap,int);.  
a4d00 20 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73        if( precis
a4d10 69 6f 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  ion>=0 ){.      
a4d20 20 20 20 20 66 6f 72 28 69 64 78 3d 31 3b 20 69      for(idx=1; i
a4d30 64 78 3c 70 72 65 63 69 73 69 6f 6e 3b 20 69 64  dx<precision; id
a4d40 78 2b 2b 29 20 62 75 66 5b 69 64 78 5d 20 3d 20  x++) buf[idx] = 
a4d50 63 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e  c;.          len
a4d60 67 74 68 20 3d 20 70 72 65 63 69 73 69 6f 6e 3b  gth = precision;
a4d70 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
a4d80 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68            length
a4d90 20 3d 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   =1;.        }. 
a4da0 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62         bufpt = b
a4db0 75 66 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  uf;.        brea
a4dc0 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74  k;.      case et
a4dd0 53 54 52 49 4e 47 3a 0a 20 20 20 20 20 20 63 61  STRING:.      ca
a4de0 73 65 20 65 74 44 59 4e 53 54 52 49 4e 47 3a 0a  se etDYNSTRING:.
a4df0 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20          bufpt = 
a4e00 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a 29  va_arg(ap,char*)
a4e10 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 62 75  ;.        if( bu
a4e20 66 70 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  fpt==0 ){.      
a4e30 20 20 20 20 62 75 66 70 74 20 3d 20 22 22 3b 0a      bufpt = "";.
a4e40 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
a4e50 28 20 78 74 79 70 65 3d 3d 65 74 44 59 4e 53 54  ( xtype==etDYNST
a4e60 52 49 4e 47 20 29 7b 0a 20 20 20 20 20 20 20 20  RING ){.        
a4e70 20 20 7a 45 78 74 72 61 20 3d 20 62 75 66 70 74    zExtra = bufpt
a4e80 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
a4e90 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f      if( precisio
a4ea0 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  n>=0 ){.        
a4eb0 20 20 66 6f 72 28 6c 65 6e 67 74 68 3d 30 3b 20    for(length=0; 
a4ec0 6c 65 6e 67 74 68 3c 70 72 65 63 69 73 69 6f 6e  length<precision
a4ed0 20 26 26 20 62 75 66 70 74 5b 6c 65 6e 67 74 68   && bufpt[length
a4ee0 5d 3b 20 6c 65 6e 67 74 68 2b 2b 29 7b 7d 0a 20  ]; length++){}. 
a4ef0 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
a4f00 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d          length =
a4f10 20 73 74 72 6c 65 6e 28 62 75 66 70 74 29 3b 0a   strlen(bufpt);.
a4f20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a4f30 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
a4f40 61 73 65 20 65 74 53 51 4c 45 53 43 41 50 45 3a  ase etSQLESCAPE:
a4f50 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53 51  .      case etSQ
a4f60 4c 45 53 43 41 50 45 32 3a 0a 20 20 20 20 20 20  LESCAPE2:.      
a4f70 63 61 73 65 20 65 74 53 51 4c 45 53 43 41 50 45  case etSQLESCAPE
a4f80 33 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  3: {.        int
a4f90 20 69 2c 20 6a 2c 20 6e 2c 20 63 68 2c 20 69 73   i, j, n, ch, is
a4fa0 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e  null;.        in
a4fb0 74 20 6e 65 65 64 51 75 6f 74 65 3b 0a 20 20 20  t needQuote;.   
a4fc0 20 20 20 20 20 63 68 61 72 20 71 20 3d 20 28 28       char q = ((
a4fd0 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 53 43 41  xtype==etSQLESCA
a4fe0 50 45 33 29 3f 27 22 27 3a 27 5c 27 27 29 3b 20  PE3)?'"':'\''); 
a4ff0 20 20 2f 2a 20 51 75 6f 74 65 20 63 68 61 72 61    /* Quote chara
a5000 63 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 20  cter */.        
a5010 63 68 61 72 20 2a 65 73 63 61 72 67 20 3d 20 76  char *escarg = v
a5020 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a 29 3b  a_arg(ap,char*);
a5030 0a 20 20 20 20 20 20 20 20 69 73 6e 75 6c 6c 20  .        isnull 
a5040 3d 20 65 73 63 61 72 67 3d 3d 30 3b 0a 20 20 20  = escarg==0;.   
a5050 20 20 20 20 20 69 66 28 20 69 73 6e 75 6c 6c 20       if( isnull 
a5060 29 20 65 73 63 61 72 67 20 3d 20 28 78 74 79 70  ) escarg = (xtyp
a5070 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 32 20  e==etSQLESCAPE2 
a5080 3f 20 22 4e 55 4c 4c 22 20 3a 20 22 28 4e 55 4c  ? "NULL" : "(NUL
a5090 4c 29 22 29 3b 0a 20 20 20 20 20 20 20 20 66 6f  L)");.        fo
a50a0 72 28 69 3d 6e 3d 30 3b 20 28 63 68 3d 65 73 63  r(i=n=0; (ch=esc
a50b0 61 72 67 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29  arg[i])!=0; i++)
a50c0 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
a50d0 63 68 3d 3d 71 20 29 20 20 6e 2b 2b 3b 0a 20 20  ch==q )  n++;.  
a50e0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a50f0 6e 65 65 64 51 75 6f 74 65 20 3d 20 21 69 73 6e  needQuote = !isn
a5100 75 6c 6c 20 26 26 20 78 74 79 70 65 3d 3d 65 74  ull && xtype==et
a5110 53 51 4c 45 53 43 41 50 45 32 3b 0a 20 20 20 20  SQLESCAPE2;.    
a5120 20 20 20 20 6e 20 2b 3d 20 69 20 2b 20 31 20 2b      n += i + 1 +
a5130 20 6e 65 65 64 51 75 6f 74 65 2a 32 3b 0a 20 20   needQuote*2;.  
a5140 20 20 20 20 20 20 69 66 28 20 6e 3e 65 74 42 55        if( n>etBU
a5150 46 53 49 5a 45 20 29 7b 0a 20 20 20 20 20 20 20  FSIZE ){.       
a5160 20 20 20 62 75 66 70 74 20 3d 20 7a 45 78 74 72     bufpt = zExtr
a5170 61 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  a = sqlite3Mallo
a5180 63 28 20 6e 20 29 3b 0a 20 20 20 20 20 20 20 20  c( n );.        
a5190 20 20 69 66 28 20 62 75 66 70 74 3d 3d 30 20 29    if( bufpt==0 )
a51a0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20   return;.       
a51b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
a51c0 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20    bufpt = buf;. 
a51d0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a51e0 20 6a 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20   j = 0;.        
a51f0 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20  if( needQuote ) 
a5200 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a  bufpt[j++] = q;.
a5210 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
a5220 20 28 63 68 3d 65 73 63 61 72 67 5b 69 5d 29 21   (ch=escarg[i])!
a5230 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  =0; i++){.      
a5240 20 20 20 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d      bufpt[j++] =
a5250 20 63 68 3b 0a 20 20 20 20 20 20 20 20 20 20 69   ch;.          i
a5260 66 28 20 63 68 3d 3d 71 20 29 20 62 75 66 70 74  f( ch==q ) bufpt
a5270 5b 6a 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20 20 20  [j++] = ch;.    
a5280 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
a5290 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20 62 75  ( needQuote ) bu
a52a0 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a 20 20  fpt[j++] = q;.  
a52b0 20 20 20 20 20 20 62 75 66 70 74 5b 6a 5d 20 3d        bufpt[j] =
a52c0 20 30 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 67   0;.        leng
a52d0 74 68 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20  th = j;.        
a52e0 2f 2a 20 54 68 65 20 70 72 65 63 69 73 69 6f 6e  /* The precision
a52f0 20 69 73 20 69 67 6e 6f 72 65 64 20 6f 6e 20 25   is ignored on %
a5300 71 20 61 6e 64 20 25 51 20 2a 2f 0a 20 20 20 20  q and %Q */.    
a5310 20 20 20 20 2f 2a 20 69 66 28 20 70 72 65 63 69      /* if( preci
a5320 73 69 6f 6e 3e 3d 30 20 26 26 20 70 72 65 63 69  sion>=0 && preci
a5330 73 69 6f 6e 3c 6c 65 6e 67 74 68 20 29 20 6c 65  sion<length ) le
a5340 6e 67 74 68 20 3d 20 70 72 65 63 69 73 69 6f 6e  ngth = precision
a5350 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 62 72 65  ; */.        bre
a5360 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
a5370 20 20 63 61 73 65 20 65 74 54 4f 4b 45 4e 3a 20    case etTOKEN: 
a5380 7b 0a 20 20 20 20 20 20 20 20 54 6f 6b 65 6e 20  {.        Token 
a5390 2a 70 54 6f 6b 65 6e 20 3d 20 76 61 5f 61 72 67  *pToken = va_arg
a53a0 28 61 70 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 20 20  (ap, Token*);.  
a53b0 20 20 20 20 20 20 69 66 28 20 70 54 6f 6b 65 6e        if( pToken
a53c0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
a53d0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70  lite3StrAccumApp
a53e0 65 6e 64 28 70 41 63 63 75 6d 2c 20 28 63 6f 6e  end(pAccum, (con
a53f0 73 74 20 63 68 61 72 2a 29 70 54 6f 6b 65 6e 2d  st char*)pToken-
a5400 3e 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 3b 0a  >z, pToken->n);.
a5410 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a5420 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 68    length = width
a5430 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72   = 0;.        br
a5440 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
a5450 20 20 20 63 61 73 65 20 65 74 53 52 43 4c 49 53     case etSRCLIS
a5460 54 3a 20 7b 0a 20 20 20 20 20 20 20 20 53 72 63  T: {.        Src
a5470 4c 69 73 74 20 2a 70 53 72 63 20 3d 20 76 61 5f  List *pSrc = va_
a5480 61 72 67 28 61 70 2c 20 53 72 63 4c 69 73 74 2a  arg(ap, SrcList*
a5490 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6b  );.        int k
a54a0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
a54b0 74 29 3b 0a 20 20 20 20 20 20 20 20 73 74 72 75  t);.        stru
a54c0 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
a54d0 2a 70 49 74 65 6d 20 3d 20 26 70 53 72 63 2d 3e  *pItem = &pSrc->
a54e0 61 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 61 73  a[k];.        as
a54f0 73 65 72 74 28 20 6b 3e 3d 30 20 26 26 20 6b 3c  sert( k>=0 && k<
a5500 70 53 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 20 20  pSrc->nSrc );.  
a5510 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d        if( pItem-
a5520 3e 7a 44 61 74 61 62 61 73 65 20 29 7b 0a 20 20  >zDatabase ){.  
a5530 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
a5540 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41  trAccumAppend(pA
a5550 63 63 75 6d 2c 20 70 49 74 65 6d 2d 3e 7a 44 61  ccum, pItem->zDa
a5560 74 61 62 61 73 65 2c 20 2d 31 29 3b 0a 20 20 20  tabase, -1);.   
a5570 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74         sqlite3St
a5580 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63  rAccumAppend(pAc
a5590 63 75 6d 2c 20 22 2e 22 2c 20 31 29 3b 0a 20 20  cum, ".", 1);.  
a55a0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a55b0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41  sqlite3StrAccumA
a55c0 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 70 49  ppend(pAccum, pI
a55d0 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 2d 31 29 3b  tem->zName, -1);
a55e0 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20  .        length 
a55f0 3d 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20  = width = 0;.   
a5600 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
a5610 20 20 7d 0a 20 20 20 20 7d 2f 2a 20 45 6e 64 20    }.    }/* End 
a5620 73 77 69 74 63 68 20 6f 76 65 72 20 74 68 65 20  switch over the 
a5630 66 6f 72 6d 61 74 20 74 79 70 65 20 2a 2f 0a 20  format type */. 
a5640 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65     /*.    ** The
a5650 20 74 65 78 74 20 6f 66 20 74 68 65 20 63 6f 6e   text of the con
a5660 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 69 6e 74  version is point
a5670 65 64 20 74 6f 20 62 79 20 22 62 75 66 70 74 22  ed to by "bufpt"
a5680 20 61 6e 64 20 69 73 0a 20 20 20 20 2a 2a 20 22   and is.    ** "
a5690 6c 65 6e 67 74 68 22 20 63 68 61 72 61 63 74 65  length" characte
a56a0 72 73 20 6c 6f 6e 67 2e 20 20 54 68 65 20 66 69  rs long.  The fi
a56b0 65 6c 64 20 77 69 64 74 68 20 69 73 20 22 77 69  eld width is "wi
a56c0 64 74 68 22 2e 20 20 44 6f 0a 20 20 20 20 2a 2a  dth".  Do.    **
a56d0 20 74 68 65 20 6f 75 74 70 75 74 2e 0a 20 20 20   the output..   
a56e0 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 66 6c 61   */.    if( !fla
a56f0 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 29 7b  g_leftjustify ){
a5700 0a 20 20 20 20 20 20 72 65 67 69 73 74 65 72 20  .      register 
a5710 69 6e 74 20 6e 73 70 61 63 65 3b 0a 20 20 20 20  int nspace;.    
a5720 20 20 6e 73 70 61 63 65 20 3d 20 77 69 64 74 68    nspace = width
a5730 2d 6c 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 69  -length;.      i
a5740 66 28 20 6e 73 70 61 63 65 3e 30 20 29 7b 0a 20  f( nspace>0 ){. 
a5750 20 20 20 20 20 20 20 61 70 70 65 6e 64 53 70 61         appendSpa
a5760 63 65 28 70 41 63 63 75 6d 2c 20 6e 73 70 61 63  ce(pAccum, nspac
a5770 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
a5780 7d 0a 20 20 20 20 69 66 28 20 6c 65 6e 67 74 68  }.    if( length
a5790 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  >0 ){.      sqli
a57a0 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
a57b0 64 28 70 41 63 63 75 6d 2c 20 62 75 66 70 74 2c  d(pAccum, bufpt,
a57c0 20 6c 65 6e 67 74 68 29 3b 0a 20 20 20 20 7d 0a   length);.    }.
a57d0 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 65 66      if( flag_lef
a57e0 74 6a 75 73 74 69 66 79 20 29 7b 0a 20 20 20 20  tjustify ){.    
a57f0 20 20 72 65 67 69 73 74 65 72 20 69 6e 74 20 6e    register int n
a5800 73 70 61 63 65 3b 0a 20 20 20 20 20 20 6e 73 70  space;.      nsp
a5810 61 63 65 20 3d 20 77 69 64 74 68 2d 6c 65 6e 67  ace = width-leng
a5820 74 68 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 73  th;.      if( ns
a5830 70 61 63 65 3e 30 20 29 7b 0a 20 20 20 20 20 20  pace>0 ){.      
a5840 20 20 61 70 70 65 6e 64 53 70 61 63 65 28 70 41    appendSpace(pA
a5850 63 63 75 6d 2c 20 6e 73 70 61 63 65 29 3b 0a 20  ccum, nspace);. 
a5860 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
a5870 20 69 66 28 20 7a 45 78 74 72 61 20 29 7b 0a 20   if( zExtra ){. 
a5880 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
a5890 65 28 7a 45 78 74 72 61 29 3b 0a 20 20 20 20 7d  e(zExtra);.    }
a58a0 0a 20 20 7d 2f 2a 20 45 6e 64 20 66 6f 72 20 6c  .  }/* End for l
a58b0 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 66 6f 72  oop over the for
a58c0 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 7d 20  mat string */.} 
a58d0 2f 2a 20 45 6e 64 20 6f 66 20 66 75 6e 63 74 69  /* End of functi
a58e0 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 70 70  on */../*.** App
a58f0 65 6e 64 20 4e 20 62 79 74 65 73 20 6f 66 20 74  end N bytes of t
a5900 65 78 74 20 66 72 6f 6d 20 7a 20 74 6f 20 74 68  ext from z to th
a5910 65 20 53 74 72 41 63 63 75 6d 20 6f 62 6a 65 63  e StrAccum objec
a5920 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
a5930 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
a5940 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28  3StrAccumAppend(
a5950 53 74 72 41 63 63 75 6d 20 2a 70 2c 20 63 6f 6e  StrAccum *p, con
a5960 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20  st char *z, int 
a5970 4e 29 7b 0a 20 20 69 66 28 20 70 2d 3e 74 6f 6f  N){.  if( p->too
a5980 42 69 67 20 7c 20 70 2d 3e 6d 61 6c 6c 6f 63 46  Big | p->mallocF
a5990 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74  ailed ){.    ret
a59a0 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e  urn;.  }.  if( N
a59b0 3c 30 20 29 7b 0a 20 20 20 20 4e 20 3d 20 73 74  <0 ){.    N = st
a59c0 72 6c 65 6e 28 7a 29 3b 0a 20 20 7d 0a 20 20 69  rlen(z);.  }.  i
a59d0 66 28 20 4e 3d 3d 30 20 29 7b 0a 20 20 20 20 72  f( N==0 ){.    r
a59e0 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28  eturn;.  }.  if(
a59f0 20 70 2d 3e 6e 43 68 61 72 2b 4e 20 3e 3d 20 70   p->nChar+N >= p
a5a00 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20  ->nAlloc ){.    
a5a10 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 20 20  char *zNew;.    
a5a20 69 66 28 20 21 70 2d 3e 75 73 65 4d 61 6c 6c 6f  if( !p->useMallo
a5a30 63 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 74 6f  c ){.      p->to
a5a40 6f 42 69 67 20 3d 20 31 3b 0a 20 20 20 20 20 20  oBig = 1;.      
a5a50 4e 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20 2d 20  N = p->nAlloc - 
a5a60 70 2d 3e 6e 43 68 61 72 20 2d 20 31 3b 0a 20 20  p->nChar - 1;.  
a5a70 20 20 20 20 69 66 28 20 4e 3c 3d 30 20 29 7b 0a      if( N<=0 ){.
a5a80 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a          return;.
a5a90 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
a5aa0 65 7b 0a 20 20 20 20 20 20 69 36 34 20 73 7a 4e  e{.      i64 szN
a5ab0 65 77 20 3d 20 70 2d 3e 6e 43 68 61 72 3b 0a 20  ew = p->nChar;. 
a5ac0 20 20 20 20 20 73 7a 4e 65 77 20 2b 3d 20 4e 20       szNew += N 
a5ad0 2b 20 31 3b 0a 20 20 20 20 20 20 69 66 28 20 73  + 1;.      if( s
a5ae0 7a 4e 65 77 20 3e 20 70 2d 3e 6d 78 41 6c 6c 6f  zNew > p->mxAllo
a5af0 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  c ){.        sql
a5b00 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 65  ite3StrAccumRese
a5b10 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d  t(p);.        p-
a5b20 3e 74 6f 6f 42 69 67 20 3d 20 31 3b 0a 20 20 20  >tooBig = 1;.   
a5b30 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
a5b40 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
a5b50 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 73 7a    p->nAlloc = sz
a5b60 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  New;.      }.   
a5b70 20 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65     zNew = sqlite
a5b80 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e  3DbMallocRaw(p->
a5b90 64 62 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 3b  db, p->nAlloc );
a5ba0 0a 20 20 20 20 20 20 69 66 28 20 7a 4e 65 77 20  .      if( zNew 
a5bb0 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  ){.        memcp
a5bc0 79 28 7a 4e 65 77 2c 20 70 2d 3e 7a 54 65 78 74  y(zNew, p->zText
a5bd0 2c 20 70 2d 3e 6e 43 68 61 72 29 3b 0a 20 20 20  , p->nChar);.   
a5be0 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41       sqlite3StrA
a5bf0 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a 20 20  ccumReset(p);.  
a5c00 20 20 20 20 20 20 70 2d 3e 7a 54 65 78 74 20 3d        p->zText =
a5c10 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d 65 6c   zNew;.      }el
a5c20 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6d  se{.        p->m
a5c30 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
a5c40 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
a5c50 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 70 29  StrAccumReset(p)
a5c60 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
a5c70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
a5c80 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 26 70 2d    }.  memcpy(&p-
a5c90 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72 5d  >zText[p->nChar]
a5ca0 2c 20 7a 2c 20 4e 29 3b 0a 20 20 70 2d 3e 6e 43  , z, N);.  p->nC
a5cb0 68 61 72 20 2b 3d 20 4e 3b 0a 7d 0a 0a 2f 2a 0a  har += N;.}../*.
a5cc0 2a 2a 20 46 69 6e 69 73 68 20 6f 66 66 20 61 20  ** Finish off a 
a5cd0 73 74 72 69 6e 67 20 62 79 20 6d 61 6b 69 6e 67  string by making
a5ce0 20 73 75 72 65 20 69 74 20 69 73 20 7a 65 72 6f   sure it is zero
a5cf0 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2a 20  -terminated..** 
a5d00 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
a5d10 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74 69 6e   to the resultin
a5d20 67 20 73 74 72 69 6e 67 2e 20 20 52 65 74 75 72  g string.  Retur
a5d30 6e 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e  n a NULL.** poin
a5d40 74 65 72 20 69 66 20 61 6e 79 20 6b 69 6e 64 20  ter if any kind 
a5d50 6f 66 20 65 72 72 6f 72 20 77 61 73 20 65 6e 63  of error was enc
a5d60 6f 75 6e 74 65 72 65 64 2e 0a 2a 2f 0a 53 51 4c  ountered..*/.SQL
a5d70 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
a5d80 20 2a 73 71 6c 69 74 65 33 53 74 72 41 63 63 75   *sqlite3StrAccu
a5d90 6d 46 69 6e 69 73 68 28 53 74 72 41 63 63 75 6d  mFinish(StrAccum
a5da0 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 7a   *p){.  if( p->z
a5db0 54 65 78 74 20 29 7b 0a 20 20 20 20 70 2d 3e 7a  Text ){.    p->z
a5dc0 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72 5d 20 3d  Text[p->nChar] =
a5dd0 20 30 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75   0;.    if( p->u
a5de0 73 65 4d 61 6c 6c 6f 63 20 26 26 20 70 2d 3e 7a  seMalloc && p->z
a5df0 54 65 78 74 3d 3d 70 2d 3e 7a 42 61 73 65 20 29  Text==p->zBase )
a5e00 7b 0a 20 20 20 20 20 20 70 2d 3e 7a 54 65 78 74  {.      p->zText
a5e10 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
a5e20 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20 70 2d 3e  ocRaw(p->db, p->
a5e30 6e 43 68 61 72 2b 31 20 29 3b 0a 20 20 20 20 20  nChar+1 );.     
a5e40 20 69 66 28 20 70 2d 3e 7a 54 65 78 74 20 29 7b   if( p->zText ){
a5e50 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
a5e60 70 2d 3e 7a 54 65 78 74 2c 20 70 2d 3e 7a 42 61  p->zText, p->zBa
a5e70 73 65 2c 20 70 2d 3e 6e 43 68 61 72 2b 31 29 3b  se, p->nChar+1);
a5e80 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
a5e90 20 20 20 20 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46        p->mallocF
a5ea0 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  ailed = 1;.     
a5eb0 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72   }.    }.  }.  r
a5ec0 65 74 75 72 6e 20 70 2d 3e 7a 54 65 78 74 3b 0a  eturn p->zText;.
a5ed0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 61  }../*.** Reset a
a5ee0 6e 20 53 74 72 41 63 63 75 6d 20 73 74 72 69 6e  n StrAccum strin
a5ef0 67 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20  g.  Reclaim all 
a5f00 6d 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 2e  malloced memory.
a5f10 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
a5f20 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
a5f30 74 72 41 63 63 75 6d 52 65 73 65 74 28 53 74 72  trAccumReset(Str
a5f40 41 63 63 75 6d 20 2a 70 29 7b 0a 20 20 69 66 28  Accum *p){.  if(
a5f50 20 70 2d 3e 7a 54 65 78 74 21 3d 70 2d 3e 7a 42   p->zText!=p->zB
a5f60 61 73 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ase ){.    sqlit
a5f70 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20  e3DbFree(p->db, 
a5f80 70 2d 3e 7a 54 65 78 74 29 3b 0a 20 20 7d 0a 20  p->zText);.  }. 
a5f90 20 70 2d 3e 7a 54 65 78 74 20 3d 20 30 3b 0a 7d   p->zText = 0;.}
a5fa0 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  ../*.** Initiali
a5fb0 7a 65 20 61 20 73 74 72 69 6e 67 20 61 63 63 75  ze a string accu
a5fc0 6d 75 6c 61 74 6f 72 0a 2a 2f 0a 53 51 4c 49 54  mulator.*/.SQLIT
a5fd0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
a5fe0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e  qlite3StrAccumIn
a5ff0 69 74 28 53 74 72 41 63 63 75 6d 20 2a 70 2c 20  it(StrAccum *p, 
a6000 63 68 61 72 20 2a 7a 42 61 73 65 2c 20 69 6e 74  char *zBase, int
a6010 20 6e 2c 20 69 6e 74 20 6d 78 29 7b 0a 20 20 70   n, int mx){.  p
a6020 2d 3e 7a 54 65 78 74 20 3d 20 70 2d 3e 7a 42 61  ->zText = p->zBa
a6030 73 65 20 3d 20 7a 42 61 73 65 3b 0a 20 20 70 2d  se = zBase;.  p-
a6040 3e 64 62 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 43  >db = 0;.  p->nC
a6050 68 61 72 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 41  har = 0;.  p->nA
a6060 6c 6c 6f 63 20 3d 20 6e 3b 0a 20 20 70 2d 3e 6d  lloc = n;.  p->m
a6070 78 41 6c 6c 6f 63 20 3d 20 6d 78 3b 0a 20 20 70  xAlloc = mx;.  p
a6080 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 31 3b  ->useMalloc = 1;
a6090 0a 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 30  .  p->tooBig = 0
a60a0 3b 0a 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ;.  p->mallocFai
a60b0 6c 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  led = 0;.}../*.*
a60c0 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d  * Print into mem
a60d0 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
a60e0 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29  m sqliteMalloc()
a60f0 2e 20 20 55 73 65 20 74 68 65 20 69 6e 74 65 72  .  Use the inter
a6100 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73  nal.** %-convers
a6110 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a  ion extensions..
a6120 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
a6130 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 56  E char *sqlite3V
a6140 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33 20  MPrintf(sqlite3 
a6150 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *db, const char 
a6160 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73  *zFormat, va_lis
a6170 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a  t ap){.  char *z
a6180 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b 53  ;.  char zBase[S
a6190 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f  QLITE_PRINT_BUF_
a61a0 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63 75  SIZE];.  StrAccu
a61b0 6d 20 61 63 63 3b 0a 20 20 73 71 6c 69 74 65 33  m acc;.  sqlite3
a61c0 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61 63  StrAccumInit(&ac
a61d0 63 2c 20 7a 42 61 73 65 2c 20 73 69 7a 65 6f 66  c, zBase, sizeof
a61e0 28 7a 42 61 73 65 29 2c 0a 20 20 20 20 20 20 20  (zBase),.       
a61f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64                 d
a6200 62 20 3f 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  b ? db->aLimit[S
a6210 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
a6220 54 48 5d 20 3a 20 53 51 4c 49 54 45 5f 4d 41 58  TH] : SQLITE_MAX
a6230 5f 4c 45 4e 47 54 48 29 3b 0a 20 20 61 63 63 2e  _LENGTH);.  acc.
a6240 64 62 20 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74  db = db;.  sqlit
a6250 65 33 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c  e3VXPrintf(&acc,
a6260 20 31 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29   1, zFormat, ap)
a6270 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 53  ;.  z = sqlite3S
a6280 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61  trAccumFinish(&a
a6290 63 63 29 3b 0a 20 20 69 66 28 20 61 63 63 2e 6d  cc);.  if( acc.m
a62a0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 64  allocFailed && d
a62b0 62 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c  b ){.    db->mal
a62c0 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
a62d0 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d   }.  return z;.}
a62e0 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 6e  ../*.** Print in
a62f0 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  to memory obtain
a6300 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61  ed from sqliteMa
a6310 6c 6c 6f 63 28 29 2e 20 20 55 73 65 20 74 68 65  lloc().  Use the
a6320 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63   internal.** %-c
a6330 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e 73  onversion extens
a6340 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ions..*/.SQLITE_
a6350 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71  PRIVATE char *sq
a6360 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 73 71 6c  lite3MPrintf(sql
a6370 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
a6380 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e  char *zFormat, .
a6390 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61  ..){.  va_list a
a63a0 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20  p;.  char *z;.  
a63b0 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f  va_start(ap, zFo
a63c0 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c  rmat);.  z = sql
a63d0 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c  ite3VMPrintf(db,
a63e0 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20   zFormat, ap);. 
a63f0 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72   va_end(ap);.  r
a6400 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn z;.}../*.*
a6410 2a 20 4c 69 6b 65 20 73 71 6c 69 74 65 33 4d 50  * Like sqlite3MP
a6420 72 69 6e 74 66 28 29 2c 20 62 75 74 20 63 61 6c  rintf(), but cal
a6430 6c 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28  l sqlite3DbFree(
a6440 29 20 6f 6e 20 7a 53 74 72 20 61 66 74 65 72 20  ) on zStr after 
a6450 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 74 68  formatting.** th
a6460 65 20 73 74 72 69 6e 67 20 61 6e 64 20 62 65 66  e string and bef
a6470 6f 72 65 20 72 65 74 75 72 6e 6e 69 6e 67 2e 20  ore returnning. 
a6480 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
a6490 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20   intended to be 
a64a0 75 73 65 64 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66  used.** to modif
a64b0 79 20 61 6e 20 65 78 69 73 74 69 6e 67 20 73 74  y an existing st
a64c0 72 69 6e 67 2e 20 20 46 6f 72 20 65 78 61 6d 70  ring.  For examp
a64d0 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  le:.**.**       
a64e0 78 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  x = sqlite3MPrin
a64f0 74 66 28 64 62 2c 20 78 2c 20 22 70 72 65 66 69  tf(db, x, "prefi
a6500 78 20 25 73 20 73 75 66 66 69 78 22 2c 20 78 29  x %s suffix", x)
a6510 3b 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ;.**.*/.SQLITE_P
a6520 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
a6530 69 74 65 33 4d 41 70 70 65 6e 64 66 28 73 71 6c  ite3MAppendf(sql
a6540 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72 20 2a  ite3 *db, char *
a6550 7a 53 74 72 2c 20 63 6f 6e 73 74 20 63 68 61 72  zStr, const char
a6560 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b   *zFormat, ...){
a6570 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20  .  va_list ap;. 
a6580 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 73   char *z;.  va_s
a6590 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74  tart(ap, zFormat
a65a0 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33  );.  z = sqlite3
a65b0 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f  VMPrintf(db, zFo
a65c0 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f  rmat, ap);.  va_
a65d0 65 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74  end(ap);.  sqlit
a65e0 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 74  e3DbFree(db, zSt
a65f0 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a  r);.  return z;.
a6600 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69  }../*.** Print i
a6610 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69  nto memory obtai
a6620 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
a6630 5f 6d 61 6c 6c 6f 63 28 29 2e 20 20 4f 6d 69 74  _malloc().  Omit
a6640 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a   the internal.**
a6650 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78   %-conversion ex
a6660 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c  tensions..*/.SQL
a6670 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71  ITE_API char *sq
a6680 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 63  lite3_vmprintf(c
a6690 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
a66a0 61 74 2c 20 76 61 5f 6c 69 73 74 20 61 70 29 7b  at, va_list ap){
a66b0 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 63 68  .  char *z;.  ch
a66c0 61 72 20 7a 42 61 73 65 5b 53 51 4c 49 54 45 5f  ar zBase[SQLITE_
a66d0 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 5d 3b  PRINT_BUF_SIZE];
a66e0 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63 63 3b  .  StrAccum acc;
a66f0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
a6700 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20  OMIT_AUTOINIT.  
a6710 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  if( sqlite3_init
a6720 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72  ialize() ) retur
a6730 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71  n 0;.#endif.  sq
a6740 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69  lite3StrAccumIni
a6750 74 28 26 61 63 63 2c 20 7a 42 61 73 65 2c 20 73  t(&acc, zBase, s
a6760 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 20 53 51  izeof(zBase), SQ
a6770 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 29  LITE_MAX_LENGTH)
a6780 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69  ;.  sqlite3VXPri
a6790 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f  ntf(&acc, 0, zFo
a67a0 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 7a 20 3d  rmat, ap);.  z =
a67b0 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
a67c0 46 69 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20  Finish(&acc);.  
a67d0 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a  return z;.}../*.
a67e0 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65  ** Print into me
a67f0 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
a6800 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  om sqlite3_mallo
a6810 63 28 29 28 29 2e 20 20 4f 6d 69 74 20 74 68 65  c()().  Omit the
a6820 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63   internal.** %-c
a6830 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e 73  onversion extens
a6840 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ions..*/.SQLITE_
a6850 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65  API char *sqlite
a6860 33 5f 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20  3_mprintf(const 
a6870 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e  char *zFormat, .
a6880 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61  ..){.  va_list a
a6890 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 23 69  p;.  char *z;.#i
a68a0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
a68b0 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28  T_AUTOINIT.  if(
a68c0 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
a68d0 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20 30  ize() ) return 0
a68e0 3b 0a 23 65 6e 64 69 66 0a 20 20 76 61 5f 73 74  ;.#endif.  va_st
a68f0 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29  art(ap, zFormat)
a6900 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f  ;.  z = sqlite3_
a6910 76 6d 70 72 69 6e 74 66 28 7a 46 6f 72 6d 61 74  vmprintf(zFormat
a6920 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28  , ap);.  va_end(
a6930 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b  ap);.  return z;
a6940 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 73 71 6c 69 74 65  .}../*.** sqlite
a6950 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 77 6f 72  3_snprintf() wor
a6960 6b 73 20 6c 69 6b 65 20 73 6e 70 72 69 6e 74 66  ks like snprintf
a6970 28 29 20 65 78 63 65 70 74 20 74 68 61 74 20 69  () except that i
a6980 74 20 69 67 6e 6f 72 65 73 20 74 68 65 0a 2a 2a  t ignores the.**
a6990 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 6c 65 20   current locale 
a69a0 73 65 74 74 69 6e 67 73 2e 20 20 54 68 69 73 20  settings.  This 
a69b0 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 66 6f 72  is important for
a69c0 20 53 51 4c 69 74 65 20 62 65 63 61 75 73 65 20   SQLite because 
a69d0 77 65 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 61 62  we.** are not ab
a69e0 6c 65 20 74 6f 20 75 73 65 20 61 20 22 2c 22 20  le to use a "," 
a69f0 61 73 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70  as the decimal p
a6a00 6f 69 6e 74 20 69 6e 20 70 6c 61 63 65 20 6f 66  oint in place of
a6a10 20 22 2e 22 20 61 73 0a 2a 2a 20 73 70 65 63 69   "." as.** speci
a6a20 66 69 65 64 20 62 79 20 73 6f 6d 65 20 6c 6f 63  fied by some loc
a6a30 61 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ales..*/.SQLITE_
a6a40 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65  API char *sqlite
a6a50 33 5f 73 6e 70 72 69 6e 74 66 28 69 6e 74 20 6e  3_snprintf(int n
a6a60 2c 20 63 68 61 72 20 2a 7a 42 75 66 2c 20 63 6f  , char *zBuf, co
a6a70 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61  nst char *zForma
a6a80 74 2c 20 2e 2e 2e 29 7b 0a 20 20 63 68 61 72 20  t, ...){.  char 
a6a90 2a 7a 3b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70  *z;.  va_list ap
a6aa0 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63 63  ;.  StrAccum acc
a6ab0 3b 0a 0a 20 20 69 66 28 20 6e 3c 3d 30 20 29 7b  ;..  if( n<=0 ){
a6ac0 0a 20 20 20 20 72 65 74 75 72 6e 20 7a 42 75 66  .    return zBuf
a6ad0 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53  ;.  }.  sqlite3S
a6ae0 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61 63 63  trAccumInit(&acc
a6af0 2c 20 7a 42 75 66 2c 20 6e 2c 20 30 29 3b 0a 20  , zBuf, n, 0);. 
a6b00 20 61 63 63 2e 75 73 65 4d 61 6c 6c 6f 63 20 3d   acc.useMalloc =
a6b10 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61   0;.  va_start(a
a6b20 70 2c 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 73 71  p,zFormat);.  sq
a6b30 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 26 61  lite3VXPrintf(&a
a6b40 63 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 74 2c 20  cc, 0, zFormat, 
a6b50 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70  ap);.  va_end(ap
a6b60 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33  );.  z = sqlite3
a6b70 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26  StrAccumFinish(&
a6b80 61 63 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a  acc);.  return z
a6b90 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64  ;.}..#if defined
a6ba0 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f  (SQLITE_DEBUG)./
a6bb0 2a 0a 2a 2a 20 41 20 76 65 72 73 69 6f 6e 20 6f  *.** A version o
a6bc0 66 20 70 72 69 6e 74 66 28 29 20 74 68 61 74 20  f printf() that 
a6bd0 75 6e 64 65 72 73 74 61 6e 64 73 20 25 6c 6c 64  understands %lld
a6be0 2e 20 20 55 73 65 64 20 66 6f 72 20 64 65 62 75  .  Used for debu
a6bf0 67 67 69 6e 67 2e 0a 2a 2a 20 54 68 65 20 70 72  gging..** The pr
a6c00 69 6e 74 66 28 29 20 62 75 69 6c 74 20 69 6e 74  intf() built int
a6c10 6f 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20  o some versions 
a6c20 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f 65 73 20  of windows does 
a6c30 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 20 25  not understand %
a6c40 6c 6c 64 0a 2a 2a 20 61 6e 64 20 73 65 67 66 61  lld.** and segfa
a6c50 75 6c 74 73 20 69 66 20 79 6f 75 20 67 69 76 65  ults if you give
a6c60 20 69 74 20 61 20 6c 6f 6e 67 20 6c 6f 6e 67 20   it a long long 
a6c70 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  int..*/.SQLITE_P
a6c80 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
a6c90 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 63  te3DebugPrintf(c
a6ca0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
a6cb0 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c  at, ...){.  va_l
a6cc0 69 73 74 20 61 70 3b 0a 20 20 53 74 72 41 63 63  ist ap;.  StrAcc
a6cd0 75 6d 20 61 63 63 3b 0a 20 20 63 68 61 72 20 7a  um acc;.  char z
a6ce0 42 75 66 5b 35 30 30 5d 3b 0a 20 20 73 71 6c 69  Buf[500];.  sqli
a6cf0 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28  te3StrAccumInit(
a6d00 26 61 63 63 2c 20 7a 42 75 66 2c 20 73 69 7a 65  &acc, zBuf, size
a6d10 6f 66 28 7a 42 75 66 29 2c 20 30 29 3b 0a 20 20  of(zBuf), 0);.  
a6d20 61 63 63 2e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20  acc.useMalloc = 
a6d30 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70  0;.  va_start(ap
a6d40 2c 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 73 71 6c  ,zFormat);.  sql
a6d50 69 74 65 33 56 58 50 72 69 6e 74 66 28 26 61 63  ite3VXPrintf(&ac
a6d60 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 61  c, 0, zFormat, a
a6d70 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29  p);.  va_end(ap)
a6d80 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63  ;.  sqlite3StrAc
a6d90 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b  cumFinish(&acc);
a6da0 0a 20 20 66 70 72 69 6e 74 66 28 73 74 64 6f 75  .  fprintf(stdou
a6db0 74 2c 22 25 73 22 2c 20 7a 42 75 66 29 3b 0a 20  t,"%s", zBuf);. 
a6dc0 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b   fflush(stdout);
a6dd0 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a  .}.#endif../****
a6de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
a6df0 66 20 70 72 69 6e 74 66 2e 63 20 2a 2a 2a 2a 2a  f printf.c *****
a6e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
a6e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
a6e40 20 66 69 6c 65 20 72 61 6e 64 6f 6d 2e 63 20 2a   file random.c *
a6e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
a6e80 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
a6e90 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
a6ea0 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
a6eb0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
a6ec0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
a6ed0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
a6ee0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
a6ef0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
a6f00 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
a6f10 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
a6f20 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
a6f30 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
a6f40 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
a6f50 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
a6f60 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
a6f70 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
a6f80 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
a6f90 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
a6fa0 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
a6fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6ff0 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
a7000 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 6f 20  ontains code to 
a7010 69 6d 70 6c 65 6d 65 6e 74 20 61 20 70 73 65 75  implement a pseu
a7020 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72  do-random number
a7030 0a 2a 2a 20 67 65 6e 65 72 61 74 6f 72 20 28 50  .** generator (P
a7040 52 4e 47 29 20 66 6f 72 20 53 51 4c 69 74 65 2e  RNG) for SQLite.
a7050 0a 2a 2a 0a 2a 2a 20 52 61 6e 64 6f 6d 20 6e 75  .**.** Random nu
a7060 6d 62 65 72 73 20 61 72 65 20 75 73 65 64 20 62  mbers are used b
a7070 79 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 64 61  y some of the da
a7080 74 61 62 61 73 65 20 62 61 63 6b 65 6e 64 73 20  tabase backends 
a7090 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 67  in order.** to g
a70a0 65 6e 65 72 61 74 65 20 72 61 6e 64 6f 6d 20 69  enerate random i
a70b0 6e 74 65 67 65 72 20 6b 65 79 73 20 66 6f 72 20  nteger keys for 
a70c0 74 61 62 6c 65 73 20 6f 72 20 72 61 6e 64 6f 6d  tables or random
a70d0 20 66 69 6c 65 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a   filenames..**.*
a70e0 2a 20 24 49 64 3a 20 72 61 6e 64 6f 6d 2e 63 2c  * $Id: random.c,
a70f0 76 20 31 2e 32 37 20 32 30 30 38 2f 31 30 2f 30  v 1.27 2008/10/0
a7100 37 20 31 35 3a 32 35 3a 34 38 20 64 72 68 20 45  7 15:25:48 drh E
a7110 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 20 41 6c 6c  xp $.*/.../* All
a7120 20 74 68 72 65 61 64 73 20 73 68 61 72 65 20 61   threads share a
a7130 20 73 69 6e 67 6c 65 20 72 61 6e 64 6f 6d 20 6e   single random n
a7140 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 2e  umber generator.
a7150 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75  .** This structu
a7160 72 65 20 69 73 20 74 68 65 20 63 75 72 72 65 6e  re is the curren
a7170 74 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 67  t state of the g
a7180 65 6e 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61  enerator..*/.sta
a7190 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73  tic SQLITE_WSD s
a71a0 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e  truct sqlite3Prn
a71b0 67 54 79 70 65 20 7b 0a 20 20 75 6e 73 69 67 6e  gType {.  unsign
a71c0 65 64 20 63 68 61 72 20 69 73 49 6e 69 74 3b 20  ed char isInit; 
a71d0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
a71e0 20 69 66 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   if initialized 
a71f0 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
a7200 61 72 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20  ar i, j;        
a7210 20 20 20 20 2f 2a 20 53 74 61 74 65 20 76 61 72      /* State var
a7220 69 61 62 6c 65 73 20 2a 2f 0a 20 20 75 6e 73 69  iables */.  unsi
a7230 67 6e 65 64 20 63 68 61 72 20 73 5b 32 35 36 5d  gned char s[256]
a7240 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74  ;          /* St
a7250 61 74 65 20 76 61 72 69 61 62 6c 65 73 20 2a 2f  ate variables */
a7260 0a 7d 20 73 71 6c 69 74 65 33 50 72 6e 67 20 3d  .} sqlite3Prng =
a7270 20 7b 20 30 2c 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20   { 0, };../*.** 
a7280 47 65 74 20 61 20 73 69 6e 67 6c 65 20 38 2d 62  Get a single 8-b
a7290 69 74 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20  it random value 
a72a0 66 72 6f 6d 20 74 68 65 20 52 43 34 20 50 52 4e  from the RC4 PRN
a72b0 47 2e 20 20 54 68 65 20 4d 75 74 65 78 0a 2a 2a  G.  The Mutex.**
a72c0 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68   must be held wh
a72d0 69 6c 65 20 65 78 65 63 75 74 69 6e 67 20 74 68  ile executing th
a72e0 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a  is routine..**.*
a72f0 2a 20 57 68 79 20 6e 6f 74 20 6a 75 73 74 20 75  * Why not just u
a7300 73 65 20 61 20 6c 69 62 72 61 72 79 20 72 61 6e  se a library ran
a7310 64 6f 6d 20 67 65 6e 65 72 61 74 6f 72 20 6c 69  dom generator li
a7320 6b 65 20 6c 72 61 6e 64 34 38 28 29 20 66 6f 72  ke lrand48() for
a7330 20 74 68 69 73 3f 0a 2a 2a 20 42 65 63 61 75 73   this?.** Becaus
a7340 65 20 74 68 65 20 4f 50 5f 4e 65 77 52 6f 77 69  e the OP_NewRowi
a7350 64 20 6f 70 63 6f 64 65 20 69 6e 20 74 68 65 20  d opcode in the 
a7360 56 44 42 45 20 64 65 70 65 6e 64 73 20 6f 6e 20  VDBE depends on 
a7370 68 61 76 69 6e 67 20 61 20 76 65 72 79 0a 2a 2a  having a very.**
a7380 20 67 6f 6f 64 20 73 6f 75 72 63 65 20 6f 66 20   good source of 
a7390 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 73 2e 20  random numbers. 
a73a0 20 54 68 65 20 6c 72 61 6e 64 34 38 28 29 20 6c   The lrand48() l
a73b0 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 20  ibrary function 
a73c0 6d 61 79 0a 2a 2a 20 77 65 6c 6c 20 62 65 20 67  may.** well be g
a73d0 6f 6f 64 20 65 6e 6f 75 67 68 2e 20 20 42 75 74  ood enough.  But
a73e0 20 6d 61 79 62 65 20 6e 6f 74 2e 20 20 4f 72 20   maybe not.  Or 
a73f0 6d 61 79 62 65 20 6c 72 61 6e 64 34 38 28 29 20  maybe lrand48() 
a7400 68 61 73 20 73 6f 6d 65 0a 2a 2a 20 73 75 62 74  has some.** subt
a7410 6c 65 20 70 72 6f 62 6c 65 6d 73 20 6f 6e 20 73  le problems on s
a7420 6f 6d 65 20 73 79 73 74 65 6d 73 20 74 68 61 74  ome systems that
a7430 20 63 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f   could cause pro
a7440 62 6c 65 6d 73 2e 20 20 49 74 20 69 73 20 68 61  blems.  It is ha
a7450 72 64 0a 2a 2a 20 74 6f 20 6b 6e 6f 77 2e 20 20  rd.** to know.  
a7460 54 6f 20 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20  To minimize the 
a7470 72 69 73 6b 20 6f 66 20 70 72 6f 62 6c 65 6d 73  risk of problems
a7480 20 64 75 65 20 74 6f 20 62 61 64 20 6c 72 61 6e   due to bad lran
a7490 64 34 38 28 29 0a 2a 2a 20 69 6d 70 6c 65 6d 65  d48().** impleme
a74a0 6e 74 61 74 69 6f 6e 73 2c 20 53 51 4c 69 74 65  ntations, SQLite
a74b0 20 75 73 65 73 20 74 68 69 73 20 72 61 6e 64 6f   uses this rando
a74c0 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74  m number generat
a74d0 6f 72 20 62 61 73 65 64 0a 2a 2a 20 6f 6e 20 52  or based.** on R
a74e0 43 34 2c 20 77 68 69 63 68 20 77 65 20 6b 6e 6f  C4, which we kno
a74f0 77 20 77 6f 72 6b 73 20 76 65 72 79 20 77 65 6c  w works very wel
a7500 6c 2e 0a 2a 2a 0a 2a 2a 20 28 4c 61 74 65 72 29  l..**.** (Later)
a7510 3a 20 20 41 63 74 75 61 6c 6c 79 2c 20 4f 50 5f  :  Actually, OP_
a7520 4e 65 77 52 6f 77 69 64 20 64 6f 65 73 20 6e 6f  NewRowid does no
a7530 74 20 64 65 70 65 6e 64 20 6f 6e 20 61 20 67 6f  t depend on a go
a7540 6f 64 20 73 6f 75 72 63 65 20 6f 66 0a 2a 2a 20  od source of.** 
a7550 72 61 6e 64 6f 6d 6e 65 73 73 20 61 6e 79 20 6d  randomness any m
a7560 6f 72 65 2e 20 20 42 75 74 20 77 65 20 77 69 6c  ore.  But we wil
a7570 6c 20 6c 65 61 76 65 20 74 68 69 73 20 63 6f 64  l leave this cod
a7580 65 20 69 6e 20 61 6c 6c 20 74 68 65 20 73 61 6d  e in all the sam
a7590 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
a75a0 20 72 61 6e 64 6f 6d 42 79 74 65 28 76 6f 69 64   randomByte(void
a75b0 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  ){.  unsigned ch
a75c0 61 72 20 74 3b 0a 0a 0a 20 20 2f 2a 20 54 68 65  ar t;...  /* The
a75d0 20 22 77 73 64 50 72 6e 67 22 20 6d 61 63 72 6f   "wsdPrng" macro
a75e0 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f   will resolve to
a75f0 20 74 68 65 20 70 73 65 75 64 6f 2d 72 61 6e 64   the pseudo-rand
a7600 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 61  om number genera
a7610 74 6f 72 0a 20 20 2a 2a 20 73 74 61 74 65 20 76  tor.  ** state v
a7620 65 63 74 6f 72 2e 20 20 49 66 20 77 72 69 74 61  ector.  If writa
a7630 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20  ble static data 
a7640 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6f  is unsupported o
a7650 6e 20 74 68 65 20 74 61 72 67 65 74 2c 0a 20 20  n the target,.  
a7660 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f  ** we have to lo
a7670 63 61 74 65 20 74 68 65 20 73 74 61 74 65 20 76  cate the state v
a7680 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d  ector at run-tim
a7690 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 20  e.  In the more 
a76a0 63 6f 6d 6d 6f 6e 0a 20 20 2a 2a 20 63 61 73 65  common.  ** case
a76b0 20 77 68 65 72 65 20 77 72 69 74 61 62 6c 65 20   where writable 
a76c0 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 73  static data is s
a76d0 75 70 70 6f 72 74 65 64 2c 20 77 73 64 50 72 6e  upported, wsdPrn
a76e0 67 20 63 61 6e 20 72 65 66 65 72 20 64 69 72 65  g can refer dire
a76f0 63 74 6c 79 0a 20 20 2a 2a 20 74 6f 20 74 68 65  ctly.  ** to the
a7700 20 22 73 71 6c 69 74 65 33 50 72 6e 67 22 20 73   "sqlite3Prng" s
a7710 74 61 74 65 20 76 65 63 74 6f 72 20 64 65 63 6c  tate vector decl
a7720 61 72 65 64 20 61 62 6f 76 65 2e 0a 20 20 2a 2f  ared above..  */
a7730 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
a7740 4d 49 54 5f 57 53 44 0a 20 20 73 74 72 75 63 74  MIT_WSD.  struct
a7750 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65   sqlite3PrngType
a7760 20 2a 70 20 3d 20 26 47 4c 4f 42 41 4c 28 73 74   *p = &GLOBAL(st
a7770 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67  ruct sqlite3Prng
a7780 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e  Type, sqlite3Prn
a7790 67 29 3b 0a 23 20 64 65 66 69 6e 65 20 77 73 64  g);.# define wsd
a77a0 50 72 6e 67 20 70 5b 30 5d 0a 23 65 6c 73 65 0a  Prng p[0].#else.
a77b0 23 20 64 65 66 69 6e 65 20 77 73 64 50 72 6e 67  # define wsdPrng
a77c0 20 73 71 6c 69 74 65 33 50 72 6e 67 0a 23 65 6e   sqlite3Prng.#en
a77d0 64 69 66 0a 0a 0a 20 20 2f 2a 20 49 6e 69 74 69  dif...  /* Initi
a77e0 61 6c 69 7a 65 20 74 68 65 20 73 74 61 74 65 20  alize the state 
a77f0 6f 66 20 74 68 65 20 72 61 6e 64 6f 6d 20 6e 75  of the random nu
a7800 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 6f  mber generator o
a7810 6e 63 65 2c 0a 20 20 2a 2a 20 74 68 65 20 66 69  nce,.  ** the fi
a7820 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 72 6f  rst time this ro
a7830 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e  utine is called.
a7840 20 20 54 68 65 20 73 65 65 64 20 76 61 6c 75 65    The seed value
a7850 20 64 6f 65 73 0a 20 20 2a 2a 20 6e 6f 74 20 6e   does.  ** not n
a7860 65 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61  eed to contain a
a7870 20 6c 6f 74 20 6f 66 20 72 61 6e 64 6f 6d 6e 65   lot of randomne
a7880 73 73 20 73 69 6e 63 65 20 77 65 20 61 72 65 20  ss since we are 
a7890 6e 6f 74 0a 20 20 2a 2a 20 74 72 79 69 6e 67 20  not.  ** trying 
a78a0 74 6f 20 64 6f 20 73 65 63 75 72 65 20 65 6e 63  to do secure enc
a78b0 72 79 70 74 69 6f 6e 20 6f 72 20 61 6e 79 74 68  ryption or anyth
a78c0 69 6e 67 20 6c 69 6b 65 20 74 68 61 74 2e 2e 2e  ing like that...
a78d0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 68 69  .  **.  ** Nothi
a78e0 6e 67 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  ng in this file 
a78f0 6f 72 20 61 6e 79 77 68 65 72 65 20 65 6c 73 65  or anywhere else
a7900 20 69 6e 20 53 51 4c 69 74 65 20 64 6f 65 73 20   in SQLite does 
a7910 61 6e 79 20 6b 69 6e 64 20 6f 66 0a 20 20 2a 2a  any kind of.  **
a7920 20 65 6e 63 72 79 70 74 69 6f 6e 2e 20 20 54 68   encryption.  Th
a7930 65 20 52 43 34 20 61 6c 67 6f 72 69 74 68 6d 20  e RC4 algorithm 
a7940 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 61 73  is being used as
a7950 20 61 20 50 52 4e 47 20 28 70 73 65 75 64 6f 2d   a PRNG (pseudo-
a7960 72 61 6e 64 6f 6d 0a 20 20 2a 2a 20 6e 75 6d 62  random.  ** numb
a7970 65 72 20 67 65 6e 65 72 61 74 6f 72 29 20 6e 6f  er generator) no
a7980 74 20 61 73 20 61 6e 20 65 6e 63 72 79 70 74 69  t as an encrypti
a7990 6f 6e 20 64 65 76 69 63 65 2e 0a 20 20 2a 2f 0a  on device..  */.
a79a0 20 20 69 66 28 20 21 77 73 64 50 72 6e 67 2e 69    if( !wsdPrng.i
a79b0 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 69 6e 74  sInit ){.    int
a79c0 20 69 3b 0a 20 20 20 20 63 68 61 72 20 6b 5b 32   i;.    char k[2
a79d0 35 36 5d 3b 0a 20 20 20 20 77 73 64 50 72 6e 67  56];.    wsdPrng
a79e0 2e 6a 20 3d 20 30 3b 0a 20 20 20 20 77 73 64 50  .j = 0;.    wsdP
a79f0 72 6e 67 2e 69 20 3d 20 30 3b 0a 20 20 20 20 73  rng.i = 0;.    s
a7a00 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f 6d 6e 65  qlite3OsRandomne
a7a10 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 5f 66  ss(sqlite3_vfs_f
a7a20 69 6e 64 28 30 29 2c 20 32 35 36 2c 20 6b 29 3b  ind(0), 256, k);
a7a30 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
a7a40 32 35 36 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  256; i++){.     
a7a50 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20 3d 20   wsdPrng.s[i] = 
a7a60 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  i;.    }.    for
a7a70 28 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b 2b  (i=0; i<256; i++
a7a80 29 7b 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67  ){.      wsdPrng
a7a90 2e 6a 20 2b 3d 20 77 73 64 50 72 6e 67 2e 73 5b  .j += wsdPrng.s[
a7aa0 69 5d 20 2b 20 6b 5b 69 5d 3b 0a 20 20 20 20 20  i] + k[i];.     
a7ab0 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77   t = wsdPrng.s[w
a7ac0 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 20 20 20  sdPrng.j];.     
a7ad0 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72   wsdPrng.s[wsdPr
a7ae0 6e 67 2e 6a 5d 20 3d 20 77 73 64 50 72 6e 67 2e  ng.j] = wsdPrng.
a7af0 73 5b 69 5d 3b 0a 20 20 20 20 20 20 77 73 64 50  s[i];.      wsdP
a7b00 72 6e 67 2e 73 5b 69 5d 20 3d 20 74 3b 0a 20 20  rng.s[i] = t;.  
a7b10 20 20 7d 0a 20 20 20 20 77 73 64 50 72 6e 67 2e    }.    wsdPrng.
a7b20 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a  isInit = 1;.  }.
a7b30 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61  .  /* Generate a
a7b40 6e 64 20 72 65 74 75 72 6e 20 73 69 6e 67 6c 65  nd return single
a7b50 20 72 61 6e 64 6f 6d 20 62 79 74 65 0a 20 20 2a   random byte.  *
a7b60 2f 0a 20 20 77 73 64 50 72 6e 67 2e 69 2b 2b 3b  /.  wsdPrng.i++;
a7b70 0a 20 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73  .  t = wsdPrng.s
a7b80 5b 77 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 77  [wsdPrng.i];.  w
a7b90 73 64 50 72 6e 67 2e 6a 20 2b 3d 20 74 3b 0a 20  sdPrng.j += t;. 
a7ba0 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72   wsdPrng.s[wsdPr
a7bb0 6e 67 2e 69 5d 20 3d 20 77 73 64 50 72 6e 67 2e  ng.i] = wsdPrng.
a7bc0 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20  s[wsdPrng.j];.  
a7bd0 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e  wsdPrng.s[wsdPrn
a7be0 67 2e 6a 5d 20 3d 20 74 3b 0a 20 20 74 20 2b 3d  g.j] = t;.  t +=
a7bf0 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72   wsdPrng.s[wsdPr
a7c00 6e 67 2e 69 5d 3b 0a 20 20 72 65 74 75 72 6e 20  ng.i];.  return 
a7c10 77 73 64 50 72 6e 67 2e 73 5b 74 5d 3b 0a 7d 0a  wsdPrng.s[t];.}.
a7c20 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 20  ./*.** Return N 
a7c30 72 61 6e 64 6f 6d 20 62 79 74 65 73 2e 0a 2a 2f  random bytes..*/
a7c40 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
a7c50 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
a7c60 65 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20  ess(int N, void 
a7c70 2a 70 42 75 66 29 7b 0a 20 20 75 6e 73 69 67 6e  *pBuf){.  unsign
a7c80 65 64 20 63 68 61 72 20 2a 7a 42 75 66 20 3d 20  ed char *zBuf = 
a7c90 70 42 75 66 3b 0a 23 69 66 20 53 51 4c 49 54 45  pBuf;.#if SQLITE
a7ca0 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 71  _THREADSAFE.  sq
a7cb0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
a7cc0 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ex = sqlite3Mute
a7cd0 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
a7ce0 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 29  TEX_STATIC_PRNG)
a7cf0 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74  ;.#endif.  sqlit
a7d00 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
a7d10 75 74 65 78 29 3b 0a 20 20 77 68 69 6c 65 28 20  utex);.  while( 
a7d20 4e 2d 2d 20 29 7b 0a 20 20 20 20 2a 28 7a 42 75  N-- ){.    *(zBu
a7d30 66 2b 2b 29 20 3d 20 72 61 6e 64 6f 6d 42 79 74  f++) = randomByt
a7d40 65 28 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  e();.  }.  sqlit
a7d50 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
a7d60 75 74 65 78 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65  utex);.}..#ifnde
a7d70 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55  f SQLITE_OMIT_BU
a7d80 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a  ILTIN_TEST./*.**
a7d90 20 46 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72   For testing pur
a7da0 70 6f 73 65 73 2c 20 77 65 20 73 6f 6d 65 74 69  poses, we someti
a7db0 6d 65 73 20 77 61 6e 74 20 74 6f 20 70 72 65 73  mes want to pres
a7dc0 65 72 76 65 20 74 68 65 20 73 74 61 74 65 20 6f  erve the state o
a7dd0 66 0a 2a 2a 20 50 52 4e 47 20 61 6e 64 20 72 65  f.** PRNG and re
a7de0 73 74 6f 72 65 20 74 68 65 20 50 52 4e 47 20 74  store the PRNG t
a7df0 6f 20 69 74 73 20 73 61 76 65 64 20 73 74 61 74  o its saved stat
a7e00 65 20 61 74 20 61 20 6c 61 74 65 72 20 74 69 6d  e at a later tim
a7e10 65 2c 20 6f 72 0a 2a 2a 20 74 6f 20 72 65 73 65  e, or.** to rese
a7e20 74 20 74 68 65 20 50 52 4e 47 20 74 6f 20 69 74  t the PRNG to it
a7e30 73 20 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2e  s initial state.
a7e40 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73    These routines
a7e50 20 61 63 63 6f 6d 70 6c 69 73 68 0a 2a 2a 20 74   accomplish.** t
a7e60 68 6f 73 65 20 74 61 73 6b 73 2e 0a 2a 2a 0a 2a  hose tasks..**.*
a7e70 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 74 65  * The sqlite3_te
a7e80 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74  st_control() int
a7e90 65 72 66 61 63 65 20 63 61 6c 6c 73 20 74 68 65  erface calls the
a7ea0 73 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a  se routines to.*
a7eb0 2a 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 50 52  * control the PR
a7ec0 4e 47 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51  NG..*/.static SQ
a7ed0 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20  LITE_WSD struct 
a7ee0 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 20  sqlite3PrngType 
a7ef0 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67  sqlite3SavedPrng
a7f00 20 3d 20 7b 20 30 2c 20 7d 3b 0a 53 51 4c 49 54   = { 0, };.SQLIT
a7f10 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
a7f20 71 6c 69 74 65 33 50 72 6e 67 53 61 76 65 53 74  qlite3PrngSaveSt
a7f30 61 74 65 28 76 6f 69 64 29 7b 0a 20 20 6d 65 6d  ate(void){.  mem
a7f40 63 70 79 28 0a 20 20 20 20 26 47 4c 4f 42 41 4c  cpy(.    &GLOBAL
a7f50 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50  (struct sqlite3P
a7f60 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33  rngType, sqlite3
a7f70 53 61 76 65 64 50 72 6e 67 29 2c 0a 20 20 20 20  SavedPrng),.    
a7f80 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73  &GLOBAL(struct s
a7f90 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20  qlite3PrngType, 
a7fa0 73 71 6c 69 74 65 33 50 72 6e 67 29 2c 0a 20 20  sqlite3Prng),.  
a7fb0 20 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33    sizeof(sqlite3
a7fc0 50 72 6e 67 29 0a 20 20 29 3b 0a 7d 0a 53 51 4c  Prng).  );.}.SQL
a7fd0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
a7fe0 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 74   sqlite3PrngRest
a7ff0 6f 72 65 53 74 61 74 65 28 76 6f 69 64 29 7b 0a  oreState(void){.
a8000 20 20 6d 65 6d 63 70 79 28 0a 20 20 20 20 26 47    memcpy(.    &G
a8010 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c  LOBAL(struct sql
a8020 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71  ite3PrngType, sq
a8030 6c 69 74 65 33 50 72 6e 67 29 2c 0a 20 20 20 20  lite3Prng),.    
a8040 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73  &GLOBAL(struct s
a8050 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20  qlite3PrngType, 
a8060 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67  sqlite3SavedPrng
a8070 29 2c 0a 20 20 20 20 73 69 7a 65 6f 66 28 73 71  ),.    sizeof(sq
a8080 6c 69 74 65 33 50 72 6e 67 29 0a 20 20 29 3b 0a  lite3Prng).  );.
a8090 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
a80a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e   void sqlite3Prn
a80b0 67 52 65 73 65 74 53 74 61 74 65 28 76 6f 69 64  gResetState(void
a80c0 29 7b 0a 20 20 47 4c 4f 42 41 4c 28 73 74 72 75  ){.  GLOBAL(stru
a80d0 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79  ct sqlite3PrngTy
a80e0 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29  pe, sqlite3Prng)
a80f0 2e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 7d 0a 23  .isInit = 0;.}.#
a8100 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
a8110 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53  OMIT_BUILTIN_TES
a8120 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  T */../*********
a8130 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 61 6e  ***** End of ran
a8140 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dom.c **********
a8150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8170 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
a8180 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
a8190 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a   utf.c *********
a81a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a81b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a81c0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34  ****/./*.** 2004
a81d0 20 41 70 72 69 6c 20 31 33 0a 2a 2a 0a 2a 2a 20   April 13.**.** 
a81e0 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
a81f0 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
a8200 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
a8210 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
a8220 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
a8230 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
a8240 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
a8250 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
a8260 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
a8270 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
a8280 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
a8290 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
a82a0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
a82b0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
a82c0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
a82d0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
a82e0 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
a82f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8330 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
a8340 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72   file contains r
a8350 6f 75 74 69 6e 65 73 20 75 73 65 64 20 74 6f 20  outines used to 
a8360 74 72 61 6e 73 6c 61 74 65 20 62 65 74 77 65 65  translate betwee
a8370 6e 20 55 54 46 2d 38 2c 20 0a 2a 2a 20 55 54 46  n UTF-8, .** UTF
a8380 2d 31 36 2c 20 55 54 46 2d 31 36 42 45 2c 20 61  -16, UTF-16BE, a
a8390 6e 64 20 55 54 46 2d 31 36 4c 45 2e 0a 2a 2a 0a  nd UTF-16LE..**.
a83a0 2a 2a 20 24 49 64 3a 20 75 74 66 2e 63 2c 76 20  ** $Id: utf.c,v 
a83b0 31 2e 36 36 20 32 30 30 38 2f 31 31 2f 30 37 20  1.66 2008/11/07 
a83c0 30 33 3a 32 39 3a 33 34 20 64 72 68 20 45 78 70  03:29:34 drh Exp
a83d0 20 24 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f   $.**.** Notes o
a83e0 6e 20 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20  n UTF-8:.**.**  
a83f0 20 42 79 74 65 2d 30 20 20 20 20 42 79 74 65 2d   Byte-0    Byte-
a8400 31 20 20 20 20 42 79 74 65 2d 32 20 20 20 20 42  1    Byte-2    B
a8410 79 74 65 2d 33 20 20 20 20 56 61 6c 75 65 0a 2a  yte-3    Value.*
a8420 2a 20 20 30 78 78 78 78 78 78 78 20 20 20 20 20  *  0xxxxxxx     
a8430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a8440 20 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30              0000
a8450 30 30 30 30 20 30 30 30 30 30 30 30 30 20 30 78  0000 00000000 0x
a8460 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 30 79 79  xxxxxx.**  110yy
a8470 79 79 79 20 20 31 30 78 78 78 78 78 78 20 20 20  yyy  10xxxxxx   
a8480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a8490 20 20 20 20 30 30 30 30 30 30 30 30 20 30 30 30      00000000 000
a84a0 30 30 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a  00yyy yyxxxxxx.*
a84b0 2a 20 20 31 31 31 30 7a 7a 7a 7a 20 20 31 30 79  *  1110zzzz  10y
a84c0 79 79 79 79 79 20 20 31 30 78 78 78 78 78 78 20  yyyyy  10xxxxxx 
a84d0 20 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30              0000
a84e0 30 30 30 30 20 7a 7a 7a 7a 79 79 79 79 20 79 79  0000 zzzzyyyy yy
a84f0 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 31 31 30  xxxxxx.**  11110
a8500 75 75 75 20 20 31 30 75 75 7a 7a 7a 7a 20 20 31  uuu  10uuzzzz  1
a8510 30 79 79 79 79 79 79 20 20 31 30 78 78 78 78 78  0yyyyyy  10xxxxx
a8520 78 20 20 20 30 30 30 75 75 75 75 75 20 7a 7a 7a  x   000uuuuu zzz
a8530 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a  zyyyy yyxxxxxx.*
a8540 2a 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f 6e  *.**.** Notes on
a8550 20 55 54 46 2d 31 36 3a 20 20 28 77 69 74 68 20   UTF-16:  (with 
a8560 77 77 77 77 2b 31 3d 3d 75 75 75 75 75 29 0a 2a  wwww+1==uuuuu).*
a8570 2a 0a 2a 2a 20 20 20 20 20 20 57 6f 72 64 2d 30  *.**      Word-0
a8580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 57                 W
a8590 6f 72 64 2d 31 20 20 20 20 20 20 20 20 20 20 56  ord-1          V
a85a0 61 6c 75 65 0a 2a 2a 20 20 31 31 30 31 31 30 77  alue.**  110110w
a85b0 77 20 77 77 7a 7a 7a 7a 79 79 20 20 20 31 31 30  w wwzzzzyy   110
a85c0 31 31 31 79 79 20 79 79 78 78 78 78 78 78 20 20  111yy yyxxxxxx  
a85d0 20 20 30 30 30 75 75 75 75 75 20 7a 7a 7a 7a 79    000uuuuu zzzzy
a85e0 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 20  yyy yyxxxxxx.** 
a85f0 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78   zzzzyyyy yyxxxx
a8600 78 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20  xx              
a8610 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30 30            000000
a8620 30 30 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78  00 zzzzyyyy yyxx
a8630 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 42 4f  xxxx.**.**.** BO
a8640 4d 20 6f 72 20 42 79 74 65 20 4f 72 64 65 72 20  M or Byte Order 
a8650 4d 61 72 6b 3a 0a 2a 2a 20 20 20 20 20 30 78 66  Mark:.**     0xf
a8660 66 20 30 78 66 65 20 20 20 6c 69 74 74 6c 65 2d  f 0xfe   little-
a8670 65 6e 64 69 61 6e 20 75 74 66 2d 31 36 20 66 6f  endian utf-16 fo
a8680 6c 6c 6f 77 73 0a 2a 2a 20 20 20 20 20 30 78 66  llows.**     0xf
a8690 65 20 30 78 66 66 20 20 20 62 69 67 2d 65 6e 64  e 0xff   big-end
a86a0 69 61 6e 20 75 74 66 2d 31 36 20 66 6f 6c 6c 6f  ian utf-16 follo
a86b0 77 73 0a 2a 2a 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  ws.**.*/./******
a86c0 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
a86d0 20 76 64 62 65 49 6e 74 2e 68 20 69 6e 20 74 68   vdbeInt.h in th
a86e0 65 20 6d 69 64 64 6c 65 20 6f 66 20 75 74 66 2e  e middle of utf.
a86f0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
a8700 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
a8710 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
a8720 69 6c 65 20 76 64 62 65 49 6e 74 2e 68 20 2a 2a  ile vdbeInt.h **
a8730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8750 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
a8760 30 30 33 20 53 65 70 74 65 6d 62 65 72 20 36 0a  003 September 6.
a8770 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
a8780 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
a8790 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
a87a0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
a87b0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
a87c0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
a87d0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
a87e0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
a87f0 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
a8800 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
a8810 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
a8820 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
a8830 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
a8840 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
a8850 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
a8860 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
a8870 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
a8880 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
a8890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a88a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a88b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a88c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
a88d0 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 68 65  * This is the he
a88e0 61 64 65 72 20 66 69 6c 65 20 66 6f 72 20 69 6e  ader file for in
a88f0 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 69  formation that i
a8900 73 20 70 72 69 76 61 74 65 20 74 6f 20 74 68 65  s private to the
a8910 0a 2a 2a 20 56 44 42 45 2e 20 20 54 68 69 73 20  .** VDBE.  This 
a8920 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64  information used
a8930 20 74 6f 20 61 6c 6c 20 62 65 20 61 74 20 74 68   to all be at th
a8940 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 69 6e  e top of the sin
a8950 67 6c 65 0a 2a 2a 20 73 6f 75 72 63 65 20 63 6f  gle.** source co
a8960 64 65 20 66 69 6c 65 20 22 76 64 62 65 2e 63 22  de file "vdbe.c"
a8970 2e 20 20 57 68 65 6e 20 74 68 61 74 20 66 69 6c  .  When that fil
a8980 65 20 62 65 63 61 6d 65 20 74 6f 6f 20 62 69 67  e became too big
a8990 20 28 6f 76 65 72 0a 2a 2a 20 36 30 30 30 20 6c   (over.** 6000 l
a89a0 69 6e 65 73 20 6c 6f 6e 67 29 20 69 74 20 77 61  ines long) it wa
a89b0 73 20 73 70 6c 69 74 20 75 70 20 69 6e 74 6f 20  s split up into 
a89c0 73 65 76 65 72 61 6c 20 73 6d 61 6c 6c 65 72 20  several smaller 
a89d0 66 69 6c 65 73 20 61 6e 64 0a 2a 2a 20 74 68 69  files and.** thi
a89e0 73 20 68 65 61 64 65 72 20 69 6e 66 6f 72 6d 61  s header informa
a89f0 74 69 6f 6e 20 77 61 73 20 66 61 63 74 6f 72 65  tion was factore
a8a00 64 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  d out..**.** $Id
a8a10 3a 20 76 64 62 65 49 6e 74 2e 68 2c 76 20 31 2e  : vdbeInt.h,v 1.
a8a20 31 35 37 20 32 30 30 38 2f 31 31 2f 30 35 20 31  157 2008/11/05 1
a8a30 36 3a 33 37 3a 33 35 20 64 72 68 20 45 78 70 20  6:37:35 drh Exp 
a8a40 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 56 44  $.*/.#ifndef _VD
a8a50 42 45 49 4e 54 5f 48 5f 0a 23 64 65 66 69 6e 65  BEINT_H_.#define
a8a60 20 5f 56 44 42 45 49 4e 54 5f 48 5f 0a 0a 2f 2a   _VDBEINT_H_../*
a8a70 0a 2a 2a 20 69 6e 74 54 6f 4b 65 79 28 29 20 61  .** intToKey() a
a8a80 6e 64 20 6b 65 79 54 6f 49 6e 74 28 29 20 75 73  nd keyToInt() us
a8a90 65 64 20 74 6f 20 74 72 61 6e 73 66 6f 72 6d 20  ed to transform 
a8aa0 74 68 65 20 72 6f 77 69 64 2e 20 20 42 75 74 20  the rowid.  But 
a8ab0 77 69 74 68 0a 2a 2a 20 74 68 65 20 6c 61 74 65  with.** the late
a8ac0 73 74 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74  st versions of t
a8ad0 68 65 20 64 65 73 69 67 6e 20 74 68 65 79 20 61  he design they a
a8ae0 72 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 64  re no-ops..*/.#d
a8af0 65 66 69 6e 65 20 6b 65 79 54 6f 49 6e 74 28 58  efine keyToInt(X
a8b00 29 20 20 20 28 58 29 0a 23 64 65 66 69 6e 65 20  )   (X).#define 
a8b10 69 6e 74 54 6f 4b 65 79 28 58 29 20 20 20 28 58  intToKey(X)   (X
a8b20 29 0a 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 20 69 73  ).../*.** SQL is
a8b30 20 74 72 61 6e 73 6c 61 74 65 64 20 69 6e 74 6f   translated into
a8b40 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20 69   a sequence of i
a8b50 6e 73 74 72 75 63 74 69 6f 6e 73 20 74 6f 20 62  nstructions to b
a8b60 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 62 79  e.** executed by
a8b70 20 61 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69   a virtual machi
a8b80 6e 65 2e 20 20 45 61 63 68 20 69 6e 73 74 72 75  ne.  Each instru
a8b90 63 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e 73 74  ction is an inst
a8ba0 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66  ance.** of the f
a8bb0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
a8bc0 72 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  re..*/.typedef s
a8bd0 74 72 75 63 74 20 56 64 62 65 4f 70 20 4f 70 3b  truct VdbeOp Op;
a8be0 0a 0a 2f 2a 0a 2a 2a 20 42 6f 6f 6c 65 61 6e 20  ../*.** Boolean 
a8bf0 76 61 6c 75 65 73 0a 2a 2f 0a 74 79 70 65 64 65  values.*/.typede
a8c00 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  f unsigned char 
a8c10 42 6f 6f 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 63  Bool;../*.** A c
a8c20 75 72 73 6f 72 20 69 73 20 61 20 70 6f 69 6e 74  ursor is a point
a8c30 65 72 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65  er into a single
a8c40 20 42 54 72 65 65 20 77 69 74 68 69 6e 20 61 20   BTree within a 
a8c50 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
a8c60 2a 20 54 68 65 20 63 75 72 73 6f 72 20 63 61 6e  * The cursor can
a8c70 20 73 65 65 6b 20 74 6f 20 61 20 42 54 72 65 65   seek to a BTree
a8c80 20 65 6e 74 72 79 20 77 69 74 68 20 61 20 70 61   entry with a pa
a8c90 72 74 69 63 75 6c 61 72 20 6b 65 79 2c 20 6f 72  rticular key, or
a8ca0 0a 2a 2a 20 6c 6f 6f 70 20 6f 76 65 72 20 61 6c  .** loop over al
a8cb0 6c 20 65 6e 74 72 69 65 73 20 6f 66 20 74 68 65  l entries of the
a8cc0 20 42 74 72 65 65 2e 20 20 59 6f 75 20 63 61 6e   Btree.  You can
a8cd0 20 61 6c 73 6f 20 69 6e 73 65 72 74 20 6e 65 77   also insert new
a8ce0 20 42 54 72 65 65 0a 2a 2a 20 65 6e 74 72 69 65   BTree.** entrie
a8cf0 73 20 6f 72 20 72 65 74 72 69 65 76 65 20 74 68  s or retrieve th
a8d00 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20 66 72  e key or data fr
a8d10 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 74 68 61  om the entry tha
a8d20 74 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20  t the cursor.** 
a8d30 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  is currently poi
a8d40 6e 74 69 6e 67 20 74 6f 2e 0a 2a 2a 20 0a 2a 2a  nting to..** .**
a8d50 20 45 76 65 72 79 20 63 75 72 73 6f 72 20 74 68   Every cursor th
a8d60 61 74 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d  at the virtual m
a8d70 61 63 68 69 6e 65 20 68 61 73 20 6f 70 65 6e 20  achine has open 
a8d80 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 62  is represented b
a8d90 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65  y an.** instance
a8da0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
a8db0 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  g structure..**.
a8dc0 2a 2a 20 49 66 20 74 68 65 20 56 64 62 65 43 75  ** If the VdbeCu
a8dd0 72 73 6f 72 2e 69 73 54 72 69 67 67 65 72 52 6f  rsor.isTriggerRo
a8de0 77 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 74  w flag is set it
a8df0 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 69 73   means that this
a8e00 20 63 75 72 73 6f 72 20 69 73 0a 2a 2a 20 72 65   cursor is.** re
a8e10 61 6c 6c 79 20 61 20 73 69 6e 67 6c 65 20 72 6f  ally a single ro
a8e20 77 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74  w that represent
a8e30 73 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c 44  s the NEW or OLD
a8e40 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 6f 66   pseudo-table of
a8e50 0a 2a 2a 20 61 20 72 6f 77 20 74 72 69 67 67 65  .** a row trigge
a8e60 72 2e 20 20 54 68 65 20 64 61 74 61 20 66 6f 72  r.  The data for
a8e70 20 74 68 65 20 72 6f 77 20 69 73 20 73 74 6f 72   the row is stor
a8e80 65 64 20 69 6e 20 56 64 62 65 43 75 72 73 6f 72  ed in VdbeCursor
a8e90 2e 70 44 61 74 61 20 61 6e 64 0a 2a 2a 20 74 68  .pData and.** th
a8ea0 65 20 72 6f 77 69 64 20 69 73 20 69 6e 20 56 64  e rowid is in Vd
a8eb0 62 65 43 75 72 73 6f 72 2e 69 4b 65 79 2e 0a 2a  beCursor.iKey..*
a8ec0 2f 0a 73 74 72 75 63 74 20 56 64 62 65 43 75 72  /.struct VdbeCur
a8ed0 73 6f 72 20 7b 0a 20 20 42 74 43 75 72 73 6f 72  sor {.  BtCursor
a8ee0 20 2a 70 43 75 72 73 6f 72 3b 20 20 20 20 2f 2a   *pCursor;    /*
a8ef0 20 54 68 65 20 63 75 72 73 6f 72 20 73 74 72 75   The cursor stru
a8f00 63 74 75 72 65 20 6f 66 20 74 68 65 20 62 61 63  cture of the bac
a8f10 6b 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44  kend */.  int iD
a8f20 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
a8f30 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 72 73  /* Index of curs
a8f40 6f 72 20 64 61 74 61 62 61 73 65 20 69 6e 20 64  or database in d
a8f50 62 2d 3e 61 44 62 5b 5d 20 28 6f 72 20 2d 31 29  b->aDb[] (or -1)
a8f60 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52 6f   */.  i64 lastRo
a8f70 77 69 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c  wid;        /* L
a8f80 61 73 74 20 72 6f 77 69 64 20 66 72 6f 6d 20 61  ast rowid from a
a8f90 20 4e 65 78 74 20 6f 72 20 4e 65 78 74 49 64 78   Next or NextIdx
a8fa0 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20   operation */.  
a8fb0 69 36 34 20 6e 65 78 74 52 6f 77 69 64 3b 20 20  i64 nextRowid;  
a8fc0 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 72 6f        /* Next ro
a8fd0 77 69 64 20 72 65 74 75 72 6e 65 64 20 62 79 20  wid returned by 
a8fe0 4f 50 5f 4e 65 77 52 6f 77 69 64 20 2a 2f 0a 20  OP_NewRowid */. 
a8ff0 20 42 6f 6f 6c 20 7a 65 72 6f 65 64 3b 20 20 20   Bool zeroed;   
a9000 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
a9010 66 20 7a 65 72 6f 65 64 20 6f 75 74 20 61 6e 64  f zeroed out and
a9020 20 72 65 61 64 79 20 66 6f 72 20 72 65 75 73 65   ready for reuse
a9030 20 2a 2f 0a 20 20 42 6f 6f 6c 20 72 6f 77 69 64   */.  Bool rowid
a9040 49 73 56 61 6c 69 64 3b 20 20 20 20 2f 2a 20 54  IsValid;    /* T
a9050 72 75 65 20 69 66 20 6c 61 73 74 52 6f 77 69 64  rue if lastRowid
a9060 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 42   is valid */.  B
a9070 6f 6f 6c 20 61 74 46 69 72 73 74 3b 20 20 20 20  ool atFirst;    
a9080 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
a9090 70 6f 69 6e 74 69 6e 67 20 74 6f 20 66 69 72 73  pointing to firs
a90a0 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 42 6f 6f  t entry */.  Boo
a90b0 6c 20 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64  l useRandomRowid
a90c0 3b 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 6e  ;  /* Generate n
a90d0 65 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  ew record number
a90e0 73 20 73 65 6d 69 2d 72 61 6e 64 6f 6d 6c 79 20  s semi-randomly 
a90f0 2a 2f 0a 20 20 42 6f 6f 6c 20 6e 75 6c 6c 52 6f  */.  Bool nullRo
a9100 77 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  w;         /* Tr
a9110 75 65 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 74  ue if pointing t
a9120 6f 20 61 20 72 6f 77 20 77 69 74 68 20 6e 6f 20  o a row with no 
a9130 64 61 74 61 20 2a 2f 0a 20 20 42 6f 6f 6c 20 6e  data */.  Bool n
a9140 65 78 74 52 6f 77 69 64 56 61 6c 69 64 3b 20 20  extRowidValid;  
a9150 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 6e  /* True if the n
a9160 65 78 74 52 6f 77 69 64 20 66 69 65 6c 64 20 69  extRowid field i
a9170 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 42 6f 6f  s valid */.  Boo
a9180 6c 20 70 73 65 75 64 6f 54 61 62 6c 65 3b 20 20  l pseudoTable;  
a9190 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20     /* This is a 
a91a0 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 65 75 64  NEW or OLD pseud
a91b0 6f 2d 74 61 62 6c 65 73 20 6f 66 20 61 20 74 72  o-tables of a tr
a91c0 69 67 67 65 72 20 2a 2f 0a 20 20 42 6f 6f 6c 20  igger */.  Bool 
a91d0 65 70 68 65 6d 50 73 65 75 64 6f 54 61 62 6c 65  ephemPseudoTable
a91e0 3b 0a 20 20 42 6f 6f 6c 20 64 65 66 65 72 72 65  ;.  Bool deferre
a91f0 64 4d 6f 76 65 74 6f 3b 20 20 2f 2a 20 41 20 63  dMoveto;  /* A c
a9200 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74  all to sqlite3Bt
a9210 72 65 65 4d 6f 76 65 74 6f 28 29 20 69 73 20 6e  reeMoveto() is n
a9220 65 65 64 65 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20  eeded */.  Bool 
a9230 69 73 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20  isTable;        
a9240 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20 74 61   /* True if a ta
a9250 62 6c 65 20 72 65 71 75 69 72 69 6e 67 20 69 6e  ble requiring in
a9260 74 65 67 65 72 20 6b 65 79 73 20 2a 2f 0a 20 20  teger keys */.  
a9270 42 6f 6f 6c 20 69 73 49 6e 64 65 78 3b 20 20 20  Bool isIndex;   
a9280 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
a9290 20 61 6e 20 69 6e 64 65 78 20 63 6f 6e 74 61 69   an index contai
a92a0 6e 69 6e 67 20 6b 65 79 73 20 6f 6e 6c 79 20 2d  ning keys only -
a92b0 20 6e 6f 20 64 61 74 61 20 2a 2f 0a 20 20 69 36   no data */.  i6
a92c0 34 20 6d 6f 76 65 74 6f 54 61 72 67 65 74 3b 20  4 movetoTarget; 
a92d0 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20      /* Argument 
a92e0 74 6f 20 74 68 65 20 64 65 66 65 72 72 65 64 20  to the deferred 
a92f0 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65  sqlite3BtreeMove
a9300 74 6f 28 29 20 2a 2f 0a 20 20 42 74 72 65 65 20  to() */.  Btree 
a9310 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20  *pBt;           
a9320 2f 2a 20 53 65 70 61 72 61 74 65 20 66 69 6c 65  /* Separate file
a9330 20 68 6f 6c 64 69 6e 67 20 74 65 6d 70 6f 72 61   holding tempora
a9340 72 79 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  ry table */.  in
a9350 74 20 6e 44 61 74 61 3b 20 20 20 20 20 20 20 20  t nData;        
a9360 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
a9370 20 62 79 74 65 73 20 69 6e 20 70 44 61 74 61 20   bytes in pData 
a9380 2a 2f 0a 20 20 63 68 61 72 20 2a 70 44 61 74 61  */.  char *pData
a9390 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61  ;          /* Da
a93a0 74 61 20 66 6f 72 20 61 20 4e 45 57 20 6f 72 20  ta for a NEW or 
a93b0 4f 4c 44 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  OLD pseudo-table
a93c0 20 2a 2f 0a 20 20 69 36 34 20 69 4b 65 79 3b 20   */.  i64 iKey; 
a93d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b              /* K
a93e0 65 79 20 66 6f 72 20 74 68 65 20 4e 45 57 20 6f  ey for the NEW o
a93f0 72 20 4f 4c 44 20 70 73 65 75 64 6f 2d 74 61 62  r OLD pseudo-tab
a9400 6c 65 20 72 6f 77 20 2a 2f 0a 20 20 4b 65 79 49  le row */.  KeyI
a9410 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 20  nfo *pKeyInfo;  
a9420 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f 75 74 20    /* Info about 
a9430 69 6e 64 65 78 20 6b 65 79 73 20 6e 65 65 64 65  index keys neede
a9440 64 20 62 79 20 69 6e 64 65 78 20 63 75 72 73 6f  d by index curso
a9450 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65  rs */.  int nFie
a9460 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ld;           /*
a9470 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64   Number of field
a9480 73 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 20  s in the header 
a9490 2a 2f 0a 20 20 69 36 34 20 73 65 71 43 6f 75 6e  */.  i64 seqCoun
a94a0 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65  t;         /* Se
a94b0 71 75 65 6e 63 65 20 63 6f 75 6e 74 65 72 20 2a  quence counter *
a94c0 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  /.  sqlite3_vtab
a94d0 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75  _cursor *pVtabCu
a94e0 72 73 6f 72 3b 20 20 2f 2a 20 54 68 65 20 63 75  rsor;  /* The cu
a94f0 72 73 6f 72 20 66 6f 72 20 61 20 76 69 72 74 75  rsor for a virtu
a9500 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 6f  al table */.  co
a9510 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
a9520 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 20 20  le *pModule;    
a9530 20 2f 2a 20 4d 6f 64 75 6c 65 20 66 6f 72 20 63   /* Module for c
a9540 75 72 73 6f 72 20 70 56 74 61 62 43 75 72 73 6f  ursor pVtabCurso
a9550 72 20 2a 2f 0a 0a 20 20 2f 2a 20 43 61 63 68 65  r */..  /* Cache
a9560 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  d information ab
a9570 6f 75 74 20 74 68 65 20 68 65 61 64 65 72 20 66  out the header f
a9580 6f 72 20 74 68 65 20 64 61 74 61 20 72 65 63 6f  or the data reco
a9590 72 64 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a  rd that the.  **
a95a0 20 63 75 72 73 6f 72 20 69 73 20 63 75 72 72 65   cursor is curre
a95b0 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f  ntly pointing to
a95c0 2e 20 20 4f 6e 6c 79 20 76 61 6c 69 64 20 69 66  .  Only valid if
a95d0 20 63 61 63 68 65 56 61 6c 69 64 20 69 73 20 74   cacheValid is t
a95e0 72 75 65 2e 0a 20 20 2a 2a 20 61 52 6f 77 20 6d  rue..  ** aRow m
a95f0 69 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 28 65  ight point to (e
a9600 70 68 65 6d 65 72 61 6c 29 20 64 61 74 61 20 66  phemeral) data f
a9610 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  or the current r
a9620 6f 77 2c 20 6f 72 20 69 74 20 6d 69 67 68 74 0a  ow, or it might.
a9630 20 20 2a 2a 20 62 65 20 4e 55 4c 4c 2e 0a 20 20    ** be NULL..  
a9640 2a 2f 0a 20 20 69 6e 74 20 63 61 63 68 65 53 74  */.  int cacheSt
a9650 61 74 75 73 3b 20 20 20 20 20 20 2f 2a 20 43 61  atus;      /* Ca
a9660 63 68 65 20 69 73 20 76 61 6c 69 64 20 69 66 20  che is valid if 
a9670 74 68 69 73 20 6d 61 74 63 68 65 73 20 56 64 62  this matches Vdb
a9680 65 2e 63 61 63 68 65 43 74 72 20 2a 2f 0a 20 20  e.cacheCtr */.  
a9690 69 6e 74 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b  int payloadSize;
a96a0 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e        /* Total n
a96b0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
a96c0 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a  n the record */.
a96d0 20 20 75 33 32 20 2a 61 54 79 70 65 3b 20 20 20    u32 *aType;   
a96e0 20 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20          /* Type 
a96f0 76 61 6c 75 65 73 20 66 6f 72 20 61 6c 6c 20 65  values for all e
a9700 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 72 65  ntries in the re
a9710 63 6f 72 64 20 2a 2f 0a 20 20 75 33 32 20 2a 61  cord */.  u32 *a
a9720 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20  Offset;         
a9730 2f 2a 20 43 61 63 68 65 64 20 6f 66 66 73 65 74  /* Cached offset
a9740 73 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f  s to the start o
a9750 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e 73 20 64  f each columns d
a9760 61 74 61 20 2a 2f 0a 20 20 75 38 20 2a 61 52 6f  ata */.  u8 *aRo
a9770 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  w;             /
a9780 2a 20 44 61 74 61 20 66 6f 72 20 74 68 65 20 63  * Data for the c
a9790 75 72 72 65 6e 74 20 72 6f 77 2c 20 69 66 20 61  urrent row, if a
a97a0 6c 6c 20 6f 6e 20 6f 6e 65 20 70 61 67 65 20 2a  ll on one page *
a97b0 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72  /.};.typedef str
a97c0 75 63 74 20 56 64 62 65 43 75 72 73 6f 72 20 56  uct VdbeCursor V
a97d0 64 62 65 43 75 72 73 6f 72 3b 0a 0a 2f 2a 0a 2a  dbeCursor;../*.*
a97e0 2a 20 41 20 76 61 6c 75 65 20 66 6f 72 20 56 64  * A value for Vd
a97f0 62 65 43 75 72 73 6f 72 2e 63 61 63 68 65 56 61  beCursor.cacheVa
a9800 6c 69 64 20 74 68 61 74 20 6d 65 61 6e 73 20 74  lid that means t
a9810 68 65 20 63 61 63 68 65 20 69 73 20 61 6c 77 61  he cache is alwa
a9820 79 73 20 69 6e 76 61 6c 69 64 2e 0a 2a 2f 0a 23  ys invalid..*/.#
a9830 64 65 66 69 6e 65 20 43 41 43 48 45 5f 53 54 41  define CACHE_STA
a9840 4c 45 20 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  LE 0../*.** Inte
a9850 72 6e 61 6c 6c 79 2c 20 74 68 65 20 76 64 62 65  rnally, the vdbe
a9860 20 6d 61 6e 69 70 75 6c 61 74 65 73 20 6e 65 61   manipulates nea
a9870 72 6c 79 20 61 6c 6c 20 53 51 4c 20 76 61 6c 75  rly all SQL valu
a9880 65 73 20 61 73 20 4d 65 6d 0a 2a 2a 20 73 74 72  es as Mem.** str
a9890 75 63 74 75 72 65 73 2e 20 45 61 63 68 20 4d 65  uctures. Each Me
a98a0 6d 20 73 74 72 75 63 74 20 6d 61 79 20 63 61 63  m struct may cac
a98b0 68 65 20 6d 75 6c 74 69 70 6c 65 20 72 65 70 72  he multiple repr
a98c0 65 73 65 6e 74 61 74 69 6f 6e 73 20 28 73 74 72  esentations (str
a98d0 69 6e 67 2c 0a 2a 2a 20 69 6e 74 65 67 65 72 20  ing,.** integer 
a98e0 65 74 63 2e 29 20 6f 66 20 74 68 65 20 73 61 6d  etc.) of the sam
a98f0 65 20 76 61 6c 75 65 2e 20 20 41 20 76 61 6c 75  e value.  A valu
a9900 65 20 28 61 6e 64 20 74 68 65 72 65 66 6f 72 65  e (and therefore
a9910 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 29 0a   Mem structure).
a9920 2a 2a 20 68 61 73 20 74 68 65 20 66 6f 6c 6c 6f  ** has the follo
a9930 77 69 6e 67 20 70 72 6f 70 65 72 74 69 65 73 3a  wing properties:
a9940 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 76 61 6c 75  .**.** Each valu
a9950 65 20 68 61 73 20 61 20 6d 61 6e 69 66 65 73 74  e has a manifest
a9960 20 74 79 70 65 2e 20 54 68 65 20 6d 61 6e 69 66   type. The manif
a9970 65 73 74 20 74 79 70 65 20 6f 66 20 74 68 65 20  est type of the 
a9980 76 61 6c 75 65 20 73 74 6f 72 65 64 0a 2a 2a 20  value stored.** 
a9990 69 6e 20 61 20 4d 65 6d 20 73 74 72 75 63 74 20  in a Mem struct 
a99a0 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  is returned by t
a99b0 68 65 20 4d 65 6d 54 79 70 65 28 4d 65 6d 2a 29  he MemType(Mem*)
a99c0 20 6d 61 63 72 6f 2e 20 54 68 65 20 74 79 70 65   macro. The type
a99d0 20 69 73 0a 2a 2a 20 6f 6e 65 20 6f 66 20 53 51   is.** one of SQ
a99e0 4c 49 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54  LITE_NULL, SQLIT
a99f0 45 5f 49 4e 54 45 47 45 52 2c 20 53 51 4c 49 54  E_INTEGER, SQLIT
a9a00 45 5f 52 45 41 4c 2c 20 53 51 4c 49 54 45 5f 54  E_REAL, SQLITE_T
a9a10 45 58 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45  EXT or.** SQLITE
a9a20 5f 42 4c 4f 42 2e 0a 2a 2f 0a 73 74 72 75 63 74  _BLOB..*/.struct
a9a30 20 4d 65 6d 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b   Mem {.  union {
a9a40 0a 20 20 20 20 69 36 34 20 69 3b 20 20 20 20 20  .    i64 i;     
a9a50 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65           /* Inte
a9a60 67 65 72 20 76 61 6c 75 65 2e 20 4f 72 20 46 75  ger value. Or Fu
a9a70 6e 63 44 65 66 2a 20 77 68 65 6e 20 66 6c 61 67  ncDef* when flag
a9a80 73 3d 3d 4d 45 4d 5f 41 67 67 20 2a 2f 0a 20 20  s==MEM_Agg */.  
a9a90 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b    FuncDef *pDef;
a9aa0 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 6f 6e        /* Used on
a9ab0 6c 79 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d  ly when flags==M
a9ac0 45 4d 5f 41 67 67 20 2a 2f 0a 20 20 7d 20 75 3b  EM_Agg */.  } u;
a9ad0 0a 20 20 64 6f 75 62 6c 65 20 72 3b 20 20 20 20  .  double r;    
a9ae0 20 20 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76         /* Real v
a9af0 61 6c 75 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  alue */.  sqlite
a9b00 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 2f 2a  3 *db;        /*
a9b10 20 54 68 65 20 61 73 73 6f 63 69 61 74 65 64 20   The associated 
a9b20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
a9b30 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ion */.  char *z
a9b40 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
a9b50 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 76  String or BLOB v
a9b60 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b  alue */.  int n;
a9b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a9b80 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61   Number of chara
a9b90 63 74 65 72 73 20 69 6e 20 73 74 72 69 6e 67 20  cters in string 
a9ba0 76 61 6c 75 65 2c 20 65 78 63 6c 75 64 69 6e 67  value, excluding
a9bb0 20 27 5c 30 27 20 2a 2f 0a 20 20 75 31 36 20 66   '\0' */.  u16 f
a9bc0 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 2f  lags;          /
a9bd0 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61 74 69  * Some combinati
a9be0 6f 6e 20 6f 66 20 4d 45 4d 5f 4e 75 6c 6c 2c 20  on of MEM_Null, 
a9bf0 4d 45 4d 5f 53 74 72 2c 20 4d 45 4d 5f 44 79 6e  MEM_Str, MEM_Dyn
a9c00 2c 20 65 74 63 2e 20 2a 2f 0a 20 20 75 38 20 20  , etc. */.  u8  
a9c10 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20  type;           
a9c20 2f 2a 20 4f 6e 65 20 6f 66 20 53 51 4c 49 54 45  /* One of SQLITE
a9c30 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 5f 54 45  _NULL, SQLITE_TE
a9c40 58 54 2c 20 53 51 4c 49 54 45 5f 49 4e 54 45 47  XT, SQLITE_INTEG
a9c50 45 52 2c 20 65 74 63 20 2a 2f 0a 20 20 75 38 20  ER, etc */.  u8 
a9c60 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20   enc;           
a9c70 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   /* SQLITE_UTF8,
a9c80 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c   SQLITE_UTF16BE,
a9c90 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20   SQLITE_UTF16LE 
a9ca0 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c  */.  void (*xDel
a9cb0 29 28 76 6f 69 64 20 2a 29 3b 20 20 2f 2a 20 49  )(void *);  /* I
a9cc0 66 20 6e 6f 74 20 6e 75 6c 6c 2c 20 63 61 6c 6c  f not null, call
a9cd0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74   this function t
a9ce0 6f 20 64 65 6c 65 74 65 20 4d 65 6d 2e 7a 20 2a  o delete Mem.z *
a9cf0 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f  /.  char *zMallo
a9d00 63 3b 20 20 20 20 20 20 2f 2a 20 44 79 6e 61 6d  c;      /* Dynam
a9d10 69 63 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61  ic buffer alloca
a9d20 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6d  ted by sqlite3_m
a9d30 61 6c 6c 6f 63 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f  alloc() */.};../
a9d40 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66  * One or more of
a9d50 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   the following f
a9d60 6c 61 67 73 20 61 72 65 20 73 65 74 20 74 6f 20  lags are set to 
a9d70 69 6e 64 69 63 61 74 65 20 74 68 65 20 76 61 6c  indicate the val
a9d80 69 64 4f 4b 0a 2a 2a 20 72 65 70 72 65 73 65 6e  idOK.** represen
a9d90 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 76  tations of the v
a9da0 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 74  alue stored in t
a9db0 68 65 20 4d 65 6d 20 73 74 72 75 63 74 2e 0a 2a  he Mem struct..*
a9dc0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45 4d 5f  *.** If the MEM_
a9dd0 4e 75 6c 6c 20 66 6c 61 67 20 69 73 20 73 65 74  Null flag is set
a9de0 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65  , then the value
a9df0 20 69 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20   is an SQL NULL 
a9e00 76 61 6c 75 65 2e 0a 2a 2a 20 4e 6f 20 6f 74 68  value..** No oth
a9e10 65 72 20 66 6c 61 67 73 20 6d 61 79 20 62 65 20  er flags may be 
a9e20 73 65 74 20 69 6e 20 74 68 69 73 20 63 61 73 65  set in this case
a9e30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d  ..**.** If the M
a9e40 45 4d 5f 53 74 72 20 66 6c 61 67 20 69 73 20 73  EM_Str flag is s
a9e50 65 74 20 74 68 65 6e 20 4d 65 6d 2e 7a 20 70 6f  et then Mem.z po
a9e60 69 6e 74 73 20 61 74 20 61 20 73 74 72 69 6e 67  ints at a string
a9e70 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e   representation.
a9e80 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 74 68 69 73  .** Usually this
a9e90 20 69 73 20 65 6e 63 6f 64 65 64 20 69 6e 20 74   is encoded in t
a9ea0 68 65 20 73 61 6d 65 20 75 6e 69 63 6f 64 65 20  he same unicode 
a9eb0 65 6e 63 6f 64 69 6e 67 20 61 73 20 74 68 65 20  encoding as the 
a9ec0 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62 61 73 65  main.** database
a9ed0 20 28 73 65 65 20 62 65 6c 6f 77 20 66 6f 72 20   (see below for 
a9ee0 65 78 63 65 70 74 69 6f 6e 73 29 2e 20 49 66 20  exceptions). If 
a9ef0 74 68 65 20 4d 45 4d 5f 54 65 72 6d 20 66 6c 61  the MEM_Term fla
a9f00 67 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 73 65 74  g is also.** set
a9f10 2c 20 74 68 65 6e 20 74 68 65 20 73 74 72 69 6e  , then the strin
a9f20 67 20 69 73 20 6e 75 6c 20 74 65 72 6d 69 6e 61  g is nul termina
a9f30 74 65 64 2e 20 54 68 65 20 4d 45 4d 5f 49 6e 74  ted. The MEM_Int
a9f40 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 0a 2a   and MEM_Real .*
a9f50 2a 20 66 6c 61 67 73 20 6d 61 79 20 63 6f 65 78  * flags may coex
a9f60 69 73 74 20 77 69 74 68 20 74 68 65 20 4d 45 4d  ist with the MEM
a9f70 5f 53 74 72 20 66 6c 61 67 2e 0a 2a 2a 0a 2a 2a  _Str flag..**.**
a9f80 20 4d 75 6c 74 69 70 6c 65 20 6f 66 20 74 68 65   Multiple of the
a9f90 73 65 20 76 61 6c 75 65 73 20 63 61 6e 20 61 70  se values can ap
a9fa0 70 65 61 72 20 69 6e 20 4d 65 6d 2e 66 6c 61 67  pear in Mem.flag
a9fb0 73 2e 20 20 42 75 74 20 6f 6e 6c 79 20 6f 6e 65  s.  But only one
a9fc0 0a 2a 2a 20 61 74 20 61 20 74 69 6d 65 20 63 61  .** at a time ca
a9fd0 6e 20 61 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e  n appear in Mem.
a9fe0 74 79 70 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  type..*/.#define
a9ff0 20 4d 45 4d 5f 4e 75 6c 6c 20 20 20 20 20 20 30   MEM_Null      0
aa000 78 30 30 30 31 20 20 20 2f 2a 20 56 61 6c 75 65  x0001   /* Value
aa010 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66   is NULL */.#def
aa020 69 6e 65 20 4d 45 4d 5f 53 74 72 20 20 20 20 20  ine MEM_Str     
aa030 20 20 30 78 30 30 30 32 20 20 20 2f 2a 20 56 61    0x0002   /* Va
aa040 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e 67 20  lue is a string 
aa050 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 49  */.#define MEM_I
aa060 6e 74 20 20 20 20 20 20 20 30 78 30 30 30 34 20  nt       0x0004 
aa070 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 61 6e    /* Value is an
aa080 20 69 6e 74 65 67 65 72 20 2a 2f 0a 23 64 65 66   integer */.#def
aa090 69 6e 65 20 4d 45 4d 5f 52 65 61 6c 20 20 20 20  ine MEM_Real    
aa0a0 20 20 30 78 30 30 30 38 20 20 20 2f 2a 20 56 61    0x0008   /* Va
aa0b0 6c 75 65 20 69 73 20 61 20 72 65 61 6c 20 6e 75  lue is a real nu
aa0c0 6d 62 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20  mber */.#define 
aa0d0 4d 45 4d 5f 42 6c 6f 62 20 20 20 20 20 20 30 78  MEM_Blob      0x
aa0e0 30 30 31 30 20 20 20 2f 2a 20 56 61 6c 75 65 20  0010   /* Value 
aa0f0 69 73 20 61 20 42 4c 4f 42 20 2a 2f 0a 0a 23 64  is a BLOB */..#d
aa100 65 66 69 6e 65 20 4d 65 6d 53 65 74 54 79 70 65  efine MemSetType
aa110 46 6c 61 67 28 70 2c 20 66 29 20 5c 0a 20 20 28  Flag(p, f) \.  (
aa120 28 70 29 2d 3e 66 6c 61 67 73 20 3d 20 28 28 70  (p)->flags = ((p
aa130 29 2d 3e 66 6c 61 67 73 26 7e 28 4d 45 4d 5f 49  )->flags&~(MEM_I
aa140 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f  nt|MEM_Real|MEM_
aa150 4e 75 6c 6c 7c 4d 45 4d 5f 42 6c 6f 62 7c 4d 45  Null|MEM_Blob|ME
aa160 4d 5f 53 74 72 29 29 7c 66 29 0a 0a 2f 2a 20 57  M_Str))|f)../* W
aa170 68 65 6e 65 76 65 72 20 4d 65 6d 20 63 6f 6e 74  henever Mem cont
aa180 61 69 6e 73 20 61 20 76 61 6c 69 64 20 73 74 72  ains a valid str
aa190 69 6e 67 20 6f 72 20 62 6c 6f 62 20 72 65 70 72  ing or blob repr
aa1a0 65 73 65 6e 74 61 74 69 6f 6e 2c 20 6f 6e 65 20  esentation, one 
aa1b0 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77  of.** the follow
aa1c0 69 6e 67 20 66 6c 61 67 73 20 6d 75 73 74 20 62  ing flags must b
aa1d0 65 20 73 65 74 20 74 6f 20 64 65 74 65 72 6d 69  e set to determi
aa1e0 6e 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 6d 61  ne the memory ma
aa1f0 6e 61 67 65 6d 65 6e 74 0a 2a 2a 20 70 6f 6c 69  nagement.** poli
aa200 63 79 20 66 6f 72 20 4d 65 6d 2e 7a 2e 20 20 54  cy for Mem.z.  T
aa210 68 65 20 4d 45 4d 5f 54 65 72 6d 20 66 6c 61 67  he MEM_Term flag
aa220 20 74 65 6c 6c 73 20 75 73 20 77 68 65 74 68 65   tells us whethe
aa230 72 20 6f 72 20 6e 6f 74 20 74 68 65 0a 2a 2a 20  r or not the.** 
aa240 73 74 72 69 6e 67 20 69 73 20 5c 30 30 30 20 6f  string is \000 o
aa250 72 20 5c 75 30 30 30 30 20 74 65 72 6d 69 6e 61  r \u0000 termina
aa260 74 65 64 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  ted.*/.#define M
aa270 45 4d 5f 54 65 72 6d 20 20 20 20 20 20 30 78 30  EM_Term      0x0
aa280 30 32 30 20 20 20 2f 2a 20 53 74 72 69 6e 67 20  020   /* String 
aa290 72 65 70 20 69 73 20 6e 75 6c 20 74 65 72 6d 69  rep is nul termi
aa2a0 6e 61 74 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  nated */.#define
aa2b0 20 4d 45 4d 5f 44 79 6e 20 20 20 20 20 20 20 30   MEM_Dyn       0
aa2c0 78 30 30 34 30 20 20 20 2f 2a 20 4e 65 65 64 20  x0040   /* Need 
aa2d0 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 46 72  to call sqliteFr
aa2e0 65 65 28 29 20 6f 6e 20 4d 65 6d 2e 7a 20 2a 2f  ee() on Mem.z */
aa2f0 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 53 74 61  .#define MEM_Sta
aa300 74 69 63 20 20 20 20 30 78 30 30 38 30 20 20 20  tic    0x0080   
aa310 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20  /* Mem.z points 
aa320 74 6f 20 61 20 73 74 61 74 69 63 20 73 74 72 69  to a static stri
aa330 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45  ng */.#define ME
aa340 4d 5f 45 70 68 65 6d 20 20 20 20 20 30 78 30 31  M_Ephem     0x01
aa350 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f  00   /* Mem.z po
aa360 69 6e 74 73 20 74 6f 20 61 6e 20 65 70 68 65 6d  ints to an ephem
aa370 65 72 61 6c 20 73 74 72 69 6e 67 20 2a 2f 0a 23  eral string */.#
aa380 64 65 66 69 6e 65 20 4d 45 4d 5f 41 67 67 20 20  define MEM_Agg  
aa390 20 20 20 20 20 30 78 30 34 30 30 20 20 20 2f 2a       0x0400   /*
aa3a0 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 6f   Mem.z points to
aa3b0 20 61 6e 20 61 67 67 20 66 75 6e 63 74 69 6f 6e   an agg function
aa3c0 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 23 64 65 66   context */.#def
aa3d0 69 6e 65 20 4d 45 4d 5f 5a 65 72 6f 20 20 20 20  ine MEM_Zero    
aa3e0 20 20 30 78 30 38 30 30 20 20 20 2f 2a 20 4d 65    0x0800   /* Me
aa3f0 6d 2e 69 20 63 6f 6e 74 61 69 6e 73 20 63 6f 75  m.i contains cou
aa400 6e 74 20 6f 66 20 30 73 20 61 70 70 65 6e 64 65  nt of 0s appende
aa410 64 20 74 6f 20 62 6c 6f 62 20 2a 2f 0a 0a 23 69  d to blob */..#i
aa420 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
aa430 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 23 75 6e 64  _INCRBLOB.  #und
aa440 65 66 20 4d 45 4d 5f 5a 65 72 6f 0a 20 20 23 64  ef MEM_Zero.  #d
aa450 65 66 69 6e 65 20 4d 45 4d 5f 5a 65 72 6f 20 30  efine MEM_Zero 0
aa460 78 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f  x0000.#endif.../
aa470 2a 20 41 20 56 64 62 65 46 75 6e 63 20 69 73 20  * A VdbeFunc is 
aa480 6a 75 73 74 20 61 20 46 75 6e 63 44 65 66 20 28  just a FuncDef (
aa490 64 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74  defined in sqlit
aa4a0 65 49 6e 74 2e 68 29 20 74 68 61 74 20 63 6f 6e  eInt.h) that con
aa4b0 74 61 69 6e 73 0a 2a 2a 20 61 64 64 69 74 69 6f  tains.** additio
aa4c0 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  nal information 
aa4d0 61 62 6f 75 74 20 61 75 78 69 6c 69 61 72 79 20  about auxiliary 
aa4e0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 62 6f 75 6e  information boun
aa4f0 64 20 74 6f 20 61 72 67 75 6d 65 6e 74 73 0a 2a  d to arguments.*
aa500 2a 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f  * of the functio
aa510 6e 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64  n.  This is used
aa520 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
aa530 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75  e sqlite3_get_au
aa540 78 64 61 74 61 28 29 0a 2a 2a 20 61 6e 64 20 73  xdata().** and s
aa550 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61  qlite3_set_auxda
aa560 74 61 28 29 20 41 50 49 73 2e 20 20 54 68 65 20  ta() APIs.  The 
aa570 22 61 75 78 64 61 74 61 22 20 69 73 20 73 6f 6d  "auxdata" is som
aa580 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61  e auxiliary data
aa590 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20  .** that can be 
aa5a0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
aa5b0 61 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d  a constant argum
aa5c0 65 6e 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f  ent to a functio
aa5d0 6e 2e 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c 6f  n.  This.** allo
aa5e0 77 73 20 66 75 6e 63 74 69 6f 6e 73 20 73 75 63  ws functions suc
aa5f0 68 20 61 73 20 22 72 65 67 65 78 70 22 20 74 6f  h as "regexp" to
aa600 20 63 6f 6d 70 69 6c 65 20 74 68 65 69 72 20 63   compile their c
aa610 6f 6e 73 74 61 6e 74 20 72 65 67 75 6c 61 72 0a  onstant regular.
aa620 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 61 72  ** expression ar
aa630 67 75 6d 65 6e 74 20 6f 6e 63 65 20 61 6e 64 20  gument once and 
aa640 72 65 75 73 65 64 20 74 68 65 20 63 6f 6d 70 69  reused the compi
aa650 6c 65 64 20 63 6f 64 65 20 66 6f 72 20 6d 75 6c  led code for mul
aa660 74 69 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61 74  tiple.** invocat
aa670 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ions..*/.struct 
aa680 56 64 62 65 46 75 6e 63 20 7b 0a 20 20 46 75 6e  VdbeFunc {.  Fun
aa690 63 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20  cDef *pFunc;    
aa6a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
aa6b0 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20  e definition of 
aa6c0 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  the function */.
aa6d0 20 20 69 6e 74 20 6e 41 75 78 3b 20 20 20 20 20    int nAux;     
aa6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aa6f0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
aa700 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66  ries allocated f
aa710 6f 72 20 61 70 41 75 78 5b 5d 20 2a 2f 0a 20 20  or apAux[] */.  
aa720 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20 7b  struct AuxData {
aa730 0a 20 20 20 20 76 6f 69 64 20 2a 70 41 75 78 3b  .    void *pAux;
aa740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aa750 20 20 20 2f 2a 20 41 75 78 20 64 61 74 61 20 66     /* Aux data f
aa760 6f 72 20 74 68 65 20 69 2d 74 68 20 61 72 67 75  or the i-th argu
aa770 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 76 6f 69 64  ment */.    void
aa780 20 28 2a 78 44 65 6c 65 74 65 29 28 76 6f 69 64   (*xDelete)(void
aa790 20 2a 29 3b 20 20 20 20 20 20 2f 2a 20 44 65 73   *);      /* Des
aa7a0 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20  tructor for the 
aa7b0 61 75 78 20 64 61 74 61 20 2a 2f 0a 20 20 7d 20  aux data */.  } 
aa7c0 61 70 41 75 78 5b 31 5d 3b 20 20 20 20 20 20 20  apAux[1];       
aa7d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
aa7e0 6e 65 20 73 6c 6f 74 20 66 6f 72 20 65 61 63 68  ne slot for each
aa7f0 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65   function argume
aa800 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  nt */.};../*.** 
aa810 54 68 65 20 22 63 6f 6e 74 65 78 74 22 20 61 72  The "context" ar
aa820 67 75 6d 65 6e 74 20 66 6f 72 20 61 20 69 6e 73  gument for a ins
aa830 74 61 6c 6c 61 62 6c 65 20 66 75 6e 63 74 69 6f  tallable functio
aa840 6e 2e 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  n.  A pointer to
aa850 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20   an.** instance 
aa860 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
aa870 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20 61  e is the first a
aa880 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 72  rgument to the r
aa890 6f 75 74 69 6e 65 73 20 75 73 65 64 0a 2a 2a 20  outines used.** 
aa8a0 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51  implement the SQ
aa8b0 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a  L functions..**.
aa8c0 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 74 79  ** There is a ty
aa8d0 70 65 64 65 66 20 66 6f 72 20 74 68 69 73 20 73  pedef for this s
aa8e0 74 72 75 63 74 75 72 65 20 69 6e 20 73 71 6c 69  tructure in sqli
aa8f0 74 65 2e 68 2e 20 20 53 6f 20 61 6c 6c 20 72 6f  te.h.  So all ro
aa900 75 74 69 6e 65 73 2c 0a 2a 2a 20 65 76 65 6e 20  utines,.** even 
aa910 74 68 65 20 70 75 62 6c 69 63 20 69 6e 74 65 72  the public inter
aa920 66 61 63 65 20 74 6f 20 53 51 4c 69 74 65 2c 20  face to SQLite, 
aa930 63 61 6e 20 75 73 65 20 61 20 70 6f 69 6e 74 65  can use a pointe
aa940 72 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74  r to this struct
aa950 75 72 65 2e 0a 2a 2a 20 42 75 74 20 74 68 69 73  ure..** But this
aa960 20 66 69 6c 65 20 69 73 20 74 68 65 20 6f 6e 6c   file is the onl
aa970 79 20 70 6c 61 63 65 20 77 68 65 72 65 20 74 68  y place where th
aa980 65 20 69 6e 74 65 72 6e 61 6c 20 64 65 74 61 69  e internal detai
aa990 6c 73 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73 74  ls of this.** st
aa9a0 72 75 63 74 75 72 65 20 61 72 65 20 6b 6e 6f 77  ructure are know
aa9b0 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74  n..**.** This st
aa9c0 72 75 63 74 75 72 65 20 69 73 20 64 65 66 69 6e  ructure is defin
aa9d0 65 64 20 69 6e 73 69 64 65 20 6f 66 20 76 64 62  ed inside of vdb
aa9e0 65 49 6e 74 2e 68 20 62 65 63 61 75 73 65 20 69  eInt.h because i
aa9f0 74 20 75 73 65 73 20 73 75 62 73 74 72 75 63 74  t uses substruct
aaa00 75 72 65 73 0a 2a 2a 20 28 4d 65 6d 29 20 77 68  ures.** (Mem) wh
aaa10 69 63 68 20 61 72 65 20 6f 6e 6c 79 20 64 65 66  ich are only def
aaa20 69 6e 65 64 20 74 68 65 72 65 2e 0a 2a 2f 0a 73  ined there..*/.s
aaa30 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 63 6f  truct sqlite3_co
aaa40 6e 74 65 78 74 20 7b 0a 20 20 46 75 6e 63 44 65  ntext {.  FuncDe
aaa50 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20  f *pFunc;       
aaa60 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 75  /* Pointer to fu
aaa70 6e 63 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69  nction informati
aaa80 6f 6e 2e 20 20 4d 55 53 54 20 42 45 20 46 49 52  on.  MUST BE FIR
aaa90 53 54 20 2a 2f 0a 20 20 56 64 62 65 46 75 6e 63  ST */.  VdbeFunc
aaaa0 20 2a 70 56 64 62 65 46 75 6e 63 3b 20 20 2f 2a   *pVdbeFunc;  /*
aaab0 20 41 75 78 69 6c 61 72 79 20 64 61 74 61 2c 20   Auxilary data, 
aaac0 69 66 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a 20  if created. */. 
aaad0 20 4d 65 6d 20 73 3b 20 20 20 20 20 20 20 20 20   Mem s;         
aaae0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65         /* The re
aaaf0 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 73 74  turn value is st
aab00 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 4d  ored here */.  M
aab10 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20  em *pMem;       
aab20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63       /* Memory c
aab30 65 6c 6c 20 75 73 65 64 20 74 6f 20 73 74 6f 72  ell used to stor
aab40 65 20 61 67 67 72 65 67 61 74 65 20 63 6f 6e 74  e aggregate cont
aab50 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45  ext */.  int isE
aab60 72 72 6f 72 3b 20 20 20 20 20 20 20 20 20 20 2f  rror;          /
aab70 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 72 65 74  * Error code ret
aab80 75 72 6e 65 64 20 62 79 20 74 68 65 20 66 75 6e  urned by the fun
aab90 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 43 6f 6c 6c  ction. */.  Coll
aaba0 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20  Seq *pColl;     
aabb0 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73    /* Collating s
aabc0 65 71 75 65 6e 63 65 20 2a 2f 0a 7d 3b 0a 0a 2f  equence */.};../
aabd0 2a 0a 2a 2a 20 41 20 53 65 74 20 73 74 72 75 63  *.** A Set struc
aabe0 74 75 72 65 20 69 73 20 75 73 65 64 20 66 6f 72  ture is used for
aabf0 20 71 75 69 63 6b 20 74 65 73 74 69 6e 67 20 74   quick testing t
aac00 6f 20 73 65 65 20 69 66 20 61 20 76 61 6c 75 65  o see if a value
aac10 0a 2a 2a 20 69 73 20 70 61 72 74 20 6f 66 20 61  .** is part of a
aac20 20 73 6d 61 6c 6c 20 73 65 74 2e 20 20 53 65 74   small set.  Set
aac30 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d  s are used to im
aac40 70 6c 65 6d 65 6e 74 20 63 6f 64 65 20 6c 69 6b  plement code lik
aac50 65 0a 2a 2a 20 74 68 69 73 3a 0a 2a 2a 20 20 20  e.** this:.**   
aac60 20 20 20 20 20 20 20 20 20 78 2e 79 20 49 4e 20           x.y IN 
aac70 28 27 68 69 27 2c 27 68 6f 6f 27 2c 27 68 75 6d  ('hi','hoo','hum
aac80 27 29 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  ').*/.typedef st
aac90 72 75 63 74 20 53 65 74 20 53 65 74 3b 0a 73 74  ruct Set Set;.st
aaca0 72 75 63 74 20 53 65 74 20 7b 0a 20 20 48 61 73  ruct Set {.  Has
aacb0 68 20 68 61 73 68 3b 20 20 20 20 20 20 20 20 20  h hash;         
aacc0 20 20 20 20 2f 2a 20 41 20 73 65 74 20 69 73 20      /* A set is 
aacd0 6a 75 73 74 20 61 20 68 61 73 68 20 74 61 62 6c  just a hash tabl
aace0 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20  e */.  HashElem 
aacf0 2a 70 72 65 76 3b 20 20 20 20 20 20 20 20 2f 2a  *prev;        /*
aad00 20 50 72 65 76 69 6f 75 73 6c 79 20 61 63 63 65   Previously acce
aad10 73 73 65 64 20 68 61 73 68 20 65 6c 65 6d 65 6e  ssed hash elemen
aad20 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20   */.};../*.** A 
aad30 46 69 66 6f 50 61 67 65 20 73 74 72 75 63 74 75  FifoPage structu
aad40 72 65 20 68 6f 6c 64 73 20 61 20 73 69 6e 67 6c  re holds a singl
aad50 65 20 70 61 67 65 20 6f 66 20 76 61 6c 76 65 73  e page of valves
aad60 2e 20 20 50 61 67 65 73 20 61 72 65 20 61 72 72  .  Pages are arr
aad70 61 6e 67 65 64 0a 2a 2a 20 69 6e 20 61 20 6c 69  anged.** in a li
aad80 73 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  st..*/.typedef s
aad90 74 72 75 63 74 20 46 69 66 6f 50 61 67 65 20 46  truct FifoPage F
aada0 69 66 6f 50 61 67 65 3b 0a 73 74 72 75 63 74 20  ifoPage;.struct 
aadb0 46 69 66 6f 50 61 67 65 20 7b 0a 20 20 69 6e 74  FifoPage {.  int
aadc0 20 6e 53 6c 6f 74 3b 20 20 20 20 20 20 20 20 20   nSlot;         
aadd0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
aade0 72 69 65 73 20 61 53 6c 6f 74 5b 5d 20 2a 2f 0a  ries aSlot[] */.
aadf0 20 20 69 6e 74 20 69 57 72 69 74 65 3b 20 20 20    int iWrite;   
aae00 20 20 20 20 20 2f 2a 20 50 75 73 68 20 74 68 65       /* Push the
aae10 20 6e 65 78 74 20 76 61 6c 75 65 20 69 6e 74 6f   next value into
aae20 20 74 68 69 73 20 65 6e 74 72 79 20 69 6e 20 61   this entry in a
aae30 53 6c 6f 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  Slot[] */.  int 
aae40 69 52 65 61 64 3b 20 20 20 20 20 20 20 20 20 2f  iRead;         /
aae50 2a 20 52 65 61 64 20 74 68 65 20 6e 65 78 74 20  * Read the next 
aae60 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 69 73 20  value from this 
aae70 65 6e 74 72 79 20 69 6e 20 61 53 6c 6f 74 5b 5d  entry in aSlot[]
aae80 20 2a 2f 0a 20 20 46 69 66 6f 50 61 67 65 20 2a   */.  FifoPage *
aae90 70 4e 65 78 74 3b 20 20 20 2f 2a 20 4e 65 78 74  pNext;   /* Next
aaea0 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 69 66   page in the fif
aaeb0 6f 20 2a 2f 0a 20 20 69 36 34 20 61 53 6c 6f 74  o */.  i64 aSlot
aaec0 5b 31 5d 3b 20 20 20 20 20 20 2f 2a 20 4f 6e 65  [1];      /* One
aaed0 20 6f 72 20 6d 6f 72 65 20 73 6c 6f 74 73 20 66   or more slots f
aaee0 6f 72 20 72 6f 77 69 64 20 76 61 6c 75 65 73 20  or rowid values 
aaef0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  */.};../*.** The
aaf00 20 46 69 66 6f 20 73 74 72 75 63 74 75 72 65 20   Fifo structure 
aaf10 69 73 20 74 79 70 65 64 65 66 2d 65 64 20 69 6e  is typedef-ed in
aaf20 20 76 64 62 65 49 6e 74 2e 68 2e 20 20 42 75 74   vdbeInt.h.  But
aaf30 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
aaf40 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 61 74 20 73  ion.** of that s
aaf50 74 72 75 63 74 75 72 65 20 69 73 20 70 72 69 76  tructure is priv
aaf60 61 74 65 20 74 6f 20 74 68 69 73 20 66 69 6c 65  ate to this file
aaf70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 46 69 66 6f  ..**.** The Fifo
aaf80 20 73 74 72 75 63 74 75 72 65 20 64 65 73 63 72   structure descr
aaf90 69 62 65 73 20 74 68 65 20 65 6e 74 69 72 65 20  ibes the entire 
aafa0 66 69 66 6f 2e 20 20 0a 2a 2f 0a 74 79 70 65 64  fifo.  .*/.typed
aafb0 65 66 20 73 74 72 75 63 74 20 46 69 66 6f 20 46  ef struct Fifo F
aafc0 69 66 6f 3b 0a 73 74 72 75 63 74 20 46 69 66 6f  ifo;.struct Fifo
aafd0 20 7b 0a 20 20 69 6e 74 20 6e 45 6e 74 72 79 3b   {.  int nEntry;
aafe0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61           /* Tota
aaff0 6c 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  l number of entr
ab000 69 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ies */.  sqlite3
ab010 20 2a 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20   *db;        /* 
ab020 54 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 64  The associated d
ab030 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
ab040 6f 6e 20 2a 2f 0a 20 20 46 69 66 6f 50 61 67 65  on */.  FifoPage
ab050 20 2a 70 46 69 72 73 74 3b 20 20 20 2f 2a 20 46   *pFirst;   /* F
ab060 69 72 73 74 20 70 61 67 65 20 6f 6e 20 74 68 65  irst page on the
ab070 20 6c 69 73 74 20 2a 2f 0a 20 20 46 69 66 6f 50   list */.  FifoP
ab080 61 67 65 20 2a 70 4c 61 73 74 3b 20 20 20 20 2f  age *pLast;    /
ab090 2a 20 4c 61 73 74 20 70 61 67 65 20 6f 6e 20 74  * Last page on t
ab0a0 68 65 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f  he list */.};../
ab0b0 2a 0a 2a 2a 20 41 20 43 6f 6e 74 65 78 74 20 73  *.** A Context s
ab0c0 74 6f 72 65 73 20 74 68 65 20 6c 61 73 74 20 69  tores the last i
ab0d0 6e 73 65 72 74 20 72 6f 77 69 64 2c 20 74 68 65  nsert rowid, the
ab0e0 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74 20   last statement 
ab0f0 63 68 61 6e 67 65 20 63 6f 75 6e 74 2c 0a 2a 2a  change count,.**
ab100 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74   and the current
ab110 20 73 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67   statement chang
ab120 65 20 63 6f 75 6e 74 20 28 69 2e 65 2e 20 63 68  e count (i.e. ch
ab130 61 6e 67 65 73 20 73 69 6e 63 65 20 6c 61 73 74  anges since last
ab140 20 73 74 61 74 65 6d 65 6e 74 29 2e 0a 2a 2a 20   statement)..** 
ab150 54 68 65 20 63 75 72 72 65 6e 74 20 6b 65 79 6c  The current keyl
ab160 69 73 74 20 69 73 20 61 6c 73 6f 20 73 74 6f 72  ist is also stor
ab170 65 64 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 78  ed in the contex
ab180 74 2e 0a 2a 2a 20 45 6c 65 6d 65 6e 74 73 20 6f  t..** Elements o
ab190 66 20 43 6f 6e 74 65 78 74 20 73 74 72 75 63 74  f Context struct
ab1a0 75 72 65 20 74 79 70 65 20 6d 61 6b 65 20 75 70  ure type make up
ab1b0 20 74 68 65 20 43 6f 6e 74 65 78 74 53 74 61 63   the ContextStac
ab1c0 6b 2c 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 75  k, which is.** u
ab1d0 70 64 61 74 65 64 20 62 79 20 74 68 65 20 43 6f  pdated by the Co
ab1e0 6e 74 65 78 74 50 75 73 68 20 61 6e 64 20 43 6f  ntextPush and Co
ab1f0 6e 74 65 78 74 50 6f 70 20 6f 70 63 6f 64 65 73  ntextPop opcodes
ab200 20 28 75 73 65 64 20 62 79 20 74 72 69 67 67 65   (used by trigge
ab210 72 73 29 2e 0a 2a 2a 20 54 68 65 20 63 6f 6e 74  rs)..** The cont
ab220 65 78 74 20 69 73 20 70 75 73 68 65 64 20 62 65  ext is pushed be
ab230 66 6f 72 65 20 65 78 65 63 75 74 69 6e 67 20 61  fore executing a
ab240 20 74 72 69 67 67 65 72 20 61 20 70 6f 70 70 65   trigger a poppe
ab250 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 74 72  d when the.** tr
ab260 69 67 67 65 72 20 66 69 6e 69 73 68 65 73 2e 0a  igger finishes..
ab270 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
ab280 74 20 43 6f 6e 74 65 78 74 20 43 6f 6e 74 65 78  t Context Contex
ab290 74 3b 0a 73 74 72 75 63 74 20 43 6f 6e 74 65 78  t;.struct Contex
ab2a0 74 20 7b 0a 20 20 69 36 34 20 6c 61 73 74 52 6f  t {.  i64 lastRo
ab2b0 77 69 64 3b 20 20 20 20 2f 2a 20 4c 61 73 74 20  wid;    /* Last 
ab2c0 69 6e 73 65 72 74 20 72 6f 77 69 64 20 28 73 71  insert rowid (sq
ab2d0 6c 69 74 65 33 2e 6c 61 73 74 52 6f 77 69 64 29  lite3.lastRowid)
ab2e0 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67   */.  int nChang
ab2f0 65 3b 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65  e;      /* State
ab300 6d 65 6e 74 20 63 68 61 6e 67 65 73 20 28 56 64  ment changes (Vd
ab310 62 65 2e 6e 43 68 61 6e 67 65 73 29 20 20 20 20  be.nChanges)    
ab320 20 2a 2f 0a 20 20 46 69 66 6f 20 73 46 69 66 6f   */.  Fifo sFifo
ab330 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 63 6f 72  ;       /* Recor
ab340 64 73 20 74 68 61 74 20 77 69 6c 6c 20 70 61 72  ds that will par
ab350 74 69 63 69 70 61 74 65 20 69 6e 20 61 20 44 45  ticipate in a DE
ab360 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 20 2a  LETE or UPDATE *
ab370 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  /.};../*.** An i
ab380 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 76  nstance of the v
ab390 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20  irtual machine. 
ab3a0 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20   This structure 
ab3b0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6d  contains the com
ab3c0 70 6c 65 74 65 0a 2a 2a 20 73 74 61 74 65 20 6f  plete.** state o
ab3d0 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61  f the virtual ma
ab3e0 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  chine..**.** The
ab3f0 20 22 73 71 6c 69 74 65 33 5f 73 74 6d 74 22 20   "sqlite3_stmt" 
ab400 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e 74 65  structure pointe
ab410 72 20 74 68 61 74 20 69 73 20 72 65 74 75 72 6e  r that is return
ab420 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f  ed by sqlite3_co
ab430 6d 70 69 6c 65 28 29 0a 2a 2a 20 69 73 20 72 65  mpile().** is re
ab440 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 74  ally a pointer t
ab450 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  o an instance of
ab460 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e   this structure.
ab470 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 64 62 65 2e  .**.** The Vdbe.
ab480 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 76 61 72  inVtabMethod var
ab490 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20  iable is set to 
ab4a0 6e 6f 6e 2d 7a 65 72 6f 20 66 6f 72 20 74 68 65  non-zero for the
ab4b0 20 64 75 72 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20   duration of.** 
ab4c0 61 6e 79 20 76 69 72 74 75 61 6c 20 74 61 62 6c  any virtual tabl
ab4d0 65 20 6d 65 74 68 6f 64 20 69 6e 76 6f 63 61 74  e method invocat
ab4e0 69 6f 6e 73 20 6d 61 64 65 20 62 79 20 74 68 65  ions made by the
ab4f0 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e 20 49   vdbe program. I
ab500 74 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 32  t is.** set to 2
ab510 20 66 6f 72 20 78 44 65 73 74 72 6f 79 20 6d 65   for xDestroy me
ab520 74 68 6f 64 20 63 61 6c 6c 73 20 61 6e 64 20 31  thod calls and 1
ab530 20 66 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 6d   for all other m
ab540 65 74 68 6f 64 73 2e 20 54 68 69 73 0a 2a 2a 20  ethods. This.** 
ab550 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 65 64  variable is used
ab560 20 66 6f 72 20 74 77 6f 20 70 75 72 70 6f 73 65   for two purpose
ab570 73 3a 20 74 6f 20 61 6c 6c 6f 77 20 78 44 65 73  s: to allow xDes
ab580 74 72 6f 79 20 6d 65 74 68 6f 64 73 20 74 6f 20  troy methods to 
ab590 65 78 65 63 75 74 65 0a 2a 2a 20 22 44 52 4f 50  execute.** "DROP
ab5a0 20 54 41 42 4c 45 22 20 73 74 61 74 65 6d 65 6e   TABLE" statemen
ab5b0 74 73 20 61 6e 64 20 74 6f 20 70 72 65 76 65 6e  ts and to preven
ab5c0 74 20 73 6f 6d 65 20 6e 61 73 74 79 20 73 69 64  t some nasty sid
ab5d0 65 20 65 66 66 65 63 74 73 20 6f 66 0a 2a 2a 20  e effects of.** 
ab5e0 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 77  malloc failure w
ab5f0 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 69 6e  hen SQLite is in
ab600 76 6f 6b 65 64 20 72 65 63 75 72 73 69 76 65 6c  voked recursivel
ab610 79 20 62 79 20 61 20 76 69 72 74 75 61 6c 20 74  y by a virtual t
ab620 61 62 6c 65 20 0a 2a 2a 20 6d 65 74 68 6f 64 20  able .** method 
ab630 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72  function..*/.str
ab640 75 63 74 20 56 64 62 65 20 7b 0a 20 20 73 71 6c  uct Vdbe {.  sql
ab650 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
ab660 20 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20 64 61   /* The whole da
ab670 74 61 62 61 73 65 20 2a 2f 0a 20 20 56 64 62 65  tabase */.  Vdbe
ab680 20 2a 70 50 72 65 76 2c 2a 70 4e 65 78 74 3b 20   *pPrev,*pNext; 
ab690 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f  /* Linked list o
ab6a0 66 20 56 44 42 45 73 20 77 69 74 68 20 74 68 65  f VDBEs with the
ab6b0 20 73 61 6d 65 20 56 64 62 65 2e 64 62 20 2a 2f   same Vdbe.db */
ab6c0 0a 20 20 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20  .  int nOp;     
ab6d0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
ab6e0 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 73   of instructions
ab6f0 20 69 6e 20 74 68 65 20 70 72 6f 67 72 61 6d 20   in the program 
ab700 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70 41 6c 6c 6f  */.  int nOpAllo
ab710 63 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  c;       /* Numb
ab720 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f  er of slots allo
ab730 63 61 74 65 64 20 66 6f 72 20 61 4f 70 5b 5d 20  cated for aOp[] 
ab740 2a 2f 0a 20 20 4f 70 20 2a 61 4f 70 3b 20 20 20  */.  Op *aOp;   
ab750 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63           /* Spac
ab760 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 76 69  e to hold the vi
ab770 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 27 73 20  rtual machine's 
ab780 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74  program */.  int
ab790 20 6e 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 20   nLabel;        
ab7a0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 61   /* Number of la
ab7b0 62 65 6c 73 20 75 73 65 64 20 2a 2f 0a 20 20 69  bels used */.  i
ab7c0 6e 74 20 6e 4c 61 62 65 6c 41 6c 6c 6f 63 3b 20  nt nLabelAlloc; 
ab7d0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
ab7e0 73 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64 20  slots allocated 
ab7f0 69 6e 20 61 4c 61 62 65 6c 5b 5d 20 2a 2f 0a 20  in aLabel[] */. 
ab800 20 69 6e 74 20 2a 61 4c 61 62 65 6c 3b 20 20 20   int *aLabel;   
ab810 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f       /* Space to
ab820 20 68 6f 6c 64 20 74 68 65 20 6c 61 62 65 6c 73   hold the labels
ab830 20 2a 2f 0a 20 20 4d 65 6d 20 2a 2a 61 70 41 72   */.  Mem **apAr
ab840 67 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67  g;        /* Arg
ab850 75 6d 65 6e 74 73 20 74 6f 20 63 75 72 72 65 6e  uments to curren
ab860 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20 75 73  tly executing us
ab870 65 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  er function */. 
ab880 20 4d 65 6d 20 2a 61 43 6f 6c 4e 61 6d 65 3b 20   Mem *aColName; 
ab890 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e       /* Column n
ab8a0 61 6d 65 73 20 74 6f 20 72 65 74 75 72 6e 20 2a  ames to return *
ab8b0 2f 0a 20 20 69 6e 74 20 6e 43 75 72 73 6f 72 3b  /.  int nCursor;
ab8c0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
ab8d0 72 20 6f 66 20 73 6c 6f 74 73 20 69 6e 20 61 70  r of slots in ap
ab8e0 43 73 72 5b 5d 20 2a 2f 0a 20 20 56 64 62 65 43  Csr[] */.  VdbeC
ab8f0 75 72 73 6f 72 20 2a 2a 61 70 43 73 72 3b 20 2f  ursor **apCsr; /
ab900 2a 20 4f 6e 65 20 65 6c 65 6d 65 6e 74 20 6f 66  * One element of
ab910 20 74 68 69 73 20 61 72 72 61 79 20 66 6f 72 20   this array for 
ab920 65 61 63 68 20 6f 70 65 6e 20 63 75 72 73 6f 72  each open cursor
ab930 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 3b 20   */.  int nVar; 
ab940 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
ab950 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
ab960 6e 20 61 56 61 72 5b 5d 20 2a 2f 0a 20 20 4d 65  n aVar[] */.  Me
ab970 6d 20 2a 61 56 61 72 3b 20 20 20 20 20 20 20 20  m *aVar;        
ab980 20 20 2f 2a 20 56 61 6c 75 65 73 20 66 6f 72 20    /* Values for 
ab990 74 68 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 20  the OP_Variable 
ab9a0 6f 70 63 6f 64 65 2e 20 2a 2f 0a 20 20 63 68 61  opcode. */.  cha
ab9b0 72 20 2a 2a 61 7a 56 61 72 3b 20 20 20 20 20 20  r **azVar;      
ab9c0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 76 61 72 69   /* Name of vari
ab9d0 61 62 6c 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6f  ables */.  int o
ab9e0 6b 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 2f  kVar;          /
ab9f0 2a 20 54 72 75 65 20 69 66 20 61 7a 56 61 72 5b  * True if azVar[
aba00 5d 20 68 61 73 20 62 65 65 6e 20 69 6e 69 74 69  ] has been initi
aba10 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20  alized */.  int 
aba20 6d 61 67 69 63 3b 20 20 20 20 20 20 20 20 20 20  magic;          
aba30 20 20 20 20 2f 2a 20 4d 61 67 69 63 20 6e 75 6d      /* Magic num
aba40 62 65 72 20 66 6f 72 20 73 61 6e 69 74 79 20 63  ber for sanity c
aba50 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 69 6e 74  hecking */.  int
aba60 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20   nMem;          
aba70 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
aba80 66 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  f memory locatio
aba90 6e 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c  ns currently all
abaa0 6f 63 61 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 20  ocated */.  Mem 
abab0 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20  *aMem;          
abac0 20 20 20 20 2f 2a 20 54 68 65 20 6d 65 6d 6f 72      /* The memor
abad0 79 20 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20  y locations */. 
abae0 20 69 6e 74 20 6e 43 61 6c 6c 62 61 63 6b 3b 20   int nCallback; 
abaf0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
abb00 65 72 20 6f 66 20 63 61 6c 6c 62 61 63 6b 73 20  er of callbacks 
abb10 69 6e 76 6f 6b 65 64 20 73 6f 20 66 61 72 20 2a  invoked so far *
abb20 2f 0a 20 20 69 6e 74 20 63 61 63 68 65 43 74 72  /.  int cacheCtr
abb30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56  ;           /* V
abb40 64 62 65 43 75 72 73 6f 72 20 72 6f 77 20 63 61  dbeCursor row ca
abb50 63 68 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 63  che generation c
abb60 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 46 69 66 6f  ounter */.  Fifo
abb70 20 73 46 69 66 6f 3b 20 20 20 20 20 20 20 20 20   sFifo;         
abb80 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f 66      /* A list of
abb90 20 52 4f 57 49 44 73 20 2a 2f 0a 20 20 69 6e 74   ROWIDs */.  int
abba0 20 63 6f 6e 74 65 78 74 53 74 61 63 6b 54 6f 70   contextStackTop
abbb0 3b 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66  ;    /* Index of
abbc0 20 74 6f 70 20 65 6c 65 6d 65 6e 74 20 69 6e 20   top element in 
abbd0 74 68 65 20 63 6f 6e 74 65 78 74 20 73 74 61 63  the context stac
abbe0 6b 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6e 74 65  k */.  int conte
abbf0 78 74 53 74 61 63 6b 44 65 70 74 68 3b 20 20 2f  xtStackDepth;  /
abc00 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68  * The size of th
abc10 65 20 22 63 6f 6e 74 65 78 74 22 20 73 74 61 63  e "context" stac
abc20 6b 20 2a 2f 0a 20 20 43 6f 6e 74 65 78 74 20 2a  k */.  Context *
abc30 63 6f 6e 74 65 78 74 53 74 61 63 6b 3b 20 20 2f  contextStack;  /
abc40 2a 20 53 74 61 63 6b 20 75 73 65 64 20 62 79 20  * Stack used by 
abc50 6f 70 63 6f 64 65 73 20 43 6f 6e 74 65 78 74 50  opcodes ContextP
abc60 75 73 68 20 26 20 43 6f 6e 74 65 78 74 50 6f 70  ush & ContextPop
abc70 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20  */.  int pc;    
abc80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
abc90 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e  The program coun
abca0 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  ter */.  int rc;
abcb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
abcc0 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74   /* Value to ret
abcd0 75 72 6e 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  urn */.  unsigne
abce0 64 20 75 6e 69 71 75 65 43 6e 74 3b 20 20 20 20  d uniqueCnt;    
abcf0 20 2f 2a 20 55 73 65 64 20 62 79 20 4f 50 5f 4d   /* Used by OP_M
abd00 61 6b 65 52 65 63 6f 72 64 20 77 68 65 6e 20 50  akeRecord when P
abd10 32 21 3d 30 20 2a 2f 0a 20 20 69 6e 74 20 65 72  2!=0 */.  int er
abd20 72 6f 72 41 63 74 69 6f 6e 3b 20 20 20 20 20 20  rorAction;      
abd30 20 20 2f 2a 20 52 65 63 6f 76 65 72 79 20 61 63    /* Recovery ac
abd40 74 69 6f 6e 20 74 6f 20 64 6f 20 69 6e 20 63 61  tion to do in ca
abd50 73 65 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 2a  se of an error *
abd60 2f 0a 20 20 69 6e 74 20 69 6e 54 65 6d 70 54 72  /.  int inTempTr
abd70 61 6e 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  ans;        /* T
abd80 72 75 65 20 69 66 20 74 65 6d 70 20 64 61 74 61  rue if temp data
abd90 62 61 73 65 20 69 73 20 74 72 61 6e 73 61 63 74  base is transact
abda0 69 6f 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  ioned */.  int n
abdb0 52 65 73 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20  ResColumn;      
abdc0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
abdd0 63 6f 6c 75 6d 6e 73 20 69 6e 20 6f 6e 65 20 72  columns in one r
abde0 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  ow of the result
abdf0 20 73 65 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a   set */.  char *
abe00 2a 61 7a 52 65 73 43 6f 6c 75 6d 6e 3b 20 20 20  *azResColumn;   
abe10 20 20 2f 2a 20 56 61 6c 75 65 73 20 66 6f 72 20    /* Values for 
abe20 6f 6e 65 20 72 6f 77 20 6f 66 20 72 65 73 75 6c  one row of resul
abe30 74 20 2a 2f 20 0a 20 20 63 68 61 72 20 2a 7a 45  t */ .  char *zE
abe40 72 72 4d 73 67 3b 20 20 20 20 20 20 20 20 20 20  rrMsg;          
abe50 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65  /* Error message
abe60 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f   written here */
abe70 0a 20 20 4d 65 6d 20 2a 70 52 65 73 75 6c 74 53  .  Mem *pResultS
abe80 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f  et;        /* Po
abe90 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61  inter to an arra
abea0 79 20 6f 66 20 72 65 73 75 6c 74 73 20 2a 2f 0a  y of results */.
abeb0 20 20 75 38 20 65 78 70 6c 61 69 6e 3b 20 20 20    u8 explain;   
abec0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
abed0 65 20 69 66 20 45 58 50 4c 41 49 4e 20 70 72 65  e if EXPLAIN pre
abee0 73 65 6e 74 20 6f 6e 20 53 51 4c 20 63 6f 6d 6d  sent on SQL comm
abef0 61 6e 64 20 2a 2f 0a 20 20 75 38 20 63 68 61 6e  and */.  u8 chan
abf00 67 65 43 6e 74 4f 6e 3b 20 20 20 20 20 20 20 20  geCntOn;        
abf10 20 2f 2a 20 54 72 75 65 20 74 6f 20 75 70 64 61   /* True to upda
abf20 74 65 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f  te the change-co
abf30 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 65 78  unter */.  u8 ex
abf40 70 69 72 65 64 3b 20 20 20 20 20 20 20 20 20 20  pired;          
abf50 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
abf60 65 20 56 4d 20 6e 65 65 64 73 20 74 6f 20 62 65  e VM needs to be
abf70 20 72 65 63 6f 6d 70 69 6c 65 64 20 2a 2f 0a 20   recompiled */. 
abf80 20 75 38 20 6d 69 6e 57 72 69 74 65 46 69 6c 65   u8 minWriteFile
abf90 46 6f 72 6d 61 74 3b 20 20 2f 2a 20 4d 69 6e 69  Format;  /* Mini
abfa0 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d 61 74 20  mum file format 
abfb0 66 6f 72 20 77 72 69 74 61 62 6c 65 20 64 61 74  for writable dat
abfc0 61 62 61 73 65 20 66 69 6c 65 73 20 2a 2f 0a 20  abase files */. 
abfd0 20 75 38 20 69 6e 56 74 61 62 4d 65 74 68 6f 64   u8 inVtabMethod
abfe0 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 65 65 20  ;        /* See 
abff0 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 2a  comments above *
ac000 2f 0a 20 20 75 38 20 75 73 65 73 53 74 6d 74 4a  /.  u8 usesStmtJ
ac010 6f 75 72 6e 61 6c 3b 20 20 20 20 20 2f 2a 20 54  ournal;     /* T
ac020 72 75 65 20 69 66 20 75 73 65 73 20 61 20 73 74  rue if uses a st
ac030 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20  atement journal 
ac040 2a 2f 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c 79  */.  u8 readOnly
ac050 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
ac060 54 72 75 65 20 66 6f 72 20 72 65 61 64 2d 6f 6e  True for read-on
ac070 6c 79 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f  ly statements */
ac080 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b 20  .  int nChange; 
ac090 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
ac0a0 6d 62 65 72 20 6f 66 20 64 62 20 63 68 61 6e 67  mber of db chang
ac0b0 65 73 20 6d 61 64 65 20 73 69 6e 63 65 20 6c 61  es made since la
ac0c0 73 74 20 72 65 73 65 74 20 2a 2f 0a 20 20 69 36  st reset */.  i6
ac0d0 34 20 73 74 61 72 74 54 69 6d 65 3b 20 20 20 20  4 startTime;    
ac0e0 20 20 20 20 20 20 2f 2a 20 54 69 6d 65 20 77 68        /* Time wh
ac0f0 65 6e 20 71 75 65 72 79 20 73 74 61 72 74 65 64  en query started
ac100 20 2d 20 75 73 65 64 20 66 6f 72 20 70 72 6f 66   - used for prof
ac110 69 6c 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62  iling */.  int b
ac120 74 72 65 65 4d 61 73 6b 3b 20 20 20 20 20 20 20  treeMask;       
ac130 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66     /* Bitmask of
ac140 20 64 62 2d 3e 61 44 62 5b 5d 20 65 6e 74 72 69   db->aDb[] entri
ac150 65 73 20 72 65 66 65 72 65 6e 63 65 64 20 2a 2f  es referenced */
ac160 0a 20 20 42 74 72 65 65 4d 75 74 65 78 41 72 72  .  BtreeMutexArr
ac170 61 79 20 61 4d 75 74 65 78 3b 20 2f 2a 20 41 6e  ay aMutex; /* An
ac180 20 61 72 72 61 79 20 6f 66 20 42 74 72 65 65 20   array of Btree 
ac190 75 73 65 64 20 68 65 72 65 20 61 6e 64 20 6e 65  used here and ne
ac1a0 65 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a 20  eding locks */. 
ac1b0 20 69 6e 74 20 61 43 6f 75 6e 74 65 72 5b 32 5d   int aCounter[2]
ac1c0 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e  ;        /* Coun
ac1d0 74 65 72 73 20 75 73 65 64 20 62 79 20 73 71 6c  ters used by sql
ac1e0 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73  ite3_stmt_status
ac1f0 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 71 6c  () */.  int nSql
ac200 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
ac210 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
ac220 20 69 6e 20 7a 53 71 6c 20 2a 2f 0a 20 20 63 68   in zSql */.  ch
ac230 61 72 20 2a 7a 53 71 6c 3b 20 20 20 20 20 20 20  ar *zSql;       
ac240 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74      /* Text of t
ac250 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
ac260 20 74 68 61 74 20 67 65 6e 65 72 61 74 65 64 20   that generated 
ac270 74 68 69 73 20 2a 2f 0a 23 69 66 64 65 66 20 53  this */.#ifdef S
ac280 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 46 49  QLITE_DEBUG.  FI
ac290 4c 45 20 2a 74 72 61 63 65 3b 20 20 20 20 20 20  LE *trace;      
ac2a0 20 20 20 20 2f 2a 20 57 72 69 74 65 20 61 6e 20      /* Write an 
ac2b0 65 78 65 63 75 74 69 6f 6e 20 74 72 61 63 65 20  execution trace 
ac2c0 68 65 72 65 2c 20 69 66 20 6e 6f 74 20 4e 55 4c  here, if not NUL
ac2d0 4c 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e  L */.#endif.  in
ac2e0 74 20 6f 70 65 6e 65 64 53 74 61 74 65 6d 65 6e  t openedStatemen
ac2f0 74 3b 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74  t;  /* True if t
ac300 68 69 73 20 56 4d 20 68 61 73 20 6f 70 65 6e 65  his VM has opene
ac310 64 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f  d a statement jo
ac320 75 72 6e 61 6c 20 2a 2f 0a 23 69 66 64 65 66 20  urnal */.#ifdef 
ac330 53 51 4c 49 54 45 5f 53 53 45 0a 20 20 69 6e 74  SQLITE_SSE.  int
ac340 20 66 65 74 63 68 49 64 3b 20 20 20 20 20 20 20   fetchId;       
ac350 20 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20     /* Statement 
ac360 6e 75 6d 62 65 72 20 75 73 65 64 20 62 79 20 73  number used by s
ac370 71 6c 69 74 65 33 5f 66 65 74 63 68 5f 73 74 61  qlite3_fetch_sta
ac380 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  tement */.  int 
ac390 6c 72 75 3b 20 20 20 20 20 20 20 20 20 20 20 20  lru;            
ac3a0 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 75 73 65    /* Counter use
ac3b0 64 20 66 6f 72 20 4c 52 55 20 63 61 63 68 65 20  d for LRU cache 
ac3c0 72 65 70 6c 61 63 65 6d 65 6e 74 20 2a 2f 0a 23  replacement */.#
ac3d0 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
ac3e0 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52  ITE_ENABLE_MEMOR
ac3f0 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 20 20 56  Y_MANAGEMENT.  V
ac400 64 62 65 20 2a 70 4c 72 75 50 72 65 76 3b 0a 20  dbe *pLruPrev;. 
ac410 20 56 64 62 65 20 2a 70 4c 72 75 4e 65 78 74 3b   Vdbe *pLruNext;
ac420 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a  .#endif.};../*.*
ac430 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
ac440 61 72 65 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75  are allowed valu
ac450 65 73 20 66 6f 72 20 56 64 62 65 2e 6d 61 67 69  es for Vdbe.magi
ac460 63 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42  c.*/.#define VDB
ac470 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 20 20 20  E_MAGIC_INIT    
ac480 20 30 78 32 36 62 63 65 61 61 35 20 20 20 20 2f   0x26bceaa5    /
ac490 2a 20 42 75 69 6c 64 69 6e 67 20 61 20 56 44 42  * Building a VDB
ac4a0 45 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 23 64 65  E program */.#de
ac4b0 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f  fine VDBE_MAGIC_
ac4c0 52 55 4e 20 20 20 20 20 20 30 78 62 64 66 32 30  RUN      0xbdf20
ac4d0 64 61 33 20 20 20 20 2f 2a 20 56 44 42 45 20 69  da3    /* VDBE i
ac4e0 73 20 72 65 61 64 79 20 74 6f 20 65 78 65 63 75  s ready to execu
ac4f0 74 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44  te */.#define VD
ac500 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 20 20  BE_MAGIC_HALT   
ac510 20 20 30 78 35 31 39 63 32 39 37 33 20 20 20 20    0x519c2973    
ac520 2f 2a 20 56 44 42 45 20 68 61 73 20 63 6f 6d 70  /* VDBE has comp
ac530 6c 65 74 65 64 20 65 78 65 63 75 74 69 6f 6e 20  leted execution 
ac540 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f  */.#define VDBE_
ac550 4d 41 47 49 43 5f 44 45 41 44 20 20 20 20 20 30  MAGIC_DEAD     0
ac560 78 62 36 30 36 63 33 63 38 20 20 20 20 2f 2a 20  xb606c3c8    /* 
ac570 54 68 65 20 56 44 42 45 20 68 61 73 20 62 65 65  The VDBE has bee
ac580 6e 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 2a 2f  n deallocated */
ac590 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e  ../*.** Function
ac5a0 20 70 72 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53   prototypes.*/.S
ac5b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
ac5c0 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 72  id sqlite3VdbeFr
ac5d0 65 65 43 75 72 73 6f 72 28 56 64 62 65 20 2a 2c  eeCursor(Vdbe *,
ac5e0 20 56 64 62 65 43 75 72 73 6f 72 2a 29 3b 0a 76   VdbeCursor*);.v
ac5f0 6f 69 64 20 73 71 6c 69 74 65 56 64 62 65 50 6f  oid sqliteVdbePo
ac600 70 53 74 61 63 6b 28 56 64 62 65 2a 2c 69 6e 74  pStack(Vdbe*,int
ac610 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
ac620 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
ac630 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 56 64  eCursorMoveto(Vd
ac640 62 65 43 75 72 73 6f 72 2a 29 3b 0a 23 69 66 20  beCursor*);.#if 
ac650 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
ac660 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64  EBUG) || defined
ac670 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 0a 53  (VDBE_PROFILE).S
ac680 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
ac690 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 50 72  id sqlite3VdbePr
ac6a0 69 6e 74 4f 70 28 46 49 4c 45 2a 2c 20 69 6e 74  intOp(FILE*, int
ac6b0 2c 20 4f 70 2a 29 3b 0a 23 65 6e 64 69 66 0a 53  , Op*);.#endif.S
ac6c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
ac6d0 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  t sqlite3VdbeSer
ac6e0 69 61 6c 54 79 70 65 4c 65 6e 28 75 33 32 29 3b  ialTypeLen(u32);
ac6f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ac700 75 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 53  u32 sqlite3VdbeS
ac710 65 72 69 61 6c 54 79 70 65 28 4d 65 6d 2a 2c 20  erialType(Mem*, 
ac720 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
ac730 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
ac740 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 75 6e  VdbeSerialPut(un
ac750 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 69 6e  signed char*, in
ac760 74 2c 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53  t, Mem*, int);.S
ac770 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
ac780 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  t sqlite3VdbeSer
ac790 69 61 6c 47 65 74 28 63 6f 6e 73 74 20 75 6e 73  ialGet(const uns
ac7a0 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 75 33 32  igned char*, u32
ac7b0 2c 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f  , Mem*);.SQLITE_
ac7c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
ac7d0 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 41 75  ite3VdbeDeleteAu
ac7e0 78 44 61 74 61 28 56 64 62 65 46 75 6e 63 2a 2c  xData(VdbeFunc*,
ac7f0 20 69 6e 74 29 3b 0a 0a 69 6e 74 20 73 71 6c 69   int);..int sqli
ac800 74 65 32 42 74 72 65 65 4b 65 79 43 6f 6d 70 61  te2BtreeKeyCompa
ac810 72 65 28 42 74 43 75 72 73 6f 72 20 2a 2c 20 63  re(BtCursor *, c
ac820 6f 6e 73 74 20 76 6f 69 64 20 2a 2c 20 69 6e 74  onst void *, int
ac830 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 53  , int, int *);.S
ac840 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
ac850 74 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78  t sqlite3VdbeIdx
ac860 4b 65 79 43 6f 6d 70 61 72 65 28 56 64 62 65 43  KeyCompare(VdbeC
ac870 75 72 73 6f 72 2a 2c 55 6e 70 61 63 6b 65 64 52  ursor*,UnpackedR
ac880 65 63 6f 72 64 2a 2c 69 6e 74 2a 29 3b 0a 53 51  ecord*,int*);.SQ
ac890 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
ac8a0 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 52   sqlite3VdbeIdxR
ac8b0 6f 77 69 64 28 42 74 43 75 72 73 6f 72 20 2a 2c  owid(BtCursor *,
ac8c0 20 69 36 34 20 2a 29 3b 0a 53 51 4c 49 54 45 5f   i64 *);.SQLITE_
ac8d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
ac8e0 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63 6f  te3MemCompare(co
ac8f0 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20  nst Mem*, const 
ac900 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 43 6f 6c 6c  Mem*, const Coll
ac910 53 65 71 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Seq*);.SQLITE_PR
ac920 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
ac930 33 56 64 62 65 45 78 65 63 28 56 64 62 65 2a 29  3VdbeExec(Vdbe*)
ac940 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
ac950 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
ac960 4c 69 73 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c  List(Vdbe*);.SQL
ac970 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
ac980 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28  sqlite3VdbeHalt(
ac990 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Vdbe*);.SQLITE_P
ac9a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
ac9b0 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f  e3VdbeChangeEnco
ac9c0 64 69 6e 67 28 4d 65 6d 20 2a 2c 20 69 6e 74 29  ding(Mem *, int)
ac9d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
ac9e0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
ac9f0 4d 65 6d 54 6f 6f 42 69 67 28 4d 65 6d 2a 29 3b  MemTooBig(Mem*);
aca00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
aca10 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
aca20 65 6d 43 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f 6e  emCopy(Mem*, con
aca30 73 74 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45  st Mem*);.SQLITE
aca40 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
aca50 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c  lite3VdbeMemShal
aca60 6c 6f 77 43 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f  lowCopy(Mem*, co
aca70 6e 73 74 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a  nst Mem*, int);.
aca80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
aca90 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d  oid sqlite3VdbeM
acaa0 65 6d 4d 6f 76 65 28 4d 65 6d 2a 2c 20 4d 65 6d  emMove(Mem*, Mem
acab0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
acac0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
acad0 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74  beMemNulTerminat
acae0 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f  e(Mem*);.SQLITE_
acaf0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
acb00 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72  te3VdbeMemSetStr
acb10 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 63 68 61  (Mem*, const cha
acb20 72 2a 2c 20 69 6e 74 2c 20 75 38 2c 20 76 6f 69  r*, int, u8, voi
acb30 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51  d(*)(void*));.SQ
acb40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
acb50 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
acb60 53 65 74 49 6e 74 36 34 28 4d 65 6d 2a 2c 20 69  SetInt64(Mem*, i
acb70 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  64);.SQLITE_PRIV
acb80 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
acb90 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65  VdbeMemSetDouble
acba0 28 4d 65 6d 2a 2c 20 64 6f 75 62 6c 65 29 3b 0a  (Mem*, double);.
acbb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
acbc0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d  oid sqlite3VdbeM
acbd0 65 6d 53 65 74 4e 75 6c 6c 28 4d 65 6d 2a 29 3b  emSetNull(Mem*);
acbe0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
acbf0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
acc00 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 28 4d  MemSetZeroBlob(M
acc10 65 6d 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  em*,int);.SQLITE
acc20 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
acc30 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57  ite3VdbeMemMakeW
acc40 72 69 74 65 61 62 6c 65 28 4d 65 6d 2a 29 3b 0a  riteable(Mem*);.
acc50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
acc60 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
acc70 6d 53 74 72 69 6e 67 69 66 79 28 4d 65 6d 2a 2c  mStringify(Mem*,
acc80 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
acc90 49 56 41 54 45 20 69 36 34 20 73 71 6c 69 74 65  IVATE i64 sqlite
acca0 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 4d 65  3VdbeIntValue(Me
accb0 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
accc0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
accd0 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79  dbeMemIntegerify
acce0 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  (Mem*);.SQLITE_P
accf0 52 49 56 41 54 45 20 64 6f 75 62 6c 65 20 73 71  RIVATE double sq
acd00 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c  lite3VdbeRealVal
acd10 75 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45  ue(Mem*);.SQLITE
acd20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
acd30 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 72  lite3VdbeInteger
acd40 41 66 66 69 6e 69 74 79 28 4d 65 6d 2a 29 3b 0a  Affinity(Mem*);.
acd50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
acd60 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
acd70 6d 52 65 61 6c 69 66 79 28 4d 65 6d 2a 29 3b 0a  mRealify(Mem*);.
acd80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
acd90 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
acda0 6d 4e 75 6d 65 72 69 66 79 28 4d 65 6d 2a 29 3b  mNumerify(Mem*);
acdb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
acdc0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
acdd0 65 6d 46 72 6f 6d 42 74 72 65 65 28 42 74 43 75  emFromBtree(BtCu
acde0 72 73 6f 72 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e  rsor*,int,int,in
acdf0 74 2c 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f  t,Mem*);.SQLITE_
ace00 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
ace10 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
ace20 73 65 28 4d 65 6d 20 2a 70 29 3b 0a 53 51 4c 49  se(Mem *p);.SQLI
ace30 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
ace40 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
ace50 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 4d 65  leaseExternal(Me
ace60 6d 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52  m *p);.SQLITE_PR
ace70 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
ace80 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65  3VdbeMemFinalize
ace90 28 4d 65 6d 2a 2c 20 46 75 6e 63 44 65 66 2a 29  (Mem*, FuncDef*)
acea0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
aceb0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
acec0 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 69  ite3OpcodeName(i
aced0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
acee0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
acef0 64 62 65 4f 70 63 6f 64 65 48 61 73 50 72 6f 70  dbeOpcodeHasProp
acf00 65 72 74 79 28 69 6e 74 2c 20 69 6e 74 29 3b 0a  erty(int, int);.
acf10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
acf20 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
acf30 6d 47 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c  mGrow(Mem *pMem,
acf40 20 69 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65 73   int n, int pres
acf50 65 72 76 65 29 3b 0a 23 69 66 64 65 66 20 53 51  erve);.#ifdef SQ
acf60 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f  LITE_ENABLE_MEMO
acf70 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 53 51  RY_MANAGEMENT.SQ
acf80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
acf90 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 6c 65   sqlite3VdbeRele
acfa0 61 73 65 42 75 66 66 65 72 73 28 56 64 62 65 20  aseBuffers(Vdbe 
acfb0 2a 70 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66  *p);.#endif..#if
acfc0 6e 64 65 66 20 4e 44 45 42 55 47 0a 53 51 4c 49  ndef NDEBUG.SQLI
acfd0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
acfe0 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
acff0 53 61 6e 69 74 79 28 4d 65 6d 2a 29 3b 0a 23 65  Sanity(Mem*);.#e
ad000 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56  ndif.SQLITE_PRIV
ad010 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
ad020 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28  dbeMemTranslate(
ad030 4d 65 6d 2a 2c 20 75 38 29 3b 0a 23 69 66 64 65  Mem*, u8);.#ifde
ad040 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53  f SQLITE_DEBUG.S
ad050 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
ad060 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
ad070 50 72 69 6e 74 53 71 6c 28 56 64 62 65 2a 29 3b  PrintSql(Vdbe*);
ad080 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ad090 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64    void sqlite3Vd
ad0a0 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74  beMemPrettyPrint
ad0b0 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 63 68 61 72  (Mem *pMem, char
ad0c0 20 2a 7a 42 75 66 29 3b 0a 23 65 6e 64 69 66 0a   *zBuf);.#endif.
ad0d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
ad0e0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
ad0f0 6d 48 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a  mHandleBom(Mem *
ad100 70 4d 65 6d 29 3b 0a 53 51 4c 49 54 45 5f 50 52  pMem);.SQLITE_PR
ad110 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
ad120 65 33 56 64 62 65 46 69 66 6f 49 6e 69 74 28 46  e3VdbeFifoInit(F
ad130 69 66 6f 2a 2c 20 73 71 6c 69 74 65 33 2a 29 3b  ifo*, sqlite3*);
ad140 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ad150 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 46  int sqlite3VdbeF
ad160 69 66 6f 50 75 73 68 28 46 69 66 6f 2a 2c 20 69  ifoPush(Fifo*, i
ad170 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  64);.SQLITE_PRIV
ad180 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
ad190 64 62 65 46 69 66 6f 50 6f 70 28 46 69 66 6f 2a  dbeFifoPop(Fifo*
ad1a0 2c 20 69 36 34 2a 29 3b 0a 53 51 4c 49 54 45 5f  , i64*);.SQLITE_
ad1b0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
ad1c0 69 74 65 33 56 64 62 65 46 69 66 6f 43 6c 65 61  ite3VdbeFifoClea
ad1d0 72 28 46 69 66 6f 2a 29 3b 0a 0a 23 69 66 6e 64  r(Fifo*);..#ifnd
ad1e0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
ad1f0 4e 43 52 42 4c 4f 42 0a 53 51 4c 49 54 45 5f 50  NCRBLOB.SQLITE_P
ad200 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c  RIVATE   int sql
ad210 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e  ite3VdbeMemExpan
ad220 64 42 6c 6f 62 28 4d 65 6d 20 2a 29 3b 0a 23 65  dBlob(Mem *);.#e
ad230 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71  lse.  #define sq
ad240 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61  lite3VdbeMemExpa
ad250 6e 64 42 6c 6f 62 28 78 29 20 53 51 4c 49 54 45  ndBlob(x) SQLITE
ad260 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64  _OK.#endif..#end
ad270 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f  if /* !defined(_
ad280 56 44 42 45 49 4e 54 5f 48 5f 29 20 2a 2f 0a 0a  VDBEINT_H_) */..
ad290 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
ad2a0 45 6e 64 20 6f 66 20 76 64 62 65 49 6e 74 2e 68  End of vdbeInt.h
ad2b0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
ad2c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ad2d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
ad2e0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
ad2f0 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
ad300 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
ad310 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  utf.c **********
ad320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
ad330 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
ad340 77 69 6e 67 20 63 6f 6e 73 74 61 6e 74 20 76 61  wing constant va
ad350 6c 75 65 20 69 73 20 75 73 65 64 20 62 79 20 74  lue is used by t
ad360 68 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44  he SQLITE_BIGEND
ad370 49 41 4e 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54  IAN and.** SQLIT
ad380 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 6d  E_LITTLEENDIAN m
ad390 61 63 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  acros..*/.SQLITE
ad3a0 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69  _PRIVATE const i
ad3b0 6e 74 20 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20  nt sqlite3one = 
ad3c0 31 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6c  1;../*.** This l
ad3d0 6f 6f 6b 75 70 20 74 61 62 6c 65 20 69 73 20 75  ookup table is u
ad3e0 73 65 64 20 74 6f 20 68 65 6c 70 20 64 65 63 6f  sed to help deco
ad3f0 64 65 20 74 68 65 20 66 69 72 73 74 20 62 79 74  de the first byt
ad400 65 20 6f 66 0a 2a 2a 20 61 20 6d 75 6c 74 69 2d  e of.** a multi-
ad410 62 79 74 65 20 55 54 46 38 20 63 68 61 72 61 63  byte UTF8 charac
ad420 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ter..*/.static c
ad430 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
ad440 61 72 20 73 71 6c 69 74 65 33 55 74 66 54 72 61  ar sqlite3UtfTra
ad450 6e 73 31 5b 5d 20 3d 20 7b 0a 20 20 30 78 30 30  ns1[] = {.  0x00
ad460 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78  , 0x01, 0x02, 0x
ad470 30 33 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20  03, 0x04, 0x05, 
ad480 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 20 30 78  0x06, 0x07,.  0x
ad490 30 38 2c 20 30 78 30 39 2c 20 30 78 30 61 2c 20  08, 0x09, 0x0a, 
ad4a0 30 78 30 62 2c 20 30 78 30 63 2c 20 30 78 30 64  0x0b, 0x0c, 0x0d
ad4b0 2c 20 30 78 30 65 2c 20 30 78 30 66 2c 0a 20 20  , 0x0e, 0x0f,.  
ad4c0 30 78 31 30 2c 20 30 78 31 31 2c 20 30 78 31 32  0x10, 0x11, 0x12
ad4d0 2c 20 30 78 31 33 2c 20 30 78 31 34 2c 20 30 78  , 0x13, 0x14, 0x
ad4e0 31 35 2c 20 30 78 31 36 2c 20 30 78 31 37 2c 0a  15, 0x16, 0x17,.
ad4f0 20 20 30 78 31 38 2c 20 30 78 31 39 2c 20 30 78    0x18, 0x19, 0x
ad500 31 61 2c 20 30 78 31 62 2c 20 30 78 31 63 2c 20  1a, 0x1b, 0x1c, 
ad510 30 78 31 64 2c 20 30 78 31 65 2c 20 30 78 31 66  0x1d, 0x1e, 0x1f
ad520 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20  ,.  0x00, 0x01, 
ad530 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34  0x02, 0x03, 0x04
ad540 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78  , 0x05, 0x06, 0x
ad550 30 37 2c 0a 20 20 30 78 30 38 2c 20 30 78 30 39  07,.  0x08, 0x09
ad560 2c 20 30 78 30 61 2c 20 30 78 30 62 2c 20 30 78  , 0x0a, 0x0b, 0x
ad570 30 63 2c 20 30 78 30 64 2c 20 30 78 30 65 2c 20  0c, 0x0d, 0x0e, 
ad580 30 78 30 66 2c 0a 20 20 30 78 30 30 2c 20 30 78  0x0f,.  0x00, 0x
ad590 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20  01, 0x02, 0x03, 
ad5a0 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36  0x04, 0x05, 0x06
ad5b0 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 30 2c 20  , 0x07,.  0x00, 
ad5c0 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33  0x01, 0x02, 0x03
ad5d0 2c 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78  , 0x00, 0x01, 0x
ad5e0 30 30 2c 20 30 78 30 30 2c 0a 7d 3b 0a 0a 0a 23  00, 0x00,.};...#
ad5f0 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46  define WRITE_UTF
ad600 38 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20 20  8(zOut, c) {    
ad610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad620 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c        \.  if( c<
ad630 30 78 30 30 30 38 30 20 29 7b 20 20 20 20 20 20  0x00080 ){      
ad640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ad660 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28  .    *zOut++ = (
ad670 63 26 30 78 46 46 29 3b 20 20 20 20 20 20 20 20  c&0xFF);        
ad680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad690 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20          \.  }   
ad6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad6d0 20 5c 0a 20 20 65 6c 73 65 20 69 66 28 20 63 3c   \.  else if( c<
ad6e0 30 78 30 30 38 30 30 20 29 7b 20 20 20 20 20 20  0x00800 ){      
ad6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad700 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
ad710 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 43 30 20 2b  *zOut++ = 0xC0 +
ad720 20 28 28 63 3e 3e 36 29 26 30 78 31 46 29 3b 20   ((c>>6)&0x1F); 
ad730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad740 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
ad750 20 3d 20 30 78 38 30 20 2b 20 28 63 20 26 20 30   = 0x80 + (c & 0
ad760 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20  x3F);           
ad770 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
ad780 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }               
ad790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad7b0 20 20 20 20 20 5c 0a 20 20 65 6c 73 65 20 69 66       \.  else if
ad7c0 28 20 63 3c 30 78 31 30 30 30 30 20 29 7b 20 20  ( c<0x10000 ){  
ad7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ad7f0 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78      *zOut++ = 0x
ad800 45 30 20 2b 20 28 28 63 3e 3e 31 32 29 26 30 78  E0 + ((c>>12)&0x
ad810 30 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  0F);            
ad820 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f         \.    *zO
ad830 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 28  ut++ = 0x80 + ((
ad840 63 3e 3e 36 29 20 26 20 30 78 33 46 29 3b 20 20  c>>6) & 0x3F);  
ad850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad860 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
ad870 30 78 38 30 20 2b 20 28 63 20 26 20 30 78 33 46  0x80 + (c & 0x3F
ad880 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
ad890 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c           \.  }el
ad8a0 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  se{             
ad8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad8d0 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
ad8e0 3d 20 30 78 46 30 20 2b 20 28 28 63 3e 3e 31 38  = 0xF0 + ((c>>18
ad8f0 29 20 26 20 30 78 30 37 29 3b 20 20 20 20 20 20  ) & 0x07);      
ad900 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
ad910 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20   *zOut++ = 0x80 
ad920 2b 20 28 28 63 3e 3e 31 32 29 20 26 20 30 78 33  + ((c>>12) & 0x3
ad930 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  F);             
ad940 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b      \.    *zOut+
ad950 2b 20 3d 20 30 78 38 30 20 2b 20 28 28 63 3e 3e  + = 0x80 + ((c>>
ad960 36 29 20 26 20 30 78 33 46 29 3b 20 20 20 20 20  6) & 0x3F);     
ad970 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ad980 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38     *zOut++ = 0x8
ad990 30 20 2b 20 28 63 20 26 20 30 78 33 46 29 3b 20  0 + (c & 0x3F); 
ad9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad9b0 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20        \.  }     
ad9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ad9f0 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 54  .}..#define WRIT
ada00 45 5f 55 54 46 31 36 4c 45 28 7a 4f 75 74 2c 20  E_UTF16LE(zOut, 
ada10 63 29 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  c) {            
ada20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ada30 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c 3d 30      \.  if( c<=0
ada40 78 46 46 46 46 20 29 7b 20 20 20 20 20 20 20 20  xFFFF ){        
ada50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ada60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ada70 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75        \.    *zOu
ada80 74 2b 2b 20 3d 20 28 63 26 30 78 30 30 46 46 29  t++ = (c&0x00FF)
ada90 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
adaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adab0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a          \.    *z
adac0 4f 75 74 2b 2b 20 3d 20 28 28 63 3e 3e 38 29 26  Out++ = ((c>>8)&
adad0 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20  0x00FF);        
adae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adaf0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65            \.  }e
adb00 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20  lse{            
adb10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adb30 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
adb40 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 28 28 63    *zOut++ = (((c
adb50 3e 3e 31 30 29 26 30 78 30 30 33 46 29 20 2b 20  >>10)&0x003F) + 
adb60 28 28 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31  (((c-0x10000)>>1
adb70 30 29 26 30 78 30 30 43 30 29 29 3b 20 20 5c 0a  0)&0x00C0));  \.
adb80 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 30      *zOut++ = (0
adb90 78 30 30 44 38 20 2b 20 28 28 28 63 2d 30 78 31  x00D8 + (((c-0x1
adba0 30 30 30 30 29 3e 3e 31 38 29 26 30 78 30 33 29  0000)>>18)&0x03)
adbb0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
adbc0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
adbd0 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20  (c&0x00FF);     
adbe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adc00 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
adc10 3d 20 28 30 78 30 30 44 43 20 2b 20 28 28 63 3e  = (0x00DC + ((c>
adc20 3e 38 29 26 30 78 30 33 29 29 3b 20 20 20 20 20  >8)&0x03));     
adc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adc40 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20      \.  }       
adc50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adc80 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69        \.}..#defi
adc90 6e 65 20 57 52 49 54 45 5f 55 54 46 31 36 42 45  ne WRITE_UTF16BE
adca0 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20 20 20  (zOut, c) {     
adcb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adcc0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69             \.  i
adcd0 66 28 20 63 3c 3d 30 78 46 46 46 46 20 29 7b 20  f( c<=0xFFFF ){ 
adce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
add00 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
add10 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 28 63     *zOut++ = ((c
add20 3e 3e 38 29 26 30 78 30 30 46 46 29 3b 20 20 20  >>8)&0x00FF);   
add30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
add40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
add50 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28  .    *zOut++ = (
add60 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20  c&0x00FF);      
add70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
add80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
add90 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20   \.  }else{     
adda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
addb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
addc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
addd0 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
adde0 20 3d 20 28 30 78 30 30 44 38 20 2b 20 28 28 28   = (0x00D8 + (((
addf0 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 38 29 26  c-0x10000)>>18)&
ade00 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 20  0x03));         
ade10 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74       \.    *zOut
ade20 2b 2b 20 3d 20 28 28 28 63 3e 3e 31 30 29 26 30  ++ = (((c>>10)&0
ade30 78 30 30 33 46 29 20 2b 20 28 28 28 63 2d 30 78  x003F) + (((c-0x
ade40 31 30 30 30 30 29 3e 3e 31 30 29 26 30 78 30 30  10000)>>10)&0x00
ade50 43 30 29 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f  C0));  \.    *zO
ade60 75 74 2b 2b 20 3d 20 28 30 78 30 30 44 43 20 2b  ut++ = (0x00DC +
ade70 20 28 28 63 3e 3e 38 29 26 30 78 30 33 29 29 3b   ((c>>8)&0x03));
ade80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ade90 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
adea0 7a 4f 75 74 2b 2b 20 3d 20 28 63 26 30 78 30 30  zOut++ = (c&0x00
adeb0 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  FF);            
adec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aded0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d             \.  }
adee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf10 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d               \.}
adf20 0a 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f 55  ..#define READ_U
adf30 54 46 31 36 4c 45 28 7a 49 6e 2c 20 63 29 7b 20  TF16LE(zIn, c){ 
adf40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf60 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20          \.  c = 
adf70 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20  (*zIn++);       
adf80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adfb0 5c 0a 20 20 63 20 2b 3d 20 28 28 2a 7a 49 6e 2b  \.  c += ((*zIn+
adfc0 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20  +)<<8);         
adfd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adfe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adff0 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20          \.  if( 
ae000 63 3e 3d 30 78 44 38 30 30 20 26 26 20 63 3c 30  c>=0xD800 && c<0
ae010 78 45 30 30 30 20 29 7b 20 20 20 20 20 20 20 20  xE000 ){        
ae020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ae040 0a 20 20 20 20 69 6e 74 20 63 32 20 3d 20 28 2a  .    int c2 = (*
ae050 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20  zIn++);         
ae060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae080 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 32 20         \.    c2 
ae090 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29  += ((*zIn++)<<8)
ae0a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
ae0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ae0d0 0a 20 20 20 20 63 20 3d 20 28 63 32 26 30 78 30  .    c = (c2&0x0
ae0e0 33 46 46 29 20 2b 20 28 28 63 26 30 78 30 30 33  3FF) + ((c&0x003
ae0f0 46 29 3c 3c 31 30 29 20 2b 20 28 28 28 63 26 30  F)<<10) + (((c&0
ae100 78 30 33 43 30 29 2b 30 78 30 30 34 30 29 3c 3c  x03C0)+0x0040)<<
ae110 31 30 29 3b 20 20 20 5c 0a 20 20 20 20 69 66 28  10);   \.    if(
ae120 20 28 63 20 26 20 30 78 46 46 46 46 30 30 30 30   (c & 0xFFFF0000
ae130 29 3d 3d 30 20 29 20 63 20 3d 20 30 78 46 46 46  )==0 ) c = 0xFFF
ae140 44 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  D;              
ae150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ae160 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20  .  }            
ae170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae1a0 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66         \.}..#def
ae1b0 69 6e 65 20 52 45 41 44 5f 55 54 46 31 36 42 45  ine READ_UTF16BE
ae1c0 28 7a 49 6e 2c 20 63 29 7b 20 20 20 20 20 20 20  (zIn, c){       
ae1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae1f0 20 20 5c 0a 20 20 63 20 3d 20 28 28 2a 7a 49 6e    \.  c = ((*zIn
ae200 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20  ++)<<8);        
ae210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae230 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20            \.  c 
ae240 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20  += (*zIn++);    
ae250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae280 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 44    \.  if( c>=0xD
ae290 38 30 30 20 26 26 20 63 3c 30 78 45 30 30 30 20  800 && c<0xE000 
ae2a0 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ){              
ae2b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae2c0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69           \.    i
ae2d0 6e 74 20 63 32 20 3d 20 28 28 2a 7a 49 6e 2b 2b  nt c2 = ((*zIn++
ae2e0 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20 20  )<<8);          
ae2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae310 20 5c 0a 20 20 20 20 63 32 20 2b 3d 20 28 2a 7a   \.    c2 += (*z
ae320 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20  In++);          
ae330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae350 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63           \.    c
ae360 20 3d 20 28 63 32 26 30 78 30 33 46 46 29 20 2b   = (c2&0x03FF) +
ae370 20 28 28 63 26 30 78 30 30 33 46 29 3c 3c 31 30   ((c&0x003F)<<10
ae380 29 20 2b 20 28 28 28 63 26 30 78 30 33 43 30 29  ) + (((c&0x03C0)
ae390 2b 30 78 30 30 34 30 29 3c 3c 31 30 29 3b 20 20  +0x0040)<<10);  
ae3a0 20 5c 0a 20 20 20 20 69 66 28 20 28 63 20 26 20   \.    if( (c & 
ae3b0 30 78 46 46 46 46 30 30 30 30 29 3d 3d 30 20 29  0xFFFF0000)==0 )
ae3c0 20 63 20 3d 20 30 78 46 46 46 44 3b 20 20 20 20   c = 0xFFFD;    
ae3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae3e0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20           \.  }  
ae3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae430 20 5c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e   \.}../*.** Tran
ae440 73 6c 61 74 65 20 61 20 73 69 6e 67 6c 65 20 55  slate a single U
ae450 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e 20  TF-8 character. 
ae460 20 52 65 74 75 72 6e 20 74 68 65 20 75 6e 69 63   Return the unic
ae470 6f 64 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a  ode value..**.**
ae480 20 44 75 72 69 6e 67 20 74 72 61 6e 73 6c 61 74   During translat
ae490 69 6f 6e 2c 20 61 73 73 75 6d 65 20 74 68 61 74  ion, assume that
ae4a0 20 74 68 65 20 62 79 74 65 20 74 68 61 74 20 7a   the byte that z
ae4b0 54 65 72 6d 20 70 6f 69 6e 74 73 0a 2a 2a 20 69  Term points.** i
ae4c0 73 20 61 20 30 78 30 30 2e 0a 2a 2a 0a 2a 2a 20  s a 0x00..**.** 
ae4d0 57 72 69 74 65 20 61 20 70 6f 69 6e 74 65 72 20  Write a pointer 
ae4e0 74 6f 20 74 68 65 20 6e 65 78 74 20 75 6e 72 65  to the next unre
ae4f0 61 64 20 62 79 74 65 20 62 61 63 6b 20 69 6e 74  ad byte back int
ae500 6f 20 2a 70 7a 4e 65 78 74 2e 0a 2a 2a 0a 2a 2a  o *pzNext..**.**
ae510 20 4e 6f 74 65 73 20 4f 6e 20 49 6e 76 61 6c 69   Notes On Invali
ae520 64 20 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20  d UTF-8:.**.**  
ae530 2a 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  *  This routine 
ae540 6e 65 76 65 72 20 61 6c 6c 6f 77 73 20 61 20 37  never allows a 7
ae550 2d 62 69 74 20 63 68 61 72 61 63 74 65 72 20 28  -bit character (
ae560 30 78 30 30 20 74 68 72 6f 75 67 68 20 30 78 37  0x00 through 0x7
ae570 66 29 20 74 6f 0a 2a 2a 20 20 20 20 20 62 65 20  f) to.**     be 
ae580 65 6e 63 6f 64 65 64 20 61 73 20 61 20 6d 75 6c  encoded as a mul
ae590 74 69 2d 62 79 74 65 20 63 68 61 72 61 63 74 65  ti-byte characte
ae5a0 72 2e 20 20 41 6e 79 20 6d 75 6c 74 69 2d 62 79  r.  Any multi-by
ae5b0 74 65 20 63 68 61 72 61 63 74 65 72 20 74 68 61  te character tha
ae5c0 74 0a 2a 2a 20 20 20 20 20 61 74 74 65 6d 70 74  t.**     attempt
ae5d0 73 20 74 6f 20 65 6e 63 6f 64 65 20 61 20 76 61  s to encode a va
ae5e0 6c 75 65 20 62 65 74 77 65 65 6e 20 30 78 30 30  lue between 0x00
ae5f0 20 61 6e 64 20 30 78 37 66 20 69 73 20 72 65 6e   and 0x7f is ren
ae600 64 65 72 65 64 20 61 73 20 30 78 66 66 66 64 2e  dered as 0xfffd.
ae610 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73 20  .**.**  *  This 
ae620 72 6f 75 74 69 6e 65 20 6e 65 76 65 72 20 61 6c  routine never al
ae630 6c 6f 77 73 20 61 20 55 54 46 31 36 20 73 75 72  lows a UTF16 sur
ae640 72 6f 67 61 74 65 20 76 61 6c 75 65 20 74 6f 20  rogate value to 
ae650 62 65 20 65 6e 63 6f 64 65 64 2e 0a 2a 2a 20 20  be encoded..**  
ae660 20 20 20 49 66 20 61 20 6d 75 6c 74 69 2d 62 79     If a multi-by
ae670 74 65 20 63 68 61 72 61 63 74 65 72 20 61 74 74  te character att
ae680 65 6d 70 74 73 20 74 6f 20 65 6e 63 6f 64 65 20  empts to encode 
ae690 61 20 76 61 6c 75 65 20 62 65 74 77 65 65 6e 0a  a value between.
ae6a0 2a 2a 20 20 20 20 20 30 78 64 38 30 30 20 61 6e  **     0xd800 an
ae6b0 64 20 30 78 65 30 30 30 20 74 68 65 6e 20 69 74  d 0xe000 then it
ae6c0 20 69 73 20 72 65 6e 64 65 72 65 64 20 61 73 20   is rendered as 
ae6d0 30 78 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a  0xfffd..**.**  *
ae6e0 20 20 42 79 74 65 73 20 69 6e 20 74 68 65 20 72    Bytes in the r
ae6f0 61 6e 67 65 20 6f 66 20 30 78 38 30 20 74 68 72  ange of 0x80 thr
ae700 6f 75 67 68 20 30 78 62 66 20 77 68 69 63 68 20  ough 0xbf which 
ae710 6f 63 63 75 72 20 61 73 20 74 68 65 20 66 69 72  occur as the fir
ae720 73 74 0a 2a 2a 20 20 20 20 20 62 79 74 65 20 6f  st.**     byte o
ae730 66 20 61 20 63 68 61 72 61 63 74 65 72 20 61 72  f a character ar
ae740 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73  e interpreted as
ae750 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 68 61   single-byte cha
ae760 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 61  racters.**     a
ae770 6e 64 20 72 65 6e 64 65 72 65 64 20 61 73 20 74  nd rendered as t
ae780 68 65 6d 73 65 6c 76 65 73 20 65 76 65 6e 20 74  hemselves even t
ae790 68 6f 75 67 68 20 74 68 65 79 20 61 72 65 20 74  hough they are t
ae7a0 65 63 68 6e 69 63 61 6c 6c 79 0a 2a 2a 20 20 20  echnically.**   
ae7b0 20 20 69 6e 76 61 6c 69 64 20 63 68 61 72 61 63    invalid charac
ae7c0 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20  ters..**.**  *  
ae7d0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63 63  This routine acc
ae7e0 65 70 74 73 20 61 6e 20 69 6e 66 69 6e 69 74 65  epts an infinite
ae7f0 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 66 66 65   number of diffe
ae800 72 65 6e 74 20 55 54 46 38 20 65 6e 63 6f 64 69  rent UTF8 encodi
ae810 6e 67 73 0a 2a 2a 20 20 20 20 20 66 6f 72 20 75  ngs.**     for u
ae820 6e 69 63 6f 64 65 20 76 61 6c 75 65 73 20 30 78  nicode values 0x
ae830 38 30 20 61 6e 64 20 67 72 65 61 74 65 72 2e 20  80 and greater. 
ae840 20 49 74 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67   It do not chang
ae850 65 20 6f 76 65 72 2d 6c 65 6e 67 74 68 0a 2a 2a  e over-length.**
ae860 20 20 20 20 20 65 6e 63 6f 64 69 6e 67 73 20 74       encodings t
ae870 6f 20 30 78 66 66 66 64 20 61 73 20 73 6f 6d 65  o 0xfffd as some
ae880 20 73 79 73 74 65 6d 73 20 72 65 63 6f 6d 6d 65   systems recomme
ae890 6e 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52  nd..*/.#define R
ae8a0 45 41 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54  EAD_UTF8(zIn, zT
ae8b0 65 72 6d 2c 20 63 29 20 20 20 20 20 20 20 20 20  erm, c)         
ae8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae8d0 20 20 5c 0a 20 20 63 20 3d 20 2a 28 7a 49 6e 2b    \.  c = *(zIn+
ae8e0 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  +);             
ae8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ae910 0a 20 20 69 66 28 20 63 3e 3d 30 78 63 30 20 29  .  if( c>=0xc0 )
ae920 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
ae930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae940 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
ae950 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66    c = sqlite3Utf
ae960 54 72 61 6e 73 31 5b 63 2d 30 78 63 30 5d 3b 20  Trans1[c-0xc0]; 
ae970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae980 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 77           \.    w
ae990 68 69 6c 65 28 20 7a 49 6e 21 3d 7a 54 65 72 6d  hile( zIn!=zTerm
ae9a0 20 26 26 20 28 2a 7a 49 6e 20 26 20 30 78 63 30   && (*zIn & 0xc0
ae9b0 29 3d 3d 30 78 38 30 20 29 7b 20 20 20 20 20 20  )==0x80 ){      
ae9c0 20 20 20 20 20 20 5c 0a 20 20 20 20 20 20 63 20        \.      c 
ae9d0 3d 20 28 63 3c 3c 36 29 20 2b 20 28 30 78 33 66  = (c<<6) + (0x3f
ae9e0 20 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b 20 20 20   & *(zIn++));   
ae9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aea00 20 20 20 5c 0a 20 20 20 20 7d 20 20 20 20 20 20     \.    }      
aea10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aea20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aea30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aea40 5c 0a 20 20 20 20 69 66 28 20 63 3c 30 78 38 30  \.    if( c<0x80
aea50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aea60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aea70 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
aea80 20 20 20 20 20 20 20 7c 7c 20 28 63 26 30 78 46         || (c&0xF
aea90 46 46 46 46 38 30 30 29 3d 3d 30 78 44 38 30 30  FFFF800)==0xD800
aeaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aeab0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
aeac0 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46 46 46      || (c&0xFFFF
aead0 46 46 46 45 29 3d 3d 30 78 46 46 46 45 20 29 7b  FFFE)==0xFFFE ){
aeae0 20 20 63 20 3d 20 30 78 46 46 46 44 3b 20 7d 20    c = 0xFFFD; } 
aeaf0 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 53 51 4c         \.  }.SQL
aeb00 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
aeb10 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28  sqlite3Utf8Read(
aeb20 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  .  const unsigne
aeb30 64 20 63 68 61 72 20 2a 7a 2c 20 20 20 20 20 20  d char *z,      
aeb40 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65     /* First byte
aeb50 20 6f 66 20 55 54 46 2d 38 20 63 68 61 72 61 63   of UTF-8 charac
aeb60 74 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75  ter */.  const u
aeb70 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 54  nsigned char *zT
aeb80 65 72 6d 2c 20 20 20 20 20 2f 2a 20 50 72 65 74  erm,     /* Pret
aeb90 65 6e 64 20 74 68 69 73 20 62 79 74 65 20 69 73  end this byte is
aeba0 20 30 78 30 30 20 2a 2f 0a 20 20 63 6f 6e 73 74   0x00 */.  const
aebb0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
aebc0 2a 70 7a 4e 65 78 74 20 20 20 20 2f 2a 20 57 72  *pzNext    /* Wr
aebd0 69 74 65 20 66 69 72 73 74 20 62 79 74 65 20 70  ite first byte p
aebe0 61 73 74 20 55 54 46 2d 38 20 63 68 61 72 20 68  ast UTF-8 char h
aebf0 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ere */.){.  int 
aec00 63 3b 0a 20 20 52 45 41 44 5f 55 54 46 38 28 7a  c;.  READ_UTF8(z
aec10 2c 20 7a 54 65 72 6d 2c 20 63 29 3b 0a 20 20 2a  , zTerm, c);.  *
aec20 70 7a 4e 65 78 74 20 3d 20 7a 3b 0a 20 20 72 65  pzNext = z;.  re
aec30 74 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 0a 0a 2f 2a  turn c;.}...../*
aec40 0a 2a 2a 20 49 66 20 74 68 65 20 54 52 41 4e 53  .** If the TRANS
aec50 4c 41 54 45 5f 54 52 41 43 45 20 6d 61 63 72 6f  LATE_TRACE macro
aec60 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65   is defined, the
aec70 20 76 61 6c 75 65 20 6f 66 20 65 61 63 68 20 4d   value of each M
aec80 65 6d 20 69 73 0a 2a 2a 20 70 72 69 6e 74 65 64  em is.** printed
aec90 20 6f 6e 20 73 74 64 65 72 72 20 6f 6e 20 74 68   on stderr on th
aeca0 65 20 77 61 79 20 69 6e 74 6f 20 61 6e 64 20 6f  e way into and o
aecb0 75 74 20 6f 66 20 73 71 6c 69 74 65 33 56 64 62  ut of sqlite3Vdb
aecc0 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 29 2e  eMemTranslate().
aecd0 0a 2a 2f 20 0a 2f 2a 20 23 64 65 66 69 6e 65 20  .*/ ./* #define 
aece0 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 20  TRANSLATE_TRACE 
aecf0 31 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  1 */..#ifndef SQ
aed00 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
aed10 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
aed20 6e 65 20 74 72 61 6e 73 66 6f 72 6d 73 20 74 68  ne transforms th
aed30 65 20 69 6e 74 65 72 6e 61 6c 20 74 65 78 74 20  e internal text 
aed40 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79  encoding used by
aed50 20 70 4d 65 6d 20 74 6f 0a 2a 2a 20 64 65 73 69   pMem to.** desi
aed60 72 65 64 45 6e 63 2e 20 49 74 20 69 73 20 61 6e  redEnc. It is an
aed70 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 73 74   error if the st
aed80 72 69 6e 67 20 69 73 20 61 6c 72 65 61 64 79 20  ring is already 
aed90 6f 66 20 74 68 65 20 64 65 73 69 72 65 64 0a 2a  of the desired.*
aeda0 2a 20 65 6e 63 6f 64 69 6e 67 2c 20 6f 72 20 69  * encoding, or i
aedb0 66 20 2a 70 4d 65 6d 20 64 6f 65 73 20 6e 6f 74  f *pMem does not
aedc0 20 63 6f 6e 74 61 69 6e 20 61 20 73 74 72 69 6e   contain a strin
aedd0 67 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49  g value..*/.SQLI
aede0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
aedf0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61  qlite3VdbeMemTra
aee00 6e 73 6c 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d  nslate(Mem *pMem
aee10 2c 20 75 38 20 64 65 73 69 72 65 64 45 6e 63 29  , u8 desiredEnc)
aee20 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20  {.  int len;    
aee30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aee40 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  /* Maximum lengt
aee50 68 20 6f 66 20 6f 75 74 70 75 74 20 73 74 72 69  h of output stri
aee60 6e 67 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  ng in bytes */. 
aee70 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
aee80 7a 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20  zOut;           
aee90 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74         /* Output
aeea0 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 75 6e 73   buffer */.  uns
aeeb0 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 3b  igned char *zIn;
aeec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aeed0 20 20 20 2f 2a 20 49 6e 70 75 74 20 69 74 65 72     /* Input iter
aeee0 61 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  ator */.  unsign
aeef0 65 64 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b 20  ed char *zTerm; 
aef00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aef10 2f 2a 20 45 6e 64 20 6f 66 20 69 6e 70 75 74 20  /* End of input 
aef20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
aef30 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20  ar *z;          
aef40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75             /* Ou
aef50 74 70 75 74 20 69 74 65 72 61 74 6f 72 20 2a 2f  tput iterator */
aef60 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
aef70 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d  c;..  assert( pM
aef80 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
aef90 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
aefa0 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
aefb0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d   );.  assert( pM
aefc0 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74  em->flags&MEM_St
aefd0 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  r );.  assert( p
aefe0 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65 73 69 72 65  Mem->enc!=desire
aeff0 64 45 6e 63 20 29 3b 0a 20 20 61 73 73 65 72 74  dEnc );.  assert
af000 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 30 20 29  ( pMem->enc!=0 )
af010 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d  ;.  assert( pMem
af020 2d 3e 6e 3e 3d 30 20 29 3b 0a 0a 23 69 66 20 64  ->n>=0 );..#if d
af030 65 66 69 6e 65 64 28 54 52 41 4e 53 4c 41 54 45  efined(TRANSLATE
af040 5f 54 52 41 43 45 29 20 26 26 20 64 65 66 69 6e  _TRACE) && defin
af050 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
af060 0a 20 20 7b 0a 20 20 20 20 63 68 61 72 20 7a 42  .  {.    char zB
af070 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 73 71 6c  uf[100];.    sql
af080 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74  ite3VdbeMemPrett
af090 79 50 72 69 6e 74 28 70 4d 65 6d 2c 20 7a 42 75  yPrint(pMem, zBu
af0a0 66 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28  f);.    fprintf(
af0b0 73 74 64 65 72 72 2c 20 22 49 4e 50 55 54 3a 20  stderr, "INPUT: 
af0c0 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20   %s\n", zBuf);. 
af0d0 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
af0e0 49 66 20 74 68 65 20 74 72 61 6e 73 6c 61 74 69  If the translati
af0f0 6f 6e 20 69 73 20 62 65 74 77 65 65 6e 20 55 54  on is between UT
af100 46 2d 31 36 20 6c 69 74 74 6c 65 20 61 6e 64 20  F-16 little and 
af110 62 69 67 20 65 6e 64 69 61 6e 2c 20 74 68 65 6e  big endian, then
af120 20 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20   .  ** all that 
af130 69 73 20 72 65 71 75 69 72 65 64 20 69 73 20 74  is required is t
af140 6f 20 73 77 61 70 20 74 68 65 20 62 79 74 65 20  o swap the byte 
af150 6f 72 64 65 72 2e 20 54 68 69 73 20 63 61 73 65  order. This case
af160 20 69 73 20 68 61 6e 64 6c 65 64 0a 20 20 2a 2a   is handled.  **
af170 20 64 69 66 66 65 72 65 6e 74 6c 79 20 66 72 6f   differently fro
af180 6d 20 74 68 65 20 6f 74 68 65 72 73 2e 0a 20 20  m the others..  
af190 2a 2f 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65  */.  if( pMem->e
af1a0 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  nc!=SQLITE_UTF8 
af1b0 26 26 20 64 65 73 69 72 65 64 45 6e 63 21 3d 53  && desiredEnc!=S
af1c0 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20  QLITE_UTF8 ){.  
af1d0 20 20 75 38 20 74 65 6d 70 3b 0a 20 20 20 20 69    u8 temp;.    i
af1e0 6e 74 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20  nt rc;.    rc = 
af1f0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61  sqlite3VdbeMemMa
af200 6b 65 57 72 69 74 65 61 62 6c 65 28 70 4d 65 6d  keWriteable(pMem
af210 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
af220 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
af230 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
af240 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20  LITE_NOMEM );.  
af250 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
af260 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20  E_NOMEM;.    }. 
af270 20 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d     zIn = (u8*)pM
af280 65 6d 2d 3e 7a 3b 0a 20 20 20 20 7a 54 65 72 6d  em->z;.    zTerm
af290 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 26   = &zIn[pMem->n&
af2a0 7e 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  ~1];.    while( 
af2b0 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20  zIn<zTerm ){.   
af2c0 20 20 20 74 65 6d 70 20 3d 20 2a 7a 49 6e 3b 0a     temp = *zIn;.
af2d0 20 20 20 20 20 20 2a 7a 49 6e 20 3d 20 2a 28 7a        *zIn = *(z
af2e0 49 6e 2b 31 29 3b 0a 20 20 20 20 20 20 7a 49 6e  In+1);.      zIn
af2f0 2b 2b 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 2b 2b  ++;.      *zIn++
af300 20 3d 20 74 65 6d 70 3b 0a 20 20 20 20 7d 0a 20   = temp;.    }. 
af310 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64     pMem->enc = d
af320 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 20 20 67  esiredEnc;.    g
af330 6f 74 6f 20 74 72 61 6e 73 6c 61 74 65 5f 6f 75  oto translate_ou
af340 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74  t;.  }..  /* Set
af350 20 6c 65 6e 20 74 6f 20 74 68 65 20 6d 61 78 69   len to the maxi
af360 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  mum number of by
af370 74 65 73 20 72 65 71 75 69 72 65 64 20 69 6e 20  tes required in 
af380 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65  the output buffe
af390 72 2e 20 2a 2f 0a 20 20 69 66 28 20 64 65 73 69  r. */.  if( desi
af3a0 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  redEnc==SQLITE_U
af3b0 54 46 38 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68  TF8 ){.    /* Wh
af3c0 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 72  en converting fr
af3d0 6f 6d 20 55 54 46 2d 31 36 2c 20 74 68 65 20 6d  om UTF-16, the m
af3e0 61 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 72 65  aximum growth re
af3f0 73 75 6c 74 73 20 66 72 6f 6d 0a 20 20 20 20 2a  sults from.    *
af400 2a 20 74 72 61 6e 73 6c 61 74 69 6e 67 20 61 20  * translating a 
af410 32 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72  2-byte character
af420 20 74 6f 20 61 20 34 2d 62 79 74 65 20 55 54 46   to a 4-byte UTF
af430 2d 38 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20  -8 character..  
af440 20 20 2a 2a 20 41 20 73 69 6e 67 6c 65 20 62 79    ** A single by
af450 74 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66  te is required f
af460 6f 72 20 74 68 65 20 6f 75 74 70 75 74 20 73 74  or the output st
af470 72 69 6e 67 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d  ring.    ** nul-
af480 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 20  terminator..    
af490 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 26  */.    pMem->n &
af4a0 3d 20 7e 31 3b 0a 20 20 20 20 6c 65 6e 20 3d 20  = ~1;.    len = 
af4b0 70 4d 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 31 3b  pMem->n * 2 + 1;
af4c0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
af4d0 20 57 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67   When converting
af4e0 20 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 55   from UTF-8 to U
af4f0 54 46 2d 31 36 20 74 68 65 20 6d 61 78 69 6d 75  TF-16 the maximu
af500 6d 20 67 72 6f 77 74 68 20 69 73 20 63 61 75 73  m growth is caus
af510 65 64 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 61  ed.    ** when a
af520 20 31 2d 62 79 74 65 20 55 54 46 2d 38 20 63 68   1-byte UTF-8 ch
af530 61 72 61 63 74 65 72 20 69 73 20 74 72 61 6e 73  aracter is trans
af540 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 32 2d 62  lated into a 2-b
af550 79 74 65 20 55 54 46 2d 31 36 0a 20 20 20 20 2a  yte UTF-16.    *
af560 2a 20 63 68 61 72 61 63 74 65 72 2e 20 54 77 6f  * character. Two
af570 20 62 79 74 65 73 20 61 72 65 20 72 65 71 75 69   bytes are requi
af580 72 65 64 20 69 6e 20 74 68 65 20 6f 75 74 70 75  red in the outpu
af590 74 20 62 75 66 66 65 72 20 66 6f 72 20 74 68 65  t buffer for the
af5a0 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d  .    ** nul-term
af5b0 69 6e 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20  inator..    */. 
af5c0 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e     len = pMem->n
af5d0 20 2a 20 32 20 2b 20 32 3b 0a 20 20 7d 0a 0a 20   * 2 + 2;.  }.. 
af5e0 20 2f 2a 20 53 65 74 20 7a 49 6e 20 74 6f 20 70   /* Set zIn to p
af5f0 6f 69 6e 74 20 61 74 20 74 68 65 20 73 74 61 72  oint at the star
af600 74 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 62  t of the input b
af610 75 66 66 65 72 20 61 6e 64 20 7a 54 65 72 6d 20  uffer and zTerm 
af620 74 6f 20 70 6f 69 6e 74 20 31 0a 20 20 2a 2a 20  to point 1.  ** 
af630 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65 6e  byte past the en
af640 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 72  d..  **.  ** Var
af650 69 61 62 6c 65 20 7a 4f 75 74 20 69 73 20 73 65  iable zOut is se
af660 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68  t to point at th
af670 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2c  e output buffer,
af680 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 0a   space obtained.
af690 20 20 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65    ** from sqlite
af6a0 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 20 20 2a 2f  3_malloc()..  */
af6b0 0a 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d  .  zIn = (u8*)pM
af6c0 65 6d 2d 3e 7a 3b 0a 20 20 7a 54 65 72 6d 20 3d  em->z;.  zTerm =
af6d0 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 5d 3b 0a   &zIn[pMem->n];.
af6e0 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33    zOut = sqlite3
af6f0 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d  DbMallocRaw(pMem
af700 2d 3e 64 62 2c 20 6c 65 6e 29 3b 0a 20 20 69 66  ->db, len);.  if
af710 28 20 21 7a 4f 75 74 20 29 7b 0a 20 20 20 20 72  ( !zOut ){.    r
af720 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
af730 45 4d 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 7a 4f  EM;.  }.  z = zO
af740 75 74 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d 2d  ut;..  if( pMem-
af750 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  >enc==SQLITE_UTF
af760 38 20 29 7b 0a 20 20 20 20 69 66 28 20 64 65 73  8 ){.    if( des
af770 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f  iredEnc==SQLITE_
af780 55 54 46 31 36 4c 45 20 29 7b 0a 20 20 20 20 20  UTF16LE ){.     
af790 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54 46   /* UTF-8 -> UTF
af7a0 2d 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61  -16 Little-endia
af7b0 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65  n */.      while
af7c0 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20  ( zIn<zTerm ){. 
af7d0 20 20 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 71         /* c = sq
af7e0 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49  lite3Utf8Read(zI
af7f0 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74  n, zTerm, (const
af800 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f 0a   u8**)&zIn); */.
af810 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46          READ_UTF
af820 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29  8(zIn, zTerm, c)
af830 3b 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f  ;.        WRITE_
af840 55 54 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0a 20  UTF16LE(z, c);. 
af850 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
af860 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
af870 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49  desiredEnc==SQLI
af880 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20  TE_UTF16BE );.  
af890 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20      /* UTF-8 -> 
af8a0 55 54 46 2d 31 36 20 42 69 67 2d 65 6e 64 69 61  UTF-16 Big-endia
af8b0 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65  n */.      while
af8c0 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20  ( zIn<zTerm ){. 
af8d0 20 20 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 71         /* c = sq
af8e0 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49  lite3Utf8Read(zI
af8f0 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74  n, zTerm, (const
af900 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f 0a   u8**)&zIn); */.
af910 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46          READ_UTF
af920 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29  8(zIn, zTerm, c)
af930 3b 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f  ;.        WRITE_
af940 55 54 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20  UTF16BE(z, c);. 
af950 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
af960 20 70 4d 65 6d 2d 3e 6e 20 3d 20 7a 20 2d 20 7a   pMem->n = z - z
af970 4f 75 74 3b 0a 20 20 20 20 2a 7a 2b 2b 20 3d 20  Out;.    *z++ = 
af980 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  0;.  }else{.    
af990 61 73 73 65 72 74 28 20 64 65 73 69 72 65 64 45  assert( desiredE
af9a0 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  nc==SQLITE_UTF8 
af9b0 29 3b 0a 20 20 20 20 69 66 28 20 70 4d 65 6d 2d  );.    if( pMem-
af9c0 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  >enc==SQLITE_UTF
af9d0 31 36 4c 45 20 29 7b 0a 20 20 20 20 20 20 2f 2a  16LE ){.      /*
af9e0 20 55 54 46 2d 31 36 20 4c 69 74 74 6c 65 2d 65   UTF-16 Little-e
af9f0 6e 64 69 61 6e 20 2d 3e 20 55 54 46 2d 38 20 2a  ndian -> UTF-8 *
afa00 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 7a  /.      while( z
afa10 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20  In<zTerm ){.    
afa20 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 4c 45      READ_UTF16LE
afa30 28 7a 49 6e 2c 20 63 29 3b 20 0a 20 20 20 20 20  (zIn, c); .     
afa40 20 20 20 57 52 49 54 45 5f 55 54 46 38 28 7a 2c     WRITE_UTF8(z,
afa50 20 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   c);.      }.   
afa60 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a   }else{.      /*
afa70 20 55 54 46 2d 31 36 20 42 69 67 2d 65 6e 64 69   UTF-16 Big-endi
afa80 61 6e 20 2d 3e 20 55 54 46 2d 38 20 2a 2f 0a 20  an -> UTF-8 */. 
afa90 20 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c       while( zIn<
afaa0 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 20  zTerm ){.       
afab0 20 52 45 41 44 5f 55 54 46 31 36 42 45 28 7a 49   READ_UTF16BE(zI
afac0 6e 2c 20 63 29 3b 20 0a 20 20 20 20 20 20 20 20  n, c); .        
afad0 57 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 63 29  WRITE_UTF8(z, c)
afae0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
afaf0 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 7a 20      pMem->n = z 
afb00 2d 20 7a 4f 75 74 3b 0a 20 20 7d 0a 20 20 2a 7a  - zOut;.  }.  *z
afb10 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
afb20 28 70 4d 65 6d 2d 3e 6e 2b 28 64 65 73 69 72 65  (pMem->n+(desire
afb30 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  dEnc==SQLITE_UTF
afb40 38 3f 31 3a 32 29 29 3c 3d 6c 65 6e 20 29 3b 0a  8?1:2))<=len );.
afb50 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
afb60 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a  mRelease(pMem);.
afb70 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d    pMem->flags &=
afb80 20 7e 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45   ~(MEM_Static|ME
afb90 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29  M_Dyn|MEM_Ephem)
afba0 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20  ;.  pMem->enc = 
afbb0 64 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 70 4d  desiredEnc;.  pM
afbc0 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 4d 45  em->flags |= (ME
afbd0 4d 5f 54 65 72 6d 7c 4d 45 4d 5f 44 79 6e 29 3b  M_Term|MEM_Dyn);
afbe0 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68  .  pMem->z = (ch
afbf0 61 72 2a 29 7a 4f 75 74 3b 0a 20 20 70 4d 65 6d  ar*)zOut;.  pMem
afc00 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d  ->zMalloc = pMem
afc10 2d 3e 7a 3b 0a 0a 74 72 61 6e 73 6c 61 74 65 5f  ->z;..translate_
afc20 6f 75 74 3a 0a 23 69 66 20 64 65 66 69 6e 65 64  out:.#if defined
afc30 28 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45  (TRANSLATE_TRACE
afc40 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c  ) && defined(SQL
afc50 49 54 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a 20  ITE_DEBUG).  {. 
afc60 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30     char zBuf[100
afc70 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ];.    sqlite3Vd
afc80 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74  beMemPrettyPrint
afc90 28 70 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20 20  (pMem, zBuf);.  
afca0 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72    fprintf(stderr
afcb0 2c 20 22 4f 55 54 50 55 54 3a 20 25 73 5c 6e 22  , "OUTPUT: %s\n"
afcc0 2c 20 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65 6e  , zBuf);.  }.#en
afcd0 64 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  dif.  return SQL
afce0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
afcf0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68   This routine ch
afd00 65 63 6b 73 20 66 6f 72 20 61 20 62 79 74 65 2d  ecks for a byte-
afd10 6f 72 64 65 72 20 6d 61 72 6b 20 61 74 20 74 68  order mark at th
afd20 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74  e beginning of t
afd30 68 65 20 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74  he .** UTF-16 st
afd40 72 69 6e 67 20 73 74 6f 72 65 64 20 69 6e 20 2a  ring stored in *
afd50 70 4d 65 6d 2e 20 49 66 20 6f 6e 65 20 69 73 20  pMem. If one is 
afd60 70 72 65 73 65 6e 74 2c 20 69 74 20 69 73 20 72  present, it is r
afd70 65 6d 6f 76 65 64 20 61 6e 64 0a 2a 2a 20 74 68  emoved and.** th
afd80 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68  e encoding of th
afd90 65 20 4d 65 6d 20 61 64 6a 75 73 74 65 64 2e 20  e Mem adjusted. 
afda0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65  This routine doe
afdb0 73 20 6e 6f 74 20 64 6f 20 61 6e 79 0a 2a 2a 20  s not do any.** 
afdc0 62 79 74 65 2d 73 77 61 70 70 69 6e 67 2c 20 69  byte-swapping, i
afdd0 74 20 6a 75 73 74 20 73 65 74 73 20 4d 65 6d 2e  t just sets Mem.
afde0 65 6e 63 20 61 70 70 72 6f 70 72 69 61 74 65 6c  enc appropriatel
afdf0 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c  y..**.** The all
afe00 6f 63 61 74 69 6f 6e 20 28 73 74 61 74 69 63 2c  ocation (static,
afe10 20 64 79 6e 61 6d 69 63 20 65 74 63 2e 29 20 61   dynamic etc.) a
afe20 6e 64 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74  nd encoding of t
afe30 68 65 20 4d 65 6d 20 6d 61 79 20 62 65 0a 2a 2a  he Mem may be.**
afe40 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73   changed by this
afe50 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51   function..*/.SQ
afe60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
afe70 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 48   sqlite3VdbeMemH
afe80 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d  andleBom(Mem *pM
afe90 65 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  em){.  int rc = 
afea0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20  SQLITE_OK;.  u8 
afeb0 62 6f 6d 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20  bom = 0;..  if( 
afec0 70 4d 65 6d 2d 3e 6e 3c 30 20 7c 7c 20 70 4d 65  pMem->n<0 || pMe
afed0 6d 2d 3e 6e 3e 31 20 29 7b 0a 20 20 20 20 75 38  m->n>1 ){.    u8
afee0 20 62 31 20 3d 20 2a 28 75 38 20 2a 29 70 4d 65   b1 = *(u8 *)pMe
afef0 6d 2d 3e 7a 3b 0a 20 20 20 20 75 38 20 62 32 20  m->z;.    u8 b2 
aff00 3d 20 2a 28 28 28 75 38 20 2a 29 70 4d 65 6d 2d  = *(((u8 *)pMem-
aff10 3e 7a 29 20 2b 20 31 29 3b 0a 20 20 20 20 69 66  >z) + 1);.    if
aff20 28 20 62 31 3d 3d 30 78 46 45 20 26 26 20 62 32  ( b1==0xFE && b2
aff30 3d 3d 30 78 46 46 20 29 7b 0a 20 20 20 20 20 20  ==0xFF ){.      
aff40 62 6f 6d 20 3d 20 53 51 4c 49 54 45 5f 55 54 46  bom = SQLITE_UTF
aff50 31 36 42 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20  16BE;.    }.    
aff60 69 66 28 20 62 31 3d 3d 30 78 46 46 20 26 26 20  if( b1==0xFF && 
aff70 62 32 3d 3d 30 78 46 45 20 29 7b 0a 20 20 20 20  b2==0xFE ){.    
aff80 20 20 62 6f 6d 20 3d 20 53 51 4c 49 54 45 5f 55    bom = SQLITE_U
aff90 54 46 31 36 4c 45 3b 0a 20 20 20 20 7d 0a 20 20  TF16LE;.    }.  
affa0 7d 0a 20 20 0a 20 20 69 66 28 20 62 6f 6d 20 29  }.  .  if( bom )
affb0 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
affc0 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69  e3VdbeMemMakeWri
affd0 74 65 61 62 6c 65 28 70 4d 65 6d 29 3b 0a 20 20  teable(pMem);.  
affe0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
afff0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 4d 65  _OK ){.      pMe
b0000 6d 2d 3e 6e 20 2d 3d 20 32 3b 0a 20 20 20 20 20  m->n -= 2;.     
b0010 20 6d 65 6d 6d 6f 76 65 28 70 4d 65 6d 2d 3e 7a   memmove(pMem->z
b0020 2c 20 26 70 4d 65 6d 2d 3e 7a 5b 32 5d 2c 20 70  , &pMem->z[2], p
b0030 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 20 20 70  Mem->n);.      p
b0040 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20  Mem->z[pMem->n] 
b0050 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 70 4d  = '\0';.      pM
b0060 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d  em->z[pMem->n+1]
b0070 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 70   = '\0';.      p
b0080 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  Mem->flags |= ME
b0090 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 4d  M_Term;.      pM
b00a0 65 6d 2d 3e 65 6e 63 20 3d 20 62 6f 6d 3b 0a 20  em->enc = bom;. 
b00b0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
b00c0 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  n rc;.}.#endif /
b00d0 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  * SQLITE_OMIT_UT
b00e0 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 70 5a  F16 */../*.** pZ
b00f0 20 69 73 20 61 20 55 54 46 2d 38 20 65 6e 63 6f   is a UTF-8 enco
b0100 64 65 64 20 75 6e 69 63 6f 64 65 20 73 74 72 69  ded unicode stri
b0110 6e 67 2e 20 49 66 20 6e 42 79 74 65 20 69 73 20  ng. If nByte is 
b0120 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 0a  less than zero,.
b0130 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75  ** return the nu
b0140 6d 62 65 72 20 6f 66 20 75 6e 69 63 6f 64 65 20  mber of unicode 
b0150 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 70 5a  characters in pZ
b0160 20 75 70 20 74 6f 20 28 62 75 74 20 6e 6f 74 20   up to (but not 
b0170 69 6e 63 6c 75 64 69 6e 67 29 0a 2a 2a 20 74 68  including).** th
b0180 65 20 66 69 72 73 74 20 30 78 30 30 20 62 79 74  e first 0x00 byt
b0190 65 2e 20 49 66 20 6e 42 79 74 65 20 69 73 20 6e  e. If nByte is n
b01a0 6f 74 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72  ot less than zer
b01b0 6f 2c 20 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a  o, return the.**
b01c0 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 63 6f   number of unico
b01d0 64 65 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  de characters in
b01e0 20 74 68 65 20 66 69 72 73 74 20 6e 42 79 74 65   the first nByte
b01f0 20 6f 66 20 70 5a 20 28 6f 72 20 75 70 20 74 6f   of pZ (or up to
b0200 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 30   .** the first 0
b0210 78 30 30 2c 20 77 68 69 63 68 65 76 65 72 20 63  x00, whichever c
b0220 6f 6d 65 73 20 66 69 72 73 74 29 2e 0a 2a 2f 0a  omes first)..*/.
b0230 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
b0240 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 43 68  nt sqlite3Utf8Ch
b0250 61 72 4c 65 6e 28 63 6f 6e 73 74 20 63 68 61 72  arLen(const char
b0260 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 42 79 74 65   *zIn, int nByte
b0270 29 7b 0a 20 20 69 6e 74 20 72 20 3d 20 30 3b 0a  ){.  int r = 0;.
b0280 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 20 3d 20    const u8 *z = 
b0290 28 63 6f 6e 73 74 20 75 38 2a 29 7a 49 6e 3b 0a  (const u8*)zIn;.
b02a0 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 54 65 72    const u8 *zTer
b02b0 6d 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3e 3d  m;.  if( nByte>=
b02c0 30 20 29 7b 0a 20 20 20 20 7a 54 65 72 6d 20 3d  0 ){.    zTerm =
b02d0 20 26 7a 5b 6e 42 79 74 65 5d 3b 0a 20 20 7d 65   &z[nByte];.  }e
b02e0 6c 73 65 7b 0a 20 20 20 20 7a 54 65 72 6d 20 3d  lse{.    zTerm =
b02f0 20 28 63 6f 6e 73 74 20 75 38 2a 29 28 2d 31 29   (const u8*)(-1)
b0300 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
b0310 7a 3c 3d 7a 54 65 72 6d 20 29 3b 0a 20 20 77 68  z<=zTerm );.  wh
b0320 69 6c 65 28 20 2a 7a 21 3d 30 20 26 26 20 7a 3c  ile( *z!=0 && z<
b0330 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 53 51 4c  zTerm ){.    SQL
b0340 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29  ITE_SKIP_UTF8(z)
b0350 3b 0a 20 20 20 20 72 2b 2b 3b 0a 20 20 7d 0a 20  ;.    r++;.  }. 
b0360 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a   return r;.}../*
b0370 20 54 68 69 73 20 74 65 73 74 20 66 75 6e 63 74   This test funct
b0380 69 6f 6e 20 69 73 20 6e 6f 74 20 63 75 72 72 65  ion is not curre
b0390 6e 74 6c 79 20 75 73 65 64 20 62 79 20 74 68 65  ntly used by the
b03a0 20 61 75 74 6f 6d 61 74 65 64 20 74 65 73 74 2d   automated test-
b03b0 73 75 69 74 65 2e 20 0a 2a 2a 20 48 65 6e 63 65  suite. .** Hence
b03c0 20 69 74 20 69 73 20 6f 6e 6c 79 20 61 76 61 69   it is only avai
b03d0 6c 61 62 6c 65 20 69 6e 20 64 65 62 75 67 20 62  lable in debug b
b03e0 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65  uilds..*/.#if de
b03f0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53  fined(SQLITE_TES
b0400 54 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51  T) && defined(SQ
b0410 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a  LITE_DEBUG)./*.*
b0420 2a 20 54 72 61 6e 73 6c 61 74 65 20 55 54 46 2d  * Translate UTF-
b0430 38 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a  8 to UTF-8..**.*
b0440 2a 20 54 68 69 73 20 68 61 73 20 74 68 65 20 65  * This has the e
b0450 66 66 65 63 74 20 6f 66 20 6d 61 6b 69 6e 67 20  ffect of making 
b0460 73 75 72 65 20 74 68 61 74 20 74 68 65 20 73 74  sure that the st
b0470 72 69 6e 67 20 69 73 20 77 65 6c 6c 2d 66 6f 72  ring is well-for
b0480 6d 65 64 0a 2a 2a 20 55 54 46 2d 38 2e 20 20 4d  med.** UTF-8.  M
b0490 69 73 63 6f 64 65 64 20 63 68 61 72 61 63 74 65  iscoded characte
b04a0 72 73 20 61 72 65 20 72 65 6d 6f 76 65 64 2e 0a  rs are removed..
b04b0 2a 2a 0a 2a 2a 20 54 68 65 20 74 72 61 6e 73 6c  **.** The transl
b04c0 61 74 69 6f 6e 20 69 73 20 64 6f 6e 65 20 69 6e  ation is done in
b04d0 2d 70 6c 61 63 65 20 28 73 69 6e 63 65 20 69 74  -place (since it
b04e0 20 69 73 20 69 6d 70 6f 73 73 69 62 6c 65 20 66   is impossible f
b04f0 6f 72 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 63  or the.** correc
b0500 74 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67  t UTF-8 encoding
b0510 20 74 6f 20 62 65 20 6c 6f 6e 67 65 72 20 74 68   to be longer th
b0520 61 6e 20 61 20 6d 61 6c 66 6f 72 6d 65 64 20 65  an a malformed e
b0530 6e 63 6f 64 69 6e 67 29 2e 0a 2a 2f 0a 53 51 4c  ncoding)..*/.SQL
b0540 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b0550 73 71 6c 69 74 65 33 55 74 66 38 54 6f 38 28 75  sqlite3Utf8To8(u
b0560 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49  nsigned char *zI
b0570 6e 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  n){.  unsigned c
b0580 68 61 72 20 2a 7a 4f 75 74 20 3d 20 7a 49 6e 3b  har *zOut = zIn;
b0590 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
b05a0 20 2a 7a 53 74 61 72 74 20 3d 20 7a 49 6e 3b 0a   *zStart = zIn;.
b05b0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
b05c0 2a 7a 54 65 72 6d 20 3d 20 26 7a 49 6e 5b 73 74  *zTerm = &zIn[st
b05d0 72 6c 65 6e 28 28 63 68 61 72 20 2a 29 7a 49 6e  rlen((char *)zIn
b05e0 29 5d 3b 0a 20 20 75 33 32 20 63 3b 0a 0a 20 20  )];.  u32 c;..  
b05f0 77 68 69 6c 65 28 20 7a 49 6e 5b 30 5d 20 29 7b  while( zIn[0] ){
b0600 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33  .    c = sqlite3
b0610 55 74 66 38 52 65 61 64 28 7a 49 6e 2c 20 7a 54  Utf8Read(zIn, zT
b0620 65 72 6d 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a  erm, (const u8**
b0630 29 26 7a 49 6e 29 3b 0a 20 20 20 20 69 66 28 20  )&zIn);.    if( 
b0640 63 21 3d 30 78 66 66 66 64 20 29 7b 0a 20 20 20  c!=0xfffd ){.   
b0650 20 20 20 57 52 49 54 45 5f 55 54 46 38 28 7a 4f     WRITE_UTF8(zO
b0660 75 74 2c 20 63 29 3b 0a 20 20 20 20 7d 0a 20 20  ut, c);.    }.  
b0670 7d 0a 20 20 2a 7a 4f 75 74 20 3d 20 30 3b 0a 20  }.  *zOut = 0;. 
b0680 20 72 65 74 75 72 6e 20 7a 4f 75 74 20 2d 20 7a   return zOut - z
b0690 53 74 61 72 74 3b 0a 7d 0a 23 65 6e 64 69 66 0a  Start;.}.#endif.
b06a0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
b06b0 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a  OMIT_UTF16./*.**
b06c0 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46 2d 31   Convert a UTF-1
b06d0 36 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20  6 string in the 
b06e0 6e 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 20  native encoding 
b06f0 69 6e 74 6f 20 61 20 55 54 46 2d 38 20 73 74 72  into a UTF-8 str
b0700 69 6e 67 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74  ing..** Memory t
b0710 6f 20 68 6f 6c 64 20 74 68 65 20 55 54 46 2d 38  o hold the UTF-8
b0720 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69   string is obtai
b0730 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
b0740 5f 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74  _malloc and must
b0750 0a 2a 2a 20 62 65 20 66 72 65 65 64 20 62 79 20  .** be freed by 
b0760 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63  the calling func
b0770 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 55 4c 4c  tion..**.** NULL
b0780 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
b0790 74 68 65 72 65 20 69 73 20 61 6e 20 61 6c 6c 6f  there is an allo
b07a0 63 61 74 69 6f 6e 20 65 72 72 6f 72 2e 0a 2a 2f  cation error..*/
b07b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b07c0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 55 74 66  char *sqlite3Utf
b07d0 31 36 74 6f 38 28 73 71 6c 69 74 65 33 20 2a 64  16to8(sqlite3 *d
b07e0 62 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  b, const void *z
b07f0 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20  , int nByte){.  
b0800 4d 65 6d 20 6d 3b 0a 20 20 6d 65 6d 73 65 74 28  Mem m;.  memset(
b0810 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 29  &m, 0, sizeof(m)
b0820 29 3b 0a 20 20 6d 2e 64 62 20 3d 20 64 62 3b 0a  );.  m.db = db;.
b0830 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
b0840 53 65 74 53 74 72 28 26 6d 2c 20 7a 2c 20 6e 42  SetStr(&m, z, nB
b0850 79 74 65 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  yte, SQLITE_UTF1
b0860 36 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f  6NATIVE, SQLITE_
b0870 53 54 41 54 49 43 29 3b 0a 20 20 73 71 6c 69 74  STATIC);.  sqlit
b0880 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f  e3VdbeChangeEnco
b0890 64 69 6e 67 28 26 6d 2c 20 53 51 4c 49 54 45 5f  ding(&m, SQLITE_
b08a0 55 54 46 38 29 3b 0a 20 20 69 66 28 20 64 62 2d  UTF8);.  if( db-
b08b0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
b08c0 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
b08d0 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a  MemRelease(&m);.
b08e0 20 20 20 20 6d 2e 7a 20 3d 20 30 3b 0a 20 20 7d      m.z = 0;.  }
b08f0 0a 20 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c  .  assert( (m.fl
b0900 61 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 21  ags & MEM_Term)!
b0910 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  =0 || db->malloc
b0920 46 61 69 6c 65 64 20 29 3b 0a 20 20 61 73 73 65  Failed );.  asse
b0930 72 74 28 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d  rt( (m.flags & M
b0940 45 4d 5f 53 74 72 29 21 3d 30 20 7c 7c 20 64 62  EM_Str)!=0 || db
b0950 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
b0960 3b 0a 20 20 72 65 74 75 72 6e 20 28 6d 2e 66 6c  ;.  return (m.fl
b0970 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 21 3d  ags & MEM_Dyn)!=
b0980 30 20 3f 20 6d 2e 7a 20 3a 20 73 71 6c 69 74 65  0 ? m.z : sqlite
b0990 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 6d 2e  3DbStrDup(db, m.
b09a0 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 5a 20  z);.}../*.** pZ 
b09b0 69 73 20 61 20 55 54 46 2d 31 36 20 65 6e 63 6f  is a UTF-16 enco
b09c0 64 65 64 20 75 6e 69 63 6f 64 65 20 73 74 72 69  ded unicode stri
b09d0 6e 67 2e 20 49 66 20 6e 43 68 61 72 20 69 73 20  ng. If nChar is 
b09e0 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 0a  less than zero,.
b09f0 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75  ** return the nu
b0a00 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 75 70  mber of bytes up
b0a10 20 74 6f 20 28 62 75 74 20 6e 6f 74 20 69 6e 63   to (but not inc
b0a20 6c 75 64 69 6e 67 29 2c 20 74 68 65 20 66 69 72  luding), the fir
b0a30 73 74 20 70 61 69 72 0a 2a 2a 20 6f 66 20 63 6f  st pair.** of co
b0a40 6e 73 65 63 75 74 69 76 65 20 30 78 30 30 20 62  nsecutive 0x00 b
b0a50 79 74 65 73 20 69 6e 20 70 5a 2e 20 49 66 20 6e  ytes in pZ. If n
b0a60 43 68 61 72 20 69 73 20 6e 6f 74 20 6c 65 73 73  Char is not less
b0a70 20 74 68 61 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 74   than zero,.** t
b0a80 68 65 6e 20 72 65 74 75 72 6e 20 74 68 65 20 6e  hen return the n
b0a90 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
b0aa0 6e 20 74 68 65 20 66 69 72 73 74 20 6e 43 68 61  n the first nCha
b0ab0 72 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63  r unicode charac
b0ac0 74 65 72 73 0a 2a 2a 20 69 6e 20 70 5a 20 28 6f  ters.** in pZ (o
b0ad0 72 20 75 70 20 75 6e 74 69 6c 20 74 68 65 20 66  r up until the f
b0ae0 69 72 73 74 20 70 61 69 72 20 6f 66 20 30 78 30  irst pair of 0x0
b0af0 30 20 62 79 74 65 73 2c 20 77 68 69 63 68 65 76  0 bytes, whichev
b0b00 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74 29 2e  er comes first).
b0b10 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
b0b20 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74  TE int sqlite3Ut
b0b30 66 31 36 42 79 74 65 4c 65 6e 28 63 6f 6e 73 74  f16ByteLen(const
b0b40 20 76 6f 69 64 20 2a 7a 49 6e 2c 20 69 6e 74 20   void *zIn, int 
b0b50 6e 43 68 61 72 29 7b 0a 20 20 75 6e 73 69 67 6e  nChar){.  unsign
b0b60 65 64 20 69 6e 74 20 63 20 3d 20 31 3b 0a 20 20  ed int c = 1;.  
b0b70 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 20 3d 20  char const *z = 
b0b80 7a 49 6e 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 30  zIn;.  int n = 0
b0b90 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 55  ;.  if( SQLITE_U
b0ba0 54 46 31 36 4e 41 54 49 56 45 3d 3d 53 51 4c 49  TF16NATIVE==SQLI
b0bb0 54 45 5f 55 54 46 31 36 42 45 20 29 7b 0a 20 20  TE_UTF16BE ){.  
b0bc0 20 20 2f 2a 20 55 73 69 6e 67 20 61 6e 20 22 69    /* Using an "i
b0bd0 66 20 28 53 51 4c 49 54 45 5f 55 54 46 31 36 4e  f (SQLITE_UTF16N
b0be0 41 54 49 56 45 3d 3d 53 51 4c 49 54 45 5f 55 54  ATIVE==SQLITE_UT
b0bf0 46 31 36 42 45 29 22 20 63 6f 6e 73 74 72 75 63  F16BE)" construc
b0c00 74 20 68 65 72 65 0a 20 20 20 20 2a 2a 20 61 6e  t here.    ** an
b0c10 64 20 69 6e 20 6f 74 68 65 72 20 70 61 72 74 73  d in other parts
b0c20 20 6f 66 20 74 68 69 73 20 66 69 6c 65 20 6d 65   of this file me
b0c30 61 6e 73 20 74 68 61 74 20 61 74 20 6f 6e 65 20  ans that at one 
b0c40 62 72 61 6e 63 68 20 77 69 6c 6c 0a 20 20 20 20  branch will.    
b0c50 2a 2a 20 6e 6f 74 20 62 65 20 63 6f 76 65 72 65  ** not be covere
b0c60 64 20 62 79 20 63 6f 76 65 72 61 67 65 20 74 65  d by coverage te
b0c70 73 74 69 6e 67 20 6f 6e 20 61 6e 79 20 73 69 6e  sting on any sin
b0c80 67 6c 65 20 68 6f 73 74 2e 20 42 75 74 20 63 6f  gle host. But co
b0c90 76 65 72 61 67 65 0a 20 20 20 20 2a 2a 20 77 69  verage.    ** wi
b0ca0 6c 6c 20 62 65 20 63 6f 6d 70 6c 65 74 65 20 69  ll be complete i
b0cb0 66 20 74 68 65 20 74 65 73 74 73 20 61 72 65 20  f the tests are 
b0cc0 72 75 6e 20 6f 6e 20 62 6f 74 68 20 61 20 6c 69  run on both a li
b0cd0 74 74 6c 65 2d 65 6e 64 69 61 6e 20 61 6e 64 20  ttle-endian and 
b0ce0 0a 20 20 20 20 2a 2a 20 62 69 67 2d 65 6e 64 69  .    ** big-endi
b0cf0 61 6e 20 68 6f 73 74 2e 20 42 65 63 61 75 73 65  an host. Because
b0d00 20 62 6f 74 68 20 74 68 65 20 55 54 46 31 36 4e   both the UTF16N
b0d10 41 54 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45  ATIVE and SQLITE
b0d20 5f 55 54 46 31 36 42 45 0a 20 20 20 20 2a 2a 20  _UTF16BE.    ** 
b0d30 6d 61 63 72 6f 73 20 61 72 65 20 63 6f 6e 73 74  macros are const
b0d40 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 74  ant at compile t
b0d50 69 6d 65 20 74 68 65 20 63 6f 6d 70 69 6c 65 72  ime the compiler
b0d60 20 63 61 6e 20 64 65 74 65 72 6d 69 6e 65 0a 20   can determine. 
b0d70 20 20 20 2a 2a 20 77 68 69 63 68 20 62 72 61 6e     ** which bran
b0d80 63 68 20 77 69 6c 6c 20 62 65 20 66 6f 6c 6c 6f  ch will be follo
b0d90 77 65 64 2e 20 49 74 20 69 73 20 74 68 65 72 65  wed. It is there
b0da0 66 6f 72 65 20 61 73 73 75 6d 65 64 20 74 68 61  fore assumed tha
b0db0 74 20 6e 6f 20 72 75 6e 74 69 6d 65 0a 20 20 20  t no runtime.   
b0dc0 20 2a 2a 20 70 65 6e 61 6c 74 79 20 69 73 20 70   ** penalty is p
b0dd0 61 69 64 20 66 6f 72 20 74 68 69 73 20 22 69 66  aid for this "if
b0de0 22 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20  " statement..   
b0df0 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 63   */.    while( c
b0e00 20 26 26 20 28 28 6e 43 68 61 72 3c 30 29 20 7c   && ((nChar<0) |
b0e10 7c 20 6e 3c 6e 43 68 61 72 29 20 29 7b 0a 20 20  | n<nChar) ){.  
b0e20 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 42 45      READ_UTF16BE
b0e30 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 6e 2b  (z, c);.      n+
b0e40 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  +;.    }.  }else
b0e50 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 63 20 26  {.    while( c &
b0e60 26 20 28 28 6e 43 68 61 72 3c 30 29 20 7c 7c 20  & ((nChar<0) || 
b0e70 6e 3c 6e 43 68 61 72 29 20 29 7b 0a 20 20 20 20  n<nChar) ){.    
b0e80 20 20 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a    READ_UTF16LE(z
b0e90 2c 20 63 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b  , c);.      n++;
b0ea0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
b0eb0 75 72 6e 20 28 7a 2d 28 63 68 61 72 20 63 6f 6e  urn (z-(char con
b0ec0 73 74 20 2a 29 7a 49 6e 29 2d 28 28 63 3d 3d 30  st *)zIn)-((c==0
b0ed0 29 3f 32 3a 30 29 3b 0a 7d 0a 0a 23 69 66 20 64  )?2:0);.}..#if d
b0ee0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45  efined(SQLITE_TE
b0ef0 53 54 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  ST)./*.** This r
b0f00 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
b0f10 20 66 72 6f 6d 20 74 68 65 20 54 43 4c 20 74 65   from the TCL te
b0f20 73 74 20 66 75 6e 63 74 69 6f 6e 20 22 74 72 61  st function "tra
b0f30 6e 73 6c 61 74 65 5f 73 65 6c 66 74 65 73 74 22  nslate_selftest"
b0f40 2e 0a 2a 2a 20 49 74 20 63 68 65 63 6b 73 20 74  ..** It checks t
b0f50 68 61 74 20 74 68 65 20 70 72 69 6d 69 74 69 76  hat the primitiv
b0f60 65 73 20 66 6f 72 20 73 65 72 69 61 6c 69 7a 69  es for serializi
b0f70 6e 67 20 61 6e 64 20 64 65 73 65 72 69 61 6c 69  ng and deseriali
b0f80 7a 69 6e 67 0a 2a 2a 20 63 68 61 72 61 63 74 65  zing.** characte
b0f90 72 73 20 69 6e 20 65 61 63 68 20 65 6e 63 6f 64  rs in each encod
b0fa0 69 6e 67 20 61 72 65 20 69 6e 76 65 72 73 65 73  ing are inverses
b0fb0 20 6f 66 20 65 61 63 68 20 6f 74 68 65 72 2e 0a   of each other..
b0fc0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b0fd0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 74  E void sqlite3Ut
b0fe0 66 53 65 6c 66 54 65 73 74 28 76 6f 69 64 29 7b  fSelfTest(void){
b0ff0 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
b1000 69 2c 20 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64  i, t;.  unsigned
b1010 20 63 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a   char zBuf[20];.
b1020 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
b1030 2a 7a 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  *z;.  unsigned c
b1040 68 61 72 20 2a 7a 54 65 72 6d 3b 0a 20 20 69 6e  har *zTerm;.  in
b1050 74 20 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  t n;.  unsigned 
b1060 69 6e 74 20 63 3b 0a 0a 20 20 66 6f 72 28 69 3d  int c;..  for(i=
b1070 30 3b 20 69 3c 30 78 30 30 31 31 30 30 30 30 3b  0; i<0x00110000;
b1080 20 69 2b 2b 29 7b 0a 20 20 20 20 7a 20 3d 20 7a   i++){.    z = z
b1090 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f 55  Buf;.    WRITE_U
b10a0 54 46 38 28 7a 2c 20 69 29 3b 0a 20 20 20 20 6e  TF8(z, i);.    n
b10b0 20 3d 20 7a 2d 7a 42 75 66 3b 0a 20 20 20 20 7a   = z-zBuf;.    z
b10c0 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 54 65  [0] = 0;.    zTe
b10d0 72 6d 20 3d 20 7a 3b 0a 20 20 20 20 7a 20 3d 20  rm = z;.    z = 
b10e0 7a 42 75 66 3b 0a 20 20 20 20 63 20 3d 20 73 71  zBuf;.    c = sq
b10f0 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 2c  lite3Utf8Read(z,
b1100 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 20 75   zTerm, (const u
b1110 38 2a 2a 29 26 7a 29 3b 0a 20 20 20 20 74 20 3d  8**)&z);.    t =
b1120 20 69 3b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30   i;.    if( i>=0
b1130 78 44 38 30 30 20 26 26 20 69 3c 3d 30 78 44 46  xD800 && i<=0xDF
b1140 46 46 20 29 20 74 20 3d 20 30 78 46 46 46 44 3b  FF ) t = 0xFFFD;
b1150 0a 20 20 20 20 69 66 28 20 28 69 26 30 78 46 46  .    if( (i&0xFF
b1160 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46 45 20  FFFFFE)==0xFFFE 
b1170 29 20 74 20 3d 20 30 78 46 46 46 44 3b 0a 20 20  ) t = 0xFFFD;.  
b1180 20 20 61 73 73 65 72 74 28 20 63 3d 3d 74 20 29    assert( c==t )
b1190 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 7a  ;.    assert( (z
b11a0 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d  -zBuf)==n );.  }
b11b0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78  .  for(i=0; i<0x
b11c0 30 30 31 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a  00110000; i++){.
b11d0 20 20 20 20 69 66 28 20 69 3e 3d 30 78 44 38 30      if( i>=0xD80
b11e0 30 20 26 26 20 69 3c 30 78 45 30 30 30 20 29 20  0 && i<0xE000 ) 
b11f0 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7a 20  continue;.    z 
b1200 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 52 49 54  = zBuf;.    WRIT
b1210 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 69 29 3b  E_UTF16LE(z, i);
b1220 0a 20 20 20 20 6e 20 3d 20 7a 2d 7a 42 75 66 3b  .    n = z-zBuf;
b1230 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20  .    z[0] = 0;. 
b1240 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20     z = zBuf;.   
b1250 20 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 2c   READ_UTF16LE(z,
b1260 20 63 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28   c);.    assert(
b1270 20 63 3d 3d 69 20 29 3b 0a 20 20 20 20 61 73 73   c==i );.    ass
b1280 65 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e  ert( (z-zBuf)==n
b1290 20 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d   );.  }.  for(i=
b12a0 30 3b 20 69 3c 30 78 30 30 31 31 30 30 30 30 3b  0; i<0x00110000;
b12b0 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69   i++){.    if( i
b12c0 3e 3d 30 78 44 38 30 30 20 26 26 20 69 3c 30 78  >=0xD800 && i<0x
b12d0 45 30 30 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  E000 ) continue;
b12e0 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20  .    z = zBuf;. 
b12f0 20 20 20 57 52 49 54 45 5f 55 54 46 31 36 42 45     WRITE_UTF16BE
b1300 28 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20 3d 20  (z, i);.    n = 
b1310 7a 2d 7a 42 75 66 3b 0a 20 20 20 20 7a 5b 30 5d  z-zBuf;.    z[0]
b1320 20 3d 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42   = 0;.    z = zB
b1330 75 66 3b 0a 20 20 20 20 52 45 41 44 5f 55 54 46  uf;.    READ_UTF
b1340 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20  16BE(z, c);.    
b1350 61 73 73 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a  assert( c==i );.
b1360 20 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d 7a      assert( (z-z
b1370 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 7d  Buf)==n );.  }.}
b1380 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
b1390 45 5f 54 45 53 54 20 2a 2f 0a 23 65 6e 64 69 66  E_TEST */.#endif
b13a0 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
b13b0 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  UTF16 */../*****
b13c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
b13d0 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a   utf.c *********
b13e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b13f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1400 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
b1410 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
b1420 66 69 6c 65 20 75 74 69 6c 2e 63 20 2a 2a 2a 2a  file util.c ****
b1430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1450 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
b1460 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
b1470 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
b1480 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
b1490 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
b14a0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
b14b0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
b14c0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
b14d0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
b14e0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
b14f0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
b1500 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
b1510 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
b1520 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
b1530 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
b1540 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
b1550 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
b1560 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
b1570 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
b1580 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
b1590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b15a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b15b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b15c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b15d0 0a 2a 2a 20 55 74 69 6c 69 74 79 20 66 75 6e 63  .** Utility func
b15e0 74 69 6f 6e 73 20 75 73 65 64 20 74 68 72 6f 75  tions used throu
b15f0 67 68 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a 2a  ghout sqlite..**
b1600 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
b1610 6e 74 61 69 6e 73 20 66 75 6e 63 74 69 6f 6e 73  ntains functions
b1620 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6e 67 20   for allocating 
b1630 6d 65 6d 6f 72 79 2c 20 63 6f 6d 70 61 72 69 6e  memory, comparin
b1640 67 0a 2a 2a 20 73 74 72 69 6e 67 73 2c 20 61 6e  g.** strings, an
b1650 64 20 73 74 75 66 66 20 6c 69 6b 65 20 74 68 61  d stuff like tha
b1660 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 75 74  t..**.** $Id: ut
b1670 69 6c 2e 63 2c 76 20 31 2e 32 34 31 20 32 30 30  il.c,v 1.241 200
b1680 38 2f 30 37 2f 32 38 20 31 39 3a 33 34 3a 35 34  8/07/28 19:34:54
b1690 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a   drh Exp $.*/...
b16a0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  /*.** Return tru
b16b0 65 20 69 66 20 74 68 65 20 66 6c 6f 61 74 69 6e  e if the floatin
b16c0 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 73  g point value is
b16d0 20 4e 6f 74 20 61 20 4e 75 6d 62 65 72 20 28 4e   Not a Number (N
b16e0 61 4e 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  aN)..*/.SQLITE_P
b16f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
b1700 65 33 49 73 4e 61 4e 28 64 6f 75 62 6c 65 20 78  e3IsNaN(double x
b1710 29 7b 0a 20 20 2f 2a 20 54 68 69 73 20 4e 61 4e  ){.  /* This NaN
b1720 20 74 65 73 74 20 73 6f 6d 65 74 69 6d 65 73 20   test sometimes 
b1730 66 61 69 6c 73 20 69 66 20 63 6f 6d 70 69 6c 65  fails if compile
b1740 64 20 6f 6e 20 47 43 43 20 77 69 74 68 20 2d 66  d on GCC with -f
b1750 66 61 73 74 2d 6d 61 74 68 2e 0a 20 20 2a 2a 20  fast-math..  ** 
b1760 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e  On the other han
b1770 64 2c 20 74 68 65 20 75 73 65 20 6f 66 20 2d 66  d, the use of -f
b1780 66 61 73 74 2d 6d 61 74 68 20 63 6f 6d 65 73 20  fast-math comes 
b1790 77 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69  with the followi
b17a0 6e 67 0a 20 20 2a 2a 20 77 61 72 6e 69 6e 67 3a  ng.  ** warning:
b17b0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20  .  **.  **      
b17c0 54 68 69 73 20 6f 70 74 69 6f 6e 20 5b 2d 66 66  This option [-ff
b17d0 61 73 74 2d 6d 61 74 68 5d 20 73 68 6f 75 6c 64  ast-math] should
b17e0 20 6e 65 76 65 72 20 62 65 20 74 75 72 6e 65 64   never be turned
b17f0 20 6f 6e 20 62 79 20 61 6e 79 0a 20 20 2a 2a 20   on by any.  ** 
b1800 20 20 20 20 20 2d 4f 20 6f 70 74 69 6f 6e 20 73       -O option s
b1810 69 6e 63 65 20 69 74 20 63 61 6e 20 72 65 73 75  ince it can resu
b1820 6c 74 20 69 6e 20 69 6e 63 6f 72 72 65 63 74 20  lt in incorrect 
b1830 6f 75 74 70 75 74 20 66 6f 72 20 70 72 6f 67 72  output for progr
b1840 61 6d 73 0a 20 20 2a 2a 20 20 20 20 20 20 77 68  ams.  **      wh
b1850 69 63 68 20 64 65 70 65 6e 64 20 6f 6e 20 61 6e  ich depend on an
b1860 20 65 78 61 63 74 20 69 6d 70 6c 65 6d 65 6e 74   exact implement
b1870 61 74 69 6f 6e 20 6f 66 20 49 45 45 45 20 6f 72  ation of IEEE or
b1880 20 49 53 4f 20 0a 20 20 2a 2a 20 20 20 20 20 20   ISO .  **      
b1890 72 75 6c 65 73 2f 73 70 65 63 69 66 69 63 61 74  rules/specificat
b18a0 69 6f 6e 73 20 66 6f 72 20 6d 61 74 68 20 66 75  ions for math fu
b18b0 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20  nctions..  **.  
b18c0 2a 2a 20 55 6e 64 65 72 20 4d 53 56 43 2c 20 74  ** Under MSVC, t
b18d0 68 69 73 20 4e 61 4e 20 74 65 73 74 20 6d 61 79  his NaN test may
b18e0 20 66 61 69 6c 20 69 66 20 63 6f 6d 70 69 6c 65   fail if compile
b18f0 64 20 77 69 74 68 20 61 20 66 6c 6f 61 74 69 6e  d with a floatin
b1900 67 2d 0a 20 20 2a 2a 20 70 6f 69 6e 74 20 70 72  g-.  ** point pr
b1910 65 63 69 73 69 6f 6e 20 6d 6f 64 65 20 6f 74 68  ecision mode oth
b1920 65 72 20 74 68 61 6e 20 2f 66 70 3a 70 72 65 63  er than /fp:prec
b1930 69 73 65 2e 20 20 46 72 6f 6d 20 74 68 65 20 4d  ise.  From the M
b1940 53 44 4e 20 0a 20 20 2a 2a 20 64 6f 63 75 6d 65  SDN .  ** docume
b1950 6e 74 61 74 69 6f 6e 3a 0a 20 20 2a 2a 0a 20 20  ntation:.  **.  
b1960 2a 2a 20 20 20 20 20 20 54 68 65 20 63 6f 6d 70  **      The comp
b1970 69 6c 65 72 20 5b 77 69 74 68 20 2f 66 70 3a 70  iler [with /fp:p
b1980 72 65 63 69 73 65 5d 20 77 69 6c 6c 20 70 72 6f  recise] will pro
b1990 70 65 72 6c 79 20 68 61 6e 64 6c 65 20 63 6f 6d  perly handle com
b19a0 70 61 72 69 73 6f 6e 73 20 0a 20 20 2a 2a 20 20  parisons .  **  
b19b0 20 20 20 20 69 6e 76 6f 6c 76 69 6e 67 20 4e 61      involving Na
b19c0 4e 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  N. For example, 
b19d0 78 20 21 3d 20 78 20 65 76 61 6c 75 61 74 65 73  x != x evaluates
b19e0 20 74 6f 20 74 72 75 65 20 69 66 20 78 20 69 73   to true if x is
b19f0 20 4e 61 4e 20 0a 20 20 2a 2a 20 20 20 20 20 20   NaN .  **      
b1a00 2e 2e 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20  ....  */.#ifdef 
b1a10 5f 5f 46 41 53 54 5f 4d 41 54 48 5f 5f 0a 23 20  __FAST_MATH__.# 
b1a20 65 72 72 6f 72 20 53 51 4c 69 74 65 20 77 69 6c  error SQLite wil
b1a30 6c 20 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65  l not work corre
b1a40 63 74 6c 79 20 77 69 74 68 20 74 68 65 20 2d 66  ctly with the -f
b1a50 66 61 73 74 2d 6d 61 74 68 20 6f 70 74 69 6f 6e  fast-math option
b1a60 20 6f 66 20 47 43 43 2e 0a 23 65 6e 64 69 66 0a   of GCC..#endif.
b1a70 20 20 76 6f 6c 61 74 69 6c 65 20 64 6f 75 62 6c    volatile doubl
b1a80 65 20 79 20 3d 20 78 3b 0a 20 20 76 6f 6c 61 74  e y = x;.  volat
b1a90 69 6c 65 20 64 6f 75 62 6c 65 20 7a 20 3d 20 79  ile double z = y
b1aa0 3b 0a 20 20 72 65 74 75 72 6e 20 79 21 3d 7a 3b  ;.  return y!=z;
b1ab0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
b1ac0 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 61   the length of a
b1ad0 20 73 74 72 69 6e 67 2c 20 65 78 63 65 70 74 20   string, except 
b1ae0 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 74 68 65  do not allow the
b1af0 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 0a 2a   string length.*
b1b00 2a 20 74 6f 20 65 78 63 65 65 64 20 74 68 65 20  * to exceed the 
b1b10 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e  SQLITE_LIMIT_LEN
b1b20 47 54 48 20 73 65 74 74 69 6e 67 2e 0a 2a 2f 0a  GTH setting..*/.
b1b30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
b1b40 6e 74 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  nt sqlite3Strlen
b1b50 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
b1b60 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20  nst char *z){.  
b1b70 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 20 3d  const char *z2 =
b1b80 20 7a 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20   z;.  int len;. 
b1b90 20 73 69 7a 65 5f 74 20 78 3b 0a 20 20 77 68 69   size_t x;.  whi
b1ba0 6c 65 28 20 2a 7a 32 20 29 7b 20 7a 32 2b 2b 3b  le( *z2 ){ z2++;
b1bb0 20 7d 0a 20 20 78 20 3d 20 7a 32 20 2d 20 7a 3b   }.  x = z2 - z;
b1bc0 0a 20 20 6c 65 6e 20 3d 20 30 78 37 66 66 66 66  .  len = 0x7ffff
b1bd0 66 66 66 20 26 20 78 3b 0a 20 20 69 66 28 20 6c  fff & x;.  if( l
b1be0 65 6e 21 3d 78 20 7c 7c 20 6c 65 6e 20 3e 20 64  en!=x || len > d
b1bf0 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
b1c00 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29  _LIMIT_LENGTH] )
b1c10 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 64 62 2d  {.    return db-
b1c20 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
b1c30 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20  IMIT_LENGTH];.  
b1c40 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
b1c50 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  n len;.  }.}../*
b1c60 0a 2a 2a 20 53 65 74 20 74 68 65 20 6d 6f 73 74  .** Set the most
b1c70 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 63 6f   recent error co
b1c80 64 65 20 61 6e 64 20 65 72 72 6f 72 20 73 74 72  de and error str
b1c90 69 6e 67 20 66 6f 72 20 74 68 65 20 73 71 6c 69  ing for the sqli
b1ca0 74 65 0a 2a 2a 20 68 61 6e 64 6c 65 20 22 64 62  te.** handle "db
b1cb0 22 2e 20 54 68 65 20 65 72 72 6f 72 20 63 6f 64  ". The error cod
b1cc0 65 20 69 73 20 73 65 74 20 74 6f 20 22 65 72 72  e is set to "err
b1cd0 5f 63 6f 64 65 22 2e 0a 2a 2a 0a 2a 2a 20 49 66  _code"..**.** If
b1ce0 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c   it is not NULL,
b1cf0 20 73 74 72 69 6e 67 20 7a 46 6f 72 6d 61 74 20   string zFormat 
b1d00 73 70 65 63 69 66 69 65 73 20 74 68 65 20 66 6f  specifies the fo
b1d10 72 6d 61 74 20 6f 66 20 74 68 65 0a 2a 2a 20 65  rmat of the.** e
b1d20 72 72 6f 72 20 73 74 72 69 6e 67 20 69 6e 20 74  rror string in t
b1d30 68 65 20 73 74 79 6c 65 20 6f 66 20 74 68 65 20  he style of the 
b1d40 70 72 69 6e 74 66 20 66 75 6e 63 74 69 6f 6e 73  printf functions
b1d50 3a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  : The following.
b1d60 2a 2a 20 66 6f 72 6d 61 74 20 63 68 61 72 61 63  ** format charac
b1d70 74 65 72 73 20 61 72 65 20 61 6c 6c 6f 77 65 64  ters are allowed
b1d80 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 25 73 20  :.**.**      %s 
b1d90 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 73 74       Insert a st
b1da0 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20 25 7a 20  ring.**      %z 
b1db0 20 20 20 20 20 41 20 73 74 72 69 6e 67 20 74 68       A string th
b1dc0 61 74 20 73 68 6f 75 6c 64 20 62 65 20 66 72 65  at should be fre
b1dd0 65 64 20 61 66 74 65 72 20 75 73 65 0a 2a 2a 20  ed after use.** 
b1de0 20 20 20 20 20 25 64 20 20 20 20 20 20 49 6e 73       %d      Ins
b1df0 65 72 74 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a  ert an integer.*
b1e00 2a 20 20 20 20 20 20 25 54 20 20 20 20 20 20 49  *      %T      I
b1e10 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e 0a 2a 2a  nsert a token.**
b1e20 20 20 20 20 20 20 25 53 20 20 20 20 20 20 49 6e        %S      In
b1e30 73 65 72 74 20 74 68 65 20 66 69 72 73 74 20 65  sert the first e
b1e40 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53 72 63 4c  lement of a SrcL
b1e50 69 73 74 0a 2a 2a 0a 2a 2a 20 7a 46 6f 72 6d 61  ist.**.** zForma
b1e60 74 20 61 6e 64 20 61 6e 79 20 73 74 72 69 6e 67  t and any string
b1e70 20 74 6f 6b 65 6e 73 20 74 68 61 74 20 66 6f 6c   tokens that fol
b1e80 6c 6f 77 20 69 74 20 61 72 65 20 61 73 73 75 6d  low it are assum
b1e90 65 64 20 74 6f 20 62 65 0a 2a 2a 20 65 6e 63 6f  ed to be.** enco
b1ea0 64 65 64 20 69 6e 20 55 54 46 2d 38 2e 0a 2a 2a  ded in UTF-8..**
b1eb0 0a 2a 2a 20 54 6f 20 63 6c 65 61 72 20 74 68 65  .** To clear the
b1ec0 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72   most recent err
b1ed0 6f 72 20 66 6f 72 20 73 71 6c 69 74 65 20 68 61  or for sqlite ha
b1ee0 6e 64 6c 65 20 22 64 62 22 2c 20 73 71 6c 69 74  ndle "db", sqlit
b1ef0 65 33 45 72 72 6f 72 0a 2a 2a 20 73 68 6f 75 6c  e3Error.** shoul
b1f00 64 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74 68  d be called with
b1f10 20 65 72 72 5f 63 6f 64 65 20 73 65 74 20 74 6f   err_code set to
b1f20 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 7a   SQLITE_OK and z
b1f30 46 6f 72 6d 61 74 20 73 65 74 0a 2a 2a 20 74 6f  Format set.** to
b1f40 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45   NULL..*/.SQLITE
b1f50 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
b1f60 6c 69 74 65 33 45 72 72 6f 72 28 73 71 6c 69 74  lite3Error(sqlit
b1f70 65 33 20 2a 64 62 2c 20 69 6e 74 20 65 72 72 5f  e3 *db, int err_
b1f80 63 6f 64 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  code, const char
b1f90 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b   *zFormat, ...){
b1fa0 0a 20 20 69 66 28 20 64 62 20 26 26 20 28 64 62  .  if( db && (db
b1fb0 2d 3e 70 45 72 72 20 7c 7c 20 28 64 62 2d 3e 70  ->pErr || (db->p
b1fc0 45 72 72 20 3d 20 73 71 6c 69 74 65 33 56 61 6c  Err = sqlite3Val
b1fd0 75 65 4e 65 77 28 64 62 29 29 21 3d 30 29 20 29  ueNew(db))!=0) )
b1fe0 7b 0a 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64  {.    db->errCod
b1ff0 65 20 3d 20 65 72 72 5f 63 6f 64 65 3b 0a 20 20  e = err_code;.  
b2000 20 20 69 66 28 20 7a 46 6f 72 6d 61 74 20 29 7b    if( zFormat ){
b2010 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 3b 0a  .      char *z;.
b2020 20 20 20 20 20 20 76 61 5f 6c 69 73 74 20 61 70        va_list ap
b2030 3b 0a 20 20 20 20 20 20 76 61 5f 73 74 61 72 74  ;.      va_start
b2040 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20  (ap, zFormat);. 
b2050 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33       z = sqlite3
b2060 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f  VMPrintf(db, zFo
b2070 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 20 20 20  rmat, ap);.     
b2080 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 20   va_end(ap);.   
b2090 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53     sqlite3ValueS
b20a0 65 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 20  etStr(db->pErr, 
b20b0 2d 31 2c 20 7a 2c 20 53 51 4c 49 54 45 5f 55 54  -1, z, SQLITE_UT
b20c0 46 38 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d  F8, SQLITE_DYNAM
b20d0 49 43 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  IC);.    }else{.
b20e0 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c        sqlite3Val
b20f0 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72  ueSetStr(db->pEr
b2100 72 2c 20 30 2c 20 30 2c 20 53 51 4c 49 54 45 5f  r, 0, 0, SQLITE_
b2110 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41  UTF8, SQLITE_STA
b2120 54 49 43 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  TIC);.    }.  }.
b2130 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20  }../*.** Add an 
b2140 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f  error message to
b2150 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
b2160 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 20 70   and increment p
b2170 50 61 72 73 65 2d 3e 6e 45 72 72 2e 0a 2a 2a 20  Parse->nErr..** 
b2180 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f  The following fo
b2190 72 6d 61 74 74 69 6e 67 20 63 68 61 72 61 63 74  rmatting charact
b21a0 65 72 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 3a  ers are allowed:
b21b0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 25 73 20 20  .**.**      %s  
b21c0 20 20 20 20 49 6e 73 65 72 74 20 61 20 73 74 72      Insert a str
b21d0 69 6e 67 0a 2a 2a 20 20 20 20 20 20 25 7a 20 20  ing.**      %z  
b21e0 20 20 20 20 41 20 73 74 72 69 6e 67 20 74 68 61      A string tha
b21f0 74 20 73 68 6f 75 6c 64 20 62 65 20 66 72 65 65  t should be free
b2200 64 20 61 66 74 65 72 20 75 73 65 0a 2a 2a 20 20  d after use.**  
b2210 20 20 20 20 25 64 20 20 20 20 20 20 49 6e 73 65      %d      Inse
b2220 72 74 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a  rt an integer.**
b2230 20 20 20 20 20 20 25 54 20 20 20 20 20 20 49 6e        %T      In
b2240 73 65 72 74 20 61 20 74 6f 6b 65 6e 0a 2a 2a 20  sert a token.** 
b2250 20 20 20 20 20 25 53 20 20 20 20 20 20 49 6e 73       %S      Ins
b2260 65 72 74 20 74 68 65 20 66 69 72 73 74 20 65 6c  ert the first el
b2270 65 6d 65 6e 74 20 6f 66 20 61 20 53 72 63 4c 69  ement of a SrcLi
b2280 73 74 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  st.**.** This fu
b2290 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65  nction should be
b22a0 20 75 73 65 64 20 74 6f 20 72 65 70 6f 72 74 20   used to report 
b22b0 61 6e 79 20 65 72 72 6f 72 20 74 68 61 74 20 6f  any error that o
b22c0 63 63 75 72 73 20 77 68 69 6c 73 74 0a 2a 2a 20  ccurs whilst.** 
b22d0 63 6f 6d 70 69 6c 69 6e 67 20 61 6e 20 53 51 4c  compiling an SQL
b22e0 20 73 74 61 74 65 6d 65 6e 74 20 28 69 2e 65 2e   statement (i.e.
b22f0 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65 33 5f   within sqlite3_
b2300 70 72 65 70 61 72 65 28 29 29 2e 20 54 68 65 0a  prepare()). The.
b2310 2a 2a 20 6c 61 73 74 20 74 68 69 6e 67 20 74 68  ** last thing th
b2320 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  e sqlite3_prepar
b2330 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65  e() function doe
b2340 73 20 69 73 20 63 6f 70 79 20 74 68 65 20 65 72  s is copy the er
b2350 72 6f 72 0a 2a 2a 20 73 74 6f 72 65 64 20 62 79  ror.** stored by
b2360 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
b2370 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65  nto the database
b2380 20 68 61 6e 64 6c 65 20 75 73 69 6e 67 20 73 71   handle using sq
b2390 6c 69 74 65 33 45 72 72 6f 72 28 29 2e 0a 2a 2a  lite3Error()..**
b23a0 20 46 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65   Function sqlite
b23b0 33 45 72 72 6f 72 28 29 20 73 68 6f 75 6c 64 20  3Error() should 
b23c0 62 65 20 75 73 65 64 20 64 75 72 69 6e 67 20 73  be used during s
b23d0 74 61 74 65 6d 65 6e 74 20 65 78 65 63 75 74 69  tatement executi
b23e0 6f 6e 0a 2a 2a 20 28 73 71 6c 69 74 65 33 5f 73  on.** (sqlite3_s
b23f0 74 65 70 28 29 20 65 74 63 2e 29 2e 0a 2a 2f 0a  tep() etc.)..*/.
b2400 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
b2410 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72  oid sqlite3Error
b2420 4d 73 67 28 50 61 72 73 65 20 2a 70 50 61 72 73  Msg(Parse *pPars
b2430 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  e, const char *z
b2440 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20  Format, ...){.  
b2450 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 73 71  va_list ap;.  sq
b2460 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
b2470 73 65 2d 3e 64 62 3b 0a 20 20 70 50 61 72 73 65  se->db;.  pParse
b2480 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 73 71 6c 69  ->nErr++;.  sqli
b2490 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 50  te3DbFree(db, pP
b24a0 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a  arse->zErrMsg);.
b24b0 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a    va_start(ap, z
b24c0 46 6f 72 6d 61 74 29 3b 0a 20 20 70 50 61 72 73  Format);.  pPars
b24d0 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c  e->zErrMsg = sql
b24e0 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c  ite3VMPrintf(db,
b24f0 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20   zFormat, ap);. 
b2500 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 69   va_end(ap);.  i
b2510 66 28 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53  f( pParse->rc==S
b2520 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
b2530 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c  pParse->rc = SQL
b2540 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 7d  ITE_ERROR;.  }.}
b2550 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68  ../*.** Clear th
b2560 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  e error message 
b2570 69 6e 20 70 50 61 72 73 65 2c 20 69 66 20 61 6e  in pParse, if an
b2580 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  y.*/.SQLITE_PRIV
b2590 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
b25a0 45 72 72 6f 72 43 6c 65 61 72 28 50 61 72 73 65  ErrorClear(Parse
b25b0 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c   *pParse){.  sql
b25c0 69 74 65 33 44 62 46 72 65 65 28 70 50 61 72 73  ite3DbFree(pPars
b25d0 65 2d 3e 64 62 2c 20 70 50 61 72 73 65 2d 3e 7a  e->db, pParse->z
b25e0 45 72 72 4d 73 67 29 3b 0a 20 20 70 50 61 72 73  ErrMsg);.  pPars
b25f0 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a  e->zErrMsg = 0;.
b2600 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 3d    pParse->nErr =
b2610 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e   0;.}../*.** Con
b2620 76 65 72 74 20 61 6e 20 53 51 4c 2d 73 74 79 6c  vert an SQL-styl
b2630 65 20 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20  e quoted string 
b2640 69 6e 74 6f 20 61 20 6e 6f 72 6d 61 6c 20 73 74  into a normal st
b2650 72 69 6e 67 20 62 79 20 72 65 6d 6f 76 69 6e 67  ring by removing
b2660 0a 2a 2a 20 74 68 65 20 71 75 6f 74 65 20 63 68  .** the quote ch
b2670 61 72 61 63 74 65 72 73 2e 20 20 54 68 65 20 63  aracters.  The c
b2680 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 64 6f 6e  onversion is don
b2690 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20 49 66 20  e in-place.  If 
b26a0 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 64 6f 65  the.** input doe
b26b0 73 20 6e 6f 74 20 62 65 67 69 6e 20 77 69 74 68  s not begin with
b26c0 20 61 20 71 75 6f 74 65 20 63 68 61 72 61 63 74   a quote charact
b26d0 65 72 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f  er, then this ro
b26e0 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f  utine.** is a no
b26f0 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 32 2d  -op..**.** 2002-
b2700 46 65 62 2d 31 34 3a 20 54 68 69 73 20 72 6f 75  Feb-14: This rou
b2710 74 69 6e 65 20 69 73 20 65 78 74 65 6e 64 65 64  tine is extended
b2720 20 74 6f 20 72 65 6d 6f 76 65 20 4d 53 2d 41 63   to remove MS-Ac
b2730 63 65 73 73 20 73 74 79 6c 65 0a 2a 2a 20 62 72  cess style.** br
b2740 61 63 6b 65 74 73 20 66 72 6f 6d 20 61 72 6f 75  ackets from arou
b2750 6e 64 20 69 64 65 6e 74 69 66 65 72 73 2e 20 20  nd identifers.  
b2760 46 6f 72 20 65 78 61 6d 70 6c 65 3a 20 20 22 5b  For example:  "[
b2770 61 2d 62 2d 63 5d 22 20 62 65 63 6f 6d 65 73 0a  a-b-c]" becomes.
b2780 2a 2a 20 22 61 2d 62 2d 63 22 2e 0a 2a 2f 0a 53  ** "a-b-c"..*/.S
b2790 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
b27a0 69 64 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74  id sqlite3Dequot
b27b0 65 28 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e  e(char *z){.  in
b27c0 74 20 71 75 6f 74 65 3b 0a 20 20 69 6e 74 20 69  t quote;.  int i
b27d0 2c 20 6a 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20  , j;.  if( z==0 
b27e0 29 20 72 65 74 75 72 6e 3b 0a 20 20 71 75 6f 74  ) return;.  quot
b27f0 65 20 3d 20 7a 5b 30 5d 3b 0a 20 20 73 77 69 74  e = z[0];.  swit
b2800 63 68 28 20 71 75 6f 74 65 20 29 7b 0a 20 20 20  ch( quote ){.   
b2810 20 63 61 73 65 20 27 5c 27 27 3a 20 20 62 72 65   case '\'':  bre
b2820 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 27 22 27  ak;.    case '"'
b2830 3a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63  :   break;.    c
b2840 61 73 65 20 27 60 27 3a 20 20 20 62 72 65 61 6b  ase '`':   break
b2850 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
b2860 20 2f 2a 20 46 6f 72 20 4d 79 53 51 4c 20 63 6f   /* For MySQL co
b2870 6d 70 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a 20  mpatibility */. 
b2880 20 20 20 63 61 73 65 20 27 5b 27 3a 20 20 20 71     case '[':   q
b2890 75 6f 74 65 20 3d 20 27 5d 27 3b 20 20 62 72 65  uote = ']';  bre
b28a0 61 6b 3b 20 20 2f 2a 20 46 6f 72 20 4d 53 20 53  ak;  /* For MS S
b28b0 71 6c 53 65 72 76 65 72 20 63 6f 6d 70 61 74 69  qlServer compati
b28c0 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 64 65  bility */.    de
b28d0 66 61 75 6c 74 3a 20 20 20 20 72 65 74 75 72 6e  fault:    return
b28e0 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 2c  ;.  }.  for(i=1,
b28f0 20 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29   j=0; z[i]; i++)
b2900 7b 0a 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d  {.    if( z[i]==
b2910 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 20 20 69  quote ){.      i
b2920 66 28 20 7a 5b 69 2b 31 5d 3d 3d 71 75 6f 74 65  f( z[i+1]==quote
b2930 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b   ){.        z[j+
b2940 2b 5d 20 3d 20 71 75 6f 74 65 3b 0a 20 20 20 20  +] = quote;.    
b2950 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 7d      i++;.      }
b2960 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 5b  else{.        z[
b2970 6a 2b 2b 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20  j++] = 0;.      
b2980 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
b2990 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
b29a0 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b    z[j++] = z[i];
b29b0 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
b29c0 20 43 6f 6e 76 65 6e 69 65 6e 74 20 73 68 6f 72   Convenient shor
b29d0 74 2d 68 61 6e 64 20 2a 2f 0a 23 64 65 66 69 6e  t-hand */.#defin
b29e0 65 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 20 73  e UpperToLower s
b29f0 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77  qlite3UpperToLow
b2a00 65 72 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 73  er../*.** Some s
b2a10 79 73 74 65 6d 73 20 68 61 76 65 20 73 74 72 69  ystems have stri
b2a20 63 6d 70 28 29 2e 20 20 4f 74 68 65 72 73 20 68  cmp().  Others h
b2a30 61 76 65 20 73 74 72 63 61 73 65 63 6d 70 28 29  ave strcasecmp()
b2a40 2e 20 20 42 65 63 61 75 73 65 0a 2a 2a 20 74 68  .  Because.** th
b2a50 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 69 73  ere is no consis
b2a60 74 65 6e 63 79 2c 20 77 65 20 77 69 6c 6c 20 64  tency, we will d
b2a70 65 66 69 6e 65 20 6f 75 72 20 6f 77 6e 2e 0a 2a  efine our own..*
b2a80 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b2a90 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 49   int sqlite3StrI
b2aa0 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  Cmp(const char *
b2ab0 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63 68 61  zLeft, const cha
b2ac0 72 20 2a 7a 52 69 67 68 74 29 7b 0a 20 20 72 65  r *zRight){.  re
b2ad0 67 69 73 74 65 72 20 75 6e 73 69 67 6e 65 64 20  gister unsigned 
b2ae0 63 68 61 72 20 2a 61 2c 20 2a 62 3b 0a 20 20 61  char *a, *b;.  a
b2af0 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
b2b00 72 20 2a 29 7a 4c 65 66 74 3b 0a 20 20 62 20 3d  r *)zLeft;.  b =
b2b10 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
b2b20 2a 29 7a 52 69 67 68 74 3b 0a 20 20 77 68 69 6c  *)zRight;.  whil
b2b30 65 28 20 2a 61 21 3d 30 20 26 26 20 55 70 70 65  e( *a!=0 && Uppe
b2b40 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 3d 3d 55 70  rToLower[*a]==Up
b2b50 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 29 7b  perToLower[*b]){
b2b60 20 61 2b 2b 3b 20 62 2b 2b 3b 20 7d 0a 20 20 72   a++; b++; }.  r
b2b70 65 74 75 72 6e 20 55 70 70 65 72 54 6f 4c 6f 77  eturn UpperToLow
b2b80 65 72 5b 2a 61 5d 20 2d 20 55 70 70 65 72 54 6f  er[*a] - UpperTo
b2b90 4c 6f 77 65 72 5b 2a 62 5d 3b 0a 7d 0a 53 51 4c  Lower[*b];.}.SQL
b2ba0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b2bb0 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
b2bc0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66  const char *zLef
b2bd0 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  t, const char *z
b2be0 52 69 67 68 74 2c 20 69 6e 74 20 4e 29 7b 0a 20  Right, int N){. 
b2bf0 20 72 65 67 69 73 74 65 72 20 75 6e 73 69 67 6e   register unsign
b2c00 65 64 20 63 68 61 72 20 2a 61 2c 20 2a 62 3b 0a  ed char *a, *b;.
b2c10 20 20 61 20 3d 20 28 75 6e 73 69 67 6e 65 64 20    a = (unsigned 
b2c20 63 68 61 72 20 2a 29 7a 4c 65 66 74 3b 0a 20 20  char *)zLeft;.  
b2c30 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  b = (unsigned ch
b2c40 61 72 20 2a 29 7a 52 69 67 68 74 3b 0a 20 20 77  ar *)zRight;.  w
b2c50 68 69 6c 65 28 20 4e 2d 2d 20 3e 20 30 20 26 26  hile( N-- > 0 &&
b2c60 20 2a 61 21 3d 30 20 26 26 20 55 70 70 65 72 54   *a!=0 && UpperT
b2c70 6f 4c 6f 77 65 72 5b 2a 61 5d 3d 3d 55 70 70 65  oLower[*a]==Uppe
b2c80 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 29 7b 20 61  rToLower[*b]){ a
b2c90 2b 2b 3b 20 62 2b 2b 3b 20 7d 0a 20 20 72 65 74  ++; b++; }.  ret
b2ca0 75 72 6e 20 4e 3c 30 20 3f 20 30 20 3a 20 55 70  urn N<0 ? 0 : Up
b2cb0 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 20 2d  perToLower[*a] -
b2cc0 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62   UpperToLower[*b
b2cd0 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ];.}../*.** Retu
b2ce0 72 6e 20 54 52 55 45 20 69 66 20 7a 20 69 73 20  rn TRUE if z is 
b2cf0 61 20 70 75 72 65 20 6e 75 6d 65 72 69 63 20 73  a pure numeric s
b2d00 74 72 69 6e 67 2e 20 20 52 65 74 75 72 6e 20 46  tring.  Return F
b2d10 41 4c 53 45 20 69 66 20 74 68 65 0a 2a 2a 20 73  ALSE if the.** s
b2d20 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 73 20 61  tring contains a
b2d30 6e 79 20 63 68 61 72 61 63 74 65 72 20 77 68 69  ny character whi
b2d40 63 68 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f  ch is not part o
b2d50 66 20 61 20 6e 75 6d 62 65 72 2e 20 49 66 0a 2a  f a number. If.*
b2d60 2a 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20  * the string is 
b2d70 6e 75 6d 65 72 69 63 20 61 6e 64 20 63 6f 6e 74  numeric and cont
b2d80 61 69 6e 73 20 74 68 65 20 27 2e 27 20 63 68 61  ains the '.' cha
b2d90 72 61 63 74 65 72 2c 20 73 65 74 20 2a 72 65 61  racter, set *rea
b2da0 6c 6e 75 6d 0a 2a 2a 20 74 6f 20 54 52 55 45 20  lnum.** to TRUE 
b2db0 28 6f 74 68 65 72 77 69 73 65 20 46 41 4c 53 45  (otherwise FALSE
b2dc0 29 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 6d 70 74  )..**.** An empt
b2dd0 79 20 73 74 72 69 6e 67 20 69 73 20 63 6f 6e 73  y string is cons
b2de0 69 64 65 72 65 64 20 6e 6f 6e 2d 6e 75 6d 65 72  idered non-numer
b2df0 69 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ic..*/.SQLITE_PR
b2e00 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b2e10 33 49 73 4e 75 6d 62 65 72 28 63 6f 6e 73 74 20  3IsNumber(const 
b2e20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 72 65  char *z, int *re
b2e30 61 6c 6e 75 6d 2c 20 75 38 20 65 6e 63 29 7b 0a  alnum, u8 enc){.
b2e40 20 20 69 6e 74 20 69 6e 63 72 20 3d 20 28 65 6e    int incr = (en
b2e50 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31  c==SQLITE_UTF8?1
b2e60 3a 32 29 3b 0a 20 20 69 66 28 20 65 6e 63 3d 3d  :2);.  if( enc==
b2e70 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29  SQLITE_UTF16BE )
b2e80 20 7a 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 3d 3d   z++;.  if( *z==
b2e90 27 2d 27 20 7c 7c 20 2a 7a 3d 3d 27 2b 27 20 29  '-' || *z=='+' )
b2ea0 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 69 66   z += incr;.  if
b2eb0 28 20 21 69 73 64 69 67 69 74 28 2a 28 75 38 2a  ( !isdigit(*(u8*
b2ec0 29 7a 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  )z) ){.    retur
b2ed0 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 20 2b 3d 20  n 0;.  }.  z += 
b2ee0 69 6e 63 72 3b 0a 20 20 69 66 28 20 72 65 61 6c  incr;.  if( real
b2ef0 6e 75 6d 20 29 20 2a 72 65 61 6c 6e 75 6d 20 3d  num ) *realnum =
b2f00 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 64   0;.  while( isd
b2f10 69 67 69 74 28 2a 28 75 38 2a 29 7a 29 20 29 7b  igit(*(u8*)z) ){
b2f20 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20   z += incr; }.  
b2f30 69 66 28 20 2a 7a 3d 3d 27 2e 27 20 29 7b 0a 20  if( *z=='.' ){. 
b2f40 20 20 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20     z += incr;.  
b2f50 20 20 69 66 28 20 21 69 73 64 69 67 69 74 28 2a    if( !isdigit(*
b2f60 28 75 38 2a 29 7a 29 20 29 20 72 65 74 75 72 6e  (u8*)z) ) return
b2f70 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69   0;.    while( i
b2f80 73 64 69 67 69 74 28 2a 28 75 38 2a 29 7a 29 20  sdigit(*(u8*)z) 
b2f90 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a  ){ z += incr; }.
b2fa0 20 20 20 20 69 66 28 20 72 65 61 6c 6e 75 6d 20      if( realnum 
b2fb0 29 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 31 3b 0a  ) *realnum = 1;.
b2fc0 20 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 65    }.  if( *z=='e
b2fd0 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27 20 29 7b 0a  ' || *z=='E' ){.
b2fe0 20 20 20 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20      z += incr;. 
b2ff0 20 20 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 7c     if( *z=='+' |
b3000 7c 20 2a 7a 3d 3d 27 2d 27 20 29 20 7a 20 2b 3d  | *z=='-' ) z +=
b3010 20 69 6e 63 72 3b 0a 20 20 20 20 69 66 28 20 21   incr;.    if( !
b3020 69 73 64 69 67 69 74 28 2a 28 75 38 2a 29 7a 29  isdigit(*(u8*)z)
b3030 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
b3040 20 77 68 69 6c 65 28 20 69 73 64 69 67 69 74 28   while( isdigit(
b3050 2a 28 75 38 2a 29 7a 29 20 29 7b 20 7a 20 2b 3d  *(u8*)z) ){ z +=
b3060 20 69 6e 63 72 3b 20 7d 0a 20 20 20 20 69 66 28   incr; }.    if(
b3070 20 72 65 61 6c 6e 75 6d 20 29 20 2a 72 65 61 6c   realnum ) *real
b3080 6e 75 6d 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72  num = 1;.  }.  r
b3090 65 74 75 72 6e 20 2a 7a 3d 3d 30 3b 0a 7d 0a 0a  eturn *z==0;.}..
b30a0 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67  /*.** The string
b30b0 20 7a 5b 5d 20 69 73 20 61 6e 20 61 73 63 69 69   z[] is an ascii
b30c0 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
b30d0 6f 66 20 61 20 72 65 61 6c 20 6e 75 6d 62 65 72  of a real number
b30e0 2e 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 69  ..** Convert thi
b30f0 73 20 73 74 72 69 6e 67 20 74 6f 20 61 20 64 6f  s string to a do
b3100 75 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  uble..**.** This
b3110 20 72 6f 75 74 69 6e 65 20 61 73 73 75 6d 65 73   routine assumes
b3120 20 74 68 61 74 20 7a 5b 5d 20 72 65 61 6c 6c 79   that z[] really
b3130 20 69 73 20 61 20 76 61 6c 69 64 20 6e 75 6d 62   is a valid numb
b3140 65 72 2e 20 20 49 66 20 69 74 0a 2a 2a 20 69 73  er.  If it.** is
b3150 20 6e 6f 74 2c 20 74 68 65 20 72 65 73 75 6c 74   not, the result
b3160 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a   is undefined..*
b3170 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
b3180 65 20 69 73 20 75 73 65 64 20 69 6e 73 74 65 61  e is used instea
b3190 64 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79  d of the library
b31a0 20 61 74 6f 66 28 29 20 66 75 6e 63 74 69 6f 6e   atof() function
b31b0 20 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 65 20   because.** the 
b31c0 6c 69 62 72 61 72 79 20 61 74 6f 66 28 29 20 6d  library atof() m
b31d0 69 67 68 74 20 77 61 6e 74 20 74 6f 20 75 73 65  ight want to use
b31e0 20 22 2c 22 20 61 73 20 74 68 65 20 64 65 63 69   "," as the deci
b31f0 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 73 74 65 61  mal point instea
b3200 64 0a 2a 2a 20 6f 66 20 22 2e 22 20 64 65 70 65  d.** of "." depe
b3210 6e 64 69 6e 67 20 6f 6e 20 68 6f 77 20 6c 6f 63  nding on how loc
b3220 61 6c 65 20 69 73 20 73 65 74 2e 20 20 42 75 74  ale is set.  But
b3230 20 74 68 61 74 20 77 6f 75 6c 64 20 63 61 75 73   that would caus
b3240 65 20 70 72 6f 62 6c 65 6d 73 0a 2a 2a 20 66 6f  e problems.** fo
b3250 72 20 53 51 4c 2e 20 20 53 6f 20 74 68 69 73 20  r SQL.  So this 
b3260 72 6f 75 74 69 6e 65 20 61 6c 77 61 79 73 20 75  routine always u
b3270 73 65 73 20 22 2e 22 20 72 65 67 61 72 64 6c 65  ses "." regardle
b3280 73 73 20 6f 66 20 6c 6f 63 61 6c 65 2e 0a 2a 2f  ss of locale..*/
b3290 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b32a0 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 46 28  int sqlite3AtoF(
b32b0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 64  const char *z, d
b32c0 6f 75 62 6c 65 20 2a 70 52 65 73 75 6c 74 29 7b  ouble *pResult){
b32d0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
b32e0 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
b32f0 49 4e 54 0a 20 20 69 6e 74 20 73 69 67 6e 20 3d  INT.  int sign =
b3300 20 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   1;.  const char
b3310 20 2a 7a 42 65 67 69 6e 20 3d 20 7a 3b 0a 20 20   *zBegin = z;.  
b3320 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20  LONGDOUBLE_TYPE 
b3330 76 31 20 3d 20 30 2e 30 3b 0a 20 20 69 6e 74 20  v1 = 0.0;.  int 
b3340 6e 53 69 67 6e 69 66 69 63 61 6e 74 20 3d 20 30  nSignificant = 0
b3350 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 73 70 61  ;.  while( isspa
b3360 63 65 28 2a 28 75 38 2a 29 7a 29 20 29 20 7a 2b  ce(*(u8*)z) ) z+
b3370 2b 3b 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2d 27  +;.  if( *z=='-'
b3380 20 29 7b 0a 20 20 20 20 73 69 67 6e 20 3d 20 2d   ){.    sign = -
b3390 31 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 65  1;.    z++;.  }e
b33a0 6c 73 65 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20  lse if( *z=='+' 
b33b0 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 0a  ){.    z++;.  }.
b33c0 20 20 77 68 69 6c 65 28 20 7a 5b 30 5d 3d 3d 27    while( z[0]=='
b33d0 30 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20  0' ){.    z++;. 
b33e0 20 7d 0a 20 20 77 68 69 6c 65 28 20 69 73 64 69   }.  while( isdi
b33f0 67 69 74 28 2a 28 75 38 2a 29 7a 29 20 29 7b 0a  git(*(u8*)z) ){.
b3400 20 20 20 20 76 31 20 3d 20 76 31 2a 31 30 2e 30      v1 = v1*10.0
b3410 20 2b 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 20   + (*z - '0');. 
b3420 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 6e 53 69 67     z++;.    nSig
b3430 6e 69 66 69 63 61 6e 74 2b 2b 3b 0a 20 20 7d 0a  nificant++;.  }.
b3440 20 20 69 66 28 20 2a 7a 3d 3d 27 2e 27 20 29 7b    if( *z=='.' ){
b3450 0a 20 20 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f  .    LONGDOUBLE_
b3460 54 59 50 45 20 64 69 76 69 73 6f 72 20 3d 20 31  TYPE divisor = 1
b3470 2e 30 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 20  .0;.    z++;.   
b3480 20 69 66 28 20 6e 53 69 67 6e 69 66 69 63 61 6e   if( nSignifican
b3490 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 77 68  t==0 ){.      wh
b34a0 69 6c 65 28 20 7a 5b 30 5d 3d 3d 27 30 27 20 29  ile( z[0]=='0' )
b34b0 7b 0a 20 20 20 20 20 20 20 20 64 69 76 69 73 6f  {.        diviso
b34c0 72 20 2a 3d 20 31 30 2e 30 3b 0a 20 20 20 20 20  r *= 10.0;.     
b34d0 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a     z++;.      }.
b34e0 20 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 28      }.    while(
b34f0 20 69 73 64 69 67 69 74 28 2a 28 75 38 2a 29 7a   isdigit(*(u8*)z
b3500 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e  ) ){.      if( n
b3510 53 69 67 6e 69 66 69 63 61 6e 74 3c 31 38 20 29  Significant<18 )
b3520 7b 0a 20 20 20 20 20 20 20 20 76 31 20 3d 20 76  {.        v1 = v
b3530 31 2a 31 30 2e 30 20 2b 20 28 2a 7a 20 2d 20 27  1*10.0 + (*z - '
b3540 30 27 29 3b 0a 20 20 20 20 20 20 20 20 64 69 76  0');.        div
b3550 69 73 6f 72 20 2a 3d 20 31 30 2e 30 3b 0a 20 20  isor *= 10.0;.  
b3560 20 20 20 20 20 20 6e 53 69 67 6e 69 66 69 63 61        nSignifica
b3570 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  nt++;.      }.  
b3580 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20      z++;.    }. 
b3590 20 20 20 76 31 20 2f 3d 20 64 69 76 69 73 6f 72     v1 /= divisor
b35a0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d  ;.  }.  if( *z==
b35b0 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27 20 29  'e' || *z=='E' )
b35c0 7b 0a 20 20 20 20 69 6e 74 20 65 73 69 67 6e 20  {.    int esign 
b35d0 3d 20 31 3b 0a 20 20 20 20 69 6e 74 20 65 76 61  = 1;.    int eva
b35e0 6c 20 3d 20 30 3b 0a 20 20 20 20 4c 4f 4e 47 44  l = 0;.    LONGD
b35f0 4f 55 42 4c 45 5f 54 59 50 45 20 73 63 61 6c 65  OUBLE_TYPE scale
b3600 20 3d 20 31 2e 30 3b 0a 20 20 20 20 7a 2b 2b 3b   = 1.0;.    z++;
b3610 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27 2d 27  .    if( *z=='-'
b3620 20 29 7b 0a 20 20 20 20 20 20 65 73 69 67 6e 20   ){.      esign 
b3630 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b  = -1;.      z++;
b3640 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 2a  .    }else if( *
b3650 7a 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 20 20  z=='+' ){.      
b3660 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77  z++;.    }.    w
b3670 68 69 6c 65 28 20 69 73 64 69 67 69 74 28 2a 28  hile( isdigit(*(
b3680 75 38 2a 29 7a 29 20 29 7b 0a 20 20 20 20 20 20  u8*)z) ){.      
b3690 65 76 61 6c 20 3d 20 65 76 61 6c 2a 31 30 20 2b  eval = eval*10 +
b36a0 20 2a 7a 20 2d 20 27 30 27 3b 0a 20 20 20 20 20   *z - '0';.     
b36b0 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20   z++;.    }.    
b36c0 77 68 69 6c 65 28 20 65 76 61 6c 3e 3d 36 34 20  while( eval>=64 
b36d0 29 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30 65  ){ scale *= 1.0e
b36e0 2b 36 34 3b 20 65 76 61 6c 20 2d 3d 20 36 34 3b  +64; eval -= 64;
b36f0 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 65 76   }.    while( ev
b3700 61 6c 3e 3d 31 36 20 29 7b 20 73 63 61 6c 65 20  al>=16 ){ scale 
b3710 2a 3d 20 31 2e 30 65 2b 31 36 3b 20 65 76 61 6c  *= 1.0e+16; eval
b3720 20 2d 3d 20 31 36 3b 20 7d 0a 20 20 20 20 77 68   -= 16; }.    wh
b3730 69 6c 65 28 20 65 76 61 6c 3e 3d 34 20 29 7b 20  ile( eval>=4 ){ 
b3740 73 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 34 3b  scale *= 1.0e+4;
b3750 20 65 76 61 6c 20 2d 3d 20 34 3b 20 7d 0a 20 20   eval -= 4; }.  
b3760 20 20 77 68 69 6c 65 28 20 65 76 61 6c 3e 3d 31    while( eval>=1
b3770 20 29 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30   ){ scale *= 1.0
b3780 65 2b 31 3b 20 65 76 61 6c 20 2d 3d 20 31 3b 20  e+1; eval -= 1; 
b3790 7d 0a 20 20 20 20 69 66 28 20 65 73 69 67 6e 3c  }.    if( esign<
b37a0 30 20 29 7b 0a 20 20 20 20 20 20 76 31 20 2f 3d  0 ){.      v1 /=
b37b0 20 73 63 61 6c 65 3b 0a 20 20 20 20 7d 65 6c 73   scale;.    }els
b37c0 65 7b 0a 20 20 20 20 20 20 76 31 20 2a 3d 20 73  e{.      v1 *= s
b37d0 63 61 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  cale;.    }.  }.
b37e0 20 20 2a 70 52 65 73 75 6c 74 20 3d 20 73 69 67    *pResult = sig
b37f0 6e 3c 30 20 3f 20 2d 76 31 20 3a 20 76 31 3b 0a  n<0 ? -v1 : v1;.
b3800 20 20 72 65 74 75 72 6e 20 7a 20 2d 20 7a 42 65    return z - zBe
b3810 67 69 6e 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74  gin;.#else.  ret
b3820 75 72 6e 20 73 71 6c 69 74 65 33 41 74 6f 69 36  urn sqlite3Atoi6
b3830 34 28 7a 2c 20 70 52 65 73 75 6c 74 29 3b 0a 23  4(z, pResult);.#
b3840 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
b3850 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
b3860 49 4e 54 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  INT */.}../*.** 
b3870 43 6f 6d 70 61 72 65 20 74 68 65 20 31 39 2d 63  Compare the 19-c
b3880 68 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 20  haracter string 
b3890 7a 4e 75 6d 20 61 67 61 69 6e 73 74 20 74 68 65  zNum against the
b38a0 20 74 65 78 74 20 72 65 70 72 65 73 65 6e 74 61   text representa
b38b0 74 69 6f 6e 0a 2a 2a 20 76 61 6c 75 65 20 32 5e  tion.** value 2^
b38c0 36 33 3a 20 20 39 32 32 33 33 37 32 30 33 36 38  63:  92233720368
b38d0 35 34 37 37 35 38 30 38 2e 20 20 52 65 74 75 72  54775808.  Retur
b38e0 6e 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f  n negative, zero
b38f0 2c 20 6f 72 20 70 6f 73 69 74 69 76 65 0a 2a 2a  , or positive.**
b3900 20 69 66 20 7a 4e 75 6d 20 69 73 20 6c 65 73 73   if zNum is less
b3910 20 74 68 61 6e 2c 20 65 71 75 61 6c 20 74 6f 2c   than, equal to,
b3920 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e   or greater than
b3930 20 74 68 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a   the string..**.
b3940 2a 2a 20 55 6e 6c 69 6b 65 20 6d 65 6d 63 6d 70  ** Unlike memcmp
b3950 28 29 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  () this routine 
b3960 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  is guaranteed to
b3970 20 72 65 74 75 72 6e 20 74 68 65 20 64 69 66 66   return the diff
b3980 65 72 65 6e 63 65 0a 2a 2a 20 69 6e 20 74 68 65  erence.** in the
b3990 20 76 61 6c 75 65 73 20 6f 66 20 74 68 65 20 6c   values of the l
b39a0 61 73 74 20 64 69 67 69 74 20 69 66 20 74 68 65  ast digit if the
b39b0 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e 63 65   only difference
b39c0 20 69 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6c 61   is in the.** la
b39d0 73 74 20 64 69 67 69 74 2e 20 20 53 6f 2c 20 66  st digit.  So, f
b39e0 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 0a 2a  or example,.**.*
b39f0 2a 20 20 20 20 20 20 63 6f 6d 70 61 72 65 32 70  *      compare2p
b3a00 6f 77 36 33 28 22 39 32 32 33 33 37 32 30 33 36  ow63("9223372036
b3a10 38 35 34 37 37 35 38 30 30 22 29 0a 2a 2a 0a 2a  854775800").**.*
b3a20 2a 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 2d 38  * will return -8
b3a30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
b3a40 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 63 6f  compare2pow63(co
b3a50 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 29 7b  nst char *zNum){
b3a60 0a 20 20 69 6e 74 20 63 3b 0a 20 20 63 20 3d 20  .  int c;.  c = 
b3a70 6d 65 6d 63 6d 70 28 7a 4e 75 6d 2c 22 39 32 32  memcmp(zNum,"922
b3a80 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30 22  337203685477580"
b3a90 2c 31 38 29 3b 0a 20 20 69 66 28 20 63 3d 3d 30  ,18);.  if( c==0
b3aa0 20 29 7b 0a 20 20 20 20 63 20 3d 20 7a 4e 75 6d   ){.    c = zNum
b3ab0 5b 31 38 5d 20 2d 20 27 38 27 3b 0a 20 20 7d 0a  [18] - '8';.  }.
b3ac0 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a 0a    return c;.}...
b3ad0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55  /*.** Return TRU
b3ae0 45 20 69 66 20 7a 4e 75 6d 20 69 73 20 61 20 36  E if zNum is a 6
b3af0 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74  4-bit signed int
b3b00 65 67 65 72 20 61 6e 64 20 77 72 69 74 65 0a 2a  eger and write.*
b3b10 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  * the value of t
b3b20 68 65 20 69 6e 74 65 67 65 72 20 69 6e 74 6f 20  he integer into 
b3b30 2a 70 4e 75 6d 2e 20 20 49 66 20 7a 4e 75 6d 20  *pNum.  If zNum 
b3b40 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65  is not an intege
b3b50 72 0a 2a 2a 20 6f 72 20 69 73 20 61 6e 20 69 6e  r.** or is an in
b3b60 74 65 67 65 72 20 74 68 61 74 20 69 73 20 74 6f  teger that is to
b3b70 6f 20 6c 61 72 67 65 20 74 6f 20 62 65 20 65 78  o large to be ex
b3b80 70 72 65 73 73 65 64 20 77 69 74 68 20 36 34 20  pressed with 64 
b3b90 62 69 74 73 2c 0a 2a 2a 20 74 68 65 6e 20 72 65  bits,.** then re
b3ba0 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a  turn false..**.*
b3bb0 2a 20 57 68 65 6e 20 74 68 69 73 20 72 6f 75 74  * When this rout
b3bc0 69 6e 65 20 77 61 73 20 6f 72 69 67 69 6e 61 6c  ine was original
b3bd0 6c 79 20 77 72 69 74 74 65 6e 20 69 74 20 64 65  ly written it de
b3be0 61 6c 74 20 77 69 74 68 20 6f 6e 6c 79 0a 2a 2a  alt with only.**
b3bf0 20 33 32 2d 62 69 74 20 6e 75 6d 62 65 72 73 2e   32-bit numbers.
b3c00 20 20 41 74 20 74 68 61 74 20 74 69 6d 65 2c 20    At that time, 
b3c10 69 74 20 77 61 73 20 6d 75 63 68 20 66 61 73 74  it was much fast
b3c20 65 72 20 74 68 61 6e 20 74 68 65 0a 2a 2a 20 61  er than the.** a
b3c30 74 6f 69 28 29 20 6c 69 62 72 61 72 79 20 72 6f  toi() library ro
b3c40 75 74 69 6e 65 20 69 6e 20 52 65 64 48 61 74 20  utine in RedHat 
b3c50 37 2e 32 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  7.2..*/.SQLITE_P
b3c60 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
b3c70 65 33 41 74 6f 69 36 34 28 63 6f 6e 73 74 20 63  e3Atoi64(const c
b3c80 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 36 34 20 2a  har *zNum, i64 *
b3c90 70 4e 75 6d 29 7b 0a 20 20 69 36 34 20 76 20 3d  pNum){.  i64 v =
b3ca0 20 30 3b 0a 20 20 69 6e 74 20 6e 65 67 3b 0a 20   0;.  int neg;. 
b3cb0 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 63 6f 6e   int i, c;.  con
b3cc0 73 74 20 63 68 61 72 20 2a 7a 53 74 61 72 74 3b  st char *zStart;
b3cd0 0a 20 20 77 68 69 6c 65 28 20 69 73 73 70 61 63  .  while( isspac
b3ce0 65 28 2a 28 75 38 2a 29 7a 4e 75 6d 29 20 29 20  e(*(u8*)zNum) ) 
b3cf0 7a 4e 75 6d 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a  zNum++;.  if( *z
b3d00 4e 75 6d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20  Num=='-' ){.    
b3d10 6e 65 67 20 3d 20 31 3b 0a 20 20 20 20 7a 4e 75  neg = 1;.    zNu
b3d20 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  m++;.  }else if(
b3d30 20 2a 7a 4e 75 6d 3d 3d 27 2b 27 20 29 7b 0a 20   *zNum=='+' ){. 
b3d40 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20 20 20 20     neg = 0;.    
b3d50 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b  zNum++;.  }else{
b3d60 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20 20  .    neg = 0;.  
b3d70 7d 0a 20 20 7a 53 74 61 72 74 20 3d 20 7a 4e 75  }.  zStart = zNu
b3d80 6d 3b 0a 20 20 77 68 69 6c 65 28 20 7a 4e 75 6d  m;.  while( zNum
b3d90 5b 30 5d 3d 3d 27 30 27 20 29 7b 20 7a 4e 75 6d  [0]=='0' ){ zNum
b3da0 2b 2b 3b 20 7d 20 2f 2a 20 53 6b 69 70 20 6f 76  ++; } /* Skip ov
b3db0 65 72 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73  er leading zeros
b3dc0 2e 20 54 69 63 6b 65 74 20 23 32 34 35 34 20 2a  . Ticket #2454 *
b3dd0 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 28 63 3d  /.  for(i=0; (c=
b3de0 7a 4e 75 6d 5b 69 5d 29 3e 3d 27 30 27 20 26 26  zNum[i])>='0' &&
b3df0 20 63 3c 3d 27 39 27 3b 20 69 2b 2b 29 7b 0a 20   c<='9'; i++){. 
b3e00 20 20 20 76 20 3d 20 76 2a 31 30 20 2b 20 63 20     v = v*10 + c 
b3e10 2d 20 27 30 27 3b 0a 20 20 7d 0a 20 20 2a 70 4e  - '0';.  }.  *pN
b3e20 75 6d 20 3d 20 6e 65 67 20 3f 20 2d 76 20 3a 20  um = neg ? -v : 
b3e30 76 3b 0a 20 20 69 66 28 20 63 21 3d 30 20 7c 7c  v;.  if( c!=0 ||
b3e40 20 28 69 3d 3d 30 20 26 26 20 7a 53 74 61 72 74   (i==0 && zStart
b3e50 3d 3d 7a 4e 75 6d 29 20 7c 7c 20 69 3e 31 39 20  ==zNum) || i>19 
b3e60 29 7b 0a 20 20 20 20 2f 2a 20 7a 4e 75 6d 20 69  ){.    /* zNum i
b3e70 73 20 65 6d 70 74 79 20 6f 72 20 63 6f 6e 74 61  s empty or conta
b3e80 69 6e 73 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 20  ins non-numeric 
b3e90 74 65 78 74 20 6f 72 20 69 73 20 6c 6f 6e 67 65  text or is longe
b3ea0 72 0a 20 20 20 20 2a 2a 20 74 68 61 6e 20 31 39  r.    ** than 19
b3eb0 20 64 69 67 69 74 73 20 28 74 68 75 73 20 67 75   digits (thus gu
b3ec0 61 72 61 6e 74 69 6e 67 20 74 68 61 74 20 69 74  aranting that it
b3ed0 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 29 20 2a   is too large) *
b3ee0 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  /.    return 0;.
b3ef0 20 20 7d 65 6c 73 65 20 69 66 28 20 69 3c 31 39    }else if( i<19
b3f00 20 29 7b 0a 20 20 20 20 2f 2a 20 4c 65 73 73 20   ){.    /* Less 
b3f10 74 68 61 6e 20 31 39 20 64 69 67 69 74 73 2c 20  than 19 digits, 
b3f20 73 6f 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20  so we know that 
b3f30 69 74 20 66 69 74 73 20 69 6e 20 36 34 20 62 69  it fits in 64 bi
b3f40 74 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  ts */.    return
b3f50 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   1;.  }else{.   
b3f60 20 2f 2a 20 31 39 2d 64 69 67 69 74 20 6e 75 6d   /* 19-digit num
b3f70 62 65 72 73 20 6d 75 73 74 20 62 65 20 6e 6f 20  bers must be no 
b3f80 6c 61 72 67 65 72 20 74 68 61 6e 20 39 32 32 33  larger than 9223
b3f90 33 37 32 30 33 36 38 35 34 37 37 35 38 30 37 20  372036854775807 
b3fa0 69 66 20 70 6f 73 69 74 69 76 65 0a 20 20 20 20  if positive.    
b3fb0 2a 2a 20 6f 72 20 39 32 32 33 33 37 32 30 33 36  ** or 9223372036
b3fc0 38 35 34 37 37 35 38 30 38 20 69 66 20 6e 65 67  854775808 if neg
b3fd0 61 74 69 76 65 2e 20 20 4e 6f 74 65 20 74 68 61  ative.  Note tha
b3fe0 74 20 39 32 32 33 33 37 32 30 33 36 38 35 34 36  t 92233720368546
b3ff0 36 35 38 30 38 0a 20 20 20 20 2a 2a 20 69 73 20  65808.    ** is 
b4000 32 5e 36 33 2e 20 2a 2f 0a 20 20 20 20 72 65 74  2^63. */.    ret
b4010 75 72 6e 20 63 6f 6d 70 61 72 65 32 70 6f 77 36  urn compare2pow6
b4020 33 28 7a 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d  3(zNum)<neg;.  }
b4030 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 74  .}../*.** The st
b4040 72 69 6e 67 20 7a 4e 75 6d 20 72 65 70 72 65 73  ring zNum repres
b4050 65 6e 74 73 20 61 6e 20 69 6e 74 65 67 65 72 2e  ents an integer.
b4060 20 20 54 68 65 72 65 20 6d 69 67 68 74 20 62 65    There might be
b4070 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 69   some other.** i
b4080 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 6c 6c 6f  nformation follo
b4090 77 69 6e 67 20 74 68 65 20 69 6e 74 65 67 65 72  wing the integer
b40a0 20 74 6f 6f 2c 20 62 75 74 20 74 68 61 74 20 70   too, but that p
b40b0 61 72 74 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a  art is ignored..
b40c0 2a 2a 20 49 66 20 74 68 65 20 69 6e 74 65 67 65  ** If the intege
b40d0 72 20 74 68 61 74 20 74 68 65 20 70 72 65 66 69  r that the prefi
b40e0 78 20 6f 66 20 7a 4e 75 6d 20 72 65 70 72 65 73  x of zNum repres
b40f0 65 6e 74 73 20 77 69 6c 6c 20 66 69 74 20 69 6e  ents will fit in
b4100 20 61 0a 2a 2a 20 36 34 2d 62 69 74 20 73 69 67   a.** 64-bit sig
b4110 6e 65 64 20 69 6e 74 65 67 65 72 2c 20 72 65 74  ned integer, ret
b4120 75 72 6e 20 54 52 55 45 2e 20 20 4f 74 68 65 72  urn TRUE.  Other
b4130 77 69 73 65 20 72 65 74 75 72 6e 20 46 41 4c 53  wise return FALS
b4140 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  E..**.** This ro
b4150 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 46 41  utine returns FA
b4160 4c 53 45 20 66 6f 72 20 74 68 65 20 73 74 72 69  LSE for the stri
b4170 6e 67 20 2d 39 32 32 33 33 37 32 30 33 36 38 35  ng -922337203685
b4180 34 37 37 35 38 30 38 20 65 76 65 6e 20 74 68 61  4775808 even tha
b4190 74 0a 2a 2a 20 74 68 61 74 20 6e 75 6d 62 65 72  t.** that number
b41a0 20 77 69 6c 6c 2c 20 69 6e 20 74 68 65 6f 72 79   will, in theory
b41b0 20 66 69 74 20 69 6e 20 61 20 36 34 2d 62 69 74   fit in a 64-bit
b41c0 20 69 6e 74 65 67 65 72 2e 20 20 50 6f 73 69 74   integer.  Posit
b41d0 69 76 65 0a 2a 2a 20 39 32 32 33 33 37 33 30 33  ive.** 922337303
b41e0 36 38 35 34 37 37 35 38 30 38 20 77 69 6c 6c 20  6854775808 will 
b41f0 6e 6f 74 20 66 69 74 20 69 6e 20 36 34 20 62 69  not fit in 64 bi
b4200 74 73 2e 20 20 53 6f 20 69 74 20 73 65 65 6d 73  ts.  So it seems
b4210 20 73 61 66 65 72 20 74 6f 20 72 65 74 75 72 6e   safer to return
b4220 0a 2a 2a 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51  .** false..*/.SQ
b4230 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
b4240 20 73 71 6c 69 74 65 33 46 69 74 73 49 6e 36 34   sqlite3FitsIn64
b4250 42 69 74 73 28 63 6f 6e 73 74 20 63 68 61 72 20  Bits(const char 
b4260 2a 7a 4e 75 6d 2c 20 69 6e 74 20 6e 65 67 46 6c  *zNum, int negFl
b4270 61 67 29 7b 0a 20 20 69 6e 74 20 69 2c 20 63 3b  ag){.  int i, c;
b4280 0a 20 20 69 6e 74 20 6e 65 67 20 3d 20 30 3b 0a  .  int neg = 0;.
b4290 20 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2d 27    if( *zNum=='-'
b42a0 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b   ){.    neg = 1;
b42b0 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d  .    zNum++;.  }
b42c0 65 6c 73 65 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d  else if( *zNum==
b42d0 27 2b 27 20 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b  '+' ){.    zNum+
b42e0 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 67  +;.  }.  if( neg
b42f0 46 6c 61 67 20 29 20 6e 65 67 20 3d 20 31 2d 6e  Flag ) neg = 1-n
b4300 65 67 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 4e  eg;.  while( *zN
b4310 75 6d 3d 3d 27 30 27 20 29 7b 0a 20 20 20 20 7a  um=='0' ){.    z
b4320 4e 75 6d 2b 2b 3b 20 20 20 2f 2a 20 53 6b 69 70  Num++;   /* Skip
b4330 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20   leading zeros. 
b4340 20 54 69 63 6b 65 74 20 23 32 34 35 34 20 2a 2f   Ticket #2454 */
b4350 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  .  }.  for(i=0; 
b4360 28 63 3d 7a 4e 75 6d 5b 69 5d 29 3e 3d 27 30 27  (c=zNum[i])>='0'
b4370 20 26 26 20 63 3c 3d 27 39 27 3b 20 69 2b 2b 29   && c<='9'; i++)
b4380 7b 7d 0a 20 20 69 66 28 20 69 3c 31 39 20 29 7b  {}.  if( i<19 ){
b4390 0a 20 20 20 20 2f 2a 20 47 75 61 72 61 6e 74 65  .    /* Guarante
b43a0 65 64 20 74 6f 20 66 69 74 20 69 66 20 6c 65 73  ed to fit if les
b43b0 73 20 74 68 61 6e 20 31 39 20 64 69 67 69 74 73  s than 19 digits
b43c0 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31   */.    return 1
b43d0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 3e  ;.  }else if( i>
b43e0 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20 47 75 61  19 ){.    /* Gua
b43f0 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20 74 6f  ranteed to be to
b4400 6f 20 62 69 67 20 69 66 20 67 72 65 61 74 65 72  o big if greater
b4410 20 74 68 61 6e 20 31 39 20 64 69 67 69 74 73 20   than 19 digits 
b4420 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  */.    return 0;
b4430 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
b4440 20 43 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74   Compare against
b4450 20 32 5e 36 33 2e 20 2a 2f 0a 20 20 20 20 72 65   2^63. */.    re
b4460 74 75 72 6e 20 63 6f 6d 70 61 72 65 32 70 6f 77  turn compare2pow
b4470 36 33 28 7a 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20  63(zNum)<neg;.  
b4480 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 7a 4e  }.}../*.** If zN
b4490 75 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e  um represents an
b44a0 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 77 69   integer that wi
b44b0 6c 6c 20 66 69 74 20 69 6e 20 33 32 2d 62 69 74  ll fit in 32-bit
b44c0 73 2c 20 74 68 65 6e 20 73 65 74 0a 2a 2a 20 2a  s, then set.** *
b44d0 70 56 61 6c 75 65 20 74 6f 20 74 68 61 74 20 69  pValue to that i
b44e0 6e 74 65 67 65 72 20 61 6e 64 20 72 65 74 75 72  nteger and retur
b44f0 6e 20 74 72 75 65 2e 20 20 4f 74 68 65 72 77 69  n true.  Otherwi
b4500 73 65 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2e  se return false.
b4510 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 6e 6f 6e 2d 6e  .**.** Any non-n
b4520 75 6d 65 72 69 63 20 63 68 61 72 61 63 74 65 72  umeric character
b4530 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 69 6e 67  s that following
b4540 20 7a 4e 75 6d 20 61 72 65 20 69 67 6e 6f 72 65   zNum are ignore
b4550 64 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 64 69  d..** This is di
b4560 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 73 71 6c  fferent from sql
b4570 69 74 65 33 41 74 6f 69 36 34 28 29 20 77 68 69  ite3Atoi64() whi
b4580 63 68 20 72 65 71 75 69 72 65 73 20 74 68 65 0a  ch requires the.
b4590 2a 2a 20 69 6e 70 75 74 20 6e 75 6d 62 65 72 20  ** input number 
b45a0 74 6f 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69  to be zero-termi
b45b0 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  nated..*/.SQLITE
b45c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
b45d0 69 74 65 33 47 65 74 49 6e 74 33 32 28 63 6f 6e  ite3GetInt32(con
b45e0 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69  st char *zNum, i
b45f0 6e 74 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 73  nt *pValue){.  s
b4600 71 6c 69 74 65 5f 69 6e 74 36 34 20 76 20 3d 20  qlite_int64 v = 
b4610 30 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20  0;.  int i, c;. 
b4620 20 69 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 20 20   int neg = 0;.  
b4630 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2d 27  if( zNum[0]=='-'
b4640 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b   ){.    neg = 1;
b4650 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d  .    zNum++;.  }
b4660 65 6c 73 65 20 69 66 28 20 7a 4e 75 6d 5b 30 5d  else if( zNum[0]
b4670 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 7a 4e 75  =='+' ){.    zNu
b4680 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65  m++;.  }.  while
b4690 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29  ( zNum[0]=='0' )
b46a0 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 66 6f 72 28 69   zNum++;.  for(i
b46b0 3d 30 3b 20 69 3c 31 31 20 26 26 20 28 63 20 3d  =0; i<11 && (c =
b46c0 20 7a 4e 75 6d 5b 69 5d 20 2d 20 27 30 27 29 3e   zNum[i] - '0')>
b46d0 3d 30 20 26 26 20 63 3c 3d 39 3b 20 69 2b 2b 29  =0 && c<=9; i++)
b46e0 7b 0a 20 20 20 20 76 20 3d 20 76 2a 31 30 20 2b  {.    v = v*10 +
b46f0 20 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68   c;.  }..  /* Th
b4700 65 20 6c 6f 6e 67 65 73 74 20 64 65 63 69 6d 61  e longest decima
b4710 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  l representation
b4720 20 6f 66 20 61 20 33 32 20 62 69 74 20 69 6e 74   of a 32 bit int
b4730 65 67 65 72 20 69 73 20 31 30 20 64 69 67 69 74  eger is 10 digit
b4740 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  s:.  **.  **    
b4750 20 20 20 20 20 20 20 20 20 31 32 33 34 35 36 37           1234567
b4760 38 39 30 0a 20 20 2a 2a 20 20 20 20 20 32 5e 33  890.  **     2^3
b4770 31 20 2d 3e 20 32 31 34 37 34 38 33 36 34 38 0a  1 -> 2147483648.
b4780 20 20 2a 2f 0a 20 20 69 66 28 20 69 3e 31 30 20    */.  if( i>10 
b4790 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
b47a0 0a 20 20 7d 0a 20 20 69 66 28 20 76 2d 6e 65 67  .  }.  if( v-neg
b47b0 3e 32 31 34 37 34 38 33 36 34 37 20 29 7b 0a 20  >2147483647 ){. 
b47c0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
b47d0 0a 20 20 69 66 28 20 6e 65 67 20 29 7b 0a 20 20  .  if( neg ){.  
b47e0 20 20 76 20 3d 20 2d 76 3b 0a 20 20 7d 0a 20 20    v = -v;.  }.  
b47f0 2a 70 56 61 6c 75 65 20 3d 20 28 69 6e 74 29 76  *pValue = (int)v
b4800 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  ;.  return 1;.}.
b4810 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76 61 72 69 61  ./*.** The varia
b4820 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67  ble-length integ
b4830 65 72 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 61  er encoding is a
b4840 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
b4850 20 4b 45 59 3a 0a 2a 2a 20 20 20 20 20 20 20 20   KEY:.**        
b4860 20 41 20 3d 20 30 78 78 78 78 78 78 78 20 20 20   A = 0xxxxxxx   
b4870 20 37 20 62 69 74 73 20 6f 66 20 64 61 74 61 20   7 bits of data 
b4880 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74  and one flag bit
b4890 0a 2a 2a 20 20 20 20 20 20 20 20 20 42 20 3d 20  .**         B = 
b48a0 31 78 78 78 78 78 78 78 20 20 20 20 37 20 62 69  1xxxxxxx    7 bi
b48b0 74 73 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f  ts of data and o
b48c0 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20  ne flag bit.**  
b48d0 20 20 20 20 20 20 20 43 20 3d 20 78 78 78 78 78         C = xxxxx
b48e0 78 78 78 20 20 20 20 38 20 62 69 74 73 20 6f 66  xxx    8 bits of
b48f0 20 64 61 74 61 0a 2a 2a 0a 2a 2a 20 20 37 20 62   data.**.**  7 b
b4900 69 74 73 20 2d 20 41 0a 2a 2a 20 31 34 20 62 69  its - A.** 14 bi
b4910 74 73 20 2d 20 42 41 0a 2a 2a 20 32 31 20 62 69  ts - BA.** 21 bi
b4920 74 73 20 2d 20 42 42 41 0a 2a 2a 20 32 38 20 62  ts - BBA.** 28 b
b4930 69 74 73 20 2d 20 42 42 42 41 0a 2a 2a 20 33 35  its - BBBA.** 35
b4940 20 62 69 74 73 20 2d 20 42 42 42 42 41 0a 2a 2a   bits - BBBBA.**
b4950 20 34 32 20 62 69 74 73 20 2d 20 42 42 42 42 42   42 bits - BBBBB
b4960 41 0a 2a 2a 20 34 39 20 62 69 74 73 20 2d 20 42  A.** 49 bits - B
b4970 42 42 42 42 42 41 0a 2a 2a 20 35 36 20 62 69 74  BBBBBA.** 56 bit
b4980 73 20 2d 20 42 42 42 42 42 42 42 41 0a 2a 2a 20  s - BBBBBBBA.** 
b4990 36 34 20 62 69 74 73 20 2d 20 42 42 42 42 42 42  64 bits - BBBBBB
b49a0 42 42 43 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 72  BBC.*/../*.** Wr
b49b0 69 74 65 20 61 20 36 34 2d 62 69 74 20 76 61 72  ite a 64-bit var
b49c0 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74  iable-length int
b49d0 65 67 65 72 20 74 6f 20 6d 65 6d 6f 72 79 20 73  eger to memory s
b49e0 74 61 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e  tarting at p[0].
b49f0 0a 2a 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 6f  .** The length o
b4a00 66 20 64 61 74 61 20 77 72 69 74 65 20 77 69 6c  f data write wil
b4a10 6c 20 62 65 20 62 65 74 77 65 65 6e 20 31 20 61  l be between 1 a
b4a20 6e 64 20 39 20 62 79 74 65 73 2e 20 20 54 68 65  nd 9 bytes.  The
b4a30 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79   number.** of by
b4a40 74 65 73 20 77 72 69 74 74 65 6e 20 69 73 20 72  tes written is r
b4a50 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 41  eturned..**.** A
b4a60 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68   variable-length
b4a70 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 69 73 74   integer consist
b4a80 73 20 6f 66 20 74 68 65 20 6c 6f 77 65 72 20 37  s of the lower 7
b4a90 20 62 69 74 73 20 6f 66 20 65 61 63 68 20 62 79   bits of each by
b4aa0 74 65 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20 62 79  te.** for all by
b4ab0 74 65 73 20 74 68 61 74 20 68 61 76 65 20 74 68  tes that have th
b4ac0 65 20 38 74 68 20 62 69 74 20 73 65 74 20 61 6e  e 8th bit set an
b4ad0 64 20 6f 6e 65 20 62 79 74 65 20 77 69 74 68 20  d one byte with 
b4ae0 74 68 65 20 38 74 68 0a 2a 2a 20 62 69 74 20 63  the 8th.** bit c
b4af0 6c 65 61 72 2e 20 20 45 78 63 65 70 74 2c 20 69  lear.  Except, i
b4b00 66 20 77 65 20 67 65 74 20 74 6f 20 74 68 65 20  f we get to the 
b4b10 39 74 68 20 62 79 74 65 2c 20 69 74 20 73 74 6f  9th byte, it sto
b4b20 72 65 73 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20  res the full.** 
b4b30 38 20 62 69 74 73 20 61 6e 64 20 69 73 20 74 68  8 bits and is th
b4b40 65 20 6c 61 73 74 20 62 79 74 65 2e 0a 2a 2f 0a  e last byte..*/.
b4b50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
b4b60 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56 61 72  nt sqlite3PutVar
b4b70 69 6e 74 28 75 6e 73 69 67 6e 65 64 20 63 68 61  int(unsigned cha
b4b80 72 20 2a 70 2c 20 75 36 34 20 76 29 7b 0a 20 20  r *p, u64 v){.  
b4b90 69 6e 74 20 69 2c 20 6a 2c 20 6e 3b 0a 20 20 75  int i, j, n;.  u
b4ba0 38 20 62 75 66 5b 31 30 5d 3b 0a 20 20 69 66 28  8 buf[10];.  if(
b4bb0 20 76 20 26 20 28 28 28 75 36 34 29 30 78 66 66   v & (((u64)0xff
b4bc0 30 30 30 30 30 30 29 3c 3c 33 32 29 20 29 7b 0a  000000)<<32) ){.
b4bd0 20 20 20 20 70 5b 38 5d 20 3d 20 76 3b 0a 20 20      p[8] = v;.  
b4be0 20 20 76 20 3e 3e 3d 20 38 3b 0a 20 20 20 20 66    v >>= 8;.    f
b4bf0 6f 72 28 69 3d 37 3b 20 69 3e 3d 30 3b 20 69 2d  or(i=7; i>=0; i-
b4c00 2d 29 7b 0a 20 20 20 20 20 20 70 5b 69 5d 20 3d  -){.      p[i] =
b4c10 20 28 76 20 26 20 30 78 37 66 29 20 7c 20 30 78   (v & 0x7f) | 0x
b4c20 38 30 3b 0a 20 20 20 20 20 20 76 20 3e 3e 3d 20  80;.      v >>= 
b4c30 37 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  7;.    }.    ret
b4c40 75 72 6e 20 39 3b 0a 20 20 7d 20 20 20 20 0a 20  urn 9;.  }    . 
b4c50 20 6e 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20   n = 0;.  do{.  
b4c60 20 20 62 75 66 5b 6e 2b 2b 5d 20 3d 20 28 76 20    buf[n++] = (v 
b4c70 26 20 30 78 37 66 29 20 7c 20 30 78 38 30 3b 0a  & 0x7f) | 0x80;.
b4c80 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 7d      v >>= 7;.  }
b4c90 77 68 69 6c 65 28 20 76 21 3d 30 20 29 3b 0a 20  while( v!=0 );. 
b4ca0 20 62 75 66 5b 30 5d 20 26 3d 20 30 78 37 66 3b   buf[0] &= 0x7f;
b4cb0 0a 20 20 61 73 73 65 72 74 28 20 6e 3c 3d 39 20  .  assert( n<=9 
b4cc0 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d  );.  for(i=0, j=
b4cd0 6e 2d 31 3b 20 6a 3e 3d 30 3b 20 6a 2d 2d 2c 20  n-1; j>=0; j--, 
b4ce0 69 2b 2b 29 7b 0a 20 20 20 20 70 5b 69 5d 20 3d  i++){.    p[i] =
b4cf0 20 62 75 66 5b 6a 5d 3b 0a 20 20 7d 0a 20 20 72   buf[j];.  }.  r
b4d00 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn n;.}../*.*
b4d10 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
b4d20 73 20 61 20 66 61 73 74 65 72 20 76 65 72 73 69  s a faster versi
b4d30 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 50 75 74  on of sqlite3Put
b4d40 56 61 72 69 6e 74 28 29 20 74 68 61 74 20 6f 6e  Varint() that on
b4d50 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20  ly.** works for 
b4d60 33 32 2d 62 69 74 20 70 6f 73 69 74 69 76 65 20  32-bit positive 
b4d70 69 6e 74 65 67 65 72 73 20 61 6e 64 20 77 68 69  integers and whi
b4d80 63 68 20 69 73 20 6f 70 74 69 6d 69 7a 65 64 20  ch is optimized 
b4d90 66 6f 72 0a 2a 2a 20 74 68 65 20 63 6f 6d 6d 6f  for.** the commo
b4da0 6e 20 63 61 73 65 20 6f 66 20 73 6d 61 6c 6c 20  n case of small 
b4db0 69 6e 74 65 67 65 72 73 2e 20 20 41 20 4d 41 43  integers.  A MAC
b4dc0 52 4f 20 76 65 72 73 69 6f 6e 2c 20 70 75 74 56  RO version, putV
b4dd0 61 72 69 6e 74 33 32 2c 0a 2a 2a 20 69 73 20 70  arint32,.** is p
b4de0 72 6f 76 69 64 65 64 20 77 68 69 63 68 20 69 6e  rovided which in
b4df0 6c 69 6e 65 73 20 74 68 65 20 73 69 6e 67 6c 65  lines the single
b4e00 2d 62 79 74 65 20 63 61 73 65 2e 20 20 41 6c 6c  -byte case.  All
b4e10 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 75 73 65   code should use
b4e20 0a 2a 2a 20 74 68 65 20 4d 41 43 52 4f 20 76 65  .** the MACRO ve
b4e30 72 73 69 6f 6e 20 61 73 20 74 68 69 73 20 66 75  rsion as this fu
b4e40 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74  nction assumes t
b4e50 68 65 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63  he single-byte c
b4e60 61 73 65 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61  ase has.** alrea
b4e70 64 79 20 62 65 65 6e 20 68 61 6e 64 6c 65 64 2e  dy been handled.
b4e80 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
b4e90 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75  TE int sqlite3Pu
b4ea0 74 56 61 72 69 6e 74 33 32 28 75 6e 73 69 67 6e  tVarint32(unsign
b4eb0 65 64 20 63 68 61 72 20 2a 70 2c 20 75 33 32 20  ed char *p, u32 
b4ec0 76 29 7b 0a 23 69 66 6e 64 65 66 20 70 75 74 56  v){.#ifndef putV
b4ed0 61 72 69 6e 74 33 32 0a 20 20 69 66 28 20 28 76  arint32.  if( (v
b4ee0 20 26 20 7e 30 78 37 66 29 3d 3d 30 20 29 7b 0a   & ~0x7f)==0 ){.
b4ef0 20 20 20 20 70 5b 30 5d 20 3d 20 76 3b 0a 20 20      p[0] = v;.  
b4f00 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
b4f10 23 65 6e 64 69 66 0a 20 20 69 66 28 20 28 76 20  #endif.  if( (v 
b4f20 26 20 7e 30 78 33 66 66 66 29 3d 3d 30 20 29 7b  & ~0x3fff)==0 ){
b4f30 0a 20 20 20 20 70 5b 30 5d 20 3d 20 28 76 3e 3e  .    p[0] = (v>>
b4f40 37 29 20 7c 20 30 78 38 30 3b 0a 20 20 20 20 70  7) | 0x80;.    p
b4f50 5b 31 5d 20 3d 20 76 20 26 20 30 78 37 66 3b 0a  [1] = v & 0x7f;.
b4f60 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
b4f70 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  }.  return sqlit
b4f80 65 33 50 75 74 56 61 72 69 6e 74 28 70 2c 20 76  e3PutVarint(p, v
b4f90 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  );.}../*.** Read
b4fa0 20 61 20 36 34 2d 62 69 74 20 76 61 72 69 61 62   a 64-bit variab
b4fb0 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65  le-length intege
b4fc0 72 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74  r from memory st
b4fd0 61 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a  arting at p[0]..
b4fe0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
b4ff0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65  mber of bytes re
b5000 61 64 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69  ad.  The value i
b5010 73 20 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e 0a  s stored in *v..
b5020 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b5030 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74  E int sqlite3Get
b5040 56 61 72 69 6e 74 28 63 6f 6e 73 74 20 75 6e 73  Varint(const uns
b5050 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75  igned char *p, u
b5060 36 34 20 2a 76 29 7b 0a 20 20 75 33 32 20 61 2c  64 *v){.  u32 a,
b5070 62 2c 73 3b 0a 0a 20 20 61 20 3d 20 2a 70 3b 0a  b,s;..  a = *p;.
b5080 20 20 2f 2a 20 61 3a 20 70 30 20 28 75 6e 6d 61    /* a: p0 (unma
b5090 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21  sked) */.  if (!
b50a0 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20  (a&0x80)).  {.  
b50b0 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65    *v = a;.    re
b50c0 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 70  turn 1;.  }..  p
b50d0 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a 20 20  ++;.  b = *p;.  
b50e0 2f 2a 20 62 3a 20 70 31 20 28 75 6e 6d 61 73 6b  /* b: p1 (unmask
b50f0 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62  ed) */.  if (!(b
b5100 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20  &0x80)).  {.    
b5110 61 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 61  a &= 0x7f;.    a
b5120 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c   = a<<7;.    a |
b5130 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b  = b;.    *v = a;
b5140 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20  .    return 2;. 
b5150 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d   }..  p++;.  a =
b5160 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a   a<<14;.  a |= *
b5170 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31  p;.  /* a: p0<<1
b5180 34 20 7c 20 70 32 20 28 75 6e 6d 61 73 6b 65 64  4 | p2 (unmasked
b5190 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30  ) */.  if (!(a&0
b51a0 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20  x80)).  {.    a 
b51b0 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30  &= (0x7f<<14)|(0
b51c0 78 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 30  x7f);.    b &= 0
b51d0 78 37 66 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c  x7f;.    b = b<<
b51e0 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20  7;.    a |= b;. 
b51f0 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72     *v = a;.    r
b5200 65 74 75 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20 20  eturn 3;.  }..  
b5210 2f 2a 20 43 53 45 31 20 66 72 6f 6d 20 62 65 6c  /* CSE1 from bel
b5220 6f 77 20 2a 2f 0a 20 20 61 20 26 3d 20 28 30 78  ow */.  a &= (0x
b5230 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a  7f<<14)|(0x7f);.
b5240 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c    p++;.  b = b<<
b5250 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20  14;.  b |= *p;. 
b5260 20 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20   /* b: p1<<14 | 
b5270 70 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f  p3 (unmasked) */
b5280 0a 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29  .  if (!(b&0x80)
b5290 29 0a 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28  ).  {.    b &= (
b52a0 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29  0x7f<<14)|(0x7f)
b52b0 3b 0a 20 20 20 20 2f 2a 20 6d 6f 76 65 64 20 43  ;.    /* moved C
b52c0 53 45 31 20 75 70 20 2a 2f 0a 20 20 20 20 2f 2a  SE1 up */.    /*
b52d0 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29   a &= (0x7f<<14)
b52e0 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20  |(0x7f); */.    
b52f0 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20  a = a<<7;.    a 
b5300 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61  |= b;.    *v = a
b5310 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 34 3b 0a  ;.    return 4;.
b5320 20 20 7d 0a 0a 20 20 2f 2a 20 61 3a 20 70 30 3c    }..  /* a: p0<
b5330 3c 31 34 20 7c 20 70 32 20 28 6d 61 73 6b 65 64  <14 | p2 (masked
b5340 29 20 2a 2f 0a 20 20 2f 2a 20 62 3a 20 70 31 3c  ) */.  /* b: p1<
b5350 3c 31 34 20 7c 20 70 33 20 28 75 6e 6d 61 73 6b  <14 | p3 (unmask
b5360 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 31 3a 73 61  ed) */.  /* 1:sa
b5370 76 65 20 6f 66 66 20 70 30 3c 3c 32 31 20 7c 20  ve off p0<<21 | 
b5380 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c  p1<<14 | p2<<7 |
b5390 20 70 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a   p3 (masked) */.
b53a0 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 31 20    /* moved CSE1 
b53b0 75 70 20 2a 2f 0a 20 20 2f 2a 20 61 20 26 3d 20  up */.  /* a &= 
b53c0 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66  (0x7f<<14)|(0x7f
b53d0 29 3b 20 2a 2f 0a 20 20 62 20 26 3d 20 28 30 78  ); */.  b &= (0x
b53e0 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a  7f<<14)|(0x7f);.
b53f0 20 20 73 20 3d 20 61 3b 0a 20 20 2f 2a 20 73 3a    s = a;.  /* s:
b5400 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 6d 61   p0<<14 | p2 (ma
b5410 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 70 2b 2b 3b  sked) */..  p++;
b5420 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20  .  a = a<<14;.  
b5430 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a  a |= *p;.  /* a:
b5440 20 70 30 3c 3c 32 38 20 7c 20 70 32 3c 3c 31 34   p0<<28 | p2<<14
b5450 20 7c 20 70 34 20 28 75 6e 6d 61 73 6b 65 64 29   | p4 (unmasked)
b5460 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78   */.  if (!(a&0x
b5470 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20  80)).  {.    /* 
b5480 77 65 20 63 61 6e 20 73 6b 69 70 20 74 68 65 73  we can skip thes
b5490 65 20 63 61 75 73 65 20 74 68 65 79 20 77 65 72  e cause they wer
b54a0 65 20 28 65 66 66 65 63 74 69 76 65 6c 79 29 20  e (effectively) 
b54b0 64 6f 6e 65 20 61 62 6f 76 65 20 69 6e 20 63 61  done above in ca
b54c0 6c 63 27 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20  lc'ing s */.    
b54d0 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32  /* a &= (0x7f<<2
b54e0 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30  8)|(0x7f<<14)|(0
b54f0 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 2f 2a 20  x7f); */.    /* 
b5500 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c  b &= (0x7f<<14)|
b5510 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 62  (0x7f); */.    b
b5520 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c   = b<<7;.    a |
b5530 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e  = b;.    s = s>>
b5540 31 38 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75  18;.    *v = ((u
b5550 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20  64)s)<<32 | a;. 
b5560 20 20 20 72 65 74 75 72 6e 20 35 3b 0a 20 20 7d     return 5;.  }
b5570 0a 0a 20 20 2f 2a 20 32 3a 73 61 76 65 20 6f 66  ..  /* 2:save of
b5580 66 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31  f p0<<21 | p1<<1
b5590 34 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28  4 | p2<<7 | p3 (
b55a0 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 73 20 3d  masked) */.  s =
b55b0 20 73 3c 3c 37 3b 0a 20 20 73 20 7c 3d 20 62 3b   s<<7;.  s |= b;
b55c0 0a 20 20 2f 2a 20 73 3a 20 70 30 3c 3c 32 31 20  .  /* s: p0<<21 
b55d0 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37  | p1<<14 | p2<<7
b55e0 20 7c 20 70 33 20 28 6d 61 73 6b 65 64 29 20 2a   | p3 (masked) *
b55f0 2f 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20  /..  p++;.  b = 
b5600 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70  b<<14;.  b |= *p
b5610 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 32 38  ;.  /* b: p1<<28
b5620 20 7c 20 70 33 3c 3c 31 34 20 7c 20 70 35 20 28   | p3<<14 | p5 (
b5630 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69  unmasked) */.  i
b5640 66 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 20  f (!(b&0x80)).  
b5650 7b 0a 20 20 20 20 2f 2a 20 77 65 20 63 61 6e 20  {.    /* we can 
b5660 73 6b 69 70 20 74 68 69 73 20 63 61 75 73 65 20  skip this cause 
b5670 69 74 20 77 61 73 20 28 65 66 66 65 63 74 69 76  it was (effectiv
b5680 65 6c 79 29 20 64 6f 6e 65 20 61 62 6f 76 65 20  ely) done above 
b5690 69 6e 20 63 61 6c 63 27 69 6e 67 20 73 20 2a 2f  in calc'ing s */
b56a0 0a 20 20 20 20 2f 2a 20 62 20 26 3d 20 28 30 78  .    /* b &= (0x
b56b0 37 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31  7f<<28)|(0x7f<<1
b56c0 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20  4)|(0x7f); */.  
b56d0 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34    a &= (0x7f<<14
b56e0 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20  )|(0x7f);.    a 
b56f0 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d  = a<<7;.    a |=
b5700 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 31   b;.    s = s>>1
b5710 38 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36  8;.    *v = ((u6
b5720 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20  4)s)<<32 | a;.  
b5730 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 7d 0a    return 6;.  }.
b5740 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c  .  p++;.  a = a<
b5750 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a  <14;.  a |= *p;.
b5760 20 20 2f 2a 20 61 3a 20 70 32 3c 3c 32 38 20 7c    /* a: p2<<28 |
b5770 20 70 34 3c 3c 31 34 20 7c 20 70 36 20 28 75 6e   p4<<14 | p6 (un
b5780 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20  masked) */.  if 
b5790 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a  (!(a&0x80)).  {.
b57a0 20 20 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c      a &= (0x7f<<
b57b0 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28  28)|(0x7f<<14)|(
b57c0 30 78 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20  0x7f);.    b &= 
b57d0 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66  (0x7f<<14)|(0x7f
b57e0 29 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b  );.    b = b<<7;
b57f0 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20  .    a |= b;.   
b5800 20 73 20 3d 20 73 3e 3e 31 31 3b 0a 20 20 20 20   s = s>>11;.    
b5810 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33  *v = ((u64)s)<<3
b5820 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72  2 | a;.    retur
b5830 6e 20 37 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  n 7;.  }..  /* C
b5840 53 45 32 20 66 72 6f 6d 20 62 65 6c 6f 77 20 2a  SE2 from below *
b5850 2f 0a 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c  /.  a &= (0x7f<<
b5860 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 70 2b  14)|(0x7f);.  p+
b5870 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a  +;.  b = b<<14;.
b5880 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20    b |= *p;.  /* 
b5890 62 3a 20 70 33 3c 3c 32 38 20 7c 20 70 35 3c 3c  b: p3<<28 | p5<<
b58a0 31 34 20 7c 20 70 37 20 28 75 6e 6d 61 73 6b 65  14 | p7 (unmaske
b58b0 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26  d) */.  if (!(b&
b58c0 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 62  0x80)).  {.    b
b58d0 20 26 3d 20 28 30 78 37 66 3c 3c 32 38 29 7c 28   &= (0x7f<<28)|(
b58e0 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29  0x7f<<14)|(0x7f)
b58f0 3b 0a 20 20 20 20 2f 2a 20 6d 6f 76 65 64 20 43  ;.    /* moved C
b5900 53 45 32 20 75 70 20 2a 2f 0a 20 20 20 20 2f 2a  SE2 up */.    /*
b5910 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29   a &= (0x7f<<14)
b5920 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20  |(0x7f); */.    
b5930 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20  a = a<<7;.    a 
b5940 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e  |= b;.    s = s>
b5950 3e 34 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75  >4;.    *v = ((u
b5960 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20  64)s)<<32 | a;. 
b5970 20 20 20 72 65 74 75 72 6e 20 38 3b 0a 20 20 7d     return 8;.  }
b5980 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61  ..  p++;.  a = a
b5990 3c 3c 31 35 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b  <<15;.  a |= *p;
b59a0 0a 20 20 2f 2a 20 61 3a 20 70 34 3c 3c 32 39 20  .  /* a: p4<<29 
b59b0 7c 20 70 36 3c 3c 31 35 20 7c 20 70 38 20 28 75  | p6<<15 | p8 (u
b59c0 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 2f  nmasked) */..  /
b59d0 2a 20 6d 6f 76 65 64 20 43 53 45 32 20 75 70 20  * moved CSE2 up 
b59e0 2a 2f 0a 20 20 2f 2a 20 61 20 26 3d 20 28 30 78  */.  /* a &= (0x
b59f0 37 66 3c 3c 32 39 29 7c 28 30 78 37 66 3c 3c 31  7f<<29)|(0x7f<<1
b5a00 35 29 7c 28 30 78 66 66 29 3b 20 2a 2f 0a 20 20  5)|(0xff); */.  
b5a10 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c  b &= (0x7f<<14)|
b5a20 28 30 78 37 66 29 3b 0a 20 20 62 20 3d 20 62 3c  (0x7f);.  b = b<
b5a30 3c 38 3b 0a 20 20 61 20 7c 3d 20 62 3b 0a 0a 20  <8;.  a |= b;.. 
b5a40 20 73 20 3d 20 73 3c 3c 34 3b 0a 20 20 62 20 3d   s = s<<4;.  b =
b5a50 20 70 5b 2d 34 5d 3b 0a 20 20 62 20 26 3d 20 30   p[-4];.  b &= 0
b5a60 78 37 66 3b 0a 20 20 62 20 3d 20 62 3e 3e 33 3b  x7f;.  b = b>>3;
b5a70 0a 20 20 73 20 7c 3d 20 62 3b 0a 0a 20 20 2a 76  .  s |= b;..  *v
b5a80 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20   = ((u64)s)<<32 
b5a90 7c 20 61 3b 0a 0a 20 20 72 65 74 75 72 6e 20 39  | a;..  return 9
b5aa0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  ;.}../*.** Read 
b5ab0 61 20 33 32 2d 62 69 74 20 76 61 72 69 61 62 6c  a 32-bit variabl
b5ac0 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e-length integer
b5ad0 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 61   from memory sta
b5ae0 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a  rting at p[0]..*
b5af0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
b5b00 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 61  ber of bytes rea
b5b10 64 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73  d.  The value is
b5b20 20 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e 0a 2a   stored in *v..*
b5b30 2a 20 41 20 4d 41 43 52 4f 20 76 65 72 73 69 6f  * A MACRO versio
b5b40 6e 2c 20 67 65 74 56 61 72 69 6e 74 33 32 2c 20  n, getVarint32, 
b5b50 69 73 20 70 72 6f 76 69 64 65 64 20 77 68 69 63  is provided whic
b5b60 68 20 69 6e 6c 69 6e 65 73 20 74 68 65 20 0a 2a  h inlines the .*
b5b70 2a 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 61  * single-byte ca
b5b80 73 65 2e 20 20 41 6c 6c 20 63 6f 64 65 20 73 68  se.  All code sh
b5b90 6f 75 6c 64 20 75 73 65 20 74 68 65 20 4d 41 43  ould use the MAC
b5ba0 52 4f 20 76 65 72 73 69 6f 6e 20 61 73 20 0a 2a  RO version as .*
b5bb0 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * this function 
b5bc0 61 73 73 75 6d 65 73 20 74 68 65 20 73 69 6e 67  assumes the sing
b5bd0 6c 65 2d 62 79 74 65 20 63 61 73 65 20 68 61 73  le-byte case has
b5be0 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 68 61   already been ha
b5bf0 6e 64 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ndled..*/.SQLITE
b5c00 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
b5c10 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28  ite3GetVarint32(
b5c20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
b5c30 68 61 72 20 2a 70 2c 20 75 33 32 20 2a 76 29 7b  har *p, u32 *v){
b5c40 0a 20 20 75 33 32 20 61 2c 62 3b 0a 0a 20 20 61  .  u32 a,b;..  a
b5c50 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70   = *p;.  /* a: p
b5c60 30 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a  0 (unmasked) */.
b5c70 23 69 66 6e 64 65 66 20 67 65 74 56 61 72 69 6e  #ifndef getVarin
b5c80 74 33 32 0a 20 20 69 66 20 28 21 28 61 26 30 78  t32.  if (!(a&0x
b5c90 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2a 76 20  80)).  {.    *v 
b5ca0 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = a;.    return 
b5cb0 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  1;.  }.#endif.. 
b5cc0 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a   p++;.  b = *p;.
b5cd0 20 20 2f 2a 20 62 3a 20 70 31 20 28 75 6e 6d 61    /* b: p1 (unma
b5ce0 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21  sked) */.  if (!
b5cf0 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20  (b&0x80)).  {.  
b5d00 20 20 61 20 26 3d 20 30 78 37 66 3b 0a 20 20 20    a &= 0x7f;.   
b5d10 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 2a   a = a<<7;.    *
b5d20 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72  v = a | b;.    r
b5d30 65 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20 20  eturn 2;.  }..  
b5d40 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34  p++;.  a = a<<14
b5d50 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f  ;.  a |= *p;.  /
b5d60 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32  * a: p0<<14 | p2
b5d70 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20   (unmasked) */. 
b5d80 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a   if (!(a&0x80)).
b5d90 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 28 30 78    {.    a &= (0x
b5da0 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a  7f<<14)|(0x7f);.
b5db0 20 20 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20      b &= 0x7f;. 
b5dc0 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20     b = b<<7;.   
b5dd0 20 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20   *v = a | b;.   
b5de0 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 7d 0a 0a   return 3;.  }..
b5df0 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c    p++;.  b = b<<
b5e00 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20  14;.  b |= *p;. 
b5e10 20 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20   /* b: p1<<14 | 
b5e20 70 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f  p3 (unmasked) */
b5e30 0a 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29  .  if (!(b&0x80)
b5e40 29 0a 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28  ).  {.    b &= (
b5e50 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29  0x7f<<14)|(0x7f)
b5e60 3b 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37 66  ;.    a &= (0x7f
b5e70 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20  <<14)|(0x7f);.  
b5e80 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20    a = a<<7;.    
b5e90 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20  *v = a | b;.    
b5ea0 72 65 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20  return 4;.  }.. 
b5eb0 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31   p++;.  a = a<<1
b5ec0 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20  4;.  a |= *p;.  
b5ed0 2f 2a 20 61 3a 20 70 30 3c 3c 32 38 20 7c 20 70  /* a: p0<<28 | p
b5ee0 32 3c 3c 31 34 20 7c 20 70 34 20 28 75 6e 6d 61  2<<14 | p4 (unma
b5ef0 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21  sked) */.  if (!
b5f00 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20  (a&0x80)).  {.  
b5f10 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32 38    a &= (0x7f<<28
b5f20 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78  )|(0x7f<<14)|(0x
b5f30 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 28 30  7f);.    b &= (0
b5f40 78 37 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c  x7f<<28)|(0x7f<<
b5f50 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20  14)|(0x7f);.    
b5f60 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 2a 76  b = b<<7;.    *v
b5f70 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65   = a | b;.    re
b5f80 74 75 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f  turn 5;.  }..  /
b5f90 2a 20 57 65 20 63 61 6e 20 6f 6e 6c 79 20 72 65  * We can only re
b5fa0 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 20 77  ach this point w
b5fb0 68 65 6e 20 72 65 61 64 69 6e 67 20 61 20 63 6f  hen reading a co
b5fc0 72 72 75 70 74 20 64 61 74 61 62 61 73 65 0a 20  rrupt database. 
b5fd0 20 2a 2a 20 66 69 6c 65 2e 20 20 49 6e 20 74 68   ** file.  In th
b5fe0 61 74 20 63 61 73 65 20 77 65 20 61 72 65 20 6e  at case we are n
b5ff0 6f 74 20 69 6e 20 61 6e 79 20 68 75 72 72 79 2e  ot in any hurry.
b6000 20 20 55 73 65 20 74 68 65 20 28 72 65 6c 61 74    Use the (relat
b6010 69 76 65 6c 79 0a 20 20 2a 2a 20 73 6c 6f 77 29  ively.  ** slow)
b6020 20 67 65 6e 65 72 61 6c 2d 70 75 72 70 6f 73 65   general-purpose
b6030 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e   sqlite3GetVarin
b6040 74 28 29 20 72 6f 75 74 69 6e 65 20 74 6f 20 65  t() routine to e
b6050 78 74 72 61 63 74 20 74 68 65 0a 20 20 2a 2a 20  xtract the.  ** 
b6060 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 7b 0a 20 20  value. */.  {.  
b6070 20 20 75 36 34 20 76 36 34 3b 0a 20 20 20 20 69    u64 v64;.    i
b6080 6e 74 20 6e 3b 0a 0a 20 20 20 20 70 20 2d 3d 20  nt n;..    p -= 
b6090 34 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74  4;.    n = sqlit
b60a0 65 33 47 65 74 56 61 72 69 6e 74 28 70 2c 20 26  e3GetVarint(p, &
b60b0 76 36 34 29 3b 0a 20 20 20 20 61 73 73 65 72 74  v64);.    assert
b60c0 28 20 6e 3e 35 20 26 26 20 6e 3c 3d 39 20 29 3b  ( n>5 && n<=9 );
b60d0 0a 20 20 20 20 2a 76 20 3d 20 28 75 33 32 29 76  .    *v = (u32)v
b60e0 36 34 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 6e  64;.    return n
b60f0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ;.  }.}../*.** R
b6100 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
b6110 20 6f 66 20 62 79 74 65 73 20 74 68 61 74 20 77   of bytes that w
b6120 69 6c 6c 20 62 65 20 6e 65 65 64 65 64 20 74 6f  ill be needed to
b6130 20 73 74 6f 72 65 20 74 68 65 20 67 69 76 65 6e   store the given
b6140 0a 2a 2a 20 36 34 2d 62 69 74 20 69 6e 74 65 67  .** 64-bit integ
b6150 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
b6160 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b6170 33 56 61 72 69 6e 74 4c 65 6e 28 75 36 34 20 76  3VarintLen(u64 v
b6180 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a  ){.  int i = 0;.
b6190 20 20 64 6f 7b 0a 20 20 20 20 69 2b 2b 3b 0a 20    do{.    i++;. 
b61a0 20 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77     v >>= 7;.  }w
b61b0 68 69 6c 65 28 20 76 21 3d 30 20 26 26 20 69 3c  hile( v!=0 && i<
b61c0 39 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b  9 );.  return i;
b61d0 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  .}.../*.** Read 
b61e0 6f 72 20 77 72 69 74 65 20 61 20 66 6f 75 72 2d  or write a four-
b61f0 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20  byte big-endian 
b6200 69 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 0a 2a  integer value..*
b6210 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b6220 20 75 33 32 20 73 71 6c 69 74 65 33 47 65 74 34   u32 sqlite3Get4
b6230 62 79 74 65 28 63 6f 6e 73 74 20 75 38 20 2a 70  byte(const u8 *p
b6240 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 5b 30  ){.  return (p[0
b6250 5d 3c 3c 32 34 29 20 7c 20 28 70 5b 31 5d 3c 3c  ]<<24) | (p[1]<<
b6260 31 36 29 20 7c 20 28 70 5b 32 5d 3c 3c 38 29 20  16) | (p[2]<<8) 
b6270 7c 20 70 5b 33 5d 3b 0a 7d 0a 53 51 4c 49 54 45  | p[3];.}.SQLITE
b6280 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
b6290 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 75 6e  lite3Put4byte(un
b62a0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20  signed char *p, 
b62b0 75 33 32 20 76 29 7b 0a 20 20 70 5b 30 5d 20 3d  u32 v){.  p[0] =
b62c0 20 76 3e 3e 32 34 3b 0a 20 20 70 5b 31 5d 20 3d   v>>24;.  p[1] =
b62d0 20 76 3e 3e 31 36 3b 0a 20 20 70 5b 32 5d 20 3d   v>>16;.  p[2] =
b62e0 20 76 3e 3e 38 3b 0a 20 20 70 5b 33 5d 20 3d 20   v>>8;.  p[3] = 
b62f0 76 3b 0a 7d 0a 0a 0a 0a 23 69 66 20 21 64 65 66  v;.}....#if !def
b6300 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
b6310 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 29 20 7c  _BLOB_LITERAL) |
b6320 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
b6330 5f 48 41 53 5f 43 4f 44 45 43 29 0a 2f 2a 0a 2a  _HAS_CODEC)./*.*
b6340 2a 20 54 72 61 6e 73 6c 61 74 65 20 61 20 73 69  * Translate a si
b6350 6e 67 6c 65 20 62 79 74 65 20 6f 66 20 48 65 78  ngle byte of Hex
b6360 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72   into an integer
b6370 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
b6380 65 6e 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 69 66  en only works if
b6390 20 68 20 72 65 61 6c 6c 79 20 69 73 20 61 20 76   h really is a v
b63a0 61 6c 69 64 20 68 65 78 61 64 65 63 69 6d 61 6c  alid hexadecimal
b63b0 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 3a 20 20  .** character:  
b63c0 30 2e 2e 39 61 2e 2e 66 41 2e 2e 46 0a 2a 2f 0a  0..9a..fA..F.*/.
b63d0 73 74 61 74 69 63 20 69 6e 74 20 68 65 78 54 6f  static int hexTo
b63e0 49 6e 74 28 69 6e 74 20 68 29 7b 0a 20 20 61 73  Int(int h){.  as
b63f0 73 65 72 74 28 20 28 68 3e 3d 27 30 27 20 26 26  sert( (h>='0' &&
b6400 20 68 3c 3d 27 39 27 29 20 7c 7c 20 20 28 68 3e   h<='9') ||  (h>
b6410 3d 27 61 27 20 26 26 20 68 3c 3d 27 66 27 29 20  ='a' && h<='f') 
b6420 7c 7c 20 20 28 68 3e 3d 27 41 27 20 26 26 20 68  ||  (h>='A' && h
b6430 3c 3d 27 46 27 29 20 29 3b 0a 23 69 66 64 65 66  <='F') );.#ifdef
b6440 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 20 20   SQLITE_ASCII.  
b6450 68 20 2b 3d 20 39 2a 28 31 26 28 68 3e 3e 36 29  h += 9*(1&(h>>6)
b6460 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  );.#endif.#ifdef
b6470 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 0a 20   SQLITE_EBCDIC. 
b6480 20 68 20 2b 3d 20 39 2a 28 31 26 7e 28 68 3e 3e   h += 9*(1&~(h>>
b6490 34 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65  4));.#endif.  re
b64a0 74 75 72 6e 20 68 20 26 20 30 78 66 3b 0a 7d 0a  turn h & 0xf;.}.
b64b0 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54  #endif /* !SQLIT
b64c0 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45  E_OMIT_BLOB_LITE
b64d0 52 41 4c 20 7c 7c 20 53 51 4c 49 54 45 5f 48 41  RAL || SQLITE_HA
b64e0 53 5f 43 4f 44 45 43 20 2a 2f 0a 0a 23 69 66 20  S_CODEC */..#if 
b64f0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
b6500 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41  OMIT_BLOB_LITERA
b6510 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  L) || defined(SQ
b6520 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 29 0a  LITE_HAS_CODEC).
b6530 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20  /*.** Convert a 
b6540 42 4c 4f 42 20 6c 69 74 65 72 61 6c 20 6f 66 20  BLOB literal of 
b6550 74 68 65 20 66 6f 72 6d 20 22 78 27 68 68 68 68  the form "x'hhhh
b6560 68 68 27 22 20 69 6e 74 6f 20 69 74 73 20 62 69  hh'" into its bi
b6570 6e 61 72 79 0a 2a 2a 20 76 61 6c 75 65 2e 20 20  nary.** value.  
b6580 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
b6590 20 74 6f 20 69 74 73 20 62 69 6e 61 72 79 20 76   to its binary v
b65a0 61 6c 75 65 2e 20 20 53 70 61 63 65 20 74 6f 20  alue.  Space to 
b65b0 68 6f 6c 64 20 74 68 65 0a 2a 2a 20 62 69 6e 61  hold the.** bina
b65c0 72 79 20 76 61 6c 75 65 20 68 61 73 20 62 65 65  ry value has bee
b65d0 6e 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  n obtained from 
b65e0 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20  malloc and must 
b65f0 62 65 20 66 72 65 65 64 20 62 79 0a 2a 2a 20 74  be freed by.** t
b6600 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69  he calling routi
b6610 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ne..*/.SQLITE_PR
b6620 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
b6630 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c  te3HexToBlob(sql
b6640 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
b6650 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b  char *z, int n){
b6660 0a 20 20 63 68 61 72 20 2a 7a 42 6c 6f 62 3b 0a  .  char *zBlob;.
b6670 20 20 69 6e 74 20 69 3b 0a 0a 20 20 7a 42 6c 6f    int i;..  zBlo
b6680 62 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69  b = (char *)sqli
b6690 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
b66a0 62 2c 20 6e 2f 32 20 2b 20 31 29 3b 0a 20 20 6e  b, n/2 + 1);.  n
b66b0 2d 2d 3b 0a 20 20 69 66 28 20 7a 42 6c 6f 62 20  --;.  if( zBlob 
b66c0 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ){.    for(i=0; 
b66d0 69 3c 6e 3b 20 69 2b 3d 32 29 7b 0a 20 20 20 20  i<n; i+=2){.    
b66e0 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d 20 3d 20 28    zBlob[i/2] = (
b66f0 68 65 78 54 6f 49 6e 74 28 7a 5b 69 5d 29 3c 3c  hexToInt(z[i])<<
b6700 34 29 20 7c 20 68 65 78 54 6f 49 6e 74 28 7a 5b  4) | hexToInt(z[
b6710 69 2b 31 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20  i+1]);.    }.   
b6720 20 7a 42 6c 6f 62 5b 69 2f 32 5d 20 3d 20 30 3b   zBlob[i/2] = 0;
b6730 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 42  .  }.  return zB
b6740 6c 6f 62 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  lob;.}.#endif /*
b6750 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c   !SQLITE_OMIT_BL
b6760 4f 42 5f 4c 49 54 45 52 41 4c 20 7c 7c 20 53 51  OB_LITERAL || SQ
b6770 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 20 2a  LITE_HAS_CODEC *
b6780 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  /.../*.** Change
b6790 20 74 68 65 20 73 71 6c 69 74 65 2e 6d 61 67 69   the sqlite.magi
b67a0 63 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f 4d 41  c from SQLITE_MA
b67b0 47 49 43 5f 4f 50 45 4e 20 74 6f 20 53 51 4c 49  GIC_OPEN to SQLI
b67c0 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 2e 0a 2a  TE_MAGIC_BUSY..*
b67d0 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  * Return an erro
b67e0 72 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20  r (non-zero) if 
b67f0 74 68 65 20 6d 61 67 69 63 20 77 61 73 20 6e 6f  the magic was no
b6800 74 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f  t SQLITE_MAGIC_O
b6810 50 45 4e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73  PEN.** when this
b6820 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
b6830 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ed..**.** This r
b6840 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
b6850 20 77 68 65 6e 20 65 6e 74 65 72 69 6e 67 20 61   when entering a
b6860 6e 20 53 51 4c 69 74 65 20 41 50 49 2e 20 20 54  n SQLite API.  T
b6870 68 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  he SQLITE_MAGIC_
b6880 4f 50 45 4e 0a 2a 2a 20 76 61 6c 75 65 20 69 6e  OPEN.** value in
b6890 64 69 63 61 74 65 73 20 74 68 61 74 20 74 68 65  dicates that the
b68a0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
b68b0 74 69 6f 6e 20 70 61 73 73 65 64 20 69 6e 74 6f  tion passed into
b68c0 20 74 68 65 20 41 50 49 20 69 73 0a 2a 2a 20 6f   the API is.** o
b68d0 70 65 6e 20 61 6e 64 20 69 73 20 6e 6f 74 20 62  pen and is not b
b68e0 65 69 6e 67 20 75 73 65 64 20 62 79 20 61 6e 6f  eing used by ano
b68f0 74 68 65 72 20 74 68 72 65 61 64 2e 20 20 42 79  ther thread.  By
b6900 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 76 61   changing the va
b6910 6c 75 65 0a 2a 2a 20 74 6f 20 53 51 4c 49 54 45  lue.** to SQLITE
b6920 5f 4d 41 47 49 43 5f 42 55 53 59 20 77 65 20 69  _MAGIC_BUSY we i
b6930 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68 65  ndicate that the
b6940 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69   connection is i
b6950 6e 20 75 73 65 2e 0a 2a 2a 20 73 71 6c 69 74 65  n use..** sqlite
b6960 33 53 61 66 65 74 79 4f 66 66 28 29 20 62 65 6c  3SafetyOff() bel
b6970 6f 77 20 77 69 6c 6c 20 63 68 61 6e 67 65 20 74  ow will change t
b6980 68 65 20 76 61 6c 75 65 20 62 61 63 6b 20 74 6f  he value back to
b6990 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50   SQLITE_MAGIC_OP
b69a0 45 4e 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 41  EN.** when the A
b69b0 50 49 20 65 78 69 74 73 2e 20 0a 2a 2a 0a 2a 2a  PI exits. .**.**
b69c0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
b69d0 20 61 20 61 74 74 65 6d 70 74 20 74 6f 20 64 65   a attempt to de
b69e0 74 65 63 74 20 69 66 20 74 77 6f 20 74 68 72 65  tect if two thre
b69f0 61 64 73 20 75 73 65 20 74 68 65 0a 2a 2a 20 73  ads use the.** s
b6a00 61 6d 65 20 73 71 6c 69 74 65 2a 20 70 6f 69 6e  ame sqlite* poin
b6a10 74 65 72 20 61 74 20 74 68 65 20 73 61 6d 65 20  ter at the same 
b6a20 74 69 6d 65 2e 20 20 54 68 65 72 65 20 69 73 20  time.  There is 
b6a30 61 20 72 61 63 65 20 0a 2a 2a 20 63 6f 6e 64 69  a race .** condi
b6a40 74 69 6f 6e 20 73 6f 20 69 74 20 69 73 20 70 6f  tion so it is po
b6a50 73 73 69 62 6c 65 20 74 68 61 74 20 74 68 65 20  ssible that the 
b6a60 65 72 72 6f 72 20 69 73 20 6e 6f 74 20 64 65 74  error is not det
b6a70 65 63 74 65 64 2e 0a 2a 2a 20 42 75 74 20 75 73  ected..** But us
b6a80 75 61 6c 6c 79 20 74 68 65 20 70 72 6f 62 6c 65  ually the proble
b6a90 6d 20 77 69 6c 6c 20 62 65 20 73 65 65 6e 2e 20  m will be seen. 
b6aa0 20 54 68 65 20 72 65 73 75 6c 74 20 77 69 6c 6c   The result will
b6ab0 20 62 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20   be an.** error 
b6ac0 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 65  which can be use
b6ad0 64 20 74 6f 20 64 65 62 75 67 20 74 68 65 20 61  d to debug the a
b6ae0 70 70 6c 69 63 61 74 69 6f 6e 20 74 68 61 74 20  pplication that 
b6af0 69 73 0a 2a 2a 20 75 73 69 6e 67 20 53 51 4c 69  is.** using SQLi
b6b00 74 65 20 69 6e 63 6f 72 72 65 63 74 6c 79 2e 0a  te incorrectly..
b6b10 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 32 30  **.** Ticket #20
b6b20 32 3a 20 20 49 66 20 64 62 2d 3e 6d 61 67 69 63  2:  If db->magic
b6b30 20 69 73 20 6e 6f 74 20 61 20 76 61 6c 69 64 20   is not a valid 
b6b40 6f 70 65 6e 20 76 61 6c 75 65 2c 20 74 61 6b 65  open value, take
b6b50 20 63 61 72 65 20 6e 6f 74 0a 2a 2a 20 74 6f 20   care not.** to 
b6b60 6d 6f 64 69 66 79 20 74 68 65 20 64 62 20 73 74  modify the db st
b6b70 72 75 63 74 75 72 65 20 61 74 20 61 6c 6c 2e 20  ructure at all. 
b6b80 20 49 74 20 63 6f 75 6c 64 20 62 65 20 74 68 61   It could be tha
b6b90 74 20 64 62 20 69 73 20 61 20 73 74 61 6c 65 0a  t db is a stale.
b6ba0 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 20 49 6e 20  ** pointer.  In 
b6bb0 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 74 20  other words, it 
b6bc0 63 6f 75 6c 64 20 62 65 20 74 68 61 74 20 74 68  could be that th
b6bd0 65 72 65 20 68 61 73 20 62 65 65 6e 20 61 20 70  ere has been a p
b6be0 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20  rior.** call to 
b6bf0 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 64 62  sqlite3_close(db
b6c00 29 20 61 6e 64 20 64 62 20 68 61 73 20 62 65 65  ) and db has bee
b6c10 6e 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20  n deallocated.  
b6c20 41 6e 64 20 77 65 20 64 6f 0a 2a 2a 20 6e 6f 74  And we do.** not
b6c30 20 77 61 6e 74 20 74 6f 20 77 72 69 74 65 20 69   want to write i
b6c40 6e 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 64 20  nto deallocated 
b6c50 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 23 69 66 64 65  memory..*/.#ifde
b6c60 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53  f SQLITE_DEBUG.S
b6c70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
b6c80 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  t sqlite3SafetyO
b6c90 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  n(sqlite3 *db){.
b6ca0 20 20 69 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d    if( db->magic=
b6cb0 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50  =SQLITE_MAGIC_OP
b6cc0 45 4e 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61  EN ){.    db->ma
b6cd0 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47  gic = SQLITE_MAG
b6ce0 49 43 5f 42 55 53 59 3b 0a 20 20 20 20 61 73 73  IC_BUSY;.    ass
b6cf0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
b6d00 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
b6d10 78 29 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  x) );.    return
b6d20 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   0;.  }else if( 
b6d30 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54  db->magic==SQLIT
b6d40 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 7b 0a  E_MAGIC_BUSY ){.
b6d50 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20      db->magic = 
b6d60 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 52  SQLITE_MAGIC_ERR
b6d70 4f 52 3b 0a 20 20 20 20 64 62 2d 3e 75 31 2e 69  OR;.    db->u1.i
b6d80 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20 31  sInterrupted = 1
b6d90 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31  ;.  }.  return 1
b6da0 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
b6db0 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6d 61 67  * Change the mag
b6dc0 69 63 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f 4d  ic from SQLITE_M
b6dd0 41 47 49 43 5f 42 55 53 59 20 74 6f 20 53 51 4c  AGIC_BUSY to SQL
b6de0 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 2e 0a  ITE_MAGIC_OPEN..
b6df0 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72  ** Return an err
b6e00 6f 72 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66  or (non-zero) if
b6e10 20 74 68 65 20 6d 61 67 69 63 20 77 61 73 20 6e   the magic was n
b6e20 6f 74 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  ot SQLITE_MAGIC_
b6e30 42 55 53 59 0a 2a 2a 20 77 68 65 6e 20 74 68 69  BUSY.** when thi
b6e40 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
b6e50 6c 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  led..*/.#ifdef S
b6e60 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49  QLITE_DEBUG.SQLI
b6e70 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
b6e80 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
b6e90 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
b6ea0 69 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53  if( db->magic==S
b6eb0 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59  QLITE_MAGIC_BUSY
b6ec0 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69   ){.    db->magi
b6ed0 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  c = SQLITE_MAGIC
b6ee0 5f 4f 50 45 4e 3b 0a 20 20 20 20 61 73 73 65 72  _OPEN;.    asser
b6ef0 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
b6f00 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
b6f10 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   );.    return 0
b6f20 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64  ;.  }else{.    d
b6f30 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54  b->magic = SQLIT
b6f40 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a 20  E_MAGIC_ERROR;. 
b6f50 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65     db->u1.isInte
b6f60 72 72 75 70 74 65 64 20 3d 20 31 3b 0a 20 20 20  rrupted = 1;.   
b6f70 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d   return 1;.  }.}
b6f80 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43  .#endif../*.** C
b6f90 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72  heck to make sur
b6fa0 65 20 77 65 20 68 61 76 65 20 61 20 76 61 6c 69  e we have a vali
b6fb0 64 20 64 62 20 70 6f 69 6e 74 65 72 2e 20 20 54  d db pointer.  T
b6fc0 68 69 73 20 74 65 73 74 20 69 73 20 6e 6f 74 0a  his test is not.
b6fd0 2a 2a 20 66 6f 6f 6c 70 72 6f 6f 66 20 62 75 74  ** foolproof but
b6fe0 20 69 74 20 64 6f 65 73 20 70 72 6f 76 69 64 65   it does provide
b6ff0 20 73 6f 6d 65 20 6d 65 61 73 75 72 65 20 6f 66   some measure of
b7000 20 70 72 6f 74 65 63 74 69 6f 6e 20 61 67 61 69   protection agai
b7010 6e 73 74 0a 2a 2a 20 6d 69 73 75 73 65 20 6f 66  nst.** misuse of
b7020 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 73   the interface s
b7030 75 63 68 20 61 73 20 70 61 73 73 69 6e 67 20 69  uch as passing i
b7040 6e 20 64 62 20 70 6f 69 6e 74 65 72 73 20 74 68  n db pointers th
b7050 61 74 20 61 72 65 0a 2a 2a 20 4e 55 4c 4c 20 6f  at are.** NULL o
b7060 72 20 77 68 69 63 68 20 68 61 76 65 20 62 65 65  r which have bee
b7070 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6c 6f  n previously clo
b7080 73 65 64 2e 20 20 49 66 20 74 68 69 73 20 72 6f  sed.  If this ro
b7090 75 74 69 6e 65 20 72 65 74 75 72 6e 73 0a 2a 2a  utine returns.**
b70a0 20 31 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74   1 it means that
b70b0 20 74 68 65 20 64 62 20 70 6f 69 6e 74 65 72 20   the db pointer 
b70c0 69 73 20 76 61 6c 69 64 20 61 6e 64 20 30 20 69  is valid and 0 i
b70d0 66 20 69 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20  f it should not 
b70e0 62 65 0a 2a 2a 20 64 65 72 65 66 65 72 65 6e 63  be.** dereferenc
b70f0 65 64 20 66 6f 72 20 61 6e 79 20 72 65 61 73 6f  ed for any reaso
b7100 6e 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20  n.  The calling 
b7110 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20  function should 
b7120 69 6e 76 6f 6b 65 0a 2a 2a 20 53 51 4c 49 54 45  invoke.** SQLITE
b7130 5f 4d 49 53 55 53 45 20 69 6d 6d 65 64 69 61 74  _MISUSE immediat
b7140 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74  ely..**.** sqlit
b7150 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f 6b 28  e3SafetyCheckOk(
b7160 29 20 72 65 71 75 69 72 65 73 20 74 68 61 74 20  ) requires that 
b7170 74 68 65 20 64 62 20 70 6f 69 6e 74 65 72 20 62  the db pointer b
b7180 65 20 76 61 6c 69 64 20 66 6f 72 0a 2a 2a 20 75  e valid for.** u
b7190 73 65 2e 20 20 73 71 6c 69 74 65 33 53 61 66 65  se.  sqlite3Safe
b71a0 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28  tyCheckSickOrOk(
b71b0 29 20 61 6c 6c 6f 77 73 20 61 20 64 62 20 70 6f  ) allows a db po
b71c0 69 6e 74 65 72 20 74 68 61 74 20 66 61 69 6c 65  inter that faile
b71d0 64 20 74 6f 0a 2a 2a 20 6f 70 65 6e 20 70 72 6f  d to.** open pro
b71e0 70 65 72 6c 79 20 61 6e 64 20 69 73 20 6e 6f 74  perly and is not
b71f0 20 66 69 74 20 66 6f 72 20 67 65 6e 65 72 61 6c   fit for general
b7200 20 75 73 65 20 62 75 74 20 77 68 69 63 68 20 63   use but which c
b7210 61 6e 20 62 65 0a 2a 2a 20 75 73 65 64 20 61 73  an be.** used as
b7220 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20   an argument to 
b7230 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29  sqlite3_errmsg()
b7240 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73   or sqlite3_clos
b7250 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  e()..*/.SQLITE_P
b7260 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
b7270 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f 6b 28  e3SafetyCheckOk(
b7280 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
b7290 69 6e 74 20 6d 61 67 69 63 3b 0a 20 20 69 66 28  int magic;.  if(
b72a0 20 64 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 20   db==0 ) return 
b72b0 30 3b 0a 20 20 6d 61 67 69 63 20 3d 20 64 62 2d  0;.  magic = db-
b72c0 3e 6d 61 67 69 63 3b 0a 20 20 69 66 28 20 6d 61  >magic;.  if( ma
b72d0 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49  gic!=SQLITE_MAGI
b72e0 43 5f 4f 50 45 4e 20 26 26 0a 20 20 20 20 20 20  C_OPEN &&.      
b72f0 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41  magic!=SQLITE_MA
b7300 47 49 43 5f 42 55 53 59 20 29 20 72 65 74 75 72  GIC_BUSY ) retur
b7310 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  n 0;.  return 1;
b7320 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
b7330 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66  E int sqlite3Saf
b7340 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b  etyCheckSickOrOk
b7350 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
b7360 20 69 6e 74 20 6d 61 67 69 63 3b 0a 20 20 69 66   int magic;.  if
b7370 28 20 64 62 3d 3d 30 20 29 20 72 65 74 75 72 6e  ( db==0 ) return
b7380 20 30 3b 0a 20 20 6d 61 67 69 63 20 3d 20 64 62   0;.  magic = db
b7390 2d 3e 6d 61 67 69 63 3b 0a 20 20 69 66 28 20 6d  ->magic;.  if( m
b73a0 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47  agic!=SQLITE_MAG
b73b0 49 43 5f 53 49 43 4b 20 26 26 0a 20 20 20 20 20  IC_SICK &&.     
b73c0 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d   magic!=SQLITE_M
b73d0 41 47 49 43 5f 4f 50 45 4e 20 26 26 0a 20 20 20  AGIC_OPEN &&.   
b73e0 20 20 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45     magic!=SQLITE
b73f0 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 20 72 65  _MAGIC_BUSY ) re
b7400 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e  turn 0;.  return
b7410 20 31 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a   1;.}../********
b7420 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 75 74  ****** End of ut
b7430 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  il.c ***********
b7440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b7450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b7460 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
b7470 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
b7480 65 20 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a  e hash.c *******
b7490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b74a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b74b0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
b74c0 31 20 53 65 70 74 65 6d 62 65 72 20 32 32 0a 2a  1 September 22.*
b74d0 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
b74e0 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
b74f0 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
b7500 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
b7510 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
b7520 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
b7530 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
b7540 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
b7550 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
b7560 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
b7570 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
b7580 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
b7590 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
b75a0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
b75b0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
b75c0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
b75d0 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
b75e0 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
b75f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b7600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b7610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b7620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
b7630 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6d 70   This is the imp
b7640 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 67  lementation of g
b7650 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c  eneric hash-tabl
b7660 65 73 0a 2a 2a 20 75 73 65 64 20 69 6e 20 53 51  es.** used in SQ
b7670 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  Lite..**.** $Id:
b7680 20 68 61 73 68 2e 63 2c 76 20 31 2e 33 31 20 32   hash.c,v 1.31 2
b7690 30 30 38 2f 31 30 2f 31 30 20 31 37 3a 34 31 3a  008/10/10 17:41:
b76a0 32 39 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  29 drh Exp $.*/.
b76b0 0a 2f 2a 20 54 75 72 6e 20 62 75 6c 6b 20 6d 65  ./* Turn bulk me
b76c0 6d 6f 72 79 20 69 6e 74 6f 20 61 20 68 61 73 68  mory into a hash
b76d0 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 62 79   table object by
b76e0 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68   initializing th
b76f0 65 0a 2a 2a 20 66 69 65 6c 64 73 20 6f 66 20 74  e.** fields of t
b7700 68 65 20 48 61 73 68 20 73 74 72 75 63 74 75 72  he Hash structur
b7710 65 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e 65 77 22 20  e..**.** "pNew" 
b7720 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
b7730 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 74  the hash table t
b7740 68 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e 69  hat is to be ini
b7750 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 22 63 6f  tialized..** "co
b7760 70 79 4b 65 79 22 20 69 73 20 74 72 75 65 20 69  pyKey" is true i
b7770 66 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  f the hash table
b7780 20 73 68 6f 75 6c 64 20 6d 61 6b 65 20 69 74 73   should make its
b7790 20 6f 77 6e 20 70 72 69 76 61 74 65 0a 2a 2a 20   own private.** 
b77a0 63 6f 70 79 20 6f 66 20 6b 65 79 73 20 61 6e 64  copy of keys and
b77b0 20 66 61 6c 73 65 20 69 66 20 69 74 20 73 68 6f   false if it sho
b77c0 75 6c 64 20 6a 75 73 74 20 75 73 65 20 74 68 65  uld just use the
b77d0 20 73 75 70 70 6c 69 65 64 20 70 6f 69 6e 74 65   supplied pointe
b77e0 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
b77f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
b7800 33 48 61 73 68 49 6e 69 74 28 48 61 73 68 20 2a  3HashInit(Hash *
b7810 70 4e 65 77 2c 20 69 6e 74 20 63 6f 70 79 4b 65  pNew, int copyKe
b7820 79 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4e  y){.  assert( pN
b7830 65 77 21 3d 30 20 29 3b 0a 20 20 70 4e 65 77 2d  ew!=0 );.  pNew-
b7840 3e 63 6f 70 79 4b 65 79 20 3d 20 63 6f 70 79 4b  >copyKey = copyK
b7850 65 79 21 3d 30 3b 0a 20 20 70 4e 65 77 2d 3e 66  ey!=0;.  pNew->f
b7860 69 72 73 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77  irst = 0;.  pNew
b7870 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 70  ->count = 0;.  p
b7880 4e 65 77 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b  New->htsize = 0;
b7890 0a 20 20 70 4e 65 77 2d 3e 68 74 20 3d 20 30 3b  .  pNew->ht = 0;
b78a0 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 6c  .}../* Remove al
b78b0 6c 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 61  l entries from a
b78c0 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 52 65   hash table.  Re
b78d0 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79  claim all memory
b78e0 2e 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20 72  ..** Call this r
b78f0 6f 75 74 69 6e 65 20 74 6f 20 64 65 6c 65 74 65  outine to delete
b7900 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f 72   a hash table or
b7910 20 74 6f 20 72 65 73 65 74 20 61 20 68 61 73 68   to reset a hash
b7920 20 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 74 68 65   table.** to the
b7930 20 65 6d 70 74 79 20 73 74 61 74 65 2e 0a 2a 2f   empty state..*/
b7940 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b7950 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68  void sqlite3Hash
b7960 43 6c 65 61 72 28 48 61 73 68 20 2a 70 48 29 7b  Clear(Hash *pH){
b7970 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65  .  HashElem *ele
b7980 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f  m;         /* Fo
b7990 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61  r looping over a
b79a0 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74  ll elements of t
b79b0 68 65 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 61  he table */..  a
b79c0 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0a  ssert( pH!=0 );.
b79d0 20 20 65 6c 65 6d 20 3d 20 70 48 2d 3e 66 69 72    elem = pH->fir
b79e0 73 74 3b 0a 20 20 70 48 2d 3e 66 69 72 73 74 20  st;.  pH->first 
b79f0 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  = 0;.  sqlite3_f
b7a00 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70  ree(pH->ht);.  p
b7a10 48 2d 3e 68 74 20 3d 20 30 3b 0a 20 20 70 48 2d  H->ht = 0;.  pH-
b7a20 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 77  >htsize = 0;.  w
b7a30 68 69 6c 65 28 20 65 6c 65 6d 20 29 7b 0a 20 20  hile( elem ){.  
b7a40 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78 74    HashElem *next
b7a50 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65  _elem = elem->ne
b7a60 78 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d 3e  xt;.    if( pH->
b7a70 63 6f 70 79 4b 65 79 20 26 26 20 65 6c 65 6d 2d  copyKey && elem-
b7a80 3e 70 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 73  >pKey ){.      s
b7a90 71 6c 69 74 65 33 5f 66 72 65 65 28 65 6c 65 6d  qlite3_free(elem
b7aa0 2d 3e 70 4b 65 79 29 3b 0a 20 20 20 20 7d 0a 20  ->pKey);.    }. 
b7ab0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
b7ac0 65 6c 65 6d 29 3b 0a 20 20 20 20 65 6c 65 6d 20  elem);.    elem 
b7ad0 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0a 20 20 7d  = next_elem;.  }
b7ae0 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20 3d 20 30  .  pH->count = 0
b7af0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 61 73 68 20  ;.}../*.** Hash 
b7b00 61 6e 64 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66  and comparison f
b7b10 75 6e 63 74 69 6f 6e 73 20 77 68 65 6e 20 74 68  unctions when th
b7b20 65 20 6d 6f 64 65 20 69 73 20 53 51 4c 49 54 45  e mode is SQLITE
b7b30 5f 48 41 53 48 5f 53 54 52 49 4e 47 0a 2a 2f 0a  _HASH_STRING.*/.
b7b40 73 74 61 74 69 63 20 69 6e 74 20 73 74 72 48 61  static int strHa
b7b50 73 68 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  sh(const void *p
b7b60 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a  Key, int nKey){.
b7b70 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20    const char *z 
b7b80 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
b7b90 70 4b 65 79 3b 0a 20 20 69 6e 74 20 68 20 3d 20  pKey;.  int h = 
b7ba0 30 3b 0a 20 20 69 66 28 20 6e 4b 65 79 3c 3d 30  0;.  if( nKey<=0
b7bb0 20 29 20 6e 4b 65 79 20 3d 20 73 74 72 6c 65 6e   ) nKey = strlen
b7bc0 28 7a 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e 4b  (z);.  while( nK
b7bd0 65 79 20 3e 20 30 20 20 29 7b 0a 20 20 20 20 68  ey > 0  ){.    h
b7be0 20 3d 20 28 68 3c 3c 33 29 20 5e 20 68 20 5e 20   = (h<<3) ^ h ^ 
b7bf0 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f  sqlite3UpperToLo
b7c00 77 65 72 5b 28 75 6e 73 69 67 6e 65 64 20 63 68  wer[(unsigned ch
b7c10 61 72 29 2a 7a 2b 2b 5d 3b 0a 20 20 20 20 6e 4b  ar)*z++];.    nK
b7c20 65 79 2d 2d 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ey--;.  }.  retu
b7c30 72 6e 20 68 20 26 20 30 78 37 66 66 66 66 66 66  rn h & 0x7ffffff
b7c40 66 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  f;.}.static int 
b7c50 73 74 72 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74  strCompare(const
b7c60 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 69 6e   void *pKey1, in
b7c70 74 20 6e 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64  t n1, const void
b7c80 20 2a 70 4b 65 79 32 2c 20 69 6e 74 20 6e 32 29   *pKey2, int n2)
b7c90 7b 0a 20 20 69 66 28 20 6e 31 21 3d 6e 32 20 29  {.  if( n1!=n2 )
b7ca0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65 74   return 1;.  ret
b7cb0 75 72 6e 20 73 71 6c 69 74 65 33 53 74 72 4e 49  urn sqlite3StrNI
b7cc0 43 6d 70 28 28 63 6f 6e 73 74 20 63 68 61 72 2a  Cmp((const char*
b7cd0 29 70 4b 65 79 31 2c 28 63 6f 6e 73 74 20 63 68  )pKey1,(const ch
b7ce0 61 72 2a 29 70 4b 65 79 32 2c 6e 31 29 3b 0a 7d  ar*)pKey2,n1);.}
b7cf0 0a 0a 0a 2f 2a 20 4c 69 6e 6b 20 61 6e 20 65 6c  .../* Link an el
b7d00 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68  ement into the h
b7d10 61 73 68 20 74 61 62 6c 65 0a 2a 2f 0a 73 74 61  ash table.*/.sta
b7d20 74 69 63 20 76 6f 69 64 20 69 6e 73 65 72 74 45  tic void insertE
b7d30 6c 65 6d 65 6e 74 28 0a 20 20 48 61 73 68 20 2a  lement(.  Hash *
b7d40 70 48 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  pH,             
b7d50 20 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65   /* The complete
b7d60 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20   hash table */. 
b7d70 20 73 74 72 75 63 74 20 5f 68 74 20 2a 70 45 6e   struct _ht *pEn
b7d80 74 72 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 65  try,    /* The e
b7d90 6e 74 72 79 20 69 6e 74 6f 20 77 68 69 63 68 20  ntry into which 
b7da0 70 4e 65 77 20 69 73 20 69 6e 73 65 72 74 65 64  pNew is inserted
b7db0 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a   */.  HashElem *
b7dc0 70 4e 65 77 20 20 20 20 20 20 20 20 20 2f 2a 20  pNew         /* 
b7dd0 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 6f 20 62  The element to b
b7de0 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 29 7b  e inserted */.){
b7df0 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 48 65  .  HashElem *pHe
b7e00 61 64 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 72  ad;       /* Fir
b7e10 73 74 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61  st element alrea
b7e20 64 79 20 69 6e 20 70 45 6e 74 72 79 20 2a 2f 0a  dy in pEntry */.
b7e30 20 20 70 48 65 61 64 20 3d 20 70 45 6e 74 72 79    pHead = pEntry
b7e40 2d 3e 63 68 61 69 6e 3b 0a 20 20 69 66 28 20 70  ->chain;.  if( p
b7e50 48 65 61 64 20 29 7b 0a 20 20 20 20 70 4e 65 77  Head ){.    pNew
b7e60 2d 3e 6e 65 78 74 20 3d 20 70 48 65 61 64 3b 0a  ->next = pHead;.
b7e70 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d      pNew->prev =
b7e80 20 70 48 65 61 64 2d 3e 70 72 65 76 3b 0a 20 20   pHead->prev;.  
b7e90 20 20 69 66 28 20 70 48 65 61 64 2d 3e 70 72 65    if( pHead->pre
b7ea0 76 20 29 7b 20 70 48 65 61 64 2d 3e 70 72 65 76  v ){ pHead->prev
b7eb0 2d 3e 6e 65 78 74 20 3d 20 70 4e 65 77 3b 20 7d  ->next = pNew; }
b7ec0 0a 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 20  .    else       
b7ed0 20 20 20 20 20 20 7b 20 70 48 2d 3e 66 69 72 73        { pH->firs
b7ee0 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20  t = pNew; }.    
b7ef0 70 48 65 61 64 2d 3e 70 72 65 76 20 3d 20 70 4e  pHead->prev = pN
b7f00 65 77 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ew;.  }else{.   
b7f10 20 70 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48   pNew->next = pH
b7f20 2d 3e 66 69 72 73 74 3b 0a 20 20 20 20 69 66 28  ->first;.    if(
b7f30 20 70 48 2d 3e 66 69 72 73 74 20 29 7b 20 70 48   pH->first ){ pH
b7f40 2d 3e 66 69 72 73 74 2d 3e 70 72 65 76 20 3d 20  ->first->prev = 
b7f50 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70 4e 65 77  pNew; }.    pNew
b7f60 2d 3e 70 72 65 76 20 3d 20 30 3b 0a 20 20 20 20  ->prev = 0;.    
b7f70 70 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65 77  pH->first = pNew
b7f80 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 2d 3e  ;.  }.  pEntry->
b7f90 63 6f 75 6e 74 2b 2b 3b 0a 20 20 70 45 6e 74 72  count++;.  pEntr
b7fa0 79 2d 3e 63 68 61 69 6e 20 3d 20 70 4e 65 77 3b  y->chain = pNew;
b7fb0 0a 7d 0a 0a 0a 2f 2a 20 52 65 73 69 7a 65 20 74  .}.../* Resize t
b7fc0 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 73 6f  he hash table so
b7fd0 20 74 68 61 74 20 69 74 20 63 61 6e 74 61 69 6e   that it cantain
b7fe0 73 20 22 6e 65 77 5f 73 69 7a 65 22 20 62 75 63  s "new_size" buc
b7ff0 6b 65 74 73 2e 0a 2a 2a 20 22 6e 65 77 5f 73 69  kets..** "new_si
b8000 7a 65 22 20 6d 75 73 74 20 62 65 20 61 20 70 6f  ze" must be a po
b8010 77 65 72 20 6f 66 20 32 2e 20 20 54 68 65 20 68  wer of 2.  The h
b8020 61 73 68 20 74 61 62 6c 65 20 6d 69 67 68 74 20  ash table might 
b8030 66 61 69 6c 20 0a 2a 2a 20 74 6f 20 72 65 73 69  fail .** to resi
b8040 7a 65 20 69 66 20 73 71 6c 69 74 65 33 5f 6d 61  ze if sqlite3_ma
b8050 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f  lloc() fails..*/
b8060 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 68  .static void reh
b8070 61 73 68 28 48 61 73 68 20 2a 70 48 2c 20 69 6e  ash(Hash *pH, in
b8080 74 20 6e 65 77 5f 73 69 7a 65 29 7b 0a 20 20 73  t new_size){.  s
b8090 74 72 75 63 74 20 5f 68 74 20 2a 6e 65 77 5f 68  truct _ht *new_h
b80a0 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
b80b0 20 54 68 65 20 6e 65 77 20 68 61 73 68 20 74 61   The new hash ta
b80c0 62 6c 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65  ble */.  HashEle
b80d0 6d 20 2a 65 6c 65 6d 2c 20 2a 6e 65 78 74 5f 65  m *elem, *next_e
b80e0 6c 65 6d 3b 20 20 20 20 2f 2a 20 46 6f 72 20 6c  lem;    /* For l
b80f0 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65 78 69 73  ooping over exis
b8100 74 69 6e 67 20 65 6c 65 6d 65 6e 74 73 20 2a 2f  ting elements */
b8110 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
b8120 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49  MALLOC_SOFT_LIMI
b8130 54 0a 20 20 69 66 28 20 6e 65 77 5f 73 69 7a 65  T.  if( new_size
b8140 2a 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f  *sizeof(struct _
b8150 68 74 29 3e 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f  ht)>SQLITE_MALLO
b8160 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 20 29 7b 0a  C_SOFT_LIMIT ){.
b8170 20 20 20 20 6e 65 77 5f 73 69 7a 65 20 3d 20 53      new_size = S
b8180 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46  QLITE_MALLOC_SOF
b8190 54 5f 4c 49 4d 49 54 2f 73 69 7a 65 6f 66 28 73  T_LIMIT/sizeof(s
b81a0 74 72 75 63 74 20 5f 68 74 29 3b 0a 20 20 7d 0a  truct _ht);.  }.
b81b0 20 20 69 66 28 20 6e 65 77 5f 73 69 7a 65 3d 3d    if( new_size==
b81c0 70 48 2d 3e 68 74 73 69 7a 65 20 29 20 72 65 74  pH->htsize ) ret
b81d0 75 72 6e 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  urn;.#endif..  /
b81e0 2a 20 54 68 65 72 65 20 69 73 20 61 20 63 61 6c  * There is a cal
b81f0 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 61 6c  l to sqlite3_mal
b8200 6c 6f 63 28 29 20 69 6e 73 69 64 65 20 72 65 68  loc() inside reh
b8210 61 73 68 28 29 2e 20 49 66 20 74 68 65 72 65 20  ash(). If there 
b8220 69 73 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20  is.  ** already 
b8230 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74  an allocation at
b8240 20 70 48 2d 3e 68 74 2c 20 74 68 65 6e 20 69 66   pH->ht, then if
b8250 20 74 68 69 73 20 6d 61 6c 6c 6f 63 28 29 20 66   this malloc() f
b8260 61 69 6c 73 20 69 74 0a 20 20 2a 2a 20 69 73 20  ails it.  ** is 
b8270 62 65 6e 69 67 6e 20 28 73 69 6e 63 65 20 66 61  benign (since fa
b8280 69 6c 69 6e 67 20 74 6f 20 72 65 73 69 7a 65 20  iling to resize 
b8290 61 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20  a hash table is 
b82a0 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 0a 20 20  a performance.  
b82b0 2a 2a 20 68 69 74 20 6f 6e 6c 79 2c 20 6e 6f 74  ** hit only, not
b82c0 20 61 20 66 61 74 61 6c 20 65 72 72 6f 72 29 2e   a fatal error).
b82d0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 48 2d 3e  .  */.  if( pH->
b82e0 68 74 73 69 7a 65 3e 30 20 29 20 73 71 6c 69 74  htsize>0 ) sqlit
b82f0 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c  e3BeginBenignMal
b8300 6c 6f 63 28 29 3b 0a 20 20 6e 65 77 5f 68 74 20  loc();.  new_ht 
b8310 3d 20 28 73 74 72 75 63 74 20 5f 68 74 20 2a 29  = (struct _ht *)
b8320 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72  sqlite3MallocZer
b8330 6f 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a 65  o( new_size*size
b8340 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 20 29  of(struct _ht) )
b8350 3b 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69  ;.  if( pH->htsi
b8360 7a 65 3e 30 20 29 20 73 71 6c 69 74 65 33 45 6e  ze>0 ) sqlite3En
b8370 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  dBenignMalloc();
b8380 0a 0a 20 20 69 66 28 20 6e 65 77 5f 68 74 3d 3d  ..  if( new_ht==
b8390 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71  0 ) return;.  sq
b83a0 6c 69 74 65 33 5f 66 72 65 65 28 70 48 2d 3e 68  lite3_free(pH->h
b83b0 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20 6e  t);.  pH->ht = n
b83c0 65 77 5f 68 74 3b 0a 20 20 70 48 2d 3e 68 74 73  ew_ht;.  pH->hts
b83d0 69 7a 65 20 3d 20 6e 65 77 5f 73 69 7a 65 3b 0a  ize = new_size;.
b83e0 20 20 66 6f 72 28 65 6c 65 6d 3d 70 48 2d 3e 66    for(elem=pH->f
b83f0 69 72 73 74 2c 20 70 48 2d 3e 66 69 72 73 74 3d  irst, pH->first=
b8400 30 3b 20 65 6c 65 6d 3b 20 65 6c 65 6d 20 3d 20  0; elem; elem = 
b8410 6e 65 78 74 5f 65 6c 65 6d 29 7b 0a 20 20 20 20  next_elem){.    
b8420 69 6e 74 20 68 20 3d 20 73 74 72 48 61 73 68 28  int h = strHash(
b8430 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d  elem->pKey, elem
b8440 2d 3e 6e 4b 65 79 29 20 26 20 28 6e 65 77 5f 73  ->nKey) & (new_s
b8450 69 7a 65 2d 31 29 3b 0a 20 20 20 20 6e 65 78 74  ize-1);.    next
b8460 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65  _elem = elem->ne
b8470 78 74 3b 0a 20 20 20 20 69 6e 73 65 72 74 45 6c  xt;.    insertEl
b8480 65 6d 65 6e 74 28 70 48 2c 20 26 6e 65 77 5f 68  ement(pH, &new_h
b8490 74 5b 68 5d 2c 20 65 6c 65 6d 29 3b 0a 20 20 7d  t[h], elem);.  }
b84a0 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63  .}../* This func
b84b0 74 69 6f 6e 20 28 66 6f 72 20 69 6e 74 65 72 6e  tion (for intern
b84c0 61 6c 20 75 73 65 20 6f 6e 6c 79 29 20 6c 6f 63  al use only) loc
b84d0 61 74 65 73 20 61 6e 20 65 6c 65 6d 65 6e 74 20  ates an element 
b84e0 69 6e 20 61 6e 0a 2a 2a 20 68 61 73 68 20 74 61  in an.** hash ta
b84f0 62 6c 65 20 74 68 61 74 20 6d 61 74 63 68 65 73  ble that matches
b8500 20 74 68 65 20 67 69 76 65 6e 20 6b 65 79 2e 20   the given key. 
b8510 20 54 68 65 20 68 61 73 68 20 66 6f 72 20 74 68   The hash for th
b8520 69 73 20 6b 65 79 20 68 61 73 0a 2a 2a 20 61 6c  is key has.** al
b8530 72 65 61 64 79 20 62 65 65 6e 20 63 6f 6d 70 75  ready been compu
b8540 74 65 64 20 61 6e 64 20 69 73 20 70 61 73 73 65  ted and is passe
b8550 64 20 61 73 20 74 68 65 20 34 74 68 20 70 61 72  d as the 4th par
b8560 61 6d 65 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  ameter..*/.stati
b8570 63 20 48 61 73 68 45 6c 65 6d 20 2a 66 69 6e 64  c HashElem *find
b8580 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68  ElementGivenHash
b8590 28 0a 20 20 63 6f 6e 73 74 20 48 61 73 68 20 2a  (.  const Hash *
b85a0 70 48 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 70  pH,     /* The p
b85b0 48 20 74 6f 20 62 65 20 73 65 61 72 63 68 65 64  H to be searched
b85c0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
b85d0 20 2a 70 4b 65 79 2c 20 20 20 2f 2a 20 54 68 65   *pKey,   /* The
b85e0 20 6b 65 79 20 77 65 20 61 72 65 20 73 65 61 72   key we are sear
b85f0 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0a 20 20 69  ching for */.  i
b8600 6e 74 20 6e 4b 65 79 2c 0a 20 20 69 6e 74 20 68  nt nKey,.  int h
b8610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
b8620 2a 20 54 68 65 20 68 61 73 68 20 66 6f 72 20 74  * The hash for t
b8630 68 69 73 20 6b 65 79 2e 20 2a 2f 0a 29 7b 0a 20  his key. */.){. 
b8640 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b   HashElem *elem;
b8650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b8660 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20  /* Used to loop 
b8670 74 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74  thru the element
b8680 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 63   list */.  int c
b8690 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  ount;           
b86a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
b86b0 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
b86c0 6c 65 66 74 20 74 6f 20 74 65 73 74 20 2a 2f 0a  left to test */.
b86d0 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b  .  if( pH->ht ){
b86e0 0a 20 20 20 20 73 74 72 75 63 74 20 5f 68 74 20  .    struct _ht 
b86f0 2a 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68  *pEntry = &pH->h
b8700 74 5b 68 5d 3b 0a 20 20 20 20 65 6c 65 6d 20 3d  t[h];.    elem =
b8710 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3b 0a   pEntry->chain;.
b8720 20 20 20 20 63 6f 75 6e 74 20 3d 20 70 45 6e 74      count = pEnt
b8730 72 79 2d 3e 63 6f 75 6e 74 3b 0a 20 20 20 20 77  ry->count;.    w
b8740 68 69 6c 65 28 20 63 6f 75 6e 74 2d 2d 20 26 26  hile( count-- &&
b8750 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 20 20 69   elem ){.      i
b8760 66 28 20 73 74 72 43 6f 6d 70 61 72 65 28 65 6c  f( strCompare(el
b8770 65 6d 2d 3e 70 4b 65 79 2c 65 6c 65 6d 2d 3e 6e  em->pKey,elem->n
b8780 4b 65 79 2c 70 4b 65 79 2c 6e 4b 65 79 29 3d 3d  Key,pKey,nKey)==
b8790 30 20 29 7b 20 0a 20 20 20 20 20 20 20 20 72 65  0 ){ .        re
b87a0 74 75 72 6e 20 65 6c 65 6d 3b 0a 20 20 20 20 20  turn elem;.     
b87b0 20 7d 0a 20 20 20 20 20 20 65 6c 65 6d 20 3d 20   }.      elem = 
b87c0 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20  elem->next;.    
b87d0 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  }.  }.  return 0
b87e0 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61  ;.}../* Remove a
b87f0 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 66 72   single entry fr
b8800 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  om the hash tabl
b8810 65 20 67 69 76 65 6e 20 61 20 70 6f 69 6e 74 65  e given a pointe
b8820 72 20 74 6f 20 74 68 61 74 0a 2a 2a 20 65 6c 65  r to that.** ele
b8830 6d 65 6e 74 20 61 6e 64 20 61 20 68 61 73 68 20  ment and a hash 
b8840 6f 6e 20 74 68 65 20 65 6c 65 6d 65 6e 74 27 73  on the element's
b8850 20 6b 65 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   key..*/.static 
b8860 76 6f 69 64 20 72 65 6d 6f 76 65 45 6c 65 6d 65  void removeEleme
b8870 6e 74 47 69 76 65 6e 48 61 73 68 28 0a 20 20 48  ntGivenHash(.  H
b8880 61 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20  ash *pH,        
b8890 20 2f 2a 20 54 68 65 20 70 48 20 63 6f 6e 74 61   /* The pH conta
b88a0 69 6e 69 6e 67 20 22 65 6c 65 6d 22 20 2a 2f 0a  ining "elem" */.
b88b0 20 20 48 61 73 68 45 6c 65 6d 2a 20 65 6c 65 6d    HashElem* elem
b88c0 2c 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65  ,   /* The eleme
b88d0 6e 74 20 74 6f 20 62 65 20 72 65 6d 6f 76 65 64  nt to be removed
b88e0 20 66 72 6f 6d 20 74 68 65 20 70 48 20 2a 2f 0a   from the pH */.
b88f0 20 20 69 6e 74 20 68 20 20 20 20 20 20 20 20 20    int h         
b8900 20 20 20 20 2f 2a 20 48 61 73 68 20 76 61 6c 75      /* Hash valu
b8910 65 20 66 6f 72 20 74 68 65 20 65 6c 65 6d 65 6e  e for the elemen
b8920 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74  t */.){.  struct
b8930 20 5f 68 74 20 2a 70 45 6e 74 72 79 3b 0a 20 20   _ht *pEntry;.  
b8940 69 66 28 20 65 6c 65 6d 2d 3e 70 72 65 76 20 29  if( elem->prev )
b8950 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 70 72 65 76  {.    elem->prev
b8960 2d 3e 6e 65 78 74 20 3d 20 65 6c 65 6d 2d 3e 6e  ->next = elem->n
b8970 65 78 74 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a 20  ext; .  }else{. 
b8980 20 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 65     pH->first = e
b8990 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20  lem->next;.  }. 
b89a0 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 65 78 74 20   if( elem->next 
b89b0 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 6e 65 78  ){.    elem->nex
b89c0 74 2d 3e 70 72 65 76 20 3d 20 65 6c 65 6d 2d 3e  t->prev = elem->
b89d0 70 72 65 76 3b 0a 20 20 7d 0a 20 20 70 45 6e 74  prev;.  }.  pEnt
b89e0 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68 5d 3b  ry = &pH->ht[h];
b89f0 0a 20 20 69 66 28 20 70 45 6e 74 72 79 2d 3e 63  .  if( pEntry->c
b8a00 68 61 69 6e 3d 3d 65 6c 65 6d 20 29 7b 0a 20 20  hain==elem ){.  
b8a10 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20    pEntry->chain 
b8a20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20  = elem->next;.  
b8a30 7d 0a 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e  }.  pEntry->coun
b8a40 74 2d 2d 3b 0a 20 20 69 66 28 20 70 45 6e 74 72  t--;.  if( pEntr
b8a50 79 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20  y->count<=0 ){. 
b8a60 20 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e     pEntry->chain
b8a70 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 0;.  }.  if( 
b8a80 70 48 2d 3e 63 6f 70 79 4b 65 79 20 29 7b 0a 20  pH->copyKey ){. 
b8a90 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
b8aa0 65 6c 65 6d 2d 3e 70 4b 65 79 29 3b 0a 20 20 7d  elem->pKey);.  }
b8ab0 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
b8ac0 20 65 6c 65 6d 20 29 3b 0a 20 20 70 48 2d 3e 63   elem );.  pH->c
b8ad0 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 28 20 70 48  ount--;.  if( pH
b8ae0 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20 20  ->count<=0 ){.  
b8af0 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 66 69    assert( pH->fi
b8b00 72 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73  rst==0 );.    as
b8b10 73 65 72 74 28 20 70 48 2d 3e 63 6f 75 6e 74 3d  sert( pH->count=
b8b20 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  =0 );.    sqlite
b8b30 33 48 61 73 68 43 6c 65 61 72 28 70 48 29 3b 0a  3HashClear(pH);.
b8b40 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 74 74 65 6d 70    }.}../* Attemp
b8b50 74 20 74 6f 20 6c 6f 63 61 74 65 20 61 6e 20 65  t to locate an e
b8b60 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 68 61  lement of the ha
b8b70 73 68 20 74 61 62 6c 65 20 70 48 20 77 69 74 68  sh table pH with
b8b80 20 61 20 6b 65 79 0a 2a 2a 20 74 68 61 74 20 6d   a key.** that m
b8b90 61 74 63 68 65 73 20 70 4b 65 79 2c 6e 4b 65 79  atches pKey,nKey
b8ba0 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  .  Return a poin
b8bb0 74 65 72 20 74 6f 20 74 68 65 20 63 6f 72 72 65  ter to the corre
b8bc0 73 70 6f 6e 64 69 6e 67 20 0a 2a 2a 20 48 61 73  sponding .** Has
b8bd0 68 45 6c 65 6d 20 73 74 72 75 63 74 75 72 65 20  hElem structure 
b8be0 66 6f 72 20 74 68 69 73 20 65 6c 65 6d 65 6e 74  for this element
b8bf0 20 69 66 20 69 74 20 69 73 20 66 6f 75 6e 64 2c   if it is found,
b8c00 20 6f 72 20 4e 55 4c 4c 0a 2a 2a 20 6f 74 68 65   or NULL.** othe
b8c10 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  rwise..*/.SQLITE
b8c20 5f 50 52 49 56 41 54 45 20 48 61 73 68 45 6c 65  _PRIVATE HashEle
b8c30 6d 20 2a 73 71 6c 69 74 65 33 48 61 73 68 46 69  m *sqlite3HashFi
b8c40 6e 64 45 6c 65 6d 28 63 6f 6e 73 74 20 48 61 73  ndElem(const Has
b8c50 68 20 2a 70 48 2c 20 63 6f 6e 73 74 20 76 6f 69  h *pH, const voi
b8c60 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65  d *pKey, int nKe
b8c70 79 29 7b 0a 20 20 69 6e 74 20 68 3b 20 20 20 20  y){.  int h;    
b8c80 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 68 61           /* A ha
b8c90 73 68 20 6f 6e 20 6b 65 79 20 2a 2f 0a 20 20 48  sh on key */.  H
b8ca0 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20  ashElem *elem;  
b8cb0 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74    /* The element
b8cc0 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 6b 65   that matches ke
b8cd0 79 20 2a 2f 0a 0a 20 20 69 66 28 20 70 48 3d 3d  y */..  if( pH==
b8ce0 30 20 7c 7c 20 70 48 2d 3e 68 74 3d 3d 30 20 29  0 || pH->ht==0 )
b8cf0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 68 20 3d   return 0;.  h =
b8d00 20 73 74 72 48 61 73 68 28 70 4b 65 79 2c 6e 4b   strHash(pKey,nK
b8d10 65 79 29 3b 0a 20 20 65 6c 65 6d 20 3d 20 66 69  ey);.  elem = fi
b8d20 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61  ndElementGivenHa
b8d30 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c  sh(pH,pKey,nKey,
b8d40 20 68 20 25 20 70 48 2d 3e 68 74 73 69 7a 65 29   h % pH->htsize)
b8d50 3b 0a 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 3b  ;.  return elem;
b8d60 0a 7d 0a 0a 2f 2a 20 41 74 74 65 6d 70 74 20 74  .}../* Attempt t
b8d70 6f 20 6c 6f 63 61 74 65 20 61 6e 20 65 6c 65 6d  o locate an elem
b8d80 65 6e 74 20 6f 66 20 74 68 65 20 68 61 73 68 20  ent of the hash 
b8d90 74 61 62 6c 65 20 70 48 20 77 69 74 68 20 61 20  table pH with a 
b8da0 6b 65 79 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63  key.** that matc
b8db0 68 65 73 20 70 4b 65 79 2c 6e 4b 65 79 2e 20 20  hes pKey,nKey.  
b8dc0 52 65 74 75 72 6e 20 74 68 65 20 64 61 74 61 20  Return the data 
b8dd0 66 6f 72 20 74 68 69 73 20 65 6c 65 6d 65 6e 74  for this element
b8de0 20 69 66 20 69 74 20 69 73 0a 2a 2a 20 66 6f 75   if it is.** fou
b8df0 6e 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74  nd, or NULL if t
b8e00 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68  here is no match
b8e10 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
b8e20 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
b8e30 33 48 61 73 68 46 69 6e 64 28 63 6f 6e 73 74 20  3HashFind(const 
b8e40 48 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73 74 20  Hash *pH, const 
b8e50 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20  void *pKey, int 
b8e60 6e 4b 65 79 29 7b 0a 20 20 48 61 73 68 45 6c 65  nKey){.  HashEle
b8e70 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 2f 2a 20 54  m *elem;    /* T
b8e80 68 65 20 65 6c 65 6d 65 6e 74 20 74 68 61 74 20  he element that 
b8e90 6d 61 74 63 68 65 73 20 6b 65 79 20 2a 2f 0a 20  matches key */. 
b8ea0 20 65 6c 65 6d 20 3d 20 73 71 6c 69 74 65 33 48   elem = sqlite3H
b8eb0 61 73 68 46 69 6e 64 45 6c 65 6d 28 70 48 2c 20  ashFindElem(pH, 
b8ec0 70 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 72  pKey, nKey);.  r
b8ed0 65 74 75 72 6e 20 65 6c 65 6d 20 3f 20 65 6c 65  eturn elem ? ele
b8ee0 6d 2d 3e 64 61 74 61 20 3a 20 30 3b 0a 7d 0a 0a  m->data : 0;.}..
b8ef0 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20 65 6c 65  /* Insert an ele
b8f00 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61  ment into the ha
b8f10 73 68 20 74 61 62 6c 65 20 70 48 2e 20 20 54 68  sh table pH.  Th
b8f20 65 20 6b 65 79 20 69 73 20 70 4b 65 79 2c 6e 4b  e key is pKey,nK
b8f30 65 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61  ey.** and the da
b8f40 74 61 20 69 73 20 22 64 61 74 61 22 2e 0a 2a 2a  ta is "data"..**
b8f50 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c 65 6d 65 6e  .** If no elemen
b8f60 74 20 65 78 69 73 74 73 20 77 69 74 68 20 61 20  t exists with a 
b8f70 6d 61 74 63 68 69 6e 67 20 6b 65 79 2c 20 74 68  matching key, th
b8f80 65 6e 20 61 20 6e 65 77 0a 2a 2a 20 65 6c 65 6d  en a new.** elem
b8f90 65 6e 74 20 69 73 20 63 72 65 61 74 65 64 2e 20  ent is created. 
b8fa0 20 41 20 63 6f 70 79 20 6f 66 20 74 68 65 20 6b   A copy of the k
b8fb0 65 79 20 69 73 20 6d 61 64 65 20 69 66 20 74 68  ey is made if th
b8fc0 65 20 63 6f 70 79 4b 65 79 0a 2a 2a 20 66 6c 61  e copyKey.** fla
b8fd0 67 20 69 73 20 73 65 74 2e 20 20 4e 55 4c 4c 20  g is set.  NULL 
b8fe0 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
b8ff0 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 65 6c  ** If another el
b9000 65 6d 65 6e 74 20 61 6c 72 65 61 64 79 20 65 78  ement already ex
b9010 69 73 74 73 20 77 69 74 68 20 74 68 65 20 73 61  ists with the sa
b9020 6d 65 20 6b 65 79 2c 20 74 68 65 6e 20 74 68 65  me key, then the
b9030 0a 2a 2a 20 6e 65 77 20 64 61 74 61 20 72 65 70  .** new data rep
b9040 6c 61 63 65 73 20 74 68 65 20 6f 6c 64 20 64 61  laces the old da
b9050 74 61 20 61 6e 64 20 74 68 65 20 6f 6c 64 20 64  ta and the old d
b9060 61 74 61 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ata is returned.
b9070 0a 2a 2a 20 54 68 65 20 6b 65 79 20 69 73 20 6e  .** The key is n
b9080 6f 74 20 63 6f 70 69 65 64 20 69 6e 20 74 68 69  ot copied in thi
b9090 73 20 69 6e 73 74 61 6e 63 65 2e 20 20 49 66 20  s instance.  If 
b90a0 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20  a malloc fails, 
b90b0 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 77 20  then.** the new 
b90c0 64 61 74 61 20 69 73 20 72 65 74 75 72 6e 65 64  data is returned
b90d0 20 61 6e 64 20 74 68 65 20 68 61 73 68 20 74 61   and the hash ta
b90e0 62 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64  ble is unchanged
b90f0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22  ..**.** If the "
b9100 64 61 74 61 22 20 70 61 72 61 6d 65 74 65 72 20  data" parameter 
b9110 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
b9120 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74   is NULL, then t
b9130 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 63 6f  he.** element co
b9140 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 22  rresponding to "
b9150 6b 65 79 22 20 69 73 20 72 65 6d 6f 76 65 64 20  key" is removed 
b9160 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61  from the hash ta
b9170 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ble..*/.SQLITE_P
b9180 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
b9190 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 48  ite3HashInsert(H
b91a0 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73 74 20 76  ash *pH, const v
b91b0 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e  oid *pKey, int n
b91c0 4b 65 79 2c 20 76 6f 69 64 20 2a 64 61 74 61 29  Key, void *data)
b91d0 7b 0a 20 20 69 6e 74 20 68 72 61 77 3b 20 20 20  {.  int hraw;   
b91e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 61 77            /* Raw
b91f0 20 68 61 73 68 20 76 61 6c 75 65 20 6f 66 20 74   hash value of t
b9200 68 65 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20  he key */.  int 
b9210 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h;              
b9220 20 20 2f 2a 20 74 68 65 20 68 61 73 68 20 6f 66    /* the hash of
b9230 20 74 68 65 20 6b 65 79 20 6d 6f 64 75 6c 6f 20   the key modulo 
b9240 68 61 73 68 20 74 61 62 6c 65 20 73 69 7a 65 20  hash table size 
b9250 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65  */.  HashElem *e
b9260 6c 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20 55 73  lem;       /* Us
b9270 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75 20  ed to loop thru 
b9280 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73 74  the element list
b9290 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a   */.  HashElem *
b92a0 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a 20 4e  new_elem;   /* N
b92b0 65 77 20 65 6c 65 6d 65 6e 74 20 61 64 64 65 64  ew element added
b92c0 20 74 6f 20 74 68 65 20 70 48 20 2a 2f 0a 0a 20   to the pH */.. 
b92d0 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 29   assert( pH!=0 )
b92e0 3b 0a 20 20 68 72 61 77 20 3d 20 73 74 72 48 61  ;.  hraw = strHa
b92f0 73 68 28 70 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a  sh(pKey, nKey);.
b9300 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a 65    if( pH->htsize
b9310 20 29 7b 0a 20 20 20 20 68 20 3d 20 68 72 61 77   ){.    h = hraw
b9320 20 25 20 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20   % pH->htsize;. 
b9330 20 20 20 65 6c 65 6d 20 3d 20 66 69 6e 64 45 6c     elem = findEl
b9340 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 70  ementGivenHash(p
b9350 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c 68 29 3b 0a  H,pKey,nKey,h);.
b9360 20 20 20 20 69 66 28 20 65 6c 65 6d 20 29 7b 0a      if( elem ){.
b9370 20 20 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64 5f        void *old_
b9380 64 61 74 61 20 3d 20 65 6c 65 6d 2d 3e 64 61 74  data = elem->dat
b9390 61 3b 0a 20 20 20 20 20 20 69 66 28 20 64 61 74  a;.      if( dat
b93a0 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  a==0 ){.        
b93b0 72 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 47 69 76  removeElementGiv
b93c0 65 6e 48 61 73 68 28 70 48 2c 65 6c 65 6d 2c 68  enHash(pH,elem,h
b93d0 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
b93e0 20 20 20 20 20 20 20 20 65 6c 65 6d 2d 3e 64 61          elem->da
b93f0 74 61 20 3d 20 64 61 74 61 3b 0a 20 20 20 20 20  ta = data;.     
b9400 20 20 20 69 66 28 20 21 70 48 2d 3e 63 6f 70 79     if( !pH->copy
b9410 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Key ){.         
b9420 20 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 28 76   elem->pKey = (v
b9430 6f 69 64 20 2a 29 70 4b 65 79 3b 0a 20 20 20 20  oid *)pKey;.    
b9440 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73      }.        as
b9450 73 65 72 74 28 6e 4b 65 79 3d 3d 65 6c 65 6d 2d  sert(nKey==elem-
b9460 3e 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 7d 0a  >nKey);.      }.
b9470 20 20 20 20 20 20 72 65 74 75 72 6e 20 6f 6c 64        return old
b9480 5f 64 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d  _data;.    }.  }
b9490 0a 20 20 69 66 28 20 64 61 74 61 3d 3d 30 20 29  .  if( data==0 )
b94a0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e 65 77   return 0;.  new
b94b0 5f 65 6c 65 6d 20 3d 20 28 48 61 73 68 45 6c 65  _elem = (HashEle
b94c0 6d 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  m*)sqlite3Malloc
b94d0 28 20 73 69 7a 65 6f 66 28 48 61 73 68 45 6c 65  ( sizeof(HashEle
b94e0 6d 29 20 29 3b 0a 20 20 69 66 28 20 6e 65 77 5f  m) );.  if( new_
b94f0 65 6c 65 6d 3d 3d 30 20 29 20 72 65 74 75 72 6e  elem==0 ) return
b9500 20 64 61 74 61 3b 0a 20 20 69 66 28 20 70 48 2d   data;.  if( pH-
b9510 3e 63 6f 70 79 4b 65 79 20 26 26 20 70 4b 65 79  >copyKey && pKey
b9520 21 3d 30 20 29 7b 0a 20 20 20 20 6e 65 77 5f 65  !=0 ){.    new_e
b9530 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 73 71 6c 69  lem->pKey = sqli
b9540 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 4b 65 79 20  te3Malloc( nKey 
b9550 29 3b 0a 20 20 20 20 69 66 28 20 6e 65 77 5f 65  );.    if( new_e
b9560 6c 65 6d 2d 3e 70 4b 65 79 3d 3d 30 20 29 7b 0a  lem->pKey==0 ){.
b9570 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
b9580 65 65 28 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20  ee(new_elem);.  
b9590 20 20 20 20 72 65 74 75 72 6e 20 64 61 74 61 3b      return data;
b95a0 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70  .    }.    memcp
b95b0 79 28 28 76 6f 69 64 2a 29 6e 65 77 5f 65 6c 65  y((void*)new_ele
b95c0 6d 2d 3e 70 4b 65 79 2c 20 70 4b 65 79 2c 20 6e  m->pKey, pKey, n
b95d0 4b 65 79 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Key);.  }else{. 
b95e0 20 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65     new_elem->pKe
b95f0 79 20 3d 20 28 76 6f 69 64 2a 29 70 4b 65 79 3b  y = (void*)pKey;
b9600 0a 20 20 7d 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d  .  }.  new_elem-
b9610 3e 6e 4b 65 79 20 3d 20 6e 4b 65 79 3b 0a 20 20  >nKey = nKey;.  
b9620 70 48 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 69  pH->count++;.  i
b9630 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 3d 3d 30  f( pH->htsize==0
b9640 20 29 7b 0a 20 20 20 20 72 65 68 61 73 68 28 70   ){.    rehash(p
b9650 48 2c 20 31 32 38 2f 73 69 7a 65 6f 66 28 70 48  H, 128/sizeof(pH
b9660 2d 3e 68 74 5b 30 5d 29 29 3b 0a 20 20 20 20 69  ->ht[0]));.    i
b9670 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 3d 3d 30  f( pH->htsize==0
b9680 20 29 7b 0a 20 20 20 20 20 20 70 48 2d 3e 63 6f   ){.      pH->co
b9690 75 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  unt = 0;.      i
b96a0 66 28 20 70 48 2d 3e 63 6f 70 79 4b 65 79 20 29  f( pH->copyKey )
b96b0 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
b96c0 33 5f 66 72 65 65 28 6e 65 77 5f 65 6c 65 6d 2d  3_free(new_elem-
b96d0 3e 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 7d 0a  >pKey);.      }.
b96e0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
b96f0 65 65 28 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20  ee(new_elem);.  
b9700 20 20 20 20 72 65 74 75 72 6e 20 64 61 74 61 3b      return data;
b9710 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
b9720 20 70 48 2d 3e 63 6f 75 6e 74 20 3e 20 70 48 2d   pH->count > pH-
b9730 3e 68 74 73 69 7a 65 20 29 7b 0a 20 20 20 20 72  >htsize ){.    r
b9740 65 68 61 73 68 28 70 48 2c 70 48 2d 3e 68 74 73  ehash(pH,pH->hts
b9750 69 7a 65 2a 32 29 3b 0a 20 20 7d 0a 20 20 61 73  ize*2);.  }.  as
b9760 73 65 72 74 28 20 70 48 2d 3e 68 74 73 69 7a 65  sert( pH->htsize
b9770 3e 30 20 29 3b 0a 20 20 68 20 3d 20 68 72 61 77  >0 );.  h = hraw
b9780 20 25 20 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20   % pH->htsize;. 
b9790 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70   insertElement(p
b97a0 48 2c 20 26 70 48 2d 3e 68 74 5b 68 5d 2c 20 6e  H, &pH->ht[h], n
b97b0 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 6e 65 77 5f  ew_elem);.  new_
b97c0 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74  elem->data = dat
b97d0 61 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  a;.  return 0;.}
b97e0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
b97f0 2a 20 45 6e 64 20 6f 66 20 68 61 73 68 2e 63 20  * End of hash.c 
b9800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9830 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
b9840 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63  * Begin file opc
b9850 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  odes.c *********
b9860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9880 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c  /./* Automatical
b9890 6c 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44  ly generated.  D
b98a0 6f 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a  o not edit */./*
b98b0 20 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64   See the mkopcod
b98c0 65 63 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f  ec.awk script fo
b98d0 72 20 64 65 74 61 69 6c 73 2e 20 2a 2f 0a 23 69  r details. */.#i
b98e0 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
b98f0 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20  E_OMIT_EXPLAIN) 
b9900 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42  || !defined(NDEB
b9910 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56  UG) || defined(V
b9920 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20  DBE_PROFILE) || 
b9930 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
b9940 45 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49  EBUG).SQLITE_PRI
b9950 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
b9960 2a 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61  *sqlite3OpcodeNa
b9970 6d 65 28 69 6e 74 20 69 29 7b 0a 20 73 74 61 74  me(int i){. stat
b9980 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  ic const char *c
b9990 6f 6e 73 74 20 61 7a 4e 61 6d 65 5b 5d 20 3d 20  onst azName[] = 
b99a0 7b 20 22 3f 22 2c 0a 20 20 20 20 20 2f 2a 20 20  { "?",.     /*  
b99b0 20 31 20 2a 2f 20 22 56 4e 65 78 74 22 2c 0a 20   1 */ "VNext",. 
b99c0 20 20 20 20 2f 2a 20 20 20 32 20 2a 2f 20 22 41      /*   2 */ "A
b99d0 66 66 69 6e 69 74 79 22 2c 0a 20 20 20 20 20 2f  ffinity",.     /
b99e0 2a 20 20 20 33 20 2a 2f 20 22 43 6f 6c 75 6d 6e  *   3 */ "Column
b99f0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 34 20 2a  ",.     /*   4 *
b9a00 2f 20 22 53 65 74 43 6f 6f 6b 69 65 22 2c 0a 20  / "SetCookie",. 
b9a10 20 20 20 20 2f 2a 20 20 20 35 20 2a 2f 20 22 53      /*   5 */ "S
b9a20 65 71 75 65 6e 63 65 22 2c 0a 20 20 20 20 20 2f  equence",.     /
b9a30 2a 20 20 20 36 20 2a 2f 20 22 4d 6f 76 65 47 74  *   6 */ "MoveGt
b9a40 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 37 20 2a  ",.     /*   7 *
b9a50 2f 20 22 52 6f 77 4b 65 79 22 2c 0a 20 20 20 20  / "RowKey",.    
b9a60 20 2f 2a 20 20 20 38 20 2a 2f 20 22 53 43 6f 70   /*   8 */ "SCop
b9a70 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 39 20  y",.     /*   9 
b9a80 2a 2f 20 22 4f 70 65 6e 57 72 69 74 65 22 2c 0a  */ "OpenWrite",.
b9a90 20 20 20 20 20 2f 2a 20 20 31 30 20 2a 2f 20 22       /*  10 */ "
b9aa0 49 66 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 31  If",.     /*  11
b9ab0 20 2a 2f 20 22 56 52 6f 77 69 64 22 2c 0a 20 20   */ "VRowid",.  
b9ac0 20 20 20 2f 2a 20 20 31 32 20 2a 2f 20 22 43 6f     /*  12 */ "Co
b9ad0 6c 6c 53 65 71 22 2c 0a 20 20 20 20 20 2f 2a 20  llSeq",.     /* 
b9ae0 20 31 33 20 2a 2f 20 22 4f 70 65 6e 52 65 61 64   13 */ "OpenRead
b9af0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 34 20 2a  ",.     /*  14 *
b9b00 2f 20 22 45 78 70 69 72 65 22 2c 0a 20 20 20 20  / "Expire",.    
b9b10 20 2f 2a 20 20 31 35 20 2a 2f 20 22 41 75 74 6f   /*  15 */ "Auto
b9b20 43 6f 6d 6d 69 74 22 2c 0a 20 20 20 20 20 2f 2a  Commit",.     /*
b9b30 20 20 31 36 20 2a 2f 20 22 4e 6f 74 22 2c 0a 20    16 */ "Not",. 
b9b40 20 20 20 20 2f 2a 20 20 31 37 20 2a 2f 20 22 50      /*  17 */ "P
b9b50 61 67 65 63 6f 75 6e 74 22 2c 0a 20 20 20 20 20  agecount",.     
b9b60 2f 2a 20 20 31 38 20 2a 2f 20 22 49 6e 74 65 67  /*  18 */ "Integ
b9b70 72 69 74 79 43 6b 22 2c 0a 20 20 20 20 20 2f 2a  rityCk",.     /*
b9b80 20 20 31 39 20 2a 2f 20 22 53 6f 72 74 22 2c 0a    19 */ "Sort",.
b9b90 20 20 20 20 20 2f 2a 20 20 32 30 20 2a 2f 20 22       /*  20 */ "
b9ba0 43 6f 70 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20  Copy",.     /*  
b9bb0 32 31 20 2a 2f 20 22 54 72 61 63 65 22 2c 0a 20  21 */ "Trace",. 
b9bc0 20 20 20 20 2f 2a 20 20 32 32 20 2a 2f 20 22 46      /*  22 */ "F
b9bd0 75 6e 63 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f  unction",.     /
b9be0 2a 20 20 32 33 20 2a 2f 20 22 49 66 4e 65 67 22  *  23 */ "IfNeg"
b9bf0 2c 0a 20 20 20 20 20 2f 2a 20 20 32 34 20 2a 2f  ,.     /*  24 */
b9c00 20 22 4e 6f 6f 70 22 2c 0a 20 20 20 20 20 2f 2a   "Noop",.     /*
b9c10 20 20 32 35 20 2a 2f 20 22 52 65 74 75 72 6e 22    25 */ "Return"
b9c20 2c 0a 20 20 20 20 20 2f 2a 20 20 32 36 20 2a 2f  ,.     /*  26 */
b9c30 20 22 4e 65 77 52 6f 77 69 64 22 2c 0a 20 20 20   "NewRowid",.   
b9c40 20 20 2f 2a 20 20 32 37 20 2a 2f 20 22 56 61 72    /*  27 */ "Var
b9c50 69 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a 20  iable",.     /* 
b9c60 20 32 38 20 2a 2f 20 22 53 74 72 69 6e 67 22 2c   28 */ "String",
b9c70 0a 20 20 20 20 20 2f 2a 20 20 32 39 20 2a 2f 20  .     /*  29 */ 
b9c80 22 52 65 61 6c 41 66 66 69 6e 69 74 79 22 2c 0a  "RealAffinity",.
b9c90 20 20 20 20 20 2f 2a 20 20 33 30 20 2a 2f 20 22       /*  30 */ "
b9ca0 56 52 65 6e 61 6d 65 22 2c 0a 20 20 20 20 20 2f  VRename",.     /
b9cb0 2a 20 20 33 31 20 2a 2f 20 22 50 61 72 73 65 53  *  31 */ "ParseS
b9cc0 63 68 65 6d 61 22 2c 0a 20 20 20 20 20 2f 2a 20  chema",.     /* 
b9cd0 20 33 32 20 2a 2f 20 22 56 4f 70 65 6e 22 2c 0a   32 */ "VOpen",.
b9ce0 20 20 20 20 20 2f 2a 20 20 33 33 20 2a 2f 20 22       /*  33 */ "
b9cf0 43 6c 6f 73 65 22 2c 0a 20 20 20 20 20 2f 2a 20  Close",.     /* 
b9d00 20 33 34 20 2a 2f 20 22 43 72 65 61 74 65 49 6e   34 */ "CreateIn
b9d10 64 65 78 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33  dex",.     /*  3
b9d20 35 20 2a 2f 20 22 49 73 55 6e 69 71 75 65 22 2c  5 */ "IsUnique",
b9d30 0a 20 20 20 20 20 2f 2a 20 20 33 36 20 2a 2f 20  .     /*  36 */ 
b9d40 22 4e 6f 74 46 6f 75 6e 64 22 2c 0a 20 20 20 20  "NotFound",.    
b9d50 20 2f 2a 20 20 33 37 20 2a 2f 20 22 49 6e 74 36   /*  37 */ "Int6
b9d60 34 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 38 20  4",.     /*  38 
b9d70 2a 2f 20 22 4d 75 73 74 42 65 49 6e 74 22 2c 0a  */ "MustBeInt",.
b9d80 20 20 20 20 20 2f 2a 20 20 33 39 20 2a 2f 20 22       /*  39 */ "
b9d90 48 61 6c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  Halt",.     /*  
b9da0 34 30 20 2a 2f 20 22 52 6f 77 69 64 22 2c 0a 20  40 */ "Rowid",. 
b9db0 20 20 20 20 2f 2a 20 20 34 31 20 2a 2f 20 22 49      /*  41 */ "I
b9dc0 64 78 4c 54 22 2c 0a 20 20 20 20 20 2f 2a 20 20  dxLT",.     /*  
b9dd0 34 32 20 2a 2f 20 22 41 64 64 49 6d 6d 22 2c 0a  42 */ "AddImm",.
b9de0 20 20 20 20 20 2f 2a 20 20 34 33 20 2a 2f 20 22       /*  43 */ "
b9df0 53 74 61 74 65 6d 65 6e 74 22 2c 0a 20 20 20 20  Statement",.    
b9e00 20 2f 2a 20 20 34 34 20 2a 2f 20 22 52 6f 77 44   /*  44 */ "RowD
b9e10 61 74 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34  ata",.     /*  4
b9e20 35 20 2a 2f 20 22 4d 65 6d 4d 61 78 22 2c 0a 20  5 */ "MemMax",. 
b9e30 20 20 20 20 2f 2a 20 20 34 36 20 2a 2f 20 22 4e      /*  46 */ "N
b9e40 6f 74 45 78 69 73 74 73 22 2c 0a 20 20 20 20 20  otExists",.     
b9e50 2f 2a 20 20 34 37 20 2a 2f 20 22 47 6f 73 75 62  /*  47 */ "Gosub
b9e60 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 38 20 2a  ",.     /*  48 *
b9e70 2f 20 22 49 6e 74 65 67 65 72 22 2c 0a 20 20 20  / "Integer",.   
b9e80 20 20 2f 2a 20 20 34 39 20 2a 2f 20 22 50 72 65    /*  49 */ "Pre
b9e90 76 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 30 20  v",.     /*  50 
b9ea0 2a 2f 20 22 56 43 6f 6c 75 6d 6e 22 2c 0a 20 20  */ "VColumn",.  
b9eb0 20 20 20 2f 2a 20 20 35 31 20 2a 2f 20 22 43 72     /*  51 */ "Cr
b9ec0 65 61 74 65 54 61 62 6c 65 22 2c 0a 20 20 20 20  eateTable",.    
b9ed0 20 2f 2a 20 20 35 32 20 2a 2f 20 22 4c 61 73 74   /*  52 */ "Last
b9ee0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 33 20 2a  ",.     /*  53 *
b9ef0 2f 20 22 49 6e 63 72 56 61 63 75 75 6d 22 2c 0a  / "IncrVacuum",.
b9f00 20 20 20 20 20 2f 2a 20 20 35 34 20 2a 2f 20 22       /*  54 */ "
b9f10 49 64 78 52 6f 77 69 64 22 2c 0a 20 20 20 20 20  IdxRowid",.     
b9f20 2f 2a 20 20 35 35 20 2a 2f 20 22 52 65 73 65 74  /*  55 */ "Reset
b9f30 43 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20  Count",.     /* 
b9f40 20 35 36 20 2a 2f 20 22 46 69 66 6f 57 72 69 74   56 */ "FifoWrit
b9f50 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 37 20  e",.     /*  57 
b9f60 2a 2f 20 22 43 6f 6e 74 65 78 74 50 75 73 68 22  */ "ContextPush"
b9f70 2c 0a 20 20 20 20 20 2f 2a 20 20 35 38 20 2a 2f  ,.     /*  58 */
b9f80 20 22 59 69 65 6c 64 22 2c 0a 20 20 20 20 20 2f   "Yield",.     /
b9f90 2a 20 20 35 39 20 2a 2f 20 22 44 72 6f 70 54 72  *  59 */ "DropTr
b9fa0 69 67 67 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20  igger",.     /* 
b9fb0 20 36 30 20 2a 2f 20 22 4f 72 22 2c 0a 20 20 20   60 */ "Or",.   
b9fc0 20 20 2f 2a 20 20 36 31 20 2a 2f 20 22 41 6e 64    /*  61 */ "And
b9fd0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 32 20 2a  ",.     /*  62 *
b9fe0 2f 20 22 44 72 6f 70 49 6e 64 65 78 22 2c 0a 20  / "DropIndex",. 
b9ff0 20 20 20 20 2f 2a 20 20 36 33 20 2a 2f 20 22 49      /*  63 */ "I
ba000 64 78 47 45 22 2c 0a 20 20 20 20 20 2f 2a 20 20  dxGE",.     /*  
ba010 36 34 20 2a 2f 20 22 49 64 78 44 65 6c 65 74 65  64 */ "IdxDelete
ba020 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 35 20 2a  ",.     /*  65 *
ba030 2f 20 22 49 73 4e 75 6c 6c 22 2c 0a 20 20 20 20  / "IsNull",.    
ba040 20 2f 2a 20 20 36 36 20 2a 2f 20 22 4e 6f 74 4e   /*  66 */ "NotN
ba050 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36  ull",.     /*  6
ba060 37 20 2a 2f 20 22 4e 65 22 2c 0a 20 20 20 20 20  7 */ "Ne",.     
ba070 2f 2a 20 20 36 38 20 2a 2f 20 22 45 71 22 2c 0a  /*  68 */ "Eq",.
ba080 20 20 20 20 20 2f 2a 20 20 36 39 20 2a 2f 20 22       /*  69 */ "
ba090 47 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 30  Gt",.     /*  70
ba0a0 20 2a 2f 20 22 4c 65 22 2c 0a 20 20 20 20 20 2f   */ "Le",.     /
ba0b0 2a 20 20 37 31 20 2a 2f 20 22 4c 74 22 2c 0a 20  *  71 */ "Lt",. 
ba0c0 20 20 20 20 2f 2a 20 20 37 32 20 2a 2f 20 22 47      /*  72 */ "G
ba0d0 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 33 20  e",.     /*  73 
ba0e0 2a 2f 20 22 56 61 63 75 75 6d 22 2c 0a 20 20 20  */ "Vacuum",.   
ba0f0 20 20 2f 2a 20 20 37 34 20 2a 2f 20 22 42 69 74    /*  74 */ "Bit
ba100 41 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37  And",.     /*  7
ba110 35 20 2a 2f 20 22 42 69 74 4f 72 22 2c 0a 20 20  5 */ "BitOr",.  
ba120 20 20 20 2f 2a 20 20 37 36 20 2a 2f 20 22 53 68     /*  76 */ "Sh
ba130 69 66 74 4c 65 66 74 22 2c 0a 20 20 20 20 20 2f  iftLeft",.     /
ba140 2a 20 20 37 37 20 2a 2f 20 22 53 68 69 66 74 52  *  77 */ "ShiftR
ba150 69 67 68 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  ight",.     /*  
ba160 37 38 20 2a 2f 20 22 41 64 64 22 2c 0a 20 20 20  78 */ "Add",.   
ba170 20 20 2f 2a 20 20 37 39 20 2a 2f 20 22 53 75 62    /*  79 */ "Sub
ba180 74 72 61 63 74 22 2c 0a 20 20 20 20 20 2f 2a 20  tract",.     /* 
ba190 20 38 30 20 2a 2f 20 22 4d 75 6c 74 69 70 6c 79   80 */ "Multiply
ba1a0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 31 20 2a  ",.     /*  81 *
ba1b0 2f 20 22 44 69 76 69 64 65 22 2c 0a 20 20 20 20  / "Divide",.    
ba1c0 20 2f 2a 20 20 38 32 20 2a 2f 20 22 52 65 6d 61   /*  82 */ "Rema
ba1d0 69 6e 64 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20  inder",.     /* 
ba1e0 20 38 33 20 2a 2f 20 22 43 6f 6e 63 61 74 22 2c   83 */ "Concat",
ba1f0 0a 20 20 20 20 20 2f 2a 20 20 38 34 20 2a 2f 20  .     /*  84 */ 
ba200 22 4d 6f 76 65 4c 65 22 2c 0a 20 20 20 20 20 2f  "MoveLe",.     /
ba210 2a 20 20 38 35 20 2a 2f 20 22 49 66 4e 6f 74 22  *  85 */ "IfNot"
ba220 2c 0a 20 20 20 20 20 2f 2a 20 20 38 36 20 2a 2f  ,.     /*  86 */
ba230 20 22 44 72 6f 70 54 61 62 6c 65 22 2c 0a 20 20   "DropTable",.  
ba240 20 20 20 2f 2a 20 20 38 37 20 2a 2f 20 22 42 69     /*  87 */ "Bi
ba250 74 4e 6f 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  tNot",.     /*  
ba260 38 38 20 2a 2f 20 22 53 74 72 69 6e 67 38 22 2c  88 */ "String8",
ba270 0a 20 20 20 20 20 2f 2a 20 20 38 39 20 2a 2f 20  .     /*  89 */ 
ba280 22 4d 61 6b 65 52 65 63 6f 72 64 22 2c 0a 20 20  "MakeRecord",.  
ba290 20 20 20 2f 2a 20 20 39 30 20 2a 2f 20 22 52 65     /*  90 */ "Re
ba2a0 73 75 6c 74 52 6f 77 22 2c 0a 20 20 20 20 20 2f  sultRow",.     /
ba2b0 2a 20 20 39 31 20 2a 2f 20 22 44 65 6c 65 74 65  *  91 */ "Delete
ba2c0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 32 20 2a  ",.     /*  92 *
ba2d0 2f 20 22 41 67 67 46 69 6e 61 6c 22 2c 0a 20 20  / "AggFinal",.  
ba2e0 20 20 20 2f 2a 20 20 39 33 20 2a 2f 20 22 43 6f     /*  93 */ "Co
ba2f0 6d 70 61 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20  mpare",.     /* 
ba300 20 39 34 20 2a 2f 20 22 47 6f 74 6f 22 2c 0a 20   94 */ "Goto",. 
ba310 20 20 20 20 2f 2a 20 20 39 35 20 2a 2f 20 22 54      /*  95 */ "T
ba320 61 62 6c 65 4c 6f 63 6b 22 2c 0a 20 20 20 20 20  ableLock",.     
ba330 2f 2a 20 20 39 36 20 2a 2f 20 22 46 69 66 6f 52  /*  96 */ "FifoR
ba340 65 61 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39  ead",.     /*  9
ba350 37 20 2a 2f 20 22 43 6c 65 61 72 22 2c 0a 20 20  7 */ "Clear",.  
ba360 20 20 20 2f 2a 20 20 39 38 20 2a 2f 20 22 4d 6f     /*  98 */ "Mo
ba370 76 65 4c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  veLt",.     /*  
ba380 39 39 20 2a 2f 20 22 56 65 72 69 66 79 43 6f 6f  99 */ "VerifyCoo
ba390 6b 69 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30  kie",.     /* 10
ba3a0 30 20 2a 2f 20 22 41 67 67 53 74 65 70 22 2c 0a  0 */ "AggStep",.
ba3b0 20 20 20 20 20 2f 2a 20 31 30 31 20 2a 2f 20 22       /* 101 */ "
ba3c0 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 22 2c 0a  SetNumColumns",.
ba3d0 20 20 20 20 20 2f 2a 20 31 30 32 20 2a 2f 20 22       /* 102 */ "
ba3e0 54 72 61 6e 73 61 63 74 69 6f 6e 22 2c 0a 20 20  Transaction",.  
ba3f0 20 20 20 2f 2a 20 31 30 33 20 2a 2f 20 22 56 46     /* 103 */ "VF
ba400 69 6c 74 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20  ilter",.     /* 
ba410 31 30 34 20 2a 2f 20 22 56 44 65 73 74 72 6f 79  104 */ "VDestroy
ba420 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 35 20 2a  ",.     /* 105 *
ba430 2f 20 22 43 6f 6e 74 65 78 74 50 6f 70 22 2c 0a  / "ContextPop",.
ba440 20 20 20 20 20 2f 2a 20 31 30 36 20 2a 2f 20 22       /* 106 */ "
ba450 4e 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31  Next",.     /* 1
ba460 30 37 20 2a 2f 20 22 49 64 78 49 6e 73 65 72 74  07 */ "IdxInsert
ba470 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 38 20 2a  ",.     /* 108 *
ba480 2f 20 22 49 6e 73 65 72 74 22 2c 0a 20 20 20 20  / "Insert",.    
ba490 20 2f 2a 20 31 30 39 20 2a 2f 20 22 44 65 73 74   /* 109 */ "Dest
ba4a0 72 6f 79 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31  roy",.     /* 11
ba4b0 30 20 2a 2f 20 22 52 65 61 64 43 6f 6f 6b 69 65  0 */ "ReadCookie
ba4c0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 31 20 2a  ",.     /* 111 *
ba4d0 2f 20 22 46 6f 72 63 65 49 6e 74 22 2c 0a 20 20  / "ForceInt",.  
ba4e0 20 20 20 2f 2a 20 31 31 32 20 2a 2f 20 22 4c 6f     /* 112 */ "Lo
ba4f0 61 64 41 6e 61 6c 79 73 69 73 22 2c 0a 20 20 20  adAnalysis",.   
ba500 20 20 2f 2a 20 31 31 33 20 2a 2f 20 22 45 78 70    /* 113 */ "Exp
ba510 6c 61 69 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31  lain",.     /* 1
ba520 31 34 20 2a 2f 20 22 4f 70 65 6e 50 73 65 75 64  14 */ "OpenPseud
ba530 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 35 20  o",.     /* 115 
ba540 2a 2f 20 22 4f 70 65 6e 45 70 68 65 6d 65 72 61  */ "OpenEphemera
ba550 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 36 20  l",.     /* 116 
ba560 2a 2f 20 22 4e 75 6c 6c 22 2c 0a 20 20 20 20 20  */ "Null",.     
ba570 2f 2a 20 31 31 37 20 2a 2f 20 22 4d 6f 76 65 22  /* 117 */ "Move"
ba580 2c 0a 20 20 20 20 20 2f 2a 20 31 31 38 20 2a 2f  ,.     /* 118 */
ba590 20 22 42 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a   "Blob",.     /*
ba5a0 20 31 31 39 20 2a 2f 20 22 52 65 77 69 6e 64 22   119 */ "Rewind"
ba5b0 2c 0a 20 20 20 20 20 2f 2a 20 31 32 30 20 2a 2f  ,.     /* 120 */
ba5c0 20 22 4d 6f 76 65 47 65 22 2c 0a 20 20 20 20 20   "MoveGe",.     
ba5d0 2f 2a 20 31 32 31 20 2a 2f 20 22 56 42 65 67 69  /* 121 */ "VBegi
ba5e0 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 32 20  n",.     /* 122 
ba5f0 2a 2f 20 22 56 55 70 64 61 74 65 22 2c 0a 20 20  */ "VUpdate",.  
ba600 20 20 20 2f 2a 20 31 32 33 20 2a 2f 20 22 49 66     /* 123 */ "If
ba610 5a 65 72 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 31  Zero",.     /* 1
ba620 32 34 20 2a 2f 20 22 56 43 72 65 61 74 65 22 2c  24 */ "VCreate",
ba630 0a 20 20 20 20 20 2f 2a 20 31 32 35 20 2a 2f 20  .     /* 125 */ 
ba640 22 46 6f 75 6e 64 22 2c 0a 20 20 20 20 20 2f 2a  "Found",.     /*
ba650 20 31 32 36 20 2a 2f 20 22 52 65 61 6c 22 2c 0a   126 */ "Real",.
ba660 20 20 20 20 20 2f 2a 20 31 32 37 20 2a 2f 20 22       /* 127 */ "
ba670 49 66 50 6f 73 22 2c 0a 20 20 20 20 20 2f 2a 20  IfPos",.     /* 
ba680 31 32 38 20 2a 2f 20 22 4e 75 6c 6c 52 6f 77 22  128 */ "NullRow"
ba690 2c 0a 20 20 20 20 20 2f 2a 20 31 32 39 20 2a 2f  ,.     /* 129 */
ba6a0 20 22 4a 75 6d 70 22 2c 0a 20 20 20 20 20 2f 2a   "Jump",.     /*
ba6b0 20 31 33 30 20 2a 2f 20 22 50 65 72 6d 75 74 61   130 */ "Permuta
ba6c0 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31  tion",.     /* 1
ba6d0 33 31 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31  31 */ "NotUsed_1
ba6e0 33 31 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 32  31",.     /* 132
ba6f0 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 32   */ "NotUsed_132
ba700 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 33 20 2a  ",.     /* 133 *
ba710 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 33 22 2c  / "NotUsed_133",
ba720 0a 20 20 20 20 20 2f 2a 20 31 33 34 20 2a 2f 20  .     /* 134 */ 
ba730 22 4e 6f 74 55 73 65 64 5f 31 33 34 22 2c 0a 20  "NotUsed_134",. 
ba740 20 20 20 20 2f 2a 20 31 33 35 20 2a 2f 20 22 4e      /* 135 */ "N
ba750 6f 74 55 73 65 64 5f 31 33 35 22 2c 0a 20 20 20  otUsed_135",.   
ba760 20 20 2f 2a 20 31 33 36 20 2a 2f 20 22 4e 6f 74    /* 136 */ "Not
ba770 55 73 65 64 5f 31 33 36 22 2c 0a 20 20 20 20 20  Used_136",.     
ba780 2f 2a 20 31 33 37 20 2a 2f 20 22 4e 6f 74 55 73  /* 137 */ "NotUs
ba790 65 64 5f 31 33 37 22 2c 0a 20 20 20 20 20 2f 2a  ed_137",.     /*
ba7a0 20 31 33 38 20 2a 2f 20 22 4e 6f 74 55 73 65 64   138 */ "NotUsed
ba7b0 5f 31 33 38 22 2c 0a 20 20 20 20 20 2f 2a 20 31  _138",.     /* 1
ba7c0 33 39 20 2a 2f 20 22 54 6f 54 65 78 74 22 2c 0a  39 */ "ToText",.
ba7d0 20 20 20 20 20 2f 2a 20 31 34 30 20 2a 2f 20 22       /* 140 */ "
ba7e0 54 6f 42 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a  ToBlob",.     /*
ba7f0 20 31 34 31 20 2a 2f 20 22 54 6f 4e 75 6d 65 72   141 */ "ToNumer
ba800 69 63 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 32  ic",.     /* 142
ba810 20 2a 2f 20 22 54 6f 49 6e 74 22 2c 0a 20 20 20   */ "ToInt",.   
ba820 20 20 2f 2a 20 31 34 33 20 2a 2f 20 22 54 6f 52    /* 143 */ "ToR
ba830 65 61 6c 22 2c 0a 20 20 7d 3b 0a 20 20 72 65 74  eal",.  };.  ret
ba840 75 72 6e 20 61 7a 4e 61 6d 65 5b 69 5d 3b 0a 7d  urn azName[i];.}
ba850 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
ba860 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
ba870 6f 70 63 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a  opcodes.c ******
ba880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba8a0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
ba8b0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
ba8c0 69 6c 65 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a  ile os_os2.c ***
ba8d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba8e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba8f0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
ba900 30 30 36 20 46 65 62 20 31 34 0a 2a 2a 0a 2a 2a  006 Feb 14.**.**
ba910 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
ba920 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
ba930 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
ba940 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
ba950 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
ba960 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
ba970 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
ba980 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
ba990 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
ba9a0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
ba9b0 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
ba9c0 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
ba9d0 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
ba9e0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
ba9f0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
baa00 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
baa10 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
baa20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
baa30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
baa40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
baa50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
baa60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
baa70 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
baa80 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61  ontains code tha
baa90 74 20 69 73 20 73 70 65 63 69 66 69 63 20 74 6f  t is specific to
baaa0 20 4f 53 2f 32 2e 0a 2a 2a 0a 2a 2a 20 24 49 64   OS/2..**.** $Id
baab0 3a 20 6f 73 5f 6f 73 32 2e 63 2c 76 20 31 2e 35  : os_os2.c,v 1.5
baac0 38 20 32 30 30 38 2f 31 31 2f 30 37 20 30 30 3a  8 2008/11/07 00:
baad0 30 36 3a 31 38 20 64 72 68 20 45 78 70 20 24 0a  06:18 drh Exp $.
baae0 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f  */...#if SQLITE_
baaf0 4f 53 5f 4f 53 32 0a 0a 2f 2a 0a 2a 2a 20 41 20  OS_OS2../*.** A 
bab00 4e 6f 74 65 20 41 62 6f 75 74 20 4d 65 6d 6f 72  Note About Memor
bab10 79 20 41 6c 6c 6f 63 61 74 69 6f 6e 3a 0a 2a 2a  y Allocation:.**
bab20 0a 2a 2a 20 54 68 69 73 20 64 72 69 76 65 72 20  .** This driver 
bab30 75 73 65 73 20 6d 61 6c 6c 6f 63 28 29 2f 66 72  uses malloc()/fr
bab40 65 65 28 29 20 64 69 72 65 63 74 6c 79 20 72 61  ee() directly ra
bab50 74 68 65 72 20 74 68 61 6e 20 67 6f 69 6e 67 20  ther than going 
bab60 74 68 72 6f 75 67 68 0a 2a 2a 20 74 68 65 20 53  through.** the S
bab70 51 4c 69 74 65 2d 77 72 61 70 70 65 72 73 20 73  QLite-wrappers s
bab80 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2f  qlite3_malloc()/
bab90 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 2e 20  sqlite3_free(). 
baba0 20 54 68 6f 73 65 20 77 72 61 70 70 65 72 73 0a   Those wrappers.
babb0 2a 2a 20 61 72 65 20 64 65 73 69 67 6e 65 64 20  ** are designed 
babc0 66 6f 72 20 75 73 65 20 6f 6e 20 65 6d 62 65 64  for use on embed
babd0 64 65 64 20 73 79 73 74 65 6d 73 20 77 68 65 72  ded systems wher
babe0 65 20 6d 65 6d 6f 72 79 20 69 73 20 73 63 61 72  e memory is scar
babf0 63 65 20 61 6e 64 0a 2a 2a 20 6d 61 6c 6c 6f 63  ce and.** malloc
bac00 20 66 61 69 6c 75 72 65 73 20 68 61 70 70 65 6e   failures happen
bac10 20 66 72 65 71 75 65 6e 74 6c 79 2e 20 20 4f 53   frequently.  OS
bac20 2f 32 20 64 6f 65 73 20 6e 6f 74 20 74 79 70 69  /2 does not typi
bac30 63 61 6c 6c 79 20 72 75 6e 20 6f 6e 0a 2a 2a 20  cally run on.** 
bac40 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d 73  embedded systems
bac50 2c 20 61 6e 64 20 77 68 65 6e 20 69 74 20 64 6f  , and when it do
bac60 65 73 20 74 68 65 20 64 65 76 65 6c 6f 70 65 72  es the developer
bac70 73 20 6e 6f 72 6d 61 6c 6c 79 20 68 61 76 65 20  s normally have 
bac80 62 69 67 67 65 72 0a 2a 2a 20 70 72 6f 62 6c 65  bigger.** proble
bac90 6d 73 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75  ms to worry abou
baca0 74 20 74 68 61 6e 20 72 75 6e 6e 69 6e 67 20 6f  t than running o
bacb0 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 20 53  ut of memory.  S
bacc0 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 74 0a 2a  o there is not.*
bacd0 2a 20 61 20 63 6f 6d 70 65 6c 6c 69 6e 67 20 6e  * a compelling n
bace0 65 65 64 20 74 6f 20 75 73 65 20 74 68 65 20 77  eed to use the w
bacf0 72 61 70 70 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 42  rappers..**.** B
bad00 75 74 20 74 68 65 72 65 20 69 73 20 61 20 67 6f  ut there is a go
bad10 6f 64 20 72 65 61 73 6f 6e 20 74 6f 20 6e 6f 74  od reason to not
bad20 20 75 73 65 20 74 68 65 20 77 72 61 70 70 65 72   use the wrapper
bad30 73 2e 20 20 49 66 20 77 65 20 75 73 65 20 74 68  s.  If we use th
bad40 65 0a 2a 2a 20 77 72 61 70 70 65 72 73 20 74 68  e.** wrappers th
bad50 65 6e 20 77 65 20 77 69 6c 6c 20 67 65 74 20 73  en we will get s
bad60 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 28  imulated malloc(
bad70 29 20 66 61 69 6c 75 72 65 73 20 77 69 74 68 69  ) failures withi
bad80 6e 20 74 68 69 73 0a 2a 2a 20 64 72 69 76 65 72  n this.** driver
bad90 2e 20 20 41 6e 64 20 74 68 61 74 20 63 61 75 73  .  And that caus
bada0 65 73 20 61 6c 6c 20 6b 69 6e 64 73 20 6f 66 20  es all kinds of 
badb0 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 6f 75 72  problems for our
badc0 20 74 65 73 74 73 2e 20 20 57 65 0a 2a 2a 20 63   tests.  We.** c
badd0 6f 75 6c 64 20 65 6e 68 61 6e 63 65 20 53 51 4c  ould enhance SQL
bade0 69 74 65 20 74 6f 20 64 65 61 6c 20 77 69 74 68  ite to deal with
badf0 20 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f   simulated mallo
bae00 63 20 66 61 69 6c 75 72 65 73 20 77 69 74 68 69  c failures withi
bae10 6e 0a 2a 2a 20 74 68 65 20 4f 53 20 64 72 69 76  n.** the OS driv
bae20 65 72 2c 20 62 75 74 20 74 68 65 20 63 6f 64 65  er, but the code
bae30 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 74 68   to deal with th
bae40 6f 73 65 20 66 61 69 6c 75 72 65 20 77 6f 75 6c  ose failure woul
bae50 64 20 6e 6f 74 0a 2a 2a 20 62 65 20 65 78 65 72  d not.** be exer
bae60 63 69 73 65 64 20 6f 6e 20 4c 69 6e 75 78 20 28  cised on Linux (
bae70 77 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 6e  which does not n
bae80 65 65 64 20 74 6f 20 6d 61 6c 6c 6f 63 28 29 20  eed to malloc() 
bae90 69 6e 20 74 68 65 20 64 72 69 76 65 72 29 0a 2a  in the driver).*
baea0 2a 20 61 6e 64 20 73 6f 20 77 65 20 77 6f 75 6c  * and so we woul
baeb0 64 20 68 61 76 65 20 64 69 66 66 69 63 75 6c 74  d have difficult
baec0 79 20 77 72 69 74 69 6e 67 20 63 6f 76 65 72 61  y writing covera
baed0 67 65 20 74 65 73 74 73 20 66 6f 72 20 74 68 61  ge tests for tha
baee0 74 0a 2a 2a 20 63 6f 64 65 2e 20 20 42 65 74 74  t.** code.  Bett
baef0 65 72 20 74 6f 20 6c 65 61 76 65 20 74 68 65 20  er to leave the 
baf00 63 6f 64 65 20 6f 75 74 2c 20 77 65 20 74 68 69  code out, we thi
baf10 6e 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f  nk..**.** The po
baf20 69 6e 74 20 6f 66 20 74 68 69 73 20 64 69 73 63  int of this disc
baf30 75 73 73 69 6f 6e 20 69 73 20 61 73 20 66 6f 6c  ussion is as fol
baf40 6c 6f 77 73 3a 20 20 57 68 65 6e 20 63 72 65 61  lows:  When crea
baf50 74 69 6e 67 20 61 20 6e 65 77 0a 2a 2a 20 4f 53  ting a new.** OS
baf60 20 6c 61 79 65 72 20 66 6f 72 20 61 6e 20 65 6d   layer for an em
baf70 62 65 64 64 65 64 20 73 79 73 74 65 6d 2c 20 69  bedded system, i
baf80 66 20 79 6f 75 20 75 73 65 20 74 68 69 73 20 66  f you use this f
baf90 69 6c 65 20 61 73 20 61 6e 20 65 78 61 6d 70 6c  ile as an exampl
bafa0 65 2c 0a 2a 2a 20 61 76 6f 69 64 20 74 68 65 20  e,.** avoid the 
bafb0 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2f  use of malloc()/
bafc0 66 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20 72  free().  Those r
bafd0 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6b 20  outines work ok 
bafe0 6f 6e 20 4f 53 2f 32 0a 2a 2a 20 64 65 73 6b 74  on OS/2.** deskt
baff0 6f 70 73 20 62 75 74 20 6e 6f 74 20 73 6f 20 77  ops but not so w
bb000 65 6c 6c 20 69 6e 20 65 6d 62 65 64 64 65 64 20  ell in embedded 
bb010 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 0a 2f 2a 0a  systems..*/../*.
bb020 2a 2a 20 4d 61 63 72 6f 73 20 75 73 65 64 20 74  ** Macros used t
bb030 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74  o determine whet
bb040 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f 20 75 73  her or not to us
bb050 65 20 74 68 72 65 61 64 73 2e 0a 2a 2f 0a 23 69  e threads..*/.#i
bb060 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
bb070 5f 54 48 52 45 41 44 53 41 46 45 29 20 26 26 20  _THREADSAFE) && 
bb080 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
bb090 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  E.# define SQLIT
bb0a0 45 5f 4f 53 32 5f 54 48 52 45 41 44 53 20 31 0a  E_OS2_THREADS 1.
bb0b0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e  #endif../*.** In
bb0c0 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 20  clude code that 
bb0d0 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c  is common to all
bb0e0 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f   os_*.c files.*/
bb0f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
bb100 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d   Include os_comm
bb110 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  on.h in the midd
bb120 6c 65 20 6f 66 20 6f 73 5f 6f 73 32 2e 63 20 2a  le of os_os2.c *
bb130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
bb140 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
bb150 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63   Begin file os_c
bb160 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  ommon.h ********
bb170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
bb190 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20  ./*.** 2004 May 
bb1a0 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  22.**.** The aut
bb1b0 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
bb1c0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
bb1d0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
bb1e0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
bb1f0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
bb200 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
bb210 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
bb220 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
bb230 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
bb240 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
bb250 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
bb260 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
bb270 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
bb280 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
bb290 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
bb2a0 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
bb2b0 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
bb2c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb2d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb2e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb2f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb300 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
bb310 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
bb320 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 74  macros and a lit
bb330 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 20  tle bit of code 
bb340 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74  that is common t
bb350 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20  o.** all of the 
bb360 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 69  platform-specifi
bb370 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29  c files (os_*.c)
bb380 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 65   and is #include
bb390 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20  d into those.** 
bb3a0 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  files..**.** Thi
bb3b0 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65  s file should be
bb3c0 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 68   #included by th
bb3d0 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f  e os_*.c files o
bb3e0 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 20  nly.  It is not 
bb3f0 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 72  a.** general pur
bb400 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c 65  pose header file
bb410 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f  ..**.** $Id: os_
bb420 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 2e 33 37 20  common.h,v 1.37 
bb430 32 30 30 38 2f 30 35 2f 32 39 20 32 30 3a 32 32  2008/05/29 20:22
bb440 3a 33 37 20 73 68 61 6e 65 20 45 78 70 20 24 0a  :37 shane Exp $.
bb450 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f 53 5f 43  */.#ifndef _OS_C
bb460 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 69 6e 65  OMMON_H_.#define
bb470 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a   _OS_COMMON_H_..
bb480 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 74  /*.** At least t
bb490 77 6f 20 62 75 67 73 20 68 61 76 65 20 73 6c 69  wo bugs have sli
bb4a0 70 70 65 64 20 69 6e 20 62 65 63 61 75 73 65 20  pped in because 
bb4b0 77 65 20 63 68 61 6e 67 65 64 20 74 68 65 20 4d  we changed the M
bb4c0 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a 2a 20 6d  EMORY_DEBUG.** m
bb4d0 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 45 5f 44  acro to SQLITE_D
bb4e0 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 20 6f 6c  EBUG and some ol
bb4f0 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 20 68 61  der makefiles ha
bb500 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 20  ve not yet made 
bb510 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 2e 20 20  the.** switch.  
bb520 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  The following co
bb530 64 65 20 73 68 6f 75 6c 64 20 63 61 74 63 68 20  de should catch 
bb540 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 61 74 20  this problem at 
bb550 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f  compile-time..*/
bb560 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 59 5f 44  .#ifdef MEMORY_D
bb570 45 42 55 47 0a 23 20 65 72 72 6f 72 20 22 54 68  EBUG.# error "Th
bb580 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 20 6d  e MEMORY_DEBUG m
bb590 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c 65 74 65  acro is obsolete
bb5a0 2e 20 20 55 73 65 20 53 51 4c 49 54 45 5f 44 45  .  Use SQLITE_DE
bb5b0 42 55 47 20 69 6e 73 74 65 61 64 2e 22 0a 23 65  BUG instead.".#e
bb5c0 6e 64 69 66 0a 0a 0a 2f 2a 0a 20 2a 20 57 68 65  ndif.../*. * Whe
bb5d0 6e 20 74 65 73 74 69 6e 67 2c 20 74 68 69 73 20  n testing, this 
bb5e0 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
bb5f0 73 74 6f 72 65 73 20 74 68 65 20 6c 6f 63 61 74  stores the locat
bb600 69 6f 6e 20 6f 66 20 74 68 65 0a 20 2a 20 70 65  ion of the. * pe
bb610 6e 64 69 6e 67 2d 62 79 74 65 20 69 6e 20 74 68  nding-byte in th
bb620 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
bb630 0a 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  . */.#ifdef SQLI
bb640 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41  TE_TEST.SQLITE_A
bb650 50 49 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  PI unsigned int 
bb660 73 71 6c 69 74 65 33 5f 70 65 6e 64 69 6e 67 5f  sqlite3_pending_
bb670 62 79 74 65 20 3d 20 30 78 34 30 30 30 30 30 30  byte = 0x4000000
bb680 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65  0;.#endif..#ifde
bb690 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53  f SQLITE_DEBUG.S
bb6a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
bb6b0 74 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  t sqlite3OSTrace
bb6c0 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 53   = 0;.#define OS
bb6d0 54 52 41 43 45 31 28 58 29 20 20 20 20 20 20 20  TRACE1(X)       
bb6e0 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54    if( sqlite3OST
bb6f0 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65  race ) sqlite3De
bb700 62 75 67 50 72 69 6e 74 66 28 58 29 0a 23 64 65  bugPrintf(X).#de
bb710 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c  fine OSTRACE2(X,
bb720 59 29 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  Y)       if( sql
bb730 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71  ite3OSTrace ) sq
bb740 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
bb750 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53  (X,Y).#define OS
bb760 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 20  TRACE3(X,Y,Z)   
bb770 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54    if( sqlite3OST
bb780 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65  race ) sqlite3De
bb790 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29  bugPrintf(X,Y,Z)
bb7a0 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
bb7b0 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28  4(X,Y,Z,A)   if(
bb7c0 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20   sqlite3OSTrace 
bb7d0 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
bb7e0 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64  intf(X,Y,Z,A).#d
bb7f0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58  efine OSTRACE5(X
bb800 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73 71  ,Y,Z,A,B) if( sq
bb810 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73  lite3OSTrace ) s
bb820 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
bb830 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65  f(X,Y,Z,A,B).#de
bb840 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c  fine OSTRACE6(X,
bb850 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20  Y,Z,A,B,C) \.   
bb860 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61   if(sqlite3OSTra
bb870 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  ce) sqlite3Debug
bb880 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42  Printf(X,Y,Z,A,B
bb890 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,C).#define OSTR
bb8a0 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  ACE7(X,Y,Z,A,B,C
bb8b0 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c  ,D) \.    if(sql
bb8c0 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c  ite3OSTrace) sql
bb8d0 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
bb8e0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23  X,Y,Z,A,B,C,D).#
bb8f0 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 53 54  else.#define OST
bb900 52 41 43 45 31 28 58 29 0a 23 64 65 66 69 6e 65  RACE1(X).#define
bb910 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 0a 23   OSTRACE2(X,Y).#
bb920 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28  define OSTRACE3(
bb930 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f  X,Y,Z).#define O
bb940 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29  STRACE4(X,Y,Z,A)
bb950 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
bb960 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65  5(X,Y,Z,A,B).#de
bb970 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c  fine OSTRACE6(X,
bb980 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69  Y,Z,A,B,C).#defi
bb990 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c  ne OSTRACE7(X,Y,
bb9a0 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69  Z,A,B,C,D).#endi
bb9b0 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20  f../*.** Macros 
bb9c0 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  for performance 
bb9d0 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c  tracing.  Normal
bb9e0 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20  ly turned off.  
bb9f0 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e  Only works.** on
bba00 20 69 34 38 36 20 68 61 72 64 77 61 72 65 2e 0a   i486 hardware..
bba10 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
bba20 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41  _PERFORMANCE_TRA
bba30 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d  CE../* .** hwtim
bba40 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c  e.h contains inl
bba50 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f  ine assembler co
bba60 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74  de for implement
bba70 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72  ing .** high-per
bba80 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20  formance timing 
bba90 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a  routines..*/./**
bbaa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
bbab0 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e  lude hwtime.h in
bbac0 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f   the middle of o
bbad0 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a  s_common.h *****
bbae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
bbaf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
bbb00 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68  in file hwtime.h
bbb10 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
bbb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
bbb40 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a  ** 2008 May 27.*
bbb50 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
bbb60 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
bbb70 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
bbb80 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
bbb90 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
bbba0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
bbbb0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
bbbc0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
bbbd0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
bbbe0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
bbbf0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
bbc00 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
bbc10 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
bbc20 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
bbc30 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
bbc40 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
bbc50 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
bbc60 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
bbc70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbc80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbc90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbcb0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
bbcc0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69  le contains inli
bbcd0 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20  ne asm code for 
bbce0 72 65 74 72 69 65 76 69 6e 67 20 22 68 69 67 68  retrieving "high
bbcf0 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a  -performance".**
bbd00 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38   counters for x8
bbd10 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2a  6 class CPUs..**
bbd20 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 6d 65 2e  .** $Id: hwtime.
bbd30 68 2c 76 20 31 2e 33 20 32 30 30 38 2f 30 38 2f  h,v 1.3 2008/08/
bbd40 30 31 20 31 34 3a 33 33 3a 31 35 20 73 68 61 6e  01 14:33:15 shan
bbd50 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64  e Exp $.*/.#ifnd
bbd60 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64  ef _HWTIME_H_.#d
bbd70 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f  efine _HWTIME_H_
bbd80 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
bbd90 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e  owing routine on
bbda0 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74  ly works on pent
bbdb0 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65  ium-class (or ne
bbdc0 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72 73 2e  wer) processors.
bbdd0 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68 65 20  .** It uses the 
bbde0 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74 6f 20  RDTSC opcode to 
bbdf0 72 65 61 64 20 74 68 65 20 63 79 63 6c 65 20 63  read the cycle c
bbe00 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 6f  ount value out o
bbe10 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73  f the.** process
bbe20 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74  or and returns t
bbe30 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68 69 73  hat value.  This
bbe40 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72   can be used for
bbe50 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f   high-res.** pro
bbe60 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28  filing..*/.#if (
bbe70 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f  defined(__GNUC__
bbe80 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53  ) || defined(_MS
bbe90 43 5f 56 45 52 29 29 20 26 26 20 5c 0a 20 20 20  C_VER)) && \.   
bbea0 20 20 20 28 64 65 66 69 6e 65 64 28 69 33 38 36     (defined(i386
bbeb0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69  ) || defined(__i
bbec0 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65  386__) || define
bbed0 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23  d(_M_IX86))..  #
bbee0 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55  if defined(__GNU
bbef0 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65  C__)..  __inline
bbf00 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  __ sqlite_uint64
bbf10 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76   sqlite3Hwtime(v
bbf20 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 67  oid){.     unsig
bbf30 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a  ned int lo, hi;.
bbf40 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76       __asm__ __v
bbf50 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73  olatile__ ("rdts
bbf60 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20  c" : "=a" (lo), 
bbf70 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 20 20 20  "=d" (hi));.    
bbf80 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f   return (sqlite_
bbf90 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20  uint64)hi << 32 
bbfa0 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c  | lo;.  }..  #el
bbfb0 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f  if defined(_MSC_
bbfc0 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70  VER)..  __declsp
bbfd0 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69  ec(naked) __inli
bbfe0 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  ne sqlite_uint64
bbff0 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 33   __cdecl sqlite3
bc000 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20  Hwtime(void){.  
bc010 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20     __asm {.     
bc020 20 20 20 72 64 74 73 63 0a 20 20 20 20 20 20 20     rdtsc.       
bc030 20 72 65 74 20 20 20 20 20 20 20 3b 20 72 65 74   ret       ; ret
bc040 75 72 6e 20 76 61 6c 75 65 20 61 74 20 45 44 58  urn value at EDX
bc050 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a  :EAX.     }.  }.
bc060 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66  .  #endif..#elif
bc070 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43   (defined(__GNUC
bc080 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f  __) && defined(_
bc090 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f  _x86_64__))..  _
bc0a0 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65  _inline__ sqlite
bc0b0 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48  _uint64 sqlite3H
bc0c0 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20  wtime(void){.   
bc0d0 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67     unsigned long
bc0e0 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73   val;.      __as
bc0f0 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f  m__ __volatile__
bc100 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 41 22   ("rdtsc" : "=A"
bc110 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 72   (val));.      r
bc120 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20  eturn val;.  }. 
bc130 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28  .#elif (defined(
bc140 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66  __GNUC__) && def
bc150 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a  ined(__ppc__))..
bc160 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c    __inline__ sql
bc170 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74  ite_uint64 sqlit
bc180 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a  e3Hwtime(void){.
bc190 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c        unsigned l
bc1a0 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b  ong long retval;
bc1b0 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  .      unsigned 
bc1c0 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20  long junk;.     
bc1d0 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74   __asm__ __volat
bc1e0 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20  ile__ ("\n\.    
bc1f0 20 20 20 20 20 20 31 3a 20 20 20 20 20 20 6d 66        1:      mf
bc200 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20  tbu   %1\n\.    
bc210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66                mf
bc220 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20  tb    %L0\n\.   
bc230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d                 m
bc240 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20  ftbu   %0\n\.   
bc250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
bc260 6d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a  mpw    %0,%1\n\.
bc270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bc280 20 20 62 6e 65 20 20 20 20 20 31 62 22 0a 20 20    bne     1b".  
bc290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bc2a0 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c 29 2c  : "=r" (retval),
bc2b0 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20   "=r" (junk));. 
bc2c0 20 20 20 20 20 72 65 74 75 72 6e 20 72 65 74 76       return retv
bc2d0 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a  al;.  }..#else..
bc2e0 20 20 23 65 72 72 6f 72 20 4e 65 65 64 20 69 6d    #error Need im
bc2f0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
bc300 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20  sqlite3Hwtime() 
bc310 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72  for your platfor
bc320 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f  m...  /*.  ** To
bc330 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74   compile without
bc340 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71   implementing sq
bc350 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f  lite3Hwtime() fo
bc360 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c  r your platform,
bc370 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65  .  ** you can re
bc380 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 65 20 23  move the above #
bc390 65 72 72 6f 72 20 61 6e 64 20 75 73 65 20 74 68  error and use th
bc3a0 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a  e following.  **
bc3b0 20 73 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20   stub function. 
bc3c0 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74   You will lose t
bc3d0 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 20 66 6f  iming support fo
bc3e0 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74  r many.  ** of t
bc3f0 68 65 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64  he debugging and
bc400 20 74 65 73 74 69 6e 67 20 75 74 69 6c 69 74 69   testing utiliti
bc410 65 73 2c 20 62 75 74 20 69 74 20 73 68 6f 75 6c  es, but it shoul
bc420 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20  d at.  ** least 
bc430 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e  compile and run.
bc440 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  .  */.SQLITE_PRI
bc450 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69  VATE   sqlite_ui
bc460 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69  nt64 sqlite3Hwti
bc470 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  me(void){ return
bc480 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34   ((sqlite_uint64
bc490 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a  )0); }..#endif..
bc4a0 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
bc4b0 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a  ed(_HWTIME_H_) *
bc4c0 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
bc4d0 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69 6d 65  ** End of hwtime
bc4e0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
bc4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc510 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
bc520 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
bc530 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
bc540 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a  in os_common.h *
bc550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc560 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 74  */..static sqlit
bc570 65 5f 75 69 6e 74 36 34 20 67 5f 73 74 61 72 74  e_uint64 g_start
bc580 3b 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f  ;.static sqlite_
bc590 75 69 6e 74 36 34 20 67 5f 65 6c 61 70 73 65 64  uint64 g_elapsed
bc5a0 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f  ;.#define TIMER_
bc5b0 53 54 41 52 54 20 20 20 20 20 20 20 67 5f 73 74  START       g_st
bc5c0 61 72 74 3d 73 71 6c 69 74 65 33 48 77 74 69 6d  art=sqlite3Hwtim
bc5d0 65 28 29 0a 23 64 65 66 69 6e 65 20 54 49 4d 45  e().#define TIME
bc5e0 52 5f 45 4e 44 20 20 20 20 20 20 20 20 20 67 5f  R_END         g_
bc5f0 65 6c 61 70 73 65 64 3d 73 71 6c 69 74 65 33 48  elapsed=sqlite3H
bc600 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a  wtime()-g_start.
bc610 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c  #define TIMER_EL
bc620 41 50 53 45 44 20 20 20 20 20 67 5f 65 6c 61 70  APSED     g_elap
bc630 73 65 64 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e  sed.#else.#defin
bc640 65 20 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 64  e TIMER_START.#d
bc650 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a  efine TIMER_END.
bc660 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c  #define TIMER_EL
bc670 41 50 53 45 44 20 20 20 20 20 28 28 73 71 6c 69  APSED     ((sqli
bc680 74 65 5f 75 69 6e 74 36 34 29 30 29 0a 23 65 6e  te_uint64)0).#en
bc690 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65  dif../*.** If we
bc6a0 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 74 68   compile with th
bc6b0 65 20 53 51 4c 49 54 45 5f 54 45 53 54 20 6d 61  e SQLITE_TEST ma
bc6c0 63 72 6f 20 73 65 74 2c 20 74 68 65 6e 20 74 68  cro set, then th
bc6d0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63  e following bloc
bc6e0 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c  k.** of code wil
bc6f0 6c 20 67 69 76 65 20 75 73 20 74 68 65 20 61 62  l give us the ab
bc700 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74  ility to simulat
bc710 65 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72  e a disk I/O err
bc720 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20  or.  This.** is 
bc730 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  used for testing
bc740 20 74 68 65 20 49 2f 4f 20 72 65 63 6f 76 65 72   the I/O recover
bc750 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64  y logic..*/.#ifd
bc760 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
bc770 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
bc780 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68  lite3_io_error_h
bc790 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  it = 0;         
bc7a0 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62     /* Total numb
bc7b0 65 72 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72 73  er of I/O Errors
bc7c0 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   */.SQLITE_API i
bc7d0 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  nt sqlite3_io_er
bc7e0 72 6f 72 5f 68 61 72 64 68 69 74 20 3d 20 30 3b  ror_hardhit = 0;
bc7f0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
bc800 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20  r of non-benign 
bc810 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45  errors */.SQLITE
bc820 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
bc830 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e  _io_error_pendin
bc840 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a  g = 0;        /*
bc850 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20 66   Count down to f
bc860 69 72 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a  irst I/O error *
bc870 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
bc880 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
bc890 72 5f 70 65 72 73 69 73 74 20 3d 20 30 3b 20 20  r_persist = 0;  
bc8a0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
bc8b0 20 49 2f 4f 20 65 72 72 6f 72 73 20 70 65 72 73   I/O errors pers
bc8c0 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ist */.SQLITE_AP
bc8d0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f  I int sqlite3_io
bc8e0 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20  _error_benign = 
bc8f0 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  0;         /* Tr
bc900 75 65 20 69 66 20 65 72 72 6f 72 73 20 61 72 65  ue if errors are
bc910 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 54   benign */.SQLIT
bc920 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
bc930 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69  3_diskfull_pendi
bc940 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41  ng = 0;.SQLITE_A
bc950 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64  PI int sqlite3_d
bc960 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65  iskfull = 0;.#de
bc970 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45  fine SimulateIOE
bc980 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 20 73 71  rrorBenign(X) sq
bc990 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62  lite3_io_error_b
bc9a0 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65 66 69 6e  enign=(X).#defin
bc9b0 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f  e SimulateIOErro
bc9c0 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 69 66 28  r(CODE)  \.  if(
bc9d0 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72   (sqlite3_io_err
bc9e0 6f 72 5f 70 65 72 73 69 73 74 20 26 26 20 73 71  or_persist && sq
bc9f0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68  lite3_io_error_h
bca00 69 74 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c 20  it) \.       || 
bca10 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
bca20 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20  _pending-- == 1 
bca30 29 20 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20  )  \.           
bca40 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 72     { local_ioerr
bca50 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61 74  (); CODE; }.stat
bca60 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f  ic void local_io
bca70 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 41 43 45  err(){.  IOTRACE
bca80 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a 20  (("IOERR\n"));. 
bca90 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
bcaa0 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20 21  r_hit++;.  if( !
bcab0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
bcac0 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c 69 74 65  _benign ) sqlite
bcad0 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68  3_io_error_hardh
bcae0 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 20  it++;.}.#define 
bcaf0 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c  SimulateDiskfull
bcb00 45 72 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20 20  Error(CODE) \.  
bcb10 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73   if( sqlite3_dis
bcb20 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 7b  kfull_pending ){
bcb30 20 5c 0a 20 20 20 20 20 69 66 28 20 73 71 6c 69   \.     if( sqli
bcb40 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e  te3_diskfull_pen
bcb50 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a 20  ding == 1 ){ \. 
bcb60 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72        local_ioer
bcb70 72 28 29 3b 20 5c 0a 20 20 20 20 20 20 20 73 71  r(); \.       sq
bcb80 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d  lite3_diskfull =
bcb90 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c   1; \.       sql
bcba0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69  ite3_io_error_hi
bcbb0 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20  t = 1; \.       
bcbc0 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65 6c  CODE; \.     }el
bcbd0 73 65 7b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c  se{ \.       sql
bcbe0 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65  ite3_diskfull_pe
bcbf0 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 20  nding--; \.     
bcc00 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a 23  } \.   }.#else.#
bcc10 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49  define SimulateI
bcc20 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 0a  OErrorBenign(X).
bcc30 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65  #define Simulate
bcc40 49 4f 45 72 72 6f 72 28 41 29 0a 23 64 65 66 69  IOError(A).#defi
bcc50 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66  ne SimulateDiskf
bcc60 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e 64  ullError(A).#end
bcc70 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74  if../*.** When t
bcc80 65 73 74 69 6e 67 2c 20 6b 65 65 70 20 61 20 63  esting, keep a c
bcc90 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62  ount of the numb
bcca0 65 72 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65 73  er of open files
bccb0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
bccc0 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41  TE_TEST.SQLITE_A
bccd0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
bcce0 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d  pen_file_count =
bccf0 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e   0;.#define Open
bcd00 43 6f 75 6e 74 65 72 28 58 29 20 20 73 71 6c 69  Counter(X)  sqli
bcd10 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f  te3_open_file_co
bcd20 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a 23  unt+=(X).#else.#
bcd30 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74  define OpenCount
bcd40 65 72 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65  er(X).#endif..#e
bcd50 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
bcd60 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20  (_OS_COMMON_H_) 
bcd70 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
bcd80 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63 6f  *** End of os_co
bcd90 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mmon.h *********
bcda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcdb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcdc0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
bcdd0 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
bcde0 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
bcdf0 20 69 6e 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a   in os_os2.c ***
bce00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bce10 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f  **/../*.** The o
bce20 73 32 46 69 6c 65 20 73 74 72 75 63 74 75 72 65  s2File structure
bce30 20 69 73 20 73 75 62 63 6c 61 73 73 20 6f 66 20   is subclass of 
bce40 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 70 65  sqlite3_file spe
bce50 63 69 66 69 63 20 66 6f 72 20 74 68 65 20 4f 53  cific for the OS
bce60 2f 32 0a 2a 2a 20 70 72 6f 74 61 62 69 6c 69 74  /2.** protabilit
bce70 79 20 6c 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 65  y layer..*/.type
bce80 64 65 66 20 73 74 72 75 63 74 20 6f 73 32 46 69  def struct os2Fi
bce90 6c 65 20 6f 73 32 46 69 6c 65 3b 0a 73 74 72 75  le os2File;.stru
bcea0 63 74 20 6f 73 32 46 69 6c 65 20 7b 0a 20 20 63  ct os2File {.  c
bceb0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f  onst sqlite3_io_
bcec0 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64  methods *pMethod
bced0 3b 20 20 2f 2a 20 41 6c 77 61 79 73 20 74 68 65  ;  /* Always the
bcee0 20 66 69 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a   first entry */.
bcef0 20 20 48 46 49 4c 45 20 68 3b 20 20 20 20 20 20    HFILE h;      
bcf00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48              /* H
bcf10 61 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73  andle for access
bcf20 69 6e 67 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a  ing the file */.
bcf30 20 20 63 68 61 72 2a 20 70 61 74 68 54 6f 44 65    char* pathToDe
bcf40 6c 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  l;          /* N
bcf50 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64  ame of file to d
bcf60 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65 2c 20  elete on close, 
bcf70 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 2a 2f 0a 20  NULL if not */. 
bcf80 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6c   unsigned char l
bcf90 6f 63 6b 74 79 70 65 3b 20 20 20 2f 2a 20 54 79  ocktype;   /* Ty
bcfa0 70 65 20 6f 66 20 6c 6f 63 6b 20 63 75 72 72 65  pe of lock curre
bcfb0 6e 74 6c 79 20 68 65 6c 64 20 6f 6e 20 74 68 69  ntly held on thi
bcfc0 73 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 23 64  s file */.};..#d
bcfd0 65 66 69 6e 65 20 4c 4f 43 4b 5f 54 49 4d 45 4f  efine LOCK_TIMEO
bcfe0 55 54 20 31 30 4c 20 2f 2a 20 74 68 65 20 64 65  UT 10L /* the de
bcff0 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 20 74 69  fault locking ti
bd000 6d 65 6f 75 74 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  meout */../*****
bd010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd050 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20  ********.** The 
bd060 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f  next group of ro
bd070 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74  utines implement
bd080 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73   the I/O methods
bd090 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79   specified.** by
bd0a0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f   the sqlite3_io_
bd0b0 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a  methods object..
bd0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd0d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
bd110 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66  ./*.** Close a f
bd120 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ile..*/.static i
bd130 6e 74 20 6f 73 32 43 6c 6f 73 65 28 20 73 71 6c  nt os2Close( sql
bd140 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 20 29 7b  ite3_file *id ){
bd150 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e  .  APIRET rc = N
bd160 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f 73 32 46 69  O_ERROR;.  os2Fi
bd170 6c 65 20 2a 70 46 69 6c 65 3b 0a 20 20 69 66 28  le *pFile;.  if(
bd180 20 69 64 20 26 26 20 28 70 46 69 6c 65 20 3d 20   id && (pFile = 
bd190 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 20 21 3d  (os2File*)id) !=
bd1a0 20 30 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43   0 ){.    OSTRAC
bd1b0 45 32 28 20 22 43 4c 4f 53 45 20 25 64 5c 6e 22  E2( "CLOSE %d\n"
bd1c0 2c 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20  , pFile->h );.  
bd1d0 20 20 72 63 20 3d 20 44 6f 73 43 6c 6f 73 65 28    rc = DosClose(
bd1e0 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20   pFile->h );.   
bd1f0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
bd200 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20   = NO_LOCK;.    
bd210 69 66 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54  if( pFile->pathT
bd220 6f 44 65 6c 20 21 3d 20 4e 55 4c 4c 20 29 7b 0a  oDel != NULL ){.
bd230 20 20 20 20 20 20 72 63 20 3d 20 44 6f 73 46 6f        rc = DosFo
bd240 72 63 65 44 65 6c 65 74 65 28 20 28 50 53 5a 29  rceDelete( (PSZ)
bd250 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c  pFile->pathToDel
bd260 20 29 3b 0a 20 20 20 20 20 20 66 72 65 65 28 20   );.      free( 
bd270 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c  pFile->pathToDel
bd280 20 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d   );.      pFile-
bd290 3e 70 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c  >pathToDel = NUL
bd2a0 4c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 64 20  L;.    }.    id 
bd2b0 3d 20 30 3b 0a 20 20 20 20 4f 70 65 6e 43 6f 75  = 0;.    OpenCou
bd2c0 6e 74 65 72 28 20 2d 31 20 29 3b 0a 20 20 7d 0a  nter( -1 );.  }.
bd2d0 0a 20 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20  .  return rc == 
bd2e0 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54  NO_ERROR ? SQLIT
bd2f0 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f  E_OK : SQLITE_IO
bd300 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ERR;.}../*.** Re
bd310 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61 20 66  ad data from a f
bd320 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 66 66 65  ile into a buffe
bd330 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54  r.  Return SQLIT
bd340 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62  E_OK if all.** b
bd350 79 74 65 73 20 77 65 72 65 20 72 65 61 64 20 73  ytes were read s
bd360 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6e 64 20  uccessfully and 
bd370 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 69 66 20  SQLITE_IOERR if 
bd380 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a  anything goes.**
bd390 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69   wrong..*/.stati
bd3a0 63 20 69 6e 74 20 6f 73 32 52 65 61 64 28 0a 20  c int os2Read(. 
bd3b0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69   sqlite3_file *i
bd3c0 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d,              
bd3d0 20 2f 2a 20 46 69 6c 65 20 74 6f 20 72 65 61 64   /* File to read
bd3e0 20 66 72 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 20   from */.  void 
bd3f0 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 20  *pBuf,          
bd400 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
bd410 69 74 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f  ite content into
bd420 20 74 68 69 73 20 62 75 66 66 65 72 20 2a 2f 0a   this buffer */.
bd430 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20    int amt,      
bd440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bd450 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
bd460 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a  ytes to read */.
bd470 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
bd480 6f 66 66 73 65 74 20 20 20 20 20 20 20 20 20 20  offset          
bd490 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69    /* Begin readi
bd4a0 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 73 65  ng at this offse
bd4b0 74 20 2a 2f 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20  t */.){.  ULONG 
bd4c0 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 20 3d 20 30  fileLocation = 0
bd4d0 4c 3b 0a 20 20 55 4c 4f 4e 47 20 67 6f 74 3b 0a  L;.  ULONG got;.
bd4e0 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65    os2File *pFile
bd4f0 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b   = (os2File*)id;
bd500 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30  .  assert( id!=0
bd510 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f   );.  SimulateIO
bd520 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
bd530 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20  LITE_IOERR_READ 
bd540 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22  );.  OSTRACE3( "
bd550 52 45 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c  READ %d lock=%d\
bd560 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46  n", pFile->h, pF
bd570 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b  ile->locktype );
bd580 0a 20 20 69 66 28 20 44 6f 73 53 65 74 46 69 6c  .  if( DosSetFil
bd590 65 50 74 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6f  ePtr(pFile->h, o
bd5a0 66 66 73 65 74 2c 20 46 49 4c 45 5f 42 45 47 49  ffset, FILE_BEGI
bd5b0 4e 2c 20 26 66 69 6c 65 4c 6f 63 61 74 69 6f 6e  N, &fileLocation
bd5c0 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b  ) != NO_ERROR ){
bd5d0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
bd5e0 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 20 20  TE_IOERR;.  }.  
bd5f0 69 66 28 20 44 6f 73 52 65 61 64 28 20 70 46 69  if( DosRead( pFi
bd600 6c 65 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74  le->h, pBuf, amt
bd610 2c 20 26 67 6f 74 20 29 20 21 3d 20 4e 4f 5f 45  , &got ) != NO_E
bd620 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75  RROR ){.    retu
bd630 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
bd640 52 45 41 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20  READ;.  }.  if( 
bd650 67 6f 74 20 3d 3d 20 28 55 4c 4f 4e 47 29 61 6d  got == (ULONG)am
bd660 74 20 29 0a 20 20 20 20 72 65 74 75 72 6e 20 53  t ).    return S
bd670 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 65 6c 73 65  QLITE_OK;.  else
bd680 20 7b 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64   {.    /* Unread
bd690 20 70 6f 72 74 69 6f 6e 73 20 6f 66 20 74 68 65   portions of the
bd6a0 20 69 6e 70 75 74 20 62 75 66 66 65 72 20 6d 75   input buffer mu
bd6b0 73 74 20 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65  st be zero-fille
bd6c0 64 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28  d */.    memset(
bd6d0 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b 67  &((char*)pBuf)[g
bd6e0 6f 74 5d 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29  ot], 0, amt-got)
bd6f0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
bd700 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f  ITE_IOERR_SHORT_
bd710 52 45 41 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  READ;.  }.}../*.
bd720 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 66 72  ** Write data fr
bd730 6f 6d 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f  om a buffer into
bd740 20 61 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e   a file.  Return
bd750 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75   SQLITE_OK on su
bd760 63 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65  ccess.** or some
bd770 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64   other error cod
bd780 65 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f  e on failure..*/
bd790 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 57  .static int os2W
bd7a0 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  rite(.  sqlite3_
bd7b0 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20  file *id,       
bd7c0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20          /* File 
bd7d0 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 2a 2f  to write into */
bd7e0 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  .  const void *p
bd7f0 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20  Buf,            
bd800 20 20 20 2f 2a 20 54 68 65 20 62 79 74 65 73 20     /* The bytes 
bd810 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 2a 2f  to be written */
bd820 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20  .  int amt,     
bd830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bd840 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
bd850 62 79 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a  bytes to write *
bd860 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  /.  sqlite3_int6
bd870 34 20 6f 66 66 73 65 74 20 20 20 20 20 20 20 20  4 offset        
bd880 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e      /* Offset in
bd890 74 6f 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62  to the file to b
bd8a0 65 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20  egin writing at 
bd8b0 2a 2f 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69  */.){.  ULONG fi
bd8c0 6c 65 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b  leLocation = 0L;
bd8d0 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e  .  APIRET rc = N
bd8e0 4f 5f 45 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47  O_ERROR;.  ULONG
bd8f0 20 77 72 6f 74 65 3b 0a 20 20 6f 73 32 46 69 6c   wrote;.  os2Fil
bd900 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46  e *pFile = (os2F
bd910 69 6c 65 2a 29 69 64 3b 0a 20 20 61 73 73 65 72  ile*)id;.  asser
bd920 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69  t( id!=0 );.  Si
bd930 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72  mulateIOError( r
bd940 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
bd950 52 52 5f 57 52 49 54 45 20 29 3b 0a 20 20 53 69  RR_WRITE );.  Si
bd960 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72  mulateDiskfullEr
bd970 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49  ror( return SQLI
bd980 54 45 5f 46 55 4c 4c 20 29 3b 0a 20 20 4f 53 54  TE_FULL );.  OST
bd990 52 41 43 45 33 28 20 22 57 52 49 54 45 20 25 64  RACE3( "WRITE %d
bd9a0 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69   lock=%d\n", pFi
bd9b0 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f  le->h, pFile->lo
bd9c0 63 6b 74 79 70 65 20 29 3b 0a 20 20 69 66 28 20  cktype );.  if( 
bd9d0 44 6f 73 53 65 74 46 69 6c 65 50 74 72 28 70 46  DosSetFilePtr(pF
bd9e0 69 6c 65 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20  ile->h, offset, 
bd9f0 46 49 4c 45 5f 42 45 47 49 4e 2c 20 26 66 69 6c  FILE_BEGIN, &fil
bda00 65 4c 6f 63 61 74 69 6f 6e 29 20 21 3d 20 4e 4f  eLocation) != NO
bda10 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 65  _ERROR ){.    re
bda20 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
bda30 52 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  R;.  }.  assert(
bda40 20 61 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69 6c   amt>0 );.  whil
bda50 65 28 20 61 6d 74 20 3e 20 30 20 26 26 0a 20 20  e( amt > 0 &&.  
bda60 20 20 20 20 20 20 20 28 20 72 63 20 3d 20 44 6f         ( rc = Do
bda70 73 57 72 69 74 65 28 20 70 46 69 6c 65 2d 3e 68  sWrite( pFile->h
bda80 2c 20 28 50 56 4f 49 44 29 70 42 75 66 2c 20 61  , (PVOID)pBuf, a
bda90 6d 74 2c 20 26 77 72 6f 74 65 20 29 20 29 20 3d  mt, &wrote ) ) =
bdaa0 3d 20 4e 4f 5f 45 52 52 4f 52 20 26 26 0a 20 20  = NO_ERROR &&.  
bdab0 20 20 20 20 20 20 20 77 72 6f 74 65 20 3e 20 30         wrote > 0
bdac0 0a 20 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d  .  ){.    amt -=
bdad0 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 42 75 66   wrote;.    pBuf
bdae0 20 3d 20 26 28 28 63 68 61 72 2a 29 70 42 75 66   = &((char*)pBuf
bdaf0 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 0a 20  )[wrote];.  }.. 
bdb00 20 72 65 74 75 72 6e 20 28 20 72 63 20 21 3d 20   return ( rc != 
bdb10 4e 4f 5f 45 52 52 4f 52 20 7c 7c 20 61 6d 74 20  NO_ERROR || amt 
bdb20 3e 20 28 69 6e 74 29 77 72 6f 74 65 20 29 20 3f  > (int)wrote ) ?
bdb30 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 3a 20 53   SQLITE_FULL : S
bdb40 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
bdb50 2a 2a 20 54 72 75 6e 63 61 74 65 20 61 6e 20 6f  ** Truncate an o
bdb60 70 65 6e 20 66 69 6c 65 20 74 6f 20 61 20 73 70  pen file to a sp
bdb70 65 63 69 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a  ecified size.*/.
bdb80 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 54 72  static int os2Tr
bdb90 75 6e 63 61 74 65 28 20 73 71 6c 69 74 65 33 5f  uncate( sqlite3_
bdba0 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 6e 42  file *id, i64 nB
bdbb0 79 74 65 20 29 7b 0a 20 20 41 50 49 52 45 54 20  yte ){.  APIRET 
bdbc0 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20  rc = NO_ERROR;. 
bdbd0 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20   os2File *pFile 
bdbe0 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a  = (os2File*)id;.
bdbf0 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 52 55    OSTRACE3( "TRU
bdc00 4e 43 41 54 45 20 25 64 20 25 6c 6c 64 5c 6e 22  NCATE %d %lld\n"
bdc10 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74  , pFile->h, nByt
bdc20 65 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49  e );.  SimulateI
bdc30 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53  OError( return S
bdc40 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e  QLITE_IOERR_TRUN
bdc50 43 41 54 45 20 29 3b 0a 20 20 72 63 20 3d 20 44  CATE );.  rc = D
bdc60 6f 73 53 65 74 46 69 6c 65 53 69 7a 65 28 20 70  osSetFileSize( p
bdc70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29  File->h, nByte )
bdc80 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20 3d 3d  ;.  return rc ==
bdc90 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49   NO_ERROR ? SQLI
bdca0 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49  TE_OK : SQLITE_I
bdcb0 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a 7d  OERR_TRUNCATE;.}
bdcc0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
bdcd0 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74  TEST./*.** Count
bdce0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66   the number of f
bdcf0 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72  ullsyncs and nor
bdd00 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73  mal syncs.  This
bdd10 20 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74   is used to test
bdd20 0a 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61  .** that syncs a
bdd30 6e 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65  nd fullsyncs are
bdd40 20 6f 63 63 75 72 69 6e 67 20 61 74 20 74 68 65   occuring at the
bdd50 20 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f   right times..*/
bdd60 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
bdd70 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75  sqlite3_sync_cou
bdd80 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41  nt = 0;.SQLITE_A
bdd90 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66  PI int sqlite3_f
bdda0 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20  ullsync_count = 
bddb0 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  0;.#endif../*.**
bddc0 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 77   Make sure all w
bddd0 72 69 74 65 73 20 74 6f 20 61 20 70 61 72 74 69  rites to a parti
bdde0 63 75 6c 61 72 20 66 69 6c 65 20 61 72 65 20 63  cular file are c
bddf0 6f 6d 6d 69 74 74 65 64 20 74 6f 20 64 69 73 6b  ommitted to disk
bde00 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
bde10 6f 73 32 53 79 6e 63 28 20 73 71 6c 69 74 65 33  os2Sync( sqlite3
bde20 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66  _file *id, int f
bde30 6c 61 67 73 20 29 7b 0a 20 20 6f 73 32 46 69 6c  lags ){.  os2Fil
bde40 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46  e *pFile = (os2F
bde50 69 6c 65 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41  ile*)id;.  OSTRA
bde60 43 45 33 28 20 22 53 59 4e 43 20 25 64 20 6c 6f  CE3( "SYNC %d lo
bde70 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  ck=%d\n", pFile-
bde80 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  >h, pFile->lockt
bde90 79 70 65 20 29 3b 0a 23 69 66 64 65 66 20 53 51  ype );.#ifdef SQ
bdea0 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20  LITE_TEST.  if( 
bdeb0 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53  flags & SQLITE_S
bdec0 59 4e 43 5f 46 55 4c 4c 29 7b 0a 20 20 20 20 73  YNC_FULL){.    s
bded0 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f  qlite3_fullsync_
bdee0 63 6f 75 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73  count++;.  }.  s
bdef0 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e  qlite3_sync_coun
bdf00 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65  t++;.#endif.  re
bdf10 74 75 72 6e 20 44 6f 73 52 65 73 65 74 42 75 66  turn DosResetBuf
bdf20 66 65 72 28 20 70 46 69 6c 65 2d 3e 68 20 29 20  fer( pFile->h ) 
bdf30 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51  == NO_ERROR ? SQ
bdf40 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45  LITE_OK : SQLITE
bdf50 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  _IOERR;.}../*.**
bdf60 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 63   Determine the c
bdf70 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 61  urrent size of a
bdf80 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a   file in bytes.*
bdf90 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32  /.static int os2
bdfa0 46 69 6c 65 53 69 7a 65 28 20 73 71 6c 69 74 65  FileSize( sqlite
bdfb0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69  3_file *id, sqli
bdfc0 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65  te3_int64 *pSize
bdfd0 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20   ){.  APIRET rc 
bdfe0 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 46 49  = NO_ERROR;.  FI
bdff0 4c 45 53 54 41 54 55 53 33 20 66 73 74 73 33 46  LESTATUS3 fsts3F
be000 69 6c 65 49 6e 66 6f 3b 0a 20 20 6d 65 6d 73 65  ileInfo;.  memse
be010 74 28 26 66 73 74 73 33 46 69 6c 65 49 6e 66 6f  t(&fsts3FileInfo
be020 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 74 73  , 0, sizeof(fsts
be030 33 46 69 6c 65 49 6e 66 6f 29 29 3b 0a 20 20 61  3FileInfo));.  a
be040 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a  ssert( id!=0 );.
be050 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
be060 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
be070 5f 49 4f 45 52 52 5f 46 53 54 41 54 20 29 3b 0a  _IOERR_FSTAT );.
be080 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 46    rc = DosQueryF
be090 69 6c 65 49 6e 66 6f 28 20 28 28 6f 73 32 46 69  ileInfo( ((os2Fi
be0a0 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 46 49 4c 5f  le*)id)->h, FIL_
be0b0 53 54 41 4e 44 41 52 44 2c 20 26 66 73 74 73 33  STANDARD, &fsts3
be0c0 46 69 6c 65 49 6e 66 6f 2c 20 73 69 7a 65 6f 66  FileInfo, sizeof
be0d0 28 46 49 4c 45 53 54 41 54 55 53 33 29 20 29 3b  (FILESTATUS3) );
be0e0 0a 20 20 69 66 28 20 72 63 20 3d 3d 20 4e 4f 5f  .  if( rc == NO_
be0f0 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 2a 70 53  ERROR ){.    *pS
be100 69 7a 65 20 3d 20 66 73 74 73 33 46 69 6c 65 49  ize = fsts3FileI
be110 6e 66 6f 2e 63 62 46 69 6c 65 3b 0a 20 20 20 20  nfo.cbFile;.    
be120 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
be130 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
be140 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
be150 52 52 5f 46 53 54 41 54 3b 0a 20 20 7d 0a 7d 0a  RR_FSTAT;.  }.}.
be160 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61  ./*.** Acquire a
be170 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e 0a 2a 2f   reader lock..*/
be180 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 52  .static int getR
be190 65 61 64 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65  eadLock( os2File
be1a0 20 2a 70 46 69 6c 65 20 29 7b 0a 20 20 46 49 4c   *pFile ){.  FIL
be1b0 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c  ELOCK  LockArea,
be1c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c  .            Unl
be1d0 6f 63 6b 41 72 65 61 3b 0a 20 20 41 50 49 52 45  ockArea;.  APIRE
be1e0 54 20 72 65 73 3b 0a 20 20 6d 65 6d 73 65 74 28  T res;.  memset(
be1f0 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69  &LockArea, 0, si
be200 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b  zeof(LockArea));
be210 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63  .  memset(&Unloc
be220 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66  kArea, 0, sizeof
be230 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20  (UnlockArea));. 
be240 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65   LockArea.lOffse
be250 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54  t = SHARED_FIRST
be260 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61  ;.  LockArea.lRa
be270 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a  nge = SHARED_SIZ
be280 45 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e  E;.  UnlockArea.
be290 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20  lOffset = 0L;.  
be2a0 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67  UnlockArea.lRang
be2b0 65 20 3d 20 30 4c 3b 0a 20 20 72 65 73 20 3d 20  e = 0L;.  res = 
be2c0 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28  DosSetFileLocks(
be2d0 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f   pFile->h, &Unlo
be2e0 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65  ckArea, &LockAre
be2f0 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c  a, LOCK_TIMEOUT,
be300 20 31 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43 45   1L );.  OSTRACE
be310 33 28 20 22 47 45 54 52 45 41 44 4c 4f 43 4b 20  3( "GETREADLOCK 
be320 25 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46  %d res=%d\n", pF
be330 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20  ile->h, res );. 
be340 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a   return res;.}..
be350 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61 20 72 65 61  /*.** Undo a rea
be360 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69 63 20  dlock.*/.static 
be370 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f  int unlockReadLo
be380 63 6b 28 20 6f 73 32 46 69 6c 65 20 2a 69 64 20  ck( os2File *id 
be390 29 7b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c  ){.  FILELOCK  L
be3a0 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20  ockArea,.       
be3b0 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b       UnlockArea;
be3c0 0a 20 20 41 50 49 52 45 54 20 72 65 73 3b 0a 20  .  APIRET res;. 
be3d0 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65   memset(&LockAre
be3e0 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63  a, 0, sizeof(Loc
be3f0 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65  kArea));.  memse
be400 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30  t(&UnlockArea, 0
be410 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41  , sizeof(UnlockA
be420 72 65 61 29 29 3b 0a 20 20 4c 6f 63 6b 41 72 65  rea));.  LockAre
be430 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a  a.lOffset = 0L;.
be440 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67    LockArea.lRang
be450 65 20 3d 20 30 4c 3b 0a 20 20 55 6e 6c 6f 63 6b  e = 0L;.  Unlock
be460 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53  Area.lOffset = S
be470 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 55  HARED_FIRST;.  U
be480 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
be490 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a   = SHARED_SIZE;.
be4a0 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69    res = DosSetFi
be4b0 6c 65 4c 6f 63 6b 73 28 20 69 64 2d 3e 68 2c 20  leLocks( id->h, 
be4c0 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f  &UnlockArea, &Lo
be4d0 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d  ckArea, LOCK_TIM
be4e0 45 4f 55 54 2c 20 31 4c 20 29 3b 0a 20 20 4f 53  EOUT, 1L );.  OS
be4f0 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 2d  TRACE3( "UNLOCK-
be500 52 45 41 44 4c 4f 43 4b 20 66 69 6c 65 20 68 61  READLOCK file ha
be510 6e 64 6c 65 3d 25 64 20 72 65 73 3d 25 64 3f 5c  ndle=%d res=%d?\
be520 6e 22 2c 20 69 64 2d 3e 68 2c 20 72 65 73 20 29  n", id->h, res )
be530 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a  ;.  return res;.
be540 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68  }../*.** Lock th
be550 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20  e file with the 
be560 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62  lock specified b
be570 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b  y parameter lock
be580 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66  type - one.** of
be590 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
be5a0 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48  **.**     (1) SH
be5b0 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  ARED_LOCK.**    
be5c0 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f   (2) RESERVED_LO
be5d0 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45  CK.**     (3) PE
be5e0 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  NDING_LOCK.**   
be5f0 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f    (4) EXCLUSIVE_
be600 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74  LOCK.**.** Somet
be610 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73  imes when reques
be620 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74  ting one lock st
be630 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20  ate, additional 
be640 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61  lock states.** a
be650 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62  re inserted in b
be660 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63  etween.  The loc
be670 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20  king might fail 
be680 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61  on one of the la
be690 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  ter.** transitio
be6a0 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c  ns leaving the l
be6b0 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72  ock state differ
be6c0 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74  ent from what it
be6d0 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20   started but.** 
be6e0 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69  still short of i
be6f0 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f  ts goal.  The fo
be700 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68  llowing chart sh
be710 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a  ows the allowed.
be720 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61  ** transitions a
be730 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20  nd the inserted 
be740 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61  intermediate sta
be750 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e  tes:.**.**    UN
be760 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44  LOCKED -> SHARED
be770 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e  .**    SHARED ->
be780 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20   RESERVED.**    
be790 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49  SHARED -> (PENDI
be7a0 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  NG) -> EXCLUSIVE
be7b0 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20  .**    RESERVED 
be7c0 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20  -> (PENDING) -> 
be7d0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20  EXCLUSIVE.**    
be7e0 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55  PENDING -> EXCLU
be7f0 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  SIVE.**.** This 
be800 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c  routine will onl
be810 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63  y increase a loc
be820 6b 2e 20 20 54 68 65 20 6f 73 32 55 6e 6c 6f 63  k.  The os2Unloc
be830 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65  k() routine.** e
be840 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63 6b 73 20  rases all locks 
be850 61 74 20 6f 6e 63 65 20 61 6e 64 20 72 65 74 75  at once and retu
be860 72 6e 73 20 75 73 20 69 6d 6d 65 64 69 61 74 65  rns us immediate
be870 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65  ly to locking le
be880 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20 69 73 20  vel 0..** It is 
be890 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20  not possible to 
be8a0 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e  lower the lockin
be8b0 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73 74 65 70  g level one step
be8c0 20 61 74 20 61 20 74 69 6d 65 2e 20 20 59 6f 75   at a time.  You
be8d0 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73 74 72 61  .** must go stra
be8e0 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69 6e 67 20  ight to locking 
be8f0 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61 74  level 0..*/.stat
be900 69 63 20 69 6e 74 20 6f 73 32 4c 6f 63 6b 28 20  ic int os2Lock( 
be910 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
be920 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 20 29  , int locktype )
be930 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
be940 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 2f 2a  ITE_OK;       /*
be950 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f   Return code fro
be960 6d 20 73 75 62 72 6f 75 74 69 6e 65 73 20 2a 2f  m subroutines */
be970 0a 20 20 41 50 49 52 45 54 20 72 65 73 20 3d 20  .  APIRET res = 
be980 4e 4f 5f 45 52 52 4f 52 3b 20 20 20 20 2f 2a 20  NO_ERROR;    /* 
be990 52 65 73 75 6c 74 20 6f 66 20 61 6e 20 4f 53 2f  Result of an OS/
be9a0 32 20 6c 6f 63 6b 20 63 61 6c 6c 20 2a 2f 0a 20  2 lock call */. 
be9b0 20 69 6e 74 20 6e 65 77 4c 6f 63 6b 74 79 70 65   int newLocktype
be9c0 3b 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 70  ;       /* Set p
be9d0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 74  File->locktype t
be9e0 6f 20 74 68 69 73 20 76 61 6c 75 65 20 62 65 66  o this value bef
be9f0 6f 72 65 20 65 78 69 74 69 6e 67 20 2a 2f 0a 20  ore exiting */. 
bea00 20 69 6e 74 20 67 6f 74 50 65 6e 64 69 6e 67 4c   int gotPendingL
bea10 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54 72 75 65 20  ock = 0;/* True 
bea20 69 66 20 77 65 20 61 63 71 75 69 72 65 64 20 61  if we acquired a
bea30 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68   PENDING lock th
bea40 69 73 20 74 69 6d 65 20 2a 2f 0a 20 20 46 49 4c  is time */.  FIL
bea50 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c  ELOCK  LockArea,
bea60 0a 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c  .            Unl
bea70 6f 63 6b 41 72 65 61 3b 0a 20 20 6f 73 32 46 69  ockArea;.  os2Fi
bea80 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32  le *pFile = (os2
bea90 46 69 6c 65 2a 29 69 64 3b 0a 20 20 6d 65 6d 73  File*)id;.  mems
beaa0 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c  et(&LockArea, 0,
beab0 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61   sizeof(LockArea
beac0 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e  ));.  memset(&Un
bead0 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a  lockArea, 0, siz
beae0 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29  eof(UnlockArea))
beaf0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  ;.  assert( pFil
beb00 65 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43  e!=0 );.  OSTRAC
beb10 45 34 28 20 22 4c 4f 43 4b 20 25 64 20 25 64 20  E4( "LOCK %d %d 
beb20 77 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65  was %d\n", pFile
beb30 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70  ->h, locktype, p
beb40 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29  File->locktype )
beb50 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  ;..  /* If there
beb60 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f   is already a lo
beb70 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 20  ck of this type 
beb80 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74  or more restrict
beb90 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20  ive on the.  ** 
beba0 6f 73 32 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68  os2File, do noth
bebb0 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 74  ing. Don't use t
bebc0 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69  he end_lock: exi
bebd0 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20  t path, as.  ** 
bebe0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
bebf0 74 65 72 28 29 20 68 61 73 6e 27 74 20 62 65 65  ter() hasn't bee
bec00 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20  n called yet..  
bec10 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  */.  if( pFile->
bec20 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79  locktype>=lockty
bec30 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43  pe ){.    OSTRAC
bec40 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 25 64 20  E3( "LOCK %d %d 
bec50 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64  ok (already held
bec60 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  )\n", pFile->h, 
bec70 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20  locktype );.    
bec80 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
bec90 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65  ;.  }..  /* Make
beca0 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e   sure the lockin
becb0 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f  g sequence is co
becc0 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73  rrect.  */.  ass
becd0 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ert( pFile->lock
bece0 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c  type!=NO_LOCK ||
becf0 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45   locktype==SHARE
bed00 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65  D_LOCK );.  asse
bed10 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45  rt( locktype!=PE
bed20 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20  NDING_LOCK );.  
bed30 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65  assert( locktype
bed40 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20  !=RESERVED_LOCK 
bed50 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  || pFile->lockty
bed60 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe==SHARED_LOCK 
bed70 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74 68  );..  /* Lock th
bed80 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62  e PENDING_LOCK b
bed90 79 74 65 20 69 66 20 77 65 20 6e 65 65 64 20 74  yte if we need t
beda0 6f 20 61 63 71 75 69 72 65 20 61 20 50 45 4e 44  o acquire a PEND
bedb0 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 20 2a 2a  ING lock or.  **
bedc0 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 20   a SHARED lock. 
bedd0 20 49 66 20 77 65 20 61 72 65 20 61 63 71 75 69   If we are acqui
bede0 72 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f  ring a SHARED lo
bedf0 63 6b 2c 20 74 68 65 20 61 63 71 75 69 73 69 74  ck, the acquisit
bee00 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20  ion of.  ** the 
bee10 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74  PENDING_LOCK byt
bee20 65 20 69 73 20 74 65 6d 70 6f 72 61 72 79 2e 0a  e is temporary..
bee30 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 6b 74 79    */.  newLockty
bee40 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  pe = pFile->lock
bee50 74 79 70 65 3b 0a 20 20 69 66 28 20 70 46 69 6c  type;.  if( pFil
bee60 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f  e->locktype==NO_
bee70 4c 4f 43 4b 0a 20 20 20 20 20 20 7c 7c 20 28 6c  LOCK.      || (l
bee80 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49  ocktype==EXCLUSI
bee90 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65  VE_LOCK && pFile
beea0 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45  ->locktype==RESE
beeb0 52 56 45 44 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a  RVED_LOCK).  ){.
beec0 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66      LockArea.lOf
beed0 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42  fset = PENDING_B
beee0 59 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65  YTE;.    LockAre
beef0 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20  a.lRange = 1L;. 
bef00 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f     UnlockArea.lO
bef10 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20  ffset = 0L;.    
bef20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67  UnlockArea.lRang
bef30 65 20 3d 20 30 4c 3b 0a 0a 20 20 20 20 2f 2a 20  e = 0L;..    /* 
bef40 77 61 69 74 20 6c 6f 6e 67 65 72 20 74 68 61 6e  wait longer than
bef50 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 20 68 65   LOCK_TIMEOUT he
bef60 72 65 20 6e 6f 74 20 74 6f 20 68 61 76 65 20 74  re not to have t
bef70 6f 20 74 72 79 20 6d 75 6c 74 69 70 6c 65 20 74  o try multiple t
bef80 69 6d 65 73 20 2a 2f 0a 20 20 20 20 72 65 73 20  imes */.    res 
bef90 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b  = DosSetFileLock
befa0 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e  s( pFile->h, &Un
befb0 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41  lockArea, &LockA
befc0 72 65 61 2c 20 31 30 30 4c 2c 20 30 4c 20 29 3b  rea, 100L, 0L );
befd0 0a 20 20 20 20 69 66 28 20 72 65 73 20 3d 3d 20  .    if( res == 
befe0 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
beff0 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b    gotPendingLock
bf000 20 3d 20 31 3b 0a 20 20 20 20 20 20 4f 53 54 52   = 1;.      OSTR
bf010 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 70  ACE3( "LOCK %d p
bf020 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 62 6f 6f 6c  ending lock bool
bf030 65 61 6e 20 73 65 74 2e 20 20 72 65 73 3d 25 64  ean set.  res=%d
bf040 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
bf050 65 73 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  es );.    }.  }.
bf060 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20  .  /* Acquire a 
bf070 73 68 61 72 65 64 20 6c 6f 63 6b 0a 20 20 2a 2f  shared lock.  */
bf080 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d  .  if( locktype=
bf090 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20  =SHARED_LOCK && 
bf0a0 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20  res == NO_ERROR 
bf0b0 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
bf0c0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d  File->locktype==
bf0d0 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72  NO_LOCK );.    r
bf0e0 65 73 20 3d 20 67 65 74 52 65 61 64 4c 6f 63 6b  es = getReadLock
bf0f0 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 69 66 28  (pFile);.    if(
bf100 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52   res == NO_ERROR
bf110 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63   ){.      newLoc
bf120 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c  ktype = SHARED_L
bf130 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f  OCK;.    }.    O
bf140 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25  STRACE3( "LOCK %
bf150 64 20 61 63 71 75 69 72 65 20 73 68 61 72 65 64  d acquire shared
bf160 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c 6e 22   lock. res=%d\n"
bf170 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20  , pFile->h, res 
bf180 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71  );.  }..  /* Acq
bf190 75 69 72 65 20 61 20 52 45 53 45 52 56 45 44 20  uire a RESERVED 
bf1a0 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20  lock.  */.  if( 
bf1b0 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56  locktype==RESERV
bf1c0 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d  ED_LOCK && res =
bf1d0 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
bf1e0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d    assert( pFile-
bf1f0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45  >locktype==SHARE
bf200 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 4c 6f  D_LOCK );.    Lo
bf210 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d  ckArea.lOffset =
bf220 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a   RESERVED_BYTE;.
bf230 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61      LockArea.lRa
bf240 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55 6e  nge = 1L;.    Un
bf250 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  lockArea.lOffset
bf260 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63   = 0L;.    Unloc
bf270 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
bf280 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73  L;.    res = Dos
bf290 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46  SetFileLocks( pF
bf2a0 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41  ile->h, &UnlockA
bf2b0 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20  rea, &LockArea, 
bf2c0 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c  LOCK_TIMEOUT, 0L
bf2d0 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20   );.    if( res 
bf2e0 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20  == NO_ERROR ){. 
bf2f0 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65       newLocktype
bf300 20 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b   = RESERVED_LOCK
bf310 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52  ;.    }.    OSTR
bf320 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 61  ACE3( "LOCK %d a
bf330 63 71 75 69 72 65 20 72 65 73 65 72 76 65 64 20  cquire reserved 
bf340 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c 6e 22 2c  lock. res=%d\n",
bf350 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29   pFile->h, res )
bf360 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75  ;.  }..  /* Acqu
bf370 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f  ire a PENDING lo
bf380 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f  ck.  */.  if( lo
bf390 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56  cktype==EXCLUSIV
bf3a0 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d  E_LOCK && res ==
bf3b0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
bf3c0 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 50   newLocktype = P
bf3d0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20  ENDING_LOCK;.   
bf3e0 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20   gotPendingLock 
bf3f0 3d 20 30 3b 0a 20 20 20 20 4f 53 54 52 41 43 45  = 0;.    OSTRACE
bf400 32 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75  2( "LOCK %d acqu
bf410 69 72 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b  ire pending lock
bf420 2e 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 62  . pending lock b
bf430 6f 6f 6c 65 61 6e 20 75 6e 73 65 74 2e 5c 6e 22  oolean unset.\n"
bf440 2c 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20  , pFile->h );.  
bf450 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20  }..  /* Acquire 
bf460 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  an EXCLUSIVE loc
bf470 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63  k.  */.  if( loc
bf480 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45  ktype==EXCLUSIVE
bf490 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d 20  _LOCK && res == 
bf4a0 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
bf4b0 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c  assert( pFile->l
bf4c0 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52 45 44 5f  ocktype>=SHARED_
bf4d0 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20  LOCK );.    res 
bf4e0 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b  = unlockReadLock
bf4f0 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54  (pFile);.    OST
bf500 52 41 43 45 32 28 20 22 75 6e 72 65 61 64 6c 6f  RACE2( "unreadlo
bf510 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20  ck = %d\n", res 
bf520 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e  );.    LockArea.
bf530 6c 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44  lOffset = SHARED
bf540 5f 46 49 52 53 54 3b 0a 20 20 20 20 4c 6f 63 6b  _FIRST;.    Lock
bf550 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48  Area.lRange = SH
bf560 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 55  ARED_SIZE;.    U
bf570 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65  nlockArea.lOffse
bf580 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f  t = 0L;.    Unlo
bf590 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20  ckArea.lRange = 
bf5a0 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f  0L;.    res = Do
bf5b0 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70  sSetFileLocks( p
bf5c0 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b  File->h, &Unlock
bf5d0 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c  Area, &LockArea,
bf5e0 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30   LOCK_TIMEOUT, 0
bf5f0 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73  L );.    if( res
bf600 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   == NO_ERROR ){.
bf610 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70        newLocktyp
bf620 65 20 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f  e = EXCLUSIVE_LO
bf630 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  CK;.    }else{. 
bf640 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 20 22       OSTRACE2( "
bf650 4f 53 2f 32 20 65 72 72 6f 72 2d 63 6f 64 65 20  OS/2 error-code 
bf660 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20 29 3b 0a  = %d\n", res );.
bf670 20 20 20 20 20 20 67 65 74 52 65 61 64 4c 6f 63        getReadLoc
bf680 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a  k(pFile);.    }.
bf690 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c      OSTRACE3( "L
bf6a0 4f 43 4b 20 25 64 20 61 63 71 75 69 72 65 20 65  OCK %d acquire e
bf6b0 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20  xclusive lock.  
bf6c0 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  res=%d\n", pFile
bf6d0 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a  ->h, res );.  }.
bf6e0 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20  .  /* If we are 
bf6f0 68 6f 6c 64 69 6e 67 20 61 20 50 45 4e 44 49 4e  holding a PENDIN
bf700 47 20 6c 6f 63 6b 20 74 68 61 74 20 6f 75 67 68  G lock that ough
bf710 74 20 74 6f 20 62 65 20 72 65 6c 65 61 73 65 64  t to be released
bf720 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65  , then.  ** rele
bf730 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f  ase it now..  */
bf740 0a 20 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e  .  if( gotPendin
bf750 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70  gLock && locktyp
bf760 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e==SHARED_LOCK )
bf770 7b 0a 20 20 20 20 69 6e 74 20 72 3b 0a 20 20 20  {.    int r;.   
bf780 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65   LockArea.lOffse
bf790 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b  t = 0L;.    Lock
bf7a0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c  Area.lRange = 0L
bf7b0 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61  ;.    UnlockArea
bf7c0 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49  .lOffset = PENDI
bf7d0 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c  NG_BYTE;.    Unl
bf7e0 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d  ockArea.lRange =
bf7f0 20 31 4c 3b 0a 20 20 20 20 72 20 3d 20 44 6f 73   1L;.    r = Dos
bf800 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46  SetFileLocks( pF
bf810 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41  ile->h, &UnlockA
bf820 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20  rea, &LockArea, 
bf830 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c  LOCK_TIMEOUT, 0L
bf840 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33   );.    OSTRACE3
bf850 28 20 22 4c 4f 43 4b 20 25 64 20 75 6e 6c 6f 63  ( "LOCK %d unloc
bf860 6b 69 6e 67 20 70 65 6e 64 69 6e 67 2f 69 73 20  king pending/is 
bf870 73 68 61 72 65 64 2e 20 72 3d 25 64 5c 6e 22 2c  shared. r=%d\n",
bf880 20 70 46 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a   pFile->h, r );.
bf890 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65    }..  /* Update
bf8a0 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68   the state of th
bf8b0 65 20 6c 6f 63 6b 20 68 61 73 20 68 65 6c 64 20  e lock has held 
bf8c0 69 6e 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  in the file desc
bf8d0 72 69 70 74 6f 72 20 74 68 65 6e 0a 20 20 2a 2a  riptor then.  **
bf8e0 20 72 65 74 75 72 6e 20 74 68 65 20 61 70 70 72   return the appr
bf8f0 6f 70 72 69 61 74 65 20 72 65 73 75 6c 74 20 63  opriate result c
bf900 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ode..  */.  if( 
bf910 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20  res == NO_ERROR 
bf920 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
bf930 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a  TE_OK;.  }else{.
bf940 20 20 20 20 4f 53 54 52 41 43 45 34 28 20 22 4c      OSTRACE4( "L
bf950 4f 43 4b 20 46 41 49 4c 45 44 20 25 64 20 74 72  OCK FAILED %d tr
bf960 79 69 6e 67 20 66 6f 72 20 25 64 20 62 75 74 20  ying for %d but 
bf970 67 6f 74 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65  got %d\n", pFile
bf980 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ->h,.           
bf990 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20 6e 65 77     locktype, new
bf9a0 4c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20  Locktype );.    
bf9b0 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
bf9c0 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c  ;.  }.  pFile->l
bf9d0 6f 63 6b 74 79 70 65 20 3d 20 6e 65 77 4c 6f 63  ocktype = newLoc
bf9e0 6b 74 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45  ktype;.  OSTRACE
bf9f0 33 28 20 22 4c 4f 43 4b 20 25 64 20 6e 6f 77 20  3( "LOCK %d now 
bfa00 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
bfa10 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
bfa20 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   );.  return rc;
bfa30 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
bfa40 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66  outine checks if
bfa50 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45   there is a RESE
bfa60 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f  RVED lock held o
bfa70 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a  n the specified.
bfa80 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20  ** file by this 
bfa90 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f  or any other pro
bfaa0 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20  cess. If such a 
bfab0 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65  lock is held, re
bfac0 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f  turn.** non-zero
bfad0 2c 20 6f 74 68 65 72 77 69 73 65 20 7a 65 72 6f  , otherwise zero
bfae0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
bfaf0 6f 73 32 43 68 65 63 6b 52 65 73 65 72 76 65 64  os2CheckReserved
bfb00 4c 6f 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 69  Lock( sqlite3_fi
bfb10 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 4f 75  le *id, int *pOu
bfb20 74 20 29 7b 0a 20 20 69 6e 74 20 72 20 3d 20 30  t ){.  int r = 0
bfb30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69  ;.  os2File *pFi
bfb40 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69  le = (os2File*)i
bfb50 64 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69  d;.  assert( pFi
bfb60 6c 65 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  le!=0 );.  if( p
bfb70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d  File->locktype>=
bfb80 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b  RESERVED_LOCK ){
bfb90 0a 20 20 20 20 72 20 3d 20 31 3b 0a 20 20 20 20  .    r = 1;.    
bfba0 4f 53 54 52 41 43 45 33 28 20 22 54 45 53 54 20  OSTRACE3( "TEST 
bfbb0 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 6c  WR-LOCK %d %d (l
bfbc0 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69 6c 65 2d  ocal)\n", pFile-
bfbd0 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 65 6c 73 65  >h, r );.  }else
bfbe0 7b 0a 20 20 20 20 46 49 4c 45 4c 4f 43 4b 20 20  {.    FILELOCK  
bfbf0 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20  LockArea,.      
bfc00 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72          UnlockAr
bfc10 65 61 3b 0a 20 20 20 20 41 50 49 52 45 54 20 72  ea;.    APIRET r
bfc20 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20  c = NO_ERROR;.  
bfc30 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72    memset(&LockAr
bfc40 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f  ea, 0, sizeof(Lo
bfc50 63 6b 41 72 65 61 29 29 3b 0a 20 20 20 20 6d 65  ckArea));.    me
bfc60 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61  mset(&UnlockArea
bfc70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f  , 0, sizeof(Unlo
bfc80 63 6b 41 72 65 61 29 29 3b 0a 20 20 20 20 4c 6f  ckArea));.    Lo
bfc90 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d  ckArea.lOffset =
bfca0 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a   RESERVED_BYTE;.
bfcb0 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61      LockArea.lRa
bfcc0 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55 6e  nge = 1L;.    Un
bfcd0 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  lockArea.lOffset
bfce0 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63   = 0L;.    Unloc
bfcf0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
bfd00 4c 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 53  L;.    rc = DosS
bfd10 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69  etFileLocks( pFi
bfd20 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72  le->h, &UnlockAr
bfd30 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c  ea, &LockArea, L
bfd40 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20  OCK_TIMEOUT, 0L 
bfd50 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28  );.    OSTRACE3(
bfd60 20 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25   "TEST WR-LOCK %
bfd70 64 20 6c 6f 63 6b 20 72 65 73 65 72 76 65 64 20  d lock reserved 
bfd80 62 79 74 65 20 72 63 3d 25 64 5c 6e 22 2c 20 70  byte rc=%d\n", p
bfd90 46 69 6c 65 2d 3e 68 2c 20 72 63 20 29 3b 0a 20  File->h, rc );. 
bfda0 20 20 20 69 66 28 20 72 63 20 3d 3d 20 4e 4f 5f     if( rc == NO_
bfdb0 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 41  ERROR ){.      A
bfdc0 50 49 52 45 54 20 72 63 75 20 3d 20 4e 4f 5f 45  PIRET rcu = NO_E
bfdd0 52 52 4f 52 3b 20 2f 2a 20 72 65 74 75 72 6e 20  RROR; /* return 
bfde0 63 6f 64 65 20 66 6f 72 20 75 6e 6c 6f 63 6b 69  code for unlocki
bfdf0 6e 67 20 2a 2f 0a 20 20 20 20 20 20 4c 6f 63 6b  ng */.      Lock
bfe00 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30  Area.lOffset = 0
bfe10 4c 3b 0a 20 20 20 20 20 20 4c 6f 63 6b 41 72 65  L;.      LockAre
bfe20 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20  a.lRange = 0L;. 
bfe30 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e       UnlockArea.
bfe40 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45 52 56  lOffset = RESERV
bfe50 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 20 20 55  ED_BYTE;.      U
bfe60 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
bfe70 20 3d 20 31 4c 3b 0a 20 20 20 20 20 20 72 63 75   = 1L;.      rcu
bfe80 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63   = DosSetFileLoc
bfe90 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55  ks( pFile->h, &U
bfea0 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b  nlockArea, &Lock
bfeb0 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f  Area, LOCK_TIMEO
bfec0 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 20 20  UT, 0L );.      
bfed0 4f 53 54 52 41 43 45 33 28 20 22 54 45 53 54 20  OSTRACE3( "TEST 
bfee0 57 52 2d 4c 4f 43 4b 20 25 64 20 75 6e 6c 6f 63  WR-LOCK %d unloc
bfef0 6b 20 72 65 73 65 72 76 65 64 20 62 79 74 65 20  k reserved byte 
bff00 72 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  r=%d\n", pFile->
bff10 68 2c 20 72 63 75 20 29 3b 0a 20 20 20 20 7d 0a  h, rcu );.    }.
bff20 20 20 20 20 72 20 3d 20 21 28 72 63 20 3d 3d 20      r = !(rc == 
bff30 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20 20 20 20 4f  NO_ERROR);.    O
bff40 53 54 52 41 43 45 33 28 20 22 54 45 53 54 20 57  STRACE3( "TEST W
bff50 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 72 65  R-LOCK %d %d (re
bff60 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 69 6c 65 2d  mote)\n", pFile-
bff70 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 0a 20 20 2a  >h, r );.  }.  *
bff80 70 4f 75 74 20 3d 20 72 3b 0a 20 20 72 65 74 75  pOut = r;.  retu
bff90 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
bffa0 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65  ./*.** Lower the
bffb0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f   locking level o
bffc0 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
bffd0 72 20 69 64 20 74 6f 20 6c 6f 63 6b 74 79 70 65  r id to locktype
bffe0 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d  .  locktype.** m
bfff0 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f  ust be either NO
c0000 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f  _LOCK or SHARED_
c0010 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  LOCK..**.** If t
c0020 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  he locking level
c0030 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73   of the file des
c0040 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61  criptor is alrea
c0050 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a  dy at or below.*
c0060 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  * the requested 
c0070 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74  locking level, t
c0080 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
c0090 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 74   no-op..**.** It
c00a0 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
c00b0 20 66 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e   for this routin
c00c0 65 20 74 6f 20 66 61 69 6c 20 69 66 20 74 68 65  e to fail if the
c00d0 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
c00e0 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b 2e 20  .** is NO_LOCK. 
c00f0 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61   If the second a
c0100 72 67 75 6d 65 6e 74 20 69 73 20 53 48 41 52 45  rgument is SHARE
c0110 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 68 69 73  D_LOCK then this
c0120 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69 67 68   routine.** migh
c0130 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  t return SQLITE_
c0140 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61 74 69 63  IOERR;.*/.static
c0150 20 69 6e 74 20 6f 73 32 55 6e 6c 6f 63 6b 28 20   int os2Unlock( 
c0160 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
c0170 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 20 29  , int locktype )
c0180 7b 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20  {.  int type;.  
c0190 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  os2File *pFile =
c01a0 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20   (os2File*)id;. 
c01b0 20 41 50 49 52 45 54 20 72 63 20 3d 20 53 51 4c   APIRET rc = SQL
c01c0 49 54 45 5f 4f 4b 3b 0a 20 20 41 50 49 52 45 54  ITE_OK;.  APIRET
c01d0 20 72 65 73 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b   res = NO_ERROR;
c01e0 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63  .  FILELOCK  Loc
c01f0 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20  kArea,.         
c0200 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20     UnlockArea;. 
c0210 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65   memset(&LockAre
c0220 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63  a, 0, sizeof(Loc
c0230 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65  kArea));.  memse
c0240 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30  t(&UnlockArea, 0
c0250 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41  , sizeof(UnlockA
c0260 72 65 61 29 29 3b 0a 20 20 61 73 73 65 72 74 28  rea));.  assert(
c0270 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 61   pFile!=0 );.  a
c0280 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c  ssert( locktype<
c0290 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
c02a0 20 20 4f 53 54 52 41 43 45 34 28 20 22 55 4e 4c    OSTRACE4( "UNL
c02b0 4f 43 4b 20 25 64 20 74 6f 20 25 64 20 77 61 73  OCK %d to %d was
c02c0 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68   %d\n", pFile->h
c02d0 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c  , locktype, pFil
c02e0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20  e->locktype );. 
c02f0 20 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c   type = pFile->l
c0300 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 74  ocktype;.  if( t
c0310 79 70 65 3e 3d 45 58 43 4c 55 53 49 56 45 5f 4c  ype>=EXCLUSIVE_L
c0320 4f 43 4b 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41  OCK ){.    LockA
c0330 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c  rea.lOffset = 0L
c0340 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c  ;.    LockArea.l
c0350 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20  Range = 0L;.    
c0360 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73  UnlockArea.lOffs
c0370 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53  et = SHARED_FIRS
c0380 54 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65  T;.    UnlockAre
c0390 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45  a.lRange = SHARE
c03a0 44 5f 53 49 5a 45 3b 0a 20 20 20 20 72 65 73 20  D_SIZE;.    res 
c03b0 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b  = DosSetFileLock
c03c0 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e  s( pFile->h, &Un
c03d0 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41  lockArea, &LockA
c03e0 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55  rea, LOCK_TIMEOU
c03f0 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54  T, 0L );.    OST
c0400 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25  RACE3( "UNLOCK %
c0410 64 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  d exclusive lock
c0420 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c   res=%d\n", pFil
c0430 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 20  e->h, res );.   
c0440 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53   if( locktype==S
c0450 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 67 65  HARED_LOCK && ge
c0460 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29  tReadLock(pFile)
c0470 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   != NO_ERROR ){.
c0480 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 73 68        /* This sh
c0490 6f 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65  ould never happe
c04a0 6e 2e 20 20 57 65 20 73 68 6f 75 6c 64 20 61 6c  n.  We should al
c04b0 77 61 79 73 20 62 65 20 61 62 6c 65 20 74 6f 0a  ways be able to.
c04c0 20 20 20 20 20 20 2a 2a 20 72 65 61 63 71 75 69        ** reacqui
c04d0 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b  re the read lock
c04e0 20 2a 2f 0a 20 20 20 20 20 20 4f 53 54 52 41 43   */.      OSTRAC
c04f0 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 74  E3( "UNLOCK %d t
c0500 6f 20 25 64 20 67 65 74 52 65 61 64 4c 6f 63 6b  o %d getReadLock
c0510 28 29 20 66 61 69 6c 65 64 5c 6e 22 2c 20 70 46  () failed\n", pF
c0520 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65  ile->h, locktype
c0530 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   );.      rc = S
c0540 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f  QLITE_IOERR_UNLO
c0550 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  CK;.    }.  }.  
c0560 69 66 28 20 74 79 70 65 3e 3d 52 45 53 45 52 56  if( type>=RESERV
c0570 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c  ED_LOCK ){.    L
c0580 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
c0590 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72  = 0L;.    LockAr
c05a0 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a  ea.lRange = 0L;.
c05b0 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c      UnlockArea.l
c05c0 4f 66 66 73 65 74 20 3d 20 52 45 53 45 52 56 45  Offset = RESERVE
c05d0 44 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c 6f  D_BYTE;.    Unlo
c05e0 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20  ckArea.lRange = 
c05f0 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f  1L;.    res = Do
c0600 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70  sSetFileLocks( p
c0610 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b  File->h, &Unlock
c0620 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c  Area, &LockArea,
c0630 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30   LOCK_TIMEOUT, 0
c0640 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45  L );.    OSTRACE
c0650 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 72 65  3( "UNLOCK %d re
c0660 73 65 72 76 65 64 20 72 65 73 3d 25 64 5c 6e 22  served res=%d\n"
c0670 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20  , pFile->h, res 
c0680 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63  );.  }.  if( loc
c0690 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26  ktype==NO_LOCK &
c06a0 26 20 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c  & type>=SHARED_L
c06b0 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 20 3d  OCK ){.    res =
c06c0 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28   unlockReadLock(
c06d0 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54 52  pFile);.    OSTR
c06e0 41 43 45 35 28 20 22 55 4e 4c 4f 43 4b 20 25 64  ACE5( "UNLOCK %d
c06f0 20 69 73 20 25 64 20 77 61 6e 74 20 25 64 20 72   is %d want %d r
c0700 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  es=%d\n", pFile-
c0710 3e 68 2c 20 74 79 70 65 2c 20 6c 6f 63 6b 74 79  >h, type, lockty
c0720 70 65 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a 20  pe, res );.  }. 
c0730 20 69 66 28 20 74 79 70 65 3e 3d 50 45 4e 44 49   if( type>=PENDI
c0740 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c  NG_LOCK ){.    L
c0750 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
c0760 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72  = 0L;.    LockAr
c0770 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a  ea.lRange = 0L;.
c0780 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c      UnlockArea.l
c0790 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47  Offset = PENDING
c07a0 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c 6f 63  _BYTE;.    Unloc
c07b0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31  kArea.lRange = 1
c07c0 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73  L;.    res = Dos
c07d0 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46  SetFileLocks( pF
c07e0 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41  ile->h, &UnlockA
c07f0 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20  rea, &LockArea, 
c0800 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c  LOCK_TIMEOUT, 0L
c0810 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33   );.    OSTRACE3
c0820 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 70 65 6e  ( "UNLOCK %d pen
c0830 64 69 6e 67 20 72 65 73 3d 25 64 5c 6e 22 2c 20  ding res=%d\n", 
c0840 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b  pFile->h, res );
c0850 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f  .  }.  pFile->lo
c0860 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70  cktype = locktyp
c0870 65 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22  e;.  OSTRACE3( "
c0880 55 4e 4c 4f 43 4b 20 25 64 20 6e 6f 77 20 25 64  UNLOCK %d now %d
c0890 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70  \n", pFile->h, p
c08a0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29  File->locktype )
c08b0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
c08c0 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f 6c 20  ../*.** Control 
c08d0 61 6e 64 20 71 75 65 72 79 20 6f 66 20 74 68 65  and query of the
c08e0 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c   open file handl
c08f0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
c0900 20 6f 73 32 46 69 6c 65 43 6f 6e 74 72 6f 6c 28   os2FileControl(
c0910 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
c0920 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a  , int op, void *
c0930 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63 68 28  pArg){.  switch(
c0940 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20   op ){.    case 
c0950 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43  SQLITE_FCNTL_LOC
c0960 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 20  KSTATE: {.      
c0970 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 28  *(int*)pArg = ((
c0980 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f  os2File*)id)->lo
c0990 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 4f 53  cktype;.      OS
c09a0 54 52 41 43 45 33 28 20 22 46 43 4e 54 4c 5f 4c  TRACE3( "FCNTL_L
c09b0 4f 43 4b 53 54 41 54 45 20 25 64 20 6c 6f 63 6b  OCKSTATE %d lock
c09c0 3d 25 64 5c 6e 22 2c 20 28 28 6f 73 32 46 69 6c  =%d\n", ((os2Fil
c09d0 65 2a 29 69 64 29 2d 3e 68 2c 20 28 28 6f 73 32  e*)id)->h, ((os2
c09e0 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74  File*)id)->lockt
c09f0 79 70 65 20 29 3b 0a 20 20 20 20 20 20 72 65 74  ype );.      ret
c0a00 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
c0a10 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
c0a20 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
c0a30 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
c0a40 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20  the sector size 
c0a50 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 20  in bytes of the 
c0a60 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b  underlying block
c0a70 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74   device for.** t
c0a80 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69 6c  he specified fil
c0a90 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f 73  e. This is almos
c0aa0 74 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74  t always 512 byt
c0ab0 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a 2a  es, but may be.*
c0ac0 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f 6d  * larger for som
c0ad0 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a  e devices..**.**
c0ae0 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 73   SQLite code ass
c0af0 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 69  umes this functi
c0b00 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20  on cannot fail. 
c0b10 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20  It also assumes 
c0b20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66  that.** if two f
c0b30 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 65 64  iles are created
c0b40 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c   in the same fil
c0b50 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74 6f  e-system directo
c0b60 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61  ry (i.e..** a da
c0b70 74 61 62 61 73 65 20 61 6e 64 20 69 74 73 20 6a  tabase and its j
c0b80 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 61  ournal file) tha
c0b90 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a  t the sector siz
c0ba0 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a  e will be the.**
c0bb0 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a   same for both..
c0bc0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  */.static int os
c0bd0 32 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69  2SectorSize(sqli
c0be0 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20  te3_file *id){. 
c0bf0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44   return SQLITE_D
c0c00 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49  EFAULT_SECTOR_SI
c0c10 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ZE;.}../*.** Ret
c0c20 75 72 6e 20 61 20 76 65 63 74 6f 72 20 6f 66 20  urn a vector of 
c0c30 64 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72  device character
c0c40 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 74 61 74 69  istics..*/.stati
c0c50 63 20 69 6e 74 20 6f 73 32 44 65 76 69 63 65 43  c int os2DeviceC
c0c60 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 73  haracteristics(s
c0c70 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
c0c80 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  {.  return 0;.}.
c0c90 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 72 61 63 74 65  ../*.** Characte
c0ca0 72 20 73 65 74 20 63 6f 6e 76 65 72 73 69 6f 6e  r set conversion
c0cb0 20 6f 62 6a 65 63 74 73 20 75 73 65 64 20 62 79   objects used by
c0cc0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 72 6f 75 74   conversion rout
c0cd0 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ines..*/.static 
c0ce0 55 63 6f 6e 76 4f 62 6a 65 63 74 20 75 63 55 74  UconvObject ucUt
c0cf0 66 38 20 3d 20 4e 55 4c 4c 3b 20 2f 2a 20 63 6f  f8 = NULL; /* co
c0d00 6e 76 65 72 74 20 62 65 74 77 65 65 6e 20 55 54  nvert between UT
c0d10 46 2d 38 20 61 6e 64 20 55 43 53 2d 32 20 2a 2f  F-8 and UCS-2 */
c0d20 0a 73 74 61 74 69 63 20 55 63 6f 6e 76 4f 62 6a  .static UconvObj
c0d30 65 63 74 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c  ect uclCp = NULL
c0d40 3b 20 20 2f 2a 20 63 6f 6e 76 65 72 74 20 62 65  ;  /* convert be
c0d50 74 77 65 65 6e 20 6c 6f 63 61 6c 20 63 6f 64 65  tween local code
c0d60 70 61 67 65 20 61 6e 64 20 55 43 53 2d 32 20 2a  page and UCS-2 *
c0d70 2f 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20  /../*.** Helper 
c0d80 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 69 74  function to init
c0d90 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e 76 65  ialize the conve
c0da0 72 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20 66 72  rsion objects fr
c0db0 6f 6d 20 61 6e 64 20 74 6f 20 55 54 46 2d 38 2e  om and to UTF-8.
c0dc0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
c0dd0 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73  initUconvObjects
c0de0 28 20 76 6f 69 64 20 29 7b 0a 20 20 69 66 28 20  ( void ){.  if( 
c0df0 55 6e 69 43 72 65 61 74 65 55 63 6f 6e 76 4f 62  UniCreateUconvOb
c0e00 6a 65 63 74 28 20 55 54 46 5f 38 2c 20 26 75 63  ject( UTF_8, &uc
c0e10 55 74 66 38 20 29 20 21 3d 20 55 4c 53 5f 53 55  Utf8 ) != ULS_SU
c0e20 43 43 45 53 53 20 29 0a 20 20 20 20 75 63 55 74  CCESS ).    ucUt
c0e30 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20 20 69 66 20  f8 = NULL;.  if 
c0e40 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f 6e 76  ( UniCreateUconv
c0e50 4f 62 6a 65 63 74 28 20 28 55 6e 69 43 68 61 72  Object( (UniChar
c0e60 20 2a 29 4c 22 40 70 61 74 68 3d 79 65 73 22 2c   *)L"@path=yes",
c0e70 20 26 75 63 6c 43 70 20 29 20 21 3d 20 55 4c 53   &uclCp ) != ULS
c0e80 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 75  _SUCCESS ).    u
c0e90 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b 0a 7d 0a 0a  clCp = NULL;.}..
c0ea0 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e  /*.** Helper fun
c0eb0 63 74 69 6f 6e 20 74 6f 20 66 72 65 65 20 74 68  ction to free th
c0ec0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 62 6a  e conversion obj
c0ed0 65 63 74 73 20 66 72 6f 6d 20 61 6e 64 20 74 6f  ects from and to
c0ee0 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69   UTF-8..*/.stati
c0ef0 63 20 76 6f 69 64 20 66 72 65 65 55 63 6f 6e 76  c void freeUconv
c0f00 4f 62 6a 65 63 74 73 28 20 76 6f 69 64 20 29 7b  Objects( void ){
c0f10 0a 20 20 69 66 20 28 20 75 63 55 74 66 38 20 29  .  if ( ucUtf8 )
c0f20 0a 20 20 20 20 55 6e 69 46 72 65 65 55 63 6f 6e  .    UniFreeUcon
c0f30 76 4f 62 6a 65 63 74 28 20 75 63 55 74 66 38 20  vObject( ucUtf8 
c0f40 29 3b 0a 20 20 69 66 20 28 20 75 63 6c 43 70 20  );.  if ( uclCp 
c0f50 29 0a 20 20 20 20 55 6e 69 46 72 65 65 55 63 6f  ).    UniFreeUco
c0f60 6e 76 4f 62 6a 65 63 74 28 20 75 63 6c 43 70 20  nvObject( uclCp 
c0f70 29 3b 0a 20 20 75 63 55 74 66 38 20 3d 20 4e 55  );.  ucUtf8 = NU
c0f80 4c 4c 3b 0a 20 20 75 63 6c 43 70 20 3d 20 4e 55  LL;.  uclCp = NU
c0f90 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c  LL;.}../*.** Hel
c0fa0 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  per function to 
c0fb0 63 6f 6e 76 65 72 74 20 55 54 46 2d 38 20 66 69  convert UTF-8 fi
c0fc0 6c 65 6e 61 6d 65 73 20 74 6f 20 6c 6f 63 61 6c  lenames to local
c0fd0 20 4f 53 2f 32 20 63 6f 64 65 70 61 67 65 2e 0a   OS/2 codepage..
c0fe0 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 65 70 20  ** The two-step 
c0ff0 70 72 6f 63 65 73 73 3a 20 66 69 72 73 74 20 63  process: first c
c1000 6f 6e 76 65 72 74 20 74 68 65 20 69 6e 63 6f 6d  onvert the incom
c1010 69 6e 67 20 55 54 46 2d 38 20 73 74 72 69 6e 67  ing UTF-8 string
c1020 0a 2a 2a 20 69 6e 74 6f 20 55 43 53 2d 32 20 61  .** into UCS-2 a
c1030 6e 64 20 74 68 65 6e 20 66 72 6f 6d 20 55 43 53  nd then from UCS
c1040 2d 32 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e  -2 to the curren
c1050 74 20 63 6f 64 65 70 61 67 65 2e 0a 2a 2a 20 54  t codepage..** T
c1060 68 65 20 72 65 74 75 72 6e 65 64 20 63 68 61 72  he returned char
c1070 20 70 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20   pointer has to 
c1080 62 65 20 66 72 65 65 64 2e 0a 2a 2f 0a 73 74 61  be freed..*/.sta
c1090 74 69 63 20 63 68 61 72 20 2a 63 6f 6e 76 65 72  tic char *conver
c10a0 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 63  tUtf8PathToCp( c
c10b0 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e 20 29 7b  onst char *in ){
c10c0 0a 20 20 55 6e 69 43 68 61 72 20 74 65 6d 70 50  .  UniChar tempP
c10d0 61 74 68 5b 43 43 48 4d 41 58 50 41 54 48 5d 3b  ath[CCHMAXPATH];
c10e0 0a 20 20 63 68 61 72 20 2a 6f 75 74 20 3d 20 28  .  char *out = (
c10f0 63 68 61 72 20 2a 29 63 61 6c 6c 6f 63 28 20 43  char *)calloc( C
c1100 43 48 4d 41 58 50 41 54 48 2c 20 31 20 29 3b 0a  CHMAXPATH, 1 );.
c1110 0a 20 20 69 66 28 20 21 6f 75 74 20 29 0a 20 20  .  if( !out ).  
c1120 20 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3b 0a 0a    return NULL;..
c1130 20 20 69 66 28 20 21 75 63 55 74 66 38 20 7c 7c    if( !ucUtf8 ||
c1140 20 21 75 63 6c 43 70 20 29 0a 20 20 20 20 69 6e   !uclCp ).    in
c1150 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29  itUconvObjects()
c1160 3b 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69 6e  ;..  /* determin
c1170 65 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65  e string for the
c1180 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20 55   conversion of U
c1190 54 46 2d 38 20 77 68 69 63 68 20 69 73 20 43 50  TF-8 which is CP
c11a0 31 32 30 38 20 2a 2f 0a 20 20 69 66 28 20 55 6e  1208 */.  if( Un
c11b0 69 53 74 72 54 6f 55 63 73 28 20 75 63 55 74 66  iStrToUcs( ucUtf
c11c0 38 2c 20 74 65 6d 70 50 61 74 68 2c 20 28 63 68  8, tempPath, (ch
c11d0 61 72 20 2a 29 69 6e 2c 20 43 43 48 4d 41 58 50  ar *)in, CCHMAXP
c11e0 41 54 48 20 29 20 21 3d 20 55 4c 53 5f 53 55 43  ATH ) != ULS_SUC
c11f0 43 45 53 53 20 29 0a 20 20 20 20 72 65 74 75 72  CESS ).    retur
c1200 6e 20 6f 75 74 3b 20 2f 2a 20 69 66 20 63 6f 6e  n out; /* if con
c1210 76 65 72 73 69 6f 6e 20 66 61 69 6c 73 2c 20 72  version fails, r
c1220 65 74 75 72 6e 20 74 68 65 20 65 6d 70 74 79 20  eturn the empty 
c1230 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 2f 2a 20  string */..  /* 
c1240 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72 20 63  conversion for c
c1250 75 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20  urrent codepage 
c1260 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 65  which can be use
c1270 64 20 66 6f 72 20 70 61 74 68 73 20 2a 2f 0a 20  d for paths */. 
c1280 20 55 6e 69 53 74 72 46 72 6f 6d 55 63 73 28 20   UniStrFromUcs( 
c1290 75 63 6c 43 70 2c 20 6f 75 74 2c 20 74 65 6d 70  uclCp, out, temp
c12a0 50 61 74 68 2c 20 43 43 48 4d 41 58 50 41 54 48  Path, CCHMAXPATH
c12b0 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6f 75   );..  return ou
c12c0 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70  t;.}../*.** Help
c12d0 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63  er function to c
c12e0 6f 6e 76 65 72 74 20 66 69 6c 65 6e 61 6d 65 73  onvert filenames
c12f0 20 66 72 6f 6d 20 6c 6f 63 61 6c 20 63 6f 64 65   from local code
c1300 70 61 67 65 20 74 6f 20 55 54 46 2d 38 2e 0a 2a  page to UTF-8..*
c1310 2a 20 54 68 65 20 74 77 6f 2d 73 74 65 70 20 70  * The two-step p
c1320 72 6f 63 65 73 73 3a 20 66 69 72 73 74 20 63 6f  rocess: first co
c1330 6e 76 65 72 74 20 74 68 65 20 69 6e 63 6f 6d 69  nvert the incomi
c1340 6e 67 20 63 6f 64 65 70 61 67 65 2d 73 70 65 63  ng codepage-spec
c1350 69 66 69 63 0a 2a 2a 20 73 74 72 69 6e 67 20 69  ific.** string i
c1360 6e 74 6f 20 55 43 53 2d 32 20 61 6e 64 20 74 68  nto UCS-2 and th
c1370 65 6e 20 66 72 6f 6d 20 55 43 53 2d 32 20 74 6f  en from UCS-2 to
c1380 20 74 68 65 20 63 6f 64 65 70 61 67 65 20 6f 66   the codepage of
c1390 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 72   UTF-8..** The r
c13a0 65 74 75 72 6e 65 64 20 63 68 61 72 20 70 6f 69  eturned char poi
c13b0 6e 74 65 72 20 68 61 73 20 74 6f 20 62 65 20 66  nter has to be f
c13c0 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  reed..**.** This
c13d0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 6e   function is non
c13e0 2d 73 74 61 74 69 63 20 74 6f 20 62 65 20 61 62  -static to be ab
c13f0 6c 65 20 74 6f 20 75 73 65 20 74 68 69 73 20 69  le to use this i
c1400 6e 20 73 68 65 6c 6c 2e 63 20 61 6e 64 0a 2a 2a  n shell.c and.**
c1410 20 73 69 6d 69 6c 61 72 20 61 70 70 6c 69 63 61   similar applica
c1420 74 69 6f 6e 73 20 74 68 61 74 20 74 61 6b 65 20  tions that take 
c1430 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 20 61 72 67  command line arg
c1440 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 63 68 61 72 20  uments..*/.char 
c1450 2a 63 6f 6e 76 65 72 74 43 70 50 61 74 68 54 6f  *convertCpPathTo
c1460 55 74 66 38 28 20 63 6f 6e 73 74 20 63 68 61 72  Utf8( const char
c1470 20 2a 69 6e 20 29 7b 0a 20 20 55 6e 69 43 68 61   *in ){.  UniCha
c1480 72 20 74 65 6d 70 50 61 74 68 5b 43 43 48 4d 41  r tempPath[CCHMA
c1490 58 50 41 54 48 5d 3b 0a 20 20 63 68 61 72 20 2a  XPATH];.  char *
c14a0 6f 75 74 20 3d 20 28 63 68 61 72 20 2a 29 63 61  out = (char *)ca
c14b0 6c 6c 6f 63 28 20 43 43 48 4d 41 58 50 41 54 48  lloc( CCHMAXPATH
c14c0 2c 20 31 20 29 3b 0a 0a 20 20 69 66 28 20 21 6f  , 1 );..  if( !o
c14d0 75 74 20 29 0a 20 20 20 20 72 65 74 75 72 6e 20  ut ).    return 
c14e0 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28 20 21 75 63  NULL;..  if( !uc
c14f0 55 74 66 38 20 7c 7c 20 21 75 63 6c 43 70 20 29  Utf8 || !uclCp )
c1500 0a 20 20 20 20 69 6e 69 74 55 63 6f 6e 76 4f 62  .    initUconvOb
c1510 6a 65 63 74 73 28 29 3b 0a 0a 20 20 2f 2a 20 63  jects();..  /* c
c1520 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72 20 63 75  onversion for cu
c1530 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20 77  rrent codepage w
c1540 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 64  hich can be used
c1550 20 66 6f 72 20 70 61 74 68 73 20 2a 2f 0a 20 20   for paths */.  
c1560 69 66 28 20 55 6e 69 53 74 72 54 6f 55 63 73 28  if( UniStrToUcs(
c1570 20 75 63 6c 43 70 2c 20 74 65 6d 70 50 61 74 68   uclCp, tempPath
c1580 2c 20 28 63 68 61 72 20 2a 29 69 6e 2c 20 43 43  , (char *)in, CC
c1590 48 4d 41 58 50 41 54 48 20 29 20 21 3d 20 55 4c  HMAXPATH ) != UL
c15a0 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20  S_SUCCESS ).    
c15b0 72 65 74 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69  return out; /* i
c15c0 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69  f conversion fai
c15d0 6c 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65  ls, return the e
c15e0 6d 70 74 79 20 73 74 72 69 6e 67 20 2a 2f 0a 0a  mpty string */..
c15f0 20 20 2f 2a 20 64 65 74 65 72 6d 69 6e 65 20 73    /* determine s
c1600 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 63 6f  tring for the co
c1610 6e 76 65 72 73 69 6f 6e 20 6f 66 20 55 54 46 2d  nversion of UTF-
c1620 38 20 77 68 69 63 68 20 69 73 20 43 50 31 32 30  8 which is CP120
c1630 38 20 2a 2f 0a 20 20 55 6e 69 53 74 72 46 72 6f  8 */.  UniStrFro
c1640 6d 55 63 73 28 20 75 63 55 74 66 38 2c 20 6f 75  mUcs( ucUtf8, ou
c1650 74 2c 20 74 65 6d 70 50 61 74 68 2c 20 43 43 48  t, tempPath, CCH
c1660 4d 41 58 50 41 54 48 20 29 3b 0a 0a 20 20 72 65  MAXPATH );..  re
c1670 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a  turn out;.}../*.
c1680 2a 2a 20 54 68 69 73 20 76 65 63 74 6f 72 20 64  ** This vector d
c1690 65 66 69 6e 65 73 20 61 6c 6c 20 74 68 65 20 6d  efines all the m
c16a0 65 74 68 6f 64 73 20 74 68 61 74 20 63 61 6e 20  ethods that can 
c16b0 6f 70 65 72 61 74 65 20 6f 6e 20 61 6e 0a 2a 2a  operate on an.**
c16c0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 66 6f   sqlite3_file fo
c16d0 72 20 6f 73 32 2e 0a 2a 2f 0a 73 74 61 74 69 63  r os2..*/.static
c16e0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69   const sqlite3_i
c16f0 6f 5f 6d 65 74 68 6f 64 73 20 6f 73 32 49 6f 4d  o_methods os2IoM
c1700 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20  ethod = {.  1,  
c1710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c1720 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f        /* iVersio
c1730 6e 20 2a 2f 0a 20 20 6f 73 32 43 6c 6f 73 65 2c  n */.  os2Close,
c1740 0a 20 20 6f 73 32 52 65 61 64 2c 0a 20 20 6f 73  .  os2Read,.  os
c1750 32 57 72 69 74 65 2c 0a 20 20 6f 73 32 54 72 75  2Write,.  os2Tru
c1760 6e 63 61 74 65 2c 0a 20 20 6f 73 32 53 79 6e 63  ncate,.  os2Sync
c1770 2c 0a 20 20 6f 73 32 46 69 6c 65 53 69 7a 65 2c  ,.  os2FileSize,
c1780 0a 20 20 6f 73 32 4c 6f 63 6b 2c 0a 20 20 6f 73  .  os2Lock,.  os
c1790 32 55 6e 6c 6f 63 6b 2c 0a 20 20 6f 73 32 43 68  2Unlock,.  os2Ch
c17a0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c  eckReservedLock,
c17b0 0a 20 20 6f 73 32 46 69 6c 65 43 6f 6e 74 72 6f  .  os2FileContro
c17c0 6c 2c 0a 20 20 6f 73 32 53 65 63 74 6f 72 53 69  l,.  os2SectorSi
c17d0 7a 65 2c 0a 20 20 6f 73 32 44 65 76 69 63 65 43  ze,.  os2DeviceC
c17e0 68 61 72 61 63 74 65 72 69 73 74 69 63 73 0a 7d  haracteristics.}
c17f0 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;../************
c1800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
c1840 2a 2a 20 48 65 72 65 20 65 6e 64 73 20 74 68 65  ** Here ends the
c1850 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 74 68 61   I/O methods tha
c1860 74 20 66 6f 72 6d 20 74 68 65 20 73 71 6c 69 74  t form the sqlit
c1870 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62  e3_io_methods ob
c1880 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ject..**.** The 
c1890 6e 65 78 74 20 62 6c 6f 63 6b 20 6f 66 20 63 6f  next block of co
c18a0 64 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  de implements th
c18b0 65 20 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a  e VFS methods..*
c18c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c18d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c18e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c18f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
c1910 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 74 65 6d  .** Create a tem
c1920 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65  porary file name
c1930 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75 66 20   in zBuf.  zBuf 
c1940 6d 75 73 74 20 62 65 20 62 69 67 20 65 6e 6f 75  must be big enou
c1950 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64 20 61 74  gh to.** hold at
c1960 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d   pVfs->mxPathnam
c1970 65 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2f  e characters..*/
c1980 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 54  .static int getT
c1990 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42 75 66  empname(int nBuf
c19a0 2c 20 63 68 61 72 20 2a 7a 42 75 66 20 29 7b 0a  , char *zBuf ){.
c19b0 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
c19c0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a 43 68  nsigned char zCh
c19d0 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63  ars[] =.    "abc
c19e0 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73  defghijklmnopqrs
c19f0 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22 41 42  tuvwxyz".    "AB
c1a00 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52  CDEFGHIJKLMNOPQR
c1a10 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20 22 30  STUVWXYZ".    "0
c1a20 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20 69 6e  123456789";.  in
c1a30 74 20 69 2c 20 6a 3b 0a 20 20 63 68 61 72 20 7a  t i, j;.  char z
c1a40 54 65 6d 70 50 61 74 68 42 75 66 5b 33 5d 3b 0a  TempPathBuf[3];.
c1a50 20 20 50 53 5a 20 7a 54 65 6d 70 50 61 74 68 20    PSZ zTempPath 
c1a60 3d 20 28 50 53 5a 29 26 7a 54 65 6d 70 50 61 74  = (PSZ)&zTempPat
c1a70 68 42 75 66 3b 0a 20 20 69 66 28 20 73 71 6c 69  hBuf;.  if( sqli
c1a80 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f  te3_temp_directo
c1a90 72 79 20 29 7b 0a 20 20 20 20 7a 54 65 6d 70 50  ry ){.    zTempP
c1aa0 61 74 68 20 3d 20 73 71 6c 69 74 65 33 5f 74 65  ath = sqlite3_te
c1ab0 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a 20 20  mp_directory;.  
c1ac0 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 44  }else{.    if( D
c1ad0 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 53 5a 29  osScanEnv( (PSZ)
c1ae0 22 54 45 4d 50 22 2c 20 26 7a 54 65 6d 70 50 61  "TEMP", &zTempPa
c1af0 74 68 20 29 20 29 7b 0a 20 20 20 20 20 20 69 66  th ) ){.      if
c1b00 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50  ( DosScanEnv( (P
c1b10 53 5a 29 22 54 4d 50 22 2c 20 26 7a 54 65 6d 70  SZ)"TMP", &zTemp
c1b20 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20 20 20  Path ) ){.      
c1b30 20 20 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76    if( DosScanEnv
c1b40 28 20 28 50 53 5a 29 22 54 4d 50 44 49 52 22 2c  ( (PSZ)"TMPDIR",
c1b50 20 26 7a 54 65 6d 70 50 61 74 68 20 29 20 29 7b   &zTempPath ) ){
c1b60 0a 20 20 20 20 20 20 20 20 20 20 20 55 4c 4f 4e  .           ULON
c1b70 47 20 75 6c 44 72 69 76 65 4e 75 6d 20 3d 20 30  G ulDriveNum = 0
c1b80 2c 20 75 6c 44 72 69 76 65 4d 61 70 20 3d 20 30  , ulDriveMap = 0
c1b90 3b 0a 20 20 20 20 20 20 20 20 20 20 20 44 6f 73  ;.           Dos
c1ba0 51 75 65 72 79 43 75 72 72 65 6e 74 44 69 73 6b  QueryCurrentDisk
c1bb0 28 20 26 75 6c 44 72 69 76 65 4e 75 6d 2c 20 26  ( &ulDriveNum, &
c1bc0 75 6c 44 72 69 76 65 4d 61 70 20 29 3b 0a 20 20  ulDriveMap );.  
c1bd0 20 20 20 20 20 20 20 20 20 73 70 72 69 6e 74 66           sprintf
c1be0 28 20 28 63 68 61 72 2a 29 7a 54 65 6d 70 50 61  ( (char*)zTempPa
c1bf0 74 68 2c 20 22 25 63 3a 22 2c 20 28 63 68 61 72  th, "%c:", (char
c1c00 29 28 20 27 41 27 20 2b 20 75 6c 44 72 69 76 65  )( 'A' + ulDrive
c1c10 4e 75 6d 20 2d 20 31 20 29 20 29 3b 0a 20 20 20  Num - 1 ) );.   
c1c20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
c1c30 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 53 74     }.  }.  /* St
c1c40 72 69 70 20 6f 66 66 20 61 20 74 72 61 69 6c 69  rip off a traili
c1c50 6e 67 20 73 6c 61 73 68 65 73 20 6f 72 20 62 61  ng slashes or ba
c1c60 63 6b 73 6c 61 73 68 65 73 2c 20 6f 74 68 65 72  ckslashes, other
c1c70 77 69 73 65 20 77 65 20 77 6f 75 6c 64 20 67 65  wise we would ge
c1c80 74 20 2a 0a 20 20 20 2a 20 6d 75 6c 74 69 70 6c  t *.   * multipl
c1c90 65 20 28 62 61 63 6b 29 73 6c 61 73 68 65 73 20  e (back)slashes 
c1ca0 77 68 69 63 68 20 63 61 75 73 65 73 20 44 6f 73  which causes Dos
c1cb0 4f 70 65 6e 28 29 20 74 6f 20 66 61 69 6c 2e 20  Open() to fail. 
c1cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 0a 20               *. 
c1cd0 20 20 2a 20 54 72 61 69 6c 69 6e 67 20 73 70 61    * Trailing spa
c1ce0 63 65 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f  ces are not allo
c1cf0 77 65 64 2c 20 65 69 74 68 65 72 2e 20 20 20 20  wed, either.    
c1d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c1d10 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 6a 20 3d          */.  j =
c1d20 20 73 74 72 6c 65 6e 28 7a 54 65 6d 70 50 61 74   strlen(zTempPat
c1d30 68 29 3b 0a 20 20 77 68 69 6c 65 28 20 6a 20 3e  h);.  while( j >
c1d40 20 30 20 26 26 20 28 20 7a 54 65 6d 70 50 61 74   0 && ( zTempPat
c1d50 68 5b 6a 2d 31 5d 20 3d 3d 20 27 5c 5c 27 20 7c  h[j-1] == '\\' |
c1d60 7c 20 7a 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d  | zTempPath[j-1]
c1d70 20 3d 3d 20 27 2f 27 0a 20 20 20 20 20 20 20 20   == '/'.        
c1d80 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 7a              || z
c1d90 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d  TempPath[j-1] ==
c1da0 20 27 20 27 20 29 20 29 7b 0a 20 20 20 20 6a 2d   ' ' ) ){.    j-
c1db0 2d 3b 0a 20 20 7d 0a 20 20 7a 54 65 6d 70 50 61  -;.  }.  zTempPa
c1dc0 74 68 5b 6a 5d 20 3d 20 27 5c 30 27 3b 0a 20 20  th[j] = '\0';.  
c1dd0 69 66 28 20 21 73 71 6c 69 74 65 33 5f 74 65 6d  if( !sqlite3_tem
c1de0 70 5f 64 69 72 65 63 74 6f 72 79 20 29 7b 0a 20  p_directory ){. 
c1df0 20 20 20 63 68 61 72 20 2a 7a 54 65 6d 70 50 61     char *zTempPa
c1e00 74 68 55 54 46 20 3d 20 63 6f 6e 76 65 72 74 43  thUTF = convertC
c1e10 70 50 61 74 68 54 6f 55 74 66 38 28 20 7a 54 65  pPathToUtf8( zTe
c1e20 6d 70 50 61 74 68 20 29 3b 0a 20 20 20 20 73 71  mpPath );.    sq
c1e30 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20  lite3_snprintf( 
c1e40 6e 42 75 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20  nBuf-30, zBuf,. 
c1e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c1e60 20 20 20 20 20 22 25 73 5c 5c 22 53 51 4c 49 54       "%s\\"SQLIT
c1e70 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46  E_TEMP_FILE_PREF
c1e80 49 58 2c 20 7a 54 65 6d 70 50 61 74 68 55 54 46  IX, zTempPathUTF
c1e90 20 29 3b 0a 20 20 20 20 66 72 65 65 28 20 7a 54   );.    free( zT
c1ea0 65 6d 70 50 61 74 68 55 54 46 20 29 3b 0a 20 20  empPathUTF );.  
c1eb0 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
c1ec0 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e 42 75  e3_snprintf( nBu
c1ed0 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20  f-30, zBuf,.    
c1ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c1ef0 20 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54    "%s\\"SQLITE_T
c1f00 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c  EMP_FILE_PREFIX,
c1f10 20 7a 54 65 6d 70 50 61 74 68 20 29 3b 0a 20 20   zTempPath );.  
c1f20 7d 0a 20 20 6a 20 3d 20 73 74 72 6c 65 6e 28 20  }.  j = strlen( 
c1f30 7a 42 75 66 20 29 3b 0a 20 20 73 71 6c 69 74 65  zBuf );.  sqlite
c1f40 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 20 32 30  3_randomness( 20
c1f50 2c 20 26 7a 42 75 66 5b 6a 5d 20 29 3b 0a 20 20  , &zBuf[j] );.  
c1f60 66 6f 72 28 20 69 20 3d 20 30 3b 20 69 20 3c 20  for( i = 0; i < 
c1f70 32 30 3b 20 69 2b 2b 2c 20 6a 2b 2b 20 29 7b 0a  20; i++, j++ ){.
c1f80 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63      zBuf[j] = (c
c1f90 68 61 72 29 7a 43 68 61 72 73 5b 20 28 28 75 6e  har)zChars[ ((un
c1fa0 73 69 67 6e 65 64 20 63 68 61 72 29 7a 42 75 66  signed char)zBuf
c1fb0 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43 68  [j])%(sizeof(zCh
c1fc0 61 72 73 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a 20  ars)-1) ];.  }. 
c1fd0 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20   zBuf[j] = 0;.  
c1fe0 4f 53 54 52 41 43 45 32 28 20 22 54 45 4d 50 20  OSTRACE2( "TEMP 
c1ff0 46 49 4c 45 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c  FILENAME: %s\n",
c2000 20 7a 42 75 66 20 29 3b 0a 20 20 72 65 74 75 72   zBuf );.  retur
c2010 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
c2020 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 72 65  ./*.** Turn a re
c2030 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65 20  lative pathname 
c2040 69 6e 74 6f 20 61 20 66 75 6c 6c 20 70 61 74 68  into a full path
c2050 6e 61 6d 65 2e 20 20 57 72 69 74 65 20 74 68 65  name.  Write the
c2060 20 66 75 6c 6c 0a 2a 2a 20 70 61 74 68 6e 61 6d   full.** pathnam
c2070 65 20 69 6e 74 6f 20 7a 46 75 6c 6c 5b 5d 2e 20  e into zFull[]. 
c2080 20 7a 46 75 6c 6c 5b 5d 20 77 69 6c 6c 20 62 65   zFull[] will be
c2090 20 61 74 20 6c 65 61 73 74 20 70 56 66 73 2d 3e   at least pVfs->
c20a0 6d 78 50 61 74 68 6e 61 6d 65 0a 2a 2a 20 62 79  mxPathname.** by
c20b0 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a  tes in size..*/.
c20c0 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 46 75  static int os2Fu
c20d0 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71  llPathname(.  sq
c20e0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
c20f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
c2100 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 6a 65  nter to vfs obje
c2110 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ct */.  const ch
c2120 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 2c 20 20  ar *zRelative,  
c2130 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20      /* Possibly 
c2140 72 65 6c 61 74 69 76 65 20 69 6e 70 75 74 20 70  relative input p
c2150 61 74 68 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75  ath */.  int nFu
c2160 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ll,             
c2170 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
c2180 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 69 6e  output buffer in
c2190 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 72   bytes */.  char
c21a0 20 2a 7a 46 75 6c 6c 20 20 20 20 20 20 20 20 20   *zFull         
c21b0 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75          /* Outpu
c21c0 74 20 62 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 20  t buffer */.){. 
c21d0 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 65   char *zRelative
c21e0 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38  Cp = convertUtf8
c21f0 50 61 74 68 54 6f 43 70 28 20 7a 52 65 6c 61 74  PathToCp( zRelat
c2200 69 76 65 20 29 3b 0a 20 20 63 68 61 72 20 7a 46  ive );.  char zF
c2210 75 6c 6c 43 70 5b 43 43 48 4d 41 58 50 41 54 48  ullCp[CCHMAXPATH
c2220 5d 20 3d 20 22 5c 30 22 3b 0a 20 20 63 68 61 72  ] = "\0";.  char
c2230 20 2a 7a 46 75 6c 6c 55 54 46 3b 0a 20 20 41 50   *zFullUTF;.  AP
c2240 49 52 45 54 20 72 63 20 3d 20 44 6f 73 51 75 65  IRET rc = DosQue
c2250 72 79 50 61 74 68 49 6e 66 6f 28 20 7a 52 65 6c  ryPathInfo( zRel
c2260 61 74 69 76 65 43 70 2c 20 46 49 4c 5f 51 55 45  ativeCp, FIL_QUE
c2270 52 59 46 55 4c 4c 4e 41 4d 45 2c 20 7a 46 75 6c  RYFULLNAME, zFul
c2280 6c 43 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20  lCp,.           
c2290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c22a0 20 20 20 20 20 43 43 48 4d 41 58 50 41 54 48 20       CCHMAXPATH 
c22b0 29 3b 0a 20 20 66 72 65 65 28 20 7a 52 65 6c 61  );.  free( zRela
c22c0 74 69 76 65 43 70 20 29 3b 0a 20 20 7a 46 75 6c  tiveCp );.  zFul
c22d0 6c 55 54 46 20 3d 20 63 6f 6e 76 65 72 74 43 70  lUTF = convertCp
c22e0 50 61 74 68 54 6f 55 74 66 38 28 20 7a 46 75 6c  PathToUtf8( zFul
c22f0 6c 43 70 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  lCp );.  sqlite3
c2300 5f 73 6e 70 72 69 6e 74 66 28 20 6e 46 75 6c 6c  _snprintf( nFull
c2310 2c 20 7a 46 75 6c 6c 2c 20 7a 46 75 6c 6c 55 54  , zFull, zFullUT
c2320 46 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 46 75  F );.  free( zFu
c2330 6c 6c 55 54 46 20 29 3b 0a 20 20 72 65 74 75 72  llUTF );.  retur
c2340 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52  n rc == NO_ERROR
c2350 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53   ? SQLITE_OK : S
c2360 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a  QLITE_IOERR;.}..
c2370 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 66 69  ./*.** Open a fi
c2380 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
c2390 74 20 6f 73 32 4f 70 65 6e 28 0a 20 20 73 71 6c  t os2Open(.  sql
c23a0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
c23b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f             /* No
c23c0 74 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73  t used */.  cons
c23d0 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20  t char *zName,  
c23e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
c23f0 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a 2f  e of the file */
c2400 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  .  sqlite3_file 
c2410 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 20 20  *id,            
c2420 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 53 51   /* Write the SQ
c2430 4c 69 74 65 20 66 69 6c 65 20 68 61 6e 64 6c 65  Lite file handle
c2440 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66   here */.  int f
c2450 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20  lags,           
c2460 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e           /* Open
c2470 20 6d 6f 64 65 20 66 6c 61 67 73 20 2a 2f 0a 20   mode flags */. 
c2480 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 20   int *pOutFlags 
c2490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
c24a0 2a 20 53 74 61 74 75 73 20 72 65 74 75 72 6e 20  * Status return 
c24b0 66 6c 61 67 73 20 2a 2f 0a 29 7b 0a 20 20 48 46  flags */.){.  HF
c24c0 49 4c 45 20 68 3b 0a 20 20 55 4c 4f 4e 47 20 75  ILE h;.  ULONG u
c24d0 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65 20 3d  lFileAttribute =
c24e0 20 46 49 4c 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20   FILE_NORMAL;.  
c24f0 55 4c 4f 4e 47 20 75 6c 4f 70 65 6e 46 6c 61 67  ULONG ulOpenFlag
c2500 73 20 3d 20 30 3b 0a 20 20 55 4c 4f 4e 47 20 75  s = 0;.  ULONG u
c2510 6c 4f 70 65 6e 4d 6f 64 65 20 3d 20 30 3b 0a 20  lOpenMode = 0;. 
c2520 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20   os2File *pFile 
c2530 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a  = (os2File*)id;.
c2540 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f    APIRET rc = NO
c2550 5f 45 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20  _ERROR;.  ULONG 
c2560 75 6c 41 63 74 69 6f 6e 3b 0a 20 20 63 68 61 72  ulAction;.  char
c2570 20 2a 7a 4e 61 6d 65 43 70 3b 0a 20 20 63 68 61   *zNameCp;.  cha
c2580 72 20 7a 54 6d 70 6e 61 6d 65 5b 43 43 48 4d 41  r zTmpname[CCHMA
c2590 58 50 41 54 48 2b 31 5d 3b 20 20 20 20 2f 2a 20  XPATH+1];    /* 
c25a0 42 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 6e  Buffer to hold n
c25b0 61 6d 65 20 6f 66 20 74 65 6d 70 20 66 69 6c 65  ame of temp file
c25c0 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65   */..  /* If the
c25d0 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
c25e0 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   to this functio
c25f0 6e 20 69 73 20 4e 55 4c 4c 2c 20 67 65 6e 65 72  n is NULL, gener
c2600 61 74 65 20 61 20 0a 20 20 2a 2a 20 74 65 6d 70  ate a .  ** temp
c2610 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 20  orary file name 
c2620 74 6f 20 75 73 65 20 0a 20 20 2a 2f 0a 20 20 69  to use .  */.  i
c2630 66 28 20 21 7a 4e 61 6d 65 20 29 7b 0a 20 20 20  f( !zName ){.   
c2640 20 69 6e 74 20 72 63 20 3d 20 67 65 74 54 65 6d   int rc = getTem
c2650 70 6e 61 6d 65 28 43 43 48 4d 41 58 50 41 54 48  pname(CCHMAXPATH
c2660 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20  +1, zTmpname);. 
c2670 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
c2680 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65  E_OK ){.      re
c2690 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
c26a0 20 20 20 7a 4e 61 6d 65 20 3d 20 7a 54 6d 70 6e     zName = zTmpn
c26b0 61 6d 65 3b 0a 20 20 7d 0a 0a 0a 20 20 6d 65 6d  ame;.  }...  mem
c26c0 73 65 74 28 20 70 46 69 6c 65 2c 20 30 2c 20 73  set( pFile, 0, s
c26d0 69 7a 65 6f 66 28 2a 70 46 69 6c 65 29 20 29 3b  izeof(*pFile) );
c26e0 0a 0a 20 20 4f 53 54 52 41 43 45 32 28 20 22 4f  ..  OSTRACE2( "O
c26f0 50 45 4e 20 77 61 6e 74 20 25 64 5c 6e 22 2c 20  PEN want %d\n", 
c2700 66 6c 61 67 73 20 29 3b 0a 0a 20 20 69 66 28 20  flags );..  if( 
c2710 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
c2720 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b  PEN_READWRITE ){
c2730 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20  .    ulOpenMode 
c2740 7c 3d 20 4f 50 45 4e 5f 41 43 43 45 53 53 5f 52  |= OPEN_ACCESS_R
c2750 45 41 44 57 52 49 54 45 3b 0a 20 20 20 20 4f 53  EADWRITE;.    OS
c2760 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 72 65  TRACE1( "OPEN re
c2770 61 64 2f 77 72 69 74 65 5c 6e 22 20 29 3b 0a 20  ad/write\n" );. 
c2780 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70   }else{.    ulOp
c2790 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 41  enMode |= OPEN_A
c27a0 43 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59 3b 0a  CCESS_READONLY;.
c27b0 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f      OSTRACE1( "O
c27c0 50 45 4e 20 72 65 61 64 20 6f 6e 6c 79 5c 6e 22  PEN read only\n"
c27d0 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66   );.  }..  if( f
c27e0 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50  lags & SQLITE_OP
c27f0 45 4e 5f 43 52 45 41 54 45 20 29 7b 0a 20 20 20  EN_CREATE ){.   
c2800 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20 7c 3d 20   ulOpenFlags |= 
c2810 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50 45 4e  OPEN_ACTION_OPEN
c2820 5f 49 46 5f 45 58 49 53 54 53 20 7c 20 4f 50 45  _IF_EXISTS | OPE
c2830 4e 5f 41 43 54 49 4f 4e 5f 43 52 45 41 54 45 5f  N_ACTION_CREATE_
c2840 49 46 5f 4e 45 57 3b 0a 20 20 20 20 4f 53 54 52  IF_NEW;.    OSTR
c2850 41 43 45 31 28 20 22 4f 50 45 4e 20 6f 70 65 6e  ACE1( "OPEN open
c2860 20 6e 65 77 2f 63 72 65 61 74 65 5c 6e 22 20 29   new/create\n" )
c2870 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75  ;.  }else{.    u
c2880 6c 4f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 50  lOpenFlags |= OP
c2890 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50 45 4e 5f 49  EN_ACTION_OPEN_I
c28a0 46 5f 45 58 49 53 54 53 20 7c 20 4f 50 45 4e 5f  F_EXISTS | OPEN_
c28b0 41 43 54 49 4f 4e 5f 46 41 49 4c 5f 49 46 5f 4e  ACTION_FAIL_IF_N
c28c0 45 57 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31  EW;.    OSTRACE1
c28d0 28 20 22 4f 50 45 4e 20 6f 70 65 6e 20 65 78 69  ( "OPEN open exi
c28e0 73 74 69 6e 67 5c 6e 22 20 29 3b 0a 20 20 7d 0a  sting\n" );.  }.
c28f0 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53  .  if( flags & S
c2900 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
c2910 44 42 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e  DB ){.    ulOpen
c2920 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 53 48 41  Mode |= OPEN_SHA
c2930 52 45 5f 44 45 4e 59 4e 4f 4e 45 3b 0a 20 20 20  RE_DENYNONE;.   
c2940 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e   OSTRACE1( "OPEN
c2950 20 73 68 61 72 65 20 72 65 61 64 2f 77 72 69 74   share read/writ
c2960 65 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b  e\n" );.  }else{
c2970 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20  .    ulOpenMode 
c2980 7c 3d 20 4f 50 45 4e 5f 53 48 41 52 45 5f 44 45  |= OPEN_SHARE_DE
c2990 4e 59 57 52 49 54 45 3b 0a 20 20 20 20 4f 53 54  NYWRITE;.    OST
c29a0 52 41 43 45 31 28 20 22 4f 50 45 4e 20 73 68 61  RACE1( "OPEN sha
c29b0 72 65 20 72 65 61 64 20 6f 6e 6c 79 5c 6e 22 20  re read only\n" 
c29c0 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c  );.  }..  if( fl
c29d0 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
c29e0 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20  N_DELETEONCLOSE 
c29f0 29 7b 0a 20 20 20 20 63 68 61 72 20 70 61 74 68  ){.    char path
c2a00 55 74 66 38 5b 43 43 48 4d 41 58 50 41 54 48 5d  Utf8[CCHMAXPATH]
c2a10 3b 0a 23 69 66 64 65 66 20 4e 44 45 42 55 47 20  ;.#ifdef NDEBUG 
c2a20 2f 2a 20 77 68 65 6e 20 64 65 62 75 67 67 69 6e  /* when debuggin
c2a30 67 20 77 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b  g we want to mak
c2a40 65 20 73 75 72 65 20 69 74 20 69 73 20 64 65 6c  e sure it is del
c2a50 65 74 65 64 20 2a 2f 0a 20 20 20 20 75 6c 46 69  eted */.    ulFi
c2a60 6c 65 41 74 74 72 69 62 75 74 65 20 3d 20 46 49  leAttribute = FI
c2a70 4c 45 5f 48 49 44 44 45 4e 3b 0a 23 65 6e 64 69  LE_HIDDEN;.#endi
c2a80 66 0a 20 20 20 20 6f 73 32 46 75 6c 6c 50 61 74  f.    os2FullPat
c2a90 68 6e 61 6d 65 28 20 70 56 66 73 2c 20 7a 4e 61  hname( pVfs, zNa
c2aa0 6d 65 2c 20 43 43 48 4d 41 58 50 41 54 48 2c 20  me, CCHMAXPATH, 
c2ab0 70 61 74 68 55 74 66 38 20 29 3b 0a 20 20 20 20  pathUtf8 );.    
c2ac0 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c  pFile->pathToDel
c2ad0 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61   = convertUtf8Pa
c2ae0 74 68 54 6f 43 70 28 20 70 61 74 68 55 74 66 38  thToCp( pathUtf8
c2af0 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31   );.    OSTRACE1
c2b00 28 20 22 4f 50 45 4e 20 68 69 64 64 65 6e 2f 64  ( "OPEN hidden/d
c2b10 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65 20 66  elete on close f
c2b20 69 6c 65 20 61 74 74 72 69 62 75 74 65 73 5c 6e  ile attributes\n
c2b30 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  " );.  }else{.  
c2b40 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44    pFile->pathToD
c2b50 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 4f  el = NULL;.    O
c2b60 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 6e  STRACE1( "OPEN n
c2b70 6f 72 6d 61 6c 20 66 69 6c 65 20 61 74 74 72 69  ormal file attri
c2b80 62 75 74 65 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a  bute\n" );.  }..
c2b90 20 20 2f 2a 20 61 6c 77 61 79 73 20 6f 70 65 6e    /* always open
c2ba0 20 69 6e 20 72 61 6e 64 6f 6d 20 61 63 63 65 73   in random acces
c2bb0 73 20 6d 6f 64 65 20 66 6f 72 20 70 6f 73 73 69  s mode for possi
c2bc0 62 6c 79 20 62 65 74 74 65 72 20 73 70 65 65 64  bly better speed
c2bd0 20 2a 2f 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64 65   */.  ulOpenMode
c2be0 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 52   |= OPEN_FLAGS_R
c2bf0 41 4e 44 4f 4d 3b 0a 20 20 75 6c 4f 70 65 6e 4d  ANDOM;.  ulOpenM
c2c00 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47  ode |= OPEN_FLAG
c2c10 53 5f 46 41 49 4c 5f 4f 4e 5f 45 52 52 4f 52 3b  S_FAIL_ON_ERROR;
c2c20 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d  .  ulOpenMode |=
c2c30 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 4e 4f 49 4e   OPEN_FLAGS_NOIN
c2c40 48 45 52 49 54 3b 0a 0a 20 20 7a 4e 61 6d 65 43  HERIT;..  zNameC
c2c50 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50  p = convertUtf8P
c2c60 61 74 68 54 6f 43 70 28 20 7a 4e 61 6d 65 20 29  athToCp( zName )
c2c70 3b 0a 20 20 72 63 20 3d 20 44 6f 73 4f 70 65 6e  ;.  rc = DosOpen
c2c80 28 20 28 50 53 5a 29 7a 4e 61 6d 65 43 70 2c 0a  ( (PSZ)zNameCp,.
c2c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2ca0 26 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  &h,.            
c2cb0 20 20 20 20 26 75 6c 41 63 74 69 6f 6e 2c 0a 20      &ulAction,. 
c2cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
c2cd0 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  L,.             
c2ce0 20 20 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75     ulFileAttribu
c2cf0 74 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  te,.            
c2d00 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 2c      ulOpenFlags,
c2d10 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
c2d20 20 75 6c 4f 70 65 6e 4d 6f 64 65 2c 0a 20 20 20   ulOpenMode,.   
c2d30 20 20 20 20 20 20 20 20 20 20 20 20 20 28 50 45               (PE
c2d40 41 4f 50 32 29 4e 55 4c 4c 20 29 3b 0a 20 20 66  AOP2)NULL );.  f
c2d50 72 65 65 28 20 7a 4e 61 6d 65 43 70 20 29 3b 0a  ree( zNameCp );.
c2d60 20 20 69 66 28 20 72 63 20 21 3d 20 4e 4f 5f 45    if( rc != NO_E
c2d70 52 52 4f 52 20 29 7b 0a 20 20 20 20 4f 53 54 52  RROR ){.    OSTR
c2d80 41 43 45 37 28 20 22 4f 50 45 4e 20 49 6e 76 61  ACE7( "OPEN Inva
c2d90 6c 69 64 20 68 61 6e 64 6c 65 20 72 63 3d 25 64  lid handle rc=%d
c2da0 3a 20 7a 4e 61 6d 65 3d 25 73 2c 20 75 6c 41 63  : zName=%s, ulAc
c2db0 74 69 6f 6e 3d 25 23 6c 78 2c 20 75 6c 41 74 74  tion=%#lx, ulAtt
c2dc0 72 3d 25 23 6c 78 2c 20 75 6c 46 6c 61 67 73 3d  r=%#lx, ulFlags=
c2dd0 25 23 6c 78 2c 20 75 6c 4d 6f 64 65 3d 25 23 6c  %#lx, ulMode=%#l
c2de0 78 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  x\n",.          
c2df0 20 20 20 20 72 63 2c 20 7a 4e 61 6d 65 2c 20 75      rc, zName, u
c2e00 6c 41 63 74 69 6f 6e 2c 20 75 6c 46 69 6c 65 41  lAction, ulFileA
c2e10 74 74 72 69 62 75 74 65 2c 20 75 6c 4f 70 65 6e  ttribute, ulOpen
c2e20 46 6c 61 67 73 2c 20 75 6c 4f 70 65 6e 4d 6f 64  Flags, ulOpenMod
c2e30 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 46 69  e );.    if( pFi
c2e40 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 0a  le->pathToDel ).
c2e50 20 20 20 20 20 20 66 72 65 65 28 20 70 46 69 6c        free( pFil
c2e60 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a  e->pathToDel );.
c2e70 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 54      pFile->pathT
c2e80 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  oDel = NULL;.   
c2e90 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c   if( flags & SQL
c2ea0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
c2eb0 54 45 20 29 7b 0a 20 20 20 20 20 20 4f 53 54 52  TE ){.      OSTR
c2ec0 41 43 45 32 28 20 22 4f 50 45 4e 20 25 64 20 49  ACE2( "OPEN %d I
c2ed0 6e 76 61 6c 69 64 20 68 61 6e 64 6c 65 5c 6e 22  nvalid handle\n"
c2ee0 2c 20 28 28 66 6c 61 67 73 20 7c 20 53 51 4c 49  , ((flags | SQLI
c2ef0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
c2f00 29 20 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e  ) & ~SQLITE_OPEN
c2f10 5f 52 45 41 44 57 52 49 54 45 29 20 29 3b 0a 20  _READWRITE) );. 
c2f20 20 20 20 20 20 72 65 74 75 72 6e 20 6f 73 32 4f       return os2O
c2f30 70 65 6e 28 20 70 56 66 73 2c 20 7a 4e 61 6d 65  pen( pVfs, zName
c2f40 2c 20 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20  , id,.          
c2f50 20 20 20 20 20 20 20 20 20 20 20 20 28 28 66 6c              ((fl
c2f60 61 67 73 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45  ags | SQLITE_OPE
c2f70 4e 5f 52 45 41 44 4f 4e 4c 59 29 20 26 20 7e 53  N_READONLY) & ~S
c2f80 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
c2f90 52 49 54 45 29 2c 0a 20 20 20 20 20 20 20 20 20  RITE),.         
c2fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4f 75               pOu
c2fb0 74 46 6c 61 67 73 20 29 3b 0a 20 20 20 20 7d 65  tFlags );.    }e
c2fc0 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72  lse{.      retur
c2fd0 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45  n SQLITE_CANTOPE
c2fe0 4e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  N;.    }.  }..  
c2ff0 69 66 28 20 70 4f 75 74 46 6c 61 67 73 20 29 7b  if( pOutFlags ){
c3000 0a 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 20  .    *pOutFlags 
c3010 3d 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  = flags & SQLITE
c3020 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20  _OPEN_READWRITE 
c3030 3f 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  ? SQLITE_OPEN_RE
c3040 41 44 57 52 49 54 45 20 3a 20 53 51 4c 49 54 45  ADWRITE : SQLITE
c3050 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a  _OPEN_READONLY;.
c3060 20 20 7d 0a 0a 20 20 70 46 69 6c 65 2d 3e 70 4d    }..  pFile->pM
c3070 65 74 68 6f 64 20 3d 20 26 6f 73 32 49 6f 4d 65  ethod = &os2IoMe
c3080 74 68 6f 64 3b 0a 20 20 70 46 69 6c 65 2d 3e 68  thod;.  pFile->h
c3090 20 3d 20 68 3b 0a 20 20 4f 70 65 6e 43 6f 75 6e   = h;.  OpenCoun
c30a0 74 65 72 28 2b 31 29 3b 0a 20 20 4f 53 54 52 41  ter(+1);.  OSTRA
c30b0 43 45 33 28 20 22 4f 50 45 4e 20 25 64 20 70 4f  CE3( "OPEN %d pO
c30c0 75 74 46 6c 61 67 73 3d 25 64 5c 6e 22 2c 20 70  utFlags=%d\n", p
c30d0 46 69 6c 65 2d 3e 68 2c 20 70 4f 75 74 46 6c 61  File->h, pOutFla
c30e0 67 73 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  gs );.  return S
c30f0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
c3100 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 6e 61  ** Delete the na
c3110 6d 65 64 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  med file..*/.sta
c3120 74 69 63 20 69 6e 74 20 6f 73 32 44 65 6c 65 74  tic int os2Delet
c3130 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  e(.  sqlite3_vfs
c3140 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20   *pVfs,         
c3150 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
c3160 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 20 2a  ot used on os2 *
c3170 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
c3180 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20 20  zFilename,      
c3190 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
c31a0 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65  me of file to de
c31b0 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 73 79  lete */.  int sy
c31c0 6e 63 44 69 72 20 20 20 20 20 20 20 20 20 20 20  ncDir           
c31d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c31e0 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20   /* Not used on 
c31f0 6f 73 32 20 2a 2f 0a 29 7b 0a 20 20 41 50 49 52  os2 */.){.  APIR
c3200 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52  ET rc = NO_ERROR
c3210 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  ;.  char *zFilen
c3220 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55  ameCp = convertU
c3230 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a 46 69  tf8PathToCp( zFi
c3240 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 53 69 6d 75  lename );.  Simu
c3250 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74  lateIOError( ret
c3260 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
c3270 5f 44 45 4c 45 54 45 20 29 3b 0a 20 20 72 63 20  _DELETE );.  rc 
c3280 3d 20 44 6f 73 44 65 6c 65 74 65 28 20 28 50 53  = DosDelete( (PS
c3290 5a 29 7a 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b  Z)zFilenameCp );
c32a0 0a 20 20 66 72 65 65 28 20 7a 46 69 6c 65 6e 61  .  free( zFilena
c32b0 6d 65 43 70 20 29 3b 0a 20 20 4f 53 54 52 41 43  meCp );.  OSTRAC
c32c0 45 32 28 20 22 44 45 4c 45 54 45 20 5c 22 25 73  E2( "DELETE \"%s
c32d0 5c 22 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65  \"\n", zFilename
c32e0 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20   );.  return rc 
c32f0 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51  == NO_ERROR ? SQ
c3300 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45  LITE_OK : SQLITE
c3310 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 3b 0a 7d  _IOERR_DELETE;.}
c3320 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68  ../*.** Check th
c3330 65 20 65 78 69 73 74 61 6e 63 65 20 61 6e 64 20  e existance and 
c3340 73 74 61 74 75 73 20 6f 66 20 61 20 66 69 6c 65  status of a file
c3350 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
c3360 6f 73 32 41 63 63 65 73 73 28 0a 20 20 73 71 6c  os2Access(.  sql
c3370 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
c3380 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73         /* Not us
c3390 65 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a 20 20 63  ed on os2 */.  c
c33a0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
c33b0 6e 61 6d 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65  name,    /* Name
c33c0 20 6f 66 20 66 69 6c 65 20 74 6f 20 63 68 65 63   of file to chec
c33d0 6b 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  k */.  int flags
c33e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
c33f0 20 2f 2a 20 54 79 70 65 20 6f 66 20 74 65 73 74   /* Type of test
c3400 20 74 6f 20 6d 61 6b 65 20 6f 6e 20 74 68 69 73   to make on this
c3410 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 2a   file */.  int *
c3420 70 4f 75 74 20 20 20 20 20 20 20 20 20 20 20 20  pOut            
c3430 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 72 65       /* Write re
c3440 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b  sults here */.){
c3450 0a 20 20 46 49 4c 45 53 54 41 54 55 53 33 20 66  .  FILESTATUS3 f
c3460 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 3b 0a  sts3ConfigInfo;.
c3470 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f    APIRET rc = NO
c3480 5f 45 52 52 4f 52 3b 0a 20 20 63 68 61 72 20 2a  _ERROR;.  char *
c3490 7a 46 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f  zFilenameCp = co
c34a0 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43  nvertUtf8PathToC
c34b0 70 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a  p( zFilename );.
c34c0 0a 20 20 6d 65 6d 73 65 74 28 20 26 66 73 74 73  .  memset( &fsts
c34d0 33 43 6f 6e 66 69 67 49 6e 66 6f 2c 20 30 2c 20  3ConfigInfo, 0, 
c34e0 73 69 7a 65 6f 66 28 66 73 74 73 33 43 6f 6e 66  sizeof(fsts3Conf
c34f0 69 67 49 6e 66 6f 29 20 29 3b 0a 20 20 72 63 20  igInfo) );.  rc 
c3500 3d 20 44 6f 73 51 75 65 72 79 50 61 74 68 49 6e  = DosQueryPathIn
c3510 66 6f 28 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61  fo( (PSZ)zFilena
c3520 6d 65 43 70 2c 20 46 49 4c 5f 53 54 41 4e 44 41  meCp, FIL_STANDA
c3530 52 44 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  RD,.            
c3540 20 20 20 20 20 20 20 20 20 20 20 20 20 26 66 73               &fs
c3550 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2c 20 73  ts3ConfigInfo, s
c3560 69 7a 65 6f 66 28 46 49 4c 45 53 54 41 54 55 53  izeof(FILESTATUS
c3570 33 29 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 46  3) );.  free( zF
c3580 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20 4f  ilenameCp );.  O
c3590 53 54 52 41 43 45 34 28 20 22 41 43 43 45 53 53  STRACE4( "ACCESS
c35a0 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f   fsts3ConfigInfo
c35b0 2e 61 74 74 72 46 69 6c 65 3d 25 64 20 66 6c 61  .attrFile=%d fla
c35c0 67 73 3d 25 64 20 72 63 3d 25 64 5c 6e 22 2c 0a  gs=%d rc=%d\n",.
c35d0 20 20 20 20 20 20 20 20 20 20 20 20 66 73 74 73              fsts
c35e0 33 43 6f 6e 66 69 67 49 6e 66 6f 2e 61 74 74 72  3ConfigInfo.attr
c35f0 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 72 63 20  File, flags, rc 
c3600 29 3b 0a 20 20 73 77 69 74 63 68 28 20 66 6c 61  );.  switch( fla
c3610 67 73 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53  gs ){.    case S
c3620 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41  QLITE_ACCESS_REA
c3630 44 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  D:.    case SQLI
c3640 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53  TE_ACCESS_EXISTS
c3650 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 28 72 63  :.      rc = (rc
c3660 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20   == NO_ERROR);. 
c3670 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22       OSTRACE3( "
c3680 41 43 43 45 53 53 20 25 73 20 61 63 63 65 73 73  ACCESS %s access
c3690 20 6f 66 20 72 65 61 64 20 61 6e 64 20 65 78 69   of read and exi
c36a0 73 74 73 20 20 72 63 3d 25 64 5c 6e 22 2c 20 7a  sts  rc=%d\n", z
c36b0 46 69 6c 65 6e 61 6d 65 2c 20 72 63 20 29 3b 0a  Filename, rc );.
c36c0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
c36d0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43   case SQLITE_ACC
c36e0 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a 0a 20  ESS_READWRITE:. 
c36f0 20 20 20 20 20 72 63 20 3d 20 28 72 63 20 3d 3d       rc = (rc ==
c3700 20 4e 4f 5f 45 52 52 4f 52 29 20 26 26 20 28 20   NO_ERROR) && ( 
c3710 28 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f  (fsts3ConfigInfo
c3720 2e 61 74 74 72 46 69 6c 65 20 26 20 46 49 4c 45  .attrFile & FILE
c3730 5f 52 45 41 44 4f 4e 4c 59 29 20 3d 3d 20 30 20  _READONLY) == 0 
c3740 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45  );.      OSTRACE
c3750 33 28 20 22 41 43 43 45 53 53 20 25 73 20 61 63  3( "ACCESS %s ac
c3760 63 65 73 73 20 6f 66 20 72 65 61 64 2f 77 72 69  cess of read/wri
c3770 74 65 20 20 72 63 3d 25 64 5c 6e 22 2c 20 7a 46  te  rc=%d\n", zF
c3780 69 6c 65 6e 61 6d 65 2c 20 72 63 20 29 3b 0a 20  ilename, rc );. 
c3790 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
c37a0 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 61  default:.      a
c37b0 73 73 65 72 74 28 20 21 22 49 6e 76 61 6c 69 64  ssert( !"Invalid
c37c0 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 22   flags argument"
c37d0 20 29 3b 0a 20 20 7d 0a 20 20 2a 70 4f 75 74 20   );.  }.  *pOut 
c37e0 3d 20 72 63 3b 0a 20 20 72 65 74 75 72 6e 20 53  = rc;.  return S
c37f0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23 69  QLITE_OK;.}...#i
c3800 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
c3810 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e  T_LOAD_EXTENSION
c3820 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65  ./*.** Interface
c3830 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20  s for opening a 
c3840 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20  shared library, 
c3850 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f  finding entry po
c3860 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74  ints.** within t
c3870 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72  he shared librar
c3880 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74  y, and closing t
c3890 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72  he shared librar
c38a0 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  y..*/./*.** Inte
c38b0 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69  rfaces for openi
c38c0 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72  ng a shared libr
c38d0 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74  ary, finding ent
c38e0 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74  ry points.** wit
c38f0 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c  hin the shared l
c3900 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73  ibrary, and clos
c3910 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c  ing the shared l
c3920 69 62 72 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69  ibrary..*/.stati
c3930 63 20 76 6f 69 64 20 2a 6f 73 32 44 6c 4f 70 65  c void *os2DlOpe
c3940 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  n(sqlite3_vfs *p
c3950 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  Vfs, const char 
c3960 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 55  *zFilename){.  U
c3970 43 48 41 52 20 6c 6f 61 64 45 72 72 5b 32 35 36  CHAR loadErr[256
c3980 5d 3b 0a 20 20 48 4d 4f 44 55 4c 45 20 68 6d 6f  ];.  HMODULE hmo
c3990 64 3b 0a 20 20 41 50 49 52 45 54 20 72 63 3b 0a  d;.  APIRET rc;.
c39a0 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d    char *zFilenam
c39b0 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66  eCp = convertUtf
c39c0 38 50 61 74 68 54 6f 43 70 28 7a 46 69 6c 65 6e  8PathToCp(zFilen
c39d0 61 6d 65 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73  ame);.  rc = Dos
c39e0 4c 6f 61 64 4d 6f 64 75 6c 65 28 28 50 53 5a 29  LoadModule((PSZ)
c39f0 6c 6f 61 64 45 72 72 2c 20 73 69 7a 65 6f 66 28  loadErr, sizeof(
c3a00 6c 6f 61 64 45 72 72 29 2c 20 7a 46 69 6c 65 6e  loadErr), zFilen
c3a10 61 6d 65 43 70 2c 20 26 68 6d 6f 64 29 3b 0a 20  ameCp, &hmod);. 
c3a20 20 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 43   free(zFilenameC
c3a30 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20  p);.  return rc 
c3a40 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 30 20  != NO_ERROR ? 0 
c3a50 3a 20 28 76 6f 69 64 2a 29 68 6d 6f 64 3b 0a 7d  : (void*)hmod;.}
c3a60 0a 2f 2a 0a 2a 2a 20 41 20 6e 6f 2d 6f 70 20 73  ./*.** A no-op s
c3a70 69 6e 63 65 20 74 68 65 20 65 72 72 6f 72 20 63  ince the error c
c3a80 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20  ode is returned 
c3a90 6f 6e 20 74 68 65 20 44 6f 73 4c 6f 61 64 4d 6f  on the DosLoadMo
c3aa0 64 75 6c 65 20 63 61 6c 6c 2e 0a 2a 2a 20 6f 73  dule call..** os
c3ab0 32 44 6c 6f 70 65 6e 20 72 65 74 75 72 6e 73 20  2Dlopen returns 
c3ac0 7a 65 72 6f 20 69 66 20 44 6f 73 4c 6f 61 64 4d  zero if DosLoadM
c3ad0 6f 64 75 6c 65 20 69 73 20 6e 6f 74 20 73 75 63  odule is not suc
c3ae0 63 65 73 73 66 75 6c 2e 0a 2a 2f 0a 73 74 61 74  cessful..*/.stat
c3af0 69 63 20 76 6f 69 64 20 6f 73 32 44 6c 45 72 72  ic void os2DlErr
c3b00 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  or(sqlite3_vfs *
c3b10 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20  pVfs, int nBuf, 
c3b20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a  char *zBufOut){.
c3b30 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0a 7d 0a 73 74  /* no-op */.}.st
c3b40 61 74 69 63 20 76 6f 69 64 20 2a 6f 73 32 44 6c  atic void *os2Dl
c3b50 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20  Sym(sqlite3_vfs 
c3b60 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61  *pVfs, void *pHa
c3b70 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  ndle, const char
c3b80 20 2a 7a 53 79 6d 62 6f 6c 29 7b 0a 20 20 50 46   *zSymbol){.  PF
c3b90 4e 20 70 66 6e 3b 0a 20 20 41 50 49 52 45 54 20  N pfn;.  APIRET 
c3ba0 72 63 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51 75  rc;.  rc = DosQu
c3bb0 65 72 79 50 72 6f 63 41 64 64 72 28 28 48 4d 4f  eryProcAddr((HMO
c3bc0 44 55 4c 45 29 70 48 61 6e 64 6c 65 2c 20 30 4c  DULE)pHandle, 0L
c3bd0 2c 20 7a 53 79 6d 62 6f 6c 2c 20 26 70 66 6e 29  , zSymbol, &pfn)
c3be0 3b 0a 20 20 69 66 28 20 72 63 20 21 3d 20 4e 4f  ;.  if( rc != NO
c3bf0 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 2f 2a  _ERROR ){.    /*
c3c00 20 69 66 20 74 68 65 20 73 79 6d 62 6f 6c 20 69   if the symbol i
c3c10 74 73 65 6c 66 20 77 61 73 20 6e 6f 74 20 66 6f  tself was not fo
c3c20 75 6e 64 2c 20 73 65 61 72 63 68 20 61 67 61 69  und, search agai
c3c30 6e 20 66 6f 72 20 74 68 65 20 73 61 6d 65 0a 20  n for the same. 
c3c40 20 20 20 20 2a 20 73 79 6d 62 6f 6c 20 77 69 74      * symbol wit
c3c50 68 20 61 6e 20 65 78 74 72 61 20 75 6e 64 65 72  h an extra under
c3c60 73 63 6f 72 65 2c 20 74 68 61 74 20 6d 69 67 68  score, that migh
c3c70 74 20 62 65 20 6e 65 65 64 65 64 20 64 65 70 65  t be needed depe
c3c80 6e 64 69 6e 67 0a 20 20 20 20 20 2a 20 6f 6e 20  nding.     * on 
c3c90 74 68 65 20 63 61 6c 6c 69 6e 67 20 63 6f 6e 76  the calling conv
c3ca0 65 6e 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 63 68  ention */.    ch
c3cb0 61 72 20 5f 7a 53 79 6d 62 6f 6c 5b 32 35 36 5d  ar _zSymbol[256]
c3cc0 20 3d 20 22 5f 22 3b 0a 20 20 20 20 73 74 72 6e   = "_";.    strn
c3cd0 63 61 74 28 5f 7a 53 79 6d 62 6f 6c 2c 20 7a 53  cat(_zSymbol, zS
c3ce0 79 6d 62 6f 6c 2c 20 32 35 35 29 3b 0a 20 20 20  ymbol, 255);.   
c3cf0 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50 72   rc = DosQueryPr
c3d00 6f 63 41 64 64 72 28 28 48 4d 4f 44 55 4c 45 29  ocAddr((HMODULE)
c3d10 70 48 61 6e 64 6c 65 2c 20 30 4c 2c 20 5f 7a 53  pHandle, 0L, _zS
c3d20 79 6d 62 6f 6c 2c 20 26 70 66 6e 29 3b 0a 20 20  ymbol, &pfn);.  
c3d30 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 21 3d  }.  return rc !=
c3d40 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 30 20 3a 20   NO_ERROR ? 0 : 
c3d50 28 76 6f 69 64 2a 29 70 66 6e 3b 0a 7d 0a 73 74  (void*)pfn;.}.st
c3d60 61 74 69 63 20 76 6f 69 64 20 6f 73 32 44 6c 43  atic void os2DlC
c3d70 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73  lose(sqlite3_vfs
c3d80 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48   *pVfs, void *pH
c3d90 61 6e 64 6c 65 29 7b 0a 20 20 44 6f 73 46 72 65  andle){.  DosFre
c3da0 65 4d 6f 64 75 6c 65 28 28 48 4d 4f 44 55 4c 45  eModule((HMODULE
c3db0 29 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c  )pHandle);.}.#el
c3dc0 73 65 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f  se /* if SQLITE_
c3dd0 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53  OMIT_LOAD_EXTENS
c3de0 49 4f 4e 20 69 73 20 64 65 66 69 6e 65 64 3a 20  ION is defined: 
c3df0 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 6f 73 32  */.  #define os2
c3e00 44 6c 4f 70 65 6e 20 30 0a 20 20 23 64 65 66 69  DlOpen 0.  #defi
c3e10 6e 65 20 6f 73 32 44 6c 45 72 72 6f 72 20 30 0a  ne os2DlError 0.
c3e20 20 20 23 64 65 66 69 6e 65 20 6f 73 32 44 6c 53    #define os2DlS
c3e30 79 6d 20 30 0a 20 20 23 64 65 66 69 6e 65 20 6f  ym 0.  #define o
c3e40 73 32 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64  s2DlClose 0.#end
c3e50 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  if.../*.** Write
c3e60 20 75 70 20 74 6f 20 6e 42 75 66 20 62 79 74 65   up to nBuf byte
c3e70 73 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20  s of randomness 
c3e80 69 6e 74 6f 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74  into zBuf..*/.st
c3e90 61 74 69 63 20 69 6e 74 20 6f 73 32 52 61 6e 64  atic int os2Rand
c3ea0 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76  omness(sqlite3_v
c3eb0 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42  fs *pVfs, int nB
c3ec0 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 20 29  uf, char *zBuf )
c3ed0 7b 0a 20 20 55 4c 4f 4e 47 20 73 69 7a 65 6f 66  {.  ULONG sizeof
c3ee0 55 4c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 55  ULong = sizeof(U
c3ef0 4c 4f 4e 47 29 3b 0a 20 20 69 6e 74 20 6e 20 3d  LONG);.  int n =
c3f00 20 30 3b 0a 20 20 69 66 28 20 73 69 7a 65 6f 66   0;.  if( sizeof
c3f10 28 44 41 54 45 54 49 4d 45 29 20 3c 3d 20 6e 42  (DATETIME) <= nB
c3f20 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 44 41  uf - n ){.    DA
c3f30 54 45 54 49 4d 45 20 78 3b 0a 20 20 20 20 44 6f  TETIME x;.    Do
c3f40 73 47 65 74 44 61 74 65 54 69 6d 65 28 26 78 29  sGetDateTime(&x)
c3f50 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42  ;.    memcpy(&zB
c3f60 75 66 5b 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 6f  uf[n], &x, sizeo
c3f70 66 28 78 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20  f(x));.    n += 
c3f80 73 69 7a 65 6f 66 28 78 29 3b 0a 20 20 7d 0a 0a  sizeof(x);.  }..
c3f90 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e    if( sizeofULon
c3fa0 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b  g <= nBuf - n ){
c3fb0 0a 20 20 20 20 50 50 49 42 20 70 70 69 62 3b 0a  .    PPIB ppib;.
c3fc0 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c      DosGetInfoBl
c3fd0 6f 63 6b 73 28 4e 55 4c 4c 2c 20 26 70 70 69 62  ocks(NULL, &ppib
c3fe0 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a  );.    memcpy(&z
c3ff0 42 75 66 5b 6e 5d 2c 20 26 70 70 69 62 2d 3e 70  Buf[n], &ppib->p
c4000 69 62 5f 75 6c 70 69 64 2c 20 73 69 7a 65 6f 66  ib_ulpid, sizeof
c4010 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d  ULong);.    n +=
c4020 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20   sizeofULong;.  
c4030 7d 0a 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 55  }..  if( sizeofU
c4040 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e  Long <= nBuf - n
c4050 20 29 7b 0a 20 20 20 20 50 54 49 42 20 70 74 69   ){.    PTIB pti
c4060 62 3b 0a 20 20 20 20 44 6f 73 47 65 74 49 6e 66  b;.    DosGetInf
c4070 6f 42 6c 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e  oBlocks(&ptib, N
c4080 55 4c 4c 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  ULL);.    memcpy
c4090 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 70 74 69 62  (&zBuf[n], &ptib
c40a0 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62  ->tib_ptib2->tib
c40b0 32 5f 75 6c 74 69 64 2c 20 73 69 7a 65 6f 66 55  2_ultid, sizeofU
c40c0 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20  Long);.    n += 
c40d0 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d  sizeofULong;.  }
c40e0 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20 73 74 69  ..  /* if we sti
c40f0 6c 6c 20 68 61 76 65 6e 27 74 20 66 69 6c 6c 65  ll haven't fille
c4100 64 20 74 68 65 20 62 75 66 66 65 72 20 79 65 74  d the buffer yet
c4110 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77   the following w
c4120 69 6c 6c 20 2a 2f 0a 20 20 2f 2a 20 67 72 61 62  ill */.  /* grab
c4130 20 65 76 65 72 79 74 68 69 6e 67 20 6f 6e 63 65   everything once
c4140 20 69 6e 73 74 65 61 64 20 6f 66 20 6d 61 6b 69   instead of maki
c4150 6e 67 20 73 65 76 65 72 61 6c 20 63 61 6c 6c 73  ng several calls
c4160 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 69 74   for a single it
c4170 65 6d 20 2a 2f 0a 20 20 69 66 28 20 73 69 7a 65  em */.  if( size
c4180 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20  ofULong <= nBuf 
c4190 2d 20 6e 20 29 7b 0a 20 20 20 20 55 4c 4f 4e 47  - n ){.    ULONG
c41a0 20 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 4d   ulSysInfo[QSV_M
c41b0 41 58 5d 3b 0a 20 20 20 20 44 6f 73 51 75 65 72  AX];.    DosQuer
c41c0 79 53 79 73 49 6e 66 6f 28 31 4c 2c 20 51 53 56  ySysInfo(1L, QSV
c41d0 5f 4d 41 58 2c 20 75 6c 53 79 73 49 6e 66 6f 2c  _MAX, ulSysInfo,
c41e0 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 2a 20 51   sizeofULong * Q
c41f0 53 56 5f 4d 41 58 29 3b 0a 0a 20 20 20 20 6d 65  SV_MAX);..    me
c4200 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26  mcpy(&zBuf[n], &
c4210 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 53  ulSysInfo[QSV_MS
c4220 5f 43 4f 55 4e 54 20 2d 20 31 5d 2c 20 73 69 7a  _COUNT - 1], siz
c4230 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e  eofULong);.    n
c4240 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b   += sizeofULong;
c4250 0a 0a 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66  ..    if( sizeof
c4260 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20  ULong <= nBuf - 
c4270 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  n ){.      memcp
c4280 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53  y(&zBuf[n], &ulS
c4290 79 73 49 6e 66 6f 5b 51 53 56 5f 54 49 4d 45 52  ysInfo[QSV_TIMER
c42a0 5f 49 4e 54 45 52 56 41 4c 20 2d 20 31 5d 2c 20  _INTERVAL - 1], 
c42b0 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20  sizeofULong);.  
c42c0 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55      n += sizeofU
c42d0 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Long;.    }.    
c42e0 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20  if( sizeofULong 
c42f0 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20  <= nBuf - n ){. 
c4300 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75       memcpy(&zBu
c4310 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f  f[n], &ulSysInfo
c4320 5b 51 53 56 5f 54 49 4d 45 5f 4c 4f 57 20 2d 20  [QSV_TIME_LOW - 
c4330 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29  1], sizeofULong)
c4340 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a  ;.      n += siz
c4350 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a  eofULong;.    }.
c4360 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c      if( sizeofUL
c4370 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20  ong <= nBuf - n 
c4380 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
c4390 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73  &zBuf[n], &ulSys
c43a0 49 6e 66 6f 5b 51 53 56 5f 54 49 4d 45 5f 48 49  Info[QSV_TIME_HI
c43b0 47 48 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55  GH - 1], sizeofU
c43c0 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b  Long);.      n +
c43d0 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20  = sizeofULong;. 
c43e0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a     }.    if( siz
c43f0 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66  eofULong <= nBuf
c4400 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65   - n ){.      me
c4410 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26  mcpy(&zBuf[n], &
c4420 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 54 4f  ulSysInfo[QSV_TO
c4430 54 41 56 41 49 4c 4d 45 4d 20 2d 20 31 5d 2c 20  TAVAILMEM - 1], 
c4440 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20  sizeofULong);.  
c4450 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55      n += sizeofU
c4460 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Long;.    }.  }.
c4470 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a  .  return n;.}..
c4480 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20  /*.** Sleep for 
c4490 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e 20  a little while. 
c44a0 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75   Return the amou
c44b0 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 74  nt of time slept
c44c0 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e  ..** The argumen
c44d0 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  t is the number 
c44e0 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20  of microseconds 
c44f0 77 65 20 77 61 6e 74 20 74 6f 20 73 6c 65 65 70  we want to sleep
c4500 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20  ..** The return 
c4510 76 61 6c 75 65 20 69 73 20 74 68 65 20 6e 75 6d  value is the num
c4520 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f  ber of microseco
c4530 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74  nds of sleep act
c4540 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74  ually.** request
c4550 65 64 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65  ed from the unde
c4560 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67  rlying operating
c4570 20 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d 62 65   system, a numbe
c4580 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67 68 74  r which.** might
c4590 20 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e   be greater than
c45a0 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65   or equal to the
c45b0 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 74 20 6e   argument, but n
c45c0 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20  ot less.** than 
c45d0 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f  the argument..*/
c45e0 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 53  .static int os2S
c45f0 6c 65 65 70 28 20 73 71 6c 69 74 65 33 5f 76 66  leep( sqlite3_vf
c4600 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d 69 63  s *pVfs, int mic
c4610 72 6f 73 65 63 20 29 7b 0a 20 20 44 6f 73 53 6c  rosec ){.  DosSl
c4620 65 65 70 28 20 28 6d 69 63 72 6f 73 65 63 2f 31  eep( (microsec/1
c4630 30 30 30 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  000) );.  return
c4640 20 6d 69 63 72 6f 73 65 63 3b 0a 7d 0a 0a 2f 2a   microsec;.}../*
c4650 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
c4660 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73  g variable, if s
c4670 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f  et to a non-zero
c4680 20 76 61 6c 75 65 2c 20 62 65 63 6f 6d 65 73 20   value, becomes 
c4690 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 65  the result.** re
c46a0 74 75 72 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  turned from sqli
c46b0 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65  te3OsCurrentTime
c46c0 28 29 2e 20 20 54 68 69 73 20 69 73 20 75 73 65  ().  This is use
c46d0 64 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a  d for testing..*
c46e0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
c46f0 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20  TEST.SQLITE_API 
c4700 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 75 72 72  int sqlite3_curr
c4710 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 0a 23 65  ent_time = 0;.#e
c4720 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64  ndif../*.** Find
c4730 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d   the current tim
c4740 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20  e (in Universal 
c4750 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65  Coordinated Time
c4760 29 2e 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a  ).  Write the.**
c4770 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e   current time an
c4780 64 20 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69  d date as a Juli
c4790 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e  an Day number in
c47a0 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a  to *prNow and.**
c47b0 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75   return 0.  Retu
c47c0 72 6e 20 31 20 69 66 20 74 68 65 20 74 69 6d 65  rn 1 if the time
c47d0 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74   and date cannot
c47e0 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 69 6e   be found..*/.in
c47f0 74 20 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65  t os2CurrentTime
c4800 28 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  ( sqlite3_vfs *p
c4810 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70 72 4e  Vfs, double *prN
c4820 6f 77 20 29 7b 0a 20 20 64 6f 75 62 6c 65 20 6e  ow ){.  double n
c4830 6f 77 3b 0a 20 20 53 48 4f 52 54 20 6d 69 6e 75  ow;.  SHORT minu
c4840 74 65 3b 20 2f 2a 20 6e 65 65 64 73 20 74 6f 20  te; /* needs to 
c4850 62 65 20 61 62 6c 65 20 74 6f 20 63 6f 70 65 20  be able to cope 
c4860 77 69 74 68 20 6e 65 67 61 74 69 76 65 20 74 69  with negative ti
c4870 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 74 20 2a 2f  mezone offset */
c4880 0a 20 20 55 53 48 4f 52 54 20 73 65 63 6f 6e 64  .  USHORT second
c4890 2c 20 68 6f 75 72 2c 0a 20 20 20 20 20 20 20 20  , hour,.        
c48a0 20 64 61 79 2c 20 6d 6f 6e 74 68 2c 20 79 65 61   day, month, yea
c48b0 72 3b 0a 20 20 44 41 54 45 54 49 4d 45 20 64 74  r;.  DATETIME dt
c48c0 3b 0a 20 20 44 6f 73 47 65 74 44 61 74 65 54 69  ;.  DosGetDateTi
c48d0 6d 65 28 20 26 64 74 20 29 3b 0a 20 20 73 65 63  me( &dt );.  sec
c48e0 6f 6e 64 20 3d 20 28 55 53 48 4f 52 54 29 64 74  ond = (USHORT)dt
c48f0 2e 73 65 63 6f 6e 64 73 3b 0a 20 20 6d 69 6e 75  .seconds;.  minu
c4900 74 65 20 3d 20 28 53 48 4f 52 54 29 64 74 2e 6d  te = (SHORT)dt.m
c4910 69 6e 75 74 65 73 20 2b 20 64 74 2e 74 69 6d 65  inutes + dt.time
c4920 7a 6f 6e 65 3b 0a 20 20 68 6f 75 72 20 3d 20 28  zone;.  hour = (
c4930 55 53 48 4f 52 54 29 64 74 2e 68 6f 75 72 73 3b  USHORT)dt.hours;
c4940 0a 20 20 64 61 79 20 3d 20 28 55 53 48 4f 52 54  .  day = (USHORT
c4950 29 64 74 2e 64 61 79 3b 0a 20 20 6d 6f 6e 74 68  )dt.day;.  month
c4960 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 6d 6f   = (USHORT)dt.mo
c4970 6e 74 68 3b 0a 20 20 79 65 61 72 20 3d 20 28 55  nth;.  year = (U
c4980 53 48 4f 52 54 29 64 74 2e 79 65 61 72 3b 0a 0a  SHORT)dt.year;..
c4990 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74 69 6f 6e    /* Calculation
c49a0 73 20 66 72 6f 6d 20 68 74 74 70 3a 2f 2f 77 77  s from http://ww
c49b0 77 2e 61 73 74 72 6f 2e 6b 65 65 6c 65 2e 61 63  w.astro.keele.ac
c49c0 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f  .uk/~rno/Astrono
c49d0 6d 79 2f 68 6a 64 2e 68 74 6d 6c 0a 20 20 20 20  my/hjd.html.    
c49e0 20 68 74 74 70 3a 2f 2f 77 77 77 2e 61 73 74 72   http://www.astr
c49f0 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 6b 2f 7e 72  o.keele.ac.uk/~r
c4a00 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 2f 68 6a 64  no/Astronomy/hjd
c4a10 2d 30 2e 31 2e 63 20 2a 2f 0a 20 20 2f 2a 20 43  -0.1.c */.  /* C
c4a20 61 6c 63 75 6c 61 74 65 20 74 68 65 20 4a 75 6c  alculate the Jul
c4a30 69 61 6e 20 64 61 79 73 20 2a 2f 0a 20 20 6e 6f  ian days */.  no
c4a40 77 20 3d 20 64 61 79 20 2d 20 33 32 30 37 36 20  w = day - 32076 
c4a50 2b 0a 20 20 20 20 31 34 36 31 2a 28 79 65 61 72  +.    1461*(year
c4a60 20 2b 20 34 38 30 30 20 2b 20 28 6d 6f 6e 74 68   + 4800 + (month
c4a70 20 2d 20 31 34 29 2f 31 32 29 2f 34 20 2b 0a 20   - 14)/12)/4 +. 
c4a80 20 20 20 33 36 37 2a 28 6d 6f 6e 74 68 20 2d 20     367*(month - 
c4a90 32 20 2d 20 28 6d 6f 6e 74 68 20 2d 20 31 34 29  2 - (month - 14)
c4aa0 2f 31 32 2a 31 32 29 2f 31 32 20 2d 0a 20 20 20  /12*12)/12 -.   
c4ab0 20 33 2a 28 28 79 65 61 72 20 2b 20 34 39 30 30   3*((year + 4900
c4ac0 20 2b 20 28 6d 6f 6e 74 68 20 2d 20 31 34 29 2f   + (month - 14)/
c4ad0 31 32 29 2f 31 30 30 29 2f 34 3b 0a 0a 20 20 2f  12)/100)/4;..  /
c4ae0 2a 20 41 64 64 20 74 68 65 20 66 72 61 63 74 69  * Add the fracti
c4af0 6f 6e 61 6c 20 68 6f 75 72 73 2c 20 6d 69 6e 73  onal hours, mins
c4b00 20 61 6e 64 20 73 65 63 6f 6e 64 73 20 2a 2f 0a   and seconds */.
c4b10 20 20 6e 6f 77 20 2b 3d 20 28 68 6f 75 72 20 2b    now += (hour +
c4b20 20 31 32 2e 30 29 2f 32 34 2e 30 3b 0a 20 20 6e   12.0)/24.0;.  n
c4b30 6f 77 20 2b 3d 20 6d 69 6e 75 74 65 2f 31 34 34  ow += minute/144
c4b40 30 2e 30 3b 0a 20 20 6e 6f 77 20 2b 3d 20 73 65  0.0;.  now += se
c4b50 63 6f 6e 64 2f 38 36 34 30 30 2e 30 3b 0a 20 20  cond/86400.0;.  
c4b60 2a 70 72 4e 6f 77 20 3d 20 6e 6f 77 3b 0a 23 69  *prNow = now;.#i
c4b70 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
c4b80 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63  .  if( sqlite3_c
c4b90 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20  urrent_time ){. 
c4ba0 20 20 20 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69     *prNow = sqli
c4bb0 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65  te3_current_time
c4bc0 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35  /86400.0 + 24405
c4bd0 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  87.5;.  }.#endif
c4be0 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
c4bf0 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 47 65  static int os2Ge
c4c00 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74  tLastError(sqlit
c4c10 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e  e3_vfs *pVfs, in
c4c20 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42  t nBuf, char *zB
c4c30 75 66 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b  uf){.  return 0;
c4c40 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  .}../*.** Initia
c4c50 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69  lize and deiniti
c4c60 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74  alize the operat
c4c70 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72  ing system inter
c4c80 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  face..*/.SQLITE_
c4c90 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
c4ca0 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a 20  os_init(void){. 
c4cb0 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f   static sqlite3_
c4cc0 76 66 73 20 6f 73 32 56 66 73 20 3d 20 7b 0a 20  vfs os2Vfs = {. 
c4cd0 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20     1,           
c4ce0 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f        /* iVersio
c4cf0 6e 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28  n */.    sizeof(
c4d00 6f 73 32 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73  os2File),   /* s
c4d10 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 43  zOsFile */.    C
c4d20 43 48 4d 41 58 50 41 54 48 2c 20 20 20 20 20 20  CHMAXPATH,      
c4d30 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20    /* mxPathname 
c4d40 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20  */.    0,       
c4d50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65            /* pNe
c4d60 78 74 20 2a 2f 0a 20 20 20 20 22 6f 73 32 22 2c  xt */.    "os2",
c4d70 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c4d80 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20  zName */.    0, 
c4d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c4da0 2f 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 0a  /* pAppData */..
c4db0 20 20 20 20 6f 73 32 4f 70 65 6e 2c 20 20 20 20      os2Open,    
c4dc0 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20         /* xOpen 
c4dd0 2a 2f 0a 20 20 20 20 6f 73 32 44 65 6c 65 74 65  */.    os2Delete
c4de0 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65  ,         /* xDe
c4df0 6c 65 74 65 20 2a 2f 0a 20 20 20 20 6f 73 32 41  lete */.    os2A
c4e00 63 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 2f  ccess,         /
c4e10 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20  * xAccess */.   
c4e20 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65   os2FullPathname
c4e30 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68  ,   /* xFullPath
c4e40 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 44  name */.    os2D
c4e50 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 2f  lOpen,         /
c4e60 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20  * xDlOpen */.   
c4e70 20 6f 73 32 44 6c 45 72 72 6f 72 2c 20 20 20 20   os2DlError,    
c4e80 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20      /* xDlError 
c4e90 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 53 79 6d 2c  */.    os2DlSym,
c4ea0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c            /* xDl
c4eb0 53 79 6d 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c  Sym */.    os2Dl
c4ec0 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 2f 2a  Close,        /*
c4ed0 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 20 20   xDlClose */.   
c4ee0 20 6f 73 32 52 61 6e 64 6f 6d 6e 65 73 73 2c 20   os2Randomness, 
c4ef0 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65      /* xRandomne
c4f00 73 73 20 2a 2f 0a 20 20 20 20 6f 73 32 53 6c 65  ss */.    os2Sle
c4f10 65 70 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ep,          /* 
c4f20 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20 6f 73  xSleep */.    os
c4f30 32 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20  2CurrentTime,   
c4f40 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65   /* xCurrentTime
c4f50 20 2a 2f 0a 20 20 20 20 6f 73 32 47 65 74 4c 61   */.    os2GetLa
c4f60 73 74 45 72 72 6f 72 20 20 20 20 2f 2a 20 78 47  stError    /* xG
c4f70 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 0a 20  etLastError */. 
c4f80 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66   };.  sqlite3_vf
c4f90 73 5f 72 65 67 69 73 74 65 72 28 26 6f 73 32 56  s_register(&os2V
c4fa0 66 73 2c 20 31 29 3b 0a 20 20 69 6e 69 74 55 63  fs, 1);.  initUc
c4fb0 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a 20 20  onvObjects();.  
c4fc0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
c4fd0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ;.}.SQLITE_API i
c4fe0 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e  nt sqlite3_os_en
c4ff0 64 28 76 6f 69 64 29 7b 0a 20 20 66 72 65 65 55  d(void){.  freeU
c5000 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a 20  convObjects();. 
c5010 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
c5020 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  K;.}..#endif /* 
c5030 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 2a 2f  SQLITE_OS_OS2 */
c5040 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
c5050 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 6f 73 32 2e  * End of os_os2.
c5060 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
c5070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5090 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
c50a0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f  * Begin file os_
c50b0 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  unix.c *********
c50c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c50d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c50e0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79  /./*.** 2004 May
c50f0 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   22.**.** The au
c5100 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
c5110 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
c5120 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
c5130 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
c5140 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
c5150 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
c5160 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
c5170 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
c5180 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
c5190 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
c51a0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
c51b0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
c51c0 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
c51d0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
c51e0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
c51f0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
c5200 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
c5210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5250 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
c5260 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
c5270 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 73 70   code that is sp
c5280 65 63 69 66 69 63 20 74 6f 20 55 6e 69 78 20 73  ecific to Unix s
c5290 79 73 74 65 6d 73 2e 0a 2a 2a 0a 2a 2a 20 24 49  ystems..**.** $I
c52a0 64 3a 20 6f 73 5f 75 6e 69 78 2e 63 2c 76 20 31  d: os_unix.c,v 1
c52b0 2e 32 30 38 20 32 30 30 38 2f 31 31 2f 30 37 20  .208 2008/11/07 
c52c0 30 30 3a 30 36 3a 31 38 20 64 72 68 20 45 78 70  00:06:18 drh Exp
c52d0 20 24 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45   $.*/.#if SQLITE
c52e0 5f 4f 53 5f 55 4e 49 58 20 20 20 20 20 20 20 20  _OS_UNIX        
c52f0 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69        /* This fi
c5300 6c 65 20 69 73 20 75 73 65 64 20 6f 6e 20 75 6e  le is used on un
c5310 69 78 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a  ix only */../*.*
c5320 2a 20 49 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  * If SQLITE_ENAB
c5330 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
c5340 20 69 73 20 64 65 66 69 6e 65 64 20 61 6e 64 20   is defined and 
c5350 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
c5360 6e 20 73 65 76 65 72 61 6c 0a 2a 2a 20 61 6c 74  n several.** alt
c5370 65 72 6e 61 74 69 76 65 20 6c 6f 63 6b 69 6e 67  ernative locking
c5380 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
c5390 20 61 72 65 20 70 72 6f 76 69 64 65 64 3a 0a 2a   are provided:.*
c53a0 2a 0a 2a 2a 20 20 20 2a 20 50 4f 53 49 58 20 6c  *.**   * POSIX l
c53b0 6f 63 6b 69 6e 67 20 28 74 68 65 20 64 65 66 61  ocking (the defa
c53c0 75 6c 74 29 2c 0a 2a 2a 20 20 20 2a 20 4e 6f 20  ult),.**   * No 
c53d0 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 2a 20  locking,.**   * 
c53e0 44 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e 67  Dot-file locking
c53f0 2c 0a 2a 2a 20 20 20 2a 20 66 6c 6f 63 6b 28 29  ,.**   * flock()
c5400 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 2a   locking,.**   *
c5410 20 41 46 50 20 6c 6f 63 6b 69 6e 67 20 28 4f 53   AFP locking (OS
c5420 58 20 6f 6e 6c 79 29 2e 0a 2a 2a 0a 2a 2a 20 53  X only)..**.** S
c5430 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
c5440 4b 49 4e 47 5f 53 54 59 4c 45 20 6f 6e 6c 79 20  KING_STYLE only 
c5450 77 6f 72 6b 73 20 6f 6e 20 61 20 4d 61 63 2e 20  works on a Mac. 
c5460 49 74 20 69 73 20 74 75 72 6e 65 64 20 6f 6e 20  It is turned on 
c5470 62 79 0a 2a 2a 20 64 65 66 61 75 6c 74 20 6f 6e  by.** default on
c5480 20 61 20 4d 61 63 20 61 6e 64 20 64 69 73 61 62   a Mac and disab
c5490 6c 65 64 20 6f 6e 20 61 6c 6c 20 6f 74 68 65 72  led on all other
c54a0 20 70 6f 73 69 78 20 70 6c 61 74 66 6f 72 6d 73   posix platforms
c54b0 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65  ..*/.#if !define
c54c0 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
c54d0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 29 0a 23  LOCKING_STYLE).#
c54e0 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 44    if defined(__D
c54f0 41 52 57 49 4e 5f 5f 29 0a 23 20 20 20 20 64 65  ARWIN__).#    de
c5500 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42  fine SQLITE_ENAB
c5510 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
c5520 20 31 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 20   1.#  else.#    
c5530 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e  define SQLITE_EN
c5540 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
c5550 4c 45 20 30 0a 23 20 20 65 6e 64 69 66 0a 23 65  LE 0.#  endif.#e
c5560 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73  ndif../*.** Thes
c5570 65 20 23 64 65 66 69 6e 65 73 20 73 68 6f 75 6c  e #defines shoul
c5580 64 20 65 6e 61 62 6c 65 20 3e 32 47 42 20 66 69  d enable >2GB fi
c5590 6c 65 20 73 75 70 70 6f 72 74 20 6f 6e 20 50 6f  le support on Po
c55a0 73 69 78 20 69 66 20 74 68 65 0a 2a 2a 20 75 6e  six if the.** un
c55b0 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69  derlying operati
c55c0 6e 67 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72  ng system suppor
c55d0 74 73 20 69 74 2e 20 20 49 66 20 74 68 65 20 4f  ts it.  If the O
c55e0 53 20 6c 61 63 6b 73 0a 2a 2a 20 6c 61 72 67 65  S lacks.** large
c55f0 20 66 69 6c 65 20 73 75 70 70 6f 72 74 2c 20 74   file support, t
c5600 68 65 73 65 20 73 68 6f 75 6c 64 20 62 65 20 6e  hese should be n
c5610 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72  o-ops..**.** Lar
c5620 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20  ge file support 
c5630 63 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64 20  can be disabled 
c5640 75 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c 49  using the -DSQLI
c5650 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 73  TE_DISABLE_LFS s
c5660 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 20  witch.** on the 
c5670 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64  compiler command
c5680 20 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73 20   line.  This is 
c5690 6e 65 63 65 73 73 61 72 79 20 69 66 20 79 6f 75  necessary if you
c56a0 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a   are compiling.*
c56b0 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d 61  * on a recent ma
c56c0 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 48 61  chine (ex: RedHa
c56d0 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75 20 77  t 7.2) but you w
c56e0 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 74 6f  ant your code to
c56f0 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f   work.** on an o
c5700 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28 65 78  lder machine (ex
c5710 3a 20 52 65 64 48 61 74 20 36 2e 30 29 2e 20 20  : RedHat 6.0).  
c5720 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c 65 20 6f  If you compile o
c5730 6e 20 52 65 64 48 61 74 20 37 2e 32 0a 2a 2a 20  n RedHat 7.2.** 
c5740 77 69 74 68 6f 75 74 20 74 68 69 73 20 6f 70 74  without this opt
c5750 69 6f 6e 2c 20 4c 46 53 20 69 73 20 65 6e 61 62  ion, LFS is enab
c5760 6c 65 2e 20 20 42 75 74 20 4c 46 53 20 64 6f 65  le.  But LFS doe
c5770 73 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 20 74  s not exist in t
c5780 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20 69 6e 20  he kernel.** in 
c5790 52 65 64 48 61 74 20 36 2e 30 2c 20 73 6f 20 74  RedHat 6.0, so t
c57a0 68 65 20 63 6f 64 65 20 77 6f 6e 27 74 20 77 6f  he code won't wo
c57b0 72 6b 2e 20 20 48 65 6e 63 65 2c 20 66 6f 72 20  rk.  Hence, for 
c57c0 6d 61 78 69 6d 75 6d 20 62 69 6e 61 72 79 0a 2a  maximum binary.*
c57d0 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 20 79 6f  * portability yo
c57e0 75 20 73 68 6f 75 6c 64 20 6f 6d 69 74 20 4c 46  u should omit LF
c57f0 53 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  S..*/.#ifndef SQ
c5800 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53  LITE_DISABLE_LFS
c5810 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45  .# define _LARGE
c5820 5f 46 49 4c 45 20 20 20 20 20 20 20 31 0a 23 20  _FILE       1.# 
c5830 69 66 6e 64 65 66 20 5f 46 49 4c 45 5f 4f 46 46  ifndef _FILE_OFF
c5840 53 45 54 5f 42 49 54 53 0a 23 20 20 20 64 65 66  SET_BITS.#   def
c5850 69 6e 65 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54  ine _FILE_OFFSET
c5860 5f 42 49 54 53 20 36 34 0a 23 20 65 6e 64 69 66  _BITS 64.# endif
c5870 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45  .# define _LARGE
c5880 46 49 4c 45 5f 53 4f 55 52 43 45 20 31 0a 23 65  FILE_SOURCE 1.#e
c5890 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 73 74 61 6e  ndif../*.** stan
c58a0 64 61 72 64 20 69 6e 63 6c 75 64 65 20 66 69 6c  dard include fil
c58b0 65 73 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20  es..*/.#include 
c58c0 3c 73 79 73 2f 74 79 70 65 73 2e 68 3e 0a 23 69  <sys/types.h>.#i
c58d0 6e 63 6c 75 64 65 20 3c 73 79 73 2f 73 74 61 74  nclude <sys/stat
c58e0 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 66 63  .h>.#include <fc
c58f0 6e 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20  ntl.h>.#include 
c5900 3c 75 6e 69 73 74 64 2e 68 3e 0a 23 69 6e 63 6c  <unistd.h>.#incl
c5910 75 64 65 20 3c 73 79 73 2f 74 69 6d 65 2e 68 3e  ude <sys/time.h>
c5920 0a 23 69 6e 63 6c 75 64 65 20 3c 65 72 72 6e 6f  .#include <errno
c5930 2e 68 3e 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f  .h>..#if SQLITE_
c5940 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
c5950 54 59 4c 45 0a 23 69 6e 63 6c 75 64 65 20 3c 73  TYLE.#include <s
c5960 79 73 2f 69 6f 63 74 6c 2e 68 3e 0a 23 69 6e 63  ys/ioctl.h>.#inc
c5970 6c 75 64 65 20 3c 73 79 73 2f 70 61 72 61 6d 2e  lude <sys/param.
c5980 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73  h>.#include <sys
c5990 2f 6d 6f 75 6e 74 2e 68 3e 0a 23 65 6e 64 69 66  /mount.h>.#endif
c59a0 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   /* SQLITE_ENABL
c59b0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
c59c0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20  */../*.** If we 
c59d0 61 72 65 20 74 6f 20 62 65 20 74 68 72 65 61 64  are to be thread
c59e0 2d 73 61 66 65 2c 20 69 6e 63 6c 75 64 65 20 74  -safe, include t
c59f0 68 65 20 70 74 68 72 65 61 64 73 20 68 65 61 64  he pthreads head
c5a00 65 72 20 61 6e 64 20 64 65 66 69 6e 65 0a 2a 2a  er and define.**
c5a10 20 74 68 65 20 53 51 4c 49 54 45 5f 55 4e 49 58   the SQLITE_UNIX
c5a20 5f 54 48 52 45 41 44 53 20 6d 61 63 72 6f 2e 0a  _THREADS macro..
c5a30 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  */.#if SQLITE_TH
c5a40 52 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e  READSAFE.# defin
c5a50 65 20 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48  e SQLITE_UNIX_TH
c5a60 52 45 41 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a  READS 1.#endif..
c5a70 2f 2a 0a 2a 2a 20 44 65 66 61 75 6c 74 20 70 65  /*.** Default pe
c5a80 72 6d 69 73 73 69 6f 6e 73 20 77 68 65 6e 20 63  rmissions when c
c5a90 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20 66 69  reating a new fi
c5aa0 6c 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  le.*/.#ifndef SQ
c5ab0 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c  LITE_DEFAULT_FIL
c5ac0 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 0a 23 20  E_PERMISSIONS.# 
c5ad0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45  define SQLITE_DE
c5ae0 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49  FAULT_FILE_PERMI
c5af0 53 53 49 4f 4e 53 20 30 36 34 34 0a 23 65 6e 64  SSIONS 0644.#end
c5b00 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75  if../*.** Maximu
c5b10 6d 20 73 75 70 70 6f 72 74 65 64 20 70 61 74 68  m supported path
c5b20 2d 6c 65 6e 67 74 68 2e 0a 2a 2f 0a 23 64 65 66  -length..*/.#def
c5b30 69 6e 65 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45  ine MAX_PATHNAME
c5b40 20 35 31 32 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65   512.../*.** The
c5b50 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74   unixFile struct
c5b60 75 72 65 20 69 73 20 73 75 62 63 6c 61 73 73 20  ure is subclass 
c5b70 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  of sqlite3_file 
c5b80 73 70 65 63 69 66 69 63 20 66 6f 72 20 74 68 65  specific for the
c5b90 20 75 6e 69 78 0a 2a 2a 20 70 72 6f 74 61 62 69   unix.** protabi
c5ba0 6c 69 74 79 20 6c 61 79 65 72 2e 0a 2a 2f 0a 74  lity layer..*/.t
c5bb0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 75 6e  ypedef struct un
c5bc0 69 78 46 69 6c 65 20 75 6e 69 78 46 69 6c 65 3b  ixFile unixFile;
c5bd0 0a 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65  .struct unixFile
c5be0 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f   {.  sqlite3_io_
c5bf0 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70  methods const *p
c5c00 4d 65 74 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 61  Method;  /* Alwa
c5c10 79 73 20 74 68 65 20 66 69 72 73 74 20 65 6e 74  ys the first ent
c5c20 72 79 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  ry */.#ifdef SQL
c5c30 49 54 45 5f 54 45 53 54 0a 20 20 2f 2a 20 49 6e  ITE_TEST.  /* In
c5c40 20 74 65 73 74 20 6d 6f 64 65 2c 20 69 6e 63 72   test mode, incr
c5c50 65 61 73 65 20 74 68 65 20 73 69 7a 65 20 6f 66  ease the size of
c5c60 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
c5c70 61 20 62 69 74 20 73 6f 20 74 68 61 74 20 0a 20  a bit so that . 
c5c80 20 2a 2a 20 69 74 20 69 73 20 6c 61 72 67 65 72   ** it is larger
c5c90 20 74 68 61 6e 20 74 68 65 20 73 74 72 75 63 74   than the struct
c5ca0 20 43 72 61 73 68 46 69 6c 65 20 64 65 66 69 6e   CrashFile defin
c5cb0 65 64 20 69 6e 20 74 65 73 74 36 2e 63 2e 0a 20  ed in test6.c.. 
c5cc0 20 2a 2f 0a 20 20 63 68 61 72 20 61 50 61 64 64   */.  char aPadd
c5cd0 69 6e 67 5b 33 32 5d 3b 0a 23 65 6e 64 69 66 0a  ing[32];.#endif.
c5ce0 20 20 73 74 72 75 63 74 20 6f 70 65 6e 43 6e 74    struct openCnt
c5cf0 20 2a 70 4f 70 65 6e 3b 20 20 20 20 2f 2a 20 49   *pOpen;    /* I
c5d00 6e 66 6f 20 61 62 6f 75 74 20 61 6c 6c 20 6f 70  nfo about all op
c5d10 65 6e 20 66 64 27 73 20 6f 6e 20 74 68 69 73 20  en fd's on this 
c5d20 69 6e 6f 64 65 20 2a 2f 0a 20 20 73 74 72 75 63  inode */.  struc
c5d30 74 20 6c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63  t lockInfo *pLoc
c5d40 6b 3b 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f  k;   /* Info abo
c5d50 75 74 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 69 73  ut locks on this
c5d60 20 69 6e 6f 64 65 20 2a 2f 0a 23 69 66 20 53 51   inode */.#if SQ
c5d70 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
c5d80 49 4e 47 5f 53 54 59 4c 45 0a 20 20 76 6f 69 64  ING_STYLE.  void
c5d90 20 2a 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74   *lockingContext
c5da0 3b 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 69 6e 67  ;     /* Locking
c5db0 20 73 74 79 6c 65 20 73 70 65 63 69 66 69 63 20   style specific 
c5dc0 73 74 61 74 65 20 2a 2f 0a 23 65 6e 64 69 66 0a  state */.#endif.
c5dd0 20 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20    int h;        
c5de0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
c5df0 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
c5e00 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  or */.  unsigned
c5e10 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 3b 20   char locktype; 
c5e20 20 20 2f 2a 20 54 68 65 20 74 79 70 65 20 6f 66    /* The type of
c5e30 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68   lock held on th
c5e40 69 73 20 66 64 20 2a 2f 0a 20 20 69 6e 74 20 64  is fd */.  int d
c5e50 69 72 66 64 3b 20 20 20 20 20 20 20 20 20 20 20  irfd;           
c5e60 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73       /* File des
c5e70 63 72 69 70 74 6f 72 20 66 6f 72 20 74 68 65 20  criptor for the 
c5e80 64 69 72 65 63 74 6f 72 79 20 2a 2f 0a 23 69 66  directory */.#if
c5e90 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
c5ea0 46 45 0a 20 20 70 74 68 72 65 61 64 5f 74 20 74  FE.  pthread_t t
c5eb0 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  id;            /
c5ec0 2a 20 54 68 65 20 74 68 72 65 61 64 20 74 68 61  * The thread tha
c5ed0 74 20 22 6f 77 6e 73 22 20 74 68 69 73 20 75 6e  t "owns" this un
c5ee0 69 78 46 69 6c 65 20 2a 2f 0a 23 65 6e 64 69 66  ixFile */.#endif
c5ef0 0a 20 20 69 6e 74 20 6c 61 73 74 45 72 72 6e 6f  .  int lastErrno
c5f00 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
c5f10 54 68 65 20 75 6e 69 78 20 65 72 72 6e 6f 20 66  The unix errno f
c5f20 72 6f 6d 20 74 68 65 20 6c 61 73 74 20 49 2f 4f  rom the last I/O
c5f30 20 65 72 72 6f 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   error */.};../*
c5f40 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65  .** Include code
c5f50 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20   that is common 
c5f60 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69  to all os_*.c fi
c5f70 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  les.*/./********
c5f80 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f  ****** Include o
c5f90 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68  s_common.h in th
c5fa0 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 75  e middle of os_u
c5fb0 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nix.c **********
c5fc0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
c5fd0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
c5fe0 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a  e os_common.h **
c5ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6010 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
c6020 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54  4 May 22.**.** T
c6030 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
c6040 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
c6050 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
c6060 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
c6070 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
c6080 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
c6090 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
c60a0 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
c60b0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
c60c0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
c60d0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
c60e0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
c60f0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
c6100 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
c6110 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
c6120 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
c6130 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
c6140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
c6190 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
c61a0 74 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64  tains macros and
c61b0 20 61 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66   a little bit of
c61c0 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f   code that is co
c61d0 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f  mmon to.** all o
c61e0 66 20 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73  f the platform-s
c61f0 70 65 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f  pecific files (o
c6200 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69  s_*.c) and is #i
c6210 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f  ncluded into tho
c6220 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a  se.** files..**.
c6230 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 73 68 6f  ** This file sho
c6240 75 6c 64 20 62 65 20 23 69 6e 63 6c 75 64 65 64  uld be #included
c6250 20 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66   by the os_*.c f
c6260 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69  iles only.  It i
c6270 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72  s not a.** gener
c6280 61 6c 20 70 75 72 70 6f 73 65 20 68 65 61 64 65  al purpose heade
c6290 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49  r file..**.** $I
c62a0 64 3a 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76  d: os_common.h,v
c62b0 20 31 2e 33 37 20 32 30 30 38 2f 30 35 2f 32 39   1.37 2008/05/29
c62c0 20 32 30 3a 32 32 3a 33 37 20 73 68 61 6e 65 20   20:22:37 shane 
c62d0 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66  Exp $.*/.#ifndef
c62e0 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23   _OS_COMMON_H_.#
c62f0 64 65 66 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f  define _OS_COMMO
c6300 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c  N_H_../*.** At l
c6310 65 61 73 74 20 74 77 6f 20 62 75 67 73 20 68 61  east two bugs ha
c6320 76 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62 65  ve slipped in be
c6330 63 61 75 73 65 20 77 65 20 63 68 61 6e 67 65 64  cause we changed
c6340 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55   the MEMORY_DEBU
c6350 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51  G.** macro to SQ
c6360 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20 73  LITE_DEBUG and s
c6370 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69  ome older makefi
c6380 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65 74  les have not yet
c6390 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77 69   made the.** swi
c63a0 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  tch.  The follow
c63b0 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64 20  ing code should 
c63c0 63 61 74 63 68 20 74 68 69 73 20 70 72 6f 62 6c  catch this probl
c63d0 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69  em at compile-ti
c63e0 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45  me..*/.#ifdef ME
c63f0 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72 72  MORY_DEBUG.# err
c6400 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44  or "The MEMORY_D
c6410 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f 62  EBUG macro is ob
c6420 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51 4c  solete.  Use SQL
c6430 49 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65 61  ITE_DEBUG instea
c6440 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  d.".#endif.../*.
c6450 20 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c   * When testing,
c6460 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72   this global var
c6470 69 61 62 6c 65 20 73 74 6f 72 65 73 20 74 68 65  iable stores the
c6480 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65   location of the
c6490 0a 20 2a 20 70 65 6e 64 69 6e 67 2d 62 79 74 65  . * pending-byte
c64a0 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
c64b0 20 66 69 6c 65 2e 0a 20 2a 2f 0a 23 69 66 64 65   file.. */.#ifde
c64c0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
c64d0 4c 49 54 45 5f 41 50 49 20 75 6e 73 69 67 6e 65  LITE_API unsigne
c64e0 64 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 65  d int sqlite3_pe
c64f0 6e 64 69 6e 67 5f 62 79 74 65 20 3d 20 30 78 34  nding_byte = 0x4
c6500 30 30 30 30 30 30 30 3b 0a 23 65 6e 64 69 66 0a  0000000;.#endif.
c6510 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
c6520 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56  EBUG.SQLITE_PRIV
c6530 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
c6540 53 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 65 66  STrace = 0;.#def
c6550 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 20  ine OSTRACE1(X) 
c6560 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
c6570 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c  te3OSTrace ) sql
c6580 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
c6590 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  X).#define OSTRA
c65a0 43 45 32 28 58 2c 59 29 20 20 20 20 20 20 20 69  CE2(X,Y)       i
c65b0 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63  f( sqlite3OSTrac
c65c0 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  e ) sqlite3Debug
c65d0 50 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 66  Printf(X,Y).#def
c65e0 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59  ine OSTRACE3(X,Y
c65f0 2c 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c 69  ,Z)     if( sqli
c6600 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c  te3OSTrace ) sql
c6610 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
c6620 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f  X,Y,Z).#define O
c6630 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29  STRACE4(X,Y,Z,A)
c6640 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53     if( sqlite3OS
c6650 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44  Trace ) sqlite3D
c6660 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a  ebugPrintf(X,Y,Z
c6670 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,A).#define OSTR
c6680 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 20  ACE5(X,Y,Z,A,B) 
c6690 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61  if( sqlite3OSTra
c66a0 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75  ce ) sqlite3Debu
c66b0 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c  gPrintf(X,Y,Z,A,
c66c0 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  B).#define OSTRA
c66d0 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29  CE6(X,Y,Z,A,B,C)
c66e0 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65   \.    if(sqlite
c66f0 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65  3OSTrace) sqlite
c6700 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59  3DebugPrintf(X,Y
c6710 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e  ,Z,A,B,C).#defin
c6720 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a  e OSTRACE7(X,Y,Z
c6730 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20 20  ,A,B,C,D) \.    
c6740 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63  if(sqlite3OSTrac
c6750 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50  e) sqlite3DebugP
c6760 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c  rintf(X,Y,Z,A,B,
c6770 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66 69  C,D).#else.#defi
c6780 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a 23  ne OSTRACE1(X).#
c6790 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28  define OSTRACE2(
c67a0 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  X,Y).#define OST
c67b0 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64 65  RACE3(X,Y,Z).#de
c67c0 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c  fine OSTRACE4(X,
c67d0 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f  Y,Z,A).#define O
c67e0 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c  STRACE5(X,Y,Z,A,
c67f0 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  B).#define OSTRA
c6800 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29  CE6(X,Y,Z,A,B,C)
c6810 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
c6820 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29  7(X,Y,Z,A,B,C,D)
c6830 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d  .#endif../*.** M
c6840 61 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f 72  acros for perfor
c6850 6d 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20 20  mance tracing.  
c6860 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20  Normally turned 
c6870 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b 73  off.  Only works
c6880 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72 64  .** on i486 hard
c6890 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  ware..*/.#ifdef 
c68a0 53 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41 4e  SQLITE_PERFORMAN
c68b0 43 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a  CE_TRACE../* .**
c68c0 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69   hwtime.h contai
c68d0 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62  ns inline assemb
c68e0 6c 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d 70  ler code for imp
c68f0 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69  lementing .** hi
c6900 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 74  gh-performance t
c6910 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a  iming routines..
c6920 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
c6930 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69 6d  ** Include hwtim
c6940 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  e.h in the middl
c6950 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68  e of os_common.h
c6960 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
c6970 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
c6980 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 77  ** Begin file hw
c6990 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  time.h *********
c69a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c69b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c69c0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61  */./*.** 2008 Ma
c69d0 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  y 27.**.** The a
c69e0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
c69f0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
c6a00 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
c6a10 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
c6a20 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
c6a30 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
c6a40 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
c6a50 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
c6a60 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
c6a70 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
c6a80 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
c6a90 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
c6aa0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
c6ab0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
c6ac0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
c6ad0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
c6ae0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
c6af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6b30 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
c6b40 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
c6b50 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64  s inline asm cod
c6b60 65 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e 67  e for retrieving
c6b70 20 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e   "high-performan
c6b80 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20  ce".** counters 
c6b90 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43 50  for x86 class CP
c6ba0 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68  Us..**.** $Id: h
c6bb0 77 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32 30  wtime.h,v 1.3 20
c6bc0 30 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a 31  08/08/01 14:33:1
c6bd0 35 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f  5 shane Exp $.*/
c6be0 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d 45  .#ifndef _HWTIME
c6bf0 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 54  _H_.#define _HWT
c6c00 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68  IME_H_../*.** Th
c6c10 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74  e following rout
c6c20 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f  ine only works o
c6c30 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20  n pentium-class 
c6c40 28 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63 65  (or newer) proce
c6c50 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65  ssors..** It use
c6c60 73 20 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f  s the RDTSC opco
c6c70 64 65 20 74 6f 20 72 65 61 64 20 74 68 65 20 63  de to read the c
c6c80 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65  ycle count value
c6c90 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70   out of the.** p
c6ca0 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74  rocessor and ret
c6cb0 75 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e  urns that value.
c6cc0 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73    This can be us
c6cd0 65 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0a  ed for high-res.
c6ce0 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f  ** profiling..*/
c6cf0 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f  .#if (defined(__
c6d00 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e  GNUC__) || defin
c6d10 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26  ed(_MSC_VER)) &&
c6d20 20 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65   \.      (define
c6d30 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e  d(i386) || defin
c6d40 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20  ed(__i386__) || 
c6d50 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29  defined(_M_IX86)
c6d60 29 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65 64  )..  #if defined
c6d70 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f  (__GNUC__)..  __
c6d80 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f  inline__ sqlite_
c6d90 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77  uint64 sqlite3Hw
c6da0 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20  time(void){.    
c6db0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f   unsigned int lo
c6dc0 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d  , hi;.     __asm
c6dd0 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20  __ __volatile__ 
c6de0 28 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20  ("rdtsc" : "=a" 
c6df0 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29  (lo), "=d" (hi))
c6e00 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28 73  ;.     return (s
c6e10 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69 20  qlite_uint64)hi 
c6e20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a  << 32 | lo;.  }.
c6e30 0a 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65 64  .  #elif defined
c6e40 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f  (_MSC_VER)..  __
c6e50 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20  declspec(naked) 
c6e60 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f  __inline sqlite_
c6e70 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73  uint64 __cdecl s
c6e80 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69  qlite3Hwtime(voi
c6e90 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b  d){.     __asm {
c6ea0 0a 20 20 20 20 20 20 20 20 72 64 74 73 63 0a 20  .        rdtsc. 
c6eb0 20 20 20 20 20 20 20 72 65 74 20 20 20 20 20 20         ret      
c6ec0 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20   ; return value 
c6ed0 61 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20 20  at EDX:EAX.     
c6ee0 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a  }.  }..  #endif.
c6ef0 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28  .#elif (defined(
c6f00 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66  __GNUC__) && def
c6f10 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29  ined(__x86_64__)
c6f20 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20  )..  __inline__ 
c6f30 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71  sqlite_uint64 sq
c6f40 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64  lite3Hwtime(void
c6f50 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65  ){.      unsigne
c6f60 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20  d long val;.    
c6f70 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61    __asm__ __vola
c6f80 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20  tile__ ("rdtsc" 
c6f90 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20  : "=A" (val));. 
c6fa0 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b       return val;
c6fb0 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65  .  }. .#elif (de
c6fc0 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20  fined(__GNUC__) 
c6fd0 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63  && defined(__ppc
c6fe0 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65  __))..  __inline
c6ff0 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  __ sqlite_uint64
c7000 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76   sqlite3Hwtime(v
c7010 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69  oid){.      unsi
c7020 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72  gned long long r
c7030 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73  etval;.      uns
c7040 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b  igned long junk;
c7050 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f  .      __asm__ _
c7060 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e  _volatile__ ("\n
c7070 5c 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20 20  \.          1:  
c7080 20 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c 6e      mftbu   %1\n
c7090 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
c70a0 20 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30 5c      mftb    %L0\
c70b0 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n\.             
c70c0 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 30 5c       mftbu   %0\
c70d0 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n\.             
c70e0 20 20 20 20 20 63 6d 70 77 20 20 20 20 25 30 2c       cmpw    %0,
c70f0 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20  %1\n\.          
c7100 20 20 20 20 20 20 20 20 62 6e 65 20 20 20 20 20          bne     
c7110 31 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  1b".            
c7120 20 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72 65        : "=r" (re
c7130 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e  tval), "=r" (jun
c7140 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  k));.      retur
c7150 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23  n retval;.  }..#
c7160 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e  else..  #error N
c7170 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  eed implementati
c7180 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74  on of sqlite3Hwt
c7190 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70  ime() for your p
c71a0 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20  latform...  /*. 
c71b0 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77   ** To compile w
c71c0 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74  ithout implement
c71d0 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d  ing sqlite3Hwtim
c71e0 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61  e() for your pla
c71f0 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20  tform,.  ** you 
c7200 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 61  can remove the a
c7210 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64 20  bove #error and 
c7220 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  use the followin
c7230 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e 63  g.  ** stub func
c7240 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20  tion.  You will 
c7250 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70 70  lose timing supp
c7260 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a  ort for many.  *
c7270 2a 20 6f 66 20 74 68 65 20 64 65 62 75 67 67 69  * of the debuggi
c7280 6e 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20 75  ng and testing u
c7290 74 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69 74  tilities, but it
c72a0 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20   should at.  ** 
c72b0 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e  least compile an
c72c0 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49  d run..  */.SQLI
c72d0 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c  TE_PRIVATE   sql
c72e0 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74  ite_uint64 sqlit
c72f0 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 20  e3Hwtime(void){ 
c7300 72 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65 5f  return ((sqlite_
c7310 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65  uint64)0); }..#e
c7320 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  ndif..#endif /* 
c7330 21 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45  !defined(_HWTIME
c7340 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  _H_) */../******
c7350 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
c7360 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a  hwtime.h *******
c7370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7390 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
c73a0 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
c73b0 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
c73c0 74 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d  t off in os_comm
c73d0 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  on.h ***********
c73e0 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63  *******/..static
c73f0 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67   sqlite_uint64 g
c7400 5f 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20 73  _start;.static s
c7410 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 65  qlite_uint64 g_e
c7420 6c 61 70 73 65 64 3b 0a 23 64 65 66 69 6e 65 20  lapsed;.#define 
c7430 54 49 4d 45 52 5f 53 54 41 52 54 20 20 20 20 20  TIMER_START     
c7440 20 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 74 65    g_start=sqlite
c7450 33 48 77 74 69 6d 65 28 29 0a 23 64 65 66 69 6e  3Hwtime().#defin
c7460 65 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 20 20  e TIMER_END     
c7470 20 20 20 20 67 5f 65 6c 61 70 73 65 64 3d 73 71      g_elapsed=sq
c7480 6c 69 74 65 33 48 77 74 69 6d 65 28 29 2d 67 5f  lite3Hwtime()-g_
c7490 73 74 61 72 74 0a 23 64 65 66 69 6e 65 20 54 49  start.#define TI
c74a0 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20  MER_ELAPSED     
c74b0 67 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73 65 0a  g_elapsed.#else.
c74c0 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54  #define TIMER_ST
c74d0 41 52 54 0a 23 64 65 66 69 6e 65 20 54 49 4d 45  ART.#define TIME
c74e0 52 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 54 49  R_END.#define TI
c74f0 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20  MER_ELAPSED     
c7500 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29  ((sqlite_uint64)
c7510 30 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  0).#endif../*.**
c7520 20 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77   If we compile w
c7530 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 54  ith the SQLITE_T
c7540 45 53 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 74  EST macro set, t
c7550 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  hen the followin
c7560 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f  g block.** of co
c7570 64 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73 20  de will give us 
c7580 74 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20 73  the ability to s
c7590 69 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20 49  imulate a disk I
c75a0 2f 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73 0a  /O error.  This.
c75b0 2a 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20 74  ** is used for t
c75c0 65 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 72  esting the I/O r
c75d0 65 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a  ecovery logic..*
c75e0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
c75f0 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20  TEST.SQLITE_API 
c7600 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  int sqlite3_io_e
c7610 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20 20  rror_hit = 0;   
c7620 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61           /* Tota
c7630 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20  l number of I/O 
c7640 45 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45  Errors */.SQLITE
c7650 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
c7660 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69  _io_error_hardhi
c7670 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a  t = 0;        /*
c7680 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 62   Number of non-b
c7690 65 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f 0a  enign errors */.
c76a0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
c76b0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
c76c0 70 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20 20  pending = 0;    
c76d0 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f 77      /* Count dow
c76e0 6e 20 74 6f 20 66 69 72 73 74 20 49 2f 4f 20 65  n to first I/O e
c76f0 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 41  rror */.SQLITE_A
c7700 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  PI int sqlite3_i
c7710 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20  o_error_persist 
c7720 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  = 0;        /* T
c7730 72 75 65 20 69 66 20 49 2f 4f 20 65 72 72 6f 72  rue if I/O error
c7740 73 20 70 65 72 73 69 73 74 20 2a 2f 0a 53 51 4c  s persist */.SQL
c7750 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
c7760 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e  te3_io_error_ben
c7770 69 67 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ign = 0;        
c7780 20 2f 2a 20 54 72 75 65 20 69 66 20 65 72 72 6f   /* True if erro
c7790 72 73 20 61 72 65 20 62 65 6e 69 67 6e 20 2a 2f  rs are benign */
c77a0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
c77b0 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c  sqlite3_diskfull
c77c0 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51  _pending = 0;.SQ
c77d0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
c77e0 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20  ite3_diskfull = 
c77f0 30 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c  0;.#define Simul
c7800 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e  ateIOErrorBenign
c7810 28 58 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  (X) sqlite3_io_e
c7820 72 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29 0a  rror_benign=(X).
c7830 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65  #define Simulate
c7840 49 4f 45 72 72 6f 72 28 43 4f 44 45 29 20 20 5c  IOError(CODE)  \
c7850 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65 33 5f  .  if( (sqlite3_
c7860 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74  io_error_persist
c7870 20 26 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65   && sqlite3_io_e
c7880 72 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20 20  rror_hit) \.    
c7890 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f     || sqlite3_io
c78a0 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d  _error_pending--
c78b0 20 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20 20 20   == 1 )  \.     
c78c0 20 20 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c           { local
c78d0 5f 69 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b 20  _ioerr(); CODE; 
c78e0 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f  }.static void lo
c78f0 63 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49  cal_ioerr(){.  I
c7900 4f 54 52 41 43 45 28 28 22 49 4f 45 52 52 5c 6e  OTRACE(("IOERR\n
c7910 22 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69  "));.  sqlite3_i
c7920 6f 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20  o_error_hit++;. 
c7930 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f 69 6f   if( !sqlite3_io
c7940 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20  _error_benign ) 
c7950 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
c7960 5f 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64  _hardhit++;.}.#d
c7970 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69  efine SimulateDi
c7980 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45  skfullError(CODE
c7990 29 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69 74  ) \.   if( sqlit
c79a0 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64  e3_diskfull_pend
c79b0 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69 66  ing ){ \.     if
c79c0 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75  ( sqlite3_diskfu
c79d0 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20  ll_pending == 1 
c79e0 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61  ){ \.       loca
c79f0 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20  l_ioerr(); \.   
c7a00 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b      sqlite3_disk
c7a10 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20  full = 1; \.    
c7a20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72     sqlite3_io_er
c7a30 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20  ror_hit = 1; \. 
c7a40 20 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20        CODE; \.  
c7a50 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20     }else{ \.    
c7a60 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66     sqlite3_diskf
c7a70 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c  ull_pending--; \
c7a80 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23  .     } \.   }.#
c7a90 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69 6d  else.#define Sim
c7aa0 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69  ulateIOErrorBeni
c7ab0 67 6e 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69  gn(X).#define Si
c7ac0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 29  mulateIOError(A)
c7ad0 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74  .#define Simulat
c7ae0 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41  eDiskfullError(A
c7af0 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
c7b00 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65  When testing, ke
c7b10 65 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68  ep a count of th
c7b20 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e  e number of open
c7b30 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65   files..*/.#ifde
c7b40 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
c7b50 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
c7b60 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63  ite3_open_file_c
c7b70 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e  ount = 0;.#defin
c7b80 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29  e OpenCounter(X)
c7b90 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66    sqlite3_open_f
c7ba0 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23  ile_count+=(X).#
c7bb0 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 70 65  else.#define Ope
c7bc0 6e 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e 64  nCounter(X).#end
c7bd0 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64  if..#endif /* !d
c7be0 65 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f  efined(_OS_COMMO
c7bf0 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  N_H_) */../*****
c7c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
c7c10 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a   os_common.h ***
c7c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7c40 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
c7c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
c7c60 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
c7c70 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 75 6e 69  ft off in os_uni
c7c80 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  x.c ************
c7c90 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ********/../*.**
c7ca0 20 44 65 66 69 6e 65 20 76 61 72 69 6f 75 73 20   Define various 
c7cb0 6d 61 63 72 6f 73 20 74 68 61 74 20 61 72 65 20  macros that are 
c7cc0 6d 69 73 73 69 6e 67 20 66 72 6f 6d 20 73 6f 6d  missing from som
c7cd0 65 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69  e systems..*/.#i
c7ce0 66 6e 64 65 66 20 4f 5f 4c 41 52 47 45 46 49 4c  fndef O_LARGEFIL
c7cf0 45 0a 23 20 64 65 66 69 6e 65 20 4f 5f 4c 41 52  E.# define O_LAR
c7d00 47 45 46 49 4c 45 20 30 0a 23 65 6e 64 69 66 0a  GEFILE 0.#endif.
c7d10 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 49  #ifdef SQLITE_DI
c7d20 53 41 42 4c 45 5f 4c 46 53 0a 23 20 75 6e 64 65  SABLE_LFS.# unde
c7d30 66 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20  f O_LARGEFILE.# 
c7d40 64 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49  define O_LARGEFI
c7d50 4c 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e  LE 0.#endif.#ifn
c7d60 64 65 66 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23  def O_NOFOLLOW.#
c7d70 20 64 65 66 69 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c   define O_NOFOLL
c7d80 4f 57 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e  OW 0.#endif.#ifn
c7d90 64 65 66 20 4f 5f 42 49 4e 41 52 59 0a 23 20 64  def O_BINARY.# d
c7da0 65 66 69 6e 65 20 4f 5f 42 49 4e 41 52 59 20 30  efine O_BINARY 0
c7db0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
c7dc0 68 65 20 44 4a 47 50 50 20 63 6f 6d 70 69 6c 65  he DJGPP compile
c7dd0 72 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 6c 6f  r environment lo
c7de0 6f 6b 73 20 6d 6f 73 74 6c 79 20 6c 69 6b 65 20  oks mostly like 
c7df0 55 6e 69 78 2c 20 62 75 74 20 69 74 0a 2a 2a 20  Unix, but it.** 
c7e00 6c 61 63 6b 73 20 74 68 65 20 66 63 6e 74 6c 28  lacks the fcntl(
c7e10 29 20 73 79 73 74 65 6d 20 63 61 6c 6c 2e 20 20  ) system call.  
c7e20 53 6f 20 72 65 64 65 66 69 6e 65 20 66 63 6e 74  So redefine fcnt
c7e30 6c 28 29 20 74 6f 20 62 65 20 73 6f 6d 65 74 68  l() to be someth
c7e40 69 6e 67 0a 2a 2a 20 74 68 61 74 20 61 6c 77 61  ing.** that alwa
c7e50 79 73 20 73 75 63 63 65 65 64 73 2e 20 20 54 68  ys succeeds.  Th
c7e60 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 6c 6f  is means that lo
c7e70 63 6b 69 6e 67 20 64 6f 65 73 20 6e 6f 74 20 6f  cking does not o
c7e80 63 63 75 72 20 75 6e 64 65 72 0a 2a 2a 20 44 4a  ccur under.** DJ
c7e90 47 50 50 2e 20 20 42 75 74 20 69 74 20 69 73 20  GPP.  But it is 
c7ea0 44 4f 53 20 2d 20 77 68 61 74 20 64 69 64 20 79  DOS - what did y
c7eb0 6f 75 20 65 78 70 65 63 74 3f 0a 2a 2f 0a 23 69  ou expect?.*/.#i
c7ec0 66 64 65 66 20 5f 5f 44 4a 47 50 50 5f 5f 0a 23  fdef __DJGPP__.#
c7ed0 20 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 41 2c   define fcntl(A,
c7ee0 42 2c 43 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f  B,C) 0.#endif../
c7ef0 2a 0a 2a 2a 20 54 68 65 20 74 68 72 65 61 64 69  *.** The threadi
c7f00 64 20 6d 61 63 72 6f 20 72 65 73 6f 6c 76 65 73  d macro resolves
c7f10 20 74 6f 20 74 68 65 20 74 68 72 65 61 64 2d 69   to the thread-i
c7f20 64 20 6f 72 20 74 6f 20 30 2e 20 20 55 73 65 64  d or to 0.  Used
c7f30 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20   for.** testing 
c7f40 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e  and debugging on
c7f50 6c 79 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54  ly..*/.#if SQLIT
c7f60 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23 64 65  E_THREADSAFE.#de
c7f70 66 69 6e 65 20 74 68 72 65 61 64 69 64 20 70 74  fine threadid pt
c7f80 68 72 65 61 64 5f 73 65 6c 66 28 29 0a 23 65 6c  hread_self().#el
c7f90 73 65 0a 23 64 65 66 69 6e 65 20 74 68 72 65 61  se.#define threa
c7fa0 64 69 64 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a  did 0.#endif../*
c7fb0 0a 2a 2a 20 53 65 74 20 6f 72 20 63 68 65 63 6b  .** Set or check
c7fc0 20 74 68 65 20 75 6e 69 78 46 69 6c 65 2e 74 69   the unixFile.ti
c7fd0 64 20 66 69 65 6c 64 2e 20 20 54 68 69 73 20 66  d field.  This f
c7fe0 69 65 6c 64 20 69 73 20 73 65 74 20 77 68 65 6e  ield is set when
c7ff0 20 61 6e 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20   an unixFile.** 
c8000 69 73 20 66 69 72 73 74 20 6f 70 65 6e 65 64 2e  is first opened.
c8010 20 20 41 6c 6c 20 73 75 62 73 65 71 75 65 6e 74    All subsequent
c8020 20 75 73 65 73 20 6f 66 20 74 68 65 20 75 6e 69   uses of the uni
c8030 78 46 69 6c 65 20 76 65 72 69 66 79 20 74 68 61  xFile verify tha
c8040 74 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 74 68  t the.** same th
c8050 72 65 61 64 20 69 73 20 6f 70 65 72 61 74 69 6e  read is operatin
c8060 67 20 6f 6e 20 74 68 65 20 75 6e 69 78 46 69 6c  g on the unixFil
c8070 65 2e 20 20 53 6f 6d 65 20 6f 70 65 72 61 74 69  e.  Some operati
c8080 6e 67 20 73 79 73 74 65 6d 73 20 64 6f 0a 2a 2a  ng systems do.**
c8090 20 6e 6f 74 20 61 6c 6c 6f 77 20 6c 6f 63 6b 73   not allow locks
c80a0 20 74 6f 20 62 65 20 6f 76 65 72 72 69 64 64 65   to be overridde
c80b0 6e 20 62 79 20 6f 74 68 65 72 20 74 68 72 65 61  n by other threa
c80c0 64 73 20 61 6e 64 20 74 68 61 74 20 72 65 73 74  ds and that rest
c80d0 72 69 63 74 69 6f 6e 0a 2a 2a 20 6d 65 61 6e 73  riction.** means
c80e0 20 74 68 61 74 20 73 71 6c 69 74 65 33 2a 20 64   that sqlite3* d
c80f0 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 73 20  atabase handles 
c8100 63 61 6e 6e 6f 74 20 62 65 20 6d 6f 76 65 64 20  cannot be moved 
c8110 66 72 6f 6d 20 6f 6e 65 20 74 68 72 65 61 64 0a  from one thread.
c8120 2a 2a 20 74 6f 20 61 6e 6f 74 68 65 72 2e 20 20  ** to another.  
c8130 54 68 69 73 20 6c 6f 67 69 63 20 6d 61 6b 65 73  This logic makes
c8140 20 73 75 72 65 20 61 20 75 73 65 72 20 64 6f 65   sure a user doe
c8150 73 20 6e 6f 74 20 74 72 79 20 74 6f 20 64 6f 20  s not try to do 
c8160 74 68 61 74 0a 2a 2a 20 62 79 20 6d 69 73 74 61  that.** by mista
c8170 6b 65 2e 0a 2a 2a 0a 2a 2a 20 56 65 72 73 69 6f  ke..**.** Versio
c8180 6e 20 33 2e 33 2e 31 20 28 32 30 30 36 2d 30 31  n 3.3.1 (2006-01
c8190 2d 31 35 29 3a 20 20 75 6e 69 78 46 69 6c 65 20  -15):  unixFile 
c81a0 63 61 6e 20 62 65 20 6d 6f 76 65 64 20 66 72 6f  can be moved fro
c81b0 6d 20 6f 6e 65 20 74 68 72 65 61 64 20 74 6f 0a  m one thread to.
c81c0 2a 2a 20 61 6e 6f 74 68 65 72 20 61 73 20 6c 6f  ** another as lo
c81d0 6e 67 20 61 73 20 77 65 20 61 72 65 20 72 75 6e  ng as we are run
c81e0 6e 69 6e 67 20 6f 6e 20 61 20 73 79 73 74 65 6d  ning on a system
c81f0 20 74 68 61 74 20 73 75 70 70 6f 72 74 73 20 74   that supports t
c8200 68 72 65 61 64 73 0a 2a 2a 20 6f 76 65 72 72 69  hreads.** overri
c8210 64 69 6e 67 20 65 61 63 68 20 6f 74 68 65 72 73  ding each others
c8220 20 6c 6f 63 6b 73 20 28 77 68 69 63 68 20 6e 6f   locks (which no
c8230 77 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f  w the most commo
c8240 6e 20 62 65 68 61 76 69 6f 72 29 0a 2a 2a 20 6f  n behavior).** o
c8250 72 20 69 66 20 6e 6f 20 6c 6f 63 6b 73 20 61 72  r if no locks ar
c8260 65 20 68 65 6c 64 2e 20 20 42 75 74 20 74 68 65  e held.  But the
c8270 20 75 6e 69 78 46 69 6c 65 2e 70 4c 6f 63 6b 20   unixFile.pLock 
c8280 66 69 65 6c 64 20 6e 65 65 64 73 20 74 6f 20 62  field needs to b
c8290 65 0a 2a 2a 20 72 65 63 6f 6d 70 75 74 65 64 20  e.** recomputed 
c82a0 62 65 63 61 75 73 65 20 69 74 73 20 6b 65 79 20  because its key 
c82b0 69 6e 63 6c 75 64 65 73 20 74 68 65 20 74 68 72  includes the thr
c82c0 65 61 64 2d 69 64 2e 20 20 53 65 65 20 74 68 65  ead-id.  See the
c82d0 20 0a 2a 2a 20 74 72 61 6e 73 66 65 72 4f 77 6e   .** transferOwn
c82e0 65 72 73 68 69 70 28 29 20 66 75 6e 63 74 69 6f  ership() functio
c82f0 6e 20 62 65 6c 6f 77 20 66 6f 72 20 61 64 64 69  n below for addi
c8300 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69  tional informati
c8310 6f 6e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  on.*/.#if SQLITE
c8320 5f 54 48 52 45 41 44 53 41 46 45 0a 23 20 64 65  _THREADSAFE.# de
c8330 66 69 6e 65 20 53 45 54 5f 54 48 52 45 41 44 49  fine SET_THREADI
c8340 44 28 58 29 20 20 20 28 58 29 2d 3e 74 69 64 20  D(X)   (X)->tid 
c8350 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29  = pthread_self()
c8360 0a 23 20 64 65 66 69 6e 65 20 43 48 45 43 4b 5f  .# define CHECK_
c8370 54 48 52 45 41 44 49 44 28 58 29 20 28 74 68 72  THREADID(X) (thr
c8380 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68  eadsOverrideEach
c8390 4f 74 68 65 72 73 4c 6f 63 6b 73 3d 3d 30 20 26  OthersLocks==0 &
c83a0 26 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  & \.            
c83b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c83c0 21 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 28  !pthread_equal((
c83d0 58 29 2d 3e 74 69 64 2c 20 70 74 68 72 65 61 64  X)->tid, pthread
c83e0 5f 73 65 6c 66 28 29 29 29 0a 23 65 6c 73 65 0a  _self())).#else.
c83f0 23 20 64 65 66 69 6e 65 20 53 45 54 5f 54 48 52  # define SET_THR
c8400 45 41 44 49 44 28 58 29 0a 23 20 64 65 66 69 6e  EADID(X).# defin
c8410 65 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44  e CHECK_THREADID
c8420 28 58 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a  (X) 0.#endif../*
c8430 0a 2a 2a 20 48 65 72 65 20 69 73 20 74 68 65 20  .** Here is the 
c8440 64 69 72 74 20 6f 6e 20 50 4f 53 49 58 20 61 64  dirt on POSIX ad
c8450 76 69 73 6f 72 79 20 6c 6f 63 6b 73 3a 20 20 41  visory locks:  A
c8460 4e 53 49 20 53 54 44 20 31 30 30 33 2e 31 20 28  NSI STD 1003.1 (
c8470 31 39 39 36 29 0a 2a 2a 20 73 65 63 74 69 6f 6e  1996).** section
c8480 20 36 2e 35 2e 32 2e 32 20 6c 69 6e 65 73 20 34   6.5.2.2 lines 4
c8490 38 33 20 74 68 72 6f 75 67 68 20 34 39 30 20 73  83 through 490 s
c84a0 70 65 63 69 66 79 20 74 68 61 74 20 77 68 65 6e  pecify that when
c84b0 20 61 20 70 72 6f 63 65 73 73 0a 2a 2a 20 73 65   a process.** se
c84c0 74 73 20 6f 72 20 63 6c 65 61 72 73 20 61 20 6c  ts or clears a l
c84d0 6f 63 6b 2c 20 74 68 61 74 20 6f 70 65 72 61 74  ock, that operat
c84e0 69 6f 6e 20 6f 76 65 72 72 69 64 65 73 20 61 6e  ion overrides an
c84f0 79 20 70 72 69 6f 72 20 6c 6f 63 6b 73 20 73 65  y prior locks se
c8500 74 0a 2a 2a 20 62 79 20 74 68 65 20 73 61 6d 65  t.** by the same
c8510 20 70 72 6f 63 65 73 73 2e 20 20 49 74 20 64 6f   process.  It do
c8520 65 73 20 6e 6f 74 20 65 78 70 6c 69 63 69 74 6c  es not explicitl
c8530 79 20 73 61 79 20 73 6f 2c 20 62 75 74 20 74 68  y say so, but th
c8540 69 73 20 69 6d 70 6c 69 65 73 0a 2a 2a 20 74 68  is implies.** th
c8550 61 74 20 69 74 20 6f 76 65 72 72 69 64 65 73 20  at it overrides 
c8560 6c 6f 63 6b 73 20 73 65 74 20 62 79 20 74 68 65  locks set by the
c8570 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20 75 73   same process us
c8580 69 6e 67 20 61 20 64 69 66 66 65 72 65 6e 74 0a  ing a different.
c8590 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ** file descript
c85a0 6f 72 2e 20 20 43 6f 6e 73 69 64 65 72 20 74 68  or.  Consider th
c85b0 69 73 20 74 65 73 74 20 63 61 73 65 3a 0a 2a 2a  is test case:.**
c85c0 20 20 20 20 20 20 20 69 6e 74 20 66 64 32 20 3d         int fd2 =
c85d0 20 6f 70 65 6e 28 22 2e 2f 66 69 6c 65 32 22 2c   open("./file2",
c85e0 20 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 41 54 2c   O_RDWR|O_CREAT,
c85f0 20 30 36 34 34 29 3b 0a 2a 2a 0a 2a 2a 20 53 75   0644);.**.** Su
c8600 70 70 6f 73 65 20 2e 2f 66 69 6c 65 31 20 61 6e  ppose ./file1 an
c8610 64 20 2e 2f 66 69 6c 65 32 20 61 72 65 20 72 65  d ./file2 are re
c8620 61 6c 6c 79 20 74 68 65 20 73 61 6d 65 20 66 69  ally the same fi
c8630 6c 65 20 28 62 65 63 61 75 73 65 0a 2a 2a 20 6f  le (because.** o
c8640 6e 65 20 69 73 20 61 20 68 61 72 64 20 6f 72 20  ne is a hard or 
c8650 73 79 6d 62 6f 6c 69 63 20 6c 69 6e 6b 20 74 6f  symbolic link to
c8660 20 74 68 65 20 6f 74 68 65 72 29 20 74 68 65 6e   the other) then
c8670 20 69 66 20 79 6f 75 20 73 65 74 0a 2a 2a 20 61   if you set.** a
c8680 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  n exclusive lock
c8690 20 6f 6e 20 66 64 31 2c 20 74 68 65 6e 20 74 72   on fd1, then tr
c86a0 79 20 74 6f 20 67 65 74 20 61 6e 20 65 78 63 6c  y to get an excl
c86b0 75 73 69 76 65 20 6c 6f 63 6b 0a 2a 2a 20 6f 6e  usive lock.** on
c86c0 20 66 64 32 2c 20 69 74 20 77 6f 72 6b 73 2e 20   fd2, it works. 
c86d0 20 49 20 77 6f 75 6c 64 20 68 61 76 65 20 65 78   I would have ex
c86e0 70 65 63 74 65 64 20 74 68 65 20 73 65 63 6f 6e  pected the secon
c86f0 64 20 6c 6f 63 6b 20 74 6f 0a 2a 2a 20 66 61 69  d lock to.** fai
c8700 6c 20 73 69 6e 63 65 20 74 68 65 72 65 20 77 61  l since there wa
c8710 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b  s already a lock
c8720 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 64 75 65   on the file due
c8730 20 74 6f 20 66 64 31 2e 0a 2a 2a 20 42 75 74 20   to fd1..** But 
c8740 6e 6f 74 20 73 6f 2e 20 20 53 69 6e 63 65 20 62  not so.  Since b
c8750 6f 74 68 20 6c 6f 63 6b 73 20 63 61 6d 65 20 66  oth locks came f
c8760 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 70 72 6f  rom the same pro
c8770 63 65 73 73 2c 20 74 68 65 0a 2a 2a 20 73 65 63  cess, the.** sec
c8780 6f 6e 64 20 6f 76 65 72 72 69 64 65 73 20 74 68  ond overrides th
c8790 65 20 66 69 72 73 74 2c 20 65 76 65 6e 20 74 68  e first, even th
c87a0 6f 75 67 68 20 74 68 65 79 20 77 65 72 65 20 6f  ough they were o
c87b0 6e 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66  n different.** f
c87c0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20  ile descriptors 
c87d0 6f 70 65 6e 65 64 20 6f 6e 20 64 69 66 66 65 72  opened on differ
c87e0 65 6e 74 20 66 69 6c 65 20 6e 61 6d 65 73 2e 0a  ent file names..
c87f0 2a 2a 0a 2a 2a 20 42 75 6d 6d 65 72 2e 20 20 49  **.** Bummer.  I
c8800 66 20 79 6f 75 20 61 73 6b 20 6d 65 2c 20 74 68  f you ask me, th
c8810 69 73 20 69 73 20 62 72 6f 6b 65 6e 2e 20 20 42  is is broken.  B
c8820 61 64 6c 79 20 62 72 6f 6b 65 6e 2e 20 20 49 74  adly broken.  It
c8830 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 77   means.** that w
c8840 65 20 63 61 6e 6e 6f 74 20 75 73 65 20 50 4f 53  e cannot use POS
c8850 49 58 20 6c 6f 63 6b 73 20 74 6f 20 73 79 6e 63  IX locks to sync
c8860 68 72 6f 6e 69 7a 65 20 66 69 6c 65 20 61 63 63  hronize file acc
c8870 65 73 73 20 61 6d 6f 6e 67 0a 2a 2a 20 63 6f 6d  ess among.** com
c8880 70 65 74 69 6e 67 20 74 68 72 65 61 64 73 20 6f  peting threads o
c8890 66 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65  f the same proce
c88a0 73 73 2e 20 20 50 4f 53 49 58 20 6c 6f 63 6b 73  ss.  POSIX locks
c88b0 20 77 69 6c 6c 20 77 6f 72 6b 20 66 69 6e 65 0a   will work fine.
c88c0 2a 2a 20 74 6f 20 73 79 6e 63 68 72 6f 6e 69 7a  ** to synchroniz
c88d0 65 20 61 63 63 65 73 73 20 66 6f 72 20 74 68 72  e access for thr
c88e0 65 61 64 73 20 69 6e 20 73 65 70 61 72 61 74 65  eads in separate
c88f0 20 70 72 6f 63 65 73 73 65 73 2c 20 62 75 74 20   processes, but 
c8900 6e 6f 74 0a 2a 2a 20 74 68 72 65 61 64 73 20 77  not.** threads w
c8910 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 20 70  ithin the same p
c8920 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f  rocess..**.** To
c8930 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 65   work around the
c8940 20 70 72 6f 62 6c 65 6d 2c 20 53 51 4c 69 74 65   problem, SQLite
c8950 20 68 61 73 20 74 6f 20 6d 61 6e 61 67 65 20 66   has to manage f
c8960 69 6c 65 20 6c 6f 63 6b 73 20 69 6e 74 65 72 6e  ile locks intern
c8970 61 6c 6c 79 0a 2a 2a 20 6f 6e 20 69 74 73 20 6f  ally.** on its o
c8980 77 6e 2e 20 20 57 68 65 6e 65 76 65 72 20 61 20  wn.  Whenever a 
c8990 6e 65 77 20 64 61 74 61 62 61 73 65 20 69 73 20  new database is 
c89a0 6f 70 65 6e 65 64 2c 20 77 65 20 68 61 76 65 20  opened, we have 
c89b0 74 6f 20 66 69 6e 64 20 74 68 65 0a 2a 2a 20 73  to find the.** s
c89c0 70 65 63 69 66 69 63 20 69 6e 6f 64 65 20 6f 66  pecific inode of
c89d0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
c89e0 6c 65 20 28 74 68 65 20 69 6e 6f 64 65 20 69 73  le (the inode is
c89f0 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74   determined by t
c8a00 68 65 0a 2a 2a 20 73 74 5f 64 65 76 20 61 6e 64  he.** st_dev and
c8a10 20 73 74 5f 69 6e 6f 20 66 69 65 6c 64 73 20 6f   st_ino fields o
c8a20 66 20 74 68 65 20 73 74 61 74 20 73 74 72 75 63  f the stat struc
c8a30 74 75 72 65 20 74 68 61 74 20 66 73 74 61 74 28  ture that fstat(
c8a40 29 20 66 69 6c 6c 73 20 69 6e 29 0a 2a 2a 20 61  ) fills in).** a
c8a50 6e 64 20 63 68 65 63 6b 20 66 6f 72 20 6c 6f 63  nd check for loc
c8a60 6b 73 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  ks already exist
c8a70 69 6e 67 20 6f 6e 20 74 68 61 74 20 69 6e 6f 64  ing on that inod
c8a80 65 2e 20 20 57 68 65 6e 20 6c 6f 63 6b 73 20 61  e.  When locks a
c8a90 72 65 0a 2a 2a 20 63 72 65 61 74 65 64 20 6f 72  re.** created or
c8aa0 20 72 65 6d 6f 76 65 64 2c 20 77 65 20 68 61 76   removed, we hav
c8ab0 65 20 74 6f 20 6c 6f 6f 6b 20 61 74 20 6f 75 72  e to look at our
c8ac0 20 6f 77 6e 20 69 6e 74 65 72 6e 61 6c 20 72 65   own internal re
c8ad0 63 6f 72 64 20 6f 66 20 74 68 65 0a 2a 2a 20 6c  cord of the.** l
c8ae0 6f 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20 61  ocks to see if a
c8af0 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 68 61  nother thread ha
c8b00 73 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65 74  s previously set
c8b10 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 61 74 20   a lock on that 
c8b20 73 61 6d 65 0a 2a 2a 20 69 6e 6f 64 65 2e 0a 2a  same.** inode..*
c8b30 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
c8b40 5f 66 69 6c 65 20 73 74 72 75 63 74 75 72 65 20  _file structure 
c8b50 66 6f 72 20 50 4f 53 49 58 20 69 73 20 6e 6f 20  for POSIX is no 
c8b60 6c 6f 6e 67 65 72 20 6a 75 73 74 20 61 6e 20 69  longer just an i
c8b70 6e 74 65 67 65 72 20 66 69 6c 65 0a 2a 2a 20 64  nteger file.** d
c8b80 65 73 63 72 69 70 74 6f 72 2e 20 20 49 74 20 69  escriptor.  It i
c8b90 73 20 6e 6f 77 20 61 20 73 74 72 75 63 74 75 72  s now a structur
c8ba0 65 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65  e that holds the
c8bb0 20 69 6e 74 65 67 65 72 20 66 69 6c 65 0a 2a 2a   integer file.**
c8bc0 20 64 65 73 63 72 69 70 74 6f 72 20 61 6e 64 20   descriptor and 
c8bd0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73  a pointer to a s
c8be0 74 72 75 63 74 75 72 65 20 74 68 61 74 20 64 65  tructure that de
c8bf0 73 63 72 69 62 65 73 20 74 68 65 20 69 6e 74 65  scribes the inte
c8c00 72 6e 61 6c 0a 2a 2a 20 6c 6f 63 6b 73 20 6f 6e  rnal.** locks on
c8c10 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
c8c20 6e 67 20 69 6e 6f 64 65 2e 20 20 54 68 65 72 65  ng inode.  There
c8c30 20 69 73 20 6f 6e 65 20 6c 6f 63 6b 69 6e 67 20   is one locking 
c8c40 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 70 65 72  structure.** per
c8c50 20 69 6e 6f 64 65 2c 20 73 6f 20 69 66 20 74 68   inode, so if th
c8c60 65 20 73 61 6d 65 20 69 6e 6f 64 65 20 69 73 20  e same inode is 
c8c70 6f 70 65 6e 65 64 20 74 77 69 63 65 2c 20 62 6f  opened twice, bo
c8c80 74 68 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75  th unixFile stru
c8c90 63 74 75 72 65 73 0a 2a 2a 20 70 6f 69 6e 74 20  ctures.** point 
c8ca0 74 6f 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b  to the same lock
c8cb0 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 20 20  ing structure.  
c8cc0 54 68 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75  The locking stru
c8cd0 63 74 75 72 65 20 6b 65 65 70 73 0a 2a 2a 20 61  cture keeps.** a
c8ce0 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
c8cf0 20 28 73 6f 20 77 65 20 77 69 6c 6c 20 6b 6e 6f   (so we will kno
c8d00 77 20 77 68 65 6e 20 74 6f 20 64 65 6c 65 74 65  w when to delete
c8d10 20 69 74 29 20 61 6e 64 20 61 20 22 63 6e 74 22   it) and a "cnt"
c8d20 0a 2a 2a 20 66 69 65 6c 64 20 74 68 61 74 20 74  .** field that t
c8d30 65 6c 6c 73 20 75 73 20 69 74 73 20 69 6e 74 65  ells us its inte
c8d40 72 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 75 73  rnal lock status
c8d50 2e 20 20 63 6e 74 3d 3d 30 20 6d 65 61 6e 73 20  .  cnt==0 means 
c8d60 74 68 65 0a 2a 2a 20 66 69 6c 65 20 69 73 20 75  the.** file is u
c8d70 6e 6c 6f 63 6b 65 64 2e 20 20 63 6e 74 3d 3d 2d  nlocked.  cnt==-
c8d80 31 20 6d 65 61 6e 73 20 74 68 65 20 66 69 6c 65  1 means the file
c8d90 20 68 61 73 20 61 6e 20 65 78 63 6c 75 73 69 76   has an exclusiv
c8da0 65 20 6c 6f 63 6b 2e 0a 2a 2a 20 63 6e 74 3e 30  e lock..** cnt>0
c8db0 20 6d 65 61 6e 73 20 74 68 65 72 65 20 61 72 65   means there are
c8dc0 20 63 6e 74 20 73 68 61 72 65 64 20 6c 6f 63 6b   cnt shared lock
c8dd0 73 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a  s on the file..*
c8de0 2a 0a 2a 2a 20 41 6e 79 20 61 74 74 65 6d 70 74  *.** Any attempt
c8df0 20 74 6f 20 6c 6f 63 6b 20 6f 72 20 75 6e 6c 6f   to lock or unlo
c8e00 63 6b 20 61 20 66 69 6c 65 20 66 69 72 73 74 20  ck a file first 
c8e10 63 68 65 63 6b 73 20 74 68 65 20 6c 6f 63 6b 69  checks the locki
c8e20 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e  ng.** structure.
c8e30 20 20 54 68 65 20 66 63 6e 74 6c 28 29 20 73 79    The fcntl() sy
c8e40 73 74 65 6d 20 63 61 6c 6c 20 69 73 20 6f 6e 6c  stem call is onl
c8e50 79 20 69 6e 76 6f 6b 65 64 20 74 6f 20 73 65 74  y invoked to set
c8e60 20 61 20 0a 2a 2a 20 50 4f 53 49 58 20 6c 6f 63   a .** POSIX loc
c8e70 6b 20 69 66 20 74 68 65 20 69 6e 74 65 72 6e 61  k if the interna
c8e80 6c 20 6c 6f 63 6b 20 73 74 72 75 63 74 75 72 65  l lock structure
c8e90 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 62 65 74   transitions bet
c8ea0 77 65 65 6e 0a 2a 2a 20 61 20 6c 6f 63 6b 65 64  ween.** a locked
c8eb0 20 61 6e 64 20 61 6e 20 75 6e 6c 6f 63 6b 65 64   and an unlocked
c8ec0 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 32 30   state..**.** 20
c8ed0 30 34 2d 4a 61 6e 2d 31 31 3a 0a 2a 2a 20 4d 6f  04-Jan-11:.** Mo
c8ee0 72 65 20 72 65 63 65 6e 74 20 64 69 73 63 6f 76  re recent discov
c8ef0 65 72 69 65 73 20 61 62 6f 75 74 20 50 4f 53 49  eries about POSI
c8f00 58 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73  X advisory locks
c8f10 2e 20 20 28 54 68 65 20 6d 6f 72 65 0a 2a 2a 20  .  (The more.** 
c8f20 49 20 64 69 73 63 6f 76 65 72 2c 20 74 68 65 20  I discover, the 
c8f30 6d 6f 72 65 20 49 20 72 65 61 6c 69 7a 65 20 74  more I realize t
c8f40 68 65 20 61 20 50 4f 53 49 58 20 61 64 76 69 73  he a POSIX advis
c8f50 6f 72 79 20 6c 6f 63 6b 73 20 61 72 65 0a 2a 2a  ory locks are.**
c8f60 20 61 6e 20 61 62 6f 6d 69 6e 61 74 69 6f 6e 2e   an abomination.
c8f70 29 0a 2a 2a 0a 2a 2a 20 49 66 20 79 6f 75 20 63  ).**.** If you c
c8f80 6c 6f 73 65 20 61 20 66 69 6c 65 20 64 65 73 63  lose a file desc
c8f90 72 69 70 74 6f 72 20 74 68 61 74 20 70 6f 69 6e  riptor that poin
c8fa0 74 73 20 74 6f 20 61 20 66 69 6c 65 20 74 68 61  ts to a file tha
c8fb0 74 20 68 61 73 20 6c 6f 63 6b 73 2c 0a 2a 2a 20  t has locks,.** 
c8fc0 61 6c 6c 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 61  all locks on tha
c8fd0 74 20 66 69 6c 65 20 74 68 61 74 20 61 72 65 20  t file that are 
c8fe0 6f 77 6e 65 64 20 62 79 20 74 68 65 20 63 75 72  owned by the cur
c8ff0 72 65 6e 74 20 70 72 6f 63 65 73 73 20 61 72 65  rent process are
c9000 0a 2a 2a 20 72 65 6c 65 61 73 65 64 2e 20 20 54  .** released.  T
c9010 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68  o work around th
c9020 69 73 20 70 72 6f 62 6c 65 6d 2c 20 65 61 63 68  is problem, each
c9030 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74   unixFile struct
c9040 75 72 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20  ure contains.** 
c9050 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
c9060 6f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72  openCnt structur
c9070 65 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65  e.  There is one
c9080 20 6f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75   openCnt structu
c9090 72 65 0a 2a 2a 20 70 65 72 20 6f 70 65 6e 20 69  re.** per open i
c90a0 6e 6f 64 65 2c 20 77 68 69 63 68 20 6d 65 61 6e  node, which mean
c90b0 73 20 74 68 61 74 20 6d 75 6c 74 69 70 6c 65 20  s that multiple 
c90c0 75 6e 69 78 46 69 6c 65 20 63 61 6e 20 70 6f 69  unixFile can poi
c90d0 6e 74 20 74 6f 20 61 20 73 69 6e 67 6c 65 0a 2a  nt to a single.*
c90e0 2a 20 6f 70 65 6e 43 6e 74 2e 20 20 57 68 65 6e  * openCnt.  When
c90f0 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d   an attempt is m
c9100 61 64 65 20 74 6f 20 63 6c 6f 73 65 20 61 6e 20  ade to close an 
c9110 75 6e 69 78 46 69 6c 65 2c 20 69 66 20 74 68 65  unixFile, if the
c9120 72 65 20 61 72 65 0a 2a 2a 20 6f 74 68 65 72 20  re are.** other 
c9130 75 6e 69 78 46 69 6c 65 20 6f 70 65 6e 20 6f 6e  unixFile open on
c9140 20 74 68 65 20 73 61 6d 65 20 69 6e 6f 64 65 20   the same inode 
c9150 74 68 61 74 20 61 72 65 20 68 6f 6c 64 69 6e 67  that are holding
c9160 20 6c 6f 63 6b 73 2c 20 74 68 65 20 63 61 6c 6c   locks, the call
c9170 0a 2a 2a 20 74 6f 20 63 6c 6f 73 65 28 29 20 74  .** to close() t
c9180 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
c9190 6f 72 20 69 73 20 64 65 66 65 72 72 65 64 20 75  or is deferred u
c91a0 6e 74 69 6c 20 61 6c 6c 20 6f 66 20 74 68 65 20  ntil all of the 
c91b0 6c 6f 63 6b 73 20 63 6c 65 61 72 2e 0a 2a 2a 20  locks clear..** 
c91c0 54 68 65 20 6f 70 65 6e 43 6e 74 20 73 74 72 75  The openCnt stru
c91d0 63 74 75 72 65 20 6b 65 65 70 73 20 61 20 6c 69  cture keeps a li
c91e0 73 74 20 6f 66 20 66 69 6c 65 20 64 65 73 63 72  st of file descr
c91f0 69 70 74 6f 72 73 20 74 68 61 74 20 6e 65 65 64  iptors that need
c9200 20 74 6f 0a 2a 2a 20 62 65 20 63 6c 6f 73 65 64   to.** be closed
c9210 20 61 6e 64 20 74 68 61 74 20 6c 69 73 74 20 69   and that list i
c9220 73 20 77 61 6c 6b 65 64 20 28 61 6e 64 20 63 6c  s walked (and cl
c9230 65 61 72 65 64 29 20 77 68 65 6e 20 74 68 65 20  eared) when the 
c9240 6c 61 73 74 20 6c 6f 63 6b 0a 2a 2a 20 63 6c 65  last lock.** cle
c9250 61 72 73 2e 0a 2a 2a 0a 2a 2a 20 46 69 72 73 74  ars..**.** First
c9260 2c 20 75 6e 64 65 72 20 4c 69 6e 75 78 20 74 68  , under Linux th
c9270 72 65 61 64 73 2c 20 62 65 63 61 75 73 65 20 65  reads, because e
c9280 61 63 68 20 74 68 72 65 61 64 20 68 61 73 20 61  ach thread has a
c9290 20 73 65 70 61 72 61 74 65 0a 2a 2a 20 70 72 6f   separate.** pro
c92a0 63 65 73 73 20 49 44 2c 20 6c 6f 63 6b 20 6f 70  cess ID, lock op
c92b0 65 72 61 74 69 6f 6e 73 20 69 6e 20 6f 6e 65 20  erations in one 
c92c0 74 68 72 65 61 64 20 64 6f 20 6e 6f 74 20 6f 76  thread do not ov
c92d0 65 72 72 69 64 65 20 6c 6f 63 6b 73 0a 2a 2a 20  erride locks.** 
c92e0 74 6f 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65  to the same file
c92f0 20 69 6e 20 6f 74 68 65 72 20 74 68 72 65 61 64   in other thread
c9300 73 2e 20 20 4c 69 6e 75 78 20 74 68 72 65 61 64  s.  Linux thread
c9310 73 20 62 65 68 61 76 65 20 6c 69 6b 65 0a 2a 2a  s behave like.**
c9320 20 73 65 70 61 72 61 74 65 20 70 72 6f 63 65 73   separate proces
c9330 73 65 73 20 69 6e 20 74 68 69 73 20 72 65 73 70  ses in this resp
c9340 65 63 74 2e 20 20 42 75 74 2c 20 69 66 20 79 6f  ect.  But, if yo
c9350 75 20 63 6c 6f 73 65 20 61 20 66 69 6c 65 0a 2a  u close a file.*
c9360 2a 20 64 65 73 63 72 69 70 74 6f 72 20 69 6e 20  * descriptor in 
c9370 6c 69 6e 75 78 20 74 68 72 65 61 64 73 2c 20 61  linux threads, a
c9380 6c 6c 20 6c 6f 63 6b 73 20 61 72 65 20 63 6c 65  ll locks are cle
c9390 61 72 65 64 2c 20 65 76 65 6e 20 6c 6f 63 6b 73  ared, even locks
c93a0 0a 2a 2a 20 6f 6e 20 6f 74 68 65 72 20 74 68 72  .** on other thr
c93b0 65 61 64 73 20 61 6e 64 20 65 76 65 6e 20 74 68  eads and even th
c93c0 6f 75 67 68 20 74 68 65 20 6f 74 68 65 72 20 74  ough the other t
c93d0 68 72 65 61 64 73 20 68 61 76 65 20 64 69 66 66  hreads have diff
c93e0 65 72 65 6e 74 0a 2a 2a 20 70 72 6f 63 65 73 73  erent.** process
c93f0 20 49 44 73 2e 20 20 4c 69 6e 75 78 20 74 68 72   IDs.  Linux thr
c9400 65 61 64 73 20 69 73 20 69 6e 63 6f 6e 73 69 73  eads is inconsis
c9410 74 65 6e 74 20 69 6e 20 74 68 69 73 20 72 65 73  tent in this res
c9420 70 65 63 74 2e 0a 2a 2a 20 28 49 27 6d 20 62 65  pect..** (I'm be
c9430 67 69 6e 6e 69 6e 67 20 74 6f 20 74 68 69 6e 6b  ginning to think
c9440 20 74 68 61 74 20 6c 69 6e 75 78 20 74 68 72 65   that linux thre
c9450 61 64 73 20 69 73 20 61 6e 20 61 62 6f 6d 69 6e  ads is an abomin
c9460 61 74 69 6f 6e 20 74 6f 6f 2e 29 0a 2a 2a 20 54  ation too.).** T
c9470 68 65 20 63 6f 6e 73 65 71 75 65 6e 63 65 20 6f  he consequence o
c9480 66 20 74 68 69 73 20 61 6c 6c 20 69 73 20 74 68  f this all is th
c9490 61 74 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  at the hash tabl
c94a0 65 20 66 6f 72 20 74 68 65 20 6c 6f 63 6b 49 6e  e for the lockIn
c94b0 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20  fo.** structure 
c94c0 68 61 73 20 74 6f 20 69 6e 63 6c 75 64 65 20 74  has to include t
c94d0 68 65 20 70 72 6f 63 65 73 73 20 69 64 20 61 73  he process id as
c94e0 20 70 61 72 74 20 6f 66 20 69 74 73 20 6b 65 79   part of its key
c94f0 20 62 65 63 61 75 73 65 0a 2a 2a 20 6c 6f 63 6b   because.** lock
c9500 73 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 74  s in different t
c9510 68 72 65 61 64 73 20 61 72 65 20 74 72 65 61 74  hreads are treat
c9520 65 64 20 61 73 20 64 69 73 74 69 6e 63 74 2e 20  ed as distinct. 
c9530 20 42 75 74 20 74 68 65 20 0a 2a 2a 20 6f 70 65   But the .** ope
c9540 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 73  nCnt structure s
c9550 68 6f 75 6c 64 20 6e 6f 74 20 69 6e 63 6c 75 64  hould not includ
c9560 65 20 74 68 65 20 70 72 6f 63 65 73 73 20 69 64  e the process id
c9570 20 69 6e 20 69 74 73 0a 2a 2a 20 6b 65 79 20 62   in its.** key b
c9580 65 63 61 75 73 65 20 63 6c 6f 73 65 28 29 20 63  ecause close() c
c9590 6c 65 61 72 73 20 6c 6f 63 6b 20 6f 6e 20 61 6c  lears lock on al
c95a0 6c 20 74 68 72 65 61 64 73 2c 20 6e 6f 74 20 6a  l threads, not j
c95b0 75 73 74 20 74 68 65 20 63 75 72 72 65 6e 74 0a  ust the current.
c95c0 2a 2a 20 74 68 72 65 61 64 2e 20 20 57 65 72 65  ** thread.  Were
c95d0 20 69 74 20 6e 6f 74 20 66 6f 72 20 74 68 69 73   it not for this
c95e0 20 67 6f 6f 66 69 6e 65 73 73 20 69 6e 20 6c 69   goofiness in li
c95f0 6e 75 78 20 74 68 72 65 61 64 73 2c 20 77 65 20  nux threads, we 
c9600 63 6f 75 6c 64 0a 2a 2a 20 63 6f 6d 62 69 6e 65  could.** combine
c9610 20 74 68 65 20 6c 6f 63 6b 49 6e 66 6f 20 61 6e   the lockInfo an
c9620 64 20 6f 70 65 6e 43 6e 74 20 73 74 72 75 63 74  d openCnt struct
c9630 75 72 65 73 20 69 6e 74 6f 20 61 20 73 69 6e 67  ures into a sing
c9640 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  le structure..**
c9650 0a 2a 2a 20 32 30 30 34 2d 4a 75 6e 2d 32 38 3a  .** 2004-Jun-28:
c9660 0a 2a 2a 20 4f 6e 20 73 6f 6d 65 20 76 65 72 73  .** On some vers
c9670 69 6f 6e 73 20 6f 66 20 6c 69 6e 75 78 2c 20 74  ions of linux, t
c9680 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72 72  hreads can overr
c9690 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 20  ide each others 
c96a0 6c 6f 63 6b 73 2e 0a 2a 2a 20 4f 6e 20 6f 74 68  locks..** On oth
c96b0 65 72 73 20 6e 6f 74 2e 20 20 53 6f 6d 65 74 69  ers not.  Someti
c96c0 6d 65 73 20 79 6f 75 20 63 61 6e 20 63 68 61 6e  mes you can chan
c96d0 67 65 20 74 68 65 20 62 65 68 61 76 69 6f 72 20  ge the behavior 
c96e0 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 73  on the same.** s
c96f0 79 73 74 65 6d 20 62 79 20 73 65 74 74 69 6e 67  ystem by setting
c9700 20 74 68 65 20 4c 44 5f 41 53 53 55 4d 45 5f 4b   the LD_ASSUME_K
c9710 45 52 4e 45 4c 20 65 6e 76 69 72 6f 6e 6d 65 6e  ERNEL environmen
c9720 74 20 76 61 72 69 61 62 6c 65 2e 20 20 54 68 65  t variable.  The
c9730 0a 2a 2a 20 50 4f 53 49 58 20 73 74 61 6e 64 61  .** POSIX standa
c9740 72 64 20 69 73 20 73 69 6c 65 6e 74 20 61 73 20  rd is silent as 
c9750 74 6f 20 77 68 69 63 68 20 62 65 68 61 76 69 6f  to which behavio
c9760 72 20 69 73 20 63 6f 72 72 65 63 74 2c 20 61 73  r is correct, as
c9770 20 66 61 72 0a 2a 2a 20 61 73 20 49 20 63 61 6e   far.** as I can
c9780 20 74 65 6c 6c 2c 20 73 6f 20 6f 74 68 65 72 20   tell, so other 
c9790 76 65 72 73 69 6f 6e 73 20 6f 66 20 75 6e 69 78  versions of unix
c97a0 20 6d 69 67 68 74 20 73 68 6f 77 20 74 68 65 20   might show the 
c97b0 73 61 6d 65 0a 2a 2a 20 69 6e 63 6f 6e 73 69 73  same.** inconsis
c97c0 74 65 6e 63 79 2e 20 20 54 68 65 72 65 20 69 73  tency.  There is
c97d0 20 6e 6f 20 6c 69 74 74 6c 65 20 64 6f 75 62 74   no little doubt
c97e0 20 69 6e 20 6d 79 20 6d 69 6e 64 20 74 68 61 74   in my mind that
c97f0 20 70 6f 73 69 78 0a 2a 2a 20 61 64 76 69 73 6f   posix.** adviso
c9800 72 79 20 6c 6f 63 6b 73 20 61 6e 64 20 6c 69 6e  ry locks and lin
c9810 75 78 20 74 68 72 65 61 64 73 20 61 72 65 20 70  ux threads are p
c9820 72 6f 66 6f 75 6e 64 6c 79 20 62 72 6f 6b 65 6e  rofoundly broken
c9830 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 77 6f 72 6b 20  ..**.** To work 
c9840 61 72 6f 75 6e 64 20 74 68 65 20 69 6e 63 6f 6e  around the incon
c9850 73 69 73 74 65 6e 63 69 65 73 2c 20 77 65 20 68  sistencies, we h
c9860 61 76 65 20 74 6f 20 74 65 73 74 20 61 74 20 72  ave to test at r
c9870 75 6e 74 69 6d 65 20 0a 2a 2a 20 77 68 65 74 68  untime .** wheth
c9880 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64  er or not thread
c9890 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65  s can override e
c98a0 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73  ach others locks
c98b0 2e 20 20 54 68 69 73 20 74 65 73 74 0a 2a 2a 20  .  This test.** 
c98c0 69 73 20 72 75 6e 20 6f 6e 63 65 2c 20 74 68 65  is run once, the
c98d0 20 66 69 72 73 74 20 74 69 6d 65 20 61 6e 79 20   first time any 
c98e0 6c 6f 63 6b 20 69 73 20 61 74 74 65 6d 70 74 65  lock is attempte
c98f0 64 2e 20 20 41 20 73 74 61 74 69 63 20 0a 2a 2a  d.  A static .**
c9900 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74   variable is set
c9910 20 74 6f 20 72 65 63 6f 72 64 20 74 68 65 20 72   to record the r
c9920 65 73 75 6c 74 73 20 6f 66 20 74 68 69 73 20 74  esults of this t
c9930 65 73 74 20 66 6f 72 20 66 75 74 75 72 65 0a 2a  est for future.*
c9940 2a 20 75 73 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  * use..*/../*.**
c9950 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
c9960 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
c9970 72 75 63 74 75 72 65 20 73 65 72 76 65 73 20 61  ructure serves a
c9980 73 20 74 68 65 20 6b 65 79 20 75 73 65 64 0a 2a  s the key used.*
c9990 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61 20 70 61  * to locate a pa
c99a0 72 74 69 63 75 6c 61 72 20 6c 6f 63 6b 49 6e 66  rticular lockInf
c99b0 6f 20 73 74 72 75 63 74 75 72 65 20 67 69 76 65  o structure give
c99c0 6e 20 69 74 73 20 69 6e 6f 64 65 2e 0a 2a 2a 0a  n its inode..**.
c99d0 2a 2a 20 49 66 20 74 68 72 65 61 64 73 20 63 61  ** If threads ca
c99e0 6e 6e 6f 74 20 6f 76 65 72 72 69 64 65 20 65 61  nnot override ea
c99f0 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2c  ch others locks,
c9a00 20 74 68 65 6e 20 77 65 20 73 65 74 20 74 68 65   then we set the
c9a10 0a 2a 2a 20 6c 6f 63 6b 4b 65 79 2e 74 69 64 20  .** lockKey.tid 
c9a20 66 69 65 6c 64 20 74 6f 20 74 68 65 20 74 68 72  field to the thr
c9a30 65 61 64 20 49 44 2e 20 20 49 66 20 74 68 72 65  ead ID.  If thre
c9a40 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65  ads can override
c9a50 0a 2a 2a 20 65 61 63 68 20 6f 74 68 65 72 73 20  .** each others 
c9a60 6c 6f 63 6b 73 20 74 68 65 6e 20 74 69 64 20 69  locks then tid i
c9a70 73 20 61 6c 77 61 79 73 20 73 65 74 20 74 6f 20  s always set to 
c9a80 7a 65 72 6f 2e 20 20 74 69 64 20 69 73 20 6f 6d  zero.  tid is om
c9a90 69 74 74 65 64 0a 2a 2a 20 69 66 20 77 65 20 63  itted.** if we c
c9aa0 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 74  ompile without t
c9ab0 68 72 65 61 64 69 6e 67 20 73 75 70 70 6f 72 74  hreading support
c9ac0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 6c 6f 63 6b  ..*/.struct lock
c9ad0 4b 65 79 20 7b 0a 20 20 64 65 76 5f 74 20 64 65  Key {.  dev_t de
c9ae0 76 3b 20 20 20 20 20 20 20 2f 2a 20 44 65 76 69  v;       /* Devi
c9af0 63 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69  ce number */.  i
c9b00 6e 6f 5f 74 20 69 6e 6f 3b 20 20 20 20 20 20 20  no_t ino;       
c9b10 2f 2a 20 49 6e 6f 64 65 20 6e 75 6d 62 65 72 20  /* Inode number 
c9b20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  */.#if SQLITE_TH
c9b30 52 45 41 44 53 41 46 45 0a 20 20 70 74 68 72 65  READSAFE.  pthre
c9b40 61 64 5f 74 20 74 69 64 3b 20 20 20 2f 2a 20 54  ad_t tid;   /* T
c9b50 68 72 65 61 64 20 49 44 20 6f 72 20 7a 65 72 6f  hread ID or zero
c9b60 20 69 66 20 74 68 72 65 61 64 73 20 63 61 6e 20   if threads can 
c9b70 6f 76 65 72 72 69 64 65 20 65 61 63 68 20 6f 74  override each ot
c9b80 68 65 72 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b  her */.#endif.};
c9b90 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
c9ba0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
c9bb0 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69  wing structure i
c9bc0 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20  s allocated for 
c9bd0 65 61 63 68 20 6f 70 65 6e 0a 2a 2a 20 69 6e 6f  each open.** ino
c9be0 64 65 20 6f 6e 20 65 61 63 68 20 74 68 72 65 61  de on each threa
c9bf0 64 20 77 69 74 68 20 61 20 64 69 66 66 65 72 65  d with a differe
c9c00 6e 74 20 70 72 6f 63 65 73 73 20 49 44 2e 20 20  nt process ID.  
c9c10 28 54 68 72 65 61 64 73 20 68 61 76 65 0a 2a 2a  (Threads have.**
c9c20 20 64 69 66 66 65 72 65 6e 74 20 70 72 6f 63 65   different proce
c9c30 73 73 20 49 44 73 20 6f 6e 20 6c 69 6e 75 78 2c  ss IDs on linux,
c9c40 20 62 75 74 20 6e 6f 74 20 6f 6e 20 6d 6f 73 74   but not on most
c9c50 20 6f 74 68 65 72 20 75 6e 69 78 65 73 2e 29 0a   other unixes.).
c9c60 2a 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69  **.** A single i
c9c70 6e 6f 64 65 20 63 61 6e 20 68 61 76 65 20 6d 75  node can have mu
c9c80 6c 74 69 70 6c 65 20 66 69 6c 65 20 64 65 73 63  ltiple file desc
c9c90 72 69 70 74 6f 72 73 2c 20 73 6f 20 65 61 63 68  riptors, so each
c9ca0 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 74 72   unixFile.** str
c9cb0 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20  ucture contains 
c9cc0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
c9cd0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
c9ce0 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 69 73   object and this
c9cf0 0a 2a 2a 20 6f 62 6a 65 63 74 20 6b 65 65 70 73  .** object keeps
c9d00 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20   a count of the 
c9d10 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 78 46 69  number of unixFi
c9d20 6c 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69  le pointing to i
c9d30 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 6c 6f 63  t..*/.struct loc
c9d40 6b 49 6e 66 6f 20 7b 0a 20 20 73 74 72 75 63 74  kInfo {.  struct
c9d50 20 6c 6f 63 6b 4b 65 79 20 6b 65 79 3b 20 20 2f   lockKey key;  /
c9d60 2a 20 54 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79  * The lookup key
c9d70 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20   */.  int cnt;  
c9d80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
c9d90 6d 62 65 72 20 6f 66 20 53 48 41 52 45 44 20 6c  mber of SHARED l
c9da0 6f 63 6b 73 20 68 65 6c 64 20 2a 2f 0a 20 20 69  ocks held */.  i
c9db0 6e 74 20 6c 6f 63 6b 74 79 70 65 3b 20 20 20 20  nt locktype;    
c9dc0 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 48      /* One of SH
c9dd0 41 52 45 44 5f 4c 4f 43 4b 2c 20 52 45 53 45 52  ARED_LOCK, RESER
c9de0 56 45 44 5f 4c 4f 43 4b 20 65 74 63 2e 20 2a 2f  VED_LOCK etc. */
c9df0 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20  .  int nRef;    
c9e00 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
c9e10 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f  r of pointers to
c9e20 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
c9e30 2a 2f 0a 20 20 73 74 72 75 63 74 20 6c 6f 63 6b  */.  struct lock
c9e40 49 6e 66 6f 20 2a 70 4e 65 78 74 2c 20 2a 70 50  Info *pNext, *pP
c9e50 72 65 76 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f  rev;   /* List o
c9e60 66 20 61 6c 6c 20 6c 6f 63 6b 49 6e 66 6f 20 6f  f all lockInfo o
c9e70 62 6a 65 63 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  bjects */.};../*
c9e80 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
c9e90 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
c9ea0 20 73 74 72 75 63 74 75 72 65 20 73 65 72 76 65   structure serve
c9eb0 73 20 61 73 20 74 68 65 20 6b 65 79 20 75 73 65  s as the key use
c9ec0 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61  d.** to locate a
c9ed0 20 70 61 72 74 69 63 75 6c 61 72 20 6f 70 65 6e   particular open
c9ee0 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 67 69  Cnt structure gi
c9ef0 76 65 6e 20 69 74 73 20 69 6e 6f 64 65 2e 20 20  ven its inode.  
c9f00 54 68 69 73 0a 2a 2a 20 69 73 20 74 68 65 20 73  This.** is the s
c9f10 61 6d 65 20 61 73 20 74 68 65 20 6c 6f 63 6b 4b  ame as the lockK
c9f20 65 79 20 65 78 63 65 70 74 20 74 68 61 74 20 74  ey except that t
c9f30 68 65 20 74 68 72 65 61 64 20 49 44 20 69 73 20  he thread ID is 
c9f40 6f 6d 69 74 74 65 64 2e 0a 2a 2f 0a 73 74 72 75  omitted..*/.stru
c9f50 63 74 20 6f 70 65 6e 4b 65 79 20 7b 0a 20 20 64  ct openKey {.  d
c9f60 65 76 5f 74 20 64 65 76 3b 20 20 20 2f 2a 20 44  ev_t dev;   /* D
c9f70 65 76 69 63 65 20 6e 75 6d 62 65 72 20 2a 2f 0a  evice number */.
c9f80 20 20 69 6e 6f 5f 74 20 69 6e 6f 3b 20 20 20 2f    ino_t ino;   /
c9f90 2a 20 49 6e 6f 64 65 20 6e 75 6d 62 65 72 20 2a  * Inode number *
c9fa0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  /.};../*.** An i
c9fb0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
c9fc0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
c9fd0 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  re is allocated 
c9fe0 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 0a 2a 2a  for each open.**
c9ff0 20 69 6e 6f 64 65 2e 20 20 54 68 69 73 20 73 74   inode.  This st
ca000 72 75 63 74 75 72 65 20 6b 65 65 70 73 20 74 72  ructure keeps tr
ca010 61 63 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  ack of the numbe
ca020 72 20 6f 66 20 6c 6f 63 6b 73 20 6f 6e 20 74 68  r of locks on th
ca030 61 74 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 49 66  at.** inode.  If
ca040 20 61 20 63 6c 6f 73 65 20 69 73 20 61 74 74 65   a close is atte
ca050 6d 70 74 65 64 20 61 67 61 69 6e 73 74 20 61 6e  mpted against an
ca060 20 69 6e 6f 64 65 20 74 68 61 74 20 69 73 20 68   inode that is h
ca070 6f 6c 64 69 6e 67 0a 2a 2a 20 6c 6f 63 6b 73 2c  olding.** locks,
ca080 20 74 68 65 20 63 6c 6f 73 65 20 69 73 20 64 65   the close is de
ca090 66 65 72 72 65 64 20 75 6e 74 69 6c 20 61 6c 6c  ferred until all
ca0a0 20 6c 6f 63 6b 73 20 63 6c 65 61 72 20 62 79 20   locks clear by 
ca0b0 61 64 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69  adding the.** fi
ca0c0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f  le descriptor to
ca0d0 20 62 65 20 63 6c 6f 73 65 64 20 74 6f 20 74 68   be closed to th
ca0e0 65 20 70 65 6e 64 69 6e 67 20 6c 69 73 74 2e 0a  e pending list..
ca0f0 2a 2f 0a 73 74 72 75 63 74 20 6f 70 65 6e 43 6e  */.struct openCn
ca100 74 20 7b 0a 20 20 73 74 72 75 63 74 20 6f 70 65  t {.  struct ope
ca110 6e 4b 65 79 20 6b 65 79 3b 20 20 20 2f 2a 20 54  nKey key;   /* T
ca120 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f  he lookup key */
ca130 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20  .  int nRef;    
ca140 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
ca150 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74  er of pointers t
ca160 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  o this structure
ca170 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f 63 6b 3b   */.  int nLock;
ca180 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
ca190 75 6d 62 65 72 20 6f 66 20 6f 75 74 73 74 61 6e  umber of outstan
ca1a0 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20  ding locks */.  
ca1b0 69 6e 74 20 6e 50 65 6e 64 69 6e 67 3b 20 20 20  int nPending;   
ca1c0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
ca1d0 6f 66 20 70 65 6e 64 69 6e 67 20 63 6c 6f 73 65  of pending close
ca1e0 28 29 20 6f 70 65 72 61 74 69 6f 6e 73 20 2a 2f  () operations */
ca1f0 0a 20 20 69 6e 74 20 2a 61 50 65 6e 64 69 6e 67  .  int *aPending
ca200 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c  ;        /* Mall
ca210 6f 63 65 64 20 73 70 61 63 65 20 68 6f 6c 64 69  oced space holdi
ca220 6e 67 20 66 64 27 73 20 61 77 61 69 74 69 6e 67  ng fd's awaiting
ca230 20 61 20 63 6c 6f 73 65 28 29 20 2a 2f 0a 20 20   a close() */.  
ca240 73 74 72 75 63 74 20 6f 70 65 6e 43 6e 74 20 2a  struct openCnt *
ca250 70 4e 65 78 74 2c 20 2a 70 50 72 65 76 3b 20 20  pNext, *pPrev;  
ca260 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c 20   /* List of all 
ca270 6f 70 65 6e 43 6e 74 20 6f 62 6a 65 63 74 73 20  openCnt objects 
ca280 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4c 69 73  */.};../*.** Lis
ca290 74 20 6f 66 20 61 6c 6c 20 6c 6f 63 6b 49 6e 66  t of all lockInf
ca2a0 6f 20 61 6e 64 20 6f 70 65 6e 43 6e 74 20 6f 62  o and openCnt ob
ca2b0 6a 65 63 74 73 2e 20 20 54 68 69 73 20 75 73 65  jects.  This use
ca2c0 64 20 74 6f 20 62 65 20 61 20 68 61 73 68 0a 2a  d to be a hash.*
ca2d0 2a 20 74 61 62 6c 65 2e 20 20 42 75 74 20 74 68  * table.  But th
ca2e0 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 62 6a 65  e number of obje
ca2f0 63 74 73 20 69 73 20 72 61 72 65 6c 79 20 6d 6f  cts is rarely mo
ca300 72 65 20 74 68 61 6e 20 61 20 64 6f 7a 65 6e 20  re than a dozen 
ca310 61 6e 64 0a 2a 2a 20 6e 65 76 65 72 20 65 78 63  and.** never exc
ca320 65 65 64 73 20 61 20 66 65 77 20 74 68 6f 75 73  eeds a few thous
ca330 61 6e 64 2e 20 20 41 6e 64 20 6c 6f 6f 6b 75 70  and.  And lookup
ca340 20 69 73 20 6e 6f 74 20 6f 6e 20 61 20 63 72 69   is not on a cri
ca350 74 69 63 61 6c 0a 2a 2a 20 70 61 74 68 20 6f 6f  tical.** path oo
ca360 20 61 20 73 69 6d 70 6c 65 20 6c 69 6e 6b 65 64   a simple linked
ca370 20 6c 69 73 74 20 77 69 6c 6c 20 73 75 66 66 69   list will suffi
ca380 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74  ce..*/.static st
ca390 72 75 63 74 20 6c 6f 63 6b 49 6e 66 6f 20 2a 6c  ruct lockInfo *l
ca3a0 6f 63 6b 4c 69 73 74 20 3d 20 30 3b 0a 73 74 61  ockList = 0;.sta
ca3b0 74 69 63 20 73 74 72 75 63 74 20 6f 70 65 6e 43  tic struct openC
ca3c0 6e 74 20 2a 6f 70 65 6e 4c 69 73 74 20 3d 20 30  nt *openList = 0
ca3d0 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6c 6f 63  ;../*.** The loc
ca3e0 6b 69 6e 67 20 73 74 79 6c 65 73 20 61 72 65 20  king styles are 
ca3f0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
ca400 74 68 65 20 64 69 66 66 65 72 65 6e 74 20 66 69  the different fi
ca410 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 63 61  le locking.** ca
ca420 70 61 62 69 6c 69 74 69 65 73 20 73 75 70 70 6f  pabilities suppo
ca430 72 74 65 64 20 62 79 20 64 69 66 66 65 72 65 6e  rted by differen
ca440 74 20 66 69 6c 65 20 73 79 73 74 65 6d 73 2e 20  t file systems. 
ca450 20 0a 2a 2a 0a 2a 2a 20 50 4f 53 49 58 20 6c 6f   .**.** POSIX lo
ca460 63 6b 69 6e 67 20 73 74 79 6c 65 20 66 75 6c 6c  cking style full
ca470 79 20 73 75 70 70 6f 72 74 73 20 73 68 61 72 65  y supports share
ca480 64 20 61 6e 64 20 65 78 63 6c 75 73 69 76 65 20  d and exclusive 
ca490 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 73  byte-range locks
ca4a0 20 0a 2a 2a 20 41 46 50 20 6c 6f 63 6b 69 6e 67   .** AFP locking
ca4b0 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 73 20 65   only supports e
ca4c0 78 63 6c 75 73 69 76 65 20 62 79 74 65 2d 72 61  xclusive byte-ra
ca4d0 6e 67 65 20 6c 6f 63 6b 73 0a 2a 2a 20 46 4c 4f  nge locks.** FLO
ca4e0 43 4b 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 73  CK only supports
ca4f0 20 61 20 73 69 6e 67 6c 65 20 66 69 6c 65 2d 67   a single file-g
ca500 6c 6f 62 61 6c 20 65 78 63 6c 75 73 69 76 65 20  lobal exclusive 
ca510 6c 6f 63 6b 0a 2a 2a 20 44 4f 54 4c 4f 43 4b 20  lock.** DOTLOCK 
ca520 69 73 6e 27 74 20 61 20 74 72 75 65 20 6c 6f 63  isn't a true loc
ca530 6b 69 6e 67 20 73 74 79 6c 65 2c 20 69 74 20 72  king style, it r
ca540 65 66 65 72 73 20 74 6f 20 74 68 65 20 75 73 65  efers to the use
ca550 20 6f 66 20 61 20 73 70 65 63 69 61 6c 0a 2a 2a   of a special.**
ca560 20 20 20 66 69 6c 65 20 6e 61 6d 65 64 20 74 68     file named th
ca570 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 64 61  e same as the da
ca580 74 61 62 61 73 65 20 66 69 6c 65 20 77 69 74 68  tabase file with
ca590 20 61 20 27 2e 6c 6f 63 6b 27 20 65 78 74 65 6e   a '.lock' exten
ca5a0 73 69 6f 6e 2c 20 74 68 69 73 0a 2a 2a 20 20 20  sion, this.**   
ca5b0 63 61 6e 20 62 65 20 75 73 65 64 20 6f 6e 20 66  can be used on f
ca5c0 69 6c 65 20 73 79 73 74 65 6d 73 20 74 68 61 74  ile systems that
ca5d0 20 64 6f 20 6e 6f 74 20 6f 66 66 65 72 20 61 6e   do not offer an
ca5e0 79 20 72 65 6c 69 61 62 6c 65 20 66 69 6c 65 20  y reliable file 
ca5f0 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 4e 4f 20 6c 6f  locking.** NO lo
ca600 63 6b 69 6e 67 20 6d 65 61 6e 73 20 74 68 61 74  cking means that
ca610 20 6e 6f 20 6c 6f 63 6b 69 6e 67 20 77 69 6c 6c   no locking will
ca620 20 62 65 20 61 74 74 65 6d 70 74 65 64 2c 20 74   be attempted, t
ca630 68 69 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 64  his is only used
ca640 20 66 6f 72 0a 2a 2a 20 20 20 72 65 61 64 2d 6f   for.**   read-o
ca650 6e 6c 79 20 66 69 6c 65 20 73 79 73 74 65 6d 73  nly file systems
ca660 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 55 4e   currently.** UN
ca670 53 55 50 50 4f 52 54 45 44 20 6d 65 61 6e 73 20  SUPPORTED means 
ca680 74 68 61 74 20 6e 6f 20 6c 6f 63 6b 69 6e 67 20  that no locking 
ca690 77 69 6c 6c 20 62 65 20 61 74 74 65 6d 70 74 65  will be attempte
ca6a0 64 2c 20 74 68 69 73 20 69 73 20 6f 6e 6c 79 20  d, this is only 
ca6b0 75 73 65 64 20 66 6f 72 0a 2a 2a 20 20 20 66 69  used for.**   fi
ca6c0 6c 65 20 73 79 73 74 65 6d 73 20 74 68 61 74 20  le systems that 
ca6d0 61 72 65 20 6b 6e 6f 77 6e 20 74 6f 20 62 65 20  are known to be 
ca6e0 75 6e 73 75 70 70 6f 72 74 65 64 0a 2a 2f 0a 23  unsupported.*/.#
ca6f0 64 65 66 69 6e 65 20 4c 4f 43 4b 49 4e 47 5f 53  define LOCKING_S
ca700 54 59 4c 45 5f 50 4f 53 49 58 20 20 20 20 20 20  TYLE_POSIX      
ca710 20 20 31 0a 23 64 65 66 69 6e 65 20 4c 4f 43 4b    1.#define LOCK
ca720 49 4e 47 5f 53 54 59 4c 45 5f 4e 4f 4e 45 20 20  ING_STYLE_NONE  
ca730 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65         2.#define
ca740 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 44   LOCKING_STYLE_D
ca750 4f 54 46 49 4c 45 20 20 20 20 20 20 33 0a 23 64  OTFILE      3.#d
ca760 65 66 69 6e 65 20 4c 4f 43 4b 49 4e 47 5f 53 54  efine LOCKING_ST
ca770 59 4c 45 5f 46 4c 4f 43 4b 20 20 20 20 20 20 20  YLE_FLOCK       
ca780 20 34 0a 23 64 65 66 69 6e 65 20 4c 4f 43 4b 49   4.#define LOCKI
ca790 4e 47 5f 53 54 59 4c 45 5f 41 46 50 20 20 20 20  NG_STYLE_AFP    
ca7a0 20 20 20 20 20 20 35 0a 0a 2f 2a 0a 2a 2a 20 4f        5../*.** O
ca7b0 6e 6c 79 20 73 65 74 20 74 68 65 20 6c 61 73 74  nly set the last
ca7c0 45 72 72 6e 6f 20 69 66 20 74 68 65 20 65 72 72  Errno if the err
ca7d0 6f 72 20 63 6f 64 65 20 69 73 20 61 20 72 65 61  or code is a rea
ca7e0 6c 20 65 72 72 6f 72 20 61 6e 64 20 6e 6f 74 20  l error and not 
ca7f0 0a 2a 2a 20 61 20 6e 6f 72 6d 61 6c 20 65 78 70  .** a normal exp
ca800 65 63 74 65 64 20 72 65 74 75 72 6e 20 63 6f 64  ected return cod
ca810 65 20 6f 66 20 53 51 4c 49 54 45 5f 42 55 53 59  e of SQLITE_BUSY
ca820 20 6f 72 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2f   or SQLITE_OK.*/
ca830 0a 23 64 65 66 69 6e 65 20 49 53 5f 4c 4f 43 4b  .#define IS_LOCK
ca840 5f 45 52 52 4f 52 28 78 29 20 20 28 28 78 20 21  _ERROR(x)  ((x !
ca850 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 20 26 26 20  = SQLITE_OK) && 
ca860 28 78 20 21 3d 20 53 51 4c 49 54 45 5f 42 55 53  (x != SQLITE_BUS
ca870 59 29 29 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65  Y))../*.** Helpe
ca880 72 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 6f  r functions to o
ca890 62 74 61 69 6e 20 61 6e 64 20 72 65 6c 69 6e 71  btain and relinq
ca8a0 75 69 73 68 20 74 68 65 20 67 6c 6f 62 61 6c 20  uish the global 
ca8b0 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63  mutex..*/.static
ca8c0 20 76 6f 69 64 20 65 6e 74 65 72 4d 75 74 65 78   void enterMutex
ca8d0 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65  (void){.  sqlite
ca8e0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71  3_mutex_enter(sq
ca8f0 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
ca900 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
ca910 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 7d 0a  TIC_MASTER));.}.
ca920 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65 61 76  static void leav
ca930 65 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20  eMutex(void){.  
ca940 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
ca950 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78  ave(sqlite3Mutex
ca960 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
ca970 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
ca980 29 29 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54  ));.}..#if SQLIT
ca990 45 5f 54 48 52 45 41 44 53 41 46 45 0a 2f 2a 0a  E_THREADSAFE./*.
ca9a0 2a 2a 20 54 68 69 73 20 76 61 72 69 61 62 6c 65  ** This variable
ca9b0 20 72 65 63 6f 72 64 73 20 77 68 65 74 68 65 72   records whether
ca9c0 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64 73 20   or not threads 
ca9d0 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63  can override eac
ca9e0 68 20 6f 74 68 65 72 73 0a 2a 2a 20 6c 6f 63 6b  h others.** lock
ca9f0 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 3a 20 20  s..**.**    0:  
caa00 4e 6f 2e 20 20 54 68 72 65 61 64 73 20 63 61 6e  No.  Threads can
caa10 6e 6f 74 20 6f 76 65 72 72 69 64 65 20 65 61 63  not override eac
caa20 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e 0a  h others locks..
caa30 2a 2a 20 20 20 20 31 3a 20 20 59 65 73 2e 20 20  **    1:  Yes.  
caa40 54 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72  Threads can over
caa50 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73  ride each others
caa60 20 6c 6f 63 6b 73 2e 0a 2a 2a 20 20 20 2d 31 3a   locks..**   -1:
caa70 20 20 57 65 20 64 6f 6e 27 74 20 6b 6e 6f 77 20    We don't know 
caa80 79 65 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 6f  yet..**.** On so
caa90 6d 65 20 73 79 73 74 65 6d 73 2c 20 77 65 20 6b  me systems, we k
caaa0 6e 6f 77 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74  now at compile-t
caab0 69 6d 65 20 69 66 20 74 68 72 65 61 64 73 20 63  ime if threads c
caac0 61 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63 68  an override each
caad0 0a 2a 2a 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73  .** others locks
caae0 2e 20 20 4f 6e 20 74 68 6f 73 65 20 73 79 73 74  .  On those syst
caaf0 65 6d 73 2c 20 74 68 65 20 53 51 4c 49 54 45 5f  ems, the SQLITE_
cab00 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f  THREAD_OVERRIDE_
cab10 4c 4f 43 4b 20 6d 61 63 72 6f 0a 2a 2a 20 77 69  LOCK macro.** wi
cab20 6c 6c 20 62 65 20 73 65 74 20 61 70 70 72 6f 70  ll be set approp
cab30 72 69 61 74 65 6c 79 2e 20 20 4f 6e 20 6f 74 68  riately.  On oth
cab40 65 72 20 73 79 73 74 65 6d 73 2c 20 77 65 20 68  er systems, we h
cab50 61 76 65 20 74 6f 20 63 68 65 63 6b 20 61 74 0a  ave to check at.
cab60 2a 2a 20 72 75 6e 74 69 6d 65 2e 20 20 4f 6e 20  ** runtime.  On 
cab70 74 68 65 73 65 20 6c 61 74 74 65 72 20 73 79 73  these latter sys
cab80 74 65 6d 73 2c 20 53 51 4c 54 49 45 5f 54 48 52  tems, SQLTIE_THR
cab90 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43  EAD_OVERRIDE_LOC
caba0 4b 20 69 73 0a 2a 2a 20 75 6e 64 65 66 69 6e 65  K is.** undefine
cabb0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 61  d..**.** This va
cabc0 72 69 61 62 6c 65 20 6e 6f 72 6d 61 6c 6c 79 20  riable normally 
cabd0 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65 20 6f  has file scope o
cabe0 6e 6c 79 2e 20 20 42 75 74 20 64 75 72 69 6e 67  nly.  But during
cabf0 20 74 65 73 74 69 6e 67 2c 20 77 65 20 6d 61 6b   testing, we mak
cac00 65 0a 2a 2a 20 69 74 20 61 20 67 6c 6f 62 61 6c  e.** it a global
cac10 20 73 6f 20 74 68 61 74 20 74 68 65 20 74 65 73   so that the tes
cac20 74 20 63 6f 64 65 20 63 61 6e 20 63 68 61 6e 67  t code can chang
cac30 65 20 69 74 73 20 76 61 6c 75 65 20 69 6e 20 6f  e its value in o
cac40 72 64 65 72 20 74 6f 20 76 65 72 69 66 79 0a 2a  rder to verify.*
cac50 2a 20 74 68 61 74 20 74 68 65 20 72 69 67 68 74  * that the right
cac60 20 73 74 75 66 66 20 68 61 70 70 65 6e 73 20 69   stuff happens i
cac70 6e 20 65 69 74 68 65 72 20 63 61 73 65 2e 0a 2a  n either case..*
cac80 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
cac90 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45  _THREAD_OVERRIDE
caca0 5f 4c 4f 43 4b 0a 23 20 64 65 66 69 6e 65 20 53  _LOCK.# define S
cacb0 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45  QLITE_THREAD_OVE
cacc0 52 52 49 44 45 5f 4c 4f 43 4b 20 2d 31 0a 23 65  RRIDE_LOCK -1.#e
cacd0 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
cace0 54 45 5f 54 45 53 54 0a 69 6e 74 20 74 68 72 65  TE_TEST.int thre
cacf0 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f  adsOverrideEachO
cad00 74 68 65 72 73 4c 6f 63 6b 73 20 3d 20 53 51 4c  thersLocks = SQL
cad10 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52  ITE_THREAD_OVERR
cad20 49 44 45 5f 4c 4f 43 4b 3b 0a 23 65 6c 73 65 0a  IDE_LOCK;.#else.
cad30 73 74 61 74 69 63 20 69 6e 74 20 74 68 72 65 61  static int threa
cad40 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74  dsOverrideEachOt
cad50 68 65 72 73 4c 6f 63 6b 73 20 3d 20 53 51 4c 49  hersLocks = SQLI
cad60 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49  TE_THREAD_OVERRI
cad70 44 45 5f 4c 4f 43 4b 3b 0a 23 65 6e 64 69 66 0a  DE_LOCK;.#endif.
cad80 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75  ./*.** This stru
cad90 63 74 75 72 65 20 68 6f 6c 64 73 20 69 6e 66 6f  cture holds info
cada0 72 6d 61 74 69 6f 6e 20 70 61 73 73 65 64 20 69  rmation passed i
cadb0 6e 74 6f 20 69 6e 64 69 76 69 64 75 61 6c 20 74  nto individual t
cadc0 65 73 74 0a 2a 2a 20 74 68 72 65 61 64 73 20 62  est.** threads b
cadd0 79 20 74 68 65 20 74 65 73 74 54 68 72 65 61 64  y the testThread
cade0 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28  LockingBehavior(
cadf0 29 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 73 74  ) routine..*/.st
cae00 72 75 63 74 20 74 68 72 65 61 64 54 65 73 74 44  ruct threadTestD
cae10 61 74 61 20 7b 0a 20 20 69 6e 74 20 66 64 3b 20  ata {.  int fd; 
cae20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
cae30 2a 20 46 69 6c 65 20 74 6f 20 62 65 20 6c 6f 63  * File to be loc
cae40 6b 65 64 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ked */.  struct 
cae50 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20 20 20  flock lock;     
cae60 2f 2a 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6f  /* The locking o
cae70 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  peration */.  in
cae80 74 20 72 65 73 75 6c 74 3b 20 20 20 20 20 20 20  t result;       
cae90 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f       /* Result o
caea0 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6f 70  f the locking op
caeb0 65 72 61 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 23  eration */.};..#
caec0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4c 4f 43  ifdef SQLITE_LOC
caed0 4b 5f 54 52 41 43 45 0a 2f 2a 0a 2a 2a 20 50 72  K_TRACE./*.** Pr
caee0 69 6e 74 20 6f 75 74 20 69 6e 66 6f 72 6d 61 74  int out informat
caef0 69 6f 6e 20 61 62 6f 75 74 20 61 6c 6c 20 6c 6f  ion about all lo
caf00 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73  cking operations
caf10 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
caf20 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72  tine is used for
caf30 20 74 72 6f 75 62 6c 65 73 68 6f 6f 74 69 6e 67   troubleshooting
caf40 20 6c 6f 63 6b 73 20 6f 6e 20 6d 75 6c 74 69 74   locks on multit
caf50 68 72 65 61 64 65 64 0a 2a 2a 20 70 6c 61 74 66  hreaded.** platf
caf60 6f 72 6d 73 2e 20 20 45 6e 61 62 6c 65 20 62 79  orms.  Enable by
caf70 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69 74 68 20   compiling with 
caf80 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 4c 4f 43  the -DSQLITE_LOC
caf90 4b 5f 54 52 41 43 45 0a 2a 2a 20 63 6f 6d 6d 61  K_TRACE.** comma
cafa0 6e 64 2d 6c 69 6e 65 20 6f 70 74 69 6f 6e 20 6f  nd-line option o
cafb0 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 2e 20  n the compiler. 
cafc0 20 54 68 69 73 20 63 6f 64 65 20 69 73 20 6e 6f   This code is no
cafd0 72 6d 61 6c 6c 79 0a 2a 2a 20 74 75 72 6e 65 64  rmally.** turned
cafe0 20 6f 66 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   off..*/.static 
caff0 69 6e 74 20 6c 6f 63 6b 54 72 61 63 65 28 69 6e  int lockTrace(in
cb000 74 20 66 64 2c 20 69 6e 74 20 6f 70 2c 20 73 74  t fd, int op, st
cb010 72 75 63 74 20 66 6c 6f 63 6b 20 2a 70 29 7b 0a  ruct flock *p){.
cb020 20 20 63 68 61 72 20 2a 7a 4f 70 4e 61 6d 65 2c    char *zOpName,
cb030 20 2a 7a 54 79 70 65 3b 0a 20 20 69 6e 74 20 73   *zType;.  int s
cb040 3b 0a 20 20 69 6e 74 20 73 61 76 65 64 45 72 72  ;.  int savedErr
cb050 6e 6f 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 46 5f  no;.  if( op==F_
cb060 47 45 54 4c 4b 20 29 7b 0a 20 20 20 20 7a 4f 70  GETLK ){.    zOp
cb070 4e 61 6d 65 20 3d 20 22 47 45 54 4c 4b 22 3b 0a  Name = "GETLK";.
cb080 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d    }else if( op==
cb090 46 5f 53 45 54 4c 4b 20 29 7b 0a 20 20 20 20 7a  F_SETLK ){.    z
cb0a0 4f 70 4e 61 6d 65 20 3d 20 22 53 45 54 4c 4b 22  OpName = "SETLK"
cb0b0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
cb0c0 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 6f 70 2c   = fcntl(fd, op,
cb0d0 20 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   p);.    sqlite3
cb0e0 44 65 62 75 67 50 72 69 6e 74 66 28 22 66 63 6e  DebugPrintf("fcn
cb0f0 74 6c 20 75 6e 6b 6e 6f 77 6e 20 25 64 20 25 64  tl unknown %d %d
cb100 20 25 64 5c 6e 22 2c 20 66 64 2c 20 6f 70 2c 20   %d\n", fd, op, 
cb110 73 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 73  s);.    return s
cb120 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 6c  ;.  }.  if( p->l
cb130 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b 20 29  _type==F_RDLCK )
cb140 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22 52  {.    zType = "R
cb150 44 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 69  DLCK";.  }else i
cb160 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f  f( p->l_type==F_
cb170 57 52 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 79  WRLCK ){.    zTy
cb180 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a 20 20  pe = "WRLCK";.  
cb190 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6c 5f 74  }else if( p->l_t
cb1a0 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a  ype==F_UNLCK ){.
cb1b0 20 20 20 20 7a 54 79 70 65 20 3d 20 22 55 4e 4c      zType = "UNL
cb1c0 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  CK";.  }else{.  
cb1d0 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20    assert( 0 );. 
cb1e0 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e   }.  assert( p->
cb1f0 6c 5f 77 68 65 6e 63 65 3d 3d 53 45 45 4b 5f 53  l_whence==SEEK_S
cb200 45 54 20 29 3b 0a 20 20 73 20 3d 20 66 63 6e 74  ET );.  s = fcnt
cb210 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0a 20 20  l(fd, op, p);.  
cb220 73 61 76 65 64 45 72 72 6e 6f 20 3d 20 65 72 72  savedErrno = err
cb230 6e 6f 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 62  no;.  sqlite3Deb
cb240 75 67 50 72 69 6e 74 66 28 22 66 63 6e 74 6c 20  ugPrintf("fcntl 
cb250 25 64 20 25 64 20 25 73 20 25 73 20 25 64 20 25  %d %d %s %s %d %
cb260 64 20 25 64 20 25 64 5c 6e 22 2c 0a 20 20 20 20  d %d %d\n",.    
cb270 20 74 68 72 65 61 64 69 64 2c 20 66 64 2c 20 7a   threadid, fd, z
cb280 4f 70 4e 61 6d 65 2c 20 7a 54 79 70 65 2c 20 28  OpName, zType, (
cb290 69 6e 74 29 70 2d 3e 6c 5f 73 74 61 72 74 2c 20  int)p->l_start, 
cb2a0 28 69 6e 74 29 70 2d 3e 6c 5f 6c 65 6e 2c 0a 20  (int)p->l_len,. 
cb2b0 20 20 20 20 28 69 6e 74 29 70 2d 3e 6c 5f 70 69      (int)p->l_pi
cb2c0 64 2c 20 73 29 3b 0a 20 20 69 66 28 20 73 3d 3d  d, s);.  if( s==
cb2d0 28 2d 31 29 20 26 26 20 6f 70 3d 3d 46 5f 53 45  (-1) && op==F_SE
cb2e0 54 4c 4b 20 26 26 20 28 70 2d 3e 6c 5f 74 79 70  TLK && (p->l_typ
cb2f0 65 3d 3d 46 5f 52 44 4c 43 4b 20 7c 7c 20 70 2d  e==F_RDLCK || p-
cb300 3e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b  >l_type==F_WRLCK
cb310 29 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  ) ){.    struct 
cb320 66 6c 6f 63 6b 20 6c 32 3b 0a 20 20 20 20 6c 32  flock l2;.    l2
cb330 20 3d 20 2a 70 3b 0a 20 20 20 20 66 63 6e 74 6c   = *p;.    fcntl
cb340 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c  (fd, F_GETLK, &l
cb350 32 29 3b 0a 20 20 20 20 69 66 28 20 6c 32 2e 6c  2);.    if( l2.l
cb360 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b 20 29  _type==F_RDLCK )
cb370 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20  {.      zType = 
cb380 22 52 44 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c  "RDLCK";.    }el
cb390 73 65 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65  se if( l2.l_type
cb3a0 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b 0a 20 20 20  ==F_WRLCK ){.   
cb3b0 20 20 20 7a 54 79 70 65 20 3d 20 22 57 52 4c 43     zType = "WRLC
cb3c0 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  K";.    }else if
cb3d0 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 55  ( l2.l_type==F_U
cb3e0 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54  NLCK ){.      zT
cb3f0 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20  ype = "UNLCK";. 
cb400 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
cb410 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20 20  assert( 0 );.   
cb420 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65   }.    sqlite3De
cb430 62 75 67 50 72 69 6e 74 66 28 22 66 63 6e 74 6c  bugPrintf("fcntl
cb440 2d 66 61 69 6c 75 72 65 2d 72 65 61 73 6f 6e 3a  -failure-reason:
cb450 20 25 73 20 25 64 20 25 64 20 25 64 5c 6e 22 2c   %s %d %d %d\n",
cb460 0a 20 20 20 20 20 20 20 7a 54 79 70 65 2c 20 28  .       zType, (
cb470 69 6e 74 29 6c 32 2e 6c 5f 73 74 61 72 74 2c 20  int)l2.l_start, 
cb480 28 69 6e 74 29 6c 32 2e 6c 5f 6c 65 6e 2c 20 28  (int)l2.l_len, (
cb490 69 6e 74 29 6c 32 2e 6c 5f 70 69 64 29 3b 0a 20  int)l2.l_pid);. 
cb4a0 20 7d 0a 20 20 65 72 72 6e 6f 20 3d 20 73 61 76   }.  errno = sav
cb4b0 65 64 45 72 72 6e 6f 3b 0a 20 20 72 65 74 75 72  edErrno;.  retur
cb4c0 6e 20 73 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 66  n s;.}.#define f
cb4d0 63 6e 74 6c 20 6c 6f 63 6b 54 72 61 63 65 0a 23  cntl lockTrace.#
cb4e0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
cb4f0 4c 4f 43 4b 5f 54 52 41 43 45 20 2a 2f 0a 0a 2f  LOCK_TRACE */../
cb500 2a 0a 2a 2a 20 54 68 65 20 74 65 73 74 54 68 72  *.** The testThr
cb510 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69  eadLockingBehavi
cb520 6f 72 28 29 20 72 6f 75 74 69 6e 65 20 6c 61 75  or() routine lau
cb530 6e 63 68 65 73 20 74 77 6f 20 73 65 70 61 72 61  nches two separa
cb540 74 65 0a 2a 2a 20 74 68 72 65 61 64 73 20 6f 6e  te.** threads on
cb550 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20   this routine.  
cb560 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 74 74  This routine att
cb570 65 6d 70 74 73 20 74 6f 20 6c 6f 63 6b 20 61 20  empts to lock a 
cb580 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 74  file.** descript
cb590 6f 72 20 74 68 65 6e 20 72 65 74 75 72 6e 73 2e  or then returns.
cb5a0 20 20 54 68 65 20 73 75 63 63 65 73 73 20 6f 72    The success or
cb5b0 20 66 61 69 6c 75 72 65 20 6f 66 20 74 68 61 74   failure of that
cb5c0 20 61 74 74 65 6d 70 74 0a 2a 2a 20 61 6c 6c 6f   attempt.** allo
cb5d0 77 73 20 74 68 65 20 74 65 73 74 54 68 72 65 61  ws the testThrea
cb5e0 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72  dLockingBehavior
cb5f0 28 29 20 70 72 6f 63 65 64 75 72 65 20 74 6f 20  () procedure to 
cb600 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 68 65  determine.** whe
cb610 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65  ther or not thre
cb620 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65  ads can override
cb630 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63   each others loc
cb640 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ks..*/.static vo
cb650 69 64 20 2a 74 68 72 65 61 64 4c 6f 63 6b 69 6e  id *threadLockin
cb660 67 54 65 73 74 28 76 6f 69 64 20 2a 70 41 72 67  gTest(void *pArg
cb670 29 7b 0a 20 20 73 74 72 75 63 74 20 74 68 72 65  ){.  struct thre
cb680 61 64 54 65 73 74 44 61 74 61 20 2a 70 44 61 74  adTestData *pDat
cb690 61 20 3d 20 28 73 74 72 75 63 74 20 74 68 72 65  a = (struct thre
cb6a0 61 64 54 65 73 74 44 61 74 61 2a 29 70 41 72 67  adTestData*)pArg
cb6b0 3b 0a 20 20 70 44 61 74 61 2d 3e 72 65 73 75 6c  ;.  pData->resul
cb6c0 74 20 3d 20 66 63 6e 74 6c 28 70 44 61 74 61 2d  t = fcntl(pData-
cb6d0 3e 66 64 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 70  >fd, F_SETLK, &p
cb6e0 44 61 74 61 2d 3e 6c 6f 63 6b 29 3b 0a 20 20 72  Data->lock);.  r
cb6f0 65 74 75 72 6e 20 70 41 72 67 3b 0a 7d 0a 0a 2f  eturn pArg;.}../
cb700 2a 0a 2a 2a 20 54 68 69 73 20 70 72 6f 63 65 64  *.** This proced
cb710 75 72 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ure attempts to 
cb720 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65  determine whethe
cb730 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64 73  r or not threads
cb740 0a 2a 2a 20 63 61 6e 20 6f 76 65 72 72 69 64 65  .** can override
cb750 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63   each others loc
cb760 6b 73 20 74 68 65 6e 20 73 65 74 73 20 74 68 65  ks then sets the
cb770 20 0a 2a 2a 20 74 68 72 65 61 64 73 4f 76 65 72   .** threadsOver
cb780 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f  rideEachOthersLo
cb790 63 6b 73 20 76 61 72 69 61 62 6c 65 20 61 70 70  cks variable app
cb7a0 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f 0a 73  ropriately..*/.s
cb7b0 74 61 74 69 63 20 76 6f 69 64 20 74 65 73 74 54  tatic void testT
cb7c0 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61  hreadLockingBeha
cb7d0 76 69 6f 72 28 69 6e 74 20 66 64 5f 6f 72 69 67  vior(int fd_orig
cb7e0 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 73  ){.  int fd;.  s
cb7f0 74 72 75 63 74 20 74 68 72 65 61 64 54 65 73 74  truct threadTest
cb800 44 61 74 61 20 64 5b 32 5d 3b 0a 20 20 70 74 68  Data d[2];.  pth
cb810 72 65 61 64 5f 74 20 74 5b 32 5d 3b 0a 0a 20 20  read_t t[2];..  
cb820 66 64 20 3d 20 64 75 70 28 66 64 5f 6f 72 69 67  fd = dup(fd_orig
cb830 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 29 20  );.  if( fd<0 ) 
cb840 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d 73 65 74  return;.  memset
cb850 28 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 29  (d, 0, sizeof(d)
cb860 29 3b 0a 20 20 64 5b 30 5d 2e 66 64 20 3d 20 66  );.  d[0].fd = f
cb870 64 3b 0a 20 20 64 5b 30 5d 2e 6c 6f 63 6b 2e 6c  d;.  d[0].lock.l
cb880 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b  _type = F_RDLCK;
cb890 0a 20 20 64 5b 30 5d 2e 6c 6f 63 6b 2e 6c 5f 6c  .  d[0].lock.l_l
cb8a0 65 6e 20 3d 20 31 3b 0a 20 20 64 5b 30 5d 2e 6c  en = 1;.  d[0].l
cb8b0 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b  ock.l_start = 0;
cb8c0 0a 20 20 64 5b 30 5d 2e 6c 6f 63 6b 2e 6c 5f 77  .  d[0].lock.l_w
cb8d0 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54  hence = SEEK_SET
cb8e0 3b 0a 20 20 64 5b 31 5d 20 3d 20 64 5b 30 5d 3b  ;.  d[1] = d[0];
cb8f0 0a 20 20 64 5b 31 5d 2e 6c 6f 63 6b 2e 6c 5f 74  .  d[1].lock.l_t
cb900 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20  ype = F_WRLCK;. 
cb910 20 70 74 68 72 65 61 64 5f 63 72 65 61 74 65 28   pthread_create(
cb920 26 74 5b 30 5d 2c 20 30 2c 20 74 68 72 65 61 64  &t[0], 0, thread
cb930 4c 6f 63 6b 69 6e 67 54 65 73 74 2c 20 26 64 5b  LockingTest, &d[
cb940 30 5d 29 3b 0a 20 20 70 74 68 72 65 61 64 5f 63  0]);.  pthread_c
cb950 72 65 61 74 65 28 26 74 5b 31 5d 2c 20 30 2c 20  reate(&t[1], 0, 
cb960 74 68 72 65 61 64 4c 6f 63 6b 69 6e 67 54 65 73  threadLockingTes
cb970 74 2c 20 26 64 5b 31 5d 29 3b 0a 20 20 70 74 68  t, &d[1]);.  pth
cb980 72 65 61 64 5f 6a 6f 69 6e 28 74 5b 30 5d 2c 20  read_join(t[0], 
cb990 30 29 3b 0a 20 20 70 74 68 72 65 61 64 5f 6a 6f  0);.  pthread_jo
cb9a0 69 6e 28 74 5b 31 5d 2c 20 30 29 3b 0a 20 20 63  in(t[1], 0);.  c
cb9b0 6c 6f 73 65 28 66 64 29 3b 0a 20 20 74 68 72 65  lose(fd);.  thre
cb9c0 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f  adsOverrideEachO
cb9d0 74 68 65 72 73 4c 6f 63 6b 73 20 3d 20 20 64 5b  thersLocks =  d[
cb9e0 30 5d 2e 72 65 73 75 6c 74 3d 3d 30 20 26 26 20  0].result==0 && 
cb9f0 64 5b 31 5d 2e 72 65 73 75 6c 74 3d 3d 30 3b 0a  d[1].result==0;.
cba00 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
cba10 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 2a 2f  TE_THREADSAFE */
cba20 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20  ../*.** Release 
cba30 61 20 6c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63  a lockInfo struc
cba40 74 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20  ture previously 
cba50 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 66 69 6e  allocated by fin
cba60 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2f 0a  dLockInfo()..*/.
cba70 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65  static void rele
cba80 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 73 74 72 75  aseLockInfo(stru
cba90 63 74 20 6c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f  ct lockInfo *pLo
cbaa0 63 6b 29 7b 0a 20 20 69 66 28 20 70 4c 6f 63 6b  ck){.  if( pLock
cbab0 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e   ){.    pLock->n
cbac0 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70  Ref--;.    if( p
cbad0 4c 6f 63 6b 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b  Lock->nRef==0 ){
cbae0 0a 20 20 20 20 20 20 69 66 28 20 70 4c 6f 63 6b  .      if( pLock
cbaf0 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 20  ->pPrev ){.     
cbb00 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b     assert( pLock
cbb10 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d  ->pPrev->pNext==
cbb20 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20 20  pLock );.       
cbb30 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 2d 3e 70   pLock->pPrev->p
cbb40 4e 65 78 74 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e  Next = pLock->pN
cbb50 65 78 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  ext;.      }else
cbb60 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
cbb70 28 20 6c 6f 63 6b 4c 69 73 74 3d 3d 70 4c 6f 63  ( lockList==pLoc
cbb80 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63  k );.        loc
cbb90 6b 4c 69 73 74 20 3d 20 70 4c 6f 63 6b 2d 3e 70  kList = pLock->p
cbba0 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Next;.      }.  
cbbb0 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70      if( pLock->p
cbbc0 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Next ){.        
cbbd0 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70  assert( pLock->p
cbbe0 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 4c 6f  Next->pPrev==pLo
cbbf0 63 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4c  ck );.        pL
cbc00 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65  ock->pNext->pPre
cbc10 76 20 3d 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76  v = pLock->pPrev
cbc20 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
cbc30 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4c 6f  sqlite3_free(pLo
cbc40 63 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  ck);.    }.  }.}
cbc50 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20  ../*.** Release 
cbc60 61 20 6f 70 65 6e 43 6e 74 20 73 74 72 75 63 74  a openCnt struct
cbc70 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61  ure previously a
cbc80 6c 6c 6f 63 61 74 65 64 20 62 79 20 66 69 6e 64  llocated by find
cbc90 4c 6f 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2f 0a 73  LockInfo()..*/.s
cbca0 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61  tatic void relea
cbcb0 73 65 4f 70 65 6e 43 6e 74 28 73 74 72 75 63 74  seOpenCnt(struct
cbcc0 20 6f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 29   openCnt *pOpen)
cbcd0 7b 0a 20 20 69 66 28 20 70 4f 70 65 6e 20 29 7b  {.  if( pOpen ){
cbce0 0a 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66  .    pOpen->nRef
cbcf0 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 65  --;.    if( pOpe
cbd00 6e 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20  n->nRef==0 ){.  
cbd10 20 20 20 20 69 66 28 20 70 4f 70 65 6e 2d 3e 70      if( pOpen->p
cbd20 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20 20 20  Prev ){.        
cbd30 61 73 73 65 72 74 28 20 70 4f 70 65 6e 2d 3e 70  assert( pOpen->p
cbd40 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 4f 70  Prev->pNext==pOp
cbd50 65 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4f  en );.        pO
cbd60 70 65 6e 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78  pen->pPrev->pNex
cbd70 74 20 3d 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74  t = pOpen->pNext
cbd80 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
cbd90 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6f         assert( o
cbda0 70 65 6e 4c 69 73 74 3d 3d 70 4f 70 65 6e 20 29  penList==pOpen )
cbdb0 3b 0a 20 20 20 20 20 20 20 20 6f 70 65 6e 4c 69  ;.        openLi
cbdc0 73 74 20 3d 20 70 4f 70 65 6e 2d 3e 70 4e 65 78  st = pOpen->pNex
cbdd0 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  t;.      }.     
cbde0 20 69 66 28 20 70 4f 70 65 6e 2d 3e 70 4e 65 78   if( pOpen->pNex
cbdf0 74 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  t ){.        ass
cbe00 65 72 74 28 20 70 4f 70 65 6e 2d 3e 70 4e 65 78  ert( pOpen->pNex
cbe10 74 2d 3e 70 50 72 65 76 3d 3d 70 4f 70 65 6e 20  t->pPrev==pOpen 
cbe20 29 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e  );.        pOpen
cbe30 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d  ->pNext->pPrev =
cbe40 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 3b 0a 20   pOpen->pPrev;. 
cbe50 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
cbe60 69 74 65 33 5f 66 72 65 65 28 70 4f 70 65 6e 2d  ite3_free(pOpen-
cbe70 3e 61 50 65 6e 64 69 6e 67 29 3b 0a 20 20 20 20  >aPending);.    
cbe80 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
cbe90 4f 70 65 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Open);.    }.  }
cbea0 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45  .}..#if SQLITE_E
cbeb0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
cbec0 59 4c 45 0a 2f 2a 0a 2a 2a 20 54 65 73 74 73 20  YLE./*.** Tests 
cbed0 61 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63  a byte-range loc
cbee0 6b 69 6e 67 20 71 75 65 72 79 20 74 6f 20 73 65  king query to se
cbef0 65 20 69 66 20 62 79 74 65 20 72 61 6e 67 65 20  e if byte range 
cbf00 6c 6f 63 6b 73 20 61 72 65 20 0a 2a 2a 20 73 75  locks are .** su
cbf10 70 70 6f 72 74 65 64 2c 20 69 66 20 6e 6f 74 20  pported, if not 
cbf20 77 65 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20  we fall back to 
cbf30 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 53 74  dotlockLockingSt
cbf40 79 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  yle..*/.static i
cbf50 6e 74 20 74 65 73 74 4c 6f 63 6b 69 6e 67 53 74  nt testLockingSt
cbf60 79 6c 65 28 69 6e 74 20 66 64 29 7b 0a 20 20 73  yle(int fd){.  s
cbf70 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b  truct flock lock
cbf80 49 6e 66 6f 3b 0a 0a 20 20 2f 2a 20 54 65 73 74  Info;..  /* Test
cbf90 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b   byte-range lock
cbfa0 20 75 73 69 6e 67 20 66 63 6e 74 6c 28 29 2e 20   using fcntl(). 
cbfb0 49 66 20 74 68 65 20 63 61 6c 6c 20 73 75 63 63  If the call succ
cbfc0 65 65 64 73 2c 20 0a 20 20 2a 2a 20 61 73 73 75  eeds, .  ** assu
cbfd0 6d 65 20 74 68 61 74 20 74 68 65 20 66 69 6c 65  me that the file
cbfe0 2d 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73  -system supports
cbff0 20 50 4f 53 49 58 20 73 74 79 6c 65 20 6c 6f 63   POSIX style loc
cc000 6b 73 2e 20 0a 20 20 2a 2f 0a 20 20 6c 6f 63 6b  ks. .  */.  lock
cc010 49 6e 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a  Info.l_len = 1;.
cc020 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 74 61    lockInfo.l_sta
cc030 72 74 20 3d 20 30 3b 0a 20 20 6c 6f 63 6b 49 6e  rt = 0;.  lockIn
cc040 66 6f 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45  fo.l_whence = SE
cc050 45 4b 5f 53 45 54 3b 0a 20 20 6c 6f 63 6b 49 6e  EK_SET;.  lockIn
cc060 66 6f 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44  fo.l_type = F_RD
cc070 4c 43 4b 3b 0a 20 20 69 66 28 20 66 63 6e 74 6c  LCK;.  if( fcntl
cc080 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c  (fd, F_GETLK, &l
cc090 6f 63 6b 49 6e 66 6f 29 21 3d 2d 31 20 29 20 7b  ockInfo)!=-1 ) {
cc0a0 0a 20 20 20 20 72 65 74 75 72 6e 20 4c 4f 43 4b  .    return LOCK
cc0b0 49 4e 47 5f 53 54 59 4c 45 5f 50 4f 53 49 58 3b  ING_STYLE_POSIX;
cc0c0 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 54 65 73  .  }.  .  /* Tes
cc0d0 74 69 6e 67 20 66 6f 72 20 66 6c 6f 63 6b 28 29  ting for flock()
cc0e0 20 63 61 6e 20 67 69 76 65 20 66 61 6c 73 65 20   can give false 
cc0f0 70 6f 73 69 74 69 76 65 73 2e 20 20 53 6f 20 69  positives.  So i
cc100 66 20 69 66 20 74 68 65 20 61 62 6f 76 65 20 0a  f if the above .
cc110 20 20 2a 2a 20 74 65 73 74 20 66 61 69 6c 73 2c    ** test fails,
cc120 20 74 68 65 6e 20 77 65 20 66 61 6c 6c 20 62 61   then we fall ba
cc130 63 6b 20 74 6f 20 75 73 69 6e 67 20 64 6f 74 2d  ck to using dot-
cc140 66 69 6c 65 20 73 74 79 6c 65 20 6c 6f 63 6b 69  file style locki
cc150 6e 67 2e 0a 20 20 2a 2f 20 20 0a 20 20 72 65 74  ng..  */  .  ret
cc160 75 72 6e 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  urn LOCKING_STYL
cc170 45 5f 44 4f 54 46 49 4c 45 3b 0a 7d 0a 23 65 6e  E_DOTFILE;.}.#en
cc180 64 69 66 0a 0a 2f 2a 20 0a 2a 2a 20 49 66 20 53  dif../* .** If S
cc190 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
cc1a0 4b 49 4e 47 5f 53 54 59 4c 45 20 69 73 20 64 65  KING_STYLE is de
cc1b0 66 69 6e 65 64 2c 20 74 68 69 73 20 66 75 6e 63  fined, this func
cc1c0 74 69 6f 6e 20 45 78 61 6d 69 6e 65 73 20 74 68  tion Examines th
cc1d0 65 20 0a 2a 2a 20 66 5f 66 73 74 79 70 65 6e 61  e .** f_fstypena
cc1e0 6d 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  me entry in the 
cc1f0 73 74 61 74 66 73 20 73 74 72 75 63 74 75 72 65  statfs structure
cc200 20 61 73 20 72 65 74 75 72 6e 65 64 20 62 79 20   as returned by 
cc210 73 74 61 74 28 29 20 66 6f 72 20 0a 2a 2a 20 74  stat() for .** t
cc220 68 65 20 66 69 6c 65 20 73 79 73 74 65 6d 20 68  he file system h
cc230 6f 73 74 69 6e 67 20 74 68 65 20 64 61 74 61 62  osting the datab
cc240 61 73 65 20 66 69 6c 65 20 61 6e 64 20 73 65 6c  ase file and sel
cc250 65 63 74 73 20 20 74 68 65 20 61 70 70 72 6f 70  ects  the approp
cc260 72 69 61 74 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67  riate.** locking
cc270 20 73 74 79 6c 65 20 62 61 73 65 64 20 6f 6e 20   style based on 
cc280 69 74 73 20 76 61 6c 75 65 2e 20 20 54 68 65 73  its value.  Thes
cc290 65 20 76 61 6c 75 65 73 20 61 6e 64 20 61 73 73  e values and ass
cc2a0 69 67 6e 6d 65 6e 74 73 20 61 72 65 20 0a 2a 2a  ignments are .**
cc2b0 20 62 61 73 65 64 20 6f 6e 20 44 61 72 77 69 6e   based on Darwin
cc2c0 2f 4f 53 58 20 62 65 68 61 76 69 6f 72 20 61 6e  /OSX behavior an
cc2d0 64 20 68 61 76 65 20 6e 6f 74 20 62 65 65 6e 20  d have not been 
cc2e0 74 68 6f 72 6f 75 67 68 6c 79 20 74 65 73 74 65  thoroughly teste
cc2f0 64 20 6f 6e 20 0a 2a 2a 20 6f 74 68 65 72 20 73  d on .** other s
cc300 79 73 74 65 6d 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  ystems..**.** If
cc310 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
cc320 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 69 73 20  OCKING_STYLE is 
cc330 6e 6f 74 20 64 65 66 69 6e 65 64 2c 20 74 68 69  not defined, thi
cc340 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79  s function alway
cc350 73 0a 2a 2a 20 72 65 74 75 72 6e 73 20 4c 4f 43  s.** returns LOC
cc360 4b 49 4e 47 5f 53 54 59 4c 45 5f 50 4f 53 49 58  KING_STYLE_POSIX
cc370 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
cc380 64 65 74 65 63 74 4c 6f 63 6b 69 6e 67 53 74 79  detectLockingSty
cc390 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  le(.  sqlite3_vf
cc3a0 73 20 2a 70 56 66 73 2c 0a 20 20 63 6f 6e 73 74  s *pVfs,.  const
cc3b0 20 63 68 61 72 20 2a 66 69 6c 65 50 61 74 68 2c   char *filePath,
cc3c0 20 0a 20 20 69 6e 74 20 66 64 0a 29 7b 0a 23 69   .  int fd.){.#i
cc3d0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
cc3e0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20  LOCKING_STYLE.  
cc3f0 73 74 72 75 63 74 20 4d 61 70 70 69 6e 67 20 7b  struct Mapping {
cc400 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
cc410 2a 7a 46 69 6c 65 73 79 73 74 65 6d 3b 0a 20 20  *zFilesystem;.  
cc420 20 20 69 6e 74 20 65 4c 6f 63 6b 69 6e 67 53 74    int eLockingSt
cc430 79 6c 65 3b 0a 20 20 7d 20 61 4d 61 70 5b 5d 20  yle;.  } aMap[] 
cc440 3d 20 7b 0a 20 20 20 20 7b 20 22 68 66 73 22 2c  = {.    { "hfs",
cc450 20 20 20 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c      LOCKING_STYL
cc460 45 5f 50 4f 53 49 58 20 7d 2c 0a 20 20 20 20 7b  E_POSIX },.    {
cc470 20 22 75 66 73 22 2c 20 20 20 20 4c 4f 43 4b 49   "ufs",    LOCKI
cc480 4e 47 5f 53 54 59 4c 45 5f 50 4f 53 49 58 20 7d  NG_STYLE_POSIX }
cc490 2c 0a 20 20 20 20 7b 20 22 61 66 70 66 73 22 2c  ,.    { "afpfs",
cc4a0 20 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f    LOCKING_STYLE_
cc4b0 41 46 50 20 7d 2c 0a 23 69 66 64 65 66 20 53 51  AFP },.#ifdef SQ
cc4c0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 46 50 5f  LITE_ENABLE_AFP_
cc4d0 4c 4f 43 4b 49 4e 47 5f 53 4d 42 0a 20 20 20 20  LOCKING_SMB.    
cc4e0 7b 20 22 73 6d 62 66 73 22 2c 20 20 4c 4f 43 4b  { "smbfs",  LOCK
cc4f0 49 4e 47 5f 53 54 59 4c 45 5f 41 46 50 20 7d 2c  ING_STYLE_AFP },
cc500 0a 23 65 6c 73 65 0a 20 20 20 20 7b 20 22 73 6d  .#else.    { "sm
cc510 62 66 73 22 2c 20 20 4c 4f 43 4b 49 4e 47 5f 53  bfs",  LOCKING_S
cc520 54 59 4c 45 5f 46 4c 4f 43 4b 20 7d 2c 0a 23 65  TYLE_FLOCK },.#e
cc530 6e 64 69 66 0a 20 20 20 20 7b 20 22 6d 73 64 6f  ndif.    { "msdo
cc540 73 22 2c 20 20 4c 4f 43 4b 49 4e 47 5f 53 54 59  s",  LOCKING_STY
cc550 4c 45 5f 44 4f 54 46 49 4c 45 20 7d 2c 0a 20 20  LE_DOTFILE },.  
cc560 20 20 7b 20 22 77 65 62 64 61 76 22 2c 20 4c 4f    { "webdav", LO
cc570 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e 4f 4e 45  CKING_STYLE_NONE
cc580 20 7d 2c 0a 20 20 20 20 7b 20 30 2c 20 30 20 7d   },.    { 0, 0 }
cc590 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  .  };.  int i;. 
cc5a0 20 73 74 72 75 63 74 20 73 74 61 74 66 73 20 66   struct statfs f
cc5b0 73 49 6e 66 6f 3b 0a 0a 20 20 69 66 28 20 21 66  sInfo;..  if( !f
cc5c0 69 6c 65 50 61 74 68 20 29 7b 0a 20 20 20 20 72  ilePath ){.    r
cc5d0 65 74 75 72 6e 20 4c 4f 43 4b 49 4e 47 5f 53 54  eturn LOCKING_ST
cc5e0 59 4c 45 5f 4e 4f 4e 45 3b 0a 20 20 7d 0a 20 20  YLE_NONE;.  }.  
cc5f0 69 66 28 20 70 56 66 73 2d 3e 70 41 70 70 44 61  if( pVfs->pAppDa
cc600 74 61 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ta ){.    return
cc610 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49   SQLITE_PTR_TO_I
cc620 4e 54 28 70 56 66 73 2d 3e 70 41 70 70 44 61 74  NT(pVfs->pAppDat
cc630 61 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73  a);.  }..  if( s
cc640 74 61 74 66 73 28 66 69 6c 65 50 61 74 68 2c 20  tatfs(filePath, 
cc650 26 66 73 49 6e 66 6f 29 20 21 3d 20 2d 31 20 29  &fsInfo) != -1 )
cc660 7b 0a 20 20 20 20 69 66 28 20 66 73 49 6e 66 6f  {.    if( fsInfo
cc670 2e 66 5f 66 6c 61 67 73 20 26 20 4d 4e 54 5f 52  .f_flags & MNT_R
cc680 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 72  DONLY ){.      r
cc690 65 74 75 72 6e 20 4c 4f 43 4b 49 4e 47 5f 53 54  eturn LOCKING_ST
cc6a0 59 4c 45 5f 4e 4f 4e 45 3b 0a 20 20 20 20 7d 0a  YLE_NONE;.    }.
cc6b0 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 61 4d 61      for(i=0; aMa
cc6c0 70 5b 69 5d 2e 7a 46 69 6c 65 73 79 73 74 65 6d  p[i].zFilesystem
cc6d0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
cc6e0 28 20 73 74 72 63 6d 70 28 66 73 49 6e 66 6f 2e  ( strcmp(fsInfo.
cc6f0 66 5f 66 73 74 79 70 65 6e 61 6d 65 2c 20 61 4d  f_fstypename, aM
cc700 61 70 5b 69 5d 2e 7a 46 69 6c 65 73 79 73 74 65  ap[i].zFilesyste
cc710 6d 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  m)==0 ){.       
cc720 20 72 65 74 75 72 6e 20 61 4d 61 70 5b 69 5d 2e   return aMap[i].
cc730 65 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20  eLockingStyle;. 
cc740 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
cc750 0a 0a 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 63  ..  /* Default c
cc760 61 73 65 2e 20 48 61 6e 64 6c 65 73 2c 20 61 6d  ase. Handles, am
cc770 6f 6e 67 73 74 20 6f 74 68 65 72 73 2c 20 22 6e  ongst others, "n
cc780 66 73 22 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e  fs". */.  return
cc790 20 74 65 73 74 4c 6f 63 6b 69 6e 67 53 74 79 6c   testLockingStyl
cc7a0 65 28 66 64 29 3b 20 20 0a 23 65 6e 64 69 66 0a  e(fd);  .#endif.
cc7b0 20 20 72 65 74 75 72 6e 20 4c 4f 43 4b 49 4e 47    return LOCKING
cc7c0 5f 53 54 59 4c 45 5f 50 4f 53 49 58 3b 0a 7d 0a  _STYLE_POSIX;.}.
cc7d0 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 66  ./*.** Given a f
cc7e0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2c 20  ile descriptor, 
cc7f0 6c 6f 63 61 74 65 20 6c 6f 63 6b 49 6e 66 6f 20  locate lockInfo 
cc800 61 6e 64 20 6f 70 65 6e 43 6e 74 20 73 74 72 75  and openCnt stru
cc810 63 74 75 72 65 73 20 74 68 61 74 0a 2a 2a 20 64  ctures that.** d
cc820 65 73 63 72 69 62 65 73 20 74 68 61 74 20 66 69  escribes that fi
cc830 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20  le descriptor.  
cc840 43 72 65 61 74 65 20 6e 65 77 20 6f 6e 65 73 20  Create new ones 
cc850 69 66 20 6e 65 63 65 73 73 61 72 79 2e 20 20 54  if necessary.  T
cc860 68 65 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c  he.** return val
cc870 75 65 73 20 6d 69 67 68 74 20 62 65 20 75 6e 69  ues might be uni
cc880 6e 69 74 69 61 6c 69 7a 65 64 20 69 66 20 61 6e  nitialized if an
cc890 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a   error occurs..*
cc8a0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 61  *.** Return an a
cc8b0 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72  ppropriate error
cc8c0 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   code..*/.static
cc8d0 20 69 6e 74 20 66 69 6e 64 4c 6f 63 6b 49 6e 66   int findLockInf
cc8e0 6f 28 0a 20 20 69 6e 74 20 66 64 2c 20 20 20 20  o(.  int fd,    
cc8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cc900 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65    /* The file de
cc910 73 63 72 69 70 74 6f 72 20 75 73 65 64 20 69 6e  scriptor used in
cc920 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20 20 73 74   the key */.  st
cc930 72 75 63 74 20 6c 6f 63 6b 49 6e 66 6f 20 2a 2a  ruct lockInfo **
cc940 70 70 4c 6f 63 6b 2c 20 20 20 20 2f 2a 20 52 65  ppLock,    /* Re
cc950 74 75 72 6e 20 74 68 65 20 6c 6f 63 6b 49 6e 66  turn the lockInf
cc960 6f 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65  o structure here
cc970 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 6f 70 65   */.  struct ope
cc980 6e 43 6e 74 20 2a 2a 70 70 4f 70 65 6e 20 20 20  nCnt **ppOpen   
cc990 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65     /* Return the
cc9a0 20 6f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75   openCnt structu
cc9b0 72 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  re here */.){.  
cc9c0 69 6e 74 20 72 63 3b 0a 20 20 73 74 72 75 63 74  int rc;.  struct
cc9d0 20 6c 6f 63 6b 4b 65 79 20 6b 65 79 31 3b 0a 20   lockKey key1;. 
cc9e0 20 73 74 72 75 63 74 20 6f 70 65 6e 4b 65 79 20   struct openKey 
cc9f0 6b 65 79 32 3b 0a 20 20 73 74 72 75 63 74 20 73  key2;.  struct s
cca00 74 61 74 20 73 74 61 74 62 75 66 3b 0a 20 20 73  tat statbuf;.  s
cca10 74 72 75 63 74 20 6c 6f 63 6b 49 6e 66 6f 20 2a  truct lockInfo *
cca20 70 4c 6f 63 6b 3b 0a 20 20 73 74 72 75 63 74 20  pLock;.  struct 
cca30 6f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 0a  openCnt *pOpen;.
cca40 20 20 72 63 20 3d 20 66 73 74 61 74 28 66 64 2c    rc = fstat(fd,
cca50 20 26 73 74 61 74 62 75 66 29 3b 0a 20 20 69 66   &statbuf);.  if
cca60 28 20 72 63 21 3d 30 20 29 7b 0a 23 69 66 64 65  ( rc!=0 ){.#ifde
cca70 66 20 45 4f 56 45 52 46 4c 4f 57 0a 20 20 20 20  f EOVERFLOW.    
cca80 69 66 28 20 65 72 72 6e 6f 3d 3d 45 4f 56 45 52  if( errno==EOVER
cca90 46 4c 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51  FLOW ) return SQ
ccaa0 4c 49 54 45 5f 4e 4f 4c 46 53 3b 0a 23 65 6e 64  LITE_NOLFS;.#end
ccab0 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  if.    return SQ
ccac0 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a  LITE_IOERR;.  }.
ccad0 0a 20 20 2f 2a 20 4f 6e 20 4f 53 20 58 20 6f 6e  .  /* On OS X on
ccae0 20 61 6e 20 6d 73 64 6f 73 20 66 69 6c 65 73 79   an msdos filesy
ccaf0 73 74 65 6d 2c 20 74 68 65 20 69 6e 6f 64 65 20  stem, the inode 
ccb00 6e 75 6d 62 65 72 20 69 73 20 72 65 70 6f 72 74  number is report
ccb10 65 64 0a 20 20 2a 2a 20 69 6e 63 6f 72 72 65 63  ed.  ** incorrec
ccb20 74 6c 79 20 66 6f 72 20 7a 65 72 6f 2d 73 69 7a  tly for zero-siz
ccb30 65 20 66 69 6c 65 73 2e 20 20 53 65 65 20 74 69  e files.  See ti
ccb40 63 6b 65 74 20 23 33 32 36 30 2e 20 20 54 6f 20  cket #3260.  To 
ccb50 77 6f 72 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64  work.  ** around
ccb60 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 28 77   this problem (w
ccb70 65 20 63 6f 6e 73 69 64 65 72 20 69 74 20 61 20  e consider it a 
ccb80 62 75 67 20 69 6e 20 4f 53 20 58 2c 20 6e 6f 74  bug in OS X, not
ccb90 20 53 51 4c 69 74 65 29 0a 20 20 2a 2a 20 77 65   SQLite).  ** we
ccba0 20 61 6c 77 61 79 73 20 69 6e 63 72 65 61 73 65   always increase
ccbb0 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 74   the file size t
ccbc0 6f 20 31 20 62 79 20 77 72 69 74 69 6e 67 20 61  o 1 by writing a
ccbd0 20 73 69 6e 67 6c 65 20 62 79 74 65 0a 20 20 2a   single byte.  *
ccbe0 2a 20 70 72 69 6f 72 20 74 6f 20 61 63 63 65 73  * prior to acces
ccbf0 73 69 6e 67 20 74 68 65 20 69 6e 6f 64 65 20 6e  sing the inode n
ccc00 75 6d 62 65 72 2e 20 20 54 68 65 20 6f 6e 65 20  umber.  The one 
ccc10 62 79 74 65 20 77 72 69 74 74 65 6e 20 69 73 0a  byte written is.
ccc20 20 20 2a 2a 20 61 6e 20 41 53 43 49 49 20 27 53    ** an ASCII 'S
ccc30 27 20 63 68 61 72 61 63 74 65 72 20 77 68 69 63  ' character whic
ccc40 68 20 61 6c 73 6f 20 68 61 70 70 65 6e 73 20 74  h also happens t
ccc50 6f 20 62 65 20 74 68 65 20 66 69 72 73 74 20 62  o be the first b
ccc60 79 74 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20  yte.  ** in the 
ccc70 68 65 61 64 65 72 20 6f 66 20 65 76 65 72 79 20  header of every 
ccc80 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 2e  SQLite database.
ccc90 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 69    In this way, i
ccca0 66 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20  f there.  ** is 
cccb0 61 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e  a race condition
cccc0 20 73 75 63 68 20 74 68 61 74 20 61 6e 6f 74 68   such that anoth
cccd0 65 72 20 74 68 72 65 61 64 20 68 61 73 20 61 6c  er thread has al
ccce0 72 65 61 64 79 20 70 6f 70 75 6c 61 74 65 64 0a  ready populated.
cccf0 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 70    ** the first p
ccd00 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  age of the datab
ccd10 61 73 65 2c 20 6e 6f 20 64 61 6d 61 67 65 20 69  ase, no damage i
ccd20 73 20 64 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69  s done..  */.  i
ccd30 66 28 20 73 74 61 74 62 75 66 2e 73 74 5f 73 69  f( statbuf.st_si
ccd40 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 77 72 69  ze==0 ){.    wri
ccd50 74 65 28 66 64 2c 20 22 53 22 2c 20 31 29 3b 0a  te(fd, "S", 1);.
ccd60 20 20 20 20 72 63 20 3d 20 66 73 74 61 74 28 66      rc = fstat(f
ccd70 64 2c 20 26 73 74 61 74 62 75 66 29 3b 0a 20 20  d, &statbuf);.  
ccd80 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20    if( rc!=0 ){. 
ccd90 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
ccda0 54 45 5f 49 4f 45 52 52 3b 0a 20 20 20 20 7d 0a  TE_IOERR;.    }.
ccdb0 20 20 7d 0a 0a 20 20 6d 65 6d 73 65 74 28 26 6b    }..  memset(&k
ccdc0 65 79 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6b  ey1, 0, sizeof(k
ccdd0 65 79 31 29 29 3b 0a 20 20 6b 65 79 31 2e 64 65  ey1));.  key1.de
ccde0 76 20 3d 20 73 74 61 74 62 75 66 2e 73 74 5f 64  v = statbuf.st_d
ccdf0 65 76 3b 0a 20 20 6b 65 79 31 2e 69 6e 6f 20 3d  ev;.  key1.ino =
cce00 20 73 74 61 74 62 75 66 2e 73 74 5f 69 6e 6f 3b   statbuf.st_ino;
cce10 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
cce20 41 44 53 41 46 45 0a 20 20 69 66 28 20 74 68 72  ADSAFE.  if( thr
cce30 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68  eadsOverrideEach
cce40 4f 74 68 65 72 73 4c 6f 63 6b 73 3c 30 20 29 7b  OthersLocks<0 ){
cce50 0a 20 20 20 20 74 65 73 74 54 68 72 65 61 64 4c  .    testThreadL
cce60 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 66  ockingBehavior(f
cce70 64 29 3b 0a 20 20 7d 0a 20 20 6b 65 79 31 2e 74  d);.  }.  key1.t
cce80 69 64 20 3d 20 74 68 72 65 61 64 73 4f 76 65 72  id = threadsOver
cce90 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f  rideEachOthersLo
ccea0 63 6b 73 20 3f 20 30 20 3a 20 70 74 68 72 65 61  cks ? 0 : pthrea
cceb0 64 5f 73 65 6c 66 28 29 3b 0a 23 65 6e 64 69 66  d_self();.#endif
ccec0 0a 20 20 6d 65 6d 73 65 74 28 26 6b 65 79 32 2c  .  memset(&key2,
cced0 20 30 2c 20 73 69 7a 65 6f 66 28 6b 65 79 32 29   0, sizeof(key2)
ccee0 29 3b 0a 20 20 6b 65 79 32 2e 64 65 76 20 3d 20  );.  key2.dev = 
ccef0 73 74 61 74 62 75 66 2e 73 74 5f 64 65 76 3b 0a  statbuf.st_dev;.
ccf00 20 20 6b 65 79 32 2e 69 6e 6f 20 3d 20 73 74 61    key2.ino = sta
ccf10 74 62 75 66 2e 73 74 5f 69 6e 6f 3b 0a 20 20 70  tbuf.st_ino;.  p
ccf20 4c 6f 63 6b 20 3d 20 6c 6f 63 6b 4c 69 73 74 3b  Lock = lockList;
ccf30 0a 20 20 77 68 69 6c 65 28 20 70 4c 6f 63 6b 20  .  while( pLock 
ccf40 26 26 20 6d 65 6d 63 6d 70 28 26 6b 65 79 31 2c  && memcmp(&key1,
ccf50 20 26 70 4c 6f 63 6b 2d 3e 6b 65 79 2c 20 73 69   &pLock->key, si
ccf60 7a 65 6f 66 28 6b 65 79 31 29 29 20 29 7b 0a 20  zeof(key1)) ){. 
ccf70 20 20 20 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b     pLock = pLock
ccf80 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69  ->pNext;.  }.  i
ccf90 66 28 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20  f( pLock==0 ){. 
ccfa0 20 20 20 70 4c 6f 63 6b 20 3d 20 73 71 6c 69 74     pLock = sqlit
ccfb0 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f  e3_malloc( sizeo
ccfc0 66 28 2a 70 4c 6f 63 6b 29 20 29 3b 0a 20 20 20  f(*pLock) );.   
ccfd0 20 69 66 28 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b   if( pLock==0 ){
ccfe0 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
ccff0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
cd000 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f  goto exit_findlo
cd010 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 7d 0a 20 20  ckinfo;.    }.  
cd020 20 20 70 4c 6f 63 6b 2d 3e 6b 65 79 20 3d 20 6b    pLock->key = k
cd030 65 79 31 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e  ey1;.    pLock->
cd040 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 70 4c  nRef = 1;.    pL
cd050 6f 63 6b 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 20  ock->cnt = 0;.  
cd060 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70    pLock->locktyp
cd070 65 20 3d 20 30 3b 0a 20 20 20 20 70 4c 6f 63 6b  e = 0;.    pLock
cd080 2d 3e 70 4e 65 78 74 20 3d 20 6c 6f 63 6b 4c 69  ->pNext = lockLi
cd090 73 74 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70  st;.    pLock->p
cd0a0 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 69 66  Prev = 0;.    if
cd0b0 28 20 6c 6f 63 6b 4c 69 73 74 20 29 20 6c 6f 63  ( lockList ) loc
cd0c0 6b 4c 69 73 74 2d 3e 70 50 72 65 76 20 3d 20 70  kList->pPrev = p
cd0d0 4c 6f 63 6b 3b 0a 20 20 20 20 6c 6f 63 6b 4c 69  Lock;.    lockLi
cd0e0 73 74 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d 65  st = pLock;.  }e
cd0f0 6c 73 65 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e  lse{.    pLock->
cd100 6e 52 65 66 2b 2b 3b 0a 20 20 7d 0a 20 20 2a 70  nRef++;.  }.  *p
cd110 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20  pLock = pLock;. 
cd120 20 69 66 28 20 70 70 4f 70 65 6e 21 3d 30 20 29   if( ppOpen!=0 )
cd130 7b 0a 20 20 20 20 70 4f 70 65 6e 20 3d 20 6f 70  {.    pOpen = op
cd140 65 6e 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c  enList;.    whil
cd150 65 28 20 70 4f 70 65 6e 20 26 26 20 6d 65 6d 63  e( pOpen && memc
cd160 6d 70 28 26 6b 65 79 32 2c 20 26 70 4f 70 65 6e  mp(&key2, &pOpen
cd170 2d 3e 6b 65 79 2c 20 73 69 7a 65 6f 66 28 6b 65  ->key, sizeof(ke
cd180 79 32 29 29 20 29 7b 0a 20 20 20 20 20 20 70 4f  y2)) ){.      pO
cd190 70 65 6e 20 3d 20 70 4f 70 65 6e 2d 3e 70 4e 65  pen = pOpen->pNe
cd1a0 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  xt;.    }.    if
cd1b0 28 20 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20  ( pOpen==0 ){.  
cd1c0 20 20 20 20 70 4f 70 65 6e 20 3d 20 73 71 6c 69      pOpen = sqli
cd1d0 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65  te3_malloc( size
cd1e0 6f 66 28 2a 70 4f 70 65 6e 29 20 29 3b 0a 20 20  of(*pOpen) );.  
cd1f0 20 20 20 20 69 66 28 20 70 4f 70 65 6e 3d 3d 30      if( pOpen==0
cd200 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65   ){.        rele
cd210 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 4c 6f 63  aseLockInfo(pLoc
cd220 6b 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  k);.        rc =
cd230 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
cd240 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74         goto exit
cd250 5f 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3b 0a 20  _findlockinfo;. 
cd260 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4f 70       }.      pOp
cd270 65 6e 2d 3e 6b 65 79 20 3d 20 6b 65 79 32 3b 0a  en->key = key2;.
cd280 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65        pOpen->nRe
cd290 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4f 70  f = 1;.      pOp
cd2a0 65 6e 2d 3e 6e 4c 6f 63 6b 20 3d 20 30 3b 0a 20  en->nLock = 0;. 
cd2b0 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e       pOpen->nPen
cd2c0 64 69 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20  ding = 0;.      
cd2d0 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 20  pOpen->aPending 
cd2e0 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e  = 0;.      pOpen
cd2f0 2d 3e 70 4e 65 78 74 20 3d 20 6f 70 65 6e 4c 69  ->pNext = openLi
cd300 73 74 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d  st;.      pOpen-
cd310 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20  >pPrev = 0;.    
cd320 20 20 69 66 28 20 6f 70 65 6e 4c 69 73 74 20 29    if( openList )
cd330 20 6f 70 65 6e 4c 69 73 74 2d 3e 70 50 72 65 76   openList->pPrev
cd340 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20   = pOpen;.      
cd350 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f 70 65 6e  openList = pOpen
cd360 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
cd370 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 2b 2b     pOpen->nRef++
cd380 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 70 4f  ;.    }.    *ppO
cd390 70 65 6e 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 7d  pen = pOpen;.  }
cd3a0 0a 0a 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69  ..exit_findlocki
cd3b0 6e 66 6f 3a 0a 20 20 72 65 74 75 72 6e 20 72 63  nfo:.  return rc
cd3c0 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ;.}..#ifdef SQLI
cd3d0 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 48  TE_DEBUG./*.** H
cd3e0 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 66  elper function f
cd3f0 6f 72 20 70 72 69 6e 74 69 6e 67 20 6f 75 74 20  or printing out 
cd400 74 72 61 63 65 20 69 6e 66 6f 72 6d 61 74 69 6f  trace informatio
cd410 6e 20 66 72 6f 6d 20 64 65 62 75 67 67 69 6e 67  n from debugging
cd420 0a 2a 2a 20 62 69 6e 61 72 69 65 73 2e 20 54 68  .** binaries. Th
cd430 69 73 20 72 65 74 75 72 6e 73 20 74 68 65 20 73  is returns the s
cd440 74 72 69 6e 67 20 72 65 70 72 65 73 65 74 61 74  tring represetat
cd450 69 6f 6e 20 6f 66 20 74 68 65 20 73 75 70 70 6c  ion of the suppl
cd460 69 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6c  ied.** integer l
cd470 6f 63 6b 2d 74 79 70 65 2e 0a 2a 2f 0a 73 74 61  ock-type..*/.sta
cd480 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
cd490 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 69 6e 74  locktypeName(int
cd4a0 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 73 77   locktype){.  sw
cd4b0 69 74 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29  itch( locktype )
cd4c0 7b 0a 20 20 63 61 73 65 20 4e 4f 5f 4c 4f 43 4b  {.  case NO_LOCK
cd4d0 3a 20 72 65 74 75 72 6e 20 22 4e 4f 4e 45 22 3b  : return "NONE";
cd4e0 0a 20 20 63 61 73 65 20 53 48 41 52 45 44 5f 4c  .  case SHARED_L
cd4f0 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 53 48 41  OCK: return "SHA
cd500 52 45 44 22 3b 0a 20 20 63 61 73 65 20 52 45 53  RED";.  case RES
cd510 45 52 56 45 44 5f 4c 4f 43 4b 3a 20 72 65 74 75  ERVED_LOCK: retu
cd520 72 6e 20 22 52 45 53 45 52 56 45 44 22 3b 0a 20  rn "RESERVED";. 
cd530 20 63 61 73 65 20 50 45 4e 44 49 4e 47 5f 4c 4f   case PENDING_LO
cd540 43 4b 3a 20 72 65 74 75 72 6e 20 22 50 45 4e 44  CK: return "PEND
cd550 49 4e 47 22 3b 0a 20 20 63 61 73 65 20 45 58 43  ING";.  case EXC
cd560 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a 20 72 65 74  LUSIVE_LOCK: ret
cd570 75 72 6e 20 22 45 58 43 4c 55 53 49 56 45 22 3b  urn "EXCLUSIVE";
cd580 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 22 45  .  }.  return "E
cd590 52 52 4f 52 22 3b 0a 7d 0a 23 65 6e 64 69 66 0a  RROR";.}.#endif.
cd5a0 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65  ./*.** If we are
cd5b0 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 61 20   currently in a 
cd5c0 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64  different thread
cd5d0 20 74 68 61 6e 20 74 68 65 20 74 68 72 65 61 64   than the thread
cd5e0 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 75 6e 69   that the.** uni
cd5f0 78 46 69 6c 65 20 61 72 67 75 6d 65 6e 74 20 62  xFile argument b
cd600 65 6c 6f 6e 67 73 20 74 6f 2c 20 74 68 65 6e 20  elongs to, then 
cd610 74 72 61 6e 73 66 65 72 20 6f 77 6e 65 72 73 68  transfer ownersh
cd620 69 70 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69  ip of the unixFi
cd630 6c 65 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 74 68  le.** over to th
cd640 65 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64  e current thread
cd650 2e 0a 2a 2a 0a 2a 2a 20 41 20 75 6e 69 78 46 69  ..**.** A unixFi
cd660 6c 65 20 69 73 20 6f 6e 6c 79 20 6f 77 6e 65 64  le is only owned
cd670 20 62 79 20 61 20 74 68 72 65 61 64 20 6f 6e 20   by a thread on 
cd680 73 79 73 74 65 6d 73 20 77 68 65 72 65 20 6f 6e  systems where on
cd690 65 20 74 68 72 65 61 64 20 69 73 0a 2a 2a 20 75  e thread is.** u
cd6a0 6e 61 62 6c 65 20 74 6f 20 6f 76 65 72 72 69 64  nable to overrid
cd6b0 65 20 6c 6f 63 6b 73 20 63 72 65 61 74 65 64 20  e locks created 
cd6c0 62 79 20 61 20 64 69 66 66 65 72 65 6e 74 20 74  by a different t
cd6d0 68 72 65 61 64 2e 20 20 52 65 64 48 61 74 39 20  hread.  RedHat9 
cd6e0 69 73 0a 2a 2a 20 61 6e 20 65 78 61 6d 70 6c 65  is.** an example
cd6f0 20 6f 66 20 73 75 63 68 20 61 20 73 79 73 74 65   of such a syste
cd700 6d 2e 0a 2a 2a 0a 2a 2a 20 4f 77 6e 65 72 73 68  m..**.** Ownersh
cd710 69 70 20 74 72 61 6e 73 66 65 72 20 69 73 20 6f  ip transfer is o
cd720 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 69 66 20 74  nly allowed if t
cd730 68 65 20 75 6e 69 78 46 69 6c 65 20 69 73 20 63  he unixFile is c
cd740 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b 65  urrently unlocke
cd750 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 75 6e 69  d..** If the uni
cd760 78 46 69 6c 65 20 69 73 20 6c 6f 63 6b 65 64 20  xFile is locked 
cd770 61 6e 64 20 61 6e 20 6f 77 6e 65 72 73 68 69 70  and an ownership
cd780 20 69 73 20 77 72 6f 6e 67 2c 20 74 68 65 6e 20   is wrong, then 
cd790 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45  return.** SQLITE
cd7a0 5f 4d 49 53 55 53 45 2e 20 20 53 51 4c 49 54 45  _MISUSE.  SQLITE
cd7b0 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20  _OK is returned 
cd7c0 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f  if everything wo
cd7d0 72 6b 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49  rks..*/.#if SQLI
cd7e0 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 73 74  TE_THREADSAFE.st
cd7f0 61 74 69 63 20 69 6e 74 20 74 72 61 6e 73 66 65  atic int transfe
cd800 72 4f 77 6e 65 72 73 68 69 70 28 75 6e 69 78 46  rOwnership(unixF
cd810 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69  ile *pFile){.  i
cd820 6e 74 20 72 63 3b 0a 20 20 70 74 68 72 65 61 64  nt rc;.  pthread
cd830 5f 74 20 68 53 65 6c 66 3b 0a 20 20 69 66 28 20  _t hSelf;.  if( 
cd840 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45  threadsOverrideE
cd850 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 29  achOthersLocks )
cd860 7b 0a 20 20 20 20 2f 2a 20 4f 77 6e 65 72 73 68  {.    /* Ownersh
cd870 69 70 20 74 72 61 6e 73 66 65 72 73 20 6e 6f 74  ip transfers not
cd880 20 6e 65 65 64 65 64 20 6f 6e 20 74 68 69 73 20   needed on this 
cd890 73 79 73 74 65 6d 20 2a 2f 0a 20 20 20 20 72 65  system */.    re
cd8a0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
cd8b0 20 20 7d 0a 20 20 68 53 65 6c 66 20 3d 20 70 74    }.  hSelf = pt
cd8c0 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20  hread_self();.  
cd8d0 69 66 28 20 70 74 68 72 65 61 64 5f 65 71 75 61  if( pthread_equa
cd8e0 6c 28 70 46 69 6c 65 2d 3e 74 69 64 2c 20 68 53  l(pFile->tid, hS
cd8f0 65 6c 66 29 20 29 7b 0a 20 20 20 20 2f 2a 20 57  elf) ){.    /* W
cd900 65 20 61 72 65 20 73 74 69 6c 6c 20 69 6e 20 74  e are still in t
cd910 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 2a  he same thread *
cd920 2f 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 22  /.    OSTRACE1("
cd930 4e 6f 2d 74 72 61 6e 73 66 65 72 2c 20 73 61 6d  No-transfer, sam
cd940 65 20 74 68 72 65 61 64 5c 6e 22 29 3b 0a 20 20  e thread\n");.  
cd950 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
cd960 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46  OK;.  }.  if( pF
cd970 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e  ile->locktype!=N
cd980 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 2f 2a  O_LOCK ){.    /*
cd990 20 57 65 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67   We cannot chang
cd9a0 65 20 6f 77 6e 65 72 73 68 69 70 20 77 68 69 6c  e ownership whil
cd9b0 65 20 77 65 20 61 72 65 20 68 6f 6c 64 69 6e 67  e we are holding
cd9c0 20 61 20 6c 6f 63 6b 21 20 2a 2f 0a 20 20 20 20   a lock! */.    
cd9d0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
cd9e0 53 55 53 45 3b 0a 20 20 7d 0a 20 20 4f 53 54 52  SUSE;.  }.  OSTR
cd9f0 41 43 45 34 28 22 54 72 61 6e 73 66 65 72 20 6f  ACE4("Transfer o
cda00 77 6e 65 72 73 68 69 70 20 6f 66 20 25 64 20 66  wnership of %d f
cda10 72 6f 6d 20 25 64 20 74 6f 20 25 64 5c 6e 22 2c  rom %d to %d\n",
cda20 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 46 69  .            pFi
cda30 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 74 69  le->h, pFile->ti
cda40 64 2c 20 68 53 65 6c 66 29 3b 0a 20 20 70 46 69  d, hSelf);.  pFi
cda50 6c 65 2d 3e 74 69 64 20 3d 20 68 53 65 6c 66 3b  le->tid = hSelf;
cda60 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 70 4c  .  if (pFile->pL
cda70 6f 63 6b 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 20  ock != NULL) {. 
cda80 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e     releaseLockIn
cda90 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 29  fo(pFile->pLock)
cdaa0 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c  ;.    rc = findL
cdab0 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 68  ockInfo(pFile->h
cdac0 2c 20 26 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2c  , &pFile->pLock,
cdad0 20 30 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45   0);.    OSTRACE
cdae0 35 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 69 73  5("LOCK    %d is
cdaf0 20 6e 6f 77 20 25 73 28 25 73 2c 25 64 29 5c 6e   now %s(%s,%d)\n
cdb00 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20  ", pFile->h,.   
cdb10 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65          locktype
cdb20 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b  Name(pFile->lock
cdb30 74 79 70 65 29 2c 0a 20 20 20 20 20 20 20 20 20  type),.         
cdb40 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70    locktypeName(p
cdb50 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63  File->pLock->loc
cdb60 6b 74 79 70 65 29 2c 20 70 46 69 6c 65 2d 3e 70  ktype), pFile->p
cdb70 4c 6f 63 6b 2d 3e 63 6e 74 29 3b 0a 20 20 20 20  Lock->cnt);.    
cdb80 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20 65  return rc;.  } e
cdb90 6c 73 65 20 7b 0a 20 20 20 20 72 65 74 75 72 6e  lse {.    return
cdba0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
cdbb0 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 4f 6e 20  }.#else.  /* On 
cdbc0 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20  single-threaded 
cdbd0 62 75 69 6c 64 73 2c 20 6f 77 6e 65 72 73 68 69  builds, ownershi
cdbe0 70 20 74 72 61 6e 73 66 65 72 20 69 73 20 61 20  p transfer is a 
cdbf0 6e 6f 2d 6f 70 20 2a 2f 0a 23 20 64 65 66 69 6e  no-op */.# defin
cdc00 65 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73  e transferOwners
cdc10 68 69 70 28 58 29 20 53 51 4c 49 54 45 5f 4f 4b  hip(X) SQLITE_OK
cdc20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53  .#endif../*.** S
cdc30 65 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 65  eek to the offse
cdc40 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  t passed as the 
cdc50 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c  second argument,
cdc60 20 74 68 65 6e 20 72 65 61 64 20 63 6e 74 20 0a   then read cnt .
cdc70 2a 2a 20 62 79 74 65 73 20 69 6e 74 6f 20 70 42  ** bytes into pB
cdc80 75 66 2e 20 52 65 74 75 72 6e 20 74 68 65 20 6e  uf. Return the n
cdc90 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61  umber of bytes a
cdca0 63 74 75 61 6c 6c 79 20 72 65 61 64 2e 0a 2a 2a  ctually read..**
cdcb0 0a 2a 2a 20 4e 42 3a 20 20 49 66 20 79 6f 75 20  .** NB:  If you 
cdcc0 64 65 66 69 6e 65 20 55 53 45 5f 50 52 45 41 44  define USE_PREAD
cdcd0 20 6f 72 20 55 53 45 5f 50 52 45 41 44 36 34 2c   or USE_PREAD64,
cdce0 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20 61   then it might a
cdcf0 6c 73 6f 0a 2a 2a 20 62 65 20 6e 65 63 65 73 73  lso.** be necess
cdd00 61 72 79 20 74 6f 20 64 65 66 69 6e 65 20 5f 58  ary to define _X
cdd10 4f 50 45 4e 5f 53 4f 55 52 43 45 20 74 6f 20 62  OPEN_SOURCE to b
cdd20 65 20 35 30 30 2e 20 20 54 68 69 73 20 76 61 72  e 500.  This var
cdd30 69 65 73 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20  ies from.** one 
cdd40 73 79 73 74 65 6d 20 74 6f 20 61 6e 6f 74 68 65  system to anothe
cdd50 72 2e 20 20 53 69 6e 63 65 20 53 51 4c 69 74 65  r.  Since SQLite
cdd60 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e 65   does not define
cdd70 20 55 53 45 5f 50 52 45 41 44 0a 2a 2a 20 61 6e   USE_PREAD.** an
cdd80 79 20 61 6e 79 20 66 6f 72 6d 20 62 79 20 64 65  y any form by de
cdd90 66 61 75 6c 74 2c 20 77 65 20 77 69 6c 6c 20 6e  fault, we will n
cdda0 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 64 65  ot attempt to de
cddb0 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52  fine _XOPEN_SOUR
cddc0 43 45 2e 0a 2a 2a 20 53 65 65 20 74 69 63 6b 65  CE..** See ticke
cddd0 74 73 20 23 32 37 34 31 20 61 6e 64 20 23 32 36  ts #2741 and #26
cdde0 38 31 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  81..*/.static in
cddf0 74 20 73 65 65 6b 41 6e 64 52 65 61 64 28 75 6e  t seekAndRead(un
cde00 69 78 46 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69  ixFile *id, sqli
cde10 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74  te3_int64 offset
cde20 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e  , void *pBuf, in
cde30 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20 67 6f  t cnt){.  int go
cde40 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66 66 73  t;.  i64 newOffs
cde50 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54 41 52  et;.  TIMER_STAR
cde60 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 55  T;.#if defined(U
cde70 53 45 5f 50 52 45 41 44 29 0a 20 20 67 6f 74 20  SE_PREAD).  got 
cde80 3d 20 70 72 65 61 64 28 69 64 2d 3e 68 2c 20 70  = pread(id->h, p
cde90 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74  Buf, cnt, offset
cdea0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
cdeb0 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20 29  rror( got = -1 )
cdec0 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28  ;.#elif defined(
cded0 55 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20 67  USE_PREAD64).  g
cdee0 6f 74 20 3d 20 70 72 65 61 64 36 34 28 69 64 2d  ot = pread64(id-
cdef0 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f  >h, pBuf, cnt, o
cdf00 66 66 73 65 74 29 3b 0a 20 20 53 69 6d 75 6c 61  ffset);.  Simula
cdf10 74 65 49 4f 45 72 72 6f 72 28 20 67 6f 74 20 3d  teIOError( got =
cdf20 20 2d 31 20 29 3b 0a 23 65 6c 73 65 0a 20 20 6e   -1 );.#else.  n
cdf30 65 77 4f 66 66 73 65 74 20 3d 20 6c 73 65 65 6b  ewOffset = lseek
cdf40 28 69 64 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20  (id->h, offset, 
cdf50 53 45 45 4b 5f 53 45 54 29 3b 0a 20 20 53 69 6d  SEEK_SET);.  Sim
cdf60 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 6e 65  ulateIOError( ne
cdf70 77 4f 66 66 73 65 74 2d 2d 20 29 3b 0a 20 20 69  wOffset-- );.  i
cdf80 66 28 20 6e 65 77 4f 66 66 73 65 74 21 3d 6f 66  f( newOffset!=of
cdf90 66 73 65 74 20 29 7b 0a 20 20 20 20 72 65 74 75  fset ){.    retu
cdfa0 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f 74  rn -1;.  }.  got
cdfb0 20 3d 20 72 65 61 64 28 69 64 2d 3e 68 2c 20 70   = read(id->h, p
cdfc0 42 75 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64 69  Buf, cnt);.#endi
cdfd0 66 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a 20  f.  TIMER_END;. 
cdfe0 20 4f 53 54 52 41 43 45 35 28 22 52 45 41 44 20   OSTRACE5("READ 
cdff0 20 20 20 25 2d 33 64 20 25 35 64 20 25 37 6c 6c     %-3d %5d %7ll
ce000 64 20 25 6c 6c 75 5c 6e 22 2c 20 69 64 2d 3e 68  d %llu\n", id->h
ce010 2c 20 67 6f 74 2c 20 6f 66 66 73 65 74 2c 20 54  , got, offset, T
ce020 49 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b 0a 20  IMER_ELAPSED);. 
ce030 20 72 65 74 75 72 6e 20 67 6f 74 3b 0a 7d 0a 0a   return got;.}..
ce040 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20  /*.** Read data 
ce050 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e 74 6f  from a file into
ce060 20 61 20 62 75 66 66 65 72 2e 20 20 52 65 74 75   a buffer.  Retu
ce070 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20  rn SQLITE_OK if 
ce080 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77 65 72  all.** bytes wer
ce090 65 20 72 65 61 64 20 73 75 63 63 65 73 73 66 75  e read successfu
ce0a0 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 45 5f 49  lly and SQLITE_I
ce0b0 4f 45 52 52 20 69 66 20 61 6e 79 74 68 69 6e 67  OERR if anything
ce0c0 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 0a   goes.** wrong..
ce0d0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e  */.static int un
ce0e0 69 78 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65  ixRead(.  sqlite
ce0f0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 0a 20 20 76  3_file *id, .  v
ce100 6f 69 64 20 2a 70 42 75 66 2c 20 0a 20 20 69 6e  oid *pBuf, .  in
ce110 74 20 61 6d 74 2c 0a 20 20 73 71 6c 69 74 65 33  t amt,.  sqlite3
ce120 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 0a 29 7b  _int64 offset.){
ce130 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 61 73  .  int got;.  as
ce140 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 67 6f  sert( id );.  go
ce150 74 20 3d 20 73 65 65 6b 41 6e 64 52 65 61 64 28  t = seekAndRead(
ce160 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 2c 20 6f  (unixFile*)id, o
ce170 66 66 73 65 74 2c 20 70 42 75 66 2c 20 61 6d 74  ffset, pBuf, amt
ce180 29 3b 0a 20 20 69 66 28 20 67 6f 74 3d 3d 61 6d  );.  if( got==am
ce190 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
ce1a0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c  SQLITE_OK;.  }el
ce1b0 73 65 20 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a  se if( got<0 ){.
ce1c0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
ce1d0 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20  E_IOERR_READ;.  
ce1e0 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 55 6e  }else{.    /* Un
ce1f0 72 65 61 64 20 70 61 72 74 73 20 6f 66 20 74 68  read parts of th
ce200 65 20 62 75 66 66 65 72 20 6d 75 73 74 20 62 65  e buffer must be
ce210 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a   zero-filled */.
ce220 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68      memset(&((ch
ce230 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20  ar*)pBuf)[got], 
ce240 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20  0, amt-got);.   
ce250 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
ce260 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b  OERR_SHORT_READ;
ce270 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65  .  }.}../*.** Se
ce280 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74  ek to the offset
ce290 20 69 6e 20 69 64 2d 3e 6f 66 66 73 65 74 20 74   in id->offset t
ce2a0 68 65 6e 20 72 65 61 64 20 63 6e 74 20 62 79 74  hen read cnt byt
ce2b0 65 73 20 69 6e 74 6f 20 70 42 75 66 2e 0a 2a 2a  es into pBuf..**
ce2c0 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
ce2d0 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 75  er of bytes actu
ce2e0 61 6c 6c 79 20 72 65 61 64 2e 20 20 55 70 64 61  ally read.  Upda
ce2f0 74 65 20 74 68 65 20 6f 66 66 73 65 74 2e 0a 2a  te the offset..*
ce300 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 65  /.static int see
ce310 6b 41 6e 64 57 72 69 74 65 28 75 6e 69 78 46 69  kAndWrite(unixFi
ce320 6c 65 20 2a 69 64 2c 20 69 36 34 20 6f 66 66 73  le *id, i64 offs
ce330 65 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  et, const void *
ce340 70 42 75 66 2c 20 69 6e 74 20 63 6e 74 29 7b 0a  pBuf, int cnt){.
ce350 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 69 36 34    int got;.  i64
ce360 20 6e 65 77 4f 66 66 73 65 74 3b 0a 20 20 54 49   newOffset;.  TI
ce370 4d 45 52 5f 53 54 41 52 54 3b 0a 23 69 66 20 64  MER_START;.#if d
ce380 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44  efined(USE_PREAD
ce390 29 0a 20 20 67 6f 74 20 3d 20 70 77 72 69 74 65  ).  got = pwrite
ce3a0 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e  (id->h, pBuf, cn
ce3b0 74 2c 20 6f 66 66 73 65 74 29 3b 0a 23 65 6c 69  t, offset);.#eli
ce3c0 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52  f defined(USE_PR
ce3d0 45 41 44 36 34 29 0a 20 20 67 6f 74 20 3d 20 70  EAD64).  got = p
ce3e0 77 72 69 74 65 36 34 28 69 64 2d 3e 68 2c 20 70  write64(id->h, p
ce3f0 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74  Buf, cnt, offset
ce400 29 3b 0a 23 65 6c 73 65 0a 20 20 6e 65 77 4f 66  );.#else.  newOf
ce410 66 73 65 74 20 3d 20 6c 73 65 65 6b 28 69 64 2d  fset = lseek(id-
ce420 3e 68 2c 20 6f 66 66 73 65 74 2c 20 53 45 45 4b  >h, offset, SEEK
ce430 5f 53 45 54 29 3b 0a 20 20 69 66 28 20 6e 65 77  _SET);.  if( new
ce440 4f 66 66 73 65 74 21 3d 6f 66 66 73 65 74 20 29  Offset!=offset )
ce450 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b  {.    return -1;
ce460 0a 20 20 7d 0a 20 20 67 6f 74 20 3d 20 77 72 69  .  }.  got = wri
ce470 74 65 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20  te(id->h, pBuf, 
ce480 63 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 54  cnt);.#endif.  T
ce490 49 4d 45 52 5f 45 4e 44 3b 0a 20 20 4f 53 54 52  IMER_END;.  OSTR
ce4a0 41 43 45 35 28 22 57 52 49 54 45 20 20 20 25 2d  ACE5("WRITE   %-
ce4b0 33 64 20 25 35 64 20 25 37 6c 6c 64 20 25 6c 6c  3d %5d %7lld %ll
ce4c0 75 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f 74  u\n", id->h, got
ce4d0 2c 20 6f 66 66 73 65 74 2c 20 54 49 4d 45 52 5f  , offset, TIMER_
ce4e0 45 4c 41 50 53 45 44 29 3b 0a 20 20 72 65 74 75  ELAPSED);.  retu
ce4f0 72 6e 20 67 6f 74 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  rn got;.}.../*.*
ce500 2a 20 57 72 69 74 65 20 64 61 74 61 20 66 72 6f  * Write data fro
ce510 6d 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20  m a buffer into 
ce520 61 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20  a file.  Return 
ce530 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63  SQLITE_OK on suc
ce540 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20  cess.** or some 
ce550 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65  other error code
ce560 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a   on failure..*/.
ce570 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 57  static int unixW
ce580 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  rite(.  sqlite3_
ce590 66 69 6c 65 20 2a 69 64 2c 20 0a 20 20 63 6f 6e  file *id, .  con
ce5a0 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 0a  st void *pBuf, .
ce5b0 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71 6c    int amt,.  sql
ce5c0 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65  ite3_int64 offse
ce5d0 74 20 0a 29 7b 0a 20 20 69 6e 74 20 77 72 6f 74  t .){.  int wrot
ce5e0 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28  e = 0;.  assert(
ce5f0 20 69 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28   id );.  assert(
ce600 20 61 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69 6c   amt>0 );.  whil
ce610 65 28 20 61 6d 74 3e 30 20 26 26 20 28 77 72 6f  e( amt>0 && (wro
ce620 74 65 20 3d 20 73 65 65 6b 41 6e 64 57 72 69 74  te = seekAndWrit
ce630 65 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 2c  e((unixFile*)id,
ce640 20 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20 61   offset, pBuf, a
ce650 6d 74 29 29 3e 30 20 29 7b 0a 20 20 20 20 61 6d  mt))>0 ){.    am
ce660 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20  t -= wrote;.    
ce670 6f 66 66 73 65 74 20 2b 3d 20 77 72 6f 74 65 3b  offset += wrote;
ce680 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 28 63  .    pBuf = &((c
ce690 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74 65  har*)pBuf)[wrote
ce6a0 5d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 6c 61 74  ];.  }.  Simulat
ce6b0 65 49 4f 45 72 72 6f 72 28 28 20 77 72 6f 74 65  eIOError(( wrote
ce6c0 3d 28 2d 31 29 2c 20 61 6d 74 3d 31 20 29 29 3b  =(-1), amt=1 ));
ce6d0 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66  .  SimulateDiskf
ce6e0 75 6c 6c 45 72 72 6f 72 28 28 20 77 72 6f 74 65  ullError(( wrote
ce6f0 3d 30 2c 20 61 6d 74 3d 31 20 29 29 3b 0a 20 20  =0, amt=1 ));.  
ce700 69 66 28 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20  if( amt>0 ){.   
ce710 20 69 66 28 20 77 72 6f 74 65 3c 30 20 29 7b 0a   if( wrote<0 ){.
ce720 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
ce730 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 3b  ITE_IOERR_WRITE;
ce740 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
ce750 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
ce760 46 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  FULL;.    }.  }.
ce770 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
ce780 4f 4b 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51  OK;.}..#ifdef SQ
ce790 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20  LITE_TEST./*.** 
ce7a0 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72  Count the number
ce7b0 20 6f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e   of fullsyncs an
ce7c0 64 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20  d normal syncs. 
ce7d0 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f   This is used to
ce7e0 20 74 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79   test.** that sy
ce7f0 6e 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63  ncs and fullsync
ce800 73 20 61 72 65 20 6f 63 63 75 72 69 6e 67 20 61  s are occuring a
ce810 74 20 74 68 65 20 72 69 67 68 74 20 74 69 6d 65  t the right time
ce820 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
ce830 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e   int sqlite3_syn
ce840 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c  c_count = 0;.SQL
ce850 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
ce860 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75  te3_fullsync_cou
ce870 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  nt = 0;.#endif..
ce880 2f 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 66 64  /*.** Use the fd
ce890 61 74 61 73 79 6e 63 28 29 20 41 50 49 20 6f 6e  atasync() API on
ce8a0 6c 79 20 69 66 20 74 68 65 20 48 41 56 45 5f 46  ly if the HAVE_F
ce8b0 44 41 54 41 53 59 4e 43 20 6d 61 63 72 6f 20 69  DATASYNC macro i
ce8c0 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 4f 74  s defined..** Ot
ce8d0 68 65 72 77 69 73 65 20 75 73 65 20 66 73 79 6e  herwise use fsyn
ce8e0 63 28 29 20 69 6e 20 69 74 73 20 70 6c 61 63 65  c() in its place
ce8f0 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 48 41 56  ..*/.#ifndef HAV
ce900 45 5f 46 44 41 54 41 53 59 4e 43 0a 23 20 64 65  E_FDATASYNC.# de
ce910 66 69 6e 65 20 66 64 61 74 61 73 79 6e 63 20 66  fine fdatasync f
ce920 73 79 6e 63 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  sync.#endif../*.
ce930 2a 2a 20 44 65 66 69 6e 65 20 48 41 56 45 5f 46  ** Define HAVE_F
ce940 55 4c 4c 46 53 59 4e 43 20 74 6f 20 30 20 6f 72  ULLFSYNC to 0 or
ce950 20 31 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20   1 depending on 
ce960 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a 2a  whether or not.*
ce970 2a 20 74 68 65 20 46 5f 46 55 4c 4c 46 53 59 4e  * the F_FULLFSYN
ce980 43 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e  C macro is defin
ce990 65 64 2e 20 20 46 5f 46 55 4c 4c 46 53 59 4e 43  ed.  F_FULLFSYNC
ce9a0 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a   is currently.**
ce9b0 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20   only available 
ce9c0 6f 6e 20 4d 61 63 20 4f 53 20 58 2e 20 20 42 75  on Mac OS X.  Bu
ce9d0 74 20 74 68 61 74 20 63 6f 75 6c 64 20 63 68 61  t that could cha
ce9e0 6e 67 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 46  nge..*/.#ifdef F
ce9f0 5f 46 55 4c 4c 46 53 59 4e 43 0a 23 20 64 65 66  _FULLFSYNC.# def
cea00 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59  ine HAVE_FULLFSY
cea10 4e 43 20 31 0a 23 65 6c 73 65 0a 23 20 64 65 66  NC 1.#else.# def
cea20 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59  ine HAVE_FULLFSY
cea30 4e 43 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  NC 0.#endif.../*
cea40 0a 2a 2a 20 54 68 65 20 66 73 79 6e 63 28 29 20  .** The fsync() 
cea50 73 79 73 74 65 6d 20 63 61 6c 6c 20 64 6f 65 73  system call does
cea60 20 6e 6f 74 20 77 6f 72 6b 20 61 73 20 61 64 76   not work as adv
cea70 65 72 74 69 73 65 64 20 6f 6e 20 6d 61 6e 79 0a  ertised on many.
cea80 2a 2a 20 75 6e 69 78 20 73 79 73 74 65 6d 73 2e  ** unix systems.
cea90 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
ceaa0 70 72 6f 63 65 64 75 72 65 20 69 73 20 61 6e 20  procedure is an 
ceab0 61 74 74 65 6d 70 74 20 74 6f 20 6d 61 6b 65 0a  attempt to make.
ceac0 2a 2a 20 69 74 20 77 6f 72 6b 20 62 65 74 74 65  ** it work bette
cead0 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c  r..**.** The SQL
ceae0 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 6d 61 63 72  ITE_NO_SYNC macr
ceaf0 6f 20 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 66  o disables all f
ceb00 73 79 6e 63 28 29 73 2e 20 20 54 68 69 73 20 69  sync()s.  This i
ceb10 73 20 75 73 65 66 75 6c 0a 2a 2a 20 66 6f 72 20  s useful.** for 
ceb20 74 65 73 74 69 6e 67 20 77 68 65 6e 20 77 65 20  testing when we 
ceb30 77 61 6e 74 20 74 6f 20 72 75 6e 20 74 68 72 6f  want to run thro
ceb40 75 67 68 20 74 68 65 20 74 65 73 74 20 73 75 69  ugh the test sui
ceb50 74 65 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 20 59  te quickly..** Y
ceb60 6f 75 20 61 72 65 20 73 74 72 6f 6e 67 6c 79 20  ou are strongly 
ceb70 61 64 76 69 73 65 64 20 2a 6e 6f 74 2a 20 74 6f  advised *not* to
ceb80 20 64 65 70 6c 6f 79 20 77 69 74 68 20 53 51 4c   deploy with SQL
ceb90 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 2a 2a 20 65  ITE_NO_SYNC.** e
ceba0 6e 61 62 6c 65 64 2c 20 68 6f 77 65 76 65 72 2c  nabled, however,
cebb0 20 73 69 6e 63 65 20 77 69 74 68 20 53 51 4c 49   since with SQLI
cebc0 54 45 5f 4e 4f 5f 53 59 4e 43 20 65 6e 61 62 6c  TE_NO_SYNC enabl
cebd0 65 64 2c 20 61 6e 20 4f 53 20 63 72 61 73 68 0a  ed, an OS crash.
cebe0 2a 2a 20 6f 72 20 70 6f 77 65 72 20 66 61 69 6c  ** or power fail
cebf0 75 72 65 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20  ure will likely 
cec00 63 6f 72 72 75 70 74 20 74 68 65 20 64 61 74 61  corrupt the data
cec10 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74  base file..*/.st
cec20 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 5f 66 73  atic int full_fs
cec30 79 6e 63 28 69 6e 74 20 66 64 2c 20 69 6e 74 20  ync(int fd, int 
cec40 66 75 6c 6c 53 79 6e 63 2c 20 69 6e 74 20 64 61  fullSync, int da
cec50 74 61 4f 6e 6c 79 29 7b 0a 20 20 69 6e 74 20 72  taOnly){.  int r
cec60 63 3b 0a 0a 20 20 2f 2a 20 52 65 63 6f 72 64 20  c;..  /* Record 
cec70 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69  the number of ti
cec80 6d 65 73 20 74 68 61 74 20 77 65 20 64 6f 20 61  mes that we do a
cec90 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63 28 29 20   normal fsync() 
ceca0 61 6e 64 20 0a 20 20 2a 2a 20 46 55 4c 4c 53 59  and .  ** FULLSY
cecb0 4e 43 2e 20 20 54 68 69 73 20 69 73 20 75 73 65  NC.  This is use
cecc0 64 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67  d during testing
cecd0 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 20   to verify that 
cece0 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 0a 20  this procedure. 
cecf0 20 2a 2a 20 67 65 74 73 20 63 61 6c 6c 65 64 20   ** gets called 
ced00 77 69 74 68 20 74 68 65 20 63 6f 72 72 65 63 74  with the correct
ced10 20 61 72 67 75 6d 65 6e 74 73 2e 0a 20 20 2a 2f   arguments..  */
ced20 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
ced30 45 53 54 0a 20 20 69 66 28 20 66 75 6c 6c 53 79  EST.  if( fullSy
ced40 6e 63 20 29 20 73 71 6c 69 74 65 33 5f 66 75 6c  nc ) sqlite3_ful
ced50 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 20  lsync_count++;. 
ced60 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f   sqlite3_sync_co
ced70 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 0a 20  unt++;.#endif.. 
ced80 20 2f 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c   /* If we compil
ced90 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c 49  ed with the SQLI
ceda0 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c 61 67 2c  TE_NO_SYNC flag,
cedb0 20 74 68 65 6e 20 73 79 6e 63 69 6e 67 20 69 73   then syncing is
cedc0 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 0a 20 20   a.  ** no-op.  
cedd0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
cede0 5f 4e 4f 5f 53 59 4e 43 0a 20 20 72 63 20 3d 20  _NO_SYNC.  rc = 
cedf0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6c 73 65  SQLITE_OK;.#else
cee00 0a 0a 23 69 66 20 48 41 56 45 5f 46 55 4c 4c 46  ..#if HAVE_FULLF
cee10 53 59 4e 43 0a 20 20 69 66 28 20 66 75 6c 6c 53  SYNC.  if( fullS
cee20 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ync ){.    rc = 
cee30 66 63 6e 74 6c 28 66 64 2c 20 46 5f 46 55 4c 4c  fcntl(fd, F_FULL
cee40 46 53 59 4e 43 2c 20 30 29 3b 0a 20 20 7d 65 6c  FSYNC, 0);.  }el
cee50 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a  se{.    rc = 1;.
cee60 20 20 7d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20    }.  /* If the 
cee70 46 55 4c 4c 46 53 59 4e 43 20 66 61 69 6c 65 64  FULLFSYNC failed
cee80 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 61  , fall back to a
cee90 74 74 65 6d 70 74 69 6e 67 20 61 6e 20 66 73 79  ttempting an fsy
ceea0 6e 63 28 29 2e 0a 20 20 20 2a 20 49 74 20 73 68  nc()..   * It sh
ceeb0 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69  ouldn't be possi
ceec0 62 6c 65 20 66 6f 72 20 66 75 6c 6c 66 73 79 6e  ble for fullfsyn
ceed0 63 20 74 6f 20 66 61 69 6c 20 6f 6e 20 74 68 65  c to fail on the
ceee0 20 6c 6f 63 61 6c 20 0a 20 20 20 2a 20 66 69 6c   local .   * fil
ceef0 65 20 73 79 73 74 65 6d 20 28 6f 6e 20 4f 53 58  e system (on OSX
cef00 29 2c 20 73 6f 20 66 61 69 6c 75 72 65 20 69 6e  ), so failure in
cef10 64 69 63 61 74 65 73 20 74 68 61 74 20 46 55 4c  dicates that FUL
cef20 4c 46 53 59 4e 43 0a 20 20 20 2a 20 69 73 6e 27  LFSYNC.   * isn'
cef30 74 20 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20  t supported for 
cef40 74 68 69 73 20 66 69 6c 65 20 73 79 73 74 65 6d  this file system
cef50 2e 20 53 6f 2c 20 61 74 74 65 6d 70 74 20 61 6e  . So, attempt an
cef60 20 66 73 79 6e 63 20 0a 20 20 20 2a 20 61 6e 64   fsync .   * and
cef70 20 28 66 6f 72 20 6e 6f 77 29 20 69 67 6e 6f 72   (for now) ignor
cef80 65 20 74 68 65 20 6f 76 65 72 68 65 61 64 20 6f  e the overhead o
cef90 66 20 61 20 73 75 70 65 72 66 6c 75 6f 75 73 20  f a superfluous 
cefa0 66 63 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a 20 20  fcntl call.  .  
cefb0 20 2a 20 49 74 27 64 20 62 65 20 62 65 74 74 65   * It'd be bette
cefc0 72 20 74 6f 20 64 65 74 65 63 74 20 66 75 6c 6c  r to detect full
cefd0 66 73 79 6e 63 20 73 75 70 70 6f 72 74 20 6f 6e  fsync support on
cefe0 63 65 20 61 6e 64 20 61 76 6f 69 64 20 0a 20 20  ce and avoid .  
ceff0 20 2a 20 74 68 65 20 66 63 6e 74 6c 20 63 61 6c   * the fcntl cal
cf000 6c 20 65 76 65 72 79 20 74 69 6d 65 20 73 79 6e  l every time syn
cf010 63 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20 20  c is called..   
cf020 2a 2f 0a 20 20 69 66 28 20 72 63 20 29 20 72 63  */.  if( rc ) rc
cf030 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 0a 23   = fsync(fd);..#
cf040 65 6c 73 65 20 0a 20 20 69 66 28 20 64 61 74 61  else .  if( data
cf050 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 72 63 20 3d  Only ){.    rc =
cf060 20 66 64 61 74 61 73 79 6e 63 28 66 64 29 3b 0a   fdatasync(fd);.
cf070 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
cf080 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 20 20 7d  = fsync(fd);.  }
cf090 0a 23 65 6e 64 69 66 20 2f 2a 20 48 41 56 45 5f  .#endif /* HAVE_
cf0a0 46 55 4c 4c 46 53 59 4e 43 20 2a 2f 0a 23 65 6e  FULLFSYNC */.#en
cf0b0 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53  dif /* defined(S
cf0c0 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 29 20 2a  QLITE_NO_SYNC) *
cf0d0 2f 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  /..  return rc;.
cf0e0 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75  }../*.** Make su
cf0f0 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20 74 6f  re all writes to
cf100 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 66 69   a particular fi
cf110 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 65 64  le are committed
cf120 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20   to disk..**.** 
cf130 49 66 20 64 61 74 61 4f 6e 6c 79 3d 3d 30 20 74  If dataOnly==0 t
cf140 68 65 6e 20 62 6f 74 68 20 74 68 65 20 66 69 6c  hen both the fil
cf150 65 20 69 74 73 65 6c 66 20 61 6e 64 20 69 74 73  e itself and its
cf160 20 6d 65 74 61 64 61 74 61 20 28 66 69 6c 65 0a   metadata (file.
cf170 2a 2a 20 73 69 7a 65 2c 20 61 63 63 65 73 73 20  ** size, access 
cf180 74 69 6d 65 2c 20 65 74 63 29 20 61 72 65 20 73  time, etc) are s
cf190 79 6e 63 65 64 2e 20 20 49 66 20 64 61 74 61 4f  ynced.  If dataO
cf1a0 6e 6c 79 21 3d 30 20 74 68 65 6e 20 6f 6e 6c 79  nly!=0 then only
cf1b0 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 64 61 74   the.** file dat
cf1c0 61 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a  a is synced..**.
cf1d0 2a 2a 20 55 6e 64 65 72 20 55 6e 69 78 2c 20 61  ** Under Unix, a
cf1e0 6c 73 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68  lso make sure th
cf1f0 61 74 20 74 68 65 20 64 69 72 65 63 74 6f 72 79  at the directory
cf200 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 66   entry for the f
cf210 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20  ile.** has been 
cf220 63 72 65 61 74 65 64 20 62 79 20 66 73 79 6e 63  created by fsync
cf230 2d 69 6e 67 20 74 68 65 20 64 69 72 65 63 74 6f  -ing the directo
cf240 72 79 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  ry that contains
cf250 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 66   the file..** If
cf260 20 77 65 20 64 6f 20 6e 6f 74 20 64 6f 20 74 68   we do not do th
cf270 69 73 20 61 6e 64 20 77 65 20 65 6e 63 6f 75 6e  is and we encoun
cf280 74 65 72 20 61 20 70 6f 77 65 72 20 66 61 69 6c  ter a power fail
cf290 75 72 65 2c 20 74 68 65 20 64 69 72 65 63 74 6f  ure, the directo
cf2a0 72 79 0a 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20  ry.** entry for 
cf2b0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68  the journal migh
cf2c0 74 20 6e 6f 74 20 65 78 69 73 74 20 61 66 74 65  t not exist afte
cf2d0 72 20 77 65 20 72 65 62 6f 6f 74 2e 20 20 54 68  r we reboot.  Th
cf2e0 65 20 6e 65 78 74 0a 2a 2a 20 53 51 4c 69 74 65  e next.** SQLite
cf2f0 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 66   to access the f
cf300 69 6c 65 20 77 69 6c 6c 20 6e 6f 74 20 6b 6e 6f  ile will not kno
cf310 77 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e  w that the journ
cf320 61 6c 20 65 78 69 73 74 73 20 28 62 65 63 61 75  al exists (becau
cf330 73 65 0a 2a 2a 20 74 68 65 20 64 69 72 65 63 74  se.** the direct
cf340 6f 72 79 20 65 6e 74 72 79 20 66 6f 72 20 74 68  ory entry for th
cf350 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 6e 65  e journal was ne
cf360 76 65 72 20 63 72 65 61 74 65 64 29 20 61 6e 64  ver created) and
cf370 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
cf380 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c  .** will not rol
cf390 6c 20 62 61 63 6b 20 2d 20 70 6f 73 73 69 62 6c  l back - possibl
cf3a0 79 20 6c 65 61 64 69 6e 67 20 74 6f 20 64 61 74  y leading to dat
cf3b0 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e  abase corruption
cf3c0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
cf3d0 75 6e 69 78 53 79 6e 63 28 73 71 6c 69 74 65 33  unixSync(sqlite3
cf3e0 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66  _file *id, int f
cf3f0 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b  lags){.  int rc;
cf400 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
cf410 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
cf420 69 64 3b 0a 0a 20 20 69 6e 74 20 69 73 44 61 74  id;..  int isDat
cf430 61 4f 6e 6c 79 20 3d 20 28 66 6c 61 67 73 26 53  aOnly = (flags&S
cf440 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f  QLITE_SYNC_DATAO
cf450 4e 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73 46 75  NLY);.  int isFu
cf460 6c 6c 73 79 6e 63 20 3d 20 28 66 6c 61 67 73 26  llsync = (flags&
cf470 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53 59  0x0F)==SQLITE_SY
cf480 4e 43 5f 46 55 4c 4c 3b 0a 0a 20 20 2f 2a 20 43  NC_FULL;..  /* C
cf490 68 65 63 6b 20 74 68 61 74 20 6f 6e 65 20 6f 66  heck that one of
cf4a0 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52   SQLITE_SYNC_NOR
cf4b0 4d 41 4c 20 6f 72 20 46 55 4c 4c 20 77 61 73 20  MAL or FULL was 
cf4c0 70 61 73 73 65 64 20 2a 2f 0a 20 20 61 73 73 65  passed */.  asse
cf4d0 72 74 28 28 66 6c 61 67 73 26 30 78 30 46 29 3d  rt((flags&0x0F)=
cf4e0 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52  =SQLITE_SYNC_NOR
cf4f0 4d 41 4c 0a 20 20 20 20 20 20 7c 7c 20 28 66 6c  MAL.      || (fl
cf500 61 67 73 26 30 78 30 46 29 3d 3d 53 51 4c 49 54  ags&0x0F)==SQLIT
cf510 45 5f 53 59 4e 43 5f 46 55 4c 4c 0a 20 20 29 3b  E_SYNC_FULL.  );
cf520 0a 0a 20 20 2f 2a 20 55 6e 69 78 20 63 61 6e 6e  ..  /* Unix cann
cf530 6f 74 2c 20 62 75 74 20 73 6f 6d 65 20 73 79 73  ot, but some sys
cf540 74 65 6d 73 20 6d 61 79 20 72 65 74 75 72 6e 20  tems may return 
cf550 53 51 4c 49 54 45 5f 46 55 4c 4c 20 66 72 6f 6d  SQLITE_FULL from
cf560 20 68 65 72 65 2e 20 54 68 69 73 0a 20 20 2a 2a   here. This.  **
cf570 20 6c 69 6e 65 20 69 73 20 74 6f 20 74 65 73 74   line is to test
cf580 20 74 68 61 74 20 64 6f 69 6e 67 20 73 6f 20 64   that doing so d
cf590 6f 65 73 20 6e 6f 74 20 63 61 75 73 65 20 61 6e  oes not cause an
cf5a0 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 2a 2f  y problems..  */
cf5b0 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66  .  SimulateDiskf
cf5c0 75 6c 6c 45 72 72 6f 72 28 20 72 65 74 75 72 6e  ullError( return
cf5d0 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 3b 0a   SQLITE_FULL );.
cf5e0 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
cf5f0 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22   );.  OSTRACE2("
cf600 53 59 4e 43 20 20 20 20 25 2d 33 64 5c 6e 22 2c  SYNC    %-3d\n",
cf610 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 72 63   pFile->h);.  rc
cf620 20 3d 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46   = full_fsync(pF
cf630 69 6c 65 2d 3e 68 2c 20 69 73 46 75 6c 6c 73 79  ile->h, isFullsy
cf640 6e 63 2c 20 69 73 44 61 74 61 4f 6e 6c 79 29 3b  nc, isDataOnly);
cf650 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
cf660 6f 72 28 20 72 63 3d 31 20 29 3b 0a 20 20 69 66  or( rc=1 );.  if
cf670 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75  ( rc ){.    retu
cf680 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
cf690 46 53 59 4e 43 3b 0a 20 20 7d 0a 20 20 69 66 28  FSYNC;.  }.  if(
cf6a0 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 3e 3d 30   pFile->dirfd>=0
cf6b0 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 34   ){.    OSTRACE4
cf6c0 28 22 44 49 52 53 59 4e 43 20 25 2d 33 64 20 28  ("DIRSYNC %-3d (
cf6d0 68 61 76 65 5f 66 75 6c 6c 66 73 79 6e 63 3d 25  have_fullfsync=%
cf6e0 64 20 66 75 6c 6c 73 79 6e 63 3d 25 64 29 5c 6e  d fullsync=%d)\n
cf6f0 22 2c 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 2c  ", pFile->dirfd,
cf700 0a 20 20 20 20 20 20 20 20 20 20 20 20 48 41 56  .            HAV
cf710 45 5f 46 55 4c 4c 46 53 59 4e 43 2c 20 69 73 46  E_FULLFSYNC, isF
cf720 75 6c 6c 73 79 6e 63 29 3b 0a 23 69 66 6e 64 65  ullsync);.#ifnde
cf730 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45  f SQLITE_DISABLE
cf740 5f 44 49 52 53 59 4e 43 0a 20 20 20 20 2f 2a 20  _DIRSYNC.    /* 
cf750 54 68 65 20 64 69 72 65 63 74 6f 72 79 20 73 79  The directory sy
cf760 6e 63 20 69 73 20 6f 6e 6c 79 20 61 74 74 65 6d  nc is only attem
cf770 70 74 65 64 20 69 66 20 66 75 6c 6c 5f 66 73 79  pted if full_fsy
cf780 6e 63 20 69 73 0a 20 20 20 20 2a 2a 20 74 75 72  nc is.    ** tur
cf790 6e 65 64 20 6f 66 66 20 6f 72 20 75 6e 61 76 61  ned off or unava
cf7a0 69 6c 61 62 6c 65 2e 20 20 49 66 20 61 20 66 75  ilable.  If a fu
cf7b0 6c 6c 5f 66 73 79 6e 63 20 6f 63 63 75 72 72 65  ll_fsync occurre
cf7c0 64 20 61 62 6f 76 65 2c 0a 20 20 20 20 2a 2a 20  d above,.    ** 
cf7d0 74 68 65 6e 20 74 68 65 20 64 69 72 65 63 74 6f  then the directo
cf7e0 72 79 20 73 79 6e 63 20 69 73 20 73 75 70 65 72  ry sync is super
cf7f0 66 6c 75 6f 75 73 2e 0a 20 20 20 20 2a 2f 0a 20  fluous..    */. 
cf800 20 20 20 69 66 28 20 28 21 48 41 56 45 5f 46 55     if( (!HAVE_FU
cf810 4c 4c 46 53 59 4e 43 20 7c 7c 20 21 69 73 46 75  LLFSYNC || !isFu
cf820 6c 6c 73 79 6e 63 29 20 26 26 20 66 75 6c 6c 5f  llsync) && full_
cf830 66 73 79 6e 63 28 70 46 69 6c 65 2d 3e 64 69 72  fsync(pFile->dir
cf840 66 64 2c 30 2c 30 29 20 29 7b 0a 20 20 20 20 20  fd,0,0) ){.     
cf850 20 20 2f 2a 0a 20 20 20 20 20 20 20 2a 2a 20 57    /*.       ** W
cf860 65 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20  e have received 
cf870 6d 75 6c 74 69 70 6c 65 20 72 65 70 6f 72 74 73  multiple reports
cf880 20 6f 66 20 66 73 79 6e 63 28 29 20 72 65 74 75   of fsync() retu
cf890 72 6e 69 6e 67 0a 20 20 20 20 20 20 20 2a 2a 20  rning.       ** 
cf8a0 65 72 72 6f 72 73 20 77 68 65 6e 20 61 70 70 6c  errors when appl
cf8b0 69 65 64 20 74 6f 20 64 69 72 65 63 74 6f 72 69  ied to directori
cf8c0 65 73 20 6f 6e 20 63 65 72 74 61 69 6e 20 66 69  es on certain fi
cf8d0 6c 65 20 73 79 73 74 65 6d 73 2e 0a 20 20 20 20  le systems..    
cf8e0 20 20 20 2a 2a 20 41 20 66 61 69 6c 65 64 20 64     ** A failed d
cf8f0 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20 69 73  irectory sync is
cf900 20 6e 6f 74 20 61 20 62 69 67 20 64 65 61 6c 2e   not a big deal.
cf910 20 20 53 6f 20 69 74 20 73 65 65 6d 73 0a 20 20    So it seems.  
cf920 20 20 20 20 20 2a 2a 20 62 65 74 74 65 72 20 74       ** better t
cf930 6f 20 69 67 6e 6f 72 65 20 74 68 65 20 65 72 72  o ignore the err
cf940 6f 72 2e 20 20 54 69 63 6b 65 74 20 23 31 36 35  or.  Ticket #165
cf950 37 0a 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  7.       */.    
cf960 20 20 20 2f 2a 20 72 65 74 75 72 6e 20 53 51 4c     /* return SQL
cf970 49 54 45 5f 49 4f 45 52 52 3b 20 2a 2f 0a 20 20  ITE_IOERR; */.  
cf980 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63    }.#endif.    c
cf990 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 64 69 72 66  lose(pFile->dirf
cf9a0 64 29 3b 20 20 2f 2a 20 4f 6e 6c 79 20 6e 65 65  d);  /* Only nee
cf9b0 64 20 74 6f 20 73 79 6e 63 20 6f 6e 63 65 2c 20  d to sync once, 
cf9c0 73 6f 20 63 6c 6f 73 65 20 74 68 65 20 64 69 72  so close the dir
cf9d0 65 63 74 6f 72 79 20 2a 2f 0a 20 20 20 20 70 46  ectory */.    pF
cf9e0 69 6c 65 2d 3e 64 69 72 66 64 20 3d 20 2d 31 3b  ile->dirfd = -1;
cf9f0 20 20 20 20 2f 2a 20 77 68 65 6e 20 77 65 20 61      /* when we a
cfa00 72 65 20 64 6f 6e 65 2e 20 2a 2f 0a 20 20 7d 0a  re done. */.  }.
cfa10 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
cfa20 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75  OK;.}../*.** Tru
cfa30 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69  ncate an open fi
cfa40 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 69 65  le to a specifie
cfa50 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63  d size.*/.static
cfa60 20 69 6e 74 20 75 6e 69 78 54 72 75 6e 63 61 74   int unixTruncat
cfa70 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
cfa80 69 64 2c 20 69 36 34 20 6e 42 79 74 65 29 7b 0a  id, i64 nByte){.
cfa90 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65    int rc;.  asse
cfaa0 72 74 28 20 69 64 20 29 3b 0a 20 20 53 69 6d 75  rt( id );.  Simu
cfab0 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74  lateIOError( ret
cfac0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
cfad0 5f 54 52 55 4e 43 41 54 45 20 29 3b 0a 20 20 72  _TRUNCATE );.  r
cfae0 63 20 3d 20 66 74 72 75 6e 63 61 74 65 28 28 28  c = ftruncate(((
cfaf0 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 68  unixFile*)id)->h
cfb00 2c 20 28 6f 66 66 5f 74 29 6e 42 79 74 65 29 3b  , (off_t)nByte);
cfb10 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20  .  if( rc ){.   
cfb20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
cfb30 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a 20  OERR_TRUNCATE;. 
cfb40 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
cfb50 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
cfb60 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72  }.}../*.** Deter
cfb70 6d 69 6e 65 20 74 68 65 20 63 75 72 72 65 6e 74  mine the current
cfb80 20 73 69 7a 65 20 6f 66 20 61 20 66 69 6c 65 20   size of a file 
cfb90 69 6e 20 62 79 74 65 73 0a 2a 2f 0a 73 74 61 74  in bytes.*/.stat
cfba0 69 63 20 69 6e 74 20 75 6e 69 78 46 69 6c 65 53  ic int unixFileS
cfbb0 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ize(sqlite3_file
cfbc0 20 2a 69 64 2c 20 69 36 34 20 2a 70 53 69 7a 65   *id, i64 *pSize
cfbd0 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  ){.  int rc;.  s
cfbe0 74 72 75 63 74 20 73 74 61 74 20 62 75 66 3b 0a  truct stat buf;.
cfbf0 20 20 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a    assert( id );.
cfc00 20 20 72 63 20 3d 20 66 73 74 61 74 28 28 28 75    rc = fstat(((u
cfc10 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c  nixFile*)id)->h,
cfc20 20 26 62 75 66 29 3b 0a 20 20 53 69 6d 75 6c 61   &buf);.  Simula
cfc30 74 65 49 4f 45 72 72 6f 72 28 20 72 63 3d 31 20  teIOError( rc=1 
cfc40 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29  );.  if( rc!=0 )
cfc50 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
cfc60 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b  ITE_IOERR_FSTAT;
cfc70 0a 20 20 7d 0a 20 20 2a 70 53 69 7a 65 20 3d 20  .  }.  *pSize = 
cfc80 62 75 66 2e 73 74 5f 73 69 7a 65 3b 0a 0a 20 20  buf.st_size;..  
cfc90 2f 2a 20 57 68 65 6e 20 6f 70 65 6e 69 6e 67 20  /* When opening 
cfca0 61 20 7a 65 72 6f 2d 73 69 7a 65 20 64 61 74 61  a zero-size data
cfcb0 62 61 73 65 2c 20 74 68 65 20 66 69 6e 64 4c 6f  base, the findLo
cfcc0 63 6b 49 6e 66 6f 28 29 20 70 72 6f 63 65 64 75  ckInfo() procedu
cfcd0 72 65 0a 20 20 2a 2a 20 77 72 69 74 65 73 20 61  re.  ** writes a
cfce0 20 73 69 6e 67 6c 65 20 62 79 74 65 20 69 6e 74   single byte int
cfcf0 6f 20 74 68 61 74 20 66 69 6c 65 20 69 6e 20 6f  o that file in o
cfd00 72 64 65 72 20 74 6f 20 77 6f 72 6b 20 61 72 6f  rder to work aro
cfd10 75 6e 64 20 61 20 62 75 67 0a 20 20 2a 2a 20 69  und a bug.  ** i
cfd20 6e 20 74 68 65 20 4f 53 2d 58 20 6d 73 64 6f 73  n the OS-X msdos
cfd30 20 66 69 6c 65 73 79 73 74 65 6d 2e 20 20 49 6e   filesystem.  In
cfd40 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20   order to avoid 
cfd50 70 72 6f 62 6c 65 6d 73 20 77 69 74 68 20 75 70  problems with up
cfd60 70 65 72 0a 20 20 2a 2a 20 6c 61 79 65 72 73 2c  per.  ** layers,
cfd70 20 77 65 20 6e 65 65 64 20 74 6f 20 72 65 70 6f   we need to repo
cfd80 72 74 20 74 68 69 73 20 66 69 6c 65 20 73 69 7a  rt this file siz
cfd90 65 20 61 73 20 7a 65 72 6f 20 65 76 65 6e 20 74  e as zero even t
cfda0 68 6f 75 67 68 20 69 74 20 69 73 0a 20 20 2a 2a  hough it is.  **
cfdb0 20 72 65 61 6c 6c 79 20 31 2e 20 20 20 54 69 63   really 1.   Tic
cfdc0 6b 65 74 20 23 33 32 36 30 2e 0a 20 20 2a 2f 0a  ket #3260..  */.
cfdd0 20 20 69 66 28 20 2a 70 53 69 7a 65 3d 3d 31 20    if( *pSize==1 
cfde0 29 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 0a 0a  ) *pSize = 0;...
cfdf0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
cfe00 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  OK;.}../*.** Thi
cfe10 73 20 72 6f 75 74 69 6e 65 20 74 72 61 6e 73 6c  s routine transl
cfe20 61 74 65 73 20 61 20 73 74 61 6e 64 61 72 64 20  ates a standard 
cfe30 50 4f 53 49 58 20 65 72 72 6e 6f 20 63 6f 64 65  POSIX errno code
cfe40 20 69 6e 74 6f 20 73 6f 6d 65 74 68 69 6e 67 0a   into something.
cfe50 2a 2a 20 75 73 65 66 75 6c 20 74 6f 20 74 68 65  ** useful to the
cfe60 20 63 6c 69 65 6e 74 73 20 6f 66 20 74 68 65 20   clients of the 
cfe70 73 71 6c 69 74 65 33 20 66 75 6e 63 74 69 6f 6e  sqlite3 function
cfe80 73 2e 20 20 53 70 65 63 69 66 69 63 61 6c 6c 79  s.  Specifically
cfe90 2c 20 69 74 20 69 73 0a 2a 2a 20 69 6e 74 65 6e  , it is.** inten
cfea0 64 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 74 65  ded to translate
cfeb0 20 61 20 76 61 72 69 65 74 79 20 6f 66 20 22 74   a variety of "t
cfec0 72 79 20 61 67 61 69 6e 22 20 65 72 72 6f 72 73  ry again" errors
cfed0 20 69 6e 74 6f 20 53 51 4c 49 54 45 5f 42 55 53   into SQLITE_BUS
cfee0 59 0a 2a 2a 20 61 6e 64 20 61 20 76 61 72 69 65  Y.** and a varie
cfef0 74 79 20 6f 66 20 22 70 6c 65 61 73 65 20 63 6c  ty of "please cl
cff00 6f 73 65 20 74 68 65 20 66 69 6c 65 20 64 65 73  ose the file des
cff10 63 72 69 70 74 6f 72 20 4e 4f 57 22 20 65 72 72  criptor NOW" err
cff20 6f 72 73 20 69 6e 74 6f 20 0a 2a 2a 20 53 51 4c  ors into .** SQL
cff30 49 54 45 5f 49 4f 45 52 52 0a 2a 2a 20 0a 2a 2a  ITE_IOERR.** .**
cff40 20 45 72 72 6f 72 73 20 64 75 72 69 6e 67 20 69   Errors during i
cff50 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66  nitialization of
cff60 20 6c 6f 63 6b 73 2c 20 6f 72 20 66 69 6c 65 20   locks, or file 
cff70 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 66  system support f
cff80 6f 72 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 73 68 6f  or locks,.** sho
cff90 75 6c 64 20 68 61 6e 64 6c 65 20 45 4e 4f 4c 43  uld handle ENOLC
cffa0 4b 2c 20 45 4e 4f 54 53 55 50 2c 20 45 4f 50 4e  K, ENOTSUP, EOPN
cffb0 4f 54 53 55 50 50 20 73 65 70 61 72 61 74 65 6c  OTSUPP separatel
cffc0 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  y..*/.static int
cffd0 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d   sqliteErrorFrom
cffe0 50 6f 73 69 78 45 72 72 6f 72 28 69 6e 74 20 70  PosixError(int p
cfff0 6f 73 69 78 45 72 72 6f 72 2c 20 69 6e 74 20 73  osixError, int s
d0000 71 6c 69 74 65 49 4f 45 72 72 29 20 7b 0a 20 20  qliteIOErr) {.  
d0010 73 77 69 74 63 68 20 28 70 6f 73 69 78 45 72 72  switch (posixErr
d0020 6f 72 29 20 7b 0a 20 20 63 61 73 65 20 30 3a 20  or) {.  case 0: 
d0030 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
d0040 54 45 5f 4f 4b 3b 0a 20 20 20 20 0a 20 20 63 61  TE_OK;.    .  ca
d0050 73 65 20 45 41 47 41 49 4e 3a 0a 20 20 63 61 73  se EAGAIN:.  cas
d0060 65 20 45 54 49 4d 45 44 4f 55 54 3a 0a 20 20 63  e ETIMEDOUT:.  c
d0070 61 73 65 20 45 42 55 53 59 3a 0a 20 20 63 61 73  ase EBUSY:.  cas
d0080 65 20 45 49 4e 54 52 3a 0a 20 20 63 61 73 65 20  e EINTR:.  case 
d0090 45 4e 4f 4c 43 4b 3a 20 20 0a 20 20 20 20 2f 2a  ENOLCK:  .    /*
d00a0 20 72 61 6e 64 6f 6d 20 4e 46 53 20 72 65 74 72   random NFS retr
d00b0 79 20 65 72 72 6f 72 2c 20 75 6e 6c 65 73 73 20  y error, unless 
d00c0 64 75 72 69 6e 67 20 66 69 6c 65 20 73 79 73 74  during file syst
d00d0 65 6d 20 73 75 70 70 6f 72 74 20 0a 20 20 20 20  em support .    
d00e0 20 2a 20 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e   * introspection
d00f0 2c 20 69 6e 20 77 68 69 63 68 20 69 74 20 61 63  , in which it ac
d0100 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77 68 61  tually means wha
d0110 74 20 69 74 20 73 61 79 73 20 2a 2f 0a 20 20 20  t it says */.   
d0120 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
d0130 55 53 59 3b 0a 20 20 20 20 0a 20 20 63 61 73 65  USY;.    .  case
d0140 20 45 41 43 43 45 53 3a 20 0a 20 20 20 20 2f 2a   EACCES: .    /*
d0150 20 45 41 43 43 45 53 20 69 73 20 6c 69 6b 65 20   EACCES is like 
d0160 45 41 47 41 49 4e 20 64 75 72 69 6e 67 20 6c 6f  EAGAIN during lo
d0170 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73  cking operations
d0180 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 74  , but not any ot
d0190 68 65 72 20 74 69 6d 65 2a 2f 0a 20 20 20 20 69  her time*/.    i
d01a0 66 28 20 28 73 71 6c 69 74 65 49 4f 45 72 72 20  f( (sqliteIOErr 
d01b0 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  == SQLITE_IOERR_
d01c0 4c 4f 43 4b 29 20 7c 7c 20 0a 09 28 73 71 6c 69  LOCK) || ..(sqli
d01d0 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54  teIOErr == SQLIT
d01e0 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 20  E_IOERR_UNLOCK) 
d01f0 7c 7c 20 0a 09 28 73 71 6c 69 74 65 49 4f 45 72  || ..(sqliteIOEr
d0200 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  r == SQLITE_IOER
d0210 52 5f 52 44 4c 4f 43 4b 29 20 7c 7c 0a 09 28 73  R_RDLOCK) ||..(s
d0220 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 51  qliteIOErr == SQ
d0230 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b  LITE_IOERR_CHECK
d0240 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 20 29 7b  RESERVEDLOCK) ){
d0250 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
d0260 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d  LITE_BUSY;.    }
d0270 0a 20 20 20 20 2f 2a 20 65 6c 73 65 20 66 61 6c  .    /* else fal
d0280 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20 20 63  l through */.  c
d0290 61 73 65 20 45 50 45 52 4d 3a 20 0a 20 20 20 20  ase EPERM: .    
d02a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45  return SQLITE_PE
d02b0 52 4d 3b 0a 20 20 20 20 0a 20 20 63 61 73 65 20  RM;.    .  case 
d02c0 45 44 45 41 44 4c 4b 3a 0a 20 20 20 20 72 65 74  EDEADLK:.    ret
d02d0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
d02e0 5f 42 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 0a 23  _BLOCKED;.    .#
d02f0 69 66 20 45 4f 50 4e 4f 54 53 55 50 50 21 3d 45  if EOPNOTSUPP!=E
d0300 4e 4f 54 53 55 50 0a 20 20 63 61 73 65 20 45 4f  NOTSUP.  case EO
d0310 50 4e 4f 54 53 55 50 50 3a 20 0a 20 20 20 20 2f  PNOTSUPP: .    /
d0320 2a 20 73 6f 6d 65 74 68 69 6e 67 20 77 65 6e 74  * something went
d0330 20 74 65 72 72 69 62 6c 79 20 61 77 72 79 2c 20   terribly awry, 
d0340 75 6e 6c 65 73 73 20 64 75 72 69 6e 67 20 66 69  unless during fi
d0350 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72  le system suppor
d0360 74 20 0a 20 20 20 20 20 2a 20 69 6e 74 72 6f 73  t .     * intros
d0370 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63  pection, in whic
d0380 68 20 69 74 20 61 63 74 75 61 6c 6c 79 20 6d 65  h it actually me
d0390 61 6e 73 20 77 68 61 74 20 69 74 20 73 61 79 73  ans what it says
d03a0 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65   */.#endif.#ifde
d03b0 66 20 45 4e 4f 54 53 55 50 0a 20 20 63 61 73 65  f ENOTSUP.  case
d03c0 20 45 4e 4f 54 53 55 50 3a 20 0a 20 20 20 20 2f   ENOTSUP: .    /
d03d0 2a 20 69 6e 76 61 6c 69 64 20 66 64 2c 20 75 6e  * invalid fd, un
d03e0 6c 65 73 73 20 64 75 72 69 6e 67 20 66 69 6c 65  less during file
d03f0 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20   system support 
d0400 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69  introspection, i
d0410 6e 20 77 68 69 63 68 20 0a 20 20 20 20 20 2a 20  n which .     * 
d0420 69 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e  it actually mean
d0430 73 20 77 68 61 74 20 69 74 20 73 61 79 73 20 2a  s what it says *
d0440 2f 0a 23 65 6e 64 69 66 0a 20 20 63 61 73 65 20  /.#endif.  case 
d0450 45 49 4f 3a 0a 20 20 63 61 73 65 20 45 42 41 44  EIO:.  case EBAD
d0460 46 3a 0a 20 20 63 61 73 65 20 45 49 4e 56 41 4c  F:.  case EINVAL
d0470 3a 0a 20 20 63 61 73 65 20 45 4e 4f 54 43 4f 4e  :.  case ENOTCON
d0480 4e 3a 0a 20 20 63 61 73 65 20 45 4e 4f 44 45 56  N:.  case ENODEV
d0490 3a 0a 20 20 63 61 73 65 20 45 4e 58 49 4f 3a 0a  :.  case ENXIO:.
d04a0 20 20 63 61 73 65 20 45 4e 4f 45 4e 54 3a 0a 20    case ENOENT:. 
d04b0 20 63 61 73 65 20 45 53 54 41 4c 45 3a 0a 20 20   case ESTALE:.  
d04c0 63 61 73 65 20 45 4e 4f 53 59 53 3a 0a 20 20 20  case ENOSYS:.   
d04d0 20 2f 2a 20 74 68 65 73 65 20 73 68 6f 75 6c 64   /* these should
d04e0 20 66 6f 72 63 65 20 74 68 65 20 63 6c 69 65 6e   force the clien
d04f0 74 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 66  t to close the f
d0500 69 6c 65 20 61 6e 64 20 72 65 63 6f 6e 6e 65 63  ile and reconnec
d0510 74 20 2a 2f 0a 20 20 20 20 0a 20 20 64 65 66 61  t */.    .  defa
d0520 75 6c 74 3a 20 0a 20 20 20 20 72 65 74 75 72 6e  ult: .    return
d0530 20 73 71 6c 69 74 65 49 4f 45 72 72 3b 0a 20 20   sqliteIOErr;.  
d0540 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
d0550 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69  routine checks i
d0560 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53  f there is a RES
d0570 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20  ERVED lock held 
d0580 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64  on the specified
d0590 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73  .** file by this
d05a0 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72   or any other pr
d05b0 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61  ocess. If such a
d05c0 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73   lock is held, s
d05d0 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74  et *pResOut.** t
d05e0 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c  o a non-zero val
d05f0 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52  ue otherwise *pR
d0600 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20  esOut is set to 
d0610 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72  zero.  The retur
d0620 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65  n value.** is se
d0630 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75  t to SQLITE_OK u
d0640 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72  nless an I/O err
d0650 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67  or occurs during
d0660 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a   lock checking..
d0670 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e  */.static int un
d0680 69 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  ixCheckReservedL
d0690 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
d06a0 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f   *id, int *pResO
d06b0 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ut){.  int rc = 
d06c0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74  SQLITE_OK;.  int
d06d0 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20   reserved = 0;. 
d06e0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
d06f0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
d0700 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  ;..  SimulateIOE
d0710 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c  rror( return SQL
d0720 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52  ITE_IOERR_CHECKR
d0730 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a  ESERVEDLOCK; );.
d0740 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
d0750 20 29 3b 0a 20 20 65 6e 74 65 72 4d 75 74 65 78   );.  enterMutex
d0760 28 29 3b 20 2f 2a 20 42 65 63 61 75 73 65 20 70  (); /* Because p
d0770 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 73 20 73  File->pLock is s
d0780 68 61 72 65 64 20 61 63 72 6f 73 73 20 74 68 72  hared across thr
d0790 65 61 64 73 20 2a 2f 0a 0a 20 20 2f 2a 20 43 68  eads */..  /* Ch
d07a0 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 20  eck if a thread 
d07b0 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20  in this process 
d07c0 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63  holds such a loc
d07d0 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65  k */.  if( pFile
d07e0 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70  ->pLock->locktyp
d07f0 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b  e>SHARED_LOCK ){
d0800 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20  .    reserved = 
d0810 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 74 68  1;.  }..  /* Oth
d0820 65 72 77 69 73 65 20 73 65 65 20 69 66 20 73 6f  erwise see if so
d0830 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  me other process
d0840 20 68 6f 6c 64 73 20 69 74 2e 0a 20 20 2a 2f 0a   holds it..  */.
d0850 20 20 69 66 28 20 21 72 65 73 65 72 76 65 64 20    if( !reserved 
d0860 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 66 6c  ){.    struct fl
d0870 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 20 20 6c 6f  ock lock;.    lo
d0880 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45  ck.l_whence = SE
d0890 45 4b 5f 53 45 54 3b 0a 20 20 20 20 6c 6f 63 6b  EK_SET;.    lock
d08a0 2e 6c 5f 73 74 61 72 74 20 3d 20 52 45 53 45 52  .l_start = RESER
d08b0 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 6c 6f  VED_BYTE;.    lo
d08c0 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20  ck.l_len = 1;.  
d08d0 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20    lock.l_type = 
d08e0 46 5f 57 52 4c 43 4b 3b 0a 20 20 20 20 69 66 20  F_WRLCK;.    if 
d08f0 28 2d 31 20 3d 3d 20 66 63 6e 74 6c 28 70 46 69  (-1 == fcntl(pFi
d0900 6c 65 2d 3e 68 2c 20 46 5f 47 45 54 4c 4b 2c 20  le->h, F_GETLK, 
d0910 26 6c 6f 63 6b 29 29 20 7b 0a 20 20 20 20 20 20  &lock)) {.      
d0920 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72  int tErrno = err
d0930 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  no;.      rc = s
d0940 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
d0950 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
d0960 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48   SQLITE_IOERR_CH
d0970 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29  ECKRESERVEDLOCK)
d0980 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  ;.      pFile->l
d0990 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e  astErrno = tErrn
d09a0 6f 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 66  o;.    } else if
d09b0 28 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 21 3d 46  ( lock.l_type!=F
d09c0 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20  _UNLCK ){.      
d09d0 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20  reserved = 1;.  
d09e0 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 6c 65 61    }.  }.  .  lea
d09f0 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53 54  veMutex();.  OST
d0a00 52 41 43 45 34 28 22 54 45 53 54 20 57 52 2d 4c  RACE4("TEST WR-L
d0a10 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 2c  OCK %d %d %d\n",
d0a20 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72   pFile->h, rc, r
d0a30 65 73 65 72 76 65 64 29 3b 0a 0a 20 20 2a 70 52  eserved);..  *pR
d0a40 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64  esOut = reserved
d0a50 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
d0a60 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65  ../*.** Lock the
d0a70 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c   file with the l
d0a80 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 79  ock specified by
d0a90 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74   parameter lockt
d0aa0 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20  ype - one.** of 
d0ab0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a  the following:.*
d0ac0 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41  *.**     (1) SHA
d0ad0 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20  RED_LOCK.**     
d0ae0 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43  (2) RESERVED_LOC
d0af0 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e  K.**     (3) PEN
d0b00 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  DING_LOCK.**    
d0b10 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c   (4) EXCLUSIVE_L
d0b20 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69  OCK.**.** Someti
d0b30 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 74  mes when request
d0b40 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61  ing one lock sta
d0b50 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c  te, additional l
d0b60 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72  ock states.** ar
d0b70 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65  e inserted in be
d0b80 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b  tween.  The lock
d0b90 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f  ing might fail o
d0ba0 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74  n one of the lat
d0bb0 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e  er.** transition
d0bc0 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f  s leaving the lo
d0bd0 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 65  ck state differe
d0be0 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20  nt from what it 
d0bf0 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73  started but.** s
d0c00 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74  till short of it
d0c10 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c  s goal.  The fol
d0c20 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f  lowing chart sho
d0c30 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a  ws the allowed.*
d0c40 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e  * transitions an
d0c50 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69  d the inserted i
d0c60 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74  ntermediate stat
d0c70 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c  es:.**.**    UNL
d0c80 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a  OCKED -> SHARED.
d0c90 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20  **    SHARED -> 
d0ca0 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53  RESERVED.**    S
d0cb0 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e  HARED -> (PENDIN
d0cc0 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a  G) -> EXCLUSIVE.
d0cd0 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 2d  **    RESERVED -
d0ce0 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45  > (PENDING) -> E
d0cf0 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50  XCLUSIVE.**    P
d0d00 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53  ENDING -> EXCLUS
d0d10 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  IVE.**.** This r
d0d20 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79  outine will only
d0d30 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b   increase a lock
d0d40 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 74  .  Use the sqlit
d0d50 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20  e3OsUnlock().** 
d0d60 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72  routine to lower
d0d70 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c   a locking level
d0d80 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
d0d90 75 6e 69 78 4c 6f 63 6b 28 73 71 6c 69 74 65 33  unixLock(sqlite3
d0da0 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c  _file *id, int l
d0db0 6f 63 6b 74 79 70 65 29 7b 0a 20 20 2f 2a 20 54  ocktype){.  /* T
d0dc0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 64 65 73  he following des
d0dd0 63 72 69 62 65 73 20 74 68 65 20 69 6d 70 6c 65  cribes the imple
d0de0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
d0df0 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73 20 61   various locks a
d0e00 6e 64 0a 20 20 2a 2a 20 6c 6f 63 6b 20 74 72 61  nd.  ** lock tra
d0e10 6e 73 69 74 69 6f 6e 73 20 69 6e 20 74 65 72 6d  nsitions in term
d0e20 73 20 6f 66 20 74 68 65 20 50 4f 53 49 58 20 61  s of the POSIX a
d0e30 64 76 69 73 6f 72 79 20 73 68 61 72 65 64 20 61  dvisory shared a
d0e40 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 20 20 2a  nd exclusive.  *
d0e50 2a 20 6c 6f 63 6b 20 70 72 69 6d 69 74 69 76 65  * lock primitive
d0e60 73 20 28 63 61 6c 6c 65 64 20 72 65 61 64 2d 6c  s (called read-l
d0e70 6f 63 6b 73 20 61 6e 64 20 77 72 69 74 65 2d 6c  ocks and write-l
d0e80 6f 63 6b 73 20 62 65 6c 6f 77 2c 20 74 6f 20 61  ocks below, to a
d0e90 76 6f 69 64 0a 20 20 2a 2a 20 63 6f 6e 66 75 73  void.  ** confus
d0ea0 69 6f 6e 20 77 69 74 68 20 53 51 4c 69 74 65 20  ion with SQLite 
d0eb0 6c 6f 63 6b 20 6e 61 6d 65 73 29 2e 20 54 68 65  lock names). The
d0ec0 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72 65 20   algorithms are 
d0ed0 63 6f 6d 70 6c 69 63 61 74 65 64 0a 20 20 2a 2a  complicated.  **
d0ee0 20 73 6c 69 67 68 74 6c 79 20 69 6e 20 6f 72 64   slightly in ord
d0ef0 65 72 20 74 6f 20 62 65 20 63 6f 6d 70 61 74 69  er to be compati
d0f00 62 6c 65 20 77 69 74 68 20 77 69 6e 64 6f 77 73  ble with windows
d0f10 20 73 79 73 74 65 6d 73 20 73 69 6d 75 6c 74 61   systems simulta
d0f20 6e 65 6f 75 73 6c 79 0a 20 20 2a 2a 20 61 63 63  neously.  ** acc
d0f30 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20  essing the same 
d0f40 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 69  database file, i
d0f50 6e 20 63 61 73 65 20 74 68 61 74 20 69 73 20 65  n case that is e
d0f60 76 65 72 20 72 65 71 75 69 72 65 64 2e 0a 20 20  ver required..  
d0f70 2a 2a 0a 20 20 2a 2a 20 53 79 6d 62 6f 6c 73 20  **.  ** Symbols 
d0f80 64 65 66 69 6e 65 64 20 69 6e 20 6f 73 2e 68 20  defined in os.h 
d0f90 69 6e 64 65 6e 74 69 66 79 20 74 68 65 20 27 70  indentify the 'p
d0fa0 65 6e 64 69 6e 67 20 62 79 74 65 27 20 61 6e 64  ending byte' and
d0fb0 20 74 68 65 20 27 72 65 73 65 72 76 65 64 0a 20   the 'reserved. 
d0fc0 20 2a 2a 20 62 79 74 65 27 2c 20 65 61 63 68 20   ** byte', each 
d0fd0 73 69 6e 67 6c 65 20 62 79 74 65 73 20 61 74 20  single bytes at 
d0fe0 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66 73 65  well known offse
d0ff0 74 73 2c 20 61 6e 64 20 74 68 65 20 27 73 68 61  ts, and the 'sha
d1000 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61  red byte.  ** ra
d1010 6e 67 65 27 2c 20 61 20 72 61 6e 67 65 20 6f 66  nge', a range of
d1020 20 35 31 30 20 62 79 74 65 73 20 61 74 20 61 20   510 bytes at a 
d1030 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66 73 65  well known offse
d1040 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 6f 20  t..  **.  ** To 
d1050 6f 62 74 61 69 6e 20 61 20 53 48 41 52 45 44 20  obtain a SHARED 
d1060 6c 6f 63 6b 2c 20 61 20 72 65 61 64 2d 6c 6f 63  lock, a read-loc
d1070 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 6f 6e  k is obtained on
d1080 20 74 68 65 20 27 70 65 6e 64 69 6e 67 0a 20 20   the 'pending.  
d1090 2a 2a 20 62 79 74 65 27 2e 20 20 49 66 20 74 68  ** byte'.  If th
d10a0 69 73 20 69 73 20 73 75 63 63 65 73 73 66 75 6c  is is successful
d10b0 2c 20 61 20 72 61 6e 64 6f 6d 20 62 79 74 65 20  , a random byte 
d10c0 66 72 6f 6d 20 74 68 65 20 27 73 68 61 72 65 64  from the 'shared
d10d0 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67 65   byte.  ** range
d10e0 27 20 69 73 20 72 65 61 64 2d 6c 6f 63 6b 65 64  ' is read-locked
d10f0 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 6f 6e   and the lock on
d1100 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20 62 79   the 'pending by
d1110 74 65 27 20 72 65 6c 65 61 73 65 64 2e 0a 20 20  te' released..  
d1120 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f 63 65 73  **.  ** A proces
d1130 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69  s may only obtai
d1140 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  n a RESERVED loc
d1150 6b 20 61 66 74 65 72 20 69 74 20 68 61 73 20 61  k after it has a
d1160 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 0a 20 20   SHARED lock..  
d1170 2a 2a 20 41 20 52 45 53 45 52 56 45 44 20 6c 6f  ** A RESERVED lo
d1180 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65  ck is implemente
d1190 64 20 62 79 20 67 72 61 62 62 69 6e 67 20 61 20  d by grabbing a 
d11a0 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68  write-lock on th
d11b0 65 0a 20 20 2a 2a 20 27 72 65 73 65 72 76 65 64  e.  ** 'reserved
d11c0 20 62 79 74 65 27 2e 20 0a 20 20 2a 2a 0a 20 20   byte'. .  **.  
d11d0 2a 2a 20 41 20 70 72 6f 63 65 73 73 20 6d 61 79  ** A process may
d11e0 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20 61 20 50   only obtain a P
d11f0 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 61 66 74 65  ENDING lock afte
d1200 72 20 69 74 20 68 61 73 20 6f 62 74 61 69 6e 65  r it has obtaine
d1210 64 20 61 0a 20 20 2a 2a 20 53 48 41 52 45 44 20  d a.  ** SHARED 
d1220 6c 6f 63 6b 2e 20 41 20 50 45 4e 44 49 4e 47 20  lock. A PENDING 
d1230 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65 6e  lock is implemen
d1240 74 65 64 20 62 79 20 6f 62 74 61 69 6e 69 6e 67  ted by obtaining
d1250 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 0a 20 20   a write-lock.  
d1260 2a 2a 20 6f 6e 20 74 68 65 20 27 70 65 6e 64 69  ** on the 'pendi
d1270 6e 67 20 62 79 74 65 27 2e 20 54 68 69 73 20 65  ng byte'. This e
d1280 6e 73 75 72 65 73 20 74 68 61 74 20 6e 6f 20 6e  nsures that no n
d1290 65 77 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20  ew SHARED locks 
d12a0 63 61 6e 20 62 65 0a 20 20 2a 2a 20 6f 62 74 61  can be.  ** obta
d12b0 69 6e 65 64 2c 20 62 75 74 20 65 78 69 73 74 69  ined, but existi
d12c0 6e 67 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20  ng SHARED locks 
d12d0 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 70  are allowed to p
d12e0 65 72 73 69 73 74 2e 20 41 20 70 72 6f 63 65 73  ersist. A proces
d12f0 73 0a 20 20 2a 2a 20 64 6f 65 73 20 6e 6f 74 20  s.  ** does not 
d1300 68 61 76 65 20 74 6f 20 6f 62 74 61 69 6e 20 61  have to obtain a
d1310 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f   RESERVED lock o
d1320 6e 20 74 68 65 20 77 61 79 20 74 6f 20 61 20 50  n the way to a P
d1330 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 0a 20 20 2a  ENDING lock..  *
d1340 2a 20 54 68 69 73 20 70 72 6f 70 65 72 74 79 20  * This property 
d1350 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 61  is used by the a
d1360 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 72 6f 6c  lgorithm for rol
d1370 6c 69 6e 67 20 62 61 63 6b 20 61 20 6a 6f 75 72  ling back a jour
d1380 6e 61 6c 20 66 69 6c 65 0a 20 20 2a 2a 20 61 66  nal file.  ** af
d1390 74 65 72 20 61 20 63 72 61 73 68 2e 0a 20 20 2a  ter a crash..  *
d13a0 2a 0a 20 20 2a 2a 20 41 6e 20 45 58 43 4c 55 53  *.  ** An EXCLUS
d13b0 49 56 45 20 6c 6f 63 6b 2c 20 6f 62 74 61 69 6e  IVE lock, obtain
d13c0 65 64 20 61 66 74 65 72 20 61 20 50 45 4e 44 49  ed after a PENDI
d13d0 4e 47 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c  NG lock is held,
d13e0 20 69 73 0a 20 20 2a 2a 20 69 6d 70 6c 65 6d 65   is.  ** impleme
d13f0 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e 69 6e  nted by obtainin
d1400 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f  g a write-lock o
d1410 6e 20 74 68 65 20 65 6e 74 69 72 65 20 27 73 68  n the entire 'sh
d1420 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72  ared byte.  ** r
d1430 61 6e 67 65 27 2e 20 53 69 6e 63 65 20 61 6c 6c  ange'. Since all
d1440 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 72 65 71   other locks req
d1450 75 69 72 65 20 61 20 72 65 61 64 2d 6c 6f 63 6b  uire a read-lock
d1460 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 62   on one of the b
d1470 79 74 65 73 0a 20 20 2a 2a 20 77 69 74 68 69 6e  ytes.  ** within
d1480 20 74 68 69 73 20 72 61 6e 67 65 2c 20 74 68 69   this range, thi
d1490 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 6e  s ensures that n
d14a0 6f 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 61 72  o other locks ar
d14b0 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 0a 20 20  e held on the.  
d14c0 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 0a 20 20  ** database. .  
d14d0 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 72 65 61 73  **.  ** The reas
d14e0 6f 6e 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65  on a single byte
d14f0 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 20   cannot be used 
d1500 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 27  instead of the '
d1510 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a  shared byte.  **
d1520 20 72 61 6e 67 65 27 20 69 73 20 74 68 61 74 20   range' is that 
d1530 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f 66  some versions of
d1540 20 77 69 6e 64 6f 77 73 20 64 6f 20 6e 6f 74 20   windows do not 
d1550 73 75 70 70 6f 72 74 20 72 65 61 64 2d 6c 6f 63  support read-loc
d1560 6b 73 2e 20 42 79 0a 20 20 2a 2a 20 6c 6f 63 6b  ks. By.  ** lock
d1570 69 6e 67 20 61 20 72 61 6e 64 6f 6d 20 62 79 74  ing a random byt
d1580 65 20 66 72 6f 6d 20 61 20 72 61 6e 67 65 2c 20  e from a range, 
d1590 63 6f 6e 63 75 72 72 65 6e 74 20 53 48 41 52 45  concurrent SHARE
d15a0 44 20 6c 6f 63 6b 73 20 6d 61 79 20 65 78 69 73  D locks may exis
d15b0 74 0a 20 20 2a 2a 20 65 76 65 6e 20 69 66 20 74  t.  ** even if t
d15c0 68 65 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d 69  he locking primi
d15d0 74 69 76 65 20 75 73 65 64 20 69 73 20 61 6c 77  tive used is alw
d15e0 61 79 73 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b  ays a write-lock
d15f0 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20  ..  */.  int rc 
d1600 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75  = SQLITE_OK;.  u
d1610 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
d1620 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
d1630 20 20 73 74 72 75 63 74 20 6c 6f 63 6b 49 6e 66    struct lockInf
d1640 6f 20 2a 70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65  o *pLock = pFile
d1650 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 73 74 72 75 63  ->pLock;.  struc
d1660 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20  t flock lock;.  
d1670 69 6e 74 20 73 3b 0a 0a 20 20 61 73 73 65 72 74  int s;..  assert
d1680 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54  ( pFile );.  OST
d1690 52 41 43 45 37 28 22 4c 4f 43 4b 20 20 20 20 25  RACE7("LOCK    %
d16a0 64 20 25 73 20 77 61 73 20 25 73 28 25 73 2c 25  d %s was %s(%s,%
d16b0 64 29 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46  d) pid=%d\n", pF
d16c0 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 6c 6f  ile->h,.      lo
d16d0 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74  cktypeName(lockt
d16e0 79 70 65 29 2c 20 6c 6f 63 6b 74 79 70 65 4e 61  ype), locktypeNa
d16f0 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  me(pFile->lockty
d1700 70 65 29 2c 0a 20 20 20 20 20 20 6c 6f 63 6b 74  pe),.      lockt
d1710 79 70 65 4e 61 6d 65 28 70 4c 6f 63 6b 2d 3e 6c  ypeName(pLock->l
d1720 6f 63 6b 74 79 70 65 29 2c 20 70 4c 6f 63 6b 2d  ocktype), pLock-
d1730 3e 63 6e 74 20 2c 20 67 65 74 70 69 64 28 29 29  >cnt , getpid())
d1740 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  ;..  /* If there
d1750 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f   is already a lo
d1760 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 20  ck of this type 
d1770 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74  or more restrict
d1780 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20  ive on the.  ** 
d1790 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20 6e 6f 74  unixFile, do not
d17a0 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20  hing. Don't use 
d17b0 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78  the end_lock: ex
d17c0 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a  it path, as.  **
d17d0 20 65 6e 74 65 72 4d 75 74 65 78 28 29 20 68 61   enterMutex() ha
d17e0 73 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 64  sn't been called
d17f0 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28   yet..  */.  if(
d1800 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d1810 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20  >=locktype ){.  
d1820 20 20 4f 53 54 52 41 43 45 33 28 22 4c 4f 43 4b    OSTRACE3("LOCK
d1830 20 20 20 20 25 64 20 25 73 20 6f 6b 20 28 61 6c      %d %s ok (al
d1840 72 65 61 64 79 20 68 65 6c 64 29 5c 6e 22 2c 20  ready held)\n", 
d1850 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20  pFile->h,.      
d1860 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61        locktypeNa
d1870 6d 65 28 6c 6f 63 6b 74 79 70 65 29 29 3b 0a 20  me(locktype));. 
d1880 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
d1890 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d  _OK;.  }..  /* M
d18a0 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63  ake sure the loc
d18b0 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73  king sequence is
d18c0 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20   correct.  */.  
d18d0 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c  assert( pFile->l
d18e0 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b  ocktype!=NO_LOCK
d18f0 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48   || locktype==SH
d1900 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61  ARED_LOCK );.  a
d1910 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21  ssert( locktype!
d1920 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b  =PENDING_LOCK );
d1930 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
d1940 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f  ype!=RESERVED_LO
d1950 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63  CK || pFile->loc
d1960 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
d1970 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73  CK );..  /* This
d1980 20 6d 75 74 65 78 20 69 73 20 6e 65 65 64 65 64   mutex is needed
d1990 20 62 65 63 61 75 73 65 20 70 46 69 6c 65 2d 3e   because pFile->
d19a0 70 4c 6f 63 6b 20 69 73 20 73 68 61 72 65 64 20  pLock is shared 
d19b0 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 0a 20  across threads. 
d19c0 20 2a 2f 0a 20 20 65 6e 74 65 72 4d 75 74 65 78   */.  enterMutex
d19d0 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73  ();..  /* Make s
d19e0 75 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ure the current 
d19f0 74 68 72 65 61 64 20 6f 77 6e 73 20 74 68 65 20  thread owns the 
d1a00 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63  pFile..  */.  rc
d1a10 20 3d 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72   = transferOwner
d1a20 73 68 69 70 28 70 46 69 6c 65 29 3b 0a 20 20 69  ship(pFile);.  i
d1a30 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
d1a40 20 29 7b 0a 20 20 20 20 6c 65 61 76 65 4d 75 74   ){.    leaveMut
d1a50 65 78 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ex();.    return
d1a60 20 72 63 3b 0a 20 20 7d 0a 20 20 70 4c 6f 63 6b   rc;.  }.  pLock
d1a70 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b   = pFile->pLock;
d1a80 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d 65 20 74  ..  /* If some t
d1a90 68 72 65 61 64 20 75 73 69 6e 67 20 74 68 69 73  hread using this
d1aa0 20 50 49 44 20 68 61 73 20 61 20 6c 6f 63 6b 20   PID has a lock 
d1ab0 76 69 61 20 61 20 64 69 66 66 65 72 65 6e 74 20  via a different 
d1ac0 75 6e 69 78 46 69 6c 65 2a 0a 20 20 2a 2a 20 68  unixFile*.  ** h
d1ad0 61 6e 64 6c 65 20 74 68 61 74 20 70 72 65 63 6c  andle that precl
d1ae0 75 64 65 73 20 74 68 65 20 72 65 71 75 65 73 74  udes the request
d1af0 65 64 20 6c 6f 63 6b 2c 20 72 65 74 75 72 6e 20  ed lock, return 
d1b00 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  BUSY..  */.  if(
d1b10 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70   (pFile->locktyp
d1b20 65 21 3d 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79  e!=pLock->lockty
d1b30 70 65 20 26 26 20 0a 20 20 20 20 20 20 20 20 20  pe && .         
d1b40 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70   (pLock->locktyp
d1b50 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20  e>=PENDING_LOCK 
d1b60 7c 7c 20 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52  || locktype>SHAR
d1b70 45 44 5f 4c 4f 43 4b 29 29 0a 20 20 29 7b 0a 20  ED_LOCK)).  ){. 
d1b80 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
d1b90 55 53 59 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e  USY;.    goto en
d1ba0 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f  d_lock;.  }..  /
d1bb0 2a 20 49 66 20 61 20 53 48 41 52 45 44 20 6c 6f  * If a SHARED lo
d1bc0 63 6b 20 69 73 20 72 65 71 75 65 73 74 65 64 2c  ck is requested,
d1bd0 20 61 6e 64 20 73 6f 6d 65 20 74 68 72 65 61 64   and some thread
d1be0 20 75 73 69 6e 67 20 74 68 69 73 20 50 49 44 20   using this PID 
d1bf0 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20 68 61 73  already.  ** has
d1c00 20 61 20 53 48 41 52 45 44 20 6f 72 20 52 45 53   a SHARED or RES
d1c10 45 52 56 45 44 20 6c 6f 63 6b 2c 20 74 68 65 6e  ERVED lock, then
d1c20 20 69 6e 63 72 65 6d 65 6e 74 20 72 65 66 65 72   increment refer
d1c30 65 6e 63 65 20 63 6f 75 6e 74 73 20 61 6e 64 0a  ence counts and.
d1c40 20 20 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49    ** return SQLI
d1c50 54 45 5f 4f 4b 2e 0a 20 20 2a 2f 0a 20 20 69 66  TE_OK..  */.  if
d1c60 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  ( locktype==SHAR
d1c70 45 44 5f 4c 4f 43 4b 20 26 26 20 0a 20 20 20 20  ED_LOCK && .    
d1c80 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79    (pLock->lockty
d1c90 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe==SHARED_LOCK 
d1ca0 7c 7c 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79  || pLock->lockty
d1cb0 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43  pe==RESERVED_LOC
d1cc0 4b 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  K) ){.    assert
d1cd0 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  ( locktype==SHAR
d1ce0 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 61  ED_LOCK );.    a
d1cf0 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f  ssert( pFile->lo
d1d00 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a 20 20 20  cktype==0 );.   
d1d10 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e   assert( pLock->
d1d20 63 6e 74 3e 30 20 29 3b 0a 20 20 20 20 70 46 69  cnt>0 );.    pFi
d1d30 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53  le->locktype = S
d1d40 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20  HARED_LOCK;.    
d1d50 70 4c 6f 63 6b 2d 3e 63 6e 74 2b 2b 3b 0a 20 20  pLock->cnt++;.  
d1d60 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e    pFile->pOpen->
d1d70 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 67 6f 74  nLock++;.    got
d1d80 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a  o end_lock;.  }.
d1d90 0a 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20  .  lock.l_len = 
d1da0 31 4c 3b 0a 0a 20 20 6c 6f 63 6b 2e 6c 5f 77 68  1L;..  lock.l_wh
d1db0 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b  ence = SEEK_SET;
d1dc0 0a 0a 20 20 2f 2a 20 41 20 50 45 4e 44 49 4e 47  ..  /* A PENDING
d1dd0 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64 65 64 20   lock is needed 
d1de0 62 65 66 6f 72 65 20 61 63 71 75 69 72 69 6e 67  before acquiring
d1df0 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 61   a SHARED lock a
d1e00 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 61  nd before.  ** a
d1e10 63 71 75 69 72 69 6e 67 20 61 6e 20 45 58 43 4c  cquiring an EXCL
d1e20 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 46 6f 72  USIVE lock.  For
d1e30 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f 63 6b   the SHARED lock
d1e40 2c 20 74 68 65 20 50 45 4e 44 49 4e 47 20 77 69  , the PENDING wi
d1e50 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 6c 65 61  ll.  ** be relea
d1e60 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  sed..  */.  if( 
d1e70 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
d1e80 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c 7c 20  _LOCK .      || 
d1e90 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55  (locktype==EXCLU
d1ea0 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69  SIVE_LOCK && pFi
d1eb0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50 45 4e  le->locktype<PEN
d1ec0 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a  DING_LOCK).  ){.
d1ed0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20      lock.l_type 
d1ee0 3d 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  = (locktype==SHA
d1ef0 52 45 44 5f 4c 4f 43 4b 3f 46 5f 52 44 4c 43 4b  RED_LOCK?F_RDLCK
d1f00 3a 46 5f 57 52 4c 43 4b 29 3b 0a 20 20 20 20 6c  :F_WRLCK);.    l
d1f10 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45  ock.l_start = PE
d1f20 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20  NDING_BYTE;.    
d1f30 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d  s = fcntl(pFile-
d1f40 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f  >h, F_SETLK, &lo
d1f50 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 73 3d 3d  ck);.    if( s==
d1f60 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 69 6e  (-1) ){.      in
d1f70 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  t tErrno = errno
d1f80 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
d1f90 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69  iteErrorFromPosi
d1fa0 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53  xError(tErrno, S
d1fb0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b  QLITE_IOERR_LOCK
d1fc0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f  );.      if( IS_
d1fd0 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29  LOCK_ERROR(rc) )
d1fe0 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d  {.        pFile-
d1ff0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72  >lastErrno = tEr
d2000 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rno;.      }.   
d2010 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b     goto end_lock
d2020 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a 20 20  ;.    }.  }...  
d2030 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 67 65  /* If control ge
d2040 74 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74  ts to this point
d2050 2c 20 74 68 65 6e 20 61 63 74 75 61 6c 6c 79 20  , then actually 
d2060 67 6f 20 61 68 65 61 64 20 61 6e 64 20 6d 61 6b  go ahead and mak
d2070 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67  e.  ** operating
d2080 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 66 6f   system calls fo
d2090 72 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  r the specified 
d20a0 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  lock..  */.  if(
d20b0 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45   locktype==SHARE
d20c0 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 69 6e  D_LOCK ){.    in
d20d0 74 20 74 45 72 72 6e 6f 20 3d 20 30 3b 0a 20 20  t tErrno = 0;.  
d20e0 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d    assert( pLock-
d20f0 3e 63 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61  >cnt==0 );.    a
d2100 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 6c 6f  ssert( pLock->lo
d2110 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a 0a 20 20  cktype==0 );..  
d2120 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20 74 68 65    /* Now get the
d2130 20 72 65 61 64 2d 6c 6f 63 6b 20 2a 2f 0a 20 20   read-lock */.  
d2140 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d    lock.l_start =
d2150 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20   SHARED_FIRST;. 
d2160 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20     lock.l_len = 
d2170 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20  SHARED_SIZE;.   
d2180 20 69 66 28 20 28 73 20 3d 20 66 63 6e 74 6c 28   if( (s = fcntl(
d2190 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c  pFile->h, F_SETL
d21a0 4b 2c 20 26 6c 6f 63 6b 29 29 3d 3d 28 2d 31 29  K, &lock))==(-1)
d21b0 20 29 7b 0a 20 20 20 20 20 20 74 45 72 72 6e 6f   ){.      tErrno
d21c0 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a   = errno;.    }.
d21d0 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20      /* Drop the 
d21e0 74 65 6d 70 6f 72 61 72 79 20 50 45 4e 44 49 4e  temporary PENDIN
d21f0 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 6c 6f  G lock */.    lo
d2200 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e  ck.l_start = PEN
d2210 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 6c  DING_BYTE;.    l
d2220 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c 3b 0a  ock.l_len = 1L;.
d2230 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20      lock.l_type 
d2240 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 69  = F_UNLCK;.    i
d2250 66 28 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e  f( fcntl(pFile->
d2260 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63  h, F_SETLK, &loc
d2270 6b 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 69  k)!=0 ){.      i
d2280 66 28 20 73 20 21 3d 20 2d 31 20 29 7b 0a 20 20  f( s != -1 ){.  
d2290 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 6f        /* This co
d22a0 75 6c 64 20 68 61 70 70 65 6e 20 77 69 74 68 20  uld happen with 
d22b0 61 20 6e 65 74 77 6f 72 6b 20 6d 6f 75 6e 74 20  a network mount 
d22c0 2a 2f 0a 20 20 20 20 20 20 20 20 74 45 72 72 6e  */.        tErrn
d22d0 6f 20 3d 20 65 72 72 6e 6f 3b 20 0a 20 20 20 20  o = errno; .    
d22e0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45      rc = sqliteE
d22f0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72  rrorFromPosixErr
d2300 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54  or(tErrno, SQLIT
d2310 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b  E_IOERR_UNLOCK);
d2320 20 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 53   .        if( IS
d2330 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20  _LOCK_ERROR(rc) 
d2340 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 69  ){.          pFi
d2350 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
d2360 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20  tErrno;.        
d2370 7d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65  }.        goto e
d2380 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 7d  nd_lock;.      }
d2390 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
d23a0 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  ==(-1) ){.      
d23b0 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72  rc = sqliteError
d23c0 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74  FromPosixError(t
d23d0 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f  Errno, SQLITE_IO
d23e0 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20  ERR_LOCK);.     
d23f0 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52   if( IS_LOCK_ERR
d2400 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20  OR(rc) ){.      
d2410 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
d2420 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
d2430 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
d2440 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63        pFile->loc
d2450 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c  ktype = SHARED_L
d2460 4f 43 4b 3b 0a 20 20 20 20 20 20 70 46 69 6c 65  OCK;.      pFile
d2470 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b  ->pOpen->nLock++
d2480 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 63  ;.      pLock->c
d2490 6e 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  nt = 1;.    }.  
d24a0 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79  }else if( lockty
d24b0 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f  pe==EXCLUSIVE_LO
d24c0 43 4b 20 26 26 20 70 4c 6f 63 6b 2d 3e 63 6e 74  CK && pLock->cnt
d24d0 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20  >1 ){.    /* We 
d24e0 61 72 65 20 74 72 79 69 6e 67 20 66 6f 72 20 61  are trying for a
d24f0 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  n exclusive lock
d2500 20 62 75 74 20 61 6e 6f 74 68 65 72 20 74 68 72   but another thr
d2510 65 61 64 20 69 6e 20 74 68 69 73 0a 20 20 20 20  ead in this.    
d2520 2a 2a 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20  ** same process 
d2530 69 73 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e 67  is still holding
d2540 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20   a shared lock. 
d2550 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  */.    rc = SQLI
d2560 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65  TE_BUSY;.  }else
d2570 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 71  {.    /* The req
d2580 75 65 73 74 20 77 61 73 20 66 6f 72 20 61 20 52  uest was for a R
d2590 45 53 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55  ESERVED or EXCLU
d25a0 53 49 56 45 20 6c 6f 63 6b 2e 20 20 49 74 20 69  SIVE lock.  It i
d25b0 73 0a 20 20 20 20 2a 2a 20 61 73 73 75 6d 65 64  s.    ** assumed
d25c0 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 61   that there is a
d25d0 20 53 48 41 52 45 44 20 6f 72 20 67 72 65 61 74   SHARED or great
d25e0 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66  er lock on the f
d25f0 69 6c 65 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61  ile.    ** alrea
d2600 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  dy..    */.    a
d2610 73 73 65 72 74 28 20 30 21 3d 70 46 69 6c 65 2d  ssert( 0!=pFile-
d2620 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20  >locktype );.   
d2630 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46   lock.l_type = F
d2640 5f 57 52 4c 43 4b 3b 0a 20 20 20 20 73 77 69 74  _WRLCK;.    swit
d2650 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a  ch( locktype ){.
d2660 20 20 20 20 20 20 63 61 73 65 20 52 45 53 45 52        case RESER
d2670 56 45 44 5f 4c 4f 43 4b 3a 0a 20 20 20 20 20 20  VED_LOCK:.      
d2680 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d    lock.l_start =
d2690 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a   RESERVED_BYTE;.
d26a0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
d26b0 20 20 20 20 20 63 61 73 65 20 45 58 43 4c 55 53       case EXCLUS
d26c0 49 56 45 5f 4c 4f 43 4b 3a 0a 20 20 20 20 20 20  IVE_LOCK:.      
d26d0 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d    lock.l_start =
d26e0 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20   SHARED_FIRST;. 
d26f0 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65         lock.l_le
d2700 6e 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b  n = SHARED_SIZE;
d2710 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
d2720 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20        default:. 
d2730 20 20 20 20 20 20 20 61 73 73 65 72 74 28 30 29         assert(0)
d2740 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 20 3d 20  ;.    }.    s = 
d2750 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20  fcntl(pFile->h, 
d2760 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b  F_SETLK, &lock);
d2770 0a 20 20 20 20 69 66 28 20 73 3d 3d 28 2d 31 29  .    if( s==(-1)
d2780 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 45   ){.      int tE
d2790 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
d27a0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45      rc = sqliteE
d27b0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72  rrorFromPosixErr
d27c0 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54  or(tErrno, SQLIT
d27d0 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20  E_IOERR_LOCK);. 
d27e0 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b       if( IS_LOCK
d27f0 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20  _ERROR(rc) ){.  
d2800 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
d2810 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b  tErrno = tErrno;
d2820 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
d2830 20 7d 0a 20 20 0a 20 20 69 66 28 20 72 63 3d 3d   }.  .  if( rc==
d2840 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
d2850 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d2860 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20   = locktype;.   
d2870 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65   pLock->locktype
d2880 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d   = locktype;.  }
d2890 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 70  else if( locktyp
d28a0 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  e==EXCLUSIVE_LOC
d28b0 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  K ){.    pFile->
d28c0 6c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49  locktype = PENDI
d28d0 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 70 4c 6f  NG_LOCK;.    pLo
d28e0 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 50  ck->locktype = P
d28f0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 7d  ENDING_LOCK;.  }
d2900 0a 0a 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 6c 65  ..end_lock:.  le
d2910 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53  aveMutex();.  OS
d2920 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20  TRACE4("LOCK    
d2930 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69  %d %s %s\n", pFi
d2940 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e  le->h, locktypeN
d2950 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a  ame(locktype), .
d2960 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45        rc==SQLITE
d2970 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61  _OK ? "ok" : "fa
d2980 69 6c 65 64 22 29 3b 0a 20 20 72 65 74 75 72 6e  iled");.  return
d2990 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f   rc;.}../*.** Lo
d29a0 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  wer the locking 
d29b0 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65  level on file de
d29c0 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74  scriptor pFile t
d29d0 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63  o locktype.  loc
d29e0 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65  ktype.** must be
d29f0 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20   either NO_LOCK 
d2a00 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a  or SHARED_LOCK..
d2a10 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63  **.** If the loc
d2a20 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68  king level of th
d2a30 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
d2a40 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 20  r is already at 
d2a50 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20  or below.** the 
d2a60 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e  requested lockin
d2a70 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f  g level, this ro
d2a80 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70  utine is a no-op
d2a90 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
d2aa0 75 6e 69 78 55 6e 6c 6f 63 6b 28 73 71 6c 69 74  unixUnlock(sqlit
d2ab0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
d2ac0 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 73 74   locktype){.  st
d2ad0 72 75 63 74 20 6c 6f 63 6b 49 6e 66 6f 20 2a 70  ruct lockInfo *p
d2ae0 4c 6f 63 6b 3b 0a 20 20 73 74 72 75 63 74 20 66  Lock;.  struct f
d2af0 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74  lock lock;.  int
d2b00 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
d2b10 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
d2b20 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
d2b30 69 64 3b 0a 20 20 69 6e 74 20 68 3b 0a 0a 20 20  id;.  int h;..  
d2b40 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
d2b50 0a 20 20 4f 53 54 52 41 43 45 37 28 22 55 4e 4c  .  OSTRACE7("UNL
d2b60 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 25  OCK  %d %d was %
d2b70 64 28 25 64 2c 25 64 29 20 70 69 64 3d 25 64 5c  d(%d,%d) pid=%d\
d2b80 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f  n", pFile->h, lo
d2b90 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 70 46  cktype,.      pF
d2ba0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70  ile->locktype, p
d2bb0 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63  File->pLock->loc
d2bc0 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 70 4c  ktype, pFile->pL
d2bd0 6f 63 6b 2d 3e 63 6e 74 2c 20 67 65 74 70 69 64  ock->cnt, getpid
d2be0 28 29 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ());..  assert( 
d2bf0 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44  locktype<=SHARED
d2c00 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 70  _LOCK );.  if( p
d2c10 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 3d  File->locktype<=
d2c20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20  locktype ){.    
d2c30 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
d2c40 3b 0a 20 20 7d 0a 20 20 69 66 28 20 43 48 45 43  ;.  }.  if( CHEC
d2c50 4b 5f 54 48 52 45 41 44 49 44 28 70 46 69 6c 65  K_THREADID(pFile
d2c60 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
d2c70 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
d2c80 20 7d 0a 20 20 65 6e 74 65 72 4d 75 74 65 78 28   }.  enterMutex(
d2c90 29 3b 0a 20 20 68 20 3d 20 70 46 69 6c 65 2d 3e  );.  h = pFile->
d2ca0 68 3b 0a 20 20 70 4c 6f 63 6b 20 3d 20 70 46 69  h;.  pLock = pFi
d2cb0 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 61 73 73  le->pLock;.  ass
d2cc0 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 21  ert( pLock->cnt!
d2cd0 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c  =0 );.  if( pFil
d2ce0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52  e->locktype>SHAR
d2cf0 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 61  ED_LOCK ){.    a
d2d00 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 6c 6f  ssert( pLock->lo
d2d10 63 6b 74 79 70 65 3d 3d 70 46 69 6c 65 2d 3e 6c  cktype==pFile->l
d2d20 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 53  ocktype );.    S
d2d30 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65  imulateIOErrorBe
d2d40 6e 69 67 6e 28 31 29 3b 0a 20 20 20 20 53 69 6d  nign(1);.    Sim
d2d50 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 68 3d  ulateIOError( h=
d2d60 28 2d 31 29 20 29 0a 20 20 20 20 53 69 6d 75 6c  (-1) ).    Simul
d2d70 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e  ateIOErrorBenign
d2d80 28 30 29 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63  (0);.    if( loc
d2d90 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
d2da0 43 4b 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b  CK ){.      lock
d2db0 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43  .l_type = F_RDLC
d2dc0 4b 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f  K;.      lock.l_
d2dd0 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45  whence = SEEK_SE
d2de0 54 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f  T;.      lock.l_
d2df0 73 74 61 72 74 20 3d 20 53 48 41 52 45 44 5f 46  start = SHARED_F
d2e00 49 52 53 54 3b 0a 20 20 20 20 20 20 6c 6f 63 6b  IRST;.      lock
d2e10 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 45 44 5f  .l_len = SHARED_
d2e20 53 49 5a 45 3b 0a 20 20 20 20 20 20 69 66 28 20  SIZE;.      if( 
d2e30 66 63 6e 74 6c 28 68 2c 20 46 5f 53 45 54 4c 4b  fcntl(h, F_SETLK
d2e40 2c 20 26 6c 6f 63 6b 29 3d 3d 28 2d 31 29 20 29  , &lock)==(-1) )
d2e50 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 74 45  {.        int tE
d2e60 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
d2e70 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
d2e80 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45  eErrorFromPosixE
d2e90 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c  rror(tErrno, SQL
d2ea0 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b  ITE_IOERR_RDLOCK
d2eb0 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 49  );.        if( I
d2ec0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29  S_LOCK_ERROR(rc)
d2ed0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46   ){.          pF
d2ee0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
d2ef0 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20   tErrno;.       
d2f00 20 7d 0a 09 09 09 09 67 6f 74 6f 20 65 6e 64 5f   }.....goto end_
d2f10 75 6e 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 7d 0a  unlock;.      }.
d2f20 20 20 20 20 7d 0a 20 20 20 20 6c 6f 63 6b 2e 6c      }.    lock.l
d2f30 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b  _type = F_UNLCK;
d2f40 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e  .    lock.l_when
d2f50 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20  ce = SEEK_SET;. 
d2f60 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20     lock.l_start 
d2f70 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a  = PENDING_BYTE;.
d2f80 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d      lock.l_len =
d2f90 20 32 4c 3b 20 20 61 73 73 65 72 74 28 20 50 45   2L;  assert( PE
d2fa0 4e 44 49 4e 47 5f 42 59 54 45 2b 31 3d 3d 52 45  NDING_BYTE+1==RE
d2fb0 53 45 52 56 45 44 5f 42 59 54 45 20 29 3b 0a 20  SERVED_BYTE );. 
d2fc0 20 20 20 69 66 28 20 66 63 6e 74 6c 28 68 2c 20     if( fcntl(h, 
d2fd0 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 21  F_SETLK, &lock)!
d2fe0 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 70  =(-1) ){.      p
d2ff0 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  Lock->locktype =
d3000 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20   SHARED_LOCK;.  
d3010 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
d3020 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  nt tErrno = errn
d3030 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  o;.      rc = sq
d3040 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
d3050 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20  ixError(tErrno, 
d3060 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c  SQLITE_IOERR_UNL
d3070 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20  OCK);.      if( 
d3080 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63  IS_LOCK_ERROR(rc
d3090 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69  ) ){.        pFi
d30a0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
d30b0 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a  tErrno;.      }.
d30c0 09 09 09 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f  ...goto end_unlo
d30d0 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ck;.    }.  }.  
d30e0 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f  if( locktype==NO
d30f0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 73 74 72  _LOCK ){.    str
d3100 75 63 74 20 6f 70 65 6e 43 6e 74 20 2a 70 4f 70  uct openCnt *pOp
d3110 65 6e 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72  en;..    /* Decr
d3120 65 6d 65 6e 74 20 74 68 65 20 73 68 61 72 65 64  ement the shared
d3130 20 6c 6f 63 6b 20 63 6f 75 6e 74 65 72 2e 20 20   lock counter.  
d3140 52 65 6c 65 61 73 65 20 74 68 65 20 6c 6f 63 6b  Release the lock
d3150 20 75 73 69 6e 67 20 61 6e 0a 20 20 20 20 2a 2a   using an.    **
d3160 20 4f 53 20 63 61 6c 6c 20 6f 6e 6c 79 20 77 68   OS call only wh
d3170 65 6e 20 61 6c 6c 20 74 68 72 65 61 64 73 20 69  en all threads i
d3180 6e 20 74 68 69 73 20 73 61 6d 65 20 70 72 6f 63  n this same proc
d3190 65 73 73 20 68 61 76 65 20 72 65 6c 65 61 73 65  ess have release
d31a0 64 0a 20 20 20 20 2a 2a 20 74 68 65 20 6c 6f 63  d.    ** the loc
d31b0 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4c  k..    */.    pL
d31c0 6f 63 6b 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 20 20  ock->cnt--;.    
d31d0 69 66 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d  if( pLock->cnt==
d31e0 30 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e  0 ){.      lock.
d31f0 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b  l_type = F_UNLCK
d3200 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77  ;.      lock.l_w
d3210 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54  hence = SEEK_SET
d3220 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73  ;.      lock.l_s
d3230 74 61 72 74 20 3d 20 6c 6f 63 6b 2e 6c 5f 6c 65  tart = lock.l_le
d3240 6e 20 3d 20 30 4c 3b 0a 20 20 20 20 20 20 53 69  n = 0L;.      Si
d3250 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e  mulateIOErrorBen
d3260 69 67 6e 28 31 29 3b 0a 20 20 20 20 20 20 53 69  ign(1);.      Si
d3270 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 68  mulateIOError( h
d3280 3d 28 2d 31 29 20 29 0a 20 20 20 20 20 20 53 69  =(-1) ).      Si
d3290 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e  mulateIOErrorBen
d32a0 69 67 6e 28 30 29 3b 0a 20 20 20 20 20 20 69 66  ign(0);.      if
d32b0 28 20 66 63 6e 74 6c 28 68 2c 20 46 5f 53 45 54  ( fcntl(h, F_SET
d32c0 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 28 2d 31 29  LK, &lock)!=(-1)
d32d0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 6f 63   ){.        pLoc
d32e0 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f  k->locktype = NO
d32f0 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 7d 65 6c  _LOCK;.      }el
d3300 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  se{.        int 
d3310 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
d3320 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
d3330 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69  iteErrorFromPosi
d3340 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53  xError(tErrno, S
d3350 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f  QLITE_IOERR_UNLO
d3360 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  CK);.        if(
d3370 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72   IS_LOCK_ERROR(r
d3380 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  c) ){.          
d3390 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
d33a0 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20   = tErrno;.     
d33b0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4c 6f     }.        pLo
d33c0 63 6b 2d 3e 63 6e 74 20 3d 20 31 3b 0a 09 09 09  ck->cnt = 1;....
d33d0 09 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b  .goto end_unlock
d33e0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
d33f0 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e  .    /* Decremen
d3400 74 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 6c  t the count of l
d3410 6f 63 6b 73 20 61 67 61 69 6e 73 74 20 74 68 69  ocks against thi
d3420 73 20 73 61 6d 65 20 66 69 6c 65 2e 20 20 57 68  s same file.  Wh
d3430 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f  en the.    ** co
d3440 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f  unt reaches zero
d3450 2c 20 63 6c 6f 73 65 20 61 6e 79 20 6f 74 68 65  , close any othe
d3460 72 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  r file descripto
d3470 72 73 20 77 68 6f 73 65 20 63 6c 6f 73 65 0a 20  rs whose close. 
d3480 20 20 20 2a 2a 20 77 61 73 20 64 65 66 65 72 72     ** was deferr
d3490 65 64 20 62 65 63 61 75 73 65 20 6f 66 20 6f 75  ed because of ou
d34a0 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2e  tstanding locks.
d34b0 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
d34c0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
d34d0 0a 20 20 20 20 20 20 70 4f 70 65 6e 20 3d 20 70  .      pOpen = p
d34e0 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20  File->pOpen;.   
d34f0 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2d     pOpen->nLock-
d3500 2d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  -;.      assert(
d3510 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e 3d 30   pOpen->nLock>=0
d3520 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f   );.      if( pO
d3530 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 26 26  pen->nLock==0 &&
d3540 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67   pOpen->nPending
d3550 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  >0 ){.        in
d3560 74 20 69 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  t i;.        for
d3570 28 69 3d 30 3b 20 69 3c 70 4f 70 65 6e 2d 3e 6e  (i=0; i<pOpen->n
d3580 50 65 6e 64 69 6e 67 3b 20 69 2b 2b 29 7b 0a 20  Pending; i++){. 
d3590 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 28 70           close(p
d35a0 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 5b 69  Open->aPending[i
d35b0 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ]);.        }.  
d35c0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
d35d0 65 65 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69  ee(pOpen->aPendi
d35e0 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 70 4f 70  ng);.        pOp
d35f0 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 20 3d 20 30  en->nPending = 0
d3600 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d  ;.        pOpen-
d3610 3e 61 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20  >aPending = 0;. 
d3620 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
d3630 0a 09 0a 65 6e 64 5f 75 6e 6c 6f 63 6b 3a 0a 20  ...end_unlock:. 
d3640 20 6c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20   leaveMutex();. 
d3650 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
d3660 4f 4b 20 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  OK ) pFile->lock
d3670 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b  type = locktype;
d3680 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
d3690 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
d36a0 74 69 6f 6e 20 70 65 72 66 6f 72 6d 73 20 74 68  tion performs th
d36b0 65 20 70 61 72 74 73 20 6f 66 20 74 68 65 20 22  e parts of the "
d36c0 63 6c 6f 73 65 20 66 69 6c 65 22 20 6f 70 65 72  close file" oper
d36d0 61 74 69 6f 6e 20 0a 2a 2a 20 63 6f 6d 6d 6f 6e  ation .** common
d36e0 20 74 6f 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20   to all locking 
d36f0 73 63 68 65 6d 65 73 2e 20 49 74 20 63 6c 6f 73  schemes. It clos
d3700 65 73 20 74 68 65 20 64 69 72 65 63 74 6f 72 79  es the directory
d3710 20 61 6e 64 20 66 69 6c 65 0a 2a 2a 20 68 61 6e   and file.** han
d3720 64 6c 65 73 2c 20 69 66 20 74 68 65 79 20 61 72  dles, if they ar
d3730 65 20 76 61 6c 69 64 2c 20 61 6e 64 20 73 65 74  e valid, and set
d3740 73 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f 66 20  s all fields of 
d3750 74 68 65 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20  the unixFile.** 
d3760 73 74 72 75 63 74 75 72 65 20 74 6f 20 30 2e 0a  structure to 0..
d3770 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6c  */.static int cl
d3780 6f 73 65 55 6e 69 78 46 69 6c 65 28 73 71 6c 69  oseUnixFile(sqli
d3790 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20  te3_file *id){. 
d37a0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
d37b0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
d37c0 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 20 29 7b  ;.  if( pFile ){
d37d0 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  .    if( pFile->
d37e0 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20  dirfd>=0 ){.    
d37f0 20 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 64    close(pFile->d
d3800 69 72 66 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20  irfd);.    }.   
d3810 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e 3d 30   if( pFile->h>=0
d3820 20 29 7b 0a 20 20 20 20 20 20 63 6c 6f 73 65 28   ){.      close(
d3830 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 7d  pFile->h);.    }
d3840 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 43  .    OSTRACE2("C
d3850 4c 4f 53 45 20 20 20 25 2d 33 64 5c 6e 22 2c 20  LOSE   %-3d\n", 
d3860 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 4f  pFile->h);.    O
d3870 70 65 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a  penCounter(-1);.
d3880 20 20 20 20 6d 65 6d 73 65 74 28 70 46 69 6c 65      memset(pFile
d3890 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78  , 0, sizeof(unix
d38a0 46 69 6c 65 29 29 3b 0a 20 20 7d 0a 20 20 72 65  File));.  }.  re
d38b0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
d38c0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61  }../*.** Close a
d38d0 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
d38e0 20 69 6e 74 20 75 6e 69 78 43 6c 6f 73 65 28 73   int unixClose(s
d38f0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
d3900 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20  {.  if( id ){.  
d3910 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
d3920 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a 29  e = (unixFile *)
d3930 69 64 3b 0a 20 20 20 20 75 6e 69 78 55 6e 6c 6f  id;.    unixUnlo
d3940 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b  ck(id, NO_LOCK);
d3950 0a 20 20 20 20 65 6e 74 65 72 4d 75 74 65 78 28  .    enterMutex(
d3960 29 3b 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65  );.    if( pFile
d3970 2d 3e 70 4f 70 65 6e 20 26 26 20 70 46 69 6c 65  ->pOpen && pFile
d3980 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 20 29  ->pOpen->nLock )
d3990 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  {.      /* If th
d39a0 65 72 65 20 61 72 65 20 6f 75 74 73 74 61 6e 64  ere are outstand
d39b0 69 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f 20 6e 6f  ing locks, do no
d39c0 74 20 61 63 74 75 61 6c 6c 79 20 63 6c 6f 73 65  t actually close
d39d0 20 74 68 65 20 66 69 6c 65 20 6a 75 73 74 0a 20   the file just. 
d39e0 20 20 20 20 20 2a 2a 20 79 65 74 20 62 65 63 61       ** yet beca
d39f0 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64 20 63  use that would c
d3a00 6c 65 61 72 20 74 68 6f 73 65 20 6c 6f 63 6b 73  lear those locks
d3a10 2e 20 20 49 6e 73 74 65 61 64 2c 20 61 64 64 20  .  Instead, add 
d3a20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a  the file.      *
d3a30 2a 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20  * descriptor to 
d3a40 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 2e  pOpen->aPending.
d3a50 20 20 49 74 20 77 69 6c 6c 20 62 65 20 61 75 74    It will be aut
d3a60 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65  omatically close
d3a70 64 20 77 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20  d when.      ** 
d3a80 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 20 69 73  the last lock is
d3a90 20 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 20 20   cleared..      
d3aa0 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 2a 61 4e  */.      int *aN
d3ab0 65 77 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74  ew;.      struct
d3ac0 20 6f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 20   openCnt *pOpen 
d3ad0 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a  = pFile->pOpen;.
d3ae0 20 20 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c        aNew = sql
d3af0 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 4f 70  ite3_realloc(pOp
d3b00 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 2c 20 28 70  en->aPending, (p
d3b10 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 2b 31  Open->nPending+1
d3b20 29 2a 73 69 7a 65 6f 66 28 69 6e 74 29 20 29 3b  )*sizeof(int) );
d3b30 0a 20 20 20 20 20 20 69 66 28 20 61 4e 65 77 3d  .      if( aNew=
d3b40 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  =0 ){.        /*
d3b50 20 49 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69   If a malloc fai
d3b60 6c 73 2c 20 6a 75 73 74 20 6c 65 61 6b 20 74 68  ls, just leak th
d3b70 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
d3b80 72 20 2a 2f 0a 20 20 20 20 20 20 7d 65 6c 73 65  r */.      }else
d3b90 7b 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d  {.        pOpen-
d3ba0 3e 61 50 65 6e 64 69 6e 67 20 3d 20 61 4e 65 77  >aPending = aNew
d3bb0 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d  ;.        pOpen-
d3bc0 3e 61 50 65 6e 64 69 6e 67 5b 70 4f 70 65 6e 2d  >aPending[pOpen-
d3bd0 3e 6e 50 65 6e 64 69 6e 67 5d 20 3d 20 70 46 69  >nPending] = pFi
d3be0 6c 65 2d 3e 68 3b 0a 20 20 20 20 20 20 20 20 70  le->h;.        p
d3bf0 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 2b 2b  Open->nPending++
d3c00 3b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d  ;.        pFile-
d3c10 3e 68 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d  >h = -1;.      }
d3c20 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 6c 65 61  .    }.    relea
d3c30 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65  seLockInfo(pFile
d3c40 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 65  ->pLock);.    re
d3c50 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 70 46 69  leaseOpenCnt(pFi
d3c60 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20  le->pOpen);.    
d3c70 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64  closeUnixFile(id
d3c80 29 3b 0a 20 20 20 20 6c 65 61 76 65 4d 75 74 65  );.    leaveMute
d3c90 78 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  x();.  }.  retur
d3ca0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
d3cb0 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  .#if SQLITE_ENAB
d3cc0 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
d3cd0 0a 23 70 72 61 67 6d 61 20 6d 61 72 6b 20 41 46  .#pragma mark AF
d3ce0 50 20 53 75 70 70 6f 72 74 0a 0a 2f 2a 0a 20 2a  P Support../*. *
d3cf0 2a 20 54 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67  * The afpLocking
d3d00 43 6f 6e 74 65 78 74 20 73 74 72 75 63 74 75 72  Context structur
d3d10 65 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 61  e contains all a
d3d20 66 70 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 63  fp lock specific
d3d30 20 73 74 61 74 65 0a 20 2a 2f 0a 74 79 70 65 64   state. */.typed
d3d40 65 66 20 73 74 72 75 63 74 20 61 66 70 4c 6f 63  ef struct afpLoc
d3d50 6b 69 6e 67 43 6f 6e 74 65 78 74 20 61 66 70 4c  kingContext afpL
d3d60 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 73  ockingContext;.s
d3d70 74 72 75 63 74 20 61 66 70 4c 6f 63 6b 69 6e 67  truct afpLocking
d3d80 43 6f 6e 74 65 78 74 20 7b 0a 20 20 75 6e 73 69  Context {.  unsi
d3d90 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 73  gned long long s
d3da0 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 3b 0a 20  haredLockByte;. 
d3db0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c   const char *fil
d3dc0 65 50 61 74 68 3b 0a 7d 3b 0a 0a 73 74 72 75 63  ePath;.};..struc
d3dd0 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50  t ByteRangeLockP
d3de0 42 32 0a 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  B2.{.  unsigned 
d3df0 6c 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74  long long offset
d3e00 3b 20 20 20 20 20 20 20 20 2f 2a 20 6f 66 66 73  ;        /* offs
d3e10 65 74 20 74 6f 20 66 69 72 73 74 20 62 79 74 65  et to first byte
d3e20 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e   to lock */.  un
d3e30 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
d3e40 20 6c 65 6e 67 74 68 3b 20 20 20 20 20 20 20 20   length;        
d3e50 2f 2a 20 6e 62 72 20 6f 66 20 62 79 74 65 73 20  /* nbr of bytes 
d3e60 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73  to lock */.  uns
d3e70 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20  igned long long 
d3e80 72 65 74 52 61 6e 67 65 53 74 61 72 74 3b 20 2f  retRangeStart; /
d3e90 2a 20 6e 62 72 20 6f 66 20 31 73 74 20 62 79 74  * nbr of 1st byt
d3ea0 65 20 6c 6f 63 6b 65 64 20 69 66 20 73 75 63 63  e locked if succ
d3eb0 65 73 73 66 75 6c 20 2a 2f 0a 20 20 75 6e 73 69  essful */.  unsi
d3ec0 67 6e 65 64 20 63 68 61 72 20 75 6e 4c 6f 63 6b  gned char unLock
d3ed0 46 6c 61 67 3b 20 20 20 20 20 20 20 20 20 2f 2a  Flag;         /*
d3ee0 20 31 20 3d 20 75 6e 6c 6f 63 6b 2c 20 30 20 3d   1 = unlock, 0 =
d3ef0 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67   lock */.  unsig
d3f00 6e 65 64 20 63 68 61 72 20 73 74 61 72 74 45 6e  ned char startEn
d3f10 64 46 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a 20  dFlag;       /* 
d3f20 31 3d 72 65 6c 20 74 6f 20 65 6e 64 20 6f 66 20  1=rel to end of 
d3f30 66 6f 72 6b 2c 20 30 3d 72 65 6c 20 74 6f 20 73  fork, 0=rel to s
d3f40 74 61 72 74 20 2a 2f 0a 20 20 69 6e 74 20 66 64  tart */.  int fd
d3f50 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
d3f60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66              /* f
d3f70 69 6c 65 20 64 65 73 63 20 74 6f 20 61 73 73 6f  ile desc to asso
d3f80 63 20 74 68 69 73 20 6c 6f 63 6b 20 77 69 74 68  c this lock with
d3f90 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 20   */.};..#define 
d3fa0 61 66 70 66 73 42 79 74 65 52 61 6e 67 65 4c 6f  afpfsByteRangeLo
d3fb0 63 6b 32 46 53 43 54 4c 20 20 20 20 20 20 20 20  ck2FSCTL        
d3fc0 5f 49 4f 57 52 28 27 7a 27 2c 20 32 33 2c 20 73  _IOWR('z', 23, s
d3fd0 74 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c  truct ByteRangeL
d3fe0 6f 63 6b 50 42 32 29 0a 0a 2f 2a 20 0a 20 2a 2a  ockPB2)../* . **
d3ff0 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
d4000 4b 20 6f 6e 20 73 75 63 63 65 73 73 2c 20 53 51  K on success, SQ
d4010 4c 49 54 45 5f 42 55 53 59 20 6f 6e 20 66 61 69  LITE_BUSY on fai
d4020 6c 75 72 65 2e 0a 20 2a 2f 0a 73 74 61 74 69 63  lure.. */.static
d4030 20 69 6e 74 20 5f 41 46 50 46 53 53 65 74 4c 6f   int _AFPFSSetLo
d4040 63 6b 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  ck(.  const char
d4050 20 2a 70 61 74 68 2c 20 0a 20 20 75 6e 69 78 46   *path, .  unixF
d4060 69 6c 65 20 2a 70 46 69 6c 65 2c 20 0a 20 20 75  ile *pFile, .  u
d4070 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
d4080 67 20 6f 66 66 73 65 74 2c 20 0a 20 20 75 6e 73  g offset, .  uns
d4090 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20  igned long long 
d40a0 6c 65 6e 67 74 68 2c 20 0a 20 20 69 6e 74 20 73  length, .  int s
d40b0 65 74 4c 6f 63 6b 46 6c 61 67 0a 29 7b 0a 20 20  etLockFlag.){.  
d40c0 73 74 72 75 63 74 20 42 79 74 65 52 61 6e 67 65  struct ByteRange
d40d0 4c 6f 63 6b 50 42 32 20 20 20 20 20 20 20 70 62  LockPB2       pb
d40e0 3b 0a 20 20 69 6e 74 20 20 20 20 20 20 20 20 20  ;.  int         
d40f0 20 20 20 20 20 20 20 20 20 20 20 20 65 72 72 3b              err;
d4100 0a 20 20 0a 20 20 70 62 2e 75 6e 4c 6f 63 6b 46  .  .  pb.unLockF
d4110 6c 61 67 20 3d 20 73 65 74 4c 6f 63 6b 46 6c 61  lag = setLockFla
d4120 67 20 3f 20 30 20 3a 20 31 3b 0a 20 20 70 62 2e  g ? 0 : 1;.  pb.
d4130 73 74 61 72 74 45 6e 64 46 6c 61 67 20 3d 20 30  startEndFlag = 0
d4140 3b 0a 20 20 70 62 2e 6f 66 66 73 65 74 20 3d 20  ;.  pb.offset = 
d4150 6f 66 66 73 65 74 3b 0a 20 20 70 62 2e 6c 65 6e  offset;.  pb.len
d4160 67 74 68 20 3d 20 6c 65 6e 67 74 68 3b 20 0a 20  gth = length; . 
d4170 20 70 62 2e 66 64 20 3d 20 70 46 69 6c 65 2d 3e   pb.fd = pFile->
d4180 68 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 41  h;.  OSTRACE5("A
d4190 46 50 4c 4f 43 4b 20 73 65 74 74 69 6e 67 20 6c  FPLOCK setting l
d41a0 6f 63 6b 20 25 73 20 66 6f 72 20 25 64 20 69 6e  ock %s for %d in
d41b0 20 72 61 6e 67 65 20 25 6c 6c 78 3a 25 6c 6c 78   range %llx:%llx
d41c0 5c 6e 22 2c 20 0a 20 20 20 20 28 73 65 74 4c 6f  \n", .    (setLo
d41d0 63 6b 46 6c 61 67 3f 22 4f 4e 22 3a 22 4f 46 46  ckFlag?"ON":"OFF
d41e0 22 29 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6f 66  "), pFile->h, of
d41f0 66 73 65 74 2c 20 6c 65 6e 67 74 68 29 3b 0a 20  fset, length);. 
d4200 20 65 72 72 20 3d 20 66 73 63 74 6c 28 70 61 74   err = fsctl(pat
d4210 68 2c 20 61 66 70 66 73 42 79 74 65 52 61 6e 67  h, afpfsByteRang
d4220 65 4c 6f 63 6b 32 46 53 43 54 4c 2c 20 26 70 62  eLock2FSCTL, &pb
d4230 2c 20 30 29 3b 0a 20 20 69 66 20 28 20 65 72 72  , 0);.  if ( err
d4240 3d 3d 2d 31 20 29 20 7b 0a 20 20 20 20 69 6e 74  ==-1 ) {.    int
d4250 20 72 63 3b 0a 20 20 20 20 69 6e 74 20 74 45 72   rc;.    int tEr
d4260 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  rno = errno;.   
d4270 20 4f 53 54 52 41 43 45 34 28 22 41 46 50 4c 4f   OSTRACE4("AFPLO
d4280 43 4b 20 66 61 69 6c 65 64 20 74 6f 20 66 73 63  CK failed to fsc
d4290 74 6c 28 29 20 27 25 73 27 20 25 64 20 25 73 5c  tl() '%s' %d %s\
d42a0 6e 22 2c 20 70 61 74 68 2c 20 74 45 72 72 6e 6f  n", path, tErrno
d42b0 2c 20 73 74 72 65 72 72 6f 72 28 74 45 72 72 6e  , strerror(tErrn
d42c0 6f 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  o));.    rc = sq
d42d0 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
d42e0 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20  ixError(tErrno, 
d42f0 73 65 74 4c 6f 63 6b 46 6c 61 67 20 3f 20 53 51  setLockFlag ? SQ
d4300 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 20  LITE_IOERR_LOCK 
d4310 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55  : SQLITE_IOERR_U
d4320 4e 4c 4f 43 4b 29 3b 20 2f 2a 20 65 72 72 6f 72  NLOCK); /* error
d4330 20 2a 2f 0a 20 20 20 20 69 66 28 20 49 53 5f 4c   */.    if( IS_L
d4340 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b  OCK_ERROR(rc) ){
d4350 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  .      pFile->la
d4360 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f  stErrno = tErrno
d4370 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
d4380 72 6e 20 72 63 3b 0a 20 20 7d 20 65 6c 73 65 20  rn rc;.  } else 
d4390 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
d43a0 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ITE_OK;.  }.}../
d43b0 2a 20 41 46 50 2d 73 74 79 6c 65 20 72 65 73 65  * AFP-style rese
d43c0 72 76 65 64 20 6c 6f 63 6b 20 63 68 65 63 6b 69  rved lock checki
d43d0 6e 67 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65  ng following the
d43e0 20 62 65 68 61 76 69 6f 72 20 6f 66 20 0a 2a 2a   behavior of .**
d43f0 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76   unixCheckReserv
d4400 65 64 4c 6f 63 6b 2c 20 73 65 65 20 74 68 65 20  edLock, see the 
d4410 75 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76 65  unixCheckReserve
d4420 64 4c 6f 63 6b 20 66 75 6e 63 74 69 6f 6e 20 63  dLock function c
d4430 6f 6d 6d 65 6e 74 73 20 2a 2f 0a 73 74 61 74 69  omments */.stati
d4440 63 20 69 6e 74 20 61 66 70 43 68 65 63 6b 52 65  c int afpCheckRe
d4450 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74  servedLock(sqlit
d4460 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
d4470 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e   *pResOut){.  in
d4480 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
d4490 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64  ;.  int reserved
d44a0 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65   = 0;.  unixFile
d44b0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
d44c0 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 53 69  ile*)id;.  .  Si
d44d0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72  mulateIOError( r
d44e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
d44f0 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44  RR_CHECKRESERVED
d4500 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73  LOCK; );.  .  as
d4510 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20  sert( pFile );. 
d4520 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65   afpLockingConte
d4530 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 28 61  xt *context = (a
d4540 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  fpLockingContext
d4550 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69   *) pFile->locki
d4560 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a 20 20  ngContext;.  .  
d4570 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74 68  /* Check if a th
d4580 72 65 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f  read in this pro
d4590 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20  cess holds such 
d45a0 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20  a lock */.  if( 
d45b0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e  pFile->locktype>
d45c0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SHARED_LOCK ){. 
d45d0 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b     reserved = 1;
d45e0 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 4f 74 68  .  }.  .  /* Oth
d45f0 65 72 77 69 73 65 20 73 65 65 20 69 66 20 73 6f  erwise see if so
d4600 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  me other process
d4610 20 68 6f 6c 64 73 20 69 74 2e 0a 20 20 20 2a 2f   holds it..   */
d4620 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 65 64  .  if( !reserved
d4630 20 29 7b 0a 20 20 20 20 2f 2a 20 6c 6f 63 6b 20   ){.    /* lock 
d4640 74 68 65 20 52 45 53 45 52 56 45 44 20 62 79 74  the RESERVED byt
d4650 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72 63  e */.    int lrc
d4660 20 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b   = _AFPFSSetLock
d4670 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61  (context->filePa
d4680 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52  th, pFile, RESER
d4690 56 45 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b 20  VED_BYTE, 1,1); 
d46a0 20 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45   .    if( SQLITE
d46b0 5f 4f 4b 3d 3d 6c 72 63 20 29 7b 0a 20 20 20 20  _OK==lrc ){.    
d46c0 20 20 2f 2a 20 69 66 20 77 65 20 73 75 63 63 65    /* if we succe
d46d0 65 64 65 64 20 69 6e 20 74 61 6b 69 6e 67 20 74  eded in taking t
d46e0 68 65 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b  he reserved lock
d46f0 2c 20 75 6e 6c 6f 63 6b 20 69 74 20 74 6f 20 72  , unlock it to r
d4700 65 73 74 6f 72 65 0a 20 20 20 20 20 20 2a 2a 20  estore.      ** 
d4710 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61  the original sta
d4720 74 65 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63 20  te */.      lrc 
d4730 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28  = _AFPFSSetLock(
d4740 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 74  context->filePat
d4750 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56  h, pFile, RESERV
d4760 45 44 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a  ED_BYTE, 1, 0);.
d4770 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20      } else {.   
d4780 20 20 20 2f 2a 20 69 66 20 77 65 20 66 61 69 6c     /* if we fail
d4790 65 64 20 74 6f 20 67 65 74 20 74 68 65 20 6c 6f  ed to get the lo
d47a0 63 6b 20 74 68 65 6e 20 73 6f 6d 65 6f 6e 65 20  ck then someone 
d47b0 65 6c 73 65 20 6d 75 73 74 20 68 61 76 65 20 69  else must have i
d47c0 74 20 2a 2f 0a 20 20 20 20 20 20 72 65 73 65 72  t */.      reser
d47d0 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  ved = 1;.    }. 
d47e0 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45     if( IS_LOCK_E
d47f0 52 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20  RROR(lrc) ){.   
d4800 20 20 20 72 63 3d 6c 72 63 3b 0a 20 20 20 20 7d     rc=lrc;.    }
d4810 0a 20 20 7d 0a 20 20 0a 20 20 4f 53 54 52 41 43  .  }.  .  OSTRAC
d4820 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b  E4("TEST WR-LOCK
d4830 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46   %d %d %d\n", pF
d4840 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65  ile->h, rc, rese
d4850 72 76 65 64 29 3b 0a 20 20 0a 20 20 2a 70 52 65  rved);.  .  *pRe
d4860 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b  sOut = reserved;
d4870 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
d4880 0a 2f 2a 20 41 46 50 2d 73 74 79 6c 65 20 6c 6f  ./* AFP-style lo
d4890 63 6b 69 6e 67 20 66 6f 6c 6c 6f 77 69 6e 67 20  cking following 
d48a0 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20  the behavior of 
d48b0 75 6e 69 78 4c 6f 63 6b 2c 20 73 65 65 20 74 68  unixLock, see th
d48c0 65 20 75 6e 69 78 4c 6f 63 6b 20 0a 2a 2a 20 66  e unixLock .** f
d48d0 75 6e 63 74 69 6f 6e 20 63 6f 6d 6d 65 6e 74 73  unction comments
d48e0 20 66 6f 72 20 64 65 74 61 69 6c 73 20 6f 66 20   for details of 
d48f0 6c 6f 63 6b 20 6d 61 6e 61 67 65 6d 65 6e 74 2e  lock management.
d4900 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61   */.static int a
d4910 66 70 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  fpLock(sqlite3_f
d4920 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63  ile *id, int loc
d4930 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 72 63  ktype){.  int rc
d4940 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
d4950 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
d4960 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
d4970 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e  .  afpLockingCon
d4980 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20  text *context = 
d4990 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  (afpLockingConte
d49a0 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63  xt *) pFile->loc
d49b0 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a  kingContext;.  .
d49c0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
d49d0 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 4c  );.  OSTRACE5("L
d49e0 4f 43 4b 20 20 20 20 25 64 20 25 73 20 77 61 73  OCK    %d %s was
d49f0 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70   %s pid=%d\n", p
d4a00 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20  File->h,.       
d4a10 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c    locktypeName(l
d4a20 6f 63 6b 74 79 70 65 29 2c 20 6c 6f 63 6b 74 79  ocktype), lockty
d4a30 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f  peName(pFile->lo
d4a40 63 6b 74 79 70 65 29 2c 20 67 65 74 70 69 64 28  cktype), getpid(
d4a50 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  ));..  /* If the
d4a60 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20  re is already a 
d4a70 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70  lock of this typ
d4a80 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69  e or more restri
d4a90 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a  ctive on the.  *
d4aa0 2a 20 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20 6e  * unixFile, do n
d4ab0 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73  othing. Don't us
d4ac0 65 20 74 68 65 20 61 66 70 5f 65 6e 64 5f 6c 6f  e the afp_end_lo
d4ad0 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61  ck: exit path, a
d4ae0 73 0a 20 20 2a 2a 20 65 6e 74 65 72 4d 75 74 65  s.  ** enterMute
d4af0 78 28 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20  x() hasn't been 
d4b00 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f  called yet..  */
d4b10 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
d4b20 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65  cktype>=locktype
d4b30 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 33   ){.    OSTRACE3
d4b40 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20  ("LOCK    %d %s 
d4b50 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64  ok (already held
d4b60 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a  )\n", pFile->h,.
d4b70 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74             lockt
d4b80 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65  ypeName(locktype
d4b90 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  ));.    return S
d4ba0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20  QLITE_OK;.  }.. 
d4bb0 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68   /* Make sure th
d4bc0 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e  e locking sequen
d4bd0 63 65 20 69 73 20 63 6f 72 72 65 63 74 0a 20 20  ce is correct.  
d4be0 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46 69  */.  assert( pFi
d4bf0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f  le->locktype!=NO
d4c00 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70  _LOCK || locktyp
d4c10 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e==SHARED_LOCK )
d4c20 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b  ;.  assert( lock
d4c30 74 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f  type!=PENDING_LO
d4c40 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  CK );.  assert( 
d4c50 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53 45 52 56  locktype!=RESERV
d4c60 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65  ED_LOCK || pFile
d4c70 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  ->locktype==SHAR
d4c80 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20  ED_LOCK );.  .  
d4c90 2f 2a 20 54 68 69 73 20 6d 75 74 65 78 20 69 73  /* This mutex is
d4ca0 20 6e 65 65 64 65 64 20 62 65 63 61 75 73 65 20   needed because 
d4cb0 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 73 20  pFile->pLock is 
d4cc0 73 68 61 72 65 64 20 61 63 72 6f 73 73 20 74 68  shared across th
d4cd0 72 65 61 64 73 0a 20 20 2a 2f 0a 20 20 65 6e 74  reads.  */.  ent
d4ce0 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a  erMutex();..  /*
d4cf0 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 63   Make sure the c
d4d00 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 6f 77  urrent thread ow
d4d10 6e 73 20 74 68 65 20 70 46 69 6c 65 2e 0a 20 20  ns the pFile..  
d4d20 2a 2f 0a 20 20 72 63 20 3d 20 74 72 61 6e 73 66  */.  rc = transf
d4d30 65 72 4f 77 6e 65 72 73 68 69 70 28 70 46 69 6c  erOwnership(pFil
d4d40 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  e);.  if( rc!=SQ
d4d50 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 6c  LITE_OK ){.    l
d4d60 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20  eaveMutex();.   
d4d70 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
d4d80 20 20 20 20 0a 20 20 2f 2a 20 41 20 50 45 4e 44      .  /* A PEND
d4d90 49 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64  ING lock is need
d4da0 65 64 20 62 65 66 6f 72 65 20 61 63 71 75 69 72  ed before acquir
d4db0 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63  ing a SHARED loc
d4dc0 6b 20 61 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a  k and before.  *
d4dd0 2a 20 61 63 71 75 69 72 69 6e 67 20 61 6e 20 45  * acquiring an E
d4de0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20  XCLUSIVE lock.  
d4df0 46 6f 72 20 74 68 65 20 53 48 41 52 45 44 20 6c  For the SHARED l
d4e00 6f 63 6b 2c 20 74 68 65 20 50 45 4e 44 49 4e 47  ock, the PENDING
d4e10 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65   will.  ** be re
d4e20 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  leased..  */.  i
d4e30 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  f( locktype==SHA
d4e40 52 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20  RED_LOCK .      
d4e50 7c 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58  || (locktype==EX
d4e60 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20  CLUSIVE_LOCK && 
d4e70 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c  pFile->locktype<
d4e80 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20  PENDING_LOCK).  
d4e90 29 7b 0a 20 20 20 20 69 6e 74 20 66 61 69 6c 65  ){.    int faile
d4ea0 64 3b 0a 20 20 20 20 66 61 69 6c 65 64 20 3d 20  d;.    failed = 
d4eb0 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f  _AFPFSSetLock(co
d4ec0 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c  ntext->filePath,
d4ed0 20 70 46 69 6c 65 2c 20 50 45 4e 44 49 4e 47 5f   pFile, PENDING_
d4ee0 42 59 54 45 2c 20 31 2c 20 31 29 3b 0a 20 20 20  BYTE, 1, 1);.   
d4ef0 20 69 66 20 28 66 61 69 6c 65 64 29 20 7b 0a 20   if (failed) {. 
d4f00 20 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64       rc = failed
d4f10 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 66 70  ;.      goto afp
d4f20 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d  _end_lock;.    }
d4f30 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 49 66 20  .  }.  .  /* If 
d4f40 63 6f 6e 74 72 6f 6c 20 67 65 74 73 20 74 6f 20  control gets to 
d4f50 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e  this point, then
d4f60 20 61 63 74 75 61 6c 6c 79 20 67 6f 20 61 68 65   actually go ahe
d4f70 61 64 20 61 6e 64 20 6d 61 6b 65 0a 20 20 2a 2a  ad and make.  **
d4f80 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
d4f90 6d 20 63 61 6c 6c 73 20 66 6f 72 20 74 68 65 20  m calls for the 
d4fa0 73 70 65 63 69 66 69 65 64 20 6c 6f 63 6b 2e 0a  specified lock..
d4fb0 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74    */.  if( lockt
d4fc0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
d4fd0 20 29 7b 0a 20 20 20 20 69 6e 74 20 6c 6b 2c 20   ){.    int lk, 
d4fe0 6c 72 63 31 2c 20 6c 72 63 32 2c 20 6c 72 63 31  lrc1, lrc2, lrc1
d4ff0 45 72 72 6e 6f 3b 0a 20 20 20 20 0a 20 20 20 20  Errno;.    .    
d5000 2f 2a 20 4e 6f 77 20 67 65 74 20 74 68 65 20 72  /* Now get the r
d5010 65 61 64 2d 6c 6f 63 6b 20 53 48 41 52 45 44 5f  ead-lock SHARED_
d5020 4c 4f 43 4b 20 2a 2f 0a 20 20 20 20 2f 2a 20 6e  LOCK */.    /* n
d5030 6f 74 65 20 74 68 61 74 20 74 68 65 20 71 75 61  ote that the qua
d5040 6c 69 74 79 20 6f 66 20 74 68 65 20 72 61 6e 64  lity of the rand
d5050 6f 6d 6e 65 73 73 20 64 6f 65 73 6e 27 74 20 6d  omness doesn't m
d5060 61 74 74 65 72 20 74 68 61 74 20 6d 75 63 68 20  atter that much 
d5070 2a 2f 0a 20 20 20 20 6c 6b 20 3d 20 72 61 6e 64  */.    lk = rand
d5080 6f 6d 28 29 3b 20 0a 20 20 20 20 63 6f 6e 74 65  om(); .    conte
d5090 78 74 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79  xt->sharedLockBy
d50a0 74 65 20 3d 20 28 6c 6b 20 26 20 30 78 37 66 66  te = (lk & 0x7ff
d50b0 66 66 66 66 66 29 25 28 53 48 41 52 45 44 5f 53  fffff)%(SHARED_S
d50c0 49 5a 45 20 2d 20 31 29 3b 0a 20 20 20 20 6c 72  IZE - 1);.    lr
d50d0 63 31 20 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f  c1 = _AFPFSSetLo
d50e0 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65  ck(context->file
d50f0 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20  Path, pFile, .  
d5100 20 20 20 20 20 20 20 20 53 48 41 52 45 44 5f 46          SHARED_F
d5110 49 52 53 54 2b 63 6f 6e 74 65 78 74 2d 3e 73 68  IRST+context->sh
d5120 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31 2c  aredLockByte, 1,
d5130 20 31 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f   1);.    if( IS_
d5140 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 31 29  LOCK_ERROR(lrc1)
d5150 20 29 7b 0a 20 20 20 20 20 20 6c 72 63 31 45 72   ){.      lrc1Er
d5160 72 6e 6f 20 3d 20 70 46 69 6c 65 2d 3e 6c 61 73  rno = pFile->las
d5170 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20  tErrno;.    }.  
d5180 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 74 65    /* Drop the te
d5190 6d 70 6f 72 61 72 79 20 50 45 4e 44 49 4e 47 20  mporary PENDING 
d51a0 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 6c 72 63 32  lock */.    lrc2
d51b0 20 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b   = _AFPFSSetLock
d51c0 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61  (context->filePa
d51d0 74 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e 44 49  th, pFile, PENDI
d51e0 4e 47 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a  NG_BYTE, 1, 0);.
d51f0 20 20 20 20 0a 20 20 20 20 69 66 28 20 49 53 5f      .    if( IS_
d5200 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 31 29  LOCK_ERROR(lrc1)
d5210 20 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65   ) {.      pFile
d5220 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c 72  ->lastErrno = lr
d5230 63 31 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72  c1Errno;.      r
d5240 63 20 3d 20 6c 72 63 31 3b 0a 20 20 20 20 20 20  c = lrc1;.      
d5250 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63  goto afp_end_loc
d5260 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 66  k;.    } else if
d5270 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28  ( IS_LOCK_ERROR(
d5280 6c 72 63 32 29 20 29 7b 0a 20 20 20 20 20 20 72  lrc2) ){.      r
d5290 63 20 3d 20 6c 72 63 32 3b 0a 20 20 20 20 20 20  c = lrc2;.      
d52a0 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63  goto afp_end_loc
d52b0 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 66  k;.    } else if
d52c0 28 20 6c 72 63 31 20 21 3d 20 53 51 4c 49 54 45  ( lrc1 != SQLITE
d52d0 5f 4f 4b 20 29 20 7b 0a 20 20 20 20 20 20 72 63  _OK ) {.      rc
d52e0 20 3d 20 6c 72 63 31 3b 0a 20 20 20 20 7d 20 65   = lrc1;.    } e
d52f0 6c 73 65 20 7b 0a 20 20 20 20 20 20 70 46 69 6c  lse {.      pFil
d5300 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48  e->locktype = SH
d5310 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d  ARED_LOCK;.    }
d5320 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
d5330 20 54 68 65 20 72 65 71 75 65 73 74 20 77 61 73   The request was
d5340 20 66 6f 72 20 61 20 52 45 53 45 52 56 45 44 20   for a RESERVED 
d5350 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  or EXCLUSIVE loc
d5360 6b 2e 20 20 49 74 20 69 73 0a 20 20 20 20 2a 2a  k.  It is.    **
d5370 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68   assumed that th
d5380 65 72 65 20 69 73 20 61 20 53 48 41 52 45 44 20  ere is a SHARED 
d5390 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20  or greater lock 
d53a0 6f 6e 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20  on the file.    
d53b0 2a 2a 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20  ** already..    
d53c0 2a 2f 0a 20 20 20 20 69 6e 74 20 66 61 69 6c 65  */.    int faile
d53d0 64 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72  d = 0;.    asser
d53e0 74 28 20 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63  t( 0!=pFile->loc
d53f0 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 69 66 20  ktype );.    if 
d5400 28 6c 6f 63 6b 74 79 70 65 20 3e 3d 20 52 45 53  (locktype >= RES
d5410 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 70 46  ERVED_LOCK && pF
d5420 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c 20  ile->locktype < 
d5430 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 20 7b  RESERVED_LOCK) {
d5440 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 63 71 75  .        /* Acqu
d5450 69 72 65 20 61 20 52 45 53 45 52 56 45 44 20 6c  ire a RESERVED l
d5460 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 66  ock */.        f
d5470 61 69 6c 65 64 20 3d 20 5f 41 46 50 46 53 53 65  ailed = _AFPFSSe
d5480 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66  tLock(context->f
d5490 69 6c 65 50 61 74 68 2c 20 70 46 69 6c 65 2c 20  ilePath, pFile, 
d54a0 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31  RESERVED_BYTE, 1
d54b0 2c 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ,1);.    }.    i
d54c0 66 20 28 21 66 61 69 6c 65 64 20 26 26 20 6c 6f  f (!failed && lo
d54d0 63 6b 74 79 70 65 20 3d 3d 20 45 58 43 4c 55 53  cktype == EXCLUS
d54e0 49 56 45 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20  IVE_LOCK) {.    
d54f0 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 6e 20    /* Acquire an 
d5500 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 2a  EXCLUSIVE lock *
d5510 2f 0a 20 20 20 20 20 20 20 20 0a 20 20 20 20 20  /.        .     
d5520 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 73   /* Remove the s
d5530 68 61 72 65 64 20 6c 6f 63 6b 20 62 65 66 6f 72  hared lock befor
d5540 65 20 74 72 79 69 6e 67 20 74 68 65 20 72 61 6e  e trying the ran
d5550 67 65 2e 20 20 77 65 27 6c 6c 20 6e 65 65 64 20  ge.  we'll need 
d5560 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 72 65 65  to .      ** ree
d5570 73 74 61 62 6c 69 73 68 20 74 68 65 20 73 68 61  stablish the sha
d5580 72 65 64 20 6c 6f 63 6b 20 69 66 20 77 65 20 63  red lock if we c
d5590 61 6e 27 74 20 67 65 74 20 74 68 65 20 20 61 66  an't get the  af
d55a0 70 55 6e 6c 6f 63 6b 0a 20 20 20 20 20 20 2a 2f  pUnlock.      */
d55b0 0a 20 20 20 20 20 20 69 66 20 28 21 28 66 61 69  .      if (!(fai
d55c0 6c 65 64 20 3d 20 5f 41 46 50 46 53 53 65 74 4c  led = _AFPFSSetL
d55d0 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c  ock(context->fil
d55e0 65 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48  ePath, pFile, SH
d55f0 41 52 45 44 5f 46 49 52 53 54 20 2b 0a 20 20 20  ARED_FIRST +.   
d5600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d5610 20 20 20 20 20 20 63 6f 6e 74 65 78 74 2d 3e 73        context->s
d5620 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31  haredLockByte, 1
d5630 2c 20 30 29 29 29 20 7b 0a 20 20 20 20 20 20 20  , 0))) {.       
d5640 20 2f 2a 20 6e 6f 77 20 61 74 74 65 6d 6d 70 74   /* now attemmpt
d5650 20 74 6f 20 67 65 74 20 74 68 65 20 65 78 63 6c   to get the excl
d5660 75 73 69 76 65 20 6c 6f 63 6b 20 72 61 6e 67 65  usive lock range
d5670 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 61 69 6c   */.        fail
d5680 65 64 20 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f  ed = _AFPFSSetLo
d5690 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65  ck(context->file
d56a0 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 41  Path, pFile, SHA
d56b0 52 45 44 5f 46 49 52 53 54 2c 20 0a 20 20 20 20  RED_FIRST, .    
d56c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d56d0 20 20 20 20 20 20 20 20 20 20 20 53 48 41 52 45             SHARE
d56e0 44 5f 53 49 5a 45 2c 20 31 29 3b 0a 20 20 20 20  D_SIZE, 1);.    
d56f0 20 20 20 20 69 66 20 28 66 61 69 6c 65 64 20 26      if (failed &
d5700 26 20 28 66 61 69 6c 65 64 20 3d 20 5f 41 46 50  & (failed = _AFP
d5710 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78  FSSetLock(contex
d5720 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70 46 69  t->filePath, pFi
d5730 6c 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  le, .           
d5740 20 20 20 20 20 20 20 20 20 20 20 20 53 48 41 52              SHAR
d5750 45 44 5f 46 49 52 53 54 20 2b 20 63 6f 6e 74 65  ED_FIRST + conte
d5760 78 74 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79  xt->sharedLockBy
d5770 74 65 2c 20 31 2c 20 31 29 29 29 20 7b 0a 20 20  te, 1, 1))) {.  
d5780 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 61 69          rc = fai
d5790 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  led;.        }. 
d57a0 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20       } else {.  
d57b0 20 20 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65        rc = faile
d57c0 64 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  d; .      }.    
d57d0 7d 0a 20 20 20 20 69 66 28 20 66 61 69 6c 65 64  }.    if( failed
d57e0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66   ){.      rc = f
d57f0 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d  ailed;.    }.  }
d5800 0a 20 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  .  .  if( rc==SQ
d5810 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70  LITE_OK ){.    p
d5820 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
d5830 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 65 6c   locktype;.  }el
d5840 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d  se if( locktype=
d5850 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20  =EXCLUSIVE_LOCK 
d5860 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f  ){.    pFile->lo
d5870 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47  cktype = PENDING
d5880 5f 4c 4f 43 4b 3b 0a 20 20 7d 0a 20 20 0a 61 66  _LOCK;.  }.  .af
d5890 70 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 6c 65  p_end_lock:.  le
d58a0 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53  aveMutex();.  OS
d58b0 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20  TRACE4("LOCK    
d58c0 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69  %d %s %s\n", pFi
d58d0 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e  le->h, locktypeN
d58e0 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a  ame(locktype), .
d58f0 20 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c           rc==SQL
d5900 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20  ITE_OK ? "ok" : 
d5910 22 66 61 69 6c 65 64 22 29 3b 0a 20 20 72 65 74  "failed");.  ret
d5920 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
d5930 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69   Lower the locki
d5940 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65  ng level on file
d5950 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c   descriptor pFil
d5960 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20  e to locktype.  
d5970 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74  locktype.** must
d5980 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f   be either NO_LO
d5990 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43  CK or SHARED_LOC
d59a0 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  K..**.** If the 
d59b0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66  locking level of
d59c0 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
d59d0 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20  ptor is already 
d59e0 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74  at or below.** t
d59f0 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63  he requested loc
d5a00 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73  king level, this
d5a10 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
d5a20 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  -op..*/.static i
d5a30 6e 74 20 61 66 70 55 6e 6c 6f 63 6b 28 73 71 6c  nt afpUnlock(sql
d5a40 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
d5a50 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20  nt locktype) {. 
d5a60 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
d5a70 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20  _OK;.  unixFile 
d5a80 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
d5a90 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70 4c 6f 63  le*)id;.  afpLoc
d5aa0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e  kingContext *con
d5ab0 74 65 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b 69  text = (afpLocki
d5ac0 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69  ngContext *) pFi
d5ad0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
d5ae0 78 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  xt;..  assert( p
d5af0 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43  File );.  OSTRAC
d5b00 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25  E5("UNLOCK  %d %
d5b10 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c  d was %d pid=%d\
d5b20 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f  n", pFile->h, lo
d5b30 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20  cktype,.        
d5b40 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d5b50 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20  , getpid());..  
d5b60 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65  assert( locktype
d5b70 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b  <=SHARED_LOCK );
d5b80 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
d5b90 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79 70 65  cktype<=locktype
d5ba0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
d5bb0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
d5bc0 69 66 28 20 43 48 45 43 4b 5f 54 48 52 45 41 44  if( CHECK_THREAD
d5bd0 49 44 28 70 46 69 6c 65 29 20 29 7b 0a 20 20 20  ID(pFile) ){.   
d5be0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d   return SQLITE_M
d5bf0 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 65 6e 74  ISUSE;.  }.  ent
d5c00 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 6e 74  erMutex();.  int
d5c10 20 66 61 69 6c 65 64 20 3d 20 53 51 4c 49 54 45   failed = SQLITE
d5c20 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 46 69 6c 65  _OK;.  if( pFile
d5c30 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45  ->locktype>SHARE
d5c40 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 69 66  D_LOCK ){.    if
d5c50 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  ( locktype==SHAR
d5c60 45 44 5f 4c 4f 43 4b 20 29 7b 0a 0a 20 20 20 20  ED_LOCK ){..    
d5c70 20 20 2f 2a 20 75 6e 6c 6f 63 6b 20 74 68 65 20    /* unlock the 
d5c80 65 78 63 6c 75 73 69 76 65 20 72 61 6e 67 65 20  exclusive range 
d5c90 2d 20 74 68 65 6e 20 72 65 2d 65 73 74 61 62 6c  - then re-establ
d5ca0 69 73 68 20 74 68 65 20 73 68 61 72 65 64 20 6c  ish the shared l
d5cb0 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 69 66 20  ock */.      if 
d5cc0 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65  (pFile->locktype
d5cd0 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ==EXCLUSIVE_LOCK
d5ce0 29 20 7b 0a 20 20 20 20 20 20 20 20 66 61 69 6c  ) {.        fail
d5cf0 65 64 20 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f  ed = _AFPFSSetLo
d5d00 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65  ck(context->file
d5d10 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 41  Path, pFile, SHA
d5d20 52 45 44 5f 46 49 52 53 54 2c 20 0a 20 20 20 20  RED_FIRST, .    
d5d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d5d40 20 20 20 20 20 20 20 20 20 20 20 20 20 53 48 41               SHA
d5d50 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20  RED_SIZE, 0);.  
d5d60 20 20 20 20 20 20 69 66 20 28 21 66 61 69 6c 65        if (!faile
d5d70 64 29 20 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  d) {.          /
d5d80 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 72  * successfully r
d5d90 65 6d 6f 76 65 64 20 74 68 65 20 65 78 63 6c 75  emoved the exclu
d5da0 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20  sive lock */.   
d5db0 20 20 20 20 20 20 20 69 66 20 28 28 66 61 69 6c         if ((fail
d5dc0 65 64 20 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f  ed = _AFPFSSetLo
d5dd0 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65  ck(context->file
d5de0 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 41  Path, pFile, SHA
d5df0 52 45 44 5f 46 49 52 53 54 2b 0a 20 20 20 20 20  RED_FIRST+.     
d5e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d5e10 20 20 20 20 20 20 20 63 6f 6e 74 65 78 74 2d 3e         context->
d5e20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20  sharedLockByte, 
d5e30 31 2c 20 31 29 29 29 20 7b 0a 20 20 20 20 20 20  1, 1))) {.      
d5e40 20 20 20 20 20 20 2f 2a 20 66 61 69 6c 65 64 20        /* failed 
d5e50 74 6f 20 72 65 2d 65 73 74 61 62 6c 69 73 68 20  to re-establish 
d5e60 6f 75 72 20 73 68 61 72 65 64 20 6c 6f 63 6b 20  our shared lock 
d5e70 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  */.            r
d5e80 63 20 3d 20 66 61 69 6c 65 64 3b 0a 20 20 20 20  c = failed;.    
d5e90 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
d5ea0 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20  } else {.       
d5eb0 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b 0a     rc = failed;.
d5ec0 20 20 20 20 20 20 20 20 7d 20 0a 20 20 20 20 20          } .     
d5ed0 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20   }.    }.    if 
d5ee0 28 72 63 20 3d 3d 20 53 51 4c 49 54 45 5f 4f 4b  (rc == SQLITE_OK
d5ef0 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74   && pFile->lockt
d5f00 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43  ype>=PENDING_LOC
d5f10 4b 29 20 7b 0a 20 20 20 20 20 20 69 66 20 28 28  K) {.      if ((
d5f20 66 61 69 6c 65 64 20 3d 20 5f 41 46 50 46 53 53  failed = _AFPFSS
d5f30 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e  etLock(context->
d5f40 66 69 6c 65 50 61 74 68 2c 20 70 46 69 6c 65 2c  filePath, pFile,
d5f50 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
d5f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d5f70 20 20 20 20 50 45 4e 44 49 4e 47 5f 42 59 54 45      PENDING_BYTE
d5f80 2c 20 31 2c 20 30 29 29 29 7b 0a 20 20 20 20 20  , 1, 0))){.     
d5f90 20 20 20 2f 2a 20 66 61 69 6c 65 64 20 74 6f 20     /* failed to 
d5fa0 72 65 6c 65 61 73 65 20 74 68 65 20 70 65 6e 64  release the pend
d5fb0 69 6e 67 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20  ing lock */.    
d5fc0 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b      rc = failed;
d5fd0 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20   .      }.    } 
d5fe0 0a 20 20 20 20 69 66 20 28 72 63 20 3d 3d 20 53  .    if (rc == S
d5ff0 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 46 69 6c  QLITE_OK && pFil
d6000 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52 45 53  e->locktype>=RES
d6010 45 52 56 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20  ERVED_LOCK) {.  
d6020 20 20 20 20 69 66 20 28 28 66 61 69 6c 65 64 20      if ((failed 
d6030 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28  = _AFPFSSetLock(
d6040 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 74  context->filePat
d6050 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20 20 20 20  h, pFile, .     
d6060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d6070 20 20 20 20 20 20 20 20 20 20 20 20 20 52 45 53               RES
d6080 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 20 30  ERVED_BYTE, 1, 0
d6090 29 29 29 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a  ))) {.        /*
d60a0 20 66 61 69 6c 65 64 20 74 6f 20 72 65 6c 65 61   failed to relea
d60b0 73 65 20 74 68 65 20 72 65 73 65 72 76 65 64 20  se the reserved 
d60c0 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20  lock */.        
d60d0 72 63 20 3d 20 66 61 69 6c 65 64 3b 20 20 0a 20  rc = failed;  . 
d60e0 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 0a 20 20       }.    } .  
d60f0 7d 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65  }.  if( locktype
d6100 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  ==NO_LOCK ){.   
d6110 20 69 6e 74 20 66 61 69 6c 65 64 20 3d 20 5f 41   int failed = _A
d6120 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74  FPFSSetLock(cont
d6130 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70  ext->filePath, p
d6140 46 69 6c 65 2c 20 0a 20 20 20 20 20 20 20 20 20  File, .         
d6150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d6160 20 20 20 20 20 20 53 48 41 52 45 44 5f 46 49 52        SHARED_FIR
d6170 53 54 20 2b 20 63 6f 6e 74 65 78 74 2d 3e 73 68  ST + context->sh
d6180 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31 2c  aredLockByte, 1,
d6190 20 30 29 3b 0a 20 20 20 20 69 66 20 28 66 61 69   0);.    if (fai
d61a0 6c 65 64 29 20 7b 0a 20 20 20 20 20 20 72 63 20  led) {.      rc 
d61b0 3d 20 66 61 69 6c 65 64 3b 20 20 0a 20 20 20 20  = failed;  .    
d61c0 7d 0a 20 20 7d 0a 20 20 69 66 20 28 72 63 20 3d  }.  }.  if (rc =
d61d0 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20  = SQLITE_OK).   
d61e0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d61f0 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 6c   = locktype;.  l
d6200 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72  eaveMutex();.  r
d6210 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
d6220 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 20  ** Close a file 
d6230 26 20 63 6c 65 61 6e 75 70 20 41 46 50 20 73 70  & cleanup AFP sp
d6240 65 63 69 66 69 63 20 6c 6f 63 6b 69 6e 67 20 63  ecific locking c
d6250 6f 6e 74 65 78 74 20 0a 2a 2f 0a 73 74 61 74 69  ontext .*/.stati
d6260 63 20 69 6e 74 20 61 66 70 43 6c 6f 73 65 28 73  c int afpClose(s
d6270 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
d6280 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20   {.  if( id ){. 
d6290 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69     unixFile *pFi
d62a0 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
d62b0 69 64 3b 0a 20 20 20 20 61 66 70 55 6e 6c 6f 63  id;.    afpUnloc
d62c0 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a  k(id, NO_LOCK);.
d62d0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
d62e0 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43  (pFile->lockingC
d62f0 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20 20 72  ontext);.  }.  r
d6300 65 74 75 72 6e 20 63 6c 6f 73 65 55 6e 69 78 46  eturn closeUnixF
d6310 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a 0a 23 70 72  ile(id);.}...#pr
d6320 61 67 6d 61 20 6d 61 72 6b 20 66 6c 6f 63 6b 28  agma mark flock(
d6330 29 20 73 74 79 6c 65 20 6c 6f 63 6b 69 6e 67 0a  ) style locking.
d6340 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6c 6f 63 6b  ./*.** The flock
d6350 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 69  LockingContext i
d6360 73 20 6e 6f 74 20 75 73 65 64 0a 2a 2f 0a 74 79  s not used.*/.ty
d6370 70 65 64 65 66 20 76 6f 69 64 20 66 6c 6f 63 6b  pedef void flock
d6380 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a  LockingContext;.
d6390 0a 2f 2a 20 66 6c 6f 63 6b 2d 73 74 79 6c 65 20  ./* flock-style 
d63a0 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 63 68  reserved lock ch
d63b0 65 63 6b 69 6e 67 20 66 6f 6c 6c 6f 77 69 6e 67  ecking following
d63c0 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66   the behavior of
d63d0 20 0a 20 2a 2a 20 75 6e 69 78 43 68 65 63 6b 52   . ** unixCheckR
d63e0 65 73 65 72 76 65 64 4c 6f 63 6b 2c 20 73 65 65  eservedLock, see
d63f0 20 74 68 65 20 75 6e 69 78 43 68 65 63 6b 52 65   the unixCheckRe
d6400 73 65 72 76 65 64 4c 6f 63 6b 20 66 75 6e 63 74  servedLock funct
d6410 69 6f 6e 20 63 6f 6d 6d 65 6e 74 73 20 2a 2f 0a  ion comments */.
d6420 73 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b  static int flock
d6430 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
d6440 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
d6450 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74  id, int *pResOut
d6460 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
d6470 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72  LITE_OK;.  int r
d6480 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75  eserved = 0;.  u
d6490 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
d64a0 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
d64b0 20 20 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45    .  SimulateIOE
d64c0 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c  rror( return SQL
d64d0 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52  ITE_IOERR_CHECKR
d64e0 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a  ESERVEDLOCK; );.
d64f0 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69    .  assert( pFi
d6500 6c 65 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 43 68  le );.  .  /* Ch
d6510 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 20  eck if a thread 
d6520 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20  in this process 
d6530 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63  holds such a loc
d6540 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65  k */.  if( pFile
d6550 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45  ->locktype>SHARE
d6560 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65  D_LOCK ){.    re
d6570 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a  served = 1;.  }.
d6580 20 20 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73    .  /* Otherwis
d6590 65 20 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74  e see if some ot
d65a0 68 65 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64  her process hold
d65b0 73 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21  s it. */.  if( !
d65c0 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20  reserved ){.    
d65d0 2f 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 67 65  /* attempt to ge
d65e0 74 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20  t the lock */.  
d65f0 20 20 69 6e 74 20 6c 72 63 20 3d 20 66 6c 6f 63    int lrc = floc
d6600 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b  k(pFile->h, LOCK
d6610 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 3b 0a  _EX | LOCK_NB);.
d6620 20 20 20 20 69 66 28 20 21 6c 72 63 20 29 7b 0a      if( !lrc ){.
d6630 20 20 20 20 20 20 2f 2a 20 67 6f 74 20 74 68 65        /* got the
d6640 20 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74   lock, unlock it
d6650 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d 20   */.      lrc = 
d6660 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20  flock(pFile->h, 
d6670 4c 4f 43 4b 5f 55 4e 29 3b 0a 20 20 20 20 20 20  LOCK_UN);.      
d6680 69 66 20 28 20 6c 72 63 20 29 20 7b 0a 20 20 20  if ( lrc ) {.   
d6690 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20       int tErrno 
d66a0 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20  = errno;.       
d66b0 20 2f 2a 20 75 6e 6c 6f 63 6b 20 66 61 69 6c 65   /* unlock faile
d66c0 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20  d with an error 
d66d0 2a 2f 0a 20 20 20 20 20 20 20 20 6c 72 63 20 3d  */.        lrc =
d66e0 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d   sqliteErrorFrom
d66f0 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e  PosixError(tErrn
d6700 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  o, SQLITE_IOERR_
d6710 55 4e 4c 4f 43 4b 29 3b 20 0a 20 20 20 20 20 20  UNLOCK); .      
d6720 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
d6730 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 20  ROR(lrc) ){.    
d6740 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
d6750 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b  tErrno = tErrno;
d6760 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
d6770 6c 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  lrc;.        }. 
d6780 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73       }.    } els
d6790 65 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 45  e {.      int tE
d67a0 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
d67b0 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31      reserved = 1
d67c0 3b 0a 20 20 20 20 20 20 2f 2a 20 73 6f 6d 65 6f  ;.      /* someo
d67d0 6e 65 20 65 6c 73 65 20 6d 69 67 68 74 20 68 61  ne else might ha
d67e0 76 65 20 69 74 20 72 65 73 65 72 76 65 64 20 2a  ve it reserved *
d67f0 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d 20 73 71  /.      lrc = sq
d6800 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
d6810 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20  ixError(tErrno, 
d6820 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43  SQLITE_IOERR_LOC
d6830 4b 29 3b 20 0a 20 20 20 20 20 20 69 66 28 20 49  K); .      if( I
d6840 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63  S_LOCK_ERROR(lrc
d6850 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69  ) ){.        pFi
d6860 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
d6870 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20  tErrno;.        
d6880 72 63 20 3d 20 6c 72 63 3b 0a 20 20 20 20 20 20  rc = lrc;.      
d6890 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 4f 53  }.    }.  }.  OS
d68a0 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52 2d  TRACE4("TEST WR-
d68b0 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22  LOCK %d %d %d\n"
d68c0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20  , pFile->h, rc, 
d68d0 72 65 73 65 72 76 65 64 29 3b 0a 0a 20 20 2a 70  reserved);..  *p
d68e0 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65  ResOut = reserve
d68f0 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  d;.  return rc;.
d6900 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c  }..static int fl
d6910 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  ockLock(sqlite3_
d6920 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f  file *id, int lo
d6930 63 6b 74 79 70 65 29 20 7b 0a 20 20 69 6e 74 20  cktype) {.  int 
d6940 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
d6950 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
d6960 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
d6970 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46  d;..  assert( pF
d6980 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 69 66 20  ile );..  /* if 
d6990 77 65 20 61 6c 72 65 61 64 79 20 68 61 76 65 20  we already have 
d69a0 61 20 6c 6f 63 6b 2c 20 69 74 20 69 73 20 65 78  a lock, it is ex
d69b0 63 6c 75 73 69 76 65 2e 20 20 0a 20 20 2a 2a 20  clusive.  .  ** 
d69c0 4a 75 73 74 20 61 64 6a 75 73 74 20 6c 65 76 65  Just adjust leve
d69d0 6c 20 61 6e 64 20 70 75 6e 74 20 6f 6e 20 6f 75  l and punt on ou
d69e0 74 74 61 20 68 65 72 65 2e 20 2a 2f 0a 20 20 69  tta here. */.  i
d69f0 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f (pFile->lockty
d6a00 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 29 20 7b 0a  pe > NO_LOCK) {.
d6a10 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
d6a20 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a  ype = locktype;.
d6a30 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
d6a40 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f  E_OK;.  }.  .  /
d6a50 2a 20 67 72 61 62 20 61 6e 20 65 78 63 6c 75 73  * grab an exclus
d6a60 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 0a 20  ive lock */.  . 
d6a70 20 69 66 20 28 66 6c 6f 63 6b 28 70 46 69 6c 65   if (flock(pFile
d6a80 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 58 20 7c 20 4c  ->h, LOCK_EX | L
d6a90 4f 43 4b 5f 4e 42 29 29 20 7b 0a 20 20 20 20 69  OCK_NB)) {.    i
d6aa0 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  nt tErrno = errn
d6ab0 6f 3b 0a 20 20 20 20 2f 2a 20 64 69 64 6e 27 74  o;.    /* didn't
d6ac0 20 67 65 74 2c 20 6d 75 73 74 20 62 65 20 62 75   get, must be bu
d6ad0 73 79 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73  sy */.    rc = s
d6ae0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
d6af0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
d6b00 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f   SQLITE_IOERR_LO
d6b10 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f  CK);.    if( IS_
d6b20 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29  LOCK_ERROR(rc) )
d6b30 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  {.      pFile->l
d6b40 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e  astErrno = tErrn
d6b50 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c 73  o;.    }.  } els
d6b60 65 20 7b 0a 20 20 20 20 2f 2a 20 67 6f 74 20 69  e {.    /* got i
d6b70 74 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 20  t, set the type 
d6b80 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f  and return ok */
d6b90 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  .    pFile->lock
d6ba0 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b  type = locktype;
d6bb0 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28  .  }.  OSTRACE4(
d6bc0 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 25  "LOCK    %d %s %
d6bd0 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  s\n", pFile->h, 
d6be0 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63  locktypeName(loc
d6bf0 6b 74 79 70 65 29 2c 20 0a 20 20 20 20 20 20 20  ktype), .       
d6c00 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f      rc==SQLITE_O
d6c10 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c  K ? "ok" : "fail
d6c20 65 64 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ed");.  return r
d6c30 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74  c;.}..static int
d6c40 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c   flockUnlock(sql
d6c50 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
d6c60 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20  nt locktype) {. 
d6c70 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
d6c80 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
d6c90 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70  ;.  .  assert( p
d6ca0 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43  File );.  OSTRAC
d6cb0 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25  E5("UNLOCK  %d %
d6cc0 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c  d was %d pid=%d\
d6cd0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f  n", pFile->h, lo
d6ce0 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20  cktype,.        
d6cf0 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
d6d00 70 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20  pe, getpid());. 
d6d10 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
d6d20 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e<=SHARED_LOCK )
d6d30 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20  ;.  .  /* no-op 
d6d40 69 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20  if possible */. 
d6d50 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
d6d60 74 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29  type==locktype )
d6d70 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
d6d80 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20  ITE_OK;.  }.  . 
d6d90 20 2f 2a 20 73 68 61 72 65 64 20 63 61 6e 20 6a   /* shared can j
d6da0 75 73 74 20 62 65 20 73 65 74 20 62 65 63 61 75  ust be set becau
d6db0 73 65 20 77 65 20 61 6c 77 61 79 73 20 68 61 76  se we always hav
d6dc0 65 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 2a  e an exclusive *
d6dd0 2f 0a 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65  /.  if (locktype
d6de0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b  ==SHARED_LOCK) {
d6df0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  .    pFile->lock
d6e00 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b  type = locktype;
d6e10 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
d6e20 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20  TE_OK;.  }.  .  
d6e30 2f 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 2c 20 75  /* no, really, u
d6e40 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 6e 74 20  nlock. */.  int 
d6e50 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65  rc = flock(pFile
d6e60 2d 3e 68 2c 20 4c 4f 43 4b 5f 55 4e 29 3b 0a 20  ->h, LOCK_UN);. 
d6e70 20 69 66 20 28 72 63 29 20 7b 0a 20 20 20 20 69   if (rc) {.    i
d6e80 6e 74 20 72 2c 20 74 45 72 72 6e 6f 20 3d 20 65  nt r, tErrno = e
d6e90 72 72 6e 6f 3b 0a 20 20 20 20 72 20 3d 20 73 71  rrno;.    r = sq
d6ea0 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
d6eb0 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20  ixError(tErrno, 
d6ec0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c  SQLITE_IOERR_UNL
d6ed0 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53  OCK);.    if( IS
d6ee0 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 29 20 29  _LOCK_ERROR(r) )
d6ef0 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  {.      pFile->l
d6f00 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e  astErrno = tErrn
d6f10 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  o;.    }.    ret
d6f20 75 72 6e 20 72 3b 0a 20 20 7d 20 65 6c 73 65 20  urn r;.  } else 
d6f30 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  {.    pFile->loc
d6f40 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b  ktype = NO_LOCK;
d6f50 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
d6f60 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  TE_OK;.  }.}../*
d6f70 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65  .** Close a file
d6f80 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
d6f90 66 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74  flockClose(sqlit
d6fa0 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20  e3_file *id) {. 
d6fb0 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20 20 66   if( id ){.    f
d6fc0 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e  lockUnlock(id, N
d6fd0 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 7d 0a 20 20 72  O_LOCK);.  }.  r
d6fe0 65 74 75 72 6e 20 63 6c 6f 73 65 55 6e 69 78 46  eturn closeUnixF
d6ff0 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a 23 70 72 61  ile(id);.}..#pra
d7000 67 6d 61 20 6d 61 72 6b 20 4f 6c 64 2d 53 63 68  gma mark Old-Sch
d7010 6f 6f 6c 20 2e 6c 6f 63 6b 20 66 69 6c 65 20 62  ool .lock file b
d7020 61 73 65 64 20 6c 6f 63 6b 69 6e 67 0a 0a 2f 2a  ased locking../*
d7030 20 44 6f 74 6c 6f 63 6b 2d 73 74 79 6c 65 20 72   Dotlock-style r
d7040 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 63 68 65  eserved lock che
d7050 63 6b 69 6e 67 20 66 6f 6c 6c 6f 77 69 6e 67 20  cking following 
d7060 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20  the behavior of 
d7070 0a 2a 2a 20 75 6e 69 78 43 68 65 63 6b 52 65 73  .** unixCheckRes
d7080 65 72 76 65 64 4c 6f 63 6b 2c 20 73 65 65 20 74  ervedLock, see t
d7090 68 65 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65  he unixCheckRese
d70a0 72 76 65 64 4c 6f 63 6b 20 66 75 6e 63 74 69 6f  rvedLock functio
d70b0 6e 20 63 6f 6d 6d 65 6e 74 73 20 2a 2f 0a 73 74  n comments */.st
d70c0 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b  atic int dotlock
d70d0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
d70e0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
d70f0 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74  id, int *pResOut
d7100 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ) {.  int rc = S
d7110 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20  QLITE_OK;.  int 
d7120 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20  reserved = 0;.  
d7130 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
d7140 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
d7150 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ..  SimulateIOEr
d7160 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49  ror( return SQLI
d7170 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45  TE_IOERR_CHECKRE
d7180 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20  SERVEDLOCK; );. 
d7190 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c   .  assert( pFil
d71a0 65 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  e );..  /* Check
d71b0 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20   if a thread in 
d71c0 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c  this process hol
d71d0 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a  ds such a lock *
d71e0 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  /.  if( pFile->l
d71f0 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c  ocktype>SHARED_L
d7200 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72  OCK ){.    reser
d7210 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a  ved = 1;.  }.  .
d7220 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73    /* Otherwise s
d7230 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72  ee if some other
d7240 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69   process holds i
d7250 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73  t. */.  if( !res
d7260 65 72 76 65 64 20 29 7b 0a 20 20 20 20 63 68 61  erved ){.    cha
d7270 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28  r *zLockFile = (
d7280 63 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f  char *)pFile->lo
d7290 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20  ckingContext;.  
d72a0 20 20 73 74 72 75 63 74 20 73 74 61 74 20 73 74    struct stat st
d72b0 61 74 42 75 66 3b 0a 20 20 20 20 0a 20 20 20 20  atBuf;.    .    
d72c0 69 66 28 20 6c 73 74 61 74 28 7a 4c 6f 63 6b 46  if( lstat(zLockF
d72d0 69 6c 65 2c 20 26 73 74 61 74 42 75 66 29 3d 3d  ile, &statBuf)==
d72e0 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 66 69  0 ){.      /* fi
d72f0 6c 65 20 65 78 69 73 74 73 2c 20 73 6f 6d 65 6f  le exists, someo
d7300 6e 65 20 65 6c 73 65 20 68 61 73 20 74 68 65 20  ne else has the 
d7310 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 72 65  lock */.      re
d7320 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 20 20  served = 1;.    
d7330 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20  }else{.      /* 
d7340 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 78  file does not ex
d7350 69 73 74 2c 20 77 65 20 63 6f 75 6c 64 20 68 61  ist, we could ha
d7360 76 65 20 69 74 20 69 66 20 77 65 20 77 61 6e 74  ve it if we want
d7370 20 69 74 20 2a 2f 0a 09 09 09 69 6e 74 20 74 45   it */....int tE
d7380 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
d7390 20 20 20 20 69 66 28 20 45 4e 4f 45 4e 54 20 21      if( ENOENT !
d73a0 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 20 20 20  = tErrno ){.    
d73b0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45      rc = sqliteE
d73c0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72  rrorFromPosixErr
d73d0 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54  or(tErrno, SQLIT
d73e0 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53  E_IOERR_CHECKRES
d73f0 45 52 56 45 44 4c 4f 43 4b 29 3b 0a 20 20 20 20  ERVEDLOCK);.    
d7400 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
d7410 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20  rrno = tErrno;. 
d7420 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
d7430 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 45 53  .  OSTRACE4("TES
d7440 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20  T WR-LOCK %d %d 
d7450 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
d7460 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a   rc, reserved);.
d7470 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65  .  *pResOut = re
d7480 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e  served;.  return
d7490 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69   rc;.}..static i
d74a0 6e 74 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 28 73  nt dotlockLock(s
d74b0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
d74c0 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b   int locktype) {
d74d0 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
d74e0 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
d74f0 69 64 3b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20  id;.  int fd;.  
d7500 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20  char *zLockFile 
d7510 3d 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d  = (char *)pFile-
d7520 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b  >lockingContext;
d7530 0a 20 20 69 6e 74 20 72 63 3d 53 51 4c 49 54 45  .  int rc=SQLITE
d7540 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 69 66 20 77 65  _OK;..  /* if we
d7550 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 20   already have a 
d7560 6c 6f 63 6b 2c 20 69 74 20 69 73 20 65 78 63 6c  lock, it is excl
d7570 75 73 69 76 65 2e 20 20 0a 20 20 2a 2a 20 4a 75  usive.  .  ** Ju
d7580 73 74 20 61 64 6a 75 73 74 20 6c 65 76 65 6c 20  st adjust level 
d7590 61 6e 64 20 70 75 6e 74 20 6f 6e 20 6f 75 74 74  and punt on outt
d75a0 61 20 68 65 72 65 2e 20 2a 2f 0a 20 20 69 66 20  a here. */.  if 
d75b0 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65  (pFile->locktype
d75c0 20 3e 20 4e 4f 5f 4c 4f 43 4b 29 20 7b 0a 20 20   > NO_LOCK) {.  
d75d0 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
d75e0 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  e = locktype;.  
d75f0 20 20 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79 73    .    /* Always
d7600 20 75 70 64 61 74 65 20 74 68 65 20 74 69 6d 65   update the time
d7610 73 74 61 6d 70 20 6f 6e 20 74 68 65 20 6f 6c 64  stamp on the old
d7620 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 75 74 69   file */.    uti
d7630 6d 65 73 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20 4e  mes(zLockFile, N
d7640 55 4c 4c 29 3b 0a 20 20 20 20 72 63 20 3d 20 53  ULL);.    rc = S
d7650 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 67 6f  QLITE_OK;.    go
d7660 74 6f 20 64 6f 74 6c 6f 63 6b 5f 65 6e 64 5f 6c  to dotlock_end_l
d7670 6f 63 6b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a  ock;.  }.  .  /*
d7680 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   check to see if
d7690 20 6c 6f 63 6b 20 66 69 6c 65 20 61 6c 72 65 61   lock file alrea
d76a0 64 79 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 73  dy exists */.  s
d76b0 74 72 75 63 74 20 73 74 61 74 20 73 74 61 74 42  truct stat statB
d76c0 75 66 3b 0a 20 20 69 66 20 28 6c 73 74 61 74 28  uf;.  if (lstat(
d76d0 7a 4c 6f 63 6b 46 69 6c 65 2c 26 73 74 61 74 42  zLockFile,&statB
d76e0 75 66 29 20 3d 3d 20 30 29 7b 0a 20 20 20 20 72  uf) == 0){.    r
d76f0 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  c = SQLITE_BUSY;
d7700 20 2f 2a 20 69 74 20 64 6f 65 73 2c 20 62 75 73   /* it does, bus
d7710 79 20 2a 2f 0a 20 20 20 20 67 6f 74 6f 20 64 6f  y */.    goto do
d7720 74 6c 6f 63 6b 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a  tlock_end_lock;.
d7730 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 72 61 62    }.  .  /* grab
d7740 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f   an exclusive lo
d7750 63 6b 20 2a 2f 0a 20 20 66 64 20 3d 20 6f 70 65  ck */.  fd = ope
d7760 6e 28 7a 4c 6f 63 6b 46 69 6c 65 2c 4f 5f 52 44  n(zLockFile,O_RD
d7770 4f 4e 4c 59 7c 4f 5f 43 52 45 41 54 7c 4f 5f 45  ONLY|O_CREAT|O_E
d7780 58 43 4c 2c 30 36 30 30 29 3b 0a 20 20 69 66 28  XCL,0600);.  if(
d7790 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20   fd<0 ){.    /* 
d77a0 66 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e 2f 63  failed to open/c
d77b0 72 65 61 74 65 20 74 68 65 20 66 69 6c 65 2c 20  reate the file, 
d77c0 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d 61 79  someone else may
d77d0 20 68 61 76 65 20 73 74 6f 6c 65 6e 20 74 68 65   have stolen the
d77e0 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 6e 74   lock */.    int
d77f0 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b   tErrno = errno;
d7800 0a 20 20 20 20 69 66 28 20 45 45 58 49 53 54 20  .    if( EEXIST 
d7810 3d 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 20 20  == tErrno ){.   
d7820 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
d7830 55 53 59 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20  USY;.    } else 
d7840 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
d7850 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69  iteErrorFromPosi
d7860 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53  xError(tErrno, S
d7870 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b  QLITE_IOERR_LOCK
d7880 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f  );.      if( IS_
d7890 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29  LOCK_ERROR(rc) )
d78a0 7b 0a 09 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72  {..pFile->lastEr
d78b0 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20  rno = tErrno;.  
d78c0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
d78d0 67 6f 74 6f 20 64 6f 74 6c 6f 63 6b 5f 65 6e 64  goto dotlock_end
d78e0 5f 6c 6f 63 6b 3b 0a 20 20 7d 20 0a 20 20 63 6c  _lock;.  } .  cl
d78f0 6f 73 65 28 66 64 29 3b 0a 20 20 0a 20 20 2f 2a  ose(fd);.  .  /*
d7900 20 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68 65   got it, set the
d7910 20 74 79 70 65 20 61 6e 64 20 72 65 74 75 72 6e   type and return
d7920 20 6f 6b 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e   ok */.  pFile->
d7930 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74  locktype = lockt
d7940 79 70 65 3b 0a 0a 20 64 6f 74 6c 6f 63 6b 5f 65  ype;.. dotlock_e
d7950 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 72 65 74 75 72  nd_lock:.  retur
d7960 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  n rc;.}..static 
d7970 69 6e 74 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63  int dotlockUnloc
d7980 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
d7990 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
d79a0 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  ) {.  unixFile *
d79b0 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
d79c0 65 2a 29 69 64 3b 0a 20 20 63 68 61 72 20 2a 7a  e*)id;.  char *z
d79d0 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 61 72  LockFile = (char
d79e0 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e   *)pFile->lockin
d79f0 67 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 61 73 73  gContext;..  ass
d7a00 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20  ert( pFile );.  
d7a10 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b  OSTRACE5("UNLOCK
d7a20 20 20 25 64 20 25 64 20 77 61 73 20 25 64 20 70    %d %d was %d p
d7a30 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  id=%d\n", pFile-
d7a40 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 09 20  >h, locktype,.. 
d7a50 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
d7a60 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20  e, getpid());.  
d7a70 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65  assert( locktype
d7a80 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b  <=SHARED_LOCK );
d7a90 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69  .  .  /* no-op i
d7aa0 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 20  f possible */.  
d7ab0 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
d7ac0 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 7b  ype==locktype ){
d7ad0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
d7ae0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20  TE_OK;.  }.  .  
d7af0 2f 2a 20 73 68 61 72 65 64 20 63 61 6e 20 6a 75  /* shared can ju
d7b00 73 74 20 62 65 20 73 65 74 20 62 65 63 61 75 73  st be set becaus
d7b10 65 20 77 65 20 61 6c 77 61 79 73 20 68 61 76 65  e we always have
d7b20 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 2a 2f   an exclusive */
d7b30 0a 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 3d  .  if (locktype=
d7b40 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a  =SHARED_LOCK) {.
d7b50 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
d7b60 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a  ype = locktype;.
d7b70 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
d7b80 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f  E_OK;.  }.  .  /
d7b90 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 2c 20 75 6e  * no, really, un
d7ba0 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 66 20 28 75  lock. */.  if (u
d7bb0 6e 6c 69 6e 6b 28 7a 4c 6f 63 6b 46 69 6c 65 29  nlink(zLockFile)
d7bc0 20 29 20 7b 0a 20 20 20 20 69 6e 74 20 72 63 2c   ) {.    int rc,
d7bd0 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b   tErrno = errno;
d7be0 0a 20 20 20 20 69 66 28 20 45 4e 4f 45 4e 54 20  .    if( ENOENT 
d7bf0 21 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 20 20  != tErrno ){.   
d7c00 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72     rc = sqliteEr
d7c10 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f  rorFromPosixErro
d7c20 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45  r(tErrno, SQLITE
d7c30 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a  _IOERR_UNLOCK);.
d7c40 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 49 53      }.    if( IS
d7c50 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20  _LOCK_ERROR(rc) 
d7c60 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  ){.      pFile->
d7c70 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72  lastErrno = tErr
d7c80 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65  no;.    }.    re
d7c90 74 75 72 6e 20 72 63 3b 20 0a 20 20 7d 0a 20 20  turn rc; .  }.  
d7ca0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
d7cb0 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 65 74  = NO_LOCK;.  ret
d7cc0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
d7cd0 0a 0a 2f 2a 0a 20 2a 2a 20 43 6c 6f 73 65 20 61  ../*. ** Close a
d7ce0 20 66 69 6c 65 2e 0a 20 2a 2f 0a 73 74 61 74 69   file.. */.stati
d7cf0 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 43 6c 6f  c int dotlockClo
d7d00 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  se(sqlite3_file 
d7d10 2a 69 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20  *id) {.  if( id 
d7d20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20  ){.    unixFile 
d7d30 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
d7d40 6c 65 2a 29 69 64 3b 0a 20 20 20 20 64 6f 74 6c  le*)id;.    dotl
d7d50 6f 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f  ockUnlock(id, NO
d7d60 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 73 71 6c 69  _LOCK);.    sqli
d7d70 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e  te3_free(pFile->
d7d80 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b  lockingContext);
d7d90 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 6c  .  }.  return cl
d7da0 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b  oseUnixFile(id);
d7db0 0a 7d 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .}...#endif /* S
d7dc0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
d7dd0 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f  KING_STYLE */../
d7de0 2a 0a 2a 2a 20 54 68 65 20 6e 6f 6c 6f 63 6b 4c  *.** The nolockL
d7df0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 69 73  ockingContext is
d7e00 20 76 6f 69 64 0a 2a 2f 0a 74 79 70 65 64 65 66   void.*/.typedef
d7e10 20 76 6f 69 64 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b   void nolockLock
d7e20 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 0a 73 74 61  ingContext;..sta
d7e30 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 43 68  tic int nolockCh
d7e40 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
d7e50 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
d7e60 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20  , int *pResOut) 
d7e70 7b 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 30  {.  *pResOut = 0
d7e80 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
d7e90 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  E_OK;.}..static 
d7ea0 69 6e 74 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 28 73  int nolockLock(s
d7eb0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
d7ec0 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b   int locktype) {
d7ed0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
d7ee0 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69  _OK;.}..static i
d7ef0 6e 74 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 28  nt nolockUnlock(
d7f00 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
d7f10 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20  , int locktype) 
d7f20 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  {.  return SQLIT
d7f30 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
d7f40 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a  lose a file..*/.
d7f50 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63  static int noloc
d7f60 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66  kClose(sqlite3_f
d7f70 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 72 65 74  ile *id) {.  ret
d7f80 75 72 6e 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c  urn closeUnixFil
d7f90 65 28 69 64 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  e(id);.}.../*.**
d7fa0 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 6e 64   Information and
d7fb0 20 63 6f 6e 74 72 6f 6c 20 6f 66 20 61 6e 20 6f   control of an o
d7fc0 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e  pen file handle.
d7fd0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75  .*/.static int u
d7fe0 6e 69 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73  nixFileControl(s
d7ff0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
d8000 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70   int op, void *p
d8010 41 72 67 29 7b 0a 20 20 73 77 69 74 63 68 28 20  Arg){.  switch( 
d8020 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53  op ){.    case S
d8030 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b  QLITE_FCNTL_LOCK
d8040 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 2a  STATE: {.      *
d8050 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 28 75  (int*)pArg = ((u
d8060 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f  nixFile*)id)->lo
d8070 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 72 65  cktype;.      re
d8080 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
d8090 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
d80a0 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
d80b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
d80c0 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65   the sector size
d80d0 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65   in bytes of the
d80e0 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63   underlying bloc
d80f0 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20  k device for.** 
d8100 74 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69  the specified fi
d8110 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f  le. This is almo
d8120 73 74 20 61 6c 77 61 79 73 20 35 31 32 20 62 79  st always 512 by
d8130 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a  tes, but may be.
d8140 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f  ** larger for so
d8150 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a  me devices..**.*
d8160 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73  * SQLite code as
d8170 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74  sumes this funct
d8180 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e  ion cannot fail.
d8190 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73   It also assumes
d81a0 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20   that.** if two 
d81b0 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 65  files are create
d81c0 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69  d in the same fi
d81d0 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74  le-system direct
d81e0 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64  ory (i.e..** a d
d81f0 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 73 20  atabase and its 
d8200 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68  journal file) th
d8210 61 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 69  at the sector si
d8220 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a  ze will be the.*
d8230 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e  * same for both.
d8240 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75  .*/.static int u
d8250 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 28 73 71  nixSectorSize(sq
d8260 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b  lite3_file *id){
d8270 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
d8280 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f  _DEFAULT_SECTOR_
d8290 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  SIZE;.}../*.** R
d82a0 65 74 75 72 6e 20 74 68 65 20 64 65 76 69 63 65  eturn the device
d82b0 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73   characteristics
d82c0 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 20 54   for the file. T
d82d0 68 69 73 20 69 73 20 61 6c 77 61 79 73 20 30 2e  his is always 0.
d82e0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75  .*/.static int u
d82f0 6e 69 78 44 65 76 69 63 65 43 68 61 72 61 63 74  nixDeviceCharact
d8300 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 65 33  eristics(sqlite3
d8310 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 72 65  _file *id){.  re
d8320 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
d8330 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   Initialize the 
d8340 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
d8350 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75  unixFile structu
d8360 72 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  re pointed to by
d8370 20 70 49 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e   pId..**.** When
d8380 20 6c 6f 63 6b 69 6e 67 20 65 78 74 65 6e 73 69   locking extensi
d8390 6f 6e 73 20 61 72 65 20 65 6e 61 62 6c 65 64 2c  ons are enabled,
d83a0 20 74 68 65 20 66 69 6c 65 70 61 74 68 20 61 6e   the filepath an
d83b0 64 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 20  d locking style 
d83c0 0a 2a 2a 20 61 72 65 20 6e 65 65 64 65 64 20 74  .** are needed t
d83d0 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  o determine the 
d83e0 75 6e 69 78 46 69 6c 65 20 70 4d 65 74 68 6f 64  unixFile pMethod
d83f0 20 74 6f 20 75 73 65 20 66 6f 72 20 6c 6f 63 6b   to use for lock
d8400 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a  ing operations..
d8410 2a 2a 20 54 68 65 20 6c 6f 63 6b 69 6e 67 2d 73  ** The locking-s
d8420 74 79 6c 65 20 73 70 65 63 69 66 69 63 20 6c 6f  tyle specific lo
d8430 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 64 61 74  ckingContext dat
d8440 61 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63  a structure is c
d8450 72 65 61 74 65 64 20 0a 2a 2a 20 61 6e 64 20 61  reated .** and a
d8460 73 73 69 67 6e 65 64 20 68 65 72 65 20 61 6c 73  ssigned here als
d8470 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  o..*/.static int
d8480 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28   fillInUnixFile(
d8490 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
d84a0 70 56 66 73 2c 20 20 20 20 20 20 2f 2a 20 50 6f  pVfs,      /* Po
d84b0 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 6a  inter to vfs obj
d84c0 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20  ect */.  int h, 
d84d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d84e0 20 2f 2a 20 4f 70 65 6e 20 66 69 6c 65 20 64 65   /* Open file de
d84f0 73 63 72 69 70 74 6f 72 20 6f 66 20 66 69 6c 65  scriptor of file
d8500 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f   being opened */
d8510 0a 20 20 69 6e 74 20 64 69 72 66 64 2c 20 20 20  .  int dirfd,   
d8520 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69             /* Di
d8530 72 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65 73  rectory file des
d8540 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 73 71 6c  criptor */.  sql
d8550 69 74 65 33 5f 66 69 6c 65 20 2a 70 49 64 2c 20  ite3_file *pId, 
d8560 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 6f       /* Write to
d8570 20 74 68 65 20 75 6e 69 78 46 69 6c 65 20 73 74   the unixFile st
d8580 72 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a  ructure here */.
d8590 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
d85a0 69 6c 65 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d  ilename,  /* Nam
d85b0 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 62 65  e of the file be
d85c0 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20  ing opened */.  
d85d0 69 6e 74 20 6e 6f 4c 6f 63 6b 20 20 20 20 20 20  int noLock      
d85e0 20 20 20 20 20 20 20 20 2f 2a 20 4f 6d 69 74 20          /* Omit 
d85f0 6c 6f 63 6b 69 6e 67 20 69 66 20 74 72 75 65 20  locking if true 
d8600 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 65 4c 6f 63  */.){.  int eLoc
d8610 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20 75 6e 69  kingStyle;.  uni
d8620 78 46 69 6c 65 20 2a 70 4e 65 77 20 3d 20 28 75  xFile *pNew = (u
d8630 6e 69 78 46 69 6c 65 20 2a 29 70 49 64 3b 0a 20  nixFile *)pId;. 
d8640 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
d8650 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 4d 61 63 72 6f  _OK;..  /* Macro
d8660 20 74 6f 20 64 65 66 69 6e 65 20 74 68 65 20 73   to define the s
d8670 74 61 74 69 63 20 63 6f 6e 74 65 6e 74 73 20 6f  tatic contents o
d8680 66 20 61 6e 20 73 71 6c 69 74 65 33 5f 69 6f 5f  f an sqlite3_io_
d8690 6d 65 74 68 6f 64 73 20 0a 20 20 2a 2a 20 73 74  methods .  ** st
d86a0 72 75 63 74 75 72 65 20 66 6f 72 20 61 20 75 6e  ructure for a un
d86b0 69 78 20 62 61 63 6b 65 6e 64 20 66 69 6c 65 2e  ix backend file.
d86c0 20 44 69 66 66 65 72 65 6e 74 20 6c 6f 63 6b 69   Different locki
d86d0 6e 67 20 6d 65 74 68 6f 64 73 0a 20 20 2a 2a 20  ng methods.  ** 
d86e0 72 65 71 75 69 72 65 20 64 69 66 66 65 72 65 6e  require differen
d86f0 74 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20  t functions for 
d8700 74 68 65 20 78 43 6c 6f 73 65 2c 20 78 4c 6f 63  the xClose, xLoc
d8710 6b 2c 20 78 55 6e 6c 6f 63 6b 20 61 6e 64 0a 20  k, xUnlock and. 
d8720 20 2a 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76   ** xCheckReserv
d8730 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 73 2e 0a  edLock methods..
d8740 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 49    */.  #define I
d8750 4f 4d 45 54 48 4f 44 53 28 78 43 6c 6f 73 65 2c  OMETHODS(xClose,
d8760 20 78 4c 6f 63 6b 2c 20 78 55 6e 6c 6f 63 6b 2c   xLock, xUnlock,
d8770 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c   xCheckReservedL
d8780 6f 63 6b 29 20 7b 20 20 20 20 5c 0a 20 20 20 20  ock) {    \.    
d8790 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1,              
d87a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69              /* i
d87b0 56 65 72 73 69 6f 6e 20 2a 2f 20 20 20 20 20 20  Version */      
d87c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d87d0 20 20 20 20 20 5c 0a 20 20 20 20 78 43 6c 6f 73       \.    xClos
d87e0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
d87f0 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65         /* xClose
d8800 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
d8810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d8820 5c 0a 20 20 20 20 75 6e 69 78 52 65 61 64 2c 20  \.    unixRead, 
d8830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d8840 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f 20 20 20    /* xRead */   
d8850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d8860 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
d8870 20 75 6e 69 78 57 72 69 74 65 2c 20 20 20 20 20   unixWrite,     
d8880 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
d8890 78 57 72 69 74 65 20 2a 2f 20 20 20 20 20 20 20  xWrite */       
d88a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d88b0 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78        \.    unix
d88c0 54 72 75 6e 63 61 74 65 2c 20 20 20 20 20 20 20  Truncate,       
d88d0 20 20 20 20 20 20 20 20 2f 2a 20 78 54 72 75 6e          /* xTrun
d88e0 63 61 74 65 20 2a 2f 20 20 20 20 20 20 20 20 20  cate */         
d88f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d8900 20 5c 0a 20 20 20 20 75 6e 69 78 53 79 6e 63 2c   \.    unixSync,
d8910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d8920 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 20 20     /* xSync */  
d8930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d8940 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
d8950 20 20 75 6e 69 78 46 69 6c 65 53 69 7a 65 2c 20    unixFileSize, 
d8960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
d8970 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 20 20 20   xFileSize */   
d8980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d8990 20 20 20 20 20 20 20 5c 0a 20 20 20 20 78 4c 6f         \.    xLo
d89a0 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ck,             
d89b0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63           /* xLoc
d89c0 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  k */            
d89d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d89e0 20 20 5c 0a 20 20 20 20 78 55 6e 6c 6f 63 6b 2c    \.    xUnlock,
d89f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d8a00 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a      /* xUnlock *
d8a10 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
d8a20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
d8a30 20 20 20 78 43 68 65 63 6b 52 65 73 65 72 76 65     xCheckReserve
d8a40 64 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 2f  dLock,         /
d8a50 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  * xCheckReserved
d8a60 4c 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20  Lock */         
d8a70 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e          \.    un
d8a80 69 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c 20 20  ixFileControl,  
d8a90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69            /* xFi
d8aa0 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 20 20 20 20  leControl */    
d8ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d8ac0 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 53 65 63     \.    unixSec
d8ad0 74 6f 72 53 69 7a 65 2c 20 20 20 20 20 20 20 20  torSize,        
d8ae0 20 20 20 20 20 2f 2a 20 78 53 65 63 74 6f 72 53       /* xSectorS
d8af0 69 7a 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ize */          
d8b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
d8b10 20 20 20 20 75 6e 69 78 44 65 76 69 63 65 43 68      unixDeviceCh
d8b20 61 72 61 63 74 65 72 69 73 74 69 63 73 20 20 20  aracteristics   
d8b30 2f 2a 20 78 44 65 76 69 63 65 43 61 70 61 62 69  /* xDeviceCapabi
d8b40 6c 69 74 69 65 73 20 2a 2f 20 20 20 20 20 20 20  lities */       
d8b50 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 20           \.  }. 
d8b60 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f   static sqlite3_
d8b70 69 6f 5f 6d 65 74 68 6f 64 73 20 61 49 6f 4d 65  io_methods aIoMe
d8b80 74 68 6f 64 5b 5d 20 3d 20 7b 0a 20 20 20 20 49  thod[] = {.    I
d8b90 4f 4d 45 54 48 4f 44 53 28 75 6e 69 78 43 6c 6f  OMETHODS(unixClo
d8ba0 73 65 2c 20 75 6e 69 78 4c 6f 63 6b 2c 20 75 6e  se, unixLock, un
d8bb0 69 78 55 6e 6c 6f 63 6b 2c 20 75 6e 69 78 43 68  ixUnlock, unixCh
d8bc0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 29  eckReservedLock)
d8bd0 20 0a 20 20 20 2c 49 4f 4d 45 54 48 4f 44 53 28   .   ,IOMETHODS(
d8be0 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 2c 20 6e 6f 6c  nolockClose, nol
d8bf0 6f 63 6b 4c 6f 63 6b 2c 20 6e 6f 6c 6f 63 6b 55  ockLock, nolockU
d8c00 6e 6c 6f 63 6b 2c 20 6e 6f 6c 6f 63 6b 43 68 65  nlock, nolockChe
d8c10 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 29 0a  ckReservedLock).
d8c20 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  #if SQLITE_ENABL
d8c30 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a  E_LOCKING_STYLE.
d8c40 20 20 20 2c 49 4f 4d 45 54 48 4f 44 53 28 64 6f     ,IOMETHODS(do
d8c50 74 6c 6f 63 6b 43 6c 6f 73 65 2c 20 64 6f 74 6c  tlockClose, dotl
d8c60 6f 63 6b 4c 6f 63 6b 2c 20 64 6f 74 6c 6f 63 6b  ockLock, dotlock
d8c70 55 6e 6c 6f 63 6b 2c 64 6f 74 6c 6f 63 6b 43 68  Unlock,dotlockCh
d8c80 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 29  eckReservedLock)
d8c90 0a 20 20 20 2c 49 4f 4d 45 54 48 4f 44 53 28 66  .   ,IOMETHODS(f
d8ca0 6c 6f 63 6b 43 6c 6f 73 65 2c 20 66 6c 6f 63 6b  lockClose, flock
d8cb0 4c 6f 63 6b 2c 20 66 6c 6f 63 6b 55 6e 6c 6f 63  Lock, flockUnloc
d8cc0 6b 2c 20 66 6c 6f 63 6b 43 68 65 63 6b 52 65 73  k, flockCheckRes
d8cd0 65 72 76 65 64 4c 6f 63 6b 29 0a 20 20 20 2c 49  ervedLock).   ,I
d8ce0 4f 4d 45 54 48 4f 44 53 28 61 66 70 43 6c 6f 73  OMETHODS(afpClos
d8cf0 65 2c 20 61 66 70 4c 6f 63 6b 2c 20 61 66 70 55  e, afpLock, afpU
d8d00 6e 6c 6f 63 6b 2c 20 61 66 70 43 68 65 63 6b 52  nlock, afpCheckR
d8d10 65 73 65 72 76 65 64 4c 6f 63 6b 29 0a 23 65 6e  eservedLock).#en
d8d20 64 69 66 0a 20 20 7d 3b 0a 20 20 2f 2a 20 54 68  dif.  };.  /* Th
d8d30 65 20 6f 72 64 65 72 20 6f 66 20 74 68 65 20 49  e order of the I
d8d40 4f 4d 45 54 48 4f 44 53 20 6d 61 63 72 6f 73 20  OMETHODS macros 
d8d50 61 62 6f 76 65 20 69 73 20 69 6d 70 6f 72 74 61  above is importa
d8d60 6e 74 2e 20 20 49 74 20 6d 75 73 74 20 62 65 20  nt.  It must be 
d8d70 74 68 65 0a 20 20 2a 2a 20 73 61 6d 65 20 6f 72  the.  ** same or
d8d80 64 65 72 20 61 73 20 74 68 65 20 4c 4f 43 4b 49  der as the LOCKI
d8d90 4e 47 5f 53 54 59 4c 45 20 6e 75 6d 62 65 72 73  NG_STYLE numbers
d8da0 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 4c  .  */.  assert(L
d8db0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 50 4f 53  OCKING_STYLE_POS
d8dc0 49 58 3d 3d 31 29 3b 0a 20 20 61 73 73 65 72 74  IX==1);.  assert
d8dd0 28 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e  (LOCKING_STYLE_N
d8de0 4f 4e 45 3d 3d 32 29 3b 0a 20 20 61 73 73 65 72  ONE==2);.  asser
d8df0 74 28 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f  t(LOCKING_STYLE_
d8e00 44 4f 54 46 49 4c 45 3d 3d 33 29 3b 0a 20 20 61  DOTFILE==3);.  a
d8e10 73 73 65 72 74 28 4c 4f 43 4b 49 4e 47 5f 53 54  ssert(LOCKING_ST
d8e20 59 4c 45 5f 46 4c 4f 43 4b 3d 3d 34 29 3b 0a 20  YLE_FLOCK==4);. 
d8e30 20 61 73 73 65 72 74 28 4c 4f 43 4b 49 4e 47 5f   assert(LOCKING_
d8e40 53 54 59 4c 45 5f 41 46 50 3d 3d 35 29 3b 0a 0a  STYLE_AFP==5);..
d8e50 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e    assert( pNew->
d8e60 70 4c 6f 63 6b 3d 3d 4e 55 4c 4c 20 29 3b 0a 20  pLock==NULL );. 
d8e70 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 70   assert( pNew->p
d8e80 4f 70 65 6e 3d 3d 4e 55 4c 4c 20 29 3b 0a 0a 20  Open==NULL );.. 
d8e90 20 4f 53 54 52 41 43 45 33 28 22 4f 50 45 4e 20   OSTRACE3("OPEN 
d8ea0 20 20 20 25 2d 33 64 20 25 73 5c 6e 22 2c 20 68     %-3d %s\n", h
d8eb0 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 20 20 20  , zFilename);   
d8ec0 20 0a 20 20 70 4e 65 77 2d 3e 68 20 3d 20 68 3b   .  pNew->h = h;
d8ed0 0a 20 20 70 4e 65 77 2d 3e 64 69 72 66 64 20 3d  .  pNew->dirfd =
d8ee0 20 64 69 72 66 64 3b 0a 20 20 53 45 54 5f 54 48   dirfd;.  SET_TH
d8ef0 52 45 41 44 49 44 28 70 4e 65 77 29 3b 0a 0a 20  READID(pNew);.. 
d8f00 20 69 66 28 20 6e 6f 4c 6f 63 6b 20 29 7b 0a 20   if( noLock ){. 
d8f10 20 20 20 65 4c 6f 63 6b 69 6e 67 53 74 79 6c 65     eLockingStyle
d8f20 20 3d 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45   = LOCKING_STYLE
d8f30 5f 4e 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a  _NONE;.  }else{.
d8f40 20 20 20 20 65 4c 6f 63 6b 69 6e 67 53 74 79 6c      eLockingStyl
d8f50 65 20 3d 20 64 65 74 65 63 74 4c 6f 63 6b 69 6e  e = detectLockin
d8f60 67 53 74 79 6c 65 28 70 56 66 73 2c 20 7a 46 69  gStyle(pVfs, zFi
d8f70 6c 65 6e 61 6d 65 2c 20 68 29 3b 0a 20 20 7d 0a  lename, h);.  }.
d8f80 0a 20 20 73 77 69 74 63 68 28 20 65 4c 6f 63 6b  .  switch( eLock
d8f90 69 6e 67 53 74 79 6c 65 20 29 7b 0a 0a 20 20 20  ingStyle ){..   
d8fa0 20 63 61 73 65 20 4c 4f 43 4b 49 4e 47 5f 53 54   case LOCKING_ST
d8fb0 59 4c 45 5f 50 4f 53 49 58 3a 20 7b 0a 20 20 20  YLE_POSIX: {.   
d8fc0 20 20 20 65 6e 74 65 72 4d 75 74 65 78 28 29 3b     enterMutex();
d8fd0 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69 6e 64  .      rc = find
d8fe0 4c 6f 63 6b 49 6e 66 6f 28 68 2c 20 26 70 4e 65  LockInfo(h, &pNe
d8ff0 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65 77 2d  w->pLock, &pNew-
d9000 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20 6c  >pOpen);.      l
d9010 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20  eaveMutex();.   
d9020 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
d9030 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  .#if SQLITE_ENAB
d9040 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
d9050 0a 20 20 20 20 63 61 73 65 20 4c 4f 43 4b 49 4e  .    case LOCKIN
d9060 47 5f 53 54 59 4c 45 5f 41 46 50 3a 20 7b 0a 20  G_STYLE_AFP: {. 
d9070 20 20 20 20 20 2f 2a 20 41 46 50 20 6c 6f 63 6b       /* AFP lock
d9080 69 6e 67 20 75 73 65 73 20 74 68 65 20 66 69 6c  ing uses the fil
d9090 65 20 70 61 74 68 20 73 6f 20 69 74 20 6e 65 65  e path so it nee
d90a0 64 73 20 74 6f 20 62 65 20 69 6e 63 6c 75 64 65  ds to be include
d90b0 64 20 69 6e 0a 20 20 20 20 20 20 2a 2a 20 74 68  d in.      ** th
d90c0 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74  e afpLockingCont
d90d0 65 78 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ext..      */.  
d90e0 20 20 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f      afpLockingCo
d90f0 6e 74 65 78 74 20 2a 70 43 74 78 3b 0a 20 20 20  ntext *pCtx;.   
d9100 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67     pNew->locking
d9110 43 6f 6e 74 65 78 74 20 3d 20 70 43 74 78 20 3d  Context = pCtx =
d9120 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
d9130 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 29 20 29   sizeof(*pCtx) )
d9140 3b 0a 20 20 20 20 20 20 69 66 28 20 70 43 74 78  ;.      if( pCtx
d9150 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
d9160 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
d9170 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
d9180 20 20 20 20 20 20 20 2f 2a 20 4e 42 3a 20 7a 46         /* NB: zF
d9190 69 6c 65 6e 61 6d 65 20 65 78 69 73 74 73 20 61  ilename exists a
d91a0 6e 64 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64  nd remains valid
d91b0 20 75 6e 74 69 6c 20 74 68 65 20 66 69 6c 65 20   until the file 
d91c0 69 73 20 63 6c 6f 73 65 64 0a 20 20 20 20 20 20  is closed.      
d91d0 20 20 2a 2a 20 61 63 63 6f 72 64 69 6e 67 20 74    ** according t
d91e0 6f 20 72 65 71 75 69 72 65 6d 65 6e 74 20 46 31  o requirement F1
d91f0 31 31 34 31 2e 20 20 53 6f 20 77 65 20 64 6f 20  1141.  So we do 
d9200 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65  not need to make
d9210 20 61 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f   a.        ** co
d9220 70 79 20 6f 66 20 74 68 65 20 66 69 6c 65 6e 61  py of the filena
d9230 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 70  me. */.        p
d9240 43 74 78 2d 3e 66 69 6c 65 50 61 74 68 20 3d 20  Ctx->filePath = 
d9250 7a 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 20 20 20  zFilename;.     
d9260 20 20 20 73 72 61 6e 64 6f 6d 64 65 76 28 29 3b     srandomdev();
d9270 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
d9280 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
d9290 20 63 61 73 65 20 4c 4f 43 4b 49 4e 47 5f 53 54   case LOCKING_ST
d92a0 59 4c 45 5f 44 4f 54 46 49 4c 45 3a 20 7b 0a 20  YLE_DOTFILE: {. 
d92b0 20 20 20 20 20 2f 2a 20 44 6f 74 66 69 6c 65 20       /* Dotfile 
d92c0 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65  locking uses the
d92d0 20 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74   file path so it
d92e0 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e 63   needs to be inc
d92f0 6c 75 64 65 64 20 69 6e 0a 20 20 20 20 20 20 2a  luded in.      *
d9300 2a 20 74 68 65 20 64 6f 74 6c 6f 63 6b 4c 6f 63  * the dotlockLoc
d9310 6b 69 6e 67 43 6f 6e 74 65 78 74 20 0a 20 20 20  kingContext .   
d9320 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72     */.      char
d9330 20 2a 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 20 20   *zLockFile;.   
d9340 20 20 20 69 6e 74 20 6e 46 69 6c 65 6e 61 6d 65     int nFilename
d9350 3b 0a 20 20 20 20 20 20 6e 46 69 6c 65 6e 61 6d  ;.      nFilenam
d9360 65 20 3d 20 73 74 72 6c 65 6e 28 7a 46 69 6c 65  e = strlen(zFile
d9370 6e 61 6d 65 29 20 2b 20 36 3b 0a 20 20 20 20 20  name) + 6;.     
d9380 20 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68   zLockFile = (ch
d9390 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  ar *)sqlite3_mal
d93a0 6c 6f 63 28 6e 46 69 6c 65 6e 61 6d 65 29 3b 0a  loc(nFilename);.
d93b0 20 20 20 20 20 20 69 66 28 20 7a 4c 6f 63 6b 46        if( zLockF
d93c0 69 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ile==0 ){.      
d93d0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
d93e0 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  MEM;.      }else
d93f0 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
d9400 33 5f 73 6e 70 72 69 6e 74 66 28 6e 46 69 6c 65  3_snprintf(nFile
d9410 6e 61 6d 65 2c 20 7a 4c 6f 63 6b 46 69 6c 65 2c  name, zLockFile,
d9420 20 22 25 73 2e 6c 6f 63 6b 22 2c 20 7a 46 69 6c   "%s.lock", zFil
d9430 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a  ename);.      }.
d9440 20 20 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b        pNew->lock
d9450 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 7a 4c 6f  ingContext = zLo
d9460 63 6b 46 69 6c 65 3b 0a 20 20 20 20 20 20 62 72  ckFile;.      br
d9470 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
d9480 63 61 73 65 20 4c 4f 43 4b 49 4e 47 5f 53 54 59  case LOCKING_STY
d9490 4c 45 5f 46 4c 4f 43 4b 3a 20 0a 20 20 20 20 63  LE_FLOCK: .    c
d94a0 61 73 65 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  ase LOCKING_STYL
d94b0 45 5f 4e 4f 4e 45 3a 20 0a 20 20 20 20 20 20 62  E_NONE: .      b
d94c0 72 65 61 6b 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  reak;.#endif.  }
d94d0 0a 20 20 0a 20 20 70 4e 65 77 2d 3e 6c 61 73 74  .  .  pNew->last
d94e0 45 72 72 6e 6f 20 3d 20 30 3b 0a 20 20 69 66 28  Errno = 0;.  if(
d94f0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
d9500 7b 0a 20 20 20 20 69 66 28 20 64 69 72 66 64 3e  {.    if( dirfd>
d9510 3d 30 20 29 20 63 6c 6f 73 65 28 64 69 72 66 64  =0 ) close(dirfd
d9520 29 3b 0a 20 20 20 20 63 6c 6f 73 65 28 68 29 3b  );.    close(h);
d9530 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e  .  }else{.    pN
d9540 65 77 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 61  ew->pMethod = &a
d9550 49 6f 4d 65 74 68 6f 64 5b 65 4c 6f 63 6b 69 6e  IoMethod[eLockin
d9560 67 53 74 79 6c 65 2d 31 5d 3b 0a 20 20 20 20 4f  gStyle-1];.    O
d9570 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a  penCounter(+1);.
d9580 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
d9590 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61  .}../*.** Open a
d95a0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
d95b0 20 74 6f 20 74 68 65 20 64 69 72 65 63 74 6f 72   to the director
d95c0 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69 6c  y containing fil
d95d0 65 20 7a 46 69 6c 65 6e 61 6d 65 2e 0a 2a 2a 20  e zFilename..** 
d95e0 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 2a  If successful, *
d95f0 70 46 64 20 69 73 20 73 65 74 20 74 6f 20 74 68  pFd is set to th
d9600 65 20 6f 70 65 6e 65 64 20 66 69 6c 65 20 64 65  e opened file de
d9610 73 63 72 69 70 74 6f 72 20 61 6e 64 0a 2a 2a 20  scriptor and.** 
d9620 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
d9630 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72  urned. If an err
d9640 6f 72 20 6f 63 63 75 72 73 2c 20 65 69 74 68 65  or occurs, eithe
d9650 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a  r SQLITE_NOMEM.*
d9660 2a 20 6f 72 20 53 51 4c 49 54 45 5f 43 41 4e 54  * or SQLITE_CANT
d9670 4f 50 45 4e 20 69 73 20 72 65 74 75 72 6e 65 64  OPEN is returned
d9680 20 61 6e 64 20 2a 70 46 64 20 69 73 20 73 65 74   and *pFd is set
d9690 20 74 6f 20 61 6e 20 75 6e 64 65 66 69 6e 65 64   to an undefined
d96a0 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a  .** value..**.**
d96b0 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73   If SQLITE_OK is
d96c0 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 20 63   returned, the c
d96d0 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73  aller is respons
d96e0 69 62 6c 65 20 66 6f 72 20 63 6c 6f 73 69 6e 67  ible for closing
d96f0 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20 64 65 73  .** the file des
d9700 63 72 69 70 74 6f 72 20 2a 70 46 64 20 75 73 69  criptor *pFd usi
d9710 6e 67 20 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a 73  ng close()..*/.s
d9720 74 61 74 69 63 20 69 6e 74 20 6f 70 65 6e 44 69  tatic int openDi
d9730 72 65 63 74 6f 72 79 28 63 6f 6e 73 74 20 63 68  rectory(const ch
d9740 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 69  ar *zFilename, i
d9750 6e 74 20 2a 70 46 64 29 7b 0a 20 20 69 6e 74 20  nt *pFd){.  int 
d9760 69 69 3b 0a 20 20 69 6e 74 20 66 64 20 3d 20 2d  ii;.  int fd = -
d9770 31 3b 0a 20 20 63 68 61 72 20 7a 44 69 72 6e 61  1;.  char zDirna
d9780 6d 65 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b  me[MAX_PATHNAME+
d9790 31 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73  1];..  sqlite3_s
d97a0 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48  nprintf(MAX_PATH
d97b0 4e 41 4d 45 2c 20 7a 44 69 72 6e 61 6d 65 2c 20  NAME, zDirname, 
d97c0 22 25 73 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29  "%s", zFilename)
d97d0 3b 0a 20 20 66 6f 72 28 69 69 3d 73 74 72 6c 65  ;.  for(ii=strle
d97e0 6e 28 7a 44 69 72 6e 61 6d 65 29 3b 20 69 69 3e  n(zDirname); ii>
d97f0 3d 30 20 26 26 20 7a 44 69 72 6e 61 6d 65 5b 69  =0 && zDirname[i
d9800 69 5d 21 3d 27 2f 27 3b 20 69 69 2d 2d 29 3b 0a  i]!='/'; ii--);.
d9810 20 20 69 66 28 20 69 69 3e 30 20 29 7b 0a 20 20    if( ii>0 ){.  
d9820 20 20 7a 44 69 72 6e 61 6d 65 5b 69 69 5d 20 3d    zDirname[ii] =
d9830 20 27 5c 30 27 3b 0a 20 20 20 20 66 64 20 3d 20   '\0';.    fd = 
d9840 6f 70 65 6e 28 7a 44 69 72 6e 61 6d 65 2c 20 4f  open(zDirname, O
d9850 5f 52 44 4f 4e 4c 59 7c 4f 5f 42 49 4e 41 52 59  _RDONLY|O_BINARY
d9860 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 66 64  , 0);.    if( fd
d9870 3e 3d 30 20 29 7b 0a 23 69 66 64 65 66 20 46 44  >=0 ){.#ifdef FD
d9880 5f 43 4c 4f 45 58 45 43 0a 20 20 20 20 20 20 66  _CLOEXEC.      f
d9890 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45 54 46 44  cntl(fd, F_SETFD
d98a0 2c 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47 45  , fcntl(fd, F_GE
d98b0 54 46 44 2c 20 30 29 20 7c 20 46 44 5f 43 4c 4f  TFD, 0) | FD_CLO
d98c0 45 58 45 43 29 3b 0a 23 65 6e 64 69 66 0a 20 20  EXEC);.#endif.  
d98d0 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 4f 50      OSTRACE3("OP
d98e0 45 4e 44 49 52 20 25 2d 33 64 20 25 73 5c 6e 22  ENDIR %-3d %s\n"
d98f0 2c 20 66 64 2c 20 7a 44 69 72 6e 61 6d 65 29 3b  , fd, zDirname);
d9900 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 46  .    }.  }.  *pF
d9910 64 20 3d 20 66 64 3b 0a 20 20 72 65 74 75 72 6e  d = fd;.  return
d9920 20 28 66 64 3e 3d 30 3f 53 51 4c 49 54 45 5f 4f   (fd>=0?SQLITE_O
d9930 4b 3a 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45  K:SQLITE_CANTOPE
d9940 4e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  N);.}../*.** Cre
d9950 61 74 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20  ate a temporary 
d9960 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42 75  file name in zBu
d9970 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 20 62 65  f.  zBuf must be
d9980 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 79   allocated.** by
d9990 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f   the calling pro
d99a0 63 65 73 73 20 61 6e 64 20 6d 75 73 74 20 62 65  cess and must be
d99b0 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 68   big enough to h
d99c0 6f 6c 64 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20  old at least.** 
d99d0 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65  pVfs->mxPathname
d99e0 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69   bytes..*/.stati
d99f0 63 20 69 6e 74 20 67 65 74 54 65 6d 70 6e 61 6d  c int getTempnam
d9a00 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72  e(int nBuf, char
d9a10 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74 61 74 69   *zBuf){.  stati
d9a20 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 7a  c const char *az
d9a30 44 69 72 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  Dirs[] = {.     
d9a40 30 2c 0a 20 20 20 20 20 22 2f 76 61 72 2f 74 6d  0,.     "/var/tm
d9a50 70 22 2c 0a 20 20 20 20 20 22 2f 75 73 72 2f 74  p",.     "/usr/t
d9a60 6d 70 22 2c 0a 20 20 20 20 20 22 2f 74 6d 70 22  mp",.     "/tmp"
d9a70 2c 0a 20 20 20 20 20 22 2e 22 2c 0a 20 20 7d 3b  ,.     ".",.  };
d9a80 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
d9a90 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a 43  unsigned char zC
d9aa0 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62  hars[] =.    "ab
d9ab0 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72  cdefghijklmnopqr
d9ac0 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22 41  stuvwxyz".    "A
d9ad0 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51  BCDEFGHIJKLMNOPQ
d9ae0 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20 22  RSTUVWXYZ".    "
d9af0 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20 69  0123456789";.  i
d9b00 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 74 72 75 63  nt i, j;.  struc
d9b10 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 63 6f  t stat buf;.  co
d9b20 6e 73 74 20 63 68 61 72 20 2a 7a 44 69 72 20 3d  nst char *zDir =
d9b30 20 22 2e 22 3b 0a 0a 20 20 2f 2a 20 49 74 27 73   ".";..  /* It's
d9b40 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61 74 65   odd to simulate
d9b50 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68 65 72   an io-error her
d9b60 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 20 74 68  e, but really th
d9b70 69 73 20 69 73 20 6a 75 73 74 0a 20 20 2a 2a 20  is is just.  ** 
d9b80 75 73 69 6e 67 20 74 68 65 20 69 6f 2d 65 72 72  using the io-err
d9b90 6f 72 20 69 6e 66 72 61 73 74 72 75 63 74 75 72  or infrastructur
d9ba0 65 20 74 6f 20 74 65 73 74 20 74 68 61 74 20 53  e to test that S
d9bb0 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 74 68  QLite handles th
d9bc0 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e  is.  ** function
d9bd0 20 66 61 69 6c 69 6e 67 2e 20 0a 20 20 2a 2f 0a   failing. .  */.
d9be0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
d9bf0 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
d9c00 5f 49 4f 45 52 52 20 29 3b 0a 0a 20 20 61 7a 44  _IOERR );..  azD
d9c10 69 72 73 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33  irs[0] = sqlite3
d9c20 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b  _temp_directory;
d9c30 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69  .  for(i=0; i<si
d9c40 7a 65 6f 66 28 61 7a 44 69 72 73 29 2f 73 69 7a  zeof(azDirs)/siz
d9c50 65 6f 66 28 61 7a 44 69 72 73 5b 30 5d 29 3b 20  eof(azDirs[0]); 
d9c60 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 7a  i++){.    if( az
d9c70 44 69 72 73 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e  Dirs[i]==0 ) con
d9c80 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 73  tinue;.    if( s
d9c90 74 61 74 28 61 7a 44 69 72 73 5b 69 5d 2c 20 26  tat(azDirs[i], &
d9ca0 62 75 66 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b  buf) ) continue;
d9cb0 0a 20 20 20 20 69 66 28 20 21 53 5f 49 53 44 49  .    if( !S_ISDI
d9cc0 52 28 62 75 66 2e 73 74 5f 6d 6f 64 65 29 20 29  R(buf.st_mode) )
d9cd0 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69   continue;.    i
d9ce0 66 28 20 61 63 63 65 73 73 28 61 7a 44 69 72 73  f( access(azDirs
d9cf0 5b 69 5d 2c 20 30 37 29 20 29 20 63 6f 6e 74 69  [i], 07) ) conti
d9d00 6e 75 65 3b 0a 20 20 20 20 7a 44 69 72 20 3d 20  nue;.    zDir = 
d9d10 61 7a 44 69 72 73 5b 69 5d 3b 0a 20 20 20 20 62  azDirs[i];.    b
d9d20 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  reak;.  }..  /* 
d9d30 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 6f  Check that the o
d9d40 75 74 70 75 74 20 62 75 66 66 65 72 20 69 73 20  utput buffer is 
d9d50 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 66 6f 72  large enough for
d9d60 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 66   the temporary f
d9d70 69 6c 65 20 0a 20 20 2a 2a 20 6e 61 6d 65 2e 20  ile .  ** name. 
d9d80 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 72 65  If it is not, re
d9d90 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
d9da0 52 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 73  R..  */.  if( (s
d9db0 74 72 6c 65 6e 28 7a 44 69 72 29 20 2b 20 73 74  trlen(zDir) + st
d9dc0 72 6c 65 6e 28 53 51 4c 49 54 45 5f 54 45 4d 50  rlen(SQLITE_TEMP
d9dd0 5f 46 49 4c 45 5f 50 52 45 46 49 58 29 20 2b 20  _FILE_PREFIX) + 
d9de0 31 37 29 20 3e 3d 20 6e 42 75 66 20 29 7b 0a 20  17) >= nBuf ){. 
d9df0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
d9e00 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 64  _ERROR;.  }..  d
d9e10 6f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  o{.    sqlite3_s
d9e20 6e 70 72 69 6e 74 66 28 6e 42 75 66 2d 31 37 2c  nprintf(nBuf-17,
d9e30 20 7a 42 75 66 2c 20 22 25 73 2f 22 53 51 4c 49   zBuf, "%s/"SQLI
d9e40 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45  TE_TEMP_FILE_PRE
d9e50 46 49 58 2c 20 7a 44 69 72 29 3b 0a 20 20 20 20  FIX, zDir);.    
d9e60 6a 20 3d 20 73 74 72 6c 65 6e 28 7a 42 75 66 29  j = strlen(zBuf)
d9e70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61  ;.    sqlite3_ra
d9e80 6e 64 6f 6d 6e 65 73 73 28 31 35 2c 20 26 7a 42  ndomness(15, &zB
d9e90 75 66 5b 6a 5d 29 3b 0a 20 20 20 20 66 6f 72 28  uf[j]);.    for(
d9ea0 69 3d 30 3b 20 69 3c 31 35 3b 20 69 2b 2b 2c 20  i=0; i<15; i++, 
d9eb0 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 42 75 66  j++){.      zBuf
d9ec0 5b 6a 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 61  [j] = (char)zCha
d9ed0 72 73 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 63  rs[ ((unsigned c
d9ee0 68 61 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69  har)zBuf[j])%(si
d9ef0 7a 65 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 20  zeof(zChars)-1) 
d9f00 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 75  ];.    }.    zBu
d9f10 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 7d 77 68 69  f[j] = 0;.  }whi
d9f20 6c 65 28 20 61 63 63 65 73 73 28 7a 42 75 66 2c  le( access(zBuf,
d9f30 30 29 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 72  0)==0 );.  retur
d9f40 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
d9f50 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20  ./*.** Open the 
d9f60 66 69 6c 65 20 7a 50 61 74 68 2e 0a 2a 2a 20 0a  file zPath..** .
d9f70 2a 2a 20 50 72 65 76 69 6f 75 73 6c 79 2c 20 74  ** Previously, t
d9f80 68 65 20 53 51 4c 69 74 65 20 4f 53 20 6c 61 79  he SQLite OS lay
d9f90 65 72 20 75 73 65 64 20 74 68 72 65 65 20 66 75  er used three fu
d9fa0 6e 63 74 69 6f 6e 73 20 69 6e 20 70 6c 61 63 65  nctions in place
d9fb0 20 6f 66 20 74 68 69 73 0a 2a 2a 20 6f 6e 65 3a   of this.** one:
d9fc0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  .**.**     sqlit
d9fd0 65 33 4f 73 4f 70 65 6e 52 65 61 64 57 72 69 74  e3OsOpenReadWrit
d9fe0 65 28 29 3b 0a 2a 2a 20 20 20 20 20 73 71 6c 69  e();.**     sqli
d9ff0 74 65 33 4f 73 4f 70 65 6e 52 65 61 64 4f 6e 6c  te3OsOpenReadOnl
da000 79 28 29 3b 0a 2a 2a 20 20 20 20 20 73 71 6c 69  y();.**     sqli
da010 74 65 33 4f 73 4f 70 65 6e 45 78 63 6c 75 73 69  te3OsOpenExclusi
da020 76 65 28 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 73  ve();.**.** Thes
da030 65 20 63 61 6c 6c 73 20 63 6f 72 72 65 73 70 6f  e calls correspo
da040 6e 64 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77  nd to the follow
da050 69 6e 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73  ing combinations
da060 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a   of flags:.**.**
da070 20 20 20 20 20 52 65 61 64 57 72 69 74 65 28 29       ReadWrite()
da080 20 2d 3e 20 20 20 20 20 28 52 45 41 44 57 52 49   ->     (READWRI
da090 54 45 20 7c 20 43 52 45 41 54 45 29 0a 2a 2a 20  TE | CREATE).** 
da0a0 20 20 20 20 52 65 61 64 4f 6e 6c 79 28 29 20 20      ReadOnly()  
da0b0 2d 3e 20 20 20 20 20 28 52 45 41 44 4f 4e 4c 59  ->     (READONLY
da0c0 29 20 0a 2a 2a 20 20 20 20 20 4f 70 65 6e 45 78  ) .**     OpenEx
da0d0 63 6c 75 73 69 76 65 28 29 20 2d 3e 20 28 52 45  clusive() -> (RE
da0e0 41 44 57 52 49 54 45 20 7c 20 43 52 45 41 54 45  ADWRITE | CREATE
da0f0 20 7c 20 45 58 43 4c 55 53 49 56 45 29 0a 2a 2a   | EXCLUSIVE).**
da100 0a 2a 2a 20 54 68 65 20 6f 6c 64 20 4f 70 65 6e  .** The old Open
da110 45 78 63 6c 75 73 69 76 65 28 29 20 61 63 63 65  Exclusive() acce
da120 70 74 65 64 20 61 20 62 6f 6f 6c 65 61 6e 20 61  pted a boolean a
da130 72 67 75 6d 65 6e 74 20 2d 20 22 64 65 6c 46 6c  rgument - "delFl
da140 61 67 22 2e 20 49 66 0a 2a 2a 20 74 72 75 65 2c  ag". If.** true,
da150 20 74 68 65 20 66 69 6c 65 20 77 61 73 20 63 6f   the file was co
da160 6e 66 69 67 75 72 65 64 20 74 6f 20 62 65 20 61  nfigured to be a
da170 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c  utomatically del
da180 65 74 65 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a  eted when the.**
da190 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 63 6c 6f   file handle clo
da1a0 73 65 64 2e 20 54 6f 20 61 63 68 69 65 76 65 20  sed. To achieve 
da1b0 74 68 65 20 73 61 6d 65 20 65 66 66 65 63 74 20  the same effect 
da1c0 75 73 69 6e 67 20 74 68 69 73 20 6e 65 77 20 0a  using this new .
da1d0 2a 2a 20 69 6e 74 65 72 66 61 63 65 2c 20 61 64  ** interface, ad
da1e0 64 20 74 68 65 20 44 45 4c 45 54 45 4f 4e 43 4c  d the DELETEONCL
da1f0 4f 53 45 20 66 6c 61 67 20 74 6f 20 74 68 6f 73  OSE flag to thos
da200 65 20 73 70 65 63 69 66 69 65 64 20 61 62 6f 76  e specified abov
da210 65 20 66 6f 72 20 0a 2a 2a 20 4f 70 65 6e 45 78  e for .** OpenEx
da220 63 6c 75 73 69 76 65 28 29 2e 0a 2a 2f 0a 73 74  clusive()..*/.st
da230 61 74 69 63 20 69 6e 74 20 75 6e 69 78 4f 70 65  atic int unixOpe
da240 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  n(.  sqlite3_vfs
da250 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74   *pVfs, .  const
da260 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20   char *zPath, . 
da270 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
da280 46 69 6c 65 2c 0a 20 20 69 6e 74 20 66 6c 61 67  File,.  int flag
da290 73 2c 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c  s,.  int *pOutFl
da2a0 61 67 73 0a 29 7b 0a 20 20 69 6e 74 20 66 64 20  ags.){.  int fd 
da2b0 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
da2c0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20          /* File 
da2d0 64 65 73 63 72 69 70 74 6f 72 20 72 65 74 75 72  descriptor retur
da2e0 6e 65 64 20 62 79 20 6f 70 65 6e 28 29 20 2a 2f  ned by open() */
da2f0 0a 20 20 69 6e 74 20 64 69 72 66 64 20 3d 20 2d  .  int dirfd = -
da300 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
da310 20 20 2f 2a 20 44 69 72 65 63 74 6f 72 79 20 66    /* Directory f
da320 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a  ile descriptor *
da330 2f 0a 20 20 69 6e 74 20 6f 66 6c 61 67 73 20 3d  /.  int oflags =
da340 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
da350 20 20 20 2f 2a 20 46 6c 61 67 73 20 74 6f 20 70     /* Flags to p
da360 61 73 73 20 74 6f 20 6f 70 65 6e 28 29 20 2a 2f  ass to open() */
da370 0a 20 20 69 6e 74 20 65 54 79 70 65 20 3d 20 66  .  int eType = f
da380 6c 61 67 73 26 30 78 46 46 46 46 46 46 30 30 3b  lags&0xFFFFFF00;
da390 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 66 69 6c    /* Type of fil
da3a0 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69  e to open */.  i
da3b0 6e 74 20 6e 6f 4c 6f 63 6b 3b 20 20 20 20 20 20  nt noLock;      
da3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
da3d0 20 54 72 75 65 20 74 6f 20 6f 6d 69 74 20 6c 6f   True to omit lo
da3e0 63 6b 69 6e 67 20 70 72 69 6d 69 74 69 76 65 73  cking primitives
da3f0 20 2a 2f 0a 0a 20 20 69 6e 74 20 69 73 45 78 63   */..  int isExc
da400 6c 75 73 69 76 65 20 20 3d 20 28 66 6c 61 67 73  lusive  = (flags
da410 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45   & SQLITE_OPEN_E
da420 58 43 4c 55 53 49 56 45 29 3b 0a 20 20 69 6e 74  XCLUSIVE);.  int
da430 20 69 73 44 65 6c 65 74 65 20 20 20 20 20 3d 20   isDelete     = 
da440 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  (flags & SQLITE_
da450 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f  OPEN_DELETEONCLO
da460 53 45 29 3b 0a 20 20 69 6e 74 20 69 73 43 72 65  SE);.  int isCre
da470 61 74 65 20 20 20 20 20 3d 20 28 66 6c 61 67 73  ate     = (flags
da480 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43   & SQLITE_OPEN_C
da490 52 45 41 54 45 29 3b 0a 20 20 69 6e 74 20 69 73  REATE);.  int is
da4a0 52 65 61 64 6f 6e 6c 79 20 20 20 3d 20 28 66 6c  Readonly   = (fl
da4b0 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
da4c0 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 20 20 69  N_READONLY);.  i
da4d0 6e 74 20 69 73 52 65 61 64 57 72 69 74 65 20 20  nt isReadWrite  
da4e0 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54  = (flags & SQLIT
da4f0 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
da500 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 63 72 65 61  );..  /* If crea
da510 74 69 6e 67 20 61 20 6d 61 73 74 65 72 20 6f 72  ting a master or
da520 20 6d 61 69 6e 2d 66 69 6c 65 20 6a 6f 75 72 6e   main-file journ
da530 61 6c 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  al, this functio
da540 6e 20 77 69 6c 6c 20 6f 70 65 6e 0a 20 20 2a 2a  n will open.  **
da550 20 61 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74   a file-descript
da560 6f 72 20 6f 6e 20 74 68 65 20 64 69 72 65 63 74  or on the direct
da570 6f 72 79 20 74 6f 6f 2e 20 54 68 65 20 66 69 72  ory too. The fir
da580 73 74 20 74 69 6d 65 20 75 6e 69 78 53 79 6e 63  st time unixSync
da590 28 29 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65  ().  ** is calle
da5a0 64 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  d the directory 
da5b0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
da5c0 77 69 6c 6c 20 62 65 20 66 73 79 6e 63 28 29 65  will be fsync()e
da5d0 64 20 61 6e 64 20 63 6c 6f 73 65 28 29 64 2e 0a  d and close()d..
da5e0 20 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4f 70 65    */.  int isOpe
da5f0 6e 44 69 72 65 63 74 6f 72 79 20 3d 20 28 69 73  nDirectory = (is
da600 43 72 65 61 74 65 20 26 26 20 0a 20 20 20 20 20  Create && .     
da610 20 28 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f   (eType==SQLITE_
da620 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52  OPEN_MASTER_JOUR
da630 4e 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51  NAL || eType==SQ
da640 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a  LITE_OPEN_MAIN_J
da650 4f 55 52 4e 41 4c 29 0a 20 20 29 3b 0a 0a 20 20  OURNAL).  );..  
da660 2f 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 7a  /* If argument z
da670 50 61 74 68 20 69 73 20 61 20 4e 55 4c 4c 20 70  Path is a NULL p
da680 6f 69 6e 74 65 72 2c 20 74 68 69 73 20 66 75 6e  ointer, this fun
da690 63 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65  ction is require
da6a0 64 20 74 6f 20 6f 70 65 6e 0a 20 20 2a 2a 20 61  d to open.  ** a
da6b0 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e   temporary file.
da6c0 20 55 73 65 20 74 68 69 73 20 62 75 66 66 65 72   Use this buffer
da6d0 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 66 69   to store the fi
da6e0 6c 65 20 6e 61 6d 65 20 69 6e 2e 0a 20 20 2a 2f  le name in..  */
da6f0 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65  .  char zTmpname
da700 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d  [MAX_PATHNAME+1]
da710 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
da720 7a 4e 61 6d 65 20 3d 20 7a 50 61 74 68 3b 0a 0a  zName = zPath;..
da730 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 66    /* Check the f
da740 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65  ollowing stateme
da750 6e 74 73 20 61 72 65 20 74 72 75 65 3a 20 0a 20  nts are true: . 
da760 20 2a 2a 0a 20 20 2a 2a 20 20 20 28 61 29 20 45   **.  **   (a) E
da770 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68  xactly one of th
da780 65 20 52 45 41 44 57 52 49 54 45 20 61 6e 64 20  e READWRITE and 
da790 52 45 41 44 4f 4e 4c 59 20 66 6c 61 67 73 20 6d  READONLY flags m
da7a0 75 73 74 20 62 65 20 73 65 74 2c 20 61 6e 64 20  ust be set, and 
da7b0 0a 20 20 2a 2a 20 20 20 28 62 29 20 69 66 20 43  .  **   (b) if C
da7c0 52 45 41 54 45 20 69 73 20 73 65 74 2c 20 74 68  REATE is set, th
da7d0 65 6e 20 52 45 41 44 57 52 49 54 45 20 6d 75 73  en READWRITE mus
da7e0 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2c 20 61  t also be set, a
da7f0 6e 64 0a 20 20 2a 2a 20 20 20 28 63 29 20 69 66  nd.  **   (c) if
da800 20 45 58 43 4c 55 53 49 56 45 20 69 73 20 73 65   EXCLUSIVE is se
da810 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45 20 6d  t, then CREATE m
da820 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2e  ust also be set.
da830 0a 20 20 2a 2a 20 20 20 28 64 29 20 69 66 20 44  .  **   (d) if D
da840 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 69 73 20  ELETEONCLOSE is 
da850 73 65 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45  set, then CREATE
da860 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65   must also be se
da870 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  t..  */.  assert
da880 28 28 69 73 52 65 61 64 6f 6e 6c 79 3d 3d 30 20  ((isReadonly==0 
da890 7c 7c 20 69 73 52 65 61 64 57 72 69 74 65 3d 3d  || isReadWrite==
da8a0 30 29 20 26 26 20 28 69 73 52 65 61 64 57 72 69  0) && (isReadWri
da8b0 74 65 20 7c 7c 20 69 73 52 65 61 64 6f 6e 6c 79  te || isReadonly
da8c0 29 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 43  ));.  assert(isC
da8d0 72 65 61 74 65 3d 3d 30 20 7c 7c 20 69 73 52 65  reate==0 || isRe
da8e0 61 64 57 72 69 74 65 29 3b 0a 20 20 61 73 73 65  adWrite);.  asse
da8f0 72 74 28 69 73 45 78 63 6c 75 73 69 76 65 3d 3d  rt(isExclusive==
da900 30 20 7c 7c 20 69 73 43 72 65 61 74 65 29 3b 0a  0 || isCreate);.
da910 20 20 61 73 73 65 72 74 28 69 73 44 65 6c 65 74    assert(isDelet
da920 65 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61 74 65  e==0 || isCreate
da930 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 61 69  );..  /* The mai
da940 6e 20 44 42 2c 20 6d 61 69 6e 20 6a 6f 75 72 6e  n DB, main journ
da950 61 6c 2c 20 61 6e 64 20 6d 61 73 74 65 72 20 6a  al, and master j
da960 6f 75 72 6e 61 6c 20 61 72 65 20 6e 65 76 65 72  ournal are never
da970 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 20   automatically. 
da980 20 2a 2a 20 64 65 6c 65 74 65 64 0a 20 20 2a 2f   ** deleted.  */
da990 0a 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65  .  assert( eType
da9a0 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  !=SQLITE_OPEN_MA
da9b0 49 4e 5f 44 42 20 7c 7c 20 21 69 73 44 65 6c 65  IN_DB || !isDele
da9c0 74 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  te );.  assert( 
da9d0 65 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50  eType!=SQLITE_OP
da9e0 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20  EN_MAIN_JOURNAL 
da9f0 7c 7c 20 21 69 73 44 65 6c 65 74 65 20 29 3b 0a  || !isDelete );.
daa00 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21    assert( eType!
daa10 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53  =SQLITE_OPEN_MAS
daa20 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 21  TER_JOURNAL || !
daa30 69 73 44 65 6c 65 74 65 20 29 3b 0a 0a 20 20 2f  isDelete );..  /
daa40 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68  * Assert that th
daa50 65 20 75 70 70 65 72 20 6c 61 79 65 72 20 68 61  e upper layer ha
daa60 73 20 73 65 74 20 6f 6e 65 20 6f 66 20 74 68 65  s set one of the
daa70 20 22 66 69 6c 65 2d 74 79 70 65 22 20 66 6c 61   "file-type" fla
daa80 67 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  gs. */.  assert(
daa90 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f   eType==SQLITE_O
daaa0 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 20 20 20 20  PEN_MAIN_DB     
daab0 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54   || eType==SQLIT
daac0 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 20 0a  E_OPEN_TEMP_DB .
daad0 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d         || eType=
daae0 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49  =SQLITE_OPEN_MAI
daaf0 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 65 54 79  N_JOURNAL || eTy
dab00 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  pe==SQLITE_OPEN_
dab10 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 0a 20 20  TEMP_JOURNAL .  
dab20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53       || eType==S
dab30 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f  QLITE_OPEN_SUBJO
dab40 55 52 4e 41 4c 20 20 20 7c 7c 20 65 54 79 70 65  URNAL   || eType
dab50 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  ==SQLITE_OPEN_MA
dab60 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 0a 20 20  STER_JOURNAL .  
dab70 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53       || eType==S
dab80 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53  QLITE_OPEN_TRANS
dab90 49 45 4e 54 5f 44 42 0a 20 20 29 3b 0a 0a 20 20  IENT_DB.  );..  
daba0 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 2c  memset(pFile, 0,
dabb0 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65   sizeof(unixFile
dabc0 29 29 3b 0a 0a 20 20 69 66 28 20 21 7a 4e 61 6d  ));..  if( !zNam
dabd0 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b  e ){.    int rc;
dabe0 0a 20 20 20 20 61 73 73 65 72 74 28 69 73 44 65  .    assert(isDe
dabf0 6c 65 74 65 20 26 26 20 21 69 73 4f 70 65 6e 44  lete && !isOpenD
dac00 69 72 65 63 74 6f 72 79 29 3b 0a 20 20 20 20 72  irectory);.    r
dac10 63 20 3d 20 67 65 74 54 65 6d 70 6e 61 6d 65 28  c = getTempname(
dac20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 2c 20  MAX_PATHNAME+1, 
dac30 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 20 69  zTmpname);.    i
dac40 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
dac50 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
dac60 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a   rc;.    }.    z
dac70 4e 61 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b  Name = zTmpname;
dac80 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 73 52 65  .  }..  if( isRe
dac90 61 64 6f 6e 6c 79 20 29 20 20 6f 66 6c 61 67 73  adonly )  oflags
daca0 20 7c 3d 20 4f 5f 52 44 4f 4e 4c 59 3b 0a 20 20   |= O_RDONLY;.  
dacb0 69 66 28 20 69 73 52 65 61 64 57 72 69 74 65 20  if( isReadWrite 
dacc0 29 20 6f 66 6c 61 67 73 20 7c 3d 20 4f 5f 52 44  ) oflags |= O_RD
dacd0 57 52 3b 0a 20 20 69 66 28 20 69 73 43 72 65 61  WR;.  if( isCrea
dace0 74 65 20 29 20 20 20 20 6f 66 6c 61 67 73 20 7c  te )    oflags |
dacf0 3d 20 4f 5f 43 52 45 41 54 3b 0a 20 20 69 66 28  = O_CREAT;.  if(
dad00 20 69 73 45 78 63 6c 75 73 69 76 65 20 29 20 6f   isExclusive ) o
dad10 66 6c 61 67 73 20 7c 3d 20 28 4f 5f 45 58 43 4c  flags |= (O_EXCL
dad20 7c 4f 5f 4e 4f 46 4f 4c 4c 4f 57 29 3b 0a 20 20  |O_NOFOLLOW);.  
dad30 6f 66 6c 61 67 73 20 7c 3d 20 28 4f 5f 4c 41 52  oflags |= (O_LAR
dad40 47 45 46 49 4c 45 7c 4f 5f 42 49 4e 41 52 59 29  GEFILE|O_BINARY)
dad50 3b 0a 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 7a  ;..  fd = open(z
dad60 4e 61 6d 65 2c 20 6f 66 6c 61 67 73 2c 20 69 73  Name, oflags, is
dad70 44 65 6c 65 74 65 3f 30 36 30 30 3a 53 51 4c 49  Delete?0600:SQLI
dad80 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f  TE_DEFAULT_FILE_
dad90 50 45 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20  PERMISSIONS);.  
dada0 69 66 28 20 66 64 3c 30 20 26 26 20 65 72 72 6e  if( fd<0 && errn
dadb0 6f 21 3d 45 49 53 44 49 52 20 26 26 20 69 73 52  o!=EISDIR && isR
dadc0 65 61 64 57 72 69 74 65 20 26 26 20 21 69 73 45  eadWrite && !isE
dadd0 78 63 6c 75 73 69 76 65 20 29 7b 0a 20 20 20 20  xclusive ){.    
dade0 2f 2a 20 46 61 69 6c 65 64 20 74 6f 20 6f 70 65  /* Failed to ope
dadf0 6e 20 74 68 65 20 66 69 6c 65 20 66 6f 72 20 72  n the file for r
dae00 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73  ead/write access
dae10 2e 20 54 72 79 20 72 65 61 64 2d 6f 6e 6c 79 2e  . Try read-only.
dae20 20 2a 2f 0a 20 20 20 20 66 6c 61 67 73 20 26 3d   */.    flags &=
dae30 20 7e 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52   ~(SQLITE_OPEN_R
dae40 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f  EADWRITE|SQLITE_
dae50 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a 20 20  OPEN_CREATE);.  
dae60 20 20 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54    flags |= SQLIT
dae70 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b  E_OPEN_READONLY;
dae80 0a 20 20 20 20 72 65 74 75 72 6e 20 75 6e 69 78  .    return unix
dae90 4f 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68  Open(pVfs, zPath
daea0 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20  , pFile, flags, 
daeb0 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 7d 0a  pOutFlags);.  }.
daec0 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20    if( fd<0 ){.  
daed0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
daee0 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a 20 20  CANTOPEN;.  }.  
daef0 69 66 28 20 69 73 44 65 6c 65 74 65 20 29 7b 0a  if( isDelete ){.
daf00 20 20 20 20 75 6e 6c 69 6e 6b 28 7a 4e 61 6d 65      unlink(zName
daf10 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 75  );.  }.  if( pOu
daf20 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 2a 70  tFlags ){.    *p
daf30 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c 61 67 73  OutFlags = flags
daf40 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  ;.  }..  assert(
daf50 66 64 21 3d 30 29 3b 0a 20 20 69 66 28 20 69 73  fd!=0);.  if( is
daf60 4f 70 65 6e 44 69 72 65 63 74 6f 72 79 20 29 7b  OpenDirectory ){
daf70 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 6f 70  .    int rc = op
daf80 65 6e 44 69 72 65 63 74 6f 72 79 28 7a 50 61 74  enDirectory(zPat
daf90 68 2c 20 26 64 69 72 66 64 29 3b 0a 20 20 20 20  h, &dirfd);.    
dafa0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
dafb0 4b 20 29 7b 0a 20 20 20 20 20 20 63 6c 6f 73 65  K ){.      close
dafc0 28 66 64 29 3b 0a 20 20 20 20 20 20 72 65 74 75  (fd);.      retu
dafd0 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d  rn rc;.    }.  }
dafe0 0a 0a 23 69 66 64 65 66 20 46 44 5f 43 4c 4f 45  ..#ifdef FD_CLOE
daff0 58 45 43 0a 20 20 66 63 6e 74 6c 28 66 64 2c 20  XEC.  fcntl(fd, 
db000 46 5f 53 45 54 46 44 2c 20 66 63 6e 74 6c 28 66  F_SETFD, fcntl(f
db010 64 2c 20 46 5f 47 45 54 46 44 2c 20 30 29 20 7c  d, F_GETFD, 0) |
db020 20 46 44 5f 43 4c 4f 45 58 45 43 29 3b 0a 23 65   FD_CLOEXEC);.#e
db030 6e 64 69 66 0a 0a 20 20 6e 6f 4c 6f 63 6b 20 3d  ndif..  noLock =
db040 20 65 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f   eType!=SQLITE_O
db050 50 45 4e 5f 4d 41 49 4e 5f 44 42 3b 0a 20 20 72  PEN_MAIN_DB;.  r
db060 65 74 75 72 6e 20 66 69 6c 6c 49 6e 55 6e 69 78  eturn fillInUnix
db070 46 69 6c 65 28 70 56 66 73 2c 20 66 64 2c 20 64  File(pVfs, fd, d
db080 69 72 66 64 2c 20 70 46 69 6c 65 2c 20 7a 50 61  irfd, pFile, zPa
db090 74 68 2c 20 6e 6f 4c 6f 63 6b 29 3b 0a 7d 0a 0a  th, noLock);.}..
db0a0 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65  /*.** Delete the
db0b0 20 66 69 6c 65 20 61 74 20 7a 50 61 74 68 2e 20   file at zPath. 
db0c0 49 66 20 74 68 65 20 64 69 72 53 79 6e 63 20 61  If the dirSync a
db0d0 72 67 75 6d 65 6e 74 20 69 73 20 74 72 75 65 2c  rgument is true,
db0e0 20 66 73 79 6e 63 28 29 0a 2a 2a 20 74 68 65 20   fsync().** the 
db0f0 64 69 72 65 63 74 6f 72 79 20 61 66 74 65 72 20  directory after 
db100 64 65 6c 65 74 69 6e 67 20 74 68 65 20 66 69 6c  deleting the fil
db110 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
db120 20 75 6e 69 78 44 65 6c 65 74 65 28 73 71 6c 69   unixDelete(sqli
db130 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63  te3_vfs *pVfs, c
db140 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68  onst char *zPath
db150 2c 20 69 6e 74 20 64 69 72 53 79 6e 63 29 7b 0a  , int dirSync){.
db160 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
db170 45 5f 4f 4b 3b 0a 20 20 53 69 6d 75 6c 61 74 65  E_OK;.  Simulate
db180 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53  IOError(return S
db190 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45  QLITE_IOERR_DELE
db1a0 54 45 29 3b 0a 20 20 75 6e 6c 69 6e 6b 28 7a 50  TE);.  unlink(zP
db1b0 61 74 68 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  ath);.#ifndef SQ
db1c0 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 44 49 52  LITE_DISABLE_DIR
db1d0 53 59 4e 43 0a 20 20 69 66 28 20 64 69 72 53 79  SYNC.  if( dirSy
db1e0 6e 63 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 64  nc ){.    int fd
db1f0 3b 0a 20 20 20 20 72 63 20 3d 20 6f 70 65 6e 44  ;.    rc = openD
db200 69 72 65 63 74 6f 72 79 28 7a 50 61 74 68 2c 20  irectory(zPath, 
db210 26 66 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63  &fd);.    if( rc
db220 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
db230 20 20 20 20 20 69 66 28 20 66 73 79 6e 63 28 66       if( fsync(f
db240 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  d) ){.        rc
db250 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
db260 44 49 52 5f 46 53 59 4e 43 3b 0a 20 20 20 20 20  DIR_FSYNC;.     
db270 20 7d 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 66   }.      close(f
db280 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  d);.    }.  }.#e
db290 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63  ndif.  return rc
db2a0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20  ;.}../*.** Test 
db2b0 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66  the existance of
db2c0 20 6f 72 20 61 63 63 65 73 73 20 70 65 72 6d 69   or access permi
db2d0 73 73 69 6f 6e 73 20 6f 66 20 66 69 6c 65 20 7a  ssions of file z
db2e0 50 61 74 68 2e 20 54 68 65 0a 2a 2a 20 74 65 73  Path. The.** tes
db2f0 74 20 70 65 72 66 6f 72 6d 65 64 20 64 65 70 65  t performed depe
db300 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 65  nds on the value
db310 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a   of flags:.**.**
db320 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43 43 45       SQLITE_ACCE
db330 53 53 5f 45 58 49 53 54 53 3a 20 52 65 74 75 72  SS_EXISTS: Retur
db340 6e 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 20  n 1 if the file 
db350 65 78 69 73 74 73 0a 2a 2a 20 20 20 20 20 53 51  exists.**     SQ
db360 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44  LITE_ACCESS_READ
db370 57 52 49 54 45 3a 20 52 65 74 75 72 6e 20 31 20  WRITE: Return 1 
db380 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 72  if the file is r
db390 65 61 64 20 61 6e 64 20 77 72 69 74 61 62 6c 65  ead and writable
db3a0 2e 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f  ..**     SQLITE_
db3b0 41 43 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59 3a  ACCESS_READONLY:
db3c0 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65   Return 1 if the
db3d0 20 66 69 6c 65 20 69 73 20 72 65 61 64 61 62 6c   file is readabl
db3e0 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69  e..**.** Otherwi
db3f0 73 65 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a  se return 0..*/.
db400 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 41  static int unixA
db410 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33  ccess(.  sqlite3
db420 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63  _vfs *pVfs, .  c
db430 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68  onst char *zPath
db440 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20  , .  int flags, 
db450 0a 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 0a  .  int *pResOut.
db460 29 7b 0a 20 20 69 6e 74 20 61 6d 6f 64 65 20 3d  ){.  int amode =
db470 20 30 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f   0;.  SimulateIO
db480 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
db490 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53  LITE_IOERR_ACCES
db4a0 53 3b 20 29 3b 0a 20 20 73 77 69 74 63 68 28 20  S; );.  switch( 
db4b0 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61 73  flags ){.    cas
db4c0 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  e SQLITE_ACCESS_
db4d0 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 61 6d  EXISTS:.      am
db4e0 6f 64 65 20 3d 20 46 5f 4f 4b 3b 0a 20 20 20 20  ode = F_OK;.    
db4f0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
db500 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  e SQLITE_ACCESS_
db510 52 45 41 44 57 52 49 54 45 3a 0a 20 20 20 20 20  READWRITE:.     
db520 20 61 6d 6f 64 65 20 3d 20 57 5f 4f 4b 7c 52 5f   amode = W_OK|R_
db530 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  OK;.      break;
db540 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
db550 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 20  _ACCESS_READ:.  
db560 20 20 20 20 61 6d 6f 64 65 20 3d 20 52 5f 4f 4b      amode = R_OK
db570 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a  ;.      break;..
db580 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20      default:.   
db590 20 20 20 61 73 73 65 72 74 28 21 22 49 6e 76 61     assert(!"Inva
db5a0 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d 65  lid flags argume
db5b0 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65  nt");.  }.  *pRe
db5c0 73 4f 75 74 20 3d 20 28 61 63 63 65 73 73 28 7a  sOut = (access(z
db5d0 50 61 74 68 2c 20 61 6d 6f 64 65 29 3d 3d 30 29  Path, amode)==0)
db5e0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
db5f0 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  E_OK;.}.../*.** 
db600 54 75 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20  Turn a relative 
db610 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20  pathname into a 
db620 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 54  full pathname. T
db630 68 65 20 72 65 6c 61 74 69 76 65 20 70 61 74 68  he relative path
db640 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 20 61 73  .** is stored as
db650 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65   a nul-terminate
db660 64 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20  d string in the 
db670 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74  buffer pointed t
db680 6f 20 62 79 0a 2a 2a 20 7a 50 61 74 68 2e 20 0a  o by.** zPath. .
db690 2a 2a 0a 2a 2a 20 7a 4f 75 74 20 70 6f 69 6e 74  **.** zOut point
db6a0 73 20 74 6f 20 61 20 62 75 66 66 65 72 20 6f 66  s to a buffer of
db6b0 20 61 74 20 6c 65 61 73 74 20 73 71 6c 69 74 65   at least sqlite
db6c0 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d 65  3_vfs.mxPathname
db6d0 20 62 79 74 65 73 20 0a 2a 2a 20 28 69 6e 20 74   bytes .** (in t
db6e0 68 69 73 20 63 61 73 65 2c 20 4d 41 58 5f 50 41  his case, MAX_PA
db6f0 54 48 4e 41 4d 45 20 62 79 74 65 73 29 2e 20 54  THNAME bytes). T
db700 68 65 20 66 75 6c 6c 2d 70 61 74 68 20 69 73 20  he full-path is 
db710 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 74 68  written to.** th
db720 69 73 20 62 75 66 66 65 72 20 62 65 66 6f 72 65  is buffer before
db730 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73   returning..*/.s
db740 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 75  tatic int unixFu
db750 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71  llPathname(.  sq
db760 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
db770 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
db780 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62  ointer to vfs ob
db790 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ject */.  const 
db7a0 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20  char *zPath,    
db7b0 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69          /* Possi
db7c0 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e 70  bly relative inp
db7d0 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74  ut path */.  int
db7e0 20 6e 4f 75 74 2c 20 20 20 20 20 20 20 20 20 20   nOut,          
db7f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
db800 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66  ze of output buf
db810 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  fer in bytes */.
db820 20 20 63 68 61 72 20 2a 7a 4f 75 74 20 20 20 20    char *zOut    
db830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
db840 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65 72  /* Output buffer
db850 20 2a 2f 0a 29 7b 0a 0a 20 20 2f 2a 20 49 74 27   */.){..  /* It'
db860 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61 74  s odd to simulat
db870 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68 65  e an io-error he
db880 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 20 74  re, but really t
db890 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20 2a 2a  his is just.  **
db8a0 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d 65 72   using the io-er
db8b0 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63 74 75  ror infrastructu
db8c0 72 65 20 74 6f 20 74 65 73 74 20 74 68 61 74 20  re to test that 
db8d0 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 74  SQLite handles t
db8e0 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f  his.  ** functio
db8f0 6e 20 66 61 69 6c 69 6e 67 2e 20 54 68 69 73 20  n failing. This 
db900 66 75 6e 63 74 69 6f 6e 20 63 6f 75 6c 64 20 66  function could f
db910 61 69 6c 20 69 66 2c 20 66 6f 72 20 65 78 61 6d  ail if, for exam
db920 70 6c 65 2c 20 74 68 65 0a 20 20 2a 2a 20 63 75  ple, the.  ** cu
db930 72 72 65 6e 74 20 77 6f 72 6b 69 6e 67 20 64 69  rrent working di
db940 72 65 63 74 6c 79 20 68 61 73 20 62 65 65 6e 20  rectly has been 
db950 75 6e 6c 69 6e 6b 65 64 2e 0a 20 20 2a 2f 0a 20  unlinked..  */. 
db960 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
db970 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ( return SQLITE_
db980 45 52 52 4f 52 20 29 3b 0a 0a 20 20 61 73 73 65  ERROR );..  asse
db990 72 74 28 20 70 56 66 73 2d 3e 6d 78 50 61 74 68  rt( pVfs->mxPath
db9a0 6e 61 6d 65 3d 3d 4d 41 58 5f 50 41 54 48 4e 41  name==MAX_PATHNA
db9b0 4d 45 20 29 3b 0a 20 20 7a 4f 75 74 5b 6e 4f 75  ME );.  zOut[nOu
db9c0 74 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69  t-1] = '\0';.  i
db9d0 66 28 20 7a 50 61 74 68 5b 30 5d 3d 3d 27 2f 27  f( zPath[0]=='/'
db9e0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
db9f0 73 6e 70 72 69 6e 74 66 28 6e 4f 75 74 2c 20 7a  snprintf(nOut, z
dba00 4f 75 74 2c 20 22 25 73 22 2c 20 7a 50 61 74 68  Out, "%s", zPath
dba10 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
dba20 69 6e 74 20 6e 43 77 64 3b 0a 20 20 20 20 69 66  int nCwd;.    if
dba30 28 20 67 65 74 63 77 64 28 7a 4f 75 74 2c 20 6e  ( getcwd(zOut, n
dba40 4f 75 74 2d 31 29 3d 3d 30 20 29 7b 0a 20 20 20  Out-1)==0 ){.   
dba50 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
dba60 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d  _CANTOPEN;.    }
dba70 0a 20 20 20 20 6e 43 77 64 20 3d 20 73 74 72 6c  .    nCwd = strl
dba80 65 6e 28 7a 4f 75 74 29 3b 0a 20 20 20 20 73 71  en(zOut);.    sq
dba90 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e  lite3_snprintf(n
dbaa0 4f 75 74 2d 6e 43 77 64 2c 20 26 7a 4f 75 74 5b  Out-nCwd, &zOut[
dbab0 6e 43 77 64 5d 2c 20 22 2f 25 73 22 2c 20 7a 50  nCwd], "/%s", zP
dbac0 61 74 68 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ath);.  }.  retu
dbad0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 23  rn SQLITE_OK;..#
dbae0 69 66 20 30 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52  if 0.  /*.  ** R
dbaf0 65 6d 6f 76 65 20 22 2f 2e 2f 22 20 70 61 74 68  emove "/./" path
dbb00 20 65 6c 65 6d 65 6e 74 73 20 61 6e 64 20 63 6f   elements and co
dbb10 6e 76 65 72 74 20 22 2f 41 2f 2e 2f 22 20 70 61  nvert "/A/./" pa
dbb20 74 68 20 65 6c 65 6d 65 6e 74 73 0a 20 20 2a 2a  th elements.  **
dbb30 20 74 6f 20 6a 75 73 74 20 22 2f 22 2e 0a 20 20   to just "/"..  
dbb40 2a 2f 0a 20 20 69 66 28 20 7a 46 75 6c 6c 20 29  */.  if( zFull )
dbb50 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a  {.    int i, j;.
dbb60 20 20 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 7a      for(i=j=0; z
dbb70 46 75 6c 6c 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20  Full[i]; i++){. 
dbb80 20 20 20 20 20 69 66 28 20 7a 46 75 6c 6c 5b 69       if( zFull[i
dbb90 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20  ]=='/' ){.      
dbba0 20 20 69 66 28 20 7a 46 75 6c 6c 5b 69 2b 31 5d    if( zFull[i+1]
dbbb0 3d 3d 27 2f 27 20 29 20 63 6f 6e 74 69 6e 75 65  =='/' ) continue
dbbc0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 46  ;.        if( zF
dbbd0 75 6c 6c 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 26  ull[i+1]=='.' &&
dbbe0 20 7a 46 75 6c 6c 5b 69 2b 32 5d 3d 3d 27 2f 27   zFull[i+2]=='/'
dbbf0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 20   ){.          i 
dbc00 2b 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20  += 1;.          
dbc10 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
dbc20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
dbc30 7a 46 75 6c 6c 5b 69 2b 31 5d 3d 3d 27 2e 27 20  zFull[i+1]=='.' 
dbc40 26 26 20 7a 46 75 6c 6c 5b 69 2b 32 5d 3d 3d 27  && zFull[i+2]=='
dbc50 2e 27 20 26 26 20 7a 46 75 6c 6c 5b 69 2b 33 5d  .' && zFull[i+3]
dbc60 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 20  =='/' ){.       
dbc70 20 20 20 77 68 69 6c 65 28 20 6a 3e 30 20 26 26     while( j>0 &&
dbc80 20 7a 46 75 6c 6c 5b 6a 2d 31 5d 21 3d 27 2f 27   zFull[j-1]!='/'
dbc90 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20 20   ){ j--; }.     
dbca0 20 20 20 20 20 69 20 2b 3d 20 33 3b 0a 20 20 20       i += 3;.   
dbcb0 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
dbcc0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
dbcd0 20 7d 0a 20 20 20 20 20 20 7a 46 75 6c 6c 5b 6a   }.      zFull[j
dbce0 2b 2b 5d 20 3d 20 7a 46 75 6c 6c 5b 69 5d 3b 0a  ++] = zFull[i];.
dbcf0 20 20 20 20 7d 0a 20 20 20 20 7a 46 75 6c 6c 5b      }.    zFull[
dbd00 6a 5d 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e 64  j] = 0;.  }.#end
dbd10 69 66 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53  if.}...#ifndef S
dbd20 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f  QLITE_OMIT_LOAD_
dbd30 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20  EXTENSION./*.** 
dbd40 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 6f  Interfaces for o
dbd50 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64 20  pening a shared 
dbd60 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e 67  library, finding
dbd70 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a   entry points.**
dbd80 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 72   within the shar
dbd90 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20  ed library, and 
dbda0 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72  closing the shar
dbdb0 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23  ed library..*/.#
dbdc0 69 6e 63 6c 75 64 65 20 3c 64 6c 66 63 6e 2e 68  include <dlfcn.h
dbdd0 3e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 75  >.static void *u
dbde0 6e 69 78 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65  nixDlOpen(sqlite
dbdf0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e  3_vfs *pVfs, con
dbe00 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
dbe10 6d 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 6c  me){.  return dl
dbe20 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20  open(zFilename, 
dbe30 52 54 4c 44 5f 4e 4f 57 20 7c 20 52 54 4c 44 5f  RTLD_NOW | RTLD_
dbe40 47 4c 4f 42 41 4c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  GLOBAL);.}../*.*
dbe50 2a 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74  * SQLite calls t
dbe60 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d 6d  his function imm
dbe70 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 20 61  ediately after a
dbe80 20 63 61 6c 6c 20 74 6f 20 75 6e 69 78 44 6c 53   call to unixDlS
dbe90 79 6d 28 29 20 6f 72 0a 2a 2a 20 75 6e 69 78 44  ym() or.** unixD
dbea0 6c 4f 70 65 6e 28 29 20 66 61 69 6c 73 20 28 72  lOpen() fails (r
dbeb0 65 74 75 72 6e 73 20 61 20 6e 75 6c 6c 20 70 6f  eturns a null po
dbec0 69 6e 74 65 72 29 2e 20 49 66 20 61 20 6d 6f 72  inter). If a mor
dbed0 65 20 64 65 74 61 69 6c 65 64 20 65 72 72 6f 72  e detailed error
dbee0 0a 2a 2a 20 6d 65 73 73 61 67 65 20 69 73 20 61  .** message is a
dbef0 76 61 69 6c 61 62 6c 65 2c 20 69 74 20 69 73 20  vailable, it is 
dbf00 77 72 69 74 74 65 6e 20 74 6f 20 7a 42 75 66 4f  written to zBufO
dbf10 75 74 2e 20 49 66 20 6e 6f 20 65 72 72 6f 72 20  ut. If no error 
dbf20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 61 76  message.** is av
dbf30 61 69 6c 61 62 6c 65 2c 20 7a 42 75 66 4f 75 74  ailable, zBufOut
dbf40 20 69 73 20 6c 65 66 74 20 75 6e 6d 6f 64 69 66   is left unmodif
dbf50 69 65 64 20 61 6e 64 20 53 51 4c 69 74 65 20 75  ied and SQLite u
dbf60 73 65 73 20 61 20 64 65 66 61 75 6c 74 0a 2a 2a  ses a default.**
dbf70 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a   error message..
dbf80 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75  */.static void u
dbf90 6e 69 78 44 6c 45 72 72 6f 72 28 73 71 6c 69 74  nixDlError(sqlit
dbfa0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e  e3_vfs *pVfs, in
dbfb0 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42  t nBuf, char *zB
dbfc0 75 66 4f 75 74 29 7b 0a 20 20 63 68 61 72 20 2a  ufOut){.  char *
dbfd0 7a 45 72 72 3b 0a 20 20 65 6e 74 65 72 4d 75 74  zErr;.  enterMut
dbfe0 65 78 28 29 3b 0a 20 20 7a 45 72 72 20 3d 20 64  ex();.  zErr = d
dbff0 6c 65 72 72 6f 72 28 29 3b 0a 20 20 69 66 28 20  lerror();.  if( 
dc000 7a 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69  zErr ){.    sqli
dc010 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75  te3_snprintf(nBu
dc020 66 2c 20 7a 42 75 66 4f 75 74 2c 20 22 25 73 22  f, zBufOut, "%s"
dc030 2c 20 7a 45 72 72 29 3b 0a 20 20 7d 0a 20 20 6c  , zErr);.  }.  l
dc040 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 73  eaveMutex();.}.s
dc050 74 61 74 69 63 20 76 6f 69 64 20 2a 75 6e 69 78  tatic void *unix
dc060 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66  DlSym(sqlite3_vf
dc070 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70  s *pVfs, void *p
dc080 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68  Handle, const ch
dc090 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 7b 0a 20 20  ar *zSymbol){.  
dc0a0 72 65 74 75 72 6e 20 64 6c 73 79 6d 28 70 48 61  return dlsym(pHa
dc0b0 6e 64 6c 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a  ndle, zSymbol);.
dc0c0 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e  }.static void un
dc0d0 69 78 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65  ixDlClose(sqlite
dc0e0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69  3_vfs *pVfs, voi
dc0f0 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 64  d *pHandle){.  d
dc100 6c 63 6c 6f 73 65 28 70 48 61 6e 64 6c 65 29 3b  lclose(pHandle);
dc110 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53  .}.#else /* if S
dc120 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f  QLITE_OMIT_LOAD_
dc130 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66  EXTENSION is def
dc140 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69  ined: */.  #defi
dc150 6e 65 20 75 6e 69 78 44 6c 4f 70 65 6e 20 20 30  ne unixDlOpen  0
dc160 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44  .  #define unixD
dc170 6c 45 72 72 6f 72 20 30 0a 20 20 23 64 65 66 69  lError 0.  #defi
dc180 6e 65 20 75 6e 69 78 44 6c 53 79 6d 20 20 20 30  ne unixDlSym   0
dc190 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44  .  #define unixD
dc1a0 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64 69 66 0a  lClose 0.#endif.
dc1b0 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6e 42 75  ./*.** Write nBu
dc1c0 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f  f bytes of rando
dc1d0 6d 20 64 61 74 61 20 74 6f 20 74 68 65 20 73 75  m data to the su
dc1e0 70 70 6c 69 65 64 20 62 75 66 66 65 72 20 7a 42  pplied buffer zB
dc1f0 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  uf..*/.static in
dc200 74 20 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73 73  t unixRandomness
dc210 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
dc220 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68  fs, int nBuf, ch
dc230 61 72 20 2a 7a 42 75 66 29 7b 0a 0a 20 20 61 73  ar *zBuf){..  as
dc240 73 65 72 74 28 6e 42 75 66 3e 3d 28 73 69 7a 65  sert(nBuf>=(size
dc250 6f 66 28 74 69 6d 65 5f 74 29 2b 73 69 7a 65 6f  of(time_t)+sizeo
dc260 66 28 69 6e 74 29 29 29 3b 0a 0a 20 20 2f 2a 20  f(int)));..  /* 
dc270 57 65 20 68 61 76 65 20 74 6f 20 69 6e 69 74 69  We have to initi
dc280 61 6c 69 7a 65 20 7a 42 75 66 20 74 6f 20 70 72  alize zBuf to pr
dc290 65 76 65 6e 74 20 76 61 6c 67 72 69 6e 64 20 66  event valgrind f
dc2a0 72 6f 6d 20 72 65 70 6f 72 74 69 6e 67 0a 20 20  rom reporting.  
dc2b0 2a 2a 20 65 72 72 6f 72 73 2e 20 20 54 68 65 20  ** errors.  The 
dc2c0 72 65 70 6f 72 74 73 20 69 73 73 75 65 64 20 62  reports issued b
dc2d0 79 20 76 61 6c 67 72 69 6e 64 20 61 72 65 20 69  y valgrind are i
dc2e0 6e 63 6f 72 72 65 63 74 20 2d 20 77 65 20 77 6f  ncorrect - we wo
dc2f0 75 6c 64 0a 20 20 2a 2a 20 70 72 65 66 65 72 20  uld.  ** prefer 
dc300 74 68 61 74 20 74 68 65 20 72 61 6e 64 6f 6d 6e  that the randomn
dc310 65 73 73 20 62 65 20 69 6e 63 72 65 61 73 65 64  ess be increased
dc320 20 62 79 20 6d 61 6b 69 6e 67 20 75 73 65 20 6f   by making use o
dc330 66 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 6e 69  f the.  ** unini
dc340 74 69 61 6c 69 7a 65 64 20 73 70 61 63 65 20 69  tialized space i
dc350 6e 20 7a 42 75 66 20 2d 20 62 75 74 20 76 61 6c  n zBuf - but val
dc360 67 72 69 6e 64 20 65 72 72 6f 72 73 20 74 65 6e  grind errors ten
dc370 64 20 74 6f 20 77 6f 72 72 79 0a 20 20 2a 2a 20  d to worry.  ** 
dc380 73 6f 6d 65 20 75 73 65 72 73 2e 20 20 52 61 74  some users.  Rat
dc390 68 65 72 20 74 68 61 6e 20 61 72 67 75 65 2c 20  her than argue, 
dc3a0 69 74 20 73 65 65 6d 73 20 65 61 73 69 65 72 20  it seems easier 
dc3b0 6a 75 73 74 20 74 6f 20 69 6e 69 74 69 61 6c 69  just to initiali
dc3c0 7a 65 0a 20 20 2a 2a 20 74 68 65 20 77 68 6f 6c  ze.  ** the whol
dc3d0 65 20 61 72 72 61 79 20 61 6e 64 20 73 69 6c 65  e array and sile
dc3e0 6e 63 65 20 76 61 6c 67 72 69 6e 64 2c 20 65 76  nce valgrind, ev
dc3f0 65 6e 20 69 66 20 74 68 61 74 20 6d 65 61 6e 73  en if that means
dc400 20 6c 65 73 73 20 72 61 6e 64 6f 6d 6e 65 73 73   less randomness
dc410 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 61 6e  .  ** in the ran
dc420 64 6f 6d 20 73 65 65 64 2e 0a 20 20 2a 2a 0a 20  dom seed..  **. 
dc430 20 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67   ** When testing
dc440 2c 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 7a  , initializing z
dc450 42 75 66 5b 5d 20 74 6f 20 7a 65 72 6f 20 69 73  Buf[] to zero is
dc460 20 61 6c 6c 20 77 65 20 64 6f 2e 20 20 54 68 61   all we do.  Tha
dc470 74 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 74 68 61  t means.  ** tha
dc480 74 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20  t we always use 
dc490 74 68 65 20 73 61 6d 65 20 72 61 6e 64 6f 6d 20  the same random 
dc4a0 6e 75 6d 62 65 72 20 73 65 71 75 65 6e 63 65 2e  number sequence.
dc4b0 20 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65    This makes the
dc4c0 0a 20 20 2a 2a 20 74 65 73 74 73 20 72 65 70 65  .  ** tests repe
dc4d0 61 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 6d  atable..  */.  m
dc4e0 65 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e  emset(zBuf, 0, n
dc4f0 42 75 66 29 3b 0a 23 69 66 20 21 64 65 66 69 6e  Buf);.#if !defin
dc500 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a  ed(SQLITE_TEST).
dc510 20 20 7b 0a 20 20 20 20 69 6e 74 20 70 69 64 2c    {.    int pid,
dc520 20 66 64 3b 0a 20 20 20 20 66 64 20 3d 20 6f 70   fd;.    fd = op
dc530 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e 64 6f 6d  en("/dev/urandom
dc540 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 20 20  ", O_RDONLY);.  
dc550 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20    if( fd<0 ){.  
dc560 20 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20      time_t t;.  
dc570 20 20 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20      time(&t);.  
dc580 20 20 20 20 6d 65 6d 63 70 79 28 7a 42 75 66 2c      memcpy(zBuf,
dc590 20 26 74 2c 20 73 69 7a 65 6f 66 28 74 29 29 3b   &t, sizeof(t));
dc5a0 0a 20 20 20 20 20 20 70 69 64 20 3d 20 67 65 74  .      pid = get
dc5b0 70 69 64 28 29 3b 0a 20 20 20 20 20 20 6d 65 6d  pid();.      mem
dc5c0 63 70 79 28 26 7a 42 75 66 5b 73 69 7a 65 6f 66  cpy(&zBuf[sizeof
dc5d0 28 74 29 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65  (t)], &pid, size
dc5e0 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 20 20 20  of(pid));.      
dc5f0 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 74  assert( sizeof(t
dc600 29 2b 73 69 7a 65 6f 66 28 70 69 64 29 3c 3d 6e  )+sizeof(pid)<=n
dc610 42 75 66 20 29 3b 0a 20 20 20 20 20 20 6e 42 75  Buf );.      nBu
dc620 66 20 3d 20 73 69 7a 65 6f 66 28 74 29 20 2b 20  f = sizeof(t) + 
dc630 73 69 7a 65 6f 66 28 70 69 64 29 3b 0a 20 20 20  sizeof(pid);.   
dc640 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 42   }else{.      nB
dc650 75 66 20 3d 20 72 65 61 64 28 66 64 2c 20 7a 42  uf = read(fd, zB
dc660 75 66 2c 20 6e 42 75 66 29 3b 0a 20 20 20 20 20  uf, nBuf);.     
dc670 20 63 6c 6f 73 65 28 66 64 29 3b 0a 20 20 20 20   close(fd);.    
dc680 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72  }.  }.#endif.  r
dc690 65 74 75 72 6e 20 6e 42 75 66 3b 0a 7d 0a 0a 0a  eturn nBuf;.}...
dc6a0 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20  /*.** Sleep for 
dc6b0 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e 20  a little while. 
dc6c0 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75   Return the amou
dc6d0 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 74  nt of time slept
dc6e0 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e  ..** The argumen
dc6f0 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  t is the number 
dc700 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20  of microseconds 
dc710 77 65 20 77 61 6e 74 20 74 6f 20 73 6c 65 65 70  we want to sleep
dc720 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20  ..** The return 
dc730 76 61 6c 75 65 20 69 73 20 74 68 65 20 6e 75 6d  value is the num
dc740 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f  ber of microseco
dc750 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74  nds of sleep act
dc760 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74  ually.** request
dc770 65 64 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65  ed from the unde
dc780 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67  rlying operating
dc790 20 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d 62 65   system, a numbe
dc7a0 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67 68 74  r which.** might
dc7b0 20 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e   be greater than
dc7c0 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65   or equal to the
dc7d0 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 74 20 6e   argument, but n
dc7e0 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20  ot less.** than 
dc7f0 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f  the argument..*/
dc800 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
dc810 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66  Sleep(sqlite3_vf
dc820 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d 69 63  s *pVfs, int mic
dc830 72 6f 73 65 63 6f 6e 64 73 29 7b 0a 23 69 66 20  roseconds){.#if 
dc840 64 65 66 69 6e 65 64 28 48 41 56 45 5f 55 53 4c  defined(HAVE_USL
dc850 45 45 50 29 20 26 26 20 48 41 56 45 5f 55 53 4c  EEP) && HAVE_USL
dc860 45 45 50 0a 20 20 75 73 6c 65 65 70 28 6d 69 63  EEP.  usleep(mic
dc870 72 6f 73 65 63 6f 6e 64 73 29 3b 0a 20 20 72 65  roseconds);.  re
dc880 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 6f 6e 64  turn microsecond
dc890 73 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 73  s;.#else.  int s
dc8a0 65 63 6f 6e 64 73 20 3d 20 28 6d 69 63 72 6f 73  econds = (micros
dc8b0 65 63 6f 6e 64 73 2b 39 39 39 39 39 39 29 2f 31  econds+999999)/1
dc8c0 30 30 30 30 30 30 3b 0a 20 20 73 6c 65 65 70 28  000000;.  sleep(
dc8d0 73 65 63 6f 6e 64 73 29 3b 0a 20 20 72 65 74 75  seconds);.  retu
dc8e0 72 6e 20 73 65 63 6f 6e 64 73 2a 31 30 30 30 30  rn seconds*10000
dc8f0 30 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a  00;.#endif.}../*
dc900 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
dc910 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73  g variable, if s
dc920 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f  et to a non-zero
dc930 20 76 61 6c 75 65 2c 20 62 65 63 6f 6d 65 73 20   value, becomes 
dc940 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 65  the result.** re
dc950 74 75 72 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  turned from sqli
dc960 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65  te3OsCurrentTime
dc970 28 29 2e 20 20 54 68 69 73 20 69 73 20 75 73 65  ().  This is use
dc980 64 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a  d for testing..*
dc990 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
dc9a0 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20  TEST.SQLITE_API 
dc9b0 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 75 72 72  int sqlite3_curr
dc9c0 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 0a 23 65  ent_time = 0;.#e
dc9d0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64  ndif../*.** Find
dc9e0 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d   the current tim
dc9f0 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20  e (in Universal 
dca00 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65  Coordinated Time
dca10 29 2e 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a  ).  Write the.**
dca20 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e   current time an
dca30 64 20 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69  d date as a Juli
dca40 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e  an Day number in
dca50 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a  to *prNow and.**
dca60 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75   return 0.  Retu
dca70 72 6e 20 31 20 69 66 20 74 68 65 20 74 69 6d 65  rn 1 if the time
dca80 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74   and date cannot
dca90 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74   be found..*/.st
dcaa0 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43 75 72  atic int unixCur
dcab0 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33  rentTime(sqlite3
dcac0 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f 75 62  _vfs *pVfs, doub
dcad0 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a 23 69 66 64  le *prNow){.#ifd
dcae0 65 66 20 4e 4f 5f 47 45 54 54 4f 44 0a 20 20 74  ef NO_GETTOD.  t
dcaf0 69 6d 65 5f 74 20 74 3b 0a 20 20 74 69 6d 65 28  ime_t t;.  time(
dcb00 26 74 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20  &t);.  *prNow = 
dcb10 74 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30  t/86400.0 + 2440
dcb20 35 38 37 2e 35 3b 0a 23 65 6c 73 65 0a 20 20 73  587.5;.#else.  s
dcb30 74 72 75 63 74 20 74 69 6d 65 76 61 6c 20 73 4e  truct timeval sN
dcb40 6f 77 3b 0a 20 20 67 65 74 74 69 6d 65 6f 66 64  ow;.  gettimeofd
dcb50 61 79 28 26 73 4e 6f 77 2c 20 30 29 3b 0a 20 20  ay(&sNow, 0);.  
dcb60 2a 70 72 4e 6f 77 20 3d 20 32 34 34 30 35 38 37  *prNow = 2440587
dcb70 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 5f 73 65 63  .5 + sNow.tv_sec
dcb80 2f 38 36 34 30 30 2e 30 20 2b 20 73 4e 6f 77 2e  /86400.0 + sNow.
dcb90 74 76 5f 75 73 65 63 2f 38 36 34 30 30 30 30 30  tv_usec/86400000
dcba0 30 30 30 2e 30 3b 0a 23 65 6e 64 69 66 0a 23 69  000.0;.#endif.#i
dcbb0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
dcbc0 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63  .  if( sqlite3_c
dcbd0 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20  urrent_time ){. 
dcbe0 20 20 20 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69     *prNow = sqli
dcbf0 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65  te3_current_time
dcc00 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35  /86400.0 + 24405
dcc10 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  87.5;.  }.#endif
dcc20 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
dcc30 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 47  static int unixG
dcc40 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69  etLastError(sqli
dcc50 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69  te3_vfs *pVfs, i
dcc60 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a  nt nBuf, char *z
dcc70 42 75 66 29 7b 0a 20 20 72 65 74 75 72 6e 20 30  Buf){.  return 0
dcc80 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69  ;.}../*.** Initi
dcc90 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74  alize the operat
dcca0 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72  ing system inter
dccb0 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  face..*/.SQLITE_
dccc0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
dccd0 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 20 0a  os_init(void){ .
dcce0 20 20 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 64 65    /* Macro to de
dccf0 66 69 6e 65 20 74 68 65 20 73 74 61 74 69 63 20  fine the static 
dcd00 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 6e 20 73  contents of an s
dcd10 71 6c 69 74 65 33 5f 76 66 73 20 73 74 72 75 63  qlite3_vfs struc
dcd20 74 75 72 65 20 66 6f 72 0a 20 20 2a 2a 20 74 68  ture for.  ** th
dcd30 65 20 75 6e 69 78 20 62 61 63 6b 65 6e 64 2e 20  e unix backend. 
dcd40 54 68 65 20 74 77 6f 20 70 61 72 61 6d 65 74 65  The two paramete
dcd50 72 73 20 61 72 65 20 74 68 65 20 76 61 6c 75 65  rs are the value
dcd60 73 20 74 6f 20 75 73 65 20 66 6f 72 0a 20 20 2a  s to use for.  *
dcd70 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66  * the sqlite3_vf
dcd80 73 2e 7a 4e 61 6d 65 20 61 6e 64 20 73 71 6c 69  s.zName and sqli
dcd90 74 65 33 5f 76 66 73 2e 70 41 70 70 44 61 74 61  te3_vfs.pAppData
dcda0 20 66 69 65 6c 64 73 2c 20 72 65 73 70 65 63 74   fields, respect
dcdb0 69 76 65 6c 79 2e 0a 20 20 2a 2a 20 0a 20 20 2a  ively..  ** .  *
dcdc0 2f 0a 20 20 23 64 65 66 69 6e 65 20 55 4e 49 58  /.  #define UNIX
dcdd0 56 46 53 28 7a 56 66 73 4e 61 6d 65 2c 20 70 56  VFS(zVfsName, pV
dcde0 66 73 41 70 70 44 61 74 61 29 20 7b 20 20 20 20  fsAppData) {    
dcdf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
dce00 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 20      1,          
dce10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65            /* iVe
dce20 72 73 69 6f 6e 20 2a 2f 20 20 20 20 20 20 20 20  rsion */        
dce30 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
dce40 20 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c    sizeof(unixFil
dce50 65 29 2c 20 20 20 20 20 2f 2a 20 73 7a 4f 73 46  e),     /* szOsF
dce60 69 6c 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ile */          
dce70 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
dce80 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2c 20 20 20  MAX_PATHNAME,   
dce90 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e        /* mxPathn
dcea0 61 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ame */          
dceb0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 30 2c          \.    0,
dcec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dced0 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 20      /* pNext */ 
dcee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dcef0 20 20 20 20 20 20 5c 0a 20 20 20 20 7a 56 66 73        \.    zVfs
dcf00 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20  Name,           
dcf10 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 20 20 20    /* zName */   
dcf20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dcf30 20 20 20 20 5c 0a 20 20 20 20 28 76 6f 69 64 20      \.    (void 
dcf40 2a 29 70 56 66 73 41 70 70 44 61 74 61 2c 20 20  *)pVfsAppData,  
dcf50 2f 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 20 20  /* pAppData */  
dcf60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dcf70 20 20 5c 0a 20 20 20 20 75 6e 69 78 4f 70 65 6e    \.    unixOpen
dcf80 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
dcf90 20 78 4f 70 65 6e 20 2a 2f 20 20 20 20 20 20 20   xOpen */       
dcfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dcfb0 5c 0a 20 20 20 20 75 6e 69 78 44 65 6c 65 74 65  \.    unixDelete
dcfc0 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78  ,           /* x
dcfd0 44 65 6c 65 74 65 20 2a 2f 20 20 20 20 20 20 20  Delete */       
dcfe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
dcff0 20 20 20 20 75 6e 69 78 41 63 63 65 73 73 2c 20      unixAccess, 
dd000 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 41 63            /* xAc
dd010 63 65 73 73 20 2a 2f 20 20 20 20 20 20 20 20 20  cess */         
dd020 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
dd030 20 20 75 6e 69 78 46 75 6c 6c 50 61 74 68 6e 61    unixFullPathna
dd040 6d 65 2c 20 20 20 20 20 2f 2a 20 78 46 75 6c 6c  me,     /* xFull
dd050 50 61 74 68 6e 61 6d 65 20 2a 2f 20 20 20 20 20  Pathname */     
dd060 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
dd070 75 6e 69 78 44 6c 4f 70 65 6e 2c 20 20 20 20 20  unixDlOpen,     
dd080 20 20 20 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e        /* xDlOpen
dd090 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
dd0a0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e          \.    un
dd0b0 69 78 44 6c 45 72 72 6f 72 2c 20 20 20 20 20 20  ixDlError,      
dd0c0 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20      /* xDlError 
dd0d0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
dd0e0 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78        \.    unix
dd0f0 44 6c 53 79 6d 2c 20 20 20 20 20 20 20 20 20 20  DlSym,          
dd100 20 20 2f 2a 20 78 44 6c 53 79 6d 20 2a 2f 20 20    /* xDlSym */  
dd110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dd120 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c      \.    unixDl
dd130 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20  Close,          
dd140 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 20 20  /* xDlClose */  
dd150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dd160 20 20 5c 0a 20 20 20 20 75 6e 69 78 52 61 6e 64    \.    unixRand
dd170 6f 6d 6e 65 73 73 2c 20 20 20 20 20 20 20 2f 2a  omness,       /*
dd180 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20 2a 2f 20   xRandomness */ 
dd190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dd1a0 5c 0a 20 20 20 20 75 6e 69 78 53 6c 65 65 70 2c  \.    unixSleep,
dd1b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
dd1c0 53 6c 65 65 70 20 2a 2f 20 20 20 20 20 20 20 20  Sleep */        
dd1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
dd1e0 20 20 20 20 75 6e 69 78 43 75 72 72 65 6e 74 54      unixCurrentT
dd1f0 69 6d 65 2c 20 20 20 20 20 20 2f 2a 20 78 43 75  ime,      /* xCu
dd200 72 72 65 6e 74 54 69 6d 65 20 2a 2f 20 20 20 20  rrentTime */    
dd210 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
dd220 20 20 75 6e 69 78 47 65 74 4c 61 73 74 45 72 72    unixGetLastErr
dd230 6f 72 20 20 20 20 20 20 2f 2a 20 78 47 65 74 4c  or      /* xGetL
dd240 61 73 74 45 72 72 6f 72 20 2a 2f 20 20 20 20 20  astError */     
dd250 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a            \.  }.
dd260 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65  .  static sqlite
dd270 33 5f 76 66 73 20 75 6e 69 78 56 66 73 20 3d 20  3_vfs unixVfs = 
dd280 55 4e 49 58 56 46 53 28 22 75 6e 69 78 22 2c 20  UNIXVFS("unix", 
dd290 30 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 45  0);.#if SQLITE_E
dd2a0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
dd2b0 59 4c 45 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73  YLE.  int i;.  s
dd2c0 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66  tatic sqlite3_vf
dd2d0 73 20 61 56 66 73 5b 5d 20 3d 20 7b 0a 20 20 20  s aVfs[] = {.   
dd2e0 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 70   UNIXVFS("unix-p
dd2f0 6f 73 69 78 22 2c 20 20 20 4c 4f 43 4b 49 4e 47  osix",   LOCKING
dd300 5f 53 54 59 4c 45 5f 50 4f 53 49 58 29 2c 20 0a  _STYLE_POSIX), .
dd310 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69      UNIXVFS("uni
dd320 78 2d 61 66 70 22 2c 20 20 20 20 20 4c 4f 43 4b  x-afp",     LOCK
dd330 49 4e 47 5f 53 54 59 4c 45 5f 41 46 50 29 2c 20  ING_STYLE_AFP), 
dd340 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e  .    UNIXVFS("un
dd350 69 78 2d 66 6c 6f 63 6b 22 2c 20 20 20 4c 4f 43  ix-flock",   LOC
dd360 4b 49 4e 47 5f 53 54 59 4c 45 5f 46 4c 4f 43 4b  KING_STYLE_FLOCK
dd370 29 2c 20 0a 20 20 20 20 55 4e 49 58 56 46 53 28  ), .    UNIXVFS(
dd380 22 75 6e 69 78 2d 64 6f 74 66 69 6c 65 22 2c 20  "unix-dotfile", 
dd390 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 44 4f  LOCKING_STYLE_DO
dd3a0 54 46 49 4c 45 29 2c 20 0a 20 20 20 20 55 4e 49  TFILE), .    UNI
dd3b0 58 56 46 53 28 22 75 6e 69 78 2d 6e 6f 6e 65 22  XVFS("unix-none"
dd3c0 2c 20 20 20 20 4c 4f 43 4b 49 4e 47 5f 53 54 59  ,    LOCKING_STY
dd3d0 4c 45 5f 4e 4f 4e 45 29 0a 20 20 7d 3b 0a 20 20  LE_NONE).  };.  
dd3e0 66 6f 72 28 69 3d 30 3b 20 69 3c 28 73 69 7a 65  for(i=0; i<(size
dd3f0 6f 66 28 61 56 66 73 29 2f 73 69 7a 65 6f 66 28  of(aVfs)/sizeof(
dd400 73 71 6c 69 74 65 33 5f 76 66 73 29 29 3b 20 69  sqlite3_vfs)); i
dd410 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ++){.    sqlite3
dd420 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 61  _vfs_register(&a
dd430 56 66 73 5b 69 5d 2c 20 30 29 3b 0a 20 20 7d 0a  Vfs[i], 0);.  }.
dd440 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33  #endif.  sqlite3
dd450 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 75  _vfs_register(&u
dd460 6e 69 78 56 66 73 2c 20 31 29 3b 0a 20 20 72 65  nixVfs, 1);.  re
dd470 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  turn SQLITE_OK; 
dd480 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f  .}../*.** Shutdo
dd490 77 6e 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67  wn the operating
dd4a0 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 61 63   system interfac
dd4b0 65 2e 20 54 68 69 73 20 69 73 20 61 20 6e 6f 2d  e. This is a no-
dd4c0 6f 70 20 66 6f 72 20 75 6e 69 78 2e 0a 2a 2f 0a  op for unix..*/.
dd4d0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
dd4e0 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f  qlite3_os_end(vo
dd4f0 69 64 29 7b 20 0a 20 20 72 65 74 75 72 6e 20 53  id){ .  return S
dd500 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 20 0a 23  QLITE_OK; .}. .#
dd510 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
dd520 4f 53 5f 55 4e 49 58 20 2a 2f 0a 0a 2f 2a 2a 2a  OS_UNIX */../***
dd530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
dd540 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a  of os_unix.c ***
dd550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
dd580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
dd590 6e 20 66 69 6c 65 20 6f 73 5f 77 69 6e 2e 63 20  n file os_win.c 
dd5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
dd5d0 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a  * 2004 May 22.**
dd5e0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
dd5f0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
dd600 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
dd610 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
dd620 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
dd630 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
dd640 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
dd650 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
dd660 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
dd670 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
dd680 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
dd690 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
dd6a0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
dd6b0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
dd6c0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
dd6d0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
dd6e0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
dd6f0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
dd700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd740 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
dd750 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
dd760 74 68 61 74 20 69 73 20 73 70 65 63 69 66 69 63  that is specific
dd770 20 74 6f 20 77 69 6e 64 6f 77 73 2e 0a 2a 2a 0a   to windows..**.
dd780 2a 2a 20 24 49 64 3a 20 6f 73 5f 77 69 6e 2e 63  ** $Id: os_win.c
dd790 2c 76 20 31 2e 31 33 37 20 32 30 30 38 2f 31 31  ,v 1.137 2008/11
dd7a0 2f 30 37 20 30 30 3a 30 36 3a 31 38 20 64 72 68  /07 00:06:18 drh
dd7b0 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 20 53 51   Exp $.*/.#if SQ
dd7c0 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 20 20 20 20  LITE_OS_WIN     
dd7d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69            /* Thi
dd7e0 73 20 66 69 6c 65 20 69 73 20 75 73 65 64 20 66  s file is used f
dd7f0 6f 72 20 77 69 6e 64 6f 77 73 20 6f 6e 6c 79 20  or windows only 
dd800 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74  */.../*.** A Not
dd810 65 20 41 62 6f 75 74 20 4d 65 6d 6f 72 79 20 41  e About Memory A
dd820 6c 6c 6f 63 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a  llocation:.**.**
dd830 20 54 68 69 73 20 64 72 69 76 65 72 20 75 73 65   This driver use
dd840 73 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28  s malloc()/free(
dd850 29 20 64 69 72 65 63 74 6c 79 20 72 61 74 68 65  ) directly rathe
dd860 72 20 74 68 61 6e 20 67 6f 69 6e 67 20 74 68 72  r than going thr
dd870 6f 75 67 68 0a 2a 2a 20 74 68 65 20 53 51 4c 69  ough.** the SQLi
dd880 74 65 2d 77 72 61 70 70 65 72 73 20 73 71 6c 69  te-wrappers sqli
dd890 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c  te3_malloc()/sql
dd8a0 69 74 65 33 5f 66 72 65 65 28 29 2e 20 20 54 68  ite3_free().  Th
dd8b0 6f 73 65 20 77 72 61 70 70 65 72 73 0a 2a 2a 20  ose wrappers.** 
dd8c0 61 72 65 20 64 65 73 69 67 6e 65 64 20 66 6f 72  are designed for
dd8d0 20 75 73 65 20 6f 6e 20 65 6d 62 65 64 64 65 64   use on embedded
dd8e0 20 73 79 73 74 65 6d 73 20 77 68 65 72 65 20 6d   systems where m
dd8f0 65 6d 6f 72 79 20 69 73 20 73 63 61 72 63 65 20  emory is scarce 
dd900 61 6e 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61  and.** malloc fa
dd910 69 6c 75 72 65 73 20 68 61 70 70 65 6e 20 66 72  ilures happen fr
dd920 65 71 75 65 6e 74 6c 79 2e 20 20 57 69 6e 33 32  equently.  Win32
dd930 20 64 6f 65 73 20 6e 6f 74 20 74 79 70 69 63 61   does not typica
dd940 6c 6c 79 20 72 75 6e 20 6f 6e 0a 2a 2a 20 65 6d  lly run on.** em
dd950 62 65 64 64 65 64 20 73 79 73 74 65 6d 73 2c 20  bedded systems, 
dd960 61 6e 64 20 77 68 65 6e 20 69 74 20 64 6f 65 73  and when it does
dd970 20 74 68 65 20 64 65 76 65 6c 6f 70 65 72 73 20   the developers 
dd980 6e 6f 72 6d 61 6c 6c 79 20 68 61 76 65 20 62 69  normally have bi
dd990 67 67 65 72 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73  gger.** problems
dd9a0 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20   to worry about 
dd9b0 74 68 61 6e 20 72 75 6e 6e 69 6e 67 20 6f 75 74  than running out
dd9c0 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 20 53 6f 20   of memory.  So 
dd9d0 74 68 65 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20  there is not.** 
dd9e0 61 20 63 6f 6d 70 65 6c 6c 69 6e 67 20 6e 65 65  a compelling nee
dd9f0 64 20 74 6f 20 75 73 65 20 74 68 65 20 77 72 61  d to use the wra
dda00 70 70 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 74  ppers..**.** But
dda10 20 74 68 65 72 65 20 69 73 20 61 20 67 6f 6f 64   there is a good
dda20 20 72 65 61 73 6f 6e 20 74 6f 20 6e 6f 74 20 75   reason to not u
dda30 73 65 20 74 68 65 20 77 72 61 70 70 65 72 73 2e  se the wrappers.
dda40 20 20 49 66 20 77 65 20 75 73 65 20 74 68 65 0a    If we use the.
dda50 2a 2a 20 77 72 61 70 70 65 72 73 20 74 68 65 6e  ** wrappers then
dda60 20 77 65 20 77 69 6c 6c 20 67 65 74 20 73 69 6d   we will get sim
dda70 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 28 29 20  ulated malloc() 
dda80 66 61 69 6c 75 72 65 73 20 77 69 74 68 69 6e 20  failures within 
dda90 74 68 69 73 0a 2a 2a 20 64 72 69 76 65 72 2e 20  this.** driver. 
ddaa0 20 41 6e 64 20 74 68 61 74 20 63 61 75 73 65 73   And that causes
ddab0 20 61 6c 6c 20 6b 69 6e 64 73 20 6f 66 20 70 72   all kinds of pr
ddac0 6f 62 6c 65 6d 73 20 66 6f 72 20 6f 75 72 20 74  oblems for our t
ddad0 65 73 74 73 2e 20 20 57 65 0a 2a 2a 20 63 6f 75  ests.  We.** cou
ddae0 6c 64 20 65 6e 68 61 6e 63 65 20 53 51 4c 69 74  ld enhance SQLit
ddaf0 65 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 73  e to deal with s
ddb00 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 20  imulated malloc 
ddb10 66 61 69 6c 75 72 65 73 20 77 69 74 68 69 6e 0a  failures within.
ddb20 2a 2a 20 74 68 65 20 4f 53 20 64 72 69 76 65 72  ** the OS driver
ddb30 2c 20 62 75 74 20 74 68 65 20 63 6f 64 65 20 74  , but the code t
ddb40 6f 20 64 65 61 6c 20 77 69 74 68 20 74 68 6f 73  o deal with thos
ddb50 65 20 66 61 69 6c 75 72 65 20 77 6f 75 6c 64 20  e failure would 
ddb60 6e 6f 74 0a 2a 2a 20 62 65 20 65 78 65 72 63 69  not.** be exerci
ddb70 73 65 64 20 6f 6e 20 4c 69 6e 75 78 20 28 77 68  sed on Linux (wh
ddb80 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65  ich does not nee
ddb90 64 20 74 6f 20 6d 61 6c 6c 6f 63 28 29 20 69 6e  d to malloc() in
ddba0 20 74 68 65 20 64 72 69 76 65 72 29 0a 2a 2a 20   the driver).** 
ddbb0 61 6e 64 20 73 6f 20 77 65 20 77 6f 75 6c 64 20  and so we would 
ddbc0 68 61 76 65 20 64 69 66 66 69 63 75 6c 74 79 20  have difficulty 
ddbd0 77 72 69 74 69 6e 67 20 63 6f 76 65 72 61 67 65  writing coverage
ddbe0 20 74 65 73 74 73 20 66 6f 72 20 74 68 61 74 0a   tests for that.
ddbf0 2a 2a 20 63 6f 64 65 2e 20 20 42 65 74 74 65 72  ** code.  Better
ddc00 20 74 6f 20 6c 65 61 76 65 20 74 68 65 20 63 6f   to leave the co
ddc10 64 65 20 6f 75 74 2c 20 77 65 20 74 68 69 6e 6b  de out, we think
ddc20 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e  ..**.** The poin
ddc30 74 20 6f 66 20 74 68 69 73 20 64 69 73 63 75 73  t of this discus
ddc40 73 69 6f 6e 20 69 73 20 61 73 20 66 6f 6c 6c 6f  sion is as follo
ddc50 77 73 3a 20 20 57 68 65 6e 20 63 72 65 61 74 69  ws:  When creati
ddc60 6e 67 20 61 20 6e 65 77 0a 2a 2a 20 4f 53 20 6c  ng a new.** OS l
ddc70 61 79 65 72 20 66 6f 72 20 61 6e 20 65 6d 62 65  ayer for an embe
ddc80 64 64 65 64 20 73 79 73 74 65 6d 2c 20 69 66 20  dded system, if 
ddc90 79 6f 75 20 75 73 65 20 74 68 69 73 20 66 69 6c  you use this fil
ddca0 65 20 61 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c  e as an example,
ddcb0 0a 2a 2a 20 61 76 6f 69 64 20 74 68 65 20 75 73  .** avoid the us
ddcc0 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2f 66 72  e of malloc()/fr
ddcd0 65 65 28 29 2e 20 20 54 68 6f 73 65 20 72 6f 75  ee().  Those rou
ddce0 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e  tines work ok on
ddcf0 20 77 69 6e 64 6f 77 73 0a 2a 2a 20 64 65 73 6b   windows.** desk
ddd00 74 6f 70 73 20 62 75 74 20 6e 6f 74 20 73 6f 20  tops but not so 
ddd10 77 65 6c 6c 20 69 6e 20 65 6d 62 65 64 64 65 64  well in embedded
ddd20 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 0a 23 69   systems..*/..#i
ddd30 6e 63 6c 75 64 65 20 3c 77 69 6e 62 61 73 65 2e  nclude <winbase.
ddd40 68 3e 0a 0a 23 69 66 64 65 66 20 5f 5f 43 59 47  h>..#ifdef __CYG
ddd50 57 49 4e 5f 5f 0a 23 20 69 6e 63 6c 75 64 65 20  WIN__.# include 
ddd60 3c 73 79 73 2f 63 79 67 77 69 6e 2e 68 3e 0a 23  <sys/cygwin.h>.#
ddd70 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63  endif../*.** Mac
ddd80 72 6f 73 20 75 73 65 64 20 74 6f 20 64 65 74 65  ros used to dete
ddd90 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72  rmine whether or
ddda0 20 6e 6f 74 20 74 6f 20 75 73 65 20 74 68 72 65   not to use thre
dddb0 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69  ads..*/.#if defi
dddc0 6e 65 64 28 54 48 52 45 41 44 53 41 46 45 29 20  ned(THREADSAFE) 
dddd0 26 26 20 54 48 52 45 41 44 53 41 46 45 0a 23 20  && THREADSAFE.# 
ddde0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 33  define SQLITE_W3
dddf0 32 5f 54 48 52 45 41 44 53 20 31 0a 23 65 6e 64  2_THREADS 1.#end
dde00 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64  if../*.** Includ
dde10 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63  e code that is c
dde20 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f  ommon to all os_
dde30 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a  *.c files.*/./**
dde40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
dde50 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68  lude os_common.h
dde60 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
dde70 66 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a  f os_win.c *****
dde80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
dde90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
ddea0 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f  in file os_commo
ddeb0 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.h ************
ddec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dded0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
ddee0 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a  ** 2004 May 22.*
ddef0 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
ddf00 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
ddf10 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
ddf20 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
ddf30 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
ddf40 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
ddf50 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
ddf60 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
ddf70 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
ddf80 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
ddf90 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
ddfa0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
ddfb0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
ddfc0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
ddfd0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
ddfe0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
ddff0 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
de000 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
de010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
de020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
de030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
de040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
de050 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
de060 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 72  le contains macr
de070 6f 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 20  os and a little 
de080 62 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61 74  bit of code that
de090 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a   is common to.**
de0a0 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 74   all of the plat
de0b0 66 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66 69  form-specific fi
de0c0 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64  les (os_*.c) and
de0d0 20 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69 6e   is #included in
de0e0 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c 65  to those.** file
de0f0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  s..**.** This fi
de100 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69 6e  le should be #in
de110 63 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f 73  cluded by the os
de120 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e  _*.c files only.
de130 20 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a 2a    It is not a.**
de140 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65   general purpose
de150 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2a   header file..**
de160 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 63 6f 6d 6d  .** $Id: os_comm
de170 6f 6e 2e 68 2c 76 20 31 2e 33 37 20 32 30 30 38  on.h,v 1.37 2008
de180 2f 30 35 2f 32 39 20 32 30 3a 32 32 3a 33 37 20  /05/29 20:22:37 
de190 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23  shane Exp $.*/.#
de1a0 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f  ifndef _OS_COMMO
de1b0 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53  N_H_.#define _OS
de1c0 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a  _COMMON_H_../*.*
de1d0 2a 20 41 74 20 6c 65 61 73 74 20 74 77 6f 20 62  * At least two b
de1e0 75 67 73 20 68 61 76 65 20 73 6c 69 70 70 65 64  ugs have slipped
de1f0 20 69 6e 20 62 65 63 61 75 73 65 20 77 65 20 63   in because we c
de200 68 61 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f 52  hanged the MEMOR
de210 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f  Y_DEBUG.** macro
de220 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 47   to SQLITE_DEBUG
de230 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20   and some older 
de240 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 65 20 6e  makefiles have n
de250 6f 74 20 79 65 74 20 6d 61 64 65 20 74 68 65 0a  ot yet made the.
de260 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 68 65 20  ** switch.  The 
de270 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73  following code s
de280 68 6f 75 6c 64 20 63 61 74 63 68 20 74 68 69 73  hould catch this
de290 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70   problem at comp
de2a0 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66  ile-time..*/.#if
de2b0 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47  def MEMORY_DEBUG
de2c0 0a 23 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45  .# error "The ME
de2d0 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f  MORY_DEBUG macro
de2e0 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55   is obsolete.  U
de2f0 73 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  se SQLITE_DEBUG 
de300 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 66  instead.".#endif
de310 0a 0a 0a 2f 2a 0a 20 2a 20 57 68 65 6e 20 74 65  .../*. * When te
de320 73 74 69 6e 67 2c 20 74 68 69 73 20 67 6c 6f 62  sting, this glob
de330 61 6c 20 76 61 72 69 61 62 6c 65 20 73 74 6f 72  al variable stor
de340 65 73 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e 20  es the location 
de350 6f 66 20 74 68 65 0a 20 2a 20 70 65 6e 64 69 6e  of the. * pendin
de360 67 2d 62 79 74 65 20 69 6e 20 74 68 65 20 64 61  g-byte in the da
de370 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 2a 2f  tabase file.. */
de380 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
de390 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 75  EST.SQLITE_API u
de3a0 6e 73 69 67 6e 65 64 20 69 6e 74 20 73 71 6c 69  nsigned int sqli
de3b0 74 65 33 5f 70 65 6e 64 69 6e 67 5f 62 79 74 65  te3_pending_byte
de3c0 20 3d 20 30 78 34 30 30 30 30 30 30 30 3b 0a 23   = 0x40000000;.#
de3d0 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
de3e0 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54  LITE_DEBUG.SQLIT
de3f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
de400 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d 20 30  lite3OSTrace = 0
de410 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ;.#define OSTRAC
de420 45 31 28 58 29 20 20 20 20 20 20 20 20 20 69 66  E1(X)         if
de430 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  ( sqlite3OSTrace
de440 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50   ) sqlite3DebugP
de450 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69 6e 65  rintf(X).#define
de460 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 20 20   OSTRACE2(X,Y)  
de470 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
de480 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65  OSTrace ) sqlite
de490 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59  3DebugPrintf(X,Y
de4a0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
de4b0 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20 69 66  E3(X,Y,Z)     if
de4c0 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  ( sqlite3OSTrace
de4d0 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50   ) sqlite3DebugP
de4e0 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65  rintf(X,Y,Z).#de
de4f0 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c  fine OSTRACE4(X,
de500 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73 71 6c  Y,Z,A)   if( sql
de510 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71  ite3OSTrace ) sq
de520 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
de530 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e  (X,Y,Z,A).#defin
de540 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a  e OSTRACE5(X,Y,Z
de550 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69 74 65  ,A,B) if( sqlite
de560 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74  3OSTrace ) sqlit
de570 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
de580 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65  Y,Z,A,B).#define
de590 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c   OSTRACE6(X,Y,Z,
de5a0 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69 66 28  A,B,C) \.    if(
de5b0 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20  sqlite3OSTrace) 
de5c0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
de5d0 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a  tf(X,Y,Z,A,B,C).
de5e0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37  #define OSTRACE7
de5f0 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 20  (X,Y,Z,A,B,C,D) 
de600 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33  \.    if(sqlite3
de610 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 33  OSTrace) sqlite3
de620 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
de630 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c 73 65  Z,A,B,C,D).#else
de640 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
de650 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  1(X).#define OST
de660 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65 66 69  RACE2(X,Y).#defi
de670 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c  ne OSTRACE3(X,Y,
de680 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  Z).#define OSTRA
de690 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65  CE4(X,Y,Z,A).#de
de6a0 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c  fine OSTRACE5(X,
de6b0 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65  Y,Z,A,B).#define
de6c0 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c   OSTRACE6(X,Y,Z,
de6d0 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f  A,B,C).#define O
de6e0 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c  STRACE7(X,Y,Z,A,
de6f0 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a 0a 2f  B,C,D).#endif../
de700 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20  *.** Macros for 
de710 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72 61 63  performance trac
de720 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74  ing.  Normally t
de730 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e 6c 79  urned off.  Only
de740 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38   works.** on i48
de750 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f 0a 23  6 hardware..*/.#
de760 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 45 52  ifdef SQLITE_PER
de770 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 0a 0a  FORMANCE_TRACE..
de780 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 20  /* .** hwtime.h 
de790 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20  contains inline 
de7a0 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 20 66  assembler code f
de7b0 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20  or implementing 
de7c0 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f 72 6d  .** high-perform
de7d0 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f 75 74  ance timing rout
de7e0 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  ines..*/./******
de7f0 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
de800 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 68 65   hwtime.h in the
de810 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 63 6f   middle of os_co
de820 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mmon.h *********
de830 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
de840 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
de850 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a  ile hwtime.h ***
de860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
de870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
de880 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
de890 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a  008 May 27.**.**
de8a0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
de8b0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
de8c0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
de8d0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
de8e0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
de8f0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
de900 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
de910 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
de920 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
de930 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
de940 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
de950 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
de960 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
de970 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
de980 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
de990 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
de9a0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
de9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
de9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
de9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
de9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
de9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
dea00 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
dea10 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61  ontains inline a
dea20 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 74 72  sm code for retr
dea30 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70 65 72  ieving "high-per
dea40 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f 75  formance".** cou
dea50 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20 63 6c  nters for x86 cl
dea60 61 73 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a 2a 20  ass CPUs..**.** 
dea70 24 49 64 3a 20 68 77 74 69 6d 65 2e 68 2c 76 20  $Id: hwtime.h,v 
dea80 31 2e 33 20 32 30 30 38 2f 30 38 2f 30 31 20 31  1.3 2008/08/01 1
dea90 34 3a 33 33 3a 31 35 20 73 68 61 6e 65 20 45 78  4:33:15 shane Ex
deaa0 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f  p $.*/.#ifndef _
deab0 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e  HWTIME_H_.#defin
deac0 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a  e _HWTIME_H_../*
dead0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
deae0 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77  g routine only w
deaf0 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d  orks on pentium-
deb00 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65 72 29  class (or newer)
deb10 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20   processors..** 
deb20 49 74 20 75 73 65 73 20 74 68 65 20 52 44 54 53  It uses the RDTS
deb30 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 61 64  C opcode to read
deb40 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75 6e 74   the cycle count
deb50 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20 74 68   value out of th
deb60 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72 20 61  e.** processor a
deb70 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61 74 20  nd returns that 
deb80 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63 61 6e  value.  This can
deb90 20 62 65 20 75 73 65 64 20 66 6f 72 20 68 69 67   be used for hig
deba0 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69  h-res.** profili
debb0 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69  ng..*/.#if (defi
debc0 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c  ned(__GNUC__) ||
debd0 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45   defined(_MSC_VE
debe0 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20 20 28  R)) && \.      (
debf0 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c  defined(i386) ||
dec00 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f   defined(__i386_
dec10 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d  _) || defined(_M
dec20 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66 20 64  _IX86))..  #if d
dec30 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29  efined(__GNUC__)
dec40 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73  ..  __inline__ s
dec50 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c  qlite_uint64 sql
dec60 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
dec70 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  {.     unsigned 
dec80 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20  int lo, hi;.    
dec90 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74   __asm__ __volat
deca0 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a  ile__ ("rdtsc" :
decb0 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22   "=a" (lo), "=d"
decc0 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72 65 74   (hi));.     ret
decd0 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e 74  urn (sqlite_uint
dece0 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f  64)hi << 32 | lo
decf0 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64  ;.  }..  #elif d
ded00 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29  efined(_MSC_VER)
ded10 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e  ..  __declspec(n
ded20 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73  aked) __inline s
ded30 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63  qlite_uint64 __c
ded40 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77 74 69  decl sqlite3Hwti
ded50 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f  me(void){.     _
ded60 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20 20 72  _asm {.        r
ded70 64 74 73 63 0a 20 20 20 20 20 20 20 20 72 65 74  dtsc.        ret
ded80 20 20 20 20 20 20 20 3b 20 72 65 74 75 72 6e 20         ; return 
ded90 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45 41 58  value at EDX:EAX
deda0 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23  .     }.  }..  #
dedb0 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28 64 65  endif..#elif (de
dedc0 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20  fined(__GNUC__) 
dedd0 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36  && defined(__x86
dede0 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c  _64__))..  __inl
dedf0 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e  ine__ sqlite_uin
dee00 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d  t64 sqlite3Hwtim
dee10 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75  e(void){.      u
dee20 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c  nsigned long val
dee30 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20  ;.      __asm__ 
dee40 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72  __volatile__ ("r
dee50 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28 76 61  dtsc" : "=A" (va
dee60 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  l));.      retur
dee70 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c  n val;.  }. .#el
dee80 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e  if (defined(__GN
dee90 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64  UC__) && defined
deea0 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f  (__ppc__))..  __
deeb0 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f  inline__ sqlite_
deec0 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77  uint64 sqlite3Hw
deed0 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20  time(void){.    
deee0 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20    unsigned long 
deef0 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20 20 20  long retval;.   
def00 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67     unsigned long
def10 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61   junk;.      __a
def20 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f  sm__ __volatile_
def30 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20  _ ("\n\.        
def40 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62 75 20    1:      mftbu 
def50 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20    %1\n\.        
def60 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 20 20            mftb  
def70 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20    %L0\n\.       
def80 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 75             mftbu
def90 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20     %0\n\.       
defa0 20 20 20 20 20 20 20 20 20 20 20 63 6d 70 77 20             cmpw 
defb0 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20     %0,%1\n\.    
defc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 6e                bn
defd0 65 20 20 20 20 20 31 62 22 0a 20 20 20 20 20 20  e     1b".      
defe0 20 20 20 20 20 20 20 20 20 20 20 20 3a 20 22 3d              : "=
deff0 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22 3d 72  r" (retval), "=r
df000 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20  " (junk));.     
df010 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a   return retval;.
df020 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65    }..#else..  #e
df030 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d  rror Need implem
df040 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69  entation of sqli
df050 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20  te3Hwtime() for 
df060 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a  your platform...
df070 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d    /*.  ** To com
df080 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69 6d 70  pile without imp
df090 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74 65  lementing sqlite
df0a0 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f  3Hwtime() for yo
df0b0 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a  ur platform,.  *
df0c0 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65  * you can remove
df0d0 20 74 68 65 20 61 62 6f 76 65 20 23 65 72 72 6f   the above #erro
df0e0 72 20 61 6e 64 20 75 73 65 20 74 68 65 20 66 6f  r and use the fo
df0f0 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75  llowing.  ** stu
df100 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75  b function.  You
df110 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e   will lose timin
df120 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6d 61  g support for ma
df130 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64  ny.  ** of the d
df140 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 65 73  ebugging and tes
df150 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73 2c 20  ting utilities, 
df160 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20 61 74  but it should at
df170 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70  .  ** least comp
df180 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a  ile and run..  *
df190 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
df1a0 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34     sqlite_uint64
df1b0 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76   sqlite3Hwtime(v
df1c0 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28 28 73  oid){ return ((s
df1d0 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 3b  qlite_uint64)0);
df1e0 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64   }..#endif..#end
df1f0 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f  if /* !defined(_
df200 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f  HWTIME_H_) */../
df210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
df220 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a  nd of hwtime.h *
df230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
df240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
df250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
df260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
df270 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
df280 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f  we left off in o
df290 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a  s_common.h *****
df2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
df2b0 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69  static sqlite_ui
df2c0 6e 74 36 34 20 67 5f 73 74 61 72 74 3b 0a 73 74  nt64 g_start;.st
df2d0 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74  atic sqlite_uint
df2e0 36 34 20 67 5f 65 6c 61 70 73 65 64 3b 0a 23 64  64 g_elapsed;.#d
df2f0 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52  efine TIMER_STAR
df300 54 20 20 20 20 20 20 20 67 5f 73 74 61 72 74 3d  T       g_start=
df310 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 0a  sqlite3Hwtime().
df320 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e  #define TIMER_EN
df330 44 20 20 20 20 20 20 20 20 20 67 5f 65 6c 61 70  D         g_elap
df340 73 65 64 3d 73 71 6c 69 74 65 33 48 77 74 69 6d  sed=sqlite3Hwtim
df350 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 64 65 66  e()-g_start.#def
df360 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45  ine TIMER_ELAPSE
df370 44 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 0a  D     g_elapsed.
df380 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 54 49  #else.#define TI
df390 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66 69 6e  MER_START.#defin
df3a0 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66  e TIMER_END.#def
df3b0 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45  ine TIMER_ELAPSE
df3c0 44 20 20 20 20 20 28 28 73 71 6c 69 74 65 5f 75  D     ((sqlite_u
df3d0 69 6e 74 36 34 29 30 29 0a 23 65 6e 64 69 66 0a  int64)0).#endif.
df3e0 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d  ./*.** If we com
df3f0 70 69 6c 65 20 77 69 74 68 20 74 68 65 20 53 51  pile with the SQ
df400 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72 6f 20  LITE_TEST macro 
df410 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 66 6f  set, then the fo
df420 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a  llowing block.**
df430 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 67 69   of code will gi
df440 76 65 20 75 73 20 74 68 65 20 61 62 69 6c 69 74  ve us the abilit
df450 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 20  y to simulate a 
df460 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20  disk I/O error. 
df470 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64   This.** is used
df480 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74 68 65   for testing the
df490 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f   I/O recovery lo
df4a0 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  gic..*/.#ifdef S
df4b0 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
df4c0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
df4d0 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d  3_io_error_hit =
df4e0 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f   0;            /
df4f0 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  * Total number o
df500 66 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0a  f I/O Errors */.
df510 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
df520 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
df530 68 61 72 64 68 69 74 20 3d 20 30 3b 20 20 20 20  hardhit = 0;    
df540 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
df550 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f   non-benign erro
df560 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  rs */.SQLITE_API
df570 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f   int sqlite3_io_
df580 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20  error_pending = 
df590 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75  0;        /* Cou
df5a0 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69 72 73 74  nt down to first
df5b0 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 51   I/O error */.SQ
df5c0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
df5d0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65  ite3_io_error_pe
df5e0 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20  rsist = 0;      
df5f0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49 2f 4f    /* True if I/O
df600 20 65 72 72 6f 72 73 20 70 65 72 73 69 73 74 20   errors persist 
df610 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
df620 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  t sqlite3_io_err
df630 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20  or_benign = 0;  
df640 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
df650 66 20 65 72 72 6f 72 73 20 61 72 65 20 62 65 6e  f errors are ben
df660 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ign */.SQLITE_AP
df670 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69  I int sqlite3_di
df680 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d  skfull_pending =
df690 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   0;.SQLITE_API i
df6a0 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66  nt sqlite3_diskf
df6b0 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65  ull = 0;.#define
df6c0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
df6d0 42 65 6e 69 67 6e 28 58 29 20 73 71 6c 69 74 65  Benign(X) sqlite
df6e0 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67  3_io_error_benig
df6f0 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69  n=(X).#define Si
df700 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 43 4f  mulateIOError(CO
df710 44 45 29 20 20 5c 0a 20 20 69 66 28 20 28 73 71  DE)  \.  if( (sq
df720 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
df730 65 72 73 69 73 74 20 26 26 20 73 71 6c 69 74 65  ersist && sqlite
df740 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20  3_io_error_hit) 
df750 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69  \.       || sqli
df760 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e  te3_io_error_pen
df770 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 20 5c  ding-- == 1 )  \
df780 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7b  .              {
df790 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20   local_ioerr(); 
df7a0 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63 20 76  CODE; }.static v
df7b0 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28  oid local_ioerr(
df7c0 29 7b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 49  ){.  IOTRACE(("I
df7d0 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73 71 6c  OERR\n"));.  sql
df7e0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69  ite3_io_error_hi
df7f0 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71 6c 69  t++;.  if( !sqli
df800 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e  te3_io_error_ben
df810 69 67 6e 20 29 20 73 71 6c 69 74 65 33 5f 69 6f  ign ) sqlite3_io
df820 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 2b 2b  _error_hardhit++
df830 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69 6d 75  ;.}.#define Simu
df840 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f  lateDiskfullErro
df850 72 28 43 4f 44 45 29 20 5c 0a 20 20 20 69 66 28  r(CODE) \.   if(
df860 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c   sqlite3_diskful
df870 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20  l_pending ){ \. 
df880 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f      if( sqlite3_
df890 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67  diskfull_pending
df8a0 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20 20 20   == 1 ){ \.     
df8b0 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b    local_ioerr();
df8c0 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65   \.       sqlite
df8d0 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20  3_diskfull = 1; 
df8e0 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  \.       sqlite3
df8f0 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20  _io_error_hit = 
df900 31 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f 44 45  1; \.       CODE
df910 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20  ; \.     }else{ 
df920 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  \.       sqlite3
df930 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e  _diskfull_pendin
df940 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a  g--; \.     } \.
df950 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69     }.#else.#defi
df960 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  ne SimulateIOErr
df970 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23 64 65 66  orBenign(X).#def
df980 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ine SimulateIOEr
df990 72 6f 72 28 41 29 0a 23 64 65 66 69 6e 65 20 53  ror(A).#define S
df9a0 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45  imulateDiskfullE
df9b0 72 72 6f 72 28 41 29 0a 23 65 6e 64 69 66 0a 0a  rror(A).#endif..
df9c0 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73 74 69  /*.** When testi
df9d0 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75 6e 74  ng, keep a count
df9e0 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   of the number o
df9f0 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f  f open files..*/
dfa00 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
dfa10 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  EST.SQLITE_API i
dfa20 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  nt sqlite3_open_
dfa30 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a  file_count = 0;.
dfa40 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e  #define OpenCoun
dfa50 74 65 72 28 58 29 20 20 73 71 6c 69 74 65 33 5f  ter(X)  sqlite3_
dfa60 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b  open_file_count+
dfa70 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 65 66 69  =(X).#else.#defi
dfa80 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58  ne OpenCounter(X
dfa90 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66  ).#endif..#endif
dfaa0 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 4f 53   /* !defined(_OS
dfab0 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a  _COMMON_H_) */..
dfac0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
dfad0 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e  End of os_common
dfae0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
dfaf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dfb00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
dfb10 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
dfb20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
dfb30 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
dfb40 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a  os_win.c *******
dfb50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
dfb60 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6d 69 63 72  ./*.** Some micr
dfb70 6f 73 6f 66 74 20 63 6f 6d 70 69 6c 65 72 73 20  osoft compilers 
dfb80 6c 61 63 6b 20 74 68 69 73 20 64 65 66 69 6e 69  lack this defini
dfb90 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  tion..*/.#ifndef
dfba0 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54   INVALID_FILE_AT
dfbb0 54 52 49 42 55 54 45 53 0a 23 20 64 65 66 69 6e  TRIBUTES.# defin
dfbc0 65 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41  e INVALID_FILE_A
dfbd0 54 54 52 49 42 55 54 45 53 20 28 28 44 57 4f 52  TTRIBUTES ((DWOR
dfbe0 44 29 2d 31 29 20 0a 23 65 6e 64 69 66 0a 0a 2f  D)-1) .#endif../
dfbf0 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 69  *.** Determine i
dfc00 66 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67  f we are dealing
dfc10 20 77 69 74 68 20 57 69 6e 64 6f 77 73 43 45 20   with WindowsCE 
dfc20 2d 20 77 68 69 63 68 20 68 61 73 20 61 20 6d 75  - which has a mu
dfc30 63 68 0a 2a 2a 20 72 65 64 75 63 65 64 20 41 50  ch.** reduced AP
dfc40 49 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  I..*/.#if SQLITE
dfc50 5f 4f 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69  _OS_WINCE.# defi
dfc60 6e 65 20 41 72 65 46 69 6c 65 41 70 69 73 41 4e  ne AreFileApisAN
dfc70 53 49 28 29 20 31 0a 23 65 6e 64 69 66 0a 0a 2f  SI() 1.#endif../
dfc80 2a 0a 2a 2a 20 57 69 6e 43 45 20 6c 61 63 6b 73  *.** WinCE lacks
dfc90 20 6e 61 74 69 76 65 20 73 75 70 70 6f 72 74 20   native support 
dfca0 66 6f 72 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67  for file locking
dfcb0 20 73 6f 20 77 65 20 68 61 76 65 20 74 6f 20 66   so we have to f
dfcc0 61 6b 65 20 69 74 0a 2a 2a 20 77 69 74 68 20 73  ake it.** with s
dfcd0 6f 6d 65 20 63 6f 64 65 20 6f 66 20 6f 75 72 20  ome code of our 
dfce0 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49  own..*/.#if SQLI
dfcf0 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 74 79 70 65  TE_OS_WINCE.type
dfd00 64 65 66 20 73 74 72 75 63 74 20 77 69 6e 63 65  def struct wince
dfd10 4c 6f 63 6b 20 7b 0a 20 20 69 6e 74 20 6e 52 65  Lock {.  int nRe
dfd20 61 64 65 72 73 3b 20 20 20 20 20 20 20 2f 2a 20  aders;       /* 
dfd30 4e 75 6d 62 65 72 20 6f 66 20 72 65 61 64 65 72  Number of reader
dfd40 20 6c 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64 20   locks obtained 
dfd50 2a 2f 0a 20 20 42 4f 4f 4c 20 62 50 65 6e 64 69  */.  BOOL bPendi
dfd60 6e 67 3b 20 20 20 20 20 20 2f 2a 20 49 6e 64 69  ng;      /* Indi
dfd70 63 61 74 65 73 20 61 20 70 65 6e 64 69 6e 67 20  cates a pending 
dfd80 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 6f 62  lock has been ob
dfd90 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c  tained */.  BOOL
dfda0 20 62 52 65 73 65 72 76 65 64 3b 20 20 20 20 20   bReserved;     
dfdb0 2f 2a 20 49 6e 64 69 63 61 74 65 73 20 61 20 72  /* Indicates a r
dfdc0 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 68 61 73  eserved lock has
dfdd0 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a   been obtained *
dfde0 2f 0a 20 20 42 4f 4f 4c 20 62 45 78 63 6c 75 73  /.  BOOL bExclus
dfdf0 69 76 65 3b 20 20 20 20 2f 2a 20 49 6e 64 69 63  ive;    /* Indic
dfe00 61 74 65 73 20 61 6e 20 65 78 63 6c 75 73 69 76  ates an exclusiv
dfe10 65 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20  e lock has been 
dfe20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 7d 20 77 69  obtained */.} wi
dfe30 6e 63 65 4c 6f 63 6b 3b 0a 23 65 6e 64 69 66 0a  nceLock;.#endif.
dfe40 0a 2f 2a 0a 2a 2a 20 54 68 65 20 77 69 6e 46 69  ./*.** The winFi
dfe50 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20  le structure is 
dfe60 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71  a subclass of sq
dfe70 6c 69 74 65 33 5f 66 69 6c 65 2a 20 73 70 65 63  lite3_file* spec
dfe80 69 66 69 63 20 74 6f 20 74 68 65 20 77 69 6e 33  ific to the win3
dfe90 32 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69 74 79  2.** portability
dfea0 20 6c 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64   layer..*/.typed
dfeb0 65 66 20 73 74 72 75 63 74 20 77 69 6e 46 69 6c  ef struct winFil
dfec0 65 20 77 69 6e 46 69 6c 65 3b 0a 73 74 72 75 63  e winFile;.struc
dfed0 74 20 77 69 6e 46 69 6c 65 20 7b 0a 20 20 63 6f  t winFile {.  co
dfee0 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  nst sqlite3_io_m
dfef0 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b  ethods *pMethod;
dff00 2f 2a 20 4d 75 73 74 20 62 65 20 66 69 72 73 74  /* Must be first
dff10 20 2a 2f 0a 20 20 48 41 4e 44 4c 45 20 68 3b 20   */.  HANDLE h; 
dff20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
dff30 20 48 61 6e 64 6c 65 20 66 6f 72 20 61 63 63 65   Handle for acce
dff40 73 73 69 6e 67 20 74 68 65 20 66 69 6c 65 20 2a  ssing the file *
dff50 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
dff60 72 20 6c 6f 63 6b 74 79 70 65 3b 20 2f 2a 20 54  r locktype; /* T
dff70 79 70 65 20 6f 66 20 6c 6f 63 6b 20 63 75 72 72  ype of lock curr
dff80 65 6e 74 6c 79 20 68 65 6c 64 20 6f 6e 20 74 68  ently held on th
dff90 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 73 68 6f  is file */.  sho
dffa0 72 74 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74  rt sharedLockByt
dffb0 65 3b 20 20 20 2f 2a 20 52 61 6e 64 6f 6d 6c 79  e;   /* Randomly
dffc0 20 63 68 6f 73 65 6e 20 62 79 74 65 20 75 73 65   chosen byte use
dffd0 64 20 61 73 20 61 20 73 68 61 72 65 64 20 6c 6f  d as a shared lo
dffe0 63 6b 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  ck */.#if SQLITE
dfff0 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 57 43 48 41  _OS_WINCE.  WCHA
e0000 52 20 2a 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73  R *zDeleteOnClos
e0010 65 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66  e;  /* Name of f
e0020 69 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 77 68  ile to delete wh
e0030 65 6e 20 63 6c 6f 73 69 6e 67 20 2a 2f 0a 20 20  en closing */.  
e0040 48 41 4e 44 4c 45 20 68 4d 75 74 65 78 3b 20 20  HANDLE hMutex;  
e0050 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78          /* Mutex
e0060 20 75 73 65 64 20 74 6f 20 63 6f 6e 74 72 6f 6c   used to control
e0070 20 61 63 63 65 73 73 20 74 6f 20 73 68 61 72 65   access to share
e0080 64 20 6c 6f 63 6b 20 2a 2f 20 20 0a 20 20 48 41  d lock */  .  HA
e0090 4e 44 4c 45 20 68 53 68 61 72 65 64 3b 20 20 20  NDLE hShared;   
e00a0 20 20 20 20 20 20 2f 2a 20 53 68 61 72 65 64 20        /* Shared 
e00b0 6d 65 6d 6f 72 79 20 73 65 67 6d 65 6e 74 20 75  memory segment u
e00c0 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20  sed for locking 
e00d0 2a 2f 0a 20 20 77 69 6e 63 65 4c 6f 63 6b 20 6c  */.  winceLock l
e00e0 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20  ocal;        /* 
e00f0 4c 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 62  Locks obtained b
e0100 79 20 74 68 69 73 20 69 6e 73 74 61 6e 63 65 20  y this instance 
e0110 6f 66 20 77 69 6e 46 69 6c 65 20 2a 2f 0a 20 20  of winFile */.  
e0120 77 69 6e 63 65 4c 6f 63 6b 20 2a 73 68 61 72 65  winceLock *share
e0130 64 3b 20 20 20 20 20 20 2f 2a 20 47 6c 6f 62 61  d;      /* Globa
e0140 6c 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6d 65  l shared lock me
e0150 6d 6f 72 79 20 66 6f 72 20 74 68 65 20 66 69 6c  mory for the fil
e0160 65 20 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a  e  */.#endif.};.
e0170 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
e0180 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 20 69  owing variable i
e0190 73 20 28 6e 6f 72 6d 61 6c 6c 79 29 20 73 65 74  s (normally) set
e01a0 20 6f 6e 63 65 20 61 6e 64 20 6e 65 76 65 72 20   once and never 
e01b0 63 68 61 6e 67 65 73 0a 2a 2a 20 74 68 65 72 65  changes.** there
e01c0 61 66 74 65 72 2e 20 20 49 74 20 72 65 63 6f 72  after.  It recor
e01d0 64 73 20 77 68 65 74 68 65 72 20 74 68 65 20 6f  ds whether the o
e01e0 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
e01f0 69 73 20 57 69 6e 39 35 0a 2a 2a 20 6f 72 20 57  is Win95.** or W
e0200 69 6e 4e 54 2e 0a 2a 2a 0a 2a 2a 20 30 3a 20 20  inNT..**.** 0:  
e0210 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   Operating syste
e0220 6d 20 75 6e 6b 6e 6f 77 6e 2e 0a 2a 2a 20 31 3a  m unknown..** 1:
e0230 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73     Operating sys
e0240 74 65 6d 20 69 73 20 57 69 6e 39 35 2e 0a 2a 2a  tem is Win95..**
e0250 20 32 3a 20 20 20 4f 70 65 72 61 74 69 6e 67 20   2:   Operating 
e0260 73 79 73 74 65 6d 20 69 73 20 57 69 6e 4e 54 2e  system is WinNT.
e0270 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20  .**.** In order 
e0280 74 6f 20 66 61 63 69 6c 69 74 61 74 65 20 74 65  to facilitate te
e0290 73 74 69 6e 67 20 6f 6e 20 61 20 57 69 6e 4e 54  sting on a WinNT
e02a0 20 73 79 73 74 65 6d 2c 20 74 68 65 20 74 65 73   system, the tes
e02b0 74 20 66 69 78 74 75 72 65 0a 2a 2a 20 63 61 6e  t fixture.** can
e02c0 20 6d 61 6e 75 61 6c 6c 79 20 73 65 74 20 74 68   manually set th
e02d0 69 73 20 76 61 6c 75 65 20 74 6f 20 31 20 74 6f  is value to 1 to
e02e0 20 65 6d 75 6c 61 74 65 20 57 69 6e 39 38 20 62   emulate Win98 b
e02f0 65 68 61 76 69 6f 72 2e 0a 2a 2f 0a 23 69 66 64  ehavior..*/.#ifd
e0300 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
e0310 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
e0320 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20  lite3_os_type = 
e0330 30 3b 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20  0;.#else.static 
e0340 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74  int sqlite3_os_t
e0350 79 70 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  ype = 0;.#endif.
e0360 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  ./*.** Return tr
e0370 75 65 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66  ue (non-zero) if
e0380 20 77 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20   we are running 
e0390 75 6e 64 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e  under WinNT, Win
e03a0 32 4b 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72  2K, WinXP,.** or
e03b0 20 57 69 6e 43 45 2e 20 20 52 65 74 75 72 6e 20   WinCE.  Return 
e03c0 66 61 6c 73 65 20 28 7a 65 72 6f 29 20 66 6f 72  false (zero) for
e03d0 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f   Win95, Win98, o
e03e0 72 20 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48  r WinME..**.** H
e03f0 65 72 65 20 69 73 20 61 6e 20 69 6e 74 65 72 65  ere is an intere
e0400 73 74 69 6e 67 20 6f 62 73 65 72 76 61 74 69 6f  sting observatio
e0410 6e 3a 20 20 57 69 6e 39 35 2c 20 57 69 6e 39 38  n:  Win95, Win98
e0420 2c 20 61 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b  , and WinME lack
e0430 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65  .** the LockFile
e0440 45 78 28 29 20 41 50 49 2e 20 20 42 75 74 20 77  Ex() API.  But w
e0450 65 20 63 61 6e 20 73 74 69 6c 6c 20 73 74 61 74  e can still stat
e0460 69 63 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69  ically link agai
e0470 6e 73 74 20 74 68 61 74 0a 2a 2a 20 41 50 49 20  nst that.** API 
e0480 61 73 20 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f  as long as we do
e0490 6e 27 74 20 63 61 6c 6c 20 69 74 20 77 69 6e 20  n't call it win 
e04a0 72 75 6e 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38  running Win95/98
e04b0 2f 4d 45 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a  /ME.  A call to.
e04c0 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ** this routine 
e04d0 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72  is used to deter
e04e0 6d 69 6e 65 20 69 66 20 74 68 65 20 68 6f 73 74  mine if the host
e04f0 20 69 73 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20   is Win95/98/ME 
e0500 6f 72 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58  or.** WinNT/2K/X
e0510 50 20 73 6f 20 74 68 61 74 20 77 65 20 77 69 6c  P so that we wil
e0520 6c 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f  l know whether o
e0530 72 20 6e 6f 74 20 77 65 20 63 61 6e 20 73 61 66  r not we can saf
e0540 65 6c 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20  ely call.** the 
e0550 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49  LockFileEx() API
e0560 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  ..*/.#if SQLITE_
e0570 4f 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e  OS_WINCE.# defin
e0580 65 20 69 73 4e 54 28 29 20 20 28 31 29 0a 23 65  e isNT()  (1).#e
e0590 6c 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74  lse.  static int
e05a0 20 69 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 20   isNT(void){.   
e05b0 20 69 66 28 20 73 71 6c 69 74 65 33 5f 6f 73 5f   if( sqlite3_os_
e05c0 74 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20  type==0 ){.     
e05d0 20 4f 53 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73   OSVERSIONINFO s
e05e0 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66  Info;.      sInf
e05f0 6f 2e 64 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66  o.dwOSVersionInf
e0600 6f 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73  oSize = sizeof(s
e0610 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 47 65 74  Info);.      Get
e0620 56 65 72 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f  VersionEx(&sInfo
e0630 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
e0640 5f 6f 73 5f 74 79 70 65 20 3d 20 73 49 6e 66 6f  _os_type = sInfo
e0650 2e 64 77 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56  .dwPlatformId==V
e0660 45 52 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33  ER_PLATFORM_WIN3
e0670 32 5f 4e 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20  2_NT ? 2 : 1;.  
e0680 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 73    }.    return s
e0690 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d 3d  qlite3_os_type==
e06a0 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  2;.  }.#endif /*
e06b0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
e06c0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65   */../*.** Conve
e06d0 72 74 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e  rt a UTF-8 strin
e06e0 67 20 74 6f 20 6d 69 63 72 6f 73 6f 66 74 20 75  g to microsoft u
e06f0 6e 69 63 6f 64 65 20 28 55 54 46 2d 31 36 3f 29  nicode (UTF-16?)
e0700 2e 20 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74  . .**.** Space t
e0710 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72  o hold the retur
e0720 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62  ned string is ob
e0730 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c  tained from mall
e0740 6f 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43  oc..*/.static WC
e0750 48 41 52 20 2a 75 74 66 38 54 6f 55 6e 69 63 6f  HAR *utf8ToUnico
e0760 64 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  de(const char *z
e0770 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74  Filename){.  int
e0780 20 6e 43 68 61 72 3b 0a 20 20 57 43 48 41 52 20   nChar;.  WCHAR 
e0790 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0a  *zWideFilename;.
e07a0 0a 20 20 6e 43 68 61 72 20 3d 20 4d 75 6c 74 69  .  nChar = Multi
e07b0 42 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 43  ByteToWideChar(C
e07c0 50 5f 55 54 46 38 2c 20 30 2c 20 7a 46 69 6c 65  P_UTF8, 0, zFile
e07d0 6e 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 20  name, -1, NULL, 
e07e0 30 29 3b 0a 20 20 7a 57 69 64 65 46 69 6c 65 6e  0);.  zWideFilen
e07f0 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 43  ame = malloc( nC
e0800 68 61 72 2a 73 69 7a 65 6f 66 28 7a 57 69 64 65  har*sizeof(zWide
e0810 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a  Filename[0]) );.
e0820 20 20 69 66 28 20 7a 57 69 64 65 46 69 6c 65 6e    if( zWideFilen
e0830 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ame==0 ){.    re
e0840 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 43  turn 0;.  }.  nC
e0850 68 61 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54  har = MultiByteT
e0860 6f 57 69 64 65 43 68 61 72 28 43 50 5f 55 54 46  oWideChar(CP_UTF
e0870 38 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c  8, 0, zFilename,
e0880 20 2d 31 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61   -1, zWideFilena
e0890 6d 65 2c 20 6e 43 68 61 72 29 3b 0a 20 20 69 66  me, nChar);.  if
e08a0 28 20 6e 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20  ( nChar==0 ){.  
e08b0 20 20 66 72 65 65 28 7a 57 69 64 65 46 69 6c 65    free(zWideFile
e08c0 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 57 69 64 65  name);.    zWide
e08d0 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20  Filename = 0;.  
e08e0 7d 0a 20 20 72 65 74 75 72 6e 20 7a 57 69 64 65  }.  return zWide
e08f0 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a  Filename;.}../*.
e0900 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f  ** Convert micro
e0910 73 6f 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20  soft unicode to 
e0920 55 54 46 2d 38 2e 20 20 53 70 61 63 65 20 74 6f  UTF-8.  Space to
e0930 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e   hold the return
e0940 65 64 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20  ed string is.** 
e0950 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61  obtained from ma
e0960 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  lloc()..*/.stati
e0970 63 20 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54  c char *unicodeT
e0980 6f 55 74 66 38 28 63 6f 6e 73 74 20 57 43 48 41  oUtf8(const WCHA
e0990 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65  R *zWideFilename
e09a0 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a  ){.  int nByte;.
e09b0 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d    char *zFilenam
e09c0 65 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 57 69  e;..  nByte = Wi
e09d0 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74  deCharToMultiByt
e09e0 65 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57  e(CP_UTF8, 0, zW
e09f0 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c  ideFilename, -1,
e0a00 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20   0, 0, 0, 0);.  
e0a10 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c  zFilename = mall
e0a20 6f 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 69  oc( nByte );.  i
e0a30 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20  f( zFilename==0 
e0a40 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
e0a50 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 57  .  }.  nByte = W
e0a60 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79  ideCharToMultiBy
e0a70 74 65 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a  te(CP_UTF8, 0, z
e0a80 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31  WideFilename, -1
e0a90 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79  , zFilename, nBy
e0aa0 74 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  te,.            
e0ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0ac0 20 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e    0, 0);.  if( n
e0ad0 42 79 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20  Byte == 0 ){.   
e0ae0 20 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29   free(zFilename)
e0af0 3b 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20  ;.    zFilename 
e0b00 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 0;.  }.  retur
e0b10 6e 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a  n zFilename;.}..
e0b20 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e  /*.** Convert an
e0b30 20 61 6e 73 69 20 73 74 72 69 6e 67 20 74 6f 20   ansi string to 
e0b40 6d 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64  microsoft unicod
e0b50 65 2c 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a  e, based on the.
e0b60 2a 2a 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70  ** current codep
e0b70 61 67 65 20 73 65 74 74 69 6e 67 73 20 66 6f 72  age settings for
e0b80 20 66 69 6c 65 20 61 70 69 73 2e 0a 2a 2a 20 0a   file apis..** .
e0b90 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64  ** Space to hold
e0ba0 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74   the returned st
e0bb0 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64  ring is obtained
e0bc0 0a 2a 2a 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e  .** from malloc.
e0bd0 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43 48 41 52  .*/.static WCHAR
e0be0 20 2a 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28   *mbcsToUnicode(
e0bf0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
e0c00 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42  ename){.  int nB
e0c10 79 74 65 3b 0a 20 20 57 43 48 41 52 20 2a 7a 4d  yte;.  WCHAR *zM
e0c20 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 69  bcsFilename;.  i
e0c30 6e 74 20 63 6f 64 65 70 61 67 65 20 3d 20 41 72  nt codepage = Ar
e0c40 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20  eFileApisANSI() 
e0c50 3f 20 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45  ? CP_ACP : CP_OE
e0c60 4d 43 50 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20  MCP;..  nByte = 
e0c70 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43  MultiByteToWideC
e0c80 68 61 72 28 63 6f 64 65 70 61 67 65 2c 20 30 2c  har(codepage, 0,
e0c90 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20   zFilename, -1, 
e0ca0 4e 55 4c 4c 2c 30 29 2a 73 69 7a 65 6f 66 28 57  NULL,0)*sizeof(W
e0cb0 43 48 41 52 29 3b 0a 20 20 7a 4d 62 63 73 46 69  CHAR);.  zMbcsFi
e0cc0 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28  lename = malloc(
e0cd0 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 4d   nByte*sizeof(zM
e0ce0 62 63 73 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20  bcsFilename[0]) 
e0cf0 29 3b 0a 20 20 69 66 28 20 7a 4d 62 63 73 46 69  );.  if( zMbcsFi
e0d00 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20  lename==0 ){.   
e0d10 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
e0d20 20 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69 42 79   nByte = MultiBy
e0d30 74 65 54 6f 57 69 64 65 43 68 61 72 28 63 6f 64  teToWideChar(cod
e0d40 65 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65 6e  epage, 0, zFilen
e0d50 61 6d 65 2c 20 2d 31 2c 20 7a 4d 62 63 73 46 69  ame, -1, zMbcsFi
e0d60 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 29 3b 0a  lename, nByte);.
e0d70 20 20 69 66 28 20 6e 42 79 74 65 3d 3d 30 20 29    if( nByte==0 )
e0d80 7b 0a 20 20 20 20 66 72 65 65 28 7a 4d 62 63 73  {.    free(zMbcs
e0d90 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a  Filename);.    z
e0da0 4d 62 63 73 46 69 6c 65 6e 61 6d 65 20 3d 20 30  MbcsFilename = 0
e0db0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a  ;.  }.  return z
e0dc0 4d 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a  MbcsFilename;.}.
e0dd0 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d  ./*.** Convert m
e0de0 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65  icrosoft unicode
e0df0 20 74 6f 20 6d 75 6c 74 69 62 79 74 65 20 63 68   to multibyte ch
e0e00 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 2c 20  aracter string, 
e0e10 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20  based on the.** 
e0e20 75 73 65 72 27 73 20 41 6e 73 69 20 63 6f 64 65  user's Ansi code
e0e30 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63  page..**.** Spac
e0e40 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65  e to hold the re
e0e50 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73  turned string is
e0e60 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a   obtained from.*
e0e70 2a 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73  * malloc()..*/.s
e0e80 74 61 74 69 63 20 63 68 61 72 20 2a 75 6e 69 63  tatic char *unic
e0e90 6f 64 65 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20  odeToMbcs(const 
e0ea0 57 43 48 41 52 20 2a 7a 57 69 64 65 46 69 6c 65  WCHAR *zWideFile
e0eb0 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79  name){.  int nBy
e0ec0 74 65 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c  te;.  char *zFil
e0ed0 65 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f 64  ename;.  int cod
e0ee0 65 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65 41  epage = AreFileA
e0ef0 70 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f 41  pisANSI() ? CP_A
e0f00 43 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a 0a  CP : CP_OEMCP;..
e0f10 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68    nByte = WideCh
e0f20 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f  arToMultiByte(co
e0f30 64 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64 65  depage, 0, zWide
e0f40 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c  Filename, -1, 0,
e0f50 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69   0, 0, 0);.  zFi
e0f60 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28  lename = malloc(
e0f70 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20   nByte );.  if( 
e0f80 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a  zFilename==0 ){.
e0f90 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
e0fa0 7d 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65  }.  nByte = Wide
e0fb0 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28  CharToMultiByte(
e0fc0 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 57 69  codepage, 0, zWi
e0fd0 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20  deFilename, -1, 
e0fe0 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65  zFilename, nByte
e0ff0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
e1000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1010 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 79  0, 0);.  if( nBy
e1020 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 66  te == 0 ){.    f
e1030 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  ree(zFilename);.
e1040 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20      zFilename = 
e1050 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
e1060 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a  zFilename;.}../*
e1070 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 75 6c 74  .** Convert mult
e1080 69 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20  ibyte character 
e1090 73 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 2e  string to UTF-8.
e10a0 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20    Space to hold 
e10b0 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20  the.** returned 
e10c0 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e  string is obtain
e10d0 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29  ed from malloc()
e10e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72  ..*/.static char
e10f0 20 2a 6d 62 63 73 54 6f 55 74 66 38 28 63 6f 6e   *mbcsToUtf8(con
e1100 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
e1110 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 46 69  me){.  char *zFi
e1120 6c 65 6e 61 6d 65 55 74 66 38 3b 0a 20 20 57 43  lenameUtf8;.  WC
e1130 48 41 52 20 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a  HAR *zTmpWide;..
e1140 20 20 7a 54 6d 70 57 69 64 65 20 3d 20 6d 62 63    zTmpWide = mbc
e1150 73 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65  sToUnicode(zFile
e1160 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 54 6d  name);.  if( zTm
e1170 70 57 69 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20  pWide==0 ){.    
e1180 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
e1190 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 20 3d 20  zFilenameUtf8 = 
e11a0 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54  unicodeToUtf8(zT
e11b0 6d 70 57 69 64 65 29 3b 0a 20 20 66 72 65 65 28  mpWide);.  free(
e11c0 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 72 65 74  zTmpWide);.  ret
e11d0 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66  urn zFilenameUtf
e11e0 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  8;.}../*.** Conv
e11f0 65 72 74 20 55 54 46 2d 38 20 74 6f 20 6d 75 6c  ert UTF-8 to mul
e1200 74 69 62 79 74 65 20 63 68 61 72 61 63 74 65 72  tibyte character
e1210 20 73 74 72 69 6e 67 2e 20 20 53 70 61 63 65 20   string.  Space 
e1220 74 6f 20 68 6f 6c 64 20 74 68 65 20 0a 2a 2a 20  to hold the .** 
e1230 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20  returned string 
e1240 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  is obtained from
e1250 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74   malloc()..*/.st
e1260 61 74 69 63 20 63 68 61 72 20 2a 75 74 66 38 54  atic char *utf8T
e1270 6f 4d 62 63 73 28 63 6f 6e 73 74 20 63 68 61 72  oMbcs(const char
e1280 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20   *zFilename){.  
e1290 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 4d  char *zFilenameM
e12a0 62 63 73 3b 0a 20 20 57 43 48 41 52 20 2a 7a 54  bcs;.  WCHAR *zT
e12b0 6d 70 57 69 64 65 3b 0a 0a 20 20 7a 54 6d 70 57  mpWide;..  zTmpW
e12c0 69 64 65 20 3d 20 75 74 66 38 54 6f 55 6e 69 63  ide = utf8ToUnic
e12d0 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  ode(zFilename);.
e12e0 20 20 69 66 28 20 7a 54 6d 70 57 69 64 65 3d 3d    if( zTmpWide==
e12f0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
e1300 30 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c 65 6e 61  0;.  }.  zFilena
e1310 6d 65 4d 62 63 73 20 3d 20 75 6e 69 63 6f 64 65  meMbcs = unicode
e1320 54 6f 4d 62 63 73 28 7a 54 6d 70 57 69 64 65 29  ToMbcs(zTmpWide)
e1330 3b 0a 20 20 66 72 65 65 28 7a 54 6d 70 57 69 64  ;.  free(zTmpWid
e1340 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 46 69  e);.  return zFi
e1350 6c 65 6e 61 6d 65 4d 62 63 73 3b 0a 7d 0a 0a 23  lenameMbcs;.}..#
e1360 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
e1370 43 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  CE./************
e1380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e13a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e13b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
e13c0 20 54 68 69 73 20 73 65 63 74 69 6f 6e 20 63 6f   This section co
e13d0 6e 74 61 69 6e 73 20 63 6f 64 65 20 66 6f 72 20  ntains code for 
e13e0 57 69 6e 43 45 20 6f 6e 6c 79 2e 0a 2a 2f 0a 2f  WinCE only..*/./
e13f0 2a 0a 2a 2a 20 57 69 6e 64 6f 77 73 43 45 20 64  *.** WindowsCE d
e1400 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 61 20 6c  oes not have a l
e1410 6f 63 61 6c 74 69 6d 65 28 29 20 66 75 6e 63 74  ocaltime() funct
e1420 69 6f 6e 2e 20 20 53 6f 20 63 72 65 61 74 65 20  ion.  So create 
e1430 61 0a 2a 2a 20 73 75 62 73 74 69 74 75 74 65 2e  a.** substitute.
e1440 0a 2a 2f 0a 73 74 72 75 63 74 20 74 6d 20 2a 5f  .*/.struct tm *_
e1450 5f 63 64 65 63 6c 20 6c 6f 63 61 6c 74 69 6d 65  _cdecl localtime
e1460 28 63 6f 6e 73 74 20 74 69 6d 65 5f 74 20 2a 74  (const time_t *t
e1470 29 0a 7b 0a 20 20 73 74 61 74 69 63 20 73 74 72  ).{.  static str
e1480 75 63 74 20 74 6d 20 79 3b 0a 20 20 46 49 4c 45  uct tm y;.  FILE
e1490 54 49 4d 45 20 75 54 6d 2c 20 6c 54 6d 3b 0a 20  TIME uTm, lTm;. 
e14a0 20 53 59 53 54 45 4d 54 49 4d 45 20 70 54 6d 3b   SYSTEMTIME pTm;
e14b0 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
e14c0 20 74 36 34 3b 0a 20 20 74 36 34 20 3d 20 2a 74   t64;.  t64 = *t
e14d0 3b 0a 20 20 74 36 34 20 3d 20 28 74 36 34 20 2b  ;.  t64 = (t64 +
e14e0 20 31 31 36 34 34 34 37 33 36 30 30 29 2a 31 30   11644473600)*10
e14f0 30 30 30 30 30 30 3b 0a 20 20 75 54 6d 2e 64 77  000000;.  uTm.dw
e1500 4c 6f 77 44 61 74 65 54 69 6d 65 20 3d 20 74 36  LowDateTime = t6
e1510 34 20 26 20 30 78 46 46 46 46 46 46 46 46 3b 0a  4 & 0xFFFFFFFF;.
e1520 20 20 75 54 6d 2e 64 77 48 69 67 68 44 61 74 65    uTm.dwHighDate
e1530 54 69 6d 65 3d 20 74 36 34 20 3e 3e 20 33 32 3b  Time= t64 >> 32;
e1540 0a 20 20 46 69 6c 65 54 69 6d 65 54 6f 4c 6f 63  .  FileTimeToLoc
e1550 61 6c 46 69 6c 65 54 69 6d 65 28 26 75 54 6d 2c  alFileTime(&uTm,
e1560 26 6c 54 6d 29 3b 0a 20 20 46 69 6c 65 54 69 6d  &lTm);.  FileTim
e1570 65 54 6f 53 79 73 74 65 6d 54 69 6d 65 28 26 6c  eToSystemTime(&l
e1580 54 6d 2c 26 70 54 6d 29 3b 0a 20 20 79 2e 74 6d  Tm,&pTm);.  y.tm
e1590 5f 79 65 61 72 20 3d 20 70 54 6d 2e 77 59 65 61  _year = pTm.wYea
e15a0 72 20 2d 20 31 39 30 30 3b 0a 20 20 79 2e 74 6d  r - 1900;.  y.tm
e15b0 5f 6d 6f 6e 20 3d 20 70 54 6d 2e 77 4d 6f 6e 74  _mon = pTm.wMont
e15c0 68 20 2d 20 31 3b 0a 20 20 79 2e 74 6d 5f 77 64  h - 1;.  y.tm_wd
e15d0 61 79 20 3d 20 70 54 6d 2e 77 44 61 79 4f 66 57  ay = pTm.wDayOfW
e15e0 65 65 6b 3b 0a 20 20 79 2e 74 6d 5f 6d 64 61 79  eek;.  y.tm_mday
e15f0 20 3d 20 70 54 6d 2e 77 44 61 79 3b 0a 20 20 79   = pTm.wDay;.  y
e1600 2e 74 6d 5f 68 6f 75 72 20 3d 20 70 54 6d 2e 77  .tm_hour = pTm.w
e1610 48 6f 75 72 3b 0a 20 20 79 2e 74 6d 5f 6d 69 6e  Hour;.  y.tm_min
e1620 20 3d 20 70 54 6d 2e 77 4d 69 6e 75 74 65 3b 0a   = pTm.wMinute;.
e1630 20 20 79 2e 74 6d 5f 73 65 63 20 3d 20 70 54 6d    y.tm_sec = pTm
e1640 2e 77 53 65 63 6f 6e 64 3b 0a 20 20 72 65 74 75  .wSecond;.  retu
e1650 72 6e 20 26 79 3b 0a 7d 0a 0a 2f 2a 20 54 68 69  rn &y;.}../* Thi
e1660 73 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20  s will never be 
e1670 63 61 6c 6c 65 64 2c 20 62 75 74 20 64 65 66 69  called, but defi
e1680 6e 65 64 20 74 6f 20 6d 61 6b 65 20 74 68 65 20  ned to make the 
e1690 63 6f 64 65 20 63 6f 6d 70 69 6c 65 20 2a 2f 0a  code compile */.
e16a0 23 64 65 66 69 6e 65 20 47 65 74 54 65 6d 70 50  #define GetTempP
e16b0 61 74 68 41 28 61 2c 62 29 0a 0a 23 64 65 66 69  athA(a,b)..#defi
e16c0 6e 65 20 4c 6f 63 6b 46 69 6c 65 28 61 2c 62 2c  ne LockFile(a,b,
e16d0 63 2c 64 2c 65 29 20 20 20 20 20 20 20 77 69 6e  c,d,e)       win
e16e0 63 65 4c 6f 63 6b 46 69 6c 65 28 26 61 2c 20 62  ceLockFile(&a, b
e16f0 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64 65 66 69  , c, d, e).#defi
e1700 6e 65 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 61 2c  ne UnlockFile(a,
e1710 62 2c 63 2c 64 2c 65 29 20 20 20 20 20 77 69 6e  b,c,d,e)     win
e1720 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 26 61 2c  ceUnlockFile(&a,
e1730 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64 65   b, c, d, e).#de
e1740 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28  fine LockFileEx(
e1750 61 2c 62 2c 63 2c 64 2c 65 2c 66 29 20 20 20 77  a,b,c,d,e,f)   w
e1760 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 78 28 26  inceLockFileEx(&
e1770 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 2c 20 66  a, b, c, d, e, f
e1780 29 0a 0a 23 64 65 66 69 6e 65 20 48 41 4e 44 4c  )..#define HANDL
e1790 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 61 29 20  E_TO_WINFILE(a) 
e17a0 28 77 69 6e 46 69 6c 65 2a 29 26 28 28 63 68 61  (winFile*)&((cha
e17b0 72 2a 29 61 29 5b 2d 6f 66 66 73 65 74 6f 66 28  r*)a)[-offsetof(
e17c0 77 69 6e 46 69 6c 65 2c 68 29 5d 0a 0a 2f 2a 0a  winFile,h)]../*.
e17d0 2a 2a 20 41 63 71 75 69 72 65 20 61 20 6c 6f 63  ** Acquire a loc
e17e0 6b 20 6f 6e 20 74 68 65 20 68 61 6e 64 6c 65 20  k on the handle 
e17f0 68 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  h.*/.static void
e1800 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69   winceMutexAcqui
e1810 72 65 28 48 41 4e 44 4c 45 20 68 29 7b 0a 20 20  re(HANDLE h){.  
e1820 20 44 57 4f 52 44 20 64 77 45 72 72 3b 0a 20 20   DWORD dwErr;.  
e1830 20 64 6f 20 7b 0a 20 20 20 20 20 64 77 45 72 72   do {.     dwErr
e1840 20 3d 20 57 61 69 74 46 6f 72 53 69 6e 67 6c 65   = WaitForSingle
e1850 4f 62 6a 65 63 74 28 68 2c 20 49 4e 46 49 4e 49  Object(h, INFINI
e1860 54 45 29 3b 0a 20 20 20 7d 20 77 68 69 6c 65 20  TE);.   } while 
e1870 28 64 77 45 72 72 20 21 3d 20 57 41 49 54 5f 4f  (dwErr != WAIT_O
e1880 42 4a 45 43 54 5f 30 20 26 26 20 64 77 45 72 72  BJECT_0 && dwErr
e1890 20 21 3d 20 57 41 49 54 5f 41 42 41 4e 44 4f 4e   != WAIT_ABANDON
e18a0 45 44 29 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 52 65 6c  ED);.}./*.** Rel
e18b0 65 61 73 65 20 61 20 6c 6f 63 6b 20 61 63 71 75  ease a lock acqu
e18c0 69 72 65 64 20 62 79 20 77 69 6e 63 65 4d 75 74  ired by winceMut
e18d0 65 78 41 63 71 75 69 72 65 28 29 0a 2a 2f 0a 23  exAcquire().*/.#
e18e0 64 65 66 69 6e 65 20 77 69 6e 63 65 4d 75 74 65  define winceMute
e18f0 78 52 65 6c 65 61 73 65 28 68 29 20 52 65 6c 65  xRelease(h) Rele
e1900 61 73 65 4d 75 74 65 78 28 68 29 0a 0a 2f 2a 0a  aseMutex(h)../*.
e1910 2a 2a 20 43 72 65 61 74 65 20 74 68 65 20 6d 75  ** Create the mu
e1920 74 65 78 20 61 6e 64 20 73 68 61 72 65 64 20 6d  tex and shared m
e1930 65 6d 6f 72 79 20 75 73 65 64 20 66 6f 72 20 6c  emory used for l
e1940 6f 63 6b 69 6e 67 20 69 6e 20 74 68 65 20 66 69  ocking in the fi
e1950 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72  le.** descriptor
e1960 20 70 46 69 6c 65 0a 2a 2f 0a 73 74 61 74 69 63   pFile.*/.static
e1970 20 42 4f 4f 4c 20 77 69 6e 63 65 43 72 65 61 74   BOOL winceCreat
e1980 65 4c 6f 63 6b 28 63 6f 6e 73 74 20 63 68 61 72  eLock(const char
e1990 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 77 69 6e   *zFilename, win
e19a0 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20  File *pFile){.  
e19b0 57 43 48 41 52 20 2a 7a 54 6f 6b 3b 0a 20 20 57  WCHAR *zTok;.  W
e19c0 43 48 41 52 20 2a 7a 4e 61 6d 65 20 3d 20 75 74  CHAR *zName = ut
e19d0 66 38 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c  f8ToUnicode(zFil
e19e0 65 6e 61 6d 65 29 3b 0a 20 20 42 4f 4f 4c 20 62  ename);.  BOOL b
e19f0 49 6e 69 74 20 3d 20 54 52 55 45 3b 0a 0a 20 20  Init = TRUE;..  
e1a00 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  /* Initialize th
e1a10 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 64 61 74 61  e local lockdata
e1a20 20 2a 2f 0a 20 20 5a 65 72 6f 4d 65 6d 6f 72 79   */.  ZeroMemory
e1a30 28 26 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2c 20  (&pFile->local, 
e1a40 73 69 7a 65 6f 66 28 70 46 69 6c 65 2d 3e 6c 6f  sizeof(pFile->lo
e1a50 63 61 6c 29 29 3b 0a 0a 20 20 2f 2a 20 52 65 70  cal));..  /* Rep
e1a60 6c 61 63 65 20 74 68 65 20 62 61 63 6b 73 6c 61  lace the backsla
e1a70 73 68 65 73 20 66 72 6f 6d 20 74 68 65 20 66 69  shes from the fi
e1a80 6c 65 6e 61 6d 65 20 61 6e 64 20 6c 6f 77 65 72  lename and lower
e1a90 63 61 73 65 20 69 74 0a 20 20 2a 2a 20 74 6f 20  case it.  ** to 
e1aa0 64 65 72 69 76 65 20 61 20 6d 75 74 65 78 20 6e  derive a mutex n
e1ab0 61 6d 65 2e 20 2a 2f 0a 20 20 7a 54 6f 6b 20 3d  ame. */.  zTok =
e1ac0 20 43 68 61 72 4c 6f 77 65 72 57 28 7a 4e 61 6d   CharLowerW(zNam
e1ad0 65 29 3b 0a 20 20 66 6f 72 20 28 3b 2a 7a 54 6f  e);.  for (;*zTo
e1ae0 6b 3b 7a 54 6f 6b 2b 2b 29 7b 0a 20 20 20 20 69  k;zTok++){.    i
e1af0 66 20 28 2a 7a 54 6f 6b 20 3d 3d 20 27 5c 5c 27  f (*zTok == '\\'
e1b00 29 20 2a 7a 54 6f 6b 20 3d 20 27 5f 27 3b 0a 20  ) *zTok = '_';. 
e1b10 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 2f   }..  /* Create/
e1b20 6f 70 65 6e 20 74 68 65 20 6e 61 6d 65 64 20 6d  open the named m
e1b30 75 74 65 78 20 2a 2f 0a 20 20 70 46 69 6c 65 2d  utex */.  pFile-
e1b40 3e 68 4d 75 74 65 78 20 3d 20 43 72 65 61 74 65  >hMutex = Create
e1b50 4d 75 74 65 78 57 28 4e 55 4c 4c 2c 20 46 41 4c  MutexW(NULL, FAL
e1b60 53 45 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 66  SE, zName);.  if
e1b70 20 28 21 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78   (!pFile->hMutex
e1b80 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 4e 61 6d  ){.    free(zNam
e1b90 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 46  e);.    return F
e1ba0 41 4c 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ALSE;.  }..  /* 
e1bb0 41 63 71 75 69 72 65 20 74 68 65 20 6d 75 74 65  Acquire the mute
e1bc0 78 20 62 65 66 6f 72 65 20 63 6f 6e 74 69 6e 75  x before continu
e1bd0 69 6e 67 20 2a 2f 0a 20 20 77 69 6e 63 65 4d 75  ing */.  winceMu
e1be0 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c 65  texAcquire(pFile
e1bf0 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 0a 20 20  ->hMutex);.  .  
e1c00 2f 2a 20 53 69 6e 63 65 20 74 68 65 20 6e 61 6d  /* Since the nam
e1c10 65 73 20 6f 66 20 6e 61 6d 65 64 20 6d 75 74 65  es of named mute
e1c20 78 65 73 2c 20 73 65 6d 61 70 68 6f 72 65 73 2c  xes, semaphores,
e1c30 20 66 69 6c 65 20 6d 61 70 70 69 6e 67 73 20 65   file mappings e
e1c40 74 63 20 61 72 65 20 0a 20 20 2a 2a 20 63 61 73  tc are .  ** cas
e1c50 65 2d 73 65 6e 73 69 74 69 76 65 2c 20 74 61 6b  e-sensitive, tak
e1c60 65 20 61 64 76 61 6e 74 61 67 65 20 6f 66 20 74  e advantage of t
e1c70 68 61 74 20 62 79 20 75 70 70 65 72 63 61 73 69  hat by uppercasi
e1c80 6e 67 20 74 68 65 20 6d 75 74 65 78 20 6e 61 6d  ng the mutex nam
e1c90 65 0a 20 20 2a 2a 20 61 6e 64 20 75 73 69 6e 67  e.  ** and using
e1ca0 20 74 68 61 74 20 61 73 20 74 68 65 20 73 68 61   that as the sha
e1cb0 72 65 64 20 66 69 6c 65 6d 61 70 70 69 6e 67 20  red filemapping 
e1cc0 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 43 68 61  name..  */.  Cha
e1cd0 72 55 70 70 65 72 57 28 7a 4e 61 6d 65 29 3b 0a  rUpperW(zName);.
e1ce0 20 20 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64    pFile->hShared
e1cf0 20 3d 20 43 72 65 61 74 65 46 69 6c 65 4d 61 70   = CreateFileMap
e1d00 70 69 6e 67 57 28 49 4e 56 41 4c 49 44 5f 48 41  pingW(INVALID_HA
e1d10 4e 44 4c 45 5f 56 41 4c 55 45 2c 20 4e 55 4c 4c  NDLE_VALUE, NULL
e1d20 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
e1d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1d40 20 20 20 20 20 20 20 20 20 50 41 47 45 5f 52 45           PAGE_RE
e1d50 41 44 57 52 49 54 45 2c 20 30 2c 20 73 69 7a 65  ADWRITE, 0, size
e1d60 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 2c 0a 20  of(winceLock),. 
e1d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1d90 20 20 20 20 20 20 7a 4e 61 6d 65 29 3b 20 20 0a        zName);  .
e1da0 0a 20 20 2f 2a 20 53 65 74 20 61 20 66 6c 61 67  .  /* Set a flag
e1db0 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 73 20   that indicates 
e1dc0 77 65 27 72 65 20 74 68 65 20 66 69 72 73 74 20  we're the first 
e1dd0 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 6d 65  to create the me
e1de0 6d 6f 72 79 20 73 6f 20 69 74 20 0a 20 20 2a 2a  mory so it .  **
e1df0 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d 69 6e   must be zero-in
e1e00 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69  itialized */.  i
e1e10 66 20 28 47 65 74 4c 61 73 74 45 72 72 6f 72 28  f (GetLastError(
e1e20 29 20 3d 3d 20 45 52 52 4f 52 5f 41 4c 52 45 41  ) == ERROR_ALREA
e1e30 44 59 5f 45 58 49 53 54 53 29 7b 0a 20 20 20 20  DY_EXISTS){.    
e1e40 62 49 6e 69 74 20 3d 20 46 41 4c 53 45 3b 0a 20  bInit = FALSE;. 
e1e50 20 7d 0a 0a 20 20 66 72 65 65 28 7a 4e 61 6d 65   }..  free(zName
e1e60 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 73  );..  /* If we s
e1e70 75 63 63 65 65 64 65 64 20 69 6e 20 6d 61 6b 69  ucceeded in maki
e1e80 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6d 65  ng the shared me
e1e90 6d 6f 72 79 20 68 61 6e 64 6c 65 2c 20 6d 61 70  mory handle, map
e1ea0 20 69 74 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46   it. */.  if (pF
e1eb0 69 6c 65 2d 3e 68 53 68 61 72 65 64 29 7b 0a 20  ile->hShared){. 
e1ec0 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64     pFile->shared
e1ed0 20 3d 20 28 77 69 6e 63 65 4c 6f 63 6b 2a 29 4d   = (winceLock*)M
e1ee0 61 70 56 69 65 77 4f 66 46 69 6c 65 28 70 46 69  apViewOfFile(pFi
e1ef0 6c 65 2d 3e 68 53 68 61 72 65 64 2c 20 0a 20 20  le->hShared, .  
e1f00 20 20 20 20 20 20 20 20 20 20 20 46 49 4c 45 5f             FILE_
e1f10 4d 41 50 5f 52 45 41 44 7c 46 49 4c 45 5f 4d 41  MAP_READ|FILE_MA
e1f20 50 5f 57 52 49 54 45 2c 20 30 2c 20 30 2c 20 73  P_WRITE, 0, 0, s
e1f30 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29  izeof(winceLock)
e1f40 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20 6d 61 70  );.    /* If map
e1f50 70 69 6e 67 20 66 61 69 6c 65 64 2c 20 63 6c 6f  ping failed, clo
e1f60 73 65 20 74 68 65 20 73 68 61 72 65 64 20 6d 65  se the shared me
e1f70 6d 6f 72 79 20 68 61 6e 64 6c 65 20 61 6e 64 20  mory handle and 
e1f80 65 72 61 73 65 20 69 74 20 2a 2f 0a 20 20 20 20  erase it */.    
e1f90 69 66 20 28 21 70 46 69 6c 65 2d 3e 73 68 61 72  if (!pFile->shar
e1fa0 65 64 29 7b 0a 20 20 20 20 20 20 43 6c 6f 73 65  ed){.      Close
e1fb0 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53  Handle(pFile->hS
e1fc0 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 70 46  hared);.      pF
e1fd0 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 20 4e  ile->hShared = N
e1fe0 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ULL;.    }.  }..
e1ff0 20 20 2f 2a 20 49 66 20 73 68 61 72 65 64 20 6d    /* If shared m
e2000 65 6d 6f 72 79 20 63 6f 75 6c 64 20 6e 6f 74 20  emory could not 
e2010 62 65 20 63 72 65 61 74 65 64 2c 20 74 68 65 6e  be created, then
e2020 20 63 6c 6f 73 65 20 74 68 65 20 6d 75 74 65 78   close the mutex
e2030 20 61 6e 64 20 66 61 69 6c 20 2a 2f 0a 20 20 69   and fail */.  i
e2040 66 20 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65  f (pFile->hShare
e2050 64 20 3d 3d 20 4e 55 4c 4c 29 7b 0a 20 20 20 20  d == NULL){.    
e2060 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73  winceMutexReleas
e2070 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29  e(pFile->hMutex)
e2080 3b 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c  ;.    CloseHandl
e2090 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29  e(pFile->hMutex)
e20a0 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68 4d 75  ;.    pFile->hMu
e20b0 74 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  tex = NULL;.    
e20c0 72 65 74 75 72 6e 20 46 41 4c 53 45 3b 0a 20 20  return FALSE;.  
e20d0 7d 0a 20 20 0a 20 20 2f 2a 20 49 6e 69 74 69 61  }.  .  /* Initia
e20e0 6c 69 7a 65 20 74 68 65 20 73 68 61 72 65 64 20  lize the shared 
e20f0 6d 65 6d 6f 72 79 20 69 66 20 77 65 27 72 65 20  memory if we're 
e2100 73 75 70 70 6f 73 65 64 20 74 6f 20 2a 2f 0a 20  supposed to */. 
e2110 20 69 66 20 28 62 49 6e 69 74 29 20 7b 0a 20 20   if (bInit) {.  
e2120 20 20 5a 65 72 6f 4d 65 6d 6f 72 79 28 70 46 69    ZeroMemory(pFi
e2130 6c 65 2d 3e 73 68 61 72 65 64 2c 20 73 69 7a 65  le->shared, size
e2140 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a  of(winceLock));.
e2150 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65    }..  winceMute
e2160 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e  xRelease(pFile->
e2170 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  hMutex);.  retur
e2180 6e 20 54 52 55 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n TRUE;.}../*.**
e2190 20 44 65 73 74 72 6f 79 20 74 68 65 20 70 61 72   Destroy the par
e21a0 74 20 6f 66 20 77 69 6e 46 69 6c 65 20 74 68 61  t of winFile tha
e21b0 74 20 64 65 61 6c 73 20 77 69 74 68 20 77 69 6e  t deals with win
e21c0 63 65 20 6c 6f 63 6b 73 0a 2a 2f 0a 73 74 61 74  ce locks.*/.stat
e21d0 69 63 20 76 6f 69 64 20 77 69 6e 63 65 44 65 73  ic void winceDes
e21e0 74 72 6f 79 4c 6f 63 6b 28 77 69 6e 46 69 6c 65  troyLock(winFile
e21f0 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 66 20 28   *pFile){.  if (
e2200 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a  pFile->hMutex){.
e2210 20 20 20 20 2f 2a 20 41 63 71 75 69 72 65 20 74      /* Acquire t
e2220 68 65 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20  he mutex */.    
e2230 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72  winceMutexAcquir
e2240 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29  e(pFile->hMutex)
e2250 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f  ;..    /* The fo
e2260 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 73 20 73  llowing blocks s
e2270 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 20 61  hould probably a
e2280 73 73 65 72 74 20 69 6e 20 64 65 62 75 67 20 6d  ssert in debug m
e2290 6f 64 65 2c 20 62 75 74 20 74 68 65 79 0a 20 20  ode, but they.  
e22a0 20 20 20 20 20 61 72 65 20 74 6f 20 63 6c 65 61       are to clea
e22b0 6e 75 70 20 69 6e 20 63 61 73 65 20 61 6e 79 20  nup in case any 
e22c0 6c 6f 63 6b 73 20 72 65 6d 61 69 6e 65 64 20 6f  locks remained o
e22d0 70 65 6e 20 2a 2f 0a 20 20 20 20 69 66 20 28 70  pen */.    if (p
e22e0 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61  File->local.nRea
e22f0 64 65 72 73 29 7b 0a 20 20 20 20 20 20 70 46 69  ders){.      pFi
e2300 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61  le->shared->nRea
e2310 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 7d 0a 20  ders --;.    }. 
e2320 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f     if (pFile->lo
e2330 63 61 6c 2e 62 52 65 73 65 72 76 65 64 29 7b 0a  cal.bReserved){.
e2340 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61        pFile->sha
e2350 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d  red->bReserved =
e2360 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 20 20   FALSE;.    }.  
e2370 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63    if (pFile->loc
e2380 61 6c 2e 62 50 65 6e 64 69 6e 67 29 7b 0a 20 20  al.bPending){.  
e2390 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65      pFile->share
e23a0 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 20 46 41  d->bPending = FA
e23b0 4c 53 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  LSE;.    }.    i
e23c0 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e  f (pFile->local.
e23d0 62 45 78 63 6c 75 73 69 76 65 29 7b 0a 20 20 20  bExclusive){.   
e23e0 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64     pFile->shared
e23f0 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 46  ->bExclusive = F
e2400 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ALSE;.    }..   
e2410 20 2f 2a 20 44 65 2d 72 65 66 65 72 65 6e 63 65   /* De-reference
e2420 20 61 6e 64 20 63 6c 6f 73 65 20 6f 75 72 20 63   and close our c
e2430 6f 70 79 20 6f 66 20 74 68 65 20 73 68 61 72 65  opy of the share
e2440 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65 20  d memory handle 
e2450 2a 2f 0a 20 20 20 20 55 6e 6d 61 70 56 69 65 77  */.    UnmapView
e2460 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e 73 68  OfFile(pFile->sh
e2470 61 72 65 64 29 3b 0a 20 20 20 20 43 6c 6f 73 65  ared);.    Close
e2480 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53  Handle(pFile->hS
e2490 68 61 72 65 64 29 3b 0a 0a 20 20 20 20 2f 2a 20  hared);..    /* 
e24a0 44 6f 6e 65 20 77 69 74 68 20 74 68 65 20 6d 75  Done with the mu
e24b0 74 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e 63 65  tex */.    wince
e24c0 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69  MutexRelease(pFi
e24d0 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 20 20 20 20  le->hMutex);    
e24e0 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65  .    CloseHandle
e24f0 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b  (pFile->hMutex);
e2500 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74  .    pFile->hMut
e2510 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 7d 0a 7d  ex = NULL;.  }.}
e2520 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 69 6d 70 6c  ../* .** An impl
e2530 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
e2540 65 20 4c 6f 63 6b 46 69 6c 65 28 29 20 41 50 49  e LockFile() API
e2550 20 6f 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20   of windows for 
e2560 77 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20  wince.*/.static 
e2570 42 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69  BOOL winceLockFi
e2580 6c 65 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68  le(.  HANDLE *ph
e2590 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77  File,.  DWORD dw
e25a0 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20  FileOffsetLow,. 
e25b0 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 66   DWORD dwFileOff
e25c0 73 65 74 48 69 67 68 2c 0a 20 20 44 57 4f 52 44  setHigh,.  DWORD
e25d0 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54   nNumberOfBytesT
e25e0 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52  oLockLow,.  DWOR
e25f0 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73  D nNumberOfBytes
e2600 54 6f 4c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20 20  ToLockHigh.){.  
e2610 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  winFile *pFile =
e2620 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49   HANDLE_TO_WINFI
e2630 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 20 42 4f  LE(phFile);.  BO
e2640 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 46 41 4c  OL bReturn = FAL
e2650 53 45 3b 0a 0a 20 20 69 66 20 28 21 70 46 69 6c  SE;..  if (!pFil
e2660 65 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75 72  e->hMutex) retur
e2670 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 65 4d  n TRUE;.  winceM
e2680 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c  utexAcquire(pFil
e2690 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 2f  e->hMutex);..  /
e26a0 2a 20 57 61 6e 74 69 6e 67 20 61 6e 20 65 78 63  * Wanting an exc
e26b0 6c 75 73 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f 0a  lusive lock? */.
e26c0 20 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73    if (dwFileOffs
e26d0 65 74 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44 5f  etLow == SHARED_
e26e0 46 49 52 53 54 0a 20 20 20 20 20 20 20 26 26 20  FIRST.       && 
e26f0 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
e2700 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 53 48 41 52 45  LockLow == SHARE
e2710 44 5f 53 49 5a 45 29 7b 0a 20 20 20 20 69 66 20  D_SIZE){.    if 
e2720 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e  (pFile->shared->
e2730 6e 52 65 61 64 65 72 73 20 3d 3d 20 30 20 26 26  nReaders == 0 &&
e2740 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e   pFile->shared->
e2750 62 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 30 29  bExclusive == 0)
e2760 7b 0a 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  {.       pFile->
e2770 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69  shared->bExclusi
e2780 76 65 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20  ve = TRUE;.     
e2790 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62    pFile->local.b
e27a0 45 78 63 6c 75 73 69 76 65 20 3d 20 54 52 55 45  Exclusive = TRUE
e27b0 3b 0a 20 20 20 20 20 20 20 62 52 65 74 75 72 6e  ;.       bReturn
e27c0 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20   = TRUE;.    }. 
e27d0 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20   }..  /* Want a 
e27e0 72 65 61 64 2d 6f 6e 6c 79 20 6c 6f 63 6b 3f 20  read-only lock? 
e27f0 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 28 64  */.  else if ((d
e2800 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3e  wFileOffsetLow >
e2810 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 26  = SHARED_FIRST &
e2820 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 77  &.            dw
e2830 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3c 20  FileOffsetLow < 
e2840 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b 20 53  SHARED_FIRST + S
e2850 48 41 52 45 44 5f 53 49 5a 45 29 20 26 26 0a 20  HARED_SIZE) &&. 
e2860 20 20 20 20 20 20 20 20 20 20 20 6e 4e 75 6d 62             nNumb
e2870 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c  erOfBytesToLockL
e2880 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66  ow == 1){.    if
e2890 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d   (pFile->shared-
e28a0 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 30  >bExclusive == 0
e28b0 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  ){.      pFile->
e28c0 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 2b  local.nReaders +
e28d0 2b 3b 0a 20 20 20 20 20 20 69 66 20 28 70 46 69  +;.      if (pFi
e28e0 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65  le->local.nReade
e28f0 72 73 20 3d 3d 20 31 29 7b 0a 20 20 20 20 20 20  rs == 1){.      
e2900 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
e2910 3e 6e 52 65 61 64 65 72 73 20 2b 2b 3b 0a 20 20  >nReaders ++;.  
e2920 20 20 20 20 7d 0a 20 20 20 20 20 20 62 52 65 74      }.      bRet
e2930 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  urn = TRUE;.    
e2940 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74  }.  }..  /* Want
e2950 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 3f   a pending lock?
e2960 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64   */.  else if (d
e2970 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d  wFileOffsetLow =
e2980 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26  = PENDING_BYTE &
e2990 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73  & nNumberOfBytes
e29a0 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b  ToLockLow == 1){
e29b0 0a 20 20 20 20 2f 2a 20 49 66 20 6e 6f 20 70 65  .    /* If no pe
e29c0 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62  nding lock has b
e29d0 65 65 6e 20 61 63 71 75 69 72 65 64 2c 20 74 68  een acquired, th
e29e0 65 6e 20 61 63 71 75 69 72 65 20 69 74 20 2a 2f  en acquire it */
e29f0 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  .    if (pFile->
e2a00 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67  shared->bPending
e2a10 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70   == 0) {.      p
e2a20 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50  File->shared->bP
e2a30 65 6e 64 69 6e 67 20 3d 20 54 52 55 45 3b 0a 20  ending = TRUE;. 
e2a40 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61       pFile->loca
e2a50 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20 54 52 55  l.bPending = TRU
e2a60 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e  E;.      bReturn
e2a70 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20   = TRUE;.    }. 
e2a80 20 7d 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 72   }.  /* Want a r
e2a90 65 73 65 72 76 65 64 20 6c 6f 63 6b 3f 20 2a 2f  eserved lock? */
e2aa0 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 69  .  else if (dwFi
e2ab0 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 52  leOffsetLow == R
e2ac0 45 53 45 52 56 45 44 5f 42 59 54 45 20 26 26 20  ESERVED_BYTE && 
e2ad0 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
e2ae0 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20  LockLow == 1){. 
e2af0 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68     if (pFile->sh
e2b00 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20  ared->bReserved 
e2b10 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70 46  == 0) {.      pF
e2b20 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65  ile->shared->bRe
e2b30 73 65 72 76 65 64 20 3d 20 54 52 55 45 3b 0a 20  served = TRUE;. 
e2b40 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61       pFile->loca
e2b50 6c 2e 62 52 65 73 65 72 76 65 64 20 3d 20 54 52  l.bReserved = TR
e2b60 55 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72  UE;.      bRetur
e2b70 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a  n = TRUE;.    }.
e2b80 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65    }..  winceMute
e2b90 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e  xRelease(pFile->
e2ba0 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  hMutex);.  retur
e2bb0 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a  n bReturn;.}../*
e2bc0 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74  .** An implement
e2bd0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 55 6e 6c  ation of the Unl
e2be0 6f 63 6b 46 69 6c 65 20 41 50 49 20 6f 66 20 77  ockFile API of w
e2bf0 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65  indows for wince
e2c00 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20  .*/.static BOOL 
e2c10 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28  winceUnlockFile(
e2c20 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c  .  HANDLE *phFil
e2c30 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c  e,.  DWORD dwFil
e2c40 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57  eOffsetLow,.  DW
e2c50 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74  ORD dwFileOffset
e2c60 48 69 67 68 2c 0a 20 20 44 57 4f 52 44 20 6e 4e  High,.  DWORD nN
e2c70 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e  umberOfBytesToUn
e2c80 6c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44  lockLow,.  DWORD
e2c90 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54   nNumberOfBytesT
e2ca0 6f 55 6e 6c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20  oUnlockHigh.){. 
e2cb0 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20   winFile *pFile 
e2cc0 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46  = HANDLE_TO_WINF
e2cd0 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 20 42  ILE(phFile);.  B
e2ce0 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 46 41  OOL bReturn = FA
e2cf0 4c 53 45 3b 0a 0a 20 20 69 66 20 28 21 70 46 69  LSE;..  if (!pFi
e2d00 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75  le->hMutex) retu
e2d10 72 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 65  rn TRUE;.  wince
e2d20 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69  MutexAcquire(pFi
e2d30 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20  le->hMutex);..  
e2d40 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20 72  /* Releasing a r
e2d50 65 61 64 65 72 20 6c 6f 63 6b 20 6f 72 20 61 6e  eader lock or an
e2d60 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20   exclusive lock 
e2d70 2a 2f 0a 20 20 69 66 20 28 64 77 46 69 6c 65 4f  */.  if (dwFileO
e2d80 66 66 73 65 74 4c 6f 77 20 3e 3d 20 53 48 41 52  ffsetLow >= SHAR
e2d90 45 44 5f 46 49 52 53 54 20 26 26 0a 20 20 20 20  ED_FIRST &&.    
e2da0 20 20 20 64 77 46 69 6c 65 4f 66 66 73 65 74 4c     dwFileOffsetL
e2db0 6f 77 20 3c 20 53 48 41 52 45 44 5f 46 49 52 53  ow < SHARED_FIRS
e2dc0 54 20 2b 20 53 48 41 52 45 44 5f 53 49 5a 45 29  T + SHARED_SIZE)
e2dd0 7b 0a 20 20 20 20 2f 2a 20 44 69 64 20 77 65 20  {.    /* Did we 
e2de0 68 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76  have an exclusiv
e2df0 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20 20 69  e lock? */.    i
e2e00 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e  f (pFile->local.
e2e10 62 45 78 63 6c 75 73 69 76 65 29 7b 0a 20 20 20  bExclusive){.   
e2e20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e     pFile->local.
e2e30 62 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c  bExclusive = FAL
e2e40 53 45 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  SE;.      pFile-
e2e50 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73  >shared->bExclus
e2e60 69 76 65 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20  ive = FALSE;.   
e2e70 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55     bReturn = TRU
e2e80 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  E;.    }..    /*
e2e90 20 44 69 64 20 77 65 20 6a 75 73 74 20 68 61 76   Did we just hav
e2ea0 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 3f  e a reader lock?
e2eb0 20 2a 2f 0a 20 20 20 20 65 6c 73 65 20 69 66 20   */.    else if 
e2ec0 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52  (pFile->local.nR
e2ed0 65 61 64 65 72 73 29 7b 0a 20 20 20 20 20 20 70  eaders){.      p
e2ee0 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61  File->local.nRea
e2ef0 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20 20 69  ders --;.      i
e2f00 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e  f (pFile->local.
e2f10 6e 52 65 61 64 65 72 73 20 3d 3d 20 30 29 0a 20  nReaders == 0). 
e2f20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 70       {.        p
e2f30 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52  File->shared->nR
e2f40 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20  eaders --;.     
e2f50 20 7d 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e   }.      bReturn
e2f60 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20   = TRUE;.    }. 
e2f70 20 7d 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69   }..  /* Releasi
e2f80 6e 67 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63  ng a pending loc
e2f90 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28  k */.  else if (
e2fa0 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20  dwFileOffsetLow 
e2fb0 3d 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20  == PENDING_BYTE 
e2fc0 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65  && nNumberOfByte
e2fd0 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20  sToUnlockLow == 
e2fe0 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c  1){.    if (pFil
e2ff0 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e  e->local.bPendin
e3000 67 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  g){.      pFile-
e3010 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 20  >local.bPending 
e3020 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70  = FALSE;.      p
e3030 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50  File->shared->bP
e3040 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a  ending = FALSE;.
e3050 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20        bReturn = 
e3060 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  TRUE;.    }.  }.
e3070 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 61    /* Releasing a
e3080 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 2a   reserved lock *
e3090 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46  /.  else if (dwF
e30a0 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20  ileOffsetLow == 
e30b0 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 26 26  RESERVED_BYTE &&
e30c0 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54   nNumberOfBytesT
e30d0 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29  oUnlockLow == 1)
e30e0 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d  {.    if (pFile-
e30f0 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64  >local.bReserved
e3100 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  ) {.      pFile-
e3110 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64  >local.bReserved
e3120 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20   = FALSE;.      
e3130 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62  pFile->shared->b
e3140 52 65 73 65 72 76 65 64 20 3d 20 46 41 4c 53 45  Reserved = FALSE
e3150 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20  ;.      bReturn 
e3160 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20  = TRUE;.    }.  
e3170 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 52  }..  winceMutexR
e3180 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d  elease(pFile->hM
e3190 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
e31a0 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a  bReturn;.}../*.*
e31b0 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74  * An implementat
e31c0 69 6f 6e 20 6f 66 20 74 68 65 20 4c 6f 63 6b 46  ion of the LockF
e31d0 69 6c 65 45 78 28 29 20 41 50 49 20 6f 66 20 77  ileEx() API of w
e31e0 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65  indows for wince
e31f0 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20  .*/.static BOOL 
e3200 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 78 28  winceLockFileEx(
e3210 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c  .  HANDLE *phFil
e3220 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 6c 61  e,.  DWORD dwFla
e3230 67 73 2c 0a 20 20 44 57 4f 52 44 20 64 77 52 65  gs,.  DWORD dwRe
e3240 73 65 72 76 65 64 2c 0a 20 20 44 57 4f 52 44 20  served,.  DWORD 
e3250 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
e3260 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44  LockLow,.  DWORD
e3270 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54   nNumberOfBytesT
e3280 6f 4c 6f 63 6b 48 69 67 68 2c 0a 20 20 4c 50 4f  oLockHigh,.  LPO
e3290 56 45 52 4c 41 50 50 45 44 20 6c 70 4f 76 65 72  VERLAPPED lpOver
e32a0 6c 61 70 70 65 64 0a 29 7b 0a 20 20 2f 2a 20 49  lapped.){.  /* I
e32b0 66 20 74 68 65 20 63 61 6c 6c 65 72 20 77 61 6e  f the caller wan
e32c0 74 73 20 61 20 73 68 61 72 65 64 20 72 65 61 64  ts a shared read
e32d0 20 6c 6f 63 6b 2c 20 66 6f 72 77 61 72 64 20 74   lock, forward t
e32e0 68 69 73 20 63 61 6c 6c 0a 20 20 2a 2a 20 74 6f  his call.  ** to
e32f0 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 20 2a   winceLockFile *
e3300 2f 0a 20 20 69 66 20 28 6c 70 4f 76 65 72 6c 61  /.  if (lpOverla
e3310 70 70 65 64 2d 3e 4f 66 66 73 65 74 20 3d 3d 20  pped->Offset == 
e3320 53 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a  SHARED_FIRST &&.
e3330 20 20 20 20 20 20 64 77 46 6c 61 67 73 20 3d 3d        dwFlags ==
e3340 20 31 20 26 26 0a 20 20 20 20 20 20 6e 4e 75 6d   1 &&.      nNum
e3350 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b  berOfBytesToLock
e3360 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44 5f 53 49  Low == SHARED_SI
e3370 5a 45 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ZE){.    return 
e3380 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 70 68  winceLockFile(ph
e3390 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 52  File, SHARED_FIR
e33a0 53 54 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20  ST, 0, 1, 0);.  
e33b0 7d 0a 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45  }.  return FALSE
e33c0 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 45 6e 64 20 6f 66  ;.}./*.** End of
e33d0 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f 64   the special cod
e33e0 65 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2a 2a 2a  e for wince.****
e33f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69  *********/.#endi
e3440 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57  f /* SQLITE_OS_W
e3450 49 4e 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  INCE */../******
e3460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e34a0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 6e  *******.** The n
e34b0 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75  ext group of rou
e34c0 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20  tines implement 
e34d0 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20  the I/O methods 
e34e0 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20  specified.** by 
e34f0 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  the sqlite3_io_m
e3500 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a  ethods object..*
e3510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
e3560 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69  /*.** Close a fi
e3570 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20  le..**.** It is 
e3580 72 65 70 6f 72 74 65 64 20 74 68 61 74 20 61 6e  reported that an
e3590 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6c 6f 73   attempt to clos
e35a0 65 20 61 20 68 61 6e 64 6c 65 20 6d 69 67 68 74  e a handle might
e35b0 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 66 61   sometimes.** fa
e35c0 69 6c 2e 20 20 54 68 69 73 20 69 73 20 61 20 76  il.  This is a v
e35d0 65 72 79 20 75 6e 72 65 61 73 6f 6e 61 62 6c 65  ery unreasonable
e35e0 20 72 65 73 75 6c 74 2c 20 62 75 74 20 77 69 6e   result, but win
e35f0 64 6f 77 73 20 69 73 20 6e 6f 74 6f 72 69 6f 75  dows is notoriou
e3600 73 0a 2a 2a 20 66 6f 72 20 62 65 69 6e 67 20 75  s.** for being u
e3610 6e 72 65 61 73 6f 6e 61 62 6c 65 20 73 6f 20 49  nreasonable so I
e3620 20 64 6f 20 6e 6f 74 20 64 6f 75 62 74 20 74 68   do not doubt th
e3630 61 74 20 69 74 20 6d 69 67 68 74 20 68 61 70 70  at it might happ
e3640 65 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 63  en.  If.** the c
e3650 6c 6f 73 65 20 66 61 69 6c 73 2c 20 77 65 20 70  lose fails, we p
e3660 61 75 73 65 20 66 6f 72 20 31 30 30 20 6d 69 6c  ause for 100 mil
e3670 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 20 74 72  liseconds and tr
e3680 79 20 61 67 61 69 6e 2e 20 20 41 73 0a 2a 2a 20  y again.  As.** 
e3690 6d 61 6e 79 20 61 73 20 4d 58 5f 43 4c 4f 53 45  many as MX_CLOSE
e36a0 5f 41 54 54 45 4d 50 54 20 61 74 74 65 6d 70 74  _ATTEMPT attempt
e36b0 73 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 68  s to close the h
e36c0 61 6e 64 6c 65 20 61 72 65 20 6d 61 64 65 20 62  andle are made b
e36d0 65 66 6f 72 65 0a 2a 2a 20 67 69 76 69 6e 67 20  efore.** giving 
e36e0 75 70 20 61 6e 64 20 72 65 74 75 72 6e 69 6e 67  up and returning
e36f0 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23 64   an error..*/.#d
e3700 65 66 69 6e 65 20 4d 58 5f 43 4c 4f 53 45 5f 41  efine MX_CLOSE_A
e3710 54 54 45 4d 50 54 20 33 0a 73 74 61 74 69 63 20  TTEMPT 3.static 
e3720 69 6e 74 20 77 69 6e 43 6c 6f 73 65 28 73 71 6c  int winClose(sql
e3730 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a  ite3_file *id){.
e3740 20 20 69 6e 74 20 72 63 2c 20 63 6e 74 20 3d 20    int rc, cnt = 
e3750 30 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46  0;.  winFile *pF
e3760 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29  ile = (winFile*)
e3770 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22  id;.  OSTRACE2("
e3780 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 70 46 69  CLOSE %d\n", pFi
e3790 6c 65 2d 3e 68 29 3b 0a 20 20 64 6f 7b 0a 20 20  le->h);.  do{.  
e37a0 20 20 72 63 20 3d 20 43 6c 6f 73 65 48 61 6e 64    rc = CloseHand
e37b0 6c 65 28 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20  le(pFile->h);.  
e37c0 7d 77 68 69 6c 65 28 20 72 63 3d 3d 30 20 26 26  }while( rc==0 &&
e37d0 20 2b 2b 63 6e 74 20 3c 20 4d 58 5f 43 4c 4f 53   ++cnt < MX_CLOS
e37e0 45 5f 41 54 54 45 4d 50 54 20 26 26 20 28 53 6c  E_ATTEMPT && (Sl
e37f0 65 65 70 28 31 30 30 29 2c 20 31 29 20 29 3b 0a  eep(100), 1) );.
e3800 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
e3810 4e 43 45 0a 23 64 65 66 69 6e 65 20 57 49 4e 43  NCE.#define WINC
e3820 45 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d  E_DELETION_ATTEM
e3830 50 54 53 20 33 0a 20 20 77 69 6e 63 65 44 65 73  PTS 3.  winceDes
e3840 74 72 6f 79 4c 6f 63 6b 28 70 46 69 6c 65 29 3b  troyLock(pFile);
e3850 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 7a 44  .  if( pFile->zD
e3860 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 20 29 7b 0a  eleteOnClose ){.
e3870 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b      int cnt = 0;
e3880 0a 20 20 20 20 77 68 69 6c 65 28 0a 20 20 20 20  .    while(.    
e3890 20 20 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c         DeleteFil
e38a0 65 57 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74  eW(pFile->zDelet
e38b0 65 4f 6e 43 6c 6f 73 65 29 3d 3d 30 0a 20 20 20  eOnClose)==0.   
e38c0 20 20 20 20 20 26 26 20 47 65 74 46 69 6c 65 41       && GetFileA
e38d0 74 74 72 69 62 75 74 65 73 57 28 70 46 69 6c 65  ttributesW(pFile
e38e0 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65  ->zDeleteOnClose
e38f0 29 21 3d 30 78 66 66 66 66 66 66 66 66 20 0a 20  )!=0xffffffff . 
e3900 20 20 20 20 20 20 20 26 26 20 63 6e 74 2b 2b 20         && cnt++ 
e3910 3c 20 57 49 4e 43 45 5f 44 45 4c 45 54 49 4f 4e  < WINCE_DELETION
e3920 5f 41 54 54 45 4d 50 54 53 0a 20 20 20 20 29 7b  _ATTEMPTS.    ){
e3930 0a 20 20 20 20 20 20 20 53 6c 65 65 70 28 31 30  .       Sleep(10
e3940 30 29 3b 20 20 2f 2a 20 57 61 69 74 20 61 20 6c  0);  /* Wait a l
e3950 69 74 74 6c 65 20 62 65 66 6f 72 65 20 74 72 79  ittle before try
e3960 69 6e 67 20 61 67 61 69 6e 20 2a 2f 0a 20 20 20  ing again */.   
e3970 20 7d 0a 20 20 20 20 66 72 65 65 28 70 46 69 6c   }.    free(pFil
e3980 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73  e->zDeleteOnClos
e3990 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  e);.  }.#endif. 
e39a0 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2d 31 29   OpenCounter(-1)
e39b0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20 3f 20  ;.  return rc ? 
e39c0 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49  SQLITE_OK : SQLI
e39d0 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a  TE_IOERR;.}../*.
e39e0 2a 2a 20 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66  ** Some microsof
e39f0 74 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b  t compilers lack
e3a00 20 74 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e   this definition
e3a10 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56  ..*/.#ifndef INV
e3a20 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f  ALID_SET_FILE_PO
e3a30 49 4e 54 45 52 0a 23 20 64 65 66 69 6e 65 20 49  INTER.# define I
e3a40 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f  NVALID_SET_FILE_
e3a50 50 4f 49 4e 54 45 52 20 28 28 44 57 4f 52 44 29  POINTER ((DWORD)
e3a60 2d 31 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  -1).#endif../*.*
e3a70 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d  * Read data from
e3a80 20 61 20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62   a file into a b
e3a90 75 66 66 65 72 2e 20 20 52 65 74 75 72 6e 20 53  uffer.  Return S
e3aa0 51 4c 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a  QLITE_OK if all.
e3ab0 2a 2a 20 62 79 74 65 73 20 77 65 72 65 20 72 65  ** bytes were re
e3ac0 61 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  ad successfully 
e3ad0 61 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  and SQLITE_IOERR
e3ae0 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65   if anything goe
e3af0 73 0a 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73  s.** wrong..*/.s
e3b00 74 61 74 69 63 20 69 6e 74 20 77 69 6e 52 65 61  tatic int winRea
e3b10 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  d(.  sqlite3_fil
e3b20 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20  e *id,          
e3b30 2f 2a 20 46 69 6c 65 20 74 6f 20 72 65 61 64 20  /* File to read 
e3b40 66 72 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  from */.  void *
e3b50 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20  pBuf,           
e3b60 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 6f       /* Write co
e3b70 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68 69 73 20  ntent into this 
e3b80 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20  buffer */.  int 
e3b90 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  amt,            
e3ba0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
e3bb0 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61   of bytes to rea
e3bc0 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69  d */.  sqlite3_i
e3bd0 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20 20 20  nt64 offset     
e3be0 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69    /* Begin readi
e3bf0 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 73 65  ng at this offse
e3c00 74 20 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75  t */.){.  LONG u
e3c10 70 70 65 72 42 69 74 73 20 3d 20 28 6f 66 66 73  pperBits = (offs
e3c20 65 74 3e 3e 33 32 29 20 26 20 30 78 37 66 66 66  et>>32) & 0x7fff
e3c30 66 66 66 66 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77  ffff;.  LONG low
e3c40 65 72 42 69 74 73 20 3d 20 6f 66 66 73 65 74 20  erBits = offset 
e3c50 26 20 30 78 66 66 66 66 66 66 66 66 3b 0a 20 20  & 0xffffffff;.  
e3c60 44 57 4f 52 44 20 72 63 3b 0a 20 20 44 57 4f 52  DWORD rc;.  DWOR
e3c70 44 20 67 6f 74 3b 0a 20 20 77 69 6e 46 69 6c 65  D got;.  winFile
e3c80 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69   *pFile = (winFi
e3c90 6c 65 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74  le*)id;.  assert
e3ca0 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d  ( id!=0 );.  Sim
e3cb0 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74  ulateIOError(ret
e3cc0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
e3cd0 5f 52 45 41 44 29 3b 0a 20 20 4f 53 54 52 41 43  _READ);.  OSTRAC
e3ce0 45 33 28 22 52 45 41 44 20 25 64 20 6c 6f 63 6b  E3("READ %d lock
e3cf0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  =%d\n", pFile->h
e3d00 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  , pFile->locktyp
e3d10 65 29 3b 0a 20 20 72 63 20 3d 20 53 65 74 46 69  e);.  rc = SetFi
e3d20 6c 65 50 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d  lePointer(pFile-
e3d30 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c 20 26  >h, lowerBits, &
e3d40 75 70 70 65 72 42 69 74 73 2c 20 46 49 4c 45 5f  upperBits, FILE_
e3d50 42 45 47 49 4e 29 3b 0a 20 20 69 66 28 20 72 63  BEGIN);.  if( rc
e3d60 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49  ==INVALID_SET_FI
e3d70 4c 45 5f 50 4f 49 4e 54 45 52 20 26 26 20 47 65  LE_POINTER && Ge
e3d80 74 4c 61 73 74 45 72 72 6f 72 28 29 21 3d 4e 4f  tLastError()!=NO
e3d90 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 65  _ERROR ){.    re
e3da0 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c  turn SQLITE_FULL
e3db0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 52 65 61  ;.  }.  if( !Rea
e3dc0 64 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20  dFile(pFile->h, 
e3dd0 70 42 75 66 2c 20 61 6d 74 2c 20 26 67 6f 74 2c  pBuf, amt, &got,
e3de0 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72   0) ){.    retur
e3df0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52  n SQLITE_IOERR_R
e3e00 45 41 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67  EAD;.  }.  if( g
e3e10 6f 74 3d 3d 28 44 57 4f 52 44 29 61 6d 74 20 29  ot==(DWORD)amt )
e3e20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
e3e30 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b  ITE_OK;.  }else{
e3e40 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 70  .    /* Unread p
e3e50 61 72 74 73 20 6f 66 20 74 68 65 20 62 75 66 66  arts of the buff
e3e60 65 72 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d  er must be zero-
e3e70 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 6d 65  filled */.    me
e3e80 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 70 42  mset(&((char*)pB
e3e90 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d 74  uf)[got], 0, amt
e3ea0 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65 74 75 72  -got);.    retur
e3eb0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53  n SQLITE_IOERR_S
e3ec0 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 0a 7d  HORT_READ;.  }.}
e3ed0 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61  ../*.** Write da
e3ee0 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72  ta from a buffer
e3ef0 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 52   into a file.  R
e3f00 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
e3f10 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72  on success.** or
e3f20 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f   some other erro
e3f30 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72  r code on failur
e3f40 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
e3f50 20 77 69 6e 57 72 69 74 65 28 0a 20 20 73 71 6c   winWrite(.  sql
e3f60 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20  ite3_file *id,  
e3f70 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74         /* File t
e3f80 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 2a 2f 0a  o write into */.
e3f90 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42    const void *pB
e3fa0 75 66 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  uf,         /* T
e3fb0 68 65 20 62 79 74 65 73 20 74 6f 20 62 65 20 77  he bytes to be w
e3fc0 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20  ritten */.  int 
e3fd0 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  amt,            
e3fe0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
e3ff0 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69 74  of bytes to writ
e4000 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69  e */.  sqlite3_i
e4010 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20 20 20  nt64 offset     
e4020 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20   /* Offset into 
e4030 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 67 69  the file to begi
e4040 6e 20 77 72 69 74 69 6e 67 20 61 74 20 2a 2f 0a  n writing at */.
e4050 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 72 42  ){.  LONG upperB
e4060 69 74 73 20 3d 20 28 6f 66 66 73 65 74 3e 3e 33  its = (offset>>3
e4070 32 29 20 26 20 30 78 37 66 66 66 66 66 66 66 3b  2) & 0x7fffffff;
e4080 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74  .  LONG lowerBit
e4090 73 20 3d 20 6f 66 66 73 65 74 20 26 20 30 78 66  s = offset & 0xf
e40a0 66 66 66 66 66 66 66 3b 0a 20 20 44 57 4f 52 44  fffffff;.  DWORD
e40b0 20 72 63 3b 0a 20 20 44 57 4f 52 44 20 77 72 6f   rc;.  DWORD wro
e40c0 74 65 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70  te;.  winFile *p
e40d0 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a  File = (winFile*
e40e0 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 69  )id;.  assert( i
e40f0 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61  d!=0 );.  Simula
e4100 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e  teIOError(return
e4110 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52   SQLITE_IOERR_WR
e4120 49 54 45 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65  ITE);.  Simulate
e4130 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 72 65  DiskfullError(re
e4140 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c  turn SQLITE_FULL
e4150 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 57  );.  OSTRACE3("W
e4160 52 49 54 45 20 25 64 20 6c 6f 63 6b 3d 25 64 5c  RITE %d lock=%d\
e4170 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46  n", pFile->h, pF
e4180 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a  ile->locktype);.
e4190 20 20 72 63 20 3d 20 53 65 74 46 69 6c 65 50 6f    rc = SetFilePo
e41a0 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c 20  inter(pFile->h, 
e41b0 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 70 70 65  lowerBits, &uppe
e41c0 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 47 49  rBits, FILE_BEGI
e41d0 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49 4e  N);.  if( rc==IN
e41e0 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50  VALID_SET_FILE_P
e41f0 4f 49 4e 54 45 52 20 26 26 20 47 65 74 4c 61 73  OINTER && GetLas
e4200 74 45 72 72 6f 72 28 29 21 3d 4e 4f 5f 45 52 52  tError()!=NO_ERR
e4210 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OR ){.    return
e4220 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20   SQLITE_FULL;.  
e4230 7d 0a 20 20 61 73 73 65 72 74 28 20 61 6d 74 3e  }.  assert( amt>
e4240 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 0a 20 20  0 );.  while(.  
e4250 20 20 20 61 6d 74 3e 30 0a 20 20 20 20 20 26 26     amt>0.     &&
e4260 20 28 72 63 20 3d 20 57 72 69 74 65 46 69 6c 65   (rc = WriteFile
e4270 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66 2c  (pFile->h, pBuf,
e4280 20 61 6d 74 2c 20 26 77 72 6f 74 65 2c 20 30 29   amt, &wrote, 0)
e4290 29 21 3d 30 0a 20 20 20 20 20 26 26 20 77 72 6f  )!=0.     && wro
e42a0 74 65 3e 30 0a 20 20 29 7b 0a 20 20 20 20 61 6d  te>0.  ){.    am
e42b0 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20  t -= wrote;.    
e42c0 70 42 75 66 20 3d 20 26 28 28 63 68 61 72 2a 29  pBuf = &((char*)
e42d0 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20  pBuf)[wrote];.  
e42e0 7d 0a 20 20 69 66 28 20 21 72 63 20 7c 7c 20 61  }.  if( !rc || a
e42f0 6d 74 3e 28 69 6e 74 29 77 72 6f 74 65 20 29 7b  mt>(int)wrote ){
e4300 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
e4310 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a 20 20 72  TE_FULL;.  }.  r
e4320 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
e4330 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61  .}../*.** Trunca
e4340 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20  te an open file 
e4350 74 6f 20 61 20 73 70 65 63 69 66 69 65 64 20 73  to a specified s
e4360 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ize.*/.static in
e4370 74 20 77 69 6e 54 72 75 6e 63 61 74 65 28 73 71  t winTruncate(sq
e4380 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
e4390 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 42  sqlite3_int64 nB
e43a0 79 74 65 29 7b 0a 20 20 44 57 4f 52 44 20 72 63  yte){.  DWORD rc
e43b0 3b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 72 42 69  ;.  LONG upperBi
e43c0 74 73 20 3d 20 28 6e 42 79 74 65 3e 3e 33 32 29  ts = (nByte>>32)
e43d0 20 26 20 30 78 37 66 66 66 66 66 66 66 3b 0a 20   & 0x7fffffff;. 
e43e0 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 20   LONG lowerBits 
e43f0 3d 20 6e 42 79 74 65 20 26 20 30 78 66 66 66 66  = nByte & 0xffff
e4400 66 66 66 66 3b 0a 20 20 77 69 6e 46 69 6c 65 20  ffff;.  winFile 
e4410 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c  *pFile = (winFil
e4420 65 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45  e*)id;.  OSTRACE
e4430 33 28 22 54 52 55 4e 43 41 54 45 20 25 64 20 25  3("TRUNCATE %d %
e4440 6c 6c 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  lld\n", pFile->h
e4450 2c 20 6e 42 79 74 65 29 3b 0a 20 20 53 69 6d 75  , nByte);.  Simu
e4460 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75  lateIOError(retu
e4470 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
e4480 54 52 55 4e 43 41 54 45 29 3b 0a 20 20 72 63 20  TRUNCATE);.  rc 
e4490 3d 20 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72  = SetFilePointer
e44a0 28 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72  (pFile->h, lower
e44b0 42 69 74 73 2c 20 26 75 70 70 65 72 42 69 74 73  Bits, &upperBits
e44c0 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20  , FILE_BEGIN);. 
e44d0 20 69 66 28 20 49 4e 56 41 4c 49 44 5f 53 45 54   if( INVALID_SET
e44e0 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 21 3d  _FILE_POINTER !=
e44f0 20 72 63 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65   rc ){.    /* Se
e4500 74 45 6e 64 4f 66 46 69 6c 65 20 77 69 6c 6c 20  tEndOfFile will 
e4510 66 61 69 6c 20 69 66 20 6e 42 79 74 65 20 69 73  fail if nByte is
e4520 20 6e 65 67 61 74 69 76 65 20 2a 2f 0a 20 20 20   negative */.   
e4530 20 69 66 28 20 53 65 74 45 6e 64 4f 66 46 69 6c   if( SetEndOfFil
e4540 65 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20  e(pFile->h) ){. 
e4550 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
e4560 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d  TE_OK;.    }.  }
e4570 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
e4580 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b  _IOERR_TRUNCATE;
e4590 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .}..#ifdef SQLIT
e45a0 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75  E_TEST./*.** Cou
e45b0 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  nt the number of
e45c0 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e   fullsyncs and n
e45d0 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54 68  ormal syncs.  Th
e45e0 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 74 65  is is used to te
e45f0 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e 63 73  st.** that syncs
e4600 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73 20 61   and fullsyncs a
e4610 72 65 20 6f 63 63 75 72 69 6e 67 20 61 74 20 74  re occuring at t
e4620 68 65 20 72 69 67 68 74 20 74 69 6d 65 73 2e 0a  he right times..
e4630 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
e4640 74 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63  t sqlite3_sync_c
e4650 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45  ount = 0;.SQLITE
e4660 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
e4670 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20  _fullsync_count 
e4680 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  = 0;.#endif../*.
e4690 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c  ** Make sure all
e46a0 20 77 72 69 74 65 73 20 74 6f 20 61 20 70 61 72   writes to a par
e46b0 74 69 63 75 6c 61 72 20 66 69 6c 65 20 61 72 65  ticular file are
e46c0 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f 20 64 69   committed to di
e46d0 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  sk..*/.static in
e46e0 74 20 77 69 6e 53 79 6e 63 28 73 71 6c 69 74 65  t winSync(sqlite
e46f0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
e4700 66 6c 61 67 73 29 7b 0a 20 20 77 69 6e 46 69 6c  flags){.  winFil
e4710 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46  e *pFile = (winF
e4720 69 6c 65 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41  ile*)id;.  OSTRA
e4730 43 45 33 28 22 53 59 4e 43 20 25 64 20 6c 6f 63  CE3("SYNC %d loc
e4740 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  k=%d\n", pFile->
e4750 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  h, pFile->lockty
e4760 70 65 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  pe);.#ifdef SQLI
e4770 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 6c  TE_TEST.  if( fl
e4780 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e  ags & SQLITE_SYN
e4790 43 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 73 71  C_FULL ){.    sq
e47a0 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63  lite3_fullsync_c
e47b0 6f 75 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71  ount++;.  }.  sq
e47c0 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74  lite3_sync_count
e47d0 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28  ++;.#endif.  if(
e47e0 20 46 6c 75 73 68 46 69 6c 65 42 75 66 66 65 72   FlushFileBuffer
e47f0 73 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20  s(pFile->h) ){. 
e4800 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
e4810 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  _OK;.  }else{.  
e4820 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e4830 49 4f 45 52 52 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  IOERR;.  }.}../*
e4840 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68  .** Determine th
e4850 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f  e current size o
e4860 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65  f a file in byte
e4870 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  s.*/.static int 
e4880 77 69 6e 46 69 6c 65 53 69 7a 65 28 73 71 6c 69  winFileSize(sqli
e4890 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71  te3_file *id, sq
e48a0 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69  lite3_int64 *pSi
e48b0 7a 65 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a  ze){.  winFile *
e48c0 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65  pFile = (winFile
e48d0 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 75 70  *)id;.  DWORD up
e48e0 70 65 72 42 69 74 73 2c 20 6c 6f 77 65 72 42 69  perBits, lowerBi
e48f0 74 73 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f  ts;.  SimulateIO
e4900 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c  Error(return SQL
e4910 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 29  ITE_IOERR_FSTAT)
e4920 3b 0a 20 20 6c 6f 77 65 72 42 69 74 73 20 3d 20  ;.  lowerBits = 
e4930 47 65 74 46 69 6c 65 53 69 7a 65 28 70 46 69 6c  GetFileSize(pFil
e4940 65 2d 3e 68 2c 20 26 75 70 70 65 72 42 69 74 73  e->h, &upperBits
e4950 29 3b 0a 20 20 2a 70 53 69 7a 65 20 3d 20 28 28  );.  *pSize = ((
e4960 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 75  (sqlite3_int64)u
e4970 70 70 65 72 42 69 74 73 29 3c 3c 33 32 29 20 2b  pperBits)<<32) +
e4980 20 6c 6f 77 65 72 42 69 74 73 3b 0a 20 20 72 65   lowerBits;.  re
e4990 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
e49a0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 4f 43 4b 46 49 4c  }../*.** LOCKFIL
e49b0 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45  E_FAIL_IMMEDIATE
e49c0 4c 59 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20  LY is undefined 
e49d0 6f 6e 20 73 6f 6d 65 20 57 69 6e 64 6f 77 73 20  on some Windows 
e49e0 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e  systems..*/.#ifn
e49f0 64 65 66 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49  def LOCKFILE_FAI
e4a00 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 0a 23 20  L_IMMEDIATELY.# 
e4a10 64 65 66 69 6e 65 20 4c 4f 43 4b 46 49 4c 45 5f  define LOCKFILE_
e4a20 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59  FAIL_IMMEDIATELY
e4a30 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a   1.#endif../*.**
e4a40 20 41 63 71 75 69 72 65 20 61 20 72 65 61 64 65   Acquire a reade
e4a50 72 20 6c 6f 63 6b 2e 0a 2a 2a 20 44 69 66 66 65  r lock..** Diffe
e4a60 72 65 6e 74 20 41 50 49 20 72 6f 75 74 69 6e 65  rent API routine
e4a70 73 20 61 72 65 20 63 61 6c 6c 65 64 20 64 65 70  s are called dep
e4a80 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65  ending on whethe
e4a90 72 20 6f 72 20 6e 6f 74 20 74 68 69 73 0a 2a 2a  r or not this.**
e4aa0 20 69 73 20 57 69 6e 39 35 20 6f 72 20 57 69 6e   is Win95 or Win
e4ab0 4e 54 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  NT..*/.static in
e4ac0 74 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 77 69  t getReadLock(wi
e4ad0 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20  nFile *pFile){. 
e4ae0 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 66 28 20   int res;.  if( 
e4af0 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 4f 56  isNT() ){.    OV
e4b00 45 52 4c 41 50 50 45 44 20 6f 76 6c 70 3b 0a 20  ERLAPPED ovlp;. 
e4b10 20 20 20 6f 76 6c 70 2e 4f 66 66 73 65 74 20 3d     ovlp.Offset =
e4b20 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20   SHARED_FIRST;. 
e4b30 20 20 20 6f 76 6c 70 2e 4f 66 66 73 65 74 48 69     ovlp.OffsetHi
e4b40 67 68 20 3d 20 30 3b 0a 20 20 20 20 6f 76 6c 70  gh = 0;.    ovlp
e4b50 2e 68 45 76 65 6e 74 20 3d 20 30 3b 0a 20 20 20  .hEvent = 0;.   
e4b60 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 45   res = LockFileE
e4b70 78 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b  x(pFile->h, LOCK
e4b80 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49  FILE_FAIL_IMMEDI
e4b90 41 54 45 4c 59 2c 0a 20 20 20 20 20 20 20 20 20  ATELY,.         
e4ba0 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 53              0, S
e4bb0 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 2c 20 26  HARED_SIZE, 0, &
e4bc0 6f 76 6c 70 29 3b 0a 2f 2a 20 69 73 4e 54 28 29  ovlp);./* isNT()
e4bd0 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f   is 1 if SQLITE_
e4be0 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20  OS_WINCE==1, so 
e4bf0 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76  this else is nev
e4c00 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2f  er executed. .*/
e4c10 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
e4c20 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b  INCE==0.  }else{
e4c30 0a 20 20 20 20 69 6e 74 20 6c 6b 3b 0a 20 20 20  .    int lk;.   
e4c40 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
e4c50 65 73 73 28 73 69 7a 65 6f 66 28 6c 6b 29 2c 20  ess(sizeof(lk), 
e4c60 26 6c 6b 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d  &lk);.    pFile-
e4c70 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 20  >sharedLockByte 
e4c80 3d 20 28 6c 6b 20 26 20 30 78 37 66 66 66 66 66  = (lk & 0x7fffff
e4c90 66 66 29 25 28 53 48 41 52 45 44 5f 53 49 5a 45  ff)%(SHARED_SIZE
e4ca0 20 2d 20 31 29 3b 0a 20 20 20 20 72 65 73 20 3d   - 1);.    res =
e4cb0 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d   LockFile(pFile-
e4cc0 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54  >h, SHARED_FIRST
e4cd0 2b 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f  +pFile->sharedLo
e4ce0 63 6b 42 79 74 65 2c 20 30 2c 20 31 2c 20 30 29  ckByte, 0, 1, 0)
e4cf0 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72  ;.#endif.  }.  r
e4d00 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a  eturn res;.}../*
e4d10 0a 2a 2a 20 55 6e 64 6f 20 61 20 72 65 61 64 6c  .** Undo a readl
e4d20 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ock.*/.static in
e4d30 74 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b  t unlockReadLock
e4d40 28 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29  (winFile *pFile)
e4d50 7b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69  {.  int res;.  i
e4d60 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20  f( isNT() ){.   
e4d70 20 72 65 73 20 3d 20 55 6e 6c 6f 63 6b 46 69 6c   res = UnlockFil
e4d80 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52  e(pFile->h, SHAR
e4d90 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41  ED_FIRST, 0, SHA
e4da0 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 2f 2a  RED_SIZE, 0);./*
e4db0 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20   isNT() is 1 if 
e4dc0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d  SQLITE_OS_WINCE=
e4dd0 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65  =1, so this else
e4de0 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74   is never execut
e4df0 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 51 4c 49  ed. .*/.#if SQLI
e4e00 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20  TE_OS_WINCE==0. 
e4e10 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 73 20   }else{.    res 
e4e20 3d 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69  = UnlockFile(pFi
e4e30 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49  le->h, SHARED_FI
e4e40 52 53 54 20 2b 20 70 46 69 6c 65 2d 3e 73 68 61  RST + pFile->sha
e4e50 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c 20  redLockByte, 0, 
e4e60 31 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20  1, 0);.#endif.  
e4e70 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a  }.  return res;.
e4e80 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68  }../*.** Lock th
e4e90 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20  e file with the 
e4ea0 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62  lock specified b
e4eb0 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b  y parameter lock
e4ec0 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66  type - one.** of
e4ed0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
e4ee0 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48  **.**     (1) SH
e4ef0 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  ARED_LOCK.**    
e4f00 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f   (2) RESERVED_LO
e4f10 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45  CK.**     (3) PE
e4f20 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  NDING_LOCK.**   
e4f30 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f    (4) EXCLUSIVE_
e4f40 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74  LOCK.**.** Somet
e4f50 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73  imes when reques
e4f60 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74  ting one lock st
e4f70 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20  ate, additional 
e4f80 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61  lock states.** a
e4f90 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62  re inserted in b
e4fa0 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63  etween.  The loc
e4fb0 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20  king might fail 
e4fc0 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61  on one of the la
e4fd0 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  ter.** transitio
e4fe0 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c  ns leaving the l
e4ff0 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72  ock state differ
e5000 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74  ent from what it
e5010 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20   started but.** 
e5020 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69  still short of i
e5030 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f  ts goal.  The fo
e5040 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68  llowing chart sh
e5050 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a  ows the allowed.
e5060 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61  ** transitions a
e5070 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20  nd the inserted 
e5080 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61  intermediate sta
e5090 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e  tes:.**.**    UN
e50a0 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44  LOCKED -> SHARED
e50b0 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e  .**    SHARED ->
e50c0 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20   RESERVED.**    
e50d0 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49  SHARED -> (PENDI
e50e0 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  NG) -> EXCLUSIVE
e50f0 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20  .**    RESERVED 
e5100 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20  -> (PENDING) -> 
e5110 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20  EXCLUSIVE.**    
e5120 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55  PENDING -> EXCLU
e5130 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  SIVE.**.** This 
e5140 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c  routine will onl
e5150 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63  y increase a loc
e5160 6b 2e 20 20 54 68 65 20 77 69 6e 55 6e 6c 6f 63  k.  The winUnloc
e5170 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65  k() routine.** e
e5180 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63 6b 73 20  rases all locks 
e5190 61 74 20 6f 6e 63 65 20 61 6e 64 20 72 65 74 75  at once and retu
e51a0 72 6e 73 20 75 73 20 69 6d 6d 65 64 69 61 74 65  rns us immediate
e51b0 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65  ly to locking le
e51c0 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20 69 73 20  vel 0..** It is 
e51d0 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20  not possible to 
e51e0 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e  lower the lockin
e51f0 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73 74 65 70  g level one step
e5200 20 61 74 20 61 20 74 69 6d 65 2e 20 20 59 6f 75   at a time.  You
e5210 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73 74 72 61  .** must go stra
e5220 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69 6e 67 20  ight to locking 
e5230 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61 74  level 0..*/.stat
e5240 69 63 20 69 6e 74 20 77 69 6e 4c 6f 63 6b 28 73  ic int winLock(s
e5250 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
e5260 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a   int locktype){.
e5270 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
e5280 45 5f 4f 4b 3b 20 20 20 20 2f 2a 20 52 65 74 75  E_OK;    /* Retu
e5290 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 62  rn code from sub
e52a0 72 6f 75 74 69 6e 65 73 20 2a 2f 0a 20 20 69 6e  routines */.  in
e52b0 74 20 72 65 73 20 3d 20 31 3b 20 20 20 20 20 20  t res = 1;      
e52c0 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f       /* Result o
e52d0 66 20 61 20 77 69 6e 64 6f 77 73 20 6c 6f 63 6b  f a windows lock
e52e0 20 63 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e   call */.  int n
e52f0 65 77 4c 6f 63 6b 74 79 70 65 3b 20 20 20 20 20  ewLocktype;     
e5300 20 20 2f 2a 20 53 65 74 20 70 46 69 6c 65 2d 3e    /* Set pFile->
e5310 6c 6f 63 6b 74 79 70 65 20 74 6f 20 74 68 69 73  locktype to this
e5320 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 65 78   value before ex
e5330 69 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 67  iting */.  int g
e5340 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20  otPendingLock = 
e5350 30 3b 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20  0;/* True if we 
e5360 61 63 71 75 69 72 65 64 20 61 20 50 45 4e 44 49  acquired a PENDI
e5370 4e 47 20 6c 6f 63 6b 20 74 68 69 73 20 74 69 6d  NG lock this tim
e5380 65 20 2a 2f 0a 20 20 77 69 6e 46 69 6c 65 20 2a  e */.  winFile *
e5390 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65  pFile = (winFile
e53a0 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28  *)id;..  assert(
e53b0 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 4f   pFile!=0 );.  O
e53c0 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 25 64  STRACE5("LOCK %d
e53d0 20 25 64 20 77 61 73 20 25 64 28 25 64 29 5c 6e   %d was %d(%d)\n
e53e0 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 46 69  ",.          pFi
e53f0 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c  le->h, locktype,
e5400 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
e5410 2c 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c  , pFile->sharedL
e5420 6f 63 6b 42 79 74 65 29 3b 0a 0a 20 20 2f 2a 20  ockByte);..  /* 
e5430 49 66 20 74 68 65 72 65 20 69 73 20 61 6c 72 65  If there is alre
e5440 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68  ady a lock of th
e5450 69 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 20  is type or more 
e5460 72 65 73 74 72 69 63 74 69 76 65 20 6f 6e 20 74  restrictive on t
e5470 68 65 0a 20 20 2a 2a 20 4f 73 46 69 6c 65 2c 20  he.  ** OsFile, 
e5480 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27  do nothing. Don'
e5490 74 20 75 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f  t use the end_lo
e54a0 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61  ck: exit path, a
e54b0 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 4f 73  s.  ** sqlite3Os
e54c0 45 6e 74 65 72 4d 75 74 65 78 28 29 20 68 61 73  EnterMutex() has
e54d0 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20  n't been called 
e54e0 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  yet..  */.  if( 
e54f0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e  pFile->locktype>
e5500 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20  =locktype ){.   
e5510 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
e5520 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b  K;.  }..  /* Mak
e5530 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69  e sure the locki
e5540 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63  ng sequence is c
e5550 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73  orrect.  */.  as
e5560 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63  sert( pFile->loc
e5570 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c  ktype!=NO_LOCK |
e5580 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  | locktype==SHAR
e5590 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73  ED_LOCK );.  ass
e55a0 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50  ert( locktype!=P
e55b0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20  ENDING_LOCK );. 
e55c0 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
e55d0 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e!=RESERVED_LOCK
e55e0 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74   || pFile->lockt
e55f0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
e5600 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74   );..  /* Lock t
e5610 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20  he PENDING_LOCK 
e5620 62 79 74 65 20 69 66 20 77 65 20 6e 65 65 64 20  byte if we need 
e5630 74 6f 20 61 63 71 75 69 72 65 20 61 20 50 45 4e  to acquire a PEN
e5640 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 20 2a  DING lock or.  *
e5650 2a 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e  * a SHARED lock.
e5660 20 20 49 66 20 77 65 20 61 72 65 20 61 63 71 75    If we are acqu
e5670 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c  iring a SHARED l
e5680 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 69 73 69  ock, the acquisi
e5690 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 68 65  tion of.  ** the
e56a0 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79   PENDING_LOCK by
e56b0 74 65 20 69 73 20 74 65 6d 70 6f 72 61 72 79 2e  te is temporary.
e56c0 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 6b 74  .  */.  newLockt
e56d0 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63  ype = pFile->loc
e56e0 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 70 46 69  ktype;.  if( pFi
e56f0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f  le->locktype==NO
e5700 5f 4c 4f 43 4b 0a 20 20 20 7c 7c 20 28 6c 6f 63  _LOCK.   || (loc
e5710 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45  ktype==EXCLUSIVE
e5720 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e  _LOCK && pFile->
e5730 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56  locktype==RESERV
e5740 45 44 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20  ED_LOCK).  ){.  
e5750 20 20 69 6e 74 20 63 6e 74 20 3d 20 33 3b 0a 20    int cnt = 3;. 
e5760 20 20 20 77 68 69 6c 65 28 20 63 6e 74 2d 2d 3e     while( cnt-->
e5770 30 20 26 26 20 28 72 65 73 20 3d 20 4c 6f 63 6b  0 && (res = Lock
e5780 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50  File(pFile->h, P
e5790 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20  ENDING_BYTE, 0, 
e57a0 31 2c 20 30 29 29 3d 3d 30 20 29 7b 0a 20 20 20  1, 0))==0 ){.   
e57b0 20 20 20 2f 2a 20 54 72 79 20 33 20 74 69 6d 65     /* Try 3 time
e57c0 73 20 74 6f 20 67 65 74 20 74 68 65 20 70 65 6e  s to get the pen
e57d0 64 69 6e 67 20 6c 6f 63 6b 2e 20 20 54 68 65 20  ding lock.  The 
e57e0 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 6d 69 67  pending lock mig
e57f0 68 74 20 62 65 0a 20 20 20 20 20 20 2a 2a 20 68  ht be.      ** h
e5800 65 6c 64 20 62 79 20 61 6e 6f 74 68 65 72 20 72  eld by another r
e5810 65 61 64 65 72 20 70 72 6f 63 65 73 73 20 77 68  eader process wh
e5820 6f 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20 69  o will release i
e5830 74 20 6d 6f 6d 65 6e 74 61 72 69 6c 79 2e 0a 20  t momentarily.. 
e5840 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 4f 53       */.      OS
e5850 54 52 41 43 45 32 28 22 63 6f 75 6c 64 20 6e 6f  TRACE2("could no
e5860 74 20 67 65 74 20 61 20 50 45 4e 44 49 4e 47 20  t get a PENDING 
e5870 6c 6f 63 6b 2e 20 63 6e 74 3d 25 64 5c 6e 22 2c  lock. cnt=%d\n",
e5880 20 63 6e 74 29 3b 0a 20 20 20 20 20 20 53 6c 65   cnt);.      Sle
e5890 65 70 28 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20  ep(1);.    }.   
e58a0 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20   gotPendingLock 
e58b0 3d 20 72 65 73 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  = res;.  }..  /*
e58c0 20 41 63 71 75 69 72 65 20 61 20 73 68 61 72 65   Acquire a share
e58d0 64 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66  d lock.  */.  if
e58e0 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  ( locktype==SHAR
e58f0 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29  ED_LOCK && res )
e5900 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46  {.    assert( pF
e5910 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e  ile->locktype==N
e5920 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65  O_LOCK );.    re
e5930 73 20 3d 20 67 65 74 52 65 61 64 4c 6f 63 6b 28  s = getReadLock(
e5940 70 46 69 6c 65 29 3b 0a 20 20 20 20 69 66 28 20  pFile);.    if( 
e5950 72 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 77  res ){.      new
e5960 4c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45  Locktype = SHARE
e5970 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20  D_LOCK;.    }.  
e5980 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20  }..  /* Acquire 
e5990 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 0a  a RESERVED lock.
e59a0 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74    */.  if( lockt
e59b0 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f  ype==RESERVED_LO
e59c0 43 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20  CK && res ){.   
e59d0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e   assert( pFile->
e59e0 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
e59f0 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73  _LOCK );.    res
e5a00 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c   = LockFile(pFil
e5a10 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42  e->h, RESERVED_B
e5a20 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20  YTE, 0, 1, 0);. 
e5a30 20 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20     if( res ){.  
e5a40 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20      newLocktype 
e5a50 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3b  = RESERVED_LOCK;
e5a60 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
e5a70 20 41 63 71 75 69 72 65 20 61 20 50 45 4e 44 49   Acquire a PENDI
e5a80 4e 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69  NG lock.  */.  i
e5a90 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43  f( locktype==EXC
e5aa0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72  LUSIVE_LOCK && r
e5ab0 65 73 20 29 7b 0a 20 20 20 20 6e 65 77 4c 6f 63  es ){.    newLoc
e5ac0 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f  ktype = PENDING_
e5ad0 4c 4f 43 4b 3b 0a 20 20 20 20 67 6f 74 50 65 6e  LOCK;.    gotPen
e5ae0 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20  dingLock = 0;.  
e5af0 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20  }..  /* Acquire 
e5b00 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  an EXCLUSIVE loc
e5b10 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63  k.  */.  if( loc
e5b20 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45  ktype==EXCLUSIVE
e5b30 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a  _LOCK && res ){.
e5b40 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c      assert( pFil
e5b50 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41  e->locktype>=SHA
e5b60 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20  RED_LOCK );.    
e5b70 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64  res = unlockRead
e5b80 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20  Lock(pFile);.   
e5b90 20 4f 53 54 52 41 43 45 32 28 22 75 6e 72 65 61   OSTRACE2("unrea
e5ba0 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72  dlock = %d\n", r
e5bb0 65 73 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c  es);.    res = L
e5bc0 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68  ockFile(pFile->h
e5bd0 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20  , SHARED_FIRST, 
e5be0 30 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20  0, SHARED_SIZE, 
e5bf0 30 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20  0);.    if( res 
e5c00 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b  ){.      newLock
e5c10 74 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45  type = EXCLUSIVE
e5c20 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65  _LOCK;.    }else
e5c30 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32  {.      OSTRACE2
e5c40 28 22 65 72 72 6f 72 2d 63 6f 64 65 20 3d 20 25  ("error-code = %
e5c50 64 5c 6e 22 2c 20 47 65 74 4c 61 73 74 45 72 72  d\n", GetLastErr
e5c60 6f 72 28 29 29 3b 0a 20 20 20 20 20 20 67 65 74  or());.      get
e5c70 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b  ReadLock(pFile);
e5c80 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
e5c90 20 49 66 20 77 65 20 61 72 65 20 68 6f 6c 64 69   If we are holdi
e5ca0 6e 67 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  ng a PENDING loc
e5cb0 6b 20 74 68 61 74 20 6f 75 67 68 74 20 74 6f 20  k that ought to 
e5cc0 62 65 20 72 65 6c 65 61 73 65 64 2c 20 74 68 65  be released, the
e5cd0 6e 0a 20 20 2a 2a 20 72 65 6c 65 61 73 65 20 69  n.  ** release i
e5ce0 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66  t now..  */.  if
e5cf0 28 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b  ( gotPendingLock
e5d00 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48   && locktype==SH
e5d10 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  ARED_LOCK ){.   
e5d20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c   UnlockFile(pFil
e5d30 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42 59  e->h, PENDING_BY
e5d40 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20  TE, 0, 1, 0);.  
e5d50 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74  }..  /* Update t
e5d60 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20  he state of the 
e5d70 6c 6f 63 6b 20 68 61 73 20 68 65 6c 64 20 69 6e  lock has held in
e5d80 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
e5d90 70 74 6f 72 20 74 68 65 6e 0a 20 20 2a 2a 20 72  ptor then.  ** r
e5da0 65 74 75 72 6e 20 74 68 65 20 61 70 70 72 6f 70  eturn the approp
e5db0 72 69 61 74 65 20 72 65 73 75 6c 74 20 63 6f 64  riate result cod
e5dc0 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 65  e..  */.  if( re
e5dd0 73 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  s ){.    rc = SQ
e5de0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65  LITE_OK;.  }else
e5df0 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28 22  {.    OSTRACE4("
e5e00 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25 64 20 74  LOCK FAILED %d t
e5e10 72 79 69 6e 67 20 66 6f 72 20 25 64 20 62 75 74  rying for %d but
e5e20 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70 46 69 6c   got %d\n", pFil
e5e30 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20  e->h,.          
e5e40 20 6c 6f 63 6b 74 79 70 65 2c 20 6e 65 77 4c 6f   locktype, newLo
e5e50 63 6b 74 79 70 65 29 3b 0a 20 20 20 20 72 63 20  cktype);.    rc 
e5e60 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  = SQLITE_BUSY;. 
e5e70 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   }.  pFile->lock
e5e80 74 79 70 65 20 3d 20 6e 65 77 4c 6f 63 6b 74 79  type = newLockty
e5e90 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  pe;.  return rc;
e5ea0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
e5eb0 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66  outine checks if
e5ec0 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45   there is a RESE
e5ed0 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f  RVED lock held o
e5ee0 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a  n the specified.
e5ef0 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20  ** file by this 
e5f00 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f  or any other pro
e5f10 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20  cess. If such a 
e5f20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65  lock is held, re
e5f30 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f  turn.** non-zero
e5f40 2c 20 6f 74 68 65 72 77 69 73 65 20 7a 65 72 6f  , otherwise zero
e5f50 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
e5f60 77 69 6e 43 68 65 63 6b 52 65 73 65 72 76 65 64  winCheckReserved
e5f70 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  Lock(sqlite3_fil
e5f80 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73  e *id, int *pRes
e5f90 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Out){.  int rc;.
e5fa0 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65    winFile *pFile
e5fb0 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b   = (winFile*)id;
e5fc0 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
e5fd0 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 46 69  !=0 );.  if( pFi
e5fe0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52 45  le->locktype>=RE
e5ff0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SERVED_LOCK ){. 
e6000 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 4f     rc = 1;.    O
e6010 53 54 52 41 43 45 33 28 22 54 45 53 54 20 57 52  STRACE3("TEST WR
e6020 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 6c 6f 63  -LOCK %d %d (loc
e6030 61 6c 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  al)\n", pFile->h
e6040 2c 20 72 63 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  , rc);.  }else{.
e6050 20 20 20 20 72 63 20 3d 20 4c 6f 63 6b 46 69 6c      rc = LockFil
e6060 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45  e(pFile->h, RESE
e6070 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c  RVED_BYTE, 0, 1,
e6080 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20   0);.    if( rc 
e6090 29 7b 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b 46  ){.      UnlockF
e60a0 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45  ile(pFile->h, RE
e60b0 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20  SERVED_BYTE, 0, 
e60c0 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20  1, 0);.    }.   
e60d0 20 72 63 20 3d 20 21 72 63 3b 0a 20 20 20 20 4f   rc = !rc;.    O
e60e0 53 54 52 41 43 45 33 28 22 54 45 53 54 20 57 52  STRACE3("TEST WR
e60f0 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 72 65 6d  -LOCK %d %d (rem
e6100 6f 74 65 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  ote)\n", pFile->
e6110 68 2c 20 72 63 29 3b 0a 20 20 7d 0a 20 20 2a 70  h, rc);.  }.  *p
e6120 52 65 73 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72  ResOut = rc;.  r
e6130 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
e6140 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20  .}../*.** Lower 
e6150 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  the locking leve
e6160 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69  l on file descri
e6170 70 74 6f 72 20 69 64 20 74 6f 20 6c 6f 63 6b 74  ptor id to lockt
e6180 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a  ype.  locktype.*
e6190 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72  * must be either
e61a0 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52   NO_LOCK or SHAR
e61b0 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49  ED_LOCK..**.** I
e61c0 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65  f the locking le
e61d0 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20  vel of the file 
e61e0 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c  descriptor is al
e61f0 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f  ready at or belo
e6200 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74  w.** the request
e6210 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  ed locking level
e6220 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  , this routine i
e6230 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a  s a no-op..**.**
e6240 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69   It is not possi
e6250 62 6c 65 20 66 6f 72 20 74 68 69 73 20 72 6f 75  ble for this rou
e6260 74 69 6e 65 20 74 6f 20 66 61 69 6c 20 69 66 20  tine to fail if 
e6270 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
e6280 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43  ent.** is NO_LOC
e6290 4b 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f 6e  K.  If the secon
e62a0 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 48  d argument is SH
e62b0 41 52 45 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 74  ARED_LOCK then t
e62c0 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d  his routine.** m
e62d0 69 67 68 74 20 72 65 74 75 72 6e 20 53 51 4c 49  ight return SQLI
e62e0 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61  TE_IOERR;.*/.sta
e62f0 74 69 63 20 69 6e 74 20 77 69 6e 55 6e 6c 6f 63  tic int winUnloc
e6300 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
e6310 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
e6320 29 7b 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a 20  ){.  int type;. 
e6330 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20   winFile *pFile 
e6340 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a  = (winFile*)id;.
e6350 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
e6360 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20  E_OK;.  assert( 
e6370 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 61 73  pFile!=0 );.  as
e6380 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d  sert( locktype<=
e6390 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20  SHARED_LOCK );. 
e63a0 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43   OSTRACE5("UNLOC
e63b0 4b 20 25 64 20 74 6f 20 25 64 20 77 61 73 20 25  K %d to %d was %
e63c0 64 28 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d  d(%d)\n", pFile-
e63d0 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 20  >h, locktype,.  
e63e0 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c          pFile->l
e63f0 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e  ocktype, pFile->
e6400 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 29 3b  sharedLockByte);
e6410 0a 20 20 74 79 70 65 20 3d 20 70 46 69 6c 65 2d  .  type = pFile-
e6420 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28  >locktype;.  if(
e6430 20 74 79 70 65 3e 3d 45 58 43 4c 55 53 49 56 45   type>=EXCLUSIVE
e6440 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c  _LOCK ){.    Unl
e6450 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68  ockFile(pFile->h
e6460 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20  , SHARED_FIRST, 
e6470 30 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20  0, SHARED_SIZE, 
e6480 30 29 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b  0);.    if( lock
e6490 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
e64a0 4b 20 26 26 20 21 67 65 74 52 65 61 64 4c 6f 63  K && !getReadLoc
e64b0 6b 28 70 46 69 6c 65 29 20 29 7b 0a 20 20 20 20  k(pFile) ){.    
e64c0 20 20 2f 2a 20 54 68 69 73 20 73 68 6f 75 6c 64    /* This should
e64d0 20 6e 65 76 65 72 20 68 61 70 70 65 6e 2e 20 20   never happen.  
e64e0 57 65 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73  We should always
e64f0 20 62 65 20 61 62 6c 65 20 74 6f 0a 20 20 20 20   be able to.    
e6500 20 20 2a 2a 20 72 65 61 63 71 75 69 72 65 20 74    ** reacquire t
e6510 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a  he read lock */.
e6520 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
e6530 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 0a  E_IOERR_UNLOCK;.
e6540 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
e6550 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c  type>=RESERVED_L
e6560 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63  OCK ){.    Unloc
e6570 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20  kFile(pFile->h, 
e6580 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30  RESERVED_BYTE, 0
e6590 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69  , 1, 0);.  }.  i
e65a0 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f  f( locktype==NO_
e65b0 4c 4f 43 4b 20 26 26 20 74 79 70 65 3e 3d 53 48  LOCK && type>=SH
e65c0 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  ARED_LOCK ){.   
e65d0 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28   unlockReadLock(
e65e0 70 46 69 6c 65 29 3b 0a 20 20 7d 0a 20 20 69 66  pFile);.  }.  if
e65f0 28 20 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f  ( type>=PENDING_
e6600 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f  LOCK ){.    Unlo
e6610 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c  ckFile(pFile->h,
e6620 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30   PENDING_BYTE, 0
e6630 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70  , 1, 0);.  }.  p
e6640 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
e6650 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 74   locktype;.  ret
e6660 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
e6670 20 43 6f 6e 74 72 6f 6c 20 61 6e 64 20 71 75 65   Control and que
e6680 72 79 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 66  ry of the open f
e6690 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73  ile handle..*/.s
e66a0 74 61 74 69 63 20 69 6e 74 20 77 69 6e 46 69 6c  tatic int winFil
e66b0 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33  eControl(sqlite3
e66c0 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f  _file *id, int o
e66d0 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a  p, void *pArg){.
e66e0 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a    switch( op ){.
e66f0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
e6700 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a  FCNTL_LOCKSTATE:
e6710 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a 29   {.      *(int*)
e6720 70 41 72 67 20 3d 20 28 28 77 69 6e 46 69 6c 65  pArg = ((winFile
e6730 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 3b  *)id)->locktype;
e6740 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
e6750 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
e6760 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
e6770 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a  TE_ERROR;.}../*.
e6780 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 65  ** Return the se
e6790 63 74 6f 72 20 73 69 7a 65 20 69 6e 20 62 79 74  ctor size in byt
e67a0 65 73 20 6f 66 20 74 68 65 20 75 6e 64 65 72 6c  es of the underl
e67b0 79 69 6e 67 20 62 6c 6f 63 6b 20 64 65 76 69 63  ying block devic
e67c0 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70 65  e for.** the spe
e67d0 63 69 66 69 65 64 20 66 69 6c 65 2e 20 54 68 69  cified file. Thi
e67e0 73 20 69 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61  s is almost alwa
e67f0 79 73 20 35 31 32 20 62 79 74 65 73 2c 20 62 75  ys 512 bytes, bu
e6800 74 20 6d 61 79 20 62 65 0a 2a 2a 20 6c 61 72 67  t may be.** larg
e6810 65 72 20 66 6f 72 20 73 6f 6d 65 20 64 65 76 69  er for some devi
e6820 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  ces..**.** SQLit
e6830 65 20 63 6f 64 65 20 61 73 73 75 6d 65 73 20 74  e code assumes t
e6840 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e  his function can
e6850 6e 6f 74 20 66 61 69 6c 2e 20 49 74 20 61 6c 73  not fail. It als
e6860 6f 20 61 73 73 75 6d 65 73 20 74 68 61 74 0a 2a  o assumes that.*
e6870 2a 20 69 66 20 74 77 6f 20 66 69 6c 65 73 20 61  * if two files a
e6880 72 65 20 63 72 65 61 74 65 64 20 69 6e 20 74 68  re created in th
e6890 65 20 73 61 6d 65 20 66 69 6c 65 2d 73 79 73 74  e same file-syst
e68a0 65 6d 20 64 69 72 65 63 74 6f 72 79 20 28 69 2e  em directory (i.
e68b0 65 2e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65  e..** a database
e68c0 20 61 6e 64 20 69 74 73 20 6a 6f 75 72 6e 61 6c   and its journal
e68d0 20 66 69 6c 65 29 20 74 68 61 74 20 74 68 65 20   file) that the 
e68e0 73 65 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c  sector size will
e68f0 20 62 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20   be the.** same 
e6900 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61  for both..*/.sta
e6910 74 69 63 20 69 6e 74 20 77 69 6e 53 65 63 74 6f  tic int winSecto
e6920 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69  rSize(sqlite3_fi
e6930 6c 65 20 2a 69 64 29 7b 0a 20 20 72 65 74 75 72  le *id){.  retur
e6940 6e 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  n SQLITE_DEFAULT
e6950 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 7d 0a  _SECTOR_SIZE;.}.
e6960 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
e6970 76 65 63 74 6f 72 20 6f 66 20 64 65 76 69 63 65  vector of device
e6980 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73   characteristics
e6990 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
e69a0 77 69 6e 44 65 76 69 63 65 43 68 61 72 61 63 74  winDeviceCharact
e69b0 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 65 33  eristics(sqlite3
e69c0 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 72 65  _file *id){.  re
e69d0 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
e69e0 20 54 68 69 73 20 76 65 63 74 6f 72 20 64 65 66   This vector def
e69f0 69 6e 65 73 20 61 6c 6c 20 74 68 65 20 6d 65 74  ines all the met
e6a00 68 6f 64 73 20 74 68 61 74 20 63 61 6e 20 6f 70  hods that can op
e6a10 65 72 61 74 65 20 6f 6e 20 61 6e 0a 2a 2a 20 73  erate on an.** s
e6a20 71 6c 69 74 65 33 5f 66 69 6c 65 20 66 6f 72 20  qlite3_file for 
e6a30 77 69 6e 33 32 2e 0a 2a 2f 0a 73 74 61 74 69 63  win32..*/.static
e6a40 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69   const sqlite3_i
e6a50 6f 5f 6d 65 74 68 6f 64 73 20 77 69 6e 49 6f 4d  o_methods winIoM
e6a60 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20  ethod = {.  1,  
e6a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e6a80 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f        /* iVersio
e6a90 6e 20 2a 2f 0a 20 20 77 69 6e 43 6c 6f 73 65 2c  n */.  winClose,
e6aa0 0a 20 20 77 69 6e 52 65 61 64 2c 0a 20 20 77 69  .  winRead,.  wi
e6ab0 6e 57 72 69 74 65 2c 0a 20 20 77 69 6e 54 72 75  nWrite,.  winTru
e6ac0 6e 63 61 74 65 2c 0a 20 20 77 69 6e 53 79 6e 63  ncate,.  winSync
e6ad0 2c 0a 20 20 77 69 6e 46 69 6c 65 53 69 7a 65 2c  ,.  winFileSize,
e6ae0 0a 20 20 77 69 6e 4c 6f 63 6b 2c 0a 20 20 77 69  .  winLock,.  wi
e6af0 6e 55 6e 6c 6f 63 6b 2c 0a 20 20 77 69 6e 43 68  nUnlock,.  winCh
e6b00 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c  eckReservedLock,
e6b10 0a 20 20 77 69 6e 46 69 6c 65 43 6f 6e 74 72 6f  .  winFileContro
e6b20 6c 2c 0a 20 20 77 69 6e 53 65 63 74 6f 72 53 69  l,.  winSectorSi
e6b30 7a 65 2c 0a 20 20 77 69 6e 44 65 76 69 63 65 43  ze,.  winDeviceC
e6b40 68 61 72 61 63 74 65 72 69 73 74 69 63 73 0a 7d  haracteristics.}
e6b50 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;../************
e6b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e6b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e6b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e6b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
e6ba0 2a 2a 20 48 65 72 65 20 65 6e 64 73 20 74 68 65  ** Here ends the
e6bb0 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 74 68 61   I/O methods tha
e6bc0 74 20 66 6f 72 6d 20 74 68 65 20 73 71 6c 69 74  t form the sqlit
e6bd0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62  e3_io_methods ob
e6be0 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ject..**.** The 
e6bf0 6e 65 78 74 20 62 6c 6f 63 6b 20 6f 66 20 63 6f  next block of co
e6c00 64 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  de implements th
e6c10 65 20 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a  e VFS methods..*
e6c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e6c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e6c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e6c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e6c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
e6c70 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54  .** Convert a UT
e6c80 46 2d 38 20 66 69 6c 65 6e 61 6d 65 20 69 6e 74  F-8 filename int
e6c90 6f 20 77 68 61 74 65 76 65 72 20 66 6f 72 6d 20  o whatever form 
e6ca0 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a  the underlying.*
e6cb0 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  * operating syst
e6cc0 65 6d 20 77 61 6e 74 73 20 66 69 6c 65 6e 61 6d  em wants filenam
e6cd0 65 73 20 69 6e 2e 20 20 53 70 61 63 65 20 74 6f  es in.  Space to
e6ce0 20 68 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74   hold the result
e6cf0 0a 2a 2a 20 69 73 20 6f 62 74 61 69 6e 65 64 20  .** is obtained 
e6d00 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e 64 20  from malloc and 
e6d10 6d 75 73 74 20 62 65 20 66 72 65 65 64 20 62 79  must be freed by
e6d20 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20   the calling.** 
e6d30 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  function..*/.sta
e6d40 74 69 63 20 76 6f 69 64 20 2a 63 6f 6e 76 65 72  tic void *conver
e6d50 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 63 6f  tUtf8Filename(co
e6d60 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
e6d70 61 6d 65 29 7b 0a 20 20 76 6f 69 64 20 2a 7a 43  ame){.  void *zC
e6d80 6f 6e 76 65 72 74 65 64 20 3d 20 30 3b 0a 20 20  onverted = 0;.  
e6d90 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20  if( isNT() ){.  
e6da0 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 75    zConverted = u
e6db0 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69  tf8ToUnicode(zFi
e6dc0 6c 65 6e 61 6d 65 29 3b 0a 2f 2a 20 69 73 4e 54  lename);./* isNT
e6dd0 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54  () is 1 if SQLIT
e6de0 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73  E_OS_WINCE==1, s
e6df0 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e  o this else is n
e6e00 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a  ever executed. .
e6e10 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  */.#if SQLITE_OS
e6e20 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73  _WINCE==0.  }els
e6e30 65 7b 0a 20 20 20 20 7a 43 6f 6e 76 65 72 74 65  e{.    zConverte
e6e40 64 20 3d 20 75 74 66 38 54 6f 4d 62 63 73 28 7a  d = utf8ToMbcs(z
e6e50 46 69 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64 69  Filename);.#endi
e6e60 66 0a 20 20 7d 0a 20 20 2f 2a 20 63 61 6c 6c 65  f.  }.  /* calle
e6e70 72 20 77 69 6c 6c 20 68 61 6e 64 6c 65 20 6f 75  r will handle ou
e6e80 74 20 6f 66 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20  t of memory */. 
e6e90 20 72 65 74 75 72 6e 20 7a 43 6f 6e 76 65 72 74   return zConvert
e6ea0 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  ed;.}../*.** Cre
e6eb0 61 74 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20  ate a temporary 
e6ec0 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42 75  file name in zBu
e6ed0 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 20 62 65  f.  zBuf must be
e6ee0 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 0a 2a   big enough to.*
e6ef0 2a 20 68 6f 6c 64 20 61 74 20 70 56 66 73 2d 3e  * hold at pVfs->
e6f00 6d 78 50 61 74 68 6e 61 6d 65 20 63 68 61 72 61  mxPathname chara
e6f10 63 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  cters..*/.static
e6f20 20 69 6e 74 20 67 65 74 54 65 6d 70 6e 61 6d 65   int getTempname
e6f30 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20  (int nBuf, char 
e6f40 2a 7a 42 75 66 29 7b 0a 20 20 73 74 61 74 69 63  *zBuf){.  static
e6f50 20 63 68 61 72 20 7a 43 68 61 72 73 5b 5d 20 3d   char zChars[] =
e6f60 0a 20 20 20 20 22 61 62 63 64 65 66 67 68 69 6a  .    "abcdefghij
e6f70 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a  klmnopqrstuvwxyz
e6f80 22 0a 20 20 20 20 22 41 42 43 44 45 46 47 48 49  ".    "ABCDEFGHI
e6f90 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59  JKLMNOPQRSTUVWXY
e6fa0 5a 22 0a 20 20 20 20 22 30 31 32 33 34 35 36 37  Z".    "01234567
e6fb0 38 39 22 3b 0a 20 20 73 69 7a 65 5f 74 20 69 2c  89";.  size_t i,
e6fc0 20 6a 3b 0a 20 20 63 68 61 72 20 7a 54 65 6d 70   j;.  char zTemp
e6fd0 50 61 74 68 5b 4d 41 58 5f 50 41 54 48 2b 31 5d  Path[MAX_PATH+1]
e6fe0 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  ;.  if( sqlite3_
e6ff0 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 29  temp_directory )
e7000 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  {.    sqlite3_sn
e7010 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 2d  printf(MAX_PATH-
e7020 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20 22  30, zTempPath, "
e7030 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 74 65 6d  %s", sqlite3_tem
e7040 70 5f 64 69 72 65 63 74 6f 72 79 29 3b 0a 20 20  p_directory);.  
e7050 7d 65 6c 73 65 20 69 66 28 20 69 73 4e 54 28 29  }else if( isNT()
e7060 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d   ){.    char *zM
e7070 75 6c 74 69 3b 0a 20 20 20 20 57 43 48 41 52 20  ulti;.    WCHAR 
e7080 7a 57 69 64 65 50 61 74 68 5b 4d 41 58 5f 50 41  zWidePath[MAX_PA
e7090 54 48 5d 3b 0a 20 20 20 20 47 65 74 54 65 6d 70  TH];.    GetTemp
e70a0 50 61 74 68 57 28 4d 41 58 5f 50 41 54 48 2d 33  PathW(MAX_PATH-3
e70b0 30 2c 20 7a 57 69 64 65 50 61 74 68 29 3b 0a 20  0, zWidePath);. 
e70c0 20 20 20 7a 4d 75 6c 74 69 20 3d 20 75 6e 69 63     zMulti = unic
e70d0 6f 64 65 54 6f 55 74 66 38 28 7a 57 69 64 65 50  odeToUtf8(zWideP
e70e0 61 74 68 29 3b 0a 20 20 20 20 69 66 28 20 7a 4d  ath);.    if( zM
e70f0 75 6c 74 69 20 29 7b 0a 20 20 20 20 20 20 73 71  ulti ){.      sq
e7100 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d  lite3_snprintf(M
e7110 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d  AX_PATH-30, zTem
e7120 70 50 61 74 68 2c 20 22 25 73 22 2c 20 7a 4d 75  pPath, "%s", zMu
e7130 6c 74 69 29 3b 0a 20 20 20 20 20 20 66 72 65 65  lti);.      free
e7140 28 7a 4d 75 6c 74 69 29 3b 0a 20 20 20 20 7d 65  (zMulti);.    }e
e7150 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72  lse{.      retur
e7160 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
e7170 20 20 20 20 7d 0a 2f 2a 20 69 73 4e 54 28 29 20      }./* isNT() 
e7180 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f  is 1 if SQLITE_O
e7190 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74  S_WINCE==1, so t
e71a0 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65  his else is neve
e71b0 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20  r executed. .** 
e71c0 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 49 20  Since the ASCII 
e71d0 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65  version of these
e71e0 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20   Windows API do 
e71f0 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 49  not exist for WI
e7200 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70  NCE,.** it's imp
e7210 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65  ortant to not re
e7220 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72  ference them for
e7230 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a   WINCE builds..*
e7240 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  /.#if SQLITE_OS_
e7250 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65  WINCE==0.  }else
e7260 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 55 74 66  {.    char *zUtf
e7270 38 3b 0a 20 20 20 20 63 68 61 72 20 7a 4d 62 63  8;.    char zMbc
e7280 73 50 61 74 68 5b 4d 41 58 5f 50 41 54 48 5d 3b  sPath[MAX_PATH];
e7290 0a 20 20 20 20 47 65 74 54 65 6d 70 50 61 74 68  .    GetTempPath
e72a0 41 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a  A(MAX_PATH-30, z
e72b0 4d 62 63 73 50 61 74 68 29 3b 0a 20 20 20 20 7a  MbcsPath);.    z
e72c0 55 74 66 38 20 3d 20 6d 62 63 73 54 6f 55 74 66  Utf8 = mbcsToUtf
e72d0 38 28 7a 4d 62 63 73 50 61 74 68 29 3b 0a 20 20  8(zMbcsPath);.  
e72e0 20 20 69 66 28 20 7a 55 74 66 38 20 29 7b 0a 20    if( zUtf8 ){. 
e72f0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
e7300 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 2d 33  rintf(MAX_PATH-3
e7310 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20 22 25  0, zTempPath, "%
e7320 73 22 2c 20 7a 55 74 66 38 29 3b 0a 20 20 20 20  s", zUtf8);.    
e7330 20 20 66 72 65 65 28 7a 55 74 66 38 29 3b 0a 20    free(zUtf8);. 
e7340 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
e7350 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
e7360 4d 45 4d 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  MEM;.    }.#endi
e7370 66 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 73 74  f.  }.  for(i=st
e7380 72 6c 65 6e 28 7a 54 65 6d 70 50 61 74 68 29 3b  rlen(zTempPath);
e7390 20 69 3e 30 20 26 26 20 7a 54 65 6d 70 50 61 74   i>0 && zTempPat
e73a0 68 5b 69 2d 31 5d 3d 3d 27 5c 5c 27 3b 20 69 2d  h[i-1]=='\\'; i-
e73b0 2d 29 7b 7d 0a 20 20 7a 54 65 6d 70 50 61 74 68  -){}.  zTempPath
e73c0 5b 69 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  [i] = 0;.  sqlit
e73d0 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66  e3_snprintf(nBuf
e73e0 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20  -30, zBuf,.     
e73f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 25                "%
e7400 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f  s\\"SQLITE_TEMP_
e7410 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54 65  FILE_PREFIX, zTe
e7420 6d 70 50 61 74 68 29 3b 0a 20 20 6a 20 3d 20 73  mpPath);.  j = s
e7430 74 72 6c 65 6e 28 7a 42 75 66 29 3b 0a 20 20 73  trlen(zBuf);.  s
e7440 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73  qlite3_randomnes
e7450 73 28 32 30 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b  s(20, &zBuf[j]);
e7460 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 30  .  for(i=0; i<20
e7470 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20  ; i++, j++){.   
e7480 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72   zBuf[j] = (char
e7490 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69 67  )zChars[ ((unsig
e74a0 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d  ned char)zBuf[j]
e74b0 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72 73  )%(sizeof(zChars
e74c0 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a 20 20 7a 42  )-1) ];.  }.  zB
e74d0 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 4f 53 54  uf[j] = 0;.  OST
e74e0 52 41 43 45 32 28 22 54 45 4d 50 20 46 49 4c 45  RACE2("TEMP FILE
e74f0 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c 20 7a 42 75  NAME: %s\n", zBu
e7500 66 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  f);.  return SQL
e7510 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a  ITE_OK; .}../*.*
e7520 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c  * The return val
e7530 75 65 20 6f 66 20 67 65 74 4c 61 73 74 45 72 72  ue of getLastErr
e7540 6f 72 4d 73 67 0a 2a 2a 20 69 73 20 7a 65 72 6f  orMsg.** is zero
e7550 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65   if the error me
e7560 73 73 61 67 65 20 66 69 74 73 20 69 6e 20 74 68  ssage fits in th
e7570 65 20 62 75 66 66 65 72 2c 20 6f 72 20 6e 6f 6e  e buffer, or non
e7580 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68 65 72 77 69  -zero.** otherwi
e7590 73 65 20 28 69 66 20 74 68 65 20 6d 65 73 73 61  se (if the messa
e75a0 67 65 20 77 61 73 20 74 72 75 6e 63 61 74 65 64  ge was truncated
e75b0 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
e75c0 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67   getLastErrorMsg
e75d0 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20  (int nBuf, char 
e75e0 2a 7a 42 75 66 29 7b 0a 20 20 44 57 4f 52 44 20  *zBuf){.  DWORD 
e75f0 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45  error = GetLastE
e7600 72 72 6f 72 28 29 3b 0a 0a 23 69 66 20 53 51 4c  rror();..#if SQL
e7610 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 73  ITE_OS_WINCE.  s
e7620 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
e7630 6e 42 75 66 2c 20 7a 42 75 66 2c 20 22 4f 73 45  nBuf, zBuf, "OsE
e7640 72 72 6f 72 20 30 78 25 78 20 28 25 75 29 22 2c  rror 0x%x (%u)",
e7650 20 65 72 72 6f 72 2c 20 65 72 72 6f 72 29 3b 0a   error, error);.
e7660 23 65 6c 73 65 0a 20 20 2f 2a 20 46 6f 72 6d 61  #else.  /* Forma
e7670 74 4d 65 73 73 61 67 65 20 72 65 74 75 72 6e 73  tMessage returns
e7680 20 30 20 6f 6e 20 66 61 69 6c 75 72 65 2e 20 20   0 on failure.  
e7690 4f 74 68 65 72 77 69 73 65 20 69 74 0a 20 20 2a  Otherwise it.  *
e76a0 2a 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75  * returns the nu
e76b0 6d 62 65 72 20 6f 66 20 54 43 48 41 52 73 20 77  mber of TCHARs w
e76c0 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6f 75  ritten to the ou
e76d0 74 70 75 74 0a 20 20 2a 2a 20 62 75 66 66 65 72  tput.  ** buffer
e76e0 2c 20 65 78 63 6c 75 64 69 6e 67 20 74 68 65 20  , excluding the 
e76f0 74 65 72 6d 69 6e 61 74 69 6e 67 20 6e 75 6c 6c  terminating null
e7700 20 63 68 61 72 2e 0a 20 20 2a 2f 0a 20 20 69 66   char..  */.  if
e7710 20 28 21 46 6f 72 6d 61 74 4d 65 73 73 61 67 65   (!FormatMessage
e7720 41 28 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45  A(FORMAT_MESSAGE
e7730 5f 46 52 4f 4d 5f 53 59 53 54 45 4d 2c 0a 20 20  _FROM_SYSTEM,.  
e7740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e7750 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20      NULL,.      
e7760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e7770 65 72 72 6f 72 2c 0a 20 20 20 20 20 20 20 20 20  error,.         
e7780 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 0a               0,.
e7790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e77a0 20 20 20 20 20 20 7a 42 75 66 2c 0a 20 20 20 20        zBuf,.    
e77b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e77c0 20 20 6e 42 75 66 2d 31 2c 0a 20 20 20 20 20 20    nBuf-1,.      
e77d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e77e0 30 29 29 0a 20 20 7b 0a 20 20 20 20 73 71 6c 69  0)).  {.    sqli
e77f0 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75  te3_snprintf(nBu
e7800 66 2c 20 7a 42 75 66 2c 20 22 4f 73 45 72 72 6f  f, zBuf, "OsErro
e7810 72 20 30 78 25 78 20 28 25 75 29 22 2c 20 65 72  r 0x%x (%u)", er
e7820 72 6f 72 2c 20 65 72 72 6f 72 29 3b 0a 20 20 7d  ror, error);.  }
e7830 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72  .#endif..  retur
e7840 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4f  n 0;.}.../*.** O
e7850 70 65 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73  pen a file..*/.s
e7860 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4f 70 65  tatic int winOpe
e7870 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  n(.  sqlite3_vfs
e7880 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 2f   *pVfs,        /
e7890 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20  * Not used */.  
e78a0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
e78b0 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  e,        /* Nam
e78c0 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 28 55  e of the file (U
e78d0 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74  TF-8) */.  sqlit
e78e0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20  e3_file *id,    
e78f0 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68       /* Write th
e7900 65 20 53 51 4c 69 74 65 20 66 69 6c 65 20 68 61  e SQLite file ha
e7910 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 69  ndle here */.  i
e7920 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20  nt flags,       
e7930 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e           /* Open
e7940 20 6d 6f 64 65 20 66 6c 61 67 73 20 2a 2f 0a 20   mode flags */. 
e7950 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 20   int *pOutFlags 
e7960 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74             /* St
e7970 61 74 75 73 20 72 65 74 75 72 6e 20 66 6c 61 67  atus return flag
e7980 73 20 2a 2f 0a 29 7b 0a 20 20 48 41 4e 44 4c 45  s */.){.  HANDLE
e7990 20 68 3b 0a 20 20 44 57 4f 52 44 20 64 77 44 65   h;.  DWORD dwDe
e79a0 73 69 72 65 64 41 63 63 65 73 73 3b 0a 20 20 44  siredAccess;.  D
e79b0 57 4f 52 44 20 64 77 53 68 61 72 65 4d 6f 64 65  WORD dwShareMode
e79c0 3b 0a 20 20 44 57 4f 52 44 20 64 77 43 72 65 61  ;.  DWORD dwCrea
e79d0 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 3b  tionDisposition;
e79e0 0a 20 20 44 57 4f 52 44 20 64 77 46 6c 61 67 73  .  DWORD dwFlags
e79f0 41 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d 20  AndAttributes = 
e7a00 30 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  0;.#if SQLITE_OS
e7a10 5f 57 49 4e 43 45 0a 20 20 69 6e 74 20 69 73 54  _WINCE.  int isT
e7a20 65 6d 70 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  emp = 0;.#endif.
e7a30 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65    winFile *pFile
e7a40 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b   = (winFile*)id;
e7a50 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72  .  void *zConver
e7a60 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  ted;            
e7a70 20 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 65       /* Filename
e7a80 20 69 6e 20 4f 53 20 65 6e 63 6f 64 69 6e 67 20   in OS encoding 
e7a90 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
e7aa0 2a 7a 55 74 66 38 4e 61 6d 65 20 3d 20 7a 4e 61  *zUtf8Name = zNa
e7ab0 6d 65 3b 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61  me;    /* Filena
e7ac0 6d 65 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f  me in UTF-8 enco
e7ad0 64 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 7a  ding */.  char z
e7ae0 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 48  Tmpname[MAX_PATH
e7af0 2b 31 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 42  +1];        /* B
e7b00 75 66 66 65 72 20 75 73 65 64 20 74 6f 20 63 72  uffer used to cr
e7b10 65 61 74 65 20 74 65 6d 70 20 66 69 6c 65 6e 61  eate temp filena
e7b20 6d 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74  me */..  /* If t
e7b30 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
e7b40 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  nt to this funct
e7b50 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 67 65 6e  ion is NULL, gen
e7b60 65 72 61 74 65 20 61 20 0a 20 20 2a 2a 20 74 65  erate a .  ** te
e7b70 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d  mporary file nam
e7b80 65 20 74 6f 20 75 73 65 20 0a 20 20 2a 2f 0a 20  e to use .  */. 
e7b90 20 69 66 28 20 21 7a 55 74 66 38 4e 61 6d 65 20   if( !zUtf8Name 
e7ba0 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20  ){.    int rc = 
e7bb0 67 65 74 54 65 6d 70 6e 61 6d 65 28 4d 41 58 5f  getTempname(MAX_
e7bc0 50 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65  PATH+1, zTmpname
e7bd0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
e7be0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
e7bf0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
e7c00 20 7d 0a 20 20 20 20 7a 55 74 66 38 4e 61 6d 65   }.    zUtf8Name
e7c10 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d   = zTmpname;.  }
e7c20 0a 0a 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74  ..  /* Convert t
e7c30 68 65 20 66 69 6c 65 6e 61 6d 65 20 74 6f 20 74  he filename to t
e7c40 68 65 20 73 79 73 74 65 6d 20 65 6e 63 6f 64 69  he system encodi
e7c50 6e 67 2e 20 2a 2f 0a 20 20 7a 43 6f 6e 76 65 72  ng. */.  zConver
e7c60 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66  ted = convertUtf
e7c70 38 46 69 6c 65 6e 61 6d 65 28 7a 55 74 66 38 4e  8Filename(zUtf8N
e7c80 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e  ame);.  if( zCon
e7c90 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20  verted==0 ){.   
e7ca0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
e7cb0 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  OMEM;.  }..  if(
e7cc0 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f   flags & SQLITE_
e7cd0 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29  OPEN_READWRITE )
e7ce0 7b 0a 20 20 20 20 64 77 44 65 73 69 72 65 64 41  {.    dwDesiredA
e7cf0 63 63 65 73 73 20 3d 20 47 45 4e 45 52 49 43 5f  ccess = GENERIC_
e7d00 52 45 41 44 20 7c 20 47 45 4e 45 52 49 43 5f 57  READ | GENERIC_W
e7d10 52 49 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  RITE;.  }else{. 
e7d20 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65     dwDesiredAcce
e7d30 73 73 20 3d 20 47 45 4e 45 52 49 43 5f 52 45 41  ss = GENERIC_REA
e7d40 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 61  D;.  }.  if( fla
e7d50 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
e7d60 5f 43 52 45 41 54 45 20 29 7b 0a 20 20 20 20 64  _CREATE ){.    d
e7d70 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69  wCreationDisposi
e7d80 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f 41 4c 57 41  tion = OPEN_ALWA
e7d90 59 53 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  YS;.  }else{.   
e7da0 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f   dwCreationDispo
e7db0 73 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f 45 58  sition = OPEN_EX
e7dc0 49 53 54 49 4e 47 3b 0a 20 20 7d 0a 20 20 69 66  ISTING;.  }.  if
e7dd0 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ( flags & SQLITE
e7de0 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 29 7b  _OPEN_MAIN_DB ){
e7df0 0a 20 20 20 20 64 77 53 68 61 72 65 4d 6f 64 65  .    dwShareMode
e7e00 20 3d 20 46 49 4c 45 5f 53 48 41 52 45 5f 52 45   = FILE_SHARE_RE
e7e10 41 44 20 7c 20 46 49 4c 45 5f 53 48 41 52 45 5f  AD | FILE_SHARE_
e7e20 57 52 49 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a  WRITE;.  }else{.
e7e30 20 20 20 20 64 77 53 68 61 72 65 4d 6f 64 65 20      dwShareMode 
e7e40 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66  = 0;.  }.  if( f
e7e50 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50  lags & SQLITE_OP
e7e60 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45  EN_DELETEONCLOSE
e7e70 20 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f   ){.#if SQLITE_O
e7e80 53 5f 57 49 4e 43 45 0a 20 20 20 20 64 77 46 6c  S_WINCE.    dwFl
e7e90 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73  agsAndAttributes
e7ea0 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54   = FILE_ATTRIBUT
e7eb0 45 5f 48 49 44 44 45 4e 3b 0a 20 20 20 20 69 73  E_HIDDEN;.    is
e7ec0 54 65 6d 70 20 3d 20 31 3b 0a 23 65 6c 73 65 0a  Temp = 1;.#else.
e7ed0 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74      dwFlagsAndAt
e7ee0 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45 5f  tributes = FILE_
e7ef0 41 54 54 52 49 42 55 54 45 5f 54 45 4d 50 4f 52  ATTRIBUTE_TEMPOR
e7f00 41 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 20  ARY.            
e7f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e7f20 20 20 20 7c 20 46 49 4c 45 5f 41 54 54 52 49 42     | FILE_ATTRIB
e7f30 55 54 45 5f 48 49 44 44 45 4e 0a 20 20 20 20 20  UTE_HIDDEN.     
e7f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e7f50 20 20 20 20 20 20 20 20 20 20 7c 20 46 49 4c 45            | FILE
e7f60 5f 46 4c 41 47 5f 44 45 4c 45 54 45 5f 4f 4e 5f  _FLAG_DELETE_ON_
e7f70 43 4c 4f 53 45 3b 0a 23 65 6e 64 69 66 0a 20 20  CLOSE;.#endif.  
e7f80 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77 46 6c 61  }else{.    dwFla
e7f90 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 20  gsAndAttributes 
e7fa0 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45  = FILE_ATTRIBUTE
e7fb0 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 7d 0a 20 20 2f  _NORMAL;.  }.  /
e7fc0 2a 20 52 65 70 6f 72 74 73 20 66 72 6f 6d 20 74  * Reports from t
e7fd0 68 65 20 69 6e 74 65 72 6e 65 74 20 61 72 65 20  he internet are 
e7fe0 74 68 61 74 20 70 65 72 66 6f 72 6d 61 6e 63 65  that performance
e7ff0 20 69 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20   is always.  ** 
e8000 62 65 74 74 65 72 20 69 66 20 46 49 4c 45 5f 46  better if FILE_F
e8010 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45 53  LAG_RANDOM_ACCES
e8020 53 20 69 73 20 75 73 65 64 2e 20 20 54 69 63 6b  S is used.  Tick
e8030 65 74 20 23 32 36 39 39 2e 20 2a 2f 0a 23 69 66  et #2699. */.#if
e8040 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
e8050 0a 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74  .  dwFlagsAndAtt
e8060 72 69 62 75 74 65 73 20 7c 3d 20 46 49 4c 45 5f  ributes |= FILE_
e8070 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45  FLAG_RANDOM_ACCE
e8080 53 53 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28  SS;.#endif.  if(
e8090 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 68   isNT() ){.    h
e80a0 20 3d 20 43 72 65 61 74 65 46 69 6c 65 57 28 28   = CreateFileW((
e80b0 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65  WCHAR*)zConverte
e80c0 64 2c 0a 20 20 20 20 20 20 20 64 77 44 65 73 69  d,.       dwDesi
e80d0 72 65 64 41 63 63 65 73 73 2c 0a 20 20 20 20 20  redAccess,.     
e80e0 20 20 64 77 53 68 61 72 65 4d 6f 64 65 2c 0a 20    dwShareMode,. 
e80f0 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20        NULL,.    
e8100 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73     dwCreationDis
e8110 70 6f 73 69 74 69 6f 6e 2c 0a 20 20 20 20 20 20  position,.      
e8120 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69   dwFlagsAndAttri
e8130 62 75 74 65 73 2c 0a 20 20 20 20 20 20 20 4e 55  butes,.       NU
e8140 4c 4c 0a 20 20 20 20 29 3b 0a 2f 2a 20 69 73 4e  LL.    );./* isN
e8150 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49  T() is 1 if SQLI
e8160 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20  TE_OS_WINCE==1, 
e8170 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20  so this else is 
e8180 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20  never executed. 
e8190 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53  .** Since the AS
e81a0 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  CII version of t
e81b0 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49  hese Windows API
e81c0 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f   do not exist fo
e81d0 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73  r WINCE,.** it's
e81e0 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f   important to no
e81f0 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d  t reference them
e8200 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64   for WINCE build
e8210 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  s..*/.#if SQLITE
e8220 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d  _OS_WINCE==0.  }
e8230 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 43 72  else{.    h = Cr
e8240 65 61 74 65 46 69 6c 65 41 28 28 63 68 61 72 2a  eateFileA((char*
e8250 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20  )zConverted,.   
e8260 20 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63      dwDesiredAcc
e8270 65 73 73 2c 0a 20 20 20 20 20 20 20 64 77 53 68  ess,.       dwSh
e8280 61 72 65 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20  areMode,.       
e8290 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 64 77 43  NULL,.       dwC
e82a0 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69  reationDispositi
e82b0 6f 6e 2c 0a 20 20 20 20 20 20 20 64 77 46 6c 61  on,.       dwFla
e82c0 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 2c  gsAndAttributes,
e82d0 0a 20 20 20 20 20 20 20 4e 55 4c 4c 0a 20 20 20  .       NULL.   
e82e0 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20   );.#endif.  }. 
e82f0 20 69 66 28 20 68 3d 3d 49 4e 56 41 4c 49 44 5f   if( h==INVALID_
e8300 48 41 4e 44 4c 45 5f 56 41 4c 55 45 20 29 7b 0a  HANDLE_VALUE ){.
e8310 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72      free(zConver
e8320 74 65 64 29 3b 0a 20 20 20 20 69 66 28 20 66 6c  ted);.    if( fl
e8330 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
e8340 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20  N_READWRITE ){. 
e8350 20 20 20 20 20 72 65 74 75 72 6e 20 77 69 6e 4f       return winO
e8360 70 65 6e 28 30 2c 20 7a 4e 61 6d 65 2c 20 69 64  pen(0, zName, id
e8370 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
e8380 28 28 66 6c 61 67 73 7c 53 51 4c 49 54 45 5f 4f  ((flags|SQLITE_O
e8390 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 26 7e 53  PEN_READONLY)&~S
e83a0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
e83b0 52 49 54 45 29 2c 20 70 4f 75 74 46 6c 61 67 73  RITE), pOutFlags
e83c0 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
e83d0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
e83e0 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20  E_CANTOPEN;.    
e83f0 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 75 74  }.  }.  if( pOut
e8400 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 69 66 28  Flags ){.    if(
e8410 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f   flags & SQLITE_
e8420 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29  OPEN_READWRITE )
e8430 7b 0a 20 20 20 20 20 20 2a 70 4f 75 74 46 6c 61  {.      *pOutFla
e8440 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs = SQLITE_OPEN
e8450 5f 52 45 41 44 57 52 49 54 45 3b 0a 20 20 20 20  _READWRITE;.    
e8460 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a 70 4f  }else{.      *pO
e8470 75 74 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 45  utFlags = SQLITE
e8480 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a  _OPEN_READONLY;.
e8490 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73      }.  }.  mems
e84a0 65 74 28 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a  et(pFile, 0, siz
e84b0 65 6f 66 28 2a 70 46 69 6c 65 29 29 3b 0a 20 20  eof(*pFile));.  
e84c0 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d  pFile->pMethod =
e84d0 20 26 77 69 6e 49 6f 4d 65 74 68 6f 64 3b 0a 20   &winIoMethod;. 
e84e0 20 70 46 69 6c 65 2d 3e 68 20 3d 20 68 3b 0a 23   pFile->h = h;.#
e84f0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
e8500 43 45 0a 20 20 69 66 28 20 28 66 6c 61 67 73 20  CE.  if( (flags 
e8510 26 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  & (SQLITE_OPEN_R
e8520 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f  EADWRITE|SQLITE_
e8530 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 29 20 3d  OPEN_MAIN_DB)) =
e8540 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  =.              
e8550 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45   (SQLITE_OPEN_RE
e8560 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f  ADWRITE|SQLITE_O
e8570 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 0a 20 20 20  PEN_MAIN_DB).   
e8580 20 20 20 20 26 26 20 21 77 69 6e 63 65 43 72 65      && !winceCre
e8590 61 74 65 4c 6f 63 6b 28 7a 4e 61 6d 65 2c 20 70  ateLock(zName, p
e85a0 46 69 6c 65 29 0a 20 20 29 7b 0a 20 20 20 20 43  File).  ){.    C
e85b0 6c 6f 73 65 48 61 6e 64 6c 65 28 68 29 3b 0a 20  loseHandle(h);. 
e85c0 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74     free(zConvert
e85d0 65 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ed);.    return 
e85e0 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b  SQLITE_CANTOPEN;
e85f0 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 54 65 6d  .  }.  if( isTem
e8600 70 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  p ){.    pFile->
e8610 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 20 3d  zDeleteOnClose =
e8620 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 7d   zConverted;.  }
e8630 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 7b 0a  else.#endif.  {.
e8640 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72      free(zConver
e8650 74 65 64 29 3b 0a 20 20 7d 0a 20 20 4f 70 65 6e  ted);.  }.  Open
e8660 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 72  Counter(+1);.  r
e8670 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
e8680 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  .}../*.** Delete
e8690 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e   the named file.
e86a0 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  .**.** Note that
e86b0 20 77 69 6e 64 6f 77 73 20 64 6f 65 73 20 6e 6f   windows does no
e86c0 74 20 61 6c 6c 6f 77 20 61 20 66 69 6c 65 20 74  t allow a file t
e86d0 6f 20 62 65 20 64 65 6c 65 74 65 64 20 69 66 20  o be deleted if 
e86e0 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70 72  some other.** pr
e86f0 6f 63 65 73 73 20 68 61 73 20 69 74 20 6f 70 65  ocess has it ope
e8700 6e 2e 20 20 53 6f 6d 65 74 69 6d 65 73 20 61 20  n.  Sometimes a 
e8710 76 69 72 75 73 20 73 63 61 6e 6e 65 72 20 6f 72  virus scanner or
e8720 20 69 6e 64 65 78 69 6e 67 20 70 72 6f 67 72 61   indexing progra
e8730 6d 0a 2a 2a 20 77 69 6c 6c 20 6f 70 65 6e 20 61  m.** will open a
e8740 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 68   journal file sh
e8750 6f 72 74 6c 79 20 61 66 74 65 72 20 69 74 20 69  ortly after it i
e8760 73 20 63 72 65 61 74 65 64 20 69 6e 20 6f 72 64  s created in ord
e8770 65 72 20 74 6f 20 64 6f 0a 2a 2a 20 77 68 61 74  er to do.** what
e8780 65 76 65 72 20 69 74 20 64 6f 65 73 2e 20 20 57  ever it does.  W
e8790 68 69 6c 65 20 74 68 69 73 20 6f 74 68 65 72 20  hile this other 
e87a0 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 69  process is holdi
e87b0 6e 67 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 6f  ng the.** file o
e87c0 70 65 6e 2c 20 77 65 20 77 69 6c 6c 20 62 65 20  pen, we will be 
e87d0 75 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65  unable to delete
e87e0 20 69 74 2e 20 20 54 6f 20 77 6f 72 6b 20 61 72   it.  To work ar
e87f0 6f 75 6e 64 20 74 68 69 73 0a 2a 2a 20 70 72 6f  ound this.** pro
e8800 62 6c 65 6d 2c 20 77 65 20 64 65 6c 61 79 20 31  blem, we delay 1
e8810 30 30 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20  00 milliseconds 
e8820 61 6e 64 20 74 72 79 20 74 6f 20 64 65 6c 65 74  and try to delet
e8830 65 20 61 67 61 69 6e 2e 20 20 55 70 0a 2a 2a 20  e again.  Up.** 
e8840 74 6f 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41  to MX_DELETION_A
e8850 54 54 45 4d 50 54 73 20 64 65 6c 65 74 69 6f 6e  TTEMPTs deletion
e8860 20 61 74 74 65 6d 70 74 73 20 61 72 65 20 72 75   attempts are ru
e8870 6e 20 62 65 66 6f 72 65 20 67 69 76 69 6e 67 0a  n before giving.
e8880 2a 2a 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e  ** up and return
e8890 69 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f  ing an error..*/
e88a0 0a 23 64 65 66 69 6e 65 20 4d 58 5f 44 45 4c 45  .#define MX_DELE
e88b0 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 20 35 0a  TION_ATTEMPTS 5.
e88c0 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 44 65  static int winDe
e88d0 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  lete(.  sqlite3_
e88e0 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20  vfs *pVfs,      
e88f0 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20      /* Not used 
e8900 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 20 20 63 6f  on win32 */.  co
e8910 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
e8920 61 6d 65 2c 20 20 20 20 20 20 2f 2a 20 4e 61 6d  ame,      /* Nam
e8930 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c  e of file to del
e8940 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 73 79 6e  ete */.  int syn
e8950 63 44 69 72 20 20 20 20 20 20 20 20 20 20 20 20  cDir            
e8960 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64       /* Not used
e8970 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 29 7b 0a   on win32 */.){.
e8980 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20    int cnt = 0;. 
e8990 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 44 57 4f   DWORD rc;.  DWO
e89a0 52 44 20 65 72 72 6f 72 3b 0a 20 20 76 6f 69 64  RD error;.  void
e89b0 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63   *zConverted = c
e89c0 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61  onvertUtf8Filena
e89d0 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  me(zFilename);. 
e89e0 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d   if( zConverted=
e89f0 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
e8a00 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
e8a10 20 7d 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45   }.  SimulateIOE
e8a20 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49  rror(return SQLI
e8a30 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 29  TE_IOERR_DELETE)
e8a40 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29  ;.  if( isNT() )
e8a50 7b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20  {.    do{.      
e8a60 44 65 6c 65 74 65 46 69 6c 65 57 28 7a 43 6f 6e  DeleteFileW(zCon
e8a70 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7d 77 68  verted);.    }wh
e8a80 69 6c 65 28 20 20 20 28 20 20 20 28 28 72 63 20  ile(   (   ((rc 
e8a90 3d 20 47 65 74 46 69 6c 65 41 74 74 72 69 62 75  = GetFileAttribu
e8aa0 74 65 73 57 28 7a 43 6f 6e 76 65 72 74 65 64 29  tesW(zConverted)
e8ab0 29 20 21 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c  ) != INVALID_FIL
e8ac0 45 5f 41 54 54 52 49 42 55 54 45 53 29 0a 20 20  E_ATTRIBUTES).  
e8ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
e8ae0 28 28 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73  ((error = GetLas
e8af0 74 45 72 72 6f 72 28 29 29 20 3d 3d 20 45 52 52  tError()) == ERR
e8b00 4f 52 5f 41 43 43 45 53 53 5f 44 45 4e 49 45 44  OR_ACCESS_DENIED
e8b10 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 26 26  )).           &&
e8b20 20 28 2b 2b 63 6e 74 20 3c 20 4d 58 5f 44 45 4c   (++cnt < MX_DEL
e8b30 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 29 0a  ETION_ATTEMPTS).
e8b40 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 53             && (S
e8b50 6c 65 65 70 28 31 30 30 29 2c 20 31 29 20 29 3b  leep(100), 1) );
e8b60 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20  ./* isNT() is 1 
e8b70 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
e8b80 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65  CE==1, so this e
e8b90 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65  lse is never exe
e8ba0 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65  cuted. .** Since
e8bb0 20 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69   the ASCII versi
e8bc0 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64  on of these Wind
e8bd0 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65  ows API do not e
e8be0 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a  xist for WINCE,.
e8bf0 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e  ** it's importan
e8c00 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e  t to not referen
e8c10 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43  ce them for WINC
e8c20 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66  E builds..*/.#if
e8c30 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
e8c40 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ==0.  }else{.   
e8c50 20 64 6f 7b 0a 20 20 20 20 20 20 44 65 6c 65 74   do{.      Delet
e8c60 65 46 69 6c 65 41 28 7a 43 6f 6e 76 65 72 74 65  eFileA(zConverte
e8c70 64 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20  d);.    }while( 
e8c80 20 20 28 20 20 20 28 28 72 63 20 3d 20 47 65 74    (   ((rc = Get
e8c90 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 41 28  FileAttributesA(
e8ca0 7a 43 6f 6e 76 65 72 74 65 64 29 29 20 21 3d 20  zConverted)) != 
e8cb0 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54  INVALID_FILE_ATT
e8cc0 52 49 42 55 54 45 53 29 0a 20 20 20 20 20 20 20  RIBUTES).       
e8cd0 20 20 20 20 20 20 20 20 7c 7c 20 28 28 65 72 72          || ((err
e8ce0 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f  or = GetLastErro
e8cf0 72 28 29 29 20 3d 3d 20 45 52 52 4f 52 5f 41 43  r()) == ERROR_AC
e8d00 43 45 53 53 5f 44 45 4e 49 45 44 29 29 0a 20 20  CESS_DENIED)).  
e8d10 20 20 20 20 20 20 20 20 20 26 26 20 28 2b 2b 63           && (++c
e8d20 6e 74 20 3c 20 4d 58 5f 44 45 4c 45 54 49 4f 4e  nt < MX_DELETION
e8d30 5f 41 54 54 45 4d 50 54 53 29 0a 20 20 20 20 20  _ATTEMPTS).     
e8d40 20 20 20 20 20 20 26 26 20 28 53 6c 65 65 70 28        && (Sleep(
e8d50 31 30 30 29 2c 20 31 29 20 29 3b 0a 23 65 6e 64  100), 1) );.#end
e8d60 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 28 7a 43  if.  }.  free(zC
e8d70 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 4f 53 54  onverted);.  OST
e8d80 52 41 43 45 32 28 22 44 45 4c 45 54 45 20 5c 22  RACE2("DELETE \"
e8d90 25 73 5c 22 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61  %s\"\n", zFilena
e8da0 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 20  me);.  return ( 
e8db0 20 20 28 72 63 20 3d 3d 20 49 4e 56 41 4c 49 44    (rc == INVALID
e8dc0 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53  _FILE_ATTRIBUTES
e8dd0 29 20 0a 20 20 20 20 20 20 20 20 20 20 26 26 20  ) .          && 
e8de0 28 65 72 72 6f 72 20 3d 3d 20 45 52 52 4f 52 5f  (error == ERROR_
e8df0 46 49 4c 45 5f 4e 4f 54 5f 46 4f 55 4e 44 29 29  FILE_NOT_FOUND))
e8e00 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53   ? SQLITE_OK : S
e8e10 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45  QLITE_IOERR_DELE
e8e20 54 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65  TE;.}../*.** Che
e8e30 63 6b 20 74 68 65 20 65 78 69 73 74 61 6e 63 65  ck the existance
e8e40 20 61 6e 64 20 73 74 61 74 75 73 20 6f 66 20 61   and status of a
e8e50 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
e8e60 20 69 6e 74 20 77 69 6e 41 63 63 65 73 73 28 0a   int winAccess(.
e8e70 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
e8e80 56 66 73 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Vfs,         /* 
e8e90 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77 69 6e 33  Not used on win3
e8ea0 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  2 */.  const cha
e8eb0 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20  r *zFilename,   
e8ec0 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c    /* Name of fil
e8ed0 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20  e to check */.  
e8ee0 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20  int flags,      
e8ef0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 79             /* Ty
e8f00 70 65 20 6f 66 20 74 65 73 74 20 74 6f 20 6d 61  pe of test to ma
e8f10 6b 65 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20  ke on this file 
e8f20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 4f 75  */.  int *pResOu
e8f30 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
e8f40 2f 2a 20 4f 55 54 3a 20 52 65 73 75 6c 74 20 2a  /* OUT: Result *
e8f50 2f 0a 29 7b 0a 20 20 44 57 4f 52 44 20 61 74 74  /.){.  DWORD att
e8f60 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 76  r;.  int rc;.  v
e8f70 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20  oid *zConverted 
e8f80 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c  = convertUtf8Fil
e8f90 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29  ename(zFilename)
e8fa0 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74  ;.  if( zConvert
e8fb0 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ed==0 ){.    ret
e8fc0 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
e8fd0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 4e 54  ;.  }.  if( isNT
e8fe0 28 29 20 29 7b 0a 20 20 20 20 61 74 74 72 20 3d  () ){.    attr =
e8ff0 20 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 74   GetFileAttribut
e9000 65 73 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e  esW((WCHAR*)zCon
e9010 76 65 72 74 65 64 29 3b 0a 2f 2a 20 69 73 4e 54  verted);./* isNT
e9020 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54  () is 1 if SQLIT
e9030 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73  E_OS_WINCE==1, s
e9040 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e  o this else is n
e9050 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a  ever executed. .
e9060 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43  ** Since the ASC
e9070 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68  II version of th
e9080 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20  ese Windows API 
e9090 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72  do not exist for
e90a0 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20   WINCE,.** it's 
e90b0 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74  important to not
e90c0 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20   reference them 
e90d0 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73  for WINCE builds
e90e0 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  ..*/.#if SQLITE_
e90f0 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65  OS_WINCE==0.  }e
e9100 6c 73 65 7b 0a 20 20 20 20 61 74 74 72 20 3d 20  lse{.    attr = 
e9110 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65  GetFileAttribute
e9120 73 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65  sA((char*)zConve
e9130 72 74 65 64 29 3b 0a 23 65 6e 64 69 66 0a 20 20  rted);.#endif.  
e9140 7d 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72  }.  free(zConver
e9150 74 65 64 29 3b 0a 20 20 73 77 69 74 63 68 28 20  ted);.  switch( 
e9160 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61 73  flags ){.    cas
e9170 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  e SQLITE_ACCESS_
e9180 52 45 41 44 3a 0a 20 20 20 20 63 61 73 65 20 53  READ:.    case S
e9190 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49  QLITE_ACCESS_EXI
e91a0 53 54 53 3a 0a 20 20 20 20 20 20 72 63 20 3d 20  STS:.      rc = 
e91b0 61 74 74 72 21 3d 49 4e 56 41 4c 49 44 5f 46 49  attr!=INVALID_FI
e91c0 4c 45 5f 41 54 54 52 49 42 55 54 45 53 3b 0a 20  LE_ATTRIBUTES;. 
e91d0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
e91e0 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45  case SQLITE_ACCE
e91f0 53 53 5f 52 45 41 44 57 52 49 54 45 3a 0a 20 20  SS_READWRITE:.  
e9200 20 20 20 20 72 63 20 3d 20 28 61 74 74 72 20 26      rc = (attr &
e9210 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f   FILE_ATTRIBUTE_
e9220 52 45 41 44 4f 4e 4c 59 29 3d 3d 30 3b 0a 20 20  READONLY)==0;.  
e9230 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64      break;.    d
e9240 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 61 73  efault:.      as
e9250 73 65 72 74 28 21 22 49 6e 76 61 6c 69 64 20 66  sert(!"Invalid f
e9260 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 22 29 3b  lags argument");
e9270 0a 20 20 7d 0a 20 20 2a 70 52 65 73 4f 75 74 20  .  }.  *pResOut 
e9280 3d 20 72 63 3b 0a 20 20 72 65 74 75 72 6e 20 53  = rc;.  return S
e9290 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a  QLITE_OK;.}.../*
e92a0 0a 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c 61 74  .** Turn a relat
e92b0 69 76 65 20 70 61 74 68 6e 61 6d 65 20 69 6e 74  ive pathname int
e92c0 6f 20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d  o a full pathnam
e92d0 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 66 75  e.  Write the fu
e92e0 6c 6c 0a 2a 2a 20 70 61 74 68 6e 61 6d 65 20 69  ll.** pathname i
e92f0 6e 74 6f 20 7a 4f 75 74 5b 5d 2e 20 20 7a 4f 75  nto zOut[].  zOu
e9300 74 5b 5d 20 77 69 6c 6c 20 62 65 20 61 74 20 6c  t[] will be at l
e9310 65 61 73 74 20 70 56 66 73 2d 3e 6d 78 50 61 74  east pVfs->mxPat
e9320 68 6e 61 6d 65 0a 2a 2a 20 62 79 74 65 73 20 69  hname.** bytes i
e9330 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69  n size..*/.stati
e9340 63 20 69 6e 74 20 77 69 6e 46 75 6c 6c 50 61 74  c int winFullPat
e9350 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33  hname(.  sqlite3
e9360 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
e9370 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
e9380 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 20  r to vfs object 
e9390 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
e93a0 2a 7a 52 65 6c 61 74 69 76 65 2c 20 20 20 20 20  *zRelative,     
e93b0 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20 72     /* Possibly r
e93c0 65 6c 61 74 69 76 65 20 69 6e 70 75 74 20 70 61  elative input pa
e93d0 74 68 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6c  th */.  int nFul
e93e0 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l,              
e93f0 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
e9400 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 69   output buffer i
e9410 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 61  n bytes */.  cha
e9420 72 20 2a 7a 46 75 6c 6c 20 20 20 20 20 20 20 20  r *zFull        
e9430 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75             /* Ou
e9440 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 29  tput buffer */.)
e9450 7b 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f  {..#if defined(_
e9460 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20 63 79 67  _CYGWIN__).  cyg
e9470 77 69 6e 5f 63 6f 6e 76 5f 74 6f 5f 66 75 6c 6c  win_conv_to_full
e9480 5f 77 69 6e 33 32 5f 70 61 74 68 28 7a 52 65 6c  _win32_path(zRel
e9490 61 74 69 76 65 2c 20 7a 46 75 6c 6c 29 3b 0a 20  ative, zFull);. 
e94a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
e94b0 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 53  K;.#endif..#if S
e94c0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20  QLITE_OS_WINCE. 
e94d0 20 2f 2a 20 57 69 6e 43 45 20 68 61 73 20 6e 6f   /* WinCE has no
e94e0 20 63 6f 6e 63 65 70 74 20 6f 66 20 61 20 72 65   concept of a re
e94f0 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65 2c  lative pathname,
e9500 20 6f 72 20 73 6f 20 49 20 61 6d 20 74 6f 6c 64   or so I am told
e9510 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  . */.  sqlite3_s
e9520 6e 70 72 69 6e 74 66 28 70 56 66 73 2d 3e 6d 78  nprintf(pVfs->mx
e9530 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c  Pathname, zFull,
e9540 20 22 25 73 22 2c 20 7a 52 65 6c 61 74 69 76 65   "%s", zRelative
e9550 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
e9560 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23  TE_OK;.#endif..#
e9570 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49  if !SQLITE_OS_WI
e9580 4e 43 45 20 26 26 20 21 64 65 66 69 6e 65 64 28  NCE && !defined(
e9590 5f 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20 69 6e  __CYGWIN__).  in
e95a0 74 20 6e 42 79 74 65 3b 0a 20 20 76 6f 69 64 20  t nByte;.  void 
e95b0 2a 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 63  *zConverted;.  c
e95c0 68 61 72 20 2a 7a 4f 75 74 3b 0a 20 20 7a 43 6f  har *zOut;.  zCo
e95d0 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72  nverted = conver
e95e0 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 52  tUtf8Filename(zR
e95f0 65 6c 61 74 69 76 65 29 3b 0a 20 20 69 66 28 20  elative);.  if( 
e9600 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 57 43  isNT() ){.    WC
e9610 48 41 52 20 2a 7a 54 65 6d 70 3b 0a 20 20 20 20  HAR *zTemp;.    
e9620 6e 42 79 74 65 20 3d 20 47 65 74 46 75 6c 6c 50  nByte = GetFullP
e9630 61 74 68 4e 61 6d 65 57 28 28 57 43 48 41 52 2a  athNameW((WCHAR*
e9640 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 30 2c 20  )zConverted, 0, 
e9650 30 2c 20 30 29 20 2b 20 33 3b 0a 20 20 20 20 7a  0, 0) + 3;.    z
e9660 54 65 6d 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e  Temp = malloc( n
e9670 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 54 65 6d  Byte*sizeof(zTem
e9680 70 5b 30 5d 29 20 29 3b 0a 20 20 20 20 69 66 28  p[0]) );.    if(
e9690 20 7a 54 65 6d 70 3d 3d 30 20 29 7b 0a 20 20 20   zTemp==0 ){.   
e96a0 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74     free(zConvert
e96b0 65 64 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ed);.      retur
e96c0 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
e96d0 20 20 20 20 7d 0a 20 20 20 20 47 65 74 46 75 6c      }.    GetFul
e96e0 6c 50 61 74 68 4e 61 6d 65 57 28 28 57 43 48 41  lPathNameW((WCHA
e96f0 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 6e  R*)zConverted, n
e9700 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20 30 29 3b  Byte, zTemp, 0);
e9710 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65  .    free(zConve
e9720 72 74 65 64 29 3b 0a 20 20 20 20 7a 4f 75 74 20  rted);.    zOut 
e9730 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 28  = unicodeToUtf8(
e9740 7a 54 65 6d 70 29 3b 0a 20 20 20 20 66 72 65 65  zTemp);.    free
e9750 28 7a 54 65 6d 70 29 3b 0a 2f 2a 20 69 73 4e 54  (zTemp);./* isNT
e9760 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54  () is 1 if SQLIT
e9770 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73  E_OS_WINCE==1, s
e9780 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e  o this else is n
e9790 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a  ever executed. .
e97a0 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43  ** Since the ASC
e97b0 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68  II version of th
e97c0 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20  ese Windows API 
e97d0 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72  do not exist for
e97e0 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20   WINCE,.** it's 
e97f0 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74  important to not
e9800 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20   reference them 
e9810 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73  for WINCE builds
e9820 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  ..*/.#if SQLITE_
e9830 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65  OS_WINCE==0.  }e
e9840 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a  lse{.    char *z
e9850 54 65 6d 70 3b 0a 20 20 20 20 6e 42 79 74 65 20  Temp;.    nByte 
e9860 3d 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d  = GetFullPathNam
e9870 65 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65  eA((char*)zConve
e9880 72 74 65 64 2c 20 30 2c 20 30 2c 20 30 29 20 2b  rted, 0, 0, 0) +
e9890 20 33 3b 0a 20 20 20 20 7a 54 65 6d 70 20 3d 20   3;.    zTemp = 
e98a0 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2a 73 69  malloc( nByte*si
e98b0 7a 65 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20 29  zeof(zTemp[0]) )
e98c0 3b 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70 3d  ;.    if( zTemp=
e98d0 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 72 65 65  =0 ){.      free
e98e0 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20  (zConverted);.  
e98f0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
e9900 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20  E_NOMEM;.    }. 
e9910 20 20 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61     GetFullPathNa
e9920 6d 65 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76  meA((char*)zConv
e9930 65 72 74 65 64 2c 20 6e 42 79 74 65 2c 20 7a 54  erted, nByte, zT
e9940 65 6d 70 2c 20 30 29 3b 0a 20 20 20 20 66 72 65  emp, 0);.    fre
e9950 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20  e(zConverted);. 
e9960 20 20 20 7a 4f 75 74 20 3d 20 6d 62 63 73 54 6f     zOut = mbcsTo
e9970 55 74 66 38 28 7a 54 65 6d 70 29 3b 0a 20 20 20  Utf8(zTemp);.   
e9980 20 66 72 65 65 28 7a 54 65 6d 70 29 3b 0a 23 65   free(zTemp);.#e
e9990 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 7a  ndif.  }.  if( z
e99a0 4f 75 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Out ){.    sqlit
e99b0 65 33 5f 73 6e 70 72 69 6e 74 66 28 70 56 66 73  e3_snprintf(pVfs
e99c0 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2c 20 7a 46  ->mxPathname, zF
e99d0 75 6c 6c 2c 20 22 25 73 22 2c 20 7a 4f 75 74 29  ull, "%s", zOut)
e99e0 3b 0a 20 20 20 20 66 72 65 65 28 7a 4f 75 74 29  ;.    free(zOut)
e99f0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
e9a00 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b  ITE_OK;.  }else{
e9a10 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
e9a20 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 65  TE_NOMEM;.  }.#e
e9a30 6e 64 69 66 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  ndif.}..#ifndef 
e9a40 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44  SQLITE_OMIT_LOAD
e9a50 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a  _EXTENSION./*.**
e9a60 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20   Interfaces for 
e9a70 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64  opening a shared
e9a80 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e   library, findin
e9a90 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a  g entry points.*
e9aa0 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61  * within the sha
e9ab0 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64  red library, and
e9ac0 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61   closing the sha
e9ad0 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a  red library..*/.
e9ae0 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73  /*.** Interfaces
e9af0 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73   for opening a s
e9b00 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66  hared library, f
e9b10 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69  inding entry poi
e9b20 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68  nts.** within th
e9b30 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79  e shared library
e9b40 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68  , and closing th
e9b50 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79  e shared library
e9b60 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
e9b70 20 2a 77 69 6e 44 6c 4f 70 65 6e 28 73 71 6c 69   *winDlOpen(sqli
e9b80 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63  te3_vfs *pVfs, c
e9b90 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
e9ba0 6e 61 6d 65 29 7b 0a 20 20 48 41 4e 44 4c 45 20  name){.  HANDLE 
e9bb0 68 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76  h;.  void *zConv
e9bc0 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55  erted = convertU
e9bd0 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 46 69 6c  tf8Filename(zFil
e9be0 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 43  ename);.  if( zC
e9bf0 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20  onverted==0 ){. 
e9c00 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
e9c10 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b  .  if( isNT() ){
e9c20 0a 20 20 20 20 68 20 3d 20 4c 6f 61 64 4c 69 62  .    h = LoadLib
e9c30 72 61 72 79 57 28 28 57 43 48 41 52 2a 29 7a 43  raryW((WCHAR*)zC
e9c40 6f 6e 76 65 72 74 65 64 29 3b 0a 2f 2a 20 69 73  onverted);./* is
e9c50 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c  NT() is 1 if SQL
e9c60 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c  ITE_OS_WINCE==1,
e9c70 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73   so this else is
e9c80 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e   never executed.
e9c90 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41   .** Since the A
e9ca0 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20  SCII version of 
e9cb0 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50  these Windows AP
e9cc0 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66  I do not exist f
e9cd0 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27  or WINCE,.** it'
e9ce0 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e  s important to n
e9cf0 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65  ot reference the
e9d00 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c  m for WINCE buil
e9d10 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54  ds..*/.#if SQLIT
e9d20 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20  E_OS_WINCE==0.  
e9d30 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 4c  }else{.    h = L
e9d40 6f 61 64 4c 69 62 72 61 72 79 41 28 28 63 68 61  oadLibraryA((cha
e9d50 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  r*)zConverted);.
e9d60 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72 65  #endif.  }.  fre
e9d70 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20  e(zConverted);. 
e9d80 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 68   return (void*)h
e9d90 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ;.}.static void 
e9da0 77 69 6e 44 6c 45 72 72 6f 72 28 73 71 6c 69 74  winDlError(sqlit
e9db0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e  e3_vfs *pVfs, in
e9dc0 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42  t nBuf, char *zB
e9dd0 75 66 4f 75 74 29 7b 0a 20 20 67 65 74 4c 61 73  ufOut){.  getLas
e9de0 74 45 72 72 6f 72 4d 73 67 28 6e 42 75 66 2c 20  tErrorMsg(nBuf, 
e9df0 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a 76 6f 69 64  zBufOut);.}.void
e9e00 20 2a 77 69 6e 44 6c 53 79 6d 28 73 71 6c 69 74   *winDlSym(sqlit
e9e10 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f  e3_vfs *pVfs, vo
e9e20 69 64 20 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e  id *pHandle, con
e9e30 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c  st char *zSymbol
e9e40 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  ){.#if SQLITE_OS
e9e50 5f 57 49 4e 43 45 0a 20 20 2f 2a 20 54 68 65 20  _WINCE.  /* The 
e9e60 47 65 74 50 72 6f 63 41 64 64 72 65 73 73 41 28  GetProcAddressA(
e9e70 29 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c  ) routine is onl
e9e80 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 77  y available on w
e9e90 69 6e 63 65 2e 20 2a 2f 0a 20 20 72 65 74 75 72  ince. */.  retur
e9ea0 6e 20 47 65 74 50 72 6f 63 41 64 64 72 65 73 73  n GetProcAddress
e9eb0 41 28 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c  A((HANDLE)pHandl
e9ec0 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65 6c  e, zSymbol);.#el
e9ed0 73 65 0a 20 20 2f 2a 20 41 6c 6c 20 6f 74 68 65  se.  /* All othe
e9ee0 72 20 77 69 6e 64 6f 77 73 20 70 6c 61 74 66 6f  r windows platfo
e9ef0 72 6d 73 20 65 78 70 65 63 74 20 47 65 74 50 72  rms expect GetPr
e9f00 6f 63 41 64 64 72 65 73 73 28 29 20 74 6f 20 74  ocAddress() to t
e9f10 61 6b 65 0a 20 20 2a 2a 20 61 6e 20 41 6e 73 69  ake.  ** an Ansi
e9f20 20 73 74 72 69 6e 67 20 72 65 67 61 72 64 6c 65   string regardle
e9f30 73 73 20 6f 66 20 74 68 65 20 5f 55 4e 49 43 4f  ss of the _UNICO
e9f40 44 45 20 73 65 74 74 69 6e 67 20 2a 2f 0a 20 20  DE setting */.  
e9f50 72 65 74 75 72 6e 20 47 65 74 50 72 6f 63 41 64  return GetProcAd
e9f60 64 72 65 73 73 28 28 48 41 4e 44 4c 45 29 70 48  dress((HANDLE)pH
e9f70 61 6e 64 6c 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b  andle, zSymbol);
e9f80 0a 23 65 6e 64 69 66 0a 7d 0a 76 6f 69 64 20 77  .#endif.}.void w
e9f90 69 6e 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65  inDlClose(sqlite
e9fa0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69  3_vfs *pVfs, voi
e9fb0 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 46  d *pHandle){.  F
e9fc0 72 65 65 4c 69 62 72 61 72 79 28 28 48 41 4e 44  reeLibrary((HAND
e9fd0 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23  LE)pHandle);.}.#
e9fe0 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c 49 54  else /* if SQLIT
e9ff0 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45  E_OMIT_LOAD_EXTE
ea000 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e 65 64  NSION is defined
ea010 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 77  : */.  #define w
ea020 69 6e 44 6c 4f 70 65 6e 20 20 30 0a 20 20 23 64  inDlOpen  0.  #d
ea030 65 66 69 6e 65 20 77 69 6e 44 6c 45 72 72 6f 72  efine winDlError
ea040 20 30 0a 20 20 23 64 65 66 69 6e 65 20 77 69 6e   0.  #define win
ea050 44 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65 66  DlSym   0.  #def
ea060 69 6e 65 20 77 69 6e 44 6c 43 6c 6f 73 65 20 30  ine winDlClose 0
ea070 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
ea080 57 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75 66  Write up to nBuf
ea090 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d   bytes of random
ea0a0 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e 0a  ness into zBuf..
ea0b0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
ea0c0 6e 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69  nRandomness(sqli
ea0d0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69  te3_vfs *pVfs, i
ea0e0 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a  nt nBuf, char *z
ea0f0 42 75 66 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20  Buf){.  int n = 
ea100 30 3b 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 28  0;.  if( sizeof(
ea110 53 59 53 54 45 4d 54 49 4d 45 29 3c 3d 6e 42 75  SYSTEMTIME)<=nBu
ea120 66 2d 6e 20 29 7b 0a 20 20 20 20 53 59 53 54 45  f-n ){.    SYSTE
ea130 4d 54 49 4d 45 20 78 3b 0a 20 20 20 20 47 65 74  MTIME x;.    Get
ea140 53 79 73 74 65 6d 54 69 6d 65 28 26 78 29 3b 0a  SystemTime(&x);.
ea150 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66      memcpy(&zBuf
ea160 5b 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28  [n], &x, sizeof(
ea170 78 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69  x));.    n += si
ea180 7a 65 6f 66 28 78 29 3b 0a 20 20 7d 0a 20 20 69  zeof(x);.  }.  i
ea190 66 28 20 73 69 7a 65 6f 66 28 44 57 4f 52 44 29  f( sizeof(DWORD)
ea1a0 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20  <=nBuf-n ){.    
ea1b0 44 57 4f 52 44 20 70 69 64 20 3d 20 47 65 74 43  DWORD pid = GetC
ea1c0 75 72 72 65 6e 74 50 72 6f 63 65 73 73 49 64 28  urrentProcessId(
ea1d0 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a  );.    memcpy(&z
ea1e0 42 75 66 5b 6e 5d 2c 20 26 70 69 64 2c 20 73 69  Buf[n], &pid, si
ea1f0 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 20  zeof(pid));.    
ea200 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 70 69 64 29  n += sizeof(pid)
ea210 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65  ;.  }.  if( size
ea220 6f 66 28 44 57 4f 52 44 29 3c 3d 6e 42 75 66 2d  of(DWORD)<=nBuf-
ea230 6e 20 29 7b 0a 20 20 20 20 44 57 4f 52 44 20 63  n ){.    DWORD c
ea240 6e 74 20 3d 20 47 65 74 54 69 63 6b 43 6f 75 6e  nt = GetTickCoun
ea250 74 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  t();.    memcpy(
ea260 26 7a 42 75 66 5b 6e 5d 2c 20 26 63 6e 74 2c 20  &zBuf[n], &cnt, 
ea270 73 69 7a 65 6f 66 28 63 6e 74 29 29 3b 0a 20 20  sizeof(cnt));.  
ea280 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 63 6e    n += sizeof(cn
ea290 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 69  t);.  }.  if( si
ea2a0 7a 65 6f 66 28 4c 41 52 47 45 5f 49 4e 54 45 47  zeof(LARGE_INTEG
ea2b0 45 52 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20  ER)<=nBuf-n ){. 
ea2c0 20 20 20 4c 41 52 47 45 5f 49 4e 54 45 47 45 52     LARGE_INTEGER
ea2d0 20 69 3b 0a 20 20 20 20 51 75 65 72 79 50 65 72   i;.    QueryPer
ea2e0 66 6f 72 6d 61 6e 63 65 43 6f 75 6e 74 65 72 28  formanceCounter(
ea2f0 26 69 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  &i);.    memcpy(
ea300 26 7a 42 75 66 5b 6e 5d 2c 20 26 69 2c 20 73 69  &zBuf[n], &i, si
ea310 7a 65 6f 66 28 69 29 29 3b 0a 20 20 20 20 6e 20  zeof(i));.    n 
ea320 2b 3d 20 73 69 7a 65 6f 66 28 69 29 3b 0a 20 20  += sizeof(i);.  
ea330 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a  }.  return n;.}.
ea340 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f  ../*.** Sleep fo
ea350 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65  r a little while
ea360 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d  .  Return the am
ea370 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65  ount of time sle
ea380 70 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  pt..*/.static in
ea390 74 20 77 69 6e 53 6c 65 65 70 28 73 71 6c 69 74  t winSleep(sqlit
ea3a0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e  e3_vfs *pVfs, in
ea3b0 74 20 6d 69 63 72 6f 73 65 63 29 7b 0a 20 20 53  t microsec){.  S
ea3c0 6c 65 65 70 28 28 6d 69 63 72 6f 73 65 63 2b 39  leep((microsec+9
ea3d0 39 39 29 2f 31 30 30 30 29 3b 0a 20 20 72 65 74  99)/1000);.  ret
ea3e0 75 72 6e 20 28 28 6d 69 63 72 6f 73 65 63 2b 39  urn ((microsec+9
ea3f0 39 39 29 2f 31 30 30 30 29 2a 31 30 30 30 3b 0a  99)/1000)*1000;.
ea400 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  }../*.** The fol
ea410 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c  lowing variable,
ea420 20 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e   if set to a non
ea430 2d 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63  -zero value, bec
ea440 6f 6d 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a  omes the result.
ea450 2a 2a 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d  ** returned from
ea460 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e   sqlite3OsCurren
ea470 74 54 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69  tTime().  This i
ea480 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69  s used for testi
ea490 6e 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ng..*/.#ifdef SQ
ea4a0 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
ea4b0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
ea4c0 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20  _current_time = 
ea4d0 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  0;.#endif../*.**
ea4e0 20 46 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e   Find the curren
ea4f0 74 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65  t time (in Unive
ea500 72 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64  rsal Coordinated
ea510 20 54 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74   Time).  Write t
ea520 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69  he.** current ti
ea530 6d 65 20 61 6e 64 20 64 61 74 65 20 61 73 20 61  me and date as a
ea540 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62   Julian Day numb
ea550 65 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61  er into *prNow a
ea560 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20  nd.** return 0. 
ea570 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65   Return 1 if the
ea580 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63   time and date c
ea590 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a  annot be found..
ea5a0 2a 2f 0a 69 6e 74 20 77 69 6e 43 75 72 72 65 6e  */.int winCurren
ea5b0 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66  tTime(sqlite3_vf
ea5c0 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20  s *pVfs, double 
ea5d0 2a 70 72 4e 6f 77 29 7b 0a 20 20 46 49 4c 45 54  *prNow){.  FILET
ea5e0 49 4d 45 20 66 74 3b 0a 20 20 2f 2a 20 46 49 4c  IME ft;.  /* FIL
ea5f0 45 54 49 4d 45 20 73 74 72 75 63 74 75 72 65 20  ETIME structure 
ea600 69 73 20 61 20 36 34 2d 62 69 74 20 76 61 6c 75  is a 64-bit valu
ea610 65 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74  e representing t
ea620 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 0a 20 20  he number of .  
ea630 20 20 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e     100-nanosecon
ea640 64 20 69 6e 74 65 72 76 61 6c 73 20 73 69 6e 63  d intervals sinc
ea650 65 20 4a 61 6e 75 61 72 79 20 31 2c 20 31 36 30  e January 1, 160
ea660 31 20 28 3d 20 4a 44 20 32 33 30 35 38 31 33 2e  1 (= JD 2305813.
ea670 35 29 2e 20 0a 20 20 2a 2f 0a 20 20 64 6f 75 62  5). .  */.  doub
ea680 6c 65 20 6e 6f 77 3b 0a 23 69 66 20 53 51 4c 49  le now;.#if SQLI
ea690 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 53 59  TE_OS_WINCE.  SY
ea6a0 53 54 45 4d 54 49 4d 45 20 74 69 6d 65 3b 0a 20  STEMTIME time;. 
ea6b0 20 47 65 74 53 79 73 74 65 6d 54 69 6d 65 28 26   GetSystemTime(&
ea6c0 74 69 6d 65 29 3b 0a 20 20 2f 2a 20 69 66 20 53  time);.  /* if S
ea6d0 79 73 74 65 6d 54 69 6d 65 54 6f 46 69 6c 65 54  ystemTimeToFileT
ea6e0 69 6d 65 28 29 20 66 61 69 6c 73 2c 20 69 74 20  ime() fails, it 
ea6f0 72 65 74 75 72 6e 73 20 7a 65 72 6f 2e 20 2a 2f  returns zero. */
ea700 0a 20 20 69 66 20 28 21 53 79 73 74 65 6d 54 69  .  if (!SystemTi
ea710 6d 65 54 6f 46 69 6c 65 54 69 6d 65 28 26 74 69  meToFileTime(&ti
ea720 6d 65 2c 26 66 74 29 29 7b 0a 20 20 20 20 72 65  me,&ft)){.    re
ea730 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6c 73  turn 1;.  }.#els
ea740 65 0a 20 20 47 65 74 53 79 73 74 65 6d 54 69 6d  e.  GetSystemTim
ea750 65 41 73 46 69 6c 65 54 69 6d 65 28 20 26 66 74  eAsFileTime( &ft
ea760 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 6e 6f 77   );.#endif.  now
ea770 20 3d 20 28 28 64 6f 75 62 6c 65 29 66 74 2e 64   = ((double)ft.d
ea780 77 48 69 67 68 44 61 74 65 54 69 6d 65 29 20 2a  wHighDateTime) *
ea790 20 34 32 39 34 39 36 37 32 39 36 2e 30 3b 20 0a   4294967296.0; .
ea7a0 20 20 2a 70 72 4e 6f 77 20 3d 20 28 6e 6f 77 20    *prNow = (now 
ea7b0 2b 20 66 74 2e 64 77 4c 6f 77 44 61 74 65 54 69  + ft.dwLowDateTi
ea7c0 6d 65 29 2f 38 36 34 30 30 30 30 30 30 30 30 30  me)/864000000000
ea7d0 2e 30 20 2b 20 32 33 30 35 38 31 33 2e 35 3b 0a  .0 + 2305813.5;.
ea7e0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
ea7f0 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ST.  if( sqlite3
ea800 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b  _current_time ){
ea810 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 73 71  .    *prNow = sq
ea820 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69  lite3_current_ti
ea830 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34  me/86400.0 + 244
ea840 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64  0587.5;.  }.#end
ea850 69 66 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  if.  return 0;.}
ea860 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 64 65 61  ../*.** The idea
ea870 20 69 73 20 74 68 61 74 20 74 68 69 73 20 66 75   is that this fu
ea880 6e 63 74 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b  nction works lik
ea890 65 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20  e a combination 
ea8a0 6f 66 0a 2a 2a 20 47 65 74 4c 61 73 74 45 72 72  of.** GetLastErr
ea8b0 6f 72 28 29 20 61 6e 64 20 46 6f 72 6d 61 74 4d  or() and FormatM
ea8c0 65 73 73 61 67 65 28 29 20 6f 6e 20 77 69 6e 64  essage() on wind
ea8d0 6f 77 73 20 28 6f 72 20 65 72 72 6e 6f 20 61 6e  ows (or errno an
ea8e0 64 0a 2a 2a 20 73 74 72 65 72 72 6f 72 5f 72 28  d.** strerror_r(
ea8f0 29 20 6f 6e 20 75 6e 69 78 29 2e 20 41 66 74 65  ) on unix). Afte
ea900 72 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65  r an error is re
ea910 74 75 72 6e 65 64 20 62 79 20 61 6e 20 4f 53 0a  turned by an OS.
ea920 2a 2a 20 66 75 6e 63 74 69 6f 6e 2c 20 53 51 4c  ** function, SQL
ea930 69 74 65 20 63 61 6c 6c 73 20 74 68 69 73 20 66  ite calls this f
ea940 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 7a 42 75  unction with zBu
ea950 66 20 70 6f 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a  f pointing to.**
ea960 20 61 20 62 75 66 66 65 72 20 6f 66 20 6e 42 75   a buffer of nBu
ea970 66 20 62 79 74 65 73 2e 20 54 68 65 20 4f 53 20  f bytes. The OS 
ea980 6c 61 79 65 72 20 73 68 6f 75 6c 64 20 70 6f 70  layer should pop
ea990 75 6c 61 74 65 20 74 68 65 0a 2a 2a 20 62 75 66  ulate the.** buf
ea9a0 66 65 72 20 77 69 74 68 20 61 20 6e 75 6c 2d 74  fer with a nul-t
ea9b0 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20  erminated UTF-8 
ea9c0 65 6e 63 6f 64 65 64 20 65 72 72 6f 72 20 6d 65  encoded error me
ea9d0 73 73 61 67 65 0a 2a 2a 20 64 65 73 63 72 69 62  ssage.** describ
ea9e0 69 6e 67 20 74 68 65 20 6c 61 73 74 20 49 4f 20  ing the last IO 
ea9f0 65 72 72 6f 72 20 74 6f 20 68 61 76 65 20 6f 63  error to have oc
eaa00 63 75 72 65 64 20 77 69 74 68 69 6e 20 74 68 65  cured within the
eaa10 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 72 65   calling.** thre
eaa20 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ad..**.** If the
eaa30 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
eaa40 73 20 74 6f 6f 20 6c 61 72 67 65 20 66 6f 72 20  s too large for 
eaa50 74 68 65 20 73 75 70 70 6c 69 65 64 20 62 75 66  the supplied buf
eaa60 66 65 72 2c 0a 2a 2a 20 69 74 20 73 68 6f 75 6c  fer,.** it shoul
eaa70 64 20 62 65 20 74 72 75 6e 63 61 74 65 64 2e 20  d be truncated. 
eaa80 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
eaa90 20 6f 66 20 78 47 65 74 4c 61 73 74 45 72 72 6f   of xGetLastErro
eaaa0 72 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 69 66 20  r.** is zero if 
eaab0 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  the error messag
eaac0 65 20 66 69 74 73 20 69 6e 20 74 68 65 20 62 75  e fits in the bu
eaad0 66 66 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72  ffer, or non-zer
eaae0 6f 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20 28  o.** otherwise (
eaaf0 69 66 20 74 68 65 20 6d 65 73 73 61 67 65 20 77  if the message w
eab00 61 73 20 74 72 75 6e 63 61 74 65 64 29 2e 20 49  as truncated). I
eab10 66 20 6e 6f 6e 2d 7a 65 72 6f 20 69 73 20 72 65  f non-zero is re
eab20 74 75 72 6e 65 64 2c 0a 2a 2a 20 74 68 65 6e 20  turned,.** then 
eab30 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73  it is not necess
eab40 61 72 79 20 74 6f 20 69 6e 63 6c 75 64 65 20 74  ary to include t
eab50 68 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f  he nul-terminato
eab60 72 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 69  r character.** i
eab70 6e 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66  n the output buf
eab80 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 20 73  fer..**.** Not s
eab90 75 70 70 6c 79 69 6e 67 20 61 6e 20 65 72 72 6f  upplying an erro
eaba0 72 20 6d 65 73 73 61 67 65 20 77 69 6c 6c 20 68  r message will h
eabb0 61 76 65 20 6e 6f 20 61 64 76 65 72 73 65 20 65  ave no adverse e
eabc0 66 66 65 63 74 0a 2a 2a 20 6f 6e 20 53 51 4c 69  ffect.** on SQLi
eabd0 74 65 2e 20 49 74 20 69 73 20 66 69 6e 65 20 74  te. It is fine t
eabe0 6f 20 68 61 76 65 20 61 6e 20 69 6d 70 6c 65 6d  o have an implem
eabf0 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 6e 65  entation that ne
eac00 76 65 72 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61  ver.** returns a
eac10 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 3a  n error message:
eac20 0a 2a 2a 0a 2a 2a 20 20 20 69 6e 74 20 78 47 65  .**.**   int xGe
eac30 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74  tLastError(sqlit
eac40 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e  e3_vfs *pVfs, in
eac50 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42  t nBuf, char *zB
eac60 75 66 29 7b 0a 2a 2a 20 20 20 20 20 61 73 73 65  uf){.**     asse
eac70 72 74 28 7a 42 75 66 5b 30 5d 3d 3d 27 5c 30 27  rt(zBuf[0]=='\0'
eac80 29 3b 0a 2a 2a 20 20 20 20 20 72 65 74 75 72 6e  );.**     return
eac90 20 30 3b 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a   0;.**   }.**.**
eaca0 20 48 6f 77 65 76 65 72 20 69 66 20 61 6e 20 65   However if an e
eacb0 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20  rror message is 
eacc0 73 75 70 70 6c 69 65 64 2c 20 69 74 20 77 69 6c  supplied, it wil
eacd0 6c 20 62 65 20 69 6e 63 6f 72 70 6f 72 61 74 65  l be incorporate
eace0 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 20 69  d.** by sqlite i
eacf0 6e 74 6f 20 74 68 65 20 65 72 72 6f 72 20 6d 65  nto the error me
ead00 73 73 61 67 65 20 61 76 61 69 6c 61 62 6c 65 20  ssage available 
ead10 74 6f 20 74 68 65 20 75 73 65 72 20 75 73 69 6e  to the user usin
ead20 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72  g.** sqlite3_err
ead30 6d 73 67 28 29 2c 20 70 6f 73 73 69 62 6c 79 20  msg(), possibly 
ead40 6d 61 6b 69 6e 67 20 49 4f 20 65 72 72 6f 72 73  making IO errors
ead50 20 65 61 73 69 65 72 20 74 6f 20 64 65 62 75 67   easier to debug
ead60 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
ead70 77 69 6e 47 65 74 4c 61 73 74 45 72 72 6f 72 28  winGetLastError(
ead80 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
ead90 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61  s, int nBuf, cha
eada0 72 20 2a 7a 42 75 66 29 7b 0a 20 20 72 65 74 75  r *zBuf){.  retu
eadb0 72 6e 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d  rn getLastErrorM
eadc0 73 67 28 6e 42 75 66 2c 20 7a 42 75 66 29 3b 0a  sg(nBuf, zBuf);.
eadd0 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c  }../*.** Initial
eade0 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61  ize and deinitia
eadf0 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 69  lize the operati
eae00 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66  ng system interf
eae10 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ace..*/.SQLITE_A
eae20 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
eae30 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20  s_init(void){.  
eae40 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76  static sqlite3_v
eae50 66 73 20 77 69 6e 56 66 73 20 3d 20 7b 0a 20 20  fs winVfs = {.  
eae60 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20    1,            
eae70 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e       /* iVersion
eae80 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 77   */.    sizeof(w
eae90 69 6e 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73 7a  inFile),   /* sz
eaea0 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 4d 41  OsFile */.    MA
eaeb0 58 5f 50 41 54 48 2c 20 20 20 20 20 20 20 20 20  X_PATH,         
eaec0 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a   /* mxPathname *
eaed0 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  /.    0,        
eaee0 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78           /* pNex
eaef0 74 20 2a 2f 0a 20 20 20 20 22 77 69 6e 33 32 22  t */.    "win32"
eaf00 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a  ,           /* z
eaf10 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20  Name */.    0,  
eaf20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
eaf30 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 20 0a  * pAppData */. .
eaf40 20 20 20 20 77 69 6e 4f 70 65 6e 2c 20 20 20 20      winOpen,    
eaf50 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20         /* xOpen 
eaf60 2a 2f 0a 20 20 20 20 77 69 6e 44 65 6c 65 74 65  */.    winDelete
eaf70 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65  ,         /* xDe
eaf80 6c 65 74 65 20 2a 2f 0a 20 20 20 20 77 69 6e 41  lete */.    winA
eaf90 63 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 2f  ccess,         /
eafa0 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20  * xAccess */.   
eafb0 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65   winFullPathname
eafc0 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68  ,   /* xFullPath
eafd0 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e 44  name */.    winD
eafe0 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 2f  lOpen,         /
eaff0 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20  * xDlOpen */.   
eb000 20 77 69 6e 44 6c 45 72 72 6f 72 2c 20 20 20 20   winDlError,    
eb010 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20      /* xDlError 
eb020 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 53 79 6d 2c  */.    winDlSym,
eb030 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c            /* xDl
eb040 53 79 6d 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c  Sym */.    winDl
eb050 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 2f 2a  Close,        /*
eb060 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 20 20   xDlClose */.   
eb070 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 2c 20   winRandomness, 
eb080 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65      /* xRandomne
eb090 73 73 20 2a 2f 0a 20 20 20 20 77 69 6e 53 6c 65  ss */.    winSle
eb0a0 65 70 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ep,          /* 
eb0b0 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20 77 69  xSleep */.    wi
eb0c0 6e 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20  nCurrentTime,   
eb0d0 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65   /* xCurrentTime
eb0e0 20 2a 2f 0a 20 20 20 20 77 69 6e 47 65 74 4c 61   */.    winGetLa
eb0f0 73 74 45 72 72 6f 72 20 20 20 20 2f 2a 20 78 47  stError    /* xG
eb100 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 0a 20  etLastError */. 
eb110 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66   };.  sqlite3_vf
eb120 73 5f 72 65 67 69 73 74 65 72 28 26 77 69 6e 56  s_register(&winV
eb130 66 73 2c 20 31 29 3b 0a 20 20 72 65 74 75 72 6e  fs, 1);.  return
eb140 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 53   SQLITE_OK; .}.S
eb150 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
eb160 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69  lite3_os_end(voi
eb170 64 29 7b 20 0a 20 20 72 65 74 75 72 6e 20 53 51  d){ .  return SQ
eb180 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64  LITE_OK;.}..#end
eb190 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f  if /* SQLITE_OS_
eb1a0 57 49 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  WIN */../*******
eb1b0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f  ******* End of o
eb1c0 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  s_win.c ********
eb1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb1f0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
eb200 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
eb210 6c 65 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a 2a  le bitvec.c ****
eb220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb240 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
eb250 30 38 20 46 65 62 72 75 61 72 79 20 31 36 0a 2a  08 February 16.*
eb260 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
eb270 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
eb280 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
eb290 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
eb2a0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
eb2b0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
eb2c0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
eb2d0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
eb2e0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
eb2f0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
eb300 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
eb310 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
eb320 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
eb330 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
eb340 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
eb350 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
eb360 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
eb370 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
eb380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb3a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb3b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
eb3c0 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65   This file imple
eb3d0 6d 65 6e 74 73 20 61 6e 20 6f 62 6a 65 63 74 20  ments an object 
eb3e0 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 20  that represents 
eb3f0 61 20 66 69 78 65 64 2d 6c 65 6e 67 74 68 0a 2a  a fixed-length.*
eb400 2a 20 62 69 74 6d 61 70 2e 20 20 42 69 74 73 20  * bitmap.  Bits 
eb410 61 72 65 20 6e 75 6d 62 65 72 65 64 20 73 74 61  are numbered sta
eb420 72 74 69 6e 67 20 77 69 74 68 20 31 2e 0a 2a 2a  rting with 1..**
eb430 0a 2a 2a 20 41 20 62 69 74 6d 61 70 20 69 73 20  .** A bitmap is 
eb440 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 77  used to record w
eb450 68 69 63 68 20 70 61 67 65 73 20 6f 66 20 61 20  hich pages of a 
eb460 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61  database file ha
eb470 76 65 20 62 65 65 6e 0a 2a 2a 20 6a 6f 75 72 6e  ve been.** journ
eb480 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 61 20 74  alled during a t
eb490 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 77  ransaction, or w
eb4a0 68 69 63 68 20 70 61 67 65 73 20 68 61 76 65 20  hich pages have 
eb4b0 74 68 65 20 22 64 6f 6e 74 2d 77 72 69 74 65 22  the "dont-write"
eb4c0 0a 2a 2a 20 70 72 6f 70 65 72 74 79 2e 20 20 55  .** property.  U
eb4d0 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 61 20 66 65  sually only a fe
eb4e0 77 20 70 61 67 65 73 20 61 72 65 20 6d 65 65 74  w pages are meet
eb4f0 20 65 69 74 68 65 72 20 63 6f 6e 64 69 74 69 6f   either conditio
eb500 6e 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 62 69 74  n..** So the bit
eb510 6d 61 70 20 69 73 20 75 73 75 61 6c 6c 79 20 73  map is usually s
eb520 70 61 72 73 65 20 61 6e 64 20 68 61 73 20 6c 6f  parse and has lo
eb530 77 20 63 61 72 64 69 6e 61 6c 69 74 79 2e 0a 2a  w cardinality..*
eb540 2a 20 42 75 74 20 73 6f 6d 65 74 69 6d 65 73 20  * But sometimes 
eb550 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 77 68 65  (for example whe
eb560 6e 20 64 75 72 69 6e 67 20 61 20 44 52 4f 50 20  n during a DROP 
eb570 6f 66 20 61 20 6c 61 72 67 65 20 74 61 62 6c 65  of a large table
eb580 29 20 6d 6f 73 74 0a 2a 2a 20 6f 72 20 61 6c 6c  ) most.** or all
eb590 20 6f 66 20 74 68 65 20 70 61 67 65 73 20 69 6e   of the pages in
eb5a0 20 61 20 64 61 74 61 62 61 73 65 20 63 61 6e 20   a database can 
eb5b0 67 65 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 2e 20  get journalled. 
eb5c0 20 49 6e 20 74 68 6f 73 65 20 63 61 73 65 73 2c   In those cases,
eb5d0 20 0a 2a 2a 20 74 68 65 20 62 69 74 6d 61 70 20   .** the bitmap 
eb5e0 62 65 63 6f 6d 65 73 20 64 65 6e 73 65 20 77 69  becomes dense wi
eb5f0 74 68 20 68 69 67 68 20 63 61 72 64 69 6e 61 6c  th high cardinal
eb600 69 74 79 2e 20 20 54 68 65 20 61 6c 67 6f 72 69  ity.  The algori
eb610 74 68 6d 20 6e 65 65 64 73 20 0a 2a 2a 20 74 6f  thm needs .** to
eb620 20 68 61 6e 64 6c 65 20 62 6f 74 68 20 63 61 73   handle both cas
eb630 65 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54  es well..**.** T
eb640 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62  he size of the b
eb650 69 74 6d 61 70 20 69 73 20 66 69 78 65 64 20 77  itmap is fixed w
eb660 68 65 6e 20 74 68 65 20 6f 62 6a 65 63 74 20 69  hen the object i
eb670 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a  s created..**.**
eb680 20 41 6c 6c 20 62 69 74 73 20 61 72 65 20 63 6c   All bits are cl
eb690 65 61 72 20 77 68 65 6e 20 74 68 65 20 62 69 74  ear when the bit
eb6a0 6d 61 70 20 69 73 20 63 72 65 61 74 65 64 2e 20  map is created. 
eb6b0 20 49 6e 64 69 76 69 64 75 61 6c 20 62 69 74 73   Individual bits
eb6c0 0a 2a 2a 20 6d 61 79 20 62 65 20 73 65 74 20 6f  .** may be set o
eb6d0 72 20 63 6c 65 61 72 65 64 20 6f 6e 65 20 61 74  r cleared one at
eb6e0 20 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54   a time..**.** T
eb6f0 65 73 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 61  est operations a
eb700 72 65 20 61 62 6f 75 74 20 31 30 30 20 74 69 6d  re about 100 tim
eb710 65 73 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 20 74  es more common t
eb720 68 61 74 20 73 65 74 20 6f 70 65 72 61 74 69 6f  hat set operatio
eb730 6e 73 2e 0a 2a 2a 20 43 6c 65 61 72 20 6f 70 65  ns..** Clear ope
eb740 72 61 74 69 6f 6e 73 20 61 72 65 20 65 78 63 65  rations are exce
eb750 65 64 69 6e 67 6c 79 20 72 61 72 65 2e 20 20 54  edingly rare.  T
eb760 68 65 72 65 20 61 72 65 20 75 73 75 61 6c 6c 79  here are usually
eb770 20 62 65 74 77 65 65 6e 0a 2a 2a 20 35 20 61 6e   between.** 5 an
eb780 64 20 35 30 30 20 73 65 74 20 6f 70 65 72 61 74  d 500 set operat
eb790 69 6f 6e 73 20 70 65 72 20 42 69 74 76 65 63 20  ions per Bitvec 
eb7a0 6f 62 6a 65 63 74 2c 20 74 68 6f 75 67 68 20 74  object, though t
eb7b0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 74  he number of set
eb7c0 73 20 63 61 6e 0a 2a 2a 20 73 6f 6d 65 74 69 6d  s can.** sometim
eb7d0 65 73 20 67 72 6f 77 20 69 6e 74 6f 20 74 65 6e  es grow into ten
eb7e0 73 20 6f 66 20 74 68 6f 75 73 61 6e 64 73 20 6f  s of thousands o
eb7f0 72 20 6c 61 72 67 65 72 2e 20 20 54 68 65 20 73  r larger.  The s
eb800 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 42 69  ize of the.** Bi
eb810 74 76 65 63 20 6f 62 6a 65 63 74 20 69 73 20 74  tvec object is t
eb820 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
eb830 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  es in the databa
eb840 73 65 20 66 69 6c 65 20 61 74 20 74 68 65 0a 2a  se file at the.*
eb850 2a 20 73 74 61 72 74 20 6f 66 20 61 20 74 72 61  * start of a tra
eb860 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64 20 69 73  nsaction, and is
eb870 20 74 68 75 73 20 75 73 75 61 6c 6c 79 20 6c 65   thus usually le
eb880 73 73 20 74 68 61 6e 20 61 20 66 65 77 20 74 68  ss than a few th
eb890 6f 75 73 61 6e 64 2c 0a 2a 2a 20 62 75 74 20 63  ousand,.** but c
eb8a0 61 6e 20 62 65 20 61 73 20 6c 61 72 67 65 20 61  an be as large a
eb8b0 73 20 32 20 62 69 6c 6c 69 6f 6e 20 66 6f 72 20  s 2 billion for 
eb8c0 61 20 72 65 61 6c 6c 79 20 62 69 67 20 64 61 74  a really big dat
eb8d0 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23  abase..**.** @(#
eb8e0 29 20 24 49 64 3a 20 62 69 74 76 65 63 2e 63 2c  ) $Id: bitvec.c,
eb8f0 76 20 31 2e 37 20 32 30 30 38 2f 31 31 2f 30 33  v 1.7 2008/11/03
eb900 20 32 30 3a 35 35 3a 30 37 20 64 72 68 20 45 78   20:55:07 drh Ex
eb910 70 20 24 0a 2a 2f 0a 0a 23 64 65 66 69 6e 65 20  p $.*/..#define 
eb920 42 49 54 56 45 43 5f 53 5a 20 20 20 20 20 20 20  BITVEC_SZ       
eb930 20 35 31 32 0a 2f 2a 20 52 6f 75 6e 64 20 74 68   512./* Round th
eb940 65 20 75 6e 69 6f 6e 20 73 69 7a 65 20 64 6f 77  e union size dow
eb950 6e 20 74 6f 20 74 68 65 20 6e 65 61 72 65 73 74  n to the nearest
eb960 20 70 6f 69 6e 74 65 72 20 62 6f 75 6e 64 61 72   pointer boundar
eb970 79 2c 20 73 69 6e 63 65 20 74 68 61 74 27 73 20  y, since that's 
eb980 68 6f 77 20 0a 2a 2a 20 69 74 20 77 69 6c 6c 20  how .** it will 
eb990 62 65 20 61 6c 69 67 6e 65 64 20 77 69 74 68 69  be aligned withi
eb9a0 6e 20 74 68 65 20 42 69 74 76 65 63 20 73 74 72  n the Bitvec str
eb9b0 75 63 74 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  uct. */.#define 
eb9c0 42 49 54 56 45 43 5f 55 53 49 5a 45 20 20 20 20  BITVEC_USIZE    
eb9d0 20 28 28 28 42 49 54 56 45 43 5f 53 5a 2d 31 32   (((BITVEC_SZ-12
eb9e0 29 2f 73 69 7a 65 6f 66 28 42 69 74 76 65 63 2a  )/sizeof(Bitvec*
eb9f0 29 29 2a 73 69 7a 65 6f 66 28 42 69 74 76 65 63  ))*sizeof(Bitvec
eba00 2a 29 29 0a 23 64 65 66 69 6e 65 20 42 49 54 56  *)).#define BITV
eba10 45 43 5f 4e 43 48 41 52 20 20 20 20 20 42 49 54  EC_NCHAR     BIT
eba20 56 45 43 5f 55 53 49 5a 45 0a 23 64 65 66 69 6e  VEC_USIZE.#defin
eba30 65 20 42 49 54 56 45 43 5f 4e 42 49 54 20 20 20  e BITVEC_NBIT   
eba40 20 20 20 28 42 49 54 56 45 43 5f 4e 43 48 41 52     (BITVEC_NCHAR
eba50 2a 38 29 0a 23 64 65 66 69 6e 65 20 42 49 54 56  *8).#define BITV
eba60 45 43 5f 4e 49 4e 54 20 20 20 20 20 20 28 42 49  EC_NINT      (BI
eba70 54 56 45 43 5f 55 53 49 5a 45 2f 34 29 0a 23 64  TVEC_USIZE/4).#d
eba80 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4d 58 48  efine BITVEC_MXH
eba90 41 53 48 20 20 20 20 28 42 49 54 56 45 43 5f 4e  ASH    (BITVEC_N
ebaa0 49 4e 54 2f 32 29 0a 23 64 65 66 69 6e 65 20 42  INT/2).#define B
ebab0 49 54 56 45 43 5f 4e 50 54 52 20 20 20 20 20 20  ITVEC_NPTR      
ebac0 28 42 49 54 56 45 43 5f 55 53 49 5a 45 2f 73 69  (BITVEC_USIZE/si
ebad0 7a 65 6f 66 28 42 69 74 76 65 63 20 2a 29 29 0a  zeof(Bitvec *)).
ebae0 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f  .#define BITVEC_
ebaf0 48 41 53 48 28 58 29 20 20 20 28 28 28 58 29 2a  HASH(X)   (((X)*
ebb00 33 37 29 25 42 49 54 56 45 43 5f 4e 49 4e 54 29  37)%BITVEC_NINT)
ebb10 0a 0a 2f 2a 0a 2a 2a 20 41 20 62 69 74 6d 61 70  ../*.** A bitmap
ebb20 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   is an instance 
ebb30 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
ebb40 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a   structure..**.*
ebb50 2a 20 54 68 69 73 20 62 69 74 6d 61 70 20 72 65  * This bitmap re
ebb60 63 6f 72 64 73 20 74 68 65 20 65 78 69 73 74 61  cords the exista
ebb70 6e 63 65 20 6f 66 20 7a 65 72 6f 20 6f 72 20 6d  nce of zero or m
ebb80 6f 72 65 20 62 69 74 73 0a 2a 2a 20 77 69 74 68  ore bits.** with
ebb90 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20   values between 
ebba0 31 20 61 6e 64 20 69 53 69 7a 65 2c 20 69 6e 63  1 and iSize, inc
ebbb0 6c 75 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  lusive..**.** Th
ebbc0 65 72 65 20 61 72 65 20 74 68 72 65 65 20 70 6f  ere are three po
ebbd0 73 73 69 62 6c 65 20 72 65 70 72 65 73 65 6e 74  ssible represent
ebbe0 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 62 69  ations of the bi
ebbf0 74 6d 61 70 2e 0a 2a 2a 20 49 66 20 69 53 69 7a  tmap..** If iSiz
ebc00 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 2c 20  e<=BITVEC_NBIT, 
ebc10 74 68 65 6e 20 42 69 74 76 65 63 2e 75 2e 61 42  then Bitvec.u.aB
ebc20 69 74 6d 61 70 5b 5d 20 69 73 20 61 20 73 74 72  itmap[] is a str
ebc30 61 69 67 68 74 0a 2a 2a 20 62 69 74 6d 61 70 2e  aight.** bitmap.
ebc40 20 20 54 68 65 20 6c 65 61 73 74 20 73 69 67 6e    The least sign
ebc50 69 66 69 63 61 6e 74 20 62 69 74 20 69 73 20 62  ificant bit is b
ebc60 69 74 20 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69  it 1..**.** If i
ebc70 53 69 7a 65 3e 42 49 54 56 45 43 5f 4e 42 49 54  Size>BITVEC_NBIT
ebc80 20 61 6e 64 20 69 44 69 76 69 73 6f 72 3d 3d 30   and iDivisor==0
ebc90 20 74 68 65 6e 20 42 69 74 76 65 63 2e 75 2e 61   then Bitvec.u.a
ebca0 48 61 73 68 5b 5d 20 69 73 0a 2a 2a 20 61 20 68  Hash[] is.** a h
ebcb0 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 20 77  ash table that w
ebcc0 69 6c 6c 20 68 6f 6c 64 20 75 70 20 74 6f 20 42  ill hold up to B
ebcd0 49 54 56 45 43 5f 4d 58 48 41 53 48 20 64 69 73  ITVEC_MXHASH dis
ebce0 74 69 6e 63 74 20 76 61 6c 75 65 73 2e 0a 2a 2a  tinct values..**
ebcf0 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74  .** Otherwise, t
ebd00 68 65 20 76 61 6c 75 65 20 69 20 69 73 20 72 65  he value i is re
ebd10 64 69 72 65 63 74 65 64 20 69 6e 74 6f 20 6f 6e  directed into on
ebd20 65 20 6f 66 20 42 49 54 56 45 43 5f 4e 50 54 52  e of BITVEC_NPTR
ebd30 0a 2a 2a 20 73 75 62 2d 62 69 74 6d 61 70 73 20  .** sub-bitmaps 
ebd40 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 42 69  pointed to by Bi
ebd50 74 76 65 63 2e 75 2e 61 70 53 75 62 5b 5d 2e 20  tvec.u.apSub[]. 
ebd60 20 45 61 63 68 20 73 75 62 62 69 74 6d 61 70 0a   Each subbitmap.
ebd70 2a 2a 20 68 61 6e 64 6c 65 73 20 75 70 20 74 6f  ** handles up to
ebd80 20 69 44 69 76 69 73 6f 72 20 73 65 70 61 72 61   iDivisor separa
ebd90 74 65 20 76 61 6c 75 65 73 20 6f 66 20 69 2e 20  te values of i. 
ebda0 20 61 70 53 75 62 5b 30 5d 20 68 6f 6c 64 73 0a   apSub[0] holds.
ebdb0 2a 2a 20 76 61 6c 75 65 73 20 62 65 74 77 65 65  ** values betwee
ebdc0 6e 20 31 20 61 6e 64 20 69 44 69 76 69 73 6f 72  n 1 and iDivisor
ebdd0 2e 20 20 61 70 53 75 62 5b 31 5d 20 68 6f 6c 64  .  apSub[1] hold
ebde0 73 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e  s values between
ebdf0 0a 2a 2a 20 69 44 69 76 69 73 6f 72 2b 31 20 61  .** iDivisor+1 a
ebe00 6e 64 20 32 2a 69 44 69 76 69 73 6f 72 2e 20 20  nd 2*iDivisor.  
ebe10 61 70 53 75 62 5b 4e 5d 20 68 6f 6c 64 73 20 76  apSub[N] holds v
ebe20 61 6c 75 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a  alues between.**
ebe30 20 4e 2a 69 44 69 76 69 73 6f 72 2b 31 20 61 6e   N*iDivisor+1 an
ebe40 64 20 28 4e 2b 31 29 2a 69 44 69 76 69 73 6f 72  d (N+1)*iDivisor
ebe50 2e 20 20 45 61 63 68 20 73 75 62 62 69 74 6d 61  .  Each subbitma
ebe60 70 20 69 73 20 6e 6f 72 6d 61 6c 69 7a 65 64 0a  p is normalized.
ebe70 2a 2a 20 74 6f 20 68 6f 6c 64 20 64 65 61 6c 20  ** to hold deal 
ebe80 77 69 74 68 20 76 61 6c 75 65 73 20 62 65 74 77  with values betw
ebe90 65 65 6e 20 31 20 61 6e 64 20 69 44 69 76 69 73  een 1 and iDivis
ebea0 6f 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 69  or..*/.struct Bi
ebeb0 74 76 65 63 20 7b 0a 20 20 75 33 32 20 69 53 69  tvec {.  u32 iSi
ebec0 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4d 61 78 69  ze;      /* Maxi
ebed0 6d 75 6d 20 62 69 74 20 69 6e 64 65 78 20 2a 2f  mum bit index */
ebee0 0a 20 20 75 33 32 20 6e 53 65 74 3b 20 20 20 20  .  u32 nSet;    
ebef0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
ebf00 62 69 74 73 20 74 68 61 74 20 61 72 65 20 73 65  bits that are se
ebf10 74 20 2a 2f 0a 20 20 75 33 32 20 69 44 69 76 69  t */.  u32 iDivi
ebf20 73 6f 72 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72  sor;   /* Number
ebf30 20 6f 66 20 62 69 74 73 20 68 61 6e 64 6c 65 64   of bits handled
ebf40 20 62 79 20 65 61 63 68 20 61 70 53 75 62 5b 5d   by each apSub[]
ebf50 20 65 6e 74 72 79 20 2a 2f 0a 20 20 75 6e 69 6f   entry */.  unio
ebf60 6e 20 7b 0a 20 20 20 20 75 38 20 61 42 69 74 6d  n {.    u8 aBitm
ebf70 61 70 5b 42 49 54 56 45 43 5f 4e 43 48 41 52 5d  ap[BITVEC_NCHAR]
ebf80 3b 20 20 20 20 2f 2a 20 42 69 74 6d 61 70 20 72  ;    /* Bitmap r
ebf90 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f  epresentation */
ebfa0 0a 20 20 20 20 75 33 32 20 61 48 61 73 68 5b 42  .    u32 aHash[B
ebfb0 49 54 56 45 43 5f 4e 49 4e 54 5d 3b 20 20 20 20  ITVEC_NINT];    
ebfc0 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20    /* Hash table 
ebfd0 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a  representation *
ebfe0 2f 0a 20 20 20 20 42 69 74 76 65 63 20 2a 61 70  /.    Bitvec *ap
ebff0 53 75 62 5b 42 49 54 56 45 43 5f 4e 50 54 52 5d  Sub[BITVEC_NPTR]
ec000 3b 20 20 2f 2a 20 52 65 63 75 72 73 69 76 65 20  ;  /* Recursive 
ec010 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a  representation *
ec020 2f 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a  /.  } u;.};../*.
ec030 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
ec040 62 69 74 6d 61 70 20 6f 62 6a 65 63 74 20 61 62  bitmap object ab
ec050 6c 65 20 74 6f 20 68 61 6e 64 6c 65 20 62 69 74  le to handle bit
ec060 73 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20  s between 0 and 
ec070 69 53 69 7a 65 2c 0a 2a 2a 20 69 6e 63 6c 75 73  iSize,.** inclus
ec080 69 76 65 2e 20 20 52 65 74 75 72 6e 20 61 20 70  ive.  Return a p
ec090 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65  ointer to the ne
ec0a0 77 20 6f 62 6a 65 63 74 2e 20 20 52 65 74 75 72  w object.  Retur
ec0b0 6e 20 4e 55 4c 4c 20 69 66 20 0a 2a 2a 20 6d 61  n NULL if .** ma
ec0c0 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53  lloc fails..*/.S
ec0d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 42 69  QLITE_PRIVATE Bi
ec0e0 74 76 65 63 20 2a 73 71 6c 69 74 65 33 42 69 74  tvec *sqlite3Bit
ec0f0 76 65 63 43 72 65 61 74 65 28 75 33 32 20 69 53  vecCreate(u32 iS
ec100 69 7a 65 29 7b 0a 20 20 42 69 74 76 65 63 20 2a  ize){.  Bitvec *
ec110 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a  p;.  assert( siz
ec120 65 6f 66 28 2a 70 29 3d 3d 42 49 54 56 45 43 5f  eof(*p)==BITVEC_
ec130 53 5a 20 29 3b 0a 20 20 70 20 3d 20 73 71 6c 69  SZ );.  p = sqli
ec140 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73  te3MallocZero( s
ec150 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 69  izeof(*p) );.  i
ec160 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 69  f( p ){.    p->i
ec170 53 69 7a 65 20 3d 20 69 53 69 7a 65 3b 0a 20 20  Size = iSize;.  
ec180 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  }.  return p;.}.
ec190 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
ec1a0 73 65 65 20 69 66 20 74 68 65 20 69 2d 74 68 20  see if the i-th 
ec1b0 62 69 74 20 69 73 20 73 65 74 2e 20 20 52 65 74  bit is set.  Ret
ec1c0 75 72 6e 20 74 72 75 65 20 6f 72 20 66 61 6c 73  urn true or fals
ec1d0 65 2e 0a 2a 2a 20 49 66 20 70 20 69 73 20 4e 55  e..** If p is NU
ec1e0 4c 4c 20 28 69 66 20 74 68 65 20 62 69 74 6d 61  LL (if the bitma
ec1f0 70 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 63  p has not been c
ec200 72 65 61 74 65 64 29 20 6f 72 20 69 66 0a 2a 2a  reated) or if.**
ec210 20 69 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e   i is out of ran
ec220 67 65 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20  ge, then return 
ec230 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  false..*/.SQLITE
ec240 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
ec250 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 42  ite3BitvecTest(B
ec260 69 74 76 65 63 20 2a 70 2c 20 75 33 32 20 69 29  itvec *p, u32 i)
ec270 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  {.  if( p==0 ) r
ec280 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 69  eturn 0;.  if( i
ec290 3e 70 2d 3e 69 53 69 7a 65 20 7c 7c 20 69 3d 3d  >p->iSize || i==
ec2a0 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
ec2b0 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49  if( p->iSize<=BI
ec2c0 54 56 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20  TVEC_NBIT ){.   
ec2d0 20 69 2d 2d 3b 0a 20 20 20 20 72 65 74 75 72 6e   i--;.    return
ec2e0 20 28 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69   (p->u.aBitmap[i
ec2f0 2f 38 5d 20 26 20 28 31 3c 3c 28 69 26 37 29 29  /8] & (1<<(i&7))
ec300 29 21 3d 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20  )!=0;.  }.  if( 
ec310 70 2d 3e 69 44 69 76 69 73 6f 72 3e 30 20 29 7b  p->iDivisor>0 ){
ec320 0a 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20 28  .    u32 bin = (
ec330 69 2d 31 29 2f 70 2d 3e 69 44 69 76 69 73 6f 72  i-1)/p->iDivisor
ec340 3b 0a 20 20 20 20 69 20 3d 20 28 69 2d 31 29 25  ;.    i = (i-1)%
ec350 70 2d 3e 69 44 69 76 69 73 6f 72 20 2b 20 31 3b  p->iDivisor + 1;
ec360 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
ec370 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 2d  te3BitvecTest(p-
ec380 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 2c 20 69  >u.apSub[bin], i
ec390 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
ec3a0 75 33 32 20 68 20 3d 20 42 49 54 56 45 43 5f 48  u32 h = BITVEC_H
ec3b0 41 53 48 28 69 29 3b 0a 20 20 20 20 77 68 69 6c  ASH(i);.    whil
ec3c0 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d  e( p->u.aHash[h]
ec3d0 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d   ){.      if( p-
ec3e0 3e 75 2e 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29  >u.aHash[h]==i )
ec3f0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
ec400 20 68 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20   h++;.      if( 
ec410 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54 20 29  h>=BITVEC_NINT )
ec420 20 68 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20   h = 0;.    }.  
ec430 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
ec440 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  }../*.** Set the
ec450 20 69 2d 74 68 20 62 69 74 2e 20 20 52 65 74 75   i-th bit.  Retu
ec460 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20  rn 0 on success 
ec470 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  and an error cod
ec480 65 20 69 66 0a 2a 2a 20 61 6e 79 74 68 69 6e 67  e if.** anything
ec490 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a   goes wrong..**.
ec4a0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
ec4b0 6d 69 67 68 74 20 63 61 75 73 65 20 73 75 62 2d  might cause sub-
ec4c0 62 69 74 6d 61 70 73 20 74 6f 20 62 65 20 61 6c  bitmaps to be al
ec4d0 6c 6f 63 61 74 65 64 2e 20 20 46 61 69 6c 69 6e  located.  Failin
ec4e0 67 0a 2a 2a 20 74 6f 20 67 65 74 20 74 68 65 20  g.** to get the 
ec4f0 6d 65 6d 6f 72 79 20 6e 65 65 64 65 64 20 74 6f  memory needed to
ec500 20 68 6f 6c 64 20 74 68 65 20 73 75 62 2d 62 69   hold the sub-bi
ec510 74 6d 61 70 20 69 73 20 74 68 65 20 6f 6e 6c 79  tmap is the only
ec520 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 67 6f 20  .** that can go 
ec530 77 72 6f 6e 67 20 77 69 74 68 20 61 6e 20 69 6e  wrong with an in
ec540 73 65 72 74 2c 20 61 73 73 75 6d 69 6e 67 20 70  sert, assuming p
ec550 20 61 6e 64 20 69 20 61 72 65 20 76 61 6c 69 64   and i are valid
ec560 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c  ..**.** The call
ec570 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73  ing function mus
ec580 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 70 20  t ensure that p 
ec590 69 73 20 61 20 76 61 6c 69 64 20 42 69 74 76 65  is a valid Bitve
ec5a0 63 20 6f 62 6a 65 63 74 0a 2a 2a 20 61 6e 64 20  c object.** and 
ec5b0 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 20 66  that the value f
ec5c0 6f 72 20 22 69 22 20 69 73 20 77 69 74 68 69 6e  or "i" is within
ec5d0 20 72 61 6e 67 65 20 6f 66 20 74 68 65 20 42 69   range of the Bi
ec5e0 74 76 65 63 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20  tvec object..** 
ec5f0 4f 74 68 65 72 77 69 73 65 20 74 68 65 20 62 65  Otherwise the be
ec600 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69  havior is undefi
ec610 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ned..*/.SQLITE_P
ec620 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
ec630 65 33 42 69 74 76 65 63 53 65 74 28 42 69 74 76  e3BitvecSet(Bitv
ec640 65 63 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20  ec *p, u32 i){. 
ec650 20 75 33 32 20 68 3b 0a 20 20 61 73 73 65 72 74   u32 h;.  assert
ec660 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  ( p!=0 );.  asse
ec670 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 61 73 73  rt( i>0 );.  ass
ec680 65 72 74 28 20 69 3c 3d 70 2d 3e 69 53 69 7a 65  ert( i<=p->iSize
ec690 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69 53 69   );.  if( p->iSi
ec6a0 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 20  ze<=BITVEC_NBIT 
ec6b0 29 7b 0a 20 20 20 20 69 2d 2d 3b 0a 20 20 20 20  ){.    i--;.    
ec6c0 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f 38  p->u.aBitmap[i/8
ec6d0 5d 20 7c 3d 20 31 20 3c 3c 20 28 69 26 37 29 3b  ] |= 1 << (i&7);
ec6e0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
ec6f0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28  TE_OK;.  }.  if(
ec700 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 7b 0a   p->iDivisor ){.
ec710 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20 28 69      u32 bin = (i
ec720 2d 31 29 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b  -1)/p->iDivisor;
ec730 0a 20 20 20 20 69 20 3d 20 28 69 2d 31 29 25 70  .    i = (i-1)%p
ec740 2d 3e 69 44 69 76 69 73 6f 72 20 2b 20 31 3b 0a  ->iDivisor + 1;.
ec750 20 20 20 20 69 66 28 20 70 2d 3e 75 2e 61 70 53      if( p->u.apS
ec760 75 62 5b 62 69 6e 5d 3d 3d 30 20 29 7b 0a 20 20  ub[bin]==0 ){.  
ec770 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e      sqlite3Begin
ec780 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
ec790 20 20 20 20 20 20 70 2d 3e 75 2e 61 70 53 75 62        p->u.apSub
ec7a0 5b 62 69 6e 5d 20 3d 20 73 71 6c 69 74 65 33 42  [bin] = sqlite3B
ec7b0 69 74 76 65 63 43 72 65 61 74 65 28 20 70 2d 3e  itvecCreate( p->
ec7c0 69 44 69 76 69 73 6f 72 20 29 3b 0a 20 20 20 20  iDivisor );.    
ec7d0 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69    sqlite3EndBeni
ec7e0 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20  gnMalloc();.    
ec7f0 20 20 69 66 28 20 70 2d 3e 75 2e 61 70 53 75 62    if( p->u.apSub
ec800 5b 62 69 6e 5d 3d 3d 30 20 29 20 72 65 74 75 72  [bin]==0 ) retur
ec810 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
ec820 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
ec830 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65   sqlite3BitvecSe
ec840 74 28 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e  t(p->u.apSub[bin
ec850 5d 2c 20 69 29 3b 0a 20 20 7d 0a 20 20 68 20 3d  ], i);.  }.  h =
ec860 20 42 49 54 56 45 43 5f 48 41 53 48 28 69 29 3b   BITVEC_HASH(i);
ec870 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 75 2e 61  .  while( p->u.a
ec880 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 69  Hash[h] ){.    i
ec890 66 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d  f( p->u.aHash[h]
ec8a0 3d 3d 69 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ==i ) return SQL
ec8b0 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 68 2b 2b 3b  ITE_OK;.    h++;
ec8c0 0a 20 20 20 20 69 66 28 20 68 3d 3d 42 49 54 56  .    if( h==BITV
ec8d0 45 43 5f 4e 49 4e 54 20 29 20 68 20 3d 20 30 3b  EC_NINT ) h = 0;
ec8e0 0a 20 20 7d 0a 20 20 70 2d 3e 6e 53 65 74 2b 2b  .  }.  p->nSet++
ec8f0 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 53 65 74 3e  ;.  if( p->nSet>
ec900 3d 42 49 54 56 45 43 5f 4d 58 48 41 53 48 20 29  =BITVEC_MXHASH )
ec910 7b 0a 20 20 20 20 69 6e 74 20 6a 2c 20 72 63 3b  {.    int j, rc;
ec920 0a 20 20 20 20 75 33 32 20 61 69 56 61 6c 75 65  .    u32 aiValue
ec930 73 5b 42 49 54 56 45 43 5f 4e 49 4e 54 5d 3b 0a  s[BITVEC_NINT];.
ec940 20 20 20 20 6d 65 6d 63 70 79 28 61 69 56 61 6c      memcpy(aiVal
ec950 75 65 73 2c 20 70 2d 3e 75 2e 61 48 61 73 68 2c  ues, p->u.aHash,
ec960 20 73 69 7a 65 6f 66 28 61 69 56 61 6c 75 65 73   sizeof(aiValues
ec970 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70  ));.    memset(p
ec980 2d 3e 75 2e 61 70 53 75 62 2c 20 30 2c 20 73 69  ->u.apSub, 0, si
ec990 7a 65 6f 66 28 70 2d 3e 75 2e 61 70 53 75 62 5b  zeof(p->u.apSub[
ec9a0 30 5d 29 2a 42 49 54 56 45 43 5f 4e 50 54 52 29  0])*BITVEC_NPTR)
ec9b0 3b 0a 20 20 20 20 70 2d 3e 69 44 69 76 69 73 6f  ;.    p->iDiviso
ec9c0 72 20 3d 20 28 70 2d 3e 69 53 69 7a 65 20 2b 20  r = (p->iSize + 
ec9d0 42 49 54 56 45 43 5f 4e 50 54 52 20 2d 20 31 29  BITVEC_NPTR - 1)
ec9e0 2f 42 49 54 56 45 43 5f 4e 50 54 52 3b 0a 20 20  /BITVEC_NPTR;.  
ec9f0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69    rc = sqlite3Bi
eca00 74 76 65 63 53 65 74 28 70 2c 20 69 29 3b 0a 20  tvecSet(p, i);. 
eca10 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 42 49     for(j=0; j<BI
eca20 54 56 45 43 5f 4e 49 4e 54 3b 20 6a 2b 2b 29 7b  TVEC_NINT; j++){
eca30 0a 20 20 20 20 20 20 69 66 28 20 61 69 56 61 6c  .      if( aiVal
eca40 75 65 73 5b 6a 5d 20 29 20 72 63 20 7c 3d 20 73  ues[j] ) rc |= s
eca50 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28  qlite3BitvecSet(
eca60 70 2c 20 61 69 56 61 6c 75 65 73 5b 6a 5d 29 3b  p, aiValues[j]);
eca70 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
eca80 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 2d 3e 75  n rc;.  }.  p->u
eca90 2e 61 48 61 73 68 5b 68 5d 20 3d 20 69 3b 0a 20  .aHash[h] = i;. 
ecaa0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
ecab0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61  K;.}../*.** Clea
ecac0 72 20 74 68 65 20 69 2d 74 68 20 62 69 74 2e 20  r the i-th bit. 
ecad0 20 52 65 74 75 72 6e 20 30 20 6f 6e 20 73 75 63   Return 0 on suc
ecae0 63 65 73 73 20 61 6e 64 20 61 6e 20 65 72 72 6f  cess and an erro
ecaf0 72 20 63 6f 64 65 20 69 66 0a 2a 2a 20 61 6e 79  r code if.** any
ecb00 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67  thing goes wrong
ecb10 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
ecb20 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
ecb30 42 69 74 76 65 63 43 6c 65 61 72 28 42 69 74 76  BitvecClear(Bitv
ecb40 65 63 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20  ec *p, u32 i){. 
ecb50 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b   assert( p!=0 );
ecb60 0a 20 20 61 73 73 65 72 74 28 20 69 3e 30 20 29  .  assert( i>0 )
ecb70 3b 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 7a 65  ;.  if( p->iSize
ecb80 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 29 7b  <=BITVEC_NBIT ){
ecb90 0a 20 20 20 20 69 2d 2d 3b 0a 20 20 20 20 70 2d  .    i--;.    p-
ecba0 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f 38 5d 20  >u.aBitmap[i/8] 
ecbb0 26 3d 20 7e 28 31 20 3c 3c 20 28 69 26 37 29 29  &= ~(1 << (i&7))
ecbc0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d  ;.  }else if( p-
ecbd0 3e 69 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20  >iDivisor ){.   
ecbe0 20 75 33 32 20 62 69 6e 20 3d 20 28 69 2d 31 29   u32 bin = (i-1)
ecbf0 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20  /p->iDivisor;.  
ecc00 20 20 69 20 3d 20 28 69 2d 31 29 25 70 2d 3e 69    i = (i-1)%p->i
ecc10 44 69 76 69 73 6f 72 20 2b 20 31 3b 0a 20 20 20  Divisor + 1;.   
ecc20 20 69 66 28 20 70 2d 3e 75 2e 61 70 53 75 62 5b   if( p->u.apSub[
ecc30 62 69 6e 5d 20 29 7b 0a 20 20 20 20 20 20 73 71  bin] ){.      sq
ecc40 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 72  lite3BitvecClear
ecc50 28 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d  (p->u.apSub[bin]
ecc60 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  , i);.    }.  }e
ecc70 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6a 3b 0a  lse{.    int j;.
ecc80 20 20 20 20 75 33 32 20 61 69 56 61 6c 75 65 73      u32 aiValues
ecc90 5b 42 49 54 56 45 43 5f 4e 49 4e 54 5d 3b 0a 20  [BITVEC_NINT];. 
ecca0 20 20 20 6d 65 6d 63 70 79 28 61 69 56 61 6c 75     memcpy(aiValu
eccb0 65 73 2c 20 70 2d 3e 75 2e 61 48 61 73 68 2c 20  es, p->u.aHash, 
eccc0 73 69 7a 65 6f 66 28 61 69 56 61 6c 75 65 73 29  sizeof(aiValues)
eccd0 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2d  );.    memset(p-
ecce0 3e 75 2e 61 48 61 73 68 2c 20 30 2c 20 73 69 7a  >u.aHash, 0, siz
eccf0 65 6f 66 28 70 2d 3e 75 2e 61 48 61 73 68 5b 30  eof(p->u.aHash[0
ecd00 5d 29 2a 42 49 54 56 45 43 5f 4e 49 4e 54 29 3b  ])*BITVEC_NINT);
ecd10 0a 20 20 20 20 70 2d 3e 6e 53 65 74 20 3d 20 30  .    p->nSet = 0
ecd20 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a  ;.    for(j=0; j
ecd30 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20 6a 2b  <BITVEC_NINT; j+
ecd40 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 69  +){.      if( ai
ecd50 56 61 6c 75 65 73 5b 6a 5d 20 26 26 20 61 69 56  Values[j] && aiV
ecd60 61 6c 75 65 73 5b 6a 5d 21 3d 69 20 29 7b 0a 20  alues[j]!=i ){. 
ecd70 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 69         sqlite3Bi
ecd80 74 76 65 63 53 65 74 28 70 2c 20 61 69 56 61 6c  tvecSet(p, aiVal
ecd90 75 65 73 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 7d  ues[j]);.      }
ecda0 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
ecdb0 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 62 69  .** Destroy a bi
ecdc0 74 6d 61 70 20 6f 62 6a 65 63 74 2e 20 20 52 65  tmap object.  Re
ecdd0 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79  claim all memory
ecde0 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45   used..*/.SQLITE
ecdf0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
ece00 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72  lite3BitvecDestr
ece10 6f 79 28 42 69 74 76 65 63 20 2a 70 29 7b 0a 20  oy(Bitvec *p){. 
ece20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
ece30 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 69 44 69  rn;.  if( p->iDi
ece40 76 69 73 6f 72 20 29 7b 0a 20 20 20 20 69 6e 74  visor ){.    int
ece50 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   i;.    for(i=0;
ece60 20 69 3c 42 49 54 56 45 43 5f 4e 50 54 52 3b 20   i<BITVEC_NPTR; 
ece70 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  i++){.      sqli
ece80 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79  te3BitvecDestroy
ece90 28 70 2d 3e 75 2e 61 70 53 75 62 5b 69 5d 29 3b  (p->u.apSub[i]);
ecea0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c  .    }.  }.  sql
eceb0 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a  ite3_free(p);.}.
ecec0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
eced0 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53  OMIT_BUILTIN_TES
ecee0 54 0a 2f 2a 0a 2a 2a 20 4c 65 74 20 56 5b 5d 20  T./*.** Let V[] 
ecef0 62 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20 75  be an array of u
ecf00 6e 73 69 67 6e 65 64 20 63 68 61 72 61 63 74 65  nsigned characte
ecf10 72 73 20 73 75 66 66 69 63 69 65 6e 74 20 74 6f  rs sufficient to
ecf20 20 68 6f 6c 64 0a 2a 2a 20 75 70 20 74 6f 20 4e   hold.** up to N
ecf30 20 62 69 74 73 2e 20 20 4c 65 74 20 49 20 62 65   bits.  Let I be
ecf40 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65 74 77   an integer betw
ecf50 65 65 6e 20 30 20 61 6e 64 20 4e 2e 20 20 30 3c  een 0 and N.  0<
ecf60 3d 49 3c 4e 2e 0a 2a 2a 20 54 68 65 6e 20 74 68  =I<N..** Then th
ecf70 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72  e following macr
ecf80 6f 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74  os can be used t
ecf90 6f 20 73 65 74 2c 20 63 6c 65 61 72 2c 20 6f 72  o set, clear, or
ecfa0 20 74 65 73 74 0a 2a 2a 20 69 6e 64 69 76 69 64   test.** individ
ecfb0 75 61 6c 20 62 69 74 73 20 77 69 74 68 69 6e 20  ual bits within 
ecfc0 56 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 45  V..*/.#define SE
ecfd0 54 42 49 54 28 56 2c 49 29 20 20 20 20 20 20 56  TBIT(V,I)      V
ecfe0 5b 49 3e 3e 33 5d 20 7c 3d 20 28 31 3c 3c 28 49  [I>>3] |= (1<<(I
ecff0 26 37 29 29 0a 23 64 65 66 69 6e 65 20 43 4c 45  &7)).#define CLE
ed000 41 52 42 49 54 28 56 2c 49 29 20 20 20 20 56 5b  ARBIT(V,I)    V[
ed010 49 3e 3e 33 5d 20 26 3d 20 7e 28 31 3c 3c 28 49  I>>3] &= ~(1<<(I
ed020 26 37 29 29 0a 23 64 65 66 69 6e 65 20 54 45 53  &7)).#define TES
ed030 54 42 49 54 28 56 2c 49 29 20 20 20 20 20 28 56  TBIT(V,I)     (V
ed040 5b 49 3e 3e 33 5d 26 28 31 3c 3c 28 49 26 37 29  [I>>3]&(1<<(I&7)
ed050 29 29 21 3d 30 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  ))!=0../*.** Thi
ed060 73 20 72 6f 75 74 69 6e 65 20 72 75 6e 73 20 61  s routine runs a
ed070 6e 20 65 78 74 65 6e 73 69 76 65 20 74 65 73 74  n extensive test
ed080 20 6f 66 20 74 68 65 20 42 69 74 76 65 63 20 63   of the Bitvec c
ed090 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ode..**.** The i
ed0a0 6e 70 75 74 20 69 73 20 61 6e 20 61 72 72 61 79  nput is an array
ed0b0 20 6f 66 20 69 6e 74 65 67 65 72 73 20 74 68 61   of integers tha
ed0c0 74 20 61 63 74 73 20 61 73 20 61 20 70 72 6f 67  t acts as a prog
ed0d0 72 61 6d 0a 2a 2a 20 74 6f 20 74 65 73 74 20 74  ram.** to test t
ed0e0 68 65 20 42 69 74 76 65 63 2e 20 20 54 68 65 20  he Bitvec.  The 
ed0f0 69 6e 74 65 67 65 72 73 20 61 72 65 20 6f 70 63  integers are opc
ed100 6f 64 65 73 20 66 6f 6c 6c 6f 77 65 64 0a 2a 2a  odes followed.**
ed110 20 62 79 20 30 2c 20 31 2c 20 6f 72 20 33 20 6f   by 0, 1, or 3 o
ed120 70 65 72 61 6e 64 73 2c 20 64 65 70 65 6e 64 69  perands, dependi
ed130 6e 67 20 6f 6e 20 74 68 65 20 6f 70 63 6f 64 65  ng on the opcode
ed140 2e 20 20 41 6e 6f 74 68 65 72 0a 2a 2a 20 6f 70  .  Another.** op
ed150 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20 69 6d 6d  code follows imm
ed160 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 20 74  ediately after t
ed170 68 65 20 6c 61 73 74 20 6f 70 65 72 61 6e 64 2e  he last operand.
ed180 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65  .**.** There are
ed190 20 36 20 6f 70 63 6f 64 65 73 20 6e 75 6d 62 65   6 opcodes numbe
ed1a0 72 65 64 20 66 72 6f 6d 20 30 20 74 68 72 6f 75  red from 0 throu
ed1b0 67 68 20 35 2e 20 20 30 20 69 73 20 74 68 65 0a  gh 5.  0 is the.
ed1c0 2a 2a 20 22 68 61 6c 74 22 20 6f 70 63 6f 64 65  ** "halt" opcode
ed1d0 20 61 6e 64 20 63 61 75 73 65 73 20 74 68 65 20   and causes the 
ed1e0 74 65 73 74 20 74 6f 20 65 6e 64 2e 0a 2a 2a 0a  test to end..**.
ed1f0 2a 2a 20 20 20 20 30 20 20 20 20 20 20 20 20 20  **    0         
ed200 20 48 61 6c 74 20 61 6e 64 20 72 65 74 75 72 6e   Halt and return
ed210 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
ed220 72 72 6f 72 73 0a 2a 2a 20 20 20 20 31 20 4e 20  rrors.**    1 N 
ed230 53 20 58 20 20 20 20 53 65 74 20 4e 20 62 69 74  S X    Set N bit
ed240 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68  s beginning with
ed250 20 53 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74   S and increment
ed260 69 6e 67 20 62 79 20 58 0a 2a 2a 20 20 20 20 32  ing by X.**    2
ed270 20 4e 20 53 20 58 20 20 20 20 43 6c 65 61 72 20   N S X    Clear 
ed280 4e 20 62 69 74 73 20 62 65 67 69 6e 6e 69 6e 67  N bits beginning
ed290 20 77 69 74 68 20 53 20 61 6e 64 20 69 6e 63 72   with S and incr
ed2a0 65 6d 65 6e 74 69 6e 67 20 62 79 20 58 0a 2a 2a  ementing by X.**
ed2b0 20 20 20 20 33 20 4e 20 20 20 20 20 20 20 20 53      3 N        S
ed2c0 65 74 20 4e 20 72 61 6e 64 6f 6d 6c 79 20 63 68  et N randomly ch
ed2d0 6f 73 65 6e 20 62 69 74 73 0a 2a 2a 20 20 20 20  osen bits.**    
ed2e0 34 20 4e 20 20 20 20 20 20 20 20 43 6c 65 61 72  4 N        Clear
ed2f0 20 4e 20 72 61 6e 64 6f 6d 6c 79 20 63 68 6f 73   N randomly chos
ed300 65 6e 20 62 69 74 73 0a 2a 2a 20 20 20 20 35 20  en bits.**    5 
ed310 4e 20 53 20 58 20 20 20 20 53 65 74 20 4e 20 62  N S X    Set N b
ed320 69 74 73 20 66 72 6f 6d 20 53 20 69 6e 63 72 65  its from S incre
ed330 6d 65 6e 74 20 58 20 69 6e 20 61 72 72 61 79 20  ment X in array 
ed340 6f 6e 6c 79 2c 20 6e 6f 74 20 69 6e 20 62 69 74  only, not in bit
ed350 76 65 63 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70  vec.**.** The op
ed360 63 6f 64 65 73 20 31 20 74 68 72 6f 75 67 68 20  codes 1 through 
ed370 34 20 70 65 72 66 6f 72 6d 20 73 65 74 20 61 6e  4 perform set an
ed380 64 20 63 6c 65 61 72 20 6f 70 65 72 61 74 69 6f  d clear operatio
ed390 6e 73 20 61 72 65 20 70 65 72 66 6f 72 6d 65 64  ns are performed
ed3a0 0a 2a 2a 20 6f 6e 20 62 6f 74 68 20 61 20 42 69  .** on both a Bi
ed3b0 74 76 65 63 20 6f 62 6a 65 63 74 20 61 6e 64 20  tvec object and 
ed3c0 6f 6e 20 61 20 6c 69 6e 65 61 72 20 61 72 72 61  on a linear arra
ed3d0 79 20 6f 66 20 62 69 74 73 20 6f 62 74 61 69 6e  y of bits obtain
ed3e0 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a  ed from malloc..
ed3f0 2a 2a 20 4f 70 63 6f 64 65 20 35 20 77 6f 72 6b  ** Opcode 5 work
ed400 73 20 6f 6e 20 74 68 65 20 6c 69 6e 65 61 72 20  s on the linear 
ed410 61 72 72 61 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20  array only, not 
ed420 6f 6e 20 74 68 65 20 42 69 74 76 65 63 2e 0a 2a  on the Bitvec..*
ed430 2a 20 4f 70 63 6f 64 65 20 35 20 69 73 20 75 73  * Opcode 5 is us
ed440 65 64 20 74 6f 20 64 65 6c 69 62 65 72 61 74 65  ed to deliberate
ed450 6c 79 20 69 6e 64 75 63 65 20 61 20 66 61 75 6c  ly induce a faul
ed460 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a  t in order to.**
ed470 20 63 6f 6e 66 69 72 6d 20 74 68 61 74 20 65 72   confirm that er
ed480 72 6f 72 20 64 65 74 65 63 74 69 6f 6e 20 77 6f  ror detection wo
ed490 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74 68  rks..**.** At th
ed4a0 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20  e conclusion of 
ed4b0 74 68 65 20 74 65 73 74 20 74 68 65 20 6c 69 6e  the test the lin
ed4c0 65 61 72 20 61 72 72 61 79 20 69 73 20 63 6f 6d  ear array is com
ed4d0 70 61 72 65 64 0a 2a 2a 20 61 67 61 69 6e 73 74  pared.** against
ed4e0 20 74 68 65 20 42 69 74 76 65 63 20 6f 62 6a 65   the Bitvec obje
ed4f0 63 74 2e 20 20 49 66 20 74 68 65 72 65 20 61 72  ct.  If there ar
ed500 65 20 61 6e 79 20 64 69 66 66 65 72 65 6e 63 65  e any difference
ed510 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69  s,.** an error i
ed520 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20  s returned.  If 
ed530 74 68 65 79 20 61 72 65 20 74 68 65 20 73 61 6d  they are the sam
ed540 65 2c 20 7a 65 72 6f 20 69 73 20 72 65 74 75 72  e, zero is retur
ed550 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20  ned..**.** If a 
ed560 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
ed570 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
ed580 72 65 74 75 72 6e 20 2d 31 2e 0a 2a 2f 0a 53 51  return -1..*/.SQ
ed590 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
ed5a0 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 42 75   sqlite3BitvecBu
ed5b0 69 6c 74 69 6e 54 65 73 74 28 69 6e 74 20 73 7a  iltinTest(int sz
ed5c0 2c 20 69 6e 74 20 2a 61 4f 70 29 7b 0a 20 20 42  , int *aOp){.  B
ed5d0 69 74 76 65 63 20 2a 70 42 69 74 76 65 63 20 3d  itvec *pBitvec =
ed5e0 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   0;.  unsigned c
ed5f0 68 61 72 20 2a 70 56 20 3d 20 30 3b 0a 20 20 69  har *pV = 0;.  i
ed600 6e 74 20 72 63 20 3d 20 2d 31 3b 0a 20 20 69 6e  nt rc = -1;.  in
ed610 74 20 69 2c 20 6e 78 2c 20 70 63 2c 20 6f 70 3b  t i, nx, pc, op;
ed620 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
ed630 74 68 65 20 42 69 74 76 65 63 20 74 6f 20 62 65  the Bitvec to be
ed640 20 74 65 73 74 65 64 20 61 6e 64 20 61 20 6c 69   tested and a li
ed650 6e 65 61 72 20 61 72 72 61 79 20 6f 66 0a 20 20  near array of.  
ed660 2a 2a 20 62 69 74 73 20 74 6f 20 61 63 74 20 61  ** bits to act a
ed670 73 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20  s the reference 
ed680 2a 2f 0a 20 20 70 42 69 74 76 65 63 20 3d 20 73  */.  pBitvec = s
ed690 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61  qlite3BitvecCrea
ed6a0 74 65 28 20 73 7a 20 29 3b 0a 20 20 70 56 20 3d  te( sz );.  pV =
ed6b0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
ed6c0 20 28 73 7a 2b 37 29 2f 38 20 2b 20 31 20 29 3b   (sz+7)/8 + 1 );
ed6d0 0a 20 20 69 66 28 20 70 42 69 74 76 65 63 3d 3d  .  if( pBitvec==
ed6e0 30 20 7c 7c 20 70 56 3d 3d 30 20 29 20 67 6f 74  0 || pV==0 ) got
ed6f0 6f 20 62 69 74 76 65 63 5f 65 6e 64 3b 0a 20 20  o bitvec_end;.  
ed700 6d 65 6d 73 65 74 28 70 56 2c 20 30 2c 20 28 73  memset(pV, 0, (s
ed710 7a 2b 37 29 2f 38 20 2b 20 31 29 3b 0a 0a 20 20  z+7)/8 + 1);..  
ed720 2f 2a 20 52 75 6e 20 74 68 65 20 70 72 6f 67 72  /* Run the progr
ed730 61 6d 20 2a 2f 0a 20 20 70 63 20 3d 20 30 3b 0a  am */.  pc = 0;.
ed740 20 20 77 68 69 6c 65 28 20 28 6f 70 20 3d 20 61    while( (op = a
ed750 4f 70 5b 70 63 5d 29 21 3d 30 20 29 7b 0a 20 20  Op[pc])!=0 ){.  
ed760 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a    switch( op ){.
ed770 20 20 20 20 20 20 63 61 73 65 20 31 3a 0a 20 20        case 1:.  
ed780 20 20 20 20 63 61 73 65 20 32 3a 0a 20 20 20 20      case 2:.    
ed790 20 20 63 61 73 65 20 35 3a 20 7b 0a 20 20 20 20    case 5: {.    
ed7a0 20 20 20 20 6e 78 20 3d 20 34 3b 0a 20 20 20 20      nx = 4;.    
ed7b0 20 20 20 20 69 20 3d 20 61 4f 70 5b 70 63 2b 32      i = aOp[pc+2
ed7c0 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 61  ] - 1;.        a
ed7d0 4f 70 5b 70 63 2b 32 5d 20 2b 3d 20 61 4f 70 5b  Op[pc+2] += aOp[
ed7e0 70 63 2b 33 5d 3b 0a 20 20 20 20 20 20 20 20 62  pc+3];.        b
ed7f0 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
ed800 20 20 20 20 63 61 73 65 20 33 3a 0a 20 20 20 20      case 3:.    
ed810 20 20 63 61 73 65 20 34 3a 20 0a 20 20 20 20 20    case 4: .     
ed820 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
ed830 20 20 20 20 6e 78 20 3d 20 32 3b 0a 20 20 20 20      nx = 2;.    
ed840 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64      sqlite3_rand
ed850 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 69 29  omness(sizeof(i)
ed860 2c 20 26 69 29 3b 0a 20 20 20 20 20 20 20 20 62  , &i);.        b
ed870 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
ed880 20 20 7d 0a 20 20 20 20 69 66 28 20 28 2d 2d 61    }.    if( (--a
ed890 4f 70 5b 70 63 2b 31 5d 29 20 3e 20 30 20 29 20  Op[pc+1]) > 0 ) 
ed8a0 6e 78 20 3d 20 30 3b 0a 20 20 20 20 70 63 20 2b  nx = 0;.    pc +
ed8b0 3d 20 6e 78 3b 0a 20 20 20 20 69 20 3d 20 28 69  = nx;.    i = (i
ed8c0 20 26 20 30 78 37 66 66 66 66 66 66 66 29 25 73   & 0x7fffffff)%s
ed8d0 7a 3b 0a 20 20 20 20 69 66 28 20 28 6f 70 20 26  z;.    if( (op &
ed8e0 20 31 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20   1)!=0 ){.      
ed8f0 53 45 54 42 49 54 28 70 56 2c 20 28 69 2b 31 29  SETBIT(pV, (i+1)
ed900 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 70 21  );.      if( op!
ed910 3d 35 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  =5 ){.        if
ed920 28 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53  ( sqlite3BitvecS
ed930 65 74 28 70 42 69 74 76 65 63 2c 20 69 2b 31 29  et(pBitvec, i+1)
ed940 20 29 20 67 6f 74 6f 20 62 69 74 76 65 63 5f 65   ) goto bitvec_e
ed950 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  nd;.      }.    
ed960 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 43 4c 45  }else{.      CLE
ed970 41 52 42 49 54 28 70 56 2c 20 28 69 2b 31 29 29  ARBIT(pV, (i+1))
ed980 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  ;.      sqlite3B
ed990 69 74 76 65 63 43 6c 65 61 72 28 70 42 69 74 76  itvecClear(pBitv
ed9a0 65 63 2c 20 69 2b 31 29 3b 0a 20 20 20 20 7d 0a  ec, i+1);.    }.
ed9b0 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 20 74    }..  /* Test t
ed9c0 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20  o make sure the 
ed9d0 6c 69 6e 65 61 72 20 61 72 72 61 79 20 65 78 61  linear array exa
ed9e0 63 74 6c 79 20 6d 61 74 63 68 65 73 20 74 68 65  ctly matches the
ed9f0 0a 20 20 2a 2a 20 42 69 74 76 65 63 20 6f 62 6a  .  ** Bitvec obj
eda00 65 63 74 2e 20 20 53 74 61 72 74 20 77 69 74 68  ect.  Start with
eda10 20 74 68 65 20 61 73 73 75 6d 70 74 69 6f 6e 20   the assumption 
eda20 74 68 61 74 20 74 68 65 79 20 64 6f 0a 20 20 2a  that they do.  *
eda30 2a 20 6d 61 74 63 68 20 28 72 63 3d 3d 30 29 2e  * match (rc==0).
eda40 20 20 43 68 61 6e 67 65 20 72 63 20 74 6f 20 6e    Change rc to n
eda50 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 64 69 73  on-zero if a dis
eda60 63 72 65 70 61 6e 63 79 0a 20 20 2a 2a 20 69 73  crepancy.  ** is
eda70 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a 20 20 72   found..  */.  r
eda80 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65  c = sqlite3Bitve
eda90 63 54 65 73 74 28 30 2c 30 29 20 2b 20 73 71 6c  cTest(0,0) + sql
edaa0 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70  ite3BitvecTest(p
edab0 42 69 74 76 65 63 2c 20 73 7a 2b 31 29 0a 20 20  Bitvec, sz+1).  
edac0 20 20 20 20 20 20 20 20 2b 20 73 71 6c 69 74 65          + sqlite
edad0 33 42 69 74 76 65 63 54 65 73 74 28 70 42 69 74  3BitvecTest(pBit
edae0 76 65 63 2c 20 30 29 3b 0a 20 20 66 6f 72 28 69  vec, 0);.  for(i
edaf0 3d 31 3b 20 69 3c 3d 73 7a 3b 20 69 2b 2b 29 7b  =1; i<=sz; i++){
edb00 0a 20 20 20 20 69 66 28 20 20 28 54 45 53 54 42  .    if(  (TESTB
edb10 49 54 28 70 56 2c 69 29 29 21 3d 73 71 6c 69 74  IT(pV,i))!=sqlit
edb20 65 33 42 69 74 76 65 63 54 65 73 74 28 70 42 69  e3BitvecTest(pBi
edb30 74 76 65 63 2c 69 29 20 29 7b 0a 20 20 20 20 20  tvec,i) ){.     
edb40 20 72 63 20 3d 20 69 3b 0a 20 20 20 20 20 20 62   rc = i;.      b
edb50 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
edb60 0a 20 20 2f 2a 20 46 72 65 65 20 61 6c 6c 6f 63  .  /* Free alloc
edb70 61 74 65 64 20 73 74 72 75 63 74 75 72 65 20 2a  ated structure *
edb80 2f 0a 62 69 74 76 65 63 5f 65 6e 64 3a 0a 20 20  /.bitvec_end:.  
edb90 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 56 29  sqlite3_free(pV)
edba0 3b 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65  ;.  sqlite3Bitve
edbb0 63 44 65 73 74 72 6f 79 28 70 42 69 74 76 65 63  cDestroy(pBitvec
edbc0 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
edbd0 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
edbe0 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f  TE_OMIT_BUILTIN_
edbf0 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  TEST */../******
edc00 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
edc10 62 69 74 76 65 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a  bitvec.c *******
edc20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
edc30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
edc40 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
edc50 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
edc60 69 6c 65 20 70 63 61 63 68 65 2e 63 20 2a 2a 2a  ile pcache.c ***
edc70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
edc80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
edc90 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
edca0 30 30 38 20 41 75 67 75 73 74 20 30 35 0a 2a 2a  008 August 05.**
edcb0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
edcc0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
edcd0 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
edce0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
edcf0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
edd00 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
edd10 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
edd20 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
edd30 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
edd40 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
edd50 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
edd60 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
edd70 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
edd80 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
edd90 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
edda0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
eddb0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
eddc0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
eddd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
edde0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eddf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ede00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
ede10 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d  This file implem
ede20 65 6e 74 73 20 74 68 61 74 20 70 61 67 65 20 63  ents that page c
ede30 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29  ache..**.** @(#)
ede40 20 24 49 64 3a 20 70 63 61 63 68 65 2e 63 2c 76   $Id: pcache.c,v
ede50 20 31 2e 33 34 20 32 30 30 38 2f 31 30 2f 31 37   1.34 2008/10/17
ede60 20 31 38 3a 35 31 3a 35 33 20 64 61 6e 69 65 6c   18:51:53 daniel
ede70 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a  k1977 Exp $.*/..
ede80 2f 2a 0a 2a 2a 20 41 20 63 6f 6d 70 6c 65 74 65  /*.** A complete
ede90 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20 61   page cache is a
edea0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
edeb0 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  is structure..**
edec0 0a 2a 2a 20 41 20 63 61 63 68 65 20 6d 61 79 20  .** A cache may 
eded0 6f 6e 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20  only be deleted 
edee0 62 79 20 69 74 73 20 6f 77 6e 65 72 20 61 6e 64  by its owner and
edef0 20 77 68 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74   while holding t
edf00 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54  he.** SQLITE_MUT
edf10 45 58 5f 53 54 41 54 55 53 5f 4c 52 55 20 6d 75  EX_STATUS_LRU mu
edf20 74 65 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50  tex..*/.struct P
edf30 43 61 63 68 65 20 7b 0a 20 20 2f 2a 2a 2a 2a 2a  Cache {.  /*****
edf40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
edf50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
edf60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
edf70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
edf80 0a 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20  .  ** The first 
edf90 67 72 6f 75 70 20 6f 66 20 65 6c 65 6d 65 6e 74  group of element
edfa0 73 20 6d 61 79 20 62 65 20 72 65 61 64 20 6f 72  s may be read or
edfb0 20 77 72 69 74 74 65 6e 20 61 74 20 61 6e 79 20   written at any 
edfc0 74 69 6d 65 20 62 79 0a 20 20 2a 2a 20 74 68 65  time by.  ** the
edfd0 20 63 61 63 68 65 20 6f 77 6e 65 72 20 77 69 74   cache owner wit
edfe0 68 6f 75 74 20 68 6f 6c 64 69 6e 67 20 74 68 65  hout holding the
edff0 20 6d 75 74 65 78 2e 20 20 4e 6f 20 74 68 72 65   mutex.  No thre
ee000 61 64 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68  ad other than th
ee010 65 0a 20 20 2a 2a 20 63 61 63 68 65 20 6f 77 6e  e.  ** cache own
ee020 65 72 20 69 73 20 70 65 72 6d 69 74 74 65 64 20  er is permitted 
ee030 74 6f 20 61 63 63 65 73 73 20 74 68 65 73 65 20  to access these 
ee040 65 6c 65 6d 65 6e 74 73 20 61 74 20 61 6e 79 20  elements at any 
ee050 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20 20 50 67 48  time..  */.  PgH
ee060 64 72 20 2a 70 44 69 72 74 79 2c 20 2a 70 44 69  dr *pDirty, *pDi
ee070 72 74 79 54 61 69 6c 3b 20 20 20 20 20 20 20 20  rtyTail;        
ee080 20 2f 2a 20 4c 69 73 74 20 6f 66 20 64 69 72 74   /* List of dirt
ee090 79 20 70 61 67 65 73 20 69 6e 20 4c 52 55 20 6f  y pages in LRU o
ee0a0 72 64 65 72 20 2a 2f 0a 20 20 50 67 48 64 72 20  rder */.  PgHdr 
ee0b0 2a 70 53 79 6e 63 65 64 3b 20 20 20 20 20 20 20  *pSynced;       
ee0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
ee0d0 20 4c 61 73 74 20 73 79 6e 63 65 64 20 70 61 67   Last synced pag
ee0e0 65 20 69 6e 20 64 69 72 74 79 20 70 61 67 65 20  e in dirty page 
ee0f0 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  list */.  int nR
ee100 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ef;             
ee110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
ee120 20 4e 75 6d 62 65 72 20 6f 66 20 70 69 6e 6e 65   Number of pinne
ee130 64 20 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74  d pages */.  int
ee140 20 6e 50 69 6e 6e 65 64 3b 20 20 20 20 20 20 20   nPinned;       
ee150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ee160 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 69   /* Number of pi
ee170 6e 6e 65 64 20 61 6e 64 2f 6f 72 20 64 69 72 74  nned and/or dirt
ee180 79 20 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74  y pages */.  int
ee190 20 6e 4d 61 78 3b 20 20 20 20 20 20 20 20 20 20   nMax;          
ee1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ee1b0 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 64 20 63   /* Configured c
ee1c0 61 63 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20 69  ache size */.  i
ee1d0 6e 74 20 6e 4d 69 6e 3b 20 20 20 20 20 20 20 20  nt nMin;        
ee1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ee1f0 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 64     /* Configured
ee200 20 6d 69 6e 69 6d 75 6d 20 63 61 63 68 65 20 73   minimum cache s
ee210 69 7a 65 20 2a 2f 0a 20 20 2f 2a 2a 2a 2a 2a 2a  ize */.  /******
ee220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ee230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ee240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ee250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ee260 0a 20 20 2a 2a 20 54 68 65 20 6e 65 78 74 20 67  .  ** The next g
ee270 72 6f 75 70 20 6f 66 20 65 6c 65 6d 65 6e 74 73  roup of elements
ee280 20 61 72 65 20 66 69 78 65 64 20 77 68 65 6e 20   are fixed when 
ee290 74 68 65 20 63 61 63 68 65 20 69 73 20 63 72 65  the cache is cre
ee2a0 61 74 65 64 20 61 6e 64 0a 20 20 2a 2a 20 6d 61  ated and.  ** ma
ee2b0 79 20 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 64  y not be changed
ee2c0 20 61 66 74 65 72 77 61 72 64 73 2e 20 20 54 68   afterwards.  Th
ee2d0 65 73 65 20 65 6c 65 6d 65 6e 74 73 20 63 61 6e  ese elements can
ee2e0 20 72 65 61 64 20 61 74 20 61 6e 79 20 74 69 6d   read at any tim
ee2f0 65 20 62 79 0a 20 20 2a 2a 20 74 68 65 20 63 61  e by.  ** the ca
ee300 63 68 65 20 6f 77 6e 65 72 20 6f 72 20 62 79 20  che owner or by 
ee310 61 6e 79 20 74 68 72 65 61 64 20 68 6f 6c 64 69  any thread holdi
ee320 6e 67 20 74 68 65 20 74 68 65 20 6d 75 74 65 78  ng the the mutex
ee330 2e 20 20 4e 6f 6e 2d 6f 77 6e 65 72 0a 20 20 2a  .  Non-owner.  *
ee340 2a 20 74 68 72 65 61 64 73 20 6d 75 73 74 20 68  * threads must h
ee350 6f 6c 64 20 74 68 65 20 6d 75 74 65 78 20 77 68  old the mutex wh
ee360 65 6e 20 72 65 61 64 69 6e 67 20 74 68 65 73 65  en reading these
ee370 20 65 6c 65 6d 65 6e 74 73 20 74 6f 20 70 72 65   elements to pre
ee380 76 65 6e 74 0a 20 20 2a 2a 20 74 68 65 20 65 6e  vent.  ** the en
ee390 74 69 72 65 20 50 43 61 63 68 65 20 6f 62 6a 65  tire PCache obje
ee3a0 63 74 20 66 72 6f 6d 20 62 65 69 6e 67 20 64 65  ct from being de
ee3b0 6c 65 74 65 64 20 64 75 72 69 6e 67 20 74 68 65  leted during the
ee3c0 20 72 65 61 64 2e 0a 20 20 2a 2f 0a 20 20 69 6e   read..  */.  in
ee3d0 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20  t szPage;       
ee3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ee3f0 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 76 65    /* Size of eve
ee400 72 79 20 70 61 67 65 20 69 6e 20 74 68 69 73 20  ry page in this 
ee410 63 61 63 68 65 20 2a 2f 0a 20 20 69 6e 74 20 73  cache */.  int s
ee420 7a 45 78 74 72 61 3b 20 20 20 20 20 20 20 20 20  zExtra;         
ee430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
ee440 2a 20 53 69 7a 65 20 6f 66 20 65 78 74 72 61 20  * Size of extra 
ee450 73 70 61 63 65 20 66 6f 72 20 65 61 63 68 20 70  space for each p
ee460 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75  age */.  int bPu
ee470 72 67 65 61 62 6c 65 3b 20 20 20 20 20 20 20 20  rgeable;        
ee480 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
ee490 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61 72  True if pages ar
ee4a0 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f  e on backing sto
ee4b0 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53  re */.  int (*xS
ee4c0 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 67 48  tress)(void*,PgH
ee4d0 64 72 2a 29 3b 20 20 20 20 20 20 20 2f 2a 20 43  dr*);       /* C
ee4e0 61 6c 6c 20 74 6f 20 74 72 79 20 6d 61 6b 65 20  all to try make 
ee4f0 61 20 70 61 67 65 20 63 6c 65 61 6e 20 2a 2f 0a  a page clean */.
ee500 20 20 76 6f 69 64 20 2a 70 53 74 72 65 73 73 3b    void *pStress;
ee510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ee520 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e        /* Argumen
ee530 74 20 74 6f 20 78 53 74 72 65 73 73 20 2a 2f 0a  t to xStress */.
ee540 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a    /*************
ee550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ee560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ee570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ee580 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20 54  *********.  ** T
ee590 68 65 20 66 69 6e 61 6c 20 67 72 6f 75 70 20 6f  he final group o
ee5a0 66 20 65 6c 65 6d 65 6e 74 73 20 63 61 6e 20 6f  f elements can o
ee5b0 6e 6c 79 20 62 65 20 61 63 63 65 73 73 65 64 20  nly be accessed 
ee5c0 77 68 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68  while holding th
ee5d0 65 0a 20 20 2a 2a 20 6d 75 74 65 78 2e 20 20 42  e.  ** mutex.  B
ee5e0 6f 74 68 20 74 68 65 20 63 61 63 68 65 20 6f 77  oth the cache ow
ee5f0 6e 65 72 20 61 6e 64 20 61 6e 79 20 6f 74 68 65  ner and any othe
ee600 72 20 74 68 72 65 61 64 20 6d 75 73 74 20 68 6f  r thread must ho
ee610 6c 64 20 74 68 65 20 6d 75 74 65 78 0a 20 20 2a  ld the mutex.  *
ee620 2a 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69  * to read or wri
ee630 74 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20  te any of these 
ee640 65 6c 65 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20  elements..  */. 
ee650 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20   int nPage;     
ee660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ee670 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75       /* Total nu
ee680 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
ee690 20 61 70 48 61 73 68 20 2a 2f 0a 20 20 69 6e 74   apHash */.  int
ee6a0 20 6e 48 61 73 68 3b 20 20 20 20 20 20 20 20 20   nHash;         
ee6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ee6c0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c   /* Number of sl
ee6d0 6f 74 73 20 69 6e 20 61 70 48 61 73 68 5b 5d 20  ots in apHash[] 
ee6e0 2a 2f 0a 20 20 50 67 48 64 72 20 2a 2a 61 70 48  */.  PgHdr **apH
ee6f0 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  ash;            
ee700 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68           /* Hash
ee710 20 74 61 62 6c 65 20 66 6f 72 20 66 61 73 74 20   table for fast 
ee720 6c 6f 6f 6b 75 70 20 62 79 20 70 67 6e 6f 20 2a  lookup by pgno *
ee730 2f 0a 20 20 50 67 48 64 72 20 2a 70 43 6c 65 61  /.  PgHdr *pClea
ee740 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
ee750 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20          /* List 
ee760 6f 66 20 63 6c 65 61 6e 20 70 61 67 65 73 20 69  of clean pages i
ee770 6e 20 75 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  n use */.};../*.
ee780 2a 2a 20 46 72 65 65 20 73 6c 6f 74 73 20 69 6e  ** Free slots in
ee790 20 74 68 65 20 70 61 67 65 20 62 6c 6f 63 6b 20   the page block 
ee7a0 61 6c 6c 6f 63 61 74 6f 72 0a 2a 2f 0a 74 79 70  allocator.*/.typ
ee7b0 65 64 65 66 20 73 74 72 75 63 74 20 50 67 46 72  edef struct PgFr
ee7c0 65 65 73 6c 6f 74 20 50 67 46 72 65 65 73 6c 6f  eeslot PgFreeslo
ee7d0 74 3b 0a 73 74 72 75 63 74 20 50 67 46 72 65 65  t;.struct PgFree
ee7e0 73 6c 6f 74 20 7b 0a 20 20 50 67 46 72 65 65 73  slot {.  PgFrees
ee7f0 6c 6f 74 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20  lot *pNext;  /* 
ee800 4e 65 78 74 20 66 72 65 65 20 73 6c 6f 74 20 2a  Next free slot *
ee810 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62  /.};../*.** Glob
ee820 61 6c 20 64 61 74 61 20 66 6f 72 20 74 68 65 20  al data for the 
ee830 70 61 67 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 73  page cache..*/.s
ee840 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44  tatic SQLITE_WSD
ee850 20 73 74 72 75 63 74 20 50 43 61 63 68 65 47 6c   struct PCacheGl
ee860 6f 62 61 6c 20 7b 0a 20 20 69 6e 74 20 69 73 49  obal {.  int isI
ee870 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  nit;            
ee880 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
ee890 54 72 75 65 20 77 68 65 6e 20 69 6e 69 74 69 61  True when initia
ee8a0 6c 69 7a 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74  lized */.  sqlit
ee8b0 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b  e3_mutex *mutex;
ee8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
ee8d0 2a 20 73 74 61 74 69 63 20 6d 75 74 65 78 20 4d  * static mutex M
ee8e0 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 20  UTEX_STATIC_LRU 
ee8f0 2a 2f 0a 0a 20 20 69 6e 74 20 6e 4d 61 78 50 61  */..  int nMaxPa
ee900 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
ee910 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 6d            /* Sum
ee920 20 6f 66 20 6e 4d 61 78 50 61 67 65 20 66 6f 72   of nMaxPage for
ee930 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 65   purgeable cache
ee940 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 69 6e 50  s */.  int nMinP
ee950 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  age;            
ee960 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75             /* Su
ee970 6d 20 6f 66 20 6e 4d 69 6e 50 61 67 65 20 66 6f  m of nMinPage fo
ee980 72 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 68  r purgeable cach
ee990 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 72  es */.  int nCur
ee9a0 72 65 6e 74 50 61 67 65 3b 20 20 20 20 20 20 20  rentPage;       
ee9b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
ee9c0 75 6d 62 65 72 20 6f 66 20 70 75 72 67 65 61 62  umber of purgeab
ee9d0 6c 65 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 74  le pages allocat
ee9e0 65 64 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70  ed */.  PgHdr *p
ee9f0 4c 72 75 48 65 61 64 2c 20 2a 70 4c 72 75 54 61  LruHead, *pLruTa
eea00 69 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  il;         /* L
eea10 52 55 20 6c 69 73 74 20 6f 66 20 75 6e 75 73 65  RU list of unuse
eea20 64 20 63 6c 65 61 6e 20 70 67 73 20 2a 2f 0a 0a  d clean pgs */..
eea30 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 73 20 72    /* Variables r
eea40 65 6c 61 74 65 64 20 74 6f 20 53 51 4c 49 54 45  elated to SQLITE
eea50 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48  _CONFIG_PAGECACH
eea60 45 20 73 65 74 74 69 6e 67 73 2e 20 2a 2f 0a 20  E settings. */. 
eea70 20 69 6e 74 20 73 7a 53 6c 6f 74 3b 20 20 20 20   int szSlot;    
eea80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
eea90 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
eeaa0 65 61 63 68 20 66 72 65 65 20 73 6c 6f 74 20 2a  each free slot *
eeab0 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 61 72 74  /.  void *pStart
eeac0 2c 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 20  , *pEnd;        
eead0 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 75 6e 64          /* Bound
eeae0 73 20 6f 66 20 70 61 67 65 63 61 63 68 65 20 6d  s of pagecache m
eeaf0 61 6c 6c 6f 63 20 72 61 6e 67 65 20 2a 2f 0a 20  alloc range */. 
eeb00 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 46 72   PgFreeslot *pFr
eeb10 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ee;             
eeb20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 70 61 67       /* Free pag
eeb30 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a 7d 20 70 63  e blocks */.} pc
eeb40 61 63 68 65 20 3d 20 7b 30 7d 3b 0a 0a 2f 2a 0a  ache = {0};../*.
eeb50 2a 2a 20 41 6c 6c 20 63 6f 64 65 20 69 6e 20 74  ** All code in t
eeb60 68 69 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20  his file should 
eeb70 61 63 63 65 73 73 20 74 68 65 20 67 6c 6f 62 61  access the globa
eeb80 6c 20 70 63 61 63 68 65 20 73 74 72 75 63 74 75  l pcache structu
eeb90 72 65 20 76 69 61 20 74 68 65 0a 2a 2a 20 61 6c  re via the.** al
eeba0 69 61 73 20 22 70 63 61 63 68 65 5f 67 22 2e 20  ias "pcache_g". 
eebb0 54 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61  This ensures tha
eebc0 74 20 74 68 65 20 57 53 44 20 65 6d 75 6c 61 74  t the WSD emulat
eebd0 69 6f 6e 20 69 73 20 75 73 65 64 20 77 68 65 6e  ion is used when
eebe0 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f  .** compiling fo
eebf0 72 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 64  r systems that d
eec00 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 72 65  o not support re
eec10 61 6c 20 57 53 44 2e 0a 2a 2f 0a 23 64 65 66 69  al WSD..*/.#defi
eec20 6e 65 20 70 63 61 63 68 65 5f 67 20 28 47 4c 4f  ne pcache_g (GLO
eec30 42 41 4c 28 73 74 72 75 63 74 20 50 43 61 63 68  BAL(struct PCach
eec40 65 47 6c 6f 62 61 6c 2c 20 70 63 61 63 68 65 29  eGlobal, pcache)
eec50 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 67 6c 6f  )../*.** All glo
eec60 62 61 6c 20 76 61 72 69 61 62 6c 65 73 20 75 73  bal variables us
eec70 65 64 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c  ed by this modul
eec80 65 20 28 61 6c 6c 20 6f 66 20 77 68 69 63 68 20  e (all of which 
eec90 61 72 65 20 67 72 6f 75 70 65 64 20 0a 2a 2a 20  are grouped .** 
eeca0 74 6f 67 65 74 68 65 72 20 69 6e 20 67 6c 6f 62  together in glob
eecb0 61 6c 20 73 74 72 75 63 74 75 72 65 20 22 70 63  al structure "pc
eecc0 61 63 68 65 22 20 61 62 6f 76 65 29 20 61 72 65  ache" above) are
eecd0 20 70 72 6f 74 65 63 74 65 64 20 62 79 20 74 68   protected by th
eece0 65 20 73 74 61 74 69 63 20 0a 2a 2a 20 53 51 4c  e static .** SQL
eecf0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
eed00 5f 4c 52 55 20 6d 75 74 65 78 2e 20 41 20 70 6f  _LRU mutex. A po
eed10 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 6d 75  inter to this mu
eed20 74 65 78 20 69 73 20 73 74 6f 72 65 64 20 69 6e  tex is stored in
eed30 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 22 70 63  .** variable "pc
eed40 61 63 68 65 2e 6d 75 74 65 78 22 2e 0a 2a 2a 0a  ache.mutex"..**.
eed50 2a 2a 20 53 6f 6d 65 20 65 6c 65 6d 65 6e 74 73  ** Some elements
eed60 20 6f 66 20 74 68 65 20 50 43 61 63 68 65 20 61   of the PCache a
eed70 6e 64 20 50 67 48 64 72 20 73 74 72 75 63 74 75  nd PgHdr structu
eed80 72 65 73 20 61 72 65 20 70 72 6f 74 65 63 74 65  res are protecte
eed90 64 20 62 79 20 74 68 65 20 0a 2a 2a 20 53 51 4c  d by the .** SQL
eeda0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 55 53  ITE_MUTEX_STATUS
eedb0 5f 4c 52 55 20 6d 75 74 65 78 20 61 6e 64 20 6f  _LRU mutex and o
eedc0 74 68 65 72 20 61 72 65 20 6e 6f 74 2e 20 20 54  ther are not.  T
eedd0 68 65 20 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20  he protected.** 
eede0 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 67 72 6f  elements are gro
eedf0 75 70 65 64 20 61 74 20 74 68 65 20 65 6e 64 20  uped at the end 
eee00 6f 66 20 74 68 65 20 73 74 72 75 63 74 75 72 65  of the structure
eee10 73 20 61 6e 64 20 61 72 65 20 63 6c 65 61 72 6c  s and are clearl
eee20 79 0a 2a 2a 20 6d 61 72 6b 65 64 2e 0a 2a 2a 0a  y.** marked..**.
eee30 2a 2a 20 55 73 65 20 74 68 65 20 66 6f 6c 6c 6f  ** Use the follo
eee40 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d 75 73 74  wing macros must
eee50 20 73 75 72 72 6f 75 6e 64 20 61 6c 6c 20 61 63   surround all ac
eee60 63 65 73 73 20 28 72 65 61 64 20 6f 72 20 77 72  cess (read or wr
eee70 69 74 65 29 0a 2a 2a 20 6f 66 20 70 72 6f 74 65  ite).** of prote
eee80 63 74 65 64 20 65 6c 65 6d 65 6e 74 73 2e 20 20  cted elements.  
eee90 54 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74  The mutex is not
eeea0 20 72 65 63 75 72 73 69 76 65 20 61 6e 64 20 6d   recursive and m
eeeb0 61 79 20 6e 6f 74 20 62 65 0a 2a 2a 20 65 6e 74  ay not be.** ent
eeec0 65 72 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f  ered more than o
eeed0 6e 63 65 2e 20 20 54 68 65 20 70 63 61 63 68 65  nce.  The pcache
eeee0 4d 75 74 65 78 48 65 6c 64 28 29 20 6d 61 63 72  MutexHeld() macr
eeef0 6f 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65  o should only be
eef00 0a 2a 2a 20 75 73 65 64 20 77 69 74 68 69 6e 20  .** used within 
eef10 61 6e 20 61 73 73 65 72 74 28 29 20 74 6f 20 76  an assert() to v
eef20 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 6d  erify that the m
eef30 75 74 65 78 20 69 73 20 62 65 69 6e 67 20 68 65  utex is being he
eef40 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 70  ld..*/.#define p
eef50 63 61 63 68 65 45 6e 74 65 72 4d 75 74 65 78 28  cacheEnterMutex(
eef60 29 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ) sqlite3_mutex_
eef70 65 6e 74 65 72 28 70 63 61 63 68 65 5f 67 2e 6d  enter(pcache_g.m
eef80 75 74 65 78 29 0a 23 64 65 66 69 6e 65 20 70 63  utex).#define pc
eef90 61 63 68 65 45 78 69 74 4d 75 74 65 78 28 29 20  acheExitMutex() 
eefa0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
eefb0 65 61 76 65 28 70 63 61 63 68 65 5f 67 2e 6d 75  eave(pcache_g.mu
eefc0 74 65 78 29 0a 23 64 65 66 69 6e 65 20 70 63 61  tex).#define pca
eefd0 63 68 65 4d 75 74 65 78 48 65 6c 64 28 29 20 20  cheMutexHeld()  
eefe0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
eeff0 6c 64 28 70 63 61 63 68 65 5f 67 2e 6d 75 74 65  ld(pcache_g.mute
ef000 78 29 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6f  x)../*.** Some o
ef010 66 20 74 68 65 20 61 73 73 65 72 74 28 29 20 6d  f the assert() m
ef020 61 63 72 6f 73 20 69 6e 20 74 68 69 73 20 63 6f  acros in this co
ef030 64 65 20 61 72 65 20 74 6f 6f 20 65 78 70 65 6e  de are too expen
ef040 73 69 76 65 20 74 6f 20 72 75 6e 0a 2a 2a 20 65  sive to run.** e
ef050 76 65 6e 20 64 75 72 69 6e 67 20 6e 6f 72 6d 61  ven during norma
ef060 6c 20 64 65 62 75 67 67 69 6e 67 2e 20 20 55 73  l debugging.  Us
ef070 65 20 74 68 65 6d 20 6f 6e 6c 79 20 72 61 72 65  e them only rare
ef080 6c 79 20 6f 6e 20 6c 6f 6e 67 2d 72 75 6e 6e 69  ly on long-runni
ef090 6e 67 0a 2a 2a 20 74 65 73 74 73 2e 20 20 45 6e  ng.** tests.  En
ef0a0 61 62 6c 65 20 74 68 65 20 65 78 70 65 6e 73 69  able the expensi
ef0b0 76 65 20 61 73 73 65 72 74 73 20 75 73 69 6e 67  ve asserts using
ef0c0 20 74 68 65 0a 2a 2a 20 2d 44 53 51 4c 49 54 45   the.** -DSQLITE
ef0d0 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56  _ENABLE_EXPENSIV
ef0e0 45 5f 41 53 53 45 52 54 3d 31 20 63 6f 6d 70 69  E_ASSERT=1 compi
ef0f0 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 2e 0a  le-time option..
ef100 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
ef110 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56  _ENABLE_EXPENSIV
ef120 45 5f 41 53 53 45 52 54 0a 23 20 64 65 66 69 6e  E_ASSERT.# defin
ef130 65 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65  e expensive_asse
ef140 72 74 28 58 29 20 20 61 73 73 65 72 74 28 58 29  rt(X)  assert(X)
ef150 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
ef160 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74  expensive_assert
ef170 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a  (X).#endif../***
ef180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
ef1a0 4c 69 6e 6b 65 64 20 4c 69 73 74 20 4d 61 6e 61  Linked List Mana
ef1b0 67 65 6d 65 6e 74 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  gement *********
ef1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69  ***********/..#i
ef1d0 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55  f !defined(NDEBU
ef1e0 47 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51  G) && defined(SQ
ef1f0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45  LITE_ENABLE_EXPE
ef200 4e 53 49 56 45 5f 41 53 53 45 52 54 29 0a 2f 2a  NSIVE_ASSERT)./*
ef210 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
ef220 20 76 65 72 69 66 69 65 73 20 74 68 61 74 20 74   verifies that t
ef230 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74  he number of ent
ef240 72 69 65 73 20 69 6e 20 74 68 65 20 68 61 73 68  ries in the hash
ef250 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 70 43 61   table.** is pCa
ef260 63 68 65 2d 3e 6e 50 61 67 65 2e 20 20 54 68 69  che->nPage.  Thi
ef270 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  s routine is use
ef280 64 20 77 69 74 68 69 6e 20 61 73 73 65 72 74 28  d within assert(
ef290 29 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20  ) statements.** 
ef2a0 6f 6e 6c 79 20 61 6e 64 20 69 73 20 74 68 65 72  only and is ther
ef2b0 65 66 6f 72 65 20 64 69 73 61 62 6c 65 64 20 64  efore disabled d
ef2c0 75 72 69 6e 67 20 70 72 6f 64 75 63 74 69 6f 6e  uring production
ef2d0 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 73 74 61 74   builds..*/.stat
ef2e0 69 63 20 69 6e 74 20 70 63 61 63 68 65 43 68 65  ic int pcacheChe
ef2f0 63 6b 48 61 73 68 43 6f 75 6e 74 28 50 43 61 63  ckHashCount(PCac
ef300 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 69  he *pCache){.  i
ef310 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 50 61 67  nt i;.  int nPag
ef320 65 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30  e = 0;.  for(i=0
ef330 3b 20 69 3c 70 43 61 63 68 65 2d 3e 6e 48 61 73  ; i<pCache->nHas
ef340 68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 50 67 48  h; i++){.    PgH
ef350 64 72 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28 70  dr *p;.    for(p
ef360 3d 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b  =pCache->apHash[
ef370 69 5d 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78  i]; p; p=p->pNex
ef380 74 48 61 73 68 29 7b 0a 20 20 20 20 20 20 6e 50  tHash){.      nP
ef390 61 67 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  age++;.    }.  }
ef3a0 0a 20 20 61 73 73 65 72 74 28 20 6e 50 61 67 65  .  assert( nPage
ef3b0 3d 3d 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 20  ==pCache->nPage 
ef3c0 29 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d  );.  return 1;.}
ef3d0 0a 23 65 6e 64 69 66 20 2f 2a 20 21 4e 44 45 42  .#endif /* !NDEB
ef3e0 55 47 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41  UG && SQLITE_ENA
ef3f0 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53  BLE_EXPENSIVE_AS
ef400 53 45 52 54 20 2a 2f 0a 0a 0a 23 69 66 20 21 64  SERT */...#if !d
ef410 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26  efined(NDEBUG) &
ef420 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  & defined(SQLITE
ef430 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56  _ENABLE_EXPENSIV
ef440 45 5f 41 53 53 45 52 54 29 0a 2f 2a 0a 2a 2a 20  E_ASSERT)./*.** 
ef450 42 61 73 65 64 20 6f 6e 20 74 68 65 20 63 75 72  Based on the cur
ef460 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 50 43  rent value of PC
ef470 61 63 68 65 2e 6e 52 65 66 20 61 6e 64 20 74 68  ache.nRef and th
ef480 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
ef490 65 0a 2a 2a 20 50 43 61 63 68 65 2e 70 44 69 72  e.** PCache.pDir
ef4a0 74 79 20 6c 69 73 74 2c 20 72 65 74 75 72 6e 20  ty list, return 
ef4b0 74 68 65 20 65 78 70 65 63 74 65 64 20 76 61 6c  the expected val
ef4c0 75 65 20 6f 66 20 74 68 65 20 50 43 61 63 68 65  ue of the PCache
ef4d0 2e 6e 50 69 6e 6e 65 64 0a 2a 2a 20 63 6f 75 6e  .nPinned.** coun
ef4e0 74 65 72 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c  ter. This is onl
ef4f0 79 20 75 73 65 64 20 69 6e 20 64 65 62 75 67 67  y used in debugg
ef500 69 6e 67 20 62 75 69 6c 64 73 2c 20 61 73 20 66  ing builds, as f
ef510 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ollows:.**.**   
ef520 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74  expensive_assert
ef530 28 20 70 43 61 63 68 65 2d 3e 6e 50 69 6e 6e 65  ( pCache->nPinne
ef540 64 3d 3d 70 63 61 63 68 65 50 69 6e 6e 65 64 43  d==pcachePinnedC
ef550 6f 75 6e 74 28 70 43 61 63 68 65 29 20 29 3b 0a  ount(pCache) );.
ef560 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 63  */.static int pc
ef570 61 63 68 65 50 69 6e 6e 65 64 43 6f 75 6e 74 28  achePinnedCount(
ef580 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b  PCache *pCache){
ef590 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 69  .  PgHdr *p;.  i
ef5a0 6e 74 20 6e 50 69 6e 6e 65 64 20 3d 20 70 43 61  nt nPinned = pCa
ef5b0 63 68 65 2d 3e 6e 52 65 66 3b 0a 20 20 66 6f 72  che->nRef;.  for
ef5c0 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74  (p=pCache->pDirt
ef5d0 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74  y; p; p=p->pNext
ef5e0 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52  ){.    if( p->nR
ef5f0 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e  ef==0 ){.      n
ef600 50 69 6e 6e 65 64 2b 2b 3b 0a 20 20 20 20 7d 0a  Pinned++;.    }.
ef610 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 50 69    }.  return nPi
ef620 6e 6e 65 64 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  nned;.}.#endif /
ef630 2a 20 21 4e 44 45 42 55 47 20 26 26 20 53 51 4c  * !NDEBUG && SQL
ef640 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e  ITE_ENABLE_EXPEN
ef650 53 49 56 45 5f 41 53 53 45 52 54 20 2a 2f 0a 0a  SIVE_ASSERT */..
ef660 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44  .#if !defined(ND
ef670 45 42 55 47 29 20 26 26 20 64 65 66 69 6e 65 64  EBUG) && defined
ef680 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 45  (SQLITE_ENABLE_E
ef690 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54 29  XPENSIVE_ASSERT)
ef6a0 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61  ./*.** Check tha
ef6b0 74 20 74 68 65 20 70 43 61 63 68 65 2d 3e 70 53  t the pCache->pS
ef6c0 79 6e 63 65 64 20 76 61 72 69 61 62 6c 65 20 69  ynced variable i
ef6d0 73 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e  s set correctly.
ef6e0 20 49 66 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74   If it.** is not
ef6f0 2c 20 65 69 74 68 65 72 20 66 61 69 6c 20 61 6e  , either fail an
ef700 20 61 73 73 65 72 74 20 6f 72 20 72 65 74 75 72   assert or retur
ef710 6e 20 7a 65 72 6f 2e 20 4f 74 68 65 72 77 69 73  n zero. Otherwis
ef720 65 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e  e, return.** non
ef730 2d 7a 65 72 6f 2e 20 54 68 69 73 20 69 73 20 6f  -zero. This is o
ef740 6e 6c 79 20 75 73 65 64 20 69 6e 20 64 65 62 75  nly used in debu
ef750 67 67 69 6e 67 20 62 75 69 6c 64 73 2c 20 61 73  gging builds, as
ef760 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
ef770 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65    expensive_asse
ef780 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b 53  rt( pcacheCheckS
ef790 79 6e 63 65 64 28 70 43 61 63 68 65 29 20 29 3b  ynced(pCache) );
ef7a0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
ef7b0 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64  cacheCheckSynced
ef7c0 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29  (PCache *pCache)
ef7d0 7b 0a 20 20 50 67 48 64 72 20 2a 70 20 3d 20 70  {.  PgHdr *p = p
ef7e0 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69  Cache->pDirtyTai
ef7f0 6c 3b 0a 20 20 66 6f 72 28 70 3d 70 43 61 63 68  l;.  for(p=pCach
ef800 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b 20 70  e->pDirtyTail; p
ef810 21 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65  !=pCache->pSynce
ef820 64 3b 20 70 3d 70 2d 3e 70 50 72 65 76 29 7b 0a  d; p=p->pPrev){.
ef830 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e      assert( p->n
ef840 52 65 66 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73  Ref || (p->flags
ef850 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
ef860 29 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ) );.  }.  retur
ef870 6e 20 28 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 52  n (p==0 || p->nR
ef880 65 66 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73 26  ef || (p->flags&
ef890 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29  PGHDR_NEED_SYNC)
ef8a0 3d 3d 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  ==0);.}.#endif /
ef8b0 2a 20 21 4e 44 45 42 55 47 20 26 26 20 53 51 4c  * !NDEBUG && SQL
ef8c0 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e  ITE_ENABLE_EXPEN
ef8d0 53 49 56 45 5f 41 53 53 45 52 54 20 2a 2f 0a 0a  SIVE_ASSERT */..
ef8e0 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 61  ../*.** Remove a
ef8f0 20 70 61 67 65 20 66 72 6f 6d 20 69 74 73 20 68   page from its h
ef900 61 73 68 20 74 61 62 6c 65 20 28 50 43 61 63 68  ash table (PCach
ef910 65 2e 61 70 48 61 73 68 5b 5d 29 2e 0a 2a 2f 0a  e.apHash[])..*/.
ef920 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63  static void pcac
ef930 68 65 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68  heRemoveFromHash
ef940 28 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a  (PgHdr *pPage){.
ef950 20 20 61 73 73 65 72 74 28 20 70 63 61 63 68 65    assert( pcache
ef960 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b 0a 20  MutexHeld() );. 
ef970 20 69 66 28 20 70 50 61 67 65 2d 3e 70 50 72 65   if( pPage->pPre
ef980 76 48 61 73 68 20 29 7b 0a 20 20 20 20 70 50 61  vHash ){.    pPa
ef990 67 65 2d 3e 70 50 72 65 76 48 61 73 68 2d 3e 70  ge->pPrevHash->p
ef9a0 4e 65 78 74 48 61 73 68 20 3d 20 70 50 61 67 65  NextHash = pPage
ef9b0 2d 3e 70 4e 65 78 74 48 61 73 68 3b 0a 20 20 7d  ->pNextHash;.  }
ef9c0 65 6c 73 65 7b 0a 20 20 20 20 50 43 61 63 68 65  else{.    PCache
ef9d0 20 2a 70 43 61 63 68 65 20 3d 20 70 50 61 67 65   *pCache = pPage
ef9e0 2d 3e 70 43 61 63 68 65 3b 0a 20 20 20 20 75 33  ->pCache;.    u3
ef9f0 32 20 68 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e  2 h = pPage->pgn
efa00 6f 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 73  o % pCache->nHas
efa10 68 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  h;.    assert( p
efa20 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d  Cache->apHash[h]
efa30 3d 3d 70 50 61 67 65 20 29 3b 0a 20 20 20 20 70  ==pPage );.    p
efa40 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d  Cache->apHash[h]
efa50 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 48   = pPage->pNextH
efa60 61 73 68 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  ash;.  }.  if( p
efa70 50 61 67 65 2d 3e 70 4e 65 78 74 48 61 73 68 20  Page->pNextHash 
efa80 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4e  ){.    pPage->pN
efa90 65 78 74 48 61 73 68 2d 3e 70 50 72 65 76 48 61  extHash->pPrevHa
efaa0 73 68 20 3d 20 70 50 61 67 65 2d 3e 70 50 72 65  sh = pPage->pPre
efab0 76 48 61 73 68 3b 0a 20 20 7d 0a 20 20 70 50 61  vHash;.  }.  pPa
efac0 67 65 2d 3e 70 43 61 63 68 65 2d 3e 6e 50 61 67  ge->pCache->nPag
efad0 65 2d 2d 3b 0a 20 20 65 78 70 65 6e 73 69 76 65  e--;.  expensive
efae0 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 65 43  _assert( pcacheC
efaf0 68 65 63 6b 48 61 73 68 43 6f 75 6e 74 28 70 50  heckHashCount(pP
efb00 61 67 65 2d 3e 70 43 61 63 68 65 29 20 29 3b 0a  age->pCache) );.
efb10 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20  }../*.** Insert 
efb20 61 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20  a page into the 
efb30 68 61 73 68 20 74 61 62 6c 65 0a 2a 2a 0a 2a 2a  hash table.**.**
efb40 20 54 68 65 20 6d 75 74 65 78 20 6d 75 73 74 20   The mutex must 
efb50 62 65 20 68 65 6c 64 20 62 79 20 74 68 65 20 63  be held by the c
efb60 61 6c 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  aller..*/.static
efb70 20 76 6f 69 64 20 70 63 61 63 68 65 41 64 64 54   void pcacheAddT
efb80 6f 48 61 73 68 28 50 67 48 64 72 20 2a 70 50 61  oHash(PgHdr *pPa
efb90 67 65 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70  ge){.  PCache *p
efba0 43 61 63 68 65 20 3d 20 70 50 61 67 65 2d 3e 70  Cache = pPage->p
efbb0 43 61 63 68 65 3b 0a 20 20 75 33 32 20 68 20 3d  Cache;.  u32 h =
efbc0 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20 25 20 70   pPage->pgno % p
efbd0 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20  Cache->nHash;.  
efbe0 61 73 73 65 72 74 28 20 70 63 61 63 68 65 4d 75  assert( pcacheMu
efbf0 74 65 78 48 65 6c 64 28 29 20 29 3b 0a 20 20 70  texHeld() );.  p
efc00 50 61 67 65 2d 3e 70 4e 65 78 74 48 61 73 68 20  Page->pNextHash 
efc10 3d 20 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68  = pCache->apHash
efc20 5b 68 5d 3b 0a 20 20 70 50 61 67 65 2d 3e 70 50  [h];.  pPage->pP
efc30 72 65 76 48 61 73 68 20 3d 20 30 3b 0a 20 20 69  revHash = 0;.  i
efc40 66 28 20 70 43 61 63 68 65 2d 3e 61 70 48 61 73  f( pCache->apHas
efc50 68 5b 68 5d 20 29 7b 0a 20 20 20 20 70 43 61 63  h[h] ){.    pCac
efc60 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 2d 3e 70  he->apHash[h]->p
efc70 50 72 65 76 48 61 73 68 20 3d 20 70 50 61 67 65  PrevHash = pPage
efc80 3b 0a 20 20 7d 0a 20 20 70 43 61 63 68 65 2d 3e  ;.  }.  pCache->
efc90 61 70 48 61 73 68 5b 68 5d 20 3d 20 70 50 61 67  apHash[h] = pPag
efca0 65 3b 0a 20 20 70 43 61 63 68 65 2d 3e 6e 50 61  e;.  pCache->nPa
efcb0 67 65 2b 2b 3b 0a 20 20 65 78 70 65 6e 73 69 76  ge++;.  expensiv
efcc0 65 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 65  e_assert( pcache
efcd0 43 68 65 63 6b 48 61 73 68 43 6f 75 6e 74 28 70  CheckHashCount(p
efce0 43 61 63 68 65 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a  Cache) );.}../*.
efcf0 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 69 6e  ** Attempt to in
efd00 63 72 65 61 73 65 20 74 68 65 20 73 69 7a 65 20  crease the size 
efd10 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 74  the hash table t
efd20 6f 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 61 74 20  o contain.** at 
efd30 6c 65 61 73 74 20 6e 48 61 73 68 20 62 75 63 6b  least nHash buck
efd40 65 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ets..*/.static i
efd50 6e 74 20 70 63 61 63 68 65 52 65 73 69 7a 65 48  nt pcacheResizeH
efd60 61 73 68 28 50 43 61 63 68 65 20 2a 70 43 61 63  ash(PCache *pCac
efd70 68 65 2c 20 69 6e 74 20 6e 48 61 73 68 29 7b 0a  he, int nHash){.
efd80 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 50 67    PgHdr *p;.  Pg
efd90 48 64 72 20 2a 2a 70 4e 65 77 3b 0a 20 20 61 73  Hdr **pNew;.  as
efda0 73 65 72 74 28 20 70 63 61 63 68 65 4d 75 74 65  sert( pcacheMute
efdb0 78 48 65 6c 64 28 29 20 29 3b 0a 23 69 66 64 65  xHeld() );.#ifde
efdc0 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f  f SQLITE_MALLOC_
efdd0 53 4f 46 54 5f 4c 49 4d 49 54 0a 20 20 69 66 28  SOFT_LIMIT.  if(
efde0 20 6e 48 61 73 68 2a 73 69 7a 65 6f 66 28 50 67   nHash*sizeof(Pg
efdf0 48 64 72 2a 29 3e 53 51 4c 49 54 45 5f 4d 41 4c  Hdr*)>SQLITE_MAL
efe00 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 20 29  LOC_SOFT_LIMIT )
efe10 7b 0a 20 20 20 20 6e 48 61 73 68 20 3d 20 53 51  {.    nHash = SQ
efe20 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54  LITE_MALLOC_SOFT
efe30 5f 4c 49 4d 49 54 2f 73 69 7a 65 6f 66 28 50 67  _LIMIT/sizeof(Pg
efe40 48 64 72 20 2a 29 3b 0a 20 20 7d 0a 23 65 6e 64  Hdr *);.  }.#end
efe50 69 66 0a 20 20 70 63 61 63 68 65 45 78 69 74 4d  if.  pcacheExitM
efe60 75 74 65 78 28 29 3b 0a 20 20 70 4e 65 77 20 3d  utex();.  pNew =
efe70 20 28 50 67 48 64 72 20 2a 2a 29 73 71 6c 69 74   (PgHdr **)sqlit
efe80 65 33 4d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  e3Malloc(sizeof(
efe90 50 67 48 64 72 2a 29 2a 6e 48 61 73 68 29 3b 0a  PgHdr*)*nHash);.
efea0 20 20 70 63 61 63 68 65 45 6e 74 65 72 4d 75 74    pcacheEnterMut
efeb0 65 78 28 29 3b 0a 20 20 69 66 28 20 21 70 4e 65  ex();.  if( !pNe
efec0 77 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  w ){.    return 
efed0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
efee0 7d 0a 20 20 6d 65 6d 73 65 74 28 70 4e 65 77 2c  }.  memset(pNew,
efef0 20 30 2c 20 73 69 7a 65 6f 66 28 50 67 48 64 72   0, sizeof(PgHdr
eff00 20 2a 29 2a 6e 48 61 73 68 29 3b 0a 20 20 73 71   *)*nHash);.  sq
eff10 6c 69 74 65 33 5f 66 72 65 65 28 70 43 61 63 68  lite3_free(pCach
eff20 65 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20 70 43  e->apHash);.  pC
eff30 61 63 68 65 2d 3e 61 70 48 61 73 68 20 3d 20 70  ache->apHash = p
eff40 4e 65 77 3b 0a 20 20 70 43 61 63 68 65 2d 3e 6e  New;.  pCache->n
eff50 48 61 73 68 20 3d 20 6e 48 61 73 68 3b 0a 20 20  Hash = nHash;.  
eff60 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 20 3d 20  pCache->nPage = 
eff70 30 3b 0a 20 0a 20 20 66 6f 72 28 70 3d 70 43 61  0;. .  for(p=pCa
eff80 63 68 65 2d 3e 70 43 6c 65 61 6e 3b 20 70 3b 20  che->pClean; p; 
eff90 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  p=p->pNext){.   
effa0 20 70 63 61 63 68 65 41 64 64 54 6f 48 61 73 68   pcacheAddToHash
effb0 28 70 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 70  (p);.  }.  for(p
effc0 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b  =pCache->pDirty;
effd0 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b   p; p=p->pNext){
effe0 0a 20 20 20 20 70 63 61 63 68 65 41 64 64 54 6f  .    pcacheAddTo
efff0 48 61 73 68 28 70 29 3b 0a 20 20 7d 0a 20 20 72  Hash(p);.  }.  r
f0000 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
f0010 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65  .}../*.** Remove
f0020 20 61 20 70 61 67 65 20 66 72 6f 6d 20 61 20 6c   a page from a l
f0030 69 6e 6b 65 64 20 6c 69 73 74 20 74 68 61 74 20  inked list that 
f0040 69 73 20 68 65 61 64 65 64 20 62 79 20 2a 70 70  is headed by *pp
f0050 48 65 61 64 2e 0a 2a 2a 20 2a 70 70 48 65 61 64  Head..** *ppHead
f0060 20 69 73 20 65 69 74 68 65 72 20 50 43 61 63 68   is either PCach
f0070 65 2e 70 43 6c 65 61 6e 20 6f 72 20 50 43 61 63  e.pClean or PCac
f0080 68 65 2e 70 44 69 72 74 79 2e 0a 2a 2f 0a 73 74  he.pDirty..*/.st
f0090 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65  atic void pcache
f00a0 52 65 6d 6f 76 65 46 72 6f 6d 4c 69 73 74 28 50  RemoveFromList(P
f00b0 67 48 64 72 20 2a 2a 70 70 48 65 61 64 2c 20 50  gHdr **ppHead, P
f00c0 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20  gHdr *pPage){.  
f00d0 69 6e 74 20 69 73 44 69 72 74 79 4c 69 73 74 20  int isDirtyList 
f00e0 3d 20 28 70 70 48 65 61 64 3d 3d 26 70 50 61 67  = (ppHead==&pPag
f00f0 65 2d 3e 70 43 61 63 68 65 2d 3e 70 44 69 72 74  e->pCache->pDirt
f0100 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 70  y);.  assert( pp
f0110 48 65 61 64 3d 3d 26 70 50 61 67 65 2d 3e 70 43  Head==&pPage->pC
f0120 61 63 68 65 2d 3e 70 43 6c 65 61 6e 20 7c 7c 20  ache->pClean || 
f0130 70 70 48 65 61 64 3d 3d 26 70 50 61 67 65 2d 3e  ppHead==&pPage->
f0140 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 20 29  pCache->pDirty )
f0150 3b 0a 20 20 61 73 73 65 72 74 28 20 70 63 61 63  ;.  assert( pcac
f0160 68 65 4d 75 74 65 78 48 65 6c 64 28 29 20 7c 7c  heMutexHeld() ||
f0170 20 70 70 48 65 61 64 21 3d 26 70 50 61 67 65 2d   ppHead!=&pPage-
f0180 3e 70 43 61 63 68 65 2d 3e 70 43 6c 65 61 6e 20  >pCache->pClean 
f0190 29 3b 0a 0a 20 20 69 66 28 20 70 50 61 67 65 2d  );..  if( pPage-
f01a0 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 70 50  >pPrev ){.    pP
f01b0 61 67 65 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78  age->pPrev->pNex
f01c0 74 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74  t = pPage->pNext
f01d0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
f01e0 73 73 65 72 74 28 20 2a 70 70 48 65 61 64 3d 3d  ssert( *ppHead==
f01f0 70 50 61 67 65 20 29 3b 0a 20 20 20 20 2a 70 70  pPage );.    *pp
f0200 48 65 61 64 20 3d 20 70 50 61 67 65 2d 3e 70 4e  Head = pPage->pN
f0210 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  ext;.  }.  if( p
f0220 50 61 67 65 2d 3e 70 4e 65 78 74 20 29 7b 0a 20  Page->pNext ){. 
f0230 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 2d     pPage->pNext-
f0240 3e 70 50 72 65 76 20 3d 20 70 50 61 67 65 2d 3e  >pPrev = pPage->
f0250 70 50 72 65 76 3b 0a 20 20 7d 0a 0a 20 20 69 66  pPrev;.  }..  if
f0260 28 20 69 73 44 69 72 74 79 4c 69 73 74 20 29 7b  ( isDirtyList ){
f0270 0a 20 20 20 20 50 43 61 63 68 65 20 2a 70 43 61  .    PCache *pCa
f0280 63 68 65 20 3d 20 70 50 61 67 65 2d 3e 70 43 61  che = pPage->pCa
f0290 63 68 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28  che;.    assert(
f02a0 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 20 7c 7c   pPage->pNext ||
f02b0 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54   pCache->pDirtyT
f02c0 61 69 6c 3d 3d 70 50 61 67 65 20 29 3b 0a 20 20  ail==pPage );.  
f02d0 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 70 4e    if( !pPage->pN
f02e0 65 78 74 20 29 7b 0a 20 20 20 20 20 20 70 43 61  ext ){.      pCa
f02f0 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 20  che->pDirtyTail 
f0300 3d 20 70 50 61 67 65 2d 3e 70 50 72 65 76 3b 0a  = pPage->pPrev;.
f0310 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 43      }.    if( pC
f0320 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 3d 3d 70  ache->pSynced==p
f0330 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 50 67  Page ){.      Pg
f0340 48 64 72 20 2a 70 53 79 6e 63 65 64 20 3d 20 70  Hdr *pSynced = p
f0350 50 61 67 65 2d 3e 70 50 72 65 76 3b 0a 20 20 20  Page->pPrev;.   
f0360 20 20 20 77 68 69 6c 65 28 20 70 53 79 6e 63 65     while( pSynce
f0370 64 20 26 26 20 28 70 53 79 6e 63 65 64 2d 3e 66  d && (pSynced->f
f0380 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f  lags&PGHDR_NEED_
f0390 53 59 4e 43 29 20 29 7b 0a 20 20 20 20 20 20 20  SYNC) ){.       
f03a0 20 70 53 79 6e 63 65 64 20 3d 20 70 53 79 6e 63   pSynced = pSync
f03b0 65 64 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20  ed->pPrev;.     
f03c0 20 7d 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d   }.      pCache-
f03d0 3e 70 53 79 6e 63 65 64 20 3d 20 70 53 79 6e 63  >pSynced = pSync
f03e0 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  ed;.    }.  }.}.
f03f0 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 70 61 67  ./*.** Add a pag
f0400 65 20 66 72 6f 6d 20 61 20 6c 69 6e 6b 65 64 20  e from a linked 
f0410 6c 69 73 74 20 74 68 61 74 20 69 73 20 68 65 61  list that is hea
f0420 64 65 64 20 62 79 20 2a 70 70 48 65 61 64 2e 0a  ded by *ppHead..
f0430 2a 2a 20 2a 70 70 48 65 61 64 20 69 73 20 65 69  ** *ppHead is ei
f0440 74 68 65 72 20 50 43 61 63 68 65 2e 70 43 6c 65  ther PCache.pCle
f0450 61 6e 20 6f 72 20 50 43 61 63 68 65 2e 70 44 69  an or PCache.pDi
f0460 72 74 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  rty..*/.static v
f0470 6f 69 64 20 70 63 61 63 68 65 41 64 64 54 6f 4c  oid pcacheAddToL
f0480 69 73 74 28 50 67 48 64 72 20 2a 2a 70 70 48 65  ist(PgHdr **ppHe
f0490 61 64 2c 20 50 67 48 64 72 20 2a 70 50 61 67 65  ad, PgHdr *pPage
f04a0 29 7b 0a 20 20 69 6e 74 20 69 73 44 69 72 74 79  ){.  int isDirty
f04b0 4c 69 73 74 20 3d 20 28 70 70 48 65 61 64 3d 3d  List = (ppHead==
f04c0 26 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d 3e  &pPage->pCache->
f04d0 70 44 69 72 74 79 29 3b 0a 20 20 61 73 73 65 72  pDirty);.  asser
f04e0 74 28 20 70 70 48 65 61 64 3d 3d 26 70 50 61 67  t( ppHead==&pPag
f04f0 65 2d 3e 70 43 61 63 68 65 2d 3e 70 43 6c 65 61  e->pCache->pClea
f0500 6e 20 7c 7c 20 70 70 48 65 61 64 3d 3d 26 70 50  n || ppHead==&pP
f0510 61 67 65 2d 3e 70 43 61 63 68 65 2d 3e 70 44 69  age->pCache->pDi
f0520 72 74 79 20 29 3b 0a 0a 20 20 69 66 28 20 28 2a  rty );..  if( (*
f0530 70 70 48 65 61 64 29 20 29 7b 0a 20 20 20 20 28  ppHead) ){.    (
f0540 2a 70 70 48 65 61 64 29 2d 3e 70 50 72 65 76 20  *ppHead)->pPrev 
f0550 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 70  = pPage;.  }.  p
f0560 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 2a 70  Page->pNext = *p
f0570 70 48 65 61 64 3b 0a 20 20 70 50 61 67 65 2d 3e  pHead;.  pPage->
f0580 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 2a 70 70  pPrev = 0;.  *pp
f0590 48 65 61 64 20 3d 20 70 50 61 67 65 3b 0a 0a 20  Head = pPage;.. 
f05a0 20 69 66 28 20 69 73 44 69 72 74 79 4c 69 73 74   if( isDirtyList
f05b0 20 29 7b 0a 20 20 20 20 50 43 61 63 68 65 20 2a   ){.    PCache *
f05c0 70 43 61 63 68 65 20 3d 20 70 50 61 67 65 2d 3e  pCache = pPage->
f05d0 70 43 61 63 68 65 3b 0a 20 20 20 20 69 66 28 20  pCache;.    if( 
f05e0 21 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54  !pCache->pDirtyT
f05f0 61 69 6c 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ail ){.      ass
f0600 65 72 74 28 20 70 50 61 67 65 2d 3e 70 4e 65 78  ert( pPage->pNex
f0610 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 43  t==0 );.      pC
f0620 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c  ache->pDirtyTail
f0630 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 7d 0a   = pPage;.    }.
f0640 20 20 20 20 69 66 28 20 21 70 43 61 63 68 65 2d      if( !pCache-
f0650 3e 70 53 79 6e 63 65 64 20 26 26 20 30 3d 3d 28  >pSynced && 0==(
f0660 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48  pPage->flags&PGH
f0670 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 29 7b  DR_NEED_SYNC) ){
f0680 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e 70  .      pCache->p
f0690 53 79 6e 63 65 64 20 3d 20 70 50 61 67 65 3b 0a  Synced = pPage;.
f06a0 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
f06b0 2a 2a 20 52 65 6d 6f 76 65 20 61 20 70 61 67 65  ** Remove a page
f06c0 20 66 72 6f 6d 20 74 68 65 20 67 6c 6f 62 61 6c   from the global
f06d0 20 4c 52 55 20 6c 69 73 74 0a 2a 2f 0a 73 74 61   LRU list.*/.sta
f06e0 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 52  tic void pcacheR
f06f0 65 6d 6f 76 65 46 72 6f 6d 4c 72 75 4c 69 73 74  emoveFromLruList
f0700 28 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a  (PgHdr *pPage){.
f0710 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
f0720 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61  3_mutex_held(pca
f0730 63 68 65 5f 67 2e 6d 75 74 65 78 29 20 29 3b 0a  che_g.mutex) );.
f0740 20 20 61 73 73 65 72 74 28 20 28 70 50 61 67 65    assert( (pPage
f0750 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49  ->flags&PGHDR_DI
f0760 52 54 59 29 3d 3d 30 20 29 3b 0a 20 20 69 66 28  RTY)==0 );.  if(
f0770 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d 3e   pPage->pCache->
f0780 62 50 75 72 67 65 61 62 6c 65 3d 3d 30 20 29 20  bPurgeable==0 ) 
f0790 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 50  return;.  if( pP
f07a0 61 67 65 2d 3e 70 4e 65 78 74 4c 72 75 20 29 7b  age->pNextLru ){
f07b0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 63 61  .    assert( pca
f07c0 63 68 65 5f 67 2e 70 4c 72 75 54 61 69 6c 21 3d  che_g.pLruTail!=
f07d0 70 50 61 67 65 20 29 3b 0a 20 20 20 20 70 50 61  pPage );.    pPa
f07e0 67 65 2d 3e 70 4e 65 78 74 4c 72 75 2d 3e 70 50  ge->pNextLru->pP
f07f0 72 65 76 4c 72 75 20 3d 20 70 50 61 67 65 2d 3e  revLru = pPage->
f0800 70 50 72 65 76 4c 72 75 3b 0a 20 20 7d 65 6c 73  pPrevLru;.  }els
f0810 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
f0820 63 61 63 68 65 5f 67 2e 70 4c 72 75 54 61 69 6c  cache_g.pLruTail
f0830 3d 3d 70 50 61 67 65 20 29 3b 0a 20 20 20 20 70  ==pPage );.    p
f0840 63 61 63 68 65 5f 67 2e 70 4c 72 75 54 61 69 6c  cache_g.pLruTail
f0850 20 3d 20 70 50 61 67 65 2d 3e 70 50 72 65 76 4c   = pPage->pPrevL
f0860 72 75 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50  ru;.  }.  if( pP
f0870 61 67 65 2d 3e 70 50 72 65 76 4c 72 75 20 29 7b  age->pPrevLru ){
f0880 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 63 61  .    assert( pca
f0890 63 68 65 5f 67 2e 70 4c 72 75 48 65 61 64 21 3d  che_g.pLruHead!=
f08a0 70 50 61 67 65 20 29 3b 0a 20 20 20 20 70 50 61  pPage );.    pPa
f08b0 67 65 2d 3e 70 50 72 65 76 4c 72 75 2d 3e 70 4e  ge->pPrevLru->pN
f08c0 65 78 74 4c 72 75 20 3d 20 70 50 61 67 65 2d 3e  extLru = pPage->
f08d0 70 4e 65 78 74 4c 72 75 3b 0a 20 20 7d 65 6c 73  pNextLru;.  }els
f08e0 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
f08f0 63 61 63 68 65 5f 67 2e 70 4c 72 75 48 65 61 64  cache_g.pLruHead
f0900 3d 3d 70 50 61 67 65 20 29 3b 0a 20 20 20 20 70  ==pPage );.    p
f0910 63 61 63 68 65 5f 67 2e 70 4c 72 75 48 65 61 64  cache_g.pLruHead
f0920 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 4c   = pPage->pNextL
f0930 72 75 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ru;.  }.}../*.**
f0940 20 41 64 64 20 61 20 70 61 67 65 20 74 6f 20 74   Add a page to t
f0950 68 65 20 67 6c 6f 62 61 6c 20 4c 52 55 20 6c 69  he global LRU li
f0960 73 74 2e 20 20 54 68 65 20 70 61 67 65 20 69 73  st.  The page is
f0970 20 6e 6f 72 6d 61 6c 6c 79 20 61 64 64 65 64 0a   normally added.
f0980 2a 2a 20 74 6f 20 74 68 65 20 66 72 6f 6e 74 20  ** to the front 
f0990 6f 66 20 74 68 65 20 6c 69 73 74 20 73 6f 20 74  of the list so t
f09a0 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 74  hat it will be t
f09b0 68 65 20 6c 61 73 74 20 70 61 67 65 20 72 65 63  he last page rec
f09c0 79 63 6c 65 64 2e 0a 2a 2a 20 48 6f 77 65 76 65  ycled..** Howeve
f09d0 72 2c 20 69 66 20 74 68 65 20 50 47 48 44 52 5f  r, if the PGHDR_
f09e0 52 45 55 53 45 5f 55 4e 4c 49 4b 45 4c 59 20 62  REUSE_UNLIKELY b
f09f0 69 74 20 69 73 20 73 65 74 2c 20 74 68 65 20 70  it is set, the p
f0a00 61 67 65 20 69 73 20 61 64 64 65 64 0a 2a 2a 20  age is added.** 
f0a10 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  to the end of th
f0a20 65 20 4c 52 55 20 6c 69 73 74 20 73 6f 20 74 68  e LRU list so th
f0a30 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 74 68  at it will be th
f0a40 65 20 6e 65 78 74 20 74 6f 20 62 65 20 72 65 63  e next to be rec
f0a50 79 63 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ycled..*/.static
f0a60 20 76 6f 69 64 20 70 63 61 63 68 65 41 64 64 54   void pcacheAddT
f0a70 6f 4c 72 75 4c 69 73 74 28 50 67 48 64 72 20 2a  oLruList(PgHdr *
f0a80 70 50 61 67 65 29 7b 0a 20 20 61 73 73 65 72 74  pPage){.  assert
f0a90 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
f0aa0 68 65 6c 64 28 70 63 61 63 68 65 5f 67 2e 6d 75  held(pcache_g.mu
f0ab0 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
f0ac0 28 20 28 70 50 61 67 65 2d 3e 66 6c 61 67 73 26  ( (pPage->flags&
f0ad0 50 47 48 44 52 5f 44 49 52 54 59 29 3d 3d 30 20  PGHDR_DIRTY)==0 
f0ae0 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e  );.  if( pPage->
f0af0 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62  pCache->bPurgeab
f0b00 6c 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  le==0 ) return;.
f0b10 20 20 69 66 28 20 70 63 61 63 68 65 5f 67 2e 70    if( pcache_g.p
f0b20 4c 72 75 54 61 69 6c 20 26 26 20 28 70 50 61 67  LruTail && (pPag
f0b30 65 2d 3e 66 6c 61 67 73 20 26 20 50 47 48 44 52  e->flags & PGHDR
f0b40 5f 52 45 55 53 45 5f 55 4e 4c 49 4b 45 4c 59 29  _REUSE_UNLIKELY)
f0b50 21 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  !=0 ){.    /* If
f0b60 20 72 65 75 73 65 20 69 73 20 75 6e 6c 69 6b 65   reuse is unlike
f0b70 6c 79 2e 20 20 50 75 74 20 74 68 65 20 70 61 67  ly.  Put the pag
f0b80 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20  e at the end of 
f0b90 74 68 65 20 4c 52 55 20 6c 69 73 74 0a 20 20 20  the LRU list.   
f0ba0 20 2a 2a 20 77 68 65 72 65 20 69 74 20 77 69 6c   ** where it wil
f0bb0 6c 20 62 65 20 72 65 63 79 63 6c 65 64 20 73 6f  l be recycled so
f0bc0 6f 6e 65 72 20 72 61 74 68 65 72 20 74 68 61 6e  oner rather than
f0bd0 20 6c 61 74 65 72 2e 20 0a 20 20 20 20 2a 2f 0a   later. .    */.
f0be0 20 20 20 20 61 73 73 65 72 74 28 20 70 63 61 63      assert( pcac
f0bf0 68 65 5f 67 2e 70 4c 72 75 48 65 61 64 20 29 3b  he_g.pLruHead );
f0c00 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78  .    pPage->pNex
f0c10 74 4c 72 75 20 3d 20 30 3b 0a 20 20 20 20 70 50  tLru = 0;.    pP
f0c20 61 67 65 2d 3e 70 50 72 65 76 4c 72 75 20 3d 20  age->pPrevLru = 
f0c30 70 63 61 63 68 65 5f 67 2e 70 4c 72 75 54 61 69  pcache_g.pLruTai
f0c40 6c 3b 0a 20 20 20 20 70 63 61 63 68 65 5f 67 2e  l;.    pcache_g.
f0c50 70 4c 72 75 54 61 69 6c 2d 3e 70 4e 65 78 74 4c  pLruTail->pNextL
f0c60 72 75 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20  ru = pPage;.    
f0c70 70 63 61 63 68 65 5f 67 2e 70 4c 72 75 54 61 69  pcache_g.pLruTai
f0c80 6c 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 70  l = pPage;.    p
f0c90 50 61 67 65 2d 3e 66 6c 61 67 73 20 26 3d 20 7e  Page->flags &= ~
f0ca0 50 47 48 44 52 5f 52 45 55 53 45 5f 55 4e 4c 49  PGHDR_REUSE_UNLI
f0cb0 4b 45 4c 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  KELY;.  }else{. 
f0cc0 20 20 20 2f 2a 20 49 66 20 72 65 75 73 65 20 69     /* If reuse i
f0cd0 73 20 70 6f 73 73 69 62 6c 65 2e 20 74 68 65 20  s possible. the 
f0ce0 70 61 67 65 20 67 6f 65 73 20 61 74 20 74 68 65  page goes at the
f0cf0 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
f0d00 65 20 4c 52 55 0a 20 20 20 20 2a 2a 20 6c 69 73  e LRU.    ** lis
f0d10 74 20 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c  t so that it wil
f0d20 6c 20 62 65 20 74 68 65 20 6c 61 73 74 20 74 6f  l be the last to
f0d30 20 62 65 20 72 65 63 79 63 6c 65 64 2e 0a 20 20   be recycled..  
f0d40 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 63 61    */.    if( pca
f0d50 63 68 65 5f 67 2e 70 4c 72 75 48 65 61 64 20 29  che_g.pLruHead )
f0d60 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 5f 67  {.      pcache_g
f0d70 2e 70 4c 72 75 48 65 61 64 2d 3e 70 50 72 65 76  .pLruHead->pPrev
f0d80 4c 72 75 20 3d 20 70 50 61 67 65 3b 0a 20 20 20  Lru = pPage;.   
f0d90 20 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4e   }.    pPage->pN
f0da0 65 78 74 4c 72 75 20 3d 20 70 63 61 63 68 65 5f  extLru = pcache_
f0db0 67 2e 70 4c 72 75 48 65 61 64 3b 0a 20 20 20 20  g.pLruHead;.    
f0dc0 70 63 61 63 68 65 5f 67 2e 70 4c 72 75 48 65 61  pcache_g.pLruHea
f0dd0 64 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 70  d = pPage;.    p
f0de0 50 61 67 65 2d 3e 70 50 72 65 76 4c 72 75 20 3d  Page->pPrevLru =
f0df0 20 30 3b 0a 20 20 20 20 69 66 28 20 70 63 61 63   0;.    if( pcac
f0e00 68 65 5f 67 2e 70 4c 72 75 54 61 69 6c 3d 3d 30  he_g.pLruTail==0
f0e10 20 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65   ){.      pcache
f0e20 5f 67 2e 70 4c 72 75 54 61 69 6c 20 3d 20 70 50  _g.pLruTail = pP
f0e30 61 67 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  age;.    }.  }.}
f0e40 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
f0e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f0e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f0e70 2a 2a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61  ** Memory Alloca
f0e80 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tion ***********
f0e90 0a 2a 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a  .**.** Initializ
f0ea0 65 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  e the page cache
f0eb0 20 6d 65 6d 6f 72 79 20 70 6f 6f 6c 2e 0a 2a 2a   memory pool..**
f0ec0 0a 2a 2a 20 54 68 69 73 20 6d 75 73 74 20 62 65  .** This must be
f0ed0 20 63 61 6c 6c 65 64 20 61 74 20 73 74 61 72 74   called at start
f0ee0 2d 74 69 6d 65 20 77 68 65 6e 20 6e 6f 20 70 61  -time when no pa
f0ef0 67 65 20 63 61 63 68 65 20 6c 69 6e 65 73 20 61  ge cache lines a
f0f00 72 65 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75  re.** checked ou
f0f10 74 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  t. This function
f0f20 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61   is not threadsa
f0f30 66 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  fe..*/.SQLITE_PR
f0f40 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
f0f50 65 33 50 43 61 63 68 65 42 75 66 66 65 72 53 65  e3PCacheBufferSe
f0f60 74 75 70 28 76 6f 69 64 20 2a 70 42 75 66 2c 20  tup(void *pBuf, 
f0f70 69 6e 74 20 73 7a 2c 20 69 6e 74 20 6e 29 7b 0a  int sz, int n){.
f0f80 20 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 3b    PgFreeslot *p;
f0f90 0a 20 20 73 7a 20 26 3d 20 7e 37 3b 0a 20 20 70  .  sz &= ~7;.  p
f0fa0 63 61 63 68 65 5f 67 2e 73 7a 53 6c 6f 74 20 3d  cache_g.szSlot =
f0fb0 20 73 7a 3b 0a 20 20 70 63 61 63 68 65 5f 67 2e   sz;.  pcache_g.
f0fc0 70 53 74 61 72 74 20 3d 20 70 42 75 66 3b 0a 20  pStart = pBuf;. 
f0fd0 20 70 63 61 63 68 65 5f 67 2e 70 46 72 65 65 20   pcache_g.pFree 
f0fe0 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 6e 2d  = 0;.  while( n-
f0ff0 2d 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 50 67  - ){.    p = (Pg
f1000 46 72 65 65 73 6c 6f 74 2a 29 70 42 75 66 3b 0a  Freeslot*)pBuf;.
f1010 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70      p->pNext = p
f1020 63 61 63 68 65 5f 67 2e 70 46 72 65 65 3b 0a 20  cache_g.pFree;. 
f1030 20 20 20 70 63 61 63 68 65 5f 67 2e 70 46 72 65     pcache_g.pFre
f1040 65 20 3d 20 70 3b 0a 20 20 20 20 70 42 75 66 20  e = p;.    pBuf 
f1050 3d 20 28 76 6f 69 64 2a 29 26 28 28 63 68 61 72  = (void*)&((char
f1060 2a 29 70 42 75 66 29 5b 73 7a 5d 3b 0a 20 20 7d  *)pBuf)[sz];.  }
f1070 0a 20 20 70 63 61 63 68 65 5f 67 2e 70 45 6e 64  .  pcache_g.pEnd
f1080 20 3d 20 70 42 75 66 3b 0a 7d 0a 0a 2f 2a 0a 2a   = pBuf;.}../*.*
f1090 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 70 61 67  * Allocate a pag
f10a0 65 20 63 61 63 68 65 20 6c 69 6e 65 2e 20 20 4c  e cache line.  L
f10b0 6f 6f 6b 20 69 6e 20 74 68 65 20 70 61 67 65 20  ook in the page 
f10c0 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 70 6f 6f  cache memory poo
f10d0 6c 20 66 69 72 73 74 0a 2a 2a 20 61 6e 64 20 75  l first.** and u
f10e0 73 65 20 61 6e 20 65 6c 65 6d 65 6e 74 20 66 72  se an element fr
f10f0 6f 6d 20 69 74 20 66 69 72 73 74 20 69 66 20 61  om it first if a
f1100 76 61 69 6c 61 62 6c 65 2e 20 20 49 66 20 6e 6f  vailable.  If no
f1110 74 68 69 6e 67 20 69 73 20 61 76 61 69 6c 61 62  thing is availab
f1120 6c 65 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 67  le.** in the pag
f1130 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 70  e cache memory p
f1140 6f 6f 6c 2c 20 67 6f 20 74 6f 20 74 68 65 20 67  ool, go to the g
f1150 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 20 6d  eneral purpose m
f1160 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e  emory allocator.
f1170 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
f1180 2a 70 63 61 63 68 65 4d 61 6c 6c 6f 63 28 69 6e  *pcacheMalloc(in
f1190 74 20 73 7a 2c 20 50 43 61 63 68 65 20 2a 70 43  t sz, PCache *pC
f11a0 61 63 68 65 29 7b 0a 20 20 61 73 73 65 72 74 28  ache){.  assert(
f11b0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
f11c0 65 6c 64 28 70 63 61 63 68 65 5f 67 2e 6d 75 74  eld(pcache_g.mut
f11d0 65 78 29 20 29 3b 0a 20 20 69 66 28 20 73 7a 3c  ex) );.  if( sz<
f11e0 3d 70 63 61 63 68 65 5f 67 2e 73 7a 53 6c 6f 74  =pcache_g.szSlot
f11f0 20 26 26 20 70 63 61 63 68 65 5f 67 2e 70 46 72   && pcache_g.pFr
f1200 65 65 20 29 7b 0a 20 20 20 20 50 67 46 72 65 65  ee ){.    PgFree
f1210 73 6c 6f 74 20 2a 70 20 3d 20 70 63 61 63 68 65  slot *p = pcache
f1220 5f 67 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 63  _g.pFree;.    pc
f1230 61 63 68 65 5f 67 2e 70 46 72 65 65 20 3d 20 70  ache_g.pFree = p
f1240 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c  ->pNext;.    sql
f1250 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51  ite3StatusSet(SQ
f1260 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45  LITE_STATUS_PAGE
f1270 43 41 43 48 45 5f 53 49 5a 45 2c 20 73 7a 29 3b  CACHE_SIZE, sz);
f1280 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74  .    sqlite3Stat
f1290 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41  usAdd(SQLITE_STA
f12a0 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53  TUS_PAGECACHE_US
f12b0 45 44 2c 20 31 29 3b 0a 20 20 20 20 72 65 74 75  ED, 1);.    retu
f12c0 72 6e 20 28 76 6f 69 64 2a 29 70 3b 0a 20 20 7d  rn (void*)p;.  }
f12d0 65 6c 73 65 7b 0a 20 20 20 20 76 6f 69 64 20 2a  else{.    void *
f12e0 70 3b 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63  p;..    /* Alloc
f12f0 61 74 65 20 61 20 6e 65 77 20 62 75 66 66 65 72  ate a new buffer
f1300 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4d 61   using sqlite3Ma
f1310 6c 6c 6f 63 2e 20 42 65 66 6f 72 65 20 64 6f 69  lloc. Before doi
f1320 6e 67 20 73 6f 2c 20 65 78 69 74 20 74 68 65 0a  ng so, exit the.
f1330 20 20 20 20 2a 2a 20 67 6c 6f 62 61 6c 20 70 63      ** global pc
f1340 61 63 68 65 20 6d 75 74 65 78 20 61 6e 64 20 75  ache mutex and u
f1350 6e 6c 6f 63 6b 20 74 68 65 20 70 61 67 65 72 2d  nlock the pager-
f1360 63 61 63 68 65 20 6f 62 6a 65 63 74 20 70 43 61  cache object pCa
f1370 63 68 65 2e 20 54 68 69 73 20 69 73 20 0a 20 20  che. This is .  
f1380 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 69 66 20    ** so that if 
f1390 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 61  the attempt to a
f13a0 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 62 75  llocate a new bu
f13b0 66 66 65 72 20 63 61 75 73 65 73 20 74 68 65 20  ffer causes the 
f13c0 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 6f 6e 66  the .    ** conf
f13d0 69 67 75 72 65 64 20 73 6f 66 74 2d 68 65 61 70  igured soft-heap
f13e0 2d 6c 69 6d 69 74 20 74 6f 20 62 65 20 62 72 65  -limit to be bre
f13f0 61 63 68 65 64 2c 20 69 74 20 77 69 6c 6c 20 62  ached, it will b
f1400 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 0a 20 20  e possible to.  
f1410 20 20 2a 2a 20 72 65 63 6c 61 69 6d 20 6d 65 6d    ** reclaim mem
f1420 6f 72 79 20 66 72 6f 6d 20 74 68 69 73 20 70 61  ory from this pa
f1430 67 65 72 2d 63 61 63 68 65 2e 0a 20 20 20 20 2a  ger-cache..    *
f1440 2f 0a 20 20 20 20 70 63 61 63 68 65 45 78 69 74  /.    pcacheExit
f1450 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 70 20 3d  Mutex();.    p =
f1460 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 73   sqlite3Malloc(s
f1470 7a 29 3b 0a 20 20 20 20 70 63 61 63 68 65 45 6e  z);.    pcacheEn
f1480 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20 20  terMutex();..   
f1490 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20   if( p ){.      
f14a0 73 7a 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  sz = sqlite3Mall
f14b0 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 20  ocSize(p);.     
f14c0 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64   sqlite3StatusAd
f14d0 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  d(SQLITE_STATUS_
f14e0 50 41 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c  PAGECACHE_OVERFL
f14f0 4f 57 2c 20 73 7a 29 3b 0a 20 20 20 20 7d 0a 20  OW, sz);.    }. 
f1500 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 7d     return p;.  }
f1510 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
f1520 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50  E void *sqlite3P
f1530 61 67 65 4d 61 6c 6c 6f 63 28 69 6e 74 20 73 7a  ageMalloc(int sz
f1540 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20  ){.  void *p;.  
f1550 70 63 61 63 68 65 45 6e 74 65 72 4d 75 74 65 78  pcacheEnterMutex
f1560 28 29 3b 0a 20 20 70 20 3d 20 70 63 61 63 68 65  ();.  p = pcache
f1570 4d 61 6c 6c 6f 63 28 73 7a 2c 20 30 29 3b 0a 20  Malloc(sz, 0);. 
f1580 20 70 63 61 63 68 65 45 78 69 74 4d 75 74 65 78   pcacheExitMutex
f1590 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a  ();.  return p;.
f15a0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65  }../*.** Release
f15b0 20 61 20 70 61 67 65 72 20 6d 65 6d 6f 72 79 20   a pager memory 
f15c0 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74  allocation.*/.st
f15d0 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65  atic void pcache
f15e0 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20  Free(void *p){. 
f15f0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
f1600 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63  _mutex_held(pcac
f1610 68 65 5f 67 2e 6d 75 74 65 78 29 20 29 3b 0a 20  he_g.mutex) );. 
f1620 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
f1630 72 6e 3b 0a 20 20 69 66 28 20 70 3e 3d 70 63 61  rn;.  if( p>=pca
f1640 63 68 65 5f 67 2e 70 53 74 61 72 74 20 26 26 20  che_g.pStart && 
f1650 70 3c 70 63 61 63 68 65 5f 67 2e 70 45 6e 64 20  p<pcache_g.pEnd 
f1660 29 7b 0a 20 20 20 20 50 67 46 72 65 65 73 6c 6f  ){.    PgFreeslo
f1670 74 20 2a 70 53 6c 6f 74 3b 0a 20 20 20 20 73 71  t *pSlot;.    sq
f1680 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53  lite3StatusAdd(S
f1690 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47  QLITE_STATUS_PAG
f16a0 45 43 41 43 48 45 5f 55 53 45 44 2c 20 2d 31 29  ECACHE_USED, -1)
f16b0 3b 0a 20 20 20 20 70 53 6c 6f 74 20 3d 20 28 50  ;.    pSlot = (P
f16c0 67 46 72 65 65 73 6c 6f 74 2a 29 70 3b 0a 20 20  gFreeslot*)p;.  
f16d0 20 20 70 53 6c 6f 74 2d 3e 70 4e 65 78 74 20 3d    pSlot->pNext =
f16e0 20 70 63 61 63 68 65 5f 67 2e 70 46 72 65 65 3b   pcache_g.pFree;
f16f0 0a 20 20 20 20 70 63 61 63 68 65 5f 67 2e 70 46  .    pcache_g.pF
f1700 72 65 65 20 3d 20 70 53 6c 6f 74 3b 0a 20 20 7d  ree = pSlot;.  }
f1710 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 53  else{.    int iS
f1720 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  ize = sqlite3Mal
f1730 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20  locSize(p);.    
f1740 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64  sqlite3StatusAdd
f1750 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50  (SQLITE_STATUS_P
f1760 41 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f  AGECACHE_OVERFLO
f1770 57 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20  W, -iSize);.    
f1780 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
f1790 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  .  }.}.SQLITE_PR
f17a0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
f17b0 65 33 50 61 67 65 46 72 65 65 28 76 6f 69 64 20  e3PageFree(void 
f17c0 2a 70 29 7b 0a 20 20 70 63 61 63 68 65 45 6e 74  *p){.  pcacheEnt
f17d0 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61  erMutex();.  pca
f17e0 63 68 65 46 72 65 65 28 70 29 3b 0a 20 20 70 63  cheFree(p);.  pc
f17f0 61 63 68 65 45 78 69 74 4d 75 74 65 78 28 29 3b  acheExitMutex();
f1800 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  .}../*.** Alloca
f1810 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e 0a 2a  te a new page..*
f1820 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72 20 2a  /.static PgHdr *
f1830 70 63 61 63 68 65 50 61 67 65 41 6c 6c 6f 63 28  pcachePageAlloc(
f1840 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b  PCache *pCache){
f1850 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 69  .  PgHdr *p;.  i
f1860 6e 74 20 73 7a 20 3d 20 73 69 7a 65 6f 66 28 2a  nt sz = sizeof(*
f1870 70 29 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a 50  p) + pCache->szP
f1880 61 67 65 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a  age + pCache->sz
f1890 45 78 74 72 61 3b 0a 20 20 61 73 73 65 72 74 28  Extra;.  assert(
f18a0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
f18b0 65 6c 64 28 70 63 61 63 68 65 5f 67 2e 6d 75 74  eld(pcache_g.mut
f18c0 65 78 29 20 29 3b 0a 20 20 70 20 3d 20 70 63 61  ex) );.  p = pca
f18d0 63 68 65 4d 61 6c 6c 6f 63 28 73 7a 2c 20 70 43  cheMalloc(sz, pC
f18e0 61 63 68 65 29 3b 0a 20 20 69 66 28 20 70 3d 3d  ache);.  if( p==
f18f0 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
f1900 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a  memset(p, 0, siz
f1910 65 6f 66 28 50 67 48 64 72 29 29 3b 0a 20 20 70  eof(PgHdr));.  p
f1920 2d 3e 70 44 61 74 61 20 3d 20 28 76 6f 69 64 2a  ->pData = (void*
f1930 29 26 70 5b 31 5d 3b 0a 20 20 70 2d 3e 70 45 78  )&p[1];.  p->pEx
f1940 74 72 61 20 3d 20 28 76 6f 69 64 2a 29 26 28 28  tra = (void*)&((
f1950 63 68 61 72 2a 29 70 2d 3e 70 44 61 74 61 29 5b  char*)p->pData)[
f1960 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 5d 3b  pCache->szPage];
f1970 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62  .  if( pCache->b
f1980 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20  Purgeable ){.   
f1990 20 70 63 61 63 68 65 5f 67 2e 6e 43 75 72 72 65   pcache_g.nCurre
f19a0 6e 74 50 61 67 65 2b 2b 3b 0a 20 20 7d 0a 20 20  ntPage++;.  }.  
f19b0 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
f19c0 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 20  ** Deallocate a 
f19d0 70 61 67 65 0a 2a 2f 0a 73 74 61 74 69 63 20 76  page.*/.static v
f19e0 6f 69 64 20 70 63 61 63 68 65 50 61 67 65 46 72  oid pcachePageFr
f19f0 65 65 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20  ee(PgHdr *p){.  
f1a00 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
f1a10 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68  mutex_held(pcach
f1a20 65 5f 67 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20  e_g.mutex) );.  
f1a30 69 66 28 20 70 2d 3e 70 43 61 63 68 65 2d 3e 62  if( p->pCache->b
f1a40 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20  Purgeable ){.   
f1a50 20 70 63 61 63 68 65 5f 67 2e 6e 43 75 72 72 65   pcache_g.nCurre
f1a60 6e 74 50 61 67 65 2d 2d 3b 0a 20 20 7d 0a 20 20  ntPage--;.  }.  
f1a70 70 63 61 63 68 65 46 72 65 65 28 70 29 3b 0a 7d  pcacheFree(p);.}
f1a80 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
f1a90 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41  ENABLE_MEMORY_MA
f1aa0 4e 41 47 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 52  NAGEMENT./*.** R
f1ab0 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
f1ac0 20 6f 66 20 62 79 74 65 73 20 74 68 61 74 20 77   of bytes that w
f1ad0 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20  ill be returned 
f1ae0 74 6f 20 74 68 65 20 68 65 61 70 20 77 68 65 6e  to the heap when
f1af0 0a 2a 2a 20 74 68 65 20 61 72 67 75 6d 65 6e 74  .** the argument
f1b00 20 69 73 20 70 61 73 73 65 64 20 74 6f 20 70 63   is passed to pc
f1b10 61 63 68 65 50 61 67 65 46 72 65 65 28 29 2e 0a  achePageFree()..
f1b20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 63  */.static int pc
f1b30 61 63 68 65 50 61 67 65 53 69 7a 65 28 50 67 48  achePageSize(PgH
f1b40 64 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  dr *p){.  assert
f1b50 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
f1b60 68 65 6c 64 28 70 63 61 63 68 65 5f 67 2e 6d 75  held(pcache_g.mu
f1b70 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
f1b80 28 20 21 70 63 61 63 68 65 5f 67 2e 70 53 74 61  ( !pcache_g.pSta
f1b90 72 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  rt );.  assert( 
f1ba0 70 2d 3e 61 70 53 61 76 65 5b 30 5d 3d 3d 30 20  p->apSave[0]==0 
f1bb0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
f1bc0 61 70 53 61 76 65 5b 31 5d 3d 3d 30 20 29 3b 0a  apSave[1]==0 );.
f1bd0 20 20 61 73 73 65 72 74 28 20 70 20 26 26 20 70    assert( p && p
f1be0 2d 3e 70 43 61 63 68 65 20 29 3b 0a 20 20 72 65  ->pCache );.  re
f1bf0 74 75 72 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c  turn sqlite3Mall
f1c00 6f 63 53 69 7a 65 28 70 29 3b 0a 7d 0a 23 65 6e  ocSize(p);.}.#en
f1c10 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d  dif../*.** Attem
f1c20 70 74 20 74 6f 20 27 72 65 63 79 63 6c 65 27 20  pt to 'recycle' 
f1c30 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20  a page from the 
f1c40 67 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74 2e  global LRU list.
f1c50 20 4f 6e 6c 79 20 63 6c 65 61 6e 2c 0a 2a 2a 20   Only clean,.** 
f1c60 75 6e 72 65 66 65 72 65 6e 63 65 64 20 70 61 67  unreferenced pag
f1c70 65 73 20 66 72 6f 6d 20 70 75 72 67 65 61 62 6c  es from purgeabl
f1c80 65 20 63 61 63 68 65 73 20 61 72 65 20 65 6c 69  e caches are eli
f1c90 67 69 62 6c 65 20 66 6f 72 20 72 65 63 79 63 6c  gible for recycl
f1ca0 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ing..**.** This 
f1cb0 66 75 6e 63 74 69 6f 6e 20 72 65 6d 6f 76 65 73  function removes
f1cc0 20 70 61 67 65 20 70 63 61 63 68 65 2e 70 4c 72   page pcache.pLr
f1cd0 75 54 61 69 6c 20 66 72 6f 6d 20 74 68 65 20 67  uTail from the g
f1ce0 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74 2c 0a  lobal LRU list,.
f1cf0 2a 2a 20 61 6e 64 20 66 72 6f 6d 20 74 68 65 20  ** and from the 
f1d00 68 61 73 68 2d 74 61 62 6c 65 20 61 6e 64 20 50  hash-table and P
f1d10 43 61 63 68 65 2e 70 43 6c 65 61 6e 20 6c 69 73  Cache.pClean lis
f1d20 74 20 6f 66 20 74 68 65 20 6f 77 6e 65 72 20 70  t of the owner p
f1d30 63 61 63 68 65 2e 0a 2a 2a 20 54 68 65 72 65 20  cache..** There 
f1d40 73 68 6f 75 6c 64 20 62 65 20 6e 6f 20 6f 74 68  should be no oth
f1d50 65 72 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f  er references to
f1d60 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a   the page..**.**
f1d70 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
f1d80 65 20 72 65 63 79 63 6c 65 64 20 70 61 67 65 20  e recycled page 
f1d90 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 72 20  is returned, or 
f1da0 4e 55 4c 4c 20 69 66 20 6e 6f 20 70 61 67 65 20  NULL if no page 
f1db0 69 73 0a 2a 2a 20 65 6c 69 67 69 62 6c 65 20 66  is.** eligible f
f1dc0 6f 72 20 72 65 63 79 63 6c 69 6e 67 2e 0a 2a 2f  or recycling..*/
f1dd0 0a 73 74 61 74 69 63 20 50 67 48 64 72 20 2a 70  .static PgHdr *p
f1de0 63 61 63 68 65 52 65 63 79 63 6c 65 50 61 67 65  cacheRecyclePage
f1df0 28 76 6f 69 64 29 7b 0a 20 20 50 67 48 64 72 20  (void){.  PgHdr 
f1e00 2a 70 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  *p = 0;.  assert
f1e10 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
f1e20 68 65 6c 64 28 70 63 61 63 68 65 5f 67 2e 6d 75  held(pcache_g.mu
f1e30 74 65 78 29 20 29 3b 0a 0a 20 20 69 66 28 20 28  tex) );..  if( (
f1e40 70 3d 70 63 61 63 68 65 5f 67 2e 70 4c 72 75 54  p=pcache_g.pLruT
f1e50 61 69 6c 29 20 29 7b 0a 20 20 20 20 61 73 73 65  ail) ){.    asse
f1e60 72 74 28 20 28 70 2d 3e 66 6c 61 67 73 26 50 47  rt( (p->flags&PG
f1e70 48 44 52 5f 44 49 52 54 59 29 3d 3d 30 20 29 3b  HDR_DIRTY)==0 );
f1e80 0a 20 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76  .    pcacheRemov
f1e90 65 46 72 6f 6d 4c 72 75 4c 69 73 74 28 70 29 3b  eFromLruList(p);
f1ea0 0a 20 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76  .    pcacheRemov
f1eb0 65 46 72 6f 6d 48 61 73 68 28 70 29 3b 0a 20 20  eFromHash(p);.  
f1ec0 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72    pcacheRemoveFr
f1ed0 6f 6d 4c 69 73 74 28 26 70 2d 3e 70 43 61 63 68  omList(&p->pCach
f1ee0 65 2d 3e 70 43 6c 65 61 6e 2c 20 70 29 3b 0a 20  e->pClean, p);. 
f1ef0 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a   }..  return p;.
f1f00 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20  }../*.** Obtain 
f1f10 73 70 61 63 65 20 66 6f 72 20 61 20 70 61 67 65  space for a page
f1f20 2e 20 54 72 79 20 74 6f 20 72 65 63 79 63 6c 65  . Try to recycle
f1f30 20 61 6e 20 6f 6c 64 20 70 61 67 65 20 69 66 20   an old page if 
f1f40 74 68 65 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65  the limit on the
f1f50 20 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 70   .** number of p
f1f60 61 67 65 73 20 68 61 73 20 62 65 65 6e 20 72 65  ages has been re
f1f70 61 63 68 65 64 2e 20 49 66 20 74 68 65 20 6c 69  ached. If the li
f1f80 6d 69 74 20 68 61 73 20 6e 6f 74 20 62 65 65 6e  mit has not been
f1f90 20 72 65 61 63 68 65 64 20 6f 72 0a 2a 2a 20 74   reached or.** t
f1fa0 68 65 72 65 20 61 72 65 20 6e 6f 20 70 61 67 65  here are no page
f1fb0 73 20 65 6c 69 67 69 62 6c 65 20 66 6f 72 20 72  s eligible for r
f1fc0 65 63 79 63 6c 69 6e 67 2c 20 61 6c 6c 6f 63 61  ecycling, alloca
f1fd0 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e 0a 2a  te a new page..*
f1fe0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  *.** Return a po
f1ff0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77  inter to the new
f2000 20 70 61 67 65 2c 20 6f 72 20 4e 55 4c 4c 20 69   page, or NULL i
f2010 66 20 61 6e 20 4f 4f 4d 20 63 6f 6e 64 69 74 69  f an OOM conditi
f2020 6f 6e 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74  on occurs..*/.st
f2030 61 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 52  atic int pcacheR
f2040 65 63 79 63 6c 65 4f 72 41 6c 6c 6f 63 28 50 43  ecycleOrAlloc(PC
f2050 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 50 67  ache *pCache, Pg
f2060 48 64 72 20 2a 2a 70 70 50 61 67 65 29 7b 0a 20  Hdr **ppPage){. 
f2070 20 50 67 48 64 72 20 2a 70 20 3d 20 30 3b 0a 0a   PgHdr *p = 0;..
f2080 20 20 69 6e 74 20 73 7a 50 61 67 65 20 3d 20 70    int szPage = p
f2090 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 3b 0a 20  Cache->szPage;. 
f20a0 20 69 6e 74 20 73 7a 45 78 74 72 61 20 3d 20 70   int szExtra = p
f20b0 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 3b 0a  Cache->szExtra;.
f20c0 0a 20 20 61 73 73 65 72 74 28 20 70 63 61 63 68  .  assert( pcach
f20d0 65 5f 67 2e 69 73 49 6e 69 74 20 29 3b 0a 20 20  e_g.isInit );.  
f20e0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
f20f0 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68  mutex_held(pcach
f2100 65 5f 67 2e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  e_g.mutex) );.. 
f2110 20 2a 70 70 50 61 67 65 20 3d 20 30 3b 0a 0a 20   *ppPage = 0;.. 
f2120 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 72   /* If we have r
f2130 65 61 63 68 65 64 20 65 69 74 68 65 72 20 74 68  eached either th
f2140 65 20 67 6c 6f 62 61 6c 20 6f 72 20 74 68 65 20  e global or the 
f2150 6c 6f 63 61 6c 20 6c 69 6d 69 74 20 66 6f 72 20  local limit for 
f2160 0a 20 20 2a 2a 20 70 69 6e 6e 65 64 2b 64 69 72  .  ** pinned+dir
f2170 74 79 20 70 61 67 65 73 2c 20 61 6e 64 20 74 68  ty pages, and th
f2180 65 72 65 20 69 73 20 61 74 20 6c 65 61 73 74 20  ere is at least 
f2190 6f 6e 65 20 64 69 72 74 79 20 70 61 67 65 2c 0a  one dirty page,.
f21a0 20 20 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20    ** invoke the 
f21b0 78 53 74 72 65 73 73 20 63 61 6c 6c 62 61 63 6b  xStress callback
f21c0 20 74 6f 20 63 61 75 73 65 20 61 20 70 61 67 65   to cause a page
f21d0 20 74 6f 20 62 65 63 6f 6d 65 20 63 6c 65 61 6e   to become clean
f21e0 2e 0a 20 20 2a 2f 0a 20 20 65 78 70 65 6e 73 69  ..  */.  expensi
f21f0 76 65 5f 61 73 73 65 72 74 28 20 70 43 61 63 68  ve_assert( pCach
f2200 65 2d 3e 6e 50 69 6e 6e 65 64 3d 3d 70 63 61 63  e->nPinned==pcac
f2210 68 65 50 69 6e 6e 65 64 43 6f 75 6e 74 28 70 43  hePinnedCount(pC
f2220 61 63 68 65 29 20 29 3b 0a 20 20 65 78 70 65 6e  ache) );.  expen
f2230 73 69 76 65 5f 61 73 73 65 72 74 28 20 70 63 61  sive_assert( pca
f2240 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64 28 70  cheCheckSynced(p
f2250 43 61 63 68 65 29 20 29 3b 0a 20 20 69 66 28 20  Cache) );.  if( 
f2260 70 43 61 63 68 65 2d 3e 78 53 74 72 65 73 73 0a  pCache->xStress.
f2270 20 20 20 26 26 20 70 43 61 63 68 65 2d 3e 70 44     && pCache->pD
f2280 69 72 74 79 0a 20 20 20 26 26 20 28 70 43 61 63  irty.   && (pCac
f2290 68 65 2d 3e 6e 50 69 6e 6e 65 64 3e 3d 28 70 63  he->nPinned>=(pc
f22a0 61 63 68 65 5f 67 2e 6e 4d 61 78 50 61 67 65 2b  ache_g.nMaxPage+
f22b0 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 2d 70 63 61  pCache->nMin-pca
f22c0 63 68 65 5f 67 2e 6e 4d 69 6e 50 61 67 65 29 0a  che_g.nMinPage).
f22d0 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 43             || pC
f22e0 61 63 68 65 2d 3e 6e 50 69 6e 6e 65 64 3e 3d 70  ache->nPinned>=p
f22f0 43 61 63 68 65 2d 3e 6e 4d 61 78 29 0a 20 20 29  Cache->nMax).  )
f2300 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67  {.    PgHdr *pPg
f2310 3b 0a 20 20 20 20 61 73 73 65 72 74 28 70 43 61  ;.    assert(pCa
f2320 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 29  che->pDirtyTail)
f2330 3b 0a 0a 20 20 20 20 66 6f 72 28 70 50 67 3d 70  ;..    for(pPg=p
f2340 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 3b 20  Cache->pSynced; 
f2350 0a 20 20 20 20 20 20 20 20 70 50 67 20 26 26 20  .        pPg && 
f2360 28 70 50 67 2d 3e 6e 52 65 66 20 7c 7c 20 28 70  (pPg->nRef || (p
f2370 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  Pg->flags&PGHDR_
f2380 4e 45 45 44 5f 53 59 4e 43 29 29 3b 20 0a 20 20  NEED_SYNC)); .  
f2390 20 20 20 20 20 20 70 50 67 3d 70 50 67 2d 3e 70        pPg=pPg->p
f23a0 50 72 65 76 0a 20 20 20 20 29 3b 0a 20 20 20 20  Prev.    );.    
f23b0 69 66 28 20 21 70 50 67 20 29 7b 0a 20 20 20 20  if( !pPg ){.    
f23c0 20 20 66 6f 72 28 70 50 67 3d 70 43 61 63 68 65    for(pPg=pCache
f23d0 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b 20 70 50  ->pDirtyTail; pP
f23e0 67 20 26 26 20 70 50 67 2d 3e 6e 52 65 66 3b 20  g && pPg->nRef; 
f23f0 70 50 67 3d 70 50 67 2d 3e 70 50 72 65 76 29 3b  pPg=pPg->pPrev);
f2400 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
f2410 50 67 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  Pg ){.      int 
f2420 72 63 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65  rc;.      pcache
f2430 45 78 69 74 4d 75 74 65 78 28 29 3b 0a 20 20 20  ExitMutex();.   
f2440 20 20 20 72 63 20 3d 20 70 43 61 63 68 65 2d 3e     rc = pCache->
f2450 78 53 74 72 65 73 73 28 70 43 61 63 68 65 2d 3e  xStress(pCache->
f2460 70 53 74 72 65 73 73 2c 20 70 50 67 29 3b 0a 20  pStress, pPg);. 
f2470 20 20 20 20 20 70 63 61 63 68 65 45 6e 74 65 72       pcacheEnter
f2480 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 69  Mutex();.      i
f2490 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
f24a0 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 42   && rc!=SQLITE_B
f24b0 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 72  USY ){.        r
f24c0 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
f24d0 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  }.    }.  }..  /
f24e0 2a 20 49 66 20 65 69 74 68 65 72 20 74 68 65 20  * If either the 
f24f0 6c 6f 63 61 6c 20 6f 72 20 74 68 65 20 67 6c 6f  local or the glo
f2500 62 61 6c 20 70 61 67 65 20 6c 69 6d 69 74 20 68  bal page limit h
f2510 61 73 20 62 65 65 6e 20 72 65 61 63 68 65 64 2c  as been reached,
f2520 20 0a 20 20 2a 2a 20 74 72 79 20 74 6f 20 72 65   .  ** try to re
f2530 63 79 63 6c 65 20 61 20 70 61 67 65 2e 20 0a 20  cycle a page. . 
f2540 20 2a 2f 0a 20 20 69 66 28 20 70 43 61 63 68 65   */.  if( pCache
f2550 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 26 26 20  ->bPurgeable && 
f2560 28 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 3e 3d  (pCache->nPage>=
f2570 70 43 61 63 68 65 2d 3e 6e 4d 61 78 2d 31 20 7c  pCache->nMax-1 |
f2580 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  |.              
f2590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
f25a0 63 61 63 68 65 5f 67 2e 6e 43 75 72 72 65 6e 74  cache_g.nCurrent
f25b0 50 61 67 65 3e 3d 70 63 61 63 68 65 5f 67 2e 6e  Page>=pcache_g.n
f25c0 4d 61 78 50 61 67 65 29 20 29 7b 0a 20 20 20 20  MaxPage) ){.    
f25d0 70 20 3d 20 70 63 61 63 68 65 52 65 63 79 63 6c  p = pcacheRecycl
f25e0 65 50 61 67 65 28 29 3b 0a 20 20 7d 0a 0a 20 20  ePage();.  }..  
f25f0 2f 2a 20 49 66 20 61 20 70 61 67 65 20 68 61 73  /* If a page has
f2600 20 62 65 65 6e 20 72 65 63 79 63 6c 65 64 20 62   been recycled b
f2610 75 74 20 69 74 20 69 73 20 74 68 65 20 77 72 6f  ut it is the wro
f2620 6e 67 20 73 69 7a 65 2c 20 66 72 65 65 20 69 74  ng size, free it
f2630 2e 20 2a 2f 0a 20 20 69 66 28 20 70 20 26 26 20  . */.  if( p && 
f2640 28 70 2d 3e 70 43 61 63 68 65 2d 3e 73 7a 50 61  (p->pCache->szPa
f2650 67 65 21 3d 73 7a 50 61 67 65 20 7c 7c 20 70 2d  ge!=szPage || p-
f2660 3e 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 21  >pCache->szPage!
f2670 3d 73 7a 45 78 74 72 61 29 20 29 7b 0a 20 20 20  =szExtra) ){.   
f2680 20 70 63 61 63 68 65 50 61 67 65 46 72 65 65 28   pcachePageFree(
f2690 70 29 3b 0a 20 20 20 20 70 20 3d 20 30 3b 0a 20  p);.    p = 0;. 
f26a0 20 7d 0a 0a 20 20 69 66 28 20 21 70 20 29 7b 0a   }..  if( !p ){.
f26b0 20 20 20 20 70 20 3d 20 70 63 61 63 68 65 50 61      p = pcachePa
f26c0 67 65 41 6c 6c 6f 63 28 70 43 61 63 68 65 29 3b  geAlloc(pCache);
f26d0 0a 20 20 7d 0a 0a 20 20 2a 70 70 50 61 67 65 20  .  }..  *ppPage 
f26e0 3d 20 70 3b 0a 20 20 72 65 74 75 72 6e 20 28 70  = p;.  return (p
f26f0 3f 53 51 4c 49 54 45 5f 4f 4b 3a 53 51 4c 49 54  ?SQLITE_OK:SQLIT
f2700 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f 2a 2a  E_NOMEM);.}../**
f2710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2740 2a 20 47 65 6e 65 72 61 6c 20 49 6e 74 65 72 66  * General Interf
f2750 61 63 65 73 20 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  aces ******.**.*
f2760 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64  * Initialize and
f2770 20 73 68 75 74 64 6f 77 6e 20 74 68 65 20 70 61   shutdown the pa
f2780 67 65 20 63 61 63 68 65 20 73 75 62 73 79 73 74  ge cache subsyst
f2790 65 6d 2e 20 4e 65 69 74 68 65 72 20 6f 66 20 74  em. Neither of t
f27a0 68 65 73 65 20 0a 2a 2a 20 66 75 6e 63 74 69 6f  hese .** functio
f27b0 6e 73 20 61 72 65 20 74 68 72 65 61 64 73 61 66  ns are threadsaf
f27c0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
f27d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
f27e0 50 63 61 63 68 65 49 6e 69 74 69 61 6c 69 7a 65  PcacheInitialize
f27f0 28 76 6f 69 64 29 7b 0a 20 20 61 73 73 65 72 74  (void){.  assert
f2800 28 20 70 63 61 63 68 65 5f 67 2e 69 73 49 6e 69  ( pcache_g.isIni
f2810 74 3d 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 65 74  t==0 );.  memset
f2820 28 26 70 63 61 63 68 65 5f 67 2c 20 30 2c 20 73  (&pcache_g, 0, s
f2830 69 7a 65 6f 66 28 70 63 61 63 68 65 29 29 3b 0a  izeof(pcache));.
f2840 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
f2850 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d  balConfig.bCoreM
f2860 75 74 65 78 20 29 7b 0a 20 20 20 20 2f 2a 20 4e  utex ){.    /* N
f2870 6f 20 6e 65 65 64 20 74 6f 20 63 68 65 63 6b 20  o need to check 
f2880 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
f2890 20 6f 66 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   of sqlite3_mute
f28a0 78 5f 61 6c 6c 6f 63 28 29 2e 20 0a 20 20 20 20  x_alloc(). .    
f28b0 2a 2a 20 41 6c 6c 6f 63 61 74 69 6e 67 20 61 20  ** Allocating a 
f28c0 73 74 61 74 69 63 20 6d 75 74 65 78 20 63 61 6e  static mutex can
f28d0 6e 6f 74 20 66 61 69 6c 2e 0a 20 20 20 20 2a 2f  not fail..    */
f28e0 0a 20 20 20 20 70 63 61 63 68 65 5f 67 2e 6d 75  .    pcache_g.mu
f28f0 74 65 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 75  tex = sqlite3_mu
f2900 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45  tex_alloc(SQLITE
f2910 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52  _MUTEX_STATIC_LR
f2920 55 29 3b 0a 20 20 7d 0a 20 20 70 63 61 63 68 65  U);.  }.  pcache
f2930 5f 67 2e 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20  _g.isInit = 1;. 
f2940 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
f2950 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  K;.}.SQLITE_PRIV
f2960 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
f2970 50 63 61 63 68 65 53 68 75 74 64 6f 77 6e 28 76  PcacheShutdown(v
f2980 6f 69 64 29 7b 0a 20 20 6d 65 6d 73 65 74 28 26  oid){.  memset(&
f2990 70 63 61 63 68 65 5f 67 2c 20 30 2c 20 73 69 7a  pcache_g, 0, siz
f29a0 65 6f 66 28 70 63 61 63 68 65 29 29 3b 0a 7d 0a  eof(pcache));.}.
f29b0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
f29c0 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20  e size in bytes 
f29d0 6f 66 20 61 20 50 43 61 63 68 65 20 6f 62 6a 65  of a PCache obje
f29e0 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ct..*/.SQLITE_PR
f29f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
f2a00 33 50 63 61 63 68 65 53 69 7a 65 28 76 6f 69 64  3PcacheSize(void
f2a10 29 7b 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66  ){ return sizeof
f2a20 28 50 43 61 63 68 65 29 3b 20 7d 0a 0a 2f 2a 0a  (PCache); }../*.
f2a30 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
f2a40 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 20  PCache object.  
f2a50 53 74 6f 72 61 67 65 20 73 70 61 63 65 20 74 6f  Storage space to
f2a60 20 68 6f 6c 64 20 74 68 65 20 6f 62 6a 65 63 74   hold the object
f2a70 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20  .** has already 
f2a80 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61  been allocated a
f2a90 6e 64 20 69 73 20 70 61 73 73 65 64 20 69 6e 20  nd is passed in 
f2aa0 61 73 20 74 68 65 20 70 20 70 6f 69 6e 74 65 72  as the p pointer
f2ab0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
f2ac0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
f2ad0 50 63 61 63 68 65 4f 70 65 6e 28 0a 20 20 69 6e  PcacheOpen(.  in
f2ae0 74 20 73 7a 50 61 67 65 2c 20 20 20 20 20 20 20  t szPage,       
f2af0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
f2b00 7a 65 20 6f 66 20 65 76 65 72 79 20 70 61 67 65  ze of every page
f2b10 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72   */.  int szExtr
f2b20 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  a,              
f2b30 20 20 20 2f 2a 20 45 78 74 72 61 20 73 70 61 63     /* Extra spac
f2b40 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
f2b50 68 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20  h each page */. 
f2b60 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 2c   int bPurgeable,
f2b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
f2b80 20 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61   True if pages a
f2b90 72 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74  re on backing st
f2ba0 6f 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78  ore */.  int (*x
f2bb0 53 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 67  Stress)(void*,Pg
f2bc0 48 64 72 2a 29 2c 2f 2a 20 43 61 6c 6c 20 74 6f  Hdr*),/* Call to
f2bd0 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67   try to make pag
f2be0 65 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f  es clean */.  vo
f2bf0 69 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 20  id *pStress,    
f2c00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
f2c10 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 73  gument to xStres
f2c20 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70  s */.  PCache *p
f2c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2c40 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61      /* Prealloca
f2c50 74 65 64 20 73 70 61 63 65 20 66 6f 72 20 74 68  ted space for th
f2c60 65 20 50 43 61 63 68 65 20 2a 2f 0a 29 7b 0a 20  e PCache */.){. 
f2c70 20 61 73 73 65 72 74 28 20 70 63 61 63 68 65 5f   assert( pcache_
f2c80 67 2e 69 73 49 6e 69 74 20 29 3b 0a 20 20 6d 65  g.isInit );.  me
f2c90 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f  mset(p, 0, sizeo
f2ca0 66 28 50 43 61 63 68 65 29 29 3b 0a 20 20 70 2d  f(PCache));.  p-
f2cb0 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61 67 65  >szPage = szPage
f2cc0 3b 0a 20 20 70 2d 3e 73 7a 45 78 74 72 61 20 3d  ;.  p->szExtra =
f2cd0 20 73 7a 45 78 74 72 61 3b 0a 20 20 70 2d 3e 62   szExtra;.  p->b
f2ce0 50 75 72 67 65 61 62 6c 65 20 3d 20 62 50 75 72  Purgeable = bPur
f2cf0 67 65 61 62 6c 65 3b 0a 20 20 70 2d 3e 78 53 74  geable;.  p->xSt
f2d00 72 65 73 73 20 3d 20 78 53 74 72 65 73 73 3b 0a  ress = xStress;.
f2d10 20 20 70 2d 3e 70 53 74 72 65 73 73 20 3d 20 70    p->pStress = p
f2d20 53 74 72 65 73 73 3b 0a 20 20 70 2d 3e 6e 4d 61  Stress;.  p->nMa
f2d30 78 20 3d 20 31 30 30 3b 0a 20 20 70 2d 3e 6e 4d  x = 100;.  p->nM
f2d40 69 6e 20 3d 20 31 30 3b 0a 0a 20 20 70 63 61 63  in = 10;..  pcac
f2d50 68 65 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a  heEnterMutex();.
f2d60 20 20 69 66 28 20 62 50 75 72 67 65 61 62 6c 65    if( bPurgeable
f2d70 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 5f 67   ){.    pcache_g
f2d80 2e 6e 4d 61 78 50 61 67 65 20 2b 3d 20 70 2d 3e  .nMaxPage += p->
f2d90 6e 4d 61 78 3b 0a 20 20 20 20 70 63 61 63 68 65  nMax;.    pcache
f2da0 5f 67 2e 6e 4d 69 6e 50 61 67 65 20 2b 3d 20 70  _g.nMinPage += p
f2db0 2d 3e 6e 4d 69 6e 3b 0a 20 20 7d 0a 0a 20 20 70  ->nMin;.  }..  p
f2dc0 63 61 63 68 65 45 78 69 74 4d 75 74 65 78 28 29  cacheExitMutex()
f2dd0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  ;.}../*.** Chang
f2de0 65 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20  e the page size 
f2df0 66 6f 72 20 50 43 61 63 68 65 20 6f 62 6a 65 63  for PCache objec
f2e00 74 2e 20 20 54 68 69 73 20 63 61 6e 20 6f 6e 6c  t.  This can onl
f2e10 79 20 68 61 70 70 65 6e 0a 2a 2a 20 77 68 65 6e  y happen.** when
f2e20 20 74 68 65 20 63 61 63 68 65 20 69 73 20 65 6d   the cache is em
f2e30 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  pty..*/.SQLITE_P
f2e40 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
f2e50 74 65 33 50 63 61 63 68 65 53 65 74 50 61 67 65  te3PcacheSetPage
f2e60 53 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61  Size(PCache *pCa
f2e70 63 68 65 2c 20 69 6e 74 20 73 7a 50 61 67 65 29  che, int szPage)
f2e80 7b 0a 20 20 61 73 73 65 72 74 28 70 43 61 63 68  {.  assert(pCach
f2e90 65 2d 3e 6e 50 61 67 65 3d 3d 30 29 3b 0a 20 20  e->nPage==0);.  
f2ea0 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 3d  pCache->szPage =
f2eb0 20 73 7a 50 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a   szPage;.}../*.*
f2ec0 2a 20 54 72 79 20 74 6f 20 6f 62 74 61 69 6e 20  * Try to obtain 
f2ed0 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20  a page from the 
f2ee0 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cache..*/.SQLITE
f2ef0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
f2f00 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 28  ite3PcacheFetch(
f2f10 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68  .  PCache *pCach
f2f20 65 2c 20 20 20 20 20 20 20 2f 2a 20 4f 62 74 61  e,       /* Obta
f2f30 69 6e 20 74 68 65 20 70 61 67 65 20 66 72 6f 6d  in the page from
f2f40 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a 20   this cache */. 
f2f50 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20   Pgno pgno,     
f2f60 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e         /* Page n
f2f70 75 6d 62 65 72 20 74 6f 20 6f 62 74 61 69 6e 20  umber to obtain 
f2f80 2a 2f 0a 20 20 69 6e 74 20 63 72 65 61 74 65 46  */.  int createF
f2f90 6c 61 67 2c 20 20 20 20 20 20 20 2f 2a 20 49 66  lag,       /* If
f2fa0 20 74 72 75 65 2c 20 63 72 65 61 74 65 20 70 61   true, create pa
f2fb0 67 65 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f  ge if it does no
f2fc0 74 20 65 78 69 73 74 20 61 6c 72 65 61 64 79 20  t exist already 
f2fd0 2a 2f 0a 20 20 50 67 48 64 72 20 2a 2a 70 70 50  */.  PgHdr **ppP
f2fe0 61 67 65 20 20 20 20 20 20 20 20 2f 2a 20 57 72  age        /* Wr
f2ff0 69 74 65 20 74 68 65 20 70 61 67 65 20 68 65 72  ite the page her
f3000 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
f3010 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
f3020 50 67 48 64 72 20 2a 70 50 61 67 65 20 3d 20 30  PgHdr *pPage = 0
f3030 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 63 61  ;..  assert( pca
f3040 63 68 65 5f 67 2e 69 73 49 6e 69 74 20 29 3b 0a  che_g.isInit );.
f3050 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65    assert( pCache
f3060 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
f3070 20 70 67 6e 6f 3e 30 20 29 3b 0a 20 20 65 78 70   pgno>0 );.  exp
f3080 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20 70  ensive_assert( p
f3090 43 61 63 68 65 2d 3e 6e 50 69 6e 6e 65 64 3d 3d  Cache->nPinned==
f30a0 70 63 61 63 68 65 50 69 6e 6e 65 64 43 6f 75 6e  pcachePinnedCoun
f30b0 74 28 70 43 61 63 68 65 29 20 29 3b 0a 0a 20 20  t(pCache) );..  
f30c0 70 63 61 63 68 65 45 6e 74 65 72 4d 75 74 65 78  pcacheEnterMutex
f30d0 28 29 3b 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68  ();..  /* Search
f30e0 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
f30f0 66 6f 72 20 74 68 65 20 72 65 71 75 65 73 74 65  for the requeste
f3100 64 20 70 61 67 65 2e 20 45 78 69 74 20 65 61 72  d page. Exit ear
f3110 6c 79 20 69 66 20 69 74 20 69 73 20 66 6f 75 6e  ly if it is foun
f3120 64 2e 20 2a 2f 0a 20 20 69 66 28 20 70 43 61 63  d. */.  if( pCac
f3130 68 65 2d 3e 61 70 48 61 73 68 20 29 7b 0a 20 20  he->apHash ){.  
f3140 20 20 75 33 32 20 68 20 3d 20 70 67 6e 6f 20 25    u32 h = pgno %
f3150 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a   pCache->nHash;.
f3160 20 20 20 20 66 6f 72 28 70 50 61 67 65 3d 70 43      for(pPage=pC
f3170 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b  ache->apHash[h];
f3180 20 70 50 61 67 65 3b 20 70 50 61 67 65 3d 70 50   pPage; pPage=pP
f3190 61 67 65 2d 3e 70 4e 65 78 74 48 61 73 68 29 7b  age->pNextHash){
f31a0 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65  .      if( pPage
f31b0 2d 3e 70 67 6e 6f 3d 3d 70 67 6e 6f 20 29 7b 0a  ->pgno==pgno ){.
f31c0 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67          if( pPag
f31d0 65 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20  e->nRef==0 ){.  
f31e0 20 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28          if( 0==(
f31f0 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48  pPage->flags&PGH
f3200 44 52 5f 44 49 52 54 59 29 20 29 7b 0a 20 20 20  DR_DIRTY) ){.   
f3210 20 20 20 20 20 20 20 20 20 70 63 61 63 68 65 52           pcacheR
f3220 65 6d 6f 76 65 46 72 6f 6d 4c 72 75 4c 69 73 74  emoveFromLruList
f3230 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  (pPage);.       
f3240 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 50 69       pCache->nPi
f3250 6e 6e 65 64 2b 2b 3b 0a 20 20 20 20 20 20 20 20  nned++;.        
f3260 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 43    }.          pC
f3270 61 63 68 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20  ache->nRef++;.  
f3280 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
f3290 70 50 61 67 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20  pPage->nRef++;. 
f32a0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
f32b0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
f32c0 0a 20 20 69 66 28 20 21 70 50 61 67 65 20 26 26  .  if( !pPage &&
f32d0 20 63 72 65 61 74 65 46 6c 61 67 20 29 7b 0a 20   createFlag ){. 
f32e0 20 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 6e     if( pCache->n
f32f0 48 61 73 68 3c 3d 70 43 61 63 68 65 2d 3e 6e 50  Hash<=pCache->nP
f3300 61 67 65 20 29 7b 0a 20 20 20 20 20 20 72 63 20  age ){.      rc 
f3310 3d 20 70 63 61 63 68 65 52 65 73 69 7a 65 48 61  = pcacheResizeHa
f3320 73 68 28 70 43 61 63 68 65 2c 20 70 43 61 63 68  sh(pCache, pCach
f3330 65 2d 3e 6e 48 61 73 68 3c 32 35 36 20 3f 20 32  e->nHash<256 ? 2
f3340 35 36 20 3a 20 70 43 61 63 68 65 2d 3e 6e 48 61  56 : pCache->nHa
f3350 73 68 2a 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20  sh*2);.    }.   
f3360 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
f3370 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  OK ){.      rc =
f3380 20 70 63 61 63 68 65 52 65 63 79 63 6c 65 4f 72   pcacheRecycleOr
f3390 41 6c 6c 6f 63 28 70 43 61 63 68 65 2c 20 26 70  Alloc(pCache, &p
f33a0 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Page);.    }.   
f33b0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
f33c0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67  OK ){.      pPag
f33d0 65 2d 3e 70 50 61 67 65 72 20 3d 20 30 3b 0a 20  e->pPager = 0;. 
f33e0 20 20 20 20 20 70 50 61 67 65 2d 3e 66 6c 61 67       pPage->flag
f33f0 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61  s = 0;.      pPa
f3400 67 65 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a  ge->pDirty = 0;.
f3410 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 67 6e        pPage->pgn
f3420 6f 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20  o = pgno;.      
f3430 70 50 61 67 65 2d 3e 70 43 61 63 68 65 20 3d 20  pPage->pCache = 
f3440 70 43 61 63 68 65 3b 0a 20 20 20 20 20 20 70 50  pCache;.      pP
f3450 61 67 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20  age->nRef = 1;. 
f3460 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65       pCache->nRe
f3470 66 2b 2b 3b 0a 20 20 20 20 20 20 70 43 61 63 68  f++;.      pCach
f3480 65 2d 3e 6e 50 69 6e 6e 65 64 2b 2b 3b 0a 20 20  e->nPinned++;.  
f3490 20 20 20 20 70 63 61 63 68 65 41 64 64 54 6f 4c      pcacheAddToL
f34a0 69 73 74 28 26 70 43 61 63 68 65 2d 3e 70 43 6c  ist(&pCache->pCl
f34b0 65 61 6e 2c 20 70 50 61 67 65 29 3b 0a 20 20 20  ean, pPage);.   
f34c0 20 20 20 70 63 61 63 68 65 41 64 64 54 6f 48 61     pcacheAddToHa
f34d0 73 68 28 70 50 61 67 65 29 3b 0a 20 20 20 20 7d  sh(pPage);.    }
f34e0 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 45 78  .  }..  pcacheEx
f34f0 69 74 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2a 70  itMutex();..  *p
f3500 70 50 61 67 65 20 3d 20 70 50 61 67 65 3b 0a 20  pPage = pPage;. 
f3510 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72   expensive_asser
f3520 74 28 20 70 43 61 63 68 65 2d 3e 6e 50 69 6e 6e  t( pCache->nPinn
f3530 65 64 3d 3d 70 63 61 63 68 65 50 69 6e 6e 65 64  ed==pcachePinned
f3540 43 6f 75 6e 74 28 70 43 61 63 68 65 29 20 29 3b  Count(pCache) );
f3550 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
f3560 20 7c 7c 20 21 63 72 65 61 74 65 46 6c 61 67 20   || !createFlag 
f3570 7c 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  || rc!=SQLITE_OK
f3580 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   );.  return rc;
f3590 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 72 65 66 65  .}../*.** Derefe
f35a0 72 65 6e 63 65 20 61 20 70 61 67 65 2e 20 20 57  rence a page.  W
f35b0 68 65 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63  hen the referenc
f35c0 65 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20  e count reaches 
f35d0 7a 65 72 6f 2c 0a 2a 2a 20 6d 6f 76 65 20 74 68  zero,.** move th
f35e0 65 20 70 61 67 65 20 74 6f 20 74 68 65 20 4c 52  e page to the LR
f35f0 55 20 6c 69 73 74 20 69 66 20 69 74 20 69 73 20  U list if it is 
f3600 63 6c 65 61 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  clean..*/.SQLITE
f3610 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
f3620 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61  lite3PcacheRelea
f3630 73 65 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20  se(PgHdr *p){.  
f3640 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e  assert( p->nRef>
f3650 30 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d  0 );.  p->nRef--
f3660 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d  ;.  if( p->nRef=
f3670 3d 30 20 29 7b 0a 20 20 20 20 50 43 61 63 68 65  =0 ){.    PCache
f3680 20 2a 70 43 61 63 68 65 20 3d 20 70 2d 3e 70 43   *pCache = p->pC
f3690 61 63 68 65 3b 0a 20 20 20 20 70 43 61 63 68 65  ache;.    pCache
f36a0 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66  ->nRef--;.    if
f36b0 28 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44  ( (p->flags&PGHD
f36c0 52 5f 44 49 52 54 59 29 3d 3d 30 20 29 7b 0a 20  R_DIRTY)==0 ){. 
f36d0 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 50 69       pCache->nPi
f36e0 6e 6e 65 64 2d 2d 3b 0a 20 20 20 20 20 20 70 63  nned--;.      pc
f36f0 61 63 68 65 45 6e 74 65 72 4d 75 74 65 78 28 29  acheEnterMutex()
f3700 3b 0a 20 20 20 20 20 20 69 66 28 20 70 63 61 63  ;.      if( pcac
f3710 68 65 5f 67 2e 6e 43 75 72 72 65 6e 74 50 61 67  he_g.nCurrentPag
f3720 65 3e 70 63 61 63 68 65 5f 67 2e 6e 4d 61 78 50  e>pcache_g.nMaxP
f3730 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70  age ){.        p
f3740 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 4c  cacheRemoveFromL
f3750 69 73 74 28 26 70 43 61 63 68 65 2d 3e 70 43 6c  ist(&pCache->pCl
f3760 65 61 6e 2c 20 70 29 3b 0a 20 20 20 20 20 20 20  ean, p);.       
f3770 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f   pcacheRemoveFro
f3780 6d 48 61 73 68 28 70 29 3b 0a 20 20 20 20 20 20  mHash(p);.      
f3790 20 20 70 63 61 63 68 65 50 61 67 65 46 72 65 65    pcachePageFree
f37a0 28 70 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  (p);.      }else
f37b0 7b 0a 20 20 20 20 20 20 20 20 70 63 61 63 68 65  {.        pcache
f37c0 41 64 64 54 6f 4c 72 75 4c 69 73 74 28 70 29 3b  AddToLruList(p);
f37d0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
f37e0 63 61 63 68 65 45 78 69 74 4d 75 74 65 78 28 29  cacheExitMutex()
f37f0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
f3800 20 20 20 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70     /* Move the p
f3810 61 67 65 20 74 6f 20 74 68 65 20 68 65 61 64 20  age to the head 
f3820 6f 66 20 74 68 65 20 63 61 63 68 65 73 20 64 69  of the caches di
f3830 72 74 79 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 20  rty list. */.   
f3840 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46     pcacheRemoveF
f3850 72 6f 6d 4c 69 73 74 28 26 70 43 61 63 68 65 2d  romList(&pCache-
f3860 3e 70 44 69 72 74 79 2c 20 70 29 3b 0a 20 20 20  >pDirty, p);.   
f3870 20 20 20 70 63 61 63 68 65 41 64 64 54 6f 4c 69     pcacheAddToLi
f3880 73 74 28 26 70 43 61 63 68 65 2d 3e 70 44 69 72  st(&pCache->pDir
f3890 74 79 2c 20 70 29 3b 0a 20 20 20 20 7d 0a 20 20  ty, p);.    }.  
f38a0 7d 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  }.}..SQLITE_PRIV
f38b0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
f38c0 50 63 61 63 68 65 52 65 66 28 50 67 48 64 72 20  PcacheRef(PgHdr 
f38d0 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 70 2d  *p){.  assert(p-
f38e0 3e 6e 52 65 66 3e 30 29 3b 0a 20 20 70 2d 3e 6e  >nRef>0);.  p->n
f38f0 52 65 66 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Ref++;.}../*.** 
f3900 44 72 6f 70 20 61 20 70 61 67 65 20 66 72 6f 6d  Drop a page from
f3910 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 65 72   the cache. Ther
f3920 65 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c  e must be exactl
f3930 79 20 6f 6e 65 20 72 65 66 65 72 65 6e 63 65 20  y one reference 
f3940 74 6f 20 74 68 65 0a 2a 2a 20 70 61 67 65 2e 20  to the.** page. 
f3950 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65  This function de
f3960 6c 65 74 65 73 20 74 68 61 74 20 72 65 66 65 72  letes that refer
f3970 65 6e 63 65 2c 20 73 6f 20 61 66 74 65 72 20 69  ence, so after i
f3980 74 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a  t returns the.**
f3990 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f   page pointed to
f39a0 20 62 79 20 70 20 69 73 20 69 6e 76 61 6c 69 64   by p is invalid
f39b0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
f39c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
f39d0 50 63 61 63 68 65 44 72 6f 70 28 50 67 48 64 72  PcacheDrop(PgHdr
f39e0 20 2a 70 29 7b 0a 20 20 50 43 61 63 68 65 20 2a   *p){.  PCache *
f39f0 70 43 61 63 68 65 3b 0a 20 20 61 73 73 65 72 74  pCache;.  assert
f3a00 28 20 70 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a  ( p->nRef==1 );.
f3a10 20 20 61 73 73 65 72 74 28 20 30 3d 3d 28 70 2d    assert( 0==(p-
f3a20 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52  >flags&PGHDR_DIR
f3a30 54 59 29 20 29 3b 0a 20 20 70 43 61 63 68 65 20  TY) );.  pCache 
f3a40 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20 70  = p->pCache;.  p
f3a50 43 61 63 68 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20  Cache->nRef--;. 
f3a60 20 70 43 61 63 68 65 2d 3e 6e 50 69 6e 6e 65 64   pCache->nPinned
f3a70 2d 2d 3b 0a 20 20 70 63 61 63 68 65 45 6e 74 65  --;.  pcacheEnte
f3a80 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61 63  rMutex();.  pcac
f3a90 68 65 52 65 6d 6f 76 65 46 72 6f 6d 4c 69 73 74  heRemoveFromList
f3aa0 28 26 70 43 61 63 68 65 2d 3e 70 43 6c 65 61 6e  (&pCache->pClean
f3ab0 2c 20 70 29 3b 0a 20 20 70 63 61 63 68 65 52 65  , p);.  pcacheRe
f3ac0 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 29 3b  moveFromHash(p);
f3ad0 0a 20 20 70 63 61 63 68 65 50 61 67 65 46 72 65  .  pcachePageFre
f3ae0 65 28 70 29 3b 0a 20 20 70 63 61 63 68 65 45 78  e(p);.  pcacheEx
f3af0 69 74 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a  itMutex();.}../*
f3b00 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68  .** Make sure th
f3b10 65 20 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64  e page is marked
f3b20 20 61 73 20 64 69 72 74 79 2e 20 20 49 66 20 69   as dirty.  If i
f3b30 74 20 69 73 6e 27 74 20 64 69 72 74 79 20 61 6c  t isn't dirty al
f3b40 72 65 61 64 79 2c 0a 2a 2a 20 6d 61 6b 65 20 69  ready,.** make i
f3b50 74 20 73 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  t so..*/.SQLITE_
f3b60 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
f3b70 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69  ite3PcacheMakeDi
f3b80 72 74 79 28 50 67 48 64 72 20 2a 70 29 7b 0a 20  rty(PgHdr *p){. 
f3b90 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 3b   PCache *pCache;
f3ba0 0a 20 20 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e  .  p->flags &= ~
f3bb0 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45  PGHDR_DONT_WRITE
f3bc0 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73  ;.  if( p->flags
f3bd0 20 26 20 50 47 48 44 52 5f 44 49 52 54 59 20 29   & PGHDR_DIRTY )
f3be0 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72   return;.  asser
f3bf0 74 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 50  t( (p->flags & P
f3c00 47 48 44 52 5f 44 49 52 54 59 29 3d 3d 30 20 29  GHDR_DIRTY)==0 )
f3c10 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e  ;.  assert( p->n
f3c20 52 65 66 3e 30 20 29 3b 0a 20 20 70 43 61 63 68  Ref>0 );.  pCach
f3c30 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20  e = p->pCache;. 
f3c40 20 70 63 61 63 68 65 45 6e 74 65 72 4d 75 74 65   pcacheEnterMute
f3c50 78 28 29 3b 0a 20 20 70 63 61 63 68 65 52 65 6d  x();.  pcacheRem
f3c60 6f 76 65 46 72 6f 6d 4c 69 73 74 28 26 70 43 61  oveFromList(&pCa
f3c70 63 68 65 2d 3e 70 43 6c 65 61 6e 2c 20 70 29 3b  che->pClean, p);
f3c80 0a 20 20 70 63 61 63 68 65 41 64 64 54 6f 4c 69  .  pcacheAddToLi
f3c90 73 74 28 26 70 43 61 63 68 65 2d 3e 70 44 69 72  st(&pCache->pDir
f3ca0 74 79 2c 20 70 29 3b 0a 20 20 70 63 61 63 68 65  ty, p);.  pcache
f3cb0 45 78 69 74 4d 75 74 65 78 28 29 3b 0a 20 20 70  ExitMutex();.  p
f3cc0 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52  ->flags |= PGHDR
f3cd0 5f 44 49 52 54 59 3b 0a 7d 0a 0a 73 74 61 74 69  _DIRTY;.}..stati
f3ce0 63 20 76 6f 69 64 20 70 63 61 63 68 65 4d 61 6b  c void pcacheMak
f3cf0 65 43 6c 65 61 6e 28 50 67 48 64 72 20 2a 70 29  eClean(PgHdr *p)
f3d00 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63  {.  PCache *pCac
f3d10 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a  he = p->pCache;.
f3d20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 66 6c 61    assert( p->fla
f3d30 67 73 20 26 20 50 47 48 44 52 5f 44 49 52 54 59  gs & PGHDR_DIRTY
f3d40 20 29 3b 0a 20 20 70 63 61 63 68 65 52 65 6d 6f   );.  pcacheRemo
f3d50 76 65 46 72 6f 6d 4c 69 73 74 28 26 70 43 61 63  veFromList(&pCac
f3d60 68 65 2d 3e 70 44 69 72 74 79 2c 20 70 29 3b 0a  he->pDirty, p);.
f3d70 20 20 70 63 61 63 68 65 41 64 64 54 6f 4c 69 73    pcacheAddToLis
f3d80 74 28 26 70 43 61 63 68 65 2d 3e 70 43 6c 65 61  t(&pCache->pClea
f3d90 6e 2c 20 70 29 3b 0a 20 20 70 2d 3e 66 6c 61 67  n, p);.  p->flag
f3da0 73 20 26 3d 20 7e 50 47 48 44 52 5f 44 49 52 54  s &= ~PGHDR_DIRT
f3db0 59 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 52 65 66  Y;.  if( p->nRef
f3dc0 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 61 63 68  ==0 ){.    pcach
f3dd0 65 41 64 64 54 6f 4c 72 75 4c 69 73 74 28 70 29  eAddToLruList(p)
f3de0 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 50  ;.    pCache->nP
f3df0 69 6e 6e 65 64 2d 2d 3b 0a 20 20 7d 0a 20 20 65  inned--;.  }.  e
f3e00 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28  xpensive_assert(
f3e10 20 70 43 61 63 68 65 2d 3e 6e 50 69 6e 6e 65 64   pCache->nPinned
f3e20 3d 3d 70 63 61 63 68 65 50 69 6e 6e 65 64 43 6f  ==pcachePinnedCo
f3e30 75 6e 74 28 70 43 61 63 68 65 29 20 29 3b 0a 7d  unt(pCache) );.}
f3e40 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72  ../*.** Make sur
f3e50 65 20 74 68 65 20 70 61 67 65 20 69 73 20 6d 61  e the page is ma
f3e60 72 6b 65 64 20 61 73 20 63 6c 65 61 6e 2e 20 20  rked as clean.  
f3e70 49 66 20 69 74 20 69 73 6e 27 74 20 63 6c 65 61  If it isn't clea
f3e80 6e 20 61 6c 72 65 61 64 79 2c 0a 2a 2a 20 6d 61  n already,.** ma
f3e90 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a 53 51 4c  ke it so..*/.SQL
f3ea0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
f3eb0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61   sqlite3PcacheMa
f3ec0 6b 65 43 6c 65 61 6e 28 50 67 48 64 72 20 2a 70  keClean(PgHdr *p
f3ed0 29 7b 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61  ){.  if( (p->fla
f3ee0 67 73 20 26 20 50 47 48 44 52 5f 44 49 52 54 59  gs & PGHDR_DIRTY
f3ef0 29 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 45  ) ){.    pcacheE
f3f00 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20  nterMutex();.   
f3f10 20 70 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e   pcacheMakeClean
f3f20 28 70 29 3b 0a 20 20 20 20 70 63 61 63 68 65 45  (p);.    pcacheE
f3f30 78 69 74 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a  xitMutex();.  }.
f3f40 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 65 76  }../*.** Make ev
f3f50 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20  ery page in the 
f3f60 63 61 63 68 65 20 63 6c 65 61 6e 2e 0a 2a 2f 0a  cache clean..*/.
f3f70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
f3f80 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
f3f90 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65  eCleanAll(PCache
f3fa0 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48   *pCache){.  PgH
f3fb0 64 72 20 2a 70 3b 0a 20 20 70 63 61 63 68 65 45  dr *p;.  pcacheE
f3fc0 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 77  nterMutex();.  w
f3fd0 68 69 6c 65 28 20 28 70 20 3d 20 70 43 61 63 68  hile( (p = pCach
f3fe0 65 2d 3e 70 44 69 72 74 79 29 21 3d 30 20 29 7b  e->pDirty)!=0 ){
f3ff0 0a 20 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76  .    pcacheRemov
f4000 65 46 72 6f 6d 4c 69 73 74 28 26 70 43 61 63 68  eFromList(&pCach
f4010 65 2d 3e 70 44 69 72 74 79 2c 20 70 29 3b 0a 20  e->pDirty, p);. 
f4020 20 20 20 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e     p->flags &= ~
f4030 50 47 48 44 52 5f 44 49 52 54 59 3b 0a 20 20 20  PGHDR_DIRTY;.   
f4040 20 70 63 61 63 68 65 41 64 64 54 6f 4c 69 73 74   pcacheAddToList
f4050 28 26 70 43 61 63 68 65 2d 3e 70 43 6c 65 61 6e  (&pCache->pClean
f4060 2c 20 70 29 3b 0a 20 20 20 20 69 66 28 20 70 2d  , p);.    if( p-
f4070 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20  >nRef==0 ){.    
f4080 20 20 70 63 61 63 68 65 41 64 64 54 6f 4c 72 75    pcacheAddToLru
f4090 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 20 20 70  List(p);.      p
f40a0 43 61 63 68 65 2d 3e 6e 50 69 6e 6e 65 64 2d 2d  Cache->nPinned--
f40b0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71  ;.    }.  }.  sq
f40c0 6c 69 74 65 33 50 63 61 63 68 65 41 73 73 65 72  lite3PcacheAsser
f40d0 74 46 6c 61 67 73 28 70 43 61 63 68 65 2c 20 30  tFlags(pCache, 0
f40e0 2c 20 50 47 48 44 52 5f 44 49 52 54 59 29 3b 0a  , PGHDR_DIRTY);.
f40f0 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65    expensive_asse
f4100 72 74 28 20 70 43 61 63 68 65 2d 3e 6e 50 69 6e  rt( pCache->nPin
f4110 6e 65 64 3d 3d 70 63 61 63 68 65 50 69 6e 6e 65  ned==pcachePinne
f4120 64 43 6f 75 6e 74 28 70 43 61 63 68 65 29 20 29  dCount(pCache) )
f4130 3b 0a 20 20 70 63 61 63 68 65 45 78 69 74 4d 75  ;.  pcacheExitMu
f4140 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  tex();.}../*.** 
f4150 43 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20  Change the page 
f4160 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 70  number of page p
f4170 20 74 6f 20 6e 65 77 50 67 6e 6f 2e 20 49 66 20   to newPgno. If 
f4180 6e 65 77 50 67 6e 6f 20 69 73 20 30 2c 20 74 68  newPgno is 0, th
f4190 65 6e 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 6f  en the.** page o
f41a0 62 6a 65 63 74 20 69 73 20 61 64 64 65 64 20 74  bject is added t
f41b0 6f 20 74 68 65 20 63 6c 65 61 6e 2d 6c 69 73 74  o the clean-list
f41c0 20 61 6e 64 20 74 68 65 20 50 47 48 44 52 5f 52   and the PGHDR_R
f41d0 45 55 53 45 5f 55 4e 4c 49 4b 45 4c 59 20 0a 2a  EUSE_UNLIKELY .*
f41e0 2a 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2f 0a 53  * flag set..*/.S
f41f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
f4200 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
f4210 4d 6f 76 65 28 50 67 48 64 72 20 2a 70 2c 20 50  Move(PgHdr *p, P
f4220 67 6e 6f 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 20  gno newPgno){.  
f4230 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e  assert( p->nRef>
f4240 30 20 29 3b 0a 20 20 70 63 61 63 68 65 45 6e 74  0 );.  pcacheEnt
f4250 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61  erMutex();.  pca
f4260 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73  cheRemoveFromHas
f4270 68 28 70 29 3b 0a 20 20 70 2d 3e 70 67 6e 6f 20  h(p);.  p->pgno 
f4280 3d 20 6e 65 77 50 67 6e 6f 3b 0a 20 20 69 66 28  = newPgno;.  if(
f4290 20 6e 65 77 50 67 6e 6f 3d 3d 30 20 29 7b 0a 20   newPgno==0 ){. 
f42a0 20 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73     if( (p->flags
f42b0 20 26 20 50 47 48 44 52 5f 44 49 52 54 59 29 20   & PGHDR_DIRTY) 
f42c0 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 4d  ){.      pcacheM
f42d0 61 6b 65 43 6c 65 61 6e 28 70 29 3b 0a 20 20 20  akeClean(p);.   
f42e0 20 7d 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20   }.    p->flags 
f42f0 3d 20 50 47 48 44 52 5f 52 45 55 53 45 5f 55 4e  = PGHDR_REUSE_UN
f4300 4c 49 4b 45 4c 59 3b 0a 20 20 7d 0a 20 20 70 63  LIKELY;.  }.  pc
f4310 61 63 68 65 41 64 64 54 6f 48 61 73 68 28 70 29  acheAddToHash(p)
f4320 3b 0a 20 20 70 63 61 63 68 65 45 78 69 74 4d 75  ;.  pcacheExitMu
f4330 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  tex();.}../*.** 
f4340 52 65 6d 6f 76 65 20 61 6c 6c 20 63 6f 6e 74 65  Remove all conte
f4350 6e 74 20 66 72 6f 6d 20 61 20 70 61 67 65 20 63  nt from a page c
f4360 61 63 68 65 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ache.*/.static v
f4370 6f 69 64 20 70 63 61 63 68 65 43 6c 65 61 72 28  oid pcacheClear(
f4380 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b  PCache *pCache){
f4390 0a 20 20 50 67 48 64 72 20 2a 70 2c 20 2a 70 4e  .  PgHdr *p, *pN
f43a0 65 78 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ext;.  assert( s
f43b0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
f43c0 64 28 70 63 61 63 68 65 5f 67 2e 6d 75 74 65 78  d(pcache_g.mutex
f43d0 29 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 43 61  ) );.  for(p=pCa
f43e0 63 68 65 2d 3e 70 43 6c 65 61 6e 3b 20 70 3b 20  che->pClean; p; 
f43f0 70 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 4e  p=pNext){.    pN
f4400 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a  ext = p->pNext;.
f4410 20 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65      pcacheRemove
f4420 46 72 6f 6d 4c 72 75 4c 69 73 74 28 70 29 3b 0a  FromLruList(p);.
f4430 20 20 20 20 70 63 61 63 68 65 50 61 67 65 46 72      pcachePageFr
f4440 65 65 28 70 29 3b 0a 20 20 7d 0a 20 20 66 6f 72  ee(p);.  }.  for
f4450 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74  (p=pCache->pDirt
f4460 79 3b 20 70 3b 20 70 3d 70 4e 65 78 74 29 7b 0a  y; p; p=pNext){.
f4470 20 20 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e 70      pNext = p->p
f4480 4e 65 78 74 3b 0a 20 20 20 20 70 63 61 63 68 65  Next;.    pcache
f4490 50 61 67 65 46 72 65 65 28 70 29 3b 0a 20 20 7d  PageFree(p);.  }
f44a0 0a 20 20 70 43 61 63 68 65 2d 3e 70 43 6c 65 61  .  pCache->pClea
f44b0 6e 20 3d 20 30 3b 0a 20 20 70 43 61 63 68 65 2d  n = 0;.  pCache-
f44c0 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 70  >pDirty = 0;.  p
f44d0 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69  Cache->pDirtyTai
f44e0 6c 20 3d 20 30 3b 0a 20 20 70 43 61 63 68 65 2d  l = 0;.  pCache-
f44f0 3e 6e 50 61 67 65 20 3d 20 30 3b 0a 20 20 70 43  >nPage = 0;.  pC
f4500 61 63 68 65 2d 3e 6e 50 69 6e 6e 65 64 20 3d 20  ache->nPinned = 
f4510 30 3b 0a 20 20 6d 65 6d 73 65 74 28 70 43 61 63  0;.  memset(pCac
f4520 68 65 2d 3e 61 70 48 61 73 68 2c 20 30 2c 20 70  he->apHash, 0, p
f4530 43 61 63 68 65 2d 3e 6e 48 61 73 68 2a 73 69 7a  Cache->nHash*siz
f4540 65 6f 66 28 70 43 61 63 68 65 2d 3e 61 70 48 61  eof(pCache->apHa
f4550 73 68 5b 30 5d 29 29 3b 0a 7d 0a 0a 0a 2f 2a 0a  sh[0]));.}.../*.
f4560 2a 2a 20 44 72 6f 70 20 65 76 65 72 79 20 63 61  ** Drop every ca
f4570 63 68 65 20 65 6e 74 72 79 20 77 68 6f 73 65 20  che entry whose 
f4580 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 67  page number is g
f4590 72 65 61 74 65 72 20 74 68 61 6e 20 22 70 67 6e  reater than "pgn
f45a0 6f 22 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  o"..*/.SQLITE_PR
f45b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
f45c0 65 33 50 63 61 63 68 65 54 72 75 6e 63 61 74 65  e3PcacheTruncate
f45d0 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c  (PCache *pCache,
f45e0 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 50   Pgno pgno){.  P
f45f0 67 48 64 72 20 2a 70 2c 20 2a 70 4e 65 78 74 3b  gHdr *p, *pNext;
f4600 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79  .  PgHdr *pDirty
f4610 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74   = pCache->pDirt
f4620 79 3b 0a 20 20 70 63 61 63 68 65 45 6e 74 65 72  y;.  pcacheEnter
f4630 4d 75 74 65 78 28 29 3b 0a 20 20 66 6f 72 28 70  Mutex();.  for(p
f4640 3d 70 43 61 63 68 65 2d 3e 70 43 6c 65 61 6e 3b  =pCache->pClean;
f4650 20 70 7c 7c 70 44 69 72 74 79 3b 20 70 3d 70 4e   p||pDirty; p=pN
f4660 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 21 70  ext){.    if( !p
f4670 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 44   ){.      p = pD
f4680 69 72 74 79 3b 0a 20 20 20 20 20 20 70 44 69 72  irty;.      pDir
f4690 74 79 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  ty = 0;.    }.  
f46a0 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65    pNext = p->pNe
f46b0 78 74 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70  xt;.    if( p->p
f46c0 67 6e 6f 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20  gno>pgno ){.    
f46d0 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30    if( p->nRef==0
f46e0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 63 61 63   ){.        pcac
f46f0 68 65 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68  heRemoveFromHash
f4700 28 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  (p);.        if(
f4710 20 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f   p->flags&PGHDR_
f4720 44 49 52 54 59 20 29 7b 0a 20 20 20 20 20 20 20  DIRTY ){.       
f4730 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46     pcacheRemoveF
f4740 72 6f 6d 4c 69 73 74 28 26 70 43 61 63 68 65 2d  romList(&pCache-
f4750 3e 70 44 69 72 74 79 2c 20 70 29 3b 0a 20 20 20  >pDirty, p);.   
f4760 20 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e         pCache->n
f4770 50 69 6e 6e 65 64 2d 2d 3b 0a 20 20 20 20 20 20  Pinned--;.      
f4780 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
f4790 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46     pcacheRemoveF
f47a0 72 6f 6d 4c 69 73 74 28 26 70 43 61 63 68 65 2d  romList(&pCache-
f47b0 3e 70 43 6c 65 61 6e 2c 20 70 29 3b 0a 20 20 20  >pClean, p);.   
f47c0 20 20 20 20 20 20 20 70 63 61 63 68 65 52 65 6d         pcacheRem
f47d0 6f 76 65 46 72 6f 6d 4c 72 75 4c 69 73 74 28 70  oveFromLruList(p
f47e0 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
f47f0 20 20 20 20 20 70 63 61 63 68 65 50 61 67 65 46       pcachePageF
f4800 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 7d 65  ree(p);.      }e
f4810 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  lse{.        /* 
f4820 49 66 20 74 68 65 72 65 20 61 72 65 20 72 65 66  If there are ref
f4830 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70  erences to the p
f4840 61 67 65 2c 20 69 74 20 63 61 6e 6e 6f 74 20 62  age, it cannot b
f4850 65 20 66 72 65 65 64 2e 20 49 6e 20 74 68 69 73  e freed. In this
f4860 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 61 73 65  .        ** case
f4870 2c 20 7a 65 72 6f 20 74 68 65 20 70 61 67 65 20  , zero the page 
f4880 63 6f 6e 74 65 6e 74 20 69 6e 73 74 65 61 64 2e  content instead.
f4890 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
f48a0 20 20 20 20 6d 65 6d 73 65 74 28 70 2d 3e 70 44      memset(p->pD
f48b0 61 74 61 2c 20 30 2c 20 70 43 61 63 68 65 2d 3e  ata, 0, pCache->
f48c0 73 7a 50 61 67 65 29 3b 0a 20 20 20 20 20 20 7d  szPage);.      }
f48d0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 63 61  .    }.  }.  pca
f48e0 63 68 65 45 78 69 74 4d 75 74 65 78 28 29 3b 0a  cheExitMutex();.
f48f0 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 72  }../*.** If ther
f4900 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20  e are currently 
f4910 6d 6f 72 65 20 74 68 61 6e 20 70 63 61 63 68 65  more than pcache
f4920 2e 6e 4d 61 78 50 61 67 65 20 70 61 67 65 73 20  .nMaxPage pages 
f4930 61 6c 6c 6f 63 61 74 65 64 2c 20 74 72 79 0a 2a  allocated, try.*
f4940 2a 20 74 6f 20 72 65 63 79 63 6c 65 20 70 61 67  * to recycle pag
f4950 65 73 20 74 6f 20 72 65 64 75 63 65 20 74 68 65  es to reduce the
f4960 20 6e 75 6d 62 65 72 20 61 6c 6c 6f 63 61 74 65   number allocate
f4970 64 20 74 6f 20 70 63 61 63 68 65 2e 6e 4d 61 78  d to pcache.nMax
f4980 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  Page..*/.static 
f4990 76 6f 69 64 20 70 63 61 63 68 65 45 6e 66 6f 72  void pcacheEnfor
f49a0 63 65 4d 61 78 50 61 67 65 28 76 6f 69 64 29 7b  ceMaxPage(void){
f49b0 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 61  .  PgHdr *p;.  a
f49c0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
f49d0 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65  utex_held(pcache
f49e0 5f 67 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 77  _g.mutex) );.  w
f49f0 68 69 6c 65 28 20 70 63 61 63 68 65 5f 67 2e 6e  hile( pcache_g.n
f4a00 43 75 72 72 65 6e 74 50 61 67 65 3e 70 63 61 63  CurrentPage>pcac
f4a10 68 65 5f 67 2e 6e 4d 61 78 50 61 67 65 20 26 26  he_g.nMaxPage &&
f4a20 20 28 70 20 3d 20 70 63 61 63 68 65 52 65 63 79   (p = pcacheRecy
f4a30 63 6c 65 50 61 67 65 28 29 29 20 29 7b 0a 20 20  clePage()) ){.  
f4a40 20 20 70 63 61 63 68 65 50 61 67 65 46 72 65 65    pcachePageFree
f4a50 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  (p);.  }.}../*.*
f4a60 2a 20 43 6c 6f 73 65 20 61 20 63 61 63 68 65 2e  * Close a cache.
f4a70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
f4a80 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
f4a90 63 61 63 68 65 43 6c 6f 73 65 28 50 43 61 63 68  cacheClose(PCach
f4aa0 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 70 63  e *pCache){.  pc
f4ab0 61 63 68 65 45 6e 74 65 72 4d 75 74 65 78 28 29  acheEnterMutex()
f4ac0 3b 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6c 6c  ;..  /* Free all
f4ad0 20 74 68 65 20 70 61 67 65 73 20 75 73 65 64 20   the pages used 
f4ae0 62 79 20 74 68 69 73 20 70 61 67 65 72 20 61 6e  by this pager an
f4af0 64 20 72 65 6d 6f 76 65 20 74 68 65 6d 20 66 72  d remove them fr
f4b00 6f 6d 20 74 68 65 20 4c 52 55 20 6c 69 73 74 2e  om the LRU list.
f4b10 20 2a 2f 0a 20 20 70 63 61 63 68 65 43 6c 65 61   */.  pcacheClea
f4b20 72 28 70 43 61 63 68 65 29 3b 0a 20 20 69 66 28  r(pCache);.  if(
f4b30 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61   pCache->bPurgea
f4b40 62 6c 65 20 29 7b 0a 20 20 20 20 70 63 61 63 68  ble ){.    pcach
f4b50 65 5f 67 2e 6e 4d 61 78 50 61 67 65 20 2d 3d 20  e_g.nMaxPage -= 
f4b60 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a 20 20  pCache->nMax;.  
f4b70 20 20 70 63 61 63 68 65 5f 67 2e 6e 4d 69 6e 50    pcache_g.nMinP
f4b80 61 67 65 20 2d 3d 20 70 43 61 63 68 65 2d 3e 6e  age -= pCache->n
f4b90 4d 69 6e 3b 0a 20 20 20 20 70 63 61 63 68 65 45  Min;.    pcacheE
f4ba0 6e 66 6f 72 63 65 4d 61 78 50 61 67 65 28 29 3b  nforceMaxPage();
f4bb0 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66  .  }.  sqlite3_f
f4bc0 72 65 65 28 70 43 61 63 68 65 2d 3e 61 70 48 61  ree(pCache->apHa
f4bd0 73 68 29 3b 0a 20 20 70 63 61 63 68 65 45 78 69  sh);.  pcacheExi
f4be0 74 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 0a 23 69  tMutex();.}...#i
f4bf0 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 20  fndef NDEBUG./* 
f4c00 0a 2a 2a 20 41 73 73 65 72 74 20 66 6c 61 67 73  .** Assert flags
f4c10 20 73 65 74 74 69 6e 67 73 20 6f 6e 20 61 6c 6c   settings on all
f4c20 20 70 61 67 65 73 2e 20 20 44 65 62 75 67 67 69   pages.  Debuggi
f4c30 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49  ng only..*/.SQLI
f4c40 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
f4c50 73 71 6c 69 74 65 33 50 63 61 63 68 65 41 73 73  sqlite3PcacheAss
f4c60 65 72 74 46 6c 61 67 73 28 50 43 61 63 68 65 20  ertFlags(PCache 
f4c70 2a 70 43 61 63 68 65 2c 20 69 6e 74 20 74 72 75  *pCache, int tru
f4c80 65 4d 61 73 6b 2c 20 69 6e 74 20 66 61 6c 73 65  eMask, int false
f4c90 4d 61 73 6b 29 7b 0a 20 20 50 67 48 64 72 20 2a  Mask){.  PgHdr *
f4ca0 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 61 63 68  p;.  for(p=pCach
f4cb0 65 2d 3e 70 44 69 72 74 79 3b 20 70 3b 20 70 3d  e->pDirty; p; p=
f4cc0 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61  p->pNext){.    a
f4cd0 73 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 67 73  ssert( (p->flags
f4ce0 26 74 72 75 65 4d 61 73 6b 29 3d 3d 74 72 75 65  &trueMask)==true
f4cf0 4d 61 73 6b 20 29 3b 0a 20 20 20 20 61 73 73 65  Mask );.    asse
f4d00 72 74 28 20 28 70 2d 3e 66 6c 61 67 73 26 66 61  rt( (p->flags&fa
f4d10 6c 73 65 4d 61 73 6b 29 3d 3d 30 20 29 3b 0a 20  lseMask)==0 );. 
f4d20 20 7d 0a 20 20 66 6f 72 28 70 3d 70 43 61 63 68   }.  for(p=pCach
f4d30 65 2d 3e 70 43 6c 65 61 6e 3b 20 70 3b 20 70 3d  e->pClean; p; p=
f4d40 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61  p->pNext){.    a
f4d50 73 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 67 73  ssert( (p->flags
f4d60 26 74 72 75 65 4d 61 73 6b 29 3d 3d 74 72 75 65  &trueMask)==true
f4d70 4d 61 73 6b 20 29 3b 0a 20 20 20 20 61 73 73 65  Mask );.    asse
f4d80 72 74 28 20 28 70 2d 3e 66 6c 61 67 73 26 66 61  rt( (p->flags&fa
f4d90 6c 73 65 4d 61 73 6b 29 3d 3d 30 20 29 3b 0a 20  lseMask)==0 );. 
f4da0 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20   }.}.#endif../* 
f4db0 0a 2a 2a 20 44 69 73 63 61 72 64 20 74 68 65 20  .** Discard the 
f4dc0 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
f4dd0 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cache..*/.SQLITE
f4de0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
f4df0 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 28  ite3PcacheClear(
f4e00 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b  PCache *pCache){
f4e10 0a 20 20 61 73 73 65 72 74 28 70 43 61 63 68 65  .  assert(pCache
f4e20 2d 3e 6e 52 65 66 3d 3d 30 29 3b 0a 20 20 70 63  ->nRef==0);.  pc
f4e30 61 63 68 65 45 6e 74 65 72 4d 75 74 65 78 28 29  acheEnterMutex()
f4e40 3b 0a 20 20 70 63 61 63 68 65 43 6c 65 61 72 28  ;.  pcacheClear(
f4e50 70 43 61 63 68 65 29 3b 0a 20 20 70 63 61 63 68  pCache);.  pcach
f4e60 65 45 78 69 74 4d 75 74 65 78 28 29 3b 0a 20 20  eExitMutex();.  
f4e70 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
f4e80 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65  ;.}../*.** Merge
f4e90 20 74 77 6f 20 6c 69 73 74 73 20 6f 66 20 70 61   two lists of pa
f4ea0 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79  ges connected by
f4eb0 20 70 44 69 72 74 79 20 61 6e 64 20 69 6e 20 70   pDirty and in p
f4ec0 67 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a 20 44 6f  gno order..** Do
f4ed0 20 6e 6f 74 20 62 6f 74 68 20 66 69 78 69 6e 67   not both fixing
f4ee0 20 74 68 65 20 70 50 72 65 76 44 69 72 74 79 20   the pPrevDirty 
f4ef0 70 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a 73 74 61  pointers..*/.sta
f4f00 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 63 68  tic PgHdr *pcach
f4f10 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73 74 28  eMergeDirtyList(
f4f20 50 67 48 64 72 20 2a 70 41 2c 20 50 67 48 64 72  PgHdr *pA, PgHdr
f4f30 20 2a 70 42 29 7b 0a 20 20 50 67 48 64 72 20 72   *pB){.  PgHdr r
f4f40 65 73 75 6c 74 2c 20 2a 70 54 61 69 6c 3b 0a 20  esult, *pTail;. 
f4f50 20 70 54 61 69 6c 20 3d 20 26 72 65 73 75 6c 74   pTail = &result
f4f60 3b 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26 26  ;.  while( pA &&
f4f70 20 70 42 20 29 7b 0a 20 20 20 20 69 66 28 20 70   pB ){.    if( p
f4f80 41 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67 6e 6f  A->pgno<pB->pgno
f4f90 20 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d   ){.      pTail-
f4fa0 3e 70 44 69 72 74 79 20 3d 20 70 41 3b 0a 20 20  >pDirty = pA;.  
f4fb0 20 20 20 20 70 54 61 69 6c 20 3d 20 70 41 3b 0a      pTail = pA;.
f4fc0 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70        pA = pA->p
f4fd0 44 69 72 74 79 3b 0a 20 20 20 20 7d 65 6c 73 65  Dirty;.    }else
f4fe0 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70  {.      pTail->p
f4ff0 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 20 20  Dirty = pB;.    
f5000 20 20 70 54 61 69 6c 20 3d 20 70 42 3b 0a 20 20    pTail = pB;.  
f5010 20 20 20 20 70 42 20 3d 20 70 42 2d 3e 70 44 69      pB = pB->pDi
f5020 72 74 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  rty;.    }.  }. 
f5030 20 69 66 28 20 70 41 20 29 7b 0a 20 20 20 20 70   if( pA ){.    p
f5040 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70  Tail->pDirty = p
f5050 41 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  A;.  }else if( p
f5060 42 20 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e  B ){.    pTail->
f5070 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 7d  pDirty = pB;.  }
f5080 65 6c 73 65 7b 0a 20 20 20 20 70 54 61 69 6c 2d  else{.    pTail-
f5090 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 7d  >pDirty = 0;.  }
f50a0 0a 20 20 72 65 74 75 72 6e 20 72 65 73 75 6c 74  .  return result
f50b0 2e 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a  .pDirty;.}../*.*
f50c0 2a 20 53 6f 72 74 20 74 68 65 20 6c 69 73 74 20  * Sort the list 
f50d0 6f 66 20 70 61 67 65 73 20 69 6e 20 61 63 63 65  of pages in acce
f50e0 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70  nding order by p
f50f0 67 6e 6f 2e 20 20 50 61 67 65 73 20 61 72 65 0a  gno.  Pages are.
f5100 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20  ** connected by 
f5110 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72 73 2e  pDirty pointers.
f5120 20 20 54 68 65 20 70 50 72 65 76 44 69 72 74 79    The pPrevDirty
f5130 20 70 6f 69 6e 74 65 72 73 20 61 72 65 0a 2a 2a   pointers are.**
f5140 20 63 6f 72 72 75 70 74 65 64 20 62 79 20 74 68   corrupted by th
f5150 69 73 20 73 6f 72 74 2e 0a 2a 2f 0a 23 64 65 66  is sort..*/.#def
f5160 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b 45  ine N_SORT_BUCKE
f5170 54 5f 41 4c 4c 4f 43 20 32 35 0a 23 64 65 66 69  T_ALLOC 25.#defi
f5180 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54  ne N_SORT_BUCKET
f5190 20 20 20 20 20 20 20 32 35 0a 23 69 66 64 65 66         25.#ifdef
f51a0 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69   SQLITE_TEST.  i
f51b0 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72  nt sqlite3_pager
f51c0 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b 65 74 20 3d  _n_sort_bucket =
f51d0 20 30 3b 0a 20 20 23 75 6e 64 65 66 20 4e 5f 53   0;.  #undef N_S
f51e0 4f 52 54 5f 42 55 43 4b 45 54 0a 20 20 23 64 65  ORT_BUCKET.  #de
f51f0 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b  fine N_SORT_BUCK
f5200 45 54 20 5c 0a 20 20 20 28 73 71 6c 69 74 65 33  ET \.   (sqlite3
f5210 5f 70 61 67 65 72 5f 6e 5f 73 6f 72 74 5f 62 75  _pager_n_sort_bu
f5220 63 6b 65 74 3f 73 71 6c 69 74 65 33 5f 70 61 67  cket?sqlite3_pag
f5230 65 72 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b 65 74  er_n_sort_bucket
f5240 3a 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 5f 41  :N_SORT_BUCKET_A
f5250 4c 4c 4f 43 29 0a 23 65 6e 64 69 66 0a 73 74 61  LLOC).#endif.sta
f5260 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 63 68  tic PgHdr *pcach
f5270 65 53 6f 72 74 44 69 72 74 79 4c 69 73 74 28 50  eSortDirtyList(P
f5280 67 48 64 72 20 2a 70 49 6e 29 7b 0a 20 20 50 67  gHdr *pIn){.  Pg
f5290 48 64 72 20 2a 61 5b 4e 5f 53 4f 52 54 5f 42 55  Hdr *a[N_SORT_BU
f52a0 43 4b 45 54 5f 41 4c 4c 4f 43 5d 2c 20 2a 70 3b  CKET_ALLOC], *p;
f52b0 0a 20 20 69 6e 74 20 69 3b 0a 20 20 6d 65 6d 73  .  int i;.  mems
f52c0 65 74 28 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28  et(a, 0, sizeof(
f52d0 61 29 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 49  a));.  while( pI
f52e0 6e 20 29 7b 0a 20 20 20 20 70 20 3d 20 70 49 6e  n ){.    p = pIn
f52f0 3b 0a 20 20 20 20 70 49 6e 20 3d 20 70 2d 3e 70  ;.    pIn = p->p
f5300 44 69 72 74 79 3b 0a 20 20 20 20 70 2d 3e 70 44  Dirty;.    p->pD
f5310 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 66 6f  irty = 0;.    fo
f5320 72 28 69 3d 30 3b 20 69 3c 4e 5f 53 4f 52 54 5f  r(i=0; i<N_SORT_
f5330 42 55 43 4b 45 54 2d 31 3b 20 69 2b 2b 29 7b 0a  BUCKET-1; i++){.
f5340 20 20 20 20 20 20 69 66 28 20 61 5b 69 5d 3d 3d        if( a[i]==
f5350 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 5b 69  0 ){.        a[i
f5360 5d 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 62  ] = p;.        b
f5370 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c 73  reak;.      }els
f5380 65 7b 0a 20 20 20 20 20 20 20 20 70 20 3d 20 70  e{.        p = p
f5390 63 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c  cacheMergeDirtyL
f53a0 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b 0a 20 20  ist(a[i], p);.  
f53b0 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 30 3b 0a        a[i] = 0;.
f53c0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
f53d0 20 20 69 66 28 20 69 3d 3d 4e 5f 53 4f 52 54 5f    if( i==N_SORT_
f53e0 42 55 43 4b 45 54 2d 31 20 29 7b 0a 20 20 20 20  BUCKET-1 ){.    
f53f0 20 20 2f 2a 20 43 6f 76 65 72 61 67 65 3a 20 54    /* Coverage: T
f5400 6f 20 67 65 74 20 68 65 72 65 2c 20 74 68 65 72  o get here, ther
f5410 65 20 6e 65 65 64 20 74 6f 20 62 65 20 32 5e 28  e need to be 2^(
f5420 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 29 20 0a  N_SORT_BUCKET) .
f5430 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74        ** element
f5440 73 20 69 6e 20 74 68 65 20 69 6e 70 75 74 20 6c  s in the input l
f5450 69 73 74 2e 20 54 68 69 73 20 69 73 20 70 6f 73  ist. This is pos
f5460 73 69 62 6c 65 2c 20 62 75 74 20 69 6d 70 72 61  sible, but impra
f5470 63 74 69 63 61 6c 2e 0a 20 20 20 20 20 20 2a 2a  ctical..      **
f5480 20 54 65 73 74 69 6e 67 20 74 68 69 73 20 6c 69   Testing this li
f5490 6e 65 20 69 73 20 74 68 65 20 70 6f 69 6e 74 20  ne is the point 
f54a0 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62  of global variab
f54b0 6c 65 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69  le.      ** sqli
f54c0 74 65 33 5f 70 61 67 65 72 5f 6e 5f 73 6f 72 74  te3_pager_n_sort
f54d0 5f 62 75 63 6b 65 74 2e 0a 20 20 20 20 20 20 2a  _bucket..      *
f54e0 2f 0a 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 70  /.      a[i] = p
f54f0 63 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c  cacheMergeDirtyL
f5500 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b 0a 20 20  ist(a[i], p);.  
f5510 20 20 7d 0a 20 20 7d 0a 20 20 70 20 3d 20 61 5b    }.  }.  p = a[
f5520 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69  0];.  for(i=1; i
f5530 3c 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 3b 20  <N_SORT_BUCKET; 
f5540 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 70 63  i++){.    p = pc
f5550 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69  acheMergeDirtyLi
f5560 73 74 28 70 2c 20 61 5b 69 5d 29 3b 0a 20 20 7d  st(p, a[i]);.  }
f5570 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
f5580 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 6c  /*.** Return a l
f5590 69 73 74 20 6f 66 20 61 6c 6c 20 64 69 72 74 79  ist of all dirty
f55a0 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 63 61   pages in the ca
f55b0 63 68 65 2c 20 73 6f 72 74 65 64 20 62 79 20 70  che, sorted by p
f55c0 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 53  age number..*/.S
f55d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50 67  QLITE_PRIVATE Pg
f55e0 48 64 72 20 2a 73 71 6c 69 74 65 33 50 63 61 63  Hdr *sqlite3Pcac
f55f0 68 65 44 69 72 74 79 4c 69 73 74 28 50 43 61 63  heDirtyList(PCac
f5600 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50  he *pCache){.  P
f5610 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70  gHdr *p;.  for(p
f5620 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b  =pCache->pDirty;
f5630 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b   p; p=p->pNext){
f5640 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79 20 3d  .    p->pDirty =
f5650 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20   p->pNext;.  }. 
f5660 20 72 65 74 75 72 6e 20 70 63 61 63 68 65 53 6f   return pcacheSo
f5670 72 74 44 69 72 74 79 4c 69 73 74 28 70 43 61 63  rtDirtyList(pCac
f5680 68 65 2d 3e 70 44 69 72 74 79 29 3b 0a 7d 0a 0a  he->pDirty);.}..
f5690 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  /* .** Return th
f56a0 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
f56b0 66 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61  f outstanding pa
f56c0 67 65 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a  ge references..*
f56d0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
f56e0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63   int sqlite3Pcac
f56f0 68 65 52 65 66 43 6f 75 6e 74 28 50 43 61 63 68  heRefCount(PCach
f5700 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 72 65  e *pCache){.  re
f5710 74 75 72 6e 20 70 43 61 63 68 65 2d 3e 6e 52 65  turn pCache->nRe
f5720 66 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49  f;.}..SQLITE_PRI
f5730 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
f5740 50 63 61 63 68 65 50 61 67 65 52 65 66 63 6f 75  PcachePageRefcou
f5750 6e 74 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20  nt(PgHdr *p){.  
f5760 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3b 0a  return p->nRef;.
f5770 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e  }../* .** Return
f5780 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65   the total numbe
f5790 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68  r of pages in th
f57a0 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49  e cache..*/.SQLI
f57b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
f57c0 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65  qlite3PcachePage
f57d0 63 6f 75 6e 74 28 50 43 61 63 68 65 20 2a 70 43  count(PCache *pC
f57e0 61 63 68 65 29 7b 0a 20 20 61 73 73 65 72 74 28  ache){.  assert(
f57f0 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 3e 3d   pCache->nPage>=
f5800 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 43  0 );.  return pC
f5810 61 63 68 65 2d 3e 6e 50 61 67 65 3b 0a 7d 0a 0a  ache->nPage;.}..
f5820 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48  #ifdef SQLITE_CH
f5830 45 43 4b 5f 50 41 47 45 53 0a 2f 2a 0a 2a 2a 20  ECK_PAGES./*.** 
f5840 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
f5850 20 75 73 65 64 20 62 79 20 74 68 65 20 70 61 67   used by the pag
f5860 65 72 2e 63 20 6d 6f 64 75 6c 65 20 74 6f 20 69  er.c module to i
f5870 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 61  terate through a
f5880 6c 6c 20 0a 2a 2a 20 70 61 67 65 73 20 69 6e 20  ll .** pages in 
f5890 74 68 65 20 63 61 63 68 65 2e 20 41 74 20 70 72  the cache. At pr
f58a0 65 73 65 6e 74 2c 20 74 68 69 73 20 69 73 20 6f  esent, this is o
f58b0 6e 6c 79 20 72 65 71 75 69 72 65 64 20 69 66 20  nly required if 
f58c0 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 48  the.** SQLITE_CH
f58d0 45 43 4b 5f 50 41 47 45 53 20 6d 61 63 72 6f 20  ECK_PAGES macro 
f58e0 28 75 73 65 64 20 66 6f 72 20 64 65 62 75 67 67  (used for debugg
f58f0 69 6e 67 29 20 69 73 20 73 70 65 63 69 66 69 65  ing) is specifie
f5900 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
f5910 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
f5920 33 50 63 61 63 68 65 49 74 65 72 61 74 65 28 50  3PcacheIterate(P
f5930 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76  Cache *pCache, v
f5940 6f 69 64 20 28 2a 78 49 74 65 72 29 28 50 67 48  oid (*xIter)(PgH
f5950 64 72 20 2a 29 29 7b 0a 20 20 50 67 48 64 72 20  dr *)){.  PgHdr 
f5960 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 61 63  *p;.  for(p=pCac
f5970 68 65 2d 3e 70 43 6c 65 61 6e 3b 20 70 3b 20 70  he->pClean; p; p
f5980 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  =p->pNext){.    
f5990 78 49 74 65 72 28 70 29 3b 0a 20 20 7d 0a 20 20  xIter(p);.  }.  
f59a0 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44  for(p=pCache->pD
f59b0 69 72 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e  irty; p; p=p->pN
f59c0 65 78 74 29 7b 0a 20 20 20 20 78 49 74 65 72 28  ext){.    xIter(
f59d0 70 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66  p);.  }.}.#endif
f59e0 0a 0a 2f 2a 20 0a 2a 2a 20 53 65 74 20 66 6c 61  ../* .** Set fla
f59f0 67 73 20 6f 6e 20 61 6c 6c 20 70 61 67 65 73 20  gs on all pages 
f5a00 69 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68  in the page cach
f5a10 65 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e .*/.SQLITE_PRI
f5a20 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
f5a30 33 50 63 61 63 68 65 43 6c 65 61 72 46 6c 61 67  3PcacheClearFlag
f5a40 73 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65  s(PCache *pCache
f5a50 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a 20 20 50  , int mask){.  P
f5a60 67 48 64 72 20 2a 70 3b 0a 0a 20 20 2f 2a 20 4f  gHdr *p;..  /* O
f5a70 62 74 61 69 6e 20 74 68 65 20 67 6c 6f 62 61 6c  btain the global
f5a80 20 6d 75 74 65 78 20 62 65 66 6f 72 65 20 6d 6f   mutex before mo
f5a90 64 69 66 79 69 6e 67 20 61 6e 79 20 50 67 48 64  difying any PgHd
f5aa0 72 2e 66 6c 61 67 73 20 76 61 72 69 61 62 6c 65  r.flags variable
f5ab0 73 20 0a 20 20 2a 2a 20 6f 72 20 74 72 61 76 65  s .  ** or trave
f5ac0 72 73 69 6e 67 20 74 68 65 20 4c 52 55 20 6c 69  rsing the LRU li
f5ad0 73 74 2e 0a 20 20 2a 2f 20 0a 20 20 70 63 61 63  st..  */ .  pcac
f5ae0 68 65 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a  heEnterMutex();.
f5af0 0a 20 20 6d 61 73 6b 20 3d 20 7e 6d 61 73 6b 3b  .  mask = ~mask;
f5b00 0a 20 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d  .  for(p=pCache-
f5b10 3e 70 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 2d  >pDirty; p; p=p-
f5b20 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e  >pNext){.    p->
f5b30 66 6c 61 67 73 20 26 3d 20 6d 61 73 6b 3b 0a 20  flags &= mask;. 
f5b40 20 7d 0a 20 20 66 6f 72 28 70 3d 70 43 61 63 68   }.  for(p=pCach
f5b50 65 2d 3e 70 43 6c 65 61 6e 3b 20 70 3b 20 70 3d  e->pClean; p; p=
f5b60 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 70  p->pNext){.    p
f5b70 2d 3e 66 6c 61 67 73 20 26 3d 20 6d 61 73 6b 3b  ->flags &= mask;
f5b80 0a 20 20 7d 0a 0a 20 20 69 66 28 20 30 3d 3d 28  .  }..  if( 0==(
f5b90 6d 61 73 6b 26 50 47 48 44 52 5f 4e 45 45 44 5f  mask&PGHDR_NEED_
f5ba0 53 59 4e 43 29 20 29 7b 0a 20 20 20 20 70 43 61  SYNC) ){.    pCa
f5bb0 63 68 65 2d 3e 70 53 79 6e 63 65 64 20 3d 20 70  che->pSynced = p
f5bc0 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69  Cache->pDirtyTai
f5bd0 6c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  l;.    assert( !
f5be0 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 20  pCache->pSynced 
f5bf0 7c 7c 20 28 70 43 61 63 68 65 2d 3e 70 53 79 6e  || (pCache->pSyn
f5c00 63 65 64 2d 3e 66 6c 61 67 73 26 50 47 48 44 52  ced->flags&PGHDR
f5c10 5f 4e 45 45 44 5f 53 59 4e 43 29 3d 3d 30 20 29  _NEED_SYNC)==0 )
f5c20 3b 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 45  ;.  }..  pcacheE
f5c30 78 69 74 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f  xitMutex();.}../
f5c40 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 73 75 67  *.** Set the sug
f5c50 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a  gested cache-siz
f5c60 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49  e value..*/.SQLI
f5c70 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
f5c80 71 6c 69 74 65 33 50 63 61 63 68 65 47 65 74 43  qlite3PcacheGetC
f5c90 61 63 68 65 73 69 7a 65 28 50 43 61 63 68 65 20  achesize(PCache 
f5ca0 2a 70 43 61 63 68 65 29 7b 0a 20 20 72 65 74 75  *pCache){.  retu
f5cb0 72 6e 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b  rn pCache->nMax;
f5cc0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
f5cd0 65 20 73 75 67 67 65 73 74 65 64 20 63 61 63 68  e suggested cach
f5ce0 65 2d 73 69 7a 65 20 76 61 6c 75 65 2e 0a 2a 2f  e-size value..*/
f5cf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
f5d00 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
f5d10 68 65 53 65 74 43 61 63 68 65 73 69 7a 65 28 50  heSetCachesize(P
f5d20 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 69  Cache *pCache, i
f5d30 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 69 66  nt mxPage){.  if
f5d40 28 20 6d 78 50 61 67 65 3c 31 30 20 29 7b 0a 20  ( mxPage<10 ){. 
f5d50 20 20 20 6d 78 50 61 67 65 20 3d 20 31 30 3b 0a     mxPage = 10;.
f5d60 20 20 7d 0a 20 20 69 66 28 20 70 43 61 63 68 65    }.  if( pCache
f5d70 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0a  ->bPurgeable ){.
f5d80 20 20 20 20 70 63 61 63 68 65 45 6e 74 65 72 4d      pcacheEnterM
f5d90 75 74 65 78 28 29 3b 0a 20 20 20 20 70 63 61 63  utex();.    pcac
f5da0 68 65 5f 67 2e 6e 4d 61 78 50 61 67 65 20 2d 3d  he_g.nMaxPage -=
f5db0 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a 20   pCache->nMax;. 
f5dc0 20 20 20 70 63 61 63 68 65 5f 67 2e 6e 4d 61 78     pcache_g.nMax
f5dd0 50 61 67 65 20 2b 3d 20 6d 78 50 61 67 65 3b 0a  Page += mxPage;.
f5de0 20 20 20 20 70 63 61 63 68 65 45 6e 66 6f 72 63      pcacheEnforc
f5df0 65 4d 61 78 50 61 67 65 28 29 3b 0a 20 20 20 20  eMaxPage();.    
f5e00 70 63 61 63 68 65 45 78 69 74 4d 75 74 65 78 28  pcacheExitMutex(
f5e10 29 3b 0a 20 20 7d 0a 20 20 70 43 61 63 68 65 2d  );.  }.  pCache-
f5e20 3e 6e 4d 61 78 20 3d 20 6d 78 50 61 67 65 3b 0a  >nMax = mxPage;.
f5e30 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
f5e40 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d  _ENABLE_MEMORY_M
f5e50 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20  ANAGEMENT./*.** 
f5e60 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
f5e70 20 63 61 6c 6c 65 64 20 74 6f 20 66 72 65 65 20   called to free 
f5e80 73 75 70 65 72 66 6c 75 6f 75 73 20 64 79 6e 61  superfluous dyna
f5e90 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65  mically allocate
f5ea0 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 65 6c 64  d memory.** held
f5eb0 20 62 79 20 74 68 65 20 70 61 67 65 72 20 73 79   by the pager sy
f5ec0 73 74 65 6d 2e 20 4d 65 6d 6f 72 79 20 69 6e 20  stem. Memory in 
f5ed0 75 73 65 20 62 79 20 61 6e 79 20 53 51 4c 69 74  use by any SQLit
f5ee0 65 20 70 61 67 65 72 20 61 6c 6c 6f 63 61 74 65  e pager allocate
f5ef0 64 0a 2a 2a 20 62 79 20 74 68 65 20 63 75 72 72  d.** by the curr
f5f00 65 6e 74 20 74 68 72 65 61 64 20 6d 61 79 20 62  ent thread may b
f5f10 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  e sqlite3_free()
f5f20 65 64 2e 0a 2a 2a 0a 2a 2a 20 6e 52 65 71 20 69  ed..**.** nReq i
f5f30 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
f5f40 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20  bytes of memory 
f5f50 72 65 71 75 69 72 65 64 2e 20 4f 6e 63 65 20 74  required. Once t
f5f60 68 69 73 20 6d 75 63 68 20 68 61 73 0a 2a 2a 20  his much has.** 
f5f70 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2c 20 74  been released, t
f5f80 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  he function retu
f5f90 72 6e 73 2e 20 54 68 65 20 72 65 74 75 72 6e 20  rns. The return 
f5fa0 76 61 6c 75 65 20 69 73 20 74 68 65 20 74 6f 74  value is the tot
f5fb0 61 6c 20 6e 75 6d 62 65 72 20 0a 2a 2a 20 6f 66  al number .** of
f5fc0 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79   bytes of memory
f5fd0 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2f 0a 53 51   released..*/.SQ
f5fe0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
f5ff0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65   sqlite3PcacheRe
f6000 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e 74 20  leaseMemory(int 
f6010 6e 52 65 71 29 7b 0a 20 20 69 6e 74 20 6e 46 72  nReq){.  int nFr
f6020 65 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 63  ee = 0;.  if( pc
f6030 61 63 68 65 5f 67 2e 70 53 74 61 72 74 3d 3d 30  ache_g.pStart==0
f6040 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70   ){.    PgHdr *p
f6050 3b 0a 20 20 20 20 70 63 61 63 68 65 45 6e 74 65  ;.    pcacheEnte
f6060 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 77 68  rMutex();.    wh
f6070 69 6c 65 28 20 28 6e 52 65 71 3c 30 20 7c 7c 20  ile( (nReq<0 || 
f6080 6e 46 72 65 65 3c 6e 52 65 71 29 20 26 26 20 28  nFree<nReq) && (
f6090 70 3d 70 63 61 63 68 65 52 65 63 79 63 6c 65 50  p=pcacheRecycleP
f60a0 61 67 65 28 29 29 20 29 7b 0a 20 20 20 20 20 20  age()) ){.      
f60b0 6e 46 72 65 65 20 2b 3d 20 70 63 61 63 68 65 50  nFree += pcacheP
f60c0 61 67 65 53 69 7a 65 28 70 29 3b 0a 20 20 20 20  ageSize(p);.    
f60d0 20 20 70 63 61 63 68 65 50 61 67 65 46 72 65 65    pcachePageFree
f60e0 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  (p);.    }.    p
f60f0 63 61 63 68 65 45 78 69 74 4d 75 74 65 78 28 29  cacheExitMutex()
f6100 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e  ;.  }.  return n
f6110 46 72 65 65 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  Free;.}.#endif /
f6120 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  * SQLITE_ENABLE_
f6130 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e  MEMORY_MANAGEMEN
f6140 54 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c  T */..#ifdef SQL
f6150 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
f6160 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
f6170 69 74 65 33 50 63 61 63 68 65 53 74 61 74 73 28  ite3PcacheStats(
f6180 0a 20 20 69 6e 74 20 2a 70 6e 43 75 72 72 65 6e  .  int *pnCurren
f6190 74 2c 0a 20 20 69 6e 74 20 2a 70 6e 4d 61 78 2c  t,.  int *pnMax,
f61a0 0a 20 20 69 6e 74 20 2a 70 6e 4d 69 6e 2c 0a 20  .  int *pnMin,. 
f61b0 20 69 6e 74 20 2a 70 6e 52 65 63 79 63 6c 61 62   int *pnRecyclab
f61c0 6c 65 0a 29 7b 0a 20 20 50 67 48 64 72 20 2a 70  le.){.  PgHdr *p
f61d0 3b 0a 20 20 69 6e 74 20 6e 52 65 63 79 63 6c 61  ;.  int nRecycla
f61e0 62 6c 65 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70  ble = 0;.  for(p
f61f0 3d 70 63 61 63 68 65 5f 67 2e 70 4c 72 75 48 65  =pcache_g.pLruHe
f6200 61 64 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78  ad; p; p=p->pNex
f6210 74 4c 72 75 29 7b 0a 20 20 20 20 6e 52 65 63 79  tLru){.    nRecy
f6220 63 6c 61 62 6c 65 2b 2b 3b 0a 20 20 7d 0a 0a 20  clable++;.  }.. 
f6230 20 2a 70 6e 43 75 72 72 65 6e 74 20 3d 20 70 63   *pnCurrent = pc
f6240 61 63 68 65 5f 67 2e 6e 43 75 72 72 65 6e 74 50  ache_g.nCurrentP
f6250 61 67 65 3b 0a 20 20 2a 70 6e 4d 61 78 20 3d 20  age;.  *pnMax = 
f6260 70 63 61 63 68 65 5f 67 2e 6e 4d 61 78 50 61 67  pcache_g.nMaxPag
f6270 65 3b 0a 20 20 2a 70 6e 4d 69 6e 20 3d 20 70 63  e;.  *pnMin = pc
f6280 61 63 68 65 5f 67 2e 6e 4d 69 6e 50 61 67 65 3b  ache_g.nMinPage;
f6290 0a 20 20 2a 70 6e 52 65 63 79 63 6c 61 62 6c 65  .  *pnRecyclable
f62a0 20 3d 20 6e 52 65 63 79 63 6c 61 62 6c 65 3b 0a   = nRecyclable;.
f62b0 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  }.#endif../*****
f62c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
f62d0 20 70 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a   pcache.c ******
f62e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f62f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6300 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
f6310 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
f6320 66 69 6c 65 20 70 61 67 65 72 2e 63 20 2a 2a 2a  file pager.c ***
f6330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6350 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
f6360 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
f6370 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
f6380 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
f6390 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
f63a0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
f63b0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
f63c0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
f63d0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
f63e0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
f63f0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
f6400 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
f6410 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
f6420 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
f6430 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
f6440 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
f6450 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
f6460 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
f6470 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
f6480 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
f6490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f64a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f64b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f64c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f64d0 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
f64e0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
f64f0 66 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  f the page cache
f6500 20 73 75 62 73 79 73 74 65 6d 20 6f 72 20 22 70   subsystem or "p
f6510 61 67 65 72 22 2e 0a 2a 2a 20 0a 2a 2a 20 54 68  ager"..** .** Th
f6520 65 20 70 61 67 65 72 20 69 73 20 75 73 65 64 20  e pager is used 
f6530 74 6f 20 61 63 63 65 73 73 20 61 20 64 61 74 61  to access a data
f6540 62 61 73 65 20 64 69 73 6b 20 66 69 6c 65 2e 20  base disk file. 
f6550 20 49 74 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a   It implements.*
f6560 2a 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 20  * atomic commit 
f6570 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 72  and rollback thr
f6580 6f 75 67 68 20 74 68 65 20 75 73 65 20 6f 66 20  ough the use of 
f6590 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74  a journal file t
f65a0 68 61 74 0a 2a 2a 20 69 73 20 73 65 70 61 72 61  hat.** is separa
f65b0 74 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  te from the data
f65c0 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20  base file.  The 
f65d0 70 61 67 65 72 20 61 6c 73 6f 20 69 6d 70 6c 65  pager also imple
f65e0 6d 65 6e 74 73 20 66 69 6c 65 0a 2a 2a 20 6c 6f  ments file.** lo
f65f0 63 6b 69 6e 67 20 74 6f 20 70 72 65 76 65 6e 74  cking to prevent
f6600 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20 66   two processes f
f6610 72 6f 6d 20 77 72 69 74 69 6e 67 20 74 68 65 20  rom writing the 
f6620 73 61 6d 65 20 64 61 74 61 62 61 73 65 0a 2a 2a  same database.**
f6630 20 66 69 6c 65 20 73 69 6d 75 6c 74 61 6e 65 6f   file simultaneo
f6640 75 73 6c 79 2c 20 6f 72 20 6f 6e 65 20 70 72 6f  usly, or one pro
f6650 63 65 73 73 20 66 72 6f 6d 20 72 65 61 64 69 6e  cess from readin
f6660 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77  g the database w
f6670 68 69 6c 65 0a 2a 2a 20 61 6e 6f 74 68 65 72 20  hile.** another 
f6680 69 73 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a  is writing..**.*
f6690 2a 20 40 28 23 29 20 24 49 64 3a 20 70 61 67 65  * @(#) $Id: page
f66a0 72 2e 63 2c 76 20 31 2e 35 30 32 20 32 30 30 38  r.c,v 1.502 2008
f66b0 2f 31 31 2f 30 37 20 30 30 3a 32 34 3a 35 34 20  /11/07 00:24:54 
f66c0 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66  drh Exp $.*/.#if
f66d0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
f66e0 5f 44 49 53 4b 49 4f 0a 0a 2f 2a 0a 2a 2a 20 4d  _DISKIO../*.** M
f66f0 61 63 72 6f 73 20 66 6f 72 20 74 72 6f 75 62 6c  acros for troubl
f6700 65 73 68 6f 6f 74 69 6e 67 2e 20 20 4e 6f 72 6d  eshooting.  Norm
f6710 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 0a  ally turned off.
f6720 2a 2f 0a 23 69 66 20 30 0a 23 64 65 66 69 6e 65  */.#if 0.#define
f6730 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
f6740 6e 74 66 20 70 72 69 6e 74 66 0a 23 64 65 66 69  ntf printf.#defi
f6750 6e 65 20 50 41 47 45 52 54 52 41 43 45 31 28 58  ne PAGERTRACE1(X
f6760 29 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  )       sqlite3D
f6770 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a 23 64  ebugPrintf(X).#d
f6780 65 66 69 6e 65 20 50 41 47 45 52 54 52 41 43 45  efine PAGERTRACE
f6790 32 28 58 2c 59 29 20 20 20 20 20 73 71 6c 69 74  2(X,Y)     sqlit
f67a0 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
f67b0 59 29 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  Y).#define PAGER
f67c0 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 20  TRACE3(X,Y,Z)   
f67d0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
f67e0 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e  tf(X,Y,Z).#defin
f67f0 65 20 50 41 47 45 52 54 52 41 43 45 34 28 58 2c  e PAGERTRACE4(X,
f6800 59 2c 5a 2c 57 29 20 73 71 6c 69 74 65 33 44 65  Y,Z,W) sqlite3De
f6810 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c  bugPrintf(X,Y,Z,
f6820 57 29 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  W).#define PAGER
f6830 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 57 2c 56  TRACE5(X,Y,Z,W,V
f6840 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
f6850 69 6e 74 66 28 58 2c 59 2c 5a 2c 57 2c 56 29 0a  intf(X,Y,Z,W,V).
f6860 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 50 41  #else.#define PA
f6870 47 45 52 54 52 41 43 45 31 28 58 29 0a 23 64 65  GERTRACE1(X).#de
f6880 66 69 6e 65 20 50 41 47 45 52 54 52 41 43 45 32  fine PAGERTRACE2
f6890 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 50 41  (X,Y).#define PA
f68a0 47 45 52 54 52 41 43 45 33 28 58 2c 59 2c 5a 29  GERTRACE3(X,Y,Z)
f68b0 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54 52  .#define PAGERTR
f68c0 41 43 45 34 28 58 2c 59 2c 5a 2c 57 29 0a 23 64  ACE4(X,Y,Z,W).#d
f68d0 65 66 69 6e 65 20 50 41 47 45 52 54 52 41 43 45  efine PAGERTRACE
f68e0 35 28 58 2c 59 2c 5a 2c 57 2c 56 29 0a 23 65 6e  5(X,Y,Z,W,V).#en
f68f0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  dif../*.** The f
f6900 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 6d 61 63  ollowing two mac
f6910 72 6f 73 20 61 72 65 20 75 73 65 64 20 77 69 74  ros are used wit
f6920 68 69 6e 20 74 68 65 20 50 41 47 45 52 54 52 41  hin the PAGERTRA
f6930 43 45 58 28 29 20 6d 61 63 72 6f 73 20 61 62 6f  CEX() macros abo
f6940 76 65 0a 2a 2a 20 74 6f 20 70 72 69 6e 74 20 6f  ve.** to print o
f6950 75 74 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74  ut file-descript
f6960 6f 72 73 2e 20 0a 2a 2a 0a 2a 2a 20 50 41 47 45  ors. .**.** PAGE
f6970 52 49 44 28 29 20 74 61 6b 65 73 20 61 20 70 6f  RID() takes a po
f6980 69 6e 74 65 72 20 74 6f 20 61 20 50 61 67 65 72  inter to a Pager
f6990 20 73 74 72 75 63 74 20 61 73 20 69 74 73 20 61   struct as its a
f69a0 72 67 75 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20  rgument. The.** 
f69b0 61 73 73 6f 63 69 61 74 65 64 20 66 69 6c 65 2d  associated file-
f69c0 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 72 65  descriptor is re
f69d0 74 75 72 6e 65 64 2e 20 46 49 4c 45 48 41 4e 44  turned. FILEHAND
f69e0 4c 45 49 44 28 29 20 74 61 6b 65 73 20 61 6e 20  LEID() takes an 
f69f0 73 71 6c 69 74 65 33 5f 66 69 6c 65 0a 2a 2a 20  sqlite3_file.** 
f6a00 73 74 72 75 63 74 20 61 73 20 69 74 73 20 61 72  struct as its ar
f6a10 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69  gument..*/.#defi
f6a20 6e 65 20 50 41 47 45 52 49 44 28 70 29 20 28 28  ne PAGERID(p) ((
f6a30 69 6e 74 29 28 70 2d 3e 66 64 29 29 0a 23 64 65  int)(p->fd)).#de
f6a40 66 69 6e 65 20 46 49 4c 45 48 41 4e 44 4c 45 49  fine FILEHANDLEI
f6a50 44 28 66 64 29 20 28 28 69 6e 74 29 66 64 29 0a  D(fd) ((int)fd).
f6a60 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20  ./*.** The page 
f6a70 63 61 63 68 65 20 61 73 20 61 20 77 68 6f 6c 65  cache as a whole
f6a80 20 69 73 20 61 6c 77 61 79 73 20 69 6e 20 6f 6e   is always in on
f6a90 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
f6aa0 6e 67 0a 2a 2a 20 73 74 61 74 65 73 3a 0a 2a 2a  ng.** states:.**
f6ab0 0a 2a 2a 20 20 20 50 41 47 45 52 5f 55 4e 4c 4f  .**   PAGER_UNLO
f6ac0 43 4b 20 20 20 20 20 20 20 20 54 68 65 20 70 61  CK        The pa
f6ad0 67 65 20 63 61 63 68 65 20 69 73 20 6e 6f 74 20  ge cache is not 
f6ae0 63 75 72 72 65 6e 74 6c 79 20 72 65 61 64 69 6e  currently readin
f6af0 67 20 6f 72 20 0a 2a 2a 20 20 20 20 20 20 20 20  g or .**        
f6b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77                 w
f6b10 72 69 74 69 6e 67 20 74 68 65 20 64 61 74 61 62  riting the datab
f6b20 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 72 65  ase file.  There
f6b30 20 69 73 20 6e 6f 0a 2a 2a 20 20 20 20 20 20 20   is no.**       
f6b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6b50 64 61 74 61 20 68 65 6c 64 20 69 6e 20 6d 65 6d  data held in mem
f6b60 6f 72 79 2e 20 20 54 68 69 73 20 69 73 20 74 68  ory.  This is th
f6b70 65 20 69 6e 69 74 69 61 6c 0a 2a 2a 20 20 20 20  e initial.**    
f6b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6b90 20 20 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20     state..**.** 
f6ba0 20 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 20    PAGER_SHARED  
f6bb0 20 20 20 20 20 20 54 68 65 20 70 61 67 65 20 63        The page c
f6bc0 61 63 68 65 20 69 73 20 72 65 61 64 69 6e 67 20  ache is reading 
f6bd0 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a  the database..**
f6be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6bf0 20 20 20 20 20 20 20 57 72 69 74 69 6e 67 20 69         Writing i
f6c00 73 20 6e 6f 74 20 70 65 72 6d 69 74 74 65 64 2e  s not permitted.
f6c10 20 20 54 68 65 72 65 20 63 61 6e 20 62 65 0a 2a    There can be.*
f6c20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
f6c30 20 20 20 20 20 20 20 20 6d 75 6c 74 69 70 6c 65          multiple
f6c40 20 72 65 61 64 65 72 73 20 61 63 63 65 73 73 69   readers accessi
f6c50 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  ng the same data
f6c60 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  base.**         
f6c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69                fi
f6c80 6c 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 74  le at the same t
f6c90 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47  ime..**.**   PAG
f6ca0 45 52 5f 52 45 53 45 52 56 45 44 20 20 20 20 20  ER_RESERVED     
f6cb0 20 54 68 69 73 20 70 72 6f 63 65 73 73 20 68 61   This process ha
f6cc0 73 20 72 65 73 65 72 76 65 64 20 74 68 65 20 64  s reserved the d
f6cd0 61 74 61 62 61 73 65 20 66 6f 72 20 77 72 69 74  atabase for writ
f6ce0 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ing.**          
f6cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 62 75 74               but
f6d00 20 68 61 73 20 6e 6f 74 20 79 65 74 20 6d 61 64   has not yet mad
f6d10 65 20 61 6e 79 20 63 68 61 6e 67 65 73 2e 20 20  e any changes.  
f6d20 4f 6e 6c 79 20 6f 6e 65 20 70 72 6f 63 65 73 73  Only one process
f6d30 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
f6d40 20 20 20 20 20 20 20 20 20 20 61 74 20 61 20 74            at a t
f6d50 69 6d 65 20 63 61 6e 20 72 65 73 65 72 76 65 20  ime can reserve 
f6d60 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54  the database.  T
f6d70 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 20  he original.**  
f6d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6d90 20 20 20 20 20 64 61 74 61 62 61 73 65 20 66 69       database fi
f6da0 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20  le has not been 
f6db0 6d 6f 64 69 66 69 65 64 20 73 6f 20 6f 74 68 65  modified so othe
f6dc0 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  r.**            
f6dd0 20 20 20 20 20 20 20 20 20 20 20 70 72 6f 63 65             proce
f6de0 73 73 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62  sses may still b
f6df0 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 6f 6e  e reading the on
f6e00 2d 64 69 73 6b 0a 2a 2a 20 20 20 20 20 20 20 20  -disk.**        
f6e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64                 d
f6e20 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a  atabase file..**
f6e30 0a 2a 2a 20 20 20 50 41 47 45 52 5f 45 58 43 4c  .**   PAGER_EXCL
f6e40 55 53 49 56 45 20 20 20 20 20 54 68 65 20 70 61  USIVE     The pa
f6e50 67 65 20 63 61 63 68 65 20 69 73 20 77 72 69 74  ge cache is writ
f6e60 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65  ing the database
f6e70 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  ..**            
f6e80 20 20 20 20 20 20 20 20 20 20 20 41 63 63 65 73             Acces
f6e90 73 20 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20  s is exclusive. 
f6ea0 20 4e 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73   No other proces
f6eb0 73 65 73 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20  ses or.**       
f6ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6ed0 74 68 72 65 61 64 73 20 63 61 6e 20 62 65 20 72  threads can be r
f6ee0 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e  eading or writin
f6ef0 67 20 77 68 69 6c 65 20 6f 6e 65 0a 2a 2a 20 20  g while one.**  
f6f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6f10 20 20 20 20 20 70 72 6f 63 65 73 73 20 69 73 20       process is 
f6f20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20  writing..**.**  
f6f30 20 50 41 47 45 52 5f 53 59 4e 43 45 44 20 20 20   PAGER_SYNCED   
f6f40 20 20 20 20 20 54 68 65 20 70 61 67 65 72 20 6d       The pager m
f6f50 6f 76 65 73 20 74 6f 20 74 68 69 73 20 73 74 61  oves to this sta
f6f60 74 65 20 66 72 6f 6d 20 50 41 47 45 52 5f 45 58  te from PAGER_EX
f6f70 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 20 20  CLUSIVE.**      
f6f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6f90 20 61 66 74 65 72 20 61 6c 6c 20 64 69 72 74 79   after all dirty
f6fa0 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65 6e   pages have been
f6fb0 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a   written to the.
f6fc0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
f6fd0 20 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73           databas
f6fe0 65 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 66  e file and the f
f6ff0 69 6c 65 20 68 61 73 20 62 65 65 6e 20 73 79 6e  ile has been syn
f7000 63 65 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20  ced to.**       
f7010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f7020 64 69 73 6b 2e 20 41 6c 6c 20 74 68 61 74 20 72  disk. All that r
f7030 65 6d 61 69 6e 73 20 74 6f 20 64 6f 20 69 73 20  emains to do is 
f7040 74 6f 20 72 65 6d 6f 76 65 20 6f 72 0a 2a 2a 20  to remove or.** 
f7050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f7060 20 20 20 20 20 20 74 72 75 6e 63 61 74 65 20 74        truncate t
f7070 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
f7080 61 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 74  and the transact
f7090 69 6f 6e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20  ion .**         
f70a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 69                wi
f70b0 6c 6c 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 2e  ll be committed.
f70c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20  .**.** The page 
f70d0 63 61 63 68 65 20 63 6f 6d 65 73 20 75 70 20 69  cache comes up i
f70e0 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20  n PAGER_UNLOCK. 
f70f0 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20   The first time 
f7100 61 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65  a.** sqlite3Page
f7110 72 47 65 74 28 29 20 6f 63 63 75 72 73 2c 20 74  rGet() occurs, t
f7120 68 65 20 73 74 61 74 65 20 74 72 61 6e 73 69 74  he state transit
f7130 69 6f 6e 73 20 74 6f 20 50 41 47 45 52 5f 53 48  ions to PAGER_SH
f7140 41 52 45 44 2e 0a 2a 2a 20 41 66 74 65 72 20 61  ARED..** After a
f7150 6c 6c 20 70 61 67 65 73 20 68 61 76 65 20 62 65  ll pages have be
f7160 65 6e 20 72 65 6c 65 61 73 65 64 20 75 73 69 6e  en released usin
f7170 67 20 73 71 6c 69 74 65 5f 70 61 67 65 5f 75 6e  g sqlite_page_un
f7180 72 65 66 28 29 2c 0a 2a 2a 20 74 68 65 20 73 74  ref(),.** the st
f7190 61 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  ate transitions 
f71a0 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 55 4e  back to PAGER_UN
f71b0 4c 4f 43 4b 2e 20 20 54 68 65 20 66 69 72 73 74  LOCK.  The first
f71c0 20 74 69 6d 65 0a 2a 2a 20 74 68 61 74 20 73 71   time.** that sq
f71d0 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
f71e0 29 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65  ) is called, the
f71f0 20 73 74 61 74 65 20 74 72 61 6e 73 69 74 69 6f   state transitio
f7200 6e 73 20 74 6f 0a 2a 2a 20 50 41 47 45 52 5f 52  ns to.** PAGER_R
f7210 45 53 45 52 56 45 44 2e 20 20 28 4e 6f 74 65 20  ESERVED.  (Note 
f7220 74 68 61 74 20 73 71 6c 69 74 65 33 50 61 67 65  that sqlite3Page
f7230 72 57 72 69 74 65 28 29 20 63 61 6e 20 6f 6e 6c  rWrite() can onl
f7240 79 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 6f  y be.** called o
f7250 6e 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67  n an outstanding
f7260 20 70 61 67 65 20 77 68 69 63 68 20 6d 65 61 6e   page which mean
f7270 73 20 74 68 61 74 20 74 68 65 20 70 61 67 65 72  s that the pager
f7280 20 6d 75 73 74 0a 2a 2a 20 62 65 20 69 6e 20 50   must.** be in P
f7290 41 47 45 52 5f 53 48 41 52 45 44 20 62 65 66 6f  AGER_SHARED befo
f72a0 72 65 20 69 74 20 74 72 61 6e 73 69 74 69 6f 6e  re it transition
f72b0 73 20 74 6f 20 50 41 47 45 52 5f 52 45 53 45 52  s to PAGER_RESER
f72c0 56 45 44 2e 29 0a 2a 2a 20 50 41 47 45 52 5f 52  VED.).** PAGER_R
f72d0 45 53 45 52 56 45 44 20 6d 65 61 6e 73 20 74 68  ESERVED means th
f72e0 61 74 20 74 68 65 72 65 20 69 73 20 61 6e 20 6f  at there is an o
f72f0 70 65 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  pen rollback jou
f7300 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 74 72 61  rnal..** The tra
f7310 6e 73 69 74 69 6f 6e 20 74 6f 20 50 41 47 45 52  nsition to PAGER
f7320 5f 45 58 43 4c 55 53 49 56 45 20 6f 63 63 75 72  _EXCLUSIVE occur
f7330 73 20 62 65 66 6f 72 65 20 61 6e 79 20 63 68 61  s before any cha
f7340 6e 67 65 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65  nges.** are made
f7350 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
f7360 20 66 69 6c 65 2c 20 74 68 6f 75 67 68 20 77 72   file, though wr
f7370 69 74 65 73 20 74 6f 20 74 68 65 20 72 6f 6c 6c  ites to the roll
f7380 62 61 63 6b 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20  back.** journal 
f7390 6f 63 63 75 72 73 20 77 69 74 68 20 6a 75 73 74  occurs with just
f73a0 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e   PAGER_RESERVED.
f73b0 20 20 41 66 74 65 72 20 61 6e 20 73 71 6c 69 74    After an sqlit
f73c0 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28  e3PagerRollback(
f73d0 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 50  ).** or sqlite3P
f73e0 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54  agerCommitPhaseT
f73f0 77 6f 28 29 2c 20 74 68 65 20 73 74 61 74 65 20  wo(), the state 
f7400 63 61 6e 20 67 6f 20 62 61 63 6b 20 74 6f 20 50  can go back to P
f7410 41 47 45 52 5f 53 48 41 52 45 44 2c 0a 2a 2a 20  AGER_SHARED,.** 
f7420 6f 72 20 69 74 20 63 61 6e 20 73 74 61 79 20 61  or it can stay a
f7430 74 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  t PAGER_EXCLUSIV
f7440 45 20 69 66 20 77 65 20 61 72 65 20 69 6e 20 65  E if we are in e
f7450 78 63 6c 75 73 69 76 65 20 61 63 63 65 73 73 20  xclusive access 
f7460 6d 6f 64 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  mode..*/.#define
f7470 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 20   PAGER_UNLOCK   
f7480 20 20 20 30 0a 23 64 65 66 69 6e 65 20 50 41 47     0.#define PAG
f7490 45 52 5f 53 48 41 52 45 44 20 20 20 20 20 20 31  ER_SHARED      1
f74a0 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 53 48     /* same as SH
f74b0 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65  ARED_LOCK */.#de
f74c0 66 69 6e 65 20 50 41 47 45 52 5f 52 45 53 45 52  fine PAGER_RESER
f74d0 56 45 44 20 20 20 20 32 20 20 20 2f 2a 20 73 61  VED    2   /* sa
f74e0 6d 65 20 61 73 20 52 45 53 45 52 56 45 44 5f 4c  me as RESERVED_L
f74f0 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  OCK */.#define P
f7500 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 20  AGER_EXCLUSIVE  
f7510 20 34 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20   4   /* same as 
f7520 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 2a  EXCLUSIVE_LOCK *
f7530 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f  /.#define PAGER_
f7540 53 59 4e 43 45 44 20 20 20 20 20 20 35 0a 0a 2f  SYNCED      5../
f7550 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49  *.** If the SQLI
f7560 54 45 5f 42 55 53 59 5f 52 45 53 45 52 56 45 44  TE_BUSY_RESERVED
f7570 5f 4c 4f 43 4b 20 6d 61 63 72 6f 20 69 73 20 73  _LOCK macro is s
f7580 65 74 20 74 6f 20 74 72 75 65 20 61 74 20 63 6f  et to true at co
f7590 6d 70 69 6c 65 2d 74 69 6d 65 2c 0a 2a 2a 20 74  mpile-time,.** t
f75a0 68 65 6e 20 66 61 69 6c 65 64 20 61 74 74 65 6d  hen failed attem
f75b0 70 74 73 20 74 6f 20 67 65 74 20 61 20 72 65 73  pts to get a res
f75c0 65 72 76 65 64 20 6c 6f 63 6b 20 77 69 6c 6c 20  erved lock will 
f75d0 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20  invoke the busy 
f75e0 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 54 68 69  callback..** Thi
f75f0 73 20 69 73 20 6f 66 66 20 62 79 20 64 65 66 61  s is off by defa
f7600 75 6c 74 2e 20 20 54 6f 20 73 65 65 20 77 68 79  ult.  To see why
f7610 2c 20 63 6f 6e 73 69 64 65 72 20 74 68 65 20 66  , consider the f
f7620 6f 6c 6c 6f 77 69 6e 67 20 73 63 65 6e 61 72 69  ollowing scenari
f7630 6f 3a 0a 2a 2a 20 0a 2a 2a 20 53 75 70 70 6f 73  o:.** .** Suppos
f7640 65 20 74 68 72 65 61 64 20 41 20 61 6c 72 65 61  e thread A alrea
f7650 64 79 20 68 61 73 20 61 20 73 68 61 72 65 64 20  dy has a shared 
f7660 6c 6f 63 6b 20 61 6e 64 20 77 61 6e 74 73 20 61  lock and wants a
f7670 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2e 0a   reserved lock..
f7680 2a 2a 20 54 68 72 65 61 64 20 42 20 61 6c 72 65  ** Thread B alre
f7690 61 64 79 20 68 61 73 20 61 20 72 65 73 65 72 76  ady has a reserv
f76a0 65 64 20 6c 6f 63 6b 20 61 6e 64 20 77 61 6e 74  ed lock and want
f76b0 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  s an exclusive l
f76c0 6f 63 6b 2e 20 20 49 66 0a 2a 2a 20 62 6f 74 68  ock.  If.** both
f76d0 20 74 68 72 65 61 64 73 20 61 72 65 20 75 73 69   threads are usi
f76e0 6e 67 20 74 68 65 69 72 20 62 75 73 79 20 63 61  ng their busy ca
f76f0 6c 6c 62 61 63 6b 73 2c 20 69 74 20 6d 69 67 68  llbacks, it migh
f7700 74 20 62 65 20 61 20 6c 6f 6e 67 20 74 69 6d 65  t be a long time
f7710 0a 2a 2a 20 62 65 20 66 6f 72 20 6f 6e 65 20 6f  .** be for one o
f7720 66 20 74 68 65 20 74 68 72 65 61 64 73 20 67 69  f the threads gi
f7730 76 65 20 75 70 20 61 6e 64 20 61 6c 6c 6f 77 73  ve up and allows
f7740 20 74 68 65 20 6f 74 68 65 72 20 74 6f 20 70 72   the other to pr
f7750 6f 63 65 65 64 2e 0a 2a 2a 20 42 75 74 20 69 66  oceed..** But if
f7760 20 74 68 65 20 74 68 72 65 61 64 20 74 72 79 69   the thread tryi
f7770 6e 67 20 74 6f 20 67 65 74 20 74 68 65 20 72 65  ng to get the re
f7780 73 65 72 76 65 64 20 6c 6f 63 6b 20 67 69 76 65  served lock give
f7790 73 20 75 70 20 71 75 69 63 6b 6c 79 0a 2a 2a 20  s up quickly.** 
f77a0 28 69 66 20 69 74 20 6e 65 76 65 72 20 69 6e 76  (if it never inv
f77b0 6f 6b 65 73 20 69 74 73 20 62 75 73 79 20 63 61  okes its busy ca
f77c0 6c 6c 62 61 63 6b 29 20 74 68 65 6e 20 74 68 65  llback) then the
f77d0 20 63 6f 6e 74 65 6e 74 69 6f 6e 20 77 69 6c 6c   contention will
f77e0 20 62 65 0a 2a 2a 20 72 65 73 6f 6c 76 65 64 20   be.** resolved 
f77f0 71 75 69 63 6b 6c 79 2e 0a 2a 2f 0a 23 69 66 6e  quickly..*/.#ifn
f7800 64 65 66 20 53 51 4c 49 54 45 5f 42 55 53 59 5f  def SQLITE_BUSY_
f7810 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 23 20  RESERVED_LOCK.# 
f7820 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 55  define SQLITE_BU
f7830 53 59 5f 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  SY_RESERVED_LOCK
f7840 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a   0.#endif../*.**
f7850 20 54 68 69 73 20 6d 61 63 72 6f 20 72 6f 75 6e   This macro roun
f7860 64 73 20 76 61 6c 75 65 73 20 75 70 20 73 6f 20  ds values up so 
f7870 74 68 61 74 20 69 66 20 74 68 65 20 76 61 6c 75  that if the valu
f7880 65 20 69 73 20 61 6e 20 61 64 64 72 65 73 73 20  e is an address 
f7890 69 74 0a 2a 2a 20 69 73 20 67 75 61 72 61 6e 74  it.** is guarant
f78a0 65 65 64 20 74 6f 20 62 65 20 61 6e 20 61 64 64  eed to be an add
f78b0 72 65 73 73 20 74 68 61 74 20 69 73 20 61 6c 69  ress that is ali
f78c0 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62 79 74  gned to an 8-byt
f78d0 65 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2f 0a 23  e boundary..*/.#
f78e0 64 65 66 69 6e 65 20 46 4f 52 43 45 5f 41 4c 49  define FORCE_ALI
f78f0 47 4e 4d 45 4e 54 28 58 29 20 20 20 28 28 28 58  GNMENT(X)   (((X
f7900 29 2b 37 29 26 7e 37 29 0a 0a 2f 2a 0a 2a 2a 20  )+7)&~7)../*.** 
f7910 41 20 6d 61 63 72 6f 20 75 73 65 64 20 66 6f 72  A macro used for
f7920 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 6f   invoking the co
f7930 64 65 63 20 69 66 20 74 68 65 72 65 20 69 73 20  dec if there is 
f7940 6f 6e 65 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  one.*/.#ifdef SQ
f7950 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 23  LITE_HAS_CODEC.#
f7960 20 64 65 66 69 6e 65 20 43 4f 44 45 43 31 28 50   define CODEC1(P
f7970 2c 44 2c 4e 2c 58 29 20 69 66 28 20 50 2d 3e 78  ,D,N,X) if( P->x
f7980 43 6f 64 65 63 21 3d 30 20 29 7b 20 50 2d 3e 78  Codec!=0 ){ P->x
f7990 43 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 41  Codec(P->pCodecA
f79a0 72 67 2c 44 2c 4e 2c 58 29 3b 20 7d 0a 23 20 64  rg,D,N,X); }.# d
f79b0 65 66 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44  efine CODEC2(P,D
f79c0 2c 4e 2c 58 29 20 28 28 63 68 61 72 2a 29 28 50  ,N,X) ((char*)(P
f79d0 2d 3e 78 43 6f 64 65 63 21 3d 30 3f 50 2d 3e 78  ->xCodec!=0?P->x
f79e0 43 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 41  Codec(P->pCodecA
f79f0 72 67 2c 44 2c 4e 2c 58 29 3a 44 29 29 0a 23 65  rg,D,N,X):D)).#e
f7a00 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 43 4f 44  lse.# define COD
f7a10 45 43 31 28 50 2c 44 2c 4e 2c 58 29 20 2f 2a 20  EC1(P,D,N,X) /* 
f7a20 4e 4f 2d 4f 50 20 2a 2f 0a 23 20 64 65 66 69 6e  NO-OP */.# defin
f7a30 65 20 43 4f 44 45 43 32 28 50 2c 44 2c 4e 2c 58  e CODEC2(P,D,N,X
f7a40 29 20 28 28 63 68 61 72 2a 29 44 29 0a 23 65 6e  ) ((char*)D).#en
f7a50 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 20 6f 70 65  dif../*.** A ope
f7a60 6e 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20  n page cache is 
f7a70 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
f7a80 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
f7a90 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 50 61  ucture..**.** Pa
f7aa0 67 65 72 2e 65 72 72 43 6f 64 65 20 6d 61 79 20  ger.errCode may 
f7ab0 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45  be set to SQLITE
f7ac0 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43  _IOERR, SQLITE_C
f7ad0 4f 52 52 55 50 54 2c 20 6f 72 0a 2a 2a 20 6f 72  ORRUPT, or.** or
f7ae0 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e 20 4f 6e   SQLITE_FULL. On
f7af0 63 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 69  ce one of the fi
f7b00 72 73 74 20 74 68 72 65 65 20 65 72 72 6f 72 73  rst three errors
f7b10 20 6f 63 63 75 72 73 2c 20 69 74 20 70 65 72 73   occurs, it pers
f7b20 69 73 74 73 0a 2a 2a 20 61 6e 64 20 69 73 20 72  ists.** and is r
f7b30 65 74 75 72 6e 65 64 20 61 73 20 74 68 65 20 72  eturned as the r
f7b40 65 73 75 6c 74 20 6f 66 20 65 76 65 72 79 20 6d  esult of every m
f7b50 61 6a 6f 72 20 70 61 67 65 72 20 41 50 49 20 63  ajor pager API c
f7b60 61 6c 6c 2e 20 20 54 68 65 0a 2a 2a 20 53 51 4c  all.  The.** SQL
f7b70 49 54 45 5f 46 55 4c 4c 20 72 65 74 75 72 6e 20  ITE_FULL return 
f7b80 63 6f 64 65 20 69 73 20 73 6c 69 67 68 74 6c 79  code is slightly
f7b90 20 64 69 66 66 65 72 65 6e 74 2e 20 49 74 20 70   different. It p
f7ba0 65 72 73 69 73 74 73 20 6f 6e 6c 79 20 75 6e 74  ersists only unt
f7bb0 69 6c 20 74 68 65 0a 2a 2a 20 6e 65 78 74 20 73  il the.** next s
f7bc0 75 63 63 65 73 73 66 75 6c 20 72 6f 6c 6c 62 61  uccessful rollba
f7bd0 63 6b 20 69 73 20 70 65 72 66 6f 72 6d 65 64 20  ck is performed 
f7be0 6f 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 63  on the pager cac
f7bf0 68 65 2e 20 41 6c 73 6f 2c 0a 2a 2a 20 53 51 4c  he. Also,.** SQL
f7c00 49 54 45 5f 46 55 4c 4c 20 64 6f 65 73 20 6e 6f  ITE_FULL does no
f7c10 74 20 61 66 66 65 63 74 20 74 68 65 20 73 71 6c  t affect the sql
f7c20 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 61  ite3PagerGet() a
f7c30 6e 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c  nd sqlite3PagerL
f7c40 6f 6f 6b 75 70 28 29 0a 2a 2a 20 41 50 49 73 2c  ookup().** APIs,
f7c50 20 74 68 65 79 20 6d 61 79 20 73 74 69 6c 6c 20   they may still 
f7c60 62 65 20 75 73 65 64 20 73 75 63 63 65 73 73 66  be used successf
f7c70 75 6c 6c 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ully..*/.struct 
f7c80 50 61 67 65 72 20 7b 0a 20 20 73 71 6c 69 74 65  Pager {.  sqlite
f7c90 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20  3_vfs *pVfs;    
f7ca0 20 20 20 20 20 20 2f 2a 20 4f 53 20 66 75 6e 63        /* OS func
f7cb0 74 69 6f 6e 73 20 74 6f 20 75 73 65 20 66 6f 72  tions to use for
f7cc0 20 49 4f 20 2a 2f 0a 20 20 75 38 20 6a 6f 75 72   IO */.  u8 jour
f7cd0 6e 61 6c 4f 70 65 6e 3b 20 20 20 20 20 20 20 20  nalOpen;        
f7ce0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
f7cf0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73  journal file des
f7d00 63 72 69 70 74 6f 72 73 20 69 73 20 76 61 6c 69  criptors is vali
f7d10 64 20 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61  d */.  u8 journa
f7d20 6c 53 74 61 72 74 65 64 3b 20 20 20 20 20 20 20  lStarted;       
f7d30 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 68 65     /* True if he
f7d40 61 64 65 72 20 6f 66 20 6a 6f 75 72 6e 61 6c 20  ader of journal 
f7d50 69 73 20 73 79 6e 63 65 64 20 2a 2f 0a 20 20 75  is synced */.  u
f7d60 38 20 75 73 65 4a 6f 75 72 6e 61 6c 3b 20 20 20  8 useJournal;   
f7d70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
f7d80 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  e a rollback jou
f7d90 72 6e 61 6c 20 6f 6e 20 74 68 69 73 20 66 69 6c  rnal on this fil
f7da0 65 20 2a 2f 0a 20 20 75 38 20 6e 6f 52 65 61 64  e */.  u8 noRead
f7db0 6c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20  lock;           
f7dc0 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 62 6f 74     /* Do not bot
f7dd0 68 65 72 20 74 6f 20 6f 62 74 61 69 6e 20 72 65  her to obtain re
f7de0 61 64 6c 6f 63 6b 73 20 2a 2f 0a 20 20 75 38 20  adlocks */.  u8 
f7df0 73 74 6d 74 4f 70 65 6e 3b 20 20 20 20 20 20 20  stmtOpen;       
f7e00 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
f7e10 20 69 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e   if the statemen
f7e20 74 20 73 75 62 6a 6f 75 72 6e 61 6c 20 69 73 20  t subjournal is 
f7e30 6f 70 65 6e 20 2a 2f 0a 20 20 75 38 20 73 74 6d  open */.  u8 stm
f7e40 74 49 6e 55 73 65 3b 20 20 20 20 20 20 20 20 20  tInUse;         
f7e50 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 77 65        /* True we
f7e60 20 61 72 65 20 69 6e 20 61 20 73 74 61 74 65 6d   are in a statem
f7e70 65 6e 74 20 73 75 62 74 72 61 6e 73 61 63 74 69  ent subtransacti
f7e80 6f 6e 20 2a 2f 0a 20 20 75 38 20 73 74 6d 74 41  on */.  u8 stmtA
f7e90 75 74 6f 6f 70 65 6e 3b 20 20 20 20 20 20 20 20  utoopen;        
f7ea0 20 20 20 20 2f 2a 20 4f 70 65 6e 20 73 74 6d 74      /* Open stmt
f7eb0 20 6a 6f 75 72 6e 61 6c 20 77 68 65 6e 20 6d 61   journal when ma
f7ec0 69 6e 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 70  in journal is op
f7ed0 65 6e 65 64 2a 2f 0a 20 20 75 38 20 6e 6f 53 79  ened*/.  u8 noSy
f7ee0 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nc;             
f7ef0 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73       /* Do not s
f7f00 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ync the journal 
f7f10 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 75 38 20  if true */.  u8 
f7f20 66 75 6c 6c 53 79 6e 63 3b 20 20 20 20 20 20 20  fullSync;       
f7f30 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 65           /* Do e
f7f40 78 74 72 61 20 73 79 6e 63 73 20 6f 66 20 74 68  xtra syncs of th
f7f50 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 6f  e journal for ro
f7f60 62 75 73 74 6e 65 73 73 20 2a 2f 0a 20 20 75 38  bustness */.  u8
f7f70 20 73 79 6e 63 5f 66 6c 61 67 73 3b 20 20 20 20   sync_flags;    
f7f80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
f7f90 20 6f 66 20 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20   of SYNC_NORMAL 
f7fa0 6f 72 20 53 59 4e 43 5f 46 55 4c 4c 20 2a 2f 0a  or SYNC_FULL */.
f7fb0 20 20 75 38 20 73 74 61 74 65 3b 20 20 20 20 20    u8 state;     
f7fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
f7fd0 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2c 20 5f   PAGER_UNLOCK, _
f7fe0 53 48 41 52 45 44 2c 20 5f 52 45 53 45 52 56 45  SHARED, _RESERVE
f7ff0 44 2c 20 65 74 63 2e 20 2a 2f 0a 20 20 75 38 20  D, etc. */.  u8 
f8000 74 65 6d 70 46 69 6c 65 3b 20 20 20 20 20 20 20  tempFile;       
f8010 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 69 6c           /* zFil
f8020 65 6e 61 6d 65 20 69 73 20 61 20 74 65 6d 70 6f  ename is a tempo
f8030 72 61 72 79 20 66 69 6c 65 20 2a 2f 0a 20 20 75  rary file */.  u
f8040 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20  8 readOnly;     
f8050 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
f8060 75 65 20 66 6f 72 20 61 20 72 65 61 64 2d 6f 6e  ue for a read-on
f8070 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  ly database */. 
f8080 20 75 38 20 6e 65 65 64 53 79 6e 63 3b 20 20 20   u8 needSync;   
f8090 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
f80a0 54 72 75 65 20 69 66 20 61 6e 20 66 73 79 6e 63  True if an fsync
f80b0 28 29 20 69 73 20 6e 65 65 64 65 64 20 6f 6e 20  () is needed on 
f80c0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20  the journal */. 
f80d0 20 75 38 20 64 69 72 74 79 43 61 63 68 65 3b 20   u8 dirtyCache; 
f80e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
f80f0 54 72 75 65 20 69 66 20 63 61 63 68 65 64 20 70  True if cached p
f8100 61 67 65 73 20 68 61 76 65 20 63 68 61 6e 67 65  ages have change
f8110 64 20 2a 2f 0a 20 20 75 38 20 61 6c 77 61 79 73  d */.  u8 always
f8120 52 6f 6c 6c 62 61 63 6b 3b 20 20 20 20 20 20 20  Rollback;       
f8130 20 20 20 2f 2a 20 44 69 73 61 62 6c 65 20 44 6f     /* Disable Do
f8140 6e 74 52 6f 6c 6c 62 61 63 6b 28 29 20 66 6f 72  ntRollback() for
f8150 20 61 6c 6c 20 70 61 67 65 73 20 2a 2f 0a 20 20   all pages */.  
f8160 75 38 20 6d 65 6d 44 62 3b 20 20 20 20 20 20 20  u8 memDb;       
f8170 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
f8180 72 75 65 20 74 6f 20 69 6e 68 69 62 69 74 20 61  rue to inhibit a
f8190 6c 6c 20 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a 20  ll file I/O */. 
f81a0 20 75 38 20 73 65 74 4d 61 73 74 65 72 3b 20 20   u8 setMaster;  
f81b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
f81c0 54 72 75 65 20 69 66 20 61 20 6d 2d 6a 20 6e 61  True if a m-j na
f81d0 6d 65 20 68 61 73 20 62 65 65 6e 20 77 72 69 74  me has been writ
f81e0 74 65 6e 20 74 6f 20 6a 72 6e 6c 20 2a 2f 0a 20  ten to jrnl */. 
f81f0 20 75 38 20 64 6f 4e 6f 74 53 79 6e 63 3b 20 20   u8 doNotSync;  
f8200 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
f8210 42 6f 6f 6c 65 61 6e 2e 20 57 68 69 6c 65 20 74  Boolean. While t
f8220 72 75 65 2c 20 64 6f 20 6e 6f 74 20 73 70 69 6c  rue, do not spil
f8230 6c 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 20  l the cache */. 
f8240 20 75 38 20 65 78 63 6c 75 73 69 76 65 4d 6f 64   u8 exclusiveMod
f8250 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e;           /* 
f8260 42 6f 6f 6c 65 61 6e 2e 20 54 72 75 65 20 69 66  Boolean. True if
f8270 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 3d 45   locking_mode==E
f8280 58 43 4c 55 53 49 56 45 20 2a 2f 0a 20 20 75 38  XCLUSIVE */.  u8
f8290 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 20 20 20   journalMode;   
f82a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 20            /* On 
f82b0 6f 66 20 74 68 65 20 50 41 47 45 52 5f 4a 4f 55  of the PAGER_JOU
f82c0 52 4e 41 4c 4d 4f 44 45 5f 2a 20 76 61 6c 75 65  RNALMODE_* value
f82d0 73 20 2a 2f 0a 20 20 75 38 20 64 62 4d 6f 64 69  s */.  u8 dbModi
f82e0 66 69 65 64 3b 20 20 20 20 20 20 20 20 20 20 20  fied;           
f82f0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
f8300 65 72 65 20 61 72 65 20 61 6e 79 20 63 68 61 6e  ere are any chan
f8310 67 65 73 20 74 6f 20 74 68 65 20 44 62 20 2a 2f  ges to the Db */
f8320 0a 20 20 75 38 20 63 68 61 6e 67 65 43 6f 75 6e  .  u8 changeCoun
f8330 74 44 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 2f  tDone;         /
f8340 2a 20 53 65 74 20 61 66 74 65 72 20 69 6e 63 72  * Set after incr
f8350 65 6d 65 6e 74 69 6e 67 20 74 68 65 20 63 68 61  ementing the cha
f8360 6e 67 65 2d 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  nge-counter */. 
f8370 20 75 33 32 20 76 66 73 46 6c 61 67 73 3b 20 20   u32 vfsFlags;  
f8380 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
f8390 46 6c 61 67 73 20 66 6f 72 20 73 71 6c 69 74 65  Flags for sqlite
f83a0 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f  3_vfs.xOpen() */
f83b0 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65 3b 20  .  int errCode; 
f83c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
f83d0 2a 20 4f 6e 65 20 6f 66 20 73 65 76 65 72 61 6c  * One of several
f83e0 20 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f 72 73   kinds of errors
f83f0 20 2a 2f 0a 20 20 69 6e 74 20 64 62 53 69 7a 65   */.  int dbSize
f8400 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
f8410 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70    /* Number of p
f8420 61 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65  ages in the file
f8430 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 69 67 44 62   */.  int origDb
f8440 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20  Size;           
f8450 20 20 2f 2a 20 64 62 53 69 7a 65 20 62 65 66 6f    /* dbSize befo
f8460 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63  re the current c
f8470 68 61 6e 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73  hange */.  int s
f8480 74 6d 74 53 69 7a 65 3b 20 20 20 20 20 20 20 20  tmtSize;        
f8490 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
f84a0 66 20 64 61 74 61 62 61 73 65 20 28 69 6e 20 70  f database (in p
f84b0 61 67 65 73 29 20 61 74 20 73 74 6d 74 5f 62 65  ages) at stmt_be
f84c0 67 69 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e  gin() */.  int n
f84d0 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  Rec;            
f84e0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
f84f0 20 6f 66 20 70 61 67 65 73 20 77 72 69 74 74 65   of pages writte
f8500 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  n to the journal
f8510 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 49   */.  u32 cksumI
f8520 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  nit;            
f8530 20 20 2f 2a 20 51 75 61 73 69 2d 72 61 6e 64 6f    /* Quasi-rando
f8540 6d 20 76 61 6c 75 65 20 61 64 64 65 64 20 74 6f  m value added to
f8550 20 65 76 65 72 79 20 63 68 65 63 6b 73 75 6d 20   every checksum 
f8560 2a 2f 0a 20 20 69 6e 74 20 73 74 6d 74 4e 52 65  */.  int stmtNRe
f8570 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
f8580 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65   /* Number of re
f8590 63 6f 72 64 73 20 69 6e 20 73 74 6d 74 20 73 75  cords in stmt su
f85a0 62 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 6e  bjournal */.  in
f85b0 74 20 6e 45 78 74 72 61 3b 20 20 20 20 20 20 20  t nExtra;       
f85c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64            /* Add
f85d0 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73   this many bytes
f85e0 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f   to each in-memo
f85f0 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74  ry page */.  int
f8600 20 70 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20   pageSize;      
f8610 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
f8620 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 61  er of bytes in a
f8630 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 6e   page */.  int n
f8640 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20  Page;           
f8650 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20         /* Total 
f8660 6e 75 6d 62 65 72 20 6f 66 20 69 6e 2d 6d 65 6d  number of in-mem
f8670 6f 72 79 20 70 61 67 65 73 20 2a 2f 0a 20 20 69  ory pages */.  i
f8680 6e 74 20 6d 78 50 61 67 65 3b 20 20 20 20 20 20  nt mxPage;      
f8690 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
f86a0 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
f86b0 70 61 67 65 73 20 74 6f 20 68 6f 6c 64 20 69 6e  pages to hold in
f86c0 20 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e 6f   cache */.  Pgno
f86d0 20 6d 78 50 67 6e 6f 3b 20 20 20 20 20 20 20 20   mxPgno;        
f86e0 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d          /* Maxim
f86f0 75 6d 20 61 6c 6c 6f 77 65 64 20 73 69 7a 65 20  um allowed size 
f8700 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
f8710 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 49 6e  */.  Bitvec *pIn
f8720 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20  Journal;        
f8730 20 2f 2a 20 4f 6e 65 20 62 69 74 20 66 6f 72 20   /* One bit for 
f8740 65 61 63 68 20 70 61 67 65 20 69 6e 20 74 68 65  each page in the
f8750 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a   database file *
f8760 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 49 6e 53  /.  Bitvec *pInS
f8770 74 6d 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  tmt;            
f8780 2f 2a 20 4f 6e 65 20 62 69 74 20 66 6f 72 20 65  /* One bit for e
f8790 61 63 68 20 70 61 67 65 20 69 6e 20 74 68 65 20  ach page in the 
f87a0 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 42 69  database */.  Bi
f87b0 74 76 65 63 20 2a 70 41 6c 77 61 79 73 52 6f 6c  tvec *pAlwaysRol
f87c0 6c 62 61 63 6b 3b 20 20 20 20 2f 2a 20 4f 6e 65  lback;    /* One
f87d0 20 62 69 74 20 66 6f 72 20 65 61 63 68 20 70 61   bit for each pa
f87e0 67 65 20 6d 61 72 6b 65 64 20 61 6c 77 61 79 73  ge marked always
f87f0 2d 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 63  -rollback */.  c
f8800 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b 20  har *zFilename; 
f8810 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
f8820 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
f8830 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61  se file */.  cha
f8840 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20 20 20 20  r *zJournal;    
f8850 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
f8860 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
f8870 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  file */.  char *
f8880 7a 44 69 72 65 63 74 6f 72 79 3b 20 20 20 20 20  zDirectory;     
f8890 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63 74 6f        /* Directo
f88a0 72 79 20 68 6f 6c 64 20 64 61 74 61 62 61 73 65  ry hold database
f88b0 20 61 6e 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   and journal fil
f88c0 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  es */.  sqlite3_
f88d0 66 69 6c 65 20 2a 66 64 2c 20 2a 6a 66 64 3b 20  file *fd, *jfd; 
f88e0 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63      /* File desc
f88f0 72 69 70 74 6f 72 73 20 66 6f 72 20 64 61 74 61  riptors for data
f8900 62 61 73 65 20 61 6e 64 20 6a 6f 75 72 6e 61 6c  base and journal
f8910 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69   */.  sqlite3_fi
f8920 6c 65 20 2a 73 74 66 64 3b 20 20 20 20 20 20 20  le *stfd;       
f8930 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69    /* File descri
f8940 70 74 6f 72 20 66 6f 72 20 74 68 65 20 73 74 61  ptor for the sta
f8950 74 65 6d 65 6e 74 20 73 75 62 6a 6f 75 72 6e 61  tement subjourna
f8960 6c 2a 2f 0a 20 20 42 75 73 79 48 61 6e 64 6c 65  l*/.  BusyHandle
f8970 72 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 3b  r *pBusyHandler;
f8980 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
f8990 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e 64 6c  sqlite.busyHandl
f89a0 65 72 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72  er */.  i64 jour
f89b0 6e 61 6c 4f 66 66 3b 20 20 20 20 20 20 20 20 20  nalOff;         
f89c0 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 62      /* Current b
f89d0 79 74 65 20 6f 66 66 73 65 74 20 69 6e 20 74 68  yte offset in th
f89e0 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a  e journal file *
f89f0 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c 48  /.  i64 journalH
f8a00 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dr;             
f8a10 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74 20 74  /* Byte offset t
f8a20 6f 20 70 72 65 76 69 6f 75 73 20 6a 6f 75 72 6e  o previous journ
f8a30 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69  al header */.  i
f8a40 36 34 20 73 74 6d 74 48 64 72 4f 66 66 3b 20 20  64 stmtHdrOff;  
f8a50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
f8a60 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  rst journal head
f8a70 65 72 20 77 72 69 74 74 65 6e 20 74 68 69 73 20  er written this 
f8a80 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69  statement */.  i
f8a90 36 34 20 73 74 6d 74 43 6b 73 75 6d 3b 20 20 20  64 stmtCksum;   
f8aa0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6b             /* ck
f8ab0 73 75 6d 49 6e 69 74 20 77 68 65 6e 20 73 74 61  sumInit when sta
f8ac0 74 65 6d 65 6e 74 20 77 61 73 20 73 74 61 72 74  tement was start
f8ad0 65 64 20 2a 2f 0a 20 20 69 36 34 20 73 74 6d 74  ed */.  i64 stmt
f8ae0 4a 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20  JSize;          
f8af0 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6a      /* Size of j
f8b00 6f 75 72 6e 61 6c 20 61 74 20 73 74 6d 74 5f 62  ournal at stmt_b
f8b10 65 67 69 6e 28 29 20 2a 2f 0a 20 20 75 33 32 20  egin() */.  u32 
f8b20 73 65 63 74 6f 72 53 69 7a 65 3b 20 20 20 20 20  sectorSize;     
f8b30 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 75 6d          /* Assum
f8b40 65 64 20 73 65 63 74 6f 72 20 73 69 7a 65 20 64  ed sector size d
f8b50 75 72 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 20 2a  uring rollback *
f8b60 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
f8b70 54 45 53 54 0a 20 20 69 6e 74 20 6e 48 69 74 2c  TEST.  int nHit,
f8b80 20 6e 4d 69 73 73 3b 20 20 20 20 20 20 20 20 20   nMiss;         
f8b90 20 20 20 2f 2a 20 43 61 63 68 65 20 68 69 74 73     /* Cache hits
f8ba0 20 61 6e 64 20 6d 69 73 73 69 6e 67 20 2a 2f 0a   and missing */.
f8bb0 20 20 69 6e 74 20 6e 52 65 61 64 2c 20 6e 57 72    int nRead, nWr
f8bc0 69 74 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ite;          /*
f8bd0 20 44 61 74 61 62 61 73 65 20 70 61 67 65 73 20   Database pages 
f8be0 72 65 61 64 2f 77 72 69 74 74 65 6e 20 2a 2f 0a  read/written */.
f8bf0 23 65 6e 64 69 66 0a 20 20 76 6f 69 64 20 28 2a  #endif.  void (*
f8c00 78 52 65 69 6e 69 74 65 72 29 28 44 62 50 61 67  xReiniter)(DbPag
f8c10 65 2a 29 3b 20 2f 2a 20 43 61 6c 6c 20 74 68 69  e*); /* Call thi
f8c20 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 72  s routine when r
f8c30 65 6c 6f 61 64 69 6e 67 20 70 61 67 65 73 20 2a  eloading pages *
f8c40 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
f8c50 48 41 53 5f 43 4f 44 45 43 0a 20 20 76 6f 69 64  HAS_CODEC.  void
f8c60 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 6f 69 64   *(*xCodec)(void
f8c70 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74  *,void*,Pgno,int
f8c80 29 3b 20 2f 2a 20 52 6f 75 74 69 6e 65 20 66 6f  ); /* Routine fo
f8c90 72 20 65 6e 2f 64 65 63 6f 64 69 6e 67 20 64 61  r en/decoding da
f8ca0 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43  ta */.  void *pC
f8cb0 6f 64 65 63 41 72 67 3b 20 20 20 20 20 20 20 20  odecArg;        
f8cc0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67      /* First arg
f8cd0 75 6d 65 6e 74 20 74 6f 20 78 43 6f 64 65 63 28  ument to xCodec(
f8ce0 29 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 68  ) */.#endif.  ch
f8cf0 61 72 20 2a 70 54 6d 70 53 70 61 63 65 3b 20 20  ar *pTmpSpace;  
f8d00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
f8d10 65 72 2e 70 61 67 65 53 69 7a 65 20 62 79 74 65  er.pageSize byte
f8d20 73 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 74  s of space for t
f8d30 6d 70 20 75 73 65 20 2a 2f 0a 20 20 63 68 61 72  mp use */.  char
f8d40 20 64 62 46 69 6c 65 56 65 72 73 5b 31 36 5d 3b   dbFileVers[16];
f8d50 20 20 20 20 20 20 20 20 2f 2a 20 43 68 61 6e 67          /* Chang
f8d60 65 73 20 77 68 65 6e 65 76 65 72 20 64 61 74 61  es whenever data
f8d70 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65  base file change
f8d80 73 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e  s */.  i64 journ
f8d90 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 20 20 20 20  alSizeLimit;    
f8da0 20 20 20 2f 2a 20 53 69 7a 65 20 6c 69 6d 69 74     /* Size limit
f8db0 20 66 6f 72 20 70 65 72 73 69 73 74 65 6e 74 20   for persistent 
f8dc0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f  journal files */
f8dd0 0a 20 20 50 43 61 63 68 65 20 2a 70 50 43 61 63  .  PCache *pPCac
f8de0 68 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  he;            /
f8df0 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 61 67  * Pointer to pag
f8e00 65 20 63 61 63 68 65 20 6f 62 6a 65 63 74 20 2a  e cache object *
f8e10 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  /.};../*.** The 
f8e20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c  following global
f8e30 20 76 61 72 69 61 62 6c 65 73 20 68 6f 6c 64 20   variables hold 
f8e40 63 6f 75 6e 74 65 72 73 20 75 73 65 64 20 66 6f  counters used fo
f8e50 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 70 75 72  r.** testing pur
f8e60 70 6f 73 65 73 20 6f 6e 6c 79 2e 20 20 54 68 65  poses only.  The
f8e70 73 65 20 76 61 72 69 61 62 6c 65 73 20 64 6f 20  se variables do 
f8e80 6e 6f 74 20 65 78 69 73 74 20 69 6e 0a 2a 2a 20  not exist in.** 
f8e90 61 20 6e 6f 6e 2d 74 65 73 74 69 6e 67 20 62 75  a non-testing bu
f8ea0 69 6c 64 2e 20 20 54 68 65 73 65 20 76 61 72 69  ild.  These vari
f8eb0 61 62 6c 65 73 20 61 72 65 20 6e 6f 74 20 74 68  ables are not th
f8ec0 72 65 61 64 2d 73 61 66 65 2e 0a 2a 2f 0a 23 69  read-safe..*/.#i
f8ed0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
f8ee0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
f8ef0 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 72 65  sqlite3_pager_re
f8f00 61 64 64 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20  addb_count = 0; 
f8f10 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
f8f20 66 75 6c 6c 20 70 61 67 65 73 20 72 65 61 64 20  full pages read 
f8f30 66 72 6f 6d 20 44 42 20 2a 2f 0a 53 51 4c 49 54  from DB */.SQLIT
f8f40 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
f8f50 33 5f 70 61 67 65 72 5f 77 72 69 74 65 64 62 5f  3_pager_writedb_
f8f60 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 2f 2a 20  count = 0;   /* 
f8f70 4e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 20 70  Number of full p
f8f80 61 67 65 73 20 77 72 69 74 74 65 6e 20 74 6f 20  ages written to 
f8f90 44 42 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  DB */.SQLITE_API
f8fa0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67   int sqlite3_pag
f8fb0 65 72 5f 77 72 69 74 65 6a 5f 63 6f 75 6e 74 20  er_writej_count 
f8fc0 3d 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65  = 0;    /* Numbe
f8fd0 72 20 6f 66 20 70 61 67 65 73 20 77 72 69 74 74  r of pages writt
f8fe0 65 6e 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f  en to journal */
f8ff0 0a 23 20 64 65 66 69 6e 65 20 50 41 47 45 52 5f  .# define PAGER_
f9000 49 4e 43 52 28 76 29 20 20 76 2b 2b 0a 23 65 6c  INCR(v)  v++.#el
f9010 73 65 0a 23 20 64 65 66 69 6e 65 20 50 41 47 45  se.# define PAGE
f9020 52 5f 49 4e 43 52 28 76 29 0a 23 65 6e 64 69 66  R_INCR(v).#endif
f9030 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4a 6f 75 72 6e 61  ..../*.** Journa
f9040 6c 20 66 69 6c 65 73 20 62 65 67 69 6e 20 77 69  l files begin wi
f9050 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  th the following
f9060 20 6d 61 67 69 63 20 73 74 72 69 6e 67 2e 20 20   magic string.  
f9070 54 68 65 20 64 61 74 61 0a 2a 2a 20 77 61 73 20  The data.** was 
f9080 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 2f 64  obtained from /d
f9090 65 76 2f 72 61 6e 64 6f 6d 2e 20 20 49 74 20 69  ev/random.  It i
f90a0 73 20 75 73 65 64 20 6f 6e 6c 79 20 61 73 20 61  s used only as a
f90b0 20 73 61 6e 69 74 79 20 63 68 65 63 6b 2e 0a 2a   sanity check..*
f90c0 2a 0a 2a 2a 20 53 69 6e 63 65 20 76 65 72 73 69  *.** Since versi
f90d0 6f 6e 20 32 2e 38 2e 30 2c 20 74 68 65 20 6a 6f  on 2.8.0, the jo
f90e0 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 63 6f 6e  urnal format con
f90f0 74 61 69 6e 73 20 61 64 64 69 74 69 6f 6e 61 6c  tains additional
f9100 20 73 61 6e 69 74 79 0a 2a 2a 20 63 68 65 63 6b   sanity.** check
f9110 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  ing information.
f9120 20 20 49 66 20 74 68 65 20 70 6f 77 65 72 20 66    If the power f
f9130 61 69 6c 73 20 77 68 69 6c 65 20 74 68 65 20 6a  ails while the j
f9140 6f 75 72 6e 61 6c 20 69 73 20 62 65 67 69 6e 0a  ournal is begin.
f9150 2a 2a 20 77 72 69 74 74 65 6e 2c 20 73 65 6d 69  ** written, semi
f9160 2d 72 61 6e 64 6f 6d 20 67 61 72 62 61 67 65 20  -random garbage 
f9170 64 61 74 61 20 6d 69 67 68 74 20 61 70 70 65 61  data might appea
f9180 72 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  r in the journal
f9190 0a 2a 2a 20 66 69 6c 65 20 61 66 74 65 72 20 70  .** file after p
f91a0 6f 77 65 72 20 69 73 20 72 65 73 74 6f 72 65 64  ower is restored
f91b0 2e 20 20 49 66 20 61 6e 20 61 74 74 65 6d 70 74  .  If an attempt
f91c0 20 69 73 20 74 68 65 6e 20 6d 61 64 65 0a 2a 2a   is then made.**
f91d0 20 74 6f 20 72 6f 6c 6c 20 74 68 65 20 6a 6f 75   to roll the jou
f91e0 72 6e 61 6c 20 62 61 63 6b 2c 20 74 68 65 20 64  rnal back, the d
f91f0 61 74 61 62 61 73 65 20 63 6f 75 6c 64 20 62 65  atabase could be
f9200 20 63 6f 72 72 75 70 74 65 64 2e 20 20 54 68 65   corrupted.  The
f9210 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 73   additional.** s
f9220 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 64  anity checking d
f9230 61 74 61 20 69 73 20 61 6e 20 61 74 74 65 6d 70  ata is an attemp
f9240 74 20 74 6f 20 64 69 73 63 6f 76 65 72 20 74 68  t to discover th
f9250 65 20 67 61 72 62 61 67 65 20 69 6e 20 74 68 65  e garbage in the
f9260 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20  .** journal and 
f9270 69 67 6e 6f 72 65 20 69 74 2e 0a 2a 2a 0a 2a 2a  ignore it..**.**
f9280 20 54 68 65 20 73 61 6e 69 74 79 20 63 68 65 63   The sanity chec
f9290 6b 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  king information
f92a0 20 66 6f 72 20 74 68 65 20 6e 65 77 20 6a 6f 75   for the new jou
f92b0 72 6e 61 6c 20 66 6f 72 6d 61 74 20 63 6f 6e 73  rnal format cons
f92c0 69 73 74 73 0a 2a 2a 20 6f 66 20 61 20 33 32 2d  ists.** of a 32-
f92d0 62 69 74 20 63 68 65 63 6b 73 75 6d 20 6f 6e 20  bit checksum on 
f92e0 65 61 63 68 20 70 61 67 65 20 6f 66 20 64 61 74  each page of dat
f92f0 61 2e 20 20 54 68 65 20 63 68 65 63 6b 73 75 6d  a.  The checksum
f9300 20 63 6f 76 65 72 73 20 62 6f 74 68 0a 2a 2a 20   covers both.** 
f9310 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  the page number 
f9320 61 6e 64 20 74 68 65 20 70 50 61 67 65 72 2d 3e  and the pPager->
f9330 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 20 6f  pageSize bytes o
f9340 66 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 70  f data for the p
f9350 61 67 65 2e 0a 2a 2a 20 54 68 69 73 20 63 6b 73  age..** This cks
f9360 75 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65  um is initialize
f9370 64 20 74 6f 20 61 20 33 32 2d 62 69 74 20 72 61  d to a 32-bit ra
f9380 6e 64 6f 6d 20 76 61 6c 75 65 20 74 68 61 74 20  ndom value that 
f9390 61 70 70 65 61 72 73 20 69 6e 20 74 68 65 0a 2a  appears in the.*
f93a0 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 72  * journal file r
f93b0 69 67 68 74 20 61 66 74 65 72 20 74 68 65 20 68  ight after the h
f93c0 65 61 64 65 72 2e 20 20 54 68 65 20 72 61 6e 64  eader.  The rand
f93d0 6f 6d 20 69 6e 69 74 69 61 6c 69 7a 65 72 20 69  om initializer i
f93e0 73 20 69 6d 70 6f 72 74 61 6e 74 2c 0a 2a 2a 20  s important,.** 
f93f0 62 65 63 61 75 73 65 20 67 61 72 62 61 67 65 20  because garbage 
f9400 64 61 74 61 20 74 68 61 74 20 61 70 70 65 61 72  data that appear
f9410 73 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20  s at the end of 
f9420 61 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6c 69 6b  a journal is lik
f9430 65 6c 79 0a 2a 2a 20 64 61 74 61 20 74 68 61 74  ely.** data that
f9440 20 77 61 73 20 6f 6e 63 65 20 69 6e 20 6f 74 68   was once in oth
f9450 65 72 20 66 69 6c 65 73 20 74 68 61 74 20 68 61  er files that ha
f9460 76 65 20 6e 6f 77 20 62 65 65 6e 20 64 65 6c 65  ve now been dele
f9470 74 65 64 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20  ted.  If the.** 
f9480 67 61 72 62 61 67 65 20 64 61 74 61 20 63 61 6d  garbage data cam
f9490 65 20 66 72 6f 6d 20 61 6e 20 6f 62 73 6f 6c 65  e from an obsole
f94a0 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c  te journal file,
f94b0 20 74 68 65 20 63 68 65 63 6b 73 75 6d 73 20 6d   the checksums m
f94c0 69 67 68 74 0a 2a 2a 20 62 65 20 63 6f 72 72 65  ight.** be corre
f94d0 63 74 2e 20 20 42 75 74 20 62 79 20 69 6e 69 74  ct.  But by init
f94e0 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 63 68 65  ializing the che
f94f0 63 6b 73 75 6d 20 74 6f 20 72 61 6e 64 6f 6d 20  cksum to random 
f9500 76 61 6c 75 65 20 77 68 69 63 68 0a 2a 2a 20 69  value which.** i
f9510 73 20 64 69 66 66 65 72 65 6e 74 20 66 6f 72 20  s different for 
f9520 65 76 65 72 79 20 6a 6f 75 72 6e 61 6c 2c 20 77  every journal, w
f9530 65 20 6d 69 6e 69 6d 69 7a 65 20 74 68 61 74 20  e minimize that 
f9540 72 69 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  risk..*/.static 
f9550 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
f9560 68 61 72 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  har aJournalMagi
f9570 63 5b 5d 20 3d 20 7b 0a 20 20 30 78 64 39 2c 20  c[] = {.  0xd9, 
f9580 30 78 64 35 2c 20 30 78 30 35 2c 20 30 78 66 39  0xd5, 0x05, 0xf9
f9590 2c 20 30 78 32 30 2c 20 30 78 61 31 2c 20 30 78  , 0x20, 0xa1, 0x
f95a0 36 33 2c 20 30 78 64 37 2c 0a 7d 3b 0a 0a 2f 2a  63, 0xd7,.};../*
f95b0 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20  .** The size of 
f95c0 74 68 65 20 68 65 61 64 65 72 20 61 6e 64 20 6f  the header and o
f95d0 66 20 65 61 63 68 20 70 61 67 65 20 69 6e 20 74  f each page in t
f95e0 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 64 65  he journal is de
f95f0 74 65 72 6d 69 6e 65 64 0a 2a 2a 20 62 79 20 74  termined.** by t
f9600 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63  he following mac
f9610 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ros..*/.#define 
f9620 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50  JOURNAL_PG_SZ(pP
f9630 61 67 65 72 29 20 20 28 28 70 50 61 67 65 72 2d  ager)  ((pPager-
f9640 3e 70 61 67 65 53 69 7a 65 29 20 2b 20 38 29 0a  >pageSize) + 8).
f9650 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e  ./*.** The journ
f9660 61 6c 20 68 65 61 64 65 72 20 73 69 7a 65 20 66  al header size f
f9670 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 49  or this pager. I
f9680 6e 20 74 68 65 20 66 75 74 75 72 65 2c 20 74 68  n the future, th
f9690 69 73 20 63 6f 75 6c 64 20 62 65 0a 2a 2a 20 73  is could be.** s
f96a0 65 74 20 74 6f 20 73 6f 6d 65 20 76 61 6c 75 65  et to some value
f96b0 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64   read from the d
f96c0 69 73 6b 20 63 6f 6e 74 72 6f 6c 6c 65 72 2e 20  isk controller. 
f96d0 54 68 65 20 69 6d 70 6f 72 74 61 6e 74 0a 2a 2a  The important.**
f96e0 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 20   characteristic 
f96f0 69 73 20 74 68 61 74 20 69 74 20 69 73 20 74 68  is that it is th
f9700 65 20 73 61 6d 65 20 73 69 7a 65 20 61 73 20 61  e same size as a
f9710 20 64 69 73 6b 20 73 65 63 74 6f 72 2e 0a 2a 2f   disk sector..*/
f9720 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c  .#define JOURNAL
f9730 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20  _HDR_SZ(pPager) 
f9740 28 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53  (pPager->sectorS
f9750 69 7a 65 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ize)../*.** The 
f9760 6d 61 63 72 6f 20 4d 45 4d 44 42 20 69 73 20 74  macro MEMDB is t
f9770 72 75 65 20 69 66 20 77 65 20 61 72 65 20 64 65  rue if we are de
f9780 61 6c 69 6e 67 20 77 69 74 68 20 61 6e 20 69 6e  aling with an in
f9790 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
f97a0 2e 0a 2a 2a 20 57 65 20 64 6f 20 74 68 69 73 20  ..** We do this 
f97b0 61 73 20 61 20 6d 61 63 72 6f 20 73 6f 20 74 68  as a macro so th
f97c0 61 74 20 69 66 20 74 68 65 20 53 51 4c 49 54 45  at if the SQLITE
f97d0 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 6d  _OMIT_MEMORYDB m
f97e0 61 63 72 6f 20 69 73 20 73 65 74 2c 0a 2a 2a 20  acro is set,.** 
f97f0 74 68 65 20 76 61 6c 75 65 20 6f 66 20 4d 45 4d  the value of MEM
f9800 44 42 20 77 69 6c 6c 20 62 65 20 61 20 63 6f 6e  DB will be a con
f9810 73 74 61 6e 74 20 61 6e 64 20 74 68 65 20 63 6f  stant and the co
f9820 6d 70 69 6c 65 72 20 77 69 6c 6c 20 6f 70 74 69  mpiler will opti
f9830 6d 69 7a 65 0a 2a 2a 20 6f 75 74 20 63 6f 64 65  mize.** out code
f9840 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 65 76 65   that would neve
f9850 72 20 65 78 65 63 75 74 65 2e 0a 2a 2f 0a 23 69  r execute..*/.#i
f9860 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
f9870 5f 4d 45 4d 4f 52 59 44 42 0a 23 20 64 65 66 69  _MEMORYDB.# defi
f9880 6e 65 20 4d 45 4d 44 42 20 30 0a 23 65 6c 73 65  ne MEMDB 0.#else
f9890 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d 44 42 20  .# define MEMDB 
f98a0 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 0a 23 65  pPager->memDb.#e
f98b0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 61 67 65  ndif../*.** Page
f98c0 20 6e 75 6d 62 65 72 20 50 41 47 45 52 5f 4d 4a   number PAGER_MJ
f98d0 5f 50 47 4e 4f 20 69 73 20 6e 65 76 65 72 20 75  _PGNO is never u
f98e0 73 65 64 20 69 6e 20 61 6e 20 53 51 4c 69 74 65  sed in an SQLite
f98f0 20 64 61 74 61 62 61 73 65 20 28 69 74 20 69 73   database (it is
f9900 0a 2a 2a 20 72 65 73 65 72 76 65 64 20 66 6f 72  .** reserved for
f9910 20 77 6f 72 6b 69 6e 67 20 61 72 6f 75 6e 64 20   working around 
f9920 61 20 77 69 6e 64 6f 77 73 2f 70 6f 73 69 78 20  a windows/posix 
f9930 69 6e 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 29  incompatibility)
f9940 2e 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 64 20  . It is.** used 
f9950 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74  in the journal t
f9960 6f 20 73 69 67 6e 69 66 79 20 74 68 61 74 20 74  o signify that t
f9970 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 20  he remainder of 
f9980 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
f9990 20 0a 2a 2a 20 69 73 20 64 65 76 6f 74 65 64 20   .** is devoted 
f99a0 74 6f 20 73 74 6f 72 69 6e 67 20 61 20 6d 61 73  to storing a mas
f99b0 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65  ter journal name
f99c0 20 2d 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20   - there are no 
f99d0 6d 6f 72 65 20 70 61 67 65 73 20 74 6f 0a 2a 2a  more pages to.**
f99e0 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 53 65 65 20   roll back. See 
f99f0 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 66 75 6e  comments for fun
f9a00 63 74 69 6f 6e 20 77 72 69 74 65 4d 61 73 74 65  ction writeMaste
f9a10 72 4a 6f 75 72 6e 61 6c 28 29 20 66 6f 72 20 64  rJournal() for d
f9a20 65 74 61 69 6c 73 2e 0a 2a 2f 0a 2f 2a 20 23 64  etails..*/./* #d
f9a30 65 66 69 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50  efine PAGER_MJ_P
f9a40 47 4e 4f 28 78 29 20 28 50 45 4e 44 49 4e 47 5f  GNO(x) (PENDING_
f9a50 42 59 54 45 2f 28 28 78 29 2d 3e 70 61 67 65 53  BYTE/((x)->pageS
f9a60 69 7a 65 29 29 20 2a 2f 0a 23 64 65 66 69 6e 65  ize)) */.#define
f9a70 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 78   PAGER_MJ_PGNO(x
f9a80 29 20 28 28 50 45 4e 44 49 4e 47 5f 42 59 54 45  ) ((PENDING_BYTE
f9a90 2f 28 28 78 29 2d 3e 70 61 67 65 53 69 7a 65 29  /((x)->pageSize)
f9aa0 29 2b 31 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  )+1)../*.** The 
f9ab0 6d 61 78 69 6d 75 6d 20 6c 65 67 61 6c 20 70 61  maximum legal pa
f9ac0 67 65 20 6e 75 6d 62 65 72 20 69 73 20 28 32 5e  ge number is (2^
f9ad0 33 31 20 2d 20 31 29 2e 0a 2a 2f 0a 23 64 65 66  31 - 1)..*/.#def
f9ae0 69 6e 65 20 50 41 47 45 52 5f 4d 41 58 5f 50 47  ine PAGER_MAX_PG
f9af0 4e 4f 20 32 31 34 37 34 38 33 36 34 37 0a 0a 2f  NO 2147483647../
f9b00 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
f9b10 20 69 66 20 70 61 67 65 20 2a 70 50 67 20 68 61   if page *pPg ha
f9b20 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 77  s already been w
f9b30 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73 74  ritten to the st
f9b40 61 74 65 6d 65 6e 74 0a 2a 2a 20 6a 6f 75 72 6e  atement.** journ
f9b50 61 6c 20 28 6f 72 20 73 74 61 74 65 6d 65 6e 74  al (or statement
f9b60 20 73 6e 61 70 73 68 6f 74 20 68 61 73 20 62 65   snapshot has be
f9b70 65 6e 20 63 72 65 61 74 65 64 2c 20 69 66 20 2a  en created, if *
f9b80 70 50 67 20 69 73 20 70 61 72 74 0a 2a 2a 20 6f  pPg is part.** o
f9b90 66 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64  f an in-memory d
f9ba0 61 74 61 62 61 73 65 29 2e 0a 2a 2f 0a 73 74 61  atabase)..*/.sta
f9bb0 74 69 63 20 69 6e 74 20 70 61 67 65 49 6e 53 74  tic int pageInSt
f9bc0 61 74 65 6d 65 6e 74 28 50 67 48 64 72 20 2a 70  atement(PgHdr *p
f9bd0 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50  Pg){.  Pager *pP
f9be0 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67  ager = pPg->pPag
f9bf0 65 72 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  er;.  return sql
f9c00 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70  ite3BitvecTest(p
f9c10 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74 2c 20  Pager->pInStmt, 
f9c20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 7d 0a 0a 2f  pPg->pgno);.}../
f9c30 2a 0a 2a 2a 20 52 65 61 64 20 61 20 33 32 2d 62  *.** Read a 32-b
f9c40 69 74 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20  it integer from 
f9c50 74 68 65 20 67 69 76 65 6e 20 66 69 6c 65 20 64  the given file d
f9c60 65 73 63 72 69 70 74 6f 72 2e 20 20 53 74 6f 72  escriptor.  Stor
f9c70 65 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a  e the integer.**
f9c80 20 74 68 61 74 20 69 73 20 72 65 61 64 20 69 6e   that is read in
f9c90 20 2a 70 52 65 73 2e 20 20 52 65 74 75 72 6e 20   *pRes.  Return 
f9ca0 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65  SQLITE_OK if eve
f9cb0 72 79 74 68 69 6e 67 20 77 6f 72 6b 65 64 2c 20  rything worked, 
f9cc0 6f 72 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 63  or an.** error c
f9cd0 6f 64 65 20 69 73 20 73 6f 6d 65 74 68 69 6e 67  ode is something
f9ce0 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a   goes wrong..**.
f9cf0 2a 2a 20 41 6c 6c 20 76 61 6c 75 65 73 20 61 72  ** All values ar
f9d00 65 20 73 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b  e stored on disk
f9d10 20 61 73 20 62 69 67 2d 65 6e 64 69 61 6e 2e 0a   as big-endian..
f9d20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65  */.static int re
f9d30 61 64 33 32 62 69 74 73 28 73 71 6c 69 74 65 33  ad32bits(sqlite3
f9d40 5f 66 69 6c 65 20 2a 66 64 2c 20 69 36 34 20 6f  _file *fd, i64 o
f9d50 66 66 73 65 74 2c 20 75 33 32 20 2a 70 52 65 73  ffset, u32 *pRes
f9d60 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  ){.  unsigned ch
f9d70 61 72 20 61 63 5b 34 5d 3b 0a 20 20 69 6e 74 20  ar ac[4];.  int 
f9d80 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65  rc = sqlite3OsRe
f9d90 61 64 28 66 64 2c 20 61 63 2c 20 73 69 7a 65 6f  ad(fd, ac, sizeo
f9da0 66 28 61 63 29 2c 20 6f 66 66 73 65 74 29 3b 0a  f(ac), offset);.
f9db0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
f9dc0 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 52 65 73  _OK ){.    *pRes
f9dd0 20 3d 20 73 71 6c 69 74 65 33 47 65 74 34 62 79   = sqlite3Get4by
f9de0 74 65 28 61 63 29 3b 0a 20 20 7d 0a 20 20 72 65  te(ac);.  }.  re
f9df0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
f9e00 2a 20 57 72 69 74 65 20 61 20 33 32 2d 62 69 74  * Write a 32-bit
f9e10 20 69 6e 74 65 67 65 72 20 69 6e 74 6f 20 61 20   integer into a 
f9e20 73 74 72 69 6e 67 20 62 75 66 66 65 72 20 69 6e  string buffer in
f9e30 20 62 69 67 2d 65 6e 64 69 61 6e 20 62 79 74 65   big-endian byte
f9e40 20 6f 72 64 65 72 2e 0a 2a 2f 0a 23 64 65 66 69   order..*/.#defi
f9e50 6e 65 20 70 75 74 33 32 62 69 74 73 28 41 2c 42  ne put32bits(A,B
f9e60 29 20 20 73 71 6c 69 74 65 33 50 75 74 34 62 79  )  sqlite3Put4by
f9e70 74 65 28 28 75 38 2a 29 41 2c 42 29 0a 0a 2f 2a  te((u8*)A,B)../*
f9e80 0a 2a 2a 20 57 72 69 74 65 20 61 20 33 32 2d 62  .** Write a 32-b
f9e90 69 74 20 69 6e 74 65 67 65 72 20 69 6e 74 6f 20  it integer into 
f9ea0 74 68 65 20 67 69 76 65 6e 20 66 69 6c 65 20 64  the given file d
f9eb0 65 73 63 72 69 70 74 6f 72 2e 20 20 52 65 74 75  escriptor.  Retu
f9ec0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20  rn SQLITE_OK.** 
f9ed0 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61 6e  on success or an
f9ee0 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73   error code is s
f9ef0 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72  omething goes wr
f9f00 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ong..*/.static i
f9f10 6e 74 20 77 72 69 74 65 33 32 62 69 74 73 28 73  nt write32bits(s
f9f20 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c  qlite3_file *fd,
f9f30 20 69 36 34 20 6f 66 66 73 65 74 2c 20 75 33 32   i64 offset, u32
f9f40 20 76 61 6c 29 7b 0a 20 20 63 68 61 72 20 61 63   val){.  char ac
f9f50 5b 34 5d 3b 0a 20 20 70 75 74 33 32 62 69 74 73  [4];.  put32bits
f9f60 28 61 63 2c 20 76 61 6c 29 3b 0a 20 20 72 65 74  (ac, val);.  ret
f9f70 75 72 6e 20 73 71 6c 69 74 65 33 4f 73 57 72 69  urn sqlite3OsWri
f9f80 74 65 28 66 64 2c 20 61 63 2c 20 34 2c 20 6f 66  te(fd, ac, 4, of
f9f90 66 73 65 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  fset);.}../*.** 
f9fa0 49 66 20 66 69 6c 65 20 70 46 64 20 69 73 20 6f  If file pFd is o
f9fb0 70 65 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65  pen, call sqlite
f9fc0 33 4f 73 55 6e 6c 6f 63 6b 28 29 20 6f 6e 20 69  3OsUnlock() on i
f9fd0 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
f9fe0 20 6f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65   osUnlock(sqlite
f9ff0 33 5f 66 69 6c 65 20 2a 70 46 64 2c 20 69 6e 74  3_file *pFd, int
fa000 20 65 4c 6f 63 6b 29 7b 0a 20 20 69 66 28 20 21   eLock){.  if( !
fa010 70 46 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b  pFd->pMethods ){
fa020 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
fa030 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74  TE_OK;.  }.  ret
fa040 75 72 6e 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c  urn sqlite3OsUnl
fa050 6f 63 6b 28 70 46 64 2c 20 65 4c 6f 63 6b 29 3b  ock(pFd, eLock);
fa060 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
fa070 75 6e 63 74 69 6f 6e 20 64 65 74 65 72 6d 69 6e  unction determin
fa080 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  es whether or no
fa090 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69  t the atomic-wri
fa0a0 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a  te optimization.
fa0b0 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 77  ** can be used w
fa0c0 69 74 68 20 74 68 69 73 20 70 61 67 65 72 2e 20  ith this pager. 
fa0d0 54 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  The optimization
fa0e0 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 66 3a   can be used if:
fa0f0 0a 2a 2a 0a 2a 2a 20 20 28 61 29 20 74 68 65 20  .**.**  (a) the 
fa100 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62  value returned b
fa110 79 20 4f 73 44 65 76 69 63 65 43 68 61 72 61 63  y OsDeviceCharac
fa120 74 65 72 69 73 74 69 63 73 28 29 20 69 6e 64 69  teristics() indi
fa130 63 61 74 65 73 20 74 68 61 74 0a 2a 2a 20 20 20  cates that.**   
fa140 20 20 20 61 20 64 61 74 61 62 61 73 65 20 70 61     a database pa
fa150 67 65 20 6d 61 79 20 62 65 20 77 72 69 74 74 65  ge may be writte
fa160 6e 20 61 74 6f 6d 69 63 61 6c 6c 79 2c 20 61 6e  n atomically, an
fa170 64 0a 2a 2a 20 20 28 62 29 20 74 68 65 20 76 61  d.**  (b) the va
fa180 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20  lue returned by 
fa190 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 20 69  OsSectorSize() i
fa1a0 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65  s less than or e
fa1b0 71 75 61 6c 0a 2a 2a 20 20 20 20 20 20 74 6f 20  qual.**      to 
fa1c0 74 68 65 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a  the page size..*
fa1d0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69  *.** If the opti
fa1e0 6d 69 7a 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20  mization cannot 
fa1f0 62 65 20 75 73 65 64 2c 20 30 20 69 73 20 72 65  be used, 0 is re
fa200 74 75 72 6e 65 64 2e 20 49 66 20 69 74 20 63 61  turned. If it ca
fa210 6e 20 62 65 20 75 73 65 64 2c 0a 2a 2a 20 74 68  n be used,.** th
fa220 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74  en the value ret
fa230 75 72 6e 65 64 20 69 73 20 74 68 65 20 73 69 7a  urned is the siz
fa240 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e of the journal
fa250 20 66 69 6c 65 20 77 68 65 6e 20 69 74 0a 2a 2a   file when it.**
fa260 20 63 6f 6e 74 61 69 6e 73 20 72 6f 6c 6c 62 61   contains rollba
fa270 63 6b 20 64 61 74 61 20 66 6f 72 20 65 78 61 63  ck data for exac
fa280 74 6c 79 20 6f 6e 65 20 70 61 67 65 2e 0a 2a 2f  tly one page..*/
fa290 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
fa2a0 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49  NABLE_ATOMIC_WRI
fa2b0 54 45 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72  TE.static int jr
fa2c0 6e 6c 42 75 66 66 65 72 53 69 7a 65 28 50 61 67  nlBufferSize(Pag
fa2d0 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69  er *pPager){.  i
fa2e0 6e 74 20 64 63 3b 20 20 20 20 20 20 20 20 20 20  nt dc;          
fa2f0 20 2f 2a 20 44 65 76 69 63 65 20 63 68 61 72 61   /* Device chara
fa300 63 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 20 20  cteristics */.  
fa310 69 6e 74 20 6e 53 65 63 74 6f 72 3b 20 20 20 20  int nSector;    
fa320 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65    /* Sector size
fa330 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67 65   */.  int szPage
fa340 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65  ;        /* Page
fa350 20 73 69 7a 65 20 2a 2f 0a 20 20 73 71 6c 69 74   size */.  sqlit
fa360 65 33 5f 66 69 6c 65 20 2a 66 64 20 3d 20 70 50  e3_file *fd = pP
fa370 61 67 65 72 2d 3e 66 64 3b 0a 0a 20 20 69 66 28  ager->fd;..  if(
fa380 20 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b   fd->pMethods ){
fa390 0a 20 20 20 20 64 63 20 3d 20 73 71 6c 69 74 65  .    dc = sqlite
fa3a0 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74  3OsDeviceCharact
fa3b0 65 72 69 73 74 69 63 73 28 66 64 29 3b 0a 20 20  eristics(fd);.  
fa3c0 20 20 6e 53 65 63 74 6f 72 20 3d 20 73 71 6c 69    nSector = sqli
fa3d0 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28  te3OsSectorSize(
fa3e0 66 64 29 3b 0a 20 20 20 20 73 7a 50 61 67 65 20  fd);.    szPage 
fa3f0 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  = pPager->pageSi
fa400 7a 65 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  ze;.  }..  asser
fa410 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41  t(SQLITE_IOCAP_A
fa420 54 4f 4d 49 43 35 31 32 3d 3d 28 35 31 32 3e 3e  TOMIC512==(512>>
fa430 38 29 29 3b 0a 20 20 61 73 73 65 72 74 28 53 51  8));.  assert(SQ
fa440 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
fa450 43 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29  C64K==(65536>>8)
fa460 29 3b 0a 0a 20 20 69 66 28 20 21 66 64 2d 3e 70  );..  if( !fd->p
fa470 4d 65 74 68 6f 64 73 20 7c 7c 20 0a 20 20 20 20  Methods || .    
fa480 20 20 20 28 64 63 20 26 20 28 53 51 4c 49 54 45     (dc & (SQLITE
fa490 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 7c 28 73  _IOCAP_ATOMIC|(s
fa4a0 7a 50 61 67 65 3e 3e 38 29 29 20 26 26 20 6e 53  zPage>>8)) && nS
fa4b0 65 63 74 6f 72 3c 3d 73 7a 50 61 67 65 29 20 29  ector<=szPage) )
fa4c0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 4a 4f 55  {.    return JOU
fa4d0 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
fa4e0 65 72 29 20 2b 20 4a 4f 55 52 4e 41 4c 5f 50 47  er) + JOURNAL_PG
fa4f0 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  _SZ(pPager);.  }
fa500 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23  .  return 0;.}.#
fa510 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  endif../*.** Thi
fa520 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c  s function shoul
fa530 64 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e  d be called when
fa540 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
fa550 20 77 69 74 68 69 6e 20 74 68 65 20 70 61 67 65   within the page
fa560 72 0a 2a 2a 20 63 6f 64 65 2e 20 54 68 65 20 66  r.** code. The f
fa570 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73  irst argument is
fa580 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
fa590 65 20 70 61 67 65 72 20 73 74 72 75 63 74 75 72  e pager structur
fa5a0 65 2c 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64  e, the.** second
fa5b0 20 74 68 65 20 65 72 72 6f 72 2d 63 6f 64 65 20   the error-code 
fa5c0 61 62 6f 75 74 20 74 6f 20 62 65 20 72 65 74 75  about to be retu
fa5d0 72 6e 65 64 20 62 79 20 61 20 70 61 67 65 72 20  rned by a pager 
fa5e0 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 20 0a 2a  API function. .*
fa5f0 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75  * The value retu
fa600 72 6e 65 64 20 69 73 20 61 20 63 6f 70 79 20 6f  rned is a copy o
fa610 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  f the second arg
fa620 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75  ument to this fu
fa630 6e 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49  nction. .**.** I
fa640 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  f the second arg
fa650 75 6d 65 6e 74 20 69 73 20 53 51 4c 49 54 45 5f  ument is SQLITE_
fa660 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f  IOERR, SQLITE_CO
fa670 52 52 55 50 54 2c 20 6f 72 20 53 51 4c 49 54 45  RRUPT, or SQLITE
fa680 5f 46 55 4c 4c 0a 2a 2a 20 74 68 65 20 65 72 72  _FULL.** the err
fa690 6f 72 20 62 65 63 6f 6d 65 73 20 70 65 72 73 69  or becomes persi
fa6a0 73 74 65 6e 74 2e 20 55 6e 74 69 6c 20 74 68 65  stent. Until the
fa6b0 20 70 65 72 73 69 73 74 65 6e 20 65 72 72 6f 72   persisten error
fa6c0 20 69 73 20 63 6c 65 61 72 65 64 2c 0a 2a 2a 20   is cleared,.** 
fa6d0 73 75 62 73 65 71 75 65 6e 74 20 41 50 49 20 63  subsequent API c
fa6e0 61 6c 6c 73 20 6f 6e 20 74 68 69 73 20 50 61 67  alls on this Pag
fa6f0 65 72 20 77 69 6c 6c 20 69 6d 6d 65 64 69 61 74  er will immediat
fa700 65 6c 79 20 72 65 74 75 72 6e 20 74 68 65 20 73  ely return the s
fa710 61 6d 65 20 0a 2a 2a 20 65 72 72 6f 72 20 63 6f  ame .** error co
fa720 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 65 72 73  de..**.** A pers
fa730 69 73 74 65 6e 74 20 65 72 72 6f 72 20 69 6e 64  istent error ind
fa740 69 63 61 74 65 73 20 74 68 61 74 20 74 68 65 20  icates that the 
fa750 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
fa760 70 61 67 65 72 2d 63 61 63 68 65 20 0a 2a 2a 20  pager-cache .** 
fa770 63 61 6e 6e 6f 74 20 62 65 20 74 72 75 73 74 65  cannot be truste
fa780 64 2e 20 54 68 69 73 20 73 74 61 74 65 20 63 61  d. This state ca
fa790 6e 20 62 65 20 63 6c 65 61 72 65 64 20 62 79 20  n be cleared by 
fa7a0 63 6f 6d 70 6c 65 74 65 6c 79 20 64 69 73 63 61  completely disca
fa7b0 72 64 69 6e 67 20 0a 2a 2a 20 74 68 65 20 63 6f  rding .** the co
fa7c0 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61  ntents of the pa
fa7d0 67 65 72 2d 63 61 63 68 65 2e 20 49 66 20 61 20  ger-cache. If a 
fa7e0 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20  transaction was 
fa7f0 61 63 74 69 76 65 20 77 68 65 6e 0a 2a 2a 20 74  active when.** t
fa800 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 65 72  he persistent er
fa810 72 6f 72 20 6f 63 63 75 72 65 64 2c 20 74 68 65  ror occured, the
fa820 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a  n the rollback j
fa830 6f 75 72 6e 61 6c 20 6d 61 79 20 6e 65 65 64 0a  ournal may need.
fa840 2a 2a 20 74 6f 20 62 65 20 72 65 70 6c 61 79 65  ** to be replaye
fa850 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
fa860 64 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 50  d pager_unlock(P
fa870 61 67 65 72 20 2a 70 50 61 67 65 72 29 3b 0a 73  ager *pPager);.s
fa880 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f  tatic int pager_
fa890 65 72 72 6f 72 28 50 61 67 65 72 20 2a 70 50 61  error(Pager *pPa
fa8a0 67 65 72 2c 20 69 6e 74 20 72 63 29 7b 0a 20 20  ger, int rc){.  
fa8b0 69 6e 74 20 72 63 32 20 3d 20 72 63 20 26 20 30  int rc2 = rc & 0
fa8c0 78 66 66 3b 0a 20 20 61 73 73 65 72 74 28 0a 20  xff;.  assert(. 
fa8d0 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72        pPager->er
fa8e0 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 46 55  rCode==SQLITE_FU
fa8f0 4c 4c 20 7c 7c 0a 20 20 20 20 20 20 20 70 50 61  LL ||.       pPa
fa900 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51  ger->errCode==SQ
fa910 4c 49 54 45 5f 4f 4b 20 7c 7c 0a 20 20 20 20 20  LITE_OK ||.     
fa920 20 20 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f    (pPager->errCo
fa930 64 65 20 26 20 30 78 66 66 29 3d 3d 53 51 4c 49  de & 0xff)==SQLI
fa940 54 45 5f 49 4f 45 52 52 0a 20 20 29 3b 0a 20 20  TE_IOERR.  );.  
fa950 69 66 28 0a 20 20 20 20 72 63 32 3d 3d 53 51 4c  if(.    rc2==SQL
fa960 49 54 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20 20 20  ITE_FULL ||.    
fa970 72 63 32 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52  rc2==SQLITE_IOER
fa980 52 20 7c 7c 0a 20 20 20 20 72 63 32 3d 3d 53 51  R ||.    rc2==SQ
fa990 4c 49 54 45 5f 43 4f 52 52 55 50 54 0a 20 20 29  LITE_CORRUPT.  )
fa9a0 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72  {.    pPager->er
fa9b0 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 20 20  rCode = rc;.    
fa9c0 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  if( pPager->stat
fa9d0 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20  e==PAGER_UNLOCK 
fa9e0 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33  .     && sqlite3
fa9f0 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70  PcacheRefCount(p
faa00 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d  Pager->pPCache)=
faa10 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  =0 .    ){.     
faa20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 72   /* If the pager
faa30 20 69 73 20 61 6c 72 65 61 64 79 20 75 6e 6c 6f   is already unlo
faa40 63 6b 65 64 2c 20 63 61 6c 6c 20 70 61 67 65 72  cked, call pager
faa50 5f 75 6e 6c 6f 63 6b 28 29 20 6e 6f 77 20 74 6f  _unlock() now to
faa60 0a 20 20 20 20 20 20 2a 2a 20 63 6c 65 61 72 20  .      ** clear 
faa70 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20  the error state 
faa80 61 6e 64 20 65 6e 73 75 72 65 20 74 68 61 74 20  and ensure that 
faa90 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20  the pager-cache 
faaa0 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6d  is .      ** com
faab0 70 6c 65 74 65 6c 79 20 65 6d 70 74 79 2e 0a 20  pletely empty.. 
faac0 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 61       */.      pa
faad0 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65  ger_unlock(pPage
faae0 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  r);.    }.  }.  
faaf0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
fab00 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 43 48  .** If SQLITE_CH
fab10 45 43 4b 5f 50 41 47 45 53 20 69 73 20 64 65 66  ECK_PAGES is def
fab20 69 6e 65 64 20 74 68 65 6e 20 77 65 20 64 6f 20  ined then we do 
fab30 73 6f 6d 65 20 73 61 6e 69 74 79 20 63 68 65 63  some sanity chec
fab40 6b 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68 65 20 63  king.** on the c
fab50 61 63 68 65 20 75 73 69 6e 67 20 61 20 68 61 73  ache using a has
fab60 68 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69  h function.  Thi
fab70 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65  s is used for te
fab80 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 65 62  sting.** and deb
fab90 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a  ugging only..*/.
faba0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48  #ifdef SQLITE_CH
fabb0 45 43 4b 5f 50 41 47 45 53 0a 2f 2a 0a 2a 2a 20  ECK_PAGES./*.** 
fabc0 52 65 74 75 72 6e 20 61 20 33 32 2d 62 69 74 20  Return a 32-bit 
fabd0 68 61 73 68 20 6f 66 20 74 68 65 20 70 61 67 65  hash of the page
fabe0 20 64 61 74 61 20 66 6f 72 20 70 50 61 67 65 2e   data for pPage.
fabf0 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33 32 20 70  .*/.static u32 p
fac00 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 69 6e  ager_datahash(in
fac10 74 20 6e 42 79 74 65 2c 20 75 6e 73 69 67 6e 65  t nByte, unsigne
fac20 64 20 63 68 61 72 20 2a 70 44 61 74 61 29 7b 0a  d char *pData){.
fac30 20 20 75 33 32 20 68 61 73 68 20 3d 20 30 3b 0a    u32 hash = 0;.
fac40 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69    int i;.  for(i
fac50 3d 30 3b 20 69 3c 6e 42 79 74 65 3b 20 69 2b 2b  =0; i<nByte; i++
fac60 29 7b 0a 20 20 20 20 68 61 73 68 20 3d 20 28 68  ){.    hash = (h
fac70 61 73 68 2a 31 30 33 39 29 20 2b 20 70 44 61 74  ash*1039) + pDat
fac80 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75  a[i];.  }.  retu
fac90 72 6e 20 68 61 73 68 3b 0a 7d 0a 73 74 61 74 69  rn hash;.}.stati
faca0 63 20 75 33 32 20 70 61 67 65 72 5f 70 61 67 65  c u32 pager_page
facb0 68 61 73 68 28 50 67 48 64 72 20 2a 70 50 61 67  hash(PgHdr *pPag
facc0 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 61 67  e){.  return pag
facd0 65 72 5f 64 61 74 61 68 61 73 68 28 70 50 61 67  er_datahash(pPag
face0 65 2d 3e 70 50 61 67 65 72 2d 3e 70 61 67 65 53  e->pPager->pageS
facf0 69 7a 65 2c 20 28 75 6e 73 69 67 6e 65 64 20 63  ize, (unsigned c
fad00 68 61 72 20 2a 29 70 50 61 67 65 2d 3e 70 44 61  har *)pPage->pDa
fad10 74 61 29 3b 0a 7d 0a 73 74 61 74 69 63 20 75 33  ta);.}.static u3
fad20 32 20 70 61 67 65 72 5f 73 65 74 5f 70 61 67 65  2 pager_set_page
fad30 68 61 73 68 28 50 67 48 64 72 20 2a 70 50 61 67  hash(PgHdr *pPag
fad40 65 29 7b 0a 20 20 70 50 61 67 65 2d 3e 70 61 67  e){.  pPage->pag
fad50 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70 61  eHash = pager_pa
fad60 67 65 68 61 73 68 28 70 50 61 67 65 29 3b 0a 7d  gehash(pPage);.}
fad70 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 48 45 43  ../*.** The CHEC
fad80 4b 5f 50 41 47 45 20 6d 61 63 72 6f 20 74 61 6b  K_PAGE macro tak
fad90 65 73 20 61 20 50 67 48 64 72 2a 20 61 73 20 61  es a PgHdr* as a
fada0 6e 20 61 72 67 75 6d 65 6e 74 2e 20 49 66 20 53  n argument. If S
fadb0 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45  QLITE_CHECK_PAGE
fadc0 53 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 2c  S.** is defined,
fadd0 20 61 6e 64 20 4e 44 45 42 55 47 20 69 73 20 6e   and NDEBUG is n
fade0 6f 74 20 64 65 66 69 6e 65 64 2c 20 61 6e 20 61  ot defined, an a
fadf0 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e  ssert() statemen
fae00 74 20 63 68 65 63 6b 73 0a 2a 2a 20 74 68 61 74  t checks.** that
fae10 20 74 68 65 20 70 61 67 65 20 69 73 20 65 69 74   the page is eit
fae20 68 65 72 20 64 69 72 74 79 20 6f 72 20 73 74 69  her dirty or sti
fae30 6c 6c 20 6d 61 74 63 68 65 73 20 74 68 65 20 63  ll matches the c
fae40 61 6c 63 75 6c 61 74 65 64 20 70 61 67 65 2d 68  alculated page-h
fae50 61 73 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ash..*/.#define 
fae60 43 48 45 43 4b 5f 50 41 47 45 28 78 29 20 63 68  CHECK_PAGE(x) ch
fae70 65 63 6b 50 61 67 65 28 78 29 0a 73 74 61 74 69  eckPage(x).stati
fae80 63 20 76 6f 69 64 20 63 68 65 63 6b 50 61 67 65  c void checkPage
fae90 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20  (PgHdr *pPg){.  
faea0 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20  Pager *pPager = 
faeb0 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 61  pPg->pPager;.  a
faec0 73 73 65 72 74 28 20 21 70 50 67 2d 3e 70 61 67  ssert( !pPg->pag
faed0 65 48 61 73 68 20 7c 7c 20 70 50 61 67 65 72 2d  eHash || pPager-
faee0 3e 65 72 72 43 6f 64 65 0a 20 20 20 20 20 20 7c  >errCode.      |
faef0 7c 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47  | (pPg->flags&PG
faf00 48 44 52 5f 44 49 52 54 59 29 20 7c 7c 20 70 50  HDR_DIRTY) || pP
faf10 67 2d 3e 70 61 67 65 48 61 73 68 3d 3d 70 61 67  g->pageHash==pag
faf20 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29  er_pagehash(pPg)
faf30 20 29 3b 0a 7d 0a 0a 23 65 6c 73 65 0a 23 64 65   );.}..#else.#de
faf40 66 69 6e 65 20 70 61 67 65 72 5f 64 61 74 61 68  fine pager_datah
faf50 61 73 68 28 58 2c 59 29 20 20 30 0a 23 64 65 66  ash(X,Y)  0.#def
faf60 69 6e 65 20 70 61 67 65 72 5f 70 61 67 65 68 61  ine pager_pageha
faf70 73 68 28 58 29 20 20 30 0a 23 64 65 66 69 6e 65  sh(X)  0.#define
faf80 20 43 48 45 43 4b 5f 50 41 47 45 28 78 29 0a 23   CHECK_PAGE(x).#
faf90 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 45  endif  /* SQLITE
fafa0 5f 43 48 45 43 4b 5f 50 41 47 45 53 20 2a 2f 0a  _CHECK_PAGES */.
fafb0 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73  ./*.** When this
fafc0 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65 20 6a   is called the j
fafd0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20  ournal file for 
fafe0 70 61 67 65 72 20 70 50 61 67 65 72 20 6d 75 73  pager pPager mus
faff0 74 20 62 65 20 6f 70 65 6e 2e 0a 2a 2a 20 54 68  t be open..** Th
fb000 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
fb010 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 72 65   file name is re
fb020 61 64 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20  ad from the end 
fb030 6f 66 20 74 68 65 20 66 69 6c 65 20 61 6e 64 20  of the file and 
fb040 0a 2a 2a 20 77 72 69 74 74 65 6e 20 69 6e 74 6f  .** written into
fb050 20 6d 65 6d 6f 72 79 20 73 75 70 70 6c 69 65 64   memory supplied
fb060 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20   by the caller. 
fb070 0a 2a 2a 0a 2a 2a 20 7a 4d 61 73 74 65 72 20 6d  .**.** zMaster m
fb080 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 61 20 62  ust point to a b
fb090 75 66 66 65 72 20 6f 66 20 61 74 20 6c 65 61 73  uffer of at leas
fb0a0 74 20 6e 4d 61 73 74 65 72 20 62 79 74 65 73 20  t nMaster bytes 
fb0b0 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a 2a 20  allocated by.** 
fb0c0 74 68 65 20 63 61 6c 6c 65 72 2e 20 54 68 69 73  the caller. This
fb0d0 20 73 68 6f 75 6c 64 20 62 65 20 73 71 6c 69 74   should be sqlit
fb0e0 65 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d  e3_vfs.mxPathnam
fb0f0 65 2b 31 20 28 74 6f 20 65 6e 73 75 72 65 20 74  e+1 (to ensure t
fb100 68 65 72 65 20 69 73 0a 2a 2a 20 65 6e 6f 75 67  here is.** enoug
fb110 68 20 73 70 61 63 65 20 74 6f 20 77 72 69 74 65  h space to write
fb120 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
fb130 6e 61 6c 20 6e 61 6d 65 29 2e 20 49 66 20 74 68  nal name). If th
fb140 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
fb150 0a 2a 2a 20 6e 61 6d 65 20 69 6e 20 74 68 65 20  .** name in the 
fb160 6a 6f 75 72 6e 61 6c 20 69 73 20 6c 6f 6e 67 65  journal is longe
fb170 72 20 74 68 61 6e 20 6e 4d 61 73 74 65 72 20 62  r than nMaster b
fb180 79 74 65 73 20 28 69 6e 63 6c 75 64 69 6e 67 20  ytes (including 
fb190 61 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61  a.** nul-termina
fb1a0 74 6f 72 29 2c 20 74 68 65 6e 20 74 68 69 73 20  tor), then this 
fb1b0 69 73 20 68 61 6e 64 6c 65 64 20 61 73 20 69 66  is handled as if
fb1c0 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e   no master journ
fb1d0 61 6c 20 6e 61 6d 65 0a 2a 2a 20 77 65 72 65 20  al name.** were 
fb1e0 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20 6a  present in the j
fb1f0 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66  ournal..**.** If
fb200 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e   no master journ
fb210 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20  al file name is 
fb220 70 72 65 73 65 6e 74 20 7a 4d 61 73 74 65 72 5b  present zMaster[
fb230 30 5d 20 69 73 20 73 65 74 20 74 6f 20 30 20 61  0] is set to 0 a
fb240 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20  nd.** SQLITE_OK 
fb250 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61  returned..*/.sta
fb260 74 69 63 20 69 6e 74 20 72 65 61 64 4d 61 73 74  tic int readMast
fb270 65 72 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65  erJournal(sqlite
fb280 33 5f 66 69 6c 65 20 2a 70 4a 72 6e 6c 2c 20 63  3_file *pJrnl, c
fb290 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c 20 69 6e  har *zMaster, in
fb2a0 74 20 6e 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e  t nMaster){.  in
fb2b0 74 20 72 63 3b 0a 20 20 75 33 32 20 6c 65 6e 3b  t rc;.  u32 len;
fb2c0 0a 20 20 69 36 34 20 73 7a 4a 3b 0a 20 20 75 33  .  i64 szJ;.  u3
fb2d0 32 20 63 6b 73 75 6d 3b 0a 20 20 75 33 32 20 75  2 cksum;.  u32 u
fb2e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
fb2f0 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64 20      /* Unsigned 
fb300 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  loop counter */.
fb310 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
fb320 61 4d 61 67 69 63 5b 38 5d 3b 20 2f 2a 20 41 20  aMagic[8]; /* A 
fb330 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74  buffer to hold t
fb340 68 65 20 6d 61 67 69 63 20 68 65 61 64 65 72 20  he magic header 
fb350 2a 2f 0a 0a 20 20 7a 4d 61 73 74 65 72 5b 30 5d  */..  zMaster[0]
fb360 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 72 63 20 3d   = '\0';..  rc =
fb370 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69   sqlite3OsFileSi
fb380 7a 65 28 70 4a 72 6e 6c 2c 20 26 73 7a 4a 29 3b  ze(pJrnl, &szJ);
fb390 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
fb3a0 45 5f 4f 4b 20 7c 7c 20 73 7a 4a 3c 31 36 20 29  E_OK || szJ<16 )
fb3b0 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
fb3c0 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70  c = read32bits(p
fb3d0 4a 72 6e 6c 2c 20 73 7a 4a 2d 31 36 2c 20 26 6c  Jrnl, szJ-16, &l
fb3e0 65 6e 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  en);.  if( rc!=S
fb3f0 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
fb400 6e 20 72 63 3b 0a 0a 20 20 69 66 28 20 6c 65 6e  n rc;..  if( len
fb410 3e 3d 6e 4d 61 73 74 65 72 20 29 7b 0a 20 20 20  >=nMaster ){.   
fb420 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
fb430 4b 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 72  K;.  }..  rc = r
fb440 65 61 64 33 32 62 69 74 73 28 70 4a 72 6e 6c 2c  ead32bits(pJrnl,
fb450 20 73 7a 4a 2d 31 32 2c 20 26 63 6b 73 75 6d 29   szJ-12, &cksum)
fb460 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
fb470 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
fb480 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  c;..  rc = sqlit
fb490 65 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c 2c 20  e3OsRead(pJrnl, 
fb4a0 61 4d 61 67 69 63 2c 20 38 2c 20 73 7a 4a 2d 38  aMagic, 8, szJ-8
fb4b0 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
fb4c0 49 54 45 5f 4f 4b 20 7c 7c 20 6d 65 6d 63 6d 70  ITE_OK || memcmp
fb4d0 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75 72 6e 61  (aMagic, aJourna
fb4e0 6c 4d 61 67 69 63 2c 20 38 29 20 29 20 72 65 74  lMagic, 8) ) ret
fb4f0 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
fb500 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 4a  sqlite3OsRead(pJ
fb510 72 6e 6c 2c 20 7a 4d 61 73 74 65 72 2c 20 6c 65  rnl, zMaster, le
fb520 6e 2c 20 73 7a 4a 2d 31 36 2d 6c 65 6e 29 3b 0a  n, szJ-16-len);.
fb530 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
fb540 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  _OK ){.    retur
fb550 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73  n rc;.  }.  zMas
fb560 74 65 72 5b 6c 65 6e 5d 20 3d 20 27 5c 30 27 3b  ter[len] = '\0';
fb570 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 20 74 68  ..  /* See if th
fb580 65 20 63 68 65 63 6b 73 75 6d 20 6d 61 74 63 68  e checksum match
fb590 65 73 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  es the master jo
fb5a0 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20  urnal name */.  
fb5b0 66 6f 72 28 75 3d 30 3b 20 75 3c 6c 65 6e 3b 20  for(u=0; u<len; 
fb5c0 75 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d 20  u++){.    cksum 
fb5d0 2d 3d 20 7a 4d 61 73 74 65 72 5b 75 5d 3b 0a 20  -= zMaster[u];. 
fb5e0 20 20 7d 0a 20 20 69 66 28 20 63 6b 73 75 6d 20    }.  if( cksum 
fb5f0 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  ){.    /* If the
fb600 20 63 68 65 63 6b 73 75 6d 20 64 6f 65 73 6e 27   checksum doesn'
fb610 74 20 61 64 64 20 75 70 2c 20 74 68 65 6e 20 6f  t add up, then o
fb620 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68  ne or more of th
fb630 65 20 64 69 73 6b 20 73 65 63 74 6f 72 73 0a 20  e disk sectors. 
fb640 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67     ** containing
fb650 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
fb660 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73 20  nal filename is 
fb670 63 6f 72 72 75 70 74 65 64 2e 20 54 68 69 73 20  corrupted. This 
fb680 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 64 65 66  means.    ** def
fb690 69 6e 69 74 65 6c 79 20 72 6f 6c 6c 20 62 61 63  initely roll bac
fb6a0 6b 2c 20 73 6f 20 6a 75 73 74 20 72 65 74 75 72  k, so just retur
fb6b0 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20  n SQLITE_OK and 
fb6c0 72 65 70 6f 72 74 20 61 20 28 6e 75 6c 29 0a 20  report a (nul). 
fb6d0 20 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75     ** master-jou
fb6e0 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a 20  rnal filename.. 
fb6f0 20 20 20 2a 2f 0a 20 20 20 20 7a 4d 61 73 74 65     */.    zMaste
fb700 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 7d  r[0] = '\0';.  }
fb710 0a 20 20 20 0a 20 20 72 65 74 75 72 6e 20 53 51  .   .  return SQ
fb720 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
fb730 2a 20 53 65 65 6b 20 74 68 65 20 6a 6f 75 72 6e  * Seek the journ
fb740 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  al file descript
fb750 6f 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 73  or to the next s
fb760 65 63 74 6f 72 20 62 6f 75 6e 64 61 72 79 20 77  ector boundary w
fb770 68 65 72 65 20 61 0a 2a 2a 20 6a 6f 75 72 6e 61  here a.** journa
fb780 6c 20 68 65 61 64 65 72 20 6d 61 79 20 62 65 20  l header may be 
fb790 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e  read or written.
fb7a0 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66   Pager.journalOf
fb7b0 66 20 69 73 20 75 70 64 61 74 65 64 20 77 69 74  f is updated wit
fb7c0 68 0a 2a 2a 20 74 68 65 20 6e 65 77 20 73 65 65  h.** the new see
fb7d0 6b 20 6f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20  k offset..**.** 
fb7e0 69 2e 65 20 66 6f 72 20 61 20 73 65 63 74 6f 72  i.e for a sector
fb7f0 20 73 69 7a 65 20 6f 66 20 35 31 32 3a 0a 2a 2a   size of 512:.**
fb800 0a 2a 2a 20 49 6e 70 75 74 20 4f 66 66 73 65 74  .** Input Offset
fb810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 75                Ou
fb820 74 70 75 74 20 4f 66 66 73 65 74 0a 2a 2a 20 2d  tput Offset.** -
fb830 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
fb840 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
fb850 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 30 20 20 20 20 20  ------.** 0     
fb860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fb870 20 20 20 20 30 0a 2a 2a 20 35 31 32 20 20 20 20      0.** 512    
fb880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fb890 20 20 20 35 31 32 0a 2a 2a 20 31 30 30 20 20 20     512.** 100   
fb8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fb8b0 20 20 20 20 35 31 32 0a 2a 2a 20 32 30 30 30 20      512.** 2000 
fb8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fb8d0 20 20 20 20 20 32 30 34 38 0a 2a 2a 20 0a 2a 2f       2048.** .*/
fb8e0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 65  .static void see
fb8f0 6b 4a 6f 75 72 6e 61 6c 48 64 72 28 50 61 67 65  kJournalHdr(Page
fb900 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 36  r *pPager){.  i6
fb910 34 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20  4 offset = 0;.  
fb920 69 36 34 20 63 20 3d 20 70 50 61 67 65 72 2d 3e  i64 c = pPager->
fb930 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 69 66  journalOff;.  if
fb940 28 20 63 20 29 7b 0a 20 20 20 20 6f 66 66 73 65  ( c ){.    offse
fb950 74 20 3d 20 28 28 63 2d 31 29 2f 4a 4f 55 52 4e  t = ((c-1)/JOURN
fb960 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
fb970 29 20 2b 20 31 29 20 2a 20 4a 4f 55 52 4e 41 4c  ) + 1) * JOURNAL
fb980 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b  _HDR_SZ(pPager);
fb990 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6f  .  }.  assert( o
fb9a0 66 66 73 65 74 25 4a 4f 55 52 4e 41 4c 5f 48 44  ffset%JOURNAL_HD
fb9b0 52 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d 30 20  R_SZ(pPager)==0 
fb9c0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 66 66  );.  assert( off
fb9d0 73 65 74 3e 3d 63 20 29 3b 0a 20 20 61 73 73 65  set>=c );.  asse
fb9e0 72 74 28 20 28 6f 66 66 73 65 74 2d 63 29 3c 4a  rt( (offset-c)<J
fb9f0 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
fba00 61 67 65 72 29 20 29 3b 0a 20 20 70 50 61 67 65  ager) );.  pPage
fba10 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20  r->journalOff = 
fba20 6f 66 66 73 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  offset;.}../*.**
fba30 20 57 72 69 74 65 20 7a 65 72 6f 73 20 6f 76 65   Write zeros ove
fba40 72 20 74 68 65 20 68 65 61 64 65 72 20 6f 66 20  r the header of 
fba50 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
fba60 2e 20 20 54 68 69 73 20 68 61 73 20 74 68 65 0a  .  This has the.
fba70 2a 2a 20 65 66 66 65 63 74 20 6f 66 20 69 6e 76  ** effect of inv
fba80 61 6c 69 64 61 74 69 6e 67 20 74 68 65 20 6a 6f  alidating the jo
fba90 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e 64 20 63  urnal file and c
fbaa0 6f 6d 6d 69 74 74 69 6e 67 20 74 68 65 0a 2a 2a  ommitting the.**
fbab0 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f   transaction..*/
fbac0 0a 73 74 61 74 69 63 20 69 6e 74 20 7a 65 72 6f  .static int zero
fbad0 4a 6f 75 72 6e 61 6c 48 64 72 28 50 61 67 65 72  JournalHdr(Pager
fbae0 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 64 6f   *pPager, int do
fbaf0 54 72 75 6e 63 61 74 65 29 7b 0a 20 20 69 6e 74  Truncate){.  int
fbb00 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
fbb10 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
fbb20 63 68 61 72 20 7a 65 72 6f 48 64 72 5b 32 38 5d  char zeroHdr[28]
fbb30 20 3d 20 7b 30 7d 3b 0a 0a 20 20 69 66 28 20 70   = {0};..  if( p
fbb40 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
fbb50 66 20 29 7b 0a 20 20 20 20 69 36 34 20 69 4c 69  f ){.    i64 iLi
fbb60 6d 69 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f  mit = pPager->jo
fbb70 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 0a  urnalSizeLimit;.
fbb80 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a  .    IOTRACE(("J
fbb90 5a 45 52 4f 48 44 52 20 25 70 5c 6e 22 2c 20 70  ZEROHDR %p\n", p
fbba0 50 61 67 65 72 29 29 0a 20 20 20 20 69 66 28 20  Pager)).    if( 
fbbb0 64 6f 54 72 75 6e 63 61 74 65 20 7c 7c 20 69 4c  doTruncate || iL
fbbc0 69 6d 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20  imit==0 ){.     
fbbd0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54   rc = sqlite3OsT
fbbe0 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e  runcate(pPager->
fbbf0 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c  jfd, 0);.    }el
fbc00 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  se{.      rc = s
fbc10 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50  qlite3OsWrite(pP
fbc20 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 65 72 6f 48  ager->jfd, zeroH
fbc30 64 72 2c 20 73 69 7a 65 6f 66 28 7a 65 72 6f 48  dr, sizeof(zeroH
fbc40 64 72 29 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20  dr), 0);.    }. 
fbc50 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
fbc60 45 5f 4f 4b 20 26 26 20 21 70 50 61 67 65 72 2d  E_OK && !pPager-
fbc70 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 20  >noSync ){.     
fbc80 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53   rc = sqlite3OsS
fbc90 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ync(pPager->jfd,
fbca0 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54   SQLITE_SYNC_DAT
fbcb0 41 4f 4e 4c 59 7c 70 50 61 67 65 72 2d 3e 73 79  AONLY|pPager->sy
fbcc0 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 20 20 20 7d  nc_flags);.    }
fbcd0 0a 0a 20 20 20 20 2f 2a 20 41 74 20 74 68 69 73  ..    /* At this
fbce0 20 70 6f 69 6e 74 20 74 68 65 20 74 72 61 6e 73   point the trans
fbcf0 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74  action is commit
fbd00 74 65 64 20 62 75 74 20 74 68 65 20 77 72 69 74  ted but the writ
fbd10 65 20 6c 6f 63 6b 20 0a 20 20 20 20 2a 2a 20 69  e lock .    ** i
fbd20 73 20 73 74 69 6c 6c 20 68 65 6c 64 20 6f 6e 20  s still held on 
fbd30 74 68 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65  the file. If the
fbd40 72 65 20 69 73 20 61 20 73 69 7a 65 20 6c 69 6d  re is a size lim
fbd50 69 74 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f  it configured fo
fbd60 72 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 65  r .    ** the pe
fbd70 72 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c  rsistent journal
fbd80 20 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c   and the journal
fbd90 20 66 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 20   file currently 
fbda0 63 6f 6e 73 75 6d 65 73 20 6d 6f 72 65 0a 20 20  consumes more.  
fbdb0 20 20 2a 2a 20 73 70 61 63 65 20 74 68 61 6e 20    ** space than 
fbdc0 74 68 61 74 20 6c 69 6d 69 74 20 61 6c 6c 6f 77  that limit allow
fbdd0 73 20 66 6f 72 2c 20 74 72 75 6e 63 61 74 65 20  s for, truncate 
fbde0 69 74 20 6e 6f 77 2e 20 54 68 65 72 65 20 69 73  it now. There is
fbdf0 20 6e 6f 20 6e 65 65 64 0a 20 20 20 20 2a 2a 20   no need.    ** 
fbe00 74 6f 20 73 79 6e 63 20 74 68 65 20 66 69 6c 65  to sync the file
fbe10 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20   following this 
fbe20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 20 20 20 2a  operation..    *
fbe30 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  /.    if( rc==SQ
fbe40 4c 49 54 45 5f 4f 4b 20 26 26 20 69 4c 69 6d 69  LITE_OK && iLimi
fbe50 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 36 34  t>0 ){.      i64
fbe60 20 73 7a 3b 0a 20 20 20 20 20 20 72 63 20 3d 20   sz;.      rc = 
fbe70 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a  sqlite3OsFileSiz
fbe80 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26  e(pPager->jfd, &
fbe90 73 7a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  sz);.      if( r
fbea0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
fbeb0 73 7a 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20  sz>iLimit ){.   
fbec0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
fbed0 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67  3OsTruncate(pPag
fbee0 65 72 2d 3e 6a 66 64 2c 20 69 4c 69 6d 69 74 29  er->jfd, iLimit)
fbef0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
fbf00 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
fbf10 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f  .}../*.** The jo
fbf20 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20  urnal file must 
fbf30 62 65 20 6f 70 65 6e 20 77 68 65 6e 20 74 68 69  be open when thi
fbf40 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
fbf50 6c 65 64 2e 20 41 20 6a 6f 75 72 6e 61 6c 0a 2a  led. A journal.*
fbf60 2a 20 68 65 61 64 65 72 20 28 4a 4f 55 52 4e 41  * header (JOURNA
fbf70 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65 73 29 20  L_HDR_SZ bytes) 
fbf80 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  is written into 
fbf90 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
fbfa0 20 61 74 20 74 68 65 0a 2a 2a 20 63 75 72 72 65   at the.** curre
fbfb0 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a  nt location..**.
fbfc0 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 66 6f  ** The format fo
fbfd0 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65  r the journal he
fbfe0 61 64 65 72 20 69 73 20 61 73 20 66 6f 6c 6c 6f  ader is as follo
fbff0 77 73 3a 0a 2a 2a 20 2d 20 38 20 62 79 74 65 73  ws:.** - 8 bytes
fc000 3a 20 4d 61 67 69 63 20 69 64 65 6e 74 69 66 79  : Magic identify
fc010 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d  ing journal form
fc020 61 74 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73  at..** - 4 bytes
fc030 3a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f  : Number of reco
fc040 72 64 73 20 69 6e 20 6a 6f 75 72 6e 61 6c 2c 20  rds in journal, 
fc050 6f 72 20 2d 31 20 6e 6f 2d 73 79 6e 63 20 6d 6f  or -1 no-sync mo
fc060 64 65 20 69 73 20 6f 6e 2e 0a 2a 2a 20 2d 20 34  de is on..** - 4
fc070 20 62 79 74 65 73 3a 20 52 61 6e 64 6f 6d 20 6e   bytes: Random n
fc080 75 6d 62 65 72 20 75 73 65 64 20 66 6f 72 20 70  umber used for p
fc090 61 67 65 20 68 61 73 68 2e 0a 2a 2a 20 2d 20 34  age hash..** - 4
fc0a0 20 62 79 74 65 73 3a 20 49 6e 69 74 69 61 6c 20   bytes: Initial 
fc0b0 64 61 74 61 62 61 73 65 20 70 61 67 65 20 63 6f  database page co
fc0c0 75 6e 74 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65  unt..** - 4 byte
fc0d0 73 3a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 75  s: Sector size u
fc0e0 73 65 64 20 62 79 20 74 68 65 20 70 72 6f 63 65  sed by the proce
fc0f0 73 73 20 74 68 61 74 20 77 72 6f 74 65 20 74 68  ss that wrote th
fc100 69 73 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 2d  is journal..** -
fc110 20 34 20 62 79 74 65 73 3a 20 44 61 74 61 62 61   4 bytes: Databa
fc120 73 65 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a  se page size..**
fc130 20 0a 2a 2a 20 46 6f 6c 6c 6f 77 65 64 20 62 79   .** Followed by
fc140 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a   (JOURNAL_HDR_SZ
fc150 20 2d 20 32 38 29 20 62 79 74 65 73 20 6f 66 20   - 28) bytes of 
fc160 75 6e 75 73 65 64 20 73 70 61 63 65 2e 0a 2a 2f  unused space..*/
fc170 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72 69 74  .static int writ
fc180 65 4a 6f 75 72 6e 61 6c 48 64 72 28 50 61 67 65  eJournalHdr(Page
fc190 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e  r *pPager){.  in
fc1a0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
fc1b0 3b 0a 20 20 63 68 61 72 20 2a 7a 48 65 61 64 65  ;.  char *zHeade
fc1c0 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70  r = pPager->pTmp
fc1d0 53 70 61 63 65 3b 0a 20 20 69 6e 74 20 6e 48 65  Space;.  int nHe
fc1e0 61 64 65 72 20 3d 20 70 50 61 67 65 72 2d 3e 70  ader = pPager->p
fc1f0 61 67 65 53 69 7a 65 3b 0a 20 20 69 6e 74 20 6e  ageSize;.  int n
fc200 57 72 69 74 65 3b 0a 0a 20 20 69 66 28 20 6e 48  Write;..  if( nH
fc210 65 61 64 65 72 3e 4a 4f 55 52 4e 41 4c 5f 48 44  eader>JOURNAL_HD
fc220 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0a  R_SZ(pPager) ){.
fc230 20 20 20 20 6e 48 65 61 64 65 72 20 3d 20 4a 4f      nHeader = JO
fc240 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61  URNAL_HDR_SZ(pPa
fc250 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  ger);.  }..  if(
fc260 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 48 64 72   pPager->stmtHdr
fc270 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 70 50  Off==0 ){.    pP
fc280 61 67 65 72 2d 3e 73 74 6d 74 48 64 72 4f 66 66  ager->stmtHdrOff
fc290 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e   = pPager->journ
fc2a0 61 6c 4f 66 66 3b 0a 20 20 7d 0a 0a 20 20 73 65  alOff;.  }..  se
fc2b0 65 6b 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61  ekJournalHdr(pPa
fc2c0 67 65 72 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e  ger);.  pPager->
fc2d0 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 70 50 61  journalHdr = pPa
fc2e0 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b  ger->journalOff;
fc2f0 0a 0a 20 20 6d 65 6d 63 70 79 28 7a 48 65 61 64  ..  memcpy(zHead
fc300 65 72 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  er, aJournalMagi
fc310 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e  c, sizeof(aJourn
fc320 61 6c 4d 61 67 69 63 29 29 3b 0a 0a 20 20 2f 2a  alMagic));..  /*
fc330 20 0a 20 20 2a 2a 20 57 72 69 74 65 20 74 68 65   .  ** Write the
fc340 20 6e 52 65 63 20 46 69 65 6c 64 20 2d 20 74 68   nRec Field - th
fc350 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
fc360 20 72 65 63 6f 72 64 73 20 74 68 61 74 20 66 6f   records that fo
fc370 6c 6c 6f 77 20 74 68 69 73 0a 20 20 2a 2a 20 6a  llow this.  ** j
fc380 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 4e  ournal header. N
fc390 6f 72 6d 61 6c 6c 79 2c 20 7a 65 72 6f 20 69 73  ormally, zero is
fc3a0 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 69 73   written to this
fc3b0 20 76 61 6c 75 65 20 61 74 20 74 68 69 73 20 74   value at this t
fc3c0 69 6d 65 2e 0a 20 20 2a 2a 20 41 66 74 65 72 20  ime..  ** After 
fc3d0 74 68 65 20 72 65 63 6f 72 64 73 20 61 72 65 20  the records are 
fc3e0 61 64 64 65 64 20 74 6f 20 74 68 65 20 6a 6f 75  added to the jou
fc3f0 72 6e 61 6c 20 28 61 6e 64 20 74 68 65 20 6a 6f  rnal (and the jo
fc400 75 72 6e 61 6c 20 73 79 6e 63 65 64 2c 20 0a 20  urnal synced, . 
fc410 20 2a 2a 20 69 66 20 69 6e 20 66 75 6c 6c 2d 73   ** if in full-s
fc420 79 6e 63 20 6d 6f 64 65 29 2c 20 74 68 65 20 7a  ync mode), the z
fc430 65 72 6f 20 69 73 20 6f 76 65 72 77 72 69 74 74  ero is overwritt
fc440 65 6e 20 77 69 74 68 20 74 68 65 20 74 72 75 65  en with the true
fc450 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 6f 66 20   number.  ** of 
fc460 72 65 63 6f 72 64 73 20 28 73 65 65 20 73 79 6e  records (see syn
fc470 63 4a 6f 75 72 6e 61 6c 28 29 29 2e 0a 20 20 2a  cJournal())..  *
fc480 2a 0a 20 20 2a 2a 20 41 20 66 61 73 74 65 72 20  *.  ** A faster 
fc490 61 6c 74 65 72 6e 61 74 69 76 65 20 69 73 20 74  alternative is t
fc4a0 6f 20 77 72 69 74 65 20 30 78 46 46 46 46 46 46  o write 0xFFFFFF
fc4b0 46 46 20 74 6f 20 74 68 65 20 6e 52 65 63 20 66  FF to the nRec f
fc4c0 69 65 6c 64 2e 20 57 68 65 6e 0a 20 20 2a 2a 20  ield. When.  ** 
fc4d0 72 65 61 64 69 6e 67 20 74 68 65 20 6a 6f 75 72  reading the jour
fc4e0 6e 61 6c 20 74 68 69 73 20 76 61 6c 75 65 20 74  nal this value t
fc4f0 65 6c 6c 73 20 53 51 4c 69 74 65 20 74 6f 20 61  ells SQLite to a
fc500 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 0a 20  ssume that the. 
fc510 20 2a 2a 20 72 65 73 74 20 6f 66 20 74 68 65 20   ** rest of the 
fc520 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 6f 6e  journal file con
fc530 74 61 69 6e 73 20 76 61 6c 69 64 20 70 61 67 65  tains valid page
fc540 20 72 65 63 6f 72 64 73 2e 20 54 68 69 73 20 61   records. This a
fc550 73 73 75 6d 70 74 69 6f 6e 0a 20 20 2a 2a 20 69  ssumption.  ** i
fc560 73 20 64 61 6e 67 65 72 6f 75 73 2c 20 61 73 20  s dangerous, as 
fc570 69 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63 63  if a failure occ
fc580 75 72 65 64 20 77 68 69 6c 73 74 20 77 72 69 74  ured whilst writ
fc590 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ing to the journ
fc5a0 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 74 20  al.  ** file it 
fc5b0 6d 61 79 20 63 6f 6e 74 61 69 6e 20 73 6f 6d 65  may contain some
fc5c0 20 67 61 72 62 61 67 65 20 64 61 74 61 2e 20 54   garbage data. T
fc5d0 68 65 72 65 20 61 72 65 20 74 77 6f 20 73 63 65  here are two sce
fc5e0 6e 61 72 69 6f 73 0a 20 20 2a 2a 20 77 68 65 72  narios.  ** wher
fc5f0 65 20 74 68 69 73 20 72 69 73 6b 20 63 61 6e 20  e this risk can 
fc600 62 65 20 69 67 6e 6f 72 65 64 3a 0a 20 20 2a 2a  be ignored:.  **
fc610 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74  .  **   * When t
fc620 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 6e  he pager is in n
fc630 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e 20 43 6f 72  o-sync mode. Cor
fc640 72 75 70 74 69 6f 6e 20 63 61 6e 20 66 6f 6c 6c  ruption can foll
fc650 6f 77 20 61 0a 20 20 2a 2a 20 20 20 20 20 70 6f  ow a.  **     po
fc660 77 65 72 20 66 61 69 6c 75 72 65 20 69 6e 20 74  wer failure in t
fc670 68 69 73 20 63 61 73 65 20 61 6e 79 77 61 79 2e  his case anyway.
fc680 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57  .  **.  **   * W
fc690 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 49  hen the SQLITE_I
fc6a0 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44  OCAP_SAFE_APPEND
fc6b0 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 54 68   flag is set. Th
fc6c0 69 73 20 67 75 61 72 61 6e 74 65 65 73 0a 20 20  is guarantees.  
fc6d0 2a 2a 20 20 20 20 20 74 68 61 74 20 67 61 72 62  **     that garb
fc6e0 61 67 65 20 64 61 74 61 20 69 73 20 6e 65 76 65  age data is neve
fc6f0 72 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68  r appended to th
fc700 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a  e journal file..
fc710 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 70 50    */.  assert(pP
fc720 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f  ager->fd->pMetho
fc730 64 73 7c 7c 70 50 61 67 65 72 2d 3e 6e 6f 53 79  ds||pPager->noSy
fc740 6e 63 29 3b 0a 20 20 69 66 28 20 28 70 50 61 67  nc);.  if( (pPag
fc750 65 72 2d 3e 6e 6f 53 79 6e 63 29 20 7c 7c 20 28  er->noSync) || (
fc760 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
fc770 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
fc780 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 0a 20  ALMODE_MEMORY). 
fc790 20 20 7c 7c 20 28 73 71 6c 69 74 65 33 4f 73 44    || (sqlite3OsD
fc7a0 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73  eviceCharacteris
fc7b0 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29  tics(pPager->fd)
fc7c0 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41  &SQLITE_IOCAP_SA
fc7d0 46 45 5f 41 50 50 45 4e 44 29 20 0a 20 20 29 7b  FE_APPEND) .  ){
fc7e0 0a 20 20 20 20 70 75 74 33 32 62 69 74 73 28 26  .    put32bits(&
fc7f0 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61  zHeader[sizeof(a
fc800 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c 20  JournalMagic)], 
fc810 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 7d  0xffffffff);.  }
fc820 65 6c 73 65 7b 0a 20 20 20 20 70 75 74 33 32 62  else{.    put32b
fc830 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a  its(&zHeader[siz
fc840 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  eof(aJournalMagi
fc850 63 29 5d 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20  c)], 0);.  }..  
fc860 2f 2a 20 54 68 65 20 72 61 6e 64 6f 6d 20 63 68  /* The random ch
fc870 65 63 6b 2d 68 61 73 68 20 69 6e 69 74 69 61 6c  eck-hash initial
fc880 69 73 65 72 20 2a 2f 20 0a 20 20 73 71 6c 69 74  iser */ .  sqlit
fc890 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69  e3_randomness(si
fc8a0 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 63 6b 73  zeof(pPager->cks
fc8b0 75 6d 49 6e 69 74 29 2c 20 26 70 50 61 67 65 72  umInit), &pPager
fc8c0 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20  ->cksumInit);.  
fc8d0 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64  put32bits(&zHead
fc8e0 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e  er[sizeof(aJourn
fc8f0 61 6c 4d 61 67 69 63 29 2b 34 5d 2c 20 70 50 61  alMagic)+4], pPa
fc900 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b  ger->cksumInit);
fc910 0a 20 20 2f 2a 20 54 68 65 20 69 6e 69 74 69 61  .  /* The initia
fc920 6c 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20  l database size 
fc930 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 26  */.  put32bits(&
fc940 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61  zHeader[sizeof(a
fc950 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 38 5d  JournalMagic)+8]
fc960 2c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  , pPager->dbSize
fc970 29 3b 0a 20 20 2f 2a 20 54 68 65 20 61 73 73 75  );.  /* The assu
fc980 6d 65 64 20 73 65 63 74 6f 72 20 73 69 7a 65 20  med sector size 
fc990 66 6f 72 20 74 68 69 73 20 70 72 6f 63 65 73 73  for this process
fc9a0 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28   */.  put32bits(
fc9b0 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28  &zHeader[sizeof(
fc9c0 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31  aJournalMagic)+1
fc9d0 32 5d 2c 20 70 50 61 67 65 72 2d 3e 73 65 63 74  2], pPager->sect
fc9e0 6f 72 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 70  orSize);.  if( p
fc9f0 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64  Pager->journalHd
fca00 72 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54  r==0 ){.    /* T
fca10 68 65 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a  he page size */.
fca20 20 20 20 20 70 75 74 33 32 62 69 74 73 28 26 7a      put32bits(&z
fca30 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a  Header[sizeof(aJ
fca40 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31 36 5d  ournalMagic)+16]
fca50 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  , pPager->pageSi
fca60 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28  ze);.  }..  for(
fca70 6e 57 72 69 74 65 3d 30 3b 20 72 63 3d 3d 53 51  nWrite=0; rc==SQ
fca80 4c 49 54 45 5f 4f 4b 26 26 6e 57 72 69 74 65 3c  LITE_OK&&nWrite<
fca90 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70  JOURNAL_HDR_SZ(p
fcaa0 50 61 67 65 72 29 3b 20 6e 57 72 69 74 65 2b 3d  Pager); nWrite+=
fcab0 6e 48 65 61 64 65 72 29 7b 0a 20 20 20 20 49 4f  nHeader){.    IO
fcac0 54 52 41 43 45 28 28 22 4a 48 44 52 20 25 70 20  TRACE(("JHDR %p 
fcad0 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61 67  %lld %d\n", pPag
fcae0 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  er, pPager->jour
fcaf0 6e 61 6c 48 64 72 2c 20 6e 48 65 61 64 65 72 29  nalHdr, nHeader)
fcb00 29 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ).    rc = sqlit
fcb10 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72  e3OsWrite(pPager
fcb20 2d 3e 6a 66 64 2c 20 7a 48 65 61 64 65 72 2c 20  ->jfd, zHeader, 
fcb30 6e 48 65 61 64 65 72 2c 20 70 50 61 67 65 72 2d  nHeader, pPager-
fcb40 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a 20 20  >journalOff);.  
fcb50 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
fcb60 6c 4f 66 66 20 2b 3d 20 6e 48 65 61 64 65 72 3b  lOff += nHeader;
fcb70 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
fcb80 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  c;.}../*.** The 
fcb90 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73  journal file mus
fcba0 74 20 62 65 20 6f 70 65 6e 20 77 68 65 6e 20 74  t be open when t
fcbb0 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2e 20 41  his is called. A
fcbc0 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20   journal header 
fcbd0 66 69 6c 65 0a 2a 2a 20 28 4a 4f 55 52 4e 41 4c  file.** (JOURNAL
fcbe0 5f 48 44 52 5f 53 5a 20 62 79 74 65 73 29 20 69  _HDR_SZ bytes) i
fcbf0 73 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20  s read from the 
fcc00 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e  current location
fcc10 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a   in the journal.
fcc20 2a 2a 20 66 69 6c 65 2e 20 53 65 65 20 63 6f 6d  ** file. See com
fcc30 6d 65 6e 74 73 20 61 62 6f 76 65 20 66 75 6e 63  ments above func
fcc40 74 69 6f 6e 20 77 72 69 74 65 4a 6f 75 72 6e 61  tion writeJourna
fcc50 6c 48 64 72 28 29 20 66 6f 72 20 61 20 64 65 73  lHdr() for a des
fcc60 63 72 69 70 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74  cription of.** t
fcc70 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  he journal heade
fcc80 72 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20  r format..**.** 
fcc90 49 66 20 74 68 65 20 68 65 61 64 65 72 20 69 73  If the header is
fcca0 20 72 65 61 64 20 73 75 63 63 65 73 73 66 75 6c   read successful
fccb0 6c 79 2c 20 2a 6e 52 65 63 20 69 73 20 73 65 74  ly, *nRec is set
fccc0 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   to the number o
fccd0 66 0a 2a 2a 20 70 61 67 65 20 72 65 63 6f 72 64  f.** page record
fcce0 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73  s following this
fccf0 20 68 65 61 64 65 72 20 61 6e 64 20 2a 64 62 53   header and *dbS
fcd00 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20 74 68  ize is set to th
fcd10 65 20 73 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a  e size of the.**
fcd20 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65   database before
fcd30 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
fcd40 20 62 65 67 61 6e 2c 20 69 6e 20 70 61 67 65 73   began, in pages
fcd50 2e 20 41 6c 73 6f 2c 20 70 50 61 67 65 72 2d 3e  . Also, pPager->
fcd60 63 6b 73 75 6d 49 6e 69 74 0a 2a 2a 20 69 73 20  cksumInit.** is 
fcd70 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65  set to the value
fcd80 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a   read from the j
fcd90 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 53  ournal header. S
fcda0 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
fcdb0 72 6e 65 64 0a 2a 2a 20 69 6e 20 74 68 69 73 20  rned.** in this 
fcdc0 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  case..**.** If t
fcdd0 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  he journal heade
fcde0 72 20 66 69 6c 65 20 61 70 70 65 61 72 73 20 74  r file appears t
fcdf0 6f 20 62 65 20 63 6f 72 72 75 70 74 65 64 2c 20  o be corrupted, 
fce00 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 0a 2a  SQLITE_DONE is.*
fce10 2a 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a  * returned and *
fce20 6e 52 65 63 20 61 6e 64 20 2a 64 62 53 69 7a 65  nRec and *dbSize
fce30 20 61 72 65 20 6e 6f 74 20 73 65 74 2e 20 20 49   are not set.  I
fce40 66 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a  f JOURNAL_HDR_SZ
fce50 20 62 79 74 65 73 0a 2a 2a 20 63 61 6e 6e 6f 74   bytes.** cannot
fce60 20 62 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68   be read from th
fce70 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61  e journal file a
fce80 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  n error code is 
fce90 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61  returned..*/.sta
fcea0 74 69 63 20 69 6e 74 20 72 65 61 64 4a 6f 75 72  tic int readJour
fceb0 6e 61 6c 48 64 72 28 0a 20 20 50 61 67 65 72 20  nalHdr(.  Pager 
fcec0 2a 70 50 61 67 65 72 2c 20 0a 20 20 69 36 34 20  *pPager, .  i64 
fced0 6a 6f 75 72 6e 61 6c 53 69 7a 65 2c 0a 20 20 75  journalSize,.  u
fcee0 33 32 20 2a 70 4e 52 65 63 2c 20 0a 20 20 75 33  32 *pNRec, .  u3
fcef0 32 20 2a 70 44 62 53 69 7a 65 0a 29 7b 0a 20 20  2 *pDbSize.){.  
fcf00 69 6e 74 20 72 63 3b 0a 20 20 75 6e 73 69 67 6e  int rc;.  unsign
fcf10 65 64 20 63 68 61 72 20 61 4d 61 67 69 63 5b 38  ed char aMagic[8
fcf20 5d 3b 20 2f 2a 20 41 20 62 75 66 66 65 72 20 74  ]; /* A buffer t
fcf30 6f 20 68 6f 6c 64 20 74 68 65 20 6d 61 67 69 63  o hold the magic
fcf40 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 36 34   header */.  i64
fcf50 20 6a 72 6e 6c 4f 66 66 3b 0a 20 20 69 6e 74 20   jrnlOff;.  int 
fcf60 69 50 61 67 65 53 69 7a 65 3b 0a 0a 20 20 73 65  iPageSize;..  se
fcf70 65 6b 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61  ekJournalHdr(pPa
fcf80 67 65 72 29 3b 0a 20 20 69 66 28 20 70 50 61 67  ger);.  if( pPag
fcf90 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2b 4a  er->journalOff+J
fcfa0 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
fcfb0 61 67 65 72 29 20 3e 20 6a 6f 75 72 6e 61 6c 53  ager) > journalS
fcfc0 69 7a 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ize ){.    retur
fcfd0 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  n SQLITE_DONE;. 
fcfe0 20 7d 0a 20 20 6a 72 6e 6c 4f 66 66 20 3d 20 70   }.  jrnlOff = p
fcff0 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
fd000 66 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  f;..  rc = sqlit
fd010 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d  e3OsRead(pPager-
fd020 3e 6a 66 64 2c 20 61 4d 61 67 69 63 2c 20 73 69  >jfd, aMagic, si
fd030 7a 65 6f 66 28 61 4d 61 67 69 63 29 2c 20 6a 72  zeof(aMagic), jr
fd040 6e 6c 4f 66 66 29 3b 0a 20 20 69 66 28 20 72 63  nlOff);.  if( rc
fd050 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
fd060 6a 72 6e 6c 4f 66 66 20 2b 3d 20 73 69 7a 65 6f  jrnlOff += sizeo
fd070 66 28 61 4d 61 67 69 63 29 3b 0a 0a 20 20 69 66  f(aMagic);..  if
fd080 28 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c  ( memcmp(aMagic,
fd090 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20   aJournalMagic, 
fd0a0 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 29 21  sizeof(aMagic))!
fd0b0 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
fd0c0 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20   SQLITE_DONE;.  
fd0d0 7d 0a 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32  }..  rc = read32
fd0e0 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64  bits(pPager->jfd
fd0f0 2c 20 6a 72 6e 6c 4f 66 66 2c 20 70 4e 52 65 63  , jrnlOff, pNRec
fd100 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  );.  if( rc ) re
fd110 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
fd120 20 72 65 61 64 33 32 62 69 74 73 28 70 50 61 67   read32bits(pPag
fd130 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f 66 66  er->jfd, jrnlOff
fd140 2b 34 2c 20 26 70 50 61 67 65 72 2d 3e 63 6b 73  +4, &pPager->cks
fd150 75 6d 49 6e 69 74 29 3b 0a 20 20 69 66 28 20 72  umInit);.  if( r
fd160 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  c ) return rc;..
fd170 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 69 74    rc = read32bit
fd180 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 6a  s(pPager->jfd, j
fd190 72 6e 6c 4f 66 66 2b 38 2c 20 70 44 62 53 69 7a  rnlOff+8, pDbSiz
fd1a0 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  e);.  if( rc ) r
fd1b0 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
fd1c0 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 50 61  = read32bits(pPa
fd1d0 67 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f 66  ger->jfd, jrnlOf
fd1e0 66 2b 31 36 2c 20 28 75 33 32 20 2a 29 26 69 50  f+16, (u32 *)&iP
fd1f0 61 67 65 53 69 7a 65 29 3b 0a 20 20 69 66 28 20  ageSize);.  if( 
fd200 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 0a 20  rc==SQLITE_OK . 
fd210 20 20 26 26 20 69 50 61 67 65 53 69 7a 65 3e 3d    && iPageSize>=
fd220 35 31 32 20 0a 20 20 20 26 26 20 69 50 61 67 65  512 .   && iPage
fd230 53 69 7a 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 58  Size<=SQLITE_MAX
fd240 5f 50 41 47 45 5f 53 49 5a 45 20 0a 20 20 20 26  _PAGE_SIZE .   &
fd250 26 20 28 28 69 50 61 67 65 53 69 7a 65 2d 31 29  & ((iPageSize-1)
fd260 26 69 50 61 67 65 53 69 7a 65 29 3d 3d 30 20 0a  &iPageSize)==0 .
fd270 20 20 29 7b 0a 20 20 20 20 75 31 36 20 70 61 67    ){.    u16 pag
fd280 65 73 69 7a 65 20 3d 20 69 50 61 67 65 53 69 7a  esize = iPageSiz
fd290 65 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  e;.    rc = sqli
fd2a0 74 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73  te3PagerSetPages
fd2b0 69 7a 65 28 70 50 61 67 65 72 2c 20 26 70 61 67  ize(pPager, &pag
fd2c0 65 73 69 7a 65 29 3b 0a 20 20 7d 0a 20 20 69 66  esize);.  }.  if
fd2d0 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
fd2e0 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74  ;..  /* Update t
fd2f0 68 65 20 61 73 73 75 6d 65 64 20 73 65 63 74 6f  he assumed secto
fd300 72 2d 73 69 7a 65 20 74 6f 20 6d 61 74 63 68 20  r-size to match 
fd310 74 68 65 20 76 61 6c 75 65 20 75 73 65 64 20 62  the value used b
fd320 79 20 0a 20 20 2a 2a 20 74 68 65 20 70 72 6f 63  y .  ** the proc
fd330 65 73 73 20 74 68 61 74 20 63 72 65 61 74 65 64  ess that created
fd340 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e 20 49   this journal. I
fd350 66 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77  f this journal w
fd360 61 73 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20  as.  ** created 
fd370 62 79 20 61 20 70 72 6f 63 65 73 73 20 6f 74 68  by a process oth
fd380 65 72 20 74 68 61 6e 20 74 68 69 73 20 6f 6e 65  er than this one
fd390 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74  , then this rout
fd3a0 69 6e 65 0a 20 20 2a 2a 20 69 73 20 62 65 69 6e  ine.  ** is bein
fd3b0 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69  g called from wi
fd3c0 74 68 69 6e 20 70 61 67 65 72 5f 70 6c 61 79 62  thin pager_playb
fd3d0 61 63 6b 28 29 2e 20 54 68 65 20 6c 6f 63 61 6c  ack(). The local
fd3e0 20 76 61 6c 75 65 0a 20 20 2a 2a 20 6f 66 20 50   value.  ** of P
fd3f0 61 67 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20  ager.sectorSize 
fd400 69 73 20 72 65 73 74 6f 72 65 64 20 61 74 20 74  is restored at t
fd410 68 65 20 65 6e 64 20 6f 66 20 74 68 61 74 20 72  he end of that r
fd420 6f 75 74 69 6e 65 2e 0a 20 20 2a 2f 0a 20 20 72  outine..  */.  r
fd430 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70  c = read32bits(p
fd440 50 61 67 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c  Pager->jfd, jrnl
fd450 4f 66 66 2b 31 32 2c 20 26 70 50 61 67 65 72 2d  Off+12, &pPager-
fd460 3e 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a 20 20  >sectorSize);.  
fd470 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
fd480 72 63 3b 0a 20 20 69 66 28 20 28 70 50 61 67 65  rc;.  if( (pPage
fd490 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 26 20  r->sectorSize & 
fd4a0 28 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53  (pPager->sectorS
fd4b0 69 7a 65 2d 31 29 29 21 3d 30 0a 20 20 20 20 20  ize-1))!=0.     
fd4c0 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73      || pPager->s
fd4d0 65 63 74 6f 72 53 69 7a 65 3e 30 78 31 30 30 30  ectorSize>0x1000
fd4e0 30 30 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  000 ){.    retur
fd4f0 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  n SQLITE_DONE;. 
fd500 20 7d 0a 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f   }..  pPager->jo
fd510 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 4a 4f 55 52  urnalOff += JOUR
fd520 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
fd530 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  r);.  return SQL
fd540 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  ITE_OK;.}.../*.*
fd550 2a 20 57 72 69 74 65 20 74 68 65 20 73 75 70 70  * Write the supp
fd560 6c 69 65 64 20 6d 61 73 74 65 72 20 6a 6f 75 72  lied master jour
fd570 6e 61 6c 20 6e 61 6d 65 20 69 6e 74 6f 20 74 68  nal name into th
fd580 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66  e journal file f
fd590 6f 72 20 70 61 67 65 72 0a 2a 2a 20 70 50 61 67  or pager.** pPag
fd5a0 65 72 20 61 74 20 74 68 65 20 63 75 72 72 65 6e  er at the curren
fd5b0 74 20 6c 6f 63 61 74 69 6f 6e 2e 20 54 68 65 20  t location. The 
fd5c0 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e  master journal n
fd5d0 61 6d 65 20 6d 75 73 74 20 62 65 20 74 68 65 20  ame must be the 
fd5e0 6c 61 73 74 0a 2a 2a 20 74 68 69 6e 67 20 77 72  last.** thing wr
fd5f0 69 74 74 65 6e 20 74 6f 20 61 20 6a 6f 75 72 6e  itten to a journ
fd600 61 6c 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20  al file. If the 
fd610 70 61 67 65 72 20 69 73 20 69 6e 20 66 75 6c 6c  pager is in full
fd620 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 74 68 65 0a  -sync mode, the.
fd630 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ** journal file 
fd640 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 64  descriptor is ad
fd650 76 61 6e 63 65 64 20 74 6f 20 74 68 65 20 6e 65  vanced to the ne
fd660 78 74 20 73 65 63 74 6f 72 20 62 6f 75 6e 64 61  xt sector bounda
fd670 72 79 20 62 65 66 6f 72 65 0a 2a 2a 20 61 6e 79  ry before.** any
fd680 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e  thing is written
fd690 2e 20 54 68 65 20 66 6f 72 6d 61 74 20 69 73 3a  . The format is:
fd6a0 0a 2a 2a 0a 2a 2a 20 2b 20 34 20 62 79 74 65 73  .**.** + 4 bytes
fd6b0 3a 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 2e  : PAGER_MJ_PGNO.
fd6c0 0a 2a 2a 20 2b 20 4e 20 62 79 74 65 73 3a 20 6c  .** + N bytes: l
fd6d0 65 6e 67 74 68 20 6f 66 20 6d 61 73 74 65 72 20  ength of master 
fd6e0 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e 0a 2a 2a  journal name..**
fd6f0 20 2b 20 34 20 62 79 74 65 73 3a 20 4e 0a 2a 2a   + 4 bytes: N.**
fd700 20 2b 20 34 20 62 79 74 65 73 3a 20 4d 61 73 74   + 4 bytes: Mast
fd710 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20  er journal name 
fd720 63 68 65 63 6b 73 75 6d 2e 0a 2a 2a 20 2b 20 38  checksum..** + 8
fd730 20 62 79 74 65 73 3a 20 61 4a 6f 75 72 6e 61 6c   bytes: aJournal
fd740 4d 61 67 69 63 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 54  Magic[]..**.** T
fd750 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
fd760 6c 20 70 61 67 65 20 63 68 65 63 6b 73 75 6d 20  l page checksum 
fd770 69 73 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68  is the sum of th
fd780 65 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6d  e bytes in the m
fd790 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c  aster.** journal
fd7a0 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   name..**.** If 
fd7b0 7a 4d 61 73 74 65 72 20 69 73 20 61 20 4e 55 4c  zMaster is a NUL
fd7c0 4c 20 70 6f 69 6e 74 65 72 20 28 6f 63 63 75 72  L pointer (occur
fd7d0 73 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 64  s for a single d
fd7e0 61 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 74  atabase transact
fd7f0 69 6f 6e 29 2c 20 0a 2a 2a 20 74 68 69 73 20 63  ion), .** this c
fd800 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  all is a no-op..
fd810 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72  */.static int wr
fd820 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  iteMasterJournal
fd830 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
fd840 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73  const char *zMas
fd850 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  ter){.  int rc;.
fd860 20 20 69 6e 74 20 6c 65 6e 3b 20 0a 20 20 69 6e    int len; .  in
fd870 74 20 69 3b 20 0a 20 20 69 36 34 20 6a 72 6e 6c  t i; .  i64 jrnl
fd880 4f 66 66 3b 0a 20 20 69 36 34 20 6a 72 6e 6c 53  Off;.  i64 jrnlS
fd890 69 7a 65 3b 0a 20 20 75 33 32 20 63 6b 73 75 6d  ize;.  u32 cksum
fd8a0 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 7a 42 75   = 0;.  char zBu
fd8b0 66 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61  f[sizeof(aJourna
fd8c0 6c 4d 61 67 69 63 29 2b 32 2a 34 5d 3b 0a 0a 20  lMagic)+2*4];.. 
fd8d0 20 69 66 28 20 21 7a 4d 61 73 74 65 72 20 7c 7c   if( !zMaster ||
fd8e0 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74   pPager->setMast
fd8f0 65 72 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  er ) return SQLI
fd900 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 50 61  TE_OK;.  if( pPa
fd910 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
fd920 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
fd930 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 20 72 65 74  ODE_MEMORY ) ret
fd940 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
fd950 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74   pPager->setMast
fd960 65 72 20 3d 20 31 3b 0a 0a 20 20 6c 65 6e 20 3d  er = 1;..  len =
fd970 20 73 74 72 6c 65 6e 28 7a 4d 61 73 74 65 72 29   strlen(zMaster)
fd980 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6c  ;.  for(i=0; i<l
fd990 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 6b  en; i++){.    ck
fd9a0 73 75 6d 20 2b 3d 20 7a 4d 61 73 74 65 72 5b 69  sum += zMaster[i
fd9b0 5d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  ];.  }..  /* If 
fd9c0 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64  in full-sync mod
fd9d0 65 2c 20 61 64 76 61 6e 63 65 20 74 6f 20 74 68  e, advance to th
fd9e0 65 20 6e 65 78 74 20 64 69 73 6b 20 73 65 63 74  e next disk sect
fd9f0 6f 72 20 62 65 66 6f 72 65 20 77 72 69 74 69 6e  or before writin
fda00 67 0a 20 20 2a 2a 20 74 68 65 20 6d 61 73 74 65  g.  ** the maste
fda10 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e 20  r journal name. 
fda20 54 68 69 73 20 69 73 20 69 6e 20 63 61 73 65 20  This is in case 
fda30 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 61 67  the previous pag
fda40 65 20 77 72 69 74 74 65 6e 20 74 6f 0a 20 20 2a  e written to.  *
fda50 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 61  * the journal ha
fda60 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 73  s already been s
fda70 79 6e 63 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  ynced..  */.  if
fda80 28 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79  ( pPager->fullSy
fda90 6e 63 20 29 7b 0a 20 20 20 20 73 65 65 6b 4a 6f  nc ){.    seekJo
fdaa0 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29  urnalHdr(pPager)
fdab0 3b 0a 20 20 7d 0a 20 20 6a 72 6e 6c 4f 66 66 20  ;.  }.  jrnlOff 
fdac0 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  = pPager->journa
fdad0 6c 4f 66 66 3b 0a 20 20 70 50 61 67 65 72 2d 3e  lOff;.  pPager->
fdae0 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 28 6c  journalOff += (l
fdaf0 65 6e 2b 32 30 29 3b 0a 0a 20 20 72 63 20 3d 20  en+20);..  rc = 
fdb00 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67  write32bits(pPag
fdb10 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f 66 66  er->jfd, jrnlOff
fdb20 2c 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28  , PAGER_MJ_PGNO(
fdb30 70 50 61 67 65 72 29 29 3b 0a 20 20 69 66 28 20  pPager));.  if( 
fdb40 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
fdb50 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 6a 72 6e  return rc;.  jrn
fdb60 6c 4f 66 66 20 2b 3d 20 34 3b 0a 0a 20 20 72 63  lOff += 4;..  rc
fdb70 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
fdb80 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a  e(pPager->jfd, z
fdb90 4d 61 73 74 65 72 2c 20 6c 65 6e 2c 20 6a 72 6e  Master, len, jrn
fdba0 6c 4f 66 66 29 3b 0a 20 20 69 66 28 20 72 63 21  lOff);.  if( rc!
fdbb0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
fdbc0 75 72 6e 20 72 63 3b 0a 20 20 6a 72 6e 6c 4f 66  urn rc;.  jrnlOf
fdbd0 66 20 2b 3d 20 6c 65 6e 3b 0a 0a 20 20 70 75 74  f += len;..  put
fdbe0 33 32 62 69 74 73 28 7a 42 75 66 2c 20 6c 65 6e  32bits(zBuf, len
fdbf0 29 3b 0a 20 20 70 75 74 33 32 62 69 74 73 28 26  );.  put32bits(&
fdc00 7a 42 75 66 5b 34 5d 2c 20 63 6b 73 75 6d 29 3b  zBuf[4], cksum);
fdc10 0a 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b  .  memcpy(&zBuf[
fdc20 38 5d 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  8], aJournalMagi
fdc30 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e  c, sizeof(aJourn
fdc40 61 6c 4d 61 67 69 63 29 29 3b 0a 20 20 72 63 20  alMagic));.  rc 
fdc50 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65  = sqlite3OsWrite
fdc60 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 42  (pPager->jfd, zB
fdc70 75 66 2c 20 38 2b 73 69 7a 65 6f 66 28 61 4a 6f  uf, 8+sizeof(aJo
fdc80 75 72 6e 61 6c 4d 61 67 69 63 29 2c 20 6a 72 6e  urnalMagic), jrn
fdc90 6c 4f 66 66 29 3b 0a 20 20 6a 72 6e 6c 4f 66 66  lOff);.  jrnlOff
fdca0 20 2b 3d 20 38 2b 73 69 7a 65 6f 66 28 61 4a 6f   += 8+sizeof(aJo
fdcb0 75 72 6e 61 6c 4d 61 67 69 63 29 3b 0a 20 20 70  urnalMagic);.  p
fdcc0 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20  Pager->needSync 
fdcd0 3d 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  = !pPager->noSyn
fdce0 63 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  c;..  /* If the 
fdcf0 70 61 67 65 72 20 69 73 20 69 6e 20 70 65 72 69  pager is in peri
fdd00 73 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c 20 6d 6f  stent-journal mo
fdd10 64 65 2c 20 74 68 65 6e 20 74 68 65 20 70 68 79  de, then the phy
fdd20 73 69 63 61 6c 20 0a 20 20 2a 2a 20 6a 6f 75 72  sical .  ** jour
fdd30 6e 61 6c 2d 66 69 6c 65 20 6d 61 79 20 65 78 74  nal-file may ext
fdd40 65 6e 64 20 70 61 73 74 20 74 68 65 20 65 6e 64  end past the end
fdd50 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 2d 6a   of the master-j
fdd60 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a 20 20 2a 2a  ournal name.  **
fdd70 20 61 6e 64 20 38 20 62 79 74 65 73 20 6f 66 20   and 8 bytes of 
fdd80 6d 61 67 69 63 20 64 61 74 61 20 6a 75 73 74 20  magic data just 
fdd90 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 66  written to the f
fdda0 69 6c 65 2e 20 54 68 69 73 20 69 73 20 0a 20 20  ile. This is .  
fddb0 2a 2a 20 64 61 6e 67 65 72 6f 75 73 20 62 65 63  ** dangerous bec
fddc0 61 75 73 65 20 74 68 65 20 63 6f 64 65 20 74 6f  ause the code to
fddd0 20 72 6f 6c 6c 62 61 63 6b 20 61 20 68 6f 74 2d   rollback a hot-
fdde0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a  journal file.  *
fddf0 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62  * will not be ab
fde00 6c 65 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6d  le to find the m
fde10 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e 61  aster-journal na
fde20 6d 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  me to determine 
fde30 0a 20 20 2a 2a 20 77 68 65 74 68 65 72 20 6f 72  .  ** whether or
fde40 20 6e 6f 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c   not the journal
fde50 20 69 73 20 68 6f 74 2e 20 0a 20 20 2a 2a 0a 20   is hot. .  **. 
fde60 20 2a 2a 20 45 61 73 69 65 73 74 20 74 68 69 6e   ** Easiest thin
fde70 67 20 74 6f 20 64 6f 20 69 6e 20 74 68 69 73 20  g to do in this 
fde80 73 63 65 6e 61 72 69 6f 20 69 73 20 74 6f 20 74  scenario is to t
fde90 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f 75 72  runcate the jour
fdea0 6e 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20 74  nal .  ** file t
fdeb0 6f 20 74 68 65 20 72 65 71 75 69 72 65 64 20 73  o the required s
fdec0 69 7a 65 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 28  ize..  */ .  if(
fded0 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29   (rc==SQLITE_OK)
fdee0 0a 20 20 20 26 26 20 28 72 63 20 3d 20 73 71 6c  .   && (rc = sql
fdef0 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70  ite3OsFileSize(p
fdf00 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 6a 72 6e  Pager->jfd, &jrn
fdf10 6c 53 69 7a 65 29 29 3d 3d 53 51 4c 49 54 45 5f  lSize))==SQLITE_
fdf20 4f 4b 0a 20 20 20 26 26 20 6a 72 6e 6c 53 69 7a  OK.   && jrnlSiz
fdf30 65 3e 6a 72 6e 6c 4f 66 66 0a 20 20 29 7b 0a 20  e>jrnlOff.  ){. 
fdf40 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
fdf50 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72  sTruncate(pPager
fdf60 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f 66 66 29 3b  ->jfd, jrnlOff);
fdf70 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
fdf80 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20  ;.}../*.** Find 
fdf90 61 20 70 61 67 65 20 69 6e 20 74 68 65 20 68 61  a page in the ha
fdfa0 73 68 20 74 61 62 6c 65 20 67 69 76 65 6e 20 69  sh table given i
fdfb0 74 73 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20  ts page number. 
fdfc0 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69   Return.** a poi
fdfd0 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 65  nter to the page
fdfe0 20 6f 72 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20   or NULL if not 
fdff0 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  found..*/.static
fe000 20 50 67 48 64 72 20 2a 70 61 67 65 72 5f 6c 6f   PgHdr *pager_lo
fe010 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61 67  okup(Pager *pPag
fe020 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a  er, Pgno pgno){.
fe030 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 73 71    PgHdr *p;.  sq
fe040 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68  lite3PcacheFetch
fe050 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
fe060 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 70 29 3b 0a  , pgno, 0, &p);.
fe070 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
fe080 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 69  *.** Clear the i
fe090 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e 20  n-memory cache. 
fe0a0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   This routine.**
fe0b0 20 73 65 74 73 20 74 68 65 20 73 74 61 74 65 20   sets the state 
fe0c0 6f 66 20 74 68 65 20 70 61 67 65 72 20 62 61 63  of the pager bac
fe0d0 6b 20 74 6f 20 77 68 61 74 20 69 74 20 77 61 73  k to what it was
fe0e0 20 77 68 65 6e 20 69 74 20 77 61 73 20 66 69 72   when it was fir
fe0f0 73 74 0a 2a 2a 20 6f 70 65 6e 65 64 2e 20 20 41  st.** opened.  A
fe100 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70  ny outstanding p
fe110 61 67 65 73 20 61 72 65 20 69 6e 76 61 6c 69 64  ages are invalid
fe120 61 74 65 64 20 61 6e 64 20 73 75 62 73 65 71 75  ated and subsequ
fe130 65 6e 74 20 61 74 74 65 6d 70 74 73 0a 2a 2a 20  ent attempts.** 
fe140 74 6f 20 61 63 63 65 73 73 20 74 68 6f 73 65 20  to access those 
fe150 70 61 67 65 73 20 77 69 6c 6c 20 6c 69 6b 65 6c  pages will likel
fe160 79 20 72 65 73 75 6c 74 20 69 6e 20 61 20 63 6f  y result in a co
fe170 72 65 64 75 6d 70 2e 0a 2a 2f 0a 73 74 61 74 69  redump..*/.stati
fe180 63 20 76 6f 69 64 20 70 61 67 65 72 5f 72 65 73  c void pager_res
fe190 65 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  et(Pager *pPager
fe1a0 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  ){.  if( pPager-
fe1b0 3e 65 72 72 43 6f 64 65 20 29 20 72 65 74 75 72  >errCode ) retur
fe1c0 6e 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63  n;.  sqlite3Pcac
fe1d0 68 65 43 6c 65 61 72 28 70 50 61 67 65 72 2d 3e  heClear(pPager->
fe1e0 70 50 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a  pPCache);.}../*.
fe1f0 2a 2a 20 55 6e 6c 6f 63 6b 20 74 68 65 20 64 61  ** Unlock the da
fe200 74 61 62 61 73 65 20 66 69 6c 65 2e 20 0a 2a 2a  tabase file. .**
fe210 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72  .** If the pager
fe220 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e   is currently in
fe230 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 64 69   error state, di
fe240 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e  scard the conten
fe250 74 73 20 6f 66 20 0a 2a 2a 20 74 68 65 20 63 61  ts of .** the ca
fe260 63 68 65 20 61 6e 64 20 72 65 73 65 74 20 74 68  che and reset th
fe270 65 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72  e Pager structur
fe280 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65  e internal state
fe290 2e 20 49 66 20 74 68 65 72 65 20 69 73 0a 2a 2a  . If there is.**
fe2a0 20 61 6e 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c   an open journal
fe2b0 2d 66 69 6c 65 2c 20 74 68 65 6e 20 74 68 65 20  -file, then the 
fe2c0 6e 65 78 74 20 74 69 6d 65 20 61 20 73 68 61 72  next time a shar
fe2d0 65 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69  ed-lock is obtai
fe2e0 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 70 61  ned.** on the pa
fe2f0 67 65 72 20 66 69 6c 65 20 28 62 79 20 74 68 69  ger file (by thi
fe300 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70  s or any other p
fe310 72 6f 63 65 73 73 29 2c 20 69 74 20 77 69 6c 6c  rocess), it will
fe320 20 62 65 0a 2a 2a 20 74 72 65 61 74 65 64 20 61   be.** treated a
fe330 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  s a hot-journal 
fe340 61 6e 64 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e  and rolled back.
fe350 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
fe360 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 50 61 67  pager_unlock(Pag
fe370 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69  er *pPager){.  i
fe380 66 28 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c  f( !pPager->excl
fe390 75 73 69 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20  usiveMode ){.   
fe3a0 20 69 6e 74 20 72 63 20 3d 20 6f 73 55 6e 6c 6f   int rc = osUnlo
fe3b0 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 4e  ck(pPager->fd, N
fe3c0 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28  O_LOCK);.    if(
fe3d0 20 72 63 20 29 20 70 50 61 67 65 72 2d 3e 65 72   rc ) pPager->er
fe3e0 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 20 20  rCode = rc;.    
fe3f0 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d  pPager->dbSize =
fe400 20 2d 31 3b 0a 20 20 20 20 49 4f 54 52 41 43 45   -1;.    IOTRACE
fe410 28 28 22 55 4e 4c 4f 43 4b 20 25 70 5c 6e 22 2c  (("UNLOCK %p\n",
fe420 20 70 50 61 67 65 72 29 29 0a 0a 20 20 20 20 2f   pPager))..    /
fe430 2a 20 41 6c 77 61 79 73 20 63 6c 6f 73 65 20 74  * Always close t
fe440 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
fe450 77 68 65 6e 20 64 72 6f 70 70 69 6e 67 20 74 68  when dropping th
fe460 65 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 2e  e database lock.
fe470 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73  .    ** Otherwis
fe480 65 2c 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65  e, another conne
fe490 63 74 69 6f 6e 20 77 69 74 68 20 6a 6f 75 72 6e  ction with journ
fe4a0 61 6c 5f 6d 6f 64 65 3d 64 65 6c 65 74 65 20 6d  al_mode=delete m
fe4b0 69 67 68 74 0a 20 20 20 20 2a 2a 20 64 65 6c 65  ight.    ** dele
fe4c0 74 65 20 74 68 65 20 66 69 6c 65 20 6f 75 74 20  te the file out 
fe4d0 66 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a 20  from under us.. 
fe4e0 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50     */.    if( pP
fe4f0 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65  ager->journalOpe
fe500 6e 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  n ){.      sqlit
fe510 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72  e3OsClose(pPager
fe520 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 70 50  ->jfd);.      pP
fe530 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65  ager->journalOpe
fe540 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c  n = 0;.      sql
fe550 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f  ite3BitvecDestro
fe560 79 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75  y(pPager->pInJou
fe570 72 6e 61 6c 29 3b 0a 20 20 20 20 20 20 70 50 61  rnal);.      pPa
fe580 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20  ger->pInJournal 
fe590 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  = 0;.      sqlit
fe5a0 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28  e3BitvecDestroy(
fe5b0 70 50 61 67 65 72 2d 3e 70 41 6c 77 61 79 73 52  pPager->pAlwaysR
fe5c0 6f 6c 6c 62 61 63 6b 29 3b 0a 20 20 20 20 20 20  ollback);.      
fe5d0 70 50 61 67 65 72 2d 3e 70 41 6c 77 61 79 73 52  pPager->pAlwaysR
fe5e0 6f 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20 20  ollback = 0;.   
fe5f0 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 50 61   }..    /* If Pa
fe600 67 65 72 2e 65 72 72 43 6f 64 65 20 69 73 20 73  ger.errCode is s
fe610 65 74 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  et, the contents
fe620 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 63 61   of the pager ca
fe630 63 68 65 20 63 61 6e 6e 6f 74 20 62 65 0a 20 20  che cannot be.  
fe640 20 20 2a 2a 20 74 72 75 73 74 65 64 2e 20 4e 6f    ** trusted. No
fe650 77 20 74 68 61 74 20 74 68 65 20 70 61 67 65 72  w that the pager
fe660 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65   file is unlocke
fe670 64 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  d, the contents 
fe680 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 61  of the.    ** ca
fe690 63 68 65 20 63 61 6e 20 62 65 20 64 69 73 63 61  che can be disca
fe6a0 72 64 65 64 20 61 6e 64 20 74 68 65 20 65 72 72  rded and the err
fe6b0 6f 72 20 63 6f 64 65 20 73 61 66 65 6c 79 20 63  or code safely c
fe6c0 6c 65 61 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20  leared..    */. 
fe6d0 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65     if( pPager->e
fe6e0 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 20 20  rrCode ){.      
fe6f0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
fe700 4b 20 29 20 70 50 61 67 65 72 2d 3e 65 72 72 43  K ) pPager->errC
fe710 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  ode = SQLITE_OK;
fe720 0a 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 73  .      pager_res
fe730 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  et(pPager);.    
fe740 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74    if( pPager->st
fe750 6d 74 4f 70 65 6e 20 29 7b 0a 20 20 20 20 20 20  mtOpen ){.      
fe760 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65    sqlite3OsClose
fe770 28 70 50 61 67 65 72 2d 3e 73 74 66 64 29 3b 0a  (pPager->stfd);.
fe780 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42          sqlite3B
fe790 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61  itvecDestroy(pPa
fe7a0 67 65 72 2d 3e 70 49 6e 53 74 6d 74 29 3b 0a 20  ger->pInStmt);. 
fe7b0 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70         pPager->p
fe7c0 49 6e 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20  InStmt = 0;.    
fe7d0 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72    }.      pPager
fe7e0 2d 3e 73 74 6d 74 4f 70 65 6e 20 3d 20 30 3b 0a  ->stmtOpen = 0;.
fe7f0 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74        pPager->st
fe800 6d 74 49 6e 55 73 65 20 3d 20 30 3b 0a 20 20 20  mtInUse = 0;.   
fe810 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
fe820 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 20  alOff = 0;.     
fe830 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
fe840 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 20  Started = 0;.   
fe850 20 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 41     pPager->stmtA
fe860 75 74 6f 6f 70 65 6e 20 3d 20 30 3b 0a 20 20 20  utoopen = 0;.   
fe870 20 20 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 44     pPager->origD
fe880 62 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d  bSize = 0;.    }
fe890 0a 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74  ..    pPager->st
fe8a0 61 74 65 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f  ate = PAGER_UNLO
fe8b0 43 4b 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  CK;.    pPager->
fe8c0 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20  changeCountDone 
fe8d0 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  = 0;.  }.}../*.*
fe8e0 2a 20 45 78 65 63 75 74 65 20 61 20 72 6f 6c 6c  * Execute a roll
fe8f0 62 61 63 6b 20 69 66 20 61 20 74 72 61 6e 73 61  back if a transa
fe900 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20  ction is active 
fe910 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 0a  and unlock the .
fe920 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
fe930 2e 20 49 66 20 74 68 65 20 70 61 67 65 72 20 68  . If the pager h
fe940 61 73 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72  as already enter
fe950 65 64 20 74 68 65 20 65 72 72 6f 72 20 73 74 61  ed the error sta
fe960 74 65 2c 20 0a 2a 2a 20 64 6f 20 6e 6f 74 20 61  te, .** do not a
fe970 74 74 65 6d 70 74 20 74 68 65 20 72 6f 6c 6c 62  ttempt the rollb
fe980 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ack..*/.static v
fe990 6f 69 64 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41  oid pagerUnlockA
fe9a0 6e 64 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72  ndRollback(Pager
fe9b0 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 65   *p){.  if( p->e
fe9c0 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f  rrCode==SQLITE_O
fe9d0 4b 20 26 26 20 70 2d 3e 73 74 61 74 65 3e 3d 50  K && p->state>=P
fe9e0 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 7b  AGER_RESERVED ){
fe9f0 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69  .    sqlite3Begi
fea00 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  nBenignMalloc();
fea10 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65  .    sqlite3Page
fea20 72 52 6f 6c 6c 62 61 63 6b 28 70 29 3b 0a 20 20  rRollback(p);.  
fea30 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69    sqlite3EndBeni
fea40 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 7d 0a  gnMalloc();.  }.
fea50 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70    pager_unlock(p
fea60 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  );.}../*.** This
fea70 20 72 6f 75 74 69 6e 65 20 65 6e 64 73 20 61 20   routine ends a 
fea80 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 41 20  transaction.  A 
fea90 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 65  transaction is e
feaa0 6e 64 65 64 20 62 79 20 65 69 74 68 65 72 0a 2a  nded by either.*
feab0 2a 20 61 20 43 4f 4d 4d 49 54 20 6f 72 20 61 20  * a COMMIT or a 
feac0 52 4f 4c 4c 42 41 43 4b 2e 0a 2a 2a 0a 2a 2a 20  ROLLBACK..**.** 
fead0 57 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  When this routin
feae0 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65  e is called, the
feaf0 20 70 61 67 65 72 20 68 61 73 20 74 68 65 20 6a   pager has the j
feb00 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 70 65 6e  ournal file open
feb10 20 61 6e 64 0a 2a 2a 20 61 20 52 45 53 45 52 56   and.** a RESERV
feb20 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20  ED or EXCLUSIVE 
feb30 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
feb40 62 61 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74  base.  This rout
feb50 69 6e 65 20 77 69 6c 6c 20 72 65 6c 65 61 73 65  ine will release
feb60 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
feb70 20 6c 6f 63 6b 20 61 6e 64 20 61 63 71 75 69 72   lock and acquir
feb80 65 73 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b  es a SHARED lock
feb90 20 69 6e 20 69 74 73 20 70 6c 61 63 65 20 69 66   in its place if
feba0 20 74 68 61 74 20 69 73 0a 2a 2a 20 74 68 65 20   that is.** the 
febb0 61 70 70 72 6f 70 72 69 61 74 65 20 74 68 69 6e  appropriate thin
febc0 67 20 74 6f 20 64 6f 2e 20 20 52 65 6c 65 61 73  g to do.  Releas
febd0 65 20 6c 6f 63 6b 73 20 75 73 75 61 6c 6c 79 20  e locks usually 
febe0 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 2c 0a  is appropriate,.
febf0 2a 2a 20 75 6e 6c 65 73 73 20 77 65 20 61 72 65  ** unless we are
fec00 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 61 63   in exclusive ac
fec10 63 65 73 73 20 6d 6f 64 65 20 6f 72 20 75 6e 6c  cess mode or unl
fec20 65 73 73 20 74 68 69 73 20 69 73 20 61 20 0a 2a  ess this is a .*
fec30 2a 20 43 4f 4d 4d 49 54 20 41 4e 44 20 42 45 47  * COMMIT AND BEG
fec40 49 4e 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 20 41  IN or ROLLBACK A
fec50 4e 44 20 42 45 47 49 4e 20 6f 70 65 72 61 74 69  ND BEGIN operati
fec60 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f  on..**.** The jo
fec70 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 65 69  urnal file is ei
fec80 74 68 65 72 20 64 65 6c 65 74 65 64 20 6f 72 20  ther deleted or 
fec90 74 72 75 6e 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a  truncated..**.**
feca0 20 54 4f 44 4f 3a 20 43 6f 6e 73 69 64 65 72 20   TODO: Consider 
fecb0 6b 65 65 70 69 6e 67 20 74 68 65 20 6a 6f 75 72  keeping the jour
fecc0 6e 61 6c 20 66 69 6c 65 20 6f 70 65 6e 20 66 6f  nal file open fo
fecd0 72 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61  r temporary data
fece0 62 61 73 65 73 2e 0a 2a 2a 20 54 68 69 73 20 6d  bases..** This m
fecf0 69 67 68 74 20 67 69 76 65 20 61 20 70 65 72 66  ight give a perf
fed00 6f 72 6d 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d  ormance improvem
fed10 65 6e 74 20 6f 6e 20 77 69 6e 64 6f 77 73 20 77  ent on windows w
fed20 68 65 72 65 20 6f 70 65 6e 69 6e 67 0a 2a 2a 20  here opening.** 
fed30 61 20 66 69 6c 65 20 69 73 20 61 6e 20 65 78 70  a file is an exp
fed40 65 6e 73 69 76 65 20 6f 70 65 72 61 74 69 6f 6e  ensive operation
fed50 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
fed60 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61  pager_end_transa
fed70 63 74 69 6f 6e 28 50 61 67 65 72 20 2a 70 50 61  ction(Pager *pPa
fed80 67 65 72 2c 20 69 6e 74 20 68 61 73 4d 61 73 74  ger, int hasMast
fed90 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  er){.  int rc = 
feda0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74  SQLITE_OK;.  int
fedb0 20 72 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b   rc2 = SQLITE_OK
fedc0 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ;.  if( pPager->
fedd0 73 74 61 74 65 3c 50 41 47 45 52 5f 52 45 53 45  state<PAGER_RESE
fede0 52 56 45 44 20 29 7b 0a 20 20 20 20 72 65 74 75  RVED ){.    retu
fedf0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
fee00 7d 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72  }.  sqlite3Pager
fee10 53 74 6d 74 43 6f 6d 6d 69 74 28 70 50 61 67 65  StmtCommit(pPage
fee20 72 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72  r);.  if( pPager
fee30 2d 3e 73 74 6d 74 4f 70 65 6e 20 26 26 20 21 70  ->stmtOpen && !p
fee40 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65  Pager->exclusive
fee50 4d 6f 64 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  Mode ){.    sqli
fee60 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65  te3OsClose(pPage
fee70 72 2d 3e 73 74 66 64 29 3b 0a 20 20 20 20 70 50  r->stfd);.    pP
fee80 61 67 65 72 2d 3e 73 74 6d 74 4f 70 65 6e 20 3d  ager->stmtOpen =
fee90 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50   0;.  }.  if( pP
feea0 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65  ager->journalOpe
feeb0 6e 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61  n ){.    if( pPa
feec0 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
feed0 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
feee0 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20  ODE_MEMORY ){.  
feef0 20 20 20 20 69 6e 74 20 69 73 4d 65 6d 6f 72 79      int isMemory
fef00 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65  Journal = sqlite
fef10 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 50  3IsMemJournal(pP
fef20 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20  ager->jfd);.    
fef30 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65    sqlite3OsClose
fef40 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20  (pPager->jfd);. 
fef50 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75       pPager->jou
fef60 72 6e 61 6c 4f 70 65 6e 20 3d 20 30 3b 0a 20 20  rnalOpen = 0;.  
fef70 20 20 20 20 69 66 28 20 21 69 73 4d 65 6d 6f 72      if( !isMemor
fef80 79 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20  yJournal ){.    
fef90 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
fefa0 4f 73 44 65 6c 65 74 65 28 70 50 61 67 65 72 2d  OsDelete(pPager-
fefb0 3e 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a  >pVfs, pPager->z
fefc0 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20  Journal, 0);.   
fefd0 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69     }.    }else i
fefe0 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  f( pPager->journ
feff0 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f  alMode==PAGER_JO
ff000 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41  URNALMODE_TRUNCA
ff010 54 45 0a 20 20 20 20 20 20 20 20 20 26 26 20 28  TE.         && (
ff020 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72  rc = sqlite3OsTr
ff030 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a  uncate(pPager->j
ff040 66 64 2c 20 30 29 29 3d 3d 53 51 4c 49 54 45 5f  fd, 0))==SQLITE_
ff050 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67  OK ){.      pPag
ff060 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d  er->journalOff =
ff070 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72   0;.      pPager
ff080 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64  ->journalStarted
ff090 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20   = 0;.    }else 
ff0a0 69 66 28 20 70 50 61 67 65 72 2d 3e 65 78 63 6c  if( pPager->excl
ff0b0 75 73 69 76 65 4d 6f 64 65 20 0a 20 20 20 20 20  usiveMode .     
ff0c0 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  || pPager->journ
ff0d0 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f  alMode==PAGER_JO
ff0e0 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53  URNALMODE_PERSIS
ff0f0 54 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72  T.    ){.      r
ff100 63 20 3d 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48  c = zeroJournalH
ff110 64 72 28 70 50 61 67 65 72 2c 20 68 61 73 4d 61  dr(pPager, hasMa
ff120 73 74 65 72 29 3b 0a 20 20 20 20 20 20 70 61 67  ster);.      pag
ff130 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c  er_error(pPager,
ff140 20 72 63 29 3b 0a 20 20 20 20 20 20 70 50 61 67   rc);.      pPag
ff150 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d  er->journalOff =
ff160 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72   0;.      pPager
ff170 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64  ->journalStarted
ff180 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 0;.    }else{
ff190 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
ff1a0 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
ff1b0 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
ff1c0 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 20 7c 7c 20  LMODE_DELETE || 
ff1d0 72 63 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  rc );.      sqli
ff1e0 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65  te3OsClose(pPage
ff1f0 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 70  r->jfd);.      p
ff200 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70  Pager->journalOp
ff210 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66  en = 0;.      if
ff220 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
ff230 26 26 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70  && !pPager->temp
ff240 46 69 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20  File ){.        
ff250 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65  rc = sqlite3OsDe
ff260 6c 65 74 65 28 70 50 61 67 65 72 2d 3e 70 56 66  lete(pPager->pVf
ff270 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72  s, pPager->zJour
ff280 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d  nal, 0);.      }
ff290 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
ff2a0 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28  e3BitvecDestroy(
ff2b0 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e  pPager->pInJourn
ff2c0 61 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  al);.    pPager-
ff2d0 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b  >pInJournal = 0;
ff2e0 0a 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76  .    sqlite3Bitv
ff2f0 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72  ecDestroy(pPager
ff300 2d 3e 70 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63  ->pAlwaysRollbac
ff310 6b 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  k);.    pPager->
ff320 70 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 20  pAlwaysRollback 
ff330 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 0;.    sqlite3
ff340 50 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 70  PcacheCleanAll(p
ff350 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b  Pager->pPCache);
ff360 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43  .#ifdef SQLITE_C
ff370 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 73  HECK_PAGES.    s
ff380 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 72  qlite3PcacheIter
ff390 61 74 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61  ate(pPager->pPCa
ff3a0 63 68 65 2c 20 70 61 67 65 72 5f 73 65 74 5f 70  che, pager_set_p
ff3b0 61 67 65 68 61 73 68 29 3b 0a 23 65 6e 64 69 66  agehash);.#endif
ff3c0 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63  .    sqlite3Pcac
ff3d0 68 65 43 6c 65 61 72 46 6c 61 67 73 28 70 50 61  heClearFlags(pPa
ff3e0 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 0a 20 20  ger->pPCache,.  
ff3f0 20 20 20 20 20 50 47 48 44 52 5f 49 4e 5f 4a 4f       PGHDR_IN_JO
ff400 55 52 4e 41 4c 20 7c 20 50 47 48 44 52 5f 4e 45  URNAL | PGHDR_NE
ff410 45 44 5f 53 59 4e 43 0a 20 20 20 20 29 3b 0a 20  ED_SYNC.    );. 
ff420 20 20 20 70 50 61 67 65 72 2d 3e 64 69 72 74 79     pPager->dirty
ff430 43 61 63 68 65 20 3d 20 30 3b 0a 20 20 20 20 70  Cache = 0;.    p
ff440 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b  Pager->nRec = 0;
ff450 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
ff460 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49  sert( pPager->pI
ff470 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20  nJournal==0 );. 
ff480 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65   }..  if( !pPage
ff490 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65  r->exclusiveMode
ff4a0 20 29 7b 0a 20 20 20 20 72 63 32 20 3d 20 6f 73   ){.    rc2 = os
ff4b0 55 6e 6c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66  Unlock(pPager->f
ff4c0 64 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b  d, SHARED_LOCK);
ff4d0 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61  .    pPager->sta
ff4e0 74 65 20 3d 20 50 41 47 45 52 5f 53 48 41 52 45  te = PAGER_SHARE
ff4f0 44 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  D;.  }else if( p
ff500 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41  Pager->state==PA
ff510 47 45 52 5f 53 59 4e 43 45 44 20 29 7b 0a 20 20  GER_SYNCED ){.  
ff520 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20    pPager->state 
ff530 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  = PAGER_EXCLUSIV
ff540 45 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d  E;.  }.  pPager-
ff550 3e 6f 72 69 67 44 62 53 69 7a 65 20 3d 20 30 3b  >origDbSize = 0;
ff560 0a 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61  .  pPager->setMa
ff570 73 74 65 72 20 3d 20 30 3b 0a 20 20 70 50 61 67  ster = 0;.  pPag
ff580 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30  er->needSync = 0
ff590 3b 0a 20 20 2f 2a 20 6c 72 75 4c 69 73 74 53 65  ;.  /* lruListSe
ff5a0 74 46 69 72 73 74 53 79 6e 63 65 64 28 70 50 61  tFirstSynced(pPa
ff5b0 67 65 72 29 3b 20 2a 2f 0a 20 20 69 66 28 20 21  ger); */.  if( !
ff5c0 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 70 50 61  MEMDB ){.    pPa
ff5d0 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 2d 31  ger->dbSize = -1
ff5e0 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e  ;.  }.  pPager->
ff5f0 64 62 4d 6f 64 69 66 69 65 64 20 3d 20 30 3b 0a  dbModified = 0;.
ff600 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53  .  return (rc==S
ff610 51 4c 49 54 45 5f 4f 4b 3f 72 63 32 3a 72 63 29  QLITE_OK?rc2:rc)
ff620 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75  ;.}../*.** Compu
ff630 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20  te and return a 
ff640 63 68 65 63 6b 73 75 6d 20 66 6f 72 20 74 68 65  checksum for the
ff650 20 70 61 67 65 20 6f 66 20 64 61 74 61 2e 0a 2a   page of data..*
ff660 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f 74  *.** This is not
ff670 20 61 20 72 65 61 6c 20 63 68 65 63 6b 73 75 6d   a real checksum
ff680 2e 20 20 49 74 20 69 73 20 72 65 61 6c 6c 79 20  .  It is really 
ff690 6a 75 73 74 20 74 68 65 20 73 75 6d 20 6f 66 20  just the sum of 
ff6a0 74 68 65 20 0a 2a 2a 20 72 61 6e 64 6f 6d 20 69  the .** random i
ff6b0 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 61 6e 64  nitial value and
ff6c0 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
ff6d0 2e 20 20 57 65 20 65 78 70 65 72 69 6d 65 6e 74  .  We experiment
ff6e0 65 64 20 77 69 74 68 0a 2a 2a 20 61 20 63 68 65  ed with.** a che
ff6f0 63 6b 73 75 6d 20 6f 66 20 74 68 65 20 65 6e 74  cksum of the ent
ff700 69 72 65 20 64 61 74 61 2c 20 62 75 74 20 74 68  ire data, but th
ff710 61 74 20 77 61 73 20 66 6f 75 6e 64 20 74 6f 20  at was found to 
ff720 62 65 20 74 6f 6f 20 73 6c 6f 77 2e 0a 2a 2a 0a  be too slow..**.
ff730 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  ** Note that the
ff740 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20   page number is 
ff750 73 74 6f 72 65 64 20 61 74 20 74 68 65 20 62 65  stored at the be
ff760 67 69 6e 6e 69 6e 67 20 6f 66 20 64 61 74 61 20  ginning of data 
ff770 61 6e 64 0a 2a 2a 20 74 68 65 20 63 68 65 63 6b  and.** the check
ff780 73 75 6d 20 69 73 20 73 74 6f 72 65 64 20 61 74  sum is stored at
ff790 20 74 68 65 20 65 6e 64 2e 20 20 54 68 69 73 20   the end.  This 
ff7a0 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20 49  is important.  I
ff7b0 66 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 63 6f 72  f journal.** cor
ff7c0 72 75 70 74 69 6f 6e 20 6f 63 63 75 72 73 20 64  ruption occurs d
ff7d0 75 65 20 74 6f 20 61 20 70 6f 77 65 72 20 66 61  ue to a power fa
ff7e0 69 6c 75 72 65 2c 20 74 68 65 20 6d 6f 73 74 20  ilure, the most 
ff7f0 6c 69 6b 65 6c 79 20 73 63 65 6e 61 72 69 6f 0a  likely scenario.
ff800 2a 2a 20 69 73 20 74 68 61 74 20 6f 6e 65 20 65  ** is that one e
ff810 6e 64 20 6f 72 20 74 68 65 20 6f 74 68 65 72 20  nd or the other 
ff820 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 77 69  of the record wi
ff830 6c 6c 20 62 65 20 63 68 61 6e 67 65 64 2e 20 20  ll be changed.  
ff840 49 74 20 69 73 0a 2a 2a 20 6d 75 63 68 20 6c 65  It is.** much le
ff850 73 73 20 6c 69 6b 65 6c 79 20 74 68 61 74 20 74  ss likely that t
ff860 68 65 20 74 77 6f 20 65 6e 64 73 20 6f 66 20 74  he two ends of t
ff870 68 65 20 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72  he journal recor
ff880 64 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 63 6f 72  d will be.** cor
ff890 72 65 63 74 20 61 6e 64 20 74 68 65 20 6d 69 64  rect and the mid
ff8a0 64 6c 65 20 62 65 20 63 6f 72 72 75 70 74 2e 20  dle be corrupt. 
ff8b0 20 54 68 75 73 2c 20 74 68 69 73 20 22 63 68 65   Thus, this "che
ff8c0 63 6b 73 75 6d 22 20 73 63 68 65 6d 65 2c 0a 2a  cksum" scheme,.*
ff8d0 2a 20 74 68 6f 75 67 68 20 66 61 73 74 20 61 6e  * though fast an
ff8e0 64 20 73 69 6d 70 6c 65 2c 20 63 61 74 63 68 65  d simple, catche
ff8f0 73 20 74 68 65 20 6d 6f 73 74 6c 79 20 6c 69 6b  s the mostly lik
ff900 65 6c 79 20 6b 69 6e 64 20 6f 66 20 63 6f 72 72  ely kind of corr
ff910 75 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 49  uption..**.** FI
ff920 58 20 4d 45 3a 20 20 43 6f 6e 73 69 64 65 72 20  X ME:  Consider 
ff930 61 64 64 69 6e 67 20 65 76 65 72 79 20 32 30 30  adding every 200
ff940 74 68 20 28 6f 72 20 73 6f 29 20 62 79 74 65 20  th (or so) byte 
ff950 6f 66 20 74 68 65 20 64 61 74 61 20 74 6f 20 74  of the data to t
ff960 68 65 0a 2a 2a 20 63 68 65 63 6b 73 75 6d 2e 20  he.** checksum. 
ff970 20 54 68 61 74 20 77 61 79 20 69 66 20 61 20 73   That way if a s
ff980 69 6e 67 6c 65 20 70 61 67 65 20 73 70 61 6e 73  ingle page spans
ff990 20 33 20 6f 72 20 6d 6f 72 65 20 64 69 73 6b 20   3 or more disk 
ff9a0 73 65 63 74 6f 72 73 20 61 6e 64 0a 2a 2a 20 6f  sectors and.** o
ff9b0 6e 6c 79 20 74 68 65 20 6d 69 64 64 6c 65 20 73  nly the middle s
ff9c0 65 63 74 6f 72 20 69 73 20 63 6f 72 72 75 70 74  ector is corrupt
ff9d0 2c 20 77 65 20 77 69 6c 6c 20 73 74 69 6c 6c 20  , we will still 
ff9e0 68 61 76 65 20 61 20 72 65 61 73 6f 6e 61 62 6c  have a reasonabl
ff9f0 65 0a 2a 2a 20 63 68 61 6e 63 65 20 6f 66 20 66  e.** chance of f
ffa00 61 69 6c 69 6e 67 20 74 68 65 20 63 68 65 63 6b  ailing the check
ffa10 73 75 6d 20 61 6e 64 20 74 68 75 73 20 64 65 74  sum and thus det
ffa20 65 63 74 69 6e 67 20 74 68 65 20 70 72 6f 62 6c  ecting the probl
ffa30 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33  em..*/.static u3
ffa40 32 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 50 61  2 pager_cksum(Pa
ffa50 67 65 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e  ger *pPager, con
ffa60 73 74 20 75 38 20 2a 61 44 61 74 61 29 7b 0a 20  st u8 *aData){. 
ffa70 20 75 33 32 20 63 6b 73 75 6d 20 3d 20 70 50 61   u32 cksum = pPa
ffa80 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 3b 0a  ger->cksumInit;.
ffa90 20 20 69 6e 74 20 69 20 3d 20 70 50 61 67 65 72    int i = pPager
ffaa0 2d 3e 70 61 67 65 53 69 7a 65 2d 32 30 30 3b 0a  ->pageSize-200;.
ffab0 20 20 77 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a    while( i>0 ){.
ffac0 20 20 20 20 63 6b 73 75 6d 20 2b 3d 20 61 44 61      cksum += aDa
ffad0 74 61 5b 69 5d 3b 0a 20 20 20 20 69 20 2d 3d 20  ta[i];.    i -= 
ffae0 32 30 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  200;.  }.  retur
ffaf0 6e 20 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f 2a 20 46  n cksum;.}../* F
ffb00 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69  orward declarati
ffb10 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  on */.static voi
ffb20 64 20 6d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64  d makeClean(PgHd
ffb30 72 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  r*);../*.** Read
ffb40 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 66   a single page f
ffb50 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  rom the journal 
ffb60 66 69 6c 65 20 6f 70 65 6e 65 64 20 6f 6e 20 66  file opened on f
ffb70 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a 2a  ile descriptor.*
ffb80 2a 20 6a 66 64 2e 20 20 50 6c 61 79 62 61 63 6b  * jfd.  Playback
ffb90 20 74 68 69 73 20 6f 6e 65 20 70 61 67 65 2e 0a   this one page..
ffba0 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 4d 61 69 6e  **.** The isMain
ffbb0 4a 72 6e 6c 20 66 6c 61 67 20 69 73 20 74 72 75  Jrnl flag is tru
ffbc0 65 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65  e if this is the
ffbd0 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a   main rollback j
ffbe0 6f 75 72 6e 61 6c 20 61 6e 64 0a 2a 2a 20 66 61  ournal and.** fa
ffbf0 6c 73 65 20 66 6f 72 20 74 68 65 20 73 74 61 74  lse for the stat
ffc00 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20 20  ement journal.  
ffc10 54 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63  The main rollbac
ffc20 6b 20 6a 6f 75 72 6e 61 6c 20 75 73 65 73 0a 2a  k journal uses.*
ffc30 2a 20 63 68 65 63 6b 73 75 6d 73 20 2d 20 74 68  * checksums - th
ffc40 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72  e statement jour
ffc50 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2f  nal does not..*/
ffc60 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65  .static int page
ffc70 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70  r_playback_one_p
ffc80 61 67 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50  age(.  Pager *pP
ffc90 61 67 65 72 2c 20 20 20 20 20 20 20 2f 2a 20 54  ager,       /* T
ffca0 68 65 20 70 61 67 65 72 20 62 65 69 6e 67 20 70  he pager being p
ffcb0 6c 61 79 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20  layed back */.  
ffcc0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 6a 66  sqlite3_file *jf
ffcd0 64 2c 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65  d,   /* The file
ffce0 20 74 68 61 74 20 69 73 20 74 68 65 20 6a 6f 75   that is the jou
ffcf0 72 6e 61 6c 20 62 65 69 6e 67 20 72 6f 6c 6c 65  rnal being rolle
ffd00 64 20 62 61 63 6b 20 2a 2f 0a 20 20 69 36 34 20  d back */.  i64 
ffd10 6f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20  offset,         
ffd20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 74 68   /* Offset of th
ffd30 65 20 70 61 67 65 20 77 69 74 68 69 6e 20 74 68  e page within th
ffd40 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69  e journal */.  i
ffd50 6e 74 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 20 20  nt isMainJrnl   
ffd60 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
ffd70 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f  main rollback jo
ffd80 75 72 6e 61 6c 2e 20 46 61 6c 73 65 20 66 6f 72  urnal. False for
ffd90 20 53 74 6d 74 20 6a 72 6e 6c 20 2a 2f 0a 29 7b   Stmt jrnl */.){
ffda0 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 67 48  .  int rc;.  PgH
ffdb0 64 72 20 2a 70 50 67 3b 20 20 20 20 20 20 20 20  dr *pPg;        
ffdc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e             /* An
ffdd0 20 65 78 69 73 74 69 6e 67 20 70 61 67 65 20 69   existing page i
ffde0 6e 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 20  n the cache */. 
ffdf0 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20   Pgno pgno;     
ffe00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
ffe10 2a 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65  * The page numbe
ffe20 72 20 6f 66 20 61 20 70 61 67 65 20 69 6e 20 6a  r of a page in j
ffe30 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20  ournal */.  u32 
ffe40 63 6b 73 75 6d 3b 20 20 20 20 20 20 20 20 20 20  cksum;          
ffe50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65            /* Che
ffe60 63 6b 73 75 6d 20 75 73 65 64 20 66 6f 72 20 73  cksum used for s
ffe70 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a  anity checking *
ffe80 2f 0a 20 20 75 38 20 2a 61 44 61 74 61 20 3d 20  /.  u8 *aData = 
ffe90 28 75 38 20 2a 29 70 50 61 67 65 72 2d 3e 70 54  (u8 *)pPager->pT
ffea0 6d 70 53 70 61 63 65 3b 20 20 20 2f 2a 20 54 65  mpSpace;   /* Te
ffeb0 6d 70 20 73 74 6f 72 61 67 65 20 66 6f 72 20 61  mp storage for a
ffec0 20 70 61 67 65 20 2a 2f 0a 0a 20 20 2f 2a 20 69   page */..  /* i
ffed0 73 4d 61 69 6e 4a 72 6e 6c 20 73 68 6f 75 6c 64  sMainJrnl should
ffee0 20 62 65 20 74 72 75 65 20 66 6f 72 20 74 68 65   be true for the
ffef0 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 61 6e   main journal an
fff00 64 20 66 61 6c 73 65 20 66 6f 72 0a 20 20 2a 2a  d false for.  **
fff10 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e   statement journ
fff20 61 6c 73 2e 20 20 56 65 72 69 66 79 20 74 68 61  als.  Verify tha
fff30 74 20 74 68 69 73 20 69 73 20 61 6c 77 61 79 73  t this is always
fff40 20 74 68 65 20 63 61 73 65 0a 20 20 2a 2f 0a 20   the case.  */. 
fff50 20 61 73 73 65 72 74 28 20 6a 66 64 20 3d 3d 20   assert( jfd == 
fff60 28 69 73 4d 61 69 6e 4a 72 6e 6c 20 3f 20 70 50  (isMainJrnl ? pP
fff70 61 67 65 72 2d 3e 6a 66 64 20 3a 20 70 50 61 67  ager->jfd : pPag
fff80 65 72 2d 3e 73 74 66 64 29 20 29 3b 0a 20 20 61  er->stfd) );.  a
fff90 73 73 65 72 74 28 20 61 44 61 74 61 20 29 3b 0a  ssert( aData );.
fffa0 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 69  .  rc = read32bi
fffb0 74 73 28 6a 66 64 2c 20 6f 66 66 73 65 74 2c 20  ts(jfd, offset, 
fffc0 26 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 72 63  &pgno);.  if( rc
fffd0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
fffe0 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20  turn rc;.  rc = 
ffff0 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 6a 66  sqlite3OsRead(jf
10000 64 2c 20 61 44 61 74 61 2c 20 70 50 61 67 65 72  d, aData, pPager
10001 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 73  ->pageSize, offs
10002 65 74 2b 34 29 3b 0a 20 20 69 66 28 20 72 63 21  et+4);.  if( rc!
10003 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
10004 75 72 6e 20 72 63 3b 0a 20 20 70 50 61 67 65 72  urn rc;.  pPager
10005 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20  ->journalOff += 
10006 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
10007 20 2b 20 34 3b 0a 0a 20 20 2f 2a 20 53 61 6e 69   + 4;..  /* Sani
10008 74 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74  ty checking on t
10009 68 65 20 70 61 67 65 2e 20 20 54 68 69 73 20 69  he page.  This i
1000a 73 20 6d 6f 72 65 20 69 6d 70 6f 72 74 61 6e 74  s more important
1000b 20 74 68 61 74 20 49 20 6f 72 69 67 69 6e 61 6c   that I original
1000c 6c 79 0a 20 20 2a 2a 20 74 68 6f 75 67 68 74 2e  ly.  ** thought.
1000d 20 20 49 66 20 61 20 70 6f 77 65 72 20 66 61 69    If a power fai
1000e 6c 75 72 65 20 6f 63 63 75 72 73 20 77 68 69 6c  lure occurs whil
1000f 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73  e the journal is
10010 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e 2c 0a   being written,.
10011 20 20 2a 2a 20 69 74 20 63 6f 75 6c 64 20 63 61    ** it could ca
10012 75 73 65 20 69 6e 76 61 6c 69 64 20 64 61 74 61  use invalid data
10013 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69   to be written i
10014 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e  nto the journal.
10015 20 20 57 65 20 6e 65 65 64 20 74 6f 0a 20 20 2a    We need to.  *
10016 2a 20 64 65 74 65 63 74 20 74 68 69 73 20 69 6e  * detect this in
10017 76 61 6c 69 64 20 64 61 74 61 20 28 77 69 74 68  valid data (with
10018 20 68 69 67 68 20 70 72 6f 62 61 62 69 6c 69 74   high probabilit
10019 79 29 20 61 6e 64 20 69 67 6e 6f 72 65 20 69 74  y) and ignore it
1001a 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 67 6e  ..  */.  if( pgn
1001b 6f 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41  o==0 || pgno==PA
1001c 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67  GER_MJ_PGNO(pPag
1001d 65 72 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  er) ){.    retur
1001e 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  n SQLITE_DONE;. 
1001f 20 7d 0a 20 20 69 66 28 20 70 67 6e 6f 3e 28 75   }.  if( pgno>(u
10020 6e 73 69 67 6e 65 64 29 70 50 61 67 65 72 2d 3e  nsigned)pPager->
10021 64 62 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 65  dbSize ){.    re
10022 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
10023 20 20 7d 0a 20 20 69 66 28 20 69 73 4d 61 69 6e    }.  if( isMain
10024 4a 72 6e 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d  Jrnl ){.    rc =
10025 20 72 65 61 64 33 32 62 69 74 73 28 6a 66 64 2c   read32bits(jfd,
10026 20 6f 66 66 73 65 74 2b 70 50 61 67 65 72 2d 3e   offset+pPager->
10027 70 61 67 65 53 69 7a 65 2b 34 2c 20 26 63 6b 73  pageSize+4, &cks
10028 75 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  um);.    if( rc 
10029 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
1002a 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
1002b 4f 66 66 20 2b 3d 20 34 3b 0a 20 20 20 20 69 66  Off += 4;.    if
1002c 28 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 70 50  ( pager_cksum(pP
1002d 61 67 65 72 2c 20 61 44 61 74 61 29 21 3d 63 6b  ager, aData)!=ck
1002e 73 75 6d 20 29 7b 0a 20 20 20 20 20 20 72 65 74  sum ){.      ret
1002f 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  urn SQLITE_DONE;
10030 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73  .    }.  }..  as
10031 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74  sert( pPager->st
10032 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 53 45 52  ate==PAGER_RESER
10033 56 45 44 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73  VED || pPager->s
10034 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c  tate>=PAGER_EXCL
10035 55 53 49 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 49  USIVE );..  /* I
10036 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69  f the pager is i
10037 6e 20 52 45 53 45 52 56 45 44 20 73 74 61 74 65  n RESERVED state
10038 2c 20 74 68 65 6e 20 74 68 65 72 65 20 6d 75 73  , then there mus
10039 74 20 62 65 20 61 20 63 6f 70 79 20 6f 66 20 74  t be a copy of t
1003a 68 69 73 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e  his.  ** page in
1003b 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65   the pager cache
1003c 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 6a  . In this case j
1003d 75 73 74 20 75 70 64 61 74 65 20 74 68 65 20 70  ust update the p
1003e 61 67 65 72 20 63 61 63 68 65 2c 0a 20 20 2a 2a  ager cache,.  **
1003f 20 6e 6f 74 20 74 68 65 20 64 61 74 61 62 61 73   not the databas
10040 65 20 66 69 6c 65 2e 20 54 68 65 20 70 61 67 65  e file. The page
10041 20 69 73 20 6c 65 66 74 20 6d 61 72 6b 65 64 20   is left marked 
10042 64 69 72 74 79 20 69 6e 20 74 68 69 73 20 63 61  dirty in this ca
10043 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e  se..  **.  ** An
10044 20 65 78 63 65 70 74 69 6f 6e 20 74 6f 20 74 68   exception to th
10045 65 20 61 62 6f 76 65 20 72 75 6c 65 3a 20 49 66  e above rule: If
10046 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
10047 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65   in no-sync mode
10048 0a 20 20 2a 2a 20 61 6e 64 20 61 20 70 61 67 65  .  ** and a page
10049 20 69 73 20 6d 6f 76 65 64 20 64 75 72 69 6e 67   is moved during
1004a 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20   an incremental 
1004b 76 61 63 75 75 6d 20 74 68 65 6e 20 74 68 65 20  vacuum then the 
1004c 70 61 67 65 20 6d 61 79 0a 20 20 2a 2a 20 6e 6f  page may.  ** no
1004d 74 20 62 65 20 69 6e 20 74 68 65 20 70 61 67 65  t be in the page
1004e 72 20 63 61 63 68 65 2e 20 4c 61 74 65 72 3a 20  r cache. Later: 
1004f 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72  if a malloc() or
10050 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73   IO error occurs
10051 0a 20 20 2a 2a 20 64 75 72 69 6e 67 20 61 20 4d  .  ** during a M
10052 6f 76 65 70 61 67 65 28 29 20 63 61 6c 6c 2c 20  ovepage() call, 
10053 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 6d 61  then the page ma
10054 79 20 6e 6f 74 20 62 65 20 69 6e 20 74 68 65 20  y not be in the 
10055 63 61 63 68 65 0a 20 20 2a 2a 20 65 69 74 68 65  cache.  ** eithe
10056 72 2e 20 53 6f 20 74 68 65 20 63 6f 6e 64 69 74  r. So the condit
10057 69 6f 6e 20 64 65 73 63 72 69 62 65 64 20 69 6e  ion described in
10058 20 74 68 65 20 61 62 6f 76 65 20 70 61 72 61 67   the above parag
10059 72 61 70 68 20 69 73 20 6e 6f 74 0a 20 20 2a 2a  raph is not.  **
1005a 20 61 73 73 65 72 74 28 29 61 62 6c 65 2e 0a 20   assert()able.. 
1005b 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 69 6e 20 45   **.  ** If in E
1005c 58 43 4c 55 53 49 56 45 20 73 74 61 74 65 2c 20  XCLUSIVE state, 
1005d 74 68 65 6e 20 77 65 20 75 70 64 61 74 65 20 74  then we update t
1005e 68 65 20 70 61 67 65 72 20 63 61 63 68 65 20 69  he pager cache i
1005f 66 20 69 74 20 65 78 69 73 74 73 0a 20 20 2a 2a  f it exists.  **
10060 20 61 6e 64 20 74 68 65 20 6d 61 69 6e 20 66 69   and the main fi
10061 6c 65 2e 20 54 68 65 20 70 61 67 65 20 69 73 20  le. The page is 
10062 74 68 65 6e 20 6d 61 72 6b 65 64 20 6e 6f 74 20  then marked not 
10063 64 69 72 74 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  dirty..  **.  **
10064 20 54 69 63 6b 65 74 20 23 31 31 37 31 3a 20 20   Ticket #1171:  
10065 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f  The statement jo
10066 75 72 6e 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74  urnal might cont
10067 61 69 6e 20 70 61 67 65 20 63 6f 6e 74 65 6e 74  ain page content
10068 20 74 68 61 74 20 69 73 0a 20 20 2a 2a 20 64 69   that is.  ** di
10069 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65  fferent from the
1006a 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 61 74   page content at
1006b 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68   the start of th
1006c 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20  e transaction.. 
1006d 20 2a 2a 20 54 68 69 73 20 6f 63 63 75 72 73 20   ** This occurs 
1006e 77 68 65 6e 20 61 20 70 61 67 65 20 69 73 20 63  when a page is c
1006f 68 61 6e 67 65 64 20 70 72 69 6f 72 20 74 6f 20  hanged prior to 
10070 74 68 65 20 73 74 61 72 74 20 6f 66 20 61 20 73  the start of a s
10071 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 74 68  tatement.  ** th
10072 65 6e 20 63 68 61 6e 67 65 64 20 61 67 61 69 6e  en changed again
10073 20 77 69 74 68 69 6e 20 74 68 65 20 73 74 61 74   within the stat
10074 65 6d 65 6e 74 2e 20 20 57 68 65 6e 20 72 6f 6c  ement.  When rol
10075 6c 69 6e 67 20 62 61 63 6b 20 73 75 63 68 20 61  ling back such a
10076 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20  .  ** statement 
10077 77 65 20 6d 75 73 74 20 6e 6f 74 20 77 72 69 74  we must not writ
10078 65 20 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61  e to the origina
10079 6c 20 64 61 74 61 62 61 73 65 20 75 6e 6c 65 73  l database unles
1007a 73 20 77 65 20 6b 6e 6f 77 0a 20 20 2a 2a 20 66  s we know.  ** f
1007b 6f 72 20 63 65 72 74 61 69 6e 20 74 68 61 74 20  or certain that 
1007c 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 20 63 6f  original page co
1007d 6e 74 65 6e 74 73 20 61 72 65 20 73 79 6e 63 65  ntents are synce
1007e 64 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20  d into the main 
1007f 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a 6f  rollback.  ** jo
10080 75 72 6e 61 6c 2e 20 20 4f 74 68 65 72 77 69 73  urnal.  Otherwis
10081 65 2c 20 61 20 70 6f 77 65 72 20 6c 6f 73 73 20  e, a power loss 
10082 6d 69 67 68 74 20 6c 65 61 76 65 20 6d 6f 64 69  might leave modi
10083 66 69 65 64 20 64 61 74 61 20 69 6e 20 74 68 65  fied data in the
10084 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66  .  ** database f
10085 69 6c 65 20 77 69 74 68 6f 75 74 20 61 6e 20 65  ile without an e
10086 6e 74 72 79 20 69 6e 20 74 68 65 20 72 6f 6c 6c  ntry in the roll
10087 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 74 68 61  back journal tha
10088 74 20 63 61 6e 0a 20 20 2a 2a 20 72 65 73 74 6f  t can.  ** resto
10089 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  re the database 
1008a 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20  to its original 
1008b 66 6f 72 6d 2e 20 20 54 77 6f 20 63 6f 6e 64 69  form.  Two condi
1008c 74 69 6f 6e 73 20 6d 75 73 74 20 62 65 0a 20 20  tions must be.  
1008d 2a 2a 20 6d 65 74 20 62 65 66 6f 72 65 20 77 72  ** met before wr
1008e 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74  iting to the dat
1008f 61 62 61 73 65 20 66 69 6c 65 73 2e 20 28 31 29  abase files. (1)
10090 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d 75   the database mu
10091 73 74 20 62 65 0a 20 20 2a 2a 20 6c 6f 63 6b 65  st be.  ** locke
10092 64 2e 20 20 28 32 29 20 77 65 20 6b 6e 6f 77 20  d.  (2) we know 
10093 74 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e 61  that the origina
10094 6c 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 69  l page content i
10095 73 20 66 75 6c 6c 79 20 73 79 6e 63 65 64 0a 20  s fully synced. 
10096 20 2a 2a 20 69 6e 20 74 68 65 20 6d 61 69 6e 20   ** in the main 
10097 6a 6f 75 72 6e 61 6c 20 65 69 74 68 65 72 20 62  journal either b
10098 65 63 61 75 73 65 20 74 68 65 20 70 61 67 65 20  ecause the page 
10099 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 20  is not in cache 
1009a 6f 72 20 65 6c 73 65 0a 20 20 2a 2a 20 74 68 65  or else.  ** the
1009b 20 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20   page is marked 
1009c 61 73 20 6e 65 65 64 53 79 6e 63 3d 3d 30 2e 0a  as needSync==0..
1009d 20 20 2a 2a 0a 20 20 2a 2a 20 32 30 30 38 2d 30    **.  ** 2008-0
1009e 34 2d 31 34 3a 20 20 57 68 65 6e 20 61 74 74 65  4-14:  When atte
1009f 6d 70 74 69 6e 67 20 74 6f 20 76 61 63 75 75 6d  mpting to vacuum
100a0 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62   a corrupt datab
100a1 61 73 65 20 66 69 6c 65 2c 20 69 74 0a 20 20 2a  ase file, it.  *
100a2 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f  * is possible to
100a3 20 66 61 69 6c 20 61 20 73 74 61 74 65 6d 65 6e   fail a statemen
100a4 74 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20  t on a database 
100a5 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 79 65  that does not ye
100a6 74 20 65 78 69 73 74 2e 0a 20 20 2a 2a 20 44 6f  t exist..  ** Do
100a7 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20   not attempt to 
100a8 77 72 69 74 65 20 69 66 20 64 61 74 61 62 61 73  write if databas
100a9 65 20 66 69 6c 65 20 68 61 73 20 6e 65 76 65 72  e file has never
100aa 20 62 65 65 6e 20 6f 70 65 6e 65 64 2e 0a 20 20   been opened..  
100ab 2a 2f 0a 20 20 70 50 67 20 3d 20 70 61 67 65 72  */.  pPg = pager
100ac 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20  _lookup(pPager, 
100ad 70 67 6e 6f 29 3b 0a 20 20 50 41 47 45 52 54 52  pgno);.  PAGERTR
100ae 41 43 45 34 28 22 50 4c 41 59 42 41 43 4b 20 25  ACE4("PLAYBACK %
100af 64 20 70 61 67 65 20 25 64 20 68 61 73 68 28 25  d page %d hash(%
100b0 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20  08x)\n",.       
100b1 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28          PAGERID(
100b2 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70  pPager), pgno, p
100b3 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 70 50  ager_datahash(pP
100b4 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20  ager->pageSize, 
100b5 61 44 61 74 61 29 29 3b 0a 20 20 69 66 28 20 28  aData));.  if( (
100b6 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50  pPager->state>=P
100b7 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 29 0a  AGER_EXCLUSIVE).
100b8 20 20 20 26 26 20 28 70 50 67 3d 3d 30 20 7c 7c     && (pPg==0 ||
100b9 20 30 3d 3d 28 70 50 67 2d 3e 66 6c 61 67 73 26   0==(pPg->flags&
100ba 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29  PGHDR_NEED_SYNC)
100bb 29 0a 20 20 20 26 26 20 28 70 50 61 67 65 72 2d  ).   && (pPager-
100bc 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 29 0a 20  >fd->pMethods). 
100bd 20 29 7b 0a 20 20 20 20 69 36 34 20 6f 66 73 74   ){.    i64 ofst
100be 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34   = (pgno-1)*(i64
100bf 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a  )pPager->pageSiz
100c0 65 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  e;.    rc = sqli
100c1 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65  te3OsWrite(pPage
100c2 72 2d 3e 66 64 2c 20 61 44 61 74 61 2c 20 70 50  r->fd, aData, pP
100c3 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20  ager->pageSize, 
100c4 6f 66 73 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28  ofst);.  }.  if(
100c5 20 70 50 67 20 29 7b 0a 20 20 20 20 2f 2a 20 4e   pPg ){.    /* N
100c6 6f 20 70 61 67 65 20 73 68 6f 75 6c 64 20 65 76  o page should ev
100c7 65 72 20 62 65 20 65 78 70 6c 69 63 69 74 6c 79  er be explicitly
100c8 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 61   rolled back tha
100c9 74 20 69 73 20 69 6e 20 75 73 65 2c 20 65 78 63  t is in use, exc
100ca 65 70 74 0a 20 20 20 20 2a 2a 20 66 6f 72 20 70  ept.    ** for p
100cb 61 67 65 20 31 20 77 68 69 63 68 20 69 73 20 68  age 1 which is h
100cc 65 6c 64 20 69 6e 20 75 73 65 20 69 6e 20 6f 72  eld in use in or
100cd 64 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20  der to keep the 
100ce 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 20 20  lock on the.    
100cf 2a 2a 20 64 61 74 61 62 61 73 65 20 61 63 74 69  ** database acti
100d0 76 65 2e 20 48 6f 77 65 76 65 72 20 73 75 63 68  ve. However such
100d1 20 61 20 70 61 67 65 20 6d 61 79 20 62 65 20 72   a page may be r
100d2 6f 6c 6c 65 64 20 62 61 63 6b 20 61 73 20 61 20  olled back as a 
100d3 72 65 73 75 6c 74 0a 20 20 20 20 2a 2a 20 6f 66  result.    ** of
100d4 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65 72 72   an internal err
100d5 6f 72 20 72 65 73 75 6c 74 69 6e 67 20 69 6e 20  or resulting in 
100d6 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 63 61 6c  an automatic cal
100d7 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69  l to.    ** sqli
100d8 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b  te3PagerRollback
100d9 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 76  ()..    */.    v
100da 6f 69 64 20 2a 70 44 61 74 61 3b 0a 20 20 20 20  oid *pData;.    
100db 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 44 61  pData = pPg->pDa
100dc 74 61 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70  ta;.    memcpy(p
100dd 44 61 74 61 2c 20 61 44 61 74 61 2c 20 70 50 61  Data, aData, pPa
100de 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a  ger->pageSize);.
100df 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
100e0 78 52 65 69 6e 69 74 65 72 20 29 7b 0a 20 20 20  xReiniter ){.   
100e1 20 20 20 70 50 61 67 65 72 2d 3e 78 52 65 69 6e     pPager->xRein
100e2 69 74 65 72 28 70 50 67 29 3b 0a 20 20 20 20 7d  iter(pPg);.    }
100e3 0a 20 20 20 20 69 66 28 20 69 73 4d 61 69 6e 4a  .    if( isMainJ
100e4 72 6e 6c 20 29 20 6d 61 6b 65 43 6c 65 61 6e 28  rnl ) makeClean(
100e5 70 50 67 29 3b 0a 23 69 66 64 65 66 20 53 51 4c  pPg);.#ifdef SQL
100e6 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a  ITE_CHECK_PAGES.
100e7 20 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73      pPg->pageHas
100e8 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61  h = pager_pageha
100e9 73 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a  sh(pPg);.#endif.
100ea 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77      /* If this w
100eb 61 73 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20  as page 1, then 
100ec 72 65 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75  restore the valu
100ed 65 20 6f 66 20 50 61 67 65 72 2e 64 62 46 69 6c  e of Pager.dbFil
100ee 65 56 65 72 73 2e 0a 20 20 20 20 2a 2a 20 44 6f  eVers..    ** Do
100ef 20 74 68 69 73 20 62 65 66 6f 72 65 20 61 6e 79   this before any
100f0 20 64 65 63 6f 64 69 6e 67 2e 20 2a 2f 0a 20 20   decoding. */.  
100f1 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b    if( pgno==1 ){
100f2 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70  .      memcpy(&p
100f3 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72  Pager->dbFileVer
100f4 73 2c 20 26 28 28 75 38 2a 29 70 44 61 74 61 29  s, &((u8*)pData)
100f5 5b 32 34 5d 2c 73 69 7a 65 6f 66 28 70 50 61 67  [24],sizeof(pPag
100f6 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29  er->dbFileVers))
100f7 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
100f8 44 65 63 6f 64 65 20 74 68 65 20 70 61 67 65 20  Decode the page 
100f9 6a 75 73 74 20 72 65 61 64 20 66 72 6f 6d 20 64  just read from d
100fa 69 73 6b 20 2a 2f 0a 20 20 20 20 43 4f 44 45 43  isk */.    CODEC
100fb 31 28 70 50 61 67 65 72 2c 20 70 44 61 74 61 2c  1(pPager, pData,
100fc 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 33 29 3b 0a   pPg->pgno, 3);.
100fd 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68      sqlite3Pcach
100fe 65 52 65 6c 65 61 73 65 28 70 50 67 29 3b 0a 20  eRelease(pPg);. 
100ff 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
10100 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74  }../*.** Paramet
10101 65 72 20 7a 4d 61 73 74 65 72 20 69 73 20 74 68  er zMaster is th
10102 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74  e name of a mast
10103 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  er journal file.
10104 20 41 20 73 69 6e 67 6c 65 20 6a 6f 75 72 6e 61   A single journa
10105 6c 0a 2a 2a 20 66 69 6c 65 20 74 68 61 74 20 72  l.** file that r
10106 65 66 65 72 72 65 64 20 74 6f 20 74 68 65 20 6d  eferred to the m
10107 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
10108 6c 65 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e  le has just been
10109 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a   rolled back..**
1010a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68   This routine ch
1010b 65 63 6b 73 20 69 66 20 69 74 20 69 73 20 70 6f  ecks if it is po
1010c 73 73 69 62 6c 65 20 74 6f 20 64 65 6c 65 74 65  ssible to delete
1010d 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
1010e 6e 61 6c 20 66 69 6c 65 2c 0a 2a 2a 20 61 6e 64  nal file,.** and
1010f 20 64 6f 65 73 20 73 6f 20 69 66 20 69 74 20 69   does so if it i
10110 73 2e 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e  s..**.** Argumen
10111 74 20 7a 4d 61 73 74 65 72 20 6d 61 79 20 70 6f  t zMaster may po
10112 69 6e 74 20 74 6f 20 50 61 67 65 72 2e 70 54 6d  int to Pager.pTm
10113 70 53 70 61 63 65 2e 20 53 6f 20 74 68 61 74 20  pSpace. So that 
10114 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20 0a 2a  buffer is not .*
10115 2a 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20  * available for 
10116 75 73 65 20 77 69 74 68 69 6e 20 74 68 69 73 20  use within this 
10117 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 0a  function..**.**.
10118 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 6a 6f  ** The master jo
10119 75 72 6e 61 6c 20 66 69 6c 65 20 63 6f 6e 74 61  urnal file conta
1011a 69 6e 73 20 74 68 65 20 6e 61 6d 65 73 20 6f 66  ins the names of
1011b 20 61 6c 6c 20 63 68 69 6c 64 20 6a 6f 75 72 6e   all child journ
1011c 61 6c 73 2e 0a 2a 2a 20 54 6f 20 74 65 6c 6c 20  als..** To tell 
1011d 69 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72  if a master jour
1011e 6e 61 6c 20 63 61 6e 20 62 65 20 64 65 6c 65 74  nal can be delet
1011f 65 64 2c 20 63 68 65 63 6b 20 74 6f 20 65 61 63  ed, check to eac
10120 68 20 6f 66 20 74 68 65 0a 2a 2a 20 63 68 69 6c  h of the.** chil
10121 64 72 65 6e 2e 20 20 49 66 20 61 6c 6c 20 63 68  dren.  If all ch
10122 69 6c 64 72 65 6e 20 61 72 65 20 65 69 74 68 65  ildren are eithe
10123 72 20 6d 69 73 73 69 6e 67 20 6f 72 20 64 6f 20  r missing or do 
10124 6e 6f 74 20 72 65 66 65 72 20 74 6f 0a 2a 2a 20  not refer to.** 
10125 61 20 64 69 66 66 65 72 65 6e 74 20 6d 61 73 74  a different mast
10126 65 72 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 6e  er journal, then
10127 20 74 68 69 73 20 6d 61 73 74 65 72 20 6a 6f 75   this master jou
10128 72 6e 61 6c 20 63 61 6e 20 62 65 20 64 65 6c 65  rnal can be dele
10129 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ted..*/.static i
1012a 6e 74 20 70 61 67 65 72 5f 64 65 6c 6d 61 73 74  nt pager_delmast
1012b 65 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  er(Pager *pPager
1012c 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d  , const char *zM
1012d 61 73 74 65 72 29 7b 0a 20 20 73 71 6c 69 74 65  aster){.  sqlite
1012e 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70 50  3_vfs *pVfs = pP
1012f 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 6e  ager->pVfs;.  in
10130 74 20 72 63 3b 0a 20 20 69 6e 74 20 6d 61 73 74  t rc;.  int mast
10131 65 72 5f 6f 70 65 6e 20 3d 20 30 3b 0a 20 20 73  er_open = 0;.  s
10132 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4d 61  qlite3_file *pMa
10133 73 74 65 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ster;.  sqlite3_
10134 66 69 6c 65 20 2a 70 4a 6f 75 72 6e 61 6c 3b 0a  file *pJournal;.
10135 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 4a    char *zMasterJ
10136 6f 75 72 6e 61 6c 20 3d 20 30 3b 20 2f 2a 20 43  ournal = 0; /* C
10137 6f 6e 74 65 6e 74 73 20 6f 66 20 6d 61 73 74 65  ontents of maste
10138 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a  r journal file *
10139 2f 0a 20 20 69 36 34 20 6e 4d 61 73 74 65 72 4a  /.  i64 nMasterJ
1013a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 2f 2a  ournal;       /*
1013b 20 53 69 7a 65 20 6f 66 20 6d 61 73 74 65 72 20   Size of master 
1013c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a  journal file */.
1013d 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6d  .  /* Open the m
1013e 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
1013f 6c 65 20 65 78 63 6c 75 73 69 76 65 6c 79 20 69  le exclusively i
10140 6e 20 63 61 73 65 20 73 6f 6d 65 20 6f 74 68 65  n case some othe
10141 72 20 70 72 6f 63 65 73 73 0a 20 20 2a 2a 20 69  r process.  ** i
10142 73 20 72 75 6e 6e 69 6e 67 20 74 68 69 73 20 72  s running this r
10143 6f 75 74 69 6e 65 20 61 6c 73 6f 2e 20 4e 6f 74  outine also. Not
10144 20 74 68 61 74 20 69 74 20 6d 61 6b 65 73 20 74   that it makes t
10145 6f 6f 20 6d 75 63 68 20 64 69 66 66 65 72 65 6e  oo much differen
10146 63 65 2e 0a 20 20 2a 2f 0a 20 20 70 4d 61 73 74  ce..  */.  pMast
10147 65 72 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69  er = (sqlite3_fi
10148 6c 65 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c  le *)sqlite3Mall
10149 6f 63 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c  oc(pVfs->szOsFil
1014a 65 20 2a 20 32 29 3b 0a 20 20 70 4a 6f 75 72 6e  e * 2);.  pJourn
1014b 61 6c 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69  al = (sqlite3_fi
1014c 6c 65 20 2a 29 28 28 28 75 38 20 2a 29 70 4d 61  le *)(((u8 *)pMa
1014d 73 74 65 72 29 20 2b 20 70 56 66 73 2d 3e 73 7a  ster) + pVfs->sz
1014e 4f 73 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 21  OsFile);.  if( !
1014f 70 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 72  pMaster ){.    r
10150 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
10151 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
10152 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 51 4c 49  nt flags = (SQLI
10153 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
10154 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53  |SQLITE_OPEN_MAS
10155 54 45 52 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20  TER_JOURNAL);.  
10156 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
10157 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4d 61 73 74  Open(pVfs, zMast
10158 65 72 2c 20 70 4d 61 73 74 65 72 2c 20 66 6c 61  er, pMaster, fla
10159 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66  gs, 0);.  }.  if
1015a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1015b 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72  ) goto delmaster
1015c 5f 6f 75 74 3b 0a 20 20 6d 61 73 74 65 72 5f 6f  _out;.  master_o
1015d 70 65 6e 20 3d 20 31 3b 0a 0a 20 20 72 63 20 3d  pen = 1;..  rc =
1015e 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69   sqlite3OsFileSi
1015f 7a 65 28 70 4d 61 73 74 65 72 2c 20 26 6e 4d 61  ze(pMaster, &nMa
10160 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20  sterJournal);.  
10161 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
10162 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74  K ) goto delmast
10163 65 72 5f 6f 75 74 3b 0a 0a 20 20 69 66 28 20 6e  er_out;..  if( n
10164 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3e 30 20  MasterJournal>0 
10165 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4a 6f  ){.    char *zJo
10166 75 72 6e 61 6c 3b 0a 20 20 20 20 63 68 61 72 20  urnal;.    char 
10167 2a 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 30 3b  *zMasterPtr = 0;
10168 0a 20 20 20 20 69 6e 74 20 6e 4d 61 73 74 65 72  .    int nMaster
10169 50 74 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 56  Ptr = pPager->pV
1016a 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31  fs->mxPathname+1
1016b 3b 0a 0a 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74  ;..    /* Load t
1016c 68 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65 72  he entire master
1016d 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e   journal file in
1016e 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65  to space obtaine
1016f 64 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 73 71  d from.    ** sq
10170 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61  lite3_malloc() a
10171 6e 64 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  nd pointed to by
10172 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2e   zMasterJournal.
10173 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 4d 61   .    */.    zMa
10174 73 74 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 28 63  sterJournal = (c
10175 68 61 72 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c  har *)sqlite3Mal
10176 6c 6f 63 28 6e 4d 61 73 74 65 72 4a 6f 75 72 6e  loc(nMasterJourn
10177 61 6c 20 2b 20 6e 4d 61 73 74 65 72 50 74 72 29  al + nMasterPtr)
10178 3b 0a 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74  ;.    if( !zMast
10179 65 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20  erJournal ){.   
1017a 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
1017b 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f  OMEM;.      goto
1017c 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a   delmaster_out;.
1017d 20 20 20 20 7d 0a 20 20 20 20 7a 4d 61 73 74 65      }.    zMaste
1017e 72 50 74 72 20 3d 20 26 7a 4d 61 73 74 65 72 4a  rPtr = &zMasterJ
1017f 6f 75 72 6e 61 6c 5b 6e 4d 61 73 74 65 72 4a 6f  ournal[nMasterJo
10180 75 72 6e 61 6c 5d 3b 0a 20 20 20 20 72 63 20 3d  urnal];.    rc =
10181 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70   sqlite3OsRead(p
10182 4d 61 73 74 65 72 2c 20 7a 4d 61 73 74 65 72 4a  Master, zMasterJ
10183 6f 75 72 6e 61 6c 2c 20 6e 4d 61 73 74 65 72 4a  ournal, nMasterJ
10184 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20  ournal, 0);.    
10185 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
10186 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74  K ) goto delmast
10187 65 72 5f 6f 75 74 3b 0a 0a 20 20 20 20 7a 4a 6f  er_out;..    zJo
10188 75 72 6e 61 6c 20 3d 20 7a 4d 61 73 74 65 72 4a  urnal = zMasterJ
10189 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 77 68 69 6c  ournal;.    whil
1018a 65 28 20 28 7a 4a 6f 75 72 6e 61 6c 2d 7a 4d 61  e( (zJournal-zMa
1018b 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3c 6e 4d 61  sterJournal)<nMa
1018c 73 74 65 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20  sterJournal ){. 
1018d 20 20 20 20 20 69 6e 74 20 65 78 69 73 74 73 3b       int exists;
1018e 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1018f 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 73  te3OsAccess(pVfs
10190 2c 20 7a 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c 49  , zJournal, SQLI
10191 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53  TE_ACCESS_EXISTS
10192 2c 20 26 65 78 69 73 74 73 29 3b 0a 20 20 20 20  , &exists);.    
10193 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
10194 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67  _OK ){.        g
10195 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75  oto delmaster_ou
10196 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  t;.      }.     
10197 20 69 66 28 20 65 78 69 73 74 73 20 29 7b 0a 20   if( exists ){. 
10198 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66         /* One of
10199 20 74 68 65 20 6a 6f 75 72 6e 61 6c 73 20 70 6f   the journals po
1019a 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 20  inted to by the 
1019b 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 65  master journal e
1019c 78 69 73 74 73 2e 0a 20 20 20 20 20 20 20 20 2a  xists..        *
1019d 2a 20 4f 70 65 6e 20 69 74 20 61 6e 64 20 63 68  * Open it and ch
1019e 65 63 6b 20 69 66 20 69 74 20 70 6f 69 6e 74 73  eck if it points
1019f 20 61 74 20 74 68 65 20 6d 61 73 74 65 72 20 6a   at the master j
101a0 6f 75 72 6e 61 6c 2e 20 49 66 0a 20 20 20 20 20  ournal. If.     
101a1 20 20 20 2a 2a 20 73 6f 2c 20 72 65 74 75 72 6e     ** so, return
101a2 20 77 69 74 68 6f 75 74 20 64 65 6c 65 74 69 6e   without deletin
101a3 67 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  g the master jou
101a4 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 20  rnal file..     
101a5 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e     */.        in
101a6 74 20 63 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  t c;.        int
101a7 20 66 6c 61 67 73 20 3d 20 28 53 51 4c 49 54 45   flags = (SQLITE
101a8 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53  _OPEN_READONLY|S
101a9 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
101aa 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 20 20  JOURNAL);.      
101ab 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
101ac 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4a 6f 75 72  Open(pVfs, zJour
101ad 6e 61 6c 2c 20 70 4a 6f 75 72 6e 61 6c 2c 20 66  nal, pJournal, f
101ae 6c 61 67 73 2c 20 30 29 3b 0a 20 20 20 20 20 20  lags, 0);.      
101af 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
101b0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
101b1 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f   goto delmaster_
101b2 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a  out;.        }..
101b3 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 61          rc = rea
101b4 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70  dMasterJournal(p
101b5 4a 6f 75 72 6e 61 6c 2c 20 7a 4d 61 73 74 65 72  Journal, zMaster
101b6 50 74 72 2c 20 6e 4d 61 73 74 65 72 50 74 72 29  Ptr, nMasterPtr)
101b7 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
101b8 33 4f 73 43 6c 6f 73 65 28 70 4a 6f 75 72 6e 61  3OsClose(pJourna
101b9 6c 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  l);.        if( 
101ba 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
101bb 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
101bc 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20  delmaster_out;. 
101bd 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
101be 20 20 63 20 3d 20 7a 4d 61 73 74 65 72 50 74 72    c = zMasterPtr
101bf 5b 30 5d 21 3d 30 20 26 26 20 73 74 72 63 6d 70  [0]!=0 && strcmp
101c0 28 7a 4d 61 73 74 65 72 50 74 72 2c 20 7a 4d 61  (zMasterPtr, zMa
101c1 73 74 65 72 29 3d 3d 30 3b 0a 20 20 20 20 20 20  ster)==0;.      
101c2 20 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 20    if( c ){.     
101c3 20 20 20 20 20 2f 2a 20 57 65 20 68 61 76 65 20       /* We have 
101c4 61 20 6d 61 74 63 68 2e 20 44 6f 20 6e 6f 74 20  a match. Do not 
101c5 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65  delete the maste
101c6 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  r journal file. 
101c7 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  */.          got
101c8 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b  o delmaster_out;
101c9 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
101ca 20 7d 0a 20 20 20 20 20 20 7a 4a 6f 75 72 6e 61   }.      zJourna
101cb 6c 20 2b 3d 20 28 73 74 72 6c 65 6e 28 7a 4a 6f  l += (strlen(zJo
101cc 75 72 6e 61 6c 29 2b 31 29 3b 0a 20 20 20 20 7d  urnal)+1);.    }
101cd 0a 20 20 7d 0a 20 20 0a 20 20 72 63 20 3d 20 73  .  }.  .  rc = s
101ce 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70  qlite3OsDelete(p
101cf 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29  Vfs, zMaster, 0)
101d0 3b 0a 0a 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74  ;..delmaster_out
101d1 3a 0a 20 20 69 66 28 20 7a 4d 61 73 74 65 72 4a  :.  if( zMasterJ
101d2 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 73 71  ournal ){.    sq
101d3 6c 69 74 65 33 5f 66 72 65 65 28 7a 4d 61 73 74  lite3_free(zMast
101d4 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 7d 20  erJournal);.  } 
101d5 20 0a 20 20 69 66 28 20 6d 61 73 74 65 72 5f 6f   .  if( master_o
101d6 70 65 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  pen ){.    sqlit
101d7 65 33 4f 73 43 6c 6f 73 65 28 70 4d 61 73 74 65  e3OsClose(pMaste
101d8 72 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  r);.  }.  sqlite
101d9 33 5f 66 72 65 65 28 70 4d 61 73 74 65 72 29 3b  3_free(pMaster);
101da 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
101db 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61  ..static void pa
101dc 67 65 72 5f 74 72 75 6e 63 61 74 65 5f 63 61 63  ger_truncate_cac
101dd 68 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  he(Pager *pPager
101de 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61  );../*.** Trunca
101df 74 65 20 74 68 65 20 6d 61 69 6e 20 66 69 6c 65  te the main file
101e0 20 6f 66 20 74 68 65 20 67 69 76 65 6e 20 70 61   of the given pa
101e1 67 65 72 20 74 6f 20 74 68 65 20 6e 75 6d 62 65  ger to the numbe
101e2 72 20 6f 66 20 70 61 67 65 73 0a 2a 2a 20 69 6e  r of pages.** in
101e3 64 69 63 61 74 65 64 2e 20 41 6c 73 6f 20 74 72  dicated. Also tr
101e4 75 6e 63 61 74 65 20 74 68 65 20 63 61 63 68 65  uncate the cache
101e5 64 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  d representation
101e6 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a   of the file..**
101e7 0a 2a 2a 20 4d 69 67 68 74 20 6d 69 67 68 74 20  .** Might might 
101e8 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74  be the case that
101e9 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73   the file on dis
101ea 6b 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61  k is smaller tha
101eb 6e 20 6e 50 61 67 65 2e 0a 2a 2a 20 54 68 69 73  n nPage..** This
101ec 20 63 61 6e 20 68 61 70 70 65 6e 2c 20 66 6f 72   can happen, for
101ed 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 77 65 20   example, if we 
101ee 61 72 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  are in the middl
101ef 65 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74 69  e of a transacti
101f0 6f 6e 0a 2a 2a 20 77 68 69 63 68 20 68 61 73 20  on.** which has 
101f1 65 78 74 65 6e 64 65 64 20 74 68 65 20 66 69 6c  extended the fil
101f2 65 20 73 69 7a 65 20 61 6e 64 20 74 68 65 20 6e  e size and the n
101f3 65 77 20 70 61 67 65 73 20 61 72 65 20 73 74 69  ew pages are sti
101f4 6c 6c 20 61 6c 6c 20 68 65 6c 64 0a 2a 2a 20 69  ll all held.** i
101f5 6e 20 63 61 63 68 65 2c 20 74 68 65 6e 20 61 6e  n cache, then an
101f6 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41 54   INSERT or UPDAT
101f7 45 20 64 6f 65 73 20 61 20 73 74 61 74 65 6d 65  E does a stateme
101f8 6e 74 20 72 6f 6c 6c 62 61 63 6b 2e 20 20 53 6f  nt rollback.  So
101f9 6d 65 0a 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20  me.** operating 
101fa 73 79 73 74 65 6d 20 69 6d 70 6c 65 6d 65 6e 74  system implement
101fb 61 74 69 6f 6e 73 20 63 61 6e 20 67 65 74 20 63  ations can get c
101fc 6f 6e 66 75 73 65 64 20 69 66 20 79 6f 75 20 74  onfused if you t
101fd 72 79 20 74 6f 0a 2a 2a 20 74 72 75 6e 63 61 74  ry to.** truncat
101fe 65 20 61 20 66 69 6c 65 20 74 6f 20 73 6f 6d 65  e a file to some
101ff 20 73 69 7a 65 20 74 68 61 74 20 69 73 20 6c 61   size that is la
10200 72 67 65 72 20 74 68 61 6e 20 69 74 20 63 75 72  rger than it cur
10201 72 65 6e 74 6c 79 20 69 73 2c 0a 2a 2a 20 73 6f  rently is,.** so
10202 20 64 65 74 65 63 74 20 74 68 69 73 20 63 61 73   detect this cas
10203 65 20 61 6e 64 20 77 72 69 74 65 20 61 20 73 69  e and write a si
10204 6e 67 6c 65 20 7a 65 72 6f 20 62 79 74 65 20 74  ngle zero byte t
10205 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  o the end of the
10206 20 6e 65 77 0a 2a 2a 20 66 69 6c 65 20 69 6e 73   new.** file ins
10207 74 65 61 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tead..*/.static 
10208 69 6e 74 20 70 61 67 65 72 5f 74 72 75 6e 63 61  int pager_trunca
10209 74 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  te(Pager *pPager
1020a 2c 20 69 6e 74 20 6e 50 61 67 65 29 7b 0a 20 20  , int nPage){.  
1020b 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1020c 4f 4b 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72  OK;.  if( pPager
1020d 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45  ->state>=PAGER_E
1020e 58 43 4c 55 53 49 56 45 20 26 26 20 70 50 61 67  XCLUSIVE && pPag
1020f 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73  er->fd->pMethods
10210 20 29 7b 0a 20 20 20 20 69 36 34 20 63 75 72 72   ){.    i64 curr
10211 65 6e 74 53 69 7a 65 2c 20 6e 65 77 53 69 7a 65  entSize, newSize
10212 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
10213 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61  e3OsFileSize(pPa
10214 67 65 72 2d 3e 66 64 2c 20 26 63 75 72 72 65 6e  ger->fd, &curren
10215 74 53 69 7a 65 29 3b 0a 20 20 20 20 6e 65 77 53  tSize);.    newS
10216 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 70 61  ize = pPager->pa
10217 67 65 53 69 7a 65 2a 28 69 36 34 29 6e 50 61 67  geSize*(i64)nPag
10218 65 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  e;.    if( rc==S
10219 51 4c 49 54 45 5f 4f 4b 20 26 26 20 63 75 72 72  QLITE_OK && curr
1021a 65 6e 74 53 69 7a 65 21 3d 6e 65 77 53 69 7a 65  entSize!=newSize
1021b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 63 75   ){.      if( cu
1021c 72 72 65 6e 74 53 69 7a 65 3e 6e 65 77 53 69 7a  rrentSize>newSiz
1021d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  e ){.        rc 
1021e 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63  = sqlite3OsTrunc
1021f 61 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20  ate(pPager->fd, 
10220 6e 65 77 53 69 7a 65 29 3b 0a 20 20 20 20 20 20  newSize);.      
10221 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72  }else{.        r
10222 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69  c = sqlite3OsWri
10223 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 22  te(pPager->fd, "
10224 22 2c 20 31 2c 20 6e 65 77 53 69 7a 65 2d 31 29  ", 1, newSize-1)
10225 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
10226 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
10227 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70  LITE_OK ){.    p
10228 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20  Pager->dbSize = 
10229 6e 50 61 67 65 3b 0a 20 20 20 20 70 61 67 65 72  nPage;.    pager
1022a 5f 74 72 75 6e 63 61 74 65 5f 63 61 63 68 65 28  _truncate_cache(
1022b 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 72  pPager);.  }.  r
1022c 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
1022d 2a 2a 20 53 65 74 20 74 68 65 20 73 65 63 74 6f  ** Set the secto
1022e 72 53 69 7a 65 20 66 6f 72 20 74 68 65 20 67 69  rSize for the gi
1022f 76 65 6e 20 70 61 67 65 72 2e 0a 2a 2a 0a 2a 2a  ven pager..**.**
10230 20 54 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65   The sector size
10231 20 69 73 20 61 74 20 6c 65 61 73 74 20 61 73 20   is at least as 
10232 62 69 67 20 61 73 20 74 68 65 20 73 65 63 74 6f  big as the secto
10233 72 20 73 69 7a 65 20 72 65 70 6f 72 74 65 64 0a  r size reported.
10234 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 4f 73 53  ** by sqlite3OsS
10235 65 63 74 6f 72 53 69 7a 65 28 29 2e 20 20 54 68  ectorSize().  Th
10236 65 20 6d 69 6e 69 6d 75 6d 20 73 65 63 74 6f 72  e minimum sector
10237 20 73 69 7a 65 20 69 73 20 35 31 32 2e 0a 2a 2f   size is 512..*/
10238 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74  .static void set
10239 53 65 63 74 6f 72 53 69 7a 65 28 50 61 67 65 72  SectorSize(Pager
1023a 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 61 73 73   *pPager){.  ass
1023b 65 72 74 28 70 50 61 67 65 72 2d 3e 66 64 2d 3e  ert(pPager->fd->
1023c 70 4d 65 74 68 6f 64 73 7c 7c 70 50 61 67 65 72  pMethods||pPager
1023d 2d 3e 74 65 6d 70 46 69 6c 65 29 3b 0a 20 20 69  ->tempFile);.  i
1023e 66 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70  f( !pPager->temp
1023f 46 69 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 53  File ){.    /* S
10240 65 63 74 6f 72 20 73 69 7a 65 20 64 6f 65 73 6e  ector size doesn
10241 27 74 20 6d 61 74 74 65 72 20 66 6f 72 20 74 65  't matter for te
10242 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 20 41  mporary files. A
10243 6c 73 6f 2c 20 74 68 65 20 66 69 6c 65 0a 20 20  lso, the file.  
10244 20 20 2a 2a 20 6d 61 79 20 6e 6f 74 20 68 61 76    ** may not hav
10245 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 79 65  e been opened ye
10246 74 2c 20 69 6e 20 77 68 63 69 68 20 63 61 73 65  t, in whcih case
10247 20 74 68 65 20 4f 73 53 65 63 74 6f 72 53 69 7a   the OsSectorSiz
10248 65 28 29 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20  e().    ** call 
10249 77 69 6c 6c 20 73 65 67 66 61 75 6c 74 2e 0a 20  will segfault.. 
1024a 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72     */.    pPager
1024b 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 73  ->sectorSize = s
1024c 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69  qlite3OsSectorSi
1024d 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a  ze(pPager->fd);.
1024e 20 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72    }.  if( pPager
1024f 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3c 35 31 32  ->sectorSize<512
10250 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   ){.    pPager->
10251 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 35 31 32  sectorSize = 512
10252 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  ;.  }.}../*.** P
10253 6c 61 79 62 61 63 6b 20 74 68 65 20 6a 6f 75 72  layback the jour
10254 6e 61 6c 20 61 6e 64 20 74 68 75 73 20 72 65 73  nal and thus res
10255 74 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73  tore the databas
10256 65 20 66 69 6c 65 20 74 6f 0a 2a 2a 20 74 68 65  e file to.** the
10257 20 73 74 61 74 65 20 69 74 20 77 61 73 20 69 6e   state it was in
10258 20 62 65 66 6f 72 65 20 77 65 20 73 74 61 72 74   before we start
10259 65 64 20 6d 61 6b 69 6e 67 20 63 68 61 6e 67 65  ed making change
1025a 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a  s.  .**.** The j
1025b 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 6d  ournal file form
1025c 61 74 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73  at is as follows
1025d 3a 20 0a 2a 2a 0a 2a 2a 20 20 28 31 29 20 20 38  : .**.**  (1)  8
1025e 20 62 79 74 65 20 70 72 65 66 69 78 2e 20 20 41   byte prefix.  A
1025f 20 63 6f 70 79 20 6f 66 20 61 4a 6f 75 72 6e 61   copy of aJourna
10260 6c 4d 61 67 69 63 5b 5d 2e 0a 2a 2a 20 20 28 32  lMagic[]..**  (2
10261 29 20 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e  )  4 byte big-en
10262 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69  dian integer whi
10263 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ch is the number
10264 20 6f 66 20 76 61 6c 69 64 20 70 61 67 65 20 72   of valid page r
10265 65 63 6f 72 64 73 0a 2a 2a 20 20 20 20 20 20 20  ecords.**       
10266 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20  in the journal. 
10267 20 49 66 20 74 68 69 73 20 76 61 6c 75 65 20 69   If this value i
10268 73 20 30 78 66 66 66 66 66 66 66 66 2c 20 74 68  s 0xffffffff, th
10269 65 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a  en compute the.*
1026a 2a 20 20 20 20 20 20 20 6e 75 6d 62 65 72 20 6f  *       number o
1026b 66 20 70 61 67 65 20 72 65 63 6f 72 64 73 20 66  f page records f
1026c 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  rom the journal 
1026d 73 69 7a 65 2e 0a 2a 2a 20 20 28 33 29 20 20 34  size..**  (3)  4
1026e 20 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e   byte big-endian
1026f 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69   integer which i
10270 73 20 74 68 65 20 69 6e 69 74 69 61 6c 20 76 61  s the initial va
10271 6c 75 65 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20  lue for the .** 
10272 20 20 20 20 20 20 73 61 6e 69 74 79 20 63 68 65        sanity che
10273 63 6b 73 75 6d 2e 0a 2a 2a 20 20 28 34 29 20 20  cksum..**  (4)  
10274 34 20 62 79 74 65 20 69 6e 74 65 67 65 72 20 77  4 byte integer w
10275 68 69 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62  hich is the numb
10276 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 74  er of pages to t
10277 72 75 6e 63 61 74 65 20 74 68 65 0a 2a 2a 20 20  runcate the.**  
10278 20 20 20 20 20 64 61 74 61 62 61 73 65 20 74 6f       database to
10279 20 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61   during a rollba
1027a 63 6b 2e 0a 2a 2a 20 20 28 35 29 20 20 34 20 62  ck..**  (5)  4 b
1027b 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69  yte big-endian i
1027c 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20  nteger which is 
1027d 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 2e  the sector size.
1027e 20 20 54 68 65 20 68 65 61 64 65 72 0a 2a 2a 20    The header.** 
1027f 20 20 20 20 20 20 69 73 20 74 68 69 73 20 6d 61        is this ma
10280 6e 79 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65  ny bytes in size
10281 2e 0a 2a 2a 20 20 28 36 29 20 20 34 20 62 79 74  ..**  (6)  4 byt
10282 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74  e big-endian int
10283 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68  eger which is th
10284 65 20 70 61 67 65 20 63 61 73 65 2e 0a 2a 2a 20  e page case..** 
10285 20 28 37 29 20 20 34 20 62 79 74 65 20 69 6e 74   (7)  4 byte int
10286 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68  eger which is th
10287 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
10288 73 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72 20  s in the master 
10289 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20  journal.**      
1028a 20 6e 61 6d 65 2e 20 20 54 68 65 20 76 61 6c 75   name.  The valu
1028b 65 20 6d 61 79 20 62 65 20 7a 65 72 6f 20 28 69  e may be zero (i
1028c 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68 65  ndicate that the
1028d 72 65 20 69 73 20 6e 6f 20 6d 61 73 74 65 72 0a  re is no master.
1028e 2a 2a 20 20 20 20 20 20 20 6a 6f 75 72 6e 61 6c  **       journal
1028f 2e 29 0a 2a 2a 20 20 28 38 29 20 20 4e 20 62 79  .).**  (8)  N by
10290 74 65 73 20 6f 66 20 74 68 65 20 6d 61 73 74 65  tes of the maste
10291 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e 20  r journal name. 
10292 20 54 68 65 20 6e 61 6d 65 20 77 69 6c 6c 20 62   The name will b
10293 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64  e nul-terminated
10294 0a 2a 2a 20 20 20 20 20 20 20 61 6e 64 20 6d 69  .**       and mi
10295 67 68 74 20 62 65 20 73 68 6f 72 74 65 72 20 74  ght be shorter t
10296 68 61 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65  han the value re
10297 61 64 20 66 72 6f 6d 20 28 35 29 2e 20 20 49 66  ad from (5).  If
10298 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 0a   the first byte.
10299 2a 2a 20 20 20 20 20 20 20 6f 66 20 74 68 65 20  **       of the 
1029a 6e 61 6d 65 20 69 73 20 5c 30 30 30 20 74 68 65  name is \000 the
1029b 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61  n there is no ma
1029c 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 20 54  ster journal.  T
1029d 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20 20 20 20  he master.**    
1029e 20 20 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20     journal name 
1029f 69 73 20 73 74 6f 72 65 64 20 69 6e 20 55 54 46  is stored in UTF
102a0 2d 38 2e 0a 2a 2a 20 20 28 39 29 20 20 5a 65 72  -8..**  (9)  Zer
102a1 6f 20 6f 72 20 6d 6f 72 65 20 70 61 67 65 73 20  o or more pages 
102a2 69 6e 73 74 61 6e 63 65 73 2c 20 65 61 63 68 20  instances, each 
102a3 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 20  as follows:.**  
102a4 20 20 20 20 20 20 2b 20 20 34 20 62 79 74 65 20        +  4 byte 
102a5 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20  page number..** 
102a6 20 20 20 20 20 20 20 2b 20 20 70 50 61 67 65 72         +  pPager
102a7 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73  ->pageSize bytes
102a8 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 20 20 20 20   of data..**    
102a9 20 20 20 20 2b 20 20 34 20 62 79 74 65 20 63 68      +  4 byte ch
102aa 65 63 6b 73 75 6d 0a 2a 2a 0a 2a 2a 20 57 68 65  ecksum.**.** Whe
102ab 6e 20 77 65 20 73 70 65 61 6b 20 6f 66 20 74 68  n we speak of th
102ac 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  e journal header
102ad 2c 20 77 65 20 6d 65 61 6e 20 74 68 65 20 66 69  , we mean the fi
102ae 72 73 74 20 38 20 69 74 65 6d 73 20 61 62 6f 76  rst 8 items abov
102af 65 2e 0a 2a 2a 20 45 61 63 68 20 65 6e 74 72 79  e..** Each entry
102b0 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   in the journal 
102b1 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
102b2 66 20 74 68 65 20 39 74 68 20 69 74 65 6d 2e 0a  f the 9th item..
102b3 2a 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68 65 20 76  **.** Call the v
102b4 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 73 65  alue from the se
102b5 63 6f 6e 64 20 62 75 6c 6c 65 74 20 22 6e 52 65  cond bullet "nRe
102b6 63 22 2e 20 20 6e 52 65 63 20 69 73 20 74 68 65  c".  nRec is the
102b7 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 76 61   number of.** va
102b8 6c 69 64 20 70 61 67 65 20 65 6e 74 72 69 65 73  lid page entries
102b9 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e   in the journal.
102ba 20 20 49 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c    In most cases,
102bb 20 79 6f 75 20 63 61 6e 20 63 6f 6d 70 75 74 65   you can compute
102bc 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66   the.** value of
102bd 20 6e 52 65 63 20 66 72 6f 6d 20 74 68 65 20 73   nRec from the s
102be 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  ize of the journ
102bf 61 6c 20 66 69 6c 65 2e 20 20 42 75 74 20 69 66  al file.  But if
102c0 20 61 20 70 6f 77 65 72 0a 2a 2a 20 66 61 69 6c   a power.** fail
102c1 75 72 65 20 6f 63 63 75 72 72 65 64 20 77 68 69  ure occurred whi
102c2 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77  le the journal w
102c3 61 73 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e  as being written
102c4 2c 20 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68  , it could be th
102c5 65 0a 2a 2a 20 63 61 73 65 20 74 68 61 74 20 74  e.** case that t
102c6 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a  he size of the j
102c7 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 64 20  ournal file had 
102c8 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69 6e 63  already been inc
102c9 72 65 61 73 65 64 20 62 75 74 0a 2a 2a 20 74 68  reased but.** th
102ca 65 20 65 78 74 72 61 20 65 6e 74 72 69 65 73 20  e extra entries 
102cb 68 61 64 20 6e 6f 74 20 79 65 74 20 6d 61 64 65  had not yet made
102cc 20 69 74 20 73 61 66 65 6c 79 20 74 6f 20 64 69   it safely to di
102cd 73 6b 2e 20 20 49 6e 20 73 75 63 68 20 61 20 63  sk.  In such a c
102ce 61 73 65 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75  ase,.** the valu
102cf 65 20 6f 66 20 6e 52 65 63 20 63 6f 6d 70 75 74  e of nRec comput
102d0 65 64 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65  ed from the file
102d1 20 73 69 7a 65 20 77 6f 75 6c 64 20 62 65 20 74   size would be t
102d2 6f 6f 20 6c 61 72 67 65 2e 20 20 46 6f 72 0a 2a  oo large.  For.*
102d3 2a 20 74 68 61 74 20 72 65 61 73 6f 6e 2c 20 77  * that reason, w
102d4 65 20 61 6c 77 61 79 73 20 75 73 65 20 74 68 65  e always use the
102d5 20 6e 52 65 63 20 76 61 6c 75 65 20 69 6e 20 74   nRec value in t
102d6 68 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a  he header..**.**
102d7 20 49 66 20 74 68 65 20 6e 52 65 63 20 76 61 6c   If the nRec val
102d8 75 65 20 69 73 20 30 78 66 66 66 66 66 66 66 66  ue is 0xffffffff
102d9 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 6e   it means that n
102da 52 65 63 20 73 68 6f 75 6c 64 20 62 65 20 63 6f  Rec should be co
102db 6d 70 75 74 65 64 0a 2a 2a 20 66 72 6f 6d 20 74  mputed.** from t
102dc 68 65 20 66 69 6c 65 20 73 69 7a 65 2e 20 20 54  he file size.  T
102dd 68 69 73 20 76 61 6c 75 65 20 69 73 20 75 73 65  his value is use
102de 64 20 77 68 65 6e 20 74 68 65 20 75 73 65 72 20  d when the user 
102df 73 65 6c 65 63 74 73 20 74 68 65 0a 2a 2a 20 6e  selects the.** n
102e0 6f 2d 73 79 6e 63 20 6f 70 74 69 6f 6e 20 66 6f  o-sync option fo
102e1 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20  r the journal.  
102e2 41 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20  A power failure 
102e3 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f 20 63 6f  could lead to co
102e4 72 72 75 70 74 69 6f 6e 0a 2a 2a 20 69 6e 20 74  rruption.** in t
102e5 68 69 73 20 63 61 73 65 2e 20 20 42 75 74 20 66  his case.  But f
102e6 6f 72 20 74 68 69 6e 67 73 20 6c 69 6b 65 20 74  or things like t
102e7 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 28  emporary table (
102e8 77 68 69 63 68 20 77 69 6c 6c 20 62 65 0a 2a 2a  which will be.**
102e9 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68   deleted when th
102ea 65 20 70 6f 77 65 72 20 69 73 20 72 65 73 74 6f  e power is resto
102eb 72 65 64 29 20 77 65 20 64 6f 6e 27 74 20 63 61  red) we don't ca
102ec 72 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74  re.  .**.** If t
102ed 68 65 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 61  he file opened a
102ee 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  s the journal fi
102ef 6c 65 20 69 73 20 6e 6f 74 20 61 20 77 65 6c 6c  le is not a well
102f0 2d 66 6f 72 6d 65 64 0a 2a 2a 20 6a 6f 75 72 6e  -formed.** journ
102f1 61 6c 20 66 69 6c 65 20 74 68 65 6e 20 61 6c 6c  al file then all
102f2 20 70 61 67 65 73 20 75 70 20 74 6f 20 74 68 65   pages up to the
102f3 20 66 69 72 73 74 20 63 6f 72 72 75 70 74 65 64   first corrupted
102f4 20 70 61 67 65 20 61 72 65 20 72 6f 6c 6c 65 64   page are rolled
102f5 0a 2a 2a 20 62 61 63 6b 20 28 6f 72 20 6e 6f 20  .** back (or no 
102f6 70 61 67 65 73 20 69 66 20 74 68 65 20 6a 6f 75  pages if the jou
102f7 72 6e 61 6c 20 68 65 61 64 65 72 20 69 73 20 63  rnal header is c
102f8 6f 72 72 75 70 74 65 64 29 2e 20 54 68 65 20 6a  orrupted). The j
102f9 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 69  ournal file.** i
102fa 73 20 74 68 65 6e 20 64 65 6c 65 74 65 64 20 61  s then deleted a
102fb 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74  nd SQLITE_OK ret
102fc 75 72 6e 65 64 2c 20 6a 75 73 74 20 61 73 20 69  urned, just as i
102fd 66 20 6e 6f 20 63 6f 72 72 75 70 74 69 6f 6e 20  f no corruption 
102fe 68 61 64 0a 2a 2a 20 62 65 65 6e 20 65 6e 63 6f  had.** been enco
102ff 75 6e 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  untered..**.** I
10300 66 20 61 6e 20 49 2f 4f 20 6f 72 20 6d 61 6c 6c  f an I/O or mall
10301 6f 63 28 29 20 65 72 72 6f 72 20 6f 63 63 75 72  oc() error occur
10302 73 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 66  s, the journal-f
10303 69 6c 65 20 69 73 20 6e 6f 74 20 64 65 6c 65 74  ile is not delet
10304 65 64 0a 2a 2a 20 61 6e 64 20 61 6e 20 65 72 72  ed.** and an err
10305 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
10306 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ned..*/.static i
10307 6e 74 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63  nt pager_playbac
10308 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  k(Pager *pPager,
10309 20 69 6e 74 20 69 73 48 6f 74 29 7b 0a 20 20 73   int isHot){.  s
1030a 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
1030b 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b   = pPager->pVfs;
1030c 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20 20 20 20  .  i64 szJ;     
1030d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
1030e 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  ize of the journ
1030f 61 6c 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73  al file in bytes
10310 20 2a 2f 0a 20 20 75 33 32 20 6e 52 65 63 3b 20   */.  u32 nRec; 
10311 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10312 2a 20 4e 75 6d 62 65 72 20 6f 66 20 52 65 63 6f  * Number of Reco
10313 72 64 73 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  rds in the journ
10314 61 6c 20 2a 2f 0a 20 20 75 33 32 20 75 3b 20 20  al */.  u32 u;  
10315 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10316 20 2f 2a 20 55 6e 73 69 67 6e 65 64 20 6c 6f 6f   /* Unsigned loo
10317 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 50  p counter */.  P
10318 67 6e 6f 20 6d 78 50 67 20 3d 20 30 3b 20 20 20  gno mxPg = 0;   
10319 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
1031a 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  of the original 
1031b 66 69 6c 65 20 69 6e 20 70 61 67 65 73 20 2a 2f  file in pages */
1031c 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
1031d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
1031e 65 73 75 6c 74 20 63 6f 64 65 20 6f 66 20 61 20  esult code of a 
1031f 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20  subroutine */.  
10320 69 6e 74 20 72 65 73 20 3d 20 31 3b 20 20 20 20  int res = 1;    
10321 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
10322 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71  e returned by sq
10323 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 29 20  lite3OsAccess() 
10324 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 73 74  */.  char *zMast
10325 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a  er = 0;       /*
10326 20 4e 61 6d 65 20 6f 66 20 6d 61 73 74 65 72 20   Name of master 
10327 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20  journal file if 
10328 61 6e 79 20 2a 2f 0a 0a 20 20 2f 2a 20 46 69 67  any */..  /* Fig
10329 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79  ure out how many
1032a 20 72 65 63 6f 72 64 73 20 61 72 65 20 69 6e 20   records are in 
1032b 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 62  the journal.  Ab
1032c 6f 72 74 20 65 61 72 6c 79 20 69 66 0a 20 20 2a  ort early if.  *
1032d 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73  * the journal is
1032e 20 65 6d 70 74 79 2e 0a 20 20 2a 2f 0a 20 20 61   empty..  */.  a
1032f 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a  ssert( pPager->j
10330 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 3b 0a 20 20  ournalOpen );.  
10331 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69  rc = sqlite3OsFi
10332 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a  leSize(pPager->j
10333 66 64 2c 20 26 73 7a 4a 29 3b 0a 20 20 69 66 28  fd, &szJ);.  if(
10334 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
10335 7c 20 73 7a 4a 3d 3d 30 20 29 7b 0a 20 20 20 20  | szJ==0 ){.    
10336 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63  goto end_playbac
10337 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61  k;.  }..  /* Rea
10338 64 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  d the master jou
10339 72 6e 61 6c 20 6e 61 6d 65 20 66 72 6f 6d 20 74  rnal name from t
1033a 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 69 66 20 69  he journal, if i
1033b 74 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20  t is present..  
1033c 2a 2a 20 49 66 20 61 20 6d 61 73 74 65 72 20 6a  ** If a master j
1033d 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65  ournal file name
1033e 20 69 73 20 73 70 65 63 69 66 69 65 64 2c 20 62   is specified, b
1033f 75 74 20 74 68 65 20 66 69 6c 65 20 69 73 20 6e  ut the file is n
10340 6f 74 0a 20 20 2a 2a 20 70 72 65 73 65 6e 74 20  ot.  ** present 
10341 6f 6e 20 64 69 73 6b 2c 20 74 68 65 6e 20 74 68  on disk, then th
10342 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6e 6f 74  e journal is not
10343 20 68 6f 74 20 61 6e 64 20 64 6f 65 73 20 6e 6f   hot and does no
10344 74 20 6e 65 65 64 20 74 6f 20 62 65 0a 20 20 2a  t need to be.  *
10345 2a 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a 20  * played back.. 
10346 20 2a 2f 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20   */.  zMaster = 
10347 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63  pPager->pTmpSpac
10348 65 3b 0a 20 20 72 63 20 3d 20 72 65 61 64 4d 61  e;.  rc = readMa
10349 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67  sterJournal(pPag
1034a 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72  er->jfd, zMaster
1034b 2c 20 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e  , pPager->pVfs->
1034c 6d 78 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20  mxPathname+1);. 
1034d 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1034e 4f 4b 20 26 26 20 7a 4d 61 73 74 65 72 5b 30 5d  OK && zMaster[0]
1034f 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
10350 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66  ite3OsAccess(pVf
10351 73 2c 20 7a 4d 61 73 74 65 72 2c 20 53 51 4c 49  s, zMaster, SQLI
10352 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53  TE_ACCESS_EXISTS
10353 2c 20 26 72 65 73 29 3b 0a 20 20 7d 0a 20 20 7a  , &res);.  }.  z
10354 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 69 66  Master = 0;.  if
10355 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
10356 7c 7c 20 21 72 65 73 20 29 7b 0a 20 20 20 20 67  || !res ){.    g
10357 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b  oto end_playback
10358 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e  ;.  }.  pPager->
10359 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a  journalOff = 0;.
1035a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20  .  /* This loop 
1035b 74 65 72 6d 69 6e 61 74 65 73 20 65 69 74 68 65  terminates eithe
1035c 72 20 77 68 65 6e 20 74 68 65 20 72 65 61 64 4a  r when the readJ
1035d 6f 75 72 6e 61 6c 48 64 72 28 29 20 63 61 6c 6c  ournalHdr() call
1035e 20 72 65 74 75 72 6e 73 0a 20 20 2a 2a 20 53 51   returns.  ** SQ
1035f 4c 49 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20  LITE_DONE or an 
10360 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e  IO error occurs.
10361 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 31 20 29   */.  while( 1 )
10362 7b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74  {..    /* Read t
10363 68 65 20 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20  he next journal 
10364 68 65 61 64 65 72 20 66 72 6f 6d 20 74 68 65 20  header from the 
10365 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 49  journal file.  I
10366 66 20 74 68 65 72 65 20 61 72 65 0a 20 20 20 20  f there are.    
10367 2a 2a 20 6e 6f 74 20 65 6e 6f 75 67 68 20 62 79  ** not enough by
10368 74 65 73 20 6c 65 66 74 20 69 6e 20 74 68 65 20  tes left in the 
10369 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72  journal file for
1036a 20 61 20 63 6f 6d 70 6c 65 74 65 20 68 65 61 64   a complete head
1036b 65 72 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 69 74  er, or.    ** it
1036c 20 69 73 20 63 6f 72 72 75 70 74 65 64 2c 20 74   is corrupted, t
1036d 68 65 6e 20 61 20 70 72 6f 63 65 73 73 20 6d 75  hen a process mu
1036e 73 74 20 6f 66 20 66 61 69 6c 65 64 20 77 68 69  st of failed whi
1036f 6c 65 20 77 72 69 74 69 6e 67 20 69 74 2e 0a 20  le writing it.. 
10370 20 20 20 2a 2a 20 54 68 69 73 20 69 6e 64 69 63     ** This indic
10371 61 74 65 73 20 6e 6f 74 68 69 6e 67 20 6d 6f 72  ates nothing mor
10372 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 6f  e needs to be ro
10373 6c 6c 65 64 20 62 61 63 6b 2e 0a 20 20 20 20 2a  lled back..    *
10374 2f 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64 4a  /.    rc = readJ
10375 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72  ournalHdr(pPager
10376 2c 20 73 7a 4a 2c 20 26 6e 52 65 63 2c 20 26 6d  , szJ, &nRec, &m
10377 78 50 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63  xPg);.    if( rc
10378 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 0a  !=SQLITE_OK ){ .
10379 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
1037a 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20  LITE_DONE ){.   
1037b 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1037c 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  _OK;.      }.   
1037d 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79     goto end_play
1037e 62 61 63 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  back;.    }..   
1037f 20 2f 2a 20 49 66 20 6e 52 65 63 20 69 73 20 30   /* If nRec is 0
10380 78 66 66 66 66 66 66 66 66 2c 20 74 68 65 6e 20  xffffffff, then 
10381 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61 73  this journal was
10382 20 63 72 65 61 74 65 64 20 62 79 20 61 20 70 72   created by a pr
10383 6f 63 65 73 73 0a 20 20 20 20 2a 2a 20 77 6f 72  ocess.    ** wor
10384 6b 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63 20  king in no-sync 
10385 6d 6f 64 65 2e 20 54 68 69 73 20 6d 65 61 6e 73  mode. This means
10386 20 74 68 61 74 20 74 68 65 20 72 65 73 74 20 6f   that the rest o
10387 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20  f the journal.  
10388 20 20 2a 2a 20 66 69 6c 65 20 63 6f 6e 73 69 73    ** file consis
10389 74 73 20 6f 66 20 70 61 67 65 73 2c 20 74 68 65  ts of pages, the
1038a 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 6a  re are no more j
1038b 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 73 2e 20  ournal headers. 
1038c 43 6f 6d 70 75 74 65 0a 20 20 20 20 2a 2a 20 74  Compute.    ** t
1038d 68 65 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63  he value of nRec
1038e 20 62 61 73 65 64 20 6f 6e 20 74 68 69 73 20 61   based on this a
1038f 73 73 75 6d 70 74 69 6f 6e 2e 0a 20 20 20 20 2a  ssumption..    *
10390 2f 0a 20 20 20 20 69 66 28 20 6e 52 65 63 3d 3d  /.    if( nRec==
10391 30 78 66 66 66 66 66 66 66 66 20 29 7b 0a 20 20  0xffffffff ){.  
10392 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
10393 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d  er->journalOff==
10394 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70  JOURNAL_HDR_SZ(p
10395 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 20 20  Pager) );.      
10396 6e 52 65 63 20 3d 20 28 73 7a 4a 20 2d 20 4a 4f  nRec = (szJ - JO
10397 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61  URNAL_HDR_SZ(pPa
10398 67 65 72 29 29 2f 4a 4f 55 52 4e 41 4c 5f 50 47  ger))/JOURNAL_PG
10399 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 20  _SZ(pPager);.   
1039a 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 6e 52   }..    /* If nR
1039b 65 63 20 69 73 20 30 20 61 6e 64 20 74 68 69 73  ec is 0 and this
1039c 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 6f 66 20   rollback is of 
1039d 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 72  a transaction cr
1039e 65 61 74 65 64 20 62 79 20 74 68 69 73 0a 20 20  eated by this.  
1039f 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 61 6e 64    ** process and
103a0 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20   if this is the 
103a1 66 69 6e 61 6c 20 68 65 61 64 65 72 20 69 6e 20  final header in 
103a2 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 65  the journal, the
103a3 6e 20 69 74 20 6d 65 61 6e 73 0a 20 20 20 20 2a  n it means.    *
103a4 2a 20 74 68 61 74 20 74 68 69 73 20 70 61 72 74  * that this part
103a5 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
103a6 77 61 73 20 62 65 69 6e 67 20 66 69 6c 6c 65 64  was being filled
103a7 20 62 75 74 20 68 61 73 20 6e 6f 74 20 79 65 74   but has not yet
103a8 20 62 65 65 6e 0a 20 20 20 20 2a 2a 20 73 79 6e   been.    ** syn
103a9 63 65 64 20 74 6f 20 64 69 73 6b 2e 20 20 43 6f  ced to disk.  Co
103aa 6d 70 75 74 65 20 74 68 65 20 6e 75 6d 62 65 72  mpute the number
103ab 20 6f 66 20 70 61 67 65 73 20 62 61 73 65 64 20   of pages based 
103ac 6f 6e 20 74 68 65 20 72 65 6d 61 69 6e 69 6e 67  on the remaining
103ad 0a 20 20 20 20 2a 2a 20 73 69 7a 65 20 6f 66 20  .    ** size of 
103ae 74 68 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a  the file..    **
103af 0a 20 20 20 20 2a 2a 20 54 68 65 20 74 68 69 72  .    ** The thir
103b0 64 20 74 65 72 6d 20 6f 66 20 74 68 65 20 74 65  d term of the te
103b1 73 74 20 77 61 73 20 61 64 64 65 64 20 74 6f 20  st was added to 
103b2 66 69 78 20 74 69 63 6b 65 74 20 23 32 35 36 35  fix ticket #2565
103b3 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
103b4 20 6e 52 65 63 3d 3d 30 20 26 26 20 21 69 73 48   nRec==0 && !isH
103b5 6f 74 20 26 26 0a 20 20 20 20 20 20 20 20 70 50  ot &&.        pP
103b6 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72  ager->journalHdr
103b7 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28  +JOURNAL_HDR_SZ(
103b8 70 50 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d  pPager)==pPager-
103b9 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 29 7b 0a 20  >journalOff ){. 
103ba 20 20 20 20 20 6e 52 65 63 20 3d 20 28 73 7a 4a       nRec = (szJ
103bb 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e   - pPager->journ
103bc 61 6c 4f 66 66 29 20 2f 20 4a 4f 55 52 4e 41 4c  alOff) / JOURNAL
103bd 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a  _PG_SZ(pPager);.
103be 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66      }..    /* If
103bf 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72   this is the fir
103c0 73 74 20 68 65 61 64 65 72 20 72 65 61 64 20 66  st header read f
103c1 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c  rom the journal,
103c2 20 74 72 75 6e 63 61 74 65 20 74 68 65 0a 20 20   truncate the.  
103c3 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69    ** database fi
103c4 6c 65 20 62 61 63 6b 20 74 6f 20 69 74 73 20 6f  le back to its o
103c5 72 69 67 69 6e 61 6c 20 73 69 7a 65 2e 0a 20 20  riginal size..  
103c6 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61    */.    if( pPa
103c7 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d  ger->journalOff=
103c8 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28  =JOURNAL_HDR_SZ(
103c9 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20  pPager) ){.     
103ca 20 72 63 20 3d 20 70 61 67 65 72 5f 74 72 75 6e   rc = pager_trun
103cb 63 61 74 65 28 70 50 61 67 65 72 2c 20 6d 78 50  cate(pPager, mxP
103cc 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  g);.      if( rc
103cd 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
103ce 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
103cf 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 20 20  playback;.      
103d0 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
103d1 43 6f 70 79 20 6f 72 69 67 69 6e 61 6c 20 70 61  Copy original pa
103d2 67 65 73 20 6f 75 74 20 6f 66 20 74 68 65 20 6a  ges out of the j
103d3 6f 75 72 6e 61 6c 20 61 6e 64 20 62 61 63 6b 20  ournal and back 
103d4 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  into the databas
103d5 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20  e file..    */. 
103d6 20 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6e 52     for(u=0; u<nR
103d7 65 63 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 20 20  ec; u++){.      
103d8 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62  rc = pager_playb
103d9 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61  ack_one_page(pPa
103da 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64  ger, pPager->jfd
103db 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  , pPager->journa
103dc 6c 4f 66 66 2c 20 31 29 3b 0a 20 20 20 20 20 20  lOff, 1);.      
103dd 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
103de 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  K ){.        if(
103df 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc==SQLITE_DONE
103e0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
103e1 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
103e2 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
103e3 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a  journalOff = szJ
103e4 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
103e5 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  k;.        }else
103e6 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  {.          /* I
103e7 66 20 77 65 20 61 72 65 20 75 6e 61 62 6c 65 20  f we are unable 
103e8 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65  to rollback, the
103e9 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  n the database i
103ea 73 20 70 72 6f 62 61 62 6c 79 0a 20 20 20 20 20  s probably.     
103eb 20 20 20 20 20 2a 2a 20 67 6f 69 6e 67 20 74 6f       ** going to
103ec 20 65 6e 64 20 75 70 20 62 65 69 6e 67 20 63 6f   end up being co
103ed 72 72 75 70 74 2e 20 20 49 74 20 69 73 20 63 6f  rrupt.  It is co
103ee 72 72 75 70 74 20 74 6f 20 75 73 2c 20 61 6e 79  rrupt to us, any
103ef 68 6f 77 2e 0a 20 20 20 20 20 20 20 20 20 20 2a  how..          *
103f0 2a 20 50 65 72 68 61 70 73 20 74 68 65 20 6e 65  * Perhaps the ne
103f1 78 74 20 70 72 6f 63 65 73 73 20 74 6f 20 63 6f  xt process to co
103f2 6d 65 20 61 6c 6f 6e 67 20 63 61 6e 20 66 69 78  me along can fix
103f3 20 69 74 2e 2e 2e 2e 0a 20 20 20 20 20 20 20 20   it.....        
103f4 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72    */.          r
103f5 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  c = SQLITE_CORRU
103f6 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20  PT_BKPT;.       
103f7 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79     goto end_play
103f8 62 61 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  back;.        }.
103f9 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
103fa 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 48 45 44  }.  /*NOTREACHED
103fb 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 30 20 29  */.  assert( 0 )
103fc 3b 0a 0a 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3a  ;..end_playback:
103fd 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
103fe 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 7a 4d 61 73  E_OK ){.    zMas
103ff 74 65 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 54  ter = pPager->pT
10400 6d 70 53 70 61 63 65 3b 0a 20 20 20 20 72 63 20  mpSpace;.    rc 
10401 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72  = readMasterJour
10402 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  nal(pPager->jfd,
10403 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65 72   zMaster, pPager
10404 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61  ->pVfs->mxPathna
10405 6d 65 2b 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28  me+1);.  }.  if(
10406 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
10407 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72  {.    rc = pager
10408 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e  _end_transaction
10409 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72  (pPager, zMaster
1040a 5b 30 5d 21 3d 27 5c 30 27 29 3b 0a 20 20 7d 0a  [0]!='\0');.  }.
1040b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1040c 5f 4f 4b 20 26 26 20 7a 4d 61 73 74 65 72 5b 30  _OK && zMaster[0
1040d 5d 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20  ] && res ){.    
1040e 2f 2a 20 49 66 20 74 68 65 72 65 20 77 61 73 20  /* If there was 
1040f 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  a master journal
10410 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e   and this routin
10411 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 73 75  e will return su
10412 63 63 65 73 73 2c 0a 20 20 20 20 2a 2a 20 73 65  ccess,.    ** se
10413 65 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 69  e if it is possi
10414 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 74 68  ble to delete th
10415 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
10416 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20  ..    */.    rc 
10417 3d 20 70 61 67 65 72 5f 64 65 6c 6d 61 73 74 65  = pager_delmaste
10418 72 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65  r(pPager, zMaste
10419 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68  r);.  }..  /* Th
1041a 65 20 50 61 67 65 72 2e 73 65 63 74 6f 72 53 69  e Pager.sectorSi
1041b 7a 65 20 76 61 72 69 61 62 6c 65 20 6d 61 79 20  ze variable may 
1041c 68 61 76 65 20 62 65 65 6e 20 75 70 64 61 74 65  have been update
1041d 64 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e 67 0a  d while rolling.
1041e 20 20 2a 2a 20 62 61 63 6b 20 61 20 6a 6f 75 72    ** back a jour
1041f 6e 61 6c 20 63 72 65 61 74 65 64 20 62 79 20 61  nal created by a
10420 20 70 72 6f 63 65 73 73 20 77 69 74 68 20 61 20   process with a 
10421 64 69 66 66 65 72 65 6e 74 20 73 65 63 74 6f 72  different sector
10422 20 73 69 7a 65 0a 20 20 2a 2a 20 76 61 6c 75 65   size.  ** value
10423 2e 20 52 65 73 65 74 20 69 74 20 74 6f 20 74 68  . Reset it to th
10424 65 20 63 6f 72 72 65 63 74 20 76 61 6c 75 65 20  e correct value 
10425 66 6f 72 20 74 68 69 73 20 70 72 6f 63 65 73 73  for this process
10426 2e 0a 20 20 2a 2f 0a 20 20 73 65 74 53 65 63 74  ..  */.  setSect
10427 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a  orSize(pPager);.
10428 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
10429 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b 20 74  /*.** Playback t
1042a 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75  he statement jou
1042b 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  rnal..**.** This
1042c 20 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 70   is similar to p
1042d 6c 61 79 69 6e 67 20 62 61 63 6b 20 74 68 65 20  laying back the 
1042e 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72  transaction jour
1042f 6e 61 6c 20 62 75 74 20 77 69 74 68 0a 2a 2a 20  nal but with.** 
10430 61 20 66 65 77 20 65 78 74 72 61 20 74 77 69 73  a few extra twis
10431 74 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29  ts..**.**    (1)
10432 20 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20    The number of 
10433 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74  pages in the dat
10434 61 62 61 73 65 20 66 69 6c 65 20 61 74 20 74 68  abase file at th
10435 65 20 73 74 61 72 74 20 6f 66 0a 2a 2a 20 20 20  e start of.**   
10436 20 20 20 20 20 20 74 68 65 20 73 74 61 74 65 6d        the statem
10437 65 6e 74 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ent is stored in
10438 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a   pPager->stmtSiz
10439 65 2c 20 6e 6f 74 20 69 6e 20 74 68 65 0a 2a 2a  e, not in the.**
1043a 20 20 20 20 20 20 20 20 20 6a 6f 75 72 6e 61 6c           journal
1043b 20 66 69 6c 65 20 69 74 73 65 6c 66 2e 0a 2a 2a   file itself..**
1043c 0a 2a 2a 20 20 20 20 28 32 29 20 20 49 6e 20 61  .**    (2)  In a
1043d 64 64 69 74 69 6f 6e 20 74 6f 20 70 6c 61 79 69  ddition to playi
1043e 6e 67 20 62 61 63 6b 20 74 68 65 20 73 74 61 74  ng back the stat
1043f 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2c 20 61  ement journal, a
10440 6c 73 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 70  lso.**         p
10441 6c 61 79 62 61 63 6b 20 61 6c 6c 20 70 61 67 65  layback all page
10442 73 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63  s of the transac
10443 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 62 65 67  tion journal beg
10444 69 6e 6e 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20  inning.**       
10445 20 20 61 74 20 6f 66 66 73 65 74 20 70 50 61 67    at offset pPag
10446 65 72 2d 3e 73 74 6d 74 4a 53 69 7a 65 2e 0a 2a  er->stmtJSize..*
10447 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
10448 65 72 5f 73 74 6d 74 5f 70 6c 61 79 62 61 63 6b  er_stmt_playback
10449 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
1044a 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20 20 20 20  .  i64 szJ;     
1044b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
1044c 69 7a 65 20 6f 66 20 74 68 65 20 66 75 6c 6c 20  ize of the full 
1044d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34  journal */.  i64
1044e 20 68 64 72 4f 66 66 3b 0a 20 20 69 6e 74 20 6e   hdrOff;.  int n
1044f 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  Rec;            
10450 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
10451 20 52 65 63 6f 72 64 73 20 2a 2f 0a 20 20 69 6e   Records */.  in
10452 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
10453 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
10454 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  ounter */.  int 
10455 72 63 3b 0a 0a 20 20 73 7a 4a 20 3d 20 70 50 61  rc;..  szJ = pPa
10456 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b  ger->journalOff;
10457 0a 0a 20 20 2f 2a 20 53 65 74 20 68 64 72 4f 66  ..  /* Set hdrOf
10458 66 20 74 6f 20 62 65 20 74 68 65 20 6f 66 66 73  f to be the offs
10459 65 74 20 6a 75 73 74 20 61 66 74 65 72 20 74 68  et just after th
1045a 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 61 73  e end of the las
1045b 74 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 70  t journal.  ** p
1045c 61 67 65 20 77 72 69 74 74 65 6e 20 62 65 66 6f  age written befo
1045d 72 65 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75  re the first jou
1045e 72 6e 61 6c 2d 68 65 61 64 65 72 20 66 6f 72 20  rnal-header for 
1045f 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 0a 20  this statement. 
10460 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20   ** transaction 
10461 77 61 73 20 77 72 69 74 74 65 6e 2c 20 6f 72 20  was written, or 
10462 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66  the end of the f
10463 69 6c 65 20 69 66 20 6e 6f 20 6a 6f 75 72 6e 61  ile if no journa
10464 6c 0a 20 20 2a 2a 20 68 65 61 64 65 72 20 77 61  l.  ** header wa
10465 73 20 77 72 69 74 74 65 6e 2e 0a 20 20 2a 2f 0a  s written..  */.
10466 20 20 68 64 72 4f 66 66 20 3d 20 70 50 61 67 65    hdrOff = pPage
10467 72 2d 3e 73 74 6d 74 48 64 72 4f 66 66 3b 0a 20  r->stmtHdrOff;. 
10468 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
10469 3e 66 75 6c 6c 53 79 6e 63 20 7c 7c 20 21 68 64  >fullSync || !hd
1046a 72 4f 66 66 20 29 3b 0a 20 20 69 66 28 20 21 68  rOff );.  if( !h
1046b 64 72 4f 66 66 20 29 7b 0a 20 20 20 20 68 64 72  drOff ){.    hdr
1046c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 7d 0a 20  Off = szJ;.  }. 
1046d 20 0a 20 20 2f 2a 20 54 72 75 6e 63 61 74 65 20   .  /* Truncate 
1046e 74 68 65 20 64 61 74 61 62 61 73 65 20 62 61 63  the database bac
1046f 6b 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61  k to its origina
10470 6c 20 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 72  l size..  */.  r
10471 63 20 3d 20 70 61 67 65 72 5f 74 72 75 6e 63 61  c = pager_trunca
10472 74 65 28 70 50 61 67 65 72 2c 20 70 50 61 67 65  te(pPager, pPage
10473 72 2d 3e 73 74 6d 74 53 69 7a 65 29 3b 0a 20 20  r->stmtSize);.  
10474 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
10475 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41  state>=PAGER_SHA
10476 52 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 67  RED );..  /* Fig
10477 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79  ure out how many
10478 20 72 65 63 6f 72 64 73 20 61 72 65 20 69 6e 20   records are in 
10479 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f  the statement jo
1047a 75 72 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 61 73  urnal..  */.  as
1047b 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74  sert( pPager->st
1047c 6d 74 49 6e 55 73 65 20 26 26 20 70 50 61 67 65  mtInUse && pPage
1047d 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29  r->journalOpen )
1047e 3b 0a 20 20 6e 52 65 63 20 3d 20 70 50 61 67 65  ;.  nRec = pPage
1047f 72 2d 3e 73 74 6d 74 4e 52 65 63 3b 0a 20 20 0a  r->stmtNRec;.  .
10480 20 20 2f 2a 20 43 6f 70 79 20 6f 72 69 67 69 6e    /* Copy origin
10481 61 6c 20 70 61 67 65 73 20 6f 75 74 20 6f 66 20  al pages out of 
10482 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f  the statement jo
10483 75 72 6e 61 6c 20 61 6e 64 20 62 61 63 6b 20 69  urnal and back i
10484 6e 74 6f 20 74 68 65 0a 20 20 2a 2a 20 64 61 74  nto the.  ** dat
10485 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4e 6f 74  abase file.  Not
10486 65 20 74 68 61 74 20 74 68 65 20 73 74 61 74 65  e that the state
10487 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 6f 6d 69  ment journal omi
10488 74 73 20 63 68 65 63 6b 73 75 6d 73 20 66 72 6f  ts checksums fro
10489 6d 0a 20 20 2a 2a 20 65 61 63 68 20 72 65 63 6f  m.  ** each reco
1048a 72 64 20 73 69 6e 63 65 20 70 6f 77 65 72 2d 66  rd since power-f
1048b 61 69 6c 75 72 65 20 72 65 63 6f 76 65 72 79 20  ailure recovery 
1048c 69 73 20 6e 6f 74 20 69 6d 70 6f 72 74 61 6e 74  is not important
1048d 20 74 6f 20 73 74 61 74 65 6d 65 6e 74 0a 20 20   to statement.  
1048e 2a 2a 20 6a 6f 75 72 6e 61 6c 73 2e 0a 20 20 2a  ** journals..  *
1048f 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  /.  for(i=0; i<n
10490 52 65 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  Rec; i++){.    i
10491 36 34 20 6f 66 66 73 65 74 20 3d 20 69 2a 28 34  64 offset = i*(4
10492 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a  +pPager->pageSiz
10493 65 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67  e);.    rc = pag
10494 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f  er_playback_one_
10495 70 61 67 65 28 70 50 61 67 65 72 2c 20 70 50 61  page(pPager, pPa
10496 67 65 72 2d 3e 73 74 66 64 2c 20 6f 66 66 73 65  ger->stfd, offse
10497 74 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65 72  t, 0);.    asser
10498 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f  t( rc!=SQLITE_DO
10499 4e 45 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63  NE );.    if( rc
1049a 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
1049b 74 6f 20 65 6e 64 5f 73 74 6d 74 5f 70 6c 61 79  to end_stmt_play
1049c 62 61 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  back;.  }..  /* 
1049d 4e 6f 77 20 72 6f 6c 6c 20 73 6f 6d 65 20 70 61  Now roll some pa
1049e 67 65 73 20 62 61 63 6b 20 66 72 6f 6d 20 74 68  ges back from th
1049f 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f  e transaction jo
104a0 75 72 6e 61 6c 2e 20 50 61 67 65 72 2e 73 74 6d  urnal. Pager.stm
104a1 74 4a 53 69 7a 65 0a 20 20 2a 2a 20 77 61 73 20  tJSize.  ** was 
104a2 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
104a3 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 68 65  journal file whe
104a4 6e 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74  n this statement
104a5 20 77 61 73 20 73 74 61 72 74 65 64 2c 20 73 6f   was started, so
104a6 0a 20 20 2a 2a 20 65 76 65 72 79 74 68 69 6e 67  .  ** everything
104a7 20 61 66 74 65 72 20 74 68 61 74 20 6e 65 65 64   after that need
104a8 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62  s to be rolled b
104a9 61 63 6b 2c 20 65 69 74 68 65 72 20 69 6e 74 6f  ack, either into
104aa 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61   the.  ** databa
104ab 73 65 2c 20 74 68 65 20 6d 65 6d 6f 72 79 20 63  se, the memory c
104ac 61 63 68 65 2c 20 6f 72 20 62 6f 74 68 2e 0a 20  ache, or both.. 
104ad 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 69 74 20 69   **.  ** If it i
104ae 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e  s not zero, then
104af 20 50 61 67 65 72 2e 73 74 6d 74 48 64 72 4f 66   Pager.stmtHdrOf
104b0 66 20 69 73 20 74 68 65 20 6f 66 66 73 65 74 20  f is the offset 
104b1 74 6f 20 74 68 65 20 73 74 61 72 74 0a 20 20 2a  to the start.  *
104b2 2a 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 6a  * of the first j
104b3 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 77 72  ournal header wr
104b4 69 74 74 65 6e 20 64 75 72 69 6e 67 20 74 68 69  itten during thi
104b5 73 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  s statement tran
104b6 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20  saction..  */.  
104b7 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
104b8 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 73 74 6d  ff = pPager->stm
104b9 74 4a 53 69 7a 65 3b 0a 20 20 70 50 61 67 65 72  tJSize;.  pPager
104ba 2d 3e 63 6b 73 75 6d 49 6e 69 74 20 3d 20 70 50  ->cksumInit = pP
104bb 61 67 65 72 2d 3e 73 74 6d 74 43 6b 73 75 6d 3b  ager->stmtCksum;
104bc 0a 20 20 77 68 69 6c 65 28 20 70 50 61 67 65 72  .  while( pPager
104bd 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3c 20 68  ->journalOff < h
104be 64 72 4f 66 66 20 29 7b 0a 20 20 20 20 72 63 20  drOff ){.    rc 
104bf 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b  = pager_playback
104c0 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72  _one_page(pPager
104c1 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70  , pPager->jfd, p
104c2 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
104c3 66 2c 20 31 29 3b 0a 20 20 20 20 61 73 73 65 72  f, 1);.    asser
104c4 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f  t( rc!=SQLITE_DO
104c5 4e 45 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63  NE );.    if( rc
104c6 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
104c7 74 6f 20 65 6e 64 5f 73 74 6d 74 5f 70 6c 61 79  to end_stmt_play
104c8 62 61 63 6b 3b 0a 20 20 7d 0a 0a 20 20 77 68 69  back;.  }..  whi
104c9 6c 65 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  le( pPager->jour
104ca 6e 61 6c 4f 66 66 20 3c 20 73 7a 4a 20 29 7b 0a  nalOff < szJ ){.
104cb 20 20 20 20 75 33 32 20 6e 4a 52 65 63 3b 20 20      u32 nJRec;  
104cc 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
104cd 20 6f 66 20 4a 6f 75 72 6e 61 6c 20 52 65 63 6f   of Journal Reco
104ce 72 64 73 20 2a 2f 0a 20 20 20 20 75 33 32 20 64  rds */.    u32 d
104cf 75 6d 6d 79 3b 0a 20 20 20 20 72 63 20 3d 20 72  ummy;.    rc = r
104d0 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50  eadJournalHdr(pP
104d1 61 67 65 72 2c 20 73 7a 4a 2c 20 26 6e 4a 52 65  ager, szJ, &nJRe
104d2 63 2c 20 26 64 75 6d 6d 79 29 3b 0a 20 20 20 20  c, &dummy);.    
104d3 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
104d4 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  K ){.      asser
104d5 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f  t( rc!=SQLITE_DO
104d6 4e 45 20 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f  NE );.      goto
104d7 20 65 6e 64 5f 73 74 6d 74 5f 70 6c 61 79 62 61   end_stmt_playba
104d8 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ck;.    }.    if
104d9 28 20 6e 4a 52 65 63 3d 3d 30 20 29 7b 0a 20 20  ( nJRec==0 ){.  
104da 20 20 20 20 6e 4a 52 65 63 20 3d 20 28 73 7a 4a      nJRec = (szJ
104db 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e   - pPager->journ
104dc 61 6c 4f 66 66 29 20 2f 20 28 70 50 61 67 65 72  alOff) / (pPager
104dd 2d 3e 70 61 67 65 53 69 7a 65 2b 38 29 3b 0a 20  ->pageSize+8);. 
104de 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 6e     }.    for(i=n
104df 4a 52 65 63 2d 31 3b 20 69 3e 3d 30 20 26 26 20  JRec-1; i>=0 && 
104e0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
104e1 66 66 20 3c 20 73 7a 4a 3b 20 69 2d 2d 29 7b 0a  ff < szJ; i--){.
104e2 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
104e3 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61  _playback_one_pa
104e4 67 65 28 70 50 61 67 65 72 2c 20 70 50 61 67 65  ge(pPager, pPage
104e5 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e  r->jfd, pPager->
104e6 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 29 3b 0a  journalOff, 1);.
104e7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63        assert( rc
104e8 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b  !=SQLITE_DONE );
104e9 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
104ea 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
104eb 65 6e 64 5f 73 74 6d 74 5f 70 6c 61 79 62 61 63  end_stmt_playbac
104ec 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  k;.    }.  }..  
104ed 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
104ee 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 0a 65 6e 64  ff = szJ;.  .end
104ef 5f 73 74 6d 74 5f 70 6c 61 79 62 61 63 6b 3a 0a  _stmt_playback:.
104f0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
104f1 5f 4f 4b 29 20 7b 0a 20 20 20 20 70 50 61 67 65  _OK) {.    pPage
104f2 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20  r->journalOff = 
104f3 73 7a 4a 3b 0a 20 20 20 20 2f 2a 20 70 61 67 65  szJ;.    /* page
104f4 72 5f 72 65 6c 6f 61 64 5f 63 61 63 68 65 28 70  r_reload_cache(p
104f5 50 61 67 65 72 29 3b 20 2a 2f 0a 20 20 7d 0a 20  Pager); */.  }. 
104f6 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
104f7 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
104f8 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  maximum number o
104f9 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65  f in-memory page
104fa 73 20 74 68 61 74 20 61 72 65 20 61 6c 6c 6f 77  s that are allow
104fb 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
104fc 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
104fd 65 33 50 61 67 65 72 53 65 74 43 61 63 68 65 73  e3PagerSetCaches
104fe 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  ize(Pager *pPage
104ff 72 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a  r, int mxPage){.
10500 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53    sqlite3PcacheS
10501 65 74 43 61 63 68 65 73 69 7a 65 28 70 50 61 67  etCachesize(pPag
10502 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 6d 78 50  er->pPCache, mxP
10503 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  age);.}../*.** A
10504 64 6a 75 73 74 20 74 68 65 20 72 6f 62 75 73 74  djust the robust
10505 6e 65 73 73 20 6f 66 20 74 68 65 20 64 61 74 61  ness of the data
10506 62 61 73 65 20 74 6f 20 64 61 6d 61 67 65 20 64  base to damage d
10507 75 65 20 74 6f 20 4f 53 20 63 72 61 73 68 65 73  ue to OS crashes
10508 0a 2a 2a 20 6f 72 20 70 6f 77 65 72 20 66 61 69  .** or power fai
10509 6c 75 72 65 73 20 62 79 20 63 68 61 6e 67 69 6e  lures by changin
1050a 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  g the number of 
1050b 73 79 6e 63 73 28 29 73 20 77 68 65 6e 20 77 72  syncs()s when wr
1050c 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 72 6f 6c  iting.** the rol
1050d 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20  lback journal.  
1050e 54 68 65 72 65 20 61 72 65 20 74 68 72 65 65 20  There are three 
1050f 6c 65 76 65 6c 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  levels:.**.**   
10510 20 4f 46 46 20 20 20 20 20 20 20 73 71 6c 69 74   OFF       sqlit
10511 65 33 4f 73 53 79 6e 63 28 29 20 69 73 20 6e 65  e3OsSync() is ne
10512 76 65 72 20 63 61 6c 6c 65 64 2e 20 20 54 68 69  ver called.  Thi
10513 73 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74  s is the default
10514 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
10515 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 61   for temporary a
10516 6e 64 20 74 72 61 6e 73 69 65 6e 74 20 66 69 6c  nd transient fil
10517 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 4e 4f 52  es..**.**    NOR
10518 4d 41 4c 20 20 20 20 54 68 65 20 6a 6f 75 72 6e  MAL    The journ
10519 61 6c 20 69 73 20 73 79 6e 63 65 64 20 6f 6e 63  al is synced onc
1051a 65 20 62 65 66 6f 72 65 20 77 72 69 74 65 73 20  e before writes 
1051b 62 65 67 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20  begin on the.** 
1051c 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74               dat
1051d 61 62 61 73 65 2e 20 20 54 68 69 73 20 69 73 20  abase.  This is 
1051e 6e 6f 72 6d 61 6c 6c 79 20 61 64 65 71 75 61 74  normally adequat
1051f 65 20 70 72 6f 74 65 63 74 69 6f 6e 2c 20 62 75  e protection, bu
10520 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
10521 20 20 69 74 20 69 73 20 74 68 65 6f 72 65 74 69    it is theoreti
10522 63 61 6c 6c 79 20 70 6f 73 73 69 62 6c 65 2c 20  cally possible, 
10523 74 68 6f 75 67 68 20 76 65 72 79 20 75 6e 6c 69  though very unli
10524 6b 65 6c 79 2c 0a 2a 2a 20 20 20 20 20 20 20 20  kely,.**        
10525 20 20 20 20 20 20 74 68 61 74 20 61 6e 20 69 6e        that an in
10526 6f 70 65 72 74 75 6e 65 20 70 6f 77 65 72 20 66  opertune power f
10527 61 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c 65 61  ailure could lea
10528 76 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a  ve the journal.*
10529 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69  *              i
1052a 6e 20 61 20 73 74 61 74 65 20 77 68 69 63 68 20  n a state which 
1052b 77 6f 75 6c 64 20 63 61 75 73 65 20 64 61 6d 61  would cause dama
1052c 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ge to the databa
1052d 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  se.**           
1052e 20 20 20 77 68 65 6e 20 69 74 20 69 73 20 72 6f     when it is ro
1052f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a  lled back..**.**
10530 20 20 20 20 46 55 4c 4c 20 20 20 20 20 20 54 68      FULL      Th
10531 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e  e journal is syn
10532 63 65 64 20 74 77 69 63 65 20 62 65 66 6f 72 65  ced twice before
10533 20 77 72 69 74 65 73 20 62 65 67 69 6e 20 6f 6e   writes begin on
10534 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
10535 20 20 20 20 20 64 61 74 61 62 61 73 65 20 28 77       database (w
10536 69 74 68 20 73 6f 6d 65 20 61 64 64 69 74 69 6f  ith some additio
10537 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  nal information 
10538 2d 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64  - the nRec field
10539 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1053a 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
1053b 68 65 61 64 65 72 20 2d 20 62 65 69 6e 67 20 77  header - being w
1053c 72 69 74 74 65 6e 20 69 6e 20 62 65 74 77 65 65  ritten in betwee
1053d 6e 20 74 68 65 20 74 77 6f 0a 2a 2a 20 20 20 20  n the two.**    
1053e 20 20 20 20 20 20 20 20 20 20 73 79 6e 63 73 29            syncs)
1053f 2e 20 20 49 66 20 77 65 20 61 73 73 75 6d 65 20  .  If we assume 
10540 74 68 61 74 20 77 72 69 74 69 6e 67 20 61 0a 2a  that writing a.*
10541 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  *              s
10542 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 74 6f  ingle disk secto
10543 72 20 69 73 20 61 74 6f 6d 69 63 2c 20 74 68 65  r is atomic, the
10544 6e 20 74 68 69 73 20 6d 6f 64 65 20 70 72 6f 76  n this mode prov
10545 69 64 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  ides.**         
10546 20 20 20 20 20 61 73 73 75 72 61 6e 63 65 20 74       assurance t
10547 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  hat the journal 
10548 77 69 6c 6c 20 6e 6f 74 20 62 65 20 63 6f 72 72  will not be corr
10549 75 70 74 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20  upted to the.** 
1054a 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6f 69               poi
1054b 6e 74 20 6f 66 20 63 61 75 73 69 6e 67 20 64 61  nt of causing da
1054c 6d 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61  mage to the data
1054d 62 61 73 65 20 64 75 72 69 6e 67 20 72 6f 6c 6c  base during roll
1054e 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 4e 75 6d 65  back..**.** Nume
1054f 72 69 63 20 76 61 6c 75 65 73 20 61 73 73 6f 63  ric values assoc
10550 69 61 74 65 64 20 77 69 74 68 20 74 68 65 73 65  iated with these
10551 20 73 74 61 74 65 73 20 61 72 65 20 4f 46 46 3d   states are OFF=
10552 3d 31 2c 20 4e 4f 52 4d 41 4c 3d 32 2c 0a 2a 2a  =1, NORMAL=2,.**
10553 20 61 6e 64 20 46 55 4c 4c 3d 33 2e 0a 2a 2f 0a   and FULL=3..*/.
10554 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
10555 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  MIT_PAGER_PRAGMA
10556 53 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  S.SQLITE_PRIVATE
10557 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67   void sqlite3Pag
10558 65 72 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c  erSetSafetyLevel
10559 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
1055a 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e 74 20 62  int level, int b
1055b 46 75 6c 6c 46 73 79 6e 63 29 7b 0a 20 20 70 50  FullFsync){.  pP
1055c 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d 20 20  ager->noSync =  
1055d 6c 65 76 65 6c 3d 3d 31 20 7c 7c 20 70 50 61 67  level==1 || pPag
1055e 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20  er->tempFile;.  
1055f 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63  pPager->fullSync
10560 20 3d 20 6c 65 76 65 6c 3d 3d 33 20 26 26 20 21   = level==3 && !
10561 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65  pPager->tempFile
10562 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 79 6e 63  ;.  pPager->sync
10563 5f 66 6c 61 67 73 20 3d 20 28 62 46 75 6c 6c 46  _flags = (bFullF
10564 73 79 6e 63 3f 53 51 4c 49 54 45 5f 53 59 4e 43  sync?SQLITE_SYNC
10565 5f 46 55 4c 4c 3a 53 51 4c 49 54 45 5f 53 59 4e  _FULL:SQLITE_SYN
10566 43 5f 4e 4f 52 4d 41 4c 29 3b 0a 20 20 69 66 28  C_NORMAL);.  if(
10567 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20   pPager->noSync 
10568 29 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79  ) pPager->needSy
10569 6e 63 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69 66  nc = 0;.}.#endif
1056a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
1056b 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72  owing global var
1056c 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65  iable is increme
1056d 6e 74 65 64 20 77 68 65 6e 65 76 65 72 20 74 68  nted whenever th
1056e 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 61 74 74  e library.** att
1056f 65 6d 70 74 73 20 74 6f 20 6f 70 65 6e 20 61 20  empts to open a 
10570 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20  temporary file. 
10571 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f   This informatio
10572 6e 20 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a  n is used for.**
10573 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 6e 61   testing and ana
10574 6c 79 73 69 73 20 6f 6e 6c 79 2e 20 20 0a 2a 2f  lysis only.  .*/
10575 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
10576 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  EST.SQLITE_API i
10577 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74  nt sqlite3_opent
10578 65 6d 70 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23  emp_count = 0;.#
10579 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65  endif../*.** Ope
1057a 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69  n a temporary fi
1057b 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65  le. .**.** Write
1057c 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
1057d 70 74 6f 72 20 69 6e 74 6f 20 2a 66 64 2e 20 20  ptor into *fd.  
1057e 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
1057f 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 73   on success or s
10580 6f 6d 65 0a 2a 2a 20 6f 74 68 65 72 20 65 72 72  ome.** other err
10581 6f 72 20 63 6f 64 65 20 69 66 20 77 65 20 66 61  or code if we fa
10582 69 6c 2e 20 54 68 65 20 4f 53 20 77 69 6c 6c 20  il. The OS will 
10583 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65  automatically de
10584 6c 65 74 65 20 74 68 65 20 74 65 6d 70 6f 72 61  lete the tempora
10585 72 79 0a 2a 2a 20 66 69 6c 65 20 77 68 65 6e 20  ry.** file when 
10586 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f  it is closed..*/
10587 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
10588 74 65 33 50 61 67 65 72 4f 70 65 6e 74 65 6d 70  te3PagerOpentemp
10589 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65  (.  Pager *pPage
1058a 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  r,        /* The
1058b 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f   pager object */
1058c 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  .  sqlite3_file 
1058d 2a 70 46 69 6c 65 2c 20 20 2f 2a 20 57 72 69 74  *pFile,  /* Writ
1058e 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72  e the file descr
1058f 69 70 74 6f 72 20 68 65 72 65 20 2a 2f 0a 20 20  iptor here */.  
10590 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20 20  int vfsFlags    
10591 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70        /* Flags p
10592 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f  assed through to
10593 20 74 68 65 20 56 46 53 20 2a 2f 0a 29 7b 0a 20   the VFS */.){. 
10594 20 69 6e 74 20 72 63 3b 0a 0a 23 69 66 64 65 66   int rc;..#ifdef
10595 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73   SQLITE_TEST.  s
10596 71 6c 69 74 65 33 5f 6f 70 65 6e 74 65 6d 70 5f  qlite3_opentemp_
10597 63 6f 75 6e 74 2b 2b 3b 20 20 2f 2a 20 55 73 65  count++;  /* Use
10598 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e  d for testing an
10599 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 20  d analysis only 
1059a 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 76 66 73  */.#endif..  vfs
1059b 46 6c 61 67 73 20 7c 3d 20 20 53 51 4c 49 54 45  Flags |=  SQLITE
1059c 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20  _OPEN_READWRITE 
1059d 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52  | SQLITE_OPEN_CR
1059e 45 41 54 45 20 7c 0a 20 20 20 20 20 20 20 20 20  EATE |.         
1059f 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45     SQLITE_OPEN_E
105a0 58 43 4c 55 53 49 56 45 20 7c 20 53 51 4c 49 54  XCLUSIVE | SQLIT
105a1 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43  E_OPEN_DELETEONC
105a2 4c 4f 53 45 3b 0a 20 20 72 63 20 3d 20 73 71 6c  LOSE;.  rc = sql
105a3 69 74 65 33 4f 73 4f 70 65 6e 28 70 50 61 67 65  ite3OsOpen(pPage
105a4 72 2d 3e 70 56 66 73 2c 20 30 2c 20 70 46 69 6c  r->pVfs, 0, pFil
105a5 65 2c 20 76 66 73 46 6c 61 67 73 2c 20 30 29 3b  e, vfsFlags, 0);
105a6 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53  .  assert( rc!=S
105a7 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 46 69 6c  QLITE_OK || pFil
105a8 65 2d 3e 70 4d 65 74 68 6f 64 73 20 29 3b 0a 20  e->pMethods );. 
105a9 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73   return rc;.}..s
105aa 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 53  tatic int pagerS
105ab 74 72 65 73 73 28 76 6f 69 64 20 2a 2c 50 67 48  tress(void *,PgH
105ac 64 72 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 72  dr *);../*.** Cr
105ad 65 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20  eate a new page 
105ae 63 61 63 68 65 20 61 6e 64 20 70 75 74 20 61 20  cache and put a 
105af 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70  pointer to the p
105b0 61 67 65 20 63 61 63 68 65 20 69 6e 20 2a 70 70  age cache in *pp
105b1 50 61 67 65 72 2e 0a 2a 2a 20 54 68 65 20 66 69  Pager..** The fi
105b2 6c 65 20 74 6f 20 62 65 20 63 61 63 68 65 64 20  le to be cached 
105b3 6e 65 65 64 20 6e 6f 74 20 65 78 69 73 74 2e 20  need not exist. 
105b4 20 54 68 65 20 66 69 6c 65 20 69 73 20 6e 6f 74   The file is not
105b5 20 6c 6f 63 6b 65 64 20 75 6e 74 69 6c 0a 2a 2a   locked until.**
105b6 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20   the first call 
105b7 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 47  to sqlite3PagerG
105b8 65 74 28 29 20 61 6e 64 20 69 73 20 6f 6e 6c 79  et() and is only
105b9 20 68 65 6c 64 20 6f 70 65 6e 20 75 6e 74 69 6c   held open until
105ba 20 74 68 65 0a 2a 2a 20 6c 61 73 74 20 70 61 67   the.** last pag
105bb 65 20 69 73 20 72 65 6c 65 61 73 65 64 20 75 73  e is released us
105bc 69 6e 67 20 73 71 6c 69 74 65 33 50 61 67 65 72  ing sqlite3Pager
105bd 55 6e 72 65 66 28 29 2e 0a 2a 2a 0a 2a 2a 20 49  Unref()..**.** I
105be 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e  f zFilename is N
105bf 55 4c 4c 20 74 68 65 6e 20 61 20 72 61 6e 64 6f  ULL then a rando
105c0 6d 6c 79 2d 6e 61 6d 65 64 20 74 65 6d 70 6f 72  mly-named tempor
105c1 61 72 79 20 66 69 6c 65 20 69 73 20 63 72 65 61  ary file is crea
105c2 74 65 64 0a 2a 2a 20 61 6e 64 20 75 73 65 64 20  ted.** and used 
105c3 61 73 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62  as the file to b
105c4 65 20 63 61 63 68 65 64 2e 20 20 54 68 65 20 66  e cached.  The f
105c5 69 6c 65 20 77 69 6c 6c 20 62 65 20 64 65 6c 65  ile will be dele
105c6 74 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63  ted.** automatic
105c7 61 6c 6c 79 20 77 68 65 6e 20 69 74 20 69 73 20  ally when it is 
105c8 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  closed..**.** If
105c9 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a   zFilename is ":
105ca 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e 20 61 6c  memory:" then al
105cb 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73  l information is
105cc 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 2e 0a   held in cache..
105cd 2a 2a 20 49 74 20 69 73 20 6e 65 76 65 72 20 77  ** It is never w
105ce 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e 20  ritten to disk. 
105cf 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65   This can be use
105d0 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61  d to implement a
105d1 6e 0a 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79 20 64  n.** in-memory d
105d2 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49  atabase..*/.SQLI
105d3 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
105d4 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28  qlite3PagerOpen(
105d5 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
105d6 70 56 66 73 2c 20 20 20 20 20 20 20 2f 2a 20 54  pVfs,       /* T
105d7 68 65 20 76 69 72 74 75 61 6c 20 66 69 6c 65 20  he virtual file 
105d8 73 79 73 74 65 6d 20 74 6f 20 75 73 65 20 2a 2f  system to use */
105d9 0a 20 20 50 61 67 65 72 20 2a 2a 70 70 50 61 67  .  Pager **ppPag
105da 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52  er,         /* R
105db 65 74 75 72 6e 20 74 68 65 20 50 61 67 65 72 20  eturn the Pager 
105dc 73 74 72 75 63 74 75 72 65 20 68 65 72 65 20 2a  structure here *
105dd 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
105de 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20  zFilename,   /* 
105df 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  Name of the data
105e0 62 61 73 65 20 66 69 6c 65 20 74 6f 20 6f 70 65  base file to ope
105e1 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72  n */.  int nExtr
105e2 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  a,              
105e3 2f 2a 20 45 78 74 72 61 20 62 79 74 65 73 20 61  /* Extra bytes a
105e4 70 70 65 6e 64 20 74 6f 20 65 61 63 68 20 69 6e  ppend to each in
105e5 2d 6d 65 6d 6f 72 79 20 70 61 67 65 20 2a 2f 0a  -memory page */.
105e6 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20    int flags,    
105e7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c             /* fl
105e8 61 67 73 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20  ags controlling 
105e9 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69  this file */.  i
105ea 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20 20 20  nt vfsFlags     
105eb 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73          /* flags
105ec 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 20   passed through 
105ed 74 6f 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 78  to sqlite3_vfs.x
105ee 4f 70 65 6e 28 29 20 2a 2f 0a 29 7b 0a 20 20 75  Open() */.){.  u
105ef 38 20 2a 70 50 74 72 3b 0a 20 20 50 61 67 65 72  8 *pPtr;.  Pager
105f0 20 2a 70 50 61 67 65 72 20 3d 20 30 3b 0a 20 20   *pPager = 0;.  
105f1 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
105f2 4f 4b 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  OK;.  int i;.  i
105f3 6e 74 20 74 65 6d 70 46 69 6c 65 20 3d 20 30 3b  nt tempFile = 0;
105f4 0a 20 20 69 6e 74 20 6d 65 6d 44 62 20 3d 20 30  .  int memDb = 0
105f5 3b 0a 20 20 69 6e 74 20 72 65 61 64 4f 6e 6c 79  ;.  int readOnly
105f6 20 3d 20 30 3b 0a 20 20 69 6e 74 20 75 73 65 4a   = 0;.  int useJ
105f7 6f 75 72 6e 61 6c 20 3d 20 28 66 6c 61 67 73 20  ournal = (flags 
105f8 26 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55  & PAGER_OMIT_JOU
105f9 52 4e 41 4c 29 3d 3d 30 3b 0a 20 20 69 6e 74 20  RNAL)==0;.  int 
105fa 6e 6f 52 65 61 64 6c 6f 63 6b 20 3d 20 28 66 6c  noReadlock = (fl
105fb 61 67 73 20 26 20 50 41 47 45 52 5f 4e 4f 5f 52  ags & PAGER_NO_R
105fc 45 41 44 4c 4f 43 4b 29 21 3d 30 3b 0a 20 20 69  EADLOCK)!=0;.  i
105fd 6e 74 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69  nt journalFileSi
105fe 7a 65 3b 0a 20 20 69 6e 74 20 70 63 61 63 68 65  ze;.  int pcache
105ff 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 50 63  Size = sqlite3Pc
10600 61 63 68 65 53 69 7a 65 28 29 3b 0a 20 20 69 6e  acheSize();.  in
10601 74 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 53  t szPageDflt = S
10602 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41  QLITE_DEFAULT_PA
10603 47 45 5f 53 49 5a 45 3b 0a 20 20 63 68 61 72 20  GE_SIZE;.  char 
10604 2a 7a 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b 0a  *zPathname = 0;.
10605 20 20 69 6e 74 20 6e 50 61 74 68 6e 61 6d 65 20    int nPathname 
10606 3d 20 30 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69  = 0;..  if( sqli
10607 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 70  te3JournalSize(p
10608 56 66 73 29 3e 73 71 6c 69 74 65 33 4d 65 6d 4a  Vfs)>sqlite3MemJ
10609 6f 75 72 6e 61 6c 53 69 7a 65 28 29 20 29 7b 0a  ournalSize() ){.
1060a 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53      journalFileS
1060b 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4a 6f 75  ize = sqlite3Jou
1060c 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 29 3b 0a  rnalSize(pVfs);.
1060d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6a 6f 75    }else{.    jou
1060e 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 3d 20 73  rnalFileSize = s
1060f 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c  qlite3MemJournal
10610 53 69 7a 65 28 29 3b 0a 20 20 7d 0a 0a 20 20 2f  Size();.  }..  /
10611 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 72 65  * The default re
10612 74 75 72 6e 20 69 73 20 61 20 4e 55 4c 4c 20 70  turn is a NULL p
10613 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 2a 70 70 50  ointer */.  *ppP
10614 61 67 65 72 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20  ager = 0;..  /* 
10615 43 6f 6d 70 75 74 65 20 61 6e 64 20 73 74 6f 72  Compute and stor
10616 65 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 6e  e the full pathn
10617 61 6d 65 20 69 6e 20 61 6e 20 61 6c 6c 6f 63 61  ame in an alloca
10618 74 65 64 20 62 75 66 66 65 72 20 70 6f 69 6e 74  ted buffer point
10619 65 64 0a 20 20 2a 2a 20 74 6f 20 62 79 20 7a 50  ed.  ** to by zP
1061a 61 74 68 6e 61 6d 65 2c 20 6c 65 6e 67 74 68 20  athname, length 
1061b 6e 50 61 74 68 6e 61 6d 65 2e 20 4f 72 2c 20 69  nPathname. Or, i
1061c 66 20 74 68 69 73 20 69 73 20 61 20 74 65 6d 70  f this is a temp
1061d 6f 72 61 72 79 20 66 69 6c 65 2c 0a 20 20 2a 2a  orary file,.  **
1061e 20 6c 65 61 76 65 20 62 6f 74 68 20 6e 50 61 74   leave both nPat
1061f 68 6e 61 6d 65 20 61 6e 64 20 7a 50 61 74 68 6e  hname and zPathn
10620 61 6d 65 20 73 65 74 20 74 6f 20 30 2e 0a 20 20  ame set to 0..  
10621 2a 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61  */.  if( zFilena
10622 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 5b  me && zFilename[
10623 30 5d 20 29 7b 0a 20 20 20 20 6e 50 61 74 68 6e  0] ){.    nPathn
10624 61 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61  ame = pVfs->mxPa
10625 74 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 7a 50  thname+1;.    zP
10626 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65  athname = sqlite
10627 33 4d 61 6c 6c 6f 63 28 6e 50 61 74 68 6e 61 6d  3Malloc(nPathnam
10628 65 2a 32 29 3b 0a 20 20 20 20 69 66 28 20 7a 50  e*2);.    if( zP
10629 61 74 68 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20  athname==0 ){.  
1062a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1062b 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 23  E_NOMEM;.    }.#
1062c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1062d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 20 20  IT_MEMORYDB.    
1062e 69 66 28 20 73 74 72 63 6d 70 28 7a 46 69 6c 65  if( strcmp(zFile
1062f 6e 61 6d 65 2c 22 3a 6d 65 6d 6f 72 79 3a 22 29  name,":memory:")
10630 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  ==0 ){.      mem
10631 44 62 20 3d 20 31 3b 0a 20 20 20 20 20 20 7a 50  Db = 1;.      zP
10632 61 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b 0a  athname[0] = 0;.
10633 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66      }else.#endif
10634 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 72 63 20  .    {.      rc 
10635 3d 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50  = sqlite3OsFullP
10636 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 46  athname(pVfs, zF
10637 69 6c 65 6e 61 6d 65 2c 20 6e 50 61 74 68 6e 61  ilename, nPathna
10638 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65 29 3b 0a  me, zPathname);.
10639 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
1063a 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1063b 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
1063c 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20  e(zPathname);.  
1063d 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1063e 20 20 20 7d 0a 20 20 20 20 6e 50 61 74 68 6e 61     }.    nPathna
1063f 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a 50 61 74  me = strlen(zPat
10640 68 6e 61 6d 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f  hname);.  }..  /
10641 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72  * Allocate memor
10642 79 20 66 6f 72 20 74 68 65 20 70 61 67 65 72 20  y for the pager 
10643 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 70  structure */.  p
10644 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 4d  Pager = sqlite3M
10645 61 6c 6c 6f 63 5a 65 72 6f 28 0a 20 20 20 20 73  allocZero(.    s
10646 69 7a 65 6f 66 28 2a 70 50 61 67 65 72 29 20 2b  izeof(*pPager) +
10647 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
10648 67 65 72 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ger structure */
10649 0a 20 20 20 20 70 63 61 63 68 65 53 69 7a 65 20  .    pcacheSize 
1064a 20 20 20 20 20 2b 20 20 20 20 20 20 20 20 20 20       +          
1064b 20 2f 2a 20 50 43 61 63 68 65 20 6f 62 6a 65 63   /* PCache objec
1064c 74 20 2a 2f 0a 20 20 20 20 6a 6f 75 72 6e 61 6c  t */.    journal
1064d 46 69 6c 65 53 69 7a 65 20 2b 20 20 20 20 20 20  FileSize +      
1064e 20 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72       /* The jour
1064f 6e 61 6c 20 66 69 6c 65 20 73 74 72 75 63 74 75  nal file structu
10650 72 65 20 2a 2f 20 0a 20 20 20 20 70 56 66 73 2d  re */ .    pVfs-
10651 3e 73 7a 4f 73 46 69 6c 65 20 20 2b 20 20 20 20  >szOsFile  +    
10652 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61         /* The ma
10653 69 6e 20 64 62 20 66 69 6c 65 20 2a 2f 0a 20 20  in db file */.  
10654 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a    journalFileSiz
10655 65 20 2a 20 32 20 2b 20 20 20 20 20 20 20 2f 2a  e * 2 +       /*
10656 20 54 68 65 20 74 77 6f 20 6a 6f 75 72 6e 61 6c   The two journal
10657 20 66 69 6c 65 73 20 2a 2f 20 0a 20 20 20 20 33   files */ .    3
10658 2a 6e 50 61 74 68 6e 61 6d 65 20 2b 20 34 30 20  *nPathname + 40 
10659 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 46             /* zF
1065a 69 6c 65 6e 61 6d 65 2c 20 7a 44 69 72 65 63 74  ilename, zDirect
1065b 6f 72 79 2c 20 7a 4a 6f 75 72 6e 61 6c 20 2a 2f  ory, zJournal */
1065c 0a 20 20 29 3b 0a 20 20 69 66 28 20 21 70 50 61  .  );.  if( !pPa
1065d 67 65 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ger ){.    sqlit
1065e 65 33 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d  e3_free(zPathnam
1065f 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  e);.    return S
10660 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
10661 0a 20 20 70 50 61 67 65 72 2d 3e 70 50 43 61 63  .  pPager->pPCac
10662 68 65 20 3d 20 28 50 43 61 63 68 65 20 2a 29 26  he = (PCache *)&
10663 70 50 61 67 65 72 5b 31 5d 3b 0a 20 20 70 50 74  pPager[1];.  pPt
10664 72 20 3d 20 28 28 75 38 20 2a 29 26 70 50 61 67  r = ((u8 *)&pPag
10665 65 72 5b 31 5d 29 20 2b 20 70 63 61 63 68 65 53  er[1]) + pcacheS
10666 69 7a 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e 76  ize;.  pPager->v
10667 66 73 46 6c 61 67 73 20 3d 20 76 66 73 46 6c 61  fsFlags = vfsFla
10668 67 73 3b 0a 20 20 70 50 61 67 65 72 2d 3e 66 64  gs;.  pPager->fd
10669 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65   = (sqlite3_file
1066a 2a 29 26 70 50 74 72 5b 70 56 66 73 2d 3e 73 7a  *)&pPtr[pVfs->sz
1066b 4f 73 46 69 6c 65 2a 30 5d 3b 0a 20 20 70 50 61  OsFile*0];.  pPa
1066c 67 65 72 2d 3e 73 74 66 64 20 3d 20 28 73 71 6c  ger->stfd = (sql
1066d 69 74 65 33 5f 66 69 6c 65 2a 29 26 70 50 74 72  ite3_file*)&pPtr
1066e 5b 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 5d  [pVfs->szOsFile]
1066f 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 66 64 20  ;.  pPager->jfd 
10670 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  = (sqlite3_file*
10671 29 26 70 50 74 72 5b 70 56 66 73 2d 3e 73 7a 4f  )&pPtr[pVfs->szO
10672 73 46 69 6c 65 2b 6a 6f 75 72 6e 61 6c 46 69 6c  sFile+journalFil
10673 65 53 69 7a 65 5d 3b 0a 20 20 70 50 61 67 65 72  eSize];.  pPager
10674 2d 3e 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 28 63  ->zFilename = (c
10675 68 61 72 2a 29 26 70 50 74 72 5b 70 56 66 73 2d  har*)&pPtr[pVfs-
10676 3e 73 7a 4f 73 46 69 6c 65 2b 32 2a 6a 6f 75 72  >szOsFile+2*jour
10677 6e 61 6c 46 69 6c 65 53 69 7a 65 5d 3b 0a 20 20  nalFileSize];.  
10678 70 50 61 67 65 72 2d 3e 7a 44 69 72 65 63 74 6f  pPager->zDirecto
10679 72 79 20 3d 20 26 70 50 61 67 65 72 2d 3e 7a 46  ry = &pPager->zF
1067a 69 6c 65 6e 61 6d 65 5b 6e 50 61 74 68 6e 61 6d  ilename[nPathnam
1067b 65 2b 31 5d 3b 0a 20 20 70 50 61 67 65 72 2d 3e  e+1];.  pPager->
1067c 7a 4a 6f 75 72 6e 61 6c 20 3d 20 26 70 50 61 67  zJournal = &pPag
1067d 65 72 2d 3e 7a 44 69 72 65 63 74 6f 72 79 5b 6e  er->zDirectory[n
1067e 50 61 74 68 6e 61 6d 65 2b 31 5d 3b 0a 20 20 70  Pathname+1];.  p
1067f 50 61 67 65 72 2d 3e 70 56 66 73 20 3d 20 70 56  Pager->pVfs = pV
10680 66 73 3b 0a 20 20 69 66 28 20 7a 50 61 74 68 6e  fs;.  if( zPathn
10681 61 6d 65 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70  ame ){.    memcp
10682 79 28 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e  y(pPager->zFilen
10683 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65 2c 20  ame, zPathname, 
10684 6e 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20  nPathname+1);.  
10685 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
10686 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 7d 0a 0a  Pathname);.  }..
10687 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 70 61    /* Open the pa
10688 67 65 72 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20  ger file..  */. 
10689 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26   if( zFilename &
1068a 26 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 26  & zFilename[0] &
1068b 26 20 21 6d 65 6d 44 62 20 29 7b 0a 20 20 20 20  & !memDb ){.    
1068c 69 66 28 20 6e 50 61 74 68 6e 61 6d 65 3e 28 70  if( nPathname>(p
1068d 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20  Vfs->mxPathname 
1068e 2d 20 73 69 7a 65 6f 66 28 22 2d 6a 6f 75 72 6e  - sizeof("-journ
1068f 61 6c 22 29 29 20 29 7b 0a 20 20 20 20 20 20 72  al")) ){.      r
10690 63 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f  c = SQLITE_CANTO
10691 50 45 4e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  PEN;.    }else{.
10692 20 20 20 20 20 20 69 6e 74 20 66 6f 75 74 20 3d        int fout =
10693 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   0;.      rc = s
10694 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66  qlite3OsOpen(pVf
10695 73 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65  s, pPager->zFile
10696 6e 61 6d 65 2c 20 70 50 61 67 65 72 2d 3e 66 64  name, pPager->fd
10697 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
10698 20 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65             pPage
10699 72 2d 3e 76 66 73 46 6c 61 67 73 2c 20 26 66 6f  r->vfsFlags, &fo
1069a 75 74 29 3b 0a 20 20 20 20 20 20 72 65 61 64 4f  ut);.      readO
1069b 6e 6c 79 20 3d 20 28 66 6f 75 74 26 53 51 4c 49  nly = (fout&SQLI
1069c 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
1069d 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  );..      /* If 
1069e 74 68 65 20 66 69 6c 65 20 77 61 73 20 73 75 63  the file was suc
1069f 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64  cessfully opened
106a0 20 66 6f 72 20 72 65 61 64 2f 77 72 69 74 65 20   for read/write 
106a1 61 63 63 65 73 73 2c 0a 20 20 20 20 20 20 2a 2a  access,.      **
106a2 20 63 68 6f 6f 73 65 20 61 20 64 65 66 61 75 6c   choose a defaul
106a3 74 20 70 61 67 65 20 73 69 7a 65 20 69 6e 20 63  t page size in c
106a4 61 73 65 20 77 65 20 68 61 76 65 20 74 6f 20 63  ase we have to c
106a5 72 65 61 74 65 20 74 68 65 0a 20 20 20 20 20 20  reate the.      
106a6 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
106a7 2e 20 54 68 65 20 64 65 66 61 75 6c 74 20 70 61  . The default pa
106a8 67 65 20 73 69 7a 65 20 69 73 20 74 68 65 20 6d  ge size is the m
106a9 61 78 69 6d 75 6d 20 6f 66 3a 0a 20 20 20 20 20  aximum of:.     
106aa 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20   **.      **    
106ab 2b 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  + SQLITE_DEFAULT
106ac 5f 50 41 47 45 5f 53 49 5a 45 2c 0a 20 20 20 20  _PAGE_SIZE,.    
106ad 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 76 61    **    + The va
106ae 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20  lue returned by 
106af 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53  sqlite3OsSectorS
106b0 69 7a 65 28 29 0a 20 20 20 20 20 20 2a 2a 20 20  ize().      **  
106b1 20 20 2b 20 54 68 65 20 6c 61 72 67 65 73 74 20    + The largest 
106b2 70 61 67 65 20 73 69 7a 65 20 74 68 61 74 20 63  page size that c
106b3 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 61 74  an be written at
106b4 6f 6d 69 63 61 6c 6c 79 2e 0a 20 20 20 20 20 20  omically..      
106b5 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  */.      if( rc=
106b6 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 72  =SQLITE_OK && !r
106b7 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20  eadOnly ){.     
106b8 20 20 20 69 6e 74 20 69 53 65 63 74 6f 72 53 69     int iSectorSi
106b9 7a 65 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 65  ze = sqlite3OsSe
106ba 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 2d  ctorSize(pPager-
106bb 3e 66 64 29 3b 0a 20 20 20 20 20 20 20 20 69 66  >fd);.        if
106bc 28 20 73 7a 50 61 67 65 44 66 6c 74 3c 69 53 65  ( szPageDflt<iSe
106bd 63 74 6f 72 53 69 7a 65 20 29 7b 0a 20 20 20 20  ctorSize ){.    
106be 20 20 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74        szPageDflt
106bf 20 3d 20 69 53 65 63 74 6f 72 53 69 7a 65 3b 0a   = iSectorSize;.
106c0 20 20 20 20 20 20 20 20 7d 0a 23 69 66 64 65 66          }.#ifdef
106c1 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41   SQLITE_ENABLE_A
106c2 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 20  TOMIC_WRITE.    
106c3 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20 20      {.          
106c4 69 6e 74 20 69 44 63 20 3d 20 73 71 6c 69 74 65  int iDc = sqlite
106c5 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74  3OsDeviceCharact
106c6 65 72 69 73 74 69 63 73 28 70 50 61 67 65 72 2d  eristics(pPager-
106c7 3e 66 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20  >fd);.          
106c8 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 20 20  int ii;.        
106c9 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f    assert(SQLITE_
106ca 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 3d  IOCAP_ATOMIC512=
106cb 3d 28 35 31 32 3e 3e 38 29 29 3b 0a 20 20 20 20  =(512>>8));.    
106cc 20 20 20 20 20 20 61 73 73 65 72 74 28 53 51 4c        assert(SQL
106cd 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
106ce 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29  64K==(65536>>8))
106cf 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  ;.          asse
106d0 72 74 28 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45  rt(SQLITE_MAX_DE
106d1 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3c  FAULT_PAGE_SIZE<
106d2 3d 36 35 35 33 36 29 3b 0a 20 20 20 20 20 20 20  =65536);.       
106d3 20 20 20 66 6f 72 28 69 69 3d 73 7a 50 61 67 65     for(ii=szPage
106d4 44 66 6c 74 3b 20 69 69 3c 3d 53 51 4c 49 54 45  Dflt; ii<=SQLITE
106d5 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47  _MAX_DEFAULT_PAG
106d6 45 5f 53 49 5a 45 3b 20 69 69 3d 69 69 2a 32 29  E_SIZE; ii=ii*2)
106d7 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  {.            if
106d8 28 20 69 44 63 26 28 53 51 4c 49 54 45 5f 49 4f  ( iDc&(SQLITE_IO
106d9 43 41 50 5f 41 54 4f 4d 49 43 7c 28 69 69 3e 3e  CAP_ATOMIC|(ii>>
106da 38 29 29 20 29 20 73 7a 50 61 67 65 44 66 6c 74  8)) ) szPageDflt
106db 20 3d 20 69 69 3b 0a 20 20 20 20 20 20 20 20 20   = ii;.         
106dc 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e   }.        }.#en
106dd 64 69 66 0a 20 20 20 20 20 20 20 20 69 66 28 20  dif.        if( 
106de 73 7a 50 61 67 65 44 66 6c 74 3e 53 51 4c 49 54  szPageDflt>SQLIT
106df 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41  E_MAX_DEFAULT_PA
106e0 47 45 5f 53 49 5a 45 20 29 7b 0a 20 20 20 20 20  GE_SIZE ){.     
106e1 20 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74 20       szPageDflt 
106e2 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46  = SQLITE_MAX_DEF
106e3 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b 0a  AULT_PAGE_SIZE;.
106e4 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
106e5 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  }.    }.  }else{
106e6 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 74 65 6d  .    /* If a tem
106e7 70 6f 72 61 72 79 20 66 69 6c 65 20 69 73 20 72  porary file is r
106e8 65 71 75 65 73 74 65 64 2c 20 69 74 20 69 73 20  equested, it is 
106e9 6e 6f 74 20 6f 70 65 6e 65 64 20 69 6d 6d 65 64  not opened immed
106ea 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a 20 49  iately..    ** I
106eb 6e 20 74 68 69 73 20 63 61 73 65 20 77 65 20 61  n this case we a
106ec 63 63 65 70 74 20 74 68 65 20 64 65 66 61 75 6c  ccept the defaul
106ed 74 20 70 61 67 65 20 73 69 7a 65 20 61 6e 64 20  t page size and 
106ee 64 65 6c 61 79 20 61 63 74 75 61 6c 6c 79 0a 20  delay actually. 
106ef 20 20 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 74 68     ** opening th
106f0 65 20 66 69 6c 65 20 75 6e 74 69 6c 20 74 68 65  e file until the
106f1 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f 20 4f   first call to O
106f2 73 57 72 69 74 65 28 29 2e 0a 20 20 20 20 2a 2a  sWrite()..    **
106f3 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 62 72 61  .    ** This bra
106f4 6e 63 68 20 69 73 20 61 6c 73 6f 20 72 75 6e 20  nch is also run 
106f5 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  for an in-memory
106f6 20 64 61 74 61 62 61 73 65 2e 20 41 6e 20 69 6e   database. An in
106f7 2d 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a 20 64  -memory.    ** d
106f8 61 74 61 62 61 73 65 20 69 73 20 74 68 65 20 73  atabase is the s
106f9 61 6d 65 20 61 73 20 61 20 74 65 6d 70 2d 66 69  ame as a temp-fi
106fa 6c 65 20 74 68 61 74 20 69 73 20 6e 65 76 65 72  le that is never
106fb 20 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 0a   written out to.
106fc 20 20 20 20 2a 2a 20 64 69 73 6b 20 61 6e 64 20      ** disk and 
106fd 75 73 65 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  uses an in-memor
106fe 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  y rollback journ
106ff 61 6c 2e 0a 20 20 20 20 2a 2f 20 0a 20 20 20 20  al..    */ .    
10700 74 65 6d 70 46 69 6c 65 20 3d 20 31 3b 0a 20 20  tempFile = 1;.  
10701 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20    pPager->state 
10702 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  = PAGER_EXCLUSIV
10703 45 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50  E;.  }..  if( pP
10704 61 67 65 72 20 26 26 20 72 63 3d 3d 53 51 4c 49  ager && rc==SQLI
10705 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61  TE_OK ){.    pPa
10706 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d  ger->pTmpSpace =
10707 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c   sqlite3PageMall
10708 6f 63 28 73 7a 50 61 67 65 44 66 6c 74 29 3b 0a  oc(szPageDflt);.
10709 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20    }..  /* If an 
1070a 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20 69 6e  error occured in
1070b 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 62   either of the b
1070c 6c 6f 63 6b 73 20 61 62 6f 76 65 2e 0a 20 20 2a  locks above..  *
1070d 2a 20 46 72 65 65 20 74 68 65 20 50 61 67 65 72  * Free the Pager
1070e 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 63   structure and c
1070f 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 20  lose the file.. 
10710 20 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 70 61   ** Since the pa
10711 67 65 72 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 63  ger is not alloc
10712 61 74 65 64 20 74 68 65 72 65 20 69 73 20 6e 6f  ated there is no
10713 20 6e 65 65 64 20 74 6f 20 73 65 74 20 0a 20 20   need to set .  
10714 2a 2a 20 61 6e 79 20 50 61 67 65 72 2e 65 72 72  ** any Pager.err
10715 4d 61 73 6b 20 76 61 72 69 61 62 6c 65 73 2e 0a  Mask variables..
10716 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 50 61 67    */.  if( !pPag
10717 65 72 20 7c 7c 20 21 70 50 61 67 65 72 2d 3e 70  er || !pPager->p
10718 54 6d 70 53 70 61 63 65 20 29 7b 0a 20 20 20 20  TmpSpace ){.    
10719 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70  sqlite3OsClose(p
1071a 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20  Pager->fd);.    
1071b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61  sqlite3_free(pPa
1071c 67 65 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ger);.    return
1071d 20 28 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b   ((rc==SQLITE_OK
1071e 29 3f 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3a 72  )?SQLITE_NOMEM:r
1071f 63 29 3b 0a 20 20 7d 0a 20 20 6e 45 78 74 72 61  c);.  }.  nExtra
10720 20 3d 20 46 4f 52 43 45 5f 41 4c 49 47 4e 4d 45   = FORCE_ALIGNME
10721 4e 54 28 6e 45 78 74 72 61 29 3b 0a 20 20 73 71  NT(nExtra);.  sq
10722 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65 6e 28  lite3PcacheOpen(
10723 73 7a 50 61 67 65 44 66 6c 74 2c 20 6e 45 78 74  szPageDflt, nExt
10724 72 61 2c 20 21 6d 65 6d 44 62 2c 0a 20 20 20 20  ra, !memDb,.    
10725 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10726 21 6d 65 6d 44 62 3f 70 61 67 65 72 53 74 72 65  !memDb?pagerStre
10727 73 73 3a 30 2c 20 28 76 6f 69 64 20 2a 29 70 50  ss:0, (void *)pP
10728 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 50  ager, pPager->pP
10729 43 61 63 68 65 29 3b 0a 0a 20 20 50 41 47 45 52  Cache);..  PAGER
1072a 54 52 41 43 45 33 28 22 4f 50 45 4e 20 25 64 20  TRACE3("OPEN %d 
1072b 25 73 5c 6e 22 2c 20 46 49 4c 45 48 41 4e 44 4c  %s\n", FILEHANDL
1072c 45 49 44 28 70 50 61 67 65 72 2d 3e 66 64 29 2c  EID(pPager->fd),
1072d 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61   pPager->zFilena
1072e 6d 65 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28  me);.  IOTRACE((
1072f 22 4f 50 45 4e 20 25 70 20 25 73 5c 6e 22 2c 20  "OPEN %p %s\n", 
10730 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e  pPager, pPager->
10731 7a 46 69 6c 65 6e 61 6d 65 29 29 0a 0a 20 20 2f  zFilename))..  /
10732 2a 20 46 69 6c 6c 20 69 6e 20 50 61 67 65 72 2e  * Fill in Pager.
10733 7a 44 69 72 65 63 74 6f 72 79 5b 5d 20 2a 2f 0a  zDirectory[] */.
10734 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 72 2d    memcpy(pPager-
10735 3e 7a 44 69 72 65 63 74 6f 72 79 2c 20 70 50 61  >zDirectory, pPa
10736 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20  ger->zFilename, 
10737 6e 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20  nPathname+1);.  
10738 66 6f 72 28 69 3d 73 74 72 6c 65 6e 28 70 50 61  for(i=strlen(pPa
10739 67 65 72 2d 3e 7a 44 69 72 65 63 74 6f 72 79 29  ger->zDirectory)
1073a 3b 20 69 3e 30 20 26 26 20 70 50 61 67 65 72 2d  ; i>0 && pPager-
1073b 3e 7a 44 69 72 65 63 74 6f 72 79 5b 69 2d 31 5d  >zDirectory[i-1]
1073c 21 3d 27 2f 27 3b 20 69 2d 2d 29 7b 7d 0a 20 20  !='/'; i--){}.  
1073d 69 66 28 20 69 3e 30 20 29 20 70 50 61 67 65 72  if( i>0 ) pPager
1073e 2d 3e 7a 44 69 72 65 63 74 6f 72 79 5b 69 2d 31  ->zDirectory[i-1
1073f 5d 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 46 69 6c  ] = 0;..  /* Fil
10740 6c 20 69 6e 20 50 61 67 65 72 2e 7a 4a 6f 75 72  l in Pager.zJour
10741 6e 61 6c 5b 5d 20 2a 2f 0a 20 20 69 66 28 20 7a  nal[] */.  if( z
10742 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20  Pathname ){.    
10743 6d 65 6d 63 70 79 28 70 50 61 67 65 72 2d 3e 7a  memcpy(pPager->z
10744 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d  Journal, pPager-
10745 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 50 61 74  >zFilename, nPat
10746 68 6e 61 6d 65 29 3b 0a 20 20 20 20 6d 65 6d 63  hname);.    memc
10747 70 79 28 26 70 50 61 67 65 72 2d 3e 7a 4a 6f 75  py(&pPager->zJou
10748 72 6e 61 6c 5b 6e 50 61 74 68 6e 61 6d 65 5d 2c  rnal[nPathname],
10749 20 22 2d 6a 6f 75 72 6e 61 6c 22 2c 20 39 29 3b   "-journal", 9);
1074a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50  .  }else{.    pP
1074b 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 3d  ager->zJournal =
1074c 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 70 50   0;.  }..  /* pP
1074d 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65  ager->journalOpe
1074e 6e 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67  n = 0; */.  pPag
1074f 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 3d  er->useJournal =
10750 20 75 73 65 4a 6f 75 72 6e 61 6c 3b 0a 20 20 70   useJournal;.  p
10751 50 61 67 65 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63  Pager->noReadloc
10752 6b 20 3d 20 6e 6f 52 65 61 64 6c 6f 63 6b 20 26  k = noReadlock &
10753 26 20 72 65 61 64 4f 6e 6c 79 3b 0a 20 20 2f 2a  & readOnly;.  /*
10754 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 4f 70 65   pPager->stmtOpe
10755 6e 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70  n = 0; */.  /* p
10756 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65  Pager->stmtInUse
10757 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50   = 0; */.  /* pP
10758 61 67 65 72 2d 3e 6e 52 65 66 20 3d 20 30 3b 20  ager->nRef = 0; 
10759 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 53  */.  pPager->dbS
1075a 69 7a 65 20 3d 20 6d 65 6d 44 62 2d 31 3b 0a 20  ize = memDb-1;. 
1075b 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
1075c 65 20 3d 20 73 7a 50 61 67 65 44 66 6c 74 3b 0a  e = szPageDflt;.
1075d 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d    /* pPager->stm
1075e 74 53 69 7a 65 20 3d 20 30 3b 20 2a 2f 0a 20 20  tSize = 0; */.  
1075f 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 4a  /* pPager->stmtJ
10760 53 69 7a 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f  Size = 0; */.  /
10761 2a 20 70 50 61 67 65 72 2d 3e 6e 50 61 67 65 20  * pPager->nPage 
10762 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72  = 0; */.  pPager
10763 2d 3e 6d 78 50 61 67 65 20 3d 20 31 30 30 3b 0a  ->mxPage = 100;.
10764 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f    pPager->mxPgno
10765 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41   = SQLITE_MAX_PA
10766 47 45 5f 43 4f 55 4e 54 3b 0a 20 20 2f 2a 20 70  GE_COUNT;.  /* p
10767 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50  Pager->state = P
10768 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b 20 2a 2f 0a  AGER_UNLOCK; */.
10769 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
1076a 2d 3e 73 74 61 74 65 20 3d 3d 20 28 74 65 6d 70  ->state == (temp
1076b 46 69 6c 65 20 3f 20 50 41 47 45 52 5f 45 58 43  File ? PAGER_EXC
1076c 4c 55 53 49 56 45 20 3a 20 50 41 47 45 52 5f 55  LUSIVE : PAGER_U
1076d 4e 4c 4f 43 4b 29 20 29 3b 0a 20 20 2f 2a 20 70  NLOCK) );.  /* p
1076e 50 61 67 65 72 2d 3e 65 72 72 4d 61 73 6b 20 3d  Pager->errMask =
1076f 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d   0; */.  pPager-
10770 3e 74 65 6d 70 46 69 6c 65 20 3d 20 74 65 6d 70  >tempFile = temp
10771 46 69 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20  File;.  assert( 
10772 74 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 5f  tempFile==PAGER_
10773 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d  LOCKINGMODE_NORM
10774 41 4c 20 0a 20 20 20 20 20 20 20 20 20 20 7c 7c  AL .          ||
10775 20 74 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52   tempFile==PAGER
10776 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43  _LOCKINGMODE_EXC
10777 4c 55 53 49 56 45 20 29 3b 0a 20 20 61 73 73 65  LUSIVE );.  asse
10778 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  rt( PAGER_LOCKIN
10779 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3d  GMODE_EXCLUSIVE=
1077a 3d 31 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e  =1 );.  pPager->
1077b 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20  exclusiveMode = 
1077c 74 65 6d 70 46 69 6c 65 3b 20 0a 20 20 70 50 61  tempFile; .  pPa
1077d 67 65 72 2d 3e 6d 65 6d 44 62 20 3d 20 6d 65 6d  ger->memDb = mem
1077e 44 62 3b 0a 20 20 70 50 61 67 65 72 2d 3e 72 65  Db;.  pPager->re
1077f 61 64 4f 6e 6c 79 20 3d 20 72 65 61 64 4f 6e 6c  adOnly = readOnl
10780 79 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e  y;.  /* pPager->
10781 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 20 2a 2f  needSync = 0; */
10782 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  .  pPager->noSyn
10783 63 20 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d 70  c = pPager->temp
10784 46 69 6c 65 20 7c 7c 20 21 75 73 65 4a 6f 75 72  File || !useJour
10785 6e 61 6c 3b 0a 20 20 70 50 61 67 65 72 2d 3e 66  nal;.  pPager->f
10786 75 6c 6c 53 79 6e 63 20 3d 20 28 70 50 61 67 65  ullSync = (pPage
10787 72 2d 3e 6e 6f 53 79 6e 63 3f 30 3a 31 29 3b 0a  r->noSync?0:1);.
10788 20 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66    pPager->sync_f
10789 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 53 59  lags = SQLITE_SY
1078a 4e 43 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 2f 2a 20  NC_NORMAL;.  /* 
1078b 70 50 61 67 65 72 2d 3e 70 46 69 72 73 74 20 3d  pPager->pFirst =
1078c 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67   0; */.  /* pPag
1078d 65 72 2d 3e 70 46 69 72 73 74 53 79 6e 63 65 64  er->pFirstSynced
1078e 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50   = 0; */.  /* pP
1078f 61 67 65 72 2d 3e 70 4c 61 73 74 20 3d 20 30 3b  ager->pLast = 0;
10790 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6e 45   */.  pPager->nE
10791 78 74 72 61 20 3d 20 6e 45 78 74 72 61 3b 0a 20  xtra = nExtra;. 
10792 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
10793 53 69 7a 65 4c 69 6d 69 74 20 3d 20 53 51 4c 49  SizeLimit = SQLI
10794 54 45 5f 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e  TE_DEFAULT_JOURN
10795 41 4c 5f 53 49 5a 45 5f 4c 49 4d 49 54 3b 0a 20  AL_SIZE_LIMIT;. 
10796 20 61 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e   assert(pPager->
10797 66 64 2d 3e 70 4d 65 74 68 6f 64 73 7c 7c 74 65  fd->pMethods||te
10798 6d 70 46 69 6c 65 29 3b 0a 20 20 73 65 74 53 65  mpFile);.  setSe
10799 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29  ctorSize(pPager)
1079a 3b 0a 20 20 69 66 28 20 6d 65 6d 44 62 20 29 7b  ;.  if( memDb ){
1079b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75  .    pPager->jou
1079c 72 6e 61 6c 4d 6f 64 65 20 3d 20 50 41 47 45 52  rnalMode = PAGER
1079d 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d  _JOURNALMODE_MEM
1079e 4f 52 59 3b 0a 20 20 7d 0a 20 20 2f 2a 20 70 50  ORY;.  }.  /* pP
1079f 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c  ager->pBusyHandl
107a0 65 72 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20  er = 0; */.  /* 
107a1 6d 65 6d 73 65 74 28 70 50 61 67 65 72 2d 3e 61  memset(pPager->a
107a2 48 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Hash, 0, sizeof(
107a3 70 50 61 67 65 72 2d 3e 61 48 61 73 68 29 29 3b  pPager->aHash));
107a4 20 2a 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 3d   */.  *ppPager =
107a5 20 70 50 61 67 65 72 3b 0a 20 20 72 65 74 75 72   pPager;.  retur
107a6 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
107a7 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 75  /*.** Set the bu
107a8 73 79 20 68 61 6e 64 6c 65 72 20 66 75 6e 63 74  sy handler funct
107a9 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
107aa 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
107ab 74 65 33 50 61 67 65 72 53 65 74 42 75 73 79 68  te3PagerSetBusyh
107ac 61 6e 64 6c 65 72 28 50 61 67 65 72 20 2a 70 50  andler(Pager *pP
107ad 61 67 65 72 2c 20 42 75 73 79 48 61 6e 64 6c 65  ager, BusyHandle
107ae 72 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 29  r *pBusyHandler)
107af 7b 0a 20 20 70 50 61 67 65 72 2d 3e 70 42 75 73  {.  pPager->pBus
107b0 79 48 61 6e 64 6c 65 72 20 3d 20 70 42 75 73 79  yHandler = pBusy
107b1 48 61 6e 64 6c 65 72 3b 0a 7d 0a 0a 2f 2a 0a 2a  Handler;.}../*.*
107b2 2a 20 53 65 74 20 74 68 65 20 72 65 69 6e 69 74  * Set the reinit
107b3 69 61 6c 69 7a 65 72 20 66 6f 72 20 74 68 69 73  ializer for this
107b4 20 70 61 67 65 72 2e 20 20 49 66 20 6e 6f 74 20   pager.  If not 
107b5 4e 55 4c 4c 2c 20 74 68 65 20 72 65 69 6e 69 74  NULL, the reinit
107b6 69 61 6c 69 7a 65 72 0a 2a 2a 20 69 73 20 63 61  ializer.** is ca
107b7 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20 63 6f  lled when the co
107b8 6e 74 65 6e 74 20 6f 66 20 61 20 70 61 67 65 20  ntent of a page 
107b9 69 6e 20 63 61 63 68 65 20 69 73 20 72 65 73 74  in cache is rest
107ba 6f 72 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67  ored to its orig
107bb 69 6e 61 6c 0a 2a 2a 20 76 61 6c 75 65 20 61 73  inal.** value as
107bc 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61 20 72   a result of a r
107bd 6f 6c 6c 62 61 63 6b 2e 20 20 54 68 65 20 63 61  ollback.  The ca
107be 6c 6c 62 61 63 6b 20 67 69 76 65 73 20 68 69 67  llback gives hig
107bf 68 65 72 2d 6c 65 76 65 6c 20 63 6f 64 65 0a 2a  her-level code.*
107c0 2a 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79  * an opportunity
107c1 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20   to restore the 
107c2 45 58 54 52 41 20 73 65 63 74 69 6f 6e 20 74 6f  EXTRA section to
107c3 20 61 67 72 65 65 20 77 69 74 68 20 74 68 65 20   agree with the 
107c4 72 65 73 74 6f 72 65 64 0a 2a 2a 20 70 61 67 65  restored.** page
107c5 20 64 61 74 61 2e 0a 2a 2f 0a 53 51 4c 49 54 45   data..*/.SQLITE
107c6 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
107c7 6c 69 74 65 33 50 61 67 65 72 53 65 74 52 65 69  lite3PagerSetRei
107c8 6e 69 74 65 72 28 50 61 67 65 72 20 2a 70 50 61  niter(Pager *pPa
107c9 67 65 72 2c 20 76 6f 69 64 20 28 2a 78 52 65 69  ger, void (*xRei
107ca 6e 69 74 29 28 44 62 50 61 67 65 2a 29 29 7b 0a  nit)(DbPage*)){.
107cb 20 20 70 50 61 67 65 72 2d 3e 78 52 65 69 6e 69    pPager->xReini
107cc 74 65 72 20 3d 20 78 52 65 69 6e 69 74 3b 0a 7d  ter = xReinit;.}
107cd 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
107ce 70 61 67 65 20 73 69 7a 65 20 74 6f 20 2a 70 50  page size to *pP
107cf 61 67 65 53 69 7a 65 2e 20 49 66 20 74 68 65 20  ageSize. If the 
107d0 73 75 67 67 65 73 74 20 6e 65 77 20 70 61 67 65  suggest new page
107d1 20 73 69 7a 65 20 69 73 0a 2a 2a 20 69 6e 61 70   size is.** inap
107d2 70 72 6f 70 72 69 61 74 65 2c 20 74 68 65 6e 20  propriate, then 
107d3 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20 70  an alternative p
107d4 61 67 65 20 73 69 7a 65 20 69 73 20 73 65 74 20  age size is set 
107d5 74 6f 20 74 68 61 74 0a 2a 2a 20 76 61 6c 75 65  to that.** value
107d6 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
107d7 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  g..*/.SQLITE_PRI
107d8 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
107d9 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65  PagerSetPagesize
107da 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
107db 75 31 36 20 2a 70 50 61 67 65 53 69 7a 65 29 7b  u16 *pPageSize){
107dc 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 50 61 67  .  int rc = pPag
107dd 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 69  er->errCode;.  i
107de 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
107df 20 29 7b 0a 20 20 20 20 75 31 36 20 70 61 67 65   ){.    u16 page
107e0 53 69 7a 65 20 3d 20 2a 70 50 61 67 65 53 69 7a  Size = *pPageSiz
107e1 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e;.    assert( p
107e2 61 67 65 53 69 7a 65 3d 3d 30 20 7c 7c 20 28 70  ageSize==0 || (p
107e3 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20  ageSize>=512 && 
107e4 70 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45  pageSize<=SQLITE
107e5 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 29 20  _MAX_PAGE_SIZE) 
107e6 29 3b 0a 20 20 20 20 69 66 28 20 70 61 67 65 53  );.    if( pageS
107e7 69 7a 65 20 26 26 20 70 61 67 65 53 69 7a 65 21  ize && pageSize!
107e8 3d 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a  =pPager->pageSiz
107e9 65 20 0a 20 20 20 20 20 26 26 20 28 70 50 61 67  e .     && (pPag
107ea 65 72 2d 3e 6d 65 6d 44 62 3d 3d 30 20 7c 7c 20  er->memDb==0 || 
107eb 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d  pPager->dbSize==
107ec 30 29 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74  0).     && sqlit
107ed 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74  e3PcacheRefCount
107ee 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
107ef 29 3d 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20  )==0 .    ){.   
107f0 20 20 20 63 68 61 72 20 2a 70 4e 65 77 20 3d 20     char *pNew = 
107f1 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 50  (char *)sqlite3P
107f2 61 67 65 4d 61 6c 6c 6f 63 28 70 61 67 65 53 69  ageMalloc(pageSi
107f3 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  ze);.      if( !
107f4 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20  pNew ){.        
107f5 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
107f6 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  M;.      }else{.
107f7 20 20 20 20 20 20 20 20 70 61 67 65 72 5f 72 65          pager_re
107f8 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20  set(pPager);.   
107f9 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70 61 67       pPager->pag
107fa 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65  eSize = pageSize
107fb 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70  ;.        if( !p
107fc 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 29 20 73  Pager->memDb ) s
107fd 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61  etSectorSize(pPa
107fe 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ger);.        sq
107ff 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 70 50  lite3PageFree(pP
10800 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 29  ager->pTmpSpace)
10801 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72  ;.        pPager
10802 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 70 4e  ->pTmpSpace = pN
10803 65 77 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ew;.        sqli
10804 74 65 33 50 63 61 63 68 65 53 65 74 50 61 67 65  te3PcacheSetPage
10805 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 43  Size(pPager->pPC
10806 61 63 68 65 2c 20 70 61 67 65 53 69 7a 65 29 3b  ache, pageSize);
10807 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
10808 20 20 20 2a 70 50 61 67 65 53 69 7a 65 20 3d 20     *pPageSize = 
10809 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
1080a 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1080b 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  c;.}../*.** Retu
1080c 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
1080d 74 68 65 20 22 74 65 6d 70 6f 72 61 72 79 20 70  the "temporary p
1080e 61 67 65 22 20 62 75 66 66 65 72 20 68 65 6c 64  age" buffer held
1080f 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 62   internally.** b
10810 79 20 74 68 65 20 70 61 67 65 72 2e 20 20 54 68  y the pager.  Th
10811 69 73 20 69 73 20 61 20 62 75 66 66 65 72 20 74  is is a buffer t
10812 68 61 74 20 69 73 20 62 69 67 20 65 6e 6f 75 67  hat is big enoug
10813 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a  h to hold the.**
10814 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 20   entire content 
10815 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 70 61  of a database pa
10816 67 65 2e 20 20 54 68 69 73 20 62 75 66 66 65 72  ge.  This buffer
10817 20 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61   is used interna
10818 6c 6c 79 0a 2a 2a 20 64 75 72 69 6e 67 20 72 6f  lly.** during ro
10819 6c 6c 62 61 63 6b 20 61 6e 64 20 77 69 6c 6c 20  llback and will 
1081a 62 65 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77  be overwritten w
1081b 68 65 6e 65 76 65 72 20 61 20 72 6f 6c 6c 62 61  henever a rollba
1081c 63 6b 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 42  ck.** occurs.  B
1081d 75 74 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73  ut other modules
1081e 20 61 72 65 20 66 72 65 65 20 74 6f 20 75 73 65   are free to use
1081f 20 69 74 20 74 6f 6f 2c 20 61 73 20 6c 6f 6e 67   it too, as long
10820 20 61 73 0a 2a 2a 20 6e 6f 20 72 6f 6c 6c 62 61   as.** no rollba
10821 63 6b 73 20 61 72 65 20 68 61 70 70 65 6e 69 6e  cks are happenin
10822 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  g..*/.SQLITE_PRI
10823 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
10824 65 33 50 61 67 65 72 54 65 6d 70 53 70 61 63 65  e3PagerTempSpace
10825 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
10826 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72  .  return pPager
10827 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 7d 0a 0a  ->pTmpSpace;.}..
10828 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f  /*.** Attempt to
10829 20 73 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d   set the maximum
1082a 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 63   database page c
1082b 6f 75 6e 74 20 69 66 20 6d 78 50 61 67 65 20 69  ount if mxPage i
1082c 73 20 70 6f 73 69 74 69 76 65 2e 20 0a 2a 2a 20  s positive. .** 
1082d 4d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 20  Make no changes 
1082e 69 66 20 6d 78 50 61 67 65 20 69 73 20 7a 65 72  if mxPage is zer
1082f 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 2e 20 20  o or negative.  
10830 41 6e 64 20 6e 65 76 65 72 20 72 65 64 75 63 65  And never reduce
10831 20 74 68 65 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20   the.** maximum 
10832 70 61 67 65 20 63 6f 75 6e 74 20 62 65 6c 6f 77  page count below
10833 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a   the current siz
10834 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
10835 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 61 72 64 6c  e..**.** Regardl
10836 65 73 73 20 6f 66 20 6d 78 50 61 67 65 2c 20 72  ess of mxPage, r
10837 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e  eturn the curren
10838 74 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63  t maximum page c
10839 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ount..*/.SQLITE_
1083a 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1083b 74 65 33 50 61 67 65 72 4d 61 78 50 61 67 65 43  te3PagerMaxPageC
1083c 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67  ount(Pager *pPag
1083d 65 72 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b  er, int mxPage){
1083e 0a 20 20 69 66 28 20 6d 78 50 61 67 65 3e 30 20  .  if( mxPage>0 
1083f 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6d  ){.    pPager->m
10840 78 50 67 6e 6f 20 3d 20 6d 78 50 61 67 65 3b 0a  xPgno = mxPage;.
10841 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 50 61 67    }.  sqlite3Pag
10842 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67  erPagecount(pPag
10843 65 72 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e  er, 0);.  return
10844 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 3b   pPager->mxPgno;
10845 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  .}../*.** The fo
10846 6c 6c 6f 77 69 6e 67 20 73 65 74 20 6f 66 20 72  llowing set of r
10847 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64  outines are used
10848 20 74 6f 20 64 69 73 61 62 6c 65 20 74 68 65 20   to disable the 
10849 73 69 6d 75 6c 61 74 65 64 0a 2a 2a 20 49 2f 4f  simulated.** I/O
1084a 20 65 72 72 6f 72 20 6d 65 63 68 61 6e 69 73 6d   error mechanism
1084b 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  .  These routine
1084c 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 76  s are used to av
1084d 6f 69 64 20 73 69 6d 75 6c 61 74 65 64 0a 2a 2a  oid simulated.**
1084e 20 65 72 72 6f 72 73 20 69 6e 20 70 6c 61 63 65   errors in place
1084f 73 20 77 68 65 72 65 20 77 65 20 64 6f 20 6e 6f  s where we do no
10850 74 20 63 61 72 65 20 61 62 6f 75 74 20 65 72 72  t care about err
10851 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73  ors..**.** Unles
10852 73 20 2d 44 53 51 4c 49 54 45 5f 54 45 53 54 3d  s -DSQLITE_TEST=
10853 31 20 69 73 20 75 73 65 64 2c 20 74 68 65 73 65  1 is used, these
10854 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 61 6c   routines are al
10855 6c 20 6e 6f 2d 6f 70 73 0a 2a 2a 20 61 6e 64 20  l no-ops.** and 
10856 67 65 6e 65 72 61 74 65 20 6e 6f 20 63 6f 64 65  generate no code
10857 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
10858 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41  TE_TEST.SQLITE_A
10859 50 49 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71  PI extern int sq
1085a 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
1085b 65 6e 64 69 6e 67 3b 0a 53 51 4c 49 54 45 5f 41  ending;.SQLITE_A
1085c 50 49 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71  PI extern int sq
1085d 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68  lite3_io_error_h
1085e 69 74 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 73  it;.static int s
1085f 61 76 65 64 5f 63 6e 74 3b 0a 76 6f 69 64 20 64  aved_cnt;.void d
10860 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64  isable_simulated
10861 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29  _io_errors(void)
10862 7b 0a 20 20 73 61 76 65 64 5f 63 6e 74 20 3d 20  {.  saved_cnt = 
10863 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
10864 5f 70 65 6e 64 69 6e 67 3b 0a 20 20 73 71 6c 69  _pending;.  sqli
10865 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e  te3_io_error_pen
10866 64 69 6e 67 20 3d 20 2d 31 3b 0a 7d 0a 76 6f 69  ding = -1;.}.voi
10867 64 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74  d enable_simulat
10868 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69  ed_io_errors(voi
10869 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f  d){.  sqlite3_io
1086a 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d  _error_pending =
1086b 20 73 61 76 65 64 5f 63 6e 74 3b 0a 7d 0a 23 65   saved_cnt;.}.#e
1086c 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 64 69 73  lse.# define dis
1086d 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69  able_simulated_i
1086e 6f 5f 65 72 72 6f 72 73 28 29 0a 23 20 64 65 66  o_errors().# def
1086f 69 6e 65 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c  ine enable_simul
10870 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29  ated_io_errors()
10871 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52  .#endif../*.** R
10872 65 61 64 20 74 68 65 20 66 69 72 73 74 20 4e 20  ead the first N 
10873 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 62  bytes from the b
10874 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20  eginning of the 
10875 66 69 6c 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79  file into memory
10876 0a 2a 2a 20 74 68 61 74 20 70 44 65 73 74 20 70  .** that pDest p
10877 6f 69 6e 74 73 20 74 6f 2e 20 0a 2a 2a 0a 2a 2a  oints to. .**.**
10878 20 4e 6f 20 65 72 72 6f 72 20 63 68 65 63 6b 69   No error checki
10879 6e 67 20 69 73 20 64 6f 6e 65 2e 20 54 68 65 20  ng is done. The 
1087a 72 61 74 69 6f 6e 61 6c 20 66 6f 72 20 74 68 69  rational for thi
1087b 73 20 69 73 20 74 68 61 74 20 74 68 69 73 20 66  s is that this f
1087c 75 6e 63 74 69 6f 6e 20 0a 2a 2a 20 6d 61 79 20  unction .** may 
1087d 62 65 20 63 61 6c 6c 65 64 20 65 76 65 6e 20 69  be called even i
1087e 66 20 74 68 65 20 66 69 6c 65 20 64 6f 65 73 20  f the file does 
1087f 6e 6f 74 20 65 78 69 73 74 20 6f 72 20 63 6f 6e  not exist or con
10880 74 61 69 6e 20 61 20 68 65 61 64 65 72 2e 20 49  tain a header. I
10881 6e 20 0a 2a 2a 20 74 68 65 73 65 20 63 61 73 65  n .** these case
10882 73 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28  s sqlite3OsRead(
10883 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 61 6e  ) will return an
10884 20 65 72 72 6f 72 2c 20 74 6f 20 77 68 69 63 68   error, to which
10885 20 74 68 65 20 63 6f 72 72 65 63 74 20 0a 2a 2a   the correct .**
10886 20 72 65 73 70 6f 6e 73 65 20 69 73 20 74 6f 20   response is to 
10887 7a 65 72 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20  zero the memory 
10888 61 74 20 70 44 65 73 74 20 61 6e 64 20 63 6f 6e  at pDest and con
10889 74 69 6e 75 65 2e 20 20 41 20 72 65 61 6c 20 49  tinue.  A real I
1088a 4f 20 65 72 72 6f 72 20 0a 2a 2a 20 77 69 6c 6c  O error .** will
1088b 20 70 72 65 73 75 6d 61 62 6c 79 20 72 65 63 75   presumably recu
1088c 72 20 61 6e 64 20 62 65 20 70 69 63 6b 65 64 20  r and be picked 
1088d 75 70 20 6c 61 74 65 72 20 28 54 6f 64 6f 3a 20  up later (Todo: 
1088e 54 68 69 6e 6b 20 61 62 6f 75 74 20 74 68 69 73  Think about this
1088f 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
10890 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
10891 50 61 67 65 72 52 65 61 64 46 69 6c 65 68 65 61  PagerReadFilehea
10892 64 65 72 28 50 61 67 65 72 20 2a 70 50 61 67 65  der(Pager *pPage
10893 72 2c 20 69 6e 74 20 4e 2c 20 75 6e 73 69 67 6e  r, int N, unsign
10894 65 64 20 63 68 61 72 20 2a 70 44 65 73 74 29 7b  ed char *pDest){
10895 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
10896 54 45 5f 4f 4b 3b 0a 20 20 6d 65 6d 73 65 74 28  TE_OK;.  memset(
10897 70 44 65 73 74 2c 20 30 2c 20 4e 29 3b 0a 20 20  pDest, 0, N);.  
10898 61 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e 66  assert(pPager->f
10899 64 2d 3e 70 4d 65 74 68 6f 64 73 7c 7c 70 50 61  d->pMethods||pPa
1089a 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 3b 0a  ger->tempFile);.
1089b 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 66 64    if( pPager->fd
1089c 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20  ->pMethods ){.  
1089d 20 20 49 4f 54 52 41 43 45 28 28 22 44 42 48 44    IOTRACE(("DBHD
1089e 52 20 25 70 20 30 20 25 64 5c 6e 22 2c 20 70 50  R %p 0 %d\n", pP
1089f 61 67 65 72 2c 20 4e 29 29 0a 20 20 20 20 72 63  ager, N)).    rc
108a0 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64   = sqlite3OsRead
108a1 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 44 65  (pPager->fd, pDe
108a2 73 74 2c 20 4e 2c 20 30 29 3b 0a 20 20 20 20 69  st, N, 0);.    i
108a3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f  f( rc==SQLITE_IO
108a4 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29  ERR_SHORT_READ )
108a5 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
108a6 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20  ITE_OK;.    }.  
108a7 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
108a8 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
108a9 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
108aa 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
108ab 64 69 73 6b 20 66 69 6c 65 20 61 73 73 6f 63 69  disk file associ
108ac 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 70 50 61  ated with.** pPa
108ad 67 65 72 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ger. .**.** If t
108ae 68 65 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20  he PENDING_BYTE 
108af 6c 69 65 73 20 6f 6e 20 74 68 65 20 70 61 67 65  lies on the page
108b0 20 64 69 72 65 63 74 6c 79 20 61 66 74 65 72 20   directly after 
108b1 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a  the end of the.*
108b2 2a 20 66 69 6c 65 2c 20 74 68 65 6e 20 63 6f 6e  * file, then con
108b3 73 69 64 65 72 20 74 68 69 73 20 70 61 67 65 20  sider this page 
108b4 70 61 72 74 20 6f 66 20 74 68 65 20 66 69 6c 65  part of the file
108b5 20 74 6f 6f 2e 20 46 6f 72 20 65 78 61 6d 70 6c   too. For exampl
108b6 65 2c 20 69 66 0a 2a 2a 20 50 45 4e 44 49 4e 47  e, if.** PENDING
108b7 5f 42 59 54 45 20 69 73 20 62 79 74 65 20 34 30  _BYTE is byte 40
108b8 39 36 20 28 74 68 65 20 66 69 72 73 74 20 62 79  96 (the first by
108b9 74 65 20 6f 66 20 70 61 67 65 20 35 29 20 61 6e  te of page 5) an
108ba 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  d the size of th
108bb 65 0a 2a 2a 20 66 69 6c 65 20 69 73 20 34 30 39  e.** file is 409
108bc 36 20 62 79 74 65 73 2c 20 35 20 69 73 20 72 65  6 bytes, 5 is re
108bd 74 75 72 6e 65 64 20 69 6e 73 74 65 61 64 20 6f  turned instead o
108be 66 20 34 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  f 4..*/.SQLITE_P
108bf 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
108c0 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
108c1 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
108c2 69 6e 74 20 2a 70 6e 50 61 67 65 29 7b 0a 20 20  int *pnPage){.  
108c3 69 36 34 20 6e 20 3d 20 30 3b 0a 20 20 69 6e 74  i64 n = 0;.  int
108c4 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70   rc;.  assert( p
108c5 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 69 66  Pager!=0 );.  if
108c6 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  ( pPager->errCod
108c7 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 50  e ){.    rc = pP
108c8 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20  ager->errCode;. 
108c9 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
108ca 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  }.  if( pPager->
108cb 64 62 53 69 7a 65 3e 3d 30 20 29 7b 0a 20 20 20  dbSize>=0 ){.   
108cc 20 6e 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53   n = pPager->dbS
108cd 69 7a 65 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a  ize;.  } else {.
108ce 20 20 20 20 61 73 73 65 72 74 28 70 50 61 67 65      assert(pPage
108cf 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 7c  r->fd->pMethods|
108d0 7c 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c  |pPager->tempFil
108d1 65 29 3b 0a 20 20 20 20 69 66 28 20 28 70 50 61  e);.    if( (pPa
108d2 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64  ger->fd->pMethod
108d3 73 29 0a 20 20 20 20 20 26 26 20 28 72 63 20 3d  s).     && (rc =
108d4 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69   sqlite3OsFileSi
108d5 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26  ze(pPager->fd, &
108d6 6e 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  n))!=SQLITE_OK )
108d7 7b 0a 20 20 20 20 20 20 70 61 67 65 72 5f 65 72  {.      pager_er
108d8 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b  ror(pPager, rc);
108d9 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
108da 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
108db 6e 3e 30 20 26 26 20 6e 3c 70 50 61 67 65 72 2d  n>0 && n<pPager-
108dc 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20  >pageSize ){.   
108dd 20 20 20 6e 20 3d 20 31 3b 0a 20 20 20 20 7d 65     n = 1;.    }e
108de 6c 73 65 7b 0a 20 20 20 20 20 20 6e 20 2f 3d 20  lse{.      n /= 
108df 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
108e0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
108e1 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50  pPager->state!=P
108e2 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 7b 0a 20  AGER_UNLOCK ){. 
108e3 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53       pPager->dbS
108e4 69 7a 65 20 3d 20 6e 3b 0a 20 20 20 20 7d 0a 20  ize = n;.    }. 
108e5 20 7d 0a 20 20 69 66 28 20 6e 3d 3d 28 50 45 4e   }.  if( n==(PEN
108e6 44 49 4e 47 5f 42 59 54 45 2f 70 50 61 67 65 72  DING_BYTE/pPager
108e7 2d 3e 70 61 67 65 53 69 7a 65 29 20 29 7b 0a 20  ->pageSize) ){. 
108e8 20 20 20 6e 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66     n++;.  }.  if
108e9 28 20 6e 3e 70 50 61 67 65 72 2d 3e 6d 78 50 67  ( n>pPager->mxPg
108ea 6e 6f 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72  no ){.    pPager
108eb 2d 3e 6d 78 50 67 6e 6f 20 3d 20 6e 3b 0a 20 20  ->mxPgno = n;.  
108ec 7d 0a 20 20 69 66 28 20 70 6e 50 61 67 65 20 29  }.  if( pnPage )
108ed 7b 0a 20 20 20 20 2a 70 6e 50 61 67 65 20 3d 20  {.    *pnPage = 
108ee 6e 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  n;.  }.  return 
108ef 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
108f0 0a 2a 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c  .** Forward decl
108f1 61 72 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69  aration.*/.stati
108f2 63 20 69 6e 74 20 73 79 6e 63 4a 6f 75 72 6e 61  c int syncJourna
108f3 6c 28 50 61 67 65 72 2a 29 3b 0a 0a 2f 2a 0a 2a  l(Pager*);../*.*
108f4 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
108f5 73 20 75 73 65 64 20 74 6f 20 74 72 75 6e 63 61  s used to trunca
108f6 74 65 20 74 68 65 20 63 61 63 68 65 20 77 68 65  te the cache whe
108f7 6e 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  n a database.** 
108f8 69 73 20 74 72 75 6e 63 61 74 65 64 2e 20 20 44  is truncated.  D
108f9 72 6f 70 20 66 72 6f 6d 20 74 68 65 20 63 61 63  rop from the cac
108fa 68 65 20 61 6c 6c 20 70 61 67 65 73 20 77 68 6f  he all pages who
108fb 73 65 20 70 67 6e 6f 20 69 73 0a 2a 2a 20 6c 61  se pgno is.** la
108fc 72 67 65 72 20 74 68 61 6e 20 70 50 61 67 65 72  rger than pPager
108fd 2d 3e 64 62 53 69 7a 65 20 61 6e 64 20 69 73 20  ->dbSize and is 
108fe 75 6e 72 65 66 65 72 65 6e 63 65 64 2e 0a 2a 2a  unreferenced..**
108ff 0a 2a 2a 20 52 65 66 65 72 65 6e 63 65 64 20 70  .** Referenced p
10900 61 67 65 73 20 6c 61 72 67 65 72 20 74 68 61 6e  ages larger than
10901 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20   pPager->dbSize 
10902 61 72 65 20 7a 65 72 6f 65 64 2e 0a 2a 2a 0a 2a  are zeroed..**.*
10903 2a 20 41 63 74 75 61 6c 6c 79 2c 20 61 74 20 74  * Actually, at t
10904 68 65 20 70 6f 69 6e 74 20 74 68 69 73 20 72 6f  he point this ro
10905 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c  utine is called,
10906 20 69 74 20 77 6f 75 6c 64 20 62 65 0a 2a 2a 20   it would be.** 
10907 61 6e 20 65 72 72 6f 72 20 74 6f 20 68 61 76 65  an error to have
10908 20 61 20 72 65 66 65 72 65 6e 63 65 64 20 70 61   a referenced pa
10909 67 65 2e 20 20 42 75 74 20 72 61 74 68 65 72 20  ge.  But rather 
1090a 74 68 61 6e 20 64 65 6c 65 74 65 0a 2a 2a 20 74  than delete.** t
1090b 68 61 74 20 70 61 67 65 20 61 6e 64 20 67 75 61  hat page and gua
1090c 72 61 6e 74 65 65 20 61 20 73 75 62 73 65 71 75  rantee a subsequ
1090d 65 6e 74 20 73 65 67 66 61 75 6c 74 2c 20 69 74  ent segfault, it
1090e 20 73 65 65 6d 73 20 62 65 74 74 65 72 0a 2a 2a   seems better.**
1090f 20 74 6f 20 7a 65 72 6f 20 69 74 20 61 6e 64 20   to zero it and 
10910 68 6f 70 65 20 74 68 61 74 20 77 65 20 65 72 72  hope that we err
10911 6f 72 20 6f 75 74 20 73 61 6e 65 6c 79 2e 0a 2a  or out sanely..*
10912 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61  /.static void pa
10913 67 65 72 5f 74 72 75 6e 63 61 74 65 5f 63 61 63  ger_truncate_cac
10914 68 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  he(Pager *pPager
10915 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63  ){.  sqlite3Pcac
10916 68 65 54 72 75 6e 63 61 74 65 28 70 50 61 67 65  heTruncate(pPage
10917 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 50 61 67  r->pPCache, pPag
10918 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 7d 0a 0a  er->dbSize);.}..
10919 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74  /*.** Try to obt
1091a 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20  ain a lock on a 
1091b 66 69 6c 65 2e 20 20 49 6e 76 6f 6b 65 20 74 68  file.  Invoke th
1091c 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20  e busy callback 
1091d 69 66 20 74 68 65 20 6c 6f 63 6b 0a 2a 2a 20 69  if the lock.** i
1091e 73 20 63 75 72 72 65 6e 74 6c 79 20 6e 6f 74 20  s currently not 
1091f 61 76 61 69 6c 61 62 6c 65 2e 20 20 52 65 70 65  available.  Repe
10920 61 74 20 75 6e 74 69 6c 20 74 68 65 20 62 75 73  at until the bus
10921 79 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72  y callback retur
10922 6e 73 0a 2a 2a 20 66 61 6c 73 65 20 6f 72 20 75  ns.** false or u
10923 6e 74 69 6c 20 74 68 65 20 6c 6f 63 6b 20 73 75  ntil the lock su
10924 63 63 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 52 65  cceeds..**.** Re
10925 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f  turn SQLITE_OK o
10926 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 61 6e  n success and an
10927 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 77   error code if w
10928 65 20 63 61 6e 6e 6f 74 20 6f 62 74 61 69 6e 0a  e cannot obtain.
10929 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e 0a 2a 2f 0a  ** the lock..*/.
1092a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72  static int pager
1092b 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 50 61  _wait_on_lock(Pa
1092c 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
1092d 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e   locktype){.  in
1092e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 65 20  t rc;..  /* The 
1092f 4f 53 20 6c 6f 63 6b 20 76 61 6c 75 65 73 20 6d  OS lock values m
10930 75 73 74 20 62 65 20 74 68 65 20 73 61 6d 65 20  ust be the same 
10931 61 73 20 74 68 65 20 50 61 67 65 72 20 6c 6f 63  as the Pager loc
10932 6b 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 61 73  k values */.  as
10933 73 65 72 74 28 20 50 41 47 45 52 5f 53 48 41 52  sert( PAGER_SHAR
10934 45 44 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  ED==SHARED_LOCK 
10935 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47  );.  assert( PAG
10936 45 52 5f 52 45 53 45 52 56 45 44 3d 3d 52 45 53  ER_RESERVED==RES
10937 45 52 56 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  ERVED_LOCK );.  
10938 61 73 73 65 72 74 28 20 50 41 47 45 52 5f 45 58  assert( PAGER_EX
10939 43 4c 55 53 49 56 45 3d 3d 45 58 43 4c 55 53 49  CLUSIVE==EXCLUSI
1093a 56 45 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a  VE_LOCK );..  /*
1093b 20 49 66 20 74 68 65 20 66 69 6c 65 20 69 73 20   If the file is 
1093c 63 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b  currently unlock
1093d 65 64 20 74 68 65 6e 20 74 68 65 20 73 69 7a 65  ed then the size
1093e 20 6d 75 73 74 20 62 65 20 75 6e 6b 6e 6f 77 6e   must be unknown
1093f 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50   */.  assert( pP
10940 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47  ager->state>=PAG
10941 45 52 5f 53 48 41 52 45 44 20 7c 7c 20 70 50 61  ER_SHARED || pPa
10942 67 65 72 2d 3e 64 62 53 69 7a 65 3c 30 20 29 3b  ger->dbSize<0 );
10943 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ..  if( pPager->
10944 73 74 61 74 65 3e 3d 6c 6f 63 6b 74 79 70 65 20  state>=locktype 
10945 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
10946 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a  TE_OK;.  }else{.
10947 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
10948 70 42 75 73 79 48 61 6e 64 6c 65 72 20 29 20 70  pBusyHandler ) p
10949 50 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64  Pager->pBusyHand
1094a 6c 65 72 2d 3e 6e 42 75 73 79 20 3d 20 30 3b 0a  ler->nBusy = 0;.
1094b 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 72      do {.      r
1094c 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63  c = sqlite3OsLoc
1094d 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 6c 6f  k(pPager->fd, lo
1094e 63 6b 74 79 70 65 29 3b 0a 20 20 20 20 7d 77 68  cktype);.    }wh
1094f 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ile( rc==SQLITE_
10950 42 55 53 59 20 26 26 20 73 71 6c 69 74 65 33 49  BUSY && sqlite3I
10951 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72  nvokeBusyHandler
10952 28 70 50 61 67 65 72 2d 3e 70 42 75 73 79 48 61  (pPager->pBusyHa
10953 6e 64 6c 65 72 29 20 29 3b 0a 20 20 20 20 69 66  ndler) );.    if
10954 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
10955 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  ){.      pPager-
10956 3e 73 74 61 74 65 20 3d 20 6c 6f 63 6b 74 79 70  >state = locktyp
10957 65 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43 45  e;.      IOTRACE
10958 28 28 22 4c 4f 43 4b 20 25 70 20 25 64 5c 6e 22  (("LOCK %p %d\n"
10959 2c 20 70 50 61 67 65 72 2c 20 6c 6f 63 6b 74 79  , pPager, lockty
1095a 70 65 29 29 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  pe)).    }.  }. 
1095b 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1095c 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68  *.** Truncate th
1095d 65 20 66 69 6c 65 20 74 6f 20 74 68 65 20 6e 75  e file to the nu
1095e 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 70  mber of pages sp
1095f 65 63 69 66 69 65 64 2e 0a 2a 2f 0a 53 51 4c 49  ecified..*/.SQLI
10960 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
10961 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63  qlite3PagerTrunc
10962 61 74 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  ate(Pager *pPage
10963 72 2c 20 50 67 6e 6f 20 6e 50 61 67 65 29 7b 0a  r, Pgno nPage){.
10964 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
10965 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20  E_OK;.  assert( 
10966 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50  pPager->state>=P
10967 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 0a  AGER_SHARED );..
10968 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61    sqlite3PagerPa
10969 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20  gecount(pPager, 
1096a 30 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72  0);.  if( pPager
1096b 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20  ->errCode ){.   
1096c 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72   rc = pPager->er
1096d 72 43 6f 64 65 3b 0a 20 20 7d 65 6c 73 65 20 69  rCode;.  }else i
1096e 66 28 20 6e 50 61 67 65 3c 28 75 6e 73 69 67 6e  f( nPage<(unsign
1096f 65 64 29 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  ed)pPager->dbSiz
10970 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 79  e ){.    rc = sy
10971 6e 63 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72  ncJournal(pPager
10972 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
10973 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
10974 20 20 2f 2a 20 47 65 74 20 61 6e 20 65 78 63 6c    /* Get an excl
10975 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 74 68  usive lock on th
10976 65 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72  e database befor
10977 65 20 74 72 75 6e 63 61 74 69 6e 67 2e 20 2a 2f  e truncating. */
10978 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
10979 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70  r_wait_on_lock(p
1097a 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45  Pager, EXCLUSIVE
1097b 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 7d 0a 20 20  _LOCK);.    }.  
1097c 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1097d 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20  _OK ){.      rc 
1097e 3d 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65  = pager_truncate
1097f 28 70 50 61 67 65 72 2c 20 6e 50 61 67 65 29 3b  (pPager, nPage);
10980 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65  .    }.  }..  re
10981 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
10982 2a 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20 70  * Shutdown the p
10983 61 67 65 20 63 61 63 68 65 2e 20 20 46 72 65 65  age cache.  Free
10984 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6e 64 20   all memory and 
10985 63 6c 6f 73 65 20 61 6c 6c 20 66 69 6c 65 73 2e  close all files.
10986 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 72 61 6e  .**.** If a tran
10987 73 61 63 74 69 6f 6e 20 77 61 73 20 69 6e 20 70  saction was in p
10988 72 6f 67 72 65 73 73 20 77 68 65 6e 20 74 68 69  rogress when thi
10989 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
1098a 6c 65 64 2c 20 74 68 61 74 0a 2a 2a 20 74 72 61  led, that.** tra
1098b 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c  nsaction is roll
1098c 65 64 20 62 61 63 6b 2e 20 20 41 6c 6c 20 6f 75  ed back.  All ou
1098d 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 73 20  tstanding pages 
1098e 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 0a  are invalidated.
1098f 2a 2a 20 61 6e 64 20 74 68 65 69 72 20 6d 65 6d  ** and their mem
10990 6f 72 79 20 69 73 20 66 72 65 65 64 2e 20 20 41  ory is freed.  A
10991 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73  ny attempt to us
10992 65 20 61 20 70 61 67 65 20 61 73 73 6f 63 69 61  e a page associa
10993 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 69 73  ted.** with this
10994 20 70 61 67 65 20 63 61 63 68 65 20 61 66 74 65   page cache afte
10995 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  r this function 
10996 72 65 74 75 72 6e 73 20 77 69 6c 6c 20 6c 69 6b  returns will lik
10997 65 6c 79 0a 2a 2a 20 72 65 73 75 6c 74 20 69 6e  ely.** result in
10998 20 61 20 63 6f 72 65 64 75 6d 70 2e 0a 2a 2a 0a   a coredump..**.
10999 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
1099a 20 61 6c 77 61 79 73 20 73 75 63 63 65 65 64 73   always succeeds
1099b 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69  . If a transacti
1099c 6f 6e 20 69 73 20 61 63 74 69 76 65 20 61 6e 20  on is active an 
1099d 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73 20 6d 61  attempt.** is ma
1099e 64 65 20 74 6f 20 72 6f 6c 6c 20 69 74 20 62 61  de to roll it ba
1099f 63 6b 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  ck. If an error 
109a0 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 68  occurs during th
109a1 65 20 72 6f 6c 6c 62 61 63 6b 20 0a 2a 2a 20 61  e rollback .** a
109a2 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 6d 61 79   hot journal may
109a3 20 62 65 20 6c 65 66 74 20 69 6e 20 74 68 65 20   be left in the 
109a4 66 69 6c 65 73 79 73 74 65 6d 20 62 75 74 20 6e  filesystem but n
109a5 6f 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72  o error is retur
109a6 6e 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61  ned.** to the ca
109a7 6c 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ller..*/.SQLITE_
109a8 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
109a9 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 50 61  te3PagerClose(Pa
109aa 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 0a 20  ger *pPager){.. 
109ab 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74   disable_simulat
109ac 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a  ed_io_errors();.
109ad 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65    sqlite3BeginBe
109ae 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20  nignMalloc();.  
109af 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20  pPager->errCode 
109b0 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65  = 0;.  pPager->e
109b1 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 30  xclusiveMode = 0
109b2 3b 0a 20 20 70 61 67 65 72 5f 72 65 73 65 74 28  ;.  pager_reset(
109b3 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 21  pPager);.  if( !
109b4 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 70 61 67  MEMDB ){.    pag
109b5 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62  erUnlockAndRollb
109b6 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  ack(pPager);.  }
109b7 0a 20 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61  .  enable_simula
109b8 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b  ted_io_errors();
109b9 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e  .  sqlite3EndBen
109ba 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 50  ignMalloc();.  P
109bb 41 47 45 52 54 52 41 43 45 32 28 22 43 4c 4f 53  AGERTRACE2("CLOS
109bc 45 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44  E %d\n", PAGERID
109bd 28 70 50 61 67 65 72 29 29 3b 0a 20 20 49 4f 54  (pPager));.  IOT
109be 52 41 43 45 28 28 22 43 4c 4f 53 45 20 25 70 5c  RACE(("CLOSE %p\
109bf 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 69  n", pPager)).  i
109c0 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  f( pPager->journ
109c1 61 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20 73 71  alOpen ){.    sq
109c2 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61  lite3OsClose(pPa
109c3 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 7d 0a 20  ger->jfd);.  }. 
109c4 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65   sqlite3BitvecDe
109c5 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 49  stroy(pPager->pI
109c6 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 73 71 6c  nJournal);.  sql
109c7 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f  ite3BitvecDestro
109c8 79 28 70 50 61 67 65 72 2d 3e 70 41 6c 77 61 79  y(pPager->pAlway
109c9 73 52 6f 6c 6c 62 61 63 6b 29 3b 0a 20 20 69 66  sRollback);.  if
109ca 28 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 4f 70  ( pPager->stmtOp
109cb 65 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  en ){.    sqlite
109cc 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d  3OsClose(pPager-
109cd 3e 73 74 66 64 29 3b 0a 20 20 7d 0a 20 20 73 71  >stfd);.  }.  sq
109ce 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61  lite3OsClose(pPa
109cf 67 65 72 2d 3e 66 64 29 3b 0a 20 20 2f 2a 20 54  ger->fd);.  /* T
109d0 65 6d 70 20 66 69 6c 65 73 20 61 72 65 20 61 75  emp files are au
109d1 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65  tomatically dele
109d2 74 65 64 20 62 79 20 74 68 65 20 4f 53 0a 20 20  ted by the OS.  
109d3 2a 2a 20 69 66 28 20 70 50 61 67 65 72 2d 3e 74  ** if( pPager->t
109d4 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 2a 2a 20  empFile ){.  ** 
109d5 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74    sqlite3OsDelet
109d6 65 28 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e  e(pPager->zFilen
109d7 61 6d 65 29 3b 0a 20 20 2a 2a 20 7d 0a 20 20 2a  ame);.  ** }.  *
109d8 2f 0a 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65  /..  sqlite3Page
109d9 46 72 65 65 28 70 50 61 67 65 72 2d 3e 70 54 6d  Free(pPager->pTm
109da 70 53 70 61 63 65 29 3b 0a 20 20 73 71 6c 69 74  pSpace);.  sqlit
109db 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28 70 50  e3PcacheClose(pP
109dc 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a  ager->pPCache);.
109dd 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
109de 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72 6e  Pager);.  return
109df 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23   SQLITE_OK;.}..#
109e0 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42  if !defined(NDEB
109e1 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  UG) || defined(S
109e2 51 4c 49 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a  QLITE_TEST)./*.*
109e3 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61 67  * Return the pag
109e4 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  e number for the
109e5 20 67 69 76 65 6e 20 70 61 67 65 20 64 61 74 61   given page data
109e6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
109e7 41 54 45 20 50 67 6e 6f 20 73 71 6c 69 74 65 33  ATE Pgno sqlite3
109e8 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 28  PagerPagenumber(
109e9 44 62 50 61 67 65 20 2a 70 29 7b 0a 20 20 72 65  DbPage *p){.  re
109ea 74 75 72 6e 20 70 2d 3e 70 67 6e 6f 3b 0a 7d 0a  turn p->pgno;.}.
109eb 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e  #endif../*.** In
109ec 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65  crement the refe
109ed 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72 20  rence count for 
109ee 61 20 70 61 67 65 2e 20 20 54 68 65 20 69 6e 70  a page.  The inp
109ef 75 74 20 70 6f 69 6e 74 65 72 20 69 73 0a 2a 2a  ut pointer is.**
109f0 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20   a reference to 
109f1 74 68 65 20 70 61 67 65 20 64 61 74 61 2e 0a 2a  the page data..*
109f2 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
109f3 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
109f4 72 52 65 66 28 44 62 50 61 67 65 20 2a 70 50 67  rRef(DbPage *pPg
109f5 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63  ){.  sqlite3Pcac
109f6 68 65 52 65 66 28 70 50 67 29 3b 0a 20 20 72 65  heRef(pPg);.  re
109f7 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
109f8 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68  }../*.** Sync th
109f9 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 6e 20 6f  e journal.  In o
109fa 74 68 65 72 20 77 6f 72 64 73 2c 20 6d 61 6b 65  ther words, make
109fb 20 73 75 72 65 20 61 6c 6c 20 74 68 65 20 70 61   sure all the pa
109fc 67 65 73 20 74 68 61 74 20 68 61 76 65 0a 2a 2a  ges that have.**
109fd 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f   been written to
109fe 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 61 76   the journal hav
109ff 65 20 61 63 74 75 61 6c 6c 79 20 72 65 61 63 68  e actually reach
10a00 65 64 20 74 68 65 20 73 75 72 66 61 63 65 20 6f  ed the surface o
10a01 66 20 74 68 65 0a 2a 2a 20 64 69 73 6b 2e 20 20  f the.** disk.  
10a02 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74  It is not safe t
10a03 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 6f 72 69  o modify the ori
10a04 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66  ginal database f
10a05 69 6c 65 20 75 6e 74 69 6c 20 61 66 74 65 72 0a  ile until after.
10a06 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68  ** the journal h
10a07 61 73 20 62 65 65 6e 20 73 79 6e 63 65 64 2e 20  as been synced. 
10a08 20 49 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   If the original
10a09 20 64 61 74 61 62 61 73 65 20 69 73 20 6d 6f 64   database is mod
10a0a 69 66 69 65 64 20 62 65 66 6f 72 65 0a 2a 2a 20  ified before.** 
10a0b 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73  the journal is s
10a0c 79 6e 63 65 64 20 61 6e 64 20 61 20 70 6f 77 65  ynced and a powe
10a0d 72 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73  r failure occurs
10a0e 2c 20 74 68 65 20 75 6e 73 79 6e 63 65 64 20 6a  , the unsynced j
10a0f 6f 75 72 6e 61 6c 0a 2a 2a 20 64 61 74 61 20 77  ournal.** data w
10a10 6f 75 6c 64 20 62 65 20 6c 6f 73 74 20 61 6e 64  ould be lost and
10a11 20 77 65 20 77 6f 75 6c 64 20 62 65 20 75 6e 61   we would be una
10a12 62 6c 65 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c  ble to completel
10a13 79 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 0a 2a  y rollback the.*
10a14 2a 20 64 61 74 61 62 61 73 65 20 63 68 61 6e 67  * database chang
10a15 65 73 2e 20 20 44 61 74 61 62 61 73 65 20 63 6f  es.  Database co
10a16 72 72 75 70 74 69 6f 6e 20 77 6f 75 6c 64 20 6f  rruption would o
10a17 63 63 75 72 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 69  ccur..** .** Thi
10a18 73 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 75  s routine also u
10a19 70 64 61 74 65 73 20 74 68 65 20 6e 52 65 63 20  pdates the nRec 
10a1a 66 69 65 6c 64 20 69 6e 20 74 68 65 20 68 65 61  field in the hea
10a1b 64 65 72 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  der of the journ
10a1c 61 6c 2e 0a 2a 2a 20 28 53 65 65 20 63 6f 6d 6d  al..** (See comm
10a1d 65 6e 74 73 20 6f 6e 20 74 68 65 20 70 61 67 65  ents on the page
10a1e 72 5f 70 6c 61 79 62 61 63 6b 28 29 20 72 6f 75  r_playback() rou
10a1f 74 69 6e 65 20 66 6f 72 20 61 64 64 69 74 69 6f  tine for additio
10a20 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
10a21 29 0a 2a 2a 20 49 66 20 74 68 65 20 73 79 6e 63  ).** If the sync
10a22 20 6d 6f 64 65 20 69 73 20 46 55 4c 4c 2c 20 74   mode is FULL, t
10a23 77 6f 20 73 79 6e 63 73 20 77 69 6c 6c 20 6f 63  wo syncs will oc
10a24 63 75 72 2e 20 20 46 69 72 73 74 20 74 68 65 20  cur.  First the 
10a25 77 68 6f 6c 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a  whole journal.**
10a26 20 69 73 20 73 79 6e 63 65 64 2c 20 74 68 65 6e   is synced, then
10a27 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 20   the nRec field 
10a28 69 73 20 75 70 64 61 74 65 64 2c 20 74 68 65 6e  is updated, then
10a29 20 61 20 73 65 63 6f 6e 64 20 73 79 6e 63 20 6f   a second sync o
10a2a 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  ccurs..**.** For
10a2b 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62   temporary datab
10a2c 61 73 65 73 2c 20 77 65 20 64 6f 20 6e 6f 74 20  ases, we do not 
10a2d 63 61 72 65 20 69 66 20 77 65 20 61 72 65 20 61  care if we are a
10a2e 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 0a  ble to rollback.
10a2f 2a 2a 20 61 66 74 65 72 20 61 20 70 6f 77 65 72  ** after a power
10a30 20 66 61 69 6c 75 72 65 2c 20 73 6f 20 6e 6f 20   failure, so no 
10a31 73 79 6e 63 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a  sync occurs..**.
10a32 2a 2a 20 49 66 20 74 68 65 20 49 4f 43 41 50 5f  ** If the IOCAP_
10a33 53 45 51 55 45 4e 54 49 41 4c 20 66 6c 61 67 20  SEQUENTIAL flag 
10a34 69 73 20 73 65 74 20 66 6f 72 20 74 68 65 20 70  is set for the p
10a35 65 72 73 69 73 74 65 6e 74 20 6d 65 64 69 61 20  ersistent media 
10a36 6f 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20  on which.** the 
10a37 64 61 74 61 62 61 73 65 20 69 73 20 73 74 6f 72  database is stor
10a38 65 64 2c 20 74 68 65 6e 20 4f 73 53 79 6e 63 28  ed, then OsSync(
10a39 29 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65  ) is never calle
10a3a 64 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  d on the journal
10a3b 0a 2a 2a 20 66 69 6c 65 2e 20 49 6e 20 74 68 69  .** file. In thi
10a3c 73 20 63 61 73 65 20 61 6c 6c 20 74 68 61 74 20  s case all that 
10a3d 69 73 20 72 65 71 75 69 72 65 64 20 69 73 20 74  is required is t
10a3e 6f 20 75 70 64 61 74 65 20 74 68 65 20 6e 52 65  o update the nRe
10a3f 63 20 66 69 65 6c 64 20 69 6e 0a 2a 2a 20 74 68  c field in.** th
10a40 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  e journal header
10a41 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
10a42 74 69 6e 65 20 63 6c 65 61 72 73 20 74 68 65 20  tine clears the 
10a43 6e 65 65 64 53 79 6e 63 20 66 69 65 6c 64 20 6f  needSync field o
10a44 66 20 65 76 65 72 79 20 70 61 67 65 20 63 75 72  f every page cur
10a45 72 65 6e 74 20 68 65 6c 64 20 69 6e 0a 2a 2a 20  rent held in.** 
10a46 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69  memory..*/.stati
10a47 63 20 69 6e 74 20 73 79 6e 63 4a 6f 75 72 6e 61  c int syncJourna
10a48 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  l(Pager *pPager)
10a49 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
10a4a 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 53 79  ITE_OK;..  /* Sy
10a4b 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 62  nc the journal b
10a4c 65 66 6f 72 65 20 6d 6f 64 69 66 79 69 6e 67 20  efore modifying 
10a4d 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
10a4e 65 0a 20 20 2a 2a 20 28 61 73 73 75 6d 69 6e 67  e.  ** (assuming
10a4f 20 74 68 65 72 65 20 69 73 20 61 20 6a 6f 75 72   there is a jour
10a50 6e 61 6c 20 61 6e 64 20 69 74 20 6e 65 65 64 73  nal and it needs
10a51 20 74 6f 20 62 65 20 73 79 6e 63 65 64 2e 29 0a   to be synced.).
10a52 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65    */.  if( pPage
10a53 72 2d 3e 6e 65 65 64 53 79 6e 63 20 29 7b 0a 20  r->needSync ){. 
10a54 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61 67     assert( !pPag
10a55 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a  er->tempFile );.
10a56 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
10a57 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47  journalMode!=PAG
10a58 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d  ER_JOURNALMODE_M
10a59 45 4d 4f 52 59 20 29 7b 0a 20 20 20 20 20 20 69  EMORY ){.      i
10a5a 6e 74 20 69 44 63 20 3d 20 73 71 6c 69 74 65 33  nt iDc = sqlite3
10a5b 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65  OsDeviceCharacte
10a5c 72 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e  ristics(pPager->
10a5d 66 64 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  fd);.      asser
10a5e 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  t( pPager->journ
10a5f 61 6c 4f 70 65 6e 20 29 3b 0a 0a 20 20 20 20 20  alOpen );..     
10a60 20 69 66 28 20 30 3d 3d 28 69 44 63 26 53 51 4c   if( 0==(iDc&SQL
10a61 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41  ITE_IOCAP_SAFE_A
10a62 50 50 45 4e 44 29 20 29 7b 0a 20 20 20 20 20 20  PPEND) ){.      
10a63 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e    /* Write the n
10a64 52 65 63 20 76 61 6c 75 65 20 69 6e 74 6f 20 74  Rec value into t
10a65 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
10a66 68 65 61 64 65 72 2e 20 49 66 20 69 6e 0a 20 20  header. If in.  
10a67 20 20 20 20 20 20 2a 2a 20 66 75 6c 6c 2d 73 79        ** full-sy
10a68 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f 64 65 2c 20  nchronous mode, 
10a69 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c  sync the journal
10a6a 20 66 69 72 73 74 2e 20 54 68 69 73 20 65 6e 73   first. This ens
10a6b 75 72 65 73 20 74 68 61 74 0a 20 20 20 20 20 20  ures that.      
10a6c 20 20 2a 2a 20 61 6c 6c 20 64 61 74 61 20 68 61    ** all data ha
10a6d 73 20 72 65 61 6c 6c 79 20 68 69 74 20 74 68 65  s really hit the
10a6e 20 64 69 73 6b 20 62 65 66 6f 72 65 20 6e 52 65   disk before nRe
10a6f 63 20 69 73 20 75 70 64 61 74 65 64 20 74 6f 20  c is updated to 
10a70 6d 61 72 6b 0a 20 20 20 20 20 20 20 20 2a 2a 20  mark.        ** 
10a71 69 74 20 61 73 20 61 20 63 61 6e 64 69 64 61 74  it as a candidat
10a72 65 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a  e for rollback..
10a73 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
10a74 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f     ** This is no
10a75 74 20 72 65 71 75 69 72 65 64 20 69 66 20 74 68  t required if th
10a76 65 20 70 65 72 73 69 73 74 65 6e 74 20 6d 65 64  e persistent med
10a77 69 61 20 73 75 70 70 6f 72 74 73 20 74 68 65 0a  ia supports the.
10a78 20 20 20 20 20 20 20 20 2a 2a 20 53 41 46 45 5f          ** SAFE_
10a79 41 50 50 45 4e 44 20 70 72 6f 70 65 72 74 79 2e  APPEND property.
10a7a 20 42 65 63 61 75 73 65 20 69 6e 20 74 68 69 73   Because in this
10a7b 20 63 61 73 65 20 69 74 20 69 73 20 6e 6f 74 20   case it is not 
10a7c 70 6f 73 73 69 62 6c 65 20 0a 20 20 20 20 20 20  possible .      
10a7d 20 20 2a 2a 20 66 6f 72 20 67 61 72 62 61 67 65    ** for garbage
10a7e 20 64 61 74 61 20 74 6f 20 62 65 20 61 70 70 65   data to be appe
10a7f 6e 64 65 64 20 74 6f 20 74 68 65 20 66 69 6c 65  nded to the file
10a80 2c 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64  , the nRec field
10a81 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 70  .        ** is p
10a82 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 30 78  opulated with 0x
10a83 46 46 46 46 46 46 46 46 20 77 68 65 6e 20 74 68  FFFFFFFF when th
10a84 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  e journal header
10a85 20 69 73 20 77 72 69 74 74 65 6e 0a 20 20 20 20   is written.    
10a86 20 20 20 20 2a 2a 20 61 6e 64 20 6e 65 76 65 72      ** and never
10a87 20 6e 65 65 64 73 20 74 6f 20 62 65 20 75 70 64   needs to be upd
10a88 61 74 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f  ated..        */
10a89 0a 20 20 20 20 20 20 20 20 69 36 34 20 6a 72 6e  .        i64 jrn
10a8a 6c 4f 66 66 3b 0a 20 20 20 20 20 20 20 20 69 66  lOff;.        if
10a8b 28 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79  ( pPager->fullSy
10a8c 6e 63 20 26 26 20 30 3d 3d 28 69 44 63 26 53 51  nc && 0==(iDc&SQ
10a8d 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45  LITE_IOCAP_SEQUE
10a8e 4e 54 49 41 4c 29 20 29 7b 0a 20 20 20 20 20 20  NTIAL) ){.      
10a8f 20 20 20 20 50 41 47 45 52 54 52 41 43 45 32 28      PAGERTRACE2(
10a90 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20 6f 66  "SYNC journal of
10a91 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28   %d\n", PAGERID(
10a92 70 50 61 67 65 72 29 29 3b 0a 20 20 20 20 20 20  pPager));.      
10a93 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 53      IOTRACE(("JS
10a94 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 65  YNC %p\n", pPage
10a95 72 29 29 0a 20 20 20 20 20 20 20 20 20 20 72 63  r)).          rc
10a96 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63   = sqlite3OsSync
10a97 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50  (pPager->jfd, pP
10a98 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73  ager->sync_flags
10a99 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
10a9a 20 72 63 21 3d 30 20 29 20 72 65 74 75 72 6e 20   rc!=0 ) return 
10a9b 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20  rc;.        }.. 
10a9c 20 20 20 20 20 20 20 6a 72 6e 6c 4f 66 66 20 3d         jrnlOff =
10a9d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
10a9e 48 64 72 20 2b 20 73 69 7a 65 6f 66 28 61 4a 6f  Hdr + sizeof(aJo
10a9f 75 72 6e 61 6c 4d 61 67 69 63 29 3b 0a 20 20 20  urnalMagic);.   
10aa0 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a       IOTRACE(("J
10aa1 48 44 52 20 25 70 20 25 6c 6c 64 20 25 64 5c 6e  HDR %p %lld %d\n
10aa2 22 2c 20 70 50 61 67 65 72 2c 20 6a 72 6e 6c 4f  ", pPager, jrnlO
10aa3 66 66 2c 20 34 29 29 3b 0a 20 20 20 20 20 20 20  ff, 4));.       
10aa4 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74   rc = write32bit
10aa5 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 6a  s(pPager->jfd, j
10aa6 72 6e 6c 4f 66 66 2c 20 70 50 61 67 65 72 2d 3e  rnlOff, pPager->
10aa7 6e 52 65 63 29 3b 0a 20 20 20 20 20 20 20 20 69  nRec);.        i
10aa8 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
10aa9 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  c;.      }.     
10aaa 20 69 66 28 20 30 3d 3d 28 69 44 63 26 53 51 4c   if( 0==(iDc&SQL
10aab 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e  ITE_IOCAP_SEQUEN
10aac 54 49 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 20  TIAL) ){.       
10aad 20 50 41 47 45 52 54 52 41 43 45 32 28 22 53 59   PAGERTRACE2("SY
10aae 4e 43 20 6a 6f 75 72 6e 61 6c 20 6f 66 20 25 64  NC journal of %d
10aaf 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61  \n", PAGERID(pPa
10ab0 67 65 72 29 29 3b 0a 20 20 20 20 20 20 20 20 49  ger));.        I
10ab1 4f 54 52 41 43 45 28 28 22 4a 53 59 4e 43 20 25  OTRACE(("JSYNC %
10ab2 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20  p\n", pPager)). 
10ab3 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
10ab4 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65 72  te3OsSync(pPager
10ab5 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 73  ->jfd, pPager->s
10ab6 79 6e 63 5f 66 6c 61 67 73 7c 20 0a 20 20 20 20  ync_flags| .    
10ab7 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e 73        (pPager->s
10ab8 79 6e 63 5f 66 6c 61 67 73 3d 3d 53 51 4c 49 54  ync_flags==SQLIT
10ab9 45 5f 53 59 4e 43 5f 46 55 4c 4c 3f 53 51 4c 49  E_SYNC_FULL?SQLI
10aba 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59  TE_SYNC_DATAONLY
10abb 3a 30 29 0a 20 20 20 20 20 20 20 20 29 3b 0a 20  :0).        );. 
10abc 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 30         if( rc!=0
10abd 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
10abe 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67      }.      pPag
10abf 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74  er->journalStart
10ac0 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  ed = 1;.    }.  
10ac1 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79    pPager->needSy
10ac2 6e 63 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20  nc = 0;..    /* 
10ac3 45 72 61 73 65 20 74 68 65 20 6e 65 65 64 53 79  Erase the needSy
10ac4 6e 63 20 66 6c 61 67 20 66 72 6f 6d 20 65 76 65  nc flag from eve
10ac5 72 79 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a  ry page..    */.
10ac6 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68      sqlite3Pcach
10ac7 65 43 6c 65 61 72 46 6c 61 67 73 28 70 50 61 67  eClearFlags(pPag
10ac8 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 50 47 48  er->pPCache, PGH
10ac9 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3b 0a 20  DR_NEED_SYNC);. 
10aca 20 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42   }..#ifndef NDEB
10acb 55 47 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 50  UG.  /* If the P
10acc 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c  ager.needSync fl
10acd 61 67 20 69 73 20 63 6c 65 61 72 20 74 68 65 6e  ag is clear then
10ace 20 74 68 65 20 50 67 48 64 72 2e 6e 65 65 64 53   the PgHdr.needS
10acf 79 6e 63 0a 20 20 2a 2a 20 66 6c 61 67 20 6d 75  ync.  ** flag mu
10ad0 73 74 20 61 6c 73 6f 20 62 65 20 63 6c 65 61 72  st also be clear
10ad1 20 66 6f 72 20 61 6c 6c 20 70 61 67 65 73 2e 20   for all pages. 
10ad2 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 69   Verify that thi
10ad3 73 0a 20 20 2a 2a 20 69 6e 76 61 72 69 61 6e 74  s.  ** invariant
10ad4 20 69 73 20 74 72 75 65 2e 0a 20 20 2a 2f 0a 20   is true..  */. 
10ad5 20 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74   else{.    sqlit
10ad6 65 33 50 63 61 63 68 65 41 73 73 65 72 74 46 6c  e3PcacheAssertFl
10ad7 61 67 73 28 70 50 61 67 65 72 2d 3e 70 50 43 61  ags(pPager->pPCa
10ad8 63 68 65 2c 20 30 2c 20 50 47 48 44 52 5f 4e 45  che, 0, PGHDR_NE
10ad9 45 44 5f 53 59 4e 43 29 3b 0a 20 20 7d 0a 23 65  ED_SYNC);.  }.#e
10ada 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 72  ndif..  return r
10adb 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65  c;.}../*.** Give
10adc 6e 20 61 20 6c 69 73 74 20 6f 66 20 70 61 67 65  n a list of page
10add 73 20 28 63 6f 6e 6e 65 63 74 65 64 20 62 79 20  s (connected by 
10ade 74 68 65 20 50 67 48 64 72 2e 70 44 69 72 74 79  the PgHdr.pDirty
10adf 20 70 6f 69 6e 74 65 72 29 20 77 72 69 74 65 0a   pointer) write.
10ae0 2a 2a 20 65 76 65 72 79 20 6f 6e 65 20 6f 66 20  ** every one of 
10ae1 74 68 6f 73 65 20 70 61 67 65 73 20 6f 75 74 20  those pages out 
10ae2 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
10ae3 66 69 6c 65 2e 20 4e 6f 20 63 61 6c 6c 73 20 61  file. No calls a
10ae4 72 65 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 74 68  re made.** to th
10ae5 65 20 70 61 67 65 2d 63 61 63 68 65 20 74 6f 20  e page-cache to 
10ae6 6d 61 72 6b 20 74 68 65 20 70 61 67 65 73 20 61  mark the pages a
10ae7 73 20 63 6c 65 61 6e 2e 20 49 74 20 69 73 20 74  s clean. It is t
10ae8 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74  he responsibilit
10ae9 79 0a 2a 2a 20 6f 66 20 74 68 65 20 63 61 6c 6c  y.** of the call
10aea 65 72 20 74 6f 20 75 73 65 20 50 63 61 63 68 65  er to use Pcache
10aeb 43 6c 65 61 6e 41 6c 6c 28 29 20 6f 72 20 50 63  CleanAll() or Pc
10aec 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 29 20  acheMakeClean() 
10aed 74 6f 20 6d 61 72 6b 0a 2a 2a 20 74 68 65 20 70  to mark.** the p
10aee 61 67 65 73 20 61 73 20 63 6c 65 61 6e 2e 0a 2a  ages as clean..*
10aef 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
10af0 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73  er_write_pagelis
10af1 74 28 50 67 48 64 72 20 2a 70 4c 69 73 74 29 7b  t(PgHdr *pList){
10af2 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
10af3 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 69  ;.  int rc;..  i
10af4 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65  f( pList==0 ) re
10af5 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
10af6 20 20 70 50 61 67 65 72 20 3d 20 70 4c 69 73 74    pPager = pList
10af7 2d 3e 70 50 61 67 65 72 3b 0a 0a 20 20 2f 2a 20  ->pPager;..  /* 
10af8 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68  At this point th
10af9 65 72 65 20 6d 61 79 20 62 65 20 65 69 74 68 65  ere may be eithe
10afa 72 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20  r a RESERVED or 
10afb 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f  EXCLUSIVE lock o
10afc 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62  n the.  ** datab
10afd 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65  ase file. If the
10afe 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 6e  re is already an
10aff 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2c   EXCLUSIVE lock,
10b00 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20   the following. 
10b01 20 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c   ** calls to sql
10b02 69 74 65 33 4f 73 4c 6f 63 6b 28 29 20 61 72 65  ite3OsLock() are
10b03 20 6e 6f 2d 6f 70 73 2e 0a 20 20 2a 2a 0a 20 20   no-ops..  **.  
10b04 2a 2a 20 4d 6f 76 69 6e 67 20 74 68 65 20 6c 6f  ** Moving the lo
10b05 63 6b 20 66 72 6f 6d 20 52 45 53 45 52 56 45 44  ck from RESERVED
10b06 20 74 6f 20 45 58 43 4c 55 53 49 56 45 20 61 63   to EXCLUSIVE ac
10b07 74 75 61 6c 6c 79 20 69 6e 76 6f 6c 76 65 73 20  tually involves 
10b08 67 6f 69 6e 67 0a 20 20 2a 2a 20 74 68 72 6f 75  going.  ** throu
10b09 67 68 20 61 6e 20 69 6e 74 65 72 6d 65 64 69 61  gh an intermedia
10b0a 74 65 20 73 74 61 74 65 20 50 45 4e 44 49 4e 47  te state PENDING
10b0b 2e 20 20 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f  .   A PENDING lo
10b0c 63 6b 20 70 72 65 76 65 6e 74 73 20 6e 65 77 0a  ck prevents new.
10b0d 20 20 2a 2a 20 72 65 61 64 65 72 73 20 66 72 6f    ** readers fro
10b0e 6d 20 61 74 74 61 63 68 69 6e 67 20 74 6f 20 74  m attaching to t
10b0f 68 65 20 64 61 74 61 62 61 73 65 20 62 75 74 20  he database but 
10b10 69 73 20 75 6e 73 75 66 66 69 63 69 65 6e 74 20  is unsufficient 
10b11 66 6f 72 20 75 73 20 74 6f 0a 20 20 2a 2a 20 77  for us to.  ** w
10b12 72 69 74 65 2e 20 20 54 68 65 20 69 64 65 61 20  rite.  The idea 
10b13 6f 66 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  of a PENDING loc
10b14 6b 20 69 73 20 74 6f 20 70 72 65 76 65 6e 74 20  k is to prevent 
10b15 6e 65 77 20 72 65 61 64 65 72 73 20 66 72 6f 6d  new readers from
10b16 0a 20 20 2a 2a 20 63 6f 6d 69 6e 67 20 69 6e 20  .  ** coming in 
10b17 77 68 69 6c 65 20 77 65 20 77 61 69 74 20 66 6f  while we wait fo
10b18 72 20 65 78 69 73 74 69 6e 67 20 72 65 61 64 65  r existing reade
10b19 72 73 20 74 6f 20 63 6c 65 61 72 2e 0a 20 20 2a  rs to clear..  *
10b1a 2a 0a 20 20 2a 2a 20 57 68 69 6c 65 20 74 68 65  *.  ** While the
10b1b 20 70 61 67 65 72 20 69 73 20 69 6e 20 74 68 65   pager is in the
10b1c 20 52 45 53 45 52 56 45 44 20 73 74 61 74 65 2c   RESERVED state,
10b1d 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61   the original da
10b1e 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20 2a 2a  tabase file.  **
10b1f 20 69 73 20 75 6e 63 68 61 6e 67 65 64 20 61 6e   is unchanged an
10b20 64 20 77 65 20 63 61 6e 20 72 6f 6c 6c 62 61 63  d we can rollbac
10b21 6b 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67  k without having
10b22 20 74 6f 20 70 6c 61 79 62 61 63 6b 20 74 68 65   to playback the
10b23 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 69 6e  .  ** journal in
10b24 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  to the original 
10b25 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
10b26 4f 6e 63 65 20 77 65 20 74 72 61 6e 73 69 74 69  Once we transiti
10b27 6f 6e 20 74 6f 0a 20 20 2a 2a 20 45 58 43 4c 55  on to.  ** EXCLU
10b28 53 49 56 45 2c 20 69 74 20 6d 65 61 6e 73 20 74  SIVE, it means t
10b29 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
10b2a 20 68 61 73 20 62 65 65 6e 20 63 68 61 6e 67 65   has been change
10b2b 64 20 61 6e 64 20 61 6e 79 20 72 6f 6c 6c 62 61  d and any rollba
10b2c 63 6b 0a 20 20 2a 2a 20 77 69 6c 6c 20 72 65 71  ck.  ** will req
10b2d 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c 20 70  uire a journal p
10b2e 6c 61 79 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20  layback..  */.  
10b2f 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74 5f  rc = pager_wait_
10b30 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20  on_lock(pPager, 
10b31 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b  EXCLUSIVE_LOCK);
10b32 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
10b33 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
10b34 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 77 68  rn rc;.  }..  wh
10b35 69 6c 65 28 20 70 4c 69 73 74 20 29 7b 0a 0a 20  ile( pList ){.. 
10b36 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c     /* If the fil
10b37 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65  e has not yet be
10b38 65 6e 20 6f 70 65 6e 65 64 2c 20 6f 70 65 6e 20  en opened, open 
10b39 69 74 20 6e 6f 77 2e 20 2a 2f 0a 20 20 20 20 69  it now. */.    i
10b3a 66 28 20 21 70 50 61 67 65 72 2d 3e 66 64 2d 3e  f( !pPager->fd->
10b3b 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20  pMethods ){.    
10b3c 20 20 61 73 73 65 72 74 28 70 50 61 67 65 72 2d    assert(pPager-
10b3d 3e 74 65 6d 70 46 69 6c 65 29 3b 0a 20 20 20 20  >tempFile);.    
10b3e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
10b3f 67 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 67  gerOpentemp(pPag
10b40 65 72 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c 20  er, pPager->fd, 
10b41 70 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 67 73  pPager->vfsFlags
10b42 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20  );.      if( rc 
10b43 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
10b44 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   }..    /* If th
10b45 65 72 65 20 61 72 65 20 64 69 72 74 79 20 70 61  ere are dirty pa
10b46 67 65 73 20 69 6e 20 74 68 65 20 70 61 67 65 20  ges in the page 
10b47 63 61 63 68 65 20 77 69 74 68 20 70 61 67 65 20  cache with page 
10b48 6e 75 6d 62 65 72 73 20 67 72 65 61 74 65 72 0a  numbers greater.
10b49 20 20 20 20 2a 2a 20 74 68 61 6e 20 50 61 67 65      ** than Page
10b4a 72 2e 64 62 53 69 7a 65 2c 20 74 68 69 73 20 6d  r.dbSize, this m
10b4b 65 61 6e 73 20 73 71 6c 69 74 65 33 50 61 67 65  eans sqlite3Page
10b4c 72 54 72 75 6e 63 61 74 65 28 29 20 77 61 73 20  rTruncate() was 
10b4d 63 61 6c 6c 65 64 20 74 6f 0a 20 20 20 20 2a 2a  called to.    **
10b4e 20 6d 61 6b 65 20 74 68 65 20 66 69 6c 65 20 73   make the file s
10b4f 6d 61 6c 6c 65 72 20 28 70 72 65 73 75 6d 61 62  maller (presumab
10b50 6c 79 20 62 79 20 61 75 74 6f 2d 76 61 63 75 75  ly by auto-vacuu
10b51 6d 20 63 6f 64 65 29 2e 20 44 6f 20 6e 6f 74 20  m code). Do not 
10b52 77 72 69 74 65 0a 20 20 20 20 2a 2a 20 61 6e 79  write.    ** any
10b53 20 73 75 63 68 20 70 61 67 65 73 20 74 6f 20 74   such pages to t
10b54 68 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a  he file..    */.
10b55 20 20 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 70      if( pList->p
10b56 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 53  gno<=pPager->dbS
10b57 69 7a 65 20 26 26 20 30 3d 3d 28 70 4c 69 73 74  ize && 0==(pList
10b58 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 4f  ->flags&PGHDR_DO
10b59 4e 54 5f 57 52 49 54 45 29 20 29 7b 0a 20 20 20  NT_WRITE) ){.   
10b5a 20 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20     i64 offset = 
10b5b 28 70 4c 69 73 74 2d 3e 70 67 6e 6f 2d 31 29 2a  (pList->pgno-1)*
10b5c 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67  (i64)pPager->pag
10b5d 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 63 68 61  eSize;.      cha
10b5e 72 20 2a 70 44 61 74 61 20 3d 20 43 4f 44 45 43  r *pData = CODEC
10b5f 32 28 70 50 61 67 65 72 2c 20 70 4c 69 73 74 2d  2(pPager, pList-
10b60 3e 70 44 61 74 61 2c 20 70 4c 69 73 74 2d 3e 70  >pData, pList->p
10b61 67 6e 6f 2c 20 36 29 3b 0a 20 20 20 20 20 20 50  gno, 6);.      P
10b62 41 47 45 52 54 52 41 43 45 34 28 22 53 54 4f 52  AGERTRACE4("STOR
10b63 45 20 25 64 20 70 61 67 65 20 25 64 20 68 61 73  E %d page %d has
10b64 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20  h(%08x)\n",.    
10b65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50                 P
10b66 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20  AGERID(pPager), 
10b67 70 4c 69 73 74 2d 3e 70 67 6e 6f 2c 20 70 61 67  pList->pgno, pag
10b68 65 72 5f 70 61 67 65 68 61 73 68 28 70 4c 69 73  er_pagehash(pLis
10b69 74 29 29 3b 0a 20 20 20 20 20 20 49 4f 54 52 41  t));.      IOTRA
10b6a 43 45 28 28 22 50 47 4f 55 54 20 25 70 20 25 64  CE(("PGOUT %p %d
10b6b 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 4c 69  \n", pPager, pLi
10b6c 73 74 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20  st->pgno));.    
10b6d 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
10b6e 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64  Write(pPager->fd
10b6f 2c 20 70 44 61 74 61 2c 20 70 50 61 67 65 72 2d  , pData, pPager-
10b70 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 65  >pageSize, offse
10b71 74 29 3b 0a 20 20 20 20 20 20 50 41 47 45 52 5f  t);.      PAGER_
10b72 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67  INCR(sqlite3_pag
10b73 65 72 5f 77 72 69 74 65 64 62 5f 63 6f 75 6e 74  er_writedb_count
10b74 29 3b 0a 20 20 20 20 20 20 50 41 47 45 52 5f 49  );.      PAGER_I
10b75 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 57 72 69  NCR(pPager->nWri
10b76 74 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  te);.      if( p
10b77 4c 69 73 74 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b  List->pgno==1 ){
10b78 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
10b79 26 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56  &pPager->dbFileV
10b7a 65 72 73 2c 20 26 70 44 61 74 61 5b 32 34 5d 2c  ers, &pData[24],
10b7b 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e   sizeof(pPager->
10b7c 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20  dbFileVers));.  
10b7d 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 6e      }.    }.#ifn
10b7e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 65  def NDEBUG.    e
10b7f 6c 73 65 7b 0a 20 20 20 20 20 20 50 41 47 45 52  lse{.      PAGER
10b80 54 52 41 43 45 33 28 22 4e 4f 53 54 4f 52 45 20  TRACE3("NOSTORE 
10b81 25 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50  %d page %d\n", P
10b82 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20  AGERID(pPager), 
10b83 70 4c 69 73 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20  pList->pgno);.  
10b84 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 69    }.#endif.    i
10b85 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
10b86 63 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  c;.#ifdef SQLITE
10b87 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20  _CHECK_PAGES.   
10b88 20 70 4c 69 73 74 2d 3e 70 61 67 65 48 61 73 68   pList->pageHash
10b89 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73   = pager_pagehas
10b8a 68 28 70 4c 69 73 74 29 3b 0a 23 65 6e 64 69 66  h(pList);.#endif
10b8b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 70 4c 69  .    pList = pLi
10b8c 73 74 2d 3e 70 44 69 72 74 79 3b 0a 20 20 7d 0a  st->pDirty;.  }.
10b8d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
10b8e 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  _OK;.}../*.** Th
10b8f 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
10b90 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 63 61  alled by the pca
10b91 63 68 65 20 6c 61 79 65 72 20 77 68 65 6e 20 69  che layer when i
10b92 74 20 68 61 73 20 72 65 61 63 68 65 64 20 73 6f  t has reached so
10b93 6d 65 0a 2a 2a 20 73 6f 66 74 20 6d 65 6d 6f 72  me.** soft memor
10b94 79 20 6c 69 6d 69 74 2e 20 54 68 65 20 61 72 67  y limit. The arg
10b95 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74  ument is a point
10b96 65 72 20 74 6f 20 61 20 70 75 72 67 65 61 62 6c  er to a purgeabl
10b97 65 20 50 61 67 65 72 20 0a 2a 2a 20 6f 62 6a 65  e Pager .** obje
10b98 63 74 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ct. This functio
10b99 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6d 61  n attempts to ma
10b9a 6b 65 20 61 20 73 69 6e 67 6c 65 20 64 69 72 74  ke a single dirt
10b9b 79 20 70 61 67 65 20 74 68 61 74 20 68 61 73 20  y page that has 
10b9c 6e 6f 0a 2a 2a 20 6f 75 74 73 74 61 6e 64 69 6e  no.** outstandin
10b9d 67 20 72 65 66 65 72 65 6e 63 65 73 20 28 69 66  g references (if
10b9e 20 6f 6e 65 20 65 78 69 73 74 73 29 20 63 6c 65   one exists) cle
10b9f 61 6e 20 73 6f 20 74 68 61 74 20 69 74 20 63 61  an so that it ca
10ba0 6e 20 62 65 20 72 65 63 79 63 6c 65 64 20 0a 2a  n be recycled .*
10ba1 2a 20 62 79 20 74 68 65 20 70 63 61 63 68 65 20  * by the pcache 
10ba2 6c 61 79 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  layer..*/.static
10ba3 20 69 6e 74 20 70 61 67 65 72 53 74 72 65 73 73   int pagerStress
10ba4 28 76 6f 69 64 20 2a 70 2c 20 50 67 48 64 72 20  (void *p, PgHdr 
10ba5 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a  *pPg){.  Pager *
10ba6 70 50 61 67 65 72 20 3d 20 28 50 61 67 65 72 20  pPager = (Pager 
10ba7 2a 29 70 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  *)p;.  int rc = 
10ba8 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66  SQLITE_OK;..  if
10ba9 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53  ( pPager->doNotS
10baa 79 6e 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ync ){.    retur
10bab 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
10bac 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d  ..  assert( pPg-
10bad 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52  >flags&PGHDR_DIR
10bae 54 59 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67  TY );.  if( pPag
10baf 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c  er->errCode==SQL
10bb0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66  ITE_OK ){.    if
10bb1 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48  ( pPg->flags&PGH
10bb2 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a  DR_NEED_SYNC ){.
10bb3 20 20 20 20 20 20 72 63 20 3d 20 73 79 6e 63 4a        rc = syncJ
10bb4 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a  ournal(pPager);.
10bb5 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
10bb6 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65  LITE_OK && pPage
10bb7 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 26 26 20 0a  r->fullSync && .
10bb8 20 20 20 20 20 20 20 20 21 28 70 50 61 67 65 72          !(pPager
10bb9 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50  ->journalMode==P
10bba 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
10bbb 5f 4d 45 4d 4f 52 59 29 20 26 26 0a 20 20 20 20  _MEMORY) &&.    
10bbc 20 20 20 20 21 28 73 71 6c 69 74 65 33 4f 73 44      !(sqlite3OsD
10bbd 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73  eviceCharacteris
10bbe 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29  tics(pPager->fd)
10bbf 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41  &SQLITE_IOCAP_SA
10bc0 46 45 5f 41 50 50 45 4e 44 29 0a 20 20 20 20 20  FE_APPEND).     
10bc1 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61 67   ){.        pPag
10bc2 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20  er->nRec = 0;.  
10bc3 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65        rc = write
10bc4 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65  JournalHdr(pPage
10bc5 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  r);.      }.    
10bc6 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  }.    if( rc==SQ
10bc7 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
10bc8 20 70 50 67 2d 3e 70 44 69 72 74 79 20 3d 20 30   pPg->pDirty = 0
10bc9 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67  ;.      rc = pag
10bca 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73  er_write_pagelis
10bcb 74 28 70 50 67 29 3b 0a 20 20 20 20 7d 0a 20 20  t(pPg);.    }.  
10bcc 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
10bcd 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 61 67  _OK ){.      pag
10bce 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c  er_error(pPager,
10bcf 20 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   rc);.    }.  }.
10bd0 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
10bd1 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69  E_OK ){.    sqli
10bd2 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65  te3PcacheMakeCle
10bd3 61 6e 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 72  an(pPg);.  }.  r
10bd4 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
10bd5 0a 2a 2a 20 52 65 74 75 72 6e 20 31 20 69 66 20  .** Return 1 if 
10bd6 74 68 65 72 65 20 69 73 20 61 20 68 6f 74 20 6a  there is a hot j
10bd7 6f 75 72 6e 61 6c 20 6f 6e 20 74 68 65 20 67 69  ournal on the gi
10bd8 76 65 6e 20 70 61 67 65 72 2e 0a 2a 2a 20 41 20  ven pager..** A 
10bd9 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6f  hot journal is o
10bda 6e 65 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f  ne that needs to
10bdb 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b 2e   be played back.
10bdc 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 75  .**.** If the cu
10bdd 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 74 68  rrent size of th
10bde 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
10bdf 69 73 20 30 20 62 75 74 20 61 20 6a 6f 75 72 6e  is 0 but a journ
10be0 61 6c 20 66 69 6c 65 0a 2a 2a 20 65 78 69 73 74  al file.** exist
10be1 73 2c 20 74 68 61 74 20 69 73 20 70 72 6f 62 61  s, that is proba
10be2 62 6c 79 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e  bly an old journ
10be3 61 6c 20 6c 65 66 74 20 6f 76 65 72 20 66 72 6f  al left over fro
10be4 6d 20 61 20 70 72 69 6f 72 0a 2a 2a 20 64 61 74  m a prior.** dat
10be5 61 62 61 73 65 20 77 69 74 68 20 74 68 65 20 73  abase with the s
10be6 61 6d 65 20 6e 61 6d 65 2e 20 20 4a 75 73 74 20  ame name.  Just 
10be7 64 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e  delete the journ
10be8 61 6c 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  al..**.** Return
10be9 20 6e 65 67 61 74 69 76 65 20 69 66 20 75 6e 61   negative if una
10bea 62 6c 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  ble to determine
10beb 20 74 68 65 20 73 74 61 74 75 73 20 6f 66 20 74   the status of t
10bec 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a  he journal..**.*
10bed 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64  * This routine d
10bee 6f 65 73 20 6e 6f 74 20 6f 70 65 6e 20 74 68 65  oes not open the
10bef 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f   journal file to
10bf0 20 65 78 61 6d 69 6e 65 20 69 74 73 0a 2a 2a 20   examine its.** 
10bf1 63 6f 6e 74 65 6e 74 2e 20 20 48 65 6e 63 65 2c  content.  Hence,
10bf2 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67   the journal mig
10bf3 68 74 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e  ht contain the n
10bf4 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 72 0a  ame of a master.
10bf5 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ** journal file 
10bf6 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 64 65  that has been de
10bf7 6c 65 74 65 64 2c 20 61 6e 64 20 68 65 6e 63 65  leted, and hence
10bf8 20 6e 6f 74 20 62 65 20 68 6f 74 2e 20 20 4f 72   not be hot.  Or
10bf9 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72 20 6f  .** the header o
10bfa 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69  f the journal mi
10bfb 67 68 74 20 62 65 20 7a 65 72 6f 65 64 20 6f 75  ght be zeroed ou
10bfc 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  t.  This routine
10bfd 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 64 69 73  .** does not dis
10bfe 63 6f 76 65 72 20 74 68 65 73 65 20 63 61 73 65  cover these case
10bff 73 20 6f 66 20 61 20 6e 6f 6e 2d 68 6f 74 20 6a  s of a non-hot j
10c00 6f 75 72 6e 61 6c 20 2d 20 69 66 20 74 68 65 0a  ournal - if the.
10c01 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ** journal file 
10c02 65 78 69 73 74 73 20 61 6e 64 20 69 73 20 6e 6f  exists and is no
10c03 74 20 65 6d 70 74 79 20 74 68 69 73 20 72 6f 75  t empty this rou
10c04 74 69 6e 65 20 61 73 73 75 6d 65 73 20 69 74 0a  tine assumes it.
10c05 2a 2a 20 69 73 20 68 6f 74 2e 20 20 54 68 65 20  ** is hot.  The 
10c06 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29  pager_playback()
10c07 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64 69   routine will di
10c08 73 63 6f 76 65 72 20 74 68 61 74 20 74 68 65 0a  scover that the.
10c09 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ** journal file 
10c0a 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 6f  is not really ho
10c0b 74 20 61 6e 64 20 77 69 6c 6c 20 6e 6f 2d 6f 70  t and will no-op
10c0c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
10c0d 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c 28 50 61  hasHotJournal(Pa
10c0e 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
10c0f 20 2a 70 45 78 69 73 74 73 29 7b 0a 20 20 73 71   *pExists){.  sq
10c10 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20  lite3_vfs *pVfs 
10c11 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a  = pPager->pVfs;.
10c12 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
10c13 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 65 78 69 73  E_OK;.  int exis
10c14 74 73 3b 0a 20 20 69 6e 74 20 6c 6f 63 6b 65 64  ts;.  int locked
10c15 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
10c16 65 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  er!=0 );.  asser
10c17 74 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f  t( pPager->useJo
10c18 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 72  urnal );.  asser
10c19 74 28 20 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70  t( pPager->fd->p
10c1a 4d 65 74 68 6f 64 73 20 29 3b 0a 20 20 2a 70 45  Methods );.  *pE
10c1b 78 69 73 74 73 20 3d 20 30 3b 0a 20 20 72 63 20  xists = 0;.  rc 
10c1c 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73  = sqlite3OsAcces
10c1d 73 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e  s(pVfs, pPager->
10c1e 7a 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c 49 54 45  zJournal, SQLITE
10c1f 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20  _ACCESS_EXISTS, 
10c20 26 65 78 69 73 74 73 29 3b 0a 20 20 69 66 28 20  &exists);.  if( 
10c21 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
10c22 20 65 78 69 73 74 73 20 29 7b 0a 20 20 20 20 72   exists ){.    r
10c23 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 43 68 65  c = sqlite3OsChe
10c24 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 70  ckReservedLock(p
10c25 50 61 67 65 72 2d 3e 66 64 2c 20 26 6c 6f 63 6b  Pager->fd, &lock
10c26 65 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  ed);.  }.  if( r
10c27 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
10c28 65 78 69 73 74 73 20 26 26 20 21 6c 6f 63 6b 65  exists && !locke
10c29 64 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 50 61  d ){.    int nPa
10c2a 67 65 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  ge;.    rc = sql
10c2b 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75  ite3PagerPagecou
10c2c 6e 74 28 70 50 61 67 65 72 2c 20 26 6e 50 61 67  nt(pPager, &nPag
10c2d 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  e);.    if( rc==
10c2e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
10c2f 20 20 69 66 28 20 6e 50 61 67 65 3d 3d 30 20 29    if( nPage==0 )
10c30 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
10c31 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20  3OsDelete(pVfs, 
10c32 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c  pPager->zJournal
10c33 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  , 0);.      }els
10c34 65 7b 0a 20 20 20 20 20 20 20 20 2a 70 45 78 69  e{.        *pExi
10c35 73 74 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  sts = 1;.      }
10c36 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
10c37 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
10c38 20 52 65 61 64 20 74 68 65 20 63 6f 6e 74 65 6e   Read the conten
10c39 74 20 6f 66 20 70 61 67 65 20 70 50 67 20 6f 75  t of page pPg ou
10c3a 74 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  t of the databas
10c3b 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  e file..*/.stati
10c3c 63 20 69 6e 74 20 72 65 61 64 44 62 50 61 67 65  c int readDbPage
10c3d 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
10c3e 50 67 48 64 72 20 2a 70 50 67 2c 20 50 67 6e 6f  PgHdr *pPg, Pgno
10c3f 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 72 63   pgno){.  int rc
10c40 3b 0a 20 20 69 36 34 20 6f 66 66 73 65 74 3b 0a  ;.  i64 offset;.
10c41 20 20 61 73 73 65 72 74 28 20 4d 45 4d 44 42 3d    assert( MEMDB=
10c42 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 70  =0 );.  assert(p
10c43 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68  Pager->fd->pMeth
10c44 6f 64 73 7c 7c 70 50 61 67 65 72 2d 3e 74 65 6d  ods||pPager->tem
10c45 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 21 70  pFile);.  if( !p
10c46 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68  Pager->fd->pMeth
10c47 6f 64 73 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ods ){.    retur
10c48 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53  n SQLITE_IOERR_S
10c49 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 0a 20  HORT_READ;.  }. 
10c4a 20 6f 66 66 73 65 74 20 3d 20 28 70 67 6e 6f 2d   offset = (pgno-
10c4b 31 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e  1)*(i64)pPager->
10c4c 70 61 67 65 53 69 7a 65 3b 0a 20 20 72 63 20 3d  pageSize;.  rc =
10c4d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70   sqlite3OsRead(p
10c4e 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 67 2d 3e  Pager->fd, pPg->
10c4f 70 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70  pData, pPager->p
10c50 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 65 74 29  ageSize, offset)
10c51 3b 0a 20 20 50 41 47 45 52 5f 49 4e 43 52 28 73  ;.  PAGER_INCR(s
10c52 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 72 65 61  qlite3_pager_rea
10c53 64 64 62 5f 63 6f 75 6e 74 29 3b 0a 20 20 50 41  ddb_count);.  PA
10c54 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d  GER_INCR(pPager-
10c55 3e 6e 52 65 61 64 29 3b 0a 20 20 49 4f 54 52 41  >nRead);.  IOTRA
10c56 43 45 28 28 22 50 47 49 4e 20 25 70 20 25 64 5c  CE(("PGIN %p %d\
10c57 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 67 6e 6f  n", pPager, pgno
10c58 29 29 3b 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d  ));.  if( pgno==
10c59 31 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28  1 ){.    memcpy(
10c5a 26 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56  &pPager->dbFileV
10c5b 65 72 73 2c 20 26 28 28 75 38 2a 29 70 50 67 2d  ers, &((u8*)pPg-
10c5c 3e 70 44 61 74 61 29 5b 32 34 5d 2c 0a 20 20 20  >pData)[24],.   
10c5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10c5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10c5f 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f             sizeo
10c60 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65  f(pPager->dbFile
10c61 56 65 72 73 29 29 3b 0a 20 20 7d 0a 20 20 43 4f  Vers));.  }.  CO
10c62 44 45 43 31 28 70 50 61 67 65 72 2c 20 70 50 67  DEC1(pPager, pPg
10c63 2d 3e 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67  ->pData, pPg->pg
10c64 6e 6f 2c 20 33 29 3b 0a 20 20 50 41 47 45 52 54  no, 3);.  PAGERT
10c65 52 41 43 45 34 28 22 46 45 54 43 48 20 25 64 20  RACE4("FETCH %d 
10c66 70 61 67 65 20 25 64 20 68 61 73 68 28 25 30 38  page %d hash(%08
10c67 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  x)\n",.         
10c68 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50        PAGERID(pP
10c69 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f  ager), pPg->pgno
10c6a 2c 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68  , pager_pagehash
10c6b 28 70 50 67 29 29 3b 0a 20 20 72 65 74 75 72 6e  (pPg));.  return
10c6c 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54   rc;.}.../*.** T
10c6d 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
10c6e 63 61 6c 6c 65 64 20 74 6f 20 6f 62 74 61 69 6e  called to obtain
10c6f 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b   the shared lock
10c70 20 72 65 71 75 69 72 65 64 20 62 65 66 6f 72 65   required before
10c71 0a 2a 2a 20 64 61 74 61 20 6d 61 79 20 62 65 20  .** data may be 
10c72 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 70 61  read from the pa
10c73 67 65 72 20 63 61 63 68 65 2e 20 49 66 20 74 68  ger cache. If th
10c74 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 68 61  e shared lock ha
10c75 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65 65  s already.** bee
10c76 6e 20 6f 62 74 61 69 6e 65 64 2c 20 74 68 69 73  n obtained, this
10c77 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
10c78 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 6d 6d 65  o-op..**.** Imme
10c79 64 69 61 74 65 6c 79 20 61 66 74 65 72 20 6f 62  diately after ob
10c7a 74 61 69 6e 69 6e 67 20 74 68 65 20 73 68 61 72  taining the shar
10c7b 65 64 20 6c 6f 63 6b 20 28 69 66 20 72 65 71 75  ed lock (if requ
10c7c 69 72 65 64 29 2c 20 74 68 69 73 20 66 75 6e 63  ired), this func
10c7d 74 69 6f 6e 0a 2a 2a 20 63 68 65 63 6b 73 20 66  tion.** checks f
10c7e 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c  or a hot-journal
10c7f 20 66 69 6c 65 2e 20 49 66 20 6f 6e 65 20 69 73   file. If one is
10c80 20 66 6f 75 6e 64 2c 20 61 6e 20 65 6d 65 72 67   found, an emerg
10c81 65 6e 63 79 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a  ency rollback.**
10c82 20 69 73 20 70 65 72 66 6f 72 6d 65 64 20 69 6d   is performed im
10c83 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2f 0a 73 74  mediately..*/.st
10c84 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 53 68  atic int pagerSh
10c85 61 72 65 64 4c 6f 63 6b 28 50 61 67 65 72 20 2a  aredLock(Pager *
10c86 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72  pPager){.  int r
10c87 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
10c88 20 69 6e 74 20 69 73 45 72 72 6f 72 52 65 73 65   int isErrorRese
10c89 74 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 49 66 20  t = 0;..  /* If 
10c8a 74 68 69 73 20 64 61 74 61 62 61 73 65 20 69 73  this database is
10c8b 20 6f 70 65 6e 65 64 20 66 6f 72 20 65 78 63 6c   opened for excl
10c8c 75 73 69 76 65 20 61 63 63 65 73 73 2c 20 68 61  usive access, ha
10c8d 73 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67  s no outstanding
10c8e 20 0a 20 20 2a 2a 20 70 61 67 65 20 72 65 66 65   .  ** page refe
10c8f 72 65 6e 63 65 73 20 61 6e 64 20 69 73 20 69 6e  rences and is in
10c90 20 61 6e 20 65 72 72 6f 72 2d 73 74 61 74 65 2c   an error-state,
10c91 20 6e 6f 77 20 69 73 20 74 68 65 20 63 68 61 6e   now is the chan
10c92 63 65 20 74 6f 20 63 6c 65 61 72 0a 20 20 2a 2a  ce to clear.  **
10c93 20 74 68 65 20 65 72 72 6f 72 2e 20 44 69 73 63   the error. Disc
10c94 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ard the contents
10c95 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63 61   of the pager-ca
10c96 63 68 65 20 61 6e 64 20 74 72 65 61 74 20 61 6e  che and treat an
10c97 79 0a 20 20 2a 2a 20 6f 70 65 6e 20 6a 6f 75 72  y.  ** open jour
10c98 6e 61 6c 20 66 69 6c 65 20 61 73 20 61 20 68 6f  nal file as a ho
10c99 74 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2f 0a  t-journal..  */.
10c9a 20 20 69 66 28 20 21 4d 45 4d 44 42 20 26 26 20    if( !MEMDB && 
10c9b 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
10c9c 65 4d 6f 64 65 20 0a 20 20 20 26 26 20 73 71 6c  eMode .   && sql
10c9d 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75  ite3PcacheRefCou
10c9e 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  nt(pPager->pPCac
10c9f 68 65 29 3d 3d 30 20 26 26 20 70 50 61 67 65 72  he)==0 && pPager
10ca0 2d 3e 65 72 72 43 6f 64 65 20 0a 20 20 29 7b 0a  ->errCode .  ){.
10ca1 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
10ca2 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 7b 0a 20  journalOpen ){. 
10ca3 20 20 20 20 20 69 73 45 72 72 6f 72 52 65 73 65       isErrorRese
10ca4 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  t = 1;.    }.   
10ca5 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
10ca6 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
10ca7 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50    pager_reset(pP
10ca8 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  ager);.  }..  /*
10ca9 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73   If the pager is
10caa 20 73 74 69 6c 6c 20 69 6e 20 61 6e 20 65 72 72   still in an err
10cab 6f 72 20 73 74 61 74 65 2c 20 64 6f 20 6e 6f 74  or state, do not
10cac 20 70 72 6f 63 65 65 64 2e 20 54 68 65 20 65 72   proceed. The er
10cad 72 6f 72 20 0a 20 20 2a 2a 20 73 74 61 74 65 20  ror .  ** state 
10cae 77 69 6c 6c 20 62 65 20 63 6c 65 61 72 65 64 20  will be cleared 
10caf 61 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69 6e  at some point in
10cb0 20 74 68 65 20 66 75 74 75 72 65 20 77 68 65 6e   the future when
10cb1 20 61 6c 6c 20 70 61 67 65 20 0a 20 20 2a 2a 20   all page .  ** 
10cb2 72 65 66 65 72 65 6e 63 65 73 20 61 72 65 20 64  references are d
10cb3 72 6f 70 70 65 64 20 61 6e 64 20 74 68 65 20 63  ropped and the c
10cb4 61 63 68 65 20 63 61 6e 20 62 65 20 64 69 73 63  ache can be disc
10cb5 61 72 64 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  arded..  */.  if
10cb6 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  ( pPager->errCod
10cb7 65 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72 72  e && pPager->err
10cb8 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f 46 55 4c  Code!=SQLITE_FUL
10cb9 4c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  L ){.    return 
10cba 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b  pPager->errCode;
10cbb 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67  .  }..  if( pPag
10cbc 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52  er->state==PAGER
10cbd 5f 55 4e 4c 4f 43 4b 20 7c 7c 20 69 73 45 72 72  _UNLOCK || isErr
10cbe 6f 72 52 65 73 65 74 20 29 7b 0a 20 20 20 20 73  orReset ){.    s
10cbf 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
10cc0 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b   = pPager->pVfs;
10cc1 0a 20 20 20 20 69 6e 74 20 69 73 48 6f 74 4a 6f  .    int isHotJo
10cc2 75 72 6e 61 6c 3b 0a 20 20 20 20 61 73 73 65 72  urnal;.    asser
10cc3 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20  t( !MEMDB );.   
10cc4 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
10cc5 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70  PcacheRefCount(p
10cc6 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d  Pager->pPCache)=
10cc7 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 21 70  =0 );.    if( !p
10cc8 50 61 67 65 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63  Pager->noReadloc
10cc9 6b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  k ){.      rc = 
10cca 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f  pager_wait_on_lo
10ccb 63 6b 28 70 50 61 67 65 72 2c 20 53 48 41 52 45  ck(pPager, SHARE
10ccc 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69  D_LOCK);.      i
10ccd 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
10cce 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
10ccf 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
10cd0 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20  e==PAGER_UNLOCK 
10cd1 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
10cd2 6e 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50  n pager_error(pP
10cd3 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20  ager, rc);.     
10cd4 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   }.      assert(
10cd5 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d   pPager->state>=
10cd6 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20  SHARED_LOCK );. 
10cd7 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
10cd8 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65  a journal file e
10cd9 78 69 73 74 73 2c 20 61 6e 64 20 74 68 65 72 65  xists, and there
10cda 20 69 73 20 6e 6f 20 52 45 53 45 52 56 45 44 20   is no RESERVED 
10cdb 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 20 20  lock on the.    
10cdc 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
10cdd 2c 20 74 68 65 6e 20 69 74 20 65 69 74 68 65 72  , then it either
10cde 20 6e 65 65 64 73 20 74 6f 20 62 65 20 70 6c 61   needs to be pla
10cdf 79 65 64 20 62 61 63 6b 20 6f 72 20 64 65 6c 65  yed back or dele
10ce0 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ted..    */.    
10ce1 69 66 28 20 21 69 73 45 72 72 6f 72 52 65 73 65  if( !isErrorRese
10ce2 74 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  t ){.      rc = 
10ce3 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c 28 70 50  hasHotJournal(pP
10ce4 61 67 65 72 2c 20 26 69 73 48 6f 74 4a 6f 75 72  ager, &isHotJour
10ce5 6e 61 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20  nal);.      if( 
10ce6 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
10ce7 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61  .        goto fa
10ce8 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20  iled;.      }.  
10ce9 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 45 72    }.    if( isEr
10cea 72 6f 72 52 65 73 65 74 20 7c 7c 20 69 73 48 6f  rorReset || isHo
10ceb 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20  tJournal ){.    
10cec 20 20 2f 2a 20 47 65 74 20 61 6e 20 45 58 43 4c    /* Get an EXCL
10ced 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68  USIVE lock on th
10cee 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
10cef 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 69   At this point i
10cf0 74 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 69 6d  t is.      ** im
10cf1 70 6f 72 74 61 6e 74 20 74 68 61 74 20 61 20 52  portant that a R
10cf2 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 20  ESERVED lock is 
10cf3 6e 6f 74 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20  not obtained on 
10cf4 74 68 65 20 77 61 79 20 74 6f 20 74 68 65 0a 20  the way to the. 
10cf5 20 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49 56       ** EXCLUSIV
10cf6 45 20 6c 6f 63 6b 2e 20 49 66 20 69 74 20 77 65  E lock. If it we
10cf7 72 65 2c 20 61 6e 6f 74 68 65 72 20 70 72 6f 63  re, another proc
10cf8 65 73 73 20 6d 69 67 68 74 20 6f 70 65 6e 20 74  ess might open t
10cf9 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61  he.      ** data
10cfa 62 61 73 65 20 66 69 6c 65 2c 20 64 65 74 65 63  base file, detec
10cfb 74 20 74 68 65 20 52 45 53 45 52 56 45 44 20 6c  t the RESERVED l
10cfc 6f 63 6b 2c 20 61 6e 64 20 63 6f 6e 63 6c 75 64  ock, and conclud
10cfd 65 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 20  e that the.     
10cfe 20 2a 2a 20 64 61 74 61 62 61 73 65 20 69 73 20   ** database is 
10cff 73 61 66 65 20 74 6f 20 72 65 61 64 20 77 68 69  safe to read whi
10d00 6c 65 20 74 68 69 73 20 70 72 6f 63 65 73 73 20  le this process 
10d01 69 73 20 73 74 69 6c 6c 20 72 6f 6c 6c 69 6e 67  is still rolling
10d02 20 69 74 20 0a 20 20 20 20 20 20 2a 2a 20 62 61   it .      ** ba
10d03 63 6b 2e 0a 20 20 20 20 20 20 2a 2a 20 0a 20 20  ck..      ** .  
10d04 20 20 20 20 2a 2a 20 42 65 63 61 75 73 65 20 74      ** Because t
10d05 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  he intermediate 
10d06 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73  RESERVED lock is
10d07 20 6e 6f 74 20 72 65 71 75 65 73 74 65 64 2c 20   not requested, 
10d08 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 73 65 63  the.      ** sec
10d09 6f 6e 64 20 70 72 6f 63 65 73 73 20 77 69 6c 6c  ond process will
10d0a 20 67 65 74 20 74 6f 20 74 68 69 73 20 70 6f 69   get to this poi
10d0b 6e 74 20 69 6e 20 74 68 65 20 63 6f 64 65 20 61  nt in the code a
10d0c 6e 64 20 66 61 69 6c 20 74 6f 0a 20 20 20 20 20  nd fail to.     
10d0d 20 2a 2a 20 6f 62 74 61 69 6e 20 69 74 73 20 6f   ** obtain its o
10d0e 77 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  wn EXCLUSIVE loc
10d0f 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
10d10 65 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 2a 2f  e file..      */
10d11 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65  .      if( pPage
10d12 72 2d 3e 73 74 61 74 65 3c 45 58 43 4c 55 53 49  r->state<EXCLUSI
10d13 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20  VE_LOCK ){.     
10d14 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
10d15 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64  sLock(pPager->fd
10d16 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  , EXCLUSIVE_LOCK
10d17 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
10d18 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
10d19 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 70            rc = p
10d1a 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65  ager_error(pPage
10d1b 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 20 20  r, rc);.        
10d1c 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20    goto failed;. 
10d1d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
10d1e 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d   pPager->state =
10d1f 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45   PAGER_EXCLUSIVE
10d20 3b 0a 20 20 20 20 20 20 7d 0a 20 0a 20 20 20 20  ;.      }. .    
10d21 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f    /* Open the jo
10d22 75 72 6e 61 6c 20 66 6f 72 20 72 65 61 64 2f 77  urnal for read/w
10d23 72 69 74 65 20 61 63 63 65 73 73 2e 20 54 68 69  rite access. Thi
10d24 73 20 69 73 20 62 65 63 61 75 73 65 20 69 6e 20  s is because in 
10d25 0a 20 20 20 20 20 20 2a 2a 20 65 78 63 6c 75 73  .      ** exclus
10d26 69 76 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20  ive-access mode 
10d27 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
10d28 74 6f 72 20 77 69 6c 6c 20 62 65 20 6b 65 70 74  tor will be kept
10d29 20 6f 70 65 6e 20 61 6e 64 0a 20 20 20 20 20 20   open and.      
10d2a 2a 2a 20 70 6f 73 73 69 62 6c 79 20 75 73 65 64  ** possibly used
10d2b 20 66 6f 72 20 61 20 74 72 61 6e 73 61 63 74 69   for a transacti
10d2c 6f 6e 20 6c 61 74 65 72 20 6f 6e 2e 20 4f 6e 20  on later on. On 
10d2d 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c 20 74 68  some systems, th
10d2e 65 0a 20 20 20 20 20 20 2a 2a 20 4f 73 54 72 75  e.      ** OsTru
10d2f 6e 63 61 74 65 28 29 20 63 61 6c 6c 20 75 73 65  ncate() call use
10d30 64 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 61  d in exclusive-a
10d31 63 63 65 73 73 20 6d 6f 64 65 20 61 6c 73 6f 20  ccess mode also 
10d32 72 65 71 75 69 72 65 73 0a 20 20 20 20 20 20 2a  requires.      *
10d33 2a 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 66  * a read/write f
10d34 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 20 20 20 20  ile handle..    
10d35 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 21    */.      if( !
10d36 69 73 45 72 72 6f 72 52 65 73 65 74 20 26 26 20  isErrorReset && 
10d37 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
10d38 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  pen==0 ){.      
10d39 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20    int res;.     
10d3a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
10d3b 73 41 63 63 65 73 73 28 70 56 66 73 2c 70 50 61  sAccess(pVfs,pPa
10d3c 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 53 51  ger->zJournal,SQ
10d3d 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53  LITE_ACCESS_EXIS
10d3e 54 53 2c 26 72 65 73 29 3b 0a 20 20 20 20 20 20  TS,&res);.      
10d3f 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
10d40 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
10d41 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20   if( res ){.    
10d42 20 20 20 20 20 20 20 20 69 6e 74 20 66 6f 75 74          int fout
10d43 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
10d44 20 20 69 6e 74 20 66 20 3d 20 53 51 4c 49 54 45    int f = SQLITE
10d45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c  _OPEN_READWRITE|
10d46 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
10d47 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 20 20  _JOURNAL;.      
10d48 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
10d49 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20  Pager->tempFile 
10d4a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  );.            r
10d4b 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  c = sqlite3OsOpe
10d4c 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e  n(pVfs, pPager->
10d4d 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72  zJournal, pPager
10d4e 2d 3e 6a 66 64 2c 20 66 2c 20 26 66 6f 75 74 29  ->jfd, f, &fout)
10d4f 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73  ;.            as
10d50 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45  sert( rc!=SQLITE
10d51 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a  _OK || pPager->j
10d52 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 3b 0a  fd->pMethods );.
10d53 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
10d54 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
10d55 20 66 6f 75 74 26 53 51 4c 49 54 45 5f 4f 50 45   fout&SQLITE_OPE
10d56 4e 5f 52 45 41 44 4f 4e 4c 59 20 29 7b 0a 20 20  N_READONLY ){.  
10d57 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
10d58 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e   SQLITE_CANTOPEN
10d59 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
10d5a 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70  sqlite3OsClose(p
10d5b 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20  Pager->jfd);.   
10d5c 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
10d5d 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
10d5e 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68          /* If th
10d5f 65 20 6a 6f 75 72 6e 61 6c 20 64 6f 65 73 20 6e  e journal does n
10d60 6f 74 20 65 78 69 73 74 2c 20 74 68 61 74 20 6d  ot exist, that m
10d61 65 61 6e 73 20 73 6f 6d 65 20 6f 74 68 65 72 20  eans some other 
10d62 70 72 6f 63 65 73 73 0a 20 20 20 20 20 20 20 20  process.        
10d63 20 20 20 20 2a 2a 20 68 61 73 20 61 6c 72 65 61      ** has alrea
10d64 64 79 20 72 6f 6c 6c 65 64 20 69 74 20 62 61 63  dy rolled it bac
10d65 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  k */.           
10d66 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
10d67 59 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  Y;.          }. 
10d68 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
10d69 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
10d6a 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
10d6b 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b      goto failed;
10d6c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
10d6d 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70  Pager->journalOp
10d6e 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 50  en = 1;.      pP
10d6f 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61  ager->journalSta
10d70 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20  rted = 0;.      
10d71 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
10d72 66 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50  ff = 0;.      pP
10d73 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20  ager->setMaster 
10d74 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65  = 0;.      pPage
10d75 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20  r->journalHdr = 
10d76 30 3b 0a 20 0a 20 20 20 20 20 20 2f 2a 20 50 6c  0;. .      /* Pl
10d77 61 79 62 61 63 6b 20 61 6e 64 20 64 65 6c 65 74  ayback and delet
10d78 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20  e the journal.  
10d79 44 72 6f 70 20 74 68 65 20 64 61 74 61 62 61 73  Drop the databas
10d7a 65 20 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a  e write.      **
10d7b 20 6c 6f 63 6b 20 61 6e 64 20 72 65 61 63 71 75   lock and reacqu
10d7c 69 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63  ire the read loc
10d7d 6b 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  k..      */.    
10d7e 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61    rc = pager_pla
10d7f 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 31 29  yback(pPager, 1)
10d80 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
10d81 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
10d82 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f       rc = pager_
10d83 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63  error(pPager, rc
10d84 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  );.        goto 
10d85 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a  failed;.      }.
10d86 20 20 20 20 20 20 61 73 73 65 72 74 28 70 50 61        assert(pPa
10d87 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45  ger->state==PAGE
10d88 52 5f 53 48 41 52 45 44 20 7c 7c 20 0a 20 20 20  R_SHARED || .   
10d89 20 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e         (pPager->
10d8a 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 26 26  exclusiveMode &&
10d8b 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 50   pPager->state>P
10d8c 41 47 45 52 5f 53 48 41 52 45 44 29 0a 20 20 20  AGER_SHARED).   
10d8d 20 20 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20     );.    }..   
10d8e 20 69 66 28 20 73 71 6c 69 74 65 33 50 63 61 63   if( sqlite3Pcac
10d8f 68 65 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67  hePagecount(pPag
10d90 65 72 2d 3e 70 50 43 61 63 68 65 29 3e 30 20 29  er->pPCache)>0 )
10d91 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73  {.      /* The s
10d92 68 61 72 65 64 2d 6c 6f 63 6b 20 68 61 73 20 6a  hared-lock has j
10d93 75 73 74 20 62 65 65 6e 20 61 63 71 75 69 72 65  ust been acquire
10d94 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  d on the databas
10d95 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20  e file.      ** 
10d96 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 61 6c  and there are al
10d97 72 65 61 64 79 20 70 61 67 65 73 20 69 6e 20 74  ready pages in t
10d98 68 65 20 63 61 63 68 65 20 28 66 72 6f 6d 20 61  he cache (from a
10d99 20 70 72 65 76 69 6f 75 73 0a 20 20 20 20 20 20   previous.      
10d9a 2a 2a 20 72 65 61 64 20 6f 72 20 77 72 69 74 65  ** read or write
10d9b 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 20 20   transaction).  
10d9c 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
10d9d 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20  the database.   
10d9e 20 20 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d     ** has been m
10d9f 6f 64 69 66 69 65 64 2e 20 20 49 66 20 74 68 65  odified.  If the
10da0 20 64 61 74 61 62 61 73 65 20 68 61 73 20 63 68   database has ch
10da1 61 6e 67 65 64 2c 20 66 6c 75 73 68 20 74 68 65  anged, flush the
10da2 0a 20 20 20 20 20 20 2a 2a 20 63 61 63 68 65 2e  .      ** cache.
10da3 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
10da4 2a 2a 20 44 61 74 61 62 61 73 65 20 63 68 61 6e  ** Database chan
10da5 67 65 73 20 69 73 20 64 65 74 65 63 74 65 64 20  ges is detected 
10da6 62 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 31 35  by looking at 15
10da7 20 62 79 74 65 73 20 62 65 67 69 6e 6e 69 6e 67   bytes beginning
10da8 0a 20 20 20 20 20 20 2a 2a 20 61 74 20 6f 66 66  .      ** at off
10da9 73 65 74 20 32 34 20 69 6e 74 6f 20 74 68 65 20  set 24 into the 
10daa 66 69 6c 65 2e 20 20 54 68 65 20 66 69 72 73 74  file.  The first
10dab 20 34 20 6f 66 20 74 68 65 73 65 20 31 36 20 62   4 of these 16 b
10dac 79 74 65 73 20 61 72 65 0a 20 20 20 20 20 20 2a  ytes are.      *
10dad 2a 20 61 20 33 32 2d 62 69 74 20 63 6f 75 6e 74  * a 32-bit count
10dae 65 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 65  er that is incre
10daf 6d 65 6e 74 65 64 20 77 69 74 68 20 65 61 63 68  mented with each
10db0 20 63 68 61 6e 67 65 2e 20 20 54 68 65 0a 20 20   change.  The.  
10db1 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 62 79 74      ** other byt
10db2 65 73 20 63 68 61 6e 67 65 20 72 61 6e 64 6f 6d  es change random
10db3 6c 79 20 77 69 74 68 20 65 61 63 68 20 66 69 6c  ly with each fil
10db4 65 20 63 68 61 6e 67 65 20 77 68 65 6e 0a 20 20  e change when.  
10db5 20 20 20 20 2a 2a 20 61 20 63 6f 64 65 63 20 69      ** a codec i
10db6 73 20 69 6e 20 75 73 65 2e 0a 20 20 20 20 20 20  s in use..      
10db7 2a 2a 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 65  ** .      ** The
10db8 72 65 20 69 73 20 61 20 76 61 6e 69 73 68 69 6e  re is a vanishin
10db9 67 6c 79 20 73 6d 61 6c 6c 20 63 68 61 6e 63 65  gly small chance
10dba 20 74 68 61 74 20 61 20 63 68 61 6e 67 65 20 77   that a change w
10dbb 69 6c 6c 20 6e 6f 74 20 62 65 20 0a 20 20 20 20  ill not be .    
10dbc 20 20 2a 2a 20 64 65 74 65 63 74 65 64 2e 20 20    ** detected.  
10dbd 54 68 65 20 63 68 61 6e 63 65 20 6f 66 20 61 6e  The chance of an
10dbe 20 75 6e 64 65 74 65 63 74 65 64 20 63 68 61 6e   undetected chan
10dbf 67 65 20 69 73 20 73 6f 20 73 6d 61 6c 6c 20 74  ge is so small t
10dc0 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 69 74 20  hat.      ** it 
10dc1 63 61 6e 20 62 65 20 6e 65 67 6c 65 63 74 65 64  can be neglected
10dc2 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
10dc3 20 63 68 61 72 20 64 62 46 69 6c 65 56 65 72 73   char dbFileVers
10dc4 5b 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e  [sizeof(pPager->
10dc5 64 62 46 69 6c 65 56 65 72 73 29 5d 3b 0a 20 20  dbFileVers)];.  
10dc6 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
10dc7 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72  Pagecount(pPager
10dc8 2c 20 30 29 3b 0a 0a 20 20 20 20 20 20 69 66 28  , 0);..      if(
10dc9 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
10dca 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
10dcb 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
10dcc 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66  ;.        goto f
10dcd 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 0a  ailed;.      }..
10dce 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 72        if( pPager
10dcf 2d 3e 64 62 53 69 7a 65 3e 30 20 29 7b 0a 20 20  ->dbSize>0 ){.  
10dd0 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22        IOTRACE(("
10dd1 43 4b 56 45 52 53 20 25 70 20 25 64 5c 6e 22 2c  CKVERS %p %d\n",
10dd2 20 70 50 61 67 65 72 2c 20 73 69 7a 65 6f 66 28   pPager, sizeof(
10dd3 64 62 46 69 6c 65 56 65 72 73 29 29 29 3b 0a 20  dbFileVers)));. 
10dd4 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
10dd5 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72  te3OsRead(pPager
10dd6 2d 3e 66 64 2c 20 26 64 62 46 69 6c 65 56 65 72  ->fd, &dbFileVer
10dd7 73 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c 65  s, sizeof(dbFile
10dd8 56 65 72 73 29 2c 20 32 34 29 3b 0a 20 20 20 20  Vers), 24);.    
10dd9 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
10dda 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
10ddb 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a     goto failed;.
10ddc 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
10ddd 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6d  }else{.        m
10dde 65 6d 73 65 74 28 64 62 46 69 6c 65 56 65 72 73  emset(dbFileVers
10ddf 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 62 46 69  , 0, sizeof(dbFi
10de0 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 20 20 20  leVers));.      
10de1 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d  }..      if( mem
10de2 63 6d 70 28 70 50 61 67 65 72 2d 3e 64 62 46 69  cmp(pPager->dbFi
10de3 6c 65 56 65 72 73 2c 20 64 62 46 69 6c 65 56 65  leVers, dbFileVe
10de4 72 73 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c  rs, sizeof(dbFil
10de5 65 56 65 72 73 29 29 21 3d 30 20 29 7b 0a 20 20  eVers))!=0 ){.  
10de6 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 73 65        pager_rese
10de7 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20  t(pPager);.     
10de8 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73   }.    }.    ass
10de9 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 78 63  ert( pPager->exc
10dea 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 70 50  lusiveMode || pP
10deb 61 67 65 72 2d 3e 73 74 61 74 65 3c 3d 50 41 47  ager->state<=PAG
10dec 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 20 20 20  ER_SHARED );.   
10ded 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61   if( pPager->sta
10dee 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b  te==PAGER_UNLOCK
10def 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72   ){.      pPager
10df0 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f  ->state = PAGER_
10df1 53 48 41 52 45 44 3b 0a 20 20 20 20 7d 0a 20 20  SHARED;.    }.  
10df2 7d 0a 0a 20 66 61 69 6c 65 64 3a 0a 20 20 69 66  }.. failed:.  if
10df3 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
10df4 29 7b 0a 20 20 20 20 2f 2a 20 70 61 67 65 72 5f  ){.    /* pager_
10df5 75 6e 6c 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f  unlock() is a no
10df6 2d 6f 70 20 66 6f 72 20 65 78 63 6c 75 73 69 76  -op for exclusiv
10df7 65 20 6d 6f 64 65 20 61 6e 64 20 69 6e 2d 6d 65  e mode and in-me
10df8 6d 6f 72 79 20 64 61 74 61 62 61 73 65 73 2e 20  mory databases. 
10df9 2a 2f 0a 20 20 20 20 70 61 67 65 72 5f 75 6e 6c  */.    pager_unl
10dfa 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  ock(pPager);.  }
10dfb 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
10dfc 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
10dfd 20 77 65 20 68 61 76 65 20 74 68 65 20 63 6f 6e   we have the con
10dfe 74 65 6e 74 20 66 6f 72 20 61 20 70 61 67 65 2e  tent for a page.
10dff 20 20 49 66 20 74 68 65 20 70 61 67 65 20 77 61    If the page wa
10e00 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20  s.** previously 
10e01 61 63 71 75 69 72 65 64 20 77 69 74 68 20 6e 6f  acquired with no
10e02 43 6f 6e 74 65 6e 74 3d 3d 31 2c 20 74 68 65 6e  Content==1, then
10e03 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 77 61 73   the content was
10e04 0a 2a 2a 20 6a 75 73 74 20 69 6e 69 74 69 61 6c  .** just initial
10e05 69 7a 65 64 20 74 6f 20 7a 65 72 6f 73 20 69 6e  ized to zeros in
10e06 73 74 65 61 64 20 6f 66 20 62 65 69 6e 67 20 72  stead of being r
10e07 65 61 64 20 66 72 6f 6d 20 64 69 73 6b 2e 0a 2a  ead from disk..*
10e08 2a 20 42 75 74 20 6e 6f 77 20 77 65 20 6e 65 65  * But now we nee
10e09 64 20 74 68 65 20 72 65 61 6c 20 64 61 74 61 20  d the real data 
10e0a 6f 66 66 20 6f 66 20 64 69 73 6b 2e 20 20 53 6f  off of disk.  So
10e0b 20 6d 61 6b 65 20 73 75 72 65 20 77 65 0a 2a 2a   make sure we.**
10e0c 20 68 61 76 65 20 69 74 2e 20 20 52 65 61 64 20   have it.  Read 
10e0d 69 74 20 69 6e 20 69 66 20 77 65 20 64 6f 20 6e  it in if we do n
10e0e 6f 74 20 68 61 76 65 20 69 74 20 61 6c 72 65 61  ot have it alrea
10e0f 64 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  dy..*/.static in
10e10 74 20 70 61 67 65 72 5f 67 65 74 5f 63 6f 6e 74  t pager_get_cont
10e11 65 6e 74 28 50 67 48 64 72 20 2a 70 50 67 29 7b  ent(PgHdr *pPg){
10e12 0a 20 20 69 66 28 20 70 50 67 2d 3e 66 6c 61 67  .  if( pPg->flag
10e13 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 52 45 41  s&PGHDR_NEED_REA
10e14 44 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20  D ){.    int rc 
10e15 3d 20 72 65 61 64 44 62 50 61 67 65 28 70 50 67  = readDbPage(pPg
10e16 2d 3e 70 50 61 67 65 72 2c 20 70 50 67 2c 20 70  ->pPager, pPg, p
10e17 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 69  Pg->pgno);.    i
10e18 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
10e19 20 29 7b 0a 20 20 20 20 20 20 70 50 67 2d 3e 66   ){.      pPg->f
10e1a 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e  lags &= ~PGHDR_N
10e1b 45 45 44 5f 52 45 41 44 3b 0a 20 20 20 20 7d 65  EED_READ;.    }e
10e1c 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72  lse{.      retur
10e1d 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  n rc;.    }.  }.
10e1e 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
10e1f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  OK;.}../*.** If 
10e20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f  the reference co
10e21 75 6e 74 20 68 61 73 20 72 65 61 63 68 65 64 20  unt has reached 
10e22 7a 65 72 6f 2c 20 61 6e 64 20 74 68 65 20 70 61  zero, and the pa
10e23 67 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 74 68  ger is not in th
10e24 65 0a 2a 2a 20 6d 69 64 64 6c 65 20 6f 66 20 61  e.** middle of a
10e25 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
10e26 6f 6e 20 6f 72 20 6f 70 65 6e 65 64 20 69 6e 20  on or opened in 
10e27 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20  exclusive mode, 
10e28 75 6e 6c 6f 63 6b 20 69 74 2e 0a 2a 2f 20 0a 73  unlock it..*/ .s
10e29 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72  tatic void pager
10e2a 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64 28 50  UnlockIfUnused(P
10e2b 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
10e2c 20 69 66 28 20 28 73 71 6c 69 74 65 33 50 63 61   if( (sqlite3Pca
10e2d 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67  cheRefCount(pPag
10e2e 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 29  er->pPCache)==0)
10e2f 0a 20 20 20 20 26 26 20 28 21 70 50 61 67 65 72  .    && (!pPager
10e30 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20  ->exclusiveMode 
10e31 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  || pPager->journ
10e32 61 6c 4f 66 66 3e 30 29 20 0a 20 20 29 7b 0a 20  alOff>0) .  ){. 
10e33 20 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e     pagerUnlockAn
10e34 64 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72  dRollback(pPager
10e35 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
10e36 44 72 6f 70 20 61 20 70 61 67 65 20 66 72 6f 6d  Drop a page from
10e37 20 74 68 65 20 63 61 63 68 65 20 75 73 69 6e 67   the cache using
10e38 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 72   sqlite3PcacheDr
10e39 6f 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  op()..**.** If t
10e3a 68 69 73 20 6d 65 61 6e 73 20 74 68 65 72 65 20  his means there 
10e3b 61 72 65 20 6e 6f 77 20 6e 6f 20 70 61 67 65 73  are now no pages
10e3c 20 77 69 74 68 20 72 65 66 65 72 65 6e 63 65 73   with references
10e3d 20 74 6f 20 74 68 65 6d 2c 20 61 20 72 6f 6c 6c   to them, a roll
10e3e 62 61 63 6b 0a 2a 2a 20 6f 63 63 75 72 73 20 61  back.** occurs a
10e3f 6e 64 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74  nd the lock on t
10e40 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 72  he database is r
10e41 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  emoved..*/.stati
10e42 63 20 76 6f 69 64 20 70 61 67 65 72 44 72 6f 70  c void pagerDrop
10e43 50 61 67 65 28 44 62 50 61 67 65 20 2a 70 50 67  Page(DbPage *pPg
10e44 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  ){.  Pager *pPag
10e45 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72  er = pPg->pPager
10e46 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68  ;.  sqlite3Pcach
10e47 65 44 72 6f 70 28 70 50 67 29 3b 0a 20 20 70 61  eDrop(pPg);.  pa
10e48 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65  gerUnlockIfUnuse
10e49 64 28 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a  d(pPager);.}../*
10e4a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61 20 70 61  .** Acquire a pa
10e4b 67 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 61 64  ge..**.** A read
10e4c 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 69 73   lock on the dis
10e4d 6b 20 66 69 6c 65 20 69 73 20 6f 62 74 61 69 6e  k file is obtain
10e4e 65 64 20 77 68 65 6e 20 74 68 65 20 66 69 72 73  ed when the firs
10e4f 74 20 70 61 67 65 20 69 73 20 61 63 71 75 69 72  t page is acquir
10e50 65 64 2e 20 0a 2a 2a 20 54 68 69 73 20 72 65 61  ed. .** This rea
10e51 64 20 6c 6f 63 6b 20 69 73 20 64 72 6f 70 70 65  d lock is droppe
10e52 64 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20  d when the last 
10e53 70 61 67 65 20 69 73 20 72 65 6c 65 61 73 65 64  page is released
10e54 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
10e55 74 69 6e 65 20 77 6f 72 6b 73 20 66 6f 72 20 61  tine works for a
10e56 6e 79 20 70 61 67 65 20 6e 75 6d 62 65 72 20 67  ny page number g
10e57 72 65 61 74 65 72 20 74 68 61 6e 20 30 2e 20 20  reater than 0.  
10e58 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  If the database.
10e59 2a 2a 20 66 69 6c 65 20 69 73 20 73 6d 61 6c 6c  ** file is small
10e5a 65 72 20 74 68 61 6e 20 74 68 65 20 72 65 71 75  er than the requ
10e5b 65 73 74 65 64 20 70 61 67 65 2c 20 74 68 65 6e  ested page, then
10e5c 20 6e 6f 20 61 63 74 75 61 6c 20 64 69 73 6b 0a   no actual disk.
10e5d 2a 2a 20 72 65 61 64 20 6f 63 63 75 72 73 20 61  ** read occurs a
10e5e 6e 64 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 6d  nd the memory im
10e5f 61 67 65 20 6f 66 20 74 68 65 20 70 61 67 65 20  age of the page 
10e60 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74  is initialized t
10e61 6f 0a 2a 2a 20 61 6c 6c 20 7a 65 72 6f 73 2e 20  o.** all zeros. 
10e62 20 54 68 65 20 65 78 74 72 61 20 64 61 74 61 20   The extra data 
10e63 61 70 70 65 6e 64 65 64 20 74 6f 20 61 20 70 61  appended to a pa
10e64 67 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 69  ge is always ini
10e65 74 69 61 6c 69 7a 65 64 0a 2a 2a 20 74 6f 20 7a  tialized.** to z
10e66 65 72 6f 73 20 74 68 65 20 66 69 72 73 74 20 74  eros the first t
10e67 69 6d 65 20 61 20 70 61 67 65 20 69 73 20 6c 6f  ime a page is lo
10e68 61 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79  aded into memory
10e69 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 71 75  ..**.** The acqu
10e6a 69 73 69 74 69 6f 6e 20 6d 69 67 68 74 20 66 61  isition might fa
10e6b 69 6c 20 66 6f 72 20 73 65 76 65 72 61 6c 20 72  il for several r
10e6c 65 61 73 6f 6e 73 2e 20 20 49 6e 20 61 6c 6c 20  easons.  In all 
10e6d 63 61 73 65 73 2c 0a 2a 2a 20 61 6e 20 61 70 70  cases,.** an app
10e6e 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 63  ropriate error c
10e6f 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20  ode is returned 
10e70 61 6e 64 20 2a 70 70 50 61 67 65 20 69 73 20 73  and *ppPage is s
10e71 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  et to NULL..**.*
10e72 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74  * See also sqlit
10e73 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 29 2e  e3PagerLookup().
10e74 20 20 42 6f 74 68 20 74 68 69 73 20 72 6f 75 74    Both this rout
10e75 69 6e 65 20 61 6e 64 20 4c 6f 6f 6b 75 70 28 29  ine and Lookup()
10e76 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 66   attempt.** to f
10e77 69 6e 64 20 61 20 70 61 67 65 20 69 6e 20 74 68  ind a page in th
10e78 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68  e in-memory cach
10e79 65 20 66 69 72 73 74 2e 20 20 49 66 20 74 68 65  e first.  If the
10e7a 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72   page is not alr
10e7b 65 61 64 79 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72  eady.** in memor
10e7c 79 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  y, this routine 
10e7d 67 6f 65 73 20 74 6f 20 64 69 73 6b 20 74 6f 20  goes to disk to 
10e7e 72 65 61 64 20 69 74 20 69 6e 20 77 68 65 72 65  read it in where
10e7f 61 73 20 4c 6f 6f 6b 75 70 28 29 0a 2a 2a 20 6a  as Lookup().** j
10e80 75 73 74 20 72 65 74 75 72 6e 73 20 30 2e 20 20  ust returns 0.  
10e81 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63 71  This routine acq
10e82 75 69 72 65 73 20 61 20 72 65 61 64 2d 6c 6f 63  uires a read-loc
10e83 6b 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65  k the first time
10e84 20 69 74 0a 2a 2a 20 68 61 73 20 74 6f 20 67 6f   it.** has to go
10e85 20 74 6f 20 64 69 73 6b 2c 20 61 6e 64 20 63 6f   to disk, and co
10e86 75 6c 64 20 61 6c 73 6f 20 70 6c 61 79 62 61 63  uld also playbac
10e87 6b 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c  k an old journal
10e88 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a   if necessary..*
10e89 2a 20 53 69 6e 63 65 20 4c 6f 6f 6b 75 70 28 29  * Since Lookup()
10e8a 20 6e 65 76 65 72 20 67 6f 65 73 20 74 6f 20 64   never goes to d
10e8b 69 73 6b 2c 20 69 74 20 6e 65 76 65 72 20 68 61  isk, it never ha
10e8c 73 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 6c  s to deal with l
10e8d 6f 63 6b 73 0a 2a 2a 20 6f 72 20 6a 6f 75 72 6e  ocks.** or journ
10e8e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  al files..**.** 
10e8f 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 20  If noContent is 
10e90 66 61 6c 73 65 2c 20 74 68 65 20 70 61 67 65 20  false, the page 
10e91 63 6f 6e 74 65 6e 74 73 20 61 72 65 20 61 63 74  contents are act
10e92 75 61 6c 6c 79 20 72 65 61 64 20 66 72 6f 6d 20  ually read from 
10e93 64 69 73 6b 2e 0a 2a 2a 20 49 66 20 6e 6f 43 6f  disk..** If noCo
10e94 6e 74 65 6e 74 20 69 73 20 74 72 75 65 2c 20 69  ntent is true, i
10e95 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20  t means that we 
10e96 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75  do not care abou
10e97 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 2a  t the contents.*
10e98 2a 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 74  * of the page at
10e99 20 74 68 69 73 20 74 69 6d 65 2c 20 73 6f 20 64   this time, so d
10e9a 6f 20 6e 6f 74 20 64 6f 20 61 20 64 69 73 6b 20  o not do a disk 
10e9b 72 65 61 64 2e 20 20 4a 75 73 74 20 66 69 6c 6c  read.  Just fill
10e9c 20 69 6e 20 74 68 65 0a 2a 2a 20 70 61 67 65 20   in the.** page 
10e9d 63 6f 6e 74 65 6e 74 20 77 69 74 68 20 7a 65 72  content with zer
10e9e 6f 73 2e 20 20 42 75 74 20 6d 61 72 6b 20 74 68  os.  But mark th
10e9f 65 20 66 61 63 74 20 74 68 61 74 20 77 65 20 68  e fact that we h
10ea0 61 76 65 20 6e 6f 74 20 72 65 61 64 20 74 68 65  ave not read the
10ea1 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 62 79 20 73  .** content by s
10ea2 65 74 74 69 6e 67 20 74 68 65 20 50 67 48 64 72  etting the PgHdr
10ea3 2e 6e 65 65 64 52 65 61 64 20 66 6c 61 67 2e 20  .needRead flag. 
10ea4 20 4c 61 74 65 72 20 6f 6e 2c 20 69 66 20 0a 2a   Later on, if .*
10ea5 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  * sqlite3PagerWr
10ea6 69 74 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20  ite() is called 
10ea7 6f 6e 20 74 68 69 73 20 70 61 67 65 20 6f 72 20  on this page or 
10ea8 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  if this routine 
10ea9 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 67 61  is.** called aga
10eaa 69 6e 20 77 69 74 68 20 6e 6f 43 6f 6e 74 65 6e  in with noConten
10eab 74 3d 3d 30 2c 20 74 68 61 74 20 6d 65 61 6e 73  t==0, that means
10eac 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e   that the conten
10ead 74 20 69 73 20 6e 65 65 64 65 64 0a 2a 2a 20 61  t is needed.** a
10eae 6e 64 20 74 68 65 20 64 69 73 6b 20 72 65 61 64  nd the disk read
10eaf 20 73 68 6f 75 6c 64 20 6f 63 63 75 72 20 61 74   should occur at
10eb0 20 74 68 61 74 20 70 6f 69 6e 74 2e 0a 2a 2f 0a   that point..*/.
10eb1 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
10eb2 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 41  nt sqlite3PagerA
10eb3 63 71 75 69 72 65 28 0a 20 20 50 61 67 65 72 20  cquire(.  Pager 
10eb4 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 2f 2a  *pPager,      /*
10eb5 20 54 68 65 20 70 61 67 65 72 20 6f 70 65 6e 20   The pager open 
10eb6 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
10eb7 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70  file */.  Pgno p
10eb8 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  gno,          /*
10eb9 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20   Page number to 
10eba 66 65 74 63 68 20 2a 2f 0a 20 20 44 62 50 61 67  fetch */.  DbPag
10ebb 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 2f  e **ppPage,    /
10ebc 2a 20 57 72 69 74 65 20 61 20 70 6f 69 6e 74 65  * Write a pointe
10ebd 72 20 74 6f 20 74 68 65 20 70 61 67 65 20 68 65  r to the page he
10ebe 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f  re */.  int noCo
10ebf 6e 74 65 6e 74 20 20 20 20 20 20 20 2f 2a 20 44  ntent       /* D
10ec0 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 72 65 61  o not bother rea
10ec1 64 69 6e 67 20 63 6f 6e 74 65 6e 74 20 66 72 6f  ding content fro
10ec2 6d 20 64 69 73 6b 20 69 66 20 74 72 75 65 20 2a  m disk if true *
10ec3 2f 0a 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50  /.){.  PgHdr *pP
10ec4 67 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b  g = 0;.  int rc;
10ec5 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ..  assert( pPag
10ec6 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52  er->state==PAGER
10ec7 5f 55 4e 4c 4f 43 4b 20 0a 20 20 20 20 20 20 20  _UNLOCK .       
10ec8 7c 7c 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  || sqlite3Pcache
10ec9 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d  RefCount(pPager-
10eca 3e 70 50 43 61 63 68 65 29 3e 30 20 0a 20 20 20  >pPCache)>0 .   
10ecb 20 20 20 20 7c 7c 20 70 67 6e 6f 3d 3d 31 0a 20      || pgno==1. 
10ecc 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 61   );..  /* The ma
10ecd 78 69 6d 75 6d 20 70 61 67 65 20 6e 75 6d 62 65  ximum page numbe
10ece 72 20 69 73 20 32 5e 33 31 2e 20 52 65 74 75 72  r is 2^31. Retur
10ecf 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
10ed0 20 69 66 20 61 20 70 61 67 65 0a 20 20 2a 2a 20   if a page.  ** 
10ed1 6e 75 6d 62 65 72 20 67 72 65 61 74 65 72 20 74  number greater t
10ed2 68 61 6e 20 74 68 69 73 2c 20 6f 72 20 7a 65 72  han this, or zer
10ed3 6f 2c 20 69 73 20 72 65 71 75 65 73 74 65 64 2e  o, is requested.
10ed4 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 67 6e 6f  .  */.  if( pgno
10ed5 3e 50 41 47 45 52 5f 4d 41 58 5f 50 47 4e 4f 20  >PAGER_MAX_PGNO 
10ed6 7c 7c 20 70 67 6e 6f 3d 3d 30 20 7c 7c 20 70 67  || pgno==0 || pg
10ed7 6e 6f 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e  no==PAGER_MJ_PGN
10ed8 4f 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20  O(pPager) ){.   
10ed9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
10eda 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d  ORRUPT_BKPT;.  }
10edb 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
10edc 20 77 65 20 68 61 76 65 20 6e 6f 74 20 68 69 74   we have not hit
10edd 20 61 6e 79 20 63 72 69 74 69 63 61 6c 20 65 72   any critical er
10ede 72 6f 72 73 2e 0a 20 20 2a 2f 20 0a 20 20 61 73  rors..  */ .  as
10edf 73 65 72 74 28 20 70 50 61 67 65 72 21 3d 30 20  sert( pPager!=0 
10ee0 29 3b 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 30  );.  *ppPage = 0
10ee1 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20  ;..  /* If this 
10ee2 69 73 20 74 68 65 20 66 69 72 73 74 20 70 61 67  is the first pag
10ee3 65 20 61 63 63 65 73 73 65 64 2c 20 74 68 65 6e  e accessed, then
10ee4 20 67 65 74 20 61 20 53 48 41 52 45 44 20 6c 6f   get a SHARED lo
10ee5 63 6b 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 64  ck.  ** on the d
10ee6 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 70 61  atabase file. pa
10ee7 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 29 20  gerSharedLock() 
10ee8 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 0a 20  is a no-op if . 
10ee9 20 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20 6c   ** a database l
10eea 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68  ock is already h
10eeb 65 6c 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d  eld..  */.  rc =
10eec 20 70 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b   pagerSharedLock
10eed 28 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20  (pPager);.  if( 
10eee 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
10eef 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
10ef0 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 50    }.  assert( pP
10ef1 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47  ager->state!=PAG
10ef2 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 0a 20 20  ER_UNLOCK );..  
10ef3 72 63 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63  rc = sqlite3Pcac
10ef4 68 65 46 65 74 63 68 28 70 50 61 67 65 72 2d 3e  heFetch(pPager->
10ef5 70 50 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 31  pPCache, pgno, 1
10ef6 2c 20 26 70 50 67 29 3b 0a 20 20 69 66 28 20 72  , &pPg);.  if( r
10ef7 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
10ef8 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
10ef9 20 7d 0a 20 20 69 66 28 20 70 50 67 2d 3e 70 50   }.  if( pPg->pP
10efa 61 67 65 72 3d 3d 30 20 29 7b 0a 20 20 20 20 2f  ager==0 ){.    /
10efb 2a 20 54 68 65 20 70 61 67 65 72 20 63 61 63 68  * The pager cach
10efc 65 20 68 61 73 20 63 72 65 61 74 65 64 20 61 20  e has created a 
10efd 6e 65 77 20 70 61 67 65 2e 20 49 74 73 20 63 6f  new page. Its co
10efe 6e 74 65 6e 74 20 6e 65 65 64 73 20 74 6f 20 0a  ntent needs to .
10eff 20 20 20 20 2a 2a 20 62 65 20 69 6e 69 74 69 61      ** be initia
10f00 6c 69 7a 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  lized..    */.  
10f01 20 20 69 6e 74 20 6e 4d 61 78 3b 0a 20 20 20 20    int nMax;.    
10f02 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65  PAGER_INCR(pPage
10f03 72 2d 3e 6e 4d 69 73 73 29 3b 0a 20 20 20 20 70  r->nMiss);.    p
10f04 50 67 2d 3e 70 50 61 67 65 72 20 3d 20 70 50 61  Pg->pPager = pPa
10f05 67 65 72 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  ger;.    if( sql
10f06 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70  ite3BitvecTest(p
10f07 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
10f08 6c 2c 20 70 67 6e 6f 29 20 29 7b 0a 20 20 20 20  l, pgno) ){.    
10f09 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20    pPg->flags |= 
10f0a 50 47 48 44 52 5f 49 4e 5f 4a 4f 55 52 4e 41 4c  PGHDR_IN_JOURNAL
10f0b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73  ;.    }.    mems
10f0c 65 74 28 70 50 67 2d 3e 70 45 78 74 72 61 2c 20  et(pPg->pExtra, 
10f0d 30 2c 20 70 50 61 67 65 72 2d 3e 6e 45 78 74 72  0, pPager->nExtr
10f0e 61 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73 71  a);..    rc = sq
10f0f 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f  lite3PagerPageco
10f10 75 6e 74 28 70 50 61 67 65 72 2c 20 26 6e 4d 61  unt(pPager, &nMa
10f11 78 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  x);.    if( rc!=
10f12 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
10f13 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55     sqlite3PagerU
10f14 6e 72 65 66 28 70 50 67 29 3b 0a 20 20 20 20 20  nref(pPg);.     
10f15 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
10f16 7d 0a 0a 20 20 20 20 69 66 28 20 6e 4d 61 78 3c  }..    if( nMax<
10f17 28 69 6e 74 29 70 67 6e 6f 20 7c 7c 20 4d 45 4d  (int)pgno || MEM
10f18 44 42 20 7c 7c 20 6e 6f 43 6f 6e 74 65 6e 74 20  DB || noContent 
10f19 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 67 6e  ){.      if( pgn
10f1a 6f 3e 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f  o>pPager->mxPgno
10f1b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
10f1c 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50  te3PagerUnref(pP
10f1d 67 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  g);.        retu
10f1e 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a  rn SQLITE_FULL;.
10f1f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 65        }.      me
10f20 6d 73 65 74 28 70 50 67 2d 3e 70 44 61 74 61 2c  mset(pPg->pData,
10f21 20 30 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65   0, pPager->page
10f22 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28  Size);.      if(
10f23 20 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20   noContent ){.  
10f24 20 20 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73        pPg->flags
10f25 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 52   |= PGHDR_NEED_R
10f26 45 41 44 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  EAD;.      }.   
10f27 20 20 20 49 4f 54 52 41 43 45 28 28 22 5a 45 52     IOTRACE(("ZER
10f28 4f 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67  O %p %d\n", pPag
10f29 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 20 20  er, pgno));.    
10f2a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
10f2b 3d 20 72 65 61 64 44 62 50 61 67 65 28 70 50 61  = readDbPage(pPa
10f2c 67 65 72 2c 20 70 50 67 2c 20 70 67 6e 6f 29 3b  ger, pPg, pgno);
10f2d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
10f2e 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d  QLITE_OK && rc!=
10f2f 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f  SQLITE_IOERR_SHO
10f30 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20  RT_READ ){.     
10f31 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 50 61 67     /* sqlite3Pag
10f32 65 72 55 6e 72 65 66 28 70 50 67 29 3b 20 2a 2f  erUnref(pPg); */
10f33 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 44 72  .        pagerDr
10f34 6f 70 50 61 67 65 28 70 50 67 29 3b 0a 20 20 20  opPage(pPg);.   
10f35 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
10f36 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69        }.    }.#i
10f37 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43  fdef SQLITE_CHEC
10f38 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 50 67 2d  K_PAGES.    pPg-
10f39 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 65  >pageHash = page
10f3a 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 3b  r_pagehash(pPg);
10f3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b  .#endif.  }else{
10f3c 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 71 75  .    /* The requ
10f3d 65 73 74 65 64 20 70 61 67 65 20 69 73 20 69 6e  ested page is in
10f3e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 2e   the page cache.
10f3f 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 73   */.    assert(s
10f40 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43  qlite3PcacheRefC
10f41 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43  ount(pPager->pPC
10f42 61 63 68 65 29 3e 30 20 7c 7c 20 70 67 6e 6f 3d  ache)>0 || pgno=
10f43 3d 31 29 3b 0a 20 20 20 20 50 41 47 45 52 5f 49  =1);.    PAGER_I
10f44 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 48 69 74  NCR(pPager->nHit
10f45 29 3b 0a 20 20 20 20 69 66 28 20 21 6e 6f 43 6f  );.    if( !noCo
10f46 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 72  ntent ){.      r
10f47 63 20 3d 20 70 61 67 65 72 5f 67 65 74 5f 63 6f  c = pager_get_co
10f48 6e 74 65 6e 74 28 70 50 67 29 3b 0a 20 20 20 20  ntent(pPg);.    
10f49 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
10f4a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
10f4b 55 6e 72 65 66 28 70 50 67 29 3b 0a 20 20 20 20  Unref(pPg);.    
10f4c 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
10f4d 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
10f4e 0a 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50  ..  *ppPage = pP
10f4f 67 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  g;.  return SQLI
10f50 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
10f51 41 63 71 75 69 72 65 20 61 20 70 61 67 65 20 69  Acquire a page i
10f52 66 20 69 74 20 69 73 20 61 6c 72 65 61 64 79 20  f it is already 
10f53 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  in the in-memory
10f54 20 63 61 63 68 65 2e 20 20 44 6f 0a 2a 2a 20 6e   cache.  Do.** n
10f55 6f 74 20 72 65 61 64 20 74 68 65 20 70 61 67 65  ot read the page
10f56 20 66 72 6f 6d 20 64 69 73 6b 2e 20 20 52 65 74   from disk.  Ret
10f57 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
10f58 20 74 68 65 20 70 61 67 65 2c 0a 2a 2a 20 6f 72   the page,.** or
10f59 20 30 20 69 66 20 74 68 65 20 70 61 67 65 20 69   0 if the page i
10f5a 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 2e 0a  s not in cache..
10f5b 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73  **.** See also s
10f5c 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29  qlite3PagerGet()
10f5d 2e 20 20 54 68 65 20 64 69 66 66 65 72 65 6e 63  .  The differenc
10f5e 65 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 72  e between this r
10f5f 6f 75 74 69 6e 65 0a 2a 2a 20 61 6e 64 20 73 71  outine.** and sq
10f60 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20  lite3PagerGet() 
10f61 69 73 20 74 68 61 74 20 5f 67 65 74 28 29 20 77  is that _get() w
10f62 69 6c 6c 20 67 6f 20 74 6f 20 74 68 65 20 64 69  ill go to the di
10f63 73 6b 20 61 6e 64 20 72 65 61 64 0a 2a 2a 20 69  sk and read.** i
10f64 6e 20 74 68 65 20 70 61 67 65 20 69 66 20 74 68  n the page if th
10f65 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c  e page is not al
10f66 72 65 61 64 79 20 69 6e 20 63 61 63 68 65 2e 20  ready in cache. 
10f67 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   This routine.**
10f68 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 66   returns NULL if
10f69 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
10f6a 20 69 6e 20 63 61 63 68 65 20 6f 72 20 69 66 20   in cache or if 
10f6b 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72  a disk I/O error
10f6c 20 0a 2a 2a 20 68 61 73 20 65 76 65 72 20 68 61   .** has ever ha
10f6d 70 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  ppened..*/.SQLIT
10f6e 45 5f 50 52 49 56 41 54 45 20 44 62 50 61 67 65  E_PRIVATE DbPage
10f6f 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f   *sqlite3PagerLo
10f70 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61 67  okup(Pager *pPag
10f71 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a  er, Pgno pgno){.
10f72 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 30    PgHdr *pPg = 0
10f73 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
10f74 65 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  er!=0 );.  asser
10f75 74 28 20 70 67 6e 6f 21 3d 30 20 29 3b 0a 0a 20  t( pgno!=0 );.. 
10f76 20 69 66 28 20 28 70 50 61 67 65 72 2d 3e 73 74   if( (pPager->st
10f77 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f 43  ate!=PAGER_UNLOC
10f78 4b 29 0a 20 20 20 26 26 20 28 70 50 61 67 65 72  K).   && (pPager
10f79 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54  ->errCode==SQLIT
10f7a 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e  E_OK || pPager->
10f7b 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f  errCode==SQLITE_
10f7c 46 55 4c 4c 29 0a 20 20 29 7b 0a 20 20 20 20 73  FULL).  ){.    s
10f7d 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63  qlite3PcacheFetc
10f7e 68 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  h(pPager->pPCach
10f7f 65 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 70 50 67  e, pgno, 0, &pPg
10f80 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  );.  }..  return
10f81 20 70 50 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   pPg;.}../*.** R
10f82 65 6c 65 61 73 65 20 61 20 70 61 67 65 2e 0a 2a  elease a page..*
10f83 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 75 6d 62  *.** If the numb
10f84 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73  er of references
10f85 20 74 6f 20 74 68 65 20 70 61 67 65 20 64 72 6f   to the page dro
10f86 70 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 6e 20  p to zero, then 
10f87 74 68 65 0a 2a 2a 20 70 61 67 65 20 69 73 20 61  the.** page is a
10f88 64 64 65 64 20 74 6f 20 74 68 65 20 4c 52 55 20  dded to the LRU 
10f89 6c 69 73 74 2e 20 20 57 68 65 6e 20 61 6c 6c 20  list.  When all 
10f8a 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6c  references to al
10f8b 6c 20 70 61 67 65 73 0a 2a 2a 20 61 72 65 20 72  l pages.** are r
10f8c 65 6c 65 61 73 65 64 2c 20 61 20 72 6f 6c 6c 62  eleased, a rollb
10f8d 61 63 6b 20 6f 63 63 75 72 73 20 61 6e 64 20 74  ack occurs and t
10f8e 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  he lock on the d
10f8f 61 74 61 62 61 73 65 20 69 73 0a 2a 2a 20 72 65  atabase is.** re
10f90 6d 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  moved..*/.SQLITE
10f91 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
10f92 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 44  ite3PagerUnref(D
10f93 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 69  bPage *pPg){.  i
10f94 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 50 61  f( pPg ){.    Pa
10f95 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50  ger *pPager = pP
10f96 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 20 20 73  g->pPager;.    s
10f97 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65  qlite3PcacheRele
10f98 61 73 65 28 70 50 67 29 3b 0a 20 20 20 20 70 61  ase(pPg);.    pa
10f99 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65  gerUnlockIfUnuse
10f9a 64 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20  d(pPager);.  }. 
10f9b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
10f9c 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61  K;.}../*.** Crea
10f9d 74 65 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  te a journal fil
10f9e 65 20 66 6f 72 20 70 50 61 67 65 72 2e 20 20 54  e for pPager.  T
10f9f 68 65 72 65 20 73 68 6f 75 6c 64 20 61 6c 72 65  here should alre
10fa0 61 64 79 20 62 65 20 61 20 52 45 53 45 52 56 45  ady be a RESERVE
10fa1 44 0a 2a 2a 20 6f 72 20 45 58 43 4c 55 53 49 56  D.** or EXCLUSIV
10fa2 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61  E lock on the da
10fa3 74 61 62 61 73 65 20 66 69 6c 65 20 77 68 65 6e  tabase file when
10fa4 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
10fa5 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 52   called..**.** R
10fa6 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
10fa7 69 66 20 65 76 65 72 79 74 68 69 6e 67 2e 20 20  if everything.  
10fa8 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  Return an error 
10fa9 63 6f 64 65 20 61 6e 64 20 72 65 6c 65 61 73 65  code and release
10faa 20 74 68 65 0a 2a 2a 20 77 72 69 74 65 20 6c 6f   the.** write lo
10fab 63 6b 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67  ck if anything g
10fac 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74  oes wrong..*/.st
10fad 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 6f  atic int pager_o
10fae 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 50 61 67 65  pen_journal(Page
10faf 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 73 71  r *pPager){.  sq
10fb0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20  lite3_vfs *pVfs 
10fb1 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a  = pPager->pVfs;.
10fb2 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 53    int flags = (S
10fb3 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
10fb4 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e  RITE|SQLITE_OPEN
10fb5 5f 45 58 43 4c 55 53 49 56 45 7c 53 51 4c 49 54  _EXCLUSIVE|SQLIT
10fb6 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a  E_OPEN_CREATE);.
10fb7 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73  .  int rc;.  ass
10fb8 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61  ert( pPager->sta
10fb9 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56  te>=PAGER_RESERV
10fba 45 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ED );.  assert( 
10fbb 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e  pPager->useJourn
10fbc 61 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  al );.  assert( 
10fbd 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e  pPager->pInJourn
10fbe 61 6c 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74  al==0 );.  sqlit
10fbf 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
10fc0 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 70  (pPager, 0);.  p
10fc1 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
10fc2 6c 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65  l = sqlite3Bitve
10fc3 63 43 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e  cCreate(pPager->
10fc4 64 62 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 70  dbSize);.  if( p
10fc5 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
10fc6 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d  l==0 ){.    rc =
10fc7 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
10fc8 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 5f 74     goto failed_t
10fc9 6f 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 3b 0a  o_open_journal;.
10fca 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65    }..  if( pPage
10fcb 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 3d 3d  r->journalOpen==
10fcc 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61  0 ){.    if( pPa
10fcd 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b  ger->tempFile ){
10fce 0a 20 20 20 20 20 20 66 6c 61 67 73 20 7c 3d 20  .      flags |= 
10fcf 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c  (SQLITE_OPEN_DEL
10fd0 45 54 45 4f 4e 43 4c 4f 53 45 7c 53 51 4c 49 54  ETEONCLOSE|SQLIT
10fd1 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52  E_OPEN_TEMP_JOUR
10fd2 4e 41 4c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  NAL);.    }else{
10fd3 0a 20 20 20 20 20 20 66 6c 61 67 73 20 7c 3d 20  .      flags |= 
10fd4 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49  (SQLITE_OPEN_MAI
10fd5 4e 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20  N_JOURNAL);.    
10fd6 7d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72  }.    if( pPager
10fd7 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50  ->journalMode==P
10fd8 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
10fd9 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20 20 20  _MEMORY ){.     
10fda 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e   sqlite3MemJourn
10fdb 61 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  alOpen(pPager->j
10fdc 66 64 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  fd);.      rc = 
10fdd 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
10fde 65 6c 73 65 7b 0a 23 69 66 64 65 66 20 53 51 4c  else{.#ifdef SQL
10fdf 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49  ITE_ENABLE_ATOMI
10fe0 43 5f 57 52 49 54 45 0a 20 20 20 20 20 20 72 63  C_WRITE.      rc
10fe1 20 3d 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61   = sqlite3Journa
10fe2 6c 4f 70 65 6e 28 0a 20 20 20 20 20 20 20 20 20  lOpen(.         
10fe3 20 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a   pVfs, pPager->z
10fe4 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d  Journal, pPager-
10fe5 3e 6a 66 64 2c 20 66 6c 61 67 73 2c 20 6a 72 6e  >jfd, flags, jrn
10fe6 6c 42 75 66 66 65 72 53 69 7a 65 28 70 50 61 67  lBufferSize(pPag
10fe7 65 72 29 0a 20 20 20 20 20 20 29 3b 0a 23 65 6c  er).      );.#el
10fe8 73 65 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  se.      rc = sq
10fe9 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73  lite3OsOpen(pVfs
10fea 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e  , pPager->zJourn
10feb 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c  al, pPager->jfd,
10fec 20 66 6c 61 67 73 2c 20 30 29 3b 0a 23 65 6e 64   flags, 0);.#end
10fed 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  if.    }.    ass
10fee 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ert( rc!=SQLITE_
10fef 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 66  OK || pPager->jf
10ff0 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 3b 0a 20  d->pMethods );. 
10ff1 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
10ff2 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 70  alOff = 0;.    p
10ff3 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72  Pager->setMaster
10ff4 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72   = 0;.    pPager
10ff5 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30  ->journalHdr = 0
10ff6 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
10ff7 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
10ff8 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
10ff9 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 20  NOMEM ){.       
10ffa 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65   sqlite3OsDelete
10ffb 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a  (pVfs, pPager->z
10ffc 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20  Journal, 0);.   
10ffd 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20     }.      goto 
10ffe 66 61 69 6c 65 64 5f 74 6f 5f 6f 70 65 6e 5f 6a  failed_to_open_j
10fff 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 7d 0a 20 20  ournal;.    }.  
11000 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  }.  pPager->jour
11001 6e 61 6c 4f 70 65 6e 20 3d 20 31 3b 0a 20 20 70  nalOpen = 1;.  p
11002 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74  Pager->journalSt
11003 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 70 50 61  arted = 0;.  pPa
11004 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20  ger->needSync = 
11005 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 52 65  0;.  pPager->nRe
11006 63 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 50 61  c = 0;.  if( pPa
11007 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a  ger->errCode ){.
11008 20 20 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d      rc = pPager-
11009 3e 65 72 72 43 6f 64 65 3b 0a 20 20 20 20 67 6f  >errCode;.    go
1100a 74 6f 20 66 61 69 6c 65 64 5f 74 6f 5f 6f 70 65  to failed_to_ope
1100b 6e 5f 6a 6f 75 72 6e 61 6c 3b 0a 20 20 7d 0a 20  n_journal;.  }. 
1100c 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62 53   pPager->origDbS
1100d 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62  ize = pPager->db
1100e 53 69 7a 65 3b 0a 0a 20 20 72 63 20 3d 20 77 72  Size;..  rc = wr
1100f 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50  iteJournalHdr(pP
11010 61 67 65 72 29 3b 0a 0a 20 20 69 66 28 20 70 50  ager);..  if( pP
11011 61 67 65 72 2d 3e 73 74 6d 74 41 75 74 6f 6f 70  ager->stmtAutoop
11012 65 6e 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45  en && rc==SQLITE
11013 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  _OK ){.    rc = 
11014 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d 74  sqlite3PagerStmt
11015 42 65 67 69 6e 28 70 50 61 67 65 72 29 3b 0a 20  Begin(pPager);. 
11016 20 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c   }.  if( rc!=SQL
11017 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51  ITE_OK && rc!=SQ
11018 4c 49 54 45 5f 4e 4f 4d 45 4d 20 26 26 20 72 63  LITE_NOMEM && rc
11019 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e  !=SQLITE_IOERR_N
1101a 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 72 63 20 3d  OMEM ){.    rc =
1101b 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73   pager_end_trans
1101c 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 30  action(pPager, 0
1101d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
1101e 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1101f 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55    rc = SQLITE_FU
11020 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  LL;.    }.  }.  
11021 72 65 74 75 72 6e 20 72 63 3b 0a 0a 66 61 69 6c  return rc;..fail
11022 65 64 5f 74 6f 5f 6f 70 65 6e 5f 6a 6f 75 72 6e  ed_to_open_journ
11023 61 6c 3a 0a 20 20 73 71 6c 69 74 65 33 42 69 74  al:.  sqlite3Bit
11024 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65  vecDestroy(pPage
11025 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a  r->pInJournal);.
11026 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75    pPager->pInJou
11027 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75  rnal = 0;.  retu
11028 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
11029 41 63 71 75 69 72 65 20 61 20 77 72 69 74 65 2d  Acquire a write-
1102a 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
1102b 62 61 73 65 2e 20 20 54 68 65 20 6c 6f 63 6b 20  base.  The lock 
1102c 69 73 20 72 65 6d 6f 76 65 64 20 77 68 65 6e 0a  is removed when.
1102d 2a 2a 20 74 68 65 20 61 6e 79 20 6f 66 20 74 68  ** the any of th
1102e 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 68 61 70 70  e following happ
1102f 65 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 73  en:.**.**   *  s
11030 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69  qlite3PagerCommi
11031 74 50 68 61 73 65 54 77 6f 28 29 20 69 73 20 63  tPhaseTwo() is c
11032 61 6c 6c 65 64 2e 0a 2a 2a 20 20 20 2a 20 20 73  alled..**   *  s
11033 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62  qlite3PagerRollb
11034 61 63 6b 28 29 20 69 73 20 63 61 6c 6c 65 64 2e  ack() is called.
11035 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33  .**   *  sqlite3
11036 50 61 67 65 72 43 6c 6f 73 65 28 29 20 69 73 20  PagerClose() is 
11037 63 61 6c 6c 65 64 2e 0a 2a 2a 20 20 20 2a 20 20  called..**   *  
11038 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
11039 66 28 29 20 69 73 20 63 61 6c 6c 65 64 20 74 6f  f() is called to
1103a 20 6f 6e 20 65 76 65 72 79 20 6f 75 74 73 74 61   on every outsta
1103b 6e 64 69 6e 67 20 70 61 67 65 2e 0a 2a 2a 0a 2a  nding page..**.*
1103c 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61  * The first para
1103d 6d 65 74 65 72 20 74 6f 20 74 68 69 73 20 72 6f  meter to this ro
1103e 75 74 69 6e 65 20 69 73 20 61 20 70 6f 69 6e 74  utine is a point
1103f 65 72 20 74 6f 20 61 6e 79 20 6f 70 65 6e 20 70  er to any open p
11040 61 67 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61  age of the.** da
11041 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4e 6f  tabase file.  No
11042 74 68 69 6e 67 20 63 68 61 6e 67 65 73 20 61 62  thing changes ab
11043 6f 75 74 20 74 68 65 20 70 61 67 65 20 2d 20 69  out the page - i
11044 74 20 69 73 20 75 73 65 64 20 6d 65 72 65 6c 79  t is used merely
11045 20 74 6f 0a 2a 2a 20 61 63 71 75 69 72 65 20 61   to.** acquire a
11046 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
11047 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65 20  Pager structure 
11048 61 6e 64 20 61 73 20 70 72 6f 6f 66 20 74 68 61  and as proof tha
11049 74 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 61 6c  t there is.** al
1104a 72 65 61 64 79 20 61 20 72 65 61 64 2d 6c 6f 63  ready a read-loc
1104b 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
1104c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63  e..**.** The sec
1104d 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 6e  ond parameter in
1104e 64 69 63 61 74 65 73 20 68 6f 77 20 6d 75 63 68  dicates how much
1104f 20 73 70 61 63 65 20 69 6e 20 62 79 74 65 73 20   space in bytes 
11050 74 6f 20 72 65 73 65 72 76 65 20 66 6f 72 20 61  to reserve for a
11051 0a 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  .** master journ
11052 61 6c 20 66 69 6c 65 2d 6e 61 6d 65 20 61 74 20  al file-name at 
11053 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65  the start of the
11054 20 6a 6f 75 72 6e 61 6c 20 77 68 65 6e 20 69 74   journal when it
11055 20 69 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a   is created..**.
11056 2a 2a 20 41 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ** A journal fil
11057 65 20 69 73 20 6f 70 65 6e 65 64 20 69 66 20 74  e is opened if t
11058 68 69 73 20 69 73 20 6e 6f 74 20 61 20 74 65 6d  his is not a tem
11059 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 20 46 6f  porary file.  Fo
1105a 72 20 74 65 6d 70 6f 72 61 72 79 0a 2a 2a 20 66  r temporary.** f
1105b 69 6c 65 73 2c 20 74 68 65 20 6f 70 65 6e 69 6e  iles, the openin
1105c 67 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  g of the journal
1105d 20 66 69 6c 65 20 69 73 20 64 65 66 65 72 72 65   file is deferre
1105e 64 20 75 6e 74 69 6c 20 74 68 65 72 65 20 69 73  d until there is
1105f 20 61 6e 0a 2a 2a 20 61 63 74 75 61 6c 20 6e 65   an.** actual ne
11060 65 64 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74  ed to write to t
11061 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a  he journal..**.*
11062 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  * If the databas
11063 65 20 69 73 20 61 6c 72 65 61 64 79 20 72 65 73  e is already res
11064 65 72 76 65 64 20 66 6f 72 20 77 72 69 74 69 6e  erved for writin
11065 67 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  g, this routine 
11066 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  is a no-op..**.*
11067 2a 20 49 66 20 65 78 46 6c 61 67 20 69 73 20 74  * If exFlag is t
11068 72 75 65 2c 20 67 6f 20 61 68 65 61 64 20 61 6e  rue, go ahead an
11069 64 20 67 65 74 20 61 6e 20 45 58 43 4c 55 53 49  d get an EXCLUSI
1106a 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66  VE lock on the f
1106b 69 6c 65 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65  ile.** immediate
1106c 6c 79 20 69 6e 73 74 65 61 64 20 6f 66 20 77 61  ly instead of wa
1106d 69 74 69 6e 67 20 75 6e 74 69 6c 20 77 65 20 74  iting until we t
1106e 72 79 20 74 6f 20 66 6c 75 73 68 20 74 68 65 20  ry to flush the 
1106f 63 61 63 68 65 2e 20 20 54 68 65 0a 2a 2a 20 65  cache.  The.** e
11070 78 46 6c 61 67 20 69 73 20 69 67 6e 6f 72 65 64  xFlag is ignored
11071 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f   if a transactio
11072 6e 20 69 73 20 61 6c 72 65 61 64 79 20 61 63 74  n is already act
11073 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ive..*/.SQLITE_P
11074 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
11075 65 33 50 61 67 65 72 42 65 67 69 6e 28 44 62 50  e3PagerBegin(DbP
11076 61 67 65 20 2a 70 50 67 2c 20 69 6e 74 20 65 78  age *pPg, int ex
11077 46 6c 61 67 29 7b 0a 20 20 50 61 67 65 72 20 2a  Flag){.  Pager *
11078 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50  pPager = pPg->pP
11079 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d  ager;.  int rc =
1107a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73   SQLITE_OK;.  as
1107b 73 65 72 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e  sert( pPg->nRef>
1107c 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
1107d 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41  Pager->state!=PA
1107e 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20  GER_UNLOCK );.  
1107f 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  if( pPager->stat
11080 65 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20  e==PAGER_SHARED 
11081 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
11082 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
11083 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  l==0 );.    asse
11084 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20  rt( !MEMDB );.  
11085 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 41    sqlite3PcacheA
11086 73 73 65 72 74 46 6c 61 67 73 28 70 50 61 67 65  ssertFlags(pPage
11087 72 2d 3e 70 50 43 61 63 68 65 2c 20 30 2c 20 50  r->pPCache, 0, P
11088 47 48 44 52 5f 49 4e 5f 4a 4f 55 52 4e 41 4c 29  GHDR_IN_JOURNAL)
11089 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
1108a 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d  e3OsLock(pPager-
1108b 3e 66 64 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f  >fd, RESERVED_LO
1108c 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  CK);.    if( rc=
1108d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1108e 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74      pPager->stat
1108f 65 20 3d 20 50 41 47 45 52 5f 52 45 53 45 52 56  e = PAGER_RESERV
11090 45 44 3b 0a 20 20 20 20 20 20 69 66 28 20 65 78  ED;.      if( ex
11091 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20  Flag ){.        
11092 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74 5f  rc = pager_wait_
11093 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20  on_lock(pPager, 
11094 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b  EXCLUSIVE_LOCK);
11095 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
11096 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
11097 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65  E_OK ){.      re
11098 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
11099 20 20 20 70 50 61 67 65 72 2d 3e 64 69 72 74 79     pPager->dirty
1109a 43 61 63 68 65 20 3d 20 30 3b 0a 20 20 20 20 50  Cache = 0;.    P
1109b 41 47 45 52 54 52 41 43 45 32 28 22 54 52 41 4e  AGERTRACE2("TRAN
1109c 53 41 43 54 49 4f 4e 20 25 64 5c 6e 22 2c 20 50  SACTION %d\n", P
1109d 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 3b  AGERID(pPager));
1109e 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d  .    if( pPager-
1109f 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 26 26 20 21  >useJournal && !
110a0 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65  pPager->tempFile
110a1 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20 70  .           && p
110a2 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
110a3 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de!=PAGER_JOURNA
110a4 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b 0a 20 20 20  LMODE_OFF ){.   
110a5 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 6f 70     rc = pager_op
110a6 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50 61 67 65  en_journal(pPage
110a7 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  r);.    }.  }els
110a8 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f  e if( pPager->jo
110a9 75 72 6e 61 6c 4f 70 65 6e 20 26 26 20 70 50 61  urnalOpen && pPa
110aa 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d  ger->journalOff=
110ab 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69  =0 ){.    /* Thi
110ac 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 74  s happens when t
110ad 68 65 20 70 61 67 65 72 20 77 61 73 20 69 6e 20  he pager was in 
110ae 65 78 63 6c 75 73 69 76 65 2d 61 63 63 65 73 73  exclusive-access
110af 20 6d 6f 64 65 20 74 68 65 20 6c 61 73 74 0a 20   mode the last. 
110b0 20 20 20 2a 2a 20 74 69 6d 65 20 61 20 28 72 65     ** time a (re
110b1 61 64 20 6f 72 20 77 72 69 74 65 29 20 74 72 61  ad or write) tra
110b2 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 73 75 63  nsaction was suc
110b3 63 65 73 73 66 75 6c 6c 79 20 63 6f 6e 63 6c 75  cessfully conclu
110b4 64 65 64 0a 20 20 20 20 2a 2a 20 62 79 20 74 68  ded.    ** by th
110b5 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49  is connection. I
110b6 6e 73 74 65 61 64 20 6f 66 20 64 65 6c 65 74 69  nstead of deleti
110b7 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ng the journal f
110b8 69 6c 65 20 69 74 20 77 61 73 20 0a 20 20 20 20  ile it was .    
110b9 2a 2a 20 6b 65 70 74 20 6f 70 65 6e 20 61 6e 64  ** kept open and
110ba 20 65 69 74 68 65 72 20 77 61 73 20 74 72 75 6e   either was trun
110bb 63 61 74 65 64 20 74 6f 20 30 20 62 79 74 65 73  cated to 0 bytes
110bc 20 6f 72 20 69 74 73 20 68 65 61 64 65 72 20 77   or its header w
110bd 61 73 0a 20 20 20 20 2a 2a 20 6f 76 65 72 77 72  as.    ** overwr
110be 69 74 74 65 6e 20 77 69 74 68 20 7a 65 72 6f 73  itten with zeros
110bf 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73  ..    */.    ass
110c0 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 52 65  ert( pPager->nRe
110c1 63 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  c==0 );.    asse
110c2 72 74 28 20 70 50 61 67 65 72 2d 3e 6f 72 69 67  rt( pPager->orig
110c3 44 62 53 69 7a 65 3d 3d 30 20 29 3b 0a 20 20 20  DbSize==0 );.   
110c4 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
110c5 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29  >pInJournal==0 )
110c6 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67  ;.    sqlite3Pag
110c7 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67  erPagecount(pPag
110c8 65 72 2c 20 30 29 3b 0a 20 20 20 20 70 50 61 67  er, 0);.    pPag
110c9 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d  er->pInJournal =
110ca 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72   sqlite3BitvecCr
110cb 65 61 74 65 28 20 70 50 61 67 65 72 2d 3e 64 62  eate( pPager->db
110cc 53 69 7a 65 20 29 3b 0a 20 20 20 20 69 66 28 20  Size );.    if( 
110cd 21 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72  !pPager->pInJour
110ce 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20  nal ){.      rc 
110cf 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
110d0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
110d1 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62 53   pPager->origDbS
110d2 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62  ize = pPager->db
110d3 53 69 7a 65 3b 0a 20 20 20 20 20 20 72 63 20 3d  Size;.      rc =
110d4 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72   writeJournalHdr
110d5 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a  (pPager);.    }.
110d6 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 70    }.  assert( !p
110d7 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70  Pager->journalOp
110d8 65 6e 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f  en || pPager->jo
110d9 75 72 6e 61 6c 4f 66 66 3e 30 20 7c 7c 20 72 63  urnalOff>0 || rc
110da 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  !=SQLITE_OK );. 
110db 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
110dc 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 70 61 67 65  *.** Make a page
110dd 20 64 69 72 74 79 2e 20 20 53 65 74 20 69 74 73   dirty.  Set its
110de 20 64 69 72 74 79 20 66 6c 61 67 20 61 6e 64 20   dirty flag and 
110df 61 64 64 20 69 74 20 74 6f 20 74 68 65 20 64 69  add it to the di
110e0 72 74 79 0a 2a 2a 20 70 61 67 65 20 6c 69 73 74  rty.** page list
110e1 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
110e2 20 6d 61 6b 65 44 69 72 74 79 28 50 67 48 64 72   makeDirty(PgHdr
110e3 20 2a 70 50 67 29 7b 0a 20 20 73 71 6c 69 74 65   *pPg){.  sqlite
110e4 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79  3PcacheMakeDirty
110e5 28 70 50 67 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  (pPg);.}../*.** 
110e6 4d 61 6b 65 20 61 20 70 61 67 65 20 63 6c 65 61  Make a page clea
110e7 6e 2e 20 20 43 6c 65 61 72 20 69 74 73 20 64 69  n.  Clear its di
110e8 72 74 79 20 62 69 74 20 61 6e 64 20 72 65 6d 6f  rty bit and remo
110e9 76 65 20 69 74 20 66 72 6f 6d 20 74 68 65 0a 2a  ve it from the.*
110ea 2a 20 64 69 72 74 79 20 70 61 67 65 20 6c 69 73  * dirty page lis
110eb 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
110ec 64 20 6d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64  d makeClean(PgHd
110ed 72 20 2a 70 50 67 29 7b 0a 20 20 73 71 6c 69 74  r *pPg){.  sqlit
110ee 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61  e3PcacheMakeClea
110ef 6e 28 70 50 67 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  n(pPg);.}.../*.*
110f0 2a 20 4d 61 72 6b 20 61 20 64 61 74 61 20 70 61  * Mark a data pa
110f1 67 65 20 61 73 20 77 72 69 74 65 61 62 6c 65 2e  ge as writeable.
110f2 20 20 54 68 65 20 70 61 67 65 20 69 73 20 77 72    The page is wr
110f3 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a  itten into the j
110f4 6f 75 72 6e 61 6c 20 0a 2a 2a 20 69 66 20 69 74  ournal .** if it
110f5 20 69 73 20 6e 6f 74 20 74 68 65 72 65 20 61 6c   is not there al
110f6 72 65 61 64 79 2e 20 20 54 68 69 73 20 72 6f 75  ready.  This rou
110f7 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c  tine must be cal
110f8 6c 65 64 20 62 65 66 6f 72 65 20 6d 61 6b 69 6e  led before makin
110f9 67 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f 20  g.** changes to 
110fa 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  a page..**.** Th
110fb 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 69  e first time thi
110fc 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
110fd 6c 65 64 2c 20 74 68 65 20 70 61 67 65 72 20 63  led, the pager c
110fe 72 65 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20  reates a new.** 
110ff 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 61 63 71 75  journal and acqu
11100 69 72 65 73 20 61 20 52 45 53 45 52 56 45 44 20  ires a RESERVED 
11101 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
11102 62 61 73 65 2e 20 20 49 66 20 74 68 65 20 52 45  base.  If the RE
11103 53 45 52 56 45 44 0a 2a 2a 20 6c 6f 63 6b 20 63  SERVED.** lock c
11104 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 63 71 75  ould not be acqu
11105 69 72 65 64 2c 20 74 68 69 73 20 72 6f 75 74 69  ired, this routi
11106 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  ne returns SQLIT
11107 45 5f 42 55 53 59 2e 20 20 54 68 65 0a 2a 2a 20  E_BUSY.  The.** 
11108 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 20  calling routine 
11109 6d 75 73 74 20 63 68 65 63 6b 20 66 6f 72 20 74  must check for t
1110a 68 61 74 20 72 65 74 75 72 6e 20 76 61 6c 75 65  hat return value
1110b 20 61 6e 64 20 62 65 20 63 61 72 65 66 75 6c 20   and be careful 
1110c 6e 6f 74 20 74 6f 0a 2a 2a 20 63 68 61 6e 67 65  not to.** change
1110d 20 61 6e 79 20 70 61 67 65 20 64 61 74 61 20 75   any page data u
1110e 6e 74 69 6c 20 74 68 69 73 20 72 6f 75 74 69 6e  ntil this routin
1110f 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  e returns SQLITE
11110 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  _OK..**.** If th
11111 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63  e journal file c
11112 6f 75 6c 64 20 6e 6f 74 20 62 65 20 77 72 69 74  ould not be writ
11113 74 65 6e 20 62 65 63 61 75 73 65 20 74 68 65 20  ten because the 
11114 64 69 73 6b 20 69 73 20 66 75 6c 6c 2c 0a 2a 2a  disk is full,.**
11115 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
11116 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  ne returns SQLIT
11117 45 5f 46 55 4c 4c 20 61 6e 64 20 64 6f 65 73 20  E_FULL and does 
11118 61 6e 20 69 6d 6d 65 64 69 61 74 65 20 72 6f 6c  an immediate rol
11119 6c 62 61 63 6b 2e 0a 2a 2a 20 41 6c 6c 20 73 75  lback..** All su
1111a 62 73 65 71 75 65 6e 74 20 77 72 69 74 65 20 61  bsequent write a
1111b 74 74 65 6d 70 74 73 20 61 6c 73 6f 20 72 65 74  ttempts also ret
1111c 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20  urn SQLITE_FULL 
1111d 75 6e 74 69 6c 20 74 68 65 72 65 0a 2a 2a 20 69  until there.** i
1111e 73 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  s a call to sqli
1111f 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 28 29  te3PagerCommit()
11120 20 6f 72 20 73 71 6c 69 74 65 33 50 61 67 65 72   or sqlite3Pager
11121 52 6f 6c 6c 62 61 63 6b 28 29 20 74 6f 0a 2a 2a  Rollback() to.**
11122 20 72 65 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69   reset..*/.stati
11123 63 20 69 6e 74 20 70 61 67 65 72 5f 77 72 69 74  c int pager_writ
11124 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20  e(PgHdr *pPg){. 
11125 20 76 6f 69 64 20 2a 70 44 61 74 61 20 3d 20 70   void *pData = p
11126 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 50 61 67  Pg->pData;.  Pag
11127 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67  er *pPager = pPg
11128 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20  ->pPager;.  int 
11129 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1112a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20  .  /* Check for 
1112b 65 72 72 6f 72 73 0a 20 20 2a 2f 0a 20 20 69 66  errors.  */.  if
1112c 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  ( pPager->errCod
1112d 65 20 29 7b 20 0a 20 20 20 20 72 65 74 75 72 6e  e ){ .    return
1112e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
1112f 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 67  ;.  }.  if( pPag
11130 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a  er->readOnly ){.
11131 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
11132 45 5f 50 45 52 4d 3b 0a 20 20 7d 0a 0a 20 20 61  E_PERM;.  }..  a
11133 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e  ssert( !pPager->
11134 73 65 74 4d 61 73 74 65 72 20 29 3b 0a 0a 20 20  setMaster );..  
11135 43 48 45 43 4b 5f 50 41 47 45 28 70 50 67 29 3b  CHECK_PAGE(pPg);
11136 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 70  ..  /* If this p
11137 61 67 65 20 77 61 73 20 70 72 65 76 69 6f 75 73  age was previous
11138 6c 79 20 61 63 71 75 69 72 65 64 20 77 69 74 68  ly acquired with
11139 20 6e 6f 43 6f 6e 74 65 6e 74 3d 3d 31 2c 20 74   noContent==1, t
1113a 68 61 74 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 77  hat means.  ** w
1113b 65 20 64 69 64 6e 27 74 20 72 65 61 6c 6c 79 20  e didn't really 
1113c 72 65 61 64 20 69 6e 20 74 68 65 20 63 6f 6e 74  read in the cont
1113d 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67 65 2e  ent of the page.
1113e 20 20 54 68 69 73 20 63 61 6e 20 68 61 70 70 65    This can happe
1113f 6e 0a 20 20 2a 2a 20 28 66 6f 72 20 65 78 61 6d  n.  ** (for exam
11140 70 6c 65 29 20 77 68 65 6e 20 74 68 65 20 70 61  ple) when the pa
11141 67 65 20 69 73 20 62 65 69 6e 67 20 6d 6f 76 65  ge is being move
11142 64 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73  d to the freelis
11143 74 2e 20 20 42 75 74 0a 20 20 2a 2a 20 6e 6f 77  t.  But.  ** now
11144 20 77 65 20 61 72 65 20 28 70 65 72 68 61 70 73   we are (perhaps
11145 29 20 6d 6f 76 69 6e 67 20 74 68 65 20 70 61 67  ) moving the pag
11146 65 20 6f 66 66 20 6f 66 20 74 68 65 20 66 72 65  e off of the fre
11147 65 6c 69 73 74 20 66 6f 72 0a 20 20 2a 2a 20 72  elist for.  ** r
11148 65 75 73 65 20 61 6e 64 20 77 65 20 6e 65 65 64  euse and we need
11149 20 74 6f 20 6b 6e 6f 77 20 69 74 73 20 6f 72 69   to know its ori
1114a 67 69 6e 61 6c 20 63 6f 6e 74 65 6e 74 20 73 6f  ginal content so
1114b 20 74 68 61 74 20 63 6f 6e 74 65 6e 74 0a 20 20   that content.  
1114c 2a 2a 20 63 61 6e 20 62 65 20 73 74 6f 72 65 64  ** can be stored
1114d 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b   in the rollback
1114e 20 6a 6f 75 72 6e 61 6c 2e 20 20 53 6f 20 64 6f   journal.  So do
1114f 20 74 68 65 20 72 65 61 64 20 61 74 20 74 68 69   the read at thi
11150 73 0a 20 20 2a 2a 20 74 69 6d 65 2e 0a 20 20 2a  s.  ** time..  *
11151 2f 0a 20 20 72 63 20 3d 20 70 61 67 65 72 5f 67  /.  rc = pager_g
11152 65 74 5f 63 6f 6e 74 65 6e 74 28 70 50 67 29 3b  et_content(pPg);
11153 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20  .  if( rc ){.   
11154 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
11155 0a 20 20 2f 2a 20 4d 61 72 6b 20 74 68 65 20 70  .  /* Mark the p
11156 61 67 65 20 61 73 20 64 69 72 74 79 2e 20 20 49  age as dirty.  I
11157 66 20 74 68 65 20 70 61 67 65 20 68 61 73 20 61  f the page has a
11158 6c 72 65 61 64 79 20 62 65 65 6e 20 77 72 69 74  lready been writ
11159 74 65 6e 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20  ten.  ** to the 
1115a 6a 6f 75 72 6e 61 6c 20 74 68 65 6e 20 77 65 20  journal then we 
1115b 63 61 6e 20 72 65 74 75 72 6e 20 72 69 67 68 74  can return right
1115c 20 61 77 61 79 2e 0a 20 20 2a 2f 0a 20 20 6d 61   away..  */.  ma
1115d 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0a 20 20  keDirty(pPg);.  
1115e 69 66 28 20 28 70 50 67 2d 3e 66 6c 61 67 73 26  if( (pPg->flags&
1115f 50 47 48 44 52 5f 49 4e 5f 4a 4f 55 52 4e 41 4c  PGHDR_IN_JOURNAL
11160 29 0a 20 20 20 26 26 20 28 70 61 67 65 49 6e 53  ).   && (pageInS
11161 74 61 74 65 6d 65 6e 74 28 70 50 67 29 20 7c 7c  tatement(pPg) ||
11162 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55   pPager->stmtInU
11163 73 65 3d 3d 30 29 20 0a 20 20 29 7b 0a 20 20 20  se==0) .  ){.   
11164 20 70 50 61 67 65 72 2d 3e 64 69 72 74 79 43 61   pPager->dirtyCa
11165 63 68 65 20 3d 20 31 3b 0a 20 20 20 20 70 50 61  che = 1;.    pPa
11166 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20  ger->dbModified 
11167 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 0a 20  = 1;.  }else{.. 
11168 20 20 20 2f 2a 20 49 66 20 77 65 20 67 65 74 20     /* If we get 
11169 74 68 69 73 20 66 61 72 2c 20 69 74 20 6d 65 61  this far, it mea
1116a 6e 73 20 74 68 61 74 20 74 68 65 20 70 61 67 65  ns that the page
1116b 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 20 20 20   needs to be.   
1116c 20 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 74   ** written to t
1116d 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a  he transaction j
1116e 6f 75 72 6e 61 6c 20 6f 72 20 74 68 65 20 63 6b  ournal or the ck
1116f 65 63 6b 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c  eckpoint journal
11170 0a 20 20 20 20 2a 2a 20 6f 72 20 62 6f 74 68 2e  .    ** or both.
11171 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46  .    **.    ** F
11172 69 72 73 74 20 63 68 65 63 6b 20 74 6f 20 73 65  irst check to se
11173 65 20 74 68 61 74 20 74 68 65 20 74 72 61 6e 73  e that the trans
11174 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 65  action journal e
11175 78 69 73 74 73 20 61 6e 64 0a 20 20 20 20 2a 2a  xists and.    **
11176 20 63 72 65 61 74 65 20 69 74 20 69 66 20 69 74   create it if it
11177 20 64 6f 65 73 20 6e 6f 74 2e 0a 20 20 20 20 2a   does not..    *
11178 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  /.    assert( pP
11179 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47  ager->state!=PAG
1117a 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 20  ER_UNLOCK );.   
1117b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
1117c 65 72 42 65 67 69 6e 28 70 50 67 2c 20 30 29 3b  erBegin(pPg, 0);
1117d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
1117e 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1117f 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
11180 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
11181 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45  ger->state>=PAGE
11182 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20  R_RESERVED );.  
11183 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6a    if( !pPager->j
11184 6f 75 72 6e 61 6c 4f 70 65 6e 20 26 26 20 70 50  ournalOpen && pP
11185 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c  ager->useJournal
11186 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 70 50  .          && pP
11187 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
11188 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e!=PAGER_JOURNAL
11189 4d 4f 44 45 5f 4f 46 46 20 29 7b 0a 20 20 20 20  MODE_OFF ){.    
1118a 20 20 72 63 20 3d 20 70 61 67 65 72 5f 6f 70 65    rc = pager_ope
1118b 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72  n_journal(pPager
1118c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
1118d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
1118e 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
1118f 20 20 70 50 61 67 65 72 2d 3e 64 69 72 74 79 43    pPager->dirtyC
11190 61 63 68 65 20 3d 20 31 3b 0a 20 20 20 20 70 50  ache = 1;.    pP
11191 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64  ager->dbModified
11192 20 3d 20 31 3b 0a 20 20 0a 20 20 20 20 2f 2a 20   = 1;.  .    /* 
11193 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  The transaction 
11194 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20 65 78 69 73  journal now exis
11195 74 73 20 61 6e 64 20 77 65 20 68 61 76 65 20 61  ts and we have a
11196 20 52 45 53 45 52 56 45 44 20 6f 72 20 61 6e 0a   RESERVED or an.
11197 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49 56 45      ** EXCLUSIVE
11198 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 6d 61 69   lock on the mai
11199 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  n database file.
1119a 20 20 57 72 69 74 65 20 74 68 65 20 63 75 72 72    Write the curr
1119b 65 6e 74 20 70 61 67 65 20 74 6f 0a 20 20 20 20  ent page to.    
1119c 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ** the transacti
1119d 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69 66 20 69 74  on journal if it
1119e 20 69 73 20 6e 6f 74 20 74 68 65 72 65 20 61 6c   is not there al
1119f 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20  ready..    */.  
111a0 20 20 69 66 28 20 21 28 70 50 67 2d 3e 66 6c 61    if( !(pPg->fla
111a1 67 73 26 50 47 48 44 52 5f 49 4e 5f 4a 4f 55 52  gs&PGHDR_IN_JOUR
111a2 4e 41 4c 29 20 26 26 20 70 50 61 67 65 72 2d 3e  NAL) && pPager->
111a3 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 7b 0a 20  journalOpen ){. 
111a4 20 20 20 20 20 69 66 28 20 28 69 6e 74 29 70 50       if( (int)pP
111a5 67 2d 3e 70 67 6e 6f 20 3c 3d 20 70 50 61 67 65  g->pgno <= pPage
111a6 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65 20 29 7b  r->origDbSize ){
111a7 0a 20 20 20 20 20 20 20 20 75 33 32 20 63 6b 73  .        u32 cks
111a8 75 6d 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72  um;.        char
111a9 20 2a 70 44 61 74 61 32 3b 0a 0a 20 20 20 20 20   *pData2;..     
111aa 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20     /* We should 
111ab 6e 65 76 65 72 20 77 72 69 74 65 20 74 6f 20 74  never write to t
111ac 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
111ad 74 68 65 20 70 61 67 65 20 74 68 61 74 0a 20 20  the page that.  
111ae 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e        ** contain
111af 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c  s the database l
111b0 6f 63 6b 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  ocks.  The follo
111b1 77 69 6e 67 20 61 73 73 65 72 74 20 76 65 72 69  wing assert veri
111b2 66 69 65 73 0a 20 20 20 20 20 20 20 20 2a 2a 20  fies.        ** 
111b3 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 2e 20  that we do not. 
111b4 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  */.        asser
111b5 74 28 20 70 50 67 2d 3e 70 67 6e 6f 21 3d 50 41  t( pPg->pgno!=PA
111b6 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67  GER_MJ_PGNO(pPag
111b7 65 72 29 20 29 3b 0a 20 20 20 20 20 20 20 20 70  er) );.        p
111b8 44 61 74 61 32 20 3d 20 43 4f 44 45 43 32 28 70  Data2 = CODEC2(p
111b9 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50  Pager, pData, pP
111ba 67 2d 3e 70 67 6e 6f 2c 20 37 29 3b 0a 20 20 20  g->pgno, 7);.   
111bb 20 20 20 20 20 63 6b 73 75 6d 20 3d 20 70 61 67       cksum = pag
111bc 65 72 5f 63 6b 73 75 6d 28 70 50 61 67 65 72 2c  er_cksum(pPager,
111bd 20 28 75 38 2a 29 70 44 61 74 61 32 29 3b 0a 20   (u8*)pData2);. 
111be 20 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74         rc = writ
111bf 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e  e32bits(pPager->
111c0 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75  jfd, pPager->jou
111c1 72 6e 61 6c 4f 66 66 2c 20 70 50 67 2d 3e 70 67  rnalOff, pPg->pg
111c2 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  no);.        if(
111c3 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
111c4 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
111c5 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
111c6 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 44 61  pPager->jfd, pDa
111c7 74 61 32 2c 20 70 50 61 67 65 72 2d 3e 70 61 67  ta2, pPager->pag
111c8 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 20  eSize,.         
111c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
111ca 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75       pPager->jou
111cb 72 6e 61 6c 4f 66 66 20 2b 20 34 29 3b 0a 20 20  rnalOff + 4);.  
111cc 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
111cd 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 70 50  journalOff += pP
111ce 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2b 34  ager->pageSize+4
111cf 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
111d0 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
111d1 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
111d2 20 20 20 72 63 20 3d 20 77 72 69 74 65 33 32 62     rc = write32b
111d3 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  its(pPager->jfd,
111d4 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
111d5 4f 66 66 2c 20 63 6b 73 75 6d 29 3b 0a 20 20 20  Off, cksum);.   
111d6 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a         pPager->j
111d7 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 34 3b 0a  ournalOff += 4;.
111d8 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
111d9 20 20 49 4f 54 52 41 43 45 28 28 22 4a 4f 55 54    IOTRACE(("JOUT
111da 20 25 70 20 25 64 20 25 6c 6c 64 20 25 64 5c 6e   %p %d %lld %d\n
111db 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 2d 3e  ", pPager, pPg->
111dc 70 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20 20  pgno, .         
111dd 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
111de 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50 61 67  journalOff, pPag
111df 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 29 3b 0a  er->pageSize));.
111e0 20 20 20 20 20 20 20 20 50 41 47 45 52 5f 49 4e          PAGER_IN
111e1 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72  CR(sqlite3_pager
111e2 5f 77 72 69 74 65 6a 5f 63 6f 75 6e 74 29 3b 0a  _writej_count);.
111e3 20 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41          PAGERTRA
111e4 43 45 35 28 22 4a 4f 55 52 4e 41 4c 20 25 64 20  CE5("JOURNAL %d 
111e5 70 61 67 65 20 25 64 20 6e 65 65 64 53 79 6e 63  page %d needSync
111e6 3d 25 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e  =%d hash(%08x)\n
111e7 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
111e8 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c  PAGERID(pPager),
111e9 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20   pPg->pgno, .   
111ea 20 20 20 20 20 20 20 20 20 20 28 28 70 50 67 2d            ((pPg-
111eb 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45  >flags&PGHDR_NEE
111ec 44 5f 53 59 4e 43 29 3f 31 3a 30 29 2c 20 70 61  D_SYNC)?1:0), pa
111ed 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67  ger_pagehash(pPg
111ee 29 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20  ));..        /* 
111ef 41 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63  An error has occ
111f0 75 72 65 64 20 77 72 69 74 69 6e 67 20 74 6f 20  ured writing to 
111f1 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
111f2 2e 20 54 68 65 20 0a 20 20 20 20 20 20 20 20 2a  . The .        *
111f3 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69  * transaction wi
111f4 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63  ll be rolled bac
111f5 6b 20 62 79 20 74 68 65 20 6c 61 79 65 72 20 61  k by the layer a
111f6 62 6f 76 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f  bove..        */
111f7 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
111f8 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
111f9 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
111fa 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  c;.        }..  
111fb 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52        pPager->nR
111fc 65 63 2b 2b 3b 0a 20 20 20 20 20 20 20 20 61 73  ec++;.        as
111fd 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49  sert( pPager->pI
111fe 6e 4a 6f 75 72 6e 61 6c 21 3d 30 20 29 3b 0a 20  nJournal!=0 );. 
111ff 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 69         sqlite3Bi
11200 74 76 65 63 53 65 74 28 70 50 61 67 65 72 2d 3e  tvecSet(pPager->
11201 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 2d  pInJournal, pPg-
11202 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20  >pgno);.        
11203 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53  if( !pPager->noS
11204 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ync ){.         
11205 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50   pPg->flags |= P
11206 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a  GHDR_NEED_SYNC;.
11207 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
11208 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74    if( pPager->st
11209 6d 74 49 6e 55 73 65 20 29 7b 0a 20 20 20 20 20  mtInUse ){.     
1120a 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76       sqlite3Bitv
1120b 65 63 53 65 74 28 70 50 61 67 65 72 2d 3e 70 49  ecSet(pPager->pI
1120c 6e 53 74 6d 74 2c 20 70 50 67 2d 3e 70 67 6e 6f  nStmt, pPg->pgno
1120d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
1120e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1120f 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6a    if( !pPager->j
11210 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 26 26  ournalStarted &&
11211 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63   !pPager->noSync
11212 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 50   ){.          pP
11213 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44  g->flags |= PGHD
11214 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20  R_NEED_SYNC;.   
11215 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 50       }.        P
11216 41 47 45 52 54 52 41 43 45 34 28 22 41 50 50 45  AGERTRACE4("APPE
11217 4e 44 20 25 64 20 70 61 67 65 20 25 64 20 6e 65  ND %d page %d ne
11218 65 64 53 79 6e 63 3d 25 64 5c 6e 22 2c 0a 20 20  edSync=%d\n",.  
11219 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41                PA
1121a 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70  GERID(pPager), p
1121b 50 67 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20  Pg->pgno,.      
1121c 20 20 20 20 20 20 20 20 20 28 28 70 50 67 2d 3e           ((pPg->
1121d 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44  flags&PGHDR_NEED
1121e 5f 53 59 4e 43 29 3f 31 3a 30 29 29 3b 0a 20 20  _SYNC)?1:0));.  
1121f 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
11220 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52  pPg->flags&PGHDR
11221 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20  _NEED_SYNC ){.  
11222 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65        pPager->ne
11223 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20  edSync = 1;.    
11224 20 20 7d 0a 20 20 20 20 20 20 70 50 67 2d 3e 66    }.      pPg->f
11225 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 49 4e  lags |= PGHDR_IN
11226 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 7d 0a  _JOURNAL;.    }.
11227 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65    .    /* If the
11228 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e   statement journ
11229 61 6c 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 74  al is open and t
1122a 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69  he page is not i
1122b 6e 20 69 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65  n it,.    ** the
1122c 6e 20 77 72 69 74 65 20 74 68 65 20 63 75 72 72  n write the curr
1122d 65 6e 74 20 70 61 67 65 20 74 6f 20 74 68 65 20  ent page to the 
1122e 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61  statement journa
1122f 6c 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a 20 20  l.  Note that.  
11230 20 20 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65    ** the stateme
11231 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61  nt journal forma
11232 74 20 64 69 66 66 65 72 73 20 66 72 6f 6d 20 74  t differs from t
11233 68 65 20 73 74 61 6e 64 61 72 64 20 6a 6f 75 72  he standard jour
11234 6e 61 6c 20 66 6f 72 6d 61 74 0a 20 20 20 20 2a  nal format.    *
11235 2a 20 69 6e 20 74 68 61 74 20 69 74 20 6f 6d 69  * in that it omi
11236 74 73 20 74 68 65 20 63 68 65 63 6b 73 75 6d 73  ts the checksums
11237 20 61 6e 64 20 74 68 65 20 68 65 61 64 65 72 2e   and the header.
11238 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
11239 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73  pPager->stmtInUs
1123a 65 20 0a 20 20 20 20 20 26 26 20 21 70 61 67 65  e .     && !page
1123b 49 6e 53 74 61 74 65 6d 65 6e 74 28 70 50 67 29  InStatement(pPg)
1123c 20 0a 20 20 20 20 20 26 26 20 28 69 6e 74 29 70   .     && (int)p
1123d 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 65 72  Pg->pgno<=pPager
1123e 2d 3e 73 74 6d 74 53 69 7a 65 20 0a 20 20 20 20  ->stmtSize .    
1123f 29 7b 0a 20 20 20 20 20 20 69 36 34 20 6f 66 66  ){.      i64 off
11240 73 65 74 20 3d 20 70 50 61 67 65 72 2d 3e 73 74  set = pPager->st
11241 6d 74 4e 52 65 63 2a 28 34 2b 70 50 61 67 65 72  mtNRec*(4+pPager
11242 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20  ->pageSize);.   
11243 20 20 20 63 68 61 72 20 2a 70 44 61 74 61 32 20     char *pData2 
11244 3d 20 43 4f 44 45 43 32 28 70 50 61 67 65 72 2c  = CODEC2(pPager,
11245 20 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e   pData, pPg->pgn
11246 6f 2c 20 37 29 3b 0a 20 20 20 20 20 20 61 73 73  o, 7);.      ass
11247 65 72 74 28 20 28 70 50 67 2d 3e 66 6c 61 67 73  ert( (pPg->flags
11248 26 50 47 48 44 52 5f 49 4e 5f 4a 4f 55 52 4e 41  &PGHDR_IN_JOURNA
11249 4c 29 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  L) .            
1124a 20 20 20 20 20 7c 7c 20 28 69 6e 74 29 70 50 67       || (int)pPg
1124b 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 6f  ->pgno>pPager->o
1124c 72 69 67 44 62 53 69 7a 65 20 29 3b 0a 20 20 20  rigDbSize );.   
1124d 20 20 20 72 63 20 3d 20 77 72 69 74 65 33 32 62     rc = write32b
1124e 69 74 73 28 70 50 61 67 65 72 2d 3e 73 74 66 64  its(pPager->stfd
1124f 2c 20 6f 66 66 73 65 74 2c 20 70 50 67 2d 3e 70  , offset, pPg->p
11250 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20  gno);.      if( 
11251 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
11252 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
11253 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61  lite3OsWrite(pPa
11254 67 65 72 2d 3e 73 74 66 64 2c 20 70 44 61 74 61  ger->stfd, pData
11255 32 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  2, pPager->pageS
11256 69 7a 65 2c 20 6f 66 66 73 65 74 2b 34 29 3b 0a  ize, offset+4);.
11257 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 50 41        }.      PA
11258 47 45 52 54 52 41 43 45 33 28 22 53 54 4d 54 2d  GERTRACE3("STMT-
11259 4a 4f 55 52 4e 41 4c 20 25 64 20 70 61 67 65 20  JOURNAL %d page 
1125a 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70  %d\n", PAGERID(p
1125b 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e  Pager), pPg->pgn
1125c 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  o);.      if( rc
1125d 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1125e 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
1125f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
11260 70 50 61 67 65 72 2d 3e 73 74 6d 74 4e 52 65 63  pPager->stmtNRec
11261 2b 2b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ++;.      assert
11262 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d  ( pPager->pInStm
11263 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71  t!=0 );.      sq
11264 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 70  lite3BitvecSet(p
11265 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74 2c 20  Pager->pInStmt, 
11266 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20  pPg->pgno);.    
11267 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61  }.  }..  /* Upda
11268 74 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  te the database 
11269 73 69 7a 65 20 61 6e 64 20 72 65 74 75 72 6e 2e  size and return.
1126a 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
1126b 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50  pPager->state>=P
1126c 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 20  AGER_SHARED );. 
1126d 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53   if( pPager->dbS
1126e 69 7a 65 3c 28 69 6e 74 29 70 50 67 2d 3e 70 67  ize<(int)pPg->pg
1126f 6e 6f 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72  no ){.    pPager
11270 2d 3e 64 62 53 69 7a 65 20 3d 20 70 50 67 2d 3e  ->dbSize = pPg->
11271 70 67 6e 6f 3b 0a 20 20 20 20 69 66 28 20 70 50  pgno;.    if( pP
11272 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 50 45  ager->dbSize==PE
11273 4e 44 49 4e 47 5f 42 59 54 45 2f 70 50 61 67 65  NDING_BYTE/pPage
11274 72 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a 20  r->pageSize ){. 
11275 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53       pPager->dbS
11276 69 7a 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  ize++;.    }.  }
11277 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
11278 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
11279 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
1127a 6d 61 72 6b 20 61 20 64 61 74 61 2d 70 61 67 65  mark a data-page
1127b 20 61 73 20 77 72 69 74 61 62 6c 65 2e 20 49 74   as writable. It
1127c 20 75 73 65 73 20 0a 2a 2a 20 70 61 67 65 72 5f   uses .** pager_
1127d 77 72 69 74 65 28 29 20 74 6f 20 6f 70 65 6e 20  write() to open 
1127e 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28  a journal file (
1127f 69 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72  if it is not alr
11280 65 61 64 79 20 6f 70 65 6e 29 0a 2a 2a 20 61 6e  eady open).** an
11281 64 20 77 72 69 74 65 20 74 68 65 20 70 61 67 65  d write the page
11282 20 2a 70 44 61 74 61 20 74 6f 20 74 68 65 20 6a   *pData to the j
11283 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68  ournal..**.** Th
11284 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74  e difference bet
11285 77 65 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  ween this functi
11286 6f 6e 20 61 6e 64 20 70 61 67 65 72 5f 77 72 69  on and pager_wri
11287 74 65 28 29 20 69 73 20 74 68 61 74 20 74 68 69  te() is that thi
11288 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 6c  s.** function al
11289 73 6f 20 64 65 61 6c 73 20 77 69 74 68 20 74 68  so deals with th
1128a 65 20 73 70 65 63 69 61 6c 20 63 61 73 65 20 77  e special case w
1128b 68 65 72 65 20 32 20 6f 72 20 6d 6f 72 65 20 70  here 2 or more p
1128c 61 67 65 73 0a 2a 2a 20 66 69 74 20 6f 6e 20 61  ages.** fit on a
1128d 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63   single disk sec
1128e 74 6f 72 2e 20 49 6e 20 74 68 69 73 20 63 61 73  tor. In this cas
1128f 65 20 61 6c 6c 20 63 6f 2d 72 65 73 69 64 65 6e  e all co-residen
11290 74 20 70 61 67 65 73 0a 2a 2a 20 6d 75 73 74 20  t pages.** must 
11291 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 65  have been writte
11292 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  n to the journal
11293 20 66 69 6c 65 20 62 65 66 6f 72 65 20 72 65 74   file before ret
11294 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54  urning..*/.SQLIT
11295 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
11296 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
11297 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 29  DbPage *pDbPage)
11298 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
11299 49 54 45 5f 4f 4b 3b 0a 0a 20 20 50 67 48 64 72  ITE_OK;..  PgHdr
1129a 20 2a 70 50 67 20 3d 20 70 44 62 50 61 67 65 3b   *pPg = pDbPage;
1129b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
1129c 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a   = pPg->pPager;.
1129d 20 20 50 67 6e 6f 20 6e 50 61 67 65 50 65 72 53    Pgno nPagePerS
1129e 65 63 74 6f 72 20 3d 20 28 70 50 61 67 65 72 2d  ector = (pPager-
1129f 3e 73 65 63 74 6f 72 53 69 7a 65 2f 70 50 61 67  >sectorSize/pPag
112a0 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 0a  er->pageSize);..
112a1 20 20 69 66 28 20 6e 50 61 67 65 50 65 72 53 65    if( nPagePerSe
112a2 63 74 6f 72 3e 31 20 29 7b 0a 20 20 20 20 50 67  ctor>1 ){.    Pg
112a3 6e 6f 20 6e 50 61 67 65 43 6f 75 6e 74 3b 20 20  no nPageCount;  
112a4 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c          /* Total
112a5 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
112a6 20 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c   in database fil
112a7 65 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 70 67  e */.    Pgno pg
112a8 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
112a9 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 67 65     /* First page
112aa 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 70   of the sector p
112ab 50 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e  Pg is located on
112ac 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 50 61  . */.    int nPa
112ad 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
112ae 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
112af 70 61 67 65 73 20 73 74 61 72 74 69 6e 67 20 61  pages starting a
112b0 74 20 70 67 31 20 74 6f 20 6a 6f 75 72 6e 61 6c  t pg1 to journal
112b1 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a   */.    int ii;.
112b2 20 20 20 20 69 6e 74 20 6e 65 65 64 53 79 6e 63      int needSync
112b3 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 53 65   = 0;..    /* Se
112b4 74 20 74 68 65 20 64 6f 4e 6f 74 53 79 6e 63 20  t the doNotSync 
112b5 66 6c 61 67 20 74 6f 20 31 2e 20 54 68 69 73 20  flag to 1. This 
112b6 69 73 20 62 65 63 61 75 73 65 20 77 65 20 63 61  is because we ca
112b7 6e 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 6a 6f 75  nnot allow a jou
112b8 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 68 65 61 64  rnal.    ** head
112b9 65 72 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e  er to be written
112ba 20 62 65 74 77 65 65 6e 20 74 68 65 20 70 61 67   between the pag
112bb 65 73 20 6a 6f 75 72 6e 61 6c 65 64 20 62 79 20  es journaled by 
112bc 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 20  this function.. 
112bd 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
112be 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20  ( !MEMDB );.    
112bf 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
112c0 64 6f 4e 6f 74 53 79 6e 63 3d 3d 30 20 29 3b 0a  doNotSync==0 );.
112c1 20 20 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f      pPager->doNo
112c2 74 53 79 6e 63 20 3d 20 31 3b 0a 0a 20 20 20 20  tSync = 1;..    
112c3 2f 2a 20 54 68 69 73 20 74 72 69 63 6b 20 61 73  /* This trick as
112c4 73 75 6d 65 73 20 74 68 61 74 20 62 6f 74 68 20  sumes that both 
112c5 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 6e  the page-size an
112c6 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 61 72  d sector-size ar
112c7 65 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 74 65  e.    ** an inte
112c8 67 65 72 20 70 6f 77 65 72 20 6f 66 20 32 2e 20  ger power of 2. 
112c9 49 74 20 73 65 74 73 20 76 61 72 69 61 62 6c 65  It sets variable
112ca 20 70 67 31 20 74 6f 20 74 68 65 20 69 64 65 6e   pg1 to the iden
112cb 74 69 66 69 65 72 0a 20 20 20 20 2a 2a 20 6f 66  tifier.    ** of
112cc 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20   the first page 
112cd 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 70 50  of the sector pP
112ce 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e 2e  g is located on.
112cf 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 67 31 20  .    */.    pg1 
112d0 3d 20 28 28 70 50 67 2d 3e 70 67 6e 6f 2d 31 29  = ((pPg->pgno-1)
112d1 20 26 20 7e 28 6e 50 61 67 65 50 65 72 53 65 63   & ~(nPagePerSec
112d2 74 6f 72 2d 31 29 29 20 2b 20 31 3b 0a 0a 20 20  tor-1)) + 1;..  
112d3 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61    sqlite3PagerPa
112d4 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20  gecount(pPager, 
112d5 28 69 6e 74 20 2a 29 26 6e 50 61 67 65 43 6f 75  (int *)&nPageCou
112d6 6e 74 29 3b 0a 20 20 20 20 69 66 28 20 70 50 67  nt);.    if( pPg
112d7 2d 3e 70 67 6e 6f 3e 6e 50 61 67 65 43 6f 75 6e  ->pgno>nPageCoun
112d8 74 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65  t ){.      nPage
112d9 20 3d 20 28 70 50 67 2d 3e 70 67 6e 6f 20 2d 20   = (pPg->pgno - 
112da 70 67 31 29 2b 31 3b 0a 20 20 20 20 7d 65 6c 73  pg1)+1;.    }els
112db 65 20 69 66 28 20 28 70 67 31 2b 6e 50 61 67 65  e if( (pg1+nPage
112dc 50 65 72 53 65 63 74 6f 72 2d 31 29 3e 6e 50 61  PerSector-1)>nPa
112dd 67 65 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20  geCount ){.     
112de 20 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 43 6f   nPage = nPageCo
112df 75 6e 74 2b 31 2d 70 67 31 3b 0a 20 20 20 20 7d  unt+1-pg1;.    }
112e0 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 67  else{.      nPag
112e1 65 20 3d 20 6e 50 61 67 65 50 65 72 53 65 63 74  e = nPagePerSect
112e2 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  or;.    }.    as
112e3 73 65 72 74 28 6e 50 61 67 65 3e 30 29 3b 0a 20  sert(nPage>0);. 
112e4 20 20 20 61 73 73 65 72 74 28 70 67 31 3c 3d 70     assert(pg1<=p
112e5 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 61  Pg->pgno);.    a
112e6 73 73 65 72 74 28 28 70 67 31 2b 6e 50 61 67 65  ssert((pg1+nPage
112e7 29 3e 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 0a 20  )>pPg->pgno);.. 
112e8 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c     for(ii=0; ii<
112e9 6e 50 61 67 65 20 26 26 20 72 63 3d 3d 53 51 4c  nPage && rc==SQL
112ea 49 54 45 5f 4f 4b 3b 20 69 69 2b 2b 29 7b 0a 20  ITE_OK; ii++){. 
112eb 20 20 20 20 20 50 67 6e 6f 20 70 67 20 3d 20 70       Pgno pg = p
112ec 67 31 2b 69 69 3b 0a 20 20 20 20 20 20 50 67 48  g1+ii;.      PgH
112ed 64 72 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 20  dr *pPage;.     
112ee 20 69 66 28 20 70 67 3d 3d 70 50 67 2d 3e 70 67   if( pg==pPg->pg
112ef 6e 6f 20 7c 7c 20 21 73 71 6c 69 74 65 33 42 69  no || !sqlite3Bi
112f0 74 76 65 63 54 65 73 74 28 70 50 61 67 65 72 2d  tvecTest(pPager-
112f1 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 29  >pInJournal, pg)
112f2 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
112f3 70 67 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e  pg!=PAGER_MJ_PGN
112f4 4f 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20  O(pPager) ){.   
112f5 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
112f6 74 65 33 50 61 67 65 72 47 65 74 28 70 50 61 67  te3PagerGet(pPag
112f7 65 72 2c 20 70 67 2c 20 26 70 50 61 67 65 29 3b  er, pg, &pPage);
112f8 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72  .          if( r
112f9 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
112fa 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
112fb 20 70 61 67 65 72 5f 77 72 69 74 65 28 70 50 61   pager_write(pPa
112fc 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ge);.           
112fd 20 69 66 28 20 70 50 61 67 65 2d 3e 66 6c 61 67   if( pPage->flag
112fe 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  s&PGHDR_NEED_SYN
112ff 43 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  C ){.           
11300 20 20 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b     needSync = 1;
11301 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
11302 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
11303 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61  e3PagerUnref(pPa
11304 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  ge);.          }
11305 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
11306 20 7d 65 6c 73 65 20 69 66 28 20 28 70 50 61 67   }else if( (pPag
11307 65 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70  e = pager_lookup
11308 28 70 50 61 67 65 72 2c 20 70 67 29 29 21 3d 30  (pPager, pg))!=0
11309 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
1130a 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48  pPage->flags&PGH
1130b 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a  DR_NEED_SYNC ){.
1130c 20 20 20 20 20 20 20 20 20 20 6e 65 65 64 53 79            needSy
1130d 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  nc = 1;.        
1130e 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
1130f 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 67  3PagerUnref(pPag
11310 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
11311 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  }..    /* If the
11312 20 50 67 48 64 72 2e 6e 65 65 64 53 79 6e 63 20   PgHdr.needSync 
11313 66 6c 61 67 20 69 73 20 73 65 74 20 66 6f 72 20  flag is set for 
11314 61 6e 79 20 6f 66 20 74 68 65 20 6e 50 61 67 65  any of the nPage
11315 20 70 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 73   pages .    ** s
11316 74 61 72 74 69 6e 67 20 61 74 20 70 67 31 2c 20  tarting at pg1, 
11317 74 68 65 6e 20 69 74 20 6e 65 65 64 73 20 74 6f  then it needs to
11318 20 62 65 20 73 65 74 20 66 6f 72 20 61 6c 6c 20   be set for all 
11319 6f 66 20 74 68 65 6d 2e 20 42 65 63 61 75 73 65  of them. Because
1131a 0a 20 20 20 20 2a 2a 20 77 72 69 74 69 6e 67 20  .    ** writing 
1131b 74 6f 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20  to any of these 
1131c 6e 50 61 67 65 20 70 61 67 65 73 20 6d 61 79 20  nPage pages may 
1131d 64 61 6d 61 67 65 20 74 68 65 20 6f 74 68 65 72  damage the other
1131e 73 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 6a 6f  s, the.    ** jo
1131f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20  urnal file must 
11320 63 6f 6e 74 61 69 6e 20 73 79 6e 63 28 29 65 64  contain sync()ed
11321 20 63 6f 70 69 65 73 20 6f 66 20 61 6c 6c 20 6f   copies of all o
11322 66 20 74 68 65 6d 0a 20 20 20 20 2a 2a 20 62 65  f them.    ** be
11323 66 6f 72 65 20 61 6e 79 20 6f 66 20 74 68 65 6d  fore any of them
11324 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20   can be written 
11325 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62  out to the datab
11326 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f  ase file..    */
11327 0a 20 20 20 20 69 66 28 20 6e 65 65 64 53 79 6e  .    if( needSyn
11328 63 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  c ){.      asser
11329 74 28 20 21 4d 45 4d 44 42 20 26 26 20 70 50 61  t( !MEMDB && pPa
1132a 67 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 20 29  ger->noSync==0 )
1132b 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30  ;.      for(ii=0
1132c 3b 20 69 69 3c 6e 50 61 67 65 20 26 26 20 6e 65  ; ii<nPage && ne
1132d 65 64 53 79 6e 63 3b 20 69 69 2b 2b 29 7b 0a 20  edSync; ii++){. 
1132e 20 20 20 20 20 20 20 50 67 48 64 72 20 2a 70 50         PgHdr *pP
1132f 61 67 65 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b  age = pager_look
11330 75 70 28 70 50 61 67 65 72 2c 20 70 67 31 2b 69  up(pPager, pg1+i
11331 69 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  i);.        if( 
11332 70 50 61 67 65 20 29 20 70 50 61 67 65 2d 3e 66  pPage ) pPage->f
11333 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45  lags |= PGHDR_NE
11334 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 20 20 20  ED_SYNC;.       
11335 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
11336 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20  ef(pPage);.     
11337 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   }.      assert(
11338 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63  pPager->needSync
11339 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73  );.    }..    as
1133a 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 6f  sert( pPager->do
1133b 4e 6f 74 53 79 6e 63 3d 3d 31 20 29 3b 0a 20 20  NotSync==1 );.  
1133c 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53    pPager->doNotS
1133d 79 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  ync = 0;.  }else
1133e 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72  {.    rc = pager
1133f 5f 77 72 69 74 65 28 70 44 62 50 61 67 65 29 3b  _write(pDbPage);
11340 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
11341 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
11342 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 70 61  n TRUE if the pa
11343 67 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20  ge given in the 
11344 61 72 67 75 6d 65 6e 74 20 77 61 73 20 70 72 65  argument was pre
11345 76 69 6f 75 73 6c 79 20 70 61 73 73 65 64 0a 2a  viously passed.*
11346 2a 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65  * to sqlite3Page
11347 72 57 72 69 74 65 28 29 2e 20 20 49 6e 20 6f 74  rWrite().  In ot
11348 68 65 72 20 77 6f 72 64 73 2c 20 72 65 74 75 72  her words, retur
11349 6e 20 54 52 55 45 20 69 66 20 69 74 20 69 73 20  n TRUE if it is 
1134a 6f 6b 0a 2a 2a 20 74 6f 20 63 68 61 6e 67 65 20  ok.** to change 
1134b 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74  the content of t
1134c 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 6e  he page..*/.#ifn
1134d 64 65 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54  def NDEBUG.SQLIT
1134e 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1134f 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
11350 65 61 62 6c 65 28 44 62 50 61 67 65 20 2a 70 50  eable(DbPage *pP
11351 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 67  g){.  return pPg
11352 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49  ->flags&PGHDR_DI
11353 52 54 59 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  RTY;.}.#endif../
11354 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 74  *.** A call to t
11355 68 69 73 20 72 6f 75 74 69 6e 65 20 74 65 6c 6c  his routine tell
11356 73 20 74 68 65 20 70 61 67 65 72 20 74 68 61 74  s the pager that
11357 20 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73   it is not neces
11358 73 61 72 79 20 74 6f 0a 2a 2a 20 77 72 69 74 65  sary to.** write
11359 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   the information
1135a 20 6f 6e 20 70 61 67 65 20 70 50 67 20 62 61 63   on page pPg bac
1135b 6b 20 74 6f 20 74 68 65 20 64 69 73 6b 2c 20 65  k to the disk, e
1135c 76 65 6e 20 74 68 6f 75 67 68 0a 2a 2a 20 74 68  ven though.** th
1135d 61 74 20 70 61 67 65 20 6d 69 67 68 74 20 62 65  at page might be
1135e 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74 79   marked as dirty
1135f 2e 20 20 54 68 69 73 20 68 61 70 70 65 6e 73 2c  .  This happens,
11360 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68   for example, wh
11361 65 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 68  en.** the page h
11362 61 73 20 62 65 65 6e 20 61 64 64 65 64 20 61 73  as been added as
11363 20 61 20 6c 65 61 66 20 6f 66 20 74 68 65 20 66   a leaf of the f
11364 72 65 65 6c 69 73 74 20 61 6e 64 20 73 6f 20 69  reelist and so i
11365 74 73 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6e 6f  ts.** content no
11366 20 6c 6f 6e 67 65 72 20 6d 61 74 74 65 72 73 2e   longer matters.
11367 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 76 65 72 6c  .**.** The overl
11368 79 69 6e 67 20 73 6f 66 74 77 61 72 65 20 6c 61  ying software la
11369 79 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72  yer calls this r
1136a 6f 75 74 69 6e 65 20 77 68 65 6e 20 61 6c 6c 20  outine when all 
1136b 6f 66 20 74 68 65 20 64 61 74 61 0a 2a 2a 20 6f  of the data.** o
1136c 6e 20 74 68 65 20 67 69 76 65 6e 20 70 61 67 65  n the given page
1136d 20 69 73 20 75 6e 75 73 65 64 2e 20 20 54 68 65   is unused.  The
1136e 20 70 61 67 65 72 20 6d 61 72 6b 73 20 74 68 65   pager marks the
1136f 20 70 61 67 65 20 61 73 20 63 6c 65 61 6e 20 73   page as clean s
11370 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 64 6f 65  o.** that it doe
11371 73 20 6e 6f 74 20 67 65 74 20 77 72 69 74 74 65  s not get writte
11372 6e 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a  n to disk..**.**
11373 20 54 65 73 74 73 20 73 68 6f 77 20 74 68 61 74   Tests show that
11374 20 74 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69   this optimizati
11375 6f 6e 2c 20 74 6f 67 65 74 68 65 72 20 77 69 74  on, together wit
11376 68 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33  h the.** sqlite3
11377 50 61 67 65 72 44 6f 6e 74 52 6f 6c 6c 62 61 63  PagerDontRollbac
11378 6b 28 29 20 62 65 6c 6f 77 2c 20 6d 6f 72 65 20  k() below, more 
11379 74 68 61 6e 20 64 6f 75 62 6c 65 20 74 68 65 20  than double the 
1137a 73 70 65 65 64 0a 2a 2a 20 6f 66 20 6c 61 72 67  speed.** of larg
1137b 65 20 49 4e 53 45 52 54 20 6f 70 65 72 61 74 69  e INSERT operati
1137c 6f 6e 73 20 61 6e 64 20 71 75 61 64 72 75 70 6c  ons and quadrupl
1137d 65 20 74 68 65 20 73 70 65 65 64 20 6f 66 20 6c  e the speed of l
1137e 61 72 67 65 20 44 45 4c 45 54 45 73 2e 0a 2a 2a  arge DELETEs..**
1137f 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 72 6f  .** When this ro
11380 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c  utine is called,
11381 20 73 65 74 20 74 68 65 20 61 6c 77 61 79 73 52   set the alwaysR
11382 6f 6c 6c 62 61 63 6b 20 66 6c 61 67 20 74 6f 20  ollback flag to 
11383 74 72 75 65 2e 0a 2a 2a 20 53 75 62 73 65 71 75  true..** Subsequ
11384 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c  ent calls to sql
11385 69 74 65 33 50 61 67 65 72 44 6f 6e 74 52 6f 6c  ite3PagerDontRol
11386 6c 62 61 63 6b 28 29 20 66 6f 72 20 74 68 65 20  lback() for the 
11387 73 61 6d 65 20 70 61 67 65 0a 2a 2a 20 77 69 6c  same page.** wil
11388 6c 20 74 68 65 72 65 61 66 74 65 72 20 62 65 20  l thereafter be 
11389 69 67 6e 6f 72 65 64 2e 20 20 54 68 69 73 20 69  ignored.  This i
1138a 73 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 61  s necessary to a
1138b 76 6f 69 64 20 61 20 70 72 6f 62 6c 65 6d 0a 2a  void a problem.*
1138c 2a 20 77 68 65 72 65 20 61 20 70 61 67 65 20 77  * where a page w
1138d 69 74 68 20 64 61 74 61 20 69 73 20 61 64 64 65  ith data is adde
1138e 64 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73  d to the freelis
1138f 74 20 64 75 72 69 6e 67 20 6f 6e 65 20 70 61 72  t during one par
11390 74 20 6f 66 0a 2a 2a 20 61 20 74 72 61 6e 73 61  t of.** a transa
11391 63 74 69 6f 6e 20 74 68 65 6e 20 72 65 6d 6f 76  ction then remov
11392 65 64 20 66 72 6f 6d 20 74 68 65 20 66 72 65 65  ed from the free
11393 6c 69 73 74 20 64 75 72 69 6e 67 20 61 20 6c 61  list during a la
11394 74 65 72 20 70 61 72 74 0a 2a 2a 20 6f 66 20 74  ter part.** of t
11395 68 65 20 73 61 6d 65 20 74 72 61 6e 73 61 63 74  he same transact
11396 69 6f 6e 20 61 6e 64 20 72 65 75 73 65 64 20 66  ion and reused f
11397 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 75  or some other pu
11398 72 70 6f 73 65 2e 20 20 57 68 65 6e 20 69 74 0a  rpose.  When it.
11399 2a 2a 20 69 73 20 66 69 72 73 74 20 61 64 64 65  ** is first adde
1139a 64 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73  d to the freelis
1139b 74 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  t, this routine 
1139c 69 73 20 63 61 6c 6c 65 64 2e 20 20 57 68 65 6e  is called.  When
1139d 20 72 65 75 73 65 64 2c 0a 2a 2a 20 74 68 65 20   reused,.** the 
1139e 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74  sqlite3PagerDont
1139f 52 6f 6c 6c 62 61 63 6b 28 29 20 72 6f 75 74 69  Rollback() routi
113a0 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 42  ne is called.  B
113a1 75 74 20 62 65 63 61 75 73 65 20 74 68 65 0a 2a  ut because the.*
113a2 2a 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 73 20  * page contains 
113a3 63 72 69 74 69 63 61 6c 20 64 61 74 61 2c 20 77  critical data, w
113a4 65 20 73 74 69 6c 6c 20 6e 65 65 64 20 74 6f 20  e still need to 
113a5 62 65 20 73 75 72 65 20 69 74 20 67 65 74 73 0a  be sure it gets.
113a6 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69  ** rolled back i
113a7 6e 20 73 70 69 74 65 20 6f 66 20 74 68 65 20 73  n spite of the s
113a8 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 52  qlite3PagerDontR
113a9 6f 6c 6c 62 61 63 6b 28 29 20 63 61 6c 6c 2e 0a  ollback() call..
113aa 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
113ab 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
113ac 65 72 44 6f 6e 74 57 72 69 74 65 28 44 62 50 61  erDontWrite(DbPa
113ad 67 65 20 2a 70 44 62 50 61 67 65 29 7b 0a 20 20  ge *pDbPage){.  
113ae 50 67 48 64 72 20 2a 70 50 67 20 3d 20 70 44 62  PgHdr *pPg = pDb
113af 50 61 67 65 3b 0a 20 20 50 61 67 65 72 20 2a 70  Page;.  Pager *p
113b0 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61  Pager = pPg->pPa
113b1 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  ger;.  int rc;..
113b2 20 20 69 66 28 20 70 50 67 2d 3e 70 67 6e 6f 3e    if( pPg->pgno>
113b3 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62 53 69  pPager->origDbSi
113b4 7a 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ze ){.    return
113b5 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
113b6 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70 41    if( pPager->pA
113b7 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 3d 3d 30  lwaysRollback==0
113b8 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
113b9 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e  pPager->pInJourn
113ba 61 6c 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72  al );.    pPager
113bb 2d 3e 70 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63  ->pAlwaysRollbac
113bc 6b 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65  k = sqlite3Bitve
113bd 63 43 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e  cCreate(pPager->
113be 6f 72 69 67 44 62 53 69 7a 65 29 3b 0a 20 20 20  origDbSize);.   
113bf 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 70 41   if( !pPager->pA
113c0 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 20 29 7b  lwaysRollback ){
113c1 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
113c2 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
113c3 7d 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c  }.  }.  rc = sql
113c4 69 74 65 33 42 69 74 76 65 63 53 65 74 28 70 50  ite3BitvecSet(pP
113c5 61 67 65 72 2d 3e 70 41 6c 77 61 79 73 52 6f 6c  ager->pAlwaysRol
113c6 6c 62 61 63 6b 2c 20 70 50 67 2d 3e 70 67 6e 6f  lback, pPg->pgno
113c7 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  );..  if( rc==SQ
113c8 4c 49 54 45 5f 4f 4b 20 26 26 20 28 70 50 67 2d  LITE_OK && (pPg-
113c9 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52  >flags&PGHDR_DIR
113ca 54 59 29 20 26 26 20 21 70 50 61 67 65 72 2d 3e  TY) && !pPager->
113cb 73 74 6d 74 49 6e 55 73 65 20 29 7b 0a 20 20 20  stmtInUse ){.   
113cc 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
113cd 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48  >state>=PAGER_SH
113ce 41 52 45 44 20 29 3b 0a 20 20 20 20 69 66 28 20  ARED );.    if( 
113cf 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d  pPager->dbSize==
113d0 28 69 6e 74 29 70 50 67 2d 3e 70 67 6e 6f 20 26  (int)pPg->pgno &
113d1 26 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62  & pPager->origDb
113d2 53 69 7a 65 3c 70 50 61 67 65 72 2d 3e 64 62 53  Size<pPager->dbS
113d3 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ize ){.      /* 
113d4 49 66 20 74 68 69 73 20 70 61 67 65 73 20 69 73  If this pages is
113d5 20 74 68 65 20 6c 61 73 74 20 70 61 67 65 20 69   the last page i
113d6 6e 20 74 68 65 20 66 69 6c 65 20 61 6e 64 20 74  n the file and t
113d7 68 65 20 66 69 6c 65 20 68 61 73 20 67 72 6f 77  he file has grow
113d8 6e 0a 20 20 20 20 20 20 2a 2a 20 64 75 72 69 6e  n.      ** durin
113d9 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72  g the current tr
113da 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20  ansaction, then 
113db 64 6f 20 4e 4f 54 20 6d 61 72 6b 20 74 68 65 20  do NOT mark the 
113dc 70 61 67 65 20 61 73 20 63 6c 65 61 6e 2e 0a 20  page as clean.. 
113dd 20 20 20 20 20 2a 2a 20 57 68 65 6e 20 74 68 65       ** When the
113de 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 67   database file g
113df 72 6f 77 73 2c 20 77 65 20 6d 75 73 74 20 6d 61  rows, we must ma
113e0 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65  ke sure that the
113e1 20 6c 61 73 74 20 70 61 67 65 0a 20 20 20 20 20   last page.     
113e2 20 2a 2a 20 67 65 74 73 20 77 72 69 74 74 65 6e   ** gets written
113e3 20 61 74 20 6c 65 61 73 74 20 6f 6e 63 65 20 73   at least once s
113e4 6f 20 74 68 61 74 20 74 68 65 20 64 69 73 6b 20  o that the disk 
113e5 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 74 68 65  file will be the
113e6 20 63 6f 72 72 65 63 74 0a 20 20 20 20 20 20 2a   correct.      *
113e7 2a 20 73 69 7a 65 2e 20 49 66 20 79 6f 75 20 64  * size. If you d
113e8 6f 20 6e 6f 74 20 77 72 69 74 65 20 74 68 69 73  o not write this
113e9 20 70 61 67 65 20 61 6e 64 20 74 68 65 20 73 69   page and the si
113ea 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 0a 20  ze of the file. 
113eb 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 64       ** on the d
113ec 69 73 6b 20 65 6e 64 73 20 75 70 20 62 65 69 6e  isk ends up bein
113ed 67 20 74 6f 6f 20 73 6d 61 6c 6c 2c 20 74 68 61  g too small, tha
113ee 74 20 63 61 6e 20 6c 65 61 64 20 74 6f 20 64 61  t can lead to da
113ef 74 61 62 61 73 65 0a 20 20 20 20 20 20 2a 2a 20  tabase.      ** 
113f0 63 6f 72 72 75 70 74 69 6f 6e 20 64 75 72 69 6e  corruption durin
113f1 67 20 74 68 65 20 6e 65 78 74 20 74 72 61 6e 73  g the next trans
113f2 61 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f  action..      */
113f3 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
113f4 20 20 50 41 47 45 52 54 52 41 43 45 33 28 22 44    PAGERTRACE3("D
113f5 4f 4e 54 5f 57 52 49 54 45 20 70 61 67 65 20 25  ONT_WRITE page %
113f6 64 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 67 2d  d of %d\n", pPg-
113f7 3e 70 67 6e 6f 2c 20 50 41 47 45 52 49 44 28 70  >pgno, PAGERID(p
113f8 50 61 67 65 72 29 29 3b 0a 20 20 20 20 20 20 49  Pager));.      I
113f9 4f 54 52 41 43 45 28 28 22 43 4c 45 41 4e 20 25  OTRACE(("CLEAN %
113fa 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c  p %d\n", pPager,
113fb 20 70 50 67 2d 3e 70 67 6e 6f 29 29 0a 20 20 20   pPg->pgno)).   
113fc 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d     pPg->flags |=
113fd 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54   PGHDR_DONT_WRIT
113fe 45 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  E;.#ifdef SQLITE
113ff 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20  _CHECK_PAGES.   
11400 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68     pPg->pageHash
11401 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73   = pager_pagehas
11402 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20  h(pPg);.#endif. 
11403 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
11404 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  n rc;.}../*.** A
11405 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 72 6f   call to this ro
11406 75 74 69 6e 65 20 74 65 6c 6c 73 20 74 68 65 20  utine tells the 
11407 70 61 67 65 72 20 74 68 61 74 20 69 66 20 61 20  pager that if a 
11408 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73 2c  rollback occurs,
11409 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74 20 6e 65  .** it is not ne
1140a 63 65 73 73 61 72 79 20 74 6f 20 72 65 73 74 6f  cessary to resto
1140b 72 65 20 74 68 65 20 64 61 74 61 20 6f 6e 20 74  re the data on t
1140c 68 65 20 67 69 76 65 6e 20 70 61 67 65 2e 20 20  he given page.  
1140d 54 68 69 73 0a 2a 2a 20 6d 65 61 6e 73 20 74 68  This.** means th
1140e 61 74 20 74 68 65 20 70 61 67 65 72 20 64 6f 65  at the pager doe
1140f 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 72 65  s not have to re
11410 63 6f 72 64 20 74 68 65 20 67 69 76 65 6e 20 70  cord the given p
11411 61 67 65 20 69 6e 20 74 68 65 0a 2a 2a 20 72 6f  age in the.** ro
11412 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a  llback journal..
11413 2a 2a 0a 2a 2a 20 49 66 20 77 65 20 68 61 76 65  **.** If we have
11414 20 6e 6f 74 20 79 65 74 20 61 63 74 75 61 6c 6c   not yet actuall
11415 79 20 72 65 61 64 20 74 68 65 20 63 6f 6e 74 65  y read the conte
11416 6e 74 20 6f 66 20 74 68 69 73 20 70 61 67 65 20  nt of this page 
11417 28 69 66 0a 2a 2a 20 74 68 65 20 50 67 48 64 72  (if.** the PgHdr
11418 2e 6e 65 65 64 52 65 61 64 20 66 6c 61 67 20 69  .needRead flag i
11419 73 20 73 65 74 29 20 74 68 65 6e 20 74 68 69 73  s set) then this
1141a 20 72 6f 75 74 69 6e 65 20 61 63 74 73 20 61 73   routine acts as
1141b 20 61 20 70 72 6f 6d 69 73 65 0a 2a 2a 20 74 68   a promise.** th
1141c 61 74 20 77 65 20 77 69 6c 6c 20 6e 65 76 65 72  at we will never
1141d 20 6e 65 65 64 20 74 6f 20 72 65 61 64 20 74 68   need to read th
1141e 65 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 69  e page content i
1141f 6e 20 74 68 65 20 66 75 74 75 72 65 2e 0a 2a 2a  n the future..**
11420 20 73 6f 20 74 68 65 20 6e 65 65 64 52 65 61 64   so the needRead
11421 20 66 6c 61 67 20 63 61 6e 20 62 65 20 63 6c 65   flag can be cle
11422 61 72 65 64 20 61 74 20 74 68 69 73 20 70 6f 69  ared at this poi
11423 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nt..*/.SQLITE_PR
11424 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
11425 65 33 50 61 67 65 72 44 6f 6e 74 52 6f 6c 6c 62  e3PagerDontRollb
11426 61 63 6b 28 44 62 50 61 67 65 20 2a 70 50 67 29  ack(DbPage *pPg)
11427 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65  {.  Pager *pPage
11428 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b  r = pPg->pPager;
11429 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ..  assert( pPag
1142a 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52  er->state>=PAGER
1142b 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 0a 20 20  _RESERVED );..  
1142c 2f 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61  /* If the journa
1142d 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 6f 70  l file is not op
1142e 65 6e 2c 20 6f 72 20 44 6f 6e 74 57 72 69 74 65  en, or DontWrite
1142f 28 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c  () has been call
11430 65 64 20 6f 6e 0a 20 20 2a 2a 20 74 68 69 73 20  ed on.  ** this 
11431 70 61 67 65 20 28 44 6f 6e 74 57 72 69 74 65 28  page (DontWrite(
11432 29 20 73 65 74 73 20 74 68 65 20 61 6c 77 61 79  ) sets the alway
11433 73 52 6f 6c 6c 62 61 63 6b 20 66 6c 61 67 29 2c  sRollback flag),
11434 20 74 68 65 6e 20 74 68 69 73 0a 20 20 2a 2a 20   then this.  ** 
11435 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f  function is a no
11436 2d 6f 70 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  -op..  */.  if( 
11437 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
11438 70 65 6e 3d 3d 30 20 0a 20 20 20 7c 7c 20 73 71  pen==0 .   || sq
11439 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28  lite3BitvecTest(
1143a 70 50 61 67 65 72 2d 3e 70 41 6c 77 61 79 73 52  pPager->pAlwaysR
1143b 6f 6c 6c 62 61 63 6b 2c 20 70 50 67 2d 3e 70 67  ollback, pPg->pg
1143c 6e 6f 29 0a 20 20 20 7c 7c 20 70 50 67 2d 3e 70  no).   || pPg->p
1143d 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 6f 72 69 67  gno>pPager->orig
1143e 44 62 53 69 7a 65 0a 20 20 29 7b 0a 20 20 20 20  DbSize.  ){.    
1143f 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 23 69 66  return;.  }..#if
11440 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43 55 52  def SQLITE_SECUR
11441 45 5f 44 45 4c 45 54 45 0a 20 20 69 66 28 20 28  E_DELETE.  if( (
11442 70 50 67 2d 3e 66 6c 61 67 73 20 26 20 50 47 48  pPg->flags & PGH
11443 44 52 5f 49 4e 5f 4a 4f 55 52 4e 41 4c 29 21 3d  DR_IN_JOURNAL)!=
11444 30 20 7c 7c 20 28 69 6e 74 29 70 50 67 2d 3e 70  0 || (int)pPg->p
11445 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 6f 72 69 67  gno>pPager->orig
11446 44 62 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 65  DbSize ){.    re
11447 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  turn;.  }.#endif
11448 0a 0a 20 20 2f 2a 20 49 66 20 53 45 43 55 52 45  ..  /* If SECURE
11449 5f 44 45 4c 45 54 45 20 69 73 20 64 69 73 61 62  _DELETE is disab
1144a 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20  led, then there 
1144b 69 73 20 6e 6f 20 77 61 79 20 74 68 61 74 20 74  is no way that t
1144c 68 69 73 0a 20 20 2a 2a 20 72 6f 75 74 69 6e 65  his.  ** routine
1144d 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 6f   can be called o
1144e 6e 20 61 20 70 61 67 65 20 66 6f 72 20 77 68 69  n a page for whi
1144f 63 68 20 73 71 6c 69 74 65 33 50 61 67 65 72 44  ch sqlite3PagerD
11450 6f 6e 74 57 72 69 74 65 28 29 0a 20 20 2a 2a 20  ontWrite().  ** 
11451 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 70 72 65  has not been pre
11452 76 69 6f 75 73 6c 79 20 63 61 6c 6c 65 64 20 64  viously called d
11453 75 72 69 6e 67 20 74 68 65 20 73 61 6d 65 20 74  uring the same t
11454 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2a  ransaction..  **
11455 20 41 6e 64 20 69 66 20 44 6f 6e 74 57 72 69 74   And if DontWrit
11456 65 28 29 20 68 61 73 20 70 72 65 76 69 6f 75 73  e() has previous
11457 6c 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 20  ly been called, 
11458 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20  the following.  
11459 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 73 20 6d 75  ** conditions mu
1145a 73 74 20 62 65 20 6d 65 74 2e 0a 20 20 2a 2a 0a  st be met..  **.
1145b 20 20 2a 2a 20 28 4c 61 74 65 72 3a 29 20 20 4e    ** (Later:)  N
1145c 6f 74 20 74 72 75 65 2e 20 20 49 66 20 74 68 65  ot true.  If the
1145d 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f 72   database is cor
1145e 72 75 70 74 65 64 20 62 79 20 68 61 76 69 6e 67  rupted by having
1145f 20 64 75 70 6c 69 63 61 74 65 0a 20 20 2a 2a 20   duplicate.  ** 
11460 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65  pages on the fre
11461 65 6c 69 73 74 20 28 65 78 3a 20 63 6f 72 72 75  elist (ex: corru
11462 70 74 39 2e 74 65 73 74 29 20 74 68 65 6e 20 74  pt9.test) then t
11463 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20  he following is 
11464 6e 6f 74 0a 20 20 2a 2a 20 6e 65 63 65 73 73 61  not.  ** necessa
11465 72 69 6c 79 20 74 72 75 65 3a 0a 20 20 2a 2f 0a  rily true:.  */.
11466 20 20 2f 2a 20 61 73 73 65 72 74 28 20 21 70 50    /* assert( !pP
11467 67 2d 3e 69 6e 4a 6f 75 72 6e 61 6c 20 26 26 20  g->inJournal && 
11468 28 69 6e 74 29 70 50 67 2d 3e 70 67 6e 6f 20 3c  (int)pPg->pgno <
11469 3d 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62  = pPager->origDb
1146a 53 69 7a 65 20 29 3b 20 2a 2f 0a 0a 20 20 61 73  Size ); */..  as
1146b 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49  sert( pPager->pI
1146c 6e 4a 6f 75 72 6e 61 6c 21 3d 30 20 29 3b 0a 20  nJournal!=0 );. 
1146d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65   sqlite3BitvecSe
1146e 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75  t(pPager->pInJou
1146f 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29  rnal, pPg->pgno)
11470 3b 0a 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c  ;.  pPg->flags |
11471 3d 20 50 47 48 44 52 5f 49 4e 5f 4a 4f 55 52 4e  = PGHDR_IN_JOURN
11472 41 4c 3b 0a 20 20 70 50 67 2d 3e 66 6c 61 67 73  AL;.  pPg->flags
11473 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f   &= ~PGHDR_NEED_
11474 52 45 41 44 3b 0a 20 20 69 66 28 20 70 50 61 67  READ;.  if( pPag
11475 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 29 7b  er->stmtInUse ){
11476 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
11477 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65 20 3e 3d  ger->stmtSize >=
11478 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62 53   pPager->origDbS
11479 69 7a 65 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ize );.    sqlit
1147a 65 33 42 69 74 76 65 63 53 65 74 28 70 50 61 67  e3BitvecSet(pPag
1147b 65 72 2d 3e 70 49 6e 53 74 6d 74 2c 20 70 50 67  er->pInStmt, pPg
1147c 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 50  ->pgno);.  }.  P
1147d 41 47 45 52 54 52 41 43 45 33 28 22 44 4f 4e 54  AGERTRACE3("DONT
1147e 5f 52 4f 4c 4c 42 41 43 4b 20 70 61 67 65 20 25  _ROLLBACK page %
1147f 64 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 67 2d  d of %d\n", pPg-
11480 3e 70 67 6e 6f 2c 20 50 41 47 45 52 49 44 28 70  >pgno, PAGERID(p
11481 50 61 67 65 72 29 29 3b 0a 20 20 49 4f 54 52 41  Pager));.  IOTRA
11482 43 45 28 28 22 47 41 52 42 41 47 45 20 25 70 20  CE(("GARBAGE %p 
11483 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70  %d\n", pPager, p
11484 50 67 2d 3e 70 67 6e 6f 29 29 0a 7d 0a 0a 0a 2f  Pg->pgno)).}.../
11485 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
11486 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 69  e is called to i
11487 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 64 61 74  ncrement the dat
11488 61 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67  abase file chang
11489 65 2d 63 6f 75 6e 74 65 72 2c 0a 2a 2a 20 73 74  e-counter,.** st
1148a 6f 72 65 64 20 61 74 20 62 79 74 65 20 32 34 20  ored at byte 24 
1148b 6f 66 20 74 68 65 20 70 61 67 65 72 20 66 69 6c  of the pager fil
1148c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1148d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e   pager_incr_chan
1148e 67 65 63 6f 75 6e 74 65 72 28 50 61 67 65 72 20  gecounter(Pager 
1148f 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 69 73 44  *pPager, int isD
11490 69 72 65 63 74 29 7b 0a 20 20 50 67 48 64 72 20  irect){.  PgHdr 
11491 2a 70 50 67 48 64 72 3b 0a 20 20 75 33 32 20 63  *pPgHdr;.  u32 c
11492 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 3b 0a 20  hange_counter;. 
11493 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
11494 5f 4f 4b 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  _OK;..#ifndef SQ
11495 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d  LITE_ENABLE_ATOM
11496 49 43 5f 57 52 49 54 45 0a 20 20 61 73 73 65 72  IC_WRITE.  asser
11497 74 28 20 69 73 44 69 72 65 63 74 3d 3d 30 20 29  t( isDirect==0 )
11498 3b 20 20 2f 2a 20 69 73 44 69 72 65 63 74 20 69  ;  /* isDirect i
11499 73 20 6f 6e 6c 79 20 74 72 75 65 20 66 6f 72 20  s only true for 
1149a 61 74 6f 6d 69 63 20 77 72 69 74 65 73 20 2a 2f  atomic writes */
1149b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 21 70  .#endif.  if( !p
1149c 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75  Pager->changeCou
1149d 6e 74 44 6f 6e 65 20 29 7b 0a 20 20 20 20 2f 2a  ntDone ){.    /*
1149e 20 4f 70 65 6e 20 70 61 67 65 20 31 20 6f 66 20   Open page 1 of 
1149f 74 68 65 20 66 69 6c 65 20 66 6f 72 20 77 72 69  the file for wri
114a0 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 63 20  ting. */.    rc 
114a1 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
114a2 74 28 70 50 61 67 65 72 2c 20 31 2c 20 26 70 50  t(pPager, 1, &pP
114a3 67 48 64 72 29 3b 0a 20 20 20 20 69 66 28 20 72  gHdr);.    if( r
114a4 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
114a5 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 69  eturn rc;..    i
114a6 66 28 20 21 69 73 44 69 72 65 63 74 20 29 7b 0a  f( !isDirect ){.
114a7 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
114a8 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 67  e3PagerWrite(pPg
114a9 48 64 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20  Hdr);.      if( 
114aa 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
114ab 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
114ac 50 61 67 65 72 55 6e 72 65 66 28 70 50 67 48 64  PagerUnref(pPgHd
114ad 72 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  r);.        retu
114ae 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20  rn rc;.      }. 
114af 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 6e 63     }..    /* Inc
114b0 72 65 6d 65 6e 74 20 74 68 65 20 76 61 6c 75 65  rement the value
114b1 20 6a 75 73 74 20 72 65 61 64 20 61 6e 64 20 77   just read and w
114b2 72 69 74 65 20 69 74 20 62 61 63 6b 20 74 6f 20  rite it back to 
114b3 62 79 74 65 20 32 34 2e 20 2a 2f 0a 20 20 20 20  byte 24. */.    
114b4 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 20 3d  change_counter =
114b5 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65   sqlite3Get4byte
114b6 28 28 75 38 2a 29 70 50 61 67 65 72 2d 3e 64 62  ((u8*)pPager->db
114b7 46 69 6c 65 56 65 72 73 29 3b 0a 20 20 20 20 63  FileVers);.    c
114b8 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 2b 2b 3b  hange_counter++;
114b9 0a 20 20 20 20 70 75 74 33 32 62 69 74 73 28 28  .    put32bits((
114ba 28 63 68 61 72 2a 29 70 50 67 48 64 72 2d 3e 70  (char*)pPgHdr->p
114bb 44 61 74 61 29 2b 32 34 2c 20 63 68 61 6e 67 65  Data)+24, change
114bc 5f 63 6f 75 6e 74 65 72 29 3b 0a 0a 23 69 66 64  _counter);..#ifd
114bd 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
114be 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20  _ATOMIC_WRITE.  
114bf 20 20 69 66 28 20 69 73 44 69 72 65 63 74 20 26    if( isDirect &
114c0 26 20 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d  & pPager->fd->pM
114c1 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 20 20  ethods ){.      
114c2 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66  const void *zBuf
114c3 20 3d 20 70 50 67 48 64 72 2d 3e 70 44 61 74 61   = pPgHdr->pData
114c4 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
114c5 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67  ite3OsWrite(pPag
114c6 65 72 2d 3e 66 64 2c 20 7a 42 75 66 2c 20 70 50  er->fd, zBuf, pP
114c7 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20  ager->pageSize, 
114c8 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  0);.    }.#endif
114c9 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65  ..    /* Release
114ca 20 74 68 65 20 70 61 67 65 20 72 65 66 65 72 65   the page refere
114cb 6e 63 65 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69  nce. */.    sqli
114cc 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50  te3PagerUnref(pP
114cd 67 48 64 72 29 3b 0a 20 20 20 20 70 50 61 67 65  gHdr);.    pPage
114ce 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f  r->changeCountDo
114cf 6e 65 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65  ne = 1;.  }.  re
114d0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
114d1 2a 20 53 79 6e 63 20 74 68 65 20 70 61 67 65 72  * Sync the pager
114d2 20 66 69 6c 65 20 74 6f 20 64 69 73 6b 2e 0a 2a   file to disk..*
114d3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
114d4 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
114d5 72 53 79 6e 63 28 50 61 67 65 72 20 2a 70 50 61  rSync(Pager *pPa
114d6 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  ger){.  int rc;.
114d7 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20    if( MEMDB ){. 
114d8 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
114d9 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  K;.  }else{.    
114da 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79  rc = sqlite3OsSy
114db 6e 63 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70  nc(pPager->fd, p
114dc 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67  Pager->sync_flag
114dd 73 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  s);.  }.  return
114de 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79   rc;.}../*.** Sy
114df 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65 20  nc the database 
114e0 66 69 6c 65 20 66 6f 72 20 74 68 65 20 70 61 67  file for the pag
114e1 65 72 20 70 50 61 67 65 72 2e 20 7a 4d 61 73 74  er pPager. zMast
114e2 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  er points to the
114e3 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 61 20 6d 61   name.** of a ma
114e4 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
114e5 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65  e that should be
114e6 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
114e7 65 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20  e individual.** 
114e8 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 7a 4d  journal file. zM
114e9 61 73 74 65 72 20 6d 61 79 20 62 65 20 4e 55 4c  aster may be NUL
114ea 4c 2c 20 77 68 69 63 68 20 69 73 20 69 6e 74 65  L, which is inte
114eb 72 70 72 65 74 65 64 20 61 73 20 6e 6f 20 6d 61  rpreted as no ma
114ec 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20  ster.** journal 
114ed 28 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61  (a single databa
114ee 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e  se transaction).
114ef 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
114f0 69 6e 65 20 65 6e 73 75 72 65 73 20 74 68 61 74  ine ensures that
114f1 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20   the journal is 
114f2 73 79 6e 63 65 64 2c 20 61 6c 6c 20 64 69 72 74  synced, all dirt
114f3 79 20 70 61 67 65 73 20 77 72 69 74 74 65 6e 0a  y pages written.
114f4 2a 2a 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ** to the databa
114f5 73 65 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20  se file and the 
114f6 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 73 79  database file sy
114f7 6e 63 65 64 2e 20 54 68 65 20 6f 6e 6c 79 20 74  nced. The only t
114f8 68 69 6e 67 20 74 68 61 74 0a 2a 2a 20 72 65 6d  hing that.** rem
114f9 61 69 6e 73 20 74 6f 20 63 6f 6d 6d 69 74 20 74  ains to commit t
114fa 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  he transaction i
114fb 73 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20  s to delete the 
114fc 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72  journal file (or
114fd 0a 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  .** master journ
114fe 61 6c 20 66 69 6c 65 20 69 66 20 73 70 65 63 69  al file if speci
114ff 66 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74  fied)..**.** Not
11500 65 20 74 68 61 74 20 69 66 20 7a 4d 61 73 74 65  e that if zMaste
11501 72 3d 3d 4e 55 4c 4c 2c 20 74 68 69 73 20 64 6f  r==NULL, this do
11502 65 73 20 6e 6f 74 20 6f 76 65 72 77 72 69 74 65  es not overwrite
11503 20 61 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75   a previous valu
11504 65 0a 2a 2a 20 70 61 73 73 65 64 20 74 6f 20 61  e.** passed to a
11505 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f  n sqlite3PagerCo
11506 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 63  mmitPhaseOne() c
11507 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61  all..**.** If pa
11508 72 61 6d 65 74 65 72 20 6e 54 72 75 6e 63 20 69  rameter nTrunc i
11509 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
1150a 20 74 68 65 20 70 61 67 65 72 20 66 69 6c 65 20   the pager file 
1150b 69 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f 0a  is truncated to.
1150c 2a 2a 20 6e 54 72 75 6e 63 20 70 61 67 65 73 20  ** nTrunc pages 
1150d 28 74 68 69 73 20 69 73 20 75 73 65 64 20 62 79  (this is used by
1150e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74   auto-vacuum dat
1150f 61 62 61 73 65 73 29 2e 0a 2a 2a 0a 2a 2a 20 49  abases)..**.** I
11510 66 20 74 68 65 20 66 69 6e 61 6c 20 70 61 72 61  f the final para
11511 6d 65 74 65 72 20 2d 20 6e 6f 53 79 6e 63 20 2d  meter - noSync -
11512 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74   is true, then t
11513 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
11514 20 69 74 73 65 6c 66 0a 2a 2a 20 69 73 20 6e 6f   itself.** is no
11515 74 20 73 79 6e 63 65 64 2e 20 54 68 65 20 63 61  t synced. The ca
11516 6c 6c 65 72 20 6d 75 73 74 20 63 61 6c 6c 20 73  ller must call s
11517 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28  qlite3PagerSync(
11518 29 20 64 69 72 65 63 74 6c 79 20 74 6f 0a 2a 2a  ) directly to.**
11519 20 73 79 6e 63 20 74 68 65 20 64 61 74 61 62 61   sync the databa
1151a 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65 20 63  se file before c
1151b 61 6c 6c 69 6e 67 20 43 6f 6d 6d 69 74 50 68 61  alling CommitPha
1151c 73 65 54 77 6f 28 29 20 74 6f 20 64 65 6c 65 74  seTwo() to delet
1151d 65 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c  e the.** journal
1151e 20 66 69 6c 65 20 69 6e 20 74 68 69 73 20 63 61   file in this ca
1151f 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  se..*/.SQLITE_PR
11520 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
11521 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73  3PagerCommitPhas
11522 65 4f 6e 65 28 0a 20 20 50 61 67 65 72 20 2a 70  eOne(.  Pager *p
11523 50 61 67 65 72 2c 20 0a 20 20 63 6f 6e 73 74 20  Pager, .  const 
11524 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c 20 0a  char *zMaster, .
11525 20 20 50 67 6e 6f 20 6e 54 72 75 6e 63 2c 0a 20    Pgno nTrunc,. 
11526 20 69 6e 74 20 6e 6f 53 79 6e 63 0a 29 7b 0a 20   int noSync.){. 
11527 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
11528 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 70 50 61 67  _OK;..  if( pPag
11529 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20  er->errCode ){. 
1152a 20 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72     return pPager
1152b 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d 0a 0a  ->errCode;.  }..
1152c 20 20 2f 2a 20 49 66 20 6e 6f 20 63 68 61 6e 67    /* If no chang
1152d 65 73 20 68 61 76 65 20 62 65 65 6e 20 6d 61 64  es have been mad
1152e 65 2c 20 77 65 20 63 61 6e 20 6c 65 61 76 65 20  e, we can leave 
1152f 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
11530 65 61 72 6c 79 2e 0a 20 20 2a 2f 0a 20 20 69 66  early..  */.  if
11531 28 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69  ( pPager->dbModi
11532 66 69 65 64 3d 3d 30 20 26 26 0a 20 20 20 20 20  fied==0 &&.     
11533 20 20 20 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72     (pPager->jour
11534 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a  nalMode!=PAGER_J
11535 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54  OURNALMODE_DELET
11536 45 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 70  E ||.          p
11537 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65  Pager->exclusive
11538 4d 6f 64 65 21 3d 30 29 20 29 7b 0a 20 20 20 20  Mode!=0) ){.    
11539 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
1153a 64 69 72 74 79 43 61 63 68 65 3d 3d 30 20 7c 7c  dirtyCache==0 ||
1153b 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
1153c 4f 70 65 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 72  Open==0 );.    r
1153d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1153e 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41  .  }..  PAGERTRA
1153f 43 45 34 28 22 44 41 54 41 42 41 53 45 20 53 59  CE4("DATABASE SY
11540 4e 43 3a 20 46 69 6c 65 3d 25 73 20 7a 4d 61 73  NC: File=%s zMas
11541 74 65 72 3d 25 73 20 6e 54 72 75 6e 63 3d 25 64  ter=%s nTrunc=%d
11542 5c 6e 22 2c 20 0a 20 20 20 20 20 20 70 50 61 67  \n", .      pPag
11543 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 7a  er->zFilename, z
11544 4d 61 73 74 65 72 2c 20 6e 54 72 75 6e 63 29 3b  Master, nTrunc);
11545 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69  ..  /* If this i
11546 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64  s an in-memory d
11547 62 2c 20 6f 72 20 6e 6f 20 70 61 67 65 73 20 68  b, or no pages h
11548 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e  ave been written
11549 20 74 6f 2c 20 6f 72 20 74 68 69 73 0a 20 20 2a   to, or this.  *
1154a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 61  * function has a
1154b 6c 72 65 61 64 79 20 62 65 65 6e 20 63 61 6c 6c  lready been call
1154c 65 64 2c 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f  ed, it is a no-o
1154d 70 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50  p..  */.  if( pP
1154e 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47  ager->state!=PAG
1154f 45 52 5f 53 59 4e 43 45 44 20 26 26 20 21 4d 45  ER_SYNCED && !ME
11550 4d 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e 64  MDB && pPager->d
11551 69 72 74 79 43 61 63 68 65 20 29 7b 0a 20 20 20  irtyCache ){.   
11552 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 0a 23 69   PgHdr *pPg;..#i
11553 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
11554 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a  LE_ATOMIC_WRITE.
11555 20 20 20 20 2f 2a 20 54 68 65 20 61 74 6f 6d 69      /* The atomi
11556 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61  c-write optimiza
11557 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64  tion can be used
11558 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 0a 20   if all of the. 
11559 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20     ** following 
1155a 61 72 65 20 74 72 75 65 3a 0a 20 20 20 20 2a 2a  are true:.    **
1155b 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 54 68 65  .    **    + The
1155c 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 73 75 70   file-system sup
1155d 70 6f 72 74 73 20 74 68 65 20 61 74 6f 6d 69 63  ports the atomic
1155e 2d 77 72 69 74 65 20 70 72 6f 70 65 72 74 79 20  -write property 
1155f 66 6f 72 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  for.    **      
11560 62 6c 6f 63 6b 73 20 6f 66 20 73 69 7a 65 20 70  blocks of size p
11561 61 67 65 2d 73 69 7a 65 2c 20 61 6e 64 0a 20 20  age-size, and.  
11562 20 20 2a 2a 20 20 20 20 2b 20 54 68 69 73 20 63    **    + This c
11563 6f 6d 6d 69 74 20 69 73 20 6e 6f 74 20 70 61 72  ommit is not par
11564 74 20 6f 66 20 61 20 6d 75 6c 74 69 2d 66 69 6c  t of a multi-fil
11565 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61  e transaction, a
11566 6e 64 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 45  nd.    **    + E
11567 78 61 63 74 6c 79 20 6f 6e 65 20 70 61 67 65 20  xactly one page 
11568 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65  has been modifie
11569 64 20 61 6e 64 20 73 74 6f 72 65 20 69 6e 20 74  d and store in t
1156a 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  he journal file.
1156b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49  .    **.    ** I
1156c 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69  f the optimizati
1156d 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64 2c 20  on can be used, 
1156e 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  then the journal
1156f 20 66 69 6c 65 20 77 69 6c 6c 20 6e 65 76 65 72   file will never
11570 0a 20 20 20 20 2a 2a 20 62 65 20 63 72 65 61 74  .    ** be creat
11571 65 64 20 66 6f 72 20 74 68 69 73 20 74 72 61 6e  ed for this tran
11572 73 61 63 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a  saction..    */.
11573 20 20 20 20 69 6e 74 20 75 73 65 41 74 6f 6d 69      int useAtomi
11574 63 57 72 69 74 65 3b 0a 20 20 20 20 70 50 67 20  cWrite;.    pPg 
11575 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 44  = sqlite3PcacheD
11576 69 72 74 79 4c 69 73 74 28 70 50 61 67 65 72 2d  irtyList(pPager-
11577 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 20 20 75  >pPCache);.    u
11578 73 65 41 74 6f 6d 69 63 57 72 69 74 65 20 3d 20  seAtomicWrite = 
11579 28 0a 20 20 20 20 20 20 20 20 21 7a 4d 61 73 74  (.        !zMast
1157a 65 72 20 26 26 20 0a 20 20 20 20 20 20 20 20 70  er && .        p
1157b 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70  Pager->journalOp
1157c 65 6e 20 26 26 0a 20 20 20 20 20 20 20 20 70 50  en &&.        pP
1157d 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
1157e 3d 3d 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a 65  ==jrnlBufferSize
1157f 28 70 50 61 67 65 72 29 20 26 26 20 0a 20 20 20  (pPager) && .   
11580 20 20 20 20 20 6e 54 72 75 6e 63 3d 3d 30 20 26       nTrunc==0 &
11581 26 20 0a 20 20 20 20 20 20 20 20 28 70 50 67 3d  & .        (pPg=
11582 3d 30 20 7c 7c 20 70 50 67 2d 3e 70 44 69 72 74  =0 || pPg->pDirt
11583 79 3d 3d 30 29 0a 20 20 20 20 29 3b 0a 20 20 20  y==0).    );.   
11584 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
11585 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 7c 7c 20  >journalOpen || 
11586 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
11587 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
11588 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a 20 20  ALMODE_OFF );.  
11589 20 20 69 66 28 20 75 73 65 41 74 6f 6d 69 63 57    if( useAtomicW
1158a 72 69 74 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a  rite ){.      /*
1158b 20 55 70 64 61 74 65 20 74 68 65 20 6e 52 65 63   Update the nRec
1158c 20 66 69 65 6c 64 20 69 6e 20 74 68 65 20 6a 6f   field in the jo
1158d 75 72 6e 61 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20  urnal file. */. 
1158e 20 20 20 20 20 69 6e 74 20 6f 66 66 73 65 74 20       int offset 
1158f 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  = pPager->journa
11590 6c 48 64 72 20 2b 20 73 69 7a 65 6f 66 28 61 4a  lHdr + sizeof(aJ
11591 6f 75 72 6e 61 6c 4d 61 67 69 63 29 3b 0a 20 20  ournalMagic);.  
11592 20 20 20 20 61 73 73 65 72 74 28 70 50 61 67 65      assert(pPage
11593 72 2d 3e 6e 52 65 63 3d 3d 31 29 3b 0a 20 20 20  r->nRec==1);.   
11594 20 20 20 72 63 20 3d 20 77 72 69 74 65 33 32 62     rc = write32b
11595 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  its(pPager->jfd,
11596 20 6f 66 66 73 65 74 2c 20 70 50 61 67 65 72 2d   offset, pPager-
11597 3e 6e 52 65 63 29 3b 0a 0a 20 20 20 20 20 20 2f  >nRec);..      /
11598 2a 20 55 70 64 61 74 65 20 74 68 65 20 64 62 20  * Update the db 
11599 66 69 6c 65 20 63 68 61 6e 67 65 20 63 6f 75 6e  file change coun
1159a 74 65 72 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ter. The followi
1159b 6e 67 20 63 61 6c 6c 20 77 69 6c 6c 20 6d 6f 64  ng call will mod
1159c 69 66 79 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  ify.      ** the
1159d 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65   in-memory repre
1159e 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 70 61 67  sentation of pag
1159f 65 20 31 20 74 6f 20 69 6e 63 6c 75 64 65 20 74  e 1 to include t
115a0 68 65 20 75 70 64 61 74 65 64 0a 20 20 20 20 20  he updated.     
115a1 20 2a 2a 20 63 68 61 6e 67 65 20 63 6f 75 6e 74   ** change count
115a2 65 72 20 61 6e 64 20 74 68 65 6e 20 77 72 69 74  er and then writ
115a3 65 20 70 61 67 65 20 31 20 64 69 72 65 63 74 6c  e page 1 directl
115a4 79 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  y to the databas
115a5 65 0a 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 2e  e.      ** file.
115a6 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 65 20   Because of the 
115a7 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 70 72 6f  atomic-write pro
115a8 70 65 72 74 79 20 6f 66 20 74 68 65 20 68 6f 73  perty of the hos
115a9 74 20 66 69 6c 65 2d 73 79 73 74 65 6d 2c 20 0a  t file-system, .
115aa 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 69 73        ** this is
115ab 20 73 61 66 65 2e 0a 20 20 20 20 20 20 2a 2f 0a   safe..      */.
115ac 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
115ad 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
115ae 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 69 6e     rc = pager_in
115af 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72  cr_changecounter
115b0 28 70 50 61 67 65 72 2c 20 31 29 3b 0a 20 20 20  (pPager, 1);.   
115b1 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
115b2 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
115b3 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28  e3JournalCreate(
115b4 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20  pPager->jfd);.  
115b5 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 21 75 73    }..    if( !us
115b6 65 41 74 6f 6d 69 63 57 72 69 74 65 20 26 26 20  eAtomicWrite && 
115b7 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 0a  rc==SQLITE_OK ).
115b8 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 49  #endif..    /* I
115b9 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  f a master journ
115ba 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 68 61 73  al file name has
115bb 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 77 72   already been wr
115bc 69 74 74 65 6e 20 74 6f 20 74 68 65 0a 20 20 20  itten to the.   
115bd 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65   ** journal file
115be 2c 20 74 68 65 6e 20 6e 6f 20 73 79 6e 63 20 69  , then no sync i
115bf 73 20 72 65 71 75 69 72 65 64 2e 20 54 68 69 73  s required. This
115c0 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 69 74   happens when it
115c1 20 69 73 0a 20 20 20 20 2a 2a 20 77 72 69 74 74   is.    ** writt
115c2 65 6e 2c 20 74 68 65 6e 20 74 68 65 20 70 72 6f  en, then the pro
115c3 63 65 73 73 20 66 61 69 6c 73 20 74 6f 20 75 70  cess fails to up
115c4 67 72 61 64 65 20 66 72 6f 6d 20 61 20 52 45 53  grade from a RES
115c5 45 52 56 45 44 20 74 6f 20 61 6e 0a 20 20 20 20  ERVED to an.    
115c6 2a 2a 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  ** EXCLUSIVE loc
115c7 6b 2e 20 54 68 65 20 6e 65 78 74 20 74 69 6d 65  k. The next time
115c8 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 72 69   the process tri
115c9 65 73 20 74 6f 20 63 6f 6d 6d 69 74 20 74 68 65  es to commit the
115ca 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74  .    ** transact
115cb 69 6f 6e 20 74 68 65 20 6d 2d 6a 20 6e 61 6d 65  ion the m-j name
115cc 20 77 69 6c 6c 20 68 61 76 65 20 61 6c 72 65 61   will have alrea
115cd 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e 2e  dy been written.
115ce 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
115cf 21 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74  !pPager->setMast
115d0 65 72 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  er ){.      rc =
115d1 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e   pager_incr_chan
115d2 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67 65 72  gecounter(pPager
115d3 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 0);.      if( 
115d4 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
115d5 67 6f 74 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a  goto sync_exit;.
115d6 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 72        if( pPager
115d7 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50  ->journalMode!=P
115d8 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
115d9 5f 4f 46 46 20 29 7b 0a 23 69 66 6e 64 65 66 20  _OFF ){.#ifndef 
115da 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
115db 56 41 43 55 55 4d 0a 20 20 20 20 20 20 20 20 69  VACUUM.        i
115dc 66 28 20 6e 54 72 75 6e 63 21 3d 30 20 29 7b 0a  f( nTrunc!=0 ){.
115dd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20            /* If 
115de 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e  this transaction
115df 20 68 61 73 20 6d 61 64 65 20 74 68 65 20 64 61   has made the da
115e0 74 61 62 61 73 65 20 73 6d 61 6c 6c 65 72 2c 20  tabase smaller, 
115e1 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 0a 20  then all pages. 
115e2 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 69 6e           ** bein
115e3 67 20 64 69 73 63 61 72 64 65 64 20 62 79 20 74  g discarded by t
115e4 68 65 20 74 72 75 6e 63 61 74 69 6f 6e 20 6d 75  he truncation mu
115e5 73 74 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f  st be written to
115e6 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20   the journal.   
115e7 20 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 2e 0a         ** file..
115e8 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
115e9 20 20 20 20 20 20 20 50 67 6e 6f 20 69 3b 0a 20         Pgno i;. 
115ea 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 53 6b           int iSk
115eb 69 70 20 3d 20 50 41 47 45 52 5f 4d 4a 5f 50 47  ip = PAGER_MJ_PG
115ec 4e 4f 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  NO(pPager);.    
115ed 20 20 20 20 20 20 66 6f 72 28 20 69 3d 6e 54 72        for( i=nTr
115ee 75 6e 63 2b 31 3b 20 69 3c 3d 70 50 61 67 65 72  unc+1; i<=pPager
115ef 2d 3e 6f 72 69 67 44 62 53 69 7a 65 3b 20 69 2b  ->origDbSize; i+
115f0 2b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  + ){.           
115f1 20 69 66 28 20 21 73 71 6c 69 74 65 33 42 69 74   if( !sqlite3Bit
115f2 76 65 63 54 65 73 74 28 70 50 61 67 65 72 2d 3e  vecTest(pPager->
115f3 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 69 29 20 26  pInJournal, i) &
115f4 26 20 69 21 3d 69 53 6b 69 70 20 29 7b 0a 20 20  & i!=iSkip ){.  
115f5 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
115f6 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
115f7 28 70 50 61 67 65 72 2c 20 69 2c 20 26 70 50 67  (pPager, i, &pPg
115f8 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
115f9 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
115fa 4f 4b 20 29 20 67 6f 74 6f 20 73 79 6e 63 5f 65  OK ) goto sync_e
115fb 78 69 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20  xit;.           
115fc 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
115fd 61 67 65 72 57 72 69 74 65 28 70 50 67 29 3b 0a  agerWrite(pPg);.
115fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
115ff 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
11600 70 50 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20  pPg);.          
11601 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
11602 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 73 79 6e  TE_OK ) goto syn
11603 63 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 20 20  c_exit;.        
11604 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
11605 7d 20 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e  } .        }.#en
11606 64 69 66 0a 20 20 20 20 20 20 20 20 72 63 20 3d  dif.        rc =
11607 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72   writeMasterJour
11608 6e 61 6c 28 70 50 61 67 65 72 2c 20 7a 4d 61 73  nal(pPager, zMas
11609 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ter);.        if
1160a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1160b 29 20 67 6f 74 6f 20 73 79 6e 63 5f 65 78 69 74  ) goto sync_exit
1160c 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
1160d 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65  yncJournal(pPage
1160e 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  r);.      }.    
1160f 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  }.    if( rc!=SQ
11610 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 73  LITE_OK ) goto s
11611 79 6e 63 5f 65 78 69 74 3b 0a 0a 23 69 66 6e 64  ync_exit;..#ifnd
11612 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
11613 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66  UTOVACUUM.    if
11614 28 20 6e 54 72 75 6e 63 21 3d 30 20 29 7b 0a 20  ( nTrunc!=0 ){. 
11615 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
11616 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 28 70  3PagerTruncate(p
11617 50 61 67 65 72 2c 20 6e 54 72 75 6e 63 29 3b 0a  Pager, nTrunc);.
11618 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
11619 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 73  LITE_OK ) goto s
1161a 79 6e 63 5f 65 78 69 74 3b 0a 20 20 20 20 7d 0a  ync_exit;.    }.
1161b 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 57  #endif..    /* W
1161c 72 69 74 65 20 61 6c 6c 20 64 69 72 74 79 20 70  rite all dirty p
1161d 61 67 65 73 20 74 6f 20 74 68 65 20 64 61 74 61  ages to the data
1161e 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20  base file */.   
1161f 20 70 50 67 20 3d 20 73 71 6c 69 74 65 33 50 63   pPg = sqlite3Pc
11620 61 63 68 65 44 69 72 74 79 4c 69 73 74 28 70 50  acheDirtyList(pP
11621 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a  ager->pPCache);.
11622 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77      rc = pager_w
11623 72 69 74 65 5f 70 61 67 65 6c 69 73 74 28 70 50  rite_pagelist(pP
11624 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  g);.    if( rc!=
11625 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
11626 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
11627 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43  QLITE_IOERR_BLOC
11628 4b 45 44 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20  KED );.      /* 
11629 54 68 65 20 65 72 72 6f 72 20 6d 69 67 68 74 20  The error might 
1162a 68 61 76 65 20 6c 65 66 74 20 74 68 65 20 64 69  have left the di
1162b 72 74 79 20 6c 69 73 74 20 61 6c 6c 20 66 6f 75  rty list all fou
1162c 6c 65 64 20 75 70 20 68 65 72 65 2c 0a 20 20 20  led up here,.   
1162d 20 20 20 2a 2a 20 62 75 74 20 74 68 61 74 20 64     ** but that d
1162e 6f 65 73 20 6e 6f 74 20 6d 61 74 74 65 72 20 62  oes not matter b
1162f 65 63 61 75 73 65 20 69 66 20 74 68 65 20 69 66  ecause if the if
11630 20 74 68 65 20 64 69 72 74 79 20 6c 69 73 74 20   the dirty list 
11631 64 69 64 0a 20 20 20 20 20 20 2a 2a 20 67 65 74  did.      ** get
11632 20 63 6f 72 72 75 70 74 65 64 2c 20 74 68 65 6e   corrupted, then
11633 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
11634 20 77 69 6c 6c 20 72 6f 6c 6c 20 62 61 63 6b 20   will roll back 
11635 61 6e 64 0a 20 20 20 20 20 20 2a 2a 20 64 69 73  and.      ** dis
11636 63 61 72 64 20 74 68 65 20 64 69 72 74 79 20 6c  card the dirty l
11637 69 73 74 2e 20 20 54 68 65 72 65 20 69 73 20 61  ist.  There is a
11638 6e 20 61 73 73 65 72 74 20 69 6e 0a 20 20 20 20  n assert in.    
11639 20 20 2a 2a 20 70 61 67 65 72 5f 67 65 74 5f 61    ** pager_get_a
1163a 6c 6c 5f 64 69 72 74 79 5f 70 61 67 65 73 28 29  ll_dirty_pages()
1163b 20 74 68 61 74 20 76 65 72 69 66 69 65 73 20 74   that verifies t
1163c 68 61 74 20 6e 6f 20 61 74 74 65 6d 70 74 0a 20  hat no attempt. 
1163d 20 20 20 20 20 2a 2a 20 69 73 20 6d 61 64 65 20       ** is made 
1163e 74 6f 20 75 73 65 20 61 6e 20 69 6e 76 61 6c 69  to use an invali
1163f 64 20 64 69 72 74 79 20 6c 69 73 74 2e 0a 20 20  d dirty list..  
11640 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 67 6f 74      */.      got
11641 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a 20 20 20  o sync_exit;.   
11642 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63   }.    sqlite3Pc
11643 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 70 50 61  acheCleanAll(pPa
11644 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a  ger->pPCache);..
11645 20 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20      /* Sync the 
11646 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a  database file. *
11647 2f 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65  /.    if( !pPage
11648 72 2d 3e 6e 6f 53 79 6e 63 20 26 26 20 21 6e 6f  r->noSync && !no
11649 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 63  Sync ){.      rc
1164a 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63   = sqlite3OsSync
1164b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61  (pPager->fd, pPa
1164c 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29  ger->sync_flags)
1164d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 49 4f 54 52  ;.    }.    IOTR
1164e 41 43 45 28 28 22 44 42 53 59 4e 43 20 25 70 5c  ACE(("DBSYNC %p\
1164f 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 0a 20 20  n", pPager))..  
11650 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20    pPager->state 
11651 3d 20 50 41 47 45 52 5f 53 59 4e 43 45 44 3b 0a  = PAGER_SYNCED;.
11652 20 20 7d 65 6c 73 65 20 69 66 28 20 4d 45 4d 44    }else if( MEMD
11653 42 20 26 26 20 6e 54 72 75 6e 63 21 3d 30 20 29  B && nTrunc!=0 )
11654 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
11655 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 28  e3PagerTruncate(
11656 70 50 61 67 65 72 2c 20 6e 54 72 75 6e 63 29 3b  pPager, nTrunc);
11657 0a 20 20 7d 0a 0a 73 79 6e 63 5f 65 78 69 74 3a  .  }..sync_exit:
11658 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
11659 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20  E_IOERR_BLOCKED 
1165a 29 7b 0a 20 20 20 20 2f 2a 20 70 61 67 65 72 5f  ){.    /* pager_
1165b 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74  incr_changecount
1165c 65 72 28 29 20 6d 61 79 20 61 74 74 65 6d 70 74  er() may attempt
1165d 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e 20 65 78   to obtain an ex
1165e 63 6c 75 73 69 76 65 0a 20 20 20 20 20 2a 20 6c  clusive.     * l
1165f 6f 63 6b 20 74 6f 20 73 70 69 6c 6c 20 74 68 65  ock to spill the
11660 20 63 61 63 68 65 20 61 6e 64 20 72 65 74 75 72   cache and retur
11661 6e 20 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 2e  n IOERR_BLOCKED.
11662 20 42 75 74 20 73 69 6e 63 65 20 0a 20 20 20 20   But since .    
11663 20 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63   * there is no c
11664 68 61 6e 63 65 20 74 68 65 20 63 61 63 68 65 20  hance the cache 
11665 69 73 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 2c  is inconsistent,
11666 20 69 74 20 69 73 0a 20 20 20 20 20 2a 20 62 65   it is.     * be
11667 74 74 65 72 20 74 6f 20 72 65 74 75 72 6e 20 53  tter to return S
11668 51 4c 49 54 45 5f 42 55 53 59 2e 0a 20 20 20 20  QLITE_BUSY..    
11669 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   */.    rc = SQL
1166a 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20  ITE_BUSY;.  }.  
1166b 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
1166c 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 61 6c 6c 20  *.** Commit all 
1166d 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 64  changes to the d
1166e 61 74 61 62 61 73 65 20 61 6e 64 20 72 65 6c 65  atabase and rele
1166f 61 73 65 20 74 68 65 20 77 72 69 74 65 20 6c 6f  ase the write lo
11670 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ck..**.** If the
11671 20 63 6f 6d 6d 69 74 20 66 61 69 6c 73 20 66 6f   commit fails fo
11672 72 20 61 6e 79 20 72 65 61 73 6f 6e 2c 20 61 20  r any reason, a 
11673 72 6f 6c 6c 62 61 63 6b 20 61 74 74 65 6d 70 74  rollback attempt
11674 20 69 73 20 6d 61 64 65 0a 2a 2a 20 61 6e 64 20   is made.** and 
11675 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  an error code is
11676 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74   returned.  If t
11677 68 65 20 63 6f 6d 6d 69 74 20 77 6f 72 6b 65 64  he commit worked
11678 2c 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69  , SQLITE_OK.** i
11679 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53  s returned..*/.S
1167a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1167b 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f  t sqlite3PagerCo
1167c 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 50 61 67  mmitPhaseTwo(Pag
1167d 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69  er *pPager){.  i
1167e 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1167f 4b 3b 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72  K;..  if( pPager
11680 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20  ->errCode ){.   
11681 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e   return pPager->
11682 65 72 72 43 6f 64 65 3b 0a 20 20 7d 0a 20 20 69  errCode;.  }.  i
11683 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  f( pPager->state
11684 3c 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20  <PAGER_RESERVED 
11685 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
11686 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
11687 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62    if( pPager->db
11688 4d 6f 64 69 66 69 65 64 3d 3d 30 20 26 26 0a 20  Modified==0 &&. 
11689 20 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e         (pPager->
1168a 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47  journalMode!=PAG
1168b 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44  ER_JOURNALMODE_D
1168c 45 4c 45 54 45 20 7c 7c 0a 20 20 20 20 20 20 20  ELETE ||.       
1168d 20 20 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75     pPager->exclu
1168e 73 69 76 65 4d 6f 64 65 21 3d 30 29 20 29 7b 0a  siveMode!=0) ){.
1168f 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
11690 65 72 2d 3e 64 69 72 74 79 43 61 63 68 65 3d 3d  er->dirtyCache==
11691 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75  0 || pPager->jou
11692 72 6e 61 6c 4f 70 65 6e 3d 3d 30 20 29 3b 0a 20  rnalOpen==0 );. 
11693 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
11694 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 50 41 47 45 52  _OK;.  }.  PAGER
11695 54 52 41 43 45 32 28 22 43 4f 4d 4d 49 54 20 25  TRACE2("COMMIT %
11696 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50  d\n", PAGERID(pP
11697 61 67 65 72 29 29 3b 0a 20 20 61 73 73 65 72 74  ager));.  assert
11698 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d  ( pPager->state=
11699 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 20 7c 7c  =PAGER_SYNCED ||
1169a 20 4d 45 4d 44 42 20 7c 7c 20 21 70 50 61 67 65   MEMDB || !pPage
1169b 72 2d 3e 64 69 72 74 79 43 61 63 68 65 20 29 3b  r->dirtyCache );
1169c 0a 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e  .  rc = pager_en
1169d 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50  d_transaction(pP
1169e 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 65  ager, pPager->se
1169f 74 4d 61 73 74 65 72 29 3b 0a 20 20 72 63 20 3d  tMaster);.  rc =
116a0 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61   pager_error(pPa
116a1 67 65 72 2c 20 72 63 29 3b 0a 20 20 72 65 74 75  ger, rc);.  retu
116a2 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
116a3 52 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 63 68 61  Rollback all cha
116a4 6e 67 65 73 2e 20 20 54 68 65 20 64 61 74 61 62  nges.  The datab
116a5 61 73 65 20 66 61 6c 6c 73 20 62 61 63 6b 20 74  ase falls back t
116a6 6f 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 6d  o PAGER_SHARED m
116a7 6f 64 65 2e 0a 2a 2a 20 41 6c 6c 20 69 6e 2d 6d  ode..** All in-m
116a8 65 6d 6f 72 79 20 63 61 63 68 65 20 70 61 67 65  emory cache page
116a9 73 20 72 65 76 65 72 74 20 74 6f 20 74 68 65 69  s revert to thei
116aa 72 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 20  r original data 
116ab 63 6f 6e 74 65 6e 74 73 2e 0a 2a 2a 20 54 68 65  contents..** The
116ac 20 6a 6f 75 72 6e 61 6c 20 69 73 20 64 65 6c 65   journal is dele
116ad 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ted..**.** This 
116ae 72 6f 75 74 69 6e 65 20 63 61 6e 6e 6f 74 20 66  routine cannot f
116af 61 69 6c 20 75 6e 6c 65 73 73 20 73 6f 6d 65 20  ail unless some 
116b0 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 69 73  other process is
116b1 20 6e 6f 74 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a   not following.*
116b2 2a 20 74 68 65 20 63 6f 72 72 65 63 74 20 6c 6f  * the correct lo
116b3 63 6b 69 6e 67 20 70 72 6f 74 6f 63 6f 6c 20 6f  cking protocol o
116b4 72 20 75 6e 6c 65 73 73 20 73 6f 6d 65 20 6f 74  r unless some ot
116b5 68 65 72 0a 2a 2a 20 70 72 6f 63 65 73 73 20 69  her.** process i
116b6 73 20 77 72 69 74 69 6e 67 20 74 72 61 73 68 20  s writing trash 
116b7 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  into the journal
116b8 20 66 69 6c 65 20 28 53 51 4c 49 54 45 5f 43 4f   file (SQLITE_CO
116b9 52 52 55 50 54 29 20 6f 72 0a 2a 2a 20 75 6e 6c  RRUPT) or.** unl
116ba 65 73 73 20 61 20 70 72 69 6f 72 20 6d 61 6c 6c  ess a prior mall
116bb 6f 63 28 29 20 66 61 69 6c 65 64 20 28 53 51 4c  oc() failed (SQL
116bc 49 54 45 5f 4e 4f 4d 45 4d 29 2e 20 20 41 70 70  ITE_NOMEM).  App
116bd 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 0a 2a  ropriate error.*
116be 2a 20 63 6f 64 65 73 20 61 72 65 20 72 65 74 75  * codes are retu
116bf 72 6e 65 64 20 66 6f 72 20 61 6c 6c 20 74 68 65  rned for all the
116c0 73 65 20 6f 63 63 61 73 69 6f 6e 73 2e 20 20 4f  se occasions.  O
116c1 74 68 65 72 77 69 73 65 2c 0a 2a 2a 20 53 51 4c  therwise,.** SQL
116c2 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
116c3 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
116c4 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
116c5 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 50  3PagerRollback(P
116c6 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
116c7 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
116c8 5f 4f 4b 3b 0a 20 20 50 41 47 45 52 54 52 41 43  _OK;.  PAGERTRAC
116c9 45 32 28 22 52 4f 4c 4c 42 41 43 4b 20 25 64 5c  E2("ROLLBACK %d\
116ca 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67  n", PAGERID(pPag
116cb 65 72 29 29 3b 0a 20 20 69 66 28 20 21 70 50 61  er));.  if( !pPa
116cc 67 65 72 2d 3e 64 69 72 74 79 43 61 63 68 65 20  ger->dirtyCache 
116cd 7c 7c 20 21 70 50 61 67 65 72 2d 3e 6a 6f 75 72  || !pPager->jour
116ce 6e 61 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20 72  nalOpen ){.    r
116cf 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72  c = pager_end_tr
116d0 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72  ansaction(pPager
116d1 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73  , pPager->setMas
116d2 74 65 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ter);.  }else if
116d3 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  ( pPager->errCod
116d4 65 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72 72  e && pPager->err
116d5 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f 46 55 4c  Code!=SQLITE_FUL
116d6 4c 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61  L ){.    if( pPa
116d7 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45  ger->state>=PAGE
116d8 52 5f 45 58 43 4c 55 53 49 56 45 20 29 7b 0a 20  R_EXCLUSIVE ){. 
116d9 20 20 20 20 20 70 61 67 65 72 5f 70 6c 61 79 62       pager_playb
116da 61 63 6b 28 70 50 61 67 65 72 2c 20 30 29 3b 0a  ack(pPager, 0);.
116db 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 70      }.    rc = p
116dc 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a  Pager->errCode;.
116dd 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28    }else{.    if(
116de 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d   pPager->state==
116df 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29  PAGER_RESERVED )
116e0 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32 3b  {.      int rc2;
116e1 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
116e2 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65  r_playback(pPage
116e3 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 63 32  r, 0);.      rc2
116e4 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61   = pager_end_tra
116e5 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c  nsaction(pPager,
116e6 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74   pPager->setMast
116e7 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  er);.      if( r
116e8 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
116e9 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32          rc = rc2
116ea 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
116eb 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
116ec 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70  pager_playback(p
116ed 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 7d  Pager, 0);.    }
116ee 0a 0a 20 20 20 20 69 66 28 20 21 4d 45 4d 44 42  ..    if( !MEMDB
116ef 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72   ){.      pPager
116f0 2d 3e 64 62 53 69 7a 65 20 3d 20 2d 31 3b 0a 20  ->dbSize = -1;. 
116f1 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
116f2 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  an error occurs 
116f3 64 75 72 69 6e 67 20 61 20 52 4f 4c 4c 42 41 43  during a ROLLBAC
116f4 4b 2c 20 77 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e  K, we can no lon
116f5 67 65 72 20 74 72 75 73 74 20 74 68 65 20 70 61  ger trust the pa
116f6 67 65 72 0a 20 20 20 20 2a 2a 20 63 61 63 68 65  ger.    ** cache
116f7 2e 20 53 6f 20 63 61 6c 6c 20 70 61 67 65 72 5f  . So call pager_
116f8 65 72 72 6f 72 28 29 20 6f 6e 20 74 68 65 20 77  error() on the w
116f9 61 79 20 6f 75 74 20 74 6f 20 6d 61 6b 65 20 61  ay out to make a
116fa 6e 79 20 65 72 72 6f 72 20 0a 20 20 20 20 2a 2a  ny error .    **
116fb 20 70 65 72 73 69 73 74 65 6e 74 2e 0a 20 20 20   persistent..   
116fc 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61 67   */.    rc = pag
116fd 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c  er_error(pPager,
116fe 20 72 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75   rc);.  }.  retu
116ff 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
11700 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74  Return TRUE if t
11701 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
11702 20 69 73 20 6f 70 65 6e 65 64 20 72 65 61 64 2d   is opened read-
11703 6f 6e 6c 79 2e 20 20 52 65 74 75 72 6e 20 46 41  only.  Return FA
11704 4c 53 45 0a 2a 2a 20 69 66 20 74 68 65 20 64 61  LSE.** if the da
11705 74 61 62 61 73 65 20 69 73 20 28 69 6e 20 74 68  tabase is (in th
11706 65 6f 72 79 29 20 77 72 69 74 61 62 6c 65 2e 0a  eory) writable..
11707 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
11708 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
11709 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 50 61 67  erIsreadonly(Pag
1170a 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72  er *pPager){.  r
1170b 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 72 65  eturn pPager->re
1170c 61 64 4f 6e 6c 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  adOnly;.}../*.**
1170d 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
1170e 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73  er of references
1170f 20 74 6f 20 74 68 65 20 70 61 67 65 72 2e 0a 2a   to the pager..*
11710 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
11711 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
11712 72 52 65 66 63 6f 75 6e 74 28 50 61 67 65 72 20  rRefcount(Pager 
11713 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75  *pPager){.  retu
11714 72 6e 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  rn sqlite3Pcache
11715 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d  RefCount(pPager-
11716 3e 70 50 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f 2a  >pPCache);.}../*
11717 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
11718 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e  umber of referen
11719 63 65 73 20 74 6f 20 74 68 65 20 73 70 65 63 69  ces to the speci
1171a 66 69 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51  fied page..*/.SQ
1171b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1171c 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
1171d 65 52 65 66 63 6f 75 6e 74 28 44 62 50 61 67 65  eRefcount(DbPage
1171e 20 2a 70 50 61 67 65 29 7b 0a 20 20 72 65 74 75   *pPage){.  retu
1171f 72 6e 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  rn sqlite3Pcache
11720 50 61 67 65 52 65 66 63 6f 75 6e 74 28 70 50 61  PageRefcount(pPa
11721 67 65 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53  ge);.}..#ifdef S
11722 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a  QLITE_TEST./*.**
11723 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
11724 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   used for testin
11725 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f  g and analysis o
11726 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  nly..*/.SQLITE_P
11727 52 49 56 41 54 45 20 69 6e 74 20 2a 73 71 6c 69  RIVATE int *sqli
11728 74 65 33 50 61 67 65 72 53 74 61 74 73 28 50 61  te3PagerStats(Pa
11729 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
1172a 73 74 61 74 69 63 20 69 6e 74 20 61 5b 31 31 5d  static int a[11]
1172b 3b 0a 20 20 61 5b 30 5d 20 3d 20 73 71 6c 69 74  ;.  a[0] = sqlit
1172c 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74  e3PcacheRefCount
1172d 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
1172e 29 3b 0a 20 20 61 5b 31 5d 20 3d 20 73 71 6c 69  );.  a[1] = sqli
1172f 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75  te3PcachePagecou
11730 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  nt(pPager->pPCac
11731 68 65 29 3b 0a 20 20 61 5b 32 5d 20 3d 20 73 71  he);.  a[2] = sq
11732 6c 69 74 65 33 50 63 61 63 68 65 47 65 74 43 61  lite3PcacheGetCa
11733 63 68 65 73 69 7a 65 28 70 50 61 67 65 72 2d 3e  chesize(pPager->
11734 70 50 43 61 63 68 65 29 3b 0a 20 20 61 5b 33 5d  pPCache);.  a[3]
11735 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a   = pPager->dbSiz
11736 65 3b 0a 20 20 61 5b 34 5d 20 3d 20 70 50 61 67  e;.  a[4] = pPag
11737 65 72 2d 3e 73 74 61 74 65 3b 0a 20 20 61 5b 35  er->state;.  a[5
11738 5d 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43  ] = pPager->errC
11739 6f 64 65 3b 0a 20 20 61 5b 36 5d 20 3d 20 70 50  ode;.  a[6] = pP
1173a 61 67 65 72 2d 3e 6e 48 69 74 3b 0a 20 20 61 5b  ager->nHit;.  a[
1173b 37 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 4d 69  7] = pPager->nMi
1173c 73 73 3b 0a 20 20 61 5b 38 5d 20 3d 20 30 3b 20  ss;.  a[8] = 0; 
1173d 20 2f 2a 20 55 73 65 64 20 74 6f 20 62 65 20 70   /* Used to be p
1173e 50 61 67 65 72 2d 3e 6e 4f 76 66 6c 20 2a 2f 0a  Pager->nOvfl */.
1173f 20 20 61 5b 39 5d 20 3d 20 70 50 61 67 65 72 2d    a[9] = pPager-
11740 3e 6e 52 65 61 64 3b 0a 20 20 61 5b 31 30 5d 20  >nRead;.  a[10] 
11741 3d 20 70 50 61 67 65 72 2d 3e 6e 57 72 69 74 65  = pPager->nWrite
11742 3b 0a 20 20 72 65 74 75 72 6e 20 61 3b 0a 7d 0a  ;.  return a;.}.
11743 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
11744 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  nt sqlite3PagerI
11745 73 4d 65 6d 64 62 28 50 61 67 65 72 20 2a 70 50  sMemdb(Pager *pP
11746 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  ager){.  return 
11747 4d 45 4d 44 42 3b 0a 7d 0a 23 65 6e 64 69 66 0a  MEMDB;.}.#endif.
11748 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 73  ./*.** Set the s
11749 74 61 74 65 6d 65 6e 74 20 72 6f 6c 6c 62 61 63  tatement rollbac
1174a 6b 20 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  k point..**.** T
1174b 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75  his routine shou
1174c 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74  ld be called wit
1174d 68 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  h the transactio
1174e 6e 20 6a 6f 75 72 6e 61 6c 20 61 6c 72 65 61 64  n journal alread
1174f 79 0a 2a 2a 20 6f 70 65 6e 2e 20 20 41 20 6e 65  y.** open.  A ne
11750 77 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72  w statement jour
11751 6e 61 6c 20 69 73 20 63 72 65 61 74 65 64 20 74  nal is created t
11752 68 61 74 20 63 61 6e 20 62 65 20 75 73 65 64 20  hat can be used 
11753 74 6f 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 63  to rollback.** c
11754 68 61 6e 67 65 73 20 6f 66 20 61 20 73 69 6e 67  hanges of a sing
11755 6c 65 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 77  le SQL command w
11756 69 74 68 69 6e 20 61 20 6c 61 72 67 65 72 20 74  ithin a larger t
11757 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  ransaction..*/.s
11758 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 53  tatic int pagerS
11759 74 6d 74 42 65 67 69 6e 28 50 61 67 65 72 20 2a  tmtBegin(Pager *
1175a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72  pPager){.  int r
1175b 63 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 50  c;.  assert( !pP
1175c 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20  ager->stmtInUse 
1175d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
1175e 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45  ger->state>=PAGE
1175f 52 5f 53 48 41 52 45 44 20 29 3b 0a 20 20 61 73  R_SHARED );.  as
11760 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62  sert( pPager->db
11761 53 69 7a 65 3e 3d 30 20 29 3b 0a 20 20 50 41 47  Size>=0 );.  PAG
11762 45 52 54 52 41 43 45 32 28 22 53 54 4d 54 2d 42  ERTRACE2("STMT-B
11763 45 47 49 4e 20 25 64 5c 6e 22 2c 20 50 41 47 45  EGIN %d\n", PAGE
11764 52 49 44 28 70 50 61 67 65 72 29 29 3b 0a 20 20  RID(pPager));.  
11765 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6a 6f 75  if( !pPager->jou
11766 72 6e 61 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20  rnalOpen ){.    
11767 70 50 61 67 65 72 2d 3e 73 74 6d 74 41 75 74 6f  pPager->stmtAuto
11768 6f 70 65 6e 20 3d 20 31 3b 0a 20 20 20 20 72 65  open = 1;.    re
11769 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1176a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 50    }.  assert( pP
1176b 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65  ager->journalOpe
1176c 6e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  n );.  assert( p
1176d 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74 3d 3d  Pager->pInStmt==
1176e 30 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70  0 );.  pPager->p
1176f 49 6e 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33  InStmt = sqlite3
11770 42 69 74 76 65 63 43 72 65 61 74 65 28 70 50 61  BitvecCreate(pPa
11771 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20  ger->dbSize);.  
11772 69 66 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 53  if( pPager->pInS
11773 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  tmt==0 ){.    /*
11774 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70   sqlite3OsLock(p
11775 50 61 67 65 72 2d 3e 66 64 2c 20 53 48 41 52 45  Pager->fd, SHARE
11776 44 5f 4c 4f 43 4b 29 3b 20 2a 2f 0a 20 20 20 20  D_LOCK); */.    
11777 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
11778 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65  MEM;.  }.  pPage
11779 72 2d 3e 73 74 6d 74 4a 53 69 7a 65 20 3d 20 70  r->stmtJSize = p
1177a 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
1177b 66 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 74 6d  f;.  pPager->stm
1177c 74 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e  tSize = pPager->
1177d 64 62 53 69 7a 65 3b 0a 20 20 70 50 61 67 65 72  dbSize;.  pPager
1177e 2d 3e 73 74 6d 74 48 64 72 4f 66 66 20 3d 20 30  ->stmtHdrOff = 0
1177f 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74  ;.  pPager->stmt
11780 43 6b 73 75 6d 20 3d 20 70 50 61 67 65 72 2d 3e  Cksum = pPager->
11781 63 6b 73 75 6d 49 6e 69 74 3b 0a 20 20 69 66 28  cksumInit;.  if(
11782 20 21 70 50 61 67 65 72 2d 3e 73 74 6d 74 4f 70   !pPager->stmtOp
11783 65 6e 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50  en ){.    if( pP
11784 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
11785 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
11786 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20  MODE_MEMORY ){. 
11787 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a       sqlite3MemJ
11788 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65  ournalOpen(pPage
11789 72 2d 3e 73 74 66 64 29 3b 0a 20 20 20 20 7d 65  r->stfd);.    }e
1178a 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
1178b 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e  sqlite3PagerOpen
1178c 74 65 6d 70 28 70 50 61 67 65 72 2c 20 70 50 61  temp(pPager, pPa
1178d 67 65 72 2d 3e 73 74 66 64 2c 20 53 51 4c 49 54  ger->stfd, SQLIT
1178e 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41  E_OPEN_SUBJOURNA
1178f 4c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  L);.      if( rc
11790 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   ){.        goto
11791 20 73 74 6d 74 5f 62 65 67 69 6e 5f 66 61 69 6c   stmt_begin_fail
11792 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ed;.      }.    
11793 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74  }.    pPager->st
11794 6d 74 4f 70 65 6e 20 3d 20 31 3b 0a 20 20 20 20  mtOpen = 1;.    
11795 70 50 61 67 65 72 2d 3e 73 74 6d 74 4e 52 65 63  pPager->stmtNRec
11796 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 50 61 67   = 0;.  }.  pPag
11797 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 3d 20  er->stmtInUse = 
11798 31 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  1;.  return SQLI
11799 54 45 5f 4f 4b 3b 0a 20 0a 73 74 6d 74 5f 62 65  TE_OK;. .stmt_be
1179a 67 69 6e 5f 66 61 69 6c 65 64 3a 0a 20 20 69 66  gin_failed:.  if
1179b 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d  ( pPager->pInStm
1179c 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
1179d 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50  BitvecDestroy(pP
1179e 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74 29 3b 0a  ager->pInStmt);.
1179f 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 53      pPager->pInS
117a0 74 6d 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72  tmt = 0;.  }.  r
117a1 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49  eturn rc;.}.SQLI
117a2 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
117a3 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d 74 42  qlite3PagerStmtB
117a4 65 67 69 6e 28 50 61 67 65 72 20 2a 70 50 61 67  egin(Pager *pPag
117a5 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  er){.  int rc;. 
117a6 20 72 63 20 3d 20 70 61 67 65 72 53 74 6d 74 42   rc = pagerStmtB
117a7 65 67 69 6e 28 70 50 61 67 65 72 29 3b 0a 20 20  egin(pPager);.  
117a8 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
117a9 0a 2a 2a 20 43 6f 6d 6d 69 74 20 61 20 73 74 61  .** Commit a sta
117aa 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  tement..*/.SQLIT
117ab 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
117ac 6c 69 74 65 33 50 61 67 65 72 53 74 6d 74 43 6f  lite3PagerStmtCo
117ad 6d 6d 69 74 28 50 61 67 65 72 20 2a 70 50 61 67  mmit(Pager *pPag
117ae 65 72 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65  er){.  if( pPage
117af 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 29 7b 0a  r->stmtInUse ){.
117b0 20 20 20 20 50 41 47 45 52 54 52 41 43 45 32 28      PAGERTRACE2(
117b1 22 53 54 4d 54 2d 43 4f 4d 4d 49 54 20 25 64 5c  "STMT-COMMIT %d\
117b2 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67  n", PAGERID(pPag
117b3 65 72 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  er));.    sqlite
117b4 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70  3BitvecDestroy(p
117b5 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74 29 3b  Pager->pInStmt);
117b6 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49 6e  .    pPager->pIn
117b7 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 70 50  Stmt = 0;.    pP
117b8 61 67 65 72 2d 3e 73 74 6d 74 4e 52 65 63 20 3d  ager->stmtNRec =
117b9 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   0;.    pPager->
117ba 73 74 6d 74 49 6e 55 73 65 20 3d 20 30 3b 0a 20  stmtInUse = 0;. 
117bb 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73     if( sqlite3Is
117bc 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65  MemJournal(pPage
117bd 72 2d 3e 73 74 66 64 29 20 29 7b 0a 20 20 20 20  r->stfd) ){.    
117be 20 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63    sqlite3OsTrunc
117bf 61 74 65 28 70 50 61 67 65 72 2d 3e 73 74 66 64  ate(pPager->stfd
117c0 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  , 0);.    }.  }.
117c1 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 41 75    pPager->stmtAu
117c2 74 6f 6f 70 65 6e 20 3d 20 30 3b 0a 20 20 72 65  toopen = 0;.  re
117c3 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
117c4 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63  }../*.** Rollbac
117c5 6b 20 61 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  k a statement..*
117c6 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
117c7 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
117c8 72 53 74 6d 74 52 6f 6c 6c 62 61 63 6b 28 50 61  rStmtRollback(Pa
117c9 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
117ca 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 50  int rc;.  if( pP
117cb 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20  ager->stmtInUse 
117cc 29 7b 0a 20 20 20 20 50 41 47 45 52 54 52 41 43  ){.    PAGERTRAC
117cd 45 32 28 22 53 54 4d 54 2d 52 4f 4c 4c 42 41 43  E2("STMT-ROLLBAC
117ce 4b 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44  K %d\n", PAGERID
117cf 28 70 50 61 67 65 72 29 29 3b 0a 20 20 20 20 72  (pPager));.    r
117d0 63 20 3d 20 70 61 67 65 72 5f 73 74 6d 74 5f 70  c = pager_stmt_p
117d1 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72 29 3b  layback(pPager);
117d2 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65  .    sqlite3Page
117d3 72 53 74 6d 74 43 6f 6d 6d 69 74 28 70 50 61 67  rStmtCommit(pPag
117d4 65 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  er);.  }else{.  
117d5 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
117d6 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e  ;.  }.  pPager->
117d7 73 74 6d 74 41 75 74 6f 6f 70 65 6e 20 3d 20 30  stmtAutoopen = 0
117d8 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
117d9 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
117da 68 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65  he full pathname
117db 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
117dc 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   file..*/.SQLITE
117dd 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
117de 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65  har *sqlite3Page
117df 72 46 69 6c 65 6e 61 6d 65 28 50 61 67 65 72 20  rFilename(Pager 
117e0 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75  *pPager){.  retu
117e1 72 6e 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65  rn pPager->zFile
117e2 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  name;.}../*.** R
117e3 65 74 75 72 6e 20 74 68 65 20 56 46 53 20 73 74  eturn the VFS st
117e4 72 75 63 74 75 72 65 20 66 6f 72 20 74 68 65 20  ructure for the 
117e5 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  pager..*/.SQLITE
117e6 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73  _PRIVATE const s
117e7 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69  qlite3_vfs *sqli
117e8 74 65 33 50 61 67 65 72 56 66 73 28 50 61 67 65  te3PagerVfs(Page
117e9 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65  r *pPager){.  re
117ea 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 70 56 66  turn pPager->pVf
117eb 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  s;.}../*.** Retu
117ec 72 6e 20 74 68 65 20 66 69 6c 65 20 68 61 6e 64  rn the file hand
117ed 6c 65 20 66 6f 72 20 74 68 65 20 64 61 74 61 62  le for the datab
117ee 61 73 65 20 66 69 6c 65 20 61 73 73 6f 63 69 61  ase file associa
117ef 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20  ted.** with the 
117f0 70 61 67 65 72 2e 20 20 54 68 69 73 20 6d 69 67  pager.  This mig
117f1 68 74 20 72 65 74 75 72 6e 20 4e 55 4c 4c 20 69  ht return NULL i
117f2 66 20 74 68 65 20 66 69 6c 65 20 68 61 73 0a 2a  f the file has.*
117f3 2a 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 6f  * not yet been o
117f4 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  pened..*/.SQLITE
117f5 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33  _PRIVATE sqlite3
117f6 5f 66 69 6c 65 20 2a 73 71 6c 69 74 65 33 50 61  _file *sqlite3Pa
117f7 67 65 72 46 69 6c 65 28 50 61 67 65 72 20 2a 70  gerFile(Pager *p
117f8 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e  Pager){.  return
117f9 20 70 50 61 67 65 72 2d 3e 66 64 3b 0a 7d 0a 0a   pPager->fd;.}..
117fa 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
117fb 20 64 69 72 65 63 74 6f 72 79 20 6f 66 20 74 68   directory of th
117fc 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
117fd 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
117fe 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  TE const char *s
117ff 71 6c 69 74 65 33 50 61 67 65 72 44 69 72 6e 61  qlite3PagerDirna
11800 6d 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  me(Pager *pPager
11801 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67  ){.  return pPag
11802 65 72 2d 3e 7a 44 69 72 65 63 74 6f 72 79 3b 0a  er->zDirectory;.
11803 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
11804 74 68 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d  the full pathnam
11805 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e of the journal
11806 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   file..*/.SQLITE
11807 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
11808 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65  har *sqlite3Page
11809 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 50 61 67  rJournalname(Pag
1180a 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72  er *pPager){.  r
1180b 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 7a 4a  eturn pPager->zJ
1180c 6f 75 72 6e 61 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ournal;.}../*.**
1180d 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
1180e 66 73 79 6e 63 28 29 20 63 61 6c 6c 73 20 61 72  fsync() calls ar
1180f 65 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 74  e disabled for t
11810 68 69 73 20 70 61 67 65 72 2e 20 20 52 65 74 75  his pager.  Retu
11811 72 6e 20 46 41 4c 53 45 0a 2a 2a 20 69 66 20 66  rn FALSE.** if f
11812 73 79 6e 63 28 29 73 20 61 72 65 20 65 78 65 63  sync()s are exec
11813 75 74 65 64 20 6e 6f 72 6d 61 6c 6c 79 2e 0a 2a  uted normally..*
11814 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
11815 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
11816 72 4e 6f 73 79 6e 63 28 50 61 67 65 72 20 2a 70  rNosync(Pager *p
11817 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e  Pager){.  return
11818 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3b   pPager->noSync;
11819 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .}..#ifdef SQLIT
1181a 45 5f 48 41 53 5f 43 4f 44 45 43 0a 2f 2a 0a 2a  E_HAS_CODEC./*.*
1181b 2a 20 53 65 74 20 74 68 65 20 63 6f 64 65 63 20  * Set the codec 
1181c 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 0a 2a  for this pager.*
1181d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1181e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67   void sqlite3Pag
1181f 65 72 53 65 74 43 6f 64 65 63 28 0a 20 20 50 61  erSetCodec(.  Pa
11820 67 65 72 20 2a 70 50 61 67 65 72 2c 0a 20 20 76  ger *pPager,.  v
11821 6f 69 64 20 2a 28 2a 78 43 6f 64 65 63 29 28 76  oid *(*xCodec)(v
11822 6f 69 64 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c  oid*,void*,Pgno,
11823 69 6e 74 29 2c 0a 20 20 76 6f 69 64 20 2a 70 43  int),.  void *pC
11824 6f 64 65 63 41 72 67 0a 29 7b 0a 20 20 70 50 61  odecArg.){.  pPa
11825 67 65 72 2d 3e 78 43 6f 64 65 63 20 3d 20 78 43  ger->xCodec = xC
11826 6f 64 65 63 3b 0a 20 20 70 50 61 67 65 72 2d 3e  odec;.  pPager->
11827 70 43 6f 64 65 63 41 72 67 20 3d 20 70 43 6f 64  pCodecArg = pCod
11828 65 63 41 72 67 3b 0a 7d 0a 23 65 6e 64 69 66 0a  ecArg;.}.#endif.
11829 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1182a 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
1182b 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 70  /*.** Move the p
1182c 61 67 65 20 70 50 67 20 74 6f 20 6c 6f 63 61 74  age pPg to locat
1182d 69 6f 6e 20 70 67 6e 6f 20 69 6e 20 74 68 65 20  ion pgno in the 
1182e 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  file..**.** Ther
1182f 65 20 6d 75 73 74 20 62 65 20 6e 6f 20 72 65 66  e must be no ref
11830 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70  erences to the p
11831 61 67 65 20 70 72 65 76 69 6f 75 73 6c 79 20 6c  age previously l
11832 6f 63 61 74 65 64 20 61 74 0a 2a 2a 20 70 67 6e  ocated at.** pgn
11833 6f 20 28 77 68 69 63 68 20 77 65 20 63 61 6c 6c  o (which we call
11834 20 70 50 67 4f 6c 64 29 20 74 68 6f 75 67 68 20   pPgOld) though 
11835 74 68 61 74 20 70 61 67 65 20 69 73 20 61 6c 6c  that page is all
11836 6f 77 65 64 20 74 6f 20 62 65 0a 2a 2a 20 69 6e  owed to be.** in
11837 20 63 61 63 68 65 2e 20 20 49 66 20 74 68 65 20   cache.  If the 
11838 70 61 67 65 20 70 72 65 76 69 6f 75 73 6c 79 20  page previously 
11839 6c 6f 63 61 74 65 64 20 61 74 20 70 67 6e 6f 20  located at pgno 
1183a 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 0a 2a  is not already.*
1183b 2a 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63  * in the rollbac
1183c 6b 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 69 73  k journal, it is
1183d 20 6e 6f 74 20 70 75 74 20 74 68 65 72 65 20 62   not put there b
1183e 79 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e  y by this routin
1183f 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72 65 6e  e..**.** Referen
11840 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 20  ces to the page 
11841 70 50 67 20 72 65 6d 61 69 6e 20 76 61 6c 69 64  pPg remain valid
11842 2e 20 55 70 64 61 74 69 6e 67 20 61 6e 79 0a 2a  . Updating any.*
11843 2a 20 6d 65 74 61 2d 64 61 74 61 20 61 73 73 6f  * meta-data asso
11844 63 69 61 74 65 64 20 77 69 74 68 20 70 50 67 20  ciated with pPg 
11845 28 69 2e 65 2e 20 64 61 74 61 20 73 74 6f 72 65  (i.e. data store
11846 64 20 69 6e 20 74 68 65 20 6e 45 78 74 72 61 20  d in the nExtra 
11847 62 79 74 65 73 0a 2a 2a 20 61 6c 6c 6f 63 61 74  bytes.** allocat
11848 65 64 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68  ed along with th
11849 65 20 70 61 67 65 29 20 69 73 20 74 68 65 20 72  e page) is the r
1184a 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66  esponsibility of
1184b 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a 0a   the caller..**.
1184c 2a 2a 20 41 20 74 72 61 6e 73 61 63 74 69 6f 6e  ** A transaction
1184d 20 6d 75 73 74 20 62 65 20 61 63 74 69 76 65 20   must be active 
1184e 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  when this routin
1184f 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 49 74 20  e is called. It 
11850 75 73 65 64 20 74 6f 20 62 65 0a 2a 2a 20 72 65  used to be.** re
11851 71 75 69 72 65 64 20 74 68 61 74 20 61 20 73 74  quired that a st
11852 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74  atement transact
11853 69 6f 6e 20 77 61 73 20 6e 6f 74 20 61 63 74 69  ion was not acti
11854 76 65 2c 20 62 75 74 20 74 68 69 73 20 72 65 73  ve, but this res
11855 74 72 69 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 20  triction.** has 
11856 62 65 65 6e 20 72 65 6d 6f 76 65 64 20 28 43 52  been removed (CR
11857 45 41 54 45 20 49 4e 44 45 58 20 6e 65 65 64 73  EATE INDEX needs
11858 20 74 6f 20 6d 6f 76 65 20 61 20 70 61 67 65 20   to move a page 
11859 77 68 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74  when a statement
1185a 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  .** transaction 
1185b 69 73 20 61 63 74 69 76 65 29 2e 0a 2a 2a 0a 2a  is active)..**.*
1185c 2a 20 49 66 20 74 68 65 20 66 6f 75 72 74 68 20  * If the fourth 
1185d 61 72 67 75 6d 65 6e 74 2c 20 69 73 43 6f 6d 6d  argument, isComm
1185e 69 74 2c 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  it, is non-zero,
1185f 20 74 68 65 6e 20 74 68 69 73 20 70 61 67 65 20   then this page 
11860 69 73 20 62 65 69 6e 67 0a 2a 2a 20 6d 6f 76 65  is being.** move
11861 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 20 64  d as part of a d
11862 61 74 61 62 61 73 65 20 72 65 6f 72 67 61 6e 69  atabase reorgani
11863 7a 61 74 69 6f 6e 20 6a 75 73 74 20 62 65 66 6f  zation just befo
11864 72 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  re the transacti
11865 6f 6e 20 0a 2a 2a 20 69 73 20 62 65 69 6e 67 20  on .** is being 
11866 63 6f 6d 6d 69 74 74 65 64 2e 20 49 6e 20 74 68  committed. In th
11867 69 73 20 63 61 73 65 2c 20 69 74 20 69 73 20 67  is case, it is g
11868 75 61 72 61 6e 74 65 65 64 20 74 68 61 74 20 74  uaranteed that t
11869 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65  he database page
1186a 20 0a 2a 2a 20 70 50 67 20 72 65 66 65 72 73 20   .** pPg refers 
1186b 74 6f 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 77  to will not be w
1186c 72 69 74 74 65 6e 20 74 6f 20 61 67 61 69 6e 20  ritten to again 
1186d 77 69 74 68 69 6e 20 74 68 69 73 20 74 72 61 6e  within this tran
1186e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  saction..*/.SQLI
1186f 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
11870 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70  qlite3PagerMovep
11871 61 67 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  age(Pager *pPage
11872 72 2c 20 44 62 50 61 67 65 20 2a 70 50 67 2c 20  r, DbPage *pPg, 
11873 50 67 6e 6f 20 70 67 6e 6f 2c 20 69 6e 74 20 69  Pgno pgno, int i
11874 73 43 6f 6d 6d 69 74 29 7b 0a 20 20 50 67 48 64  sCommit){.  PgHd
11875 72 20 2a 70 50 67 4f 6c 64 3b 20 20 2f 2a 20 54  r *pPgOld;  /* T
11876 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 6f 76  he page being ov
11877 65 72 77 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20  erwritten. */.  
11878 50 67 6e 6f 20 6e 65 65 64 53 79 6e 63 50 67 6e  Pgno needSyncPgn
11879 6f 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74  o = 0;..  assert
1187a 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30 20 29 3b  ( pPg->nRef>0 );
1187b 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 35 28  ..  PAGERTRACE5(
1187c 22 4d 4f 56 45 20 25 64 20 70 61 67 65 20 25 64  "MOVE %d page %d
1187d 20 28 6e 65 65 64 53 79 6e 63 3d 25 64 29 20 6d   (needSync=%d) m
1187e 6f 76 65 73 20 74 6f 20 25 64 5c 6e 22 2c 20 0a  oves to %d\n", .
1187f 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50        PAGERID(pP
11880 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f  ager), pPg->pgno
11881 2c 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47  , (pPg->flags&PG
11882 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3f 31  HDR_NEED_SYNC)?1
11883 3a 30 2c 20 70 67 6e 6f 29 3b 0a 20 20 49 4f 54  :0, pgno);.  IOT
11884 52 41 43 45 28 28 22 4d 4f 56 45 20 25 70 20 25  RACE(("MOVE %p %
11885 64 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c  d %d\n", pPager,
11886 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 70 67 6e 6f   pPg->pgno, pgno
11887 29 29 0a 0a 20 20 70 61 67 65 72 5f 67 65 74 5f  ))..  pager_get_
11888 63 6f 6e 74 65 6e 74 28 70 50 67 29 3b 0a 0a 20  content(pPg);.. 
11889 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e   /* If the journ
1188a 61 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73  al needs to be s
1188b 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20 70  ync()ed before p
1188c 61 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 63 61  age pPg->pgno ca
1188d 6e 0a 20 20 2a 2a 20 62 65 20 77 72 69 74 74 65  n.  ** be writte
1188e 6e 20 74 6f 2c 20 73 74 6f 72 65 20 70 50 67 2d  n to, store pPg-
1188f 3e 70 67 6e 6f 20 69 6e 20 6c 6f 63 61 6c 20 76  >pgno in local v
11890 61 72 69 61 62 6c 65 20 6e 65 65 64 53 79 6e 63  ariable needSync
11891 50 67 6e 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  Pgno..  **.  ** 
11892 49 66 20 74 68 65 20 69 73 43 6f 6d 6d 69 74 20  If the isCommit 
11893 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65  flag is set, the
11894 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f  re is no need to
11895 20 72 65 6d 65 6d 62 65 72 20 74 68 61 74 0a 20   remember that. 
11896 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   ** the journal 
11897 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63  needs to be sync
11898 28 29 65 64 20 62 65 66 6f 72 65 20 64 61 74 61  ()ed before data
11899 62 61 73 65 20 70 61 67 65 20 70 50 67 2d 3e 70  base page pPg->p
1189a 67 6e 6f 20 0a 20 20 2a 2a 20 63 61 6e 20 62 65  gno .  ** can be
1189b 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 54 68 65   written to. The
1189c 20 63 61 6c 6c 65 72 20 68 61 73 20 61 6c 72 65   caller has alre
1189d 61 64 79 20 70 72 6f 6d 69 73 65 64 20 6e 6f 74  ady promised not
1189e 20 74 6f 20 77 72 69 74 65 20 74 6f 20 69 74 2e   to write to it.
1189f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 50 67  .  */.  if( (pPg
118a0 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45  ->flags&PGHDR_NE
118a1 45 44 5f 53 59 4e 43 29 20 26 26 20 21 69 73 43  ED_SYNC) && !isC
118a2 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 6e 65 65  ommit ){.    nee
118a3 64 53 79 6e 63 50 67 6e 6f 20 3d 20 70 50 67 2d  dSyncPgno = pPg-
118a4 3e 70 67 6e 6f 3b 0a 20 20 20 20 61 73 73 65 72  >pgno;.    asser
118a5 74 28 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 50  t( (pPg->flags&P
118a6 47 48 44 52 5f 49 4e 5f 4a 4f 55 52 4e 41 4c 29  GHDR_IN_JOURNAL)
118a7 20 7c 7c 20 28 69 6e 74 29 70 67 6e 6f 3e 70 50   || (int)pgno>pP
118a8 61 67 65 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65  ager->origDbSize
118a9 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
118aa 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52  pPg->flags&PGHDR
118ab 5f 44 49 52 54 59 20 29 3b 0a 20 20 20 20 61 73  _DIRTY );.    as
118ac 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 65  sert( pPager->ne
118ad 65 64 53 79 6e 63 20 29 3b 0a 20 20 7d 0a 0a 20  edSync );.  }.. 
118ae 20 2f 2a 20 49 66 20 74 68 65 20 63 61 63 68 65   /* If the cache
118af 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 61 67 65   contains a page
118b0 20 77 69 74 68 20 70 61 67 65 2d 6e 75 6d 62 65   with page-numbe
118b1 72 20 70 67 6e 6f 2c 20 72 65 6d 6f 76 65 20 69  r pgno, remove i
118b2 74 0a 20 20 2a 2a 20 66 72 6f 6d 20 69 74 73 20  t.  ** from its 
118b3 68 61 73 68 20 63 68 61 69 6e 2e 20 41 6c 73 6f  hash chain. Also
118b4 2c 20 69 66 20 74 68 65 20 50 67 48 64 72 2e 6e  , if the PgHdr.n
118b5 65 65 64 53 79 6e 63 20 77 61 73 20 73 65 74 20  eedSync was set 
118b6 66 6f 72 20 0a 20 20 2a 2a 20 70 61 67 65 20 70  for .  ** page p
118b7 67 6e 6f 20 62 65 66 6f 72 65 20 74 68 65 20 27  gno before the '
118b8 6d 6f 76 65 27 20 6f 70 65 72 61 74 69 6f 6e 2c  move' operation,
118b9 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20   it needs to be 
118ba 72 65 74 61 69 6e 65 64 20 0a 20 20 2a 2a 20 66  retained .  ** f
118bb 6f 72 20 74 68 65 20 70 61 67 65 20 6d 6f 76 65  or the page move
118bc 64 20 74 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20  d there..  */.  
118bd 70 50 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28  pPg->flags &= ~(
118be 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 7c  PGHDR_NEED_SYNC|
118bf 50 47 48 44 52 5f 49 4e 5f 4a 4f 55 52 4e 41 4c  PGHDR_IN_JOURNAL
118c0 29 3b 0a 20 20 70 50 67 4f 6c 64 20 3d 20 70 61  );.  pPgOld = pa
118c1 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65  ger_lookup(pPage
118c2 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 61 73 73 65  r, pgno);.  asse
118c3 72 74 28 20 21 70 50 67 4f 6c 64 20 7c 7c 20 70  rt( !pPgOld || p
118c4 50 67 4f 6c 64 2d 3e 6e 52 65 66 3d 3d 31 20 29  PgOld->nRef==1 )
118c5 3b 0a 20 20 69 66 28 20 70 50 67 4f 6c 64 20 29  ;.  if( pPgOld )
118c6 7b 0a 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73  {.    pPg->flags
118c7 20 7c 3d 20 28 70 50 67 4f 6c 64 2d 3e 66 6c 61   |= (pPgOld->fla
118c8 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  gs&PGHDR_NEED_SY
118c9 4e 43 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  NC);.  }.  if( s
118ca 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74  qlite3BitvecTest
118cb 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72  (pPager->pInJour
118cc 6e 61 6c 2c 20 70 67 6e 6f 29 20 29 7b 0a 20 20  nal, pgno) ){.  
118cd 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20    pPg->flags |= 
118ce 50 47 48 44 52 5f 49 4e 5f 4a 4f 55 52 4e 41 4c  PGHDR_IN_JOURNAL
118cf 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33  ;.  }..  sqlite3
118d0 50 63 61 63 68 65 4d 6f 76 65 28 70 50 67 2c 20  PcacheMove(pPg, 
118d1 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70 50 67  pgno);.  if( pPg
118d2 4f 6c 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Old ){.    sqlit
118d3 65 33 50 63 61 63 68 65 4d 6f 76 65 28 70 50 67  e3PcacheMove(pPg
118d4 4f 6c 64 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  Old, 0);.    sql
118d5 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73  ite3PcacheReleas
118d6 65 28 70 50 67 4f 6c 64 29 3b 0a 20 20 7d 0a 0a  e(pPgOld);.  }..
118d7 20 20 6d 61 6b 65 44 69 72 74 79 28 70 50 67 29    makeDirty(pPg)
118d8 3b 0a 20 20 70 50 61 67 65 72 2d 3e 64 69 72 74  ;.  pPager->dirt
118d9 79 43 61 63 68 65 20 3d 20 31 3b 0a 20 20 70 50  yCache = 1;.  pP
118da 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64  ager->dbModified
118db 20 3d 20 31 3b 0a 0a 20 20 69 66 28 20 6e 65 65   = 1;..  if( nee
118dc 64 53 79 6e 63 50 67 6e 6f 20 29 7b 0a 20 20 20  dSyncPgno ){.   
118dd 20 2f 2a 20 49 66 20 6e 65 65 64 53 79 6e 63 50   /* If needSyncP
118de 67 6e 6f 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  gno is non-zero,
118df 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61   then the journa
118e0 6c 20 66 69 6c 65 20 6e 65 65 64 73 20 74 6f 20  l file needs to 
118e1 62 65 20 0a 20 20 20 20 2a 2a 20 73 79 6e 63 28  be .    ** sync(
118e2 29 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 64  )ed before any d
118e3 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 74  ata is written t
118e4 6f 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  o database file 
118e5 70 61 67 65 20 6e 65 65 64 53 79 6e 63 50 67 6e  page needSyncPgn
118e6 6f 2e 0a 20 20 20 20 2a 2a 20 43 75 72 72 65 6e  o..    ** Curren
118e7 74 6c 79 2c 20 6e 6f 20 73 75 63 68 20 70 61 67  tly, no such pag
118e8 65 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20  e exists in the 
118e9 70 61 67 65 2d 63 61 63 68 65 20 61 6e 64 20 74  page-cache and t
118ea 68 65 20 0a 20 20 20 20 2a 2a 20 22 69 73 20 6a  he .    ** "is j
118eb 6f 75 72 6e 61 6c 65 64 22 20 62 69 74 76 65 63  ournaled" bitvec
118ec 20 66 6c 61 67 20 68 61 73 20 62 65 65 6e 20 73   flag has been s
118ed 65 74 2e 20 54 68 69 73 20 6e 65 65 64 73 20 74  et. This needs t
118ee 6f 20 62 65 20 72 65 6d 65 64 69 65 64 20 62 79  o be remedied by
118ef 0a 20 20 20 20 2a 2a 20 6c 6f 61 64 69 6e 67 20  .    ** loading 
118f0 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68  the page into th
118f1 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 61 6e  e pager-cache an
118f2 64 20 73 65 74 74 69 6e 67 20 74 68 65 20 50 67  d setting the Pg
118f3 48 64 72 2e 6e 65 65 64 53 79 6e 63 20 0a 20 20  Hdr.needSync .  
118f4 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 20 20 2a    ** flag..    *
118f5 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20  *.    ** If the 
118f6 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f 61 64 20  attempt to load 
118f7 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68  the page into th
118f8 65 20 70 61 67 65 2d 63 61 63 68 65 20 66 61 69  e page-cache fai
118f9 6c 73 2c 20 28 64 75 65 0a 20 20 20 20 2a 2a 20  ls, (due.    ** 
118fa 74 6f 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72  to a malloc() or
118fb 20 49 4f 20 66 61 69 6c 75 72 65 29 2c 20 63 6c   IO failure), cl
118fc 65 61 72 20 74 68 65 20 62 69 74 20 69 6e 20 74  ear the bit in t
118fd 68 65 20 70 49 6e 4a 6f 75 72 6e 61 6c 5b 5d 0a  he pInJournal[].
118fe 20 20 20 20 2a 2a 20 61 72 72 61 79 2e 20 4f 74      ** array. Ot
118ff 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 20  herwise, if the 
11900 70 61 67 65 20 69 73 20 6c 6f 61 64 65 64 20 61  page is loaded a
11901 6e 64 20 77 72 69 74 74 65 6e 20 61 67 61 69 6e  nd written again
11902 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 69 73 20   in.    ** this 
11903 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74 20  transaction, it 
11904 6d 61 79 20 62 65 20 77 72 69 74 74 65 6e 20 74  may be written t
11905 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
11906 69 6c 65 20 62 65 66 6f 72 65 0a 20 20 20 20 2a  ile before.    *
11907 2a 20 69 74 20 69 73 20 73 79 6e 63 65 64 20 69  * it is synced i
11908 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  nto the journal 
11909 66 69 6c 65 2e 20 54 68 69 73 20 77 61 79 2c 20  file. This way, 
1190a 69 74 20 6d 61 79 20 65 6e 64 20 75 70 20 69 6e  it may end up in
1190b 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72  .    ** the jour
1190c 6e 61 6c 20 66 69 6c 65 20 74 77 69 63 65 2c 20  nal file twice, 
1190d 62 75 74 20 74 68 61 74 20 69 73 20 6e 6f 74 20  but that is not 
1190e 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 20 20 2a  a problem..    *
1190f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 71 6c  *.    ** The sql
11910 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 63  ite3PagerGet() c
11911 61 6c 6c 20 6d 61 79 20 63 61 75 73 65 20 74 68  all may cause th
11912 65 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 73 79 6e  e journal to syn
11913 63 2e 20 53 6f 20 6d 61 6b 65 0a 20 20 20 20 2a  c. So make.    *
11914 2a 20 73 75 72 65 20 74 68 65 20 50 61 67 65 72  * sure the Pager
11915 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 20 69  .needSync flag i
11916 73 20 73 65 74 20 74 6f 6f 2e 0a 20 20 20 20 2a  s set too..    *
11917 2f 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20  /.    int rc;.  
11918 20 20 50 67 48 64 72 20 2a 70 50 67 48 64 72 3b    PgHdr *pPgHdr;
11919 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
1191a 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 29 3b  ger->needSync );
1191b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1191c 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72  3PagerGet(pPager
1191d 2c 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2c 20  , needSyncPgno, 
1191e 26 70 50 67 48 64 72 29 3b 0a 20 20 20 20 69 66  &pPgHdr);.    if
1191f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
11920 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61  ){.      if( pPa
11921 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20  ger->pInJournal 
11922 26 26 20 28 69 6e 74 29 6e 65 65 64 53 79 6e 63  && (int)needSync
11923 50 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 6f 72  Pgno<=pPager->or
11924 69 67 44 62 53 69 7a 65 20 29 7b 0a 20 20 20 20  igDbSize ){.    
11925 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65      sqlite3Bitve
11926 63 43 6c 65 61 72 28 70 50 61 67 65 72 2d 3e 70  cClear(pPager->p
11927 49 6e 4a 6f 75 72 6e 61 6c 2c 20 6e 65 65 64 53  InJournal, needS
11928 79 6e 63 50 67 6e 6f 29 3b 0a 20 20 20 20 20 20  yncPgno);.      
11929 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  }.      return r
1192a 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61  c;.    }.    pPa
1192b 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20  ger->needSync = 
1192c 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  1;.    assert( p
1192d 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d 30  Pager->noSync==0
1192e 20 26 26 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20   && !MEMDB );.  
1192f 20 20 70 50 67 48 64 72 2d 3e 66 6c 61 67 73 20    pPgHdr->flags 
11930 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  |= PGHDR_NEED_SY
11931 4e 43 3b 0a 20 20 20 20 70 50 67 48 64 72 2d 3e  NC;.    pPgHdr->
11932 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 49  flags |= PGHDR_I
11933 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 6d  N_JOURNAL;.    m
11934 61 6b 65 44 69 72 74 79 28 70 50 67 48 64 72 29  akeDirty(pPgHdr)
11935 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67  ;.    sqlite3Pag
11936 65 72 55 6e 72 65 66 28 70 50 67 48 64 72 29 3b  erUnref(pPgHdr);
11937 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53  .  }..  return S
11938 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64  QLITE_OK;.}.#end
11939 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  if../*.** Return
1193a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
1193b 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 73  e data for the s
1193c 70 65 63 69 66 69 65 64 20 70 61 67 65 2e 0a 2a  pecified page..*
1193d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1193e 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61   void *sqlite3Pa
1193f 67 65 72 47 65 74 44 61 74 61 28 44 62 50 61 67  gerGetData(DbPag
11940 65 20 2a 70 50 67 29 7b 0a 20 20 61 73 73 65 72  e *pPg){.  asser
11941 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30 20 7c  t( pPg->nRef>0 |
11942 7c 20 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e 6d  | pPg->pPager->m
11943 65 6d 44 62 20 29 3b 0a 20 20 72 65 74 75 72 6e  emDb );.  return
11944 20 70 50 67 2d 3e 70 44 61 74 61 3b 0a 7d 0a 0a   pPg->pData;.}..
11945 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70  /*.** Return a p
11946 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 50 61  ointer to the Pa
11947 67 65 72 2e 6e 45 78 74 72 61 20 62 79 74 65 73  ger.nExtra bytes
11948 20 6f 66 20 22 65 78 74 72 61 22 20 73 70 61 63   of "extra" spac
11949 65 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20  e .** allocated 
1194a 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 73  along with the s
1194b 70 65 63 69 66 69 65 64 20 70 61 67 65 2e 0a 2a  pecified page..*
1194c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1194d 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61   void *sqlite3Pa
1194e 67 65 72 47 65 74 45 78 74 72 61 28 44 62 50 61  gerGetExtra(DbPa
1194f 67 65 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65  ge *pPg){.  Page
11950 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d  r *pPager = pPg-
11951 3e 70 50 61 67 65 72 3b 0a 20 20 72 65 74 75 72  >pPager;.  retur
11952 6e 20 28 70 50 61 67 65 72 3f 70 50 67 2d 3e 70  n (pPager?pPg->p
11953 45 78 74 72 61 3a 30 29 3b 0a 7d 0a 0a 2f 2a 0a  Extra:0);.}../*.
11954 2a 2a 20 47 65 74 2f 73 65 74 20 74 68 65 20 6c  ** Get/set the l
11955 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 20  ocking-mode for 
11956 74 68 69 73 20 70 61 67 65 72 2e 20 50 61 72 61  this pager. Para
11957 6d 65 74 65 72 20 65 4d 6f 64 65 20 6d 75 73 74  meter eMode must
11958 20 62 65 20 6f 6e 65 0a 2a 2a 20 6f 66 20 50 41   be one.** of PA
11959 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
1195a 51 55 45 52 59 2c 20 50 41 47 45 52 5f 4c 4f 43  QUERY, PAGER_LOC
1195b 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20  KINGMODE_NORMAL 
1195c 6f 72 20 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43  or .** PAGER_LOC
1195d 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49  KINGMODE_EXCLUSI
1195e 56 45 2e 20 49 66 20 74 68 65 20 70 61 72 61 6d  VE. If the param
1195f 65 74 65 72 20 69 73 20 6e 6f 74 20 5f 51 55 45  eter is not _QUE
11960 52 59 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20  RY, then.** the 
11961 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 69 73 20  locking-mode is 
11962 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65  set to the value
11963 20 73 70 65 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a   specified..**.*
11964 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76  * The returned v
11965 61 6c 75 65 20 69 73 20 65 69 74 68 65 72 20 50  alue is either P
11966 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
11967 5f 4e 4f 52 4d 41 4c 20 6f 72 0a 2a 2a 20 50 41  _NORMAL or.** PA
11968 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
11969 45 58 43 4c 55 53 49 56 45 2c 20 69 6e 64 69 63  EXCLUSIVE, indic
1196a 61 74 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e  ating the curren
1196b 74 20 28 70 6f 73 73 69 62 6c 79 20 75 70 64 61  t (possibly upda
1196c 74 65 64 29 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 2d  ted).** locking-
1196d 6d 6f 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  mode..*/.SQLITE_
1196e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1196f 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d  te3PagerLockingM
11970 6f 64 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  ode(Pager *pPage
11971 72 2c 20 69 6e 74 20 65 4d 6f 64 65 29 7b 0a 20  r, int eMode){. 
11972 20 61 73 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d   assert( eMode==
11973 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44  PAGER_LOCKINGMOD
11974 45 5f 51 55 45 52 59 0a 20 20 20 20 20 20 20 20  E_QUERY.        
11975 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41      || eMode==PA
11976 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
11977 4e 4f 52 4d 41 4c 0a 20 20 20 20 20 20 20 20 20  NORMAL.         
11978 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47     || eMode==PAG
11979 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45  ER_LOCKINGMODE_E
1197a 58 43 4c 55 53 49 56 45 20 29 3b 0a 20 20 61 73  XCLUSIVE );.  as
1197b 73 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b  sert( PAGER_LOCK
1197c 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 3c 30 20  INGMODE_QUERY<0 
1197d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47  );.  assert( PAG
1197e 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e  ER_LOCKINGMODE_N
1197f 4f 52 4d 41 4c 3e 3d 30 20 26 26 20 50 41 47 45  ORMAL>=0 && PAGE
11980 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58  R_LOCKINGMODE_EX
11981 43 4c 55 53 49 56 45 3e 3d 30 20 29 3b 0a 20 20  CLUSIVE>=0 );.  
11982 69 66 28 20 65 4d 6f 64 65 3e 3d 30 20 26 26 20  if( eMode>=0 && 
11983 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c  !pPager->tempFil
11984 65 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d  e ){.    pPager-
11985 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d  >exclusiveMode =
11986 20 65 4d 6f 64 65 3b 0a 20 20 7d 0a 20 20 72 65   eMode;.  }.  re
11987 74 75 72 6e 20 28 69 6e 74 29 70 50 61 67 65 72  turn (int)pPager
11988 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 3b  ->exclusiveMode;
11989 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65  .}../*.** Get/se
1198a 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f  t the journal-mo
1198b 64 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65  de for this page
1198c 72 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d 6f  r. Parameter eMo
1198d 64 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f  de must be one o
1198e 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 50 41 47 45  f:.**.**    PAGE
1198f 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55  R_JOURNALMODE_QU
11990 45 52 59 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f  ERY.**    PAGER_
11991 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45  JOURNALMODE_DELE
11992 54 45 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a  TE.**    PAGER_J
11993 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43  OURNALMODE_TRUNC
11994 41 54 45 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f  ATE.**    PAGER_
11995 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53  JOURNALMODE_PERS
11996 49 53 54 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f  IST.**    PAGER_
11997 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a  JOURNALMODE_OFF.
11998 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 72  **.** If the par
11999 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 5f 51  ameter is not _Q
1199a 55 45 52 59 2c 20 74 68 65 6e 20 74 68 65 20 6a  UERY, then the j
1199b 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 69 73 20 73  ournal-mode is s
1199c 65 74 20 74 6f 20 74 68 65 0a 2a 2a 20 76 61 6c  et to the.** val
1199d 75 65 20 73 70 65 63 69 66 69 65 64 2e 0a 2a 2a  ue specified..**
1199e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64  .** The returned
1199f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 63 75   indicate the cu
119a0 72 72 65 6e 74 20 28 70 6f 73 73 69 62 6c 79 20  rrent (possibly 
119a1 75 70 64 61 74 65 64 29 0a 2a 2a 20 6a 6f 75 72  updated).** jour
119a2 6e 61 6c 2d 6d 6f 64 65 2e 0a 2a 2f 0a 53 51 4c  nal-mode..*/.SQL
119a3 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
119a4 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72  sqlite3PagerJour
119a5 6e 61 6c 4d 6f 64 65 28 50 61 67 65 72 20 2a 70  nalMode(Pager *p
119a6 50 61 67 65 72 2c 20 69 6e 74 20 65 4d 6f 64 65  Pager, int eMode
119a7 29 7b 0a 20 20 69 66 28 20 21 4d 45 4d 44 42 20  ){.  if( !MEMDB 
119a8 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 65  ){.    assert( e
119a9 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
119aa 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 0a 20 20  NALMODE_QUERY.  
119ab 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65              || e
119ac 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
119ad 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 20  NALMODE_DELETE. 
119ae 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
119af 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  eMode==PAGER_JOU
119b0 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54  RNALMODE_TRUNCAT
119b1 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E.              
119b2 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  || eMode==PAGER_
119b3 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53  JOURNALMODE_PERS
119b4 49 53 54 0a 20 20 20 20 20 20 20 20 20 20 20 20  IST.            
119b5 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45    || eMode==PAGE
119b6 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46  R_JOURNALMODE_OF
119b7 46 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  F .             
119b8 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52   || eMode==PAGER
119b9 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d  _JOURNALMODE_MEM
119ba 4f 52 59 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ORY );.    asser
119bb 74 28 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  t( PAGER_JOURNAL
119bc 4d 4f 44 45 5f 51 55 45 52 59 3c 30 20 29 3b 0a  MODE_QUERY<0 );.
119bd 20 20 20 20 69 66 28 20 65 4d 6f 64 65 3e 3d 30      if( eMode>=0
119be 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72   ){.      pPager
119bf 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20  ->journalMode = 
119c0 65 4d 6f 64 65 3b 0a 20 20 20 20 7d 65 6c 73 65  eMode;.    }else
119c1 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
119c2 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  eMode==PAGER_JOU
119c3 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 20 29  RNALMODE_QUERY )
119c4 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
119c5 74 75 72 6e 20 28 69 6e 74 29 70 50 61 67 65 72  turn (int)pPager
119c6 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 0a 7d  ->journalMode;.}
119c7 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 74 20  ../*.** Get/set 
119c8 74 68 65 20 73 69 7a 65 2d 6c 69 6d 69 74 20 75  the size-limit u
119c9 73 65 64 20 66 6f 72 20 70 65 72 73 69 73 74 65  sed for persiste
119ca 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73  nt journal files
119cb 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
119cc 41 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 50  ATE i64 sqlite3P
119cd 61 67 65 72 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c  agerJournalSizeL
119ce 69 6d 69 74 28 50 61 67 65 72 20 2a 70 50 61 67  imit(Pager *pPag
119cf 65 72 2c 20 69 36 34 20 69 4c 69 6d 69 74 29 7b  er, i64 iLimit){
119d0 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 3e 3d 2d  .  if( iLimit>=-
119d1 31 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d  1 ){.    pPager-
119d2 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69  >journalSizeLimi
119d3 74 20 3d 20 69 4c 69 6d 69 74 3b 0a 20 20 7d 0a  t = iLimit;.  }.
119d4 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
119d5 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69  >journalSizeLimi
119d6 74 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  t;.}..#endif /* 
119d7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b  SQLITE_OMIT_DISK
119d8 49 4f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  IO */../********
119d9 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61  ****** End of pa
119da 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ger.c **********
119db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
119dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
119dd 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
119de 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
119df 65 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a  e btmutex.c ****
119e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
119e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
119e2 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
119e3 37 20 41 75 67 75 73 74 20 32 37 0a 2a 2a 0a 2a  7 August 27.**.*
119e4 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
119e5 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
119e6 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
119e7 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
119e8 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
119e9 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
119ea 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
119eb 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
119ec 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
119ed 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
119ee 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
119ef 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
119f0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
119f1 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
119f2 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
119f3 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
119f4 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
119f5 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
119f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
119f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
119f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
119f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
119fa 20 24 49 64 3a 20 62 74 6d 75 74 65 78 2e 63 2c   $Id: btmutex.c,
119fb 76 20 31 2e 31 31 20 32 30 30 38 2f 31 30 2f 30  v 1.11 2008/10/0
119fc 37 20 31 35 3a 32 35 3a 34 38 20 64 72 68 20 45  7 15:25:48 drh E
119fd 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  xp $.**.** This 
119fe 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
119ff 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  de used to imple
11a00 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 6f 6e 20  ment mutexes on 
11a01 42 74 72 65 65 20 6f 62 6a 65 63 74 73 2e 0a 2a  Btree objects..*
11a02 2a 20 54 68 69 73 20 63 6f 64 65 20 72 65 61 6c  * This code real
11a03 6c 79 20 62 65 6c 6f 6e 67 73 20 69 6e 20 62 74  ly belongs in bt
11a04 72 65 65 2e 63 2e 20 20 42 75 74 20 62 74 72 65  ree.c.  But btre
11a05 65 2e 63 20 69 73 20 67 65 74 74 69 6e 67 20 74  e.c is getting t
11a06 6f 6f 0a 2a 2a 20 62 69 67 20 61 6e 64 20 77 65  oo.** big and we
11a07 20 77 61 6e 74 20 74 6f 20 62 72 65 61 6b 20 69   want to break i
11a08 74 20 64 6f 77 6e 20 73 6f 6d 65 2e 20 20 54 68  t down some.  Th
11a09 69 73 20 70 61 63 6b 61 67 65 64 20 73 65 65 6d  is packaged seem
11a0a 65 64 20 6c 69 6b 65 0a 2a 2a 20 61 20 67 6f 6f  ed like.** a goo
11a0b 64 20 62 72 65 61 6b 6f 75 74 2e 0a 2a 2f 0a 2f  d breakout..*/./
11a0c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49  ************** I
11a0d 6e 63 6c 75 64 65 20 62 74 72 65 65 49 6e 74 2e  nclude btreeInt.
11a0e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
11a0f 6f 66 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a  of btmutex.c ***
11a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
11a11 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
11a12 65 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65 49  egin file btreeI
11a13 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
11a14 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11a15 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
11a16 2a 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20  *.** 2004 April 
11a17 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  6.**.** The auth
11a18 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
11a19 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
11a1a 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
11a1b 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
11a1c 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
11a1d 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
11a1e 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
11a1f 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
11a20 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
11a21 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
11a22 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
11a23 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
11a24 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
11a25 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
11a26 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
11a27 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
11a28 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
11a29 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11a2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11a2b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11a2c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11a2d 0a 2a 2a 20 24 49 64 3a 20 62 74 72 65 65 49 6e  .** $Id: btreeIn
11a2e 74 2e 68 2c 76 20 31 2e 33 34 20 32 30 30 38 2f  t.h,v 1.34 2008/
11a2f 30 39 2f 33 30 20 31 37 3a 31 38 3a 31 37 20 64  09/30 17:18:17 d
11a30 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54  rh Exp $.**.** T
11a31 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65  his file impleme
11a32 6e 74 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28  nts a external (
11a33 64 69 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61  disk-based) data
11a34 62 61 73 65 20 75 73 69 6e 67 20 42 54 72 65 65  base using BTree
11a35 73 2e 0a 2a 2a 20 46 6f 72 20 61 20 64 65 74 61  s..** For a deta
11a36 69 6c 65 64 20 64 69 73 63 75 73 73 69 6f 6e 20  iled discussion 
11a37 6f 66 20 42 54 72 65 65 73 2c 20 72 65 66 65 72  of BTrees, refer
11a38 20 74 6f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 6f   to.**.**     Do
11a39 6e 61 6c 64 20 45 2e 20 4b 6e 75 74 68 2c 20 54  nald E. Knuth, T
11a3a 48 45 20 41 52 54 20 4f 46 20 43 4f 4d 50 55 54  HE ART OF COMPUT
11a3b 45 52 20 50 52 4f 47 52 41 4d 4d 49 4e 47 2c 20  ER PROGRAMMING, 
11a3c 56 6f 6c 75 6d 65 20 33 3a 0a 2a 2a 20 20 20 20  Volume 3:.**    
11a3d 20 22 53 6f 72 74 69 6e 67 20 41 6e 64 20 53 65   "Sorting And Se
11a3e 61 72 63 68 69 6e 67 22 2c 20 70 61 67 65 73 20  arching", pages 
11a3f 34 37 33 2d 34 38 30 2e 20 41 64 64 69 73 6f 6e  473-480. Addison
11a40 2d 57 65 73 6c 65 79 0a 2a 2a 20 20 20 20 20 50  -Wesley.**     P
11a41 75 62 6c 69 73 68 69 6e 67 20 43 6f 6d 70 61 6e  ublishing Compan
11a42 79 2c 20 52 65 61 64 69 6e 67 2c 20 4d 61 73 73  y, Reading, Mass
11a43 61 63 68 75 73 65 74 74 73 2e 0a 2a 2a 0a 2a 2a  achusetts..**.**
11a44 20 54 68 65 20 62 61 73 69 63 20 69 64 65 61 20   The basic idea 
11a45 69 73 20 74 68 61 74 20 65 61 63 68 20 70 61 67  is that each pag
11a46 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f  e of the file co
11a47 6e 74 61 69 6e 73 20 4e 20 64 61 74 61 62 61 73  ntains N databas
11a48 65 0a 2a 2a 20 65 6e 74 72 69 65 73 20 61 6e 64  e.** entries and
11a49 20 4e 2b 31 20 70 6f 69 6e 74 65 72 73 20 74 6f   N+1 pointers to
11a4a 20 73 75 62 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a   subpages..**.**
11a4b 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
11a4c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11a4d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11a4e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11a4f 2d 2d 2d 0a 2a 2a 20 20 20 7c 20 20 50 74 72 28  ---.**   |  Ptr(
11a50 30 29 20 7c 20 4b 65 79 28 30 29 20 7c 20 50 74  0) | Key(0) | Pt
11a51 72 28 31 29 20 7c 20 4b 65 79 28 31 29 20 7c 20  r(1) | Key(1) | 
11a52 2e 2e 2e 20 7c 20 4b 65 79 28 4e 2d 31 29 20 7c  ... | Key(N-1) |
11a53 20 50 74 72 28 4e 29 20 7c 0a 2a 2a 20 20 20 2d   Ptr(N) |.**   -
11a54 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11a55 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11a56 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11a57 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
11a58 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65  **.** All of the
11a59 20 6b 65 79 73 20 6f 6e 20 74 68 65 20 70 61 67   keys on the pag
11a5a 65 20 74 68 61 74 20 50 74 72 28 30 29 20 70 6f  e that Ptr(0) po
11a5b 69 6e 74 73 20 74 6f 20 68 61 76 65 20 76 61 6c  ints to have val
11a5c 75 65 73 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e  ues less.** than
11a5d 20 4b 65 79 28 30 29 2e 20 20 41 6c 6c 20 6f 66   Key(0).  All of
11a5e 20 74 68 65 20 6b 65 79 73 20 6f 6e 20 70 61 67   the keys on pag
11a5f 65 20 50 74 72 28 31 29 20 61 6e 64 20 69 74 73  e Ptr(1) and its
11a60 20 73 75 62 70 61 67 65 73 20 68 61 76 65 0a 2a   subpages have.*
11a61 2a 20 76 61 6c 75 65 73 20 67 72 65 61 74 65 72  * values greater
11a62 20 74 68 61 6e 20 4b 65 79 28 30 29 20 61 6e 64   than Key(0) and
11a63 20 6c 65 73 73 20 74 68 61 6e 20 4b 65 79 28 31   less than Key(1
11a64 29 2e 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b  ).  All of the k
11a65 65 79 73 0a 2a 2a 20 6f 6e 20 50 74 72 28 4e 29  eys.** on Ptr(N)
11a66 20 61 6e 64 20 69 74 73 20 73 75 62 70 61 67 65   and its subpage
11a67 73 20 68 61 76 65 20 76 61 6c 75 65 73 20 67 72  s have values gr
11a68 65 61 74 65 72 20 74 68 61 6e 20 4b 65 79 28 4e  eater than Key(N
11a69 2d 31 29 2e 20 20 41 6e 64 0a 2a 2a 20 73 6f 20  -1).  And.** so 
11a6a 66 6f 72 74 68 2e 0a 2a 2a 0a 2a 2a 20 46 69 6e  forth..**.** Fin
11a6b 64 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61  ding a particula
11a6c 72 20 6b 65 79 20 72 65 71 75 69 72 65 73 20 72  r key requires r
11a6d 65 61 64 69 6e 67 20 4f 28 6c 6f 67 28 4d 29 29  eading O(log(M))
11a6e 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20   pages from the 
11a6f 0a 2a 2a 20 64 69 73 6b 20 77 68 65 72 65 20 4d  .** disk where M
11a70 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
11a71 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65  f entries in the
11a72 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20   tree..**.** In 
11a73 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74  this implementat
11a74 69 6f 6e 2c 20 61 20 73 69 6e 67 6c 65 20 66 69  ion, a single fi
11a75 6c 65 20 63 61 6e 20 68 6f 6c 64 20 6f 6e 65 20  le can hold one 
11a76 6f 72 20 6d 6f 72 65 20 73 65 70 61 72 61 74 65  or more separate
11a77 20 0a 2a 2a 20 42 54 72 65 65 73 2e 20 20 45 61   .** BTrees.  Ea
11a78 63 68 20 42 54 72 65 65 20 69 73 20 69 64 65 6e  ch BTree is iden
11a79 74 69 66 69 65 64 20 62 79 20 74 68 65 20 69 6e  tified by the in
11a7a 64 65 78 20 6f 66 20 69 74 73 20 72 6f 6f 74 20  dex of its root 
11a7b 70 61 67 65 2e 20 20 54 68 65 0a 2a 2a 20 6b 65  page.  The.** ke
11a7c 79 20 61 6e 64 20 64 61 74 61 20 66 6f 72 20 61  y and data for a
11a7d 6e 79 20 65 6e 74 72 79 20 61 72 65 20 63 6f 6d  ny entry are com
11a7e 62 69 6e 65 64 20 74 6f 20 66 6f 72 6d 20 74 68  bined to form th
11a7f 65 20 22 70 61 79 6c 6f 61 64 22 2e 20 20 41 0a  e "payload".  A.
11a80 2a 2a 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20  ** fixed amount 
11a81 6f 66 20 70 61 79 6c 6f 61 64 20 63 61 6e 20 62  of payload can b
11a82 65 20 63 61 72 72 69 65 64 20 64 69 72 65 63 74  e carried direct
11a83 6c 79 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ly on the databa
11a84 73 65 0a 2a 2a 20 70 61 67 65 2e 20 20 49 66 20  se.** page.  If 
11a85 74 68 65 20 70 61 79 6c 6f 61 64 20 69 73 20 6c  the payload is l
11a86 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 70  arger than the p
11a87 72 65 73 65 74 20 61 6d 6f 75 6e 74 20 74 68 65  reset amount the
11a88 6e 20 73 75 72 70 6c 75 73 0a 2a 2a 20 62 79 74  n surplus.** byt
11a89 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e  es are stored on
11a8a 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e   overflow pages.
11a8b 20 20 54 68 65 20 70 61 79 6c 6f 61 64 20 66 6f    The payload fo
11a8c 72 20 61 6e 20 65 6e 74 72 79 0a 2a 2a 20 61 6e  r an entry.** an
11a8d 64 20 74 68 65 20 70 72 65 63 65 64 69 6e 67 20  d the preceding 
11a8e 70 6f 69 6e 74 65 72 20 61 72 65 20 63 6f 6d 62  pointer are comb
11a8f 69 6e 65 64 20 74 6f 20 66 6f 72 6d 20 61 20 22  ined to form a "
11a90 43 65 6c 6c 22 2e 20 20 45 61 63 68 20 0a 2a 2a  Cell".  Each .**
11a91 20 70 61 67 65 20 68 61 73 20 61 20 73 6d 61 6c   page has a smal
11a92 6c 20 68 65 61 64 65 72 20 77 68 69 63 68 20 63  l header which c
11a93 6f 6e 74 61 69 6e 73 20 74 68 65 20 50 74 72 28  ontains the Ptr(
11a94 4e 29 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 6f  N) pointer and o
11a95 74 68 65 72 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74  ther.** informat
11a96 69 6f 6e 20 73 75 63 68 20 61 73 20 74 68 65 20  ion such as the 
11a97 73 69 7a 65 20 6f 66 20 6b 65 79 20 61 6e 64 20  size of key and 
11a98 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 46 4f 52 4d  data..**.** FORM
11a99 41 54 20 44 45 54 41 49 4c 53 0a 2a 2a 0a 2a 2a  AT DETAILS.**.**
11a9a 20 54 68 65 20 66 69 6c 65 20 69 73 20 64 69 76   The file is div
11a9b 69 64 65 64 20 69 6e 74 6f 20 70 61 67 65 73 2e  ided into pages.
11a9c 20 20 54 68 65 20 66 69 72 73 74 20 70 61 67 65    The first page
11a9d 20 69 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20   is called page 
11a9e 31 2c 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64  1,.** the second
11a9f 20 69 73 20 70 61 67 65 20 32 2c 20 61 6e 64 20   is page 2, and 
11aa0 73 6f 20 66 6f 72 74 68 2e 20 20 41 20 70 61 67  so forth.  A pag
11aa1 65 20 6e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f  e number of zero
11aa2 20 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 22 6e   indicates.** "n
11aa3 6f 20 73 75 63 68 20 70 61 67 65 22 2e 20 20 54  o such page".  T
11aa4 68 65 20 70 61 67 65 20 73 69 7a 65 20 63 61 6e  he page size can
11aa5 20 62 65 20 61 6e 79 74 68 69 6e 67 20 62 65 74   be anything bet
11aa6 77 65 65 6e 20 35 31 32 20 61 6e 64 20 36 35 35  ween 512 and 655
11aa7 33 36 2e 0a 2a 2a 20 45 61 63 68 20 70 61 67 65  36..** Each page
11aa8 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61   can be either a
11aa9 20 62 74 72 65 65 20 70 61 67 65 2c 20 61 20 66   btree page, a f
11aaa 72 65 65 6c 69 73 74 20 70 61 67 65 20 6f 72 20  reelist page or 
11aab 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70  an overflow.** p
11aac 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  age..**.** The f
11aad 69 72 73 74 20 70 61 67 65 20 69 73 20 61 6c 77  irst page is alw
11aae 61 79 73 20 61 20 62 74 72 65 65 20 70 61 67 65  ays a btree page
11aaf 2e 20 20 54 68 65 20 66 69 72 73 74 20 31 30 30  .  The first 100
11ab0 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 66 69   bytes of the fi
11ab1 72 73 74 0a 2a 2a 20 70 61 67 65 20 63 6f 6e 74  rst.** page cont
11ab2 61 69 6e 20 61 20 73 70 65 63 69 61 6c 20 68 65  ain a special he
11ab3 61 64 65 72 20 28 74 68 65 20 22 66 69 6c 65 20  ader (the "file 
11ab4 68 65 61 64 65 72 22 29 20 74 68 61 74 20 64 65  header") that de
11ab5 73 63 72 69 62 65 73 20 74 68 65 20 66 69 6c 65  scribes the file
11ab6 2e 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 20  ..** The format 
11ab7 6f 66 20 74 68 65 20 66 69 6c 65 20 68 65 61 64  of the file head
11ab8 65 72 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73  er is as follows
11ab9 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 45 54  :.**.**   OFFSET
11aba 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52     SIZE    DESCR
11abb 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 30  IPTION.**      0
11abc 20 20 20 20 20 20 31 36 20 20 20 20 20 48 65 61        16     Hea
11abd 64 65 72 20 73 74 72 69 6e 67 3a 20 22 53 51 4c  der string: "SQL
11abe 69 74 65 20 66 6f 72 6d 61 74 20 33 5c 30 30 30  ite format 3\000
11abf 22 0a 2a 2a 20 20 20 20 20 31 36 20 20 20 20 20  ".**     16     
11ac0 20 20 32 20 20 20 20 20 50 61 67 65 20 73 69 7a    2     Page siz
11ac1 65 20 69 6e 20 62 79 74 65 73 2e 20 20 0a 2a 2a  e in bytes.  .**
11ac2 20 20 20 20 20 31 38 20 20 20 20 20 20 20 31 20       18       1 
11ac3 20 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20      File format 
11ac4 77 72 69 74 65 20 76 65 72 73 69 6f 6e 0a 2a 2a  write version.**
11ac5 20 20 20 20 20 31 39 20 20 20 20 20 20 20 31 20       19       1 
11ac6 20 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20      File format 
11ac7 72 65 61 64 20 76 65 72 73 69 6f 6e 0a 2a 2a 20  read version.** 
11ac8 20 20 20 20 32 30 20 20 20 20 20 20 20 31 20 20      20       1  
11ac9 20 20 20 42 79 74 65 73 20 6f 66 20 75 6e 75 73     Bytes of unus
11aca 65 64 20 73 70 61 63 65 20 61 74 20 74 68 65 20  ed space at the 
11acb 65 6e 64 20 6f 66 20 65 61 63 68 20 70 61 67 65  end of each page
11acc 0a 2a 2a 20 20 20 20 20 32 31 20 20 20 20 20 20  .**     21      
11acd 20 31 20 20 20 20 20 4d 61 78 20 65 6d 62 65 64   1     Max embed
11ace 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63  ded payload frac
11acf 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 32 20 20  tion.**     22  
11ad0 20 20 20 20 20 31 20 20 20 20 20 4d 69 6e 20 65       1     Min e
11ad1 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20  mbedded payload 
11ad2 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20  fraction.**     
11ad3 32 33 20 20 20 20 20 20 20 31 20 20 20 20 20 4d  23       1     M
11ad4 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20  in leaf payload 
11ad5 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20  fraction.**     
11ad6 32 34 20 20 20 20 20 20 20 34 20 20 20 20 20 46  24       4     F
11ad7 69 6c 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74  ile change count
11ad8 65 72 0a 2a 2a 20 20 20 20 20 32 38 20 20 20 20  er.**     28    
11ad9 20 20 20 34 20 20 20 20 20 52 65 73 65 72 76 65     4     Reserve
11ada 64 20 66 6f 72 20 66 75 74 75 72 65 20 75 73 65  d for future use
11adb 0a 2a 2a 20 20 20 20 20 33 32 20 20 20 20 20 20  .**     32      
11adc 20 34 20 20 20 20 20 46 69 72 73 74 20 66 72 65   4     First fre
11add 65 6c 69 73 74 20 70 61 67 65 0a 2a 2a 20 20 20  elist page.**   
11ade 20 20 33 36 20 20 20 20 20 20 20 34 20 20 20 20    36       4    
11adf 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c   Number of freel
11ae0 69 73 74 20 70 61 67 65 73 20 69 6e 20 74 68 65  ist pages in the
11ae1 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 34 30 20   file.**     40 
11ae2 20 20 20 20 20 36 30 20 20 20 20 20 31 35 20 34       60     15 4
11ae3 2d 62 79 74 65 20 6d 65 74 61 20 76 61 6c 75 65  -byte meta value
11ae4 73 20 70 61 73 73 65 64 20 74 6f 20 68 69 67 68  s passed to high
11ae5 65 72 20 6c 61 79 65 72 73 0a 2a 2a 0a 2a 2a 20  er layers.**.** 
11ae6 41 6c 6c 20 6f 66 20 74 68 65 20 69 6e 74 65 67  All of the integ
11ae7 65 72 20 76 61 6c 75 65 73 20 61 72 65 20 62 69  er values are bi
11ae8 67 2d 65 6e 64 69 61 6e 20 28 6d 6f 73 74 20 73  g-endian (most s
11ae9 69 67 6e 69 66 69 63 61 6e 74 20 62 79 74 65 20  ignificant byte 
11aea 66 69 72 73 74 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  first)..**.** Th
11aeb 65 20 66 69 6c 65 20 63 68 61 6e 67 65 20 63 6f  e file change co
11aec 75 6e 74 65 72 20 69 73 20 69 6e 63 72 65 6d 65  unter is increme
11aed 6e 74 65 64 20 77 68 65 6e 20 74 68 65 20 64 61  nted when the da
11aee 74 61 62 61 73 65 20 69 73 20 63 68 61 6e 67 65  tabase is change
11aef 64 0a 2a 2a 20 54 68 69 73 20 63 6f 75 6e 74 65  d.** This counte
11af0 72 20 61 6c 6c 6f 77 73 20 6f 74 68 65 72 20 70  r allows other p
11af1 72 6f 63 65 73 73 65 73 20 74 6f 20 6b 6e 6f 77  rocesses to know
11af2 20 77 68 65 6e 20 74 68 65 20 66 69 6c 65 20 68   when the file h
11af3 61 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 61 6e  as changed.** an
11af4 64 20 74 68 75 73 20 77 68 65 6e 20 74 68 65 79  d thus when they
11af5 20 6e 65 65 64 20 74 6f 20 66 6c 75 73 68 20 74   need to flush t
11af6 68 65 69 72 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a  heir cache..**.*
11af7 2a 20 54 68 65 20 6d 61 78 20 65 6d 62 65 64 64  * The max embedd
11af8 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74  ed payload fract
11af9 69 6f 6e 20 69 73 20 74 68 65 20 61 6d 6f 75 6e  ion is the amoun
11afa 74 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 20 75  t of the total u
11afb 73 61 62 6c 65 0a 2a 2a 20 73 70 61 63 65 20 69  sable.** space i
11afc 6e 20 61 20 70 61 67 65 20 74 68 61 74 20 63 61  n a page that ca
11afd 6e 20 62 65 20 63 6f 6e 73 75 6d 65 64 20 62 79  n be consumed by
11afe 20 61 20 73 69 6e 67 6c 65 20 63 65 6c 6c 20 66   a single cell f
11aff 6f 72 20 73 74 61 6e 64 61 72 64 0a 2a 2a 20 42  or standard.** B
11b00 2d 74 72 65 65 20 28 6e 6f 6e 2d 4c 45 41 46 44  -tree (non-LEAFD
11b01 41 54 41 29 20 74 61 62 6c 65 73 2e 20 20 41 20  ATA) tables.  A 
11b02 76 61 6c 75 65 20 6f 66 20 32 35 35 20 6d 65 61  value of 255 mea
11b03 6e 73 20 31 30 30 25 2e 20 20 54 68 65 20 64 65  ns 100%.  The de
11b04 66 61 75 6c 74 0a 2a 2a 20 69 73 20 74 6f 20 6c  fault.** is to l
11b05 69 6d 69 74 20 74 68 65 20 6d 61 78 69 6d 75 6d  imit the maximum
11b06 20 63 65 6c 6c 20 73 69 7a 65 20 73 6f 20 74 68   cell size so th
11b07 61 74 20 61 74 20 6c 65 61 73 74 20 34 20 63 65  at at least 4 ce
11b08 6c 6c 73 20 77 69 6c 6c 20 66 69 74 0a 2a 2a 20  lls will fit.** 
11b09 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20 20 54 68  on one page.  Th
11b0a 75 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 6d  us the default m
11b0b 61 78 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c  ax embedded payl
11b0c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69 73 20  oad fraction is 
11b0d 36 34 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  64..**.** If the
11b0e 20 70 61 79 6c 6f 61 64 20 66 6f 72 20 61 20 63   payload for a c
11b0f 65 6c 6c 20 69 73 20 6c 61 72 67 65 72 20 74 68  ell is larger th
11b10 61 6e 20 74 68 65 20 6d 61 78 20 70 61 79 6c 6f  an the max paylo
11b11 61 64 2c 20 74 68 65 6e 20 65 78 74 72 61 0a 2a  ad, then extra.*
11b12 2a 20 70 61 79 6c 6f 61 64 20 69 73 20 73 70 69  * payload is spi
11b13 6c 6c 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77  lled to overflow
11b14 20 70 61 67 65 73 2e 20 20 4f 6e 63 65 20 61 6e   pages.  Once an
11b15 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69   overflow page i
11b16 73 20 61 6c 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20  s allocated,.** 
11b17 61 73 20 6d 61 6e 79 20 62 79 74 65 73 20 61 73  as many bytes as
11b18 20 70 6f 73 73 69 62 6c 65 20 61 72 65 20 6d 6f   possible are mo
11b19 76 65 64 20 69 6e 74 6f 20 74 68 65 20 6f 76 65  ved into the ove
11b1a 72 66 6c 6f 77 20 70 61 67 65 73 20 77 69 74 68  rflow pages with
11b1b 6f 75 74 20 6c 65 74 74 69 6e 67 0a 2a 2a 20 74  out letting.** t
11b1c 68 65 20 63 65 6c 6c 20 73 69 7a 65 20 64 72 6f  he cell size dro
11b1d 70 20 62 65 6c 6f 77 20 74 68 65 20 6d 69 6e 20  p below the min 
11b1e 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64  embedded payload
11b1f 20 66 72 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   fraction..**.**
11b20 20 54 68 65 20 6d 69 6e 20 6c 65 61 66 20 70 61   The min leaf pa
11b21 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69  yload fraction i
11b22 73 20 6c 69 6b 65 20 74 68 65 20 6d 69 6e 20 65  s like the min e
11b23 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20  mbedded payload 
11b24 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 65 78 63 65  fraction.** exce
11b25 70 74 20 74 68 61 74 20 69 74 20 61 70 70 6c 69  pt that it appli
11b26 65 73 20 74 6f 20 6c 65 61 66 20 6e 6f 64 65 73  es to leaf nodes
11b27 20 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74   in a LEAFDATA t
11b28 72 65 65 2e 20 20 54 68 65 20 6d 61 78 69 6d 75  ree.  The maximu
11b29 6d 0a 2a 2a 20 70 61 79 6c 6f 61 64 20 66 72 61  m.** payload fra
11b2a 63 74 69 6f 6e 20 66 6f 72 20 61 20 4c 45 41 46  ction for a LEAF
11b2b 44 41 54 41 20 74 72 65 65 20 69 73 20 61 6c 77  DATA tree is alw
11b2c 61 79 73 20 31 30 30 25 20 28 6f 72 20 32 35 35  ays 100% (or 255
11b2d 29 20 61 6e 64 20 69 74 0a 2a 2a 20 6e 6f 74 20  ) and it.** not 
11b2e 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65  specified in the
11b2f 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 45   header..**.** E
11b30 61 63 68 20 62 74 72 65 65 20 70 61 67 65 73 20  ach btree pages 
11b31 69 73 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20  is divided into 
11b32 74 68 72 65 65 20 73 65 63 74 69 6f 6e 73 3a 20  three sections: 
11b33 20 54 68 65 20 68 65 61 64 65 72 2c 20 74 68 65   The header, the
11b34 0a 2a 2a 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72  .** cell pointer
11b35 20 61 72 72 61 79 2c 20 61 6e 64 20 74 68 65 20   array, and the 
11b36 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65  cell content are
11b37 61 2e 20 20 50 61 67 65 20 31 20 61 6c 73 6f 20  a.  Page 1 also 
11b38 68 61 73 20 61 20 31 30 30 2d 62 79 74 65 0a 2a  has a 100-byte.*
11b39 2a 20 66 69 6c 65 20 68 65 61 64 65 72 20 74 68  * file header th
11b3a 61 74 20 6f 63 63 75 72 73 20 62 65 66 6f 72 65  at occurs before
11b3b 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72   the page header
11b3c 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d  ..**.**      |--
11b3d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a  --------------|.
11b3e 2a 2a 20 20 20 20 20 20 7c 20 66 69 6c 65 20 68  **      | file h
11b3f 65 61 64 65 72 20 20 20 20 7c 20 20 20 31 30 30  eader    |   100
11b40 20 62 79 74 65 73 2e 20 20 50 61 67 65 20 31 20   bytes.  Page 1 
11b41 6f 6e 6c 79 2e 0a 2a 2a 20 20 20 20 20 20 7c 2d  only..**      |-
11b42 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c  ---------------|
11b43 0a 2a 2a 20 20 20 20 20 20 7c 20 70 61 67 65 20  .**      | page 
11b44 68 65 61 64 65 72 20 20 20 20 7c 20 20 20 38 20  header    |   8 
11b45 62 79 74 65 73 20 66 6f 72 20 6c 65 61 76 65 73  bytes for leaves
11b46 2e 20 20 31 32 20 62 79 74 65 73 20 66 6f 72 20  .  12 bytes for 
11b47 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 0a 2a  interior nodes.*
11b48 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d  *      |--------
11b49 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20  --------|.**    
11b4a 20 20 7c 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72    | cell pointer
11b4b 20 20 20 7c 20 20 20 7c 20 20 32 20 62 79 74 65     |   |  2 byte
11b4c 73 20 70 65 72 20 63 65 6c 6c 2e 20 20 53 6f 72  s per cell.  Sor
11b4d 74 65 64 20 6f 72 64 65 72 2e 0a 2a 2a 20 20 20  ted order..**   
11b4e 20 20 20 7c 20 61 72 72 61 79 20 20 20 20 20 20     | array      
11b4f 20 20 20 20 7c 20 20 20 7c 20 20 47 72 6f 77 73      |   |  Grows
11b50 20 64 6f 77 6e 77 61 72 64 0a 2a 2a 20 20 20 20   downward.**    
11b51 20 20 7c 20 20 20 20 20 20 20 20 20 20 20 20 20    |             
11b52 20 20 20 7c 20 20 20 76 0a 2a 2a 20 20 20 20 20     |   v.**     
11b53 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   |--------------
11b54 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 75 6e  --|.**      | un
11b55 61 6c 6c 6f 63 61 74 65 64 20 20 20 20 7c 0a 2a  allocated    |.*
11b56 2a 20 20 20 20 20 20 7c 20 73 70 61 63 65 20 20  *      | space  
11b57 20 20 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20          |.**    
11b58 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    |-------------
11b59 2d 2d 2d 7c 20 20 20 5e 20 20 47 72 6f 77 73 20  ---|   ^  Grows 
11b5a 75 70 77 61 72 64 73 0a 2a 2a 20 20 20 20 20 20  upwards.**      
11b5b 7c 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 20  | cell content  
11b5c 20 7c 20 20 20 7c 20 20 41 72 62 69 74 72 61 72   |   |  Arbitrar
11b5d 79 20 6f 72 64 65 72 20 69 6e 74 65 72 73 70 65  y order interspe
11b5e 72 73 65 64 20 77 69 74 68 20 66 72 65 65 62 6c  rsed with freebl
11b5f 6f 63 6b 73 2e 0a 2a 2a 20 20 20 20 20 20 7c 20  ocks..**      | 
11b60 61 72 65 61 20 20 20 20 20 20 20 20 20 20 20 7c  area           |
11b61 20 20 20 7c 20 20 61 6e 64 20 66 72 65 65 20 73     |  and free s
11b62 70 61 63 65 20 66 72 61 67 6d 65 6e 74 73 2e 0a  pace fragments..
11b63 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d  **      |-------
11b64 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 0a 2a 2a  ---------|.**.**
11b65 20 54 68 65 20 70 61 67 65 20 68 65 61 64 65 72   The page header
11b66 73 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69  s looks like thi
11b67 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 45  s:.**.**   OFFSE
11b68 54 20 20 20 53 49 5a 45 20 20 20 20 20 44 45 53  T   SIZE     DES
11b69 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20  CRIPTION.**     
11b6a 20 30 20 20 20 20 20 20 20 31 20 20 20 20 20 20   0       1      
11b6b 46 6c 61 67 73 2e 20 31 3a 20 69 6e 74 6b 65 79  Flags. 1: intkey
11b6c 2c 20 32 3a 20 7a 65 72 6f 64 61 74 61 2c 20 34  , 2: zerodata, 4
11b6d 3a 20 6c 65 61 66 64 61 74 61 2c 20 38 3a 20 6c  : leafdata, 8: l
11b6e 65 61 66 0a 2a 2a 20 20 20 20 20 20 31 20 20 20  eaf.**      1   
11b6f 20 20 20 20 32 20 20 20 20 20 20 62 79 74 65 20      2      byte 
11b70 6f 66 66 73 65 74 20 74 6f 20 74 68 65 20 66 69  offset to the fi
11b71 72 73 74 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a  rst freeblock.**
11b72 20 20 20 20 20 20 33 20 20 20 20 20 20 20 32 20        3       2 
11b73 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 63       number of c
11b74 65 6c 6c 73 20 6f 6e 20 74 68 69 73 20 70 61 67  ells on this pag
11b75 65 0a 2a 2a 20 20 20 20 20 20 35 20 20 20 20 20  e.**      5     
11b76 20 20 32 20 20 20 20 20 20 66 69 72 73 74 20 62    2      first b
11b77 79 74 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20  yte of the cell 
11b78 63 6f 6e 74 65 6e 74 20 61 72 65 61 0a 2a 2a 20  content area.** 
11b79 20 20 20 20 20 37 20 20 20 20 20 20 20 31 20 20       7       1  
11b7a 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 66 72      number of fr
11b7b 61 67 6d 65 6e 74 65 64 20 66 72 65 65 20 62 79  agmented free by
11b7c 74 65 73 0a 2a 2a 20 20 20 20 20 20 38 20 20 20  tes.**      8   
11b7d 20 20 20 20 34 20 20 20 20 20 20 52 69 67 68 74      4      Right
11b7e 20 63 68 69 6c 64 20 28 74 68 65 20 50 74 72 28   child (the Ptr(
11b7f 4e 29 20 76 61 6c 75 65 29 2e 20 20 4f 6d 69 74  N) value).  Omit
11b80 74 65 64 20 6f 6e 20 6c 65 61 76 65 73 2e 0a 2a  ted on leaves..*
11b81 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 64  *.** The flags d
11b82 65 66 69 6e 65 20 74 68 65 20 66 6f 72 6d 61 74  efine the format
11b83 20 6f 66 20 74 68 69 73 20 62 74 72 65 65 20 70   of this btree p
11b84 61 67 65 2e 20 20 54 68 65 20 6c 65 61 66 20 66  age.  The leaf f
11b85 6c 61 67 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a  lag means that.*
11b86 2a 20 74 68 69 73 20 70 61 67 65 20 68 61 73 20  * this page has 
11b87 6e 6f 20 63 68 69 6c 64 72 65 6e 2e 20 20 54 68  no children.  Th
11b88 65 20 7a 65 72 6f 64 61 74 61 20 66 6c 61 67 20  e zerodata flag 
11b89 6d 65 61 6e 73 20 74 68 61 74 20 74 68 69 73 20  means that this 
11b8a 70 61 67 65 20 63 61 72 72 69 65 73 0a 2a 2a 20  page carries.** 
11b8b 6f 6e 6c 79 20 6b 65 79 73 20 61 6e 64 20 6e 6f  only keys and no
11b8c 20 64 61 74 61 2e 20 20 54 68 65 20 69 6e 74 6b   data.  The intk
11b8d 65 79 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68  ey flag means th
11b8e 61 74 20 74 68 65 20 6b 65 79 20 69 73 20 61 20  at the key is a 
11b8f 69 6e 74 65 67 65 72 0a 2a 2a 20 77 68 69 63 68  integer.** which
11b90 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68   is stored in th
11b91 65 20 6b 65 79 20 73 69 7a 65 20 65 6e 74 72 79  e key size entry
11b92 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 68 65 61   of the cell hea
11b93 64 65 72 20 72 61 74 68 65 72 20 74 68 61 6e 20  der rather than 
11b94 69 6e 0a 2a 2a 20 74 68 65 20 70 61 79 6c 6f 61  in.** the payloa
11b95 64 20 61 72 65 61 2e 0a 2a 2a 0a 2a 2a 20 54 68  d area..**.** Th
11b96 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61  e cell pointer a
11b97 72 72 61 79 20 62 65 67 69 6e 73 20 6f 6e 20 74  rray begins on t
11b98 68 65 20 66 69 72 73 74 20 62 79 74 65 20 61 66  he first byte af
11b99 74 65 72 20 74 68 65 20 70 61 67 65 20 68 65 61  ter the page hea
11b9a 64 65 72 2e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c  der..** The cell
11b9b 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 63   pointer array c
11b9c 6f 6e 74 61 69 6e 73 20 7a 65 72 6f 20 6f 72 20  ontains zero or 
11b9d 6d 6f 72 65 20 32 2d 62 79 74 65 20 6e 75 6d 62  more 2-byte numb
11b9e 65 72 73 20 77 68 69 63 68 20 61 72 65 0a 2a 2a  ers which are.**
11b9f 20 6f 66 66 73 65 74 73 20 66 72 6f 6d 20 74 68   offsets from th
11ba0 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74  e beginning of t
11ba1 68 65 20 70 61 67 65 20 74 6f 20 74 68 65 20 63  he page to the c
11ba2 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 74  ell content in t
11ba3 68 65 20 63 65 6c 6c 0a 2a 2a 20 63 6f 6e 74 65  he cell.** conte
11ba4 6e 74 20 61 72 65 61 2e 20 20 54 68 65 20 63 65  nt area.  The ce
11ba5 6c 6c 20 70 6f 69 6e 74 65 72 73 20 6f 63 63 75  ll pointers occu
11ba6 72 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65  r in sorted orde
11ba7 72 2e 20 20 54 68 65 20 73 79 73 74 65 6d 20 73  r.  The system s
11ba8 74 72 69 76 65 73 0a 2a 2a 20 74 6f 20 6b 65 65  trives.** to kee
11ba9 70 20 66 72 65 65 20 73 70 61 63 65 20 61 66 74  p free space aft
11baa 65 72 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c  er the last cell
11bab 20 70 6f 69 6e 74 65 72 20 73 6f 20 74 68 61 74   pointer so that
11bac 20 6e 65 77 20 63 65 6c 6c 73 20 63 61 6e 0a 2a   new cells can.*
11bad 2a 20 62 65 20 65 61 73 69 6c 79 20 61 64 64 65  * be easily adde
11bae 64 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67  d without having
11baf 20 74 6f 20 64 65 66 72 61 67 6d 65 6e 74 20 74   to defragment t
11bb0 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 43  he page..**.** C
11bb1 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 73  ell content is s
11bb2 74 6f 72 65 64 20 61 74 20 74 68 65 20 76 65 72  tored at the ver
11bb3 79 20 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67  y end of the pag
11bb4 65 20 61 6e 64 20 67 72 6f 77 73 20 74 6f 77 61  e and grows towa
11bb5 72 64 20 74 68 65 0a 2a 2a 20 62 65 67 69 6e 6e  rd the.** beginn
11bb6 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67 65 2e  ing of the page.
11bb7 0a 2a 2a 0a 2a 2a 20 55 6e 75 73 65 64 20 73 70  .**.** Unused sp
11bb8 61 63 65 20 77 69 74 68 69 6e 20 74 68 65 20 63  ace within the c
11bb9 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61  ell content area
11bba 20 69 73 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e   is collected in
11bbb 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74  to a linked list
11bbc 20 6f 66 0a 2a 2a 20 66 72 65 65 62 6c 6f 63 6b   of.** freeblock
11bbd 73 2e 20 20 45 61 63 68 20 66 72 65 65 62 6c 6f  s.  Each freeblo
11bbe 63 6b 20 69 73 20 61 74 20 6c 65 61 73 74 20 34  ck is at least 4
11bbf 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20   bytes in size. 
11bc0 20 54 68 65 20 62 79 74 65 20 6f 66 66 73 65 74   The byte offset
11bc1 0a 2a 2a 20 74 6f 20 74 68 65 20 66 69 72 73 74  .** to the first
11bc2 20 66 72 65 65 62 6c 6f 63 6b 20 69 73 20 67 69   freeblock is gi
11bc3 76 65 6e 20 69 6e 20 74 68 65 20 68 65 61 64 65  ven in the heade
11bc4 72 2e 20 20 46 72 65 65 62 6c 6f 63 6b 73 20 6f  r.  Freeblocks o
11bc5 63 63 75 72 20 69 6e 0a 2a 2a 20 69 6e 63 72 65  ccur in.** incre
11bc6 61 73 69 6e 67 20 6f 72 64 65 72 2e 20 20 42 65  asing order.  Be
11bc7 63 61 75 73 65 20 61 20 66 72 65 65 62 6c 6f 63  cause a freebloc
11bc8 6b 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61  k must be at lea
11bc9 73 74 20 34 20 62 79 74 65 73 20 69 6e 20 73 69  st 4 bytes in si
11bca 7a 65 2c 0a 2a 2a 20 61 6e 79 20 67 72 6f 75 70  ze,.** any group
11bcb 20 6f 66 20 33 20 6f 72 20 66 65 77 65 72 20 75   of 3 or fewer u
11bcc 6e 75 73 65 64 20 62 79 74 65 73 20 69 6e 20 74  nused bytes in t
11bcd 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  he cell content 
11bce 61 72 65 61 20 63 61 6e 6e 6f 74 0a 2a 2a 20 65  area cannot.** e
11bcf 78 69 73 74 20 6f 6e 20 74 68 65 20 66 72 65 65  xist on the free
11bd0 62 6c 6f 63 6b 20 63 68 61 69 6e 2e 20 20 41 20  block chain.  A 
11bd1 67 72 6f 75 70 20 6f 66 20 33 20 6f 72 20 66 65  group of 3 or fe
11bd2 77 65 72 20 66 72 65 65 20 62 79 74 65 73 20 69  wer free bytes i
11bd3 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 61 20 66 72  s called.** a fr
11bd4 61 67 6d 65 6e 74 2e 20 20 54 68 65 20 74 6f 74  agment.  The tot
11bd5 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  al number of byt
11bd6 65 73 20 69 6e 20 61 6c 6c 20 66 72 61 67 6d 65  es in all fragme
11bd7 6e 74 73 20 69 73 20 72 65 63 6f 72 64 65 64 2e  nts is recorded.
11bd8 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 67 65 20  .** in the page 
11bd9 68 65 61 64 65 72 20 61 74 20 6f 66 66 73 65 74  header at offset
11bda 20 37 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a   7..**.**    SIZ
11bdb 45 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e  E    DESCRIPTION
11bdc 0a 2a 2a 20 20 20 20 20 20 32 20 20 20 20 20 42  .**      2     B
11bdd 79 74 65 20 6f 66 66 73 65 74 20 6f 66 20 74 68  yte offset of th
11bde 65 20 6e 65 78 74 20 66 72 65 65 62 6c 6f 63 6b  e next freeblock
11bdf 0a 2a 2a 20 20 20 20 20 20 32 20 20 20 20 20 42  .**      2     B
11be0 79 74 65 73 20 69 6e 20 74 68 69 73 20 66 72 65  ytes in this fre
11be1 65 62 6c 6f 63 6b 0a 2a 2a 0a 2a 2a 20 43 65 6c  eblock.**.** Cel
11be2 6c 73 20 61 72 65 20 6f 66 20 76 61 72 69 61 62  ls are of variab
11be3 6c 65 20 6c 65 6e 67 74 68 2e 20 20 43 65 6c 6c  le length.  Cell
11be4 73 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20  s are stored in 
11be5 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
11be6 20 61 72 65 61 20 61 74 0a 2a 2a 20 74 68 65 20   area at.** the 
11be7 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65 2e  end of the page.
11be8 20 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68    Pointers to th
11be9 65 20 63 65 6c 6c 73 20 61 72 65 20 69 6e 20 74  e cells are in t
11bea 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20  he cell pointer 
11beb 61 72 72 61 79 0a 2a 2a 20 74 68 61 74 20 69 6d  array.** that im
11bec 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77  mediately follow
11bed 73 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65  s the page heade
11bee 72 2e 20 20 43 65 6c 6c 73 20 69 73 20 6e 6f 74  r.  Cells is not
11bef 20 6e 65 63 65 73 73 61 72 69 6c 79 0a 2a 2a 20   necessarily.** 
11bf0 63 6f 6e 74 69 67 75 6f 75 73 20 6f 72 20 69 6e  contiguous or in
11bf1 20 6f 72 64 65 72 2c 20 62 75 74 20 63 65 6c 6c   order, but cell
11bf2 20 70 6f 69 6e 74 65 72 73 20 61 72 65 20 63 6f   pointers are co
11bf3 6e 74 69 67 75 6f 75 73 20 61 6e 64 20 69 6e 20  ntiguous and in 
11bf4 6f 72 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c  order..**.** Cel
11bf5 6c 20 63 6f 6e 74 65 6e 74 20 6d 61 6b 65 73 20  l content makes 
11bf6 75 73 65 20 6f 66 20 76 61 72 69 61 62 6c 65 20  use of variable 
11bf7 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 2e  length integers.
11bf8 20 20 41 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20    A variable.** 
11bf9 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 69  length integer i
11bfa 73 20 31 20 74 6f 20 39 20 62 79 74 65 73 20 77  s 1 to 9 bytes w
11bfb 68 65 72 65 20 74 68 65 20 6c 6f 77 65 72 20 37  here the lower 7
11bfc 20 62 69 74 73 20 6f 66 20 65 61 63 68 20 0a 2a   bits of each .*
11bfd 2a 20 62 79 74 65 20 61 72 65 20 75 73 65 64 2e  * byte are used.
11bfe 20 20 54 68 65 20 69 6e 74 65 67 65 72 20 63 6f    The integer co
11bff 6e 73 69 73 74 73 20 6f 66 20 61 6c 6c 20 62 79  nsists of all by
11c00 74 65 73 20 74 68 61 74 20 68 61 76 65 20 62 69  tes that have bi
11c01 74 20 38 20 73 65 74 20 61 6e 64 0a 2a 2a 20 74  t 8 set and.** t
11c02 68 65 20 66 69 72 73 74 20 62 79 74 65 20 77 69  he first byte wi
11c03 74 68 20 62 69 74 20 38 20 63 6c 65 61 72 2e 20  th bit 8 clear. 
11c04 20 54 68 65 20 6d 6f 73 74 20 73 69 67 6e 69 66   The most signif
11c05 69 63 61 6e 74 20 62 79 74 65 20 6f 66 20 74 68  icant byte of th
11c06 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 61 70 70  e integer.** app
11c07 65 61 72 73 20 66 69 72 73 74 2e 20 20 41 20 76  ears first.  A v
11c08 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69  ariable-length i
11c09 6e 74 65 67 65 72 20 6d 61 79 20 6e 6f 74 20 62  nteger may not b
11c0a 65 20 6d 6f 72 65 20 74 68 61 6e 20 39 20 62 79  e more than 9 by
11c0b 74 65 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 41 73 20  tes long..** As 
11c0c 61 20 73 70 65 63 69 61 6c 20 63 61 73 65 2c 20  a special case, 
11c0d 61 6c 6c 20 38 20 62 79 74 65 73 20 6f 66 20 74  all 8 bytes of t
11c0e 68 65 20 39 74 68 20 62 79 74 65 20 61 72 65 20  he 9th byte are 
11c0f 75 73 65 64 20 61 73 20 64 61 74 61 2e 20 20 54  used as data.  T
11c10 68 69 73 0a 2a 2a 20 61 6c 6c 6f 77 73 20 61 20  his.** allows a 
11c11 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 74  64-bit integer t
11c12 6f 20 62 65 20 65 6e 63 6f 64 65 64 20 69 6e 20  o be encoded in 
11c13 39 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20  9 bytes..**.**  
11c14 20 20 30 78 30 30 20 20 20 20 20 20 20 20 20 20    0x00          
11c15 20 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f              beco
11c16 6d 65 73 20 20 30 78 30 30 30 30 30 30 30 30 0a  mes  0x00000000.
11c17 2a 2a 20 20 20 20 30 78 37 66 20 20 20 20 20 20  **    0x7f      
11c18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11c19 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30  becomes  0x00000
11c1a 30 37 66 0a 2a 2a 20 20 20 20 30 78 38 31 20 30  07f.**    0x81 0
11c1b 78 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20  x00             
11c1c 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30      becomes  0x0
11c1d 30 30 30 30 30 38 30 0a 2a 2a 20 20 20 20 30 78  0000080.**    0x
11c1e 38 32 20 30 78 30 30 20 20 20 20 20 20 20 20 20  82 0x00         
11c1f 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20          becomes 
11c20 20 30 78 30 30 30 30 30 31 30 30 0a 2a 2a 20 20   0x00000100.**  
11c21 20 20 30 78 38 30 20 30 78 37 66 20 20 20 20 20    0x80 0x7f     
11c22 20 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f              beco
11c23 6d 65 73 20 20 30 78 30 30 30 30 30 30 37 66 0a  mes  0x0000007f.
11c24 2a 2a 20 20 20 20 30 78 38 61 20 30 78 39 31 20  **    0x8a 0x91 
11c25 30 78 64 31 20 30 78 61 63 20 30 78 37 38 20 20  0xd1 0xac 0x78  
11c26 62 65 63 6f 6d 65 73 20 20 30 78 31 32 33 34 35  becomes  0x12345
11c27 36 37 38 0a 2a 2a 20 20 20 20 30 78 38 31 20 30  678.**    0x81 0
11c28 78 38 31 20 30 78 38 31 20 30 78 38 31 20 30 78  x81 0x81 0x81 0x
11c29 30 31 20 20 62 65 63 6f 6d 65 73 20 20 30 78 31  01  becomes  0x1
11c2a 30 32 30 34 30 38 31 0a 2a 2a 0a 2a 2a 20 56 61  0204081.**.** Va
11c2b 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e  riable length in
11c2c 74 65 67 65 72 73 20 61 72 65 20 75 73 65 64 20  tegers are used 
11c2d 66 6f 72 20 72 6f 77 69 64 73 20 61 6e 64 20 74  for rowids and t
11c2e 6f 20 68 6f 6c 64 20 74 68 65 20 6e 75 6d 62 65  o hold the numbe
11c2f 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 6f 66  r of.** bytes of
11c30 20 6b 65 79 20 61 6e 64 20 64 61 74 61 20 69 6e   key and data in
11c31 20 61 20 62 74 72 65 65 20 63 65 6c 6c 2e 0a 2a   a btree cell..*
11c32 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74  *.** The content
11c33 20 6f 66 20 61 20 63 65 6c 6c 20 6c 6f 6f 6b 73   of a cell looks
11c34 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   like this:.**.*
11c35 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 45 53  *    SIZE    DES
11c36 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20  CRIPTION.**     
11c37 20 34 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62   4     Page numb
11c38 65 72 20 6f 66 20 74 68 65 20 6c 65 66 74 20 63  er of the left c
11c39 68 69 6c 64 2e 20 4f 6d 69 74 74 65 64 20 69 66  hild. Omitted if
11c3a 20 6c 65 61 66 20 66 6c 61 67 20 69 73 20 73 65   leaf flag is se
11c3b 74 2e 0a 2a 2a 20 20 20 20 20 76 61 72 20 20 20  t..**     var   
11c3c 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
11c3d 20 6f 66 20 64 61 74 61 2e 20 4f 6d 69 74 74 65   of data. Omitte
11c3e 64 20 69 66 20 74 68 65 20 7a 65 72 6f 64 61 74  d if the zerodat
11c3f 61 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a  a flag is set..*
11c40 2a 20 20 20 20 20 76 61 72 20 20 20 20 4e 75 6d  *     var    Num
11c41 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
11c42 6b 65 79 2e 20 4f 72 20 74 68 65 20 6b 65 79 20  key. Or the key 
11c43 69 74 73 65 6c 66 20 69 66 20 69 6e 74 6b 65 79  itself if intkey
11c44 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a   flag is set..**
11c45 20 20 20 20 20 20 2a 20 20 20 20 20 50 61 79 6c        *     Payl
11c46 6f 61 64 0a 2a 2a 20 20 20 20 20 20 34 20 20 20  oad.**      4   
11c47 20 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20    First page of 
11c48 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 61  the overflow cha
11c49 69 6e 2e 20 20 4f 6d 69 74 74 65 64 20 69 66 20  in.  Omitted if 
11c4a 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 0a 2a  no overflow.**.*
11c4b 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  * Overflow pages
11c4c 20 66 6f 72 6d 20 61 20 6c 69 6e 6b 65 64 20 6c   form a linked l
11c4d 69 73 74 2e 20 20 45 61 63 68 20 70 61 67 65 20  ist.  Each page 
11c4e 65 78 63 65 70 74 20 74 68 65 20 6c 61 73 74 20  except the last 
11c4f 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a  is completely.**
11c50 20 66 69 6c 6c 65 64 20 77 69 74 68 20 64 61 74   filled with dat
11c51 61 20 28 70 61 67 65 73 69 7a 65 20 2d 20 34 20  a (pagesize - 4 
11c52 62 79 74 65 73 29 2e 20 20 54 68 65 20 6c 61 73  bytes).  The las
11c53 74 20 70 61 67 65 20 63 61 6e 20 68 61 76 65 20  t page can have 
11c54 61 73 20 6c 69 74 74 6c 65 0a 2a 2a 20 61 73 20  as little.** as 
11c55 31 20 62 79 74 65 20 6f 66 20 64 61 74 61 2e 0a  1 byte of data..
11c56 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20  **.**    SIZE   
11c57 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20   DESCRIPTION.** 
11c58 20 20 20 20 20 34 20 20 20 20 20 50 61 67 65 20       4     Page 
11c59 6e 75 6d 62 65 72 20 6f 66 20 6e 65 78 74 20 6f  number of next o
11c5a 76 65 72 66 6c 6f 77 20 70 61 67 65 0a 2a 2a 20  verflow page.** 
11c5b 20 20 20 20 20 2a 20 20 20 20 20 44 61 74 61 0a       *     Data.
11c5c 2a 2a 0a 2a 2a 20 46 72 65 65 6c 69 73 74 20 70  **.** Freelist p
11c5d 61 67 65 73 20 63 6f 6d 65 20 69 6e 20 74 77 6f  ages come in two
11c5e 20 73 75 62 74 79 70 65 73 3a 20 74 72 75 6e 6b   subtypes: trunk
11c5f 20 70 61 67 65 73 20 61 6e 64 20 6c 65 61 66 20   pages and leaf 
11c60 70 61 67 65 73 2e 20 20 54 68 65 0a 2a 2a 20 66  pages.  The.** f
11c61 69 6c 65 20 68 65 61 64 65 72 20 70 6f 69 6e 74  ile header point
11c62 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 69  s to the first i
11c63 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  n a linked list 
11c64 6f 66 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 20  of trunk page.  
11c65 45 61 63 68 20 74 72 75 6e 6b 0a 2a 2a 20 70 61  Each trunk.** pa
11c66 67 65 20 70 6f 69 6e 74 73 20 74 6f 20 6d 75 6c  ge points to mul
11c67 74 69 70 6c 65 20 6c 65 61 66 20 70 61 67 65 73  tiple leaf pages
11c68 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f  .  The content o
11c69 66 20 61 20 6c 65 61 66 20 70 61 67 65 20 69 73  f a leaf page is
11c6a 0a 2a 2a 20 75 6e 73 70 65 63 69 66 69 65 64 2e  .** unspecified.
11c6b 20 20 41 20 74 72 75 6e 6b 20 70 61 67 65 20 6c    A trunk page l
11c6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a  ooks like this:.
11c6d 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20  **.**    SIZE   
11c6e 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20   DESCRIPTION.** 
11c6f 20 20 20 20 20 34 20 20 20 20 20 50 61 67 65 20       4     Page 
11c70 6e 75 6d 62 65 72 20 6f 66 20 6e 65 78 74 20 74  number of next t
11c71 72 75 6e 6b 20 70 61 67 65 0a 2a 2a 20 20 20 20  runk page.**    
11c72 20 20 34 20 20 20 20 20 4e 75 6d 62 65 72 20 6f    4     Number o
11c73 66 20 6c 65 61 66 20 70 6f 69 6e 74 65 72 73 20  f leaf pointers 
11c74 6f 6e 20 74 68 69 73 20 70 61 67 65 0a 2a 2a 20  on this page.** 
11c75 20 20 20 20 20 2a 20 20 20 20 20 7a 65 72 6f 20       *     zero 
11c76 6f 72 20 6d 6f 72 65 20 70 61 67 65 73 20 6e 75  or more pages nu
11c77 6d 62 65 72 73 20 6f 66 20 6c 65 61 76 65 73 0a  mbers of leaves.
11c78 2a 2f 0a 0a 2f 2a 20 52 6f 75 6e 64 20 75 70 20  */../* Round up 
11c79 61 20 6e 75 6d 62 65 72 20 74 6f 20 74 68 65 20  a number to the 
11c7a 6e 65 78 74 20 6c 61 72 67 65 72 20 6d 75 6c 74  next larger mult
11c7b 69 70 6c 65 20 6f 66 20 38 2e 20 20 54 68 69 73  iple of 8.  This
11c7c 20 69 73 20 75 73 65 64 0a 2a 2a 20 74 6f 20 66   is used.** to f
11c7d 6f 72 63 65 20 38 2d 62 79 74 65 20 61 6c 69 67  orce 8-byte alig
11c7e 6e 6d 65 6e 74 20 6f 6e 20 36 34 2d 62 69 74 20  nment on 64-bit 
11c7f 61 72 63 68 69 74 65 63 74 75 72 65 73 2e 0a 2a  architectures..*
11c80 2f 0a 23 64 65 66 69 6e 65 20 52 4f 55 4e 44 38  /.#define ROUND8
11c81 28 78 29 20 20 20 28 28 78 2b 37 29 26 7e 37 29  (x)   ((x+7)&~7)
11c82 0a 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  .../* The follow
11c83 69 6e 67 20 76 61 6c 75 65 20 69 73 20 74 68 65  ing value is the
11c84 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c 20 73 69   maximum cell si
11c85 7a 65 20 61 73 73 75 6d 69 6e 67 20 61 20 6d 61  ze assuming a ma
11c86 78 69 6d 75 6d 20 70 61 67 65 0a 2a 2a 20 73 69  ximum page.** si
11c87 7a 65 20 67 69 76 65 20 61 62 6f 76 65 2e 0a 2a  ze give above..*
11c88 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43 45 4c  /.#define MX_CEL
11c89 4c 5f 53 49 5a 45 28 70 42 74 29 20 20 28 70 42  L_SIZE(pBt)  (pB
11c8a 74 2d 3e 70 61 67 65 53 69 7a 65 2d 38 29 0a 0a  t->pageSize-8)..
11c8b 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e  /* The maximum n
11c8c 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f  umber of cells o
11c8d 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20  n a single page 
11c8e 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  of the database.
11c8f 20 20 54 68 69 73 0a 2a 2a 20 61 73 73 75 6d 65    This.** assume
11c90 73 20 61 20 6d 69 6e 69 6d 75 6d 20 63 65 6c 6c  s a minimum cell
11c91 20 73 69 7a 65 20 6f 66 20 36 20 62 79 74 65 73   size of 6 bytes
11c92 20 20 28 34 20 62 79 74 65 73 20 66 6f 72 20 74    (4 bytes for t
11c93 68 65 20 63 65 6c 6c 20 69 74 73 65 6c 66 0a 2a  he cell itself.*
11c94 2a 20 70 6c 75 73 20 32 20 62 79 74 65 73 20 66  * plus 2 bytes f
11c95 6f 72 20 74 68 65 20 69 6e 64 65 78 20 74 6f 20  or the index to 
11c96 74 68 65 20 63 65 6c 6c 20 69 6e 20 74 68 65 20  the cell in the 
11c97 70 61 67 65 20 68 65 61 64 65 72 29 2e 20 20 53  page header).  S
11c98 75 63 68 0a 2a 2a 20 73 6d 61 6c 6c 20 63 65 6c  uch.** small cel
11c99 6c 73 20 77 69 6c 6c 20 62 65 20 72 61 72 65 2c  ls will be rare,
11c9a 20 62 75 74 20 74 68 65 79 20 61 72 65 20 70 6f   but they are po
11c9b 73 73 69 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69  ssible..*/.#defi
11c9c 6e 65 20 4d 58 5f 43 45 4c 4c 28 70 42 74 29 20  ne MX_CELL(pBt) 
11c9d 28 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2d  ((pBt->pageSize-
11c9e 38 29 2f 36 29 0a 0a 2f 2a 20 46 6f 72 77 61 72  8)/6)../* Forwar
11c9f 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 2a  d declarations *
11ca0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
11ca1 20 4d 65 6d 50 61 67 65 20 4d 65 6d 50 61 67 65   MemPage MemPage
11ca2 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
11ca3 20 42 74 4c 6f 63 6b 20 42 74 4c 6f 63 6b 3b 0a   BtLock BtLock;.
11ca4 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61  ./*.** This is a
11ca5 20 6d 61 67 69 63 20 73 74 72 69 6e 67 20 74 68   magic string th
11ca6 61 74 20 61 70 70 65 61 72 73 20 61 74 20 74 68  at appears at th
11ca7 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65  e beginning of e
11ca8 76 65 72 79 0a 2a 2a 20 53 51 4c 69 74 65 20 64  very.** SQLite d
11ca9 61 74 61 62 61 73 65 20 69 6e 20 6f 72 64 65 72  atabase in order
11caa 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68 65   to identify the
11cab 20 66 69 6c 65 20 61 73 20 61 20 72 65 61 6c 20   file as a real 
11cac 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  database..**.** 
11cad 59 6f 75 20 63 61 6e 20 63 68 61 6e 67 65 20 74  You can change t
11cae 68 69 73 20 76 61 6c 75 65 20 61 74 20 63 6f 6d  his value at com
11caf 70 69 6c 65 2d 74 69 6d 65 20 62 79 20 73 70 65  pile-time by spe
11cb0 63 69 66 79 69 6e 67 20 61 0a 2a 2a 20 2d 44 53  cifying a.** -DS
11cb1 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45  QLITE_FILE_HEADE
11cb2 52 3d 22 2e 2e 2e 22 20 6f 6e 20 74 68 65 20 63  R="..." on the c
11cb3 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 2d  ompiler command-
11cb4 6c 69 6e 65 2e 20 20 54 68 65 0a 2a 2a 20 68 65  line.  The.** he
11cb5 61 64 65 72 20 6d 75 73 74 20 62 65 20 65 78 61  ader must be exa
11cb6 63 74 6c 79 20 31 36 20 62 79 74 65 73 20 69 6e  ctly 16 bytes in
11cb7 63 6c 75 64 69 6e 67 20 74 68 65 20 7a 65 72 6f  cluding the zero
11cb8 2d 74 65 72 6d 69 6e 61 74 6f 72 20 73 6f 0a 2a  -terminator so.*
11cb9 2a 20 74 68 65 20 73 74 72 69 6e 67 20 69 74 73  * the string its
11cba 65 6c 66 20 73 68 6f 75 6c 64 20 62 65 20 31 35  elf should be 15
11cbb 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67   characters long
11cbc 2e 20 20 49 66 20 79 6f 75 20 63 68 61 6e 67 65  .  If you change
11cbd 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72 2c 20  .** the header, 
11cbe 74 68 65 6e 20 79 6f 75 72 20 63 75 73 74 6f 6d  then your custom
11cbf 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 6e 6f   library will no
11cc0 74 20 62 65 20 61 62 6c 65 20 74 6f 20 72 65 61  t be able to rea
11cc1 64 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 73 20  d .** databases 
11cc2 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 65  generated by the
11cc3 20 73 74 61 6e 64 61 72 64 20 74 6f 6f 6c 73 20   standard tools 
11cc4 61 6e 64 20 74 68 65 20 73 74 61 6e 64 61 72 64  and the standard
11cc5 20 74 6f 6f 6c 73 0a 2a 2a 20 77 69 6c 6c 20 6e   tools.** will n
11cc6 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 72 65  ot be able to re
11cc7 61 64 20 64 61 74 61 62 61 73 65 73 20 63 72 65  ad databases cre
11cc8 61 74 65 64 20 62 79 20 79 6f 75 72 20 63 75 73  ated by your cus
11cc9 74 6f 6d 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a  tom library..*/.
11cca 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 46  #ifndef SQLITE_F
11ccb 49 4c 45 5f 48 45 41 44 45 52 20 2f 2a 20 31 32  ILE_HEADER /* 12
11ccc 33 34 35 36 37 38 39 20 31 32 33 34 35 36 20 2a  3456789 123456 *
11ccd 2f 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49  /.#  define SQLI
11cce 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 20 22  TE_FILE_HEADER "
11ccf 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 22  SQLite format 3"
11cd0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50  .#endif../*.** P
11cd1 61 67 65 20 74 79 70 65 20 66 6c 61 67 73 2e 20  age type flags. 
11cd2 20 41 6e 20 4f 52 65 64 20 63 6f 6d 62 69 6e 61   An ORed combina
11cd3 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 66 6c  tion of these fl
11cd4 61 67 73 20 61 70 70 65 61 72 20 61 73 20 74 68  ags appear as th
11cd5 65 0a 2a 2a 20 66 69 72 73 74 20 62 79 74 65 20  e.** first byte 
11cd6 6f 66 20 6f 6e 2d 64 69 73 6b 20 69 6d 61 67 65  of on-disk image
11cd7 20 6f 66 20 65 76 65 72 79 20 42 54 72 65 65 20   of every BTree 
11cd8 70 61 67 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  page..*/.#define
11cd9 20 50 54 46 5f 49 4e 54 4b 45 59 20 20 20 20 30   PTF_INTKEY    0
11cda 78 30 31 0a 23 64 65 66 69 6e 65 20 50 54 46 5f  x01.#define PTF_
11cdb 5a 45 52 4f 44 41 54 41 20 20 30 78 30 32 0a 23  ZERODATA  0x02.#
11cdc 64 65 66 69 6e 65 20 50 54 46 5f 4c 45 41 46 44  define PTF_LEAFD
11cdd 41 54 41 20 20 30 78 30 34 0a 23 64 65 66 69 6e  ATA  0x04.#defin
11cde 65 20 50 54 46 5f 4c 45 41 46 20 20 20 20 20 20  e PTF_LEAF      
11cdf 30 78 30 38 0a 0a 2f 2a 0a 2a 2a 20 41 73 20 65  0x08../*.** As e
11ce0 61 63 68 20 70 61 67 65 20 6f 66 20 74 68 65 20  ach page of the 
11ce1 66 69 6c 65 20 69 73 20 6c 6f 61 64 65 64 20 69  file is loaded i
11ce2 6e 74 6f 20 6d 65 6d 6f 72 79 2c 20 61 6e 20 69  nto memory, an i
11ce3 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
11ce4 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75  ollowing.** stru
11ce5 63 74 75 72 65 20 69 73 20 61 70 70 65 6e 64 65  cture is appende
11ce6 64 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65  d and initialize
11ce7 64 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 69 73  d to zero.  This
11ce8 20 73 74 72 75 63 74 75 72 65 20 73 74 6f 72 65   structure store
11ce9 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  s.** information
11cea 20 61 62 6f 75 74 20 74 68 65 20 70 61 67 65 20   about the page 
11ceb 74 68 61 74 20 69 73 20 64 65 63 6f 64 65 64 20  that is decoded 
11cec 66 72 6f 6d 20 74 68 65 20 72 61 77 20 66 69 6c  from the raw fil
11ced 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  e page..**.** Th
11cee 65 20 70 50 61 72 65 6e 74 20 66 69 65 6c 64 20  e pParent field 
11cef 70 6f 69 6e 74 73 20 62 61 63 6b 20 74 6f 20 74  points back to t
11cf0 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20  he parent page. 
11cf1 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 75 73 20   This allows us 
11cf2 74 6f 0a 2a 2a 20 77 61 6c 6b 20 75 70 20 74 68  to.** walk up th
11cf3 65 20 42 54 72 65 65 20 66 72 6f 6d 20 61 6e 79  e BTree from any
11cf4 20 6c 65 61 66 20 74 6f 20 74 68 65 20 72 6f 6f   leaf to the roo
11cf5 74 2e 20 20 43 61 72 65 20 6d 75 73 74 20 62 65  t.  Care must be
11cf6 20 74 61 6b 65 6e 20 74 6f 0a 2a 2a 20 75 6e 72   taken to.** unr
11cf7 65 66 28 29 20 74 68 65 20 70 61 72 65 6e 74 20  ef() the parent 
11cf8 70 61 67 65 20 70 6f 69 6e 74 65 72 20 77 68 65  page pointer whe
11cf9 6e 20 74 68 69 73 20 70 61 67 65 20 69 73 20 6e  n this page is n
11cfa 6f 20 6c 6f 6e 67 65 72 20 72 65 66 65 72 65 6e  o longer referen
11cfb 63 65 64 2e 0a 2a 2a 20 54 68 65 20 70 61 67 65  ced..** The page
11cfc 44 65 73 74 72 75 63 74 6f 72 28 29 20 72 6f 75  Destructor() rou
11cfd 74 69 6e 65 20 68 61 6e 64 6c 65 73 20 74 68 61  tine handles tha
11cfe 74 20 63 68 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 41  t chore..**.** A
11cff 63 63 65 73 73 20 74 6f 20 61 6c 6c 20 66 69 65  ccess to all fie
11d00 6c 64 73 20 6f 66 20 74 68 69 73 20 73 74 72 75  lds of this stru
11d01 63 74 75 72 65 20 69 73 20 63 6f 6e 74 72 6f 6c  cture is control
11d02 6c 65 64 20 62 79 20 74 68 65 20 6d 75 74 65 78  led by the mutex
11d03 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 4d 65  .** stored in Me
11d04 6d 50 61 67 65 2e 70 42 74 2d 3e 6d 75 74 65 78  mPage.pBt->mutex
11d05 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 50  ..*/.struct MemP
11d06 61 67 65 20 7b 0a 20 20 75 38 20 69 73 49 6e 69  age {.  u8 isIni
11d07 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t;           /* 
11d08 54 72 75 65 20 69 66 20 70 72 65 76 69 6f 75 73  True if previous
11d09 6c 79 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20  ly initialized. 
11d0a 4d 55 53 54 20 42 45 20 46 49 52 53 54 21 20 2a  MUST BE FIRST! *
11d0b 2f 0a 20 20 75 38 20 6e 4f 76 65 72 66 6c 6f 77  /.  u8 nOverflow
11d0c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ;        /* Numb
11d0d 65 72 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 63  er of overflow c
11d0e 65 6c 6c 20 62 6f 64 69 65 73 20 69 6e 20 61 43  ell bodies in aC
11d0f 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 69 6e  ell[] */.  u8 in
11d10 74 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20  tKey;           
11d11 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 74 6b 65  /* True if intke
11d12 79 20 66 6c 61 67 20 69 73 20 73 65 74 20 2a 2f  y flag is set */
11d13 0a 20 20 75 38 20 6c 65 61 66 3b 20 20 20 20 20  .  u8 leaf;     
11d14 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
11d15 69 66 20 6c 65 61 66 20 66 6c 61 67 20 69 73 20  if leaf flag is 
11d16 73 65 74 20 2a 2f 0a 20 20 75 38 20 68 61 73 44  set */.  u8 hasD
11d17 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ata;          /*
11d18 20 54 72 75 65 20 69 66 20 74 68 69 73 20 70 61   True if this pa
11d19 67 65 20 73 74 6f 72 65 73 20 64 61 74 61 20 2a  ge stores data *
11d1a 2f 0a 20 20 75 38 20 68 64 72 4f 66 66 73 65 74  /.  u8 hdrOffset
11d1b 3b 20 20 20 20 20 20 20 20 2f 2a 20 31 30 30 20  ;        /* 100 
11d1c 66 6f 72 20 70 61 67 65 20 31 2e 20 20 30 20 6f  for page 1.  0 o
11d1d 74 68 65 72 77 69 73 65 20 2a 2f 0a 20 20 75 38  therwise */.  u8
11d1e 20 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 20 20   childPtrSize;  
11d1f 20 20 20 2f 2a 20 30 20 69 66 20 6c 65 61 66 3d     /* 0 if leaf=
11d20 3d 31 2e 20 20 34 20 69 66 20 6c 65 61 66 3d 3d  =1.  4 if leaf==
11d21 30 20 2a 2f 0a 20 20 75 31 36 20 6d 61 78 4c 6f  0 */.  u16 maxLo
11d22 63 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 43  cal;        /* C
11d23 6f 70 79 20 6f 66 20 42 74 53 68 61 72 65 64 2e  opy of BtShared.
11d24 6d 61 78 4c 6f 63 61 6c 20 6f 72 20 42 74 53 68  maxLocal or BtSh
11d25 61 72 65 64 2e 6d 61 78 4c 65 61 66 20 2a 2f 0a  ared.maxLeaf */.
11d26 20 20 75 31 36 20 6d 69 6e 4c 6f 63 61 6c 3b 20    u16 minLocal; 
11d27 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f         /* Copy o
11d28 66 20 42 74 53 68 61 72 65 64 2e 6d 69 6e 4c 6f  f BtShared.minLo
11d29 63 61 6c 20 6f 72 20 42 74 53 68 61 72 65 64 2e  cal or BtShared.
11d2a 6d 69 6e 4c 65 61 66 20 2a 2f 0a 20 20 75 31 36  minLeaf */.  u16
11d2b 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 20   cellOffset;    
11d2c 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 61 44    /* Index in aD
11d2d 61 74 61 20 6f 66 20 66 69 72 73 74 20 63 65 6c  ata of first cel
11d2e 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 75  l pointer */.  u
11d2f 31 36 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20  16 nFree;       
11d30 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
11d31 20 66 72 65 65 20 62 79 74 65 73 20 6f 6e 20 74   free bytes on t
11d32 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 31 36  he page */.  u16
11d33 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20   nCell;         
11d34 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
11d35 65 6c 6c 73 20 6f 6e 20 74 68 69 73 20 70 61 67  ells on this pag
11d36 65 2c 20 6c 6f 63 61 6c 20 61 6e 64 20 6f 76 66  e, local and ovf
11d37 6c 20 2a 2f 0a 20 20 75 31 36 20 6d 61 73 6b 50  l */.  u16 maskP
11d38 61 67 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d  age;        /* M
11d39 61 73 6b 20 66 6f 72 20 70 61 67 65 20 6f 66 66  ask for page off
11d3a 73 65 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  set */.  struct 
11d3b 5f 4f 76 66 6c 43 65 6c 6c 20 7b 20 20 20 2f 2a  _OvflCell {   /*
11d3c 20 43 65 6c 6c 73 20 74 68 61 74 20 77 69 6c 6c   Cells that will
11d3d 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61 44 61 74   not fit on aDat
11d3e 61 5b 5d 20 2a 2f 0a 20 20 20 20 75 38 20 2a 70  a[] */.    u8 *p
11d3f 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 2f  Cell;          /
11d40 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68  * Pointers to th
11d41 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 6f 76  e body of the ov
11d42 65 72 66 6c 6f 77 20 63 65 6c 6c 20 2a 2f 0a 20  erflow cell */. 
11d43 20 20 20 75 31 36 20 69 64 78 3b 20 20 20 20 20     u16 idx;     
11d44 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74         /* Insert
11d45 20 74 68 69 73 20 63 65 6c 6c 20 62 65 66 6f 72   this cell befor
11d46 65 20 69 64 78 2d 74 68 20 6e 6f 6e 2d 6f 76 65  e idx-th non-ove
11d47 72 66 6c 6f 77 20 63 65 6c 6c 20 2a 2f 0a 20 20  rflow cell */.  
11d48 7d 20 61 4f 76 66 6c 5b 35 5d 3b 0a 20 20 42 74  } aOvfl[5];.  Bt
11d49 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20  Shared *pBt;    
11d4a 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
11d4b 20 42 74 53 68 61 72 65 64 20 74 68 61 74 20 74   BtShared that t
11d4c 68 69 73 20 70 61 67 65 20 69 73 20 70 61 72 74  his page is part
11d4d 20 6f 66 20 2a 2f 0a 20 20 75 38 20 2a 61 44 61   of */.  u8 *aDa
11d4e 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ta;           /*
11d4f 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 69 73 6b   Pointer to disk
11d50 20 69 6d 61 67 65 20 6f 66 20 74 68 65 20 70 61   image of the pa
11d51 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 44 62 50  ge data */.  DbP
11d52 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20 20 20  age *pDbPage;   
11d53 20 20 2f 2a 20 50 61 67 65 72 20 70 61 67 65 20    /* Pager page 
11d54 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f  handle */.  Pgno
11d55 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20   pgno;          
11d56 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20   /* Page number 
11d57 66 6f 72 20 74 68 69 73 20 70 61 67 65 20 2a 2f  for this page */
11d58 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69  .};../*.** The i
11d59 6e 2d 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20 6f  n-memory image o
11d5a 66 20 61 20 64 69 73 6b 20 70 61 67 65 20 68 61  f a disk page ha
11d5b 73 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20  s the auxiliary 
11d5c 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 70 70 65  information appe
11d5d 6e 64 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 65  nded.** to the e
11d5e 6e 64 2e 20 20 45 58 54 52 41 5f 53 49 5a 45 20  nd.  EXTRA_SIZE 
11d5f 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
11d60 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20   bytes of space 
11d61 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 0a 2a  needed to hold.*
11d62 2a 20 74 68 61 74 20 65 78 74 72 61 20 69 6e 66  * that extra inf
11d63 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65  ormation..*/.#de
11d64 66 69 6e 65 20 45 58 54 52 41 5f 53 49 5a 45 20  fine EXTRA_SIZE 
11d65 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 0a  sizeof(MemPage).
11d66 0a 2f 2a 20 41 20 42 74 72 65 65 20 68 61 6e 64  ./* A Btree hand
11d67 6c 65 0a 2a 2a 0a 2a 2a 20 41 20 64 61 74 61 62  le.**.** A datab
11d68 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63  ase connection c
11d69 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65  ontains a pointe
11d6a 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65  r to an instance
11d6b 20 6f 66 0a 2a 2a 20 74 68 69 73 20 6f 62 6a 65   of.** this obje
11d6c 63 74 20 66 6f 72 20 65 76 65 72 79 20 64 61 74  ct for every dat
11d6d 61 62 61 73 65 20 66 69 6c 65 20 74 68 61 74 20  abase file that 
11d6e 69 74 20 68 61 73 20 6f 70 65 6e 2e 20 20 54 68  it has open.  Th
11d6f 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  is structure.** 
11d70 69 73 20 6f 70 61 71 75 65 20 74 6f 20 74 68 65  is opaque to the
11d71 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
11d72 74 69 6f 6e 2e 20 20 54 68 65 20 64 61 74 61 62  tion.  The datab
11d73 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63  ase connection c
11d74 61 6e 6e 6f 74 0a 2a 2a 20 73 65 65 20 74 68 65  annot.** see the
11d75 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66 20 74 68   internals of th
11d76 69 73 20 73 74 72 75 63 74 75 72 65 20 61 6e 64  is structure and
11d77 20 6f 6e 6c 79 20 64 65 61 6c 73 20 77 69 74 68   only deals with
11d78 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20   pointers to.** 
11d79 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a  this structure..
11d7a 2a 2a 0a 2a 2a 20 46 6f 72 20 73 6f 6d 65 20 64  **.** For some d
11d7b 61 74 61 62 61 73 65 20 66 69 6c 65 73 2c 20 74  atabase files, t
11d7c 68 65 20 73 61 6d 65 20 75 6e 64 65 72 6c 79 69  he same underlyi
11d7d 6e 67 20 64 61 74 61 62 61 73 65 20 63 61 63 68  ng database cach
11d7e 65 20 6d 69 67 68 74 20 62 65 20 0a 2a 2a 20 73  e might be .** s
11d7f 68 61 72 65 64 20 62 65 74 77 65 65 6e 20 6d 75  hared between mu
11d80 6c 74 69 70 6c 65 20 63 6f 6e 6e 65 63 74 69 6f  ltiple connectio
11d81 6e 73 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73  ns.  In that cas
11d82 65 2c 20 65 61 63 68 20 63 6f 6e 74 65 63 74 69  e, each contecti
11d83 6f 6e 0a 2a 2a 20 68 61 73 20 69 74 20 6f 77 6e  on.** has it own
11d84 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73   pointer to this
11d85 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 20 65 61   object.  But ea
11d86 63 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  ch instance of t
11d87 68 69 73 20 6f 62 6a 65 63 74 0a 2a 2a 20 70 6f  his object.** po
11d88 69 6e 74 73 20 74 6f 20 74 68 65 20 73 61 6d 65  ints to the same
11d89 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74   BtShared object
11d8a 2e 20 20 54 68 65 20 64 61 74 61 62 61 73 65 20  .  The database 
11d8b 63 61 63 68 65 20 61 6e 64 20 74 68 65 0a 2a 2a  cache and the.**
11d8c 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74   schema associat
11d8d 65 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61  ed with the data
11d8e 62 61 73 65 20 66 69 6c 65 20 61 72 65 20 61 6c  base file are al
11d8f 6c 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68  l contained with
11d90 69 6e 0a 2a 2a 20 74 68 65 20 42 74 53 68 61 72  in.** the BtShar
11d91 65 64 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a  ed object..**.**
11d92 20 41 6c 6c 20 66 69 65 6c 64 73 20 69 6e 20 74   All fields in t
11d93 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72  his structure ar
11d94 65 20 61 63 63 65 73 73 65 64 20 75 6e 64 65 72  e accessed under
11d95 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e 0a   sqlite3.mutex..
11d96 2a 2a 20 54 68 65 20 70 42 74 20 70 6f 69 6e 74  ** The pBt point
11d97 65 72 20 69 74 73 65 6c 66 20 6d 61 79 20 6e 6f  er itself may no
11d98 74 20 62 65 20 63 68 61 6e 67 65 64 20 77 68 69  t be changed whi
11d99 6c 65 20 74 68 65 72 65 20 65 78 69 73 74 73 20  le there exists 
11d9a 63 75 72 73 6f 72 73 20 0a 2a 2a 20 69 6e 20 74  cursors .** in t
11d9b 68 65 20 72 65 66 65 72 65 6e 63 65 64 20 42 74  he referenced Bt
11d9c 53 68 61 72 65 64 20 74 68 61 74 20 70 6f 69 6e  Shared that poin
11d9d 74 20 62 61 63 6b 20 74 6f 20 74 68 69 73 20 42  t back to this B
11d9e 74 72 65 65 20 73 69 6e 63 65 20 74 68 6f 73 65  tree since those
11d9f 0a 2a 2a 20 63 75 72 73 6f 72 73 20 68 61 76 65  .** cursors have
11da0 20 74 6f 20 64 6f 20 67 6f 20 74 68 72 6f 75 67   to do go throug
11da1 68 20 74 68 69 73 20 42 74 72 65 65 20 74 6f 20  h this Btree to 
11da2 66 69 6e 64 20 74 68 65 69 72 20 42 74 53 68 61  find their BtSha
11da3 72 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 79 20  red and.** they 
11da4 6f 66 74 65 6e 20 64 6f 20 73 6f 20 77 69 74 68  often do so with
11da5 6f 75 74 20 68 6f 6c 64 69 6e 67 20 73 71 6c 69  out holding sqli
11da6 74 65 33 2e 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74  te3.mutex..*/.st
11da7 72 75 63 74 20 42 74 72 65 65 20 7b 0a 20 20 73  ruct Btree {.  s
11da8 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
11da9 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
11daa 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 6f 6c  e connection hol
11dab 64 69 6e 67 20 74 68 69 73 20 62 74 72 65 65 20  ding this btree 
11dac 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  */.  BtShared *p
11dad 42 74 3b 20 20 20 20 20 2f 2a 20 53 68 61 72 61  Bt;     /* Shara
11dae 62 6c 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74  ble content of t
11daf 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 75  his btree */.  u
11db0 38 20 69 6e 54 72 61 6e 73 3b 20 20 20 20 20 20  8 inTrans;      
11db1 20 20 2f 2a 20 54 52 41 4e 53 5f 4e 4f 4e 45 2c    /* TRANS_NONE,
11db2 20 54 52 41 4e 53 5f 52 45 41 44 20 6f 72 20 54   TRANS_READ or T
11db3 52 41 4e 53 5f 57 52 49 54 45 20 2a 2f 0a 20 20  RANS_WRITE */.  
11db4 75 38 20 73 68 61 72 61 62 6c 65 3b 20 20 20 20  u8 sharable;    
11db5 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65     /* True if we
11db6 20 63 61 6e 20 73 68 61 72 65 20 70 42 74 20 77   can share pBt w
11db7 69 74 68 20 61 6e 6f 74 68 65 72 20 64 62 20 2a  ith another db *
11db8 2f 0a 20 20 75 38 20 6c 6f 63 6b 65 64 3b 20 20  /.  u8 locked;  
11db9 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
11dba 66 20 64 62 20 63 75 72 72 65 6e 74 6c 79 20 68  f db currently h
11dbb 61 73 20 70 42 74 20 6c 6f 63 6b 65 64 20 2a 2f  as pBt locked */
11dbc 0a 20 20 69 6e 74 20 77 61 6e 74 54 6f 4c 6f 63  .  int wantToLoc
11dbd 6b 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  k;    /* Number 
11dbe 6f 66 20 6e 65 73 74 65 64 20 63 61 6c 6c 73 20  of nested calls 
11dbf 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  to sqlite3BtreeE
11dc0 6e 74 65 72 28 29 20 2a 2f 0a 20 20 42 74 72 65  nter() */.  Btre
11dc1 65 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f  e *pNext;      /
11dc2 2a 20 4c 69 73 74 20 6f 66 20 6f 74 68 65 72 20  * List of other 
11dc3 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73 20  sharable Btrees 
11dc4 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 64 62  from the same db
11dc5 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 50 72   */.  Btree *pPr
11dc6 65 76 3b 20 20 20 20 20 20 2f 2a 20 42 61 63 6b  ev;      /* Back
11dc7 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20   pointer of the 
11dc8 73 61 6d 65 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a  same list */.};.
11dc9 0a 2f 2a 0a 2a 2a 20 42 74 72 65 65 2e 69 6e 54  ./*.** Btree.inT
11dca 72 61 6e 73 20 6d 61 79 20 74 61 6b 65 20 6f 6e  rans may take on
11dcb 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
11dcc 6e 67 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a  ng values..**.**
11dcd 20 49 66 20 74 68 65 20 73 68 61 72 65 64 2d 64   If the shared-d
11dce 61 74 61 20 65 78 74 65 6e 73 69 6f 6e 20 69 73  ata extension is
11dcf 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 72 65 20   enabled, there 
11dd0 6d 61 79 20 62 65 20 6d 75 6c 74 69 70 6c 65 20  may be multiple 
11dd1 75 73 65 72 73 0a 2a 2a 20 6f 66 20 74 68 65 20  users.** of the 
11dd2 42 74 72 65 65 20 73 74 72 75 63 74 75 72 65 2e  Btree structure.
11dd3 20 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f 66 20   At most one of 
11dd4 74 68 65 73 65 20 6d 61 79 20 6f 70 65 6e 20 61  these may open a
11dd5 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
11dd6 6f 6e 2c 0a 2a 2a 20 62 75 74 20 61 6e 79 20 6e  on,.** but any n
11dd7 75 6d 62 65 72 20 6d 61 79 20 68 61 76 65 20 61  umber may have a
11dd8 63 74 69 76 65 20 72 65 61 64 20 74 72 61 6e 73  ctive read trans
11dd9 61 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 66  actions..*/.#def
11dda 69 6e 65 20 54 52 41 4e 53 5f 4e 4f 4e 45 20 20  ine TRANS_NONE  
11ddb 30 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53 5f  0.#define TRANS_
11ddc 52 45 41 44 20 20 31 0a 23 64 65 66 69 6e 65 20  READ  1.#define 
11ddd 54 52 41 4e 53 5f 57 52 49 54 45 20 32 0a 0a 2f  TRANS_WRITE 2../
11dde 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
11ddf 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20   of this object 
11de0 72 65 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e  represents a sin
11de1 67 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  gle database fil
11de2 65 2e 0a 2a 2a 20 0a 2a 2a 20 41 20 73 69 6e 67  e..** .** A sing
11de3 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  le database file
11de4 20 63 61 6e 20 62 65 20 69 6e 20 75 73 65 20 61   can be in use a
11de5 73 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20  s the same time 
11de6 62 79 20 74 77 6f 0a 2a 2a 20 6f 72 20 6d 6f 72  by two.** or mor
11de7 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
11de8 63 74 69 6f 6e 73 2e 20 20 57 68 65 6e 20 74 77  ctions.  When tw
11de9 6f 20 6f 72 20 6d 6f 72 65 20 63 6f 6e 6e 65 63  o or more connec
11dea 74 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 73 68 61  tions are.** sha
11deb 72 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61  ring the same da
11dec 74 61 62 61 73 65 20 66 69 6c 65 2c 20 65 61 63  tabase file, eac
11ded 68 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73  h connection has
11dee 20 69 74 20 6f 77 6e 0a 2a 2a 20 70 72 69 76 61   it own.** priva
11def 74 65 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20  te Btree object 
11df0 66 6f 72 20 74 68 65 20 66 69 6c 65 20 61 6e 64  for the file and
11df1 20 65 61 63 68 20 6f 66 20 74 68 6f 73 65 20 42   each of those B
11df2 74 72 65 65 73 20 70 6f 69 6e 74 73 0a 2a 2a 20  trees points.** 
11df3 74 6f 20 74 68 69 73 20 6f 6e 65 20 42 74 53 68  to this one BtSh
11df4 61 72 65 64 20 6f 62 6a 65 63 74 2e 20 20 42 74  ared object.  Bt
11df5 53 68 61 72 65 64 2e 6e 52 65 66 20 69 73 20 74  Shared.nRef is t
11df6 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20  he number of.** 
11df7 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 75 72 72  connections curr
11df8 65 6e 74 6c 79 20 73 68 61 72 69 6e 67 20 74 68  ently sharing th
11df9 69 73 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  is database file
11dfa 2e 0a 2a 2a 0a 2a 2a 20 46 69 65 6c 64 73 20 69  ..**.** Fields i
11dfb 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  n this structure
11dfc 20 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e   are accessed un
11dfd 64 65 72 20 74 68 65 20 42 74 53 68 61 72 65 64  der the BtShared
11dfe 2e 6d 75 74 65 78 0a 2a 2a 20 6d 75 74 65 78 2c  .mutex.** mutex,
11dff 20 65 78 63 65 70 74 20 66 6f 72 20 6e 52 65 66   except for nRef
11e00 20 61 6e 64 20 70 4e 65 78 74 20 77 68 69 63 68   and pNext which
11e01 20 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e   are accessed un
11e02 64 65 72 20 74 68 65 0a 2a 2a 20 67 6c 6f 62 61  der the.** globa
11e03 6c 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  l SQLITE_MUTEX_S
11e04 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74  TATIC_MASTER mut
11e05 65 78 2e 20 20 54 68 65 20 70 50 61 67 65 72 20  ex.  The pPager 
11e06 66 69 65 6c 64 0a 2a 2a 20 6d 61 79 20 6e 6f 74  field.** may not
11e07 20 62 65 20 6d 6f 64 69 66 69 65 64 20 6f 6e 63   be modified onc
11e08 65 20 69 74 20 69 73 20 69 6e 69 74 69 61 6c 6c  e it is initiall
11e09 79 20 73 65 74 20 61 73 20 6c 6f 6e 67 20 61 73  y set as long as
11e0a 20 6e 52 65 66 3e 30 2e 0a 2a 2a 20 54 68 65 20   nRef>0..** The 
11e0b 70 53 63 68 65 6d 61 20 66 69 65 6c 64 20 6d 61  pSchema field ma
11e0c 79 20 62 65 20 73 65 74 20 6f 6e 63 65 20 75 6e  y be set once un
11e0d 64 65 72 20 42 74 53 68 61 72 65 64 2e 6d 75 74  der BtShared.mut
11e0e 65 78 20 61 6e 64 0a 2a 2a 20 74 68 65 72 65 61  ex and.** therea
11e0f 66 74 65 72 20 69 73 20 75 6e 63 68 61 6e 67 65  fter is unchange
11e10 64 20 61 73 20 6c 6f 6e 67 20 61 73 20 6e 52 65  d as long as nRe
11e11 66 3e 30 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42  f>0..*/.struct B
11e12 74 53 68 61 72 65 64 20 7b 0a 20 20 50 61 67 65  tShared {.  Page
11e13 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20  r *pPager;      
11e14 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 63 61    /* The page ca
11e15 63 68 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  che */.  sqlite3
11e16 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 2f   *db;          /
11e17 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
11e18 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20  ction currently 
11e19 75 73 69 6e 67 20 74 68 69 73 20 42 74 72 65 65  using this Btree
11e1a 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a   */.  BtCursor *
11e1b 70 43 75 72 73 6f 72 3b 20 20 20 20 2f 2a 20 41  pCursor;    /* A
11e1c 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 6f 70 65   list of all ope
11e1d 6e 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 4d  n cursors */.  M
11e1e 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b 20  emPage *pPage1; 
11e1f 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61       /* First pa
11e20 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ge of the databa
11e21 73 65 20 2a 2f 0a 20 20 75 38 20 69 6e 53 74 6d  se */.  u8 inStm
11e22 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
11e23 20 54 72 75 65 20 69 66 20 77 65 20 61 72 65 20   True if we are 
11e24 69 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 73  in a statement s
11e25 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f  ubtransaction */
11e26 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20  .  u8 readOnly; 
11e27 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
11e28 20 69 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69   if the underlyi
11e29 6e 67 20 66 69 6c 65 20 69 73 20 72 65 61 64 6f  ng file is reado
11e2a 6e 6c 79 20 2a 2f 0a 20 20 75 38 20 70 61 67 65  nly */.  u8 page
11e2b 53 69 7a 65 46 69 78 65 64 3b 20 20 20 20 20 2f  SizeFixed;     /
11e2c 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 70 61  * True if the pa
11e2d 67 65 20 73 69 7a 65 20 63 61 6e 20 6e 6f 20 6c  ge size can no l
11e2e 6f 6e 67 65 72 20 62 65 20 63 68 61 6e 67 65 64  onger be changed
11e2f 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
11e30 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
11e31 55 4d 0a 20 20 75 38 20 61 75 74 6f 56 61 63 75  UM.  u8 autoVacu
11e32 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72  um;        /* Tr
11e33 75 65 20 69 66 20 61 75 74 6f 2d 76 61 63 75 75  ue if auto-vacuu
11e34 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a  m is enabled */.
11e35 20 20 75 38 20 69 6e 63 72 56 61 63 75 75 6d 3b    u8 incrVacuum;
11e36 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
11e37 69 66 20 69 6e 63 72 2d 76 61 63 75 75 6d 20 69  if incr-vacuum i
11e38 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 50  s enabled */.  P
11e39 67 6e 6f 20 6e 54 72 75 6e 63 3b 20 20 20 20 20  gno nTrunc;     
11e3a 20 20 20 20 20 2f 2a 20 4e 6f 6e 2d 7a 65 72 6f       /* Non-zero
11e3b 20 69 66 20 74 68 65 20 64 62 20 77 69 6c 6c 20   if the db will 
11e3c 62 65 20 74 72 75 6e 63 61 74 65 64 20 28 69 6e  be truncated (in
11e3d 63 72 20 76 61 63 75 75 6d 29 20 2a 2f 0a 23 65  cr vacuum) */.#e
11e3e 6e 64 69 66 0a 20 20 75 31 36 20 70 61 67 65 53  ndif.  u16 pageS
11e3f 69 7a 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ize;         /* 
11e40 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  Total number of 
11e41 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67 65 20  bytes on a page 
11e42 2a 2f 0a 20 20 75 31 36 20 75 73 61 62 6c 65 53  */.  u16 usableS
11e43 69 7a 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75  ize;       /* Nu
11e44 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 20 62  mber of usable b
11e45 79 74 65 73 20 6f 6e 20 65 61 63 68 20 70 61 67  ytes on each pag
11e46 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 61 78 4c 6f  e */.  int maxLo
11e47 63 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  cal;         /* 
11e48 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61  Maximum local pa
11e49 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41  yload in non-LEA
11e4a 46 44 41 54 41 20 74 61 62 6c 65 73 20 2a 2f 0a  FDATA tables */.
11e4b 20 20 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 3b 20    int minLocal; 
11e4c 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d          /* Minim
11e4d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64  um local payload
11e4e 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46 44 41 54 41   in non-LEAFDATA
11e4f 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 69 6e 74   tables */.  int
11e50 20 6d 61 78 4c 65 61 66 3b 20 20 20 20 20 20 20   maxLeaf;       
11e51 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 6f     /* Maximum lo
11e52 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 61  cal payload in a
11e53 20 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65 20   LEAFDATA table 
11e54 2a 2f 0a 20 20 69 6e 74 20 6d 69 6e 4c 65 61 66  */.  int minLeaf
11e55 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69  ;          /* Mi
11e56 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c  nimum local payl
11e57 6f 61 64 20 69 6e 20 61 20 4c 45 41 46 44 41 54  oad in a LEAFDAT
11e58 41 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20  A table */.  u8 
11e59 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b 20 20  inTransaction;  
11e5a 20 20 20 2f 2a 20 54 72 61 6e 73 61 63 74 69 6f     /* Transactio
11e5b 6e 20 73 74 61 74 65 20 2a 2f 0a 20 20 69 6e 74  n state */.  int
11e5c 20 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b 20 20   nTransaction;  
11e5d 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
11e5e 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e  open transaction
11e5f 73 20 28 72 65 61 64 20 2b 20 77 72 69 74 65 29  s (read + write)
11e60 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 63 68   */.  void *pSch
11e61 65 6d 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 50  ema;        /* P
11e62 6f 69 6e 74 65 72 20 74 6f 20 73 70 61 63 65 20  ointer to space 
11e63 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c  allocated by sql
11e64 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 28  ite3BtreeSchema(
11e65 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46  ) */.  void (*xF
11e66 72 65 65 53 63 68 65 6d 61 29 28 76 6f 69 64 2a  reeSchema)(void*
11e67 29 3b 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f  );  /* Destructo
11e68 72 20 66 6f 72 20 42 74 53 68 61 72 65 64 2e 70  r for BtShared.p
11e69 53 63 68 65 6d 61 20 2a 2f 0a 20 20 73 71 6c 69  Schema */.  sqli
11e6a 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
11e6b 3b 20 2f 2a 20 4e 6f 6e 2d 72 65 63 75 72 73 69  ; /* Non-recursi
11e6c 76 65 20 6d 75 74 65 78 20 72 65 71 75 69 72 65  ve mutex require
11e6d 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 69 73  d to access this
11e6e 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 42 75 73   struct */.  Bus
11e6f 79 48 61 6e 64 6c 65 72 20 62 75 73 79 48 64 72  yHandler busyHdr
11e70 3b 20 20 2f 2a 20 54 68 65 20 62 75 73 79 20 68  ;  /* The busy h
11e71 61 6e 64 6c 65 72 20 66 6f 72 20 74 68 69 73 20  andler for this 
11e72 62 74 72 65 65 20 2a 2f 0a 23 69 66 6e 64 65 66  btree */.#ifndef
11e73 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
11e74 52 45 44 5f 43 41 43 48 45 0a 20 20 69 6e 74 20  RED_CACHE.  int 
11e75 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20  nRef;           
11e76 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
11e77 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 69  eferences to thi
11e78 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  s structure */. 
11e79 20 42 74 53 68 61 72 65 64 20 2a 70 4e 65 78 74   BtShared *pNext
11e7a 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 6f  ;      /* Next o
11e7b 6e 20 61 20 6c 69 73 74 20 6f 66 20 73 68 61 72  n a list of shar
11e7c 61 62 6c 65 20 42 74 53 68 61 72 65 64 20 73 74  able BtShared st
11e7d 72 75 63 74 73 20 2a 2f 0a 20 20 42 74 4c 6f 63  ructs */.  BtLoc
11e7e 6b 20 2a 70 4c 6f 63 6b 3b 20 20 20 20 20 20 20  k *pLock;       
11e7f 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6c 6f 63 6b   /* List of lock
11e80 73 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 73  s held on this s
11e81 68 61 72 65 64 2d 62 74 72 65 65 20 73 74 72 75  hared-btree stru
11e82 63 74 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70  ct */.  Btree *p
11e83 45 78 63 6c 75 73 69 76 65 3b 20 20 20 20 2f 2a  Exclusive;    /*
11e84 20 42 74 72 65 65 20 77 69 74 68 20 61 6e 20 45   Btree with an E
11e85 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e  XCLUSIVE lock on
11e86 20 74 68 65 20 77 68 6f 6c 65 20 64 62 20 2a 2f   the whole db */
11e87 0a 23 65 6e 64 69 66 0a 20 20 75 38 20 2a 70 54  .#endif.  u8 *pT
11e88 6d 70 53 70 61 63 65 3b 20 20 20 20 20 20 20 20  mpSpace;        
11e89 2f 2a 20 42 74 53 68 61 72 65 64 2e 70 61 67 65  /* BtShared.page
11e8a 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 73 70  Size bytes of sp
11e8b 61 63 65 20 66 6f 72 20 74 6d 70 20 75 73 65 20  ace for tmp use 
11e8c 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  */.};../*.** An 
11e8d 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
11e8e 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
11e8f 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 68  ure is used to h
11e90 6f 6c 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a  old information.
11e91 2a 2a 20 61 62 6f 75 74 20 61 20 63 65 6c 6c 2e  ** about a cell.
11e92 20 20 54 68 65 20 70 61 72 73 65 43 65 6c 6c 50    The parseCellP
11e93 74 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 69  tr() function fi
11e94 6c 6c 73 20 69 6e 20 74 68 69 73 20 73 74 72 75  lls in this stru
11e95 63 74 75 72 65 0a 2a 2a 20 62 61 73 65 64 20 6f  cture.** based o
11e96 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 65 78  n information ex
11e97 74 72 61 63 74 20 66 72 6f 6d 20 74 68 65 20 72  tract from the r
11e98 61 77 20 64 69 73 6b 20 70 61 67 65 2e 0a 2a 2f  aw disk page..*/
11e99 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
11e9a 43 65 6c 6c 49 6e 66 6f 20 43 65 6c 6c 49 6e 66  CellInfo CellInf
11e9b 6f 3b 0a 73 74 72 75 63 74 20 43 65 6c 6c 49 6e  o;.struct CellIn
11e9c 66 6f 20 7b 0a 20 20 75 38 20 2a 70 43 65 6c 6c  fo {.  u8 *pCell
11e9d 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72  ;     /* Pointer
11e9e 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66   to the start of
11e9f 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f   cell content */
11ea0 0a 20 20 69 36 34 20 6e 4b 65 79 3b 20 20 20 20  .  i64 nKey;    
11ea1 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 66 6f 72    /* The key for
11ea2 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 73 2c 20   INTKEY tables, 
11ea3 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  or number of byt
11ea4 65 73 20 69 6e 20 6b 65 79 20 2a 2f 0a 20 20 75  es in key */.  u
11ea5 33 32 20 6e 44 61 74 61 3b 20 20 20 20 20 2f 2a  32 nData;     /*
11ea6 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
11ea7 20 6f 66 20 64 61 74 61 20 2a 2f 0a 20 20 75 33   of data */.  u3
11ea8 32 20 6e 50 61 79 6c 6f 61 64 3b 20 20 2f 2a 20  2 nPayload;  /* 
11ea9 54 6f 74 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20  Total amount of 
11eaa 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 75 31 36  payload */.  u16
11eab 20 6e 48 65 61 64 65 72 3b 20 20 20 2f 2a 20 53   nHeader;   /* S
11eac 69 7a 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20  ize of the cell 
11ead 63 6f 6e 74 65 6e 74 20 68 65 61 64 65 72 20 69  content header i
11eae 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 31 36  n bytes */.  u16
11eaf 20 6e 4c 6f 63 61 6c 3b 20 20 20 20 2f 2a 20 41   nLocal;    /* A
11eb0 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64  mount of payload
11eb1 20 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f   held locally */
11eb2 0a 20 20 75 31 36 20 69 4f 76 65 72 66 6c 6f 77  .  u16 iOverflow
11eb3 3b 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 6f  ; /* Offset to o
11eb4 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6e 75 6d  verflow page num
11eb5 62 65 72 2e 20 20 5a 65 72 6f 20 69 66 20 6e 6f  ber.  Zero if no
11eb6 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 75   overflow */.  u
11eb7 31 36 20 6e 53 69 7a 65 3b 20 20 20 20 20 2f 2a  16 nSize;     /*
11eb8 20 53 69 7a 65 20 6f 66 20 74 68 65 20 63 65 6c   Size of the cel
11eb9 6c 20 63 6f 6e 74 65 6e 74 20 6f 6e 20 74 68 65  l content on the
11eba 20 6d 61 69 6e 20 62 2d 74 72 65 65 20 70 61 67   main b-tree pag
11ebb 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d  e */.};../*.** M
11ebc 61 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20  aximum depth of 
11ebd 61 6e 20 53 51 4c 69 74 65 20 42 2d 54 72 65 65  an SQLite B-Tree
11ebe 20 73 74 72 75 63 74 75 72 65 2e 20 41 6e 79 20   structure. Any 
11ebf 42 2d 54 72 65 65 20 64 65 65 70 65 72 20 74 68  B-Tree deeper th
11ec0 61 6e 0a 2a 2a 20 74 68 69 73 20 77 69 6c 6c 20  an.** this will 
11ec1 62 65 20 64 65 63 6c 61 72 65 64 20 63 6f 72 72  be declared corr
11ec2 75 70 74 2e 20 54 68 69 73 20 76 61 6c 75 65 20  upt. This value 
11ec3 69 73 20 63 61 6c 63 75 6c 61 74 65 64 20 62 61  is calculated ba
11ec4 73 65 64 20 6f 6e 20 61 0a 2a 2a 20 6d 61 78 69  sed on a.** maxi
11ec5 6d 75 6d 20 64 61 74 61 62 61 73 65 20 73 69 7a  mum database siz
11ec6 65 20 6f 66 20 32 5e 33 31 20 70 61 67 65 73 20  e of 2^31 pages 
11ec7 61 20 6d 69 6e 69 6d 75 6d 20 66 61 6e 6f 75 74  a minimum fanout
11ec8 20 6f 66 20 32 20 66 6f 72 20 61 0a 2a 2a 20 72   of 2 for a.** r
11ec9 6f 6f 74 2d 6e 6f 64 65 20 61 6e 64 20 33 20 66  oot-node and 3 f
11eca 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 69 6e 74  or all other int
11ecb 65 72 6e 61 6c 20 6e 6f 64 65 73 2e 0a 2a 2a 0a  ernal nodes..**.
11ecc 2a 2a 20 49 66 20 61 20 74 72 65 65 20 74 68 61  ** If a tree tha
11ecd 74 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20  t appears to be 
11ece 74 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 69 73  taller than this
11ecf 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c   is encountered,
11ed0 20 69 74 20 69 73 0a 2a 2a 20 61 73 73 75 6d 65   it is.** assume
11ed1 64 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62  d that the datab
11ed2 61 73 65 20 69 73 20 63 6f 72 72 75 70 74 2e 0a  ase is corrupt..
11ed3 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 43 55 52  */.#define BTCUR
11ed4 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 20 32 30  SOR_MAX_DEPTH 20
11ed5 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 75 72 73 6f 72  ../*.** A cursor
11ed6 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
11ed7 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 65 6e   a particular en
11ed8 74 72 79 20 77 69 74 68 69 6e 20 61 20 70 61 72  try within a par
11ed9 74 69 63 75 6c 61 72 0a 2a 2a 20 62 2d 74 72 65  ticular.** b-tre
11eda 65 20 77 69 74 68 69 6e 20 61 20 64 61 74 61 62  e within a datab
11edb 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  ase file..**.** 
11edc 54 68 65 20 65 6e 74 72 79 20 69 73 20 69 64 65  The entry is ide
11edd 6e 74 69 66 69 65 64 20 62 79 20 69 74 73 20 4d  ntified by its M
11ede 65 6d 50 61 67 65 20 61 6e 64 20 74 68 65 20 69  emPage and the i
11edf 6e 64 65 78 20 69 6e 0a 2a 2a 20 4d 65 6d 50 61  ndex in.** MemPa
11ee0 67 65 2e 61 43 65 6c 6c 5b 5d 20 6f 66 20 74 68  ge.aCell[] of th
11ee1 65 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 57  e entry..**.** W
11ee2 68 65 6e 20 61 20 73 69 6e 67 6c 65 20 64 61 74  hen a single dat
11ee3 61 62 61 73 65 20 66 69 6c 65 20 63 61 6e 20 73  abase file can s
11ee4 68 61 72 65 64 20 62 79 20 74 77 6f 20 6d 6f 72  hared by two mor
11ee5 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
11ee6 63 74 69 6f 6e 73 2c 0a 2a 2a 20 62 75 74 20 63  ctions,.** but c
11ee7 75 72 73 6f 72 73 20 63 61 6e 6e 6f 74 20 62 65  ursors cannot be
11ee8 20 73 68 61 72 65 64 2e 20 20 45 61 63 68 20 63   shared.  Each c
11ee9 75 72 73 6f 72 20 69 73 20 61 73 73 6f 63 69 61  ursor is associa
11eea 74 65 64 20 77 69 74 68 20 61 0a 2a 2a 20 70 61  ted with a.** pa
11eeb 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73  rticular databas
11eec 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 64 65  e connection ide
11eed 6e 74 69 66 69 65 64 20 42 74 43 75 72 73 6f 72  ntified BtCursor
11eee 2e 70 42 74 72 65 65 2e 64 62 2e 0a 2a 2a 0a 2a  .pBtree.db..**.*
11eef 2a 20 46 69 65 6c 64 73 20 69 6e 20 74 68 69 73  * Fields in this
11ef0 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 61   structure are a
11ef1 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 74 68  ccessed under th
11ef2 65 20 42 74 53 68 61 72 65 64 2e 6d 75 74 65 78  e BtShared.mutex
11ef3 0a 2a 2a 20 66 6f 75 6e 64 20 61 74 20 73 65 6c  .** found at sel
11ef4 66 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 2e 20 0a  f->pBt->mutex. .
11ef5 2a 2f 0a 73 74 72 75 63 74 20 42 74 43 75 72 73  */.struct BtCurs
11ef6 6f 72 20 7b 0a 20 20 42 74 72 65 65 20 2a 70 42  or {.  Btree *pB
11ef7 74 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20  tree;           
11ef8 20 2f 2a 20 54 68 65 20 42 74 72 65 65 20 74 6f   /* The Btree to
11ef9 20 77 68 69 63 68 20 74 68 69 73 20 63 75 72 73   which this curs
11efa 6f 72 20 62 65 6c 6f 6e 67 73 20 2a 2f 0a 20 20  or belongs */.  
11efb 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20  BtShared *pBt;  
11efc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
11efd 20 42 74 53 68 61 72 65 64 20 74 68 69 73 20 63   BtShared this c
11efe 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20  ursor points to 
11eff 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  */.  BtCursor *p
11f00 4e 65 78 74 2c 20 2a 70 50 72 65 76 3b 20 20 2f  Next, *pPrev;  /
11f01 2a 20 46 6f 72 6d 73 20 61 20 6c 69 6e 6b 65 64  * Forms a linked
11f02 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63 75 72   list of all cur
11f03 73 6f 72 73 20 2a 2f 0a 20 20 73 74 72 75 63 74  sors */.  struct
11f04 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
11f05 66 6f 3b 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20  fo; /* Argument 
11f06 70 61 73 73 65 64 20 74 6f 20 63 6f 6d 70 61 72  passed to compar
11f07 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  ison function */
11f08 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74  .  Pgno pgnoRoot
11f09 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
11f0a 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66  The root page of
11f0b 20 74 68 69 73 20 74 72 65 65 20 2a 2f 0a 20 20   this tree */.  
11f0c 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 20 20  CellInfo info;  
11f0d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70            /* A p
11f0e 61 72 73 65 20 6f 66 20 74 68 65 20 63 65 6c 6c  arse of the cell
11f0f 20 77 65 20 61 72 65 20 70 6f 69 6e 74 69 6e 67   we are pointing
11f10 20 61 74 20 2a 2f 0a 20 20 75 38 20 77 72 46 6c   at */.  u8 wrFl
11f11 61 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ag;             
11f12 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77 72     /* True if wr
11f13 69 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 61  itable */.  u8 a
11f14 74 4c 61 73 74 3b 20 20 20 20 20 20 20 20 20 20  tLast;          
11f15 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
11f16 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20  pointing to the 
11f17 6c 61 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20  last entry */.  
11f18 75 38 20 76 61 6c 69 64 4e 4b 65 79 3b 20 20 20  u8 validNKey;   
11f19 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
11f1a 65 20 69 66 20 69 6e 66 6f 2e 6e 4b 65 79 20 69  e if info.nKey i
11f1b 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 75 38 20  s valid */.  u8 
11f1c 65 53 74 61 74 65 3b 20 20 20 20 20 20 20 20 20  eState;         
11f1d 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66         /* One of
11f1e 20 74 68 65 20 43 55 52 53 4f 52 5f 58 58 58 20   the CURSOR_XXX 
11f1f 63 6f 6e 73 74 61 6e 74 73 20 28 73 65 65 20 62  constants (see b
11f20 65 6c 6f 77 29 20 2a 2f 0a 20 20 76 6f 69 64 20  elow) */.  void 
11f21 2a 70 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 53  *pKey;      /* S
11f22 61 76 65 64 20 6b 65 79 20 74 68 61 74 20 77 61  aved key that wa
11f23 73 20 63 75 72 73 6f 72 27 73 20 6c 61 73 74 20  s cursor's last 
11f24 6b 6e 6f 77 6e 20 70 6f 73 69 74 69 6f 6e 20 2a  known position *
11f25 2f 0a 20 20 69 36 34 20 6e 4b 65 79 3b 20 20 20  /.  i64 nKey;   
11f26 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
11f27 70 4b 65 79 2c 20 6f 72 20 6c 61 73 74 20 69 6e  pKey, or last in
11f28 74 65 67 65 72 20 6b 65 79 20 2a 2f 0a 20 20 69  teger key */.  i
11f29 6e 74 20 73 6b 69 70 3b 20 20 20 20 20 20 20 20  nt skip;        
11f2a 2f 2a 20 28 73 6b 69 70 3c 30 29 20 2d 3e 20 50  /* (skip<0) -> P
11f2b 72 65 76 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70  rev() is a no-op
11f2c 2e 20 28 73 6b 69 70 3e 30 29 20 2d 3e 20 4e 65  . (skip>0) -> Ne
11f2d 78 74 28 29 20 69 73 20 2a 2f 0a 23 69 66 6e 64  xt() is */.#ifnd
11f2e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
11f2f 4e 43 52 42 4c 4f 42 0a 20 20 75 38 20 69 73 49  NCRBLOB.  u8 isI
11f30 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 3b 20 20  ncrblobHandle;  
11f31 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
11f32 68 69 73 20 63 75 72 73 6f 72 20 69 73 20 61 6e  his cursor is an
11f33 20 69 6e 63 72 2e 20 69 6f 20 68 61 6e 64 6c 65   incr. io handle
11f34 20 2a 2f 0a 20 20 50 67 6e 6f 20 2a 61 4f 76 65   */.  Pgno *aOve
11f35 72 66 6c 6f 77 3b 20 20 20 20 20 20 20 20 20 20  rflow;          
11f36 2f 2a 20 43 61 63 68 65 20 6f 66 20 6f 76 65 72  /* Cache of over
11f37 66 6c 6f 77 20 70 61 67 65 20 6c 6f 63 61 74 69  flow page locati
11f38 6f 6e 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69  ons */.#endif.#i
11f39 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 75  fndef NDEBUG.  u
11f3a 38 20 70 61 67 65 73 53 68 75 66 66 6c 65 64 3b  8 pagesShuffled;
11f3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
11f3c 20 69 66 20 42 74 72 65 65 20 70 61 67 65 73 20   if Btree pages 
11f3d 61 72 65 20 72 65 61 72 72 61 6e 67 65 64 20 62  are rearranged b
11f3e 79 20 62 61 6c 61 6e 63 65 28 29 2a 2f 0a 23 65  y balance()*/.#e
11f3f 6e 64 69 66 0a 20 20 69 31 36 20 69 50 61 67 65  ndif.  i16 iPage
11f40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11f41 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11f42 49 6e 64 65 78 20 6f 66 20 63 75 72 72 65 6e 74  Index of current
11f43 20 70 61 67 65 20 69 6e 20 61 70 50 61 67 65 20   page in apPage 
11f44 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70  */.  MemPage *ap
11f45 50 61 67 65 5b 42 54 43 55 52 53 4f 52 5f 4d 41  Page[BTCURSOR_MA
11f46 58 5f 44 45 50 54 48 5d 3b 20 20 2f 2a 20 50 61  X_DEPTH];  /* Pa
11f47 67 65 73 20 66 72 6f 6d 20 72 6f 6f 74 20 74 6f  ges from root to
11f48 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 2a 2f   current page */
11f49 0a 20 20 75 31 36 20 61 69 49 64 78 5b 42 54 43  .  u16 aiIdx[BTC
11f4a 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 5d  URSOR_MAX_DEPTH]
11f4b 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72  ;        /* Curr
11f4c 65 6e 74 20 69 6e 64 65 78 20 69 6e 20 61 70 50  ent index in apP
11f4d 61 67 65 5b 69 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  age[i] */.};../*
11f4e 0a 2a 2a 20 50 6f 74 65 6e 74 69 61 6c 20 76 61  .** Potential va
11f4f 6c 75 65 73 20 66 6f 72 20 42 74 43 75 72 73 6f  lues for BtCurso
11f50 72 2e 65 53 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20  r.eState..**.** 
11f51 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a 0a 2a 2a  CURSOR_VALID:.**
11f52 20 20 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 73     Cursor points
11f53 20 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72   to a valid entr
11f54 79 2e 20 67 65 74 50 61 79 6c 6f 61 64 28 29 20  y. getPayload() 
11f55 65 74 63 2e 20 6d 61 79 20 62 65 20 63 61 6c 6c  etc. may be call
11f56 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52  ed..**.** CURSOR
11f57 5f 49 4e 56 41 4c 49 44 3a 0a 2a 2a 20 20 20 43  _INVALID:.**   C
11f58 75 72 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20 70  ursor does not p
11f59 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 20  oint to a valid 
11f5a 65 6e 74 72 79 2e 20 54 68 69 73 20 63 61 6e 20  entry. This can 
11f5b 68 61 70 70 65 6e 20 28 66 6f 72 20 65 78 61 6d  happen (for exam
11f5c 70 6c 65 29 20 0a 2a 2a 20 20 20 62 65 63 61 75  ple) .**   becau
11f5d 73 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  se the table is 
11f5e 65 6d 70 74 79 20 6f 72 20 62 65 63 61 75 73 65  empty or because
11f5f 20 42 74 72 65 65 43 75 72 73 6f 72 46 69 72 73   BtreeCursorFirs
11f60 74 28 29 20 68 61 73 20 6e 6f 74 20 62 65 65 6e  t() has not been
11f61 0a 2a 2a 20 20 20 63 61 6c 6c 65 64 2e 0a 2a 2a  .**   called..**
11f62 0a 2a 2a 20 43 55 52 53 4f 52 5f 52 45 51 55 49  .** CURSOR_REQUI
11f63 52 45 53 45 45 4b 3a 0a 2a 2a 20 20 20 54 68 65  RESEEK:.**   The
11f64 20 74 61 62 6c 65 20 74 68 61 74 20 74 68 69 73   table that this
11f65 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e   cursor was open
11f66 65 64 20 6f 6e 20 73 74 69 6c 6c 20 65 78 69 73  ed on still exis
11f67 74 73 2c 20 62 75 74 20 68 61 73 20 62 65 65 6e  ts, but has been
11f68 20 0a 2a 2a 20 20 20 6d 6f 64 69 66 69 65 64 20   .**   modified 
11f69 73 69 6e 63 65 20 74 68 65 20 63 75 72 73 6f 72  since the cursor
11f6a 20 77 61 73 20 6c 61 73 74 20 75 73 65 64 2e 20   was last used. 
11f6b 54 68 65 20 63 75 72 73 6f 72 20 70 6f 73 69 74  The cursor posit
11f6c 69 6f 6e 20 69 73 20 73 61 76 65 64 0a 2a 2a 20  ion is saved.** 
11f6d 20 20 69 6e 20 76 61 72 69 61 62 6c 65 73 20 42    in variables B
11f6e 74 43 75 72 73 6f 72 2e 70 4b 65 79 20 61 6e 64  tCursor.pKey and
11f6f 20 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 2e 20   BtCursor.nKey. 
11f70 57 68 65 6e 20 61 20 63 75 72 73 6f 72 20 69 73  When a cursor is
11f71 20 69 6e 20 0a 2a 2a 20 20 20 74 68 69 73 20 73   in .**   this s
11f72 74 61 74 65 2c 20 72 65 73 74 6f 72 65 43 75 72  tate, restoreCur
11f73 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 63 61  sorPosition() ca
11f74 6e 20 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 61  n be called to a
11f75 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 73  ttempt to.**   s
11f76 65 65 6b 20 74 68 65 20 63 75 72 73 6f 72 20 74  eek the cursor t
11f77 6f 20 74 68 65 20 73 61 76 65 64 20 70 6f 73 69  o the saved posi
11f78 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53  tion..**.** CURS
11f79 4f 52 5f 46 41 55 4c 54 3a 0a 2a 2a 20 20 20 41  OR_FAULT:.**   A
11f7a 20 75 6e 72 65 63 6f 76 65 72 61 62 6c 65 20 65   unrecoverable e
11f7b 72 72 6f 72 20 28 61 6e 20 49 2f 4f 20 65 72 72  rror (an I/O err
11f7c 6f 72 20 6f 72 20 61 20 6d 61 6c 6c 6f 63 20 66  or or a malloc f
11f7d 61 69 6c 75 72 65 29 20 68 61 73 20 6f 63 63 75  ailure) has occu
11f7e 72 72 65 64 0a 2a 2a 20 20 20 6f 6e 20 61 20 64  rred.**   on a d
11f7f 69 66 66 65 72 65 6e 74 20 63 6f 6e 6e 65 63 74  ifferent connect
11f80 69 6f 6e 20 74 68 61 74 20 73 68 61 72 65 73 20  ion that shares 
11f81 74 68 65 20 42 74 53 68 61 72 65 64 20 63 61 63  the BtShared cac
11f82 68 65 20 77 69 74 68 20 74 68 69 73 0a 2a 2a 20  he with this.** 
11f83 20 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 65    cursor.  The e
11f84 72 72 6f 72 20 68 61 73 20 6c 65 66 74 20 74 68  rror has left th
11f85 65 20 63 61 63 68 65 20 69 6e 20 61 6e 20 69 6e  e cache in an in
11f86 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65  consistent state
11f87 2e 0a 2a 2a 20 20 20 44 6f 20 6e 6f 74 68 69 6e  ..**   Do nothin
11f88 67 20 65 6c 73 65 20 77 69 74 68 20 74 68 69 73  g else with this
11f89 20 63 75 72 73 6f 72 2e 20 20 41 6e 79 20 61 74   cursor.  Any at
11f8a 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 65  tempt to use the
11f8b 20 63 75 72 73 6f 72 0a 2a 2a 20 20 20 73 68 6f   cursor.**   sho
11f8c 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 65  uld return the e
11f8d 72 72 6f 72 20 63 6f 64 65 20 73 74 6f 72 65 64  rror code stored
11f8e 20 69 6e 20 42 74 43 75 72 73 6f 72 2e 73 6b 69   in BtCursor.ski
11f8f 70 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 55 52  p.*/.#define CUR
11f90 53 4f 52 5f 49 4e 56 41 4c 49 44 20 20 20 20 20  SOR_INVALID     
11f91 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20        0.#define 
11f92 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 20  CURSOR_VALID    
11f93 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69           1.#defi
11f94 6e 65 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52  ne CURSOR_REQUIR
11f95 45 53 45 45 4b 20 20 20 20 20 20 20 32 0a 23 64  ESEEK       2.#d
11f96 65 66 69 6e 65 20 43 55 52 53 4f 52 5f 46 41 55  efine CURSOR_FAU
11f97 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 20 33  LT             3
11f98 0a 0a 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73  ../* The databas
11f99 65 20 70 61 67 65 20 74 68 65 20 50 45 4e 44 49  e page the PENDI
11f9a 4e 47 5f 42 59 54 45 20 6f 63 63 75 70 69 65 73  NG_BYTE occupies
11f9b 2e 20 54 68 69 73 20 70 61 67 65 20 69 73 20 6e  . This page is n
11f9c 65 76 65 72 20 75 73 65 64 2e 0a 2a 2a 20 54 4f  ever used..** TO
11f9d 44 4f 3a 20 54 68 69 73 20 6d 61 63 72 6f 20 69  DO: This macro i
11f9e 73 20 76 65 72 79 20 73 69 6d 69 6c 61 72 79 20  s very similary 
11f9f 74 6f 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  to PAGER_MJ_PGNO
11fa0 28 29 20 69 6e 20 70 61 67 65 72 2e 63 2e 20 54  () in pager.c. T
11fa1 68 65 79 0a 2a 2a 20 73 68 6f 75 6c 64 20 70 6f  hey.** should po
11fa2 73 73 69 62 6c 79 20 62 65 20 63 6f 6e 73 6f 6c  ssibly be consol
11fa3 69 64 61 74 65 64 20 28 70 72 65 73 75 6d 61 62  idated (presumab
11fa4 6c 79 20 69 6e 20 70 61 67 65 72 2e 68 29 2e 0a  ly in pager.h)..
11fa5 2a 2a 0a 2a 2a 20 49 66 20 64 69 73 6b 20 49 2f  **.** If disk I/
11fa6 4f 20 69 73 20 6f 6d 69 74 74 65 64 20 28 6d 65  O is omitted (me
11fa7 61 6e 69 6e 67 20 74 68 61 74 20 74 68 65 20 64  aning that the d
11fa8 61 74 61 62 61 73 65 20 69 73 20 73 74 6f 72 65  atabase is store
11fa9 64 20 70 75 72 65 6c 79 0a 2a 2a 20 69 6e 20 6d  d purely.** in m
11faa 65 6d 6f 72 79 29 20 74 68 65 6e 20 74 68 65 72  emory) then ther
11fab 65 20 69 73 20 6e 6f 20 70 65 6e 64 69 6e 67 20  e is no pending 
11fac 62 79 74 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  byte..*/.#ifdef 
11fad 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b  SQLITE_OMIT_DISK
11fae 49 4f 0a 23 20 64 65 66 69 6e 65 20 50 45 4e 44  IO.# define PEND
11faf 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
11fb0 74 29 20 20 30 78 37 66 66 66 66 66 66 66 0a 23  t)  0x7fffffff.#
11fb1 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 50 45  else.# define PE
11fb2 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
11fb3 70 42 74 29 20 28 28 50 45 4e 44 49 4e 47 5f 42  pBt) ((PENDING_B
11fb4 59 54 45 2f 28 70 42 74 29 2d 3e 70 61 67 65 53  YTE/(pBt)->pageS
11fb5 69 7a 65 29 2b 31 29 0a 23 65 6e 64 69 66 0a 0a  ize)+1).#endif..
11fb6 2f 2a 0a 2a 2a 20 41 20 6c 69 6e 6b 65 64 20 6c  /*.** A linked l
11fb7 69 73 74 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  ist of the follo
11fb8 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 73 20  wing structures 
11fb9 69 73 20 73 74 6f 72 65 64 20 61 74 20 42 74 53  is stored at BtS
11fba 68 61 72 65 64 2e 70 4c 6f 63 6b 2e 0a 2a 2a 20  hared.pLock..** 
11fbb 4c 6f 63 6b 73 20 61 72 65 20 61 64 64 65 64 20  Locks are added 
11fbc 28 6f 72 20 75 70 67 72 61 64 65 64 20 66 72 6f  (or upgraded fro
11fbd 6d 20 52 45 41 44 5f 4c 4f 43 4b 20 74 6f 20 57  m READ_LOCK to W
11fbe 52 49 54 45 5f 4c 4f 43 4b 29 20 77 68 65 6e 20  RITE_LOCK) when 
11fbf 61 20 63 75 72 73 6f 72 20 0a 2a 2a 20 69 73 20  a cursor .** is 
11fc0 6f 70 65 6e 65 64 20 6f 6e 20 74 68 65 20 74 61  opened on the ta
11fc1 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 20 70 61  ble with root pa
11fc2 67 65 20 42 74 53 68 61 72 65 64 2e 69 54 61 62  ge BtShared.iTab
11fc3 6c 65 2e 20 4c 6f 63 6b 73 20 61 72 65 20 72 65  le. Locks are re
11fc4 6d 6f 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68  moved.** from th
11fc5 69 73 20 6c 69 73 74 20 77 68 65 6e 20 61 20 74  is list when a t
11fc6 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f  ransaction is co
11fc7 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65  mmitted or rolle
11fc8 64 20 62 61 63 6b 2c 20 6f 72 20 77 68 65 6e 0a  d back, or when.
11fc9 2a 2a 20 61 20 62 74 72 65 65 20 68 61 6e 64 6c  ** a btree handl
11fca 65 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a  e is closed..*/.
11fcb 73 74 72 75 63 74 20 42 74 4c 6f 63 6b 20 7b 0a  struct BtLock {.
11fcc 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 3b    Btree *pBtree;
11fcd 20 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65          /* Btree
11fce 20 68 61 6e 64 6c 65 20 68 6f 6c 64 69 6e 67 20   handle holding 
11fcf 74 68 69 73 20 6c 6f 63 6b 20 2a 2f 0a 20 20 50  this lock */.  P
11fd0 67 6e 6f 20 69 54 61 62 6c 65 3b 20 20 20 20 20  gno iTable;     
11fd1 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67       /* Root pag
11fd2 65 20 6f 66 20 74 61 62 6c 65 20 2a 2f 0a 20 20  e of table */.  
11fd3 75 38 20 65 4c 6f 63 6b 3b 20 20 20 20 20 20 20  u8 eLock;       
11fd4 20 20 20 20 20 20 2f 2a 20 52 45 41 44 5f 4c 4f        /* READ_LO
11fd5 43 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b  CK or WRITE_LOCK
11fd6 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4e   */.  BtLock *pN
11fd7 65 78 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  ext;        /* N
11fd8 65 78 74 20 69 6e 20 42 74 53 68 61 72 65 64 2e  ext in BtShared.
11fd9 70 4c 6f 63 6b 20 6c 69 73 74 20 2a 2f 0a 7d 3b  pLock list */.};
11fda 0a 0a 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 76  ../* Candidate v
11fdb 61 6c 75 65 73 20 66 6f 72 20 42 74 4c 6f 63 6b  alues for BtLock
11fdc 2e 65 4c 6f 63 6b 20 2a 2f 0a 23 64 65 66 69 6e  .eLock */.#defin
11fdd 65 20 52 45 41 44 5f 4c 4f 43 4b 20 20 20 20 20  e READ_LOCK     
11fde 31 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f  1.#define WRITE_
11fdf 4c 4f 43 4b 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a  LOCK    2../*.**
11fe0 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 64 65   These macros de
11fe1 66 69 6e 65 20 74 68 65 20 6c 6f 63 61 74 69 6f  fine the locatio
11fe2 6e 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72  n of the pointer
11fe3 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 20 61  -map entry for a
11fe4 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61   .** database pa
11fe5 67 65 2e 20 54 68 65 20 66 69 72 73 74 20 61 72  ge. The first ar
11fe6 67 75 6d 65 6e 74 20 74 6f 20 65 61 63 68 20 69  gument to each i
11fe7 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
11fe8 75 73 61 62 6c 65 0a 2a 2a 20 62 79 74 65 73 20  usable.** bytes 
11fe9 6f 6e 20 65 61 63 68 20 70 61 67 65 20 6f 66 20  on each page of 
11fea 74 68 65 20 64 61 74 61 62 61 73 65 20 28 6f 66  the database (of
11feb 74 65 6e 20 31 30 32 34 29 2e 20 54 68 65 20 73  ten 1024). The s
11fec 65 63 6f 6e 64 20 69 73 20 74 68 65 0a 2a 2a 20  econd is the.** 
11fed 70 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 6c  page number to l
11fee 6f 6f 6b 20 75 70 20 69 6e 20 74 68 65 20 70 6f  ook up in the po
11fef 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a  inter map..**.**
11ff0 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 20 72   PTRMAP_PAGENO r
11ff1 65 74 75 72 6e 73 20 74 68 65 20 64 61 74 61 62  eturns the datab
11ff2 61 73 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  ase page number 
11ff3 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d  of the pointer-m
11ff4 61 70 0a 2a 2a 20 70 61 67 65 20 74 68 61 74 20  ap.** page that 
11ff5 73 74 6f 72 65 73 20 74 68 65 20 72 65 71 75 69  stores the requi
11ff6 72 65 64 20 70 6f 69 6e 74 65 72 2e 20 50 54 52  red pointer. PTR
11ff7 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 20 72 65  MAP_PTROFFSET re
11ff8 74 75 72 6e 73 0a 2a 2a 20 74 68 65 20 6f 66 66  turns.** the off
11ff9 73 65 74 20 6f 66 20 74 68 65 20 72 65 71 75 65  set of the reque
11ffa 73 74 65 64 20 6d 61 70 20 65 6e 74 72 79 2e 0a  sted map entry..
11ffb 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 67 6e  **.** If the pgn
11ffc 6f 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65  o argument passe
11ffd 64 20 74 6f 20 50 54 52 4d 41 50 5f 50 41 47 45  d to PTRMAP_PAGE
11ffe 4e 4f 20 69 73 20 61 20 70 6f 69 6e 74 65 72 2d  NO is a pointer-
11fff 6d 61 70 20 70 61 67 65 2c 0a 2a 2a 20 74 68 65  map page,.** the
12000 6e 20 70 67 6e 6f 20 69 73 20 72 65 74 75 72 6e  n pgno is return
12001 65 64 2e 20 53 6f 20 28 70 67 6e 6f 3d 3d 50 54  ed. So (pgno==PT
12002 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 67 73 7a  RMAP_PAGENO(pgsz
12003 2c 20 70 67 6e 6f 29 29 20 63 61 6e 20 62 65 0a  , pgno)) can be.
12004 2a 2a 20 75 73 65 64 20 74 6f 20 74 65 73 74 20  ** used to test 
12005 69 66 20 70 67 6e 6f 20 69 73 20 61 20 70 6f 69  if pgno is a poi
12006 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2e 20 50  nter-map page. P
12007 54 52 4d 41 50 5f 49 53 50 41 47 45 20 69 6d 70  TRMAP_ISPAGE imp
12008 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 69 73 20  lements.** this 
12009 74 65 73 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  test..*/.#define
1200a 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70   PTRMAP_PAGENO(p
1200b 42 74 2c 20 70 67 6e 6f 29 20 70 74 72 6d 61 70  Bt, pgno) ptrmap
1200c 50 61 67 65 6e 6f 28 70 42 74 2c 20 70 67 6e 6f  Pageno(pBt, pgno
1200d 29 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50  ).#define PTRMAP
1200e 5f 50 54 52 4f 46 46 53 45 54 28 70 67 70 74 72  _PTROFFSET(pgptr
1200f 6d 61 70 2c 20 70 67 6e 6f 29 20 28 35 2a 28 70  map, pgno) (5*(p
12010 67 6e 6f 2d 70 67 70 74 72 6d 61 70 2d 31 29 29  gno-pgptrmap-1))
12011 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f  .#define PTRMAP_
12012 49 53 50 41 47 45 28 70 42 74 2c 20 70 67 6e 6f  ISPAGE(pBt, pgno
12013 29 20 28 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f  ) (PTRMAP_PAGENO
12014 28 28 70 42 74 29 2c 28 70 67 6e 6f 29 29 3d 3d  ((pBt),(pgno))==
12015 28 70 67 6e 6f 29 29 0a 0a 2f 2a 0a 2a 2a 20 54  (pgno))../*.** T
12016 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69  he pointer map i
12017 73 20 61 20 6c 6f 6f 6b 75 70 20 74 61 62 6c 65  s a lookup table
12018 20 74 68 61 74 20 69 64 65 6e 74 69 66 69 65 73   that identifies
12019 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   the parent page
1201a 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20 63 68 69   for.** each chi
1201b 6c 64 20 70 61 67 65 20 69 6e 20 74 68 65 20 64  ld page in the d
1201c 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54  atabase file.  T
1201d 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 69  he parent page i
1201e 73 20 74 68 65 20 70 61 67 65 20 74 68 61 74 0a  s the page that.
1201f 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f  ** contains a po
12020 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 68 69  inter to the chi
12021 6c 64 2e 20 20 45 76 65 72 79 20 70 61 67 65 20  ld.  Every page 
12022 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
12023 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 30 20 6f 72  contains.** 0 or
12024 20 31 20 70 61 72 65 6e 74 20 70 61 67 65 73 2e   1 parent pages.
12025 20 20 28 49 6e 20 74 68 69 73 20 63 6f 6e 74 65    (In this conte
12026 78 74 20 27 64 61 74 61 62 61 73 65 20 70 61 67  xt 'database pag
12027 65 27 20 72 65 66 65 72 73 0a 2a 2a 20 74 6f 20  e' refers.** to 
12028 61 6e 79 20 70 61 67 65 20 74 68 61 74 20 69 73  any page that is
12029 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65   not part of the
1202a 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 74 73   pointer map its
1202b 65 6c 66 2e 29 20 20 45 61 63 68 20 70 6f 69 6e  elf.)  Each poin
1202c 74 65 72 20 6d 61 70 0a 2a 2a 20 65 6e 74 72 79  ter map.** entry
1202d 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 73   consists of a s
1202e 69 6e 67 6c 65 20 62 79 74 65 20 27 74 79 70 65  ingle byte 'type
1202f 27 20 61 6e 64 20 61 20 34 20 62 79 74 65 20 70  ' and a 4 byte p
12030 61 72 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65  arent page numbe
12031 72 2e 0a 2a 2a 20 54 68 65 20 50 54 52 4d 41 50  r..** The PTRMAP
12032 5f 58 58 58 20 69 64 65 6e 74 69 66 69 65 72 73  _XXX identifiers
12033 20 62 65 6c 6f 77 20 61 72 65 20 74 68 65 20 76   below are the v
12034 61 6c 69 64 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a  alid types..**.*
12035 2a 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66  * The purpose of
12036 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   the pointer map
12037 20 69 73 20 74 6f 20 66 61 63 69 6c 69 74 79 20   is to facility 
12038 6d 6f 76 69 6e 67 20 70 61 67 65 73 20 66 72 6f  moving pages fro
12039 6d 20 6f 6e 65 0a 2a 2a 20 70 6f 73 69 74 69 6f  m one.** positio
1203a 6e 20 69 6e 20 74 68 65 20 66 69 6c 65 20 74 6f  n in the file to
1203b 20 61 6e 6f 74 68 65 72 20 61 73 20 70 61 72 74   another as part
1203c 20 6f 66 20 61 75 74 6f 76 61 63 75 75 6d 2e 20   of autovacuum. 
1203d 20 57 68 65 6e 20 61 20 70 61 67 65 0a 2a 2a 20   When a page.** 
1203e 69 73 20 6d 6f 76 65 64 2c 20 74 68 65 20 70 6f  is moved, the po
1203f 69 6e 74 65 72 20 69 6e 20 69 74 73 20 70 61 72  inter in its par
12040 65 6e 74 20 6d 75 73 74 20 62 65 20 75 70 64 61  ent must be upda
12041 74 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  ted to point to 
12042 74 68 65 0a 2a 2a 20 6e 65 77 20 6c 6f 63 61 74  the.** new locat
12043 69 6f 6e 2e 20 20 54 68 65 20 70 6f 69 6e 74 65  ion.  The pointe
12044 72 20 6d 61 70 20 69 73 20 75 73 65 64 20 74 6f  r map is used to
12045 20 6c 6f 63 61 74 65 20 74 68 65 20 70 61 72 65   locate the pare
12046 6e 74 20 70 61 67 65 20 71 75 69 63 6b 6c 79 2e  nt page quickly.
12047 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 52 4f  .**.** PTRMAP_RO
12048 4f 54 50 41 47 45 3a 20 54 68 65 20 64 61 74 61  OTPAGE: The data
12049 62 61 73 65 20 70 61 67 65 20 69 73 20 61 20 72  base page is a r
1204a 6f 6f 74 2d 70 61 67 65 2e 20 54 68 65 20 70 61  oot-page. The pa
1204b 67 65 2d 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74  ge-number is not
1204c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1204d 20 20 20 20 20 75 73 65 64 20 69 6e 20 74 68 69       used in thi
1204e 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54  s case..**.** PT
1204f 52 4d 41 50 5f 46 52 45 45 50 41 47 45 3a 20 54  RMAP_FREEPAGE: T
12050 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65  he database page
12051 20 69 73 20 61 6e 20 75 6e 75 73 65 64 20 28 66   is an unused (f
12052 72 65 65 29 20 70 61 67 65 2e 20 54 68 65 20 70  ree) page. The p
12053 61 67 65 2d 6e 75 6d 62 65 72 20 0a 2a 2a 20 20  age-number .**  
12054 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12055 69 73 20 6e 6f 74 20 75 73 65 64 20 69 6e 20 74  is not used in t
12056 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  his case..**.** 
12057 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31  PTRMAP_OVERFLOW1
12058 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70  : The database p
12059 61 67 65 20 69 73 20 74 68 65 20 66 69 72 73 74  age is the first
1205a 20 70 61 67 65 20 69 6e 20 61 20 6c 69 73 74 20   page in a list 
1205b 6f 66 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  of .**          
1205c 20 20 20 20 20 20 20 20 20 6f 76 65 72 66 6c 6f           overflo
1205d 77 20 70 61 67 65 73 2e 20 54 68 65 20 70 61 67  w pages. The pag
1205e 65 20 6e 75 6d 62 65 72 20 69 64 65 6e 74 69 66  e number identif
1205f 69 65 73 20 74 68 65 20 70 61 67 65 20 74 68 61  ies the page tha
12060 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
12061 20 20 20 20 20 20 20 63 6f 6e 74 61 69 6e 73 20         contains 
12062 74 68 65 20 63 65 6c 6c 20 77 69 74 68 20 61 20  the cell with a 
12063 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20  pointer to this 
12064 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 2a  overflow page..*
12065 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52  *.** PTRMAP_OVER
12066 46 4c 4f 57 32 3a 20 54 68 65 20 64 61 74 61 62  FLOW2: The datab
12067 61 73 65 20 70 61 67 65 20 69 73 20 74 68 65 20  ase page is the 
12068 73 65 63 6f 6e 64 20 6f 72 20 6c 61 74 65 72 20  second or later 
12069 70 61 67 65 20 69 6e 20 61 20 6c 69 73 74 20 6f  page in a list o
1206a 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  f.**            
1206b 20 20 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20         overflow 
1206c 70 61 67 65 73 2e 20 54 68 65 20 70 61 67 65 2d  pages. The page-
1206d 6e 75 6d 62 65 72 20 69 64 65 6e 74 69 66 69 65  number identifie
1206e 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a 2a  s the previous.*
1206f 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
12070 20 20 20 20 70 61 67 65 20 69 6e 20 74 68 65 20      page in the 
12071 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 69  overflow page li
12072 73 74 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50  st..**.** PTRMAP
12073 5f 42 54 52 45 45 3a 20 54 68 65 20 64 61 74 61  _BTREE: The data
12074 62 61 73 65 20 70 61 67 65 20 69 73 20 61 20 6e  base page is a n
12075 6f 6e 2d 72 6f 6f 74 20 62 74 72 65 65 20 70 61  on-root btree pa
12076 67 65 2e 20 54 68 65 20 70 61 67 65 20 6e 75 6d  ge. The page num
12077 62 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ber.**          
12078 20 20 20 20 20 69 64 65 6e 74 69 66 69 65 73 20       identifies 
12079 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20  the parent page 
1207a 69 6e 20 74 68 65 20 62 74 72 65 65 2e 0a 2a 2f  in the btree..*/
1207b 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f  .#define PTRMAP_
1207c 52 4f 4f 54 50 41 47 45 20 31 0a 23 64 65 66 69  ROOTPAGE 1.#defi
1207d 6e 65 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41  ne PTRMAP_FREEPA
1207e 47 45 20 32 0a 23 64 65 66 69 6e 65 20 50 54 52  GE 2.#define PTR
1207f 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 33 0a  MAP_OVERFLOW1 3.
12080 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f  #define PTRMAP_O
12081 56 45 52 46 4c 4f 57 32 20 34 0a 23 64 65 66 69  VERFLOW2 4.#defi
12082 6e 65 20 50 54 52 4d 41 50 5f 42 54 52 45 45 20  ne PTRMAP_BTREE 
12083 35 0a 0a 2f 2a 20 41 20 62 75 6e 63 68 20 6f 66  5../* A bunch of
12084 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
12085 65 6e 74 73 20 74 6f 20 63 68 65 63 6b 20 74 68  ents to check th
12086 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 74  e transaction st
12087 61 74 65 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a  ate variables.**
12088 20 6f 66 20 68 61 6e 64 6c 65 20 70 20 28 74 79   of handle p (ty
12089 70 65 20 42 74 72 65 65 2a 29 20 61 72 65 20 69  pe Btree*) are i
1208a 6e 74 65 72 6e 61 6c 6c 79 20 63 6f 6e 73 69 73  nternally consis
1208b 74 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  tent..*/.#define
1208c 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28   btreeIntegrity(
1208d 70 29 20 5c 0a 20 20 61 73 73 65 72 74 28 20 70  p) \.  assert( p
1208e 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63  ->pBt->inTransac
1208f 74 69 6f 6e 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45  tion!=TRANS_NONE
12090 20 7c 7c 20 70 2d 3e 70 42 74 2d 3e 6e 54 72 61   || p->pBt->nTra
12091 6e 73 61 63 74 69 6f 6e 3d 3d 30 20 29 3b 20 5c  nsaction==0 ); \
12092 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42  .  assert( p->pB
12093 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
12094 3e 3d 70 2d 3e 69 6e 54 72 61 6e 73 20 29 3b 20  >=p->inTrans ); 
12095 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 49 53 41  .../*.** The ISA
12096 55 54 4f 56 41 43 55 55 4d 20 6d 61 63 72 6f 20  UTOVACUUM macro 
12097 69 73 20 75 73 65 64 20 77 69 74 68 69 6e 20 62  is used within b
12098 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29  alance_nonroot()
12099 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a   to determine.**
1209a 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65   if the database
1209b 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76   supports auto-v
1209c 61 63 75 75 6d 20 6f 72 20 6e 6f 74 2e 20 42 65  acuum or not. Be
1209d 63 61 75 73 65 20 69 74 20 69 73 20 75 73 65 64  cause it is used
1209e 0a 2a 2a 20 77 69 74 68 69 6e 20 61 6e 20 65 78  .** within an ex
1209f 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73  pression that is
120a0 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20   an argument to 
120a1 61 6e 6f 74 68 65 72 20 6d 61 63 72 6f 20 0a 2a  another macro .*
120a2 2a 20 28 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 52  * (sqliteMallocR
120a3 61 77 29 2c 20 69 74 20 69 73 20 6e 6f 74 20 70  aw), it is not p
120a4 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65 20 63  ossible to use c
120a5 6f 6e 64 69 74 69 6f 6e 61 6c 20 63 6f 6d 70 69  onditional compi
120a6 6c 61 74 69 6f 6e 2e 0a 2a 2a 20 53 6f 2c 20 74  lation..** So, t
120a7 68 69 73 20 6d 61 63 72 6f 20 69 73 20 64 65 66  his macro is def
120a8 69 6e 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2f  ined instead..*/
120a9 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
120aa 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
120ab 23 64 65 66 69 6e 65 20 49 53 41 55 54 4f 56 41  #define ISAUTOVA
120ac 43 55 55 4d 20 28 70 42 74 2d 3e 61 75 74 6f 56  CUUM (pBt->autoV
120ad 61 63 75 75 6d 29 0a 23 65 6c 73 65 0a 23 64 65  acuum).#else.#de
120ae 66 69 6e 65 20 49 53 41 55 54 4f 56 41 43 55 55  fine ISAUTOVACUU
120af 4d 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  M 0.#endif.../*.
120b0 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72  ** This structur
120b1 65 20 69 73 20 70 61 73 73 65 64 20 61 72 6f 75  e is passed arou
120b2 6e 64 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 74  nd through all t
120b3 68 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69  he sanity checki
120b4 6e 67 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69  ng routines.** i
120b5 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20  n order to keep 
120b6 74 72 61 63 6b 20 6f 66 20 73 6f 6d 65 20 67 6c  track of some gl
120b7 6f 62 61 6c 20 73 74 61 74 65 20 69 6e 66 6f 72  obal state infor
120b8 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64  mation..*/.typed
120b9 65 66 20 73 74 72 75 63 74 20 49 6e 74 65 67 72  ef struct Integr
120ba 69 74 79 43 6b 20 49 6e 74 65 67 72 69 74 79 43  ityCk IntegrityC
120bb 6b 3b 0a 73 74 72 75 63 74 20 49 6e 74 65 67 72  k;.struct Integr
120bc 69 74 79 43 6b 20 7b 0a 20 20 42 74 53 68 61 72  ityCk {.  BtShar
120bd 65 64 20 2a 70 42 74 3b 20 20 20 20 2f 2a 20 54  ed *pBt;    /* T
120be 68 65 20 74 72 65 65 20 62 65 69 6e 67 20 63 68  he tree being ch
120bf 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a 20 20 50  ecked out */.  P
120c0 61 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20  ager *pPager;   
120c1 20 2f 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74   /* The associat
120c2 65 64 20 70 61 67 65 72 2e 20 20 41 6c 73 6f 20  ed pager.  Also 
120c3 61 63 63 65 73 73 69 62 6c 65 20 62 79 20 70 42  accessible by pB
120c4 74 2d 3e 70 50 61 67 65 72 20 2a 2f 0a 20 20 69  t->pPager */.  i
120c5 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20  nt nPage;       
120c6 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
120c7 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62  ges in the datab
120c8 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 6e  ase */.  int *an
120c9 52 65 66 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75  Ref;       /* Nu
120ca 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 65 61  mber of times ea
120cb 63 68 20 70 61 67 65 20 69 73 20 72 65 66 65 72  ch page is refer
120cc 65 6e 63 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6d  enced */.  int m
120cd 78 45 72 72 3b 20 20 20 20 20 20 20 20 2f 2a 20  xErr;        /* 
120ce 53 74 6f 70 20 61 63 63 75 6d 75 6c 61 74 69 6e  Stop accumulatin
120cf 67 20 65 72 72 6f 72 73 20 77 68 65 6e 20 74 68  g errors when th
120d0 69 73 20 72 65 61 63 68 65 73 20 7a 65 72 6f 20  is reaches zero 
120d1 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20  */.  int nErr;  
120d2 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
120d3 20 6f 66 20 6d 65 73 73 61 67 65 73 20 77 72 69   of messages wri
120d4 74 74 65 6e 20 74 6f 20 7a 45 72 72 4d 73 67 20  tten to zErrMsg 
120d5 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20  so far */.  int 
120d6 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 2f 2a  mallocFailed; /*
120d7 20 41 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   A memory alloca
120d8 74 69 6f 6e 20 65 72 72 6f 72 20 68 61 73 20 6f  tion error has o
120d9 63 63 75 72 72 65 64 20 2a 2f 0a 20 20 53 74 72  ccurred */.  Str
120da 41 63 63 75 6d 20 65 72 72 4d 73 67 3b 20 20 2f  Accum errMsg;  /
120db 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 74 68 65  * Accumulate the
120dc 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74   error message t
120dd 65 78 74 20 68 65 72 65 20 2a 2f 0a 7d 3b 0a 0a  ext here */.};..
120de 2f 2a 0a 2a 2a 20 52 65 61 64 20 6f 72 20 77 72  /*.** Read or wr
120df 69 74 65 20 61 20 74 77 6f 2d 20 61 6e 64 20 66  ite a two- and f
120e0 6f 75 72 2d 62 79 74 65 20 62 69 67 2d 65 6e 64  our-byte big-end
120e1 69 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75  ian integer valu
120e2 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67  es..*/.#define g
120e3 65 74 32 62 79 74 65 28 78 29 20 20 20 28 28 78  et2byte(x)   ((x
120e4 29 5b 30 5d 3c 3c 38 20 7c 20 28 78 29 5b 31 5d  )[0]<<8 | (x)[1]
120e5 29 0a 23 64 65 66 69 6e 65 20 70 75 74 32 62 79  ).#define put2by
120e6 74 65 28 70 2c 76 29 20 28 28 70 29 5b 30 5d 20  te(p,v) ((p)[0] 
120e7 3d 20 28 76 29 3e 3e 38 2c 20 28 70 29 5b 31 5d  = (v)>>8, (p)[1]
120e8 20 3d 20 28 76 29 29 0a 23 64 65 66 69 6e 65 20   = (v)).#define 
120e9 67 65 74 34 62 79 74 65 20 73 71 6c 69 74 65 33  get4byte sqlite3
120ea 47 65 74 34 62 79 74 65 0a 23 64 65 66 69 6e 65  Get4byte.#define
120eb 20 70 75 74 34 62 79 74 65 20 73 71 6c 69 74 65   put4byte sqlite
120ec 33 50 75 74 34 62 79 74 65 0a 0a 2f 2a 0a 2a 2a  3Put4byte../*.**
120ed 20 49 6e 74 65 72 6e 61 6c 20 72 6f 75 74 69 6e   Internal routin
120ee 65 73 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62  es that should b
120ef 65 20 61 63 63 65 73 73 65 64 20 62 79 20 74 68  e accessed by th
120f0 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 6f 6e  e btree layer on
120f1 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ly..*/.SQLITE_PR
120f2 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
120f3 33 42 74 72 65 65 47 65 74 50 61 67 65 28 42 74  3BtreeGetPage(Bt
120f4 53 68 61 72 65 64 2a 2c 20 50 67 6e 6f 2c 20 4d  Shared*, Pgno, M
120f5 65 6d 50 61 67 65 2a 2a 2c 20 69 6e 74 29 3b 0a  emPage**, int);.
120f6 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
120f7 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  nt sqlite3BtreeI
120f8 6e 69 74 50 61 67 65 28 4d 65 6d 50 61 67 65 20  nitPage(MemPage 
120f9 2a 70 50 61 67 65 29 3b 0a 53 51 4c 49 54 45 5f  *pPage);.SQLITE_
120fa 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
120fb 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65  ite3BtreeParseCe
120fc 6c 6c 50 74 72 28 4d 65 6d 50 61 67 65 2a 2c 20  llPtr(MemPage*, 
120fd 75 38 2a 2c 20 43 65 6c 6c 49 6e 66 6f 2a 29 3b  u8*, CellInfo*);
120fe 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
120ff 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
12100 65 50 61 72 73 65 43 65 6c 6c 28 4d 65 6d 50 61  eParseCell(MemPa
12101 67 65 2a 2c 20 69 6e 74 2c 20 43 65 6c 6c 49 6e  ge*, int, CellIn
12102 66 6f 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  fo*);.SQLITE_PRI
12103 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
12104 42 74 72 65 65 52 65 73 74 6f 72 65 43 75 72 73  BtreeRestoreCurs
12105 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74 43 75 72  orPosition(BtCur
12106 73 6f 72 20 2a 70 43 75 72 29 3b 0a 53 51 4c 49  sor *pCur);.SQLI
12107 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
12108 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 54  sqlite3BtreeGetT
12109 65 6d 70 43 75 72 73 6f 72 28 42 74 43 75 72 73  empCursor(BtCurs
1210a 6f 72 20 2a 70 43 75 72 2c 20 42 74 43 75 72 73  or *pCur, BtCurs
1210b 6f 72 20 2a 70 54 65 6d 70 43 75 72 29 3b 0a 53  or *pTempCur);.S
1210c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1210d 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 52  id sqlite3BtreeR
1210e 65 6c 65 61 73 65 54 65 6d 70 43 75 72 73 6f 72  eleaseTempCursor
1210f 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29  (BtCursor *pCur)
12110 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
12111 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
12112 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e 74 28 42  eeMoveToParent(B
12113 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 3b 0a  tCursor *pCur);.
12114 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
12115 20 45 6e 64 20 6f 66 20 62 74 72 65 65 49 6e 74   End of btreeInt
12116 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
12117 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12118 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
12119 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1211a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
1211b 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
1211c 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a   btmutex.c *****
1211d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1211e 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
1211f 41 44 53 41 46 45 20 26 26 20 21 64 65 66 69 6e  ADSAFE && !defin
12120 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ed(SQLITE_OMIT_S
12121 48 41 52 45 44 5f 43 41 43 48 45 29 0a 0a 0a 2f  HARED_CACHE).../
12122 2a 0a 2a 2a 20 45 6e 74 65 72 20 61 20 6d 75 74  *.** Enter a mut
12123 65 78 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20  ex on the given 
12124 42 54 72 65 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a  BTree object..**
12125 0a 2a 2a 20 49 66 20 74 68 65 20 6f 62 6a 65 63  .** If the objec
12126 74 20 69 73 20 6e 6f 74 20 73 68 61 72 61 62 6c  t is not sharabl
12127 65 2c 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78  e, then no mutex
12128 20 69 73 20 65 76 65 72 20 72 65 71 75 69 72 65   is ever require
12129 64 0a 2a 2a 20 61 6e 64 20 74 68 69 73 20 72 6f  d.** and this ro
1212a 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70  utine is a no-op
1212b 2e 20 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e  .  The underlyin
1212c 67 20 6d 75 74 65 78 20 69 73 20 6e 6f 6e 2d 72  g mutex is non-r
1212d 65 63 75 72 73 69 76 65 2e 0a 2a 2a 20 42 75 74  ecursive..** But
1212e 20 77 65 20 6b 65 65 70 20 61 20 72 65 66 65 72   we keep a refer
1212f 65 6e 63 65 20 63 6f 75 6e 74 20 69 6e 20 42 74  ence count in Bt
12130 72 65 65 2e 77 61 6e 74 54 6f 4c 6f 63 6b 20 73  ree.wantToLock s
12131 6f 20 74 68 65 20 62 65 68 61 76 69 6f 72 0a 2a  o the behavior.*
12132 2a 20 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66  * of this interf
12133 61 63 65 20 69 73 20 72 65 63 75 72 73 69 76 65  ace is recursive
12134 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64  ..**.** To avoid
12135 20 64 65 61 64 6c 6f 63 6b 73 2c 20 6d 75 6c 74   deadlocks, mult
12136 69 70 6c 65 20 42 74 72 65 65 73 20 61 72 65 20  iple Btrees are 
12137 6c 6f 63 6b 65 64 20 69 6e 20 74 68 65 20 73 61  locked in the sa
12138 6d 65 20 6f 72 64 65 72 0a 2a 2a 20 62 79 20 61  me order.** by a
12139 6c 6c 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ll database conn
1213a 65 63 74 69 6f 6e 73 2e 20 20 54 68 65 20 70 2d  ections.  The p-
1213b 3e 70 4e 65 78 74 20 69 73 20 61 20 6c 69 73 74  >pNext is a list
1213c 20 6f 66 20 6f 74 68 65 72 0a 2a 2a 20 42 74 72   of other.** Btr
1213d 65 65 73 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f  ees belonging to
1213e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
1213f 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 73  se connection as
12140 20 74 68 65 20 70 20 42 74 72 65 65 0a 2a 2a 20   the p Btree.** 
12141 77 68 69 63 68 20 6e 65 65 64 20 74 6f 20 62 65  which need to be
12142 20 6c 6f 63 6b 65 64 20 61 66 74 65 72 20 70 2e   locked after p.
12143 20 20 49 66 20 77 65 20 63 61 6e 6e 6f 74 20 67    If we cannot g
12144 65 74 20 61 20 6c 6f 63 6b 20 6f 6e 0a 2a 2a 20  et a lock on.** 
12145 70 2c 20 74 68 65 6e 20 66 69 72 73 74 20 75 6e  p, then first un
12146 6c 6f 63 6b 20 61 6c 6c 20 6f 66 20 74 68 65 20  lock all of the 
12147 6f 74 68 65 72 73 20 6f 6e 20 70 2d 3e 70 4e 65  others on p->pNe
12148 78 74 2c 20 74 68 65 6e 20 77 61 69 74 0a 2a 2a  xt, then wait.**
12149 20 66 6f 72 20 74 68 65 20 6c 6f 63 6b 20 74 6f   for the lock to
1214a 20 62 65 63 6f 6d 65 20 61 76 61 69 6c 61 62 6c   become availabl
1214b 65 20 6f 6e 20 70 2c 20 74 68 65 6e 20 72 65 6c  e on p, then rel
1214c 6f 63 6b 20 61 6c 6c 20 6f 66 20 74 68 65 0a 2a  ock all of the.*
1214d 2a 20 73 75 62 73 65 71 75 65 6e 74 20 42 74 72  * subsequent Btr
1214e 65 65 73 20 74 68 61 74 20 64 65 73 69 72 65 20  ees that desire 
1214f 61 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54  a lock..*/.SQLIT
12150 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
12151 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
12152 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74  (Btree *p){.  Bt
12153 72 65 65 20 2a 70 4c 61 74 65 72 3b 0a 0a 20 20  ree *pLater;..  
12154 2f 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61  /* Some basic sa
12155 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e  nity checking on
12156 20 74 68 65 20 42 74 72 65 65 2e 20 20 54 68 65   the Btree.  The
12157 20 6c 69 73 74 20 6f 66 20 42 74 72 65 65 73 0a   list of Btrees.
12158 20 20 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62    ** connected b
12159 79 20 70 4e 65 78 74 20 61 6e 64 20 70 50 72 65  y pNext and pPre
1215a 76 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 20 73  v should be in s
1215b 6f 72 74 65 64 20 6f 72 64 65 72 20 62 79 0a 20  orted order by. 
1215c 20 2a 2a 20 42 74 72 65 65 2e 70 42 74 20 76 61   ** Btree.pBt va
1215d 6c 75 65 2e 20 41 6c 6c 20 65 6c 65 6d 65 6e 74  lue. All element
1215e 73 20 6f 66 20 74 68 65 20 6c 69 73 74 20 73 68  s of the list sh
1215f 6f 75 6c 64 20 62 65 6c 6f 6e 67 20 74 6f 0a 20  ould belong to. 
12160 20 2a 2a 20 74 68 65 20 73 61 6d 65 20 63 6f 6e   ** the same con
12161 6e 65 63 74 69 6f 6e 2e 20 4f 6e 6c 79 20 73 68  nection. Only sh
12162 61 72 65 64 20 42 74 72 65 65 73 20 61 72 65 20  ared Btrees are 
12163 6f 6e 20 74 68 65 20 6c 69 73 74 2e 20 2a 2f 0a  on the list. */.
12164 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4e 65    assert( p->pNe
12165 78 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78  xt==0 || p->pNex
12166 74 2d 3e 70 42 74 3e 70 2d 3e 70 42 74 20 29 3b  t->pBt>p->pBt );
12167 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50  .  assert( p->pP
12168 72 65 76 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72  rev==0 || p->pPr
12169 65 76 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29  ev->pBt<p->pBt )
1216a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70  ;.  assert( p->p
1216b 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e  Next==0 || p->pN
1216c 65 78 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29  ext->db==p->db )
1216d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70  ;.  assert( p->p
1216e 50 72 65 76 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50  Prev==0 || p->pP
1216f 72 65 76 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29  rev->db==p->db )
12170 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73  ;.  assert( p->s
12171 68 61 72 61 62 6c 65 20 7c 7c 20 28 70 2d 3e 70  harable || (p->p
12172 4e 65 78 74 3d 3d 30 20 26 26 20 70 2d 3e 70 50  Next==0 && p->pP
12173 72 65 76 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a  rev==0) );..  /*
12174 20 43 68 65 63 6b 20 66 6f 72 20 6c 6f 63 6b 69   Check for locki
12175 6e 67 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 2a  ng consistency *
12176 2f 0a 20 20 61 73 73 65 72 74 28 20 21 70 2d 3e  /.  assert( !p->
12177 6c 6f 63 6b 65 64 20 7c 7c 20 70 2d 3e 77 61 6e  locked || p->wan
12178 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 61  tToLock>0 );.  a
12179 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62  ssert( p->sharab
1217a 6c 65 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c  le || p->wantToL
1217b 6f 63 6b 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20  ock==0 );..  /* 
1217c 57 65 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64  We should alread
1217d 79 20 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e  y hold a lock on
1217e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
1217f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73  nnection */.  as
12180 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
12181 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e  tex_held(p->db->
12182 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 69 66 28  mutex) );..  if(
12183 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 20   !p->sharable ) 
12184 72 65 74 75 72 6e 3b 0a 20 20 70 2d 3e 77 61 6e  return;.  p->wan
12185 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 69 66 28  tToLock++;.  if(
12186 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 20 72 65 74   p->locked ) ret
12187 75 72 6e 3b 0a 0a 20 20 2f 2a 20 49 6e 20 6d 6f  urn;..  /* In mo
12188 73 74 20 63 61 73 65 73 2c 20 77 65 20 73 68 6f  st cases, we sho
12189 75 6c 64 20 62 65 20 61 62 6c 65 20 74 6f 20 61  uld be able to a
1218a 63 71 75 69 72 65 20 74 68 65 20 6c 6f 63 6b 20  cquire the lock 
1218b 77 65 0a 20 20 2a 2a 20 77 61 6e 74 20 77 69 74  we.  ** want wit
1218c 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 67  hout having to g
1218d 6f 20 74 68 72 6f 75 67 68 74 20 74 68 65 20 61  o throught the a
1218e 73 63 65 6e 64 69 6e 67 20 6c 6f 63 6b 0a 20 20  scending lock.  
1218f 2a 2a 20 70 72 6f 63 65 64 75 72 65 20 74 68 61  ** procedure tha
12190 74 20 66 6f 6c 6c 6f 77 73 2e 20 20 4a 75 73 74  t follows.  Just
12191 20 62 65 20 73 75 72 65 20 6e 6f 74 20 74 6f 20   be sure not to 
12192 62 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66  block..  */.  if
12193 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
12194 74 72 79 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65  try(p->pBt->mute
12195 78 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  x)==SQLITE_OK ){
12196 0a 20 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d  .    p->locked =
12197 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   1;.    return;.
12198 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f 20 61 76 6f    }..  /* To avo
12199 69 64 20 64 65 61 64 6c 6f 63 6b 2c 20 66 69 72  id deadlock, fir
1219a 73 74 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 6c  st release all l
1219b 6f 63 6b 73 20 77 69 74 68 20 61 20 6c 61 72 67  ocks with a larg
1219c 65 72 0a 20 20 2a 2a 20 42 74 53 68 61 72 65 64  er.  ** BtShared
1219d 20 61 64 64 72 65 73 73 2e 20 20 54 68 65 6e 20   address.  Then 
1219e 61 63 71 75 69 72 65 20 6f 75 72 20 6c 6f 63 6b  acquire our lock
1219f 2e 20 20 54 68 65 6e 20 72 65 61 63 71 75 69 72  .  Then reacquir
121a0 65 0a 20 20 2a 2a 20 74 68 65 20 6f 74 68 65 72  e.  ** the other
121a1 20 42 74 53 68 61 72 65 64 20 6c 6f 63 6b 73 20   BtShared locks 
121a2 74 68 61 74 20 77 65 20 75 73 65 64 20 74 6f 20  that we used to 
121a3 68 6f 6c 64 20 69 6e 20 61 73 63 65 6e 64 69 6e  hold in ascendin
121a4 67 0a 20 20 2a 2a 20 6f 72 64 65 72 2e 0a 20 20  g.  ** order..  
121a5 2a 2f 0a 20 20 66 6f 72 28 70 4c 61 74 65 72 3d  */.  for(pLater=
121a6 70 2d 3e 70 4e 65 78 74 3b 20 70 4c 61 74 65 72  p->pNext; pLater
121a7 3b 20 70 4c 61 74 65 72 3d 70 4c 61 74 65 72 2d  ; pLater=pLater-
121a8 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73  >pNext){.    ass
121a9 65 72 74 28 20 70 4c 61 74 65 72 2d 3e 73 68 61  ert( pLater->sha
121aa 72 61 62 6c 65 20 29 3b 0a 20 20 20 20 61 73 73  rable );.    ass
121ab 65 72 74 28 20 70 4c 61 74 65 72 2d 3e 70 4e 65  ert( pLater->pNe
121ac 78 74 3d 3d 30 20 7c 7c 20 70 4c 61 74 65 72 2d  xt==0 || pLater-
121ad 3e 70 4e 65 78 74 2d 3e 70 42 74 3e 70 4c 61 74  >pNext->pBt>pLat
121ae 65 72 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61  er->pBt );.    a
121af 73 73 65 72 74 28 20 21 70 4c 61 74 65 72 2d 3e  ssert( !pLater->
121b0 6c 6f 63 6b 65 64 20 7c 7c 20 70 4c 61 74 65 72  locked || pLater
121b1 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29  ->wantToLock>0 )
121b2 3b 0a 20 20 20 20 69 66 28 20 70 4c 61 74 65 72  ;.    if( pLater
121b3 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20  ->locked ){.    
121b4 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
121b5 6c 65 61 76 65 28 70 4c 61 74 65 72 2d 3e 70 42  leave(pLater->pB
121b6 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20  t->mutex);.     
121b7 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20   pLater->locked 
121b8 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  = 0;.    }.  }. 
121b9 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
121ba 6e 74 65 72 28 70 2d 3e 70 42 74 2d 3e 6d 75 74  nter(p->pBt->mut
121bb 65 78 29 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 65 64  ex);.  p->locked
121bc 20 3d 20 31 3b 0a 20 20 66 6f 72 28 70 4c 61 74   = 1;.  for(pLat
121bd 65 72 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 4c 61  er=p->pNext; pLa
121be 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61 74  ter; pLater=pLat
121bf 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  er->pNext){.    
121c0 69 66 28 20 70 4c 61 74 65 72 2d 3e 77 61 6e 74  if( pLater->want
121c1 54 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20  ToLock ){.      
121c2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
121c3 74 65 72 28 70 4c 61 74 65 72 2d 3e 70 42 74 2d  ter(pLater->pBt-
121c4 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 70  >mutex);.      p
121c5 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 3d 20  Later->locked = 
121c6 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  1;.    }.  }.}..
121c7 2f 2a 0a 2a 2a 20 45 78 69 74 20 74 68 65 20 72  /*.** Exit the r
121c8 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 6f  ecursive mutex o
121c9 6e 20 61 20 42 74 72 65 65 2e 0a 2a 2f 0a 53 51  n a Btree..*/.SQ
121ca 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
121cb 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65  d sqlite3BtreeLe
121cc 61 76 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  ave(Btree *p){. 
121cd 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65   if( p->sharable
121ce 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
121cf 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20  p->wantToLock>0 
121d0 29 3b 0a 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f  );.    p->wantTo
121d1 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 20  Lock--;.    if( 
121d2 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30  p->wantToLock==0
121d3 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
121d4 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 3b 0a 20  ( p->locked );. 
121d5 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
121d6 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 42 74 2d  ex_leave(p->pBt-
121d7 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 70  >mutex);.      p
121d8 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30 3b 0a 20 20  ->locked = 0;.  
121d9 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64    }.  }.}..#ifnd
121da 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20  ef NDEBUG./*.** 
121db 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74  Return true if t
121dc 68 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 65  he BtShared mute
121dd 78 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65  x is held on the
121de 20 62 74 72 65 65 2e 20 20 0a 2a 2a 0a 2a 2a 20   btree.  .**.** 
121df 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 6b  This routine mak
121e0 65 73 20 6e 6f 20 64 65 74 65 72 6d 69 6e 61 74  es no determinat
121e1 69 6f 6e 20 6f 6e 65 20 77 68 79 20 6f 72 20 61  ion one why or a
121e2 6e 6f 74 68 65 72 20 69 66 20 74 68 65 0a 2a 2a  nother if the.**
121e3 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
121e4 74 69 6f 6e 20 6d 75 74 65 78 20 69 73 20 68 65  tion mutex is he
121e5 6c 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ld..**.** This r
121e6 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 6f  outine is used o
121e7 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  nly from within 
121e8 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
121e9 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  nts..*/.SQLITE_P
121ea 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
121eb 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65  e3BtreeHoldsMute
121ec 78 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72  x(Btree *p){.  r
121ed 65 74 75 72 6e 20 28 70 2d 3e 73 68 61 72 61 62  eturn (p->sharab
121ee 6c 65 3d 3d 30 20 7c 7c 0a 20 20 20 20 20 20 20  le==0 ||.       
121ef 20 20 20 20 20 20 28 70 2d 3e 6c 6f 63 6b 65 64        (p->locked
121f0 20 26 26 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63   && p->wantToLoc
121f1 6b 20 26 26 20 73 71 6c 69 74 65 33 5f 6d 75 74  k && sqlite3_mut
121f2 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e  ex_held(p->pBt->
121f3 6d 75 74 65 78 29 29 29 3b 0a 7d 0a 23 65 6e 64  mutex)));.}.#end
121f4 69 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  if...#ifndef SQL
121f5 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f  ITE_OMIT_INCRBLO
121f6 42 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 61 6e  B./*.** Enter an
121f7 64 20 6c 65 61 76 65 20 61 20 6d 75 74 65 78 20  d leave a mutex 
121f8 6f 6e 20 61 20 42 74 72 65 65 20 67 69 76 65 6e  on a Btree given
121f9 20 61 20 63 75 72 73 6f 72 20 6f 77 6e 65 64 20   a cursor owned 
121fa 62 79 20 74 68 61 74 0a 2a 2a 20 42 74 72 65 65  by that.** Btree
121fb 2e 20 20 54 68 65 73 65 20 65 6e 74 72 79 20 70  .  These entry p
121fc 6f 69 6e 74 73 20 61 72 65 20 75 73 65 64 20 62  oints are used b
121fd 79 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 49 2f  y incremental I/
121fe 4f 20 61 6e 64 20 63 61 6e 20 62 65 0a 2a 2a 20  O and can be.** 
121ff 6f 6d 69 74 74 65 64 20 69 66 20 74 68 61 74 20  omitted if that 
12200 6d 6f 64 75 6c 65 20 69 73 20 6e 6f 74 20 75 73  module is not us
12201 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
12202 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
12203 65 33 42 74 72 65 65 45 6e 74 65 72 43 75 72 73  e3BtreeEnterCurs
12204 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  or(BtCursor *pCu
12205 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  r){.  sqlite3Btr
12206 65 65 45 6e 74 65 72 28 70 43 75 72 2d 3e 70 42  eeEnter(pCur->pB
12207 74 72 65 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  tree);.}.SQLITE_
12208 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
12209 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 75  ite3BtreeLeaveCu
1220a 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70  rsor(BtCursor *p
1220b 43 75 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 42  Cur){.  sqlite3B
1220c 74 72 65 65 4c 65 61 76 65 28 70 43 75 72 2d 3e  treeLeave(pCur->
1220d 70 42 74 72 65 65 29 3b 0a 7d 0a 23 65 6e 64 69  pBtree);.}.#endi
1220e 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1220f 5f 49 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a 0a 2f  _INCRBLOB */.../
12210 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d  *.** Enter the m
12211 75 74 65 78 20 6f 6e 20 65 76 65 72 79 20 42 74  utex on every Bt
12212 72 65 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  ree associated w
12213 69 74 68 20 61 20 64 61 74 61 62 61 73 65 0a 2a  ith a database.*
12214 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54  * connection.  T
12215 68 69 73 20 69 73 20 6e 65 65 64 65 64 20 28 66  his is needed (f
12216 6f 72 20 65 78 61 6d 70 6c 65 29 20 70 72 69 6f  or example) prio
12217 72 20 74 6f 20 70 61 72 73 69 6e 67 0a 2a 2a 20  r to parsing.** 
12218 61 20 73 74 61 74 65 6d 65 6e 74 20 73 69 6e 63  a statement sinc
12219 65 20 77 65 20 77 69 6c 6c 20 62 65 20 63 6f 6d  e we will be com
1221a 70 61 72 69 6e 67 20 74 61 62 6c 65 20 61 6e 64  paring table and
1221b 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 0a 2a 2a   column names.**
1221c 20 61 67 61 69 6e 73 74 20 61 6c 6c 20 73 63 68   against all sch
1221d 65 6d 61 73 20 61 6e 64 20 77 65 20 64 6f 20 6e  emas and we do n
1221e 6f 74 20 77 61 6e 74 20 74 68 6f 73 65 20 73 63  ot want those sc
1221f 68 65 6d 61 73 20 62 65 69 6e 67 0a 2a 2a 20 72  hemas being.** r
12220 65 73 65 74 20 6f 75 74 20 66 72 6f 6d 20 75 6e  eset out from un
12221 64 65 72 20 75 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  der us..**.** Th
12222 65 72 65 20 69 73 20 61 20 63 6f 72 72 65 73 70  ere is a corresp
12223 6f 6e 64 69 6e 67 20 6c 65 61 76 65 2d 61 6c 6c  onding leave-all
12224 20 70 72 6f 63 65 64 75 72 65 73 2e 0a 2a 2a 0a   procedures..**.
12225 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74  ** Enter the mut
12226 65 78 65 73 20 69 6e 20 61 63 63 65 6e 64 69 6e  exes in accendin
12227 67 20 6f 72 64 65 72 20 62 79 20 42 74 53 68 61  g order by BtSha
12228 72 65 64 20 70 6f 69 6e 74 65 72 20 61 64 64 72  red pointer addr
12229 65 73 73 0a 2a 2a 20 74 6f 20 61 76 6f 69 64 20  ess.** to avoid 
1222a 74 68 65 20 70 6f 73 73 69 62 69 6c 69 74 79 20  the possibility 
1222b 6f 66 20 64 65 61 64 6c 6f 63 6b 20 77 68 65 6e  of deadlock when
1222c 20 74 77 6f 20 74 68 72 65 61 64 73 20 77 69 74   two threads wit
1222d 68 0a 2a 2a 20 74 77 6f 20 6f 72 20 6d 6f 72 65  h.** two or more
1222e 20 62 74 72 65 65 73 20 69 6e 20 63 6f 6d 6d 6f   btrees in commo
1222f 6e 20 62 6f 74 68 20 74 72 79 20 74 6f 20 6c 6f  n both try to lo
12230 63 6b 20 61 6c 6c 20 74 68 65 69 72 20 62 74 72  ck all their btr
12231 65 65 73 0a 2a 2a 20 61 74 20 74 68 65 20 73 61  ees.** at the sa
12232 6d 65 20 69 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53  me instant..*/.S
12233 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
12234 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  id sqlite3BtreeE
12235 6e 74 65 72 41 6c 6c 28 73 71 6c 69 74 65 33 20  nterAll(sqlite3 
12236 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  *db){.  int i;. 
12237 20 42 74 72 65 65 20 2a 70 2c 20 2a 70 4c 61 74   Btree *p, *pLat
12238 65 72 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  er;.  assert( sq
12239 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1223a 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
1223b 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
1223c 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70  nDb; i++){.    p
1223d 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
1223e 74 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20  t;.    if( p && 
1223f 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20  p->sharable ){. 
12240 20 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f       p->wantToLo
12241 63 6b 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20  ck++;.      if( 
12242 21 70 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20  !p->locked ){.  
12243 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
12244 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 31 20 29  >wantToLock==1 )
12245 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  ;.        while(
12246 20 70 2d 3e 70 50 72 65 76 20 29 20 70 20 3d 20   p->pPrev ) p = 
12247 70 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20  p->pPrev;.      
12248 20 20 77 68 69 6c 65 28 20 70 2d 3e 6c 6f 63 6b    while( p->lock
12249 65 64 20 26 26 20 70 2d 3e 70 4e 65 78 74 20 29  ed && p->pNext )
1224a 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20   p = p->pNext;. 
1224b 20 20 20 20 20 20 20 66 6f 72 28 70 4c 61 74 65         for(pLate
1224c 72 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 20 70 4c  r = p->pNext; pL
1224d 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61  ater; pLater=pLa
1224e 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  ter->pNext){.   
1224f 20 20 20 20 20 20 20 69 66 28 20 70 4c 61 74 65         if( pLate
12250 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20  r->locked ){.   
12251 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
12252 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 4c 61  _mutex_leave(pLa
12253 74 65 72 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  ter->pBt->mutex)
12254 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4c  ;.            pL
12255 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30  ater->locked = 0
12256 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
12257 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
12258 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20  while( p ){.    
12259 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
1225a 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 42 74  tex_enter(p->pBt
1225b 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20  ->mutex);.      
1225c 20 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 2b 2b 3b      p->locked++;
1225d 0a 20 20 20 20 20 20 20 20 20 20 70 20 3d 20 70  .          p = p
1225e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20  ->pNext;.       
1225f 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
12260 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  .  }.}.SQLITE_PR
12261 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
12262 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28  e3BtreeLeaveAll(
12263 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
12264 69 6e 74 20 69 3b 0a 20 20 42 74 72 65 65 20 2a  int i;.  Btree *
12265 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  p;.  assert( sql
12266 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
12267 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
12268 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
12269 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20  Db; i++){.    p 
1226a 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74  = db->aDb[i].pBt
1226b 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 70  ;.    if( p && p
1226c 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20  ->sharable ){.  
1226d 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77      assert( p->w
1226e 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20  antToLock>0 );. 
1226f 20 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f       p->wantToLo
12270 63 6b 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 20  ck--;.      if( 
12271 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30  p->wantToLock==0
12272 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
12273 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 3b  rt( p->locked );
12274 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
12275 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e  _mutex_leave(p->
12276 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  pBt->mutex);.   
12277 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d       p->locked =
12278 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
12279 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66  }.  }.}..#ifndef
1227a 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65   NDEBUG./*.** Re
1227b 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65  turn true if the
1227c 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20   current thread 
1227d 68 6f 6c 64 73 20 74 68 65 20 64 61 74 61 62 61  holds the databa
1227e 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a  se connection.**
1227f 20 6d 75 74 65 78 20 61 6e 64 20 61 6c 6c 20 72   mutex and all r
12280 65 71 75 69 72 65 64 20 42 74 53 68 61 72 65 64  equired BtShared
12281 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20   mutexes..**.** 
12282 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
12283 75 73 65 64 20 69 6e 73 69 64 65 20 61 73 73 65  used inside asse
12284 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20  rt() statements 
12285 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  only..*/.SQLITE_
12286 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
12287 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c  te3BtreeHoldsAll
12288 4d 75 74 65 78 65 73 28 73 71 6c 69 74 65 33 20  Mutexes(sqlite3 
12289 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  *db){.  int i;. 
1228a 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f 6d 75   if( !sqlite3_mu
1228b 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
1228c 65 78 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ex) ){.    retur
1228d 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  n 0;.  }.  for(i
1228e 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
1228f 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a  ++){.    Btree *
12290 70 3b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61  p;.    p = db->a
12291 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69  Db[i].pBt;.    i
12292 66 28 20 70 20 26 26 20 70 2d 3e 73 68 61 72 61  f( p && p->shara
12293 62 6c 65 20 26 26 0a 20 20 20 20 20 20 20 20 20  ble &&.         
12294 28 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d  (p->wantToLock==
12295 30 20 7c 7c 20 21 73 71 6c 69 74 65 33 5f 6d 75  0 || !sqlite3_mu
12296 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d  tex_held(p->pBt-
12297 3e 6d 75 74 65 78 29 29 20 29 7b 0a 20 20 20 20  >mutex)) ){.    
12298 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
12299 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31  }.  }.  return 1
1229a 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44  ;.}.#endif /* ND
1229b 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41  EBUG */../*.** A
1229c 64 64 20 61 20 6e 65 77 20 42 74 72 65 65 20 70  dd a new Btree p
1229d 6f 69 6e 74 65 72 20 74 6f 20 61 20 42 74 72 65  ointer to a Btre
1229e 65 4d 75 74 65 78 41 72 72 61 79 2e 20 0a 2a 2a  eMutexArray. .**
1229f 20 69 66 20 74 68 65 20 70 6f 69 6e 74 65 72 20   if the pointer 
122a0 63 61 6e 20 70 6f 73 73 69 62 6c 79 20 62 65 20  can possibly be 
122a1 73 68 61 72 65 64 20 77 69 74 68 0a 2a 2a 20 61  shared with.** a
122a2 6e 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20  nother database 
122a3 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  connection..**.*
122a4 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 73 20 61  * The pointers a
122a5 72 65 20 6b 65 70 74 20 69 6e 20 73 6f 72 74 65  re kept in sorte
122a6 64 20 6f 72 64 65 72 20 62 79 20 70 42 74 72 65  d order by pBtre
122a7 65 2d 3e 70 42 74 2e 20 20 54 68 61 74 0a 2a 2a  e->pBt.  That.**
122a8 20 77 61 79 20 77 68 65 6e 20 77 65 20 67 6f 20   way when we go 
122a9 74 6f 20 65 6e 74 65 72 20 61 6c 6c 20 74 68 65  to enter all the
122aa 20 6d 75 74 65 78 65 73 2c 20 77 65 20 63 61 6e   mutexes, we can
122ab 20 65 6e 74 65 72 20 74 68 65 6d 0a 2a 2a 20 69   enter them.** i
122ac 6e 20 6f 72 64 65 72 20 77 69 74 68 6f 75 74 20  n order without 
122ad 65 76 65 72 79 20 68 61 76 69 6e 67 20 74 6f 20  every having to 
122ae 62 61 63 6b 75 70 20 61 6e 64 20 72 65 74 72 79  backup and retry
122af 20 61 6e 64 20 77 69 74 68 6f 75 74 0a 2a 2a 20   and without.** 
122b0 77 6f 72 72 79 69 6e 67 20 61 62 6f 75 74 20 64  worrying about d
122b1 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54  eadlock..**.** T
122b2 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 68 61  he number of sha
122b3 72 65 64 20 62 74 72 65 65 73 20 77 69 6c 6c 20  red btrees will 
122b4 61 6c 77 61 79 73 20 62 65 20 73 6d 61 6c 6c 20  always be small 
122b5 28 75 73 75 61 6c 6c 79 20 30 20 6f 72 20 31 29  (usually 0 or 1)
122b6 0a 2a 2a 20 73 6f 20 61 6e 20 69 6e 73 65 72 74  .** so an insert
122b7 69 6f 6e 20 73 6f 72 74 20 69 73 20 61 6e 20 61  ion sort is an a
122b8 64 65 71 75 61 74 65 20 61 6c 67 6f 72 69 74 68  dequate algorith
122b9 6d 20 68 65 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54  m here..*/.SQLIT
122ba 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
122bb 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78  qlite3BtreeMutex
122bc 41 72 72 61 79 49 6e 73 65 72 74 28 42 74 72 65  ArrayInsert(Btre
122bd 65 4d 75 74 65 78 41 72 72 61 79 20 2a 70 41 72  eMutexArray *pAr
122be 72 61 79 2c 20 42 74 72 65 65 20 2a 70 42 74 72  ray, Btree *pBtr
122bf 65 65 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  ee){.  int i, j;
122c0 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
122c1 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65 3d 3d  ;.  if( pBtree==
122c2 30 20 7c 7c 20 70 42 74 72 65 65 2d 3e 73 68 61  0 || pBtree->sha
122c3 72 61 62 6c 65 3d 3d 30 20 29 20 72 65 74 75 72  rable==0 ) retur
122c4 6e 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  n;.#ifndef NDEBU
122c5 47 0a 20 20 7b 0a 20 20 20 20 66 6f 72 28 69 3d  G.  {.    for(i=
122c6 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75  0; i<pArray->nMu
122c7 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  tex; i++){.     
122c8 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d   assert( pArray-
122c9 3e 61 42 74 72 65 65 5b 69 5d 21 3d 70 42 74 72  >aBtree[i]!=pBtr
122ca 65 65 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ee );.    }.  }.
122cb 23 65 6e 64 69 66 0a 20 20 61 73 73 65 72 74 28  #endif.  assert(
122cc 20 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3e   pArray->nMutex>
122cd 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
122ce 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3c 73  pArray->nMutex<s
122cf 69 7a 65 6f 66 28 70 41 72 72 61 79 2d 3e 61 42  izeof(pArray->aB
122d0 74 72 65 65 29 2f 73 69 7a 65 6f 66 28 70 41 72  tree)/sizeof(pAr
122d1 72 61 79 2d 3e 61 42 74 72 65 65 5b 30 5d 29 2d  ray->aBtree[0])-
122d2 31 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 42 74  1 );.  pBt = pBt
122d3 72 65 65 2d 3e 70 42 74 3b 0a 20 20 66 6f 72 28  ree->pBt;.  for(
122d4 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e  i=0; i<pArray->n
122d5 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20  Mutex; i++){.   
122d6 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d   assert( pArray-
122d7 3e 61 42 74 72 65 65 5b 69 5d 21 3d 70 42 74 72  >aBtree[i]!=pBtr
122d8 65 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 41  ee );.    if( pA
122d9 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 2d  rray->aBtree[i]-
122da 3e 70 42 74 3e 70 42 74 20 29 7b 0a 20 20 20 20  >pBt>pBt ){.    
122db 20 20 66 6f 72 28 6a 3d 70 41 72 72 61 79 2d 3e    for(j=pArray->
122dc 6e 4d 75 74 65 78 3b 20 6a 3e 69 3b 20 6a 2d 2d  nMutex; j>i; j--
122dd 29 7b 0a 20 20 20 20 20 20 20 20 70 41 72 72 61  ){.        pArra
122de 79 2d 3e 61 42 74 72 65 65 5b 6a 5d 20 3d 20 70  y->aBtree[j] = p
122df 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 6a 2d  Array->aBtree[j-
122e0 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  1];.      }.    
122e1 20 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65    pArray->aBtree
122e2 5b 69 5d 20 3d 20 70 42 74 72 65 65 3b 0a 20 20  [i] = pBtree;.  
122e3 20 20 20 20 70 41 72 72 61 79 2d 3e 6e 4d 75 74      pArray->nMut
122e4 65 78 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75  ex++;.      retu
122e5 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  rn;.    }.  }.  
122e6 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 70  pArray->aBtree[p
122e7 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 2b 2b 5d  Array->nMutex++]
122e8 20 3d 20 70 42 74 72 65 65 3b 0a 7d 0a 0a 2f 2a   = pBtree;.}../*
122e9 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75  .** Enter the mu
122ea 74 65 78 20 6f 66 20 65 76 65 72 79 20 62 74 72  tex of every btr
122eb 65 65 20 69 6e 20 74 68 65 20 61 72 72 61 79 2e  ee in the array.
122ec 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69    This routine i
122ed 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 74 20 74  s.** called at t
122ee 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
122ef 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28  sqlite3VdbeExec(
122f0 29 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73 20  ).  The mutexes 
122f1 61 72 65 0a 2a 2a 20 65 78 69 74 65 64 20 61 74  are.** exited at
122f2 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
122f3 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  same function..*
122f4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
122f5 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
122f6 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65  eeMutexArrayEnte
122f7 72 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61  r(BtreeMutexArra
122f8 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69 6e  y *pArray){.  in
122f9 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  t i;.  for(i=0; 
122fa 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78  i<pArray->nMutex
122fb 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65  ; i++){.    Btre
122fc 65 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e 61  e *p = pArray->a
122fd 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f 2a  Btree[i];.    /*
122fe 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e 69   Some basic sani
122ff 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20  ty checking */. 
12300 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30 20     assert( i==0 
12301 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65  || pArray->aBtre
12302 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e 70  e[i-1]->pBt<p->p
12303 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  Bt );.    assert
12304 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20  ( !p->locked || 
12305 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20  p->wantToLock>0 
12306 29 3b 0a 0a 20 20 20 20 2f 2a 20 57 65 20 73 68  );..    /* We sh
12307 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 6f 6c  ould already hol
12308 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  d a lock on the 
12309 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1230a 69 6f 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  ion */.    asser
1230b 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1230c 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74  _held(p->db->mut
1230d 65 78 29 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 77  ex) );..    p->w
1230e 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20  antToLock++;.   
1230f 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20   if( !p->locked 
12310 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29  && p->sharable )
12311 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
12312 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70  mutex_enter(p->p
12313 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20  Bt->mutex);.    
12314 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b    p->locked = 1;
12315 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
12316 0a 2a 2a 20 4c 65 61 76 65 20 74 68 65 20 6d 75  .** Leave the mu
12317 74 65 78 20 6f 66 20 65 76 65 72 79 20 62 74 72  tex of every btr
12318 65 65 20 69 6e 20 74 68 65 20 67 72 6f 75 70 2e  ee in the group.
12319 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1231a 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
1231b 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65  treeMutexArrayLe
1231c 61 76 65 28 42 74 72 65 65 4d 75 74 65 78 41 72  ave(BtreeMutexAr
1231d 72 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 20  ray *pArray){.  
1231e 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30  int i;.  for(i=0
1231f 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74  ; i<pArray->nMut
12320 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74  ex; i++){.    Bt
12321 72 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79 2d  ree *p = pArray-
12322 3e 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20  >aBtree[i];.    
12323 2f 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61  /* Some basic sa
12324 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f  nity checking */
12325 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d  .    assert( i==
12326 30 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 74  0 || pArray->aBt
12327 72 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d  ree[i-1]->pBt<p-
12328 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65  >pBt );.    asse
12329 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c  rt( p->locked ||
1232a 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 3b   !p->sharable );
1232b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
1232c 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a  wantToLock>0 );.
1232d 0a 20 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c  .    /* We shoul
1232e 64 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61  d already hold a
1232f 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74   lock on the dat
12330 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
12331 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
12332 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
12333 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ld(p->db->mutex)
12334 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 77 61 6e 74   );..    p->want
12335 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 69 66  ToLock--;.    if
12336 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d  ( p->wantToLock=
12337 3d 30 20 26 26 20 70 2d 3e 6c 6f 63 6b 65 64 20  =0 && p->locked 
12338 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
12339 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e  _mutex_leave(p->
1233a 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  pBt->mutex);.   
1233b 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30     p->locked = 0
1233c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a  ;.    }.  }.}...
1233d 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54  #endif  /* SQLIT
1233e 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20  E_THREADSAFE && 
1233f 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41  !SQLITE_OMIT_SHA
12340 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 2f 2a  RED_CACHE */../*
12341 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
12342 64 20 6f 66 20 62 74 6d 75 74 65 78 2e 63 20 2a  d of btmutex.c *
12343 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12344 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12345 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
12346 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
12347 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65 2e 63  gin file btree.c
12348 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
12349 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1234a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1234b 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20 36  .** 2004 April 6
1234c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
1234d 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
1234e 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
1234f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
12350 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
12351 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
12352 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
12353 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
12354 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
12355 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
12356 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
12357 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
12358 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
12359 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
1235a 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
1235b 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
1235c 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
1235d 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
1235e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1235f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12361 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
12362 2a 2a 20 24 49 64 3a 20 62 74 72 65 65 2e 63 2c  ** $Id: btree.c,
12363 76 20 31 2e 35 32 37 20 32 30 30 38 2f 31 31 2f  v 1.527 2008/11/
12364 30 33 20 32 30 3a 35 35 3a 30 37 20 64 72 68 20  03 20:55:07 drh 
12365 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73  Exp $.**.** This
12366 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73   file implements
12367 20 61 20 65 78 74 65 72 6e 61 6c 20 28 64 69 73   a external (dis
12368 6b 2d 62 61 73 65 64 29 20 64 61 74 61 62 61 73  k-based) databas
12369 65 20 75 73 69 6e 67 20 42 54 72 65 65 73 2e 0a  e using BTrees..
1236a 2a 2a 20 53 65 65 20 74 68 65 20 68 65 61 64 65  ** See the heade
1236b 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 22 62 74  r comment on "bt
1236c 72 65 65 49 6e 74 2e 68 22 20 66 6f 72 20 61 64  reeInt.h" for ad
1236d 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
1236e 74 69 6f 6e 2e 0a 2a 2a 20 49 6e 63 6c 75 64 69  tion..** Includi
1236f 6e 67 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e  ng a description
12370 20 6f 66 20 66 69 6c 65 20 66 6f 72 6d 61 74 20   of file format 
12371 61 6e 64 20 61 6e 20 6f 76 65 72 76 69 65 77 20  and an overview 
12372 6f 66 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f  of operation..*/
12373 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61 64  ../*.** The head
12374 65 72 20 73 74 72 69 6e 67 20 74 68 61 74 20 61  er string that a
12375 70 70 65 61 72 73 20 61 74 20 74 68 65 20 62 65  ppears at the be
12376 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79  ginning of every
12377 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62  .** SQLite datab
12378 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ase..*/.static c
12379 6f 6e 73 74 20 63 68 61 72 20 7a 4d 61 67 69 63  onst char zMagic
1237a 48 65 61 64 65 72 5b 5d 20 3d 20 53 51 4c 49 54  Header[] = SQLIT
1237b 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 3b 0a 0a  E_FILE_HEADER;..
1237c 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 69 73 20 67  /*.** Set this g
1237d 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 74  lobal variable t
1237e 6f 20 31 20 74 6f 20 65 6e 61 62 6c 65 20 74 72  o 1 to enable tr
1237f 61 63 69 6e 67 20 75 73 69 6e 67 20 74 68 65 20  acing using the 
12380 54 52 41 43 45 0a 2a 2a 20 6d 61 63 72 6f 2e 0a  TRACE.** macro..
12381 2a 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73 71 6c  */.#if 0.int sql
12382 69 74 65 33 42 74 72 65 65 54 72 61 63 65 3d 30  ite3BtreeTrace=0
12383 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e  ;  /* True to en
12384 61 62 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f 0a  able tracing */.
12385 23 20 64 65 66 69 6e 65 20 54 52 41 43 45 28 58  # define TRACE(X
12386 29 20 20 69 66 28 73 71 6c 69 74 65 33 42 74 72  )  if(sqlite3Btr
12387 65 65 54 72 61 63 65 29 7b 70 72 69 6e 74 66 20  eeTrace){printf 
12388 58 3b 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29  X;fflush(stdout)
12389 3b 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  ;}.#else.# defin
1238a 65 20 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69  e TRACE(X).#endi
1238b 66 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d  f../*.** Sometim
1238c 65 73 20 77 65 20 6e 65 65 64 20 61 20 73 6d 61  es we need a sma
1238d 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 63 6f 64  ll amount of cod
1238e 65 20 73 75 63 68 20 61 73 20 61 20 76 61 72 69  e such as a vari
1238f 61 62 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61 74  able initializat
12390 69 6f 6e 0a 2a 2a 20 74 6f 20 73 65 74 75 70 20  ion.** to setup 
12391 66 6f 72 20 61 20 6c 61 74 65 72 20 61 73 73 65  for a later asse
12392 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 2e 20  rt() statement. 
12393 20 57 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20   We do not want 
12394 74 68 69 73 20 63 6f 64 65 20 74 6f 0a 2a 2a 20  this code to.** 
12395 61 70 70 65 61 72 20 77 68 65 6e 20 61 73 73 65  appear when asse
12396 72 74 28 29 20 69 73 20 64 69 73 61 62 6c 65 64  rt() is disabled
12397 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
12398 20 6d 61 63 72 6f 20 69 73 20 74 68 65 72 65 66   macro is theref
12399 6f 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 63  ore.** used to c
1239a 6f 6e 74 61 69 6e 20 74 68 61 74 20 73 65 74 75  ontain that setu
1239b 70 20 63 6f 64 65 2e 20 20 54 68 65 20 22 56 56  p code.  The "VV
1239c 41 22 20 61 63 72 6f 6e 79 6d 20 73 74 61 6e 64  A" acronym stand
1239d 73 20 66 6f 72 0a 2a 2a 20 22 56 65 72 69 66 69  s for.** "Verifi
1239e 63 61 74 69 6f 6e 2c 20 56 61 6c 69 64 61 74 69  cation, Validati
1239f 6f 6e 2c 20 61 6e 64 20 41 63 63 72 65 64 69 74  on, and Accredit
123a0 61 74 69 6f 6e 22 2e 20 20 49 6e 20 6f 74 68 65  ation".  In othe
123a1 72 20 77 6f 72 64 73 2c 20 74 68 65 0a 2a 2a 20  r words, the.** 
123a2 63 6f 64 65 20 77 69 74 68 69 6e 20 56 56 41 5f  code within VVA_
123a3 4f 4e 4c 59 28 29 20 77 69 6c 6c 20 6f 6e 6c 79  ONLY() will only
123a4 20 72 75 6e 20 64 75 72 69 6e 67 20 76 65 72 69   run during veri
123a5 66 69 63 61 74 69 6f 6e 20 70 72 6f 63 65 73 73  fication process
123a6 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e  es..*/.#ifndef N
123a7 44 45 42 55 47 0a 23 20 64 65 66 69 6e 65 20 56  DEBUG.# define V
123a8 56 41 5f 4f 4e 4c 59 28 58 29 20 20 58 0a 23 65  VA_ONLY(X)  X.#e
123a9 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 56 56 41  lse.# define VVA
123aa 5f 4f 4e 4c 59 28 58 29 0a 23 65 6e 64 69 66 0a  _ONLY(X).#endif.
123ab 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
123ac 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
123ad 43 48 45 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74  CHE./*.** A list
123ae 20 6f 66 20 42 74 53 68 61 72 65 64 20 6f 62 6a   of BtShared obj
123af 65 63 74 73 20 74 68 61 74 20 61 72 65 20 65 6c  ects that are el
123b0 69 67 69 62 6c 65 20 66 6f 72 20 70 61 72 74 69  igible for parti
123b1 63 69 70 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20 73  cipation.** in s
123b2 68 61 72 65 64 20 63 61 63 68 65 2e 20 20 54 68  hared cache.  Th
123b3 69 73 20 76 61 72 69 61 62 6c 65 20 68 61 73 20  is variable has 
123b4 66 69 6c 65 20 73 63 6f 70 65 20 64 75 72 69 6e  file scope durin
123b5 67 20 6e 6f 72 6d 61 6c 20 62 75 69 6c 64 73 2c  g normal builds,
123b6 0a 2a 2a 20 62 75 74 20 74 68 65 20 74 65 73 74  .** but the test
123b7 20 68 61 72 6e 65 73 73 20 6e 65 65 64 73 20 74   harness needs t
123b8 6f 20 61 63 63 65 73 73 20 69 74 20 73 6f 20 77  o access it so w
123b9 65 20 6d 61 6b 65 20 69 74 20 67 6c 6f 62 61 6c  e make it global
123ba 20 66 6f 72 20 0a 2a 2a 20 74 65 73 74 20 62 75   for .** test bu
123bb 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  ilds..*/.#ifdef 
123bc 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49  SQLITE_TEST.SQLI
123bd 54 45 5f 50 52 49 56 41 54 45 20 42 74 53 68 61  TE_PRIVATE BtSha
123be 72 65 64 20 2a 53 51 4c 49 54 45 5f 57 53 44 20  red *SQLITE_WSD 
123bf 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 63  sqlite3SharedCac
123c0 68 65 4c 69 73 74 20 3d 20 30 3b 0a 23 65 6c 73  heList = 0;.#els
123c1 65 0a 73 74 61 74 69 63 20 42 74 53 68 61 72 65  e.static BtShare
123c2 64 20 2a 53 51 4c 49 54 45 5f 57 53 44 20 73 71  d *SQLITE_WSD sq
123c3 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65  lite3SharedCache
123c4 4c 69 73 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66  List = 0;.#endif
123c5 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
123c6 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
123c7 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  CHE */..#ifndef 
123c8 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
123c9 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 45  ED_CACHE./*.** E
123ca 6e 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c 65  nable or disable
123cb 20 74 68 65 20 73 68 61 72 65 64 20 70 61 67 65   the shared page
123cc 72 20 61 6e 64 20 73 63 68 65 6d 61 20 66 65 61  r and schema fea
123cd 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  tures..**.** Thi
123ce 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 6e 6f  s routine has no
123cf 20 65 66 66 65 63 74 20 6f 6e 20 65 78 69 73 74   effect on exist
123d0 69 6e 67 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ing database con
123d1 6e 65 63 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65  nections..** The
123d2 20 73 68 61 72 65 64 20 63 61 63 68 65 20 73 65   shared cache se
123d3 74 74 69 6e 67 20 65 66 66 65 63 74 73 20 6f 6e  tting effects on
123d4 6c 79 20 66 75 74 75 72 65 20 63 61 6c 6c 73 20  ly future calls 
123d5 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70  to.** sqlite3_op
123d6 65 6e 28 29 2c 20 73 71 6c 69 74 65 33 5f 6f 70  en(), sqlite3_op
123d7 65 6e 31 36 28 29 2c 20 6f 72 20 73 71 6c 69 74  en16(), or sqlit
123d8 65 33 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2f  e3_open_v2()..*/
123d9 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
123da 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73  sqlite3_enable_s
123db 68 61 72 65 64 5f 63 61 63 68 65 28 69 6e 74 20  hared_cache(int 
123dc 65 6e 61 62 6c 65 29 7b 0a 20 20 73 71 6c 69 74  enable){.  sqlit
123dd 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73  e3GlobalConfig.s
123de 68 61 72 65 64 43 61 63 68 65 45 6e 61 62 6c 65  haredCacheEnable
123df 64 20 3d 20 65 6e 61 62 6c 65 3b 0a 20 20 72 65  d = enable;.  re
123e0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
123e1 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  }.#endif.../*.**
123e2 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61   Forward declara
123e3 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  tion.*/.static i
123e4 6e 74 20 63 68 65 63 6b 52 65 61 64 4c 6f 63 6b  nt checkReadLock
123e5 73 28 42 74 72 65 65 2a 2c 20 50 67 6e 6f 2c 20  s(Btree*, Pgno, 
123e6 42 74 43 75 72 73 6f 72 2a 2c 20 69 36 34 29 3b  BtCursor*, i64);
123e7 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ...#ifdef SQLITE
123e8 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
123e9 48 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65  HE.  /*.  ** The
123ea 20 66 75 6e 63 74 69 6f 6e 73 20 71 75 65 72 79   functions query
123eb 54 61 62 6c 65 4c 6f 63 6b 28 29 2c 20 6c 6f 63  TableLock(), loc
123ec 6b 54 61 62 6c 65 28 29 20 61 6e 64 20 75 6e 6c  kTable() and unl
123ed 6f 63 6b 41 6c 6c 54 61 62 6c 65 73 28 29 0a 20  ockAllTables(). 
123ee 20 2a 2a 20 6d 61 6e 69 70 75 6c 61 74 65 20 65   ** manipulate e
123ef 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 42 74  ntries in the Bt
123f0 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c 69 6e  Shared.pLock lin
123f1 6b 65 64 20 6c 69 73 74 20 75 73 65 64 20 74 6f  ked list used to
123f2 20 73 74 6f 72 65 0a 20 20 2a 2a 20 73 68 61 72   store.  ** shar
123f3 65 64 2d 63 61 63 68 65 20 74 61 62 6c 65 20 6c  ed-cache table l
123f4 65 76 65 6c 20 6c 6f 63 6b 73 2e 20 49 66 20 74  evel locks. If t
123f5 68 65 20 6c 69 62 72 61 72 79 20 69 73 20 63 6f  he library is co
123f6 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a  mpiled with the.
123f7 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68    ** shared-cach
123f8 65 20 66 65 61 74 75 72 65 20 64 69 73 61 62 6c  e feature disabl
123f9 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69  ed, then there i
123fa 73 20 6f 6e 6c 79 20 65 76 65 72 20 6f 6e 65 20  s only ever one 
123fb 75 73 65 72 0a 20 20 2a 2a 20 6f 66 20 65 61 63  user.  ** of eac
123fc 68 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63  h BtShared struc
123fd 74 75 72 65 20 61 6e 64 20 73 6f 20 74 68 69 73  ture and so this
123fe 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6e 6f 74 20   locking is not 
123ff 6e 65 63 65 73 73 61 72 79 2e 20 0a 20 20 2a 2a  necessary. .  **
12400 20 53 6f 20 64 65 66 69 6e 65 20 74 68 65 20 6c   So define the l
12401 6f 63 6b 20 72 65 6c 61 74 65 64 20 66 75 6e 63  ock related func
12402 74 69 6f 6e 73 20 61 73 20 6e 6f 2d 6f 70 73 2e  tions as no-ops.
12403 0a 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20  .  */.  #define 
12404 71 75 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 61  queryTableLock(a
12405 2c 62 2c 63 29 20 53 51 4c 49 54 45 5f 4f 4b 0a  ,b,c) SQLITE_OK.
12406 20 20 23 64 65 66 69 6e 65 20 6c 6f 63 6b 54 61    #define lockTa
12407 62 6c 65 28 61 2c 62 2c 63 29 20 53 51 4c 49 54  ble(a,b,c) SQLIT
12408 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 75  E_OK.  #define u
12409 6e 6c 6f 63 6b 41 6c 6c 54 61 62 6c 65 73 28 61  nlockAllTables(a
1240a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  ).#endif..#ifnde
1240b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
1240c 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a  ARED_CACHE./*.**
1240d 20 51 75 65 72 79 20 74 6f 20 73 65 65 20 69 66   Query to see if
1240e 20 62 74 72 65 65 20 68 61 6e 64 6c 65 20 70 20   btree handle p 
1240f 6d 61 79 20 6f 62 74 61 69 6e 20 61 20 6c 6f 63  may obtain a loc
12410 6b 20 6f 66 20 74 79 70 65 20 65 4c 6f 63 6b 20  k of type eLock 
12411 0a 2a 2a 20 28 52 45 41 44 5f 4c 4f 43 4b 20 6f  .** (READ_LOCK o
12412 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 6f 6e  r WRITE_LOCK) on
12413 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 20   the table with 
12414 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 2e 20  root-page iTab. 
12415 52 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45  Return.** SQLITE
12416 5f 4f 4b 20 69 66 20 74 68 65 20 6c 6f 63 6b 20  _OK if the lock 
12417 6d 61 79 20 62 65 20 6f 62 74 61 69 6e 65 64 20  may be obtained 
12418 28 62 79 20 63 61 6c 6c 69 6e 67 20 6c 6f 63 6b  (by calling lock
12419 54 61 62 6c 65 28 29 29 2c 20 6f 72 0a 2a 2a 20  Table()), or.** 
1241a 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66  SQLITE_LOCKED if
1241b 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   not..*/.static 
1241c 69 6e 74 20 71 75 65 72 79 54 61 62 6c 65 4c 6f  int queryTableLo
1241d 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e  ck(Btree *p, Pgn
1241e 6f 20 69 54 61 62 2c 20 75 38 20 65 4c 6f 63 6b  o iTab, u8 eLock
1241f 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  ){.  BtShared *p
12420 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42  Bt = p->pBt;.  B
12421 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 0a 20  tLock *pIter;.. 
12422 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
12423 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28  BtreeHoldsMutex(
12424 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  p) );.  assert( 
12425 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b  eLock==READ_LOCK
12426 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45   || eLock==WRITE
12427 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72  _LOCK );.  asser
12428 74 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b 0a 20  t( p->db!=0 );. 
12429 20 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61   .  /* This is a
1242a 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 73 68   no-op if the sh
1242b 61 72 65 64 2d 63 61 63 68 65 20 69 73 20 6e 6f  ared-cache is no
1242c 74 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 69  t enabled */.  i
1242d 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20  f( !p->sharable 
1242e 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
1242f 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
12430 2f 2a 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72  /* If some other
12431 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68   connection is h
12432 6f 6c 64 69 6e 67 20 61 6e 20 65 78 63 6c 75 73  olding an exclus
12433 69 76 65 20 6c 6f 63 6b 2c 20 74 68 65 0a 20 20  ive lock, the.  
12434 2a 2a 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63  ** requested loc
12435 6b 20 6d 61 79 20 6e 6f 74 20 62 65 20 6f 62 74  k may not be obt
12436 61 69 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  ained..  */.  if
12437 28 20 70 42 74 2d 3e 70 45 78 63 6c 75 73 69 76  ( pBt->pExclusiv
12438 65 20 26 26 20 70 42 74 2d 3e 70 45 78 63 6c 75  e && pBt->pExclu
12439 73 69 76 65 21 3d 70 20 29 7b 0a 20 20 20 20 72  sive!=p ){.    r
1243a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43  eturn SQLITE_LOC
1243b 4b 45 44 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54  KED;.  }..  /* T
1243c 68 69 73 20 28 61 6c 6f 6e 67 20 77 69 74 68 20  his (along with 
1243d 6c 6f 63 6b 54 61 62 6c 65 28 29 29 20 69 73 20  lockTable()) is 
1243e 77 68 65 72 65 20 74 68 65 20 52 65 61 64 55 6e  where the ReadUn
1243f 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 69  committed flag i
12440 73 0a 20 20 2a 2a 20 64 65 61 6c 74 20 77 69 74  s.  ** dealt wit
12441 68 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72  h. If the caller
12442 20 69 73 20 71 75 65 72 79 69 6e 67 20 66 6f 72   is querying for
12443 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 61 6e 64   a read-lock and
12444 20 74 68 65 20 66 6c 61 67 20 69 73 0a 20 20 2a   the flag is.  *
12445 2a 20 73 65 74 2c 20 69 74 20 69 73 20 75 6e 63  * set, it is unc
12446 6f 6e 64 69 74 69 6f 6e 61 6c 6c 79 20 67 72 61  onditionally gra
12447 6e 74 65 64 20 2d 20 65 76 65 6e 20 69 66 20 74  nted - even if t
12448 68 65 72 65 20 61 72 65 20 77 72 69 74 65 2d 6c  here are write-l
12449 6f 63 6b 73 0a 20 20 2a 2a 20 6f 6e 20 74 68 65  ocks.  ** on the
1244a 20 74 61 62 6c 65 2e 20 49 66 20 61 20 77 72 69   table. If a wri
1244b 74 65 2d 6c 6f 63 6b 20 69 73 20 72 65 71 75 65  te-lock is reque
1244c 73 74 65 64 2c 20 74 68 65 20 52 65 61 64 55 6e  sted, the ReadUn
1244d 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 0a 20  committed flag. 
1244e 20 2a 2a 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69   ** is not consi
1244f 64 65 72 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  dered..  **.  **
12450 20 49 6e 20 66 75 6e 63 74 69 6f 6e 20 6c 6f 63   In function loc
12451 6b 54 61 62 6c 65 28 29 2c 20 69 66 20 61 20 72  kTable(), if a r
12452 65 61 64 2d 6c 6f 63 6b 20 69 73 20 64 65 6d 61  ead-lock is dema
12453 6e 64 65 64 20 61 6e 64 20 74 68 65 20 0a 20 20  nded and the .  
12454 2a 2a 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74  ** ReadUncommitt
12455 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20  ed flag is set, 
12456 6e 6f 20 65 6e 74 72 79 20 69 73 20 61 64 64 65  no entry is adde
12457 64 20 74 6f 20 74 68 65 20 6c 6f 63 6b 73 20 6c  d to the locks l
12458 69 73 74 20 0a 20 20 2a 2a 20 28 42 74 53 68 61  ist .  ** (BtSha
12459 72 65 64 2e 70 4c 6f 63 6b 29 2e 0a 20 20 2a 2a  red.pLock)..  **
1245a 0a 20 20 2a 2a 20 54 6f 20 73 75 6d 6d 61 72 69  .  ** To summari
1245b 7a 65 3a 20 49 66 20 74 68 65 20 52 65 61 64 55  ze: If the ReadU
1245c 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20  ncommitted flag 
1245d 69 73 20 73 65 74 2c 20 74 68 65 6e 20 72 65 61  is set, then rea
1245e 64 20 63 75 72 73 6f 72 73 20 64 6f 0a 20 20 2a  d cursors do.  *
1245f 2a 20 6e 6f 74 20 63 72 65 61 74 65 20 6f 72 20  * not create or 
12460 72 65 73 70 65 63 74 20 74 61 62 6c 65 20 6c 6f  respect table lo
12461 63 6b 73 2e 20 54 68 65 20 6c 6f 63 6b 69 6e 67  cks. The locking
12462 20 70 72 6f 63 65 64 75 72 65 20 66 6f 72 20 61   procedure for a
12463 20 0a 20 20 2a 2a 20 77 72 69 74 65 2d 63 75 72   .  ** write-cur
12464 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20 63 68 61  sor does not cha
12465 6e 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  nge..  */.  if( 
12466 0a 20 20 20 20 30 3d 3d 28 70 2d 3e 64 62 2d 3e  .    0==(p->db->
12467 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61  flags&SQLITE_Rea
12468 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 20 7c 7c  dUncommitted) ||
12469 20 0a 20 20 20 20 65 4c 6f 63 6b 3d 3d 57 52 49   .    eLock==WRI
1246a 54 45 5f 4c 4f 43 4b 20 7c 7c 0a 20 20 20 20 69  TE_LOCK ||.    i
1246b 54 61 62 3d 3d 4d 41 53 54 45 52 5f 52 4f 4f 54  Tab==MASTER_ROOT
1246c 0a 20 20 29 7b 0a 20 20 20 20 66 6f 72 28 70 49  .  ){.    for(pI
1246d 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20  ter=pBt->pLock; 
1246e 70 49 74 65 72 3b 20 70 49 74 65 72 3d 70 49 74  pIter; pIter=pIt
1246f 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  er->pNext){.    
12470 20 20 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74    if( pIter->pBt
12471 72 65 65 21 3d 70 20 26 26 20 70 49 74 65 72 2d  ree!=p && pIter-
12472 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 26  >iTable==iTab &&
12473 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 49 74   .          (pIt
12474 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f 63 6b  er->eLock!=eLock
12475 20 7c 7c 20 65 4c 6f 63 6b 21 3d 52 45 41 44 5f   || eLock!=READ_
12476 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 20 20 20 20  LOCK) ){.       
12477 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c   return SQLITE_L
12478 4f 43 4b 45 44 3b 0a 20 20 20 20 20 20 7d 0a 20  OCKED;.      }. 
12479 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
1247a 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23  n SQLITE_OK;.}.#
1247b 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45  endif /* !SQLITE
1247c 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
1247d 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  HE */..#ifndef S
1247e 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
1247f 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 64  D_CACHE./*.** Ad
12480 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  d a lock on the 
12481 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d  table with root-
12482 70 61 67 65 20 69 54 61 62 6c 65 20 74 6f 20 74  page iTable to t
12483 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 20  he shared-btree 
12484 75 73 65 64 0a 2a 2a 20 62 79 20 42 74 72 65 65  used.** by Btree
12485 20 68 61 6e 64 6c 65 20 70 2e 20 50 61 72 61 6d   handle p. Param
12486 65 74 65 72 20 65 4c 6f 63 6b 20 6d 75 73 74 20  eter eLock must 
12487 62 65 20 65 69 74 68 65 72 20 52 45 41 44 5f 4c  be either READ_L
12488 4f 43 4b 20 6f 72 20 0a 2a 2a 20 57 52 49 54 45  OCK or .** WRITE
12489 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c  _LOCK..**.** SQL
1248a 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
1248b 65 64 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 69  ed if the lock i
1248c 73 20 61 64 64 65 64 20 73 75 63 63 65 73 73 66  s added successf
1248d 75 6c 6c 79 2e 20 53 51 4c 49 54 45 5f 42 55 53  ully. SQLITE_BUS
1248e 59 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f  Y and.** SQLITE_
1248f 4e 4f 4d 45 4d 20 6d 61 79 20 61 6c 73 6f 20 62  NOMEM may also b
12490 65 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73  e returned..*/.s
12491 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 54 61  tatic int lockTa
12492 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 50 67  ble(Btree *p, Pg
12493 6e 6f 20 69 54 61 62 6c 65 2c 20 75 38 20 65 4c  no iTable, u8 eL
12494 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 72 65 64  ock){.  BtShared
12495 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
12496 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20    BtLock *pLock 
12497 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70  = 0;.  BtLock *p
12498 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 28  Iter;..  assert(
12499 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
1249a 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20  dsMutex(p) );.  
1249b 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52  assert( eLock==R
1249c 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f 63  EAD_LOCK || eLoc
1249d 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b  k==WRITE_LOCK );
1249e 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62  .  assert( p->db
1249f 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69  !=0 );..  /* Thi
124a0 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20  s is a no-op if 
124a1 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 65  the shared-cache
124a2 20 69 73 20 6e 6f 74 20 65 6e 61 62 6c 65 64 20   is not enabled 
124a3 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 61  */.  if( !p->sha
124a4 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 72 65 74  rable ){.    ret
124a5 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
124a6 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 53 51   }..  assert( SQ
124a7 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 54 61  LITE_OK==queryTa
124a8 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 6c  bleLock(p, iTabl
124a9 65 2c 20 65 4c 6f 63 6b 29 20 29 3b 0a 0a 20 20  e, eLock) );..  
124aa 2f 2a 20 49 66 20 74 68 65 20 72 65 61 64 2d 75  /* If the read-u
124ab 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20  ncommitted flag 
124ac 69 73 20 73 65 74 20 61 6e 64 20 61 20 72 65 61  is set and a rea
124ad 64 2d 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73  d-lock is reques
124ae 74 65 64 2c 0a 20 20 2a 2a 20 72 65 74 75 72 6e  ted,.  ** return
124af 20 65 61 72 6c 79 20 77 69 74 68 6f 75 74 20 61   early without a
124b0 64 64 69 6e 67 20 61 6e 20 65 6e 74 72 79 20 74  dding an entry t
124b1 6f 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70  o the BtShared.p
124b2 4c 6f 63 6b 20 6c 69 73 74 2e 20 53 65 65 0a 20  Lock list. See. 
124b3 20 2a 2a 20 63 6f 6d 6d 65 6e 74 20 69 6e 20 66   ** comment in f
124b4 75 6e 63 74 69 6f 6e 20 71 75 65 72 79 54 61 62  unction queryTab
124b5 6c 65 4c 6f 63 6b 28 29 20 66 6f 72 20 6d 6f 72  leLock() for mor
124b6 65 20 69 6e 66 6f 20 6f 6e 20 68 61 6e 64 6c 69  e info on handli
124b7 6e 67 20 0a 20 20 2a 2a 20 74 68 65 20 52 65 61  ng .  ** the Rea
124b8 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61  dUncommitted fla
124b9 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 0a 20  g..  */.  if( . 
124ba 20 20 20 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73     (p->db->flags
124bb 26 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f  &SQLITE_ReadUnco
124bc 6d 6d 69 74 74 65 64 29 20 26 26 20 0a 20 20 20  mmitted) && .   
124bd 20 28 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f   (eLock==READ_LO
124be 43 4b 29 20 26 26 0a 20 20 20 20 69 54 61 62 6c  CK) &&.    iTabl
124bf 65 21 3d 4d 41 53 54 45 52 5f 52 4f 4f 54 0a 20  e!=MASTER_ROOT. 
124c0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
124c1 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20  QLITE_OK;.  }.. 
124c2 20 2f 2a 20 46 69 72 73 74 20 73 65 61 72 63 68   /* First search
124c3 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20 61 6e   the list for an
124c4 20 65 78 69 73 74 69 6e 67 20 6c 6f 63 6b 20 6f   existing lock o
124c5 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f  n this table. */
124c6 0a 20 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74  .  for(pIter=pBt
124c7 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20  ->pLock; pIter; 
124c8 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65  pIter=pIter->pNe
124c9 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74  xt){.    if( pIt
124ca 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62  er->iTable==iTab
124cb 6c 65 20 26 26 20 70 49 74 65 72 2d 3e 70 42 74  le && pIter->pBt
124cc 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20  ree==p ){.      
124cd 70 4c 6f 63 6b 20 3d 20 70 49 74 65 72 3b 0a 20  pLock = pIter;. 
124ce 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
124cf 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  }.  }..  /* If t
124d0 68 65 20 61 62 6f 76 65 20 73 65 61 72 63 68 20  he above search 
124d1 64 69 64 20 6e 6f 74 20 66 69 6e 64 20 61 20 42  did not find a B
124d2 74 4c 6f 63 6b 20 73 74 72 75 63 74 20 61 73 73  tLock struct ass
124d3 6f 63 69 61 74 69 6e 67 20 42 74 72 65 65 20 70  ociating Btree p
124d4 0a 20 20 2a 2a 20 77 69 74 68 20 74 61 62 6c 65  .  ** with table
124d5 20 69 54 61 62 6c 65 2c 20 61 6c 6c 6f 63 61 74   iTable, allocat
124d6 65 20 6f 6e 65 20 61 6e 64 20 6c 69 6e 6b 20 69  e one and link i
124d7 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74 2e  t into the list.
124d8 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 4c 6f  .  */.  if( !pLo
124d9 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 20  ck ){.    pLock 
124da 3d 20 28 42 74 4c 6f 63 6b 20 2a 29 73 71 6c 69  = (BtLock *)sqli
124db 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69  te3MallocZero(si
124dc 7a 65 6f 66 28 42 74 4c 6f 63 6b 29 29 3b 0a 20  zeof(BtLock));. 
124dd 20 20 20 69 66 28 20 21 70 4c 6f 63 6b 20 29 7b     if( !pLock ){
124de 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
124df 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
124e0 7d 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 69 54 61  }.    pLock->iTa
124e1 62 6c 65 20 3d 20 69 54 61 62 6c 65 3b 0a 20 20  ble = iTable;.  
124e2 20 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 20    pLock->pBtree 
124e3 3d 20 70 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e  = p;.    pLock->
124e4 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 4c 6f  pNext = pBt->pLo
124e5 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e 70 4c 6f  ck;.    pBt->pLo
124e6 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d 0a  ck = pLock;.  }.
124e7 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 42 74  .  /* Set the Bt
124e8 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 76 61 72 69 61  Lock.eLock varia
124e9 62 6c 65 20 74 6f 20 74 68 65 20 6d 61 78 69 6d  ble to the maxim
124ea 75 6d 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  um of the curren
124eb 74 20 6c 6f 63 6b 0a 20 20 2a 2a 20 61 6e 64 20  t lock.  ** and 
124ec 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f  the requested lo
124ed 63 6b 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 69  ck. This means i
124ee 66 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 77  f a write-lock w
124ef 61 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 0a  as already held.
124f0 20 20 2a 2a 20 61 6e 64 20 61 20 72 65 61 64 2d    ** and a read-
124f1 6c 6f 63 6b 20 72 65 71 75 65 73 74 65 64 2c 20  lock requested, 
124f2 77 65 20 64 6f 6e 27 74 20 69 6e 63 6f 72 72 65  we don't incorre
124f3 63 74 6c 79 20 64 6f 77 6e 67 72 61 64 65 20 74  ctly downgrade t
124f4 68 65 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20  he lock..  */.  
124f5 61 73 73 65 72 74 28 20 57 52 49 54 45 5f 4c 4f  assert( WRITE_LO
124f6 43 4b 3e 52 45 41 44 5f 4c 4f 43 4b 20 29 3b 0a  CK>READ_LOCK );.
124f7 20 20 69 66 28 20 65 4c 6f 63 6b 3e 70 4c 6f 63    if( eLock>pLoc
124f8 6b 2d 3e 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20  k->eLock ){.    
124f9 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 65  pLock->eLock = e
124fa 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  Lock;.  }..  ret
124fb 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
124fc 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49  .#endif /* !SQLI
124fd 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
124fe 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  ACHE */..#ifndef
124ff 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
12500 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20  RED_CACHE./*.** 
12501 52 65 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 20  Release all the 
12502 74 61 62 6c 65 20 6c 6f 63 6b 73 20 28 6c 6f 63  table locks (loc
12503 6b 73 20 6f 62 74 61 69 6e 65 64 20 76 69 61 20  ks obtained via 
12504 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 6c 6f 63  calls to the loc
12505 6b 54 61 62 6c 65 28 29 0a 2a 2a 20 70 72 6f 63  kTable().** proc
12506 65 64 75 72 65 29 20 68 65 6c 64 20 62 79 20 42  edure) held by B
12507 74 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 0a 2a  tree handle p..*
12508 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e  /.static void un
12509 6c 6f 63 6b 41 6c 6c 54 61 62 6c 65 73 28 42 74  lockAllTables(Bt
1250a 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61  ree *p){.  BtSha
1250b 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
1250c 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 2a 70 70  t;.  BtLock **pp
1250d 49 74 65 72 20 3d 20 26 70 42 74 2d 3e 70 4c 6f  Iter = &pBt->pLo
1250e 63 6b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  ck;..  assert( s
1250f 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
12510 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73  Mutex(p) );.  as
12511 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c  sert( p->sharabl
12512 65 20 7c 7c 20 30 3d 3d 2a 70 70 49 74 65 72 20  e || 0==*ppIter 
12513 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 2a 70 70  );..  while( *pp
12514 49 74 65 72 20 29 7b 0a 20 20 20 20 42 74 4c 6f  Iter ){.    BtLo
12515 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 2a 70 70 49  ck *pLock = *ppI
12516 74 65 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ter;.    assert(
12517 20 70 42 74 2d 3e 70 45 78 63 6c 75 73 69 76 65   pBt->pExclusive
12518 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 45 78 63  ==0 || pBt->pExc
12519 6c 75 73 69 76 65 3d 3d 70 4c 6f 63 6b 2d 3e 70  lusive==pLock->p
1251a 42 74 72 65 65 20 29 3b 0a 20 20 20 20 69 66 28  Btree );.    if(
1251b 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d 3d   pLock->pBtree==
1251c 70 20 29 7b 0a 20 20 20 20 20 20 2a 70 70 49 74  p ){.      *ppIt
1251d 65 72 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78  er = pLock->pNex
1251e 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  t;.      sqlite3
1251f 5f 66 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 20  _free(pLock);.  
12520 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
12521 70 49 74 65 72 20 3d 20 26 70 4c 6f 63 6b 2d 3e  pIter = &pLock->
12522 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  pNext;.    }.  }
12523 0a 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 45 78  ..  if( pBt->pEx
12524 63 6c 75 73 69 76 65 3d 3d 70 20 29 7b 0a 20 20  clusive==p ){.  
12525 20 20 70 42 74 2d 3e 70 45 78 63 6c 75 73 69 76    pBt->pExclusiv
12526 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  e = 0;.  }.}.#en
12527 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
12528 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 20  IT_SHARED_CACHE 
12529 2a 2f 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
1252a 72 65 6c 65 61 73 65 50 61 67 65 28 4d 65 6d 50  releasePage(MemP
1252b 61 67 65 20 2a 70 50 61 67 65 29 3b 20 20 2f 2a  age *pPage);  /*
1252c 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e   Forward referen
1252d 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 65 72  ce */../*.** Ver
1252e 69 66 79 20 74 68 61 74 20 74 68 65 20 63 75 72  ify that the cur
1252f 73 6f 72 20 68 6f 6c 64 73 20 61 20 6d 75 74 65  sor holds a mute
12530 78 20 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65  x on the BtShare
12531 64 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45  d.*/.#ifndef NDE
12532 42 55 47 0a 73 74 61 74 69 63 20 69 6e 74 20 63  BUG.static int c
12533 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
12534 42 74 43 75 72 73 6f 72 20 2a 70 29 7b 0a 20 20  BtCursor *p){.  
12535 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d  return sqlite3_m
12536 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74  utex_held(p->pBt
12537 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 23 65 6e 64  ->mutex);.}.#end
12538 69 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  if...#ifndef SQL
12539 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f  ITE_OMIT_INCRBLO
1253a 42 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 64 61  B./*.** Invalida
1253b 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  te the overflow 
1253c 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20  page-list cache 
1253d 66 6f 72 20 63 75 72 73 6f 72 20 70 43 75 72 2c  for cursor pCur,
1253e 20 69 66 20 61 6e 79 2e 0a 2a 2f 0a 73 74 61 74   if any..*/.stat
1253f 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61  ic void invalida
12540 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28  teOverflowCache(
12541 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
12542 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
12543 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
12544 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ) );.  sqlite3_f
12545 72 65 65 28 70 43 75 72 2d 3e 61 4f 76 65 72 66  ree(pCur->aOverf
12546 6c 6f 77 29 3b 0a 20 20 70 43 75 72 2d 3e 61 4f  low);.  pCur->aO
12547 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 7d 0a 0a  verflow = 0;.}..
12548 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65  /*.** Invalidate
12549 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61   the overflow pa
1254a 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20 66 6f  ge-list cache fo
1254b 72 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 6f 70  r all cursors op
1254c 65 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 73  ened.** on the s
1254d 68 61 72 65 64 20 62 74 72 65 65 20 73 74 72 75  hared btree stru
1254e 63 74 75 72 65 20 70 42 74 2e 0a 2a 2f 0a 73 74  cture pBt..*/.st
1254f 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69  atic void invali
12550 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43  dateAllOverflowC
12551 61 63 68 65 28 42 74 53 68 61 72 65 64 20 2a 70  ache(BtShared *p
12552 42 74 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20  Bt){.  BtCursor 
12553 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  *p;.  assert( sq
12554 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
12555 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
12556 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75    for(p=pBt->pCu
12557 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e  rsor; p; p=p->pN
12558 65 78 74 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69  ext){.    invali
12559 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68  dateOverflowCach
1255a 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73  e(p);.  }.}.#els
1255b 65 0a 20 20 23 64 65 66 69 6e 65 20 69 6e 76 61  e.  #define inva
1255c 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61  lidateOverflowCa
1255d 63 68 65 28 78 29 0a 20 20 23 64 65 66 69 6e 65  che(x).  #define
1255e 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76   invalidateAllOv
1255f 65 72 66 6c 6f 77 43 61 63 68 65 28 78 29 0a 23  erflowCache(x).#
12560 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 61 76  endif../*.** Sav
12561 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 75  e the current cu
12562 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 69 6e  rsor position in
12563 20 74 68 65 20 76 61 72 69 61 62 6c 65 73 20 42   the variables B
12564 74 43 75 72 73 6f 72 2e 6e 4b 65 79 20 0a 2a 2a  tCursor.nKey .**
12565 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 70 4b   and BtCursor.pK
12566 65 79 2e 20 54 68 65 20 63 75 72 73 6f 72 27 73  ey. The cursor's
12567 20 73 74 61 74 65 20 69 73 20 73 65 74 20 74 6f   state is set to
12568 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53   CURSOR_REQUIRES
12569 45 45 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  EEK..*/.static i
1256a 6e 74 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73  nt saveCursorPos
1256b 69 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a  ition(BtCursor *
1256c 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b  pCur){.  int rc;
1256d 0a 0a 20 20 61 73 73 65 72 74 28 20 43 55 52 53  ..  assert( CURS
1256e 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e  OR_VALID==pCur->
1256f 65 53 74 61 74 65 20 29 3b 0a 20 20 61 73 73 65  eState );.  asse
12570 72 74 28 20 30 3d 3d 70 43 75 72 2d 3e 70 4b 65  rt( 0==pCur->pKe
12571 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63  y );.  assert( c
12572 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
12573 70 43 75 72 29 20 29 3b 0a 0a 20 20 72 63 20 3d  pCur) );..  rc =
12574 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79   sqlite3BtreeKey
12575 53 69 7a 65 28 70 43 75 72 2c 20 26 70 43 75 72  Size(pCur, &pCur
12576 2d 3e 6e 4b 65 79 29 3b 0a 0a 20 20 2f 2a 20 49  ->nKey);..  /* I
12577 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 74  f this is an int
12578 4b 65 79 20 74 61 62 6c 65 2c 20 74 68 65 6e 20  Key table, then 
12579 74 68 65 20 61 62 6f 76 65 20 63 61 6c 6c 20 74  the above call t
1257a 6f 20 42 74 72 65 65 4b 65 79 53 69 7a 65 28 29  o BtreeKeySize()
1257b 0a 20 20 2a 2a 20 73 74 6f 72 65 73 20 74 68 65  .  ** stores the
1257c 20 69 6e 74 65 67 65 72 20 6b 65 79 20 69 6e 20   integer key in 
1257d 70 43 75 72 2d 3e 6e 4b 65 79 2e 20 49 6e 20 74  pCur->nKey. In t
1257e 68 69 73 20 63 61 73 65 20 74 68 69 73 20 76 61  his case this va
1257f 6c 75 65 20 69 73 0a 20 20 2a 2a 20 61 6c 6c 20  lue is.  ** all 
12580 74 68 61 74 20 69 73 20 72 65 71 75 69 72 65 64  that is required
12581 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20  . Otherwise, if 
12582 70 43 75 72 20 69 73 20 6e 6f 74 20 6f 70 65 6e  pCur is not open
12583 20 6f 6e 20 61 6e 20 69 6e 74 4b 65 79 0a 20 20   on an intKey.  
12584 2a 2a 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 6d  ** table, then m
12585 61 6c 6c 6f 63 20 73 70 61 63 65 20 66 6f 72 20  alloc space for 
12586 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 70 43  and store the pC
12587 75 72 2d 3e 6e 4b 65 79 20 62 79 74 65 73 20 6f  ur->nKey bytes o
12588 66 20 6b 65 79 20 0a 20 20 2a 2a 20 64 61 74 61  f key .  ** data
12589 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d  ..  */.  if( rc=
1258a 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 30 3d  =SQLITE_OK && 0=
1258b 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d  =pCur->apPage[0]
1258c 2d 3e 69 6e 74 4b 65 79 29 7b 0a 20 20 20 20 76  ->intKey){.    v
1258d 6f 69 64 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69  oid *pKey = sqli
1258e 74 65 33 4d 61 6c 6c 6f 63 28 70 43 75 72 2d 3e  te3Malloc(pCur->
1258f 6e 4b 65 79 29 3b 0a 20 20 20 20 69 66 28 20 70  nKey);.    if( p
12590 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20  Key ){.      rc 
12591 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65  = sqlite3BtreeKe
12592 79 28 70 43 75 72 2c 20 30 2c 20 70 43 75 72 2d  y(pCur, 0, pCur-
12593 3e 6e 4b 65 79 2c 20 70 4b 65 79 29 3b 0a 20 20  >nKey, pKey);.  
12594 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
12595 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
12596 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 70 4b   pCur->pKey = pK
12597 65 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ey;.      }else{
12598 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
12599 5f 66 72 65 65 28 70 4b 65 79 29 3b 0a 20 20 20  _free(pKey);.   
1259a 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
1259b 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1259c 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20  E_NOMEM;.    }. 
1259d 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 70 43   }.  assert( !pC
1259e 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69  ur->apPage[0]->i
1259f 6e 74 4b 65 79 20 7c 7c 20 21 70 43 75 72 2d 3e  ntKey || !pCur->
125a0 70 4b 65 79 20 29 3b 0a 0a 20 20 69 66 28 20 72  pKey );..  if( r
125a1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
125a2 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66      int i;.    f
125a3 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 43 75 72 2d  or(i=0; i<=pCur-
125a4 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20  >iPage; i++){.  
125a5 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
125a6 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 29  pCur->apPage[i])
125a7 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 70  ;.      pCur->ap
125a8 50 61 67 65 5b 69 5d 20 3d 20 30 3b 0a 20 20 20  Page[i] = 0;.   
125a9 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61   }.    pCur->iPa
125aa 67 65 20 3d 20 2d 31 3b 0a 20 20 20 20 70 43 75  ge = -1;.    pCu
125ab 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53  r->eState = CURS
125ac 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 3b 0a  OR_REQUIRESEEK;.
125ad 20 20 7d 0a 0a 20 20 69 6e 76 61 6c 69 64 61 74    }..  invalidat
125ae 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70  eOverflowCache(p
125af 43 75 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  Cur);.  return r
125b0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 65  c;.}../*.** Save
125b1 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f   the positions o
125b2 66 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 65 78  f all cursors ex
125b3 63 65 70 74 20 70 45 78 63 65 70 74 20 6f 70 65  cept pExcept ope
125b4 6e 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 0a  n on the table .
125b5 2a 2a 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67  ** with root-pag
125b6 65 20 69 52 6f 6f 74 2e 20 55 73 75 61 6c 6c 79  e iRoot. Usually
125b7 2c 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64  , this is called
125b8 20 6a 75 73 74 20 62 65 66 6f 72 65 20 63 75 72   just before cur
125b9 73 6f 72 0a 2a 2a 20 70 45 78 63 65 70 74 20 69  sor.** pExcept i
125ba 73 20 75 73 65 64 20 74 6f 20 6d 6f 64 69 66 79  s used to modify
125bb 20 74 68 65 20 74 61 62 6c 65 20 28 42 74 72 65   the table (Btre
125bc 65 44 65 6c 65 74 65 28 29 20 6f 72 20 42 74 72  eDelete() or Btr
125bd 65 65 49 6e 73 65 72 74 28 29 29 2e 0a 2a 2f 0a  eeInsert())..*/.
125be 73 74 61 74 69 63 20 69 6e 74 20 73 61 76 65 41  static int saveA
125bf 6c 6c 43 75 72 73 6f 72 73 28 42 74 53 68 61 72  llCursors(BtShar
125c0 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 69 52  ed *pBt, Pgno iR
125c1 6f 6f 74 2c 20 42 74 43 75 72 73 6f 72 20 2a 70  oot, BtCursor *p
125c2 45 78 63 65 70 74 29 7b 0a 20 20 42 74 43 75 72  Except){.  BtCur
125c3 73 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74  sor *p;.  assert
125c4 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
125c5 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
125c6 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45   );.  assert( pE
125c7 78 63 65 70 74 3d 3d 30 20 7c 7c 20 70 45 78 63  xcept==0 || pExc
125c8 65 70 74 2d 3e 70 42 74 3d 3d 70 42 74 20 29 3b  ept->pBt==pBt );
125c9 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43  .  for(p=pBt->pC
125ca 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70  ursor; p; p=p->p
125cb 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70  Next){.    if( p
125cc 21 3d 70 45 78 63 65 70 74 20 26 26 20 28 30 3d  !=pExcept && (0=
125cd 3d 69 52 6f 6f 74 20 7c 7c 20 70 2d 3e 70 67 6e  =iRoot || p->pgn
125ce 6f 52 6f 6f 74 3d 3d 69 52 6f 6f 74 29 20 26 26  oRoot==iRoot) &&
125cf 20 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 53 74   .        p->eSt
125d0 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
125d1 44 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72  D ){.      int r
125d2 63 20 3d 20 73 61 76 65 43 75 72 73 6f 72 50 6f  c = saveCursorPo
125d3 73 69 74 69 6f 6e 28 70 29 3b 0a 20 20 20 20 20  sition(p);.     
125d4 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
125d5 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  rc ){.        re
125d6 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
125d7 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
125d8 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
125d9 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68  ../*.** Clear th
125da 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72  e current cursor
125db 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 53 51   position..*/.SQ
125dc 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
125dd 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c  d sqlite3BtreeCl
125de 65 61 72 43 75 72 73 6f 72 28 42 74 43 75 72 73  earCursor(BtCurs
125df 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73  or *pCur){.  ass
125e0 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
125e1 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
125e2 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43   sqlite3_free(pC
125e3 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 70 43 75  ur->pKey);.  pCu
125e4 72 2d 3e 70 4b 65 79 20 3d 20 30 3b 0a 20 20 70  r->pKey = 0;.  p
125e5 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55  Cur->eState = CU
125e6 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 7d 0a  RSOR_INVALID;.}.
125e7 0a 2f 2a 0a 2a 2a 20 52 65 73 74 6f 72 65 20 74  ./*.** Restore t
125e8 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65  he cursor to the
125e9 20 70 6f 73 69 74 69 6f 6e 20 69 74 20 77 61 73   position it was
125ea 20 69 6e 20 28 6f 72 20 61 73 20 63 6c 6f 73 65   in (or as close
125eb 20 74 6f 20 61 73 20 70 6f 73 73 69 62 6c 65 29   to as possible)
125ec 0a 2a 2a 20 77 68 65 6e 20 73 61 76 65 43 75 72  .** when saveCur
125ed 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 77 61  sorPosition() wa
125ee 73 20 63 61 6c 6c 65 64 2e 20 4e 6f 74 65 20 74  s called. Note t
125ef 68 61 74 20 74 68 69 73 20 63 61 6c 6c 20 64 65  hat this call de
125f0 6c 65 74 65 73 20 74 68 65 20 0a 2a 2a 20 73 61  letes the .** sa
125f1 76 65 64 20 70 6f 73 69 74 69 6f 6e 20 69 6e 66  ved position inf
125f2 6f 20 73 74 6f 72 65 64 20 62 79 20 73 61 76 65  o stored by save
125f3 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29  CursorPosition()
125f4 2c 20 73 6f 20 74 68 65 72 65 20 63 61 6e 20 62  , so there can b
125f5 65 0a 2a 2a 20 61 74 20 6d 6f 73 74 20 6f 6e 65  e.** at most one
125f6 20 65 66 66 65 63 74 69 76 65 20 72 65 73 74 6f   effective resto
125f7 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  reCursorPosition
125f8 28 29 20 63 61 6c 6c 20 61 66 74 65 72 20 65 61  () call after ea
125f9 63 68 20 0a 2a 2a 20 73 61 76 65 43 75 72 73 6f  ch .** saveCurso
125fa 72 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f 0a  rPosition()..*/.
125fb 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
125fc 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 52  nt sqlite3BtreeR
125fd 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69  estoreCursorPosi
125fe 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70  tion(BtCursor *p
125ff 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Cur){.  int rc;.
12600 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
12601 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
12602 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
12603 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53  ur->eState>=CURS
12604 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29  OR_REQUIRESEEK )
12605 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53  ;.  if( pCur->eS
12606 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55  tate==CURSOR_FAU
12607 4c 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  LT ){.    return
12608 20 70 43 75 72 2d 3e 73 6b 69 70 3b 0a 20 20 7d   pCur->skip;.  }
12609 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20  .  pCur->eState 
1260a 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  = CURSOR_INVALID
1260b 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1260c 42 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 72  BtreeMoveto(pCur
1260d 2c 20 70 43 75 72 2d 3e 70 4b 65 79 2c 20 70 43  , pCur->pKey, pC
1260e 75 72 2d 3e 6e 4b 65 79 2c 20 30 2c 20 26 70 43  ur->nKey, 0, &pC
1260f 75 72 2d 3e 73 6b 69 70 29 3b 0a 20 20 69 66 28  ur->skip);.  if(
12610 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
12611 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
12612 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a  ee(pCur->pKey);.
12613 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d      pCur->pKey =
12614 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   0;.    assert( 
12615 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
12616 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43  RSOR_VALID || pC
12617 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
12618 4f 52 5f 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20  OR_INVALID );.  
12619 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
1261a 0a 0a 23 64 65 66 69 6e 65 20 72 65 73 74 6f 72  ..#define restor
1261b 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  eCursorPosition(
1261c 70 29 20 5c 0a 20 20 28 70 2d 3e 65 53 74 61 74  p) \.  (p->eStat
1261d 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 52  e>=CURSOR_REQUIR
1261e 45 53 45 45 4b 20 3f 20 5c 0a 20 20 20 20 20 20  ESEEK ? \.      
1261f 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 52     sqlite3BtreeR
12620 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69  estoreCursorPosi
12621 74 69 6f 6e 28 70 29 20 3a 20 5c 0a 20 20 20 20  tion(p) : \.    
12622 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 29 0a       SQLITE_OK).
12623 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65  ./*.** Determine
12624 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
12625 61 20 63 75 72 73 6f 72 20 68 61 73 20 6d 6f 76  a cursor has mov
12626 65 64 20 66 72 6f 6d 20 74 68 65 20 70 6f 73 69  ed from the posi
12627 74 69 6f 6e 20 69 74 0a 2a 2a 20 77 61 73 20 6c  tion it.** was l
12628 61 73 74 20 70 6c 61 63 65 64 20 61 74 2e 20 20  ast placed at.  
12629 43 75 72 73 6f 72 73 20 63 61 6e 20 6d 6f 76 65  Cursors can move
1262a 20 77 68 65 6e 20 74 68 65 20 72 6f 77 20 74 68   when the row th
1262b 65 79 20 61 72 65 20 70 6f 69 6e 74 69 6e 67 0a  ey are pointing.
1262c 2a 2a 20 61 74 20 69 73 20 64 65 6c 65 74 65 64  ** at is deleted
1262d 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20   out from under 
1262e 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  them..**.** This
1262f 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
12630 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69   an error code i
12631 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73  f something goes
12632 20 77 72 6f 6e 67 2e 20 20 54 68 65 0a 2a 2a 20   wrong.  The.** 
12633 69 6e 74 65 67 65 72 20 2a 70 48 61 73 4d 6f 76  integer *pHasMov
12634 65 64 20 69 73 20 73 65 74 20 74 6f 20 6f 6e 65  ed is set to one
12635 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 68   if the cursor h
12636 61 73 20 6d 6f 76 65 64 20 61 6e 64 20 30 20 69  as moved and 0 i
12637 66 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  f not..*/.SQLITE
12638 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
12639 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 48  ite3BtreeCursorH
1263a 61 73 4d 6f 76 65 64 28 42 74 43 75 72 73 6f 72  asMoved(BtCursor
1263b 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 48 61   *pCur, int *pHa
1263c 73 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72  sMoved){.  int r
1263d 63 3b 0a 0a 20 20 72 63 20 3d 20 72 65 73 74 6f  c;..  rc = resto
1263e 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  reCursorPosition
1263f 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63  (pCur);.  if( rc
12640 20 29 7b 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76   ){.    *pHasMov
12641 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75  ed = 1;.    retu
12642 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28  rn rc;.  }.  if(
12643 20 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43   pCur->eState!=C
12644 55 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70  URSOR_VALID || p
12645 43 75 72 2d 3e 73 6b 69 70 21 3d 30 20 29 7b 0a  Cur->skip!=0 ){.
12646 20 20 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d      *pHasMoved =
12647 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   1;.  }else{.   
12648 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 30 3b   *pHasMoved = 0;
12649 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
1264a 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e  LITE_OK;.}..#ifn
1264b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1264c 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a  AUTOVACUUM./*.**
1264d 20 47 69 76 65 6e 20 61 20 70 61 67 65 20 6e 75   Given a page nu
1264e 6d 62 65 72 20 6f 66 20 61 20 72 65 67 75 6c 61  mber of a regula
1264f 72 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2c  r database page,
12650 20 72 65 74 75 72 6e 20 74 68 65 20 70 61 67 65   return the page
12651 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  .** number for t
12652 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70  he pointer-map p
12653 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  age that contain
12654 73 20 74 68 65 20 65 6e 74 72 79 20 66 6f 72 20  s the entry for 
12655 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 70 61 67  the.** input pag
12656 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61  e number..*/.sta
12657 74 69 63 20 50 67 6e 6f 20 70 74 72 6d 61 70 50  tic Pgno ptrmapP
12658 61 67 65 6e 6f 28 42 74 53 68 61 72 65 64 20 2a  ageno(BtShared *
12659 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b  pBt, Pgno pgno){
1265a 0a 20 20 69 6e 74 20 6e 50 61 67 65 73 50 65 72  .  int nPagesPer
1265b 4d 61 70 50 61 67 65 2c 20 69 50 74 72 4d 61 70  MapPage, iPtrMap
1265c 2c 20 72 65 74 3b 0a 20 20 61 73 73 65 72 74 28  , ret;.  assert(
1265d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1265e 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
1265f 29 3b 0a 20 20 6e 50 61 67 65 73 50 65 72 4d 61  );.  nPagesPerMa
12660 70 50 61 67 65 20 3d 20 28 70 42 74 2d 3e 75 73  pPage = (pBt->us
12661 61 62 6c 65 53 69 7a 65 2f 35 29 2b 31 3b 0a 20  ableSize/5)+1;. 
12662 20 69 50 74 72 4d 61 70 20 3d 20 28 70 67 6e 6f   iPtrMap = (pgno
12663 2d 32 29 2f 6e 50 61 67 65 73 50 65 72 4d 61 70  -2)/nPagesPerMap
12664 50 61 67 65 3b 0a 20 20 72 65 74 20 3d 20 28 69  Page;.  ret = (i
12665 50 74 72 4d 61 70 2a 6e 50 61 67 65 73 50 65 72  PtrMap*nPagesPer
12666 4d 61 70 50 61 67 65 29 20 2b 20 32 3b 20 0a 20  MapPage) + 2; . 
12667 20 69 66 28 20 72 65 74 3d 3d 50 45 4e 44 49 4e   if( ret==PENDIN
12668 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29  G_BYTE_PAGE(pBt)
12669 20 29 7b 0a 20 20 20 20 72 65 74 2b 2b 3b 0a 20   ){.    ret++;. 
1266a 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 74 3b   }.  return ret;
1266b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  .}../*.** Write 
1266c 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68  an entry into th
1266d 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a  e pointer map..*
1266e 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1266f 65 20 75 70 64 61 74 65 73 20 74 68 65 20 70 6f  e updates the po
12670 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20  inter map entry 
12671 66 6f 72 20 70 61 67 65 20 6e 75 6d 62 65 72 20  for page number 
12672 27 6b 65 79 27 0a 2a 2a 20 73 6f 20 74 68 61 74  'key'.** so that
12673 20 69 74 20 6d 61 70 73 20 74 6f 20 74 79 70 65   it maps to type
12674 20 27 65 54 79 70 65 27 20 61 6e 64 20 70 61 72   'eType' and par
12675 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20  ent page number 
12676 27 70 67 6e 6f 27 2e 0a 2a 2a 20 41 6e 20 65 72  'pgno'..** An er
12677 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
12678 72 6e 65 64 20 69 66 20 73 6f 6d 65 74 68 69 6e  rned if somethin
12679 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 6f 74  g goes wrong, ot
1267a 68 65 72 77 69 73 65 20 53 51 4c 49 54 45 5f 4f  herwise SQLITE_O
1267b 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  K..*/.static int
1267c 20 70 74 72 6d 61 70 50 75 74 28 42 74 53 68 61   ptrmapPut(BtSha
1267d 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6b  red *pBt, Pgno k
1267e 65 79 2c 20 75 38 20 65 54 79 70 65 2c 20 50 67  ey, u8 eType, Pg
1267f 6e 6f 20 70 61 72 65 6e 74 29 7b 0a 20 20 44 62  no parent){.  Db
12680 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20 20  Page *pDbPage;  
12681 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d  /* The pointer m
12682 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20  ap page */.  u8 
12683 2a 70 50 74 72 6d 61 70 3b 20 20 20 20 20 20 2f  *pPtrmap;      /
12684 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61  * The pointer ma
12685 70 20 64 61 74 61 20 2a 2f 0a 20 20 50 67 6e 6f  p data */.  Pgno
12686 20 69 50 74 72 6d 61 70 3b 20 20 20 20 20 2f 2a   iPtrmap;     /*
12687 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   The pointer map
12688 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a   page number */.
12689 20 20 69 6e 74 20 6f 66 66 73 65 74 3b 20 20 20    int offset;   
1268a 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e      /* Offset in
1268b 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67   pointer map pag
1268c 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  e */.  int rc;..
1268d 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1268e 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
1268f 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 2f 2a  ->mutex) );.  /*
12690 20 54 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72   The master-jour
12691 6e 61 6c 20 70 61 67 65 20 6e 75 6d 62 65 72 20  nal page number 
12692 6d 75 73 74 20 6e 65 76 65 72 20 62 65 20 75 73  must never be us
12693 65 64 20 61 73 20 61 20 70 6f 69 6e 74 65 72 20  ed as a pointer 
12694 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 61 73  map page */.  as
12695 73 65 72 74 28 20 30 3d 3d 50 54 52 4d 41 50 5f  sert( 0==PTRMAP_
12696 49 53 50 41 47 45 28 70 42 74 2c 20 50 45 4e 44  ISPAGE(pBt, PEND
12697 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
12698 74 29 29 20 29 3b 0a 0a 20 20 61 73 73 65 72 74  t)) );..  assert
12699 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ( pBt->autoVacuu
1269a 6d 20 29 3b 0a 20 20 69 66 28 20 6b 65 79 3d 3d  m );.  if( key==
1269b 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
1269c 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
1269d 4b 50 54 3b 0a 20 20 7d 0a 20 20 69 50 74 72 6d  KPT;.  }.  iPtrm
1269e 61 70 20 3d 20 50 54 52 4d 41 50 5f 50 41 47 45  ap = PTRMAP_PAGE
1269f 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20  NO(pBt, key);.  
126a0 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
126a1 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65 72  rGet(pBt->pPager
126a2 2c 20 69 50 74 72 6d 61 70 2c 20 26 70 44 62 50  , iPtrmap, &pDbP
126a3 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  age);.  if( rc!=
126a4 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
126a5 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
126a6 20 20 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41    offset = PTRMA
126a7 50 5f 50 54 52 4f 46 46 53 45 54 28 69 50 74 72  P_PTROFFSET(iPtr
126a8 6d 61 70 2c 20 6b 65 79 29 3b 0a 20 20 70 50 74  map, key);.  pPt
126a9 72 6d 61 70 20 3d 20 28 75 38 20 2a 29 73 71 6c  rmap = (u8 *)sql
126aa 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61  ite3PagerGetData
126ab 28 70 44 62 50 61 67 65 29 3b 0a 0a 20 20 69 66  (pDbPage);..  if
126ac 28 20 65 54 79 70 65 21 3d 70 50 74 72 6d 61 70  ( eType!=pPtrmap
126ad 5b 6f 66 66 73 65 74 5d 20 7c 7c 20 67 65 74 34  [offset] || get4
126ae 62 79 74 65 28 26 70 50 74 72 6d 61 70 5b 6f 66  byte(&pPtrmap[of
126af 66 73 65 74 2b 31 5d 29 21 3d 70 61 72 65 6e 74  fset+1])!=parent
126b0 20 29 7b 0a 20 20 20 20 54 52 41 43 45 28 28 22   ){.    TRACE(("
126b1 50 54 52 4d 41 50 5f 55 50 44 41 54 45 3a 20 25  PTRMAP_UPDATE: %
126b2 64 2d 3e 28 25 64 2c 25 64 29 5c 6e 22 2c 20 6b  d->(%d,%d)\n", k
126b3 65 79 2c 20 65 54 79 70 65 2c 20 70 61 72 65 6e  ey, eType, paren
126b4 74 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  t));.    rc = sq
126b5 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
126b6 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66  pDbPage);.    if
126b7 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
126b8 29 7b 0a 20 20 20 20 20 20 70 50 74 72 6d 61 70  ){.      pPtrmap
126b9 5b 6f 66 66 73 65 74 5d 20 3d 20 65 54 79 70 65  [offset] = eType
126ba 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65  ;.      put4byte
126bb 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74  (&pPtrmap[offset
126bc 2b 31 5d 2c 20 70 61 72 65 6e 74 29 3b 0a 20 20  +1], parent);.  
126bd 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74    }.  }..  sqlit
126be 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 62  e3PagerUnref(pDb
126bf 50 61 67 65 29 3b 0a 20 20 72 65 74 75 72 6e 20  Page);.  return 
126c0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61  rc;.}../*.** Rea
126c1 64 20 61 6e 20 65 6e 74 72 79 20 66 72 6f 6d 20  d an entry from 
126c2 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e  the pointer map.
126c3 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
126c4 69 6e 65 20 72 65 74 72 69 65 76 65 73 20 74 68  ine retrieves th
126c5 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e  e pointer map en
126c6 74 72 79 20 66 6f 72 20 70 61 67 65 20 27 6b 65  try for page 'ke
126c7 79 27 2c 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74  y', writing.** t
126c8 68 65 20 74 79 70 65 20 61 6e 64 20 70 61 72 65  he type and pare
126c9 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74  nt page number t
126ca 6f 20 2a 70 45 54 79 70 65 20 61 6e 64 20 2a 70  o *pEType and *p
126cb 50 67 6e 6f 20 72 65 73 70 65 63 74 69 76 65 6c  Pgno respectivel
126cc 79 2e 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20 63  y..** An error c
126cd 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20  ode is returned 
126ce 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65  if something goe
126cf 73 20 77 72 6f 6e 67 2c 20 6f 74 68 65 72 77 69  s wrong, otherwi
126d0 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f  se SQLITE_OK..*/
126d1 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 72 6d  .static int ptrm
126d2 61 70 47 65 74 28 42 74 53 68 61 72 65 64 20 2a  apGet(BtShared *
126d3 70 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75  pBt, Pgno key, u
126d4 38 20 2a 70 45 54 79 70 65 2c 20 50 67 6e 6f 20  8 *pEType, Pgno 
126d5 2a 70 50 67 6e 6f 29 7b 0a 20 20 44 62 50 61 67  *pPgno){.  DbPag
126d6 65 20 2a 70 44 62 50 61 67 65 3b 20 20 20 2f 2a  e *pDbPage;   /*
126d7 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   The pointer map
126d8 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 69   page */.  int i
126d9 50 74 72 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a  Ptrmap;       /*
126da 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67   Pointer map pag
126db 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20  e index */.  u8 
126dc 2a 70 50 74 72 6d 61 70 3b 20 20 20 20 20 20 20  *pPtrmap;       
126dd 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 70  /* Pointer map p
126de 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e  age data */.  in
126df 74 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 20  t offset;       
126e0 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 65 6e   /* Offset of en
126e1 74 72 79 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d  try in pointer m
126e2 61 70 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 0a  ap */.  int rc;.
126e3 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
126e4 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42  e3_mutex_held(pB
126e5 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20  t->mutex) );..  
126e6 69 50 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 50  iPtrmap = PTRMAP
126e7 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79  _PAGENO(pBt, key
126e8 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
126e9 33 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70  3PagerGet(pBt->p
126ea 50 61 67 65 72 2c 20 69 50 74 72 6d 61 70 2c 20  Pager, iPtrmap, 
126eb 26 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28  &pDbPage);.  if(
126ec 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 72 65   rc!=0 ){.    re
126ed 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70  turn rc;.  }.  p
126ee 50 74 72 6d 61 70 20 3d 20 28 75 38 20 2a 29 73  Ptrmap = (u8 *)s
126ef 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61  qlite3PagerGetDa
126f0 74 61 28 70 44 62 50 61 67 65 29 3b 0a 0a 20 20  ta(pDbPage);..  
126f1 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50 5f  offset = PTRMAP_
126f2 50 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d 61  PTROFFSET(iPtrma
126f3 70 2c 20 6b 65 79 29 3b 0a 20 20 61 73 73 65 72  p, key);.  asser
126f4 74 28 20 70 45 54 79 70 65 21 3d 30 20 29 3b 0a  t( pEType!=0 );.
126f5 20 20 2a 70 45 54 79 70 65 20 3d 20 70 50 74 72    *pEType = pPtr
126f6 6d 61 70 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 69  map[offset];.  i
126f7 66 28 20 70 50 67 6e 6f 20 29 20 2a 70 50 67 6e  f( pPgno ) *pPgn
126f8 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50  o = get4byte(&pP
126f9 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d 29  trmap[offset+1])
126fa 3b 0a 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65  ;..  sqlite3Page
126fb 72 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b  rUnref(pDbPage);
126fc 0a 20 20 69 66 28 20 2a 70 45 54 79 70 65 3c 31  .  if( *pEType<1
126fd 20 7c 7c 20 2a 70 45 54 79 70 65 3e 35 20 29 20   || *pEType>5 ) 
126fe 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
126ff 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 72 65  RRUPT_BKPT;.  re
12700 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
12701 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 64  }..#else /* if d
12702 65 66 69 6e 65 64 20 53 51 4c 49 54 45 5f 4f 4d  efined SQLITE_OM
12703 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f  IT_AUTOVACUUM */
12704 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d 61  .  #define ptrma
12705 70 50 75 74 28 77 2c 78 2c 79 2c 7a 29 20 53 51  pPut(w,x,y,z) SQ
12706 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e  LITE_OK.  #defin
12707 65 20 70 74 72 6d 61 70 47 65 74 28 77 2c 78 2c  e ptrmapGet(w,x,
12708 79 2c 7a 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20  y,z) SQLITE_OK. 
12709 20 23 64 65 66 69 6e 65 20 70 74 72 6d 61 70 50   #define ptrmapP
1270a 75 74 4f 76 66 6c 28 79 2c 7a 29 20 53 51 4c 49  utOvfl(y,z) SQLI
1270b 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 2f 2a  TE_OK.#endif../*
1270c 0a 2a 2a 20 47 69 76 65 6e 20 61 20 62 74 72 65  .** Given a btre
1270d 65 20 70 61 67 65 20 61 6e 64 20 61 20 63 65 6c  e page and a cel
1270e 6c 20 69 6e 64 65 78 20 28 30 20 6d 65 61 6e 73  l index (0 means
1270f 20 74 68 65 20 66 69 72 73 74 20 63 65 6c 6c 20   the first cell 
12710 6f 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65 2c 20  on.** the page, 
12711 31 20 6d 65 61 6e 73 20 74 68 65 20 73 65 63 6f  1 means the seco
12712 6e 64 20 63 65 6c 6c 2c 20 61 6e 64 20 73 6f 20  nd cell, and so 
12713 66 6f 72 74 68 29 20 72 65 74 75 72 6e 20 61 20  forth) return a 
12714 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68  pointer.** to th
12715 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 2e 0a  e cell content..
12716 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
12717 6e 65 20 77 6f 72 6b 73 20 6f 6e 6c 79 20 66 6f  ne works only fo
12718 72 20 70 61 67 65 73 20 74 68 61 74 20 64 6f 20  r pages that do 
12719 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 6f 76 65 72  not contain over
1271a 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 23  flow cells..*/.#
1271b 64 65 66 69 6e 65 20 66 69 6e 64 43 65 6c 6c 28  define findCell(
1271c 50 2c 49 29 20 5c 0a 20 20 28 28 50 29 2d 3e 61  P,I) \.  ((P)->a
1271d 44 61 74 61 20 2b 20 28 28 50 29 2d 3e 6d 61 73  Data + ((P)->mas
1271e 6b 50 61 67 65 20 26 20 67 65 74 32 62 79 74 65  kPage & get2byte
1271f 28 26 28 50 29 2d 3e 61 44 61 74 61 5b 28 50 29  (&(P)->aData[(P)
12720 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 28  ->cellOffset+2*(
12721 49 29 5d 29 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68  I)])))../*.** Th
12722 69 73 20 61 20 6d 6f 72 65 20 63 6f 6d 70 6c 65  is a more comple
12723 78 20 76 65 72 73 69 6f 6e 20 6f 66 20 66 69 6e  x version of fin
12724 64 43 65 6c 6c 28 29 20 74 68 61 74 20 77 6f 72  dCell() that wor
12725 6b 73 20 66 6f 72 0a 2a 2a 20 70 61 67 65 73 20  ks for.** pages 
12726 74 68 61 74 20 64 6f 20 63 6f 6e 74 61 69 6e 20  that do contain 
12727 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 20  overflow cells. 
12728 20 53 65 65 20 69 6e 73 65 72 74 0a 2a 2f 0a 73   See insert.*/.s
12729 74 61 74 69 63 20 75 38 20 2a 66 69 6e 64 4f 76  tatic u8 *findOv
1272a 65 72 66 6c 6f 77 43 65 6c 6c 28 4d 65 6d 50 61  erflowCell(MemPa
1272b 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69  ge *pPage, int i
1272c 43 65 6c 6c 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  Cell){.  int i;.
1272d 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1272e 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
1272f 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
12730 29 3b 0a 20 20 66 6f 72 28 69 3d 70 50 61 67 65  );.  for(i=pPage
12731 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2d 31 3b 20 69  ->nOverflow-1; i
12732 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69  >=0; i--){.    i
12733 6e 74 20 6b 3b 0a 20 20 20 20 73 74 72 75 63 74  nt k;.    struct
12734 20 5f 4f 76 66 6c 43 65 6c 6c 20 2a 70 4f 76 66   _OvflCell *pOvf
12735 6c 3b 0a 20 20 20 20 70 4f 76 66 6c 20 3d 20 26  l;.    pOvfl = &
12736 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 69 5d 3b  pPage->aOvfl[i];
12737 0a 20 20 20 20 6b 20 3d 20 70 4f 76 66 6c 2d 3e  .    k = pOvfl->
12738 69 64 78 3b 0a 20 20 20 20 69 66 28 20 6b 3c 3d  idx;.    if( k<=
12739 69 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69  iCell ){.      i
1273a 66 28 20 6b 3d 3d 69 43 65 6c 6c 20 29 7b 0a 20  f( k==iCell ){. 
1273b 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 4f         return pO
1273c 76 66 6c 2d 3e 70 43 65 6c 6c 3b 0a 20 20 20 20  vfl->pCell;.    
1273d 20 20 7d 0a 20 20 20 20 20 20 69 43 65 6c 6c 2d    }.      iCell-
1273e 2d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  -;.    }.  }.  r
1273f 65 74 75 72 6e 20 66 69 6e 64 43 65 6c 6c 28 70  eturn findCell(p
12740 50 61 67 65 2c 20 69 43 65 6c 6c 29 3b 0a 7d 0a  Page, iCell);.}.
12741 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 61 20 63  ./*.** Parse a c
12742 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 62 6c 6f 63  ell content bloc
12743 6b 20 61 6e 64 20 66 69 6c 6c 20 69 6e 20 74 68  k and fill in th
12744 65 20 43 65 6c 6c 49 6e 66 6f 20 73 74 72 75 63  e CellInfo struc
12745 74 75 72 65 2e 20 20 54 68 65 72 65 0a 2a 2a 20  ture.  There.** 
12746 61 72 65 20 74 77 6f 20 76 65 72 73 69 6f 6e 73  are two versions
12747 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f   of this functio
12748 6e 2e 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  n.  sqlite3Btree
12749 50 61 72 73 65 43 65 6c 6c 28 29 20 74 61 6b 65  ParseCell() take
1274a 73 20 61 20 0a 2a 2a 20 63 65 6c 6c 20 69 6e 64  s a .** cell ind
1274b 65 78 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64  ex as the second
1274c 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 20 73 71   argument and sq
1274d 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43  lite3BtreeParseC
1274e 65 6c 6c 50 74 72 28 29 20 0a 2a 2a 20 74 61 6b  ellPtr() .** tak
1274f 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  es a pointer to 
12750 74 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20  the body of the 
12751 63 65 6c 6c 20 61 73 20 69 74 73 20 73 65 63 6f  cell as its seco
12752 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a  nd argument..**.
12753 2a 2a 20 57 69 74 68 69 6e 20 74 68 69 73 20 66  ** Within this f
12754 69 6c 65 2c 20 74 68 65 20 70 61 72 73 65 43 65  ile, the parseCe
12755 6c 6c 28 29 20 6d 61 63 72 6f 20 63 61 6e 20 62  ll() macro can b
12756 65 20 63 61 6c 6c 65 64 20 69 6e 73 74 65 61 64  e called instead
12757 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74   of.** sqlite3Bt
12758 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28  reeParseCellPtr(
12759 29 2e 20 55 73 69 6e 67 20 73 6f 6d 65 20 63 6f  ). Using some co
1275a 6d 70 69 6c 65 72 73 2c 20 74 68 69 73 20 77 69  mpilers, this wi
1275b 6c 6c 20 62 65 20 66 61 73 74 65 72 2e 0a 2a 2f  ll be faster..*/
1275c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1275d 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
1275e 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 0a 20  eParseCellPtr(. 
1275f 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c   MemPage *pPage,
12760 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
12761 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
12762 63 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 43  cell */.  u8 *pC
12763 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  ell,            
12764 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
12765 74 68 65 20 63 65 6c 6c 20 74 65 78 74 2e 20 2a  the cell text. *
12766 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49  /.  CellInfo *pI
12767 6e 66 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 46  nfo         /* F
12768 69 6c 6c 20 69 6e 20 74 68 69 73 20 73 74 72 75  ill in this stru
12769 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e  cture */.){.  in
1276a 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  t n;            
1276b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1276c 62 79 74 65 73 20 69 6e 20 63 65 6c 6c 20 63 6f  bytes in cell co
1276d 6e 74 65 6e 74 20 68 65 61 64 65 72 20 2a 2f 0a  ntent header */.
1276e 20 20 75 33 32 20 6e 50 61 79 6c 6f 61 64 3b 20    u32 nPayload; 
1276f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
12770 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
12771 63 65 6c 6c 20 70 61 79 6c 6f 61 64 20 2a 2f 0a  cell payload */.
12772 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
12773 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50  e3_mutex_held(pP
12774 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  age->pBt->mutex)
12775 20 29 3b 0a 0a 20 20 70 49 6e 66 6f 2d 3e 70 43   );..  pInfo->pC
12776 65 6c 6c 20 3d 20 70 43 65 6c 6c 3b 0a 20 20 61  ell = pCell;.  a
12777 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65  ssert( pPage->le
12778 61 66 3d 3d 30 20 7c 7c 20 70 50 61 67 65 2d 3e  af==0 || pPage->
12779 6c 65 61 66 3d 3d 31 20 29 3b 0a 20 20 6e 20 3d  leaf==1 );.  n =
1277a 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72   pPage->childPtr
1277b 53 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20  Size;.  assert( 
1277c 6e 3d 3d 34 2d 34 2a 70 50 61 67 65 2d 3e 6c 65  n==4-4*pPage->le
1277d 61 66 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67  af );.  if( pPag
1277e 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20  e->intKey ){.   
1277f 20 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44   if( pPage->hasD
12780 61 74 61 20 29 7b 0a 20 20 20 20 20 20 6e 20 2b  ata ){.      n +
12781 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 26 70  = getVarint32(&p
12782 43 65 6c 6c 5b 6e 5d 2c 20 6e 50 61 79 6c 6f 61  Cell[n], nPayloa
12783 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  d);.    }else{. 
12784 20 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 3d 20       nPayload = 
12785 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 20 2b  0;.    }.    n +
12786 3d 20 67 65 74 56 61 72 69 6e 74 28 26 70 43 65  = getVarint(&pCe
12787 6c 6c 5b 6e 5d 2c 20 28 75 36 34 2a 29 26 70 49  ll[n], (u64*)&pI
12788 6e 66 6f 2d 3e 6e 4b 65 79 29 3b 0a 20 20 20 20  nfo->nKey);.    
12789 70 49 6e 66 6f 2d 3e 6e 44 61 74 61 20 3d 20 6e  pInfo->nData = n
1278a 50 61 79 6c 6f 61 64 3b 0a 20 20 7d 65 6c 73 65  Payload;.  }else
1278b 7b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 44 61  {.    pInfo->nDa
1278c 74 61 20 3d 20 30 3b 0a 20 20 20 20 6e 20 2b 3d  ta = 0;.    n +=
1278d 20 67 65 74 56 61 72 69 6e 74 33 32 28 26 70 43   getVarint32(&pC
1278e 65 6c 6c 5b 6e 5d 2c 20 6e 50 61 79 6c 6f 61 64  ell[n], nPayload
1278f 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4b  );.    pInfo->nK
12790 65 79 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20  ey = nPayload;. 
12791 20 7d 0a 20 20 70 49 6e 66 6f 2d 3e 6e 50 61 79   }.  pInfo->nPay
12792 6c 6f 61 64 20 3d 20 6e 50 61 79 6c 6f 61 64 3b  load = nPayload;
12793 0a 20 20 70 49 6e 66 6f 2d 3e 6e 48 65 61 64 65  .  pInfo->nHeade
12794 72 20 3d 20 6e 3b 0a 20 20 69 66 28 20 6c 69 6b  r = n;.  if( lik
12795 65 6c 79 28 6e 50 61 79 6c 6f 61 64 3c 3d 70 50  ely(nPayload<=pP
12796 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 29 20 29  age->maxLocal) )
12797 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73  {.    /* This is
12798 20 74 68 65 20 28 65 61 73 79 29 20 63 6f 6d 6d   the (easy) comm
12799 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20 74 68  on case where th
1279a 65 20 65 6e 74 69 72 65 20 70 61 79 6c 6f 61 64  e entire payload
1279b 20 66 69 74 73 0a 20 20 20 20 2a 2a 20 6f 6e 20   fits.    ** on 
1279c 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 2e 20  the local page. 
1279d 20 4e 6f 20 6f 76 65 72 66 6c 6f 77 20 69 73 20   No overflow is 
1279e 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f  required..    */
1279f 0a 20 20 20 20 69 6e 74 20 6e 53 69 7a 65 3b 20  .    int nSize; 
127a0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61           /* Tota
127a1 6c 20 73 69 7a 65 20 6f 66 20 63 65 6c 6c 20 63  l size of cell c
127a2 6f 6e 74 65 6e 74 20 69 6e 20 62 79 74 65 73 20  ontent in bytes 
127a3 2a 2f 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 6e  */.    nSize = n
127a4 50 61 79 6c 6f 61 64 20 2b 20 6e 3b 0a 20 20 20  Payload + n;.   
127a5 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d   pInfo->nLocal =
127a6 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 20 20 70   nPayload;.    p
127a7 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20  Info->iOverflow 
127a8 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 28 6e 53  = 0;.    if( (nS
127a9 69 7a 65 20 26 20 7e 33 29 3d 3d 30 20 29 7b 0a  ize & ~3)==0 ){.
127aa 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 34 3b        nSize = 4;
127ab 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d          /* Minim
127ac 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 20  um cell size is 
127ad 34 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 70  4 */.    }.    p
127ae 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 6e 53  Info->nSize = nS
127af 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ize;.  }else{.  
127b0 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 79 6c    /* If the payl
127b1 6f 61 64 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74  oad will not fit
127b2 20 63 6f 6d 70 6c 65 74 65 6c 79 20 6f 6e 20 74   completely on t
127b3 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 2c 20 77  he local page, w
127b4 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 74 6f  e have.    ** to
127b5 20 64 65 63 69 64 65 20 68 6f 77 20 6d 75 63 68   decide how much
127b6 20 74 6f 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c   to store locall
127b7 79 20 61 6e 64 20 68 6f 77 20 6d 75 63 68 20 74  y and how much t
127b8 6f 20 73 70 69 6c 6c 20 6f 6e 74 6f 0a 20 20 20  o spill onto.   
127b9 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   ** overflow pag
127ba 65 73 2e 20 20 54 68 65 20 73 74 72 61 74 65 67  es.  The strateg
127bb 79 20 69 73 20 74 6f 20 6d 69 6e 69 6d 69 7a 65  y is to minimize
127bc 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 75   the amount of u
127bd 6e 75 73 65 64 0a 20 20 20 20 2a 2a 20 73 70 61  nused.    ** spa
127be 63 65 20 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 70  ce on overflow p
127bf 61 67 65 73 20 77 68 69 6c 65 20 6b 65 65 70 69  ages while keepi
127c0 6e 67 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66  ng the amount of
127c1 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 0a 20   local storage. 
127c2 20 20 20 2a 2a 20 69 6e 20 62 65 74 77 65 65 6e     ** in between
127c3 20 6d 69 6e 4c 6f 63 61 6c 20 61 6e 64 20 6d 61   minLocal and ma
127c4 78 4c 6f 63 61 6c 2e 0a 20 20 20 20 2a 2a 0a 20  xLocal..    **. 
127c5 20 20 20 2a 2a 20 57 61 72 6e 69 6e 67 3a 20 20     ** Warning:  
127c6 63 68 61 6e 67 69 6e 67 20 74 68 65 20 77 61 79  changing the way
127c7 20 6f 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f 61   overflow payloa
127c8 64 20 69 73 20 64 69 73 74 72 69 62 75 74 65 64  d is distributed
127c9 20 69 6e 20 61 6e 79 0a 20 20 20 20 2a 2a 20 77   in any.    ** w
127ca 61 79 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69  ay will result i
127cb 6e 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c  n an incompatibl
127cc 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 0a 20  e file format.. 
127cd 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 69     */.    int mi
127ce 6e 4c 6f 63 61 6c 3b 20 20 2f 2a 20 4d 69 6e 69  nLocal;  /* Mini
127cf 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61  mum amount of pa
127d0 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c  yload held local
127d1 6c 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 61  ly */.    int ma
127d2 78 4c 6f 63 61 6c 3b 20 20 2f 2a 20 4d 61 78 69  xLocal;  /* Maxi
127d3 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61  mum amount of pa
127d4 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c  yload held local
127d5 6c 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 75  ly */.    int su
127d6 72 70 6c 75 73 3b 20 20 20 2f 2a 20 4f 76 65 72  rplus;   /* Over
127d7 66 6c 6f 77 20 70 61 79 6c 6f 61 64 20 61 76 61  flow payload ava
127d8 69 6c 61 62 6c 65 20 66 6f 72 20 6c 6f 63 61 6c  ilable for local
127d9 20 73 74 6f 72 61 67 65 20 2a 2f 0a 0a 20 20 20   storage */..   
127da 20 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67   minLocal = pPag
127db 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20  e->minLocal;.   
127dc 20 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 50 61 67   maxLocal = pPag
127dd 65 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20 20 20  e->maxLocal;.   
127de 20 73 75 72 70 6c 75 73 20 3d 20 6d 69 6e 4c 6f   surplus = minLo
127df 63 61 6c 20 2b 20 28 6e 50 61 79 6c 6f 61 64 20  cal + (nPayload 
127e0 2d 20 6d 69 6e 4c 6f 63 61 6c 29 25 28 70 50 61  - minLocal)%(pPa
127e1 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53  ge->pBt->usableS
127e2 69 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20 69 66  ize - 4);.    if
127e3 28 20 73 75 72 70 6c 75 73 20 3c 3d 20 6d 61 78  ( surplus <= max
127e4 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 70  Local ){.      p
127e5 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 73  Info->nLocal = s
127e6 75 72 70 6c 75 73 3b 0a 20 20 20 20 7d 65 6c 73  urplus;.    }els
127e7 65 7b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e  e{.      pInfo->
127e8 6e 4c 6f 63 61 6c 20 3d 20 6d 69 6e 4c 6f 63 61  nLocal = minLoca
127e9 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 6e  l;.    }.    pIn
127ea 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 3d 20  fo->iOverflow = 
127eb 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 2b 20  pInfo->nLocal + 
127ec 6e 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 53  n;.    pInfo->nS
127ed 69 7a 65 20 3d 20 70 49 6e 66 6f 2d 3e 69 4f 76  ize = pInfo->iOv
127ee 65 72 66 6c 6f 77 20 2b 20 34 3b 0a 20 20 7d 0a  erflow + 4;.  }.
127ef 7d 0a 23 64 65 66 69 6e 65 20 70 61 72 73 65 43  }.#define parseC
127f0 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c  ell(pPage, iCell
127f1 2c 20 70 49 6e 66 6f 29 20 5c 0a 20 20 73 71 6c  , pInfo) \.  sql
127f2 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65  ite3BtreeParseCe
127f3 6c 6c 50 74 72 28 28 70 50 61 67 65 29 2c 20 66  llPtr((pPage), f
127f4 69 6e 64 43 65 6c 6c 28 28 70 50 61 67 65 29 2c  indCell((pPage),
127f5 20 28 69 43 65 6c 6c 29 29 2c 20 28 70 49 6e 66   (iCell)), (pInf
127f6 6f 29 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  o)).SQLITE_PRIVA
127f7 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
127f8 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 0a 20  treeParseCell(. 
127f9 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c   MemPage *pPage,
127fa 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
127fb 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
127fc 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 43  cell */.  int iC
127fd 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  ell,            
127fe 20 20 2f 2a 20 54 68 65 20 63 65 6c 6c 20 69 6e    /* The cell in
127ff 64 65 78 2e 20 20 46 69 72 73 74 20 63 65 6c 6c  dex.  First cell
12800 20 69 73 20 30 20 2a 2f 0a 20 20 43 65 6c 6c 49   is 0 */.  CellI
12801 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20 20  nfo *pInfo      
12802 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68     /* Fill in th
12803 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  is structure */.
12804 29 7b 0a 20 20 70 61 72 73 65 43 65 6c 6c 28 70  ){.  parseCell(p
12805 50 61 67 65 2c 20 69 43 65 6c 6c 2c 20 70 49 6e  Page, iCell, pIn
12806 66 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  fo);.}../*.** Co
12807 6d 70 75 74 65 20 74 68 65 20 74 6f 74 61 6c 20  mpute the total 
12808 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
12809 74 68 61 74 20 61 20 43 65 6c 6c 20 6e 65 65 64  that a Cell need
1280a 73 20 69 6e 20 74 68 65 20 63 65 6c 6c 0a 2a 2a  s in the cell.**
1280b 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 74 68   data area of th
1280c 65 20 62 74 72 65 65 2d 70 61 67 65 2e 20 20 54  e btree-page.  T
1280d 68 65 20 72 65 74 75 72 6e 20 6e 75 6d 62 65 72  he return number
1280e 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 63 65   includes the ce
1280f 6c 6c 0a 2a 2a 20 64 61 74 61 20 68 65 61 64 65  ll.** data heade
12810 72 20 61 6e 64 20 74 68 65 20 6c 6f 63 61 6c 20  r and the local 
12811 70 61 79 6c 6f 61 64 2c 20 62 75 74 20 6e 6f 74  payload, but not
12812 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70 61   any overflow pa
12813 67 65 20 6f 72 0a 2a 2a 20 74 68 65 20 73 70 61  ge or.** the spa
12814 63 65 20 75 73 65 64 20 62 79 20 74 68 65 20 63  ce used by the c
12815 65 6c 6c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a  ell pointer..*/.
12816 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73  #ifndef NDEBUG.s
12817 74 61 74 69 63 20 75 31 36 20 63 65 6c 6c 53 69  tatic u16 cellSi
12818 7a 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  ze(MemPage *pPag
12819 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20  e, int iCell){. 
1281a 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a   CellInfo info;.
1281b 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61    sqlite3BtreePa
1281c 72 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69  rseCell(pPage, i
1281d 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20  Cell, &info);.  
1281e 72 65 74 75 72 6e 20 69 6e 66 6f 2e 6e 53 69 7a  return info.nSiz
1281f 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 73 74 61 74  e;.}.#endif.stat
12820 69 63 20 75 31 36 20 63 65 6c 6c 53 69 7a 65 50  ic u16 cellSizeP
12821 74 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  tr(MemPage *pPag
12822 65 2c 20 75 38 20 2a 70 43 65 6c 6c 29 7b 0a 20  e, u8 *pCell){. 
12823 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a   CellInfo info;.
12824 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61    sqlite3BtreePa
12825 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65  rseCellPtr(pPage
12826 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b  , pCell, &info);
12827 0a 20 20 72 65 74 75 72 6e 20 69 6e 66 6f 2e 6e  .  return info.n
12828 53 69 7a 65 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  Size;.}..#ifndef
12829 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
1282a 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 49 66  OVACUUM./*.** If
1282b 20 74 68 65 20 63 65 6c 6c 20 70 43 65 6c 6c 2c   the cell pCell,
1282c 20 70 61 72 74 20 6f 66 20 70 61 67 65 20 70 50   part of page pP
1282d 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70  age contains a p
1282e 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 6e 20  ointer.** to an 
1282f 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 69  overflow page, i
12830 6e 73 65 72 74 20 61 6e 20 65 6e 74 72 79 20 69  nsert an entry i
12831 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d  nto the pointer-
12832 6d 61 70 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6f  map.** for the o
12833 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 2a 2f  verflow page..*/
12834 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 72 6d  .static int ptrm
12835 61 70 50 75 74 4f 76 66 6c 50 74 72 28 4d 65 6d  apPutOvflPtr(Mem
12836 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20  Page *pPage, u8 
12837 2a 70 43 65 6c 6c 29 7b 0a 20 20 43 65 6c 6c 49  *pCell){.  CellI
12838 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 61 73 73 65  nfo info;.  asse
12839 72 74 28 20 70 43 65 6c 6c 21 3d 30 20 29 3b 0a  rt( pCell!=0 );.
1283a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61    sqlite3BtreePa
1283b 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65  rseCellPtr(pPage
1283c 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b  , pCell, &info);
1283d 0a 20 20 61 73 73 65 72 74 28 20 28 69 6e 66 6f  .  assert( (info
1283e 2e 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e 69  .nData+(pPage->i
1283f 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65  ntKey?0:info.nKe
12840 79 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f  y))==info.nPaylo
12841 61 64 20 29 3b 0a 20 20 69 66 28 20 28 69 6e 66  ad );.  if( (inf
12842 6f 2e 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e  o.nData+(pPage->
12843 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b  intKey?0:info.nK
12844 65 79 29 29 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c  ey))>info.nLocal
12845 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6f 76 66   ){.    Pgno ovf
12846 6c 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 43  l = get4byte(&pC
12847 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c  ell[info.iOverfl
12848 6f 77 5d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ow]);.    return
12849 20 70 74 72 6d 61 70 50 75 74 28 70 50 61 67 65   ptrmapPut(pPage
1284a 2d 3e 70 42 74 2c 20 6f 76 66 6c 2c 20 50 54 52  ->pBt, ovfl, PTR
1284b 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 70  MAP_OVERFLOW1, p
1284c 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d  Page->pgno);.  }
1284d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1284e 5f 4f 4b 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 49 66 20  _OK;.}./*.** If 
1284f 74 68 65 20 63 65 6c 6c 20 77 69 74 68 20 69 6e  the cell with in
12850 64 65 78 20 69 43 65 6c 6c 20 6f 6e 20 70 61 67  dex iCell on pag
12851 65 20 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73  e pPage contains
12852 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f   a pointer.** to
12853 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   an overflow pag
12854 65 2c 20 69 6e 73 65 72 74 20 61 6e 20 65 6e 74  e, insert an ent
12855 72 79 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e  ry into the poin
12856 74 65 72 2d 6d 61 70 0a 2a 2a 20 66 6f 72 20 74  ter-map.** for t
12857 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  he overflow page
12858 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
12859 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 4d 65  ptrmapPutOvfl(Me
1285a 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e  mPage *pPage, in
1285b 74 20 69 43 65 6c 6c 29 7b 0a 20 20 75 38 20 2a  t iCell){.  u8 *
1285c 70 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28  pCell;.  assert(
1285d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1285e 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
1285f 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 65 6c  mutex) );.  pCel
12860 6c 20 3d 20 66 69 6e 64 4f 76 65 72 66 6c 6f 77  l = findOverflow
12861 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c  Cell(pPage, iCel
12862 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 74 72  l);.  return ptr
12863 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50  mapPutOvflPtr(pP
12864 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 7d 0a 23  age, pCell);.}.#
12865 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65  endif.../*.** De
12866 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70 61 67  fragment the pag
12867 65 20 67 69 76 65 6e 2e 20 20 41 6c 6c 20 43 65  e given.  All Ce
12868 6c 6c 73 20 61 72 65 20 6d 6f 76 65 64 20 74 6f  lls are moved to
12869 20 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 74   the.** end of t
1286a 68 65 20 70 61 67 65 20 61 6e 64 20 61 6c 6c 20  he page and all 
1286b 66 72 65 65 20 73 70 61 63 65 20 69 73 20 63 6f  free space is co
1286c 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 6f 6e 65  llected into one
1286d 0a 2a 2a 20 62 69 67 20 46 72 65 65 42 6c 6b 20  .** big FreeBlk 
1286e 74 68 61 74 20 6f 63 63 75 72 73 20 69 6e 20 62  that occurs in b
1286f 65 74 77 65 65 6e 20 74 68 65 20 68 65 61 64 65  etween the heade
12870 72 20 61 6e 64 20 63 65 6c 6c 0a 2a 2a 20 70 6f  r and cell.** po
12871 69 6e 74 65 72 20 61 72 72 61 79 20 61 6e 64 20  inter array and 
12872 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
12873 20 61 72 65 61 2e 0a 2a 2f 0a 73 74 61 74 69 63   area..*/.static
12874 20 76 6f 69 64 20 64 65 66 72 61 67 6d 65 6e 74   void defragment
12875 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50  Page(MemPage *pP
12876 61 67 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20  age){.  int i;  
12877 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12878 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
12879 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20  er */.  int pc; 
1287a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1287b 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
1287c 20 61 20 69 2d 74 68 20 63 65 6c 6c 20 2a 2f 0a   a i-th cell */.
1287d 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20 20    int addr;     
1287e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1287f 4f 66 66 73 65 74 20 6f 66 20 66 69 72 73 74 20  Offset of first 
12880 62 79 74 65 20 61 66 74 65 72 20 63 65 6c 6c 20  byte after cell 
12881 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 2a 2f  pointer array */
12882 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20  .  int hdr;     
12883 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
12884 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 70   Offset to the p
12885 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20  age header */.  
12886 69 6e 74 20 73 69 7a 65 3b 20 20 20 20 20 20 20  int size;       
12887 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
12888 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20 2a 2f 0a  ze of a cell */.
12889 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65    int usableSize
1288a 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
1288b 4e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65  Number of usable
1288c 20 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67 65   bytes on a page
1288d 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66   */.  int cellOf
1288e 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20  fset;           
1288f 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68   /* Offset to th
12890 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61  e cell pointer a
12891 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 63 62  rray */.  int cb
12892 72 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rk;             
12893 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74       /* Offset t
12894 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65  o the cell conte
12895 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e 74  nt area */.  int
12896 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20   nCell;         
12897 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
12898 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68  r of cells on th
12899 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69  e page */.  unsi
1289a 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 3b  gned char *data;
1289b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
1289c 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 75 6e 73  ge data */.  uns
1289d 69 67 6e 65 64 20 63 68 61 72 20 2a 74 65 6d 70  igned char *temp
1289e 3b 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20  ;       /* Temp 
1289f 61 72 65 61 20 66 6f 72 20 63 65 6c 6c 20 63 6f  area for cell co
128a0 6e 74 65 6e 74 20 2a 2f 0a 0a 20 20 61 73 73 65  ntent */..  asse
128a1 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
128a2 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67  Iswriteable(pPag
128a3 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  e->pDbPage) );. 
128a4 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
128a5 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  pBt!=0 );.  asse
128a6 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e  rt( pPage->pBt->
128a7 75 73 61 62 6c 65 53 69 7a 65 20 3c 3d 20 53 51  usableSize <= SQ
128a8 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49  LITE_MAX_PAGE_SI
128a9 5a 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ZE );.  assert( 
128aa 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
128ab 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
128ac 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
128ad 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
128ae 6d 75 74 65 78 29 20 29 3b 0a 20 20 74 65 6d 70  mutex) );.  temp
128af 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 54   = sqlite3PagerT
128b0 65 6d 70 53 70 61 63 65 28 70 50 61 67 65 2d 3e  empSpace(pPage->
128b1 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20  pBt->pPager);.  
128b2 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44  data = pPage->aD
128b3 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61  ata;.  hdr = pPa
128b4 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20  ge->hdrOffset;. 
128b5 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 70 50   cellOffset = pP
128b6 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 3b  age->cellOffset;
128b7 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65  .  nCell = pPage
128b8 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72  ->nCell;.  asser
128b9 74 28 20 6e 43 65 6c 6c 3d 3d 67 65 74 32 62 79  t( nCell==get2by
128ba 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 29  te(&data[hdr+3])
128bb 20 29 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65   );.  usableSize
128bc 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75   = pPage->pBt->u
128bd 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63 62 72  sableSize;.  cbr
128be 6b 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  k = get2byte(&da
128bf 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 6d 65  ta[hdr+5]);.  me
128c0 6d 63 70 79 28 26 74 65 6d 70 5b 63 62 72 6b 5d  mcpy(&temp[cbrk]
128c1 2c 20 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20 75  , &data[cbrk], u
128c2 73 61 62 6c 65 53 69 7a 65 20 2d 20 63 62 72 6b  sableSize - cbrk
128c3 29 3b 0a 20 20 63 62 72 6b 20 3d 20 75 73 61 62  );.  cbrk = usab
128c4 6c 65 53 69 7a 65 3b 0a 20 20 66 6f 72 28 69 3d  leSize;.  for(i=
128c5 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29  0; i<nCell; i++)
128c6 7b 0a 20 20 20 20 75 38 20 2a 70 41 64 64 72 3b  {.    u8 *pAddr;
128c7 20 20 20 20 20 2f 2a 20 54 68 65 20 69 2d 74 68       /* The i-th
128c8 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f   cell pointer */
128c9 0a 20 20 20 20 70 41 64 64 72 20 3d 20 26 64 61  .    pAddr = &da
128ca 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20  ta[cellOffset + 
128cb 69 2a 32 5d 3b 0a 20 20 20 20 70 63 20 3d 20 67  i*2];.    pc = g
128cc 65 74 32 62 79 74 65 28 70 41 64 64 72 29 3b 0a  et2byte(pAddr);.
128cd 20 20 20 20 61 73 73 65 72 74 28 20 70 63 3c 70      assert( pc<p
128ce 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c  Page->pBt->usabl
128cf 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 73 69 7a  eSize );.    siz
128d0 65 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28  e = cellSizePtr(
128d1 70 50 61 67 65 2c 20 26 74 65 6d 70 5b 70 63 5d  pPage, &temp[pc]
128d2 29 3b 0a 20 20 20 20 63 62 72 6b 20 2d 3d 20 73  );.    cbrk -= s
128d3 69 7a 65 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  ize;.    memcpy(
128d4 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20 26 74 65  &data[cbrk], &te
128d5 6d 70 5b 70 63 5d 2c 20 73 69 7a 65 29 3b 0a 20  mp[pc], size);. 
128d6 20 20 20 70 75 74 32 62 79 74 65 28 70 41 64 64     put2byte(pAdd
128d7 72 2c 20 63 62 72 6b 29 3b 0a 20 20 7d 0a 20 20  r, cbrk);.  }.  
128d8 61 73 73 65 72 74 28 20 63 62 72 6b 3e 3d 63 65  assert( cbrk>=ce
128d9 6c 6c 4f 66 66 73 65 74 2b 32 2a 6e 43 65 6c 6c  llOffset+2*nCell
128da 20 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26   );.  put2byte(&
128db 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 63 62 72  data[hdr+5], cbr
128dc 6b 29 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 31  k);.  data[hdr+1
128dd 5d 20 3d 20 30 3b 0a 20 20 64 61 74 61 5b 68 64  ] = 0;.  data[hd
128de 72 2b 32 5d 20 3d 20 30 3b 0a 20 20 64 61 74 61  r+2] = 0;.  data
128df 5b 68 64 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 61  [hdr+7] = 0;.  a
128e0 64 64 72 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74  ddr = cellOffset
128e1 2b 32 2a 6e 43 65 6c 6c 3b 0a 20 20 6d 65 6d 73  +2*nCell;.  mems
128e2 65 74 28 26 64 61 74 61 5b 61 64 64 72 5d 2c 20  et(&data[addr], 
128e3 30 2c 20 63 62 72 6b 2d 61 64 64 72 29 3b 0a 7d  0, cbrk-addr);.}
128e4 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ../*.** Allocate
128e5 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f 66 20   nByte bytes of 
128e6 73 70 61 63 65 20 6f 6e 20 61 20 70 61 67 65 2e  space on a page.
128e7 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  .**.** Return th
128e8 65 20 69 6e 64 65 78 20 69 6e 74 6f 20 70 50 61  e index into pPa
128e9 67 65 2d 3e 61 44 61 74 61 5b 5d 20 6f 66 20 74  ge->aData[] of t
128ea 68 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66  he first byte of
128eb 0a 2a 2a 20 74 68 65 20 6e 65 77 20 61 6c 6c 6f  .** the new allo
128ec 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 63 61 6c  cation.  The cal
128ed 6c 65 72 20 67 75 61 72 61 6e 74 65 65 73 20 74  ler guarantees t
128ee 68 61 74 20 74 68 65 72 65 20 69 73 20 65 6e 6f  hat there is eno
128ef 75 67 68 0a 2a 2a 20 73 70 61 63 65 2e 20 20 54  ugh.** space.  T
128f0 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
128f1 20 6e 65 76 65 72 20 66 61 69 6c 2e 0a 2a 2a 0a   never fail..**.
128f2 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 63  ** If the page c
128f3 6f 6e 74 61 69 6e 73 20 6e 42 79 74 65 73 20 6f  ontains nBytes o
128f4 66 20 66 72 65 65 20 73 70 61 63 65 20 62 75 74  f free space but
128f5 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69   does not contai
128f6 6e 0a 2a 2a 20 6e 42 79 74 65 73 20 6f 66 20 63  n.** nBytes of c
128f7 6f 6e 74 69 67 75 6f 75 73 20 66 72 65 65 20 73  ontiguous free s
128f8 70 61 63 65 2c 20 74 68 65 6e 20 74 68 69 73 20  pace, then this 
128f9 72 6f 75 74 69 6e 65 20 61 75 74 6f 6d 61 74 69  routine automati
128fa 63 61 6c 6c 79 0a 2a 2a 20 63 61 6c 6c 73 20 64  cally.** calls d
128fb 65 66 72 61 67 65 6d 65 6e 74 50 61 67 65 28 29  efragementPage()
128fc 20 74 6f 20 63 6f 6e 73 6f 6c 69 64 61 74 65 20   to consolidate 
128fd 61 6c 6c 20 66 72 65 65 20 73 70 61 63 65 20 62  all free space b
128fe 65 66 6f 72 65 20 0a 2a 2a 20 61 6c 6c 6f 63 61  efore .** alloca
128ff 74 69 6e 67 20 74 68 65 20 6e 65 77 20 63 68 75  ting the new chu
12900 6e 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  nk..*/.static in
12901 74 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 28  t allocateSpace(
12902 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20  MemPage *pPage, 
12903 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e  int nByte){.  in
12904 74 20 61 64 64 72 2c 20 70 63 2c 20 68 64 72 3b  t addr, pc, hdr;
12905 0a 20 20 69 6e 74 20 73 69 7a 65 3b 0a 20 20 69  .  int size;.  i
12906 6e 74 20 6e 46 72 61 67 3b 0a 20 20 69 6e 74 20  nt nFrag;.  int 
12907 74 6f 70 3b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c  top;.  int nCell
12908 3b 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73  ;.  int cellOffs
12909 65 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  et;.  unsigned c
1290a 68 61 72 20 2a 64 61 74 61 3b 0a 20 20 0a 20 20  har *data;.  .  
1290b 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44  data = pPage->aD
1290c 61 74 61 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ata;.  assert( s
1290d 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
1290e 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44  teable(pPage->pD
1290f 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65  bPage) );.  asse
12910 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 20 29  rt( pPage->pBt )
12911 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
12912 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
12913 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
12914 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  ) );.  assert( n
12915 42 79 74 65 3e 3d 30 20 29 3b 20 20 2f 2a 20 4d  Byte>=0 );  /* M
12916 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65  inimum cell size
12917 20 69 73 20 34 20 2a 2f 0a 20 20 61 73 73 65 72   is 4 */.  asser
12918 74 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 3e  t( pPage->nFree>
12919 3d 6e 42 79 74 65 20 29 3b 0a 20 20 61 73 73 65  =nByte );.  asse
1291a 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72  rt( pPage->nOver
1291b 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 70 50 61  flow==0 );.  pPa
1291c 67 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 6e 42 79  ge->nFree -= nBy
1291d 74 65 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67  te;.  hdr = pPag
1291e 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 0a 20  e->hdrOffset;.. 
1291f 20 6e 46 72 61 67 20 3d 20 64 61 74 61 5b 68 64   nFrag = data[hd
12920 72 2b 37 5d 3b 0a 20 20 69 66 28 20 6e 46 72 61  r+7];.  if( nFra
12921 67 3c 36 30 20 29 7b 0a 20 20 20 20 2f 2a 20 53  g<60 ){.    /* S
12922 65 61 72 63 68 20 74 68 65 20 66 72 65 65 6c 69  earch the freeli
12923 73 74 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61  st looking for a
12924 20 73 6c 6f 74 20 62 69 67 20 65 6e 6f 75 67 68   slot big enough
12925 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 0a   to satisfy the.
12926 20 20 20 20 2a 2a 20 73 70 61 63 65 20 72 65 71      ** space req
12927 75 65 73 74 2e 20 2a 2f 0a 20 20 20 20 61 64 64  uest. */.    add
12928 72 20 3d 20 68 64 72 2b 31 3b 0a 20 20 20 20 77  r = hdr+1;.    w
12929 68 69 6c 65 28 20 28 70 63 20 3d 20 67 65 74 32  hile( (pc = get2
1292a 62 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d  byte(&data[addr]
1292b 29 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 69  ))>0 ){.      si
1292c 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  ze = get2byte(&d
1292d 61 74 61 5b 70 63 2b 32 5d 29 3b 0a 20 20 20 20  ata[pc+2]);.    
1292e 20 20 69 66 28 20 73 69 7a 65 3e 3d 6e 42 79 74    if( size>=nByt
1292f 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  e ){.        if(
12930 20 73 69 7a 65 3c 6e 42 79 74 65 2b 34 20 29 7b   size<nByte+4 ){
12931 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70  .          memcp
12932 79 28 26 64 61 74 61 5b 61 64 64 72 5d 2c 20 26  y(&data[addr], &
12933 64 61 74 61 5b 70 63 5d 2c 20 32 29 3b 0a 20 20  data[pc], 2);.  
12934 20 20 20 20 20 20 20 20 64 61 74 61 5b 68 64 72          data[hdr
12935 2b 37 5d 20 3d 20 6e 46 72 61 67 20 2b 20 73 69  +7] = nFrag + si
12936 7a 65 20 2d 20 6e 42 79 74 65 3b 0a 20 20 20 20  ze - nByte;.    
12937 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 63 3b        return pc;
12938 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
12939 20 20 20 20 20 20 20 20 20 20 70 75 74 32 62 79            put2by
1293a 74 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 2c 20  te(&data[pc+2], 
1293b 73 69 7a 65 2d 6e 42 79 74 65 29 3b 0a 20 20 20  size-nByte);.   
1293c 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 63         return pc
1293d 20 2b 20 73 69 7a 65 20 2d 20 6e 42 79 74 65 3b   + size - nByte;
1293e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1293f 20 7d 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20   }.      addr = 
12940 70 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  pc;.    }.  }.. 
12941 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d   /* Allocate mem
12942 6f 72 79 20 66 72 6f 6d 20 74 68 65 20 67 61 70  ory from the gap
12943 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20   in between the 
12944 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72  cell pointer arr
12945 61 79 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20  ay.  ** and the 
12946 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65  cell content are
12947 61 2e 0a 20 20 2a 2f 0a 20 20 74 6f 70 20 3d 20  a..  */.  top = 
12948 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
12949 64 72 2b 35 5d 29 3b 0a 20 20 6e 43 65 6c 6c 20  dr+5]);.  nCell 
1294a 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
1294b 5b 68 64 72 2b 33 5d 29 3b 0a 20 20 63 65 6c 6c  [hdr+3]);.  cell
1294c 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e  Offset = pPage->
1294d 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 69 66  cellOffset;.  if
1294e 28 20 6e 46 72 61 67 3e 3d 36 30 20 7c 7c 20 63  ( nFrag>=60 || c
1294f 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 6e 43  ellOffset + 2*nC
12950 65 6c 6c 20 3e 20 74 6f 70 20 2d 20 6e 42 79 74  ell > top - nByt
12951 65 20 29 7b 0a 20 20 20 20 64 65 66 72 61 67 6d  e ){.    defragm
12952 65 6e 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a  entPage(pPage);.
12953 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79      top = get2by
12954 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29  te(&data[hdr+5])
12955 3b 0a 20 20 7d 0a 20 20 74 6f 70 20 2d 3d 20 6e  ;.  }.  top -= n
12956 42 79 74 65 3b 0a 20 20 61 73 73 65 72 74 28 20  Byte;.  assert( 
12957 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 6e  cellOffset + 2*n
12958 43 65 6c 6c 20 3c 3d 20 74 6f 70 20 29 3b 0a 20  Cell <= top );. 
12959 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b   put2byte(&data[
1295a 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a 20 20  hdr+5], top);.  
1295b 72 65 74 75 72 6e 20 74 6f 70 3b 0a 7d 0a 0a 2f  return top;.}../
1295c 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 73 65  *.** Return a se
1295d 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 50 61  ction of the pPa
1295e 67 65 2d 3e 61 44 61 74 61 20 74 6f 20 74 68 65  ge->aData to the
1295f 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a 20 54 68   freelist..** Th
12960 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20  e first byte of 
12961 74 68 65 20 6e 65 77 20 66 72 65 65 20 62 6c 6f  the new free blo
12962 63 6b 20 69 73 20 70 50 61 67 65 2d 3e 61 44 69  ck is pPage->aDi
12963 73 6b 5b 73 74 61 72 74 5d 0a 2a 2a 20 61 6e 64  sk[start].** and
12964 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
12965 20 62 6c 6f 63 6b 20 69 73 20 22 73 69 7a 65 22   block is "size"
12966 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 4d 6f   bytes..**.** Mo
12967 73 74 20 6f 66 20 74 68 65 20 65 66 66 6f 72 74  st of the effort
12968 20 68 65 72 65 20 69 73 20 69 6e 76 6f 6c 76 65   here is involve
12969 64 20 69 6e 20 63 6f 61 6c 65 73 69 6e 67 20 61  d in coalesing a
1296a 64 6a 61 63 65 6e 74 0a 2a 2a 20 66 72 65 65 20  djacent.** free 
1296b 62 6c 6f 63 6b 73 20 69 6e 74 6f 20 61 20 73 69  blocks into a si
1296c 6e 67 6c 65 20 62 69 67 20 66 72 65 65 20 62 6c  ngle big free bl
1296d 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ock..*/.static v
1296e 6f 69 64 20 66 72 65 65 53 70 61 63 65 28 4d 65  oid freeSpace(Me
1296f 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e  mPage *pPage, in
12970 74 20 73 74 61 72 74 2c 20 69 6e 74 20 73 69 7a  t start, int siz
12971 65 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 2c 20  e){.  int addr, 
12972 70 62 65 67 69 6e 2c 20 68 64 72 3b 0a 20 20 75  pbegin, hdr;.  u
12973 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61  nsigned char *da
12974 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74  ta = pPage->aDat
12975 61 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50  a;..  assert( pP
12976 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20  age->pBt!=0 );. 
12977 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
12978 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
12979 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
1297a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 74   );.  assert( st
1297b 61 72 74 3e 3d 70 50 61 67 65 2d 3e 68 64 72 4f  art>=pPage->hdrO
1297c 66 66 73 65 74 2b 36 2b 28 70 50 61 67 65 2d 3e  ffset+6+(pPage->
1297d 6c 65 61 66 3f 30 3a 34 29 20 29 3b 0a 20 20 61  leaf?0:4) );.  a
1297e 73 73 65 72 74 28 20 28 73 74 61 72 74 20 2b 20  ssert( (start + 
1297f 73 69 7a 65 29 3c 3d 70 50 61 67 65 2d 3e 70 42  size)<=pPage->pB
12980 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b  t->usableSize );
12981 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
12982 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50  e3_mutex_held(pP
12983 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  age->pBt->mutex)
12984 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69   );.  assert( si
12985 7a 65 3e 3d 30 20 29 3b 20 20 20 2f 2a 20 4d 69  ze>=0 );   /* Mi
12986 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20  nimum cell size 
12987 69 73 20 34 20 2a 2f 0a 0a 23 69 66 64 65 66 20  is 4 */..#ifdef 
12988 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45  SQLITE_SECURE_DE
12989 4c 45 54 45 0a 20 20 2f 2a 20 4f 76 65 72 77 72  LETE.  /* Overwr
1298a 69 74 65 20 64 65 6c 65 74 65 64 20 69 6e 66 6f  ite deleted info
1298b 72 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a 65 72  rmation with zer
1298c 6f 73 20 77 68 65 6e 20 74 68 65 20 53 45 43 55  os when the SECU
1298d 52 45 5f 44 45 4c 45 54 45 20 0a 20 20 2a 2a 20  RE_DELETE .  ** 
1298e 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65  option is enable
1298f 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  d at compile-tim
12990 65 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 64  e */.  memset(&d
12991 61 74 61 5b 73 74 61 72 74 5d 2c 20 30 2c 20 73  ata[start], 0, s
12992 69 7a 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20  ize);.#endif..  
12993 2f 2a 20 41 64 64 20 74 68 65 20 73 70 61 63 65  /* Add the space
12994 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 6c   back into the l
12995 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 66 72  inked list of fr
12996 65 65 62 6c 6f 63 6b 73 20 2a 2f 0a 20 20 68 64  eeblocks */.  hd
12997 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66  r = pPage->hdrOf
12998 66 73 65 74 3b 0a 20 20 61 64 64 72 20 3d 20 68  fset;.  addr = h
12999 64 72 20 2b 20 31 3b 0a 20 20 77 68 69 6c 65 28  dr + 1;.  while(
1299a 20 28 70 62 65 67 69 6e 20 3d 20 67 65 74 32 62   (pbegin = get2b
1299b 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29  yte(&data[addr])
1299c 29 3c 73 74 61 72 74 20 26 26 20 70 62 65 67 69  )<start && pbegi
1299d 6e 3e 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  n>0 ){.    asser
1299e 74 28 20 70 62 65 67 69 6e 3c 3d 70 50 61 67 65  t( pbegin<=pPage
1299f 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  ->pBt->usableSiz
129a0 65 2d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 72  e-4 );.    asser
129a1 74 28 20 70 62 65 67 69 6e 3e 61 64 64 72 20 29  t( pbegin>addr )
129a2 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 70 62 65  ;.    addr = pbe
129a3 67 69 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  gin;.  }.  asser
129a4 74 28 20 70 62 65 67 69 6e 3c 3d 70 50 61 67 65  t( pbegin<=pPage
129a5 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  ->pBt->usableSiz
129a6 65 2d 34 20 29 3b 0a 20 20 61 73 73 65 72 74 28  e-4 );.  assert(
129a7 20 70 62 65 67 69 6e 3e 61 64 64 72 20 7c 7c 20   pbegin>addr || 
129a8 70 62 65 67 69 6e 3d 3d 30 20 29 3b 0a 20 20 70  pbegin==0 );.  p
129a9 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 61 64  ut2byte(&data[ad
129aa 64 72 5d 2c 20 73 74 61 72 74 29 3b 0a 20 20 70  dr], start);.  p
129ab 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 73 74  ut2byte(&data[st
129ac 61 72 74 5d 2c 20 70 62 65 67 69 6e 29 3b 0a 20  art], pbegin);. 
129ad 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b   put2byte(&data[
129ae 73 74 61 72 74 2b 32 5d 2c 20 73 69 7a 65 29 3b  start+2], size);
129af 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20  .  pPage->nFree 
129b0 2b 3d 20 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 43  += size;..  /* C
129b1 6f 61 6c 65 73 63 65 20 61 64 6a 61 63 65 6e 74  oalesce adjacent
129b2 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a   free blocks */.
129b3 20 20 61 64 64 72 20 3d 20 70 50 61 67 65 2d 3e    addr = pPage->
129b4 68 64 72 4f 66 66 73 65 74 20 2b 20 31 3b 0a 20  hdrOffset + 1;. 
129b5 20 77 68 69 6c 65 28 20 28 70 62 65 67 69 6e 20   while( (pbegin 
129b6 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
129b7 5b 61 64 64 72 5d 29 29 3e 30 20 29 7b 0a 20 20  [addr]))>0 ){.  
129b8 20 20 69 6e 74 20 70 6e 65 78 74 2c 20 70 73 69    int pnext, psi
129b9 7a 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ze;.    assert( 
129ba 70 62 65 67 69 6e 3e 61 64 64 72 20 29 3b 0a 20  pbegin>addr );. 
129bb 20 20 20 61 73 73 65 72 74 28 20 70 62 65 67 69     assert( pbegi
129bc 6e 3c 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75  n<=pPage->pBt->u
129bd 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 3b 0a 20  sableSize-4 );. 
129be 20 20 20 70 6e 65 78 74 20 3d 20 67 65 74 32 62     pnext = get2b
129bf 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e  yte(&data[pbegin
129c0 5d 29 3b 0a 20 20 20 20 70 73 69 7a 65 20 3d 20  ]);.    psize = 
129c1 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  get2byte(&data[p
129c2 62 65 67 69 6e 2b 32 5d 29 3b 0a 20 20 20 20 69  begin+2]);.    i
129c3 66 28 20 70 62 65 67 69 6e 20 2b 20 70 73 69 7a  f( pbegin + psiz
129c4 65 20 2b 20 33 20 3e 3d 20 70 6e 65 78 74 20 26  e + 3 >= pnext &
129c5 26 20 70 6e 65 78 74 3e 30 20 29 7b 0a 20 20 20  & pnext>0 ){.   
129c6 20 20 20 69 6e 74 20 66 72 61 67 20 3d 20 70 6e     int frag = pn
129c7 65 78 74 20 2d 20 28 70 62 65 67 69 6e 2b 70 73  ext - (pbegin+ps
129c8 69 7a 65 29 3b 0a 20 20 20 20 20 20 61 73 73 65  ize);.      asse
129c9 72 74 28 20 66 72 61 67 3c 3d 64 61 74 61 5b 70  rt( frag<=data[p
129ca 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b  Page->hdrOffset+
129cb 37 5d 20 29 3b 0a 20 20 20 20 20 20 64 61 74 61  7] );.      data
129cc 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65  [pPage->hdrOffse
129cd 74 2b 37 5d 20 2d 3d 20 66 72 61 67 3b 0a 20 20  t+7] -= frag;.  
129ce 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61      put2byte(&da
129cf 74 61 5b 70 62 65 67 69 6e 5d 2c 20 67 65 74 32  ta[pbegin], get2
129d0 62 79 74 65 28 26 64 61 74 61 5b 70 6e 65 78 74  byte(&data[pnext
129d1 5d 29 29 3b 0a 20 20 20 20 20 20 70 75 74 32 62  ]));.      put2b
129d2 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e  yte(&data[pbegin
129d3 2b 32 5d 2c 20 70 6e 65 78 74 2b 67 65 74 32 62  +2], pnext+get2b
129d4 79 74 65 28 26 64 61 74 61 5b 70 6e 65 78 74 2b  yte(&data[pnext+
129d5 32 5d 29 2d 70 62 65 67 69 6e 29 3b 0a 20 20 20  2])-pbegin);.   
129d6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 64   }else{.      ad
129d7 64 72 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20 20  dr = pbegin;.   
129d8 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
129d9 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
129da 20 61 72 65 61 20 62 65 67 69 6e 73 20 77 69 74   area begins wit
129db 68 20 61 20 66 72 65 65 62 6c 6f 63 6b 2c 20 72  h a freeblock, r
129dc 65 6d 6f 76 65 20 69 74 2e 20 2a 2f 0a 20 20 69  emove it. */.  i
129dd 66 28 20 64 61 74 61 5b 68 64 72 2b 31 5d 3d 3d  f( data[hdr+1]==
129de 64 61 74 61 5b 68 64 72 2b 35 5d 20 26 26 20 64  data[hdr+5] && d
129df 61 74 61 5b 68 64 72 2b 32 5d 3d 3d 64 61 74 61  ata[hdr+2]==data
129e0 5b 68 64 72 2b 36 5d 20 29 7b 0a 20 20 20 20 69  [hdr+6] ){.    i
129e1 6e 74 20 74 6f 70 3b 0a 20 20 20 20 70 62 65 67  nt top;.    pbeg
129e2 69 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  in = get2byte(&d
129e3 61 74 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 20  ata[hdr+1]);.   
129e4 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 68 64   memcpy(&data[hd
129e5 72 2b 31 5d 2c 20 26 64 61 74 61 5b 70 62 65 67  r+1], &data[pbeg
129e6 69 6e 5d 2c 20 32 29 3b 0a 20 20 20 20 74 6f 70  in], 2);.    top
129e7 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
129e8 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 70  a[hdr+5]);.    p
129e9 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  ut2byte(&data[hd
129ea 72 2b 35 5d 2c 20 74 6f 70 20 2b 20 67 65 74 32  r+5], top + get2
129eb 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69  byte(&data[pbegi
129ec 6e 2b 32 5d 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  n+2]));.  }.}../
129ed 2a 0a 2a 2a 20 44 65 63 6f 64 65 20 74 68 65 20  *.** Decode the 
129ee 66 6c 61 67 73 20 62 79 74 65 20 28 74 68 65 20  flags byte (the 
129ef 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68  first byte of th
129f0 65 20 68 65 61 64 65 72 29 20 66 6f 72 20 61 20  e header) for a 
129f1 70 61 67 65 0a 2a 2a 20 61 6e 64 20 69 6e 69 74  page.** and init
129f2 69 61 6c 69 7a 65 20 66 69 65 6c 64 73 20 6f 66  ialize fields of
129f3 20 74 68 65 20 4d 65 6d 50 61 67 65 20 73 74 72   the MemPage str
129f4 75 63 74 75 72 65 20 61 63 63 6f 72 64 69 6e 67  ucture according
129f5 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74  ly..**.** Only t
129f6 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d  he following com
129f7 62 69 6e 61 74 69 6f 6e 73 20 61 72 65 20 73 75  binations are su
129f8 70 70 6f 72 74 65 64 2e 20 20 41 6e 79 74 68 69  pported.  Anythi
129f9 6e 67 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20  ng different.** 
129fa 69 6e 64 69 63 61 74 65 73 20 61 20 63 6f 72 72  indicates a corr
129fb 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69 6c  upt database fil
129fc 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  es:.**.**       
129fd 20 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 0a 2a    PTF_ZERODATA.*
129fe 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f 5a 45  *         PTF_ZE
129ff 52 4f 44 41 54 41 20 7c 20 50 54 46 5f 4c 45 41  RODATA | PTF_LEA
12a00 46 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46  F.**         PTF
12a01 5f 4c 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f  _LEAFDATA | PTF_
12a02 49 4e 54 4b 45 59 0a 2a 2a 20 20 20 20 20 20 20  INTKEY.**       
12a03 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 7c    PTF_LEAFDATA |
12a04 20 50 54 46 5f 49 4e 54 4b 45 59 20 7c 20 50 54   PTF_INTKEY | PT
12a05 46 5f 4c 45 41 46 0a 2a 2f 0a 73 74 61 74 69 63  F_LEAF.*/.static
12a06 20 69 6e 74 20 64 65 63 6f 64 65 46 6c 61 67 73   int decodeFlags
12a07 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
12a08 20 69 6e 74 20 66 6c 61 67 42 79 74 65 29 7b 0a   int flagByte){.
12a09 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b    BtShared *pBt;
12a0a 20 20 20 20 20 2f 2a 20 41 20 63 6f 70 79 20 6f       /* A copy o
12a0b 66 20 70 50 61 67 65 2d 3e 70 42 74 20 2a 2f 0a  f pPage->pBt */.
12a0c 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
12a0d 2d 3e 68 64 72 4f 66 66 73 65 74 3d 3d 28 70 50  ->hdrOffset==(pP
12a0e 61 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20 3f 20 31  age->pgno==1 ? 1
12a0f 30 30 20 3a 20 30 29 20 29 3b 0a 20 20 61 73 73  00 : 0) );.  ass
12a10 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
12a11 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
12a12 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
12a13 70 50 61 67 65 2d 3e 6c 65 61 66 20 3d 20 66 6c  pPage->leaf = fl
12a14 61 67 42 79 74 65 3e 3e 33 3b 20 20 61 73 73 65  agByte>>3;  asse
12a15 72 74 28 20 50 54 46 5f 4c 45 41 46 20 3d 3d 20  rt( PTF_LEAF == 
12a16 31 3c 3c 33 20 29 3b 0a 20 20 66 6c 61 67 42 79  1<<3 );.  flagBy
12a17 74 65 20 26 3d 20 7e 50 54 46 5f 4c 45 41 46 3b  te &= ~PTF_LEAF;
12a18 0a 20 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50  .  pPage->childP
12a19 74 72 53 69 7a 65 20 3d 20 34 2d 34 2a 70 50 61  trSize = 4-4*pPa
12a1a 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 70 42 74 20  ge->leaf;.  pBt 
12a1b 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20  = pPage->pBt;.  
12a1c 69 66 28 20 66 6c 61 67 42 79 74 65 3d 3d 28 50  if( flagByte==(P
12a1d 54 46 5f 4c 45 41 46 44 41 54 41 20 7c 20 50 54  TF_LEAFDATA | PT
12a1e 46 5f 49 4e 54 4b 45 59 29 20 29 7b 0a 20 20 20  F_INTKEY) ){.   
12a1f 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3d   pPage->intKey =
12a20 20 31 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 68   1;.    pPage->h
12a21 61 73 44 61 74 61 20 3d 20 70 50 61 67 65 2d 3e  asData = pPage->
12a22 6c 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65 2d  leaf;.    pPage-
12a23 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 42 74 2d  >maxLocal = pBt-
12a24 3e 6d 61 78 4c 65 61 66 3b 0a 20 20 20 20 70 50  >maxLeaf;.    pP
12a25 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20  age->minLocal = 
12a26 70 42 74 2d 3e 6d 69 6e 4c 65 61 66 3b 0a 20 20  pBt->minLeaf;.  
12a27 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 42 79  }else if( flagBy
12a28 74 65 3d 3d 50 54 46 5f 5a 45 52 4f 44 41 54 41  te==PTF_ZERODATA
12a29 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69   ){.    pPage->i
12a2a 6e 74 4b 65 79 20 3d 20 30 3b 0a 20 20 20 20 70  ntKey = 0;.    p
12a2b 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 3d 20  Page->hasData = 
12a2c 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61  0;.    pPage->ma
12a2d 78 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61  xLocal = pBt->ma
12a2e 78 4c 6f 63 61 6c 3b 0a 20 20 20 20 70 50 61 67  xLocal;.    pPag
12a2f 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42  e->minLocal = pB
12a30 74 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 7d  t->minLocal;.  }
12a31 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
12a32 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
12a33 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75  BKPT;.  }.  retu
12a34 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
12a35 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a  ./*.** Initializ
12a36 65 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20  e the auxiliary 
12a37 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20  information for 
12a38 61 20 64 69 73 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a  a disk block..**
12a39 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
12a3a 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e  E_OK on success.
12a3b 20 20 49 66 20 77 65 20 73 65 65 20 74 68 61 74    If we see that
12a3c 20 74 68 65 20 70 61 67 65 20 64 6f 65 73 0a 2a   the page does.*
12a3d 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20  * not contain a 
12a3e 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61  well-formed data
12a3f 62 61 73 65 20 70 61 67 65 2c 20 74 68 65 6e 20  base page, then 
12a40 72 65 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54  return .** SQLIT
12a41 45 5f 43 4f 52 52 55 50 54 2e 20 20 4e 6f 74 65  E_CORRUPT.  Note
12a42 20 74 68 61 74 20 61 20 72 65 74 75 72 6e 20 6f   that a return o
12a43 66 20 53 51 4c 49 54 45 5f 4f 4b 20 64 6f 65 73  f SQLITE_OK does
12a44 20 6e 6f 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65   not.** guarante
12a45 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20  e that the page 
12a46 69 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 2e 20  is well-formed. 
12a47 20 49 74 20 6f 6e 6c 79 20 73 68 6f 77 73 20 74   It only shows t
12a48 68 61 74 0a 2a 2a 20 77 65 20 66 61 69 6c 65 64  hat.** we failed
12a49 20 74 6f 20 64 65 74 65 63 74 20 61 6e 79 20 63   to detect any c
12a4a 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 53 51  orruption..*/.SQ
12a4b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
12a4c 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69   sqlite3BtreeIni
12a4d 74 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70  tPage(MemPage *p
12a4e 50 61 67 65 29 7b 0a 0a 20 20 61 73 73 65 72 74  Page){..  assert
12a4f 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20  ( pPage->pBt!=0 
12a50 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
12a51 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
12a52 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65  pPage->pBt->mute
12a53 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
12a54 70 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 73 71 6c  pPage->pgno==sql
12a55 69 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d  ite3PagerPagenum
12a56 62 65 72 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ber(pPage->pDbPa
12a57 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ge) );.  assert(
12a58 20 70 50 61 67 65 20 3d 3d 20 73 71 6c 69 74 65   pPage == sqlite
12a59 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70  3PagerGetExtra(p
12a5a 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
12a5b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
12a5c 65 2d 3e 61 44 61 74 61 20 3d 3d 20 73 71 6c 69  e->aData == sqli
12a5d 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28  te3PagerGetData(
12a5e 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20  pPage->pDbPage) 
12a5f 29 3b 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65  );..  if( !pPage
12a60 2d 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20  ->isInit ){.    
12a61 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20  int pc;         
12a62 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
12a63 20 61 20 66 72 65 65 62 6c 6f 63 6b 20 77 69 74   a freeblock wit
12a64 68 69 6e 20 70 50 61 67 65 2d 3e 61 44 61 74 61  hin pPage->aData
12a65 5b 5d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 68 64  [] */.    int hd
12a66 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  r;           /* 
12a67 4f 66 66 73 65 74 20 74 6f 20 62 65 67 69 6e 6e  Offset to beginn
12a68 69 6e 67 20 6f 66 20 70 61 67 65 20 68 65 61 64  ing of page head
12a69 65 72 20 2a 2f 0a 20 20 20 20 75 38 20 2a 64 61  er */.    u8 *da
12a6a 74 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ta;          /* 
12a6b 45 71 75 61 6c 20 74 6f 20 70 50 61 67 65 2d 3e  Equal to pPage->
12a6c 61 44 61 74 61 20 2a 2f 0a 20 20 20 20 42 74 53  aData */.    BtS
12a6d 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20  hared *pBt;     
12a6e 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 62     /* The main b
12a6f 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a  tree structure *
12a70 2f 0a 20 20 20 20 69 6e 74 20 75 73 61 62 6c 65  /.    int usable
12a71 53 69 7a 65 3b 20 20 20 20 2f 2a 20 41 6d 6f 75  Size;    /* Amou
12a72 6e 74 20 6f 66 20 75 73 61 62 6c 65 20 73 70 61  nt of usable spa
12a73 63 65 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20  ce on each page 
12a74 2a 2f 0a 20 20 20 20 69 6e 74 20 63 65 6c 6c 4f  */.    int cellO
12a75 66 66 73 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66  ffset;    /* Off
12a76 73 65 74 20 66 72 6f 6d 20 73 74 61 72 74 20 6f  set from start o
12a77 66 20 70 61 67 65 20 74 6f 20 66 69 72 73 74 20  f page to first 
12a78 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a  cell pointer */.
12a79 20 20 20 20 69 6e 74 20 6e 46 72 65 65 3b 20 20      int nFree;  
12a7a 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
12a7b 20 6f 66 20 75 6e 75 73 65 64 20 62 79 74 65 73   of unused bytes
12a7c 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a   on the page */.
12a7d 20 20 20 20 69 6e 74 20 74 6f 70 3b 20 20 20 20      int top;    
12a7e 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
12a7f 62 79 74 65 20 6f 66 20 74 68 65 20 63 65 6c 6c  byte of the cell
12a80 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f   content area */
12a81 0a 0a 20 20 20 20 70 42 74 20 3d 20 70 50 61 67  ..    pBt = pPag
12a82 65 2d 3e 70 42 74 3b 0a 0a 20 20 20 20 68 64 72  e->pBt;..    hdr
12a83 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66   = pPage->hdrOff
12a84 73 65 74 3b 0a 20 20 20 20 64 61 74 61 20 3d 20  set;.    data = 
12a85 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20  pPage->aData;.  
12a86 20 20 69 66 28 20 64 65 63 6f 64 65 46 6c 61 67    if( decodeFlag
12a87 73 28 70 50 61 67 65 2c 20 64 61 74 61 5b 68 64  s(pPage, data[hd
12a88 72 5d 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c  r]) ) return SQL
12a89 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
12a8a 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42  ;.    assert( pB
12a8b 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35 31 32  t->pageSize>=512
12a8c 20 26 26 20 70 42 74 2d 3e 70 61 67 65 53 69 7a   && pBt->pageSiz
12a8d 65 3c 3d 33 32 37 36 38 20 29 3b 0a 20 20 20 20  e<=32768 );.    
12a8e 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20  pPage->maskPage 
12a8f 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20  = pBt->pageSize 
12a90 2d 20 31 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  - 1;.    pPage->
12a91 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20  nOverflow = 0;. 
12a92 20 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20     usableSize = 
12a93 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b  pBt->usableSize;
12a94 0a 20 20 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c  .    pPage->cell
12a95 4f 66 66 73 65 74 20 3d 20 63 65 6c 6c 4f 66 66  Offset = cellOff
12a96 73 65 74 20 3d 20 68 64 72 20 2b 20 31 32 20 2d  set = hdr + 12 -
12a97 20 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a   4*pPage->leaf;.
12a98 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79      top = get2by
12a99 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29  te(&data[hdr+5])
12a9a 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 43 65  ;.    pPage->nCe
12a9b 6c 6c 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  ll = get2byte(&d
12a9c 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a 20 20 20  ata[hdr+3]);.   
12a9d 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c   if( pPage->nCel
12a9e 6c 3e 4d 58 5f 43 45 4c 4c 28 70 42 74 29 20 29  l>MX_CELL(pBt) )
12a9f 7b 0a 20 20 20 20 20 20 2f 2a 20 54 6f 20 6d 61  {.      /* To ma
12aa0 6e 79 20 63 65 6c 6c 73 20 66 6f 72 20 61 20 73  ny cells for a s
12aa1 69 6e 67 6c 65 20 70 61 67 65 2e 20 20 54 68 65  ingle page.  The
12aa2 20 70 61 67 65 20 6d 75 73 74 20 62 65 20 63 6f   page must be co
12aa3 72 72 75 70 74 20 2a 2f 0a 20 20 20 20 20 20 72  rrupt */.      r
12aa4 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
12aa5 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d  RUPT_BKPT;.    }
12aa6 0a 20 20 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75  .  .    /* Compu
12aa7 74 65 20 74 68 65 20 74 6f 74 61 6c 20 66 72 65  te the total fre
12aa8 65 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20 70  e space on the p
12aa9 61 67 65 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20  age */.    pc = 
12aaa 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
12aab 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6e 46 72 65  dr+1]);.    nFre
12aac 65 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d 20  e = data[hdr+7] 
12aad 2b 20 74 6f 70 20 2d 20 28 63 65 6c 6c 4f 66 66  + top - (cellOff
12aae 73 65 74 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e  set + 2*pPage->n
12aaf 43 65 6c 6c 29 3b 0a 20 20 20 20 77 68 69 6c 65  Cell);.    while
12ab0 28 20 70 63 3e 30 20 29 7b 0a 20 20 20 20 20 20  ( pc>0 ){.      
12ab1 69 6e 74 20 6e 65 78 74 2c 20 73 69 7a 65 3b 0a  int next, size;.
12ab2 20 20 20 20 20 20 69 66 28 20 70 63 3e 75 73 61        if( pc>usa
12ab3 62 6c 65 53 69 7a 65 2d 34 20 29 7b 0a 20 20 20  bleSize-4 ){.   
12ab4 20 20 20 20 20 2f 2a 20 46 72 65 65 20 62 6c 6f       /* Free blo
12ab5 63 6b 20 69 73 20 6f 66 66 20 74 68 65 20 70 61  ck is off the pa
12ab6 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65  ge */.        re
12ab7 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
12ab8 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20  UPT_BKPT; .     
12ab9 20 7d 0a 20 20 20 20 20 20 6e 65 78 74 20 3d 20   }.      next = 
12aba 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  get2byte(&data[p
12abb 63 5d 29 3b 0a 20 20 20 20 20 20 73 69 7a 65 20  c]);.      size 
12abc 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
12abd 5b 70 63 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69  [pc+2]);.      i
12abe 66 28 20 6e 65 78 74 3e 30 20 26 26 20 6e 65 78  f( next>0 && nex
12abf 74 3c 3d 70 63 2b 73 69 7a 65 2b 33 20 29 7b 0a  t<=pc+size+3 ){.
12ac0 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20          /* Free 
12ac1 62 6c 6f 63 6b 73 20 6d 75 73 74 20 62 65 20 69  blocks must be i
12ac2 6e 20 61 63 63 65 6e 64 69 6e 67 20 6f 72 64 65  n accending orde
12ac3 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74  r */.        ret
12ac4 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
12ac5 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20  PT_BKPT; .      
12ac6 7d 0a 20 20 20 20 20 20 6e 46 72 65 65 20 2b 3d  }.      nFree +=
12ac7 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 70 63 20   size;.      pc 
12ac8 3d 20 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20  = next;.    }.  
12ac9 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d    pPage->nFree =
12aca 20 6e 46 72 65 65 3b 0a 20 20 20 20 69 66 28 20   nFree;.    if( 
12acb 6e 46 72 65 65 3e 3d 75 73 61 62 6c 65 53 69 7a  nFree>=usableSiz
12acc 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 46 72  e ){.      /* Fr
12acd 65 65 20 73 70 61 63 65 20 63 61 6e 6e 6f 74 20  ee space cannot 
12ace 65 78 63 65 65 64 20 74 6f 74 61 6c 20 70 61 67  exceed total pag
12acf 65 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 20 20  e size */.      
12ad0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
12ad1 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20  RRUPT_BKPT; .   
12ad2 20 7d 0a 0a 23 69 66 20 30 0a 20 20 2f 2a 20 43   }..#if 0.  /* C
12ad3 68 65 63 6b 20 74 68 61 74 20 61 6c 6c 20 74 68  heck that all th
12ad4 65 20 6f 66 66 73 65 74 73 20 69 6e 20 74 68 65  e offsets in the
12ad5 20 63 65 6c 6c 20 6f 66 66 73 65 74 20 61 72 72   cell offset arr
12ad6 61 79 20 61 72 65 20 77 69 74 68 69 6e 20 72 61  ay are within ra
12ad7 6e 67 65 2e 20 0a 20 20 2a 2a 20 0a 20 20 2a 2a  nge. .  ** .  **
12ad8 20 4f 6d 69 74 74 69 6e 67 20 74 68 69 73 20 63   Omitting this c
12ad9 6f 6e 73 69 73 74 65 6e 63 79 20 63 68 65 63 6b  onsistency check
12ada 20 61 6e 64 20 75 73 69 6e 67 20 74 68 65 20 70   and using the p
12adb 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 6d  Page->maskPage m
12adc 61 73 6b 0a 20 20 2a 2a 20 74 6f 20 70 72 65 76  ask.  ** to prev
12add 65 6e 74 20 6f 76 65 72 72 75 6e 6e 69 6e 67 20  ent overrunning 
12ade 74 68 65 20 70 61 67 65 20 62 75 66 66 65 72 20  the page buffer 
12adf 69 6e 20 66 69 6e 64 43 65 6c 6c 28 29 20 72 65  in findCell() re
12ae0 73 75 6c 74 73 20 69 6e 20 61 0a 20 20 2a 2a 20  sults in a.  ** 
12ae1 32 2e 35 25 20 70 65 72 66 6f 72 6d 61 6e 63 65  2.5% performance
12ae2 20 67 61 69 6e 2e 0a 20 20 2a 2f 0a 20 20 7b 0a   gain..  */.  {.
12ae3 20 20 20 20 75 38 20 2a 70 4f 66 66 3b 20 20 20      u8 *pOff;   
12ae4 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72       /* Iterator
12ae5 20 75 73 65 64 20 74 6f 20 63 68 65 63 6b 20 61   used to check a
12ae6 6c 6c 20 63 65 6c 6c 20 6f 66 66 73 65 74 73 20  ll cell offsets 
12ae7 61 72 65 20 69 6e 20 72 61 6e 67 65 20 2a 2f 0a  are in range */.
12ae8 20 20 20 20 75 38 20 2a 70 45 6e 64 3b 20 20 20      u8 *pEnd;   
12ae9 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
12aea 74 6f 20 65 6e 64 20 6f 66 20 63 65 6c 6c 20 6f  to end of cell o
12aeb 66 66 73 65 74 20 61 72 72 61 79 20 2a 2f 0a 20  ffset array */. 
12aec 20 20 20 75 38 20 6d 61 73 6b 3b 20 20 20 20 20     u8 mask;     
12aed 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 62      /* Mask of b
12aee 69 74 73 20 74 68 61 74 20 6d 75 73 74 20 62 65  its that must be
12aef 20 7a 65 72 6f 20 69 6e 20 4d 53 42 20 6f 66 20   zero in MSB of 
12af0 63 65 6c 6c 20 6f 66 66 73 65 74 73 20 2a 2f 0a  cell offsets */.
12af1 20 20 20 20 6d 61 73 6b 20 3d 20 7e 28 28 28 75      mask = ~(((u
12af2 38 29 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  8)(pBt->pageSize
12af3 3e 3e 38 29 29 2d 31 29 3b 0a 20 20 20 20 70 45  >>8))-1);.    pE
12af4 6e 64 20 3d 20 26 64 61 74 61 5b 63 65 6c 6c 4f  nd = &data[cellO
12af5 66 66 73 65 74 20 2b 20 70 50 61 67 65 2d 3e 6e  ffset + pPage->n
12af6 43 65 6c 6c 2a 32 5d 3b 0a 20 20 20 20 66 6f 72  Cell*2];.    for
12af7 28 70 4f 66 66 3d 26 64 61 74 61 5b 63 65 6c 6c  (pOff=&data[cell
12af8 4f 66 66 73 65 74 5d 3b 20 70 4f 66 66 21 3d 70  Offset]; pOff!=p
12af9 45 6e 64 20 26 26 20 21 28 28 2a 70 4f 66 66 29  End && !((*pOff)
12afa 26 6d 61 73 6b 29 3b 20 70 4f 66 66 2b 3d 32 29  &mask); pOff+=2)
12afb 3b 0a 20 20 20 20 69 66 28 20 70 4f 66 66 21 3d  ;.    if( pOff!=
12afc 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 72 65  pEnd ){.      re
12afd 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
12afe 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a  UPT_BKPT;.    }.
12aff 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
12b00 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20  pPage->isInit = 
12b01 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  1;.  }.  return 
12b02 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
12b03 0a 2a 2a 20 53 65 74 20 75 70 20 61 20 72 61 77  .** Set up a raw
12b04 20 70 61 67 65 20 73 6f 20 74 68 61 74 20 69 74   page so that it
12b05 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 64 61   looks like a da
12b06 74 61 62 61 73 65 20 70 61 67 65 20 68 6f 6c 64  tabase page hold
12b07 69 6e 67 0a 2a 2a 20 6e 6f 20 65 6e 74 72 69 65  ing.** no entrie
12b08 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
12b09 64 20 7a 65 72 6f 50 61 67 65 28 4d 65 6d 50 61  d zeroPage(MemPa
12b0a 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 66  ge *pPage, int f
12b0b 6c 61 67 73 29 7b 0a 20 20 75 6e 73 69 67 6e 65  lags){.  unsigne
12b0c 64 20 63 68 61 72 20 2a 64 61 74 61 20 3d 20 70  d char *data = p
12b0d 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 42  Page->aData;.  B
12b0e 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
12b0f 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 6e 74  Page->pBt;.  int
12b10 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64   hdr = pPage->hd
12b11 72 4f 66 66 73 65 74 3b 0a 20 20 69 6e 74 20 66  rOffset;.  int f
12b12 69 72 73 74 3b 0a 0a 20 20 61 73 73 65 72 74 28  irst;..  assert(
12b13 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
12b14 65 6e 75 6d 62 65 72 28 70 50 61 67 65 2d 3e 70  enumber(pPage->p
12b15 44 62 50 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e  DbPage)==pPage->
12b16 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 74  pgno );.  assert
12b17 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  ( sqlite3PagerGe
12b18 74 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44  tExtra(pPage->pD
12b19 62 50 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 2a  bPage) == (void*
12b1a 29 70 50 61 67 65 20 29 3b 0a 20 20 61 73 73 65  )pPage );.  asse
12b1b 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
12b1c 47 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70  GetData(pPage->p
12b1d 44 62 50 61 67 65 29 20 3d 3d 20 64 61 74 61 20  DbPage) == data 
12b1e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
12b1f 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65  ite3PagerIswrite
12b20 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50  able(pPage->pDbP
12b21 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74  age) );.  assert
12b22 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
12b23 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
12b24 20 29 3b 0a 20 20 2f 2a 6d 65 6d 73 65 74 28 26   );.  /*memset(&
12b25 64 61 74 61 5b 68 64 72 5d 2c 20 30 2c 20 70 42  data[hdr], 0, pB
12b26 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20  t->usableSize - 
12b27 68 64 72 29 3b 2a 2f 0a 20 20 64 61 74 61 5b 68  hdr);*/.  data[h
12b28 64 72 5d 20 3d 20 66 6c 61 67 73 3b 0a 20 20 66  dr] = flags;.  f
12b29 69 72 73 74 20 3d 20 68 64 72 20 2b 20 38 20 2b  irst = hdr + 8 +
12b2a 20 34 2a 28 28 66 6c 61 67 73 26 50 54 46 5f 4c   4*((flags&PTF_L
12b2b 45 41 46 29 3d 3d 30 29 3b 0a 20 20 6d 65 6d 73  EAF)==0);.  mems
12b2c 65 74 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 2c  et(&data[hdr+1],
12b2d 20 30 2c 20 34 29 3b 0a 20 20 64 61 74 61 5b 68   0, 4);.  data[h
12b2e 64 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 70 75 74  dr+7] = 0;.  put
12b2f 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
12b30 35 5d 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65 53  5], pBt->usableS
12b31 69 7a 65 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e  ize);.  pPage->n
12b32 46 72 65 65 20 3d 20 70 42 74 2d 3e 75 73 61 62  Free = pBt->usab
12b33 6c 65 53 69 7a 65 20 2d 20 66 69 72 73 74 3b 0a  leSize - first;.
12b34 20 20 64 65 63 6f 64 65 46 6c 61 67 73 28 70 50    decodeFlags(pP
12b35 61 67 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70  age, flags);.  p
12b36 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 20  Page->hdrOffset 
12b37 3d 20 68 64 72 3b 0a 20 20 70 50 61 67 65 2d 3e  = hdr;.  pPage->
12b38 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 66 69 72  cellOffset = fir
12b39 73 74 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76  st;.  pPage->nOv
12b3a 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 61 73  erflow = 0;.  as
12b3b 73 65 72 74 28 20 70 42 74 2d 3e 70 61 67 65 53  sert( pBt->pageS
12b3c 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 42 74 2d  ize>=512 && pBt-
12b3d 3e 70 61 67 65 53 69 7a 65 3c 3d 33 32 37 36 38  >pageSize<=32768
12b3e 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6d 61 73   );.  pPage->mas
12b3f 6b 50 61 67 65 20 3d 20 70 42 74 2d 3e 70 61 67  kPage = pBt->pag
12b40 65 53 69 7a 65 20 2d 20 31 3b 0a 20 20 70 50 61  eSize - 1;.  pPa
12b41 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20  ge->nCell = 0;. 
12b42 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d   pPage->isInit =
12b43 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f   1;.}.../*.** Co
12b44 6e 76 65 72 74 20 61 20 44 62 50 61 67 65 20 6f  nvert a DbPage o
12b45 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 74 68 65  btained from the
12b46 20 70 61 67 65 72 20 69 6e 74 6f 20 61 20 4d 65   pager into a Me
12b47 6d 50 61 67 65 20 75 73 65 64 20 62 79 0a 2a 2a  mPage used by.**
12b48 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65 72   the btree layer
12b49 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50  ..*/.static MemP
12b4a 61 67 65 20 2a 62 74 72 65 65 50 61 67 65 46 72  age *btreePageFr
12b4b 6f 6d 44 62 50 61 67 65 28 44 62 50 61 67 65 20  omDbPage(DbPage 
12b4c 2a 70 44 62 50 61 67 65 2c 20 50 67 6e 6f 20 70  *pDbPage, Pgno p
12b4d 67 6e 6f 2c 20 42 74 53 68 61 72 65 64 20 2a 70  gno, BtShared *p
12b4e 42 74 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  Bt){.  MemPage *
12b4f 70 50 61 67 65 20 3d 20 28 4d 65 6d 50 61 67 65  pPage = (MemPage
12b50 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  *)sqlite3PagerGe
12b51 74 45 78 74 72 61 28 70 44 62 50 61 67 65 29 3b  tExtra(pDbPage);
12b52 0a 20 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20  .  pPage->aData 
12b53 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
12b54 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a  tData(pDbPage);.
12b55 20 20 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65    pPage->pDbPage
12b56 20 3d 20 70 44 62 50 61 67 65 3b 0a 20 20 70 50   = pDbPage;.  pP
12b57 61 67 65 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a  age->pBt = pBt;.
12b58 20 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20    pPage->pgno = 
12b59 70 67 6e 6f 3b 0a 20 20 70 50 61 67 65 2d 3e 68  pgno;.  pPage->h
12b5a 64 72 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65  drOffset = pPage
12b5b 2d 3e 70 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 20  ->pgno==1 ? 100 
12b5c 3a 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 50  : 0;.  return pP
12b5d 61 67 65 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  age; .}../*.** G
12b5e 65 74 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74  et a page from t
12b5f 68 65 20 70 61 67 65 72 2e 20 20 49 6e 69 74 69  he pager.  Initi
12b60 61 6c 69 7a 65 20 74 68 65 20 4d 65 6d 50 61 67  alize the MemPag
12b61 65 2e 70 42 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d  e.pBt and.** Mem
12b62 50 61 67 65 2e 61 44 61 74 61 20 65 6c 65 6d 65  Page.aData eleme
12b63 6e 74 73 20 69 66 20 6e 65 65 64 65 64 2e 0a 2a  nts if needed..*
12b64 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 6f 43 6f  *.** If the noCo
12b65 6e 74 65 6e 74 20 66 6c 61 67 20 69 73 20 73 65  ntent flag is se
12b66 74 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  t, it means that
12b67 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20   we do not care 
12b68 61 62 6f 75 74 0a 2a 2a 20 74 68 65 20 63 6f 6e  about.** the con
12b69 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67 65  tent of the page
12b6a 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 20 20   at this time.  
12b6b 53 6f 20 64 6f 20 6e 6f 74 20 67 6f 20 74 6f 20  So do not go to 
12b6c 74 68 65 20 64 69 73 6b 0a 2a 2a 20 74 6f 20 66  the disk.** to f
12b6d 65 74 63 68 20 74 68 65 20 63 6f 6e 74 65 6e 74  etch the content
12b6e 2e 20 20 4a 75 73 74 20 66 69 6c 6c 20 69 6e 20  .  Just fill in 
12b6f 74 68 65 20 63 6f 6e 74 65 6e 74 20 77 69 74 68  the content with
12b70 20 7a 65 72 6f 73 20 66 6f 72 20 6e 6f 77 2e 0a   zeros for now..
12b71 2a 2a 20 49 66 20 69 6e 20 74 68 65 20 66 75 74  ** If in the fut
12b72 75 72 65 20 77 65 20 63 61 6c 6c 20 73 71 6c 69  ure we call sqli
12b73 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20  te3PagerWrite() 
12b74 6f 6e 20 74 68 69 73 20 70 61 67 65 2c 20 74 68  on this page, th
12b75 61 74 0a 2a 2a 20 6d 65 61 6e 73 20 77 65 20 68  at.** means we h
12b76 61 76 65 20 73 74 61 72 74 65 64 20 74 6f 20 62  ave started to b
12b77 65 20 63 6f 6e 63 65 72 6e 65 64 20 61 62 6f 75  e concerned abou
12b78 74 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 74 68  t content and th
12b79 65 20 64 69 73 6b 0a 2a 2a 20 72 65 61 64 20 73  e disk.** read s
12b7a 68 6f 75 6c 64 20 6f 63 63 75 72 20 61 74 20 74  hould occur at t
12b7b 68 61 74 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 53 51  hat point..*/.SQ
12b7c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
12b7d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
12b7e 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64  Page(.  BtShared
12b7f 20 2a 70 42 74 2c 20 20 20 20 20 20 20 2f 2a 20   *pBt,       /* 
12b80 54 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 50  The btree */.  P
12b81 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20  gno pgno,       
12b82 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
12b83 20 74 68 65 20 70 61 67 65 20 74 6f 20 66 65 74   the page to fet
12b84 63 68 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  ch */.  MemPage 
12b85 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 2f 2a 20  **ppPage,    /* 
12b86 52 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 20  Return the page 
12b87 69 6e 20 74 68 69 73 20 70 61 72 61 6d 65 74 65  in this paramete
12b88 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e  r */.  int noCon
12b89 74 65 6e 74 20 20 20 20 20 20 20 20 2f 2a 20 44  tent        /* D
12b8a 6f 20 6e 6f 74 20 6c 6f 61 64 20 70 61 67 65 20  o not load page 
12b8b 63 6f 6e 74 65 6e 74 20 69 66 20 74 72 75 65 20  content if true 
12b8c 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
12b8d 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67    DbPage *pDbPag
12b8e 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  e;..  assert( sq
12b8f 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
12b90 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
12b91 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
12b92 67 65 72 41 63 71 75 69 72 65 28 70 42 74 2d 3e  gerAcquire(pBt->
12b93 70 50 61 67 65 72 2c 20 70 67 6e 6f 2c 20 28 44  pPager, pgno, (D
12b94 62 50 61 67 65 2a 2a 29 26 70 44 62 50 61 67 65  bPage**)&pDbPage
12b95 2c 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 20  , noContent);.  
12b96 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
12b97 72 63 3b 0a 20 20 2a 70 70 50 61 67 65 20 3d 20  rc;.  *ppPage = 
12b98 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50  btreePageFromDbP
12b99 61 67 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e  age(pDbPage, pgn
12b9a 6f 2c 20 70 42 74 29 3b 0a 20 20 72 65 74 75 72  o, pBt);.  retur
12b9b 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
12b9c 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
12b9d 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74   size of the dat
12b9e 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 70 61  abase file in pa
12b9f 67 65 73 2e 20 20 4f 72 20 72 65 74 75 72 6e 20  ges.  Or return 
12ba0 2d 31 20 69 66 0a 2a 2a 20 74 68 65 72 65 20 69  -1 if.** there i
12ba1 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 65 72  s any kind of er
12ba2 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ror..*/.static i
12ba3 6e 74 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e  nt pagerPagecoun
12ba4 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  t(Pager *pPager)
12ba5 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e  {.  int rc;.  in
12ba6 74 20 6e 50 61 67 65 3b 0a 20 20 72 63 20 3d 20  t nPage;.  rc = 
12ba7 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
12ba8 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 26 6e  count(pPager, &n
12ba9 50 61 67 65 29 3b 0a 20 20 72 65 74 75 72 6e 20  Page);.  return 
12baa 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3f 6e  (rc==SQLITE_OK?n
12bab 50 61 67 65 3a 2d 31 29 3b 0a 7d 0a 0a 2f 2a 0a  Page:-1);.}../*.
12bac 2a 2a 20 47 65 74 20 61 20 70 61 67 65 20 66 72  ** Get a page fr
12bad 6f 6d 20 74 68 65 20 70 61 67 65 72 20 61 6e 64  om the pager and
12bae 20 69 6e 69 74 69 61 6c 69 7a 65 20 69 74 2e 20   initialize it. 
12baf 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   This routine.**
12bb0 20 69 73 20 6a 75 73 74 20 61 20 63 6f 6e 76 65   is just a conve
12bb1 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72 20 61  nience wrapper a
12bb2 72 6f 75 6e 64 20 73 65 70 61 72 61 74 65 20 63  round separate c
12bb3 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74  alls to.** sqlit
12bb4 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 29  e3BtreeGetPage()
12bb5 20 61 6e 64 20 73 71 6c 69 74 65 33 42 74 72 65   and sqlite3Btre
12bb6 65 49 6e 69 74 50 61 67 65 28 29 2e 0a 2a 2f 0a  eInitPage()..*/.
12bb7 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 41 6e  static int getAn
12bb8 64 49 6e 69 74 50 61 67 65 28 0a 20 20 42 74 53  dInitPage(.  BtS
12bb9 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20  hared *pBt,     
12bba 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
12bbb 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50  base file */.  P
12bbc 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20  gno pgno,       
12bbd 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
12bbe 20 74 68 65 20 70 61 67 65 20 74 6f 20 67 65 74   the page to get
12bbf 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a   */.  MemPage **
12bc0 70 70 50 61 67 65 20 20 20 20 20 2f 2a 20 57 72  ppPage     /* Wr
12bc1 69 74 65 20 74 68 65 20 70 61 67 65 20 70 6f 69  ite the page poi
12bc2 6e 74 65 72 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  nter here */.){.
12bc3 20 20 69 6e 74 20 72 63 3b 0a 20 20 44 62 50 61    int rc;.  DbPa
12bc4 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 20 20 4d  ge *pDbPage;.  M
12bc5 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 0a  emPage *pPage;..
12bc6 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
12bc7 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
12bc8 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66  ->mutex) );.  if
12bc9 28 20 70 67 6e 6f 3d 3d 30 20 29 7b 0a 20 20 20  ( pgno==0 ){.   
12bca 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
12bcb 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20  ORRUPT_BKPT; .  
12bcc 7d 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20 6f 66  }..  /* It is of
12bcd 74 65 6e 20 74 68 65 20 63 61 73 65 20 74 68 61  ten the case tha
12bce 74 20 74 68 65 20 70 61 67 65 20 77 65 20 77 61  t the page we wa
12bcf 6e 74 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e  nt is already in
12bd0 20 63 61 63 68 65 2e 0a 20 20 2a 2a 20 49 66 20   cache..  ** If 
12bd1 73 6f 2c 20 67 65 74 20 69 74 20 64 69 72 65 63  so, get it direc
12bd2 74 6c 79 2e 20 20 54 68 69 73 20 73 61 76 65 73  tly.  This saves
12bd3 20 75 73 20 66 72 6f 6d 20 68 61 76 69 6e 67 20   us from having 
12bd4 74 6f 20 63 61 6c 6c 0a 20 20 2a 2a 20 70 61 67  to call.  ** pag
12bd5 65 72 50 61 67 65 63 6f 75 6e 74 28 29 20 74 6f  erPagecount() to
12bd6 20 6d 61 6b 65 20 73 75 72 65 20 70 67 6e 6f 20   make sure pgno 
12bd7 69 73 20 77 69 74 68 69 6e 20 6c 69 6d 69 74 73  is within limits
12bd8 2c 20 77 68 69 63 68 20 72 65 73 75 6c 74 73 0a  , which results.
12bd9 20 20 2a 2a 20 69 6e 20 61 20 6d 65 61 73 75 72    ** in a measur
12bda 65 61 62 6c 65 20 70 65 72 66 6f 72 6d 61 6e 63  eable performanc
12bdb 65 20 69 6d 70 72 6f 76 65 6d 65 6e 74 73 2e 0a  e improvements..
12bdc 20 20 2a 2f 0a 20 20 70 44 62 50 61 67 65 20 3d    */.  pDbPage =
12bdd 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f   sqlite3PagerLoo
12bde 6b 75 70 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  kup(pBt->pPager,
12bdf 20 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70 44   pgno);.  if( pD
12be0 62 50 61 67 65 20 29 7b 0a 20 20 20 20 2f 2a 20  bPage ){.    /* 
12be1 50 61 67 65 20 69 73 20 61 6c 72 65 61 64 79 20  Page is already 
12be2 69 6e 20 63 61 63 68 65 20 2a 2f 0a 20 20 20 20  in cache */.    
12be3 2a 70 70 50 61 67 65 20 3d 20 70 50 61 67 65 20  *ppPage = pPage 
12be4 3d 20 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44  = btreePageFromD
12be5 62 50 61 67 65 28 70 44 62 50 61 67 65 2c 20 70  bPage(pDbPage, p
12be6 67 6e 6f 2c 20 70 42 74 29 3b 0a 20 20 20 20 72  gno, pBt);.    r
12be7 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
12be8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 50   }else{.    /* P
12be9 61 67 65 20 6e 6f 74 20 69 6e 20 63 61 63 68 65  age not in cache
12bea 2e 20 20 41 63 71 75 69 72 65 20 69 74 2e 20 2a  .  Acquire it. *
12beb 2f 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3e 70  /.    if( pgno>p
12bec 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42  agerPagecount(pB
12bed 74 2d 3e 70 50 61 67 65 72 29 20 29 7b 0a 20 20  t->pPager) ){.  
12bee 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
12bef 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20  E_CORRUPT_BKPT; 
12bf0 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
12bf1 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
12bf2 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 2c 20 70  age(pBt, pgno, p
12bf3 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 69  pPage, 0);.    i
12bf4 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
12bf5 63 3b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 2a  c;.    pPage = *
12bf6 70 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 69 66  ppPage;.  }.  if
12bf7 28 20 21 70 50 61 67 65 2d 3e 69 73 49 6e 69 74  ( !pPage->isInit
12bf8 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
12bf9 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67  ite3BtreeInitPag
12bfa 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20  e(pPage);.  }.  
12bfb 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
12bfc 4b 20 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65  K ){.    release
12bfd 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
12bfe 20 2a 70 70 50 61 67 65 20 3d 20 30 3b 0a 20 20   *ppPage = 0;.  
12bff 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
12c00 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20  ../*.** Release 
12c01 61 20 4d 65 6d 50 61 67 65 2e 20 20 54 68 69 73  a MemPage.  This
12c02 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65   should be calle
12c03 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20  d once for each 
12c04 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f  prior.** call to
12c05 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
12c06 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  Page..*/.static 
12c07 76 6f 69 64 20 72 65 6c 65 61 73 65 50 61 67 65  void releasePage
12c08 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29  (MemPage *pPage)
12c09 7b 0a 20 20 69 66 28 20 70 50 61 67 65 20 29 7b  {.  if( pPage ){
12c0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
12c0b 67 65 2d 3e 61 44 61 74 61 20 29 3b 0a 20 20 20  ge->aData );.   
12c0c 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
12c0d 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72  pBt );.    asser
12c0e 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47  t( sqlite3PagerG
12c0f 65 74 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70  etExtra(pPage->p
12c10 44 62 50 61 67 65 29 20 3d 3d 20 28 76 6f 69 64  DbPage) == (void
12c11 2a 29 70 50 61 67 65 20 29 3b 0a 20 20 20 20 61  *)pPage );.    a
12c12 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
12c13 67 65 72 47 65 74 44 61 74 61 28 70 50 61 67 65  gerGetData(pPage
12c14 2d 3e 70 44 62 50 61 67 65 29 3d 3d 70 50 61 67  ->pDbPage)==pPag
12c15 65 2d 3e 61 44 61 74 61 20 29 3b 0a 20 20 20 20  e->aData );.    
12c16 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
12c17 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65  mutex_held(pPage
12c18 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
12c19 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65  .    sqlite3Page
12c1a 72 55 6e 72 65 66 28 70 50 61 67 65 2d 3e 70 44  rUnref(pPage->pD
12c1b 62 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  bPage);.  }.}../
12c1c 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 61 20 72 6f  *.** During a ro
12c1d 6c 6c 62 61 63 6b 2c 20 77 68 65 6e 20 74 68 65  llback, when the
12c1e 20 70 61 67 65 72 20 72 65 6c 6f 61 64 73 20 69   pager reloads i
12c1f 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20  nformation into 
12c20 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 73 6f 20  the cache.** so 
12c21 74 68 61 74 20 74 68 65 20 63 61 63 68 65 20 69  that the cache i
12c22 73 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69 74  s restored to it
12c23 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65  s original state
12c24 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66   at the start of
12c25 0a 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74  .** the transact
12c26 69 6f 6e 2c 20 66 6f 72 20 65 61 63 68 20 70 61  ion, for each pa
12c27 67 65 20 72 65 73 74 6f 72 65 64 20 74 68 69 73  ge restored this
12c28 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
12c29 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ed..**.** This r
12c2a 6f 75 74 69 6e 65 20 6e 65 65 64 73 20 74 6f 20  outine needs to 
12c2b 72 65 73 65 74 20 74 68 65 20 65 78 74 72 61 20  reset the extra 
12c2c 64 61 74 61 20 73 65 63 74 69 6f 6e 20 61 74 20  data section at 
12c2d 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a  the end of the.*
12c2e 2a 20 70 61 67 65 20 74 6f 20 61 67 72 65 65 20  * page to agree 
12c2f 77 69 74 68 20 74 68 65 20 72 65 73 74 6f 72 65  with the restore
12c30 64 20 64 61 74 61 2e 0a 2a 2f 0a 73 74 61 74 69  d data..*/.stati
12c31 63 20 76 6f 69 64 20 70 61 67 65 52 65 69 6e 69  c void pageReini
12c32 74 28 44 62 50 61 67 65 20 2a 70 44 61 74 61 29  t(DbPage *pData)
12c33 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  {.  MemPage *pPa
12c34 67 65 3b 0a 20 20 70 50 61 67 65 20 3d 20 28 4d  ge;.  pPage = (M
12c35 65 6d 50 61 67 65 20 2a 29 73 71 6c 69 74 65 33  emPage *)sqlite3
12c36 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70 44  PagerGetExtra(pD
12c37 61 74 61 29 3b 0a 20 20 69 66 28 20 70 50 61 67  ata);.  if( pPag
12c38 65 2d 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20  e->isInit ){.   
12c39 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
12c3a 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67  _mutex_held(pPag
12c3b 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  e->pBt->mutex) )
12c3c 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49  ;.    pPage->isI
12c3d 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 69 66 28  nit = 0;.    if(
12c3e 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
12c3f 65 52 65 66 63 6f 75 6e 74 28 70 44 61 74 61 29  eRefcount(pData)
12c40 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  >0 ){.      sqli
12c41 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65  te3BtreeInitPage
12c42 28 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20  (pPage);.    }. 
12c43 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f   }.}../*.** Invo
12c44 6b 65 20 74 68 65 20 62 75 73 79 20 68 61 6e 64  ke the busy hand
12c45 6c 65 72 20 66 6f 72 20 61 20 62 74 72 65 65 2e  ler for a btree.
12c46 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
12c47 71 6c 69 74 65 33 42 74 72 65 65 49 6e 76 6f 6b  qlite3BtreeInvok
12c48 65 42 75 73 79 48 61 6e 64 6c 65 72 28 76 6f 69  eBusyHandler(voi
12c49 64 20 2a 70 41 72 67 2c 20 69 6e 74 20 6e 29 7b  d *pArg, int n){
12c4a 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
12c4b 20 3d 20 28 42 74 53 68 61 72 65 64 2a 29 70 41   = (BtShared*)pA
12c4c 72 67 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42  rg;.  assert( pB
12c4d 74 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72  t->db );.  asser
12c4e 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
12c4f 5f 68 65 6c 64 28 70 42 74 2d 3e 64 62 2d 3e 6d  _held(pBt->db->m
12c50 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72  utex) );.  retur
12c51 6e 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42  n sqlite3InvokeB
12c52 75 73 79 48 61 6e 64 6c 65 72 28 26 70 42 74 2d  usyHandler(&pBt-
12c53 3e 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72  >db->busyHandler
12c54 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e  );.}../*.** Open
12c55 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65   a database file
12c56 2e 0a 2a 2a 20 0a 2a 2a 20 7a 46 69 6c 65 6e 61  ..** .** zFilena
12c57 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  me is the name o
12c58 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
12c59 69 6c 65 2e 20 20 49 66 20 7a 46 69 6c 65 6e 61  ile.  If zFilena
12c5a 6d 65 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 61 20  me is NULL.** a 
12c5b 6e 65 77 20 64 61 74 61 62 61 73 65 20 77 69 74  new database wit
12c5c 68 20 61 20 72 61 6e 64 6f 6d 20 6e 61 6d 65 20  h a random name 
12c5d 69 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 69  is created.  Thi
12c5e 73 20 72 61 6e 64 6f 6d 6c 79 20 6e 61 6d 65 64  s randomly named
12c5f 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c  .** database fil
12c60 65 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 74 65  e will be delete
12c61 64 20 77 68 65 6e 20 73 71 6c 69 74 65 33 42 74  d when sqlite3Bt
12c62 72 65 65 43 6c 6f 73 65 28 29 20 69 73 20 63 61  reeClose() is ca
12c63 6c 6c 65 64 2e 0a 2a 2a 20 49 66 20 7a 46 69 6c  lled..** If zFil
12c64 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72  ename is ":memor
12c65 79 3a 22 20 74 68 65 6e 20 61 6e 20 69 6e 2d 6d  y:" then an in-m
12c66 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 69  emory database i
12c67 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 74 68 61  s created.** tha
12c68 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c  t is automatical
12c69 6c 79 20 64 65 73 74 72 6f 79 65 64 20 77 68 65  ly destroyed whe
12c6a 6e 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a  n it is closed..
12c6b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
12c6c 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
12c6d 65 65 4f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20  eeOpen(.  const 
12c6e 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c  char *zFilename,
12c6f 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
12c70 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67   file containing
12c71 20 74 68 65 20 42 54 72 65 65 20 64 61 74 61 62   the BTree datab
12c72 61 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ase */.  sqlite3
12c73 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
12c74 20 2f 2a 20 41 73 73 6f 63 69 61 74 65 64 20 64   /* Associated d
12c75 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a  atabase handle *
12c76 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70 70 42 74  /.  Btree **ppBt
12c77 72 65 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  ree,        /* P
12c78 6f 69 6e 74 65 72 20 74 6f 20 6e 65 77 20 42 74  ointer to new Bt
12c79 72 65 65 20 6f 62 6a 65 63 74 20 77 72 69 74 74  ree object writt
12c7a 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  en here */.  int
12c7b 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
12c7c 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 73 20       /* Options 
12c7d 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67  */.  int vfsFlag
12c7e 73 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  s            /* 
12c7f 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72  Flags passed thr
12c80 6f 75 67 68 20 74 6f 20 73 71 6c 69 74 65 33 5f  ough to sqlite3_
12c81 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 29  vfs.xOpen() */.)
12c82 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  {.  sqlite3_vfs 
12c83 2a 70 56 66 73 3b 20 20 20 20 20 20 2f 2a 20 54  *pVfs;      /* T
12c84 68 65 20 56 46 53 20 74 6f 20 75 73 65 20 66 6f  he VFS to use fo
12c85 72 20 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a  r this btree */.
12c86 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
12c87 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 53 68 61  = 0;      /* Sha
12c88 72 65 64 20 70 61 72 74 20 6f 66 20 62 74 72 65  red part of btre
12c89 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  e structure */. 
12c8a 20 42 74 72 65 65 20 2a 70 3b 20 20 20 20 20 20   Btree *p;      
12c8b 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64           /* Hand
12c8c 6c 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a  le to return */.
12c8d 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
12c8e 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 52 65 73  E_OK;.  int nRes
12c8f 65 72 76 65 3b 0a 20 20 75 6e 73 69 67 6e 65 64  erve;.  unsigned
12c90 20 63 68 61 72 20 7a 44 62 48 65 61 64 65 72 5b   char zDbHeader[
12c91 31 30 30 5d 3b 0a 0a 20 20 2f 2a 20 53 65 74 20  100];..  /* Set 
12c92 74 68 65 20 76 61 72 69 61 62 6c 65 20 69 73 4d  the variable isM
12c93 65 6d 64 62 20 74 6f 20 74 72 75 65 20 66 6f 72  emdb to true for
12c94 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
12c95 74 61 62 61 73 65 2c 20 6f 72 20 0a 20 20 2a 2a  tabase, or .  **
12c96 20 66 61 6c 73 65 20 66 6f 72 20 61 20 66 69 6c   false for a fil
12c97 65 2d 62 61 73 65 64 20 64 61 74 61 62 61 73 65  e-based database
12c98 2e 20 54 68 69 73 20 73 79 6d 62 6f 6c 20 69 73  . This symbol is
12c99 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64 20 69   only required i
12c9a 66 0a 20 20 2a 2a 20 65 69 74 68 65 72 20 6f 66  f.  ** either of
12c9b 20 74 68 65 20 73 68 61 72 65 64 2d 64 61 74 61   the shared-data
12c9c 20 6f 72 20 61 75 74 6f 76 61 63 75 75 6d 20 66   or autovacuum f
12c9d 65 61 74 75 72 65 73 20 61 72 65 20 63 6f 6d 70  eatures are comp
12c9e 69 6c 65 64 20 0a 20 20 2a 2a 20 69 6e 74 6f 20  iled .  ** into 
12c9f 74 68 65 20 6c 69 62 72 61 72 79 2e 0a 20 20 2a  the library..  *
12ca0 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
12ca1 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
12ca2 44 5f 43 41 43 48 45 29 20 7c 7c 20 21 64 65 66  D_CACHE) || !def
12ca3 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
12ca4 5f 41 55 54 4f 56 41 43 55 55 4d 29 0a 20 20 23  _AUTOVACUUM).  #
12ca5 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
12ca6 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 20 20 63  T_MEMORYDB.    c
12ca7 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 62  onst int isMemdb
12ca8 20 3d 20 30 3b 0a 20 20 23 65 6c 73 65 0a 20 20   = 0;.  #else.  
12ca9 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65    const int isMe
12caa 6d 64 62 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 20  mdb = zFilename 
12cab 26 26 20 21 73 74 72 63 6d 70 28 7a 46 69 6c 65  && !strcmp(zFile
12cac 6e 61 6d 65 2c 20 22 3a 6d 65 6d 6f 72 79 3a 22  name, ":memory:"
12cad 29 3b 0a 20 20 23 65 6e 64 69 66 0a 23 65 6e 64  );.  #endif.#end
12cae 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62  if..  assert( db
12caf 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
12cb0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
12cb1 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
12cb2 3b 0a 0a 20 20 70 56 66 73 20 3d 20 64 62 2d 3e  ;..  pVfs = db->
12cb3 70 56 66 73 3b 0a 20 20 70 20 3d 20 73 71 6c 69  pVfs;.  p = sqli
12cb4 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69  te3MallocZero(si
12cb5 7a 65 6f 66 28 42 74 72 65 65 29 29 3b 0a 20 20  zeof(Btree));.  
12cb6 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65  if( !p ){.    re
12cb7 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
12cb8 4d 3b 0a 20 20 7d 0a 20 20 70 2d 3e 69 6e 54 72  M;.  }.  p->inTr
12cb9 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45  ans = TRANS_NONE
12cba 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a  ;.  p->db = db;.
12cbb 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
12cbc 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
12cbd 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66 69  _CACHE) && !defi
12cbe 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
12cbf 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20 20 2a  DISKIO).  /*.  *
12cc0 2a 20 49 66 20 74 68 69 73 20 42 74 72 65 65 20  * If this Btree 
12cc1 69 73 20 61 20 63 61 6e 64 69 64 61 74 65 20 66  is a candidate f
12cc2 6f 72 20 73 68 61 72 65 64 20 63 61 63 68 65 2c  or shared cache,
12cc3 20 74 72 79 20 74 6f 20 66 69 6e 64 20 61 6e 0a   try to find an.
12cc4 20 20 2a 2a 20 65 78 69 73 74 69 6e 67 20 42 74    ** existing Bt
12cc5 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 74 68  Shared object th
12cc6 61 74 20 77 65 20 63 61 6e 20 73 68 61 72 65 20  at we can share 
12cc7 77 69 74 68 0a 20 20 2a 2f 0a 20 20 69 66 28 20  with.  */.  if( 
12cc8 69 73 4d 65 6d 64 62 3d 3d 30 0a 20 20 20 26 26  isMemdb==0.   &&
12cc9 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51   (db->flags & SQ
12cca 4c 49 54 45 5f 56 74 61 62 29 3d 3d 30 0a 20 20  LITE_Vtab)==0.  
12ccb 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26   && zFilename &&
12ccc 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 0a 20 20   zFilename[0].  
12ccd 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
12cce 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73  e3GlobalConfig.s
12ccf 68 61 72 65 64 43 61 63 68 65 45 6e 61 62 6c 65  haredCacheEnable
12cd0 64 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e  d ){.      int n
12cd1 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d 20 70  FullPathname = p
12cd2 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b  Vfs->mxPathname+
12cd3 31 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  1;.      char *z
12cd4 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d 20 73  FullPathname = s
12cd5 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 46 75  qlite3Malloc(nFu
12cd6 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20  llPathname);.   
12cd7 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
12cd8 20 2a 6d 75 74 65 78 53 68 61 72 65 64 3b 0a 20   *mutexShared;. 
12cd9 20 20 20 20 20 70 2d 3e 73 68 61 72 61 62 6c 65       p->sharable
12cda 20 3d 20 31 3b 0a 20 20 20 20 20 20 64 62 2d 3e   = 1;.      db->
12cdb 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f  flags |= SQLITE_
12cdc 53 68 61 72 65 64 43 61 63 68 65 3b 0a 20 20 20  SharedCache;.   
12cdd 20 20 20 69 66 28 20 21 7a 46 75 6c 6c 50 61 74     if( !zFullPat
12cde 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20  hname ){.       
12cdf 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29   sqlite3_free(p)
12ce0 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
12ce1 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
12ce2 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
12ce3 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61  ite3OsFullPathna
12ce4 6d 65 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61  me(pVfs, zFilena
12ce5 6d 65 2c 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d  me, nFullPathnam
12ce6 65 2c 20 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65  e, zFullPathname
12ce7 29 3b 0a 20 20 20 20 20 20 6d 75 74 65 78 53 68  );.      mutexSh
12ce8 61 72 65 64 20 3d 20 73 71 6c 69 74 65 33 4d 75  ared = sqlite3Mu
12ce9 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
12cea 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
12ceb 54 45 52 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  TER);.      sqli
12cec 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
12ced 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20  mutexShared);.  
12cee 20 20 20 20 66 6f 72 28 70 42 74 3d 47 4c 4f 42      for(pBt=GLOB
12cef 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c  AL(BtShared*,sql
12cf0 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c  ite3SharedCacheL
12cf1 69 73 74 29 3b 20 70 42 74 3b 20 70 42 74 3d 70  ist); pBt; pBt=p
12cf2 42 74 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  Bt->pNext){.    
12cf3 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d      assert( pBt-
12cf4 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 20 20 20  >nRef>0 );.     
12cf5 20 20 20 69 66 28 20 30 3d 3d 73 74 72 63 6d 70     if( 0==strcmp
12cf6 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20  (zFullPathname, 
12cf7 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65  sqlite3PagerFile
12cf8 6e 61 6d 65 28 70 42 74 2d 3e 70 50 61 67 65 72  name(pBt->pPager
12cf9 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  )).             
12cfa 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 50 61      && sqlite3Pa
12cfb 67 65 72 56 66 73 28 70 42 74 2d 3e 70 50 61 67  gerVfs(pBt->pPag
12cfc 65 72 29 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20  er)==pVfs ){.   
12cfd 20 20 20 20 20 20 20 70 2d 3e 70 42 74 20 3d 20         p->pBt = 
12cfe 70 42 74 3b 0a 20 20 20 20 20 20 20 20 20 20 70  pBt;.          p
12cff 42 74 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20  Bt->nRef++;.    
12d00 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
12d01 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
12d02 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
12d03 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 68  ex_leave(mutexSh
12d04 61 72 65 64 29 3b 0a 20 20 20 20 20 20 73 71 6c  ared);.      sql
12d05 69 74 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c 50  ite3_free(zFullP
12d06 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a  athname);.    }.
12d07 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
12d08 42 55 47 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20  BUG.    else{.  
12d09 20 20 20 20 2f 2a 20 49 6e 20 64 65 62 75 67 20      /* In debug 
12d0a 6d 6f 64 65 2c 20 77 65 20 6d 61 72 6b 20 61 6c  mode, we mark al
12d0b 6c 20 70 65 72 73 69 73 74 65 6e 74 20 64 61 74  l persistent dat
12d0c 61 62 61 73 65 73 20 61 73 20 73 68 61 72 61 62  abases as sharab
12d0d 6c 65 0a 20 20 20 20 20 20 2a 2a 20 65 76 65 6e  le.      ** even
12d0e 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 6e   when they are n
12d0f 6f 74 2e 20 20 54 68 69 73 20 65 78 65 72 63 69  ot.  This exerci
12d10 73 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  ses the locking 
12d11 63 6f 64 65 20 61 6e 64 0a 20 20 20 20 20 20 2a  code and.      *
12d12 2a 20 67 69 76 65 73 20 6d 6f 72 65 20 6f 70 70  * gives more opp
12d13 6f 72 74 75 6e 69 74 79 20 66 6f 72 20 61 73 73  ortunity for ass
12d14 65 72 74 73 28 73 71 6c 69 74 65 33 5f 6d 75 74  erts(sqlite3_mut
12d15 65 78 5f 68 65 6c 64 28 29 29 0a 20 20 20 20 20  ex_held()).     
12d16 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 74   ** statements t
12d17 6f 20 66 69 6e 64 20 6c 6f 63 6b 69 6e 67 20 70  o find locking p
12d18 72 6f 62 6c 65 6d 73 2e 0a 20 20 20 20 20 20 2a  roblems..      *
12d19 2f 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 72 61  /.      p->shara
12d1a 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 23  ble = 1;.    }.#
12d1b 65 6e 64 69 66 0a 20 20 7d 0a 23 65 6e 64 69 66  endif.  }.#endif
12d1c 0a 20 20 69 66 28 20 70 42 74 3d 3d 30 20 29 7b  .  if( pBt==0 ){
12d1d 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54  .    /*.    ** T
12d1e 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73  he following ass
12d1f 65 72 74 73 20 6d 61 6b 65 20 73 75 72 65 20 74  erts make sure t
12d20 68 61 74 20 73 74 72 75 63 74 75 72 65 73 20 75  hat structures u
12d21 73 65 64 20 62 79 20 74 68 65 20 62 74 72 65 65  sed by the btree
12d22 20 61 72 65 0a 20 20 20 20 2a 2a 20 74 68 65 20   are.    ** the 
12d23 72 69 67 68 74 20 73 69 7a 65 2e 20 20 54 68 69  right size.  Thi
12d24 73 20 69 73 20 74 6f 20 67 75 61 72 64 20 61 67  s is to guard ag
12d25 61 69 6e 73 74 20 73 69 7a 65 20 63 68 61 6e 67  ainst size chang
12d26 65 73 20 74 68 61 74 20 72 65 73 75 6c 74 0a 20  es that result. 
12d27 20 20 20 2a 2a 20 77 68 65 6e 20 63 6f 6d 70 69     ** when compi
12d28 6c 69 6e 67 20 6f 6e 20 61 20 64 69 66 66 65 72  ling on a differ
12d29 65 6e 74 20 61 72 63 68 69 74 65 63 74 75 72 65  ent architecture
12d2a 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73  ..    */.    ass
12d2b 65 72 74 28 20 73 69 7a 65 6f 66 28 69 36 34 29  ert( sizeof(i64)
12d2c 3d 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 28 69 36  ==8 || sizeof(i6
12d2d 34 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73  4)==4 );.    ass
12d2e 65 72 74 28 20 73 69 7a 65 6f 66 28 75 36 34 29  ert( sizeof(u64)
12d2f 3d 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 28 75 36  ==8 || sizeof(u6
12d30 34 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73  4)==4 );.    ass
12d31 65 72 74 28 20 73 69 7a 65 6f 66 28 75 33 32 29  ert( sizeof(u32)
12d32 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==4 );.    asser
12d33 74 28 20 73 69 7a 65 6f 66 28 75 31 36 29 3d 3d  t( sizeof(u16)==
12d34 32 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  2 );.    assert(
12d35 20 73 69 7a 65 6f 66 28 50 67 6e 6f 29 3d 3d 34   sizeof(Pgno)==4
12d36 20 29 3b 0a 20 20 0a 20 20 20 20 70 42 74 20 3d   );.  .    pBt =
12d37 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65   sqlite3MallocZe
12d38 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 42 74 29  ro( sizeof(*pBt)
12d39 20 29 3b 0a 20 20 20 20 69 66 28 20 70 42 74 3d   );.    if( pBt=
12d3a 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  =0 ){.      rc =
12d3b 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
12d3c 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 5f       goto btree_
12d3d 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  open_out;.    }.
12d3e 20 20 20 20 70 42 74 2d 3e 62 75 73 79 48 64 72      pBt->busyHdr
12d3f 2e 78 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33  .xFunc = sqlite3
12d40 42 74 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48  BtreeInvokeBusyH
12d41 61 6e 64 6c 65 72 3b 0a 20 20 20 20 70 42 74 2d  andler;.    pBt-
12d42 3e 62 75 73 79 48 64 72 2e 70 41 72 67 20 3d 20  >busyHdr.pArg = 
12d43 70 42 74 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  pBt;.    rc = sq
12d44 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 70  lite3PagerOpen(p
12d45 56 66 73 2c 20 26 70 42 74 2d 3e 70 50 61 67 65  Vfs, &pBt->pPage
12d46 72 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20  r, zFilename,.  
12d47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12d48 20 20 20 20 20 20 20 20 45 58 54 52 41 5f 53 49          EXTRA_SI
12d49 5a 45 2c 20 66 6c 61 67 73 2c 20 76 66 73 46 6c  ZE, flags, vfsFl
12d4a 61 67 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ags);.    if( rc
12d4b 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
12d4c 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
12d4d 33 50 61 67 65 72 52 65 61 64 46 69 6c 65 68 65  3PagerReadFilehe
12d4e 61 64 65 72 28 70 42 74 2d 3e 70 50 61 67 65 72  ader(pBt->pPager
12d4f 2c 73 69 7a 65 6f 66 28 7a 44 62 48 65 61 64 65  ,sizeof(zDbHeade
12d50 72 29 2c 7a 44 62 48 65 61 64 65 72 29 3b 0a 20  r),zDbHeader);. 
12d51 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21     }.    if( rc!
12d52 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
12d53 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f      goto btree_o
12d54 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  pen_out;.    }. 
12d55 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53     sqlite3PagerS
12d56 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28 70 42  etBusyhandler(pB
12d57 74 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d  t->pPager, &pBt-
12d58 3e 62 75 73 79 48 64 72 29 3b 0a 20 20 20 20 70  >busyHdr);.    p
12d59 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 0a  ->pBt = pBt;.  .
12d5a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
12d5b 53 65 74 52 65 69 6e 69 74 65 72 28 70 42 74 2d  SetReiniter(pBt-
12d5c 3e 70 50 61 67 65 72 2c 20 70 61 67 65 52 65 69  >pPager, pageRei
12d5d 6e 69 74 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70  nit);.    pBt->p
12d5e 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20  Cursor = 0;.    
12d5f 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b  pBt->pPage1 = 0;
12d60 0a 20 20 20 20 70 42 74 2d 3e 72 65 61 64 4f 6e  .    pBt->readOn
12d61 6c 79 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  ly = sqlite3Page
12d62 72 49 73 72 65 61 64 6f 6e 6c 79 28 70 42 74 2d  rIsreadonly(pBt-
12d63 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 70 42  >pPager);.    pB
12d64 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 67 65  t->pageSize = ge
12d65 74 32 62 79 74 65 28 26 7a 44 62 48 65 61 64 65  t2byte(&zDbHeade
12d66 72 5b 31 36 5d 29 3b 0a 20 20 20 20 69 66 28 20  r[16]);.    if( 
12d67 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c 35 31  pBt->pageSize<51
12d68 32 20 7c 7c 20 70 42 74 2d 3e 70 61 67 65 53 69  2 || pBt->pageSi
12d69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41  ze>SQLITE_MAX_PA
12d6a 47 45 5f 53 49 5a 45 0a 20 20 20 20 20 20 20 20  GE_SIZE.        
12d6b 20 7c 7c 20 28 28 70 42 74 2d 3e 70 61 67 65 53   || ((pBt->pageS
12d6c 69 7a 65 2d 31 29 26 70 42 74 2d 3e 70 61 67 65  ize-1)&pBt->page
12d6d 53 69 7a 65 29 21 3d 30 20 29 7b 0a 20 20 20 20  Size)!=0 ){.    
12d6e 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20    pBt->pageSize 
12d6f 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  = 0;.      sqlit
12d70 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69  e3PagerSetPagesi
12d71 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  ze(pBt->pPager, 
12d72 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b  &pBt->pageSize);
12d73 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
12d74 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
12d75 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
12d76 6d 61 67 69 63 20 6e 61 6d 65 20 22 3a 6d 65 6d  magic name ":mem
12d77 6f 72 79 3a 22 20 77 69 6c 6c 20 63 72 65 61 74  ory:" will creat
12d78 65 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64  e an in-memory d
12d79 61 74 61 62 61 73 65 2c 20 74 68 65 6e 0a 20 20  atabase, then.  
12d7a 20 20 20 20 2a 2a 20 6c 65 61 76 65 20 74 68 65      ** leave the
12d7b 20 61 75 74 6f 56 61 63 75 75 6d 20 6d 6f 64 65   autoVacuum mode
12d7c 20 61 74 20 30 20 28 64 6f 20 6e 6f 74 20 61 75   at 0 (do not au
12d7d 74 6f 2d 76 61 63 75 75 6d 29 2c 20 65 76 65 6e  to-vacuum), even
12d7e 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 53 51 4c   if.      ** SQL
12d7f 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f  ITE_DEFAULT_AUTO
12d80 56 41 43 55 55 4d 20 69 73 20 74 72 75 65 2e 20  VACUUM is true. 
12d81 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e  On the other han
12d82 64 2c 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 53  d, if.      ** S
12d83 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52  QLITE_OMIT_MEMOR
12d84 59 44 42 20 68 61 73 20 62 65 65 6e 20 64 65 66  YDB has been def
12d85 69 6e 65 64 2c 20 74 68 65 6e 20 22 3a 6d 65 6d  ined, then ":mem
12d86 6f 72 79 3a 22 20 69 73 20 6a 75 73 74 20 61 0a  ory:" is just a.
12d87 20 20 20 20 20 20 2a 2a 20 72 65 67 75 6c 61 72        ** regular
12d88 20 66 69 6c 65 2d 6e 61 6d 65 2e 20 49 6e 20 74   file-name. In t
12d89 68 69 73 20 63 61 73 65 20 74 68 65 20 61 75 74  his case the aut
12d8a 6f 2d 76 61 63 75 75 6d 20 61 70 70 6c 69 65 73  o-vacuum applies
12d8b 20 61 73 20 70 65 72 20 6e 6f 72 6d 61 6c 2e 0a   as per normal..
12d8c 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
12d8d 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20  f( zFilename && 
12d8e 21 69 73 4d 65 6d 64 62 20 29 7b 0a 20 20 20 20  !isMemdb ){.    
12d8f 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63      pBt->autoVac
12d90 75 75 6d 20 3d 20 28 53 51 4c 49 54 45 5f 44 45  uum = (SQLITE_DE
12d91 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d  FAULT_AUTOVACUUM
12d92 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 20   ? 1 : 0);.     
12d93 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75     pBt->incrVacu
12d94 75 6d 20 3d 20 28 53 51 4c 49 54 45 5f 44 45 46  um = (SQLITE_DEF
12d95 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 3d  AULT_AUTOVACUUM=
12d96 3d 32 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20  =2 ? 1 : 0);.   
12d97 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
12d98 20 20 6e 52 65 73 65 72 76 65 20 3d 20 30 3b 0a    nReserve = 0;.
12d99 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
12d9a 20 6e 52 65 73 65 72 76 65 20 3d 20 7a 44 62 48   nReserve = zDbH
12d9b 65 61 64 65 72 5b 32 30 5d 3b 0a 20 20 20 20 20  eader[20];.     
12d9c 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69   pBt->pageSizeFi
12d9d 78 65 64 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66  xed = 1;.#ifndef
12d9e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
12d9f 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 70 42  OVACUUM.      pB
12da0 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20  t->autoVacuum = 
12da1 28 67 65 74 34 62 79 74 65 28 26 7a 44 62 48 65  (get4byte(&zDbHe
12da2 61 64 65 72 5b 33 36 20 2b 20 34 2a 34 5d 29 3f  ader[36 + 4*4])?
12da3 31 3a 30 29 3b 0a 20 20 20 20 20 20 70 42 74 2d  1:0);.      pBt-
12da4 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 67  >incrVacuum = (g
12da5 65 74 34 62 79 74 65 28 26 7a 44 62 48 65 61 64  et4byte(&zDbHead
12da6 65 72 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a  er[36 + 7*4])?1:
12da7 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d  0);.#endif.    }
12da8 0a 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65  .    pBt->usable
12da9 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61 67 65  Size = pBt->page
12daa 53 69 7a 65 20 2d 20 6e 52 65 73 65 72 76 65 3b  Size - nReserve;
12dab 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 42  .    assert( (pB
12dac 74 2d 3e 70 61 67 65 53 69 7a 65 20 26 20 37 29  t->pageSize & 7)
12dad 3d 3d 30 20 29 3b 20 20 2f 2a 20 38 2d 62 79 74  ==0 );  /* 8-byt
12dae 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 70  e alignment of p
12daf 61 67 65 53 69 7a 65 20 2a 2f 0a 20 20 20 20 73  ageSize */.    s
12db0 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61  qlite3PagerSetPa
12db1 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67  gesize(pBt->pPag
12db2 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 69  er, &pBt->pageSi
12db3 7a 65 29 3b 0a 20 20 20 0a 23 69 66 20 21 64 65  ze);.   .#if !de
12db4 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
12db5 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29 20  T_SHARED_CACHE) 
12db6 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
12db7 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a  TE_OMIT_DISKIO).
12db8 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 6e      /* Add the n
12db9 65 77 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65  ew BtShared obje
12dba 63 74 20 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64  ct to the linked
12dbb 20 6c 69 73 74 20 73 68 61 72 61 62 6c 65 20 42   list sharable B
12dbc 74 53 68 61 72 65 64 73 2e 0a 20 20 20 20 2a 2f  tShareds..    */
12dbd 0a 20 20 20 20 69 66 28 20 70 2d 3e 73 68 61 72  .    if( p->shar
12dbe 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 73 71  able ){.      sq
12dbf 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
12dc0 65 78 53 68 61 72 65 64 3b 0a 20 20 20 20 20 20  exShared;.      
12dc1 70 42 74 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20  pBt->nRef = 1;. 
12dc2 20 20 20 20 20 6d 75 74 65 78 53 68 61 72 65 64       mutexShared
12dc3 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
12dc4 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
12dc5 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
12dc6 3b 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49  ;.      if( SQLI
12dc7 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26  TE_THREADSAFE &&
12dc8 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
12dc9 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20  nfig.bCoreMutex 
12dca 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e  ){.        pBt->
12dcb 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d  mutex = sqlite3M
12dcc 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
12dcd 5f 4d 55 54 45 58 5f 46 41 53 54 29 3b 0a 20 20  _MUTEX_FAST);.  
12dce 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 6d        if( pBt->m
12dcf 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20  utex==0 ){.     
12dd0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
12dd1 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20  _NOMEM;.        
12dd2 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
12dd3 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ed = 0;.        
12dd4 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65    goto btree_ope
12dd5 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d  n_out;.        }
12dd6 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
12dd7 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
12dd8 65 72 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b  er(mutexShared);
12dd9 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 4e 65 78  .      pBt->pNex
12dda 74 20 3d 20 47 4c 4f 42 41 4c 28 42 74 53 68 61  t = GLOBAL(BtSha
12ddb 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72  red*,sqlite3Shar
12ddc 65 64 43 61 63 68 65 4c 69 73 74 29 3b 0a 20 20  edCacheList);.  
12ddd 20 20 20 20 47 4c 4f 42 41 4c 28 42 74 53 68 61      GLOBAL(BtSha
12dde 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72  red*,sqlite3Shar
12ddf 65 64 43 61 63 68 65 4c 69 73 74 29 20 3d 20 70  edCacheList) = p
12de0 42 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  Bt;.      sqlite
12de1 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75  3_mutex_leave(mu
12de2 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20  texShared);.    
12de3 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 23 69  }.#endif.  }..#i
12de4 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
12de5 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
12de6 43 48 45 29 20 26 26 20 21 64 65 66 69 6e 65 64  CHE) && !defined
12de7 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53  (SQLITE_OMIT_DIS
12de8 4b 49 4f 29 0a 20 20 2f 2a 20 49 66 20 74 68 65  KIO).  /* If the
12de9 20 6e 65 77 20 42 74 72 65 65 20 75 73 65 73 20   new Btree uses 
12dea 61 20 73 68 61 72 61 62 6c 65 20 70 42 74 53 68  a sharable pBtSh
12deb 61 72 65 64 2c 20 74 68 65 6e 20 6c 69 6e 6b 20  ared, then link 
12dec 74 68 65 20 6e 65 77 0a 20 20 2a 2a 20 42 74 72  the new.  ** Btr
12ded 65 65 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74  ee into the list
12dee 20 6f 66 20 61 6c 6c 20 73 68 61 72 61 62 6c 65   of all sharable
12def 20 42 74 72 65 65 73 20 66 6f 72 20 74 68 65 20   Btrees for the 
12df0 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  same connection.
12df1 0a 20 20 2a 2a 20 54 68 65 20 6c 69 73 74 20 69  .  ** The list i
12df2 73 20 6b 65 70 74 20 69 6e 20 61 73 63 65 6e 64  s kept in ascend
12df3 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70 42 74  ing order by pBt
12df4 20 61 64 64 72 65 73 73 2e 0a 20 20 2a 2f 0a 20   address..  */. 
12df5 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65   if( p->sharable
12df6 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
12df7 20 20 20 42 74 72 65 65 20 2a 70 53 69 62 3b 0a     Btree *pSib;.
12df8 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64      for(i=0; i<d
12df9 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20  b->nDb; i++){.  
12dfa 20 20 20 20 69 66 28 20 28 70 53 69 62 20 3d 20      if( (pSib = 
12dfb 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 21  db->aDb[i].pBt)!
12dfc 3d 30 20 26 26 20 70 53 69 62 2d 3e 73 68 61 72  =0 && pSib->shar
12dfd 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20  able ){.        
12dfe 77 68 69 6c 65 28 20 70 53 69 62 2d 3e 70 50 72  while( pSib->pPr
12dff 65 76 20 29 7b 20 70 53 69 62 20 3d 20 70 53 69  ev ){ pSib = pSi
12e00 62 2d 3e 70 50 72 65 76 3b 20 7d 0a 20 20 20 20  b->pPrev; }.    
12e01 20 20 20 20 69 66 28 20 70 2d 3e 70 42 74 3c 70      if( p->pBt<p
12e02 53 69 62 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20  Sib->pBt ){.    
12e03 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d        p->pNext =
12e04 20 70 53 69 62 3b 0a 20 20 20 20 20 20 20 20 20   pSib;.         
12e05 20 70 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20   p->pPrev = 0;. 
12e06 20 20 20 20 20 20 20 20 20 70 53 69 62 2d 3e 70           pSib->p
12e07 50 72 65 76 20 3d 20 70 3b 0a 20 20 20 20 20 20  Prev = p;.      
12e08 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
12e09 20 20 20 77 68 69 6c 65 28 20 70 53 69 62 2d 3e     while( pSib->
12e0a 70 4e 65 78 74 20 26 26 20 70 53 69 62 2d 3e 70  pNext && pSib->p
12e0b 4e 65 78 74 2d 3e 70 42 74 3c 70 2d 3e 70 42 74  Next->pBt<p->pBt
12e0c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
12e0d 70 53 69 62 20 3d 20 70 53 69 62 2d 3e 70 4e 65  pSib = pSib->pNe
12e0e 78 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  xt;.          }.
12e0f 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65            p->pNe
12e10 78 74 20 3d 20 70 53 69 62 2d 3e 70 4e 65 78 74  xt = pSib->pNext
12e11 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70  ;.          p->p
12e12 50 72 65 76 20 3d 20 70 53 69 62 3b 0a 20 20 20  Prev = pSib;.   
12e13 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4e         if( p->pN
12e14 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ext ){.         
12e15 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72     p->pNext->pPr
12e16 65 76 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20  ev = p;.        
12e17 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 53    }.          pS
12e18 69 62 2d 3e 70 4e 65 78 74 20 3d 20 70 3b 0a 20  ib->pNext = p;. 
12e19 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
12e1a 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
12e1b 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
12e1c 0a 20 20 2a 70 70 42 74 72 65 65 20 3d 20 70 3b  .  *ppBtree = p;
12e1d 0a 0a 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74  ..btree_open_out
12e1e 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  :.  if( rc!=SQLI
12e1f 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28  TE_OK ){.    if(
12e20 20 70 42 74 20 26 26 20 70 42 74 2d 3e 70 50 61   pBt && pBt->pPa
12e21 67 65 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ger ){.      sql
12e22 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70  ite3PagerClose(p
12e23 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20  Bt->pPager);.   
12e24 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66   }.    sqlite3_f
12e25 72 65 65 28 70 42 74 29 3b 0a 20 20 20 20 73 71  ree(pBt);.    sq
12e26 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20  lite3_free(p);. 
12e27 20 20 20 2a 70 70 42 74 72 65 65 20 3d 20 30 3b     *ppBtree = 0;
12e28 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
12e29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65  ;.}../*.** Decre
12e2a 6d 65 6e 74 20 74 68 65 20 42 74 53 68 61 72 65  ment the BtShare
12e2b 64 2e 6e 52 65 66 20 63 6f 75 6e 74 65 72 2e 20  d.nRef counter. 
12e2c 20 57 68 65 6e 20 69 74 20 72 65 61 63 68 65 73   When it reaches
12e2d 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 6d 6f 76 65   zero,.** remove
12e2e 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73 74   the BtShared st
12e2f 72 75 63 74 75 72 65 20 66 72 6f 6d 20 74 68 65  ructure from the
12e30 20 73 68 61 72 69 6e 67 20 6c 69 73 74 2e 20 20   sharing list.  
12e31 52 65 74 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69  Return.** true i
12e32 66 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 6e  f the BtShared.n
12e33 52 65 66 20 63 6f 75 6e 74 65 72 20 72 65 61 63  Ref counter reac
12e34 68 65 73 20 7a 65 72 6f 20 61 6e 64 20 72 65 74  hes zero and ret
12e35 75 72 6e 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20  urn.** false if 
12e36 69 74 20 69 73 20 73 74 69 6c 6c 20 70 6f 73 69  it is still posi
12e37 74 69 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tive..*/.static 
12e38 69 6e 74 20 72 65 6d 6f 76 65 46 72 6f 6d 53 68  int removeFromSh
12e39 61 72 69 6e 67 4c 69 73 74 28 42 74 53 68 61 72  aringList(BtShar
12e3a 65 64 20 2a 70 42 74 29 7b 0a 23 69 66 6e 64 65  ed *pBt){.#ifnde
12e3b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
12e3c 41 52 45 44 5f 43 41 43 48 45 0a 20 20 73 71 6c  ARED_CACHE.  sql
12e3d 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4d 61 73  ite3_mutex *pMas
12e3e 74 65 72 3b 0a 20 20 42 74 53 68 61 72 65 64 20  ter;.  BtShared 
12e3f 2a 70 4c 69 73 74 3b 0a 20 20 69 6e 74 20 72 65  *pList;.  int re
12e40 6d 6f 76 65 64 20 3d 20 30 3b 0a 0a 20 20 61 73  moved = 0;..  as
12e41 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
12e42 74 65 78 5f 6e 6f 74 68 65 6c 64 28 70 42 74 2d  tex_notheld(pBt-
12e43 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d 61  >mutex) );.  pMa
12e44 73 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d 75  ster = sqlite3Mu
12e45 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
12e46 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
12e47 54 45 52 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  TER);.  sqlite3_
12e48 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 4d 61 73  mutex_enter(pMas
12e49 74 65 72 29 3b 0a 20 20 70 42 74 2d 3e 6e 52 65  ter);.  pBt->nRe
12e4a 66 2d 2d 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e  f--;.  if( pBt->
12e4b 6e 52 65 66 3c 3d 30 20 29 7b 0a 20 20 20 20 69  nRef<=0 ){.    i
12e4c 66 28 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72  f( GLOBAL(BtShar
12e4d 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65  ed*,sqlite3Share
12e4e 64 43 61 63 68 65 4c 69 73 74 29 3d 3d 70 42 74  dCacheList)==pBt
12e4f 20 29 7b 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c   ){.      GLOBAL
12e50 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74  (BtShared*,sqlit
12e51 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73  e3SharedCacheLis
12e52 74 29 20 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b  t) = pBt->pNext;
12e53 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
12e54 20 20 70 4c 69 73 74 20 3d 20 47 4c 4f 42 41 4c    pList = GLOBAL
12e55 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74  (BtShared*,sqlit
12e56 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73  e3SharedCacheLis
12e57 74 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28  t);.      while(
12e58 20 41 4c 57 41 59 53 28 70 4c 69 73 74 29 20 26   ALWAYS(pList) &
12e59 26 20 70 4c 69 73 74 2d 3e 70 4e 65 78 74 21 3d  & pList->pNext!=
12e5a 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70  pBt ){.        p
12e5b 4c 69 73 74 3d 70 4c 69 73 74 2d 3e 70 4e 65 78  List=pList->pNex
12e5c 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  t;.      }.     
12e5d 20 69 66 28 20 41 4c 57 41 59 53 28 70 4c 69 73   if( ALWAYS(pLis
12e5e 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c  t) ){.        pL
12e5f 69 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74  ist->pNext = pBt
12e60 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d  ->pNext;.      }
12e61 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 53  .    }.    if( S
12e62 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
12e63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
12e64 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 70 42 74  3_mutex_free(pBt
12e65 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a  ->mutex);.    }.
12e66 20 20 20 20 72 65 6d 6f 76 65 64 20 3d 20 31 3b      removed = 1;
12e67 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
12e68 75 74 65 78 5f 6c 65 61 76 65 28 70 4d 61 73 74  utex_leave(pMast
12e69 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65  er);.  return re
12e6a 6d 6f 76 65 64 3b 0a 23 65 6c 73 65 0a 20 20 72  moved;.#else.  r
12e6b 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a  eturn 1;.#endif.
12e6c 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75  }../*.** Make su
12e6d 72 65 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63  re pBt->pTmpSpac
12e6e 65 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61  e points to an a
12e6f 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 0a 2a 2a  llocation of .**
12e70 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42   MX_CELL_SIZE(pB
12e71 74 29 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61  t) bytes..*/.sta
12e72 74 69 63 20 76 6f 69 64 20 61 6c 6c 6f 63 61 74  tic void allocat
12e73 65 54 65 6d 70 53 70 61 63 65 28 42 74 53 68 61  eTempSpace(BtSha
12e74 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 66 28  red *pBt){.  if(
12e75 20 21 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65   !pBt->pTmpSpace
12e76 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e 70 54 6d   ){.    pBt->pTm
12e77 70 53 70 61 63 65 20 3d 20 73 71 6c 69 74 65 33  pSpace = sqlite3
12e78 50 61 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d  PageMalloc( pBt-
12e79 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 7d  >pageSize );.  }
12e7a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74  .}../*.** Free t
12e7b 68 65 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63  he pBt->pTmpSpac
12e7c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a  e allocation.*/.
12e7d 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65  static void free
12e7e 54 65 6d 70 53 70 61 63 65 28 42 74 53 68 61 72  TempSpace(BtShar
12e7f 65 64 20 2a 70 42 74 29 7b 0a 20 20 73 71 6c 69  ed *pBt){.  sqli
12e80 74 65 33 50 61 67 65 46 72 65 65 28 20 70 42 74  te3PageFree( pBt
12e81 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20  ->pTmpSpace);.  
12e82 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d  pBt->pTmpSpace =
12e83 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f   0;.}../*.** Clo
12e84 73 65 20 61 6e 20 6f 70 65 6e 20 64 61 74 61 62  se an open datab
12e85 61 73 65 20 61 6e 64 20 69 6e 76 61 6c 69 64 61  ase and invalida
12e86 74 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 2e 0a  te all cursors..
12e87 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
12e88 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
12e89 65 65 43 6c 6f 73 65 28 42 74 72 65 65 20 2a 70  eeClose(Btree *p
12e8a 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  ){.  BtShared *p
12e8b 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42  Bt = p->pBt;.  B
12e8c 74 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 0a  tCursor *pCur;..
12e8d 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 63    /* Close all c
12e8e 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 20 76 69  ursors opened vi
12e8f 61 20 74 68 69 73 20 68 61 6e 64 6c 65 2e 20 20  a this handle.  
12e90 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  */.  assert( sql
12e91 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
12e92 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
12e93 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
12e94 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e  nter(p);.  pBt->
12e95 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 70 43  db = p->db;.  pC
12e96 75 72 20 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f  ur = pBt->pCurso
12e97 72 3b 0a 20 20 77 68 69 6c 65 28 20 70 43 75 72  r;.  while( pCur
12e98 20 29 7b 0a 20 20 20 20 42 74 43 75 72 73 6f 72   ){.    BtCursor
12e99 20 2a 70 54 6d 70 20 3d 20 70 43 75 72 3b 0a 20   *pTmp = pCur;. 
12e9a 20 20 20 70 43 75 72 20 3d 20 70 43 75 72 2d 3e     pCur = pCur->
12e9b 70 4e 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70  pNext;.    if( p
12e9c 54 6d 70 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29  Tmp->pBtree==p )
12e9d 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  {.      sqlite3B
12e9e 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28  treeCloseCursor(
12e9f 70 54 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  pTmp);.    }.  }
12ea0 0a 0a 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20  ..  /* Rollback 
12ea1 61 6e 79 20 61 63 74 69 76 65 20 74 72 61 6e 73  any active trans
12ea2 61 63 74 69 6f 6e 20 61 6e 64 20 66 72 65 65 20  action and free 
12ea3 74 68 65 20 68 61 6e 64 6c 65 20 73 74 72 75 63  the handle struc
12ea4 74 75 72 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63  ture..  ** The c
12ea5 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74  all to sqlite3Bt
12ea6 72 65 65 52 6f 6c 6c 62 61 63 6b 28 29 20 64 72  reeRollback() dr
12ea7 6f 70 73 20 61 6e 79 20 74 61 62 6c 65 2d 6c 6f  ops any table-lo
12ea8 63 6b 73 20 68 65 6c 64 20 62 79 0a 20 20 2a 2a  cks held by.  **
12ea9 20 74 68 69 73 20 68 61 6e 64 6c 65 2e 0a 20 20   this handle..  
12eaa 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  */.  sqlite3Btre
12eab 65 52 6f 6c 6c 62 61 63 6b 28 70 29 3b 0a 20 20  eRollback(p);.  
12eac 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
12ead 65 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  e(p);..  /* If t
12eae 68 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6f  here are still o
12eaf 74 68 65 72 20 6f 75 74 73 74 61 6e 64 69 6e 67  ther outstanding
12eb0 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74   references to t
12eb1 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 0a  he shared-btree.
12eb2 20 20 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20    ** structure, 
12eb3 72 65 74 75 72 6e 20 6e 6f 77 2e 20 54 68 65 20  return now. The 
12eb4 72 65 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 69  remainder of thi
12eb5 73 20 70 72 6f 63 65 64 75 72 65 20 63 6c 65 61  s procedure clea
12eb6 6e 73 20 0a 20 20 2a 2a 20 75 70 20 74 68 65 20  ns .  ** up the 
12eb7 73 68 61 72 65 64 2d 62 74 72 65 65 2e 0a 20 20  shared-btree..  
12eb8 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  */.  assert( p->
12eb9 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 26 26  wantToLock==0 &&
12eba 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b   p->locked==0 );
12ebb 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61  .  if( !p->shara
12ebc 62 6c 65 20 7c 7c 20 72 65 6d 6f 76 65 46 72 6f  ble || removeFro
12ebd 6d 53 68 61 72 69 6e 67 4c 69 73 74 28 70 42 74  mSharingList(pBt
12ebe 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  ) ){.    /* The 
12ebf 70 42 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72  pBt is no longer
12ec0 20 6f 6e 20 74 68 65 20 73 68 61 72 69 6e 67 20   on the sharing 
12ec1 6c 69 73 74 2c 20 73 6f 20 77 65 20 63 61 6e 20  list, so we can 
12ec2 61 63 63 65 73 73 0a 20 20 20 20 2a 2a 20 69 74  access.    ** it
12ec3 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20   without having 
12ec4 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 75 74 65  to hold the mute
12ec5 78 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  x..    **.    **
12ec6 20 43 6c 65 61 6e 20 6f 75 74 20 61 6e 64 20 64   Clean out and d
12ec7 65 6c 65 74 65 20 74 68 65 20 42 74 53 68 61 72  elete the BtShar
12ec8 65 64 20 6f 62 6a 65 63 74 2e 0a 20 20 20 20 2a  ed object..    *
12ec9 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70  /.    assert( !p
12eca 42 74 2d 3e 70 43 75 72 73 6f 72 20 29 3b 0a 20  Bt->pCursor );. 
12ecb 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 43     sqlite3PagerC
12ecc 6c 6f 73 65 28 70 42 74 2d 3e 70 50 61 67 65 72  lose(pBt->pPager
12ecd 29 3b 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e  );.    if( pBt->
12ece 78 46 72 65 65 53 63 68 65 6d 61 20 26 26 20 70  xFreeSchema && p
12ecf 42 74 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20  Bt->pSchema ){. 
12ed0 20 20 20 20 20 70 42 74 2d 3e 78 46 72 65 65 53       pBt->xFreeS
12ed1 63 68 65 6d 61 28 70 42 74 2d 3e 70 53 63 68 65  chema(pBt->pSche
12ed2 6d 61 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ma);.    }.    s
12ed3 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74 2d  qlite3_free(pBt-
12ed4 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 66  >pSchema);.    f
12ed5 72 65 65 54 65 6d 70 53 70 61 63 65 28 70 42 74  reeTempSpace(pBt
12ed6 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
12ed7 72 65 65 28 70 42 74 29 3b 0a 20 20 7d 0a 0a 23  ree(pBt);.  }..#
12ed8 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
12ed9 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
12eda 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e    assert( p->wan
12edb 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 20 20  tToLock==0 );.  
12edc 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65  assert( p->locke
12edd 64 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d  d==0 );.  if( p-
12ede 3e 70 50 72 65 76 20 29 20 70 2d 3e 70 50 72 65  >pPrev ) p->pPre
12edf 76 2d 3e 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e  v->pNext = p->pN
12ee0 65 78 74 3b 0a 20 20 69 66 28 20 70 2d 3e 70 4e  ext;.  if( p->pN
12ee1 65 78 74 20 29 20 70 2d 3e 70 4e 65 78 74 2d 3e  ext ) p->pNext->
12ee2 70 50 72 65 76 20 3d 20 70 2d 3e 70 50 72 65 76  pPrev = p->pPrev
12ee3 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69  ;.#endif..  sqli
12ee4 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 72  te3_free(p);.  r
12ee5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
12ee6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  .}../*.** Change
12ee7 20 74 68 65 20 6c 69 6d 69 74 20 6f 6e 20 74 68   the limit on th
12ee8 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
12ee9 73 20 61 6c 6c 6f 77 65 64 20 69 6e 20 74 68 65  s allowed in the
12eea 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   cache..**.** Th
12eeb 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
12eec 20 6f 66 20 63 61 63 68 65 20 70 61 67 65 73 20   of cache pages 
12eed 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 61 62  is set to the ab
12eee 73 6f 6c 75 74 65 0a 2a 2a 20 76 61 6c 75 65 20  solute.** value 
12eef 6f 66 20 6d 78 50 61 67 65 2e 20 20 49 66 20 6d  of mxPage.  If m
12ef0 78 50 61 67 65 20 69 73 20 6e 65 67 61 74 69 76  xPage is negativ
12ef1 65 2c 20 74 68 65 20 70 61 67 65 72 20 77 69 6c  e, the pager wil
12ef2 6c 0a 2a 2a 20 6f 70 65 72 61 74 65 20 61 73 79  l.** operate asy
12ef3 6e 63 68 72 6f 6e 6f 75 73 6c 79 20 2d 20 69 74  nchronously - it
12ef4 20 77 69 6c 6c 20 6e 6f 74 20 73 74 6f 70 20 74   will not stop t
12ef5 6f 20 64 6f 20 66 73 79 6e 63 28 29 73 0a 2a 2a  o do fsync()s.**
12ef6 20 74 6f 20 69 6e 73 75 72 65 20 64 61 74 61 20   to insure data 
12ef7 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  is written to th
12ef8 65 20 64 69 73 6b 20 73 75 72 66 61 63 65 20 62  e disk surface b
12ef9 65 66 6f 72 65 0a 2a 2a 20 63 6f 6e 74 69 6e 75  efore.** continu
12efa 69 6e 67 2e 20 20 54 72 61 6e 73 61 63 74 69 6f  ing.  Transactio
12efb 6e 73 20 73 74 69 6c 6c 20 77 6f 72 6b 20 69 66  ns still work if
12efc 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20   synchronous is 
12efd 6f 66 66 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20  off,.** and the 
12efe 64 61 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20  database cannot 
12eff 62 65 20 63 6f 72 72 75 70 74 65 64 20 69 66 20  be corrupted if 
12f00 74 68 69 73 20 70 72 6f 67 72 61 6d 0a 2a 2a 20  this program.** 
12f01 63 72 61 73 68 65 73 2e 20 20 42 75 74 20 69 66  crashes.  But if
12f02 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73   the operating s
12f03 79 73 74 65 6d 20 63 72 61 73 68 65 73 20 6f 72  ystem crashes or
12f04 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 61 6e 20   there is.** an 
12f05 61 62 72 75 70 74 20 70 6f 77 65 72 20 66 61 69  abrupt power fai
12f06 6c 75 72 65 20 77 68 65 6e 20 73 79 6e 63 68 72  lure when synchr
12f07 6f 6e 6f 75 73 20 69 73 20 6f 66 66 2c 20 74 68  onous is off, th
12f08 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f  e database.** co
12f09 75 6c 64 20 62 65 20 6c 65 66 74 20 69 6e 20 61  uld be left in a
12f0a 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 61  n inconsistent a
12f0b 6e 64 20 75 6e 72 65 63 6f 76 65 72 61 62 6c 65  nd unrecoverable
12f0c 20 73 74 61 74 65 2e 0a 2a 2a 20 53 79 6e 63 68   state..** Synch
12f0d 72 6f 6e 6f 75 73 20 69 73 20 6f 6e 20 62 79 20  ronous is on by 
12f0e 64 65 66 61 75 6c 74 20 73 6f 20 64 61 74 61 62  default so datab
12f0f 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 69  ase corruption i
12f10 73 20 6e 6f 74 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c  s not.** normall
12f11 79 20 61 20 77 6f 72 72 79 2e 0a 2a 2f 0a 53 51  y a worry..*/.SQ
12f12 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
12f13 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
12f14 43 61 63 68 65 53 69 7a 65 28 42 74 72 65 65 20  CacheSize(Btree 
12f15 2a 70 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b  *p, int mxPage){
12f16 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
12f17 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73   = p->pBt;.  ass
12f18 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
12f19 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d  ex_held(p->db->m
12f1a 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74  utex) );.  sqlit
12f1b 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
12f1c 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  .  sqlite3PagerS
12f1d 65 74 43 61 63 68 65 73 69 7a 65 28 70 42 74 2d  etCachesize(pBt-
12f1e 3e 70 50 61 67 65 72 2c 20 6d 78 50 61 67 65 29  >pPager, mxPage)
12f1f 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
12f20 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
12f21 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
12f22 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
12f23 65 20 77 61 79 20 64 61 74 61 20 69 73 20 73 79  e way data is sy
12f24 6e 63 65 64 20 74 6f 20 64 69 73 6b 20 69 6e 20  nced to disk in 
12f25 6f 72 64 65 72 20 74 6f 20 69 6e 63 72 65 61 73  order to increas
12f26 65 20 6f 72 20 64 65 63 72 65 61 73 65 0a 2a 2a  e or decrease.**
12f27 20 68 6f 77 20 77 65 6c 6c 20 74 68 65 20 64 61   how well the da
12f28 74 61 62 61 73 65 20 72 65 73 69 73 74 73 20 64  tabase resists d
12f29 61 6d 61 67 65 20 64 75 65 20 74 6f 20 4f 53 20  amage due to OS 
12f2a 63 72 61 73 68 65 73 20 61 6e 64 20 70 6f 77 65  crashes and powe
12f2b 72 0a 2a 2a 20 66 61 69 6c 75 72 65 73 2e 20 20  r.** failures.  
12f2c 4c 65 76 65 6c 20 31 20 69 73 20 74 68 65 20 73  Level 1 is the s
12f2d 61 6d 65 20 61 73 20 61 73 79 6e 63 68 72 6f 6e  ame as asynchron
12f2e 6f 75 73 20 28 6e 6f 20 73 79 6e 63 73 28 29 20  ous (no syncs() 
12f2f 6f 63 63 75 72 20 61 6e 64 0a 2a 2a 20 74 68 65  occur and.** the
12f30 72 65 20 69 73 20 61 20 68 69 67 68 20 70 72 6f  re is a high pro
12f31 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61 6d 61  bability of dama
12f32 67 65 29 20 20 4c 65 76 65 6c 20 32 20 69 73 20  ge)  Level 2 is 
12f33 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20 54 68  the default.  Th
12f34 65 72 65 0a 2a 2a 20 69 73 20 61 20 76 65 72 79  ere.** is a very
12f35 20 6c 6f 77 20 62 75 74 20 6e 6f 6e 2d 7a 65 72   low but non-zer
12f36 6f 20 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66  o probability of
12f37 20 64 61 6d 61 67 65 2e 20 20 4c 65 76 65 6c 20   damage.  Level 
12f38 33 20 72 65 64 75 63 65 73 20 74 68 65 0a 2a 2a  3 reduces the.**
12f39 20 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20   probability of 
12f3a 64 61 6d 61 67 65 20 74 6f 20 6e 65 61 72 20 7a  damage to near z
12f3b 65 72 6f 20 62 75 74 20 77 69 74 68 20 61 20 77  ero but with a w
12f3c 72 69 74 65 20 70 65 72 66 6f 72 6d 61 6e 63 65  rite performance
12f3d 20 72 65 64 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23   reduction..*/.#
12f3e 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
12f3f 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53  IT_PAGER_PRAGMAS
12f40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
12f41 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
12f42 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 42  SetSafetyLevel(B
12f43 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6c 65 76  tree *p, int lev
12f44 65 6c 2c 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63  el, int fullSync
12f45 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  ){.  BtShared *p
12f46 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61  Bt = p->pBt;.  a
12f47 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
12f48 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d  utex_held(p->db-
12f49 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c  >mutex) );.  sql
12f4a 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
12f4b 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65  );.  sqlite3Page
12f4c 72 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28  rSetSafetyLevel(
12f4d 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6c 65 76  pBt->pPager, lev
12f4e 65 6c 2c 20 66 75 6c 6c 53 79 6e 63 29 3b 0a 20  el, fullSync);. 
12f4f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
12f50 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ve(p);.  return 
12f51 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e  SQLITE_OK;.}.#en
12f52 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  dif../*.** Retur
12f53 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 67 69  n TRUE if the gi
12f54 76 65 6e 20 62 74 72 65 65 20 69 73 20 73 65 74  ven btree is set
12f55 20 74 6f 20 73 61 66 65 74 79 20 6c 65 76 65 6c   to safety level
12f56 20 31 2e 20 20 49 6e 20 6f 74 68 65 72 0a 2a 2a   1.  In other.**
12f57 20 77 6f 72 64 73 2c 20 72 65 74 75 72 6e 20 54   words, return T
12f58 52 55 45 20 69 66 20 6e 6f 20 73 79 6e 63 28 29  RUE if no sync()
12f59 20 6f 63 63 75 72 73 20 6f 6e 20 74 68 65 20 64   occurs on the d
12f5a 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51  isk files..*/.SQ
12f5b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
12f5c 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e   sqlite3BtreeSyn
12f5d 63 44 69 73 61 62 6c 65 64 28 42 74 72 65 65 20  cDisabled(Btree 
12f5e 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  *p){.  BtShared 
12f5f 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20  *pBt = p->pBt;. 
12f60 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72   int rc;.  asser
12f61 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
12f62 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74  _held(p->db->mut
12f63 65 78 29 20 29 3b 20 20 0a 20 20 73 71 6c 69 74  ex) );  .  sqlit
12f64 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
12f65 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 20 26  .  assert( pBt &
12f66 26 20 70 42 74 2d 3e 70 50 61 67 65 72 20 29 3b  & pBt->pPager );
12f67 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50  .  rc = sqlite3P
12f68 61 67 65 72 4e 6f 73 79 6e 63 28 70 42 74 2d 3e  agerNosync(pBt->
12f69 70 50 61 67 65 72 29 3b 0a 20 20 73 71 6c 69 74  pPager);.  sqlit
12f6a 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
12f6b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
12f6c 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
12f6d 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f  LITE_OMIT_PAGER_
12f6e 50 52 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 66  PRAGMAS) || !def
12f6f 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
12f70 5f 56 41 43 55 55 4d 29 0a 2f 2a 0a 2a 2a 20 43  _VACUUM)./*.** C
12f71 68 61 6e 67 65 20 74 68 65 20 64 65 66 61 75 6c  hange the defaul
12f72 74 20 70 61 67 65 73 20 73 69 7a 65 20 61 6e 64  t pages size and
12f73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72   the number of r
12f74 65 73 65 72 76 65 64 20 62 79 74 65 73 20 70 65  eserved bytes pe
12f75 72 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  r page..**.** Th
12f76 65 20 70 61 67 65 20 73 69 7a 65 20 6d 75 73 74  e page size must
12f77 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32   be a power of 2
12f78 20 62 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64   between 512 and
12f79 20 36 35 35 33 36 2e 20 20 49 66 20 74 68 65 20   65536.  If the 
12f7a 70 61 67 65 0a 2a 2a 20 73 69 7a 65 20 73 75 70  page.** size sup
12f7b 70 6c 69 65 64 20 64 6f 65 73 20 6e 6f 74 20 6d  plied does not m
12f7c 65 65 74 20 74 68 69 73 20 63 6f 6e 73 74 72 61  eet this constra
12f7d 69 6e 74 20 74 68 65 6e 20 74 68 65 20 70 61 67  int then the pag
12f7e 65 20 73 69 7a 65 20 69 73 20 6e 6f 74 0a 2a 2a  e size is not.**
12f7f 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20   changed..**.** 
12f80 50 61 67 65 20 73 69 7a 65 73 20 61 72 65 20 63  Page sizes are c
12f81 6f 6e 73 74 72 61 69 6e 65 64 20 74 6f 20 62 65  onstrained to be
12f82 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20   a power of two 
12f83 73 6f 20 74 68 61 74 20 74 68 65 20 72 65 67 69  so that the regi
12f84 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 64 61 74  on.** of the dat
12f85 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 64 20  abase file used 
12f86 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 28 62 65 67  for locking (beg
12f87 69 6e 6e 69 6e 67 20 61 74 20 50 45 4e 44 49 4e  inning at PENDIN
12f88 47 5f 42 59 54 45 2c 0a 2a 2a 20 74 68 65 20 66  G_BYTE,.** the f
12f89 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74  irst byte past t
12f8a 68 65 20 31 47 42 20 62 6f 75 6e 64 61 72 79 2c  he 1GB boundary,
12f8b 20 30 78 34 30 30 30 30 30 30 30 29 20 6e 65 65   0x40000000) nee
12f8c 64 73 20 74 6f 20 6f 63 63 75 72 0a 2a 2a 20 61  ds to occur.** a
12f8d 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  t the beginning 
12f8e 6f 66 20 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a  of a page..**.**
12f8f 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 6e 52   If parameter nR
12f90 65 73 65 72 76 65 20 69 73 20 6c 65 73 73 20 74  eserve is less t
12f91 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74  han zero, then t
12f92 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73  he number of res
12f93 65 72 76 65 64 0a 2a 2a 20 62 79 74 65 73 20 70  erved.** bytes p
12f94 65 72 20 70 61 67 65 20 69 73 20 6c 65 66 74 20  er page is left 
12f95 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51  unchanged..*/.SQ
12f96 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
12f97 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
12f98 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 20 2a  PageSize(Btree *
12f99 70 2c 20 69 6e 74 20 70 61 67 65 53 69 7a 65 2c  p, int pageSize,
12f9a 20 69 6e 74 20 6e 52 65 73 65 72 76 65 29 7b 0a   int nReserve){.
12f9b 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
12f9c 45 5f 4f 4b 3b 0a 20 20 42 74 53 68 61 72 65 64  E_OK;.  BtShared
12f9d 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
12f9e 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
12f9f 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20 70 42  ter(p);.  if( pB
12fa0 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64  t->pageSizeFixed
12fa1 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42   ){.    sqlite3B
12fa2 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
12fa3 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
12fa4 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20  READONLY;.  }.  
12fa5 69 66 28 20 6e 52 65 73 65 72 76 65 3c 30 20 29  if( nReserve<0 )
12fa6 7b 0a 20 20 20 20 6e 52 65 73 65 72 76 65 20 3d  {.    nReserve =
12fa7 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d   pBt->pageSize -
12fa8 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
12fa9 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 61 67 65  ;.  }.  if( page
12faa 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 61 67  Size>=512 && pag
12fab 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45 5f 4d 41  eSize<=SQLITE_MA
12fac 58 5f 50 41 47 45 5f 53 49 5a 45 20 26 26 0a 20  X_PAGE_SIZE &&. 
12fad 20 20 20 20 20 20 20 28 28 70 61 67 65 53 69 7a         ((pageSiz
12fae 65 2d 31 29 26 70 61 67 65 53 69 7a 65 29 3d 3d  e-1)&pageSize)==
12faf 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  0 ){.    assert(
12fb0 20 28 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d   (pageSize & 7)=
12fb1 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
12fb2 28 20 21 70 42 74 2d 3e 70 50 61 67 65 31 20 26  ( !pBt->pPage1 &
12fb3 26 20 21 70 42 74 2d 3e 70 43 75 72 73 6f 72 20  & !pBt->pCursor 
12fb4 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 61 67 65  );.    pBt->page
12fb5 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 3b  Size = pageSize;
12fb6 0a 20 20 20 20 66 72 65 65 54 65 6d 70 53 70 61  .    freeTempSpa
12fb7 63 65 28 70 42 74 29 3b 0a 20 20 20 20 72 63 20  ce(pBt);.    rc 
12fb8 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65  = sqlite3PagerSe
12fb9 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70  tPagesize(pBt->p
12fba 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67  Pager, &pBt->pag
12fbb 65 53 69 7a 65 29 3b 0a 20 20 7d 0a 20 20 70 42  eSize);.  }.  pB
12fbc 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20  t->usableSize = 
12fbd 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20  pBt->pageSize - 
12fbe 6e 52 65 73 65 72 76 65 3b 0a 20 20 73 71 6c 69  nReserve;.  sqli
12fbf 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
12fc0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
12fc1 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
12fc2 68 65 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66  he currently def
12fc3 69 6e 65 64 20 70 61 67 65 20 73 69 7a 65 0a 2a  ined page size.*
12fc4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
12fc5 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
12fc6 65 47 65 74 50 61 67 65 53 69 7a 65 28 42 74 72  eGetPageSize(Btr
12fc7 65 65 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  ee *p){.  return
12fc8 20 70 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a   p->pBt->pageSiz
12fc9 65 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  e;.}.SQLITE_PRIV
12fca 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
12fcb 74 72 65 65 47 65 74 52 65 73 65 72 76 65 28 42  treeGetReserve(B
12fcc 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  tree *p){.  int 
12fcd 6e 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  n;.  sqlite3Btre
12fce 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 6e 20 3d  eEnter(p);.  n =
12fcf 20 70 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a   p->pBt->pageSiz
12fd0 65 20 2d 20 70 2d 3e 70 42 74 2d 3e 75 73 61 62  e - p->pBt->usab
12fd1 6c 65 53 69 7a 65 3b 0a 20 20 73 71 6c 69 74 65  leSize;.  sqlite
12fd2 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
12fd3 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f    return n;.}../
12fd4 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6d 61 78  *.** Set the max
12fd5 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 20  imum page count 
12fd6 66 6f 72 20 61 20 64 61 74 61 62 61 73 65 20 69  for a database i
12fd7 66 20 6d 78 50 61 67 65 20 69 73 20 70 6f 73 69  f mxPage is posi
12fd8 74 69 76 65 2e 0a 2a 2a 20 4e 6f 20 63 68 61 6e  tive..** No chan
12fd9 67 65 73 20 61 72 65 20 6d 61 64 65 20 69 66 20  ges are made if 
12fda 6d 78 50 61 67 65 20 69 73 20 30 20 6f 72 20 6e  mxPage is 0 or n
12fdb 65 67 61 74 69 76 65 2e 0a 2a 2a 20 52 65 67 61  egative..** Rega
12fdc 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 76 61  rdless of the va
12fdd 6c 75 65 20 6f 66 20 6d 78 50 61 67 65 2c 20 72  lue of mxPage, r
12fde 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75  eturn the maximu
12fdf 6d 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f  m page count..*/
12fe0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
12fe1 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
12fe2 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 42 74 72  MaxPageCount(Btr
12fe3 65 65 20 2a 70 2c 20 69 6e 74 20 6d 78 50 61 67  ee *p, int mxPag
12fe4 65 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73  e){.  int n;.  s
12fe5 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
12fe6 28 70 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74  (p);.  n = sqlit
12fe7 65 33 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f  e3PagerMaxPageCo
12fe8 75 6e 74 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67  unt(p->pBt->pPag
12fe9 65 72 2c 20 6d 78 50 61 67 65 29 3b 0a 20 20 73  er, mxPage);.  s
12fea 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
12feb 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 3b  (p);.  return n;
12fec 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  .}.#endif /* !de
12fed 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
12fee 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 29  T_PAGER_PRAGMAS)
12fef 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
12ff0 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29  ITE_OMIT_VACUUM)
12ff1 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67   */../*.** Chang
12ff2 65 20 74 68 65 20 27 61 75 74 6f 2d 76 61 63 75  e the 'auto-vacu
12ff3 75 6d 27 20 70 72 6f 70 65 72 74 79 20 6f 66 20  um' property of 
12ff4 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 49 66  the database. If
12ff5 20 74 68 65 20 27 61 75 74 6f 56 61 63 75 75 6d   the 'autoVacuum
12ff6 27 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69  '.** parameter i
12ff7 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
12ff8 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64   auto-vacuum mod
12ff9 65 20 69 73 20 65 6e 61 62 6c 65 64 2e 20 49 66  e is enabled. If
12ffa 20 7a 65 72 6f 2c 20 69 74 0a 2a 2a 20 69 73 20   zero, it.** is 
12ffb 64 69 73 61 62 6c 65 64 2e 20 54 68 65 20 64 65  disabled. The de
12ffc 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20  fault value for 
12ffd 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75 6d 20  the auto-vacuum 
12ffe 70 72 6f 70 65 72 74 79 20 69 73 20 0a 2a 2a 20  property is .** 
12fff 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68  determined by th
13000 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  e SQLITE_DEFAULT
13001 5f 41 55 54 4f 56 41 43 55 55 4d 20 6d 61 63 72  _AUTOVACUUM macr
13002 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  o..*/.SQLITE_PRI
13003 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
13004 42 74 72 65 65 53 65 74 41 75 74 6f 56 61 63 75  BtreeSetAutoVacu
13005 75 6d 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74  um(Btree *p, int
13006 20 61 75 74 6f 56 61 63 75 75 6d 29 7b 0a 23 69   autoVacuum){.#i
13007 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
13008 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 72 65  _AUTOVACUUM.  re
13009 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44  turn SQLITE_READ
1300a 4f 4e 4c 59 3b 0a 23 65 6c 73 65 0a 20 20 42 74  ONLY;.#else.  Bt
1300b 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
1300c 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d  >pBt;.  int rc =
1300d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e   SQLITE_OK;.  in
1300e 74 20 61 76 20 3d 20 28 61 75 74 6f 56 61 63 75  t av = (autoVacu
1300f 75 6d 3f 31 3a 30 29 3b 0a 0a 20 20 73 71 6c 69  um?1:0);..  sqli
13010 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
13011 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 61 67  ;.  if( pBt->pag
13012 65 53 69 7a 65 46 69 78 65 64 20 26 26 20 61 76  eSizeFixed && av
13013 21 3d 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  !=pBt->autoVacuu
13014 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  m ){.    rc = SQ
13015 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20  LITE_READONLY;. 
13016 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 42 74 2d   }else{.    pBt-
13017 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 61 76  >autoVacuum = av
13018 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42  ;.  }.  sqlite3B
13019 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
1301a 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69  return rc;.#endi
1301b 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  f.}../*.** Retur
1301c 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  n the value of t
1301d 68 65 20 27 61 75 74 6f 2d 76 61 63 75 75 6d 27  he 'auto-vacuum'
1301e 20 70 72 6f 70 65 72 74 79 2e 20 49 66 20 61 75   property. If au
1301f 74 6f 2d 76 61 63 75 75 6d 20 69 73 20 0a 2a 2a  to-vacuum is .**
13020 20 65 6e 61 62 6c 65 64 20 31 20 69 73 20 72 65   enabled 1 is re
13021 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73  turned. Otherwis
13022 65 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  e 0..*/.SQLITE_P
13023 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
13024 65 33 42 74 72 65 65 47 65 74 41 75 74 6f 56 61  e3BtreeGetAutoVa
13025 63 75 75 6d 28 42 74 72 65 65 20 2a 70 29 7b 0a  cuum(Btree *p){.
13026 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
13027 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
13028 72 65 74 75 72 6e 20 42 54 52 45 45 5f 41 55 54  return BTREE_AUT
13029 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 3b 0a 23 65  OVACUUM_NONE;.#e
1302a 6c 73 65 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  lse.  int rc;.  
1302b 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
1302c 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 28 0a 20  r(p);.  rc = (. 
1302d 20 20 20 28 21 70 2d 3e 70 42 74 2d 3e 61 75 74     (!p->pBt->aut
1302e 6f 56 61 63 75 75 6d 29 3f 42 54 52 45 45 5f 41  oVacuum)?BTREE_A
1302f 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 3a 0a  UTOVACUUM_NONE:.
13030 20 20 20 20 28 21 70 2d 3e 70 42 74 2d 3e 69 6e      (!p->pBt->in
13031 63 72 56 61 63 75 75 6d 29 3f 42 54 52 45 45 5f  crVacuum)?BTREE_
13032 41 55 54 4f 56 41 43 55 55 4d 5f 46 55 4c 4c 3a  AUTOVACUUM_FULL:
13033 0a 20 20 20 20 42 54 52 45 45 5f 41 55 54 4f 56  .    BTREE_AUTOV
13034 41 43 55 55 4d 5f 49 4e 43 52 0a 20 20 29 3b 0a  ACUUM_INCR.  );.
13035 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
13036 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
13037 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 0a   rc;.#endif.}...
13038 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 72 65 66 65  /*.** Get a refe
13039 72 65 6e 63 65 20 74 6f 20 70 50 61 67 65 31 20  rence to pPage1 
1303a 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
1303b 66 69 6c 65 2e 20 20 54 68 69 73 20 77 69 6c 6c  file.  This will
1303c 0a 2a 2a 20 61 6c 73 6f 20 61 63 71 75 69 72 65  .** also acquire
1303d 20 61 20 72 65 61 64 6c 6f 63 6b 20 6f 6e 20 74   a readlock on t
1303e 68 61 74 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  hat file..**.** 
1303f 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
13040 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 73  urned on success
13041 2e 20 20 49 66 20 74 68 65 20 66 69 6c 65 20 69  .  If the file i
13042 73 20 6e 6f 74 20 61 0a 2a 2a 20 77 65 6c 6c 2d  s not a.** well-
13043 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65 20  formed database 
13044 66 69 6c 65 2c 20 74 68 65 6e 20 53 51 4c 49 54  file, then SQLIT
13045 45 5f 43 4f 52 52 55 50 54 20 69 73 20 72 65 74  E_CORRUPT is ret
13046 75 72 6e 65 64 2e 0a 2a 2a 20 53 51 4c 49 54 45  urned..** SQLITE
13047 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 6e 65  _BUSY is returne
13048 64 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73  d if the databas
13049 65 20 69 73 20 6c 6f 63 6b 65 64 2e 20 20 53 51  e is locked.  SQ
1304a 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 69 73  LITE_NOMEM.** is
1304b 20 72 65 74 75 72 6e 65 64 20 69 66 20 77 65 20   returned if we 
1304c 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72  run out of memor
1304d 79 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  y. .*/.static in
1304e 74 20 6c 6f 63 6b 42 74 72 65 65 28 42 74 53 68  t lockBtree(BtSh
1304f 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e  ared *pBt){.  in
13050 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 20  t rc;.  MemPage 
13051 2a 70 50 61 67 65 31 3b 0a 20 20 69 6e 74 20 6e  *pPage1;.  int n
13052 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28  Page;..  assert(
13053 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
13054 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
13055 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 50  );.  if( pBt->pP
13056 61 67 65 31 20 29 20 72 65 74 75 72 6e 20 53 51  age1 ) return SQ
13057 4c 49 54 45 5f 4f 4b 3b 0a 20 20 72 63 20 3d 20  LITE_OK;.  rc = 
13058 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
13059 61 67 65 28 70 42 74 2c 20 31 2c 20 26 70 50 61  age(pBt, 1, &pPa
1305a 67 65 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 72  ge1, 0);.  if( r
1305b 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
1305c 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20  eturn rc;..  /* 
1305d 44 6f 20 73 6f 6d 65 20 63 68 65 63 6b 69 6e 67  Do some checking
1305e 20 74 6f 20 68 65 6c 70 20 69 6e 73 75 72 65 20   to help insure 
1305f 74 68 65 20 66 69 6c 65 20 77 65 20 6f 70 65 6e  the file we open
13060 65 64 20 72 65 61 6c 6c 79 20 69 73 0a 20 20 2a  ed really is.  *
13061 2a 20 61 20 76 61 6c 69 64 20 64 61 74 61 62 61  * a valid databa
13062 73 65 20 66 69 6c 65 2e 20 0a 20 20 2a 2f 0a 20  se file. .  */. 
13063 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
13064 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d  erPagecount(pBt-
13065 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29  >pPager, &nPage)
13066 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
13067 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74  TE_OK ){.    got
13068 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69  o page1_init_fai
13069 6c 65 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  led;.  }else if(
1306a 20 6e 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20   nPage>0 ){.    
1306b 69 6e 74 20 70 61 67 65 53 69 7a 65 3b 0a 20 20  int pageSize;.  
1306c 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65    int usableSize
1306d 3b 0a 20 20 20 20 75 38 20 2a 70 61 67 65 31 20  ;.    u8 *page1 
1306e 3d 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61 3b  = pPage1->aData;
1306f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
13070 5f 4e 4f 54 41 44 42 3b 0a 20 20 20 20 69 66 28  _NOTADB;.    if(
13071 20 6d 65 6d 63 6d 70 28 70 61 67 65 31 2c 20 7a   memcmp(page1, z
13072 4d 61 67 69 63 48 65 61 64 65 72 2c 20 31 36 29  MagicHeader, 16)
13073 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  !=0 ){.      got
13074 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69  o page1_init_fai
13075 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  led;.    }.    i
13076 66 28 20 70 61 67 65 31 5b 31 38 5d 3e 31 20 29  f( page1[18]>1 )
13077 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 72 65 61  {.      pBt->rea
13078 64 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20 20 20 7d  dOnly = 1;.    }
13079 0a 20 20 20 20 69 66 28 20 70 61 67 65 31 5b 31  .    if( page1[1
1307a 39 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 67 6f  9]>1 ){.      go
1307b 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61  to page1_init_fa
1307c 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  iled;.    }..   
1307d 20 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20   /* The maximum 
1307e 65 6d 62 65 64 64 65 64 20 66 72 61 63 74 69 6f  embedded fractio
1307f 6e 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c  n must be exactl
13080 79 20 32 35 25 2e 20 20 41 6e 64 20 74 68 65 20  y 25%.  And the 
13081 6d 69 6e 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 65  minimum.    ** e
13082 6d 62 65 64 64 65 64 20 66 72 61 63 74 69 6f 6e  mbedded fraction
13083 20 6d 75 73 74 20 62 65 20 31 32 2e 35 25 20 66   must be 12.5% f
13084 6f 72 20 62 6f 74 68 20 6c 65 61 66 2d 64 61 74  or both leaf-dat
13085 61 20 61 6e 64 20 6e 6f 6e 2d 6c 65 61 66 2d 64  a and non-leaf-d
13086 61 74 61 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20  ata..    ** The 
13087 6f 72 69 67 69 6e 61 6c 20 64 65 73 69 67 6e 20  original design 
13088 61 6c 6c 6f 77 65 64 20 74 68 65 73 65 20 61 6d  allowed these am
13089 6f 75 6e 74 73 20 74 6f 20 76 61 72 79 2c 20 62  ounts to vary, b
1308a 75 74 20 61 73 20 6f 66 0a 20 20 20 20 2a 2a 20  ut as of.    ** 
1308b 76 65 72 73 69 6f 6e 20 33 2e 36 2e 30 2c 20 77  version 3.6.0, w
1308c 65 20 72 65 71 75 69 72 65 20 74 68 65 6d 20 74  e require them t
1308d 6f 20 62 65 20 66 69 78 65 64 2e 0a 20 20 20 20  o be fixed..    
1308e 2a 2f 0a 20 20 20 20 69 66 28 20 6d 65 6d 63 6d  */.    if( memcm
1308f 70 28 26 70 61 67 65 31 5b 32 31 5d 2c 20 22 5c  p(&page1[21], "\
13090 31 30 30 5c 30 34 30 5c 30 34 30 22 2c 33 29 21  100\040\040",3)!
13091 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  =0 ){.      goto
13092 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c   page1_init_fail
13093 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 61  ed;.    }.    pa
13094 67 65 53 69 7a 65 20 3d 20 67 65 74 32 62 79 74  geSize = get2byt
13095 65 28 26 70 61 67 65 31 5b 31 36 5d 29 3b 0a 20  e(&page1[16]);. 
13096 20 20 20 69 66 28 20 28 28 70 61 67 65 53 69 7a     if( ((pageSiz
13097 65 2d 31 29 26 70 61 67 65 53 69 7a 65 29 21 3d  e-1)&pageSize)!=
13098 30 20 7c 7c 20 70 61 67 65 53 69 7a 65 3c 35 31  0 || pageSize<51
13099 32 20 7c 7c 0a 20 20 20 20 20 20 20 20 28 53 51  2 ||.        (SQ
1309a 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49  LITE_MAX_PAGE_SI
1309b 5a 45 3c 33 32 37 36 38 20 26 26 20 70 61 67 65  ZE<32768 && page
1309c 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f  Size>SQLITE_MAX_
1309d 50 41 47 45 5f 53 49 5a 45 29 0a 20 20 20 20 29  PAGE_SIZE).    )
1309e 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67  {.      goto pag
1309f 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a  e1_init_failed;.
130a0 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
130a1 28 20 28 70 61 67 65 53 69 7a 65 20 26 20 37 29  ( (pageSize & 7)
130a2 3d 3d 30 20 29 3b 0a 20 20 20 20 75 73 61 62 6c  ==0 );.    usabl
130a3 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65  eSize = pageSize
130a4 20 2d 20 70 61 67 65 31 5b 32 30 5d 3b 0a 20 20   - page1[20];.  
130a5 20 20 69 66 28 20 70 61 67 65 53 69 7a 65 21 3d    if( pageSize!=
130a6 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b  pBt->pageSize ){
130a7 0a 20 20 20 20 20 20 2f 2a 20 41 66 74 65 72 20  .      /* After 
130a8 72 65 61 64 69 6e 67 20 74 68 65 20 66 69 72 73  reading the firs
130a9 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61  t page of the da
130aa 74 61 62 61 73 65 20 61 73 73 75 6d 69 6e 67 20  tabase assuming 
130ab 61 20 70 61 67 65 20 73 69 7a 65 0a 20 20 20 20  a page size.    
130ac 20 20 2a 2a 20 6f 66 20 42 74 53 68 61 72 65 64    ** of BtShared
130ad 2e 70 61 67 65 53 69 7a 65 2c 20 77 65 20 68 61  .pageSize, we ha
130ae 76 65 20 64 69 73 63 6f 76 65 72 65 64 20 74 68  ve discovered th
130af 61 74 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65  at the page-size
130b0 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 63 74   is.      ** act
130b1 75 61 6c 6c 79 20 70 61 67 65 53 69 7a 65 2e 20  ually pageSize. 
130b2 55 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62  Unlock the datab
130b3 61 73 65 2c 20 6c 65 61 76 65 20 70 42 74 2d 3e  ase, leave pBt->
130b4 70 50 61 67 65 31 20 61 74 0a 20 20 20 20 20 20  pPage1 at.      
130b5 2a 2a 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75  ** zero and retu
130b6 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 54 68  rn SQLITE_OK. Th
130b7 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 63 61  e caller will ca
130b8 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ll this function
130b9 0a 20 20 20 20 20 20 2a 2a 20 61 67 61 69 6e 20  .      ** again 
130ba 77 69 74 68 20 74 68 65 20 63 6f 72 72 65 63 74  with the correct
130bb 20 70 61 67 65 2d 73 69 7a 65 2e 0a 20 20 20 20   page-size..    
130bc 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 6c 65 61    */.      relea
130bd 73 65 50 61 67 65 28 70 50 61 67 65 31 29 3b 0a  sePage(pPage1);.
130be 20 20 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c        pBt->usabl
130bf 65 53 69 7a 65 20 3d 20 75 73 61 62 6c 65 53 69  eSize = usableSi
130c0 7a 65 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70  ze;.      pBt->p
130c1 61 67 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69  ageSize = pageSi
130c2 7a 65 3b 0a 20 20 20 20 20 20 66 72 65 65 54 65  ze;.      freeTe
130c3 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20  mpSpace(pBt);.  
130c4 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
130c5 53 65 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d  SetPagesize(pBt-
130c6 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70  >pPager, &pBt->p
130c7 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20  ageSize);.      
130c8 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
130c9 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
130ca 75 73 61 62 6c 65 53 69 7a 65 3c 35 30 30 20 29  usableSize<500 )
130cb 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67  {.      goto pag
130cc 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a  e1_init_failed;.
130cd 20 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 70      }.    pBt->p
130ce 61 67 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69  ageSize = pageSi
130cf 7a 65 3b 0a 20 20 20 20 70 42 74 2d 3e 75 73 61  ze;.    pBt->usa
130d0 62 6c 65 53 69 7a 65 20 3d 20 75 73 61 62 6c 65  bleSize = usable
130d1 53 69 7a 65 3b 0a 23 69 66 6e 64 65 66 20 53 51  Size;.#ifndef SQ
130d2 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
130d3 43 55 55 4d 0a 20 20 20 20 70 42 74 2d 3e 61 75  CUUM.    pBt->au
130d4 74 6f 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34  toVacuum = (get4
130d5 62 79 74 65 28 26 70 61 67 65 31 5b 33 36 20 2b  byte(&page1[36 +
130d6 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20   4*4])?1:0);.   
130d7 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d   pBt->incrVacuum
130d8 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 70 61   = (get4byte(&pa
130d9 67 65 31 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31  ge1[36 + 7*4])?1
130da 3a 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a  :0);.#endif.  }.
130db 0a 20 20 2f 2a 20 6d 61 78 4c 6f 63 61 6c 20 69  .  /* maxLocal i
130dc 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d  s the maximum am
130dd 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20  ount of payload 
130de 74 6f 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c 79  to store locally
130df 20 66 6f 72 0a 20 20 2a 2a 20 61 20 63 65 6c 6c   for.  ** a cell
130e0 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 69 74 20  .  Make sure it 
130e1 69 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 20  is small enough 
130e2 73 6f 20 74 68 61 74 20 61 74 20 6c 65 61 73 74  so that at least
130e3 20 6d 69 6e 46 61 6e 6f 75 74 0a 20 20 2a 2a 20   minFanout.  ** 
130e4 63 65 6c 6c 73 20 63 61 6e 20 77 69 6c 6c 20 66  cells can will f
130e5 69 74 20 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20  it on one page. 
130e6 20 57 65 20 61 73 73 75 6d 65 20 61 20 31 30 2d   We assume a 10-
130e7 62 79 74 65 20 70 61 67 65 20 68 65 61 64 65 72  byte page header
130e8 2e 0a 20 20 2a 2a 20 42 65 73 69 64 65 73 20 74  ..  ** Besides t
130e9 68 65 20 70 61 79 6c 6f 61 64 2c 20 74 68 65 20  he payload, the 
130ea 63 65 6c 6c 20 6d 75 73 74 20 73 74 6f 72 65 3a  cell must store:
130eb 0a 20 20 2a 2a 20 20 20 20 20 32 2d 62 79 74 65  .  **     2-byte
130ec 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
130ed 63 65 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 34 2d  cell.  **     4-
130ee 62 79 74 65 20 63 68 69 6c 64 20 70 6f 69 6e 74  byte child point
130ef 65 72 0a 20 20 2a 2a 20 20 20 20 20 39 2d 62 79  er.  **     9-by
130f0 74 65 20 6e 4b 65 79 20 76 61 6c 75 65 0a 20 20  te nKey value.  
130f1 2a 2a 20 20 20 20 20 34 2d 62 79 74 65 20 6e 44  **     4-byte nD
130f2 61 74 61 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20  ata value.  **  
130f3 20 20 20 34 2d 62 79 74 65 20 6f 76 65 72 66 6c     4-byte overfl
130f4 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72 0a  ow page pointer.
130f5 20 20 2a 2a 20 53 6f 20 61 20 63 65 6c 6c 20 63    ** So a cell c
130f6 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 32 2d 62  onsists of a 2-b
130f7 79 74 65 20 70 6f 69 6e 65 72 2c 20 61 20 68 65  yte poiner, a he
130f8 61 64 65 72 20 77 68 69 63 68 20 69 73 20 61 73  ader which is as
130f9 20 6d 75 63 68 20 61 73 0a 20 20 2a 2a 20 31 37   much as.  ** 17
130fa 20 62 79 74 65 73 20 6c 6f 6e 67 2c 20 30 20 74   bytes long, 0 t
130fb 6f 20 4e 20 62 79 74 65 73 20 6f 66 20 70 61 79  o N bytes of pay
130fc 6c 6f 61 64 2c 20 61 6e 64 20 61 6e 20 6f 70 74  load, and an opt
130fd 69 6f 6e 61 6c 20 34 20 62 79 74 65 20 6f 76 65  ional 4 byte ove
130fe 72 66 6c 6f 77 0a 20 20 2a 2a 20 70 61 67 65 20  rflow.  ** page 
130ff 70 6f 69 6e 74 65 72 2e 0a 20 20 2a 2f 0a 20 20  pointer..  */.  
13100 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20  pBt->maxLocal = 
13101 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  (pBt->usableSize
13102 2d 31 32 29 2a 36 34 2f 32 35 35 20 2d 20 32 33  -12)*64/255 - 23
13103 3b 0a 20 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61  ;.  pBt->minLoca
13104 6c 20 3d 20 28 70 42 74 2d 3e 75 73 61 62 6c 65  l = (pBt->usable
13105 53 69 7a 65 2d 31 32 29 2a 33 32 2f 32 35 35 20  Size-12)*32/255 
13106 2d 20 32 33 3b 0a 20 20 70 42 74 2d 3e 6d 61 78  - 23;.  pBt->max
13107 4c 65 61 66 20 3d 20 70 42 74 2d 3e 75 73 61 62  Leaf = pBt->usab
13108 6c 65 53 69 7a 65 20 2d 20 33 35 3b 0a 20 20 70  leSize - 35;.  p
13109 42 74 2d 3e 6d 69 6e 4c 65 61 66 20 3d 20 28 70  Bt->minLeaf = (p
1310a 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31  Bt->usableSize-1
1310b 32 29 2a 33 32 2f 32 35 35 20 2d 20 32 33 3b 0a  2)*32/255 - 23;.
1310c 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6d    assert( pBt->m
1310d 61 78 4c 65 61 66 20 2b 20 32 33 20 3c 3d 20 4d  axLeaf + 23 <= M
1310e 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29  X_CELL_SIZE(pBt)
1310f 20 29 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65   );.  pBt->pPage
13110 31 20 3d 20 70 50 61 67 65 31 3b 0a 20 20 72 65  1 = pPage1;.  re
13111 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
13112 0a 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c  .page1_init_fail
13113 65 64 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67  ed:.  releasePag
13114 65 28 70 50 61 67 65 31 29 3b 0a 20 20 70 42 74  e(pPage1);.  pBt
13115 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20  ->pPage1 = 0;.  
13116 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
13117 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
13118 20 77 6f 72 6b 73 20 6c 69 6b 65 20 6c 6f 63 6b   works like lock
13119 42 74 72 65 65 28 29 20 65 78 63 65 70 74 20 74  Btree() except t
1311a 68 61 74 20 69 74 20 61 6c 73 6f 20 69 6e 76 6f  hat it also invo
1311b 6b 65 73 20 74 68 65 0a 2a 2a 20 62 75 73 79 20  kes the.** busy 
1311c 63 61 6c 6c 62 61 63 6b 20 69 66 20 74 68 65 72  callback if ther
1311d 65 20 69 73 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e  e is lock conten
1311e 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
1311f 69 6e 74 20 6c 6f 63 6b 42 74 72 65 65 57 69 74  int lockBtreeWit
13120 68 52 65 74 72 79 28 42 74 72 65 65 20 2a 70 52  hRetry(Btree *pR
13121 65 66 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ef){.  int rc = 
13122 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73  SQLITE_OK;..  as
13123 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
13124 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 52 65  eeHoldsMutex(pRe
13125 66 29 20 29 3b 0a 20 20 69 66 28 20 70 52 65 66  f) );.  if( pRef
13126 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53  ->inTrans==TRANS
13127 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 75 38 20  _NONE ){.    u8 
13128 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20  inTransaction = 
13129 70 52 65 66 2d 3e 70 42 74 2d 3e 69 6e 54 72 61  pRef->pBt->inTra
1312a 6e 73 61 63 74 69 6f 6e 3b 0a 20 20 20 20 62 74  nsaction;.    bt
1312b 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 52 65  reeIntegrity(pRe
1312c 66 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  f);.    rc = sql
1312d 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72  ite3BtreeBeginTr
1312e 61 6e 73 28 70 52 65 66 2c 20 30 29 3b 0a 20 20  ans(pRef, 0);.  
1312f 20 20 70 52 65 66 2d 3e 70 42 74 2d 3e 69 6e 54    pRef->pBt->inT
13130 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 69 6e 54  ransaction = inT
13131 72 61 6e 73 61 63 74 69 6f 6e 3b 0a 20 20 20 20  ransaction;.    
13132 70 52 65 66 2d 3e 69 6e 54 72 61 6e 73 20 3d 20  pRef->inTrans = 
13133 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20  TRANS_NONE;.    
13134 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
13135 4b 20 29 7b 0a 20 20 20 20 20 20 70 52 65 66 2d  K ){.      pRef-
13136 3e 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69  >pBt->nTransacti
13137 6f 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  on--;.    }.    
13138 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70  btreeIntegrity(p
13139 52 65 66 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Ref);.  }.  retu
1313a 72 6e 20 72 63 3b 0a 7d 0a 20 20 20 20 20 20 20  rn rc;.}.       
1313b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65  ../*.** If there
1313c 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64   are no outstand
1313d 69 6e 67 20 63 75 72 73 6f 72 73 20 61 6e 64 20  ing cursors and 
1313e 77 65 20 61 72 65 20 6e 6f 74 20 69 6e 20 74 68  we are not in th
1313f 65 20 6d 69 64 64 6c 65 0a 2a 2a 20 6f 66 20 61  e middle.** of a
13140 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 75 74   transaction but
13141 20 74 68 65 72 65 20 69 73 20 61 20 72 65 61 64   there is a read
13142 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74   lock on the dat
13143 61 62 61 73 65 2c 20 74 68 65 6e 0a 2a 2a 20 74  abase, then.** t
13144 68 69 73 20 72 6f 75 74 69 6e 65 20 75 6e 72 65  his routine unre
13145 66 73 20 74 68 65 20 66 69 72 73 74 20 70 61 67  fs the first pag
13146 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
13147 65 20 66 69 6c 65 20 77 68 69 63 68 20 0a 2a 2a  e file which .**
13148 20 68 61 73 20 74 68 65 20 65 66 66 65 63 74 20   has the effect 
13149 6f 66 20 72 65 6c 65 61 73 69 6e 67 20 74 68 65  of releasing the
1314a 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a   read lock..**.*
1314b 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61  * If there are a
1314c 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 63  ny outstanding c
1314d 75 72 73 6f 72 73 2c 20 74 68 69 73 20 72 6f 75  ursors, this rou
1314e 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tine is a no-op.
1314f 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  .**.** If there 
13150 69 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  is a transaction
13151 20 69 6e 20 70 72 6f 67 72 65 73 73 2c 20 74 68   in progress, th
13152 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
13153 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63  no-op..*/.static
13154 20 76 6f 69 64 20 75 6e 6c 6f 63 6b 42 74 72 65   void unlockBtre
13155 65 49 66 55 6e 75 73 65 64 28 42 74 53 68 61 72  eIfUnused(BtShar
13156 65 64 20 2a 70 42 74 29 7b 0a 20 20 61 73 73 65  ed *pBt){.  asse
13157 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
13158 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  x_held(pBt->mute
13159 78 29 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d  x) );.  if( pBt-
1315a 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d  >inTransaction==
1315b 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 70 42  TRANS_NONE && pB
1315c 74 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20 26 26  t->pCursor==0 &&
1315d 20 70 42 74 2d 3e 70 50 61 67 65 31 21 3d 30 20   pBt->pPage1!=0 
1315e 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
1315f 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28  e3PagerRefcount(
13160 70 42 74 2d 3e 70 50 61 67 65 72 29 3e 3d 31 20  pBt->pPager)>=1 
13161 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
13162 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44   pBt->pPage1->aD
13163 61 74 61 20 29 3b 0a 23 69 66 20 30 0a 20 20 20  ata );.#if 0.   
13164 20 20 20 69 66 28 20 70 42 74 2d 3e 70 50 61 67     if( pBt->pPag
13165 65 31 2d 3e 61 44 61 74 61 3d 3d 30 20 29 7b 0a  e1->aData==0 ){.
13166 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20          MemPage 
13167 2a 70 50 61 67 65 20 3d 20 70 42 74 2d 3e 70 50  *pPage = pBt->pP
13168 61 67 65 31 3b 0a 20 20 20 20 20 20 20 20 70 50  age1;.        pP
13169 61 67 65 2d 3e 61 44 61 74 61 20 3d 20 73 71 6c  age->aData = sql
1316a 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61  ite3PagerGetData
1316b 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
1316c 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 2d  ;.        pPage-
1316d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 20 20  >pBt = pBt;.    
1316e 20 20 20 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20      pPage->pgno 
1316f 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  = 1;.      }.#en
13170 64 69 66 0a 20 20 20 20 20 20 72 65 6c 65 61 73  dif.      releas
13171 65 50 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65  ePage(pBt->pPage
13172 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42  1);.    }.    pB
13173 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20  t->pPage1 = 0;. 
13174 20 20 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d     pBt->inStmt =
13175 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   0;.  }.}../*.**
13176 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 64 61   Create a new da
13177 74 61 62 61 73 65 20 62 79 20 69 6e 69 74 69 61  tabase by initia
13178 6c 69 7a 69 6e 67 20 74 68 65 20 66 69 72 73 74  lizing the first
13179 20 70 61 67 65 20 6f 66 20 74 68 65 0a 2a 2a 20   page of the.** 
1317a 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
1317b 69 6e 74 20 6e 65 77 44 61 74 61 62 61 73 65 28  int newDatabase(
1317c 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a  BtShared *pBt){.
1317d 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 31 3b 0a    MemPage *pP1;.
1317e 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
1317f 2a 64 61 74 61 3b 0a 20 20 69 6e 74 20 72 63 3b  *data;.  int rc;
13180 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20  .  int nPage;.. 
13181 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
13182 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
13183 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20  >mutex) );.  rc 
13184 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61  = sqlite3PagerPa
13185 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61  gecount(pBt->pPa
13186 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20  ger, &nPage);.  
13187 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
13188 4b 20 7c 7c 20 6e 50 61 67 65 3e 30 20 29 7b 0a  K || nPage>0 ){.
13189 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1318a 20 7d 0a 20 20 70 50 31 20 3d 20 70 42 74 2d 3e   }.  pP1 = pBt->
1318b 70 50 61 67 65 31 3b 0a 20 20 61 73 73 65 72 74  pPage1;.  assert
1318c 28 20 70 50 31 21 3d 30 20 29 3b 0a 20 20 64 61  ( pP1!=0 );.  da
1318d 74 61 20 3d 20 70 50 31 2d 3e 61 44 61 74 61 3b  ta = pP1->aData;
1318e 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50  .  rc = sqlite3P
1318f 61 67 65 72 57 72 69 74 65 28 70 50 31 2d 3e 70  agerWrite(pP1->p
13190 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72  DbPage);.  if( r
13191 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
13192 20 6d 65 6d 63 70 79 28 64 61 74 61 2c 20 7a 4d   memcpy(data, zM
13193 61 67 69 63 48 65 61 64 65 72 2c 20 73 69 7a 65  agicHeader, size
13194 6f 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72 29  of(zMagicHeader)
13195 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a  );.  assert( siz
13196 65 6f 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72  eof(zMagicHeader
13197 29 3d 3d 31 36 20 29 3b 0a 20 20 70 75 74 32 62  )==16 );.  put2b
13198 79 74 65 28 26 64 61 74 61 5b 31 36 5d 2c 20 70  yte(&data[16], p
13199 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20  Bt->pageSize);. 
1319a 20 64 61 74 61 5b 31 38 5d 20 3d 20 31 3b 0a 20   data[18] = 1;. 
1319b 20 64 61 74 61 5b 31 39 5d 20 3d 20 31 3b 0a 20   data[19] = 1;. 
1319c 20 64 61 74 61 5b 32 30 5d 20 3d 20 70 42 74 2d   data[20] = pBt-
1319d 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 42 74 2d  >pageSize - pBt-
1319e 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 64  >usableSize;.  d
1319f 61 74 61 5b 32 31 5d 20 3d 20 36 34 3b 0a 20 20  ata[21] = 64;.  
131a0 64 61 74 61 5b 32 32 5d 20 3d 20 33 32 3b 0a 20  data[22] = 32;. 
131a1 20 64 61 74 61 5b 32 33 5d 20 3d 20 33 32 3b 0a   data[23] = 32;.
131a2 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 32    memset(&data[2
131a3 34 5d 2c 20 30 2c 20 31 30 30 2d 32 34 29 3b 0a  4], 0, 100-24);.
131a4 20 20 7a 65 72 6f 50 61 67 65 28 70 50 31 2c 20    zeroPage(pP1, 
131a5 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c  PTF_INTKEY|PTF_L
131a6 45 41 46 7c 50 54 46 5f 4c 45 41 46 44 41 54 41  EAF|PTF_LEAFDATA
131a7 20 29 3b 0a 20 20 70 42 74 2d 3e 70 61 67 65 53   );.  pBt->pageS
131a8 69 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a 23 69  izeFixed = 1;.#i
131a9 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
131aa 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 61  T_AUTOVACUUM.  a
131ab 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f  ssert( pBt->auto
131ac 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70 42 74  Vacuum==1 || pBt
131ad 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d 3d 30 20  ->autoVacuum==0 
131ae 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  );.  assert( pBt
131af 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d 31 20  ->incrVacuum==1 
131b0 7c 7c 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75  || pBt->incrVacu
131b1 75 6d 3d 3d 30 20 29 3b 0a 20 20 70 75 74 34 62  um==0 );.  put4b
131b2 79 74 65 28 26 64 61 74 61 5b 33 36 20 2b 20 34  yte(&data[36 + 4
131b3 2a 34 5d 2c 20 70 42 74 2d 3e 61 75 74 6f 56 61  *4], pBt->autoVa
131b4 63 75 75 6d 29 3b 0a 20 20 70 75 74 34 62 79 74  cuum);.  put4byt
131b5 65 28 26 64 61 74 61 5b 33 36 20 2b 20 37 2a 34  e(&data[36 + 7*4
131b6 5d 2c 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75  ], pBt->incrVacu
131b7 75 6d 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65  um);.#endif.  re
131b8 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
131b9 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74  }../*.** Attempt
131ba 20 74 6f 20 73 74 61 72 74 20 61 20 6e 65 77 20   to start a new 
131bb 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 20 77  transaction. A w
131bc 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
131bd 0a 2a 2a 20 69 73 20 73 74 61 72 74 65 64 20 69  .** is started i
131be 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  f the second arg
131bf 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 7a 65 72 6f  ument is nonzero
131c0 2c 20 6f 74 68 65 72 77 69 73 65 20 61 20 72 65  , otherwise a re
131c1 61 64 2d 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69  ad-.** transacti
131c2 6f 6e 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f  on.  If the seco
131c3 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 32  nd argument is 2
131c4 20 6f 72 20 6d 6f 72 65 20 61 6e 64 20 65 78 63   or more and exc
131c5 6c 75 73 69 76 65 0a 2a 2a 20 74 72 61 6e 73 61  lusive.** transa
131c6 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 65 64  ction is started
131c7 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 6e  , meaning that n
131c8 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20  o other process 
131c9 69 73 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 6f  is allowed.** to
131ca 20 61 63 63 65 73 73 20 74 68 65 20 64 61 74 61   access the data
131cb 62 61 73 65 2e 20 20 41 20 70 72 65 65 78 69 73  base.  A preexis
131cc 74 69 6e 67 20 74 72 61 6e 73 61 63 74 69 6f 6e  ting transaction
131cd 20 6d 61 79 20 6e 6f 74 20 62 65 0a 2a 2a 20 75   may not be.** u
131ce 70 67 72 61 64 65 64 20 74 6f 20 65 78 63 6c 75  pgraded to exclu
131cf 73 69 76 65 20 62 79 20 63 61 6c 6c 69 6e 67 20  sive by calling 
131d0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 20 73  this routine a s
131d1 65 63 6f 6e 64 20 74 69 6d 65 20 2d 20 74 68 65  econd time - the
131d2 0a 2a 2a 20 65 78 63 6c 75 73 69 76 69 74 79 20  .** exclusivity 
131d3 66 6c 61 67 20 6f 6e 6c 79 20 77 6f 72 6b 73 20  flag only works 
131d4 66 6f 72 20 61 20 6e 65 77 20 74 72 61 6e 73 61  for a new transa
131d5 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 77  ction..**.** A w
131d6 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
131d7 20 6d 75 73 74 20 62 65 20 73 74 61 72 74 65 64   must be started
131d8 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70 74 69   before attempti
131d9 6e 67 20 61 6e 79 20 0a 2a 2a 20 63 68 61 6e 67  ng any .** chang
131da 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  es to the databa
131db 73 65 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 68 65  se.  None of the
131dc 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69   following routi
131dd 6e 65 73 20 0a 2a 2a 20 77 69 6c 6c 20 77 6f 72  nes .** will wor
131de 6b 20 75 6e 6c 65 73 73 20 61 20 74 72 61 6e 73  k unless a trans
131df 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 65  action is starte
131e0 64 20 66 69 72 73 74 3a 0a 2a 2a 0a 2a 2a 20 20  d first:.**.**  
131e1 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
131e2 43 72 65 61 74 65 54 61 62 6c 65 28 29 0a 2a 2a  CreateTable().**
131e3 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
131e4 65 65 43 72 65 61 74 65 49 6e 64 65 78 28 29 0a  eeCreateIndex().
131e5 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  **      sqlite3B
131e6 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28 29  treeClearTable()
131e7 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33  .**      sqlite3
131e8 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 29  BtreeDropTable()
131e9 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33  .**      sqlite3
131ea 42 74 72 65 65 49 6e 73 65 72 74 28 29 0a 2a 2a  BtreeInsert().**
131eb 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
131ec 65 65 44 65 6c 65 74 65 28 29 0a 2a 2a 20 20 20  eeDelete().**   
131ed 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 55     sqlite3BtreeU
131ee 70 64 61 74 65 4d 65 74 61 28 29 0a 2a 2a 0a 2a  pdateMeta().**.*
131ef 2a 20 49 66 20 61 6e 20 69 6e 69 74 69 61 6c 20  * If an initial 
131f0 61 74 74 65 6d 70 74 20 74 6f 20 61 63 71 75 69  attempt to acqui
131f1 72 65 20 74 68 65 20 6c 6f 63 6b 20 66 61 69 6c  re the lock fail
131f2 73 20 62 65 63 61 75 73 65 20 6f 66 20 6c 6f 63  s because of loc
131f3 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 0a 2a 2a 20  k contention.** 
131f4 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65  and the database
131f5 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20   was previously 
131f6 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 6e 20 69  unlocked, then i
131f7 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68  nvoke the busy h
131f8 61 6e 64 6c 65 72 0a 2a 2a 20 69 66 20 74 68 65  andler.** if the
131f9 72 65 20 69 73 20 6f 6e 65 2e 20 20 42 75 74 20  re is one.  But 
131fa 69 66 20 74 68 65 72 65 20 77 61 73 20 70 72 65  if there was pre
131fb 76 69 6f 75 73 6c 79 20 61 20 72 65 61 64 2d 6c  viously a read-l
131fc 6f 63 6b 2c 20 64 6f 20 6e 6f 74 0a 2a 2a 20 69  ock, do not.** i
131fd 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68  nvoke the busy h
131fe 61 6e 64 6c 65 72 20 2d 20 6a 75 73 74 20 72 65  andler - just re
131ff 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
13200 2e 20 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69  .  SQLITE_BUSY i
13201 73 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 77  s .** returned w
13202 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 6c 72  hen there is alr
13203 65 61 64 79 20 61 20 72 65 61 64 2d 6c 6f 63 6b  eady a read-lock
13204 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 61 76 6f   in order to avo
13205 69 64 20 61 20 64 65 61 64 6c 6f 63 6b 2e 0a 2a  id a deadlock..*
13206 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 74 68 65  *.** Suppose the
13207 72 65 20 61 72 65 20 74 77 6f 20 70 72 6f 63 65  re are two proce
13208 73 73 65 73 20 41 20 61 6e 64 20 42 2e 20 20 41  sses A and B.  A
13209 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b   has a read lock
1320a 20 61 6e 64 20 42 20 68 61 73 0a 2a 2a 20 61 20   and B has.** a 
1320b 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2e 20 20  reserved lock.  
1320c 42 20 74 72 69 65 73 20 74 6f 20 70 72 6f 6d 6f  B tries to promo
1320d 74 65 20 74 6f 20 65 78 63 6c 75 73 69 76 65 20  te to exclusive 
1320e 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62  but is blocked b
1320f 65 63 61 75 73 65 0a 2a 2a 20 6f 66 20 41 27 73  ecause.** of A's
13210 20 72 65 61 64 20 6c 6f 63 6b 2e 20 20 41 20 74   read lock.  A t
13211 72 69 65 73 20 74 6f 20 70 72 6f 6d 6f 74 65 20  ries to promote 
13212 74 6f 20 72 65 73 65 72 76 65 64 20 62 75 74 20  to reserved but 
13213 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 42 2e  is blocked by B.
13214 0a 2a 2a 20 4f 6e 65 20 6f 72 20 74 68 65 20 6f  .** One or the o
13215 74 68 65 72 20 6f 66 20 74 68 65 20 74 77 6f 20  ther of the two 
13216 70 72 6f 63 65 73 73 65 73 20 6d 75 73 74 20 67  processes must g
13217 69 76 65 20 77 61 79 20 6f 72 20 74 68 65 72 65  ive way or there
13218 20 63 61 6e 20 62 65 0a 2a 2a 20 6e 6f 20 70 72   can be.** no pr
13219 6f 67 72 65 73 73 2e 20 20 42 79 20 72 65 74 75  ogress.  By retu
1321a 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 42 55 53  rning SQLITE_BUS
1321b 59 20 61 6e 64 20 6e 6f 74 20 69 6e 76 6f 6b 69  Y and not invoki
1321c 6e 67 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c  ng the busy call
1321d 62 61 63 6b 0a 2a 2a 20 77 68 65 6e 20 41 20 61  back.** when A a
1321e 6c 72 65 61 64 79 20 68 61 73 20 61 20 72 65 61  lready has a rea
1321f 64 20 6c 6f 63 6b 2c 20 77 65 20 65 6e 63 6f 75  d lock, we encou
13220 72 61 67 65 20 41 20 74 6f 20 67 69 76 65 20 75  rage A to give u
13221 70 20 61 6e 64 20 6c 65 74 20 42 0a 2a 2a 20 70  p and let B.** p
13222 72 6f 63 65 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  roceed..*/.SQLIT
13223 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
13224 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54  lite3BtreeBeginT
13225 72 61 6e 73 28 42 74 72 65 65 20 2a 70 2c 20 69  rans(Btree *p, i
13226 6e 74 20 77 72 66 6c 61 67 29 7b 0a 20 20 42 74  nt wrflag){.  Bt
13227 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
13228 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d  >pBt;.  int rc =
13229 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 73   SQLITE_OK;..  s
1322a 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1322b 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 62 20 3d  (p);.  pBt->db =
1322c 20 70 2d 3e 64 62 3b 0a 20 20 62 74 72 65 65 49   p->db;.  btreeI
1322d 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 0a 20 20  ntegrity(p);..  
1322e 2f 2a 20 49 66 20 74 68 65 20 62 74 72 65 65 20  /* If the btree 
1322f 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 61 20  is already in a 
13230 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
13231 6e 2c 20 6f 72 20 69 74 0a 20 20 2a 2a 20 69 73  n, or it.  ** is
13232 20 61 6c 72 65 61 64 79 20 69 6e 20 61 20 72 65   already in a re
13233 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 61  ad-transaction a
13234 6e 64 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61  nd a read-transa
13235 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 72 65  ction.  ** is re
13236 71 75 65 73 74 65 64 2c 20 74 68 69 73 20 69 73  quested, this is
13237 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2f 0a 20   a no-op..  */. 
13238 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d   if( p->inTrans=
13239 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 7c 7c 20  =TRANS_WRITE || 
1323a 28 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41  (p->inTrans==TRA
1323b 4e 53 5f 52 45 41 44 20 26 26 20 21 77 72 66 6c  NS_READ && !wrfl
1323c 61 67 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ag) ){.    goto 
1323d 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d  trans_begun;.  }
1323e 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 72 61  ..  /* Write tra
1323f 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f  nsactions are no
13240 74 20 70 6f 73 73 69 62 6c 65 20 6f 6e 20 61 20  t possible on a 
13241 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61  read-only databa
13242 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 42 74 2d  se */.  if( pBt-
13243 3e 72 65 61 64 4f 6e 6c 79 20 26 26 20 77 72 66  >readOnly && wrf
13244 6c 61 67 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  lag ){.    rc = 
13245 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b  SQLITE_READONLY;
13246 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f  .    goto trans_
13247 62 65 67 75 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  begun;.  }..  /*
13248 20 49 66 20 61 6e 6f 74 68 65 72 20 64 61 74 61   If another data
13249 62 61 73 65 20 68 61 6e 64 6c 65 20 68 61 73 20  base handle has 
1324a 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 64 20 61  already opened a
1324b 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
1324c 6f 6e 20 0a 20 20 2a 2a 20 6f 6e 20 74 68 69 73  on .  ** on this
1324d 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 73 74   shared-btree st
1324e 72 75 63 74 75 72 65 20 61 6e 64 20 61 20 73 65  ructure and a se
1324f 63 6f 6e 64 20 77 72 69 74 65 20 74 72 61 6e 73  cond write trans
13250 61 63 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 72  action is.  ** r
13251 65 71 75 65 73 74 65 64 2c 20 72 65 74 75 72 6e  equested, return
13252 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a 20 20   SQLITE_BUSY..  
13253 2a 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e 69 6e  */.  if( pBt->in
13254 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41  Transaction==TRA
13255 4e 53 5f 57 52 49 54 45 20 26 26 20 77 72 66 6c  NS_WRITE && wrfl
13256 61 67 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  ag ){.    rc = S
13257 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20  QLITE_BUSY;.    
13258 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e  goto trans_begun
13259 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53  ;.  }..#ifndef S
1325a 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
1325b 44 5f 43 41 43 48 45 0a 20 20 69 66 28 20 77 72  D_CACHE.  if( wr
1325c 66 6c 61 67 3e 31 20 29 7b 0a 20 20 20 20 42 74  flag>1 ){.    Bt
1325d 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 20 20 20  Lock *pIter;.   
1325e 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e   for(pIter=pBt->
1325f 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49  pLock; pIter; pI
13260 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74  ter=pIter->pNext
13261 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 49 74  ){.      if( pIt
13262 65 72 2d 3e 70 42 74 72 65 65 21 3d 70 20 29 7b  er->pBtree!=p ){
13263 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
13264 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20  LITE_BUSY;.     
13265 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65     goto trans_be
13266 67 75 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  gun;.      }.   
13267 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20   }.  }.#endif.. 
13268 20 64 6f 20 7b 0a 20 20 20 20 69 66 28 20 70 42   do {.    if( pB
13269 74 2d 3e 70 50 61 67 65 31 3d 3d 30 20 29 7b 0a  t->pPage1==0 ){.
1326a 20 20 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20        do{.      
1326b 20 20 72 63 20 3d 20 6c 6f 63 6b 42 74 72 65 65    rc = lockBtree
1326c 28 70 42 74 29 3b 0a 20 20 20 20 20 20 7d 77 68  (pBt);.      }wh
1326d 69 6c 65 28 20 70 42 74 2d 3e 70 50 61 67 65 31  ile( pBt->pPage1
1326e 3d 3d 30 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  ==0 && rc==SQLIT
1326f 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 7d 0a 0a 20  E_OK );.    }.. 
13270 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
13271 45 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29  E_OK && wrflag )
13272 7b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d  {.      if( pBt-
13273 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20  >readOnly ){.   
13274 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
13275 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 20  _READONLY;.     
13276 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
13277 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
13278 72 42 65 67 69 6e 28 70 42 74 2d 3e 70 50 61 67  rBegin(pBt->pPag
13279 65 31 2d 3e 70 44 62 50 61 67 65 2c 20 77 72 66  e1->pDbPage, wrf
1327a 6c 61 67 3e 31 29 3b 0a 20 20 20 20 20 20 20 20  lag>1);.        
1327b 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1327c 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  K ){.          r
1327d 63 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65 28  c = newDatabase(
1327e 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  pBt);.        }.
1327f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
13280 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
13281 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
13282 69 66 28 20 77 72 66 6c 61 67 20 29 20 70 42 74  if( wrflag ) pBt
13283 2d 3e 69 6e 53 74 6d 74 20 3d 20 30 3b 0a 20 20  ->inStmt = 0;.  
13284 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 75    }else{.      u
13285 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73  nlockBtreeIfUnus
13286 65 64 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a 20  ed(pBt);.    }. 
13287 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c   }while( rc==SQL
13288 49 54 45 5f 42 55 53 59 20 26 26 20 70 42 74 2d  ITE_BUSY && pBt-
13289 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d  >inTransaction==
1328a 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 0a 20 20  TRANS_NONE &&.  
1328b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42          sqlite3B
1328c 74 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48 61  treeInvokeBusyHa
1328d 6e 64 6c 65 72 28 70 42 74 2c 20 30 29 20 29 3b  ndler(pBt, 0) );
1328e 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
1328f 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28  TE_OK ){.    if(
13290 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41   p->inTrans==TRA
13291 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 20  NS_NONE ){.     
13292 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69   pBt->nTransacti
13293 6f 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  on++;.    }.    
13294 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 28 77 72  p->inTrans = (wr
13295 66 6c 61 67 3f 54 52 41 4e 53 5f 57 52 49 54 45  flag?TRANS_WRITE
13296 3a 54 52 41 4e 53 5f 52 45 41 44 29 3b 0a 20 20  :TRANS_READ);.  
13297 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73    if( p->inTrans
13298 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74  >pBt->inTransact
13299 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 70 42 74  ion ){.      pBt
1329a 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20  ->inTransaction 
1329b 3d 20 70 2d 3e 69 6e 54 72 61 6e 73 3b 0a 20 20  = p->inTrans;.  
1329c 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
1329d 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
1329e 41 43 48 45 0a 20 20 20 20 69 66 28 20 77 72 66  ACHE.    if( wrf
1329f 6c 61 67 3e 31 20 29 7b 0a 20 20 20 20 20 20 61  lag>1 ){.      a
132a0 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70 45 78  ssert( !pBt->pEx
132a1 63 6c 75 73 69 76 65 20 29 3b 0a 20 20 20 20 20  clusive );.     
132a2 20 70 42 74 2d 3e 70 45 78 63 6c 75 73 69 76 65   pBt->pExclusive
132a3 20 3d 20 70 3b 0a 20 20 20 20 7d 0a 23 65 6e 64   = p;.    }.#end
132a4 69 66 0a 20 20 7d 0a 0a 0a 74 72 61 6e 73 5f 62  if.  }...trans_b
132a5 65 67 75 6e 3a 0a 20 20 62 74 72 65 65 49 6e 74  egun:.  btreeInt
132a6 65 67 72 69 74 79 28 70 29 3b 0a 20 20 73 71 6c  egrity(p);.  sql
132a7 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
132a8 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
132a9 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  }...#ifndef SQLI
132aa 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
132ab 55 4d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  UM../*.** Set th
132ac 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e  e pointer-map en
132ad 74 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 63 68  tries for all ch
132ae 69 6c 64 72 65 6e 20 6f 66 20 70 61 67 65 20 70  ildren of page p
132af 50 61 67 65 2e 20 41 6c 73 6f 2c 20 69 66 0a 2a  Page. Also, if.*
132b0 2a 20 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73  * pPage contains
132b1 20 63 65 6c 6c 73 20 74 68 61 74 20 70 6f 69 6e   cells that poin
132b2 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61  t to overflow pa
132b3 67 65 73 2c 20 73 65 74 20 74 68 65 20 70 6f 69  ges, set the poi
132b4 6e 74 65 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72  nter.** map entr
132b5 69 65 73 20 66 6f 72 20 74 68 65 20 6f 76 65 72  ies for the over
132b6 66 6c 6f 77 20 70 61 67 65 73 20 61 73 20 77 65  flow pages as we
132b7 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ll..*/.static in
132b8 74 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70  t setChildPtrmap
132b9 73 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  s(MemPage *pPage
132ba 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  ){.  int i;     
132bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
132bc 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74          /* Count
132bd 65 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20  er variable */. 
132be 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20   int nCell;     
132bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
132c0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
132c1 20 63 65 6c 6c 73 20 69 6e 20 70 61 67 65 20 70   cells in page p
132c2 50 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63  Page */.  int rc
132c3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
132c4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
132c5 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
132c6 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
132c7 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69   pPage->pBt;.  i
132c8 6e 74 20 69 73 49 6e 69 74 4f 72 69 67 20 3d 20  nt isInitOrig = 
132c9 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 3b 0a 20  pPage->isInit;. 
132ca 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 61   Pgno pgno = pPa
132cb 67 65 2d 3e 70 67 6e 6f 3b 0a 0a 20 20 61 73 73  ge->pgno;..  ass
132cc 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
132cd 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
132ce 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
132cf 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
132d0 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 65 29  eInitPage(pPage)
132d1 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
132d2 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74  TE_OK ){.    got
132d3 6f 20 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d  o set_child_ptrm
132d4 61 70 73 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 6e  aps_out;.  }.  n
132d5 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43  Cell = pPage->nC
132d6 65 6c 6c 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b  ell;..  for(i=0;
132d7 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a   i<nCell; i++){.
132d8 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20      u8 *pCell = 
132d9 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20  findCell(pPage, 
132da 69 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 70 74  i);..    rc = pt
132db 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70  rmapPutOvflPtr(p
132dc 50 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20  Page, pCell);.  
132dd 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
132de 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  _OK ){.      got
132df 6f 20 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d  o set_child_ptrm
132e0 61 70 73 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a  aps_out;.    }..
132e1 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e      if( !pPage->
132e2 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 50 67  leaf ){.      Pg
132e3 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 67  no childPgno = g
132e4 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3b 0a  et4byte(pCell);.
132e5 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61        rc = ptrma
132e6 70 50 75 74 28 70 42 74 2c 20 63 68 69 6c 64 50  pPut(pBt, childP
132e7 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45  gno, PTRMAP_BTRE
132e8 45 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20  E, pgno);.      
132e9 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
132ea 4b 20 29 20 67 6f 74 6f 20 73 65 74 5f 63 68 69  K ) goto set_chi
132eb 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3b 0a  ld_ptrmaps_out;.
132ec 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28      }.  }..  if(
132ed 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
132ee 0a 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 50  .    Pgno childP
132ef 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26  gno = get4byte(&
132f0 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61  pPage->aData[pPa
132f1 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  ge->hdrOffset+8]
132f2 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d  );.    rc = ptrm
132f3 61 70 50 75 74 28 70 42 74 2c 20 63 68 69 6c 64  apPut(pBt, child
132f4 50 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52  Pgno, PTRMAP_BTR
132f5 45 45 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 0a  EE, pgno);.  }..
132f6 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70  set_child_ptrmap
132f7 73 5f 6f 75 74 3a 0a 20 20 70 50 61 67 65 2d 3e  s_out:.  pPage->
132f8 69 73 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f  isInit = isInitO
132f9 72 69 67 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  rig;.  return rc
132fa 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 77  ;.}../*.** Somew
132fb 68 65 72 65 20 6f 6e 20 70 50 61 67 65 2c 20 77  here on pPage, w
132fc 68 69 63 68 20 69 73 20 67 75 61 72 65 6e 74 65  hich is guarente
132fd 65 64 20 74 6f 20 62 65 20 61 20 62 74 72 65 65  ed to be a btree
132fe 20 70 61 67 65 2c 20 6e 6f 74 20 61 6e 20 6f 76   page, not an ov
132ff 65 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 2c 20  erflow.** page, 
13300 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
13301 70 61 67 65 20 69 46 72 6f 6d 2e 20 4d 6f 64 69  page iFrom. Modi
13302 66 79 20 74 68 69 73 20 70 6f 69 6e 74 65 72 20  fy this pointer 
13303 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74  so that it point
13304 73 20 74 6f 0a 2a 2a 20 69 54 6f 2e 20 50 61 72  s to.** iTo. Par
13305 61 6d 65 74 65 72 20 65 54 79 70 65 20 64 65 73  ameter eType des
13306 63 72 69 62 65 73 20 74 68 65 20 74 79 70 65 20  cribes the type 
13307 6f 66 20 70 6f 69 6e 74 65 72 20 74 6f 20 62 65  of pointer to be
13308 20 6d 6f 64 69 66 69 65 64 2c 20 61 73 20 0a 2a   modified, as .*
13309 2a 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  * follows:.**.**
1330a 20 50 54 52 4d 41 50 5f 42 54 52 45 45 3a 20 20   PTRMAP_BTREE:  
1330b 20 20 20 70 50 61 67 65 20 69 73 20 61 20 62 74     pPage is a bt
1330c 72 65 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f  ree-page. The po
1330d 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20  inter points at 
1330e 61 20 63 68 69 6c 64 20 0a 2a 2a 20 20 20 20 20  a child .**     
1330f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61                pa
13310 67 65 20 6f 66 20 70 50 61 67 65 2e 0a 2a 2a 0a  ge of pPage..**.
13311 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  ** PTRMAP_OVERFL
13312 4f 57 31 3a 20 70 50 61 67 65 20 69 73 20 61 20  OW1: pPage is a 
13313 62 74 72 65 65 2d 70 61 67 65 2e 20 54 68 65 20  btree-page. The 
13314 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61  pointer points a
13315 74 20 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a  t an overflow.**
13316 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13317 20 20 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20     page pointed 
13318 74 6f 20 62 79 20 6f 6e 65 20 6f 66 20 74 68 65  to by one of the
13319 20 63 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65 2e   cells on pPage.
1331a 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56  .**.** PTRMAP_OV
1331b 45 52 46 4c 4f 57 32 3a 20 70 50 61 67 65 20 69  ERFLOW2: pPage i
1331c 73 20 61 6e 20 6f 76 65 72 66 6c 6f 77 2d 70 61  s an overflow-pa
1331d 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20  ge. The pointer 
1331e 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6e 65  points at the ne
1331f 78 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  xt.**           
13320 20 20 20 20 20 20 20 20 6f 76 65 72 66 6c 6f 77          overflow
13321 20 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69 73   page in the lis
13322 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
13323 20 6d 6f 64 69 66 79 50 61 67 65 50 6f 69 6e 74   modifyPagePoint
13324 65 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  er(MemPage *pPag
13325 65 2c 20 50 67 6e 6f 20 69 46 72 6f 6d 2c 20 50  e, Pgno iFrom, P
13326 67 6e 6f 20 69 54 6f 2c 20 75 38 20 65 54 79 70  gno iTo, u8 eTyp
13327 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  e){.  assert( sq
13328 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
13329 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74  (pPage->pBt->mut
1332a 65 78 29 20 29 3b 0a 20 20 69 66 28 20 65 54 79  ex) );.  if( eTy
1332b 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46  pe==PTRMAP_OVERF
1332c 4c 4f 57 32 20 29 7b 0a 20 20 20 20 2f 2a 20 54  LOW2 ){.    /* T
1332d 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20 61 6c  he pointer is al
1332e 77 61 79 73 20 74 68 65 20 66 69 72 73 74 20 34  ways the first 4
1332f 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 70 61   bytes of the pa
13330 67 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e  ge in this case.
13331 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 67 65 74    */.    if( get
13332 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61  4byte(pPage->aDa
13333 74 61 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20  ta)!=iFrom ){.  
13334 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
13335 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
13336 20 20 20 20 7d 0a 20 20 20 20 70 75 74 34 62 79      }.    put4by
13337 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c  te(pPage->aData,
13338 20 69 54 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a   iTo);.  }else{.
13339 20 20 20 20 69 6e 74 20 69 73 49 6e 69 74 4f 72      int isInitOr
1333a 69 67 20 3d 20 70 50 61 67 65 2d 3e 69 73 49 6e  ig = pPage->isIn
1333b 69 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20  it;.    int i;. 
1333c 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 0a 20     int nCell;.. 
1333d 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 49     sqlite3BtreeI
1333e 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a  nitPage(pPage);.
1333f 20 20 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67      nCell = pPag
13340 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 66  e->nCell;..    f
13341 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b  or(i=0; i<nCell;
13342 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 75 38 20   i++){.      u8 
13343 2a 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c  *pCell = findCel
13344 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a 20 20 20  l(pPage, i);.   
13345 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54     if( eType==PT
13346 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 29  RMAP_OVERFLOW1 )
13347 7b 0a 20 20 20 20 20 20 20 20 43 65 6c 6c 49 6e  {.        CellIn
13348 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 20  fo info;.       
13349 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72   sqlite3BtreePar
1334a 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c  seCellPtr(pPage,
1334b 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a   pCell, &info);.
1334c 20 20 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f          if( info
1334d 2e 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20  .iOverflow ){.  
1334e 20 20 20 20 20 20 20 20 69 66 28 20 69 46 72 6f          if( iFro
1334f 6d 3d 3d 67 65 74 34 62 79 74 65 28 26 70 43 65  m==get4byte(&pCe
13350 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f  ll[info.iOverflo
13351 77 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  w]) ){.         
13352 20 20 20 70 75 74 34 62 79 74 65 28 26 70 43 65     put4byte(&pCe
13353 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f  ll[info.iOverflo
13354 77 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20  w], iTo);.      
13355 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
13356 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
13357 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a   }.      }else{.
13358 20 20 20 20 20 20 20 20 69 66 28 20 67 65 74 34          if( get4
13359 62 79 74 65 28 70 43 65 6c 6c 29 3d 3d 69 46 72  byte(pCell)==iFr
1335a 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  om ){.          
1335b 70 75 74 34 62 79 74 65 28 70 43 65 6c 6c 2c 20  put4byte(pCell, 
1335c 69 54 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20  iTo);.          
1335d 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
1335e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1335f 20 0a 20 20 20 20 69 66 28 20 69 3d 3d 6e 43 65   .    if( i==nCe
13360 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ll ){.      if( 
13361 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 42 54  eType!=PTRMAP_BT
13362 52 45 45 20 7c 7c 20 0a 20 20 20 20 20 20 20 20  REE || .        
13363 20 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67    get4byte(&pPag
13364 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e  e->aData[pPage->
13365 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 21 3d 69  hdrOffset+8])!=i
13366 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  From ){.        
13367 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
13368 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
13369 20 20 7d 0a 20 20 20 20 20 20 70 75 74 34 62 79    }.      put4by
1336a 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61  te(&pPage->aData
1336b 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65  [pPage->hdrOffse
1336c 74 2b 38 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20  t+8], iTo);.    
1336d 7d 0a 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73  }..    pPage->is
1336e 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 69  Init = isInitOri
1336f 67 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  g;.  }.  return 
13370 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f  SQLITE_OK;.}.../
13371 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 6f 70  *.** Move the op
13372 65 6e 20 64 61 74 61 62 61 73 65 20 70 61 67 65  en database page
13373 20 70 44 62 50 61 67 65 20 74 6f 20 6c 6f 63 61   pDbPage to loca
13374 74 69 6f 6e 20 69 46 72 65 65 50 61 67 65 20 69  tion iFreePage i
13375 6e 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61  n the .** databa
13376 73 65 2e 20 54 68 65 20 70 44 62 50 61 67 65 20  se. The pDbPage 
13377 72 65 66 65 72 65 6e 63 65 20 72 65 6d 61 69 6e  reference remain
13378 73 20 76 61 6c 69 64 2e 0a 2a 2f 0a 73 74 61 74  s valid..*/.stat
13379 69 63 20 69 6e 74 20 72 65 6c 6f 63 61 74 65 50  ic int relocateP
1337a 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20  age(.  BtShared 
1337b 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20 20 20  *pBt,           
1337c 2f 2a 20 42 74 72 65 65 20 2a 2f 0a 20 20 4d 65  /* Btree */.  Me
1337d 6d 50 61 67 65 20 2a 70 44 62 50 61 67 65 2c 20  mPage *pDbPage, 
1337e 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70         /* Open p
1337f 61 67 65 20 74 6f 20 6d 6f 76 65 20 2a 2f 0a 20  age to move */. 
13380 20 75 38 20 65 54 79 70 65 2c 20 20 20 20 20 20   u8 eType,      
13381 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
13382 6e 74 65 72 20 6d 61 70 20 27 74 79 70 65 27 20  nter map 'type' 
13383 65 6e 74 72 79 20 66 6f 72 20 70 44 62 50 61 67  entry for pDbPag
13384 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 74 72  e */.  Pgno iPtr
13385 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20  Page,           
13386 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 27  /* Pointer map '
13387 70 61 67 65 2d 6e 6f 27 20 65 6e 74 72 79 20 66  page-no' entry f
13388 6f 72 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20  or pDbPage */.  
13389 50 67 6e 6f 20 69 46 72 65 65 50 61 67 65 2c 20  Pgno iFreePage, 
1338a 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1338b 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 6d 6f 76 65  location to move
1338c 20 70 44 62 50 61 67 65 20 74 6f 20 2a 2f 0a 20   pDbPage to */. 
1338d 20 69 6e 74 20 69 73 43 6f 6d 6d 69 74 0a 29 7b   int isCommit.){
1338e 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 74 72  .  MemPage *pPtr
1338f 50 61 67 65 3b 20 20 20 2f 2a 20 54 68 65 20 70  Page;   /* The p
13390 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  age that contain
13391 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70  s a pointer to p
13392 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f  DbPage */.  Pgno
13393 20 69 44 62 50 61 67 65 20 3d 20 70 44 62 50 61   iDbPage = pDbPa
13394 67 65 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65  ge->pgno;.  Page
13395 72 20 2a 70 50 61 67 65 72 20 3d 20 70 42 74 2d  r *pPager = pBt-
13396 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 72  >pPager;.  int r
13397 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 65 54  c;..  assert( eT
13398 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52  ype==PTRMAP_OVER
13399 46 4c 4f 57 32 20 7c 7c 20 65 54 79 70 65 3d 3d  FLOW2 || eType==
1339a 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31  PTRMAP_OVERFLOW1
1339b 20 7c 7c 20 0a 20 20 20 20 20 20 65 54 79 70 65   || .      eType
1339c 3d 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20 7c  ==PTRMAP_BTREE |
1339d 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f  | eType==PTRMAP_
1339e 52 4f 4f 54 50 41 47 45 20 29 3b 0a 20 20 61 73  ROOTPAGE );.  as
1339f 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
133a0 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
133a1 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
133a2 28 20 70 44 62 50 61 67 65 2d 3e 70 42 74 3d 3d  ( pDbPage->pBt==
133a3 70 42 74 20 29 3b 0a 0a 20 20 2f 2a 20 4d 6f 76  pBt );..  /* Mov
133a4 65 20 70 61 67 65 20 69 44 62 50 61 67 65 20 66  e page iDbPage f
133a5 72 6f 6d 20 69 74 73 20 63 75 72 72 65 6e 74 20  rom its current 
133a6 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 70 61 67 65  location to page
133a7 20 6e 75 6d 62 65 72 20 69 46 72 65 65 50 61 67   number iFreePag
133a8 65 20 2a 2f 0a 20 20 54 52 41 43 45 28 28 22 41  e */.  TRACE(("A
133a9 55 54 4f 56 41 43 55 55 4d 3a 20 4d 6f 76 69 6e  UTOVACUUM: Movin
133aa 67 20 25 64 20 74 6f 20 66 72 65 65 20 70 61 67  g %d to free pag
133ab 65 20 25 64 20 28 70 74 72 20 70 61 67 65 20 25  e %d (ptr page %
133ac 64 20 74 79 70 65 20 25 64 29 5c 6e 22 2c 20 0a  d type %d)\n", .
133ad 20 20 20 20 20 20 69 44 62 50 61 67 65 2c 20 69        iDbPage, i
133ae 46 72 65 65 50 61 67 65 2c 20 69 50 74 72 50 61  FreePage, iPtrPa
133af 67 65 2c 20 65 54 79 70 65 29 29 3b 0a 20 20 72  ge, eType));.  r
133b0 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
133b1 4d 6f 76 65 70 61 67 65 28 70 50 61 67 65 72 2c  Movepage(pPager,
133b2 20 70 44 62 50 61 67 65 2d 3e 70 44 62 50 61 67   pDbPage->pDbPag
133b3 65 2c 20 69 46 72 65 65 50 61 67 65 2c 20 69 73  e, iFreePage, is
133b4 43 6f 6d 6d 69 74 29 3b 0a 20 20 69 66 28 20 72  Commit);.  if( r
133b5 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
133b6 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
133b7 20 7d 0a 20 20 70 44 62 50 61 67 65 2d 3e 70 67   }.  pDbPage->pg
133b8 6e 6f 20 3d 20 69 46 72 65 65 50 61 67 65 3b 0a  no = iFreePage;.
133b9 0a 20 20 2f 2a 20 49 66 20 70 44 62 50 61 67 65  .  /* If pDbPage
133ba 20 77 61 73 20 61 20 62 74 72 65 65 2d 70 61 67   was a btree-pag
133bb 65 2c 20 74 68 65 6e 20 69 74 20 6d 61 79 20 68  e, then it may h
133bc 61 76 65 20 63 68 69 6c 64 20 70 61 67 65 73 20  ave child pages 
133bd 61 6e 64 2f 6f 72 20 63 65 6c 6c 73 0a 20 20 2a  and/or cells.  *
133be 2a 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20  * that point to 
133bf 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20  overflow pages. 
133c0 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20  The pointer map 
133c1 65 6e 74 72 69 65 73 20 66 6f 72 20 61 6c 6c 20  entries for all 
133c2 74 68 65 73 65 0a 20 20 2a 2a 20 70 61 67 65 73  these.  ** pages
133c3 20 6e 65 65 64 20 74 6f 20 62 65 20 63 68 61 6e   need to be chan
133c4 67 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  ged..  **.  ** I
133c5 66 20 70 44 62 50 61 67 65 20 69 73 20 61 6e 20  f pDbPage is an 
133c6 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 74  overflow page, t
133c7 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 34 20  hen the first 4 
133c8 62 79 74 65 73 20 6d 61 79 20 73 74 6f 72 65 20  bytes may store 
133c9 61 0a 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20 74  a.  ** pointer t
133ca 6f 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 6f  o a subsequent o
133cb 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49 66  verflow page. If
133cc 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61 73   this is the cas
133cd 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 74 68 65  e, then.  ** the
133ce 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 6e 65 65   pointer map nee
133cf 64 73 20 74 6f 20 62 65 20 75 70 64 61 74 65 64  ds to be updated
133d0 20 66 6f 72 20 74 68 65 20 73 75 62 73 65 71 75   for the subsequ
133d1 65 6e 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  ent overflow pag
133d2 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 54  e..  */.  if( eT
133d3 79 70 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52 45  ype==PTRMAP_BTRE
133d4 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d  E || eType==PTRM
133d5 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20  AP_ROOTPAGE ){. 
133d6 20 20 20 72 63 20 3d 20 73 65 74 43 68 69 6c 64     rc = setChild
133d7 50 74 72 6d 61 70 73 28 70 44 62 50 61 67 65 29  Ptrmaps(pDbPage)
133d8 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
133d9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
133da 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
133db 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 50  }.  }else{.    P
133dc 67 6e 6f 20 6e 65 78 74 4f 76 66 6c 20 3d 20 67  gno nextOvfl = g
133dd 65 74 34 62 79 74 65 28 70 44 62 50 61 67 65 2d  et4byte(pDbPage-
133de 3e 61 44 61 74 61 29 3b 0a 20 20 20 20 69 66 28  >aData);.    if(
133df 20 6e 65 78 74 4f 76 66 6c 21 3d 30 20 29 7b 0a   nextOvfl!=0 ){.
133e0 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61        rc = ptrma
133e1 70 50 75 74 28 70 42 74 2c 20 6e 65 78 74 4f 76  pPut(pBt, nextOv
133e2 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46  fl, PTRMAP_OVERF
133e3 4c 4f 57 32 2c 20 69 46 72 65 65 50 61 67 65 29  LOW2, iFreePage)
133e4 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
133e5 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
133e6 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
133e7 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
133e8 7d 0a 0a 20 20 2f 2a 20 46 69 78 20 74 68 65 20  }..  /* Fix the 
133e9 64 61 74 61 62 61 73 65 20 70 6f 69 6e 74 65 72  database pointer
133ea 20 6f 6e 20 70 61 67 65 20 69 50 74 72 50 61 67   on page iPtrPag
133eb 65 20 74 68 61 74 20 70 6f 69 6e 74 65 64 20 61  e that pointed a
133ec 74 20 69 44 62 50 61 67 65 20 73 6f 0a 20 20 2a  t iDbPage so.  *
133ed 2a 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73  * that it points
133ee 20 61 74 20 69 46 72 65 65 50 61 67 65 2e 20 41   at iFreePage. A
133ef 6c 73 6f 20 66 69 78 20 74 68 65 20 70 6f 69 6e  lso fix the poin
133f0 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20 66 6f  ter map entry fo
133f1 72 0a 20 20 2a 2a 20 69 50 74 72 50 61 67 65 2e  r.  ** iPtrPage.
133f2 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 54 79 70  .  */.  if( eTyp
133f3 65 21 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41  e!=PTRMAP_ROOTPA
133f4 47 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  GE ){.    rc = s
133f5 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61  qlite3BtreeGetPa
133f6 67 65 28 70 42 74 2c 20 69 50 74 72 50 61 67 65  ge(pBt, iPtrPage
133f7 2c 20 26 70 50 74 72 50 61 67 65 2c 20 30 29 3b  , &pPtrPage, 0);
133f8 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
133f9 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
133fa 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
133fb 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
133fc 33 50 61 67 65 72 57 72 69 74 65 28 70 50 74 72  3PagerWrite(pPtr
133fd 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a  Page->pDbPage);.
133fe 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
133ff 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
13400 65 6c 65 61 73 65 50 61 67 65 28 70 50 74 72 50  eleasePage(pPtrP
13401 61 67 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75  age);.      retu
13402 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
13403 20 72 63 20 3d 20 6d 6f 64 69 66 79 50 61 67 65   rc = modifyPage
13404 50 6f 69 6e 74 65 72 28 70 50 74 72 50 61 67 65  Pointer(pPtrPage
13405 2c 20 69 44 62 50 61 67 65 2c 20 69 46 72 65 65  , iDbPage, iFree
13406 50 61 67 65 2c 20 65 54 79 70 65 29 3b 0a 20 20  Page, eType);.  
13407 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
13408 74 72 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28  trPage);.    if(
13409 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1340a 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72  {.      rc = ptr
1340b 6d 61 70 50 75 74 28 70 42 74 2c 20 69 46 72 65  mapPut(pBt, iFre
1340c 65 50 61 67 65 2c 20 65 54 79 70 65 2c 20 69 50  ePage, eType, iP
1340d 74 72 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20  trPage);.    }. 
1340e 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
1340f 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65  }../* Forward de
13410 63 6c 61 72 61 74 69 6f 6e 20 72 65 71 75 69 72  claration requir
13411 65 64 20 62 79 20 69 6e 63 72 56 61 63 75 75 6d  ed by incrVacuum
13412 53 74 65 70 28 29 2e 20 2a 2f 0a 73 74 61 74 69  Step(). */.stati
13413 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 42 74  c int allocateBt
13414 72 65 65 50 61 67 65 28 42 74 53 68 61 72 65 64  reePage(BtShared
13415 20 2a 2c 20 4d 65 6d 50 61 67 65 20 2a 2a 2c 20   *, MemPage **, 
13416 50 67 6e 6f 20 2a 2c 20 50 67 6e 6f 2c 20 75 38  Pgno *, Pgno, u8
13417 29 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72  );../*.** Perfor
13418 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70 20  m a single step 
13419 6f 66 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61  of an incrementa
1341a 6c 2d 76 61 63 75 75 6d 2e 20 49 66 20 73 75 63  l-vacuum. If suc
1341b 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 72 65 74 75  cessful,.** retu
1341c 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66  rn SQLITE_OK. If
1341d 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 6f 72   there is no wor
1341e 6b 20 74 6f 20 64 6f 20 28 61 6e 64 20 74 68 65  k to do (and the
1341f 72 65 66 6f 72 65 20 6e 6f 0a 2a 2a 20 70 6f 69  refore no.** poi
13420 6e 74 20 69 6e 20 63 61 6c 6c 69 6e 67 20 74 68  nt in calling th
13421 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 67 61 69  is function agai
13422 6e 29 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  n), return SQLIT
13423 45 5f 44 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20 4d 6f  E_DONE..**.** Mo
13424 72 65 20 73 70 65 63 69 66 69 63 6c 79 2c 20 74  re specificly, t
13425 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 74  his function att
13426 65 6d 70 74 73 20 74 6f 20 72 65 2d 6f 72 67 61  empts to re-orga
13427 6e 69 7a 65 20 74 68 65 20 0a 2a 2a 20 64 61 74  nize the .** dat
13428 61 62 61 73 65 20 73 6f 20 74 68 61 74 20 74 68  abase so that th
13429 65 20 6c 61 73 74 20 70 61 67 65 20 6f 66 20 74  e last page of t
1342a 68 65 20 66 69 6c 65 20 63 75 72 72 65 6e 74 6c  he file currentl
1342b 79 20 69 6e 20 75 73 65 0a 2a 2a 20 69 73 20 6e  y in use.** is n
1342c 6f 20 6c 6f 6e 67 65 72 20 69 6e 20 75 73 65 2e  o longer in use.
1342d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 46  .**.** If the nF
1342e 69 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  in parameter is 
1342f 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 69 6d  non-zero, the im
13430 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73  plementation ass
13431 75 6d 65 73 0a 2a 2a 20 74 68 61 74 20 74 68 65  umes.** that the
13432 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6b 65 65   caller will kee
13433 70 20 63 61 6c 6c 69 6e 67 20 69 6e 63 72 56 61  p calling incrVa
13434 63 75 75 6d 53 74 65 70 28 29 20 75 6e 74 69 6c  cuumStep() until
13435 0a 2a 2a 20 69 74 20 72 65 74 75 72 6e 73 20 53  .** it returns S
13436 51 4c 49 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e  QLITE_DONE or an
13437 20 65 72 72 6f 72 2c 20 61 6e 64 20 74 68 61 74   error, and that
13438 20 6e 46 69 6e 20 69 73 20 74 68 65 0a 2a 2a 20   nFin is the.** 
13439 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
1343a 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1343b 65 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 61  e will contain a
1343c 66 74 65 72 20 74 68 69 73 20 0a 2a 2a 20 70 72  fter this .** pr
1343d 6f 63 65 73 73 20 69 73 20 63 6f 6d 70 6c 65 74  ocess is complet
1343e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1343f 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28   incrVacuumStep(
13440 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50  BtShared *pBt, P
13441 67 6e 6f 20 6e 46 69 6e 29 7b 0a 20 20 50 67 6e  gno nFin){.  Pgn
13442 6f 20 69 4c 61 73 74 50 67 3b 20 20 20 20 20 20  o iLastPg;      
13443 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 70         /* Last p
13444 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62  age in the datab
13445 61 73 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 46  ase */.  Pgno nF
13446 72 65 65 4c 69 73 74 3b 20 20 20 20 20 20 20 20  reeList;        
13447 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
13448 70 61 67 65 73 20 73 74 69 6c 6c 20 6f 6e 20 74  pages still on t
13449 68 65 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a  he free-list */.
1344a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1344b 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42  e3_mutex_held(pB
1344c 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69  t->mutex) );.  i
1344d 4c 61 73 74 50 67 20 3d 20 70 42 74 2d 3e 6e 54  LastPg = pBt->nT
1344e 72 75 6e 63 3b 0a 20 20 69 66 28 20 69 4c 61 73  runc;.  if( iLas
1344f 74 50 67 3d 3d 30 20 29 7b 0a 20 20 20 20 69 4c  tPg==0 ){.    iL
13450 61 73 74 50 67 20 3d 20 70 61 67 65 72 50 61 67  astPg = pagerPag
13451 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67  ecount(pBt->pPag
13452 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  er);.  }..  if( 
13453 21 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70  !PTRMAP_ISPAGE(p
13454 42 74 2c 20 69 4c 61 73 74 50 67 29 20 26 26 20  Bt, iLastPg) && 
13455 69 4c 61 73 74 50 67 21 3d 50 45 4e 44 49 4e 47  iLastPg!=PENDING
13456 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20  _BYTE_PAGE(pBt) 
13457 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20  ){.    int rc;. 
13458 20 20 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20     u8 eType;.   
13459 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65 3b 0a   Pgno iPtrPage;.
1345a 0a 20 20 20 20 6e 46 72 65 65 4c 69 73 74 20 3d  .    nFreeList =
1345b 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e   get4byte(&pBt->
1345c 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36  pPage1->aData[36
1345d 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e 46 72 65  ]);.    if( nFre
1345e 65 4c 69 73 74 3d 3d 30 20 7c 7c 20 6e 46 69 6e  eList==0 || nFin
1345f 3d 3d 69 4c 61 73 74 50 67 20 29 7b 0a 20 20 20  ==iLastPg ){.   
13460 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
13461 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 20  _DONE;.    }..  
13462 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74    rc = ptrmapGet
13463 28 70 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26  (pBt, iLastPg, &
13464 65 54 79 70 65 2c 20 26 69 50 74 72 50 61 67 65  eType, &iPtrPage
13465 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
13466 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
13467 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
13468 20 7d 0a 20 20 20 20 69 66 28 20 65 54 79 70 65   }.    if( eType
13469 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47  ==PTRMAP_ROOTPAG
1346a 45 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  E ){.      retur
1346b 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
1346c 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a 20 20  _BKPT;.    }..  
1346d 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52    if( eType==PTR
1346e 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 7b 0a  MAP_FREEPAGE ){.
1346f 20 20 20 20 20 20 69 66 28 20 6e 46 69 6e 3d 3d        if( nFin==
13470 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  0 ){.        /* 
13471 52 65 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20  Remove the page 
13472 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 73 20 66  from the files f
13473 72 65 65 2d 6c 69 73 74 2e 20 54 68 69 73 20 69  ree-list. This i
13474 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 0a 20  s not required. 
13475 20 20 20 20 20 20 20 2a 2a 20 69 66 20 6e 46 69         ** if nFi
13476 6e 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 49  n is non-zero. I
13477 6e 20 74 68 61 74 20 63 61 73 65 2c 20 74 68 65  n that case, the
13478 20 66 72 65 65 2d 6c 69 73 74 20 77 69 6c 6c 20   free-list will 
13479 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72  be.        ** tr
1347a 75 6e 63 61 74 65 64 20 74 6f 20 7a 65 72 6f 20  uncated to zero 
1347b 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74  after this funct
1347c 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 73 6f 20  ion returns, so 
1347d 69 74 20 64 6f 65 73 6e 27 74 20 0a 20 20 20 20  it doesn't .    
1347e 20 20 20 20 2a 2a 20 6d 61 74 74 65 72 20 69 66      ** matter if
1347f 20 69 74 20 73 74 69 6c 6c 20 63 6f 6e 74 61 69   it still contai
13480 6e 73 20 73 6f 6d 65 20 67 61 72 62 61 67 65 20  ns some garbage 
13481 65 6e 74 72 69 65 73 2e 0a 20 20 20 20 20 20 20  entries..       
13482 20 2a 2f 0a 20 20 20 20 20 20 20 20 50 67 6e 6f   */.        Pgno
13483 20 69 46 72 65 65 50 67 3b 0a 20 20 20 20 20 20   iFreePg;.      
13484 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 72 65 65    MemPage *pFree
13485 50 67 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  Pg;.        rc =
13486 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61   allocateBtreePa
13487 67 65 28 70 42 74 2c 20 26 70 46 72 65 65 50 67  ge(pBt, &pFreePg
13488 2c 20 26 69 46 72 65 65 50 67 2c 20 69 4c 61 73  , &iFreePg, iLas
13489 74 50 67 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  tPg, 1);.       
1348a 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1348b 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
1348c 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
1348d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73     }.        ass
1348e 65 72 74 28 20 69 46 72 65 65 50 67 3d 3d 69 4c  ert( iFreePg==iL
1348f 61 73 74 50 67 20 29 3b 0a 20 20 20 20 20 20 20  astPg );.       
13490 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 46 72   releasePage(pFr
13491 65 65 50 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20  eePg);.      }. 
13492 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20     } else {.    
13493 20 20 50 67 6e 6f 20 69 46 72 65 65 50 67 3b 20    Pgno iFreePg; 
13494 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
13495 6e 64 65 78 20 6f 66 20 66 72 65 65 20 70 61 67  ndex of free pag
13496 65 20 74 6f 20 6d 6f 76 65 20 70 4c 61 73 74 50  e to move pLastP
13497 67 20 74 6f 20 2a 2f 0a 20 20 20 20 20 20 4d 65  g to */.      Me
13498 6d 50 61 67 65 20 2a 70 4c 61 73 74 50 67 3b 0a  mPage *pLastPg;.
13499 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1349a 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28  te3BtreeGetPage(
1349b 70 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 70  pBt, iLastPg, &p
1349c 4c 61 73 74 50 67 2c 20 30 29 3b 0a 20 20 20 20  LastPg, 0);.    
1349d 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1349e 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
1349f 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
134a0 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 6e  }..      /* If n
134a1 46 69 6e 20 69 73 20 7a 65 72 6f 2c 20 74 68 69  Fin is zero, thi
134a2 73 20 6c 6f 6f 70 20 72 75 6e 73 20 65 78 61 63  s loop runs exac
134a3 74 6c 79 20 6f 6e 63 65 20 61 6e 64 20 70 61 67  tly once and pag
134a4 65 20 70 4c 61 73 74 50 67 0a 20 20 20 20 20 20  e pLastPg.      
134a5 2a 2a 20 69 73 20 73 77 61 70 70 65 64 20 77 69  ** is swapped wi
134a6 74 68 20 74 68 65 20 66 69 72 73 74 20 66 72 65  th the first fre
134a7 65 20 70 61 67 65 20 70 75 6c 6c 65 64 20 6f 66  e page pulled of
134a8 66 20 74 68 65 20 66 72 65 65 20 6c 69 73 74 2e  f the free list.
134a9 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
134aa 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20  ** On the other 
134ab 68 61 6e 64 2c 20 69 66 20 6e 46 69 6e 20 69 73  hand, if nFin is
134ac 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65   greater than ze
134ad 72 6f 2c 20 74 68 65 6e 20 6b 65 65 70 0a 20 20  ro, then keep.  
134ae 20 20 20 20 2a 2a 20 6c 6f 6f 70 69 6e 67 20 75      ** looping u
134af 6e 74 69 6c 20 61 20 66 72 65 65 2d 70 61 67 65  ntil a free-page
134b0 20 6c 6f 63 61 74 65 64 20 77 69 74 68 69 6e 20   located within 
134b1 74 68 65 20 66 69 72 73 74 20 6e 46 69 6e 20 70  the first nFin p
134b2 61 67 65 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66  ages.      ** of
134b3 20 74 68 65 20 66 69 6c 65 20 69 73 20 66 6f 75   the file is fou
134b4 6e 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  nd..      */.   
134b5 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20     do {.        
134b6 4d 65 6d 50 61 67 65 20 2a 70 46 72 65 65 50 67  MemPage *pFreePg
134b7 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 61  ;.        rc = a
134b8 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65  llocateBtreePage
134b9 28 70 42 74 2c 20 26 70 46 72 65 65 50 67 2c 20  (pBt, &pFreePg, 
134ba 26 69 46 72 65 65 50 67 2c 20 30 2c 20 30 29 3b  &iFreePg, 0, 0);
134bb 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
134bc 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
134bd 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50          releaseP
134be 61 67 65 28 70 4c 61 73 74 50 67 29 3b 0a 20 20  age(pLastPg);.  
134bf 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
134c0 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  c;.        }.   
134c1 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
134c2 28 70 46 72 65 65 50 67 29 3b 0a 20 20 20 20 20  (pFreePg);.     
134c3 20 7d 77 68 69 6c 65 28 20 6e 46 69 6e 21 3d 30   }while( nFin!=0
134c4 20 26 26 20 69 46 72 65 65 50 67 3e 6e 46 69 6e   && iFreePg>nFin
134c5 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
134c6 28 20 69 46 72 65 65 50 67 3c 69 4c 61 73 74 50  ( iFreePg<iLastP
134c7 67 20 29 3b 0a 20 20 20 20 20 20 0a 20 20 20 20  g );.      .    
134c8 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
134c9 67 65 72 57 72 69 74 65 28 70 4c 61 73 74 50 67  gerWrite(pLastPg
134ca 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
134cb 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
134cc 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
134cd 63 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65  c = relocatePage
134ce 28 70 42 74 2c 20 70 4c 61 73 74 50 67 2c 20 65  (pBt, pLastPg, e
134cf 54 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20  Type, iPtrPage, 
134d0 69 46 72 65 65 50 67 2c 20 6e 46 69 6e 21 3d 30  iFreePg, nFin!=0
134d1 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
134d2 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4c 61   releasePage(pLa
134d3 73 74 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28  stPg);.      if(
134d4 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
134d5 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
134d6 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   rc;.      }.   
134d7 20 7d 0a 20 20 7d 0a 0a 20 20 70 42 74 2d 3e 6e   }.  }..  pBt->n
134d8 54 72 75 6e 63 20 3d 20 69 4c 61 73 74 50 67 20  Trunc = iLastPg 
134d9 2d 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 70 42  - 1;.  while( pB
134da 74 2d 3e 6e 54 72 75 6e 63 3d 3d 50 45 4e 44 49  t->nTrunc==PENDI
134db 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74  NG_BYTE_PAGE(pBt
134dc 29 7c 7c 50 54 52 4d 41 50 5f 49 53 50 41 47 45  )||PTRMAP_ISPAGE
134dd 28 70 42 74 2c 20 70 42 74 2d 3e 6e 54 72 75 6e  (pBt, pBt->nTrun
134de 63 29 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e 6e  c) ){.    pBt->n
134df 54 72 75 6e 63 2d 2d 3b 0a 20 20 7d 0a 20 20 72  Trunc--;.  }.  r
134e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
134e1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 77 72 69 74  .}../*.** A writ
134e2 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75  e-transaction mu
134e3 73 74 20 62 65 20 6f 70 65 6e 65 64 20 62 65 66  st be opened bef
134e4 6f 72 65 20 63 61 6c 6c 69 6e 67 20 74 68 69 73  ore calling this
134e5 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 49 74   function..** It
134e6 20 70 65 72 66 6f 72 6d 73 20 61 20 73 69 6e 67   performs a sing
134e7 6c 65 20 75 6e 69 74 20 6f 66 20 77 6f 72 6b 20  le unit of work 
134e8 74 6f 77 61 72 64 73 20 61 6e 20 69 6e 63 72 65  towards an incre
134e9 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 2e 0a 2a  mental vacuum..*
134ea 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 63 72  *.** If the incr
134eb 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 69  emental vacuum i
134ec 73 20 66 69 6e 69 73 68 65 64 20 61 66 74 65 72  s finished after
134ed 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 68   this function h
134ee 61 73 20 72 75 6e 2c 0a 2a 2a 20 53 51 4c 49 54  as run,.** SQLIT
134ef 45 5f 44 4f 4e 45 20 69 73 20 72 65 74 75 72 6e  E_DONE is return
134f0 65 64 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74  ed. If it is not
134f1 20 66 69 6e 69 73 68 65 64 2c 20 62 75 74 20 6e   finished, but n
134f2 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 2c  o error occured,
134f3 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  .** SQLITE_OK is
134f4 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72   returned. Other
134f5 77 69 73 65 20 61 6e 20 53 51 4c 69 74 65 20 65  wise an SQLite e
134f6 72 72 6f 72 20 63 6f 64 65 2e 20 0a 2a 2f 0a 53  rror code. .*/.S
134f7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
134f8 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e  t sqlite3BtreeIn
134f9 63 72 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a  crVacuum(Btree *
134fa 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  p){.  int rc;.  
134fb 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
134fc 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74  p->pBt;..  sqlit
134fd 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
134fe 0a 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e  .  pBt->db = p->
134ff 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42  db;.  assert( pB
13500 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
13501 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 26 26  ==TRANS_WRITE &&
13502 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41   p->inTrans==TRA
13503 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 69 66  NS_WRITE );.  if
13504 28 20 21 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  ( !pBt->autoVacu
13505 75 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  um ){.    rc = S
13506 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65  QLITE_DONE;.  }e
13507 6c 73 65 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64  lse{.    invalid
13508 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61  ateAllOverflowCa
13509 63 68 65 28 70 42 74 29 3b 0a 20 20 20 20 72 63  che(pBt);.    rc
1350a 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65   = incrVacuumSte
1350b 70 28 70 42 74 2c 20 30 29 3b 0a 20 20 7d 0a 20  p(pBt, 0);.  }. 
1350c 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
1350d 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ve(p);.  return 
1350e 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rc;.}../*.** Thi
1350f 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
13510 6c 65 64 20 70 72 69 6f 72 20 74 6f 20 73 71 6c  led prior to sql
13511 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 20  ite3PagerCommit 
13512 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74 69  when a transacti
13513 6f 6e 0a 2a 2a 20 69 73 20 63 6f 6d 6d 69 74 65  on.** is commite
13514 64 20 66 6f 72 20 61 6e 20 61 75 74 6f 2d 76 61  d for an auto-va
13515 63 75 75 6d 20 64 61 74 61 62 61 73 65 2e 0a 2a  cuum database..*
13516 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f  *.** If SQLITE_O
13517 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74  K is returned, t
13518 68 65 6e 20 2a 70 6e 54 72 75 6e 63 20 69 73 20  hen *pnTrunc is 
13519 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65  set to the numbe
1351a 72 20 6f 66 20 70 61 67 65 73 0a 2a 2a 20 74 68  r of pages.** th
1351b 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
1351c 73 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63 61  should be trunca
1351d 74 65 64 20 74 6f 20 64 75 72 69 6e 67 20 74 68  ted to during th
1351e 65 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 73 73  e commit process
1351f 2e 20 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 20 64  . .** i.e. the d
13520 61 74 61 62 61 73 65 20 68 61 73 20 62 65 65 6e  atabase has been
13521 20 72 65 6f 72 67 61 6e 69 7a 65 64 20 73 6f 20   reorganized so 
13522 74 68 61 74 20 6f 6e 6c 79 20 74 68 65 20 66 69  that only the fi
13523 72 73 74 20 2a 70 6e 54 72 75 6e 63 0a 2a 2a 20  rst *pnTrunc.** 
13524 70 61 67 65 73 20 61 72 65 20 69 6e 20 75 73 65  pages are in use
13525 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
13526 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74  autoVacuumCommit
13527 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20  (BtShared *pBt, 
13528 50 67 6e 6f 20 2a 70 6e 54 72 75 6e 63 29 7b 0a  Pgno *pnTrunc){.
13529 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1352a 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20 2a 70  E_OK;.  Pager *p
1352b 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61  Pager = pBt->pPa
1352c 67 65 72 3b 0a 20 20 56 56 41 5f 4f 4e 4c 59 28  ger;.  VVA_ONLY(
1352d 20 69 6e 74 20 6e 52 65 66 20 3d 20 73 71 6c 69   int nRef = sqli
1352e 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74  te3PagerRefcount
1352f 28 70 50 61 67 65 72 29 20 29 3b 0a 0a 20 20 61  (pPager) );..  a
13530 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
13531 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d  utex_held(pBt->m
13532 75 74 65 78 29 20 29 3b 0a 20 20 69 6e 76 61 6c  utex) );.  inval
13533 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77  idateAllOverflow
13534 43 61 63 68 65 28 70 42 74 29 3b 0a 20 20 61 73  Cache(pBt);.  as
13535 73 65 72 74 28 70 42 74 2d 3e 61 75 74 6f 56 61  sert(pBt->autoVa
13536 63 75 75 6d 29 3b 0a 20 20 69 66 28 20 21 70 42  cuum);.  if( !pB
13537 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 29 7b  t->incrVacuum ){
13538 0a 20 20 20 20 50 67 6e 6f 20 6e 46 69 6e 20 3d  .    Pgno nFin =
13539 20 30 3b 0a 0a 20 20 20 20 69 66 28 20 70 42 74   0;..    if( pBt
1353a 2d 3e 6e 54 72 75 6e 63 3d 3d 30 20 29 7b 0a 20  ->nTrunc==0 ){. 
1353b 20 20 20 20 20 50 67 6e 6f 20 6e 46 72 65 65 3b       Pgno nFree;
1353c 0a 20 20 20 20 20 20 50 67 6e 6f 20 6e 50 74 72  .      Pgno nPtr
1353d 6d 61 70 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74  map;.      const
1353e 20 69 6e 74 20 70 67 73 7a 20 3d 20 70 42 74 2d   int pgsz = pBt-
1353f 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20  >pageSize;.     
13540 20 69 6e 74 20 6e 4f 72 69 67 20 3d 20 70 61 67   int nOrig = pag
13541 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d  erPagecount(pBt-
13542 3e 70 50 61 67 65 72 29 3b 0a 0a 20 20 20 20 20  >pPager);..     
13543 20 69 66 28 20 50 54 52 4d 41 50 5f 49 53 50 41   if( PTRMAP_ISPA
13544 47 45 28 70 42 74 2c 20 6e 4f 72 69 67 29 20 29  GE(pBt, nOrig) )
13545 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
13546 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
13547 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20  BKPT;.      }.  
13548 20 20 20 20 69 66 28 20 6e 4f 72 69 67 3d 3d 50      if( nOrig==P
13549 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
1354a 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20  (pBt) ){.       
1354b 20 6e 4f 72 69 67 2d 2d 3b 0a 20 20 20 20 20 20   nOrig--;.      
1354c 7d 0a 20 20 20 20 20 20 6e 46 72 65 65 20 3d 20  }.      nFree = 
1354d 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70  get4byte(&pBt->p
1354e 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d  Page1->aData[36]
1354f 29 3b 0a 20 20 20 20 20 20 6e 50 74 72 6d 61 70  );.      nPtrmap
13550 20 3d 20 28 6e 46 72 65 65 2d 6e 4f 72 69 67 2b   = (nFree-nOrig+
13551 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42  PTRMAP_PAGENO(pB
13552 74 2c 20 6e 4f 72 69 67 29 2b 70 67 73 7a 2f 35  t, nOrig)+pgsz/5
13553 29 2f 28 70 67 73 7a 2f 35 29 3b 0a 20 20 20 20  )/(pgsz/5);.    
13554 20 20 6e 46 69 6e 20 3d 20 6e 4f 72 69 67 20 2d    nFin = nOrig -
13555 20 6e 46 72 65 65 20 2d 20 6e 50 74 72 6d 61 70   nFree - nPtrmap
13556 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 4f 72 69  ;.      if( nOri
13557 67 3e 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  g>PENDING_BYTE_P
13558 41 47 45 28 70 42 74 29 20 26 26 20 6e 46 69 6e  AGE(pBt) && nFin
13559 3c 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  <=PENDING_BYTE_P
1355a 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20  AGE(pBt) ){.    
1355b 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 20 20      nFin--;.    
1355c 20 20 7d 0a 20 20 20 20 20 20 77 68 69 6c 65 28    }.      while(
1355d 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70   PTRMAP_ISPAGE(p
1355e 42 74 2c 20 6e 46 69 6e 29 20 7c 7c 20 6e 46 69  Bt, nFin) || nFi
1355f 6e 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  n==PENDING_BYTE_
13560 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20  PAGE(pBt) ){.   
13561 20 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 20       nFin--;.   
13562 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
13563 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54  while( rc==SQLIT
13564 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63  E_OK ){.      rc
13565 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65   = incrVacuumSte
13566 70 28 70 42 74 2c 20 6e 46 69 6e 29 3b 0a 20 20  p(pBt, nFin);.  
13567 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d    }.    if( rc==
13568 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20  SQLITE_DONE ){. 
13569 20 20 20 20 20 61 73 73 65 72 74 28 6e 46 69 6e       assert(nFin
1356a 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 6e 54 72 75  ==0 || pBt->nTru
1356b 6e 63 3d 3d 30 20 7c 7c 20 6e 46 69 6e 3c 3d 70  nc==0 || nFin<=p
1356c 42 74 2d 3e 6e 54 72 75 6e 63 29 3b 0a 20 20 20  Bt->nTrunc);.   
1356d 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
1356e 4b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74  K;.      if( pBt
1356f 2d 3e 6e 54 72 75 6e 63 20 26 26 20 6e 46 69 6e  ->nTrunc && nFin
13570 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
13571 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
13572 74 65 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e  te(pBt->pPage1->
13573 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
13574 20 20 70 75 74 34 62 79 74 65 28 26 70 42 74 2d    put4byte(&pBt-
13575 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  >pPage1->aData[3
13576 32 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  2], 0);.        
13577 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70  put4byte(&pBt->p
13578 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d  Page1->aData[36]
13579 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 42  , 0);.        pB
1357a 74 2d 3e 6e 54 72 75 6e 63 20 3d 20 6e 46 69 6e  t->nTrunc = nFin
1357b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1357c 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1357d 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73  TE_OK ){.      s
1357e 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62  qlite3PagerRollb
1357f 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20  ack(pPager);.   
13580 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63   }.  }..  if( rc
13581 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
13582 20 20 20 2a 70 6e 54 72 75 6e 63 20 3d 20 70 42     *pnTrunc = pB
13583 74 2d 3e 6e 54 72 75 6e 63 3b 0a 20 20 20 20 70  t->nTrunc;.    p
13584 42 74 2d 3e 6e 54 72 75 6e 63 20 3d 20 30 3b 0a  Bt->nTrunc = 0;.
13585 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 52    }.  assert( nR
13586 65 66 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 72  ef==sqlite3Pager
13587 52 65 66 63 6f 75 6e 74 28 70 50 61 67 65 72 29  Refcount(pPager)
13588 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   );.  return rc;
13589 0a 7d 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  .}..#endif../*.*
1358a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64  * This routine d
1358b 6f 65 73 20 74 68 65 20 66 69 72 73 74 20 70 68  oes the first ph
1358c 61 73 65 20 6f 66 20 61 20 74 77 6f 2d 70 68 61  ase of a two-pha
1358d 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68 69 73  se commit.  This
1358e 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 63 61 75 73   routine.** caus
1358f 65 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f  es a rollback jo
13590 75 72 6e 61 6c 20 74 6f 20 62 65 20 63 72 65 61  urnal to be crea
13591 74 65 64 20 28 69 66 20 69 74 20 64 6f 65 73 20  ted (if it does 
13592 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73  not already exis
13593 74 29 0a 2a 2a 20 61 6e 64 20 70 6f 70 75 6c 61  t).** and popula
13594 74 65 64 20 77 69 74 68 20 65 6e 6f 75 67 68 20  ted with enough 
13595 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 6f 20 74  information so t
13596 68 61 74 20 69 66 20 61 20 70 6f 77 65 72 20 6c  hat if a power l
13597 6f 73 73 20 6f 63 63 75 72 73 0a 2a 2a 20 74 68  oss occurs.** th
13598 65 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 62  e database can b
13599 65 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69 74  e restored to it
1359a 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65  s original state
1359b 20 62 79 20 70 6c 61 79 69 6e 67 20 62 61 63 6b   by playing back
1359c 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e  .** the journal.
1359d 20 20 54 68 65 6e 20 74 68 65 20 63 6f 6e 74 65    Then the conte
1359e 6e 74 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  nts of the journ
1359f 61 6c 20 61 72 65 20 66 6c 75 73 68 65 64 20 6f  al are flushed o
135a0 75 74 20 74 6f 0a 2a 2a 20 74 68 65 20 64 69 73  ut to.** the dis
135a1 6b 2e 20 20 41 66 74 65 72 20 74 68 65 20 6a 6f  k.  After the jo
135a2 75 72 6e 61 6c 20 69 73 20 73 61 66 65 6c 79 20  urnal is safely 
135a3 6f 6e 20 6f 78 69 64 65 2c 20 74 68 65 20 63 68  on oxide, the ch
135a4 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20  anges to the.** 
135a5 64 61 74 61 62 61 73 65 20 61 72 65 20 77 72 69  database are wri
135a6 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 64 61  tten into the da
135a7 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20  tabase file and 
135a8 66 6c 75 73 68 65 64 20 74 6f 20 6f 78 69 64 65  flushed to oxide
135a9 2e 0a 2a 2a 20 41 74 20 74 68 65 20 65 6e 64 20  ..** At the end 
135aa 6f 66 20 74 68 69 73 20 63 61 6c 6c 2c 20 74 68  of this call, th
135ab 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  e rollback journ
135ac 61 6c 20 73 74 69 6c 6c 20 65 78 69 73 74 73 20  al still exists 
135ad 6f 6e 20 74 68 65 0a 2a 2a 20 64 69 73 6b 20 61  on the.** disk a
135ae 6e 64 20 77 65 20 61 72 65 20 73 74 69 6c 6c 20  nd we are still 
135af 68 6f 6c 64 69 6e 67 20 61 6c 6c 20 6c 6f 63 6b  holding all lock
135b0 73 2c 20 73 6f 20 74 68 65 20 74 72 61 6e 73 61  s, so the transa
135b1 63 74 69 6f 6e 20 68 61 73 20 6e 6f 74 0a 2a 2a  ction has not.**
135b2 20 63 6f 6d 6d 69 74 74 65 64 2e 20 20 53 65 65   committed.  See
135b3 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
135b4 6d 69 74 28 29 20 66 6f 72 20 74 68 65 20 73 65  mit() for the se
135b5 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20 74 68  cond phase of th
135b6 65 0a 2a 2a 20 63 6f 6d 6d 69 74 20 70 72 6f 63  e.** commit proc
135b7 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ess..**.** This 
135b8 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 20  call is a no-op 
135b9 69 66 20 6e 6f 20 77 72 69 74 65 2d 74 72 61 6e  if no write-tran
135ba 73 61 63 74 69 6f 6e 20 69 73 20 63 75 72 72 65  saction is curre
135bb 6e 74 6c 79 20 61 63 74 69 76 65 20 6f 6e 20 70  ntly active on p
135bc 42 74 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77  Bt..**.** Otherw
135bd 69 73 65 2c 20 73 79 6e 63 20 74 68 65 20 64 61  ise, sync the da
135be 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f 72 20  tabase file for 
135bf 74 68 65 20 62 74 72 65 65 20 70 42 74 2e 20 7a  the btree pBt. z
135c0 4d 61 73 74 65 72 20 70 6f 69 6e 74 73 20 74 6f  Master points to
135c1 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  .** the name of 
135c2 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  a master journal
135c3 20 66 69 6c 65 20 74 68 61 74 20 73 68 6f 75 6c   file that shoul
135c4 64 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74  d be written int
135c5 6f 20 74 68 65 0a 2a 2a 20 69 6e 64 69 76 69 64  o the.** individ
135c6 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ual journal file
135c7 2c 20 6f 72 20 69 73 20 4e 55 4c 4c 2c 20 69 6e  , or is NULL, in
135c8 64 69 63 61 74 69 6e 67 20 6e 6f 20 6d 61 73 74  dicating no mast
135c9 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
135ca 0a 2a 2a 20 28 73 69 6e 67 6c 65 20 64 61 74 61  .** (single data
135cb 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  base transaction
135cc 29 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68  )..**.** When th
135cd 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68  is is called, th
135ce 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
135cf 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20   should already 
135d0 68 61 76 65 20 62 65 65 6e 0a 2a 2a 20 63 72 65  have been.** cre
135d1 61 74 65 64 2c 20 70 6f 70 75 6c 61 74 65 64 20  ated, populated 
135d2 77 69 74 68 20 74 68 69 73 20 6a 6f 75 72 6e 61  with this journa
135d3 6c 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 73 79  l pointer and sy
135d4 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a  nced to disk..**
135d5 0a 2a 2a 20 4f 6e 63 65 20 74 68 69 73 20 69 73  .** Once this is
135d6 20 72 6f 75 74 69 6e 65 20 68 61 73 20 72 65 74   routine has ret
135d7 75 72 6e 65 64 2c 20 74 68 65 20 6f 6e 6c 79 20  urned, the only 
135d8 74 68 69 6e 67 20 72 65 71 75 69 72 65 64 20 74  thing required t
135d9 6f 20 63 6f 6d 6d 69 74 0a 2a 2a 20 74 68 65 20  o commit.** the 
135da 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
135db 6e 20 66 6f 72 20 74 68 69 73 20 64 61 74 61 62  n for this datab
135dc 61 73 65 20 66 69 6c 65 20 69 73 20 74 6f 20 64  ase file is to d
135dd 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61  elete the journa
135de 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  l..*/.SQLITE_PRI
135df 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
135e0 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
135e1 4f 6e 65 28 42 74 72 65 65 20 2a 70 2c 20 63 6f  One(Btree *p, co
135e2 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65  nst char *zMaste
135e3 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  r){.  int rc = S
135e4 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
135e5 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e  p->inTrans==TRAN
135e6 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 42  S_WRITE ){.    B
135e7 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
135e8 2d 3e 70 42 74 3b 0a 20 20 20 20 50 67 6e 6f 20  ->pBt;.    Pgno 
135e9 6e 54 72 75 6e 63 20 3d 20 30 3b 0a 20 20 20 20  nTrunc = 0;.    
135ea 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
135eb 72 28 70 29 3b 0a 20 20 20 20 70 42 74 2d 3e 64  r(p);.    pBt->d
135ec 62 20 3d 20 70 2d 3e 64 62 3b 0a 23 69 66 6e 64  b = p->db;.#ifnd
135ed 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
135ee 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66  UTOVACUUM.    if
135ef 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ( pBt->autoVacuu
135f0 6d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  m ){.      rc = 
135f1 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74  autoVacuumCommit
135f2 28 70 42 74 2c 20 26 6e 54 72 75 6e 63 29 3b 20  (pBt, &nTrunc); 
135f3 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
135f4 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
135f5 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
135f6 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 20 20  Leave(p);.      
135f7 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
135f8 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69     }.    }.#endi
135f9 66 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  f.    rc = sqlit
135fa 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61  e3PagerCommitPha
135fb 73 65 4f 6e 65 28 70 42 74 2d 3e 70 50 61 67 65  seOne(pBt->pPage
135fc 72 2c 20 7a 4d 61 73 74 65 72 2c 20 6e 54 72 75  r, zMaster, nTru
135fd 6e 63 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69  nc, 0);.    sqli
135fe 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
135ff 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
13600 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d  c;.}../*.** Comm
13601 69 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  it the transacti
13602 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20  on currently in 
13603 70 72 6f 67 72 65 73 73 2e 0a 2a 2a 0a 2a 2a 20  progress..**.** 
13604 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70  This routine imp
13605 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 65 63 6f  lements the seco
13606 6e 64 20 70 68 61 73 65 20 6f 66 20 61 20 32 2d  nd phase of a 2-
13607 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54  phase commit.  T
13608 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72  he.** sqlite3Btr
13609 65 65 53 79 6e 63 28 29 20 72 6f 75 74 69 6e 65  eeSync() routine
1360a 20 64 6f 65 73 20 74 68 65 20 66 69 72 73 74 20   does the first 
1360b 70 68 61 73 65 20 61 6e 64 20 73 68 6f 75 6c 64  phase and should
1360c 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 70   be invoked.** p
1360d 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20  rior to calling 
1360e 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 54  this routine.  T
1360f 68 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  he sqlite3BtreeS
13610 79 6e 63 28 29 20 72 6f 75 74 69 6e 65 20 64 69  ync() routine di
13611 64 0a 2a 2a 20 61 6c 6c 20 74 68 65 20 77 6f 72  d.** all the wor
13612 6b 20 6f 66 20 77 72 69 74 69 6e 67 20 69 6e 66  k of writing inf
13613 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20 74 6f 20  ormation out to 
13614 64 69 73 6b 20 61 6e 64 20 66 6c 75 73 68 69 6e  disk and flushin
13615 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74  g the.** content
13616 73 20 73 6f 20 74 68 61 74 20 74 68 65 79 20 61  s so that they a
13617 72 65 20 77 72 69 74 74 65 6e 20 6f 6e 74 6f 20  re written onto 
13618 74 68 65 20 64 69 73 6b 20 70 6c 61 74 74 65 72  the disk platter
13619 2e 20 20 41 6c 6c 20 74 68 69 73 0a 2a 2a 20 72  .  All this.** r
1361a 6f 75 74 69 6e 65 20 68 61 73 20 74 6f 20 64 6f  outine has to do
1361b 20 69 73 20 64 65 6c 65 74 65 20 6f 72 20 74 72   is delete or tr
1361c 75 6e 63 61 74 65 20 74 68 65 20 72 6f 6c 6c 62  uncate the rollb
1361d 61 63 6b 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 28  ack journal.** (
1361e 77 68 69 63 68 20 63 61 75 73 65 73 20 74 68 65  which causes the
1361f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20   transaction to 
13620 63 6f 6d 6d 69 74 29 20 61 6e 64 20 64 72 6f 70  commit) and drop
13621 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   locks..**.** Th
13622 69 73 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20  is will release 
13623 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 6f  the write lock o
13624 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
13625 69 6c 65 2e 20 20 49 66 20 74 68 65 72 65 0a 2a  ile.  If there.*
13626 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20  * are no active 
13627 63 75 72 73 6f 72 73 2c 20 69 74 20 61 6c 73 6f  cursors, it also
13628 20 72 65 6c 65 61 73 65 73 20 74 68 65 20 72 65   releases the re
13629 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49  ad lock..*/.SQLI
1362a 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1362b 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
1362c 74 50 68 61 73 65 54 77 6f 28 42 74 72 65 65 20  tPhaseTwo(Btree 
1362d 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  *p){.  BtShared 
1362e 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a  *pBt = p->pBt;..
1362f 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
13630 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64  ter(p);.  pBt->d
13631 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 62 74 72  b = p->db;.  btr
13632 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a  eeIntegrity(p);.
13633 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e  .  /* If the han
13634 64 6c 65 20 68 61 73 20 61 20 77 72 69 74 65 2d  dle has a write-
13635 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e  transaction open
13636 2c 20 63 6f 6d 6d 69 74 20 74 68 65 20 73 68 61  , commit the sha
13637 72 65 64 2d 62 74 72 65 65 73 20 0a 20 20 2a 2a  red-btrees .  **
13638 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64   transaction and
13639 20 73 65 74 20 74 68 65 20 73 68 61 72 65 64 20   set the shared 
1363a 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53 5f 52  state to TRANS_R
1363b 45 41 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  EAD..  */.  if( 
1363c 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e  p->inTrans==TRAN
1363d 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 69  S_WRITE ){.    i
1363e 6e 74 20 72 63 3b 0a 20 20 20 20 61 73 73 65 72  nt rc;.    asser
1363f 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61  t( pBt->inTransa
13640 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49  ction==TRANS_WRI
13641 54 45 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  TE );.    assert
13642 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74  ( pBt->nTransact
13643 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 72 63 20  ion>0 );.    rc 
13644 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f  = sqlite3PagerCo
13645 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 42 74  mmitPhaseTwo(pBt
13646 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69  ->pPager);.    i
13647 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
13648 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
13649 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
1364a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
1364b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e  .    }.    pBt->
1364c 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20  inTransaction = 
1364d 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 20 20 20  TRANS_READ;.    
1364e 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d 20 30 3b  pBt->inStmt = 0;
1364f 0a 20 20 7d 0a 20 20 75 6e 6c 6f 63 6b 41 6c 6c  .  }.  unlockAll
13650 54 61 62 6c 65 73 28 70 29 3b 0a 0a 20 20 2f 2a  Tables(p);..  /*
13651 20 49 66 20 74 68 65 20 68 61 6e 64 6c 65 20 68   If the handle h
13652 61 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 74  as any kind of t
13653 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c  ransaction open,
13654 20 64 65 63 72 65 6d 65 6e 74 20 74 68 65 20 74   decrement the t
13655 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20  ransaction.  ** 
13656 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 73 68 61  count of the sha
13657 72 65 64 20 62 74 72 65 65 2e 20 49 66 20 74 68  red btree. If th
13658 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f  e transaction co
13659 75 6e 74 20 72 65 61 63 68 65 73 20 30 2c 20 73  unt reaches 0, s
1365a 65 74 0a 20 20 2a 2a 20 74 68 65 20 73 68 61 72  et.  ** the shar
1365b 65 64 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e  ed state to TRAN
1365c 53 5f 4e 4f 4e 45 2e 20 54 68 65 20 75 6e 6c 6f  S_NONE. The unlo
1365d 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28  ckBtreeIfUnused(
1365e 29 20 63 61 6c 6c 20 62 65 6c 6f 77 0a 20 20 2a  ) call below.  *
1365f 2a 20 77 69 6c 6c 20 75 6e 6c 6f 63 6b 20 74 68  * will unlock th
13660 65 20 70 61 67 65 72 2e 0a 20 20 2a 2f 0a 20 20  e pager..  */.  
13661 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d  if( p->inTrans!=
13662 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20  TRANS_NONE ){.  
13663 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74    pBt->nTransact
13664 69 6f 6e 2d 2d 3b 0a 20 20 20 20 69 66 28 20 30  ion--;.    if( 0
13665 3d 3d 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74  ==pBt->nTransact
13666 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 70 42 74  ion ){.      pBt
13667 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20  ->inTransaction 
13668 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20  = TRANS_NONE;.  
13669 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65    }.  }..  /* Se
1366a 74 20 74 68 65 20 68 61 6e 64 6c 65 73 20 63 75  t the handles cu
1366b 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  rrent transactio
1366c 6e 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53  n state to TRANS
1366d 5f 4e 4f 4e 45 20 61 6e 64 20 75 6e 6c 6f 63 6b  _NONE and unlock
1366e 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65 72 20  .  ** the pager 
1366f 69 66 20 74 68 69 73 20 63 61 6c 6c 20 63 6c 6f  if this call clo
13670 73 65 64 20 74 68 65 20 6f 6e 6c 79 20 72 65 61  sed the only rea
13671 64 20 6f 72 20 77 72 69 74 65 20 74 72 61 6e 73  d or write trans
13672 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 70  action..  */.  p
13673 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e  ->inTrans = TRAN
13674 53 5f 4e 4f 4e 45 3b 0a 20 20 75 6e 6c 6f 63 6b  S_NONE;.  unlock
13675 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42  BtreeIfUnused(pB
13676 74 29 3b 0a 0a 20 20 62 74 72 65 65 49 6e 74 65  t);..  btreeInte
13677 67 72 69 74 79 28 70 29 3b 0a 20 20 73 71 6c 69  grity(p);.  sqli
13678 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
13679 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
1367a 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  E_OK;.}../*.** D
1367b 6f 20 62 6f 74 68 20 70 68 61 73 65 73 20 6f 66  o both phases of
1367c 20 61 20 63 6f 6d 6d 69 74 2e 0a 2a 2f 0a 53 51   a commit..*/.SQ
1367d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1367e 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
1367f 6d 69 74 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  mit(Btree *p){. 
13680 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74   int rc;.  sqlit
13681 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
13682 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
13683 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f  treeCommitPhaseO
13684 6e 65 28 70 2c 20 30 29 3b 0a 20 20 69 66 28 20  ne(p, 0);.  if( 
13685 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
13686 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
13687 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73  3BtreeCommitPhas
13688 65 54 77 6f 28 70 29 3b 0a 20 20 7d 0a 20 20 73  eTwo(p);.  }.  s
13689 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
1368a 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
1368b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45  ;.}..#ifndef NDE
1368c 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  BUG./*.** Return
1368d 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 77   the number of w
1368e 72 69 74 65 2d 63 75 72 73 6f 72 73 20 6f 70 65  rite-cursors ope
1368f 6e 20 6f 6e 20 74 68 69 73 20 68 61 6e 64 6c 65  n on this handle
13690 2e 20 54 68 69 73 20 69 73 20 66 6f 72 20 75 73  . This is for us
13691 65 0a 2a 2a 20 69 6e 20 61 73 73 65 72 74 28 29  e.** in assert()
13692 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 20 73 6f   expressions, so
13693 20 69 74 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70   it is only comp
13694 69 6c 65 64 20 69 66 20 4e 44 45 42 55 47 20 69  iled if NDEBUG i
13695 73 20 6e 6f 74 0a 2a 2a 20 64 65 66 69 6e 65 64  s not.** defined
13696 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20  ..**.** For the 
13697 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73  purposes of this
13698 20 72 6f 75 74 69 6e 65 2c 20 61 20 77 72 69 74   routine, a writ
13699 65 2d 63 75 72 73 6f 72 20 69 73 20 61 6e 79 20  e-cursor is any 
1369a 63 75 72 73 6f 72 20 74 68 61 74 0a 2a 2a 20 69  cursor that.** i
1369b 73 20 63 61 70 61 62 6c 65 20 6f 66 20 77 72 69  s capable of wri
1369c 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61  ting to the data
1369d 62 73 65 2e 20 20 54 68 61 74 20 6d 65 61 6e 73  bse.  That means
1369e 20 74 68 65 20 63 75 72 73 6f 72 20 77 61 73 0a   the cursor was.
1369f 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 20 6f 70  ** originally op
136a0 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67  ened for writing
136a1 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72 20   and the cursor 
136a2 68 61 73 20 6e 6f 74 20 62 65 20 64 69 73 61 62  has not be disab
136a3 6c 65 64 0a 2a 2a 20 62 79 20 68 61 76 69 6e 67  led.** by having
136a4 20 69 74 73 20 73 74 61 74 65 20 63 68 61 6e 67   its state chang
136a5 65 64 20 74 6f 20 43 55 52 53 4f 52 5f 46 41 55  ed to CURSOR_FAU
136a6 4c 54 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  LT..*/.static in
136a7 74 20 63 6f 75 6e 74 57 72 69 74 65 43 75 72 73  t countWriteCurs
136a8 6f 72 73 28 42 74 53 68 61 72 65 64 20 2a 70 42  ors(BtShared *pB
136a9 74 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a  t){.  BtCursor *
136aa 70 43 75 72 3b 0a 20 20 69 6e 74 20 72 20 3d 20  pCur;.  int r = 
136ab 30 3b 0a 20 20 66 6f 72 28 70 43 75 72 3d 70 42  0;.  for(pCur=pB
136ac 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 43 75 72  t->pCursor; pCur
136ad 3b 20 70 43 75 72 3d 70 43 75 72 2d 3e 70 4e 65  ; pCur=pCur->pNe
136ae 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75  xt){.    if( pCu
136af 72 2d 3e 77 72 46 6c 61 67 20 26 26 20 70 43 75  r->wrFlag && pCu
136b0 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f  r->eState!=CURSO
136b1 52 5f 46 41 55 4c 54 20 29 20 72 2b 2b 3b 20 0a  R_FAULT ) r++; .
136b2 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b 0a    }.  return r;.
136b3 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
136b4 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74  This routine set
136b5 73 20 74 68 65 20 73 74 61 74 65 20 74 6f 20 43  s the state to C
136b6 55 52 53 4f 52 5f 46 41 55 4c 54 20 61 6e 64 20  URSOR_FAULT and 
136b7 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64  the error.** cod
136b8 65 20 74 6f 20 65 72 72 43 6f 64 65 20 66 6f 72  e to errCode for
136b9 20 65 76 65 72 79 20 63 75 72 73 6f 72 20 6f 6e   every cursor on
136ba 20 42 74 53 68 61 72 65 64 20 74 68 61 74 20 70   BtShared that p
136bb 42 74 72 65 65 0a 2a 2a 20 72 65 66 65 72 65 6e  Btree.** referen
136bc 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79  ces..**.** Every
136bd 20 63 75 72 73 6f 72 20 69 73 20 74 72 69 70 70   cursor is tripp
136be 65 64 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 75  ed, including cu
136bf 72 73 6f 72 73 20 74 68 61 74 20 62 65 6c 6f 6e  rsors that belon
136c0 67 0a 2a 2a 20 74 6f 20 6f 74 68 65 72 20 64 61  g.** to other da
136c1 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
136c2 6e 73 20 74 68 61 74 20 68 61 70 70 65 6e 20 74  ns that happen t
136c3 6f 20 62 65 20 73 68 61 72 69 6e 67 0a 2a 2a 20  o be sharing.** 
136c4 74 68 65 20 63 61 63 68 65 20 77 69 74 68 20 70  the cache with p
136c5 42 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  Btree..**.** Thi
136c6 73 20 72 6f 75 74 69 6e 65 20 67 65 74 73 20 63  s routine gets c
136c7 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 72 6f 6c  alled when a rol
136c8 6c 62 61 63 6b 20 6f 63 63 75 72 73 2e 0a 2a 2a  lback occurs..**
136c9 20 41 6c 6c 20 63 75 72 73 6f 72 73 20 75 73 69   All cursors usi
136ca 6e 67 20 74 68 65 20 73 61 6d 65 20 63 61 63 68  ng the same cach
136cb 65 20 6d 75 73 74 20 62 65 20 74 72 69 70 70 65  e must be trippe
136cc 64 0a 2a 2a 20 74 6f 20 70 72 65 76 65 6e 74 20  d.** to prevent 
136cd 74 68 65 6d 20 66 72 6f 6d 20 74 72 79 69 6e 67  them from trying
136ce 20 74 6f 20 75 73 65 20 74 68 65 20 62 74 72 65   to use the btre
136cf 65 20 61 66 74 65 72 0a 2a 2a 20 74 68 65 20 72  e after.** the r
136d0 6f 6c 6c 62 61 63 6b 2e 20 20 54 68 65 20 72 6f  ollback.  The ro
136d1 6c 6c 62 61 63 6b 20 6d 61 79 20 68 61 76 65 20  llback may have 
136d2 64 65 6c 65 74 65 64 20 74 61 62 6c 65 73 0a 2a  deleted tables.*
136d3 2a 20 6f 72 20 6d 6f 76 65 64 20 72 6f 6f 74 20  * or moved root 
136d4 70 61 67 65 73 2c 20 73 6f 20 69 74 20 69 73 20  pages, so it is 
136d5 6e 6f 74 20 73 75 66 66 69 63 69 65 6e 74 20 74  not sufficient t
136d6 6f 0a 2a 2a 20 73 61 76 65 20 74 68 65 20 73 74  o.** save the st
136d7 61 74 65 20 6f 66 20 74 68 65 20 63 75 72 73 6f  ate of the curso
136d8 72 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d  r.  The cursor m
136d9 75 73 74 20 62 65 0a 2a 2a 20 69 6e 76 61 6c 69  ust be.** invali
136da 64 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  dated..*/.SQLITE
136db 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
136dc 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 41 6c  lite3BtreeTripAl
136dd 6c 43 75 72 73 6f 72 73 28 42 74 72 65 65 20 2a  lCursors(Btree *
136de 70 42 74 72 65 65 2c 20 69 6e 74 20 65 72 72 43  pBtree, int errC
136df 6f 64 65 29 7b 0a 20 20 42 74 43 75 72 73 6f 72  ode){.  BtCursor
136e0 20 2a 70 3b 0a 20 20 73 71 6c 69 74 65 33 42 74   *p;.  sqlite3Bt
136e1 72 65 65 45 6e 74 65 72 28 70 42 74 72 65 65 29  reeEnter(pBtree)
136e2 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 72 65 65  ;.  for(p=pBtree
136e3 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20  ->pBt->pCursor; 
136e4 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a  p; p=p->pNext){.
136e5 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
136e6 43 6c 65 61 72 43 75 72 73 6f 72 28 70 29 3b 0a  ClearCursor(p);.
136e7 20 20 20 20 70 2d 3e 65 53 74 61 74 65 20 3d 20      p->eState = 
136e8 43 55 52 53 4f 52 5f 46 41 55 4c 54 3b 0a 20 20  CURSOR_FAULT;.  
136e9 20 20 70 2d 3e 73 6b 69 70 20 3d 20 65 72 72 43    p->skip = errC
136ea 6f 64 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ode;.  }.  sqlit
136eb 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 42 74  e3BtreeLeave(pBt
136ec 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ree);.}../*.** R
136ed 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e  ollback the tran
136ee 73 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72  saction in progr
136ef 65 73 73 2e 20 20 41 6c 6c 20 63 75 72 73 6f 72  ess.  All cursor
136f0 73 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 69 6e 76  s will be.** inv
136f1 61 6c 69 64 65 64 20 62 79 20 74 68 69 73 20 6f  alided by this o
136f2 70 65 72 61 74 69 6f 6e 2e 20 20 41 6e 79 20 61  peration.  Any a
136f3 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 61 20  ttempt to use a 
136f4 63 75 72 73 6f 72 0a 2a 2a 20 74 68 61 74 20 77  cursor.** that w
136f5 61 73 20 6f 70 65 6e 20 61 74 20 74 68 65 20 62  as open at the b
136f6 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 69 73  eginning of this
136f7 20 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c 6c 20   operation will 
136f8 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 61 6e 20  result.** in an 
136f9 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  error..**.** Thi
136fa 73 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20 74  s will release t
136fb 68 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e  he write lock on
136fc 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
136fd 6c 65 2e 20 20 49 66 20 74 68 65 72 65 0a 2a 2a  le.  If there.**
136fe 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 63   are no active c
136ff 75 72 73 6f 72 73 2c 20 69 74 20 61 6c 73 6f 20  ursors, it also 
13700 72 65 6c 65 61 73 65 73 20 74 68 65 20 72 65 61  releases the rea
13701 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54  d lock..*/.SQLIT
13702 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
13703 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61  lite3BtreeRollba
13704 63 6b 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20  ck(Btree *p){.  
13705 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72  int rc;.  BtShar
13706 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
13707 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ;.  MemPage *pPa
13708 67 65 31 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42  ge1;..  sqlite3B
13709 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
1370a 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b  pBt->db = p->db;
1370b 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43  .  rc = saveAllC
1370c 75 72 73 6f 72 73 28 70 42 74 2c 20 30 2c 20 30  ursors(pBt, 0, 0
1370d 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
1370e 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
1370f 43 48 45 0a 20 20 69 66 28 20 72 63 21 3d 53 51  CHE.  if( rc!=SQ
13710 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f  LITE_OK ){.    /
13711 2a 20 54 68 69 73 20 69 73 20 61 20 68 6f 72 72  * This is a horr
13712 69 62 6c 65 20 73 69 74 75 61 74 69 6f 6e 2e 20  ible situation. 
13713 41 6e 20 49 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28  An IO or malloc(
13714 29 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20  ) error occured 
13715 77 68 69 6c 73 74 0a 20 20 20 20 2a 2a 20 74 72  whilst.    ** tr
13716 79 69 6e 67 20 74 6f 20 73 61 76 65 20 63 75 72  ying to save cur
13717 73 6f 72 20 70 6f 73 69 74 69 6f 6e 73 2e 20 49  sor positions. I
13718 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74  f this is an aut
13719 6f 6d 61 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20  omatic rollback 
1371a 28 61 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 72  (as.    ** the r
1371b 65 73 75 6c 74 20 6f 66 20 61 20 63 6f 6e 73 74  esult of a const
1371c 72 61 69 6e 74 2c 20 6d 61 6c 6c 6f 63 28 29 20  raint, malloc() 
1371d 66 61 69 6c 75 72 65 20 6f 72 20 49 4f 20 65 72  failure or IO er
1371e 72 6f 72 29 20 74 68 65 6e 20 0a 20 20 20 20 2a  ror) then .    *
1371f 2a 20 74 68 65 20 63 61 63 68 65 20 6d 61 79 20  * the cache may 
13720 62 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 69 6e  be internally in
13721 63 6f 6e 73 69 73 74 65 6e 74 20 28 6e 6f 74 20  consistent (not 
13722 63 6f 6e 74 61 69 6e 20 76 61 6c 69 64 20 74 72  contain valid tr
13723 65 65 73 29 20 73 6f 0a 20 20 20 20 2a 2a 20 77  ees) so.    ** w
13724 65 20 63 61 6e 6e 6f 74 20 73 69 6d 70 6c 79 20  e cannot simply 
13725 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72  return the error
13726 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 20   to the caller. 
13727 49 6e 73 74 65 61 64 2c 20 61 62 6f 72 74 20 0a  Instead, abort .
13728 20 20 20 20 2a 2a 20 61 6c 6c 20 71 75 65 72 69      ** all queri
13729 65 73 20 74 68 61 74 20 6d 61 79 20 62 65 20 75  es that may be u
1372a 73 69 6e 67 20 61 6e 79 20 6f 66 20 74 68 65 20  sing any of the 
1372b 63 75 72 73 6f 72 73 20 74 68 61 74 20 66 61 69  cursors that fai
1372c 6c 65 64 20 74 6f 20 73 61 76 65 2e 0a 20 20 20  led to save..   
1372d 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42   */.    sqlite3B
1372e 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f  treeTripAllCurso
1372f 72 73 28 70 2c 20 72 63 29 3b 0a 20 20 7d 0a 23  rs(p, rc);.  }.#
13730 65 6e 64 69 66 0a 20 20 62 74 72 65 65 49 6e 74  endif.  btreeInt
13731 65 67 72 69 74 79 28 70 29 3b 0a 20 20 75 6e 6c  egrity(p);.  unl
13732 6f 63 6b 41 6c 6c 54 61 62 6c 65 73 28 70 29 3b  ockAllTables(p);
13733 0a 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61  ..  if( p->inTra
13734 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20  ns==TRANS_WRITE 
13735 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 32 3b 0a  ){.    int rc2;.
13736 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
13737 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
13738 20 20 20 20 70 42 74 2d 3e 6e 54 72 75 6e 63 20      pBt->nTrunc 
13739 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20  = 0;.#endif..   
1373a 20 61 73 73 65 72 74 28 20 54 52 41 4e 53 5f 57   assert( TRANS_W
1373b 52 49 54 45 3d 3d 70 42 74 2d 3e 69 6e 54 72 61  RITE==pBt->inTra
1373c 6e 73 61 63 74 69 6f 6e 20 29 3b 0a 20 20 20 20  nsaction );.    
1373d 72 63 32 20 3d 20 73 71 6c 69 74 65 33 50 61 67  rc2 = sqlite3Pag
1373e 65 72 52 6f 6c 6c 62 61 63 6b 28 70 42 74 2d 3e  erRollback(pBt->
1373f 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28  pPager);.    if(
13740 20 72 63 32 21 3d 53 51 4c 49 54 45 5f 4f 4b 20   rc2!=SQLITE_OK 
13741 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 63  ){.      rc = rc
13742 32 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  2;.    }..    /*
13743 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d 61   The rollback ma
13744 79 20 68 61 76 65 20 64 65 73 74 72 6f 79 65 64  y have destroyed
13745 20 74 68 65 20 70 50 61 67 65 31 2d 3e 61 44 61   the pPage1->aDa
13746 74 61 20 76 61 6c 75 65 2e 20 20 53 6f 0a 20 20  ta value.  So.  
13747 20 20 2a 2a 20 63 61 6c 6c 20 73 71 6c 69 74 65    ** call sqlite
13748 33 42 74 72 65 65 47 65 74 50 61 67 65 28 29 20  3BtreeGetPage() 
13749 6f 6e 20 70 61 67 65 20 31 20 61 67 61 69 6e 20  on page 1 again 
1374a 74 6f 20 6d 61 6b 65 0a 20 20 20 20 2a 2a 20 73  to make.    ** s
1374b 75 72 65 20 70 50 61 67 65 31 2d 3e 61 44 61 74  ure pPage1->aDat
1374c 61 20 69 73 20 73 65 74 20 63 6f 72 72 65 63 74  a is set correct
1374d 6c 79 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 73  ly. */.    if( s
1374e 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61  qlite3BtreeGetPa
1374f 67 65 28 70 42 74 2c 20 31 2c 20 26 70 50 61 67  ge(pBt, 1, &pPag
13750 65 31 2c 20 30 29 3d 3d 53 51 4c 49 54 45 5f 4f  e1, 0)==SQLITE_O
13751 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61  K ){.      relea
13752 73 65 50 61 67 65 28 70 50 61 67 65 31 29 3b 0a  sePage(pPage1);.
13753 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
13754 28 20 63 6f 75 6e 74 57 72 69 74 65 43 75 72 73  ( countWriteCurs
13755 6f 72 73 28 70 42 74 29 3d 3d 30 20 29 3b 0a 20  ors(pBt)==0 );. 
13756 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61     pBt->inTransa
13757 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52 45  ction = TRANS_RE
13758 41 44 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70  AD;.  }..  if( p
13759 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53  ->inTrans!=TRANS
1375a 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 61 73 73  _NONE ){.    ass
1375b 65 72 74 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73  ert( pBt->nTrans
1375c 61 63 74 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20  action>0 );.    
1375d 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f  pBt->nTransactio
1375e 6e 2d 2d 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d  n--;.    if( 0==
1375f 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f  pBt->nTransactio
13760 6e 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e  n ){.      pBt->
13761 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20  inTransaction = 
13762 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20  TRANS_NONE;.    
13763 7d 0a 20 20 7d 0a 0a 20 20 70 2d 3e 69 6e 54 72  }.  }..  p->inTr
13764 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45  ans = TRANS_NONE
13765 3b 0a 20 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20  ;.  pBt->inStmt 
13766 3d 20 30 3b 0a 20 20 75 6e 6c 6f 63 6b 42 74 72  = 0;.  unlockBtr
13767 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b  eeIfUnused(pBt);
13768 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69  ..  btreeIntegri
13769 74 79 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  ty(p);.  sqlite3
1376a 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
1376b 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1376c 2a 0a 2a 2a 20 53 74 61 72 74 20 61 20 73 74 61  *.** Start a sta
1376d 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61  tement subtransa
1376e 63 74 69 6f 6e 2e 20 20 54 68 65 20 73 75 62 74  ction.  The subt
1376f 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 0a 2a  ransaction can.*
13770 2a 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64 20  * can be rolled 
13771 62 61 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74  back independent
13772 6c 79 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 74  ly of the main t
13773 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 59  ransaction..** Y
13774 6f 75 20 6d 75 73 74 20 73 74 61 72 74 20 61 20  ou must start a 
13775 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 66 6f  transaction befo
13776 72 65 20 73 74 61 72 74 69 6e 67 20 61 20 73 75  re starting a su
13777 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a  btransaction..**
13778 20 54 68 65 20 73 75 62 74 72 61 6e 73 61 63 74   The subtransact
13779 69 6f 6e 20 69 73 20 65 6e 64 65 64 20 61 75 74  ion is ended aut
1377a 6f 6d 61 74 69 63 61 6c 6c 79 20 69 66 20 74 68  omatically if th
1377b 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 74 69  e main transacti
1377c 6f 6e 0a 2a 2a 20 63 6f 6d 6d 69 74 73 20 6f 72  on.** commits or
1377d 20 72 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2a 0a   rolls back..**.
1377e 2a 2a 20 4f 6e 6c 79 20 6f 6e 65 20 73 75 62 74  ** Only one subt
1377f 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 62  ransaction may b
13780 65 20 61 63 74 69 76 65 20 61 74 20 61 20 74 69  e active at a ti
13781 6d 65 2e 20 20 49 74 20 69 73 20 61 6e 20 65 72  me.  It is an er
13782 72 6f 72 20 74 6f 20 74 72 79 0a 2a 2a 20 74 6f  ror to try.** to
13783 20 73 74 61 72 74 20 61 20 6e 65 77 20 73 75 62   start a new sub
13784 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 61  transaction if a
13785 6e 6f 74 68 65 72 20 73 75 62 74 72 61 6e 73 61  nother subtransa
13786 63 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 79  ction is already
13787 20 61 63 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 53   active..**.** S
13788 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e  tatement subtran
13789 73 61 63 74 69 6f 6e 73 20 61 72 65 20 75 73 65  sactions are use
1378a 64 20 61 72 6f 75 6e 64 20 69 6e 64 69 76 69 64  d around individ
1378b 75 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ual SQL statemen
1378c 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 63  ts.** that are c
1378d 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20  ontained within 
1378e 61 20 42 45 47 49 4e 2e 2e 2e 43 4f 4d 4d 49 54  a BEGIN...COMMIT
1378f 20 62 6c 6f 63 6b 2e 20 20 49 66 20 61 20 63 6f   block.  If a co
13790 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72 6f  nstraint.** erro
13791 72 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20  r occurs within 
13792 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 74  the statement, t
13793 68 65 20 65 66 66 65 63 74 20 6f 66 20 74 68 61  he effect of tha
13794 74 20 6f 6e 65 20 73 74 61 74 65 6d 65 6e 74 0a  t one statement.
13795 2a 2a 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64  ** can be rolled
13796 20 62 61 63 6b 20 77 69 74 68 6f 75 74 20 68 61   back without ha
13797 76 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b  ving to rollback
13798 20 74 68 65 20 65 6e 74 69 72 65 20 74 72 61 6e   the entire tran
13799 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  saction..*/.SQLI
1379a 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1379b 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e  qlite3BtreeBegin
1379c 53 74 6d 74 28 42 74 72 65 65 20 2a 70 29 7b 0a  Stmt(Btree *p){.
1379d 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68    int rc;.  BtSh
1379e 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
1379f 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  Bt;.  sqlite3Btr
137a0 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42  eeEnter(p);.  pB
137a1 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20  t->db = p->db;. 
137a2 20 69 66 28 20 28 70 2d 3e 69 6e 54 72 61 6e 73   if( (p->inTrans
137a3 21 3d 54 52 41 4e 53 5f 57 52 49 54 45 29 20 7c  !=TRANS_WRITE) |
137a4 7c 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 29 7b  | pBt->inStmt ){
137a5 0a 20 20 20 20 72 63 20 3d 20 70 42 74 2d 3e 72  .    rc = pBt->r
137a6 65 61 64 4f 6e 6c 79 20 3f 20 53 51 4c 49 54 45  eadOnly ? SQLITE
137a7 5f 52 45 41 44 4f 4e 4c 59 20 3a 20 53 51 4c 49  _READONLY : SQLI
137a8 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73  TE_ERROR;.  }els
137a9 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
137aa 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f  Bt->inTransactio
137ab 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29  n==TRANS_WRITE )
137ac 3b 0a 20 20 20 20 72 63 20 3d 20 70 42 74 2d 3e  ;.    rc = pBt->
137ad 72 65 61 64 4f 6e 6c 79 20 3f 20 53 51 4c 49 54  readOnly ? SQLIT
137ae 45 5f 4f 4b 20 3a 20 73 71 6c 69 74 65 33 50 61  E_OK : sqlite3Pa
137af 67 65 72 53 74 6d 74 42 65 67 69 6e 28 70 42 74  gerStmtBegin(pBt
137b0 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 70  ->pPager);.    p
137b1 42 74 2d 3e 69 6e 53 74 6d 74 20 3d 20 31 3b 0a  Bt->inStmt = 1;.
137b2 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72    }.  sqlite3Btr
137b3 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65  eeLeave(p);.  re
137b4 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
137b5 2a 2a 20 43 6f 6d 6d 69 74 20 74 68 65 20 73 74  ** Commit the st
137b6 61 74 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61  atment subtransa
137b7 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20  ction currently 
137b8 69 6e 20 70 72 6f 67 72 65 73 73 2e 20 20 49 66  in progress.  If
137b9 20 6e 6f 0a 2a 2a 20 73 75 62 74 72 61 6e 73 61   no.** subtransa
137ba 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2c  ction is active,
137bb 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70   this is a no-op
137bc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
137bd 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
137be 74 72 65 65 43 6f 6d 6d 69 74 53 74 6d 74 28 42  treeCommitStmt(B
137bf 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  tree *p){.  int 
137c0 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  rc;.  BtShared *
137c1 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
137c2 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
137c3 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 62 20  r(p);.  pBt->db 
137c4 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70  = p->db;.  if( p
137c5 42 74 2d 3e 69 6e 53 74 6d 74 20 26 26 20 21 70  Bt->inStmt && !p
137c6 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a  Bt->readOnly ){.
137c7 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
137c8 50 61 67 65 72 53 74 6d 74 43 6f 6d 6d 69 74 28  PagerStmtCommit(
137c9 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20  pBt->pPager);.  
137ca 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
137cb 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
137cc 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d 20 30   pBt->inStmt = 0
137cd 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
137ce 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
137cf 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
137d0 52 6f 6c 6c 62 61 63 6b 20 74 68 65 20 61 63 74  Rollback the act
137d1 69 76 65 20 73 74 61 74 65 6d 65 6e 74 20 73 75  ive statement su
137d2 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49  btransaction.  I
137d3 66 20 6e 6f 20 73 75 62 74 72 61 6e 73 61 63 74  f no subtransact
137d4 69 6f 6e 0a 2a 2a 20 69 73 20 61 63 74 69 76 65  ion.** is active
137d5 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
137d6 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20   a no-op..**.** 
137d7 41 6c 6c 20 63 75 72 73 6f 72 73 20 77 69 6c 6c  All cursors will
137d8 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65 64 20   be invalidated 
137d9 62 79 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f  by this operatio
137da 6e 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 0a  n.  Any attempt.
137db 2a 2a 20 74 6f 20 75 73 65 20 61 20 63 75 72 73  ** to use a curs
137dc 6f 72 20 74 68 61 74 20 77 61 73 20 6f 70 65 6e  or that was open
137dd 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   at the beginnin
137de 67 20 6f 66 20 74 68 69 73 20 6f 70 65 72 61 74  g of this operat
137df 69 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 72 65 73 75  ion.** will resu
137e0 6c 74 20 69 6e 20 61 6e 20 65 72 72 6f 72 2e 0a  lt in an error..
137e1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
137e2 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
137e3 65 65 52 6f 6c 6c 62 61 63 6b 53 74 6d 74 28 42  eeRollbackStmt(B
137e4 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  tree *p){.  int 
137e5 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
137e6 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
137e7 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69  = p->pBt;.  sqli
137e8 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
137e9 3b 0a 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d  ;.  pBt->db = p-
137ea 3e 64 62 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e  >db;.  if( pBt->
137eb 69 6e 53 74 6d 74 20 26 26 20 21 70 42 74 2d 3e  inStmt && !pBt->
137ec 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20  readOnly ){.    
137ed 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
137ee 72 53 74 6d 74 52 6f 6c 6c 62 61 63 6b 28 70 42  rStmtRollback(pB
137ef 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20  t->pPager);.    
137f0 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d 20 30 3b  pBt->inStmt = 0;
137f1 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74  .  }.  sqlite3Bt
137f2 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
137f3 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
137f4 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
137f5 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20 42  cursor for the B
137f6 54 72 65 65 20 77 68 6f 73 65 20 72 6f 6f 74 20  Tree whose root 
137f7 69 73 20 6f 6e 20 74 68 65 20 70 61 67 65 0a 2a  is on the page.*
137f8 2a 20 69 54 61 62 6c 65 2e 20 20 54 68 65 20 61  * iTable.  The a
137f9 63 74 20 6f 66 20 61 63 71 75 69 72 69 6e 67 20  ct of acquiring 
137fa 61 20 63 75 72 73 6f 72 20 67 65 74 73 20 61 20  a cursor gets a 
137fb 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 0a 2a 2a  read lock on .**
137fc 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
137fd 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72 46  le..**.** If wrF
137fe 6c 61 67 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65  lag==0, then the
137ff 20 63 75 72 73 6f 72 20 63 61 6e 20 6f 6e 6c 79   cursor can only
13800 20 62 65 20 75 73 65 64 20 66 6f 72 20 72 65 61   be used for rea
13801 64 69 6e 67 2e 0a 2a 2a 20 49 66 20 77 72 46 6c  ding..** If wrFl
13802 61 67 3d 3d 31 2c 20 74 68 65 6e 20 74 68 65 20  ag==1, then the 
13803 63 75 72 73 6f 72 20 63 61 6e 20 62 65 20 75 73  cursor can be us
13804 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f  ed for reading o
13805 72 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67  r for.** writing
13806 20 69 66 20 6f 74 68 65 72 20 63 6f 6e 64 69 74   if other condit
13807 69 6f 6e 73 20 66 6f 72 20 77 72 69 74 69 6e 67  ions for writing
13808 20 61 72 65 20 61 6c 73 6f 20 6d 65 74 2e 20 20   are also met.  
13809 54 68 65 73 65 0a 2a 2a 20 61 72 65 20 74 68 65  These.** are the
1380a 20 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74   conditions that
1380b 20 6d 75 73 74 20 62 65 20 6d 65 74 20 69 6e 20   must be met in 
1380c 6f 72 64 65 72 20 66 6f 72 20 77 72 69 74 69 6e  order for writin
1380d 67 20 74 6f 0a 2a 2a 20 62 65 20 61 6c 6c 6f 77  g to.** be allow
1380e 65 64 3a 0a 2a 2a 0a 2a 2a 20 31 3a 20 20 54 68  ed:.**.** 1:  Th
1380f 65 20 63 75 72 73 6f 72 20 6d 75 73 74 20 68 61  e cursor must ha
13810 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 77  ve been opened w
13811 69 74 68 20 77 72 46 6c 61 67 3d 3d 31 0a 2a 2a  ith wrFlag==1.**
13812 0a 2a 2a 20 32 3a 20 20 4f 74 68 65 72 20 64 61  .** 2:  Other da
13813 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
13814 6e 73 20 74 68 61 74 20 73 68 61 72 65 20 74 68  ns that share th
13815 65 20 73 61 6d 65 20 70 61 67 65 72 20 63 61 63  e same pager cac
13816 68 65 0a 2a 2a 20 20 20 20 20 62 75 74 20 77 68  he.**     but wh
13817 69 63 68 20 61 72 65 20 6e 6f 74 20 69 6e 20 74  ich are not in t
13818 68 65 20 52 45 41 44 5f 55 4e 43 4f 4d 4d 49 54  he READ_UNCOMMIT
13819 54 45 44 20 73 74 61 74 65 20 6d 61 79 20 6e 6f  TED state may no
1381a 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 63 75  t have.**     cu
1381b 72 73 6f 72 73 20 6f 70 65 6e 20 77 69 74 68 20  rsors open with 
1381c 77 72 46 6c 61 67 3d 3d 30 20 6f 6e 20 74 68 65  wrFlag==0 on the
1381d 20 73 61 6d 65 20 74 61 62 6c 65 2e 20 20 4f 74   same table.  Ot
1381e 68 65 72 77 69 73 65 0a 2a 2a 20 20 20 20 20 74  herwise.**     t
1381f 68 65 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20  he changes made 
13820 62 79 20 74 68 69 73 20 77 72 69 74 65 20 63 75  by this write cu
13821 72 73 6f 72 20 77 6f 75 6c 64 20 62 65 20 76 69  rsor would be vi
13822 73 69 62 6c 65 20 74 6f 0a 2a 2a 20 20 20 20 20  sible to.**     
13823 74 68 65 20 72 65 61 64 20 63 75 72 73 6f 72 73  the read cursors
13824 20 69 6e 20 74 68 65 20 6f 74 68 65 72 20 64 61   in the other da
13825 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
13826 6e 2e 0a 2a 2a 0a 2a 2a 20 33 3a 20 20 54 68 65  n..**.** 3:  The
13827 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 62   database must b
13828 65 20 77 72 69 74 61 62 6c 65 20 28 6e 6f 74 20  e writable (not 
13829 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 65 64  on read-only med
1382a 69 61 29 0a 2a 2a 0a 2a 2a 20 34 3a 20 20 54 68  ia).**.** 4:  Th
1382b 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20 61  ere must be an a
1382c 63 74 69 76 65 20 74 72 61 6e 73 61 63 74 69 6f  ctive transactio
1382d 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 63 68 65 63  n..**.** No chec
1382e 6b 69 6e 67 20 69 73 20 64 6f 6e 65 20 74 6f 20  king is done to 
1382f 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 70  make sure that p
13830 61 67 65 20 69 54 61 62 6c 65 20 72 65 61 6c 6c  age iTable reall
13831 79 20 69 73 20 74 68 65 0a 2a 2a 20 72 6f 6f 74  y is the.** root
13832 20 70 61 67 65 20 6f 66 20 61 20 62 2d 74 72 65   page of a b-tre
13833 65 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74  e.  If it is not
13834 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f  , then the curso
13835 72 20 61 63 71 75 69 72 65 64 0a 2a 2a 20 77 69  r acquired.** wi
13836 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72  ll not work corr
13837 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20  ectly..**.** It 
13838 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20  is assumed that 
13839 74 68 65 20 73 71 6c 69 74 65 33 42 74 72 65 65  the sqlite3Btree
1383a 43 75 72 73 6f 72 53 69 7a 65 28 29 20 62 79 74  CursorSize() byt
1383b 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 0a 2a 2a  es of memory .**
1383c 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70   pointed to by p
1383d 43 75 72 20 68 61 76 65 20 62 65 65 6e 20 7a 65  Cur have been ze
1383e 72 6f 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c  roed by the call
1383f 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  er..*/.static in
13840 74 20 62 74 72 65 65 43 75 72 73 6f 72 28 0a 20  t btreeCursor(. 
13841 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20 20   Btree *p,      
13842 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13843 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62          /* The b
13844 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 54  tree */.  int iT
13845 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20  able,           
13846 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13847 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66   /* Root page of
13848 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a   table to open *
13849 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20  /.  int wrFlag, 
1384a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1384b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20             /* 1 
1384c 74 6f 20 77 72 69 74 65 2e 20 30 20 72 65 61 64  to write. 0 read
1384d 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63  -only */.  struc
1384e 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49  t KeyInfo *pKeyI
1384f 6e 66 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20  nfo,            
13850 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74    /* First arg t
13851 6f 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e  o comparison fun
13852 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72  ction */.  BtCur
13853 73 6f 72 20 2a 70 43 75 72 20 20 20 20 20 20 20  sor *pCur       
13854 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13855 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 6e    /* Space for n
13856 65 77 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a  ew cursor */.){.
13857 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68    int rc;.  BtSh
13858 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
13859 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  Bt;..  assert( s
1385a 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
1385b 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 69 66  Mutex(p) );.  if
1385c 28 20 77 72 46 6c 61 67 20 29 7b 0a 20 20 20 20  ( wrFlag ){.    
1385d 69 66 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c  if( pBt->readOnl
1385e 79 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  y ){.      retur
1385f 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c  n SQLITE_READONL
13860 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  Y;.    }.    if(
13861 20 63 68 65 63 6b 52 65 61 64 4c 6f 63 6b 73 28   checkReadLocks(
13862 70 2c 20 69 54 61 62 6c 65 2c 20 30 2c 20 30 29  p, iTable, 0, 0)
13863 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
13864 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a   SQLITE_LOCKED;.
13865 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28      }.  }..  if(
13866 20 70 42 74 2d 3e 70 50 61 67 65 31 3d 3d 30 20   pBt->pPage1==0 
13867 29 7b 0a 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b  ){.    rc = lock
13868 42 74 72 65 65 57 69 74 68 52 65 74 72 79 28 70  BtreeWithRetry(p
13869 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
1386a 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1386b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
1386c 20 7d 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e   }.    if( pBt->
1386d 72 65 61 64 4f 6e 6c 79 20 26 26 20 77 72 46 6c  readOnly && wrFl
1386e 61 67 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  ag ){.      retu
1386f 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e  rn SQLITE_READON
13870 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  LY;.    }.  }.  
13871 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 3d  pCur->pgnoRoot =
13872 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 3b 0a 20   (Pgno)iTable;. 
13873 20 69 66 28 20 69 54 61 62 6c 65 3d 3d 31 20 26   if( iTable==1 &
13874 26 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74  & pagerPagecount
13875 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3d 3d 30  (pBt->pPager)==0
13876 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
13877 49 54 45 5f 45 4d 50 54 59 3b 0a 20 20 20 20 67  ITE_EMPTY;.    g
13878 6f 74 6f 20 63 72 65 61 74 65 5f 63 75 72 73 6f  oto create_curso
13879 72 5f 65 78 63 65 70 74 69 6f 6e 3b 0a 20 20 7d  r_exception;.  }
1387a 0a 20 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e  .  rc = getAndIn
1387b 69 74 50 61 67 65 28 70 42 74 2c 20 70 43 75 72  itPage(pBt, pCur
1387c 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 43 75  ->pgnoRoot, &pCu
1387d 72 2d 3e 61 70 50 61 67 65 5b 30 5d 29 3b 0a 20  r->apPage[0]);. 
1387e 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1387f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 63  OK ){.    goto c
13880 72 65 61 74 65 5f 63 75 72 73 6f 72 5f 65 78 63  reate_cursor_exc
13881 65 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f  eption;.  }..  /
13882 2a 20 4e 6f 77 20 74 68 61 74 20 6e 6f 20 6f 74  * Now that no ot
13883 68 65 72 20 65 72 72 6f 72 73 20 63 61 6e 20 6f  her errors can o
13884 63 63 75 72 2c 20 66 69 6e 69 73 68 20 66 69 6c  ccur, finish fil
13885 6c 69 6e 67 20 69 6e 20 74 68 65 20 42 74 43 75  ling in the BtCu
13886 72 73 6f 72 0a 20 20 2a 2a 20 76 61 72 69 61 62  rsor.  ** variab
13887 6c 65 73 2c 20 6c 69 6e 6b 20 74 68 65 20 63 75  les, link the cu
13888 72 73 6f 72 20 69 6e 74 6f 20 74 68 65 20 42 74  rsor into the Bt
13889 53 68 61 72 65 64 20 6c 69 73 74 20 61 6e 64 20  Shared list and 
1388a 73 65 74 20 2a 70 70 43 75 72 20 28 74 68 65 0a  set *ppCur (the.
1388b 20 20 2a 2a 20 6f 75 74 70 75 74 20 61 72 67 75    ** output argu
1388c 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e  ment to this fun
1388d 63 74 69 6f 6e 29 2e 0a 20 20 2a 2f 0a 20 20 70  ction)..  */.  p
1388e 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20  Cur->pKeyInfo = 
1388f 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 70 43 75 72  pKeyInfo;.  pCur
13890 2d 3e 70 42 74 72 65 65 20 3d 20 70 3b 0a 20 20  ->pBtree = p;.  
13891 70 43 75 72 2d 3e 70 42 74 20 3d 20 70 42 74 3b  pCur->pBt = pBt;
13892 0a 20 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20  .  pCur->wrFlag 
13893 3d 20 77 72 46 6c 61 67 3b 0a 20 20 70 43 75 72  = wrFlag;.  pCur
13894 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70  ->pNext = pBt->p
13895 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 70 43  Cursor;.  if( pC
13896 75 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20  ur->pNext ){.   
13897 20 70 43 75 72 2d 3e 70 4e 65 78 74 2d 3e 70 50   pCur->pNext->pP
13898 72 65 76 20 3d 20 70 43 75 72 3b 0a 20 20 7d 0a  rev = pCur;.  }.
13899 20 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d    pBt->pCursor =
1389a 20 70 43 75 72 3b 0a 20 20 70 43 75 72 2d 3e 65   pCur;.  pCur->e
1389b 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49  State = CURSOR_I
1389c 4e 56 41 4c 49 44 3b 0a 0a 20 20 72 65 74 75 72  NVALID;..  retur
1389d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 63 72  n SQLITE_OK;..cr
1389e 65 61 74 65 5f 63 75 72 73 6f 72 5f 65 78 63 65  eate_cursor_exce
1389f 70 74 69 6f 6e 3a 0a 20 20 72 65 6c 65 61 73 65  ption:.  release
138a0 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67  Page(pCur->apPag
138a1 65 5b 30 5d 29 3b 0a 20 20 75 6e 6c 6f 63 6b 42  e[0]);.  unlockB
138a2 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74  treeIfUnused(pBt
138a3 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
138a4 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
138a5 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
138a6 65 43 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65  eCursor(.  Btree
138a7 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
138a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
138a9 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74         /* The bt
138aa 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61  ree */.  int iTa
138ab 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ble,            
138ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
138ad 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67       /* Root pag
138ae 65 20 6f 66 20 74 61 62 6c 65 20 74 6f 20 6f 70  e of table to op
138af 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c  en */.  int wrFl
138b0 61 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ag,             
138b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
138b2 20 20 20 20 2f 2a 20 31 20 74 6f 20 77 72 69 74      /* 1 to writ
138b3 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a  e. 0 read-only *
138b4 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e  /.  struct KeyIn
138b5 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20  fo *pKeyInfo,   
138b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
138b7 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74 6f 20  /* First arg to 
138b8 78 43 6f 6d 70 61 72 65 28 29 20 2a 2f 0a 20 20  xCompare() */.  
138b9 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 20 20  BtCursor *pCur  
138ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
138bb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
138bc 72 69 74 65 20 6e 65 77 20 63 75 72 73 6f 72 20  rite new cursor 
138bd 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  here */.){.  int
138be 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74   rc;.  sqlite3Bt
138bf 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70  reeEnter(p);.  p
138c0 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64  ->pBt->db = p->d
138c1 62 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 43  b;.  rc = btreeC
138c2 75 72 73 6f 72 28 70 2c 20 69 54 61 62 6c 65 2c  ursor(p, iTable,
138c3 20 77 72 46 6c 61 67 2c 20 70 4b 65 79 49 6e 66   wrFlag, pKeyInf
138c4 6f 2c 20 70 43 75 72 29 3b 0a 20 20 73 71 6c 69  o, pCur);.  sqli
138c5 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
138c6 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
138c7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
138c8 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
138c9 43 75 72 73 6f 72 53 69 7a 65 28 29 7b 0a 20 20  CursorSize(){.  
138ca 72 65 74 75 72 6e 20 73 69 7a 65 6f 66 28 42 74  return sizeof(Bt
138cb 43 75 72 73 6f 72 29 3b 0a 7d 0a 0a 0a 0a 2f 2a  Cursor);.}..../*
138cc 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 75 72 73  .** Close a curs
138cd 6f 72 2e 20 20 54 68 65 20 72 65 61 64 20 6c 6f  or.  The read lo
138ce 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ck on the databa
138cf 73 65 20 66 69 6c 65 20 69 73 20 72 65 6c 65 61  se file is relea
138d0 73 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20  sed.** when the 
138d1 6c 61 73 74 20 63 75 72 73 6f 72 20 69 73 20 63  last cursor is c
138d2 6c 6f 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  losed..*/.SQLITE
138d3 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
138d4 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75  ite3BtreeCloseCu
138d5 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70  rsor(BtCursor *p
138d6 43 75 72 29 7b 0a 20 20 42 74 72 65 65 20 2a 70  Cur){.  Btree *p
138d7 42 74 72 65 65 20 3d 20 70 43 75 72 2d 3e 70 42  Btree = pCur->pB
138d8 74 72 65 65 3b 0a 20 20 69 66 28 20 70 42 74 72  tree;.  if( pBtr
138d9 65 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  ee ){.    int i;
138da 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70  .    BtShared *p
138db 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a  Bt = pCur->pBt;.
138dc 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
138dd 45 6e 74 65 72 28 70 42 74 72 65 65 29 3b 0a 20  Enter(pBtree);. 
138de 20 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 42 74     pBt->db = pBt
138df 72 65 65 2d 3e 64 62 3b 0a 20 20 20 20 73 71 6c  ree->db;.    sql
138e0 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75  ite3BtreeClearCu
138e1 72 73 6f 72 28 70 43 75 72 29 3b 0a 20 20 20 20  rsor(pCur);.    
138e2 69 66 28 20 70 43 75 72 2d 3e 70 50 72 65 76 20  if( pCur->pPrev 
138e3 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70  ){.      pCur->p
138e4 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 43  Prev->pNext = pC
138e5 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d  ur->pNext;.    }
138e6 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 42 74 2d  else{.      pBt-
138e7 3e 70 43 75 72 73 6f 72 20 3d 20 70 43 75 72 2d  >pCursor = pCur-
138e8 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20  >pNext;.    }.  
138e9 20 20 69 66 28 20 70 43 75 72 2d 3e 70 4e 65 78    if( pCur->pNex
138ea 74 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d  t ){.      pCur-
138eb 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20  >pNext->pPrev = 
138ec 70 43 75 72 2d 3e 70 50 72 65 76 3b 0a 20 20 20  pCur->pPrev;.   
138ed 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20   }.    for(i=0; 
138ee 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b 20  i<=pCur->iPage; 
138ef 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65  i++){.      rele
138f0 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70  asePage(pCur->ap
138f1 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a  Page[i]);.    }.
138f2 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49      unlockBtreeI
138f3 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20  fUnused(pBt);.  
138f4 20 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72    invalidateOver
138f5 66 6c 6f 77 43 61 63 68 65 28 70 43 75 72 29 3b  flowCache(pCur);
138f6 0a 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f  .    /* sqlite3_
138f7 66 72 65 65 28 70 43 75 72 29 3b 20 2a 2f 0a 20  free(pCur); */. 
138f8 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
138f9 65 61 76 65 28 70 42 74 72 65 65 29 3b 0a 20 20  eave(pBtree);.  
138fa 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
138fb 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  E_OK;.}../*.** M
138fc 61 6b 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20  ake a temporary 
138fd 63 75 72 73 6f 72 20 62 79 20 66 69 6c 6c 69 6e  cursor by fillin
138fe 67 20 69 6e 20 74 68 65 20 66 69 65 6c 64 73 20  g in the fields 
138ff 6f 66 20 70 54 65 6d 70 43 75 72 2e 0a 2a 2a 20  of pTempCur..** 
13900 54 68 65 20 74 65 6d 70 6f 72 61 72 79 20 63 75  The temporary cu
13901 72 73 6f 72 20 69 73 20 6e 6f 74 20 6f 6e 20 74  rsor is not on t
13902 68 65 20 63 75 72 73 6f 72 20 6c 69 73 74 20 66  he cursor list f
13903 6f 72 20 74 68 65 20 42 74 72 65 65 2e 0a 2a 2f  or the Btree..*/
13904 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
13905 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
13906 65 47 65 74 54 65 6d 70 43 75 72 73 6f 72 28 42  eGetTempCursor(B
13907 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 42  tCursor *pCur, B
13908 74 43 75 72 73 6f 72 20 2a 70 54 65 6d 70 43 75  tCursor *pTempCu
13909 72 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61  r){.  int i;.  a
1390a 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
1390b 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
1390c 0a 20 20 6d 65 6d 63 70 79 28 70 54 65 6d 70 43  .  memcpy(pTempC
1390d 75 72 2c 20 70 43 75 72 2c 20 73 69 7a 65 6f 66  ur, pCur, sizeof
1390e 28 42 74 43 75 72 73 6f 72 29 29 3b 0a 20 20 70  (BtCursor));.  p
1390f 54 65 6d 70 43 75 72 2d 3e 70 4e 65 78 74 20 3d  TempCur->pNext =
13910 20 30 3b 0a 20 20 70 54 65 6d 70 43 75 72 2d 3e   0;.  pTempCur->
13911 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 66 6f 72  pPrev = 0;.  for
13912 28 69 3d 30 3b 20 69 3c 3d 70 54 65 6d 70 43 75  (i=0; i<=pTempCu
13913 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a  r->iPage; i++){.
13914 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
13915 52 65 66 28 70 54 65 6d 70 43 75 72 2d 3e 61 70  Ref(pTempCur->ap
13916 50 61 67 65 5b 69 5d 2d 3e 70 44 62 50 61 67 65  Page[i]->pDbPage
13917 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
13918 44 65 6c 65 74 65 20 61 20 74 65 6d 70 6f 72 61  Delete a tempora
13919 72 79 20 63 75 72 73 6f 72 20 73 75 63 68 20 61  ry cursor such a
1391a 73 20 77 61 73 20 6d 61 64 65 20 62 79 20 74 68  s was made by th
1391b 65 20 43 72 65 61 74 65 54 65 6d 70 6f 72 61 72  e CreateTemporar
1391c 79 43 75 72 73 6f 72 28 29 0a 2a 2a 20 66 75 6e  yCursor().** fun
1391d 63 74 69 6f 6e 20 61 62 6f 76 65 2e 0a 2a 2f 0a  ction above..*/.
1391e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1391f 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
13920 52 65 6c 65 61 73 65 54 65 6d 70 43 75 72 73 6f  ReleaseTempCurso
13921 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  r(BtCursor *pCur
13922 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73  ){.  int i;.  as
13923 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
13924 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
13925 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 43    for(i=0; i<=pC
13926 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b  ur->iPage; i++){
13927 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65  .    sqlite3Page
13928 72 55 6e 72 65 66 28 70 43 75 72 2d 3e 61 70 50  rUnref(pCur->apP
13929 61 67 65 5b 69 5d 2d 3e 70 44 62 50 61 67 65 29  age[i]->pDbPage)
1392a 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  ;.  }.}../*.** M
1392b 61 6b 65 20 73 75 72 65 20 74 68 65 20 42 74 43  ake sure the BtC
1392c 75 72 73 6f 72 2a 20 67 69 76 65 6e 20 69 6e 20  ursor* given in 
1392d 74 68 65 20 61 72 67 75 6d 65 6e 74 20 68 61 73  the argument has
1392e 20 61 20 76 61 6c 69 64 0a 2a 2a 20 42 74 43 75   a valid.** BtCu
1392f 72 73 6f 72 2e 69 6e 66 6f 20 73 74 72 75 63 74  rsor.info struct
13930 75 72 65 2e 20 20 49 66 20 69 74 20 69 73 20 6e  ure.  If it is n
13931 6f 74 20 61 6c 72 65 61 64 79 20 76 61 6c 69 64  ot already valid
13932 2c 20 63 61 6c 6c 0a 2a 2a 20 73 71 6c 69 74 65  , call.** sqlite
13933 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28  3BtreeParseCell(
13934 29 20 74 6f 20 66 69 6c 6c 20 69 74 20 69 6e 2e  ) to fill it in.
13935 0a 2a 2a 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e  .**.** BtCursor.
13936 69 6e 66 6f 20 69 73 20 61 20 63 61 63 68 65 20  info is a cache 
13937 6f 66 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69  of the informati
13938 6f 6e 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e  on in the curren
13939 74 20 63 65 6c 6c 2e 0a 2a 2a 20 55 73 69 6e 67  t cell..** Using
1393a 20 74 68 69 73 20 63 61 63 68 65 20 72 65 64 75   this cache redu
1393b 63 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ces the number o
1393c 66 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74  f calls to sqlit
1393d 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c  e3BtreeParseCell
1393e 28 29 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 37 2d 30  ()..**.** 2007-0
1393f 36 2d 32 35 3a 20 20 54 68 65 72 65 20 69 73 20  6-25:  There is 
13940 61 20 62 75 67 20 69 6e 20 73 6f 6d 65 20 76 65  a bug in some ve
13941 72 73 69 6f 6e 73 20 6f 66 20 4d 53 56 43 20 74  rsions of MSVC t
13942 68 61 74 20 63 61 75 73 65 20 74 68 65 0a 2a 2a  hat cause the.**
13943 20 63 6f 6d 70 69 6c 65 72 20 74 6f 20 63 72 61   compiler to cra
13944 73 68 20 77 68 65 6e 20 67 65 74 43 65 6c 6c 49  sh when getCellI
13945 6e 66 6f 28 29 20 69 73 20 69 6d 70 6c 65 6d 65  nfo() is impleme
13946 6e 74 65 64 20 61 73 20 61 20 6d 61 63 72 6f 2e  nted as a macro.
13947 0a 2a 2a 20 42 75 74 20 74 68 65 72 65 20 69 73  .** But there is
13948 20 61 20 6d 65 61 73 75 72 65 61 62 6c 65 20 73   a measureable s
13949 70 65 65 64 20 61 64 76 61 6e 74 61 67 65 20 74  peed advantage t
1394a 6f 20 75 73 69 6e 67 20 74 68 65 20 6d 61 63 72  o using the macr
1394b 6f 20 6f 6e 20 67 63 63 0a 2a 2a 20 28 77 68 65  o on gcc.** (whe
1394c 6e 20 6c 65 73 73 20 63 6f 6d 70 69 6c 65 72 20  n less compiler 
1394d 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 6c 69  optimizations li
1394e 6b 65 20 2d 4f 73 20 6f 72 20 2d 4f 30 20 61 72  ke -Os or -O0 ar
1394f 65 20 75 73 65 64 20 61 6e 64 20 74 68 65 0a 2a  e used and the.*
13950 2a 20 63 6f 6d 70 69 6c 65 72 20 69 73 20 6e 6f  * compiler is no
13951 74 20 64 6f 69 6e 67 20 61 67 72 65 73 73 69 76  t doing agressiv
13952 65 20 69 6e 6c 69 6e 69 6e 67 2e 29 20 20 53 6f  e inlining.)  So
13953 20 77 65 20 75 73 65 20 61 20 72 65 61 6c 20 66   we use a real f
13954 75 6e 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 4d  unction.** for M
13955 53 56 43 20 61 6e 64 20 61 20 6d 61 63 72 6f 20  SVC and a macro 
13956 66 6f 72 20 65 76 65 72 79 74 68 69 6e 67 20 65  for everything e
13957 6c 73 65 2e 20 20 54 69 63 6b 65 74 20 23 32 34  lse.  Ticket #24
13958 35 37 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e  57..*/.#ifndef N
13959 44 45 42 55 47 0a 20 20 73 74 61 74 69 63 20 76  DEBUG.  static v
1395a 6f 69 64 20 61 73 73 65 72 74 43 65 6c 6c 49 6e  oid assertCellIn
1395b 66 6f 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  fo(BtCursor *pCu
1395c 72 29 7b 0a 20 20 20 20 43 65 6c 6c 49 6e 66 6f  r){.    CellInfo
1395d 20 69 6e 66 6f 3b 0a 20 20 20 20 69 6e 74 20 69   info;.    int i
1395e 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 69 50 61  Page = pCur->iPa
1395f 67 65 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26  ge;.    memset(&
13960 69 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28  info, 0, sizeof(
13961 69 6e 66 6f 29 29 3b 0a 20 20 20 20 73 71 6c 69  info));.    sqli
13962 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c  te3BtreeParseCel
13963 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69  l(pCur->apPage[i
13964 50 61 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69 49  Page], pCur->aiI
13965 64 78 5b 69 50 61 67 65 5d 2c 20 26 69 6e 66 6f  dx[iPage], &info
13966 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d  );.    assert( m
13967 65 6d 63 6d 70 28 26 69 6e 66 6f 2c 20 26 70 43  emcmp(&info, &pC
13968 75 72 2d 3e 69 6e 66 6f 2c 20 73 69 7a 65 6f 66  ur->info, sizeof
13969 28 69 6e 66 6f 29 29 3d 3d 30 20 29 3b 0a 20 20  (info))==0 );.  
1396a 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e  }.#else.  #defin
1396b 65 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f  e assertCellInfo
1396c 28 78 29 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  (x).#endif.#ifde
1396d 66 20 5f 4d 53 43 5f 56 45 52 0a 20 20 2f 2a 20  f _MSC_VER.  /* 
1396e 55 73 65 20 61 20 72 65 61 6c 20 66 75 6e 63 74  Use a real funct
1396f 69 6f 6e 20 69 6e 20 4d 53 56 43 20 74 6f 20 77  ion in MSVC to w
13970 6f 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 73 20  ork around bugs 
13971 69 6e 20 74 68 61 74 20 63 6f 6d 70 69 6c 65 72  in that compiler
13972 2e 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 76 6f  . */.  static vo
13973 69 64 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 42  id getCellInfo(B
13974 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a  tCursor *pCur){.
13975 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e      if( pCur->in
13976 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b 0a 20  fo.nSize==0 ){. 
13977 20 20 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d       int iPage =
13978 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20   pCur->iPage;.  
13979 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1397a 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e  ParseCell(pCur->
1397b 61 70 50 61 67 65 5b 69 50 61 67 65 5d 2c 70 43  apPage[iPage],pC
1397c 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 5d  ur->aiIdx[iPage]
1397d 2c 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a 20  ,&pCur->info);. 
1397e 20 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64       pCur->valid
1397f 4e 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 7d 65  NKey = 1;.    }e
13980 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72  lse{.      asser
13981 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b  tCellInfo(pCur);
13982 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65  .    }.  }.#else
13983 20 2f 2a 20 69 66 20 6e 6f 74 20 5f 4d 53 43 5f   /* if not _MSC_
13984 56 45 52 20 2a 2f 0a 20 20 2f 2a 20 55 73 65 20  VER */.  /* Use 
13985 61 20 6d 61 63 72 6f 20 69 6e 20 61 6c 6c 20 6f  a macro in all o
13986 74 68 65 72 20 63 6f 6d 70 69 6c 65 72 73 20 73  ther compilers s
13987 6f 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74  o that the funct
13988 69 6f 6e 20 69 73 20 69 6e 6c 69 6e 65 64 20 2a  ion is inlined *
13989 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 43 65 6c  /.#define getCel
1398a 6c 49 6e 66 6f 28 70 43 75 72 29 20 20 20 20 20  lInfo(pCur)     
1398b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1398c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1398d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1398e 20 5c 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 69   \.  if( pCur->i
1398f 6e 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b 20  nfo.nSize==0 ){ 
13990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13991 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13992 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13993 20 20 5c 0a 20 20 20 20 69 6e 74 20 69 50 61 67    \.    int iPag
13994 65 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 3b  e = pCur->iPage;
13995 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13996 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13997 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13998 20 20 20 5c 0a 20 20 20 20 73 71 6c 69 74 65 33     \.    sqlite3
13999 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70  BtreeParseCell(p
1399a 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67  Cur->apPage[iPag
1399b 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 78 5b 69  e],pCur->aiIdx[i
1399c 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e 69 6e 66  Page],&pCur->inf
1399d 6f 29 3b 20 5c 0a 20 20 20 20 70 43 75 72 2d 3e  o); \.    pCur->
1399e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 20 20  validNKey = 1;  
1399f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
139a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
139a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
139a2 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20       \.  }else{ 
139a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
139a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
139a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
139a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
139a7 20 20 20 20 20 20 5c 0a 20 20 20 20 61 73 73 65        \.    asse
139a8 72 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29  rtCellInfo(pCur)
139a9 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
139aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
139ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
139ac 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65 6e         \.  }.#en
139ad 64 69 66 20 2f 2a 20 5f 4d 53 43 5f 56 45 52 20  dif /* _MSC_VER 
139ae 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70  */../*.** Set *p
139af 53 69 7a 65 20 74 6f 20 74 68 65 20 73 69 7a 65  Size to the size
139b0 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 6e   of the buffer n
139b1 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 74 68  eeded to hold th
139b2 65 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 74 68  e value of.** th
139b3 65 20 6b 65 79 20 66 6f 72 20 74 68 65 20 63 75  e key for the cu
139b4 72 72 65 6e 74 20 65 6e 74 72 79 2e 20 20 49 66  rrent entry.  If
139b5 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6e   the cursor is n
139b6 6f 74 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 74  ot pointing.** t
139b7 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72 79 2c  o a valid entry,
139b8 20 2a 70 53 69 7a 65 20 69 73 20 73 65 74 20 74   *pSize is set t
139b9 6f 20 30 2e 20 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  o 0. .**.** For 
139ba 61 20 74 61 62 6c 65 20 77 69 74 68 20 74 68 65  a table with the
139bb 20 49 4e 54 4b 45 59 20 66 6c 61 67 20 73 65 74   INTKEY flag set
139bc 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  , this routine r
139bd 65 74 75 72 6e 73 20 74 68 65 20 6b 65 79 0a 2a  eturns the key.*
139be 2a 20 69 74 73 65 6c 66 2c 20 6e 6f 74 20 74 68  * itself, not th
139bf 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
139c0 73 20 69 6e 20 74 68 65 20 6b 65 79 2e 0a 2a 2f  s in the key..*/
139c1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
139c2 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
139c3 4b 65 79 53 69 7a 65 28 42 74 43 75 72 73 6f 72  KeySize(BtCursor
139c4 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 53 69   *pCur, i64 *pSi
139c5 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  ze){.  int rc;..
139c6 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
139c7 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
139c8 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f   );.  rc = resto
139c9 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  reCursorPosition
139ca 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63  (pCur);.  if( rc
139cb 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
139cc 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
139cd 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
139ce 49 4e 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d  INVALID || pCur-
139cf 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
139d0 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 69 66 28  VALID );.    if(
139d1 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
139d2 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b  URSOR_INVALID ){
139d3 0a 20 20 20 20 20 20 2a 70 53 69 7a 65 20 3d 20  .      *pSize = 
139d4 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  0;.    }else{.  
139d5 20 20 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28      getCellInfo(
139d6 70 43 75 72 29 3b 0a 20 20 20 20 20 20 2a 70 53  pCur);.      *pS
139d7 69 7a 65 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f  ize = pCur->info
139d8 2e 6e 4b 65 79 3b 0a 20 20 20 20 7d 0a 20 20 7d  .nKey;.    }.  }
139d9 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
139da 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70 53 69 7a  ./*.** Set *pSiz
139db 65 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20  e to the number 
139dc 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61  of bytes of data
139dd 20 69 6e 20 74 68 65 20 65 6e 74 72 79 20 74 68   in the entry th
139de 65 0a 2a 2a 20 63 75 72 73 6f 72 20 63 75 72 72  e.** cursor curr
139df 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f 2e  ently points to.
139e0 20 20 41 6c 77 61 79 73 20 72 65 74 75 72 6e 20    Always return 
139e1 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 46 61  SQLITE_OK..** Fa
139e2 69 6c 75 72 65 20 69 73 20 6e 6f 74 20 70 6f 73  ilure is not pos
139e3 73 69 62 6c 65 2e 20 20 49 66 20 74 68 65 20 63  sible.  If the c
139e4 75 72 73 6f 72 20 69 73 20 6e 6f 74 20 63 75 72  ursor is not cur
139e5 72 65 6e 74 6c 79 0a 2a 2a 20 70 6f 69 6e 74 69  rently.** pointi
139e6 6e 67 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 28  ng to an entry (
139e7 77 68 69 63 68 20 63 61 6e 20 68 61 70 70 65 6e  which can happen
139e8 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69  , for example, i
139e9 66 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73  f.** the databas
139ea 65 20 69 73 20 65 6d 70 74 79 29 20 74 68 65 6e  e is empty) then
139eb 20 2a 70 53 69 7a 65 20 69 73 20 73 65 74 20 74   *pSize is set t
139ec 6f 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  o 0..*/.SQLITE_P
139ed 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
139ee 65 33 42 74 72 65 65 44 61 74 61 53 69 7a 65 28  e3BtreeDataSize(
139ef 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
139f0 75 33 32 20 2a 70 53 69 7a 65 29 7b 0a 20 20 69  u32 *pSize){.  i
139f1 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74  nt rc;..  assert
139f2 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
139f3 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63  ex(pCur) );.  rc
139f4 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72   = restoreCursor
139f5 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a  Position(pCur);.
139f6 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
139f7 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72  _OK ){.    asser
139f8 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  t( pCur->eState=
139f9 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20  =CURSOR_INVALID 
139fa 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  || pCur->eState=
139fb 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b  =CURSOR_VALID );
139fc 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 65  .    if( pCur->e
139fd 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e  State==CURSOR_IN
139fe 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 2f  VALID ){.      /
139ff 2a 20 4e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 61  * Not pointing a
13a00 74 20 61 20 76 61 6c 69 64 20 65 6e 74 72 79 20  t a valid entry 
13a01 2d 20 73 65 74 20 2a 70 53 69 7a 65 20 74 6f 20  - set *pSize to 
13a02 30 2e 20 2a 2f 0a 20 20 20 20 20 20 2a 70 53 69  0. */.      *pSi
13a03 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  ze = 0;.    }els
13a04 65 7b 0a 20 20 20 20 20 20 67 65 74 43 65 6c 6c  e{.      getCell
13a05 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 20 20  Info(pCur);.    
13a06 20 20 2a 70 53 69 7a 65 20 3d 20 70 43 75 72 2d    *pSize = pCur-
13a07 3e 69 6e 66 6f 2e 6e 44 61 74 61 3b 0a 20 20 20  >info.nData;.   
13a08 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
13a09 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76  rc;.}../*.** Giv
13a0a 65 6e 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62  en the page numb
13a0b 65 72 20 6f 66 20 61 6e 20 6f 76 65 72 66 6c 6f  er of an overflo
13a0c 77 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61  w page in the da
13a0d 74 61 62 61 73 65 20 28 70 61 72 61 6d 65 74 65  tabase (paramete
13a0e 72 0a 2a 2a 20 6f 76 66 6c 29 2c 20 74 68 69 73  r.** ovfl), this
13a0f 20 66 75 6e 63 74 69 6f 6e 20 66 69 6e 64 73 20   function finds 
13a10 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  the page number 
13a11 6f 66 20 74 68 65 20 6e 65 78 74 20 70 61 67 65  of the next page
13a12 20 69 6e 20 74 68 65 20 0a 2a 2a 20 6c 69 6e 6b   in the .** link
13a13 65 64 20 6c 69 73 74 20 6f 66 20 6f 76 65 72 66  ed list of overf
13a14 6c 6f 77 20 70 61 67 65 73 2e 20 49 66 20 70 6f  low pages. If po
13a15 73 73 69 62 6c 65 2c 20 69 74 20 75 73 65 73 20  ssible, it uses 
13a16 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a  the auto-vacuum.
13a17 2a 2a 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 64  ** pointer-map d
13a18 61 74 61 20 69 6e 73 74 65 61 64 20 6f 66 20 72  ata instead of r
13a19 65 61 64 69 6e 67 20 74 68 65 20 63 6f 6e 74 65  eading the conte
13a1a 6e 74 20 6f 66 20 70 61 67 65 20 6f 76 66 6c 20  nt of page ovfl 
13a1b 74 6f 20 64 6f 20 73 6f 2e 20 0a 2a 2a 0a 2a 2a  to do so. .**.**
13a1c 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
13a1d 75 72 73 20 61 6e 20 53 51 4c 69 74 65 20 65 72  urs an SQLite er
13a1e 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
13a1f 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 3a  rned. Otherwise:
13a20 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 70 50  .**.** Unless pP
13a21 67 6e 6f 4e 65 78 74 20 69 73 20 4e 55 4c 4c 2c  gnoNext is NULL,
13a22 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
13a23 20 6f 66 20 74 68 65 20 6e 65 78 74 20 6f 76 65   of the next ove
13a24 72 66 6c 6f 77 20 0a 2a 2a 20 70 61 67 65 20 69  rflow .** page i
13a25 6e 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73  n the linked lis
13a26 74 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20  t is written to 
13a27 2a 70 50 67 6e 6f 4e 65 78 74 2e 20 49 66 20 70  *pPgnoNext. If p
13a28 61 67 65 20 6f 76 66 6c 0a 2a 2a 20 69 73 20 74  age ovfl.** is t
13a29 68 65 20 6c 61 73 74 20 70 61 67 65 20 69 6e 20  he last page in 
13a2a 69 74 73 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2c  its linked list,
13a2b 20 2a 70 50 67 6e 6f 4e 65 78 74 20 69 73 20 73   *pPgnoNext is s
13a2c 65 74 20 74 6f 20 7a 65 72 6f 2e 20 0a 2a 2a 0a  et to zero. .**.
13a2d 2a 2a 20 49 66 20 70 70 50 61 67 65 20 69 73 20  ** If ppPage is 
13a2e 6e 6f 74 20 4e 55 4c 4c 2c 20 2a 70 70 50 61 67  not NULL, *ppPag
13a2f 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  e is set to the 
13a30 4d 65 6d 50 61 67 65 2a 20 68 61 6e 64 6c 65 0a  MemPage* handle.
13a31 2a 2a 20 66 6f 72 20 70 61 67 65 20 6f 76 66 6c  ** for page ovfl
13a32 2e 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  . The underlying
13a33 20 70 61 67 65 72 20 70 61 67 65 20 6d 61 79 20   pager page may 
13a34 68 61 76 65 20 62 65 65 6e 20 72 65 71 75 65 73  have been reques
13a35 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20  ted.** with the 
13a36 6e 6f 43 6f 6e 74 65 6e 74 20 66 6c 61 67 20 73  noContent flag s
13a37 65 74 2c 20 73 6f 20 74 68 65 20 70 61 67 65 20  et, so the page 
13a38 64 61 74 61 20 61 63 63 65 73 73 61 62 6c 65 20  data accessable 
13a39 76 69 61 0a 2a 2a 20 74 68 69 73 20 68 61 6e 64  via.** this hand
13a3a 6c 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 74 72  le may not be tr
13a3b 75 73 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  usted..*/.static
13a3c 20 69 6e 74 20 67 65 74 4f 76 65 72 66 6c 6f 77   int getOverflow
13a3d 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64  Page(.  BtShared
13a3e 20 2a 70 42 74 2c 20 0a 20 20 50 67 6e 6f 20 6f   *pBt, .  Pgno o
13a3f 76 66 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  vfl,            
13a40 20 20 20 20 20 20 20 2f 2a 20 4f 76 65 72 66 6c         /* Overfl
13a41 6f 77 20 70 61 67 65 20 2a 2f 0a 20 20 4d 65 6d  ow page */.  Mem
13a42 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 20  Page **ppPage,  
13a43 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
13a44 3a 20 4d 65 6d 50 61 67 65 20 68 61 6e 64 6c 65  : MemPage handle
13a45 20 2a 2f 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e   */.  Pgno *pPgn
13a46 6f 4e 65 78 74 20 20 20 20 20 20 20 20 20 20 20  oNext           
13a47 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 65 78 74 20     /* OUT: Next 
13a48 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6e 75  overflow page nu
13a49 6d 62 65 72 20 2a 2f 0a 29 7b 0a 20 20 50 67 6e  mber */.){.  Pgn
13a4a 6f 20 6e 65 78 74 20 3d 20 30 3b 0a 20 20 69 6e  o next = 0;.  in
13a4b 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  t rc;..  assert(
13a4c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
13a4d 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
13a4e 29 3b 0a 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74  );.  /* One of t
13a4f 68 65 73 65 20 6d 75 73 74 20 6e 6f 74 20 62 65  hese must not be
13a50 20 4e 55 4c 4c 2e 20 4f 74 68 65 72 77 69 73 65   NULL. Otherwise
13a51 2c 20 77 68 79 20 63 61 6c 6c 20 74 68 69 73 20  , why call this 
13a52 66 75 6e 63 74 69 6f 6e 3f 20 2a 2f 0a 20 20 61  function? */.  a
13a53 73 73 65 72 74 28 70 70 50 61 67 65 20 7c 7c 20  ssert(ppPage || 
13a54 70 50 67 6e 6f 4e 65 78 74 29 3b 0a 0a 20 20 2f  pPgnoNext);..  /
13a55 2a 20 49 66 20 70 50 67 6e 6f 4e 65 78 74 20 69  * If pPgnoNext i
13a56 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 69  s NULL, then thi
13a57 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65  s function is be
13a58 69 6e 67 20 63 61 6c 6c 65 64 20 74 6f 20 6f 62  ing called to ob
13a59 74 61 69 6e 0a 20 20 2a 2a 20 61 20 4d 65 6d 50  tain.  ** a MemP
13a5a 61 67 65 2a 20 72 65 66 65 72 65 6e 63 65 20 6f  age* reference o
13a5b 6e 6c 79 2e 20 4e 6f 20 70 61 67 65 2d 64 61 74  nly. No page-dat
13a5c 61 20 69 73 20 72 65 71 75 69 72 65 64 20 69 6e  a is required in
13a5d 20 74 68 69 73 20 63 61 73 65 2e 0a 20 20 2a 2f   this case..  */
13a5e 0a 20 20 69 66 28 20 21 70 50 67 6e 6f 4e 65 78  .  if( !pPgnoNex
13a5f 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
13a60 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
13a61 61 67 65 28 70 42 74 2c 20 6f 76 66 6c 2c 20 70  age(pBt, ovfl, p
13a62 70 50 61 67 65 2c 20 31 29 3b 0a 20 20 7d 0a 0a  pPage, 1);.  }..
13a63 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
13a64 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
13a65 20 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e 64 20   /* Try to find 
13a66 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e  the next page in
13a67 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c 69   the overflow li
13a68 73 74 20 75 73 69 6e 67 20 74 68 65 0a 20 20 2a  st using the.  *
13a69 2a 20 61 75 74 6f 76 61 63 75 75 6d 20 70 6f 69  * autovacuum poi
13a6a 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 20  nter-map pages. 
13a6b 47 75 65 73 73 20 74 68 61 74 20 74 68 65 20 6e  Guess that the n
13a6c 65 78 74 20 70 61 67 65 20 69 6e 20 0a 20 20 2a  ext page in .  *
13a6d 2a 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c  * the overflow l
13a6e 69 73 74 20 69 73 20 70 61 67 65 20 6e 75 6d 62  ist is page numb
13a6f 65 72 20 28 6f 76 66 6c 2b 31 29 2e 20 49 66 20  er (ovfl+1). If 
13a70 74 68 61 74 20 67 75 65 73 73 20 74 75 72 6e 73  that guess turns
13a71 20 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62 65   .  ** out to be
13a72 20 77 72 6f 6e 67 2c 20 66 61 6c 6c 20 62 61 63   wrong, fall bac
13a73 6b 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74 68 65  k to loading the
13a74 20 64 61 74 61 20 6f 66 20 70 61 67 65 20 0a 20   data of page . 
13a75 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 76 66 6c 20   ** number ovfl 
13a76 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  to determine the
13a77 20 6e 65 78 74 20 70 61 67 65 20 6e 75 6d 62 65   next page numbe
13a78 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 42  r..  */.  if( pB
13a79 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b  t->autoVacuum ){
13a7a 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a  .    Pgno pgno;.
13a7b 20 20 20 20 50 67 6e 6f 20 69 47 75 65 73 73 20      Pgno iGuess 
13a7c 3d 20 6f 76 66 6c 2b 31 3b 0a 20 20 20 20 75 38  = ovfl+1;.    u8
13a7d 20 65 54 79 70 65 3b 0a 0a 20 20 20 20 77 68 69   eType;..    whi
13a7e 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47  le( PTRMAP_ISPAG
13a7f 45 28 70 42 74 2c 20 69 47 75 65 73 73 29 20 7c  E(pBt, iGuess) |
13a80 7c 20 69 47 75 65 73 73 3d 3d 50 45 4e 44 49 4e  | iGuess==PENDIN
13a81 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29  G_BYTE_PAGE(pBt)
13a82 20 29 7b 0a 20 20 20 20 20 20 69 47 75 65 73 73   ){.      iGuess
13a83 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  ++;.    }..    i
13a84 66 28 20 69 47 75 65 73 73 3c 3d 70 61 67 65 72  f( iGuess<=pager
13a85 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70  Pagecount(pBt->p
13a86 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20  Pager) ){.      
13a87 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70  rc = ptrmapGet(p
13a88 42 74 2c 20 69 47 75 65 73 73 2c 20 26 65 54 79  Bt, iGuess, &eTy
13a89 70 65 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 20 20  pe, &pgno);.    
13a8a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
13a8b 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
13a8c 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
13a8d 7d 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 70  }.      if( eTyp
13a8e 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  e==PTRMAP_OVERFL
13a8f 4f 57 32 20 26 26 20 70 67 6e 6f 3d 3d 6f 76 66  OW2 && pgno==ovf
13a90 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 65 78  l ){.        nex
13a91 74 20 3d 20 69 47 75 65 73 73 3b 0a 20 20 20 20  t = iGuess;.    
13a92 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65    }.    }.  }.#e
13a93 6e 64 69 66 0a 0a 20 20 69 66 28 20 6e 65 78 74  ndif..  if( next
13a94 3d 3d 30 20 7c 7c 20 70 70 50 61 67 65 20 29 7b  ==0 || ppPage ){
13a95 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  .    MemPage *pP
13a96 61 67 65 20 3d 20 30 3b 0a 0a 20 20 20 20 72 63  age = 0;..    rc
13a97 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
13a98 65 74 50 61 67 65 28 70 42 74 2c 20 6f 76 66 6c  etPage(pBt, ovfl
13a99 2c 20 26 70 50 61 67 65 2c 20 6e 65 78 74 21 3d  , &pPage, next!=
13a9a 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 72  0);.    assert(r
13a9b 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
13a9c 70 50 61 67 65 3d 3d 30 29 3b 0a 20 20 20 20 69  pPage==0);.    i
13a9d 66 28 20 6e 65 78 74 3d 3d 30 20 26 26 20 72 63  f( next==0 && rc
13a9e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
13a9f 20 20 20 20 20 6e 65 78 74 20 3d 20 67 65 74 34       next = get4
13aa0 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74  byte(pPage->aDat
13aa1 61 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  a);.    }..    i
13aa2 66 28 20 70 70 50 61 67 65 20 29 7b 0a 20 20 20  f( ppPage ){.   
13aa3 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50 61     *ppPage = pPa
13aa4 67 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ge;.    }else{. 
13aa5 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
13aa6 28 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20  (pPage);.    }. 
13aa7 20 7d 0a 20 20 2a 70 50 67 6e 6f 4e 65 78 74 20   }.  *pPgnoNext 
13aa8 3d 20 6e 65 78 74 3b 0a 0a 20 20 72 65 74 75 72  = next;..  retur
13aa9 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  n rc;.}../*.** C
13aaa 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 61 20  opy data from a 
13aab 62 75 66 66 65 72 20 74 6f 20 61 20 70 61 67 65  buffer to a page
13aac 2c 20 6f 72 20 66 72 6f 6d 20 61 20 70 61 67 65  , or from a page
13aad 20 74 6f 20 61 20 62 75 66 66 65 72 2e 0a 2a 2a   to a buffer..**
13aae 0a 2a 2a 20 70 50 61 79 6c 6f 61 64 20 69 73 20  .** pPayload is 
13aaf 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 64 61 74  a pointer to dat
13ab0 61 20 73 74 6f 72 65 64 20 6f 6e 20 64 61 74 61  a stored on data
13ab1 62 61 73 65 20 70 61 67 65 20 70 44 62 50 61 67  base page pDbPag
13ab2 65 2e 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e  e..** If argumen
13ab3 74 20 65 4f 70 20 69 73 20 66 61 6c 73 65 2c 20  t eOp is false, 
13ab4 74 68 65 6e 20 6e 42 79 74 65 20 62 79 74 65 73  then nByte bytes
13ab5 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f 70   of data are cop
13ab6 69 65 64 0a 2a 2a 20 66 72 6f 6d 20 70 50 61 79  ied.** from pPay
13ab7 6c 6f 61 64 20 74 6f 20 74 68 65 20 62 75 66 66  load to the buff
13ab8 65 72 20 70 6f 69 6e 74 65 64 20 61 74 20 62 79  er pointed at by
13ab9 20 70 42 75 66 2e 20 49 66 20 65 4f 70 20 69 73   pBuf. If eOp is
13aba 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 73   true,.** then s
13abb 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
13abc 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20  () is called on 
13abd 70 44 62 50 61 67 65 20 61 6e 64 20 6e 42 79 74  pDbPage and nByt
13abe 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 64 61  e bytes.** of da
13abf 74 61 20 61 72 65 20 63 6f 70 69 65 64 20 66 72  ta are copied fr
13ac0 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 70 42  om the buffer pB
13ac1 75 66 20 74 6f 20 70 50 61 79 6c 6f 61 64 2e 0a  uf to pPayload..
13ac2 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20  **.** SQLITE_OK 
13ac3 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73  is returned on s
13ac4 75 63 63 65 73 73 2c 20 6f 74 68 65 72 77 69 73  uccess, otherwis
13ac5 65 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e  e an error code.
13ac6 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
13ac7 6f 70 79 50 61 79 6c 6f 61 64 28 0a 20 20 76 6f  opyPayload(.  vo
13ac8 69 64 20 2a 70 50 61 79 6c 6f 61 64 2c 20 20 20  id *pPayload,   
13ac9 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
13aca 65 72 20 74 6f 20 70 61 67 65 20 64 61 74 61 20  er to page data 
13acb 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c  */.  void *pBuf,
13acc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
13acd 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 75 66  * Pointer to buf
13ace 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79  fer */.  int nBy
13acf 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  te,             
13ad0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
13ad1 62 79 74 65 73 20 74 6f 20 63 6f 70 79 20 2a 2f  bytes to copy */
13ad2 0a 20 20 69 6e 74 20 65 4f 70 2c 20 20 20 20 20  .  int eOp,     
13ad3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
13ad4 30 20 2d 3e 20 63 6f 70 79 20 66 72 6f 6d 20 70  0 -> copy from p
13ad5 61 67 65 2c 20 31 20 2d 3e 20 63 6f 70 79 20 74  age, 1 -> copy t
13ad6 6f 20 70 61 67 65 20 2a 2f 0a 20 20 44 62 50 61  o page */.  DbPa
13ad7 67 65 20 2a 70 44 62 50 61 67 65 20 20 20 20 20  ge *pDbPage     
13ad8 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f        /* Page co
13ad9 6e 74 61 69 6e 69 6e 67 20 70 50 61 79 6c 6f 61  ntaining pPayloa
13ada 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 65 4f  d */.){.  if( eO
13adb 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 70 79  p ){.    /* Copy
13adc 20 64 61 74 61 20 66 72 6f 6d 20 62 75 66 66 65   data from buffe
13add 72 20 74 6f 20 70 61 67 65 20 28 61 20 77 72 69  r to page (a wri
13ade 74 65 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a 2f  te operation) */
13adf 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71  .    int rc = sq
13ae0 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
13ae1 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66  pDbPage);.    if
13ae2 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
13ae3 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
13ae4 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65  rc;.    }.    me
13ae5 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 2c 20 70  mcpy(pPayload, p
13ae6 42 75 66 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d  Buf, nByte);.  }
13ae7 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f 70  else{.    /* Cop
13ae8 79 20 64 61 74 61 20 66 72 6f 6d 20 70 61 67 65  y data from page
13ae9 20 74 6f 20 62 75 66 66 65 72 20 28 61 20 72 65   to buffer (a re
13aea 61 64 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a 2f  ad operation) */
13aeb 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 42 75 66  .    memcpy(pBuf
13aec 2c 20 70 50 61 79 6c 6f 61 64 2c 20 6e 42 79 74  , pPayload, nByt
13aed 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  e);.  }.  return
13aee 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
13aef 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
13af0 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  on is used to re
13af1 61 64 20 6f 72 20 6f 76 65 72 77 72 69 74 65 20  ad or overwrite 
13af2 70 61 79 6c 6f 61 64 20 69 6e 66 6f 72 6d 61 74  payload informat
13af3 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 20 65  ion.** for the e
13af4 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 70 43  ntry that the pC
13af5 75 72 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69  ur cursor is poi
13af6 6e 74 69 6e 67 20 74 6f 2e 20 49 66 20 74 68 65  nting to. If the
13af7 20 65 4f 70 0a 2a 2a 20 70 61 72 61 6d 65 74 65   eOp.** paramete
13af8 72 20 69 73 20 30 2c 20 74 68 69 73 20 69 73 20  r is 0, this is 
13af9 61 20 72 65 61 64 20 6f 70 65 72 61 74 69 6f 6e  a read operation
13afa 20 28 64 61 74 61 20 63 6f 70 69 65 64 20 69 6e   (data copied in
13afb 74 6f 0a 2a 2a 20 62 75 66 66 65 72 20 70 42 75  to.** buffer pBu
13afc 66 29 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 6e  f). If it is non
13afd 2d 7a 65 72 6f 2c 20 61 20 77 72 69 74 65 20 28  -zero, a write (
13afe 64 61 74 61 20 63 6f 70 69 65 64 20 66 72 6f 6d  data copied from
13aff 0a 2a 2a 20 62 75 66 66 65 72 20 70 42 75 66 29  .** buffer pBuf)
13b00 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 6f 74 61 6c 20  ..**.** A total 
13b01 6f 66 20 22 61 6d 74 22 20 62 79 74 65 73 20 61  of "amt" bytes a
13b02 72 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74  re read or writt
13b03 65 6e 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20  en beginning at 
13b04 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 20 44 61 74  "offset"..** Dat
13b05 61 20 69 73 20 72 65 61 64 20 74 6f 20 6f 72 20  a is read to or 
13b06 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20  from the buffer 
13b07 70 42 75 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  pBuf..**.** This
13b08 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f   routine does no
13b09 74 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63  t make a distinc
13b0a 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 6b 65 79  tion between key
13b0b 20 61 6e 64 20 64 61 74 61 2e 0a 2a 2a 20 49 74   and data..** It
13b0c 20 6a 75 73 74 20 72 65 61 64 73 20 6f 72 20 77   just reads or w
13b0d 72 69 74 65 73 20 62 79 74 65 73 20 66 72 6f 6d  rites bytes from
13b0e 20 74 68 65 20 70 61 79 6c 6f 61 64 20 61 72 65   the payload are
13b0f 61 2e 20 20 44 61 74 61 20 6d 69 67 68 74 20 0a  a.  Data might .
13b10 2a 2a 20 61 70 70 65 61 72 20 6f 6e 20 74 68 65  ** appear on the
13b11 20 6d 61 69 6e 20 70 61 67 65 20 6f 72 20 62 65   main page or be
13b12 20 73 63 61 74 74 65 72 65 64 20 6f 75 74 20 6f   scattered out o
13b13 6e 20 6d 75 6c 74 69 70 6c 65 20 6f 76 65 72 66  n multiple overf
13b14 6c 6f 77 20 0a 2a 2a 20 70 61 67 65 73 2e 0a 2a  low .** pages..*
13b15 2a 0a 2a 2a 20 49 66 20 74 68 65 20 42 74 43 75  *.** If the BtCu
13b16 72 73 6f 72 2e 69 73 49 6e 63 72 62 6c 6f 62 48  rsor.isIncrblobH
13b17 61 6e 64 6c 65 20 66 6c 61 67 20 69 73 20 73 65  andle flag is se
13b18 74 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72 65  t, and the curre
13b19 6e 74 0a 2a 2a 20 63 75 72 73 6f 72 20 65 6e 74  nt.** cursor ent
13b1a 72 79 20 75 73 65 73 20 6f 6e 65 20 6f 72 20 6d  ry uses one or m
13b1b 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  ore overflow pag
13b1c 65 73 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  es, this functio
13b1d 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 73 20 73  n.** allocates s
13b1e 70 61 63 65 20 66 6f 72 20 61 6e 64 20 6c 61 7a  pace for and laz
13b1f 69 6c 79 20 70 6f 70 6c 75 61 74 65 73 20 74 68  ily popluates th
13b20 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d  e overflow page-
13b21 6c 69 73 74 20 0a 2a 2a 20 63 61 63 68 65 20 61  list .** cache a
13b22 72 72 61 79 20 28 42 74 43 75 72 73 6f 72 2e 61  rray (BtCursor.a
13b23 4f 76 65 72 66 6c 6f 77 29 2e 20 53 75 62 73 65  Overflow). Subse
13b24 71 75 65 6e 74 20 63 61 6c 6c 73 20 75 73 65 20  quent calls use 
13b25 74 68 69 73 0a 2a 2a 20 63 61 63 68 65 20 74 6f  this.** cache to
13b26 20 6d 61 6b 65 20 73 65 65 6b 69 6e 67 20 74 6f   make seeking to
13b27 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 6f 66   the supplied of
13b28 66 73 65 74 20 6d 6f 72 65 20 65 66 66 69 63 69  fset more effici
13b29 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20  ent..**.** Once 
13b2a 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  an overflow page
13b2b 2d 6c 69 73 74 20 63 61 63 68 65 20 68 61 73 20  -list cache has 
13b2c 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 20  been allocated, 
13b2d 69 74 20 6d 61 79 20 62 65 0a 2a 2a 20 69 6e 76  it may be.** inv
13b2e 61 6c 69 64 61 74 65 64 20 69 66 20 73 6f 6d 65  alidated if some
13b2f 20 6f 74 68 65 72 20 63 75 72 73 6f 72 20 77 72   other cursor wr
13b30 69 74 65 73 20 74 6f 20 74 68 65 20 73 61 6d 65  ites to the same
13b31 20 74 61 62 6c 65 2c 20 6f 72 20 69 66 0a 2a 2a   table, or if.**
13b32 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6d   the cursor is m
13b33 6f 76 65 64 20 74 6f 20 61 20 64 69 66 66 65 72  oved to a differ
13b34 65 6e 74 20 72 6f 77 2e 20 41 64 64 69 74 69 6f  ent row. Additio
13b35 6e 61 6c 6c 79 2c 20 69 6e 20 61 75 74 6f 2d 76  nally, in auto-v
13b36 61 63 75 75 6d 0a 2a 2a 20 6d 6f 64 65 2c 20 74  acuum.** mode, t
13b37 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 76 65  he following eve
13b38 6e 74 73 20 6d 61 79 20 69 6e 76 61 6c 69 64 61  nts may invalida
13b39 74 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70  te an overflow p
13b3a 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2e 0a  age-list cache..
13b3b 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 6e 63  **.**   * An inc
13b3c 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 2c  remental vacuum,
13b3d 0a 2a 2a 20 20 20 2a 20 41 20 63 6f 6d 6d 69 74  .**   * A commit
13b3e 20 69 6e 20 61 75 74 6f 5f 76 61 63 75 75 6d 3d   in auto_vacuum=
13b3f 22 66 75 6c 6c 22 20 6d 6f 64 65 2c 0a 2a 2a 20  "full" mode,.** 
13b40 20 20 2a 20 43 72 65 61 74 69 6e 67 20 61 20 74    * Creating a t
13b41 61 62 6c 65 20 28 6d 61 79 20 72 65 71 75 69 72  able (may requir
13b42 65 20 6d 6f 76 69 6e 67 20 61 6e 20 6f 76 65 72  e moving an over
13b43 66 6c 6f 77 20 70 61 67 65 29 2e 0a 2a 2f 0a 73  flow page)..*/.s
13b44 74 61 74 69 63 20 69 6e 74 20 61 63 63 65 73 73  tatic int access
13b45 50 61 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 72  Payload(.  BtCur
13b46 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20  sor *pCur,      
13b47 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69  /* Cursor pointi
13b48 6e 67 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 72  ng to entry to r
13b49 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e  ead from */.  in
13b4a 74 20 6f 66 66 73 65 74 2c 20 20 20 20 20 20 20  t offset,       
13b4b 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64     /* Begin read
13b4c 69 6e 67 20 74 68 69 73 20 66 61 72 20 69 6e 74  ing this far int
13b4d 6f 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 69  o payload */.  i
13b4e 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20  nt amt,         
13b4f 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 69 73      /* Read this
13b50 20 6d 61 6e 79 20 62 79 74 65 73 20 2a 2f 0a 20   many bytes */. 
13b51 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
13b52 70 42 75 66 2c 20 2f 2a 20 57 72 69 74 65 20 74  pBuf, /* Write t
13b53 68 65 20 62 79 74 65 73 20 69 6e 74 6f 20 74 68  he bytes into th
13b54 69 73 20 62 75 66 66 65 72 20 2a 2f 20 0a 20 20  is buffer */ .  
13b55 69 6e 74 20 73 6b 69 70 4b 65 79 2c 20 20 20 20  int skipKey,    
13b56 20 20 20 20 20 2f 2a 20 6f 66 66 73 65 74 20 62       /* offset b
13b57 65 67 69 6e 73 20 61 74 20 64 61 74 61 20 69 66  egins at data if
13b58 20 74 68 69 73 20 69 73 20 74 72 75 65 20 2a 2f   this is true */
13b59 0a 20 20 69 6e 74 20 65 4f 70 20 20 20 20 20 20  .  int eOp      
13b5a 20 20 20 20 20 20 20 20 2f 2a 20 7a 65 72 6f 20          /* zero 
13b5b 74 6f 20 72 65 61 64 2e 20 6e 6f 6e 2d 7a 65 72  to read. non-zer
13b5c 6f 20 74 6f 20 77 72 69 74 65 2e 20 2a 2f 0a 29  o to write. */.)
13b5d 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  {.  unsigned cha
13b5e 72 20 2a 61 50 61 79 6c 6f 61 64 3b 0a 20 20 69  r *aPayload;.  i
13b5f 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
13b60 4b 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20  K;.  u32 nKey;. 
13b61 20 69 6e 74 20 69 49 64 78 20 3d 20 30 3b 0a 20   int iIdx = 0;. 
13b62 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20   MemPage *pPage 
13b63 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70  = pCur->apPage[p
13b64 43 75 72 2d 3e 69 50 61 67 65 5d 3b 20 2f 2a 20  Cur->iPage]; /* 
13b65 42 74 72 65 65 20 70 61 67 65 20 6f 66 20 63 75  Btree page of cu
13b66 72 72 65 6e 74 20 65 6e 74 72 79 20 2a 2f 0a 20  rrent entry */. 
13b67 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20   BtShared *pBt; 
13b68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13b69 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
13b6a 42 74 72 65 65 20 74 68 69 73 20 63 75 72 73 6f  Btree this curso
13b6b 72 20 62 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a  r belongs to */.
13b6c 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
13b6d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
13b6e 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
13b6f 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73  OR_VALID );.  as
13b70 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64  sert( pCur->aiId
13b71 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70  x[pCur->iPage]<p
13b72 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20  Page->nCell );. 
13b73 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 3e   assert( offset>
13b74 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
13b75 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
13b76 28 70 43 75 72 29 20 29 3b 0a 0a 20 20 67 65 74  (pCur) );..  get
13b77 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a  CellInfo(pCur);.
13b78 20 20 61 50 61 79 6c 6f 61 64 20 3d 20 70 43 75    aPayload = pCu
13b79 72 2d 3e 69 6e 66 6f 2e 70 43 65 6c 6c 20 2b 20  r->info.pCell + 
13b7a 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 48 65 61 64  pCur->info.nHead
13b7b 65 72 3b 0a 20 20 6e 4b 65 79 20 3d 20 28 70 50  er;.  nKey = (pP
13b7c 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3f 20 30 20  age->intKey ? 0 
13b7d 3a 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65  : pCur->info.nKe
13b7e 79 29 3b 0a 0a 20 20 69 66 28 20 73 6b 69 70 4b  y);..  if( skipK
13b7f 65 79 20 29 7b 0a 20 20 20 20 6f 66 66 73 65 74  ey ){.    offset
13b80 20 2b 3d 20 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20   += nKey;.  }.  
13b81 69 66 28 20 6f 66 66 73 65 74 2b 61 6d 74 20 3e  if( offset+amt >
13b82 20 6e 4b 65 79 2b 70 43 75 72 2d 3e 69 6e 66 6f   nKey+pCur->info
13b83 2e 6e 44 61 74 61 20 29 7b 0a 20 20 20 20 2f 2a  .nData ){.    /*
13b84 20 54 72 79 69 6e 67 20 74 6f 20 72 65 61 64 20   Trying to read 
13b85 6f 72 20 77 72 69 74 65 20 70 61 73 74 20 74 68  or write past th
13b86 65 20 65 6e 64 20 6f 66 20 74 68 65 20 64 61 74  e end of the dat
13b87 61 20 69 73 20 61 6e 20 65 72 72 6f 72 20 2a 2f  a is an error */
13b88 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
13b89 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
13b8a 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  .  }..  /* Check
13b8b 20 69 66 20 64 61 74 61 20 6d 75 73 74 20 62 65   if data must be
13b8c 20 72 65 61 64 2f 77 72 69 74 74 65 6e 20 74 6f   read/written to
13b8d 2f 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65 20  /from the btree 
13b8e 70 61 67 65 20 69 74 73 65 6c 66 2e 20 2a 2f 0a  page itself. */.
13b8f 20 20 69 66 28 20 6f 66 66 73 65 74 3c 70 43 75    if( offset<pCu
13b90 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29  r->info.nLocal )
13b91 7b 0a 20 20 20 20 69 6e 74 20 61 20 3d 20 61 6d  {.    int a = am
13b92 74 3b 0a 20 20 20 20 69 66 28 20 61 2b 6f 66 66  t;.    if( a+off
13b93 73 65 74 3e 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  set>pCur->info.n
13b94 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 61  Local ){.      a
13b95 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c   = pCur->info.nL
13b96 6f 63 61 6c 20 2d 20 6f 66 66 73 65 74 3b 0a 20  ocal - offset;. 
13b97 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 63 6f     }.    rc = co
13b98 70 79 50 61 79 6c 6f 61 64 28 26 61 50 61 79 6c  pyPayload(&aPayl
13b99 6f 61 64 5b 6f 66 66 73 65 74 5d 2c 20 70 42 75  oad[offset], pBu
13b9a 66 2c 20 61 2c 20 65 4f 70 2c 20 70 50 61 67 65  f, a, eOp, pPage
13b9b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
13b9c 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20  offset = 0;.    
13b9d 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 20 20 61  pBuf += a;.    a
13b9e 6d 74 20 2d 3d 20 61 3b 0a 20 20 7d 65 6c 73 65  mt -= a;.  }else
13b9f 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 2d 3d 20  {.    offset -= 
13ba0 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61  pCur->info.nLoca
13ba1 6c 3b 0a 20 20 7d 0a 0a 20 20 70 42 74 20 3d 20  l;.  }..  pBt = 
13ba2 70 43 75 72 2d 3e 70 42 74 3b 0a 20 20 69 66 28  pCur->pBt;.  if(
13ba3 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
13ba4 26 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 63  & amt>0 ){.    c
13ba5 6f 6e 73 74 20 69 6e 74 20 6f 76 66 6c 53 69 7a  onst int ovflSiz
13ba6 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53  e = pBt->usableS
13ba7 69 7a 65 20 2d 20 34 3b 20 20 2f 2a 20 42 79 74  ize - 4;  /* Byt
13ba8 65 73 20 63 6f 6e 74 65 6e 74 20 70 65 72 20 6f  es content per o
13ba9 76 66 6c 20 70 61 67 65 20 2a 2f 0a 20 20 20 20  vfl page */.    
13baa 50 67 6e 6f 20 6e 65 78 74 50 61 67 65 3b 0a 0a  Pgno nextPage;..
13bab 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 67      nextPage = g
13bac 65 74 34 62 79 74 65 28 26 61 50 61 79 6c 6f 61  et4byte(&aPayloa
13bad 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f  d[pCur->info.nLo
13bae 63 61 6c 5d 29 3b 0a 0a 23 69 66 6e 64 65 66 20  cal]);..#ifndef 
13baf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52  SQLITE_OMIT_INCR
13bb0 42 4c 4f 42 0a 20 20 20 20 2f 2a 20 49 66 20 74  BLOB.    /* If t
13bb1 68 65 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e  he isIncrblobHan
13bb2 64 6c 65 20 66 6c 61 67 20 69 73 20 73 65 74 20  dle flag is set 
13bb3 61 6e 64 20 74 68 65 20 42 74 43 75 72 73 6f 72  and the BtCursor
13bb4 2e 61 4f 76 65 72 66 6c 6f 77 5b 5d 0a 20 20 20  .aOverflow[].   
13bb5 20 2a 2a 20 68 61 73 20 6e 6f 74 20 62 65 65 6e   ** has not been
13bb6 20 61 6c 6c 6f 63 61 74 65 64 2c 20 61 6c 6c 6f   allocated, allo
13bb7 63 61 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65  cate it now. The
13bb8 20 61 72 72 61 79 20 69 73 20 73 69 7a 65 64 20   array is sized 
13bb9 61 74 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 65 6e  at.    ** one en
13bba 74 72 79 20 66 6f 72 20 65 61 63 68 20 6f 76 65  try for each ove
13bbb 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68  rflow page in th
13bbc 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e  e overflow chain
13bbd 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 70 61 67  . The.    ** pag
13bbe 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
13bbf 66 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70  first overflow p
13bc0 61 67 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e  age is stored in
13bc1 20 61 4f 76 65 72 66 6c 6f 77 5b 30 5d 2c 0a 20   aOverflow[0],. 
13bc2 20 20 20 2a 2a 20 65 74 63 2e 20 41 20 76 61 6c     ** etc. A val
13bc3 75 65 20 6f 66 20 30 20 69 6e 20 74 68 65 20 61  ue of 0 in the a
13bc4 4f 76 65 72 66 6c 6f 77 5b 5d 20 61 72 72 61 79  Overflow[] array
13bc5 20 6d 65 61 6e 73 20 22 6e 6f 74 20 79 65 74 20   means "not yet 
13bc6 6b 6e 6f 77 6e 22 0a 20 20 20 20 2a 2a 20 28 74  known".    ** (t
13bc7 68 65 20 63 61 63 68 65 20 69 73 20 6c 61 7a 69  he cache is lazi
13bc8 6c 79 20 70 6f 70 75 6c 61 74 65 64 29 2e 0a 20  ly populated).. 
13bc9 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43     */.    if( pC
13bca 75 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61  ur->isIncrblobHa
13bcb 6e 64 6c 65 20 26 26 20 21 70 43 75 72 2d 3e 61  ndle && !pCur->a
13bcc 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20  Overflow ){.    
13bcd 20 20 69 6e 74 20 6e 4f 76 66 6c 20 3d 20 28 70    int nOvfl = (p
13bce 43 75 72 2d 3e 69 6e 66 6f 2e 6e 50 61 79 6c 6f  Cur->info.nPaylo
13bcf 61 64 2d 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c  ad-pCur->info.nL
13bd0 6f 63 61 6c 2b 6f 76 66 6c 53 69 7a 65 2d 31 29  ocal+ovflSize-1)
13bd1 2f 6f 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20 20  /ovflSize;.     
13bd2 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77   pCur->aOverflow
13bd3 20 3d 20 28 50 67 6e 6f 20 2a 29 73 71 6c 69 74   = (Pgno *)sqlit
13bd4 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a  e3MallocZero(siz
13bd5 65 6f 66 28 50 67 6e 6f 29 2a 6e 4f 76 66 6c 29  eof(Pgno)*nOvfl)
13bd6 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 4f 76 66  ;.      if( nOvf
13bd7 6c 20 26 26 20 21 70 43 75 72 2d 3e 61 4f 76 65  l && !pCur->aOve
13bd8 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20  rflow ){.       
13bd9 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
13bda 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  EM;.      }.    
13bdb 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  }..    /* If the
13bdc 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c   overflow page-l
13bdd 69 73 74 20 63 61 63 68 65 20 68 61 73 20 62 65  ist cache has be
13bde 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  en allocated and
13bdf 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 6e 74 72   the.    ** entr
13be0 79 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20  y for the first 
13be1 72 65 71 75 69 72 65 64 20 6f 76 65 72 66 6c 6f  required overflo
13be2 77 20 70 61 67 65 20 69 73 20 76 61 6c 69 64 2c  w page is valid,
13be3 20 73 6b 69 70 0a 20 20 20 20 2a 2a 20 64 69 72   skip.    ** dir
13be4 65 63 74 6c 79 20 74 6f 20 69 74 2e 0a 20 20 20  ectly to it..   
13be5 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43 75 72   */.    if( pCur
13be6 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 26 26 20 70  ->aOverflow && p
13be7 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 6f  Cur->aOverflow[o
13be8 66 66 73 65 74 2f 6f 76 66 6c 53 69 7a 65 5d 20  ffset/ovflSize] 
13be9 29 7b 0a 20 20 20 20 20 20 69 49 64 78 20 3d 20  ){.      iIdx = 
13bea 28 6f 66 66 73 65 74 2f 6f 76 66 6c 53 69 7a 65  (offset/ovflSize
13beb 29 3b 0a 20 20 20 20 20 20 6e 65 78 74 50 61 67  );.      nextPag
13bec 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72 66  e = pCur->aOverf
13bed 6c 6f 77 5b 69 49 64 78 5d 3b 0a 20 20 20 20 20  low[iIdx];.     
13bee 20 6f 66 66 73 65 74 20 3d 20 28 6f 66 66 73 65   offset = (offse
13bef 74 25 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20  t%ovflSize);.   
13bf0 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 66   }.#endif..    f
13bf1 6f 72 28 20 3b 20 72 63 3d 3d 53 51 4c 49 54 45  or( ; rc==SQLITE
13bf2 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 26 26 20  _OK && amt>0 && 
13bf3 6e 65 78 74 50 61 67 65 3b 20 69 49 64 78 2b 2b  nextPage; iIdx++
13bf4 29 7b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ){..#ifndef SQLI
13bf5 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42  TE_OMIT_INCRBLOB
13bf6 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 72 65 71  .      /* If req
13bf7 75 69 72 65 64 2c 20 70 6f 70 75 6c 61 74 65 20  uired, populate 
13bf8 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  the overflow pag
13bf9 65 2d 6c 69 73 74 20 63 61 63 68 65 2e 20 2a 2f  e-list cache. */
13bfa 0a 20 20 20 20 20 20 69 66 28 20 70 43 75 72 2d  .      if( pCur-
13bfb 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20  >aOverflow ){.  
13bfc 20 20 20 20 20 20 61 73 73 65 72 74 28 21 70 43        assert(!pC
13bfd 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49  ur->aOverflow[iI
13bfe 64 78 5d 20 7c 7c 20 70 43 75 72 2d 3e 61 4f 76  dx] || pCur->aOv
13bff 65 72 66 6c 6f 77 5b 69 49 64 78 5d 3d 3d 6e 65  erflow[iIdx]==ne
13c00 78 74 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  xtPage);.       
13c01 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77   pCur->aOverflow
13c02 5b 69 49 64 78 5d 20 3d 20 6e 65 78 74 50 61 67  [iIdx] = nextPag
13c03 65 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  e;.      }.#endi
13c04 66 0a 0a 20 20 20 20 20 20 69 66 28 20 6f 66 66  f..      if( off
13c05 73 65 74 3e 3d 6f 76 66 6c 53 69 7a 65 20 29 7b  set>=ovflSize ){
13c06 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
13c07 6f 6e 6c 79 20 72 65 61 73 6f 6e 20 74 6f 20 72  only reason to r
13c08 65 61 64 20 74 68 69 73 20 70 61 67 65 20 69 73  ead this page is
13c09 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 70   to obtain the p
13c0a 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e  age.        ** n
13c0b 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 6e 65  umber for the ne
13c0c 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 6f  xt page in the o
13c0d 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 54  verflow chain. T
13c0e 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 20 20  he page.        
13c0f 2a 2a 20 64 61 74 61 20 69 73 20 6e 6f 74 20 72  ** data is not r
13c10 65 71 75 69 72 65 64 2e 20 53 6f 20 66 69 72 73  equired. So firs
13c11 74 20 74 72 79 20 74 6f 20 6c 6f 6f 6b 75 70 20  t try to lookup 
13c12 74 68 65 20 6f 76 65 72 66 6c 6f 77 0a 20 20 20  the overflow.   
13c13 20 20 20 20 20 2a 2a 20 70 61 67 65 2d 6c 69 73       ** page-lis
13c14 74 20 63 61 63 68 65 2c 20 69 66 20 61 6e 79 2c  t cache, if any,
13c15 20 74 68 65 6e 20 66 61 6c 6c 20 62 61 63 6b 20   then fall back 
13c16 74 6f 20 74 68 65 20 67 65 74 4f 76 65 72 66 6c  to the getOverfl
13c17 6f 77 50 61 67 65 28 29 0a 20 20 20 20 20 20 20  owPage().       
13c18 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20   ** function..  
13c19 20 20 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66        */.#ifndef
13c1a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43   SQLITE_OMIT_INC
13c1b 52 42 4c 4f 42 0a 20 20 20 20 20 20 20 20 69 66  RBLOB.        if
13c1c 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f  ( pCur->aOverflo
13c1d 77 20 26 26 20 70 43 75 72 2d 3e 61 4f 76 65 72  w && pCur->aOver
13c1e 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 20 29 7b 0a  flow[iIdx+1] ){.
13c1f 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 50 61            nextPa
13c20 67 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72  ge = pCur->aOver
13c21 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 3b 0a 20 20  flow[iIdx+1];.  
13c22 20 20 20 20 20 20 7d 20 65 6c 73 65 20 0a 23 65        } else .#e
13c23 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20 20 72  ndif.          r
13c24 63 20 3d 20 67 65 74 4f 76 65 72 66 6c 6f 77 50  c = getOverflowP
13c25 61 67 65 28 70 42 74 2c 20 6e 65 78 74 50 61 67  age(pBt, nextPag
13c26 65 2c 20 30 2c 20 26 6e 65 78 74 50 61 67 65 29  e, 0, &nextPage)
13c27 3b 0a 20 20 20 20 20 20 20 20 6f 66 66 73 65 74  ;.        offset
13c28 20 2d 3d 20 6f 76 66 6c 53 69 7a 65 3b 0a 20 20   -= ovflSize;.  
13c29 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
13c2a 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 72 65     /* Need to re
13c2b 61 64 20 74 68 69 73 20 70 61 67 65 20 70 72 6f  ad this page pro
13c2c 70 65 72 6c 79 2e 20 49 74 20 63 6f 6e 74 61 69  perly. It contai
13c2d 6e 73 20 73 6f 6d 65 20 6f 66 20 74 68 65 0a 20  ns some of the. 
13c2e 20 20 20 20 20 20 20 2a 2a 20 72 61 6e 67 65 20         ** range 
13c2f 6f 66 20 64 61 74 61 20 74 68 61 74 20 69 73 20  of data that is 
13c30 62 65 69 6e 67 20 72 65 61 64 20 28 65 4f 70 3d  being read (eOp=
13c31 3d 30 29 20 6f 72 20 77 72 69 74 74 65 6e 20 28  =0) or written (
13c32 65 4f 70 21 3d 30 29 2e 0a 20 20 20 20 20 20 20  eOp!=0)..       
13c33 20 2a 2f 0a 20 20 20 20 20 20 20 20 44 62 50 61   */.        DbPa
13c34 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 20 20 20  ge *pDbPage;.   
13c35 20 20 20 20 20 69 6e 74 20 61 20 3d 20 61 6d 74       int a = amt
13c36 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
13c37 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70  qlite3PagerGet(p
13c38 42 74 2d 3e 70 50 61 67 65 72 2c 20 6e 65 78 74  Bt->pPager, next
13c39 50 61 67 65 2c 20 26 70 44 62 50 61 67 65 29 3b  Page, &pDbPage);
13c3a 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
13c3b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
13c3c 20 20 20 20 20 20 20 20 61 50 61 79 6c 6f 61 64          aPayload
13c3d 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47   = sqlite3PagerG
13c3e 65 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b  etData(pDbPage);
13c3f 0a 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 50  .          nextP
13c40 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 61  age = get4byte(a
13c41 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 20 20  Payload);.      
13c42 20 20 20 20 69 66 28 20 61 20 2b 20 6f 66 66 73      if( a + offs
13c43 65 74 20 3e 20 6f 76 66 6c 53 69 7a 65 20 29 7b  et > ovflSize ){
13c44 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 20 3d  .            a =
13c45 20 6f 76 66 6c 53 69 7a 65 20 2d 20 6f 66 66 73   ovflSize - offs
13c46 65 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  et;.          }.
13c47 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 63            rc = c
13c48 6f 70 79 50 61 79 6c 6f 61 64 28 26 61 50 61 79  opyPayload(&aPay
13c49 6c 6f 61 64 5b 6f 66 66 73 65 74 2b 34 5d 2c 20  load[offset+4], 
13c4a 70 42 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 44  pBuf, a, eOp, pD
13c4b 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  bPage);.        
13c4c 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
13c4d 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20  ref(pDbPage);.  
13c4e 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 20 3d          offset =
13c4f 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61 6d   0;.          am
13c50 74 20 2d 3d 20 61 3b 0a 20 20 20 20 20 20 20 20  t -= a;.        
13c51 20 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 20    pBuf += a;.   
13c52 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
13c53 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
13c54 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
13c55 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 72 65   amt>0 ){.    re
13c56 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
13c57 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20  UPT_BKPT;.  }.  
13c58 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
13c59 0a 2a 2a 20 52 65 61 64 20 70 61 72 74 20 6f 66  .** Read part of
13c5a 20 74 68 65 20 6b 65 79 20 61 73 73 6f 63 69 61   the key associa
13c5b 74 65 64 20 77 69 74 68 20 63 75 72 73 6f 72 20  ted with cursor 
13c5c 70 43 75 72 2e 20 20 45 78 61 63 74 6c 79 0a 2a  pCur.  Exactly.*
13c5d 2a 20 22 61 6d 74 22 20 62 79 74 65 73 20 77 69  * "amt" bytes wi
13c5e 6c 6c 20 62 65 20 74 72 61 6e 73 66 65 72 65 64  ll be transfered
13c5f 20 69 6e 74 6f 20 70 42 75 66 5b 5d 2e 20 20 54   into pBuf[].  T
13c60 68 65 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 62  he transfer.** b
13c61 65 67 69 6e 73 20 61 74 20 22 6f 66 66 73 65 74  egins at "offset
13c62 22 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  "..**.** Return 
13c63 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63  SQLITE_OK on suc
13c64 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72  cess or an error
13c65 20 63 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e   code if anythin
13c66 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e  g goes.** wrong.
13c67 20 20 41 6e 20 65 72 72 6f 72 20 69 73 20 72 65    An error is re
13c68 74 75 72 6e 65 64 20 69 66 20 22 6f 66 66 73 65  turned if "offse
13c69 74 2b 61 6d 74 22 20 69 73 20 6c 61 72 67 65 72  t+amt" is larger
13c6a 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 61 76 61   than.** the ava
13c6b 69 6c 61 62 6c 65 20 70 61 79 6c 6f 61 64 2e 0a  ilable payload..
13c6c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
13c6d 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
13c6e 65 65 4b 65 79 28 42 74 43 75 72 73 6f 72 20 2a  eeKey(BtCursor *
13c6f 70 43 75 72 2c 20 75 33 32 20 6f 66 66 73 65 74  pCur, u32 offset
13c70 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 20  , u32 amt, void 
13c71 2a 70 42 75 66 29 7b 0a 20 20 69 6e 74 20 72 63  *pBuf){.  int rc
13c72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  ;..  assert( cur
13c73 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
13c74 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65  ur) );.  rc = re
13c75 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74  storeCursorPosit
13c76 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28  ion(pCur);.  if(
13c77 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
13c78 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  {.    assert( pC
13c79 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
13c7a 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20 20  OR_VALID );.    
13c7b 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50  assert( pCur->iP
13c7c 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e  age>=0 && pCur->
13c7d 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
13c7e 67 65 5d 20 29 3b 0a 20 20 20 20 69 66 28 20 70  ge] );.    if( p
13c7f 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e  Cur->apPage[0]->
13c80 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20  intKey ){.      
13c81 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
13c82 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
13c83 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  }.    assert( pC
13c84 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
13c85 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e 61 70 50  iPage]<pCur->apP
13c86 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
13c87 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 72  ->nCell );.    r
13c88 63 20 3d 20 61 63 63 65 73 73 50 61 79 6c 6f 61  c = accessPayloa
13c89 64 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20  d(pCur, offset, 
13c8a 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64 20 63  amt, (unsigned c
13c8b 68 61 72 2a 29 70 42 75 66 2c 20 30 2c 20 30 29  har*)pBuf, 0, 0)
13c8c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
13c8d 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  c;.}../*.** Read
13c8e 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74   part of the dat
13c8f 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  a associated wit
13c90 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20  h cursor pCur.  
13c91 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22  Exactly.** "amt"
13c92 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74   bytes will be t
13c93 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70  ransfered into p
13c94 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e  Buf[].  The tran
13c95 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61  sfer.** begins a
13c96 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a  t "offset"..**.*
13c97 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  * Return SQLITE_
13c98 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72  OK on success or
13c99 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69   an error code i
13c9a 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a  f anything goes.
13c9b 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65 72  ** wrong.  An er
13c9c 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20  ror is returned 
13c9d 69 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 22 20  if "offset+amt" 
13c9e 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 0a 2a  is larger than.*
13c9f 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20  * the available 
13ca0 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 4c 49  payload..*/.SQLI
13ca1 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
13ca2 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28  qlite3BtreeData(
13ca3 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
13ca4 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20  u32 offset, u32 
13ca5 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 29  amt, void *pBuf)
13ca6 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 23 69 66  {.  int rc;..#if
13ca7 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
13ca8 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 69 66 20 28  _INCRBLOB.  if (
13ca9 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
13caa 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b  URSOR_INVALID ){
13cab 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
13cac 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 23 65  TE_ABORT;.  }.#e
13cad 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20  ndif..  assert( 
13cae 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
13caf 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d  (pCur) );.  rc =
13cb0 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f   restoreCursorPo
13cb1 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20  sition(pCur);.  
13cb2 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
13cb3 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  K ){.    assert(
13cb4 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
13cb5 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20  URSOR_VALID );. 
13cb6 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
13cb7 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75  >iPage>=0 && pCu
13cb8 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
13cb9 69 50 61 67 65 5d 20 29 3b 0a 20 20 20 20 61 73  iPage] );.    as
13cba 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64  sert( pCur->aiId
13cbb 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70  x[pCur->iPage]<p
13cbc 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
13cbd 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20  ->iPage]->nCell 
13cbe 29 3b 0a 20 20 20 20 72 63 20 3d 20 61 63 63 65  );.    rc = acce
13cbf 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20  ssPayload(pCur, 
13cc0 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 42 75  offset, amt, pBu
13cc1 66 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  f, 1, 0);.  }.  
13cc2 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
13cc3 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69  .** Return a poi
13cc4 6e 74 65 72 20 74 6f 20 70 61 79 6c 6f 61 64 20  nter to payload 
13cc5 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d  information from
13cc6 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20   the entry that 
13cc7 74 68 65 20 0a 2a 2a 20 70 43 75 72 20 63 75 72  the .** pCur cur
13cc8 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20  sor is pointing 
13cc9 74 6f 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 72  to.  The pointer
13cca 20 69 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e   is to the begin
13ccb 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74 68 65 20 6b  ning of.** the k
13ccc 65 79 20 69 66 20 73 6b 69 70 4b 65 79 3d 3d 30  ey if skipKey==0
13ccd 20 61 6e 64 20 69 74 20 70 6f 69 6e 74 73 20 74   and it points t
13cce 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  o the beginning 
13ccf 6f 66 20 64 61 74 61 20 69 66 0a 2a 2a 20 73 6b  of data if.** sk
13cd0 69 70 4b 65 79 3d 3d 31 2e 20 20 54 68 65 20 6e  ipKey==1.  The n
13cd1 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
13cd2 66 20 61 76 61 69 6c 61 62 6c 65 20 6b 65 79 2f  f available key/
13cd3 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 0a  data is written.
13cd4 2a 2a 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 20 20  ** into *pAmt.  
13cd5 49 66 20 2a 70 41 6d 74 3d 3d 30 2c 20 74 68 65  If *pAmt==0, the
13cd6 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75  n the value retu
13cd7 72 6e 65 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65  rned will not be
13cd8 0a 2a 2a 20 61 20 76 61 6c 69 64 20 70 6f 69 6e  .** a valid poin
13cd9 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ter..**.** This 
13cda 72 6f 75 74 69 6e 65 20 69 73 20 61 6e 20 6f 70  routine is an op
13cdb 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 49 74 20  timization.  It 
13cdc 69 73 20 63 6f 6d 6d 6f 6e 20 66 6f 72 20 74 68  is common for th
13cdd 65 20 65 6e 74 69 72 65 20 6b 65 79 0a 2a 2a 20  e entire key.** 
13cde 61 6e 64 20 64 61 74 61 20 74 6f 20 66 69 74 20  and data to fit 
13cdf 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67  on the local pag
13ce0 65 20 61 6e 64 20 66 6f 72 20 74 68 65 72 65 20  e and for there 
13ce1 74 6f 20 62 65 20 6e 6f 20 6f 76 65 72 66 6c 6f  to be no overflo
13ce2 77 0a 2a 2a 20 70 61 67 65 73 2e 20 20 57 68 65  w.** pages.  Whe
13ce3 6e 20 74 68 61 74 20 69 73 20 73 6f 2c 20 74 68  n that is so, th
13ce4 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62  is routine can b
13ce5 65 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73  e used to access
13ce6 20 74 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20   the.** key and 
13ce7 64 61 74 61 20 77 69 74 68 6f 75 74 20 6d 61 6b  data without mak
13ce8 69 6e 67 20 61 20 63 6f 70 79 2e 20 20 49 66 20  ing a copy.  If 
13ce9 74 68 65 20 6b 65 79 20 61 6e 64 2f 6f 72 20 64  the key and/or d
13cea 61 74 61 20 73 70 69 6c 6c 73 0a 2a 2a 20 6f 6e  ata spills.** on
13ceb 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  to overflow page
13cec 73 2c 20 74 68 65 6e 20 61 63 63 65 73 73 50 61  s, then accessPa
13ced 79 6c 6f 61 64 28 29 20 6d 75 73 74 20 62 65 20  yload() must be 
13cee 75 73 65 64 20 74 6f 20 72 65 61 73 73 65 6d 62  used to reassemb
13cef 6c 79 0a 2a 2a 20 74 68 65 20 6b 65 79 2f 64 61  ly.** the key/da
13cf0 74 61 20 61 6e 64 20 63 6f 70 79 20 69 74 20 69  ta and copy it i
13cf1 6e 74 6f 20 61 20 70 72 65 61 6c 6c 6f 63 61 74  nto a preallocat
13cf2 65 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a  ed buffer..**.**
13cf3 20 54 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74   The pointer ret
13cf4 75 72 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f  urned by this ro
13cf5 75 74 69 6e 65 20 6c 6f 6f 6b 73 20 64 69 72 65  utine looks dire
13cf6 63 74 6c 79 20 69 6e 74 6f 20 74 68 65 20 63 61  ctly into the ca
13cf7 63 68 65 64 0a 2a 2a 20 70 61 67 65 20 6f 66 20  ched.** page of 
13cf8 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54  the database.  T
13cf9 68 65 20 64 61 74 61 20 6d 69 67 68 74 20 63 68  he data might ch
13cfa 61 6e 67 65 20 6f 72 20 6d 6f 76 65 20 74 68 65  ange or move the
13cfb 20 6e 65 78 74 20 74 69 6d 65 0a 2a 2a 20 61 6e   next time.** an
13cfc 79 20 62 74 72 65 65 20 72 6f 75 74 69 6e 65 20  y btree routine 
13cfd 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74  is called..*/.st
13cfe 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
13cff 6e 65 64 20 63 68 61 72 20 2a 66 65 74 63 68 50  ned char *fetchP
13d00 61 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 73  ayload(.  BtCurs
13d01 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 2f  or *pCur,      /
13d02 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e  * Cursor pointin
13d03 67 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 65  g to entry to re
13d04 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74  ad from */.  int
13d05 20 2a 70 41 6d 74 2c 20 20 20 20 20 20 20 20 20   *pAmt,         
13d06 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e    /* Write the n
13d07 75 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62  umber of availab
13d08 6c 65 20 62 79 74 65 73 20 68 65 72 65 20 2a 2f  le bytes here */
13d09 0a 20 20 69 6e 74 20 73 6b 69 70 4b 65 79 20 20  .  int skipKey  
13d0a 20 20 20 20 20 20 20 20 2f 2a 20 72 65 61 64 20          /* read 
13d0b 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 64 61 74  beginning at dat
13d0c 61 20 69 66 20 74 68 69 73 20 69 73 20 74 72 75  a if this is tru
13d0d 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e  e */.){.  unsign
13d0e 65 64 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61  ed char *aPayloa
13d0f 64 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  d;.  MemPage *pP
13d10 61 67 65 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b  age;.  u32 nKey;
13d11 0a 20 20 69 6e 74 20 6e 4c 6f 63 61 6c 3b 0a 0a  .  int nLocal;..
13d12 20 20 61 73 73 65 72 74 28 20 70 43 75 72 21 3d    assert( pCur!=
13d13 30 20 26 26 20 70 43 75 72 2d 3e 69 50 61 67 65  0 && pCur->iPage
13d14 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50  >=0 && pCur->apP
13d15 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
13d16 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  );.  assert( pCu
13d17 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
13d18 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73  R_VALID );.  ass
13d19 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
13d1a 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
13d1b 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61   pPage = pCur->a
13d1c 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
13d1d 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43  e];.  assert( pC
13d1e 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
13d1f 69 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43  iPage]<pPage->nC
13d20 65 6c 6c 20 29 3b 0a 20 20 67 65 74 43 65 6c 6c  ell );.  getCell
13d21 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 61 50  Info(pCur);.  aP
13d22 61 79 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69  ayload = pCur->i
13d23 6e 66 6f 2e 70 43 65 6c 6c 3b 0a 20 20 61 50 61  nfo.pCell;.  aPa
13d24 79 6c 6f 61 64 20 2b 3d 20 70 43 75 72 2d 3e 69  yload += pCur->i
13d25 6e 66 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20 69  nfo.nHeader;.  i
13d26 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79  f( pPage->intKey
13d27 20 29 7b 0a 20 20 20 20 6e 4b 65 79 20 3d 20 30   ){.    nKey = 0
13d28 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e  ;.  }else{.    n
13d29 4b 65 79 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f  Key = pCur->info
13d2a 2e 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28  .nKey;.  }.  if(
13d2b 20 73 6b 69 70 4b 65 79 20 29 7b 0a 20 20 20 20   skipKey ){.    
13d2c 61 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65 79  aPayload += nKey
13d2d 3b 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70  ;.    nLocal = p
13d2e 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c  Cur->info.nLocal
13d2f 20 2d 20 6e 4b 65 79 3b 0a 20 20 7d 65 6c 73 65   - nKey;.  }else
13d30 7b 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70  {.    nLocal = p
13d31 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c  Cur->info.nLocal
13d32 3b 0a 20 20 20 20 69 66 28 20 6e 4c 6f 63 61 6c  ;.    if( nLocal
13d33 3e 6e 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 6e  >nKey ){.      n
13d34 4c 6f 63 61 6c 20 3d 20 6e 4b 65 79 3b 0a 20 20  Local = nKey;.  
13d35 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 41 6d 74 20    }.  }.  *pAmt 
13d36 3d 20 6e 4c 6f 63 61 6c 3b 0a 20 20 72 65 74 75  = nLocal;.  retu
13d37 72 6e 20 61 50 61 79 6c 6f 61 64 3b 0a 7d 0a 0a  rn aPayload;.}..
13d38 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 65  ./*.** For the e
13d39 6e 74 72 79 20 74 68 61 74 20 63 75 72 73 6f 72  ntry that cursor
13d3a 20 70 43 75 72 20 69 73 20 70 6f 69 6e 74 20 74   pCur is point t
13d3b 6f 2c 20 72 65 74 75 72 6e 20 61 73 0a 2a 2a 20  o, return as.** 
13d3c 6d 61 6e 79 20 62 79 74 65 73 20 6f 66 20 74 68  many bytes of th
13d3d 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20 61 73  e key or data as
13d3e 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 6f   are available o
13d3f 6e 20 74 68 65 20 6c 6f 63 61 6c 0a 2a 2a 20 62  n the local.** b
13d40 2d 74 72 65 65 20 70 61 67 65 2e 20 20 57 72 69  -tree page.  Wri
13d41 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  te the number of
13d42 20 61 76 61 69 6c 61 62 6c 65 20 62 79 74 65 73   available bytes
13d43 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 0a 2a 2a 0a   into *pAmt..**.
13d44 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 72  ** The pointer r
13d45 65 74 75 72 6e 65 64 20 69 73 20 65 70 68 65 6d  eturned is ephem
13d46 65 72 61 6c 2e 20 20 54 68 65 20 6b 65 79 2f 64  eral.  The key/d
13d47 61 74 61 20 6d 61 79 20 6d 6f 76 65 0a 2a 2a 20  ata may move.** 
13d48 6f 72 20 62 65 20 64 65 73 74 72 6f 79 65 64 20  or be destroyed 
13d49 6f 6e 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c  on the next call
13d4a 20 74 6f 20 61 6e 79 20 42 74 72 65 65 20 72 6f   to any Btree ro
13d4b 75 74 69 6e 65 2c 0a 2a 2a 20 69 6e 63 6c 75 64  utine,.** includ
13d4c 69 6e 67 20 63 61 6c 6c 73 20 66 72 6f 6d 20 6f  ing calls from o
13d4d 74 68 65 72 20 74 68 72 65 61 64 73 20 61 67 61  ther threads aga
13d4e 69 6e 73 74 20 74 68 65 20 73 61 6d 65 20 63 61  inst the same ca
13d4f 63 68 65 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 61  che..** Hence, a
13d50 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 42 74   mutex on the Bt
13d51 53 68 61 72 65 64 20 73 68 6f 75 6c 64 20 62 65  Shared should be
13d52 20 68 65 6c 64 20 70 72 69 6f 72 20 74 6f 20 63   held prior to c
13d53 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 72  alling.** this r
13d54 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  outine..**.** Th
13d55 65 73 65 20 72 6f 75 74 69 6e 65 73 20 69 73 20  ese routines is 
13d56 75 73 65 64 20 74 6f 20 67 65 74 20 71 75 69 63  used to get quic
13d57 6b 20 61 63 63 65 73 73 20 74 6f 20 6b 65 79 20  k access to key 
13d58 61 6e 64 20 64 61 74 61 0a 2a 2a 20 69 6e 20 74  and data.** in t
13d59 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77  he common case w
13d5a 68 65 72 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77  here no overflow
13d5b 20 70 61 67 65 73 20 61 72 65 20 75 73 65 64 2e   pages are used.
13d5c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
13d5d 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  TE const void *s
13d5e 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 46 65  qlite3BtreeKeyFe
13d5f 74 63 68 28 42 74 43 75 72 73 6f 72 20 2a 70 43  tch(BtCursor *pC
13d60 75 72 2c 20 69 6e 74 20 2a 70 41 6d 74 29 7b 0a  ur, int *pAmt){.
13d61 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
13d62 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
13d63 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e   );.  if( pCur->
13d64 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56  eState==CURSOR_V
13d65 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75  ALID ){.    retu
13d66 72 6e 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 29  rn (const void*)
13d67 66 65 74 63 68 50 61 79 6c 6f 61 64 28 70 43 75  fetchPayload(pCu
13d68 72 2c 20 70 41 6d 74 2c 20 30 29 3b 0a 20 20 7d  r, pAmt, 0);.  }
13d69 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53  .  return 0;.}.S
13d6a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
13d6b 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
13d6c 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68 28  3BtreeDataFetch(
13d6d 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
13d6e 69 6e 74 20 2a 70 41 6d 74 29 7b 0a 20 20 61 73  int *pAmt){.  as
13d6f 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
13d70 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
13d71 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61    if( pCur->eSta
13d72 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
13d73 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28   ){.    return (
13d74 63 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63  const void*)fetc
13d75 68 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70  hPayload(pCur, p
13d76 41 6d 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72  Amt, 1);.  }.  r
13d77 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a  eturn 0;.}.../*.
13d78 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73  ** Move the curs
13d79 6f 72 20 64 6f 77 6e 20 74 6f 20 61 20 6e 65 77  or down to a new
13d7a 20 63 68 69 6c 64 20 70 61 67 65 2e 20 20 54 68   child page.  Th
13d7b 65 20 6e 65 77 50 67 6e 6f 20 61 72 67 75 6d 65  e newPgno argume
13d7c 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 67  nt is the.** pag
13d7d 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
13d7e 63 68 69 6c 64 20 70 61 67 65 20 74 6f 20 6d 6f  child page to mo
13d7f 76 65 20 74 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63  ve to..*/.static
13d80 20 69 6e 74 20 6d 6f 76 65 54 6f 43 68 69 6c 64   int moveToChild
13d81 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c  (BtCursor *pCur,
13d82 20 75 33 32 20 6e 65 77 50 67 6e 6f 29 7b 0a 20   u32 newPgno){. 
13d83 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 69   int rc;.  int i
13d84 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a   = pCur->iPage;.
13d85 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 50    MemPage *pNewP
13d86 61 67 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20  age;.  BtShared 
13d87 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74  *pBt = pCur->pBt
13d88 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  ;..  assert( cur
13d89 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
13d8a 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ur) );.  assert(
13d8b 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
13d8c 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20  URSOR_VALID );. 
13d8d 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69   assert( pCur->i
13d8e 50 61 67 65 3c 42 54 43 55 52 53 4f 52 5f 4d 41  Page<BTCURSOR_MA
13d8f 58 5f 44 45 50 54 48 20 29 3b 0a 20 20 69 66 28  X_DEPTH );.  if(
13d90 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 28 42   pCur->iPage>=(B
13d91 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54  TCURSOR_MAX_DEPT
13d92 48 2d 31 29 20 29 7b 0a 20 20 20 20 72 65 74 75  H-1) ){.    retu
13d93 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
13d94 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 63  T_BKPT;.  }.  rc
13d95 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67   = getAndInitPag
13d96 65 28 70 42 74 2c 20 6e 65 77 50 67 6e 6f 2c 20  e(pBt, newPgno, 
13d97 26 70 4e 65 77 50 61 67 65 29 3b 0a 20 20 69 66  &pNewPage);.  if
13d98 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
13d99 3b 0a 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65  ;.  pCur->apPage
13d9a 5b 69 2b 31 5d 20 3d 20 70 4e 65 77 50 61 67 65  [i+1] = pNewPage
13d9b 3b 0a 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  ;.  pCur->aiIdx[
13d9c 69 2b 31 5d 20 3d 20 30 3b 0a 20 20 70 43 75 72  i+1] = 0;.  pCur
13d9d 2d 3e 69 50 61 67 65 2b 2b 3b 0a 0a 20 20 70 43  ->iPage++;..  pC
13d9e 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d  ur->info.nSize =
13d9f 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69   0;.  pCur->vali
13da0 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66 28  dNKey = 0;.  if(
13da1 20 70 4e 65 77 50 61 67 65 2d 3e 6e 43 65 6c 6c   pNewPage->nCell
13da2 3c 31 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  <1 ){.    return
13da3 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
13da4 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75  BKPT;.  }.  retu
13da5 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
13da6 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
13da7 2f 2a 0a 2a 2a 20 50 61 67 65 20 70 50 61 72 65  /*.** Page pPare
13da8 6e 74 20 69 73 20 61 6e 20 69 6e 74 65 72 6e 61  nt is an interna
13da9 6c 20 28 6e 6f 6e 2d 6c 65 61 66 29 20 74 72 65  l (non-leaf) tre
13daa 65 20 70 61 67 65 2e 20 54 68 69 73 20 66 75 6e  e page. This fun
13dab 63 74 69 6f 6e 20 0a 2a 2a 20 61 73 73 65 72 74  ction .** assert
13dac 73 20 74 68 61 74 20 70 61 67 65 20 6e 75 6d 62  s that page numb
13dad 65 72 20 69 43 68 69 6c 64 20 69 73 20 74 68 65  er iChild is the
13dae 20 6c 65 66 74 2d 63 68 69 6c 64 20 69 66 20 74   left-child if t
13daf 68 65 20 69 49 64 78 27 74 68 0a 2a 2a 20 63 65  he iIdx'th.** ce
13db0 6c 6c 20 69 6e 20 70 61 67 65 20 70 50 61 72 65  ll in page pPare
13db1 6e 74 2e 20 4f 72 2c 20 69 66 20 69 49 64 78 20  nt. Or, if iIdx 
13db2 69 73 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20  is equal to the 
13db3 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 0a  total number of.
13db4 2a 2a 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 72  ** cells in pPar
13db5 65 6e 74 2c 20 74 68 61 74 20 70 61 67 65 20 6e  ent, that page n
13db6 75 6d 62 65 72 20 69 43 68 69 6c 64 20 69 73 20  umber iChild is 
13db7 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20  the right-child 
13db8 6f 66 0a 2a 2a 20 74 68 65 20 70 61 67 65 2e 0a  of.** the page..
13db9 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
13dba 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78  ssertParentIndex
13dbb 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e  (MemPage *pParen
13dbc 74 2c 20 69 6e 74 20 69 49 64 78 2c 20 50 67 6e  t, int iIdx, Pgn
13dbd 6f 20 69 43 68 69 6c 64 29 7b 0a 20 20 61 73 73  o iChild){.  ass
13dbe 65 72 74 28 20 69 49 64 78 3c 3d 70 50 61 72 65  ert( iIdx<=pPare
13dbf 6e 74 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 69  nt->nCell );.  i
13dc0 66 28 20 69 49 64 78 3d 3d 70 50 61 72 65 6e 74  f( iIdx==pParent
13dc1 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 61  ->nCell ){.    a
13dc2 73 73 65 72 74 28 20 67 65 74 34 62 79 74 65 28  ssert( get4byte(
13dc3 26 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b  &pParent->aData[
13dc4 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73  pParent->hdrOffs
13dc5 65 74 2b 38 5d 29 3d 3d 69 43 68 69 6c 64 20 29  et+8])==iChild )
13dc6 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
13dc7 73 73 65 72 74 28 20 67 65 74 34 62 79 74 65 28  ssert( get4byte(
13dc8 66 69 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74  findCell(pParent
13dc9 2c 20 69 49 64 78 29 29 3d 3d 69 43 68 69 6c 64  , iIdx))==iChild
13dca 20 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a   );.  }.}.#else.
13dcb 23 20 20 64 65 66 69 6e 65 20 61 73 73 65 72 74  #  define assert
13dcc 50 61 72 65 6e 74 49 6e 64 65 78 28 78 2c 79 2c  ParentIndex(x,y,
13dcd 7a 29 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  z) .#endif../*.*
13dce 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f  * Move the curso
13dcf 72 20 75 70 20 74 6f 20 74 68 65 20 70 61 72 65  r up to the pare
13dd0 6e 74 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70  nt page..**.** p
13dd1 43 75 72 2d 3e 69 64 78 20 69 73 20 73 65 74 20  Cur->idx is set 
13dd2 74 6f 20 74 68 65 20 63 65 6c 6c 20 69 6e 64 65  to the cell inde
13dd3 78 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  x that contains 
13dd4 74 68 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74  the pointer.** t
13dd5 6f 20 74 68 65 20 70 61 67 65 20 77 65 20 61 72  o the page we ar
13dd6 65 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 2e 20 20  e coming from.  
13dd7 49 66 20 77 65 20 61 72 65 20 63 6f 6d 69 6e 67  If we are coming
13dd8 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 72 69 67   from the.** rig
13dd9 68 74 2d 6d 6f 73 74 20 63 68 69 6c 64 20 70 61  ht-most child pa
13dda 67 65 20 74 68 65 6e 20 70 43 75 72 2d 3e 69 64  ge then pCur->id
13ddb 78 20 69 73 20 73 65 74 20 74 6f 20 6f 6e 65 20  x is set to one 
13ddc 6d 6f 72 65 20 74 68 61 6e 0a 2a 2a 20 74 68 65  more than.** the
13ddd 20 6c 61 72 67 65 73 74 20 63 65 6c 6c 20 69 6e   largest cell in
13dde 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  dex..*/.SQLITE_P
13ddf 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
13de0 74 65 33 42 74 72 65 65 4d 6f 76 65 54 6f 50 61  te3BtreeMoveToPa
13de1 72 65 6e 74 28 42 74 43 75 72 73 6f 72 20 2a 70  rent(BtCursor *p
13de2 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20  Cur){.  assert( 
13de3 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
13de4 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
13de5 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  rt( pCur->eState
13de6 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  ==CURSOR_VALID )
13de7 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
13de8 2d 3e 69 50 61 67 65 3e 30 20 29 3b 0a 20 20 61  ->iPage>0 );.  a
13de9 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50  ssert( pCur->apP
13dea 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
13deb 20 29 3b 0a 20 20 61 73 73 65 72 74 50 61 72 65   );.  assertPare
13dec 6e 74 49 6e 64 65 78 28 0a 20 20 20 20 70 43 75  ntIndex(.    pCu
13ded 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
13dee 69 50 61 67 65 2d 31 5d 2c 20 0a 20 20 20 20 70  iPage-1], .    p
13def 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
13df0 3e 69 50 61 67 65 2d 31 5d 2c 20 0a 20 20 20 20  >iPage-1], .    
13df1 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
13df2 72 2d 3e 69 50 61 67 65 5d 2d 3e 70 67 6e 6f 0a  r->iPage]->pgno.
13df3 20 20 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 61    );.  releasePa
13df4 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  ge(pCur->apPage[
13df5 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 3b 0a 20  pCur->iPage]);. 
13df6 20 70 43 75 72 2d 3e 69 50 61 67 65 2d 2d 3b 0a   pCur->iPage--;.
13df7 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69    pCur->info.nSi
13df8 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e  ze = 0;.  pCur->
13df9 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 7d  validNKey = 0;.}
13dfa 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65  ../*.** Move the
13dfb 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 72   cursor to the r
13dfc 6f 6f 74 20 70 61 67 65 0a 2a 2f 0a 73 74 61 74  oot page.*/.stat
13dfd 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52 6f 6f  ic int moveToRoo
13dfe 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  t(BtCursor *pCur
13dff 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 52  ){.  MemPage *pR
13e00 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  oot;.  int rc = 
13e01 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74 72  SQLITE_OK;.  Btr
13e02 65 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42  ee *p = pCur->pB
13e03 74 72 65 65 3b 0a 20 20 42 74 53 68 61 72 65 64  tree;.  BtShared
13e04 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
13e05 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
13e06 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
13e07 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43  ) );.  assert( C
13e08 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 3c 20  URSOR_INVALID < 
13e09 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45  CURSOR_REQUIRESE
13e0a 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  EK );.  assert( 
13e0b 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 3c  CURSOR_VALID   <
13e0c 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53   CURSOR_REQUIRES
13e0d 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  EEK );.  assert(
13e0e 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 20 20   CURSOR_FAULT   
13e0f 3e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  > CURSOR_REQUIRE
13e10 53 45 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43  SEEK );.  if( pC
13e11 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53  ur->eState>=CURS
13e12 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29  OR_REQUIRESEEK )
13e13 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e  {.    if( pCur->
13e14 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46  eState==CURSOR_F
13e15 41 55 4c 54 20 29 7b 0a 20 20 20 20 20 20 72 65  AULT ){.      re
13e16 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 3b  turn pCur->skip;
13e17 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
13e18 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73  e3BtreeClearCurs
13e19 6f 72 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20  or(pCur);.  }.. 
13e1a 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65   if( pCur->iPage
13e1b 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  >=0 ){.    int i
13e1c 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69  ;.    for(i=1; i
13e1d 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69  <=pCur->iPage; i
13e1e 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61  ++){.      relea
13e1f 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50  sePage(pCur->apP
13e20 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20  age[i]);.    }. 
13e21 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20   }else{.    if( 
13e22 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b  .      SQLITE_OK
13e23 21 3d 28 72 63 20 3d 20 67 65 74 41 6e 64 49 6e  !=(rc = getAndIn
13e24 69 74 50 61 67 65 28 70 42 74 2c 20 70 43 75 72  itPage(pBt, pCur
13e25 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 43 75  ->pgnoRoot, &pCu
13e26 72 2d 3e 61 70 50 61 67 65 5b 30 5d 29 29 0a 20  r->apPage[0])). 
13e27 20 20 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72     ){.      pCur
13e28 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f  ->eState = CURSO
13e29 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20  R_INVALID;.     
13e2a 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
13e2b 7d 0a 20 20 7d 0a 0a 20 20 70 52 6f 6f 74 20 3d  }.  }..  pRoot =
13e2c 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d   pCur->apPage[0]
13e2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 52 6f 6f  ;.  assert( pRoo
13e2e 74 2d 3e 70 67 6e 6f 3d 3d 70 43 75 72 2d 3e 70  t->pgno==pCur->p
13e2f 67 6e 6f 52 6f 6f 74 20 29 3b 0a 20 20 70 43 75  gnoRoot );.  pCu
13e30 72 2d 3e 69 50 61 67 65 20 3d 20 30 3b 0a 20 20  r->iPage = 0;.  
13e31 70 43 75 72 2d 3e 61 69 49 64 78 5b 30 5d 20 3d  pCur->aiIdx[0] =
13e32 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f   0;.  pCur->info
13e33 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43  .nSize = 0;.  pC
13e34 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 30 3b 0a  ur->atLast = 0;.
13e35 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65    pCur->validNKe
13e36 79 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 52  y = 0;..  if( pR
13e37 6f 6f 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26 26  oot->nCell==0 &&
13e38 20 21 70 52 6f 6f 74 2d 3e 6c 65 61 66 20 29 7b   !pRoot->leaf ){
13e39 0a 20 20 20 20 50 67 6e 6f 20 73 75 62 70 61 67  .    Pgno subpag
13e3a 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e;.    assert( p
13e3b 52 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d 31 20 29 3b  Root->pgno==1 );
13e3c 0a 20 20 20 20 73 75 62 70 61 67 65 20 3d 20 67  .    subpage = g
13e3d 65 74 34 62 79 74 65 28 26 70 52 6f 6f 74 2d 3e  et4byte(&pRoot->
13e3e 61 44 61 74 61 5b 70 52 6f 6f 74 2d 3e 68 64 72  aData[pRoot->hdr
13e3f 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20  Offset+8]);.    
13e40 61 73 73 65 72 74 28 20 73 75 62 70 61 67 65 3e  assert( subpage>
13e41 30 20 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 65  0 );.    pCur->e
13e42 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 56  State = CURSOR_V
13e43 41 4c 49 44 3b 0a 20 20 20 20 72 63 20 3d 20 6d  ALID;.    rc = m
13e44 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c  oveToChild(pCur,
13e45 20 73 75 62 70 61 67 65 29 3b 0a 20 20 7d 65 6c   subpage);.  }el
13e46 73 65 7b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53  se{.    pCur->eS
13e47 74 61 74 65 20 3d 20 28 28 70 52 6f 6f 74 2d 3e  tate = ((pRoot->
13e48 6e 43 65 6c 6c 3e 30 29 3f 43 55 52 53 4f 52 5f  nCell>0)?CURSOR_
13e49 56 41 4c 49 44 3a 43 55 52 53 4f 52 5f 49 4e 56  VALID:CURSOR_INV
13e4a 41 4c 49 44 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ALID);.  }.  ret
13e4b 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
13e4c 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72   Move the cursor
13e4d 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 6c 65 66   down to the lef
13e4e 74 2d 6d 6f 73 74 20 6c 65 61 66 20 65 6e 74 72  t-most leaf entr
13e4f 79 20 62 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a  y beneath the.**
13e50 20 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20   entry to which 
13e51 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  it is currently 
13e52 70 6f 69 6e 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  pointing..**.** 
13e53 54 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 6c 65  The left-most le
13e54 61 66 20 69 73 20 74 68 65 20 6f 6e 65 20 77 69  af is the one wi
13e55 74 68 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20  th the smallest 
13e56 6b 65 79 20 2d 20 74 68 65 20 66 69 72 73 74 0a  key - the first.
13e57 2a 2a 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20  ** in ascending 
13e58 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  order..*/.static
13e59 20 69 6e 74 20 6d 6f 76 65 54 6f 4c 65 66 74 6d   int moveToLeftm
13e5a 6f 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43  ost(BtCursor *pC
13e5b 75 72 29 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  ur){.  Pgno pgno
13e5c 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
13e5d 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67  ITE_OK;.  MemPag
13e5e 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73  e *pPage;..  ass
13e5f 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
13e60 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
13e61 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65   assert( pCur->e
13e62 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41  State==CURSOR_VA
13e63 4c 49 44 20 29 3b 0a 20 20 77 68 69 6c 65 28 20  LID );.  while( 
13e64 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
13e65 20 21 28 70 50 61 67 65 20 3d 20 70 43 75 72 2d   !(pPage = pCur-
13e66 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
13e67 61 67 65 5d 29 2d 3e 6c 65 61 66 20 29 7b 0a 20  age])->leaf ){. 
13e68 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
13e69 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
13e6a 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  ge]<pPage->nCell
13e6b 20 29 3b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67   );.    pgno = g
13e6c 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c  et4byte(findCell
13e6d 28 70 50 61 67 65 2c 20 70 43 75 72 2d 3e 61 69  (pPage, pCur->ai
13e6e 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
13e6f 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76  ));.    rc = mov
13e70 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 70  eToChild(pCur, p
13e71 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  gno);.  }.  retu
13e72 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
13e73 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20  Move the cursor 
13e74 64 6f 77 6e 20 74 6f 20 74 68 65 20 72 69 67 68  down to the righ
13e75 74 2d 6d 6f 73 74 20 6c 65 61 66 20 65 6e 74 72  t-most leaf entr
13e76 79 20 62 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a  y beneath the.**
13e77 20 70 61 67 65 20 74 6f 20 77 68 69 63 68 20 69   page to which i
13e78 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70  t is currently p
13e79 6f 69 6e 74 69 6e 67 2e 20 20 4e 6f 74 69 63 65  ointing.  Notice
13e7a 20 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 0a   the difference.
13e7b 2a 2a 20 62 65 74 77 65 65 6e 20 6d 6f 76 65 54  ** between moveT
13e7c 6f 4c 65 66 74 6d 6f 73 74 28 29 20 61 6e 64 20  oLeftmost() and 
13e7d 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28  moveToRightmost(
13e7e 29 2e 20 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f  ).  moveToLeftmo
13e7f 73 74 28 29 0a 2a 2a 20 66 69 6e 64 73 20 74 68  st().** finds th
13e80 65 20 6c 65 66 74 2d 6d 6f 73 74 20 65 6e 74 72  e left-most entr
13e81 79 20 62 65 6e 65 61 74 68 20 74 68 65 20 2a 65  y beneath the *e
13e82 6e 74 72 79 2a 20 77 68 65 72 65 61 73 20 6d 6f  ntry* whereas mo
13e83 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 29 0a  veToRightmost().
13e84 2a 2a 20 66 69 6e 64 73 20 74 68 65 20 72 69 67  ** finds the rig
13e85 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 62 65  ht-most entry be
13e86 6e 65 61 74 68 20 74 68 65 20 2a 70 61 67 65 2a  neath the *page*
13e87 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 69 67 68  ..**.** The righ
13e88 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 69 73 20  t-most entry is 
13e89 74 68 65 20 6f 6e 65 20 77 69 74 68 20 74 68 65  the one with the
13e8a 20 6c 61 72 67 65 73 74 20 6b 65 79 20 2d 20 74   largest key - t
13e8b 68 65 20 6c 61 73 74 0a 2a 2a 20 6b 65 79 20 69  he last.** key i
13e8c 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65  n ascending orde
13e8d 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
13e8e 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74   moveToRightmost
13e8f 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29  (BtCursor *pCur)
13e90 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20  {.  Pgno pgno;. 
13e91 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
13e92 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  _OK;.  MemPage *
13e93 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74  pPage;..  assert
13e94 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
13e95 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73  ex(pCur) );.  as
13e96 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61  sert( pCur->eSta
13e97 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
13e98 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d   );.  while( rc=
13e99 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28  =SQLITE_OK && !(
13e9a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70  pPage = pCur->ap
13e9b 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
13e9c 5d 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20  ])->leaf ){.    
13e9d 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28  pgno = get4byte(
13e9e 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50  &pPage->aData[pP
13e9f 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38  age->hdrOffset+8
13ea0 5d 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69  ]);.    pCur->ai
13ea1 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
13ea2 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b   = pPage->nCell;
13ea3 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f  .    rc = moveTo
13ea4 43 68 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f  Child(pCur, pgno
13ea5 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  );.  }.  if( rc=
13ea6 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
13ea7 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43    pCur->aiIdx[pC
13ea8 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50 61  ur->iPage] = pPa
13ea9 67 65 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20  ge->nCell-1;.   
13eaa 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a   pCur->info.nSiz
13eab 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d  e = 0;.    pCur-
13eac 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a  >validNKey = 0;.
13ead 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
13eae 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20  .}../* Move the 
13eaf 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 66 69  cursor to the fi
13eb0 72 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65  rst entry in the
13eb1 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20   table.  Return 
13eb2 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20  SQLITE_OK.** on 
13eb3 73 75 63 63 65 73 73 2e 20 20 53 65 74 20 2a 70  success.  Set *p
13eb4 52 65 73 20 74 6f 20 30 20 69 66 20 74 68 65 20  Res to 0 if the 
13eb5 63 75 72 73 6f 72 20 61 63 74 75 61 6c 6c 79 20  cursor actually 
13eb6 70 6f 69 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68  points to someth
13eb7 69 6e 67 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70  ing.** or set *p
13eb8 52 65 73 20 74 6f 20 31 20 69 66 20 74 68 65 20  Res to 1 if the 
13eb9 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a  table is empty..
13eba 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
13ebb 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
13ebc 65 65 46 69 72 73 74 28 42 74 43 75 72 73 6f 72  eeFirst(BtCursor
13ebd 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65   *pCur, int *pRe
13ebe 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  s){.  int rc;.. 
13ebf 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
13ec0 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
13ec1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
13ec2 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
13ec3 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62  pCur->pBtree->db
13ec4 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63  ->mutex) );.  rc
13ec5 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43   = moveToRoot(pC
13ec6 75 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  ur);.  if( rc==S
13ec7 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
13ec8 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  if( pCur->eState
13ec9 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  ==CURSOR_INVALID
13eca 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
13ecb 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70  ( pCur->apPage[p
13ecc 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65  Cur->iPage]->nCe
13ecd 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a  ll==0 );.      *
13ece 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20  pRes = 1;.      
13ecf 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
13ed0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
13ed1 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61   assert( pCur->a
13ed2 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
13ed3 65 5d 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20  e]->nCell>0 );. 
13ed4 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a       *pRes = 0;.
13ed5 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54        rc = moveT
13ed6 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b  oLeftmost(pCur);
13ed7 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
13ed8 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f  urn rc;.}../* Mo
13ed9 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f  ve the cursor to
13eda 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20   the last entry 
13edb 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52  in the table.  R
13edc 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a  eturn SQLITE_OK.
13edd 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20  ** on success.  
13ede 53 65 74 20 2a 70 52 65 73 20 74 6f 20 30 20 69  Set *pRes to 0 i
13edf 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 63 74  f the cursor act
13ee0 75 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20  ually points to 
13ee1 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20  something.** or 
13ee2 73 65 74 20 2a 70 52 65 73 20 74 6f 20 31 20 69  set *pRes to 1 i
13ee3 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65  f the table is e
13ee4 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  mpty..*/.SQLITE_
13ee5 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
13ee6 74 65 33 42 74 72 65 65 4c 61 73 74 28 42 74 43  te3BtreeLast(BtC
13ee7 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74  ursor *pCur, int
13ee8 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72   *pRes){.  int r
13ee9 63 3b 0a 20 0a 20 20 61 73 73 65 72 74 28 20 63  c;. .  assert( c
13eea 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
13eeb 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72  pCur) );.  asser
13eec 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
13eed 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72  _held(pCur->pBtr
13eee 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ee->db->mutex) )
13eef 3b 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52  ;.  rc = moveToR
13ef0 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28  oot(pCur);.  if(
13ef1 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
13ef2 7b 0a 20 20 20 20 69 66 28 20 43 55 52 53 4f 52  {.    if( CURSOR
13ef3 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e  _INVALID==pCur->
13ef4 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20 20 20  eState ){.      
13ef5 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70  assert( pCur->ap
13ef6 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
13ef7 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20  ]->nCell==0 );. 
13ef8 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a       *pRes = 1;.
13ef9 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
13efa 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65   assert( pCur->e
13efb 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41  State==CURSOR_VA
13efc 4c 49 44 20 29 3b 0a 20 20 20 20 20 20 2a 70 52  LID );.      *pR
13efd 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63  es = 0;.      rc
13efe 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f   = moveToRightmo
13eff 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20  st(pCur);.      
13f00 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72  getCellInfo(pCur
13f01 29 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61  );.      pCur->a
13f02 74 4c 61 73 74 20 3d 20 72 63 3d 3d 53 51 4c 49  tLast = rc==SQLI
13f03 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d  TE_OK;.    }.  }
13f04 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
13f05 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72  ./* Move the cur
13f06 73 6f 72 20 73 6f 20 74 68 61 74 20 69 74 20 70  sor so that it p
13f07 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65 6e 74 72  oints to an entr
13f08 79 20 6e 65 61 72 20 74 68 65 20 6b 65 79 20 0a  y near the key .
13f09 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 20  ** specified by 
13f0a 70 49 64 78 4b 65 79 20 6f 72 20 69 6e 74 4b 65  pIdxKey or intKe
13f0b 79 2e 20 20 20 52 65 74 75 72 6e 20 61 20 73 75  y.   Return a su
13f0c 63 63 65 73 73 20 63 6f 64 65 2e 0a 2a 2a 0a 2a  ccess code..**.*
13f0d 2a 20 46 6f 72 20 49 4e 54 4b 45 59 20 74 61 62  * For INTKEY tab
13f0e 6c 65 73 2c 20 74 68 65 20 69 6e 74 4b 65 79 20  les, the intKey 
13f0f 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 73 65  parameter is use
13f10 64 2e 20 20 70 49 64 78 4b 65 79 20 0a 2a 2a 20  d.  pIdxKey .** 
13f11 6d 75 73 74 20 62 65 20 4e 55 4c 4c 2e 20 20 46  must be NULL.  F
13f12 6f 72 20 69 6e 64 65 78 20 74 61 62 6c 65 73 2c  or index tables,
13f13 20 70 49 64 78 4b 65 79 20 69 73 20 75 73 65 64   pIdxKey is used
13f14 20 61 6e 64 20 69 6e 74 4b 65 79 0a 2a 2a 20 69   and intKey.** i
13f15 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a  s ignored..**.**
13f16 20 49 66 20 61 6e 20 65 78 61 63 74 20 6d 61 74   If an exact mat
13f17 63 68 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c  ch is not found,
13f18 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72   then the cursor
13f19 20 69 73 20 61 6c 77 61 79 73 0a 2a 2a 20 6c 65   is always.** le
13f1a 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61  ft pointing at a
13f1b 20 6c 65 61 66 20 70 61 67 65 20 77 68 69 63 68   leaf page which
13f1c 20 77 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20   would hold the 
13f1d 65 6e 74 72 79 20 69 66 20 69 74 0a 2a 2a 20 77  entry if it.** w
13f1e 65 72 65 20 70 72 65 73 65 6e 74 2e 20 20 54 68  ere present.  Th
13f1f 65 20 63 75 72 73 6f 72 20 6d 69 67 68 74 20 70  e cursor might p
13f20 6f 69 6e 74 20 74 6f 20 61 6e 20 65 6e 74 72 79  oint to an entry
13f21 20 74 68 61 74 20 63 6f 6d 65 73 0a 2a 2a 20 62   that comes.** b
13f22 65 66 6f 72 65 20 6f 72 20 61 66 74 65 72 20 74  efore or after t
13f23 68 65 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  he key..**.** Th
13f24 65 20 72 65 73 75 6c 74 20 6f 66 20 63 6f 6d 70  e result of comp
13f25 61 72 69 6e 67 20 74 68 65 20 6b 65 79 20 77 69  aring the key wi
13f26 74 68 20 74 68 65 20 65 6e 74 72 79 20 74 6f 20  th the entry to 
13f27 77 68 69 63 68 20 74 68 65 0a 2a 2a 20 63 75 72  which the.** cur
13f28 73 6f 72 20 69 73 20 77 72 69 74 74 65 6e 20 74  sor is written t
13f29 6f 20 2a 70 52 65 73 20 69 66 20 70 52 65 73 21  o *pRes if pRes!
13f2a 3d 4e 55 4c 4c 2e 20 20 54 68 65 20 6d 65 61 6e  =NULL.  The mean
13f2b 69 6e 67 20 6f 66 0a 2a 2a 20 74 68 69 73 20 76  ing of.** this v
13f2c 61 6c 75 65 20 69 73 20 61 73 20 66 6f 6c 6c 6f  alue is as follo
13f2d 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70  ws:.**.**     *p
13f2e 52 65 73 3c 30 20 20 20 20 20 20 54 68 65 20 63  Res<0      The c
13f2f 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f  ursor is left po
13f30 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74  inting at an ent
13f31 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20  ry that.**      
13f32 20 20 20 20 20 20 20 20 20 20 20 20 69 73 20 73              is s
13f33 6d 61 6c 6c 65 72 20 74 68 61 6e 20 70 4b 65 79  maller than pKey
13f34 20 6f 72 20 69 66 20 74 68 65 20 74 61 62 6c 65   or if the table
13f35 20 69 73 20 65 6d 70 74 79 0a 2a 2a 20 20 20 20   is empty.**    
13f36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6e                an
13f37 64 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  d the cursor is 
13f38 74 68 65 72 65 66 6f 72 65 20 6c 65 66 74 20 70  therefore left p
13f39 6f 69 6e 74 20 74 6f 20 6e 6f 74 68 69 6e 67 2e  oint to nothing.
13f3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73  .**.**     *pRes
13f3b 3d 3d 30 20 20 20 20 20 54 68 65 20 63 75 72 73  ==0     The curs
13f3c 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74  or is left point
13f3d 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20  ing at an entry 
13f3e 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  that.**         
13f3f 20 20 20 20 20 20 20 20 20 65 78 61 63 74 6c 79           exactly
13f40 20 6d 61 74 63 68 65 73 20 70 4b 65 79 2e 0a 2a   matches pKey..*
13f41 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3e 30  *.**     *pRes>0
13f42 20 20 20 20 20 20 54 68 65 20 63 75 72 73 6f 72        The cursor
13f43 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
13f44 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68  g at an entry th
13f45 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  at.**           
13f46 20 20 20 20 20 20 20 69 73 20 6c 61 72 67 65 72         is larger
13f47 20 74 68 61 6e 20 70 4b 65 79 2e 0a 2a 2a 0a 2a   than pKey..**.*
13f48 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
13f49 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
13f4a 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28  eMovetoUnpacked(
13f4b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75  .  BtCursor *pCu
13f4c 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  r,          /* T
13f4d 68 65 20 63 75 72 73 6f 72 20 74 6f 20 62 65 20  he cursor to be 
13f4e 6d 6f 76 65 64 20 2a 2f 0a 20 20 55 6e 70 61 63  moved */.  Unpac
13f4f 6b 65 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b  kedRecord *pIdxK
13f50 65 79 2c 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20  ey, /* Unpacked 
13f51 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 69  index key */.  i
13f52 36 34 20 69 6e 74 4b 65 79 2c 20 20 20 20 20 20  64 intKey,      
13f53 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
13f54 61 62 6c 65 20 6b 65 79 20 2a 2f 0a 20 20 69 6e  able key */.  in
13f55 74 20 62 69 61 73 52 69 67 68 74 2c 20 20 20 20  t biasRight,    
13f56 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75         /* If tru
13f57 65 2c 20 62 69 61 73 20 74 68 65 20 73 65 61 72  e, bias the sear
13f58 63 68 20 74 6f 20 74 68 65 20 68 69 67 68 20 65  ch to the high e
13f59 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65  nd */.  int *pRe
13f5a 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s               
13f5b 20 2f 2a 20 57 72 69 74 65 20 73 65 61 72 63 68   /* Write search
13f5c 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f   results here */
13f5d 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  .){.  int rc;.. 
13f5e 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
13f5f 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
13f60 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
13f61 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
13f62 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62  pCur->pBtree->db
13f63 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f  ->mutex) );..  /
13f64 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20  * If the cursor 
13f65 69 73 20 61 6c 72 65 61 64 79 20 70 6f 73 69 74  is already posit
13f66 69 6f 6e 65 64 20 61 74 20 74 68 65 20 70 6f 69  ioned at the poi
13f67 6e 74 20 77 65 20 61 72 65 20 74 72 79 69 6e 67  nt we are trying
13f68 0a 20 20 2a 2a 20 74 6f 20 6d 6f 76 65 20 74 6f  .  ** to move to
13f69 2c 20 74 68 65 6e 20 6a 75 73 74 20 72 65 74 75  , then just retu
13f6a 72 6e 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67  rn without doing
13f6b 20 61 6e 79 20 77 6f 72 6b 20 2a 2f 0a 20 20 69   any work */.  i
13f6c 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  f( pCur->eState=
13f6d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 26 26  =CURSOR_VALID &&
13f6e 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79   pCur->validNKey
13f6f 20 0a 20 20 20 26 26 20 70 43 75 72 2d 3e 61 70   .   && pCur->ap
13f70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20  Page[0]->intKey 
13f71 0a 20 20 29 7b 0a 20 20 20 20 69 66 28 20 70 43  .  ){.    if( pC
13f72 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 69  ur->info.nKey==i
13f73 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 2a  ntKey ){.      *
13f74 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20  pRes = 0;.      
13f75 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
13f76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
13f77 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 26 26 20  pCur->atLast && 
13f78 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3c  pCur->info.nKey<
13f79 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20  intKey ){.      
13f7a 2a 70 52 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20  *pRes = -1;.    
13f7b 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
13f7c 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  OK;.    }.  }.. 
13f7d 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74   rc = moveToRoot
13f7e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63  (pCur);.  if( rc
13f7f 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
13f80 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  c;.  }.  assert(
13f81 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
13f82 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20  ur->iPage] );.  
13f83 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70  assert( pCur->ap
13f84 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
13f85 5d 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 69  ]->isInit );.  i
13f86 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  f( pCur->eState=
13f87 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20  =CURSOR_INVALID 
13f88 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 2d  ){.    *pRes = -
13f89 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  1;.    assert( p
13f8a 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
13f8b 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d  ->iPage]->nCell=
13f8c 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  =0 );.    return
13f8d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
13f8e 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
13f8f 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65  apPage[0]->intKe
13f90 79 20 7c 7c 20 70 49 64 78 4b 65 79 20 29 3b 0a  y || pIdxKey );.
13f91 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 69    for(;;){.    i
13f92 6e 74 20 6c 77 72 2c 20 75 70 72 3b 0a 20 20 20  nt lwr, upr;.   
13f93 20 50 67 6e 6f 20 63 68 6c 64 50 67 3b 0a 20 20   Pgno chldPg;.  
13f94 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
13f95 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b   = pCur->apPage[
13f96 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20  pCur->iPage];.  
13f97 20 20 69 6e 74 20 63 20 3d 20 2d 31 3b 20 20 2f    int c = -1;  /
13f98 2a 20 70 52 65 73 20 72 65 74 75 72 6e 20 69 66  * pRes return if
13f99 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 20   table is empty 
13f9a 6d 75 73 74 20 62 65 20 2d 31 20 2a 2f 0a 20 20  must be -1 */.  
13f9b 20 20 6c 77 72 20 3d 20 30 3b 0a 20 20 20 20 75    lwr = 0;.    u
13f9c 70 72 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c  pr = pPage->nCel
13f9d 6c 2d 31 3b 0a 20 20 20 20 69 66 28 20 21 70 50  l-1;.    if( !pP
13f9e 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 70  age->intKey && p
13f9f 49 64 78 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20  IdxKey==0 ){.   
13fa0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43     rc = SQLITE_C
13fa1 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
13fa2 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66     goto moveto_f
13fa3 69 6e 69 73 68 3b 0a 20 20 20 20 7d 0a 20 20 20  inish;.    }.   
13fa4 20 69 66 28 20 62 69 61 73 52 69 67 68 74 20 29   if( biasRight )
13fa5 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69  {.      pCur->ai
13fa6 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
13fa7 20 3d 20 75 70 72 3b 0a 20 20 20 20 7d 65 6c 73   = upr;.    }els
13fa8 65 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61  e{.      pCur->a
13fa9 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
13faa 5d 20 3d 20 28 75 70 72 2b 6c 77 72 29 2f 32 3b  ] = (upr+lwr)/2;
13fab 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6c  .    }.    if( l
13fac 77 72 3c 3d 75 70 72 20 29 20 66 6f 72 28 3b 3b  wr<=upr ) for(;;
13fad 29 7b 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 70  ){.      void *p
13fae 43 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 69  CellKey;.      i
13faf 36 34 20 6e 43 65 6c 6c 4b 65 79 3b 0a 20 20 20  64 nCellKey;.   
13fb0 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75     int idx = pCu
13fb1 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
13fb2 50 61 67 65 5d 3b 0a 20 20 20 20 20 20 70 43 75  Page];.      pCu
13fb3 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20  r->info.nSize = 
13fb4 30 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 76  0;.      pCur->v
13fb5 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 0a 20 20  alidNKey = 1;.  
13fb6 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69      if( pPage->i
13fb7 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20  ntKey ){.       
13fb8 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20   u8 *pCell;.    
13fb9 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64      pCell = find
13fba 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29  Cell(pPage, idx)
13fbb 20 2b 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50   + pPage->childP
13fbc 74 72 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20  trSize;.        
13fbd 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61  if( pPage->hasDa
13fbe 74 61 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ta ){.          
13fbf 75 33 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20 20  u32 dummy;.     
13fc0 20 20 20 20 20 70 43 65 6c 6c 20 2b 3d 20 67 65       pCell += ge
13fc1 74 56 61 72 69 6e 74 33 32 28 70 43 65 6c 6c 2c  tVarint32(pCell,
13fc2 20 64 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20   dummy);.       
13fc3 20 7d 0a 20 20 20 20 20 20 20 20 67 65 74 56 61   }.        getVa
13fc4 72 69 6e 74 28 70 43 65 6c 6c 2c 20 28 75 36 34  rint(pCell, (u64
13fc5 2a 29 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20  *)&nCellKey);.  
13fc6 20 20 20 20 20 20 69 66 28 20 6e 43 65 6c 6c 4b        if( nCellK
13fc7 65 79 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20  ey==intKey ){.  
13fc8 20 20 20 20 20 20 20 20 63 20 3d 20 30 3b 0a 20          c = 0;. 
13fc9 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
13fca 20 6e 43 65 6c 6c 4b 65 79 3c 69 6e 74 4b 65 79   nCellKey<intKey
13fcb 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 20   ){.          c 
13fcc 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d 65  = -1;.        }e
13fcd 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61  lse{.          a
13fce 73 73 65 72 74 28 20 6e 43 65 6c 6c 4b 65 79 3e  ssert( nCellKey>
13fcf 69 6e 74 4b 65 79 20 29 3b 0a 20 20 20 20 20 20  intKey );.      
13fd0 20 20 20 20 63 20 3d 20 2b 31 3b 0a 20 20 20 20      c = +1;.    
13fd1 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
13fd2 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 61  e{.        int a
13fd3 76 61 69 6c 61 62 6c 65 3b 0a 20 20 20 20 20 20  vailable;.      
13fd4 20 20 70 43 65 6c 6c 4b 65 79 20 3d 20 28 76 6f    pCellKey = (vo
13fd5 69 64 20 2a 29 66 65 74 63 68 50 61 79 6c 6f 61  id *)fetchPayloa
13fd6 64 28 70 43 75 72 2c 20 26 61 76 61 69 6c 61 62  d(pCur, &availab
13fd7 6c 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  le, 0);.        
13fd8 6e 43 65 6c 6c 4b 65 79 20 3d 20 70 43 75 72 2d  nCellKey = pCur-
13fd9 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20  >info.nKey;.    
13fda 20 20 20 20 69 66 28 20 61 76 61 69 6c 61 62 6c      if( availabl
13fdb 65 3e 3d 6e 43 65 6c 6c 4b 65 79 20 29 7b 0a 20  e>=nCellKey ){. 
13fdc 20 20 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c           c = sql
13fdd 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f  ite3VdbeRecordCo
13fde 6d 70 61 72 65 28 6e 43 65 6c 6c 4b 65 79 2c 20  mpare(nCellKey, 
13fdf 70 43 65 6c 6c 4b 65 79 2c 20 70 49 64 78 4b 65  pCellKey, pIdxKe
13fe0 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  y);.        }els
13fe1 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43 65  e{.          pCe
13fe2 6c 6c 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 4d  llKey = sqlite3M
13fe3 61 6c 6c 6f 63 28 20 6e 43 65 6c 6c 4b 65 79 20  alloc( nCellKey 
13fe4 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
13fe5 20 70 43 65 6c 6c 4b 65 79 3d 3d 30 20 29 7b 0a   pCellKey==0 ){.
13fe6 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
13fe7 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
13fe8 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20             goto 
13fe9 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20  moveto_finish;. 
13fea 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
13feb 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
13fec 33 42 74 72 65 65 4b 65 79 28 70 43 75 72 2c 20  3BtreeKey(pCur, 
13fed 30 2c 20 6e 43 65 6c 6c 4b 65 79 2c 20 28 76 6f  0, nCellKey, (vo
13fee 69 64 20 2a 29 70 43 65 6c 6c 4b 65 79 29 3b 0a  id *)pCellKey);.
13fef 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 73 71            c = sq
13ff0 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43  lite3VdbeRecordC
13ff1 6f 6d 70 61 72 65 28 6e 43 65 6c 6c 4b 65 79 2c  ompare(nCellKey,
13ff2 20 70 43 65 6c 6c 4b 65 79 2c 20 70 49 64 78 4b   pCellKey, pIdxK
13ff3 65 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  ey);.          s
13ff4 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 65 6c  qlite3_free(pCel
13ff5 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 20  lKey);.         
13ff6 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 6d   if( rc ) goto m
13ff7 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20  oveto_finish;.  
13ff8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
13ff9 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29        if( c==0 )
13ffa 7b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e  {.        pCur->
13ffb 69 6e 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c  info.nKey = nCel
13ffc 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 69 66  lKey;.        if
13ffd 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20  ( pPage->intKey 
13ffe 26 26 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20  && !pPage->leaf 
13fff 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 77 72  ){.          lwr
14000 20 3d 20 69 64 78 3b 0a 20 20 20 20 20 20 20 20   = idx;.        
14001 20 20 75 70 72 20 3d 20 6c 77 72 20 2d 20 31 3b    upr = lwr - 1;
14002 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
14003 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
14004 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
14005 52 65 73 20 29 20 2a 70 52 65 73 20 3d 20 30 3b  Res ) *pRes = 0;
14006 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
14007 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
14008 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f       goto moveto
14009 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 20  _finish;.       
1400a 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
1400b 20 69 66 28 20 63 3c 30 20 29 7b 0a 20 20 20 20   if( c<0 ){.    
1400c 20 20 20 20 6c 77 72 20 3d 20 69 64 78 2b 31 3b      lwr = idx+1;
1400d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1400e 20 20 20 20 20 20 75 70 72 20 3d 20 69 64 78 2d        upr = idx-
1400f 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
14010 20 69 66 28 20 6c 77 72 3e 75 70 72 20 29 7b 0a   if( lwr>upr ){.
14011 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e          pCur->in
14012 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b  fo.nKey = nCellK
14013 65 79 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  ey;.        brea
14014 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
14015 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
14016 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28 6c 77 72  r->iPage] = (lwr
14017 2b 75 70 72 29 2f 32 3b 0a 20 20 20 20 7d 0a 20  +upr)/2;.    }. 
14018 20 20 20 61 73 73 65 72 74 28 20 6c 77 72 3d 3d     assert( lwr==
14019 75 70 72 2b 31 20 29 3b 0a 20 20 20 20 61 73 73  upr+1 );.    ass
1401a 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e  ert( pPage->isIn
1401b 69 74 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50  it );.    if( pP
1401c 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
1401d 20 20 20 63 68 6c 64 50 67 20 3d 20 30 3b 0a 20     chldPg = 0;. 
1401e 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 77 72     }else if( lwr
1401f 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29  >=pPage->nCell )
14020 7b 0a 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d  {.      chldPg =
14021 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65   get4byte(&pPage
14022 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68  ->aData[pPage->h
14023 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20  drOffset+8]);.  
14024 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63    }else{.      c
14025 68 6c 64 50 67 20 3d 20 67 65 74 34 62 79 74 65  hldPg = get4byte
14026 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c  (findCell(pPage,
14027 20 6c 77 72 29 29 3b 0a 20 20 20 20 7d 0a 20 20   lwr));.    }.  
14028 20 20 69 66 28 20 63 68 6c 64 50 67 3d 3d 30 20    if( chldPg==0 
14029 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
1402a 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
1402b 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e  r->iPage]<pCur->
1402c 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
1402d 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20  ge]->nCell );.  
1402e 20 20 20 20 69 66 28 20 70 52 65 73 20 29 20 2a      if( pRes ) *
1402f 70 52 65 73 20 3d 20 63 3b 0a 20 20 20 20 20 20  pRes = c;.      
14030 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
14031 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74        goto movet
14032 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 7d 0a  o_finish;.    }.
14033 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b      pCur->aiIdx[
14034 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 6c  pCur->iPage] = l
14035 77 72 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e  wr;.    pCur->in
14036 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20  fo.nSize = 0;.  
14037 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65    pCur->validNKe
14038 79 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20  y = 0;.    rc = 
14039 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72  moveToChild(pCur
1403a 2c 20 63 68 6c 64 50 67 29 3b 0a 20 20 20 20 69  , chldPg);.    i
1403b 66 28 20 72 63 20 29 20 67 6f 74 6f 20 6d 6f 76  f( rc ) goto mov
1403c 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 7d 0a  eto_finish;.  }.
1403d 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3a 0a 20  moveto_finish:. 
1403e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1403f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 76 65 72  *.** In this ver
14040 73 69 6f 6e 20 6f 66 20 42 74 72 65 65 4d 6f 76  sion of BtreeMov
14041 65 74 6f 2c 20 70 4b 65 79 20 69 73 20 61 20 70  eto, pKey is a p
14042 61 63 6b 65 64 20 69 6e 64 65 78 20 72 65 63 6f  acked index reco
14043 72 64 0a 2a 2a 20 73 75 63 68 20 61 73 20 69 73  rd.** such as is
14044 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 68   generated by th
14045 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20  e OP_MakeRecord 
14046 6f 70 63 6f 64 65 2e 20 20 55 6e 70 61 63 6b 20  opcode.  Unpack 
14047 74 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20 61 6e  the.** record an
14048 64 20 74 68 65 6e 20 63 61 6c 6c 20 42 74 72 65  d then call Btre
14049 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28  eMovetoUnpacked(
1404a 29 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b  ) to do the work
1404b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1404c 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
1404d 74 72 65 65 4d 6f 76 65 74 6f 28 0a 20 20 42 74  treeMoveto(.  Bt
1404e 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20  Cursor *pCur,   
1404f 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 70 65 6e    /* Cursor open
14050 20 6f 6e 20 74 68 65 20 62 74 72 65 65 20 74 6f   on the btree to
14051 20 62 65 20 73 65 61 72 63 68 65 64 20 2a 2f 0a   be searched */.
14052 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b    const void *pK
14053 65 79 2c 20 20 20 2f 2a 20 50 61 63 6b 65 64 20  ey,   /* Packed 
14054 6b 65 79 20 69 66 20 74 68 65 20 62 74 72 65 65  key if the btree
14055 20 69 73 20 61 6e 20 69 6e 64 65 78 20 2a 2f 0a   is an index */.
14056 20 20 69 36 34 20 6e 4b 65 79 2c 20 20 20 20 20    i64 nKey,     
14057 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72        /* Integer
14058 20 6b 65 79 20 66 6f 72 20 74 61 62 6c 65 73 2e   key for tables.
14059 20 20 53 69 7a 65 20 6f 66 20 70 4b 65 79 20 66    Size of pKey f
1405a 6f 72 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20  or indices */.  
1405b 69 6e 74 20 62 69 61 73 2c 20 20 20 20 20 20 20  int bias,       
1405c 20 20 20 20 2f 2a 20 42 69 61 73 20 73 65 61 72      /* Bias sear
1405d 63 68 20 74 6f 20 74 68 65 20 68 69 67 68 20 65  ch to the high e
1405e 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65  nd */.  int *pRe
1405f 73 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57  s           /* W
14060 72 69 74 65 20 73 65 61 72 63 68 20 72 65 73 75  rite search resu
14061 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  lts here */.){. 
14062 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
14063 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
14064 74 61 74 75 73 20 63 6f 64 65 20 2a 2f 0a 20 20  tatus code */.  
14065 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a  UnpackedRecord *
14066 70 49 64 78 4b 65 79 3b 20 20 20 2f 2a 20 55 6e  pIdxKey;   /* Un
14067 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79  packed index key
14068 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52 65   */.  UnpackedRe
14069 63 6f 72 64 20 61 53 70 61 63 65 5b 31 36 5d 3b  cord aSpace[16];
1406a 20 2f 2a 20 54 65 6d 70 20 73 70 61 63 65 20 66   /* Temp space f
1406b 6f 72 20 70 49 64 78 4b 65 79 20 2d 20 74 6f 20  or pIdxKey - to 
1406c 61 76 6f 69 64 20 61 20 6d 61 6c 6c 6f 63 20 2a  avoid a malloc *
1406d 2f 0a 0a 20 20 69 66 28 20 70 4b 65 79 20 29 7b  /..  if( pKey ){
1406e 0a 20 20 20 20 70 49 64 78 4b 65 79 20 3d 20 73  .    pIdxKey = s
1406f 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64  qlite3VdbeRecord
14070 55 6e 70 61 63 6b 28 70 43 75 72 2d 3e 70 4b 65  Unpack(pCur->pKe
14071 79 49 6e 66 6f 2c 20 6e 4b 65 79 2c 20 70 4b 65  yInfo, nKey, pKe
14072 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  y,.             
14073 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14074 20 20 20 20 20 20 20 20 20 61 53 70 61 63 65 2c           aSpace,
14075 20 73 69 7a 65 6f 66 28 61 53 70 61 63 65 29 29   sizeof(aSpace))
14076 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78 4b 65  ;.    if( pIdxKe
14077 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51  y==0 ) return SQ
14078 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65  LITE_NOMEM;.  }e
14079 6c 73 65 7b 0a 20 20 20 20 70 49 64 78 4b 65 79  lse{.    pIdxKey
1407a 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 63 20 3d   = 0;.  }.  rc =
1407b 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
1407c 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 75 72  etoUnpacked(pCur
1407d 2c 20 70 49 64 78 4b 65 79 2c 20 6e 4b 65 79 2c  , pIdxKey, nKey,
1407e 20 62 69 61 73 2c 20 70 52 65 73 29 3b 0a 20 20   bias, pRes);.  
1407f 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20  if( pKey ){.    
14080 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74  sqlite3VdbeDelet
14081 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28  eUnpackedRecord(
14082 70 49 64 78 4b 65 79 29 3b 0a 20 20 7d 0a 20 20  pIdxKey);.  }.  
14083 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
14084 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45  *.** Return TRUE
14085 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 69   if the cursor i
14086 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 61  s not pointing a
14087 74 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68  t an entry of th
14088 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  e table..**.** T
14089 52 55 45 20 77 69 6c 6c 20 62 65 20 72 65 74 75  RUE will be retu
1408a 72 6e 65 64 20 61 66 74 65 72 20 61 20 63 61 6c  rned after a cal
1408b 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65  l to sqlite3Btre
1408c 65 4e 65 78 74 28 29 20 6d 6f 76 65 73 0a 2a 2a  eNext() moves.**
1408d 20 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 65   past the last e
1408e 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c  ntry in the tabl
1408f 65 20 6f 72 20 73 71 6c 69 74 65 33 42 74 72 65  e or sqlite3Btre
14090 65 50 72 65 76 28 29 20 6d 6f 76 65 73 20 70 61  ePrev() moves pa
14091 73 74 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20  st.** the first 
14092 65 6e 74 72 79 2e 20 20 54 52 55 45 20 69 73 20  entry.  TRUE is 
14093 61 6c 73 6f 20 72 65 74 75 72 6e 65 64 20 69 66  also returned if
14094 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d   the table is em
14095 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  pty..*/.SQLITE_P
14096 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
14097 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 75 72  e3BtreeEof(BtCur
14098 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 2f 2a  sor *pCur){.  /*
14099 20 54 4f 44 4f 3a 20 57 68 61 74 20 69 66 20 74   TODO: What if t
1409a 68 65 20 63 75 72 73 6f 72 20 69 73 20 69 6e 20  he cursor is in 
1409b 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45  CURSOR_REQUIRESE
1409c 45 4b 20 62 75 74 20 61 6c 6c 20 74 61 62 6c 65  EK but all table
1409d 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20 68 61   entries.  ** ha
1409e 76 65 20 62 65 65 6e 20 64 65 6c 65 74 65 64 3f  ve been deleted?
1409f 20 54 68 69 73 20 41 50 49 20 77 69 6c 6c 20 6e   This API will n
140a0 65 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 6f  eed to change to
140a1 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   return an error
140a2 20 63 6f 64 65 0a 20 20 2a 2a 20 61 73 20 77 65   code.  ** as we
140a3 6c 6c 20 61 73 20 74 68 65 20 62 6f 6f 6c 65 61  ll as the boolea
140a4 6e 20 72 65 73 75 6c 74 20 76 61 6c 75 65 2e 0a  n result value..
140a5 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 43    */.  return (C
140a6 55 52 53 4f 52 5f 56 41 4c 49 44 21 3d 70 43 75  URSOR_VALID!=pCu
140a7 72 2d 3e 65 53 74 61 74 65 29 3b 0a 7d 0a 0a 2f  r->eState);.}../
140a8 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
140a9 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
140aa 69 6f 6e 20 68 61 6e 64 6c 65 20 66 6f 72 20 61  ion handle for a
140ab 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a 53 51 4c 49   cursor..*/.SQLI
140ac 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74  TE_PRIVATE sqlit
140ad 65 33 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  e3 *sqlite3Btree
140ae 43 75 72 73 6f 72 44 62 28 63 6f 6e 73 74 20 42  CursorDb(const B
140af 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a  tCursor *pCur){.
140b0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
140b1 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75  3_mutex_held(pCu
140b2 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d  r->pBtree->db->m
140b3 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72  utex) );.  retur
140b4 6e 20 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e  n pCur->pBtree->
140b5 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 76  db;.}../*.** Adv
140b6 61 6e 63 65 20 74 68 65 20 63 75 72 73 6f 72 20  ance the cursor 
140b7 74 6f 20 74 68 65 20 6e 65 78 74 20 65 6e 74 72  to the next entr
140b8 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  y in the databas
140b9 65 2e 20 20 49 66 0a 2a 2a 20 73 75 63 63 65 73  e.  If.** succes
140ba 73 66 75 6c 20 74 68 65 6e 20 73 65 74 20 2a 70  sful then set *p
140bb 52 65 73 3d 30 2e 20 20 49 66 20 74 68 65 20 63  Res=0.  If the c
140bc 75 72 73 6f 72 0a 2a 2a 20 77 61 73 20 61 6c 72  ursor.** was alr
140bd 65 61 64 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f  eady pointing to
140be 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20   the last entry 
140bf 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
140c0 62 65 66 6f 72 65 0a 2a 2a 20 74 68 69 73 20 72  before.** this r
140c1 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65  outine was calle
140c2 64 2c 20 74 68 65 6e 20 73 65 74 20 2a 70 52 65  d, then set *pRe
140c3 73 3d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  s=1..*/.SQLITE_P
140c4 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
140c5 65 33 42 74 72 65 65 4e 65 78 74 28 42 74 43 75  e3BtreeNext(BtCu
140c6 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20  rsor *pCur, int 
140c7 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63  *pRes){.  int rc
140c8 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 4d  ;.  int idx;.  M
140c9 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 0a  emPage *pPage;..
140ca 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
140cb 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
140cc 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f   );.  rc = resto
140cd 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  reCursorPosition
140ce 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63  (pCur);.  if( rc
140cf 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
140d0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
140d1 7d 0a 20 20 61 73 73 65 72 74 28 20 70 52 65 73  }.  assert( pRes
140d2 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 43 55 52  !=0 );.  if( CUR
140d3 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75  SOR_INVALID==pCu
140d4 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 20 20  r->eState ){.   
140d5 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20   *pRes = 1;.    
140d6 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
140d7 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 75 72  ;.  }.  if( pCur
140d8 2d 3e 73 6b 69 70 3e 30 20 29 7b 0a 20 20 20 20  ->skip>0 ){.    
140d9 70 43 75 72 2d 3e 73 6b 69 70 20 3d 20 30 3b 0a  pCur->skip = 0;.
140da 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20      *pRes = 0;. 
140db 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
140dc 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d  _OK;.  }.  pCur-
140dd 3e 73 6b 69 70 20 3d 20 30 3b 0a 0a 20 20 70 50  >skip = 0;..  pP
140de 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61  age = pCur->apPa
140df 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b  ge[pCur->iPage];
140e0 0a 20 20 69 64 78 20 3d 20 2b 2b 70 43 75 72 2d  .  idx = ++pCur-
140e1 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
140e2 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ge];.  assert( p
140e3 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a  Page->isInit );.
140e4 20 20 61 73 73 65 72 74 28 20 69 64 78 3c 3d 70    assert( idx<=p
140e5 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a  Page->nCell );..
140e6 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69    pCur->info.nSi
140e7 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e  ze = 0;.  pCur->
140e8 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20  validNKey = 0;. 
140e9 20 69 66 28 20 69 64 78 3e 3d 70 50 61 67 65 2d   if( idx>=pPage-
140ea 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 69 66  >nCell ){.    if
140eb 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  ( !pPage->leaf )
140ec 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76  {.      rc = mov
140ed 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 67  eToChild(pCur, g
140ee 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e  et4byte(&pPage->
140ef 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72  aData[pPage->hdr
140f0 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a 20 20 20  Offset+8]));.   
140f1 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75     if( rc ) retu
140f2 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 72 63 20  rn rc;.      rc 
140f3 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74  = moveToLeftmost
140f4 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 2a 70  (pCur);.      *p
140f5 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72  Res = 0;.      r
140f6 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
140f7 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 69 66      do{.      if
140f8 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30  ( pCur->iPage==0
140f9 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 52 65   ){.        *pRe
140fa 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70  s = 1;.        p
140fb 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55  Cur->eState = CU
140fc 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20  RSOR_INVALID;.  
140fd 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
140fe 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a  ITE_OK;.      }.
140ff 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
14100 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e 74 28 70  eeMoveToParent(p
14101 43 75 72 29 3b 0a 20 20 20 20 20 20 70 50 61 67  Cur);.      pPag
14102 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65  e = pCur->apPage
14103 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20  [pCur->iPage];. 
14104 20 20 20 7d 77 68 69 6c 65 28 20 70 43 75 72 2d     }while( pCur-
14105 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
14106 67 65 5d 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c  ge]>=pPage->nCel
14107 6c 20 29 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d  l );.    *pRes =
14108 20 30 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67   0;.    if( pPag
14109 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20  e->intKey ){.   
1410a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1410b 74 72 65 65 4e 65 78 74 28 70 43 75 72 2c 20 70  treeNext(pCur, p
1410c 52 65 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  Res);.    }else{
1410d 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
1410e 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20  TE_OK;.    }.   
1410f 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
14110 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 69    *pRes = 0;.  i
14111 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  f( pPage->leaf )
14112 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
14113 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 63  ITE_OK;.  }.  rc
14114 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73   = moveToLeftmos
14115 74 28 70 43 75 72 29 3b 0a 20 20 72 65 74 75 72  t(pCur);.  retur
14116 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  n rc;.}.../*.** 
14117 53 74 65 70 20 74 68 65 20 63 75 72 73 6f 72 20  Step the cursor 
14118 74 6f 20 74 68 65 20 62 61 63 6b 20 74 6f 20 74  to the back to t
14119 68 65 20 70 72 65 76 69 6f 75 73 20 65 6e 74 72  he previous entr
1411a 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  y in the databas
1411b 65 2e 20 20 49 66 0a 2a 2a 20 73 75 63 63 65 73  e.  If.** succes
1411c 73 66 75 6c 20 74 68 65 6e 20 73 65 74 20 2a 70  sful then set *p
1411d 52 65 73 3d 30 2e 20 20 49 66 20 74 68 65 20 63  Res=0.  If the c
1411e 75 72 73 6f 72 0a 2a 2a 20 77 61 73 20 61 6c 72  ursor.** was alr
1411f 65 61 64 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f  eady pointing to
14120 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79   the first entry
14121 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
14122 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 69 73 20   before.** this 
14123 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c 6c  routine was call
14124 65 64 2c 20 74 68 65 6e 20 73 65 74 20 2a 70 52  ed, then set *pR
14125 65 73 3d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  es=1..*/.SQLITE_
14126 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
14127 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73  te3BtreePrevious
14128 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c  (BtCursor *pCur,
14129 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 69   int *pRes){.  i
1412a 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65  nt rc;.  MemPage
1412b 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65   *pPage;..  asse
1412c 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  rt( cursorHoldsM
1412d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20  utex(pCur) );.  
1412e 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73  rc = restoreCurs
1412f 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29  orPosition(pCur)
14130 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
14131 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74  TE_OK ){.    ret
14132 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 43  urn rc;.  }.  pC
14133 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 30 3b 0a  ur->atLast = 0;.
14134 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56    if( CURSOR_INV
14135 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61  ALID==pCur->eSta
14136 74 65 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20  te ){.    *pRes 
14137 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 1;.    return 
14138 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
14139 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 3c   if( pCur->skip<
1413a 30 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 73  0 ){.    pCur->s
1413b 6b 69 70 20 3d 20 30 3b 0a 20 20 20 20 2a 70 52  kip = 0;.    *pR
1413c 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75  es = 0;.    retu
1413d 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
1413e 7d 0a 20 20 70 43 75 72 2d 3e 73 6b 69 70 20 3d  }.  pCur->skip =
1413f 20 30 3b 0a 0a 20 20 70 50 61 67 65 20 3d 20 70   0;..  pPage = p
14140 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
14141 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65  ->iPage];.  asse
14142 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69  rt( pPage->isIni
14143 74 20 29 3b 0a 20 20 69 66 28 20 21 70 50 61 67  t );.  if( !pPag
14144 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 69  e->leaf ){.    i
14145 6e 74 20 69 64 78 20 3d 20 70 43 75 72 2d 3e 61  nt idx = pCur->a
14146 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
14147 5d 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65  ];.    rc = move
14148 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 67 65  ToChild(pCur, ge
14149 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28  t4byte(findCell(
1414a 70 50 61 67 65 2c 20 69 64 78 29 29 29 3b 0a 20  pPage, idx)));. 
1414b 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
1414c 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
1414d 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76    }.    rc = mov
1414e 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 70 43 75  eToRightmost(pCu
1414f 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  r);.  }else{.   
14150 20 77 68 69 6c 65 28 20 70 43 75 72 2d 3e 61 69   while( pCur->ai
14151 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
14152 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ==0 ){.      if(
14153 20 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20   pCur->iPage==0 
14154 29 7b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d  ){.        pCur-
14155 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52  >eState = CURSOR
14156 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 20  _INVALID;.      
14157 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20    *pRes = 1;.   
14158 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
14159 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20  TE_OK;.      }. 
1415a 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
1415b 65 4d 6f 76 65 54 6f 50 61 72 65 6e 74 28 70 43  eMoveToParent(pC
1415c 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ur);.    }.    p
1415d 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20  Cur->info.nSize 
1415e 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76  = 0;.    pCur->v
1415f 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 0a 20  alidNKey = 0;.. 
14160 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70     pCur->aiIdx[p
14161 43 75 72 2d 3e 69 50 61 67 65 5d 2d 2d 3b 0a 20  Cur->iPage]--;. 
14162 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d     pPage = pCur-
14163 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
14164 61 67 65 5d 3b 0a 20 20 20 20 69 66 28 20 70 50  age];.    if( pP
14165 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21  age->intKey && !
14166 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20  pPage->leaf ){. 
14167 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
14168 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 70  3BtreePrevious(p
14169 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 20 20 20  Cur, pRes);.    
1416a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
1416b 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
1416c 20 7d 0a 20 20 7d 0a 20 20 2a 70 52 65 73 20 3d   }.  }.  *pRes =
1416d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   0;.  return rc;
1416e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  .}../*.** Alloca
1416f 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 66 72  te a new page fr
14170 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20  om the database 
14171 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  file..**.** The 
14172 6e 65 77 20 70 61 67 65 20 69 73 20 6d 61 72 6b  new page is mark
14173 65 64 20 61 73 20 64 69 72 74 79 2e 20 20 28 49  ed as dirty.  (I
14174 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 73  n other words, s
14175 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
14176 28 29 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 64  ().** has alread
14177 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 6f 6e  y been called on
14178 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e 29 20   the new page.) 
14179 20 54 68 65 20 6e 65 77 20 70 61 67 65 20 68 61   The new page ha
1417a 73 20 61 6c 73 6f 0a 2a 2a 20 62 65 65 6e 20 72  s also.** been r
1417b 65 66 65 72 65 6e 63 65 64 20 61 6e 64 20 74 68  eferenced and th
1417c 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e  e calling routin
1417d 65 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65  e is responsible
1417e 20 66 6f 72 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20   for calling.** 
1417f 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
14180 66 28 29 20 6f 6e 20 74 68 65 20 6e 65 77 20 70  f() on the new p
14181 61 67 65 20 77 68 65 6e 20 69 74 20 69 73 20 64  age when it is d
14182 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54  one..**.** SQLIT
14183 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
14184 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 41 6e   on success.  An
14185 79 20 6f 74 68 65 72 20 72 65 74 75 72 6e 20 76  y other return v
14186 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 0a 2a  alue indicates.*
14187 2a 20 61 6e 20 65 72 72 6f 72 2e 20 20 2a 70 70  * an error.  *pp
14188 50 61 67 65 20 61 6e 64 20 2a 70 50 67 6e 6f 20  Page and *pPgno 
14189 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20 69 6e  are undefined in
1418a 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 6e   the event of an
1418b 20 65 72 72 6f 72 2e 0a 2a 2a 20 44 6f 20 6e 6f   error..** Do no
1418c 74 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33  t invoke sqlite3
1418d 50 61 67 65 72 55 6e 72 65 66 28 29 20 6f 6e 20  PagerUnref() on 
1418e 2a 70 70 50 61 67 65 20 69 66 20 61 6e 20 65 72  *ppPage if an er
1418f 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ror is returned.
14190 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 6e  .**.** If the "n
14191 65 61 72 62 79 22 20 70 61 72 61 6d 65 74 65 72  earby" parameter
14192 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20   is not 0, then 
14193 61 20 28 66 65 65 62 6c 65 29 20 65 66 66 6f 72  a (feeble) effor
14194 74 20 69 73 20 6d 61 64 65 20 74 6f 20 0a 2a 2a  t is made to .**
14195 20 6c 6f 63 61 74 65 20 61 20 70 61 67 65 20 63   locate a page c
14196 6c 6f 73 65 20 74 6f 20 74 68 65 20 70 61 67 65  lose to the page
14197 20 6e 75 6d 62 65 72 20 22 6e 65 61 72 62 79 22   number "nearby"
14198 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75  .  This can be u
14199 73 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 61 74 74  sed in an.** att
1419a 65 6d 70 74 20 74 6f 20 6b 65 65 70 20 72 65 6c  empt to keep rel
1419b 61 74 65 64 20 70 61 67 65 73 20 63 6c 6f 73 65  ated pages close
1419c 20 74 6f 20 65 61 63 68 20 6f 74 68 65 72 20 69   to each other i
1419d 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
1419e 69 6c 65 2c 0a 2a 2a 20 77 68 69 63 68 20 69 6e  ile,.** which in
1419f 20 74 75 72 6e 20 63 61 6e 20 6d 61 6b 65 20 64   turn can make d
141a0 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20 66  atabase access f
141a1 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  aster..**.** If 
141a2 74 68 65 20 22 65 78 61 63 74 22 20 70 61 72 61  the "exact" para
141a3 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 30 2c 20  meter is not 0, 
141a4 61 6e 64 20 74 68 65 20 70 61 67 65 2d 6e 75 6d  and the page-num
141a5 62 65 72 20 6e 65 61 72 62 79 20 65 78 69 73 74  ber nearby exist
141a6 73 20 0a 2a 2a 20 61 6e 79 77 68 65 72 65 20 6f  s .** anywhere o
141a7 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2c  n the free-list,
141a8 20 74 68 65 6e 20 69 74 20 69 73 20 67 75 61 72   then it is guar
141a9 65 6e 74 65 65 64 20 74 6f 20 62 65 20 72 65 74  enteed to be ret
141aa 75 72 6e 65 64 2e 20 54 68 69 73 0a 2a 2a 20 69  urned. This.** i
141ab 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 20 61  s only used by a
141ac 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62  uto-vacuum datab
141ad 61 73 65 73 20 77 68 65 6e 20 61 6c 6c 6f 63 61  ases when alloca
141ae 74 69 6e 67 20 61 20 6e 65 77 20 74 61 62 6c 65  ting a new table
141af 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
141b0 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67  allocateBtreePag
141b1 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  e(.  BtShared *p
141b2 42 74 2c 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a  Bt, .  MemPage *
141b3 2a 70 70 50 61 67 65 2c 20 0a 20 20 50 67 6e 6f  *ppPage, .  Pgno
141b4 20 2a 70 50 67 6e 6f 2c 20 0a 20 20 50 67 6e 6f   *pPgno, .  Pgno
141b5 20 6e 65 61 72 62 79 2c 0a 20 20 75 38 20 65 78   nearby,.  u8 ex
141b6 61 63 74 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65  act.){.  MemPage
141b7 20 2a 70 50 61 67 65 31 3b 0a 20 20 69 6e 74 20   *pPage1;.  int 
141b8 72 63 3b 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20  rc;.  int n;    
141b9 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
141ba 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c  ges on the freel
141bb 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6b 3b 20  ist */.  int k; 
141bc 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
141bd 20 6c 65 61 76 65 73 20 6f 6e 20 74 68 65 20 74   leaves on the t
141be 72 75 6e 6b 20 6f 66 20 74 68 65 20 66 72 65 65  runk of the free
141bf 6c 69 73 74 20 2a 2f 0a 20 20 4d 65 6d 50 61 67  list */.  MemPag
141c0 65 20 2a 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20  e *pTrunk = 0;. 
141c1 20 4d 65 6d 50 61 67 65 20 2a 70 50 72 65 76 54   MemPage *pPrevT
141c2 72 75 6e 6b 20 3d 20 30 3b 0a 0a 20 20 61 73 73  runk = 0;..  ass
141c3 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
141c4 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74  ex_held(pBt->mut
141c5 65 78 29 20 29 3b 0a 20 20 70 50 61 67 65 31 20  ex) );.  pPage1 
141c6 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a 20  = pBt->pPage1;. 
141c7 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26 70   n = get4byte(&p
141c8 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d  Page1->aData[36]
141c9 29 3b 0a 20 20 69 66 28 20 6e 3e 30 20 29 7b 0a  );.  if( n>0 ){.
141ca 20 20 20 20 2f 2a 20 54 68 65 72 65 20 61 72 65      /* There are
141cb 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72   pages on the fr
141cc 65 65 6c 69 73 74 2e 20 20 52 65 75 73 65 20 6f  eelist.  Reuse o
141cd 6e 65 20 6f 66 20 74 68 6f 73 65 20 70 61 67 65  ne of those page
141ce 73 2e 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 69  s. */.    Pgno i
141cf 54 72 75 6e 6b 3b 0a 20 20 20 20 75 38 20 73 65  Trunk;.    u8 se
141d0 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 20 2f 2a  archList = 0; /*
141d1 20 49 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73   If the free-lis
141d2 74 20 6d 75 73 74 20 62 65 20 73 65 61 72 63 68  t must be search
141d3 65 64 20 66 6f 72 20 27 6e 65 61 72 62 79 27 20  ed for 'nearby' 
141d4 2a 2f 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20 49  */.    .    /* I
141d5 66 20 74 68 65 20 27 65 78 61 63 74 27 20 70 61  f the 'exact' pa
141d6 72 61 6d 65 74 65 72 20 77 61 73 20 74 72 75 65  rameter was true
141d7 20 61 6e 64 20 61 20 71 75 65 72 79 20 6f 66 20   and a query of 
141d8 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a  the pointer-map.
141d9 20 20 20 20 2a 2a 20 73 68 6f 77 73 20 74 68 61      ** shows tha
141da 74 20 74 68 65 20 70 61 67 65 20 27 6e 65 61 72  t the page 'near
141db 62 79 27 20 69 73 20 73 6f 6d 65 77 68 65 72 65  by' is somewhere
141dc 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73   on the free-lis
141dd 74 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 74  t, then.    ** t
141de 68 65 20 65 6e 74 69 72 65 2d 6c 69 73 74 20 77  he entire-list w
141df 69 6c 6c 20 62 65 20 73 65 61 72 63 68 65 64 20  ill be searched 
141e0 66 6f 72 20 74 68 61 74 20 70 61 67 65 2e 0a 20  for that page.. 
141e1 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51     */.#ifndef SQ
141e2 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
141e3 43 55 55 4d 0a 20 20 20 20 69 66 28 20 65 78 61  CUUM.    if( exa
141e4 63 74 20 26 26 20 6e 65 61 72 62 79 3c 3d 70 61  ct && nearby<=pa
141e5 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74  gerPagecount(pBt
141e6 2d 3e 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20  ->pPager) ){.   
141e7 20 20 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20     u8 eType;.   
141e8 20 20 20 61 73 73 65 72 74 28 20 6e 65 61 72 62     assert( nearb
141e9 79 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  y>0 );.      ass
141ea 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61  ert( pBt->autoVa
141eb 63 75 75 6d 20 29 3b 0a 20 20 20 20 20 20 72 63  cuum );.      rc
141ec 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70 42 74   = ptrmapGet(pBt
141ed 2c 20 6e 65 61 72 62 79 2c 20 26 65 54 79 70 65  , nearby, &eType
141ee 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 0);.      if( 
141ef 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
141f0 20 20 20 20 20 20 69 66 28 20 65 54 79 70 65 3d        if( eType=
141f1 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45  =PTRMAP_FREEPAGE
141f2 20 29 7b 0a 20 20 20 20 20 20 20 20 73 65 61 72   ){.        sear
141f3 63 68 4c 69 73 74 20 3d 20 31 3b 0a 20 20 20 20  chList = 1;.    
141f4 20 20 7d 0a 20 20 20 20 20 20 2a 70 50 67 6e 6f    }.      *pPgno
141f5 20 3d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 7d   = nearby;.    }
141f6 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20  .#endif..    /* 
141f7 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 66 72  Decrement the fr
141f8 65 65 2d 6c 69 73 74 20 63 6f 75 6e 74 20 62 79  ee-list count by
141f9 20 31 2e 20 53 65 74 20 69 54 72 75 6e 6b 20 74   1. Set iTrunk t
141fa 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  o the index of t
141fb 68 65 0a 20 20 20 20 2a 2a 20 66 69 72 73 74 20  he.    ** first 
141fc 66 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20  free-list trunk 
141fd 70 61 67 65 2e 20 69 50 72 65 76 54 72 75 6e 6b  page. iPrevTrunk
141fe 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 31 2e   is initially 1.
141ff 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d  .    */.    rc =
14200 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
14201 74 65 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61  te(pPage1->pDbPa
14202 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  ge);.    if( rc 
14203 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
14204 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65   put4byte(&pPage
14205 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 6e 2d  1->aData[36], n-
14206 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20  1);..    /* The 
14207 63 6f 64 65 20 77 69 74 68 69 6e 20 74 68 69 73  code within this
14208 20 6c 6f 6f 70 20 69 73 20 72 75 6e 20 6f 6e 6c   loop is run onl
14209 79 20 6f 6e 63 65 20 69 66 20 74 68 65 20 27 73  y once if the 's
1420a 65 61 72 63 68 4c 69 73 74 27 20 76 61 72 69 61  earchList' varia
1420b 62 6c 65 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f  ble.    ** is no
1420c 74 20 74 72 75 65 2e 20 4f 74 68 65 72 77 69 73  t true. Otherwis
1420d 65 2c 20 69 74 20 72 75 6e 73 20 6f 6e 63 65 20  e, it runs once 
1420e 66 6f 72 20 65 61 63 68 20 74 72 75 6e 6b 2d 70  for each trunk-p
1420f 61 67 65 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a  age on the.    *
14210 2a 20 66 72 65 65 2d 6c 69 73 74 20 75 6e 74 69  * free-list unti
14211 6c 20 74 68 65 20 70 61 67 65 20 27 6e 65 61 72  l the page 'near
14212 62 79 27 20 69 73 20 6c 6f 63 61 74 65 64 2e 0a  by' is located..
14213 20 20 20 20 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a      */.    do {.
14214 20 20 20 20 20 20 70 50 72 65 76 54 72 75 6e 6b        pPrevTrunk
14215 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20   = pTrunk;.     
14216 20 69 66 28 20 70 50 72 65 76 54 72 75 6e 6b 20   if( pPrevTrunk 
14217 29 7b 0a 20 20 20 20 20 20 20 20 69 54 72 75 6e  ){.        iTrun
14218 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50  k = get4byte(&pP
14219 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b  revTrunk->aData[
1421a 30 5d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  0]);.      }else
1421b 7b 0a 20 20 20 20 20 20 20 20 69 54 72 75 6e 6b  {.        iTrunk
1421c 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61   = get4byte(&pPa
1421d 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 3b  ge1->aData[32]);
1421e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
1421f 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
14220 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 54 72  GetPage(pBt, iTr
14221 75 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29  unk, &pTrunk, 0)
14222 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ;.      if( rc )
14223 7b 0a 20 20 20 20 20 20 20 20 70 54 72 75 6e 6b  {.        pTrunk
14224 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 67 6f   = 0;.        go
14225 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f  to end_allocate_
14226 70 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  page;.      }.. 
14227 20 20 20 20 20 6b 20 3d 20 67 65 74 34 62 79 74       k = get4byt
14228 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61  e(&pTrunk->aData
14229 5b 34 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  [4]);.      if( 
1422a 6b 3d 3d 30 20 26 26 20 21 73 65 61 72 63 68 4c  k==0 && !searchL
1422b 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ist ){.        /
1422c 2a 20 54 68 65 20 74 72 75 6e 6b 20 68 61 73 20  * The trunk has 
1422d 6e 6f 20 6c 65 61 76 65 73 20 61 6e 64 20 74 68  no leaves and th
1422e 65 20 6c 69 73 74 20 69 73 20 6e 6f 74 20 62 65  e list is not be
1422f 69 6e 67 20 73 65 61 72 63 68 65 64 2e 20 0a 20  ing searched. . 
14230 20 20 20 20 20 20 20 2a 2a 20 53 6f 20 65 78 74         ** So ext
14231 72 61 63 74 20 74 68 65 20 74 72 75 6e 6b 20 70  ract the trunk p
14232 61 67 65 20 69 74 73 65 6c 66 20 61 6e 64 20 75  age itself and u
14233 73 65 20 69 74 20 61 73 20 74 68 65 20 6e 65 77  se it as the new
14234 6c 79 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  ly .        ** a
14235 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20 2a 2f  llocated page */
14236 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
14237 20 70 50 72 65 76 54 72 75 6e 6b 3d 3d 30 20 29   pPrevTrunk==0 )
14238 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
14239 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
1423a 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65  (pTrunk->pDbPage
1423b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
1423c 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67  c ){.          g
1423d 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65  oto end_allocate
1423e 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d  _page;.        }
1423f 0a 20 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20  .        *pPgno 
14240 3d 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20  = iTrunk;.      
14241 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 31    memcpy(&pPage1
14242 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 26 70 54  ->aData[32], &pT
14243 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20  runk->aData[0], 
14244 34 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 50  4);.        *ppP
14245 61 67 65 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20  age = pTrunk;.  
14246 20 20 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30        pTrunk = 0
14247 3b 0a 20 20 20 20 20 20 20 20 54 52 41 43 45 28  ;.        TRACE(
14248 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74  ("ALLOCATE: %d t
14249 72 75 6e 6b 20 2d 20 25 64 20 66 72 65 65 20 70  runk - %d free p
1424a 61 67 65 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70  ages left\n", *p
1424b 50 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20  Pgno, n-1));.   
1424c 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6b 3e 70     }else if( k>p
1424d 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34  Bt->usableSize/4
1424e 20 2d 20 32 20 29 7b 0a 20 20 20 20 20 20 20 20   - 2 ){.        
1424f 2f 2a 20 56 61 6c 75 65 20 6f 66 20 6b 20 69 73  /* Value of k is
14250 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 20   out of range.  
14251 44 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74  Database corrupt
14252 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 72  ion */.        r
14253 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  c = SQLITE_CORRU
14254 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20  PT_BKPT;.       
14255 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61   goto end_alloca
14256 74 65 5f 70 61 67 65 3b 0a 23 69 66 6e 64 65 66  te_page;.#ifndef
14257 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
14258 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 7d 65  OVACUUM.      }e
14259 6c 73 65 20 69 66 28 20 73 65 61 72 63 68 4c 69  lse if( searchLi
1425a 73 74 20 26 26 20 6e 65 61 72 62 79 3d 3d 69 54  st && nearby==iT
1425b 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 20  runk ){.        
1425c 2f 2a 20 54 68 65 20 6c 69 73 74 20 69 73 20 62  /* The list is b
1425d 65 69 6e 67 20 73 65 61 72 63 68 65 64 20 61 6e  eing searched an
1425e 64 20 74 68 69 73 20 74 72 75 6e 6b 20 70 61 67  d this trunk pag
1425f 65 20 69 73 20 74 68 65 20 70 61 67 65 0a 20 20  e is the page.  
14260 20 20 20 20 20 20 2a 2a 20 74 6f 20 61 6c 6c 6f        ** to allo
14261 63 61 74 65 2c 20 72 65 67 61 72 64 6c 65 73 73  cate, regardless
14262 20 6f 66 20 77 68 65 74 68 65 72 20 69 74 20 68   of whether it h
14263 61 73 20 6c 65 61 76 65 73 2e 0a 20 20 20 20 20  as leaves..     
14264 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73     */.        as
14265 73 65 72 74 28 20 2a 70 50 67 6e 6f 3d 3d 69 54  sert( *pPgno==iT
14266 72 75 6e 6b 20 29 3b 0a 20 20 20 20 20 20 20 20  runk );.        
14267 2a 70 70 50 61 67 65 20 3d 20 70 54 72 75 6e 6b  *ppPage = pTrunk
14268 3b 0a 20 20 20 20 20 20 20 20 73 65 61 72 63 68  ;.        search
14269 4c 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  List = 0;.      
1426a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
1426b 67 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d  gerWrite(pTrunk-
1426c 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  >pDbPage);.     
1426d 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
1426e 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
1426f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20  allocate_page;. 
14270 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
14271 20 69 66 28 20 6b 3d 3d 30 20 29 7b 0a 20 20 20   if( k==0 ){.   
14272 20 20 20 20 20 20 20 69 66 28 20 21 70 50 72 65         if( !pPre
14273 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20  vTrunk ){.      
14274 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50        memcpy(&pP
14275 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c  age1->aData[32],
14276 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b   &pTrunk->aData[
14277 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20  0], 4);.        
14278 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
14279 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 72       memcpy(&pPr
1427a 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30  evTrunk->aData[0
1427b 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74  ], &pTrunk->aDat
1427c 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20  a[0], 4);.      
1427d 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65      }.        }e
1427e 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  lse{.          /
1427f 2a 20 54 68 65 20 74 72 75 6e 6b 20 70 61 67 65  * The trunk page
14280 20 69 73 20 72 65 71 75 69 72 65 64 20 62 79 20   is required by 
14281 74 68 65 20 63 61 6c 6c 65 72 20 62 75 74 20 69  the caller but i
14282 74 20 63 6f 6e 74 61 69 6e 73 20 0a 20 20 20 20  t contains .    
14283 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72        ** pointer
14284 73 20 74 6f 20 66 72 65 65 2d 6c 69 73 74 20 6c  s to free-list l
14285 65 61 76 65 73 2e 20 54 68 65 20 66 69 72 73 74  eaves. The first
14286 20 6c 65 61 66 20 62 65 63 6f 6d 65 73 20 61 20   leaf becomes a 
14287 74 72 75 6e 6b 0a 20 20 20 20 20 20 20 20 20 20  trunk.          
14288 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 69 73 20  ** page in this 
14289 63 61 73 65 2e 0a 20 20 20 20 20 20 20 20 20 20  case..          
1428a 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 4d 65 6d  */.          Mem
1428b 50 61 67 65 20 2a 70 4e 65 77 54 72 75 6e 6b 3b  Page *pNewTrunk;
1428c 0a 20 20 20 20 20 20 20 20 20 20 50 67 6e 6f 20  .          Pgno 
1428d 69 4e 65 77 54 72 75 6e 6b 20 3d 20 67 65 74 34  iNewTrunk = get4
1428e 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44  byte(&pTrunk->aD
1428f 61 74 61 5b 38 5d 29 3b 0a 20 20 20 20 20 20 20  ata[8]);.       
14290 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
14291 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c  treeGetPage(pBt,
14292 20 69 4e 65 77 54 72 75 6e 6b 2c 20 26 70 4e 65   iNewTrunk, &pNe
14293 77 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20  wTrunk, 0);.    
14294 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
14295 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
14296 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
14297 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20  allocate_page;. 
14298 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
14299 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1429a 33 50 61 67 65 72 57 72 69 74 65 28 70 4e 65 77  3PagerWrite(pNew
1429b 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b  Trunk->pDbPage);
1429c 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72  .          if( r
1429d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1429e 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6c 65              rele
1429f 61 73 65 50 61 67 65 28 70 4e 65 77 54 72 75 6e  asePage(pNewTrun
142a0 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  k);.            
142a1 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74  goto end_allocat
142a2 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20  e_page;.        
142a3 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 6d 65    }.          me
142a4 6d 63 70 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d  mcpy(&pNewTrunk-
142a5 3e 61 44 61 74 61 5b 30 5d 2c 20 26 70 54 72 75  >aData[0], &pTru
142a6 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29  nk->aData[0], 4)
142a7 3b 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 34  ;.          put4
142a8 62 79 74 65 28 26 70 4e 65 77 54 72 75 6e 6b 2d  byte(&pNewTrunk-
142a9 3e 61 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b  >aData[4], k-1);
142aa 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70  .          memcp
142ab 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44  y(&pNewTrunk->aD
142ac 61 74 61 5b 38 5d 2c 20 26 70 54 72 75 6e 6b 2d  ata[8], &pTrunk-
142ad 3e 61 44 61 74 61 5b 31 32 5d 2c 20 28 6b 2d 31  >aData[12], (k-1
142ae 29 2a 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20  )*4);.          
142af 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77  releasePage(pNew
142b0 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20  Trunk);.        
142b1 20 20 69 66 28 20 21 70 50 72 65 76 54 72 75 6e    if( !pPrevTrun
142b2 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  k ){.           
142b3 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65   put4byte(&pPage
142b4 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 69 4e  1->aData[32], iN
142b5 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20  ewTrunk);.      
142b6 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
142b7 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
142b8 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
142b9 72 65 76 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67  revTrunk->pDbPag
142ba 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  e);.            
142bb 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
142bc 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64          goto end
142bd 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a  _allocate_page;.
142be 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
142bf 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79            put4by
142c0 74 65 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e  te(&pPrevTrunk->
142c1 61 44 61 74 61 5b 30 5d 2c 20 69 4e 65 77 54 72  aData[0], iNewTr
142c2 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20  unk);.          
142c3 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
142c4 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a      pTrunk = 0;.
142c5 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22          TRACE(("
142c6 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75  ALLOCATE: %d tru
142c7 6e 6b 20 2d 20 25 64 20 66 72 65 65 20 70 61 67  nk - %d free pag
142c8 65 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67  es left\n", *pPg
142c9 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 23 65 6e 64 69  no, n-1));.#endi
142ca 66 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  f.      }else{. 
142cb 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 63         /* Extrac
142cc 74 20 61 20 6c 65 61 66 20 66 72 6f 6d 20 74 68  t a leaf from th
142cd 65 20 74 72 75 6e 6b 20 2a 2f 0a 20 20 20 20 20  e trunk */.     
142ce 20 20 20 69 6e 74 20 63 6c 6f 73 65 73 74 3b 0a     int closest;.
142cf 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 50 61          Pgno iPa
142d0 67 65 3b 0a 20 20 20 20 20 20 20 20 75 6e 73 69  ge;.        unsi
142d1 67 6e 65 64 20 63 68 61 72 20 2a 61 44 61 74 61  gned char *aData
142d2 20 3d 20 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61   = pTrunk->aData
142d3 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
142d4 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
142d5 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65  (pTrunk->pDbPage
142d6 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
142d7 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67  c ){.          g
142d8 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65  oto end_allocate
142d9 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d  _page;.        }
142da 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 65 61  .        if( nea
142db 72 62 79 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  rby>0 ){.       
142dc 20 20 20 69 6e 74 20 69 2c 20 64 69 73 74 3b 0a     int i, dist;.
142dd 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 73            closes
142de 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  t = 0;.         
142df 20 64 69 73 74 20 3d 20 67 65 74 34 62 79 74 65   dist = get4byte
142e0 28 26 61 44 61 74 61 5b 38 5d 29 20 2d 20 6e 65  (&aData[8]) - ne
142e1 61 72 62 79 3b 0a 20 20 20 20 20 20 20 20 20 20  arby;.          
142e2 69 66 28 20 64 69 73 74 3c 30 20 29 20 64 69 73  if( dist<0 ) dis
142e3 74 20 3d 20 2d 64 69 73 74 3b 0a 20 20 20 20 20  t = -dist;.     
142e4 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c       for(i=1; i<
142e5 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  k; i++){.       
142e6 20 20 20 20 20 69 6e 74 20 64 32 20 3d 20 67 65       int d2 = ge
142e7 74 34 62 79 74 65 28 26 61 44 61 74 61 5b 38 2b  t4byte(&aData[8+
142e8 69 2a 34 5d 29 20 2d 20 6e 65 61 72 62 79 3b 0a  i*4]) - nearby;.
142e9 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
142ea 64 32 3c 30 20 29 20 64 32 20 3d 20 2d 64 32 3b  d2<0 ) d2 = -d2;
142eb 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
142ec 20 64 32 3c 64 69 73 74 20 29 7b 0a 20 20 20 20   d2<dist ){.    
142ed 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 73            closes
142ee 74 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20 20  t = i;.         
142ef 20 20 20 20 20 64 69 73 74 20 3d 20 64 32 3b 0a       dist = d2;.
142f0 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
142f1 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
142f2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
142f3 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b 0a     closest = 0;.
142f4 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20          }..     
142f5 20 20 20 69 50 61 67 65 20 3d 20 67 65 74 34 62     iPage = get4b
142f6 79 74 65 28 26 61 44 61 74 61 5b 38 2b 63 6c 6f  yte(&aData[8+clo
142f7 73 65 73 74 2a 34 5d 29 3b 0a 20 20 20 20 20 20  sest*4]);.      
142f8 20 20 69 66 28 20 21 73 65 61 72 63 68 4c 69 73    if( !searchLis
142f9 74 20 7c 7c 20 69 50 61 67 65 3d 3d 6e 65 61 72  t || iPage==near
142fa 62 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  by ){.          
142fb 69 6e 74 20 6e 50 61 67 65 3b 0a 20 20 20 20 20  int nPage;.     
142fc 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 50       *pPgno = iP
142fd 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 6e  age;.          n
142fe 50 61 67 65 20 3d 20 70 61 67 65 72 50 61 67 65  Page = pagerPage
142ff 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65  count(pBt->pPage
14300 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  r);.          if
14301 28 20 2a 70 50 67 6e 6f 3e 6e 50 61 67 65 20 29  ( *pPgno>nPage )
14302 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  {.            /*
14303 20 46 72 65 65 20 70 61 67 65 20 6f 66 66 20 74   Free page off t
14304 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69  he end of the fi
14305 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  le */.          
14306 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f    rc = SQLITE_CO
14307 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
14308 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64          goto end
14309 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a  _allocate_page;.
1430a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1430b 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c        TRACE(("AL
1430c 4c 4f 43 41 54 45 3a 20 25 64 20 77 61 73 20 6c  LOCATE: %d was l
1430d 65 61 66 20 25 64 20 6f 66 20 25 64 20 6f 6e 20  eaf %d of %d on 
1430e 74 72 75 6e 6b 20 25 64 22 0a 20 20 20 20 20 20  trunk %d".      
1430f 20 20 20 20 20 20 20 20 20 20 20 22 3a 20 25 64             ": %d
14310 20 6d 6f 72 65 20 66 72 65 65 20 70 61 67 65 73   more free pages
14311 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
14312 20 20 20 20 20 20 2a 70 50 67 6e 6f 2c 20 63 6c        *pPgno, cl
14313 6f 73 65 73 74 2b 31 2c 20 6b 2c 20 70 54 72 75  osest+1, k, pTru
14314 6e 6b 2d 3e 70 67 6e 6f 2c 20 6e 2d 31 29 29 3b  nk->pgno, n-1));
14315 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63  .          if( c
14316 6c 6f 73 65 73 74 3c 6b 2d 31 20 29 7b 0a 20 20  losest<k-1 ){.  
14317 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79            memcpy
14318 28 26 61 44 61 74 61 5b 38 2b 63 6c 6f 73 65 73  (&aData[8+closes
14319 74 2a 34 5d 2c 20 26 61 44 61 74 61 5b 34 2b 6b  t*4], &aData[4+k
1431a 2a 34 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20  *4], 4);.       
1431b 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70     }.          p
1431c 75 74 34 62 79 74 65 28 26 61 44 61 74 61 5b 34  ut4byte(&aData[4
1431d 5d 2c 20 6b 2d 31 29 3b 0a 20 20 20 20 20 20 20  ], k-1);.       
1431e 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1431f 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c  treeGetPage(pBt,
14320 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61 67 65 2c   *pPgno, ppPage,
14321 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69   1);.          i
14322 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
14323 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
14324 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74  sqlite3PagerDont
14325 52 6f 6c 6c 62 61 63 6b 28 28 2a 70 70 50 61 67  Rollback((*ppPag
14326 65 29 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  e)->pDbPage);.  
14327 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
14328 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
14329 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50  ((*ppPage)->pDbP
1432a 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  age);.          
1432b 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1432c 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
1432d 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
1432e 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20  (*ppPage);.     
1432f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
14330 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73     }.          s
14331 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a 20  earchList = 0;. 
14332 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
14333 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61  .      releasePa
14334 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29 3b 0a  ge(pPrevTrunk);.
14335 20 20 20 20 20 20 70 50 72 65 76 54 72 75 6e 6b        pPrevTrunk
14336 20 3d 20 30 3b 0a 20 20 20 20 7d 77 68 69 6c 65   = 0;.    }while
14337 28 20 73 65 61 72 63 68 4c 69 73 74 20 29 3b 0a  ( searchList );.
14338 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
14339 54 68 65 72 65 20 61 72 65 20 6e 6f 20 70 61 67  There are no pag
1433a 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69  es on the freeli
1433b 73 74 2c 20 73 6f 20 63 72 65 61 74 65 20 61 20  st, so create a 
1433c 6e 65 77 20 70 61 67 65 20 61 74 20 74 68 65 0a  new page at the.
1433d 20 20 20 20 2a 2a 20 65 6e 64 20 6f 66 20 74 68      ** end of th
1433e 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 69 6e  e file */.    in
1433f 74 20 6e 50 61 67 65 20 3d 20 70 61 67 65 72 50  t nPage = pagerP
14340 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50  agecount(pBt->pP
14341 61 67 65 72 29 3b 0a 20 20 20 20 2a 70 50 67 6e  ager);.    *pPgn
14342 6f 20 3d 20 6e 50 61 67 65 20 2b 20 31 3b 0a 0a  o = nPage + 1;..
14343 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
14344 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
14345 20 20 20 69 66 28 20 70 42 74 2d 3e 6e 54 72 75     if( pBt->nTru
14346 6e 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41  nc ){.      /* A
14347 6e 20 69 6e 63 72 2d 76 61 63 75 75 6d 20 68 61  n incr-vacuum ha
14348 73 20 61 6c 72 65 61 64 79 20 72 75 6e 20 77 69  s already run wi
14349 74 68 69 6e 20 74 68 69 73 20 74 72 61 6e 73 61  thin this transa
1434a 63 74 69 6f 6e 2e 20 53 6f 20 74 68 65 0a 20 20  ction. So the.  
1434b 20 20 20 20 2a 2a 20 70 61 67 65 20 74 6f 20 61      ** page to a
1434c 6c 6c 6f 63 61 74 65 20 69 73 20 6e 6f 74 20 66  llocate is not f
1434d 72 6f 6d 20 74 68 65 20 70 68 79 73 69 63 61 6c  rom the physical
1434e 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65   end of the file
1434f 2c 20 62 75 74 0a 20 20 20 20 20 20 2a 2a 20 61  , but.      ** a
14350 74 20 70 42 74 2d 3e 6e 54 72 75 6e 63 2e 20 0a  t pBt->nTrunc. .
14351 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 2a        */.      *
14352 70 50 67 6e 6f 20 3d 20 70 42 74 2d 3e 6e 54 72  pPgno = pBt->nTr
14353 75 6e 63 2b 31 3b 0a 20 20 20 20 20 20 69 66 28  unc+1;.      if(
14354 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47   *pPgno==PENDING
14355 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20  _BYTE_PAGE(pBt) 
14356 29 7b 0a 20 20 20 20 20 20 20 20 28 2a 70 50 67  ){.        (*pPg
14357 6e 6f 29 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20  no)++;.      }. 
14358 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 42 74     }.    if( pBt
14359 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20  ->autoVacuum && 
1435a 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42  PTRMAP_ISPAGE(pB
1435b 74 2c 20 2a 70 50 67 6e 6f 29 20 29 7b 0a 20 20  t, *pPgno) ){.  
1435c 20 20 20 20 2f 2a 20 49 66 20 2a 70 50 67 6e 6f      /* If *pPgno
1435d 20 72 65 66 65 72 73 20 74 6f 20 61 20 70 6f 69   refers to a poi
1435e 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 61  nter-map page, a
1435f 6c 6c 6f 63 61 74 65 20 74 77 6f 20 6e 65 77 20  llocate two new 
14360 70 61 67 65 73 0a 20 20 20 20 20 20 2a 2a 20 61  pages.      ** a
14361 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
14362 20 66 69 6c 65 20 69 6e 73 74 65 61 64 20 6f 66   file instead of
14363 20 6f 6e 65 2e 20 54 68 65 20 66 69 72 73 74 20   one. The first 
14364 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 0a 20  allocated page. 
14365 20 20 20 20 20 2a 2a 20 62 65 63 6f 6d 65 73 20       ** becomes 
14366 61 20 6e 65 77 20 70 6f 69 6e 74 65 72 2d 6d 61  a new pointer-ma
14367 70 20 70 61 67 65 2c 20 74 68 65 20 73 65 63 6f  p page, the seco
14368 6e 64 20 69 73 20 75 73 65 64 20 62 79 20 74 68  nd is used by th
14369 65 20 63 61 6c 6c 65 72 2e 0a 20 20 20 20 20 20  e caller..      
1436a 2a 2f 0a 20 20 20 20 20 20 54 52 41 43 45 28 28  */.      TRACE((
1436b 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66 72  "ALLOCATE: %d fr
1436c 6f 6d 20 65 6e 64 20 6f 66 20 66 69 6c 65 20 28  om end of file (
1436d 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65  pointer-map page
1436e 29 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b 0a  )\n", *pPgno));.
1436f 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2a 70        assert( *p
14370 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59  Pgno!=PENDING_BY
14371 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a  TE_PAGE(pBt) );.
14372 20 20 20 20 20 20 28 2a 70 50 67 6e 6f 29 2b 2b        (*pPgno)++
14373 3b 0a 20 20 20 20 20 20 69 66 28 20 2a 70 50 67  ;.      if( *pPg
14374 6e 6f 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45  no==PENDING_BYTE
14375 5f 50 41 47 45 28 70 42 74 29 20 29 7b 20 28 2a  _PAGE(pBt) ){ (*
14376 70 50 67 6e 6f 29 2b 2b 3b 20 7d 0a 20 20 20 20  pPgno)++; }.    
14377 7d 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 6e  }.    if( pBt->n
14378 54 72 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 70  Trunc ){.      p
14379 42 74 2d 3e 6e 54 72 75 6e 63 20 3d 20 2a 70 50  Bt->nTrunc = *pP
1437a 67 6e 6f 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  gno;.    }.#endi
1437b 66 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 2a  f..    assert( *
1437c 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42  pPgno!=PENDING_B
1437d 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b  YTE_PAGE(pBt) );
1437e 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1437f 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42  3BtreeGetPage(pB
14380 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61 67  t, *pPgno, ppPag
14381 65 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72  e, 0);.    if( r
14382 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
14383 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
14384 61 67 65 72 57 72 69 74 65 28 28 2a 70 70 50 61  agerWrite((*ppPa
14385 67 65 29 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  ge)->pDbPage);. 
14386 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
14387 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65  E_OK ){.      re
14388 6c 65 61 73 65 50 61 67 65 28 2a 70 70 50 61 67  leasePage(*ppPag
14389 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 54 52  e);.    }.    TR
1438a 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20  ACE(("ALLOCATE: 
1438b 25 64 20 66 72 6f 6d 20 65 6e 64 20 6f 66 20 66  %d from end of f
1438c 69 6c 65 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29  ile\n", *pPgno))
1438d 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  ;.  }..  assert(
1438e 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47   *pPgno!=PENDING
1438f 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20  _BYTE_PAGE(pBt) 
14390 29 3b 0a 0a 65 6e 64 5f 61 6c 6c 6f 63 61 74 65  );..end_allocate
14391 5f 70 61 67 65 3a 0a 20 20 72 65 6c 65 61 73 65  _page:.  release
14392 50 61 67 65 28 70 54 72 75 6e 6b 29 3b 0a 20 20  Page(pTrunk);.  
14393 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 72 65  releasePage(pPre
14394 76 54 72 75 6e 6b 29 3b 0a 20 20 69 66 28 20 72  vTrunk);.  if( r
14395 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
14396 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
14397 52 65 66 63 6f 75 6e 74 28 28 2a 70 70 50 61 67  Refcount((*ppPag
14398 65 29 2d 3e 70 44 62 50 61 67 65 29 3e 31 20 29  e)->pDbPage)>1 )
14399 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67  {.    releasePag
1439a 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20  e(*ppPage);.    
1439b 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
1439c 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a  RRUPT_BKPT;.  }.
1439d 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1439e 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 70 61 67 65  /*.** Add a page
1439f 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
143a0 20 66 69 6c 65 20 74 6f 20 74 68 65 20 66 72 65   file to the fre
143a1 65 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c  elist..**.** sql
143a2 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 29  ite3PagerUnref()
143a3 20 69 73 20 4e 4f 54 20 63 61 6c 6c 65 64 20 66   is NOT called f
143a4 6f 72 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 74 61  or pPage..*/.sta
143a5 74 69 63 20 69 6e 74 20 66 72 65 65 50 61 67 65  tic int freePage
143a6 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29  (MemPage *pPage)
143a7 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  {.  BtShared *pB
143a8 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a  t = pPage->pBt;.
143a9 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
143aa 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b  1 = pBt->pPage1;
143ab 0a 20 20 69 6e 74 20 72 63 2c 20 6e 2c 20 6b 3b  .  int rc, n, k;
143ac 0a 0a 20 20 2f 2a 20 50 72 65 70 61 72 65 20 74  ..  /* Prepare t
143ad 68 65 20 70 61 67 65 20 66 6f 72 20 66 72 65 65  he page for free
143ae 69 6e 67 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ing */.  assert(
143af 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
143b0 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
143b1 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
143b2 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3e  rt( pPage->pgno>
143b3 31 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 69 73  1 );.  pPage->is
143b4 49 6e 69 74 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20  Init = 0;..  /* 
143b5 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 66 72  Increment the fr
143b6 65 65 20 70 61 67 65 20 63 6f 75 6e 74 20 6f 6e  ee page count on
143b7 20 70 50 61 67 65 31 20 2a 2f 0a 20 20 72 63 20   pPage1 */.  rc 
143b8 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
143b9 69 74 65 28 70 50 61 67 65 31 2d 3e 70 44 62 50  ite(pPage1->pDbP
143ba 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29  age);.  if( rc )
143bb 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 6e 20   return rc;.  n 
143bc 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67  = get4byte(&pPag
143bd 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a  e1->aData[36]);.
143be 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67    put4byte(&pPag
143bf 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 6e  e1->aData[36], n
143c0 2b 31 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c  +1);..#ifdef SQL
143c1 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54  ITE_SECURE_DELET
143c2 45 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 53 51  E.  /* If the SQ
143c3 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45  LITE_SECURE_DELE
143c4 54 45 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20  TE compile-time 
143c5 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65  option is enable
143c6 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 61 6c 77  d, then.  ** alw
143c7 61 79 73 20 66 75 6c 6c 79 20 6f 76 65 72 77 72  ays fully overwr
143c8 69 74 65 20 64 65 6c 65 74 65 64 20 69 6e 66 6f  ite deleted info
143c9 72 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a 65 72  rmation with zer
143ca 6f 73 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20  os..  */.  rc = 
143cb 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
143cc 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
143cd 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  );.  if( rc ) re
143ce 74 75 72 6e 20 72 63 3b 0a 20 20 6d 65 6d 73 65  turn rc;.  memse
143cf 74 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20  t(pPage->aData, 
143d0 30 2c 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 70  0, pPage->pBt->p
143d1 61 67 65 53 69 7a 65 29 3b 0a 23 65 6e 64 69 66  ageSize);.#endif
143d2 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61  ..  /* If the da
143d3 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 20  tabase supports 
143d4 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 77 72 69  auto-vacuum, wri
143d5 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74  te an entry in t
143d6 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20  he pointer-map. 
143d7 20 2a 2a 20 74 6f 20 69 6e 64 69 63 61 74 65 20   ** to indicate 
143d8 74 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73  that the page is
143d9 20 66 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 69 66   free..  */.  if
143da 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29  ( ISAUTOVACUUM )
143db 7b 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61  {.    rc = ptrma
143dc 70 50 75 74 28 70 42 74 2c 20 70 50 61 67 65 2d  pPut(pBt, pPage-
143dd 3e 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 46 52  >pgno, PTRMAP_FR
143de 45 45 50 41 47 45 2c 20 30 29 3b 0a 20 20 20 20  EEPAGE, 0);.    
143df 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
143e0 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e  rc;.  }..  if( n
143e1 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  ==0 ){.    /* Th
143e2 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20  is is the first 
143e3 66 72 65 65 20 70 61 67 65 20 2a 2f 0a 20 20 20  free page */.   
143e4 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
143e5 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70  erWrite(pPage->p
143e6 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28  DbPage);.    if(
143e7 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
143e8 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 67  .    memset(pPag
143e9 65 2d 3e 61 44 61 74 61 2c 20 30 2c 20 38 29 3b  e->aData, 0, 8);
143ea 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70  .    put4byte(&p
143eb 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d  Page1->aData[32]
143ec 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a  , pPage->pgno);.
143ed 20 20 20 20 54 52 41 43 45 28 28 22 46 52 45 45      TRACE(("FREE
143ee 2d 50 41 47 45 3a 20 25 64 20 66 69 72 73 74 5c  -PAGE: %d first\
143ef 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29  n", pPage->pgno)
143f0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
143f1 2f 2a 20 4f 74 68 65 72 20 66 72 65 65 20 70 61  /* Other free pa
143f2 67 65 73 20 61 6c 72 65 61 64 79 20 65 78 69 73  ges already exis
143f3 74 2e 20 20 52 65 74 72 69 76 65 20 74 68 65 20  t.  Retrive the 
143f4 66 69 72 73 74 20 74 72 75 6e 6b 20 70 61 67 65  first trunk page
143f5 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 66  .    ** of the f
143f6 72 65 65 6c 69 73 74 20 61 6e 64 20 66 69 6e 64  reelist and find
143f7 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 6c 65   out how many le
143f8 61 76 65 73 20 69 74 20 68 61 73 2e 20 2a 2f 0a  aves it has. */.
143f9 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72      MemPage *pTr
143fa 75 6e 6b 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  unk;.    rc = sq
143fb 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
143fc 65 28 70 42 74 2c 20 67 65 74 34 62 79 74 65 28  e(pBt, get4byte(
143fd 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  &pPage1->aData[3
143fe 32 5d 29 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29  2]), &pTrunk, 0)
143ff 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72  ;.    if( rc ) r
14400 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 6b 20  eturn rc;.    k 
14401 3d 20 67 65 74 34 62 79 74 65 28 26 70 54 72 75  = get4byte(&pTru
14402 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29 3b 0a 20  nk->aData[4]);. 
14403 20 20 20 69 66 28 20 6b 3e 3d 70 42 74 2d 3e 75     if( k>=pBt->u
14404 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20  sableSize/4 - 8 
14405 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ){.      /* The 
14406 74 72 75 6e 6b 20 69 73 20 66 75 6c 6c 2e 20 20  trunk is full.  
14407 54 75 72 6e 20 74 68 65 20 70 61 67 65 20 62 65  Turn the page be
14408 69 6e 67 20 66 72 65 65 64 20 69 6e 74 6f 20 61  ing freed into a
14409 20 6e 65 77 0a 20 20 20 20 20 20 2a 2a 20 74 72   new.      ** tr
1440a 75 6e 6b 20 70 61 67 65 20 77 69 74 68 20 6e 6f  unk page with no
1440b 20 6c 65 61 76 65 73 2e 0a 20 20 20 20 20 20 2a   leaves..      *
1440c 2a 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20  *.      ** Note 
1440d 74 68 61 74 20 74 68 65 20 74 72 75 6e 6b 20 70  that the trunk p
1440e 61 67 65 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c  age is not reall
1440f 79 20 66 75 6c 6c 20 75 6e 74 69 6c 20 69 74 20  y full until it 
14410 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20 2a  contains.      *
14411 2a 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d  * usableSize/4 -
14412 20 32 20 65 6e 74 72 69 65 73 2c 20 6e 6f 74 20   2 entries, not 
14413 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38  usableSize/4 - 8
14414 20 65 6e 74 72 69 65 73 20 61 73 20 77 65 20 68   entries as we h
14415 61 76 65 0a 20 20 20 20 20 20 2a 2a 20 63 6f 64  ave.      ** cod
14416 65 64 2e 20 20 42 75 74 20 64 75 65 20 74 6f 20  ed.  But due to 
14417 61 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 20 69  a coding error i
14418 6e 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  n versions of SQ
14419 4c 69 74 65 20 70 72 69 6f 72 20 74 6f 0a 20 20  Lite prior to.  
1441a 20 20 20 20 2a 2a 20 33 2e 36 2e 30 2c 20 64 61      ** 3.6.0, da
1441b 74 61 62 61 73 65 73 20 77 69 74 68 20 66 72 65  tabases with fre
1441c 65 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65  elist trunk page
1441d 73 20 68 6f 6c 64 69 6e 67 20 6d 6f 72 65 20 74  s holding more t
1441e 68 61 6e 0a 20 20 20 20 20 20 2a 2a 20 75 73 61  han.      ** usa
1441f 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 65 6e  bleSize/4 - 8 en
14420 74 72 69 65 73 20 77 69 6c 6c 20 62 65 20 72 65  tries will be re
14421 70 6f 72 74 65 64 20 61 73 20 63 6f 72 72 75 70  ported as corrup
14422 74 2e 20 20 49 6e 20 6f 72 64 65 72 0a 20 20 20  t.  In order.   
14423 20 20 20 2a 2a 20 74 6f 20 6d 61 69 6e 74 61 69     ** to maintai
14424 6e 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70  n backwards comp
14425 61 74 69 62 69 6c 69 74 79 20 77 69 74 68 20 6f  atibility with o
14426 6c 64 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66  lder versions of
14427 20 53 51 4c 69 74 65 2c 0a 20 20 20 20 20 20 2a   SQLite,.      *
14428 2a 20 77 65 20 77 69 6c 6c 20 63 6f 6e 74 61 69  * we will contai
14429 6e 20 74 6f 20 72 65 73 74 72 69 63 74 20 74 68  n to restrict th
1442a 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  e number of entr
1442b 69 65 73 20 74 6f 20 75 73 61 62 6c 65 53 69 7a  ies to usableSiz
1442c 65 2f 34 20 2d 20 38 0a 20 20 20 20 20 20 2a 2a  e/4 - 8.      **
1442d 20 66 6f 72 20 6e 6f 77 2e 20 20 41 74 20 73 6f   for now.  At so
1442e 6d 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20  me point in the 
1442f 66 75 74 75 72 65 20 28 6f 6e 63 65 20 65 76 65  future (once eve
14430 72 79 6f 6e 65 20 68 61 73 20 75 70 67 72 61 64  ryone has upgrad
14431 65 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 33  ed.      ** to 3
14432 2e 36 2e 30 20 6f 72 20 6c 61 74 65 72 29 20 77  .6.0 or later) w
14433 65 20 73 68 6f 75 6c 64 20 63 6f 6e 73 69 64 65  e should conside
14434 72 20 66 69 78 69 6e 67 20 74 68 65 20 63 6f 6e  r fixing the con
14435 64 69 74 69 6f 6e 61 6c 20 61 62 6f 76 65 0a 20  ditional above. 
14436 20 20 20 20 20 2a 2a 20 74 6f 20 72 65 61 64 20       ** to read 
14437 22 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 32 22  "usableSize/4-2"
14438 20 69 6e 73 74 65 61 64 20 6f 66 20 22 75 73 61   instead of "usa
14439 62 6c 65 53 69 7a 65 2f 34 2d 38 22 2e 0a 20 20  bleSize/4-8"..  
1443a 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20      */.      rc 
1443b 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
1443c 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ite(pPage->pDbPa
1443d 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ge);.      if( r
1443e 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1443f 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65          put4byte
14440 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 70  (pPage->aData, p
14441 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29 3b 0a 20 20  Trunk->pgno);.  
14442 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26        put4byte(&
14443 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 34 5d 2c  pPage->aData[4],
14444 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74   0);.        put
14445 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61  4byte(&pPage1->a
14446 44 61 74 61 5b 33 32 5d 2c 20 70 50 61 67 65 2d  Data[32], pPage-
14447 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20  >pgno);.        
14448 54 52 41 43 45 28 28 22 46 52 45 45 2d 50 41 47  TRACE(("FREE-PAG
14449 45 3a 20 25 64 20 6e 65 77 20 74 72 75 6e 6b 20  E: %d new trunk 
1444a 70 61 67 65 20 72 65 70 6c 61 63 69 6e 67 20 25  page replacing %
1444b 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  d\n",.          
1444c 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 67 6e        pPage->pgn
1444d 6f 2c 20 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29  o, pTrunk->pgno)
1444e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
1444f 65 6c 73 65 20 69 66 28 20 6b 3c 30 20 29 7b 0a  else if( k<0 ){.
14450 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
14451 45 5f 43 4f 52 52 55 50 54 3b 0a 20 20 20 20 7d  E_CORRUPT;.    }
14452 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41  else{.      /* A
14453 64 64 20 74 68 65 20 6e 65 77 6c 79 20 66 72 65  dd the newly fre
14454 65 64 20 70 61 67 65 20 61 73 20 61 20 6c 65 61  ed page as a lea
14455 66 20 6f 6e 20 74 68 65 20 63 75 72 72 65 6e 74  f on the current
14456 20 74 72 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20   trunk */.      
14457 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
14458 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70  rWrite(pTrunk->p
14459 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69  DbPage);.      i
1445a 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1445b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 75 74 34   ){.        put4
1445c 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44  byte(&pTrunk->aD
1445d 61 74 61 5b 34 5d 2c 20 6b 2b 31 29 3b 0a 20 20  ata[4], k+1);.  
1445e 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26        put4byte(&
1445f 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 2b  pTrunk->aData[8+
14460 6b 2a 34 5d 2c 20 70 50 61 67 65 2d 3e 70 67 6e  k*4], pPage->pgn
14461 6f 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  o);.#ifndef SQLI
14462 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45  TE_SECURE_DELETE
14463 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
14464 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72  lite3PagerDontWr
14465 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ite(pPage->pDbPa
14466 67 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  ge);.#endif.    
14467 20 20 7d 0a 20 20 20 20 20 20 54 52 41 43 45 28    }.      TRACE(
14468 28 22 46 52 45 45 2d 50 41 47 45 3a 20 25 64 20  ("FREE-PAGE: %d 
14469 6c 65 61 66 20 6f 6e 20 74 72 75 6e 6b 20 70 61  leaf on trunk pa
1446a 67 65 20 25 64 5c 6e 22 2c 70 50 61 67 65 2d 3e  ge %d\n",pPage->
1446b 70 67 6e 6f 2c 70 54 72 75 6e 6b 2d 3e 70 67 6e  pgno,pTrunk->pgn
1446c 6f 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  o));.    }.    r
1446d 65 6c 65 61 73 65 50 61 67 65 28 70 54 72 75 6e  eleasePage(pTrun
1446e 6b 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  k);.  }.  return
1446f 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72   rc;.}../*.** Fr
14470 65 65 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20  ee any overflow 
14471 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 64  pages associated
14472 20 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20   with the given 
14473 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  Cell..*/.static 
14474 69 6e 74 20 63 6c 65 61 72 43 65 6c 6c 28 4d 65  int clearCell(Me
14475 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 6e  mPage *pPage, un
14476 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65  signed char *pCe
14477 6c 6c 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  ll){.  BtShared 
14478 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42  *pBt = pPage->pB
14479 74 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e  t;.  CellInfo in
1447a 66 6f 3b 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 50  fo;.  Pgno ovflP
1447b 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  gno;.  int rc;. 
1447c 20 69 6e 74 20 6e 4f 76 66 6c 3b 0a 20 20 69 6e   int nOvfl;.  in
1447d 74 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 3b 0a  t ovflPageSize;.
1447e 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1447f 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50  e3_mutex_held(pP
14480 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  age->pBt->mutex)
14481 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72   );.  sqlite3Btr
14482 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70  eeParseCellPtr(p
14483 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e  Page, pCell, &in
14484 66 6f 29 3b 0a 20 20 69 66 28 20 69 6e 66 6f 2e  fo);.  if( info.
14485 69 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 7b 0a  iOverflow==0 ){.
14486 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
14487 45 5f 4f 4b 3b 20 20 2f 2a 20 4e 6f 20 6f 76 65  E_OK;  /* No ove
14488 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 52 65 74  rflow pages. Ret
14489 75 72 6e 20 77 69 74 68 6f 75 74 20 64 6f 69 6e  urn without doin
1448a 67 20 61 6e 79 74 68 69 6e 67 20 2a 2f 0a 20 20  g anything */.  
1448b 7d 0a 20 20 6f 76 66 6c 50 67 6e 6f 20 3d 20 67  }.  ovflPgno = g
1448c 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69  et4byte(&pCell[i
1448d 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b  nfo.iOverflow]);
1448e 0a 20 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 20  .  ovflPageSize 
1448f 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  = pBt->usableSiz
14490 65 20 2d 20 34 3b 0a 20 20 6e 4f 76 66 6c 20 3d  e - 4;.  nOvfl =
14491 20 28 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20   (info.nPayload 
14492 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2b 20  - info.nLocal + 
14493 6f 76 66 6c 50 61 67 65 53 69 7a 65 20 2d 20 31  ovflPageSize - 1
14494 29 2f 6f 76 66 6c 50 61 67 65 53 69 7a 65 3b 0a  )/ovflPageSize;.
14495 20 20 61 73 73 65 72 74 28 20 6f 76 66 6c 50 67    assert( ovflPg
14496 6e 6f 3d 3d 30 20 7c 7c 20 6e 4f 76 66 6c 3e 30  no==0 || nOvfl>0
14497 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e 4f 76   );.  while( nOv
14498 66 6c 2d 2d 20 29 7b 0a 20 20 20 20 4d 65 6d 50  fl-- ){.    MemP
14499 61 67 65 20 2a 70 4f 76 66 6c 3b 0a 20 20 20 20  age *pOvfl;.    
1449a 69 66 28 20 6f 76 66 6c 50 67 6e 6f 3d 3d 30 20  if( ovflPgno==0 
1449b 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e 70 61 67 65  || ovflPgno>page
1449c 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e  rPagecount(pBt->
1449d 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20  pPager) ){.     
1449e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
1449f 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
144a0 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 67 65 74   }..    rc = get
144a1 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 70 42 74  OverflowPage(pBt
144a2 2c 20 6f 76 66 6c 50 67 6e 6f 2c 20 26 70 4f 76  , ovflPgno, &pOv
144a3 66 6c 2c 20 28 6e 4f 76 66 6c 3d 3d 30 29 3f 30  fl, (nOvfl==0)?0
144a4 3a 26 6f 76 66 6c 50 67 6e 6f 29 3b 0a 20 20 20  :&ovflPgno);.   
144a5 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
144a6 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 66 72   rc;.    rc = fr
144a7 65 65 50 61 67 65 28 70 4f 76 66 6c 29 3b 0a 20  eePage(pOvfl);. 
144a8 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55     sqlite3PagerU
144a9 6e 72 65 66 28 70 4f 76 66 6c 2d 3e 70 44 62 50  nref(pOvfl->pDbP
144aa 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63  age);.    if( rc
144ab 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
144ac 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
144ad 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
144ae 72 65 61 74 65 20 74 68 65 20 62 79 74 65 20 73  reate the byte s
144af 65 71 75 65 6e 63 65 20 75 73 65 64 20 74 6f 20  equence used to 
144b0 72 65 70 72 65 73 65 6e 74 20 61 20 63 65 6c 6c  represent a cell
144b1 20 6f 6e 20 70 61 67 65 20 70 50 61 67 65 0a 2a   on page pPage.*
144b2 2a 20 61 6e 64 20 77 72 69 74 65 20 74 68 61 74  * and write that
144b3 20 62 79 74 65 20 73 65 71 75 65 6e 63 65 20 69   byte sequence i
144b4 6e 74 6f 20 70 43 65 6c 6c 5b 5d 2e 20 20 4f 76  nto pCell[].  Ov
144b5 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 72 65  erflow pages are
144b6 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e  .** allocated an
144b7 64 20 66 69 6c 6c 65 64 20 69 6e 20 61 73 20 6e  d filled in as n
144b8 65 63 65 73 73 61 72 79 2e 20 20 54 68 65 20 63  ecessary.  The c
144b9 61 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65  alling procedure
144ba 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 62  .** is responsib
144bb 6c 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73 75  le for making su
144bc 72 65 20 73 75 66 66 69 63 69 65 6e 74 20 73 70  re sufficient sp
144bd 61 63 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c  ace has been all
144be 6f 63 61 74 65 64 0a 2a 2a 20 66 6f 72 20 70 43  ocated.** for pC
144bf 65 6c 6c 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74  ell[]..**.** Not
144c0 65 20 74 68 61 74 20 70 43 65 6c 6c 20 64 6f 65  e that pCell doe
144c1 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20  s not necessary 
144c2 6e 65 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f  need to point to
144c3 20 74 68 65 20 70 50 61 67 65 2d 3e 61 44 61 74   the pPage->aDat
144c4 61 0a 2a 2a 20 61 72 65 61 2e 20 20 70 43 65 6c  a.** area.  pCel
144c5 6c 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f  l might point to
144c6 20 73 6f 6d 65 20 74 65 6d 70 6f 72 61 72 79 20   some temporary 
144c7 73 74 6f 72 61 67 65 2e 20 20 54 68 65 20 63 65  storage.  The ce
144c8 6c 6c 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 63 6f  ll will.** be co
144c9 6e 73 74 72 75 63 74 65 64 20 69 6e 20 74 68 69  nstructed in thi
144ca 73 20 74 65 6d 70 6f 72 61 72 79 20 61 72 65 61  s temporary area
144cb 20 74 68 65 6e 20 63 6f 70 69 65 64 20 69 6e 74   then copied int
144cc 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 61 0a 2a  o pPage->aData.*
144cd 2a 20 6c 61 74 65 72 2e 0a 2a 2f 0a 73 74 61 74  * later..*/.stat
144ce 69 63 20 69 6e 74 20 66 69 6c 6c 49 6e 43 65 6c  ic int fillInCel
144cf 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  l(.  MemPage *pP
144d0 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  age,            
144d1 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20      /* The page 
144d2 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
144d3 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 75 6e 73 69  e cell */.  unsi
144d4 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65 6c 6c  gned char *pCell
144d5 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  ,          /* Co
144d6 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74  mplete text of t
144d7 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 63 6f 6e  he cell */.  con
144d8 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69  st void *pKey, i
144d9 36 34 20 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 54  64 nKey,    /* T
144da 68 65 20 6b 65 79 20 2a 2f 0a 20 20 63 6f 6e 73  he key */.  cons
144db 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c 69 6e  t void *pData,in
144dc 74 20 6e 44 61 74 61 2c 20 20 20 2f 2a 20 54 68  t nData,   /* Th
144dd 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20  e data */.  int 
144de 6e 5a 65 72 6f 2c 20 20 20 20 20 20 20 20 20 20  nZero,          
144df 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78             /* Ex
144e0 74 72 61 20 7a 65 72 6f 20 62 79 74 65 73 20 74  tra zero bytes t
144e1 6f 20 61 70 70 65 6e 64 20 74 6f 20 70 44 61 74  o append to pDat
144e2 61 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 53 69  a */.  int *pnSi
144e3 7a 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ze              
144e4 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63        /* Write c
144e5 65 6c 6c 20 73 69 7a 65 20 68 65 72 65 20 2a 2f  ell size here */
144e6 0a 29 7b 0a 20 20 69 6e 74 20 6e 50 61 79 6c 6f  .){.  int nPaylo
144e7 61 64 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a  ad;.  const u8 *
144e8 70 53 72 63 3b 0a 20 20 69 6e 74 20 6e 53 72 63  pSrc;.  int nSrc
144e9 2c 20 6e 2c 20 72 63 3b 0a 20 20 69 6e 74 20 73  , n, rc;.  int s
144ea 70 61 63 65 4c 65 66 74 3b 0a 20 20 4d 65 6d 50  paceLeft;.  MemP
144eb 61 67 65 20 2a 70 4f 76 66 6c 20 3d 20 30 3b 0a  age *pOvfl = 0;.
144ec 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 6f 52 65    MemPage *pToRe
144ed 6c 65 61 73 65 20 3d 20 30 3b 0a 20 20 75 6e 73  lease = 0;.  uns
144ee 69 67 6e 65 64 20 63 68 61 72 20 2a 70 50 72 69  igned char *pPri
144ef 6f 72 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  or;.  unsigned c
144f0 68 61 72 20 2a 70 50 61 79 6c 6f 61 64 3b 0a 20  har *pPayload;. 
144f1 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
144f2 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 50   pPage->pBt;.  P
144f3 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 30  gno pgnoOvfl = 0
144f4 3b 0a 20 20 69 6e 74 20 6e 48 65 61 64 65 72 3b  ;.  int nHeader;
144f5 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f  .  CellInfo info
144f6 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
144f7 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
144f8 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65  pPage->pBt->mute
144f9 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c  x) );..  /* Fill
144fa 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e 20   in the header. 
144fb 2a 2f 0a 20 20 6e 48 65 61 64 65 72 20 3d 20 30  */.  nHeader = 0
144fc 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e  ;.  if( !pPage->
144fd 6c 65 61 66 20 29 7b 0a 20 20 20 20 6e 48 65 61  leaf ){.    nHea
144fe 64 65 72 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20  der += 4;.  }.  
144ff 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61  if( pPage->hasDa
14500 74 61 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65  ta ){.    nHeade
14501 72 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 28 26  r += putVarint(&
14502 70 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20  pCell[nHeader], 
14503 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 3b 0a 20 20  nData+nZero);.  
14504 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 44 61 74 61  }else{.    nData
14505 20 3d 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20   = nZero = 0;.  
14506 7d 0a 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 70  }.  nHeader += p
14507 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b  utVarint(&pCell[
14508 6e 48 65 61 64 65 72 5d 2c 20 2a 28 75 36 34 2a  nHeader], *(u64*
14509 29 26 6e 4b 65 79 29 3b 0a 20 20 73 71 6c 69 74  )&nKey);.  sqlit
1450a 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c  e3BtreeParseCell
1450b 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c  Ptr(pPage, pCell
1450c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 61 73 73 65  , &info);.  asse
1450d 72 74 28 20 69 6e 66 6f 2e 6e 48 65 61 64 65 72  rt( info.nHeader
1450e 3d 3d 6e 48 65 61 64 65 72 20 29 3b 0a 20 20 61  ==nHeader );.  a
1450f 73 73 65 72 74 28 20 69 6e 66 6f 2e 6e 4b 65 79  ssert( info.nKey
14510 3d 3d 6e 4b 65 79 20 29 3b 0a 20 20 61 73 73 65  ==nKey );.  asse
14511 72 74 28 20 69 6e 66 6f 2e 6e 44 61 74 61 3d 3d  rt( info.nData==
14512 6e 44 61 74 61 2b 6e 5a 65 72 6f 20 29 3b 0a 20  nData+nZero );. 
14513 20 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74   .  /* Fill in t
14514 68 65 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20  he payload */.  
14515 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 44 61 74 61  nPayload = nData
14516 20 2b 20 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 20   + nZero;.  if( 
14517 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b  pPage->intKey ){
14518 0a 20 20 20 20 70 53 72 63 20 3d 20 70 44 61 74  .    pSrc = pDat
14519 61 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 6e 44  a;.    nSrc = nD
1451a 61 74 61 3b 0a 20 20 20 20 6e 44 61 74 61 20 3d  ata;.    nData =
1451b 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
1451c 20 6e 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65   nPayload += nKe
1451d 79 3b 0a 20 20 20 20 70 53 72 63 20 3d 20 70 4b  y;.    pSrc = pK
1451e 65 79 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 6e  ey;.    nSrc = n
1451f 4b 65 79 3b 0a 20 20 7d 0a 20 20 2a 70 6e 53 69  Key;.  }.  *pnSi
14520 7a 65 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b  ze = info.nSize;
14521 0a 20 20 73 70 61 63 65 4c 65 66 74 20 3d 20 69  .  spaceLeft = i
14522 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 70 50  nfo.nLocal;.  pP
14523 61 79 6c 6f 61 64 20 3d 20 26 70 43 65 6c 6c 5b  ayload = &pCell[
14524 6e 48 65 61 64 65 72 5d 3b 0a 20 20 70 50 72 69  nHeader];.  pPri
14525 6f 72 20 3d 20 26 70 43 65 6c 6c 5b 69 6e 66 6f  or = &pCell[info
14526 2e 69 4f 76 65 72 66 6c 6f 77 5d 3b 0a 0a 20 20  .iOverflow];..  
14527 77 68 69 6c 65 28 20 6e 50 61 79 6c 6f 61 64 3e  while( nPayload>
14528 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73 70 61  0 ){.    if( spa
14529 63 65 4c 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20  ceLeft==0 ){.   
1452a 20 20 20 69 6e 74 20 69 73 45 78 61 63 74 20 3d     int isExact =
1452b 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   0;.#ifndef SQLI
1452c 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
1452d 55 4d 0a 20 20 20 20 20 20 50 67 6e 6f 20 70 67  UM.      Pgno pg
1452e 6e 6f 50 74 72 6d 61 70 20 3d 20 70 67 6e 6f 4f  noPtrmap = pgnoO
1452f 76 66 6c 3b 20 2f 2a 20 4f 76 65 72 66 6c 6f 77  vfl; /* Overflow
14530 20 70 61 67 65 20 70 6f 69 6e 74 65 72 2d 6d 61   page pointer-ma
14531 70 20 65 6e 74 72 79 20 70 61 67 65 20 2a 2f 0a  p entry page */.
14532 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61        if( pBt->a
14533 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20  utoVacuum ){.   
14534 20 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 20       do{.       
14535 20 20 20 70 67 6e 6f 4f 76 66 6c 2b 2b 3b 0a 20     pgnoOvfl++;. 
14536 20 20 20 20 20 20 20 7d 20 77 68 69 6c 65 28 20         } while( 
14537 0a 20 20 20 20 20 20 20 20 20 20 50 54 52 4d 41  .          PTRMA
14538 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 70 67  P_ISPAGE(pBt, pg
14539 6e 6f 4f 76 66 6c 29 20 7c 7c 20 70 67 6e 6f 4f  noOvfl) || pgnoO
1453a 76 66 6c 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54  vfl==PENDING_BYT
1453b 45 5f 50 41 47 45 28 70 42 74 29 20 0a 20 20 20  E_PAGE(pBt) .   
1453c 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20       );.        
1453d 69 66 28 20 70 67 6e 6f 4f 76 66 6c 3e 31 20 29  if( pgnoOvfl>1 )
1453e 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69  {.          /* i
1453f 73 45 78 61 63 74 20 3d 20 31 3b 20 2a 2f 0a 20  sExact = 1; */. 
14540 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
14541 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 72 63  .#endif.      rc
14542 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65   = allocateBtree
14543 50 61 67 65 28 70 42 74 2c 20 26 70 4f 76 66 6c  Page(pBt, &pOvfl
14544 2c 20 26 70 67 6e 6f 4f 76 66 6c 2c 20 70 67 6e  , &pgnoOvfl, pgn
14545 6f 4f 76 66 6c 2c 20 69 73 45 78 61 63 74 29 3b  oOvfl, isExact);
14546 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
14547 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
14548 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
14549 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74  database support
1454a 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 61  s auto-vacuum, a
1454b 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 6f 72  nd the second or
1454c 20 73 75 62 73 65 71 75 65 6e 74 0a 20 20 20 20   subsequent.    
1454d 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70 61    ** overflow pa
1454e 67 65 20 69 73 20 62 65 69 6e 67 20 61 6c 6c 6f  ge is being allo
1454f 63 61 74 65 64 2c 20 61 64 64 20 61 6e 20 65 6e  cated, add an en
14550 74 72 79 20 74 6f 20 74 68 65 20 70 6f 69 6e 74  try to the point
14551 65 72 2d 6d 61 70 0a 20 20 20 20 20 20 2a 2a 20  er-map.      ** 
14552 66 6f 72 20 74 68 61 74 20 70 61 67 65 20 6e 6f  for that page no
14553 77 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  w. .      **.   
14554 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73     ** If this is
14555 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 66   the first overf
14556 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e 20 77  low page, then w
14557 72 69 74 65 20 61 20 70 61 72 74 69 61 6c 20 65  rite a partial e
14558 6e 74 72 79 20 0a 20 20 20 20 20 20 2a 2a 20 74  ntry .      ** t
14559 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  o the pointer-ma
1455a 70 2e 20 49 66 20 77 65 20 77 72 69 74 65 20 6e  p. If we write n
1455b 6f 74 68 69 6e 67 20 74 6f 20 74 68 69 73 20 70  othing to this p
1455c 6f 69 6e 74 65 72 2d 6d 61 70 20 73 6c 6f 74 2c  ointer-map slot,
1455d 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74  .      ** then t
1455e 68 65 20 6f 70 74 69 6d 69 73 74 69 63 20 6f 76  he optimistic ov
1455f 65 72 66 6c 6f 77 20 63 68 61 69 6e 20 70 72 6f  erflow chain pro
14560 63 65 73 73 69 6e 67 20 69 6e 20 63 6c 65 61 72  cessing in clear
14561 43 65 6c 6c 28 29 0a 20 20 20 20 20 20 2a 2a 20  Cell().      ** 
14562 6d 61 79 20 6d 69 73 69 6e 74 65 72 70 72 65 74  may misinterpret
14563 20 74 68 65 20 75 6e 69 6e 69 74 69 61 6c 69 73   the uninitialis
14564 65 64 20 76 61 6c 75 65 73 20 61 6e 64 20 64 65  ed values and de
14565 6c 65 74 65 20 74 68 65 0a 20 20 20 20 20 20 2a  lete the.      *
14566 2a 20 77 72 6f 6e 67 20 70 61 67 65 73 20 66 72  * wrong pages fr
14567 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  om the database.
14568 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
14569 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  if( pBt->autoVac
1456a 75 75 6d 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  uum && rc==SQLIT
1456b 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1456c 75 38 20 65 54 79 70 65 20 3d 20 28 70 67 6e 6f  u8 eType = (pgno
1456d 50 74 72 6d 61 70 3f 50 54 52 4d 41 50 5f 4f 56  Ptrmap?PTRMAP_OV
1456e 45 52 46 4c 4f 57 32 3a 50 54 52 4d 41 50 5f 4f  ERFLOW2:PTRMAP_O
1456f 56 45 52 46 4c 4f 57 31 29 3b 0a 20 20 20 20 20  VERFLOW1);.     
14570 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75     rc = ptrmapPu
14571 74 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 2c  t(pBt, pgnoOvfl,
14572 20 65 54 79 70 65 2c 20 70 67 6e 6f 50 74 72 6d   eType, pgnoPtrm
14573 61 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ap);.        if(
14574 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20   rc ){.         
14575 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4f 76   releasePage(pOv
14576 66 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  fl);.        }. 
14577 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
14578 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
14579 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
1457a 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a 20  e(pToRelease);. 
1457b 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
1457c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1457d 70 75 74 34 62 79 74 65 28 70 50 72 69 6f 72 2c  put4byte(pPrior,
1457e 20 70 67 6e 6f 4f 76 66 6c 29 3b 0a 20 20 20 20   pgnoOvfl);.    
1457f 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54    releasePage(pT
14580 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 20  oRelease);.     
14581 20 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 70 4f   pToRelease = pO
14582 76 66 6c 3b 0a 20 20 20 20 20 20 70 50 72 69 6f  vfl;.      pPrio
14583 72 20 3d 20 70 4f 76 66 6c 2d 3e 61 44 61 74 61  r = pOvfl->aData
14584 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65  ;.      put4byte
14585 28 70 50 72 69 6f 72 2c 20 30 29 3b 0a 20 20 20  (pPrior, 0);.   
14586 20 20 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70     pPayload = &p
14587 4f 76 66 6c 2d 3e 61 44 61 74 61 5b 34 5d 3b 0a  Ovfl->aData[4];.
14588 20 20 20 20 20 20 73 70 61 63 65 4c 65 66 74 20        spaceLeft 
14589 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  = pBt->usableSiz
1458a 65 20 2d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20  e - 4;.    }.   
1458b 20 6e 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20   n = nPayload;. 
1458c 20 20 20 69 66 28 20 6e 3e 73 70 61 63 65 4c 65     if( n>spaceLe
1458d 66 74 20 29 20 6e 20 3d 20 73 70 61 63 65 4c 65  ft ) n = spaceLe
1458e 66 74 3b 0a 20 20 20 20 69 66 28 20 6e 53 72 63  ft;.    if( nSrc
1458f 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  >0 ){.      if( 
14590 6e 3e 6e 53 72 63 20 29 20 6e 20 3d 20 6e 53 72  n>nSrc ) n = nSr
14591 63 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  c;.      assert(
14592 20 70 53 72 63 20 29 3b 0a 20 20 20 20 20 20 6d   pSrc );.      m
14593 65 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 2c 20  emcpy(pPayload, 
14594 70 53 72 63 2c 20 6e 29 3b 0a 20 20 20 20 7d 65  pSrc, n);.    }e
14595 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65  lse{.      memse
14596 74 28 70 50 61 79 6c 6f 61 64 2c 20 30 2c 20 6e  t(pPayload, 0, n
14597 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 50 61  );.    }.    nPa
14598 79 6c 6f 61 64 20 2d 3d 20 6e 3b 0a 20 20 20 20  yload -= n;.    
14599 70 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 3b 0a 20  pPayload += n;. 
1459a 20 20 20 70 53 72 63 20 2b 3d 20 6e 3b 0a 20 20     pSrc += n;.  
1459b 20 20 6e 53 72 63 20 2d 3d 20 6e 3b 0a 20 20 20    nSrc -= n;.   
1459c 20 73 70 61 63 65 4c 65 66 74 20 2d 3d 20 6e 3b   spaceLeft -= n;
1459d 0a 20 20 20 20 69 66 28 20 6e 53 72 63 3d 3d 30  .    if( nSrc==0
1459e 20 29 7b 0a 20 20 20 20 20 20 6e 53 72 63 20 3d   ){.      nSrc =
1459f 20 6e 44 61 74 61 3b 0a 20 20 20 20 20 20 70 53   nData;.      pS
145a0 72 63 20 3d 20 70 44 61 74 61 3b 0a 20 20 20 20  rc = pData;.    
145a1 7d 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50  }.  }.  releaseP
145a2 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b  age(pToRelease);
145a3 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
145a4 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  _OK;.}../*.** Re
145a5 6d 6f 76 65 20 74 68 65 20 69 2d 74 68 20 63 65  move the i-th ce
145a6 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e 20 20  ll from pPage.  
145a7 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 66 66  This routine eff
145a8 65 63 74 73 20 70 50 61 67 65 20 6f 6e 6c 79 2e  ects pPage only.
145a9 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 63 6f 6e  .** The cell con
145aa 74 65 6e 74 20 69 73 20 6e 6f 74 20 66 72 65 65  tent is not free
145ab 64 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 64  d or deallocated
145ac 2e 20 20 49 74 20 69 73 20 61 73 73 75 6d 65 64  .  It is assumed
145ad 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 65 6c   that.** the cel
145ae 6c 20 63 6f 6e 74 65 6e 74 20 68 61 73 20 62 65  l content has be
145af 65 6e 20 63 6f 70 69 65 64 20 73 6f 6d 65 70 6c  en copied somepl
145b0 61 63 65 20 65 6c 73 65 2e 20 20 54 68 69 73 20  ace else.  This 
145b1 72 6f 75 74 69 6e 65 20 6a 75 73 74 0a 2a 2a 20  routine just.** 
145b2 72 65 6d 6f 76 65 73 20 74 68 65 20 72 65 66 65  removes the refe
145b3 72 65 6e 63 65 20 74 6f 20 74 68 65 20 63 65 6c  rence to the cel
145b4 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e 0a 2a 2a  l from pPage..**
145b5 0a 2a 2a 20 22 73 7a 22 20 6d 75 73 74 20 62 65  .** "sz" must be
145b6 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
145b7 79 74 65 73 20 69 6e 20 74 68 65 20 63 65 6c 6c  ytes in the cell
145b8 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
145b9 20 64 72 6f 70 43 65 6c 6c 28 4d 65 6d 50 61 67   dropCell(MemPag
145ba 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 64  e *pPage, int id
145bb 78 2c 20 69 6e 74 20 73 7a 29 7b 0a 20 20 69 6e  x, int sz){.  in
145bc 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  t i;          /*
145bd 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
145be 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20 20 20  .  int pc;      
145bf 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20     /* Offset to 
145c0 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 6f 66 20  cell content of 
145c1 63 65 6c 6c 20 62 65 69 6e 67 20 64 65 6c 65 74  cell being delet
145c2 65 64 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61  ed */.  u8 *data
145c3 3b 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65  ;       /* pPage
145c4 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 20 75 38 20  ->aData */.  u8 
145c5 2a 70 74 72 3b 20 20 20 20 20 20 20 20 2f 2a 20  *ptr;        /* 
145c6 55 73 65 64 20 74 6f 20 6d 6f 76 65 20 62 79 74  Used to move byt
145c7 65 73 20 61 72 6f 75 6e 64 20 77 69 74 68 69 6e  es around within
145c8 20 64 61 74 61 5b 5d 20 2a 2f 0a 0a 20 20 61 73   data[] */..  as
145c9 73 65 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20  sert( idx>=0 && 
145ca 69 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  idx<pPage->nCell
145cb 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a   );.  assert( sz
145cc 3d 3d 63 65 6c 6c 53 69 7a 65 28 70 50 61 67 65  ==cellSize(pPage
145cd 2c 20 69 64 78 29 20 29 3b 0a 20 20 61 73 73 65  , idx) );.  asse
145ce 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
145cf 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67  Iswriteable(pPag
145d0 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  e->pDbPage) );. 
145d1 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
145d2 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67  _mutex_held(pPag
145d3 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  e->pBt->mutex) )
145d4 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65  ;.  data = pPage
145d5 2d 3e 61 44 61 74 61 3b 0a 20 20 70 74 72 20 3d  ->aData;.  ptr =
145d6 20 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 63 65   &data[pPage->ce
145d7 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 69 64 78  llOffset + 2*idx
145d8 5d 3b 0a 20 20 70 63 20 3d 20 67 65 74 32 62 79  ];.  pc = get2by
145d9 74 65 28 70 74 72 29 3b 0a 20 20 61 73 73 65 72  te(ptr);.  asser
145da 74 28 20 70 63 3e 31 30 20 26 26 20 70 63 2b 73  t( pc>10 && pc+s
145db 7a 3c 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75  z<=pPage->pBt->u
145dc 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 66  sableSize );.  f
145dd 72 65 65 53 70 61 63 65 28 70 50 61 67 65 2c 20  reeSpace(pPage, 
145de 70 63 2c 20 73 7a 29 3b 0a 20 20 66 6f 72 28 69  pc, sz);.  for(i
145df 3d 69 64 78 2b 31 3b 20 69 3c 70 50 61 67 65 2d  =idx+1; i<pPage-
145e0 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 2c 20 70 74 72  >nCell; i++, ptr
145e1 2b 3d 32 29 7b 0a 20 20 20 20 70 74 72 5b 30 5d  +=2){.    ptr[0]
145e2 20 3d 20 70 74 72 5b 32 5d 3b 0a 20 20 20 20 70   = ptr[2];.    p
145e3 74 72 5b 31 5d 20 3d 20 70 74 72 5b 33 5d 3b 0a  tr[1] = ptr[3];.
145e4 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65    }.  pPage->nCe
145e5 6c 6c 2d 2d 3b 0a 20 20 70 75 74 32 62 79 74 65  ll--;.  put2byte
145e6 28 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 68 64  (&data[pPage->hd
145e7 72 4f 66 66 73 65 74 2b 33 5d 2c 20 70 50 61 67  rOffset+3], pPag
145e8 65 2d 3e 6e 43 65 6c 6c 29 3b 0a 20 20 70 50 61  e->nCell);.  pPa
145e9 67 65 2d 3e 6e 46 72 65 65 20 2b 3d 20 32 3b 0a  ge->nFree += 2;.
145ea 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20  }../*.** Insert 
145eb 61 20 6e 65 77 20 63 65 6c 6c 20 6f 6e 20 70 50  a new cell on pP
145ec 61 67 65 20 61 74 20 63 65 6c 6c 20 69 6e 64 65  age at cell inde
145ed 78 20 22 69 22 2e 20 20 70 43 65 6c 6c 20 70 6f  x "i".  pCell po
145ee 69 6e 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63  ints to the.** c
145ef 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 63 65  ontent of the ce
145f0 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ll..**.** If the
145f1 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 77 69   cell content wi
145f2 6c 6c 20 66 69 74 20 6f 6e 20 74 68 65 20 70 61  ll fit on the pa
145f3 67 65 2c 20 74 68 65 6e 20 70 75 74 20 69 74 20  ge, then put it 
145f4 74 68 65 72 65 2e 20 20 49 66 20 69 74 0a 2a 2a  there.  If it.**
145f5 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 2c 20 74   will not fit, t
145f6 68 65 6e 20 6d 61 6b 65 20 61 20 63 6f 70 79 20  hen make a copy 
145f7 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  of the cell cont
145f8 65 6e 74 20 69 6e 74 6f 20 70 54 65 6d 70 20 69  ent into pTemp i
145f9 66 0a 2a 2a 20 70 54 65 6d 70 20 69 73 20 6e 6f  f.** pTemp is no
145fa 74 20 6e 75 6c 6c 2e 20 20 52 65 67 61 72 64 6c  t null.  Regardl
145fb 65 73 73 20 6f 66 20 70 54 65 6d 70 2c 20 61 6c  ess of pTemp, al
145fc 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 65 6e 74  locate a new ent
145fd 72 79 0a 2a 2a 20 69 6e 20 70 50 61 67 65 2d 3e  ry.** in pPage->
145fe 61 4f 76 66 6c 5b 5d 20 61 6e 64 20 6d 61 6b 65  aOvfl[] and make
145ff 20 69 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   it point to the
14600 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 28 65   cell content (e
14601 69 74 68 65 72 0a 2a 2a 20 69 6e 20 70 54 65 6d  ither.** in pTem
14602 70 20 6f 72 20 74 68 65 20 6f 72 69 67 69 6e 61  p or the origina
14603 6c 20 70 43 65 6c 6c 29 20 61 6e 64 20 61 6c 73  l pCell) and als
14604 6f 20 72 65 63 6f 72 64 20 69 74 73 20 69 6e 64  o record its ind
14605 65 78 2e 20 0a 2a 2a 20 41 6c 6c 6f 63 61 74 69  ex. .** Allocati
14606 6e 67 20 61 20 6e 65 77 20 65 6e 74 72 79 20 69  ng a new entry i
14607 6e 20 70 50 61 67 65 2d 3e 61 43 65 6c 6c 5b 5d  n pPage->aCell[]
14608 20 69 6d 70 6c 69 65 73 20 74 68 61 74 20 0a 2a   implies that .*
14609 2a 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c  * pPage->nOverfl
1460a 6f 77 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65  ow is incremente
1460b 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 53 6b 69  d..**.** If nSki
1460c 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  p is non-zero, t
1460d 68 65 6e 20 64 6f 20 6e 6f 74 20 63 6f 70 79 20  hen do not copy 
1460e 74 68 65 20 66 69 72 73 74 20 6e 53 6b 69 70 20  the first nSkip 
1460f 62 79 74 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20  bytes of the.** 
14610 63 65 6c 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72  cell. The caller
14611 20 77 69 6c 6c 20 6f 76 65 72 77 72 69 74 65 20   will overwrite 
14612 74 68 65 6d 20 61 66 74 65 72 20 74 68 69 73 20  them after this 
14613 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
14614 2e 20 49 66 0a 2a 2a 20 6e 53 6b 69 70 20 69 73  . If.** nSkip is
14615 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20   non-zero, then 
14616 70 43 65 6c 6c 20 6d 61 79 20 6e 6f 74 20 70 6f  pCell may not po
14617 69 6e 74 20 74 6f 20 61 6e 20 69 6e 76 61 6c 69  int to an invali
14618 64 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  d memory locatio
14619 6e 20 0a 2a 2a 20 28 62 75 74 20 70 43 65 6c 6c  n .** (but pCell
1461a 2b 6e 53 6b 69 70 20 69 73 20 61 6c 77 61 79 73  +nSkip is always
1461b 20 76 61 6c 69 64 29 2e 0a 2a 2f 0a 73 74 61 74   valid)..*/.stat
1461c 69 63 20 69 6e 74 20 69 6e 73 65 72 74 43 65 6c  ic int insertCel
1461d 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  l(.  MemPage *pP
1461e 61 67 65 2c 20 20 20 2f 2a 20 50 61 67 65 20 69  age,   /* Page i
1461f 6e 74 6f 20 77 68 69 63 68 20 77 65 20 61 72 65  nto which we are
14620 20 63 6f 70 79 69 6e 67 20 2a 2f 0a 20 20 69 6e   copying */.  in
14621 74 20 69 2c 20 20 20 20 20 20 20 20 20 20 20 20  t i,            
14622 2f 2a 20 4e 65 77 20 63 65 6c 6c 20 62 65 63 6f  /* New cell beco
14623 6d 65 73 20 74 68 65 20 69 2d 74 68 20 63 65 6c  mes the i-th cel
14624 6c 20 6f 66 20 74 68 65 20 70 61 67 65 20 2a 2f  l of the page */
14625 0a 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20  .  u8 *pCell,   
14626 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20       /* Content 
14627 6f 66 20 74 68 65 20 6e 65 77 20 63 65 6c 6c 20  of the new cell 
14628 2a 2f 0a 20 20 69 6e 74 20 73 7a 2c 20 20 20 20  */.  int sz,    
14629 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20         /* Bytes 
1462a 6f 66 20 63 6f 6e 74 65 6e 74 20 69 6e 20 70 43  of content in pC
1462b 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 54 65  ell */.  u8 *pTe
1462c 6d 70 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 65  mp,        /* Te
1462d 6d 70 20 73 74 6f 72 61 67 65 20 73 70 61 63 65  mp storage space
1462e 20 66 6f 72 20 70 43 65 6c 6c 2c 20 69 66 20 6e   for pCell, if n
1462f 65 65 64 65 64 20 2a 2f 0a 20 20 75 38 20 6e 53  eeded */.  u8 nS
14630 6b 69 70 20 20 20 20 20 20 20 20 20 20 2f 2a 20  kip          /* 
14631 44 6f 20 6e 6f 74 20 77 72 69 74 65 20 74 68 65  Do not write the
14632 20 66 69 72 73 74 20 6e 53 6b 69 70 20 62 79 74   first nSkip byt
14633 65 73 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 2a  es of the cell *
14634 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 64 78 3b 20  /.){.  int idx; 
14635 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72           /* Wher
14636 65 20 74 6f 20 77 72 69 74 65 20 6e 65 77 20 63  e to write new c
14637 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 64  ell content in d
14638 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6a  ata[] */.  int j
14639 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
1463a 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
1463b 20 20 69 6e 74 20 74 6f 70 3b 20 20 20 20 20 20    int top;      
1463c 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74      /* First byt
1463d 65 20 6f 66 20 63 6f 6e 74 65 6e 74 20 66 6f 72  e of content for
1463e 20 61 6e 79 20 63 65 6c 6c 20 69 6e 20 64 61 74   any cell in dat
1463f 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64  a[] */.  int end
14640 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69  ;          /* Fi
14641 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74 68  rst byte past th
14642 65 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e  e last cell poin
14643 74 65 72 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f  ter in data[] */
14644 0a 20 20 69 6e 74 20 69 6e 73 3b 20 20 20 20 20  .  int ins;     
14645 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e       /* Index in
14646 20 64 61 74 61 5b 5d 20 77 68 65 72 65 20 6e 65   data[] where ne
14647 77 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 69  w cell pointer i
14648 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20  s inserted */.  
14649 69 6e 74 20 68 64 72 3b 20 20 20 20 20 20 20 20  int hdr;        
1464a 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f    /* Offset into
1464b 20 64 61 74 61 5b 5d 20 6f 66 20 74 68 65 20 70   data[] of the p
1464c 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20  age header */.  
1464d 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20  int cellOffset; 
1464e 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
1464f 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74  first cell point
14650 65 72 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a  er in data[] */.
14651 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20    u8 *data;     
14652 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 74 65      /* The conte
14653 6e 74 20 6f 66 20 74 68 65 20 77 68 6f 6c 65 20  nt of the whole 
14654 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 74  page */.  u8 *pt
14655 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55  r;          /* U
14656 73 65 64 20 66 6f 72 20 6d 6f 76 69 6e 67 20 69  sed for moving i
14657 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 72 6f 75 6e  nformation aroun
14658 64 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 0a  d in data[] */..
14659 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
1465a 26 20 69 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c  & i<=pPage->nCel
1465b 6c 2b 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c  l+pPage->nOverfl
1465c 6f 77 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ow );.  assert( 
1465d 73 7a 3d 3d 63 65 6c 6c 53 69 7a 65 50 74 72 28  sz==cellSizePtr(
1465e 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 20 29 3b  pPage, pCell) );
1465f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
14660 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50  e3_mutex_held(pP
14661 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  age->pBt->mutex)
14662 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d   );.  if( pPage-
14663 3e 6e 4f 76 65 72 66 6c 6f 77 20 7c 7c 20 73 7a  >nOverflow || sz
14664 2b 32 3e 70 50 61 67 65 2d 3e 6e 46 72 65 65 20  +2>pPage->nFree 
14665 29 7b 0a 20 20 20 20 69 66 28 20 70 54 65 6d 70  ){.    if( pTemp
14666 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   ){.      memcpy
14667 28 70 54 65 6d 70 2b 6e 53 6b 69 70 2c 20 70 43  (pTemp+nSkip, pC
14668 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53  ell+nSkip, sz-nS
14669 6b 69 70 29 3b 0a 20 20 20 20 20 20 70 43 65 6c  kip);.      pCel
1466a 6c 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20 20 7d  l = pTemp;.    }
1466b 0a 20 20 20 20 6a 20 3d 20 70 50 61 67 65 2d 3e  .    j = pPage->
1466c 6e 4f 76 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20 20  nOverflow++;.   
1466d 20 61 73 73 65 72 74 28 20 6a 3c 73 69 7a 65 6f   assert( j<sizeo
1466e 66 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 29 2f  f(pPage->aOvfl)/
1466f 73 69 7a 65 6f 66 28 70 50 61 67 65 2d 3e 61 4f  sizeof(pPage->aO
14670 76 66 6c 5b 30 5d 29 20 29 3b 0a 20 20 20 20 70  vfl[0]) );.    p
14671 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 70  Page->aOvfl[j].p
14672 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 3b 0a 20 20  Cell = pCell;.  
14673 20 20 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a    pPage->aOvfl[j
14674 5d 2e 69 64 78 20 3d 20 69 3b 0a 20 20 20 20 70  ].idx = i;.    p
14675 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 30 3b  Page->nFree = 0;
14676 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
14677 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61  t rc = sqlite3Pa
14678 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e  gerWrite(pPage->
14679 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66  pDbPage);.    if
1467a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1467b 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
1467c 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  rc;.    }.    as
1467d 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
1467e 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50  erIswriteable(pP
1467f 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  age->pDbPage) );
14680 0a 20 20 20 20 64 61 74 61 20 3d 20 70 50 61 67  .    data = pPag
14681 65 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 68 64  e->aData;.    hd
14682 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66  r = pPage->hdrOf
14683 66 73 65 74 3b 0a 20 20 20 20 74 6f 70 20 3d 20  fset;.    top = 
14684 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
14685 64 72 2b 35 5d 29 3b 0a 20 20 20 20 63 65 6c 6c  dr+5]);.    cell
14686 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e  Offset = pPage->
14687 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 20 20  cellOffset;.    
14688 65 6e 64 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74  end = cellOffset
14689 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c   + 2*pPage->nCel
1468a 6c 20 2b 20 32 3b 0a 20 20 20 20 69 6e 73 20 3d  l + 2;.    ins =
1468b 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a   cellOffset + 2*
1468c 69 3b 0a 20 20 20 20 69 66 28 20 65 6e 64 20 3e  i;.    if( end >
1468d 20 74 6f 70 20 2d 20 73 7a 20 29 7b 0a 20 20 20   top - sz ){.   
1468e 20 20 20 64 65 66 72 61 67 6d 65 6e 74 50 61 67     defragmentPag
1468f 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20  e(pPage);.      
14690 74 6f 70 20 3d 20 67 65 74 32 62 79 74 65 28 26  top = get2byte(&
14691 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20  data[hdr+5]);.  
14692 20 20 20 20 61 73 73 65 72 74 28 20 65 6e 64 20      assert( end 
14693 2b 20 73 7a 20 3c 3d 20 74 6f 70 20 29 3b 0a 20  + sz <= top );. 
14694 20 20 20 7d 0a 20 20 20 20 69 64 78 20 3d 20 61     }.    idx = a
14695 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 70 50 61  llocateSpace(pPa
14696 67 65 2c 20 73 7a 29 3b 0a 20 20 20 20 61 73 73  ge, sz);.    ass
14697 65 72 74 28 20 69 64 78 3e 30 20 29 3b 0a 20 20  ert( idx>0 );.  
14698 20 20 61 73 73 65 72 74 28 20 65 6e 64 20 3c 3d    assert( end <=
14699 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
1469a 68 64 72 2b 35 5d 29 20 29 3b 0a 20 20 20 20 70  hdr+5]) );.    p
1469b 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 2b 3b 0a 20  Page->nCell++;. 
1469c 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20     pPage->nFree 
1469d 2d 3d 20 32 3b 0a 20 20 20 20 6d 65 6d 63 70 79  -= 2;.    memcpy
1469e 28 26 64 61 74 61 5b 69 64 78 2b 6e 53 6b 69 70  (&data[idx+nSkip
1469f 5d 2c 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c 20  ], pCell+nSkip, 
146a0 73 7a 2d 6e 53 6b 69 70 29 3b 0a 20 20 20 20 66  sz-nSkip);.    f
146a1 6f 72 28 6a 3d 65 6e 64 2d 32 2c 20 70 74 72 3d  or(j=end-2, ptr=
146a2 26 64 61 74 61 5b 6a 5d 3b 20 6a 3e 69 6e 73 3b  &data[j]; j>ins;
146a3 20 6a 2d 3d 32 2c 20 70 74 72 2d 3d 32 29 7b 0a   j-=2, ptr-=2){.
146a4 20 20 20 20 20 20 70 74 72 5b 30 5d 20 3d 20 70        ptr[0] = p
146a5 74 72 5b 2d 32 5d 3b 0a 20 20 20 20 20 20 70 74  tr[-2];.      pt
146a6 72 5b 31 5d 20 3d 20 70 74 72 5b 2d 31 5d 3b 0a  r[1] = ptr[-1];.
146a7 20 20 20 20 7d 0a 20 20 20 20 70 75 74 32 62 79      }.    put2by
146a8 74 65 28 26 64 61 74 61 5b 69 6e 73 5d 2c 20 69  te(&data[ins], i
146a9 64 78 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74  dx);.    put2byt
146aa 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 2c 20  e(&data[hdr+3], 
146ab 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b 0a 23  pPage->nCell);.#
146ac 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
146ad 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
146ae 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 42 74    if( pPage->pBt
146af 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a  ->autoVacuum ){.
146b0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 65 6c        /* The cel
146b1 6c 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61 20  l may contain a 
146b2 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 6f 76  pointer to an ov
146b3 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49 66 20  erflow page. If 
146b4 73 6f 2c 20 77 72 69 74 65 0a 20 20 20 20 20 20  so, write.      
146b5 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 66 6f 72  ** the entry for
146b6 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61   the overflow pa
146b7 67 65 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e  ge into the poin
146b8 74 65 72 20 6d 61 70 2e 0a 20 20 20 20 20 20 2a  ter map..      *
146b9 2f 0a 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f  /.      CellInfo
146ba 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 73 71 6c   info;.      sql
146bb 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65  ite3BtreeParseCe
146bc 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65  llPtr(pPage, pCe
146bd 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20  ll, &info);.    
146be 20 20 61 73 73 65 72 74 28 20 28 69 6e 66 6f 2e    assert( (info.
146bf 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e 69 6e  nData+(pPage->in
146c0 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 79  tKey?0:info.nKey
146c1 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61  ))==info.nPayloa
146c2 64 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28  d );.      if( (
146c3 69 6e 66 6f 2e 6e 44 61 74 61 2b 28 70 50 61 67  info.nData+(pPag
146c4 65 2d 3e 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f  e->intKey?0:info
146c5 2e 6e 4b 65 79 29 29 3e 69 6e 66 6f 2e 6e 4c 6f  .nKey))>info.nLo
146c6 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 50  cal ){.        P
146c7 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 67  gno pgnoOvfl = g
146c8 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69  et4byte(&pCell[i
146c9 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b  nfo.iOverflow]);
146ca 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 74  .        rc = pt
146cb 72 6d 61 70 50 75 74 28 70 50 61 67 65 2d 3e 70  rmapPut(pPage->p
146cc 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 50 54  Bt, pgnoOvfl, PT
146cd 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c 20  RMAP_OVERFLOW1, 
146ce 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20  pPage->pgno);.  
146cf 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
146d0 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
146d1 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   rc;.      }.   
146d2 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20   }.#endif.  }.. 
146d3 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
146d4 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20  K;.}../*.** Add 
146d5 61 20 6c 69 73 74 20 6f 66 20 63 65 6c 6c 73 20  a list of cells 
146d6 74 6f 20 61 20 70 61 67 65 2e 20 20 54 68 65 20  to a page.  The 
146d7 70 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20 69  page should be i
146d8 6e 69 74 69 61 6c 6c 79 20 65 6d 70 74 79 2e 0a  nitially empty..
146d9 2a 2a 20 54 68 65 20 63 65 6c 6c 73 20 61 72 65  ** The cells are
146da 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 66   guaranteed to f
146db 69 74 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 0a  it on the page..
146dc 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
146dd 73 73 65 6d 62 6c 65 50 61 67 65 28 0a 20 20 4d  ssemblePage(.  M
146de 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20  emPage *pPage,  
146df 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 6f 20   /* The page to 
146e0 62 65 20 61 73 73 65 6d 62 6c 69 65 64 20 2a 2f  be assemblied */
146e1 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 20 20  .  int nCell,   
146e2 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62       /* The numb
146e3 65 72 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61  er of cells to a
146e4 64 64 20 74 6f 20 74 68 69 73 20 70 61 67 65 20  dd to this page 
146e5 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c  */.  u8 **apCell
146e6 2c 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65  ,      /* Pointe
146e7 72 73 20 74 6f 20 63 65 6c 6c 20 62 6f 64 69 65  rs to cell bodie
146e8 73 20 2a 2f 0a 20 20 75 31 36 20 2a 61 53 69 7a  s */.  u16 *aSiz
146e9 65 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65  e        /* Size
146ea 73 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 2a  s of the cells *
146eb 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20  /.){.  int i;   
146ec 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
146ed 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
146ee 74 20 74 6f 74 61 6c 53 69 7a 65 3b 20 20 20 20  t totalSize;    
146ef 2f 2a 20 54 6f 74 61 6c 20 73 69 7a 65 20 6f 66  /* Total size of
146f0 20 61 6c 6c 20 63 65 6c 6c 73 20 2a 2f 0a 20 20   all cells */.  
146f1 69 6e 74 20 68 64 72 3b 20 20 20 20 20 20 20 20  int hdr;        
146f2 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 70 61    /* Index of pa
146f3 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69  ge header */.  i
146f4 6e 74 20 63 65 6c 6c 70 74 72 3b 20 20 20 20 20  nt cellptr;     
146f5 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 6e   /* Address of n
146f6 65 78 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72  ext cell pointer
146f7 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 62 6f   */.  int cellbo
146f8 64 79 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 65  dy;     /* Addre
146f9 73 73 20 6f 66 20 6e 65 78 74 20 63 65 6c 6c 20  ss of next cell 
146fa 62 6f 64 79 20 2a 2f 0a 20 20 75 38 20 2a 64 61  body */.  u8 *da
146fb 74 61 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 44  ta;         /* D
146fc 61 74 61 20 66 6f 72 20 74 68 65 20 70 61 67 65  ata for the page
146fd 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
146fe 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d  Page->nOverflow=
146ff 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
14700 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
14701 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d  ld(pPage->pBt->m
14702 75 74 65 78 29 20 29 3b 0a 20 20 74 6f 74 61 6c  utex) );.  total
14703 53 69 7a 65 20 3d 20 30 3b 0a 20 20 66 6f 72 28  Size = 0;.  for(
14704 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b  i=0; i<nCell; i+
14705 2b 29 7b 0a 20 20 20 20 74 6f 74 61 6c 53 69 7a  +){.    totalSiz
14706 65 20 2b 3d 20 61 53 69 7a 65 5b 69 5d 3b 0a 20  e += aSize[i];. 
14707 20 7d 0a 20 20 61 73 73 65 72 74 28 20 74 6f 74   }.  assert( tot
14708 61 6c 53 69 7a 65 2b 32 2a 6e 43 65 6c 6c 3c 3d  alSize+2*nCell<=
14709 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 3b 0a  pPage->nFree );.
1470a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
1470b 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 63  >nCell==0 );.  c
1470c 65 6c 6c 70 74 72 20 3d 20 70 50 61 67 65 2d 3e  ellptr = pPage->
1470d 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 64 61  cellOffset;.  da
1470e 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74  ta = pPage->aDat
1470f 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65  a;.  hdr = pPage
14710 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 70  ->hdrOffset;.  p
14711 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  ut2byte(&data[hd
14712 72 2b 33 5d 2c 20 6e 43 65 6c 6c 29 3b 0a 20 20  r+3], nCell);.  
14713 69 66 28 20 6e 43 65 6c 6c 20 29 7b 0a 20 20 20  if( nCell ){.   
14714 20 63 65 6c 6c 62 6f 64 79 20 3d 20 61 6c 6c 6f   cellbody = allo
14715 63 61 74 65 53 70 61 63 65 28 70 50 61 67 65 2c  cateSpace(pPage,
14716 20 74 6f 74 61 6c 53 69 7a 65 29 3b 0a 20 20 20   totalSize);.   
14717 20 61 73 73 65 72 74 28 20 63 65 6c 6c 62 6f 64   assert( cellbod
14718 79 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  y>0 );.    asser
14719 74 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20  t( pPage->nFree 
1471a 3e 3d 20 32 2a 6e 43 65 6c 6c 20 29 3b 0a 20 20  >= 2*nCell );.  
1471b 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2d    pPage->nFree -
1471c 3d 20 32 2a 6e 43 65 6c 6c 3b 0a 20 20 20 20 66  = 2*nCell;.    f
1471d 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b  or(i=0; i<nCell;
1471e 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 75 74   i++){.      put
1471f 32 62 79 74 65 28 26 64 61 74 61 5b 63 65 6c 6c  2byte(&data[cell
14720 70 74 72 5d 2c 20 63 65 6c 6c 62 6f 64 79 29 3b  ptr], cellbody);
14721 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 64  .      memcpy(&d
14722 61 74 61 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20 61  ata[cellbody], a
14723 70 43 65 6c 6c 5b 69 5d 2c 20 61 53 69 7a 65 5b  pCell[i], aSize[
14724 69 5d 29 3b 0a 20 20 20 20 20 20 63 65 6c 6c 70  i]);.      cellp
14725 74 72 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 63  tr += 2;.      c
14726 65 6c 6c 62 6f 64 79 20 2b 3d 20 61 53 69 7a 65  ellbody += aSize
14727 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  [i];.    }.    a
14728 73 73 65 72 74 28 20 63 65 6c 6c 62 6f 64 79 3d  ssert( cellbody=
14729 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61  =pPage->pBt->usa
1472a 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 7d 0a 20  bleSize );.  }. 
1472b 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20   pPage->nCell = 
1472c 6e 43 65 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  nCell;.}../*.** 
1472d 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 61  The following pa
1472e 72 61 6d 65 74 65 72 73 20 64 65 74 65 72 6d 69  rameters determi
1472f 6e 65 20 68 6f 77 20 6d 61 6e 79 20 61 64 6a 61  ne how many adja
14730 63 65 6e 74 20 70 61 67 65 73 20 67 65 74 20 69  cent pages get i
14731 6e 76 6f 6c 76 65 64 0a 2a 2a 20 69 6e 20 61 20  nvolved.** in a 
14732 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65 72 61 74  balancing operat
14733 69 6f 6e 2e 20 20 4e 4e 20 69 73 20 74 68 65 20  ion.  NN is the 
14734 6e 75 6d 62 65 72 20 6f 66 20 6e 65 69 67 68 62  number of neighb
14735 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69  ors on either si
14736 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 67  de.** of the pag
14737 65 20 74 68 61 74 20 70 61 72 74 69 63 69 70 61  e that participa
14738 74 65 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63  te in the balanc
14739 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20  ing operation.  
1473a 4e 42 20 69 73 20 74 68 65 0a 2a 2a 20 74 6f 74  NB is the.** tot
1473b 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  al number of pag
1473c 65 73 20 74 68 61 74 20 70 61 72 74 69 63 69 70  es that particip
1473d 61 74 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20 74  ate, including t
1473e 68 65 20 74 61 72 67 65 74 20 70 61 67 65 20 61  he target page a
1473f 6e 64 0a 2a 2a 20 4e 4e 20 6e 65 69 67 68 62 6f  nd.** NN neighbo
14740 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64  rs on either sid
14741 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 69 6e  e..**.** The min
14742 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 4e 4e  imum value of NN
14743 20 69 73 20 31 20 28 6f 66 20 63 6f 75 72 73 65   is 1 (of course
14744 29 2e 20 20 49 6e 63 72 65 61 73 69 6e 67 20 4e  ).  Increasing N
14745 4e 20 61 62 6f 76 65 20 31 0a 2a 2a 20 28 74 6f  N above 1.** (to
14746 20 32 20 6f 72 20 33 29 20 67 69 76 65 73 20 61   2 or 3) gives a
14747 20 6d 6f 64 65 73 74 20 69 6d 70 72 6f 76 65 6d   modest improvem
14748 65 6e 74 20 69 6e 20 53 45 4c 45 43 54 20 61 6e  ent in SELECT an
14749 64 20 44 45 4c 45 54 45 20 70 65 72 66 6f 72 6d  d DELETE perform
1474a 61 6e 63 65 0a 2a 2a 20 69 6e 20 65 78 63 68 61  ance.** in excha
1474b 6e 67 65 20 66 6f 72 20 61 20 6c 61 72 67 65 72  nge for a larger
1474c 20 64 65 67 72 61 64 61 74 69 6f 6e 20 69 6e 20   degradation in 
1474d 49 4e 53 45 52 54 20 61 6e 64 20 55 50 44 41 54  INSERT and UPDAT
1474e 45 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 0a 2a  E performance..*
1474f 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 4e  * The value of N
14750 4e 20 61 70 70 65 61 72 73 20 74 6f 20 67 69 76  N appears to giv
14751 65 20 74 68 65 20 62 65 73 74 20 72 65 73 75 6c  e the best resul
14752 74 73 20 6f 76 65 72 61 6c 6c 2e 0a 2a 2f 0a 23  ts overall..*/.#
14753 64 65 66 69 6e 65 20 4e 4e 20 31 20 20 20 20 20  define NN 1     
14754 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
14755 72 20 6f 66 20 6e 65 69 67 68 62 6f 72 73 20 6f  r of neighbors o
14756 6e 20 65 69 74 68 65 72 20 73 69 64 65 20 6f 66  n either side of
14757 20 70 50 61 67 65 20 2a 2f 0a 23 64 65 66 69 6e   pPage */.#defin
14758 65 20 4e 42 20 28 4e 4e 2a 32 2b 31 29 20 20 20  e NB (NN*2+1)   
14759 20 20 20 2f 2a 20 54 6f 74 61 6c 20 70 61 67 65     /* Total page
1475a 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 74 68  s involved in th
1475b 65 20 62 61 6c 61 6e 63 65 20 2a 2f 0a 0a 2f 2a  e balance */../*
1475c 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e   Forward referen
1475d 63 65 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  ce */.static int
1475e 20 62 61 6c 61 6e 63 65 28 42 74 43 75 72 73 6f   balance(BtCurso
1475f 72 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69 66 6e 64  r*, int);..#ifnd
14760 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51  ef SQLITE_OMIT_Q
14761 55 49 43 4b 42 41 4c 41 4e 43 45 0a 2f 2a 0a 2a  UICKBALANCE./*.*
14762 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f  * This version o
14763 66 20 62 61 6c 61 6e 63 65 28 29 20 68 61 6e 64  f balance() hand
14764 6c 65 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 73  les the common s
14765 70 65 63 69 61 6c 20 63 61 73 65 20 77 68 65 72  pecial case wher
14766 65 0a 2a 2a 20 61 20 6e 65 77 20 65 6e 74 72 79  e.** a new entry
14767 20 69 73 20 62 65 69 6e 67 20 69 6e 73 65 72 74   is being insert
14768 65 64 20 6f 6e 20 74 68 65 20 65 78 74 72 65 6d  ed on the extrem
14769 65 20 72 69 67 68 74 2d 65 6e 64 20 6f 66 20 74  e right-end of t
1476a 68 65 0a 2a 2a 20 74 72 65 65 2c 20 69 6e 20 6f  he.** tree, in o
1476b 74 68 65 72 20 77 6f 72 64 73 2c 20 77 68 65 6e  ther words, when
1476c 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 77   the new entry w
1476d 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 6c  ill become the l
1476e 61 72 67 65 73 74 0a 2a 2a 20 65 6e 74 72 79 20  argest.** entry 
1476f 69 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 0a  in the tree..**.
14770 2a 2a 20 49 6e 73 74 65 61 64 20 6f 66 20 74 72  ** Instead of tr
14771 79 69 6e 67 20 62 61 6c 61 6e 63 65 20 74 68 65  ying balance the
14772 20 33 20 72 69 67 68 74 2d 6d 6f 73 74 20 6c 65   3 right-most le
14773 61 66 20 70 61 67 65 73 2c 20 6a 75 73 74 20 61  af pages, just a
14774 64 64 0a 2a 2a 20 61 20 6e 65 77 20 70 61 67 65  dd.** a new page
14775 20 74 6f 20 74 68 65 20 72 69 67 68 74 2d 68 61   to the right-ha
14776 6e 64 20 73 69 64 65 20 61 6e 64 20 70 75 74 20  nd side and put 
14777 74 68 65 20 6f 6e 65 20 6e 65 77 20 65 6e 74 72  the one new entr
14778 79 20 69 6e 0a 2a 2a 20 74 68 61 74 20 70 61 67  y in.** that pag
14779 65 2e 20 20 54 68 69 73 20 6c 65 61 76 65 73 20  e.  This leaves 
1477a 74 68 65 20 72 69 67 68 74 20 73 69 64 65 20 6f  the right side o
1477b 66 20 74 68 65 20 74 72 65 65 20 73 6f 6d 65 77  f the tree somew
1477c 68 61 74 0a 2a 2a 20 75 6e 62 61 6c 61 6e 63 65  hat.** unbalance
1477d 64 2e 20 20 42 75 74 20 6f 64 64 73 20 61 72 65  d.  But odds are
1477e 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 62 65   that we will be
1477f 20 69 6e 73 65 72 74 69 6e 67 20 6e 65 77 20 65   inserting new e
14780 6e 74 72 69 65 73 0a 2a 2a 20 61 74 20 74 68 65  ntries.** at the
14781 20 65 6e 64 20 73 6f 6f 6e 20 61 66 74 65 72 77   end soon afterw
14782 61 72 64 73 20 73 6f 20 74 68 65 20 6e 65 61 72  ards so the near
14783 6c 79 20 65 6d 70 74 79 20 70 61 67 65 20 77 69  ly empty page wi
14784 6c 6c 20 71 75 69 63 6b 6c 79 0a 2a 2a 20 66 69  ll quickly.** fi
14785 6c 6c 20 75 70 2e 20 20 4f 6e 20 61 76 65 72 61  ll up.  On avera
14786 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 50 61 67 65 20  ge..**.** pPage 
14787 69 73 20 74 68 65 20 6c 65 61 66 20 70 61 67 65  is the leaf page
14788 20 77 68 69 63 68 20 69 73 20 74 68 65 20 72 69   which is the ri
14789 67 68 74 2d 6d 6f 73 74 20 70 61 67 65 20 69 6e  ght-most page in
1478a 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 20 70 50   the tree..** pP
1478b 61 72 65 6e 74 20 69 73 20 69 74 73 20 70 61 72  arent is its par
1478c 65 6e 74 2e 20 20 70 50 61 67 65 20 6d 75 73 74  ent.  pPage must
1478d 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 20 6f   have a single o
1478e 76 65 72 66 6c 6f 77 20 65 6e 74 72 79 0a 2a 2a  verflow entry.**
1478f 20 77 68 69 63 68 20 69 73 20 61 6c 73 6f 20 74   which is also t
14790 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e  he right-most en
14791 74 72 79 20 6f 6e 20 74 68 65 20 70 61 67 65 2e  try on the page.
14792 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62  .*/.static int b
14793 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 42 74 43  alance_quick(BtC
14794 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20  ursor *pCur){.  
14795 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67  int rc;.  MemPag
14796 65 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 50  e *pNew = 0;.  P
14797 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b 0a 20 20 75  gno pgnoNew;.  u
14798 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 75 31 36 20  8 *pCell;.  u16 
14799 73 7a 43 65 6c 6c 3b 0a 20 20 43 65 6c 6c 49 6e  szCell;.  CellIn
1479a 66 6f 20 69 6e 66 6f 3b 0a 20 20 4d 65 6d 50 61  fo info;.  MemPa
1479b 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72  ge *pPage = pCur
1479c 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
1479d 50 61 67 65 5d 3b 0a 20 20 4d 65 6d 50 61 67 65  Page];.  MemPage
1479e 20 2a 70 50 61 72 65 6e 74 20 3d 20 70 43 75 72   *pParent = pCur
1479f 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
147a0 50 61 67 65 2d 31 5d 3b 0a 20 20 42 74 53 68 61  Page-1];.  BtSha
147a1 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65  red *pBt = pPage
147a2 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 70 61 72  ->pBt;.  int par
147a3 65 6e 74 49 64 78 20 3d 20 70 50 61 72 65 6e 74  entIdx = pParent
147a4 2d 3e 6e 43 65 6c 6c 3b 20 20 20 2f 2a 20 70 50  ->nCell;   /* pP
147a5 61 72 65 6e 74 20 6e 65 77 20 64 69 76 69 64 65  arent new divide
147a6 72 20 63 65 6c 6c 20 69 6e 64 65 78 20 2a 2f 0a  r cell index */.
147a7 20 20 69 6e 74 20 70 61 72 65 6e 74 53 69 7a 65    int parentSize
147a8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
147a9 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6e      /* Size of n
147aa 65 77 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20  ew divider cell 
147ab 2a 2f 0a 20 20 75 38 20 70 61 72 65 6e 74 43 65  */.  u8 parentCe
147ac 6c 6c 5b 36 34 5d 3b 20 20 20 20 20 20 20 20 20  ll[64];         
147ad 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20         /* Space 
147ae 66 6f 72 20 74 68 65 20 6e 65 77 20 64 69 76 69  for the new divi
147af 64 65 72 20 63 65 6c 6c 20 2a 2f 0a 0a 20 20 61  der cell */..  a
147b0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
147b1 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d  utex_held(pPage-
147b2 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  >pBt->mutex) );.
147b3 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61  .  /* Allocate a
147b4 20 6e 65 77 20 70 61 67 65 2e 20 49 6e 73 65 72   new page. Inser
147b5 74 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63  t the overflow c
147b6 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 0a 20  ell from pPage. 
147b7 20 2a 2a 20 69 6e 74 6f 20 69 74 2e 20 54 68 65   ** into it. The
147b8 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 6f 76 65  n remove the ove
147b9 72 66 6c 6f 77 20 63 65 6c 6c 20 66 72 6f 6d 20  rflow cell from 
147ba 70 50 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 72 63  pPage..  */.  rc
147bb 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65   = allocateBtree
147bc 50 61 67 65 28 70 42 74 2c 20 26 70 4e 65 77 2c  Page(pBt, &pNew,
147bd 20 26 70 67 6e 6f 4e 65 77 2c 20 30 2c 20 30 29   &pgnoNew, 0, 0)
147be 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
147bf 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 43 65  TE_OK ){.    pCe
147c0 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 61 4f 76 66  ll = pPage->aOvf
147c1 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a 20 20 20 20  l[0].pCell;.    
147c2 73 7a 43 65 6c 6c 20 3d 20 63 65 6c 6c 53 69 7a  szCell = cellSiz
147c3 65 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c  ePtr(pPage, pCel
147c4 6c 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67 65  l);.    zeroPage
147c5 28 70 4e 65 77 2c 20 70 50 61 67 65 2d 3e 61 44  (pNew, pPage->aD
147c6 61 74 61 5b 30 5d 29 3b 0a 20 20 20 20 61 73 73  ata[0]);.    ass
147c7 65 6d 62 6c 65 50 61 67 65 28 70 4e 65 77 2c 20  emblePage(pNew, 
147c8 31 2c 20 26 70 43 65 6c 6c 2c 20 26 73 7a 43 65  1, &pCell, &szCe
147c9 6c 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  ll);.    pPage->
147ca 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20  nOverflow = 0;. 
147cb 20 0a 20 20 20 20 2f 2a 20 70 50 61 67 65 20 69   .    /* pPage i
147cc 73 20 63 75 72 72 65 6e 74 6c 79 20 74 68 65 20  s currently the 
147cd 72 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 70  right-child of p
147ce 50 61 72 65 6e 74 2e 20 43 68 61 6e 67 65 20 74  Parent. Change t
147cf 68 69 73 0a 20 20 20 20 2a 2a 20 73 6f 20 74 68  his.    ** so th
147d0 61 74 20 74 68 65 20 72 69 67 68 74 2d 63 68 69  at the right-chi
147d1 6c 64 20 69 73 20 74 68 65 20 6e 65 77 20 70 61  ld is the new pa
147d2 67 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 62 6f  ge allocated abo
147d3 76 65 20 61 6e 64 0a 20 20 20 20 2a 2a 20 70 50  ve and.    ** pP
147d4 61 67 65 20 69 73 20 74 68 65 20 6e 65 78 74 2d  age is the next-
147d5 74 6f 2d 72 69 67 68 74 20 63 68 69 6c 64 2e 20  to-right child. 
147d6 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49  .    **.    ** I
147d7 67 6e 6f 72 65 20 74 68 65 20 72 65 74 75 72 6e  gnore the return
147d8 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 61   value of the ca
147d9 6c 6c 20 74 6f 20 66 69 6c 6c 49 6e 43 65 6c 6c  ll to fillInCell
147da 28 29 2e 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 29  (). fillInCell()
147db 0a 20 20 20 20 2a 2a 20 6d 61 79 20 6f 6e 6c 79  .    ** may only
147dc 20 72 65 74 75 72 6e 20 6f 74 68 65 72 20 74 68   return other th
147dd 61 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20  an SQLITE_OK if 
147de 69 74 20 69 73 20 72 65 71 75 69 72 65 64 20 74  it is required t
147df 6f 20 61 6c 6c 6f 63 61 74 65 0a 20 20 20 20 2a  o allocate.    *
147e0 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 76  * one or more ov
147e1 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 53 69  erflow pages. Si
147e2 6e 63 65 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20  nce an internal 
147e3 74 61 62 6c 65 20 42 2d 54 72 65 65 20 63 65 6c  table B-Tree cel
147e4 6c 20 0a 20 20 20 20 2a 2a 20 6d 61 79 20 6e 65  l .    ** may ne
147e5 76 65 72 20 73 70 69 6c 6c 20 6f 76 65 72 20 6f  ver spill over o
147e6 6e 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20  nto an overflow 
147e7 70 61 67 65 20 28 69 74 20 69 73 20 61 20 6d 61  page (it is a ma
147e8 78 69 6d 75 6d 20 6f 66 20 0a 20 20 20 20 2a 2a  ximum of .    **
147e9 20 31 33 20 62 79 74 65 73 20 69 6e 20 73 69 7a   13 bytes in siz
147ea 65 29 2c 20 69 74 20 69 73 20 6e 6f 74 20 6e 65  e), it is not ne
147eb 63 63 65 73 73 61 72 79 20 74 6f 20 63 68 65 63  ccessary to chec
147ec 6b 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64  k the return cod
147ed 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  e..    **.    **
147ee 20 53 69 6d 69 6c 61 72 6c 79 2c 20 74 68 65 20   Similarly, the 
147ef 69 6e 73 65 72 74 43 65 6c 6c 28 29 20 66 75 6e  insertCell() fun
147f0 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69  ction cannot fai
147f1 6c 20 69 66 20 74 68 65 20 70 61 67 65 0a 20 20  l if the page.  
147f2 20 20 2a 2a 20 62 65 69 6e 67 20 69 6e 73 65 72    ** being inser
147f3 74 65 64 20 69 6e 74 6f 20 69 73 20 61 6c 72 65  ted into is alre
147f4 61 64 79 20 77 72 69 74 61 62 6c 65 20 61 6e 64  ady writable and
147f5 20 74 68 65 20 63 65 6c 6c 20 64 6f 65 73 20 6e   the cell does n
147f6 6f 74 20 0a 20 20 20 20 2a 2a 20 63 6f 6e 74 61  ot .    ** conta
147f7 69 6e 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70  in an overflow p
147f8 6f 69 6e 74 65 72 2e 20 53 6f 20 69 67 6e 6f 72  ointer. So ignor
147f9 65 20 74 68 69 73 20 72 65 74 75 72 6e 20 63 6f  e this return co
147fa 64 65 20 74 6f 6f 2e 0a 20 20 20 20 2a 2f 0a 20  de too..    */. 
147fb 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
147fc 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 20  ->nCell>0 );.   
147fd 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c   pCell = findCel
147fe 6c 28 70 50 61 67 65 2c 20 70 50 61 67 65 2d 3e  l(pPage, pPage->
147ff 6e 43 65 6c 6c 2d 31 29 3b 0a 20 20 20 20 73 71  nCell-1);.    sq
14800 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43  lite3BtreeParseC
14801 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43  ellPtr(pPage, pC
14802 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20  ell, &info);.   
14803 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 70 50 61 72   fillInCell(pPar
14804 65 6e 74 2c 20 70 61 72 65 6e 74 43 65 6c 6c 2c  ent, parentCell,
14805 20 30 2c 20 69 6e 66 6f 2e 6e 4b 65 79 2c 20 30   0, info.nKey, 0
14806 2c 20 30 2c 20 30 2c 20 26 70 61 72 65 6e 74 53  , 0, 0, &parentS
14807 69 7a 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ize);.    assert
14808 28 20 70 61 72 65 6e 74 53 69 7a 65 3c 36 34 20  ( parentSize<64 
14809 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  );.    assert( s
1480a 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
1480b 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e  teable(pParent->
1480c 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20 20  pDbPage) );.    
1480d 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 72 65  insertCell(pPare
1480e 6e 74 2c 20 70 61 72 65 6e 74 49 64 78 2c 20 70  nt, parentIdx, p
1480f 61 72 65 6e 74 43 65 6c 6c 2c 20 70 61 72 65 6e  arentCell, paren
14810 74 53 69 7a 65 2c 20 30 2c 20 34 29 3b 0a 20 20  tSize, 0, 4);.  
14811 20 20 70 75 74 34 62 79 74 65 28 66 69 6e 64 4f    put4byte(findO
14812 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 50 61 72  verflowCell(pPar
14813 65 6e 74 2c 70 61 72 65 6e 74 49 64 78 29 2c 20  ent,parentIdx), 
14814 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20  pPage->pgno);.  
14815 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 72    put4byte(&pPar
14816 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65  ent->aData[pPare
14817 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  nt->hdrOffset+8]
14818 2c 20 70 67 6e 6f 4e 65 77 29 3b 0a 20 20 0a 20  , pgnoNew);.  . 
14819 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73     /* If this is
1481a 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20   an auto-vacuum 
1481b 64 61 74 61 62 61 73 65 2c 20 75 70 64 61 74 65  database, update
1481c 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   the pointer map
1481d 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 65 6e 74  .    ** with ent
1481e 72 69 65 73 20 66 6f 72 20 74 68 65 20 6e 65 77  ries for the new
1481f 20 70 61 67 65 2c 20 61 6e 64 20 61 6e 79 20 70   page, and any p
14820 6f 69 6e 74 65 72 20 66 72 6f 6d 20 74 68 65 20  ointer from the 
14821 0a 20 20 20 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20  .    ** cell on 
14822 74 68 65 20 70 61 67 65 20 74 6f 20 61 6e 20 6f  the page to an o
14823 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 20 20  verflow page..  
14824 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 49 53 41    */.    if( ISA
14825 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20  UTOVACUUM ){.   
14826 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75     rc = ptrmapPu
14827 74 28 70 42 74 2c 20 70 67 6e 6f 4e 65 77 2c 20  t(pBt, pgnoNew, 
14828 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50  PTRMAP_BTREE, pP
14829 61 72 65 6e 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20  arent->pgno);.  
1482a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1482b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1482c 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 4f   rc = ptrmapPutO
1482d 76 66 6c 28 70 4e 65 77 2c 20 30 29 3b 0a 20 20  vfl(pNew, 0);.  
1482e 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
1482f 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20   /* Release the 
14830 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65  reference to the
14831 20 6e 65 77 20 70 61 67 65 2e 20 2a 2f 0a 20 20   new page. */.  
14832 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e    releasePage(pN
14833 65 77 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41  ew);.  }..  /* A
14834 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65  t this point the
14835 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 76 61   pPage->nFree va
14836 72 69 61 62 6c 65 20 69 73 20 6e 6f 74 20 73 65  riable is not se
14837 74 20 63 6f 72 72 65 63 74 6c 79 20 77 69 74 68  t correctly with
14838 0a 20 20 2a 2a 20 72 65 73 70 65 63 74 20 74 6f  .  ** respect to
14839 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
1483a 74 68 65 20 70 61 67 65 20 28 62 65 63 61 75 73  the page (becaus
1483b 65 20 69 74 20 77 61 73 20 73 65 74 20 74 6f 20  e it was set to 
1483c 30 20 62 79 20 0a 20 20 2a 2a 20 69 6e 73 65 72  0 by .  ** inser
1483d 74 43 65 6c 6c 29 2e 20 53 6f 20 63 61 6c 6c 20  tCell). So call 
1483e 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74  sqlite3BtreeInit
1483f 50 61 67 65 28 29 20 74 6f 20 6d 61 6b 65 20 73  Page() to make s
14840 75 72 65 20 69 74 20 69 73 0a 20 20 2a 2a 20 63  ure it is.  ** c
14841 6f 72 72 65 63 74 2e 0a 20 20 2a 2a 0a 20 20 2a  orrect..  **.  *
14842 2a 20 54 68 69 73 20 68 61 73 20 74 6f 20 62 65  * This has to be
14843 20 64 6f 6e 65 20 65 76 65 6e 20 69 66 20 61 6e   done even if an
14844 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20 72   error will be r
14845 65 74 75 72 6e 65 64 2e 20 4e 6f 72 6d 61 6c 6c  eturned. Normall
14846 79 2c 20 69 66 0a 20 20 2a 2a 20 61 6e 20 65 72  y, if.  ** an er
14847 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e  ror occurs durin
14848 67 20 74 72 65 65 20 62 61 6c 61 6e 63 69 6e 67  g tree balancing
14849 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  , the contents o
1484a 66 20 4d 65 6d 50 61 67 65 20 61 72 65 0a 20 20  f MemPage are.  
1484b 2a 2a 20 6e 6f 74 20 69 6d 70 6f 72 74 61 6e 74  ** not important
1484c 2c 20 61 73 20 74 68 65 79 20 77 69 6c 6c 20 62  , as they will b
1484d 65 20 72 65 63 61 6c 63 75 6c 61 74 65 64 20 77  e recalculated w
1484e 68 65 6e 20 74 68 65 20 70 61 67 65 20 69 73 20  hen the page is 
1484f 72 6f 6c 6c 65 64 0a 20 20 2a 2a 20 62 61 63 6b  rolled.  ** back
14850 2e 20 42 75 74 20 68 65 72 65 2c 20 69 6e 20 62  . But here, in b
14851 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 2c 20  alance_quick(), 
14852 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74  it is possible t
14853 68 61 74 20 70 50 61 67 65 20 68 61 73 20 0a 20  hat pPage has . 
14854 20 2a 2a 20 6e 6f 74 20 79 65 74 20 62 65 65 6e   ** not yet been
14855 20 6d 61 72 6b 65 64 20 64 69 72 74 79 20 6f 72   marked dirty or
14856 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
14857 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  e journal file. 
14858 54 68 65 72 65 66 6f 72 65 0a 20 20 2a 2a 20 69  Therefore.  ** i
14859 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 72 6f  t will not be ro
1485a 6c 6c 65 64 20 62 61 63 6b 20 61 6e 64 20 73 6f  lled back and so
1485b 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74   it is important
1485c 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
1485d 61 74 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65  at.  ** the page
1485e 20 64 61 74 61 20 61 6e 64 20 63 6f 6e 74 65 6e   data and conten
1485f 74 73 20 6f 66 20 4d 65 6d 50 61 67 65 20 61 72  ts of MemPage ar
14860 65 20 63 6f 6e 73 69 73 74 65 6e 74 2e 0a 20 20  e consistent..  
14861 2a 2f 0a 20 20 70 50 61 67 65 2d 3e 69 73 49 6e  */.  pPage->isIn
14862 69 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  it = 0;.  sqlite
14863 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 70  3BtreeInitPage(p
14864 50 61 67 65 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  Page);..  /* If 
14865 65 76 65 72 79 74 68 69 6e 67 20 65 6c 73 65 20  everything else 
14866 73 75 63 63 65 65 64 65 64 2c 20 62 61 6c 61 6e  succeeded, balan
14867 63 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 61  ce the parent pa
14868 67 65 2c 20 69 6e 20 0a 20 20 2a 2a 20 63 61 73  ge, in .  ** cas
14869 65 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65  e the divider ce
1486a 6c 6c 20 69 6e 73 65 72 74 65 64 20 63 61 75 73  ll inserted caus
1486b 65 64 20 69 74 20 74 6f 20 62 65 63 6f 6d 65 20  ed it to become 
1486c 6f 76 65 72 66 75 6c 6c 2e 0a 20 20 2a 2f 0a 20  overfull..  */. 
1486d 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1486e 4f 4b 20 29 7b 0a 20 20 20 20 72 65 6c 65 61 73  OK ){.    releas
1486f 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
14870 20 20 70 43 75 72 2d 3e 69 50 61 67 65 2d 2d 3b    pCur->iPage--;
14871 0a 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63  .    rc = balanc
14872 65 28 70 43 75 72 2c 20 30 29 3b 0a 20 20 7d 0a  e(pCur, 0);.  }.
14873 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23    return rc;.}.#
14874 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
14875 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43  OMIT_QUICKBALANC
14876 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  E */../*.** This
14877 20 72 6f 75 74 69 6e 65 20 72 65 64 69 73 74 72   routine redistr
14878 69 62 75 74 65 73 20 43 65 6c 6c 73 20 6f 6e 20  ibutes Cells on 
14879 70 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f 20  pPage and up to 
1487a 4e 4e 2a 32 20 73 69 62 6c 69 6e 67 73 0a 2a 2a  NN*2 siblings.**
1487b 20 6f 66 20 70 50 61 67 65 20 73 6f 20 74 68 61   of pPage so tha
1487c 74 20 61 6c 6c 20 70 61 67 65 73 20 68 61 76 65  t all pages have
1487d 20 61 62 6f 75 74 20 74 68 65 20 73 61 6d 65 20   about the same 
1487e 61 6d 6f 75 6e 74 20 6f 66 20 66 72 65 65 20 73  amount of free s
1487f 70 61 63 65 2e 0a 2a 2a 20 55 73 75 61 6c 6c 79  pace..** Usually
14880 20 4e 4e 20 73 69 62 6c 69 6e 67 73 20 6f 6e 20   NN siblings on 
14881 65 69 74 68 65 72 20 73 69 64 65 20 6f 66 20 70  either side of p
14882 50 61 67 65 20 69 73 20 75 73 65 64 20 69 6e 20  Page is used in 
14883 74 68 65 20 62 61 6c 61 6e 63 69 6e 67 2c 0a 2a  the balancing,.*
14884 2a 20 74 68 6f 75 67 68 20 6d 6f 72 65 20 73 69  * though more si
14885 62 6c 69 6e 67 73 20 6d 69 67 68 74 20 63 6f 6d  blings might com
14886 65 20 66 72 6f 6d 20 6f 6e 65 20 73 69 64 65 20  e from one side 
14887 69 66 20 70 50 61 67 65 20 69 73 20 74 68 65 20  if pPage is the 
14888 66 69 72 73 74 0a 2a 2a 20 6f 72 20 6c 61 73 74  first.** or last
14889 20 63 68 69 6c 64 20 6f 66 20 69 74 73 20 70 61   child of its pa
1488a 72 65 6e 74 2e 20 20 49 66 20 70 50 61 67 65 20  rent.  If pPage 
1488b 68 61 73 20 66 65 77 65 72 20 74 68 61 6e 20 32  has fewer than 2
1488c 2a 4e 4e 20 73 69 62 6c 69 6e 67 73 0a 2a 2a 20  *NN siblings.** 
1488d 28 73 6f 6d 65 74 68 69 6e 67 20 77 68 69 63 68  (something which
1488e 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e   can only happen
1488f 20 69 66 20 70 50 61 67 65 20 69 73 20 74 68 65   if pPage is the
14890 20 72 6f 6f 74 20 70 61 67 65 20 6f 72 20 61 20   root page or a 
14891 0a 2a 2a 20 63 68 69 6c 64 20 6f 66 20 72 6f 6f  .** child of roo
14892 74 29 20 74 68 65 6e 20 61 6c 6c 20 61 76 61 69  t) then all avai
14893 6c 61 62 6c 65 20 73 69 62 6c 69 6e 67 73 20 70  lable siblings p
14894 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 74 68  articipate in th
14895 65 20 62 61 6c 61 6e 63 69 6e 67 2e 0a 2a 2a 0a  e balancing..**.
14896 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66  ** The number of
14897 20 73 69 62 6c 69 6e 67 73 20 6f 66 20 70 50 61   siblings of pPa
14898 67 65 20 6d 69 67 68 74 20 62 65 20 69 6e 63 72  ge might be incr
14899 65 61 73 65 64 20 6f 72 20 64 65 63 72 65 61 73  eased or decreas
1489a 65 64 20 62 79 20 6f 6e 65 20 6f 72 0a 2a 2a 20  ed by one or.** 
1489b 74 77 6f 20 69 6e 20 61 6e 20 65 66 66 6f 72 74  two in an effort
1489c 20 74 6f 20 6b 65 65 70 20 70 61 67 65 73 20 6e   to keep pages n
1489d 65 61 72 6c 79 20 66 75 6c 6c 20 62 75 74 20 6e  early full but n
1489e 6f 74 20 6f 76 65 72 20 66 75 6c 6c 2e 20 54 68  ot over full. Th
1489f 65 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a 20 69  e root page.** i
148a0 73 20 73 70 65 63 69 61 6c 20 61 6e 64 20 69 73  s special and is
148a1 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65 20 6e   allowed to be n
148a2 65 61 72 6c 79 20 65 6d 70 74 79 2e 20 49 66 20  early empty. If 
148a3 70 50 61 67 65 20 69 73 20 0a 2a 2a 20 74 68 65  pPage is .** the
148a4 20 72 6f 6f 74 20 70 61 67 65 2c 20 74 68 65 6e   root page, then
148a5 20 74 68 65 20 64 65 70 74 68 20 6f 66 20 74 68   the depth of th
148a6 65 20 74 72 65 65 20 6d 69 67 68 74 20 62 65 20  e tree might be 
148a7 69 6e 63 72 65 61 73 65 64 0a 2a 2a 20 6f 72 20  increased.** or 
148a8 64 65 63 72 65 61 73 65 64 20 62 79 20 6f 6e 65  decreased by one
148a9 2c 20 61 73 20 6e 65 63 65 73 73 61 72 79 2c 20  , as necessary, 
148aa 74 6f 20 6b 65 65 70 20 74 68 65 20 72 6f 6f 74  to keep the root
148ab 20 70 61 67 65 20 66 72 6f 6d 20 62 65 69 6e 67   page from being
148ac 0a 2a 2a 20 6f 76 65 72 66 75 6c 6c 20 6f 72 20  .** overfull or 
148ad 63 6f 6d 70 6c 65 74 65 6c 79 20 65 6d 70 74 79  completely empty
148ae 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
148af 74 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74  t when this rout
148b0 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 73  ine is called, s
148b1 6f 6d 65 20 6f 66 20 74 68 65 20 43 65 6c 6c 73  ome of the Cells
148b2 20 6f 6e 20 70 50 61 67 65 0a 2a 2a 20 6d 69 67   on pPage.** mig
148b3 68 74 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20  ht not actually 
148b4 62 65 20 73 74 6f 72 65 64 20 69 6e 20 70 50 61  be stored in pPa
148b5 67 65 2d 3e 61 44 61 74 61 5b 5d 2e 20 20 54 68  ge->aData[].  Th
148b6 69 73 20 63 61 6e 20 68 61 70 70 65 6e 0a 2a 2a  is can happen.**
148b7 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20   if the page is 
148b8 6f 76 65 72 66 75 6c 6c 2e 20 20 50 61 72 74 20  overfull.  Part 
148b9 6f 66 20 74 68 65 20 6a 6f 62 20 6f 66 20 74 68  of the job of th
148ba 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 6f  is routine is to
148bb 0a 2a 2a 20 6d 61 6b 65 20 73 75 72 65 20 61 6c  .** make sure al
148bc 6c 20 43 65 6c 6c 73 20 66 6f 72 20 70 50 61 67  l Cells for pPag
148bd 65 20 6f 6e 63 65 20 61 67 61 69 6e 20 66 69 74  e once again fit
148be 20 69 6e 20 70 50 61 67 65 2d 3e 61 44 61 74 61   in pPage->aData
148bf 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65  []..**.** In the
148c0 20 63 6f 75 72 73 65 20 6f 66 20 62 61 6c 61 6e   course of balan
148c1 63 69 6e 67 20 74 68 65 20 73 69 62 6c 69 6e 67  cing the sibling
148c2 73 20 6f 66 20 70 50 61 67 65 2c 20 74 68 65 20  s of pPage, the 
148c3 70 61 72 65 6e 74 20 6f 66 20 70 50 61 67 65 0a  parent of pPage.
148c4 2a 2a 20 6d 69 67 68 74 20 62 65 63 6f 6d 65 20  ** might become 
148c5 6f 76 65 72 66 75 6c 6c 20 6f 72 20 75 6e 64 65  overfull or unde
148c6 72 66 75 6c 6c 2e 20 20 49 66 20 74 68 61 74 20  rfull.  If that 
148c7 68 61 70 70 65 6e 73 2c 20 74 68 65 6e 20 74 68  happens, then th
148c8 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73  is routine.** is
148c9 20 63 61 6c 6c 65 64 20 72 65 63 75 72 73 69 76   called recursiv
148ca 65 6c 79 20 6f 6e 20 74 68 65 20 70 61 72 65 6e  ely on the paren
148cb 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73  t..**.** If this
148cc 20 72 6f 75 74 69 6e 65 20 66 61 69 6c 73 20 66   routine fails f
148cd 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e 2c 20 69  or any reason, i
148ce 74 20 6d 69 67 68 74 20 6c 65 61 76 65 20 74 68  t might leave th
148cf 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 6e  e database.** in
148d0 20 61 20 63 6f 72 72 75 70 74 65 64 20 73 74 61   a corrupted sta
148d1 74 65 2e 20 20 53 6f 20 69 66 20 74 68 69 73 20  te.  So if this 
148d2 72 6f 75 74 69 6e 65 20 66 61 69 6c 73 2c 20 74  routine fails, t
148d3 68 65 20 64 61 74 61 62 61 73 65 20 73 68 6f 75  he database shou
148d4 6c 64 0a 2a 2a 20 62 65 20 72 6f 6c 6c 65 64 20  ld.** be rolled 
148d5 62 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  back..*/.static 
148d6 69 6e 74 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72  int balance_nonr
148d7 6f 6f 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43  oot(BtCursor *pC
148d8 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  ur){.  MemPage *
148d9 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20  pPage;          
148da 20 20 20 20 2f 2a 20 54 68 65 20 6f 76 65 72 20      /* The over 
148db 6f 72 20 75 6e 64 65 72 66 75 6c 6c 20 70 61 67  or underfull pag
148dc 65 20 74 6f 20 62 61 6c 61 6e 63 65 20 2a 2f 0a  e to balance */.
148dd 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 72 65    MemPage *pPare
148de 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  nt;            /
148df 2a 20 54 68 65 20 70 61 72 65 6e 74 20 6f 66 20  * The parent of 
148e0 70 50 61 67 65 20 2a 2f 0a 20 20 42 74 53 68 61  pPage */.  BtSha
148e1 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20  red *pBt;       
148e2 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 77          /* The w
148e3 68 6f 6c 65 20 64 61 74 61 62 61 73 65 20 2a 2f  hole database */
148e4 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 30  .  int nCell = 0
148e5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
148e6 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c  /* Number of cel
148e7 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a  ls in apCell[] *
148e8 2f 0a 20 20 69 6e 74 20 6e 4d 61 78 43 65 6c 6c  /.  int nMaxCell
148e9 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  s = 0;          
148ea 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73 69   /* Allocated si
148eb 7a 65 20 6f 66 20 61 70 43 65 6c 6c 2c 20 73 7a  ze of apCell, sz
148ec 43 65 6c 6c 2c 20 61 46 72 6f 6d 2e 20 2a 2f 0a  Cell, aFrom. */.
148ed 20 20 69 6e 74 20 6e 4f 6c 64 3b 20 20 20 20 20    int nOld;     
148ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
148ef 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  * Number of page
148f0 73 20 69 6e 20 61 70 4f 6c 64 5b 5d 20 2a 2f 0a  s in apOld[] */.
148f1 20 20 69 6e 74 20 6e 4e 65 77 3b 20 20 20 20 20    int nNew;     
148f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
148f3 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  * Number of page
148f4 73 20 69 6e 20 61 70 4e 65 77 5b 5d 20 2a 2f 0a  s in apNew[] */.
148f5 20 20 69 6e 74 20 6e 44 69 76 3b 20 20 20 20 20    int nDiv;     
148f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
148f7 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c  * Number of cell
148f8 73 20 69 6e 20 61 70 44 69 76 5b 5d 20 2a 2f 0a  s in apDiv[] */.
148f9 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6b 3b 20 20    int i, j, k;  
148fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
148fb 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20  * Loop counters 
148fc 2a 2f 0a 20 20 69 6e 74 20 69 64 78 3b 20 20 20  */.  int idx;   
148fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
148fe 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 70 50    /* Index of pP
148ff 61 67 65 20 69 6e 20 70 50 61 72 65 6e 74 2d 3e  age in pParent->
14900 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74  aCell[] */.  int
14901 20 6e 78 44 69 76 3b 20 20 20 20 20 20 20 20 20   nxDiv;         
14902 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78            /* Nex
14903 74 20 64 69 76 69 64 65 72 20 73 6c 6f 74 20 69  t divider slot i
14904 6e 20 70 50 61 72 65 6e 74 2d 3e 61 43 65 6c 6c  n pParent->aCell
14905 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20  [] */.  int rc; 
14906 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14907 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74 75       /* The retu
14908 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74  rn code */.  int
14909 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b   leafCorrection;
1490a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 34 20 69            /* 4 i
1490b 66 20 70 50 61 67 65 20 69 73 20 61 20 6c 65 61  f pPage is a lea
1490c 66 2e 20 20 30 20 69 66 20 6e 6f 74 20 2a 2f 0a  f.  0 if not */.
1490d 20 20 69 6e 74 20 6c 65 61 66 44 61 74 61 3b 20    int leafData; 
1490e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1490f 2a 20 54 72 75 65 20 69 66 20 70 50 61 67 65 20  * True if pPage 
14910 69 73 20 61 20 6c 65 61 66 20 6f 66 20 61 20 4c  is a leaf of a L
14911 45 41 46 44 41 54 41 20 74 72 65 65 20 2a 2f 0a  EAFDATA tree */.
14912 20 20 69 6e 74 20 75 73 61 62 6c 65 53 70 61 63    int usableSpac
14913 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e;             /
14914 2a 20 42 79 74 65 73 20 69 6e 20 70 50 61 67 65  * Bytes in pPage
14915 20 62 65 79 6f 6e 64 20 74 68 65 20 68 65 61 64   beyond the head
14916 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 61 67 65  er */.  int page
14917 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  Flags;          
14918 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66       /* Value of
14919 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d   pPage->aData[0]
1491a 20 2a 2f 0a 20 20 69 6e 74 20 73 75 62 74 6f 74   */.  int subtot
1491b 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  al;             
1491c 20 20 20 2f 2a 20 53 75 62 74 6f 74 61 6c 20 6f     /* Subtotal o
1491d 66 20 62 79 74 65 73 20 69 6e 20 63 65 6c 6c 73  f bytes in cells
1491e 20 6f 6e 20 6f 6e 65 20 70 61 67 65 20 2a 2f 0a   on one page */.
1491f 20 20 69 6e 74 20 69 53 70 61 63 65 31 20 3d 20    int iSpace1 = 
14920 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0;             /
14921 2a 20 46 69 72 73 74 20 75 6e 75 73 65 64 20 62  * First unused b
14922 79 74 65 20 6f 66 20 61 53 70 61 63 65 31 5b 5d  yte of aSpace1[]
14923 20 2a 2f 0a 20 20 69 6e 74 20 69 53 70 61 63 65   */.  int iSpace
14924 32 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  2 = 0;          
14925 20 20 20 2f 2a 20 46 69 72 73 74 20 75 6e 75 73     /* First unus
14926 65 64 20 62 79 74 65 20 6f 66 20 61 53 70 61 63  ed byte of aSpac
14927 65 32 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 73 7a  e2[] */.  int sz
14928 53 63 72 61 74 63 68 3b 20 20 20 20 20 20 20 20  Scratch;        
14929 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
1492a 66 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79  f scratch memory
1492b 20 72 65 71 75 65 73 74 65 64 20 2a 2f 0a 20 20   requested */.  
1492c 4d 65 6d 50 61 67 65 20 2a 61 70 4f 6c 64 5b 4e  MemPage *apOld[N
1492d 42 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  B];          /* 
1492e 70 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f 20  pPage and up to 
1492f 74 77 6f 20 73 69 62 6c 69 6e 67 73 20 2a 2f 0a  two siblings */.
14930 20 20 50 67 6e 6f 20 70 67 6e 6f 4f 6c 64 5b 4e    Pgno pgnoOld[N
14931 42 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  B];            /
14932 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 73 20 66  * Page numbers f
14933 6f 72 20 65 61 63 68 20 70 61 67 65 20 69 6e 20  or each page in 
14934 61 70 4f 6c 64 5b 5d 20 2a 2f 0a 20 20 4d 65 6d  apOld[] */.  Mem
14935 50 61 67 65 20 2a 61 70 43 6f 70 79 5b 4e 42 5d  Page *apCopy[NB]
14936 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 69  ;         /* Pri
14937 76 61 74 65 20 63 6f 70 69 65 73 20 6f 66 20 61  vate copies of a
14938 70 4f 6c 64 5b 5d 20 70 61 67 65 73 20 2a 2f 0a  pOld[] pages */.
14939 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 4e 65 77    MemPage *apNew
1493a 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 2f  [NB+2];        /
1493b 2a 20 70 50 61 67 65 20 61 6e 64 20 75 70 20 74  * pPage and up t
1493c 6f 20 4e 42 20 73 69 62 6c 69 6e 67 73 20 61 66  o NB siblings af
1493d 74 65 72 20 62 61 6c 61 6e 63 69 6e 67 20 2a 2f  ter balancing */
1493e 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 4e 65 77 5b  .  Pgno pgnoNew[
1493f 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 20 20  NB+2];          
14940 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 73 20  /* Page numbers 
14941 66 6f 72 20 65 61 63 68 20 70 61 67 65 20 69 6e  for each page in
14942 20 61 70 4e 65 77 5b 5d 20 2a 2f 0a 20 20 75 38   apNew[] */.  u8
14943 20 2a 61 70 44 69 76 5b 4e 42 5d 3b 20 20 20 20   *apDiv[NB];    
14944 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69             /* Di
14945 76 69 64 65 72 20 63 65 6c 6c 73 20 69 6e 20 70  vider cells in p
14946 50 61 72 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  Parent */.  int 
14947 63 6e 74 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20  cntNew[NB+2];   
14948 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
14949 78 20 69 6e 20 61 43 65 6c 6c 5b 5d 20 6f 66 20  x in aCell[] of 
1494a 63 65 6c 6c 20 61 66 74 65 72 20 69 2d 74 68 20  cell after i-th 
1494b 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a  page */.  int sz
1494c 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20  New[NB+2];      
1494d 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 62 69 6e         /* Combin
1494e 65 64 20 73 69 7a 65 20 6f 66 20 63 65 6c 6c 73  ed size of cells
1494f 20 70 6c 61 63 65 20 6f 6e 20 69 2d 74 68 20 70   place on i-th p
14950 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 70  age */.  u8 **ap
14951 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20 20  Cell = 0;       
14952 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 65 6c        /* All cel
14953 6c 73 20 62 65 67 69 6e 20 62 61 6c 61 6e 63 65  ls begin balance
14954 64 20 2a 2f 0a 20 20 75 31 36 20 2a 73 7a 43 65  d */.  u16 *szCe
14955 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ll;             
14956 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 73 69 7a      /* Local siz
14957 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20 69  e of all cells i
14958 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20  n apCell[] */.  
14959 75 38 20 2a 61 43 6f 70 79 5b 4e 42 5d 3b 20 20  u8 *aCopy[NB];  
1495a 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20         /* Space 
1495b 66 6f 72 20 68 6f 6c 64 69 6e 67 20 64 61 74 61  for holding data
1495c 20 6f 66 20 61 70 43 6f 70 79 5b 5d 20 2a 2f 0a   of apCopy[] */.
1495d 20 20 75 38 20 2a 61 53 70 61 63 65 31 3b 20 20    u8 *aSpace1;  
1495e 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63           /* Spac
1495f 65 20 66 6f 72 20 63 6f 70 69 65 73 20 6f 66 20  e for copies of 
14960 64 69 76 69 64 65 72 73 20 63 65 6c 6c 73 20 62  dividers cells b
14961 65 66 6f 72 65 20 62 61 6c 61 6e 63 65 20 2a 2f  efore balance */
14962 0a 20 20 75 38 20 2a 61 53 70 61 63 65 32 20 3d  .  u8 *aSpace2 =
14963 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 53 70 61   0;       /* Spa
14964 63 65 20 66 6f 72 20 6f 76 65 72 66 6c 6f 77 20  ce for overflow 
14965 64 69 76 69 64 65 72 73 20 63 65 6c 6c 73 20 61  dividers cells a
14966 66 74 65 72 20 62 61 6c 61 6e 63 65 20 2a 2f 0a  fter balance */.
14967 20 20 75 38 20 2a 61 46 72 6f 6d 20 3d 20 30 3b    u8 *aFrom = 0;
14968 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72  ..  pPage = pCur
14969 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
1496a 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28  Page];.  assert(
1496b 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1496c 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
1496d 6d 75 74 65 78 29 20 29 3b 0a 20 20 56 56 41 5f  mutex) );.  VVA_
1496e 4f 4e 4c 59 28 20 70 43 75 72 2d 3e 70 61 67 65  ONLY( pCur->page
1496f 73 53 68 75 66 66 6c 65 64 20 3d 20 31 20 29 3b  sShuffled = 1 );
14970 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 46 69 6e  ..  /* .  ** Fin
14971 64 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67  d the parent pag
14972 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  e..  */.  assert
14973 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 30 20  ( pCur->iPage>0 
14974 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
14975 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20  ge->isInit );.  
14976 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
14977 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
14978 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20  pPage->pDbPage) 
14979 7c 7c 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  || pPage->nOverf
1497a 6c 6f 77 3d 3d 31 20 29 3b 0a 20 20 70 42 74 20  low==1 );.  pBt 
1497b 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20  = pPage->pBt;.  
1497c 70 50 61 72 65 6e 74 20 3d 20 70 43 75 72 2d 3e  pParent = pCur->
1497d 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
1497e 67 65 2d 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ge-1];.  assert(
1497f 20 70 50 61 72 65 6e 74 20 29 3b 0a 20 20 69 66   pParent );.  if
14980 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  ( SQLITE_OK!=(rc
14981 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
14982 72 69 74 65 28 70 50 61 72 65 6e 74 2d 3e 70 44  rite(pParent->pD
14983 62 50 61 67 65 29 29 20 29 7b 0a 20 20 20 20 72  bPage)) ){.    r
14984 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
14985 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45   TRACE(("BALANCE
14986 3a 20 62 65 67 69 6e 20 70 61 67 65 20 25 64 20  : begin page %d 
14987 63 68 69 6c 64 20 6f 66 20 25 64 5c 6e 22 2c 20  child of %d\n", 
14988 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 50 61  pPage->pgno, pPa
14989 72 65 6e 74 2d 3e 70 67 6e 6f 29 29 3b 0a 0a 23  rent->pgno));..#
1498a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1498b 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 0a  IT_QUICKBALANCE.
1498c 20 20 2f 2a 0a 20 20 2a 2a 20 41 20 73 70 65 63    /*.  ** A spec
1498d 69 61 6c 20 63 61 73 65 3a 20 20 49 66 20 61 20  ial case:  If a 
1498e 6e 65 77 20 65 6e 74 72 79 20 68 61 73 20 6a 75  new entry has ju
1498f 73 74 20 62 65 65 6e 20 69 6e 73 65 72 74 65 64  st been inserted
14990 20 69 6e 74 6f 20 61 0a 20 20 2a 2a 20 74 61 62   into a.  ** tab
14991 6c 65 20 28 74 68 61 74 20 69 73 2c 20 61 20 62  le (that is, a b
14992 74 72 65 65 20 77 69 74 68 20 69 6e 74 65 67 65  tree with intege
14993 72 20 6b 65 79 73 20 61 6e 64 20 61 6c 6c 20 64  r keys and all d
14994 61 74 61 20 61 74 20 74 68 65 20 6c 65 61 76 65  ata at the leave
14995 73 29 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20  s).  ** and the 
14996 6e 65 77 20 65 6e 74 72 79 20 69 73 20 74 68 65  new entry is the
14997 20 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72   right-most entr
14998 79 20 69 6e 20 74 68 65 20 74 72 65 65 20 28 69  y in the tree (i
14999 74 20 68 61 73 20 74 68 65 0a 20 20 2a 2a 20 6c  t has the.  ** l
1499a 61 72 67 65 73 74 20 6b 65 79 29 20 74 68 65 6e  argest key) then
1499b 20 75 73 65 20 74 68 65 20 73 70 65 63 69 61 6c   use the special
1499c 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29   balance_quick()
1499d 20 72 6f 75 74 69 6e 65 20 66 6f 72 0a 20 20 2a   routine for.  *
1499e 2a 20 62 61 6c 61 6e 63 69 6e 67 2e 20 20 62 61  * balancing.  ba
1499f 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 20 69 73  lance_quick() is
149a0 20 6d 75 63 68 20 66 61 73 74 65 72 20 61 6e 64   much faster and
149a1 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 74 69   results in a ti
149a2 67 68 74 65 72 0a 20 20 2a 2a 20 70 61 63 6b 69  ghter.  ** packi
149a3 6e 67 20 6f 66 20 64 61 74 61 20 69 6e 20 74 68  ng of data in th
149a4 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 2e 0a 20  e common case.. 
149a5 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 2d   */.  if( pPage-
149a6 3e 6c 65 61 66 20 26 26 0a 20 20 20 20 20 20 70  >leaf &&.      p
149a7 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 0a  Page->intKey &&.
149a8 20 20 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f 76        pPage->nOv
149a9 65 72 66 6c 6f 77 3d 3d 31 20 26 26 0a 20 20 20  erflow==1 &&.   
149aa 20 20 20 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b     pPage->aOvfl[
149ab 30 5d 2e 69 64 78 3d 3d 70 50 61 67 65 2d 3e 6e  0].idx==pPage->n
149ac 43 65 6c 6c 20 26 26 0a 20 20 20 20 20 20 70 50  Cell &&.      pP
149ad 61 72 65 6e 74 2d 3e 70 67 6e 6f 21 3d 31 20 26  arent->pgno!=1 &
149ae 26 0a 20 20 20 20 20 20 67 65 74 34 62 79 74 65  &.      get4byte
149af 28 26 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 61  (&pParent->aData
149b0 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66  [pParent->hdrOff
149b1 73 65 74 2b 38 5d 29 3d 3d 70 50 61 67 65 2d 3e  set+8])==pPage->
149b2 70 67 6e 6f 0a 20 20 29 7b 0a 20 20 20 20 61 73  pgno.  ){.    as
149b3 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 6e 74  sert( pPage->int
149b4 4b 65 79 20 29 3b 0a 20 20 20 20 2f 2a 0a 20 20  Key );.    /*.  
149b5 20 20 2a 2a 20 54 4f 44 4f 3a 20 43 68 65 63 6b    ** TODO: Check
149b6 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20 74 6f   the siblings to
149b7 20 74 68 65 20 6c 65 66 74 20 6f 66 20 70 50 61   the left of pPa
149b8 67 65 2e 20 49 74 20 6d 61 79 20 62 65 20 74 68  ge. It may be th
149b9 61 74 0a 20 20 20 20 2a 2a 20 74 68 65 79 20 61  at.    ** they a
149ba 72 65 20 6e 6f 74 20 66 75 6c 6c 20 61 6e 64 20  re not full and 
149bb 6e 6f 20 6e 65 77 20 70 61 67 65 20 69 73 20 72  no new page is r
149bc 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a  equired..    */.
149bd 20 20 20 20 72 65 74 75 72 6e 20 62 61 6c 61 6e      return balan
149be 63 65 5f 71 75 69 63 6b 28 70 43 75 72 29 3b 0a  ce_quick(pCur);.
149bf 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66    }.#endif..  if
149c0 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  ( SQLITE_OK!=(rc
149c1 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
149c2 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50  rite(pPage->pDbP
149c3 61 67 65 29 29 20 29 7b 0a 20 20 20 20 72 65 74  age)) ){.    ret
149c4 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f  urn rc;.  }..  /
149c5 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20  *.  ** Find the 
149c6 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 61 72 65  cell in the pare
149c7 6e 74 20 70 61 67 65 20 77 68 6f 73 65 20 6c 65  nt page whose le
149c8 66 74 20 63 68 69 6c 64 20 70 6f 69 6e 74 73 20  ft child points 
149c9 62 61 63 6b 0a 20 20 2a 2a 20 74 6f 20 70 50 61  back.  ** to pPa
149ca 67 65 2e 20 20 54 68 65 20 22 69 64 78 22 20 76  ge.  The "idx" v
149cb 61 72 69 61 62 6c 65 20 69 73 20 74 68 65 20 69  ariable is the i
149cc 6e 64 65 78 20 6f 66 20 74 68 61 74 20 63 65 6c  ndex of that cel
149cd 6c 2e 20 20 49 66 20 70 50 61 67 65 0a 20 20 2a  l.  If pPage.  *
149ce 2a 20 69 73 20 74 68 65 20 72 69 67 68 74 6d 6f  * is the rightmo
149cf 73 74 20 63 68 69 6c 64 20 6f 66 20 70 50 61 72  st child of pPar
149d0 65 6e 74 20 74 68 65 6e 20 73 65 74 20 69 64 78  ent then set idx
149d1 20 74 6f 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65   to pParent->nCe
149d2 6c 6c 20 0a 20 20 2a 2f 0a 20 20 69 64 78 20 3d  ll .  */.  idx =
149d3 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
149d4 72 2d 3e 69 50 61 67 65 2d 31 5d 3b 0a 20 20 61  r->iPage-1];.  a
149d5 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78  ssertParentIndex
149d6 28 70 50 61 72 65 6e 74 2c 20 69 64 78 2c 20 70  (pParent, idx, p
149d7 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20  Page->pgno);..  
149d8 2f 2a 0a 20 20 2a 2a 20 49 6e 69 74 69 61 6c 69  /*.  ** Initiali
149d9 7a 65 20 76 61 72 69 61 62 6c 65 73 20 73 6f 20  ze variables so 
149da 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20  that it will be 
149db 73 61 66 65 20 74 6f 20 6a 75 6d 70 0a 20 20 2a  safe to jump.  *
149dc 2a 20 64 69 72 65 63 74 6c 79 20 74 6f 20 62 61  * directly to ba
149dd 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 20 61 74  lance_cleanup at
149de 20 61 6e 79 20 6d 6f 6d 65 6e 74 2e 0a 20 20 2a   any moment..  *
149df 2f 0a 20 20 6e 4f 6c 64 20 3d 20 6e 4e 65 77 20  /.  nOld = nNew 
149e0 3d 20 30 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  = 0;..  /*.  ** 
149e1 46 69 6e 64 20 73 69 62 6c 69 6e 67 20 70 61 67  Find sibling pag
149e2 65 73 20 74 6f 20 70 50 61 67 65 20 61 6e 64 20  es to pPage and 
149e3 74 68 65 20 63 65 6c 6c 73 20 69 6e 20 70 50 61  the cells in pPa
149e4 72 65 6e 74 20 74 68 61 74 20 64 69 76 69 64 65  rent that divide
149e5 0a 20 20 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e  .  ** the siblin
149e6 67 73 2e 20 20 41 6e 20 61 74 74 65 6d 70 74 20  gs.  An attempt 
149e7 69 73 20 6d 61 64 65 20 74 6f 20 66 69 6e 64 20  is made to find 
149e8 4e 4e 20 73 69 62 6c 69 6e 67 73 20 6f 6e 20 65  NN siblings on e
149e9 69 74 68 65 72 0a 20 20 2a 2a 20 73 69 64 65 20  ither.  ** side 
149ea 6f 66 20 70 50 61 67 65 2e 20 20 4d 6f 72 65 20  of pPage.  More 
149eb 73 69 62 6c 69 6e 67 73 20 61 72 65 20 74 61 6b  siblings are tak
149ec 65 6e 20 66 72 6f 6d 20 6f 6e 65 20 73 69 64 65  en from one side
149ed 2c 20 68 6f 77 65 76 65 72 2c 20 69 66 0a 20 20  , however, if.  
149ee 2a 2a 20 70 50 61 67 65 20 74 68 65 72 65 20 61  ** pPage there a
149ef 72 65 20 66 65 77 65 72 20 74 68 61 6e 20 4e 4e  re fewer than NN
149f0 20 73 69 62 6c 69 6e 67 73 20 6f 6e 20 74 68 65   siblings on the
149f1 20 6f 74 68 65 72 20 73 69 64 65 2e 20 20 49 66   other side.  If
149f2 20 70 50 61 72 65 6e 74 0a 20 20 2a 2a 20 68 61   pParent.  ** ha
149f3 73 20 4e 42 20 6f 72 20 66 65 77 65 72 20 63 68  s NB or fewer ch
149f4 69 6c 64 72 65 6e 20 74 68 65 6e 20 61 6c 6c 20  ildren then all 
149f5 63 68 69 6c 64 72 65 6e 20 6f 66 20 70 50 61 72  children of pPar
149f6 65 6e 74 20 61 72 65 20 74 61 6b 65 6e 2e 0a 20  ent are taken.. 
149f7 20 2a 2f 0a 20 20 6e 78 44 69 76 20 3d 20 69 64   */.  nxDiv = id
149f8 78 20 2d 20 4e 4e 3b 0a 20 20 69 66 28 20 6e 78  x - NN;.  if( nx
149f9 44 69 76 20 2b 20 4e 42 20 3e 20 70 50 61 72 65  Div + NB > pPare
149fa 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20  nt->nCell ){.   
149fb 20 6e 78 44 69 76 20 3d 20 70 50 61 72 65 6e 74   nxDiv = pParent
149fc 2d 3e 6e 43 65 6c 6c 20 2d 20 4e 42 20 2b 20 31  ->nCell - NB + 1
149fd 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 78 44 69  ;.  }.  if( nxDi
149fe 76 3c 30 20 29 7b 0a 20 20 20 20 6e 78 44 69 76  v<0 ){.    nxDiv
149ff 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 6e 44 69 76   = 0;.  }.  nDiv
14a00 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 2c   = 0;.  for(i=0,
14a01 20 6b 3d 6e 78 44 69 76 3b 20 69 3c 4e 42 3b 20   k=nxDiv; i<NB; 
14a02 69 2b 2b 2c 20 6b 2b 2b 29 7b 0a 20 20 20 20 69  i++, k++){.    i
14a03 66 28 20 6b 3c 70 50 61 72 65 6e 74 2d 3e 6e 43  f( k<pParent->nC
14a04 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 61 70 44  ell ){.      apD
14a05 69 76 5b 69 5d 20 3d 20 66 69 6e 64 43 65 6c 6c  iv[i] = findCell
14a06 28 70 50 61 72 65 6e 74 2c 20 6b 29 3b 0a 20 20  (pParent, k);.  
14a07 20 20 20 20 6e 44 69 76 2b 2b 3b 0a 20 20 20 20      nDiv++;.    
14a08 20 20 61 73 73 65 72 74 28 20 21 70 50 61 72 65    assert( !pPare
14a09 6e 74 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 20 20  nt->leaf );.    
14a0a 20 20 70 67 6e 6f 4f 6c 64 5b 69 5d 20 3d 20 67    pgnoOld[i] = g
14a0b 65 74 34 62 79 74 65 28 61 70 44 69 76 5b 69 5d  et4byte(apDiv[i]
14a0c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  );.    }else if(
14a0d 20 6b 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65   k==pParent->nCe
14a0e 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 67 6e 6f  ll ){.      pgno
14a0f 4f 6c 64 5b 69 5d 20 3d 20 67 65 74 34 62 79 74  Old[i] = get4byt
14a10 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 44 61 74  e(&pParent->aDat
14a11 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66  a[pParent->hdrOf
14a12 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 7d 65  fset+8]);.    }e
14a13 6c 73 65 7b 0a 20 20 20 20 20 20 62 72 65 61 6b  lse{.      break
14a14 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
14a15 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28   getAndInitPage(
14a16 70 42 74 2c 20 70 67 6e 6f 4f 6c 64 5b 69 5d 2c  pBt, pgnoOld[i],
14a17 20 26 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 20   &apOld[i]);.   
14a18 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62   if( rc ) goto b
14a19 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a  alance_cleanup;.
14a1a 20 20 20 20 2f 2a 20 61 70 4f 6c 64 5b 69 5d 2d      /* apOld[i]-
14a1b 3e 69 64 78 50 61 72 65 6e 74 20 3d 20 6b 3b 20  >idxParent = k; 
14a1c 2a 2f 0a 20 20 20 20 61 70 43 6f 70 79 5b 69 5d  */.    apCopy[i]
14a1d 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74   = 0;.    assert
14a1e 28 20 69 3d 3d 6e 4f 6c 64 20 29 3b 0a 20 20 20  ( i==nOld );.   
14a1f 20 6e 4f 6c 64 2b 2b 3b 0a 20 20 20 20 6e 4d 61   nOld++;.    nMa
14a20 78 43 65 6c 6c 73 20 2b 3d 20 31 2b 61 70 4f 6c  xCells += 1+apOl
14a21 64 5b 69 5d 2d 3e 6e 43 65 6c 6c 2b 61 70 4f 6c  d[i]->nCell+apOl
14a22 64 5b 69 5d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b  d[i]->nOverflow;
14a23 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20  .  }..  /* Make 
14a24 6e 4d 61 78 43 65 6c 6c 73 20 61 20 6d 75 6c 74  nMaxCells a mult
14a25 69 70 6c 65 20 6f 66 20 34 20 69 6e 20 6f 72 64  iple of 4 in ord
14a26 65 72 20 74 6f 20 70 72 65 73 65 72 76 65 20 38  er to preserve 8
14a27 2d 62 79 74 65 0a 20 20 2a 2a 20 61 6c 69 67 6e  -byte.  ** align
14a28 6d 65 6e 74 20 2a 2f 0a 20 20 6e 4d 61 78 43 65  ment */.  nMaxCe
14a29 6c 6c 73 20 3d 20 28 6e 4d 61 78 43 65 6c 6c 73  lls = (nMaxCells
14a2a 20 2b 20 33 29 26 7e 33 3b 0a 0a 20 20 2f 2a 0a   + 3)&~3;..  /*.
14a2b 20 20 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70    ** Allocate sp
14a2c 61 63 65 20 66 6f 72 20 6d 65 6d 6f 72 79 20 73  ace for memory s
14a2d 74 72 75 63 74 75 72 65 73 0a 20 20 2a 2f 0a 20  tructures.  */. 
14a2e 20 73 7a 53 63 72 61 74 63 68 20 3d 0a 20 20 20   szScratch =.   
14a2f 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73 2a 73 69      nMaxCells*si
14a30 7a 65 6f 66 28 75 38 2a 29 20 20 20 20 20 20 20  zeof(u8*)       
14a31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14a32 2f 2a 20 61 70 43 65 6c 6c 20 2a 2f 0a 20 20 20  /* apCell */.   
14a33 20 20 2b 20 6e 4d 61 78 43 65 6c 6c 73 2a 73 69    + nMaxCells*si
14a34 7a 65 6f 66 28 75 31 36 29 20 20 20 20 20 20 20  zeof(u16)       
14a35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14a36 2f 2a 20 73 7a 43 65 6c 6c 20 2a 2f 0a 20 20 20  /* szCell */.   
14a37 20 20 2b 20 28 52 4f 55 4e 44 38 28 73 69 7a 65    + (ROUND8(size
14a38 6f 66 28 4d 65 6d 50 61 67 65 29 29 2b 70 42 74  of(MemPage))+pBt
14a39 2d 3e 70 61 67 65 53 69 7a 65 29 2a 4e 42 20 20  ->pageSize)*NB  
14a3a 2f 2a 20 61 43 6f 70 79 20 2a 2f 0a 20 20 20 20  /* aCopy */.    
14a3b 20 2b 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65   + pBt->pageSize
14a3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14a3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
14a3e 2a 20 61 53 70 61 63 65 31 20 2a 2f 0a 20 20 20  * aSpace1 */.   
14a3f 20 20 2b 20 28 49 53 41 55 54 4f 56 41 43 55 55    + (ISAUTOVACUU
14a40 4d 20 3f 20 6e 4d 61 78 43 65 6c 6c 73 20 3a 20  M ? nMaxCells : 
14a41 30 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  0);             
14a42 2f 2a 20 61 46 72 6f 6d 20 2a 2f 0a 20 20 61 70  /* aFrom */.  ap
14a43 43 65 6c 6c 20 3d 20 73 71 6c 69 74 65 33 53 63  Cell = sqlite3Sc
14a44 72 61 74 63 68 4d 61 6c 6c 6f 63 28 20 73 7a 53  ratchMalloc( szS
14a45 63 72 61 74 63 68 20 29 3b 20 0a 20 20 69 66 28  cratch ); .  if(
14a46 20 61 70 43 65 6c 6c 3d 3d 30 20 29 7b 0a 20 20   apCell==0 ){.  
14a47 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
14a48 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 62 61  MEM;.    goto ba
14a49 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  lance_cleanup;. 
14a4a 20 7d 0a 20 20 73 7a 43 65 6c 6c 20 3d 20 28 75   }.  szCell = (u
14a4b 31 36 2a 29 26 61 70 43 65 6c 6c 5b 6e 4d 61 78  16*)&apCell[nMax
14a4c 43 65 6c 6c 73 5d 3b 0a 20 20 61 43 6f 70 79 5b  Cells];.  aCopy[
14a4d 30 5d 20 3d 20 28 75 38 2a 29 26 73 7a 43 65 6c  0] = (u8*)&szCel
14a4e 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d 3b 0a 20 20  l[nMaxCells];.  
14a4f 61 73 73 65 72 74 28 20 28 28 61 43 6f 70 79 5b  assert( ((aCopy[
14a50 30 5d 20 2d 20 28 75 38 2a 29 61 70 43 65 6c 6c  0] - (u8*)apCell
14a51 29 20 26 20 37 29 3d 3d 30 20 29 3b 20 2f 2a 20  ) & 7)==0 ); /* 
14a52 38 2d 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74  8-byte alignment
14a53 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 66   required */.  f
14a54 6f 72 28 69 3d 31 3b 20 69 3c 4e 42 3b 20 69 2b  or(i=1; i<NB; i+
14a55 2b 29 7b 0a 20 20 20 20 61 43 6f 70 79 5b 69 5d  +){.    aCopy[i]
14a56 20 3d 20 26 61 43 6f 70 79 5b 69 2d 31 5d 5b 70   = &aCopy[i-1][p
14a57 42 74 2d 3e 70 61 67 65 53 69 7a 65 2b 52 4f 55  Bt->pageSize+ROU
14a58 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65 6d 50 61  ND8(sizeof(MemPa
14a59 67 65 29 29 5d 3b 0a 20 20 20 20 61 73 73 65 72  ge))];.    asser
14a5a 74 28 20 28 28 61 43 6f 70 79 5b 69 5d 20 2d 20  t( ((aCopy[i] - 
14a5b 28 75 38 2a 29 61 70 43 65 6c 6c 29 20 26 20 37  (u8*)apCell) & 7
14a5c 29 3d 3d 30 20 29 3b 20 2f 2a 20 38 2d 62 79 74  )==0 ); /* 8-byt
14a5d 65 20 61 6c 69 67 6e 6d 65 6e 74 20 72 65 71 75  e alignment requ
14a5e 69 72 65 64 20 2a 2f 0a 20 20 7d 0a 20 20 61 53  ired */.  }.  aS
14a5f 70 61 63 65 31 20 3d 20 26 61 43 6f 70 79 5b 4e  pace1 = &aCopy[N
14a60 42 2d 31 5d 5b 70 42 74 2d 3e 70 61 67 65 53 69  B-1][pBt->pageSi
14a61 7a 65 2b 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66  ze+ROUND8(sizeof
14a62 28 4d 65 6d 50 61 67 65 29 29 5d 3b 0a 20 20 61  (MemPage))];.  a
14a63 73 73 65 72 74 28 20 28 28 61 53 70 61 63 65 31  ssert( ((aSpace1
14a64 20 2d 20 28 75 38 2a 29 61 70 43 65 6c 6c 29 20   - (u8*)apCell) 
14a65 26 20 37 29 3d 3d 30 20 29 3b 20 2f 2a 20 38 2d  & 7)==0 ); /* 8-
14a66 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 72  byte alignment r
14a67 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 66 28  equired */.  if(
14a68 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b   ISAUTOVACUUM ){
14a69 0a 20 20 20 20 61 46 72 6f 6d 20 3d 20 26 61 53  .    aFrom = &aS
14a6a 70 61 63 65 31 5b 70 42 74 2d 3e 70 61 67 65 53  pace1[pBt->pageS
14a6b 69 7a 65 5d 3b 0a 20 20 7d 0a 20 20 61 53 70 61  ize];.  }.  aSpa
14a6c 63 65 32 20 3d 20 73 71 6c 69 74 65 33 50 61 67  ce2 = sqlite3Pag
14a6d 65 4d 61 6c 6c 6f 63 28 70 42 74 2d 3e 70 61 67  eMalloc(pBt->pag
14a6e 65 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 61 53  eSize);.  if( aS
14a6f 70 61 63 65 32 3d 3d 30 20 29 7b 0a 20 20 20 20  pace2==0 ){.    
14a70 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
14a71 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 62 61 6c 61  M;.    goto bala
14a72 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  nce_cleanup;.  }
14a73 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 61  .  .  /*.  ** Ma
14a74 6b 65 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65  ke copies of the
14a75 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70 50 61 67   content of pPag
14a76 65 20 61 6e 64 20 69 74 73 20 73 69 62 6c 69 6e  e and its siblin
14a77 67 73 20 69 6e 74 6f 20 61 4f 6c 64 5b 5d 2e 0a  gs into aOld[]..
14a78 20 20 2a 2a 20 54 68 65 20 72 65 73 74 20 6f 66    ** The rest of
14a79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77   this function w
14a7a 69 6c 6c 20 75 73 65 20 64 61 74 61 20 66 72 6f  ill use data fro
14a7b 6d 20 74 68 65 20 63 6f 70 69 65 73 20 72 61 74  m the copies rat
14a7c 68 65 72 0a 20 20 2a 2a 20 74 68 61 74 20 74 68  her.  ** that th
14a7d 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 73  e original pages
14a7e 20 73 69 6e 63 65 20 74 68 65 20 6f 72 69 67 69   since the origi
14a7f 6e 61 6c 20 70 61 67 65 73 20 77 69 6c 6c 20 62  nal pages will b
14a80 65 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 70 72  e in the.  ** pr
14a81 6f 63 65 73 73 20 6f 66 20 62 65 69 6e 67 20 6f  ocess of being o
14a82 76 65 72 77 72 69 74 74 65 6e 2e 0a 20 20 2a 2f  verwritten..  */
14a83 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f  .  for(i=0; i<nO
14a84 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4d 65  ld; i++){.    Me
14a85 6d 50 61 67 65 20 2a 70 20 3d 20 61 70 43 6f 70  mPage *p = apCop
14a86 79 5b 69 5d 20 3d 20 28 4d 65 6d 50 61 67 65 2a  y[i] = (MemPage*
14a87 29 61 43 6f 70 79 5b 69 5d 3b 0a 20 20 20 20 6d  )aCopy[i];.    m
14a88 65 6d 63 70 79 28 70 2c 20 61 70 4f 6c 64 5b 69  emcpy(p, apOld[i
14a89 5d 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67  ], sizeof(MemPag
14a8a 65 29 29 3b 0a 20 20 20 20 70 2d 3e 61 44 61 74  e));.    p->aDat
14a8b 61 20 3d 20 28 76 6f 69 64 2a 29 26 70 5b 31 5d  a = (void*)&p[1]
14a8c 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 2d 3e  ;.    memcpy(p->
14a8d 61 44 61 74 61 2c 20 61 70 4f 6c 64 5b 69 5d 2d  aData, apOld[i]-
14a8e 3e 61 44 61 74 61 2c 20 70 42 74 2d 3e 70 61 67  >aData, pBt->pag
14a8f 65 53 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f  eSize);.  }..  /
14a90 2a 0a 20 20 2a 2a 20 4c 6f 61 64 20 70 6f 69 6e  *.  ** Load poin
14a91 74 65 72 73 20 74 6f 20 61 6c 6c 20 63 65 6c 6c  ters to all cell
14a92 73 20 6f 6e 20 73 69 62 6c 69 6e 67 20 70 61 67  s on sibling pag
14a93 65 73 20 61 6e 64 20 74 68 65 20 64 69 76 69 64  es and the divid
14a94 65 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e  er cells.  ** in
14a95 74 6f 20 74 68 65 20 6c 6f 63 61 6c 20 61 70 43  to the local apC
14a96 65 6c 6c 5b 5d 20 61 72 72 61 79 2e 20 20 4d 61  ell[] array.  Ma
14a97 6b 65 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65  ke copies of the
14a98 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 0a 20   divider cells. 
14a99 20 2a 2a 20 69 6e 74 6f 20 73 70 61 63 65 20 6f   ** into space o
14a9a 62 74 61 69 6e 65 64 20 66 6f 72 6d 20 61 53 70  btained form aSp
14a9b 61 63 65 31 5b 5d 20 61 6e 64 20 72 65 6d 6f 76  ace1[] and remov
14a9c 65 20 74 68 65 20 74 68 65 20 64 69 76 69 64 65  e the the divide
14a9d 72 20 43 65 6c 6c 73 0a 20 20 2a 2a 20 66 72 6f  r Cells.  ** fro
14a9e 6d 20 70 50 61 72 65 6e 74 2e 0a 20 20 2a 2a 0a  m pParent..  **.
14a9f 20 20 2a 2a 20 49 66 20 74 68 65 20 73 69 62 6c    ** If the sibl
14aa0 69 6e 67 73 20 61 72 65 20 6f 6e 20 6c 65 61 66  ings are on leaf
14aa1 20 70 61 67 65 73 2c 20 74 68 65 6e 20 74 68 65   pages, then the
14aa2 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 20   child pointers 
14aa3 6f 66 20 74 68 65 0a 20 20 2a 2a 20 64 69 76 69  of the.  ** divi
14aa4 64 65 72 20 63 65 6c 6c 73 20 61 72 65 20 73 74  der cells are st
14aa5 72 69 70 70 65 64 20 66 72 6f 6d 20 74 68 65 20  ripped from the 
14aa6 63 65 6c 6c 73 20 62 65 66 6f 72 65 20 74 68 65  cells before the
14aa7 79 20 61 72 65 20 63 6f 70 69 65 64 0a 20 20 2a  y are copied.  *
14aa8 2a 20 69 6e 74 6f 20 61 53 70 61 63 65 31 5b 5d  * into aSpace1[]
14aa9 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20  .  In this way, 
14aaa 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 43  all cells in apC
14aab 65 6c 6c 5b 5d 20 61 72 65 20 77 69 74 68 6f 75  ell[] are withou
14aac 74 0a 20 20 2a 2a 20 63 68 69 6c 64 20 70 6f 69  t.  ** child poi
14aad 6e 74 65 72 73 2e 20 20 49 66 20 73 69 62 6c 69  nters.  If sibli
14aae 6e 67 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 76  ngs are not leav
14aaf 65 73 2c 20 74 68 65 6e 20 61 6c 6c 20 63 65 6c  es, then all cel
14ab0 6c 20 69 6e 0a 20 20 2a 2a 20 61 70 43 65 6c 6c  l in.  ** apCell
14ab1 5b 5d 20 69 6e 63 6c 75 64 65 20 63 68 69 6c 64  [] include child
14ab2 20 70 6f 69 6e 74 65 72 73 2e 20 20 45 69 74 68   pointers.  Eith
14ab3 65 72 20 77 61 79 2c 20 61 6c 6c 20 63 65 6c 6c  er way, all cell
14ab4 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 0a 20 20  s in apCell[].  
14ab5 2a 2a 20 61 72 65 20 61 6c 69 6b 65 2e 0a 20 20  ** are alike..  
14ab6 2a 2a 0a 20 20 2a 2a 20 6c 65 61 66 43 6f 72 72  **.  ** leafCorr
14ab7 65 63 74 69 6f 6e 3a 20 20 34 20 69 66 20 70 50  ection:  4 if pP
14ab8 61 67 65 20 69 73 20 61 20 6c 65 61 66 2e 20 20  age is a leaf.  
14ab9 30 20 69 66 20 70 50 61 67 65 20 69 73 20 6e 6f  0 if pPage is no
14aba 74 20 61 20 6c 65 61 66 2e 0a 20 20 2a 2a 20 20  t a leaf..  **  
14abb 20 20 20 20 20 6c 65 61 66 44 61 74 61 3a 20 20       leafData:  
14abc 31 20 69 66 20 70 50 61 67 65 20 68 6f 6c 64 73  1 if pPage holds
14abd 20 6b 65 79 2b 64 61 74 61 20 61 6e 64 20 70 50   key+data and pP
14abe 61 72 65 6e 74 20 68 6f 6c 64 73 20 6f 6e 6c 79  arent holds only
14abf 20 6b 65 79 73 2e 0a 20 20 2a 2f 0a 20 20 6e 43   keys..  */.  nC
14ac0 65 6c 6c 20 3d 20 30 3b 0a 20 20 6c 65 61 66 43  ell = 0;.  leafC
14ac1 6f 72 72 65 63 74 69 6f 6e 20 3d 20 70 50 61 67  orrection = pPag
14ac2 65 2d 3e 6c 65 61 66 2a 34 3b 0a 20 20 6c 65 61  e->leaf*4;.  lea
14ac3 66 44 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 68  fData = pPage->h
14ac4 61 73 44 61 74 61 3b 0a 20 20 66 6f 72 28 69 3d  asData;.  for(i=
14ac5 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b  0; i<nOld; i++){
14ac6 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f  .    MemPage *pO
14ac7 6c 64 20 3d 20 61 70 43 6f 70 79 5b 69 5d 3b 0a  ld = apCopy[i];.
14ac8 20 20 20 20 69 6e 74 20 6c 69 6d 69 74 20 3d 20      int limit = 
14ac9 70 4f 6c 64 2d 3e 6e 43 65 6c 6c 2b 70 4f 6c 64  pOld->nCell+pOld
14aca 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20  ->nOverflow;.   
14acb 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6c 69 6d 69   for(j=0; j<limi
14acc 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 61  t; j++){.      a
14acd 73 73 65 72 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61  ssert( nCell<nMa
14ace 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20  xCells );.      
14acf 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20  apCell[nCell] = 
14ad0 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c  findOverflowCell
14ad1 28 70 4f 6c 64 2c 20 6a 29 3b 0a 20 20 20 20 20  (pOld, j);.     
14ad2 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d   szCell[nCell] =
14ad3 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4f 6c   cellSizePtr(pOl
14ad4 64 2c 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d  d, apCell[nCell]
14ad5 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53 41  );.      if( ISA
14ad6 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20  UTOVACUUM ){.   
14ad7 20 20 20 20 20 69 6e 74 20 61 3b 0a 20 20 20 20       int a;.    
14ad8 20 20 20 20 61 46 72 6f 6d 5b 6e 43 65 6c 6c 5d      aFrom[nCell]
14ad9 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20 66 6f   = i;.        fo
14ada 72 28 61 3d 30 3b 20 61 3c 70 4f 6c 64 2d 3e 6e  r(a=0; a<pOld->n
14adb 4f 76 65 72 66 6c 6f 77 3b 20 61 2b 2b 29 7b 0a  Overflow; a++){.
14adc 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 4f            if( pO
14add 6c 64 2d 3e 61 4f 76 66 6c 5b 61 5d 2e 70 43 65  ld->aOvfl[a].pCe
14ade 6c 6c 3d 3d 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c  ll==apCell[nCell
14adf 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ] ){.           
14ae0 20 61 46 72 6f 6d 5b 6e 43 65 6c 6c 5d 20 3d 20   aFrom[nCell] = 
14ae1 30 78 46 46 3b 0a 20 20 20 20 20 20 20 20 20 20  0xFF;.          
14ae2 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
14ae3 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
14ae4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e 43 65       }.      nCe
14ae5 6c 6c 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ll++;.    }.    
14ae6 69 66 28 20 69 3c 6e 4f 6c 64 2d 31 20 29 7b 0a  if( i<nOld-1 ){.
14ae7 20 20 20 20 20 20 75 31 36 20 73 7a 20 3d 20 63        u16 sz = c
14ae8 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 72 65  ellSizePtr(pPare
14ae9 6e 74 2c 20 61 70 44 69 76 5b 69 5d 29 3b 0a 20  nt, apDiv[i]);. 
14aea 20 20 20 20 20 69 66 28 20 6c 65 61 66 44 61 74       if( leafDat
14aeb 61 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  a ){.        /* 
14aec 57 69 74 68 20 74 68 65 20 4c 45 41 46 44 41 54  With the LEAFDAT
14aed 41 20 66 6c 61 67 2c 20 70 50 61 72 65 6e 74 20  A flag, pParent 
14aee 63 65 6c 6c 73 20 68 6f 6c 64 20 6f 6e 6c 79 20  cells hold only 
14aef 49 4e 54 4b 45 59 73 20 74 68 61 74 0a 20 20 20  INTKEYs that.   
14af0 20 20 20 20 20 2a 2a 20 61 72 65 20 64 75 70 6c       ** are dupl
14af1 69 63 61 74 65 73 20 6f 66 20 6b 65 79 73 20 6f  icates of keys o
14af2 6e 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65  n the child page
14af3 73 2e 20 20 57 65 20 6e 65 65 64 20 74 6f 20 72  s.  We need to r
14af4 65 6d 6f 76 65 0a 20 20 20 20 20 20 20 20 2a 2a  emove.        **
14af5 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c   the divider cel
14af6 6c 73 20 66 72 6f 6d 20 70 50 61 72 65 6e 74 2c  ls from pParent,
14af7 20 62 75 74 20 74 68 65 20 64 69 76 69 64 65 72   but the divider
14af8 73 20 63 65 6c 6c 73 20 61 72 65 20 6e 6f 74 0a  s cells are not.
14af9 20 20 20 20 20 20 20 20 2a 2a 20 61 64 64 65 64          ** added
14afa 20 74 6f 20 61 70 43 65 6c 6c 5b 5d 20 62 65 63   to apCell[] bec
14afb 61 75 73 65 20 74 68 65 79 20 61 72 65 20 64 75  ause they are du
14afc 70 6c 69 63 61 74 65 73 20 6f 66 20 63 68 69 6c  plicates of chil
14afd 64 20 63 65 6c 6c 73 2e 0a 20 20 20 20 20 20 20  d cells..       
14afe 20 2a 2f 0a 20 20 20 20 20 20 20 20 64 72 6f 70   */.        drop
14aff 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78  Cell(pParent, nx
14b00 44 69 76 2c 20 73 7a 29 3b 0a 20 20 20 20 20 20  Div, sz);.      
14b01 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 75  }else{.        u
14b02 38 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20 20 20  8 *pTemp;.      
14b03 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3c    assert( nCell<
14b04 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20  nMaxCells );.   
14b05 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c       szCell[nCel
14b06 6c 5d 20 3d 20 73 7a 3b 0a 20 20 20 20 20 20 20  l] = sz;.       
14b07 20 70 54 65 6d 70 20 3d 20 26 61 53 70 61 63 65   pTemp = &aSpace
14b08 31 5b 69 53 70 61 63 65 31 5d 3b 0a 20 20 20 20  1[iSpace1];.    
14b09 20 20 20 20 69 53 70 61 63 65 31 20 2b 3d 20 73      iSpace1 += s
14b0a 7a 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  z;.        asser
14b0b 74 28 20 73 7a 3c 3d 70 42 74 2d 3e 70 61 67 65  t( sz<=pBt->page
14b0c 53 69 7a 65 2f 34 20 29 3b 0a 20 20 20 20 20 20  Size/4 );.      
14b0d 20 20 61 73 73 65 72 74 28 20 69 53 70 61 63 65    assert( iSpace
14b0e 31 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  1<=pBt->pageSize
14b0f 20 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63   );.        memc
14b10 70 79 28 70 54 65 6d 70 2c 20 61 70 44 69 76 5b  py(pTemp, apDiv[
14b11 69 5d 2c 20 73 7a 29 3b 0a 20 20 20 20 20 20 20  i], sz);.       
14b12 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d   apCell[nCell] =
14b13 20 70 54 65 6d 70 2b 6c 65 61 66 43 6f 72 72 65   pTemp+leafCorre
14b14 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20 69  ction;.        i
14b15 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20  f( ISAUTOVACUUM 
14b16 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 46 72  ){.          aFr
14b17 6f 6d 5b 6e 43 65 6c 6c 5d 20 3d 20 30 78 46 46  om[nCell] = 0xFF
14b18 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
14b19 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 50 61      dropCell(pPa
14b1a 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20 73 7a 29  rent, nxDiv, sz)
14b1b 3b 0a 20 20 20 20 20 20 20 20 73 7a 43 65 6c 6c  ;.        szCell
14b1c 5b 6e 43 65 6c 6c 5d 20 2d 3d 20 6c 65 61 66 43  [nCell] -= leafC
14b1d 6f 72 72 65 63 74 69 6f 6e 3b 0a 20 20 20 20 20  orrection;.     
14b1e 20 20 20 61 73 73 65 72 74 28 20 67 65 74 34 62     assert( get4b
14b1f 79 74 65 28 70 54 65 6d 70 29 3d 3d 70 67 6e 6f  yte(pTemp)==pgno
14b20 4f 6c 64 5b 69 5d 20 29 3b 0a 20 20 20 20 20 20  Old[i] );.      
14b21 20 20 69 66 28 20 21 70 4f 6c 64 2d 3e 6c 65 61    if( !pOld->lea
14b22 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  f ){.          a
14b23 73 73 65 72 74 28 20 6c 65 61 66 43 6f 72 72 65  ssert( leafCorre
14b24 63 74 69 6f 6e 3d 3d 30 20 29 3b 0a 20 20 20 20  ction==0 );.    
14b25 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67        /* The rig
14b26 68 74 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68  ht pointer of th
14b27 65 20 63 68 69 6c 64 20 70 61 67 65 20 70 4f 6c  e child page pOl
14b28 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20 6c 65  d becomes the le
14b29 66 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  ft.          ** 
14b2a 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 64  pointer of the d
14b2b 69 76 69 64 65 72 20 63 65 6c 6c 20 2a 2f 0a 20  ivider cell */. 
14b2c 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28           memcpy(
14b2d 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c 20 26  apCell[nCell], &
14b2e 70 4f 6c 64 2d 3e 61 44 61 74 61 5b 70 4f 6c 64  pOld->aData[pOld
14b2f 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20  ->hdrOffset+8], 
14b30 34 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  4);.        }els
14b31 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  e{.          ass
14b32 65 72 74 28 20 6c 65 61 66 43 6f 72 72 65 63 74  ert( leafCorrect
14b33 69 6f 6e 3d 3d 34 20 29 3b 0a 20 20 20 20 20 20  ion==4 );.      
14b34 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b 6e      if( szCell[n
14b35 43 65 6c 6c 5d 3c 34 20 29 7b 0a 20 20 20 20 20  Cell]<4 ){.     
14b36 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74         /* Do not
14b37 20 61 6c 6c 6f 77 20 61 6e 79 20 63 65 6c 6c 73   allow any cells
14b38 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 34 20   smaller than 4 
14b39 62 79 74 65 73 2e 20 2a 2f 0a 20 20 20 20 20 20  bytes. */.      
14b3a 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65        szCell[nCe
14b3b 6c 6c 5d 20 3d 20 34 3b 0a 20 20 20 20 20 20 20  ll] = 4;.       
14b3c 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
14b3d 20 20 20 20 20 20 20 6e 43 65 6c 6c 2b 2b 3b 0a         nCell++;.
14b3e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
14b3f 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 67  }..  /*.  ** Fig
14b40 75 72 65 20 6f 75 74 20 74 68 65 20 6e 75 6d 62  ure out the numb
14b41 65 72 20 6f 66 20 70 61 67 65 73 20 6e 65 65 64  er of pages need
14b42 65 64 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 6e  ed to hold all n
14b43 43 65 6c 6c 20 63 65 6c 6c 73 2e 0a 20 20 2a 2a  Cell cells..  **
14b44 20 53 74 6f 72 65 20 74 68 69 73 20 6e 75 6d 62   Store this numb
14b45 65 72 20 69 6e 20 22 6b 22 2e 20 20 41 6c 73 6f  er in "k".  Also
14b46 20 63 6f 6d 70 75 74 65 20 73 7a 4e 65 77 5b 5d   compute szNew[]
14b47 20 77 68 69 63 68 20 69 73 20 74 68 65 20 74 6f   which is the to
14b48 74 61 6c 0a 20 20 2a 2a 20 73 69 7a 65 20 6f 66  tal.  ** size of
14b49 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20 74 68   all cells on th
14b4a 65 20 69 2d 74 68 20 70 61 67 65 20 61 6e 64 20  e i-th page and 
14b4b 63 6e 74 4e 65 77 5b 5d 20 77 68 69 63 68 20 69  cntNew[] which i
14b4c 73 20 74 68 65 20 69 6e 64 65 78 0a 20 20 2a 2a  s the index.  **
14b4d 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 6f 66 20   in apCell[] of 
14b4e 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20 64 69  the cell that di
14b4f 76 69 64 65 73 20 70 61 67 65 20 69 20 66 72 6f  vides page i fro
14b50 6d 20 70 61 67 65 20 69 2b 31 2e 20 20 0a 20 20  m page i+1.  .  
14b51 2a 2a 20 63 6e 74 4e 65 77 5b 6b 5d 20 73 68 6f  ** cntNew[k] sho
14b52 75 6c 64 20 65 71 75 61 6c 20 6e 43 65 6c 6c 2e  uld equal nCell.
14b53 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 6c 75 65  .  **.  ** Value
14b54 73 20 63 6f 6d 70 75 74 65 64 20 62 79 20 74 68  s computed by th
14b55 69 73 20 62 6c 6f 63 6b 3a 0a 20 20 2a 2a 0a 20  is block:.  **. 
14b56 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6b 3a   **           k:
14b57 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65   The total numbe
14b58 72 20 6f 66 20 73 69 62 6c 69 6e 67 20 70 61 67  r of sibling pag
14b59 65 73 0a 20 20 2a 2a 20 20 20 20 73 7a 4e 65 77  es.  **    szNew
14b5a 5b 69 5d 3a 20 53 70 61 63 65 64 20 75 73 65 64  [i]: Spaced used
14b5b 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 73 69 62   on the i-th sib
14b5c 6c 69 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a 20  ling page..  ** 
14b5d 20 20 63 6e 74 4e 65 77 5b 69 5d 3a 20 49 6e 64    cntNew[i]: Ind
14b5e 65 78 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61  ex in apCell[] a
14b5f 6e 64 20 73 7a 43 65 6c 6c 5b 5d 20 66 6f 72 20  nd szCell[] for 
14b60 74 68 65 20 66 69 72 73 74 20 63 65 6c 6c 20 74  the first cell t
14b61 6f 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20  o.  **          
14b62 20 20 20 20 74 68 65 20 72 69 67 68 74 20 6f 66      the right of
14b63 20 74 68 65 20 69 2d 74 68 20 73 69 62 6c 69 6e   the i-th siblin
14b64 67 20 70 61 67 65 2e 0a 20 20 2a 2a 20 75 73 61  g page..  ** usa
14b65 62 6c 65 53 70 61 63 65 3a 20 4e 75 6d 62 65 72  bleSpace: Number
14b66 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 61   of bytes of spa
14b67 63 65 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20  ce available on 
14b68 65 61 63 68 20 73 69 62 6c 69 6e 67 2e 0a 20 20  each sibling..  
14b69 2a 2a 20 0a 20 20 2a 2f 0a 20 20 75 73 61 62 6c  ** .  */.  usabl
14b6a 65 53 70 61 63 65 20 3d 20 70 42 74 2d 3e 75 73  eSpace = pBt->us
14b6b 61 62 6c 65 53 69 7a 65 20 2d 20 31 32 20 2b 20  ableSize - 12 + 
14b6c 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a  leafCorrection;.
14b6d 20 20 66 6f 72 28 73 75 62 74 6f 74 61 6c 3d 6b    for(subtotal=k
14b6e 3d 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69  =i=0; i<nCell; i
14b6f 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  ++){.    assert(
14b70 20 69 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a   i<nMaxCells );.
14b71 20 20 20 20 73 75 62 74 6f 74 61 6c 20 2b 3d 20      subtotal += 
14b72 73 7a 43 65 6c 6c 5b 69 5d 20 2b 20 32 3b 0a 20  szCell[i] + 2;. 
14b73 20 20 20 69 66 28 20 73 75 62 74 6f 74 61 6c 20     if( subtotal 
14b74 3e 20 75 73 61 62 6c 65 53 70 61 63 65 20 29 7b  > usableSpace ){
14b75 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b 6b 5d 20  .      szNew[k] 
14b76 3d 20 73 75 62 74 6f 74 61 6c 20 2d 20 73 7a 43  = subtotal - szC
14b77 65 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 63 6e  ell[i];.      cn
14b78 74 4e 65 77 5b 6b 5d 20 3d 20 69 3b 0a 20 20 20  tNew[k] = i;.   
14b79 20 20 20 69 66 28 20 6c 65 61 66 44 61 74 61 20     if( leafData 
14b7a 29 7b 20 69 2d 2d 3b 20 7d 0a 20 20 20 20 20 20  ){ i--; }.      
14b7b 73 75 62 74 6f 74 61 6c 20 3d 20 30 3b 0a 20 20  subtotal = 0;.  
14b7c 20 20 20 20 6b 2b 2b 3b 0a 20 20 20 20 7d 0a 20      k++;.    }. 
14b7d 20 7d 0a 20 20 73 7a 4e 65 77 5b 6b 5d 20 3d 20   }.  szNew[k] = 
14b7e 73 75 62 74 6f 74 61 6c 3b 0a 20 20 63 6e 74 4e  subtotal;.  cntN
14b7f 65 77 5b 6b 5d 20 3d 20 6e 43 65 6c 6c 3b 0a 20  ew[k] = nCell;. 
14b80 20 6b 2b 2b 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a   k++;..  /*.  **
14b81 20 54 68 65 20 70 61 63 6b 69 6e 67 20 63 6f 6d   The packing com
14b82 70 75 74 65 64 20 62 79 20 74 68 65 20 70 72 65  puted by the pre
14b83 76 69 6f 75 73 20 62 6c 6f 63 6b 20 69 73 20 62  vious block is b
14b84 69 61 73 65 64 20 74 6f 77 61 72 64 20 74 68 65  iased toward the
14b85 20 73 69 62 6c 69 6e 67 73 0a 20 20 2a 2a 20 6f   siblings.  ** o
14b86 6e 20 74 68 65 20 6c 65 66 74 20 73 69 64 65 2e  n the left side.
14b87 20 20 54 68 65 20 6c 65 66 74 20 73 69 62 6c 69    The left sibli
14b88 6e 67 73 20 61 72 65 20 61 6c 77 61 79 73 20 6e  ngs are always n
14b89 65 61 72 6c 79 20 66 75 6c 6c 2c 20 77 68 69 6c  early full, whil
14b8a 65 20 74 68 65 0a 20 20 2a 2a 20 72 69 67 68 74  e the.  ** right
14b8b 2d 6d 6f 73 74 20 73 69 62 6c 69 6e 67 20 6d 69  -most sibling mi
14b8c 67 68 74 20 62 65 20 6e 65 61 72 6c 79 20 65 6d  ght be nearly em
14b8d 70 74 79 2e 20 20 54 68 69 73 20 62 6c 6f 63 6b  pty.  This block
14b8e 20 6f 66 20 63 6f 64 65 20 61 74 74 65 6d 70 74   of code attempt
14b8f 73 0a 20 20 2a 2a 20 74 6f 20 61 64 6a 75 73 74  s.  ** to adjust
14b90 20 74 68 65 20 70 61 63 6b 69 6e 67 20 6f 66 20   the packing of 
14b91 73 69 62 6c 69 6e 67 73 20 74 6f 20 67 65 74 20  siblings to get 
14b92 61 20 62 65 74 74 65 72 20 62 61 6c 61 6e 63 65  a better balance
14b93 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73  ..  **.  ** This
14b94 20 61 64 6a 75 73 74 6d 65 6e 74 20 69 73 20 6d   adjustment is m
14b95 6f 72 65 20 74 68 61 6e 20 61 6e 20 6f 70 74 69  ore than an opti
14b96 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20 70  mization.  The p
14b97 61 63 6b 69 6e 67 20 61 62 6f 76 65 20 6d 69 67  acking above mig
14b98 68 74 0a 20 20 2a 2a 20 62 65 20 73 6f 20 6f 75  ht.  ** be so ou
14b99 74 20 6f 66 20 62 61 6c 61 6e 63 65 20 61 73 20  t of balance as 
14b9a 74 6f 20 62 65 20 69 6c 6c 65 67 61 6c 2e 20 20  to be illegal.  
14b9b 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65  For example, the
14b9c 20 72 69 67 68 74 2d 6d 6f 73 74 0a 20 20 2a 2a   right-most.  **
14b9d 20 73 69 62 6c 69 6e 67 20 6d 69 67 68 74 20 62   sibling might b
14b9e 65 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 6d 70  e completely emp
14b9f 74 79 2e 20 20 54 68 69 73 20 61 64 6a 75 73 74  ty.  This adjust
14ba0 6d 65 6e 74 20 69 73 20 6e 6f 74 20 6f 70 74 69  ment is not opti
14ba1 6f 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  onal..  */.  for
14ba2 28 69 3d 6b 2d 31 3b 20 69 3e 30 3b 20 69 2d 2d  (i=k-1; i>0; i--
14ba3 29 7b 0a 20 20 20 20 69 6e 74 20 73 7a 52 69 67  ){.    int szRig
14ba4 68 74 20 3d 20 73 7a 4e 65 77 5b 69 5d 3b 20 20  ht = szNew[i];  
14ba5 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69 62 6c 69  /* Size of sibli
14ba6 6e 67 20 6f 6e 20 74 68 65 20 72 69 67 68 74 20  ng on the right 
14ba7 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 4c 65 66  */.    int szLef
14ba8 74 20 3d 20 73 7a 4e 65 77 5b 69 2d 31 5d 3b 20  t = szNew[i-1]; 
14ba9 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69 62 6c 69  /* Size of sibli
14baa 6e 67 20 6f 6e 20 74 68 65 20 6c 65 66 74 20 2a  ng on the left *
14bab 2f 0a 20 20 20 20 69 6e 74 20 72 3b 20 20 20 20  /.    int r;    
14bac 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
14bad 65 78 20 6f 66 20 72 69 67 68 74 2d 6d 6f 73 74  ex of right-most
14bae 20 63 65 6c 6c 20 69 6e 20 6c 65 66 74 20 73 69   cell in left si
14baf 62 6c 69 6e 67 20 2a 2f 0a 20 20 20 20 69 6e 74  bling */.    int
14bb0 20 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   d;             
14bb1 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 69 72   /* Index of fir
14bb2 73 74 20 63 65 6c 6c 20 74 6f 20 74 68 65 20 6c  st cell to the l
14bb3 65 66 74 20 6f 66 20 72 69 67 68 74 20 73 69 62  eft of right sib
14bb4 6c 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 72 20 3d  ling */..    r =
14bb5 20 63 6e 74 4e 65 77 5b 69 2d 31 5d 20 2d 20 31   cntNew[i-1] - 1
14bb6 3b 0a 20 20 20 20 64 20 3d 20 72 20 2b 20 31 20  ;.    d = r + 1 
14bb7 2d 20 6c 65 61 66 44 61 74 61 3b 0a 20 20 20 20  - leafData;.    
14bb8 61 73 73 65 72 74 28 20 64 3c 6e 4d 61 78 43 65  assert( d<nMaxCe
14bb9 6c 6c 73 20 29 3b 0a 20 20 20 20 61 73 73 65 72  lls );.    asser
14bba 74 28 20 72 3c 6e 4d 61 78 43 65 6c 6c 73 20 29  t( r<nMaxCells )
14bbb 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73 7a 52  ;.    while( szR
14bbc 69 67 68 74 3d 3d 30 20 7c 7c 20 73 7a 52 69 67  ight==0 || szRig
14bbd 68 74 2b 73 7a 43 65 6c 6c 5b 64 5d 2b 32 3c 3d  ht+szCell[d]+2<=
14bbe 73 7a 4c 65 66 74 2d 28 73 7a 43 65 6c 6c 5b 72  szLeft-(szCell[r
14bbf 5d 2b 32 29 20 29 7b 0a 20 20 20 20 20 20 73 7a  ]+2) ){.      sz
14bc0 52 69 67 68 74 20 2b 3d 20 73 7a 43 65 6c 6c 5b  Right += szCell[
14bc1 64 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20 73 7a  d] + 2;.      sz
14bc2 4c 65 66 74 20 2d 3d 20 73 7a 43 65 6c 6c 5b 72  Left -= szCell[r
14bc3 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20 63 6e 74  ] + 2;.      cnt
14bc4 4e 65 77 5b 69 2d 31 5d 2d 2d 3b 0a 20 20 20 20  New[i-1]--;.    
14bc5 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b 69 2d 31    r = cntNew[i-1
14bc6 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20 64 20 3d  ] - 1;.      d =
14bc7 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 44 61 74   r + 1 - leafDat
14bc8 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 7a 4e  a;.    }.    szN
14bc9 65 77 5b 69 5d 20 3d 20 73 7a 52 69 67 68 74 3b  ew[i] = szRight;
14bca 0a 20 20 20 20 73 7a 4e 65 77 5b 69 2d 31 5d 20  .    szNew[i-1] 
14bcb 3d 20 73 7a 4c 65 66 74 3b 0a 20 20 7d 0a 0a 20  = szLeft;.  }.. 
14bcc 20 2f 2a 20 45 69 74 68 65 72 20 77 65 20 66 6f   /* Either we fo
14bcd 75 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  und one or more 
14bce 63 65 6c 6c 73 20 28 63 6e 74 6e 65 77 5b 30 5d  cells (cntnew[0]
14bcf 29 3e 30 29 20 6f 72 20 77 65 20 61 72 65 20 74  )>0) or we are t
14bd0 68 65 0a 20 20 2a 2a 20 61 20 76 69 72 74 75 61  he.  ** a virtua
14bd1 6c 20 72 6f 6f 74 20 70 61 67 65 2e 20 20 41 20  l root page.  A 
14bd2 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67  virtual root pag
14bd3 65 20 69 73 20 77 68 65 6e 20 74 68 65 20 72 65  e is when the re
14bd4 61 6c 20 72 6f 6f 74 0a 20 20 2a 2a 20 70 61 67  al root.  ** pag
14bd5 65 20 69 73 20 70 61 67 65 20 31 20 61 6e 64 20  e is page 1 and 
14bd6 77 65 20 61 72 65 20 74 68 65 20 6f 6e 6c 79 20  we are the only 
14bd7 63 68 69 6c 64 20 6f 66 20 74 68 61 74 20 70 61  child of that pa
14bd8 67 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  ge..  */.  asser
14bd9 74 28 20 63 6e 74 4e 65 77 5b 30 5d 3e 30 20 7c  t( cntNew[0]>0 |
14bda 7c 20 28 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f  | (pParent->pgno
14bdb 3d 3d 31 20 26 26 20 70 50 61 72 65 6e 74 2d 3e  ==1 && pParent->
14bdc 6e 43 65 6c 6c 3d 3d 30 29 20 29 3b 0a 0a 20 20  nCell==0) );..  
14bdd 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 74 65  /*.  ** Allocate
14bde 20 6b 20 6e 65 77 20 70 61 67 65 73 2e 20 20 52   k new pages.  R
14bdf 65 75 73 65 20 6f 6c 64 20 70 61 67 65 73 20 77  euse old pages w
14be0 68 65 72 65 20 70 6f 73 73 69 62 6c 65 2e 0a 20  here possible.. 
14be1 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50   */.  assert( pP
14be2 61 67 65 2d 3e 70 67 6e 6f 3e 31 20 29 3b 0a 20  age->pgno>1 );. 
14be3 20 70 61 67 65 46 6c 61 67 73 20 3d 20 70 50 61   pageFlags = pPa
14be4 67 65 2d 3e 61 44 61 74 61 5b 30 5d 3b 0a 20 20  ge->aData[0];.  
14be5 66 6f 72 28 69 3d 30 3b 20 69 3c 6b 3b 20 69 2b  for(i=0; i<k; i+
14be6 2b 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20  +){.    MemPage 
14be7 2a 70 4e 65 77 3b 0a 20 20 20 20 69 66 28 20 69  *pNew;.    if( i
14be8 3c 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 20 20 70  <nOld ){.      p
14be9 4e 65 77 20 3d 20 61 70 4e 65 77 5b 69 5d 20 3d  New = apNew[i] =
14bea 20 61 70 4f 6c 64 5b 69 5d 3b 0a 20 20 20 20 20   apOld[i];.     
14beb 20 70 67 6e 6f 4e 65 77 5b 69 5d 20 3d 20 70 67   pgnoNew[i] = pg
14bec 6e 6f 4f 6c 64 5b 69 5d 3b 0a 20 20 20 20 20 20  noOld[i];.      
14bed 61 70 4f 6c 64 5b 69 5d 20 3d 20 30 3b 0a 20 20  apOld[i] = 0;.  
14bee 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
14bef 50 61 67 65 72 57 72 69 74 65 28 70 4e 65 77 2d  PagerWrite(pNew-
14bf0 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  >pDbPage);.     
14bf1 20 6e 4e 65 77 2b 2b 3b 0a 20 20 20 20 20 20 69   nNew++;.      i
14bf2 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c  f( rc ) goto bal
14bf3 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  ance_cleanup;.  
14bf4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61    }else{.      a
14bf5 73 73 65 72 74 28 20 69 3e 30 20 29 3b 0a 20 20  ssert( i>0 );.  
14bf6 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74      rc = allocat
14bf7 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20  eBtreePage(pBt, 
14bf8 26 70 4e 65 77 2c 20 26 70 67 6e 6f 4e 65 77 5b  &pNew, &pgnoNew[
14bf9 69 5d 2c 20 70 67 6e 6f 4e 65 77 5b 69 2d 31 5d  i], pgnoNew[i-1]
14bfa 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 0);.      if( 
14bfb 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63  rc ) goto balanc
14bfc 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20  e_cleanup;.     
14bfd 20 61 70 4e 65 77 5b 69 5d 20 3d 20 70 4e 65 77   apNew[i] = pNew
14bfe 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b 3b 0a  ;.      nNew++;.
14bff 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
14c00 46 72 65 65 20 61 6e 79 20 6f 6c 64 20 70 61 67  Free any old pag
14c01 65 73 20 74 68 61 74 20 77 65 72 65 20 6e 6f 74  es that were not
14c02 20 72 65 75 73 65 64 20 61 73 20 6e 65 77 20 70   reused as new p
14c03 61 67 65 73 2e 0a 20 20 2a 2f 0a 20 20 77 68 69  ages..  */.  whi
14c04 6c 65 28 20 69 3c 6e 4f 6c 64 20 29 7b 0a 20 20  le( i<nOld ){.  
14c05 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 65 28    rc = freePage(
14c06 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 20 20 69  apOld[i]);.    i
14c07 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c  f( rc ) goto bal
14c08 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  ance_cleanup;.  
14c09 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 61 70    releasePage(ap
14c0a 4f 6c 64 5b 69 5d 29 3b 0a 20 20 20 20 61 70 4f  Old[i]);.    apO
14c0b 6c 64 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 69  ld[i] = 0;.    i
14c0c 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20  ++;.  }..  /*.  
14c0d 2a 2a 20 50 75 74 20 74 68 65 20 6e 65 77 20 70  ** Put the new p
14c0e 61 67 65 73 20 69 6e 20 61 63 63 65 6e 64 69 6e  ages in accendin
14c0f 67 20 6f 72 64 65 72 2e 20 20 54 68 69 73 20 68  g order.  This h
14c10 65 6c 70 73 20 74 6f 0a 20 20 2a 2a 20 6b 65 65  elps to.  ** kee
14c11 70 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65  p entries in the
14c12 20 64 69 73 6b 20 66 69 6c 65 20 69 6e 20 6f 72   disk file in or
14c13 64 65 72 20 73 6f 20 74 68 61 74 20 61 20 73 63  der so that a sc
14c14 61 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74  an.  ** of the t
14c15 61 62 6c 65 20 69 73 20 61 20 6c 69 6e 65 61 72  able is a linear
14c16 20 73 63 61 6e 20 74 68 72 6f 75 67 68 20 74 68   scan through th
14c17 65 20 66 69 6c 65 2e 20 20 54 68 61 74 0a 20 20  e file.  That.  
14c18 2a 2a 20 69 6e 20 74 75 72 6e 20 68 65 6c 70 73  ** in turn helps
14c19 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73   the operating s
14c1a 79 73 74 65 6d 20 74 6f 20 64 65 6c 69 76 65 72  ystem to deliver
14c1b 20 70 61 67 65 73 0a 20 20 2a 2a 20 66 72 6f 6d   pages.  ** from
14c1c 20 74 68 65 20 64 69 73 6b 20 6d 6f 72 65 20 72   the disk more r
14c1d 61 70 69 64 6c 79 2e 0a 20 20 2a 2a 0a 20 20 2a  apidly..  **.  *
14c1e 2a 20 41 6e 20 4f 28 6e 5e 32 29 20 69 6e 73 65  * An O(n^2) inse
14c1f 72 74 69 6f 6e 20 73 6f 72 74 20 61 6c 67 6f 72  rtion sort algor
14c20 69 74 68 6d 20 69 73 20 75 73 65 64 2c 20 62 75  ithm is used, bu
14c21 74 20 73 69 6e 63 65 0a 20 20 2a 2a 20 6e 20 69  t since.  ** n i
14c22 73 20 6e 65 76 65 72 20 6d 6f 72 65 20 74 68 61  s never more tha
14c23 6e 20 4e 42 20 28 61 20 73 6d 61 6c 6c 20 63 6f  n NB (a small co
14c24 6e 73 74 61 6e 74 29 2c 20 74 68 61 74 20 73 68  nstant), that sh
14c25 6f 75 6c 64 0a 20 20 2a 2a 20 6e 6f 74 20 62 65  ould.  ** not be
14c26 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 2a 2a   a problem..  **
14c27 0a 20 20 2a 2a 20 57 68 65 6e 20 4e 42 3d 3d 33  .  ** When NB==3
14c28 2c 20 74 68 69 73 20 6f 6e 65 20 6f 70 74 69 6d  , this one optim
14c29 69 7a 61 74 69 6f 6e 20 6d 61 6b 65 73 20 74 68  ization makes th
14c2a 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20  e database.  ** 
14c2b 61 62 6f 75 74 20 32 35 25 20 66 61 73 74 65 72  about 25% faster
14c2c 20 66 6f 72 20 6c 61 72 67 65 20 69 6e 73 65 72   for large inser
14c2d 74 69 6f 6e 73 20 61 6e 64 20 64 65 6c 65 74 69  tions and deleti
14c2e 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  ons..  */.  for(
14c2f 69 3d 30 3b 20 69 3c 6b 2d 31 3b 20 69 2b 2b 29  i=0; i<k-1; i++)
14c30 7b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 56 20 3d  {.    int minV =
14c31 20 70 67 6e 6f 4e 65 77 5b 69 5d 3b 0a 20 20 20   pgnoNew[i];.   
14c32 20 69 6e 74 20 6d 69 6e 49 20 3d 20 69 3b 0a 20   int minI = i;. 
14c33 20 20 20 66 6f 72 28 6a 3d 69 2b 31 3b 20 6a 3c     for(j=i+1; j<
14c34 6b 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69  k; j++){.      i
14c35 66 28 20 70 67 6e 6f 4e 65 77 5b 6a 5d 3c 28 75  f( pgnoNew[j]<(u
14c36 6e 73 69 67 6e 65 64 29 6d 69 6e 56 20 29 7b 0a  nsigned)minV ){.
14c37 20 20 20 20 20 20 20 20 6d 69 6e 49 20 3d 20 6a          minI = j
14c38 3b 0a 20 20 20 20 20 20 20 20 6d 69 6e 56 20 3d  ;.        minV =
14c39 20 70 67 6e 6f 4e 65 77 5b 6a 5d 3b 0a 20 20 20   pgnoNew[j];.   
14c3a 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
14c3b 66 28 20 6d 69 6e 49 3e 69 20 29 7b 0a 20 20 20  f( minI>i ){.   
14c3c 20 20 20 69 6e 74 20 74 3b 0a 20 20 20 20 20 20     int t;.      
14c3d 4d 65 6d 50 61 67 65 20 2a 70 54 3b 0a 20 20 20  MemPage *pT;.   
14c3e 20 20 20 74 20 3d 20 70 67 6e 6f 4e 65 77 5b 69     t = pgnoNew[i
14c3f 5d 3b 0a 20 20 20 20 20 20 70 54 20 3d 20 61 70  ];.      pT = ap
14c40 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20 70 67  New[i];.      pg
14c41 6e 6f 4e 65 77 5b 69 5d 20 3d 20 70 67 6e 6f 4e  noNew[i] = pgnoN
14c42 65 77 5b 6d 69 6e 49 5d 3b 0a 20 20 20 20 20 20  ew[minI];.      
14c43 61 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4e 65 77  apNew[i] = apNew
14c44 5b 6d 69 6e 49 5d 3b 0a 20 20 20 20 20 20 70 67  [minI];.      pg
14c45 6e 6f 4e 65 77 5b 6d 69 6e 49 5d 20 3d 20 74 3b  noNew[minI] = t;
14c46 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 6d 69 6e  .      apNew[min
14c47 49 5d 20 3d 20 70 54 3b 0a 20 20 20 20 7d 0a 20  I] = pT;.    }. 
14c48 20 7d 0a 20 20 54 52 41 43 45 28 28 22 42 41 4c   }.  TRACE(("BAL
14c49 41 4e 43 45 3a 20 6f 6c 64 3a 20 25 64 20 25 64  ANCE: old: %d %d
14c4a 20 25 64 20 20 6e 65 77 3a 20 25 64 28 25 64 29   %d  new: %d(%d)
14c4b 20 25 64 28 25 64 29 20 25 64 28 25 64 29 20 25   %d(%d) %d(%d) %
14c4c 64 28 25 64 29 20 25 64 28 25 64 29 5c 6e 22 2c  d(%d) %d(%d)\n",
14c4d 0a 20 20 20 20 70 67 6e 6f 4f 6c 64 5b 30 5d 2c  .    pgnoOld[0],
14c4e 20 0a 20 20 20 20 6e 4f 6c 64 3e 3d 32 20 3f 20   .    nOld>=2 ? 
14c4f 70 67 6e 6f 4f 6c 64 5b 31 5d 20 3a 20 30 2c 0a  pgnoOld[1] : 0,.
14c50 20 20 20 20 6e 4f 6c 64 3e 3d 33 20 3f 20 70 67      nOld>=3 ? pg
14c51 6e 6f 4f 6c 64 5b 32 5d 20 3a 20 30 2c 0a 20 20  noOld[2] : 0,.  
14c52 20 20 70 67 6e 6f 4e 65 77 5b 30 5d 2c 20 73 7a    pgnoNew[0], sz
14c53 4e 65 77 5b 30 5d 2c 0a 20 20 20 20 6e 4e 65 77  New[0],.    nNew
14c54 3e 3d 32 20 3f 20 70 67 6e 6f 4e 65 77 5b 31 5d  >=2 ? pgnoNew[1]
14c55 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 32 20 3f 20   : 0, nNew>=2 ? 
14c56 73 7a 4e 65 77 5b 31 5d 20 3a 20 30 2c 0a 20 20  szNew[1] : 0,.  
14c57 20 20 6e 4e 65 77 3e 3d 33 20 3f 20 70 67 6e 6f    nNew>=3 ? pgno
14c58 4e 65 77 5b 32 5d 20 3a 20 30 2c 20 6e 4e 65 77  New[2] : 0, nNew
14c59 3e 3d 33 20 3f 20 73 7a 4e 65 77 5b 32 5d 20 3a  >=3 ? szNew[2] :
14c5a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 34 20   0,.    nNew>=4 
14c5b 3f 20 70 67 6e 6f 4e 65 77 5b 33 5d 20 3a 20 30  ? pgnoNew[3] : 0
14c5c 2c 20 6e 4e 65 77 3e 3d 34 20 3f 20 73 7a 4e 65  , nNew>=4 ? szNe
14c5d 77 5b 33 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e  w[3] : 0,.    nN
14c5e 65 77 3e 3d 35 20 3f 20 70 67 6e 6f 4e 65 77 5b  ew>=5 ? pgnoNew[
14c5f 34 5d 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 35 20  4] : 0, nNew>=5 
14c60 3f 20 73 7a 4e 65 77 5b 34 5d 20 3a 20 30 29 29  ? szNew[4] : 0))
14c61 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 45 76 65  ;..  /*.  ** Eve
14c62 6e 6c 79 20 64 69 73 74 72 69 62 75 74 65 20 74  nly distribute t
14c63 68 65 20 64 61 74 61 20 69 6e 20 61 70 43 65 6c  he data in apCel
14c64 6c 5b 5d 20 61 63 72 6f 73 73 20 74 68 65 20 6e  l[] across the n
14c65 65 77 20 70 61 67 65 73 2e 0a 20 20 2a 2a 20 49  ew pages..  ** I
14c66 6e 73 65 72 74 20 64 69 76 69 64 65 72 20 63 65  nsert divider ce
14c67 6c 6c 73 20 69 6e 74 6f 20 70 50 61 72 65 6e 74  lls into pParent
14c68 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 20   as necessary.. 
14c69 20 2a 2f 0a 20 20 6a 20 3d 20 30 3b 0a 20 20 66   */.  j = 0;.  f
14c6a 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 3b 20  or(i=0; i<nNew; 
14c6b 69 2b 2b 29 7b 0a 20 20 20 20 2f 2a 20 41 73 73  i++){.    /* Ass
14c6c 65 6d 62 6c 65 20 74 68 65 20 6e 65 77 20 73 69  emble the new si
14c6d 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a 20  bling page. */. 
14c6e 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77     MemPage *pNew
14c6f 20 3d 20 61 70 4e 65 77 5b 69 5d 3b 0a 20 20 20   = apNew[i];.   
14c70 20 61 73 73 65 72 74 28 20 6a 3c 6e 4d 61 78 43   assert( j<nMaxC
14c71 65 6c 6c 73 20 29 3b 0a 20 20 20 20 61 73 73 65  ells );.    asse
14c72 72 74 28 20 70 4e 65 77 2d 3e 70 67 6e 6f 3d 3d  rt( pNew->pgno==
14c73 70 67 6e 6f 4e 65 77 5b 69 5d 20 29 3b 0a 20 20  pgnoNew[i] );.  
14c74 20 20 7a 65 72 6f 50 61 67 65 28 70 4e 65 77 2c    zeroPage(pNew,
14c75 20 70 61 67 65 46 6c 61 67 73 29 3b 0a 20 20 20   pageFlags);.   
14c76 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 70 4e   assemblePage(pN
14c77 65 77 2c 20 63 6e 74 4e 65 77 5b 69 5d 2d 6a 2c  ew, cntNew[i]-j,
14c78 20 26 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 73 7a   &apCell[j], &sz
14c79 43 65 6c 6c 5b 6a 5d 29 3b 0a 20 20 20 20 61 73  Cell[j]);.    as
14c7a 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 43 65 6c  sert( pNew->nCel
14c7b 6c 3e 30 20 7c 7c 20 28 6e 4e 65 77 3d 3d 31 20  l>0 || (nNew==1 
14c7c 26 26 20 63 6e 74 4e 65 77 5b 30 5d 3d 3d 30 29  && cntNew[0]==0)
14c7d 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
14c7e 70 4e 65 77 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d  pNew->nOverflow=
14c7f 3d 30 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66  =0 );..    /* If
14c80 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f   this is an auto
14c81 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65  -vacuum database
14c82 2c 20 75 70 64 61 74 65 20 74 68 65 20 70 6f 69  , update the poi
14c83 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 69 65 73  nter map entries
14c84 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 70 6f 69  .    ** that poi
14c85 6e 74 20 74 6f 20 74 68 65 20 73 69 62 6c 69 6e  nt to the siblin
14c86 67 73 20 74 68 61 74 20 77 65 72 65 20 72 65 61  gs that were rea
14c87 72 72 61 6e 67 65 64 2e 20 54 68 65 73 65 20 63  rranged. These c
14c88 61 6e 20 62 65 3a 20 6c 65 66 74 0a 20 20 20 20  an be: left.    
14c89 2a 2a 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 63  ** children of c
14c8a 65 6c 6c 73 2c 20 74 68 65 20 72 69 67 68 74 2d  ells, the right-
14c8b 63 68 69 6c 64 20 6f 66 20 74 68 65 20 70 61 67  child of the pag
14c8c 65 2c 20 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70  e, or overflow p
14c8d 61 67 65 73 0a 20 20 20 20 2a 2a 20 70 6f 69 6e  ages.    ** poin
14c8e 74 65 64 20 74 6f 20 62 79 20 63 65 6c 6c 73 2e  ted to by cells.
14c8f 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
14c90 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a  ISAUTOVACUUM ){.
14c91 20 20 20 20 20 20 66 6f 72 28 6b 3d 6a 3b 20 6b        for(k=j; k
14c92 3c 63 6e 74 4e 65 77 5b 69 5d 3b 20 6b 2b 2b 29  <cntNew[i]; k++)
14c93 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
14c94 28 20 6b 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b  ( k<nMaxCells );
14c95 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 46 72  .        if( aFr
14c96 6f 6d 5b 6b 5d 3d 3d 30 78 46 46 20 7c 7c 20 61  om[k]==0xFF || a
14c97 70 43 6f 70 79 5b 61 46 72 6f 6d 5b 6b 5d 5d 2d  pCopy[aFrom[k]]-
14c98 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d 3e 70 67 6e  >pgno!=pNew->pgn
14c99 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  o ){.          r
14c9a 63 20 3d 20 70 74 72 6d 61 70 50 75 74 4f 76 66  c = ptrmapPutOvf
14c9b 6c 28 70 4e 65 77 2c 20 6b 2d 6a 29 3b 0a 20 20  l(pNew, k-j);.  
14c9c 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
14c9d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6c 65 61  SQLITE_OK && lea
14c9e 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 30 20 29  fCorrection==0 )
14c9f 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  {.            rc
14ca0 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74   = ptrmapPut(pBt
14ca1 2c 20 67 65 74 34 62 79 74 65 28 61 70 43 65 6c  , get4byte(apCel
14ca2 6c 5b 6b 5d 29 2c 20 50 54 52 4d 41 50 5f 42 54  l[k]), PTRMAP_BT
14ca3 52 45 45 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 29  REE, pNew->pgno)
14ca4 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
14ca5 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
14ca6 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
14ca7 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61           goto ba
14ca8 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  lance_cleanup;. 
14ca9 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
14caa 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
14cab 20 7d 0a 0a 20 20 20 20 6a 20 3d 20 63 6e 74 4e   }..    j = cntN
14cac 65 77 5b 69 5d 3b 0a 0a 20 20 20 20 2f 2a 20 49  ew[i];..    /* I
14cad 66 20 74 68 65 20 73 69 62 6c 69 6e 67 20 70 61  f the sibling pa
14cae 67 65 20 61 73 73 65 6d 62 6c 65 64 20 61 62 6f  ge assembled abo
14caf 76 65 20 77 61 73 20 6e 6f 74 20 74 68 65 20 72  ve was not the r
14cb0 69 67 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e  ight-most siblin
14cb1 67 2c 0a 20 20 20 20 2a 2a 20 69 6e 73 65 72 74  g,.    ** insert
14cb2 20 61 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20   a divider cell 
14cb3 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20  into the parent 
14cb4 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  page..    */.   
14cb5 20 69 66 28 20 69 3c 6e 4e 65 77 2d 31 20 26 26   if( i<nNew-1 &&
14cb6 20 6a 3c 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20   j<nCell ){.    
14cb7 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20    u8 *pCell;.   
14cb8 20 20 20 75 38 20 2a 70 54 65 6d 70 3b 0a 20 20     u8 *pTemp;.  
14cb9 20 20 20 20 69 6e 74 20 73 7a 3b 0a 0a 20 20 20      int sz;..   
14cba 20 20 20 61 73 73 65 72 74 28 20 6a 3c 6e 4d 61     assert( j<nMa
14cbb 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20  xCells );.      
14cbc 70 43 65 6c 6c 20 3d 20 61 70 43 65 6c 6c 5b 6a  pCell = apCell[j
14cbd 5d 3b 0a 20 20 20 20 20 20 73 7a 20 3d 20 73 7a  ];.      sz = sz
14cbe 43 65 6c 6c 5b 6a 5d 20 2b 20 6c 65 61 66 43 6f  Cell[j] + leafCo
14cbf 72 72 65 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20  rrection;.      
14cc0 70 54 65 6d 70 20 3d 20 26 61 53 70 61 63 65 32  pTemp = &aSpace2
14cc1 5b 69 53 70 61 63 65 32 5d 3b 0a 20 20 20 20 20  [iSpace2];.     
14cc2 20 69 66 28 20 21 70 4e 65 77 2d 3e 6c 65 61 66   if( !pNew->leaf
14cc3 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63   ){.        memc
14cc4 70 79 28 26 70 4e 65 77 2d 3e 61 44 61 74 61 5b  py(&pNew->aData[
14cc5 38 5d 2c 20 70 43 65 6c 6c 2c 20 34 29 3b 0a 20  8], pCell, 4);. 
14cc6 20 20 20 20 20 20 20 69 66 28 20 49 53 41 55 54         if( ISAUT
14cc7 4f 56 41 43 55 55 4d 20 0a 20 20 20 20 20 20 20  OVACUUM .       
14cc8 20 20 26 26 20 28 61 46 72 6f 6d 5b 6a 5d 3d 3d    && (aFrom[j]==
14cc9 30 78 46 46 20 7c 7c 20 61 70 43 6f 70 79 5b 61  0xFF || apCopy[a
14cca 46 72 6f 6d 5b 6a 5d 5d 2d 3e 70 67 6e 6f 21 3d  From[j]]->pgno!=
14ccb 70 4e 65 77 2d 3e 70 67 6e 6f 29 0a 20 20 20 20  pNew->pgno).    
14ccc 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20      ){.         
14ccd 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28   rc = ptrmapPut(
14cce 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 70 43  pBt, get4byte(pC
14ccf 65 6c 6c 29 2c 20 50 54 52 4d 41 50 5f 42 54 52  ell), PTRMAP_BTR
14cd0 45 45 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 29 3b  EE, pNew->pgno);
14cd1 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72  .          if( r
14cd2 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
14cd3 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f              goto
14cd4 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70   balance_cleanup
14cd5 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
14cd6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
14cd7 6c 73 65 20 69 66 28 20 6c 65 61 66 44 61 74 61  lse if( leafData
14cd8 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49   ){.        /* I
14cd9 66 20 74 68 65 20 74 72 65 65 20 69 73 20 61 20  f the tree is a 
14cda 6c 65 61 66 2d 64 61 74 61 20 74 72 65 65 2c 20  leaf-data tree, 
14cdb 61 6e 64 20 74 68 65 20 73 69 62 6c 69 6e 67 73  and the siblings
14cdc 20 61 72 65 20 6c 65 61 76 65 73 2c 20 0a 20 20   are leaves, .  
14cdd 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68        ** then th
14cde 65 72 65 20 69 73 20 6e 6f 20 64 69 76 69 64 65  ere is no divide
14cdf 72 20 63 65 6c 6c 20 69 6e 20 61 70 43 65 6c 6c  r cell in apCell
14ce0 5b 5d 2e 20 49 6e 73 74 65 61 64 2c 20 74 68 65  []. Instead, the
14ce1 20 64 69 76 69 64 65 72 20 0a 20 20 20 20 20 20   divider .      
14ce2 20 20 2a 2a 20 63 65 6c 6c 20 63 6f 6e 73 69 73    ** cell consis
14ce3 74 73 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65  ts of the intege
14ce4 72 20 6b 65 79 20 66 6f 72 20 74 68 65 20 72 69  r key for the ri
14ce5 67 68 74 2d 6d 6f 73 74 20 63 65 6c 6c 20 6f 66  ght-most cell of
14ce6 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65   .        ** the
14ce7 20 73 69 62 6c 69 6e 67 2d 70 61 67 65 20 61 73   sibling-page as
14ce8 73 65 6d 62 6c 65 64 20 61 62 6f 76 65 20 6f 6e  sembled above on
14ce9 6c 79 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ly..        */. 
14cea 20 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20         CellInfo 
14ceb 69 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 6a 2d  info;.        j-
14cec 2d 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  -;.        sqlit
14ced 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c  e3BtreeParseCell
14cee 50 74 72 28 70 4e 65 77 2c 20 61 70 43 65 6c 6c  Ptr(pNew, apCell
14cef 5b 6a 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20  [j], &info);.   
14cf0 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 70 54 65       pCell = pTe
14cf1 6d 70 3b 0a 20 20 20 20 20 20 20 20 66 69 6c 6c  mp;.        fill
14cf2 49 6e 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20  InCell(pParent, 
14cf3 70 43 65 6c 6c 2c 20 30 2c 20 69 6e 66 6f 2e 6e  pCell, 0, info.n
14cf4 4b 65 79 2c 20 30 2c 20 30 2c 20 30 2c 20 26 73  Key, 0, 0, 0, &s
14cf5 7a 29 3b 0a 20 20 20 20 20 20 20 20 70 54 65 6d  z);.        pTem
14cf6 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c  p = 0;.      }el
14cf7 73 65 7b 0a 20 20 20 20 20 20 20 20 70 43 65 6c  se{.        pCel
14cf8 6c 20 2d 3d 20 34 3b 0a 20 20 20 20 20 20 20 20  l -= 4;.        
14cf9 2f 2a 20 4f 62 73 63 75 72 65 20 63 61 73 65 20  /* Obscure case 
14cfa 66 6f 72 20 6e 6f 6e 2d 6c 65 61 66 2d 64 61 74  for non-leaf-dat
14cfb 61 20 74 72 65 65 73 3a 20 49 66 20 74 68 65 20  a trees: If the 
14cfc 63 65 6c 6c 20 61 74 20 70 43 65 6c 6c 20 77 61  cell at pCell wa
14cfd 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 72 65  s.        ** pre
14cfe 76 69 6f 75 73 6c 79 20 73 74 6f 72 65 64 20 6f  viously stored o
14cff 6e 20 61 20 6c 65 61 66 20 6e 6f 64 65 2c 20 61  n a leaf node, a
14d00 6e 64 20 69 74 73 20 72 65 70 6f 72 74 65 64 20  nd its reported 
14d01 73 69 7a 65 20 77 61 73 20 34 0a 20 20 20 20 20  size was 4.     
14d02 20 20 20 2a 2a 20 62 79 74 65 73 2c 20 74 68 65     ** bytes, the
14d03 6e 20 69 74 20 6d 61 79 20 61 63 74 75 61 6c 6c  n it may actuall
14d04 79 20 62 65 20 73 6d 61 6c 6c 65 72 20 74 68 61  y be smaller tha
14d05 6e 20 74 68 69 73 20 0a 20 20 20 20 20 20 20 20  n this .        
14d06 2a 2a 20 28 73 65 65 20 73 71 6c 69 74 65 33 42  ** (see sqlite3B
14d07 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72  treeParseCellPtr
14d08 28 29 2c 20 34 20 62 79 74 65 73 20 69 73 20 74  (), 4 bytes is t
14d09 68 65 20 6d 69 6e 69 6d 75 6d 20 73 69 7a 65 20  he minimum size 
14d0a 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e  of.        ** an
14d0b 79 20 63 65 6c 6c 29 2e 20 42 75 74 20 69 74 20  y cell). But it 
14d0c 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20  is important to 
14d0d 70 61 73 73 20 74 68 65 20 63 6f 72 72 65 63 74  pass the correct
14d0e 20 73 69 7a 65 20 74 6f 20 0a 20 20 20 20 20 20   size to .      
14d0f 20 20 2a 2a 20 69 6e 73 65 72 74 43 65 6c 6c 28    ** insertCell(
14d10 29 2c 20 73 6f 20 72 65 70 61 72 73 65 20 74 68  ), so reparse th
14d11 65 20 63 65 6c 6c 20 6e 6f 77 2e 0a 20 20 20 20  e cell now..    
14d12 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
14d13 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 69 73  * Note that this
14d14 20 63 61 6e 20 6e 65 76 65 72 20 68 61 70 70 65   can never happe
14d15 6e 20 69 6e 20 61 6e 20 53 51 4c 69 74 65 20 64  n in an SQLite d
14d16 61 74 61 20 66 69 6c 65 2c 20 61 73 20 61 6c 6c  ata file, as all
14d17 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 65 6c 6c  .        ** cell
14d18 73 20 61 72 65 20 61 74 20 6c 65 61 73 74 20 34  s are at least 4
14d19 20 62 79 74 65 73 2e 20 49 74 20 6f 6e 6c 79 20   bytes. It only 
14d1a 68 61 70 70 65 6e 73 20 69 6e 20 62 2d 74 72 65  happens in b-tre
14d1b 65 73 20 75 73 65 64 0a 20 20 20 20 20 20 20 20  es used.        
14d1c 2a 2a 20 74 6f 20 65 76 61 6c 75 61 74 65 20 22  ** to evaluate "
14d1d 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22  IN (SELECT ...)"
14d1e 20 61 6e 64 20 73 69 6d 69 6c 61 72 20 63 6c 61   and similar cla
14d1f 75 73 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f  uses..        */
14d20 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 7a 43  .        if( szC
14d21 65 6c 6c 5b 6a 5d 3d 3d 34 20 29 7b 0a 20 20 20  ell[j]==4 ){.   
14d22 20 20 20 20 20 20 20 61 73 73 65 72 74 28 6c 65         assert(le
14d23 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 29  afCorrection==4)
14d24 3b 0a 20 20 20 20 20 20 20 20 20 20 73 7a 20 3d  ;.          sz =
14d25 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61   cellSizePtr(pPa
14d26 72 65 6e 74 2c 20 70 43 65 6c 6c 29 3b 0a 20 20  rent, pCell);.  
14d27 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
14d28 20 20 20 20 20 20 69 53 70 61 63 65 32 20 2b 3d        iSpace2 +=
14d29 20 73 7a 3b 0a 20 20 20 20 20 20 61 73 73 65 72   sz;.      asser
14d2a 74 28 20 73 7a 3c 3d 70 42 74 2d 3e 70 61 67 65  t( sz<=pBt->page
14d2b 53 69 7a 65 2f 34 20 29 3b 0a 20 20 20 20 20 20  Size/4 );.      
14d2c 61 73 73 65 72 74 28 20 69 53 70 61 63 65 32 3c  assert( iSpace2<
14d2d 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29  =pBt->pageSize )
14d2e 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 69 6e 73  ;.      rc = ins
14d2f 65 72 74 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c  ertCell(pParent,
14d30 20 6e 78 44 69 76 2c 20 70 43 65 6c 6c 2c 20 73   nxDiv, pCell, s
14d31 7a 2c 20 70 54 65 6d 70 2c 20 34 29 3b 0a 20 20  z, pTemp, 4);.  
14d32 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
14d33 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 62 61 6c  TE_OK ) goto bal
14d34 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  ance_cleanup;.  
14d35 20 20 20 20 70 75 74 34 62 79 74 65 28 66 69 6e      put4byte(fin
14d36 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 50  dOverflowCell(pP
14d37 61 72 65 6e 74 2c 6e 78 44 69 76 29 2c 20 70 4e  arent,nxDiv), pN
14d38 65 77 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 20 20  ew->pgno);..    
14d39 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
14d3a 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64  an auto-vacuum d
14d3b 61 74 61 62 61 73 65 2c 20 61 6e 64 20 6e 6f 74  atabase, and not
14d3c 20 61 20 6c 65 61 66 2d 64 61 74 61 20 74 72 65   a leaf-data tre
14d3d 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e  e,.      ** then
14d3e 20 75 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e   update the poin
14d3f 74 65 72 20 6d 61 70 20 77 69 74 68 20 61 6e 20  ter map with an 
14d40 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6f 76  entry for the ov
14d41 65 72 66 6c 6f 77 20 70 61 67 65 0a 20 20 20 20  erflow page.    
14d42 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 63 65    ** that the ce
14d43 6c 6c 20 6a 75 73 74 20 69 6e 73 65 72 74 65 64  ll just inserted
14d44 20 70 6f 69 6e 74 73 20 74 6f 20 28 69 66 20 61   points to (if a
14d45 6e 79 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ny)..      */.  
14d46 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41      if( ISAUTOVA
14d47 43 55 55 4d 20 26 26 20 21 6c 65 61 66 44 61 74  CUUM && !leafDat
14d48 61 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  a ){.        rc 
14d49 3d 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28  = ptrmapPutOvfl(
14d4a 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 29 3b  pParent, nxDiv);
14d4b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
14d4c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
14d4d 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c          goto bal
14d4e 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  ance_cleanup;.  
14d4f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
14d50 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20        j++;.     
14d51 20 6e 78 44 69 76 2b 2b 3b 0a 20 20 20 20 7d 0a   nxDiv++;.    }.
14d52 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20  .    /* Set the 
14d53 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72  pointer-map entr
14d54 79 20 66 6f 72 20 74 68 65 20 6e 65 77 20 73 69  y for the new si
14d55 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a 20  bling page. */. 
14d56 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43     if( ISAUTOVAC
14d57 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20  UUM ){.      rc 
14d58 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c  = ptrmapPut(pBt,
14d59 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 50 54 52   pNew->pgno, PTR
14d5a 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61 72 65  MAP_BTREE, pPare
14d5b 6e 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20  nt->pgno);.     
14d5c 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
14d5d 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  OK ){.        go
14d5e 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e  to balance_clean
14d5f 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  up;.      }.    
14d60 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  }.  }.  assert( 
14d61 6a 3d 3d 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73  j==nCell );.  as
14d62 73 65 72 74 28 20 6e 4f 6c 64 3e 30 20 29 3b 0a  sert( nOld>0 );.
14d63 20 20 61 73 73 65 72 74 28 20 6e 4e 65 77 3e 30    assert( nNew>0
14d64 20 29 3b 0a 20 20 69 66 28 20 28 70 61 67 65 46   );.  if( (pageF
14d65 6c 61 67 73 20 26 20 50 54 46 5f 4c 45 41 46 29  lags & PTF_LEAF)
14d66 3d 3d 30 20 29 7b 0a 20 20 20 20 75 38 20 2a 7a  ==0 ){.    u8 *z
14d67 43 68 69 6c 64 20 3d 20 26 61 70 43 6f 70 79 5b  Child = &apCopy[
14d68 6e 4f 6c 64 2d 31 5d 2d 3e 61 44 61 74 61 5b 38  nOld-1]->aData[8
14d69 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61  ];.    memcpy(&a
14d6a 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 61 44  pNew[nNew-1]->aD
14d6b 61 74 61 5b 38 5d 2c 20 7a 43 68 69 6c 64 2c 20  ata[8], zChild, 
14d6c 34 29 3b 0a 20 20 20 20 69 66 28 20 49 53 41 55  4);.    if( ISAU
14d6d 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20  TOVACUUM ){.    
14d6e 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74    rc = ptrmapPut
14d6f 28 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 7a  (pBt, get4byte(z
14d70 43 68 69 6c 64 29 2c 20 50 54 52 4d 41 50 5f 42  Child), PTRMAP_B
14d71 54 52 45 45 2c 20 61 70 4e 65 77 5b 6e 4e 65 77  TREE, apNew[nNew
14d72 2d 31 5d 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20  -1]->pgno);.    
14d73 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
14d74 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67  _OK ){.        g
14d75 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61  oto balance_clea
14d76 6e 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nup;.      }.   
14d77 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6e 78 44   }.  }.  if( nxD
14d78 69 76 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65  iv==pParent->nCe
14d79 6c 6c 2b 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65  ll+pParent->nOve
14d7a 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 2f 2a 20  rflow ){.    /* 
14d7b 52 69 67 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69  Right-most sibli
14d7c 6e 67 20 69 73 20 74 68 65 20 72 69 67 68 74 2d  ng is the right-
14d7d 6d 6f 73 74 20 63 68 69 6c 64 20 6f 66 20 70 50  most child of pP
14d7e 61 72 65 6e 74 20 2a 2f 0a 20 20 20 20 70 75 74  arent */.    put
14d7f 34 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d 3e  4byte(&pParent->
14d80 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68  aData[pParent->h
14d81 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 70 67 6e  drOffset+8], pgn
14d82 6f 4e 65 77 5b 6e 4e 65 77 2d 31 5d 29 3b 0a 20  oNew[nNew-1]);. 
14d83 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 52   }else{.    /* R
14d84 69 67 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e  ight-most siblin
14d85 67 20 69 73 20 74 68 65 20 6c 65 66 74 20 63 68  g is the left ch
14d86 69 6c 64 20 6f 66 20 74 68 65 20 66 69 72 73 74  ild of the first
14d87 20 65 6e 74 72 79 20 69 6e 20 70 50 61 72 65 6e   entry in pParen
14d88 74 0a 20 20 20 20 2a 2a 20 70 61 73 74 20 74 68  t.    ** past th
14d89 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 64 69 76  e right-most div
14d8a 69 64 65 72 20 65 6e 74 72 79 20 2a 2f 0a 20 20  ider entry */.  
14d8b 20 20 70 75 74 34 62 79 74 65 28 66 69 6e 64 4f    put4byte(findO
14d8c 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 50 61 72  verflowCell(pPar
14d8d 65 6e 74 2c 20 6e 78 44 69 76 29 2c 20 70 67 6e  ent, nxDiv), pgn
14d8e 6f 4e 65 77 5b 6e 4e 65 77 2d 31 5d 29 3b 0a 20  oNew[nNew-1]);. 
14d8f 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 42 61   }..  /*.  ** Ba
14d90 6c 61 6e 63 65 20 74 68 65 20 70 61 72 65 6e 74  lance the parent
14d91 20 70 61 67 65 2e 20 20 4e 6f 74 65 20 74 68 61   page.  Note tha
14d92 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61  t the current pa
14d93 67 65 20 28 70 50 61 67 65 29 20 6d 69 67 68 74  ge (pPage) might
14d94 0a 20 20 2a 2a 20 68 61 76 65 20 62 65 65 6e 20  .  ** have been 
14d95 61 64 64 65 64 20 74 6f 20 74 68 65 20 66 72 65  added to the fre
14d96 65 6c 69 73 74 20 73 6f 20 69 74 20 6d 69 67 68  elist so it migh
14d97 74 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 20 69  t no longer be i
14d98 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2a  nitialized..  **
14d99 20 42 75 74 20 74 68 65 20 70 61 72 65 6e 74 20   But the parent 
14d9a 70 61 67 65 20 77 69 6c 6c 20 61 6c 77 61 79 73  page will always
14d9b 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e   be initialized.
14d9c 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
14d9d 70 50 61 72 65 6e 74 2d 3e 69 73 49 6e 69 74 20  pParent->isInit 
14d9e 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 63 72 61  );.  sqlite3Scra
14d9f 74 63 68 46 72 65 65 28 61 70 43 65 6c 6c 29 3b  tchFree(apCell);
14da0 0a 20 20 61 70 43 65 6c 6c 20 3d 20 30 3b 0a 20  .  apCell = 0;. 
14da1 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61   releasePage(pPa
14da2 67 65 29 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61  ge);.  pCur->iPa
14da3 67 65 2d 2d 3b 0a 20 20 72 63 20 3d 20 62 61 6c  ge--;.  rc = bal
14da4 61 6e 63 65 28 70 43 75 72 2c 20 30 29 3b 0a 20  ance(pCur, 0);. 
14da5 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 6c 65 61   .  /*.  ** Clea
14da6 6e 75 70 20 62 65 66 6f 72 65 20 72 65 74 75 72  nup before retur
14da7 6e 69 6e 67 2e 0a 20 20 2a 2f 0a 62 61 6c 61 6e  ning..  */.balan
14da8 63 65 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71  ce_cleanup:.  sq
14da9 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 61 53  lite3PageFree(aS
14daa 70 61 63 65 32 29 3b 0a 20 20 73 71 6c 69 74 65  pace2);.  sqlite
14dab 33 53 63 72 61 74 63 68 46 72 65 65 28 61 70 43  3ScratchFree(apC
14dac 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  ell);.  for(i=0;
14dad 20 69 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20   i<nOld; i++){. 
14dae 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 61     releasePage(a
14daf 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20  pOld[i]);.  }.  
14db0 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 3b  for(i=0; i<nNew;
14db1 20 69 2b 2b 29 7b 0a 20 20 20 20 72 65 6c 65 61   i++){.    relea
14db2 73 65 50 61 67 65 28 61 70 4e 65 77 5b 69 5d 29  sePage(apNew[i])
14db3 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 72 65 6c 65  ;.  }..  /* rele
14db4 61 73 65 50 61 67 65 28 70 50 61 72 65 6e 74 29  asePage(pParent)
14db5 3b 20 2a 2f 0a 20 20 54 52 41 43 45 28 28 22 42  ; */.  TRACE(("B
14db6 41 4c 41 4e 43 45 3a 20 66 69 6e 69 73 68 65 64  ALANCE: finished
14db7 20 77 69 74 68 20 25 64 3a 20 6f 6c 64 3d 25 64   with %d: old=%d
14db8 20 6e 65 77 3d 25 64 20 63 65 6c 6c 73 3d 25 64   new=%d cells=%d
14db9 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70  \n",.          p
14dba 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 6e 4f 6c 64  Page->pgno, nOld
14dbb 2c 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c 29 29 3b  , nNew, nCell));
14dbc 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
14dbd 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
14dbe 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66  tine is called f
14dbf 6f 72 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  or the root page
14dc0 20 6f 66 20 61 20 62 74 72 65 65 20 77 68 65 6e   of a btree when
14dc1 20 74 68 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67   the root.** pag
14dc2 65 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 63 65  e contains no ce
14dc3 6c 6c 73 2e 20 20 54 68 69 73 20 69 73 20 61 6e  lls.  This is an
14dc4 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 20   opportunity to 
14dc5 6d 61 6b 65 20 74 68 65 20 74 72 65 65 0a 2a 2a  make the tree.**
14dc6 20 73 68 61 6c 6c 6f 77 65 72 20 62 79 20 6f 6e   shallower by on
14dc7 65 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74  e level..*/.stat
14dc8 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f 73  ic int balance_s
14dc9 68 61 6c 6c 6f 77 65 72 28 42 74 43 75 72 73 6f  hallower(BtCurso
14dca 72 20 2a 70 43 75 72 29 7b 0a 20 20 4d 65 6d 50  r *pCur){.  MemP
14dcb 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20 20  age *pPage;     
14dcc 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74           /* Root
14dcd 20 70 61 67 65 20 6f 66 20 42 2d 54 72 65 65 20   page of B-Tree 
14dce 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 43  */.  MemPage *pC
14dcf 68 69 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20  hild;           
14dd0 20 20 2f 2a 20 54 68 65 20 6f 6e 6c 79 20 63 68    /* The only ch
14dd1 69 6c 64 20 70 61 67 65 20 6f 66 20 70 50 61 67  ild page of pPag
14dd2 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  e */.  Pgno pgno
14dd3 43 68 69 6c 64 3b 20 20 20 20 20 20 20 20 20 20  Child;          
14dd4 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62      /* Page numb
14dd5 65 72 20 66 6f 72 20 70 43 68 69 6c 64 20 2a 2f  er for pChild */
14dd6 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
14dd7 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20  TE_OK;          
14dd8 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66  /* Return code f
14dd9 72 6f 6d 20 73 75 62 70 72 6f 63 65 64 75 72 65  rom subprocedure
14dda 73 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20  s */.  BtShared 
14ddb 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20  *pBt;           
14ddc 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61         /* The ma
14ddd 69 6e 20 42 54 72 65 65 20 73 74 72 75 63 74 75  in BTree structu
14dde 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 43 65  re */.  int mxCe
14ddf 6c 6c 50 65 72 50 61 67 65 3b 20 20 20 20 20 20  llPerPage;      
14de0 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20       /* Maximum 
14de1 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20  number of cells 
14de2 70 65 72 20 70 61 67 65 20 2a 2f 0a 20 20 75 38  per page */.  u8
14de3 20 2a 2a 61 70 43 65 6c 6c 3b 20 20 20 20 20 20   **apCell;      
14de4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c             /* Al
14de5 6c 20 63 65 6c 6c 73 20 66 72 6f 6d 20 70 61 67  l cells from pag
14de6 65 73 20 62 65 69 6e 67 20 62 61 6c 61 6e 63 65  es being balance
14de7 64 20 2a 2f 0a 20 20 75 31 36 20 2a 73 7a 43 65  d */.  u16 *szCe
14de8 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ll;             
14de9 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 73 69 7a      /* Local siz
14dea 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20 2a  e of all cells *
14deb 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  /..  assert( pCu
14dec 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 3b 0a 20  r->iPage==0 );. 
14ded 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61   pPage = pCur->a
14dee 70 50 61 67 65 5b 30 5d 3b 0a 0a 20 20 61 73 73  pPage[0];..  ass
14def 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c  ert( pPage->nCel
14df0 6c 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  l==0 );.  assert
14df1 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
14df2 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d  held(pPage->pBt-
14df3 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 42 74  >mutex) );.  pBt
14df4 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20   = pPage->pBt;. 
14df5 20 6d 78 43 65 6c 6c 50 65 72 50 61 67 65 20 3d   mxCellPerPage =
14df6 20 4d 58 5f 43 45 4c 4c 28 70 42 74 29 3b 0a 20   MX_CELL(pBt);. 
14df7 20 61 70 43 65 6c 6c 20 3d 20 73 71 6c 69 74 65   apCell = sqlite
14df8 33 4d 61 6c 6c 6f 63 28 20 6d 78 43 65 6c 6c 50  3Malloc( mxCellP
14df9 65 72 50 61 67 65 2a 28 73 69 7a 65 6f 66 28 75  erPage*(sizeof(u
14dfa 38 2a 29 2b 73 69 7a 65 6f 66 28 75 31 36 29 29  8*)+sizeof(u16))
14dfb 20 29 3b 0a 20 20 69 66 28 20 61 70 43 65 6c 6c   );.  if( apCell
14dfc 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ==0 ) return SQL
14dfd 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 7a 43  ITE_NOMEM;.  szC
14dfe 65 6c 6c 20 3d 20 28 75 31 36 2a 29 26 61 70 43  ell = (u16*)&apC
14dff 65 6c 6c 5b 6d 78 43 65 6c 6c 50 65 72 50 61 67  ell[mxCellPerPag
14e00 65 5d 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d  e];.  if( pPage-
14e01 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 2f 2a 20  >leaf ){.    /* 
14e02 54 68 65 20 74 61 62 6c 65 20 69 73 20 63 6f 6d  The table is com
14e03 70 6c 65 74 65 6c 79 20 65 6d 70 74 79 20 2a 2f  pletely empty */
14e04 0a 20 20 20 20 54 52 41 43 45 28 28 22 42 41 4c  .    TRACE(("BAL
14e05 41 4e 43 45 3a 20 65 6d 70 74 79 20 74 61 62 6c  ANCE: empty tabl
14e06 65 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 2d 3e  e %d\n", pPage->
14e07 70 67 6e 6f 29 29 3b 0a 20 20 7d 65 6c 73 65 7b  pgno));.  }else{
14e08 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74  .    /* The root
14e09 20 70 61 67 65 20 69 73 20 65 6d 70 74 79 20 62   page is empty b
14e0a 75 74 20 68 61 73 20 6f 6e 65 20 63 68 69 6c 64  ut has one child
14e0b 2e 20 20 54 72 61 6e 73 66 65 72 20 74 68 65 0a  .  Transfer the.
14e0c 20 20 20 20 2a 2a 20 69 6e 66 6f 72 6d 61 74 69      ** informati
14e0d 6f 6e 20 66 72 6f 6d 20 74 68 61 74 20 6f 6e 65  on from that one
14e0e 20 63 68 69 6c 64 20 69 6e 74 6f 20 74 68 65 20   child into the 
14e0f 72 6f 6f 74 20 70 61 67 65 20 69 66 20 69 74 20  root page if it 
14e10 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 66 69 74  .    ** will fit
14e11 2e 20 20 54 68 69 73 20 72 65 64 75 63 65 73 20  .  This reduces 
14e12 74 68 65 20 64 65 70 74 68 20 6f 66 20 74 68 65  the depth of the
14e13 20 74 72 65 65 20 62 79 20 6f 6e 65 2e 0a 20 20   tree by one..  
14e14 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74    **.    ** If t
14e15 68 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20  he root page is 
14e16 70 61 67 65 20 31 2c 20 69 74 20 68 61 73 20 6c  page 1, it has l
14e17 65 73 73 20 73 70 61 63 65 20 61 76 61 69 6c 61  ess space availa
14e18 62 6c 65 20 74 68 61 6e 0a 20 20 20 20 2a 2a 20  ble than.    ** 
14e19 69 74 73 20 63 68 69 6c 64 20 28 64 75 65 20 74  its child (due t
14e1a 6f 20 74 68 65 20 31 30 30 20 62 79 74 65 20 68  o the 100 byte h
14e1b 65 61 64 65 72 20 74 68 61 74 20 6f 63 63 75 72  eader that occur
14e1c 73 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69  s at the beginni
14e1d 6e 67 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65  ng.    ** of the
14e1e 20 64 61 74 61 62 61 73 65 20 66 6c 65 29 2c 20   database fle), 
14e1f 73 6f 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 20  so it might not 
14e20 62 65 20 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20  be able to hold 
14e21 61 6c 6c 20 6f 66 20 74 68 65 20 0a 20 20 20 20  all of the .    
14e22 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 63  ** information c
14e23 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e  urrently contain
14e24 65 64 20 69 6e 20 74 68 65 20 63 68 69 6c 64 2e  ed in the child.
14e25 20 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65    If this is the
14e26 20 0a 20 20 20 20 2a 2a 20 63 61 73 65 2c 20 74   .    ** case, t
14e27 68 65 6e 20 64 6f 20 6e 6f 74 20 64 6f 20 74 68  hen do not do th
14e28 65 20 74 72 61 6e 73 66 65 72 2e 20 20 4c 65 61  e transfer.  Lea
14e29 76 65 20 70 61 67 65 20 31 20 65 6d 70 74 79 20  ve page 1 empty 
14e2a 65 78 63 65 70 74 0a 20 20 20 20 2a 2a 20 66 6f  except.    ** fo
14e2b 72 20 74 68 65 20 72 69 67 68 74 2d 70 6f 69 6e  r the right-poin
14e2c 74 65 72 20 74 6f 20 74 68 65 20 63 68 69 6c 64  ter to the child
14e2d 20 70 61 67 65 2e 20 20 54 68 65 20 63 68 69 6c   page.  The chil
14e2e 64 20 70 61 67 65 20 62 65 63 6f 6d 65 73 0a 20  d page becomes. 
14e2f 20 20 20 2a 2a 20 74 68 65 20 76 69 72 74 75 61     ** the virtua
14e30 6c 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 72  l root of the tr
14e31 65 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 56  ee..    */.    V
14e32 56 41 5f 4f 4e 4c 59 28 20 70 43 75 72 2d 3e 70  VA_ONLY( pCur->p
14e33 61 67 65 73 53 68 75 66 66 6c 65 64 20 3d 20 31  agesShuffled = 1
14e34 20 29 3b 0a 20 20 20 20 70 67 6e 6f 43 68 69 6c   );.    pgnoChil
14e35 64 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50  d = get4byte(&pP
14e36 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65  age->aData[pPage
14e37 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b  ->hdrOffset+8]);
14e38 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 67 6e  .    assert( pgn
14e39 6f 43 68 69 6c 64 3e 30 20 29 3b 0a 20 20 20 20  oChild>0 );.    
14e3a 61 73 73 65 72 74 28 20 70 67 6e 6f 43 68 69 6c  assert( pgnoChil
14e3b 64 3c 3d 70 61 67 65 72 50 61 67 65 63 6f 75 6e  d<=pagerPagecoun
14e3c 74 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 70 50  t(pPage->pBt->pP
14e3d 61 67 65 72 29 20 29 3b 0a 20 20 20 20 72 63 20  ager) );.    rc 
14e3e 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
14e3f 74 50 61 67 65 28 70 50 61 67 65 2d 3e 70 42 74  tPage(pPage->pBt
14e40 2c 20 70 67 6e 6f 43 68 69 6c 64 2c 20 26 70 43  , pgnoChild, &pC
14e41 68 69 6c 64 2c 20 30 29 3b 0a 20 20 20 20 69 66  hild, 0);.    if
14e42 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f  ( rc ) goto end_
14e43 73 68 61 6c 6c 6f 77 5f 62 61 6c 61 6e 63 65 3b  shallow_balance;
14e44 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e  .    if( pPage->
14e45 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20  pgno==1 ){.     
14e46 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
14e47 65 65 49 6e 69 74 50 61 67 65 28 70 43 68 69 6c  eeInitPage(pChil
14e48 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  d);.      if( rc
14e49 20 29 20 67 6f 74 6f 20 65 6e 64 5f 73 68 61 6c   ) goto end_shal
14e4a 6c 6f 77 5f 62 61 6c 61 6e 63 65 3b 0a 20 20 20  low_balance;.   
14e4b 20 20 20 61 73 73 65 72 74 28 20 70 43 68 69 6c     assert( pChil
14e4c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20  d->nOverflow==0 
14e4d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 43 68  );.      if( pCh
14e4e 69 6c 64 2d 3e 6e 46 72 65 65 3e 3d 31 30 30 20  ild->nFree>=100 
14e4f 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
14e50 65 20 63 68 69 6c 64 20 69 6e 66 6f 72 6d 61 74  e child informat
14e51 69 6f 6e 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20  ion will fit on 
14e52 74 68 65 20 72 6f 6f 74 20 70 61 67 65 2c 20 73  the root page, s
14e53 6f 20 64 6f 20 74 68 65 0a 20 20 20 20 20 20 20  o do the.       
14e54 20 2a 2a 20 63 6f 70 79 20 2a 2f 0a 20 20 20 20   ** copy */.    
14e55 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20      int i;.     
14e56 20 20 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67     zeroPage(pPag
14e57 65 2c 20 70 43 68 69 6c 64 2d 3e 61 44 61 74 61  e, pChild->aData
14e58 5b 30 5d 29 3b 0a 20 20 20 20 20 20 20 20 66 6f  [0]);.        fo
14e59 72 28 69 3d 30 3b 20 69 3c 70 43 68 69 6c 64 2d  r(i=0; i<pChild-
14e5a 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20  >nCell; i++){.  
14e5b 20 20 20 20 20 20 20 20 61 70 43 65 6c 6c 5b 69          apCell[i
14e5c 5d 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 43 68  ] = findCell(pCh
14e5d 69 6c 64 2c 69 29 3b 0a 20 20 20 20 20 20 20 20  ild,i);.        
14e5e 20 20 73 7a 43 65 6c 6c 5b 69 5d 20 3d 20 63 65    szCell[i] = ce
14e5f 6c 6c 53 69 7a 65 50 74 72 28 70 43 68 69 6c 64  llSizePtr(pChild
14e60 2c 20 61 70 43 65 6c 6c 5b 69 5d 29 3b 0a 20 20  , apCell[i]);.  
14e61 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
14e62 61 73 73 65 6d 62 6c 65 50 61 67 65 28 70 50 61  assemblePage(pPa
14e63 67 65 2c 20 70 43 68 69 6c 64 2d 3e 6e 43 65 6c  ge, pChild->nCel
14e64 6c 2c 20 61 70 43 65 6c 6c 2c 20 73 7a 43 65 6c  l, apCell, szCel
14e65 6c 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43  l);.        /* C
14e66 6f 70 79 20 74 68 65 20 72 69 67 68 74 2d 70 6f  opy the right-po
14e67 69 6e 74 65 72 20 6f 66 20 74 68 65 20 63 68 69  inter of the chi
14e68 6c 64 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74  ld to the parent
14e69 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 75 74  . */.        put
14e6a 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44  4byte(&pPage->aD
14e6b 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66  ata[pPage->hdrOf
14e6c 66 73 65 74 2b 38 5d 2c 20 0a 20 20 20 20 20 20  fset+8], .      
14e6d 20 20 20 20 20 20 67 65 74 34 62 79 74 65 28 26        get4byte(&
14e6e 70 43 68 69 6c 64 2d 3e 61 44 61 74 61 5b 70 43  pChild->aData[pC
14e6f 68 69 6c 64 2d 3e 68 64 72 4f 66 66 73 65 74 2b  hild->hdrOffset+
14e70 38 5d 29 29 3b 0a 20 20 20 20 20 20 20 20 66 72  8]));.        fr
14e71 65 65 50 61 67 65 28 70 43 68 69 6c 64 29 3b 0a  eePage(pChild);.
14e72 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22          TRACE(("
14e73 42 41 4c 41 4e 43 45 3a 20 63 68 69 6c 64 20 25  BALANCE: child %
14e74 64 20 74 72 61 6e 73 66 65 72 20 74 6f 20 70 61  d transfer to pa
14e75 67 65 20 31 5c 6e 22 2c 20 70 43 68 69 6c 64 2d  ge 1\n", pChild-
14e76 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 7d  >pgno));.      }
14e77 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a  else{.        /*
14e78 20 54 68 65 20 63 68 69 6c 64 20 68 61 73 20 6d   The child has m
14e79 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ore information 
14e7a 74 68 61 74 20 77 69 6c 6c 20 66 69 74 20 6f 6e  that will fit on
14e7b 20 74 68 65 20 72 6f 6f 74 2e 0a 20 20 20 20 20   the root..     
14e7c 20 20 20 2a 2a 20 54 68 65 20 74 72 65 65 20 69     ** The tree i
14e7d 73 20 61 6c 72 65 61 64 79 20 62 61 6c 61 6e 63  s already balanc
14e7e 65 64 2e 20 20 44 6f 20 6e 6f 74 68 69 6e 67 2e  ed.  Do nothing.
14e7f 20 2a 2f 0a 20 20 20 20 20 20 20 20 54 52 41 43   */.        TRAC
14e80 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 63 68 69  E(("BALANCE: chi
14e81 6c 64 20 25 64 20 77 69 6c 6c 20 6e 6f 74 20 66  ld %d will not f
14e82 69 74 20 6f 6e 20 70 61 67 65 20 31 5c 6e 22 2c  it on page 1\n",
14e83 20 70 43 68 69 6c 64 2d 3e 70 67 6e 6f 29 29 3b   pChild->pgno));
14e84 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
14e85 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79  se{.      memcpy
14e86 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 70  (pPage->aData, p
14e87 43 68 69 6c 64 2d 3e 61 44 61 74 61 2c 20 70 50  Child->aData, pP
14e88 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  age->pBt->usable
14e89 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 70 50 61  Size);.      pPa
14e8a 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a  ge->isInit = 0;.
14e8b 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
14e8c 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28  e3BtreeInitPage(
14e8d 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 61 73  pPage);.      as
14e8e 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
14e8f 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20 66 72 65  _OK );.      fre
14e90 65 50 61 67 65 28 70 43 68 69 6c 64 29 3b 0a 20  ePage(pChild);. 
14e91 20 20 20 20 20 54 52 41 43 45 28 28 22 42 41 4c       TRACE(("BAL
14e92 41 4e 43 45 3a 20 74 72 61 6e 73 66 65 72 20 63  ANCE: transfer c
14e93 68 69 6c 64 20 25 64 20 69 6e 74 6f 20 72 6f 6f  hild %d into roo
14e94 74 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20  t %d\n",.       
14e95 20 20 20 20 20 20 20 70 43 68 69 6c 64 2d 3e 70         pChild->p
14e96 67 6e 6f 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f  gno, pPage->pgno
14e97 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  ));.    }.    as
14e98 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76  sert( pPage->nOv
14e99 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 20  erflow==0 );.   
14e9a 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55   if( ISAUTOVACUU
14e9b 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  M ){.      rc = 
14e9c 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28  setChildPtrmaps(
14e9d 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20  pPage);.    }.  
14e9e 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43    releasePage(pC
14e9f 68 69 6c 64 29 3b 0a 20 20 7d 0a 65 6e 64 5f 73  hild);.  }.end_s
14ea0 68 61 6c 6c 6f 77 5f 62 61 6c 61 6e 63 65 3a 0a  hallow_balance:.
14ea1 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61    sqlite3_free(a
14ea2 70 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e  pCell);.  return
14ea3 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54   rc;.}.../*.** T
14ea4 68 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20  he root page is 
14ea5 6f 76 65 72 66 75 6c 6c 0a 2a 2a 0a 2a 2a 20 57  overfull.**.** W
14ea6 68 65 6e 20 74 68 69 73 20 68 61 70 70 65 6e 73  hen this happens
14ea7 2c 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 63  , Create a new c
14ea8 68 69 6c 64 20 70 61 67 65 20 61 6e 64 20 63 6f  hild page and co
14ea9 70 79 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e  py the.** conten
14eaa 74 73 20 6f 66 20 74 68 65 20 72 6f 6f 74 20 69  ts of the root i
14eab 6e 74 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 20  nto the child.  
14eac 54 68 65 6e 20 6d 61 6b 65 20 74 68 65 20 72 6f  Then make the ro
14ead 6f 74 0a 2a 2a 20 70 61 67 65 20 61 6e 20 65 6d  ot.** page an em
14eae 70 74 79 20 70 61 67 65 20 77 69 74 68 20 72 69  pty page with ri
14eaf 67 68 74 43 68 69 6c 64 20 70 6f 69 6e 74 69 6e  ghtChild pointin
14eb0 67 20 74 6f 20 74 68 65 20 6e 65 77 0a 2a 2a 20  g to the new.** 
14eb1 63 68 69 6c 64 2e 20 20 20 46 69 6e 61 6c 6c 79  child.   Finally
14eb2 2c 20 63 61 6c 6c 20 62 61 6c 61 6e 63 65 5f 69  , call balance_i
14eb3 6e 74 65 72 6e 61 6c 28 29 20 6f 6e 20 74 68 65  nternal() on the
14eb4 20 6e 65 77 20 63 68 69 6c 64 0a 2a 2a 20 74 6f   new child.** to
14eb5 20 63 61 75 73 65 20 69 74 20 74 6f 20 73 70 6c   cause it to spl
14eb6 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  it..*/.static in
14eb7 74 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72  t balance_deeper
14eb8 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29  (BtCursor *pCur)
14eb9 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  {.  int rc;     
14eba 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
14ebb 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 75 62  n value from sub
14ebc 70 72 6f 63 65 64 75 72 65 73 20 2a 2f 0a 20 20  procedures */.  
14ebd 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 20  MemPage *pPage; 
14ebe 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
14ebf 6f 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  o the root page 
14ec0 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 43  */.  MemPage *pC
14ec1 68 69 6c 64 3b 20 20 20 20 2f 2a 20 50 6f 69 6e  hild;    /* Poin
14ec2 74 65 72 20 74 6f 20 61 20 6e 65 77 20 63 68 69  ter to a new chi
14ec3 6c 64 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e  ld page */.  Pgn
14ec4 6f 20 70 67 6e 6f 43 68 69 6c 64 3b 20 20 20 20  o pgnoChild;    
14ec5 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20   /* Page number 
14ec6 6f 66 20 74 68 65 20 6e 65 77 20 63 68 69 6c 64  of the new child
14ec7 20 70 61 67 65 20 2a 2f 0a 20 20 42 74 53 68 61   page */.  BtSha
14ec8 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20  red *pBt;       
14ec9 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 2a    /* The BTree *
14eca 2f 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69  /.  int usableSi
14ecb 7a 65 3b 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c  ze;     /* Total
14ecc 20 75 73 61 62 6c 65 20 73 69 7a 65 20 6f 66 20   usable size of 
14ecd 61 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a  a page */.  u8 *
14ece 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  data;           
14ecf 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68  /* Content of th
14ed0 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 2a 2f  e parent page */
14ed1 0a 20 20 75 38 20 2a 63 64 61 74 61 3b 20 20 20  .  u8 *cdata;   
14ed2 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e         /* Conten
14ed3 74 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 70  t of the child p
14ed4 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72  age */.  int hdr
14ed5 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
14ed6 4f 66 66 73 65 74 20 74 6f 20 70 61 67 65 20 68  Offset to page h
14ed7 65 61 64 65 72 20 69 6e 20 70 61 72 65 6e 74 20  eader in parent 
14ed8 2a 2f 0a 20 20 69 6e 74 20 63 62 72 6b 3b 20 20  */.  int cbrk;  
14ed9 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
14eda 65 74 20 74 6f 20 63 6f 6e 74 65 6e 74 20 6f 66  et to content of
14edb 20 66 69 72 73 74 20 63 65 6c 6c 20 69 6e 20 70   first cell in p
14edc 61 72 65 6e 74 20 2a 2f 0a 0a 20 20 61 73 73 65  arent */..  asse
14edd 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d  rt( pCur->iPage=
14ede 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
14edf 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d  pCur->apPage[0]-
14ee0 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 29 3b 0a  >nOverflow>0 );.
14ee1 0a 20 20 56 56 41 5f 4f 4e 4c 59 28 20 70 43 75  .  VVA_ONLY( pCu
14ee2 72 2d 3e 70 61 67 65 73 53 68 75 66 66 6c 65 64  r->pagesShuffled
14ee3 20 3d 20 31 20 29 3b 0a 20 20 70 50 61 67 65 20   = 1 );.  pPage 
14ee4 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30  = pCur->apPage[0
14ee5 5d 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 67 65  ];.  pBt = pPage
14ee6 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28  ->pBt;.  assert(
14ee7 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
14ee8 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
14ee9 29 3b 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61  );.  rc = alloca
14eea 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c  teBtreePage(pBt,
14eeb 20 26 70 43 68 69 6c 64 2c 20 26 70 67 6e 6f 43   &pChild, &pgnoC
14eec 68 69 6c 64 2c 20 70 50 61 67 65 2d 3e 70 67 6e  hild, pPage->pgn
14eed 6f 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20  o, 0);.  if( rc 
14eee 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 61  ) return rc;.  a
14eef 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
14ef0 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
14ef1 43 68 69 6c 64 2d 3e 70 44 62 50 61 67 65 29 20  Child->pDbPage) 
14ef2 29 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20  );.  usableSize 
14ef3 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  = pBt->usableSiz
14ef4 65 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67  e;.  data = pPag
14ef5 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20  e->aData;.  hdr 
14ef6 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  = pPage->hdrOffs
14ef7 65 74 3b 0a 20 20 63 62 72 6b 20 3d 20 67 65 74  et;.  cbrk = get
14ef8 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
14ef9 35 5d 29 3b 0a 20 20 63 64 61 74 61 20 3d 20 70  5]);.  cdata = p
14efa 43 68 69 6c 64 2d 3e 61 44 61 74 61 3b 0a 20 20  Child->aData;.  
14efb 6d 65 6d 63 70 79 28 63 64 61 74 61 2c 20 26 64  memcpy(cdata, &d
14efc 61 74 61 5b 68 64 72 5d 2c 20 70 50 61 67 65 2d  ata[hdr], pPage-
14efd 3e 63 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 70 50  >cellOffset+2*pP
14efe 61 67 65 2d 3e 6e 43 65 6c 6c 2d 68 64 72 29 3b  age->nCell-hdr);
14eff 0a 20 20 6d 65 6d 63 70 79 28 26 63 64 61 74 61  .  memcpy(&cdata
14f00 5b 63 62 72 6b 5d 2c 20 26 64 61 74 61 5b 63 62  [cbrk], &data[cb
14f01 72 6b 5d 2c 20 75 73 61 62 6c 65 53 69 7a 65 2d  rk], usableSize-
14f02 63 62 72 6b 29 3b 0a 20 20 0a 20 20 72 63 20 3d  cbrk);.  .  rc =
14f03 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69   sqlite3BtreeIni
14f04 74 50 61 67 65 28 70 43 68 69 6c 64 29 3b 0a 20  tPage(pChild);. 
14f05 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
14f06 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 43  OK ){.    int nC
14f07 6f 70 79 20 3d 20 70 50 61 67 65 2d 3e 6e 4f 76  opy = pPage->nOv
14f08 65 72 66 6c 6f 77 2a 73 69 7a 65 6f 66 28 70 50  erflow*sizeof(pP
14f09 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d 29 3b 0a  age->aOvfl[0]);.
14f0a 20 20 20 20 6d 65 6d 63 70 79 28 70 43 68 69 6c      memcpy(pChil
14f0b 64 2d 3e 61 4f 76 66 6c 2c 20 70 50 61 67 65 2d  d->aOvfl, pPage-
14f0c 3e 61 4f 76 66 6c 2c 20 6e 43 6f 70 79 29 3b 0a  >aOvfl, nCopy);.
14f0d 20 20 20 20 70 43 68 69 6c 64 2d 3e 6e 4f 76 65      pChild->nOve
14f0e 72 66 6c 6f 77 20 3d 20 70 50 61 67 65 2d 3e 6e  rflow = pPage->n
14f0f 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 69 66  Overflow;.    if
14f10 28 20 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66  ( pChild->nOverf
14f11 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 70 43 68  low ){.      pCh
14f12 69 6c 64 2d 3e 6e 46 72 65 65 20 3d 20 30 3b 0a  ild->nFree = 0;.
14f13 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
14f14 28 20 70 43 68 69 6c 64 2d 3e 6e 43 65 6c 6c 3d  ( pChild->nCell=
14f15 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b  =pPage->nCell );
14f16 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70 50  .    zeroPage(pP
14f17 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e 61 44 61  age, pChild->aDa
14f18 74 61 5b 30 5d 20 26 20 7e 50 54 46 5f 4c 45 41  ta[0] & ~PTF_LEA
14f19 46 29 3b 0a 20 20 20 20 70 75 74 34 62 79 74 65  F);.    put4byte
14f1a 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70  (&pPage->aData[p
14f1b 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b  Page->hdrOffset+
14f1c 38 5d 2c 20 70 67 6e 6f 43 68 69 6c 64 29 3b 0a  8], pgnoChild);.
14f1d 20 20 20 20 54 52 41 43 45 28 28 22 42 41 4c 41      TRACE(("BALA
14f1e 4e 43 45 3a 20 63 6f 70 79 20 72 6f 6f 74 20 25  NCE: copy root %
14f1f 64 20 69 6e 74 6f 20 25 64 5c 6e 22 2c 20 70 50  d into %d\n", pP
14f20 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 43 68 69 6c  age->pgno, pChil
14f21 64 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 69  d->pgno));.    i
14f22 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20  f( ISAUTOVACUUM 
14f23 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74  ){.      rc = pt
14f24 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 43 68  rmapPut(pBt, pCh
14f25 69 6c 64 2d 3e 70 67 6e 6f 2c 20 50 54 52 4d 41  ild->pgno, PTRMA
14f26 50 5f 42 54 52 45 45 2c 20 70 50 61 67 65 2d 3e  P_BTREE, pPage->
14f27 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28  pgno);.      if(
14f28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
14f29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
14f2a 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 70  etChildPtrmaps(p
14f2b 43 68 69 6c 64 29 3b 0a 20 20 20 20 20 20 7d 0a  Child);.      }.
14f2c 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28      }.  }..  if(
14f2d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
14f2e 7b 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67  {.    pCur->iPag
14f2f 65 2b 2b 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61  e++;.    pCur->a
14f30 70 50 61 67 65 5b 31 5d 20 3d 20 70 43 68 69 6c  pPage[1] = pChil
14f31 64 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49  d;.    pCur->aiI
14f32 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 72  dx[0] = 0;.    r
14f33 63 20 3d 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72  c = balance_nonr
14f34 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 65 6c  oot(pCur);.  }el
14f35 73 65 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50  se{.    releaseP
14f36 61 67 65 28 70 43 68 69 6c 64 29 3b 0a 20 20 7d  age(pChild);.  }
14f37 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
14f38 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65  ../*.** The page
14f39 20 74 68 61 74 20 70 43 75 72 20 63 75 72 72 65   that pCur curre
14f3a 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 68  ntly points to h
14f3b 61 73 20 6a 75 73 74 20 62 65 65 6e 20 6d 6f 64  as just been mod
14f3c 69 66 69 65 64 20 69 6e 0a 2a 2a 20 73 6f 6d 65  ified in.** some
14f3d 20 77 61 79 2e 20 54 68 69 73 20 66 75 6e 63 74   way. This funct
14f3e 69 6f 6e 20 66 69 67 75 72 65 73 20 6f 75 74 20  ion figures out 
14f3f 69 66 20 74 68 69 73 20 6d 6f 64 69 66 69 63 61  if this modifica
14f40 74 69 6f 6e 20 6d 65 61 6e 73 20 74 68 65 0a 2a  tion means the.*
14f41 2a 20 74 72 65 65 20 6e 65 65 64 73 20 74 6f 20  * tree needs to 
14f42 62 65 20 62 61 6c 61 6e 63 65 64 2c 20 61 6e 64  be balanced, and
14f43 20 69 66 20 73 6f 20 63 61 6c 6c 73 20 74 68 65   if so calls the
14f44 20 61 70 70 72 6f 70 72 69 61 74 65 20 62 61 6c   appropriate bal
14f45 61 6e 63 69 6e 67 20 0a 2a 2a 20 72 6f 75 74 69  ancing .** routi
14f46 6e 65 2e 0a 2a 2a 20 0a 2a 2a 20 50 61 72 61 6d  ne..** .** Param
14f47 65 74 65 72 20 69 73 49 6e 73 65 72 74 20 69 73  eter isInsert is
14f48 20 74 72 75 65 20 69 66 20 61 20 6e 65 77 20 63   true if a new c
14f49 65 6c 6c 20 77 61 73 20 6a 75 73 74 20 69 6e 73  ell was just ins
14f4a 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 0a 2a  erted into the.*
14f4b 2a 20 70 61 67 65 2c 20 6f 72 20 66 61 6c 73 65  * page, or false
14f4c 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73   otherwise..*/.s
14f4d 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63  tatic int balanc
14f4e 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  e(BtCursor *pCur
14f4f 2c 20 69 6e 74 20 69 73 49 6e 73 65 72 74 29 7b  , int isInsert){
14f50 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
14f51 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 65  TE_OK;.  MemPage
14f52 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e   *pPage = pCur->
14f53 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
14f54 67 65 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ge];..  assert( 
14f55 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
14f56 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d  ld(pPage->pBt->m
14f57 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70  utex) );.  if( p
14f58 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b  Cur->iPage==0 ){
14f59 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
14f5a 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67  3PagerWrite(pPag
14f5b 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  e->pDbPage);.   
14f5c 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
14f5d 4f 4b 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f 76  OK && pPage->nOv
14f5e 65 72 66 6c 6f 77 3e 30 20 29 7b 0a 20 20 20 20  erflow>0 ){.    
14f5f 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 64    rc = balance_d
14f60 65 65 70 65 72 28 70 43 75 72 29 3b 0a 20 20 20  eeper(pCur);.   
14f61 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53   }.    if( rc==S
14f62 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67  QLITE_OK && pPag
14f63 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 7b 0a 20  e->nCell==0 ){. 
14f64 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63       rc = balanc
14f65 65 5f 73 68 61 6c 6c 6f 77 65 72 28 70 43 75 72  e_shallower(pCur
14f66 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
14f67 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d  {.    if( pPage-
14f68 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20  >nOverflow>0 || 
14f69 0a 20 20 20 20 20 20 20 20 28 21 69 73 49 6e 73  .        (!isIns
14f6a 65 72 74 20 26 26 20 70 50 61 67 65 2d 3e 6e 46  ert && pPage->nF
14f6b 72 65 65 3e 70 50 61 67 65 2d 3e 70 42 74 2d 3e  ree>pPage->pBt->
14f6c 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 29 20  usableSize*2/3) 
14f6d 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 62 61  ){.      rc = ba
14f6e 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 70 43  lance_nonroot(pC
14f6f 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ur);.    }.  }. 
14f70 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
14f71 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
14f72 65 20 63 68 65 63 6b 73 20 61 6c 6c 20 63 75 72  e checks all cur
14f73 73 6f 72 73 20 74 68 61 74 20 70 6f 69 6e 74 20  sors that point 
14f74 74 6f 20 74 61 62 6c 65 20 70 67 6e 6f 52 6f 6f  to table pgnoRoo
14f75 74 2e 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20  t..** If any of 
14f76 74 68 6f 73 65 20 63 75 72 73 6f 72 73 20 77 65  those cursors we
14f77 72 65 20 6f 70 65 6e 65 64 20 77 69 74 68 20 77  re opened with w
14f78 72 46 6c 61 67 3d 3d 30 20 69 6e 20 61 20 64 69  rFlag==0 in a di
14f79 66 66 65 72 65 6e 74 0a 2a 2a 20 64 61 74 61 62  fferent.** datab
14f7a 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28  ase connection (
14f7b 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  a database conne
14f7c 63 74 69 6f 6e 20 74 68 61 74 20 73 68 61 72 65  ction that share
14f7d 73 20 74 68 65 20 70 61 67 65 72 0a 2a 2a 20 63  s the pager.** c
14f7e 61 63 68 65 20 77 69 74 68 20 74 68 65 20 63 75  ache with the cu
14f7f 72 72 65 6e 74 20 63 6f 6e 6e 65 63 74 69 6f 6e  rrent connection
14f80 29 20 61 6e 64 20 74 68 61 74 20 6f 74 68 65 72  ) and that other
14f81 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20   connection .** 
14f82 69 73 20 6e 6f 74 20 69 6e 20 74 68 65 20 52 65  is not in the Re
14f83 61 64 55 6e 63 6f 6d 6d 6d 69 74 74 65 64 20 73  adUncommmitted s
14f84 74 61 74 65 2c 20 74 68 65 6e 20 74 68 69 73 20  tate, then this 
14f85 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
14f86 0a 2a 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  .** SQLITE_LOCKE
14f87 44 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 77 65 6c 6c  D..**.** As well
14f88 20 61 73 20 63 75 72 73 6f 72 73 20 77 69 74 68   as cursors with
14f89 20 77 72 46 6c 61 67 3d 3d 30 2c 20 63 75 72 73   wrFlag==0, curs
14f8a 6f 72 73 20 77 69 74 68 20 77 72 46 6c 61 67 3d  ors with wrFlag=
14f8b 3d 31 20 61 6e 64 20 0a 2a 2a 20 69 73 49 6e 63  =1 and .** isInc
14f8c 72 62 6c 6f 62 48 61 6e 64 6c 65 3d 3d 31 20 61  rblobHandle==1 a
14f8d 72 65 20 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72  re also consider
14f8e 65 64 20 27 72 65 61 64 27 20 63 75 72 73 6f 72  ed 'read' cursor
14f8f 73 2e 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 0a  s. Incremental .
14f90 2a 2a 20 62 6c 6f 62 20 63 75 72 73 6f 72 73 20  ** blob cursors 
14f91 61 72 65 20 75 73 65 64 20 66 6f 72 20 62 6f 74  are used for bot
14f92 68 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72  h reading and wr
14f93 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  iting..**.** Whe
14f94 6e 20 70 67 6e 6f 52 6f 6f 74 20 69 73 20 74 68  n pgnoRoot is th
14f95 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61  e root page of a
14f96 6e 20 69 6e 74 6b 65 79 20 74 61 62 6c 65 2c 20  n intkey table, 
14f97 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
14f98 20 61 6c 73 6f 0a 2a 2a 20 72 65 73 70 6f 6e 73   also.** respons
14f99 69 62 6c 65 20 66 6f 72 20 69 6e 76 61 6c 69 64  ible for invalid
14f9a 61 74 69 6e 67 20 69 6e 63 72 65 6d 65 6e 74 61  ating incrementa
14f9b 6c 20 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 77  l blob cursors w
14f9c 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 72 6f  hen the table ro
14f9d 77 0a 2a 2a 20 6f 6e 20 77 68 69 63 68 20 74 68  w.** on which th
14f9e 65 79 20 61 72 65 20 6f 70 65 6e 65 64 20 69 73  ey are opened is
14f9f 20 64 65 6c 65 74 65 64 20 6f 72 20 6d 6f 64 69   deleted or modi
14fa0 66 69 65 64 2e 20 43 75 72 73 6f 72 73 20 61 72  fied. Cursors ar
14fa1 65 20 69 6e 76 61 6c 69 64 61 74 65 64 0a 2a 2a  e invalidated.**
14fa2 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68   according to th
14fa3 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 75 6c 65  e following rule
14fa4 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 57 68  s:.**.**   1) Wh
14fa5 65 6e 20 42 74 72 65 65 43 6c 65 61 72 54 61 62  en BtreeClearTab
14fa6 6c 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 74  le() is called t
14fa7 6f 20 63 6f 6d 70 6c 65 74 65 6c 79 20 64 65 6c  o completely del
14fa8 65 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ete the contents
14fa9 0a 2a 2a 20 20 20 20 20 20 6f 66 20 61 20 42 2d  .**      of a B-
14faa 54 72 65 65 20 74 61 62 6c 65 2c 20 70 45 78 63  Tree table, pExc
14fab 6c 75 64 65 20 69 73 20 73 65 74 20 74 6f 20 7a  lude is set to z
14fac 65 72 6f 20 61 6e 64 20 70 61 72 61 6d 65 74 65  ero and paramete
14fad 72 20 69 52 6f 77 20 69 73 20 0a 2a 2a 20 20 20  r iRow is .**   
14fae 20 20 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65     set to non-ze
14faf 72 6f 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ro. In this case
14fb0 20 61 6c 6c 20 69 6e 63 72 65 6d 65 6e 74 61 6c   all incremental
14fb1 20 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 6f 70   blob cursors op
14fb2 65 6e 0a 2a 2a 20 20 20 20 20 20 6f 6e 20 74 68  en.**      on th
14fb3 65 20 74 61 62 6c 65 20 72 6f 6f 74 65 64 20 61  e table rooted a
14fb4 74 20 70 67 6e 6f 52 6f 6f 74 20 61 72 65 20 69  t pgnoRoot are i
14fb5 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a  nvalidated..**.*
14fb6 2a 20 20 20 32 29 20 57 68 65 6e 20 42 74 72 65  *   2) When Btre
14fb7 65 49 6e 73 65 72 74 28 29 2c 20 42 74 72 65 65  eInsert(), Btree
14fb8 44 65 6c 65 74 65 28 29 20 6f 72 20 42 74 72 65  Delete() or Btre
14fb9 65 50 75 74 44 61 74 61 28 29 20 69 73 20 63 61  ePutData() is ca
14fba 6c 6c 65 64 20 74 6f 20 0a 2a 2a 20 20 20 20 20  lled to .**     
14fbb 20 6d 6f 64 69 66 79 20 61 20 74 61 62 6c 65 20   modify a table 
14fbc 72 6f 77 20 76 69 61 20 61 6e 20 53 51 4c 20 73  row via an SQL s
14fbd 74 61 74 65 6d 65 6e 74 2c 20 70 45 78 63 6c 75  tatement, pExclu
14fbe 64 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  de is set to the
14fbf 20 0a 2a 2a 20 20 20 20 20 20 77 72 69 74 65 20   .**      write 
14fc0 63 75 72 73 6f 72 20 75 73 65 64 20 74 6f 20 64  cursor used to d
14fc1 6f 20 74 68 65 20 6d 6f 64 69 66 69 63 61 74 69  o the modificati
14fc2 6f 6e 20 61 6e 64 20 70 61 72 61 6d 65 74 65 72  on and parameter
14fc3 20 69 52 6f 77 20 69 73 20 73 65 74 0a 2a 2a 20   iRow is set.** 
14fc4 20 20 20 20 20 74 6f 20 74 68 65 20 69 6e 74 65       to the inte
14fc5 67 65 72 20 72 6f 77 20 69 64 20 6f 66 20 74 68  ger row id of th
14fc6 65 20 42 2d 54 72 65 65 20 65 6e 74 72 79 20 62  e B-Tree entry b
14fc7 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e 20 55  eing modified. U
14fc8 6e 6c 65 73 73 0a 2a 2a 20 20 20 20 20 20 70 45  nless.**      pE
14fc9 78 63 6c 75 64 65 20 69 73 20 69 74 73 65 6c 66  xclude is itself
14fca 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20   an incremental 
14fcb 62 6c 6f 62 20 63 75 72 73 6f 72 2c 20 74 68 65  blob cursor, the
14fcc 6e 20 61 6c 6c 20 69 6e 63 72 65 6d 65 6e 74 61  n all incrementa
14fcd 6c 0a 2a 2a 20 20 20 20 20 20 62 6c 6f 62 20 63  l.**      blob c
14fce 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 72  ursors open on r
14fcf 6f 77 20 69 52 6f 77 20 6f 66 20 74 68 65 20 42  ow iRow of the B
14fd0 2d 54 72 65 65 20 61 72 65 20 69 6e 76 61 6c 69  -Tree are invali
14fd1 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 33  dated..**.**   3
14fd2 29 20 49 66 20 62 6f 74 68 20 70 45 78 63 6c 75  ) If both pExclu
14fd3 64 65 20 61 6e 64 20 69 52 6f 77 20 61 72 65 20  de and iRow are 
14fd4 73 65 74 20 74 6f 20 7a 65 72 6f 2c 20 6e 6f 20  set to zero, no 
14fd5 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62  incremental blob
14fd6 20 0a 2a 2a 20 20 20 20 20 20 63 75 72 73 6f 72   .**      cursor
14fd7 73 20 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65  s are invalidate
14fd8 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
14fd9 20 63 68 65 63 6b 52 65 61 64 4c 6f 63 6b 73 28   checkReadLocks(
14fda 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65  .  Btree *pBtree
14fdb 2c 20 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 52 6f  , .  Pgno pgnoRo
14fdc 6f 74 2c 20 0a 20 20 42 74 43 75 72 73 6f 72 20  ot, .  BtCursor 
14fdd 2a 70 45 78 63 6c 75 64 65 2c 0a 20 20 69 36 34  *pExclude,.  i64
14fde 20 69 52 6f 77 0a 29 7b 0a 20 20 42 74 43 75 72   iRow.){.  BtCur
14fdf 73 6f 72 20 2a 70 3b 0a 20 20 42 74 53 68 61 72  sor *p;.  BtShar
14fe0 65 64 20 2a 70 42 74 20 3d 20 70 42 74 72 65 65  ed *pBt = pBtree
14fe1 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33  ->pBt;.  sqlite3
14fe2 20 2a 64 62 20 3d 20 70 42 74 72 65 65 2d 3e 64   *db = pBtree->d
14fe3 62 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  b;.  assert( sql
14fe4 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75  ite3BtreeHoldsMu
14fe5 74 65 78 28 70 42 74 72 65 65 29 20 29 3b 0a 20  tex(pBtree) );. 
14fe6 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75 72   for(p=pBt->pCur
14fe7 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65  sor; p; p=p->pNe
14fe8 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 3d 3d  xt){.    if( p==
14fe9 70 45 78 63 6c 75 64 65 20 29 20 63 6f 6e 74 69  pExclude ) conti
14fea 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  nue;.    if( p->
14feb 70 67 6e 6f 52 6f 6f 74 21 3d 70 67 6e 6f 52 6f  pgnoRoot!=pgnoRo
14fec 6f 74 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 23  ot ) continue;.#
14fed 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
14fee 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20  IT_INCRBLOB.    
14fef 69 66 28 20 70 2d 3e 69 73 49 6e 63 72 62 6c 6f  if( p->isIncrblo
14ff0 62 48 61 6e 64 6c 65 20 26 26 20 28 20 0a 20 20  bHandle && ( .  
14ff1 20 20 20 20 20 20 20 28 21 70 45 78 63 6c 75 64         (!pExclud
14ff2 65 20 26 26 20 69 52 6f 77 29 0a 20 20 20 20 20  e && iRow).     
14ff3 20 7c 7c 20 28 70 45 78 63 6c 75 64 65 20 26 26   || (pExclude &&
14ff4 20 21 70 45 78 63 6c 75 64 65 2d 3e 69 73 49 6e   !pExclude->isIn
14ff5 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 26 26 20  crblobHandle && 
14ff6 70 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 69 52  p->info.nKey==iR
14ff7 6f 77 29 0a 20 20 20 20 29 29 7b 0a 20 20 20 20  ow).    )){.    
14ff8 20 20 70 2d 3e 65 53 74 61 74 65 20 3d 20 43 55    p->eState = CU
14ff9 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20  RSOR_INVALID;.  
14ffa 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 69    }.#endif.    i
14ffb 66 28 20 70 2d 3e 65 53 74 61 74 65 21 3d 43 55  f( p->eState!=CU
14ffc 52 53 4f 52 5f 56 41 4c 49 44 20 29 20 63 6f 6e  RSOR_VALID ) con
14ffd 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 70  tinue;.    if( p
14ffe 2d 3e 77 72 46 6c 61 67 3d 3d 30 20 0a 23 69 66  ->wrFlag==0 .#if
14fff 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
15000 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20 7c  _INCRBLOB.     |
15001 7c 20 70 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48  | p->isIncrblobH
15002 61 6e 64 6c 65 0a 23 65 6e 64 69 66 0a 20 20 20  andle.#endif.   
15003 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
15004 33 20 2a 64 62 4f 74 68 65 72 20 3d 20 70 2d 3e  3 *dbOther = p->
15005 70 42 74 72 65 65 2d 3e 64 62 3b 0a 20 20 20 20  pBtree->db;.    
15006 20 20 69 66 28 20 64 62 4f 74 68 65 72 3d 3d 30    if( dbOther==0
15007 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 28 64 62   ||.         (db
15008 4f 74 68 65 72 21 3d 64 62 20 26 26 20 28 64 62  Other!=db && (db
15009 4f 74 68 65 72 2d 3e 66 6c 61 67 73 20 26 20 53  Other->flags & S
1500a 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d  QLITE_ReadUncomm
1500b 69 74 74 65 64 29 3d 3d 30 29 20 29 7b 0a 20 20  itted)==0) ){.  
1500c 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
1500d 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20 20  ITE_LOCKED;.    
1500e 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
1500f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
15010 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72  ;.}../*.** Inser
15011 74 20 61 20 6e 65 77 20 72 65 63 6f 72 64 20 69  t a new record i
15012 6e 74 6f 20 74 68 65 20 42 54 72 65 65 2e 20 20  nto the BTree.  
15013 54 68 65 20 6b 65 79 20 69 73 20 67 69 76 65 6e  The key is given
15014 20 62 79 20 28 70 4b 65 79 2c 6e 4b 65 79 29 0a   by (pKey,nKey).
15015 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 20  ** and the data 
15016 69 73 20 67 69 76 65 6e 20 62 79 20 28 70 44 61  is given by (pDa
15017 74 61 2c 6e 44 61 74 61 29 2e 20 20 54 68 65 20  ta,nData).  The 
15018 63 75 72 73 6f 72 20 69 73 20 75 73 65 64 20 6f  cursor is used o
15019 6e 6c 79 20 74 6f 0a 2a 2a 20 64 65 66 69 6e 65  nly to.** define
1501a 20 77 68 61 74 20 74 61 62 6c 65 20 74 68 65 20   what table the 
1501b 72 65 63 6f 72 64 20 73 68 6f 75 6c 64 20 62 65  record should be
1501c 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 2e 20   inserted into. 
1501d 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69   The cursor.** i
1501e 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20  s left pointing 
1501f 61 74 20 61 20 72 61 6e 64 6f 6d 20 6c 6f 63 61  at a random loca
15020 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  tion..**.** For 
15021 61 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 2c  an INTKEY table,
15022 20 6f 6e 6c 79 20 74 68 65 20 6e 4b 65 79 20 76   only the nKey v
15023 61 6c 75 65 20 6f 66 20 74 68 65 20 6b 65 79 20  alue of the key 
15024 69 73 20 75 73 65 64 2e 20 20 70 4b 65 79 20 69  is used.  pKey i
15025 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 20 46  s.** ignored.  F
15026 6f 72 20 61 20 5a 45 52 4f 44 41 54 41 20 74 61  or a ZERODATA ta
15027 62 6c 65 2c 20 74 68 65 20 70 44 61 74 61 20 61  ble, the pData a
15028 6e 64 20 6e 44 61 74 61 20 61 72 65 20 62 6f 74  nd nData are bot
15029 68 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 51  h ignored..*/.SQ
1502a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1502b 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73   sqlite3BtreeIns
1502c 65 72 74 28 0a 20 20 42 74 43 75 72 73 6f 72 20  ert(.  BtCursor 
1502d 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20 20 20  *pCur,          
1502e 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20        /* Insert 
1502f 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 74 61  data into the ta
15030 62 6c 65 20 6f 66 20 74 68 69 73 20 63 75 72 73  ble of this curs
15031 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  or */.  const vo
15032 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b  id *pKey, i64 nK
15033 65 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b 65  ey,    /* The ke
15034 79 20 6f 66 20 74 68 65 20 6e 65 77 20 72 65 63  y of the new rec
15035 6f 72 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  ord */.  const v
15036 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20  oid *pData, int 
15037 6e 44 61 74 61 2c 20 20 2f 2a 20 54 68 65 20 64  nData,  /* The d
15038 61 74 61 20 6f 66 20 74 68 65 20 6e 65 77 20 72  ata of the new r
15039 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  ecord */.  int n
1503a 5a 65 72 6f 2c 20 20 20 20 20 20 20 20 20 20 20  Zero,           
1503b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
1503c 62 65 72 20 6f 66 20 65 78 74 72 61 20 30 20 62  ber of extra 0 b
1503d 79 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20 74  ytes to append t
1503e 6f 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20  o data */.  int 
1503f 61 70 70 65 6e 64 42 69 61 73 20 20 20 20 20 20  appendBias      
15040 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
15041 75 65 20 69 66 20 74 68 69 73 20 69 73 20 6c 69  ue if this is li
15042 6b 65 6c 79 20 61 6e 20 61 70 70 65 6e 64 20 2a  kely an append *
15043 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  /.){.  int rc;. 
15044 20 69 6e 74 20 6c 6f 63 3b 0a 20 20 69 6e 74 20   int loc;.  int 
15045 73 7a 4e 65 77 3b 0a 20 20 69 6e 74 20 69 64 78  szNew;.  int idx
15046 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ;.  MemPage *pPa
15047 67 65 3b 0a 20 20 42 74 72 65 65 20 2a 70 20 3d  ge;.  Btree *p =
15048 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20   pCur->pBtree;. 
15049 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
1504a 20 70 2d 3e 70 42 74 3b 0a 20 20 75 6e 73 69 67   p->pBt;.  unsig
1504b 6e 65 64 20 63 68 61 72 20 2a 6f 6c 64 43 65 6c  ned char *oldCel
1504c 6c 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  l;.  unsigned ch
1504d 61 72 20 2a 6e 65 77 43 65 6c 6c 20 3d 20 30 3b  ar *newCell = 0;
1504e 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73  ..  assert( curs
1504f 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75  orHoldsMutex(pCu
15050 72 29 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d  r) );.  if( pBt-
15051 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d  >inTransaction!=
15052 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20  TRANS_WRITE ){. 
15053 20 20 20 2f 2a 20 4d 75 73 74 20 73 74 61 72 74     /* Must start
15054 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62   a transaction b
15055 65 66 6f 72 65 20 64 6f 69 6e 67 20 61 6e 20 69  efore doing an i
15056 6e 73 65 72 74 20 2a 2f 0a 20 20 20 20 72 63 20  nsert */.    rc 
15057 3d 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20  = pBt->readOnly 
15058 3f 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c  ? SQLITE_READONL
15059 59 20 3a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  Y : SQLITE_ERROR
1505a 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  ;.    return rc;
1505b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21  .  }.  assert( !
1505c 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b  pBt->readOnly );
1505d 0a 20 20 69 66 28 20 21 70 43 75 72 2d 3e 77 72  .  if( !pCur->wr
1505e 46 6c 61 67 20 29 7b 0a 20 20 20 20 72 65 74 75  Flag ){.    retu
1505f 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 20  rn SQLITE_PERM; 
15060 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 6f 74 20    /* Cursor not 
15061 6f 70 65 6e 20 66 6f 72 20 77 72 69 74 69 6e 67  open for writing
15062 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 63 68   */.  }.  if( ch
15063 65 63 6b 52 65 61 64 4c 6f 63 6b 73 28 70 43 75  eckReadLocks(pCu
15064 72 2d 3e 70 42 74 72 65 65 2c 20 70 43 75 72 2d  r->pBtree, pCur-
15065 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 2c  >pgnoRoot, pCur,
15066 20 6e 4b 65 79 29 20 29 7b 0a 20 20 20 20 72 65   nKey) ){.    re
15067 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  turn SQLITE_LOCK
15068 45 44 3b 20 2f 2a 20 54 68 65 20 74 61 62 6c 65  ED; /* The table
15069 20 70 43 75 72 20 70 6f 69 6e 74 73 20 74 6f 20   pCur points to 
1506a 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20  has a read lock 
1506b 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 75  */.  }.  if( pCu
1506c 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
1506d 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 72  R_FAULT ){.    r
1506e 65 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70  eturn pCur->skip
1506f 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 61 76 65  ;.  }..  /* Save
15070 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f   the positions o
15071 66 20 61 6e 79 20 6f 74 68 65 72 20 63 75 72 73  f any other curs
15072 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69 73  ors open on this
15073 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 71 6c 69   table */.  sqli
15074 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72  te3BtreeClearCur
15075 73 6f 72 28 70 43 75 72 29 3b 0a 20 20 69 66 28  sor(pCur);.  if(
15076 20 0a 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21   .    SQLITE_OK!
15077 3d 28 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75  =(rc = saveAllCu
15078 72 73 6f 72 73 28 70 42 74 2c 20 70 43 75 72 2d  rsors(pBt, pCur-
15079 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29  >pgnoRoot, pCur)
1507a 29 20 7c 7c 0a 20 20 20 20 53 51 4c 49 54 45 5f  ) ||.    SQLITE_
1507b 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65  OK!=(rc = sqlite
1507c 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75  3BtreeMoveto(pCu
1507d 72 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 61  r, pKey, nKey, a
1507e 70 70 65 6e 64 42 69 61 73 2c 20 26 6c 6f 63 29  ppendBias, &loc)
1507f 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ).  ){.    retur
15080 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 70 50 61  n rc;.  }..  pPa
15081 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67  ge = pCur->apPag
15082 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a  e[pCur->iPage];.
15083 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
15084 3e 69 6e 74 4b 65 79 20 7c 7c 20 6e 4b 65 79 3e  >intKey || nKey>
15085 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
15086 70 50 61 67 65 2d 3e 6c 65 61 66 20 7c 7c 20 21  pPage->leaf || !
15087 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b  pPage->intKey );
15088 0a 20 20 54 52 41 43 45 28 28 22 49 4e 53 45 52  .  TRACE(("INSER
15089 54 3a 20 74 61 62 6c 65 3d 25 64 20 6e 6b 65 79  T: table=%d nkey
1508a 3d 25 6c 6c 64 20 6e 64 61 74 61 3d 25 64 20 70  =%lld ndata=%d p
1508b 61 67 65 3d 25 64 20 25 73 5c 6e 22 2c 0a 20 20  age=%d %s\n",.  
1508c 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 70 67          pCur->pg
1508d 6e 6f 52 6f 6f 74 2c 20 6e 4b 65 79 2c 20 6e 44  noRoot, nKey, nD
1508e 61 74 61 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f  ata, pPage->pgno
1508f 2c 0a 20 20 20 20 20 20 20 20 20 20 6c 6f 63 3d  ,.          loc=
15090 3d 30 20 3f 20 22 6f 76 65 72 77 72 69 74 65 22  =0 ? "overwrite"
15091 20 3a 20 22 6e 65 77 20 65 6e 74 72 79 22 29 29   : "new entry"))
15092 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
15093 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 61  e->isInit );.  a
15094 6c 6c 6f 63 61 74 65 54 65 6d 70 53 70 61 63 65  llocateTempSpace
15095 28 70 42 74 29 3b 0a 20 20 6e 65 77 43 65 6c 6c  (pBt);.  newCell
15096 20 3d 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63   = pBt->pTmpSpac
15097 65 3b 0a 20 20 69 66 28 20 6e 65 77 43 65 6c 6c  e;.  if( newCell
15098 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ==0 ) return SQL
15099 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 72 63 20  ITE_NOMEM;.  rc 
1509a 3d 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 70 50 61  = fillInCell(pPa
1509b 67 65 2c 20 6e 65 77 43 65 6c 6c 2c 20 70 4b 65  ge, newCell, pKe
1509c 79 2c 20 6e 4b 65 79 2c 20 70 44 61 74 61 2c 20  y, nKey, pData, 
1509d 6e 44 61 74 61 2c 20 6e 5a 65 72 6f 2c 20 26 73  nData, nZero, &s
1509e 7a 4e 65 77 29 3b 0a 20 20 69 66 28 20 72 63 20  zNew);.  if( rc 
1509f 29 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72  ) goto end_inser
150a0 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a 4e  t;.  assert( szN
150a1 65 77 3d 3d 63 65 6c 6c 53 69 7a 65 50 74 72 28  ew==cellSizePtr(
150a2 70 50 61 67 65 2c 20 6e 65 77 43 65 6c 6c 29 20  pPage, newCell) 
150a3 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a 4e  );.  assert( szN
150a4 65 77 3c 3d 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45  ew<=MX_CELL_SIZE
150a5 28 70 42 74 29 20 29 3b 0a 20 20 69 64 78 20 3d  (pBt) );.  idx =
150a6 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
150a7 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 69 66 28  r->iPage];.  if(
150a8 20 6c 6f 63 3d 3d 30 20 26 26 20 43 55 52 53 4f   loc==0 && CURSO
150a9 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65  R_VALID==pCur->e
150aa 53 74 61 74 65 20 29 7b 0a 20 20 20 20 75 31 36  State ){.    u16
150ab 20 73 7a 4f 6c 64 3b 0a 20 20 20 20 61 73 73 65   szOld;.    asse
150ac 72 74 28 20 69 64 78 3c 70 50 61 67 65 2d 3e 6e  rt( idx<pPage->n
150ad 43 65 6c 6c 20 29 3b 0a 20 20 20 20 72 63 20 3d  Cell );.    rc =
150ae 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
150af 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  te(pPage->pDbPag
150b0 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  e);.    if( rc )
150b1 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64  {.      goto end
150b2 5f 69 6e 73 65 72 74 3b 0a 20 20 20 20 7d 0a 20  _insert;.    }. 
150b3 20 20 20 6f 6c 64 43 65 6c 6c 20 3d 20 66 69 6e     oldCell = fin
150b4 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78  dCell(pPage, idx
150b5 29 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67  );.    if( !pPag
150b6 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20  e->leaf ){.     
150b7 20 6d 65 6d 63 70 79 28 6e 65 77 43 65 6c 6c 2c   memcpy(newCell,
150b8 20 6f 6c 64 43 65 6c 6c 2c 20 34 29 3b 0a 20 20   oldCell, 4);.  
150b9 20 20 7d 0a 20 20 20 20 73 7a 4f 6c 64 20 3d 20    }.    szOld = 
150ba 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67  cellSizePtr(pPag
150bb 65 2c 20 6f 6c 64 43 65 6c 6c 29 3b 0a 20 20 20  e, oldCell);.   
150bc 20 72 63 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28   rc = clearCell(
150bd 70 50 61 67 65 2c 20 6f 6c 64 43 65 6c 6c 29 3b  pPage, oldCell);
150be 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f  .    if( rc ) go
150bf 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20  to end_insert;. 
150c0 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 50 61 67     dropCell(pPag
150c1 65 2c 20 69 64 78 2c 20 73 7a 4f 6c 64 29 3b 0a  e, idx, szOld);.
150c2 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 3c    }else if( loc<
150c3 30 20 26 26 20 70 50 61 67 65 2d 3e 6e 43 65 6c  0 && pPage->nCel
150c4 6c 3e 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  l>0 ){.    asser
150c5 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  t( pPage->leaf )
150c6 3b 0a 20 20 20 20 69 64 78 20 3d 20 2b 2b 70 43  ;.    idx = ++pC
150c7 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
150c8 69 50 61 67 65 5d 3b 0a 20 20 20 20 70 43 75 72  iPage];.    pCur
150c9 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30  ->info.nSize = 0
150ca 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69  ;.    pCur->vali
150cb 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 65 6c  dNKey = 0;.  }el
150cc 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
150cd 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20  pPage->leaf );. 
150ce 20 7d 0a 20 20 72 63 20 3d 20 69 6e 73 65 72 74   }.  rc = insert
150cf 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c  Cell(pPage, idx,
150d0 20 6e 65 77 43 65 6c 6c 2c 20 73 7a 4e 65 77 2c   newCell, szNew,
150d1 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63   0, 0);.  if( rc
150d2 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
150d3 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20  to end_insert;. 
150d4 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43   rc = balance(pC
150d5 75 72 2c 20 31 29 3b 0a 20 20 69 66 28 20 72 63  ur, 1);.  if( rc
150d6 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
150d7 20 20 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43     moveToRoot(pC
150d8 75 72 29 3b 0a 20 20 7d 0a 65 6e 64 5f 69 6e 73  ur);.  }.end_ins
150d9 65 72 74 3a 0a 20 20 72 65 74 75 72 6e 20 72 63  ert:.  return rc
150da 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74  ;.}../*.** Delet
150db 65 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74  e the entry that
150dc 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 70   the cursor is p
150dd 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 65  ointing to.  The
150de 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20 6c 65   cursor.** is le
150df 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61  ft pointing at a
150e0 20 61 72 62 69 74 72 61 72 79 20 6c 6f 63 61 74   arbitrary locat
150e1 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
150e2 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
150e3 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 42 74  e3BtreeDelete(Bt
150e4 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
150e5 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20   MemPage *pPage 
150e6 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70  = pCur->apPage[p
150e7 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 69  Cur->iPage];.  i
150e8 6e 74 20 69 64 78 3b 0a 20 20 75 6e 73 69 67 6e  nt idx;.  unsign
150e9 65 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 3b 0a  ed char *pCell;.
150ea 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 67 6e 6f    int rc;.  Pgno
150eb 20 70 67 6e 6f 43 68 69 6c 64 20 3d 20 30 3b 0a   pgnoChild = 0;.
150ec 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 43 75    Btree *p = pCu
150ed 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 42 74 53  r->pBtree;.  BtS
150ee 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
150ef 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  pBt;..  assert( 
150f0 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
150f1 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
150f2 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69  rt( pPage->isIni
150f3 74 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e  t );.  if( pBt->
150f4 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d 54  inTransaction!=T
150f5 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20  RANS_WRITE ){.  
150f6 20 20 2f 2a 20 4d 75 73 74 20 73 74 61 72 74 20    /* Must start 
150f7 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65  a transaction be
150f8 66 6f 72 65 20 64 6f 69 6e 67 20 61 20 64 65 6c  fore doing a del
150f9 65 74 65 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  ete */.    rc = 
150fa 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3f 20  pBt->readOnly ? 
150fb 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20  SQLITE_READONLY 
150fc 3a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  : SQLITE_ERROR;.
150fd 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
150fe 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 70 42   }.  assert( !pB
150ff 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20  t->readOnly );. 
15100 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74   if( pCur->eStat
15101 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20  e==CURSOR_FAULT 
15102 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 43  ){.    return pC
15103 75 72 2d 3e 73 6b 69 70 3b 0a 20 20 7d 0a 20 20  ur->skip;.  }.  
15104 69 66 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  if( pCur->aiIdx[
15105 70 43 75 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 50  pCur->iPage]>=pP
15106 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20  age->nCell ){.  
15107 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
15108 45 52 52 4f 52 3b 20 20 2f 2a 20 54 68 65 20 63  ERROR;  /* The c
15109 75 72 73 6f 72 20 69 73 20 6e 6f 74 20 70 6f 69  ursor is not poi
1510a 6e 74 69 6e 67 20 74 6f 20 61 6e 79 74 68 69 6e  nting to anythin
1510b 67 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 21  g */.  }.  if( !
1510c 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 29 7b 0a  pCur->wrFlag ){.
1510d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1510e 45 5f 50 45 52 4d 3b 20 20 20 2f 2a 20 44 69 64  E_PERM;   /* Did
1510f 20 6e 6f 74 20 6f 70 65 6e 20 74 68 69 73 20 63   not open this c
15110 75 72 73 6f 72 20 66 6f 72 20 77 72 69 74 69 6e  ursor for writin
15111 67 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 63  g */.  }.  if( c
15112 68 65 63 6b 52 65 61 64 4c 6f 63 6b 73 28 70 43  heckReadLocks(pC
15113 75 72 2d 3e 70 42 74 72 65 65 2c 20 70 43 75 72  ur->pBtree, pCur
15114 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72  ->pgnoRoot, pCur
15115 2c 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65  , pCur->info.nKe
15116 79 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  y) ){.    return
15117 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 20   SQLITE_LOCKED; 
15118 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 70 43 75  /* The table pCu
15119 72 20 70 6f 69 6e 74 73 20 74 6f 20 68 61 73 20  r points to has 
1511a 61 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20  a read lock */. 
1511b 20 7d 0a 0a 20 20 2f 2a 20 52 65 73 74 6f 72 65   }..  /* Restore
1511c 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 75 72   the current cur
1511d 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 28 61 20  sor position (a 
1511e 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 63 75 72  no-op if the cur
1511f 73 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 0a 20  sor is not in . 
15120 20 2a 2a 20 43 55 52 53 4f 52 5f 52 45 51 55 49   ** CURSOR_REQUI
15121 52 45 53 45 45 4b 20 73 74 61 74 65 29 20 61 6e  RESEEK state) an
15122 64 20 73 61 76 65 20 74 68 65 20 70 6f 73 69 74  d save the posit
15123 69 6f 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65  ions of any othe
15124 72 20 63 75 72 73 6f 72 73 20 0a 20 20 2a 2a 20  r cursors .  ** 
15125 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65  open on the same
15126 20 74 61 62 6c 65 2e 20 54 68 65 6e 20 63 61 6c   table. Then cal
15127 6c 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  l sqlite3PagerWr
15128 69 74 65 28 29 20 6f 6e 20 74 68 65 20 70 61 67  ite() on the pag
15129 65 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20  e.  ** that the 
1512a 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 20 64 65  entry will be de
1512b 6c 65 74 65 64 20 66 72 6f 6d 2e 0a 20 20 2a 2f  leted from..  */
1512c 0a 20 20 69 66 28 20 0a 20 20 20 20 28 72 63 20  .  if( .    (rc 
1512d 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50  = restoreCursorP
1512e 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 29 21 3d  osition(pCur))!=
1512f 30 20 7c 7c 0a 20 20 20 20 28 72 63 20 3d 20 73  0 ||.    (rc = s
15130 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42  aveAllCursors(pB
15131 74 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f  t, pCur->pgnoRoo
15132 74 2c 20 70 43 75 72 29 29 21 3d 30 20 7c 7c 0a  t, pCur))!=0 ||.
15133 20 20 20 20 28 72 63 20 3d 20 73 71 6c 69 74 65      (rc = sqlite
15134 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67  3PagerWrite(pPag
15135 65 2d 3e 70 44 62 50 61 67 65 29 29 21 3d 30 0a  e->pDbPage))!=0.
15136 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20    ){.    return 
15137 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f  rc;.  }..  /* Lo
15138 63 61 74 65 20 74 68 65 20 63 65 6c 6c 20 77 69  cate the cell wi
15139 74 68 69 6e 20 69 74 73 20 70 61 67 65 20 61 6e  thin its page an
1513a 64 20 6c 65 61 76 65 20 70 43 65 6c 6c 20 70 6f  d leave pCell po
1513b 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 0a 20 20  inting to the.  
1513c 2a 2a 20 64 61 74 61 2e 20 54 68 65 20 63 6c 65  ** data. The cle
1513d 61 72 43 65 6c 6c 28 29 20 63 61 6c 6c 20 66 72  arCell() call fr
1513e 65 65 73 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77  ees any overflow
1513f 20 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 65   pages associate
15140 64 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20  d with the.  ** 
15141 63 65 6c 6c 2e 20 54 68 65 20 63 65 6c 6c 20 69  cell. The cell i
15142 74 73 65 6c 66 20 69 73 20 73 74 69 6c 6c 20 69  tself is still i
15143 6e 74 61 63 74 2e 0a 20 20 2a 2f 0a 20 20 69 64  ntact..  */.  id
15144 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  x = pCur->aiIdx[
15145 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20  pCur->iPage];.  
15146 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c  pCell = findCell
15147 28 70 50 61 67 65 2c 20 69 64 78 29 3b 0a 20 20  (pPage, idx);.  
15148 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66  if( !pPage->leaf
15149 20 29 7b 0a 20 20 20 20 70 67 6e 6f 43 68 69 6c   ){.    pgnoChil
1514a 64 20 3d 20 67 65 74 34 62 79 74 65 28 70 43 65  d = get4byte(pCe
1514b 6c 6c 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  ll);.  }.  rc = 
1514c 63 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c  clearCell(pPage,
1514d 20 70 43 65 6c 6c 29 3b 0a 20 20 69 66 28 20 72   pCell);.  if( r
1514e 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  c ){.    return 
1514f 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21  rc;.  }..  if( !
15150 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20  pPage->leaf ){. 
15151 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65     /*.    ** The
15152 20 65 6e 74 72 79 20 77 65 20 61 72 65 20 61 62   entry we are ab
15153 6f 75 74 20 74 6f 20 64 65 6c 65 74 65 20 69 73  out to delete is
15154 20 6e 6f 74 20 61 20 6c 65 61 66 20 73 6f 20 69   not a leaf so i
15155 66 20 77 65 20 64 6f 20 6e 6f 74 0a 20 20 20 20  f we do not.    
15156 2a 2a 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20  ** do something 
15157 77 65 20 77 69 6c 6c 20 6c 65 61 76 65 20 61 20  we will leave a 
15158 68 6f 6c 65 20 6f 6e 20 61 6e 20 69 6e 74 65 72  hole on an inter
15159 6e 61 6c 20 70 61 67 65 2e 0a 20 20 20 20 2a 2a  nal page..    **
1515a 20 57 65 20 68 61 76 65 20 74 6f 20 66 69 6c 6c   We have to fill
1515b 20 74 68 65 20 68 6f 6c 65 20 62 79 20 6d 6f 76   the hole by mov
1515c 69 6e 67 20 69 6e 20 61 20 63 65 6c 6c 20 66 72  ing in a cell fr
1515d 6f 6d 20 61 20 6c 65 61 66 2e 20 20 54 68 65 0a  om a leaf.  The.
1515e 20 20 20 20 2a 2a 20 6e 65 78 74 20 43 65 6c 6c      ** next Cell
1515f 20 61 66 74 65 72 20 74 68 65 20 6f 6e 65 20 74   after the one t
15160 6f 20 62 65 20 64 65 6c 65 74 65 64 20 69 73 20  o be deleted is 
15161 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 65 78  guaranteed to ex
15162 69 73 74 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74  ist and.    ** t
15163 6f 20 62 65 20 61 20 6c 65 61 66 20 73 6f 20 77  o be a leaf so w
15164 65 20 63 61 6e 20 75 73 65 20 69 74 2e 0a 20 20  e can use it..  
15165 20 20 2a 2f 0a 20 20 20 20 42 74 43 75 72 73 6f    */.    BtCurso
15166 72 20 6c 65 61 66 43 75 72 3b 0a 20 20 20 20 4d  r leafCur;.    M
15167 65 6d 50 61 67 65 20 2a 70 4c 65 61 66 50 61 67  emPage *pLeafPag
15168 65 3b 0a 0a 20 20 20 20 75 6e 73 69 67 6e 65 64  e;..    unsigned
15169 20 63 68 61 72 20 2a 70 4e 65 78 74 3b 0a 20 20   char *pNext;.  
1516a 20 20 69 6e 74 20 6e 6f 74 55 73 65 64 3b 0a 20    int notUsed;. 
1516b 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72     unsigned char
1516c 20 2a 74 65 6d 70 43 65 6c 6c 20 3d 20 30 3b 0a   *tempCell = 0;.
1516d 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61      assert( !pPa
1516e 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 20 20  ge->intKey );.  
1516f 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65    sqlite3BtreeGe
15170 74 54 65 6d 70 43 75 72 73 6f 72 28 70 43 75 72  tTempCursor(pCur
15171 2c 20 26 6c 65 61 66 43 75 72 29 3b 0a 20 20 20  , &leafCur);.   
15172 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
15173 65 65 4e 65 78 74 28 26 6c 65 61 66 43 75 72 2c  eeNext(&leafCur,
15174 20 26 6e 6f 74 55 73 65 64 29 3b 0a 20 20 20 20   &notUsed);.    
15175 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
15176 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  K ){.      asser
15177 74 28 20 6c 65 61 66 43 75 72 2e 61 69 49 64 78  t( leafCur.aiIdx
15178 5b 6c 65 61 66 43 75 72 2e 69 50 61 67 65 5d 3d  [leafCur.iPage]=
15179 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4c 65 61  =0 );.      pLea
1517a 66 50 61 67 65 20 3d 20 6c 65 61 66 43 75 72 2e  fPage = leafCur.
1517b 61 70 50 61 67 65 5b 6c 65 61 66 43 75 72 2e 69  apPage[leafCur.i
1517c 50 61 67 65 5d 3b 0a 20 20 20 20 20 20 72 63 20  Page];.      rc 
1517d 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
1517e 69 74 65 28 70 4c 65 61 66 50 61 67 65 2d 3e 70  ite(pLeafPage->p
1517f 44 62 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20  DbPage);.    }. 
15180 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
15181 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e  E_OK ){.      in
15182 74 20 6c 65 61 66 43 75 72 73 6f 72 49 6e 76 61  t leafCursorInva
15183 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 75  lid = 0;.      u
15184 31 36 20 73 7a 4e 65 78 74 3b 0a 20 20 20 20 20  16 szNext;.     
15185 20 54 52 41 43 45 28 28 22 44 45 4c 45 54 45 3a   TRACE(("DELETE:
15186 20 74 61 62 6c 65 3d 25 64 20 64 65 6c 65 74 65   table=%d delete
15187 20 69 6e 74 65 72 6e 61 6c 20 66 72 6f 6d 20 25   internal from %
15188 64 20 72 65 70 6c 61 63 65 20 66 72 6f 6d 20 6c  d replace from l
15189 65 61 66 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20  eaf %d\n",.     
1518a 20 20 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f      pCur->pgnoRo
1518b 6f 74 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c  ot, pPage->pgno,
1518c 20 70 4c 65 61 66 50 61 67 65 2d 3e 70 67 6e 6f   pLeafPage->pgno
1518d 29 29 3b 0a 20 20 20 20 20 20 64 72 6f 70 43 65  ));.      dropCe
1518e 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c 20 63  ll(pPage, idx, c
1518f 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65  ellSizePtr(pPage
15190 2c 20 70 43 65 6c 6c 29 29 3b 0a 20 20 20 20 20  , pCell));.     
15191 20 70 4e 65 78 74 20 3d 20 66 69 6e 64 43 65 6c   pNext = findCel
15192 6c 28 70 4c 65 61 66 50 61 67 65 2c 20 30 29 3b  l(pLeafPage, 0);
15193 0a 20 20 20 20 20 20 73 7a 4e 65 78 74 20 3d 20  .      szNext = 
15194 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4c 65 61  cellSizePtr(pLea
15195 66 50 61 67 65 2c 20 70 4e 65 78 74 29 3b 0a 20  fPage, pNext);. 
15196 20 20 20 20 20 61 73 73 65 72 74 28 20 4d 58 5f       assert( MX_
15197 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 3e 3d  CELL_SIZE(pBt)>=
15198 73 7a 4e 65 78 74 2b 34 20 29 3b 0a 20 20 20 20  szNext+4 );.    
15199 20 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70 53 70    allocateTempSp
1519a 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 20 20  ace(pBt);.      
1519b 74 65 6d 70 43 65 6c 6c 20 3d 20 70 42 74 2d 3e  tempCell = pBt->
1519c 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 20 20 20  pTmpSpace;.     
1519d 20 69 66 28 20 74 65 6d 70 43 65 6c 6c 3d 3d 30   if( tempCell==0
1519e 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
1519f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
151a0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
151a1 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
151a2 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 69  {.        rc = i
151a3 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c  nsertCell(pPage,
151a4 20 69 64 78 2c 20 70 4e 65 78 74 2d 34 2c 20 73   idx, pNext-4, s
151a5 7a 4e 65 78 74 2b 34 2c 20 74 65 6d 70 43 65 6c  zNext+4, tempCel
151a6 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 0a  l, 0);.      }..
151a7 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 69  .      /* The "i
151a8 66 22 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20  f" statement in 
151a9 74 68 65 20 6e 65 78 74 20 63 6f 64 65 20 62 6c  the next code bl
151aa 6f 63 6b 20 69 73 20 63 72 69 74 69 63 61 6c 2e  ock is critical.
151ab 20 20 54 68 65 0a 20 20 20 20 20 20 2a 2a 20 73    The.      ** s
151ac 6c 69 67 68 74 65 73 74 20 65 72 72 6f 72 20 69  lightest error i
151ad 6e 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74  n that statement
151ae 20 77 6f 75 6c 64 20 61 6c 6c 6f 77 20 53 51 4c   would allow SQL
151af 69 74 65 20 74 6f 20 6f 70 65 72 61 74 65 0a 20  ite to operate. 
151b0 20 20 20 20 20 2a 2a 20 63 6f 72 72 65 63 74 6c       ** correctl
151b1 79 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 74 69  y most of the ti
151b2 6d 65 20 62 75 74 20 70 72 6f 64 75 63 65 20 76  me but produce v
151b3 65 72 79 20 72 61 72 65 20 66 61 69 6c 75 72 65  ery rare failure
151b4 73 2e 20 20 54 6f 0a 20 20 20 20 20 20 2a 2a 20  s.  To.      ** 
151b5 67 75 61 72 64 20 61 67 61 69 6e 73 74 20 74 68  guard against th
151b6 69 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  is, the followin
151b7 67 20 6d 61 63 72 6f 73 20 68 65 6c 70 20 74 6f  g macros help to
151b8 20 76 65 72 69 66 79 20 74 68 61 74 0a 20 20 20   verify that.   
151b9 20 20 20 2a 2a 20 74 68 65 20 22 69 66 22 20 73     ** the "if" s
151ba 74 61 74 65 6d 65 6e 74 20 69 73 20 77 65 6c 6c  tatement is well
151bb 20 74 65 73 74 65 64 2e 0a 20 20 20 20 20 20 2a   tested..      *
151bc 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
151bd 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c  ( pPage->nOverfl
151be 6f 77 3d 3d 30 20 26 26 20 70 50 61 67 65 2d 3e  ow==0 && pPage->
151bf 6e 46 72 65 65 3c 70 42 74 2d 3e 75 73 61 62 6c  nFree<pBt->usabl
151c0 65 53 69 7a 65 2a 32 2f 33 20 0a 20 20 20 20 20  eSize*2/3 .     
151c1 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 70              && p
151c2 4c 65 61 66 50 61 67 65 2d 3e 6e 46 72 65 65 2b  LeafPage->nFree+
151c3 32 2b 73 7a 4e 65 78 74 20 3e 20 70 42 74 2d 3e  2+szNext > pBt->
151c4 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 20 29  usableSize*2/3 )
151c5 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
151c6 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c  ( pPage->nOverfl
151c7 6f 77 3d 3d 30 20 26 26 20 70 50 61 67 65 2d 3e  ow==0 && pPage->
151c8 6e 46 72 65 65 3d 3d 70 42 74 2d 3e 75 73 61 62  nFree==pBt->usab
151c9 6c 65 53 69 7a 65 2a 32 2f 33 20 0a 20 20 20 20  leSize*2/3 .    
151ca 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20               && 
151cb 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 72 65 65  pLeafPage->nFree
151cc 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70 42 74 2d  +2+szNext > pBt-
151cd 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 20  >usableSize*2/3 
151ce 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
151cf 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  e( pPage->nOverf
151d0 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 67 65 2d  low==0 && pPage-
151d1 3e 6e 46 72 65 65 3d 3d 70 42 74 2d 3e 75 73 61  >nFree==pBt->usa
151d2 62 6c 65 53 69 7a 65 2a 32 2f 33 2b 31 20 0a 20  bleSize*2/3+1 . 
151d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
151d4 26 26 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46  && pLeafPage->nF
151d5 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70  ree+2+szNext > p
151d6 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32  Bt->usableSize*2
151d7 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  /3 );.      test
151d8 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76  case( pPage->nOv
151d9 65 72 66 6c 6f 77 3e 30 20 26 26 20 70 50 61 67  erflow>0 && pPag
151da 65 2d 3e 6e 46 72 65 65 3c 3d 70 42 74 2d 3e 75  e->nFree<=pBt->u
151db 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 0a 20 20  sableSize*2/3.  
151dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
151dd 26 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 72  & pLeafPage->nFr
151de 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70 42  ee+2+szNext > pB
151df 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f  t->usableSize*2/
151e0 33 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  3 );.      testc
151e1 61 73 65 28 20 28 70 50 61 67 65 2d 3e 6e 4f 76  ase( (pPage->nOv
151e2 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 28 70 50 61  erflow>0 || (pPa
151e3 67 65 2d 3e 6e 46 72 65 65 20 3e 20 70 42 74 2d  ge->nFree > pBt-
151e4 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 29  >usableSize*2/3)
151e5 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ).              
151e6 20 20 20 26 26 20 70 4c 65 61 66 50 61 67 65 2d     && pLeafPage-
151e7 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20  >nFree+2+szNext 
151e8 3d 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  == pBt->usableSi
151e9 7a 65 2a 32 2f 33 20 29 3b 0a 0a 0a 20 20 20 20  ze*2/3 );...    
151ea 20 20 69 66 28 20 28 70 50 61 67 65 2d 3e 6e 4f    if( (pPage->nO
151eb 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 28 70 50  verflow>0 || (pP
151ec 61 67 65 2d 3e 6e 46 72 65 65 20 3e 20 70 42 74  age->nFree > pBt
151ed 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33  ->usableSize*2/3
151ee 29 29 20 26 26 0a 20 20 20 20 20 20 20 20 20 20  )) &&.          
151ef 28 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 72 65  (pLeafPage->nFre
151f0 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70 42 74  e+2+szNext > pBt
151f1 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33  ->usableSize*2/3
151f2 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20  ).      ){.     
151f3 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63     /* This branc
151f4 68 20 69 73 20 74 61 6b 65 6e 20 69 66 20 74 68  h is taken if th
151f5 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20  e internal node 
151f6 69 73 20 6e 6f 77 20 65 69 74 68 65 72 20 6f 76  is now either ov
151f7 65 72 66 6c 6f 77 69 6e 67 0a 20 20 20 20 20 20  erflowing.      
151f8 20 20 2a 2a 20 6f 72 20 75 6e 64 65 72 66 75 6c    ** or underful
151f9 6c 20 61 6e 64 20 74 68 65 20 6c 65 61 66 20 6e  l and the leaf n
151fa 6f 64 65 20 77 69 6c 6c 20 62 65 20 75 6e 64 65  ode will be unde
151fb 72 66 75 6c 6c 20 61 66 74 65 72 20 74 68 65 20  rfull after the 
151fc 6a 75 73 74 20 63 65 6c 6c 20 0a 20 20 20 20 20  just cell .     
151fd 20 20 20 2a 2a 20 63 6f 70 69 65 64 20 74 6f 20     ** copied to 
151fe 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64  the internal nod
151ff 65 20 69 73 20 64 65 6c 65 74 65 64 20 66 72 6f  e is deleted fro
15200 6d 20 69 74 2e 20 54 68 69 73 20 69 73 20 61 20  m it. This is a 
15201 73 70 65 63 69 61 6c 0a 20 20 20 20 20 20 20 20  special.        
15202 2a 2a 20 63 61 73 65 20 62 65 63 61 75 73 65 20  ** case because 
15203 74 68 65 20 63 61 6c 6c 20 74 6f 20 62 61 6c 61  the call to bala
15204 6e 63 65 28 29 20 74 6f 20 63 6f 72 72 65 63 74  nce() to correct
15205 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f   the internal no
15206 64 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 61  de.        ** ma
15207 79 20 63 68 61 6e 67 65 20 74 68 65 20 74 72 65  y change the tre
15208 65 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20  e structure and 
15209 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 63  invalidate the c
1520a 6f 6e 74 65 6e 74 73 20 6f 66 0a 20 20 20 20 20  ontents of.     
1520b 20 20 20 2a 2a 20 74 68 65 20 6c 65 61 66 43 75     ** the leafCu
1520c 72 2e 61 70 50 61 67 65 5b 5d 20 61 6e 64 20 6c  r.apPage[] and l
1520d 65 61 66 43 75 72 2e 61 69 49 64 78 5b 5d 20 61  eafCur.aiIdx[] a
1520e 72 72 61 79 73 2c 20 77 68 69 63 68 20 77 69 6c  rrays, which wil
1520f 6c 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  l be.        ** 
15210 75 73 65 64 20 62 79 20 74 68 65 20 62 61 6c 61  used by the bala
15211 6e 63 65 28 29 20 72 65 71 75 69 72 65 64 20 74  nce() required t
15212 6f 20 63 6f 72 72 65 63 74 20 74 68 65 20 75 6e  o correct the un
15213 64 65 72 66 75 6c 6c 20 6c 65 61 66 0a 20 20 20  derfull leaf.   
15214 20 20 20 20 20 2a 2a 20 6e 6f 64 65 2e 0a 20 20       ** node..  
15215 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
15216 20 2a 2a 20 54 68 65 20 66 6f 72 6d 75 6c 61 20   ** The formula 
15217 75 73 65 64 20 69 6e 20 74 68 65 20 65 78 70 72  used in the expr
15218 65 73 73 69 6f 6e 20 61 62 6f 76 65 20 61 72 65  ession above are
15219 20 62 61 73 65 64 20 6f 6e 20 66 61 63 65 74 73   based on facets
1521a 20 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 74   of.        ** t
1521b 68 65 20 53 51 4c 69 74 65 20 66 69 6c 65 2d 66  he SQLite file-f
1521c 6f 72 6d 61 74 20 74 68 61 74 20 64 6f 20 6e 6f  ormat that do no
1521d 74 20 63 68 61 6e 67 65 20 6f 76 65 72 20 74 69  t change over ti
1521e 6d 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  me..        */. 
1521f 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28         testcase(
15220 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 3d 3d 70   pPage->nFree==p
15221 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32  Bt->usableSize*2
15222 2f 33 2b 31 20 29 3b 0a 20 20 20 20 20 20 20 20  /3+1 );.        
15223 74 65 73 74 63 61 73 65 28 20 70 4c 65 61 66 50  testcase( pLeafP
15224 61 67 65 2d 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e  age->nFree+2+szN
15225 65 78 74 3d 3d 70 42 74 2d 3e 75 73 61 62 6c 65  ext==pBt->usable
15226 53 69 7a 65 2a 32 2f 33 2b 31 20 29 3b 0a 20 20  Size*2/3+1 );.  
15227 20 20 20 20 20 20 6c 65 61 66 43 75 72 73 6f 72        leafCursor
15228 49 6e 76 61 6c 69 64 20 3d 20 31 3b 0a 20 20 20  Invalid = 1;.   
15229 20 20 20 7d 20 20 20 20 20 20 20 20 0a 0a 20 20     }        ..  
1522a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1522b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1522c 20 70 75 74 34 62 79 74 65 28 66 69 6e 64 4f 76   put4byte(findOv
1522d 65 72 66 6c 6f 77 43 65 6c 6c 28 70 50 61 67 65  erflowCell(pPage
1522e 2c 20 69 64 78 29 2c 20 70 67 6e 6f 43 68 69 6c  , idx), pgnoChil
1522f 64 29 3b 0a 20 20 20 20 20 20 20 20 56 56 41 5f  d);.        VVA_
15230 4f 4e 4c 59 28 20 70 43 75 72 2d 3e 70 61 67 65  ONLY( pCur->page
15231 73 53 68 75 66 66 6c 65 64 20 3d 20 30 20 29 3b  sShuffled = 0 );
15232 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 61  .        rc = ba
15233 6c 61 6e 63 65 28 70 43 75 72 2c 20 30 29 3b 0a  lance(pCur, 0);.
15234 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69        }..      i
15235 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
15236 20 26 26 20 6c 65 61 66 43 75 72 73 6f 72 49 6e   && leafCursorIn
15237 76 61 6c 69 64 20 29 7b 0a 20 20 20 20 20 20 20  valid ){.       
15238 20 2f 2a 20 54 68 65 20 6c 65 61 66 2d 6e 6f 64   /* The leaf-nod
15239 65 20 69 73 20 6e 6f 77 20 75 6e 64 65 72 66 75  e is now underfu
1523a 6c 6c 20 61 6e 64 20 73 6f 20 74 68 65 20 74 72  ll and so the tr
1523b 65 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 0a  ee needs to be .
1523c 20 20 20 20 20 20 20 20 2a 2a 20 72 65 62 61 6c          ** rebal
1523d 61 6e 63 65 64 2e 20 48 6f 77 65 76 65 72 2c 20  anced. However, 
1523e 74 68 65 20 62 61 6c 61 6e 63 65 28 29 20 6f 70  the balance() op
1523f 65 72 61 74 69 6f 6e 20 6f 6e 20 74 68 65 20 69  eration on the i
15240 6e 74 65 72 6e 61 6c 0a 20 20 20 20 20 20 20 20  nternal.        
15241 2a 2a 20 6e 6f 64 65 20 61 62 6f 76 65 20 6d 61  ** node above ma
15242 79 20 68 61 76 65 20 6d 6f 64 69 66 69 65 64 20  y have modified 
15243 74 68 65 20 73 74 72 75 63 74 75 72 65 20 6f 66  the structure of
15244 20 74 68 65 20 42 2d 54 72 65 65 20 61 6e 64 0a   the B-Tree and.
15245 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 20 74 68          ** so th
15246 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e  e current conten
15247 74 73 20 6f 66 20 6c 65 61 66 43 75 72 2e 61 70  ts of leafCur.ap
15248 50 61 67 65 5b 5d 20 61 6e 64 20 6c 65 61 66 43  Page[] and leafC
15249 75 72 2e 61 69 49 64 78 5b 5d 0a 20 20 20 20 20  ur.aiIdx[].     
1524a 20 20 20 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65     ** may not be
1524b 20 74 72 75 73 74 65 64 2e 0a 20 20 20 20 20 20   trusted..      
1524c 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
1524d 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62  It is not possib
1524e 6c 65 20 74 6f 20 63 6f 70 79 20 74 68 65 20 61  le to copy the a
1524f 6e 63 65 73 74 72 79 20 66 72 6f 6d 20 70 43 75  ncestry from pCu
15250 72 2c 20 61 73 20 74 68 65 20 73 61 6d 65 0a 20  r, as the same. 
15251 20 20 20 20 20 20 20 2a 2a 20 62 61 6c 61 6e 63         ** balanc
15252 65 28 29 20 63 61 6c 6c 20 68 61 73 20 69 6e 76  e() call has inv
15253 61 6c 69 64 61 74 65 64 20 74 68 65 20 70 43 75  alidated the pCu
15254 72 2d 3e 61 70 50 61 67 65 5b 5d 20 61 6e 64 20  r->apPage[] and 
15255 61 69 49 64 78 5b 5d 0a 20 20 20 20 20 20 20 20  aiIdx[].        
15256 2a 2a 20 61 72 72 61 79 73 2e 20 0a 20 20 20 20  ** arrays. .    
15257 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
15258 2a 20 54 68 65 20 63 61 6c 6c 20 74 6f 20 73 61  * The call to sa
15259 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  veCursorPosition
1525a 28 29 20 62 65 6c 6f 77 20 69 6e 74 65 72 6e 61  () below interna
1525b 6c 6c 79 20 73 61 76 65 73 20 74 68 65 20 0a 20  lly saves the . 
1525c 20 20 20 20 20 20 20 2a 2a 20 6b 65 79 20 74 68         ** key th
1525d 61 74 20 6c 65 61 66 43 75 72 20 69 73 20 63 75  at leafCur is cu
1525e 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67  rrently pointing
1525f 20 74 6f 2e 20 43 75 72 72 65 6e 74 6c 79 2c 20   to. Currently, 
15260 74 68 65 72 65 0a 20 20 20 20 20 20 20 20 2a 2a  there.        **
15261 20 61 72 65 20 74 77 6f 20 63 6f 70 69 65 73 20   are two copies 
15262 6f 66 20 74 68 61 74 20 6b 65 79 20 69 6e 20 74  of that key in t
15263 68 65 20 74 72 65 65 20 2d 20 6f 6e 65 20 68 65  he tree - one he
15264 72 65 20 6f 6e 20 74 68 65 20 6c 65 61 66 0a 20  re on the leaf. 
15265 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 61         ** page a
15266 6e 64 20 6f 6e 65 20 6f 6e 20 73 6f 6d 65 20 69  nd one on some i
15267 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 69 6e 20  nternal node in 
15268 74 68 65 20 74 72 65 65 2e 20 54 68 65 20 63 6f  the tree. The co
15269 70 79 20 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a  py on.        **
1526a 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 69   the leaf node i
1526b 73 20 61 6c 77 61 79 73 20 74 68 65 20 6e 65 78  s always the nex
1526c 74 20 6b 65 79 20 69 6e 20 74 72 65 65 2d 6f 72  t key in tree-or
1526d 64 65 72 20 61 66 74 65 72 20 74 68 65 20 0a 20  der after the . 
1526e 20 20 20 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f         ** copy o
1526f 6e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e  n the internal n
15270 6f 64 65 2e 20 53 6f 2c 20 74 68 65 20 63 61 6c  ode. So, the cal
15271 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65  l to sqlite3Btre
15272 65 4e 65 78 74 28 29 0a 20 20 20 20 20 20 20 20  eNext().        
15273 2a 2a 20 63 61 6c 6c 73 20 72 65 73 74 6f 72 65  ** calls restore
15274 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29  CursorPosition()
15275 20 74 6f 20 70 6f 69 6e 74 20 74 68 65 20 63 75   to point the cu
15276 72 73 6f 72 20 74 6f 20 74 68 65 20 63 6f 70 79  rsor to the copy
15277 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 74 6f 72  .        ** stor
15278 65 64 20 6f 6e 20 74 68 65 20 69 6e 74 65 72 6e  ed on the intern
15279 61 6c 20 6e 6f 64 65 2c 20 74 68 65 6e 20 61 64  al node, then ad
1527a 76 61 6e 63 65 73 20 74 6f 20 74 68 65 20 6e 65  vances to the ne
1527b 78 74 20 65 6e 74 72 79 2c 0a 20 20 20 20 20 20  xt entry,.      
1527c 20 20 2a 2a 20 77 68 69 63 68 20 68 61 70 70 65    ** which happe
1527d 6e 73 20 74 6f 20 62 65 20 74 68 65 20 63 6f 70  ns to be the cop
1527e 79 20 6f 66 20 74 68 65 20 6b 65 79 20 6f 6e 20  y of the key on 
1527f 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64  the internal nod
15280 65 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 65  e..        ** Ne
15281 74 20 65 66 66 65 63 74 3a 20 6c 65 61 66 43 75  t effect: leafCu
15282 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 62 61  r is pointing ba
15283 63 6b 20 74 6f 20 74 68 65 20 64 75 70 6c 69 63  ck to the duplic
15284 61 74 65 20 63 65 6c 6c 0a 20 20 20 20 20 20 20  ate cell.       
15285 20 2a 2a 20 74 68 61 74 20 6e 65 65 64 73 20 74   ** that needs t
15286 6f 20 62 65 20 72 65 6d 6f 76 65 64 2c 20 61 6e  o be removed, an
15287 64 20 74 68 65 20 6c 65 61 66 43 75 72 2e 61 70  d the leafCur.ap
15288 50 61 67 65 5b 5d 20 61 6e 64 0a 20 20 20 20 20  Page[] and.     
15289 20 20 20 2a 2a 20 6c 65 61 66 43 75 72 2e 61 69     ** leafCur.ai
1528a 49 64 78 5b 5d 20 61 72 72 61 79 73 20 61 72 65  Idx[] arrays are
1528b 20 63 6f 72 72 65 63 74 2e 0a 20 20 20 20 20 20   correct..      
1528c 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 56 56 41    */.        VVA
1528d 5f 4f 4e 4c 59 28 20 50 67 6e 6f 20 6c 65 61 66  _ONLY( Pgno leaf
1528e 50 67 6e 6f 20 3d 20 70 4c 65 61 66 50 61 67 65  Pgno = pLeafPage
1528f 2d 3e 70 67 6e 6f 20 29 3b 0a 20 20 20 20 20 20  ->pgno );.      
15290 20 20 72 63 20 3d 20 73 61 76 65 43 75 72 73 6f    rc = saveCurso
15291 72 50 6f 73 69 74 69 6f 6e 28 26 6c 65 61 66 43  rPosition(&leafC
15292 75 72 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ur);.        if(
15293 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
15294 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
15295 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78   sqlite3BtreeNex
15296 74 28 26 6c 65 61 66 43 75 72 2c 20 26 6e 6f 74  t(&leafCur, &not
15297 55 73 65 64 29 3b 0a 20 20 20 20 20 20 20 20 7d  Used);.        }
15298 0a 20 20 20 20 20 20 20 20 70 4c 65 61 66 50 61  .        pLeafPa
15299 67 65 20 3d 20 6c 65 61 66 43 75 72 2e 61 70 50  ge = leafCur.apP
1529a 61 67 65 5b 6c 65 61 66 43 75 72 2e 69 50 61 67  age[leafCur.iPag
1529b 65 5d 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  e];.        asse
1529c 72 74 28 20 70 4c 65 61 66 50 61 67 65 2d 3e 70  rt( pLeafPage->p
1529d 67 6e 6f 3d 3d 6c 65 61 66 50 67 6e 6f 20 29 3b  gno==leafPgno );
1529e 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1529f 20 6c 65 61 66 43 75 72 2e 61 69 49 64 78 5b 6c   leafCur.aiIdx[l
152a0 65 61 66 43 75 72 2e 69 50 61 67 65 5d 3d 3d 30  eafCur.iPage]==0
152a1 20 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20   );.      }..   
152a2 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
152a3 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
152a4 64 72 6f 70 43 65 6c 6c 28 70 4c 65 61 66 50 61  dropCell(pLeafPa
152a5 67 65 2c 20 30 2c 20 73 7a 4e 65 78 74 29 3b 0a  ge, 0, szNext);.
152a6 20 20 20 20 20 20 20 20 56 56 41 5f 4f 4e 4c 59          VVA_ONLY
152a7 28 20 6c 65 61 66 43 75 72 2e 70 61 67 65 73 53  ( leafCur.pagesS
152a8 68 75 66 66 6c 65 64 20 3d 20 30 20 29 3b 0a 20  huffled = 0 );. 
152a9 20 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61         rc = bala
152aa 6e 63 65 28 26 6c 65 61 66 43 75 72 2c 20 30 29  nce(&leafCur, 0)
152ab 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
152ac 28 20 6c 65 61 66 43 75 72 73 6f 72 49 6e 76 61  ( leafCursorInva
152ad 6c 69 64 20 7c 7c 20 21 6c 65 61 66 43 75 72 2e  lid || !leafCur.
152ae 70 61 67 65 73 53 68 75 66 66 6c 65 64 0a 20 20  pagesShuffled.  
152af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
152b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
152b1 20 7c 7c 20 21 70 43 75 72 2d 3e 70 61 67 65 73   || !pCur->pages
152b2 53 68 75 66 66 6c 65 64 20 29 3b 0a 20 20 20 20  Shuffled );.    
152b3 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71    }.    }.    sq
152b4 6c 69 74 65 33 42 74 72 65 65 52 65 6c 65 61 73  lite3BtreeReleas
152b5 65 54 65 6d 70 43 75 72 73 6f 72 28 26 6c 65 61  eTempCursor(&lea
152b6 66 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  fCur);.  }else{.
152b7 20 20 20 20 54 52 41 43 45 28 28 22 44 45 4c 45      TRACE(("DELE
152b8 54 45 3a 20 74 61 62 6c 65 3d 25 64 20 64 65 6c  TE: table=%d del
152b9 65 74 65 20 66 72 6f 6d 20 6c 65 61 66 20 25 64  ete from leaf %d
152ba 5c 6e 22 2c 0a 20 20 20 20 20 20 20 70 43 75 72  \n",.       pCur
152bb 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 50 61 67  ->pgnoRoot, pPag
152bc 65 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 64  e->pgno));.    d
152bd 72 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 69  ropCell(pPage, i
152be 64 78 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72 28  dx, cellSizePtr(
152bf 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 29 3b 0a  pPage, pCell));.
152c0 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65      rc = balance
152c1 28 70 43 75 72 2c 20 30 29 3b 0a 20 20 7d 0a 20  (pCur, 0);.  }. 
152c2 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
152c3 4f 4b 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f  OK ){.    moveTo
152c4 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a  Root(pCur);.  }.
152c5 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
152c6 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e  /*.** Create a n
152c7 65 77 20 42 54 72 65 65 20 74 61 62 6c 65 2e 20  ew BTree table. 
152c8 20 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 54   Write into *piT
152c9 61 62 6c 65 20 74 68 65 20 70 61 67 65 0a 2a 2a  able the page.**
152ca 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
152cb 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65  root page of the
152cc 20 6e 65 77 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a   new table..**.*
152cd 2a 20 54 68 65 20 74 79 70 65 20 6f 66 20 74 79  * The type of ty
152ce 70 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64  pe is determined
152cf 20 62 79 20 74 68 65 20 66 6c 61 67 73 20 70 61   by the flags pa
152d0 72 61 6d 65 74 65 72 2e 20 20 4f 6e 6c 79 20 74  rameter.  Only t
152d1 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20  he.** following 
152d2 76 61 6c 75 65 73 20 6f 66 20 66 6c 61 67 73 20  values of flags 
152d3 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e  are currently in
152d4 20 75 73 65 2e 20 20 4f 74 68 65 72 20 76 61 6c   use.  Other val
152d5 75 65 73 20 66 6f 72 0a 2a 2a 20 66 6c 61 67 73  ues for.** flags
152d6 20 6d 69 67 68 74 20 6e 6f 74 20 77 6f 72 6b 3a   might not work:
152d7 0a 2a 2a 0a 2a 2a 20 20 20 20 20 42 54 52 45 45  .**.**     BTREE
152d8 5f 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f 4c 45  _INTKEY|BTREE_LE
152d9 41 46 44 41 54 41 20 20 20 20 20 55 73 65 64 20  AFDATA     Used 
152da 66 6f 72 20 53 51 4c 20 74 61 62 6c 65 73 20 77  for SQL tables w
152db 69 74 68 20 72 6f 77 69 64 20 6b 65 79 73 0a 2a  ith rowid keys.*
152dc 2a 20 20 20 20 20 42 54 52 45 45 5f 5a 45 52 4f  *     BTREE_ZERO
152dd 44 41 54 41 20 20 20 20 20 20 20 20 20 20 20 20  DATA            
152de 20 20 20 20 20 20 55 73 65 64 20 66 6f 72 20 53        Used for S
152df 51 4c 20 69 6e 64 69 63 65 73 0a 2a 2f 0a 73 74  QL indices.*/.st
152e0 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 43 72  atic int btreeCr
152e1 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65 20  eateTable(Btree 
152e2 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c 65  *p, int *piTable
152e3 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20  , int flags){.  
152e4 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
152e5 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67  p->pBt;.  MemPag
152e6 65 20 2a 70 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f  e *pRoot;.  Pgno
152e7 20 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74   pgnoRoot;.  int
152e8 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   rc;..  assert( 
152e9 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
152ea 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 69  sMutex(p) );.  i
152eb 66 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61  f( pBt->inTransa
152ec 63 74 69 6f 6e 21 3d 54 52 41 4e 53 5f 57 52 49  ction!=TRANS_WRI
152ed 54 45 20 29 7b 0a 20 20 20 20 2f 2a 20 4d 75 73  TE ){.    /* Mus
152ee 74 20 73 74 61 72 74 20 61 20 74 72 61 6e 73 61  t start a transa
152ef 63 74 69 6f 6e 20 66 69 72 73 74 20 2a 2f 0a 20  ction first */. 
152f0 20 20 20 72 63 20 3d 20 70 42 74 2d 3e 72 65 61     rc = pBt->rea
152f1 64 4f 6e 6c 79 20 3f 20 53 51 4c 49 54 45 5f 52  dOnly ? SQLITE_R
152f2 45 41 44 4f 4e 4c 59 20 3a 20 53 51 4c 49 54 45  EADONLY : SQLITE
152f3 5f 45 52 52 4f 52 3b 0a 20 20 20 20 72 65 74 75  _ERROR;.    retu
152f4 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73  rn rc;.  }.  ass
152f5 65 72 74 28 20 21 70 42 74 2d 3e 72 65 61 64 4f  ert( !pBt->readO
152f6 6e 6c 79 20 29 3b 0a 0a 23 69 66 64 65 66 20 53  nly );..#ifdef S
152f7 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
152f8 41 43 55 55 4d 0a 20 20 72 63 20 3d 20 61 6c 6c  ACUUM.  rc = all
152f9 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70  ocateBtreePage(p
152fa 42 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e  Bt, &pRoot, &pgn
152fb 6f 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20  oRoot, 1, 0);.  
152fc 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65  if( rc ){.    re
152fd 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6c  turn rc;.  }.#el
152fe 73 65 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 75  se.  if( pBt->au
152ff 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20  toVacuum ){.    
15300 50 67 6e 6f 20 70 67 6e 6f 4d 6f 76 65 3b 20 20  Pgno pgnoMove;  
15301 20 20 20 20 2f 2a 20 4d 6f 76 65 20 61 20 70 61      /* Move a pa
15302 67 65 20 68 65 72 65 20 74 6f 20 6d 61 6b 65 20  ge here to make 
15303 72 6f 6f 6d 20 66 6f 72 20 74 68 65 20 72 6f 6f  room for the roo
15304 74 2d 70 61 67 65 20 2a 2f 0a 20 20 20 20 4d 65  t-page */.    Me
15305 6d 50 61 67 65 20 2a 70 50 61 67 65 4d 6f 76 65  mPage *pPageMove
15306 3b 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 6f  ; /* The page to
15307 20 6d 6f 76 65 20 74 6f 2e 20 2a 2f 0a 0a 20 20   move to. */..  
15308 20 20 2f 2a 20 43 72 65 61 74 69 6e 67 20 61 20    /* Creating a 
15309 6e 65 77 20 74 61 62 6c 65 20 6d 61 79 20 70 72  new table may pr
1530a 6f 62 61 62 6c 79 20 72 65 71 75 69 72 65 20 6d  obably require m
1530b 6f 76 69 6e 67 20 61 6e 20 65 78 69 73 74 69 6e  oving an existin
1530c 67 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a  g database.    *
1530d 2a 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20 66  * to make room f
1530e 6f 72 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  or the new table
1530f 73 20 72 6f 6f 74 20 70 61 67 65 2e 20 49 6e 20  s root page. In 
15310 63 61 73 65 20 74 68 69 73 20 70 61 67 65 20 74  case this page t
15311 75 72 6e 73 0a 20 20 20 20 2a 2a 20 6f 75 74 20  urns.    ** out 
15312 74 6f 20 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f  to be an overflo
15313 77 20 70 61 67 65 2c 20 64 65 6c 65 74 65 20 61  w page, delete a
15314 6c 6c 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  ll overflow page
15315 2d 6d 61 70 20 63 61 63 68 65 73 0a 20 20 20 20  -map caches.    
15316 2a 2a 20 68 65 6c 64 20 62 79 20 6f 70 65 6e 20  ** held by open 
15317 63 75 72 73 6f 72 73 2e 0a 20 20 20 20 2a 2f 0a  cursors..    */.
15318 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 41 6c      invalidateAl
15319 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70  lOverflowCache(p
1531a 42 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61  Bt);..    /* Rea
1531b 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d  d the value of m
1531c 65 74 61 5b 33 5d 20 66 72 6f 6d 20 74 68 65 20  eta[3] from the 
1531d 64 61 74 61 62 61 73 65 20 74 6f 20 64 65 74 65  database to dete
1531e 72 6d 69 6e 65 20 77 68 65 72 65 20 74 68 65 0a  rmine where the.
1531f 20 20 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65      ** root page
15320 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c   of the new tabl
15321 65 20 73 68 6f 75 6c 64 20 67 6f 2e 20 6d 65 74  e should go. met
15322 61 5b 33 5d 20 69 73 20 74 68 65 20 6c 61 72 67  a[3] is the larg
15323 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20  est root-page.  
15324 20 20 2a 2a 20 63 72 65 61 74 65 64 20 73 6f 20    ** created so 
15325 66 61 72 2c 20 73 6f 20 74 68 65 20 6e 65 77 20  far, so the new 
15326 72 6f 6f 74 2d 70 61 67 65 20 69 73 20 28 6d 65  root-page is (me
15327 74 61 5b 33 5d 2b 31 29 2e 0a 20 20 20 20 2a 2f  ta[3]+1)..    */
15328 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
15329 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 2c  3BtreeGetMeta(p,
1532a 20 34 2c 20 26 70 67 6e 6f 52 6f 6f 74 29 3b 0a   4, &pgnoRoot);.
1532b 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1532c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
1532d 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
1532e 20 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a      pgnoRoot++;.
1532f 0a 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20  .    /* The new 
15330 72 6f 6f 74 2d 70 61 67 65 20 6d 61 79 20 6e 6f  root-page may no
15331 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f  t be allocated o
15332 6e 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20  n a pointer-map 
15333 70 61 67 65 2c 20 6f 72 20 74 68 65 0a 20 20 20  page, or the.   
15334 20 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45   ** PENDING_BYTE
15335 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20   page..    */.  
15336 20 20 77 68 69 6c 65 28 20 70 67 6e 6f 52 6f 6f    while( pgnoRoo
15337 74 3d 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f  t==PTRMAP_PAGENO
15338 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 29 20  (pBt, pgnoRoot) 
15339 7c 7c 0a 20 20 20 20 20 20 20 20 70 67 6e 6f 52  ||.        pgnoR
1533a 6f 6f 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54  oot==PENDING_BYT
1533b 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20  E_PAGE(pBt) ){. 
1533c 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b       pgnoRoot++;
1533d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
1533e 74 28 20 70 67 6e 6f 52 6f 6f 74 3e 3d 33 20 29  t( pgnoRoot>=3 )
1533f 3b 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61  ;..    /* Alloca
15340 74 65 20 61 20 70 61 67 65 2e 20 54 68 65 20 70  te a page. The p
15341 61 67 65 20 74 68 61 74 20 63 75 72 72 65 6e 74  age that current
15342 6c 79 20 72 65 73 69 64 65 73 20 61 74 20 70 67  ly resides at pg
15343 6e 6f 52 6f 6f 74 20 77 69 6c 6c 0a 20 20 20 20  noRoot will.    
15344 2a 2a 20 62 65 20 6d 6f 76 65 64 20 74 6f 20 74  ** be moved to t
15345 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67  he allocated pag
15346 65 20 28 75 6e 6c 65 73 73 20 74 68 65 20 61 6c  e (unless the al
15347 6c 6f 63 61 74 65 64 20 70 61 67 65 20 68 61 70  located page hap
15348 70 65 6e 73 0a 20 20 20 20 2a 2a 20 74 6f 20 72  pens.    ** to r
15349 65 73 69 64 65 20 61 74 20 70 67 6e 6f 52 6f 6f  eside at pgnoRoo
1534a 74 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72  t)..    */.    r
1534b 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65  c = allocateBtre
1534c 65 50 61 67 65 28 70 42 74 2c 20 26 70 50 61 67  ePage(pBt, &pPag
1534d 65 4d 6f 76 65 2c 20 26 70 67 6e 6f 4d 6f 76 65  eMove, &pgnoMove
1534e 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 31 29 3b 0a  , pgnoRoot, 1);.
1534f 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
15350 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
15351 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
15352 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 4d 6f 76  .    if( pgnoMov
15353 65 21 3d 70 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20  e!=pgnoRoot ){. 
15354 20 20 20 20 20 2f 2a 20 70 67 6e 6f 52 6f 6f 74       /* pgnoRoot
15355 20 69 73 20 74 68 65 20 70 61 67 65 20 74 68 61   is the page tha
15356 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 66  t will be used f
15357 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65  or the root-page
15358 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65   of.      ** the
15359 20 6e 65 77 20 74 61 62 6c 65 20 28 61 73 73 75   new table (assu
1535a 6d 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 64 69  ming an error di
1535b 64 20 6e 6f 74 20 6f 63 63 75 72 29 2e 20 42 75  d not occur). Bu
1535c 74 20 77 65 20 77 65 72 65 0a 20 20 20 20 20 20  t we were.      
1535d 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 70 67 6e  ** allocated pgn
1535e 6f 4d 6f 76 65 2e 20 49 66 20 72 65 71 75 69 72  oMove. If requir
1535f 65 64 20 28 69 2e 65 2e 20 69 66 20 69 74 20 77  ed (i.e. if it w
15360 61 73 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 64  as not allocated
15361 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 65 78 74  .      ** by ext
15362 65 6e 64 69 6e 67 20 74 68 65 20 66 69 6c 65 29  ending the file)
15363 2c 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61  , the current pa
15364 67 65 20 61 74 20 70 6f 73 69 74 69 6f 6e 20 70  ge at position p
15365 67 6e 6f 4d 6f 76 65 0a 20 20 20 20 20 20 2a 2a  gnoMove.      **
15366 20 69 73 20 61 6c 72 65 61 64 79 20 6a 6f 75 72   is already jour
15367 6e 61 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a  naled..      */.
15368 20 20 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a        u8 eType;.
15369 20 20 20 20 20 20 50 67 6e 6f 20 69 50 74 72 50        Pgno iPtrP
1536a 61 67 65 3b 0a 0a 20 20 20 20 20 20 72 65 6c 65  age;..      rele
1536b 61 73 65 50 61 67 65 28 70 50 61 67 65 4d 6f 76  asePage(pPageMov
1536c 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4d 6f  e);..      /* Mo
1536d 76 65 20 74 68 65 20 70 61 67 65 20 63 75 72 72  ve the page curr
1536e 65 6e 74 6c 79 20 61 74 20 70 67 6e 6f 52 6f 6f  ently at pgnoRoo
1536f 74 20 74 6f 20 70 67 6e 6f 4d 6f 76 65 2e 20 2a  t to pgnoMove. *
15370 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  /.      rc = sql
15371 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
15372 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20  (pBt, pgnoRoot, 
15373 26 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 20  &pRoot, 0);.    
15374 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
15375 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
15376 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
15377 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72  }.      rc = ptr
15378 6d 61 70 47 65 74 28 70 42 74 2c 20 70 67 6e 6f  mapGet(pBt, pgno
15379 52 6f 6f 74 2c 20 26 65 54 79 70 65 2c 20 26 69  Root, &eType, &i
1537a 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20  PtrPage);.      
1537b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1537c 4b 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d  K || eType==PTRM
1537d 41 50 5f 52 4f 4f 54 50 41 47 45 20 7c 7c 20 65  AP_ROOTPAGE || e
1537e 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45  Type==PTRMAP_FRE
1537f 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20 20  EPAGE ){.       
15380 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f   releasePage(pRo
15381 6f 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  ot);.        ret
15382 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
15383 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65 54        assert( eT
15384 79 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f 4f 54  ype!=PTRMAP_ROOT
15385 50 41 47 45 20 29 3b 0a 20 20 20 20 20 20 61 73  PAGE );.      as
15386 73 65 72 74 28 20 65 54 79 70 65 21 3d 50 54 52  sert( eType!=PTR
15387 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 3b 0a  MAP_FREEPAGE );.
15388 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
15389 65 33 50 61 67 65 72 57 72 69 74 65 28 70 52 6f  e3PagerWrite(pRo
1538a 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  ot->pDbPage);.  
1538b 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1538c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1538d 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f   releasePage(pRo
1538e 6f 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  ot);.        ret
1538f 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
15390 20 20 20 20 20 20 72 63 20 3d 20 72 65 6c 6f 63        rc = reloc
15391 61 74 65 50 61 67 65 28 70 42 74 2c 20 70 52 6f  atePage(pBt, pRo
15392 6f 74 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50  ot, eType, iPtrP
15393 61 67 65 2c 20 70 67 6e 6f 4d 6f 76 65 2c 20 30  age, pgnoMove, 0
15394 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  );.      release
15395 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 0a 20 20  Page(pRoot);..  
15396 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 74 68      /* Obtain th
15397 65 20 70 61 67 65 20 61 74 20 70 67 6e 6f 52 6f  e page at pgnoRo
15398 6f 74 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  ot */.      if( 
15399 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1539a 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
1539b 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
1539c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1539d 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
1539e 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f 74  pgnoRoot, &pRoot
1539f 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 0);.      if( 
153a0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
153a1 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
153a2 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
153a3 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
153a4 67 65 72 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e  gerWrite(pRoot->
153a5 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
153a6 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
153a7 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c  K ){.        rel
153a8 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b  easePage(pRoot);
153a9 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
153aa 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
153ab 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 52 6f  }else{.      pRo
153ac 6f 74 20 3d 20 70 50 61 67 65 4d 6f 76 65 3b 0a  ot = pPageMove;.
153ad 20 20 20 20 7d 20 0a 0a 20 20 20 20 2f 2a 20 55      } ..    /* U
153ae 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65  pdate the pointe
153af 72 2d 6d 61 70 20 61 6e 64 20 6d 65 74 61 2d 64  r-map and meta-d
153b0 61 74 61 20 77 69 74 68 20 74 68 65 20 6e 65 77  ata with the new
153b1 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65   root-page numbe
153b2 72 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70  r. */.    rc = p
153b3 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 67  trmapPut(pBt, pg
153b4 6e 6f 52 6f 6f 74 2c 20 50 54 52 4d 41 50 5f 52  noRoot, PTRMAP_R
153b5 4f 4f 54 50 41 47 45 2c 20 30 29 3b 0a 20 20 20  OOTPAGE, 0);.   
153b6 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
153b7 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f   releasePage(pRo
153b8 6f 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ot);.      retur
153b9 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
153ba 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
153bb 65 55 70 64 61 74 65 4d 65 74 61 28 70 2c 20 34  eUpdateMeta(p, 4
153bc 2c 20 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20  , pgnoRoot);.   
153bd 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
153be 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f   releasePage(pRo
153bf 6f 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ot);.      retur
153c0 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d  n rc;.    }..  }
153c1 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 61  else{.    rc = a
153c2 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65  llocateBtreePage
153c3 28 70 42 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70  (pBt, &pRoot, &p
153c4 67 6e 6f 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a  gnoRoot, 1, 0);.
153c5 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
153c6 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6e 64  urn rc;.  }.#end
153c7 69 66 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  if.  assert( sql
153c8 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65  ite3PagerIswrite
153c9 61 62 6c 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50  able(pRoot->pDbP
153ca 61 67 65 29 20 29 3b 0a 20 20 7a 65 72 6f 50 61  age) );.  zeroPa
153cb 67 65 28 70 52 6f 6f 74 2c 20 66 6c 61 67 73 20  ge(pRoot, flags 
153cc 7c 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 73  | PTF_LEAF);.  s
153cd 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
153ce 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29  (pRoot->pDbPage)
153cf 3b 0a 20 20 2a 70 69 54 61 62 6c 65 20 3d 20 28  ;.  *piTable = (
153d0 69 6e 74 29 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20  int)pgnoRoot;.  
153d1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
153d2 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
153d3 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
153d4 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 42  reeCreateTable(B
153d5 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 2a 70 69  tree *p, int *pi
153d6 54 61 62 6c 65 2c 20 69 6e 74 20 66 6c 61 67 73  Table, int flags
153d7 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  ){.  int rc;.  s
153d8 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
153d9 28 70 29 3b 0a 20 20 70 2d 3e 70 42 74 2d 3e 64  (p);.  p->pBt->d
153da 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 72 63 20  b = p->db;.  rc 
153db 3d 20 62 74 72 65 65 43 72 65 61 74 65 54 61 62  = btreeCreateTab
153dc 6c 65 28 70 2c 20 70 69 54 61 62 6c 65 2c 20 66  le(p, piTable, f
153dd 6c 61 67 73 29 3b 0a 20 20 73 71 6c 69 74 65 33  lags);.  sqlite3
153de 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
153df 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
153e0 2a 0a 2a 2a 20 45 72 61 73 65 20 74 68 65 20 67  *.** Erase the g
153e1 69 76 65 6e 20 64 61 74 61 62 61 73 65 20 70 61  iven database pa
153e2 67 65 20 61 6e 64 20 61 6c 6c 20 69 74 73 20 63  ge and all its c
153e3 68 69 6c 64 72 65 6e 2e 20 20 52 65 74 75 72 6e  hildren.  Return
153e4 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 74 6f 20  .** the page to 
153e5 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2f  the freelist..*/
153e6 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6c 65 61  .static int clea
153e7 72 44 61 74 61 62 61 73 65 50 61 67 65 28 0a 20  rDatabasePage(. 
153e8 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20   BtShared *pBt, 
153e9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
153ea 20 42 54 72 65 65 20 74 68 61 74 20 63 6f 6e 74   BTree that cont
153eb 61 69 6e 73 20 74 68 65 20 74 61 62 6c 65 20 2a  ains the table *
153ec 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20  /.  Pgno pgno,  
153ed 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
153ee 65 20 6e 75 6d 62 65 72 20 74 6f 20 63 6c 65 61  e number to clea
153ef 72 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a  r */.  MemPage *
153f0 70 50 61 72 65 6e 74 2c 20 20 20 20 20 2f 2a 20  pParent,     /* 
153f1 50 61 72 65 6e 74 20 70 61 67 65 2e 20 20 4e 55  Parent page.  NU
153f2 4c 4c 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 20  LL for the root 
153f3 2a 2f 0a 20 20 69 6e 74 20 66 72 65 65 50 61 67  */.  int freePag
153f4 65 46 6c 61 67 2c 20 20 20 20 20 2f 2a 20 44 65  eFlag,     /* De
153f5 61 6c 6c 6f 63 61 74 65 20 70 61 67 65 20 69 66  allocate page if
153f6 20 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a   true */.  int *
153f7 70 6e 43 68 61 6e 67 65 0a 29 7b 0a 20 20 4d 65  pnChange.){.  Me
153f8 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30  mPage *pPage = 0
153f9 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 6e  ;.  int rc;.  un
153fa 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65  signed char *pCe
153fb 6c 6c 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20  ll;.  int i;..  
153fc 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
153fd 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
153fe 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
153ff 70 67 6e 6f 3e 70 61 67 65 72 50 61 67 65 63 6f  pgno>pagerPageco
15400 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29  unt(pBt->pPager)
15401 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
15402 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
15403 50 54 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20  PT;.  }..  rc = 
15404 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70  getAndInitPage(p
15405 42 74 2c 20 70 67 6e 6f 2c 20 26 70 50 61 67 65  Bt, pgno, &pPage
15406 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f  );.  if( rc ) go
15407 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73 65  to cleardatabase
15408 70 61 67 65 5f 6f 75 74 3b 0a 20 20 66 6f 72 28  page_out;.  for(
15409 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43  i=0; i<pPage->nC
1540a 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70  ell; i++){.    p
1540b 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28  Cell = findCell(
1540c 70 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 69  pPage, i);.    i
1540d 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20  f( !pPage->leaf 
1540e 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 63 6c  ){.      rc = cl
1540f 65 61 72 44 61 74 61 62 61 73 65 50 61 67 65 28  earDatabasePage(
15410 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 70 43  pBt, get4byte(pC
15411 65 6c 6c 29 2c 20 70 50 61 67 65 2c 20 31 2c 20  ell), pPage, 1, 
15412 70 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 20  pnChange);.     
15413 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 63   if( rc ) goto c
15414 6c 65 61 72 64 61 74 61 62 61 73 65 70 61 67 65  leardatabasepage
15415 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _out;.    }.    
15416 72 63 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28 70  rc = clearCell(p
15417 50 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20  Page, pCell);.  
15418 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20    if( rc ) goto 
15419 63 6c 65 61 72 64 61 74 61 62 61 73 65 70 61 67  cleardatabasepag
1541a 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 69 66 28  e_out;.  }.  if(
1541b 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
1541c 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44  .    rc = clearD
1541d 61 74 61 62 61 73 65 50 61 67 65 28 70 42 74 2c  atabasePage(pBt,
1541e 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65   get4byte(&pPage
1541f 2d 3e 61 44 61 74 61 5b 38 5d 29 2c 20 70 50 61  ->aData[8]), pPa
15420 67 65 2c 20 31 2c 20 70 6e 43 68 61 6e 67 65 29  ge, 1, pnChange)
15421 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67  ;.    if( rc ) g
15422 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73  oto cleardatabas
15423 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 65 6c  epage_out;.  }el
15424 73 65 20 69 66 28 20 70 6e 43 68 61 6e 67 65 20  se if( pnChange 
15425 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
15426 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a  Page->intKey );.
15427 20 20 20 20 2a 70 6e 43 68 61 6e 67 65 20 2b 3d      *pnChange +=
15428 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20   pPage->nCell;. 
15429 20 7d 0a 20 20 69 66 28 20 66 72 65 65 50 61 67   }.  if( freePag
1542a 65 46 6c 61 67 20 29 7b 0a 20 20 20 20 72 63 20  eFlag ){.    rc 
1542b 3d 20 66 72 65 65 50 61 67 65 28 70 50 61 67 65  = freePage(pPage
1542c 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28  );.  }else if( (
1542d 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1542e 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44  rWrite(pPage->pD
1542f 62 50 61 67 65 29 29 3d 3d 30 20 29 7b 0a 20 20  bPage))==0 ){.  
15430 20 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67 65    zeroPage(pPage
15431 2c 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30  , pPage->aData[0
15432 5d 20 7c 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20  ] | PTF_LEAF);. 
15433 20 7d 0a 0a 63 6c 65 61 72 64 61 74 61 62 61 73   }..cleardatabas
15434 65 70 61 67 65 5f 6f 75 74 3a 0a 20 20 72 65 6c  epage_out:.  rel
15435 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b  easePage(pPage);
15436 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
15437 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c  ./*.** Delete al
15438 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72  l information fr
15439 6f 6d 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c  om a single tabl
1543a 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  e in the databas
1543b 65 2e 20 20 69 54 61 62 6c 65 20 69 73 0a 2a 2a  e.  iTable is.**
1543c 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
1543d 20 6f 66 20 74 68 65 20 72 6f 6f 74 20 6f 66 20   of the root of 
1543e 74 68 65 20 74 61 62 6c 65 2e 20 20 41 66 74 65  the table.  Afte
1543f 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  r this routine r
15440 65 74 75 72 6e 73 2c 0a 2a 2a 20 74 68 65 20 72  eturns,.** the r
15441 6f 6f 74 20 70 61 67 65 20 69 73 20 65 6d 70 74  oot page is empt
15442 79 2c 20 62 75 74 20 73 74 69 6c 6c 20 65 78 69  y, but still exi
15443 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  sts..**.** This 
15444 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 66 61 69  routine will fai
15445 6c 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4c 4f  l with SQLITE_LO
15446 43 4b 45 44 20 69 66 20 74 68 65 72 65 20 61 72  CKED if there ar
15447 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 72 65  e any open.** re
15448 61 64 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68  ad cursors on th
15449 65 20 74 61 62 6c 65 2e 20 20 4f 70 65 6e 20 77  e table.  Open w
1544a 72 69 74 65 20 63 75 72 73 6f 72 73 20 61 72 65  rite cursors are
1544b 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 0a 2a 2a   moved to the.**
1544c 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62   root of the tab
1544d 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 6e 43  le..**.** If pnC
1544e 68 61 6e 67 65 20 69 73 20 6e 6f 74 20 4e 55 4c  hange is not NUL
1544f 4c 2c 20 74 68 65 6e 20 74 61 62 6c 65 20 69 54  L, then table iT
15450 61 62 6c 65 20 6d 75 73 74 20 62 65 20 61 6e 20  able must be an 
15451 69 6e 74 6b 65 79 20 74 61 62 6c 65 2e 20 54 68  intkey table. Th
15452 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 76 61 6c  e.** integer val
15453 75 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ue pointed to by
15454 20 70 6e 43 68 61 6e 67 65 20 69 73 20 69 6e 63   pnChange is inc
15455 72 65 6d 65 6e 74 65 64 20 62 79 20 74 68 65 20  remented by the 
15456 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6e 74  number of.** ent
15457 72 69 65 73 20 69 6e 20 74 68 65 20 74 61 62 6c  ries in the tabl
15458 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
15459 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1545a 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28  BtreeClearTable(
1545b 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54  Btree *p, int iT
1545c 61 62 6c 65 2c 20 69 6e 74 20 2a 70 6e 43 68 61  able, int *pnCha
1545d 6e 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  nge){.  int rc;.
1545e 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
1545f 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69  = p->pBt;.  sqli
15460 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
15461 3b 0a 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d  ;.  pBt->db = p-
15462 3e 64 62 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e  >db;.  if( p->in
15463 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49  Trans!=TRANS_WRI
15464 54 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70  TE ){.    rc = p
15465 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3f 20 53  Bt->readOnly ? S
15466 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 3a  QLITE_READONLY :
15467 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
15468 20 7d 65 6c 73 65 20 69 66 28 20 28 72 63 20 3d   }else if( (rc =
15469 20 63 68 65 63 6b 52 65 61 64 4c 6f 63 6b 73 28   checkReadLocks(
1546a 70 2c 20 69 54 61 62 6c 65 2c 20 30 2c 20 31 29  p, iTable, 0, 1)
1546b 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  )!=SQLITE_OK ){.
1546c 20 20 20 20 2f 2a 20 6e 6f 74 68 69 6e 67 20 74      /* nothing t
1546d 6f 20 64 6f 20 2a 2f 0a 20 20 7d 65 6c 73 65 20  o do */.  }else 
1546e 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28  if( SQLITE_OK!=(
1546f 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73  rc = saveAllCurs
15470 6f 72 73 28 70 42 74 2c 20 69 54 61 62 6c 65 2c  ors(pBt, iTable,
15471 20 30 29 29 20 29 7b 0a 20 20 20 20 2f 2a 20 6e   0)) ){.    /* n
15472 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a  othing to do */.
15473 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
15474 3d 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 50  = clearDatabaseP
15475 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69  age(pBt, (Pgno)i
15476 54 61 62 6c 65 2c 20 30 2c 20 30 2c 20 70 6e 43  Table, 0, 0, pnC
15477 68 61 6e 67 65 29 3b 0a 20 20 7d 0a 20 20 73 71  hange);.  }.  sq
15478 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
15479 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  p);.  return rc;
1547a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 20  .}../*.** Erase 
1547b 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  all information 
1547c 69 6e 20 61 20 74 61 62 6c 65 20 61 6e 64 20 61  in a table and a
1547d 64 64 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74  dd the root of t
1547e 68 65 20 74 61 62 6c 65 20 74 6f 0a 2a 2a 20 74  he table to.** t
1547f 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 45 78  he freelist.  Ex
15480 63 65 70 74 2c 20 74 68 65 20 72 6f 6f 74 20 6f  cept, the root o
15481 66 20 74 68 65 20 70 72 69 6e 63 69 70 6c 65 20  f the principle 
15482 74 61 62 6c 65 20 28 74 68 65 20 6f 6e 65 20 6f  table (the one o
15483 6e 0a 2a 2a 20 70 61 67 65 20 31 29 20 69 73 20  n.** page 1) is 
15484 6e 65 76 65 72 20 61 64 64 65 64 20 74 6f 20 74  never added to t
15485 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a 0a  he freelist..**.
15486 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
15487 77 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 53  will fail with S
15488 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20  QLITE_LOCKED if 
15489 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 6f 70  there are any op
1548a 65 6e 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 6e  en.** cursors on
1548b 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a   the table..**.*
1548c 2a 20 49 66 20 41 55 54 4f 56 41 43 55 55 4d 20  * If AUTOVACUUM 
1548d 69 73 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 74  is enabled and t
1548e 68 65 20 70 61 67 65 20 61 74 20 69 54 61 62 6c  he page at iTabl
1548f 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6c 61 73  e is not the las
15490 74 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 69  t.** root page i
15491 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
15492 69 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 6c 61  ile, then the la
15493 73 74 20 72 6f 6f 74 20 70 61 67 65 20 0a 2a 2a  st root page .**
15494 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
15495 20 66 69 6c 65 20 69 73 20 6d 6f 76 65 64 20 69   file is moved i
15496 6e 74 6f 20 74 68 65 20 73 6c 6f 74 20 66 6f 72  nto the slot for
15497 6d 65 72 6c 79 20 6f 63 63 75 70 69 65 64 20 62  merly occupied b
15498 79 0a 2a 2a 20 69 54 61 62 6c 65 20 61 6e 64 20  y.** iTable and 
15499 74 68 61 74 20 6c 61 73 74 20 73 6c 6f 74 20 66  that last slot f
1549a 6f 72 6d 65 72 6c 79 20 6f 63 63 75 70 69 65 64  ormerly occupied
1549b 20 62 79 20 74 68 65 20 6c 61 73 74 20 72 6f 6f   by the last roo
1549c 74 20 70 61 67 65 0a 2a 2a 20 69 73 20 61 64 64  t page.** is add
1549d 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69  ed to the freeli
1549e 73 74 20 69 6e 73 74 65 61 64 20 6f 66 20 69 54  st instead of iT
1549f 61 62 6c 65 2e 20 20 49 6e 20 74 68 69 73 20 73  able.  In this s
154a0 61 79 2c 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20  ay, all.** root 
154a1 70 61 67 65 73 20 61 72 65 20 6b 65 70 74 20 61  pages are kept a
154a2 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  t the beginning 
154a3 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
154a4 66 69 6c 65 2c 20 77 68 69 63 68 0a 2a 2a 20 69  file, which.** i
154a5 73 20 6e 65 63 65 73 73 61 72 79 20 66 6f 72 20  s necessary for 
154a6 41 55 54 4f 56 41 43 55 55 4d 20 74 6f 20 77 6f  AUTOVACUUM to wo
154a7 72 6b 20 72 69 67 68 74 2e 20 20 2a 70 69 4d 6f  rk right.  *piMo
154a8 76 65 64 20 69 73 20 73 65 74 20 74 6f 20 74 68  ved is set to th
154a9 65 20 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65  e .** page numbe
154aa 72 20 74 68 61 74 20 75 73 65 64 20 74 6f 20 62  r that used to b
154ab 65 20 74 68 65 20 6c 61 73 74 20 72 6f 6f 74 20  e the last root 
154ac 70 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c 65  page in the file
154ad 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 6d   before.** the m
154ae 6f 76 65 2e 20 20 49 66 20 6e 6f 20 70 61 67 65  ove.  If no page
154af 20 67 65 74 73 20 6d 6f 76 65 64 2c 20 2a 70 69   gets moved, *pi
154b0 4d 6f 76 65 64 20 69 73 20 73 65 74 20 74 6f 20  Moved is set to 
154b1 30 2e 0a 2a 2a 20 54 68 65 20 6c 61 73 74 20 72  0..** The last r
154b2 6f 6f 74 20 70 61 67 65 20 69 73 20 72 65 63 6f  oot page is reco
154b3 72 64 65 64 20 69 6e 20 6d 65 74 61 5b 33 5d 20  rded in meta[3] 
154b4 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66  and the value of
154b5 0a 2a 2a 20 6d 65 74 61 5b 33 5d 20 69 73 20 75  .** meta[3] is u
154b6 70 64 61 74 65 64 20 62 79 20 74 68 69 73 20 70  pdated by this p
154b7 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61  rocedure..*/.sta
154b8 74 69 63 20 69 6e 74 20 62 74 72 65 65 44 72 6f  tic int btreeDro
154b9 70 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c  pTable(Btree *p,
154ba 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e 74   int iTable, int
154bb 20 2a 70 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e   *piMoved){.  in
154bc 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 20  t rc;.  MemPage 
154bd 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 42 74  *pPage = 0;.  Bt
154be 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
154bf 3e 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28  >pBt;..  assert(
154c0 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
154c1 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20  dsMutex(p) );.  
154c2 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d  if( p->inTrans!=
154c3 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20  TRANS_WRITE ){. 
154c4 20 20 20 72 65 74 75 72 6e 20 70 42 74 2d 3e 72     return pBt->r
154c5 65 61 64 4f 6e 6c 79 20 3f 20 53 51 4c 49 54 45  eadOnly ? SQLITE
154c6 5f 52 45 41 44 4f 4e 4c 59 20 3a 20 53 51 4c 49  _READONLY : SQLI
154c7 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20  TE_ERROR;.  }.. 
154c8 20 2f 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61   /* It is illega
154c9 6c 20 74 6f 20 64 72 6f 70 20 61 20 74 61 62 6c  l to drop a tabl
154ca 65 20 69 66 20 61 6e 79 20 63 75 72 73 6f 72 73  e if any cursors
154cb 20 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74 68 65   are open on the
154cc 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20  .  ** database. 
154cd 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20  This is because 
154ce 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d  in auto-vacuum m
154cf 6f 64 65 20 74 68 65 20 62 61 63 6b 65 6e 64 20  ode the backend 
154d0 6d 61 79 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f  may.  ** need to
154d1 20 6d 6f 76 65 20 61 6e 6f 74 68 65 72 20 72 6f   move another ro
154d2 6f 74 2d 70 61 67 65 20 74 6f 20 66 69 6c 6c 20  ot-page to fill 
154d3 61 20 67 61 70 20 6c 65 66 74 20 62 79 20 74 68  a gap left by th
154d4 65 20 64 65 6c 65 74 65 64 0a 20 20 2a 2a 20 72  e deleted.  ** r
154d5 6f 6f 74 20 70 61 67 65 2e 20 49 66 20 61 6e 20  oot page. If an 
154d6 6f 70 65 6e 20 63 75 72 73 6f 72 20 77 61 73 20  open cursor was 
154d7 75 73 69 6e 67 20 74 68 69 73 20 70 61 67 65 20  using this page 
154d8 61 20 70 72 6f 62 6c 65 6d 20 77 6f 75 6c 64 20  a problem would 
154d9 0a 20 20 2a 2a 20 6f 63 63 75 72 2e 0a 20 20 2a  .  ** occur..  *
154da 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 43 75  /.  if( pBt->pCu
154db 72 73 6f 72 20 29 7b 0a 20 20 20 20 72 65 74 75  rsor ){.    retu
154dc 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  rn SQLITE_LOCKED
154dd 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71  ;.  }..  rc = sq
154de 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
154df 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 54 61  e(pBt, (Pgno)iTa
154e0 62 6c 65 2c 20 26 70 50 61 67 65 2c 20 30 29 3b  ble, &pPage, 0);
154e1 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75  .  if( rc ) retu
154e2 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71  rn rc;.  rc = sq
154e3 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54  lite3BtreeClearT
154e4 61 62 6c 65 28 70 2c 20 69 54 61 62 6c 65 2c 20  able(p, iTable, 
154e5 30 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a  0);.  if( rc ){.
154e6 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
154e7 70 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75  pPage);.    retu
154e8 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a 70  rn rc;.  }..  *p
154e9 69 4d 6f 76 65 64 20 3d 20 30 3b 0a 0a 20 20 69  iMoved = 0;..  i
154ea 66 28 20 69 54 61 62 6c 65 3e 31 20 29 7b 0a 23  f( iTable>1 ){.#
154eb 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
154ec 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
154ed 20 72 63 20 3d 20 66 72 65 65 50 61 67 65 28 70   rc = freePage(p
154ee 50 61 67 65 29 3b 0a 20 20 20 20 72 65 6c 65 61  Page);.    relea
154ef 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 23  sePage(pPage);.#
154f0 65 6c 73 65 0a 20 20 20 20 69 66 28 20 70 42 74  else.    if( pBt
154f1 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a  ->autoVacuum ){.
154f2 20 20 20 20 20 20 50 67 6e 6f 20 6d 61 78 52 6f        Pgno maxRo
154f3 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 20 20 72 63  otPgno;.      rc
154f4 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
154f5 65 74 4d 65 74 61 28 70 2c 20 34 2c 20 26 6d 61  etMeta(p, 4, &ma
154f6 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a 20 20 20 20  xRootPgno);.    
154f7 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
154f8 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
154f9 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65  eleasePage(pPage
154fa 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
154fb 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  n rc;.      }.. 
154fc 20 20 20 20 20 69 66 28 20 69 54 61 62 6c 65 3d       if( iTable=
154fd 3d 6d 61 78 52 6f 6f 74 50 67 6e 6f 20 29 7b 0a  =maxRootPgno ){.
154fe 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68          /* If th
154ff 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72  e table being dr
15500 6f 70 70 65 64 20 69 73 20 74 68 65 20 74 61 62  opped is the tab
15501 6c 65 20 77 69 74 68 20 74 68 65 20 6c 61 72 67  le with the larg
15502 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20  est root-page.  
15503 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20        ** number 
15504 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c  in the database,
15505 20 70 75 74 20 74 68 65 20 72 6f 6f 74 20 70 61   put the root pa
15506 67 65 20 6f 6e 20 74 68 65 20 66 72 65 65 20 6c  ge on the free l
15507 69 73 74 2e 20 0a 20 20 20 20 20 20 20 20 2a 2f  ist. .        */
15508 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 72  .        rc = fr
15509 65 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  eePage(pPage);. 
1550a 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61         releasePa
1550b 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20  ge(pPage);.     
1550c 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1550d 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1550e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
1550f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
15510 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54  se{.        /* T
15511 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64  he table being d
15512 72 6f 70 70 65 64 20 64 6f 65 73 20 6e 6f 74 20  ropped does not 
15513 68 61 76 65 20 74 68 65 20 6c 61 72 67 65 73 74  have the largest
15514 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 20   root-page.     
15515 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 69 6e 20     ** number in 
15516 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 53 6f  the database. So
15517 20 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 74   move the page t
15518 68 61 74 20 64 6f 65 73 20 69 6e 74 6f 20 74 68  hat does into th
15519 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 67 61  e .        ** ga
1551a 70 20 6c 65 66 74 20 62 79 20 74 68 65 20 64 65  p left by the de
1551b 6c 65 74 65 64 20 72 6f 6f 74 2d 70 61 67 65 2e  leted root-page.
1551c 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
1551d 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4d 6f      MemPage *pMo
1551e 76 65 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65  ve;.        rele
1551f 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a  asePage(pPage);.
15520 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
15521 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
15522 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e  (pBt, maxRootPgn
15523 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 29 3b 0a 20  o, &pMove, 0);. 
15524 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
15525 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
15526 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
15527 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
15528 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65     rc = relocate
15529 50 61 67 65 28 70 42 74 2c 20 70 4d 6f 76 65 2c  Page(pBt, pMove,
1552a 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45   PTRMAP_ROOTPAGE
1552b 2c 20 30 2c 20 69 54 61 62 6c 65 2c 20 30 29 3b  , 0, iTable, 0);
1552c 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
1552d 50 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20  Page(pMove);.   
1552e 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
1552f 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
15530 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
15531 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
15532 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
15533 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 6d  eeGetPage(pBt, m
15534 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26 70 4d 6f  axRootPgno, &pMo
15535 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  ve, 0);.        
15536 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
15537 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  K ){.          r
15538 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
15539 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d    }.        rc =
1553a 20 66 72 65 65 50 61 67 65 28 70 4d 6f 76 65 29   freePage(pMove)
1553b 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73  ;.        releas
1553c 65 50 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20  ePage(pMove);.  
1553d 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1553e 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1553f 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
15540 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
15541 20 20 2a 70 69 4d 6f 76 65 64 20 3d 20 6d 61 78    *piMoved = max
15542 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 20 20  RootPgno;.      
15543 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20  }..      /* Set 
15544 74 68 65 20 6e 65 77 20 27 6d 61 78 2d 72 6f 6f  the new 'max-roo
15545 74 2d 70 61 67 65 27 20 76 61 6c 75 65 20 69 6e  t-page' value in
15546 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 65   the database he
15547 61 64 65 72 2e 20 54 68 69 73 0a 20 20 20 20 20  ader. This.     
15548 20 2a 2a 20 69 73 20 74 68 65 20 6f 6c 64 20 76   ** is the old v
15549 61 6c 75 65 20 6c 65 73 73 20 6f 6e 65 2c 20 6c  alue less one, l
1554a 65 73 73 20 6f 6e 65 20 6d 6f 72 65 20 69 66 20  ess one more if 
1554b 74 68 61 74 20 68 61 70 70 65 6e 73 20 74 6f 0a  that happens to.
1554c 20 20 20 20 20 20 2a 2a 20 62 65 20 61 20 72 6f        ** be a ro
1554d 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2c 20  ot-page number, 
1554e 6c 65 73 73 20 6f 6e 65 20 61 67 61 69 6e 20 69  less one again i
1554f 66 20 74 68 61 74 20 69 73 20 74 68 65 0a 20 20  f that is the.  
15550 20 20 20 20 2a 2a 20 50 45 4e 44 49 4e 47 5f 42      ** PENDING_B
15551 59 54 45 5f 50 41 47 45 2e 0a 20 20 20 20 20 20  YTE_PAGE..      
15552 2a 2f 0a 20 20 20 20 20 20 6d 61 78 52 6f 6f 74  */.      maxRoot
15553 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 69 66  Pgno--;.      if
15554 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50  ( maxRootPgno==P
15555 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
15556 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20  (pBt) ){.       
15557 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a   maxRootPgno--;.
15558 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
15559 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50  ( maxRootPgno==P
1555a 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74  TRMAP_PAGENO(pBt
1555b 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 20 29  , maxRootPgno) )
1555c 7b 0a 20 20 20 20 20 20 20 20 6d 61 78 52 6f 6f  {.        maxRoo
1555d 74 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 7d  tPgno--;.      }
1555e 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6d  .      assert( m
1555f 61 78 52 6f 6f 74 50 67 6e 6f 21 3d 50 45 4e 44  axRootPgno!=PEND
15560 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
15561 74 29 20 29 3b 0a 0a 20 20 20 20 20 20 72 63 20  t) );..      rc 
15562 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70  = sqlite3BtreeUp
15563 64 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 6d  dateMeta(p, 4, m
15564 61 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a 20 20 20  axRootPgno);.   
15565 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
15566 20 3d 20 66 72 65 65 50 61 67 65 28 70 50 61 67   = freePage(pPag
15567 65 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73  e);.      releas
15568 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
15569 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c    }.#endif.  }el
1556a 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 73 71  se{.    /* If sq
1556b 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61  lite3BtreeDropTa
1556c 62 6c 65 20 77 61 73 20 63 61 6c 6c 65 64 20 6f  ble was called o
1556d 6e 20 70 61 67 65 20 31 2e 20 2a 2f 0a 20 20 20  n page 1. */.   
1556e 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67 65 2c   zeroPage(pPage,
1556f 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f   PTF_INTKEY|PTF_
15570 4c 45 41 46 20 29 3b 0a 20 20 20 20 72 65 6c 65  LEAF );.    rele
15571 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a  asePage(pPage);.
15572 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
15573 20 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56    .}.SQLITE_PRIV
15574 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
15575 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74  treeDropTable(Bt
15576 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 62  ree *p, int iTab
15577 6c 65 2c 20 69 6e 74 20 2a 70 69 4d 6f 76 65 64  le, int *piMoved
15578 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  ){.  int rc;.  s
15579 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1557a 28 70 29 3b 0a 20 20 70 2d 3e 70 42 74 2d 3e 64  (p);.  p->pBt->d
1557b 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 72 63 20  b = p->db;.  rc 
1557c 3d 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c 65  = btreeDropTable
1557d 28 70 2c 20 69 54 61 62 6c 65 2c 20 70 69 4d 6f  (p, iTable, piMo
1557e 76 65 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  ved);.  sqlite3B
1557f 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
15580 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
15581 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 6d 65  *.** Read the me
15582 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f  ta-information o
15583 75 74 20 6f 66 20 61 20 64 61 74 61 62 61 73 65  ut of a database
15584 20 66 69 6c 65 2e 20 20 4d 65 74 61 5b 30 5d 0a   file.  Meta[0].
15585 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ** is the number
15586 20 6f 66 20 66 72 65 65 20 70 61 67 65 73 20 63   of free pages c
15587 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 20  urrently in the 
15588 64 61 74 61 62 61 73 65 2e 20 20 4d 65 74 61 5b  database.  Meta[
15589 31 5d 0a 2a 2a 20 74 68 72 6f 75 67 68 20 6d 65  1].** through me
1558a 74 61 5b 31 35 5d 20 61 72 65 20 61 76 61 69 6c  ta[15] are avail
1558b 61 62 6c 65 20 66 6f 72 20 75 73 65 20 62 79 20  able for use by 
1558c 68 69 67 68 65 72 20 6c 61 79 65 72 73 2e 20 20  higher layers.  
1558d 4d 65 74 61 5b 30 5d 0a 2a 2a 20 69 73 20 72 65  Meta[0].** is re
1558e 61 64 2d 6f 6e 6c 79 2c 20 74 68 65 20 6f 74 68  ad-only, the oth
1558f 65 72 73 20 61 72 65 20 72 65 61 64 2f 77 72 69  ers are read/wri
15590 74 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 73  te..** .** The s
15591 63 68 65 6d 61 20 6c 61 79 65 72 20 6e 75 6d 62  chema layer numb
15592 65 72 73 20 6d 65 74 61 20 76 61 6c 75 65 73 20  ers meta values 
15593 64 69 66 66 65 72 65 6e 74 6c 79 2e 20 20 41 74  differently.  At
15594 20 74 68 65 20 73 63 68 65 6d 61 0a 2a 2a 20 6c   the schema.** l
15595 61 79 65 72 20 28 61 6e 64 20 74 68 65 20 53 65  ayer (and the Se
15596 74 43 6f 6f 6b 69 65 20 61 6e 64 20 52 65 61 64  tCookie and Read
15597 43 6f 6f 6b 69 65 20 6f 70 63 6f 64 65 73 29 20  Cookie opcodes) 
15598 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  the number of.**
15599 20 66 72 65 65 20 70 61 67 65 73 20 69 73 20 6e   free pages is n
1559a 6f 74 20 76 69 73 69 62 6c 65 2e 20 20 53 6f 20  ot visible.  So 
1559b 43 6f 6f 6b 69 65 5b 30 5d 20 69 73 20 74 68 65  Cookie[0] is the
1559c 20 73 61 6d 65 20 61 73 20 4d 65 74 61 5b 31 5d   same as Meta[1]
1559d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1559e 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
1559f 74 72 65 65 47 65 74 4d 65 74 61 28 42 74 72 65  treeGetMeta(Btre
155a0 65 20 2a 70 2c 20 69 6e 74 20 69 64 78 2c 20 75  e *p, int idx, u
155a1 33 32 20 2a 70 4d 65 74 61 29 7b 0a 20 20 44 62  32 *pMeta){.  Db
155a2 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 20  Page *pDbPage;. 
155a3 20 69 6e 74 20 72 63 3b 0a 20 20 75 6e 73 69 67   int rc;.  unsig
155a4 6e 65 64 20 63 68 61 72 20 2a 70 50 31 3b 0a 20  ned char *pP1;. 
155a5 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
155a6 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69   p->pBt;..  sqli
155a7 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
155a8 3b 0a 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d  ;.  pBt->db = p-
155a9 3e 64 62 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 69  >db;..  /* Readi
155aa 6e 67 20 61 20 6d 65 74 61 2d 64 61 74 61 20 76  ng a meta-data v
155ab 61 6c 75 65 20 72 65 71 75 69 72 65 73 20 61 20  alue requires a 
155ac 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 70 61 67  read-lock on pag
155ad 65 20 31 20 28 61 6e 64 20 68 65 6e 63 65 0a 20  e 1 (and hence. 
155ae 20 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 5f 6d   ** the sqlite_m
155af 61 73 74 65 72 20 74 61 62 6c 65 2e 20 57 65 20  aster table. We 
155b0 67 72 61 62 20 74 68 69 73 20 6c 6f 63 6b 20 72  grab this lock r
155b1 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65  egardless of whe
155b2 74 68 65 72 20 6f 72 0a 20 20 2a 2a 20 6e 6f 74  ther or.  ** not
155b3 20 74 68 65 20 53 51 4c 49 54 45 5f 52 65 61 64   the SQLITE_Read
155b4 55 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67  Uncommitted flag
155b5 20 69 73 20 73 65 74 20 28 74 68 65 20 74 61 62   is set (the tab
155b6 6c 65 20 72 6f 6f 74 65 64 20 61 74 20 70 61 67  le rooted at pag
155b7 65 0a 20 20 2a 2a 20 31 20 69 73 20 74 72 65 61  e.  ** 1 is trea
155b8 74 65 64 20 61 73 20 61 20 73 70 65 63 69 61 6c  ted as a special
155b9 20 63 61 73 65 20 62 79 20 71 75 65 72 79 54 61   case by queryTa
155ba 62 6c 65 4c 6f 63 6b 28 29 20 61 6e 64 20 6c 6f  bleLock() and lo
155bb 63 6b 54 61 62 6c 65 28 29 29 2e 0a 20 20 2a 2f  ckTable())..  */
155bc 0a 20 20 72 63 20 3d 20 71 75 65 72 79 54 61 62  .  rc = queryTab
155bd 6c 65 4c 6f 63 6b 28 70 2c 20 31 2c 20 52 45 41  leLock(p, 1, REA
155be 44 5f 4c 4f 43 4b 29 3b 0a 20 20 69 66 28 20 72  D_LOCK);.  if( r
155bf 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
155c0 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
155c1 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65  Leave(p);.    re
155c2 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
155c3 61 73 73 65 72 74 28 20 69 64 78 3e 3d 30 20 26  assert( idx>=0 &
155c4 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 20 20 69  & idx<=15 );.  i
155c5 66 28 20 70 42 74 2d 3e 70 50 61 67 65 31 20 29  f( pBt->pPage1 )
155c6 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 62 2d 74  {.    /* The b-t
155c7 72 65 65 20 69 73 20 61 6c 72 65 61 64 79 20 68  ree is already h
155c8 6f 6c 64 69 6e 67 20 61 20 72 65 66 65 72 65 6e  olding a referen
155c9 63 65 20 74 6f 20 70 61 67 65 20 31 20 6f 66 20  ce to page 1 of 
155ca 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20  the database.   
155cb 20 2a 2a 20 66 69 6c 65 2e 20 49 6e 20 74 68 69   ** file. In thi
155cc 73 20 63 61 73 65 20 74 68 65 20 72 65 71 75 69  s case the requi
155cd 72 65 64 20 6d 65 74 61 2d 64 61 74 61 20 76 61  red meta-data va
155ce 6c 75 65 20 63 61 6e 20 62 65 20 72 65 61 64 20  lue can be read 
155cf 64 69 72 65 63 74 6c 79 0a 20 20 20 20 2a 2a 20  directly.    ** 
155d0 66 72 6f 6d 20 74 68 65 20 70 61 67 65 20 64 61  from the page da
155d1 74 61 20 6f 66 20 74 68 69 73 20 72 65 66 65 72  ta of this refer
155d2 65 6e 63 65 2e 20 54 68 69 73 20 69 73 20 73 6c  ence. This is sl
155d3 69 67 68 74 6c 79 20 66 61 73 74 65 72 20 74 68  ightly faster th
155d4 61 6e 0a 20 20 20 20 2a 2a 20 72 65 71 75 65 73  an.    ** reques
155d5 74 69 6e 67 20 61 20 6e 65 77 20 72 65 66 65 72  ting a new refer
155d6 65 6e 63 65 20 66 72 6f 6d 20 74 68 65 20 70 61  ence from the pa
155d7 67 65 72 20 6c 61 79 65 72 2e 0a 20 20 20 20 2a  ger layer..    *
155d8 2f 0a 20 20 20 20 70 50 31 20 3d 20 28 75 6e 73  /.    pP1 = (uns
155d9 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 42 74  igned char *)pBt
155da 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 3b  ->pPage1->aData;
155db 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
155dc 20 54 68 65 20 62 2d 74 72 65 65 20 64 6f 65 73   The b-tree does
155dd 20 6e 6f 74 20 68 61 76 65 20 61 20 72 65 66 65   not have a refe
155de 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20  rence to page 1 
155df 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
155e0 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 20 4f 62 74  file..    ** Obt
155e1 61 69 6e 20 6f 6e 65 20 66 72 6f 6d 20 74 68 65  ain one from the
155e2 20 70 61 67 65 72 20 6c 61 79 65 72 2e 0a 20 20   pager layer..  
155e3 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71    */.    rc = sq
155e4 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 42  lite3PagerGet(pB
155e5 74 2d 3e 70 50 61 67 65 72 2c 20 31 2c 20 26 70  t->pPager, 1, &p
155e6 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28  DbPage);.    if(
155e7 20 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c   rc ){.      sql
155e8 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
155e9 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
155ea 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50  rc;.    }.    pP
155eb 31 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  1 = (unsigned ch
155ec 61 72 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65  ar *)sqlite3Page
155ed 72 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65  rGetData(pDbPage
155ee 29 3b 0a 20 20 7d 0a 20 20 2a 70 4d 65 74 61 20  );.  }.  *pMeta 
155ef 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 31 5b  = get4byte(&pP1[
155f0 33 36 20 2b 20 69 64 78 2a 34 5d 29 3b 0a 0a 20  36 + idx*4]);.. 
155f1 20 2f 2a 20 49 66 20 74 68 65 20 62 2d 74 72 65   /* If the b-tre
155f2 65 20 69 73 20 6e 6f 74 20 68 6f 6c 64 69 6e 67  e is not holding
155f3 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20   a reference to 
155f4 70 61 67 65 20 31 2c 20 74 68 65 6e 20 6f 6e 65  page 1, then one
155f5 20 77 61 73 20 0a 20 20 2a 2a 20 72 65 71 75 65   was .  ** reque
155f6 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61  sted from the pa
155f7 67 65 72 20 6c 61 79 65 72 20 69 6e 20 74 68 65  ger layer in the
155f8 20 61 62 6f 76 65 20 62 6c 6f 63 6b 2e 20 52 65   above block. Re
155f9 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20  lease it now..  
155fa 2a 2f 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70  */.  if( !pBt->p
155fb 50 61 67 65 31 20 29 7b 0a 20 20 20 20 73 71 6c  Page1 ){.    sql
155fc 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
155fd 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a 0a 20 20  DbPage);.  }..  
155fe 2f 2a 20 49 66 20 61 75 74 6f 76 61 63 75 75 6d  /* If autovacuum
155ff 65 64 20 69 73 20 64 69 73 61 62 6c 65 64 20 69  ed is disabled i
15600 6e 20 74 68 69 73 20 62 75 69 6c 64 20 62 75 74  n this build but
15601 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 20 74   we are trying t
15602 6f 20 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 61  o .  ** access a
15603 6e 20 61 75 74 6f 76 61 63 75 75 6d 65 64 20 64  n autovacuumed d
15604 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 6d 61  atabase, then ma
15605 6b 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ke the database 
15606 72 65 61 64 6f 6e 6c 79 2e 20 0a 20 20 2a 2f 0a  readonly. .  */.
15607 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
15608 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
15609 69 66 28 20 69 64 78 3d 3d 34 20 26 26 20 2a 70  if( idx==4 && *p
1560a 4d 65 74 61 3e 30 20 29 20 70 42 74 2d 3e 72 65  Meta>0 ) pBt->re
1560b 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 23 65 6e 64  adOnly = 1;.#end
1560c 69 66 0a 0a 20 20 2f 2a 20 47 72 61 62 20 74 68  if..  /* Grab th
1560d 65 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 70  e read-lock on p
1560e 61 67 65 20 31 2e 20 2a 2f 0a 20 20 72 63 20 3d  age 1. */.  rc =
1560f 20 6c 6f 63 6b 54 61 62 6c 65 28 70 2c 20 31 2c   lockTable(p, 1,
15610 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 73   READ_LOCK);.  s
15611 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
15612 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
15613 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  ;.}../*.** Write
15614 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f   meta-informatio
15615 6e 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20  n back into the 
15616 64 61 74 61 62 61 73 65 2e 20 20 4d 65 74 61 5b  database.  Meta[
15617 30 5d 20 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e  0] is.** read-on
15618 6c 79 20 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62  ly and may not b
15619 65 20 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a 53 51  e written..*/.SQ
1561a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1561b 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64   sqlite3BtreeUpd
1561c 61 74 65 4d 65 74 61 28 42 74 72 65 65 20 2a 70  ateMeta(Btree *p
1561d 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20 69  , int idx, u32 i
1561e 4d 65 74 61 29 7b 0a 20 20 42 74 53 68 61 72 65  Meta){.  BtShare
1561f 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
15620 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
15621 20 2a 70 50 31 3b 0a 20 20 69 6e 74 20 72 63 3b   *pP1;.  int rc;
15622 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3e 3d  .  assert( idx>=
15623 31 20 26 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a  1 && idx<=15 );.
15624 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
15625 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64  ter(p);.  pBt->d
15626 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28  b = p->db;.  if(
15627 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41   p->inTrans!=TRA
15628 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20  NS_WRITE ){.    
15629 72 63 20 3d 20 70 42 74 2d 3e 72 65 61 64 4f 6e  rc = pBt->readOn
1562a 6c 79 20 3f 20 53 51 4c 49 54 45 5f 52 45 41 44  ly ? SQLITE_READ
1562b 4f 4e 4c 59 20 3a 20 53 51 4c 49 54 45 5f 45 52  ONLY : SQLITE_ER
1562c 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ROR;.  }else{.  
1562d 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70    assert( pBt->p
1562e 50 61 67 65 31 21 3d 30 20 29 3b 0a 20 20 20 20  Page1!=0 );.    
1562f 70 50 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65  pP1 = pBt->pPage
15630 31 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 72 63  1->aData;.    rc
15631 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
15632 72 69 74 65 28 70 42 74 2d 3e 70 50 61 67 65 31  rite(pBt->pPage1
15633 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
15634 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
15635 4b 20 29 7b 0a 20 20 20 20 20 20 70 75 74 34 62  K ){.      put4b
15636 79 74 65 28 26 70 50 31 5b 33 36 20 2b 20 69 64  yte(&pP1[36 + id
15637 78 2a 34 5d 2c 20 69 4d 65 74 61 29 3b 0a 23 69  x*4], iMeta);.#i
15638 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
15639 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
1563a 20 20 20 69 66 28 20 69 64 78 3d 3d 37 20 29 7b     if( idx==7 ){
1563b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1563c 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
1563d 20 7c 7c 20 69 4d 65 74 61 3d 3d 30 20 29 3b 0a   || iMeta==0 );.
1563e 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1563f 69 4d 65 74 61 3d 3d 30 20 7c 7c 20 69 4d 65 74  iMeta==0 || iMet
15640 61 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20  a==1 );.        
15641 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20  pBt->incrVacuum 
15642 3d 20 69 4d 65 74 61 3b 0a 20 20 20 20 20 20 7d  = iMeta;.      }
15643 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20  .#endif.    }.  
15644 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  }.  sqlite3Btree
15645 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
15646 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
15647 52 65 74 75 72 6e 20 74 68 65 20 66 6c 61 67 20  Return the flag 
15648 62 79 74 65 20 61 74 20 74 68 65 20 62 65 67 69  byte at the begi
15649 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67  nning of the pag
1564a 65 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f  e that the curso
1564b 72 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 6c  r.** is currentl
1564c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a 2a  y pointing to..*
1564d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1564e 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
1564f 65 46 6c 61 67 73 28 42 74 43 75 72 73 6f 72 20  eFlags(BtCursor 
15650 2a 70 43 75 72 29 7b 0a 20 20 2f 2a 20 54 4f 44  *pCur){.  /* TOD
15651 4f 3a 20 57 68 61 74 20 61 62 6f 75 74 20 43 55  O: What about CU
15652 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b  RSOR_REQUIRESEEK
15653 20 73 74 61 74 65 3f 20 50 72 6f 62 61 62 6c 79   state? Probably
15654 20 6e 65 65 64 20 74 6f 20 63 61 6c 6c 0a 20 20   need to call.  
15655 2a 2a 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72  ** restoreCursor
15656 50 6f 73 69 74 69 6f 6e 28 29 20 68 65 72 65 2e  Position() here.
15657 0a 20 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  .  */.  MemPage 
15658 2a 70 50 61 67 65 3b 0a 20 20 72 65 73 74 6f 72  *pPage;.  restor
15659 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  eCursorPosition(
1565a 70 43 75 72 29 3b 0a 20 20 70 50 61 67 65 20 3d  pCur);.  pPage =
1565b 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
1565c 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73  ur->iPage];.  as
1565d 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
1565e 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
1565f 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
15660 3e 70 42 74 3d 3d 70 43 75 72 2d 3e 70 42 74 20  >pBt==pCur->pBt 
15661 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67  );.  return pPag
15662 65 20 3f 20 70 50 61 67 65 2d 3e 61 44 61 74 61  e ? pPage->aData
15663 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65  [pPage->hdrOffse
15664 74 5d 20 3a 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  t] : 0;.}.../*.*
15665 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61 67  * Return the pag
15666 65 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  er associated wi
15667 74 68 20 61 20 42 54 72 65 65 2e 20 20 54 68 69  th a BTree.  Thi
15668 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  s routine is use
15669 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67  d for.** testing
1566a 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f   and debugging o
1566b 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  nly..*/.SQLITE_P
1566c 52 49 56 41 54 45 20 50 61 67 65 72 20 2a 73 71  RIVATE Pager *sq
1566d 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
1566e 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72 65 74  Btree *p){.  ret
1566f 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67  urn p->pBt->pPag
15670 65 72 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53  er;.}..#ifndef S
15671 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47  QLITE_OMIT_INTEG
15672 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a  RITY_CHECK./*.**
15673 20 41 70 70 65 6e 64 20 61 20 6d 65 73 73 61 67   Append a messag
15674 65 20 74 6f 20 74 68 65 20 65 72 72 6f 72 20 6d  e to the error m
15675 65 73 73 61 67 65 20 73 74 72 69 6e 67 2e 0a 2a  essage string..*
15676 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 68  /.static void ch
15677 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 0a 20 20  eckAppendMsg(.  
15678 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68  IntegrityCk *pCh
15679 65 63 6b 2c 0a 20 20 63 68 61 72 20 2a 7a 4d 73  eck,.  char *zMs
1567a 67 31 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  g1,.  const char
1567b 20 2a 7a 46 6f 72 6d 61 74 2c 0a 20 20 2e 2e 2e   *zFormat,.  ...
1567c 0a 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70  .){.  va_list ap
1567d 3b 0a 20 20 69 66 28 20 21 70 43 68 65 63 6b 2d  ;.  if( !pCheck-
1567e 3e 6d 78 45 72 72 20 29 20 72 65 74 75 72 6e 3b  >mxErr ) return;
1567f 0a 20 20 70 43 68 65 63 6b 2d 3e 6d 78 45 72 72  .  pCheck->mxErr
15680 2d 2d 3b 0a 20 20 70 43 68 65 63 6b 2d 3e 6e 45  --;.  pCheck->nE
15681 72 72 2b 2b 3b 0a 20 20 76 61 5f 73 74 61 72 74  rr++;.  va_start
15682 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20  (ap, zFormat);. 
15683 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 65 72 72   if( pCheck->err
15684 4d 73 67 2e 6e 43 68 61 72 20 29 7b 0a 20 20 20  Msg.nChar ){.   
15685 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
15686 41 70 70 65 6e 64 28 26 70 43 68 65 63 6b 2d 3e  Append(&pCheck->
15687 65 72 72 4d 73 67 2c 20 22 5c 6e 22 2c 20 31 29  errMsg, "\n", 1)
15688 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 4d 73 67  ;.  }.  if( zMsg
15689 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  1 ){.    sqlite3
1568a 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 26  StrAccumAppend(&
1568b 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2c 20  pCheck->errMsg, 
1568c 7a 4d 73 67 31 2c 20 2d 31 29 3b 0a 20 20 7d 0a  zMsg1, -1);.  }.
1568d 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74    sqlite3VXPrint
1568e 66 28 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73  f(&pCheck->errMs
1568f 67 2c 20 31 2c 20 7a 46 6f 72 6d 61 74 2c 20 61  g, 1, zFormat, a
15690 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29  p);.  va_end(ap)
15691 3b 0a 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e  ;.  if( pCheck->
15692 65 72 72 4d 73 67 2e 6d 61 6c 6c 6f 63 46 61 69  errMsg.mallocFai
15693 6c 65 64 20 29 7b 0a 20 20 20 20 70 43 68 65 63  led ){.    pChec
15694 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  k->mallocFailed 
15695 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69  = 1;.  }.}.#endi
15696 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
15697 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b  _INTEGRITY_CHECK
15698 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
15699 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49  ITE_OMIT_INTEGRI
1569a 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 41  TY_CHECK./*.** A
1569b 64 64 20 31 20 74 6f 20 74 68 65 20 72 65 66 65  dd 1 to the refe
1569c 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72 20  rence count for 
1569d 70 61 67 65 20 69 50 61 67 65 2e 20 20 49 66 20  page iPage.  If 
1569e 74 68 69 73 20 69 73 20 74 68 65 20 73 65 63 6f  this is the seco
1569f 6e 64 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 20  nd.** reference 
156a0 74 6f 20 74 68 65 20 70 61 67 65 2c 20 61 64 64  to the page, add
156a1 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
156a2 65 20 74 6f 20 70 43 68 65 63 6b 2d 3e 7a 45 72  e to pCheck->zEr
156a3 72 4d 73 67 2e 0a 2a 2a 20 52 65 74 75 72 6e 20  rMsg..** Return 
156a4 31 20 69 66 20 74 68 65 72 65 20 61 72 65 20 32  1 if there are 2
156a5 20 6f 72 65 20 6d 6f 72 65 20 72 65 66 65 72 65   ore more refere
156a6 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 65  nces to the page
156a7 20 61 6e 64 20 30 20 69 66 0a 2a 2a 20 69 66 20   and 0 if.** if 
156a8 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73  this is the firs
156a9 74 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74  t reference to t
156aa 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 41  he page..**.** A
156ab 6c 73 6f 20 63 68 65 63 6b 20 74 68 61 74 20 74  lso check that t
156ac 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69  he page number i
156ad 73 20 69 6e 20 62 6f 75 6e 64 73 2e 0a 2a 2f 0a  s in bounds..*/.
156ae 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b  static int check
156af 52 65 66 28 49 6e 74 65 67 72 69 74 79 43 6b 20  Ref(IntegrityCk 
156b0 2a 70 43 68 65 63 6b 2c 20 69 6e 74 20 69 50 61  *pCheck, int iPa
156b1 67 65 2c 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65  ge, char *zConte
156b2 78 74 29 7b 0a 20 20 69 66 28 20 69 50 61 67 65  xt){.  if( iPage
156b3 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  ==0 ) return 1;.
156b4 20 20 69 66 28 20 69 50 61 67 65 3e 70 43 68 65    if( iPage>pChe
156b5 63 6b 2d 3e 6e 50 61 67 65 20 7c 7c 20 69 50 61  ck->nPage || iPa
156b6 67 65 3c 30 20 29 7b 0a 20 20 20 20 63 68 65 63  ge<0 ){.    chec
156b7 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63  kAppendMsg(pChec
156b8 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 69 6e  k, zContext, "in
156b9 76 61 6c 69 64 20 70 61 67 65 20 6e 75 6d 62 65  valid page numbe
156ba 72 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20  r %d", iPage);. 
156bb 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
156bc 0a 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 61  .  if( pCheck->a
156bd 6e 52 65 66 5b 69 50 61 67 65 5d 3d 3d 31 20 29  nRef[iPage]==1 )
156be 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e  {.    checkAppen
156bf 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f  dMsg(pCheck, zCo
156c0 6e 74 65 78 74 2c 20 22 32 6e 64 20 72 65 66 65  ntext, "2nd refe
156c1 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 25 64  rence to page %d
156c2 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 72  ", iPage);.    r
156c3 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72  eturn 1;.  }.  r
156c4 65 74 75 72 6e 20 20 28 70 43 68 65 63 6b 2d 3e  eturn  (pCheck->
156c5 61 6e 52 65 66 5b 69 50 61 67 65 5d 2b 2b 29 3e  anRef[iPage]++)>
156c6 31 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  1;.}..#ifndef SQ
156c7 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
156c8 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b  CUUM./*.** Check
156c9 20 74 68 61 74 20 74 68 65 20 65 6e 74 72 79 20   that the entry 
156ca 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d  in the pointer-m
156cb 61 70 20 66 6f 72 20 70 61 67 65 20 69 43 68 69  ap for page iChi
156cc 6c 64 20 6d 61 70 73 20 74 6f 20 0a 2a 2a 20 70  ld maps to .** p
156cd 61 67 65 20 69 50 61 72 65 6e 74 2c 20 70 6f 69  age iParent, poi
156ce 6e 74 65 72 20 74 79 70 65 20 70 74 72 54 79 70  nter type ptrTyp
156cf 65 2e 20 49 66 20 6e 6f 74 2c 20 61 70 70 65 6e  e. If not, appen
156d0 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  d an error messa
156d1 67 65 0a 2a 2a 20 74 6f 20 70 43 68 65 63 6b 2e  ge.** to pCheck.
156d2 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
156d3 63 68 65 63 6b 50 74 72 6d 61 70 28 0a 20 20 49  checkPtrmap(.  I
156d4 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65  ntegrityCk *pChe
156d5 63 6b 2c 20 20 20 2f 2a 20 49 6e 74 65 67 72 69  ck,   /* Integri
156d6 74 79 20 63 68 65 63 6b 20 63 6f 6e 74 65 78 74  ty check context
156d7 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 43 68 69 6c   */.  Pgno iChil
156d8 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  d,           /* 
156d9 43 68 69 6c 64 20 70 61 67 65 20 6e 75 6d 62 65  Child page numbe
156da 72 20 2a 2f 0a 20 20 75 38 20 65 54 79 70 65 2c  r */.  u8 eType,
156db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
156dc 20 45 78 70 65 63 74 65 64 20 70 6f 69 6e 74 65   Expected pointe
156dd 72 20 6d 61 70 20 74 79 70 65 20 2a 2f 0a 20 20  r map type */.  
156de 50 67 6e 6f 20 69 50 61 72 65 6e 74 2c 20 20 20  Pgno iParent,   
156df 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74         /* Expect
156e0 65 64 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70  ed pointer map p
156e1 61 72 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65  arent page numbe
156e2 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f  r */.  char *zCo
156e3 6e 74 65 78 74 20 20 20 20 20 20 20 20 20 2f 2a  ntext         /*
156e4 20 43 6f 6e 74 65 78 74 20 64 65 73 63 72 69 70   Context descrip
156e5 74 69 6f 6e 20 28 75 73 65 64 20 66 6f 72 20 65  tion (used for e
156e6 72 72 6f 72 20 6d 73 67 29 20 2a 2f 0a 29 7b 0a  rror msg) */.){.
156e7 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 38 20 65    int rc;.  u8 e
156e8 50 74 72 6d 61 70 54 79 70 65 3b 0a 20 20 50 67  PtrmapType;.  Pg
156e9 6e 6f 20 69 50 74 72 6d 61 70 50 61 72 65 6e 74  no iPtrmapParent
156ea 3b 0a 0a 20 20 72 63 20 3d 20 70 74 72 6d 61 70  ;..  rc = ptrmap
156eb 47 65 74 28 70 43 68 65 63 6b 2d 3e 70 42 74 2c  Get(pCheck->pBt,
156ec 20 69 43 68 69 6c 64 2c 20 26 65 50 74 72 6d 61   iChild, &ePtrma
156ed 70 54 79 70 65 2c 20 26 69 50 74 72 6d 61 70 50  pType, &iPtrmapP
156ee 61 72 65 6e 74 29 3b 0a 20 20 69 66 28 20 72 63  arent);.  if( rc
156ef 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
156f0 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73     checkAppendMs
156f1 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65  g(pCheck, zConte
156f2 78 74 2c 20 22 46 61 69 6c 65 64 20 74 6f 20 72  xt, "Failed to r
156f3 65 61 64 20 70 74 72 6d 61 70 20 6b 65 79 3d 25  ead ptrmap key=%
156f4 64 22 2c 20 69 43 68 69 6c 64 29 3b 0a 20 20 20  d", iChild);.   
156f5 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
156f6 69 66 28 20 65 50 74 72 6d 61 70 54 79 70 65 21  if( ePtrmapType!
156f7 3d 65 54 79 70 65 20 7c 7c 20 69 50 74 72 6d 61  =eType || iPtrma
156f8 70 50 61 72 65 6e 74 21 3d 69 50 61 72 65 6e 74  pParent!=iParent
156f9 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70   ){.    checkApp
156fa 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a  endMsg(pCheck, z
156fb 43 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20  Context, .      
156fc 22 42 61 64 20 70 74 72 20 6d 61 70 20 65 6e 74  "Bad ptr map ent
156fd 72 79 20 6b 65 79 3d 25 64 20 65 78 70 65 63 74  ry key=%d expect
156fe 65 64 3d 28 25 64 2c 25 64 29 20 67 6f 74 3d 28  ed=(%d,%d) got=(
156ff 25 64 2c 25 64 29 22 2c 20 0a 20 20 20 20 20 20  %d,%d)", .      
15700 69 43 68 69 6c 64 2c 20 65 54 79 70 65 2c 20 69  iChild, eType, i
15701 50 61 72 65 6e 74 2c 20 65 50 74 72 6d 61 70 54  Parent, ePtrmapT
15702 79 70 65 2c 20 69 50 74 72 6d 61 70 50 61 72 65  ype, iPtrmapPare
15703 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69  nt);.  }.}.#endi
15704 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74  f../*.** Check t
15705 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66 20  he integrity of 
15706 74 68 65 20 66 72 65 65 6c 69 73 74 20 6f 72 20  the freelist or 
15707 6f 66 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70  of an overflow p
15708 61 67 65 20 6c 69 73 74 2e 0a 2a 2a 20 56 65 72  age list..** Ver
15709 69 66 79 20 74 68 61 74 20 74 68 65 20 6e 75 6d  ify that the num
1570a 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e 20  ber of pages on 
1570b 74 68 65 20 6c 69 73 74 20 69 73 20 4e 2e 0a 2a  the list is N..*
1570c 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 68  /.static void ch
1570d 65 63 6b 4c 69 73 74 28 0a 20 20 49 6e 74 65 67  eckList(.  Integ
1570e 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20  rityCk *pCheck, 
1570f 20 2f 2a 20 49 6e 74 65 67 72 69 74 79 20 63 68   /* Integrity ch
15710 65 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  ecking context *
15711 2f 0a 20 20 69 6e 74 20 69 73 46 72 65 65 4c 69  /.  int isFreeLi
15712 73 74 2c 20 20 20 20 20 20 20 2f 2a 20 54 72 75  st,       /* Tru
15713 65 20 66 6f 72 20 61 20 66 72 65 65 6c 69 73 74  e for a freelist
15714 2e 20 20 46 61 6c 73 65 20 66 6f 72 20 6f 76 65  .  False for ove
15715 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 20  rflow page list 
15716 2a 2f 0a 20 20 69 6e 74 20 69 50 61 67 65 2c 20  */.  int iPage, 
15717 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
15718 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 66 69  ge number for fi
15719 72 73 74 20 70 61 67 65 20 69 6e 20 74 68 65 20  rst page in the 
1571a 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 4e 2c  list */.  int N,
1571b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1571c 2f 2a 20 45 78 70 65 63 74 65 64 20 6e 75 6d 62  /* Expected numb
1571d 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74  er of pages in t
1571e 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 63 68 61  he list */.  cha
1571f 72 20 2a 7a 43 6f 6e 74 65 78 74 20 20 20 20 20  r *zContext     
15720 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 66 6f     /* Context fo
15721 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73  r error messages
15722 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   */.){.  int i;.
15723 20 20 69 6e 74 20 65 78 70 65 63 74 65 64 20 3d    int expected =
15724 20 4e 3b 0a 20 20 69 6e 74 20 69 46 69 72 73 74   N;.  int iFirst
15725 20 3d 20 69 50 61 67 65 3b 0a 20 20 77 68 69 6c   = iPage;.  whil
15726 65 28 20 4e 2d 2d 20 3e 20 30 20 26 26 20 70 43  e( N-- > 0 && pC
15727 68 65 63 6b 2d 3e 6d 78 45 72 72 20 29 7b 0a 20  heck->mxErr ){. 
15728 20 20 20 44 62 50 61 67 65 20 2a 70 4f 76 66 6c     DbPage *pOvfl
15729 50 61 67 65 3b 0a 20 20 20 20 75 6e 73 69 67 6e  Page;.    unsign
1572a 65 64 20 63 68 61 72 20 2a 70 4f 76 66 6c 44 61  ed char *pOvflDa
1572b 74 61 3b 0a 20 20 20 20 69 66 28 20 69 50 61 67  ta;.    if( iPag
1572c 65 3c 31 20 29 7b 0a 20 20 20 20 20 20 63 68 65  e<1 ){.      che
1572d 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65  ckAppendMsg(pChe
1572e 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20  ck, zContext,.  
1572f 20 20 20 20 20 20 20 22 25 64 20 6f 66 20 25 64         "%d of %d
15730 20 70 61 67 65 73 20 6d 69 73 73 69 6e 67 20 66   pages missing f
15731 72 6f 6d 20 6f 76 65 72 66 6c 6f 77 20 6c 69 73  rom overflow lis
15732 74 20 73 74 61 72 74 69 6e 67 20 61 74 20 25 64  t starting at %d
15733 22 2c 0a 20 20 20 20 20 20 20 20 20 20 4e 2b 31  ",.          N+1
15734 2c 20 65 78 70 65 63 74 65 64 2c 20 69 46 69 72  , expected, iFir
15735 73 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  st);.      break
15736 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
15737 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 6b 2c  checkRef(pCheck,
15738 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74   iPage, zContext
15739 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69  ) ) break;.    i
1573a 66 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47  f( sqlite3PagerG
1573b 65 74 28 70 43 68 65 63 6b 2d 3e 70 50 61 67 65  et(pCheck->pPage
1573c 72 2c 20 28 50 67 6e 6f 29 69 50 61 67 65 2c 20  r, (Pgno)iPage, 
1573d 26 70 4f 76 66 6c 50 61 67 65 29 20 29 7b 0a 20  &pOvflPage) ){. 
1573e 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64       checkAppend
1573f 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e  Msg(pCheck, zCon
15740 74 65 78 74 2c 20 22 66 61 69 6c 65 64 20 74 6f  text, "failed to
15741 20 67 65 74 20 70 61 67 65 20 25 64 22 2c 20 69   get page %d", i
15742 50 61 67 65 29 3b 0a 20 20 20 20 20 20 62 72 65  Page);.      bre
15743 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f  ak;.    }.    pO
15744 76 66 6c 44 61 74 61 20 3d 20 28 75 6e 73 69 67  vflData = (unsig
15745 6e 65 64 20 63 68 61 72 20 2a 29 73 71 6c 69 74  ned char *)sqlit
15746 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70  e3PagerGetData(p
15747 4f 76 66 6c 50 61 67 65 29 3b 0a 20 20 20 20 69  OvflPage);.    i
15748 66 28 20 69 73 46 72 65 65 4c 69 73 74 20 29 7b  f( isFreeList ){
15749 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 67  .      int n = g
1574a 65 74 34 62 79 74 65 28 26 70 4f 76 66 6c 44 61  et4byte(&pOvflDa
1574b 74 61 5b 34 5d 29 3b 0a 23 69 66 6e 64 65 66 20  ta[4]);.#ifndef 
1574c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
1574d 56 41 43 55 55 4d 0a 20 20 20 20 20 20 69 66 28  VACUUM.      if(
1574e 20 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75   pCheck->pBt->au
1574f 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20  toVacuum ){.    
15750 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28      checkPtrmap(
15751 70 43 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 50  pCheck, iPage, P
15752 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 2c 20  TRMAP_FREEPAGE, 
15753 30 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20  0, zContext);.  
15754 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
15755 20 20 20 69 66 28 20 6e 3e 70 43 68 65 63 6b 2d     if( n>pCheck-
15756 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  >pBt->usableSize
15757 2f 34 2d 32 20 29 7b 0a 20 20 20 20 20 20 20 20  /4-2 ){.        
15758 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70  checkAppendMsg(p
15759 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c  Check, zContext,
1575a 0a 20 20 20 20 20 20 20 20 20 20 20 22 66 72 65  .           "fre
1575b 65 6c 69 73 74 20 6c 65 61 66 20 63 6f 75 6e 74  elist leaf count
1575c 20 74 6f 6f 20 62 69 67 20 6f 6e 20 70 61 67 65   too big on page
1575d 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 20   %d", iPage);.  
1575e 20 20 20 20 20 20 4e 2d 2d 3b 0a 20 20 20 20 20        N--;.     
1575f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
15760 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b  for(i=0; i<n; i+
15761 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 50 67  +){.          Pg
15762 6e 6f 20 69 46 72 65 65 50 61 67 65 20 3d 20 67  no iFreePage = g
15763 65 74 34 62 79 74 65 28 26 70 4f 76 66 6c 44 61  et4byte(&pOvflDa
15764 74 61 5b 38 2b 69 2a 34 5d 29 3b 0a 23 69 66 6e  ta[8+i*4]);.#ifn
15765 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
15766 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20  AUTOVACUUM.     
15767 20 20 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d       if( pCheck-
15768 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d  >pBt->autoVacuum
15769 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1576a 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65  checkPtrmap(pChe
1576b 63 6b 2c 20 69 46 72 65 65 50 61 67 65 2c 20 50  ck, iFreePage, P
1576c 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 2c 20  TRMAP_FREEPAGE, 
1576d 30 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20  0, zContext);.  
1576e 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66          }.#endif
1576f 0a 20 20 20 20 20 20 20 20 20 20 63 68 65 63 6b  .          check
15770 52 65 66 28 70 43 68 65 63 6b 2c 20 69 46 72 65  Ref(pCheck, iFre
15771 65 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29  ePage, zContext)
15772 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
15773 20 20 20 20 4e 20 2d 3d 20 6e 3b 0a 20 20 20 20      N -= n;.    
15774 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65    }.    }.#ifnde
15775 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
15776 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 65 6c 73  TOVACUUM.    els
15777 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74  e{.      /* If t
15778 68 69 73 20 64 61 74 61 62 61 73 65 20 73 75 70  his database sup
15779 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75  ports auto-vacuu
1577a 6d 20 61 6e 64 20 69 50 61 67 65 20 69 73 20 6e  m and iPage is n
1577b 6f 74 20 74 68 65 20 6c 61 73 74 0a 20 20 20 20  ot the last.    
1577c 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 69    ** page in thi
1577d 73 20 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 2c  s overflow list,
1577e 20 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 20   check that the 
1577f 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72  pointer-map entr
15780 79 20 66 6f 72 0a 20 20 20 20 20 20 2a 2a 20 74  y for.      ** t
15781 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 61 67  he following pag
15782 65 20 6d 61 74 63 68 65 73 20 69 50 61 67 65 2e  e matches iPage.
15783 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
15784 69 66 28 20 70 43 68 65 63 6b 2d 3e 70 42 74 2d  if( pCheck->pBt-
15785 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 4e  >autoVacuum && N
15786 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 20  >0 ){.        i 
15787 3d 20 67 65 74 34 62 79 74 65 28 70 4f 76 66 6c  = get4byte(pOvfl
15788 44 61 74 61 29 3b 0a 20 20 20 20 20 20 20 20 63  Data);.        c
15789 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63  heckPtrmap(pChec
1578a 6b 2c 20 69 2c 20 50 54 52 4d 41 50 5f 4f 56 45  k, i, PTRMAP_OVE
1578b 52 46 4c 4f 57 32 2c 20 69 50 61 67 65 2c 20 7a  RFLOW2, iPage, z
1578c 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  Context);.      
1578d 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  }.    }.#endif. 
1578e 20 20 20 69 50 61 67 65 20 3d 20 67 65 74 34 62     iPage = get4b
1578f 79 74 65 28 70 4f 76 66 6c 44 61 74 61 29 3b 0a  yte(pOvflData);.
15790 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
15791 55 6e 72 65 66 28 70 4f 76 66 6c 50 61 67 65 29  Unref(pOvflPage)
15792 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f  ;.  }.}.#endif /
15793 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  * SQLITE_OMIT_IN
15794 54 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f  TEGRITY_CHECK */
15795 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
15796 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f  _OMIT_INTEGRITY_
15797 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 44 6f 20 76  CHECK./*.** Do v
15798 61 72 69 6f 75 73 20 73 61 6e 69 74 79 20 63 68  arious sanity ch
15799 65 63 6b 73 20 6f 6e 20 61 20 73 69 6e 67 6c 65  ecks on a single
1579a 20 70 61 67 65 20 6f 66 20 61 20 74 72 65 65 2e   page of a tree.
1579b 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20    Return.** the 
1579c 74 72 65 65 20 64 65 70 74 68 2e 20 20 52 6f 6f  tree depth.  Roo
1579d 74 20 70 61 67 65 73 20 72 65 74 75 72 6e 20 30  t pages return 0
1579e 2e 20 20 50 61 72 65 6e 74 73 20 6f 66 20 72 6f  .  Parents of ro
1579f 6f 74 20 70 61 67 65 73 0a 2a 2a 20 72 65 74 75  ot pages.** retu
157a0 72 6e 20 31 2c 20 61 6e 64 20 73 6f 20 66 6f 72  rn 1, and so for
157a1 74 68 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 73 65  th..** .** These
157a2 20 63 68 65 63 6b 73 20 61 72 65 20 64 6f 6e 65   checks are done
157a3 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 31 2e 20  :.**.**      1. 
157a4 20 4d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   Make sure that 
157a5 63 65 6c 6c 73 20 61 6e 64 20 66 72 65 65 62 6c  cells and freebl
157a6 6f 63 6b 73 20 64 6f 20 6e 6f 74 20 6f 76 65 72  ocks do not over
157a7 6c 61 70 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  lap.**          
157a8 62 75 74 20 63 6f 6d 62 69 6e 65 20 74 6f 20 63  but combine to c
157a9 6f 6d 70 6c 65 74 65 6c 79 20 63 6f 76 65 72 20  ompletely cover 
157aa 74 68 65 20 70 61 67 65 2e 0a 2a 2a 20 20 4e 4f  the page..**  NO
157ab 20 20 32 2e 20 20 4d 61 6b 65 20 73 75 72 65 20    2.  Make sure 
157ac 63 65 6c 6c 20 6b 65 79 73 20 61 72 65 20 69 6e  cell keys are in
157ad 20 6f 72 64 65 72 2e 0a 2a 2a 20 20 4e 4f 20 20   order..**  NO  
157ae 33 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 6e 6f  3.  Make sure no
157af 20 6b 65 79 20 69 73 20 6c 65 73 73 20 74 68 61   key is less tha
157b0 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a 4c  n or equal to zL
157b1 6f 77 65 72 42 6f 75 6e 64 2e 0a 2a 2a 20 20 4e  owerBound..**  N
157b2 4f 20 20 34 2e 20 20 4d 61 6b 65 20 73 75 72 65  O  4.  Make sure
157b3 20 6e 6f 20 6b 65 79 20 69 73 20 67 72 65 61 74   no key is great
157b4 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  er than or equal
157b5 20 74 6f 20 7a 55 70 70 65 72 42 6f 75 6e 64 2e   to zUpperBound.
157b6 0a 2a 2a 20 20 20 20 20 20 35 2e 20 20 43 68 65  .**      5.  Che
157b7 63 6b 20 74 68 65 20 69 6e 74 65 67 72 69 74 79  ck the integrity
157b8 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   of overflow pag
157b9 65 73 2e 0a 2a 2a 20 20 20 20 20 20 36 2e 20 20  es..**      6.  
157ba 52 65 63 75 72 73 69 76 65 6c 79 20 63 61 6c 6c  Recursively call
157bb 20 63 68 65 63 6b 54 72 65 65 50 61 67 65 20 6f   checkTreePage o
157bc 6e 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 2e 0a  n all children..
157bd 2a 2a 20 20 20 20 20 20 37 2e 20 20 56 65 72 69  **      7.  Veri
157be 66 79 20 74 68 61 74 20 74 68 65 20 64 65 70 74  fy that the dept
157bf 68 20 6f 66 20 61 6c 6c 20 63 68 69 6c 64 72 65  h of all childre
157c0 6e 20 69 73 20 74 68 65 20 73 61 6d 65 2e 0a 2a  n is the same..*
157c1 2a 20 20 20 20 20 20 38 2e 20 20 4d 61 6b 65 20  *      8.  Make 
157c2 73 75 72 65 20 74 68 69 73 20 70 61 67 65 20 69  sure this page i
157c3 73 20 61 74 20 6c 65 61 73 74 20 33 33 25 20 66  s at least 33% f
157c4 75 6c 6c 20 6f 72 20 65 6c 73 65 20 69 74 20 69  ull or else it i
157c5 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  s.**          th
157c6 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 72  e root of the tr
157c7 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ee..*/.static in
157c8 74 20 63 68 65 63 6b 54 72 65 65 50 61 67 65 28  t checkTreePage(
157c9 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a  .  IntegrityCk *
157ca 70 43 68 65 63 6b 2c 20 20 2f 2a 20 43 6f 6e 74  pCheck,  /* Cont
157cb 65 78 74 20 66 6f 72 20 74 68 65 20 73 61 6e 69  ext for the sani
157cc 74 79 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e  ty check */.  in
157cd 74 20 69 50 61 67 65 2c 20 20 20 20 20 20 20 20  t iPage,        
157ce 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62      /* Page numb
157cf 65 72 20 6f 66 20 74 68 65 20 70 61 67 65 20 74  er of the page t
157d0 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20 4d 65 6d  o check */.  Mem
157d1 50 61 67 65 20 2a 70 50 61 72 65 6e 74 2c 20 20  Page *pParent,  
157d2 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 70 61 67     /* Parent pag
157d3 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 50 61  e */.  char *zPa
157d4 72 65 6e 74 43 6f 6e 74 65 78 74 20 20 2f 2a 20  rentContext  /* 
157d5 50 61 72 65 6e 74 20 63 6f 6e 74 65 78 74 20 2a  Parent context *
157d6 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  /.){.  MemPage *
157d7 70 50 61 67 65 3b 0a 20 20 69 6e 74 20 69 2c 20  pPage;.  int i, 
157d8 72 63 2c 20 64 65 70 74 68 2c 20 64 32 2c 20 70  rc, depth, d2, p
157d9 67 6e 6f 2c 20 63 6e 74 3b 0a 20 20 69 6e 74 20  gno, cnt;.  int 
157da 68 64 72 2c 20 63 65 6c 6c 53 74 61 72 74 3b 0a  hdr, cellStart;.
157db 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 75    int nCell;.  u
157dc 38 20 2a 64 61 74 61 3b 0a 20 20 42 74 53 68 61  8 *data;.  BtSha
157dd 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 6e 74 20  red *pBt;.  int 
157de 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63 68  usableSize;.  ch
157df 61 72 20 7a 43 6f 6e 74 65 78 74 5b 31 30 30 5d  ar zContext[100]
157e0 3b 0a 20 20 63 68 61 72 20 2a 68 69 74 3b 0a 0a  ;.  char *hit;..
157e1 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
157e2 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65  tf(sizeof(zConte
157e3 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22  xt), zContext, "
157e4 50 61 67 65 20 25 64 3a 20 22 2c 20 69 50 61 67  Page %d: ", iPag
157e5 65 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20  e);..  /* Check 
157e6 74 68 61 74 20 74 68 65 20 70 61 67 65 20 65 78  that the page ex
157e7 69 73 74 73 0a 20 20 2a 2f 0a 20 20 70 42 74 20  ists.  */.  pBt 
157e8 3d 20 70 43 68 65 63 6b 2d 3e 70 42 74 3b 0a 20  = pCheck->pBt;. 
157e9 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 42   usableSize = pB
157ea 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20  t->usableSize;. 
157eb 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20 29 20   if( iPage==0 ) 
157ec 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
157ed 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 6b 2c  checkRef(pCheck,
157ee 20 69 50 61 67 65 2c 20 7a 50 61 72 65 6e 74 43   iPage, zParentC
157ef 6f 6e 74 65 78 74 29 20 29 20 72 65 74 75 72 6e  ontext) ) return
157f0 20 30 3b 0a 20 20 69 66 28 20 28 72 63 20 3d 20   0;.  if( (rc = 
157f1 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
157f2 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69  age(pBt, (Pgno)i
157f3 50 61 67 65 2c 20 26 70 50 61 67 65 2c 20 30 29  Page, &pPage, 0)
157f4 29 21 3d 30 20 29 7b 0a 20 20 20 20 63 68 65 63  )!=0 ){.    chec
157f5 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63  kAppendMsg(pChec
157f6 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20  k, zContext,.   
157f7 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 67      "unable to g
157f8 65 74 20 74 68 65 20 70 61 67 65 2e 20 65 72 72  et the page. err
157f9 6f 72 20 63 6f 64 65 3d 25 64 22 2c 20 72 63 29  or code=%d", rc)
157fa 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
157fb 20 20 7d 0a 20 20 69 66 28 20 28 72 63 20 3d 20    }.  if( (rc = 
157fc 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74  sqlite3BtreeInit
157fd 50 61 67 65 28 70 50 61 67 65 29 29 21 3d 30 20  Page(pPage))!=0 
157fe 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65  ){.    checkAppe
157ff 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43  ndMsg(pCheck, zC
15800 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20 20  ontext, .       
15801 20 20 20 20 20 20 20 20 20 20 20 20 22 73 71 6c              "sql
15802 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67  ite3BtreeInitPag
15803 65 28 29 20 72 65 74 75 72 6e 73 20 65 72 72 6f  e() returns erro
15804 72 20 63 6f 64 65 20 25 64 22 2c 20 72 63 29 3b  r code %d", rc);
15805 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65  .    releasePage
15806 28 70 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74  (pPage);.    ret
15807 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  urn 0;.  }..  /*
15808 20 43 68 65 63 6b 20 6f 75 74 20 61 6c 6c 20 74   Check out all t
15809 68 65 20 63 65 6c 6c 73 2e 0a 20 20 2a 2f 0a 20  he cells..  */. 
1580a 20 64 65 70 74 68 20 3d 20 30 3b 0a 20 20 66 6f   depth = 0;.  fo
1580b 72 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e  r(i=0; i<pPage->
1580c 6e 43 65 6c 6c 20 26 26 20 70 43 68 65 63 6b 2d  nCell && pCheck-
1580d 3e 6d 78 45 72 72 3b 20 69 2b 2b 29 7b 0a 20 20  >mxErr; i++){.  
1580e 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20    u8 *pCell;.   
1580f 20 69 6e 74 20 73 7a 3b 0a 20 20 20 20 43 65 6c   int sz;.    Cel
15810 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 20  lInfo info;..   
15811 20 2f 2a 20 43 68 65 63 6b 20 70 61 79 6c 6f 61   /* Check payloa
15812 64 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  d overflow pages
15813 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
15814 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
15815 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20 7a  eof(zContext), z
15816 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20  Context,.       
15817 20 20 20 20 20 20 22 4f 6e 20 74 72 65 65 20 70        "On tree p
15818 61 67 65 20 25 64 20 63 65 6c 6c 20 25 64 3a 20  age %d cell %d: 
15819 22 2c 20 69 50 61 67 65 2c 20 69 29 3b 0a 20 20  ", iPage, i);.  
1581a 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65    pCell = findCe
1581b 6c 6c 28 70 50 61 67 65 2c 69 29 3b 0a 20 20 20  ll(pPage,i);.   
1581c 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72   sqlite3BtreePar
1581d 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c  seCellPtr(pPage,
1581e 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a   pCell, &info);.
1581f 20 20 20 20 73 7a 20 3d 20 69 6e 66 6f 2e 6e 44      sz = info.nD
15820 61 74 61 3b 0a 20 20 20 20 69 66 28 20 21 70 50  ata;.    if( !pP
15821 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 20 73 7a  age->intKey ) sz
15822 20 2b 3d 20 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20   += info.nKey;. 
15823 20 20 20 61 73 73 65 72 74 28 20 73 7a 3d 3d 69     assert( sz==i
15824 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a  nfo.nPayload );.
15825 20 20 20 20 69 66 28 20 73 7a 3e 69 6e 66 6f 2e      if( sz>info.
15826 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20  nLocal ){.      
15827 69 6e 74 20 6e 50 61 67 65 20 3d 20 28 73 7a 20  int nPage = (sz 
15828 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2b 20  - info.nLocal + 
15829 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 35 29 2f  usableSize - 5)/
1582a 28 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 29  (usableSize - 4)
1582b 3b 0a 20 20 20 20 20 20 50 67 6e 6f 20 70 67 6e  ;.      Pgno pgn
1582c 6f 4f 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65  oOvfl = get4byte
1582d 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76  (&pCell[info.iOv
1582e 65 72 66 6c 6f 77 5d 29 3b 0a 23 69 66 6e 64 65  erflow]);.#ifnde
1582f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
15830 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 69  TOVACUUM.      i
15831 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  f( pBt->autoVacu
15832 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68  um ){.        ch
15833 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b  eckPtrmap(pCheck
15834 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 50 54 52 4d  , pgnoOvfl, PTRM
15835 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 69 50  AP_OVERFLOW1, iP
15836 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a  age, zContext);.
15837 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
15838 20 20 20 20 20 63 68 65 63 6b 4c 69 73 74 28 70       checkList(p
15839 43 68 65 63 6b 2c 20 30 2c 20 70 67 6e 6f 4f 76  Check, 0, pgnoOv
1583a 66 6c 2c 20 6e 50 61 67 65 2c 20 7a 43 6f 6e 74  fl, nPage, zCont
1583b 65 78 74 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ext);.    }..   
1583c 20 2f 2a 20 43 68 65 63 6b 20 73 61 6e 69 74 79   /* Check sanity
1583d 20 6f 66 20 6c 65 66 74 20 63 68 69 6c 64 20 70   of left child p
1583e 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  age..    */.    
1583f 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66  if( !pPage->leaf
15840 20 29 7b 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d   ){.      pgno =
15841 20 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29   get4byte(pCell)
15842 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
15843 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
15844 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e  .      if( pBt->
15845 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20  autoVacuum ){.  
15846 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61        checkPtrma
15847 70 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20  p(pCheck, pgno, 
15848 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 69 50  PTRMAP_BTREE, iP
15849 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a  age, zContext);.
1584a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
1584b 20 20 20 20 20 64 32 20 3d 20 63 68 65 63 6b 54       d2 = checkT
1584c 72 65 65 50 61 67 65 28 70 43 68 65 63 6b 2c 70  reePage(pCheck,p
1584d 67 6e 6f 2c 70 50 61 67 65 2c 7a 43 6f 6e 74 65  gno,pPage,zConte
1584e 78 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69  xt);.      if( i
1584f 3e 30 20 26 26 20 64 32 21 3d 64 65 70 74 68 20  >0 && d2!=depth 
15850 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b  ){.        check
15851 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b  AppendMsg(pCheck
15852 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 43 68 69  , zContext, "Chi
15853 6c 64 20 70 61 67 65 20 64 65 70 74 68 20 64 69  ld page depth di
15854 66 66 65 72 73 22 29 3b 0a 20 20 20 20 20 20 7d  ffers");.      }
15855 0a 20 20 20 20 20 20 64 65 70 74 68 20 3d 20 64  .      depth = d
15856 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  2;.    }.  }.  i
15857 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20  f( !pPage->leaf 
15858 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 65  ){.    pgno = ge
15859 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61  t4byte(&pPage->a
1585a 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f  Data[pPage->hdrO
1585b 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 73  ffset+8]);.    s
1585c 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1585d 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29  sizeof(zContext)
1585e 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20 20  , zContext, .   
1585f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15860 20 20 22 4f 6e 20 70 61 67 65 20 25 64 20 61 74    "On page %d at
15861 20 72 69 67 68 74 20 63 68 69 6c 64 3a 20 22 2c   right child: ",
15862 20 69 50 61 67 65 29 3b 0a 23 69 66 6e 64 65 66   iPage);.#ifndef
15863 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
15864 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20  OVACUUM.    if( 
15865 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
15866 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 50 74  ){.      checkPt
15867 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 67 6e  rmap(pCheck, pgn
15868 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c  o, PTRMAP_BTREE,
15869 20 69 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20   iPage, 0);.    
1586a 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 68 65  }.#endif.    che
1586b 63 6b 54 72 65 65 50 61 67 65 28 70 43 68 65 63  ckTreePage(pChec
1586c 6b 2c 20 70 67 6e 6f 2c 20 70 50 61 67 65 2c 20  k, pgno, pPage, 
1586d 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20  zContext);.  }. 
1586e 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20  .  /* Check for 
1586f 63 6f 6d 70 6c 65 74 65 20 63 6f 76 65 72 61 67  complete coverag
15870 65 20 6f 66 20 74 68 65 20 70 61 67 65 0a 20 20  e of the page.  
15871 2a 2f 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67  */.  data = pPag
15872 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20  e->aData;.  hdr 
15873 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  = pPage->hdrOffs
15874 65 74 3b 0a 20 20 68 69 74 20 3d 20 73 71 6c 69  et;.  hit = sqli
15875 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 20 70  te3PageMalloc( p
15876 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a  Bt->pageSize );.
15877 20 20 69 66 28 20 68 69 74 3d 3d 30 20 29 7b 0a    if( hit==0 ){.
15878 20 20 20 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c      pCheck->mall
15879 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
1587a 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 73 65  }else{.    memse
1587b 74 28 68 69 74 2c 20 30 2c 20 75 73 61 62 6c 65  t(hit, 0, usable
1587c 53 69 7a 65 20 29 3b 0a 20 20 20 20 6d 65 6d 73  Size );.    mems
1587d 65 74 28 68 69 74 2c 20 31 2c 20 67 65 74 32 62  et(hit, 1, get2b
1587e 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d  yte(&data[hdr+5]
1587f 29 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d 20  ));.    nCell = 
15880 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
15881 64 72 2b 33 5d 29 3b 0a 20 20 20 20 63 65 6c 6c  dr+3]);.    cell
15882 53 74 61 72 74 20 3d 20 68 64 72 20 2b 20 31 32  Start = hdr + 12
15883 20 2d 20 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66   - 4*pPage->leaf
15884 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
15885 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20  <nCell; i++){.  
15886 20 20 20 20 69 6e 74 20 70 63 20 3d 20 67 65 74      int pc = get
15887 32 62 79 74 65 28 26 64 61 74 61 5b 63 65 6c 6c  2byte(&data[cell
15888 53 74 61 72 74 2b 69 2a 32 5d 29 3b 0a 20 20 20  Start+i*2]);.   
15889 20 20 20 75 31 36 20 73 69 7a 65 20 3d 20 31 30     u16 size = 10
1588a 32 34 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b  24;.      int j;
1588b 0a 20 20 20 20 20 20 69 66 28 20 70 63 3c 3d 75  .      if( pc<=u
1588c 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20  sableSize ){.   
1588d 20 20 20 20 20 73 69 7a 65 20 3d 20 63 65 6c 6c       size = cell
1588e 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 26  SizePtr(pPage, &
1588f 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20 20 20  data[pc]);.     
15890 20 7d 0a 20 20 20 20 20 20 69 66 28 20 28 70 63   }.      if( (pc
15891 2b 73 69 7a 65 2d 31 29 3e 3d 75 73 61 62 6c 65  +size-1)>=usable
15892 53 69 7a 65 20 7c 7c 20 70 63 3c 30 20 29 7b 0a  Size || pc<0 ){.
15893 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70          checkApp
15894 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 30  endMsg(pCheck, 0
15895 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 22  , .            "
15896 43 6f 72 72 75 70 74 69 6f 6e 20 64 65 74 65 63  Corruption detec
15897 74 65 64 20 69 6e 20 63 65 6c 6c 20 25 64 20 6f  ted in cell %d o
15898 6e 20 70 61 67 65 20 25 64 22 2c 69 2c 69 50 61  n page %d",i,iPa
15899 67 65 2c 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c  ge,0);.      }el
1589a 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  se{.        for(
1589b 6a 3d 70 63 2b 73 69 7a 65 2d 31 3b 20 6a 3e 3d  j=pc+size-1; j>=
1589c 70 63 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a 5d 2b  pc; j--) hit[j]+
1589d 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  +;.      }.    }
1589e 0a 20 20 20 20 66 6f 72 28 63 6e 74 3d 30 2c 20  .    for(cnt=0, 
1589f 69 3d 67 65 74 32 62 79 74 65 28 26 64 61 74 61  i=get2byte(&data
158a0 5b 68 64 72 2b 31 5d 29 3b 20 69 3e 30 20 26 26  [hdr+1]); i>0 &&
158a1 20 69 3c 75 73 61 62 6c 65 53 69 7a 65 20 26 26   i<usableSize &&
158a2 20 63 6e 74 3c 31 30 30 30 30 3b 20 0a 20 20 20   cnt<10000; .   
158a3 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 29 7b 0a          cnt++){.
158a4 20 20 20 20 20 20 69 6e 74 20 73 69 7a 65 20 3d        int size =
158a5 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
158a6 69 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 6e 74  i+2]);.      int
158a7 20 6a 3b 0a 20 20 20 20 20 20 69 66 28 20 28 69   j;.      if( (i
158a8 2b 73 69 7a 65 2d 31 29 3e 3d 75 73 61 62 6c 65  +size-1)>=usable
158a9 53 69 7a 65 20 7c 7c 20 69 3c 30 20 29 7b 0a 20  Size || i<0 ){. 
158aa 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65         checkAppe
158ab 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 30 2c  ndMsg(pCheck, 0,
158ac 20 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 22    .            "
158ad 43 6f 72 72 75 70 74 69 6f 6e 20 64 65 74 65 63  Corruption detec
158ae 74 65 64 20 69 6e 20 63 65 6c 6c 20 25 64 20 6f  ted in cell %d o
158af 6e 20 70 61 67 65 20 25 64 22 2c 69 2c 69 50 61  n page %d",i,iPa
158b0 67 65 2c 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c  ge,0);.      }el
158b1 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  se{.        for(
158b2 6a 3d 69 2b 73 69 7a 65 2d 31 3b 20 6a 3e 3d 69  j=i+size-1; j>=i
158b3 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a 5d 2b 2b 3b  ; j--) hit[j]++;
158b4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
158b5 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
158b6 61 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20  a[i]);.    }.   
158b7 20 66 6f 72 28 69 3d 63 6e 74 3d 30 3b 20 69 3c   for(i=cnt=0; i<
158b8 75 73 61 62 6c 65 53 69 7a 65 3b 20 69 2b 2b 29  usableSize; i++)
158b9 7b 0a 20 20 20 20 20 20 69 66 28 20 68 69 74 5b  {.      if( hit[
158ba 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  i]==0 ){.       
158bb 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65   cnt++;.      }e
158bc 6c 73 65 20 69 66 28 20 68 69 74 5b 69 5d 3e 31  lse if( hit[i]>1
158bd 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63   ){.        chec
158be 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63  kAppendMsg(pChec
158bf 6b 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20  k, 0,.          
158c0 22 4d 75 6c 74 69 70 6c 65 20 75 73 65 73 20 66  "Multiple uses f
158c1 6f 72 20 62 79 74 65 20 25 64 20 6f 66 20 70 61  or byte %d of pa
158c2 67 65 20 25 64 22 2c 20 69 2c 20 69 50 61 67 65  ge %d", i, iPage
158c3 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
158c4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
158c5 20 20 20 20 69 66 28 20 63 6e 74 21 3d 64 61 74      if( cnt!=dat
158c6 61 5b 68 64 72 2b 37 5d 20 29 7b 0a 20 20 20 20  a[hdr+7] ){.    
158c7 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67    checkAppendMsg
158c8 28 70 43 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20  (pCheck, 0, .   
158c9 20 20 20 20 20 20 20 22 46 72 61 67 6d 65 6e 74         "Fragment
158ca 65 64 20 73 70 61 63 65 20 69 73 20 25 64 20 62  ed space is %d b
158cb 79 74 65 20 72 65 70 6f 72 74 65 64 20 61 73 20  yte reported as 
158cc 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 0a  %d on page %d",.
158cd 20 20 20 20 20 20 20 20 20 20 63 6e 74 2c 20 64            cnt, d
158ce 61 74 61 5b 68 64 72 2b 37 5d 2c 20 69 50 61 67  ata[hdr+7], iPag
158cf 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e);.    }.  }.  
158d0 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28  sqlite3PageFree(
158d1 68 69 74 29 3b 0a 0a 20 20 72 65 6c 65 61 73 65  hit);..  release
158d2 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 72  Page(pPage);.  r
158d3 65 74 75 72 6e 20 64 65 70 74 68 2b 31 3b 0a 7d  eturn depth+1;.}
158d4 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
158d5 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59  E_OMIT_INTEGRITY
158d6 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64  _CHECK */..#ifnd
158d7 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
158d8 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f  NTEGRITY_CHECK./
158d9 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
158da 65 20 64 6f 65 73 20 61 20 63 6f 6d 70 6c 65 74  e does a complet
158db 65 20 63 68 65 63 6b 20 6f 66 20 74 68 65 20 67  e check of the g
158dc 69 76 65 6e 20 42 54 72 65 65 20 66 69 6c 65 2e  iven BTree file.
158dd 20 20 61 52 6f 6f 74 5b 5d 20 69 73 0a 2a 2a 20    aRoot[] is.** 
158de 61 6e 20 61 72 72 61 79 20 6f 66 20 70 61 67 65  an array of page
158df 73 20 6e 75 6d 62 65 72 73 20 77 65 72 65 20 65  s numbers were e
158e0 61 63 68 20 70 61 67 65 20 6e 75 6d 62 65 72 20  ach page number 
158e1 69 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  is the root page
158e2 20 6f 66 0a 2a 2a 20 61 20 74 61 62 6c 65 2e 20   of.** a table. 
158e3 20 6e 52 6f 6f 74 20 69 73 20 74 68 65 20 6e 75   nRoot is the nu
158e4 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
158e5 69 6e 20 61 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20  in aRoot..**.** 
158e6 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72  Write the number
158e7 20 6f 66 20 65 72 72 6f 72 20 73 65 65 6e 20 69   of error seen i
158e8 6e 20 2a 70 6e 45 72 72 2e 20 20 45 78 63 65 70  n *pnErr.  Excep
158e9 74 20 66 6f 72 20 73 6f 6d 65 20 6d 65 6d 6f 72  t for some memor
158ea 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  y.** allocation 
158eb 65 72 72 6f 72 73 2c 20 20 6e 6e 20 65 72 72 6f  errors,  nn erro
158ec 72 20 6d 65 73 73 61 67 65 20 69 73 20 68 65 6c  r message is hel
158ed 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61  d in memory obta
158ee 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c  ined from.** mal
158ef 6c 6f 63 20 69 73 20 72 65 74 75 72 6e 65 64 20  loc is returned 
158f0 69 66 20 2a 70 6e 45 72 72 20 69 73 20 6e 6f 6e  if *pnErr is non
158f1 2d 7a 65 72 6f 2e 20 20 49 66 20 2a 70 6e 45 72  -zero.  If *pnEr
158f2 72 3d 3d 30 20 74 68 65 6e 20 4e 55 4c 4c 20 69  r==0 then NULL i
158f3 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 0a 2a  s.** returned..*
158f4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
158f5 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74   char *sqlite3Bt
158f6 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 63  reeIntegrityChec
158f7 6b 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 20  k(.  Btree *p,  
158f8 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20     /* The btree 
158f9 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a 2f  to be checked */
158fa 0a 20 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 20  .  int *aRoot,  
158fb 20 2f 2a 20 41 6e 20 61 72 72 61 79 20 6f 66 20   /* An array of 
158fc 72 6f 6f 74 20 70 61 67 65 73 20 6e 75 6d 62 65  root pages numbe
158fd 72 73 20 66 6f 72 20 69 6e 64 69 76 69 64 75 61  rs for individua
158fe 6c 20 74 72 65 65 73 20 2a 2f 0a 20 20 69 6e 74  l trees */.  int
158ff 20 6e 52 6f 6f 74 2c 20 20 20 20 2f 2a 20 4e 75   nRoot,    /* Nu
15900 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
15901 69 6e 20 61 52 6f 6f 74 5b 5d 20 2a 2f 0a 20 20  in aRoot[] */.  
15902 69 6e 74 20 6d 78 45 72 72 2c 20 20 20 20 2f 2a  int mxErr,    /*
15903 20 53 74 6f 70 20 72 65 70 6f 72 74 69 6e 67 20   Stop reporting 
15904 65 72 72 6f 72 73 20 61 66 74 65 72 20 74 68 69  errors after thi
15905 73 20 6d 61 6e 79 20 2a 2f 0a 20 20 69 6e 74 20  s many */.  int 
15906 2a 70 6e 45 72 72 20 20 20 20 2f 2a 20 57 72 69  *pnErr    /* Wri
15907 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72  te number of err
15908 6f 72 73 20 73 65 65 6e 20 74 6f 20 74 68 69 73  ors seen to this
15909 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 29 7b 0a   variable */.){.
1590a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e    int i;.  int n
1590b 52 65 66 3b 0a 20 20 49 6e 74 65 67 72 69 74 79  Ref;.  Integrity
1590c 43 6b 20 73 43 68 65 63 6b 3b 0a 20 20 42 74 53  Ck sCheck;.  BtS
1590d 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
1590e 70 42 74 3b 0a 20 20 63 68 61 72 20 7a 45 72 72  pBt;.  char zErr
1590f 5b 31 30 30 5d 3b 0a 0a 20 20 73 71 6c 69 74 65  [100];..  sqlite
15910 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
15911 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64    pBt->db = p->d
15912 62 3b 0a 20 20 6e 52 65 66 20 3d 20 73 71 6c 69  b;.  nRef = sqli
15913 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74  te3PagerRefcount
15914 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20  (pBt->pPager);. 
15915 20 69 66 28 20 6c 6f 63 6b 42 74 72 65 65 57 69   if( lockBtreeWi
15916 74 68 52 65 74 72 79 28 70 29 21 3d 53 51 4c 49  thRetry(p)!=SQLI
15917 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 6e  TE_OK ){.    *pn
15918 45 72 72 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c  Err = 1;.    sql
15919 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
1591a 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71  );.    return sq
1591b 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30 2c  lite3DbStrDup(0,
1591c 20 22 63 61 6e 6e 6f 74 20 61 63 71 75 69 72 65   "cannot acquire
1591d 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20   a read lock on 
1591e 74 68 65 20 64 61 74 61 62 61 73 65 22 29 3b 0a  the database");.
1591f 20 20 7d 0a 20 20 73 43 68 65 63 6b 2e 70 42 74    }.  sCheck.pBt
15920 20 3d 20 70 42 74 3b 0a 20 20 73 43 68 65 63 6b   = pBt;.  sCheck
15921 2e 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70  .pPager = pBt->p
15922 50 61 67 65 72 3b 0a 20 20 73 43 68 65 63 6b 2e  Pager;.  sCheck.
15923 6e 50 61 67 65 20 3d 20 70 61 67 65 72 50 61 67  nPage = pagerPag
15924 65 63 6f 75 6e 74 28 73 43 68 65 63 6b 2e 70 50  ecount(sCheck.pP
15925 61 67 65 72 29 3b 0a 20 20 73 43 68 65 63 6b 2e  ager);.  sCheck.
15926 6d 78 45 72 72 20 3d 20 6d 78 45 72 72 3b 0a 20  mxErr = mxErr;. 
15927 20 73 43 68 65 63 6b 2e 6e 45 72 72 20 3d 20 30   sCheck.nErr = 0
15928 3b 0a 20 20 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f  ;.  sCheck.mallo
15929 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 2a  cFailed = 0;.  *
1592a 70 6e 45 72 72 20 3d 20 30 3b 0a 23 69 66 6e 64  pnErr = 0;.#ifnd
1592b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1592c 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66 28 20  UTOVACUUM.  if( 
1592d 70 42 74 2d 3e 6e 54 72 75 6e 63 21 3d 30 20 29  pBt->nTrunc!=0 )
1592e 7b 0a 20 20 20 20 73 43 68 65 63 6b 2e 6e 50 61  {.    sCheck.nPa
1592f 67 65 20 3d 20 70 42 74 2d 3e 6e 54 72 75 6e 63  ge = pBt->nTrunc
15930 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69  ;.  }.#endif.  i
15931 66 28 20 73 43 68 65 63 6b 2e 6e 50 61 67 65 3d  f( sCheck.nPage=
15932 3d 30 20 29 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b  =0 ){.    unlock
15933 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42  BtreeIfUnused(pB
15934 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  t);.    sqlite3B
15935 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
15936 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
15937 20 20 73 43 68 65 63 6b 2e 61 6e 52 65 66 20 3d    sCheck.anRef =
15938 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20   sqlite3Malloc( 
15939 28 73 43 68 65 63 6b 2e 6e 50 61 67 65 2b 31 29  (sCheck.nPage+1)
1593a 2a 73 69 7a 65 6f 66 28 73 43 68 65 63 6b 2e 61  *sizeof(sCheck.a
1593b 6e 52 65 66 5b 30 5d 29 20 29 3b 0a 20 20 69 66  nRef[0]) );.  if
1593c 28 20 21 73 43 68 65 63 6b 2e 61 6e 52 65 66 20  ( !sCheck.anRef 
1593d 29 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72  ){.    unlockBtr
1593e 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b  eeIfUnused(pBt);
1593f 0a 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 31 3b  .    *pnErr = 1;
15940 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
15941 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72  eLeave(p);.    r
15942 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66  eturn 0;.  }.  f
15943 6f 72 28 69 3d 30 3b 20 69 3c 3d 73 43 68 65 63  or(i=0; i<=sChec
15944 6b 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 73  k.nPage; i++){ s
15945 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d  Check.anRef[i] =
15946 20 30 3b 20 7d 0a 20 20 69 20 3d 20 50 45 4e 44   0; }.  i = PEND
15947 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
15948 74 29 3b 0a 20 20 69 66 28 20 69 3c 3d 73 43 68  t);.  if( i<=sCh
15949 65 63 6b 2e 6e 50 61 67 65 20 29 7b 0a 20 20 20  eck.nPage ){.   
1594a 20 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d   sCheck.anRef[i]
1594b 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c 69   = 1;.  }.  sqli
1594c 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28  te3StrAccumInit(
1594d 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 2c 20  &sCheck.errMsg, 
1594e 7a 45 72 72 2c 20 73 69 7a 65 6f 66 28 7a 45 72  zErr, sizeof(zEr
1594f 72 29 2c 20 32 30 30 30 30 29 3b 0a 0a 20 20 2f  r), 20000);..  /
15950 2a 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65  * Check the inte
15951 67 72 69 74 79 20 6f 66 20 74 68 65 20 66 72 65  grity of the fre
15952 65 6c 69 73 74 0a 20 20 2a 2f 0a 20 20 63 68 65  elist.  */.  che
15953 63 6b 4c 69 73 74 28 26 73 43 68 65 63 6b 2c 20  ckList(&sCheck, 
15954 31 2c 20 67 65 74 34 62 79 74 65 28 26 70 42 74  1, get4byte(&pBt
15955 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  ->pPage1->aData[
15956 33 32 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20  32]),.          
15957 20 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d    get4byte(&pBt-
15958 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  >pPage1->aData[3
15959 36 5d 29 2c 20 22 4d 61 69 6e 20 66 72 65 65 6c  6]), "Main freel
1595a 69 73 74 3a 20 22 29 3b 0a 0a 20 20 2f 2a 20 43  ist: ");..  /* C
1595b 68 65 63 6b 20 61 6c 6c 20 74 68 65 20 74 61 62  heck all the tab
1595c 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  les..  */.  for(
1595d 69 3d 30 3b 20 69 3c 6e 52 6f 6f 74 20 26 26 20  i=0; i<nRoot && 
1595e 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69 2b  sCheck.mxErr; i+
1595f 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 52 6f 6f  +){.    if( aRoo
15960 74 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  t[i]==0 ) contin
15961 75 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ue;.#ifndef SQLI
15962 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
15963 55 4d 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e  UM.    if( pBt->
15964 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 61 52  autoVacuum && aR
15965 6f 6f 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20  oot[i]>1 ){.    
15966 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 26 73    checkPtrmap(&s
15967 43 68 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c  Check, aRoot[i],
15968 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45   PTRMAP_ROOTPAGE
15969 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 23  , 0, 0);.    }.#
1596a 65 6e 64 69 66 0a 20 20 20 20 63 68 65 63 6b 54  endif.    checkT
1596b 72 65 65 50 61 67 65 28 26 73 43 68 65 63 6b 2c  reePage(&sCheck,
1596c 20 61 52 6f 6f 74 5b 69 5d 2c 20 30 2c 20 22 4c   aRoot[i], 0, "L
1596d 69 73 74 20 6f 66 20 74 72 65 65 20 72 6f 6f 74  ist of tree root
1596e 73 3a 20 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  s: ");.  }..  /*
1596f 20 4d 61 6b 65 20 73 75 72 65 20 65 76 65 72 79   Make sure every
15970 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c   page in the fil
15971 65 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 0a  e is referenced.
15972 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b 20    */.  for(i=1; 
15973 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65 20  i<=sCheck.nPage 
15974 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b  && sCheck.mxErr;
15975 20 69 2b 2b 29 7b 0a 23 69 66 64 65 66 20 53 51   i++){.#ifdef SQ
15976 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
15977 43 55 55 4d 0a 20 20 20 20 69 66 28 20 73 43 68  CUUM.    if( sCh
15978 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20  eck.anRef[i]==0 
15979 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70  ){.      checkAp
1597a 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c  pendMsg(&sCheck,
1597b 20 30 2c 20 22 50 61 67 65 20 25 64 20 69 73 20   0, "Page %d is 
1597c 6e 65 76 65 72 20 75 73 65 64 22 2c 20 69 29 3b  never used", i);
1597d 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20  .    }.#else.   
1597e 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62   /* If the datab
1597f 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74  ase supports aut
15980 6f 2d 76 61 63 75 75 6d 2c 20 6d 61 6b 65 20 73  o-vacuum, make s
15981 75 72 65 20 6e 6f 20 74 61 62 6c 65 73 20 63 6f  ure no tables co
15982 6e 74 61 69 6e 0a 20 20 20 20 2a 2a 20 72 65 66  ntain.    ** ref
15983 65 72 65 6e 63 65 73 20 74 6f 20 70 6f 69 6e 74  erences to point
15984 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 0a 20 20  er-map pages..  
15985 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 43 68    */.    if( sCh
15986 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20  eck.anRef[i]==0 
15987 26 26 20 0a 20 20 20 20 20 20 20 28 50 54 52 4d  && .       (PTRM
15988 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 69  AP_PAGENO(pBt, i
15989 29 21 3d 69 20 7c 7c 20 21 70 42 74 2d 3e 61 75  )!=i || !pBt->au
1598a 74 6f 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 20  toVacuum) ){.   
1598b 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73     checkAppendMs
1598c 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50  g(&sCheck, 0, "P
1598d 61 67 65 20 25 64 20 69 73 20 6e 65 76 65 72 20  age %d is never 
1598e 75 73 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 7d  used", i);.    }
1598f 0a 20 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e  .    if( sCheck.
15990 61 6e 52 65 66 5b 69 5d 21 3d 30 20 26 26 20 0a  anRef[i]!=0 && .
15991 20 20 20 20 20 20 20 28 50 54 52 4d 41 50 5f 50         (PTRMAP_P
15992 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 3d 3d 69  AGENO(pBt, i)==i
15993 20 26 26 20 70 42 74 2d 3e 61 75 74 6f 56 61 63   && pBt->autoVac
15994 75 75 6d 29 20 29 7b 0a 20 20 20 20 20 20 63 68  uum) ){.      ch
15995 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43  eckAppendMsg(&sC
15996 68 65 63 6b 2c 20 30 2c 20 22 50 6f 69 6e 74 65  heck, 0, "Pointe
15997 72 20 6d 61 70 20 70 61 67 65 20 25 64 20 69 73  r map page %d is
15998 20 72 65 66 65 72 65 6e 63 65 64 22 2c 20 69 29   referenced", i)
15999 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
1599a 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75   }..  /* Make su
1599b 72 65 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73  re this analysis
1599c 20 64 69 64 20 6e 6f 74 20 6c 65 61 76 65 20 61   did not leave a
1599d 6e 79 20 75 6e 72 65 66 28 29 20 70 61 67 65 73  ny unref() pages
1599e 0a 20 20 2a 2f 0a 20 20 75 6e 6c 6f 63 6b 42 74  .  */.  unlockBt
1599f 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29  reeIfUnused(pBt)
159a0 3b 0a 20 20 69 66 28 20 6e 52 65 66 20 21 3d 20  ;.  if( nRef != 
159a1 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63  sqlite3PagerRefc
159a2 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72  ount(pBt->pPager
159a3 29 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70  ) ){.    checkAp
159a4 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c  pendMsg(&sCheck,
159a5 20 30 2c 20 0a 20 20 20 20 20 20 22 4f 75 74 73   0, .      "Outs
159a6 74 61 6e 64 69 6e 67 20 70 61 67 65 20 63 6f 75  tanding page cou
159a7 6e 74 20 67 6f 65 73 20 66 72 6f 6d 20 25 64 20  nt goes from %d 
159a8 74 6f 20 25 64 20 64 75 72 69 6e 67 20 74 68 69  to %d during thi
159a9 73 20 61 6e 61 6c 79 73 69 73 22 2c 0a 20 20 20  s analysis",.   
159aa 20 20 20 6e 52 65 66 2c 20 73 71 6c 69 74 65 33     nRef, sqlite3
159ab 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 42  PagerRefcount(pB
159ac 74 2d 3e 70 50 61 67 65 72 29 0a 20 20 20 20 29  t->pPager).    )
159ad 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61  ;.  }..  /* Clea
159ae 6e 20 20 75 70 20 61 6e 64 20 72 65 70 6f 72 74  n  up and report
159af 20 65 72 72 6f 72 73 2e 0a 20 20 2a 2f 0a 20 20   errors..  */.  
159b0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
159b1 65 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  e(p);.  sqlite3_
159b2 66 72 65 65 28 73 43 68 65 63 6b 2e 61 6e 52 65  free(sCheck.anRe
159b3 66 29 3b 0a 20 20 69 66 28 20 73 43 68 65 63 6b  f);.  if( sCheck
159b4 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  .mallocFailed ){
159b5 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41  .    sqlite3StrA
159b6 63 63 75 6d 52 65 73 65 74 28 26 73 43 68 65 63  ccumReset(&sChec
159b7 6b 2e 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 2a  k.errMsg);.    *
159b8 70 6e 45 72 72 20 3d 20 73 43 68 65 63 6b 2e 6e  pnErr = sCheck.n
159b9 45 72 72 2b 31 3b 0a 20 20 20 20 72 65 74 75 72  Err+1;.    retur
159ba 6e 20 30 3b 0a 20 20 7d 0a 20 20 2a 70 6e 45 72  n 0;.  }.  *pnEr
159bb 72 20 3d 20 73 43 68 65 63 6b 2e 6e 45 72 72 3b  r = sCheck.nErr;
159bc 0a 20 20 69 66 28 20 73 43 68 65 63 6b 2e 6e 45  .  if( sCheck.nE
159bd 72 72 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 53  rr==0 ) sqlite3S
159be 74 72 41 63 63 75 6d 52 65 73 65 74 28 26 73 43  trAccumReset(&sC
159bf 68 65 63 6b 2e 65 72 72 4d 73 67 29 3b 0a 20 20  heck.errMsg);.  
159c0 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 53 74  return sqlite3St
159c1 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 73 43  rAccumFinish(&sC
159c2 68 65 63 6b 2e 65 72 72 4d 73 67 29 3b 0a 7d 0a  heck.errMsg);.}.
159c3 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
159c4 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f  _OMIT_INTEGRITY_
159c5 43 48 45 43 4b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  CHECK */../*.** 
159c6 52 65 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20  Return the full 
159c7 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20  pathname of the 
159c8 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61 62  underlying datab
159c9 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  ase file..**.** 
159ca 54 68 65 20 70 61 67 65 72 20 66 69 6c 65 6e 61  The pager filena
159cb 6d 65 20 69 73 20 69 6e 76 61 72 69 61 6e 74 20  me is invariant 
159cc 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 70  as long as the p
159cd 61 67 65 72 20 69 73 0a 2a 2a 20 6f 70 65 6e 20  ager is.** open 
159ce 73 6f 20 69 74 20 69 73 20 73 61 66 65 20 74 6f  so it is safe to
159cf 20 61 63 63 65 73 73 20 77 69 74 68 6f 75 74 20   access without 
159d0 74 68 65 20 42 74 53 68 61 72 65 64 20 6d 75 74  the BtShared mut
159d1 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ex..*/.SQLITE_PR
159d2 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72  IVATE const char
159d3 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47 65   *sqlite3BtreeGe
159d4 74 46 69 6c 65 6e 61 6d 65 28 42 74 72 65 65 20  tFilename(Btree 
159d5 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
159d6 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 21 3d 30  ->pBt->pPager!=0
159d7 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c   );.  return sql
159d8 69 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d  ite3PagerFilenam
159d9 65 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72  e(p->pBt->pPager
159da 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
159db 72 6e 20 74 68 65 20 70 61 74 68 6e 61 6d 65 20  rn the pathname 
159dc 6f 66 20 74 68 65 20 64 69 72 65 63 74 6f 72 79  of the directory
159dd 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74   that contains t
159de 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
159df 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65  ..**.** The page
159e0 72 20 64 69 72 65 63 74 6f 72 79 20 6e 61 6d 65  r directory name
159e1 20 69 73 20 69 6e 76 61 72 69 61 6e 74 20 61 73   is invariant as
159e2 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 70 61 67   long as the pag
159e3 65 72 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f  er is.** open so
159e4 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 61   it is safe to a
159e5 63 63 65 73 73 20 77 69 74 68 6f 75 74 20 74 68  ccess without th
159e6 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78  e BtShared mutex
159e7 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
159e8 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
159e9 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 44  sqlite3BtreeGetD
159ea 69 72 6e 61 6d 65 28 42 74 72 65 65 20 2a 70 29  irname(Btree *p)
159eb 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70  {.  assert( p->p
159ec 42 74 2d 3e 70 50 61 67 65 72 21 3d 30 20 29 3b  Bt->pPager!=0 );
159ed 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
159ee 33 50 61 67 65 72 44 69 72 6e 61 6d 65 28 70 2d  3PagerDirname(p-
159ef 3e 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 7d  >pBt->pPager);.}
159f0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
159f1 68 65 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74  he pathname of t
159f2 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
159f3 66 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 73  for this databas
159f4 65 2e 20 54 68 65 20 72 65 74 75 72 6e 0a 2a 2a  e. The return.**
159f5 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 72   value of this r
159f6 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 73 61  outine is the sa
159f7 6d 65 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66  me regardless of
159f8 20 77 68 65 74 68 65 72 20 74 68 65 20 6a 6f 75   whether the jou
159f9 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 68 61 73  rnal file.** has
159fa 20 62 65 65 6e 20 63 72 65 61 74 65 64 20 6f 72   been created or
159fb 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20   not..**.** The 
159fc 70 61 67 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  pager journal fi
159fd 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76 61 72 69  lename is invari
159fe 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 74  ant as long as t
159ff 68 65 20 70 61 67 65 72 20 69 73 0a 2a 2a 20 6f  he pager is.** o
15a00 70 65 6e 20 73 6f 20 69 74 20 69 73 20 73 61 66  pen so it is saf
15a01 65 20 74 6f 20 61 63 63 65 73 73 20 77 69 74 68  e to access with
15a02 6f 75 74 20 74 68 65 20 42 74 53 68 61 72 65 64  out the BtShared
15a03 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54   mutex..*/.SQLIT
15a04 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
15a05 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72  char *sqlite3Btr
15a06 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65  eeGetJournalname
15a07 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73  (Btree *p){.  as
15a08 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50  sert( p->pBt->pP
15a09 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 72 65 74  ager!=0 );.  ret
15a0a 75 72 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72  urn sqlite3Pager
15a0b 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 70 2d 3e 70  Journalname(p->p
15a0c 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 7d 0a 0a  Bt->pPager);.}..
15a0d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
15a0e 4d 49 54 5f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a  MIT_VACUUM./*.**
15a0f 20 43 6f 70 79 20 74 68 65 20 63 6f 6d 70 6c 65   Copy the comple
15a10 74 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70 42  te content of pB
15a11 74 46 72 6f 6d 20 69 6e 74 6f 20 70 42 74 54 6f  tFrom into pBtTo
15a12 2e 20 20 41 20 74 72 61 6e 73 61 63 74 69 6f 6e  .  A transaction
15a13 0a 2a 2a 20 6d 75 73 74 20 62 65 20 61 63 74 69  .** must be acti
15a14 76 65 20 66 6f 72 20 62 6f 74 68 20 66 69 6c 65  ve for both file
15a15 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a  s..**.** The siz
15a16 65 20 6f 66 20 66 69 6c 65 20 70 54 6f 20 6d 61  e of file pTo ma
15a17 79 20 62 65 20 72 65 64 75 63 65 64 20 62 79 20  y be reduced by 
15a18 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a  this operation..
15a19 2a 2a 20 49 66 20 61 6e 79 74 68 69 6e 67 20 67  ** If anything g
15a1a 6f 65 73 20 77 72 6f 6e 67 2c 20 74 68 65 20 74  oes wrong, the t
15a1b 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 70 54  ransaction on pT
15a1c 6f 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b  o is rolled back
15a1d 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63  . .**.** If succ
15a1e 65 73 73 66 75 6c 2c 20 43 6f 6d 6d 69 74 50 68  essful, CommitPh
15a1f 61 73 65 4f 6e 65 28 29 20 6d 61 79 20 62 65 20  aseOne() may be 
15a20 63 61 6c 6c 65 64 20 6f 6e 20 70 54 6f 20 62 65  called on pTo be
15a21 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 20  fore returning. 
15a22 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 73  .** The caller s
15a23 68 6f 75 6c 64 20 66 69 6e 69 73 68 20 63 6f 6d  hould finish com
15a24 6d 69 74 74 69 6e 67 20 74 68 65 20 74 72 61 6e  mitting the tran
15a25 73 61 63 74 69 6f 6e 20 6f 6e 20 70 54 6f 20 62  saction on pTo b
15a26 79 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c  y calling.** sql
15a27 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28  ite3BtreeCommit(
15a28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
15a29 20 62 74 72 65 65 43 6f 70 79 46 69 6c 65 28 42   btreeCopyFile(B
15a2a 74 72 65 65 20 2a 70 54 6f 2c 20 42 74 72 65 65  tree *pTo, Btree
15a2b 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 6e 74 20   *pFrom){.  int 
15a2c 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
15a2d 20 20 50 67 6e 6f 20 69 3b 0a 0a 20 20 50 67 6e    Pgno i;..  Pgn
15a2e 6f 20 6e 46 72 6f 6d 50 61 67 65 3b 20 20 20 20  o nFromPage;    
15a2f 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
15a30 67 65 73 20 69 6e 20 70 46 72 6f 6d 20 2a 2f 0a  ges in pFrom */.
15a31 20 20 50 67 6e 6f 20 6e 54 6f 50 61 67 65 3b 20    Pgno nToPage; 
15a32 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
15a33 6f 66 20 70 61 67 65 73 20 69 6e 20 70 54 6f 20  of pages in pTo 
15a34 2a 2f 0a 20 20 50 67 6e 6f 20 6e 4e 65 77 50 61  */.  Pgno nNewPa
15a35 67 65 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ge;      /* Numb
15a36 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 70  er of pages in p
15a37 54 6f 20 61 66 74 65 72 20 74 68 65 20 63 6f 70  To after the cop
15a38 79 20 2a 2f 0a 0a 20 20 50 67 6e 6f 20 69 53 6b  y */..  Pgno iSk
15a39 69 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50  ip;         /* P
15a3a 65 6e 64 69 6e 67 20 62 79 74 65 20 70 61 67 65  ending byte page
15a3b 20 69 6e 20 70 54 6f 20 2a 2f 0a 20 20 69 6e 74   in pTo */.  int
15a3c 20 6e 54 6f 50 61 67 65 53 69 7a 65 3b 20 20 20   nToPageSize;   
15a3d 20 2f 2a 20 50 61 67 65 20 73 69 7a 65 20 6f 66   /* Page size of
15a3e 20 70 54 6f 20 69 6e 20 62 79 74 65 73 20 2a 2f   pTo in bytes */
15a3f 0a 20 20 69 6e 74 20 6e 46 72 6f 6d 50 61 67 65  .  int nFromPage
15a40 53 69 7a 65 3b 20 20 2f 2a 20 50 61 67 65 20 73  Size;  /* Page s
15a41 69 7a 65 20 6f 66 20 70 46 72 6f 6d 20 69 6e 20  ize of pFrom in 
15a42 62 79 74 65 73 20 2a 2f 0a 0a 20 20 42 74 53 68  bytes */..  BtSh
15a43 61 72 65 64 20 2a 70 42 74 54 6f 20 3d 20 70 54  ared *pBtTo = pT
15a44 6f 2d 3e 70 42 74 3b 0a 20 20 42 74 53 68 61 72  o->pBt;.  BtShar
15a45 65 64 20 2a 70 42 74 46 72 6f 6d 20 3d 20 70 46  ed *pBtFrom = pF
15a46 72 6f 6d 2d 3e 70 42 74 3b 0a 20 20 70 42 74 54  rom->pBt;.  pBtT
15a47 6f 2d 3e 64 62 20 3d 20 70 54 6f 2d 3e 64 62 3b  o->db = pTo->db;
15a48 0a 20 20 70 42 74 46 72 6f 6d 2d 3e 64 62 20 3d  .  pBtFrom->db =
15a49 20 70 46 72 6f 6d 2d 3e 64 62 3b 0a 0a 20 20 6e   pFrom->db;..  n
15a4a 54 6f 50 61 67 65 53 69 7a 65 20 3d 20 70 42 74  ToPageSize = pBt
15a4b 54 6f 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20  To->pageSize;.  
15a4c 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65 20 3d 20  nFromPageSize = 
15a4d 70 42 74 46 72 6f 6d 2d 3e 70 61 67 65 53 69 7a  pBtFrom->pageSiz
15a4e 65 3b 0a 0a 20 20 69 66 28 20 70 54 6f 2d 3e 69  e;..  if( pTo->i
15a4f 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52  nTrans!=TRANS_WR
15a50 49 54 45 20 7c 7c 20 70 46 72 6f 6d 2d 3e 69 6e  ITE || pFrom->in
15a51 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49  Trans!=TRANS_WRI
15a52 54 45 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  TE ){.    return
15a53 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
15a54 20 7d 0a 20 20 69 66 28 20 70 42 74 54 6f 2d 3e   }.  if( pBtTo->
15a55 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 72  pCursor ){.    r
15a56 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
15a57 59 3b 0a 20 20 7d 0a 0a 20 20 6e 54 6f 50 61 67  Y;.  }..  nToPag
15a58 65 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f 75  e = pagerPagecou
15a59 6e 74 28 70 42 74 54 6f 2d 3e 70 50 61 67 65 72  nt(pBtTo->pPager
15a5a 29 3b 0a 20 20 6e 46 72 6f 6d 50 61 67 65 20 3d  );.  nFromPage =
15a5b 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28   pagerPagecount(
15a5c 70 42 74 46 72 6f 6d 2d 3e 70 50 61 67 65 72 29  pBtFrom->pPager)
15a5d 3b 0a 20 20 69 53 6b 69 70 20 3d 20 50 45 4e 44  ;.  iSkip = PEND
15a5e 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
15a5f 74 54 6f 29 3b 0a 0a 20 20 2f 2a 20 56 61 72 69  tTo);..  /* Vari
15a60 61 62 6c 65 20 6e 4e 65 77 50 61 67 65 20 69 73  able nNewPage is
15a61 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
15a62 61 67 65 73 20 72 65 71 75 69 72 65 64 20 74 6f  ages required to
15a63 20 73 74 6f 72 65 20 74 68 65 0a 20 20 2a 2a 20   store the.  ** 
15a64 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 46 72 6f  contents of pFro
15a65 6d 20 75 73 69 6e 67 20 74 68 65 20 63 75 72 72  m using the curr
15a66 65 6e 74 20 70 61 67 65 2d 73 69 7a 65 20 6f 66  ent page-size of
15a67 20 70 54 6f 2e 0a 20 20 2a 2f 0a 20 20 6e 4e 65   pTo..  */.  nNe
15a68 77 50 61 67 65 20 3d 20 28 28 69 36 34 29 6e 46  wPage = ((i64)nF
15a69 72 6f 6d 50 61 67 65 20 2a 20 28 69 36 34 29 6e  romPage * (i64)n
15a6a 46 72 6f 6d 50 61 67 65 53 69 7a 65 20 2b 20 28  FromPageSize + (
15a6b 69 36 34 29 6e 54 6f 50 61 67 65 53 69 7a 65 20  i64)nToPageSize 
15a6c 2d 20 31 29 20 2f 20 0a 20 20 20 20 20 20 28 69  - 1) / .      (i
15a6d 36 34 29 6e 54 6f 50 61 67 65 53 69 7a 65 3b 0a  64)nToPageSize;.
15a6e 0a 20 20 66 6f 72 28 69 3d 31 3b 20 72 63 3d 3d  .  for(i=1; rc==
15a6f 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 69 3c  SQLITE_OK && (i<
15a70 3d 6e 54 6f 50 61 67 65 20 7c 7c 20 69 3c 3d 6e  =nToPage || i<=n
15a71 4e 65 77 50 61 67 65 29 3b 20 69 2b 2b 29 7b 0a  NewPage); i++){.
15a72 0a 20 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20  .    /* Journal 
15a73 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67  the original pag
15a74 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  e..    **.    **
15a75 20 69 53 6b 69 70 20 69 73 20 74 68 65 20 70 61   iSkip is the pa
15a76 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
15a77 20 6c 6f 63 6b 69 6e 67 20 70 61 67 65 20 28 50   locking page (P
15a78 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
15a79 29 0a 20 20 20 20 2a 2a 20 69 6e 20 64 61 74 61  ).    ** in data
15a7a 62 61 73 65 20 2a 70 54 6f 20 28 62 65 66 6f 72  base *pTo (befor
15a7b 65 20 74 68 65 20 63 6f 70 79 29 2e 20 54 68 69  e the copy). Thi
15a7c 73 20 70 61 67 65 20 69 73 20 6e 65 76 65 72 20  s page is never 
15a7d 77 72 69 74 74 65 6e 20 0a 20 20 20 20 2a 2a 20  written .    ** 
15a7e 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  into the journal
15a7f 20 66 69 6c 65 2e 20 55 6e 6c 65 73 73 20 69 3d   file. Unless i=
15a80 3d 69 53 6b 69 70 20 6f 72 20 74 68 65 20 70 61  =iSkip or the pa
15a81 67 65 20 77 61 73 20 6e 6f 74 0a 20 20 20 20 2a  ge was not.    *
15a82 2a 20 70 72 65 73 65 6e 74 20 69 6e 20 70 54 6f  * present in pTo
15a83 20 62 65 66 6f 72 65 20 74 68 65 20 63 6f 70 79   before the copy
15a84 20 6f 70 65 72 61 74 69 6f 6e 2c 20 6a 6f 75 72   operation, jour
15a85 6e 61 6c 20 70 61 67 65 20 69 20 66 72 6f 6d 20  nal page i from 
15a86 70 54 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  pTo..    */.    
15a87 69 66 28 20 69 21 3d 69 53 6b 69 70 20 26 26 20  if( i!=iSkip && 
15a88 69 3c 3d 6e 54 6f 50 61 67 65 20 29 7b 0a 20 20  i<=nToPage ){.  
15a89 20 20 20 20 44 62 50 61 67 65 20 2a 70 44 62 50      DbPage *pDbP
15a8a 61 67 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 72  age = 0;.      r
15a8b 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
15a8c 47 65 74 28 70 42 74 54 6f 2d 3e 70 50 61 67 65  Get(pBtTo->pPage
15a8d 72 2c 20 69 2c 20 26 70 44 62 50 61 67 65 29 3b  r, i, &pDbPage);
15a8e 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
15a8f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
15a90 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
15a91 50 61 67 65 72 57 72 69 74 65 28 70 44 62 50 61  PagerWrite(pDbPa
15a92 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ge);.        if(
15a93 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
15a94 26 20 69 3e 6e 46 72 6f 6d 50 61 67 65 20 29 7b  & i>nFromPage ){
15a95 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 59 65  .          /* Ye
15a96 61 68 2e 20 20 49 74 20 73 65 65 6d 73 20 77 69  ah.  It seems wi
15a97 65 72 64 20 74 6f 20 63 61 6c 6c 20 44 6f 6e 74  erd to call Dont
15a98 57 72 69 74 65 28 29 20 72 69 67 68 74 20 61 66  Write() right af
15a99 74 65 72 20 57 72 69 74 65 28 29 2e 20 42 75 74  ter Write(). But
15a9a 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68  .          ** th
15a9b 61 74 20 69 73 20 62 65 63 61 75 73 65 20 74 68  at is because th
15a9c 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 6f 73 65  e names of those
15a9d 20 70 72 6f 63 65 64 75 72 65 73 20 64 6f 20 6e   procedures do n
15a9e 6f 74 20 65 78 61 63 74 6c 79 20 0a 20 20 20 20  ot exactly .    
15a9f 20 20 20 20 20 20 2a 2a 20 72 65 70 72 65 73 65        ** represe
15aa0 6e 74 20 77 68 61 74 20 74 68 65 79 20 64 6f 2e  nt what they do.
15aa1 20 20 57 72 69 74 65 28 29 20 72 65 61 6c 6c 79    Write() really
15aa2 20 6d 65 61 6e 73 20 22 70 75 74 20 74 68 69 73   means "put this
15aa3 20 70 61 67 65 20 69 6e 20 74 68 65 0a 20 20 20   page in the.   
15aa4 20 20 20 20 20 20 20 2a 2a 20 72 6f 6c 6c 62 61         ** rollba
15aa5 63 6b 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 6d  ck journal and m
15aa6 61 72 6b 20 69 74 20 61 73 20 64 69 72 74 79 20  ark it as dirty 
15aa7 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20  so that it will 
15aa8 62 65 20 77 72 69 74 74 65 6e 0a 20 20 20 20 20  be written.     
15aa9 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 64       ** to the d
15aaa 61 74 61 62 61 73 65 20 66 69 6c 65 20 6c 61 74  atabase file lat
15aab 65 72 2e 22 20 20 44 6f 6e 74 57 72 69 74 65 28  er."  DontWrite(
15aac 29 20 75 6e 64 6f 65 73 20 74 68 65 20 73 65 63  ) undoes the sec
15aad 6f 6e 64 20 70 61 72 74 20 6f 66 0a 20 20 20 20  ond part of.    
15aae 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 61 6e        ** that an
15aaf 64 20 70 72 65 76 65 6e 74 73 20 74 68 65 20 70  d prevents the p
15ab0 61 67 65 20 66 72 6f 6d 20 62 65 69 6e 67 20 77  age from being w
15ab1 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61  ritten to the da
15ab2 74 61 62 61 73 65 2e 20 54 68 65 0a 20 20 20 20  tabase. The.    
15ab3 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69 73        ** page is
15ab4 20 73 74 69 6c 6c 20 6f 6e 20 74 68 65 20 72 6f   still on the ro
15ab5 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20  llback journal, 
15ab6 74 68 6f 75 67 68 2e 20 20 41 6e 64 20 74 68 61  though.  And tha
15ab7 74 20 69 73 20 74 68 65 20 0a 20 20 20 20 20 20  t is the .      
15ab8 20 20 20 20 2a 2a 20 77 68 6f 6c 65 20 70 6f 69      ** whole poi
15ab9 6e 74 20 6f 66 20 74 68 69 73 20 62 6c 6f 63 6b  nt of this block
15aba 3a 20 74 6f 20 70 75 74 20 70 61 67 65 73 20 6f  : to put pages o
15abb 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a  n the rollback j
15abc 6f 75 72 6e 61 6c 2e 20 0a 20 20 20 20 20 20 20  ournal. .       
15abd 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
15abe 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
15abf 72 44 6f 6e 74 57 72 69 74 65 28 70 44 62 50 61  rDontWrite(pDbPa
15ac0 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ge);.        }. 
15ac1 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
15ac2 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 65  gerUnref(pDbPage
15ac3 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
15ac4 0a 0a 20 20 20 20 2f 2a 20 4f 76 65 72 77 72 69  ..    /* Overwri
15ac5 74 65 20 74 68 65 20 64 61 74 61 20 69 6e 20 70  te the data in p
15ac6 61 67 65 20 69 20 6f 66 20 74 68 65 20 74 61 72  age i of the tar
15ac7 67 65 74 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  get database */.
15ac8 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
15ac9 54 45 5f 4f 4b 20 26 26 20 69 21 3d 69 53 6b 69  TE_OK && i!=iSki
15aca 70 20 26 26 20 69 3c 3d 6e 4e 65 77 50 61 67 65  p && i<=nNewPage
15acb 20 29 7b 0a 0a 20 20 20 20 20 20 44 62 50 61 67   ){..      DbPag
15acc 65 20 2a 70 54 6f 50 61 67 65 20 3d 20 30 3b 0a  e *pToPage = 0;.
15acd 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e        sqlite3_in
15ace 74 36 34 20 69 4f 66 66 3b 0a 0a 20 20 20 20 20  t64 iOff;..     
15acf 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
15ad0 65 72 47 65 74 28 70 42 74 54 6f 2d 3e 70 50 61  erGet(pBtTo->pPa
15ad1 67 65 72 2c 20 69 2c 20 26 70 54 6f 50 61 67 65  ger, i, &pToPage
15ad2 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
15ad3 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
15ad4 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
15ad5 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 6f  e3PagerWrite(pTo
15ad6 50 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a 0a  Page);.      }..
15ad7 20 20 20 20 20 20 66 6f 72 28 0a 20 20 20 20 20        for(.     
15ad8 20 20 20 69 4f 66 66 3d 28 69 2d 31 29 2a 6e 54     iOff=(i-1)*nT
15ad9 6f 50 61 67 65 53 69 7a 65 3b 20 0a 20 20 20 20  oPageSize; .    
15ada 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f      rc==SQLITE_O
15adb 4b 20 26 26 20 69 4f 66 66 3c 69 2a 6e 54 6f 50  K && iOff<i*nToP
15adc 61 67 65 53 69 7a 65 3b 20 0a 20 20 20 20 20 20  ageSize; .      
15add 20 20 69 4f 66 66 20 2b 3d 20 6e 46 72 6f 6d 50    iOff += nFromP
15ade 61 67 65 53 69 7a 65 0a 20 20 20 20 20 20 29 7b  ageSize.      ){
15adf 0a 20 20 20 20 20 20 20 20 44 62 50 61 67 65 20  .        DbPage 
15ae0 2a 70 46 72 6f 6d 50 61 67 65 20 3d 20 30 3b 0a  *pFromPage = 0;.
15ae1 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72          Pgno iFr
15ae2 6f 6d 20 3d 20 28 69 4f 66 66 2f 6e 46 72 6f 6d  om = (iOff/nFrom
15ae3 50 61 67 65 53 69 7a 65 29 2b 31 3b 0a 0a 20 20  PageSize)+1;..  
15ae4 20 20 20 20 20 20 69 66 28 20 69 46 72 6f 6d 3d        if( iFrom=
15ae5 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  =PENDING_BYTE_PA
15ae6 47 45 28 70 42 74 46 72 6f 6d 29 20 29 7b 0a 20  GE(pBtFrom) ){. 
15ae7 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75           continu
15ae8 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  e;.        }..  
15ae9 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
15aea 65 33 50 61 67 65 72 47 65 74 28 70 42 74 46 72  e3PagerGet(pBtFr
15aeb 6f 6d 2d 3e 70 50 61 67 65 72 2c 20 69 46 72 6f  om->pPager, iFro
15aec 6d 2c 20 26 70 46 72 6f 6d 50 61 67 65 29 3b 0a  m, &pFromPage);.
15aed 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
15aee 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
15aef 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 6f         char *zTo
15af0 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47   = sqlite3PagerG
15af1 65 74 44 61 74 61 28 70 54 6f 50 61 67 65 29 3b  etData(pToPage);
15af2 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20  .          char 
15af3 2a 7a 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65 33  *zFrom = sqlite3
15af4 50 61 67 65 72 47 65 74 44 61 74 61 28 70 46 72  PagerGetData(pFr
15af5 6f 6d 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  omPage);.       
15af6 20 20 20 69 6e 74 20 6e 43 6f 70 79 3b 0a 0a 20     int nCopy;.. 
15af7 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 46 72           if( nFr
15af8 6f 6d 50 61 67 65 53 69 7a 65 3e 3d 6e 54 6f 50  omPageSize>=nToP
15af9 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  ageSize ){.     
15afa 20 20 20 20 20 20 20 7a 46 72 6f 6d 20 2b 3d 20         zFrom += 
15afb 28 28 69 2d 31 29 2a 6e 54 6f 50 61 67 65 53 69  ((i-1)*nToPageSi
15afc 7a 65 20 2d 20 28 28 69 46 72 6f 6d 2d 31 29 2a  ze - ((iFrom-1)*
15afd 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65 29 29 3b  nFromPageSize));
15afe 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 43 6f  .            nCo
15aff 70 79 20 3d 20 6e 54 6f 50 61 67 65 53 69 7a 65  py = nToPageSize
15b00 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
15b01 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a  e{.            z
15b02 54 6f 20 2b 3d 20 28 28 28 69 46 72 6f 6d 2d 31  To += (((iFrom-1
15b03 29 2a 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65 29  )*nFromPageSize)
15b04 20 2d 20 28 69 2d 31 29 2a 6e 54 6f 50 61 67 65   - (i-1)*nToPage
15b05 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Size);.         
15b06 20 20 20 6e 43 6f 70 79 20 3d 20 6e 46 72 6f 6d     nCopy = nFrom
15b07 50 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 20  PageSize;.      
15b08 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20      }..         
15b09 20 6d 65 6d 63 70 79 28 7a 54 6f 2c 20 7a 46 72   memcpy(zTo, zFr
15b0a 6f 6d 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 20  om, nCopy);.    
15b0b 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
15b0c 65 72 55 6e 72 65 66 28 70 46 72 6f 6d 50 61 67  erUnref(pFromPag
15b0d 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  e);.        }.  
15b0e 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28      }..      if(
15b0f 20 70 54 6f 50 61 67 65 20 29 7b 0a 20 20 20 20   pToPage ){.    
15b10 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 20 3d      MemPage *p =
15b11 20 28 4d 65 6d 50 61 67 65 20 2a 29 73 71 6c 69   (MemPage *)sqli
15b12 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61  te3PagerGetExtra
15b13 28 70 54 6f 50 61 67 65 29 3b 0a 20 20 20 20 20  (pToPage);.     
15b14 20 20 20 70 2d 3e 69 73 49 6e 69 74 20 3d 20 30     p->isInit = 0
15b15 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
15b16 33 50 61 67 65 72 55 6e 72 65 66 28 70 54 6f 50  3PagerUnref(pToP
15b17 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  age);.      }.  
15b18 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66    }.  }..  /* If
15b19 20 74 68 69 6e 67 73 20 68 61 76 65 20 77 6f 72   things have wor
15b1a 6b 65 64 20 73 6f 20 66 61 72 2c 20 74 68 65 20  ked so far, the 
15b1b 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6d 61  database file ma
15b1c 79 20 6e 65 65 64 20 74 6f 20 62 65 20 0a 20 20  y need to be .  
15b1d 2a 2a 20 74 72 75 6e 63 61 74 65 64 2e 20 54 68  ** truncated. Th
15b1e 65 20 63 6f 6d 70 6c 65 78 20 70 61 72 74 20 69  e complex part i
15b1f 73 20 74 68 61 74 20 69 74 20 6d 61 79 20 6e 65  s that it may ne
15b20 65 64 20 74 6f 20 62 65 20 74 72 75 6e 63 61 74  ed to be truncat
15b21 65 64 20 74 6f 0a 20 20 2a 2a 20 61 20 73 69 7a  ed to.  ** a siz
15b22 65 20 74 68 61 74 20 69 73 20 6e 6f 74 20 61 6e  e that is not an
15b23 20 69 6e 74 65 67 65 72 20 6d 75 6c 74 69 70 6c   integer multipl
15b24 65 20 6f 66 20 6e 54 6f 50 61 67 65 53 69 7a 65  e of nToPageSize
15b25 20 2d 20 74 68 65 20 63 75 72 72 65 6e 74 0a 20   - the current. 
15b26 20 2a 2a 20 70 61 67 65 20 73 69 7a 65 20 75 73   ** page size us
15b27 65 64 20 62 79 20 74 68 65 20 70 61 67 65 72 20  ed by the pager 
15b28 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
15b29 42 2d 54 72 65 65 20 70 54 6f 2e 0a 20 20 2a 2a  B-Tree pTo..  **
15b2a 0a 20 20 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c  .  ** For exampl
15b2b 65 2c 20 73 61 79 20 74 68 65 20 70 61 67 65 2d  e, say the page-
15b2c 73 69 7a 65 20 6f 66 20 70 54 6f 20 69 73 20 32  size of pTo is 2
15b2d 30 34 38 20 62 79 74 65 73 20 61 6e 64 20 74 68  048 bytes and th
15b2e 65 20 6f 72 69 67 69 6e 61 6c 20 0a 20 20 2a 2a  e original .  **
15b2f 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
15b30 20 69 73 20 35 20 28 31 30 20 4b 42 20 66 69 6c   is 5 (10 KB fil
15b31 65 29 2e 20 49 66 20 70 46 72 6f 6d 20 68 61 73  e). If pFrom has
15b32 20 61 20 70 61 67 65 20 73 69 7a 65 20 6f 66 20   a page size of 
15b33 31 30 32 34 20 0a 20 20 2a 2a 20 62 79 74 65 73  1024 .  ** bytes
15b34 20 61 6e 64 20 39 20 70 61 67 65 73 2c 20 74 68   and 9 pages, th
15b35 65 6e 20 74 68 65 20 66 69 6c 65 20 6e 65 65 64  en the file need
15b36 73 20 74 6f 20 62 65 20 74 72 75 6e 63 61 74 65  s to be truncate
15b37 64 20 74 6f 20 39 4b 42 2e 0a 20 20 2a 2f 0a 20  d to 9KB..  */. 
15b38 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
15b39 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 46  OK ){.    if( nF
15b3a 72 6f 6d 50 61 67 65 53 69 7a 65 21 3d 6e 54 6f  romPageSize!=nTo
15b3b 50 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20 20  PageSize ){.    
15b3c 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
15b3d 70 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 50  pFile = sqlite3P
15b3e 61 67 65 72 46 69 6c 65 28 70 42 74 54 6f 2d 3e  agerFile(pBtTo->
15b3f 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 69  pPager);.      i
15b40 36 34 20 69 53 69 7a 65 20 3d 20 28 69 36 34 29  64 iSize = (i64)
15b41 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65 20 2a 20  nFromPageSize * 
15b42 28 69 36 34 29 6e 46 72 6f 6d 50 61 67 65 3b 0a  (i64)nFromPage;.
15b43 20 20 20 20 20 20 69 36 34 20 69 4e 6f 77 20 3d        i64 iNow =
15b44 20 28 69 36 34 29 28 28 6e 54 6f 50 61 67 65 3e   (i64)((nToPage>
15b45 6e 4e 65 77 50 61 67 65 29 3f 6e 54 6f 50 61 67  nNewPage)?nToPag
15b46 65 3a 6e 4e 65 77 50 61 67 65 29 20 2a 20 28 69  e:nNewPage) * (i
15b47 36 34 29 6e 54 6f 50 61 67 65 53 69 7a 65 3b 20  64)nToPageSize; 
15b48 0a 20 20 20 20 20 20 69 36 34 20 69 50 65 6e 64  .      i64 iPend
15b49 69 6e 67 20 3d 20 28 28 69 36 34 29 50 45 4e 44  ing = ((i64)PEND
15b4a 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
15b4b 74 54 6f 29 2d 31 29 20 2a 28 69 36 34 29 6e 54  tTo)-1) *(i64)nT
15b4c 6f 50 61 67 65 53 69 7a 65 3b 0a 20 20 0a 20 20  oPageSize;.  .  
15b4d 20 20 20 20 61 73 73 65 72 74 28 20 69 53 69 7a      assert( iSiz
15b4e 65 3c 3d 69 4e 6f 77 20 29 3b 0a 20 20 0a 20 20  e<=iNow );.  .  
15b4f 20 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 70 68      /* Commit ph
15b50 61 73 65 20 6f 6e 65 20 73 79 6e 63 73 20 74 68  ase one syncs th
15b51 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61  e journal file a
15b52 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70  ssociated with p
15b53 54 6f 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e  To .      ** con
15b54 74 61 69 6e 69 6e 67 20 74 68 65 20 6f 72 69 67  taining the orig
15b55 69 6e 61 6c 20 64 61 74 61 2e 20 49 74 20 64 6f  inal data. It do
15b56 65 73 20 6e 6f 74 20 73 79 6e 63 20 74 68 65 20  es not sync the 
15b57 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20  database file.  
15b58 20 20 20 20 2a 2a 20 69 74 73 65 6c 66 2e 20 41      ** itself. A
15b59 66 74 65 72 20 64 6f 69 6e 67 20 74 68 69 73 20  fter doing this 
15b5a 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 75 73  it is safe to us
15b5b 65 20 4f 73 54 72 75 6e 63 61 74 65 28 29 20 61  e OsTruncate() a
15b5c 6e 64 20 6f 74 68 65 72 0a 20 20 20 20 20 20 2a  nd other.      *
15b5d 2a 20 66 69 6c 65 20 41 50 49 73 20 6f 6e 20 74  * file APIs on t
15b5e 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
15b5f 20 64 69 72 65 63 74 6c 79 2e 0a 20 20 20 20 20   directly..     
15b60 20 2a 2f 0a 20 20 20 20 20 20 70 42 74 54 6f 2d   */.      pBtTo-
15b61 3e 64 62 20 3d 20 70 54 6f 2d 3e 64 62 3b 0a 20  >db = pTo->db;. 
15b62 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
15b63 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73  3PagerCommitPhas
15b64 65 4f 6e 65 28 70 42 74 54 6f 2d 3e 70 50 61 67  eOne(pBtTo->pPag
15b65 65 72 2c 20 30 2c 20 30 2c 20 31 29 3b 0a 20 20  er, 0, 0, 1);.  
15b66 20 20 20 20 69 66 28 20 69 53 69 7a 65 3c 69 4e      if( iSize<iN
15b67 6f 77 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45  ow && rc==SQLITE
15b68 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
15b69 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75  c = sqlite3OsTru
15b6a 6e 63 61 74 65 28 70 46 69 6c 65 2c 20 69 53 69  ncate(pFile, iSi
15b6b 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 0a  ze);.      }.  .
15b6c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f        /* The loo
15b6d 70 20 74 68 61 74 20 63 6f 70 69 65 64 20 64 61  p that copied da
15b6e 74 61 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65  ta from database
15b6f 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f 20 64 69   pFrom to pTo di
15b70 64 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20 70  d not.      ** p
15b71 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f 63 6b  opulate the lock
15b72 69 6e 67 20 70 61 67 65 20 6f 66 20 64 61 74 61  ing page of data
15b73 62 61 73 65 20 70 54 6f 2e 20 49 66 20 74 68 65  base pTo. If the
15b74 20 70 61 67 65 2d 73 69 7a 65 20 6f 66 0a 20 20   page-size of.  
15b75 20 20 20 20 2a 2a 20 70 46 72 6f 6d 20 69 73 20      ** pFrom is 
15b76 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 61  smaller than tha
15b77 74 20 6f 66 20 70 54 6f 2c 20 74 68 69 73 20 6d  t of pTo, this m
15b78 65 61 6e 73 20 73 6f 6d 65 20 64 61 74 61 20 77  eans some data w
15b79 69 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 6e 6f 74  ill.      ** not
15b7a 20 68 61 76 65 20 62 65 65 6e 20 63 6f 70 69 65   have been copie
15b7b 64 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  d. .      **.   
15b7c 20 20 20 2a 2a 20 54 68 69 73 20 62 6c 6f 63 6b     ** This block
15b7d 20 63 6f 70 69 65 73 20 74 68 65 20 6d 69 73 73   copies the miss
15b7e 69 6e 67 20 64 61 74 61 20 66 72 6f 6d 20 64 61  ing data from da
15b7f 74 61 62 61 73 65 20 70 46 72 6f 6d 20 74 6f 20  tabase pFrom to 
15b80 70 54 6f 20 0a 20 20 20 20 20 20 2a 2a 20 75 73  pTo .      ** us
15b81 69 6e 67 20 66 69 6c 65 20 41 50 49 73 2e 20 54  ing file APIs. T
15b82 68 69 73 20 69 73 20 73 61 66 65 20 62 65 63 61  his is safe beca
15b83 75 73 65 20 61 74 20 74 68 69 73 20 70 6f 69 6e  use at this poin
15b84 74 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 0a 20  t we know that. 
15b85 20 20 20 20 20 2a 2a 20 61 6c 6c 20 6f 66 20 74       ** all of t
15b86 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61  he original data
15b87 20 66 72 6f 6d 20 70 54 6f 20 68 61 73 20 62 65   from pTo has be
15b88 65 6e 20 73 79 6e 63 65 64 20 69 6e 74 6f 20 74  en synced into t
15b89 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75  he .      ** jou
15b8a 72 6e 61 6c 20 66 69 6c 65 2e 20 41 74 20 74 68  rnal file. At th
15b8b 69 73 20 70 6f 69 6e 74 20 69 74 20 77 6f 75 6c  is point it woul
15b8c 64 20 62 65 20 73 61 66 65 20 74 6f 20 64 6f 20  d be safe to do 
15b8d 61 6e 79 74 68 69 6e 67 20 61 74 0a 20 20 20 20  anything at.    
15b8e 20 20 2a 2a 20 61 6c 6c 20 74 6f 20 74 68 65 20    ** all to the 
15b8f 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 65 78  database file ex
15b90 63 65 70 74 20 74 72 75 6e 63 61 74 65 20 69 74  cept truncate it
15b91 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73 2e 0a   to zero bytes..
15b92 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
15b93 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
15b94 20 26 26 20 6e 46 72 6f 6d 50 61 67 65 53 69 7a   && nFromPageSiz
15b95 65 3c 6e 54 6f 50 61 67 65 53 69 7a 65 20 26 26  e<nToPageSize &&
15b96 20 69 53 69 7a 65 3e 69 50 65 6e 64 69 6e 67 29   iSize>iPending)
15b97 7b 0a 20 20 20 20 20 20 20 20 69 36 34 20 69 4f  {.        i64 iO
15b98 66 66 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  ff;.        for(
15b99 0a 20 20 20 20 20 20 20 20 20 20 69 4f 66 66 3d  .          iOff=
15b9a 69 50 65 6e 64 69 6e 67 3b 20 0a 20 20 20 20 20  iPending; .     
15b9b 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f       rc==SQLITE_
15b9c 4f 4b 20 26 26 20 69 4f 66 66 3c 28 69 50 65 6e  OK && iOff<(iPen
15b9d 64 69 6e 67 2b 6e 54 6f 50 61 67 65 53 69 7a 65  ding+nToPageSize
15b9e 29 3b 20 0a 20 20 20 20 20 20 20 20 20 20 69 4f  ); .          iO
15b9f 66 66 20 2b 3d 20 6e 46 72 6f 6d 50 61 67 65 53  ff += nFromPageS
15ba0 69 7a 65 0a 20 20 20 20 20 20 20 20 29 7b 0a 20  ize.        ){. 
15ba1 20 20 20 20 20 20 20 20 20 44 62 50 61 67 65 20           DbPage 
15ba2 2a 70 46 72 6f 6d 50 61 67 65 20 3d 20 30 3b 0a  *pFromPage = 0;.
15ba3 20 20 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69            Pgno i
15ba4 46 72 6f 6d 20 3d 20 28 69 4f 66 66 2f 6e 46 72  From = (iOff/nFr
15ba5 6f 6d 50 61 67 65 53 69 7a 65 29 2b 31 3b 0a 20  omPageSize)+1;. 
15ba6 20 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20   .          if( 
15ba7 69 46 72 6f 6d 3d 3d 50 45 4e 44 49 4e 47 5f 42  iFrom==PENDING_B
15ba8 59 54 45 5f 50 41 47 45 28 70 42 74 46 72 6f 6d  YTE_PAGE(pBtFrom
15ba9 29 20 7c 7c 20 69 46 72 6f 6d 3e 6e 46 72 6f 6d  ) || iFrom>nFrom
15baa 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Page ){.        
15bab 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
15bac 20 20 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20          }.  .   
15bad 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
15bae 74 65 33 50 61 67 65 72 47 65 74 28 70 42 74 46  te3PagerGet(pBtF
15baf 72 6f 6d 2d 3e 70 50 61 67 65 72 2c 20 69 46 72  rom->pPager, iFr
15bb0 6f 6d 2c 20 26 70 46 72 6f 6d 50 61 67 65 29 3b  om, &pFromPage);
15bb1 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72  .          if( r
15bb2 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
15bb3 20 20 20 20 20 20 20 20 20 20 20 20 63 68 61 72              char
15bb4 20 2a 7a 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65   *zFrom = sqlite
15bb5 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 46  3PagerGetData(pF
15bb6 72 6f 6d 50 61 67 65 29 3b 0a 20 20 20 20 20 20  romPage);.      
15bb7 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
15bb8 65 33 4f 73 57 72 69 74 65 28 70 46 69 6c 65 2c  e3OsWrite(pFile,
15bb9 20 7a 46 72 6f 6d 2c 20 6e 46 72 6f 6d 50 61 67   zFrom, nFromPag
15bba 65 53 69 7a 65 2c 20 69 4f 66 66 29 3b 0a 20 20  eSize, iOff);.  
15bbb 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
15bbc 33 50 61 67 65 72 55 6e 72 65 66 28 70 46 72 6f  3PagerUnref(pFro
15bbd 6d 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  mPage);.        
15bbe 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
15bbf 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 20 20 2f      }.  .      /
15bc0 2a 20 53 79 6e 63 20 74 68 65 20 64 61 74 61 62  * Sync the datab
15bc1 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20  ase file */.    
15bc2 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
15bc3 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
15bc4 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
15bc5 53 79 6e 63 28 70 42 74 54 6f 2d 3e 70 50 61 67  Sync(pBtTo->pPag
15bc6 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  er);.      }.   
15bc7 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
15bc8 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 54   = sqlite3PagerT
15bc9 72 75 6e 63 61 74 65 28 70 42 74 54 6f 2d 3e 70  runcate(pBtTo->p
15bca 50 61 67 65 72 2c 20 6e 4e 65 77 50 61 67 65 29  Pager, nNewPage)
15bcb 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
15bcc 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
15bcd 0a 20 20 20 20 20 20 70 42 74 54 6f 2d 3e 70 61  .      pBtTo->pa
15bce 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20 30 3b  geSizeFixed = 0;
15bcf 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66  .    }.  }..  if
15bd0 28 20 72 63 20 29 7b 0a 20 20 20 20 73 71 6c 69  ( rc ){.    sqli
15bd1 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b  te3BtreeRollback
15bd2 28 70 54 6f 29 3b 0a 20 20 7d 0a 0a 20 20 72 65  (pTo);.  }..  re
15bd3 74 75 72 6e 20 72 63 3b 20 20 0a 7d 0a 53 51 4c  turn rc;  .}.SQL
15bd4 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
15bd5 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79  sqlite3BtreeCopy
15bd6 46 69 6c 65 28 42 74 72 65 65 20 2a 70 54 6f 2c  File(Btree *pTo,
15bd7 20 42 74 72 65 65 20 2a 70 46 72 6f 6d 29 7b 0a   Btree *pFrom){.
15bd8 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69    int rc;.  sqli
15bd9 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 54  te3BtreeEnter(pT
15bda 6f 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  o);.  sqlite3Btr
15bdb 65 65 45 6e 74 65 72 28 70 46 72 6f 6d 29 3b 0a  eeEnter(pFrom);.
15bdc 20 20 72 63 20 3d 20 62 74 72 65 65 43 6f 70 79    rc = btreeCopy
15bdd 46 69 6c 65 28 70 54 6f 2c 20 70 46 72 6f 6d 29  File(pTo, pFrom)
15bde 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
15bdf 4c 65 61 76 65 28 70 46 72 6f 6d 29 3b 0a 20 20  Leave(pFrom);.  
15be0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
15be1 65 28 70 54 6f 29 3b 0a 20 20 72 65 74 75 72 6e  e(pTo);.  return
15be2 20 72 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f   rc;.}..#endif /
15be3 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41  * SQLITE_OMIT_VA
15be4 43 55 55 4d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52  CUUM */../*.** R
15be5 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69  eturn non-zero i
15be6 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  f a transaction 
15be7 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51  is active..*/.SQ
15be8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
15be9 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49   sqlite3BtreeIsI
15bea 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70 29  nTrans(Btree *p)
15beb 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30  {.  assert( p==0
15bec 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   || sqlite3_mute
15bed 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
15bee 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  tex) );.  return
15bef 20 28 70 20 26 26 20 28 70 2d 3e 69 6e 54 72 61   (p && (p->inTra
15bf0 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 29  ns==TRANS_WRITE)
15bf1 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
15bf2 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61  rn non-zero if a
15bf3 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73   statement trans
15bf4 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65  action is active
15bf5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
15bf6 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
15bf7 74 72 65 65 49 73 49 6e 53 74 6d 74 28 42 74 72  treeIsInStmt(Btr
15bf8 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  ee *p){.  assert
15bf9 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  ( sqlite3BtreeHo
15bfa 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20  ldsMutex(p) );. 
15bfb 20 72 65 74 75 72 6e 20 28 70 2d 3e 70 42 74 20   return (p->pBt 
15bfc 26 26 20 70 2d 3e 70 42 74 2d 3e 69 6e 53 74 6d  && p->pBt->inStm
15bfd 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  t);.}../*.** Ret
15bfe 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20  urn non-zero if 
15bff 61 20 72 65 61 64 20 28 6f 72 20 77 72 69 74 65  a read (or write
15c00 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  ) transaction is
15c01 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49   active..*/.SQLI
15c02 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
15c03 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52  qlite3BtreeIsInR
15c04 65 61 64 54 72 61 6e 73 28 42 74 72 65 65 20 2a  eadTrans(Btree *
15c05 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  p){.  assert( sq
15c06 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
15c07 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  (p->db->mutex) )
15c08 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 20 26 26  ;.  return (p &&
15c09 20 28 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52   (p->inTrans!=TR
15c0a 41 4e 53 5f 4e 4f 4e 45 29 29 3b 0a 7d 0a 0a 2f  ANS_NONE));.}../
15c0b 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
15c0c 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  on returns a poi
15c0d 6e 74 65 72 20 74 6f 20 61 20 62 6c 6f 62 20 6f  nter to a blob o
15c0e 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61  f memory associa
15c0f 74 65 64 20 77 69 74 68 0a 2a 2a 20 61 20 73 69  ted with.** a si
15c10 6e 67 6c 65 20 73 68 61 72 65 64 2d 62 74 72 65  ngle shared-btre
15c11 65 2e 20 54 68 65 20 6d 65 6d 6f 72 79 20 69 73  e. The memory is
15c12 20 75 73 65 64 20 62 79 20 63 6c 69 65 6e 74 20   used by client 
15c13 63 6f 64 65 20 66 6f 72 20 69 74 73 20 6f 77 6e  code for its own
15c14 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 28 66 6f  .** purposes (fo
15c15 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20 73 74  r example, to st
15c16 6f 72 65 20 61 20 68 69 67 68 2d 6c 65 76 65 6c  ore a high-level
15c17 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74   schema associat
15c18 65 64 20 77 69 74 68 20 0a 2a 2a 20 74 68 65 20  ed with .** the 
15c19 73 68 61 72 65 64 2d 62 74 72 65 65 29 2e 20 54  shared-btree). T
15c1a 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 6d  he btree layer m
15c1b 61 6e 61 67 65 73 20 72 65 66 65 72 65 6e 63 65  anages reference
15c1c 20 63 6f 75 6e 74 69 6e 67 20 69 73 73 75 65 73   counting issues
15c1d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  ..**.** The firs
15c1e 74 20 74 69 6d 65 20 74 68 69 73 20 69 73 20 63  t time this is c
15c1f 61 6c 6c 65 64 20 6f 6e 20 61 20 73 68 61 72 65  alled on a share
15c20 64 2d 62 74 72 65 65 2c 20 6e 42 79 74 65 73 20  d-btree, nBytes 
15c21 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a  bytes of memory.
15c22 2a 2a 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64  ** are allocated
15c23 2c 20 7a 65 72 6f 65 64 2c 20 61 6e 64 20 72 65  , zeroed, and re
15c24 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61  turned to the ca
15c25 6c 6c 65 72 2e 20 46 6f 72 20 65 61 63 68 20 73  ller. For each s
15c26 75 62 73 65 71 75 65 6e 74 20 0a 2a 2a 20 63 61  ubsequent .** ca
15c27 6c 6c 20 74 68 65 20 6e 42 79 74 65 73 20 70 61  ll the nBytes pa
15c28 72 61 6d 65 74 65 72 20 69 73 20 69 67 6e 6f 72  rameter is ignor
15c29 65 64 20 61 6e 64 20 61 20 70 6f 69 6e 74 65 72  ed and a pointer
15c2a 20 74 6f 20 74 68 65 20 73 61 6d 65 20 62 6c 6f   to the same blo
15c2b 62 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79 20 72  b.** of memory r
15c2c 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 0a 2a 2a 20  eturned. .**.** 
15c2d 49 66 20 74 68 65 20 6e 42 79 74 65 73 20 70 61  If the nBytes pa
15c2e 72 61 6d 65 74 65 72 20 69 73 20 30 20 61 6e 64  rameter is 0 and
15c2f 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 6d   the blob of mem
15c30 6f 72 79 20 68 61 73 20 6e 6f 74 20 79 65 74 20  ory has not yet 
15c31 62 65 65 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65  been.** allocate
15c32 64 2c 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65  d, a null pointe
15c33 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49  r is returned. I
15c34 66 20 74 68 65 20 62 6c 6f 62 20 68 61 73 20 61  f the blob has a
15c35 6c 72 65 61 64 79 20 62 65 65 6e 0a 2a 2a 20 61  lready been.** a
15c36 6c 6c 6f 63 61 74 65 64 2c 20 69 74 20 69 73 20  llocated, it is 
15c37 72 65 74 75 72 6e 65 64 20 61 73 20 6e 6f 72 6d  returned as norm
15c38 61 6c 2e 0a 2a 2a 0a 2a 2a 20 4a 75 73 74 20 62  al..**.** Just b
15c39 65 66 6f 72 65 20 74 68 65 20 73 68 61 72 65 64  efore the shared
15c3a 2d 62 74 72 65 65 20 69 73 20 63 6c 6f 73 65 64  -btree is closed
15c3b 2c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 70  , the function p
15c3c 61 73 73 65 64 20 61 73 20 74 68 65 20 0a 2a 2a  assed as the .**
15c3d 20 78 46 72 65 65 20 61 72 67 75 6d 65 6e 74 20   xFree argument 
15c3e 77 68 65 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20  when the memory 
15c3f 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 20 6d  allocation was m
15c40 61 64 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f  ade is invoked o
15c41 6e 20 74 68 65 20 0a 2a 2a 20 62 6c 6f 62 20 6f  n the .** blob o
15c42 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f  f allocated memo
15c43 72 79 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ry. This functio
15c44 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 20 63 61 6c  n should not cal
15c45 6c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  l sqlite3_free()
15c46 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 65 6d 6f 72  .** on the memor
15c47 79 2c 20 74 68 65 20 62 74 72 65 65 20 6c 61 79  y, the btree lay
15c48 65 72 20 64 6f 65 73 20 74 68 61 74 2e 0a 2a 2f  er does that..*/
15c49 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
15c4a 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72  void *sqlite3Btr
15c4b 65 65 53 63 68 65 6d 61 28 42 74 72 65 65 20 2a  eeSchema(Btree *
15c4c 70 2c 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 76  p, int nBytes, v
15c4d 6f 69 64 28 2a 78 46 72 65 65 29 28 76 6f 69 64  oid(*xFree)(void
15c4e 20 2a 29 29 7b 0a 20 20 42 74 53 68 61 72 65 64   *)){.  BtShared
15c4f 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
15c50 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
15c51 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20 21 70  ter(p);.  if( !p
15c52 42 74 2d 3e 70 53 63 68 65 6d 61 20 26 26 20 6e  Bt->pSchema && n
15c53 42 79 74 65 73 20 29 7b 0a 20 20 20 20 70 42 74  Bytes ){.    pBt
15c54 2d 3e 70 53 63 68 65 6d 61 20 3d 20 73 71 6c 69  ->pSchema = sqli
15c55 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42  te3MallocZero(nB
15c56 79 74 65 73 29 3b 0a 20 20 20 20 70 42 74 2d 3e  ytes);.    pBt->
15c57 78 46 72 65 65 53 63 68 65 6d 61 20 3d 20 78 46  xFreeSchema = xF
15c58 72 65 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ree;.  }.  sqlit
15c59 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
15c5a 0a 20 20 72 65 74 75 72 6e 20 70 42 74 2d 3e 70  .  return pBt->p
15c5b 53 63 68 65 6d 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Schema;.}../*.**
15c5c 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
15c5d 61 6e 6f 74 68 65 72 20 75 73 65 72 20 6f 66 20  another user of 
15c5e 74 68 65 20 73 61 6d 65 20 73 68 61 72 65 64 20  the same shared 
15c5f 62 74 72 65 65 20 61 73 20 74 68 65 20 61 72 67  btree as the arg
15c60 75 6d 65 6e 74 0a 2a 2a 20 68 61 6e 64 6c 65 20  ument.** handle 
15c61 68 6f 6c 64 73 20 61 6e 20 65 78 63 6c 75 73 69  holds an exclusi
15c62 76 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73  ve lock on the s
15c63 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
15c64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
15c65 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
15c66 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f 63 6b  3BtreeSchemaLock
15c67 65 64 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20  ed(Btree *p){.  
15c68 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74  int rc;.  assert
15c69 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
15c6a 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  held(p->db->mute
15c6b 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  x) );.  sqlite3B
15c6c 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
15c6d 72 63 20 3d 20 28 71 75 65 72 79 54 61 62 6c 65  rc = (queryTable
15c6e 4c 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52  Lock(p, MASTER_R
15c6f 4f 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 21  OOT, READ_LOCK)!
15c70 3d 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 20 20 73  =SQLITE_OK);.  s
15c71 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
15c72 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
15c73 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51  ;.}...#ifndef SQ
15c74 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
15c75 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 4f 62 74  _CACHE./*.** Obt
15c76 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68  ain a lock on th
15c77 65 20 74 61 62 6c 65 20 77 68 6f 73 65 20 72 6f  e table whose ro
15c78 6f 74 20 70 61 67 65 20 69 73 20 69 54 61 62 2e  ot page is iTab.
15c79 20 20 54 68 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73    The.** lock is
15c7a 20 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66   a write lock if
15c7b 20 69 73 57 72 69 74 65 6c 6f 63 6b 20 69 73 20   isWritelock is 
15c7c 74 72 75 65 20 6f 72 20 61 20 72 65 61 64 20 6c  true or a read l
15c7d 6f 63 6b 0a 2a 2a 20 69 66 20 69 74 20 69 73 20  ock.** if it is 
15c7e 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  false..*/.SQLITE
15c7f 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
15c80 69 74 65 33 42 74 72 65 65 4c 6f 63 6b 54 61 62  ite3BtreeLockTab
15c81 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74  le(Btree *p, int
15c82 20 69 54 61 62 2c 20 75 38 20 69 73 57 72 69 74   iTab, u8 isWrit
15c83 65 4c 6f 63 6b 29 7b 0a 20 20 69 6e 74 20 72 63  eLock){.  int rc
15c84 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
15c85 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20  if( p->sharable 
15c86 29 7b 0a 20 20 20 20 75 38 20 6c 6f 63 6b 54 79  ){.    u8 lockTy
15c87 70 65 20 3d 20 52 45 41 44 5f 4c 4f 43 4b 20 2b  pe = READ_LOCK +
15c88 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b 0a 20 20   isWriteLock;.  
15c89 20 20 61 73 73 65 72 74 28 20 52 45 41 44 5f 4c    assert( READ_L
15c8a 4f 43 4b 2b 31 3d 3d 57 52 49 54 45 5f 4c 4f 43  OCK+1==WRITE_LOC
15c8b 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  K );.    assert(
15c8c 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 30 20   isWriteLock==0 
15c8d 7c 7c 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d  || isWriteLock==
15c8e 31 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  1 );.    sqlite3
15c8f 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
15c90 20 20 20 72 63 20 3d 20 71 75 65 72 79 54 61 62     rc = queryTab
15c91 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 2c 20  leLock(p, iTab, 
15c92 6c 6f 63 6b 54 79 70 65 29 3b 0a 20 20 20 20 69  lockType);.    i
15c93 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
15c94 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c   ){.      rc = l
15c95 6f 63 6b 54 61 62 6c 65 28 70 2c 20 69 54 61 62  ockTable(p, iTab
15c96 2c 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 20 20 20  , lockType);.   
15c97 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74   }.    sqlite3Bt
15c98 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d  reeLeave(p);.  }
15c99 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
15c9a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20  #endif..#ifndef 
15c9b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52  SQLITE_OMIT_INCR
15c9c 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d  BLOB./*.** Argum
15c9d 65 6e 74 20 70 43 73 72 20 6d 75 73 74 20 62 65  ent pCsr must be
15c9e 20 61 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64   a cursor opened
15c9f 20 66 6f 72 20 77 72 69 74 69 6e 67 20 6f 6e 20   for writing on 
15ca0 61 6e 20 0a 2a 2a 20 49 4e 54 4b 45 59 20 74 61  an .** INTKEY ta
15ca1 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 70 6f  ble currently po
15ca2 69 6e 74 69 6e 67 20 61 74 20 61 20 76 61 6c 69  inting at a vali
15ca3 64 20 74 61 62 6c 65 20 65 6e 74 72 79 2e 20 0a  d table entry. .
15ca4 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
15ca5 20 6d 6f 64 69 66 69 65 73 20 74 68 65 20 64 61   modifies the da
15ca6 74 61 20 73 74 6f 72 65 64 20 61 73 20 70 61 72  ta stored as par
15ca7 74 20 6f 66 20 74 68 61 74 20 65 6e 74 72 79 2e  t of that entry.
15ca8 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 64 61 74  .** Only the dat
15ca9 61 20 63 6f 6e 74 65 6e 74 20 6d 61 79 20 6f 6e  a content may on
15caa 6c 79 20 62 65 20 6d 6f 64 69 66 69 65 64 2c 20  ly be modified, 
15cab 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62  it is not possib
15cac 6c 65 0a 2a 2a 20 74 6f 20 63 68 61 6e 67 65 20  le.** to change 
15cad 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68  the length of th
15cae 65 20 64 61 74 61 20 73 74 6f 72 65 64 2e 0a 2a  e data stored..*
15caf 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
15cb0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
15cb1 65 50 75 74 44 61 74 61 28 42 74 43 75 72 73 6f  ePutData(BtCurso
15cb2 72 20 2a 70 43 73 72 2c 20 75 33 32 20 6f 66 66  r *pCsr, u32 off
15cb3 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f  set, u32 amt, vo
15cb4 69 64 20 2a 7a 29 7b 0a 20 20 61 73 73 65 72 74  id *z){.  assert
15cb5 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
15cb6 65 78 28 70 43 73 72 29 20 29 3b 0a 20 20 61 73  ex(pCsr) );.  as
15cb7 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
15cb8 74 65 78 5f 68 65 6c 64 28 70 43 73 72 2d 3e 70  tex_held(pCsr->p
15cb9 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78  Btree->db->mutex
15cba 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 70 43  ) );.  assert(pC
15cbb 73 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61  sr->isIncrblobHa
15cbc 6e 64 6c 65 29 3b 0a 0a 20 20 72 65 73 74 6f 72  ndle);..  restor
15cbd 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  eCursorPosition(
15cbe 70 43 73 72 29 3b 0a 20 20 61 73 73 65 72 74 28  pCsr);.  assert(
15cbf 20 70 43 73 72 2d 3e 65 53 74 61 74 65 21 3d 43   pCsr->eState!=C
15cc0 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45  URSOR_REQUIRESEE
15cc1 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 73 72 2d  K );.  if( pCsr-
15cc2 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f  >eState!=CURSOR_
15cc3 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 74  VALID ){.    ret
15cc4 75 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 54  urn SQLITE_ABORT
15cc5 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63  ;.  }..  /* Chec
15cc6 6b 20 73 6f 6d 65 20 70 72 65 63 6f 6e 64 69 74  k some precondit
15cc7 69 6f 6e 73 3a 20 0a 20 20 2a 2a 20 20 20 28 61  ions: .  **   (a
15cc8 29 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  ) the cursor is 
15cc9 6f 70 65 6e 20 66 6f 72 20 77 72 69 74 69 6e 67  open for writing
15cca 2c 0a 20 20 2a 2a 20 20 20 28 62 29 20 74 68 65  ,.  **   (b) the
15ccb 72 65 20 69 73 20 6e 6f 20 72 65 61 64 2d 6c 6f  re is no read-lo
15ccc 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20  ck on the table 
15ccd 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 20 61  being modified a
15cce 6e 64 0a 20 20 2a 2a 20 20 20 28 63 29 20 74 68  nd.  **   (c) th
15ccf 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 20  e cursor points 
15cd0 61 74 20 61 20 76 61 6c 69 64 20 72 6f 77 20 6f  at a valid row o
15cd1 66 20 61 6e 20 69 6e 74 4b 65 79 20 74 61 62 6c  f an intKey tabl
15cd2 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70  e..  */.  if( !p
15cd3 43 73 72 2d 3e 77 72 46 6c 61 67 20 29 7b 0a 20  Csr->wrFlag ){. 
15cd4 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
15cd5 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20  _READONLY;.  }. 
15cd6 20 61 73 73 65 72 74 28 20 21 70 43 73 72 2d 3e   assert( !pCsr->
15cd7 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 0a 20  pBt->readOnly . 
15cd8 20 20 20 20 20 20 20 20 20 26 26 20 70 43 73 72           && pCsr
15cd9 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63  ->pBt->inTransac
15cda 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54  tion==TRANS_WRIT
15cdb 45 20 29 3b 0a 20 20 69 66 28 20 63 68 65 63 6b  E );.  if( check
15cdc 52 65 61 64 4c 6f 63 6b 73 28 70 43 73 72 2d 3e  ReadLocks(pCsr->
15cdd 70 42 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 67  pBtree, pCsr->pg
15cde 6e 6f 52 6f 6f 74 2c 20 70 43 73 72 2c 20 30 29  noRoot, pCsr, 0)
15cdf 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
15ce0 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 20 2f 2a  QLITE_LOCKED; /*
15ce1 20 54 68 65 20 74 61 62 6c 65 20 70 43 75 72 20   The table pCur 
15ce2 70 6f 69 6e 74 73 20 74 6f 20 68 61 73 20 61 20  points to has a 
15ce3 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 7d  read lock */.  }
15ce4 0a 20 20 69 66 28 20 70 43 73 72 2d 3e 65 53 74  .  if( pCsr->eSt
15ce5 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41  ate==CURSOR_INVA
15ce6 4c 49 44 20 7c 7c 20 21 70 43 73 72 2d 3e 61 70  LID || !pCsr->ap
15ce7 50 61 67 65 5b 70 43 73 72 2d 3e 69 50 61 67 65  Page[pCsr->iPage
15ce8 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20  ]->intKey ){.   
15ce9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
15cea 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  RROR;.  }..  ret
15ceb 75 72 6e 20 61 63 63 65 73 73 50 61 79 6c 6f 61  urn accessPayloa
15cec 64 28 70 43 73 72 2c 20 6f 66 66 73 65 74 2c 20  d(pCsr, offset, 
15ced 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64 20 63  amt, (unsigned c
15cee 68 61 72 20 2a 29 7a 2c 20 30 2c 20 31 29 3b 0a  har *)z, 0, 1);.
15cef 7d 0a 0a 2f 2a 20 0a 2a 2a 20 53 65 74 20 61 20  }../* .** Set a 
15cf0 66 6c 61 67 20 6f 6e 20 74 68 69 73 20 63 75 72  flag on this cur
15cf1 73 6f 72 20 74 6f 20 63 61 63 68 65 20 74 68 65  sor to cache the
15cf2 20 6c 6f 63 61 74 69 6f 6e 73 20 6f 66 20 70 61   locations of pa
15cf3 67 65 73 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a  ges from the .**
15cf4 20 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 66   overflow list f
15cf5 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  or the current r
15cf6 6f 77 2e 20 54 68 69 73 20 69 73 20 75 73 65 64  ow. This is used
15cf7 20 62 79 20 63 75 72 73 6f 72 73 20 6f 70 65 6e   by cursors open
15cf8 65 64 0a 2a 2a 20 66 6f 72 20 69 6e 63 72 65 6d  ed.** for increm
15cf9 65 6e 74 61 6c 20 62 6c 6f 62 20 49 4f 20 6f 6e  ental blob IO on
15cfa 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ly..**.** This f
15cfb 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 61 20 66  unction sets a f
15cfc 6c 61 67 20 6f 6e 6c 79 2e 20 54 68 65 20 61 63  lag only. The ac
15cfd 74 75 61 6c 20 70 61 67 65 20 6c 6f 63 61 74 69  tual page locati
15cfe 6f 6e 20 63 61 63 68 65 0a 2a 2a 20 28 73 74 6f  on cache.** (sto
15cff 72 65 64 20 69 6e 20 42 74 43 75 72 73 6f 72 2e  red in BtCursor.
15d00 61 4f 76 65 72 66 6c 6f 77 5b 5d 29 20 69 73 20  aOverflow[]) is 
15d01 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 75 73  allocated and us
15d02 65 64 20 62 79 20 66 75 6e 63 74 69 6f 6e 0a 2a  ed by function.*
15d03 2a 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28  * accessPayload(
15d04 29 20 28 74 68 65 20 77 6f 72 6b 65 72 20 66 75  ) (the worker fu
15d05 6e 63 74 69 6f 6e 20 66 6f 72 20 73 71 6c 69 74  nction for sqlit
15d06 65 33 42 74 72 65 65 44 61 74 61 28 29 20 61 6e  e3BtreeData() an
15d07 64 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65  d.** sqlite3Btre
15d08 65 50 75 74 44 61 74 61 28 29 29 2e 0a 2a 2f 0a  ePutData())..*/.
15d09 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
15d0a 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
15d0b 43 61 63 68 65 4f 76 65 72 66 6c 6f 77 28 42 74  CacheOverflow(Bt
15d0c 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
15d0d 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
15d0e 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
15d0f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
15d10 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
15d11 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62  pCur->pBtree->db
15d12 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
15d13 73 65 72 74 28 21 70 43 75 72 2d 3e 69 73 49 6e  sert(!pCur->isIn
15d14 63 72 62 6c 6f 62 48 61 6e 64 6c 65 29 3b 0a 20  crblobHandle);. 
15d15 20 61 73 73 65 72 74 28 21 70 43 75 72 2d 3e 61   assert(!pCur->a
15d16 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43 75  Overflow);.  pCu
15d17 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e  r->isIncrblobHan
15d18 64 6c 65 20 3d 20 31 3b 0a 7d 0a 23 65 6e 64 69  dle = 1;.}.#endi
15d19 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f../************
15d1a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65 2e  ** End of btree.
15d1b 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
15d1c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15d1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15d1e 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
15d1f 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64  ** Begin file vd
15d20 62 65 66 69 66 6f 2e 63 20 2a 2a 2a 2a 2a 2a 2a  befifo.c *******
15d21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15d22 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15d23 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4a 75  */./*.** 2005 Ju
15d24 6e 65 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ne 16.**.** The 
15d25 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
15d26 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
15d27 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
15d28 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
15d29 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
15d2a 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
15d2b 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
15d2c 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
15d2d 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
15d2e 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
15d2f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
15d30 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
15d31 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
15d32 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
15d33 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
15d34 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
15d35 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
15d36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15d37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15d38 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15d39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15d3a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
15d3b 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 46  e implements a F
15d3c 49 46 4f 20 71 75 65 75 65 20 6f 66 20 72 6f 77  IFO queue of row
15d3d 69 64 73 20 75 73 65 64 20 66 6f 72 20 70 72 6f  ids used for pro
15d3e 63 65 73 73 69 6e 67 0a 2a 2a 20 55 50 44 41 54  cessing.** UPDAT
15d3f 45 20 61 6e 64 20 44 45 4c 45 54 45 20 73 74 61  E and DELETE sta
15d40 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 24  tements..**.** $
15d41 49 64 3a 20 76 64 62 65 66 69 66 6f 2e 63 2c 76  Id: vdbefifo.c,v
15d42 20 31 2e 38 20 32 30 30 38 2f 30 37 2f 32 38 20   1.8 2008/07/28 
15d43 31 39 3a 33 34 3a 35 34 20 64 72 68 20 45 78 70  19:34:54 drh Exp
15d44 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e   $.*/../*.** Con
15d45 73 74 61 6e 74 73 20 46 49 46 4f 53 49 5a 45 5f  stants FIFOSIZE_
15d46 46 49 52 53 54 20 61 6e 64 20 46 49 46 4f 53 49  FIRST and FIFOSI
15d47 5a 45 5f 4d 41 58 20 61 72 65 20 74 68 65 20 69  ZE_MAX are the i
15d48 6e 69 74 69 61 6c 0a 2a 2a 20 6e 75 6d 62 65 72  nitial.** number
15d49 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
15d4a 20 66 69 66 6f 20 70 61 67 65 20 61 6e 64 20 74   fifo page and t
15d4b 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
15d4c 72 20 6f 66 0a 2a 2a 20 65 6e 74 72 69 65 73 20  r of.** entries 
15d4d 69 6e 20 61 20 66 69 66 6f 20 70 61 67 65 2e 0a  in a fifo page..
15d4e 2a 2f 0a 23 64 65 66 69 6e 65 20 46 49 46 4f 53  */.#define FIFOS
15d4f 49 5a 45 5f 46 49 52 53 54 20 28 28 28 31 32 38  IZE_FIRST (((128
15d50 2d 73 69 7a 65 6f 66 28 46 69 66 6f 50 61 67 65  -sizeof(FifoPage
15d51 29 29 2f 38 29 2b 31 29 0a 23 69 66 64 65 66 20  ))/8)+1).#ifdef 
15d52 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f  SQLITE_MALLOC_SO
15d53 46 54 5f 4c 49 4d 49 54 0a 23 20 64 65 66 69 6e  FT_LIMIT.# defin
15d54 65 20 46 49 46 4f 53 49 5a 45 5f 4d 41 58 20 20  e FIFOSIZE_MAX  
15d55 20 28 28 28 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f   (((SQLITE_MALLO
15d56 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 2d 73 69 7a  C_SOFT_LIMIT-siz
15d57 65 6f 66 28 46 69 66 6f 50 61 67 65 29 29 2f 38  eof(FifoPage))/8
15d58 29 2b 31 29 0a 23 65 6c 73 65 0a 23 20 64 65 66  )+1).#else.# def
15d59 69 6e 65 20 46 49 46 4f 53 49 5a 45 5f 4d 41 58  ine FIFOSIZE_MAX
15d5a 20 20 20 28 28 28 32 36 32 31 34 34 2d 73 69 7a     (((262144-siz
15d5b 65 6f 66 28 46 69 66 6f 50 61 67 65 29 29 2f 38  eof(FifoPage))/8
15d5c 29 2b 31 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  )+1).#endif../*.
15d5d 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65  ** Allocate a ne
15d5e 77 20 46 69 66 6f 50 61 67 65 20 61 6e 64 20 72  w FifoPage and r
15d5f 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
15d60 74 6f 20 69 74 2e 20 20 52 65 74 75 72 6e 20 4e  to it.  Return N
15d61 55 4c 4c 20 69 66 0a 2a 2a 20 77 65 20 72 75 6e  ULL if.** we run
15d62 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20   out of memory. 
15d63 20 4c 65 61 76 65 20 73 70 61 63 65 20 6f 6e 20   Leave space on 
15d64 74 68 65 20 70 61 67 65 20 66 6f 72 20 6e 45 6e  the page for nEn
15d65 74 72 79 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a  try entries..*/.
15d66 73 74 61 74 69 63 20 46 69 66 6f 50 61 67 65 20  static FifoPage 
15d67 2a 61 6c 6c 6f 63 61 74 65 46 69 66 6f 50 61 67  *allocateFifoPag
15d68 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  e(sqlite3 *db, i
15d69 6e 74 20 6e 45 6e 74 72 79 29 7b 0a 20 20 46 69  nt nEntry){.  Fi
15d6a 66 6f 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20  foPage *pPage;. 
15d6b 20 69 66 28 20 6e 45 6e 74 72 79 3e 46 49 46 4f   if( nEntry>FIFO
15d6c 53 49 5a 45 5f 4d 41 58 20 29 7b 0a 20 20 20 20  SIZE_MAX ){.    
15d6d 6e 45 6e 74 72 79 20 3d 20 46 49 46 4f 53 49 5a  nEntry = FIFOSIZ
15d6e 45 5f 4d 41 58 3b 0a 20 20 7d 0a 20 20 70 50 61  E_MAX;.  }.  pPa
15d6f 67 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ge = sqlite3DbMa
15d70 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65  llocRaw(db, size
15d71 6f 66 28 46 69 66 6f 50 61 67 65 29 20 2b 20 73  of(FifoPage) + s
15d72 69 7a 65 6f 66 28 69 36 34 29 2a 28 6e 45 6e 74  izeof(i64)*(nEnt
15d73 72 79 2d 31 29 20 29 3b 0a 20 20 69 66 28 20 70  ry-1) );.  if( p
15d74 50 61 67 65 20 29 7b 0a 20 20 20 20 70 50 61 67  Page ){.    pPag
15d75 65 2d 3e 6e 53 6c 6f 74 20 3d 20 6e 45 6e 74 72  e->nSlot = nEntr
15d76 79 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 57  y;.    pPage->iW
15d77 72 69 74 65 20 3d 20 30 3b 0a 20 20 20 20 70 50  rite = 0;.    pP
15d78 61 67 65 2d 3e 69 52 65 61 64 20 3d 20 30 3b 0a  age->iRead = 0;.
15d79 20 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 74      pPage->pNext
15d7a 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 0;.  }.  retu
15d7b 72 6e 20 70 50 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a  rn pPage;.}../*.
15d7c 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 20  ** Initialize a 
15d7d 46 69 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a  Fifo structure..
15d7e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
15d7f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
15d80 62 65 46 69 66 6f 49 6e 69 74 28 46 69 66 6f 20  beFifoInit(Fifo 
15d81 2a 70 46 69 66 6f 2c 20 73 71 6c 69 74 65 33 20  *pFifo, sqlite3 
15d82 2a 64 62 29 7b 0a 20 20 6d 65 6d 73 65 74 28 70  *db){.  memset(p
15d83 46 69 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Fifo, 0, sizeof(
15d84 2a 70 46 69 66 6f 29 29 3b 0a 20 20 70 46 69 66  *pFifo));.  pFif
15d85 6f 2d 3e 64 62 20 3d 20 64 62 3b 0a 7d 0a 0a 2f  o->db = db;.}../
15d86 2a 0a 2a 2a 20 50 75 73 68 20 61 20 73 69 6e 67  *.** Push a sing
15d87 6c 65 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65  le 64-bit intege
15d88 72 20 76 61 6c 75 65 20 69 6e 74 6f 20 74 68 65  r value into the
15d89 20 46 69 66 6f 2e 20 20 52 65 74 75 72 6e 20 53   Fifo.  Return S
15d8a 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6e 6f 72 6d  QLITE_OK.** norm
15d8b 61 6c 6c 79 2e 20 20 20 53 51 4c 49 54 45 5f 4e  ally.   SQLITE_N
15d8c 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64  OMEM is returned
15d8d 20 69 66 20 77 65 20 61 72 65 20 75 6e 61 62 6c   if we are unabl
15d8e 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 0a 2a 2a  e to allocate.**
15d8f 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49   memory..*/.SQLI
15d90 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
15d91 71 6c 69 74 65 33 56 64 62 65 46 69 66 6f 50 75  qlite3VdbeFifoPu
15d92 73 68 28 46 69 66 6f 20 2a 70 46 69 66 6f 2c 20  sh(Fifo *pFifo, 
15d93 69 36 34 20 76 61 6c 29 7b 0a 20 20 46 69 66 6f  i64 val){.  Fifo
15d94 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 70  Page *pPage;.  p
15d95 50 61 67 65 20 3d 20 70 46 69 66 6f 2d 3e 70 4c  Page = pFifo->pL
15d96 61 73 74 3b 0a 20 20 69 66 28 20 70 50 61 67 65  ast;.  if( pPage
15d97 3d 3d 30 20 29 7b 0a 20 20 20 20 70 50 61 67 65  ==0 ){.    pPage
15d98 20 3d 20 70 46 69 66 6f 2d 3e 70 4c 61 73 74 20   = pFifo->pLast 
15d99 3d 20 70 46 69 66 6f 2d 3e 70 46 69 72 73 74 20  = pFifo->pFirst 
15d9a 3d 0a 20 20 20 20 20 20 20 20 20 61 6c 6c 6f 63  =.         alloc
15d9b 61 74 65 46 69 66 6f 50 61 67 65 28 70 46 69 66  ateFifoPage(pFif
15d9c 6f 2d 3e 64 62 2c 20 46 49 46 4f 53 49 5a 45 5f  o->db, FIFOSIZE_
15d9d 46 49 52 53 54 29 3b 0a 20 20 20 20 69 66 28 20  FIRST);.    if( 
15d9e 70 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20  pPage==0 ){.    
15d9f 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
15da0 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d  NOMEM;.    }.  }
15da1 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 2d 3e  else if( pPage->
15da2 69 57 72 69 74 65 3e 3d 70 50 61 67 65 2d 3e 6e  iWrite>=pPage->n
15da3 53 6c 6f 74 20 29 7b 0a 20 20 20 20 70 50 61 67  Slot ){.    pPag
15da4 65 2d 3e 70 4e 65 78 74 20 3d 20 61 6c 6c 6f 63  e->pNext = alloc
15da5 61 74 65 46 69 66 6f 50 61 67 65 28 70 46 69 66  ateFifoPage(pFif
15da6 6f 2d 3e 64 62 2c 20 70 46 69 66 6f 2d 3e 6e 45  o->db, pFifo->nE
15da7 6e 74 72 79 29 3b 0a 20 20 20 20 69 66 28 20 70  ntry);.    if( p
15da8 50 61 67 65 2d 3e 70 4e 65 78 74 3d 3d 30 20 29  Page->pNext==0 )
15da9 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
15daa 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
15dab 20 7d 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70   }.    pPage = p
15dac 46 69 66 6f 2d 3e 70 4c 61 73 74 20 3d 20 70 50  Fifo->pLast = pP
15dad 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a  age->pNext;.  }.
15dae 20 20 70 50 61 67 65 2d 3e 61 53 6c 6f 74 5b 70    pPage->aSlot[p
15daf 50 61 67 65 2d 3e 69 57 72 69 74 65 2b 2b 5d 20  Page->iWrite++] 
15db0 3d 20 76 61 6c 3b 0a 20 20 70 46 69 66 6f 2d 3e  = val;.  pFifo->
15db1 6e 45 6e 74 72 79 2b 2b 3b 0a 20 20 72 65 74 75  nEntry++;.  retu
15db2 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
15db3 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 61  ./*.** Extract a
15db4 20 73 69 6e 67 6c 65 20 36 34 2d 62 69 74 20 69   single 64-bit i
15db5 6e 74 65 67 65 72 20 76 61 6c 75 65 20 66 72 6f  nteger value fro
15db6 6d 20 74 68 65 20 46 69 66 6f 2e 20 20 54 68 65  m the Fifo.  The
15db7 20 69 6e 74 65 67 65 72 0a 2a 2a 20 65 78 74 72   integer.** extr
15db8 61 63 74 65 64 20 69 73 20 74 68 65 20 6f 6e 65  acted is the one
15db9 20 6c 65 61 73 74 20 72 65 63 65 6e 74 6c 79 20   least recently 
15dba 69 6e 73 65 72 74 65 64 2e 20 20 49 66 20 74 68  inserted.  If th
15dbb 65 20 46 69 66 6f 20 69 73 20 65 6d 70 74 79 0a  e Fifo is empty.
15dbc 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ** return SQLITE
15dbd 5f 44 4f 4e 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45  _DONE..*/.SQLITE
15dbe 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
15dbf 69 74 65 33 56 64 62 65 46 69 66 6f 50 6f 70 28  ite3VdbeFifoPop(
15dc0 46 69 66 6f 20 2a 70 46 69 66 6f 2c 20 69 36 34  Fifo *pFifo, i64
15dc1 20 2a 70 56 61 6c 29 7b 0a 20 20 46 69 66 6f 50   *pVal){.  FifoP
15dc2 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 69 66  age *pPage;.  if
15dc3 28 20 70 46 69 66 6f 2d 3e 6e 45 6e 74 72 79 3d  ( pFifo->nEntry=
15dc4 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
15dc5 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20   SQLITE_DONE;.  
15dc6 7d 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 66  }.  assert( pFif
15dc7 6f 2d 3e 6e 45 6e 74 72 79 3e 30 20 29 3b 0a 20  o->nEntry>0 );. 
15dc8 20 70 50 61 67 65 20 3d 20 70 46 69 66 6f 2d 3e   pPage = pFifo->
15dc9 70 46 69 72 73 74 3b 0a 20 20 61 73 73 65 72 74  pFirst;.  assert
15dca 28 20 70 50 61 67 65 21 3d 30 20 29 3b 0a 20 20  ( pPage!=0 );.  
15dcb 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69  assert( pPage->i
15dcc 57 72 69 74 65 3e 70 50 61 67 65 2d 3e 69 52 65  Write>pPage->iRe
15dcd 61 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ad );.  assert( 
15dce 70 50 61 67 65 2d 3e 69 57 72 69 74 65 3c 3d 70  pPage->iWrite<=p
15dcf 50 61 67 65 2d 3e 6e 53 6c 6f 74 20 29 3b 0a 20  Page->nSlot );. 
15dd0 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
15dd1 69 52 65 61 64 3c 70 50 61 67 65 2d 3e 6e 53 6c  iRead<pPage->nSl
15dd2 6f 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ot );.  assert( 
15dd3 70 50 61 67 65 2d 3e 69 52 65 61 64 3e 3d 30 20  pPage->iRead>=0 
15dd4 29 3b 0a 20 20 2a 70 56 61 6c 20 3d 20 70 50 61  );.  *pVal = pPa
15dd5 67 65 2d 3e 61 53 6c 6f 74 5b 70 50 61 67 65 2d  ge->aSlot[pPage-
15dd6 3e 69 52 65 61 64 2b 2b 5d 3b 0a 20 20 70 46 69  >iRead++];.  pFi
15dd7 66 6f 2d 3e 6e 45 6e 74 72 79 2d 2d 3b 0a 20 20  fo->nEntry--;.  
15dd8 69 66 28 20 70 50 61 67 65 2d 3e 69 52 65 61 64  if( pPage->iRead
15dd9 3e 3d 70 50 61 67 65 2d 3e 69 57 72 69 74 65 20  >=pPage->iWrite 
15dda 29 7b 0a 20 20 20 20 70 46 69 66 6f 2d 3e 70 46  ){.    pFifo->pF
15ddb 69 72 73 74 20 3d 20 70 50 61 67 65 2d 3e 70 4e  irst = pPage->pN
15ddc 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ext;.    sqlite3
15ddd 44 62 46 72 65 65 28 70 46 69 66 6f 2d 3e 64 62  DbFree(pFifo->db
15dde 2c 20 70 50 61 67 65 29 3b 0a 20 20 20 20 69 66  , pPage);.    if
15ddf 28 20 70 46 69 66 6f 2d 3e 6e 45 6e 74 72 79 3d  ( pFifo->nEntry=
15de0 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  =0 ){.      asse
15de1 72 74 28 20 70 46 69 66 6f 2d 3e 70 4c 61 73 74  rt( pFifo->pLast
15de2 3d 3d 70 50 61 67 65 20 29 3b 0a 20 20 20 20 20  ==pPage );.     
15de3 20 70 46 69 66 6f 2d 3e 70 4c 61 73 74 20 3d 20   pFifo->pLast = 
15de4 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  0;.    }else{.  
15de5 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 66      assert( pFif
15de6 6f 2d 3e 70 46 69 72 73 74 21 3d 30 20 29 3b 0a  o->pFirst!=0 );.
15de7 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
15de8 20 20 20 61 73 73 65 72 74 28 20 70 46 69 66 6f     assert( pFifo
15de9 2d 3e 6e 45 6e 74 72 79 3e 30 20 29 3b 0a 20 20  ->nEntry>0 );.  
15dea 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
15deb 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  E_OK;.}../*.** D
15dec 65 6c 65 74 65 20 61 6c 6c 20 69 6e 66 6f 72 6d  elete all inform
15ded 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 46 69 66  ation from a Fif
15dee 6f 20 6f 62 6a 65 63 74 2e 20 20 20 46 72 65 65  o object.   Free
15def 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 68 65 6c 64   all memory held
15df0 0a 2a 2a 20 62 79 20 74 68 65 20 46 69 66 6f 2e  .** by the Fifo.
15df1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
15df2 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
15df3 64 62 65 46 69 66 6f 43 6c 65 61 72 28 46 69 66  dbeFifoClear(Fif
15df4 6f 20 2a 70 46 69 66 6f 29 7b 0a 20 20 46 69 66  o *pFifo){.  Fif
15df5 6f 50 61 67 65 20 2a 70 50 61 67 65 2c 20 2a 70  oPage *pPage, *p
15df6 4e 65 78 74 50 61 67 65 3b 0a 20 20 66 6f 72 28  NextPage;.  for(
15df7 70 50 61 67 65 3d 70 46 69 66 6f 2d 3e 70 46 69  pPage=pFifo->pFi
15df8 72 73 74 3b 20 70 50 61 67 65 3b 20 70 50 61 67  rst; pPage; pPag
15df9 65 3d 70 4e 65 78 74 50 61 67 65 29 7b 0a 20 20  e=pNextPage){.  
15dfa 20 20 70 4e 65 78 74 50 61 67 65 20 3d 20 70 50    pNextPage = pP
15dfb 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  age->pNext;.    
15dfc 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 46  sqlite3DbFree(pF
15dfd 69 66 6f 2d 3e 64 62 2c 20 70 50 61 67 65 29 3b  ifo->db, pPage);
15dfe 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64  .  }.  sqlite3Vd
15dff 62 65 46 69 66 6f 49 6e 69 74 28 70 46 69 66 6f  beFifoInit(pFifo
15e00 2c 20 70 46 69 66 6f 2d 3e 64 62 29 3b 0a 7d 0a  , pFifo->db);.}.
15e01 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
15e02 20 45 6e 64 20 6f 66 20 76 64 62 65 66 69 66 6f   End of vdbefifo
15e03 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
15e04 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15e05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
15e06 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
15e07 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65   Begin file vdbe
15e08 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  mem.c **********
15e09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15e0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
15e0b 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20  ./*.** 2004 May 
15e0c 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  26.**.** The aut
15e0d 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
15e0e 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
15e0f 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
15e10 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
15e11 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
15e12 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
15e13 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
15e14 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
15e15 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
15e16 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
15e17 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
15e18 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
15e19 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
15e1a 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
15e1b 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
15e1c 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
15e1d 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
15e1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15e1f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15e21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15e22 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
15e23 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
15e24 75 73 65 20 74 6f 20 6d 61 6e 69 70 75 6c 61 74  use to manipulat
15e25 65 20 22 4d 65 6d 22 20 73 74 72 75 63 74 75 72  e "Mem" structur
15e26 65 2e 20 20 41 20 22 4d 65 6d 22 0a 2a 2a 20 73  e.  A "Mem".** s
15e27 74 6f 72 65 73 20 61 20 73 69 6e 67 6c 65 20 76  tores a single v
15e28 61 6c 75 65 20 69 6e 20 74 68 65 20 56 44 42 45  alue in the VDBE
15e29 2e 20 20 4d 65 6d 20 69 73 20 61 6e 20 6f 70 61  .  Mem is an opa
15e2a 71 75 65 20 73 74 72 75 63 74 75 72 65 20 76 69  que structure vi
15e2b 73 69 62 6c 65 0a 2a 2a 20 6f 6e 6c 79 20 77 69  sible.** only wi
15e2c 74 68 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20  thin the VDBE.  
15e2d 49 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e  Interface routin
15e2e 65 73 20 72 65 66 65 72 20 74 6f 20 61 20 4d 65  es refer to a Me
15e2f 6d 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 6e  m using the.** n
15e30 61 6d 65 20 73 71 6c 69 74 65 5f 76 61 6c 75 65  ame sqlite_value
15e31 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65  .**.** $Id: vdbe
15e32 6d 65 6d 2e 63 2c 76 20 31 2e 31 32 35 20 32 30  mem.c,v 1.125 20
15e33 30 38 2f 31 31 2f 30 35 20 31 37 3a 34 31 3a 31  08/11/05 17:41:1
15e34 39 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  9 drh Exp $.*/..
15e35 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74  /*.** Call sqlit
15e36 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42  e3VdbeMemExpandB
15e37 6c 6f 62 28 29 20 6f 6e 20 74 68 65 20 73 75 70  lob() on the sup
15e38 70 6c 69 65 64 20 76 61 6c 75 65 20 28 74 79 70  plied value (typ
15e39 65 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20  e Mem*).** P if 
15e3a 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65  required..*/.#de
15e3b 66 69 6e 65 20 65 78 70 61 6e 64 42 6c 6f 62 28  fine expandBlob(
15e3c 50 29 20 28 28 28 50 29 2d 3e 66 6c 61 67 73 26  P) (((P)->flags&
15e3d 4d 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65  MEM_Zero)?sqlite
15e3e 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c  3VdbeMemExpandBl
15e3f 6f 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20  ob(P):0)../*.** 
15e40 49 66 20 70 4d 65 6d 20 69 73 20 61 6e 20 6f 62  If pMem is an ob
15e41 6a 65 63 74 20 77 69 74 68 20 61 20 76 61 6c 69  ject with a vali
15e42 64 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65  d string represe
15e43 6e 74 61 74 69 6f 6e 2c 20 74 68 69 73 20 72 6f  ntation, this ro
15e44 75 74 69 6e 65 0a 2a 2a 20 65 6e 73 75 72 65 73  utine.** ensures
15e45 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 65 6e   the internal en
15e46 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20 73  coding for the s
15e47 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61  tring representa
15e48 74 69 6f 6e 20 69 73 0a 2a 2a 20 27 64 65 73 69  tion is.** 'desi
15e49 72 65 64 45 6e 63 27 2c 20 6f 6e 65 20 6f 66 20  redEnc', one of 
15e4a 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c  SQLITE_UTF8, SQL
15e4b 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 20 53  ITE_UTF16LE or S
15e4c 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2e 0a 2a  QLITE_UTF16BE..*
15e4d 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20  *.** If pMem is 
15e4e 6e 6f 74 20 61 20 73 74 72 69 6e 67 20 6f 62 6a  not a string obj
15e4f 65 63 74 2c 20 6f 72 20 74 68 65 20 65 6e 63 6f  ect, or the enco
15e50 64 69 6e 67 20 6f 66 20 74 68 65 20 73 74 72 69  ding of the stri
15e51 6e 67 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 61  ng.** representa
15e52 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 79 20  tion is already 
15e53 73 74 6f 72 65 64 20 75 73 69 6e 67 20 74 68 65  stored using the
15e54 20 72 65 71 75 65 73 74 65 64 20 65 6e 63 6f 64   requested encod
15e55 69 6e 67 2c 20 74 68 65 6e 20 74 68 69 73 0a 2a  ing, then this.*
15e56 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  * routine is a n
15e57 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49  o-op..**.** SQLI
15e58 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
15e59 64 20 69 66 20 74 68 65 20 63 6f 6e 76 65 72 73  d if the convers
15e5a 69 6f 6e 20 69 73 20 73 75 63 63 65 73 73 66 75  ion is successfu
15e5b 6c 20 28 6f 72 20 6e 6f 74 20 72 65 71 75 69 72  l (or not requir
15e5c 65 64 29 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e  ed)..** SQLITE_N
15e5d 4f 4d 45 4d 20 6d 61 79 20 62 65 20 72 65 74 75  OMEM may be retu
15e5e 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63  rned if a malloc
15e5f 28 29 20 66 61 69 6c 73 20 64 75 72 69 6e 67 20  () fails during 
15e60 63 6f 6e 76 65 72 73 69 6f 6e 0a 2a 2a 20 62 65  conversion.** be
15e61 74 77 65 65 6e 20 66 6f 72 6d 61 74 73 2e 0a 2a  tween formats..*
15e62 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
15e63 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
15e64 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d  ChangeEncoding(M
15e65 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 64 65  em *pMem, int de
15e66 73 69 72 65 64 45 6e 63 29 7b 0a 20 20 69 6e 74  siredEnc){.  int
15e67 20 72 63 3b 0a 20 20 69 66 28 20 21 28 70 4d 65   rc;.  if( !(pMe
15e68 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72  m->flags&MEM_Str
15e69 29 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d  ) || pMem->enc==
15e6a 64 65 73 69 72 65 64 45 6e 63 20 29 7b 0a 20 20  desiredEnc ){.  
15e6b 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
15e6c 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  OK;.  }.  assert
15e6d 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
15e6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
15e6f 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
15e70 74 65 78 29 20 29 3b 0a 23 69 66 64 65 66 20 53  tex) );.#ifdef S
15e71 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
15e72 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
15e73 5f 45 52 52 4f 52 3b 0a 23 65 6c 73 65 0a 0a 20  _ERROR;.#else.. 
15e74 20 2f 2a 20 4d 65 6d 54 72 61 6e 73 6c 61 74 65   /* MemTranslate
15e75 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51  () may return SQ
15e76 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 4c 49 54  LITE_OK or SQLIT
15e77 45 5f 4e 4f 4d 45 4d 2e 20 49 66 20 4e 4f 4d 45  E_NOMEM. If NOME
15e78 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 20  M is returned,. 
15e79 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 65 6e 63   ** then the enc
15e7a 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 76 61 6c  oding of the val
15e7b 75 65 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20  ue may not have 
15e7c 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 20  changed..  */.  
15e7d 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
15e7e 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 70 4d 65  MemTranslate(pMe
15e7f 6d 2c 20 64 65 73 69 72 65 64 45 6e 63 29 3b 0a  m, desiredEnc);.
15e80 20 20 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c    assert(rc==SQL
15e81 49 54 45 5f 4f 4b 20 20 20 20 7c 7c 20 72 63 3d  ITE_OK    || rc=
15e82 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a  =SQLITE_NOMEM);.
15e83 20 20 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c    assert(rc==SQL
15e84 49 54 45 5f 4f 4b 20 20 20 20 7c 7c 20 70 4d 65  ITE_OK    || pMe
15e85 6d 2d 3e 65 6e 63 21 3d 64 65 73 69 72 65 64 45  m->enc!=desiredE
15e86 6e 63 29 3b 0a 20 20 61 73 73 65 72 74 28 72 63  nc);.  assert(rc
15e87 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c  ==SQLITE_NOMEM |
15e88 7c 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 65 73  | pMem->enc==des
15e89 69 72 65 64 45 6e 63 29 3b 0a 20 20 72 65 74 75  iredEnc);.  retu
15e8a 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a  rn rc;.#endif.}.
15e8b 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
15e8c 20 70 4d 65 6d 2d 3e 7a 20 70 6f 69 6e 74 73 20   pMem->z points 
15e8d 74 6f 20 61 20 77 72 69 74 61 62 6c 65 20 61 6c  to a writable al
15e8e 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61 74 20 6c  location of at l
15e8f 65 61 73 74 20 0a 2a 2a 20 6e 20 62 79 74 65 73  east .** n bytes
15e90 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d  ..**.** If the m
15e91 65 6d 6f 72 79 20 63 65 6c 6c 20 63 75 72 72 65  emory cell curre
15e92 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73 20 73 74  ntly contains st
15e93 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 64 61 74  ring or blob dat
15e94 61 0a 2a 2a 20 61 6e 64 20 74 68 65 20 74 68 69  a.** and the thi
15e95 72 64 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73  rd argument pass
15e96 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  ed to this funct
15e97 69 6f 6e 20 69 73 20 74 72 75 65 2c 20 74 68 65  ion is true, the
15e98 20 0a 2a 2a 20 63 75 72 72 65 6e 74 20 63 6f 6e   .** current con
15e99 74 65 6e 74 20 6f 66 20 74 68 65 20 63 65 6c 6c  tent of the cell
15e9a 20 69 73 20 70 72 65 73 65 72 76 65 64 2e 20 4f   is preserved. O
15e9b 74 68 65 72 77 69 73 65 2c 20 69 74 20 6d 61 79  therwise, it may
15e9c 0a 2a 2a 20 62 65 20 64 69 73 63 61 72 64 65 64  .** be discarded
15e9d 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  .  .**.** This f
15e9e 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 74 68 65  unction sets the
15e9f 20 4d 45 4d 5f 44 79 6e 20 66 6c 61 67 20 61 6e   MEM_Dyn flag an
15ea0 64 20 63 6c 65 61 72 73 20 61 6e 79 20 78 44 65  d clears any xDe
15ea1 6c 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 49  l callback..** I
15ea2 74 20 61 6c 73 6f 20 63 6c 65 61 72 73 20 4d 45  t also clears ME
15ea3 4d 5f 45 70 68 65 6d 20 61 6e 64 20 4d 45 4d 5f  M_Ephem and MEM_
15ea4 53 74 61 74 69 63 2e 20 49 66 20 74 68 65 20 70  Static. If the p
15ea5 72 65 73 65 72 76 65 20 66 6c 61 67 20 69 73 20  reserve flag is 
15ea6 0a 2a 2a 20 6e 6f 74 20 73 65 74 2c 20 4d 65 6d  .** not set, Mem
15ea7 2e 6e 20 69 73 20 7a 65 72 6f 65 64 2e 0a 2a 2f  .n is zeroed..*/
15ea8 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
15ea9 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
15eaa 65 6d 47 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d  emGrow(Mem *pMem
15eab 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65  , int n, int pre
15eac 73 65 72 76 65 29 7b 0a 20 20 61 73 73 65 72 74  serve){.  assert
15ead 28 20 31 20 3e 3d 0a 20 20 20 20 28 28 70 4d 65  ( 1 >=.    ((pMe
15eae 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 26 26 20 70 4d  m->zMalloc && pM
15eaf 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 3d 3d 70 4d 65  em->zMalloc==pMe
15eb0 6d 2d 3e 7a 29 20 3f 20 31 20 3a 20 30 29 20 2b  m->z) ? 1 : 0) +
15eb1 0a 20 20 20 20 28 28 28 70 4d 65 6d 2d 3e 66 6c  .    (((pMem->fl
15eb2 61 67 73 26 4d 45 4d 5f 44 79 6e 29 26 26 70 4d  ags&MEM_Dyn)&&pM
15eb3 65 6d 2d 3e 78 44 65 6c 29 20 3f 20 31 20 3a 20  em->xDel) ? 1 : 
15eb4 30 29 20 2b 20 0a 20 20 20 20 28 28 70 4d 65 6d  0) + .    ((pMem
15eb5 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 45 70 68 65  ->flags&MEM_Ephe
15eb6 6d 29 20 3f 20 31 20 3a 20 30 29 20 2b 20 0a 20  m) ? 1 : 0) + . 
15eb7 20 20 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73     ((pMem->flags
15eb8 26 4d 45 4d 5f 53 74 61 74 69 63 29 20 3f 20 31  &MEM_Static) ? 1
15eb9 20 3a 20 30 29 0a 20 20 29 3b 0a 0a 20 20 69 66   : 0).  );..  if
15eba 28 20 6e 3c 33 32 20 29 20 6e 20 3d 20 33 32 3b  ( n<32 ) n = 32;
15ebb 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 44 62  .  if( sqlite3Db
15ebc 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4d 65 6d 2d  MallocSize(pMem-
15ebd 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c  >db, pMem->zMall
15ebe 6f 63 29 3c 6e 20 29 7b 0a 20 20 20 20 69 66 28  oc)<n ){.    if(
15ebf 20 70 72 65 73 65 72 76 65 20 26 26 20 70 4d 65   preserve && pMe
15ec0 6d 2d 3e 7a 3d 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c  m->z==pMem->zMal
15ec1 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 70 4d 65  loc ){.      pMe
15ec2 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 4d 61  m->z = pMem->zMa
15ec3 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62  lloc = sqlite3Db
15ec4 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 4d  ReallocOrFree(pM
15ec5 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 2c  em->db, pMem->z,
15ec6 20 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21   n);.      if( !
15ec7 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 20  pMem->z ){.     
15ec8 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
15ec9 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20   MEM_Null;.     
15eca 20 7d 0a 20 20 20 20 20 20 70 72 65 73 65 72 76   }.      preserv
15ecb 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65  e = 0;.    }else
15ecc 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  {.      sqlite3D
15ecd 62 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c 20  bFree(pMem->db, 
15ece 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a  pMem->zMalloc);.
15ecf 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c        pMem->zMal
15ed0 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  loc = sqlite3DbM
15ed1 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d 2d 3e 64  allocRaw(pMem->d
15ed2 62 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  b, n);.    }.  }
15ed3 0a 0a 20 20 69 66 28 20 70 72 65 73 65 72 76 65  ..  if( preserve
15ed4 20 26 26 20 70 4d 65 6d 2d 3e 7a 20 26 26 20 70   && pMem->z && p
15ed5 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 26 26 20  Mem->zMalloc && 
15ed6 70 4d 65 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a  pMem->z!=pMem->z
15ed7 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 6d 65  Malloc ){.    me
15ed8 6d 63 70 79 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c  mcpy(pMem->zMall
15ed9 6f 63 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 70 4d 65  oc, pMem->z, pMe
15eda 6d 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28  m->n);.  }.  if(
15edb 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d   pMem->flags&MEM
15edc 5f 44 79 6e 20 26 26 20 70 4d 65 6d 2d 3e 78 44  _Dyn && pMem->xD
15edd 65 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e  el ){.    pMem->
15ede 78 44 65 6c 28 28 76 6f 69 64 20 2a 29 28 70 4d  xDel((void *)(pM
15edf 65 6d 2d 3e 7a 29 29 3b 0a 20 20 7d 0a 0a 20 20  em->z));.  }..  
15ee0 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e  pMem->z = pMem->
15ee1 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 70 4d 65 6d 2d  zMalloc;.  pMem-
15ee2 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f  >flags &= ~(MEM_
15ee3 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63  Ephem|MEM_Static
15ee4 29 3b 0a 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20  );.  pMem->xDel 
15ee5 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 28 70  = 0;.  return (p
15ee6 4d 65 6d 2d 3e 7a 20 3f 20 53 51 4c 49 54 45 5f  Mem->z ? SQLITE_
15ee7 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  OK : SQLITE_NOME
15ee8 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  M);.}../*.** Mak
15ee9 65 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 20  e the given Mem 
15eea 6f 62 6a 65 63 74 20 4d 45 4d 5f 44 79 6e 2e 20  object MEM_Dyn. 
15eeb 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
15eec 20 6d 61 6b 65 20 69 74 20 73 6f 0a 2a 2a 20 74   make it so.** t
15eed 68 61 74 20 61 6e 79 20 54 45 58 54 20 6f 72 20  hat any TEXT or 
15eee 42 4c 4f 42 20 63 6f 6e 74 65 6e 74 20 69 73 20  BLOB content is 
15eef 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79  stored in memory
15ef0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a   obtained from.*
15ef1 2a 20 6d 61 6c 6c 6f 63 28 29 2e 20 20 49 6e 20  * malloc().  In 
15ef2 74 68 69 73 20 77 61 79 2c 20 77 65 20 6b 6e 6f  this way, we kno
15ef3 77 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72  w that the memor
15ef4 79 20 69 73 20 73 61 66 65 20 74 6f 20 62 65 0a  y is safe to be.
15ef5 2a 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 20 6f  ** overwritten o
15ef6 72 20 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a  r altered..**.**
15ef7 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
15ef8 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20  K on success or 
15ef9 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20  SQLITE_NOMEM if 
15efa 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f  malloc fails..*/
15efb 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
15efc 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
15efd 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28  emMakeWriteable(
15efe 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e  Mem *pMem){.  in
15eff 74 20 66 3b 0a 20 20 61 73 73 65 72 74 28 20 70  t f;.  assert( p
15f00 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  Mem->db==0 || sq
15f01 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
15f02 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  (pMem->db->mutex
15f03 29 20 29 3b 0a 20 20 65 78 70 61 6e 64 42 6c 6f  ) );.  expandBlo
15f04 62 28 70 4d 65 6d 29 3b 0a 20 20 66 20 3d 20 70  b(pMem);.  f = p
15f05 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66  Mem->flags;.  if
15f06 28 20 28 66 26 28 4d 45 4d 5f 53 74 72 7c 4d 45  ( (f&(MEM_Str|ME
15f07 4d 5f 42 6c 6f 62 29 29 20 26 26 20 70 4d 65 6d  M_Blob)) && pMem
15f08 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c  ->z!=pMem->zMall
15f09 6f 63 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71  oc ){.    if( sq
15f0a 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77  lite3VdbeMemGrow
15f0b 28 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 20 2b  (pMem, pMem->n +
15f0c 20 32 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20   2, 1) ){.      
15f0d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
15f0e 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  MEM;.    }.    p
15f0f 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20  Mem->z[pMem->n] 
15f10 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a  = 0;.    pMem->z
15f11 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 3b  [pMem->n+1] = 0;
15f12 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  .    pMem->flags
15f13 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20   |= MEM_Term;.  
15f14 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  }..  return SQLI
15f15 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
15f16 49 66 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d  If the given Mem
15f17 2a 20 68 61 73 20 61 20 7a 65 72 6f 2d 66 69 6c  * has a zero-fil
15f18 6c 65 64 20 74 61 69 6c 2c 20 74 75 72 6e 20 69  led tail, turn i
15f19 74 20 69 6e 74 6f 20 61 6e 20 6f 72 64 69 6e 61  t into an ordina
15f1a 72 79 0a 2a 2a 20 62 6c 6f 62 20 73 74 6f 72 65  ry.** blob store
15f1b 64 20 69 6e 20 64 79 6e 61 6d 69 63 61 6c 6c 79  d in dynamically
15f1c 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65   allocated space
15f1d 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
15f1e 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f  ITE_OMIT_INCRBLO
15f1f 42 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  B.SQLITE_PRIVATE
15f20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
15f21 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65  MemExpandBlob(Me
15f22 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 66 28 20  m *pMem){.  if( 
15f23 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
15f24 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 69 6e  M_Zero ){.    in
15f25 74 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 73 73  t nByte;.    ass
15f26 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  ert( pMem->flags
15f27 26 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20  &MEM_Blob );.   
15f28 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64   assert( pMem->d
15f29 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
15f2a 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d  mutex_held(pMem-
15f2b 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a  >db->mutex) );..
15f2c 20 20 20 20 2f 2a 20 53 65 74 20 6e 42 79 74 65      /* Set nByte
15f2d 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   to the number o
15f2e 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64  f bytes required
15f2f 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 65 78   to store the ex
15f30 70 61 6e 64 65 64 20 62 6c 6f 62 2e 20 2a 2f 0a  panded blob. */.
15f31 20 20 20 20 6e 42 79 74 65 20 3d 20 70 4d 65 6d      nByte = pMem
15f32 2d 3e 6e 20 2b 20 70 4d 65 6d 2d 3e 75 2e 69 3b  ->n + pMem->u.i;
15f33 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3c 3d  .    if( nByte<=
15f34 30 20 29 7b 0a 20 20 20 20 20 20 6e 42 79 74 65  0 ){.      nByte
15f35 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 1;.    }.    
15f36 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d  if( sqlite3VdbeM
15f37 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79  emGrow(pMem, nBy
15f38 74 65 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20  te, 1) ){.      
15f39 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
15f3a 4d 45 4d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  MEM;.    }..    
15f3b 6d 65 6d 73 65 74 28 26 70 4d 65 6d 2d 3e 7a 5b  memset(&pMem->z[
15f3c 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65  pMem->n], 0, pMe
15f3d 6d 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 70 4d 65  m->u.i);.    pMe
15f3e 6d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e  m->n += pMem->u.
15f3f 69 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  i;.    pMem->fla
15f40 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 5a 65 72 6f  gs &= ~(MEM_Zero
15f41 7c 4d 45 4d 5f 54 65 72 6d 29 3b 0a 20 20 7d 0a  |MEM_Term);.  }.
15f42 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
15f43 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f  OK;.}.#endif.../
15f44 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74  *.** Make sure t
15f45 68 65 20 67 69 76 65 6e 20 4d 65 6d 20 69 73 20  he given Mem is 
15f46 5c 75 30 30 30 30 20 74 65 72 6d 69 6e 61 74 65  \u0000 terminate
15f47 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
15f48 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
15f49 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e  VdbeMemNulTermin
15f4a 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a  ate(Mem *pMem){.
15f4b 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
15f4c 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
15f4d 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d  _mutex_held(pMem
15f4e 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
15f4f 20 20 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61    if( (pMem->fla
15f50 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 21 3d  gs & MEM_Term)!=
15f51 30 20 7c 7c 20 28 70 4d 65 6d 2d 3e 66 6c 61 67  0 || (pMem->flag
15f52 73 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20  s & MEM_Str)==0 
15f53 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
15f54 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f 2a 20 4e 6f  LITE_OK;   /* No
15f55 74 68 69 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a 20  thing to do */. 
15f56 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
15f57 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d  VdbeMemGrow(pMem
15f58 2c 20 70 4d 65 6d 2d 3e 6e 2b 32 2c 20 31 29 20  , pMem->n+2, 1) 
15f59 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
15f5a 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
15f5b 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e    pMem->z[pMem->
15f5c 6e 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e  n] = 0;.  pMem->
15f5d 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30  z[pMem->n+1] = 0
15f5e 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  ;.  pMem->flags 
15f5f 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 72  |= MEM_Term;.  r
15f60 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
15f61 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4d 45  .}../*.** Add ME
15f62 4d 5f 53 74 72 20 74 6f 20 74 68 65 20 73 65 74  M_Str to the set
15f63 20 6f 66 20 72 65 70 72 65 73 65 6e 74 61 74 69   of representati
15f64 6f 6e 73 20 66 6f 72 20 74 68 65 20 67 69 76 65  ons for the give
15f65 6e 20 4d 65 6d 2e 20 20 4e 75 6d 62 65 72 73 0a  n Mem.  Numbers.
15f66 2a 2a 20 61 72 65 20 63 6f 6e 76 65 72 74 65 64  ** are converted
15f67 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 73   using sqlite3_s
15f68 6e 70 72 69 6e 74 66 28 29 2e 20 20 43 6f 6e 76  nprintf().  Conv
15f69 65 72 74 69 6e 67 20 61 20 42 4c 4f 42 20 74 6f  erting a BLOB to
15f6a 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20   a string.** is 
15f6b 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 45  a no-op..**.** E
15f6c 78 69 73 74 69 6e 67 20 72 65 70 72 65 73 65 6e  xisting represen
15f6d 74 61 74 69 6f 6e 73 20 4d 45 4d 5f 49 6e 74 20  tations MEM_Int 
15f6e 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 61 72 65  and MEM_Real are
15f6f 20 2a 6e 6f 74 2a 20 69 6e 76 61 6c 69 64 61 74   *not* invalidat
15f70 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 4d 45 4d 5f  ed..**.** A MEM_
15f71 4e 75 6c 6c 20 76 61 6c 75 65 20 77 69 6c 6c 20  Null value will 
15f72 6e 65 76 65 72 20 62 65 20 70 61 73 73 65 64 20  never be passed 
15f73 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
15f74 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
15f75 69 73 0a 2a 2a 20 75 73 65 64 20 66 6f 72 20 63  is.** used for c
15f76 6f 6e 76 65 72 74 69 6e 67 20 76 61 6c 75 65 73  onverting values
15f77 20 74 6f 20 74 65 78 74 20 66 6f 72 20 72 65 74   to text for ret
15f78 75 72 6e 69 6e 67 20 74 6f 20 74 68 65 20 75 73  urning to the us
15f79 65 72 20 28 69 2e 65 2e 20 76 69 61 0a 2a 2a 20  er (i.e. via.** 
15f7a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
15f7b 78 74 28 29 29 2c 20 6f 72 20 66 6f 72 20 65 6e  xt()), or for en
15f7c 73 75 72 69 6e 67 20 74 68 61 74 20 76 61 6c 75  suring that valu
15f7d 65 73 20 74 6f 20 62 65 20 75 73 65 64 20 61 73  es to be used as
15f7e 20 62 74 72 65 65 0a 2a 2a 20 6b 65 79 73 20 61   btree.** keys a
15f7f 72 65 20 73 74 72 69 6e 67 73 2e 20 49 6e 20 74  re strings. In t
15f80 68 65 20 66 6f 72 6d 65 72 20 63 61 73 65 20 61  he former case a
15f81 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73   NULL pointer is
15f82 20 72 65 74 75 72 6e 65 64 20 74 68 65 0a 2a 2a   returned the.**
15f83 20 75 73 65 72 20 61 6e 64 20 74 68 65 20 6c 61   user and the la
15f84 74 65 72 20 69 73 20 61 6e 20 69 6e 74 65 72 6e  ter is an intern
15f85 61 6c 20 70 72 6f 67 72 61 6d 6d 69 6e 67 20 65  al programming e
15f86 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rror..*/.SQLITE_
15f87 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
15f88 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67  te3VdbeMemString
15f89 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69  ify(Mem *pMem, i
15f8a 6e 74 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20 72  nt enc){.  int r
15f8b 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
15f8c 20 69 6e 74 20 66 67 20 3d 20 70 4d 65 6d 2d 3e   int fg = pMem->
15f8d 66 6c 61 67 73 3b 0a 20 20 63 6f 6e 73 74 20 69  flags;.  const i
15f8e 6e 74 20 6e 42 79 74 65 20 3d 20 33 32 3b 0a 0a  nt nByte = 32;..
15f8f 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
15f90 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
15f91 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d  _mutex_held(pMem
15f92 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
15f93 20 20 61 73 73 65 72 74 28 20 21 28 66 67 26 4d    assert( !(fg&M
15f94 45 4d 5f 5a 65 72 6f 29 20 29 3b 0a 20 20 61 73  EM_Zero) );.  as
15f95 73 65 72 74 28 20 21 28 66 67 26 28 4d 45 4d 5f  sert( !(fg&(MEM_
15f96 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 20 29  Str|MEM_Blob)) )
15f97 3b 0a 20 20 61 73 73 65 72 74 28 20 66 67 26 28  ;.  assert( fg&(
15f98 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c  MEM_Int|MEM_Real
15f99 29 20 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69  ) );..  if( sqli
15f9a 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70  te3VdbeMemGrow(p
15f9b 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 30 29 20 29  Mem, nByte, 0) )
15f9c 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
15f9d 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a  ITE_NOMEM;.  }..
15f9e 20 20 2f 2a 20 46 6f 72 20 61 20 52 65 61 6c 20    /* For a Real 
15f9f 6f 72 20 49 6e 74 65 67 65 72 2c 20 75 73 65 20  or Integer, use 
15fa0 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
15fa1 29 20 74 6f 20 70 72 6f 64 75 63 65 20 74 68 65  ) to produce the
15fa2 20 55 54 46 2d 38 0a 20 20 2a 2a 20 73 74 72 69   UTF-8.  ** stri
15fa3 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ng representatio
15fa4 6e 20 6f 66 20 74 68 65 20 76 61 6c 75 65 2e 20  n of the value. 
15fa5 54 68 65 6e 2c 20 69 66 20 74 68 65 20 72 65 71  Then, if the req
15fa6 75 69 72 65 64 20 65 6e 63 6f 64 69 6e 67 0a 20  uired encoding. 
15fa7 20 2a 2a 20 69 73 20 55 54 46 2d 31 36 6c 65 20   ** is UTF-16le 
15fa8 6f 72 20 55 54 46 2d 31 36 62 65 20 64 6f 20 61  or UTF-16be do a
15fa9 20 74 72 61 6e 73 6c 61 74 69 6f 6e 2e 0a 20 20   translation..  
15faa 2a 2a 20 0a 20 20 2a 2a 20 46 49 58 20 4d 45 3a  ** .  ** FIX ME:
15fab 20 49 74 20 77 6f 75 6c 64 20 62 65 20 62 65 74   It would be bet
15fac 74 65 72 20 69 66 20 73 71 6c 69 74 65 33 5f 73  ter if sqlite3_s
15fad 6e 70 72 69 6e 74 66 28 29 20 63 6f 75 6c 64 20  nprintf() could 
15fae 64 6f 20 55 54 46 2d 31 36 2e 0a 20 20 2a 2f 0a  do UTF-16..  */.
15faf 20 20 69 66 28 20 66 67 20 26 20 4d 45 4d 5f 49    if( fg & MEM_I
15fb0 6e 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  nt ){.    sqlite
15fb1 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 79 74 65  3_snprintf(nByte
15fb2 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 6c 6c 64  , pMem->z, "%lld
15fb3 22 2c 20 70 4d 65 6d 2d 3e 75 2e 69 29 3b 0a 20  ", pMem->u.i);. 
15fb4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
15fb5 72 74 28 20 66 67 20 26 20 4d 45 4d 5f 52 65 61  rt( fg & MEM_Rea
15fb6 6c 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  l );.    sqlite3
15fb7 5f 73 6e 70 72 69 6e 74 66 28 6e 42 79 74 65 2c  _snprintf(nByte,
15fb8 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 21 2e 31 35   pMem->z, "%!.15
15fb9 67 22 2c 20 70 4d 65 6d 2d 3e 72 29 3b 0a 20 20  g", pMem->r);.  
15fba 7d 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 74  }.  pMem->n = st
15fbb 72 6c 65 6e 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20  rlen(pMem->z);. 
15fbc 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c   pMem->enc = SQL
15fbd 49 54 45 5f 55 54 46 38 3b 0a 20 20 70 4d 65 6d  ITE_UTF8;.  pMem
15fbe 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53  ->flags |= MEM_S
15fbf 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 73  tr|MEM_Term;.  s
15fc0 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
15fc1 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 65  Encoding(pMem, e
15fc2 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  nc);.  return rc
15fc3 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72  ;.}../*.** Memor
15fc4 79 20 63 65 6c 6c 20 70 4d 65 6d 20 63 6f 6e 74  y cell pMem cont
15fc5 61 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 78 74  ains the context
15fc6 20 6f 66 20 61 6e 20 61 67 67 72 65 67 61 74 65   of an aggregate
15fc7 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68   function..** Th
15fc8 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73  is routine calls
15fc9 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 20 6d 65   the finalize me
15fca 74 68 6f 64 20 66 6f 72 20 74 68 61 74 20 66 75  thod for that fu
15fcb 6e 63 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20  nction.  The.** 
15fcc 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 61 67  result of the ag
15fcd 67 72 65 67 61 74 65 20 69 73 20 73 74 6f 72 65  gregate is store
15fce 64 20 62 61 63 6b 20 69 6e 74 6f 20 70 4d 65 6d  d back into pMem
15fcf 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ..**.** Return S
15fd0 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20 74  QLITE_ERROR if t
15fd1 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 72 65 70  he finalizer rep
15fd2 6f 72 74 73 20 61 6e 20 65 72 72 6f 72 2e 20 20  orts an error.  
15fd3 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 74 68  SQLITE_OK.** oth
15fd4 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  erwise..*/.SQLIT
15fd5 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
15fd6 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61  lite3VdbeMemFina
15fd7 6c 69 7a 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20  lize(Mem *pMem, 
15fd8 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 29 7b  FuncDef *pFunc){
15fd9 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
15fda 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 46 75  TE_OK;.  if( pFu
15fdb 6e 63 20 26 26 20 70 46 75 6e 63 2d 3e 78 46 69  nc && pFunc->xFi
15fdc 6e 61 6c 69 7a 65 20 29 7b 0a 20 20 20 20 73 71  nalize ){.    sq
15fdd 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74  lite3_context ct
15fde 78 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  x;.    assert( (
15fdf 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
15fe0 4d 5f 4e 75 6c 6c 29 21 3d 30 20 7c 7c 20 70 46  M_Null)!=0 || pF
15fe1 75 6e 63 3d 3d 70 4d 65 6d 2d 3e 75 2e 70 44 65  unc==pMem->u.pDe
15fe2 66 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  f );.    assert(
15fe3 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20   pMem->db==0 || 
15fe4 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
15fe5 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74  ld(pMem->db->mut
15fe6 65 78 29 20 29 3b 0a 20 20 20 20 6d 65 6d 73 65  ex) );.    memse
15fe7 74 28 26 63 74 78 2c 20 30 2c 20 73 69 7a 65 6f  t(&ctx, 0, sizeo
15fe8 66 28 63 74 78 29 29 3b 0a 20 20 20 20 63 74 78  f(ctx));.    ctx
15fe9 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  .s.flags = MEM_N
15fea 75 6c 6c 3b 0a 20 20 20 20 63 74 78 2e 73 2e 64  ull;.    ctx.s.d
15feb 62 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b 0a 20 20  b = pMem->db;.  
15fec 20 20 63 74 78 2e 70 4d 65 6d 20 3d 20 70 4d 65    ctx.pMem = pMe
15fed 6d 3b 0a 20 20 20 20 63 74 78 2e 70 46 75 6e 63  m;.    ctx.pFunc
15fee 20 3d 20 70 46 75 6e 63 3b 0a 20 20 20 20 70 46   = pFunc;.    pF
15fef 75 6e 63 2d 3e 78 46 69 6e 61 6c 69 7a 65 28 26  unc->xFinalize(&
15ff0 63 74 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ctx);.    assert
15ff1 28 20 30 3d 3d 28 70 4d 65 6d 2d 3e 66 6c 61 67  ( 0==(pMem->flag
15ff2 73 26 4d 45 4d 5f 44 79 6e 29 20 26 26 20 21 70  s&MEM_Dyn) && !p
15ff3 4d 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a 20 20 20  Mem->xDel );.   
15ff4 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
15ff5 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a  Mem->db, pMem->z
15ff6 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 2a 70 4d  Malloc);.    *pM
15ff7 65 6d 20 3d 20 63 74 78 2e 73 3b 0a 20 20 20 20  em = ctx.s;.    
15ff8 72 63 20 3d 20 28 63 74 78 2e 69 73 45 72 72 6f  rc = (ctx.isErro
15ff9 72 3f 53 51 4c 49 54 45 5f 45 52 52 4f 52 3a 53  r?SQLITE_ERROR:S
15ffa 51 4c 49 54 45 5f 4f 4b 29 3b 0a 20 20 7d 0a 20  QLITE_OK);.  }. 
15ffb 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
15ffc 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d 65 6d 6f  *.** If the memo
15ffd 72 79 20 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 73  ry cell contains
15ffe 20 61 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20   a string value 
15fff 74 68 61 74 20 6d 75 73 74 20 62 65 20 66 72 65  that must be fre
16000 65 64 20 62 79 0a 2a 2a 20 69 6e 76 6f 6b 69 6e  ed by.** invokin
16001 67 20 61 6e 20 65 78 74 65 72 6e 61 6c 20 63 61  g an external ca
16002 6c 6c 62 61 63 6b 2c 20 66 72 65 65 20 69 74 20  llback, free it 
16003 6e 6f 77 2e 20 43 61 6c 6c 69 6e 67 20 74 68 69  now. Calling thi
16004 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 64 6f  s function.** do
16005 65 73 20 6e 6f 74 20 66 72 65 65 20 61 6e 79 20  es not free any 
16006 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 62 75 66 66  Mem.zMalloc buff
16007 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
16008 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
16009 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
1600a 45 78 74 65 72 6e 61 6c 28 4d 65 6d 20 2a 70 29  External(Mem *p)
1600b 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64  {.  assert( p->d
1600c 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
1600d 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62  mutex_held(p->db
1600e 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66  ->mutex) );.  if
1600f 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 41  ( p->flags&MEM_A
16010 67 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  gg ){.    sqlite
16011 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65  3VdbeMemFinalize
16012 28 70 2c 20 70 2d 3e 75 2e 70 44 65 66 29 3b 0a  (p, p->u.pDef);.
16013 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e      assert( (p->
16014 66 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 29  flags & MEM_Agg)
16015 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ==0 );.    sqlit
16016 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
16017 28 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  (p);.  }else if(
16018 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79   p->flags&MEM_Dy
16019 6e 20 26 26 20 70 2d 3e 78 44 65 6c 20 29 7b 0a  n && p->xDel ){.
1601a 20 20 20 20 70 2d 3e 78 44 65 6c 28 28 76 6f 69      p->xDel((voi
1601b 64 20 2a 29 70 2d 3e 7a 29 3b 0a 20 20 20 20 70  d *)p->z);.    p
1601c 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 7d 0a  ->xDel = 0;.  }.
1601d 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65  }../*.** Release
1601e 20 61 6e 79 20 6d 65 6d 6f 72 79 20 68 65 6c 64   any memory held
1601f 20 62 79 20 74 68 65 20 4d 65 6d 2e 20 54 68 69   by the Mem. Thi
16020 73 20 6d 61 79 20 6c 65 61 76 65 20 74 68 65 20  s may leave the 
16021 4d 65 6d 20 69 6e 20 61 6e 0a 2a 2a 20 69 6e 63  Mem in an.** inc
16022 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 2c  onsistent state,
16023 20 66 6f 72 20 65 78 61 6d 70 6c 65 20 77 69 74   for example wit
16024 68 20 28 4d 65 6d 2e 7a 3d 3d 30 29 20 61 6e 64  h (Mem.z==0) and
16025 0a 2a 2a 20 28 4d 65 6d 2e 74 79 70 65 3d 3d 53  .** (Mem.type==S
16026 51 4c 49 54 45 5f 54 45 58 54 29 2e 0a 2a 2f 0a  QLITE_TEXT)..*/.
16027 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
16028 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d  oid sqlite3VdbeM
16029 65 6d 52 65 6c 65 61 73 65 28 4d 65 6d 20 2a 70  emRelease(Mem *p
1602a 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ){.  sqlite3Vdbe
1602b 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e  MemReleaseExtern
1602c 61 6c 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  al(p);.  sqlite3
1602d 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d  DbFree(p->db, p-
1602e 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 70 2d 3e  >zMalloc);.  p->
1602f 7a 20 3d 20 30 3b 0a 20 20 70 2d 3e 7a 4d 61 6c  z = 0;.  p->zMal
16030 6c 6f 63 20 3d 20 30 3b 0a 20 20 70 2d 3e 78 44  loc = 0;.  p->xD
16031 65 6c 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  el = 0;.}../*.**
16032 20 43 6f 6e 76 65 72 74 20 61 20 36 34 2d 62 69   Convert a 64-bi
16033 74 20 49 45 45 45 20 64 6f 75 62 6c 65 20 69 6e  t IEEE double in
16034 74 6f 20 61 20 36 34 2d 62 69 74 20 73 69 67 6e  to a 64-bit sign
16035 65 64 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 49  ed integer..** I
16036 66 20 74 68 65 20 64 6f 75 62 6c 65 20 69 73 20  f the double is 
16037 74 6f 6f 20 6c 61 72 67 65 2c 20 72 65 74 75 72  too large, retur
16038 6e 20 30 78 38 30 30 30 30 30 30 30 30 30 30 30  n 0x800000000000
16039 30 30 30 30 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74  0000..**.** Most
1603a 20 73 79 73 74 65 6d 73 20 61 70 70 65 61 72 20   systems appear 
1603b 74 6f 20 64 6f 20 74 68 69 73 20 73 69 6d 70 6c  to do this simpl
1603c 79 20 62 79 20 61 73 73 69 67 6e 69 6e 67 0a 2a  y by assigning.*
1603d 2a 20 76 61 72 69 61 62 6c 65 73 20 61 6e 64 20  * variables and 
1603e 77 69 74 68 6f 75 74 20 74 68 65 20 65 78 74 72  without the extr
1603f 61 20 72 61 6e 67 65 20 74 65 73 74 73 2e 20 20  a range tests.  
16040 42 75 74 0a 2a 2a 20 74 68 65 72 65 20 61 72 65  But.** there are
16041 20 72 65 70 6f 72 74 73 20 74 68 61 74 20 77 69   reports that wi
16042 6e 64 6f 77 73 20 74 68 72 6f 77 73 20 61 6e 20  ndows throws an 
16043 65 78 70 65 63 74 69 6f 6e 0a 2a 2a 20 69 66 20  expection.** if 
16044 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  the floating poi
16045 6e 74 20 76 61 6c 75 65 20 69 73 20 6f 75 74 20  nt value is out 
16046 6f 66 20 72 61 6e 67 65 2e 20 28 53 65 65 20 74  of range. (See t
16047 69 63 6b 65 74 20 23 32 38 38 30 2e 29 0a 2a 2a  icket #2880.).**
16048 20 42 65 63 61 75 73 65 20 77 65 20 64 6f 20 6e   Because we do n
16049 6f 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20 75 6e  ot completely un
1604a 64 65 72 73 74 61 6e 64 20 74 68 65 20 70 72 6f  derstand the pro
1604b 62 6c 65 6d 2c 20 77 65 20 77 69 6c 6c 0a 2a 2a  blem, we will.**
1604c 20 74 61 6b 65 20 74 68 65 20 63 6f 6e 73 65 72   take the conser
1604d 76 61 74 69 76 65 20 61 70 70 72 6f 61 63 68 20  vative approach 
1604e 61 6e 64 20 61 6c 77 61 79 73 20 64 6f 20 72 61  and always do ra
1604f 6e 67 65 20 74 65 73 74 73 0a 2a 2a 20 62 65 66  nge tests.** bef
16050 6f 72 65 20 61 74 74 65 6d 70 74 69 6e 67 20 74  ore attempting t
16051 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a  he conversion..*
16052 2f 0a 73 74 61 74 69 63 20 69 36 34 20 64 6f 75  /.static i64 dou
16053 62 6c 65 54 6f 49 6e 74 36 34 28 64 6f 75 62 6c  bleToInt64(doubl
16054 65 20 72 29 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20  e r){.  /*.  ** 
16055 4d 61 6e 79 20 63 6f 6d 70 69 6c 65 72 73 20 77  Many compilers w
16056 65 20 65 6e 63 6f 75 6e 74 65 72 20 64 6f 20 6e  e encounter do n
16057 6f 74 20 64 65 66 69 6e 65 20 63 6f 6e 73 74 61  ot define consta
16058 6e 74 73 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a  nts for the.  **
16059 20 6d 69 6e 69 6d 75 6d 20 61 6e 64 20 6d 61 78   minimum and max
1605a 69 6d 75 6d 20 36 34 2d 62 69 74 20 69 6e 74 65  imum 64-bit inte
1605b 67 65 72 73 2c 20 6f 72 20 74 68 65 79 20 64 65  gers, or they de
1605c 66 69 6e 65 20 74 68 65 6d 0a 20 20 2a 2a 20 69  fine them.  ** i
1605d 6e 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e 20 20  nconsistently.  
1605e 41 6e 64 20 6d 61 6e 79 20 64 6f 20 6e 6f 74 20  And many do not 
1605f 75 6e 64 65 72 73 74 61 6e 64 20 74 68 65 20 22  understand the "
16060 4c 4c 22 20 6e 6f 74 61 74 69 6f 6e 2e 0a 20 20  LL" notation..  
16061 2a 2a 20 53 6f 20 77 65 20 64 65 66 69 6e 65 20  ** So we define 
16062 6f 75 72 20 6f 77 6e 20 73 74 61 74 69 63 20 63  our own static c
16063 6f 6e 73 74 61 6e 74 73 20 68 65 72 65 20 75 73  onstants here us
16064 69 6e 67 20 6e 6f 74 68 69 6e 67 0a 20 20 2a 2a  ing nothing.  **
16065 20 6c 61 72 67 65 72 20 74 68 61 6e 20 61 20 33   larger than a 3
16066 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 63 6f  2-bit integer co
16067 6e 73 74 61 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73  nstant..  */.  s
16068 74 61 74 69 63 20 63 6f 6e 73 74 20 69 36 34 20  tatic const i64 
16069 6d 61 78 49 6e 74 20 3d 20 4c 41 52 47 45 53 54  maxInt = LARGEST
1606a 5f 49 4e 54 36 34 3b 0a 20 20 73 74 61 74 69 63  _INT64;.  static
1606b 20 63 6f 6e 73 74 20 69 36 34 20 6d 69 6e 49 6e   const i64 minIn
1606c 74 20 3d 20 53 4d 41 4c 4c 45 53 54 5f 49 4e 54  t = SMALLEST_INT
1606d 36 34 3b 0a 0a 20 20 69 66 28 20 72 3c 28 64 6f  64;..  if( r<(do
1606e 75 62 6c 65 29 6d 69 6e 49 6e 74 20 29 7b 0a 20  uble)minInt ){. 
1606f 20 20 20 72 65 74 75 72 6e 20 6d 69 6e 49 6e 74     return minInt
16070 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 3e  ;.  }else if( r>
16071 28 64 6f 75 62 6c 65 29 6d 61 78 49 6e 74 20 29  (double)maxInt )
16072 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 69 6e  {.    return min
16073 49 6e 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  Int;.  }else{.  
16074 20 20 72 65 74 75 72 6e 20 28 69 36 34 29 72 3b    return (i64)r;
16075 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
16076 74 75 72 6e 20 73 6f 6d 65 20 6b 69 6e 64 20 6f  turn some kind o
16077 66 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  f integer value 
16078 77 68 69 63 68 20 69 73 20 74 68 65 20 62 65 73  which is the bes
16079 74 20 77 65 20 63 61 6e 20 64 6f 0a 2a 2a 20 61  t we can do.** a
1607a 74 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74  t representing t
1607b 68 65 20 76 61 6c 75 65 20 74 68 61 74 20 2a 70  he value that *p
1607c 4d 65 6d 20 64 65 73 63 72 69 62 65 73 20 61 73  Mem describes as
1607d 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20   an integer..** 
1607e 49 66 20 70 4d 65 6d 20 69 73 20 61 6e 20 69 6e  If pMem is an in
1607f 74 65 67 65 72 2c 20 74 68 65 6e 20 74 68 65 20  teger, then the 
16080 76 61 6c 75 65 20 69 73 20 65 78 61 63 74 2e 20  value is exact. 
16081 20 49 66 20 70 4d 65 6d 20 69 73 0a 2a 2a 20 61   If pMem is.** a
16082 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20   floating-point 
16083 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72  then the value r
16084 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 69  eturned is the i
16085 6e 74 65 67 65 72 20 70 61 72 74 2e 0a 2a 2a 20  nteger part..** 
16086 49 66 20 70 4d 65 6d 20 69 73 20 61 20 73 74 72  If pMem is a str
16087 69 6e 67 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65  ing or blob, the
16088 6e 20 77 65 20 6d 61 6b 65 20 61 6e 20 61 74 74  n we make an att
16089 65 6d 70 74 20 74 6f 20 63 6f 6e 76 65 72 74 0a  empt to convert.
1608a 2a 2a 20 69 74 20 69 6e 74 6f 20 61 20 69 6e 74  ** it into a int
1608b 65 67 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20  eger and return 
1608c 74 68 61 74 2e 20 20 49 66 20 70 4d 65 6d 20 69  that.  If pMem i
1608d 73 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30  s NULL, return 0
1608e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20  ..**.** If pMem 
1608f 69 73 20 61 20 73 74 72 69 6e 67 2c 20 69 74 73  is a string, its
16090 20 65 6e 63 6f 64 69 6e 67 20 6d 69 67 68 74 20   encoding might 
16091 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53  be changed..*/.S
16092 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36  QLITE_PRIVATE i6
16093 34 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74  4 sqlite3VdbeInt
16094 56 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d 29  Value(Mem *pMem)
16095 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20  {.  int flags;. 
16096 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64   assert( pMem->d
16097 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
16098 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d  mutex_held(pMem-
16099 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
1609a 20 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66   flags = pMem->f
1609b 6c 61 67 73 3b 0a 20 20 69 66 28 20 66 6c 61 67  lags;.  if( flag
1609c 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20  s & MEM_Int ){. 
1609d 20 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e     return pMem->
1609e 75 2e 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  u.i;.  }else if(
1609f 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61   flags & MEM_Rea
160a0 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  l ){.    return 
160a1 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d  doubleToInt64(pM
160a2 65 6d 2d 3e 72 29 3b 0a 20 20 7d 65 6c 73 65 20  em->r);.  }else 
160a3 69 66 28 20 66 6c 61 67 73 20 26 20 28 4d 45 4d  if( flags & (MEM
160a4 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29  _Str|MEM_Blob) )
160a5 7b 0a 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b  {.    i64 value;
160a6 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  .    pMem->flags
160a7 20 7c 3d 20 4d 45 4d 5f 53 74 72 3b 0a 20 20 20   |= MEM_Str;.   
160a8 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65   if( sqlite3Vdbe
160a9 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70  ChangeEncoding(p
160aa 4d 65 6d 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  Mem, SQLITE_UTF8
160ab 29 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69  ).       || sqli
160ac 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72  te3VdbeMemNulTer
160ad 6d 69 6e 61 74 65 28 70 4d 65 6d 29 20 29 7b 0a  minate(pMem) ){.
160ae 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
160af 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
160b0 28 20 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20  ( pMem->z );.   
160b1 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 70   sqlite3Atoi64(p
160b2 4d 65 6d 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 3b  Mem->z, &value);
160b3 0a 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 75  .    return valu
160b4 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  e;.  }else{.    
160b5 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a  return 0;.  }.}.
160b6 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
160b7 65 20 62 65 73 74 20 72 65 70 72 65 73 65 6e 74  e best represent
160b8 61 74 69 6f 6e 20 6f 66 20 70 4d 65 6d 20 74 68  ation of pMem th
160b9 61 74 20 77 65 20 63 61 6e 20 67 65 74 20 69 6e  at we can get in
160ba 74 6f 20 61 0a 2a 2a 20 64 6f 75 62 6c 65 2e 20  to a.** double. 
160bb 20 49 66 20 70 4d 65 6d 20 69 73 20 61 6c 72 65   If pMem is alre
160bc 61 64 79 20 61 20 64 6f 75 62 6c 65 20 6f 72 20  ady a double or 
160bd 61 6e 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75  an integer, retu
160be 72 6e 20 69 74 73 0a 2a 2a 20 76 61 6c 75 65 2e  rn its.** value.
160bf 20 20 49 66 20 69 74 20 69 73 20 61 20 73 74 72    If it is a str
160c0 69 6e 67 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79  ing or blob, try
160c1 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74   to convert it t
160c2 6f 20 61 20 64 6f 75 62 6c 65 2e 0a 2a 2a 20 49  o a double..** I
160c3 66 20 69 74 20 69 73 20 61 20 4e 55 4c 4c 2c 20  f it is a NULL, 
160c4 72 65 74 75 72 6e 20 30 2e 30 2e 0a 2a 2f 0a 53  return 0.0..*/.S
160c5 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 64 6f  QLITE_PRIVATE do
160c6 75 62 6c 65 20 73 71 6c 69 74 65 33 56 64 62 65  uble sqlite3Vdbe
160c7 52 65 61 6c 56 61 6c 75 65 28 4d 65 6d 20 2a 70  RealValue(Mem *p
160c8 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20  Mem){.  assert( 
160c9 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73  pMem->db==0 || s
160ca 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
160cb 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65  d(pMem->db->mute
160cc 78 29 20 29 3b 0a 20 20 69 66 28 20 70 4d 65 6d  x) );.  if( pMem
160cd 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65  ->flags & MEM_Re
160ce 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  al ){.    return
160cf 20 70 4d 65 6d 2d 3e 72 3b 0a 20 20 7d 65 6c 73   pMem->r;.  }els
160d0 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67  e if( pMem->flag
160d1 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20  s & MEM_Int ){. 
160d2 20 20 20 72 65 74 75 72 6e 20 28 64 6f 75 62 6c     return (doubl
160d3 65 29 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 7d  e)pMem->u.i;.  }
160d4 65 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66  else if( pMem->f
160d5 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c  lags & (MEM_Str|
160d6 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20  MEM_Blob) ){.   
160d7 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d 20 30 2e   double val = 0.
160d8 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  0;.    pMem->fla
160d9 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 72 3b 0a 20  gs |= MEM_Str;. 
160da 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64     if( sqlite3Vd
160db 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67  beChangeEncoding
160dc 28 70 4d 65 6d 2c 20 53 51 4c 49 54 45 5f 55 54  (pMem, SQLITE_UT
160dd 46 38 29 0a 20 20 20 20 20 20 20 7c 7c 20 73 71  F8).       || sq
160de 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54  lite3VdbeMemNulT
160df 65 72 6d 69 6e 61 74 65 28 70 4d 65 6d 29 20 29  erminate(pMem) )
160e0 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
160e1 2e 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  .0;.    }.    as
160e2 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 20 29 3b  sert( pMem->z );
160e3 0a 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46  .    sqlite3AtoF
160e4 28 70 4d 65 6d 2d 3e 7a 2c 20 26 76 61 6c 29 3b  (pMem->z, &val);
160e5 0a 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b  .    return val;
160e6 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
160e7 74 75 72 6e 20 30 2e 30 3b 0a 20 20 7d 0a 7d 0a  turn 0.0;.  }.}.
160e8 0a 2f 2a 0a 2a 2a 20 54 68 65 20 4d 45 4d 20 73  ./*.** The MEM s
160e9 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 72 65  tructure is alre
160ea 61 64 79 20 61 20 4d 45 4d 5f 52 65 61 6c 2e 20  ady a MEM_Real. 
160eb 20 54 72 79 20 74 6f 20 61 6c 73 6f 20 6d 61 6b   Try to also mak
160ec 65 20 69 74 20 61 0a 2a 2a 20 4d 45 4d 5f 49 6e  e it a.** MEM_In
160ed 74 20 69 66 20 77 65 20 63 61 6e 2e 0a 2a 2f 0a  t if we can..*/.
160ee 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
160ef 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49  oid sqlite3VdbeI
160f0 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 4d  ntegerAffinity(M
160f1 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73  em *pMem){.  ass
160f2 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  ert( pMem->flags
160f3 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a 20   & MEM_Real );. 
160f4 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64   assert( pMem->d
160f5 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
160f6 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d  mutex_held(pMem-
160f7 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a  >db->mutex) );..
160f8 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 64 6f    pMem->u.i = do
160f9 75 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d 65 6d  ubleToInt64(pMem
160fa 2d 3e 72 29 3b 0a 20 20 69 66 28 20 70 4d 65 6d  ->r);.  if( pMem
160fb 2d 3e 72 3d 3d 28 64 6f 75 62 6c 65 29 70 4d 65  ->r==(double)pMe
160fc 6d 2d 3e 75 2e 69 20 29 7b 0a 20 20 20 20 70 4d  m->u.i ){.    pM
160fd 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d  em->flags |= MEM
160fe 5f 49 6e 74 3b 0a 20 20 7d 0a 7d 0a 0a 73 74 61  _Int;.  }.}..sta
160ff 74 69 63 20 76 6f 69 64 20 73 65 74 54 79 70 65  tic void setType
16100 46 6c 61 67 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20  Flag(Mem *pMem, 
16101 69 6e 74 20 66 29 7b 0a 20 20 4d 65 6d 53 65 74  int f){.  MemSet
16102 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 66  TypeFlag(pMem, f
16103 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  );.}../*.** Conv
16104 65 72 74 20 70 4d 65 6d 20 74 6f 20 74 79 70 65  ert pMem to type
16105 20 69 6e 74 65 67 65 72 2e 20 20 49 6e 76 61 6c   integer.  Inval
16106 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20  idate any prior 
16107 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e  representations.
16108 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
16109 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
1610a 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28  beMemIntegerify(
1610b 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73  Mem *pMem){.  as
1610c 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d  sert( pMem->db==
1610d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
1610e 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62  ex_held(pMem->db
1610f 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d  ->mutex) );.  pM
16110 65 6d 2d 3e 75 2e 69 20 3d 20 73 71 6c 69 74 65  em->u.i = sqlite
16111 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 4d  3VdbeIntValue(pM
16112 65 6d 29 3b 0a 20 20 73 65 74 54 79 70 65 46 6c  em);.  setTypeFl
16113 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 49 6e 74  ag(pMem, MEM_Int
16114 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
16115 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
16116 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 73 6f 20  Convert pMem so 
16117 74 68 61 74 20 69 74 20 69 73 20 6f 66 20 74 79  that it is of ty
16118 70 65 20 4d 45 4d 5f 52 65 61 6c 2e 0a 2a 2a 20  pe MEM_Real..** 
16119 49 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70  Invalidate any p
1611a 72 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74  rior representat
1611b 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ions..*/.SQLITE_
1611c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1611d 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66  te3VdbeMemRealif
1611e 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20  y(Mem *pMem){.  
1611f 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62  assert( pMem->db
16120 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d  ==0 || sqlite3_m
16121 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e  utex_held(pMem->
16122 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
16123 70 4d 65 6d 2d 3e 72 20 3d 20 73 71 6c 69 74 65  pMem->r = sqlite
16124 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70  3VdbeRealValue(p
16125 4d 65 6d 29 3b 0a 20 20 73 65 74 54 79 70 65 46  Mem);.  setTypeF
16126 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52 65  lag(pMem, MEM_Re
16127 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  al);.  return SQ
16128 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
16129 2a 20 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 73  * Convert pMem s
1612a 6f 20 74 68 61 74 20 69 74 20 68 61 73 20 74 79  o that it has ty
1612b 70 65 73 20 4d 45 4d 5f 52 65 61 6c 20 6f 72 20  pes MEM_Real or 
1612c 4d 45 4d 5f 49 6e 74 20 6f 72 20 62 6f 74 68 2e  MEM_Int or both.
1612d 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61  .** Invalidate a
1612e 6e 79 20 70 72 69 6f 72 20 72 65 70 72 65 73 65  ny prior represe
1612f 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c  ntations..*/.SQL
16130 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
16131 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75  sqlite3VdbeMemNu
16132 6d 65 72 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d  merify(Mem *pMem
16133 29 7b 0a 20 20 64 6f 75 62 6c 65 20 72 31 2c 20  ){.  double r1, 
16134 72 32 3b 0a 20 20 69 36 34 20 69 3b 0a 20 20 61  r2;.  i64 i;.  a
16135 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c  ssert( (pMem->fl
16136 61 67 73 20 26 20 28 4d 45 4d 5f 49 6e 74 7c 4d  ags & (MEM_Int|M
16137 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 4e 75 6c 6c  EM_Real|MEM_Null
16138 29 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ))==0 );.  asser
16139 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  t( (pMem->flags 
1613a 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f  & (MEM_Blob|MEM_
1613b 53 74 72 29 29 21 3d 30 20 29 3b 0a 20 20 61 73  Str))!=0 );.  as
1613c 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d  sert( pMem->db==
1613d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
1613e 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62  ex_held(pMem->db
1613f 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 31  ->mutex) );.  r1
16140 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65   = sqlite3VdbeRe
16141 61 6c 56 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20  alValue(pMem);. 
16142 20 69 20 3d 20 64 6f 75 62 6c 65 54 6f 49 6e 74   i = doubleToInt
16143 36 34 28 72 31 29 3b 0a 20 20 72 32 20 3d 20 28  64(r1);.  r2 = (
16144 64 6f 75 62 6c 65 29 69 3b 0a 20 20 69 66 28 20  double)i;.  if( 
16145 72 31 3d 3d 72 32 20 29 7b 0a 20 20 20 20 73 71  r1==r2 ){.    sq
16146 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65  lite3VdbeMemInte
16147 67 65 72 69 66 79 28 70 4d 65 6d 29 3b 0a 20 20  gerify(pMem);.  
16148 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d  }else{.    pMem-
16149 3e 72 20 3d 20 72 31 3b 0a 20 20 20 20 73 65 74  >r = r1;.    set
1614a 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d  TypeFlag(pMem, M
1614b 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 7d 0a 20 20  EM_Real);.  }.  
1614c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1614d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74  ;.}../*.** Delet
1614e 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76  e any previous v
1614f 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65  alue and set the
16150 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e   value stored in
16151 20 2a 70 4d 65 6d 20 74 6f 20 4e 55 4c 4c 2e 0a   *pMem to NULL..
16152 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
16153 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
16154 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 4d 65 6d  beMemSetNull(Mem
16155 20 2a 70 4d 65 6d 29 7b 0a 20 20 73 65 74 54 79   *pMem){.  setTy
16156 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d  peFlag(pMem, MEM
16157 5f 4e 75 6c 6c 29 3b 0a 20 20 70 4d 65 6d 2d 3e  _Null);.  pMem->
16158 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55  type = SQLITE_NU
16159 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c  LL;.}../*.** Del
1615a 65 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73  ete any previous
1615b 20 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74   value and set t
1615c 68 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20 61  he value to be a
1615d 20 42 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68 0a   BLOB of length.
1615e 2a 2a 20 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ** n containing 
1615f 61 6c 6c 20 7a 65 72 6f 73 2e 0a 2a 2f 0a 53 51  all zeros..*/.SQ
16160 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
16161 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
16162 53 65 74 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 20  SetZeroBlob(Mem 
16163 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 29 7b 0a 20  *pMem, int n){. 
16164 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
16165 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20  elease(pMem);.  
16166 73 65 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d  setTypeFlag(pMem
16167 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 70  , MEM_Blob);.  p
16168 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
16169 5f 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 3b 0a  _Blob|MEM_Zero;.
1616a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53    pMem->type = S
1616b 51 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 70 4d  QLITE_BLOB;.  pM
1616c 65 6d 2d 3e 6e 20 3d 20 30 3b 0a 20 20 69 66 28  em->n = 0;.  if(
1616d 20 6e 3c 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20   n<0 ) n = 0;.  
1616e 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 6e 3b 0a 20  pMem->u.i = n;. 
1616f 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c   pMem->enc = SQL
16170 49 54 45 5f 55 54 46 38 3b 0a 7d 0a 0a 2f 2a 0a  ITE_UTF8;.}../*.
16171 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72  ** Delete any pr
16172 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64  evious value and
16173 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 73   set the value s
16174 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74  tored in *pMem t
16175 6f 20 76 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65  o val,.** manife
16176 73 74 20 74 79 70 65 20 49 4e 54 45 47 45 52 2e  st type INTEGER.
16177 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
16178 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
16179 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 4d  dbeMemSetInt64(M
1617a 65 6d 20 2a 70 4d 65 6d 2c 20 69 36 34 20 76 61  em *pMem, i64 va
1617b 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  l){.  sqlite3Vdb
1617c 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d  eMemRelease(pMem
1617d 29 3b 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d  );.  pMem->u.i =
1617e 20 76 61 6c 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c   val;.  pMem->fl
1617f 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20  ags = MEM_Int;. 
16180 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
16181 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 7d 0a  LITE_INTEGER;.}.
16182 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e  ./*.** Delete an
16183 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65  y previous value
16184 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c   and set the val
16185 75 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d  ue stored in *pM
16186 65 6d 20 74 6f 20 76 61 6c 2c 0a 2a 2a 20 6d 61  em to val,.** ma
16187 6e 69 66 65 73 74 20 74 79 70 65 20 52 45 41 4c  nifest type REAL
16188 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
16189 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1618a 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65  VdbeMemSetDouble
1618b 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 64 6f 75 62  (Mem *pMem, doub
1618c 6c 65 20 76 61 6c 29 7b 0a 20 20 69 66 28 20 73  le val){.  if( s
1618d 71 6c 69 74 65 33 49 73 4e 61 4e 28 76 61 6c 29  qlite3IsNaN(val)
1618e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1618f 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4d  dbeMemSetNull(pM
16190 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  em);.  }else{.  
16191 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
16192 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20  Release(pMem);. 
16193 20 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 76 61 6c     pMem->r = val
16194 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  ;.    pMem->flag
16195 73 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20  s = MEM_Real;.  
16196 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53    pMem->type = S
16197 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d  QLITE_FLOAT;.  }
16198 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
16199 20 74 72 75 65 20 69 66 20 74 68 65 20 4d 65 6d   true if the Mem
1619a 20 6f 62 6a 65 63 74 20 63 6f 6e 74 61 69 6e 73   object contains
1619b 20 61 20 54 45 58 54 20 6f 72 20 42 4c 4f 42 20   a TEXT or BLOB 
1619c 74 68 61 74 20 69 73 0a 2a 2a 20 74 6f 6f 20 6c  that is.** too l
1619d 61 72 67 65 20 2d 20 77 68 6f 73 65 20 73 69 7a  arge - whose siz
1619e 65 20 65 78 63 65 65 64 73 20 53 51 4c 49 54 45  e exceeds SQLITE
1619f 5f 4d 41 58 5f 4c 45 4e 47 54 48 2e 0a 2a 2f 0a  _MAX_LENGTH..*/.
161a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
161a1 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
161a2 6d 54 6f 6f 42 69 67 28 4d 65 6d 20 2a 70 29 7b  mTooBig(Mem *p){
161a3 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62  .  assert( p->db
161a4 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e  !=0 );.  if( p->
161a5 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72  flags & (MEM_Str
161a6 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20  |MEM_Blob) ){.  
161a7 20 20 69 6e 74 20 6e 20 3d 20 70 2d 3e 6e 3b 0a    int n = p->n;.
161a8 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73      if( p->flags
161a9 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20   & MEM_Zero ){. 
161aa 20 20 20 20 20 6e 20 2b 3d 20 70 2d 3e 75 2e 69       n += p->u.i
161ab 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
161ac 72 6e 20 6e 3e 70 2d 3e 64 62 2d 3e 61 4c 69 6d  rn n>p->db->aLim
161ad 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
161ae 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a 20 20 72  LENGTH];.  }.  r
161af 65 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a 2f 2a 0a  eturn 0; .}../*.
161b0 2a 2a 20 53 69 7a 65 20 6f 66 20 73 74 72 75 63  ** Size of struc
161b1 74 20 4d 65 6d 20 6e 6f 74 20 69 6e 63 6c 75 64  t Mem not includ
161b2 69 6e 67 20 74 68 65 20 4d 65 6d 2e 7a 4d 61 6c  ing the Mem.zMal
161b3 6c 6f 63 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23  loc member..*/.#
161b4 64 65 66 69 6e 65 20 4d 45 4d 43 45 4c 4c 53 49  define MEMCELLSI
161b5 5a 45 20 28 73 69 7a 65 5f 74 29 28 26 28 28 28  ZE (size_t)(&(((
161b6 4d 65 6d 20 2a 29 30 29 2d 3e 7a 4d 61 6c 6c 6f  Mem *)0)->zMallo
161b7 63 29 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20  c))../*.** Make 
161b8 61 6e 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20  an shallow copy 
161b9 6f 66 20 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54  of pFrom into pT
161ba 6f 2e 20 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e  o.  Prior conten
161bb 74 73 20 6f 66 0a 2a 2a 20 70 54 6f 20 61 72 65  ts of.** pTo are
161bc 20 66 72 65 65 64 2e 20 20 54 68 65 20 70 46 72   freed.  The pFr
161bd 6f 6d 2d 3e 7a 20 66 69 65 6c 64 20 69 73 20 6e  om->z field is n
161be 6f 74 20 64 75 70 6c 69 63 61 74 65 64 2e 20 20  ot duplicated.  
161bf 49 66 0a 2a 2a 20 70 46 72 6f 6d 2d 3e 7a 20 69  If.** pFrom->z i
161c0 73 20 75 73 65 64 2c 20 74 68 65 6e 20 70 54 6f  s used, then pTo
161c1 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68  ->z points to th
161c2 65 20 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20  e same thing as 
161c3 70 46 72 6f 6d 2d 3e 7a 0a 2a 2a 20 61 6e 64 20  pFrom->z.** and 
161c4 66 6c 61 67 73 20 67 65 74 73 20 73 72 63 54 79  flags gets srcTy
161c5 70 65 20 28 65 69 74 68 65 72 20 4d 45 4d 5f 45  pe (either MEM_E
161c6 70 68 65 6d 20 6f 72 20 4d 45 4d 5f 53 74 61 74  phem or MEM_Stat
161c7 69 63 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ic)..*/.SQLITE_P
161c8 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
161c9 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f  te3VdbeMemShallo
161ca 77 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c 20  wCopy(Mem *pTo, 
161cb 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f 6d  const Mem *pFrom
161cc 2c 20 69 6e 74 20 73 72 63 54 79 70 65 29 7b 0a  , int srcType){.
161cd 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
161ce 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28  ReleaseExternal(
161cf 70 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70  pTo);.  memcpy(p
161d0 54 6f 2c 20 70 46 72 6f 6d 2c 20 4d 45 4d 43 45  To, pFrom, MEMCE
161d1 4c 4c 53 49 5a 45 29 3b 0a 20 20 70 54 6f 2d 3e  LLSIZE);.  pTo->
161d2 78 44 65 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20  xDel = 0;.  if( 
161d3 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45  (pFrom->flags&ME
161d4 4d 5f 44 79 6e 29 21 3d 30 20 7c 7c 20 70 46 72  M_Dyn)!=0 || pFr
161d5 6f 6d 2d 3e 7a 3d 3d 70 46 72 6f 6d 2d 3e 7a 4d  om->z==pFrom->zM
161d6 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 70 54 6f  alloc ){.    pTo
161d7 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d  ->flags &= ~(MEM
161d8 5f 44 79 6e 7c 4d 45 4d 5f 53 74 61 74 69 63 7c  _Dyn|MEM_Static|
161d9 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20  MEM_Ephem);.    
161da 61 73 73 65 72 74 28 20 73 72 63 54 79 70 65 3d  assert( srcType=
161db 3d 4d 45 4d 5f 45 70 68 65 6d 20 7c 7c 20 73 72  =MEM_Ephem || sr
161dc 63 54 79 70 65 3d 3d 4d 45 4d 5f 53 74 61 74 69  cType==MEM_Stati
161dd 63 20 29 3b 0a 20 20 20 20 70 54 6f 2d 3e 66 6c  c );.    pTo->fl
161de 61 67 73 20 7c 3d 20 73 72 63 54 79 70 65 3b 0a  ags |= srcType;.
161df 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b    }.}../*.** Mak
161e0 65 20 61 20 66 75 6c 6c 20 63 6f 70 79 20 6f 66  e a full copy of
161e1 20 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e   pFrom into pTo.
161e2 20 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e 74 73    Prior contents
161e3 20 6f 66 20 70 54 6f 20 61 72 65 0a 2a 2a 20 66   of pTo are.** f
161e4 72 65 65 64 20 62 65 66 6f 72 65 20 74 68 65 20  reed before the 
161e5 63 6f 70 79 20 69 73 20 6d 61 64 65 2e 0a 2a 2f  copy is made..*/
161e6 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
161e7 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
161e8 65 6d 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c  emCopy(Mem *pTo,
161e9 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f   const Mem *pFro
161ea 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  m){.  int rc = S
161eb 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c  QLITE_OK;..  sql
161ec 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
161ed 73 65 45 78 74 65 72 6e 61 6c 28 70 54 6f 29 3b  seExternal(pTo);
161ee 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70  .  memcpy(pTo, p
161ef 46 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a  From, MEMCELLSIZ
161f0 45 29 3b 0a 20 20 70 54 6f 2d 3e 66 6c 61 67 73  E);.  pTo->flags
161f1 20 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 0a 20   &= ~MEM_Dyn;.. 
161f2 20 69 66 28 20 70 54 6f 2d 3e 66 6c 61 67 73 26   if( pTo->flags&
161f3 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f  (MEM_Str|MEM_Blo
161f4 62 29 20 29 7b 0a 20 20 20 20 69 66 28 20 30 3d  b) ){.    if( 0=
161f5 3d 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d  =(pFrom->flags&M
161f6 45 4d 5f 53 74 61 74 69 63 29 20 29 7b 0a 20 20  EM_Static) ){.  
161f7 20 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c      pTo->flags |
161f8 3d 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20  = MEM_Ephem;.   
161f9 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
161fa 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61  dbeMemMakeWritea
161fb 62 6c 65 28 70 54 6f 29 3b 0a 20 20 20 20 7d 0a  ble(pTo);.    }.
161fc 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
161fd 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73  ;.}../*.** Trans
161fe 66 65 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  fer the contents
161ff 20 6f 66 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f   of pFrom to pTo
16200 2e 20 41 6e 79 20 65 78 69 73 74 69 6e 67 20 76  . Any existing v
16201 61 6c 75 65 20 69 6e 20 70 54 6f 20 69 73 0a 2a  alue in pTo is.*
16202 2a 20 66 72 65 65 64 2e 20 49 66 20 70 46 72 6f  * freed. If pFro
16203 6d 20 63 6f 6e 74 61 69 6e 73 20 65 70 68 65 6d  m contains ephem
16204 65 72 61 6c 20 64 61 74 61 2c 20 61 20 63 6f 70  eral data, a cop
16205 79 20 69 73 20 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a  y is made..**.**
16206 20 70 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 73 20   pFrom contains 
16207 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 77 68 65 6e  an SQL NULL when
16208 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
16209 74 75 72 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  turns..*/.SQLITE
1620a 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1620b 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65  lite3VdbeMemMove
1620c 28 4d 65 6d 20 2a 70 54 6f 2c 20 4d 65 6d 20 2a  (Mem *pTo, Mem *
1620d 70 46 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74  pFrom){.  assert
1620e 28 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c  ( pFrom->db==0 |
1620f 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
16210 68 65 6c 64 28 70 46 72 6f 6d 2d 3e 64 62 2d 3e  held(pFrom->db->
16211 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
16212 72 74 28 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c  rt( pTo->db==0 |
16213 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
16214 68 65 6c 64 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75  held(pTo->db->mu
16215 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
16216 28 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c  ( pFrom->db==0 |
16217 7c 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20  | pTo->db==0 || 
16218 70 46 72 6f 6d 2d 3e 64 62 3d 3d 70 54 6f 2d 3e  pFrom->db==pTo->
16219 64 62 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33  db );..  sqlite3
1621a 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
1621b 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54  To);.  memcpy(pT
1621c 6f 2c 20 70 46 72 6f 6d 2c 20 73 69 7a 65 6f 66  o, pFrom, sizeof
1621d 28 4d 65 6d 29 29 3b 0a 20 20 70 46 72 6f 6d 2d  (Mem));.  pFrom-
1621e 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c  >flags = MEM_Nul
1621f 6c 3b 0a 20 20 70 46 72 6f 6d 2d 3e 78 44 65 6c  l;.  pFrom->xDel
16220 20 3d 20 30 3b 0a 20 20 70 46 72 6f 6d 2d 3e 7a   = 0;.  pFrom->z
16221 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 7d 0a 0a 2f  Malloc = 0;.}../
16222 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
16223 76 61 6c 75 65 20 6f 66 20 61 20 4d 65 6d 20 74  value of a Mem t
16224 6f 20 62 65 20 61 20 73 74 72 69 6e 67 20 6f 72  o be a string or
16225 20 61 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54   a BLOB..**.** T
16226 68 65 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65  he memory manage
16227 6d 65 6e 74 20 73 74 72 61 74 65 67 79 20 64 65  ment strategy de
16228 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c  pends on the val
16229 75 65 20 6f 66 20 74 68 65 20 78 44 65 6c 0a 2a  ue of the xDel.*
1622a 2a 20 70 61 72 61 6d 65 74 65 72 2e 20 49 66 20  * parameter. If 
1622b 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 64  the value passed
1622c 20 69 73 20 53 51 4c 49 54 45 5f 54 52 41 4e 53   is SQLITE_TRANS
1622d 49 45 4e 54 2c 20 74 68 65 6e 20 74 68 65 20 0a  IENT, then the .
1622e 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 63 6f 70  ** string is cop
1622f 69 65 64 20 69 6e 74 6f 20 61 20 28 70 6f 73 73  ied into a (poss
16230 69 62 6c 79 20 65 78 69 73 74 69 6e 67 29 20 62  ibly existing) b
16231 75 66 66 65 72 20 6d 61 6e 61 67 65 64 20 62 79  uffer managed by
16232 20 74 68 65 20 0a 2a 2a 20 4d 65 6d 20 73 74 72   the .** Mem str
16233 75 63 74 75 72 65 2e 20 4f 74 68 65 72 77 69 73  ucture. Otherwis
16234 65 2c 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20  e, any existing 
16235 62 75 66 66 65 72 20 69 73 20 66 72 65 65 64 20  buffer is freed 
16236 61 6e 64 20 74 68 65 0a 2a 2a 20 70 6f 69 6e 74  and the.** point
16237 65 72 20 63 6f 70 69 65 64 2e 0a 2a 2f 0a 53 51  er copied..*/.SQ
16238 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
16239 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1623a 65 74 53 74 72 28 0a 20 20 4d 65 6d 20 2a 70 4d  etStr(.  Mem *pM
1623b 65 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  em,          /* 
1623c 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 73  Memory cell to s
1623d 65 74 20 74 6f 20 73 74 72 69 6e 67 20 76 61 6c  et to string val
1623e 75 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ue */.  const ch
1623f 61 72 20 2a 7a 2c 20 20 20 20 20 20 2f 2a 20 53  ar *z,      /* S
16240 74 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 2a 2f  tring pointer */
16241 0a 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20  .  int n,       
16242 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20         /* Bytes 
16243 69 6e 20 73 74 72 69 6e 67 2c 20 6f 72 20 6e 65  in string, or ne
16244 67 61 74 69 76 65 20 2a 2f 0a 20 20 75 38 20 65  gative */.  u8 e
16245 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  nc,             
16246 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 6f 66 20 7a  /* Encoding of z
16247 2e 20 20 30 20 66 6f 72 20 42 4c 4f 42 73 20 2a  .  0 for BLOBs *
16248 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29  /.  void (*xDel)
16249 28 76 6f 69 64 2a 29 20 2f 2a 20 44 65 73 74 72  (void*) /* Destr
1624a 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a  uctor function *
1624b 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65  /.){.  int nByte
1624c 20 3d 20 6e 3b 20 20 20 20 20 20 2f 2a 20 4e 65   = n;      /* Ne
1624d 77 20 76 61 6c 75 65 20 66 6f 72 20 70 4d 65 6d  w value for pMem
1624e 2d 3e 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 69  ->n */.  int iLi
1624f 6d 69 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  mit;         /* 
16250 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20  Maximum allowed 
16251 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 73  string or blob s
16252 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61  ize */.  int fla
16253 67 73 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20  gs = 0;      /* 
16254 4e 65 77 20 76 61 6c 75 65 20 66 6f 72 20 70 4d  New value for pM
16255 65 6d 2d 3e 66 6c 61 67 73 20 2a 2f 0a 0a 20 20  em->flags */..  
16256 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62  assert( pMem->db
16257 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d  ==0 || sqlite3_m
16258 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e  utex_held(pMem->
16259 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  db->mutex) );.. 
1625a 20 2f 2a 20 49 66 20 7a 20 69 73 20 61 20 4e 55   /* If z is a NU
1625b 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 73 65 74 20  LL pointer, set 
1625c 70 4d 65 6d 20 74 6f 20 63 6f 6e 74 61 69 6e 20  pMem to contain 
1625d 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 2a 2f 0a  an SQL NULL. */.
1625e 20 20 69 66 28 20 21 7a 20 29 7b 0a 20 20 20 20    if( !z ){.    
1625f 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
16260 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20 20 20  tNull(pMem);.   
16261 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
16262 4b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4d  K;.  }..  if( pM
16263 65 6d 2d 3e 64 62 20 29 7b 0a 20 20 20 20 69 4c  em->db ){.    iL
16264 69 6d 69 74 20 3d 20 70 4d 65 6d 2d 3e 64 62 2d  imit = pMem->db-
16265 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
16266 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20  IMIT_LENGTH];.  
16267 7d 65 6c 73 65 7b 0a 20 20 20 20 69 4c 69 6d 69  }else{.    iLimi
16268 74 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c  t = SQLITE_MAX_L
16269 45 4e 47 54 48 3b 0a 20 20 7d 0a 20 20 66 6c 61  ENGTH;.  }.  fla
1626a 67 73 20 3d 20 28 65 6e 63 3d 3d 30 3f 4d 45 4d  gs = (enc==0?MEM
1626b 5f 42 6c 6f 62 3a 4d 45 4d 5f 53 74 72 29 3b 0a  _Blob:MEM_Str);.
1626c 20 20 69 66 28 20 6e 42 79 74 65 3c 30 20 29 7b    if( nByte<0 ){
1626d 0a 20 20 20 20 61 73 73 65 72 74 28 20 65 6e 63  .    assert( enc
1626e 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 65  !=0 );.    if( e
1626f 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  nc==SQLITE_UTF8 
16270 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6e 42 79  ){.      for(nBy
16271 74 65 3d 30 3b 20 6e 42 79 74 65 3c 3d 69 4c 69  te=0; nByte<=iLi
16272 6d 69 74 20 26 26 20 7a 5b 6e 42 79 74 65 5d 3b  mit && z[nByte];
16273 20 6e 42 79 74 65 2b 2b 29 7b 7d 0a 20 20 20 20   nByte++){}.    
16274 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 6f 72  }else{.      for
16275 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74 65 3c  (nByte=0; nByte<
16276 3d 69 4c 69 6d 69 74 20 26 26 20 28 7a 5b 6e 42  =iLimit && (z[nB
16277 79 74 65 5d 20 7c 20 7a 5b 6e 42 79 74 65 2b 31  yte] | z[nByte+1
16278 5d 29 3b 20 6e 42 79 74 65 2b 3d 32 29 7b 7d 0a  ]); nByte+=2){}.
16279 20 20 20 20 7d 0a 20 20 20 20 66 6c 61 67 73 20      }.    flags 
1627a 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 7d  |= MEM_Term;.  }
1627b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f  ..  /* The follo
1627c 77 69 6e 67 20 62 6c 6f 63 6b 20 73 65 74 73 20  wing block sets 
1627d 74 68 65 20 6e 65 77 20 76 61 6c 75 65 73 20 6f  the new values o
1627e 66 20 4d 65 6d 2e 7a 20 61 6e 64 20 4d 65 6d 2e  f Mem.z and Mem.
1627f 78 44 65 6c 2e 20 49 74 0a 20 20 2a 2a 20 61 6c  xDel. It.  ** al
16280 73 6f 20 73 65 74 73 20 61 20 66 6c 61 67 20 69  so sets a flag i
16281 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  n local variable
16282 20 22 66 6c 61 67 73 22 20 74 6f 20 69 6e 64 69   "flags" to indi
16283 63 61 74 65 20 74 68 65 20 6d 65 6d 6f 72 79 0a  cate the memory.
16284 20 20 2a 2a 20 6d 61 6e 61 67 65 6d 65 6e 74 20    ** management 
16285 28 6f 6e 65 20 6f 66 20 4d 45 4d 5f 44 79 6e 20  (one of MEM_Dyn 
16286 6f 72 20 4d 45 4d 5f 53 74 61 74 69 63 29 2e 0a  or MEM_Static)..
16287 20 20 2a 2f 0a 20 20 69 66 28 20 78 44 65 6c 3d    */.  if( xDel=
16288 3d 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e  =SQLITE_TRANSIEN
16289 54 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 6c  T ){.    int nAl
1628a 6c 6f 63 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20  loc = nByte;.   
1628b 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 54   if( flags&MEM_T
1628c 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 6e 41 6c  erm ){.      nAl
1628d 6c 6f 63 20 2b 3d 20 28 65 6e 63 3d 3d 53 51 4c  loc += (enc==SQL
1628e 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 3b 0a 20  ITE_UTF8?1:2);. 
1628f 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 42 79     }.    if( nBy
16290 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20  te>iLimit ){.   
16291 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
16292 5f 54 4f 4f 42 49 47 3b 0a 20 20 20 20 7d 0a 20  _TOOBIG;.    }. 
16293 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64     if( sqlite3Vd
16294 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20  beMemGrow(pMem, 
16295 6e 41 6c 6c 6f 63 2c 20 30 29 20 29 7b 0a 20 20  nAlloc, 0) ){.  
16296 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
16297 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20  E_NOMEM;.    }. 
16298 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2d 3e     memcpy(pMem->
16299 7a 2c 20 7a 2c 20 6e 41 6c 6c 6f 63 29 3b 0a 20  z, z, nAlloc);. 
1629a 20 7d 65 6c 73 65 20 69 66 28 20 78 44 65 6c 3d   }else if( xDel=
1629b 3d 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20  =SQLITE_DYNAMIC 
1629c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1629d 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65  beMemRelease(pMe
1629e 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d  m);.    pMem->zM
1629f 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e 7a 20  alloc = pMem->z 
162a0 3d 20 28 63 68 61 72 20 2a 29 7a 3b 0a 20 20 20  = (char *)z;.   
162a1 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b   pMem->xDel = 0;
162a2 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
162a3 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
162a4 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70  ase(pMem);.    p
162a5 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a  Mem->z = (char *
162a6 29 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 78 44  )z;.    pMem->xD
162a7 65 6c 20 3d 20 78 44 65 6c 3b 0a 20 20 20 20 66  el = xDel;.    f
162a8 6c 61 67 73 20 7c 3d 20 28 28 78 44 65 6c 3d 3d  lags |= ((xDel==
162a9 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3f 4d  SQLITE_STATIC)?M
162aa 45 4d 5f 53 74 61 74 69 63 3a 4d 45 4d 5f 44 79  EM_Static:MEM_Dy
162ab 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42  n);.  }.  if( nB
162ac 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20  yte>iLimit ){.  
162ad 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
162ae 54 4f 4f 42 49 47 3b 0a 20 20 7d 0a 0a 20 20 70  TOOBIG;.  }..  p
162af 4d 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74 65 3b 0a  Mem->n = nByte;.
162b0 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
162b1 66 6c 61 67 73 3b 0a 20 20 70 4d 65 6d 2d 3e 65  flags;.  pMem->e
162b2 6e 63 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53  nc = (enc==0 ? S
162b3 51 4c 49 54 45 5f 55 54 46 38 20 3a 20 65 6e 63  QLITE_UTF8 : enc
162b4 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20  );.  pMem->type 
162b5 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 51 4c 49  = (enc==0 ? SQLI
162b6 54 45 5f 42 4c 4f 42 20 3a 20 53 51 4c 49 54 45  TE_BLOB : SQLITE
162b7 5f 54 45 58 54 29 3b 0a 0a 23 69 66 6e 64 65 66  _TEXT);..#ifndef
162b8 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
162b9 31 36 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65  16.  if( pMem->e
162ba 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  nc!=SQLITE_UTF8 
162bb 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  && sqlite3VdbeMe
162bc 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d 65 6d 29  mHandleBom(pMem)
162bd 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
162be 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
162bf 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72  .#endif..  retur
162c0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
162c1 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68  /*.** Compare th
162c2 65 20 76 61 6c 75 65 73 20 63 6f 6e 74 61 69 6e  e values contain
162c3 65 64 20 62 79 20 74 68 65 20 74 77 6f 20 6d 65  ed by the two me
162c4 6d 6f 72 79 20 63 65 6c 6c 73 2c 20 72 65 74 75  mory cells, retu
162c5 72 6e 69 6e 67 0a 2a 2a 20 6e 65 67 61 74 69 76  rning.** negativ
162c6 65 2c 20 7a 65 72 6f 20 6f 72 20 70 6f 73 69 74  e, zero or posit
162c7 69 76 65 20 69 66 20 70 4d 65 6d 31 20 69 73 20  ive if pMem1 is 
162c8 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c  less than, equal
162c9 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 72 0a   to, or greater.
162ca 2a 2a 20 74 68 61 6e 20 70 4d 65 6d 32 2e 20 53  ** than pMem2. S
162cb 6f 72 74 69 6e 67 20 6f 72 64 65 72 20 69 73 20  orting order is 
162cc 4e 55 4c 4c 27 73 20 66 69 72 73 74 2c 20 66 6f  NULL's first, fo
162cd 6c 6c 6f 77 65 64 20 62 79 20 6e 75 6d 62 65 72  llowed by number
162ce 73 20 28 69 6e 74 65 67 65 72 73 0a 2a 2a 20 61  s (integers.** a
162cf 6e 64 20 72 65 61 6c 73 29 20 73 6f 72 74 65 64  nd reals) sorted
162d0 20 6e 75 6d 65 72 69 63 61 6c 6c 79 2c 20 66 6f   numerically, fo
162d1 6c 6c 6f 77 65 64 20 62 79 20 74 65 78 74 20 6f  llowed by text o
162d2 72 64 65 72 65 64 20 62 79 20 74 68 65 20 63 6f  rdered by the co
162d3 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65  llating.** seque
162d4 6e 63 65 20 70 43 6f 6c 6c 20 61 6e 64 20 66 69  nce pColl and fi
162d5 6e 61 6c 6c 79 20 62 6c 6f 62 27 73 20 6f 72 64  nally blob's ord
162d6 65 72 65 64 20 62 79 20 6d 65 6d 63 6d 70 28 29  ered by memcmp()
162d7 2e 0a 2a 2a 0a 2a 2a 20 54 77 6f 20 4e 55 4c 4c  ..**.** Two NULL
162d8 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f 6e 73   values are cons
162d9 69 64 65 72 65 64 20 65 71 75 61 6c 20 62 79 20  idered equal by 
162da 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  this function..*
162db 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
162dc 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 43   int sqlite3MemC
162dd 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 4d 65 6d  ompare(const Mem
162de 20 2a 70 4d 65 6d 31 2c 20 63 6f 6e 73 74 20 4d   *pMem1, const M
162df 65 6d 20 2a 70 4d 65 6d 32 2c 20 63 6f 6e 73 74  em *pMem2, const
162e0 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 29   CollSeq *pColl)
162e1 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e  {.  int rc;.  in
162e2 74 20 66 31 2c 20 66 32 3b 0a 20 20 69 6e 74 20  t f1, f2;.  int 
162e3 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 3b 0a  combined_flags;.
162e4 0a 20 20 2f 2a 20 49 6e 74 65 72 63 68 61 6e 67  .  /* Interchang
162e5 65 20 70 4d 65 6d 31 20 61 6e 64 20 70 4d 65 6d  e pMem1 and pMem
162e6 32 20 69 66 20 74 68 65 20 63 6f 6c 6c 61 74 69  2 if the collati
162e7 6e 67 20 73 65 71 75 65 6e 63 65 20 73 70 65 63  ng sequence spec
162e8 69 66 69 65 73 0a 20 20 2a 2a 20 44 45 53 43 20  ifies.  ** DESC 
162e9 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 66 31  order..  */.  f1
162ea 20 3d 20 70 4d 65 6d 31 2d 3e 66 6c 61 67 73 3b   = pMem1->flags;
162eb 0a 20 20 66 32 20 3d 20 70 4d 65 6d 32 2d 3e 66  .  f2 = pMem2->f
162ec 6c 61 67 73 3b 0a 20 20 63 6f 6d 62 69 6e 65 64  lags;.  combined
162ed 5f 66 6c 61 67 73 20 3d 20 66 31 7c 66 32 3b 0a  _flags = f1|f2;.
162ee 20 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 61   .  /* If one va
162ef 6c 75 65 20 69 73 20 4e 55 4c 4c 2c 20 69 74 20  lue is NULL, it 
162f0 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65  is less than the
162f1 20 6f 74 68 65 72 2e 20 49 66 20 62 6f 74 68 20   other. If both 
162f2 76 61 6c 75 65 73 0a 20 20 2a 2a 20 61 72 65 20  values.  ** are 
162f3 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e 0a  NULL, return 0..
162f4 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69    */.  if( combi
162f5 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d 5f 4e 75  ned_flags&MEM_Nu
162f6 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ll ){.    return
162f7 20 28 66 32 26 4d 45 4d 5f 4e 75 6c 6c 29 20 2d   (f2&MEM_Null) -
162f8 20 28 66 31 26 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a   (f1&MEM_Null);.
162f9 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65    }..  /* If one
162fa 20 76 61 6c 75 65 20 69 73 20 61 20 6e 75 6d 62   value is a numb
162fb 65 72 20 61 6e 64 20 74 68 65 20 6f 74 68 65 72  er and the other
162fc 20 69 73 20 6e 6f 74 2c 20 74 68 65 20 6e 75 6d   is not, the num
162fd 62 65 72 20 69 73 20 6c 65 73 73 2e 0a 20 20 2a  ber is less..  *
162fe 2a 20 49 66 20 62 6f 74 68 20 61 72 65 20 6e 75  * If both are nu
162ff 6d 62 65 72 73 2c 20 63 6f 6d 70 61 72 65 20 61  mbers, compare a
16300 73 20 72 65 61 6c 73 20 69 66 20 6f 6e 65 20 69  s reals if one i
16301 73 20 61 20 72 65 61 6c 2c 20 6f 72 20 61 73 20  s a real, or as 
16302 69 6e 74 65 67 65 72 73 0a 20 20 2a 2a 20 69 66  integers.  ** if
16303 20 62 6f 74 68 20 76 61 6c 75 65 73 20 61 72 65   both values are
16304 20 69 6e 74 65 67 65 72 73 2e 0a 20 20 2a 2f 0a   integers..  */.
16305 20 20 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f 66    if( combined_f
16306 6c 61 67 73 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45  lags&(MEM_Int|ME
16307 4d 5f 52 65 61 6c 29 20 29 7b 0a 20 20 20 20 69  M_Real) ){.    i
16308 66 28 20 21 28 66 31 26 28 4d 45 4d 5f 49 6e 74  f( !(f1&(MEM_Int
16309 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0a 20  |MEM_Real)) ){. 
1630a 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
1630b 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 28 66     }.    if( !(f
1630c 32 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52  2&(MEM_Int|MEM_R
1630d 65 61 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 72  eal)) ){.      r
1630e 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a  eturn -1;.    }.
1630f 20 20 20 20 69 66 28 20 28 66 31 20 26 20 66 32      if( (f1 & f2
16310 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 29   & MEM_Int)==0 )
16311 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72  {.      double r
16312 31 2c 20 72 32 3b 0a 20 20 20 20 20 20 69 66 28  1, r2;.      if(
16313 20 28 66 31 26 4d 45 4d 5f 52 65 61 6c 29 3d 3d   (f1&MEM_Real)==
16314 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 31 20  0 ){.        r1 
16315 3d 20 70 4d 65 6d 31 2d 3e 75 2e 69 3b 0a 20 20  = pMem1->u.i;.  
16316 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
16317 20 20 20 72 31 20 3d 20 70 4d 65 6d 31 2d 3e 72     r1 = pMem1->r
16318 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
16319 69 66 28 20 28 66 32 26 4d 45 4d 5f 52 65 61 6c  if( (f2&MEM_Real
1631a 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
1631b 72 32 20 3d 20 70 4d 65 6d 32 2d 3e 75 2e 69 3b  r2 = pMem2->u.i;
1631c 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1631d 20 20 20 20 20 20 72 32 20 3d 20 70 4d 65 6d 32        r2 = pMem2
1631e 2d 3e 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ->r;.      }.   
1631f 20 20 20 69 66 28 20 72 31 3c 72 32 20 29 20 72     if( r1<r2 ) r
16320 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20 20  eturn -1;.      
16321 69 66 28 20 72 31 3e 72 32 20 29 20 72 65 74 75  if( r1>r2 ) retu
16322 72 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75  rn 1;.      retu
16323 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  rn 0;.    }else{
16324 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 66  .      assert( f
16325 31 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20  1&MEM_Int );.   
16326 20 20 20 61 73 73 65 72 74 28 20 66 32 26 4d 45     assert( f2&ME
16327 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 20 20 69  M_Int );.      i
16328 66 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3c 20  f( pMem1->u.i < 
16329 70 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65 74  pMem2->u.i ) ret
1632a 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20 20 69 66  urn -1;.      if
1632b 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3e 20 70  ( pMem1->u.i > p
1632c 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65 74 75  Mem2->u.i ) retu
1632d 72 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75  rn 1;.      retu
1632e 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rn 0;.    }.  }.
1632f 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 61 6c  .  /* If one val
16330 75 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 61  ue is a string a
16331 6e 64 20 74 68 65 20 6f 74 68 65 72 20 69 73 20  nd the other is 
16332 61 20 62 6c 6f 62 2c 20 74 68 65 20 73 74 72 69  a blob, the stri
16333 6e 67 20 69 73 20 6c 65 73 73 2e 0a 20 20 2a 2a  ng is less..  **
16334 20 49 66 20 62 6f 74 68 20 61 72 65 20 73 74 72   If both are str
16335 69 6e 67 73 2c 20 63 6f 6d 70 61 72 65 20 75 73  ings, compare us
16336 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e  ing the collatin
16337 67 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a  g functions..  *
16338 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e 65 64  /.  if( combined
16339 5f 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20 29  _flags&MEM_Str )
1633a 7b 0a 20 20 20 20 69 66 28 20 28 66 31 20 26 20  {.    if( (f1 & 
1633b 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29 7b 0a 20  MEM_Str)==0 ){. 
1633c 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
1633d 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 66 32     }.    if( (f2
1633e 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29   & MEM_Str)==0 )
1633f 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d  {.      return -
16340 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73  1;.    }..    as
16341 73 65 72 74 28 20 70 4d 65 6d 31 2d 3e 65 6e 63  sert( pMem1->enc
16342 3d 3d 70 4d 65 6d 32 2d 3e 65 6e 63 20 29 3b 0a  ==pMem2->enc );.
16343 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
16344 31 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  1->enc==SQLITE_U
16345 54 46 38 20 7c 7c 20 0a 20 20 20 20 20 20 20 20  TF8 || .        
16346 20 20 20 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d      pMem1->enc==
16347 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 7c  SQLITE_UTF16LE |
16348 7c 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 51  | pMem1->enc==SQ
16349 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a  LITE_UTF16BE );.
1634a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c  .    /* The coll
1634b 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6d  ation sequence m
1634c 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20 61  ust be defined a
1634d 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 65 76  t this point, ev
1634e 65 6e 20 69 66 0a 20 20 20 20 2a 2a 20 74 68 65  en if.    ** the
1634f 20 75 73 65 72 20 64 65 6c 65 74 65 73 20 74 68   user deletes th
16350 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  e collation sequ
16351 65 6e 63 65 20 61 66 74 65 72 20 74 68 65 20 76  ence after the v
16352 64 62 65 20 70 72 6f 67 72 61 6d 20 69 73 0a 20  dbe program is. 
16353 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 28     ** compiled (
16354 74 68 69 73 20 77 61 73 20 6e 6f 74 20 61 6c 77  this was not alw
16355 61 79 73 20 74 68 65 20 63 61 73 65 29 2e 0a 20  ays the case).. 
16356 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
16357 28 20 21 70 43 6f 6c 6c 20 7c 7c 20 70 43 6f 6c  ( !pColl || pCol
16358 6c 2d 3e 78 43 6d 70 20 29 3b 0a 0a 20 20 20 20  l->xCmp );..    
16359 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20  if( pColl ){.   
1635a 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 65 6e     if( pMem1->en
1635b 63 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63 20 29 7b  c==pColl->enc ){
1635c 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
1635d 73 74 72 69 6e 67 73 20 61 72 65 20 61 6c 72 65  strings are alre
1635e 61 64 79 20 69 6e 20 74 68 65 20 63 6f 72 72 65  ady in the corre
1635f 63 74 20 65 6e 63 6f 64 69 6e 67 2e 20 20 43 61  ct encoding.  Ca
16360 6c 6c 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  ll the.        *
16361 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e  * comparison fun
16362 63 74 69 6f 6e 20 64 69 72 65 63 74 6c 79 20 2a  ction directly *
16363 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  /.        return
16364 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f   pColl->xCmp(pCo
16365 6c 6c 2d 3e 70 55 73 65 72 2c 70 4d 65 6d 31 2d  ll->pUser,pMem1-
16366 3e 6e 2c 70 4d 65 6d 31 2d 3e 7a 2c 70 4d 65 6d  >n,pMem1->z,pMem
16367 32 2d 3e 6e 2c 70 4d 65 6d 32 2d 3e 7a 29 3b 0a  2->n,pMem2->z);.
16368 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
16369 20 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20       const void 
1636a 2a 76 31 2c 20 2a 76 32 3b 0a 20 20 20 20 20 20  *v1, *v2;.      
1636b 20 20 69 6e 74 20 6e 31 2c 20 6e 32 3b 0a 20 20    int n1, n2;.  
1636c 20 20 20 20 20 20 4d 65 6d 20 63 31 3b 0a 20 20        Mem c1;.  
1636d 20 20 20 20 20 20 4d 65 6d 20 63 32 3b 0a 20 20        Mem c2;.  
1636e 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 63 31        memset(&c1
1636f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 63 31 29 29  , 0, sizeof(c1))
16370 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74  ;.        memset
16371 28 26 63 32 2c 20 30 2c 20 73 69 7a 65 6f 66 28  (&c2, 0, sizeof(
16372 63 32 29 29 3b 0a 20 20 20 20 20 20 20 20 73 71  c2));.        sq
16373 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c  lite3VdbeMemShal
16374 6c 6f 77 43 6f 70 79 28 26 63 31 2c 20 70 4d 65  lowCopy(&c1, pMe
16375 6d 31 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a  m1, MEM_Ephem);.
16376 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
16377 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70  dbeMemShallowCop
16378 79 28 26 63 32 2c 20 70 4d 65 6d 32 2c 20 4d 45  y(&c2, pMem2, ME
16379 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 20 20  M_Ephem);.      
1637a 20 20 76 31 20 3d 20 73 71 6c 69 74 65 33 56 61    v1 = sqlite3Va
1637b 6c 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 33  lueText((sqlite3
1637c 5f 76 61 6c 75 65 2a 29 26 63 31 2c 20 70 43 6f  _value*)&c1, pCo
1637d 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20  ll->enc);.      
1637e 20 20 6e 31 20 3d 20 76 31 3d 3d 30 20 3f 20 30    n1 = v1==0 ? 0
1637f 20 3a 20 63 31 2e 6e 3b 0a 20 20 20 20 20 20 20   : c1.n;.       
16380 20 76 32 20 3d 20 73 71 6c 69 74 65 33 56 61 6c   v2 = sqlite3Val
16381 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 33 5f  ueText((sqlite3_
16382 76 61 6c 75 65 2a 29 26 63 32 2c 20 70 43 6f 6c  value*)&c2, pCol
16383 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 20  l->enc);.       
16384 20 6e 32 20 3d 20 76 32 3d 3d 30 20 3f 20 30 20   n2 = v2==0 ? 0 
16385 3a 20 63 32 2e 6e 3b 0a 20 20 20 20 20 20 20 20  : c2.n;.        
16386 72 63 20 3d 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70  rc = pColl->xCmp
16387 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 20 6e  (pColl->pUser, n
16388 31 2c 20 76 31 2c 20 6e 32 2c 20 76 32 29 3b 0a  1, v1, n2, v2);.
16389 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1638a 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 63  dbeMemRelease(&c
1638b 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  1);.        sqli
1638c 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
1638d 65 28 26 63 32 29 3b 0a 20 20 20 20 20 20 20 20  e(&c2);.        
1638e 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
1638f 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20   }.    }.    /* 
16390 49 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  If a NULL pointe
16391 72 20 77 61 73 20 70 61 73 73 65 64 20 61 73 20  r was passed as 
16392 74 68 65 20 63 6f 6c 6c 61 74 65 20 66 75 6e 63  the collate func
16393 74 69 6f 6e 2c 20 66 61 6c 6c 20 74 68 72 6f 75  tion, fall throu
16394 67 68 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65  gh.    ** to the
16395 20 62 6c 6f 62 20 63 61 73 65 20 61 6e 64 20 75   blob case and u
16396 73 65 20 6d 65 6d 63 6d 70 28 29 2e 20 20 2a 2f  se memcmp().  */
16397 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 42 6f 74 68  .  }. .  /* Both
16398 20 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65 20   values must be 
16399 62 6c 6f 62 73 2e 20 20 43 6f 6d 70 61 72 65 20  blobs.  Compare 
1639a 75 73 69 6e 67 20 6d 65 6d 63 6d 70 28 29 2e 20  using memcmp(). 
1639b 20 2a 2f 0a 20 20 72 63 20 3d 20 6d 65 6d 63 6d   */.  rc = memcm
1639c 70 28 70 4d 65 6d 31 2d 3e 7a 2c 20 70 4d 65 6d  p(pMem1->z, pMem
1639d 32 2d 3e 7a 2c 20 28 70 4d 65 6d 31 2d 3e 6e 3e  2->z, (pMem1->n>
1639e 70 4d 65 6d 32 2d 3e 6e 29 3f 70 4d 65 6d 32 2d  pMem2->n)?pMem2-
1639f 3e 6e 3a 70 4d 65 6d 31 2d 3e 6e 29 3b 0a 20 20  >n:pMem1->n);.  
163a0 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20  if( rc==0 ){.   
163a1 20 72 63 20 3d 20 70 4d 65 6d 31 2d 3e 6e 20 2d   rc = pMem1->n -
163a2 20 70 4d 65 6d 32 2d 3e 6e 3b 0a 20 20 7d 0a 20   pMem2->n;.  }. 
163a3 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
163a4 2a 0a 2a 2a 20 4d 6f 76 65 20 64 61 74 61 20 6f  *.** Move data o
163a5 75 74 20 6f 66 20 61 20 62 74 72 65 65 20 6b 65  ut of a btree ke
163a6 79 20 6f 72 20 64 61 74 61 20 66 69 65 6c 64 20  y or data field 
163a7 61 6e 64 20 69 6e 74 6f 20 61 20 4d 65 6d 20 73  and into a Mem s
163a8 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65  tructure..** The
163a9 20 64 61 74 61 20 6f 72 20 6b 65 79 20 69 73 20   data or key is 
163aa 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 65  taken from the e
163ab 6e 74 72 79 20 74 68 61 74 20 70 43 75 72 20 69  ntry that pCur i
163ac 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  s currently poin
163ad 74 69 6e 67 0a 2a 2a 20 74 6f 2e 20 20 6f 66 66  ting.** to.  off
163ae 73 65 74 20 61 6e 64 20 61 6d 74 20 64 65 74 65  set and amt dete
163af 72 6d 69 6e 65 20 77 68 61 74 20 70 6f 72 74 69  rmine what porti
163b0 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 20 6f  on of the data o
163b1 72 20 6b 65 79 20 74 6f 20 72 65 74 72 69 65 76  r key to retriev
163b2 65 2e 0a 2a 2a 20 6b 65 79 20 69 73 20 74 72 75  e..** key is tru
163b3 65 20 74 6f 20 67 65 74 20 74 68 65 20 6b 65 79  e to get the key
163b4 20 6f 72 20 66 61 6c 73 65 20 74 6f 20 67 65 74   or false to get
163b5 20 64 61 74 61 2e 20 20 54 68 65 20 72 65 73 75   data.  The resu
163b6 6c 74 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a  lt is written.**
163b7 20 69 6e 74 6f 20 74 68 65 20 70 4d 65 6d 20 65   into the pMem e
163b8 6c 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  lement..**.** Th
163b9 65 20 70 4d 65 6d 20 73 74 72 75 63 74 75 72 65  e pMem structure
163ba 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 62   is assumed to b
163bb 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e  e uninitialized.
163bc 20 20 41 6e 79 20 70 72 69 6f 72 20 63 6f 6e 74    Any prior cont
163bd 65 6e 74 0a 2a 2a 20 69 73 20 6f 76 65 72 77 72  ent.** is overwr
163be 69 74 74 65 6e 20 77 69 74 68 6f 75 74 20 62 65  itten without be
163bf 69 6e 67 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a  ing freed..**.**
163c0 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   If this routine
163c1 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 72   fails for any r
163c2 65 61 73 6f 6e 20 28 6d 61 6c 6c 6f 63 20 72 65  eason (malloc re
163c3 74 75 72 6e 73 20 4e 55 4c 4c 20 6f 72 20 75 6e  turns NULL or un
163c4 61 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 61 64 20  able.** to read 
163c5 66 72 6f 6d 20 74 68 65 20 64 69 73 6b 29 20 74  from the disk) t
163c6 68 65 6e 20 74 68 65 20 70 4d 65 6d 20 69 73 20  hen the pMem is 
163c7 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e  left in an incon
163c8 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0a 2a  sistent state..*
163c9 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
163ca 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
163cb 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 0a 20 20  MemFromBtree(.  
163cc 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
163cd 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e    /* Cursor poin
163ce 74 69 6e 67 20 61 74 20 72 65 63 6f 72 64 20 74  ting at record t
163cf 6f 20 72 65 74 72 69 65 76 65 2e 20 2a 2f 0a 20  o retrieve. */. 
163d0 20 69 6e 74 20 6f 66 66 73 65 74 2c 20 20 20 20   int offset,    
163d1 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f     /* Offset fro
163d2 6d 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 64  m the start of d
163d3 61 74 61 20 74 6f 20 72 65 74 75 72 6e 20 62 79  ata to return by
163d4 74 65 73 20 66 72 6f 6d 2e 20 2a 2f 0a 20 20 69  tes from. */.  i
163d5 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20  nt amt,         
163d6 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
163d7 74 65 73 20 74 6f 20 72 65 74 75 72 6e 2e 20 2a  tes to return. *
163d8 2f 0a 20 20 69 6e 74 20 6b 65 79 2c 20 20 20 20  /.  int key,    
163d9 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65        /* If true
163da 2c 20 72 65 74 72 69 65 76 65 20 66 72 6f 6d 20  , retrieve from 
163db 74 68 65 20 62 74 72 65 65 20 6b 65 79 2c 20 6e  the btree key, n
163dc 6f 74 20 64 61 74 61 2e 20 2a 2f 0a 20 20 4d 65  ot data. */.  Me
163dd 6d 20 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 20  m *pMem         
163de 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20 64  /* OUT: Return d
163df 61 74 61 20 69 6e 20 74 68 69 73 20 4d 65 6d 20  ata in this Mem 
163e0 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 29 7b  structure. */.){
163e1 0a 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b 20  .  char *zData; 
163e2 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 72        /* Data fr
163e3 6f 6d 20 74 68 65 20 62 74 72 65 65 20 6c 61 79  om the btree lay
163e4 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 76 61 69  er */.  int avai
163e5 6c 61 62 6c 65 20 3d 20 30 3b 20 2f 2a 20 4e 75  lable = 0; /* Nu
163e6 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 76  mber of bytes av
163e7 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c  ailable on the l
163e8 6f 63 61 6c 20 62 74 72 65 65 20 70 61 67 65 20  ocal btree page 
163e9 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
163ea 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62  ;       /* Datab
163eb 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
163ec 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  /.  int rc = SQL
163ed 49 54 45 5f 4f 4b 3b 0a 0a 20 20 64 62 20 3d 20  ITE_OK;..  db = 
163ee 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
163ef 6f 72 44 62 28 70 43 75 72 29 3b 0a 20 20 61 73  orDb(pCur);.  as
163f0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
163f1 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
163f2 65 78 29 20 29 3b 0a 20 20 69 66 28 20 6b 65 79  ex) );.  if( key
163f3 20 29 7b 0a 20 20 20 20 7a 44 61 74 61 20 3d 20   ){.    zData = 
163f4 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 42  (char *)sqlite3B
163f5 74 72 65 65 4b 65 79 46 65 74 63 68 28 70 43 75  treeKeyFetch(pCu
163f6 72 2c 20 26 61 76 61 69 6c 61 62 6c 65 29 3b 0a  r, &available);.
163f7 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 44 61    }else{.    zDa
163f8 74 61 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c  ta = (char *)sql
163f9 69 74 65 33 42 74 72 65 65 44 61 74 61 46 65 74  ite3BtreeDataFet
163fa 63 68 28 70 43 75 72 2c 20 26 61 76 61 69 6c 61  ch(pCur, &availa
163fb 62 6c 65 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65  ble);.  }.  asse
163fc 72 74 28 20 7a 44 61 74 61 21 3d 30 20 29 3b 0a  rt( zData!=0 );.
163fd 0a 20 20 69 66 28 20 6f 66 66 73 65 74 2b 61 6d  .  if( offset+am
163fe 74 3c 3d 61 76 61 69 6c 61 62 6c 65 20 26 26 20  t<=available && 
163ff 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45  ((pMem->flags&ME
16400 4d 5f 44 79 6e 29 3d 3d 30 20 7c 7c 20 70 4d 65  M_Dyn)==0 || pMe
16401 6d 2d 3e 78 44 65 6c 29 20 29 7b 0a 20 20 20 20  m->xDel) ){.    
16402 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
16403 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20  lease(pMem);.   
16404 20 70 4d 65 6d 2d 3e 7a 20 3d 20 26 7a 44 61 74   pMem->z = &zDat
16405 61 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 20 20 70  a[offset];.    p
16406 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
16407 5f 42 6c 6f 62 7c 4d 45 4d 5f 45 70 68 65 6d 3b  _Blob|MEM_Ephem;
16408 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 53 51 4c  .  }else if( SQL
16409 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71  ITE_OK==(rc = sq
1640a 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77  lite3VdbeMemGrow
1640b 28 70 4d 65 6d 2c 20 61 6d 74 2b 32 2c 20 30 29  (pMem, amt+2, 0)
1640c 29 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66  ) ){.    pMem->f
1640d 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c  lags = MEM_Blob|
1640e 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 54 65 72 6d  MEM_Dyn|MEM_Term
1640f 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20  ;.    pMem->enc 
16410 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74  = 0;.    pMem->t
16411 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f  ype = SQLITE_BLO
16412 42 3b 0a 20 20 20 20 69 66 28 20 6b 65 79 20 29  B;.    if( key )
16413 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
16414 69 74 65 33 42 74 72 65 65 4b 65 79 28 70 43 75  ite3BtreeKey(pCu
16415 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20  r, offset, amt, 
16416 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 7d 65  pMem->z);.    }e
16417 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
16418 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61  sqlite3BtreeData
16419 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61  (pCur, offset, a
1641a 6d 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20  mt, pMem->z);.  
1641b 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b    }.    pMem->z[
1641c 61 6d 74 5d 20 3d 20 30 3b 0a 20 20 20 20 70 4d  amt] = 0;.    pM
1641d 65 6d 2d 3e 7a 5b 61 6d 74 2b 31 5d 20 3d 20 30  em->z[amt+1] = 0
1641e 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
1641f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
16420 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
16421 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20  elease(pMem);.  
16422 20 20 7d 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e    }.  }.  pMem->
16423 6e 20 3d 20 61 6d 74 3b 0a 0a 20 20 72 65 74 75  n = amt;..  retu
16424 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 30 0a  rn rc;.}..#if 0.
16425 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 76 61  /*.** Perform va
16426 72 69 6f 75 73 20 63 68 65 63 6b 73 20 6f 6e 20  rious checks on 
16427 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  the memory cell 
16428 70 4d 65 6d 2e 20 41 6e 20 61 73 73 65 72 74 28  pMem. An assert(
16429 29 20 77 69 6c 6c 0a 2a 2a 20 66 61 69 6c 20 69  ) will.** fail i
1642a 66 20 70 4d 65 6d 20 69 73 20 69 6e 74 65 72 6e  f pMem is intern
1642b 61 6c 6c 79 20 69 6e 63 6f 6e 73 69 73 74 65 6e  ally inconsisten
1642c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
1642d 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1642e 33 56 64 62 65 4d 65 6d 53 61 6e 69 74 79 28 4d  3VdbeMemSanity(M
1642f 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74  em *pMem){.  int
16430 20 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66   flags = pMem->f
16431 6c 61 67 73 3b 0a 20 20 61 73 73 65 72 74 28 20  lags;.  assert( 
16432 66 6c 61 67 73 21 3d 30 20 29 3b 20 20 2f 2a 20  flags!=0 );  /* 
16433 4d 75 73 74 20 64 65 66 69 6e 65 20 73 6f 6d 65  Must define some
16434 20 74 79 70 65 20 2a 2f 0a 20 20 69 66 28 20 66   type */.  if( f
16435 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c  lags & (MEM_Str|
16436 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20  MEM_Blob) ){.   
16437 20 69 6e 74 20 78 20 3d 20 66 6c 61 67 73 20 26   int x = flags &
16438 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d   (MEM_Static|MEM
16439 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 7c 4d  _Dyn|MEM_Ephem|M
1643a 45 4d 5f 53 68 6f 72 74 29 3b 0a 20 20 20 20 61  EM_Short);.    a
1643b 73 73 65 72 74 28 20 78 21 3d 30 20 29 3b 20 20  ssert( x!=0 );  
1643c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72            /* Str
1643d 69 6e 67 73 20 6d 75 73 74 20 64 65 66 69 6e 65  ings must define
1643e 20 61 20 73 74 72 69 6e 67 20 73 75 62 74 79 70   a string subtyp
1643f 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  e */.    assert(
16440 20 28 78 20 26 20 28 78 2d 31 29 29 3d 3d 30 20   (x & (x-1))==0 
16441 29 3b 20 20 2f 2a 20 4f 6e 6c 79 20 6f 6e 65 20  );  /* Only one 
16442 73 74 72 69 6e 67 20 73 75 62 74 79 70 65 20 63  string subtype c
16443 61 6e 20 62 65 20 64 65 66 69 6e 65 64 20 2a 2f  an be defined */
16444 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65  .    assert( pMe
16445 6d 2d 3e 7a 21 3d 30 20 29 3b 20 20 20 20 20 20  m->z!=0 );      
16446 2f 2a 20 53 74 72 69 6e 67 73 20 6d 75 73 74 20  /* Strings must 
16447 68 61 76 65 20 61 20 76 61 6c 75 65 20 2a 2f 0a  have a value */.
16448 20 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69      /* Mem.z poi
16449 6e 74 73 20 74 6f 20 4d 65 6d 2e 7a 53 68 6f 72  nts to Mem.zShor
1644a 74 20 69 66 66 20 74 68 65 20 73 75 62 74 79 70  t iff the subtyp
1644b 65 20 69 73 20 4d 45 4d 5f 53 68 6f 72 74 20 2a  e is MEM_Short *
1644c 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 78  /.    assert( (x
1644d 20 26 20 4d 45 4d 5f 53 68 6f 72 74 29 3d 3d 30   & MEM_Short)==0
1644e 20 7c 7c 20 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65   || pMem->z==pMe
1644f 6d 2d 3e 7a 53 68 6f 72 74 20 29 3b 0a 20 20 20  m->zShort );.   
16450 20 61 73 73 65 72 74 28 20 28 78 20 26 20 4d 45   assert( (x & ME
16451 4d 5f 53 68 6f 72 74 29 21 3d 30 20 7c 7c 20 70  M_Short)!=0 || p
16452 4d 65 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a 53  Mem->z!=pMem->zS
16453 68 6f 72 74 20 29 3b 0a 20 20 20 20 2f 2a 20 4e  hort );.    /* N
16454 6f 20 64 65 73 74 72 75 63 74 6f 72 20 75 6e 6c  o destructor unl
16455 65 73 73 20 74 68 65 72 65 20 69 73 20 4d 45 4d  ess there is MEM
16456 5f 44 79 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65  _Dyn */.    asse
16457 72 74 28 20 70 4d 65 6d 2d 3e 78 44 65 6c 3d 3d  rt( pMem->xDel==
16458 30 20 7c 7c 20 28 70 4d 65 6d 2d 3e 66 6c 61 67  0 || (pMem->flag
16459 73 20 26 20 4d 45 4d 5f 44 79 6e 29 21 3d 30 20  s & MEM_Dyn)!=0 
1645a 29 3b 0a 0a 20 20 20 20 69 66 28 20 28 66 6c 61  );..    if( (fla
1645b 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 20 29 7b  gs & MEM_Str) ){
1645c 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1645d 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45  Mem->enc==SQLITE
1645e 5f 55 54 46 38 20 7c 7c 20 0a 20 20 20 20 20 20  _UTF8 || .      
1645f 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e          pMem->en
16460 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42  c==SQLITE_UTF16B
16461 45 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20  E ||.           
16462 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51     pMem->enc==SQ
16463 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 0a 20 20  LITE_UTF16LE .  
16464 20 20 20 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20      );.      /* 
16465 49 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 73  If the string is
16466 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 61   UTF-8 encoded a
16467 6e 64 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65  nd nul terminate
16468 64 2c 20 74 68 65 6e 20 70 4d 65 6d 2d 3e 6e 0a  d, then pMem->n.
16469 20 20 20 20 20 20 2a 2a 20 6d 75 73 74 20 62 65        ** must be
1646a 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74   the length of t
1646b 68 65 20 73 74 72 69 6e 67 2e 20 20 28 4c 61 74  he string.  (Lat
1646c 65 72 3a 29 20 20 49 66 20 74 68 65 20 64 61 74  er:)  If the dat
1646d 61 62 61 73 65 20 66 69 6c 65 0a 20 20 20 20 20  abase file.     
1646e 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 6f 72   ** has been cor
1646f 72 75 70 74 65 64 2c 20 27 5c 30 30 30 27 20 63  rupted, '\000' c
16470 68 61 72 61 63 74 65 72 73 20 6d 69 67 68 74 20  haracters might 
16471 68 61 76 65 20 62 65 65 6e 20 69 6e 73 65 72 74  have been insert
16472 65 64 0a 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f  ed.      ** into
16473 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 74   the middle of t
16474 68 65 20 73 74 72 69 6e 67 2e 20 20 49 6e 20 74  he string.  In t
16475 68 61 74 20 63 61 73 65 2c 20 74 68 65 20 73 74  hat case, the st
16476 72 6c 65 6e 28 29 20 6d 69 67 68 74 0a 20 20 20  rlen() might.   
16477 20 20 20 2a 2a 20 62 65 20 6c 65 73 73 2e 0a 20     ** be less.. 
16478 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
16479 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c  ( pMem->enc==SQL
1647a 49 54 45 5f 55 54 46 38 20 26 26 20 28 66 6c 61  ITE_UTF8 && (fla
1647b 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 20 29  gs & MEM_Term) )
1647c 7b 20 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  { .        asser
1647d 74 28 20 73 74 72 6c 65 6e 28 70 4d 65 6d 2d 3e  t( strlen(pMem->
1647e 7a 29 3c 3d 70 4d 65 6d 2d 3e 6e 20 29 3b 0a 20  z)<=pMem->n );. 
1647f 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
16480 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 3d  Mem->z[pMem->n]=
16481 3d 30 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  =0 );.      }.  
16482 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
16483 20 2f 2a 20 43 61 6e 6e 6f 74 20 64 65 66 69 6e   /* Cannot defin
16484 65 20 61 20 73 74 72 69 6e 67 20 73 75 62 74 79  e a string subty
16485 70 65 20 66 6f 72 20 6e 6f 6e 2d 73 74 72 69 6e  pe for non-strin
16486 67 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 20  g objects */.   
16487 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e   assert( (pMem->
16488 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 61  flags & (MEM_Sta
16489 74 69 63 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f  tic|MEM_Dyn|MEM_
1648a 45 70 68 65 6d 7c 4d 45 4d 5f 53 68 6f 72 74 29  Ephem|MEM_Short)
1648b 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  )==0 );.    asse
1648c 72 74 28 20 70 4d 65 6d 2d 3e 78 44 65 6c 3d 3d  rt( pMem->xDel==
1648d 30 20 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 4d 45  0 );.  }.  /* ME
1648e 4d 5f 4e 75 6c 6c 20 65 78 63 6c 75 64 65 73 20  M_Null excludes 
1648f 61 6c 6c 20 6f 74 68 65 72 20 74 79 70 65 73 20  all other types 
16490 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d  */.  assert( (pM
16491 65 6d 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53  em->flags&(MEM_S
16492 74 72 7c 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52  tr|MEM_Int|MEM_R
16493 65 61 6c 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d  eal|MEM_Blob))==
16494 30 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28  0.          || (
16495 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pMem->flags&MEM_
16496 4e 75 6c 6c 29 3d 3d 30 20 29 3b 0a 20 20 2f 2a  Null)==0 );.  /*
16497 20 49 66 20 74 68 65 20 4d 45 4d 20 69 73 20 62   If the MEM is b
16498 6f 74 68 20 72 65 61 6c 20 61 6e 64 20 69 6e 74  oth real and int
16499 65 67 65 72 2c 20 74 68 65 20 76 61 6c 75 65 73  eger, the values
1649a 20 61 72 65 20 65 71 75 61 6c 20 2a 2f 0a 20 20   are equal */.  
1649b 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66  assert( (pMem->f
1649c 6c 61 67 73 20 26 20 28 4d 45 4d 5f 49 6e 74 7c  lags & (MEM_Int|
1649d 4d 45 4d 5f 52 65 61 6c 29 29 21 3d 28 4d 45 4d  MEM_Real))!=(MEM
1649e 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20 0a  _Int|MEM_Real) .
1649f 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 4d 65            || pMe
164a0 6d 2d 3e 72 3d 3d 70 4d 65 6d 2d 3e 75 2e 69 20  m->r==pMem->u.i 
164a1 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20  );.}.#endif../* 
164a2 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
164a3 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20   only available 
164a4 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 69 74 20 69  internally, it i
164a5 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68  s not part of th
164a6 65 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 41 50  e.** external AP
164a7 49 2e 20 49 74 20 77 6f 72 6b 73 20 69 6e 20 61  I. It works in a
164a8 20 73 69 6d 69 6c 61 72 20 77 61 79 20 74 6f 20   similar way to 
164a9 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
164aa 78 74 28 29 2c 0a 2a 2a 20 65 78 63 65 70 74 20  xt(),.** except 
164ab 74 68 65 20 64 61 74 61 20 72 65 74 75 72 6e 65  the data returne
164ac 64 20 69 73 20 69 6e 20 74 68 65 20 65 6e 63 6f  d is in the enco
164ad 64 69 6e 67 20 73 70 65 63 69 66 69 65 64 20 62  ding specified b
164ae 79 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20  y the second.** 
164af 70 61 72 61 6d 65 74 65 72 2c 20 77 68 69 63 68  parameter, which
164b0 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20   must be one of 
164b1 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20  SQLITE_UTF16BE, 
164b2 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f  SQLITE_UTF16LE o
164b3 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 38  r.** SQLITE_UTF8
164b4 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 36 2d 30 32  ..**.** (2006-02
164b5 2d 31 36 3a 29 20 20 54 68 65 20 65 6e 63 20 76  -16:)  The enc v
164b6 61 6c 75 65 20 63 61 6e 20 62 65 20 6f 72 2d 65  alue can be or-e
164b7 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 55 54  d with SQLITE_UT
164b8 46 31 36 5f 41 4c 49 47 4e 45 44 2e 0a 2a 2a 20  F16_ALIGNED..** 
164b9 49 66 20 74 68 61 74 20 69 73 20 74 68 65 20 63  If that is the c
164ba 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 72 65  ase, then the re
164bb 73 75 6c 74 20 6d 75 73 74 20 62 65 20 61 6c 69  sult must be ali
164bc 67 6e 65 64 20 6f 6e 20 61 6e 20 65 76 65 6e 20  gned on an even 
164bd 62 79 74 65 0a 2a 2a 20 62 6f 75 6e 64 61 72 79  byte.** boundary
164be 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
164bf 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  ATE const void *
164c0 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74  sqlite3ValueText
164c1 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20  (sqlite3_value* 
164c2 70 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20  pVal, u8 enc){. 
164c3 20 69 66 28 20 21 70 56 61 6c 20 29 20 72 65 74   if( !pVal ) ret
164c4 75 72 6e 20 30 3b 0a 0a 20 20 61 73 73 65 72 74  urn 0;..  assert
164c5 28 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pVal->db==0 ||
164c6 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
164c7 65 6c 64 28 70 56 61 6c 2d 3e 64 62 2d 3e 6d 75  eld(pVal->db->mu
164c8 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
164c9 28 20 28 65 6e 63 26 33 29 3d 3d 28 65 6e 63 26  ( (enc&3)==(enc&
164ca 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c  ~SQLITE_UTF16_AL
164cb 49 47 4e 45 44 29 20 29 3b 0a 0a 20 20 69 66 28  IGNED) );..  if(
164cc 20 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d   pVal->flags&MEM
164cd 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74  _Null ){.    ret
164ce 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73  urn 0;.  }.  ass
164cf 65 72 74 28 20 28 4d 45 4d 5f 42 6c 6f 62 3e 3e  ert( (MEM_Blob>>
164d0 33 29 20 3d 3d 20 4d 45 4d 5f 53 74 72 20 29 3b  3) == MEM_Str );
164d1 0a 20 20 70 56 61 6c 2d 3e 66 6c 61 67 73 20 7c  .  pVal->flags |
164d2 3d 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26  = (pVal->flags &
164d3 20 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a 20   MEM_Blob)>>3;. 
164d4 20 65 78 70 61 6e 64 42 6c 6f 62 28 70 56 61 6c   expandBlob(pVal
164d5 29 3b 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 66  );.  if( pVal->f
164d6 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20 29 7b 0a  lags&MEM_Str ){.
164d7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
164d8 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 56  hangeEncoding(pV
164d9 61 6c 2c 20 65 6e 63 20 26 20 7e 53 51 4c 49 54  al, enc & ~SQLIT
164da 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29  E_UTF16_ALIGNED)
164db 3b 0a 20 20 20 20 69 66 28 20 28 65 6e 63 20 26  ;.    if( (enc &
164dc 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c   SQLITE_UTF16_AL
164dd 49 47 4e 45 44 29 21 3d 30 20 26 26 20 31 3d 3d  IGNED)!=0 && 1==
164de 28 31 26 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f  (1&SQLITE_PTR_TO
164df 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a 29 29 20 29  _INT(pVal->z)) )
164e0 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
164e1 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 28  (pVal->flags & (
164e2 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74  MEM_Ephem|MEM_St
164e3 61 74 69 63 29 29 21 3d 30 20 29 3b 0a 20 20 20  atic))!=0 );.   
164e4 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64     if( sqlite3Vd
164e5 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62  beMemMakeWriteab
164e6 6c 65 28 70 56 61 6c 29 21 3d 53 51 4c 49 54 45  le(pVal)!=SQLITE
164e7 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
164e8 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d  eturn 0;.      }
164e9 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
164ea 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d  e3VdbeMemNulTerm
164eb 69 6e 61 74 65 28 70 56 61 6c 29 3b 0a 20 20 7d  inate(pVal);.  }
164ec 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
164ed 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d  ( (pVal->flags&M
164ee 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 3b 0a 20  EM_Blob)==0 );. 
164ef 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
164f0 6d 53 74 72 69 6e 67 69 66 79 28 70 56 61 6c 2c  mStringify(pVal,
164f1 20 65 6e 63 29 3b 0a 20 20 20 20 61 73 73 65 72   enc);.    asser
164f2 74 28 20 30 3d 3d 28 31 26 28 69 6e 74 29 70 56  t( 0==(1&(int)pV
164f3 61 6c 2d 3e 7a 29 20 29 3b 0a 20 20 7d 0a 20 20  al->z) );.  }.  
164f4 61 73 73 65 72 74 28 70 56 61 6c 2d 3e 65 6e 63  assert(pVal->enc
164f5 3d 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49 54 45  ==(enc & ~SQLITE
164f6 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 20  _UTF16_ALIGNED) 
164f7 7c 7c 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 0a 20  || pVal->db==0. 
164f8 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
164f9 70 56 61 6c 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  pVal->db->malloc
164fa 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20  Failed );.  if( 
164fb 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20  pVal->enc==(enc 
164fc 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f  & ~SQLITE_UTF16_
164fd 41 4c 49 47 4e 45 44 29 20 29 7b 0a 20 20 20 20  ALIGNED) ){.    
164fe 72 65 74 75 72 6e 20 70 56 61 6c 2d 3e 7a 3b 0a  return pVal->z;.
164ff 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
16500 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  urn 0;.  }.}../*
16501 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
16502 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
16503 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  bject..*/.SQLITE
16504 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33  _PRIVATE sqlite3
16505 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56  _value *sqlite3V
16506 61 6c 75 65 4e 65 77 28 73 71 6c 69 74 65 33 20  alueNew(sqlite3 
16507 2a 64 62 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d  *db){.  Mem *p =
16508 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
16509 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
1650a 2a 70 29 29 3b 0a 20 20 69 66 28 20 70 20 29 7b  *p));.  if( p ){
1650b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20  .    p->flags = 
1650c 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 2d  MEM_Null;.    p-
1650d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e  >type = SQLITE_N
1650e 55 4c 4c 3b 0a 20 20 20 20 70 2d 3e 64 62 20 3d  ULL;.    p->db =
1650f 20 64 62 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   db;.  }.  retur
16510 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72  n p;.}../*.** Cr
16511 65 61 74 65 20 61 20 6e 65 77 20 73 71 6c 69 74  eate a new sqlit
16512 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2c  e3_value object,
16513 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
16514 76 61 6c 75 65 20 6f 66 20 70 45 78 70 72 2e 0a  value of pExpr..
16515 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 6e 6c 79 20  **.** This only 
16516 77 6f 72 6b 73 20 66 6f 72 20 76 65 72 79 20 73  works for very s
16517 69 6d 70 6c 65 20 65 78 70 72 65 73 73 69 6f 6e  imple expression
16518 73 20 74 68 61 74 20 63 6f 6e 73 69 73 74 20 6f  s that consist o
16519 66 20 6f 6e 65 20 63 6f 6e 73 74 61 6e 74 0a 2a  f one constant.*
1651a 2a 20 74 6f 6b 65 6e 20 28 69 2e 65 2e 20 22 35  * token (i.e. "5
1651b 22 2c 20 22 35 2e 31 22 2c 20 22 27 61 20 73 74  ", "5.1", "'a st
1651c 72 69 6e 67 27 22 29 2e 20 49 66 20 74 68 65 20  ring'"). If the 
1651d 65 78 70 72 65 73 73 69 6f 6e 20 63 61 6e 0a 2a  expression can.*
1651e 2a 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 64  * be converted d
1651f 69 72 65 63 74 6c 79 20 69 6e 74 6f 20 61 20 76  irectly into a v
16520 61 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20 76  alue, then the v
16521 61 6c 75 65 20 69 73 20 61 6c 6c 6f 63 61 74 65  alue is allocate
16522 64 20 61 6e 64 0a 2a 2a 20 61 20 70 6f 69 6e 74  d and.** a point
16523 65 72 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70  er written to *p
16524 70 56 61 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72  pVal. The caller
16525 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
16526 66 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67  for deallocating
16527 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 62 79  .** the value by
16528 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f 20 73   passing it to s
16529 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
1652a 29 20 6c 61 74 65 72 20 6f 6e 2e 20 49 66 20 74  ) later on. If t
1652b 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a  he expression.**
1652c 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65   cannot be conve
1652d 72 74 65 64 20 74 6f 20 61 20 76 61 6c 75 65 2c  rted to a value,
1652e 20 74 68 65 6e 20 2a 70 70 56 61 6c 20 69 73 20   then *ppVal is 
1652f 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a  set to NULL..*/.
16530 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
16531 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46  nt sqlite3ValueF
16532 72 6f 6d 45 78 70 72 28 0a 20 20 73 71 6c 69 74  romExpr(.  sqlit
16533 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
16534 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
16535 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
16536 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  */.  Expr *pExpr
16537 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
16538 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e  * The expression
16539 20 74 6f 20 65 76 61 6c 75 61 74 65 20 2a 2f 0a   to evaluate */.
1653a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20    u8 enc,       
1653b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
1653c 6e 63 6f 64 69 6e 67 20 74 6f 20 75 73 65 20 2a  ncoding to use *
1653d 2f 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 2c  /.  u8 affinity,
1653e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1653f 20 41 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65   Affinity to use
16540 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61   */.  sqlite3_va
16541 6c 75 65 20 2a 2a 70 70 56 61 6c 20 20 20 20 20  lue **ppVal     
16542 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 65 77  /* Write the new
16543 20 76 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a 29   value here */.)
16544 7b 0a 20 20 69 6e 74 20 6f 70 3b 0a 20 20 63 68  {.  int op;.  ch
16545 61 72 20 2a 7a 56 61 6c 20 3d 20 30 3b 0a 20 20  ar *zVal = 0;.  
16546 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
16547 56 61 6c 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20  Val = 0;..  if( 
16548 21 70 45 78 70 72 20 29 7b 0a 20 20 20 20 2a 70  !pExpr ){.    *p
16549 70 56 61 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65  pVal = 0;.    re
1654a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1654b 20 20 7d 0a 20 20 6f 70 20 3d 20 70 45 78 70 72    }.  op = pExpr
1654c 2d 3e 6f 70 3b 0a 0a 20 20 69 66 28 20 6f 70 3d  ->op;..  if( op=
1654d 3d 54 4b 5f 53 54 52 49 4e 47 20 7c 7c 20 6f 70  =TK_STRING || op
1654e 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 6f 70  ==TK_FLOAT || op
1654f 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a  ==TK_INTEGER ){.
16550 20 20 20 20 7a 56 61 6c 20 3d 20 73 71 6c 69 74      zVal = sqlit
16551 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20  e3DbStrNDup(db, 
16552 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f  (char*)pExpr->to
16553 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f  ken.z, pExpr->to
16554 6b 65 6e 2e 6e 29 3b 0a 20 20 20 20 70 56 61 6c  ken.n);.    pVal
16555 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e   = sqlite3ValueN
16556 65 77 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20  ew(db);.    if( 
16557 21 7a 56 61 6c 20 7c 7c 20 21 70 56 61 6c 20 29  !zVal || !pVal )
16558 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20   goto no_mem;.  
16559 20 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65    sqlite3Dequote
1655a 28 7a 56 61 6c 29 3b 0a 20 20 20 20 73 71 6c 69  (zVal);.    sqli
1655b 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 70  te3ValueSetStr(p
1655c 56 61 6c 2c 20 2d 31 2c 20 7a 56 61 6c 2c 20 53  Val, -1, zVal, S
1655d 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49  QLITE_UTF8, SQLI
1655e 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20  TE_DYNAMIC);.   
1655f 20 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 49 4e 54   if( (op==TK_INT
16560 45 47 45 52 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 46  EGER || op==TK_F
16561 4c 4f 41 54 20 29 20 26 26 20 61 66 66 69 6e 69  LOAT ) && affini
16562 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ty==SQLITE_AFF_N
16563 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ONE ){.      sql
16564 69 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66  ite3ValueApplyAf
16565 66 69 6e 69 74 79 28 70 56 61 6c 2c 20 53 51 4c  finity(pVal, SQL
16566 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 2c  ITE_AFF_NUMERIC,
16567 20 65 6e 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65   enc);.    }else
16568 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
16569 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74  alueApplyAffinit
1656a 79 28 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79  y(pVal, affinity
1656b 2c 20 65 6e 63 29 3b 0a 20 20 20 20 7d 0a 20 20  , enc);.    }.  
1656c 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b  }else if( op==TK
1656d 5f 55 4d 49 4e 55 53 20 29 20 7b 0a 20 20 20 20  _UMINUS ) {.    
1656e 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73  if( SQLITE_OK==s
1656f 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45  qlite3ValueFromE
16570 78 70 72 28 64 62 2c 70 45 78 70 72 2d 3e 70 4c  xpr(db,pExpr->pL
16571 65 66 74 2c 65 6e 63 2c 61 66 66 69 6e 69 74 79  eft,enc,affinity
16572 2c 26 70 56 61 6c 29 20 29 7b 0a 20 20 20 20 20  ,&pVal) ){.     
16573 20 70 56 61 6c 2d 3e 75 2e 69 20 3d 20 2d 31 20   pVal->u.i = -1 
16574 2a 20 70 56 61 6c 2d 3e 75 2e 69 3b 0a 20 20 20  * pVal->u.i;.   
16575 20 20 20 70 56 61 6c 2d 3e 72 20 3d 20 2d 31 2e     pVal->r = -1.
16576 30 20 2a 20 70 56 61 6c 2d 3e 72 3b 0a 20 20 20  0 * pVal->r;.   
16577 20 7d 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53   }.  }.#ifndef S
16578 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f  QLITE_OMIT_BLOB_
16579 4c 49 54 45 52 41 4c 0a 20 20 65 6c 73 65 20 69  LITERAL.  else i
1657a 66 28 20 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 20 29  f( op==TK_BLOB )
1657b 7b 0a 20 20 20 20 69 6e 74 20 6e 56 61 6c 3b 0a  {.    int nVal;.
1657c 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70      assert( pExp
1657d 72 2d 3e 74 6f 6b 65 6e 2e 6e 3e 3d 33 20 29 3b  r->token.n>=3 );
1657e 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78  .    assert( pEx
1657f 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d  pr->token.z[0]==
16580 27 78 27 20 7c 7c 20 70 45 78 70 72 2d 3e 74 6f  'x' || pExpr->to
16581 6b 65 6e 2e 7a 5b 30 5d 3d 3d 27 58 27 20 29 3b  ken.z[0]=='X' );
16582 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78  .    assert( pEx
16583 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 31 5d 3d 3d  pr->token.z[1]==
16584 27 5c 27 27 20 29 3b 0a 20 20 20 20 61 73 73 65  '\'' );.    asse
16585 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  rt( pExpr->token
16586 2e 7a 5b 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e  .z[pExpr->token.
16587 6e 2d 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20  n-1]=='\'' );.  
16588 20 20 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33    pVal = sqlite3
16589 56 61 6c 75 65 4e 65 77 28 64 62 29 3b 0a 20 20  ValueNew(db);.  
1658a 20 20 69 66 28 20 21 70 56 61 6c 20 29 20 67 6f    if( !pVal ) go
1658b 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 6e  to no_mem;.    n
1658c 56 61 6c 20 3d 20 70 45 78 70 72 2d 3e 74 6f 6b  Val = pExpr->tok
1658d 65 6e 2e 6e 20 2d 20 33 3b 0a 20 20 20 20 7a 56  en.n - 3;.    zV
1658e 61 6c 20 3d 20 28 63 68 61 72 2a 29 70 45 78 70  al = (char*)pExp
1658f 72 2d 3e 74 6f 6b 65 6e 2e 7a 20 2b 20 32 3b 0a  r->token.z + 2;.
16590 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
16591 65 6d 53 65 74 53 74 72 28 70 56 61 6c 2c 20 73  emSetStr(pVal, s
16592 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28  qlite3HexToBlob(
16593 64 62 2c 20 7a 56 61 6c 2c 20 6e 56 61 6c 29 2c  db, zVal, nVal),
16594 20 6e 56 61 6c 2f 32 2c 0a 20 20 20 20 20 20 20   nVal/2,.       
16595 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16596 20 20 30 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41    0, SQLITE_DYNA
16597 4d 49 43 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  MIC);.  }.#endif
16598 0a 0a 20 20 2a 70 70 56 61 6c 20 3d 20 70 56 61  ..  *ppVal = pVa
16599 6c 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  l;.  return SQLI
1659a 54 45 5f 4f 4b 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 0a  TE_OK;..no_mem:.
1659b 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
1659c 65 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65  ed = 1;.  sqlite
1659d 33 44 62 46 72 65 65 28 64 62 2c 20 7a 56 61 6c  3DbFree(db, zVal
1659e 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 61 6c 75  );.  sqlite3Valu
1659f 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20 2a  eFree(pVal);.  *
165a0 70 70 56 61 6c 20 3d 20 30 3b 0a 20 20 72 65 74  ppVal = 0;.  ret
165a1 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
165a2 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  ;.}../*.** Chang
165a3 65 20 74 68 65 20 73 74 72 69 6e 67 20 76 61 6c  e the string val
165a4 75 65 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33  ue of an sqlite3
165a5 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2f  _value object.*/
165a6 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
165a7 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75  void sqlite3Valu
165a8 65 53 65 74 53 74 72 28 0a 20 20 73 71 6c 69 74  eSetStr(.  sqlit
165a9 65 33 5f 76 61 6c 75 65 20 2a 76 2c 20 20 20 20  e3_value *v,    
165aa 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 62 65 20   /* Value to be 
165ab 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c 20  set */.  int n, 
165ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
165ad 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 74 72 69  * Length of stri
165ae 6e 67 20 7a 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ng z */.  const 
165af 76 6f 69 64 20 2a 7a 2c 20 20 20 20 20 20 20 20  void *z,        
165b0 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 6e  /* Text of the n
165b1 65 77 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 75  ew string */.  u
165b2 38 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20 20  8 enc,          
165b3 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67       /* Encoding
165b4 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 76 6f 69   to use */.  voi
165b5 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29  d (*xDel)(void*)
165b6 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72     /* Destructor
165b7 20 66 6f 72 20 74 68 65 20 73 74 72 69 6e 67 20   for the string 
165b8 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 76 20 29 20  */.){.  if( v ) 
165b9 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
165ba 74 53 74 72 28 28 4d 65 6d 20 2a 29 76 2c 20 7a  tStr((Mem *)v, z
165bb 2c 20 6e 2c 20 65 6e 63 2c 20 78 44 65 6c 29 3b  , n, enc, xDel);
165bc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61  .}../*.** Free a
165bd 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  n sqlite3_value 
165be 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c 49 54 45  object.*/.SQLITE
165bf 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
165c0 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 73  lite3ValueFree(s
165c1 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 76 29  qlite3_value *v)
165c2 7b 0a 20 20 69 66 28 20 21 76 20 29 20 72 65 74  {.  if( !v ) ret
165c3 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  urn;.  sqlite3Vd
165c4 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 28 4d 65  beMemRelease((Me
165c5 6d 20 2a 29 76 29 3b 0a 20 20 73 71 6c 69 74 65  m *)v);.  sqlite
165c6 33 44 62 46 72 65 65 28 28 28 4d 65 6d 2a 29 76  3DbFree(((Mem*)v
165c7 29 2d 3e 64 62 2c 20 76 29 3b 0a 7d 0a 0a 2f 2a  )->db, v);.}../*
165c8 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
165c9 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
165ca 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 61  n the sqlite3_va
165cb 6c 75 65 20 6f 62 6a 65 63 74 20 61 73 73 75 6d  lue object assum
165cc 69 6e 67 0a 2a 2a 20 74 68 61 74 20 69 74 20 75  ing.** that it u
165cd 73 65 73 20 74 68 65 20 65 6e 63 6f 64 69 6e 67  ses the encoding
165ce 20 22 65 6e 63 22 0a 2a 2f 0a 53 51 4c 49 54 45   "enc".*/.SQLITE
165cf 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
165d0 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 73  ite3ValueBytes(s
165d1 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
165d2 61 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 4d  al, u8 enc){.  M
165d3 65 6d 20 2a 70 20 3d 20 28 4d 65 6d 2a 29 70 56  em *p = (Mem*)pV
165d4 61 6c 3b 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c  al;.  if( (p->fl
165d5 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 21  ags & MEM_Blob)!
165d6 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 56 61 6c  =0 || sqlite3Val
165d7 75 65 54 65 78 74 28 70 56 61 6c 2c 20 65 6e 63  ueText(pVal, enc
165d8 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  ) ){.    if( p->
165d9 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f  flags & MEM_Zero
165da 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
165db 20 70 2d 3e 6e 2b 70 2d 3e 75 2e 69 3b 0a 20 20   p->n+p->u.i;.  
165dc 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
165dd 65 74 75 72 6e 20 70 2d 3e 6e 3b 0a 20 20 20 20  eturn p->n;.    
165de 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  }.  }.  return 0
165df 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
165e0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65  **** End of vdbe
165e1 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  mem.c **********
165e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
165e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
165e4 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
165e5 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
165e6 76 64 62 65 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a  vdbeaux.c ******
165e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
165e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
165e9 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20  ***/./*.** 2003 
165ea 53 65 70 74 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a  September 6.**.*
165eb 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
165ec 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
165ed 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
165ee 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
165ef 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
165f0 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
165f1 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
165f2 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
165f3 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
165f4 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
165f5 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
165f6 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
165f7 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
165f8 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
165f9 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
165fa 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
165fb 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
165fc 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
165fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
165fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
165ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
16601 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
16602 20 63 6f 64 65 20 75 73 65 64 20 66 6f 72 20 63   code used for c
16603 72 65 61 74 69 6e 67 2c 20 64 65 73 74 72 6f 79  reating, destroy
16604 69 6e 67 2c 20 61 6e 64 20 70 6f 70 75 6c 61 74  ing, and populat
16605 69 6e 67 0a 2a 2a 20 61 20 56 44 42 45 20 28 6f  ing.** a VDBE (o
16606 72 20 61 6e 20 22 73 71 6c 69 74 65 33 5f 73 74  r an "sqlite3_st
16607 6d 74 22 20 61 73 20 69 74 20 69 73 20 6b 6e 6f  mt" as it is kno
16608 77 6e 20 74 6f 20 74 68 65 20 6f 75 74 73 69 64  wn to the outsid
16609 65 20 77 6f 72 6c 64 2e 29 20 20 50 72 69 6f 72  e world.)  Prior
1660a 0a 2a 2a 20 74 6f 20 76 65 72 73 69 6f 6e 20 32  .** to version 2
1660b 2e 38 2e 37 2c 20 61 6c 6c 20 74 68 69 73 20 63  .8.7, all this c
1660c 6f 64 65 20 77 61 73 20 63 6f 6d 62 69 6e 65 64  ode was combined
1660d 20 69 6e 74 6f 20 74 68 65 20 76 64 62 65 2e 63   into the vdbe.c
1660e 20 73 6f 75 72 63 65 20 66 69 6c 65 2e 0a 2a 2a   source file..**
1660f 20 42 75 74 20 74 68 61 74 20 66 69 6c 65 20 77   But that file w
16610 61 73 20 67 65 74 74 69 6e 67 20 74 6f 6f 20 62  as getting too b
16611 69 67 20 73 6f 20 74 68 69 73 20 73 75 62 72 6f  ig so this subro
16612 75 74 69 6e 65 73 20 77 65 72 65 20 73 70 6c 69  utines were spli
16613 74 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  t out..**.** $Id
16614 3a 20 76 64 62 65 61 75 78 2e 63 2c 76 20 31 2e  : vdbeaux.c,v 1.
16615 34 31 38 20 32 30 30 38 2f 31 31 2f 30 35 20 31  418 2008/11/05 1
16616 37 3a 34 31 3a 31 39 20 64 72 68 20 45 78 70 20  7:41:19 drh Exp 
16617 24 0a 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68  $.*/..../*.** Wh
16618 65 6e 20 64 65 62 75 67 67 69 6e 67 20 74 68 65  en debugging the
16619 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20   code generator 
1661a 69 6e 20 61 20 73 79 6d 62 6f 6c 69 63 20 64 65  in a symbolic de
1661b 62 75 67 67 65 72 2c 20 6f 6e 65 20 63 61 6e 0a  bugger, one can.
1661c 2a 2a 20 73 65 74 20 74 68 65 20 73 71 6c 69 74  ** set the sqlit
1661d 65 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65  e3VdbeAddopTrace
1661e 20 74 6f 20 31 20 61 6e 64 20 61 6c 6c 20 6f 70   to 1 and all op
1661f 63 6f 64 65 73 20 77 69 6c 6c 20 62 65 20 70 72  codes will be pr
16620 69 6e 74 65 64 0a 2a 2a 20 61 73 20 74 68 65 79  inted.** as they
16621 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 74 68   are added to th
16622 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 73 74  e instruction st
16623 72 65 61 6d 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  ream..*/.#ifdef 
16624 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c  SQLITE_DEBUG.SQL
16625 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
16626 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 6f 70  sqlite3VdbeAddop
16627 54 72 61 63 65 20 3d 20 30 3b 0a 23 65 6e 64 69  Trace = 0;.#endi
16628 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  f.../*.** Create
16629 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20 64   a new virtual d
1662a 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 2e 0a  atabase engine..
1662b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1662c 45 20 56 64 62 65 20 2a 73 71 6c 69 74 65 33 56  E Vdbe *sqlite3V
1662d 64 62 65 43 72 65 61 74 65 28 73 71 6c 69 74 65  dbeCreate(sqlite
1662e 33 20 2a 64 62 29 7b 0a 20 20 56 64 62 65 20 2a  3 *db){.  Vdbe *
1662f 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33  p;.  p = sqlite3
16630 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
16631 20 73 69 7a 65 6f 66 28 56 64 62 65 29 20 29 3b   sizeof(Vdbe) );
16632 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
16633 74 75 72 6e 20 30 3b 0a 20 20 70 2d 3e 64 62 20  turn 0;.  p->db 
16634 3d 20 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e  = db;.  if( db->
16635 70 56 64 62 65 20 29 7b 0a 20 20 20 20 64 62 2d  pVdbe ){.    db-
16636 3e 70 56 64 62 65 2d 3e 70 50 72 65 76 20 3d 20  >pVdbe->pPrev = 
16637 70 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4e 65 78  p;.  }.  p->pNex
16638 74 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b 0a 20  t = db->pVdbe;. 
16639 20 70 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20   p->pPrev = 0;. 
1663a 20 64 62 2d 3e 70 56 64 62 65 20 3d 20 70 3b 0a   db->pVdbe = p;.
1663b 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42    p->magic = VDB
1663c 45 5f 4d 41 47 49 43 5f 49 4e 49 54 3b 0a 20 20  E_MAGIC_INIT;.  
1663d 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
1663e 2a 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 65 20  ** Remember the 
1663f 53 51 4c 20 73 74 72 69 6e 67 20 66 6f 72 20 61  SQL string for a
16640 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
16641 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ent..*/.SQLITE_P
16642 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
16643 74 65 33 56 64 62 65 53 65 74 53 71 6c 28 56 64  te3VdbeSetSql(Vd
16644 62 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61  be *p, const cha
16645 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20  r *z, int n){.  
16646 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
16647 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  n;.  assert( p->
16648 7a 53 71 6c 3d 3d 30 20 29 3b 0a 20 20 70 2d 3e  zSql==0 );.  p->
16649 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 44 62  zSql = sqlite3Db
1664a 53 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c 20 7a  StrNDup(p->db, z
1664b 2c 20 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  , n);.}../*.** R
1664c 65 74 75 72 6e 20 74 68 65 20 53 51 4c 20 61 73  eturn the SQL as
1664d 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20  sociated with a 
1664e 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
1664f 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  nt.*/.SQLITE_API
16650 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
16651 69 74 65 33 5f 73 71 6c 28 73 71 6c 69 74 65 33  ite3_sql(sqlite3
16652 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20  _stmt *pStmt){. 
16653 20 72 65 74 75 72 6e 20 28 28 56 64 62 65 20 2a   return ((Vdbe *
16654 29 70 53 74 6d 74 29 2d 3e 7a 53 71 6c 3b 0a 7d  )pStmt)->zSql;.}
16655 0a 0a 2f 2a 0a 2a 2a 20 53 77 61 70 20 61 6c 6c  ../*.** Swap all
16656 20 63 6f 6e 74 65 6e 74 20 62 65 74 77 65 65 6e   content between
16657 20 74 77 6f 20 56 44 42 45 20 73 74 72 75 63 74   two VDBE struct
16658 75 72 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ures..*/.SQLITE_
16659 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1665a 69 74 65 33 56 64 62 65 53 77 61 70 28 56 64 62  ite3VdbeSwap(Vdb
1665b 65 20 2a 70 41 2c 20 56 64 62 65 20 2a 70 42 29  e *pA, Vdbe *pB)
1665c 7b 0a 20 20 56 64 62 65 20 74 6d 70 2c 20 2a 70  {.  Vdbe tmp, *p
1665d 54 6d 70 3b 0a 20 20 63 68 61 72 20 2a 7a 54 6d  Tmp;.  char *zTm
1665e 70 3b 0a 20 20 69 6e 74 20 6e 54 6d 70 3b 0a 20  p;.  int nTmp;. 
1665f 20 74 6d 70 20 3d 20 2a 70 41 3b 0a 20 20 2a 70   tmp = *pA;.  *p
16660 41 20 3d 20 2a 70 42 3b 0a 20 20 2a 70 42 20 3d  A = *pB;.  *pB =
16661 20 74 6d 70 3b 0a 20 20 70 54 6d 70 20 3d 20 70   tmp;.  pTmp = p
16662 41 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 41 2d 3e  A->pNext;.  pA->
16663 70 4e 65 78 74 20 3d 20 70 42 2d 3e 70 4e 65 78  pNext = pB->pNex
16664 74 3b 0a 20 20 70 42 2d 3e 70 4e 65 78 74 20 3d  t;.  pB->pNext =
16665 20 70 54 6d 70 3b 0a 20 20 70 54 6d 70 20 3d 20   pTmp;.  pTmp = 
16666 70 41 2d 3e 70 50 72 65 76 3b 0a 20 20 70 41 2d  pA->pPrev;.  pA-
16667 3e 70 50 72 65 76 20 3d 20 70 42 2d 3e 70 50 72  >pPrev = pB->pPr
16668 65 76 3b 0a 20 20 70 42 2d 3e 70 50 72 65 76 20  ev;.  pB->pPrev 
16669 3d 20 70 54 6d 70 3b 0a 20 20 7a 54 6d 70 20 3d  = pTmp;.  zTmp =
1666a 20 70 41 2d 3e 7a 53 71 6c 3b 0a 20 20 70 41 2d   pA->zSql;.  pA-
1666b 3e 7a 53 71 6c 20 3d 20 70 42 2d 3e 7a 53 71 6c  >zSql = pB->zSql
1666c 3b 0a 20 20 70 42 2d 3e 7a 53 71 6c 20 3d 20 7a  ;.  pB->zSql = z
1666d 54 6d 70 3b 0a 20 20 6e 54 6d 70 20 3d 20 70 41  Tmp;.  nTmp = pA
1666e 2d 3e 6e 53 71 6c 3b 0a 20 20 70 41 2d 3e 6e 53  ->nSql;.  pA->nS
1666f 71 6c 20 3d 20 70 42 2d 3e 6e 53 71 6c 3b 0a 20  ql = pB->nSql;. 
16670 20 70 42 2d 3e 6e 53 71 6c 20 3d 20 6e 54 6d 70   pB->nSql = nTmp
16671 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ;.}..#ifdef SQLI
16672 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54  TE_DEBUG./*.** T
16673 75 72 6e 20 74 72 61 63 69 6e 67 20 6f 6e 20 6f  urn tracing on o
16674 72 20 6f 66 66 0a 2a 2f 0a 53 51 4c 49 54 45 5f  r off.*/.SQLITE_
16675 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
16676 69 74 65 33 56 64 62 65 54 72 61 63 65 28 56 64  ite3VdbeTrace(Vd
16677 62 65 20 2a 70 2c 20 46 49 4c 45 20 2a 74 72 61  be *p, FILE *tra
16678 63 65 29 7b 0a 20 20 70 2d 3e 74 72 61 63 65 20  ce){.  p->trace 
16679 3d 20 74 72 61 63 65 3b 0a 7d 0a 23 65 6e 64 69  = trace;.}.#endi
1667a 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 69 7a 65 20  f../*.** Resize 
1667b 74 68 65 20 56 64 62 65 2e 61 4f 70 20 61 72 72  the Vdbe.aOp arr
1667c 61 79 20 73 6f 20 74 68 61 74 20 69 74 20 63 6f  ay so that it co
1667d 6e 74 61 69 6e 73 20 61 74 20 6c 65 61 73 74 20  ntains at least 
1667e 4e 0a 2a 2a 20 65 6c 65 6d 65 6e 74 73 2e 0a 2a  N.** elements..*
1667f 2a 0a 2a 2a 20 49 66 20 61 6e 20 6f 75 74 2d 6f  *.** If an out-o
16680 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f 72 20 6f  f-memory error o
16681 63 63 75 72 73 20 77 68 69 6c 65 20 72 65 73 69  ccurs while resi
16682 7a 69 6e 67 20 74 68 65 20 61 72 72 61 79 2c 0a  zing the array,.
16683 2a 2a 20 56 64 62 65 2e 61 4f 70 20 61 6e 64 20  ** Vdbe.aOp and 
16684 56 64 62 65 2e 6e 4f 70 41 6c 6c 6f 63 20 72 65  Vdbe.nOpAlloc re
16685 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65 64 20 28  main unchanged (
16686 74 68 69 73 20 69 73 20 73 6f 20 74 68 61 74 0a  this is so that.
16687 2a 2a 20 61 6e 79 20 6f 70 63 6f 64 65 73 20 61  ** any opcodes a
16688 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 64  lready allocated
16689 20 63 61 6e 20 62 65 20 63 6f 72 72 65 63 74 6c   can be correctl
1668a 79 20 64 65 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a  y deallocated.**
1668b 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20   along with the 
1668c 72 65 73 74 20 6f 66 20 74 68 65 20 56 64 62 65  rest of the Vdbe
1668d 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
1668e 64 20 72 65 73 69 7a 65 4f 70 41 72 72 61 79 28  d resizeOpArray(
1668f 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 4e 29 7b  Vdbe *p, int N){
16690 0a 20 20 56 64 62 65 4f 70 20 2a 70 4e 65 77 3b  .  VdbeOp *pNew;
16691 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  pNew = sqlite
16692 33 44 62 52 65 61 6c 6c 6f 63 28 70 2d 3e 64 62  3DbRealloc(p->db
16693 2c 20 70 2d 3e 61 4f 70 2c 20 4e 2a 73 69 7a 65  , p->aOp, N*size
16694 6f 66 28 4f 70 29 29 3b 0a 20 20 69 66 28 20 70  of(Op));.  if( p
16695 4e 65 77 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 4f  New ){.    p->nO
16696 70 41 6c 6c 6f 63 20 3d 20 4e 3b 0a 20 20 20 20  pAlloc = N;.    
16697 70 2d 3e 61 4f 70 20 3d 20 70 4e 65 77 3b 0a 20  p->aOp = pNew;. 
16698 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20   }.}../*.** Add 
16699 61 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69 6f  a new instructio
1669a 6e 20 74 6f 20 74 68 65 20 6c 69 73 74 20 6f 66  n to the list of
1669b 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 63 75   instructions cu
1669c 72 72 65 6e 74 20 69 6e 20 74 68 65 0a 2a 2a 20  rrent in the.** 
1669d 56 44 42 45 2e 20 20 52 65 74 75 72 6e 20 74 68  VDBE.  Return th
1669e 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65  e address of the
1669f 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69 6f 6e   new instruction
166a0 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65  ..**.** Paramete
166a1 72 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70 20 20  rs:.**.**    p  
166a2 20 20 20 20 20 20 20 20 20 20 20 20 20 50 6f 69               Poi
166a3 6e 74 65 72 20 74 6f 20 74 68 65 20 56 44 42 45  nter to the VDBE
166a4 0a 2a 2a 0a 2a 2a 20 20 20 20 6f 70 20 20 20 20  .**.**    op    
166a5 20 20 20 20 20 20 20 20 20 20 54 68 65 20 6f 70            The op
166a6 63 6f 64 65 20 66 6f 72 20 74 68 69 73 20 69 6e  code for this in
166a7 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20  struction.**.** 
166a8 20 20 20 70 31 2c 20 70 32 2c 20 70 33 20 20 20     p1, p2, p3   
166a9 20 20 20 4f 70 65 72 61 6e 64 73 0a 2a 2a 0a 2a     Operands.**.*
166aa 2a 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 65  * Use the sqlite
166ab 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
166ac 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  l() function to 
166ad 66 69 78 20 61 6e 20 61 64 64 72 65 73 73 20 61  fix an address a
166ae 6e 64 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65  nd.** the sqlite
166af 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 29 20  3VdbeChangeP4() 
166b0 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 68 61 6e  function to chan
166b1 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ge the value of 
166b2 74 68 65 20 50 34 0a 2a 2a 20 6f 70 65 72 61 6e  the P4.** operan
166b3 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
166b4 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
166b5 56 64 62 65 41 64 64 4f 70 33 28 56 64 62 65 20  VdbeAddOp3(Vdbe 
166b6 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20  *p, int op, int 
166b7 70 31 2c 20 69 6e 74 20 70 32 2c 20 69 6e 74 20  p1, int p2, int 
166b8 70 33 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  p3){.  int i;.  
166b9 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 0a 20 20  VdbeOp *pOp;..  
166ba 69 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 61 73  i = p->nOp;.  as
166bb 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d  sert( p->magic==
166bc 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20  VDBE_MAGIC_INIT 
166bd 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 41  );.  if( p->nOpA
166be 6c 6c 6f 63 3c 3d 69 20 29 7b 0a 20 20 20 20 72  lloc<=i ){.    r
166bf 65 73 69 7a 65 4f 70 41 72 72 61 79 28 70 2c 20  esizeOpArray(p, 
166c0 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3f 20 70 2d  p->nOpAlloc ? p-
166c1 3e 6e 4f 70 41 6c 6c 6f 63 2a 32 20 3a 20 31 30  >nOpAlloc*2 : 10
166c2 32 34 2f 73 69 7a 65 6f 66 28 4f 70 29 29 3b 0a  24/sizeof(Op));.
166c3 20 20 20 20 69 66 28 20 70 2d 3e 64 62 2d 3e 6d      if( p->db->m
166c4 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
166c5 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
166c6 20 20 20 7d 0a 20 20 7d 0a 20 20 70 2d 3e 6e 4f     }.  }.  p->nO
166c7 70 2b 2b 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d  p++;.  pOp = &p-
166c8 3e 61 4f 70 5b 69 5d 3b 0a 20 20 70 4f 70 2d 3e  >aOp[i];.  pOp->
166c9 6f 70 63 6f 64 65 20 3d 20 6f 70 3b 0a 20 20 70  opcode = op;.  p
166ca 4f 70 2d 3e 70 35 20 3d 20 30 3b 0a 20 20 70 4f  Op->p5 = 0;.  pO
166cb 70 2d 3e 70 31 20 3d 20 70 31 3b 0a 20 20 70 4f  p->p1 = p1;.  pO
166cc 70 2d 3e 70 32 20 3d 20 70 32 3b 0a 20 20 70 4f  p->p2 = p2;.  pO
166cd 70 2d 3e 70 33 20 3d 20 70 33 3b 0a 20 20 70 4f  p->p3 = p3;.  pO
166ce 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 70  p->p4.p = 0;.  p
166cf 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f  Op->p4type = P4_
166d0 4e 4f 54 55 53 45 44 3b 0a 20 20 70 2d 3e 65 78  NOTUSED;.  p->ex
166d1 70 69 72 65 64 20 3d 20 30 3b 0a 23 69 66 64 65  pired = 0;.#ifde
166d2 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
166d3 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3d   pOp->zComment =
166d4 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65   0;.  if( sqlite
166d5 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 20  3VdbeAddopTrace 
166d6 29 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69  ) sqlite3VdbePri
166d7 6e 74 4f 70 28 30 2c 20 69 2c 20 26 70 2d 3e 61  ntOp(0, i, &p->a
166d8 4f 70 5b 69 5d 29 3b 0a 23 65 6e 64 69 66 0a 23  Op[i]);.#endif.#
166d9 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49  ifdef VDBE_PROFI
166da 4c 45 0a 20 20 70 4f 70 2d 3e 63 79 63 6c 65 73  LE.  pOp->cycles
166db 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 63 6e 74   = 0;.  pOp->cnt
166dc 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72   = 0;.#endif.  r
166dd 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51 4c 49 54  eturn i;.}.SQLIT
166de 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
166df 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28  lite3VdbeAddOp0(
166e0 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 29  Vdbe *p, int op)
166e1 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
166e2 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20  e3VdbeAddOp3(p, 
166e3 6f 70 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 7d 0a  op, 0, 0, 0);.}.
166e4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
166e5 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
166e6 64 4f 70 31 28 56 64 62 65 20 2a 70 2c 20 69 6e  dOp1(Vdbe *p, in
166e7 74 20 6f 70 2c 20 69 6e 74 20 70 31 29 7b 0a 20  t op, int p1){. 
166e8 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56   return sqlite3V
166e9 64 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c  dbeAddOp3(p, op,
166ea 20 70 31 2c 20 30 2c 20 30 29 3b 0a 7d 0a 53 51   p1, 0, 0);.}.SQ
166eb 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
166ec 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
166ed 70 32 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20  p2(Vdbe *p, int 
166ee 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69 6e 74 20  op, int p1, int 
166ef 70 32 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  p2){.  return sq
166f0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
166f1 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c 20 30  p, op, p1, p2, 0
166f2 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  );.}.../*.** Add
166f3 20 61 6e 20 6f 70 63 6f 64 65 20 74 68 61 74 20   an opcode that 
166f4 69 6e 63 6c 75 64 65 73 20 74 68 65 20 70 34 20  includes the p4 
166f5 76 61 6c 75 65 20 61 73 20 61 20 70 6f 69 6e 74  value as a point
166f6 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
166f7 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
166f8 33 56 64 62 65 41 64 64 4f 70 34 28 0a 20 20 56  3VdbeAddOp4(.  V
166f9 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20  dbe *p,         
166fa 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 6f 70     /* Add the op
166fb 63 6f 64 65 20 74 6f 20 74 68 69 73 20 56 4d 20  code to this VM 
166fc 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20  */.  int op,    
166fd 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
166fe 6e 65 77 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20  new opcode */.  
166ff 69 6e 74 20 70 31 2c 20 20 20 20 20 20 20 20 20  int p1,         
16700 20 20 20 20 2f 2a 20 54 68 65 20 50 31 20 6f 70      /* The P1 op
16701 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70  erand */.  int p
16702 32 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  2,             /
16703 2a 20 54 68 65 20 50 32 20 6f 70 65 72 61 6e 64  * The P2 operand
16704 20 2a 2f 0a 20 20 69 6e 74 20 70 33 2c 20 20 20   */.  int p3,   
16705 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
16706 20 50 33 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20   P3 operand */. 
16707 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34   const char *zP4
16708 2c 20 20 20 20 2f 2a 20 54 68 65 20 50 34 20 6f  ,    /* The P4 o
16709 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20  perand */.  int 
1670a 70 34 74 79 70 65 20 20 20 20 20 20 20 20 20 20  p4type          
1670b 2f 2a 20 50 34 20 6f 70 65 72 61 6e 64 20 74 79  /* P4 operand ty
1670c 70 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61  pe */.){.  int a
1670d 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
1670e 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 70  eAddOp3(p, op, p
1670f 31 2c 20 70 32 2c 20 70 33 29 3b 0a 20 20 73 71  1, p2, p3);.  sq
16710 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
16711 34 28 70 2c 20 61 64 64 72 2c 20 7a 50 34 2c 20  4(p, addr, zP4, 
16712 70 34 74 79 70 65 29 3b 0a 20 20 72 65 74 75 72  p4type);.  retur
16713 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n addr;.}../*.**
16714 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 79   Create a new sy
16715 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20 66 6f 72  mbolic label for
16716 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20   an instruction 
16717 74 68 61 74 20 68 61 73 20 79 65 74 20 74 6f 20  that has yet to 
16718 62 65 0a 2a 2a 20 63 6f 64 65 64 2e 20 20 54 68  be.** coded.  Th
16719 65 20 73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c  e symbolic label
1671a 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20   is really just 
1671b 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65  a negative numbe
1671c 72 2e 20 20 54 68 65 0a 2a 2a 20 6c 61 62 65 6c  r.  The.** label
1671d 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 20   can be used as 
1671e 74 68 65 20 50 32 20 76 61 6c 75 65 20 6f 66 20  the P2 value of 
1671f 61 6e 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4c  an operation.  L
16720 61 74 65 72 2c 20 77 68 65 6e 0a 2a 2a 20 74 68  ater, when.** th
16721 65 20 6c 61 62 65 6c 20 69 73 20 72 65 73 6f 6c  e label is resol
16722 76 65 64 20 74 6f 20 61 20 73 70 65 63 69 66 69  ved to a specifi
16723 63 20 61 64 64 72 65 73 73 2c 20 74 68 65 20 56  c address, the V
16724 44 42 45 20 77 69 6c 6c 20 73 63 61 6e 0a 2a 2a  DBE will scan.**
16725 20 74 68 72 6f 75 67 68 20 69 74 73 20 6f 70 65   through its ope
16726 72 61 74 69 6f 6e 20 6c 69 73 74 20 61 6e 64 20  ration list and 
16727 63 68 61 6e 67 65 20 61 6c 6c 20 76 61 6c 75 65  change all value
16728 73 20 6f 66 20 50 32 20 77 68 69 63 68 20 6d 61  s of P2 which ma
16729 74 63 68 0a 2a 2a 20 74 68 65 20 6c 61 62 65 6c  tch.** the label
1672a 20 69 6e 74 6f 20 74 68 65 20 72 65 73 6f 6c 76   into the resolv
1672b 65 64 20 61 64 64 72 65 73 73 2e 0a 2a 2a 0a 2a  ed address..**.*
1672c 2a 20 54 68 65 20 56 44 42 45 20 6b 6e 6f 77 73  * The VDBE knows
1672d 20 74 68 61 74 20 61 20 50 32 20 76 61 6c 75 65   that a P2 value
1672e 20 69 73 20 61 20 6c 61 62 65 6c 20 62 65 63 61   is a label beca
1672f 75 73 65 20 6c 61 62 65 6c 73 20 61 72 65 0a 2a  use labels are.*
16730 2a 20 61 6c 77 61 79 73 20 6e 65 67 61 74 69 76  * always negativ
16731 65 20 61 6e 64 20 50 32 20 76 61 6c 75 65 73 20  e and P2 values 
16732 61 72 65 20 73 75 70 70 6f 73 65 20 74 6f 20 62  are suppose to b
16733 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a  e non-negative..
16734 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6e 65 67 61  ** Hence, a nega
16735 74 69 76 65 20 50 32 20 76 61 6c 75 65 20 69 73  tive P2 value is
16736 20 61 20 6c 61 62 65 6c 20 74 68 61 74 20 68 61   a label that ha
16737 73 20 79 65 74 20 74 6f 20 62 65 20 72 65 73 6f  s yet to be reso
16738 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 5a 65 72 6f  lved..**.** Zero
16739 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
1673a 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73  a malloc() fails
1673b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1673c 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
1673d 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64 62  dbeMakeLabel(Vdb
1673e 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  e *p){.  int i;.
1673f 20 20 69 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c 2b    i = p->nLabel+
16740 2b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  +;.  assert( p->
16741 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49  magic==VDBE_MAGI
16742 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20  C_INIT );.  if( 
16743 69 3e 3d 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f  i>=p->nLabelAllo
16744 63 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 4c 61 62  c ){.    p->nLab
16745 65 6c 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 4c 61  elAlloc = p->nLa
16746 62 65 6c 41 6c 6c 6f 63 2a 32 20 2b 20 31 30 3b  belAlloc*2 + 10;
16747 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d  .    p->aLabel =
16748 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f   sqlite3DbReallo
16749 63 4f 72 46 72 65 65 28 70 2d 3e 64 62 2c 20 70  cOrFree(p->db, p
1674a 2d 3e 61 4c 61 62 65 6c 2c 0a 20 20 20 20 20 20  ->aLabel,.      
1674b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1674c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 2d                p-
1674d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63 2a 73 69 7a  >nLabelAlloc*siz
1674e 65 6f 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d  eof(p->aLabel[0]
1674f 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d  ));.  }.  if( p-
16750 3e 61 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70  >aLabel ){.    p
16751 2d 3e 61 4c 61 62 65 6c 5b 69 5d 20 3d 20 2d 31  ->aLabel[i] = -1
16752 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d  ;.  }.  return -
16753 31 2d 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  1-i;.}../*.** Re
16754 73 6f 6c 76 65 20 6c 61 62 65 6c 20 22 78 22 20  solve label "x" 
16755 74 6f 20 62 65 20 74 68 65 20 61 64 64 72 65 73  to be the addres
16756 73 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e  s of the next in
16757 73 74 72 75 63 74 69 6f 6e 20 74 6f 0a 2a 2a 20  struction to.** 
16758 62 65 20 69 6e 73 65 72 74 65 64 2e 20 20 54 68  be inserted.  Th
16759 65 20 70 61 72 61 6d 65 74 65 72 20 22 78 22 20  e parameter "x" 
1675a 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 6f  must have been o
1675b 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20  btained from.** 
1675c 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20  a prior call to 
1675d 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
1675e 61 62 65 6c 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  abel()..*/.SQLIT
1675f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
16760 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
16761 65 4c 61 62 65 6c 28 56 64 62 65 20 2a 70 2c 20  eLabel(Vdbe *p, 
16762 69 6e 74 20 78 29 7b 0a 20 20 69 6e 74 20 6a 20  int x){.  int j 
16763 3d 20 2d 31 2d 78 3b 0a 20 20 61 73 73 65 72 74  = -1-x;.  assert
16764 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45  ( p->magic==VDBE
16765 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20  _MAGIC_INIT );. 
16766 20 61 73 73 65 72 74 28 20 6a 3e 3d 30 20 26 26   assert( j>=0 &&
16767 20 6a 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a   j<p->nLabel );.
16768 20 20 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20    if( p->aLabel 
16769 29 7b 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c  ){.    p->aLabel
1676a 5b 6a 5d 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20  [j] = p->nOp;.  
1676b 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 70 20  }.}../*.** Loop 
1676c 74 68 72 6f 75 67 68 20 74 68 65 20 70 72 6f 67  through the prog
1676d 72 61 6d 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20  ram looking for 
1676e 50 32 20 76 61 6c 75 65 73 20 74 68 61 74 20 61  P2 values that a
1676f 72 65 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 6f  re negative.** o
16770 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69  n jump instructi
16771 6f 6e 73 2e 20 20 45 61 63 68 20 73 75 63 68 20  ons.  Each such 
16772 76 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65 6c  value is a label
16773 2e 20 20 52 65 73 6f 6c 76 65 20 74 68 65 0a 2a  .  Resolve the.*
16774 2a 20 6c 61 62 65 6c 20 62 79 20 73 65 74 74 69  * label by setti
16775 6e 67 20 74 68 65 20 50 32 20 76 61 6c 75 65 20  ng the P2 value 
16776 74 6f 20 69 74 73 20 63 6f 72 72 65 63 74 20 6e  to its correct n
16777 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2e 0a 2a  on-zero value..*
16778 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
16779 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65  e is called once
1677a 20 61 66 74 65 72 20 61 6c 6c 20 6f 70 63 6f 64   after all opcod
1677b 65 73 20 68 61 76 65 20 62 65 65 6e 20 69 6e 73  es have been ins
1677c 65 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 56 61 72  erted..**.** Var
1677d 69 61 62 6c 65 20 2a 70 4d 61 78 46 75 6e 63 41  iable *pMaxFuncA
1677e 72 67 73 20 69 73 20 73 65 74 20 74 6f 20 74 68  rgs is set to th
1677f 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20  e maximum value 
16780 6f 66 20 61 6e 79 20 50 32 20 61 72 67 75 6d 65  of any P2 argume
16781 6e 74 20 0a 2a 2a 20 74 6f 20 61 6e 20 4f 50 5f  nt .** to an OP_
16782 46 75 6e 63 74 69 6f 6e 2c 20 4f 50 5f 41 67 67  Function, OP_Agg
16783 53 74 65 70 20 6f 72 20 4f 50 5f 56 46 69 6c 74  Step or OP_VFilt
16784 65 72 20 6f 70 63 6f 64 65 2e 20 54 68 69 73 20  er opcode. This 
16785 69 73 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 73  is used by .** s
16786 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65  qlite3VdbeMakeRe
16787 61 64 79 28 29 20 74 6f 20 73 69 7a 65 20 74 68  ady() to size th
16788 65 20 56 64 62 65 2e 61 70 41 72 67 5b 5d 20 61  e Vdbe.apArg[] a
16789 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  rray..**.** This
1678a 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 64 6f   routine also do
1678b 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  es the following
1678c 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 3a 20 20   optimization:  
1678d 49 74 20 73 63 61 6e 73 20 66 6f 72 0a 2a 2a 20  It scans for.** 
1678e 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 74 68 61  instructions tha
1678f 74 20 6d 69 67 68 74 20 63 61 75 73 65 20 61 20  t might cause a 
16790 73 74 61 74 65 6d 65 6e 74 20 72 6f 6c 6c 62 61  statement rollba
16791 63 6b 2e 20 20 53 75 63 68 20 69 6e 73 74 72 75  ck.  Such instru
16792 63 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 3a 0a 2a  ctions.** are:.*
16793 2a 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 48 61 6c  *.**   *  OP_Hal
16794 74 20 77 69 74 68 20 50 31 3d 53 51 4c 49 54 45  t with P1=SQLITE
16795 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e 64 20  _CONSTRAINT and 
16796 50 32 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20  P2=OE_Abort..** 
16797 20 20 2a 20 20 4f 50 5f 44 65 73 74 72 6f 79 0a    *  OP_Destroy.
16798 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 55 70 64 61  **   *  OP_VUpda
16799 74 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 52  te.**   *  OP_VR
1679a 65 6e 61 6d 65 0a 2a 2a 0a 2a 2a 20 49 66 20 6e  ename.**.** If n
1679b 6f 20 73 75 63 68 20 69 6e 73 74 72 75 63 74 69  o such instructi
1679c 6f 6e 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 65  on is found, the
1679d 6e 20 65 76 65 72 79 20 53 74 61 74 65 6d 65 6e  n every Statemen
1679e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 0a 2a  t instruction .*
1679f 2a 20 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20  * is changed to 
167a0 61 20 4e 6f 6f 70 2e 20 20 49 6e 20 74 68 69 73  a Noop.  In this
167a1 20 77 61 79 2c 20 77 65 20 61 76 6f 69 64 20 63   way, we avoid c
167a2 72 65 61 74 69 6e 67 20 74 68 65 20 73 74 61 74  reating the stat
167a3 65 6d 65 6e 74 20 0a 2a 2a 20 6a 6f 75 72 6e 61  ement .** journa
167a4 6c 20 66 69 6c 65 20 75 6e 6e 65 63 65 73 73 61  l file unnecessa
167a5 72 69 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rily..*/.static 
167a6 76 6f 69 64 20 72 65 73 6f 6c 76 65 50 32 56 61  void resolveP2Va
167a7 6c 75 65 73 28 56 64 62 65 20 2a 70 2c 20 69 6e  lues(Vdbe *p, in
167a8 74 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 73 29  t *pMaxFuncArgs)
167a9 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  {.  int i;.  int
167aa 20 6e 4d 61 78 41 72 67 73 20 3d 20 30 3b 0a 20   nMaxArgs = 0;. 
167ab 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 6e 74 20   Op *pOp;.  int 
167ac 2a 61 4c 61 62 65 6c 20 3d 20 70 2d 3e 61 4c 61  *aLabel = p->aLa
167ad 62 65 6c 3b 0a 20 20 69 6e 74 20 64 6f 65 73 53  bel;.  int doesS
167ae 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 61 63 6b  tatementRollback
167af 20 3d 20 30 3b 0a 20 20 69 6e 74 20 68 61 73 53   = 0;.  int hasS
167b0 74 61 74 65 6d 65 6e 74 42 65 67 69 6e 20 3d 20  tatementBegin = 
167b1 30 3b 0a 20 20 70 2d 3e 72 65 61 64 4f 6e 6c 79  0;.  p->readOnly
167b2 20 3d 20 31 3b 0a 20 20 70 2d 3e 75 73 65 73 53   = 1;.  p->usesS
167b3 74 6d 74 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a  tmtJournal = 0;.
167b4 20 20 66 6f 72 28 70 4f 70 3d 70 2d 3e 61 4f 70    for(pOp=p->aOp
167b5 2c 20 69 3d 70 2d 3e 6e 4f 70 2d 31 3b 20 69 3e  , i=p->nOp-1; i>
167b6 3d 30 3b 20 69 2d 2d 2c 20 70 4f 70 2b 2b 29 7b  =0; i--, pOp++){
167b7 0a 20 20 20 20 75 38 20 6f 70 63 6f 64 65 20 3d  .    u8 opcode =
167b8 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 0a 20   pOp->opcode;.. 
167b9 20 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f     if( opcode==O
167ba 50 5f 46 75 6e 63 74 69 6f 6e 20 7c 7c 20 6f 70  P_Function || op
167bb 63 6f 64 65 3d 3d 4f 50 5f 41 67 67 53 74 65 70  code==OP_AggStep
167bc 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f   ){.      if( pO
167bd 70 2d 3e 70 35 3e 6e 4d 61 78 41 72 67 73 20 29  p->p5>nMaxArgs )
167be 20 6e 4d 61 78 41 72 67 73 20 3d 20 70 4f 70 2d   nMaxArgs = pOp-
167bf 3e 70 35 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  >p5;.#ifndef SQL
167c0 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
167c1 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65 20  TABLE.    }else 
167c2 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56  if( opcode==OP_V
167c3 55 70 64 61 74 65 20 29 7b 0a 20 20 20 20 20 20  Update ){.      
167c4 69 66 28 20 70 4f 70 2d 3e 70 32 3e 6e 4d 61 78  if( pOp->p2>nMax
167c5 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20  Args ) nMaxArgs 
167c6 3d 20 70 4f 70 2d 3e 70 32 3b 0a 23 65 6e 64 69  = pOp->p2;.#endi
167c7 66 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  f.    }.    if( 
167c8 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48 61 6c 74 20  opcode==OP_Halt 
167c9 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70  ){.      if( pOp
167ca 2d 3e 70 31 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e  ->p1==SQLITE_CON
167cb 53 54 52 41 49 4e 54 20 26 26 20 70 4f 70 2d 3e  STRAINT && pOp->
167cc 70 32 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b 0a  p2==OE_Abort ){.
167cd 20 20 20 20 20 20 20 20 64 6f 65 73 53 74 61 74          doesStat
167ce 65 6d 65 6e 74 52 6f 6c 6c 62 61 63 6b 20 3d 20  ementRollback = 
167cf 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
167d0 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d  else if( opcode=
167d1 3d 4f 50 5f 53 74 61 74 65 6d 65 6e 74 20 29 7b  =OP_Statement ){
167d2 0a 20 20 20 20 20 20 68 61 73 53 74 61 74 65 6d  .      hasStatem
167d3 65 6e 74 42 65 67 69 6e 20 3d 20 31 3b 0a 20 20  entBegin = 1;.  
167d4 20 20 20 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a      p->usesStmtJ
167d5 6f 75 72 6e 61 6c 20 3d 20 31 3b 0a 20 20 20 20  ournal = 1;.    
167d6 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65  }else if( opcode
167d7 3d 3d 4f 50 5f 44 65 73 74 72 6f 79 20 29 7b 0a  ==OP_Destroy ){.
167d8 20 20 20 20 20 20 64 6f 65 73 53 74 61 74 65 6d        doesStatem
167d9 65 6e 74 52 6f 6c 6c 62 61 63 6b 20 3d 20 31 3b  entRollback = 1;
167da 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f  .    }else if( o
167db 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 6e 73 61  pcode==OP_Transa
167dc 63 74 69 6f 6e 20 26 26 20 70 4f 70 2d 3e 70 32  ction && pOp->p2
167dd 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  !=0 ){.      p->
167de 72 65 61 64 4f 6e 6c 79 20 3d 20 30 3b 0a 23 69  readOnly = 0;.#i
167df 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
167e0 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
167e1 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63     }else if( opc
167e2 6f 64 65 3d 3d 4f 50 5f 56 55 70 64 61 74 65 20  ode==OP_VUpdate 
167e3 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 52  || opcode==OP_VR
167e4 65 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 64  ename ){.      d
167e5 6f 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c  oesStatementRoll
167e6 62 61 63 6b 20 3d 20 31 3b 0a 20 20 20 20 7d 65  back = 1;.    }e
167e7 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d  lse if( opcode==
167e8 4f 50 5f 56 46 69 6c 74 65 72 20 29 7b 0a 20 20  OP_VFilter ){.  
167e9 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20      int n;.     
167ea 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 20   assert( p->nOp 
167eb 2d 20 69 20 3e 3d 20 33 20 29 3b 0a 20 20 20 20  - i >= 3 );.    
167ec 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31    assert( pOp[-1
167ed 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 6e 74  ].opcode==OP_Int
167ee 65 67 65 72 20 29 3b 0a 20 20 20 20 20 20 6e 20  eger );.      n 
167ef 3d 20 70 4f 70 5b 2d 31 5d 2e 70 31 3b 0a 20 20  = pOp[-1].p1;.  
167f0 20 20 20 20 69 66 28 20 6e 3e 6e 4d 61 78 41 72      if( n>nMaxAr
167f1 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20 3d 20  gs ) nMaxArgs = 
167f2 6e 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a  n;.#endif.    }.
167f3 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
167f4 56 64 62 65 4f 70 63 6f 64 65 48 61 73 50 72 6f  VdbeOpcodeHasPro
167f5 70 65 72 74 79 28 6f 70 63 6f 64 65 2c 20 4f 50  perty(opcode, OP
167f6 46 4c 47 5f 4a 55 4d 50 29 20 26 26 20 70 4f 70  FLG_JUMP) && pOp
167f7 2d 3e 70 32 3c 30 20 29 7b 0a 20 20 20 20 20 20  ->p2<0 ){.      
167f8 61 73 73 65 72 74 28 20 2d 31 2d 70 4f 70 2d 3e  assert( -1-pOp->
167f9 70 32 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a  p2<p->nLabel );.
167fa 20 20 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20        pOp->p2 = 
167fb 61 4c 61 62 65 6c 5b 2d 31 2d 70 4f 70 2d 3e 70  aLabel[-1-pOp->p
167fc 32 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  2];.    }.  }.  
167fd 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d  sqlite3DbFree(p-
167fe 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 3b  >db, p->aLabel);
167ff 0a 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20 30  .  p->aLabel = 0
16800 3b 0a 0a 20 20 2a 70 4d 61 78 46 75 6e 63 41 72  ;..  *pMaxFuncAr
16801 67 73 20 3d 20 6e 4d 61 78 41 72 67 73 3b 0a 0a  gs = nMaxArgs;..
16802 20 20 2f 2a 20 49 66 20 77 65 20 6e 65 76 65 72    /* If we never
16803 20 72 6f 6c 6c 62 61 63 6b 20 61 20 73 74 61 74   rollback a stat
16804 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
16805 6e 2c 20 74 68 65 6e 20 73 74 61 74 65 6d 65 6e  n, then statemen
16806 74 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69  t.  ** transacti
16807 6f 6e 73 20 61 72 65 20 6e 6f 74 20 6e 65 65 64  ons are not need
16808 65 64 2e 20 20 53 6f 20 63 68 61 6e 67 65 20 65  ed.  So change e
16809 76 65 72 79 20 4f 50 5f 53 74 61 74 65 6d 65 6e  very OP_Statemen
1680a 74 0a 20 20 2a 2a 20 6f 70 63 6f 64 65 20 69 6e  t.  ** opcode in
1680b 74 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70 2e 20 20  to an OP_Noop.  
1680c 54 68 69 73 20 61 76 6f 69 64 20 61 20 63 61 6c  This avoid a cal
1680d 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 4f 70  l to sqlite3OsOp
1680e 65 6e 45 78 63 6c 75 73 69 76 65 28 29 0a 20 20  enExclusive().  
1680f 2a 2a 20 77 68 69 63 68 20 63 61 6e 20 62 65 20  ** which can be 
16810 65 78 70 65 6e 73 69 76 65 20 6f 6e 20 73 6f 6d  expensive on som
16811 65 20 70 6c 61 74 66 6f 72 6d 73 2e 0a 20 20 2a  e platforms..  *
16812 2f 0a 20 20 69 66 28 20 68 61 73 53 74 61 74 65  /.  if( hasState
16813 6d 65 6e 74 42 65 67 69 6e 20 26 26 20 21 64 6f  mentBegin && !do
16814 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62  esStatementRollb
16815 61 63 6b 20 29 7b 0a 20 20 20 20 70 2d 3e 75 73  ack ){.    p->us
16816 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 3d 20  esStmtJournal = 
16817 30 3b 0a 20 20 20 20 66 6f 72 28 70 4f 70 3d 70  0;.    for(pOp=p
16818 2d 3e 61 4f 70 2c 20 69 3d 70 2d 3e 6e 4f 70 2d  ->aOp, i=p->nOp-
16819 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f  1; i>=0; i--, pO
1681a 70 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  p++){.      if( 
1681b 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
1681c 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20 20  Statement ){.   
1681d 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65       pOp->opcode
1681e 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20   = OP_Noop;.    
1681f 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a    }.    }.  }.}.
16820 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
16821 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65  e address of the
16822 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f   next instructio
16823 6e 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64  n to be inserted
16824 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
16825 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
16826 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 56  dbeCurrentAddr(V
16827 64 62 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  dbe *p){.  asser
16828 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42  t( p->magic==VDB
16829 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a  E_MAGIC_INIT );.
1682a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 4f 70 3b    return p->nOp;
1682b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20  .}../*.** Add a 
1682c 77 68 6f 6c 65 20 6c 69 73 74 20 6f 66 20 6f 70  whole list of op
1682d 65 72 61 74 69 6f 6e 73 20 74 6f 20 74 68 65 20  erations to the 
1682e 6f 70 65 72 61 74 69 6f 6e 20 73 74 61 63 6b 2e  operation stack.
1682f 20 20 52 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20    Return the.** 
16830 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 66  address of the f
16831 69 72 73 74 20 6f 70 65 72 61 74 69 6f 6e 20 61  irst operation a
16832 64 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  dded..*/.SQLITE_
16833 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
16834 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
16835 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6e 4f  (Vdbe *p, int nO
16836 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 20 63 6f  p, VdbeOpList co
16837 6e 73 74 20 2a 61 4f 70 29 7b 0a 20 20 69 6e 74  nst *aOp){.  int
16838 20 61 64 64 72 3b 0a 20 20 61 73 73 65 72 74 28   addr;.  assert(
16839 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f   p->magic==VDBE_
1683a 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20  MAGIC_INIT );.  
1683b 69 66 28 20 70 2d 3e 6e 4f 70 20 2b 20 6e 4f 70  if( p->nOp + nOp
1683c 20 3e 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 29   > p->nOpAlloc )
1683d 7b 0a 20 20 20 20 72 65 73 69 7a 65 4f 70 41 72  {.    resizeOpAr
1683e 72 61 79 28 70 2c 20 70 2d 3e 6e 4f 70 41 6c 6c  ray(p, p->nOpAll
1683f 6f 63 20 3f 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63  oc ? p->nOpAlloc
16840 2a 32 20 3a 20 31 30 32 34 2f 73 69 7a 65 6f 66  *2 : 1024/sizeof
16841 28 4f 70 29 29 3b 0a 20 20 20 20 61 73 73 65 72  (Op));.    asser
16842 74 28 20 70 2d 3e 6e 4f 70 2b 6e 4f 70 3c 3d 70  t( p->nOp+nOp<=p
16843 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 7c 7c 20 70 2d  ->nOpAlloc || p-
16844 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
16845 64 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  d );.  }.  if( p
16846 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
16847 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ed ){.    return
16848 20 30 3b 0a 20 20 7d 0a 20 20 61 64 64 72 20 3d   0;.  }.  addr =
16849 20 70 2d 3e 6e 4f 70 3b 0a 20 20 69 66 28 20 6e   p->nOp;.  if( n
1684a 4f 70 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  Op>0 ){.    int 
1684b 69 3b 0a 20 20 20 20 56 64 62 65 4f 70 4c 69 73  i;.    VdbeOpLis
1684c 74 20 63 6f 6e 73 74 20 2a 70 49 6e 20 3d 20 61  t const *pIn = a
1684d 4f 70 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  Op;.    for(i=0;
1684e 20 69 3c 6e 4f 70 3b 20 69 2b 2b 2c 20 70 49 6e   i<nOp; i++, pIn
1684f 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70  ++){.      int p
16850 32 20 3d 20 70 49 6e 2d 3e 70 32 3b 0a 20 20 20  2 = pIn->p2;.   
16851 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 75 74 20     VdbeOp *pOut 
16852 3d 20 26 70 2d 3e 61 4f 70 5b 69 2b 61 64 64 72  = &p->aOp[i+addr
16853 5d 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 6f  ];.      pOut->o
16854 70 63 6f 64 65 20 3d 20 70 49 6e 2d 3e 6f 70 63  pcode = pIn->opc
16855 6f 64 65 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d  ode;.      pOut-
16856 3e 70 31 20 3d 20 70 49 6e 2d 3e 70 31 3b 0a 20  >p1 = pIn->p1;. 
16857 20 20 20 20 20 69 66 28 20 70 32 3c 30 20 26 26       if( p2<0 &&
16858 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f   sqlite3VdbeOpco
16859 64 65 48 61 73 50 72 6f 70 65 72 74 79 28 70 4f  deHasProperty(pO
1685a 75 74 2d 3e 6f 70 63 6f 64 65 2c 20 4f 50 46 4c  ut->opcode, OPFL
1685b 47 5f 4a 55 4d 50 29 20 29 7b 0a 20 20 20 20 20  G_JUMP) ){.     
1685c 20 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 61 64     pOut->p2 = ad
1685d 64 72 20 2b 20 41 44 44 52 28 70 32 29 3b 0a 20  dr + ADDR(p2);. 
1685e 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1685f 20 20 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 70      pOut->p2 = p
16860 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  2;.      }.     
16861 20 70 4f 75 74 2d 3e 70 33 20 3d 20 70 49 6e 2d   pOut->p3 = pIn-
16862 3e 70 33 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d  >p3;.      pOut-
16863 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54  >p4type = P4_NOT
16864 55 53 45 44 3b 0a 20 20 20 20 20 20 70 4f 75 74  USED;.      pOut
16865 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20  ->p4.p = 0;.    
16866 20 20 70 4f 75 74 2d 3e 70 35 20 3d 20 30 3b 0a    pOut->p5 = 0;.
16867 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
16868 42 55 47 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e  BUG.      pOut->
16869 7a 43 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20  zComment = 0;.  
1686a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56      if( sqlite3V
1686b 64 62 65 41 64 64 6f 70 54 72 61 63 65 20 29 7b  dbeAddopTrace ){
1686c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1686d 56 64 62 65 50 72 69 6e 74 4f 70 28 30 2c 20 69  VdbePrintOp(0, i
1686e 2b 61 64 64 72 2c 20 26 70 2d 3e 61 4f 70 5b 69  +addr, &p->aOp[i
1686f 2b 61 64 64 72 5d 29 3b 0a 20 20 20 20 20 20 7d  +addr]);.      }
16870 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20  .#endif.    }.  
16871 20 20 70 2d 3e 6e 4f 70 20 2b 3d 20 6e 4f 70 3b    p->nOp += nOp;
16872 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 64  .  }.  return ad
16873 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61  dr;.}../*.** Cha
16874 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66  nge the value of
16875 20 74 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20   the P1 operand 
16876 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 69  for a specific i
16877 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54  nstruction..** T
16878 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
16879 73 65 66 75 6c 20 77 68 65 6e 20 61 20 6c 61 72  seful when a lar
1687a 67 65 20 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f  ge program is lo
1687b 61 64 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73  aded from a.** s
1687c 74 61 74 69 63 20 61 72 72 61 79 20 75 73 69 6e  tatic array usin
1687d 67 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  g sqlite3VdbeAdd
1687e 4f 70 4c 69 73 74 20 62 75 74 20 77 65 20 77 61  OpList but we wa
1687f 6e 74 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20  nt to make a.** 
16880 66 65 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65  few minor change
16881 73 20 74 6f 20 74 68 65 20 70 72 6f 67 72 61 6d  s to the program
16882 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
16883 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
16884 56 64 62 65 43 68 61 6e 67 65 50 31 28 56 64 62  VdbeChangeP1(Vdb
16885 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20  e *p, int addr, 
16886 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 65  int val){.  asse
16887 72 74 28 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6d  rt( p==0 || p->m
16888 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43  agic==VDBE_MAGIC
16889 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 70  _INIT );.  if( p
1688a 20 26 26 20 61 64 64 72 3e 3d 30 20 26 26 20 70   && addr>=0 && p
1688b 2d 3e 6e 4f 70 3e 61 64 64 72 20 26 26 20 70 2d  ->nOp>addr && p-
1688c 3e 61 4f 70 20 29 7b 0a 20 20 20 20 70 2d 3e 61  >aOp ){.    p->a
1688d 4f 70 5b 61 64 64 72 5d 2e 70 31 20 3d 20 76 61  Op[addr].p1 = va
1688e 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  l;.  }.}../*.** 
1688f 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65  Change the value
16890 20 6f 66 20 74 68 65 20 50 32 20 6f 70 65 72 61   of the P2 opera
16891 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 69  nd for a specifi
16892 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  c instruction..*
16893 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
16894 73 20 75 73 65 66 75 6c 20 66 6f 72 20 73 65 74  s useful for set
16895 74 69 6e 67 20 61 20 6a 75 6d 70 20 64 65 73 74  ting a jump dest
16896 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  ination..*/.SQLI
16897 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
16898 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
16899 65 50 32 28 56 64 62 65 20 2a 70 2c 20 69 6e 74  eP2(Vdbe *p, int
1689a 20 61 64 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b   addr, int val){
1689b 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20  .  assert( p==0 
1689c 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42  || p->magic==VDB
1689d 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a  E_MAGIC_INIT );.
1689e 20 20 69 66 28 20 70 20 26 26 20 61 64 64 72 3e    if( p && addr>
1689f 3d 30 20 26 26 20 70 2d 3e 6e 4f 70 3e 61 64 64  =0 && p->nOp>add
168a0 72 20 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a 20  r && p->aOp ){. 
168a1 20 20 20 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e     p->aOp[addr].
168a2 70 32 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a  p2 = val;.  }.}.
168a3 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
168a4 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50  e value of the P
168a5 33 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 20  3 operand for a 
168a6 73 70 65 63 69 66 69 63 20 69 6e 73 74 72 75 63  specific instruc
168a7 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
168a8 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
168a9 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33  ite3VdbeChangeP3
168aa 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64  (Vdbe *p, int ad
168ab 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20  dr, int val){.  
168ac 61 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20  assert( p==0 || 
168ad 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d  p->magic==VDBE_M
168ae 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69  AGIC_INIT );.  i
168af 66 28 20 70 20 26 26 20 61 64 64 72 3e 3d 30 20  f( p && addr>=0 
168b0 26 26 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 20 26  && p->nOp>addr &
168b1 26 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20  & p->aOp ){.    
168b2 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 33 20  p->aOp[addr].p3 
168b3 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  = val;.  }.}../*
168b4 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76  .** Change the v
168b5 61 6c 75 65 20 6f 66 20 74 68 65 20 50 35 20 6f  alue of the P5 o
168b6 70 65 72 61 6e 64 20 66 6f 72 20 74 68 65 20 6d  perand for the m
168b7 6f 73 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20  ost recently.** 
168b8 61 64 64 65 64 20 6f 70 65 72 61 74 69 6f 6e 2e  added operation.
168b9 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
168ba 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
168bb 64 62 65 43 68 61 6e 67 65 50 35 28 56 64 62 65  dbeChangeP5(Vdbe
168bc 20 2a 70 2c 20 75 38 20 76 61 6c 29 7b 0a 20 20   *p, u8 val){.  
168bd 61 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20  assert( p==0 || 
168be 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d  p->magic==VDBE_M
168bf 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69  AGIC_INIT );.  i
168c0 66 28 20 70 20 26 26 20 70 2d 3e 61 4f 70 20 29  f( p && p->aOp )
168c1 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  {.    assert( p-
168c2 3e 6e 4f 70 3e 30 20 29 3b 0a 20 20 20 20 70 2d  >nOp>0 );.    p-
168c3 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 70  >aOp[p->nOp-1].p
168c4 35 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a  5 = val;.  }.}..
168c5 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
168c6 20 50 32 20 6f 70 65 72 61 6e 64 20 6f 66 20 69   P2 operand of i
168c7 6e 73 74 72 75 63 74 69 6f 6e 20 61 64 64 72 20  nstruction addr 
168c8 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74  so that it point
168c9 73 20 74 6f 0a 2a 2a 20 74 68 65 20 61 64 64 72  s to.** the addr
168ca 65 73 73 20 6f 66 20 74 68 65 20 6e 65 78 74 20  ess of the next 
168cb 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 62  instruction to b
168cc 65 20 63 6f 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49  e coded..*/.SQLI
168cd 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
168ce 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
168cf 65 72 65 28 56 64 62 65 20 2a 70 2c 20 69 6e 74  ere(Vdbe *p, int
168d0 20 61 64 64 72 29 7b 0a 20 20 73 71 6c 69 74 65   addr){.  sqlite
168d1 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 70 2c  3VdbeChangeP2(p,
168d2 20 61 64 64 72 2c 20 70 2d 3e 6e 4f 70 29 3b 0a   addr, p->nOp);.
168d3 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  }.../*.** If the
168d4 20 69 6e 70 75 74 20 46 75 6e 63 44 65 66 20 73   input FuncDef s
168d5 74 72 75 63 74 75 72 65 20 69 73 20 65 70 68 65  tructure is ephe
168d6 6d 65 72 61 6c 2c 20 74 68 65 6e 20 66 72 65 65  meral, then free
168d7 20 69 74 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20   it.  If.** the 
168d8 46 75 6e 63 44 65 66 20 69 73 20 6e 6f 74 20 65  FuncDef is not e
168d9 70 68 65 72 6d 61 6c 2c 20 74 68 65 6e 20 64 6f  phermal, then do
168da 20 6e 6f 74 68 69 6e 67 2e 0a 2a 2f 0a 73 74 61   nothing..*/.sta
168db 74 69 63 20 76 6f 69 64 20 66 72 65 65 45 70 68  tic void freeEph
168dc 65 6d 65 72 61 6c 46 75 6e 63 74 69 6f 6e 28 73  emeralFunction(s
168dd 71 6c 69 74 65 33 20 2a 64 62 2c 20 46 75 6e 63  qlite3 *db, Func
168de 44 65 66 20 2a 70 44 65 66 29 7b 0a 20 20 69 66  Def *pDef){.  if
168df 28 20 70 44 65 66 20 26 26 20 28 70 44 65 66 2d  ( pDef && (pDef-
168e0 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
168e1 46 55 4e 43 5f 45 50 48 45 4d 29 21 3d 30 20 29  FUNC_EPHEM)!=0 )
168e2 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  {.    sqlite3DbF
168e3 72 65 65 28 64 62 2c 20 70 44 65 66 29 3b 0a 20  ree(db, pDef);. 
168e4 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65   }.}../*.** Dele
168e5 74 65 20 61 20 50 34 20 76 61 6c 75 65 20 69 66  te a P4 value if
168e6 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73   necessary..*/.s
168e7 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 50  tatic void freeP
168e8 34 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  4(sqlite3 *db, i
168e9 6e 74 20 70 34 74 79 70 65 2c 20 76 6f 69 64 20  nt p4type, void 
168ea 2a 70 34 29 7b 0a 20 20 69 66 28 20 70 34 20 29  *p4){.  if( p4 )
168eb 7b 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 34  {.    switch( p4
168ec 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61  type ){.      ca
168ed 73 65 20 50 34 5f 52 45 41 4c 3a 0a 20 20 20 20  se P4_REAL:.    
168ee 20 20 63 61 73 65 20 50 34 5f 49 4e 54 36 34 3a    case P4_INT64:
168ef 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 4d  .      case P4_M
168f0 50 52 49 4e 54 46 3a 0a 20 20 20 20 20 20 63 61  PRINTF:.      ca
168f1 73 65 20 50 34 5f 44 59 4e 41 4d 49 43 3a 0a 20  se P4_DYNAMIC:. 
168f2 20 20 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59       case P4_KEY
168f3 49 4e 46 4f 3a 0a 20 20 20 20 20 20 63 61 73 65  INFO:.      case
168f4 20 50 34 5f 49 4e 54 41 52 52 41 59 3a 0a 20 20   P4_INTARRAY:.  
168f5 20 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49      case P4_KEYI
168f6 4e 46 4f 5f 48 41 4e 44 4f 46 46 3a 20 7b 0a 20  NFO_HANDOFF: {. 
168f7 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
168f8 46 72 65 65 28 64 62 2c 20 70 34 29 3b 0a 20 20  Free(db, p4);.  
168f9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
168fa 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20     }.      case 
168fb 50 34 5f 56 44 42 45 46 55 4e 43 3a 20 7b 0a 20  P4_VDBEFUNC: {. 
168fc 20 20 20 20 20 20 20 56 64 62 65 46 75 6e 63 20         VdbeFunc 
168fd 2a 70 56 64 62 65 46 75 6e 63 20 3d 20 28 56 64  *pVdbeFunc = (Vd
168fe 62 65 46 75 6e 63 20 2a 29 70 34 3b 0a 20 20 20  beFunc *)p4;.   
168ff 20 20 20 20 20 66 72 65 65 45 70 68 65 6d 65 72       freeEphemer
16900 61 6c 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70  alFunction(db, p
16901 56 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 29  VdbeFunc->pFunc)
16902 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
16903 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 44 61  3VdbeDeleteAuxDa
16904 74 61 28 70 56 64 62 65 46 75 6e 63 2c 20 30 29  ta(pVdbeFunc, 0)
16905 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
16906 33 44 62 46 72 65 65 28 64 62 2c 20 70 56 64 62  3DbFree(db, pVdb
16907 65 46 75 6e 63 29 3b 0a 20 20 20 20 20 20 20 20  eFunc);.        
16908 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
16909 20 20 20 20 20 63 61 73 65 20 50 34 5f 46 55 4e       case P4_FUN
1690a 43 44 45 46 3a 20 7b 0a 20 20 20 20 20 20 20 20  CDEF: {.        
1690b 66 72 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e  freeEphemeralFun
1690c 63 74 69 6f 6e 28 64 62 2c 20 28 46 75 6e 63 44  ction(db, (FuncD
1690d 65 66 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20  ef*)p4);.       
1690e 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
1690f 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 4d 45        case P4_ME
16910 4d 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  M: {.        sql
16911 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 28 73  ite3ValueFree((s
16912 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 70 34  qlite3_value*)p4
16913 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
16914 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
16915 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68    }.}.../*.** Ch
16916 61 6e 67 65 20 4e 20 6f 70 63 6f 64 65 73 20 73  ange N opcodes s
16917 74 61 72 74 69 6e 67 20 61 74 20 61 64 64 72 20  tarting at addr 
16918 74 6f 20 4e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 53 51  to No-ops..*/.SQ
16919 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1691a 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61  d sqlite3VdbeCha
1691b 6e 67 65 54 6f 4e 6f 6f 70 28 56 64 62 65 20 2a  ngeToNoop(Vdbe *
1691c 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74  p, int addr, int
1691d 20 4e 29 7b 0a 20 20 69 66 28 20 70 20 26 26 20   N){.  if( p && 
1691e 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20 56 64  p->aOp ){.    Vd
1691f 62 65 4f 70 20 2a 70 4f 70 20 3d 20 26 70 2d 3e  beOp *pOp = &p->
16920 61 4f 70 5b 61 64 64 72 5d 3b 0a 20 20 20 20 73  aOp[addr];.    s
16921 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e  qlite3 *db = p->
16922 64 62 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 4e  db;.    while( N
16923 2d 2d 20 29 7b 0a 20 20 20 20 20 20 66 72 65 65  -- ){.      free
16924 50 34 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79  P4(db, pOp->p4ty
16925 70 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a  pe, pOp->p4.p);.
16926 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 4f 70        memset(pOp
16927 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 4f 70 5b  , 0, sizeof(pOp[
16928 30 5d 29 29 3b 0a 20 20 20 20 20 20 70 4f 70 2d  0]));.      pOp-
16929 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f  >opcode = OP_Noo
1692a 70 3b 0a 20 20 20 20 20 20 70 4f 70 2b 2b 3b 0a  p;.      pOp++;.
1692b 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
1692c 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61  ** Change the va
1692d 6c 75 65 20 6f 66 20 74 68 65 20 50 34 20 6f 70  lue of the P4 op
1692e 65 72 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63  erand for a spec
1692f 69 66 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e  ific instruction
16930 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
16931 65 20 69 73 20 75 73 65 66 75 6c 20 77 68 65 6e  e is useful when
16932 20 61 20 6c 61 72 67 65 20 70 72 6f 67 72 61 6d   a large program
16933 20 69 73 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20   is loaded from 
16934 61 0a 2a 2a 20 73 74 61 74 69 63 20 61 72 72 61  a.** static arra
16935 79 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 56  y using sqlite3V
16936 64 62 65 41 64 64 4f 70 4c 69 73 74 20 62 75 74  dbeAddOpList but
16937 20 77 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65   we want to make
16938 20 61 0a 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20   a.** few minor 
16939 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 70  changes to the p
1693a 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66  rogram..**.** If
1693b 20 6e 3e 3d 30 20 74 68 65 6e 20 74 68 65 20 50   n>=0 then the P
1693c 34 20 6f 70 65 72 61 6e 64 20 69 73 20 64 79 6e  4 operand is dyn
1693d 61 6d 69 63 2c 20 6d 65 61 6e 69 6e 67 20 74 68  amic, meaning th
1693e 61 74 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20  at a copy of.** 
1693f 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 6d 61  the string is ma
16940 64 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f  de into memory o
16941 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c  btained from sql
16942 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a  ite3_malloc()..*
16943 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 6e 3d 3d  * A value of n==
16944 30 20 6d 65 61 6e 73 20 63 6f 70 79 20 62 79 74  0 means copy byt
16945 65 73 20 6f 66 20 7a 50 34 20 75 70 20 74 6f 20  es of zP4 up to 
16946 61 6e 64 20 69 6e 63 6c 75 64 69 6e 67 20 74 68  and including th
16947 65 0a 2a 2a 20 66 69 72 73 74 20 6e 75 6c 6c 20  e.** first null 
16948 62 79 74 65 2e 20 20 49 66 20 6e 3e 30 20 74 68  byte.  If n>0 th
16949 65 6e 20 63 6f 70 79 20 6e 2b 31 20 62 79 74 65  en copy n+1 byte
1694a 73 20 6f 66 20 7a 50 34 2e 0a 2a 2a 0a 2a 2a 20  s of zP4..**.** 
1694b 49 66 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f  If n==P4_KEYINFO
1694c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 7a   it means that z
1694d 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  P4 is a pointer 
1694e 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72  to a KeyInfo str
1694f 75 63 74 75 72 65 2e 0a 2a 2a 20 41 20 63 6f 70  ucture..** A cop
16950 79 20 69 73 20 6d 61 64 65 20 6f 66 20 74 68 65  y is made of the
16951 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75   KeyInfo structu
16952 72 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f  re into memory o
16953 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20  btained from.** 
16954 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2c 20  sqlite3_malloc, 
16955 74 6f 20 62 65 20 66 72 65 65 64 20 77 68 65 6e  to be freed when
16956 20 74 68 65 20 56 64 62 65 20 69 73 20 66 69 6e   the Vdbe is fin
16957 61 6c 69 7a 65 64 2e 0a 2a 2a 20 6e 3d 3d 50 34  alized..** n==P4
16958 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
16959 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20   indicates that 
1695a 7a 50 34 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  zP4 points to a 
1695b 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
1695c 65 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 6d  e.** stored in m
1695d 65 6d 6f 72 79 20 74 68 61 74 20 74 68 65 20 63  emory that the c
1695e 61 6c 6c 65 72 20 68 61 73 20 6f 62 74 61 69 6e  aller has obtain
1695f 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  ed from sqlite3_
16960 6d 61 6c 6c 6f 63 2e 20 54 68 65 20 0a 2a 2a 20  malloc. The .** 
16961 63 61 6c 6c 65 72 20 73 68 6f 75 6c 64 20 6e 6f  caller should no
16962 74 20 66 72 65 65 20 74 68 65 20 61 6c 6c 6f 63  t free the alloc
16963 61 74 69 6f 6e 2c 20 69 74 20 77 69 6c 6c 20 62  ation, it will b
16964 65 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 65  e freed when the
16965 20 56 64 62 65 20 69 73 0a 2a 2a 20 66 69 6e 61   Vdbe is.** fina
16966 6c 69 7a 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 4f 74  lized..** .** Ot
16967 68 65 72 20 76 61 6c 75 65 73 20 6f 66 20 6e 20  her values of n 
16968 28 50 34 5f 53 54 41 54 49 43 2c 20 50 34 5f 43  (P4_STATIC, P4_C
16969 4f 4c 4c 53 45 51 20 65 74 63 2e 29 20 69 6e 64  OLLSEQ etc.) ind
1696a 69 63 61 74 65 20 74 68 61 74 20 7a 50 34 20 70  icate that zP4 p
1696b 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 61 20 73 74  oints.** to a st
1696c 72 69 6e 67 20 6f 72 20 73 74 72 75 63 74 75 72  ring or structur
1696d 65 20 74 68 61 74 20 69 73 20 67 75 61 72 61 6e  e that is guaran
1696e 74 65 65 64 20 74 6f 20 65 78 69 73 74 20 66 6f  teed to exist fo
1696f 72 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f  r the lifetime o
16970 66 0a 2a 2a 20 74 68 65 20 56 64 62 65 2e 20 49  f.** the Vdbe. I
16971 6e 20 74 68 65 73 65 20 63 61 73 65 73 20 77 65  n these cases we
16972 20 63 61 6e 20 6a 75 73 74 20 63 6f 70 79 20 74   can just copy t
16973 68 65 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a  he pointer..**.*
16974 2a 20 49 66 20 61 64 64 72 3c 30 20 74 68 65 6e  * If addr<0 then
16975 20 63 68 61 6e 67 65 20 50 34 20 6f 6e 20 74 68   change P4 on th
16976 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20  e most recently 
16977 69 6e 73 65 72 74 65 64 20 69 6e 73 74 72 75 63  inserted instruc
16978 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
16979 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1697a 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
1697b 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64  (Vdbe *p, int ad
1697c 64 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  dr, const char *
1697d 7a 50 34 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 4f  zP4, int n){.  O
1697e 70 20 2a 70 4f 70 3b 0a 20 20 73 71 6c 69 74 65  p *pOp;.  sqlite
1697f 33 20 2a 64 62 3b 0a 20 20 61 73 73 65 72 74 28  3 *db;.  assert(
16980 20 70 21 3d 30 20 29 3b 0a 20 20 64 62 20 3d 20   p!=0 );.  db = 
16981 70 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28  p->db;.  assert(
16982 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f   p->magic==VDBE_
16983 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20  MAGIC_INIT );.  
16984 69 66 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c  if( p->aOp==0 ||
16985 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
16986 64 20 29 7b 0a 20 20 20 20 69 66 20 28 6e 20 21  d ){.    if (n !
16987 3d 20 50 34 5f 4b 45 59 49 4e 46 4f 29 20 7b 0a  = P4_KEYINFO) {.
16988 20 20 20 20 20 20 66 72 65 65 50 34 28 64 62 2c        freeP4(db,
16989 20 6e 2c 20 28 76 6f 69 64 2a 29 2a 28 63 68 61   n, (void*)*(cha
1698a 72 2a 2a 29 26 7a 50 34 29 3b 0a 20 20 20 20 7d  r**)&zP4);.    }
1698b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
1698c 0a 20 20 61 73 73 65 72 74 28 20 61 64 64 72 3c  .  assert( addr<
1698d 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 69 66 28 20  p->nOp );.  if( 
1698e 61 64 64 72 3c 30 20 29 7b 0a 20 20 20 20 61 64  addr<0 ){.    ad
1698f 64 72 20 3d 20 70 2d 3e 6e 4f 70 20 2d 20 31 3b  dr = p->nOp - 1;
16990 0a 20 20 20 20 69 66 28 20 61 64 64 72 3c 30 20  .    if( addr<0 
16991 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20  ) return;.  }.  
16992 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 61 64  pOp = &p->aOp[ad
16993 64 72 5d 3b 0a 20 20 66 72 65 65 50 34 28 64 62  dr];.  freeP4(db
16994 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70  , pOp->p4type, p
16995 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20 20 70 4f 70  Op->p4.p);.  pOp
16996 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 69 66  ->p4.p = 0;.  if
16997 28 20 6e 3d 3d 50 34 5f 49 4e 54 33 32 20 29 7b  ( n==P4_INT32 ){
16998 0a 20 20 20 20 2f 2a 20 4e 6f 74 65 3a 20 74 68  .    /* Note: th
16999 69 73 20 63 61 73 74 20 69 73 20 73 61 66 65 2c  is cast is safe,
1699a 20 62 65 63 61 75 73 65 20 74 68 65 20 6f 72 69   because the ori
1699b 67 69 6e 20 64 61 74 61 20 70 6f 69 6e 74 20 77  gin data point w
1699c 61 73 20 61 6e 20 69 6e 74 0a 20 20 20 20 2a 2a  as an int.    **
1699d 20 74 68 61 74 20 77 61 73 20 63 61 73 74 20 74   that was cast t
1699e 6f 20 61 20 28 63 6f 6e 73 74 20 63 68 61 72 20  o a (const char 
1699f 2a 29 2e 20 2a 2f 0a 20 20 20 20 70 4f 70 2d 3e  *). */.    pOp->
169a0 70 34 2e 69 20 3d 20 53 51 4c 49 54 45 5f 50 54  p4.i = SQLITE_PT
169a1 52 5f 54 4f 5f 49 4e 54 28 7a 50 34 29 3b 0a 20  R_TO_INT(zP4);. 
169a2 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d     pOp->p4type =
169a3 20 6e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   n;.  }else if( 
169a4 7a 50 34 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4f  zP4==0 ){.    pO
169a5 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20  p->p4.p = 0;.   
169a6 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50   pOp->p4type = P
169a7 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 7d 65 6c  4_NOTUSED;.  }el
169a8 73 65 20 69 66 28 20 6e 3d 3d 50 34 5f 4b 45 59  se if( n==P4_KEY
169a9 49 4e 46 4f 20 29 7b 0a 20 20 20 20 4b 65 79 49  INFO ){.    KeyI
169aa 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20  nfo *pKeyInfo;. 
169ab 20 20 20 69 6e 74 20 6e 46 69 65 6c 64 2c 20 6e     int nField, n
169ac 42 79 74 65 3b 0a 0a 20 20 20 20 6e 46 69 65 6c  Byte;..    nFiel
169ad 64 20 3d 20 28 28 4b 65 79 49 6e 66 6f 2a 29 7a  d = ((KeyInfo*)z
169ae 50 34 29 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 20  P4)->nField;.   
169af 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28   nByte = sizeof(
169b0 2a 70 4b 65 79 49 6e 66 6f 29 20 2b 20 28 6e 46  *pKeyInfo) + (nF
169b1 69 65 6c 64 2d 31 29 2a 73 69 7a 65 6f 66 28 70  ield-1)*sizeof(p
169b2 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 30  KeyInfo->aColl[0
169b3 5d 29 20 2b 20 6e 46 69 65 6c 64 3b 0a 20 20 20  ]) + nField;.   
169b4 20 70 4b 65 79 49 6e 66 6f 20 3d 20 73 71 6c 69   pKeyInfo = sqli
169b5 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 42 79 74 65  te3Malloc( nByte
169b6 20 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e   );.    pOp->p4.
169b7 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79 49  pKeyInfo = pKeyI
169b8 6e 66 6f 3b 0a 20 20 20 20 69 66 28 20 70 4b 65  nfo;.    if( pKe
169b9 79 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 75  yInfo ){.      u
169ba 38 20 2a 61 53 6f 72 74 4f 72 64 65 72 3b 0a 20  8 *aSortOrder;. 
169bb 20 20 20 20 20 6d 65 6d 63 70 79 28 70 4b 65 79       memcpy(pKey
169bc 49 6e 66 6f 2c 20 7a 50 34 2c 20 6e 42 79 74 65  Info, zP4, nByte
169bd 29 3b 0a 20 20 20 20 20 20 61 53 6f 72 74 4f 72  );.      aSortOr
169be 64 65 72 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e  der = pKeyInfo->
169bf 61 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20  aSortOrder;.    
169c0 20 20 69 66 28 20 61 53 6f 72 74 4f 72 64 65 72    if( aSortOrder
169c1 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4b 65 79   ){.        pKey
169c2 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72  Info->aSortOrder
169c3 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
169c4 72 2a 29 26 70 4b 65 79 49 6e 66 6f 2d 3e 61 43  r*)&pKeyInfo->aC
169c5 6f 6c 6c 5b 6e 46 69 65 6c 64 5d 3b 0a 20 20 20  oll[nField];.   
169c6 20 20 20 20 20 6d 65 6d 63 70 79 28 70 4b 65 79       memcpy(pKey
169c7 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72  Info->aSortOrder
169c8 2c 20 61 53 6f 72 74 4f 72 64 65 72 2c 20 6e 46  , aSortOrder, nF
169c9 69 65 6c 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ield);.      }. 
169ca 20 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65       pOp->p4type
169cb 20 3d 20 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20   = P4_KEYINFO;. 
169cc 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
169cd 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  p->db->mallocFai
169ce 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 70  led = 1;.      p
169cf 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f  Op->p4type = P4_
169d0 4e 4f 54 55 53 45 44 3b 0a 20 20 20 20 7d 0a 20  NOTUSED;.    }. 
169d1 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 50 34   }else if( n==P4
169d2 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
169d3 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e   ){.    pOp->p4.
169d4 70 20 3d 20 28 76 6f 69 64 2a 29 7a 50 34 3b 0a  p = (void*)zP4;.
169d5 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20      pOp->p4type 
169d6 3d 20 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20  = P4_KEYINFO;.  
169d7 7d 65 6c 73 65 20 69 66 28 20 6e 3c 30 20 29 7b  }else if( n<0 ){
169d8 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d  .    pOp->p4.p =
169d9 20 28 76 6f 69 64 2a 29 7a 50 34 3b 0a 20 20 20   (void*)zP4;.   
169da 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 6e   pOp->p4type = n
169db 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
169dc 66 28 20 6e 3d 3d 30 20 29 20 6e 20 3d 20 73 74  f( n==0 ) n = st
169dd 72 6c 65 6e 28 7a 50 34 29 3b 0a 20 20 20 20 70  rlen(zP4);.    p
169de 4f 70 2d 3e 70 34 2e 7a 20 3d 20 73 71 6c 69 74  Op->p4.z = sqlit
169df 65 33 44 62 53 74 72 4e 44 75 70 28 70 2d 3e 64  e3DbStrNDup(p->d
169e0 62 2c 20 7a 50 34 2c 20 6e 29 3b 0a 20 20 20 20  b, zP4, n);.    
169e1 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34  pOp->p4type = P4
169e2 5f 44 59 4e 41 4d 49 43 3b 0a 20 20 7d 0a 7d 0a  _DYNAMIC;.  }.}.
169e3 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
169e4 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
169e5 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68 65 20   comment on the 
169e6 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  the most recentl
169e7 79 20 63 6f 64 65 64 20 69 6e 73 74 72 75 63 74  y coded instruct
169e8 69 6f 6e 2e 20 20 4f 72 0a 2a 2a 20 69 6e 73 65  ion.  Or.** inse
169e9 72 74 20 61 20 4e 6f 2d 6f 70 20 61 6e 64 20 61  rt a No-op and a
169ea 64 64 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 74  dd the comment t
169eb 6f 20 74 68 61 74 20 6e 65 77 20 69 6e 73 74 72  o that new instr
169ec 75 63 74 69 6f 6e 2e 20 20 54 68 69 73 0a 2a 2a  uction.  This.**
169ed 20 6d 61 6b 65 73 20 74 68 65 20 63 6f 64 65 20   makes the code 
169ee 65 61 73 69 65 72 20 74 6f 20 72 65 61 64 20 64  easier to read d
169ef 75 72 69 6e 67 20 64 65 62 75 67 67 69 6e 67 2e  uring debugging.
169f0 20 20 4e 6f 6e 65 20 6f 66 20 74 68 69 73 20 68    None of this h
169f1 61 70 70 65 6e 73 0a 2a 2a 20 69 6e 20 61 20 70  appens.** in a p
169f2 72 6f 64 75 63 74 69 6f 6e 20 62 75 69 6c 64 2e  roduction build.
169f3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
169f4 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
169f5 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65 20  dbeComment(Vdbe 
169f6 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  *p, const char *
169f7 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20  zFormat, ...){. 
169f8 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 61   va_list ap;.  a
169f9 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20  ssert( p->nOp>0 
169fa 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b 0a  || p->aOp==0 );.
169fb 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4f 70    assert( p->aOp
169fc 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70 2d  ==0 || p->aOp[p-
169fd 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74  >nOp-1].zComment
169fe 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61  ==0 || p->db->ma
169ff 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
16a00 69 66 28 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20  if( p->nOp ){.  
16a01 20 20 63 68 61 72 20 2a 2a 70 7a 20 3d 20 26 70    char **pz = &p
16a02 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e  ->aOp[p->nOp-1].
16a03 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 76 61  zComment;.    va
16a04 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d  _start(ap, zForm
16a05 61 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  at);.    sqlite3
16a06 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 2a 70  DbFree(p->db, *p
16a07 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 3d 20 73 71  z);.    *pz = sq
16a08 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 70 2d  lite3VMPrintf(p-
16a09 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70  >db, zFormat, ap
16a0a 29 3b 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 70  );.    va_end(ap
16a0b 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f  );.  }.}.SQLITE_
16a0c 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
16a0d 69 74 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d  ite3VdbeNoopComm
16a0e 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 63 6f 6e  ent(Vdbe *p, con
16a0f 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74  st char *zFormat
16a10 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73  , ...){.  va_lis
16a11 74 20 61 70 3b 0a 20 20 73 71 6c 69 74 65 33 56  t ap;.  sqlite3V
16a12 64 62 65 41 64 64 4f 70 30 28 70 2c 20 4f 50 5f  dbeAddOp0(p, OP_
16a13 4e 6f 6f 70 29 3b 0a 20 20 61 73 73 65 72 74 28  Noop);.  assert(
16a14 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c 20 70 2d 3e   p->nOp>0 || p->
16a15 61 4f 70 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  aOp==0 );.  asse
16a16 72 74 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c  rt( p->aOp==0 ||
16a17 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31   p->aOp[p->nOp-1
16a18 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d 30 20 7c 7c  ].zComment==0 ||
16a19 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   p->db->mallocFa
16a1a 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70 2d  iled );.  if( p-
16a1b 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 63 68 61 72  >nOp ){.    char
16a1c 20 2a 2a 70 7a 20 3d 20 26 70 2d 3e 61 4f 70 5b   **pz = &p->aOp[
16a1d 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65  p->nOp-1].zComme
16a1e 6e 74 3b 0a 20 20 20 20 76 61 5f 73 74 61 72 74  nt;.    va_start
16a1f 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20  (ap, zFormat);. 
16a20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
16a21 28 70 2d 3e 64 62 2c 20 2a 70 7a 29 3b 0a 20 20  (p->db, *pz);.  
16a22 20 20 2a 70 7a 20 3d 20 73 71 6c 69 74 65 33 56    *pz = sqlite3V
16a23 4d 50 72 69 6e 74 66 28 70 2d 3e 64 62 2c 20 7a  MPrintf(p->db, z
16a24 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 20  Format, ap);.   
16a25 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 7d   va_end(ap);.  }
16a26 0a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20 4e 44  .}.#endif  /* ND
16a27 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52  EBUG */../*.** R
16a28 65 74 75 72 6e 20 74 68 65 20 6f 70 63 6f 64 65  eturn the opcode
16a29 20 66 6f 72 20 61 20 67 69 76 65 6e 20 61 64 64   for a given add
16a2a 72 65 73 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ress..*/.SQLITE_
16a2b 50 52 49 56 41 54 45 20 56 64 62 65 4f 70 20 2a  PRIVATE VdbeOp *
16a2c 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70  sqlite3VdbeGetOp
16a2d 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64  (Vdbe *p, int ad
16a2e 64 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  dr){.  assert( p
16a2f 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41  ->magic==VDBE_MA
16a30 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 61 73  GIC_INIT );.  as
16a31 73 65 72 74 28 20 28 61 64 64 72 3e 3d 30 20 26  sert( (addr>=0 &
16a32 26 20 61 64 64 72 3c 70 2d 3e 6e 4f 70 29 20 7c  & addr<p->nOp) |
16a33 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  | p->db->mallocF
16a34 61 69 6c 65 64 20 29 3b 0a 20 20 72 65 74 75 72  ailed );.  retur
16a35 6e 20 28 28 61 64 64 72 3e 3d 30 20 26 26 20 61  n ((addr>=0 && a
16a36 64 64 72 3c 70 2d 3e 6e 4f 70 29 3f 28 26 70 2d  ddr<p->nOp)?(&p-
16a37 3e 61 4f 70 5b 61 64 64 72 5d 29 3a 30 29 3b 0a  >aOp[addr]):0);.
16a38 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  }..#if !defined(
16a39 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c  SQLITE_OMIT_EXPL
16a3a 41 49 4e 29 20 7c 7c 20 21 64 65 66 69 6e 65 64  AIN) || !defined
16a3b 28 4e 44 45 42 55 47 29 20 5c 0a 20 20 20 20 20  (NDEBUG) \.     
16a3c 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f  || defined(VDBE_
16a3d 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66 69  PROFILE) || defi
16a3e 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
16a3f 29 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20  )./*.** Compute 
16a40 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 64 65  a string that de
16a41 73 63 72 69 62 65 73 20 74 68 65 20 50 34 20 70  scribes the P4 p
16a42 61 72 61 6d 65 74 65 72 20 66 6f 72 20 61 6e 20  arameter for an 
16a43 6f 70 63 6f 64 65 2e 0a 2a 2a 20 55 73 65 20 7a  opcode..** Use z
16a44 54 65 6d 70 20 66 6f 72 20 61 6e 79 20 72 65 71  Temp for any req
16a45 75 69 72 65 64 20 74 65 6d 70 6f 72 61 72 79 20  uired temporary 
16a46 62 75 66 66 65 72 20 73 70 61 63 65 2e 0a 2a 2f  buffer space..*/
16a47 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 64 69  .static char *di
16a48 73 70 6c 61 79 50 34 28 4f 70 20 2a 70 4f 70 2c  splayP4(Op *pOp,
16a49 20 63 68 61 72 20 2a 7a 54 65 6d 70 2c 20 69 6e   char *zTemp, in
16a4a 74 20 6e 54 65 6d 70 29 7b 0a 20 20 63 68 61 72  t nTemp){.  char
16a4b 20 2a 7a 50 34 20 3d 20 7a 54 65 6d 70 3b 0a 20   *zP4 = zTemp;. 
16a4c 20 61 73 73 65 72 74 28 20 6e 54 65 6d 70 3e 3d   assert( nTemp>=
16a4d 32 30 20 29 3b 0a 20 20 73 77 69 74 63 68 28 20  20 );.  switch( 
16a4e 70 4f 70 2d 3e 70 34 74 79 70 65 20 29 7b 0a 20  pOp->p4type ){. 
16a4f 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e     case P4_KEYIN
16a50 46 4f 5f 53 54 41 54 49 43 3a 0a 20 20 20 20 63  FO_STATIC:.    c
16a51 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f 3a 20  ase P4_KEYINFO: 
16a52 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 2c 20 6a  {.      int i, j
16a53 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20  ;.      KeyInfo 
16a54 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d  *pKeyInfo = pOp-
16a55 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  >p4.pKeyInfo;.  
16a56 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
16a57 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d  intf(nTemp, zTem
16a58 70 2c 20 22 6b 65 79 69 6e 66 6f 28 25 64 22 2c  p, "keyinfo(%d",
16a59 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c   pKeyInfo->nFiel
16a5a 64 29 3b 0a 20 20 20 20 20 20 69 20 3d 20 73 74  d);.      i = st
16a5b 72 6c 65 6e 28 7a 54 65 6d 70 29 3b 0a 20 20 20  rlen(zTemp);.   
16a5c 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4b     for(j=0; j<pK
16a5d 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 3b 20  eyInfo->nField; 
16a5e 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 43 6f  j++){.        Co
16a5f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70  llSeq *pColl = p
16a60 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6a  KeyInfo->aColl[j
16a61 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ];.        if( p
16a62 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  Coll ){.        
16a63 20 20 69 6e 74 20 6e 20 3d 20 73 74 72 6c 65 6e    int n = strlen
16a64 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a  (pColl->zName);.
16a65 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 2b            if( i+
16a66 6e 3e 6e 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20  n>nTemp-6 ){.   
16a67 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28           memcpy(
16a68 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c 2e 2e 2e 22  &zTemp[i],",..."
16a69 2c 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ,4);.           
16a6a 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
16a6b 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7a 54    }.          zT
16a6c 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2c 27 3b 0a  emp[i++] = ',';.
16a6d 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 4b            if( pK
16a6e 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64  eyInfo->aSortOrd
16a6f 65 72 20 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e  er && pKeyInfo->
16a70 61 53 6f 72 74 4f 72 64 65 72 5b 6a 5d 20 29 7b  aSortOrder[j] ){
16a71 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a 54 65  .            zTe
16a72 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2d 27 3b 0a 20  mp[i++] = '-';. 
16a73 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
16a74 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65       memcpy(&zTe
16a75 6d 70 5b 69 5d 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e  mp[i], pColl->zN
16a76 61 6d 65 2c 6e 2b 31 29 3b 0a 20 20 20 20 20 20  ame,n+1);.      
16a77 20 20 20 20 69 20 2b 3d 20 6e 3b 0a 20 20 20 20      i += n;.    
16a78 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 2b      }else if( i+
16a79 34 3c 6e 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20  4<nTemp-6 ){.   
16a7a 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a         memcpy(&z
16a7b 54 65 6d 70 5b 69 5d 2c 22 2c 6e 69 6c 22 2c 34  Temp[i],",nil",4
16a7c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 20 2b  );.          i +
16a7d 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 4;.        }. 
16a7e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 54 65       }.      zTe
16a7f 6d 70 5b 69 2b 2b 5d 20 3d 20 27 29 27 3b 0a 20  mp[i++] = ')';. 
16a80 20 20 20 20 20 7a 54 65 6d 70 5b 69 5d 20 3d 20       zTemp[i] = 
16a81 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  0;.      assert(
16a82 20 69 3c 6e 54 65 6d 70 20 29 3b 0a 20 20 20 20   i<nTemp );.    
16a83 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
16a84 20 20 20 63 61 73 65 20 50 34 5f 43 4f 4c 4c 53     case P4_COLLS
16a85 45 51 3a 20 7b 0a 20 20 20 20 20 20 43 6f 6c 6c  EQ: {.      Coll
16a86 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 4f 70  Seq *pColl = pOp
16a87 2d 3e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 20 20  ->p4.pColl;.    
16a88 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
16a89 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c  tf(nTemp, zTemp,
16a8a 20 22 63 6f 6c 6c 73 65 71 28 25 2e 32 30 73 29   "collseq(%.20s)
16a8b 22 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29  ", pColl->zName)
16a8c 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
16a8d 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34     }.    case P4
16a8e 5f 46 55 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20  _FUNCDEF: {.    
16a8f 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 20    FuncDef *pDef 
16a90 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b  = pOp->p4.pFunc;
16a91 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
16a92 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a  nprintf(nTemp, z
16a93 54 65 6d 70 2c 20 22 25 73 28 25 64 29 22 2c 20  Temp, "%s(%d)", 
16a94 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65  pDef->zName, pDe
16a95 66 2d 3e 6e 41 72 67 29 3b 0a 20 20 20 20 20 20  f->nArg);.      
16a96 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
16a97 20 63 61 73 65 20 50 34 5f 49 4e 54 36 34 3a 20   case P4_INT64: 
16a98 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
16a99 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20  snprintf(nTemp, 
16a9a 7a 54 65 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 2a  zTemp, "%lld", *
16a9b 70 4f 70 2d 3e 70 34 2e 70 49 36 34 29 3b 0a 20  pOp->p4.pI64);. 
16a9c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
16a9d 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e  }.    case P4_IN
16a9e 54 33 32 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  T32: {.      sql
16a9f 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54  ite3_snprintf(nT
16aa0 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 64 22  emp, zTemp, "%d"
16aa1 2c 20 70 4f 70 2d 3e 70 34 2e 69 29 3b 0a 20 20  , pOp->p4.i);.  
16aa2 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
16aa3 0a 20 20 20 20 63 61 73 65 20 50 34 5f 52 45 41  .    case P4_REA
16aa4 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  L: {.      sqlit
16aa5 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d  e3_snprintf(nTem
16aa6 70 2c 20 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67  p, zTemp, "%.16g
16aa7 22 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61  ", *pOp->p4.pRea
16aa8 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  l);.      break;
16aa9 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
16aaa 50 34 5f 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20  P4_MEM: {.      
16aab 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 70 4f 70 2d  Mem *pMem = pOp-
16aac 3e 70 34 2e 70 4d 65 6d 3b 0a 20 20 20 20 20 20  >p4.pMem;.      
16aad 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66  assert( (pMem->f
16aae 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29  lags & MEM_Null)
16aaf 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28  ==0 );.      if(
16ab0 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d   pMem->flags & M
16ab1 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 20 20  EM_Str ){.      
16ab2 20 20 7a 50 34 20 3d 20 70 4d 65 6d 2d 3e 7a 3b    zP4 = pMem->z;
16ab3 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
16ab4 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d   pMem->flags & M
16ab5 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 20 20  EM_Int ){.      
16ab6 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
16ab7 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c  tf(nTemp, zTemp,
16ab8 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75   "%lld", pMem->u
16ab9 2e 69 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  .i);.      }else
16aba 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73   if( pMem->flags
16abb 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20   & MEM_Real ){. 
16abc 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73         sqlite3_s
16abd 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a  nprintf(nTemp, z
16abe 54 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c 20 70  Temp, "%.16g", p
16abf 4d 65 6d 2d 3e 72 29 3b 0a 20 20 20 20 20 20 7d  Mem->r);.      }
16ac0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
16ac1 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
16ac2 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
16ac3 41 42 4c 45 0a 20 20 20 20 63 61 73 65 20 50 34  ABLE.    case P4
16ac4 5f 56 54 41 42 3a 20 7b 0a 20 20 20 20 20 20 73  _VTAB: {.      s
16ac5 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
16ac6 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74  ab = pOp->p4.pVt
16ac7 61 62 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ab;.      sqlite
16ac8 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70  3_snprintf(nTemp
16ac9 2c 20 7a 54 65 6d 70 2c 20 22 76 74 61 62 3a 25  , zTemp, "vtab:%
16aca 70 3a 25 70 22 2c 20 70 56 74 61 62 2c 20 70 56  p:%p", pVtab, pV
16acb 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 29 3b 0a 20  tab->pModule);. 
16acc 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
16acd 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73  }.#endif.    cas
16ace 65 20 50 34 5f 49 4e 54 41 52 52 41 59 3a 20 7b  e P4_INTARRAY: {
16acf 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
16ad0 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a  nprintf(nTemp, z
16ad1 54 65 6d 70 2c 20 22 69 6e 74 61 72 72 61 79 22  Temp, "intarray"
16ad2 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
16ad3 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
16ad4 74 3a 20 7b 0a 20 20 20 20 20 20 7a 50 34 20 3d  t: {.      zP4 =
16ad5 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20   pOp->p4.z;.    
16ad6 20 20 69 66 28 20 7a 50 34 3d 3d 30 20 29 7b 0a    if( zP4==0 ){.
16ad7 20 20 20 20 20 20 20 20 7a 50 34 20 3d 20 7a 54          zP4 = zT
16ad8 65 6d 70 3b 0a 20 20 20 20 20 20 20 20 7a 54 65  emp;.        zTe
16ad9 6d 70 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 20  mp[0] = 0;.     
16ada 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61   }.    }.  }.  a
16adb 73 73 65 72 74 28 20 7a 50 34 21 3d 30 20 29 3b  ssert( zP4!=0 );
16adc 0a 20 20 72 65 74 75 72 6e 20 7a 50 34 3b 0a 7d  .  return zP4;.}
16add 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44  .#endif../*.** D
16ade 65 63 6c 61 72 65 20 74 6f 20 74 68 65 20 56 64  eclare to the Vd
16adf 62 65 20 74 68 61 74 20 74 68 65 20 42 54 72 65  be that the BTre
16ae0 65 20 6f 62 6a 65 63 74 20 61 74 20 64 62 2d 3e  e object at db->
16ae1 61 44 62 5b 69 5d 20 69 73 20 75 73 65 64 2e 0a  aDb[i] is used..
16ae2 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  **.*/.SQLITE_PRI
16ae3 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
16ae4 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 56  3VdbeUsesBtree(V
16ae5 64 62 65 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a  dbe *p, int i){.
16ae6 20 20 69 6e 74 20 6d 61 73 6b 3b 0a 20 20 61 73    int mask;.  as
16ae7 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
16ae8 70 2d 3e 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  p->db->nDb );.  
16ae9 61 73 73 65 72 74 28 20 69 3c 73 69 7a 65 6f 66  assert( i<sizeof
16aea 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 29 2a 38  (p->btreeMask)*8
16aeb 20 29 3b 0a 20 20 6d 61 73 6b 20 3d 20 31 3c 3c   );.  mask = 1<<
16aec 69 3b 0a 20 20 69 66 28 20 28 70 2d 3e 62 74 72  i;.  if( (p->btr
16aed 65 65 4d 61 73 6b 20 26 20 6d 61 73 6b 29 3d 3d  eeMask & mask)==
16aee 30 20 29 7b 0a 20 20 20 20 70 2d 3e 62 74 72 65  0 ){.    p->btre
16aef 65 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20  eMask |= mask;. 
16af0 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d     sqlite3BtreeM
16af1 75 74 65 78 41 72 72 61 79 49 6e 73 65 72 74 28  utexArrayInsert(
16af2 26 70 2d 3e 61 4d 75 74 65 78 2c 20 70 2d 3e 64  &p->aMutex, p->d
16af3 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 3b 0a  b->aDb[i].pBt);.
16af4 20 20 7d 0a 7d 0a 0a 0a 23 69 66 20 64 65 66 69    }.}...#if defi
16af5 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c 45  ned(VDBE_PROFILE
16af6 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
16af7 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a  ITE_DEBUG)./*.**
16af8 20 50 72 69 6e 74 20 61 20 73 69 6e 67 6c 65 20   Print a single 
16af9 6f 70 63 6f 64 65 2e 20 20 54 68 69 73 20 72 6f  opcode.  This ro
16afa 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f  utine is used fo
16afb 72 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79  r debugging only
16afc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
16afd 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
16afe 56 64 62 65 50 72 69 6e 74 4f 70 28 46 49 4c 45  VdbePrintOp(FILE
16aff 20 2a 70 4f 75 74 2c 20 69 6e 74 20 70 63 2c 20   *pOut, int pc, 
16b00 4f 70 20 2a 70 4f 70 29 7b 0a 20 20 63 68 61 72  Op *pOp){.  char
16b01 20 2a 7a 50 34 3b 0a 20 20 63 68 61 72 20 7a 50   *zP4;.  char zP
16b02 74 72 5b 35 30 5d 3b 0a 20 20 73 74 61 74 69 63  tr[50];.  static
16b03 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
16b04 72 6d 61 74 31 20 3d 20 22 25 34 64 20 25 2d 31  rmat1 = "%4d %-1
16b05 33 73 20 25 34 64 20 25 34 64 20 25 34 64 20 25  3s %4d %4d %4d %
16b06 2d 34 73 20 25 2e 32 58 20 25 73 5c 6e 22 3b 0a  -4s %.2X %s\n";.
16b07 20 20 69 66 28 20 70 4f 75 74 3d 3d 30 20 29 20    if( pOut==0 ) 
16b08 70 4f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20  pOut = stdout;. 
16b09 20 7a 50 34 20 3d 20 64 69 73 70 6c 61 79 50 34   zP4 = displayP4
16b0a 28 70 4f 70 2c 20 7a 50 74 72 2c 20 73 69 7a 65  (pOp, zPtr, size
16b0b 6f 66 28 7a 50 74 72 29 29 3b 0a 20 20 66 70 72  of(zPtr));.  fpr
16b0c 69 6e 74 66 28 70 4f 75 74 2c 20 7a 46 6f 72 6d  intf(pOut, zForm
16b0d 61 74 31 2c 20 70 63 2c 20 0a 20 20 20 20 20 20  at1, pc, .      
16b0e 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d  sqlite3OpcodeNam
16b0f 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 2c 20  e(pOp->opcode), 
16b10 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 32  pOp->p1, pOp->p2
16b11 2c 20 70 4f 70 2d 3e 70 33 2c 20 7a 50 34 2c 20  , pOp->p3, zP4, 
16b12 70 4f 70 2d 3e 70 35 2c 0a 23 69 66 64 65 66 20  pOp->p5,.#ifdef 
16b13 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20  SQLITE_DEBUG.   
16b14 20 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74     pOp->zComment
16b15 20 3f 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74   ? pOp->zComment
16b16 20 3a 20 22 22 0a 23 65 6c 73 65 0a 20 20 20 20   : "".#else.    
16b17 20 20 22 22 0a 23 65 6e 64 69 66 0a 20 20 29 3b    "".#endif.  );
16b18 0a 20 20 66 66 6c 75 73 68 28 70 4f 75 74 29 3b  .  fflush(pOut);
16b19 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
16b1a 20 52 65 6c 65 61 73 65 20 61 6e 20 61 72 72 61   Release an arra
16b1b 79 20 6f 66 20 4e 20 4d 65 6d 20 65 6c 65 6d 65  y of N Mem eleme
16b1c 6e 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  nts.*/.static vo
16b1d 69 64 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72  id releaseMemArr
16b1e 61 79 28 4d 65 6d 20 2a 70 2c 20 69 6e 74 20 4e  ay(Mem *p, int N
16b1f 29 7b 0a 20 20 69 66 28 20 70 20 26 26 20 4e 20  ){.  if( p && N 
16b20 29 7b 0a 20 20 20 20 4d 65 6d 20 2a 70 45 6e 64  ){.    Mem *pEnd
16b21 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64  ;.    sqlite3 *d
16b22 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 69  b = p->db;.    i
16b23 6e 74 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64  nt malloc_failed
16b24 20 3d 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69   = db->mallocFai
16b25 6c 65 64 3b 0a 20 20 20 20 66 6f 72 28 70 45 6e  led;.    for(pEn
16b26 64 3d 26 70 5b 4e 5d 3b 20 70 3c 70 45 6e 64 3b  d=&p[N]; p<pEnd;
16b27 20 70 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73   p++){.      ass
16b28 65 72 74 28 20 28 26 70 5b 31 5d 29 3d 3d 70 45  ert( (&p[1])==pE
16b29 6e 64 20 7c 7c 20 70 5b 30 5d 2e 64 62 3d 3d 70  nd || p[0].db==p
16b2a 5b 31 5d 2e 64 62 20 29 3b 0a 0a 20 20 20 20 20  [1].db );..     
16b2b 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 69   /* This block i
16b2c 73 20 72 65 61 6c 6c 79 20 61 6e 20 69 6e 6c 69  s really an inli
16b2d 6e 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 73  ned version of s
16b2e 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
16b2f 65 61 73 65 28 29 0a 20 20 20 20 20 20 2a 2a 20  ease().      ** 
16b30 74 68 61 74 20 74 61 6b 65 73 20 61 64 76 61 6e  that takes advan
16b31 74 61 67 65 20 6f 66 20 74 68 65 20 66 61 63 74  tage of the fact
16b32 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 79   that the memory
16b33 20 63 65 6c 6c 20 76 61 6c 75 65 20 69 73 20 0a   cell value is .
16b34 20 20 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 73        ** being s
16b35 65 74 20 74 6f 20 4e 55 4c 4c 20 61 66 74 65 72  et to NULL after
16b36 20 72 65 6c 65 61 73 69 6e 67 20 61 6e 79 20 64   releasing any d
16b37 79 6e 61 6d 69 63 20 72 65 73 6f 75 72 63 65 73  ynamic resources
16b38 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  ..      **.     
16b39 20 2a 2a 20 54 68 65 20 6a 75 73 74 69 66 69 63   ** The justific
16b3a 61 74 69 6f 6e 20 66 6f 72 20 64 75 70 6c 69 63  ation for duplic
16b3b 61 74 69 6e 67 20 63 6f 64 65 20 69 73 20 74 68  ating code is th
16b3c 61 74 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20  at according to 
16b3d 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 67 72  .      ** callgr
16b3e 69 6e 64 2c 20 74 68 69 73 20 63 61 75 73 65 73  ind, this causes
16b3f 20 61 20 63 65 72 74 61 69 6e 20 74 65 73 74 20   a certain test 
16b40 63 61 73 65 20 74 6f 20 68 69 74 20 74 68 65 20  case to hit the 
16b41 43 50 55 20 34 2e 37 20 0a 20 20 20 20 20 20 2a  CPU 4.7 .      *
16b42 2a 20 70 65 72 63 65 6e 74 20 6c 65 73 73 20 28  * percent less (
16b43 78 38 36 20 6c 69 6e 75 78 2c 20 67 63 63 20 76  x86 linux, gcc v
16b44 65 72 73 69 6f 6e 20 34 2e 31 2e 32 2c 20 2d 4f  ersion 4.1.2, -O
16b45 36 29 20 74 68 61 6e 20 69 66 20 0a 20 20 20 20  6) than if .    
16b46 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d 52    ** sqlite3MemR
16b47 65 6c 65 61 73 65 28 29 20 77 65 72 65 20 63 61  elease() were ca
16b48 6c 6c 65 64 20 66 72 6f 6d 20 68 65 72 65 2e 20  lled from here. 
16b49 57 69 74 68 20 2d 4f 32 2c 20 74 68 69 73 20 6a  With -O2, this j
16b4a 75 6d 70 73 0a 20 20 20 20 20 20 2a 2a 20 74 6f  umps.      ** to
16b4b 20 36 2e 36 20 70 65 72 63 65 6e 74 2e 20 54 68   6.6 percent. Th
16b4c 65 20 74 65 73 74 20 63 61 73 65 20 69 73 20 69  e test case is i
16b4d 6e 73 65 72 74 69 6e 67 20 31 30 30 30 20 72 6f  nserting 1000 ro
16b4e 77 73 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20  ws into a table 
16b4f 0a 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20 6e  .      ** with n
16b50 6f 20 69 6e 64 65 78 65 73 20 75 73 69 6e 67 20  o indexes using 
16b51 61 20 73 69 6e 67 6c 65 20 70 72 65 70 61 72 65  a single prepare
16b52 64 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65  d INSERT stateme
16b53 6e 74 2c 20 62 69 6e 64 28 29 20 0a 20 20 20 20  nt, bind() .    
16b54 20 20 2a 2a 20 61 6e 64 20 72 65 73 65 74 28 29    ** and reset()
16b55 2e 20 49 6e 73 65 72 74 73 20 61 72 65 20 67 72  . Inserts are gr
16b56 6f 75 70 65 64 20 69 6e 74 6f 20 61 20 74 72 61  ouped into a tra
16b57 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20  nsaction..      
16b58 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e  */.      if( p->
16b59 66 6c 61 67 73 26 28 4d 45 4d 5f 41 67 67 7c 4d  flags&(MEM_Agg|M
16b5a 45 4d 5f 44 79 6e 29 20 29 7b 0a 20 20 20 20 20  EM_Dyn) ){.     
16b5b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
16b5c 6d 52 65 6c 65 61 73 65 28 70 29 3b 0a 20 20 20  mRelease(p);.   
16b5d 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e     }else if( p->
16b5e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20  zMalloc ){.     
16b5f 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
16b60 28 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 29  (db, p->zMalloc)
16b61 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 7a 4d 61  ;.        p->zMa
16b62 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 20 20  lloc = 0;.      
16b63 7d 0a 0a 20 20 20 20 20 20 70 2d 3e 66 6c 61 67  }..      p->flag
16b64 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
16b65 20 20 7d 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c    }.    db->mall
16b66 6f 63 46 61 69 6c 65 64 20 3d 20 6d 61 6c 6c 6f  ocFailed = mallo
16b67 63 5f 66 61 69 6c 65 64 3b 0a 20 20 7d 0a 7d 0a  c_failed;.  }.}.
16b68 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
16b69 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e  NABLE_MEMORY_MAN
16b6a 41 47 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50  AGEMENT.SQLITE_P
16b6b 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
16b6c 65 33 56 64 62 65 52 65 6c 65 61 73 65 42 75 66  e3VdbeReleaseBuf
16b6d 66 65 72 73 28 56 64 62 65 20 2a 70 29 7b 0a 20  fers(Vdbe *p){. 
16b6e 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 6e   int ii;.  int n
16b6f 46 72 65 65 20 3d 20 30 3b 0a 20 20 61 73 73 65  Free = 0;.  asse
16b70 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
16b71 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
16b72 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 69  tex) );.  for(ii
16b73 3d 31 3b 20 69 69 3c 3d 70 2d 3e 6e 4d 65 6d 3b  =1; ii<=p->nMem;
16b74 20 69 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d 20   ii++){.    Mem 
16b75 2a 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d  *pMem = &p->aMem
16b76 5b 69 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 4d  [ii];.    if( pM
16b77 65 6d 2d 3e 7a 20 26 26 20 70 4d 65 6d 2d 3e 66  em->z && pMem->f
16b78 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 20 29 7b 0a  lags&MEM_Dyn ){.
16b79 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
16b7a 4d 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a 20 20 20  Mem->xDel );.   
16b7b 20 20 20 6e 46 72 65 65 20 2b 3d 20 73 71 6c 69     nFree += sqli
16b7c 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28  te3DbMallocSize(
16b7d 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e  pMem->db, pMem->
16b7e 7a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  z);.      sqlite
16b7f 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
16b80 70 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  pMem);.    }.  }
16b81 0a 20 20 72 65 74 75 72 6e 20 6e 46 72 65 65 3b  .  return nFree;
16b82 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  .}.#endif..#ifnd
16b83 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45  ef SQLITE_OMIT_E
16b84 58 50 4c 41 49 4e 0a 2f 2a 0a 2a 2a 20 47 69 76  XPLAIN./*.** Giv
16b85 65 20 61 20 6c 69 73 74 69 6e 67 20 6f 66 20 74  e a listing of t
16b86 68 65 20 70 72 6f 67 72 61 6d 20 69 6e 20 74 68  he program in th
16b87 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  e virtual machin
16b88 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74  e..**.** The int
16b89 65 72 66 61 63 65 20 69 73 20 74 68 65 20 73 61  erface is the sa
16b8a 6d 65 20 61 73 20 73 71 6c 69 74 65 33 56 64 62  me as sqlite3Vdb
16b8b 65 45 78 65 63 28 29 2e 20 20 42 75 74 20 69 6e  eExec().  But in
16b8c 73 74 65 61 64 20 6f 66 0a 2a 2a 20 72 75 6e 6e  stead of.** runn
16b8d 69 6e 67 20 74 68 65 20 63 6f 64 65 2c 20 69 74  ing the code, it
16b8e 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 61 6c   invokes the cal
16b8f 6c 62 61 63 6b 20 6f 6e 63 65 20 66 6f 72 20 65  lback once for e
16b90 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  ach instruction.
16b91 0a 2a 2a 20 54 68 69 73 20 66 65 61 74 75 72 65  .** This feature
16b92 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c   is used to impl
16b93 65 6d 65 6e 74 20 22 45 58 50 4c 41 49 4e 22 2e  ement "EXPLAIN".
16b94 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 70 2d 3e 65  .**.** When p->e
16b95 78 70 6c 61 69 6e 3d 3d 31 2c 20 65 61 63 68 20  xplain==1, each 
16b96 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 6c  instruction is l
16b97 69 73 74 65 64 2e 20 20 57 68 65 6e 0a 2a 2a 20  isted.  When.** 
16b98 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 2c 20 6f  p->explain==2, o
16b99 6e 6c 79 20 4f 50 5f 45 78 70 6c 61 69 6e 20 69  nly OP_Explain i
16b9a 6e 73 74 72 75 63 74 69 6f 6e 73 20 61 72 65 20  nstructions are 
16b9b 6c 69 73 74 65 64 20 61 6e 64 20 74 68 65 73 65  listed and these
16b9c 0a 2a 2a 20 61 72 65 20 73 68 6f 77 6e 20 69 6e  .** are shown in
16b9d 20 61 20 64 69 66 66 65 72 65 6e 74 20 66 6f 72   a different for
16b9e 6d 61 74 2e 20 20 70 2d 3e 65 78 70 6c 61 69 6e  mat.  p->explain
16b9f 3d 3d 32 20 69 73 20 75 73 65 64 20 74 6f 20 69  ==2 is used to i
16ba0 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 45 58 50 4c  mplement.** EXPL
16ba1 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 2e 0a  AIN QUERY PLAN..
16ba2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
16ba3 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
16ba4 65 4c 69 73 74 28 0a 20 20 56 64 62 65 20 2a 70  eList(.  Vdbe *p
16ba5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16ba6 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a     /* The VDBE *
16ba7 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  /.){.  sqlite3 *
16ba8 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e  db = p->db;.  in
16ba9 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  t i;.  int rc = 
16baa 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d  SQLITE_OK;.  Mem
16bab 20 2a 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73   *pMem = p->pRes
16bac 75 6c 74 53 65 74 20 3d 20 26 70 2d 3e 61 4d 65  ultSet = &p->aMe
16bad 6d 5b 31 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28  m[1];..  assert(
16bae 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29 3b 0a 20   p->explain );. 
16baf 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56   if( p->magic!=V
16bb0 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 20  DBE_MAGIC_RUN ) 
16bb1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
16bb2 53 55 53 45 3b 0a 20 20 61 73 73 65 72 74 28 20  SUSE;.  assert( 
16bb3 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54  db->magic==SQLIT
16bb4 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a  E_MAGIC_BUSY );.
16bb5 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 3d    assert( p->rc=
16bb6 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d  =SQLITE_OK || p-
16bb7 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59  >rc==SQLITE_BUSY
16bb8 20 29 3b 0a 0a 20 20 2f 2a 20 45 76 65 6e 20 74   );..  /* Even t
16bb9 68 6f 75 67 68 20 74 68 69 73 20 6f 70 63 6f 64  hough this opcod
16bba 65 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 64  e does not use d
16bbb 79 6e 61 6d 69 63 20 73 74 72 69 6e 67 73 20 66  ynamic strings f
16bbc 6f 72 0a 20 20 2a 2a 20 74 68 65 20 72 65 73 75  or.  ** the resu
16bbd 6c 74 2c 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d  lt, result colum
16bbe 6e 73 20 6d 61 79 20 62 65 63 6f 6d 65 20 64 79  ns may become dy
16bbf 6e 61 6d 69 63 20 69 66 20 74 68 65 20 75 73 65  namic if the use
16bc0 72 20 63 61 6c 6c 73 0a 20 20 2a 2a 20 73 71 6c  r calls.  ** sql
16bc1 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
16bc2 31 36 28 29 2c 20 63 61 75 73 69 6e 67 20 61 20  16(), causing a 
16bc3 74 72 61 6e 73 6c 61 74 69 6f 6e 20 74 6f 20 55  translation to U
16bc4 54 46 2d 31 36 20 65 6e 63 6f 64 69 6e 67 2e 0a  TF-16 encoding..
16bc5 20 20 2a 2f 0a 20 20 72 65 6c 65 61 73 65 4d 65    */.  releaseMe
16bc6 6d 41 72 72 61 79 28 70 4d 65 6d 2c 20 70 2d 3e  mArray(pMem, p->
16bc7 6e 4d 65 6d 29 3b 0a 0a 20 20 64 6f 7b 0a 20 20  nMem);..  do{.  
16bc8 20 20 69 20 3d 20 70 2d 3e 70 63 2b 2b 3b 0a 20    i = p->pc++;. 
16bc9 20 7d 77 68 69 6c 65 28 20 69 3c 70 2d 3e 6e 4f   }while( i<p->nO
16bca 70 20 26 26 20 70 2d 3e 65 78 70 6c 61 69 6e 3d  p && p->explain=
16bcb 3d 32 20 26 26 20 70 2d 3e 61 4f 70 5b 69 5d 2e  =2 && p->aOp[i].
16bcc 6f 70 63 6f 64 65 21 3d 4f 50 5f 45 78 70 6c 61  opcode!=OP_Expla
16bcd 69 6e 20 29 3b 0a 20 20 69 66 28 20 69 3e 3d 70  in );.  if( i>=p
16bce 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 70 2d 3e  ->nOp ){.    p->
16bcf 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
16bd0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
16bd1 44 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66  DONE;.  }else if
16bd2 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72  ( db->u1.isInter
16bd3 72 75 70 74 65 64 20 29 7b 0a 20 20 20 20 70 2d  rupted ){.    p-
16bd4 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54  >rc = SQLITE_INT
16bd5 45 52 52 55 50 54 3b 0a 20 20 20 20 72 63 20 3d  ERRUPT;.    rc =
16bd6 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
16bd7 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
16bd8 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c  ing(&p->zErrMsg,
16bd9 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74   db, "%s", sqlit
16bda 65 33 45 72 72 53 74 72 28 70 2d 3e 72 63 29 29  e3ErrStr(p->rc))
16bdb 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63  ;.  }else{.    c
16bdc 68 61 72 20 2a 7a 3b 0a 20 20 20 20 4f 70 20 2a  har *z;.    Op *
16bdd 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d  pOp = &p->aOp[i]
16bde 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 65 78 70  ;.    if( p->exp
16bdf 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 20  lain==1 ){.     
16be0 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
16be1 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 70 4d  EM_Int;.      pM
16be2 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54  em->type = SQLIT
16be3 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20  E_INTEGER;.     
16be4 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 69 3b 20   pMem->u.i = i; 
16be5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16be6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
16be7 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65  * Program counte
16be8 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2b  r */.      pMem+
16be9 2b 3b 0a 20 20 0a 20 20 20 20 20 20 70 4d 65 6d  +;.  .      pMem
16bea 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74  ->flags = MEM_St
16beb 61 74 69 63 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d  atic|MEM_Str|MEM
16bec 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65  _Term;.      pMe
16bed 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29 73 71  m->z = (char*)sq
16bee 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28  lite3OpcodeName(
16bef 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 3b 20 20 2f  pOp->opcode);  /
16bf0 2a 20 4f 70 63 6f 64 65 20 2a 2f 0a 20 20 20 20  * Opcode */.    
16bf1 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
16bf2 7a 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4d  z!=0 );.      pM
16bf3 65 6d 2d 3e 6e 20 3d 20 73 74 72 6c 65 6e 28 70  em->n = strlen(p
16bf4 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70  Mem->z);.      p
16bf5 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
16bf6 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20 20 70  TE_TEXT;.      p
16bf7 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54  Mem->enc = SQLIT
16bf8 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 70 4d  E_UTF8;.      pM
16bf9 65 6d 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  em++;.    }..   
16bfa 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
16bfb 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4d 65 6d  EM_Int;.    pMem
16bfc 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 31 3b  ->u.i = pOp->p1;
16bfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16bfe 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 31 20            /* P1 
16bff 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70  */.    pMem->typ
16c00 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47  e = SQLITE_INTEG
16c01 45 52 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a  ER;.    pMem++;.
16c02 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  .    pMem->flags
16c03 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20   = MEM_Int;.    
16c04 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d  pMem->u.i = pOp-
16c05 3e 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20  >p2;            
16c06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
16c07 20 50 32 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d   P2 */.    pMem-
16c08 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49  >type = SQLITE_I
16c09 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 4d 65 6d  NTEGER;.    pMem
16c0a 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e  ++;..    if( p->
16c0b 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20  explain==1 ){.  
16c0c 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
16c0d 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20  = MEM_Int;.     
16c0e 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70   pMem->u.i = pOp
16c0f 2d 3e 70 33 3b 20 20 20 20 20 20 20 20 20 20 20  ->p3;           
16c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
16c11 2a 20 50 33 20 2a 2f 0a 20 20 20 20 20 20 70 4d  * P3 */.      pM
16c12 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54  em->type = SQLIT
16c13 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20  E_INTEGER;.     
16c14 20 70 4d 65 6d 2b 2b 3b 0a 20 20 20 20 7d 0a 0a   pMem++;.    }..
16c15 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56      if( sqlite3V
16c16 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c  dbeMemGrow(pMem,
16c17 20 33 32 2c 20 30 29 20 29 7b 20 20 20 20 20 20   32, 0) ){      
16c18 20 20 20 20 20 20 2f 2a 20 50 34 20 2a 2f 0a 20        /* P4 */. 
16c19 20 20 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c       p->db->mall
16c1a 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
16c1b 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
16c1c 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20  E_NOMEM;.    }. 
16c1d 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
16c1e 20 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 72   MEM_Dyn|MEM_Str
16c1f 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 7a  |MEM_Term;.    z
16c20 20 3d 20 64 69 73 70 6c 61 79 50 34 28 70 4f 70   = displayP4(pOp
16c21 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 33 32 29 3b 0a  , pMem->z, 32);.
16c22 20 20 20 20 69 66 28 20 7a 21 3d 70 4d 65 6d 2d      if( z!=pMem-
16c23 3e 7a 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  >z ){.      sqli
16c24 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72  te3VdbeMemSetStr
16c25 28 70 4d 65 6d 2c 20 7a 2c 20 2d 31 2c 20 53 51  (pMem, z, -1, SQ
16c26 4c 49 54 45 5f 55 54 46 38 2c 20 30 29 3b 0a 20  LITE_UTF8, 0);. 
16c27 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
16c28 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 21  assert( pMem->z!
16c29 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d  =0 );.      pMem
16c2a 2d 3e 6e 20 3d 20 73 74 72 6c 65 6e 28 70 4d 65  ->n = strlen(pMe
16c2b 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65  m->z);.      pMe
16c2c 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f  m->enc = SQLITE_
16c2d 55 54 46 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20  UTF8;.    }.    
16c2e 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
16c2f 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 70 4d  ITE_TEXT;.    pM
16c30 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70  em++;..    if( p
16c31 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a  ->explain==1 ){.
16c32 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
16c33 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65  3VdbeMemGrow(pMe
16c34 6d 2c 20 34 2c 20 30 29 20 29 7b 0a 20 20 20 20  m, 4, 0) ){.    
16c35 20 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f      p->db->mallo
16c36 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
16c37 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
16c38 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
16c39 7d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  }.      pMem->fl
16c3a 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e 7c 4d 45  ags = MEM_Dyn|ME
16c3b 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a  M_Str|MEM_Term;.
16c3c 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20        pMem->n = 
16c3d 32 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  2;.      sqlite3
16c3e 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 70 4d 65  _snprintf(3, pMe
16c3f 6d 2d 3e 7a 2c 20 22 25 2e 32 78 22 2c 20 70 4f  m->z, "%.2x", pO
16c40 70 2d 3e 70 35 29 3b 20 20 20 2f 2a 20 50 35 20  p->p5);   /* P5 
16c41 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74  */.      pMem->t
16c42 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58  ype = SQLITE_TEX
16c43 54 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65  T;.      pMem->e
16c44 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38  nc = SQLITE_UTF8
16c45 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a  ;.      pMem++;.
16c46 20 20 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45    .#ifdef SQLITE
16c47 5f 44 45 42 55 47 0a 20 20 20 20 20 20 69 66 28  _DEBUG.      if(
16c48 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 29   pOp->zComment )
16c49 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e  {.        pMem->
16c4a 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72 7c  flags = MEM_Str|
16c4b 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20  MEM_Term;.      
16c4c 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4f 70 2d    pMem->z = pOp-
16c4d 3e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 20  >zComment;.     
16c4e 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 74 72     pMem->n = str
16c4f 6c 65 6e 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20  len(pMem->z);.  
16c50 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20        pMem->enc 
16c51 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20  = SQLITE_UTF8;. 
16c52 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70         pMem->typ
16c53 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b  e = SQLITE_TEXT;
16c54 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e  .      }else.#en
16c55 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20  dif.      {.    
16c56 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
16c57 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 20 20 20 20 20  = MEM_Null;     
16c58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c59 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 2a 2f 0a    /* Comment */.
16c5a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79          pMem->ty
16c5b 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  pe = SQLITE_NULL
16c5c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
16c5d 0a 20 20 20 20 70 2d 3e 6e 52 65 73 43 6f 6c 75  .    p->nResColu
16c5e 6d 6e 20 3d 20 38 20 2d 20 35 2a 28 70 2d 3e 65  mn = 8 - 5*(p->e
16c5f 78 70 6c 61 69 6e 2d 31 29 3b 0a 20 20 20 20 70  xplain-1);.    p
16c60 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  ->rc = SQLITE_OK
16c61 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
16c62 45 5f 52 4f 57 3b 0a 20 20 7d 0a 20 20 72 65 74  E_ROW;.  }.  ret
16c63 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66  urn rc;.}.#endif
16c64 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
16c65 45 58 50 4c 41 49 4e 20 2a 2f 0a 0a 23 69 66 64  EXPLAIN */..#ifd
16c66 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
16c67 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65 20  /*.** Print the 
16c68 53 51 4c 20 74 68 61 74 20 77 61 73 20 75 73 65  SQL that was use
16c69 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20  d to generate a 
16c6a 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f  VDBE program..*/
16c6b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
16c6c 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
16c6d 50 72 69 6e 74 53 71 6c 28 56 64 62 65 20 2a 70  PrintSql(Vdbe *p
16c6e 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 3d 20 70  ){.  int nOp = p
16c6f 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 4f 70 20  ->nOp;.  VdbeOp 
16c70 2a 70 4f 70 3b 0a 20 20 69 66 28 20 6e 4f 70 3c  *pOp;.  if( nOp<
16c71 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 4f  1 ) return;.  pO
16c72 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 30 5d 3b 0a  p = &p->aOp[0];.
16c73 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64    if( pOp->opcod
16c74 65 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26 20 70  e==OP_Trace && p
16c75 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 7b 0a 20  Op->p4.z!=0 ){. 
16c76 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
16c77 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20   = pOp->p4.z;.  
16c78 20 20 77 68 69 6c 65 28 20 69 73 73 70 61 63 65    while( isspace
16c79 28 2a 28 75 38 2a 29 7a 29 20 29 20 7a 2b 2b 3b  (*(u8*)z) ) z++;
16c7a 0a 20 20 20 20 70 72 69 6e 74 66 28 22 53 51 4c  .    printf("SQL
16c7b 3a 20 5b 25 73 5d 5c 6e 22 2c 20 7a 29 3b 0a 20  : [%s]\n", z);. 
16c7c 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66   }.}.#endif..#if
16c7d 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
16c7e 5f 4f 4d 49 54 5f 54 52 41 43 45 29 20 26 26 20  _OMIT_TRACE) && 
16c7f 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
16c80 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 29 0a 2f  NABLE_IOTRACE)./
16c81 2a 0a 2a 2a 20 50 72 69 6e 74 20 61 6e 20 49 4f  *.** Print an IO
16c82 54 52 41 43 45 20 6d 65 73 73 61 67 65 20 73 68  TRACE message sh
16c83 6f 77 69 6e 67 20 53 51 4c 20 63 6f 6e 74 65 6e  owing SQL conten
16c84 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
16c85 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
16c86 33 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c 28  3VdbeIOTraceSql(
16c87 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  Vdbe *p){.  int 
16c88 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20  nOp = p->nOp;.  
16c89 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 69  VdbeOp *pOp;.  i
16c8a 66 28 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63  f( sqlite3IoTrac
16c8b 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  e==0 ) return;. 
16c8c 20 69 66 28 20 6e 4f 70 3c 31 20 29 20 72 65 74   if( nOp<1 ) ret
16c8d 75 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d  urn;.  pOp = &p-
16c8e 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 66 28 20 70  >aOp[0];.  if( p
16c8f 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 54  Op->opcode==OP_T
16c90 72 61 63 65 20 26 26 20 70 4f 70 2d 3e 70 34 2e  race && pOp->p4.
16c91 7a 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  z!=0 ){.    int 
16c92 69 2c 20 6a 3b 0a 20 20 20 20 63 68 61 72 20 7a  i, j;.    char z
16c93 5b 31 30 30 30 5d 3b 0a 20 20 20 20 73 71 6c 69  [1000];.    sqli
16c94 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
16c95 65 6f 66 28 7a 29 2c 20 7a 2c 20 22 25 73 22 2c  eof(z), z, "%s",
16c96 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20   pOp->p4.z);.   
16c97 20 66 6f 72 28 69 3d 30 3b 20 69 73 73 70 61 63   for(i=0; isspac
16c98 65 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  e((unsigned char
16c99 29 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20  )z[i]); i++){}. 
16c9a 20 20 20 66 6f 72 28 6a 3d 30 3b 20 7a 5b 69 5d     for(j=0; z[i]
16c9b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
16c9c 28 20 69 73 73 70 61 63 65 28 28 75 6e 73 69 67  ( isspace((unsig
16c9d 6e 65 64 20 63 68 61 72 29 7a 5b 69 5d 29 20 29  ned char)z[i]) )
16c9e 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b  {.        if( z[
16c9f 69 2d 31 5d 21 3d 27 20 27 20 29 7b 0a 20 20 20  i-1]!=' ' ){.   
16ca0 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20         z[j++] = 
16ca1 27 20 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ' ';.        }. 
16ca2 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
16ca3 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69      z[j++] = z[i
16ca4 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  ];.      }.    }
16ca5 0a 20 20 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20  .    z[j] = 0;. 
16ca6 20 20 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63     sqlite3IoTrac
16ca7 65 28 22 53 51 4c 20 25 73 5c 6e 22 2c 20 7a 29  e("SQL %s\n", z)
16ca8 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f  ;.  }.}.#endif /
16ca9 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  * !SQLITE_OMIT_T
16caa 52 41 43 45 20 26 26 20 53 51 4c 49 54 45 5f 45  RACE && SQLITE_E
16cab 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 20 2a 2f  NABLE_IOTRACE */
16cac 0a 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65  .../*.** Prepare
16cad 20 61 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69   a virtual machi
16cae 6e 65 20 66 6f 72 20 65 78 65 63 75 74 69 6f 6e  ne for execution
16caf 2e 20 20 54 68 69 73 20 69 6e 76 6f 6c 76 65 73  .  This involves
16cb0 20 74 68 69 6e 67 73 20 73 75 63 68 0a 2a 2a 20   things such.** 
16cb1 61 73 20 61 6c 6c 6f 63 61 74 69 6e 67 20 73 74  as allocating st
16cb2 61 63 6b 20 73 70 61 63 65 20 61 6e 64 20 69 6e  ack space and in
16cb3 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 70  itializing the p
16cb4 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 2e 0a  rogram counter..
16cb5 2a 2a 20 41 66 74 65 72 20 74 68 65 20 56 44 42  ** After the VDB
16cb6 45 20 68 61 73 20 62 65 20 70 72 65 70 70 65 64  E has be prepped
16cb7 2c 20 69 74 20 63 61 6e 20 62 65 20 65 78 65 63  , it can be exec
16cb8 75 74 65 64 20 62 79 20 6f 6e 65 20 6f 72 20 6d  uted by one or m
16cb9 6f 72 65 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20  ore.** calls to 
16cba 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28  sqlite3VdbeExec(
16cbb 29 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ).  .**.** This 
16cbc 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20  is the only way 
16cbd 74 6f 20 6d 6f 76 65 20 61 20 56 44 42 45 20 66  to move a VDBE f
16cbe 72 6f 6d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49  rom VDBE_MAGIC_I
16cbf 4e 49 54 20 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d  NIT to.** VDBE_M
16cc0 41 47 49 43 5f 52 55 4e 2e 0a 2a 2f 0a 53 51 4c  AGIC_RUN..*/.SQL
16cc1 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
16cc2 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
16cc3 52 65 61 64 79 28 0a 20 20 56 64 62 65 20 2a 70  Ready(.  Vdbe *p
16cc4 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
16cc5 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
16cc6 44 42 45 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61  DBE */.  int nVa
16cc7 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
16cc8 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
16cc9 72 20 6f 66 20 27 3f 27 20 73 65 65 20 69 6e 20  r of '?' see in 
16cca 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
16ccb 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 2c  t */.  int nMem,
16ccc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16ccd 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
16cce 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20  of memory cells 
16ccf 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20  to allocate */. 
16cd0 20 69 6e 74 20 6e 43 75 72 73 6f 72 2c 20 20 20   int nCursor,   
16cd1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16cd2 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 75 72  /* Number of cur
16cd3 73 6f 72 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65  sors to allocate
16cd4 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 78 70 6c   */.  int isExpl
16cd5 61 69 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  ain             
16cd6 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
16cd7 74 68 65 20 45 58 50 4c 41 49 4e 20 6b 65 79 77  the EXPLAIN keyw
16cd8 6f 72 64 73 20 69 73 20 70 72 65 73 65 6e 74 20  ords is present 
16cd9 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20  */.){.  int n;. 
16cda 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
16cdb 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72 74 28  ->db;..  assert(
16cdc 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72   p!=0 );.  asser
16cdd 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42  t( p->magic==VDB
16cde 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a  E_MAGIC_INIT );.
16cdf 0a 20 20 2f 2a 20 54 68 65 72 65 20 73 68 6f 75  .  /* There shou
16ce0 6c 64 20 62 65 20 61 74 20 6c 65 61 73 74 20 6f  ld be at least o
16ce1 6e 65 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2f 0a  ne opcode..  */.
16ce2 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70    assert( p->nOp
16ce3 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20  >0 );..  /* Set 
16ce4 74 68 65 20 6d 61 67 69 63 20 74 6f 20 56 44 42  the magic to VDB
16ce5 45 5f 4d 41 47 49 43 5f 52 55 4e 20 73 6f 6f 6e  E_MAGIC_RUN soon
16ce6 65 72 20 72 61 74 68 65 72 20 74 68 61 6e 20 6c  er rather than l
16ce7 61 74 65 72 2e 20 54 68 69 73 0a 20 20 20 2a 20  ater. This.   * 
16ce8 69 73 20 62 65 63 61 75 73 65 20 74 68 65 20 63  is because the c
16ce9 61 6c 6c 20 74 6f 20 72 65 73 69 7a 65 4f 70 41  all to resizeOpA
16cea 72 72 61 79 28 29 20 62 65 6c 6f 77 20 6d 61 79  rray() below may
16ceb 20 73 68 72 69 6e 6b 20 74 68 65 0a 20 20 20 2a   shrink the.   *
16cec 20 70 2d 3e 61 4f 70 5b 5d 20 61 72 72 61 79 20   p->aOp[] array 
16ced 74 6f 20 73 61 76 65 20 6d 65 6d 6f 72 79 20 69  to save memory i
16cee 66 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 69 6e  f called when in
16cef 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20   VDBE_MAGIC_RUN 
16cf0 0a 20 20 20 2a 20 73 74 61 74 65 2e 0a 20 20 20  .   * state..   
16cf1 2a 2f 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20  */.  p->magic = 
16cf2 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 3b 0a  VDBE_MAGIC_RUN;.
16cf3 0a 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 63  .  /* For each c
16cf4 75 72 73 6f 72 20 72 65 71 75 69 72 65 64 2c 20  ursor required, 
16cf5 61 6c 73 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20  also allocate a 
16cf6 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 4d 65 6d  memory cell. Mem
16cf7 6f 72 79 0a 20 20 2a 2a 20 63 65 6c 6c 73 20 28  ory.  ** cells (
16cf8 6e 4d 65 6d 2b 31 2d 6e 43 75 72 73 6f 72 29 2e  nMem+1-nCursor).
16cf9 2e 6e 4d 65 6d 2c 20 69 6e 63 6c 75 73 69 76 65  .nMem, inclusive
16cfa 2c 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20  , will never be 
16cfb 75 73 65 64 20 62 79 0a 20 20 2a 2a 20 74 68 65  used by.  ** the
16cfc 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e 20 49   vdbe program. I
16cfd 6e 73 74 65 61 64 20 74 68 65 79 20 61 72 65 20  nstead they are 
16cfe 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65  used to allocate
16cff 20 73 70 61 63 65 20 66 6f 72 0a 20 20 2a 2a 20   space for.  ** 
16d00 56 64 62 65 43 75 72 73 6f 72 2f 42 74 43 75 72  VdbeCursor/BtCur
16d01 73 6f 72 20 73 74 72 75 63 74 75 72 65 73 2e 20  sor structures. 
16d02 54 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f  The blob of memo
16d03 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ry associated wi
16d04 74 68 20 0a 20 20 2a 2a 20 63 75 72 73 6f 72 20  th .  ** cursor 
16d05 30 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 6d  0 is stored in m
16d06 65 6d 6f 72 79 20 63 65 6c 6c 20 6e 4d 65 6d 2e  emory cell nMem.
16d07 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28 6e 4d   Memory cell (nM
16d08 65 6d 2d 31 29 0a 20 20 2a 2a 20 73 74 6f 72 65  em-1).  ** store
16d09 73 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65  s the blob of me
16d0a 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20  mory associated 
16d0b 77 69 74 68 20 63 75 72 73 6f 72 20 31 2c 20 65  with cursor 1, e
16d0c 74 63 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53 65  tc..  **.  ** Se
16d0d 65 20 61 6c 73 6f 3a 20 61 6c 6c 6f 63 61 74 65  e also: allocate
16d0e 43 75 72 73 6f 72 28 29 2e 0a 20 20 2a 2f 0a 20  Cursor()..  */. 
16d0f 20 6e 4d 65 6d 20 2b 3d 20 6e 43 75 72 73 6f 72   nMem += nCursor
16d10 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c  ;..  /*.  ** All
16d11 6f 63 61 74 69 6f 6e 20 73 70 61 63 65 20 66 6f  ocation space fo
16d12 72 20 72 65 67 69 73 74 65 72 73 2e 0a 20 20 2a  r registers..  *
16d13 2f 0a 20 20 69 66 28 20 70 2d 3e 61 4d 65 6d 3d  /.  if( p->aMem=
16d14 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41  =0 ){.    int nA
16d15 72 67 3b 20 20 20 20 20 20 20 2f 2a 20 4d 61 78  rg;       /* Max
16d16 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61  imum number of a
16d17 72 67 73 20 70 61 73 73 65 64 20 74 6f 20 61 20  rgs passed to a 
16d18 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20 2a  user function. *
16d19 2f 0a 20 20 20 20 72 65 73 6f 6c 76 65 50 32 56  /.    resolveP2V
16d1a 61 6c 75 65 73 28 70 2c 20 26 6e 41 72 67 29 3b  alues(p, &nArg);
16d1b 0a 20 20 20 20 2f 2a 72 65 73 69 7a 65 4f 70 41  .    /*resizeOpA
16d1c 72 72 61 79 28 70 2c 20 70 2d 3e 6e 4f 70 29 3b  rray(p, p->nOp);
16d1d 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e  */.    assert( n
16d1e 56 61 72 3e 3d 30 20 29 3b 0a 20 20 20 20 69 66  Var>=0 );.    if
16d1f 28 20 69 73 45 78 70 6c 61 69 6e 20 26 26 20 6e  ( isExplain && n
16d20 4d 65 6d 3c 31 30 20 29 7b 0a 20 20 20 20 20 20  Mem<10 ){.      
16d21 6e 4d 65 6d 20 3d 20 31 30 3b 0a 20 20 20 20 7d  nMem = 10;.    }
16d22 0a 20 20 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 73  .    p->aMem = s
16d23 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
16d24 72 6f 28 64 62 2c 0a 20 20 20 20 20 20 20 20 6e  ro(db,.        n
16d25 4d 65 6d 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 20  Mem*sizeof(Mem) 
16d26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
16d27 20 61 4d 65 6d 20 2a 2f 0a 20 20 20 20 20 20 2b   aMem */.      +
16d28 20 6e 56 61 72 2a 73 69 7a 65 6f 66 28 4d 65 6d   nVar*sizeof(Mem
16d29 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  )               
16d2a 2f 2a 20 61 56 61 72 20 2a 2f 0a 20 20 20 20 20  /* aVar */.     
16d2b 20 2b 20 6e 41 72 67 2a 73 69 7a 65 6f 66 28 4d   + nArg*sizeof(M
16d2c 65 6d 2a 29 20 20 20 20 20 20 20 20 20 20 20 20  em*)            
16d2d 20 20 2f 2a 20 61 70 41 72 67 20 2a 2f 0a 20 20    /* apArg */.  
16d2e 20 20 20 20 2b 20 6e 56 61 72 2a 73 69 7a 65 6f      + nVar*sizeo
16d2f 66 28 63 68 61 72 2a 29 20 20 20 20 20 20 20 20  f(char*)        
16d30 20 20 20 20 20 2f 2a 20 61 7a 56 61 72 20 2a 2f       /* azVar */
16d31 0a 20 20 20 20 20 20 2b 20 6e 43 75 72 73 6f 72  .      + nCursor
16d32 2a 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73  *sizeof(VdbeCurs
16d33 6f 72 2a 29 2b 31 20 20 2f 2a 20 61 70 43 73 72  or*)+1  /* apCsr
16d34 20 2a 2f 0a 20 20 20 20 29 3b 0a 20 20 20 20 69   */.    );.    i
16d35 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  f( !db->mallocFa
16d36 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 70 2d  iled ){.      p-
16d37 3e 61 4d 65 6d 2d 2d 3b 20 20 20 20 20 20 20 20  >aMem--;        
16d38 20 20 20 20 20 2f 2a 20 61 4d 65 6d 5b 5d 20 67       /* aMem[] g
16d39 6f 65 73 20 66 72 6f 6d 20 31 2e 2e 6e 4d 65 6d  oes from 1..nMem
16d3a 20 2a 2f 0a 20 20 20 20 20 20 70 2d 3e 6e 4d 65   */.      p->nMe
16d3b 6d 20 3d 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20  m = nMem;       
16d3c 20 2f 2a 20 20 20 20 20 20 20 6e 6f 74 20 66 72   /*       not fr
16d3d 6f 6d 20 30 2e 2e 6e 4d 65 6d 2d 31 20 2a 2f 0a  om 0..nMem-1 */.
16d3e 20 20 20 20 20 20 70 2d 3e 61 56 61 72 20 3d 20        p->aVar = 
16d3f 26 70 2d 3e 61 4d 65 6d 5b 6e 4d 65 6d 2b 31 5d  &p->aMem[nMem+1]
16d40 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 56 61 72 20  ;.      p->nVar 
16d41 3d 20 6e 56 61 72 3b 0a 20 20 20 20 20 20 70 2d  = nVar;.      p-
16d42 3e 6f 6b 56 61 72 20 3d 20 30 3b 0a 20 20 20 20  >okVar = 0;.    
16d43 20 20 70 2d 3e 61 70 41 72 67 20 3d 20 28 4d 65    p->apArg = (Me
16d44 6d 2a 2a 29 26 70 2d 3e 61 56 61 72 5b 6e 56 61  m**)&p->aVar[nVa
16d45 72 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 61 7a 56  r];.      p->azV
16d46 61 72 20 3d 20 28 63 68 61 72 2a 2a 29 26 70 2d  ar = (char**)&p-
16d47 3e 61 70 41 72 67 5b 6e 41 72 67 5d 3b 0a 20 20  >apArg[nArg];.  
16d48 20 20 20 20 70 2d 3e 61 70 43 73 72 20 3d 20 28      p->apCsr = (
16d49 56 64 62 65 43 75 72 73 6f 72 2a 2a 29 26 70 2d  VdbeCursor**)&p-
16d4a 3e 61 7a 56 61 72 5b 6e 56 61 72 5d 3b 0a 20 20  >azVar[nVar];.  
16d4b 20 20 20 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d      p->nCursor =
16d4c 20 6e 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20   nCursor;.      
16d4d 66 6f 72 28 6e 3d 30 3b 20 6e 3c 6e 56 61 72 3b  for(n=0; n<nVar;
16d4e 20 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70   n++){.        p
16d4f 2d 3e 61 56 61 72 5b 6e 5d 2e 66 6c 61 67 73 20  ->aVar[n].flags 
16d50 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20  = MEM_Null;.    
16d51 20 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 64      p->aVar[n].d
16d52 62 20 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a  b = db;.      }.
16d53 20 20 20 20 20 20 66 6f 72 28 6e 3d 31 3b 20 6e        for(n=1; n
16d54 3c 3d 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b 0a 20 20  <=nMem; n++){.  
16d55 20 20 20 20 20 20 70 2d 3e 61 4d 65 6d 5b 6e 5d        p->aMem[n]
16d56 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c  .flags = MEM_Nul
16d57 6c 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 4d  l;.        p->aM
16d58 65 6d 5b 6e 5d 2e 64 62 20 3d 20 64 62 3b 0a 20  em[n].db = db;. 
16d59 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
16d5a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
16d5b 45 42 55 47 0a 20 20 66 6f 72 28 6e 3d 31 3b 20  EBUG.  for(n=1; 
16d5c 6e 3c 70 2d 3e 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b  n<p->nMem; n++){
16d5d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
16d5e 61 4d 65 6d 5b 6e 5d 2e 64 62 3d 3d 64 62 20 29  aMem[n].db==db )
16d5f 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
16d60 70 2d 3e 70 63 20 3d 20 2d 31 3b 0a 20 20 70 2d  p->pc = -1;.  p-
16d61 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  >rc = SQLITE_OK;
16d62 0a 20 20 70 2d 3e 75 6e 69 71 75 65 43 6e 74 20  .  p->uniqueCnt 
16d63 3d 20 30 3b 0a 20 20 70 2d 3e 65 72 72 6f 72 41  = 0;.  p->errorA
16d64 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74  ction = OE_Abort
16d65 3b 0a 20 20 70 2d 3e 65 78 70 6c 61 69 6e 20 7c  ;.  p->explain |
16d66 3d 20 69 73 45 78 70 6c 61 69 6e 3b 0a 20 20 70  = isExplain;.  p
16d67 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d  ->magic = VDBE_M
16d68 41 47 49 43 5f 52 55 4e 3b 0a 20 20 70 2d 3e 6e  AGIC_RUN;.  p->n
16d69 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70 2d  Change = 0;.  p-
16d6a 3e 63 61 63 68 65 43 74 72 20 3d 20 31 3b 0a 20  >cacheCtr = 1;. 
16d6b 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65   p->minWriteFile
16d6c 46 6f 72 6d 61 74 20 3d 20 32 35 35 3b 0a 20 20  Format = 255;.  
16d6d 70 2d 3e 6f 70 65 6e 65 64 53 74 61 74 65 6d 65  p->openedStateme
16d6e 6e 74 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 56  nt = 0;.#ifdef V
16d6f 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 7b 0a  DBE_PROFILE.  {.
16d70 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66      int i;.    f
16d71 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70  or(i=0; i<p->nOp
16d72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 2d  ; i++){.      p-
16d73 3e 61 4f 70 5b 69 5d 2e 63 6e 74 20 3d 20 30 3b  >aOp[i].cnt = 0;
16d74 0a 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d  .      p->aOp[i]
16d75 2e 63 79 63 6c 65 73 20 3d 20 30 3b 0a 20 20 20  .cycles = 0;.   
16d76 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a   }.  }.#endif.}.
16d77 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 56  ./*.** Close a V
16d78 44 42 45 20 63 75 72 73 6f 72 20 61 6e 64 20 72  DBE cursor and r
16d79 65 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 20 72  elease all the r
16d7a 65 73 6f 75 72 63 65 73 20 74 68 61 74 20 63 75  esources that cu
16d7b 72 73 6f 72 20 0a 2a 2a 20 68 61 70 70 65 6e 73  rsor .** happens
16d7c 20 74 6f 20 68 6f 6c 64 2e 0a 2a 2f 0a 53 51 4c   to hold..*/.SQL
16d7d 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
16d7e 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65   sqlite3VdbeFree
16d7f 43 75 72 73 6f 72 28 56 64 62 65 20 2a 70 2c 20  Cursor(Vdbe *p, 
16d80 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 29  VdbeCursor *pCx)
16d81 7b 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 20 29  {.  if( pCx==0 )
16d82 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
16d83 7d 0a 20 20 69 66 28 20 70 43 78 2d 3e 70 42 74  }.  if( pCx->pBt
16d84 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42   ){.    sqlite3B
16d85 74 72 65 65 43 6c 6f 73 65 28 70 43 78 2d 3e 70  treeClose(pCx->p
16d86 42 74 29 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20  Bt);.    /* The 
16d87 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 77 69 6c  pCx->pCursor wil
16d88 6c 20 62 65 20 63 6c 6f 73 65 20 61 75 74 6f 6d  l be close autom
16d89 61 74 69 63 61 6c 6c 79 2c 20 69 66 20 69 74 20  atically, if it 
16d8a 65 78 69 73 74 73 2c 20 62 79 0a 20 20 20 20 2a  exists, by.    *
16d8b 2a 20 74 68 65 20 63 61 6c 6c 20 61 62 6f 76 65  * the call above
16d8c 2e 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28  . */.  }else if(
16d8d 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 29 7b   pCx->pCursor ){
16d8e 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
16d8f 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 43 78  eCloseCursor(pCx
16d90 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a  ->pCursor);.  }.
16d91 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
16d92 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
16d93 0a 20 20 69 66 28 20 70 43 78 2d 3e 70 56 74 61  .  if( pCx->pVta
16d94 62 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73  bCursor ){.    s
16d95 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
16d96 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 20  or *pVtabCursor 
16d97 3d 20 70 43 78 2d 3e 70 56 74 61 62 43 75 72 73  = pCx->pVtabCurs
16d98 6f 72 3b 0a 20 20 20 20 63 6f 6e 73 74 20 73 71  or;.    const sq
16d99 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
16d9a 6f 64 75 6c 65 20 3d 20 70 43 78 2d 3e 70 4d 6f  odule = pCx->pMo
16d9b 64 75 6c 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56  dule;.    p->inV
16d9c 74 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20  tabMethod = 1;. 
16d9d 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33     (void)sqlite3
16d9e 53 61 66 65 74 79 4f 66 66 28 70 2d 3e 64 62 29  SafetyOff(p->db)
16d9f 3b 0a 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78  ;.    pModule->x
16da0 43 6c 6f 73 65 28 70 56 74 61 62 43 75 72 73 6f  Close(pVtabCurso
16da1 72 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71  r);.    (void)sq
16da2 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 70 2d  lite3SafetyOn(p-
16da3 3e 64 62 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56  >db);.    p->inV
16da4 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20  tabMethod = 0;. 
16da5 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20   }.#endif.  if( 
16da6 21 70 43 78 2d 3e 65 70 68 65 6d 50 73 65 75 64  !pCx->ephemPseud
16da7 6f 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 73 71  oTable ){.    sq
16da8 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64  lite3DbFree(p->d
16da9 62 2c 20 70 43 78 2d 3e 70 44 61 74 61 29 3b 0a  b, pCx->pData);.
16daa 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f    }.}../*.** Clo
16dab 73 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 65  se all cursors e
16dac 78 63 65 70 74 20 66 6f 72 20 56 54 61 62 20 63  xcept for VTab c
16dad 75 72 73 6f 72 73 20 74 68 61 74 20 61 72 65 20  ursors that are 
16dae 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 69 6e 20  currently.** in 
16daf 75 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  use..*/.static v
16db0 6f 69 64 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73  oid closeAllCurs
16db1 6f 72 73 45 78 63 65 70 74 41 63 74 69 76 65 56  orsExceptActiveV
16db2 74 61 62 73 28 56 64 62 65 20 2a 70 29 7b 0a 20  tabs(Vdbe *p){. 
16db3 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 2d   int i;.  if( p-
16db4 3e 61 70 43 73 72 3d 3d 30 20 29 20 72 65 74 75  >apCsr==0 ) retu
16db5 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  rn;.  for(i=0; i
16db6 3c 70 2d 3e 6e 43 75 72 73 6f 72 3b 20 69 2b 2b  <p->nCursor; i++
16db7 29 7b 0a 20 20 20 20 56 64 62 65 43 75 72 73 6f  ){.    VdbeCurso
16db8 72 20 2a 70 43 20 3d 20 70 2d 3e 61 70 43 73 72  r *pC = p->apCsr
16db9 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 43 20  [i];.    if( pC 
16dba 26 26 20 28 21 70 2d 3e 69 6e 56 74 61 62 4d 65  && (!p->inVtabMe
16dbb 74 68 6f 64 20 7c 7c 20 21 70 43 2d 3e 70 56 74  thod || !pC->pVt
16dbc 61 62 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20  abCursor) ){.   
16dbd 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 72     sqlite3VdbeFr
16dbe 65 65 43 75 72 73 6f 72 28 70 2c 20 70 43 29 3b  eeCursor(p, pC);
16dbf 0a 20 20 20 20 20 20 70 2d 3e 61 70 43 73 72 5b  .      p->apCsr[
16dc0 69 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  i] = 0;.    }.  
16dc1 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e  }.}../*.** Clean
16dc2 20 75 70 20 74 68 65 20 56 4d 20 61 66 74 65 72   up the VM after
16dc3 20 65 78 65 63 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a   execution..**.*
16dc4 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * This routine w
16dc5 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  ill automaticall
16dc6 79 20 63 6c 6f 73 65 20 61 6e 79 20 63 75 72 73  y close any curs
16dc7 6f 72 73 2c 20 6c 69 73 74 73 2c 20 61 6e 64 2f  ors, lists, and/
16dc8 6f 72 0a 2a 2a 20 73 6f 72 74 65 72 73 20 74 68  or.** sorters th
16dc9 61 74 20 77 65 72 65 20 6c 65 66 74 20 6f 70 65  at were left ope
16dca 6e 2e 20 20 49 74 20 61 6c 73 6f 20 64 65 6c 65  n.  It also dele
16dcb 74 65 73 20 74 68 65 20 76 61 6c 75 65 73 20 6f  tes the values o
16dcc 66 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 69  f.** variables i
16dcd 6e 20 74 68 65 20 61 56 61 72 5b 5d 20 61 72 72  n the aVar[] arr
16dce 61 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ay..*/.static vo
16dcf 69 64 20 43 6c 65 61 6e 75 70 28 56 64 62 65 20  id Cleanup(Vdbe 
16dd0 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  *p){.  int i;.  
16dd1 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d  sqlite3 *db = p-
16dd2 3e 64 62 3b 0a 20 20 63 6c 6f 73 65 41 6c 6c 43  >db;.  closeAllC
16dd3 75 72 73 6f 72 73 45 78 63 65 70 74 41 63 74 69  ursorsExceptActi
16dd4 76 65 56 74 61 62 73 28 70 29 3b 0a 20 20 66 6f  veVtabs(p);.  fo
16dd5 72 28 69 3d 31 3b 20 69 3c 3d 70 2d 3e 6e 4d 65  r(i=1; i<=p->nMe
16dd6 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d  m; i++){.    Mem
16dd7 53 65 74 54 79 70 65 46 6c 61 67 28 26 70 2d 3e  SetTypeFlag(&p->
16dd8 61 4d 65 6d 5b 69 5d 2c 20 4d 45 4d 5f 4e 75 6c  aMem[i], MEM_Nul
16dd9 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73  l);.  }.  releas
16dda 65 4d 65 6d 41 72 72 61 79 28 26 70 2d 3e 61 4d  eMemArray(&p->aM
16ddb 65 6d 5b 31 5d 2c 20 70 2d 3e 6e 4d 65 6d 29 3b  em[1], p->nMem);
16ddc 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69  .  sqlite3VdbeFi
16ddd 66 6f 43 6c 65 61 72 28 26 70 2d 3e 73 46 69 66  foClear(&p->sFif
16dde 6f 29 3b 0a 20 20 69 66 28 20 70 2d 3e 63 6f 6e  o);.  if( p->con
16ddf 74 65 78 74 53 74 61 63 6b 20 29 7b 0a 20 20 20  textStack ){.   
16de0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 63   for(i=0; i<p->c
16de1 6f 6e 74 65 78 74 53 74 61 63 6b 54 6f 70 3b 20  ontextStackTop; 
16de2 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  i++){.      sqli
16de3 74 65 33 56 64 62 65 46 69 66 6f 43 6c 65 61 72  te3VdbeFifoClear
16de4 28 26 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63  (&p->contextStac
16de5 6b 5b 69 5d 2e 73 46 69 66 6f 29 3b 0a 20 20 20  k[i].sFifo);.   
16de6 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62   }.    sqlite3Db
16de7 46 72 65 65 28 64 62 2c 20 70 2d 3e 63 6f 6e 74  Free(db, p->cont
16de8 65 78 74 53 74 61 63 6b 29 3b 0a 20 20 7d 0a 20  extStack);.  }. 
16de9 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b   p->contextStack
16dea 20 3d 20 30 3b 0a 20 20 70 2d 3e 63 6f 6e 74 65   = 0;.  p->conte
16deb 78 74 53 74 61 63 6b 44 65 70 74 68 20 3d 20 30  xtStackDepth = 0
16dec 3b 0a 20 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74  ;.  p->contextSt
16ded 61 63 6b 54 6f 70 20 3d 20 30 3b 0a 20 20 73 71  ackTop = 0;.  sq
16dee 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
16def 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70  p->zErrMsg);.  p
16df0 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20  ->zErrMsg = 0;. 
16df1 20 70 2d 3e 70 52 65 73 75 6c 74 53 65 74 20 3d   p->pResultSet =
16df2 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74   0;.}../*.** Set
16df3 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72   the number of r
16df4 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 74 68  esult columns th
16df5 61 74 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72  at will be retur
16df6 6e 65 64 20 62 79 20 74 68 69 73 20 53 51 4c 0a  ned by this SQL.
16df7 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68  ** statement. Th
16df8 69 73 20 69 73 20 6e 6f 77 20 73 65 74 20 61 74  is is now set at
16df9 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 72   compile time, r
16dfa 61 74 68 65 72 20 74 68 61 6e 20 64 75 72 69 6e  ather than durin
16dfb 67 0a 2a 2a 20 65 78 65 63 75 74 69 6f 6e 20 6f  g.** execution o
16dfc 66 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72  f the vdbe progr
16dfd 61 6d 20 73 6f 20 74 68 61 74 20 73 71 6c 69 74  am so that sqlit
16dfe 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28  e3_column_count(
16dff 29 20 63 61 6e 0a 2a 2a 20 62 65 20 63 61 6c 6c  ) can.** be call
16e00 65 64 20 6f 6e 20 61 6e 20 53 51 4c 20 73 74 61  ed on an SQL sta
16e01 74 65 6d 65 6e 74 20 62 65 66 6f 72 65 20 73 71  tement before sq
16e02 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 0a 2a 2f  lite3_step()..*/
16e03 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
16e04 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
16e05 53 65 74 4e 75 6d 43 6f 6c 73 28 56 64 62 65 20  SetNumCols(Vdbe 
16e06 2a 70 2c 20 69 6e 74 20 6e 52 65 73 43 6f 6c 75  *p, int nResColu
16e07 6d 6e 29 7b 0a 20 20 4d 65 6d 20 2a 70 43 6f 6c  mn){.  Mem *pCol
16e08 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20  Name;.  int n;. 
16e09 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
16e0a 2d 3e 64 62 3b 0a 0a 20 20 72 65 6c 65 61 73 65  ->db;..  release
16e0b 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61 43 6f 6c  MemArray(p->aCol
16e0c 4e 61 6d 65 2c 20 70 2d 3e 6e 52 65 73 43 6f 6c  Name, p->nResCol
16e0d 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 29 3b 0a  umn*COLNAME_N);.
16e0e 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
16e0f 64 62 2c 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 29  db, p->aColName)
16e10 3b 0a 20 20 6e 20 3d 20 6e 52 65 73 43 6f 6c 75  ;.  n = nResColu
16e11 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 3b 0a 20 20  mn*COLNAME_N;.  
16e12 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 3d 20  p->nResColumn = 
16e13 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 20 20 70 2d  nResColumn;.  p-
16e14 3e 61 43 6f 6c 4e 61 6d 65 20 3d 20 70 43 6f 6c  >aColName = pCol
16e15 4e 61 6d 65 20 3d 20 28 4d 65 6d 2a 29 73 71 6c  Name = (Mem*)sql
16e16 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
16e17 28 64 62 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29  (db, sizeof(Mem)
16e18 2a 6e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61  *n );.  if( p->a
16e19 43 6f 6c 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74  ColName==0 ) ret
16e1a 75 72 6e 3b 0a 20 20 77 68 69 6c 65 28 20 6e 2d  urn;.  while( n-
16e1b 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 70 43 6f  - > 0 ){.    pCo
16e1c 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 20 3d 20 4d  lName->flags = M
16e1d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 43 6f  EM_Null;.    pCo
16e1e 6c 4e 61 6d 65 2d 3e 64 62 20 3d 20 70 2d 3e 64  lName->db = p->d
16e1f 62 3b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2b  b;.    pColName+
16e20 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  +;.  }.}../*.** 
16e21 53 65 74 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  Set the name of 
16e22 74 68 65 20 69 64 78 27 74 68 20 63 6f 6c 75 6d  the idx'th colum
16e23 6e 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64  n to be returned
16e24 20 62 79 20 74 68 65 20 53 51 4c 20 73 74 61 74   by the SQL stat
16e25 65 6d 65 6e 74 2e 0a 2a 2a 20 7a 4e 61 6d 65 20  ement..** zName 
16e26 6d 75 73 74 20 62 65 20 61 20 70 6f 69 6e 74 65  must be a pointe
16e27 72 20 74 6f 20 61 20 6e 75 6c 20 74 65 72 6d 69  r to a nul termi
16e28 6e 61 74 65 64 20 73 74 72 69 6e 67 2e 0a 2a 2a  nated string..**
16e29 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 75  .** This call mu
16e2a 73 74 20 62 65 20 6d 61 64 65 20 61 66 74 65 72  st be made after
16e2b 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74   a call to sqlit
16e2c 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
16e2d 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  ()..**.** The fi
16e2e 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 2c 20 78  nal parameter, x
16e2f 44 65 6c 2c 20 6d 75 73 74 20 62 65 20 6f 6e 65  Del, must be one
16e30 20 6f 66 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d   of SQLITE_DYNAM
16e31 49 43 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  IC, SQLITE_STATI
16e32 43 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 54  C.** or SQLITE_T
16e33 52 41 4e 53 49 45 4e 54 2e 20 49 66 20 69 74 20  RANSIENT. If it 
16e34 69 73 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49  is SQLITE_DYNAMI
16e35 43 2c 20 74 68 65 6e 20 74 68 65 20 62 75 66 66  C, then the buff
16e36 65 72 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f  er pointed.** to
16e37 20 62 79 20 7a 4e 61 6d 65 20 77 69 6c 6c 20 62   by zName will b
16e38 65 20 66 72 65 65 64 20 62 79 20 73 71 6c 69 74  e freed by sqlit
16e39 65 33 44 62 46 72 65 65 28 29 20 77 68 65 6e 20  e3DbFree() when 
16e3a 74 68 65 20 76 64 62 65 20 69 73 20 64 65 73 74  the vdbe is dest
16e3b 72 6f 79 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  royed..*/.SQLITE
16e3c 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
16e3d 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
16e3e 6d 65 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20  me(.  Vdbe *p,  
16e3f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16e40 20 20 20 20 20 20 20 2f 2a 20 56 64 62 65 20 62         /* Vdbe b
16e41 65 69 6e 67 20 63 6f 6e 66 69 67 75 72 65 64 20  eing configured 
16e42 2a 2f 0a 20 20 69 6e 74 20 69 64 78 2c 20 20 20  */.  int idx,   
16e43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16e44 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
16e45 66 20 63 6f 6c 75 6d 6e 20 7a 4e 61 6d 65 20 61  f column zName a
16e46 70 70 6c 69 65 73 20 74 6f 20 2a 2f 0a 20 20 69  pplies to */.  i
16e47 6e 74 20 76 61 72 2c 20 20 20 20 20 20 20 20 20  nt var,         
16e48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16e49 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 43 4f  /* One of the CO
16e4a 4c 4e 41 4d 45 5f 2a 20 63 6f 6e 73 74 61 6e 74  LNAME_* constant
16e4b 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  s */.  const cha
16e4c 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20  r *zName,       
16e4d 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
16e4e 65 72 20 74 6f 20 62 75 66 66 65 72 20 63 6f 6e  er to buffer con
16e4f 74 61 69 6e 69 6e 67 20 6e 61 6d 65 20 2a 2f 0a  taining name */.
16e50 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76    void (*xDel)(v
16e51 6f 69 64 2a 29 20 20 20 20 20 20 20 20 20 20 20  oid*)           
16e52 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 6d 61 6e     /* Memory man
16e53 61 67 65 6d 65 6e 74 20 73 74 72 61 74 65 67 79  agement strategy
16e54 20 66 6f 72 20 7a 4e 61 6d 65 20 2a 2f 0a 29 7b   for zName */.){
16e55 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d  .  int rc;.  Mem
16e56 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 61 73   *pColName;.  as
16e57 73 65 72 74 28 20 69 64 78 3c 70 2d 3e 6e 52 65  sert( idx<p->nRe
16e58 73 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 61 73 73  sColumn );.  ass
16e59 65 72 74 28 20 76 61 72 3c 43 4f 4c 4e 41 4d 45  ert( var<COLNAME
16e5a 5f 4e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 64  _N );.  if( p->d
16e5b 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
16e5c 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  ){.    assert( !
16e5d 7a 4e 61 6d 65 20 7c 7c 20 78 44 65 6c 21 3d 53  zName || xDel!=S
16e5e 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 29 3b  QLITE_DYNAMIC );
16e5f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
16e60 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
16e61 61 73 73 65 72 74 28 20 70 2d 3e 61 43 6f 6c 4e  assert( p->aColN
16e62 61 6d 65 21 3d 30 20 29 3b 0a 20 20 70 43 6f 6c  ame!=0 );.  pCol
16e63 4e 61 6d 65 20 3d 20 26 28 70 2d 3e 61 43 6f 6c  Name = &(p->aCol
16e64 4e 61 6d 65 5b 69 64 78 2b 76 61 72 2a 70 2d 3e  Name[idx+var*p->
16e65 6e 52 65 73 43 6f 6c 75 6d 6e 5d 29 3b 0a 20 20  nResColumn]);.  
16e66 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
16e67 4d 65 6d 53 65 74 53 74 72 28 70 43 6f 6c 4e 61  MemSetStr(pColNa
16e68 6d 65 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53  me, zName, -1, S
16e69 51 4c 49 54 45 5f 55 54 46 38 2c 20 78 44 65 6c  QLITE_UTF8, xDel
16e6a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21  );.  assert( rc!
16e6b 3d 30 20 7c 7c 20 21 7a 4e 61 6d 65 20 7c 7c 20  =0 || !zName || 
16e6c 28 70 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73  (pColName->flags
16e6d 26 4d 45 4d 5f 54 65 72 6d 29 21 3d 30 20 29 3b  &MEM_Term)!=0 );
16e6e 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
16e6f 0a 2f 2a 0a 2a 2a 20 41 20 72 65 61 64 20 6f 72  ./*.** A read or
16e70 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
16e71 6f 6e 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f  on may or may no
16e72 74 20 62 65 20 61 63 74 69 76 65 20 6f 6e 20 64  t be active on d
16e73 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a  atabase handle.*
16e74 2a 20 64 62 2e 20 49 66 20 61 20 74 72 61 6e 73  * db. If a trans
16e75 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65  action is active
16e76 2c 20 63 6f 6d 6d 69 74 20 69 74 2e 20 49 66 20  , commit it. If 
16e77 74 68 65 72 65 20 69 73 20 61 0a 2a 2a 20 77 72  there is a.** wr
16e78 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20  ite-transaction 
16e79 73 70 61 6e 6e 69 6e 67 20 6d 6f 72 65 20 74 68  spanning more th
16e7a 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20  an one database 
16e7b 66 69 6c 65 2c 20 74 68 69 73 20 72 6f 75 74 69  file, this routi
16e7c 6e 65 0a 2a 2a 20 74 61 6b 65 73 20 63 61 72 65  ne.** takes care
16e7d 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a   of the master j
16e7e 6f 75 72 6e 61 6c 20 74 72 69 63 6b 65 72 79 2e  ournal trickery.
16e7f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76  .*/.static int v
16e80 64 62 65 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65  dbeCommit(sqlite
16e81 33 20 2a 64 62 2c 20 56 64 62 65 20 2a 70 29 7b  3 *db, Vdbe *p){
16e82 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
16e83 6e 54 72 61 6e 73 20 3d 20 30 3b 20 20 2f 2a 20  nTrans = 0;  /* 
16e84 4e 75 6d 62 65 72 20 6f 66 20 64 61 74 61 62 61  Number of databa
16e85 73 65 73 20 77 69 74 68 20 61 6e 20 61 63 74 69  ses with an acti
16e86 76 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63  ve write-transac
16e87 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63  tion */.  int rc
16e88 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
16e89 69 6e 74 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20  int needXcommit 
16e8a 3d 20 30 3b 0a 0a 20 20 2f 2a 20 42 65 66 6f 72  = 0;..  /* Befor
16e8b 65 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67  e doing anything
16e8c 20 65 6c 73 65 2c 20 63 61 6c 6c 20 74 68 65 20   else, call the 
16e8d 78 53 79 6e 63 28 29 20 63 61 6c 6c 62 61 63 6b  xSync() callback
16e8e 20 66 6f 72 20 61 6e 79 0a 20 20 2a 2a 20 76 69   for any.  ** vi
16e8f 72 74 75 61 6c 20 6d 6f 64 75 6c 65 20 74 61 62  rtual module tab
16e90 6c 65 73 20 77 72 69 74 74 65 6e 20 69 6e 20 74  les written in t
16e91 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  his transaction.
16e92 20 54 68 69 73 20 68 61 73 20 74 6f 0a 20 20 2a   This has to.  *
16e93 2a 20 62 65 20 64 6f 6e 65 20 62 65 66 6f 72 65  * be done before
16e94 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 77 68 65   determining whe
16e95 74 68 65 72 20 61 20 6d 61 73 74 65 72 20 6a 6f  ther a master jo
16e96 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 0a 20  urnal file is . 
16e97 20 2a 2a 20 72 65 71 75 69 72 65 64 2c 20 61 73   ** required, as
16e98 20 61 6e 20 78 53 79 6e 63 28 29 20 63 61 6c 6c   an xSync() call
16e99 62 61 63 6b 20 6d 61 79 20 61 64 64 20 61 6e 20  back may add an 
16e9a 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
16e9b 65 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 74 72  e.  ** to the tr
16e9c 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a  ansaction..  */.
16e9d 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74    rc = sqlite3Vt
16e9e 61 62 53 79 6e 63 28 64 62 2c 20 26 70 2d 3e 7a  abSync(db, &p->z
16e9f 45 72 72 4d 73 67 29 3b 0a 20 20 69 66 28 20 72  ErrMsg);.  if( r
16ea0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
16ea1 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
16ea2 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f   }..  /* This lo
16ea3 6f 70 20 64 65 74 65 72 6d 69 6e 65 73 20 28 61  op determines (a
16ea4 29 20 69 66 20 74 68 65 20 63 6f 6d 6d 69 74 20  ) if the commit 
16ea5 68 6f 6f 6b 20 73 68 6f 75 6c 64 20 62 65 20 69  hook should be i
16ea6 6e 76 6f 6b 65 64 20 61 6e 64 0a 20 20 2a 2a 20  nvoked and.  ** 
16ea7 28 62 29 20 68 6f 77 20 6d 61 6e 79 20 64 61 74  (b) how many dat
16ea8 61 62 61 73 65 20 66 69 6c 65 73 20 68 61 76 65  abase files have
16ea9 20 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e   open write tran
16eaa 73 61 63 74 69 6f 6e 73 2c 20 6e 6f 74 20 0a 20  sactions, not . 
16eab 20 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 74 68   ** including th
16eac 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 2e  e temp database.
16ead 20 28 62 29 20 69 73 20 69 6d 70 6f 72 74 61 6e   (b) is importan
16eae 74 20 62 65 63 61 75 73 65 20 69 66 20 6d 6f 72  t because if mor
16eaf 65 20 74 68 61 6e 20 0a 20 20 2a 2a 20 6f 6e 65  e than .  ** one
16eb0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68   database file h
16eb1 61 73 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 65  as an open write
16eb2 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 20   transaction, a 
16eb3 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 20  master journal. 
16eb4 20 2a 2a 20 66 69 6c 65 20 69 73 20 72 65 71 75   ** file is requ
16eb5 69 72 65 64 20 66 6f 72 20 61 6e 20 61 74 6f 6d  ired for an atom
16eb6 69 63 20 63 6f 6d 6d 69 74 2e 0a 20 20 2a 2f 20  ic commit..  */ 
16eb7 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62  .  for(i=0; i<db
16eb8 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20  ->nDb; i++){ .  
16eb9 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64    Btree *pBt = d
16eba 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20  b->aDb[i].pBt;. 
16ebb 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74     if( sqlite3Bt
16ebc 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 42 74  reeIsInTrans(pBt
16ebd 29 20 29 7b 0a 20 20 20 20 20 20 6e 65 65 64 58  ) ){.      needX
16ebe 63 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20  commit = 1;.    
16ebf 20 20 69 66 28 20 69 21 3d 31 20 29 20 6e 54 72    if( i!=1 ) nTr
16ec0 61 6e 73 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  ans++;.    }.  }
16ec1 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
16ec2 61 72 65 20 61 6e 79 20 77 72 69 74 65 2d 74 72  are any write-tr
16ec3 61 6e 73 61 63 74 69 6f 6e 73 20 61 74 20 61 6c  ansactions at al
16ec4 6c 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 6f  l, invoke the co
16ec5 6d 6d 69 74 20 68 6f 6f 6b 20 2a 2f 0a 20 20 69  mmit hook */.  i
16ec6 66 28 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 26  f( needXcommit &
16ec7 26 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c  & db->xCommitCal
16ec8 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 28 76 6f  lback ){.    (vo
16ec9 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
16eca 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 72 63 20  Off(db);.    rc 
16ecb 3d 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c  = db->xCommitCal
16ecc 6c 62 61 63 6b 28 64 62 2d 3e 70 43 6f 6d 6d 69  lback(db->pCommi
16ecd 74 41 72 67 29 3b 0a 20 20 20 20 28 76 6f 69 64  tArg);.    (void
16ece 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e  )sqlite3SafetyOn
16ecf 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 72 63  (db);.    if( rc
16ed0 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
16ed1 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49   SQLITE_CONSTRAI
16ed2 4e 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  NT;.    }.  }.. 
16ed3 20 2f 2a 20 54 68 65 20 73 69 6d 70 6c 65 20 63   /* The simple c
16ed4 61 73 65 20 2d 20 6e 6f 20 6d 6f 72 65 20 74 68  ase - no more th
16ed5 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20  an one database 
16ed6 66 69 6c 65 20 28 6e 6f 74 20 63 6f 75 6e 74 69  file (not counti
16ed7 6e 67 20 74 68 65 0a 20 20 2a 2a 20 54 45 4d 50  ng the.  ** TEMP
16ed8 20 64 61 74 61 62 61 73 65 29 20 68 61 73 20 61   database) has a
16ed9 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 63 74   transaction act
16eda 69 76 65 2e 20 20 20 54 68 65 72 65 20 69 73 20  ive.   There is 
16edb 6e 6f 20 6e 65 65 64 20 66 6f 72 20 74 68 65 0a  no need for the.
16edc 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75 72    ** master-jour
16edd 6e 61 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  nal..  **.  ** I
16ede 66 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  f the return val
16edf 75 65 20 6f 66 20 73 71 6c 69 74 65 33 42 74 72  ue of sqlite3Btr
16ee0 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 29 20  eeGetFilename() 
16ee1 69 73 20 61 20 7a 65 72 6f 20 6c 65 6e 67 74 68  is a zero length
16ee2 0a 20 20 2a 2a 20 73 74 72 69 6e 67 2c 20 69 74  .  ** string, it
16ee3 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 6e 20   means the main 
16ee4 64 61 74 61 62 61 73 65 20 69 73 20 3a 6d 65 6d  database is :mem
16ee5 6f 72 79 3a 20 6f 72 20 61 20 74 65 6d 70 20 66  ory: or a temp f
16ee6 69 6c 65 2e 20 20 49 6e 20 0a 20 20 2a 2a 20 74  ile.  In .  ** t
16ee7 68 61 74 20 63 61 73 65 20 77 65 20 64 6f 20 6e  hat case we do n
16ee8 6f 74 20 73 75 70 70 6f 72 74 20 61 74 6f 6d 69  ot support atomi
16ee9 63 20 6d 75 6c 74 69 2d 66 69 6c 65 20 63 6f 6d  c multi-file com
16eea 6d 69 74 73 2c 20 73 6f 20 75 73 65 20 74 68 65  mits, so use the
16eeb 20 0a 20 20 2a 2a 20 73 69 6d 70 6c 65 20 63 61   .  ** simple ca
16eec 73 65 20 74 68 65 6e 20 74 6f 6f 2e 0a 20 20 2a  se then too..  *
16eed 2f 0a 20 20 69 66 28 20 30 3d 3d 73 74 72 6c 65  /.  if( 0==strle
16eee 6e 28 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  n(sqlite3BtreeGe
16eef 74 46 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44  tFilename(db->aD
16ef0 62 5b 30 5d 2e 70 42 74 29 29 20 7c 7c 20 6e 54  b[0].pBt)) || nT
16ef1 72 61 6e 73 3c 3d 31 20 29 7b 0a 20 20 20 20 66  rans<=1 ){.    f
16ef2 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49  or(i=0; rc==SQLI
16ef3 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e  TE_OK && i<db->n
16ef4 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20  Db; i++){ .     
16ef5 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62   Btree *pBt = db
16ef6 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20  ->aDb[i].pBt;.  
16ef7 20 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20      if( pBt ){. 
16ef8 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
16ef9 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68  te3BtreeCommitPh
16efa 61 73 65 4f 6e 65 28 70 42 74 2c 20 30 29 3b 0a  aseOne(pBt, 0);.
16efb 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
16efc 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d     /* Do the com
16efd 6d 69 74 20 6f 6e 6c 79 20 69 66 20 61 6c 6c 20  mit only if all 
16efe 64 61 74 61 62 61 73 65 73 20 73 75 63 63 65 73  databases succes
16eff 73 66 75 6c 6c 79 20 63 6f 6d 70 6c 65 74 65 20  sfully complete 
16f00 70 68 61 73 65 20 31 2e 20 0a 20 20 20 20 2a 2a  phase 1. .    **
16f01 20 49 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 42   If one of the B
16f02 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f  treeCommitPhaseO
16f03 6e 65 28 29 20 63 61 6c 6c 73 20 66 61 69 6c 73  ne() calls fails
16f04 2c 20 74 68 69 73 20 69 6e 64 69 63 61 74 65 73  , this indicates
16f05 20 61 6e 0a 20 20 20 20 2a 2a 20 49 4f 20 65 72   an.    ** IO er
16f06 72 6f 72 20 77 68 69 6c 65 20 64 65 6c 65 74 69  ror while deleti
16f07 6e 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67  ng or truncating
16f08 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e   a journal file.
16f09 20 49 74 20 69 73 20 75 6e 6c 69 6b 65 6c 79 2c   It is unlikely,
16f0a 0a 20 20 20 20 2a 2a 20 62 75 74 20 63 6f 75 6c  .    ** but coul
16f0b 64 20 68 61 70 70 65 6e 2e 20 49 6e 20 74 68 69  d happen. In thi
16f0c 73 20 63 61 73 65 20 61 62 61 6e 64 6f 6e 20 70  s case abandon p
16f0d 72 6f 63 65 73 73 69 6e 67 20 61 6e 64 20 72 65  rocessing and re
16f0e 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 2e 0a  turn the error..
16f0f 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69      */.    for(i
16f10 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
16f11 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  K && i<db->nDb; 
16f12 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74 72 65  i++){.      Btre
16f13 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62  e *pBt = db->aDb
16f14 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69  [i].pBt;.      i
16f15 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20  f( pBt ){.      
16f16 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
16f17 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77  reeCommitPhaseTw
16f18 6f 28 70 42 74 29 3b 0a 20 20 20 20 20 20 7d 0a  o(pBt);.      }.
16f19 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
16f1a 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
16f1b 20 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62       sqlite3Vtab
16f1c 43 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 20 20 20  Commit(db);.    
16f1d 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20  }.  }..  /* The 
16f1e 63 6f 6d 70 6c 65 78 20 63 61 73 65 20 2d 20 54  complex case - T
16f1f 68 65 72 65 20 69 73 20 61 20 6d 75 6c 74 69 2d  here is a multi-
16f20 66 69 6c 65 20 77 72 69 74 65 2d 74 72 61 6e 73  file write-trans
16f21 61 63 74 69 6f 6e 20 61 63 74 69 76 65 2e 0a 20  action active.. 
16f22 20 2a 2a 20 54 68 69 73 20 72 65 71 75 69 72 65   ** This require
16f23 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  s a master journ
16f24 61 6c 20 66 69 6c 65 20 74 6f 20 65 6e 73 75 72  al file to ensur
16f25 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  e the transactio
16f26 6e 20 69 73 0a 20 20 2a 2a 20 63 6f 6d 6d 69 74  n is.  ** commit
16f27 74 65 64 20 61 74 6f 6d 69 63 6c 79 2e 0a 20 20  ted atomicly..  
16f28 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
16f29 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 20 20  E_OMIT_DISKIO.  
16f2a 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
16f2b 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 64 62  3_vfs *pVfs = db
16f2c 2d 3e 70 56 66 73 3b 0a 20 20 20 20 69 6e 74 20  ->pVfs;.    int 
16f2d 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20  needSync = 0;.  
16f2e 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 20    char *zMaster 
16f2f 3d 20 30 3b 20 20 20 2f 2a 20 46 69 6c 65 2d 6e  = 0;   /* File-n
16f30 61 6d 65 20 66 6f 72 20 74 68 65 20 6d 61 73 74  ame for the mast
16f31 65 72 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20  er journal */.  
16f32 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d    char const *zM
16f33 61 69 6e 46 69 6c 65 20 3d 20 73 71 6c 69 74 65  ainFile = sqlite
16f34 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d  3BtreeGetFilenam
16f35 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74  e(db->aDb[0].pBt
16f36 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
16f37 69 6c 65 20 2a 70 4d 61 73 74 65 72 20 3d 20 30  ile *pMaster = 0
16f38 3b 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65 74  ;.    i64 offset
16f39 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 72 65   = 0;.    int re
16f3a 73 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 6c 65 63  s;..    /* Selec
16f3b 74 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  t a master journ
16f3c 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a  al file name */.
16f3d 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 75      do {.      u
16f3e 33 32 20 72 61 6e 64 6f 6d 3b 0a 20 20 20 20 20  32 random;.     
16f3f 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
16f40 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20  b, zMaster);.   
16f41 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f     sqlite3_rando
16f42 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 72 61 6e  mness(sizeof(ran
16f43 64 6f 6d 29 2c 20 26 72 61 6e 64 6f 6d 29 3b 0a  dom), &random);.
16f44 20 20 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20        zMaster = 
16f45 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
16f46 62 2c 20 22 25 73 2d 6d 6a 25 30 38 58 22 2c 20  b, "%s-mj%08X", 
16f47 7a 4d 61 69 6e 46 69 6c 65 2c 20 72 61 6e 64 6f  zMainFile, rando
16f48 6d 26 30 78 37 66 66 66 66 66 66 66 29 3b 0a 20  m&0x7fffffff);. 
16f49 20 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74 65       if( !zMaste
16f4a 72 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  r ){.        ret
16f4b 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
16f4c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
16f4d 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63  rc = sqlite3OsAc
16f4e 63 65 73 73 28 70 56 66 73 2c 20 7a 4d 61 73 74  cess(pVfs, zMast
16f4f 65 72 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53  er, SQLITE_ACCES
16f50 53 5f 45 58 49 53 54 53 2c 20 26 72 65 73 29 3b  S_EXISTS, &res);
16f51 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 72 63 3d  .    }while( rc=
16f52 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 65  =SQLITE_OK && re
16f53 73 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  s );.    if( rc=
16f54 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
16f55 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20      /* Open the 
16f56 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 20  master journal. 
16f57 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  */.      rc = sq
16f58 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f  lite3OsOpenMallo
16f59 63 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c  c(pVfs, zMaster,
16f5a 20 26 70 4d 61 73 74 65 72 2c 20 0a 20 20 20 20   &pMaster, .    
16f5b 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
16f5c 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49  N_READWRITE|SQLI
16f5d 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 0a  TE_OPEN_CREATE|.
16f5e 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
16f5f 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 7c  _OPEN_EXCLUSIVE|
16f60 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54  SQLITE_OPEN_MAST
16f61 45 52 5f 4a 4f 55 52 4e 41 4c 2c 20 30 0a 20 20  ER_JOURNAL, 0.  
16f62 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20      );.    }.   
16f63 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
16f64 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  OK ){.      sqli
16f65 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d  te3DbFree(db, zM
16f66 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 72 65  aster);.      re
16f67 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
16f68 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68  .    /* Write th
16f69 65 20 6e 61 6d 65 20 6f 66 20 65 61 63 68 20 64  e name of each d
16f6a 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20  atabase file in 
16f6b 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
16f6c 69 6e 74 6f 20 74 68 65 20 6e 65 77 0a 20 20 20  into the new.   
16f6d 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e   ** master journ
16f6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 61 6e 20 65  al file. If an e
16f6f 72 72 6f 72 20 6f 63 63 75 72 73 20 61 74 20 74  rror occurs at t
16f70 68 69 73 20 70 6f 69 6e 74 20 63 6c 6f 73 65 0a  his point close.
16f71 20 20 20 20 2a 2a 20 61 6e 64 20 64 65 6c 65 74      ** and delet
16f72 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  e the master jou
16f73 72 6e 61 6c 20 66 69 6c 65 2e 20 41 6c 6c 20 74  rnal file. All t
16f74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f  he individual jo
16f75 75 72 6e 61 6c 20 66 69 6c 65 73 0a 20 20 20 20  urnal files.    
16f76 2a 2a 20 73 74 69 6c 6c 20 68 61 76 65 20 27 6e  ** still have 'n
16f77 75 6c 6c 27 20 61 73 20 74 68 65 20 6d 61 73 74  ull' as the mast
16f78 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74  er journal point
16f79 65 72 2c 20 73 6f 20 74 68 65 79 20 77 69 6c 6c  er, so they will
16f7a 20 72 6f 6c 6c 0a 20 20 20 20 2a 2a 20 62 61 63   roll.    ** bac
16f7b 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79 20  k independently 
16f7c 69 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63 63  if a failure occ
16f7d 75 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  urs..    */.    
16f7e 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
16f7f 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  Db; i++){.      
16f80 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d  Btree *pBt = db-
16f81 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20  >aDb[i].pBt;.   
16f82 20 20 20 69 66 28 20 69 3d 3d 31 20 29 20 63 6f     if( i==1 ) co
16f83 6e 74 69 6e 75 65 3b 20 20 20 2f 2a 20 49 67 6e  ntinue;   /* Ign
16f84 6f 72 65 20 74 68 65 20 54 45 4d 50 20 64 61 74  ore the TEMP dat
16f85 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 69  abase */.      i
16f86 66 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  f( sqlite3BtreeI
16f87 73 49 6e 54 72 61 6e 73 28 70 42 74 29 20 29 7b  sInTrans(pBt) ){
16f88 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 63 6f  .        char co
16f89 6e 73 74 20 2a 7a 46 69 6c 65 20 3d 20 73 71 6c  nst *zFile = sql
16f8a 69 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75 72  ite3BtreeGetJour
16f8b 6e 61 6c 6e 61 6d 65 28 70 42 74 29 3b 0a 20 20  nalname(pBt);.  
16f8c 20 20 20 20 20 20 69 66 28 20 7a 46 69 6c 65 5b        if( zFile[
16f8d 30 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  0]==0 ) continue
16f8e 3b 20 20 2f 2a 20 49 67 6e 6f 72 65 20 3a 6d 65  ;  /* Ignore :me
16f8f 6d 6f 72 79 3a 20 64 61 74 61 62 61 73 65 73 20  mory: databases 
16f90 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  */.        if( !
16f91 6e 65 65 64 53 79 6e 63 20 26 26 20 21 73 71 6c  needSync && !sql
16f92 69 74 65 33 42 74 72 65 65 53 79 6e 63 44 69 73  ite3BtreeSyncDis
16f93 61 62 6c 65 64 28 70 42 74 29 20 29 7b 0a 20 20  abled(pBt) ){.  
16f94 20 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e 63          needSync
16f95 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 1;.        }.
16f96 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
16f97 69 74 65 33 4f 73 57 72 69 74 65 28 70 4d 61 73  ite3OsWrite(pMas
16f98 74 65 72 2c 20 7a 46 69 6c 65 2c 20 73 74 72 6c  ter, zFile, strl
16f99 65 6e 28 7a 46 69 6c 65 29 2b 31 2c 20 6f 66 66  en(zFile)+1, off
16f9a 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6f 66  set);.        of
16f9b 66 73 65 74 20 2b 3d 20 73 74 72 6c 65 6e 28 7a  fset += strlen(z
16f9c 46 69 6c 65 29 2b 31 3b 0a 20 20 20 20 20 20 20  File)+1;.       
16f9d 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
16f9e 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
16f9f 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72  sqlite3OsCloseFr
16fa0 65 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20  ee(pMaster);.   
16fa1 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73         sqlite3Os
16fa2 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d 61  Delete(pVfs, zMa
16fa3 73 74 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 20  ster, 0);.      
16fa4 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
16fa5 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a  e(db, zMaster);.
16fa6 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
16fa7 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   rc;.        }. 
16fa8 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
16fa9 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 6d 61    /* Sync the ma
16faa 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
16fab 65 2e 20 49 66 20 74 68 65 20 49 4f 43 41 50 5f  e. If the IOCAP_
16fac 53 45 51 55 45 4e 54 49 41 4c 20 64 65 76 69 63  SEQUENTIAL devic
16fad 65 0a 20 20 20 20 2a 2a 20 66 6c 61 67 20 69 73  e.    ** flag is
16fae 20 73 65 74 20 74 68 69 73 20 69 73 20 6e 6f 74   set this is not
16faf 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a   required..    *
16fb0 2f 0a 20 20 20 20 7a 4d 61 69 6e 46 69 6c 65 20  /.    zMainFile 
16fb1 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
16fb2 74 44 69 72 6e 61 6d 65 28 64 62 2d 3e 61 44 62  tDirname(db->aDb
16fb3 5b 30 5d 2e 70 42 74 29 3b 0a 20 20 20 20 69 66  [0].pBt);.    if
16fb4 28 20 28 6e 65 65 64 53 79 6e 63 20 0a 20 20 20  ( (needSync .   
16fb5 20 20 26 26 20 28 30 3d 3d 28 73 71 6c 69 74 65    && (0==(sqlite
16fb6 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74  3OsDeviceCharact
16fb7 65 72 69 73 74 69 63 73 28 70 4d 61 73 74 65 72  eristics(pMaster
16fb8 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53  )&SQLITE_IOCAP_S
16fb9 45 51 55 45 4e 54 49 41 4c 29 29 0a 20 20 20 20  EQUENTIAL)).    
16fba 20 26 26 20 28 72 63 3d 73 71 6c 69 74 65 33 4f   && (rc=sqlite3O
16fbb 73 53 79 6e 63 28 70 4d 61 73 74 65 72 2c 20 53  sSync(pMaster, S
16fbc 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41  QLITE_SYNC_NORMA
16fbd 4c 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20  L))!=SQLITE_OK) 
16fbe 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
16fbf 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 73  OsCloseFree(pMas
16fc0 74 65 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ter);.      sqli
16fc1 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73  te3OsDelete(pVfs
16fc2 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20  , zMaster, 0);. 
16fc3 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
16fc4 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b  ee(db, zMaster);
16fc5 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
16fc6 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
16fc7 53 79 6e 63 20 61 6c 6c 20 74 68 65 20 64 62 20  Sync all the db 
16fc8 66 69 6c 65 73 20 69 6e 76 6f 6c 76 65 64 20 69  files involved i
16fc9 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  n the transactio
16fca 6e 2e 20 54 68 65 20 73 61 6d 65 20 63 61 6c 6c  n. The same call
16fcb 0a 20 20 20 20 2a 2a 20 73 65 74 73 20 74 68 65  .    ** sets the
16fcc 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
16fcd 70 6f 69 6e 74 65 72 20 69 6e 20 65 61 63 68 20  pointer in each 
16fce 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e  individual journ
16fcf 61 6c 2e 20 49 66 0a 20 20 20 20 2a 2a 20 61 6e  al. If.    ** an
16fd0 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 68 65   error occurs he
16fd1 72 65 2c 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74  re, do not delet
16fd2 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  e the master jou
16fd3 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 2a  rnal file..    *
16fd4 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20  *.    ** If the 
16fd5 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72  error occurs dur
16fd6 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 63 61  ing the first ca
16fd7 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c  ll to.    ** sql
16fd8 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50  ite3BtreeCommitP
16fd9 68 61 73 65 4f 6e 65 28 29 2c 20 74 68 65 6e 20  haseOne(), then 
16fda 74 68 65 72 65 20 69 73 20 61 20 63 68 61 6e 63  there is a chanc
16fdb 65 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a  e that the.    *
16fdc 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  * master journal
16fdd 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 6f 72   file will be or
16fde 70 68 61 6e 65 64 2e 20 42 75 74 20 77 65 20 63  phaned. But we c
16fdf 61 6e 6e 6f 74 20 64 65 6c 65 74 65 20 69 74 2c  annot delete it,
16fe0 0a 20 20 20 20 2a 2a 20 69 6e 20 63 61 73 65 20  .    ** in case 
16fe1 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
16fe2 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 77 61 73  al file name was
16fe3 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
16fe4 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a  e journal.    **
16fe5 20 66 69 6c 65 20 62 65 66 6f 72 65 20 74 68 65   file before the
16fe6 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 65 64   failure occured
16fe7 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72  ..    */.    for
16fe8 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45  (i=0; rc==SQLITE
16fe9 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62  _OK && i<db->nDb
16fea 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 42  ; i++){ .      B
16feb 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e  tree *pBt = db->
16fec 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20  aDb[i].pBt;.    
16fed 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20    if( pBt ){.   
16fee 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
16fef 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73  3BtreeCommitPhas
16ff0 65 4f 6e 65 28 70 42 74 2c 20 7a 4d 61 73 74 65  eOne(pBt, zMaste
16ff1 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  r);.      }.    
16ff2 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43  }.    sqlite3OsC
16ff3 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65 72  loseFree(pMaster
16ff4 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
16ff5 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
16ff6 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
16ff7 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20  db, zMaster);.  
16ff8 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
16ff9 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c     }..    /* Del
16ffa 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a  ete the master j
16ffb 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69  ournal file. Thi
16ffc 73 20 63 6f 6d 6d 69 74 73 20 74 68 65 20 74 72  s commits the tr
16ffd 61 6e 73 61 63 74 69 6f 6e 2e 20 41 66 74 65 72  ansaction. After
16ffe 0a 20 20 20 20 2a 2a 20 64 6f 69 6e 67 20 74 68  .    ** doing th
16fff 69 73 20 74 68 65 20 64 69 72 65 63 74 6f 72 79  is the directory
17000 20 69 73 20 73 79 6e 63 65 64 20 61 67 61 69 6e   is synced again
17001 20 62 65 66 6f 72 65 20 61 6e 79 20 69 6e 64 69   before any indi
17002 76 69 64 75 61 6c 0a 20 20 20 20 2a 2a 20 74 72  vidual.    ** tr
17003 61 6e 73 61 63 74 69 6f 6e 20 66 69 6c 65 73 20  ansaction files 
17004 61 72 65 20 64 65 6c 65 74 65 64 2e 0a 20 20 20  are deleted..   
17005 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   */.    rc = sql
17006 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66  ite3OsDelete(pVf
17007 73 2c 20 7a 4d 61 73 74 65 72 2c 20 31 29 3b 0a  s, zMaster, 1);.
17008 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
17009 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a  e(db, zMaster);.
1700a 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b      zMaster = 0;
1700b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20  .    if( rc ){. 
1700c 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1700d 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 6c      }..    /* Al
1700e 6c 20 66 69 6c 65 73 20 61 6e 64 20 64 69 72 65  l files and dire
1700f 63 74 6f 72 69 65 73 20 68 61 76 65 20 61 6c 72  ctories have alr
17010 65 61 64 79 20 62 65 65 6e 20 73 79 6e 63 65 64  eady been synced
17011 2c 20 73 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  , so the followi
17012 6e 67 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 73 20  ng.    ** calls 
17013 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  to sqlite3BtreeC
17014 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20  ommitPhaseTwo() 
17015 61 72 65 20 6f 6e 6c 79 20 63 6c 6f 73 69 6e 67  are only closing
17016 20 66 69 6c 65 73 20 61 6e 64 0a 20 20 20 20 2a   files and.    *
17017 2a 20 64 65 6c 65 74 69 6e 67 20 6f 72 20 74 72  * deleting or tr
17018 75 6e 63 61 74 69 6e 67 20 6a 6f 75 72 6e 61 6c  uncating journal
17019 73 2e 20 49 66 20 73 6f 6d 65 74 68 69 6e 67 20  s. If something 
1701a 67 6f 65 73 20 77 72 6f 6e 67 20 77 68 69 6c 65  goes wrong while
1701b 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 69 73 20  .    ** this is 
1701c 68 61 70 70 65 6e 69 6e 67 20 77 65 20 64 6f 6e  happening we don
1701d 27 74 20 72 65 61 6c 6c 79 20 63 61 72 65 2e 20  't really care. 
1701e 54 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66  The integrity of
1701f 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e   the.    ** tran
17020 73 61 63 74 69 6f 6e 20 69 73 20 61 6c 72 65 61  saction is alrea
17021 64 79 20 67 75 61 72 61 6e 74 65 65 64 2c 20 62  dy guaranteed, b
17022 75 74 20 73 6f 6d 65 20 73 74 72 61 79 20 27 63  ut some stray 'c
17023 6f 6c 64 27 20 6a 6f 75 72 6e 61 6c 73 0a 20 20  old' journals.  
17024 20 20 2a 2a 20 6d 61 79 20 62 65 20 6c 79 69 6e    ** may be lyin
17025 67 20 61 72 6f 75 6e 64 2e 20 52 65 74 75 72 6e  g around. Return
17026 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  ing an error cod
17027 65 20 77 6f 6e 27 74 20 68 65 6c 70 20 6d 61 74  e won't help mat
17028 74 65 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ters..    */.   
17029 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74   disable_simulat
1702a 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a  ed_io_errors();.
1702b 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e      sqlite3Begin
1702c 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
1702d 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64      for(i=0; i<d
1702e 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20  b->nDb; i++){ . 
1702f 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20       Btree *pBt 
17030 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74  = db->aDb[i].pBt
17031 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20  ;.      if( pBt 
17032 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
17033 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
17034 73 65 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 20  seTwo(pBt);.    
17035 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71    }.    }.    sq
17036 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61  lite3EndBenignMa
17037 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 65 6e 61 62  lloc();.    enab
17038 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f  le_simulated_io_
17039 65 72 72 6f 72 73 28 29 3b 0a 0a 20 20 20 20 73  errors();..    s
1703a 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74  qlite3VtabCommit
1703b 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  (db);.  }.#endif
1703c 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
1703d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f  ../* .** This ro
1703e 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 68 61  utine checks tha
1703f 74 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 63  t the sqlite3.ac
17040 74 69 76 65 56 64 62 65 43 6e 74 20 63 6f 75 6e  tiveVdbeCnt coun
17041 74 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61  t variable.** ma
17042 74 63 68 65 73 20 74 68 65 20 6e 75 6d 62 65 72  tches the number
17043 20 6f 66 20 76 64 62 65 27 73 20 69 6e 20 74 68   of vdbe's in th
17044 65 20 6c 69 73 74 20 73 71 6c 69 74 65 33 2e 70  e list sqlite3.p
17045 56 64 62 65 20 74 68 61 74 20 61 72 65 0a 2a 2a  Vdbe that are.**
17046 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76   currently activ
17047 65 2e 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20  e. An assertion 
17048 66 61 69 6c 73 20 69 66 20 74 68 65 20 74 77 6f  fails if the two
17049 20 63 6f 75 6e 74 73 20 64 6f 20 6e 6f 74 20 6d   counts do not m
1704a 61 74 63 68 2e 0a 2a 2a 20 54 68 69 73 20 69 73  atch..** This is
1704b 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 73 65 6c   an internal sel
1704c 66 2d 63 68 65 63 6b 20 6f 6e 6c 79 20 2d 20 69  f-check only - i
1704d 74 20 69 73 20 6e 6f 74 20 61 6e 20 65 73 73 65  t is not an esse
1704e 6e 74 69 61 6c 20 70 72 6f 63 65 73 73 69 6e 67  ntial processing
1704f 0a 2a 2a 20 73 74 65 70 2e 0a 2a 2a 0a 2a 2a 20  .** step..**.** 
17050 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20  This is a no-op 
17051 69 66 20 4e 44 45 42 55 47 20 69 73 20 64 65 66  if NDEBUG is def
17052 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  ined..*/.#ifndef
17053 20 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20 76   NDEBUG.static v
17054 6f 69 64 20 63 68 65 63 6b 41 63 74 69 76 65 56  oid checkActiveV
17055 64 62 65 43 6e 74 28 73 71 6c 69 74 65 33 20 2a  dbeCnt(sqlite3 *
17056 64 62 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a  db){.  Vdbe *p;.
17057 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20    int cnt = 0;. 
17058 20 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 30 3b   int nWrite = 0;
17059 0a 20 20 70 20 3d 20 64 62 2d 3e 70 56 64 62 65  .  p = db->pVdbe
1705a 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a  ;.  while( p ){.
1705b 20 20 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63      if( p->magic
1705c 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e  ==VDBE_MAGIC_RUN
1705d 20 26 26 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a   && p->pc>=0 ){.
1705e 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20        cnt++;.   
1705f 20 20 20 69 66 28 20 70 2d 3e 72 65 61 64 4f 6e     if( p->readOn
17060 6c 79 3d 3d 30 20 29 20 6e 57 72 69 74 65 2b 2b  ly==0 ) nWrite++
17061 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20  ;.    }.    p = 
17062 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20  p->pNext;.  }.  
17063 61 73 73 65 72 74 28 20 63 6e 74 3d 3d 64 62 2d  assert( cnt==db-
17064 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 20 29  >activeVdbeCnt )
17065 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 57 72 69  ;.  assert( nWri
17066 74 65 3d 3d 64 62 2d 3e 77 72 69 74 65 56 64 62  te==db->writeVdb
17067 65 43 6e 74 20 29 3b 0a 7d 0a 23 65 6c 73 65 0a  eCnt );.}.#else.
17068 23 64 65 66 69 6e 65 20 63 68 65 63 6b 41 63 74  #define checkAct
17069 69 76 65 56 64 62 65 43 6e 74 28 78 29 0a 23 65  iveVdbeCnt(x).#e
1706a 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20  ndif../*.** For 
1706b 65 76 65 72 79 20 42 74 72 65 65 20 74 68 61 74  every Btree that
1706c 20 69 6e 20 64 61 74 61 62 61 73 65 20 63 6f 6e   in database con
1706d 6e 65 63 74 69 6f 6e 20 64 62 20 77 68 69 63 68  nection db which
1706e 20 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f   .** has been mo
1706f 64 69 66 69 65 64 2c 20 22 74 72 69 70 22 20 6f  dified, "trip" o
17070 72 20 69 6e 76 61 6c 69 64 61 74 65 20 65 61 63  r invalidate eac
17071 68 20 63 75 72 73 6f 72 20 69 6e 0a 2a 2a 20 74  h cursor in.** t
17072 68 61 74 20 42 74 72 65 65 20 6d 69 67 68 74 20  hat Btree might 
17073 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69  have been modifi
17074 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 63  ed so that the c
17075 75 72 73 6f 72 0a 2a 2a 20 63 61 6e 20 6e 65 76  ursor.** can nev
17076 65 72 20 62 65 20 75 73 65 64 20 61 67 61 69 6e  er be used again
17077 2e 20 20 54 68 69 73 20 68 61 70 70 65 6e 73 20  .  This happens 
17078 77 68 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 0a  when a rollback.
17079 2a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 57 65 20  *** occurs.  We 
1707a 68 61 76 65 20 74 6f 20 74 72 69 70 20 61 6c 6c  have to trip all
1707b 20 74 68 65 20 6f 74 68 65 72 20 63 75 72 73 6f   the other curso
1707c 72 73 2c 20 65 76 65 6e 0a 2a 2a 20 63 75 72 73  rs, even.** curs
1707d 6f 72 20 66 72 6f 6d 20 6f 74 68 65 72 20 56 4d  or from other VM
1707e 73 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 64  s in different d
1707f 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
17080 6f 6e 73 2c 0a 2a 2a 20 73 6f 20 74 68 61 74 20  ons,.** so that 
17081 6e 6f 6e 65 20 6f 66 20 74 68 65 6d 20 74 72 79  none of them try
17082 20 74 6f 20 75 73 65 20 74 68 65 20 64 61 74 61   to use the data
17083 20 61 74 20 77 68 69 63 68 20 74 68 65 79 0a 2a   at which they.*
17084 2a 20 77 65 72 65 20 70 6f 69 6e 74 69 6e 67 20  * were pointing 
17085 61 6e 64 20 77 68 69 63 68 20 6e 6f 77 20 6d 61  and which now ma
17086 79 20 68 61 76 65 20 62 65 65 6e 20 63 68 61 6e  y have been chan
17087 67 65 64 20 64 75 65 0a 2a 2a 20 74 6f 20 74 68  ged due.** to th
17088 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a  e rollback..**.*
17089 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 61 74 20  * Remember that 
1708a 61 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6e 20 64  a rollback can d
1708b 65 6c 65 74 65 20 74 61 62 6c 65 73 20 63 6f 6d  elete tables com
1708c 70 6c 65 74 65 20 61 6e 64 0a 2a 2a 20 72 65 6f  plete and.** reo
1708d 72 64 65 72 20 72 6f 6f 74 70 61 67 65 73 2e 20  rder rootpages. 
1708e 20 53 6f 20 69 74 20 69 73 20 6e 6f 74 20 73 75   So it is not su
1708f 66 66 69 63 69 65 6e 74 20 6a 75 73 74 20 74 6f  fficient just to
17090 20 73 61 76 65 0a 2a 2a 20 74 68 65 20 73 74 61   save.** the sta
17091 74 65 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72  te of the cursor
17092 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e  .  We have to in
17093 76 61 6c 69 64 61 74 65 20 74 68 65 20 63 75 72  validate the cur
17094 73 6f 72 0a 2a 2a 20 73 6f 20 74 68 61 74 20 69  sor.** so that i
17095 74 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 20  t is never used 
17096 61 67 61 69 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  again..*/.static
17097 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65   void invalidate
17098 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65  CursorsOnModifie
17099 64 42 74 72 65 65 73 28 73 71 6c 69 74 65 33 20  dBtrees(sqlite3 
1709a 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  *db){.  int i;. 
1709b 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
1709c 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42  nDb; i++){.    B
1709d 74 72 65 65 20 2a 70 20 3d 20 64 62 2d 3e 61 44  tree *p = db->aD
1709e 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66  b[i].pBt;.    if
1709f 28 20 70 20 26 26 20 73 71 6c 69 74 65 33 42 74  ( p && sqlite3Bt
170a0 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 29 20  reeIsInTrans(p) 
170a1 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
170a2 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73  BtreeTripAllCurs
170a3 6f 72 73 28 70 2c 20 53 51 4c 49 54 45 5f 41 42  ors(p, SQLITE_AB
170a4 4f 52 54 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ORT);.    }.  }.
170a5 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
170a6 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
170a7 74 68 65 20 77 68 65 6e 20 61 20 56 44 42 45 20  the when a VDBE 
170a8 74 72 69 65 73 20 74 6f 20 68 61 6c 74 2e 20 20  tries to halt.  
170a9 49 66 20 74 68 65 20 56 44 42 45 0a 2a 2a 20 68  If the VDBE.** h
170aa 61 73 20 6d 61 64 65 20 63 68 61 6e 67 65 73 20  as made changes 
170ab 61 6e 64 20 69 73 20 69 6e 20 61 75 74 6f 63 6f  and is in autoco
170ac 6d 6d 69 74 20 6d 6f 64 65 2c 20 74 68 65 6e 20  mmit mode, then 
170ad 63 6f 6d 6d 69 74 20 74 68 6f 73 65 0a 2a 2a 20  commit those.** 
170ae 63 68 61 6e 67 65 73 2e 20 20 49 66 20 61 20 72  changes.  If a r
170af 6f 6c 6c 62 61 63 6b 20 69 73 20 6e 65 65 64 65  ollback is neede
170b0 64 2c 20 74 68 65 6e 20 64 6f 20 74 68 65 20 72  d, then do the r
170b1 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54  ollback..**.** T
170b2 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74  his routine is t
170b3 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d  he only way to m
170b4 6f 76 65 20 74 68 65 20 73 74 61 74 65 20 6f 66  ove the state of
170b5 20 61 20 56 4d 20 66 72 6f 6d 0a 2a 2a 20 53 51   a VM from.** SQ
170b6 4c 49 54 45 5f 4d 41 47 49 43 5f 52 55 4e 20 74  LITE_MAGIC_RUN t
170b7 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 48  o SQLITE_MAGIC_H
170b8 41 4c 54 2e 20 20 49 74 20 69 73 20 68 61 72 6d  ALT.  It is harm
170b9 6c 65 73 73 20 74 6f 0a 2a 2a 20 63 61 6c 6c 20  less to.** call 
170ba 74 68 69 73 20 6f 6e 20 61 20 56 4d 20 74 68 61  this on a VM tha
170bb 74 20 69 73 20 69 6e 20 74 68 65 20 53 51 4c 49  t is in the SQLI
170bc 54 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 73 74  TE_MAGIC_HALT st
170bd 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ate..**.** Retur
170be 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e  n an error code.
170bf 20 20 49 66 20 74 68 65 20 63 6f 6d 6d 69 74 20    If the commit 
170c0 63 6f 75 6c 64 20 6e 6f 74 20 63 6f 6d 70 6c 65  could not comple
170c1 74 65 20 62 65 63 61 75 73 65 20 6f 66 0a 2a 2a  te because of.**
170c2 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e   lock contention
170c3 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
170c4 42 55 53 59 2e 20 20 49 66 20 53 51 4c 49 54 45  BUSY.  If SQLITE
170c5 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 6e 65  _BUSY is returne
170c6 64 2c 20 69 74 0a 2a 2a 20 6d 65 61 6e 73 20 74  d, it.** means t
170c7 68 65 20 63 6c 6f 73 65 20 64 69 64 20 6e 6f 74  he close did not
170c8 20 68 61 70 70 65 6e 20 61 6e 64 20 6e 65 65 64   happen and need
170c9 73 20 74 6f 20 62 65 20 72 65 70 65 61 74 65 64  s to be repeated
170ca 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
170cb 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
170cc 64 62 65 48 61 6c 74 28 56 64 62 65 20 2a 70 29  dbeHalt(Vdbe *p)
170cd 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
170ce 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69  = p->db;.  int i
170cf 3b 0a 20 20 69 6e 74 20 28 2a 78 46 75 6e 63 29  ;.  int (*xFunc)
170d0 28 42 74 72 65 65 20 2a 70 42 74 29 20 3d 20 30  (Btree *pBt) = 0
170d1 3b 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74  ;  /* Function t
170d2 6f 20 63 61 6c 6c 20 6f 6e 20 65 61 63 68 20 62  o call on each b
170d3 74 72 65 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a  tree backend */.
170d4 20 20 69 6e 74 20 69 73 53 70 65 63 69 61 6c 45    int isSpecialE
170d5 72 72 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20  rror;           
170d6 20 2f 2a 20 53 65 74 20 74 6f 20 74 72 75 65 20   /* Set to true 
170d7 69 66 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  if SQLITE_NOMEM 
170d8 6f 72 20 49 4f 45 52 52 20 2a 2f 0a 0a 20 20 2f  or IOERR */..  /
170d9 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
170da 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6c 6f 67  contains the log
170db 69 63 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e  ic that determin
170dc 65 73 20 69 66 20 61 20 73 74 61 74 65 6d 65 6e  es if a statemen
170dd 74 20 6f 72 0a 20 20 2a 2a 20 74 72 61 6e 73 61  t or.  ** transa
170de 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63 6f  ction will be co
170df 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65  mmitted or rolle
170e0 64 20 62 61 63 6b 20 61 73 20 61 20 72 65 73 75  d back as a resu
170e1 6c 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 65  lt of the.  ** e
170e2 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 69 73  xecution of this
170e3 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
170e4 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20  . .  **.  ** If 
170e5 61 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  any of the follo
170e6 77 69 6e 67 20 65 72 72 6f 72 73 20 6f 63 63 75  wing errors occu
170e7 72 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  r:.  **.  **    
170e8 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20   SQLITE_NOMEM.  
170e9 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f  **     SQLITE_IO
170ea 45 52 52 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c  ERR.  **     SQL
170eb 49 54 45 5f 46 55 4c 4c 0a 20 20 2a 2a 20 20 20  ITE_FULL.  **   
170ec 20 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55    SQLITE_INTERRU
170ed 50 54 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  PT.  **.  ** The
170ee 6e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 63  n the internal c
170ef 61 63 68 65 20 6d 69 67 68 74 20 68 61 76 65 20  ache might have 
170f0 62 65 65 6e 20 6c 65 66 74 20 69 6e 20 61 6e 20  been left in an 
170f1 69 6e 63 6f 6e 73 69 73 74 65 6e 74 0a 20 20 2a  inconsistent.  *
170f2 2a 20 73 74 61 74 65 2e 20 20 57 65 20 6e 65 65  * state.  We nee
170f3 64 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68  d to rollback th
170f4 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  e statement tran
170f5 73 61 63 74 69 6f 6e 2c 20 69 66 20 74 68 65 72  saction, if ther
170f6 65 20 69 73 0a 20 20 2a 2a 20 6f 6e 65 2c 20 6f  e is.  ** one, o
170f7 72 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74  r the complete t
170f8 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68  ransaction if th
170f9 65 72 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d  ere is no statem
170fa 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ent transaction.
170fb 0a 20 20 2a 2f 0a 0a 20 20 69 66 28 20 70 2d 3e  .  */..  if( p->
170fc 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
170fd 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20   ){.    p->rc = 
170fe 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
170ff 7d 0a 20 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73  }.  closeAllCurs
17100 6f 72 73 45 78 63 65 70 74 41 63 74 69 76 65 56  orsExceptActiveV
17101 74 61 62 73 28 70 29 3b 0a 20 20 69 66 28 20 70  tabs(p);.  if( p
17102 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41  ->magic!=VDBE_MA
17103 47 49 43 5f 52 55 4e 20 29 7b 0a 20 20 20 20 72  GIC_RUN ){.    r
17104 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
17105 0a 20 20 7d 0a 20 20 63 68 65 63 6b 41 63 74 69  .  }.  checkActi
17106 76 65 56 64 62 65 43 6e 74 28 64 62 29 3b 0a 0a  veVdbeCnt(db);..
17107 20 20 2f 2a 20 4e 6f 20 63 6f 6d 6d 69 74 20 6f    /* No commit o
17108 72 20 72 6f 6c 6c 62 61 63 6b 20 6e 65 65 64 65  r rollback neede
17109 64 20 69 66 20 74 68 65 20 70 72 6f 67 72 61 6d  d if the program
1710a 20 6e 65 76 65 72 20 73 74 61 72 74 65 64 20 2a   never started *
1710b 2f 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30  /.  if( p->pc>=0
1710c 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 72 63 3b   ){.    int mrc;
1710d 20 20 20 2f 2a 20 50 72 69 6d 61 72 79 20 65 72     /* Primary er
1710e 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 70 2d  ror code from p-
1710f 3e 72 63 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 4c  >rc */..    /* L
17110 6f 63 6b 20 61 6c 6c 20 62 74 72 65 65 73 20 75  ock all btrees u
17111 73 65 64 20 62 79 20 74 68 65 20 73 74 61 74 65  sed by the state
17112 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 73 71 6c 69  ment */.    sqli
17113 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72  te3BtreeMutexArr
17114 61 79 45 6e 74 65 72 28 26 70 2d 3e 61 4d 75 74  ayEnter(&p->aMut
17115 65 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65  ex);..    /* Che
17116 63 6b 20 66 6f 72 20 6f 6e 65 20 6f 66 20 74 68  ck for one of th
17117 65 20 73 70 65 63 69 61 6c 20 65 72 72 6f 72 73  e special errors
17118 20 2a 2f 0a 20 20 20 20 6d 72 63 20 3d 20 70 2d   */.    mrc = p-
17119 3e 72 63 20 26 20 30 78 66 66 3b 0a 20 20 20 20  >rc & 0xff;.    
1711a 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 20 3d  isSpecialError =
1711b 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d   mrc==SQLITE_NOM
1711c 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54  EM || mrc==SQLIT
1711d 45 5f 49 4f 45 52 52 0a 20 20 20 20 20 20 20 20  E_IOERR.        
1711e 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
1711f 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45  mrc==SQLITE_INTE
17120 52 52 55 50 54 20 7c 7c 20 6d 72 63 3d 3d 53 51  RRUPT || mrc==SQ
17121 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 69  LITE_FULL;.    i
17122 66 28 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f  f( isSpecialErro
17123 72 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66  r ){.      /* If
17124 20 74 68 65 20 71 75 65 72 79 20 77 61 73 20 72   the query was r
17125 65 61 64 2d 6f 6e 6c 79 2c 20 77 65 20 6e 65 65  ead-only, we nee
17126 64 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b  d do no rollback
17127 20 61 74 20 61 6c 6c 2e 20 4f 74 68 65 72 77 69   at all. Otherwi
17128 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 70 72 6f  se,.      ** pro
17129 63 65 65 64 20 77 69 74 68 20 74 68 65 20 73 70  ceed with the sp
1712a 65 63 69 61 6c 20 68 61 6e 64 6c 69 6e 67 2e 0a  ecial handling..
1712b 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
1712c 66 28 20 21 70 2d 3e 72 65 61 64 4f 6e 6c 79 20  f( !p->readOnly 
1712d 7c 7c 20 6d 72 63 21 3d 53 51 4c 49 54 45 5f 49  || mrc!=SQLITE_I
1712e 4e 54 45 52 52 55 50 54 20 29 7b 0a 20 20 20 20  NTERRUPT ){.    
1712f 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53      if( p->rc==S
17130 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43  QLITE_IOERR_BLOC
17131 4b 45 44 20 26 26 20 70 2d 3e 75 73 65 73 53 74  KED && p->usesSt
17132 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20  mtJournal ){.   
17133 20 20 20 20 20 20 20 78 46 75 6e 63 20 3d 20 73         xFunc = s
17134 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62  qlite3BtreeRollb
17135 61 63 6b 53 74 6d 74 3b 0a 20 20 20 20 20 20 20  ackStmt;.       
17136 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54     p->rc = SQLIT
17137 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20  E_BUSY;.        
17138 7d 65 6c 73 65 20 69 66 28 20 28 6d 72 63 3d 3d  }else if( (mrc==
17139 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20  SQLITE_NOMEM || 
1713a 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c  mrc==SQLITE_FULL
1713b 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ).              
1713c 20 20 20 20 20 26 26 20 70 2d 3e 75 73 65 73 53       && p->usesS
1713d 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20  tmtJournal ){.  
1713e 20 20 20 20 20 20 20 20 78 46 75 6e 63 20 3d 20          xFunc = 
1713f 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c  sqlite3BtreeRoll
17140 62 61 63 6b 53 74 6d 74 3b 0a 20 20 20 20 20 20  backStmt;.      
17141 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
17142 20 20 20 2f 2a 20 57 65 20 61 72 65 20 66 6f 72     /* We are for
17143 63 65 64 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b  ced to roll back
17144 20 74 68 65 20 61 63 74 69 76 65 20 74 72 61 6e   the active tran
17145 73 61 63 74 69 6f 6e 2e 20 42 65 66 6f 72 65 20  saction. Before 
17146 64 6f 69 6e 67 0a 20 20 20 20 20 20 20 20 20 20  doing.          
17147 2a 2a 20 73 6f 2c 20 61 62 6f 72 74 20 61 6e 79  ** so, abort any
17148 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e 74   other statement
17149 73 20 74 68 69 73 20 68 61 6e 64 6c 65 20 63 75  s this handle cu
1714a 72 72 65 6e 74 6c 79 20 68 61 73 20 61 63 74 69  rrently has acti
1714b 76 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f  ve..          */
1714c 0a 20 20 20 20 20 20 20 20 20 20 69 6e 76 61 6c  .          inval
1714d 69 64 61 74 65 43 75 72 73 6f 72 73 4f 6e 4d 6f  idateCursorsOnMo
1714e 64 69 66 69 65 64 42 74 72 65 65 73 28 64 62 29  difiedBtrees(db)
1714f 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
17150 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64  te3RollbackAll(d
17151 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62  b);.          db
17152 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31  ->autoCommit = 1
17153 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
17154 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20    }.    }.  .   
17155 20 2f 2a 20 49 66 20 74 68 65 20 61 75 74 6f 2d   /* If the auto-
17156 63 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73 20 73  commit flag is s
17157 65 74 20 61 6e 64 20 74 68 69 73 20 69 73 20 74  et and this is t
17158 68 65 20 6f 6e 6c 79 20 61 63 74 69 76 65 20 76  he only active v
17159 64 62 65 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a  dbe, then.    **
1715a 20 77 65 20 64 6f 20 65 69 74 68 65 72 20 61 20   we do either a 
1715b 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61  commit or rollba
1715c 63 6b 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  ck of the curren
1715d 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a  t transaction. .
1715e 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f      **.    ** No
1715f 74 65 3a 20 54 68 69 73 20 62 6c 6f 63 6b 20 61  te: This block a
17160 6c 73 6f 20 72 75 6e 73 20 69 66 20 6f 6e 65 20  lso runs if one 
17161 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 65  of the special e
17162 72 72 6f 72 73 20 68 61 6e 64 6c 65 64 20 0a 20  rrors handled . 
17163 20 20 20 2a 2a 20 61 62 6f 76 65 20 68 61 73 20     ** above has 
17164 6f 63 63 75 72 72 65 64 2e 20 0a 20 20 20 20 2a  occurred. .    *
17165 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 75  /.    if( db->au
17166 74 6f 43 6f 6d 6d 69 74 20 26 26 20 64 62 2d 3e  toCommit && db->
17167 77 72 69 74 65 56 64 62 65 43 6e 74 3d 3d 28 70  writeVdbeCnt==(p
17168 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d 30 29 20 29  ->readOnly==0) )
17169 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 72  {.      if( p->r
1716a 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
1716b 28 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 3d  (p->errorAction=
1716c 3d 4f 45 5f 46 61 69 6c 20 26 26 20 21 69 73 53  =OE_Fail && !isS
1716d 70 65 63 69 61 6c 45 72 72 6f 72 29 20 29 7b 0a  pecialError) ){.
1716e 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61          /* The a
1716f 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 20  uto-commit flag 
17170 69 73 20 74 72 75 65 2c 20 61 6e 64 20 74 68 65  is true, and the
17171 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 77 61   vdbe program wa
17172 73 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 75  s .        ** su
17173 63 63 65 73 73 66 75 6c 20 6f 72 20 68 69 74 20  ccessful or hit 
17174 61 6e 20 27 4f 52 20 46 41 49 4c 27 20 63 6f 6e  an 'OR FAIL' con
17175 73 74 72 61 69 6e 74 2e 20 54 68 69 73 20 6d 65  straint. This me
17176 61 6e 73 20 61 20 63 6f 6d 6d 69 74 20 0a 20 20  ans a commit .  
17177 20 20 20 20 20 20 2a 2a 20 69 73 20 72 65 71 75        ** is requ
17178 69 72 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f  ired..        */
17179 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 63 20  .        int rc 
1717a 3d 20 76 64 62 65 43 6f 6d 6d 69 74 28 64 62 2c  = vdbeCommit(db,
1717b 20 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   p);.        if(
1717c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59   rc==SQLITE_BUSY
1717d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
1717e 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41  lite3BtreeMutexA
1717f 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d  rrayLeave(&p->aM
17180 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20 20  utex);.         
17181 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
17182 55 53 59 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  USY;.        }el
17183 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  se if( rc!=SQLIT
17184 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
17185 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20    p->rc = rc;.  
17186 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52          sqlite3R
17187 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a  ollbackAll(db);.
17188 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
17189 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1718a 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68  CommitInternalCh
1718b 61 6e 67 65 73 28 64 62 29 3b 0a 20 20 20 20 20  anges(db);.     
1718c 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
1718d 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1718e 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29  3RollbackAll(db)
1718f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
17190 6c 73 65 20 69 66 28 20 21 78 46 75 6e 63 20 29  lse if( !xFunc )
17191 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 72  {.      if( p->r
17192 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
17193 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 3d 3d  p->errorAction==
17194 4f 45 5f 46 61 69 6c 20 29 7b 0a 20 20 20 20 20  OE_Fail ){.     
17195 20 20 20 69 66 28 20 70 2d 3e 6f 70 65 6e 65 64     if( p->opened
17196 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20 20  Statement ){.   
17197 20 20 20 20 20 20 20 78 46 75 6e 63 20 3d 20 73         xFunc = s
17198 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
17199 74 53 74 6d 74 3b 0a 20 20 20 20 20 20 20 20 7d  tStmt;.        }
1719a 20 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66   .      }else if
1719b 28 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e  ( p->errorAction
1719c 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b 0a 20 20  ==OE_Abort ){.  
1719d 20 20 20 20 20 20 78 46 75 6e 63 20 3d 20 73 71        xFunc = sq
1719e 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61  lite3BtreeRollba
1719f 63 6b 53 74 6d 74 3b 0a 20 20 20 20 20 20 7d 65  ckStmt;.      }e
171a0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 76  lse{.        inv
171a1 61 6c 69 64 61 74 65 43 75 72 73 6f 72 73 4f 6e  alidateCursorsOn
171a2 4d 6f 64 69 66 69 65 64 42 74 72 65 65 73 28 64  ModifiedBtrees(d
171a3 62 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  b);.        sqli
171a4 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64  te3RollbackAll(d
171a5 62 29 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e  b);.        db->
171a6 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a  autoCommit = 1;.
171a7 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
171a8 0a 20 20 20 20 2f 2a 20 49 66 20 78 46 75 6e 63  .    /* If xFunc
171a9 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   is not NULL, th
171aa 65 6e 20 69 74 20 69 73 20 6f 6e 65 20 6f 66 20  en it is one of 
171ab 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c  sqlite3BtreeRoll
171ac 62 61 63 6b 53 74 6d 74 20 6f 72 0a 20 20 20 20  backStmt or.    
171ad 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  ** sqlite3BtreeC
171ae 6f 6d 6d 69 74 53 74 6d 74 2e 20 43 61 6c 6c 20  ommitStmt. Call 
171af 69 74 20 6f 6e 63 65 20 6f 6e 20 65 61 63 68 20  it once on each 
171b0 62 61 63 6b 65 6e 64 2e 20 49 66 20 61 6e 20 65  backend. If an e
171b1 72 72 6f 72 20 6f 63 63 75 72 73 0a 20 20 20 20  rror occurs.    
171b2 2a 2a 20 61 6e 64 20 74 68 65 20 72 65 74 75 72  ** and the retur
171b3 6e 20 63 6f 64 65 20 69 73 20 73 74 69 6c 6c 20  n code is still 
171b4 53 51 4c 49 54 45 5f 4f 4b 2c 20 73 65 74 20 74  SQLITE_OK, set t
171b5 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 74  he return code t
171b6 6f 20 74 68 65 20 6e 65 77 0a 20 20 20 20 2a 2a  o the new.    **
171b7 20 65 72 72 6f 72 20 76 61 6c 75 65 2e 0a 20 20   error value..  
171b8 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
171b9 21 78 46 75 6e 63 20 7c 7c 0a 20 20 20 20 20 20  !xFunc ||.      
171ba 78 46 75 6e 63 3d 3d 73 71 6c 69 74 65 33 42 74  xFunc==sqlite3Bt
171bb 72 65 65 43 6f 6d 6d 69 74 53 74 6d 74 20 7c 7c  reeCommitStmt ||
171bc 0a 20 20 20 20 20 20 78 46 75 6e 63 3d 3d 73 71  .      xFunc==sq
171bd 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61  lite3BtreeRollba
171be 63 6b 53 74 6d 74 0a 20 20 20 20 29 3b 0a 20 20  ckStmt.    );.  
171bf 20 20 66 6f 72 28 69 3d 30 3b 20 78 46 75 6e 63    for(i=0; xFunc
171c0 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69   && i<db->nDb; i
171c1 2b 2b 29 7b 20 0a 20 20 20 20 20 20 69 6e 74 20  ++){ .      int 
171c2 72 63 3b 0a 20 20 20 20 20 20 42 74 72 65 65 20  rc;.      Btree 
171c3 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69  *pBt = db->aDb[i
171c4 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28  ].pBt;.      if(
171c5 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20   pBt ){.        
171c6 72 63 20 3d 20 78 46 75 6e 63 28 70 42 74 29 3b  rc = xFunc(pBt);
171c7 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20  .        if( rc 
171c8 26 26 20 28 70 2d 3e 72 63 3d 3d 53 51 4c 49 54  && (p->rc==SQLIT
171c9 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53  E_OK || p->rc==S
171ca 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
171cb 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  ) ){.          p
171cc 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 20  ->rc = rc;.     
171cd 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
171ce 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73  ee(db, p->zErrMs
171cf 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d  g);.          p-
171d0 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  >zErrMsg = 0;.  
171d1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
171d2 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20      }.  .    /* 
171d3 49 66 20 74 68 69 73 20 77 61 73 20 61 6e 20 49  If this was an I
171d4 4e 53 45 52 54 2c 20 55 50 44 41 54 45 20 6f 72  NSERT, UPDATE or
171d5 20 44 45 4c 45 54 45 20 61 6e 64 20 74 68 65 20   DELETE and the 
171d6 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 63 6f  statement was co
171d7 6d 6d 69 74 74 65 64 2c 20 0a 20 20 20 20 2a 2a  mmitted, .    **
171d8 20 73 65 74 20 74 68 65 20 63 68 61 6e 67 65 20   set the change 
171d9 63 6f 75 6e 74 65 72 2e 20 0a 20 20 20 20 2a 2f  counter. .    */
171da 0a 20 20 20 20 69 66 28 20 70 2d 3e 63 68 61 6e  .    if( p->chan
171db 67 65 43 6e 74 4f 6e 20 26 26 20 70 2d 3e 70 63  geCntOn && p->pc
171dc 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  >=0 ){.      if(
171dd 20 21 78 46 75 6e 63 20 7c 7c 20 78 46 75 6e 63   !xFunc || xFunc
171de 3d 3d 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  ==sqlite3BtreeCo
171df 6d 6d 69 74 53 74 6d 74 20 29 7b 0a 20 20 20 20  mmitStmt ){.    
171e0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
171e1 65 74 43 68 61 6e 67 65 73 28 64 62 2c 20 70 2d  etChanges(db, p-
171e2 3e 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 20  >nChange);.     
171e3 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
171e4 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 68  sqlite3VdbeSetCh
171e5 61 6e 67 65 73 28 64 62 2c 20 30 29 3b 0a 20 20  anges(db, 0);.  
171e6 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 6e      }.      p->n
171e7 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 20 20  Change = 0;.    
171e8 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 52 6f 6c 6c  }.  .    /* Roll
171e9 62 61 63 6b 20 6f 72 20 63 6f 6d 6d 69 74 20 61  back or commit a
171ea 6e 79 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65  ny schema change
171eb 73 20 74 68 61 74 20 6f 63 63 75 72 72 65 64 2e  s that occurred.
171ec 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 72   */.    if( p->r
171ed 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c!=SQLITE_OK && 
171ee 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45  db->flags&SQLITE
171ef 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 20 29  _InternChanges )
171f0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  {.      sqlite3R
171f1 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65  esetInternalSche
171f2 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 20 20 20  ma(db, 0);.     
171f3 20 64 62 2d 3e 66 6c 61 67 73 20 3d 20 28 64 62   db->flags = (db
171f4 2d 3e 66 6c 61 67 73 20 7c 20 53 51 4c 49 54 45  ->flags | SQLITE
171f5 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 29 3b  _InternChanges);
171f6 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52  .    }..    /* R
171f7 65 6c 65 61 73 65 20 74 68 65 20 6c 6f 63 6b 73  elease the locks
171f8 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42   */.    sqlite3B
171f9 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65  treeMutexArrayLe
171fa 61 76 65 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b  ave(&p->aMutex);
171fb 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 65 20 68 61  .  }..  /* We ha
171fc 76 65 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  ve successfully 
171fd 68 61 6c 74 65 64 20 61 6e 64 20 63 6c 6f 73 65  halted and close
171fe 64 20 74 68 65 20 56 4d 2e 20 20 52 65 63 6f 72  d the VM.  Recor
171ff 64 20 74 68 69 73 20 66 61 63 74 2e 20 2a 2f 0a  d this fact. */.
17200 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29    if( p->pc>=0 )
17201 7b 0a 20 20 20 20 64 62 2d 3e 61 63 74 69 76 65  {.    db->active
17202 56 64 62 65 43 6e 74 2d 2d 3b 0a 20 20 20 20 69  VdbeCnt--;.    i
17203 66 28 20 21 70 2d 3e 72 65 61 64 4f 6e 6c 79 20  f( !p->readOnly 
17204 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 77 72 69  ){.      db->wri
17205 74 65 56 64 62 65 43 6e 74 2d 2d 3b 0a 20 20 20  teVdbeCnt--;.   
17206 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 64   }.    assert( d
17207 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74  b->activeVdbeCnt
17208 3e 3d 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43  >=db->writeVdbeC
17209 6e 74 20 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6d  nt );.  }.  p->m
1720a 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49  agic = VDBE_MAGI
1720b 43 5f 48 41 4c 54 3b 0a 20 20 63 68 65 63 6b 41  C_HALT;.  checkA
1720c 63 74 69 76 65 56 64 62 65 43 6e 74 28 64 62 29  ctiveVdbeCnt(db)
1720d 3b 0a 20 20 69 66 28 20 70 2d 3e 64 62 2d 3e 6d  ;.  if( p->db->m
1720e 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1720f 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54     p->rc = SQLIT
17210 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20  E_NOMEM;.  }..  
17211 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
17212 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68  ;.}.../*.** Each
17213 20 56 44 42 45 20 68 6f 6c 64 73 20 74 68 65 20   VDBE holds the 
17214 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 6d 6f  result of the mo
17215 73 74 20 72 65 63 65 6e 74 20 73 71 6c 69 74 65  st recent sqlite
17216 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 0a 2a 2a  3_step() call.**
17217 20 69 6e 20 70 2d 3e 72 63 2e 20 20 54 68 69 73   in p->rc.  This
17218 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68   routine sets th
17219 61 74 20 72 65 73 75 6c 74 20 62 61 63 6b 20 74  at result back t
1721a 6f 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a  o SQLITE_OK..*/.
1721b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1721c 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 52  oid sqlite3VdbeR
1721d 65 73 65 74 53 74 65 70 52 65 73 75 6c 74 28 56  esetStepResult(V
1721e 64 62 65 20 2a 70 29 7b 0a 20 20 70 2d 3e 72 63  dbe *p){.  p->rc
1721f 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a   = SQLITE_OK;.}.
17220 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20  ./*.** Clean up 
17221 61 20 56 44 42 45 20 61 66 74 65 72 20 65 78 65  a VDBE after exe
17222 63 75 74 69 6f 6e 20 62 75 74 20 64 6f 20 6e 6f  cution but do no
17223 74 20 64 65 6c 65 74 65 20 74 68 65 20 56 44 42  t delete the VDB
17224 45 20 6a 75 73 74 20 79 65 74 2e 0a 2a 2a 20 57  E just yet..** W
17225 72 69 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d  rite any error m
17226 65 73 73 61 67 65 73 20 69 6e 74 6f 20 2a 70 7a  essages into *pz
17227 45 72 72 4d 73 67 2e 20 20 52 65 74 75 72 6e 20  ErrMsg.  Return 
17228 74 68 65 20 72 65 73 75 6c 74 20 63 6f 64 65 2e  the result code.
17229 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 69  .**.** After thi
1722a 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 75 6e  s routine is run
1722b 2c 20 74 68 65 20 56 44 42 45 20 73 68 6f 75 6c  , the VDBE shoul
1722c 64 20 62 65 20 72 65 61 64 79 20 74 6f 20 62 65  d be ready to be
1722d 20 65 78 65 63 75 74 65 64 0a 2a 2a 20 61 67 61   executed.** aga
1722e 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 6c 6f 6f  in..**.** To loo
1722f 6b 20 61 74 20 69 74 20 61 6e 6f 74 68 65 72 20  k at it another 
17230 77 61 79 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  way, this routin
17231 65 20 72 65 73 65 74 73 20 74 68 65 20 73 74 61  e resets the sta
17232 74 65 20 6f 66 20 74 68 65 0a 2a 2a 20 76 69 72  te of the.** vir
17233 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 66 72 6f  tual machine fro
17234 6d 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e  m VDBE_MAGIC_RUN
17235 20 6f 72 20 56 44 42 45 5f 4d 41 47 49 43 5f 48   or VDBE_MAGIC_H
17236 41 4c 54 20 62 61 63 6b 20 74 6f 0a 2a 2a 20 56  ALT back to.** V
17237 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 2e 0a  DBE_MAGIC_INIT..
17238 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
17239 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
1723a 65 52 65 73 65 74 28 56 64 62 65 20 2a 70 29 7b  eReset(Vdbe *p){
1723b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a  .  sqlite3 *db;.
1723c 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20    db = p->db;.. 
1723d 20 2f 2a 20 49 66 20 74 68 65 20 56 4d 20 64 69   /* If the VM di
1723e 64 20 6e 6f 74 20 72 75 6e 20 74 6f 20 63 6f 6d  d not run to com
1723f 70 6c 65 74 69 6f 6e 20 6f 72 20 69 66 20 69 74  pletion or if it
17240 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 61 6e 0a   encountered an.
17241 20 20 2a 2a 20 65 72 72 6f 72 2c 20 74 68 65 6e    ** error, then
17242 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 68 61   it might not ha
17243 76 65 20 62 65 65 6e 20 68 61 6c 74 65 64 20 70  ve been halted p
17244 72 6f 70 65 72 6c 79 2e 20 20 53 6f 20 68 61 6c  roperly.  So hal
17245 74 0a 20 20 2a 2a 20 69 74 20 6e 6f 77 2e 0a 20  t.  ** it now.. 
17246 20 2a 2f 0a 20 20 28 76 6f 69 64 29 73 71 6c 69   */.  (void)sqli
17247 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b  te3SafetyOn(db);
17248 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 48 61  .  sqlite3VdbeHa
17249 6c 74 28 70 29 3b 0a 20 20 28 76 6f 69 64 29 73  lt(p);.  (void)s
1724a 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
1724b 64 62 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  db);..  /* If th
1724c 65 20 56 44 42 45 20 68 61 73 20 62 65 20 72 75  e VDBE has be ru
1724d 6e 20 65 76 65 6e 20 70 61 72 74 69 61 6c 6c 79  n even partially
1724e 2c 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20  , then transfer 
1724f 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 0a 20  the error code. 
17250 20 2a 2a 20 61 6e 64 20 65 72 72 6f 72 20 6d 65   ** and error me
17251 73 73 61 67 65 20 66 72 6f 6d 20 74 68 65 20 56  ssage from the V
17252 44 42 45 20 69 6e 74 6f 20 74 68 65 20 6d 61 69  DBE into the mai
17253 6e 20 64 61 74 61 62 61 73 65 20 73 74 72 75 63  n database struc
17254 74 75 72 65 2e 20 20 42 75 74 0a 20 20 2a 2a 20  ture.  But.  ** 
17255 69 66 20 74 68 65 20 56 44 42 45 20 68 61 73 20  if the VDBE has 
17256 6a 75 73 74 20 62 65 65 6e 20 73 65 74 20 74 6f  just been set to
17257 20 72 75 6e 20 62 75 74 20 68 61 73 20 6e 6f 74   run but has not
17258 20 61 63 74 75 61 6c 6c 79 20 65 78 65 63 75 74   actually execut
17259 65 64 20 61 6e 79 0a 20 20 2a 2a 20 69 6e 73 74  ed any.  ** inst
1725a 72 75 63 74 69 6f 6e 73 20 79 65 74 2c 20 6c 65  ructions yet, le
1725b 61 76 65 20 74 68 65 20 6d 61 69 6e 20 64 61 74  ave the main dat
1725c 61 62 61 73 65 20 65 72 72 6f 72 20 69 6e 66 6f  abase error info
1725d 72 6d 61 74 69 6f 6e 20 75 6e 63 68 61 6e 67 65  rmation unchange
1725e 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d  d..  */.  if( p-
1725f 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 69 66  >pc>=0 ){.    if
17260 28 20 70 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0a  ( p->zErrMsg ){.
17261 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67        sqlite3Beg
17262 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  inBenignMalloc()
17263 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
17264 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70  alueSetStr(db->p
17265 45 72 72 2c 2d 31 2c 70 2d 3e 7a 45 72 72 4d 73  Err,-1,p->zErrMs
17266 67 2c 53 51 4c 49 54 45 5f 55 54 46 38 2c 53 51  g,SQLITE_UTF8,SQ
17267 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
17268 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e  .      sqlite3En
17269 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  dBenignMalloc();
1726a 0a 20 20 20 20 20 20 64 62 2d 3e 65 72 72 43 6f  .      db->errCo
1726b 64 65 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 20 20  de = p->rc;.    
1726c 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1726d 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b  db, p->zErrMsg);
1726e 0a 20 20 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73  .      p->zErrMs
1726f 67 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65  g = 0;.    }else
17270 20 69 66 28 20 70 2d 3e 72 63 20 29 7b 0a 20 20   if( p->rc ){.  
17271 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
17272 28 64 62 2c 20 70 2d 3e 72 63 2c 20 30 29 3b 0a  (db, p->rc, 0);.
17273 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
17274 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
17275 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b  , SQLITE_OK, 0);
17276 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
17277 66 28 20 70 2d 3e 72 63 20 26 26 20 70 2d 3e 65  f( p->rc && p->e
17278 78 70 69 72 65 64 20 29 7b 0a 20 20 20 20 2f 2a  xpired ){.    /*
17279 20 54 68 65 20 65 78 70 69 72 65 64 20 66 6c 61   The expired fla
1727a 67 20 77 61 73 20 73 65 74 20 6f 6e 20 74 68 65  g was set on the
1727b 20 56 44 42 45 20 62 65 66 6f 72 65 20 74 68 65   VDBE before the
1727c 20 66 69 72 73 74 20 63 61 6c 6c 0a 20 20 20 20   first call.    
1727d 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 74  ** to sqlite3_st
1727e 65 70 28 29 2e 20 46 6f 72 20 63 6f 6e 73 69 73  ep(). For consis
1727f 74 65 6e 63 79 20 28 73 69 6e 63 65 20 73 71 6c  tency (since sql
17280 69 74 65 33 5f 73 74 65 70 28 29 20 77 61 73 0a  ite3_step() was.
17281 20 20 20 20 2a 2a 20 63 61 6c 6c 65 64 29 2c 20      ** called), 
17282 73 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65  set the database
17283 20 65 72 72 6f 72 20 69 6e 20 74 68 69 73 20 63   error in this c
17284 61 73 65 20 61 73 20 77 65 6c 6c 2e 0a 20 20 20  ase as well..   
17285 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45   */.    sqlite3E
17286 72 72 6f 72 28 64 62 2c 20 70 2d 3e 72 63 2c 20  rror(db, p->rc, 
17287 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
17288 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70  alueSetStr(db->p
17289 45 72 72 2c 20 2d 31 2c 20 70 2d 3e 7a 45 72 72  Err, -1, p->zErr
1728a 4d 73 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  Msg, SQLITE_UTF8
1728b 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
1728c 4e 54 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  NT);.    sqlite3
1728d 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45  DbFree(db, p->zE
1728e 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a  rrMsg);.    p->z
1728f 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a  ErrMsg = 0;.  }.
17290 0a 20 20 2f 2a 20 52 65 63 6c 61 69 6d 20 61 6c  .  /* Reclaim al
17291 6c 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79  l memory used by
17292 20 74 68 65 20 56 44 42 45 0a 20 20 2a 2f 0a 20   the VDBE.  */. 
17293 20 43 6c 65 61 6e 75 70 28 70 29 3b 0a 0a 20 20   Cleanup(p);..  
17294 2f 2a 20 53 61 76 65 20 70 72 6f 66 69 6c 69 6e  /* Save profilin
17295 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72  g information fr
17296 6f 6d 20 74 68 69 73 20 56 44 42 45 20 72 75 6e  om this VDBE run
17297 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 56 44  ..  */.#ifdef VD
17298 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 7b 0a 20  BE_PROFILE.  {. 
17299 20 20 20 46 49 4c 45 20 2a 6f 75 74 20 3d 20 66     FILE *out = f
1729a 6f 70 65 6e 28 22 76 64 62 65 5f 70 72 6f 66 69  open("vdbe_profi
1729b 6c 65 2e 6f 75 74 22 2c 20 22 61 22 29 3b 0a 20  le.out", "a");. 
1729c 20 20 20 69 66 28 20 6f 75 74 20 29 7b 0a 20 20     if( out ){.  
1729d 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20      int i;.     
1729e 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 2d   fprintf(out, "-
1729f 2d 2d 2d 20 22 29 3b 0a 20 20 20 20 20 20 66 6f  --- ");.      fo
172a0 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b  r(i=0; i<p->nOp;
172a1 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 66   i++){.        f
172a2 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 30 32  printf(out, "%02
172a3 78 22 2c 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70  x", p->aOp[i].op
172a4 63 6f 64 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  code);.      }. 
172a5 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74       fprintf(out
172a6 2c 20 22 5c 6e 22 29 3b 0a 20 20 20 20 20 20 66  , "\n");.      f
172a7 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70  or(i=0; i<p->nOp
172a8 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
172a9 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 36  fprintf(out, "%6
172aa 64 20 25 31 30 6c 6c 64 20 25 38 6c 6c 64 20 22  d %10lld %8lld "
172ab 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e  ,.           p->
172ac 61 4f 70 5b 69 5d 2e 63 6e 74 2c 0a 20 20 20 20  aOp[i].cnt,.    
172ad 20 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d         p->aOp[i]
172ae 2e 63 79 63 6c 65 73 2c 0a 20 20 20 20 20 20 20  .cycles,.       
172af 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e      p->aOp[i].cn
172b0 74 3e 30 20 3f 20 70 2d 3e 61 4f 70 5b 69 5d 2e  t>0 ? p->aOp[i].
172b1 63 79 63 6c 65 73 2f 70 2d 3e 61 4f 70 5b 69 5d  cycles/p->aOp[i]
172b2 2e 63 6e 74 20 3a 20 30 0a 20 20 20 20 20 20 20  .cnt : 0.       
172b3 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   );.        sqli
172b4 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 6f  te3VdbePrintOp(o
172b5 75 74 2c 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69  ut, i, &p->aOp[i
172b6 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ]);.      }.    
172b7 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a 20    fclose(out);. 
172b8 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
172b9 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42    p->magic = VDB
172ba 45 5f 4d 41 47 49 43 5f 49 4e 49 54 3b 0a 20 20  E_MAGIC_INIT;.  
172bb 72 65 74 75 72 6e 20 70 2d 3e 72 63 20 26 20 64  return p->rc & d
172bc 62 2d 3e 65 72 72 4d 61 73 6b 3b 0a 7d 0a 20 0a  b->errMask;.}. .
172bd 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 61  /*.** Clean up a
172be 6e 64 20 64 65 6c 65 74 65 20 61 20 56 44 42 45  nd delete a VDBE
172bf 20 61 66 74 65 72 20 65 78 65 63 75 74 69 6f 6e   after execution
172c0 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 69 6e 74  .  Return an int
172c1 65 67 65 72 20 77 68 69 63 68 20 69 73 0a 2a 2a  eger which is.**
172c2 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 64 65   the result code
172c3 2e 20 20 57 72 69 74 65 20 61 6e 79 20 65 72 72  .  Write any err
172c4 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 20  or message text 
172c5 69 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 0a  into *pzErrMsg..
172c6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
172c7 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
172c8 65 46 69 6e 61 6c 69 7a 65 28 56 64 62 65 20 2a  eFinalize(Vdbe *
172c9 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  p){.  int rc = S
172ca 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
172cb 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d  p->magic==VDBE_M
172cc 41 47 49 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e 6d  AGIC_RUN || p->m
172cd 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43  agic==VDBE_MAGIC
172ce 5f 48 41 4c 54 20 29 7b 0a 20 20 20 20 72 63 20  _HALT ){.    rc 
172cf 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73  = sqlite3VdbeRes
172d0 65 74 28 70 29 3b 0a 20 20 20 20 61 73 73 65 72  et(p);.    asser
172d1 74 28 20 28 72 63 20 26 20 70 2d 3e 64 62 2d 3e  t( (rc & p->db->
172d2 65 72 72 4d 61 73 6b 29 3d 3d 72 63 20 29 3b 0a  errMask)==rc );.
172d3 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6d    }else if( p->m
172d4 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43  agic!=VDBE_MAGIC
172d5 5f 49 4e 49 54 20 29 7b 0a 20 20 20 20 72 65 74  _INIT ){.    ret
172d6 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  urn SQLITE_MISUS
172d7 45 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  E;.  }.  sqlite3
172d8 56 64 62 65 44 65 6c 65 74 65 28 70 29 3b 0a 20  VdbeDelete(p);. 
172d9 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
172da 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68 65 20 64 65  *.** Call the de
172db 73 74 72 75 63 74 6f 72 20 66 6f 72 20 65 61 63  structor for eac
172dc 68 20 61 75 78 64 61 74 61 20 65 6e 74 72 79 20  h auxdata entry 
172dd 69 6e 20 70 56 64 62 65 46 75 6e 63 20 66 6f 72  in pVdbeFunc for
172de 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 63 6f   which.** the co
172df 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 20  rresponding bit 
172e0 69 6e 20 6d 61 73 6b 20 69 73 20 63 6c 65 61 72  in mask is clear
172e1 2e 20 20 41 75 78 64 61 74 61 20 65 6e 74 72 69  .  Auxdata entri
172e2 65 73 20 62 65 79 6f 6e 64 20 33 31 0a 2a 2a 20  es beyond 31.** 
172e3 61 72 65 20 61 6c 77 61 79 73 20 64 65 73 74 72  are always destr
172e4 6f 79 65 64 2e 20 20 54 6f 20 64 65 73 74 72 6f  oyed.  To destro
172e5 79 20 61 6c 6c 20 61 75 78 64 61 74 61 20 65 6e  y all auxdata en
172e6 74 72 69 65 73 2c 20 63 61 6c 6c 20 74 68 69 73  tries, call this
172e7 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77 69 74 68  .** routine with
172e8 20 6d 61 73 6b 3d 3d 30 2e 0a 2a 2f 0a 53 51 4c   mask==0..*/.SQL
172e9 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
172ea 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65   sqlite3VdbeDele
172eb 74 65 41 75 78 44 61 74 61 28 56 64 62 65 46 75  teAuxData(VdbeFu
172ec 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 2c 20 69  nc *pVdbeFunc, i
172ed 6e 74 20 6d 61 73 6b 29 7b 0a 20 20 69 6e 74 20  nt mask){.  int 
172ee 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  i;.  for(i=0; i<
172ef 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78 3b  pVdbeFunc->nAux;
172f0 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63   i++){.    struc
172f1 74 20 41 75 78 44 61 74 61 20 2a 70 41 75 78 20  t AuxData *pAux 
172f2 3d 20 26 70 56 64 62 65 46 75 6e 63 2d 3e 61 70  = &pVdbeFunc->ap
172f3 41 75 78 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20  Aux[i];.    if( 
172f4 28 69 3e 33 31 20 7c 7c 20 21 28 6d 61 73 6b 26  (i>31 || !(mask&
172f5 28 31 3c 3c 69 29 29 29 20 26 26 20 70 41 75 78  (1<<i))) && pAux
172f6 2d 3e 70 41 75 78 20 29 7b 0a 20 20 20 20 20 20  ->pAux ){.      
172f7 69 66 28 20 70 41 75 78 2d 3e 78 44 65 6c 65 74  if( pAux->xDelet
172f8 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 41 75  e ){.        pAu
172f9 78 2d 3e 78 44 65 6c 65 74 65 28 70 41 75 78 2d  x->xDelete(pAux-
172fa 3e 70 41 75 78 29 3b 0a 20 20 20 20 20 20 7d 0a  >pAux);.      }.
172fb 20 20 20 20 20 20 70 41 75 78 2d 3e 70 41 75 78        pAux->pAux
172fc 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
172fd 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  }../*.** Delete 
172fe 61 6e 20 65 6e 74 69 72 65 20 56 44 42 45 2e 0a  an entire VDBE..
172ff 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
17300 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
17301 62 65 44 65 6c 65 74 65 28 56 64 62 65 20 2a 70  beDelete(Vdbe *p
17302 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 71  ){.  int i;.  sq
17303 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66  lite3 *db;..  if
17304 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( p==0 ) return;
17305 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20  .  db = p->db;. 
17306 20 69 66 28 20 70 2d 3e 70 50 72 65 76 20 29 7b   if( p->pPrev ){
17307 0a 20 20 20 20 70 2d 3e 70 50 72 65 76 2d 3e 70  .    p->pPrev->p
17308 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b  Next = p->pNext;
17309 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
1730a 73 65 72 74 28 20 64 62 2d 3e 70 56 64 62 65 3d  sert( db->pVdbe=
1730b 3d 70 20 29 3b 0a 20 20 20 20 64 62 2d 3e 70 56  =p );.    db->pV
1730c 64 62 65 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a  dbe = p->pNext;.
1730d 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4e 65    }.  if( p->pNe
1730e 78 74 20 29 7b 0a 20 20 20 20 70 2d 3e 70 4e 65  xt ){.    p->pNe
1730f 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 2d 3e 70  xt->pPrev = p->p
17310 50 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Prev;.  }.  if( 
17311 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20 4f 70  p->aOp ){.    Op
17312 20 2a 70 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b 0a   *pOp = p->aOp;.
17313 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
17314 2d 3e 6e 4f 70 3b 20 69 2b 2b 2c 20 70 4f 70 2b  ->nOp; i++, pOp+
17315 2b 29 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34  +){.      freeP4
17316 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65  (db, pOp->p4type
17317 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 23 69  , pOp->p4.p);.#i
17318 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
17319 47 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  G.      sqlite3D
1731a 62 46 72 65 65 28 64 62 2c 20 70 4f 70 2d 3e 7a  bFree(db, pOp->z
1731b 43 6f 6d 6d 65 6e 74 29 3b 0a 23 65 6e 64 69 66  Comment);.#endif
1731c 20 20 20 20 20 0a 20 20 20 20 7d 0a 20 20 20 20       .    }.    
1731d 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1731e 2c 20 70 2d 3e 61 4f 70 29 3b 0a 20 20 7d 0a 20  , p->aOp);.  }. 
1731f 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79   releaseMemArray
17320 28 70 2d 3e 61 56 61 72 2c 20 70 2d 3e 6e 56 61  (p->aVar, p->nVa
17321 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  r);.  sqlite3DbF
17322 72 65 65 28 64 62 2c 20 70 2d 3e 61 4c 61 62 65  ree(db, p->aLabe
17323 6c 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 4d 65  l);.  if( p->aMe
17324 6d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  m ){.    sqlite3
17325 44 62 46 72 65 65 28 64 62 2c 20 26 70 2d 3e 61  DbFree(db, &p->a
17326 4d 65 6d 5b 31 5d 29 3b 0a 20 20 7d 0a 20 20 72  Mem[1]);.  }.  r
17327 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70  eleaseMemArray(p
17328 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e  ->aColName, p->n
17329 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d  ResColumn*COLNAM
1732a 45 5f 4e 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  E_N);.  sqlite3D
1732b 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 43 6f  bFree(db, p->aCo
1732c 6c 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65  lName);.  sqlite
1732d 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a  3DbFree(db, p->z
1732e 53 71 6c 29 3b 0a 20 20 70 2d 3e 6d 61 67 69 63  Sql);.  p->magic
1732f 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 44 45   = VDBE_MAGIC_DE
17330 41 44 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  AD;.  sqlite3DbF
17331 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f  ree(db, p);.}../
17332 2a 0a 2a 2a 20 49 66 20 61 20 4d 6f 76 65 54 6f  *.** If a MoveTo
17333 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 70 65   operation is pe
17334 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 67 69 76  nding on the giv
17335 65 6e 20 63 75 72 73 6f 72 2c 20 74 68 65 6e 20  en cursor, then 
17336 64 6f 20 74 68 61 74 0a 2a 2a 20 4d 6f 76 65 54  do that.** MoveT
17337 6f 20 6e 6f 77 2e 20 20 52 65 74 75 72 6e 20 61  o now.  Return a
17338 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 49  n error code.  I
17339 66 20 6e 6f 20 4d 6f 76 65 54 6f 20 69 73 20 70  f no MoveTo is p
1733a 65 6e 64 69 6e 67 2c 20 74 68 69 73 0a 2a 2a 20  ending, this.** 
1733b 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74  routine does not
1733c 68 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73  hing and returns
1733d 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53   SQLITE_OK..*/.S
1733e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1733f 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  t sqlite3VdbeCur
17340 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62 65 43 75  sorMoveto(VdbeCu
17341 72 73 6f 72 20 2a 70 29 7b 0a 20 20 69 66 28 20  rsor *p){.  if( 
17342 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  p->deferredMovet
17343 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73  o ){.    int res
17344 2c 20 72 63 3b 0a 23 69 66 64 65 66 20 53 51 4c  , rc;.#ifdef SQL
17345 49 54 45 5f 54 45 53 54 0a 20 20 20 20 65 78 74  ITE_TEST.    ext
17346 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  ern int sqlite3_
17347 73 65 61 72 63 68 5f 63 6f 75 6e 74 3b 0a 23 65  search_count;.#e
17348 6e 64 69 66 0a 20 20 20 20 61 73 73 65 72 74 28  ndif.    assert(
17349 20 70 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20   p->isTable );. 
1734a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1734b 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b  treeMovetoUnpack
1734c 65 64 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 30  ed(p->pCursor, 0
1734d 2c 20 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65  , p->movetoTarge
1734e 74 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20 20 20  t, 0, &res);.   
1734f 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
17350 20 72 63 3b 0a 20 20 20 20 70 2d 3e 6c 61 73 74   rc;.    p->last
17351 52 6f 77 69 64 20 3d 20 6b 65 79 54 6f 49 6e 74  Rowid = keyToInt
17352 28 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65 74  (p->movetoTarget
17353 29 3b 0a 20 20 20 20 70 2d 3e 72 6f 77 69 64 49  );.    p->rowidI
17354 73 56 61 6c 69 64 20 3d 20 72 65 73 3d 3d 30 3b  sValid = res==0;
17355 0a 20 20 20 20 69 66 28 20 72 65 73 3c 30 20 29  .    if( res<0 )
17356 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
17357 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 70 2d  ite3BtreeNext(p-
17358 3e 70 43 75 72 73 6f 72 2c 20 26 72 65 73 29 3b  >pCursor, &res);
17359 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20  .      if( rc ) 
1735a 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
1735b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
1735c 45 53 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  EST.    sqlite3_
1735d 73 65 61 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a  search_count++;.
1735e 23 65 6e 64 69 66 0a 20 20 20 20 70 2d 3e 64 65  #endif.    p->de
1735f 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30  ferredMoveto = 0
17360 3b 0a 20 20 20 20 70 2d 3e 63 61 63 68 65 53 74  ;.    p->cacheSt
17361 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41  atus = CACHE_STA
17362 4c 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  LE;.  }else if( 
17363 70 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20  p->pCursor ){.  
17364 20 20 69 6e 74 20 68 61 73 4d 6f 76 65 64 3b 0a    int hasMoved;.
17365 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c      int rc = sql
17366 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 48  ite3BtreeCursorH
17367 61 73 4d 6f 76 65 64 28 70 2d 3e 70 43 75 72 73  asMoved(p->pCurs
17368 6f 72 2c 20 26 68 61 73 4d 6f 76 65 64 29 3b 0a  or, &hasMoved);.
17369 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
1736a 75 72 6e 20 72 63 3b 0a 20 20 20 20 69 66 28 20  urn rc;.    if( 
1736b 68 61 73 4d 6f 76 65 64 20 29 7b 0a 20 20 20 20  hasMoved ){.    
1736c 20 20 70 2d 3e 63 61 63 68 65 53 74 61 74 75 73    p->cacheStatus
1736d 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a   = CACHE_STALE;.
1736e 20 20 20 20 20 20 70 2d 3e 6e 75 6c 6c 52 6f 77        p->nullRow
1736f 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 1;.    }.  }.
17370 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
17371 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  OK;.}../*.** The
17372 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74   following funct
17373 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69  ions:.**.** sqli
17374 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
17375 65 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64  e().** sqlite3Vd
17376 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28  beSerialTypeLen(
17377 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65  ).** sqlite3Vdbe
17378 53 65 72 69 61 6c 4c 65 6e 28 29 0a 2a 2a 20 73  SerialLen().** s
17379 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
1737a 50 75 74 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33  Put().** sqlite3
1737b 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 29 0a  VdbeSerialGet().
1737c 2a 2a 0a 2a 2a 20 65 6e 63 61 70 73 75 6c 61 74  **.** encapsulat
1737d 65 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20  e the code that 
1737e 73 65 72 69 61 6c 69 7a 65 73 20 76 61 6c 75 65  serializes value
1737f 73 20 66 6f 72 20 73 74 6f 72 61 67 65 20 69 6e  s for storage in
17380 20 53 51 4c 69 74 65 0a 2a 2a 20 64 61 74 61 20   SQLite.** data 
17381 61 6e 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64  and index record
17382 73 2e 20 45 61 63 68 20 73 65 72 69 61 6c 69 7a  s. Each serializ
17383 65 64 20 76 61 6c 75 65 20 63 6f 6e 73 69 73 74  ed value consist
17384 73 20 6f 66 20 61 0a 2a 2a 20 27 73 65 72 69 61  s of a.** 'seria
17385 6c 2d 74 79 70 65 27 20 61 6e 64 20 61 20 62 6c  l-type' and a bl
17386 6f 62 20 6f 66 20 64 61 74 61 2e 20 54 68 65 20  ob of data. The 
17387 73 65 72 69 61 6c 20 74 79 70 65 20 69 73 20 61  serial type is a
17388 6e 20 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65  n 8-byte unsigne
17389 64 0a 2a 2a 20 69 6e 74 65 67 65 72 2c 20 73 74  d.** integer, st
1738a 6f 72 65 64 20 61 73 20 61 20 76 61 72 69 6e 74  ored as a varint
1738b 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 20 53 51  ..**.** In an SQ
1738c 4c 69 74 65 20 69 6e 64 65 78 20 72 65 63 6f 72  Lite index recor
1738d 64 2c 20 74 68 65 20 73 65 72 69 61 6c 20 74 79  d, the serial ty
1738e 70 65 20 69 73 20 73 74 6f 72 65 64 20 64 69 72  pe is stored dir
1738f 65 63 74 6c 79 20 62 65 66 6f 72 65 0a 2a 2a 20  ectly before.** 
17390 74 68 65 20 62 6c 6f 62 20 6f 66 20 64 61 74 61  the blob of data
17391 20 74 68 61 74 20 69 74 20 63 6f 72 72 65 73 70   that it corresp
17392 6f 6e 64 73 20 74 6f 2e 20 49 6e 20 61 20 74 61  onds to. In a ta
17393 62 6c 65 20 72 65 63 6f 72 64 2c 20 61 6c 6c 20  ble record, all 
17394 73 65 72 69 61 6c 0a 2a 2a 20 74 79 70 65 73 20  serial.** types 
17395 61 72 65 20 73 74 6f 72 65 64 20 61 74 20 74 68  are stored at th
17396 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 72  e start of the r
17397 65 63 6f 72 64 2c 20 61 6e 64 20 74 68 65 20 62  ecord, and the b
17398 6c 6f 62 73 20 6f 66 20 64 61 74 61 20 61 74 0a  lobs of data at.
17399 2a 2a 20 74 68 65 20 65 6e 64 2e 20 48 65 6e 63  ** the end. Henc
1739a 65 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e  e these function
1739b 73 20 61 6c 6c 6f 77 20 74 68 65 20 63 61 6c 6c  s allow the call
1739c 65 72 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65  er to handle the
1739d 0a 2a 2a 20 73 65 72 69 61 6c 2d 74 79 70 65 20  .** serial-type 
1739e 61 6e 64 20 64 61 74 61 20 62 6c 6f 62 20 73 65  and data blob se
1739f 70 65 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20  perately..**.** 
173a0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61  The following ta
173a1 62 6c 65 20 64 65 73 63 72 69 62 65 73 20 74 68  ble describes th
173a2 65 20 76 61 72 69 6f 75 73 20 73 74 6f 72 61 67  e various storag
173a3 65 20 63 6c 61 73 73 65 73 20 66 6f 72 20 64 61  e classes for da
173a4 74 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 73 65 72 69  ta:.**.**   seri
173a5 61 6c 20 74 79 70 65 20 20 20 20 20 20 20 20 62  al type        b
173a6 79 74 65 73 20 6f 66 20 64 61 74 61 20 20 20 20  ytes of data    
173a7 20 20 74 79 70 65 0a 2a 2a 20 20 20 2d 2d 2d 2d    type.**   ----
173a8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d  ----------     -
173a9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20  --------------  
173aa 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
173ab 2d 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20  -.**      0     
173ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
173ad 30 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c  0            NUL
173ae 4c 0a 2a 2a 20 20 20 20 20 20 31 20 20 20 20 20  L.**      1     
173af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
173b0 31 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67  1            sig
173b1 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20  ned integer.**  
173b2 20 20 20 20 32 20 20 20 20 20 20 20 20 20 20 20      2           
173b3 20 20 20 20 20 20 20 20 20 20 32 20 20 20 20 20            2     
173b4 20 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e         signed in
173b5 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 33 20  teger.**      3 
173b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
173b7 20 20 20 20 33 20 20 20 20 20 20 20 20 20 20 20      3           
173b8 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a   signed integer.
173b9 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 20 20  **      4       
173ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 20                4 
173bb 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65             signe
173bc 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20  d integer.**    
173bd 20 20 35 20 20 20 20 20 20 20 20 20 20 20 20 20    5             
173be 20 20 20 20 20 20 20 20 36 20 20 20 20 20 20 20          6       
173bf 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65       signed inte
173c0 67 65 72 0a 2a 2a 20 20 20 20 20 20 36 20 20 20  ger.**      6   
173c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
173c2 20 20 38 20 20 20 20 20 20 20 20 20 20 20 20 73    8            s
173c3 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a  igned integer.**
173c4 20 20 20 20 20 20 37 20 20 20 20 20 20 20 20 20        7         
173c5 20 20 20 20 20 20 20 20 20 20 20 20 38 20 20 20              8   
173c6 20 20 20 20 20 20 20 20 20 49 45 45 45 20 66 6c           IEEE fl
173c7 6f 61 74 0a 2a 2a 20 20 20 20 20 20 38 20 20 20  oat.**      8   
173c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
173c9 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 49    0            I
173ca 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20  nteger constant 
173cb 30 0a 2a 2a 20 20 20 20 20 20 39 20 20 20 20 20  0.**      9     
173cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
173cd 30 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 74  0            Int
173ce 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 31 0a  eger constant 1.
173cf 2a 2a 20 20 20 20 20 31 30 2c 31 31 20 20 20 20  **     10,11    
173d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
173d1 20 20 20 20 20 20 20 20 20 20 20 72 65 73 65 72             reser
173d2 76 65 64 20 66 6f 72 20 65 78 70 61 6e 73 69 6f  ved for expansio
173d3 6e 0a 2a 2a 20 20 20 20 4e 3e 3d 31 32 20 61 6e  n.**    N>=12 an
173d4 64 20 65 76 65 6e 20 20 20 20 20 20 20 28 4e 2d  d even       (N-
173d5 31 32 29 2f 32 20 20 20 20 20 20 20 20 42 4c 4f  12)/2        BLO
173d6 42 0a 2a 2a 20 20 20 20 4e 3e 3d 31 33 20 61 6e  B.**    N>=13 an
173d7 64 20 6f 64 64 20 20 20 20 20 20 20 20 28 4e 2d  d odd        (N-
173d8 31 33 29 2f 32 20 20 20 20 20 20 20 20 74 65 78  13)/2        tex
173d9 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 38 20 61 6e  t.**.** The 8 an
173da 64 20 39 20 74 79 70 65 73 20 77 65 72 65 20 61  d 9 types were a
173db 64 64 65 64 20 69 6e 20 33 2e 33 2e 30 2c 20 66  dded in 3.3.0, f
173dc 69 6c 65 20 66 6f 72 6d 61 74 20 34 2e 20 20 50  ile format 4.  P
173dd 72 69 6f 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a  rior versions.**
173de 20 6f 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20   of SQLite will 
173df 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 20 74  not understand t
173e0 68 6f 73 65 20 73 65 72 69 61 6c 20 74 79 70 65  hose serial type
173e1 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  s..*/../*.** Ret
173e2 75 72 6e 20 74 68 65 20 73 65 72 69 61 6c 2d 74  urn the serial-t
173e3 79 70 65 20 66 6f 72 20 74 68 65 20 76 61 6c 75  ype for the valu
173e4 65 20 73 74 6f 72 65 64 20 69 6e 20 70 4d 65 6d  e stored in pMem
173e5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
173e6 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 56  ATE u32 sqlite3V
173e7 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 4d 65  dbeSerialType(Me
173e8 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 66 69 6c  m *pMem, int fil
173e9 65 5f 66 6f 72 6d 61 74 29 7b 0a 20 20 69 6e 74  e_format){.  int
173ea 20 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66   flags = pMem->f
173eb 6c 61 67 73 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a  lags;.  int n;..
173ec 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f    if( flags&MEM_
173ed 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75  Null ){.    retu
173ee 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rn 0;.  }.  if( 
173ef 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 7b  flags&MEM_Int ){
173f0 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f  .    /* Figure o
173f1 75 74 20 77 68 65 74 68 65 72 20 74 6f 20 75 73  ut whether to us
173f2 65 20 31 2c 20 32 2c 20 34 2c 20 36 20 6f 72 20  e 1, 2, 4, 6 or 
173f3 38 20 62 79 74 65 73 2e 20 2a 2f 0a 23 20 20 20  8 bytes. */.#   
173f4 64 65 66 69 6e 65 20 4d 41 58 5f 36 42 59 54 45  define MAX_6BYTE
173f5 20 28 28 28 28 69 36 34 29 30 78 30 30 30 30 38   ((((i64)0x00008
173f6 30 30 30 29 3c 3c 33 32 29 2d 31 29 0a 20 20 20  000)<<32)-1).   
173f7 20 69 36 34 20 69 20 3d 20 70 4d 65 6d 2d 3e 75   i64 i = pMem->u
173f8 2e 69 3b 0a 20 20 20 20 75 36 34 20 75 3b 0a 20  .i;.    u64 u;. 
173f9 20 20 20 69 66 28 20 66 69 6c 65 5f 66 6f 72 6d     if( file_form
173fa 61 74 3e 3d 34 20 26 26 20 28 69 26 31 29 3d 3d  at>=4 && (i&1)==
173fb 69 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  i ){.      retur
173fc 6e 20 38 2b 69 3b 0a 20 20 20 20 7d 0a 20 20 20  n 8+i;.    }.   
173fd 20 75 20 3d 20 69 3c 30 20 3f 20 2d 69 20 3a 20   u = i<0 ? -i : 
173fe 69 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 31 32  i;.    if( u<=12
173ff 37 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  7 ) return 1;.  
17400 20 20 69 66 28 20 75 3c 3d 33 32 37 36 37 20 29    if( u<=32767 )
17401 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 69   return 2;.    i
17402 66 28 20 75 3c 3d 38 33 38 38 36 30 37 20 29 20  f( u<=8388607 ) 
17403 72 65 74 75 72 6e 20 33 3b 0a 20 20 20 20 69 66  return 3;.    if
17404 28 20 75 3c 3d 32 31 34 37 34 38 33 36 34 37 20  ( u<=2147483647 
17405 29 20 72 65 74 75 72 6e 20 34 3b 0a 20 20 20 20  ) return 4;.    
17406 69 66 28 20 75 3c 3d 4d 41 58 5f 36 42 59 54 45  if( u<=MAX_6BYTE
17407 20 29 20 72 65 74 75 72 6e 20 35 3b 0a 20 20 20   ) return 5;.   
17408 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 7d 0a 20   return 6;.  }. 
17409 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 52   if( flags&MEM_R
1740a 65 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72  eal ){.    retur
1740b 6e 20 37 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  n 7;.  }.  asser
1740c 74 28 20 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 61 6c  t( pMem->db->mal
1740d 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 66 6c 61  locFailed || fla
1740e 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f  gs&(MEM_Str|MEM_
1740f 42 6c 6f 62 29 20 29 3b 0a 20 20 6e 20 3d 20 70  Blob) );.  n = p
17410 4d 65 6d 2d 3e 6e 3b 0a 20 20 69 66 28 20 66 6c  Mem->n;.  if( fl
17411 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29  ags & MEM_Zero )
17412 7b 0a 20 20 20 20 6e 20 2b 3d 20 70 4d 65 6d 2d  {.    n += pMem-
17413 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 61 73 73 65  >u.i;.  }.  asse
17414 72 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 72 65  rt( n>=0 );.  re
17415 74 75 72 6e 20 28 28 6e 2a 32 29 20 2b 20 31 32  turn ((n*2) + 12
17416 20 2b 20 28 28 66 6c 61 67 73 26 4d 45 4d 5f 53   + ((flags&MEM_S
17417 74 72 29 21 3d 30 29 29 3b 0a 7d 0a 0a 2f 2a 0a  tr)!=0));.}../*.
17418 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6c 65  ** Return the le
17419 6e 67 74 68 20 6f 66 20 74 68 65 20 64 61 74 61  ngth of the data
1741a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
1741b 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 73  o the supplied s
1741c 65 72 69 61 6c 2d 74 79 70 65 2e 0a 2a 2f 0a 53  erial-type..*/.S
1741d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1741e 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  t sqlite3VdbeSer
1741f 69 61 6c 54 79 70 65 4c 65 6e 28 75 33 32 20 73  ialTypeLen(u32 s
17420 65 72 69 61 6c 5f 74 79 70 65 29 7b 0a 20 20 69  erial_type){.  i
17421 66 28 20 73 65 72 69 61 6c 5f 74 79 70 65 3e 3d  f( serial_type>=
17422 31 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  12 ){.    return
17423 20 28 73 65 72 69 61 6c 5f 74 79 70 65 2d 31 32   (serial_type-12
17424 29 2f 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  )/2;.  }else{.  
17425 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
17426 38 20 61 53 69 7a 65 5b 5d 20 3d 20 7b 20 30 2c  8 aSize[] = { 0,
17427 20 31 2c 20 32 2c 20 33 2c 20 34 2c 20 36 2c 20   1, 2, 3, 4, 6, 
17428 38 2c 20 38 2c 20 30 2c 20 30 2c 20 30 2c 20 30  8, 8, 0, 0, 0, 0
17429 20 7d 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 61   };.    return a
1742a 53 69 7a 65 5b 73 65 72 69 61 6c 5f 74 79 70 65  Size[serial_type
1742b 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ];.  }.}../*.** 
1742c 49 66 20 77 65 20 61 72 65 20 6f 6e 20 61 6e 20  If we are on an 
1742d 61 72 63 68 69 74 65 63 74 75 72 65 20 77 69 74  architecture wit
1742e 68 20 6d 69 78 65 64 2d 65 6e 64 69 61 6e 20 66  h mixed-endian f
1742f 6c 6f 61 74 69 6e 67 20 0a 2a 2a 20 70 6f 69 6e  loating .** poin
17430 74 73 20 28 65 78 3a 20 41 52 4d 37 29 20 74 68  ts (ex: ARM7) th
17431 65 6e 20 73 77 61 70 20 74 68 65 20 6c 6f 77 65  en swap the lowe
17432 72 20 34 20 62 79 74 65 73 20 77 69 74 68 20 74  r 4 bytes with t
17433 68 65 20 0a 2a 2a 20 75 70 70 65 72 20 34 20 62  he .** upper 4 b
17434 79 74 65 73 2e 20 20 52 65 74 75 72 6e 20 74 68  ytes.  Return th
17435 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20  e result..**.** 
17436 46 6f 72 20 6d 6f 73 74 20 61 72 63 68 69 74 65  For most archite
17437 63 74 75 72 65 73 2c 20 74 68 69 73 20 69 73 20  ctures, this is 
17438 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 28  a no-op..**.** (
17439 6c 61 74 65 72 29 3a 20 20 49 74 20 69 73 20 72  later):  It is r
1743a 65 70 6f 72 74 65 64 20 74 6f 20 6d 65 20 74 68  eported to me th
1743b 61 74 20 74 68 65 20 6d 69 78 65 64 2d 65 6e 64  at the mixed-end
1743c 69 61 6e 20 70 72 6f 62 6c 65 6d 0a 2a 2a 20 6f  ian problem.** o
1743d 6e 20 41 52 4d 37 20 69 73 20 61 6e 20 69 73 73  n ARM7 is an iss
1743e 75 65 20 77 69 74 68 20 47 43 43 2c 20 6e 6f 74  ue with GCC, not
1743f 20 77 69 74 68 20 74 68 65 20 41 52 4d 37 20 63   with the ARM7 c
17440 68 69 70 2e 20 20 49 74 20 73 65 65 6d 73 0a 2a  hip.  It seems.*
17441 2a 20 74 68 61 74 20 65 61 72 6c 79 20 76 65 72  * that early ver
17442 73 69 6f 6e 73 20 6f 66 20 47 43 43 20 73 74 6f  sions of GCC sto
17443 72 65 64 20 74 68 65 20 74 77 6f 20 77 6f 72 64  red the two word
17444 73 20 6f 66 20 61 20 36 34 2d 62 69 74 0a 2a 2a  s of a 64-bit.**
17445 20 66 6c 6f 61 74 20 69 6e 20 74 68 65 20 77 72   float in the wr
17446 6f 6e 67 20 6f 72 64 65 72 2e 20 20 41 6e 64 20  ong order.  And 
17447 74 68 61 74 20 65 72 72 6f 72 20 68 61 73 20 62  that error has b
17448 65 65 6e 20 70 72 6f 70 61 67 61 74 65 64 0a 2a  een propagated.*
17449 2a 20 65 76 65 72 20 73 69 6e 63 65 2e 20 20 54  * ever since.  T
1744a 68 65 20 62 6c 61 6d 65 20 69 73 20 6e 6f 74 20  he blame is not 
1744b 6e 65 63 65 73 73 61 72 69 6c 79 20 77 69 74 68  necessarily with
1744c 20 47 43 43 2c 20 74 68 6f 75 67 68 2e 0a 2a 2a   GCC, though..**
1744d 20 47 43 43 20 6d 69 67 68 74 20 68 61 76 65 20   GCC might have 
1744e 6a 75 73 74 20 63 6f 70 79 69 6e 67 20 74 68 65  just copying the
1744f 20 70 72 6f 62 6c 65 6d 20 66 72 6f 6d 20 61 20   problem from a 
17450 70 72 69 6f 72 20 63 6f 6d 70 69 6c 65 72 2e 0a  prior compiler..
17451 2a 2a 20 49 20 61 6d 20 61 6c 73 6f 20 74 6f 6c  ** I am also tol
17452 64 20 74 68 61 74 20 6e 65 77 65 72 20 76 65 72  d that newer ver
17453 73 69 6f 6e 73 20 6f 66 20 47 43 43 20 74 68 61  sions of GCC tha
17454 74 20 66 6f 6c 6c 6f 77 20 61 20 64 69 66 66 65  t follow a diffe
17455 72 65 6e 74 0a 2a 2a 20 41 42 49 20 67 65 74 20  rent.** ABI get 
17456 74 68 65 20 62 79 74 65 20 6f 72 64 65 72 20 72  the byte order r
17457 69 67 68 74 2e 0a 2a 2a 0a 2a 2a 20 44 65 76 65  ight..**.** Deve
17458 6c 6f 70 65 72 73 20 75 73 69 6e 67 20 53 51 4c  lopers using SQL
17459 69 74 65 20 6f 6e 20 61 6e 20 41 52 4d 37 20 73  ite on an ARM7 s
1745a 68 6f 75 6c 64 20 63 6f 6d 70 69 6c 65 20 61 6e  hould compile an
1745b 64 20 72 75 6e 20 74 68 65 69 72 0a 2a 2a 20 61  d run their.** a
1745c 70 70 6c 69 63 61 74 69 6f 6e 20 75 73 69 6e 67  pplication using
1745d 20 2d 44 53 51 4c 49 54 45 5f 44 45 42 55 47 3d   -DSQLITE_DEBUG=
1745e 31 20 61 74 20 6c 65 61 73 74 20 6f 6e 63 65 2e  1 at least once.
1745f 20 20 57 69 74 68 20 44 45 42 55 47 0a 2a 2a 20    With DEBUG.** 
17460 65 6e 61 62 6c 65 64 2c 20 73 6f 6d 65 20 61 73  enabled, some as
17461 73 65 72 74 73 20 62 65 6c 6f 77 20 77 69 6c 6c  serts below will
17462 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65   ensure that the
17463 20 62 79 74 65 20 6f 72 64 65 72 20 6f 66 0a 2a   byte order of.*
17464 2a 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  * floating point
17465 20 76 61 6c 75 65 73 20 69 73 20 63 6f 72 72 65   values is corre
17466 63 74 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 37 2d  ct..**.** (2007-
17467 30 38 2d 33 30 29 20 20 46 72 61 6e 6b 20 76 61  08-30)  Frank va
17468 6e 20 56 75 67 74 20 68 61 73 20 73 74 75 64 69  n Vugt has studi
17469 65 64 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20  ed this problem 
1746a 63 6c 6f 73 65 6c 79 0a 2a 2a 20 61 6e 64 20 68  closely.** and h
1746b 61 73 20 73 65 6e 64 20 68 69 73 20 66 69 6e 64  as send his find
1746c 69 6e 67 73 20 74 6f 20 74 68 65 20 53 51 4c 69  ings to the SQLi
1746d 74 65 20 64 65 76 65 6c 6f 70 65 72 73 2e 20 20  te developers.  
1746e 46 72 61 6e 6b 0a 2a 2a 20 77 72 69 74 65 73 20  Frank.** writes 
1746f 74 68 61 74 20 73 6f 6d 65 20 4c 69 6e 75 78 20  that some Linux 
17470 6b 65 72 6e 65 6c 73 20 6f 66 66 65 72 20 66 6c  kernels offer fl
17471 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 68 61 72  oating point har
17472 64 77 61 72 65 0a 2a 2a 20 65 6d 75 6c 61 74 69  dware.** emulati
17473 6f 6e 20 74 68 61 74 20 75 73 65 73 20 6f 6e 6c  on that uses onl
17474 79 20 33 32 2d 62 69 74 20 6d 61 6e 74 69 73 73  y 32-bit mantiss
17475 61 73 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20  as instead of a 
17476 66 75 6c 6c 20 0a 2a 2a 20 34 38 2d 62 69 74 73  full .** 48-bits
17477 20 61 73 20 72 65 71 75 69 72 65 64 20 62 79 20   as required by 
17478 74 68 65 20 49 45 45 45 20 73 74 61 6e 64 61 72  the IEEE standar
17479 64 2e 20 20 28 54 68 69 73 20 69 73 20 74 68 65  d.  (This is the
1747a 0a 2a 2a 20 43 4f 4e 46 49 47 5f 46 50 45 5f 46  .** CONFIG_FPE_F
1747b 41 53 54 46 50 45 20 6f 70 74 69 6f 6e 2e 29 20  ASTFPE option.) 
1747c 20 4f 6e 20 73 75 63 68 20 73 79 73 74 65 6d 73   On such systems
1747d 2c 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  , floating point
1747e 0a 2a 2a 20 62 79 74 65 20 73 77 61 70 70 69 6e  .** byte swappin
1747f 67 20 62 65 63 6f 6d 65 73 20 76 65 72 79 20 63  g becomes very c
17480 6f 6d 70 6c 69 63 61 74 65 64 2e 20 20 54 6f 20  omplicated.  To 
17481 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d 73 2c 0a  avoid problems,.
17482 2a 2a 20 74 68 65 20 6e 65 63 65 73 73 61 72 79  ** the necessary
17483 20 62 79 74 65 20 73 77 61 70 70 69 6e 67 20 69   byte swapping i
17484 73 20 63 61 72 72 69 65 64 20 6f 75 74 20 75 73  s carried out us
17485 69 6e 67 20 61 20 36 34 2d 62 69 74 20 69 6e 74  ing a 64-bit int
17486 65 67 65 72 0a 2a 2a 20 72 61 74 68 65 72 20 74  eger.** rather t
17487 68 61 6e 20 61 20 36 34 2d 62 69 74 20 66 6c 6f  han a 64-bit flo
17488 61 74 2e 20 20 46 72 61 6e 6b 20 61 73 73 75 72  at.  Frank assur
17489 65 73 20 75 73 20 74 68 61 74 20 74 68 65 20 63  es us that the c
1748a 6f 64 65 20 68 65 72 65 0a 2a 2a 20 77 6f 72 6b  ode here.** work
1748b 73 20 66 6f 72 20 68 69 6d 2e 20 20 57 65 2c 20  s for him.  We, 
1748c 74 68 65 20 64 65 76 65 6c 6f 70 65 72 73 2c 20  the developers, 
1748d 68 61 76 65 20 6e 6f 20 77 61 79 20 74 6f 20 69  have no way to i
1748e 6e 64 65 70 65 6e 64 65 6e 74 6c 79 0a 2a 2a 20  ndependently.** 
1748f 76 65 72 69 66 79 20 74 68 69 73 2c 20 62 75 74  verify this, but
17490 20 46 72 61 6e 6b 20 73 65 65 6d 73 20 74 6f 20   Frank seems to 
17491 6b 6e 6f 77 20 77 68 61 74 20 68 65 20 69 73 20  know what he is 
17492 74 61 6c 6b 69 6e 67 20 61 62 6f 75 74 0a 2a 2a  talking about.**
17493 20 73 6f 20 77 65 20 74 72 75 73 74 20 68 69 6d   so we trust him
17494 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
17495 54 45 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f  TE_MIXED_ENDIAN_
17496 36 34 42 49 54 5f 46 4c 4f 41 54 0a 73 74 61 74  64BIT_FLOAT.stat
17497 69 63 20 75 36 34 20 66 6c 6f 61 74 53 77 61 70  ic u64 floatSwap
17498 28 75 36 34 20 69 6e 29 7b 0a 20 20 75 6e 69 6f  (u64 in){.  unio
17499 6e 20 7b 0a 20 20 20 20 75 36 34 20 72 3b 0a 20  n {.    u64 r;. 
1749a 20 20 20 75 33 32 20 69 5b 32 5d 3b 0a 20 20 7d     u32 i[2];.  }
1749b 20 75 3b 0a 20 20 75 33 32 20 74 3b 0a 0a 20 20   u;.  u32 t;..  
1749c 75 2e 72 20 3d 20 69 6e 3b 0a 20 20 74 20 3d 20  u.r = in;.  t = 
1749d 75 2e 69 5b 30 5d 3b 0a 20 20 75 2e 69 5b 30 5d  u.i[0];.  u.i[0]
1749e 20 3d 20 75 2e 69 5b 31 5d 3b 0a 20 20 75 2e 69   = u.i[1];.  u.i
1749f 5b 31 5d 20 3d 20 74 3b 0a 20 20 72 65 74 75 72  [1] = t;.  retur
174a0 6e 20 75 2e 72 3b 0a 7d 0a 23 20 64 65 66 69 6e  n u.r;.}.# defin
174a1 65 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61  e swapMixedEndia
174a2 6e 46 6c 6f 61 74 28 58 29 20 20 58 20 3d 20 66  nFloat(X)  X = f
174a3 6c 6f 61 74 53 77 61 70 28 58 29 0a 23 65 6c 73  loatSwap(X).#els
174a4 65 0a 23 20 64 65 66 69 6e 65 20 73 77 61 70 4d  e.# define swapM
174a5 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28  ixedEndianFloat(
174a6 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  X).#endif../*.**
174a7 20 57 72 69 74 65 20 74 68 65 20 73 65 72 69 61   Write the seria
174a8 6c 69 7a 65 64 20 64 61 74 61 20 62 6c 6f 62 20  lized data blob 
174a9 66 6f 72 20 74 68 65 20 76 61 6c 75 65 20 73 74  for the value st
174aa 6f 72 65 64 20 69 6e 20 70 4d 65 6d 20 69 6e 74  ored in pMem int
174ab 6f 20 0a 2a 2a 20 62 75 66 2e 20 49 74 20 69 73  o .** buf. It is
174ac 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68   assumed that th
174ad 65 20 63 61 6c 6c 65 72 20 68 61 73 20 61 6c 6c  e caller has all
174ae 6f 63 61 74 65 64 20 73 75 66 66 69 63 69 65 6e  ocated sufficien
174af 74 20 73 70 61 63 65 2e 0a 2a 2a 20 52 65 74 75  t space..** Retu
174b0 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
174b1 20 62 79 74 65 73 20 77 72 69 74 74 65 6e 2e 0a   bytes written..
174b2 2a 2a 0a 2a 2a 20 6e 42 75 66 20 69 73 20 74 68  **.** nBuf is th
174b3 65 20 61 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63  e amount of spac
174b4 65 20 6c 65 66 74 20 69 6e 20 62 75 66 5b 5d 2e  e left in buf[].
174b5 20 20 6e 42 75 66 20 6d 75 73 74 20 61 6c 77 61    nBuf must alwa
174b6 79 73 20 62 65 0a 2a 2a 20 6c 61 72 67 65 20 65  ys be.** large e
174b7 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 68  nough to hold th
174b8 65 20 65 6e 74 69 72 65 20 66 69 65 6c 64 2e 20  e entire field. 
174b9 20 45 78 63 65 70 74 2c 20 69 66 20 74 68 65 20   Except, if the 
174ba 66 69 65 6c 64 20 69 73 0a 2a 2a 20 61 20 62 6c  field is.** a bl
174bb 6f 62 20 77 69 74 68 20 61 20 7a 65 72 6f 2d 66  ob with a zero-f
174bc 69 6c 6c 65 64 20 74 61 69 6c 2c 20 74 68 65 6e  illed tail, then
174bd 20 62 75 66 5b 5d 20 6d 69 67 68 74 20 62 65 20   buf[] might be 
174be 6a 75 73 74 20 74 68 65 20 72 69 67 68 74 0a 2a  just the right.*
174bf 2a 20 73 69 7a 65 20 74 6f 20 68 6f 6c 64 20 65  * size to hold e
174c0 76 65 72 79 74 68 69 6e 67 20 65 78 63 65 70 74  verything except
174c1 20 66 6f 72 20 74 68 65 20 7a 65 72 6f 2d 66 69   for the zero-fi
174c2 6c 6c 65 64 20 74 61 69 6c 2e 20 20 49 66 20 62  lled tail.  If b
174c3 75 66 5b 5d 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20  uf[].** is only 
174c4 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f  big enough to ho
174c5 6c 64 20 74 68 65 20 6e 6f 6e 2d 7a 65 72 6f 20  ld the non-zero 
174c6 70 72 65 66 69 78 2c 20 74 68 65 6e 20 6f 6e 6c  prefix, then onl
174c7 79 20 77 72 69 74 65 20 74 68 61 74 0a 2a 2a 20  y write that.** 
174c8 70 72 65 66 69 78 20 69 6e 74 6f 20 62 75 66 5b  prefix into buf[
174c9 5d 2e 20 20 42 75 74 20 69 66 20 62 75 66 5b 5d  ].  But if buf[]
174ca 20 69 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 68   is large enough
174cb 20 74 6f 20 68 6f 6c 64 20 62 6f 74 68 20 74 68   to hold both th
174cc 65 0a 2a 2a 20 70 72 65 66 69 78 20 61 6e 64 20  e.** prefix and 
174cd 74 68 65 20 74 61 69 6c 20 74 68 65 6e 20 77 72  the tail then wr
174ce 69 74 65 20 74 68 65 20 70 72 65 66 69 78 20 61  ite the prefix a
174cf 6e 64 20 73 65 74 20 74 68 65 20 74 61 69 6c 20  nd set the tail 
174d0 74 6f 20 61 6c 6c 0a 2a 2a 20 7a 65 72 6f 73 2e  to all.** zeros.
174d1 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  .**.** Return th
174d2 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
174d3 73 20 61 63 74 75 61 6c 6c 79 20 77 72 69 74 74  s actually writt
174d4 65 6e 20 69 6e 74 6f 20 62 75 66 5b 5d 2e 20 20  en into buf[].  
174d5 54 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66  The number.** of
174d6 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 7a 65   bytes in the ze
174d7 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 20 69  ro-filled tail i
174d8 73 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68  s included in th
174d9 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f  e return value o
174da 6e 6c 79 0a 2a 2a 20 69 66 20 74 68 6f 73 65 20  nly.** if those 
174db 62 79 74 65 73 20 77 65 72 65 20 7a 65 72 6f 65  bytes were zeroe
174dc 64 20 69 6e 20 62 75 66 5b 5d 2e 0a 2a 2f 20 0a  d in buf[]..*/ .
174dd 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
174de 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  nt sqlite3VdbeSe
174df 72 69 61 6c 50 75 74 28 75 38 20 2a 62 75 66 2c  rialPut(u8 *buf,
174e0 20 69 6e 74 20 6e 42 75 66 2c 20 4d 65 6d 20 2a   int nBuf, Mem *
174e1 70 4d 65 6d 2c 20 69 6e 74 20 66 69 6c 65 5f 66  pMem, int file_f
174e2 6f 72 6d 61 74 29 7b 0a 20 20 75 33 32 20 73 65  ormat){.  u32 se
174e3 72 69 61 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69  rial_type = sqli
174e4 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
174e5 65 28 70 4d 65 6d 2c 20 66 69 6c 65 5f 66 6f 72  e(pMem, file_for
174e6 6d 61 74 29 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b  mat);.  int len;
174e7 0a 0a 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 61  ..  /* Integer a
174e8 6e 64 20 52 65 61 6c 20 2a 2f 0a 20 20 69 66 28  nd Real */.  if(
174e9 20 73 65 72 69 61 6c 5f 74 79 70 65 3c 3d 37 20   serial_type<=7 
174ea 26 26 20 73 65 72 69 61 6c 5f 74 79 70 65 3e 30  && serial_type>0
174eb 20 29 7b 0a 20 20 20 20 75 36 34 20 76 3b 0a 20   ){.    u64 v;. 
174ec 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 66     int i;.    if
174ed 28 20 73 65 72 69 61 6c 5f 74 79 70 65 3d 3d 37  ( serial_type==7
174ee 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
174ef 28 20 73 69 7a 65 6f 66 28 76 29 3d 3d 73 69 7a  ( sizeof(v)==siz
174f0 65 6f 66 28 70 4d 65 6d 2d 3e 72 29 20 29 3b 0a  eof(pMem->r) );.
174f1 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 76 2c        memcpy(&v,
174f2 20 26 70 4d 65 6d 2d 3e 72 2c 20 73 69 7a 65 6f   &pMem->r, sizeo
174f3 66 28 76 29 29 3b 0a 20 20 20 20 20 20 73 77 61  f(v));.      swa
174f4 70 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61  pMixedEndianFloa
174f5 74 28 76 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  t(v);.    }else{
174f6 0a 20 20 20 20 20 20 76 20 3d 20 70 4d 65 6d 2d  .      v = pMem-
174f7 3e 75 2e 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20  >u.i;.    }.    
174f8 6c 65 6e 20 3d 20 69 20 3d 20 73 71 6c 69 74 65  len = i = sqlite
174f9 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c  3VdbeSerialTypeL
174fa 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 29 3b  en(serial_type);
174fb 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e  .    assert( len
174fc 3c 3d 6e 42 75 66 20 29 3b 0a 20 20 20 20 77 68  <=nBuf );.    wh
174fd 69 6c 65 28 20 69 2d 2d 20 29 7b 0a 20 20 20 20  ile( i-- ){.    
174fe 20 20 62 75 66 5b 69 5d 20 3d 20 28 76 26 30 78    buf[i] = (v&0x
174ff 46 46 29 3b 0a 20 20 20 20 20 20 76 20 3e 3e 3d  FF);.      v >>=
17500 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65   8;.    }.    re
17501 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20  turn len;.  }.. 
17502 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 62 6c   /* String or bl
17503 6f 62 20 2a 2f 0a 20 20 69 66 28 20 73 65 72 69  ob */.  if( seri
17504 61 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20  al_type>=12 ){. 
17505 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d     assert( pMem-
17506 3e 6e 20 2b 20 28 28 70 4d 65 6d 2d 3e 66 6c 61  >n + ((pMem->fla
17507 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 29 3f 70  gs & MEM_Zero)?p
17508 4d 65 6d 2d 3e 75 2e 69 3a 30 29 0a 20 20 20 20  Mem->u.i:0).    
17509 20 20 20 20 20 20 20 20 20 3d 3d 20 73 71 6c 69           == sqli
1750a 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
1750b 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65  eLen(serial_type
1750c 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ) );.    assert(
1750d 20 70 4d 65 6d 2d 3e 6e 3c 3d 6e 42 75 66 20 29   pMem->n<=nBuf )
1750e 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d  ;.    len = pMem
1750f 2d 3e 6e 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  ->n;.    memcpy(
17510 62 75 66 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 6c 65  buf, pMem->z, le
17511 6e 29 3b 0a 20 20 20 20 69 66 28 20 70 4d 65 6d  n);.    if( pMem
17512 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65  ->flags & MEM_Ze
17513 72 6f 20 29 7b 0a 20 20 20 20 20 20 6c 65 6e 20  ro ){.      len 
17514 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20  += pMem->u.i;.  
17515 20 20 20 20 69 66 28 20 6c 65 6e 3e 6e 42 75 66      if( len>nBuf
17516 20 29 7b 0a 20 20 20 20 20 20 20 20 6c 65 6e 20   ){.        len 
17517 3d 20 6e 42 75 66 3b 0a 20 20 20 20 20 20 7d 0a  = nBuf;.      }.
17518 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 62 75        memset(&bu
17519 66 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c 20 6c  f[pMem->n], 0, l
1751a 65 6e 2d 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20  en-pMem->n);.   
1751b 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6c 65   }.    return le
1751c 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 55 4c  n;.  }..  /* NUL
1751d 4c 20 6f 72 20 63 6f 6e 73 74 61 6e 74 73 20 30  L or constants 0
1751e 20 6f 72 20 31 20 2a 2f 0a 20 20 72 65 74 75 72   or 1 */.  retur
1751f 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  n 0;.}../*.** De
17520 73 65 72 69 61 6c 69 7a 65 20 74 68 65 20 64 61  serialize the da
17521 74 61 20 62 6c 6f 62 20 70 6f 69 6e 74 65 64 20  ta blob pointed 
17522 74 6f 20 62 79 20 62 75 66 20 61 73 20 73 65 72  to by buf as ser
17523 69 61 6c 20 74 79 70 65 20 73 65 72 69 61 6c 5f  ial type serial_
17524 74 79 70 65 0a 2a 2a 20 61 6e 64 20 73 74 6f 72  type.** and stor
17525 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20  e the result in 
17526 70 4d 65 6d 2e 20 20 52 65 74 75 72 6e 20 74 68  pMem.  Return th
17527 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
17528 73 20 72 65 61 64 2e 0a 2a 2f 20 0a 53 51 4c 49  s read..*/ .SQLI
17529 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1752a 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
1752b 47 65 74 28 0a 20 20 63 6f 6e 73 74 20 75 6e 73  Get(.  const uns
1752c 69 67 6e 65 64 20 63 68 61 72 20 2a 62 75 66 2c  igned char *buf,
1752d 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20 74       /* Buffer t
1752e 6f 20 64 65 73 65 72 69 61 6c 69 7a 65 20 66 72  o deserialize fr
1752f 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 73 65 72 69  om */.  u32 seri
17530 61 6c 5f 74 79 70 65 2c 20 20 20 20 20 20 20 20  al_type,        
17531 20 20 20 20 20 20 2f 2a 20 53 65 72 69 61 6c 20        /* Serial 
17532 74 79 70 65 20 74 6f 20 64 65 73 65 72 69 61 6c  type to deserial
17533 69 7a 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d  ize */.  Mem *pM
17534 65 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20  em              
17535 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79         /* Memory
17536 20 63 65 6c 6c 20 74 6f 20 77 72 69 74 65 20 76   cell to write v
17537 61 6c 75 65 20 69 6e 74 6f 20 2a 2f 0a 29 7b 0a  alue into */.){.
17538 20 20 73 77 69 74 63 68 28 20 73 65 72 69 61 6c    switch( serial
17539 5f 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73  _type ){.    cas
1753a 65 20 31 30 3a 20 20 20 2f 2a 20 52 65 73 65 72  e 10:   /* Reser
1753b 76 65 64 20 66 6f 72 20 66 75 74 75 72 65 20 75  ved for future u
1753c 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31  se */.    case 1
1753d 31 3a 20 20 20 2f 2a 20 52 65 73 65 72 76 65 64  1:   /* Reserved
1753e 20 66 6f 72 20 66 75 74 75 72 65 20 75 73 65 20   for future use 
1753f 2a 2f 0a 20 20 20 20 63 61 73 65 20 30 3a 20 7b  */.    case 0: {
17540 20 20 2f 2a 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20    /* NULL */.   
17541 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
17542 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20   MEM_Null;.     
17543 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
17544 20 20 63 61 73 65 20 31 3a 20 7b 20 2f 2a 20 31    case 1: { /* 1
17545 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74  -byte signed int
17546 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d  eger */.      pM
17547 65 6d 2d 3e 75 2e 69 20 3d 20 28 73 69 67 6e 65  em->u.i = (signe
17548 64 20 63 68 61 72 29 62 75 66 5b 30 5d 3b 0a 20  d char)buf[0];. 
17549 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73       pMem->flags
1754a 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20   = MEM_Int;.    
1754b 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
1754c 7d 0a 20 20 20 20 63 61 73 65 20 32 3a 20 7b 20  }.    case 2: { 
1754d 2f 2a 20 32 2d 62 79 74 65 20 73 69 67 6e 65 64  /* 2-byte signed
1754e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20   integer */.    
1754f 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 28    pMem->u.i = ((
17550 28 73 69 67 6e 65 64 20 63 68 61 72 29 62 75 66  (signed char)buf
17551 5b 30 5d 29 3c 3c 38 29 20 7c 20 62 75 66 5b 31  [0])<<8) | buf[1
17552 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  ];.      pMem->f
17553 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
17554 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a        return 2;.
17555 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 33      }.    case 3
17556 3a 20 7b 20 2f 2a 20 33 2d 62 79 74 65 20 73 69  : { /* 3-byte si
17557 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a  gned integer */.
17558 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20        pMem->u.i 
17559 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68 61 72  = (((signed char
1755a 29 62 75 66 5b 30 5d 29 3c 3c 31 36 29 20 7c 20  )buf[0])<<16) | 
1755b 28 62 75 66 5b 31 5d 3c 3c 38 29 20 7c 20 62 75  (buf[1]<<8) | bu
1755c 66 5b 32 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d  f[2];.      pMem
1755d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e  ->flags = MEM_In
1755e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  t;.      return 
1755f 33 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  3;.    }.    cas
17560 65 20 34 3a 20 7b 20 2f 2a 20 34 2d 62 79 74 65  e 4: { /* 4-byte
17561 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20   signed integer 
17562 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75  */.      pMem->u
17563 2e 69 20 3d 20 28 62 75 66 5b 30 5d 3c 3c 32 34  .i = (buf[0]<<24
17564 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 31 36 29  ) | (buf[1]<<16)
17565 20 7c 20 28 62 75 66 5b 32 5d 3c 3c 38 29 20 7c   | (buf[2]<<8) |
17566 20 62 75 66 5b 33 5d 3b 0a 20 20 20 20 20 20 70   buf[3];.      p
17567 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
17568 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75  _Int;.      retu
17569 72 6e 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 4;.    }.    
1756a 63 61 73 65 20 35 3a 20 7b 20 2f 2a 20 36 2d 62  case 5: { /* 6-b
1756b 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67  yte signed integ
1756c 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 20  er */.      u64 
1756d 78 20 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68  x = (((signed ch
1756e 61 72 29 62 75 66 5b 30 5d 29 3c 3c 38 29 20 7c  ar)buf[0])<<8) |
1756f 20 62 75 66 5b 31 5d 3b 0a 20 20 20 20 20 20 75   buf[1];.      u
17570 33 32 20 79 20 3d 20 28 62 75 66 5b 32 5d 3c 3c  32 y = (buf[2]<<
17571 32 34 29 20 7c 20 28 62 75 66 5b 33 5d 3c 3c 31  24) | (buf[3]<<1
17572 36 29 20 7c 20 28 62 75 66 5b 34 5d 3c 3c 38 29  6) | (buf[4]<<8)
17573 20 7c 20 62 75 66 5b 35 5d 3b 0a 20 20 20 20 20   | buf[5];.     
17574 20 78 20 3d 20 28 78 3c 3c 33 32 29 20 7c 20 79   x = (x<<32) | y
17575 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e  ;.      pMem->u.
17576 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b 0a 20  i = *(i64*)&x;. 
17577 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73       pMem->flags
17578 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20   = MEM_Int;.    
17579 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 20 20    return 6;.    
1757a 7d 0a 20 20 20 20 63 61 73 65 20 36 3a 20 20 20  }.    case 6:   
1757b 2f 2a 20 38 2d 62 79 74 65 20 73 69 67 6e 65 64  /* 8-byte signed
1757c 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20   integer */.    
1757d 63 61 73 65 20 37 3a 20 7b 20 2f 2a 20 49 45 45  case 7: { /* IEE
1757e 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  E floating point
1757f 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 78 3b   */.      u64 x;
17580 0a 20 20 20 20 20 20 75 33 32 20 79 3b 0a 23 69  .      u32 y;.#i
17581 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55  f !defined(NDEBU
17582 47 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  G) && !defined(S
17583 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
17584 49 4e 47 5f 50 4f 49 4e 54 29 0a 20 20 20 20 20  ING_POINT).     
17585 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20   /* Verify that 
17586 69 6e 74 65 67 65 72 73 20 61 6e 64 20 66 6c 6f  integers and flo
17587 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75  ating point valu
17588 65 73 20 75 73 65 20 74 68 65 20 73 61 6d 65 0a  es use the same.
17589 20 20 20 20 20 20 2a 2a 20 62 79 74 65 20 6f 72        ** byte or
1758a 64 65 72 2e 20 20 4f 72 2c 20 74 68 61 74 20 69  der.  Or, that i
1758b 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f 45  f SQLITE_MIXED_E
1758c 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f 41  NDIAN_64BIT_FLOA
1758d 54 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 64 65  T is.      ** de
1758e 66 69 6e 65 64 20 74 68 61 74 20 36 34 2d 62 69  fined that 64-bi
1758f 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  t floating point
17590 20 76 61 6c 75 65 73 20 72 65 61 6c 6c 79 20 61   values really a
17591 72 65 20 6d 69 78 65 64 0a 20 20 20 20 20 20 2a  re mixed.      *
17592 2a 20 65 6e 64 69 61 6e 2e 0a 20 20 20 20 20 20  * endian..      
17593 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69 63 20  */.      static 
17594 63 6f 6e 73 74 20 75 36 34 20 74 31 20 3d 20 28  const u64 t1 = (
17595 28 75 36 34 29 30 78 33 66 66 30 30 30 30 30 29  (u64)0x3ff00000)
17596 3c 3c 33 32 3b 0a 20 20 20 20 20 20 73 74 61 74  <<32;.      stat
17597 69 63 20 63 6f 6e 73 74 20 64 6f 75 62 6c 65 20  ic const double 
17598 72 31 20 3d 20 31 2e 30 3b 0a 20 20 20 20 20 20  r1 = 1.0;.      
17599 75 36 34 20 74 32 20 3d 20 74 31 3b 0a 20 20 20  u64 t2 = t1;.   
1759a 20 20 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69     swapMixedEndi
1759b 61 6e 46 6c 6f 61 74 28 74 32 29 3b 0a 20 20 20  anFloat(t2);.   
1759c 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f     assert( sizeo
1759d 66 28 72 31 29 3d 3d 73 69 7a 65 6f 66 28 74 32  f(r1)==sizeof(t2
1759e 29 20 26 26 20 6d 65 6d 63 6d 70 28 26 72 31 2c  ) && memcmp(&r1,
1759f 20 26 74 32 2c 20 73 69 7a 65 6f 66 28 72 31 29   &t2, sizeof(r1)
175a0 29 3d 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a 0a  )==0 );.#endif..
175a1 20 20 20 20 20 20 78 20 3d 20 28 62 75 66 5b 30        x = (buf[0
175a2 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 31 5d  ]<<24) | (buf[1]
175a3 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 32 5d 3c  <<16) | (buf[2]<
175a4 3c 38 29 20 7c 20 62 75 66 5b 33 5d 3b 0a 20 20  <8) | buf[3];.  
175a5 20 20 20 20 79 20 3d 20 28 62 75 66 5b 34 5d 3c      y = (buf[4]<
175a6 3c 32 34 29 20 7c 20 28 62 75 66 5b 35 5d 3c 3c  <24) | (buf[5]<<
175a7 31 36 29 20 7c 20 28 62 75 66 5b 36 5d 3c 3c 38  16) | (buf[6]<<8
175a8 29 20 7c 20 62 75 66 5b 37 5d 3b 0a 20 20 20 20  ) | buf[7];.    
175a9 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 20 7c 20    x = (x<<32) | 
175aa 79 3b 0a 20 20 20 20 20 20 69 66 28 20 73 65 72  y;.      if( ser
175ab 69 61 6c 5f 74 79 70 65 3d 3d 36 20 29 7b 0a 20  ial_type==6 ){. 
175ac 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69         pMem->u.i
175ad 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b 0a 20 20   = *(i64*)&x;.  
175ae 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
175af 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20  s = MEM_Int;.   
175b0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
175b1 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
175b2 28 78 29 3d 3d 38 20 26 26 20 73 69 7a 65 6f 66  (x)==8 && sizeof
175b3 28 70 4d 65 6d 2d 3e 72 29 3d 3d 38 20 29 3b 0a  (pMem->r)==8 );.
175b4 20 20 20 20 20 20 20 20 73 77 61 70 4d 69 78 65          swapMixe
175b5 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 78 29 3b  dEndianFloat(x);
175b6 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
175b7 26 70 4d 65 6d 2d 3e 72 2c 20 26 78 2c 20 73 69  &pMem->r, &x, si
175b8 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20 20 20 20  zeof(x));.      
175b9 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
175ba 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 70 4d 65  sqlite3IsNaN(pMe
175bb 6d 2d 3e 72 29 20 3f 20 4d 45 4d 5f 4e 75 6c 6c  m->r) ? MEM_Null
175bc 20 3a 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 20   : MEM_Real;.   
175bd 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72     }.      retur
175be 6e 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 8;.    }.    c
175bf 61 73 65 20 38 3a 20 20 20 20 2f 2a 20 49 6e 74  ase 8:    /* Int
175c0 65 67 65 72 20 30 20 2a 2f 0a 20 20 20 20 63 61  eger 0 */.    ca
175c1 73 65 20 39 3a 20 7b 20 20 2f 2a 20 49 6e 74 65  se 9: {  /* Inte
175c2 67 65 72 20 31 20 2a 2f 0a 20 20 20 20 20 20 70  ger 1 */.      p
175c3 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 65 72 69 61  Mem->u.i = seria
175c4 6c 5f 74 79 70 65 2d 38 3b 0a 20 20 20 20 20 20  l_type-8;.      
175c5 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
175c6 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74  M_Int;.      ret
175c7 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 0;.    }.   
175c8 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
175c9 20 20 69 6e 74 20 6c 65 6e 20 3d 20 28 73 65 72    int len = (ser
175ca 69 61 6c 5f 74 79 70 65 2d 31 32 29 2f 32 3b 0a  ial_type-12)/2;.
175cb 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20        pMem->z = 
175cc 28 63 68 61 72 20 2a 29 62 75 66 3b 0a 20 20 20  (char *)buf;.   
175cd 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6c 65 6e     pMem->n = len
175ce 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 78 44  ;.      pMem->xD
175cf 65 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66  el = 0;.      if
175d0 28 20 73 65 72 69 61 6c 5f 74 79 70 65 26 30 78  ( serial_type&0x
175d1 30 31 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4d  01 ){.        pM
175d2 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
175d3 53 74 72 20 7c 20 4d 45 4d 5f 45 70 68 65 6d 3b  Str | MEM_Ephem;
175d4 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
175d5 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
175d6 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20 4d  s = MEM_Blob | M
175d7 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20 20  EM_Ephem;.      
175d8 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 6c  }.      return l
175d9 65 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  en;.    }.  }.  
175da 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a  return 0;.}.../*
175db 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 6e 4b  .** Given the nK
175dc 65 79 2d 62 79 74 65 20 65 6e 63 6f 64 69 6e 67  ey-byte encoding
175dd 20 6f 66 20 61 20 72 65 63 6f 72 64 20 69 6e 20   of a record in 
175de 70 4b 65 79 5b 5d 2c 20 70 61 72 73 65 20 74 68  pKey[], parse th
175df 65 0a 2a 2a 20 72 65 63 6f 72 64 20 69 6e 74 6f  e.** record into
175e0 20 61 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72   a UnpackedRecor
175e1 64 20 73 74 72 75 63 74 75 72 65 2e 20 20 52 65  d structure.  Re
175e2 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
175e3 6f 0a 2a 2a 20 74 68 61 74 20 73 74 72 75 63 74  o.** that struct
175e4 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ure..**.** The c
175e5 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  alling function 
175e6 6d 69 67 68 74 20 70 72 6f 76 69 64 65 20 73 7a  might provide sz
175e7 53 70 61 63 65 20 62 79 74 65 73 20 6f 66 20 6d  Space bytes of m
175e8 65 6d 6f 72 79 0a 2a 2a 20 73 70 61 63 65 20 61  emory.** space a
175e9 74 20 70 53 70 61 63 65 2e 20 20 54 68 69 73 20  t pSpace.  This 
175ea 73 70 61 63 65 20 63 61 6e 20 62 65 20 75 73 65  space can be use
175eb 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65  d to hold the re
175ec 74 75 72 6e 65 64 0a 2a 2a 20 56 44 62 65 50 61  turned.** VDbePa
175ed 72 73 65 64 52 65 63 6f 72 64 20 73 74 72 75 63  rsedRecord struc
175ee 74 75 72 65 20 69 66 20 69 74 20 69 73 20 6c 61  ture if it is la
175ef 72 67 65 20 65 6e 6f 75 67 68 2e 20 20 49 66 20  rge enough.  If 
175f0 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 62 69 67  it is.** not big
175f1 20 65 6e 6f 75 67 68 2c 20 73 70 61 63 65 20 69   enough, space i
175f2 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  s obtained from 
175f3 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
175f4 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75  ..**.** The retu
175f5 72 6e 65 64 20 73 74 72 75 63 74 75 72 65 20 73  rned structure s
175f6 68 6f 75 6c 64 20 62 65 20 63 6c 6f 73 65 64 20  hould be closed 
175f7 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20  by a call to.** 
175f8 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74  sqlite3VdbeDelet
175f9 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28  eUnpackedRecord(
175fa 29 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52  )..*/ .SQLITE_PR
175fb 49 56 41 54 45 20 55 6e 70 61 63 6b 65 64 52 65  IVATE UnpackedRe
175fc 63 6f 72 64 20 2a 73 71 6c 69 74 65 33 56 64 62  cord *sqlite3Vdb
175fd 65 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 0a 20  eRecordUnpack(. 
175fe 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
175ff 66 6f 2c 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72  fo,     /* Infor
17600 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mation about the
17601 20 72 65 63 6f 72 64 20 66 6f 72 6d 61 74 20 2a   record format *
17602 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20  /.  int nKey,   
17603 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
17604 7a 65 20 6f 66 20 74 68 65 20 62 69 6e 61 72 79  ze of the binary
17605 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 6f 6e   record */.  con
17606 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20  st void *pKey,  
17607 20 20 20 20 2f 2a 20 54 68 65 20 62 69 6e 61 72      /* The binar
17608 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 55 6e  y record */.  Un
17609 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 53  packedRecord *pS
1760a 70 61 63 65 2c 2f 2a 20 53 70 61 63 65 20 61 76  pace,/* Space av
1760b 61 69 6c 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20  ailable to hold 
1760c 72 65 73 75 6c 74 69 6e 67 20 6f 62 6a 65 63 74  resulting object
1760d 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 70 61 63   */.  int szSpac
1760e 65 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e            /* 
1760f 53 69 7a 65 20 6f 66 20 70 53 70 61 63 65 5b 5d  Size of pSpace[]
17610 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 29 7b 0a   in bytes */.){.
17611 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
17612 20 63 68 61 72 20 2a 61 4b 65 79 20 3d 20 28 63   char *aKey = (c
17613 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
17614 61 72 20 2a 29 70 4b 65 79 3b 0a 20 20 55 6e 70  ar *)pKey;.  Unp
17615 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 3b 0a  ackedRecord *p;.
17616 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 69    int nByte;.  i
17617 6e 74 20 69 64 78 2c 20 64 3b 0a 20 20 75 31 36  nt idx, d;.  u16
17618 20 75 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   u;             
17619 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64 20      /* Unsigned 
1761a 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  loop counter */.
1761b 20 20 75 33 32 20 73 7a 48 64 72 3b 0a 20 20 4d    u32 szHdr;.  M
1761c 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 0a 20 20 61  em *pMem;.  .  a
1761d 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 4d 65  ssert( sizeof(Me
1761e 6d 29 3e 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b  m)>sizeof(*p) );
1761f 0a 20 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f  .  nByte = sizeo
17620 66 28 4d 65 6d 29 2a 28 70 4b 65 79 49 6e 66 6f  f(Mem)*(pKeyInfo
17621 2d 3e 6e 46 69 65 6c 64 2b 32 29 3b 0a 20 20 69  ->nField+2);.  i
17622 66 28 20 6e 42 79 74 65 3e 73 7a 53 70 61 63 65  f( nByte>szSpace
17623 20 29 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69   ){.    p = sqli
17624 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70  te3DbMallocRaw(p
17625 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 6e 42 79  KeyInfo->db, nBy
17626 74 65 29 3b 0a 20 20 20 20 69 66 28 20 70 3d 3d  te);.    if( p==
17627 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
17628 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 55 4e 50    p->flags = UNP
17629 41 43 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20  ACKED_NEED_FREE 
1762a 7c 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f  | UNPACKED_NEED_
1762b 44 45 53 54 52 4f 59 3b 0a 20 20 7d 65 6c 73 65  DESTROY;.  }else
1762c 7b 0a 20 20 20 20 70 20 3d 20 70 53 70 61 63 65  {.    p = pSpace
1762d 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d  ;.    p->flags =
1762e 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44   UNPACKED_NEED_D
1762f 45 53 54 52 4f 59 3b 0a 20 20 7d 0a 20 20 70 2d  ESTROY;.  }.  p-
17630 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79  >pKeyInfo = pKey
17631 49 6e 66 6f 3b 0a 20 20 70 2d 3e 6e 46 69 65 6c  Info;.  p->nFiel
17632 64 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46  d = pKeyInfo->nF
17633 69 65 6c 64 20 2b 20 31 3b 0a 20 20 70 2d 3e 61  ield + 1;.  p->a
17634 4d 65 6d 20 3d 20 70 4d 65 6d 20 3d 20 26 28 28  Mem = pMem = &((
17635 4d 65 6d 2a 29 70 29 5b 31 5d 3b 0a 20 20 69 64  Mem*)p)[1];.  id
17636 78 20 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28  x = getVarint32(
17637 61 4b 65 79 2c 20 73 7a 48 64 72 29 3b 0a 20 20  aKey, szHdr);.  
17638 64 20 3d 20 73 7a 48 64 72 3b 0a 20 20 75 20 3d  d = szHdr;.  u =
17639 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 64 78   0;.  while( idx
1763a 3c 73 7a 48 64 72 20 26 26 20 75 3c 70 2d 3e 6e  <szHdr && u<p->n
1763b 46 69 65 6c 64 20 29 7b 0a 20 20 20 20 75 33 32  Field ){.    u32
1763c 20 73 65 72 69 61 6c 5f 74 79 70 65 3b 0a 0a 20   serial_type;.. 
1763d 20 20 20 69 64 78 20 2b 3d 20 67 65 74 56 61 72     idx += getVar
1763e 69 6e 74 33 32 28 20 61 4b 65 79 2b 69 64 78 2c  int32( aKey+idx,
1763f 20 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20   serial_type);. 
17640 20 20 20 69 66 28 20 64 3e 3d 6e 4b 65 79 20 26     if( d>=nKey &
17641 26 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  & sqlite3VdbeSer
17642 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61  ialTypeLen(seria
17643 6c 5f 74 79 70 65 29 3e 30 20 29 20 62 72 65 61  l_type)>0 ) brea
17644 6b 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63  k;.    pMem->enc
17645 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63   = pKeyInfo->enc
17646 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 64 62 20 3d  ;.    pMem->db =
17647 20 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20   pKeyInfo->db;. 
17648 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
17649 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d   0;.    pMem->zM
1764a 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 64  alloc = 0;.    d
1764b 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53   += sqlite3VdbeS
1764c 65 72 69 61 6c 47 65 74 28 26 61 4b 65 79 5b 64  erialGet(&aKey[d
1764d 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 2c 20  ], serial_type, 
1764e 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2b  pMem);.    pMem+
1764f 2b 3b 0a 20 20 20 20 75 2b 2b 3b 0a 20 20 7d 0a  +;.    u++;.  }.
17650 20 20 61 73 73 65 72 74 28 20 75 3c 3d 70 4b 65    assert( u<=pKe
17651 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20  yInfo->nField + 
17652 31 20 29 3b 0a 20 20 70 2d 3e 6e 46 69 65 6c 64  1 );.  p->nField
17653 20 3d 20 75 3b 0a 20 20 72 65 74 75 72 6e 20 28   = u;.  return (
17654 76 6f 69 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  void*)p;.}../*.*
17655 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64  * This routine d
17656 65 73 74 72 6f 79 73 20 61 20 55 6e 70 61 63 6b  estroys a Unpack
17657 65 64 52 65 63 6f 72 64 20 6f 62 6a 65 63 74 0a  edRecord object.
17658 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
17659 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
1765a 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64  beDeleteUnpacked
1765b 52 65 63 6f 72 64 28 55 6e 70 61 63 6b 65 64 52  Record(UnpackedR
1765c 65 63 6f 72 64 20 2a 70 29 7b 0a 20 20 69 66 28  ecord *p){.  if(
1765d 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d   p ){.    if( p-
1765e 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45  >flags & UNPACKE
1765f 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 20 29  D_NEED_DESTROY )
17660 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20  {.      int i;. 
17661 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a       Mem *pMem;.
17662 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70        for(i=0, p
17663 4d 65 6d 3d 70 2d 3e 61 4d 65 6d 3b 20 69 3c 70  Mem=p->aMem; i<p
17664 2d 3e 6e 46 69 65 6c 64 3b 20 69 2b 2b 2c 20 70  ->nField; i++, p
17665 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  Mem++){.        
17666 69 66 28 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f  if( pMem->zMallo
17667 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  c ){.          s
17668 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
17669 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20  ease(pMem);.    
1766a 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
1766b 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 66    }.    if( p->f
1766c 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f  lags & UNPACKED_
1766d 4e 45 45 44 5f 46 52 45 45 20 29 7b 0a 20 20 20  NEED_FREE ){.   
1766e 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1766f 28 70 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 64 62  (p->pKeyInfo->db
17670 2c 20 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  , p);.    }.  }.
17671 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
17672 6e 63 74 69 6f 6e 20 63 6f 6d 70 61 72 65 73 20  nction compares 
17673 74 68 65 20 74 77 6f 20 74 61 62 6c 65 20 72 6f  the two table ro
17674 77 73 20 6f 72 20 69 6e 64 65 78 20 72 65 63 6f  ws or index reco
17675 72 64 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64  rds.** specified
17676 20 62 79 20 7b 6e 4b 65 79 31 2c 20 70 4b 65 79   by {nKey1, pKey
17677 31 7d 20 61 6e 64 20 70 50 4b 65 79 32 2e 20 20  1} and pPKey2.  
17678 49 74 20 72 65 74 75 72 6e 73 20 61 20 6e 65 67  It returns a neg
17679 61 74 69 76 65 2c 20 7a 65 72 6f 0a 2a 2a 20 6f  ative, zero.** o
1767a 72 20 70 6f 73 69 74 69 76 65 20 69 6e 74 65 67  r positive integ
1767b 65 72 20 69 66 20 6b 65 79 31 20 69 73 20 6c 65  er if key1 is le
1767c 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c 20 74  ss than, equal t
1767d 6f 20 6f 72 20 0a 2a 2a 20 67 72 65 61 74 65 72  o or .** greater
1767e 20 74 68 61 6e 20 6b 65 79 32 2e 20 20 54 68 65   than key2.  The
1767f 20 7b 6e 4b 65 79 31 2c 20 70 4b 65 79 31 7d 20   {nKey1, pKey1} 
17680 6b 65 79 20 6d 75 73 74 20 62 65 20 61 20 62 6c  key must be a bl
17681 6f 62 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79  ob.** created by
17682 20 74 68 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72   th OP_MakeRecor
17683 64 20 6f 70 63 6f 64 65 20 6f 66 20 74 68 65 20  d opcode of the 
17684 56 44 42 45 2e 20 20 54 68 65 20 70 50 4b 65 79  VDBE.  The pPKey
17685 32 0a 2a 2a 20 6b 65 79 20 6d 75 73 74 20 62 65  2.** key must be
17686 20 61 20 70 61 72 73 65 64 20 6b 65 79 20 73 75   a parsed key su
17687 63 68 20 61 73 20 6f 62 74 61 69 6e 65 64 20 66  ch as obtained f
17688 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64  rom.** sqlite3Vd
17689 62 65 50 61 72 73 65 52 65 63 6f 72 64 2e 0a 2a  beParseRecord..*
1768a 2a 0a 2a 2a 20 4b 65 79 31 20 61 6e 64 20 4b 65  *.** Key1 and Ke
1768b 79 32 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74  y2 do not have t
1768c 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 73 61  o contain the sa
1768d 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65  me number of fie
1768e 6c 64 73 2e 0a 2a 2a 20 54 68 65 20 6b 65 79 20  lds..** The key 
1768f 77 69 74 68 20 66 65 77 65 72 20 66 69 65 6c 64  with fewer field
17690 73 20 69 73 20 75 73 75 61 6c 6c 79 20 63 6f 6d  s is usually com
17691 70 61 72 65 73 20 6c 65 73 73 20 74 68 61 6e 20  pares less than 
17692 74 68 65 20 0a 2a 2a 20 6c 6f 6e 67 65 72 20 6b  the .** longer k
17693 65 79 2e 20 20 48 6f 77 65 76 65 72 20 69 66 20  ey.  However if 
17694 74 68 65 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43  the UNPACKED_INC
17695 52 4b 45 59 20 66 6c 61 67 73 20 69 6e 20 70 50  RKEY flags in pP
17696 4b 65 79 32 20 69 73 20 73 65 74 0a 2a 2a 20 61  Key2 is set.** a
17697 6e 64 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72  nd the common pr
17698 65 66 69 78 65 73 20 61 72 65 20 65 71 75 61 6c  efixes are equal
17699 2c 20 74 68 65 6e 20 6b 65 79 31 20 69 73 20 6c  , then key1 is l
1769a 65 73 73 20 74 68 61 6e 20 6b 65 79 32 2e 0a 2a  ess than key2..*
1769b 2a 20 4f 72 20 69 66 20 74 68 65 20 55 4e 50 41  * Or if the UNPA
1769c 43 4b 45 44 5f 4d 41 54 43 48 5f 50 52 45 46 49  CKED_MATCH_PREFI
1769d 58 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e  X flag is set an
1769e 64 20 74 68 65 20 70 72 65 66 69 78 65 73 20 61  d the prefixes a
1769f 72 65 0a 2a 2a 20 65 71 75 61 6c 2c 20 74 68 65  re.** equal, the
176a0 6e 20 74 68 65 20 6b 65 79 73 20 61 72 65 20 63  n the keys are c
176a1 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 65 20  onsidered to be 
176a2 65 71 75 61 6c 20 61 6e 64 0a 2a 2a 20 74 68 65  equal and.** the
176a3 20 70 61 72 74 73 20 62 65 79 6f 6e 64 20 74 68   parts beyond th
176a4 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 20  e common prefix 
176a5 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a  are ignored..**.
176a6 2a 2a 20 49 66 20 74 68 65 20 55 4e 50 41 43 4b  ** If the UNPACK
176a7 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 20  ED_IGNORE_ROWID 
176a8 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65  flag is set, the
176a9 6e 20 74 68 65 20 6c 61 73 74 20 62 79 74 65 20  n the last byte 
176aa 6f 66 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72  of.** the header
176ab 20 6f 66 20 70 4b 65 79 31 20 69 73 20 69 67 6e   of pKey1 is ign
176ac 6f 72 65 64 2e 20 20 49 74 20 69 73 20 61 73 73  ored.  It is ass
176ad 75 6d 65 64 20 74 68 61 74 20 70 4b 65 79 31 20  umed that pKey1 
176ae 69 73 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 20 6b  is.** an index k
176af 65 79 2c 20 61 6e 64 20 74 68 75 73 20 65 6e 64  ey, and thus end
176b0 73 20 77 69 74 68 20 61 20 72 6f 77 69 64 20 76  s with a rowid v
176b1 61 6c 75 65 2e 20 20 54 68 65 20 6c 61 73 74 20  alue.  The last 
176b2 62 79 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20 68  byte.** of the h
176b3 65 61 64 65 72 20 77 69 6c 6c 20 74 68 65 72 65  eader will there
176b4 66 6f 72 65 20 62 65 20 74 68 65 20 73 65 72 69  fore be the seri
176b5 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65 20 72  al type of the r
176b6 6f 77 69 64 3a 0a 2a 2a 20 6f 6e 65 20 6f 66 20  owid:.** one of 
176b7 31 2c 20 32 2c 20 33 2c 20 34 2c 20 35 2c 20 36  1, 2, 3, 4, 5, 6
176b8 2c 20 38 2c 20 6f 72 20 39 20 2d 20 74 68 65 20  , 8, or 9 - the 
176b9 69 6e 74 65 67 65 72 20 73 65 72 69 61 6c 20 74  integer serial t
176ba 79 70 65 73 2e 0a 2a 2a 20 54 68 65 20 73 65 72  ypes..** The ser
176bb 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65 20  ial type of the 
176bc 66 69 6e 61 6c 20 72 6f 77 69 64 20 77 69 6c 6c  final rowid will
176bd 20 61 6c 77 61 79 73 20 62 65 20 61 20 73 69 6e   always be a sin
176be 67 6c 65 20 62 79 74 65 2e 0a 2a 2a 20 42 79 20  gle byte..** By 
176bf 69 67 6e 6f 72 69 6e 67 20 74 68 69 73 20 6c 61  ignoring this la
176c0 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 68  st byte of the h
176c1 65 61 64 65 72 2c 20 77 65 20 66 6f 72 63 65 20  eader, we force 
176c2 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 0a 2a  the comparison.*
176c3 2a 20 74 6f 20 69 67 6e 6f 72 65 20 74 68 65 20  * to ignore the 
176c4 72 6f 77 69 64 20 61 74 20 74 68 65 20 65 6e 64  rowid at the end
176c5 20 6f 66 20 6b 65 79 31 2e 0a 2a 2f 0a 53 51 4c   of key1..*/.SQL
176c6 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
176c7 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72  sqlite3VdbeRecor
176c8 64 43 6f 6d 70 61 72 65 28 0a 20 20 69 6e 74 20  dCompare(.  int 
176c9 6e 4b 65 79 31 2c 20 63 6f 6e 73 74 20 76 6f 69  nKey1, const voi
176ca 64 20 2a 70 4b 65 79 31 2c 20 2f 2a 20 4c 65 66  d *pKey1, /* Lef
176cb 74 20 6b 65 79 20 2a 2f 0a 20 20 55 6e 70 61 63  t key */.  Unpac
176cc 6b 65 64 52 65 63 6f 72 64 20 2a 70 50 4b 65 79  kedRecord *pPKey
176cd 32 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68  2        /* Righ
176ce 74 20 6b 65 79 20 2a 2f 0a 29 7b 0a 20 20 75 33  t key */.){.  u3
176cf 32 20 64 31 3b 20 20 20 20 20 20 20 20 20 20 20  2 d1;           
176d0 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20   /* Offset into 
176d1 61 4b 65 79 5b 5d 20 6f 66 20 6e 65 78 74 20 64  aKey[] of next d
176d2 61 74 61 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20  ata element */. 
176d3 20 75 33 32 20 69 64 78 31 3b 20 20 20 20 20 20   u32 idx1;      
176d4 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e      /* Offset in
176d5 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20 6e 65 78  to aKey[] of nex
176d6 74 20 68 65 61 64 65 72 20 65 6c 65 6d 65 6e 74  t header element
176d7 20 2a 2f 0a 20 20 75 33 32 20 73 7a 48 64 72 31   */.  u32 szHdr1
176d8 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ;        /* Numb
176d9 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 68  er of bytes in h
176da 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69  eader */.  int i
176db 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 46 69 65   = 0;.  int nFie
176dc 6c 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30  ld;.  int rc = 0
176dd 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ;.  const unsign
176de 65 64 20 63 68 61 72 20 2a 61 4b 65 79 31 20 3d  ed char *aKey1 =
176df 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64   (const unsigned
176e0 20 63 68 61 72 20 2a 29 70 4b 65 79 31 3b 0a 20   char *)pKey1;. 
176e1 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
176e2 66 6f 3b 0a 20 20 4d 65 6d 20 6d 65 6d 31 3b 0a  fo;.  Mem mem1;.
176e3 0a 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 70 50  .  pKeyInfo = pP
176e4 4b 65 79 32 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a  Key2->pKeyInfo;.
176e5 20 20 6d 65 6d 31 2e 65 6e 63 20 3d 20 70 4b 65    mem1.enc = pKe
176e6 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a 20 20 6d 65  yInfo->enc;.  me
176e7 6d 31 2e 64 62 20 3d 20 70 4b 65 79 49 6e 66 6f  m1.db = pKeyInfo
176e8 2d 3e 64 62 3b 0a 20 20 6d 65 6d 31 2e 66 6c 61  ->db;.  mem1.fla
176e9 67 73 20 3d 20 30 3b 0a 20 20 6d 65 6d 31 2e 7a  gs = 0;.  mem1.z
176ea 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 0a 20  Malloc = 0;.  . 
176eb 20 69 64 78 31 20 3d 20 67 65 74 56 61 72 69 6e   idx1 = getVarin
176ec 74 33 32 28 61 4b 65 79 31 2c 20 73 7a 48 64 72  t32(aKey1, szHdr
176ed 31 29 3b 0a 20 20 64 31 20 3d 20 73 7a 48 64 72  1);.  d1 = szHdr
176ee 31 3b 0a 20 20 69 66 28 20 70 50 4b 65 79 32 2d  1;.  if( pPKey2-
176ef 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45  >flags & UNPACKE
176f0 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 20 29  D_IGNORE_ROWID )
176f1 7b 0a 20 20 20 20 73 7a 48 64 72 31 2d 2d 3b 0a  {.    szHdr1--;.
176f2 20 20 7d 0a 20 20 6e 46 69 65 6c 64 20 3d 20 70    }.  nField = p
176f3 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 3b  KeyInfo->nField;
176f4 0a 20 20 77 68 69 6c 65 28 20 69 64 78 31 3c 73  .  while( idx1<s
176f5 7a 48 64 72 31 20 26 26 20 69 3c 70 50 4b 65 79  zHdr1 && i<pPKey
176f6 32 2d 3e 6e 46 69 65 6c 64 20 29 7b 0a 20 20 20  2->nField ){.   
176f7 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65   u32 serial_type
176f8 31 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20  1;..    /* Read 
176f9 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 73  the serial types
176fa 20 66 6f 72 20 74 68 65 20 6e 65 78 74 20 65 6c   for the next el
176fb 65 6d 65 6e 74 20 69 6e 20 65 61 63 68 20 6b 65  ement in each ke
176fc 79 2e 20 2a 2f 0a 20 20 20 20 69 64 78 31 20 2b  y. */.    idx1 +
176fd 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 20 61  = getVarint32( a
176fe 4b 65 79 31 2b 69 64 78 31 2c 20 73 65 72 69 61  Key1+idx1, seria
176ff 6c 5f 74 79 70 65 31 20 29 3b 0a 20 20 20 20 69  l_type1 );.    i
17700 66 28 20 64 31 3e 3d 6e 4b 65 79 31 20 26 26 20  f( d1>=nKey1 && 
17701 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
17702 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f  lTypeLen(serial_
17703 74 79 70 65 31 29 3e 30 20 29 20 62 72 65 61 6b  type1)>0 ) break
17704 3b 0a 0a 20 20 20 20 2f 2a 20 45 78 74 72 61 63  ;..    /* Extrac
17705 74 20 74 68 65 20 76 61 6c 75 65 73 20 74 6f 20  t the values to 
17706 62 65 20 63 6f 6d 70 61 72 65 64 2e 0a 20 20 20  be compared..   
17707 20 2a 2f 0a 20 20 20 20 64 31 20 2b 3d 20 73 71   */.    d1 += sq
17708 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47  lite3VdbeSerialG
17709 65 74 28 26 61 4b 65 79 31 5b 64 31 5d 2c 20 73  et(&aKey1[d1], s
1770a 65 72 69 61 6c 5f 74 79 70 65 31 2c 20 26 6d 65  erial_type1, &me
1770b 6d 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 20  m1);..    /* Do 
1770c 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 0a 20  the comparison. 
1770d 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73     */.    rc = s
1770e 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65  qlite3MemCompare
1770f 28 26 6d 65 6d 31 2c 20 26 70 50 4b 65 79 32 2d  (&mem1, &pPKey2-
17710 3e 61 4d 65 6d 5b 69 5d 2c 0a 20 20 20 20 20 20  >aMem[i],.      
17711 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17712 20 20 20 20 20 69 3c 6e 46 69 65 6c 64 20 3f 20       i<nField ? 
17713 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b  pKeyInfo->aColl[
17714 69 5d 20 3a 20 30 29 3b 0a 20 20 20 20 69 66 28  i] : 0);.    if(
17715 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20   rc!=0 ){.      
17716 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
17717 20 69 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20   i++;.  }.  if( 
17718 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 20 29 20 73  mem1.zMalloc ) s
17719 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1771a 65 61 73 65 28 26 6d 65 6d 31 29 3b 0a 0a 20 20  ease(&mem1);..  
1771b 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20  if( rc==0 ){.   
1771c 20 2f 2a 20 72 63 3d 3d 30 20 68 65 72 65 20 6d   /* rc==0 here m
1771d 65 61 6e 73 20 74 68 61 74 20 6f 6e 65 20 6f 66  eans that one of
1771e 20 74 68 65 20 6b 65 79 73 20 72 61 6e 20 6f 75   the keys ran ou
1771f 74 20 6f 66 20 66 69 65 6c 64 73 20 61 6e 64 0a  t of fields and.
17720 20 20 20 20 2a 2a 20 61 6c 6c 20 74 68 65 20 66      ** all the f
17721 69 65 6c 64 73 20 75 70 20 74 6f 20 74 68 61 74  ields up to that
17722 20 70 6f 69 6e 74 20 77 65 72 65 20 65 71 75 61   point were equa
17723 6c 2e 20 49 66 20 74 68 65 20 55 4e 50 41 43 4b  l. If the UNPACK
17724 45 44 5f 49 4e 43 52 4b 45 59 0a 20 20 20 20 2a  ED_INCRKEY.    *
17725 2a 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74  * flag is set, t
17726 68 65 6e 20 62 72 65 61 6b 20 74 68 65 20 74 69  hen break the ti
17727 65 20 62 79 20 74 72 65 61 74 69 6e 67 20 6b 65  e by treating ke
17728 79 32 20 61 73 20 6c 61 72 67 65 72 2e 0a 20 20  y2 as larger..  
17729 20 20 2a 2a 20 49 66 20 74 68 65 20 55 50 41 43    ** If the UPAC
1772a 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48  KED_PREFIX_MATCH
1772b 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68   flag is set, th
1772c 65 6e 20 6b 65 79 73 20 77 69 74 68 20 63 6f 6d  en keys with com
1772d 6d 6f 6e 20 70 72 65 66 69 78 65 73 0a 20 20 20  mon prefixes.   
1772e 20 2a 2a 20 61 72 65 20 63 6f 6e 73 69 64 65 72   ** are consider
1772f 65 64 20 74 6f 20 62 65 20 65 71 75 61 6c 2e 20  ed to be equal. 
17730 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20   Otherwise, the 
17731 6c 6f 6e 67 65 72 20 6b 65 79 20 69 73 20 74 68  longer key is th
17732 65 20 0a 20 20 20 20 2a 2a 20 6c 61 72 67 65 72  e .    ** larger
17733 2e 20 20 41 73 20 69 74 20 68 61 70 70 65 6e 73  .  As it happens
17734 2c 20 74 68 65 20 70 50 4b 65 79 32 20 77 69 6c  , the pPKey2 wil
17735 6c 20 61 6c 77 61 79 73 20 62 65 20 74 68 65 20  l always be the 
17736 6c 6f 6e 67 65 72 0a 20 20 20 20 2a 2a 20 69 66  longer.    ** if
17737 20 74 68 65 72 65 20 69 73 20 61 20 64 69 66 66   there is a diff
17738 65 72 65 6e 63 65 2e 0a 20 20 20 20 2a 2f 0a 20  erence..    */. 
17739 20 20 20 69 66 28 20 70 50 4b 65 79 32 2d 3e 66     if( pPKey2->f
1773a 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f  lags & UNPACKED_
1773b 49 4e 43 52 4b 45 59 20 29 7b 0a 20 20 20 20 20  INCRKEY ){.     
1773c 20 72 63 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 65   rc = -1;.    }e
1773d 6c 73 65 20 69 66 28 20 70 50 4b 65 79 32 2d 3e  lse if( pPKey2->
1773e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44  flags & UNPACKED
1773f 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 20 29 7b  _PREFIX_MATCH ){
17740 0a 20 20 20 20 20 20 2f 2a 20 4c 65 61 76 65 20  .      /* Leave 
17741 72 63 3d 3d 30 20 2a 2f 0a 20 20 20 20 7d 65 6c  rc==0 */.    }el
17742 73 65 20 69 66 28 20 69 64 78 31 3c 73 7a 48 64  se if( idx1<szHd
17743 72 31 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  r1 ){.      rc =
17744 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73   1;.    }.  }els
17745 65 20 69 66 28 20 70 4b 65 79 49 6e 66 6f 2d 3e  e if( pKeyInfo->
17746 61 53 6f 72 74 4f 72 64 65 72 20 26 26 20 69 3c  aSortOrder && i<
17747 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64  pKeyInfo->nField
17748 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
17749 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f  && pKeyInfo->aSo
1774a 72 74 4f 72 64 65 72 5b 69 5d 20 29 7b 0a 20 20  rtOrder[i] ){.  
1774b 20 20 72 63 20 3d 20 2d 72 63 3b 0a 20 20 7d 0a    rc = -rc;.  }.
1774c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1774d 20 0a 0a 2f 2a 0a 2a 2a 20 70 43 75 72 20 70 6f   ../*.** pCur po
1774e 69 6e 74 73 20 61 74 20 61 6e 20 69 6e 64 65 78  ints at an index
1774f 20 65 6e 74 72 79 20 63 72 65 61 74 65 64 20 75   entry created u
17750 73 69 6e 67 20 74 68 65 20 4f 50 5f 4d 61 6b 65  sing the OP_Make
17751 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e 0a 2a  Record opcode..*
17752 2a 20 52 65 61 64 20 74 68 65 20 72 6f 77 69 64  * Read the rowid
17753 20 28 74 68 65 20 6c 61 73 74 20 66 69 65 6c 64   (the last field
17754 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 29 20   in the record) 
17755 61 6e 64 20 73 74 6f 72 65 20 69 74 20 69 6e 20  and store it in 
17756 2a 72 6f 77 69 64 2e 0a 2a 2a 20 52 65 74 75 72  *rowid..** Retur
17757 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65  n SQLITE_OK if e
17758 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b 73 2c  verything works,
17759 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64   or an error cod
1775a 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a  e otherwise..*/.
1775b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1775c 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 49 64  nt sqlite3VdbeId
1775d 78 52 6f 77 69 64 28 42 74 43 75 72 73 6f 72 20  xRowid(BtCursor 
1775e 2a 70 43 75 72 2c 20 69 36 34 20 2a 72 6f 77 69  *pCur, i64 *rowi
1775f 64 29 7b 0a 20 20 69 36 34 20 6e 43 65 6c 6c 4b  d){.  i64 nCellK
17760 65 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63  ey = 0;.  int rc
17761 3b 0a 20 20 75 33 32 20 73 7a 48 64 72 3b 20 20  ;.  u32 szHdr;  
17762 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
17763 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20   the header */. 
17764 20 75 33 32 20 74 79 70 65 52 6f 77 69 64 3b 20   u32 typeRowid; 
17765 20 20 20 2f 2a 20 53 65 72 69 61 6c 20 74 79 70     /* Serial typ
17766 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20 2a  e of the rowid *
17767 2f 0a 20 20 75 33 32 20 6c 65 6e 52 6f 77 69 64  /.  u32 lenRowid
17768 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66  ;     /* Size of
17769 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20   the rowid */.  
1776a 4d 65 6d 20 6d 2c 20 76 3b 0a 0a 20 20 73 71 6c  Mem m, v;..  sql
1776b 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65  ite3BtreeKeySize
1776c 28 70 43 75 72 2c 20 26 6e 43 65 6c 6c 4b 65 79  (pCur, &nCellKey
1776d 29 3b 0a 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65  );.  if( nCellKe
1776e 79 3c 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  y<=0 ){.    retu
1776f 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
17770 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 6d 2e  T_BKPT;.  }.  m.
17771 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 6d 2e 64  flags = 0;.  m.d
17772 62 20 3d 20 30 3b 0a 20 20 6d 2e 7a 4d 61 6c 6c  b = 0;.  m.zMall
17773 6f 63 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 73  oc = 0;.  rc = s
17774 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f  qlite3VdbeMemFro
17775 6d 42 74 72 65 65 28 70 43 75 72 2c 20 30 2c 20  mBtree(pCur, 0, 
17776 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d 29  nCellKey, 1, &m)
17777 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20  ;.  if( rc ){.  
17778 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
17779 0a 20 20 28 76 6f 69 64 29 67 65 74 56 61 72 69  .  (void)getVari
1777a 6e 74 33 32 28 28 75 38 2a 29 6d 2e 7a 2c 20 73  nt32((u8*)m.z, s
1777b 7a 48 64 72 29 3b 0a 20 20 28 76 6f 69 64 29 67  zHdr);.  (void)g
1777c 65 74 56 61 72 69 6e 74 33 32 28 28 75 38 2a 29  etVarint32((u8*)
1777d 26 6d 2e 7a 5b 73 7a 48 64 72 2d 31 5d 2c 20 74  &m.z[szHdr-1], t
1777e 79 70 65 52 6f 77 69 64 29 3b 0a 20 20 6c 65 6e  ypeRowid);.  len
1777f 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 56  Rowid = sqlite3V
17780 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e  dbeSerialTypeLen
17781 28 74 79 70 65 52 6f 77 69 64 29 3b 0a 20 20 73  (typeRowid);.  s
17782 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
17783 47 65 74 28 28 75 38 2a 29 26 6d 2e 7a 5b 6d 2e  Get((u8*)&m.z[m.
17784 6e 2d 6c 65 6e 52 6f 77 69 64 5d 2c 20 74 79 70  n-lenRowid], typ
17785 65 52 6f 77 69 64 2c 20 26 76 29 3b 0a 20 20 2a  eRowid, &v);.  *
17786 72 6f 77 69 64 20 3d 20 76 2e 75 2e 69 3b 0a 20  rowid = v.u.i;. 
17787 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
17788 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 65  elease(&m);.  re
17789 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1778a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65  }../*.** Compare
1778b 20 74 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20   the key of the 
1778c 69 6e 64 65 78 20 65 6e 74 72 79 20 74 68 61 74  index entry that
1778d 20 63 75 72 73 6f 72 20 70 43 20 69 73 20 70 6f   cursor pC is po
1778e 69 6e 74 20 74 6f 20 61 67 61 69 6e 73 74 0a 2a  int to against.*
1778f 2a 20 74 68 65 20 6b 65 79 20 73 74 72 69 6e 67  * the key string
17790 20 69 6e 20 70 4b 65 79 20 28 6f 66 20 6c 65 6e   in pKey (of len
17791 67 74 68 20 6e 4b 65 79 29 2e 20 20 57 72 69 74  gth nKey).  Writ
17792 65 20 69 6e 74 6f 20 2a 70 52 65 73 20 61 20 6e  e into *pRes a n
17793 75 6d 62 65 72 0a 2a 2a 20 74 68 61 74 20 69 73  umber.** that is
17794 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 2c   negative, zero,
17795 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66 20   or positive if 
17796 70 43 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c  pC is less than,
17797 20 65 71 75 61 6c 20 74 6f 2c 0a 2a 2a 20 6f 72   equal to,.** or
17798 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 70 4b   greater than pK
17799 65 79 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49  ey.  Return SQLI
1779a 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73  TE_OK on success
1779b 2e 0a 2a 2a 0a 2a 2a 20 70 4b 65 79 20 69 73 20  ..**.** pKey is 
1779c 65 69 74 68 65 72 20 63 72 65 61 74 65 64 20 77  either created w
1779d 69 74 68 6f 75 74 20 61 20 72 6f 77 69 64 20 6f  ithout a rowid o
1779e 72 20 69 73 20 74 72 75 6e 63 61 74 65 64 20 73  r is truncated s
1779f 6f 20 74 68 61 74 20 69 74 0a 2a 2a 20 6f 6d 69  o that it.** omi
177a0 74 73 20 74 68 65 20 72 6f 77 69 64 20 61 74 20  ts the rowid at 
177a1 74 68 65 20 65 6e 64 2e 20 20 54 68 65 20 72 6f  the end.  The ro
177a2 77 69 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f  wid at the end o
177a3 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72  f the index entr
177a4 79 0a 2a 2a 20 69 73 20 69 67 6e 6f 72 65 64 20  y.** is ignored 
177a5 61 73 20 77 65 6c 6c 2e 20 20 48 65 6e 63 65 2c  as well.  Hence,
177a6 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e   this routine on
177a7 6c 79 20 63 6f 6d 70 61 72 65 73 20 74 68 65 20  ly compares the 
177a8 70 72 65 66 69 78 65 73 20 0a 2a 2a 20 6f 66 20  prefixes .** of 
177a9 74 68 65 20 6b 65 79 73 20 70 72 69 6f 72 20 74  the keys prior t
177aa 6f 20 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 69  o the final rowi
177ab 64 2c 20 6e 6f 74 20 74 68 65 20 65 6e 74 69 72  d, not the entir
177ac 65 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 70 55 6e  e key..**.** pUn
177ad 70 61 63 6b 65 64 20 6d 61 79 20 62 65 20 61 6e  packed may be an
177ae 20 75 6e 70 61 63 6b 65 64 20 76 65 72 73 69 6f   unpacked versio
177af 6e 20 6f 66 20 70 4b 65 79 2c 6e 4b 65 79 2e 20  n of pKey,nKey. 
177b0 20 49 66 20 70 55 6e 70 61 63 6b 65 64 20 69 73   If pUnpacked is
177b1 0a 2a 2a 20 73 75 70 70 6c 69 65 64 20 69 74 20  .** supplied it 
177b2 69 73 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65  is used in place
177b3 20 6f 66 20 70 4b 65 79 2c 6e 4b 65 79 2e 0a 2a   of pKey,nKey..*
177b4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
177b5 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
177b6 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 0a 20  IdxKeyCompare(. 
177b7 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 2c   VdbeCursor *pC,
177b8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
177b9 54 68 65 20 63 75 72 73 6f 72 20 74 6f 20 63 6f  The cursor to co
177ba 6d 70 61 72 65 20 61 67 61 69 6e 73 74 20 2a 2f  mpare against */
177bb 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  .  UnpackedRecor
177bc 64 20 2a 70 55 6e 70 61 63 6b 65 64 2c 20 20 2f  d *pUnpacked,  /
177bd 2a 20 55 6e 70 61 63 6b 65 64 20 76 65 72 73 69  * Unpacked versi
177be 6f 6e 20 6f 66 20 70 4b 65 79 20 61 6e 64 20 6e  on of pKey and n
177bf 4b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 2a 72 65  Key */.  int *re
177c0 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s               
177c1 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68       /* Write th
177c2 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 72 65 73  e comparison res
177c3 75 6c 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  ult here */.){. 
177c4 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 20 3d 20   i64 nCellKey = 
177c5 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42  0;.  int rc;.  B
177c6 74 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20  tCursor *pCur = 
177c7 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 4d  pC->pCursor;.  M
177c8 65 6d 20 6d 3b 0a 0a 20 20 73 71 6c 69 74 65 33  em m;..  sqlite3
177c9 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75  BtreeKeySize(pCu
177ca 72 2c 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20  r, &nCellKey);. 
177cb 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 3d 30   if( nCellKey<=0
177cc 20 29 7b 0a 20 20 20 20 2a 72 65 73 20 3d 20 30   ){.    *res = 0
177cd 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
177ce 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 6d 2e  ITE_OK;.  }.  m.
177cf 64 62 20 3d 20 30 3b 0a 20 20 6d 2e 66 6c 61 67  db = 0;.  m.flag
177d0 73 20 3d 20 30 3b 0a 20 20 6d 2e 7a 4d 61 6c 6c  s = 0;.  m.zMall
177d1 6f 63 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 73  oc = 0;.  rc = s
177d2 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f  qlite3VdbeMemFro
177d3 6d 42 74 72 65 65 28 70 43 2d 3e 70 43 75 72 73  mBtree(pC->pCurs
177d4 6f 72 2c 20 30 2c 20 6e 43 65 6c 6c 4b 65 79 2c  or, 0, nCellKey,
177d5 20 31 2c 20 26 6d 29 3b 0a 20 20 69 66 28 20 72   1, &m);.  if( r
177d6 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  c ){.    return 
177d7 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  rc;.  }.  assert
177d8 28 20 70 55 6e 70 61 63 6b 65 64 2d 3e 66 6c 61  ( pUnpacked->fla
177d9 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 49 47  gs & UNPACKED_IG
177da 4e 4f 52 45 5f 52 4f 57 49 44 20 29 3b 0a 20 20  NORE_ROWID );.  
177db 2a 72 65 73 20 3d 20 73 71 6c 69 74 65 33 56 64  *res = sqlite3Vd
177dc 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28  beRecordCompare(
177dd 6d 2e 6e 2c 20 6d 2e 7a 2c 20 70 55 6e 70 61 63  m.n, m.z, pUnpac
177de 6b 65 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  ked);.  sqlite3V
177df 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d  dbeMemRelease(&m
177e0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
177e1 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
177e2 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74  This routine set
177e3 73 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 62  s the value to b
177e4 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 75  e returned by su
177e5 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74  bsequent calls t
177e6 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 68 61  o.** sqlite3_cha
177e7 6e 67 65 73 28 29 20 6f 6e 20 74 68 65 20 64 61  nges() on the da
177e8 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 27 64  tabase handle 'd
177e9 62 27 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  b'. .*/.SQLITE_P
177ea 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
177eb 74 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 65  te3VdbeSetChange
177ec 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  s(sqlite3 *db, i
177ed 6e 74 20 6e 43 68 61 6e 67 65 29 7b 0a 20 20 61  nt nChange){.  a
177ee 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
177ef 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75  utex_held(db->mu
177f0 74 65 78 29 20 29 3b 0a 20 20 64 62 2d 3e 6e 43  tex) );.  db->nC
177f1 68 61 6e 67 65 20 3d 20 6e 43 68 61 6e 67 65 3b  hange = nChange;
177f2 0a 20 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 68 61  .  db->nTotalCha
177f3 6e 67 65 20 2b 3d 20 6e 43 68 61 6e 67 65 3b 0a  nge += nChange;.
177f4 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 61 20 66  }../*.** Set a f
177f5 6c 61 67 20 69 6e 20 74 68 65 20 76 64 62 65 20  lag in the vdbe 
177f6 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 63 68  to update the ch
177f7 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 77 68 65  ange counter whe
177f8 6e 20 69 74 20 69 73 20 66 69 6e 61 6c 69 73 65  n it is finalise
177f9 64 0a 2a 2a 20 6f 72 20 72 65 73 65 74 2e 0a 2a  d.** or reset..*
177fa 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
177fb 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
177fc 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 56 64  eCountChanges(Vd
177fd 62 65 20 2a 76 29 7b 0a 20 20 76 2d 3e 63 68 61  be *v){.  v->cha
177fe 6e 67 65 43 6e 74 4f 6e 20 3d 20 31 3b 0a 7d 0a  ngeCntOn = 1;.}.
177ff 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 65 76 65 72  ./*.** Mark ever
17800 79 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  y prepared state
17801 6d 65 6e 74 20 61 73 73 6f 63 69 61 74 65 64 20  ment associated 
17802 77 69 74 68 20 61 20 64 61 74 61 62 61 73 65 20  with a database 
17803 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 61 73  connection.** as
17804 20 65 78 70 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20   expired..**.** 
17805 41 6e 20 65 78 70 69 72 65 64 20 73 74 61 74 65  An expired state
17806 6d 65 6e 74 20 6d 65 61 6e 73 20 74 68 61 74 20  ment means that 
17807 72 65 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 66  recompilation of
17808 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69   the statement i
17809 73 0a 2a 2a 20 72 65 63 6f 6d 6d 65 6e 64 2e 20  s.** recommend. 
1780a 20 53 74 61 74 65 6d 65 6e 74 73 20 65 78 70 69   Statements expi
1780b 72 65 20 77 68 65 6e 20 74 68 69 6e 67 73 20 68  re when things h
1780c 61 70 70 65 6e 20 74 68 61 74 20 6d 61 6b 65 20  appen that make 
1780d 74 68 65 69 72 0a 2a 2a 20 70 72 6f 67 72 61 6d  their.** program
1780e 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 52 65 6d  s obsolete.  Rem
1780f 6f 76 69 6e 67 20 75 73 65 72 2d 64 65 66 69 6e  oving user-defin
17810 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20  ed functions or 
17811 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 71  collating.** seq
17812 75 65 6e 63 65 73 2c 20 6f 72 20 63 68 61 6e 67  uences, or chang
17813 69 6e 67 20 61 6e 20 61 75 74 68 6f 72 69 7a 61  ing an authoriza
17814 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 61 72  tion function ar
17815 65 20 74 68 65 20 74 79 70 65 73 20 6f 66 0a 2a  e the types of.*
17816 2a 20 74 68 69 6e 67 73 20 74 68 61 74 20 6d 61  * things that ma
17817 6b 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74  ke prepared stat
17818 65 6d 65 6e 74 73 20 6f 62 73 6f 6c 65 74 65 2e  ements obsolete.
17819 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1781a 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
1781b 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61  xpirePreparedSta
1781c 74 65 6d 65 6e 74 73 28 73 71 6c 69 74 65 33 20  tements(sqlite3 
1781d 2a 64 62 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b  *db){.  Vdbe *p;
1781e 0a 20 20 66 6f 72 28 70 20 3d 20 64 62 2d 3e 70  .  for(p = db->p
1781f 56 64 62 65 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e  Vdbe; p; p=p->pN
17820 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 65 78 70  ext){.    p->exp
17821 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a  ired = 1;.  }.}.
17822 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
17823 65 20 64 61 74 61 62 61 73 65 20 61 73 73 6f 63  e database assoc
17824 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 56  iated with the V
17825 64 62 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  dbe..*/.SQLITE_P
17826 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 20 2a  RIVATE sqlite3 *
17827 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 56 64  sqlite3VdbeDb(Vd
17828 62 65 20 2a 76 29 7b 0a 20 20 72 65 74 75 72 6e  be *v){.  return
17829 20 76 2d 3e 64 62 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a   v->db;.}../****
1782a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
1782b 66 20 76 64 62 65 61 75 78 2e 63 20 2a 2a 2a 2a  f vdbeaux.c ****
1782c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1782d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1782e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
1782f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
17830 20 66 69 6c 65 20 76 64 62 65 61 70 69 2e 63 20   file vdbeapi.c 
17831 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17832 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17833 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
17834 20 32 30 30 34 20 4d 61 79 20 32 36 0a 2a 2a 0a   2004 May 26.**.
17835 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
17836 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
17837 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
17838 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
17839 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
1783a 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
1783b 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
1783c 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
1783d 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
1783e 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1783f 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
17840 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
17841 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
17842 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
17843 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
17844 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
17845 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
17846 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
17847 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17848 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17849 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1784a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
1784b 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
1784c 61 69 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f  ains code use to
1784d 20 69 6d 70 6c 65 6d 65 6e 74 20 41 50 49 73 20   implement APIs 
1784e 74 68 61 74 20 61 72 65 20 70 61 72 74 20 6f 66  that are part of
1784f 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e 0a 2a 2a   the.** VDBE..**
17850 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 61 70 69  .** $Id: vdbeapi
17851 2e 63 2c 76 20 31 2e 31 34 38 20 32 30 30 38 2f  .c,v 1.148 2008/
17852 31 31 2f 30 35 20 31 36 3a 33 37 3a 33 35 20 64  11/05 16:37:35 d
17853 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66  rh Exp $.*/..#if
17854 20 30 20 26 26 20 64 65 66 69 6e 65 64 28 53 51   0 && defined(SQ
17855 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f  LITE_ENABLE_MEMO
17856 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 29 0a 2f  RY_MANAGEMENT)./
17857 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
17858 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e  ng structure con
17859 74 61 69 6e 73 20 70 6f 69 6e 74 65 72 73 20 74  tains pointers t
1785a 6f 20 74 68 65 20 65 6e 64 20 70 6f 69 6e 74 73  o the end points
1785b 20 6f 66 20 61 0a 2a 2a 20 64 6f 75 62 6c 79 2d   of a.** doubly-
1785c 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61  linked list of a
1785d 6c 6c 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20  ll compiled SQL 
1785e 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20  statements that 
1785f 6d 61 79 20 62 65 20 68 6f 6c 64 69 6e 67 0a 2a  may be holding.*
17860 2a 20 62 75 66 66 65 72 73 20 65 6c 69 67 69 62  * buffers eligib
17861 6c 65 20 66 6f 72 20 72 65 6c 65 61 73 65 20 77  le for release w
17862 68 65 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f  hen the sqlite3_
17863 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29  release_memory()
17864 20 69 6e 74 65 72 66 61 63 65 20 69 73 0a 2a 2a   interface is.**
17865 20 69 6e 76 6f 6b 65 64 2e 20 41 63 63 65 73 73   invoked. Access
17866 20 74 6f 20 74 68 69 73 20 6c 69 73 74 20 69 73   to this list is
17867 20 70 72 6f 74 65 63 74 65 64 20 62 79 20 74 68   protected by th
17868 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  e SQLITE_MUTEX_S
17869 54 41 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 6d 75  TATIC_LRU2.** mu
1786a 74 65 78 2e 0a 2a 2a 0a 2a 2a 20 53 74 61 74 65  tex..**.** State
1786b 6d 65 6e 74 73 20 61 72 65 20 61 64 64 65 64 20  ments are added 
1786c 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  to the end of th
1786d 69 73 20 6c 69 73 74 20 77 68 65 6e 20 73 71 6c  is list when sql
1786e 69 74 65 33 5f 72 65 73 65 74 28 29 20 69 73 0a  ite3_reset() is.
1786f 2a 2a 20 63 61 6c 6c 65 64 2e 20 54 68 65 79 20  ** called. They 
17870 61 72 65 20 72 65 6d 6f 76 65 64 20 65 69 74 68  are removed eith
17871 65 72 20 77 68 65 6e 20 73 71 6c 69 74 65 33 5f  er when sqlite3_
17872 73 74 65 70 28 29 20 6f 72 20 73 71 6c 69 74 65  step() or sqlite
17873 33 5f 66 69 6e 61 6c 69 7a 65 28 29 0a 2a 2a 20  3_finalize().** 
17874 69 73 20 63 61 6c 6c 65 64 2e 20 57 68 65 6e 20  is called. When 
17875 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 61  statements are a
17876 64 64 65 64 20 74 6f 20 74 68 69 73 20 6c 69 73  dded to this lis
17877 74 2c 20 74 68 65 20 61 73 73 6f 63 69 61 74 65  t, the associate
17878 64 20 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 61  d .** register a
17879 72 72 61 79 20 28 70 2d 3e 61 4d 65 6d 5b 31 2e  rray (p->aMem[1.
1787a 2e 70 2d 3e 6e 4d 65 6d 5d 29 20 6d 61 79 20 63  .p->nMem]) may c
1787b 6f 6e 74 61 69 6e 20 64 79 6e 61 6d 69 63 20 62  ontain dynamic b
1787c 75 66 66 65 72 73 20 74 68 61 74 0a 2a 2a 20 63  uffers that.** c
1787d 61 6e 20 62 65 20 66 72 65 65 64 20 75 73 69 6e  an be freed usin
1787e 67 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 6c  g sqlite3VdbeRel
1787f 65 61 73 65 4d 65 6d 6f 72 79 28 29 2e 0a 2a 2a  easeMemory()..**
17880 0a 2a 2a 20 57 68 65 6e 20 73 74 61 74 65 6d 65  .** When stateme
17881 6e 74 73 20 61 72 65 20 61 64 64 65 64 20 6f 72  nts are added or
17882 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68   removed from th
17883 69 73 20 6c 69 73 74 2c 20 74 68 65 20 6d 75 74  is list, the mut
17884 65 78 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64  ex.** associated
17885 20 77 69 74 68 20 74 68 65 20 56 64 62 65 20 62   with the Vdbe b
17886 65 69 6e 67 20 61 64 64 65 64 20 6f 72 20 72 65  eing added or re
17887 6d 6f 76 65 64 20 28 56 64 62 65 2e 64 62 2d 3e  moved (Vdbe.db->
17888 6d 75 74 65 78 29 20 69 73 0a 2a 2a 20 61 6c 72  mutex) is.** alr
17889 65 61 64 79 20 68 65 6c 64 2e 20 54 68 65 20 4c  eady held. The L
1788a 52 55 32 20 6d 75 74 65 78 20 69 73 20 74 68 65  RU2 mutex is the
1788b 6e 20 6f 62 74 61 69 6e 65 64 2c 20 62 6c 6f 63  n obtained, bloc
1788c 6b 69 6e 67 20 69 66 20 6e 65 63 65 73 73 61 72  king if necessar
1788d 79 2c 0a 2a 2a 20 74 68 65 20 6c 69 6e 6b 65 64  y,.** the linked
1788e 2d 6c 69 73 74 20 70 6f 69 6e 74 65 72 73 20 6d  -list pointers m
1788f 61 6e 69 70 75 6c 61 74 65 64 20 61 6e 64 20 74  anipulated and t
17890 68 65 20 4c 52 55 32 20 6d 75 74 65 78 20 72 65  he LRU2 mutex re
17891 6c 69 6e 71 75 69 73 68 65 64 2e 0a 2a 2f 0a 73  linquished..*/.s
17892 74 72 75 63 74 20 53 74 61 74 65 6d 65 6e 74 4c  truct StatementL
17893 72 75 4c 69 73 74 20 7b 0a 20 20 56 64 62 65 20  ruList {.  Vdbe 
17894 2a 70 46 69 72 73 74 3b 0a 20 20 56 64 62 65 20  *pFirst;.  Vdbe 
17895 2a 70 4c 61 73 74 3b 0a 7d 3b 0a 73 74 61 74 69  *pLast;.};.stati
17896 63 20 73 74 72 75 63 74 20 53 74 61 74 65 6d 65  c struct Stateme
17897 6e 74 4c 72 75 4c 69 73 74 20 73 71 6c 69 74 65  ntLruList sqlite
17898 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 3b 0a  3LruStatements;.
17899 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61  ./*.** Check tha
1789a 74 20 74 68 65 20 6c 69 73 74 20 6c 6f 6f 6b 73  t the list looks
1789b 20 74 6f 20 62 65 20 69 6e 74 65 72 6e 61 6c 6c   to be internall
1789c 79 20 63 6f 6e 73 69 73 74 65 6e 74 2e 20 54 68  y consistent. Th
1789d 69 73 20 69 73 20 75 73 65 64 0a 2a 2a 20 61 73  is is used.** as
1789e 20 70 61 72 74 20 6f 66 20 61 6e 20 61 73 73 65   part of an asse
1789f 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 20 61  rt() statement a
178a0 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
178a1 20 20 20 61 73 73 65 72 74 28 20 73 74 6d 74 4c     assert( stmtL
178a2 72 75 43 68 65 63 6b 28 29 20 29 3b 0a 2a 2f 0a  ruCheck() );.*/.
178a3 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73  #ifndef NDEBUG.s
178a4 74 61 74 69 63 20 69 6e 74 20 73 74 6d 74 4c 72  tatic int stmtLr
178a5 75 43 68 65 63 6b 28 29 7b 0a 20 20 56 64 62 65  uCheck(){.  Vdbe
178a6 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 73 71 6c   *p;.  for(p=sql
178a7 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74  ite3LruStatement
178a8 73 2e 70 46 69 72 73 74 3b 20 70 3b 20 70 3d 70  s.pFirst; p; p=p
178a9 2d 3e 70 4c 72 75 4e 65 78 74 29 7b 0a 20 20 20  ->pLruNext){.   
178aa 20 61 73 73 65 72 74 28 70 2d 3e 70 4c 72 75 4e   assert(p->pLruN
178ab 65 78 74 20 7c 7c 20 70 3d 3d 73 71 6c 69 74 65  ext || p==sqlite
178ac 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70  3LruStatements.p
178ad 4c 61 73 74 29 3b 0a 20 20 20 20 61 73 73 65 72  Last);.    asser
178ae 74 28 21 70 2d 3e 70 4c 72 75 4e 65 78 74 20 7c  t(!p->pLruNext |
178af 7c 20 70 2d 3e 70 4c 72 75 4e 65 78 74 2d 3e 70  | p->pLruNext->p
178b0 4c 72 75 50 72 65 76 3d 3d 70 29 3b 0a 20 20 20  LruPrev==p);.   
178b1 20 61 73 73 65 72 74 28 70 2d 3e 70 4c 72 75 50   assert(p->pLruP
178b2 72 65 76 20 7c 7c 20 70 3d 3d 73 71 6c 69 74 65  rev || p==sqlite
178b3 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70  3LruStatements.p
178b4 46 69 72 73 74 29 3b 0a 20 20 20 20 61 73 73 65  First);.    asse
178b5 72 74 28 21 70 2d 3e 70 4c 72 75 50 72 65 76 20  rt(!p->pLruPrev 
178b6 7c 7c 20 70 2d 3e 70 4c 72 75 50 72 65 76 2d 3e  || p->pLruPrev->
178b7 70 4c 72 75 4e 65 78 74 3d 3d 70 29 3b 0a 20 20  pLruNext==p);.  
178b8 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  }.  return 1;.}.
178b9 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 64  #endif../*.** Ad
178ba 64 20 76 64 62 65 20 70 20 74 6f 20 74 68 65 20  d vdbe p to the 
178bb 65 6e 64 20 6f 66 20 74 68 65 20 73 74 61 74 65  end of the state
178bc 6d 65 6e 74 20 6c 72 75 20 6c 69 73 74 2e 20 49  ment lru list. I
178bd 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61  t is assumed tha
178be 74 0a 2a 2a 20 70 20 69 73 20 6e 6f 74 20 61 6c  t.** p is not al
178bf 72 65 61 64 79 20 70 61 72 74 20 6f 66 20 74 68  ready part of th
178c0 65 20 6c 69 73 74 20 77 68 65 6e 20 74 68 69 73  e list when this
178c1 20 69 73 20 63 61 6c 6c 65 64 2e 20 54 68 65 20   is called. The 
178c2 6c 72 75 20 6c 69 73 74 0a 2a 2a 20 69 73 20 70  lru list.** is p
178c3 72 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 20  rotected by the 
178c4 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
178c5 54 49 43 5f 4c 52 55 20 6d 75 74 65 78 2e 0a 2a  TIC_LRU mutex..*
178c6 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 74  /.static void st
178c7 6d 74 4c 72 75 41 64 64 28 56 64 62 65 20 2a 70  mtLruAdd(Vdbe *p
178c8 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ){.  sqlite3_mut
178c9 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33  ex_enter(sqlite3
178ca 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
178cb 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c  E_MUTEX_STATIC_L
178cc 52 55 32 29 29 3b 0a 0a 20 20 69 66 28 20 70 2d  RU2));..  if( p-
178cd 3e 70 4c 72 75 50 72 65 76 20 7c 7c 20 70 2d 3e  >pLruPrev || p->
178ce 70 4c 72 75 4e 65 78 74 20 7c 7c 20 73 71 6c 69  pLruNext || sqli
178cf 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73  te3LruStatements
178d0 2e 70 46 69 72 73 74 3d 3d 70 20 29 7b 0a 20 20  .pFirst==p ){.  
178d1 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
178d2 6c 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74  leave(sqlite3Mut
178d3 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
178d4 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32  UTEX_STATIC_LRU2
178d5 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  ));.    return;.
178d6 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 73    }..  assert( s
178d7 74 6d 74 4c 72 75 43 68 65 63 6b 28 29 20 29 3b  tmtLruCheck() );
178d8 0a 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33  ..  if( !sqlite3
178d9 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46  LruStatements.pF
178da 69 72 73 74 20 29 7b 0a 20 20 20 20 61 73 73 65  irst ){.    asse
178db 72 74 28 20 21 73 71 6c 69 74 65 33 4c 72 75 53  rt( !sqlite3LruS
178dc 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 73 74 20  tatements.pLast 
178dd 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4c 72  );.    sqlite3Lr
178de 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 72  uStatements.pFir
178df 73 74 20 3d 20 70 3b 0a 20 20 20 20 73 71 6c 69  st = p;.    sqli
178e0 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73  te3LruStatements
178e1 2e 70 4c 61 73 74 20 3d 20 70 3b 0a 20 20 7d 65  .pLast = p;.  }e
178e2 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
178e3 20 21 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74   !sqlite3LruStat
178e4 65 6d 65 6e 74 73 2e 70 4c 61 73 74 2d 3e 70 4c  ements.pLast->pL
178e5 72 75 4e 65 78 74 20 29 3b 0a 20 20 20 20 70 2d  ruNext );.    p-
178e6 3e 70 4c 72 75 50 72 65 76 20 3d 20 73 71 6c 69  >pLruPrev = sqli
178e7 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73  te3LruStatements
178e8 2e 70 4c 61 73 74 3b 0a 20 20 20 20 73 71 6c 69  .pLast;.    sqli
178e9 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73  te3LruStatements
178ea 2e 70 4c 61 73 74 2d 3e 70 4c 72 75 4e 65 78 74  .pLast->pLruNext
178eb 20 3d 20 70 3b 0a 20 20 20 20 73 71 6c 69 74 65   = p;.    sqlite
178ec 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70  3LruStatements.p
178ed 4c 61 73 74 20 3d 20 70 3b 0a 20 20 7d 0a 0a 20  Last = p;.  }.. 
178ee 20 61 73 73 65 72 74 28 20 73 74 6d 74 4c 72 75   assert( stmtLru
178ef 43 68 65 63 6b 28 29 20 29 3b 0a 0a 20 20 73 71  Check() );..  sq
178f0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
178f1 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  e(sqlite3MutexAl
178f2 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
178f3 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a  _STATIC_LRU2));.
178f4 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e  }../*.** Assumin
178f5 67 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 55 54  g the SQLITE_MUT
178f6 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 20 6d  EX_STATIC_LRU2 m
178f7 75 74 65 78 74 20 69 73 20 61 6c 72 65 61 64 79  utext is already
178f8 20 68 65 6c 64 2c 20 72 65 6d 6f 76 65 0a 2a 2a   held, remove.**
178f9 20 73 74 61 74 65 6d 65 6e 74 20 70 20 66 72 6f   statement p fro
178fa 6d 20 74 68 65 20 6c 65 61 73 74 2d 72 65 63 65  m the least-rece
178fb 6e 74 6c 79 2d 75 73 65 64 20 73 74 61 74 65 6d  ntly-used statem
178fc 65 6e 74 20 6c 69 73 74 2e 20 49 66 20 74 68 65  ent list. If the
178fd 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69   .** statement i
178fe 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  s not currently 
178ff 70 61 72 74 20 6f 66 20 74 68 65 20 6c 69 73 74  part of the list
17900 2c 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 61  , this call is a
17901 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69   no-op..*/.stati
17902 63 20 76 6f 69 64 20 73 74 6d 74 4c 72 75 52 65  c void stmtLruRe
17903 6d 6f 76 65 4e 6f 6d 75 74 65 78 28 56 64 62 65  moveNomutex(Vdbe
17904 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 70   *p){.  if( p->p
17905 4c 72 75 50 72 65 76 20 7c 7c 20 70 2d 3e 70 4c  LruPrev || p->pL
17906 72 75 4e 65 78 74 20 7c 7c 20 70 3d 3d 73 71 6c  ruNext || p==sql
17907 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74  ite3LruStatement
17908 73 2e 70 46 69 72 73 74 20 29 7b 0a 20 20 20 20  s.pFirst ){.    
17909 61 73 73 65 72 74 28 20 73 74 6d 74 4c 72 75 43  assert( stmtLruC
1790a 68 65 63 6b 28 29 20 29 3b 0a 20 20 20 20 69 66  heck() );.    if
1790b 28 20 70 2d 3e 70 4c 72 75 4e 65 78 74 20 29 7b  ( p->pLruNext ){
1790c 0a 20 20 20 20 20 20 70 2d 3e 70 4c 72 75 4e 65  .      p->pLruNe
1790d 78 74 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 70  xt->pLruPrev = p
1790e 2d 3e 70 4c 72 75 50 72 65 76 3b 0a 20 20 20 20  ->pLruPrev;.    
1790f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
17910 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74  ite3LruStatement
17911 73 2e 70 4c 61 73 74 20 3d 20 70 2d 3e 70 4c 72  s.pLast = p->pLr
17912 75 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20  uPrev;.    }.   
17913 20 69 66 28 20 70 2d 3e 70 4c 72 75 50 72 65 76   if( p->pLruPrev
17914 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 72   ){.      p->pLr
17915 75 50 72 65 76 2d 3e 70 4c 72 75 4e 65 78 74 20  uPrev->pLruNext 
17916 3d 20 70 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20  = p->pLruNext;. 
17917 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
17918 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d  sqlite3LruStatem
17919 65 6e 74 73 2e 70 46 69 72 73 74 20 3d 20 70 2d  ents.pFirst = p-
1791a 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 20 7d  >pLruNext;.    }
1791b 0a 20 20 20 20 70 2d 3e 70 4c 72 75 4e 65 78 74  .    p->pLruNext
1791c 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70 4c 72   = 0;.    p->pLr
1791d 75 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 61  uPrev = 0;.    a
1791e 73 73 65 72 74 28 20 73 74 6d 74 4c 72 75 43 68  ssert( stmtLruCh
1791f 65 63 6b 28 29 20 29 3b 0a 20 20 7d 0a 7d 0a 0a  eck() );.  }.}..
17920 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 74  /*.** Assuming t
17921 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  he SQLITE_MUTEX_
17922 53 54 41 54 49 43 5f 4c 52 55 32 20 6d 75 74 65  STATIC_LRU2 mute
17923 78 74 20 69 73 20 6e 6f 74 20 68 65 6c 64 2c 20  xt is not held, 
17924 72 65 6d 6f 76 65 0a 2a 2a 20 73 74 61 74 65 6d  remove.** statem
17925 65 6e 74 20 70 20 66 72 6f 6d 20 74 68 65 20 6c  ent p from the l
17926 65 61 73 74 2d 72 65 63 65 6e 74 6c 79 2d 75 73  east-recently-us
17927 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6c 69 73  ed statement lis
17928 74 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20 73 74  t. If the .** st
17929 61 74 65 6d 65 6e 74 20 69 73 20 6e 6f 74 20 63  atement is not c
1792a 75 72 72 65 6e 74 6c 79 20 70 61 72 74 20 6f 66  urrently part of
1792b 20 74 68 65 20 6c 69 73 74 2c 20 74 68 69 73 20   the list, this 
1792c 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  call is a no-op.
1792d 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1792e 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 28 56 64  stmtLruRemove(Vd
1792f 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65  be *p){.  sqlite
17930 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71  3_mutex_enter(sq
17931 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
17932 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
17933 54 49 43 5f 4c 52 55 32 29 29 3b 0a 20 20 73 74  TIC_LRU2));.  st
17934 6d 74 4c 72 75 52 65 6d 6f 76 65 4e 6f 6d 75 74  mtLruRemoveNomut
17935 65 78 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  ex(p);.  sqlite3
17936 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c  _mutex_leave(sql
17937 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
17938 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
17939 49 43 5f 4c 52 55 32 29 29 3b 0a 7d 0a 0a 2f 2a  IC_LRU2));.}../*
1793a 0a 2a 2a 20 54 72 79 20 74 6f 20 72 65 6c 65 61  .** Try to relea
1793b 73 65 20 6e 20 62 79 74 65 73 20 6f 66 20 6d 65  se n bytes of me
1793c 6d 6f 72 79 20 62 79 20 66 72 65 65 69 6e 67 20  mory by freeing 
1793d 62 75 66 66 65 72 73 20 61 73 73 6f 63 69 61 74  buffers associat
1793e 65 64 20 0a 2a 2a 20 77 69 74 68 20 74 68 65 20  ed .** with the 
1793f 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73  memory registers
17940 20 6f 66 20 63 75 72 72 65 6e 74 6c 79 20 75 6e   of currently un
17941 75 73 65 64 20 76 64 62 65 73 2e 0a 2a 2f 0a 53  used vdbes..*/.S
17942 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
17943 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 6c  t sqlite3VdbeRel
17944 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e 74 20 6e  easeMemory(int n
17945 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20  ){.  Vdbe *p;.  
17946 56 64 62 65 20 2a 70 4e 65 78 74 3b 0a 20 20 69  Vdbe *pNext;.  i
17947 6e 74 20 6e 46 72 65 65 20 3d 20 30 3b 0a 0a 20  nt nFree = 0;.. 
17948 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
17949 6e 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 65  nter(sqlite3Mute
1794a 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
1794b 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 29  TEX_STATIC_LRU2)
1794c 29 3b 0a 20 20 66 6f 72 28 70 3d 73 71 6c 69 74  );.  for(p=sqlit
1794d 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e  e3LruStatements.
1794e 70 46 69 72 73 74 3b 20 70 20 26 26 20 6e 46 72  pFirst; p && nFr
1794f 65 65 3c 6e 3b 20 70 3d 70 4e 65 78 74 29 7b 0a  ee<n; p=pNext){.
17950 20 20 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e 70      pNext = p->p
17951 4c 72 75 4e 65 78 74 3b 0a 0a 20 20 20 20 2f 2a  LruNext;..    /*
17952 20 46 6f 72 20 65 61 63 68 20 73 74 61 74 65 6d   For each statem
17953 65 6e 74 20 68 61 6e 64 6c 65 20 69 6e 20 74 68  ent handle in th
17954 65 20 6c 72 75 20 6c 69 73 74 2c 20 61 74 74 65  e lru list, atte
17955 6d 70 74 20 74 6f 20 6f 62 74 61 69 6e 20 74 68  mpt to obtain th
17956 65 0a 20 20 20 20 2a 2a 20 61 73 73 6f 63 69 61  e.    ** associa
17957 74 65 64 20 64 61 74 61 62 61 73 65 20 6d 75 74  ted database mut
17958 65 78 2e 20 49 66 20 69 74 20 63 61 6e 6e 6f 74  ex. If it cannot
17959 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 63 6f   be obtained, co
1795a 6e 74 69 6e 75 65 0a 20 20 20 20 2a 2a 20 74 6f  ntinue.    ** to
1795b 20 74 68 65 20 6e 65 78 74 20 73 74 61 74 65 6d   the next statem
1795c 65 6e 74 20 68 61 6e 64 6c 65 2e 20 49 74 20 69  ent handle. It i
1795d 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74  s not possible t
1795e 6f 20 62 6c 6f 63 6b 20 6f 6e 0a 20 20 20 20 2a  o block on.    *
1795f 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d  * the database m
17960 75 74 65 78 20 2d 20 74 68 61 74 20 63 6f 75 6c  utex - that coul
17961 64 20 63 61 75 73 65 20 64 65 61 64 6c 6f 63 6b  d cause deadlock
17962 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
17963 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69   SQLITE_OK==sqli
17964 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 70 2d  te3_mutex_try(p-
17965 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 7b 0a 20  >db->mutex) ){. 
17966 20 20 20 20 20 6e 46 72 65 65 20 2b 3d 20 73 71       nFree += sq
17967 6c 69 74 65 33 56 64 62 65 52 65 6c 65 61 73 65  lite3VdbeRelease
17968 42 75 66 66 65 72 73 28 70 29 3b 0a 20 20 20 20  Buffers(p);.    
17969 20 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 4e    stmtLruRemoveN
1796a 6f 6d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 20  omutex(p);.     
1796b 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
1796c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  eave(p->db->mute
1796d 78 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  x);.    }.  }.  
1796e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1796f 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78  ave(sqlite3Mutex
17970 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
17971 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 29  EX_STATIC_LRU2))
17972 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6e 46 72 65  ;..  return nFre
17973 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c  e;.}../*.** Call
17974 20 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72   sqlite3Reprepar
17975 65 28 29 20 6f 6e 20 74 68 65 20 73 74 61 74 65  e() on the state
17976 6d 65 6e 74 2e 20 52 65 6d 6f 76 65 20 69 74 20  ment. Remove it 
17977 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 6c 72 75 20  from the.** lru 
17978 6c 69 73 74 20 62 65 66 6f 72 65 20 64 6f 69 6e  list before doin
17979 67 20 73 6f 2c 20 61 73 20 52 65 70 72 65 70 61  g so, as Reprepa
1797a 72 65 28 29 20 77 69 6c 6c 20 66 72 65 65 20 61  re() will free a
1797b 6c 6c 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79  ll the.** memory
1797c 20 72 65 67 69 73 74 65 72 20 62 75 66 66 65 72   register buffer
1797d 73 20 61 6e 79 77 61 79 2e 0a 2a 2f 0a 69 6e 74  s anyway..*/.int
1797e 20 76 64 62 65 52 65 70 72 65 70 61 72 65 28 56   vdbeReprepare(V
1797f 64 62 65 20 2a 70 29 7b 0a 20 20 73 74 6d 74 4c  dbe *p){.  stmtL
17980 72 75 52 65 6d 6f 76 65 28 70 29 3b 0a 20 20 72  ruRemove(p);.  r
17981 65 74 75 72 6e 20 73 71 6c 69 74 65 33 52 65 70  eturn sqlite3Rep
17982 72 65 70 61 72 65 28 70 29 3b 0a 7d 0a 0a 23 65  repare(p);.}..#e
17983 6c 73 65 20 20 20 20 20 20 20 2f 2a 20 21 53 51  lse       /* !SQ
17984 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f  LITE_ENABLE_MEMO
17985 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 20 2a 2f  RY_MANAGEMENT */
17986 0a 20 20 23 64 65 66 69 6e 65 20 73 74 6d 74 4c  .  #define stmtL
17987 72 75 52 65 6d 6f 76 65 28 78 29 0a 20 20 23 64  ruRemove(x).  #d
17988 65 66 69 6e 65 20 73 74 6d 74 4c 72 75 41 64 64  efine stmtLruAdd
17989 28 78 29 0a 20 20 23 64 65 66 69 6e 65 20 76 64  (x).  #define vd
1798a 62 65 52 65 70 72 65 70 61 72 65 28 78 29 20 73  beReprepare(x) s
1798b 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28  qlite3Reprepare(
1798c 78 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e  x).#endif...#ifn
1798d 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1798e 44 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a  DEPRECATED./*.**
1798f 20 52 65 74 75 72 6e 20 54 52 55 45 20 28 6e 6f   Return TRUE (no
17990 6e 2d 7a 65 72 6f 29 20 6f 66 20 74 68 65 20 73  n-zero) of the s
17991 74 61 74 65 6d 65 6e 74 20 73 75 70 70 6c 69 65  tatement supplie
17992 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74  d as an argument
17993 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 62 65 20   needs.** to be 
17994 72 65 63 6f 6d 70 69 6c 65 64 2e 20 20 41 20 73  recompiled.  A s
17995 74 61 74 65 6d 65 6e 74 20 6e 65 65 64 73 20 74  tatement needs t
17996 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 20  o be recompiled 
17997 77 68 65 6e 65 76 65 72 20 74 68 65 0a 2a 2a 20  whenever the.** 
17998 65 78 65 63 75 74 69 6f 6e 20 65 6e 76 69 72 6f  execution enviro
17999 6e 6d 65 6e 74 20 63 68 61 6e 67 65 73 20 69 6e  nment changes in
1799a 20 61 20 77 61 79 20 74 68 61 74 20 77 6f 75 6c   a way that woul
1799b 64 20 61 6c 74 65 72 20 74 68 65 20 70 72 6f 67  d alter the prog
1799c 72 61 6d 0a 2a 2a 20 74 68 61 74 20 73 71 6c 69  ram.** that sqli
1799d 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 67 65  te3_prepare() ge
1799e 6e 65 72 61 74 65 73 2e 20 20 46 6f 72 20 65 78  nerates.  For ex
1799f 61 6d 70 6c 65 2c 20 69 66 20 6e 65 77 20 66 75  ample, if new fu
179a0 6e 63 74 69 6f 6e 73 20 6f 72 0a 2a 2a 20 63 6f  nctions or.** co
179a1 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
179a2 73 20 61 72 65 20 72 65 67 69 73 74 65 72 65 64  s are registered
179a3 20 6f 72 20 69 66 20 61 6e 20 61 75 74 68 6f 72   or if an author
179a4 69 7a 65 72 20 66 75 6e 63 74 69 6f 6e 20 69 73  izer function is
179a5 0a 2a 2a 20 61 64 64 65 64 20 6f 72 20 63 68 61  .** added or cha
179a6 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  nged..*/.SQLITE_
179a7 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
179a8 65 78 70 69 72 65 64 28 73 71 6c 69 74 65 33 5f  expired(sqlite3_
179a9 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20  stmt *pStmt){.  
179aa 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a  Vdbe *p = (Vdbe*
179ab 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e  )pStmt;.  return
179ac 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 65 78 70 69   p==0 || p->expi
179ad 72 65 64 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  red;.}.#endif../
179ae 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
179af 6e 67 20 72 6f 75 74 69 6e 65 20 64 65 73 74 72  ng routine destr
179b0 6f 79 73 20 61 20 76 69 72 74 75 61 6c 20 6d 61  oys a virtual ma
179b1 63 68 69 6e 65 20 74 68 61 74 20 69 73 20 63 72  chine that is cr
179b2 65 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20  eated by.** the 
179b3 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 28  sqlite3_compile(
179b4 29 20 72 6f 75 74 69 6e 65 2e 20 54 68 65 20 69  ) routine. The i
179b5 6e 74 65 67 65 72 20 72 65 74 75 72 6e 65 64 20  nteger returned 
179b6 69 73 20 61 6e 20 53 51 4c 49 54 45 5f 0a 2a 2a  is an SQLITE_.**
179b7 20 73 75 63 63 65 73 73 2f 66 61 69 6c 75 72 65   success/failure
179b8 20 63 6f 64 65 20 74 68 61 74 20 64 65 73 63 72   code that descr
179b9 69 62 65 73 20 74 68 65 20 72 65 73 75 6c 74 20  ibes the result 
179ba 6f 66 20 65 78 65 63 75 74 69 6e 67 20 74 68 65  of executing the
179bb 20 76 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68   virtual.** mach
179bc 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ine..**.** This 
179bd 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65  routine sets the
179be 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20   error code and 
179bf 73 74 72 69 6e 67 20 72 65 74 75 72 6e 65 64 20  string returned 
179c0 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72  by.** sqlite3_er
179c1 72 63 6f 64 65 28 29 2c 20 73 71 6c 69 74 65 33  rcode(), sqlite3
179c2 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73 71  _errmsg() and sq
179c3 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29  lite3_errmsg16()
179c4 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
179c5 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  int sqlite3_fina
179c6 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  lize(sqlite3_stm
179c7 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74  t *pStmt){.  int
179c8 20 72 63 3b 0a 20 20 69 66 28 20 70 53 74 6d 74   rc;.  if( pStmt
179c9 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ==0 ){.    rc = 
179ca 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c  SQLITE_OK;.  }el
179cb 73 65 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 20  se{.    Vdbe *v 
179cc 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a  = (Vdbe*)pStmt;.
179cd 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
179ce 44 53 41 46 45 0a 20 20 20 20 73 71 6c 69 74 65  DSAFE.    sqlite
179cf 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d  3_mutex *mutex =
179d0 20 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 3b 0a 23   v->db->mutex;.#
179d1 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 74 65  endif.    sqlite
179d2 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75  3_mutex_enter(mu
179d3 74 65 78 29 3b 0a 20 20 20 20 73 74 6d 74 4c 72  tex);.    stmtLr
179d4 75 52 65 6d 6f 76 65 28 76 29 3b 0a 20 20 20 20  uRemove(v);.    
179d5 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
179d6 46 69 6e 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20  Finalize(v);.   
179d7 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
179d8 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 7d  eave(mutex);.  }
179d9 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
179da 0a 2f 2a 0a 2a 2a 20 54 65 72 6d 69 6e 61 74 65  ./*.** Terminate
179db 20 74 68 65 20 63 75 72 72 65 6e 74 20 65 78 65   the current exe
179dc 63 75 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c  cution of an SQL
179dd 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72   statement and r
179de 65 73 65 74 20 69 74 0a 2a 2a 20 62 61 63 6b 20  eset it.** back 
179df 74 6f 20 69 74 73 20 73 74 61 72 74 69 6e 67 20  to its starting 
179e0 73 74 61 74 65 20 73 6f 20 74 68 61 74 20 69 74  state so that it
179e1 20 63 61 6e 20 62 65 20 72 65 75 73 65 64 2e 20   can be reused. 
179e2 41 20 73 75 63 63 65 73 73 20 63 6f 64 65 20 66  A success code f
179e3 72 6f 6d 0a 2a 2a 20 74 68 65 20 70 72 69 6f 72  rom.** the prior
179e4 20 65 78 65 63 75 74 69 6f 6e 20 69 73 20 72 65   execution is re
179e5 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  turned..**.** Th
179e6 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20  is routine sets 
179e7 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61  the error code a
179e8 6e 64 20 73 74 72 69 6e 67 20 72 65 74 75 72 6e  nd string return
179e9 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33  ed by.** sqlite3
179ea 5f 65 72 72 63 6f 64 65 28 29 2c 20 73 71 6c 69  _errcode(), sqli
179eb 74 65 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64  te3_errmsg() and
179ec 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31   sqlite3_errmsg1
179ed 36 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  6()..*/.SQLITE_A
179ee 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72  PI int sqlite3_r
179ef 65 73 65 74 28 73 71 6c 69 74 65 33 5f 73 74 6d  eset(sqlite3_stm
179f0 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74  t *pStmt){.  int
179f1 20 72 63 3b 0a 20 20 69 66 28 20 70 53 74 6d 74   rc;.  if( pStmt
179f2 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ==0 ){.    rc = 
179f3 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c  SQLITE_OK;.  }el
179f4 73 65 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 20  se{.    Vdbe *v 
179f5 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a  = (Vdbe*)pStmt;.
179f6 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
179f7 78 5f 65 6e 74 65 72 28 76 2d 3e 64 62 2d 3e 6d  x_enter(v->db->m
179f8 75 74 65 78 29 3b 0a 20 20 20 20 72 63 20 3d 20  utex);.    rc = 
179f9 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74  sqlite3VdbeReset
179fa 28 76 29 3b 0a 20 20 20 20 73 74 6d 74 4c 72 75  (v);.    stmtLru
179fb 41 64 64 28 76 29 3b 0a 20 20 20 20 73 71 6c 69  Add(v);.    sqli
179fc 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79  te3VdbeMakeReady
179fd 28 76 2c 20 2d 31 2c 20 30 2c 20 30 2c 20 30 29  (v, -1, 0, 0, 0)
179fe 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 72  ;.    assert( (r
179ff 63 20 26 20 28 76 2d 3e 64 62 2d 3e 65 72 72 4d  c & (v->db->errM
17a00 61 73 6b 29 29 3d 3d 72 63 20 29 3b 0a 20 20 20  ask))==rc );.   
17a01 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
17a02 65 61 76 65 28 76 2d 3e 64 62 2d 3e 6d 75 74 65  eave(v->db->mute
17a03 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  x);.  }.  return
17a04 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65   rc;.}../*.** Se
17a05 74 20 61 6c 6c 20 74 68 65 20 70 61 72 61 6d 65  t all the parame
17a06 74 65 72 73 20 69 6e 20 74 68 65 20 63 6f 6d 70  ters in the comp
17a07 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65  iled SQL stateme
17a08 6e 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53  nt to NULL..*/.S
17a09 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
17a0a 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64  lite3_clear_bind
17a0b 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d  ings(sqlite3_stm
17a0c 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74  t *pStmt){.  int
17a0d 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53   i;.  int rc = S
17a0e 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 56 64 62 65  QLITE_OK;.  Vdbe
17a0f 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74   *p = (Vdbe*)pSt
17a10 6d 74 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  mt;.#if SQLITE_T
17a11 48 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69  HREADSAFE.  sqli
17a12 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
17a13 20 3d 20 28 28 56 64 62 65 2a 29 70 53 74 6d 74   = ((Vdbe*)pStmt
17a14 29 2d 3e 64 62 2d 3e 6d 75 74 65 78 3b 0a 23 65  )->db->mutex;.#e
17a15 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ndif.  sqlite3_m
17a16 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78  utex_enter(mutex
17a17 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
17a18 70 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20  p->nVar; i++){. 
17a19 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
17a1a 6d 52 65 6c 65 61 73 65 28 26 70 2d 3e 61 56 61  mRelease(&p->aVa
17a1b 72 5b 69 5d 29 3b 0a 20 20 20 20 70 2d 3e 61 56  r[i]);.    p->aV
17a1c 61 72 5b 69 5d 2e 66 6c 61 67 73 20 3d 20 4d 45  ar[i].flags = ME
17a1d 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 0a 20 20 73 71  M_Null;.  }.  sq
17a1e 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
17a1f 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  e(mutex);.  retu
17a20 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a  rn rc;.}.../****
17a21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17a22 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33  ******** sqlite3
17a23 5f 76 61 6c 75 65 5f 20 20 2a 2a 2a 2a 2a 2a 2a  _value_  *******
17a24 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17a25 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20  ********.** The 
17a26 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e  following routin
17a27 65 73 20 65 78 74 72 61 63 74 20 69 6e 66 6f 72  es extract infor
17a28 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 4d 65  mation from a Me
17a29 6d 20 6f 72 20 73 71 6c 69 74 65 33 5f 76 61 6c  m or sqlite3_val
17a2a 75 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e  ue.** structure.
17a2b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
17a2c 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
17a2d 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 73 71  e3_value_blob(sq
17a2e 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
17a2f 6c 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 28  l){.  Mem *p = (
17a30 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 69 66 28  Mem*)pVal;.  if(
17a31 20 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d   p->flags & (MEM
17a32 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 20 29  _Blob|MEM_Str) )
17a33 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
17a34 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 70  eMemExpandBlob(p
17a35 29 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20  );.    p->flags 
17a36 26 3d 20 7e 4d 45 4d 5f 53 74 72 3b 0a 20 20 20  &= ~MEM_Str;.   
17a37 20 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d   p->flags |= MEM
17a38 5f 42 6c 6f 62 3b 0a 20 20 20 20 72 65 74 75 72  _Blob;.    retur
17a39 6e 20 70 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 65 7b  n p->z;.  }else{
17a3a 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
17a3b 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 70  te3_value_text(p
17a3c 56 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49  Val);.  }.}.SQLI
17a3d 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
17a3e 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 73  e3_value_bytes(s
17a3f 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
17a40 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  al){.  return sq
17a41 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28  lite3ValueBytes(
17a42 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46  pVal, SQLITE_UTF
17a43 38 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  8);.}.SQLITE_API
17a44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c   int sqlite3_val
17a45 75 65 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74  ue_bytes16(sqlit
17a46 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b  e3_value *pVal){
17a47 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
17a48 33 56 61 6c 75 65 42 79 74 65 73 28 70 56 61 6c  3ValueBytes(pVal
17a49 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  , SQLITE_UTF16NA
17a4a 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  TIVE);.}.SQLITE_
17a4b 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74  API double sqlit
17a4c 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28  e3_value_double(
17a4d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
17a4e 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  Val){.  return s
17a4f 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61  qlite3VdbeRealVa
17a50 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b  lue((Mem*)pVal);
17a51 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  .}.SQLITE_API in
17a52 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  t sqlite3_value_
17a53 69 6e 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  int(sqlite3_valu
17a54 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75  e *pVal){.  retu
17a55 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e  rn sqlite3VdbeIn
17a56 74 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61  tValue((Mem*)pVa
17a57 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  l);.}.SQLITE_API
17a58 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71   sqlite_int64 sq
17a59 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36  lite3_value_int6
17a5a 34 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  4(sqlite3_value 
17a5b 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e  *pVal){.  return
17a5c 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56   sqlite3VdbeIntV
17a5d 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29  alue((Mem*)pVal)
17a5e 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  ;.}.SQLITE_API c
17a5f 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
17a60 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar *sqlite3_valu
17a61 65 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f 76  e_text(sqlite3_v
17a62 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72  alue *pVal){.  r
17a63 65 74 75 72 6e 20 28 63 6f 6e 73 74 20 75 6e 73  eturn (const uns
17a64 69 67 6e 65 64 20 63 68 61 72 20 2a 29 73 71 6c  igned char *)sql
17a65 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56  ite3ValueText(pV
17a66 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29  al, SQLITE_UTF8)
17a67 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ;.}.#ifndef SQLI
17a68 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51  TE_OMIT_UTF16.SQ
17a69 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
17a6a 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c  oid *sqlite3_val
17a6b 75 65 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65  ue_text16(sqlite
17a6c 33 5f 76 61 6c 75 65 2a 20 70 56 61 6c 29 7b 0a  3_value* pVal){.
17a6d 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
17a6e 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20  ValueText(pVal, 
17a6f 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49  SQLITE_UTF16NATI
17a70 56 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  VE);.}.SQLITE_AP
17a71 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
17a72 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
17a73 31 36 62 65 28 73 71 6c 69 74 65 33 5f 76 61 6c  16be(sqlite3_val
17a74 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74  ue *pVal){.  ret
17a75 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65  urn sqlite3Value
17a76 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54  Text(pVal, SQLIT
17a77 45 5f 55 54 46 31 36 42 45 29 3b 0a 7d 0a 53 51  E_UTF16BE);.}.SQ
17a78 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
17a79 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c  oid *sqlite3_val
17a7a 75 65 5f 74 65 78 74 31 36 6c 65 28 73 71 6c 69  ue_text16le(sqli
17a7b 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29  te3_value *pVal)
17a7c 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
17a7d 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c  e3ValueText(pVal
17a7e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  , SQLITE_UTF16LE
17a7f 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  );.}.#endif /* S
17a80 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
17a81 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   */.SQLITE_API i
17a82 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt sqlite3_value
17a83 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61  _type(sqlite3_va
17a84 6c 75 65 2a 20 70 56 61 6c 29 7b 0a 20 20 72 65  lue* pVal){.  re
17a85 74 75 72 6e 20 70 56 61 6c 2d 3e 74 79 70 65 3b  turn pVal->type;
17a86 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
17a87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17a88 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  * sqlite3_result
17a89 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  _  *************
17a8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17a8b 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  **.** The follow
17a8c 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ing routines are
17a8d 20 75 73 65 64 20 62 79 20 75 73 65 72 2d 64 65   used by user-de
17a8e 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20  fined functions 
17a8f 74 6f 20 73 70 65 63 69 66 79 0a 2a 2a 20 74 68  to specify.** th
17a90 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 73 75 6c  e function resul
17a91 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
17a92 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
17a93 73 75 6c 74 5f 62 6c 6f 62 28 0a 20 20 73 71 6c  sult_blob(.  sql
17a94 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43  ite3_context *pC
17a95 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69  tx, .  const voi
17a96 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20  d *z, .  int n, 
17a97 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28  .  void (*xDel)(
17a98 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73  void *).){.  ass
17a99 65 72 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 61  ert( n>=0 );.  a
17a9a 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
17a9b 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e  utex_held(pCtx->
17a9c 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  s.db->mutex) );.
17a9d 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
17a9e 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c  SetStr(&pCtx->s,
17a9f 20 7a 2c 20 6e 2c 20 30 2c 20 78 44 65 6c 29 3b   z, n, 0, xDel);
17aa0 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  .}.SQLITE_API vo
17aa1 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
17aa2 74 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33  t_double(sqlite3
17aa3 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
17aa4 64 6f 75 62 6c 65 20 72 56 61 6c 29 7b 0a 20 20  double rVal){.  
17aa5 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
17aa6 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d  mutex_held(pCtx-
17aa7 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  >s.db->mutex) );
17aa8 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
17aa9 6d 53 65 74 44 6f 75 62 6c 65 28 26 70 43 74 78  mSetDouble(&pCtx
17aaa 2d 3e 73 2c 20 72 56 61 6c 29 3b 0a 7d 0a 53 51  ->s, rVal);.}.SQ
17aab 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
17aac 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
17aad 6f 72 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  or(sqlite3_conte
17aae 78 74 20 2a 70 43 74 78 2c 20 63 6f 6e 73 74 20  xt *pCtx, const 
17aaf 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b  char *z, int n){
17ab0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
17ab1 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
17ab2 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29  tx->s.db->mutex)
17ab3 20 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72   );.  pCtx->isEr
17ab4 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f 45 52 52  ror = SQLITE_ERR
17ab5 4f 52 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  OR;.  sqlite3Vdb
17ab6 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78  eMemSetStr(&pCtx
17ab7 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54  ->s, z, n, SQLIT
17ab8 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 54  E_UTF8, SQLITE_T
17ab9 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 69 66  RANSIENT);.}.#if
17aba 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
17abb 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50  _UTF16.SQLITE_AP
17abc 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
17abd 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 73 71  esult_error16(sq
17abe 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
17abf 43 74 78 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  Ctx, const void 
17ac0 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73  *z, int n){.  as
17ac1 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
17ac2 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
17ac3 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
17ac4 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d   pCtx->isError =
17ac5 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
17ac6 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
17ac7 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20  etStr(&pCtx->s, 
17ac8 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46  z, n, SQLITE_UTF
17ac9 31 36 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45  16NATIVE, SQLITE
17aca 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23  _TRANSIENT);.}.#
17acb 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 41 50 49  endif.SQLITE_API
17acc 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
17acd 73 75 6c 74 5f 69 6e 74 28 73 71 6c 69 74 65 33  sult_int(sqlite3
17ace 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
17acf 69 6e 74 20 69 56 61 6c 29 7b 0a 20 20 61 73 73  int iVal){.  ass
17ad0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
17ad1 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e  ex_held(pCtx->s.
17ad2 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
17ad3 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
17ad4 74 49 6e 74 36 34 28 26 70 43 74 78 2d 3e 73 2c  tInt64(&pCtx->s,
17ad5 20 28 69 36 34 29 69 56 61 6c 29 3b 0a 7d 0a 53   (i64)iVal);.}.S
17ad6 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
17ad7 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
17ad8 74 36 34 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  t64(sqlite3_cont
17ad9 65 78 74 20 2a 70 43 74 78 2c 20 69 36 34 20 69  ext *pCtx, i64 i
17ada 56 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20  Val){.  assert( 
17adb 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
17adc 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d  ld(pCtx->s.db->m
17add 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74  utex) );.  sqlit
17ade 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36  e3VdbeMemSetInt6
17adf 34 28 26 70 43 74 78 2d 3e 73 2c 20 69 56 61 6c  4(&pCtx->s, iVal
17ae0 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  );.}.SQLITE_API 
17ae1 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
17ae2 75 6c 74 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33  ult_null(sqlite3
17ae3 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 29 7b  _context *pCtx){
17ae4 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
17ae5 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
17ae6 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29  tx->s.db->mutex)
17ae7 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62   );.  sqlite3Vdb
17ae8 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 70 43 74  eMemSetNull(&pCt
17ae9 78 2d 3e 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  x->s);.}.SQLITE_
17aea 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
17aeb 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 0a 20 20  _result_text(.  
17aec 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
17aed 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20  *pCtx, .  const 
17aee 63 68 61 72 20 2a 7a 2c 20 0a 20 20 69 6e 74 20  char *z, .  int 
17aef 6e 2c 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c  n,.  void (*xDel
17af0 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61  )(void *).){.  a
17af1 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
17af2 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e  utex_held(pCtx->
17af3 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  s.db->mutex) );.
17af4 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
17af5 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c  SetStr(&pCtx->s,
17af6 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54   z, n, SQLITE_UT
17af7 46 38 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23 69 66  F8, xDel);.}.#if
17af8 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
17af9 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50  _UTF16.SQLITE_AP
17afa 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
17afb 65 73 75 6c 74 5f 74 65 78 74 31 36 28 0a 20 20  esult_text16(.  
17afc 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
17afd 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20  *pCtx, .  const 
17afe 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20  void *z, .  int 
17aff 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65  n, .  void (*xDe
17b00 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20  l)(void *).){.  
17b01 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
17b02 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d  mutex_held(pCtx-
17b03 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  >s.db->mutex) );
17b04 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
17b05 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73  mSetStr(&pCtx->s
17b06 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55  , z, n, SQLITE_U
17b07 54 46 31 36 4e 41 54 49 56 45 2c 20 78 44 65 6c  TF16NATIVE, xDel
17b08 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  );.}.SQLITE_API 
17b09 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
17b0a 75 6c 74 5f 74 65 78 74 31 36 62 65 28 0a 20 20  ult_text16be(.  
17b0b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
17b0c 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20  *pCtx, .  const 
17b0d 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20  void *z, .  int 
17b0e 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65  n, .  void (*xDe
17b0f 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20  l)(void *).){.  
17b10 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
17b11 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d  mutex_held(pCtx-
17b12 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  >s.db->mutex) );
17b13 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
17b14 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73  mSetStr(&pCtx->s
17b15 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55  , z, n, SQLITE_U
17b16 54 46 31 36 42 45 2c 20 78 44 65 6c 29 3b 0a 7d  TF16BE, xDel);.}
17b17 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
17b18 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
17b19 74 65 78 74 31 36 6c 65 28 0a 20 20 73 71 6c 69  text16le(.  sqli
17b1a 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74  te3_context *pCt
17b1b 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  x, .  const void
17b1c 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a   *z, .  int n, .
17b1d 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76    void (*xDel)(v
17b1e 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65  oid *).){.  asse
17b1f 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
17b20 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64  x_held(pCtx->s.d
17b21 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73  b->mutex) );.  s
17b22 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
17b23 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c  Str(&pCtx->s, z,
17b24 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36   n, SQLITE_UTF16
17b25 4c 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23 65 6e  LE, xDel);.}.#en
17b26 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
17b27 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49  IT_UTF16 */.SQLI
17b28 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
17b29 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65  te3_result_value
17b2a 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
17b2b 20 2a 70 43 74 78 2c 20 73 71 6c 69 74 65 33 5f   *pCtx, sqlite3_
17b2c 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 29 7b 0a  value *pValue){.
17b2d 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
17b2e 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74  3_mutex_held(pCt
17b2f 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  x->s.db->mutex) 
17b30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
17b31 4d 65 6d 43 6f 70 79 28 26 70 43 74 78 2d 3e 73  MemCopy(&pCtx->s
17b32 2c 20 70 56 61 6c 75 65 29 3b 0a 7d 0a 53 51 4c  , pValue);.}.SQL
17b33 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
17b34 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f  ite3_result_zero
17b35 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e  blob(sqlite3_con
17b36 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20  text *pCtx, int 
17b37 6e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  n){.  assert( sq
17b38 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
17b39 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74  (pCtx->s.db->mut
17b3a 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ex) );.  sqlite3
17b3b 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c  VdbeMemSetZeroBl
17b3c 6f 62 28 26 70 43 74 78 2d 3e 73 2c 20 6e 29 3b  ob(&pCtx->s, n);
17b3d 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  .}.SQLITE_API vo
17b3e 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
17b3f 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28 73 71 6c  t_error_code(sql
17b40 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43  ite3_context *pC
17b41 74 78 2c 20 69 6e 74 20 65 72 72 43 6f 64 65 29  tx, int errCode)
17b42 7b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72 6f  {.  pCtx->isErro
17b43 72 20 3d 20 65 72 72 43 6f 64 65 3b 0a 7d 0a 0a  r = errCode;.}..
17b44 2f 2a 20 46 6f 72 63 65 20 61 6e 20 53 51 4c 49  /* Force an SQLI
17b45 54 45 5f 54 4f 4f 42 49 47 20 65 72 72 6f 72 2e  TE_TOOBIG error.
17b46 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76   */.SQLITE_API v
17b47 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
17b48 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28  lt_error_toobig(
17b49 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
17b4a 2a 70 43 74 78 29 7b 0a 20 20 61 73 73 65 72 74  *pCtx){.  assert
17b4b 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
17b4c 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d  held(pCtx->s.db-
17b4d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 74  >mutex) );.  pCt
17b4e 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c  x->isError = SQL
17b4f 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 73 71  ITE_TOOBIG;.  sq
17b50 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53  lite3VdbeMemSetS
17b51 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 22 73 74  tr(&pCtx->s, "st
17b52 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74 6f 6f  ring or blob too
17b53 20 62 69 67 22 2c 20 2d 31 2c 20 0a 20 20 20 20   big", -1, .    
17b54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17b55 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20     SQLITE_UTF8, 
17b56 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
17b57 7d 0a 0a 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f  }../* An SQLITE_
17b58 4e 4f 4d 45 4d 20 65 72 72 6f 72 2e 20 2a 2f 0a  NOMEM error. */.
17b59 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
17b5a 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
17b5b 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74  rror_nomem(sqlit
17b5c 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
17b5d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
17b5e 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
17b5f 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65  pCtx->s.db->mute
17b60 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  x) );.  sqlite3V
17b61 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 70  dbeMemSetNull(&p
17b62 43 74 78 2d 3e 73 29 3b 0a 20 20 70 43 74 78 2d  Ctx->s);.  pCtx-
17b63 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 54  >isError = SQLIT
17b64 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 70 43 74 78 2d  E_NOMEM;.  pCtx-
17b65 3e 73 2e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  >s.db->mallocFai
17b66 6c 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  led = 1;.}../*.*
17b67 2a 20 45 78 65 63 75 74 65 20 74 68 65 20 73 74  * Execute the st
17b68 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2c 20 65  atement pStmt, e
17b69 69 74 68 65 72 20 75 6e 74 69 6c 20 61 20 72 6f  ither until a ro
17b6a 77 20 6f 66 20 64 61 74 61 20 69 73 20 72 65 61  w of data is rea
17b6b 64 79 2c 20 74 68 65 0a 2a 2a 20 73 74 61 74 65  dy, the.** state
17b6c 6d 65 6e 74 20 69 73 20 63 6f 6d 70 6c 65 74 65  ment is complete
17b6d 6c 79 20 65 78 65 63 75 74 65 64 20 6f 72 20 61  ly executed or a
17b6e 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a  n error occurs..
17b6f 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
17b70 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  ne implements th
17b71 65 20 62 75 6c 6b 20 6f 66 20 74 68 65 20 6c 6f  e bulk of the lo
17b72 67 69 63 20 62 65 68 69 6e 64 20 74 68 65 20 73  gic behind the s
17b73 71 6c 69 74 65 5f 73 74 65 70 28 29 0a 2a 2a 20  qlite_step().** 
17b74 41 50 49 2e 20 20 54 68 65 20 6f 6e 6c 79 20 74  API.  The only t
17b75 68 69 6e 67 20 6f 6d 69 74 74 65 64 20 69 73 20  hing omitted is 
17b76 74 68 65 20 61 75 74 6f 6d 61 74 69 63 20 72 65  the automatic re
17b77 63 6f 6d 70 69 6c 65 20 69 66 20 61 20 0a 2a 2a  compile if a .**
17b78 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 20 68   schema change h
17b79 61 73 20 6f 63 63 75 72 72 65 64 2e 20 20 54 68  as occurred.  Th
17b7a 61 74 20 64 65 74 61 69 6c 20 69 73 20 68 61 6e  at detail is han
17b7b 64 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 6f  dled by the.** o
17b7c 75 74 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65  uter sqlite3_ste
17b7d 70 28 29 20 77 72 61 70 70 65 72 20 70 72 6f 63  p() wrapper proc
17b7e 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  edure..*/.static
17b7f 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 65 70   int sqlite3Step
17b80 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c  (Vdbe *p){.  sql
17b81 69 74 65 33 20 2a 64 62 3b 0a 20 20 69 6e 74 20  ite3 *db;.  int 
17b82 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 70 29  rc;..  assert(p)
17b83 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63  ;.  if( p->magic
17b84 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e  !=VDBE_MAGIC_RUN
17b85 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
17b86 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
17b87 7d 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74  }..  /* Assert t
17b88 68 61 74 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73  hat malloc() has
17b89 20 6e 6f 74 20 66 61 69 6c 65 64 20 2a 2f 0a 20   not failed */. 
17b8a 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69   db = p->db;.  i
17b8b 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
17b8c 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72  led ){.    retur
17b8d 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
17b8e 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d 3e 70 63    }..  if( p->pc
17b8f 3c 3d 30 20 26 26 20 70 2d 3e 65 78 70 69 72 65  <=0 && p->expire
17b90 64 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  d ){.    if( p->
17b91 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
17b92 0a 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 53  .      p->rc = S
17b93 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20  QLITE_SCHEMA;.  
17b94 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c    }.    rc = SQL
17b95 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 67  ITE_ERROR;.    g
17b96 6f 74 6f 20 65 6e 64 5f 6f 66 5f 73 74 65 70 3b  oto end_of_step;
17b97 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74  .  }.  if( sqlit
17b98 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29  e3SafetyOn(db) )
17b99 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51  {.    p->rc = SQ
17b9a 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 20  LITE_MISUSE;.   
17b9b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d   return SQLITE_M
17b9c 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28  ISUSE;.  }.  if(
17b9d 20 70 2d 3e 70 63 3c 30 20 29 7b 0a 20 20 20 20   p->pc<0 ){.    
17b9e 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  /* If there are 
17b9f 6e 6f 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65  no other stateme
17ba0 6e 74 73 20 63 75 72 72 65 6e 74 6c 79 20 72 75  nts currently ru
17ba1 6e 6e 69 6e 67 2c 20 74 68 65 6e 0a 20 20 20 20  nning, then.    
17ba2 2a 2a 20 72 65 73 65 74 20 74 68 65 20 69 6e 74  ** reset the int
17ba3 65 72 72 75 70 74 20 66 6c 61 67 2e 20 20 54 68  errupt flag.  Th
17ba4 69 73 20 70 72 65 76 65 6e 74 73 20 61 20 63 61  is prevents a ca
17ba5 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e  ll to sqlite3_in
17ba6 74 65 72 72 75 70 74 0a 20 20 20 20 2a 2a 20 66  terrupt.    ** f
17ba7 72 6f 6d 20 69 6e 74 65 72 72 75 70 74 69 6e 67  rom interrupting
17ba8 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61   a statement tha
17ba9 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20 73 74  t has not yet st
17baa 61 72 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  arted..    */.  
17bab 20 20 69 66 28 20 64 62 2d 3e 61 63 74 69 76 65    if( db->active
17bac 56 64 62 65 43 6e 74 3d 3d 30 20 29 7b 0a 20 20  VdbeCnt==0 ){.  
17bad 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74      db->u1.isInt
17bae 65 72 72 75 70 74 65 64 20 3d 20 30 3b 0a 20 20  errupted = 0;.  
17baf 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
17bb0 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20  ITE_OMIT_TRACE. 
17bb1 20 20 20 69 66 28 20 64 62 2d 3e 78 50 72 6f 66     if( db->xProf
17bb2 69 6c 65 20 26 26 20 21 64 62 2d 3e 69 6e 69 74  ile && !db->init
17bb3 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20 20 64  .busy ){.      d
17bb4 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20 20 20  ouble rNow;.    
17bb5 20 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65    sqlite3OsCurre
17bb6 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c  ntTime(db->pVfs,
17bb7 20 26 72 4e 6f 77 29 3b 0a 20 20 20 20 20 20 70   &rNow);.      p
17bb8 2d 3e 73 74 61 72 74 54 69 6d 65 20 3d 20 28 72  ->startTime = (r
17bb9 4e 6f 77 20 2d 20 28 69 6e 74 29 72 4e 6f 77 29  Now - (int)rNow)
17bba 2a 33 36 30 30 2e 30 2a 32 34 2e 30 2a 31 30 30  *3600.0*24.0*100
17bbb 30 30 30 30 30 30 30 2e 30 3b 0a 20 20 20 20 7d  0000000.0;.    }
17bbc 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 64 62 2d  .#endif..    db-
17bbd 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 2b 2b  >activeVdbeCnt++
17bbe 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 65 61  ;.    if( p->rea
17bbf 64 4f 6e 6c 79 3d 3d 30 20 29 20 64 62 2d 3e 77  dOnly==0 ) db->w
17bc0 72 69 74 65 56 64 62 65 43 6e 74 2b 2b 3b 0a 20  riteVdbeCnt++;. 
17bc1 20 20 20 70 2d 3e 70 63 20 3d 20 30 3b 0a 20 20     p->pc = 0;.  
17bc2 20 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 28    stmtLruRemove(
17bc3 70 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20  p);.  }.#ifndef 
17bc4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c  SQLITE_OMIT_EXPL
17bc5 41 49 4e 0a 20 20 69 66 28 20 70 2d 3e 65 78 70  AIN.  if( p->exp
17bc6 6c 61 69 6e 20 29 7b 0a 20 20 20 20 72 63 20 3d  lain ){.    rc =
17bc7 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69 73 74   sqlite3VdbeList
17bc8 28 70 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e  (p);.  }else.#en
17bc9 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
17bca 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a 20 20  IT_EXPLAIN */.  
17bcb 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
17bcc 65 33 56 64 62 65 45 78 65 63 28 70 29 3b 0a 20  e3VdbeExec(p);. 
17bcd 20 7d 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65   }..  if( sqlite
17bce 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29  3SafetyOff(db) )
17bcf 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
17bd0 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 0a 23  E_MISUSE;.  }..#
17bd1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
17bd2 49 54 5f 54 52 41 43 45 0a 20 20 2f 2a 20 49 6e  IT_TRACE.  /* In
17bd3 76 6f 6b 65 20 74 68 65 20 70 72 6f 66 69 6c 65  voke the profile
17bd4 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 74 68 65   callback if the
17bd5 72 65 20 69 73 20 6f 6e 65 0a 20 20 2a 2f 0a 20  re is one.  */. 
17bd6 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
17bd7 52 4f 57 20 26 26 20 64 62 2d 3e 78 50 72 6f 66  ROW && db->xProf
17bd8 69 6c 65 20 26 26 20 21 64 62 2d 3e 69 6e 69 74  ile && !db->init
17bd9 2e 62 75 73 79 20 26 26 20 70 2d 3e 6e 4f 70 3e  .busy && p->nOp>
17bda 30 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20  0.           && 
17bdb 70 2d 3e 61 4f 70 5b 30 5d 2e 6f 70 63 6f 64 65  p->aOp[0].opcode
17bdc 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26 20 70 2d  ==OP_Trace && p-
17bdd 3e 61 4f 70 5b 30 5d 2e 70 34 2e 7a 21 3d 30 20  >aOp[0].p4.z!=0 
17bde 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 72 4e  ){.    double rN
17bdf 6f 77 3b 0a 20 20 20 20 75 36 34 20 65 6c 61 70  ow;.    u64 elap
17be0 73 65 54 69 6d 65 3b 0a 0a 20 20 20 20 73 71 6c  seTime;..    sql
17be1 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d  ite3OsCurrentTim
17be2 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 4e 6f  e(db->pVfs, &rNo
17be3 77 29 3b 0a 20 20 20 20 65 6c 61 70 73 65 54 69  w);.    elapseTi
17be4 6d 65 20 3d 20 28 72 4e 6f 77 20 2d 20 28 69 6e  me = (rNow - (in
17be5 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a 32  t)rNow)*3600.0*2
17be6 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30 2e 30  4.0*1000000000.0
17be7 20 2d 20 70 2d 3e 73 74 61 72 74 54 69 6d 65 3b   - p->startTime;
17be8 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f 66 69 6c  .    db->xProfil
17be9 65 28 64 62 2d 3e 70 50 72 6f 66 69 6c 65 41 72  e(db->pProfileAr
17bea 67 2c 20 70 2d 3e 61 4f 70 5b 30 5d 2e 70 34 2e  g, p->aOp[0].p4.
17beb 7a 2c 20 65 6c 61 70 73 65 54 69 6d 65 29 3b 0a  z, elapseTime);.
17bec 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 64 62    }.#endif..  db
17bed 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a  ->errCode = rc;.
17bee 20 20 2f 2a 73 71 6c 69 74 65 33 45 72 72 6f 72    /*sqlite3Error
17bef 28 70 2d 3e 64 62 2c 20 72 63 2c 20 30 29 3b 2a  (p->db, rc, 0);*
17bf0 2f 0a 20 20 70 2d 3e 72 63 20 3d 20 73 71 6c 69  /.  p->rc = sqli
17bf1 74 65 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62  te3ApiExit(p->db
17bf2 2c 20 70 2d 3e 72 63 29 3b 0a 65 6e 64 5f 6f 66  , p->rc);.end_of
17bf3 5f 73 74 65 70 3a 0a 20 20 61 73 73 65 72 74 28  _step:.  assert(
17bf4 20 28 72 63 26 30 78 66 66 29 3d 3d 72 63 20 29   (rc&0xff)==rc )
17bf5 3b 0a 20 20 69 66 28 20 70 2d 3e 7a 53 71 6c 20  ;.  if( p->zSql 
17bf6 26 26 20 28 72 63 26 30 78 66 66 29 3c 53 51 4c  && (rc&0xff)<SQL
17bf7 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 2f  ITE_ROW ){.    /
17bf8 2a 20 54 68 69 73 20 62 65 68 61 76 69 6f 72 20  * This behavior 
17bf9 6f 63 63 75 72 73 20 69 66 20 73 71 6c 69 74 65  occurs if sqlite
17bfa 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 20 77  3_prepare_v2() w
17bfb 61 73 20 75 73 65 64 20 74 6f 20 62 75 69 6c 64  as used to build
17bfc 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 72 65 70  .    ** the prep
17bfd 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 2e 20  ared statement. 
17bfe 20 52 65 74 75 72 6e 20 65 72 72 6f 72 20 63 6f   Return error co
17bff 64 65 73 20 64 69 72 65 63 74 6c 79 20 2a 2f 0a  des directly */.
17c00 20 20 20 20 70 2d 3e 64 62 2d 3e 65 72 72 43 6f      p->db->errCo
17c01 64 65 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 20 20  de = p->rc;.    
17c02 2f 2a 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28  /* sqlite3Error(
17c03 70 2d 3e 64 62 2c 20 70 2d 3e 72 63 2c 20 30 29  p->db, p->rc, 0)
17c04 3b 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  ; */.    return 
17c05 70 2d 3e 72 63 3b 0a 20 20 7d 65 6c 73 65 7b 0a  p->rc;.  }else{.
17c06 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 66      /* This is f
17c07 6f 72 20 6c 65 67 61 63 79 20 73 71 6c 69 74 65  or legacy sqlite
17c08 33 5f 70 72 65 70 61 72 65 28 29 20 62 75 69 6c  3_prepare() buil
17c09 64 73 20 61 6e 64 20 77 68 65 6e 20 74 68 65 20  ds and when the 
17c0a 63 6f 64 65 0a 20 20 20 20 2a 2a 20 69 73 20 53  code.    ** is S
17c0b 51 4c 49 54 45 5f 52 4f 57 20 6f 72 20 53 51 4c  QLITE_ROW or SQL
17c0c 49 54 45 5f 44 4f 4e 45 20 2a 2f 0a 20 20 20 20  ITE_DONE */.    
17c0d 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d  return rc;.  }.}
17c0e 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ../*.** This is 
17c0f 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20 69 6d  the top-level im
17c10 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
17c11 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20  sqlite3_step(). 
17c12 20 43 61 6c 6c 0a 2a 2a 20 73 71 6c 69 74 65 33   Call.** sqlite3
17c13 53 74 65 70 28 29 20 74 6f 20 64 6f 20 6d 6f 73  Step() to do mos
17c14 74 20 6f 66 20 74 68 65 20 77 6f 72 6b 2e 20 20  t of the work.  
17c15 49 66 20 61 20 73 63 68 65 6d 61 20 65 72 72 6f  If a schema erro
17c16 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 63 61 6c  r occurs,.** cal
17c17 6c 20 73 71 6c 69 74 65 33 52 65 70 72 65 70 61  l sqlite3Reprepa
17c18 72 65 28 29 20 61 6e 64 20 74 72 79 20 61 67 61  re() and try aga
17c19 69 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  in..*/.#ifdef SQ
17c1a 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53 45 52  LITE_OMIT_PARSER
17c1b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
17c1c 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 71 6c  sqlite3_step(sql
17c1d 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
17c1e 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
17c1f 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 69  LITE_MISUSE;.  i
17c20 66 28 20 70 53 74 6d 74 20 29 7b 0a 20 20 20 20  f( pStmt ){.    
17c21 56 64 62 65 20 2a 76 3b 0a 20 20 20 20 76 20 3d  Vdbe *v;.    v =
17c22 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20   (Vdbe*)pStmt;. 
17c23 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
17c24 5f 65 6e 74 65 72 28 76 2d 3e 64 62 2d 3e 6d 75  _enter(v->db->mu
17c25 74 65 78 29 3b 0a 20 20 20 20 72 63 20 3d 20 73  tex);.    rc = s
17c26 71 6c 69 74 65 33 53 74 65 70 28 76 29 3b 0a 20  qlite3Step(v);. 
17c27 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
17c28 5f 6c 65 61 76 65 28 76 2d 3e 64 62 2d 3e 6d 75  _leave(v->db->mu
17c29 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  tex);.  }.  retu
17c2a 72 6e 20 72 63 3b 0a 7d 0a 23 65 6c 73 65 0a 53  rn rc;.}.#else.S
17c2b 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
17c2c 6c 69 74 65 33 5f 73 74 65 70 28 73 71 6c 69 74  lite3_step(sqlit
17c2d 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b  e3_stmt *pStmt){
17c2e 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
17c2f 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 69 66 28  TE_MISUSE;.  if(
17c30 20 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 69 6e   pStmt ){.    in
17c31 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 56  t cnt = 0;.    V
17c32 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a 29  dbe *v = (Vdbe*)
17c33 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69 74  pStmt;.    sqlit
17c34 65 33 20 2a 64 62 20 3d 20 76 2d 3e 64 62 3b 0a  e3 *db = v->db;.
17c35 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
17c36 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
17c37 78 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28  x);.    while( (
17c38 72 63 20 3d 20 73 71 6c 69 74 65 33 53 74 65 70  rc = sqlite3Step
17c39 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f 53 43 48  (v))==SQLITE_SCH
17c3a 45 4d 41 0a 20 20 20 20 20 20 20 20 20 20 20 26  EMA.           &
17c3b 26 20 63 6e 74 2b 2b 20 3c 20 35 0a 20 20 20 20  & cnt++ < 5.    
17c3c 20 20 20 20 20 20 20 26 26 20 76 64 62 65 52 65         && vdbeRe
17c3d 70 72 65 70 61 72 65 28 76 29 20 29 7b 0a 20 20  prepare(v) ){.  
17c3e 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65      sqlite3_rese
17c3f 74 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 20 20  t(pStmt);.      
17c40 76 2d 3e 65 78 70 69 72 65 64 20 3d 20 30 3b 0a  v->expired = 0;.
17c41 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
17c42 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20  ==SQLITE_SCHEMA 
17c43 26 26 20 76 2d 3e 7a 53 71 6c 20 26 26 20 64 62  && v->zSql && db
17c44 2d 3e 70 45 72 72 20 29 7b 0a 20 20 20 20 20 20  ->pErr ){.      
17c45 2f 2a 20 54 68 69 73 20 63 61 73 65 20 6f 63 63  /* This case occ
17c46 75 72 73 20 61 66 74 65 72 20 66 61 69 6c 69 6e  urs after failin
17c47 67 20 74 6f 20 72 65 63 6f 6d 70 69 6c 65 20 61  g to recompile a
17c48 6e 20 73 71 6c 20 73 74 61 74 65 6d 65 6e 74 2e  n sql statement.
17c49 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20 65   .      ** The e
17c4a 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f  rror message fro
17c4b 6d 20 74 68 65 20 53 51 4c 20 63 6f 6d 70 69 6c  m the SQL compil
17c4c 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  er has already b
17c4d 65 65 6e 20 6c 6f 61 64 65 64 20 0a 20 20 20 20  een loaded .    
17c4e 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 64 61    ** into the da
17c4f 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 54  tabase handle. T
17c50 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70 69 65 73  his block copies
17c51 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   the error messa
17c52 67 65 20 0a 20 20 20 20 20 20 2a 2a 20 66 72 6f  ge .      ** fro
17c53 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  m the database h
17c54 61 6e 64 6c 65 20 69 6e 74 6f 20 74 68 65 20 73  andle into the s
17c55 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 73 65 74  tatement and set
17c56 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 0a  s the statement.
17c57 20 20 20 20 20 20 2a 2a 20 70 72 6f 67 72 61 6d        ** program
17c58 20 63 6f 75 6e 74 65 72 20 74 6f 20 30 20 74 6f   counter to 0 to
17c59 20 65 6e 73 75 72 65 20 74 68 61 74 20 77 68 65   ensure that whe
17c5a 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  n the statement 
17c5b 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 66 69 6e  is .      ** fin
17c5c 61 6c 69 7a 65 64 20 6f 72 20 72 65 73 65 74 20  alized or reset 
17c5d 74 68 65 20 70 61 72 73 65 72 20 65 72 72 6f 72  the parser error
17c5e 20 6d 65 73 73 61 67 65 20 69 73 20 61 76 61 69   message is avai
17c5f 6c 61 62 6c 65 20 76 69 61 0a 20 20 20 20 20 20  lable via.      
17c60 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  ** sqlite3_errms
17c61 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  g() and sqlite3_
17c62 65 72 72 63 6f 64 65 28 29 2e 0a 20 20 20 20 20  errcode()..     
17c63 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20   */.      const 
17c64 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 28 63 6f  char *zErr = (co
17c65 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74  nst char *)sqlit
17c66 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 64 62  e3_value_text(db
17c67 2d 3e 70 45 72 72 29 3b 20 0a 20 20 20 20 20 20  ->pErr); .      
17c68 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
17c69 2c 20 76 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20  , v->zErrMsg);. 
17c6a 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61       if( !db->ma
17c6b 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
17c6c 20 20 20 20 20 20 76 2d 3e 7a 45 72 72 4d 73 67        v->zErrMsg
17c6d 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
17c6e 75 70 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20  up(db, zErr);.  
17c6f 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20      } else {.   
17c70 20 20 20 20 20 76 2d 3e 7a 45 72 72 4d 73 67 20       v->zErrMsg 
17c71 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 76 2d 3e  = 0;.        v->
17c72 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
17c73 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  M;.      }.    }
17c74 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
17c75 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29  3ApiExit(db, rc)
17c76 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
17c77 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
17c78 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  tex);.  }.  retu
17c79 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a  rn rc;.}.#endif.
17c7a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74  ./*.** Extract t
17c7b 68 65 20 75 73 65 72 20 64 61 74 61 20 66 72 6f  he user data fro
17c7c 6d 20 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  m a sqlite3_cont
17c7d 65 78 74 20 73 74 72 75 63 74 75 72 65 20 61 6e  ext structure an
17c7e 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f  d return a.** po
17c7f 69 6e 74 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a  inter to it..*/.
17c80 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
17c81 2a 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61  *sqlite3_user_da
17c82 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ta(sqlite3_conte
17c83 78 74 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  xt *p){.  assert
17c84 28 20 70 20 26 26 20 70 2d 3e 70 46 75 6e 63 20  ( p && p->pFunc 
17c85 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70  );.  return p->p
17c86 46 75 6e 63 2d 3e 70 55 73 65 72 44 61 74 61 3b  Func->pUserData;
17c87 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63  .}../*.** Extrac
17c88 74 20 74 68 65 20 75 73 65 72 20 64 61 74 61 20  t the user data 
17c89 66 72 6f 6d 20 61 20 73 71 6c 69 74 65 33 5f 63  from a sqlite3_c
17c8a 6f 6e 74 65 78 74 20 73 74 72 75 63 74 75 72 65  ontext structure
17c8b 20 61 6e 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a   and return a.**
17c8c 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 0a   pointer to it..
17c8d 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  */.SQLITE_API sq
17c8e 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 63  lite3 *sqlite3_c
17c8f 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
17c90 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
17c91 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
17c92 70 20 26 26 20 70 2d 3e 70 46 75 6e 63 20 29 3b  p && p->pFunc );
17c93 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 73 2e 64  .  return p->s.d
17c94 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  b;.}../*.** The 
17c95 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 68 65  following is the
17c96 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
17c97 6f 66 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69  of an SQL functi
17c98 6f 6e 20 74 68 61 74 20 61 6c 77 61 79 73 0a 2a  on that always.*
17c99 2a 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 20  * fails with an 
17c9a 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74  error message st
17c9b 61 74 69 6e 67 20 74 68 61 74 20 74 68 65 20 66  ating that the f
17c9c 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
17c9d 69 6e 20 74 68 65 0a 2a 2a 20 77 72 6f 6e 67 20  in the.** wrong 
17c9e 63 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 73 71  context.  The sq
17c9f 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66  lite3_overload_f
17ca0 75 6e 63 74 69 6f 6e 28 29 20 41 50 49 20 6d 69  unction() API mi
17ca1 67 68 74 20 63 6f 6e 73 74 72 75 63 74 0a 2a 2a  ght construct.**
17ca2 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68   SQL function th
17ca3 61 74 20 75 73 65 20 74 68 69 73 20 72 6f 75 74  at use this rout
17ca4 69 6e 65 20 73 6f 20 74 68 61 74 20 74 68 65 20  ine so that the 
17ca5 66 75 6e 63 74 69 6f 6e 73 20 77 69 6c 6c 20 65  functions will e
17ca6 78 69 73 74 0a 2a 2a 20 66 6f 72 20 6e 61 6d 65  xist.** for name
17ca7 20 72 65 73 6f 6c 75 74 69 6f 6e 20 62 75 74 20   resolution but 
17ca8 61 72 65 20 61 63 74 75 61 6c 6c 79 20 6f 76 65  are actually ove
17ca9 72 6c 6f 61 64 65 64 20 62 79 20 74 68 65 20 78  rloaded by the x
17caa 46 69 6e 64 46 75 6e 63 74 69 6f 6e 0a 2a 2a 20  FindFunction.** 
17cab 6d 65 74 68 6f 64 20 6f 66 20 76 69 72 74 75 61  method of virtua
17cac 6c 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a 53 51 4c  l tables..*/.SQL
17cad 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
17cae 20 73 71 6c 69 74 65 33 49 6e 76 61 6c 69 64 46   sqlite3InvalidF
17caf 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74  unction(.  sqlit
17cb0 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
17cb1 65 78 74 2c 20 20 2f 2a 20 54 68 65 20 66 75 6e  ext,  /* The fun
17cb2 63 74 69 6f 6e 20 63 61 6c 6c 69 6e 67 20 63 6f  ction calling co
17cb3 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 61  ntext */.  int a
17cb4 72 67 63 2c 20 20 20 20 20 20 20 20 20 20 20 20  rgc,            
17cb5 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
17cb6 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  of arguments to 
17cb7 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  the function */.
17cb8 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
17cb9 2a 2a 61 72 67 76 20 20 20 20 20 20 20 2f 2a 20  **argv       /* 
17cba 56 61 6c 75 65 20 6f 66 20 65 61 63 68 20 61 72  Value of each ar
17cbb 67 75 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 63  gument */.){.  c
17cbc 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
17cbd 20 3d 20 63 6f 6e 74 65 78 74 2d 3e 70 46 75 6e   = context->pFun
17cbe 63 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 63 68 61 72  c->zName;.  char
17cbf 20 2a 7a 45 72 72 3b 0a 20 20 7a 45 72 72 20 3d   *zErr;.  zErr =
17cc0 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
17cc1 30 2c 0a 20 20 20 20 20 20 22 75 6e 61 62 6c 65  0,.      "unable
17cc2 20 74 6f 20 75 73 65 20 66 75 6e 63 74 69 6f 6e   to use function
17cc3 20 25 73 20 69 6e 20 74 68 65 20 72 65 71 75 65   %s in the reque
17cc4 73 74 65 64 20 63 6f 6e 74 65 78 74 22 2c 20 7a  sted context", z
17cc5 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Name);.  sqlite3
17cc6 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f  _result_error(co
17cc7 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29  ntext, zErr, -1)
17cc8 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
17cc9 28 7a 45 72 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  (zErr);.}../*.**
17cca 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20 72 65 74   Allocate or ret
17ccb 75 72 6e 20 74 68 65 20 61 67 67 72 65 67 61 74  urn the aggregat
17ccc 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 61 20  e context for a 
17ccd 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20 20  user function.  
17cce 41 20 6e 65 77 0a 2a 2a 20 63 6f 6e 74 65 78 74  A new.** context
17ccf 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e   is allocated on
17cd0 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 2e   the first call.
17cd1 20 20 53 75 62 73 65 71 75 65 6e 74 20 63 61 6c    Subsequent cal
17cd2 6c 73 20 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a  ls return the.**
17cd3 20 73 61 6d 65 20 63 6f 6e 74 65 78 74 20 74 68   same context th
17cd4 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 64 20  at was returned 
17cd5 6f 6e 20 70 72 69 6f 72 20 63 61 6c 6c 73 2e 0a  on prior calls..
17cd6 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
17cd7 69 64 20 2a 73 71 6c 69 74 65 33 5f 61 67 67 72  id *sqlite3_aggr
17cd8 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 73 71  egate_context(sq
17cd9 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
17cda 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20  , int nByte){.  
17cdb 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 61 73 73  Mem *pMem;.  ass
17cdc 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70 46 75  ert( p && p->pFu
17cdd 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e 63 2d 3e  nc && p->pFunc->
17cde 78 53 74 65 70 20 29 3b 0a 20 20 61 73 73 65 72  xStep );.  asser
17cdf 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
17ce0 5f 68 65 6c 64 28 70 2d 3e 73 2e 64 62 2d 3e 6d  _held(p->s.db->m
17ce1 75 74 65 78 29 20 29 3b 0a 20 20 70 4d 65 6d 20  utex) );.  pMem 
17ce2 3d 20 70 2d 3e 70 4d 65 6d 3b 0a 20 20 69 66 28  = p->pMem;.  if(
17ce3 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20   (pMem->flags & 
17ce4 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 29 7b 0a 20  MEM_Agg)==0 ){. 
17ce5 20 20 20 69 66 28 20 6e 42 79 74 65 3d 3d 30 20     if( nByte==0 
17ce6 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
17ce7 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78  VdbeMemReleaseEx
17ce8 74 65 72 6e 61 6c 28 70 4d 65 6d 29 3b 0a 20 20  ternal(pMem);.  
17ce9 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
17cea 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20  = MEM_Null;.    
17ceb 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 30 3b 0a 20    pMem->z = 0;. 
17cec 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
17ced 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
17cee 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20  ow(pMem, nByte, 
17cef 30 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  0);.      pMem->
17cf0 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 41 67 67 3b  flags = MEM_Agg;
17cf1 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 70  .      pMem->u.p
17cf2 44 65 66 20 3d 20 70 2d 3e 70 46 75 6e 63 3b 0a  Def = p->pFunc;.
17cf3 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e        if( pMem->
17cf4 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d  z ){.        mem
17cf5 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20  set(pMem->z, 0, 
17cf6 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 7d 0a  nByte);.      }.
17cf7 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
17cf8 72 6e 20 28 76 6f 69 64 2a 29 70 4d 65 6d 2d 3e  rn (void*)pMem->
17cf9 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  z;.}../*.** Retu
17cfa 72 6e 20 74 68 65 20 61 75 78 69 6c 61 72 79 20  rn the auxilary 
17cfb 64 61 74 61 20 70 6f 69 6e 74 65 72 2c 20 69 66  data pointer, if
17cfc 20 61 6e 79 2c 20 66 6f 72 20 74 68 65 20 69 41   any, for the iA
17cfd 72 67 27 74 68 20 61 72 67 75 6d 65 6e 74 20 74  rg'th argument t
17cfe 6f 0a 2a 2a 20 74 68 65 20 75 73 65 72 2d 66 75  o.** the user-fu
17cff 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 62  nction defined b
17d00 79 20 70 43 74 78 2e 0a 2a 2f 0a 53 51 4c 49 54  y pCtx..*/.SQLIT
17d01 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
17d02 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28  te3_get_auxdata(
17d03 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
17d04 2a 70 43 74 78 2c 20 69 6e 74 20 69 41 72 67 29  *pCtx, int iArg)
17d05 7b 0a 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56  {.  VdbeFunc *pV
17d06 64 62 65 46 75 6e 63 3b 0a 0a 20 20 61 73 73 65  dbeFunc;..  asse
17d07 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
17d08 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64  x_held(pCtx->s.d
17d09 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70  b->mutex) );.  p
17d0a 56 64 62 65 46 75 6e 63 20 3d 20 70 43 74 78 2d  VdbeFunc = pCtx-
17d0b 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 69 66  >pVdbeFunc;.  if
17d0c 28 20 21 70 56 64 62 65 46 75 6e 63 20 7c 7c 20  ( !pVdbeFunc || 
17d0d 69 41 72 67 3e 3d 70 56 64 62 65 46 75 6e 63 2d  iArg>=pVdbeFunc-
17d0e 3e 6e 41 75 78 20 7c 7c 20 69 41 72 67 3c 30 20  >nAux || iArg<0 
17d0f 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
17d10 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 56  .  }.  return pV
17d11 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b 69  dbeFunc->apAux[i
17d12 41 72 67 5d 2e 70 41 75 78 3b 0a 7d 0a 0a 2f 2a  Arg].pAux;.}../*
17d13 0a 2a 2a 20 53 65 74 20 74 68 65 20 61 75 78 69  .** Set the auxi
17d14 6c 61 72 79 20 64 61 74 61 20 70 6f 69 6e 74 65  lary data pointe
17d15 72 20 61 6e 64 20 64 65 6c 65 74 65 20 66 75 6e  r and delete fun
17d16 63 74 69 6f 6e 2c 20 66 6f 72 20 74 68 65 20 69  ction, for the i
17d17 41 72 67 27 74 68 0a 2a 2a 20 61 72 67 75 6d 65  Arg'th.** argume
17d18 6e 74 20 74 6f 20 74 68 65 20 75 73 65 72 2d 66  nt to the user-f
17d19 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 64 20  unction defined 
17d1a 62 79 20 70 43 74 78 2e 20 41 6e 79 20 70 72 65  by pCtx. Any pre
17d1b 76 69 6f 75 73 20 76 61 6c 75 65 20 69 73 0a 2a  vious value is.*
17d1c 2a 20 64 65 6c 65 74 65 64 20 62 79 20 63 61 6c  * deleted by cal
17d1d 6c 69 6e 67 20 74 68 65 20 64 65 6c 65 74 65 20  ling the delete 
17d1e 66 75 6e 63 74 69 6f 6e 20 73 70 65 63 69 66 69  function specifi
17d1f 65 64 20 77 68 65 6e 20 69 74 20 77 61 73 20 73  ed when it was s
17d20 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  et..*/.SQLITE_AP
17d21 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73  I void sqlite3_s
17d22 65 74 5f 61 75 78 64 61 74 61 28 0a 20 20 73 71  et_auxdata(.  sq
17d23 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
17d24 43 74 78 2c 20 0a 20 20 69 6e 74 20 69 41 72 67  Ctx, .  int iArg
17d25 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c  , .  void *pAux,
17d26 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 65   .  void (*xDele
17d27 74 65 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20  te)(void*).){.  
17d28 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20 2a  struct AuxData *
17d29 70 41 75 78 44 61 74 61 3b 0a 20 20 56 64 62 65  pAuxData;.  Vdbe
17d2a 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 3b  Func *pVdbeFunc;
17d2b 0a 20 20 69 66 28 20 69 41 72 67 3c 30 20 29 20  .  if( iArg<0 ) 
17d2c 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 0a 20 20  goto failed;..  
17d2d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
17d2e 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d  mutex_held(pCtx-
17d2f 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  >s.db->mutex) );
17d30 0a 20 20 70 56 64 62 65 46 75 6e 63 20 3d 20 70  .  pVdbeFunc = p
17d31 43 74 78 2d 3e 70 56 64 62 65 46 75 6e 63 3b 0a  Ctx->pVdbeFunc;.
17d32 20 20 69 66 28 20 21 70 56 64 62 65 46 75 6e 63    if( !pVdbeFunc
17d33 20 7c 7c 20 70 56 64 62 65 46 75 6e 63 2d 3e 6e   || pVdbeFunc->n
17d34 41 75 78 3c 3d 69 41 72 67 20 29 7b 0a 20 20 20  Aux<=iArg ){.   
17d35 20 69 6e 74 20 6e 41 75 78 20 3d 20 28 70 56 64   int nAux = (pVd
17d36 62 65 46 75 6e 63 20 3f 20 70 56 64 62 65 46 75  beFunc ? pVdbeFu
17d37 6e 63 2d 3e 6e 41 75 78 20 3a 20 30 29 3b 0a 20  nc->nAux : 0);. 
17d38 20 20 20 69 6e 74 20 6e 4d 61 6c 6c 6f 63 20 3d     int nMalloc =
17d39 20 73 69 7a 65 6f 66 28 56 64 62 65 46 75 6e 63   sizeof(VdbeFunc
17d3a 29 20 2b 20 73 69 7a 65 6f 66 28 73 74 72 75 63  ) + sizeof(struc
17d3b 74 20 41 75 78 44 61 74 61 29 2a 69 41 72 67 3b  t AuxData)*iArg;
17d3c 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 20 3d  .    pVdbeFunc =
17d3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f   sqlite3DbReallo
17d3e 63 28 70 43 74 78 2d 3e 73 2e 64 62 2c 20 70 56  c(pCtx->s.db, pV
17d3f 64 62 65 46 75 6e 63 2c 20 6e 4d 61 6c 6c 6f 63  dbeFunc, nMalloc
17d40 29 3b 0a 20 20 20 20 69 66 28 20 21 70 56 64 62  );.    if( !pVdb
17d41 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 67  eFunc ){.      g
17d42 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20  oto failed;.    
17d43 7d 0a 20 20 20 20 70 43 74 78 2d 3e 70 56 64 62  }.    pCtx->pVdb
17d44 65 46 75 6e 63 20 3d 20 70 56 64 62 65 46 75 6e  eFunc = pVdbeFun
17d45 63 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 70  c;.    memset(&p
17d46 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b  VdbeFunc->apAux[
17d47 6e 41 75 78 5d 2c 20 30 2c 20 73 69 7a 65 6f 66  nAux], 0, sizeof
17d48 28 73 74 72 75 63 74 20 41 75 78 44 61 74 61 29  (struct AuxData)
17d49 2a 28 69 41 72 67 2b 31 2d 6e 41 75 78 29 29 3b  *(iArg+1-nAux));
17d4a 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 2d 3e  .    pVdbeFunc->
17d4b 6e 41 75 78 20 3d 20 69 41 72 67 2b 31 3b 0a 20  nAux = iArg+1;. 
17d4c 20 20 20 70 56 64 62 65 46 75 6e 63 2d 3e 70 46     pVdbeFunc->pF
17d4d 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70 46 75 6e  unc = pCtx->pFun
17d4e 63 3b 0a 20 20 7d 0a 0a 20 20 70 41 75 78 44 61  c;.  }..  pAuxDa
17d4f 74 61 20 3d 20 26 70 56 64 62 65 46 75 6e 63 2d  ta = &pVdbeFunc-
17d50 3e 61 70 41 75 78 5b 69 41 72 67 5d 3b 0a 20 20  >apAux[iArg];.  
17d51 69 66 28 20 70 41 75 78 44 61 74 61 2d 3e 70 41  if( pAuxData->pA
17d52 75 78 20 26 26 20 70 41 75 78 44 61 74 61 2d 3e  ux && pAuxData->
17d53 78 44 65 6c 65 74 65 20 29 7b 0a 20 20 20 20 70  xDelete ){.    p
17d54 41 75 78 44 61 74 61 2d 3e 78 44 65 6c 65 74 65  AuxData->xDelete
17d55 28 70 41 75 78 44 61 74 61 2d 3e 70 41 75 78 29  (pAuxData->pAux)
17d56 3b 0a 20 20 7d 0a 20 20 70 41 75 78 44 61 74 61  ;.  }.  pAuxData
17d57 2d 3e 70 41 75 78 20 3d 20 70 41 75 78 3b 0a 20  ->pAux = pAux;. 
17d58 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c 65   pAuxData->xDele
17d59 74 65 20 3d 20 78 44 65 6c 65 74 65 3b 0a 20 20  te = xDelete;.  
17d5a 72 65 74 75 72 6e 3b 0a 0a 66 61 69 6c 65 64 3a  return;..failed:
17d5b 0a 20 20 69 66 28 20 78 44 65 6c 65 74 65 20 29  .  if( xDelete )
17d5c 7b 0a 20 20 20 20 78 44 65 6c 65 74 65 28 70 41  {.    xDelete(pA
17d5d 75 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e  ux);.  }.}..#ifn
17d5e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
17d5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a  DEPRECATED./*.**
17d60 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
17d61 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 65 20  er of times the 
17d62 53 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 6f 66  Step function of
17d63 20 61 20 61 67 67 72 65 67 61 74 65 20 68 61 73   a aggregate has
17d64 20 62 65 65 6e 20 0a 2a 2a 20 63 61 6c 6c 65 64   been .** called
17d65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
17d66 63 74 69 6f 6e 20 69 73 20 64 65 70 72 65 63 61  ction is depreca
17d67 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20 75 73 65  ted.  Do not use
17d68 20 69 74 20 66 6f 72 20 6e 65 77 20 63 6f 64 65   it for new code
17d69 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 72 6f 76  .  It is.** prov
17d6a 69 64 65 20 6f 6e 6c 79 20 74 6f 20 61 76 6f 69  ide only to avoi
17d6b 64 20 62 72 65 61 6b 69 6e 67 20 6c 65 67 61 63  d breaking legac
17d6c 79 20 63 6f 64 65 2e 20 20 4e 65 77 20 61 67 67  y code.  New agg
17d6d 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 0a  regate function.
17d6e 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
17d6f 6e 73 20 73 68 6f 75 6c 64 20 6b 65 65 70 20 74  ns should keep t
17d70 68 65 69 72 20 6f 77 6e 20 63 6f 75 6e 74 73 20  heir own counts 
17d71 77 69 74 68 69 6e 20 74 68 65 69 72 20 61 67 67  within their agg
17d72 72 65 67 61 74 65 0a 2a 2a 20 63 6f 6e 74 65 78  regate.** contex
17d73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
17d74 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 67 67   int sqlite3_agg
17d75 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 71 6c  regate_count(sql
17d76 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29  ite3_context *p)
17d77 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26  {.  assert( p &&
17d78 20 70 2d 3e 70 46 75 6e 63 20 26 26 20 70 2d 3e   p->pFunc && p->
17d79 70 46 75 6e 63 2d 3e 78 53 74 65 70 20 29 3b 0a  pFunc->xStep );.
17d7a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 4d 65 6d    return p->pMem
17d7b 2d 3e 6e 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  ->n;.}.#endif../
17d7c 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
17d7d 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
17d7e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  s in the result 
17d7f 73 65 74 20 66 6f 72 20 74 68 65 20 73 74 61 74  set for the stat
17d80 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a  ement pStmt..*/.
17d81 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
17d82 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f  qlite3_column_co
17d83 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  unt(sqlite3_stmt
17d84 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65   *pStmt){.  Vdbe
17d85 20 2a 70 56 6d 20 3d 20 28 56 64 62 65 20 2a 29   *pVm = (Vdbe *)
17d86 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20  pStmt;.  return 
17d87 70 56 6d 20 3f 20 70 56 6d 2d 3e 6e 52 65 73 43  pVm ? pVm->nResC
17d88 6f 6c 75 6d 6e 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a  olumn : 0;.}../*
17d89 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
17d8a 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20  umber of values 
17d8b 61 76 61 69 6c 61 62 6c 65 20 66 72 6f 6d 20 74  available from t
17d8c 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f  he current row o
17d8d 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74  f the.** current
17d8e 6c 79 20 65 78 65 63 75 74 69 6e 67 20 73 74 61  ly executing sta
17d8f 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f  tement pStmt..*/
17d90 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
17d91 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75  sqlite3_data_cou
17d92 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  nt(sqlite3_stmt 
17d93 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20  *pStmt){.  Vdbe 
17d94 2a 70 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70  *pVm = (Vdbe *)p
17d95 53 74 6d 74 3b 0a 20 20 69 66 28 20 70 56 6d 3d  Stmt;.  if( pVm=
17d96 3d 30 20 7c 7c 20 70 56 6d 2d 3e 70 52 65 73 75  =0 || pVm->pResu
17d97 6c 74 53 65 74 3d 3d 30 20 29 20 72 65 74 75 72  ltSet==0 ) retur
17d98 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 56  n 0;.  return pV
17d99 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 7d  m->nResColumn;.}
17d9a 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74  .../*.** Check t
17d9b 6f 20 73 65 65 20 69 66 20 63 6f 6c 75 6d 6e 20  o see if column 
17d9c 69 43 6f 6c 20 6f 66 20 74 68 65 20 67 69 76 65  iCol of the give
17d9d 6e 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 76  n statement is v
17d9e 61 6c 69 64 2e 20 20 49 66 0a 2a 2a 20 69 74 20  alid.  If.** it 
17d9f 69 73 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69  is, return a poi
17da0 6e 74 65 72 20 74 6f 20 74 68 65 20 4d 65 6d 20  nter to the Mem 
17da1 66 6f 72 20 74 68 65 20 76 61 6c 75 65 20 6f 66  for the value of
17da2 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a   that column..**
17da3 20 49 66 20 69 43 6f 6c 20 69 73 20 6e 6f 74 20   If iCol is not 
17da4 76 61 6c 69 64 2c 20 72 65 74 75 72 6e 20 61 20  valid, return a 
17da5 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d  pointer to a Mem
17da6 20 77 68 69 63 68 20 68 61 73 20 61 20 76 61 6c   which has a val
17da7 75 65 0a 2a 2a 20 6f 66 20 4e 55 4c 4c 2e 0a 2a  ue.** of NULL..*
17da8 2f 0a 73 74 61 74 69 63 20 4d 65 6d 20 2a 63 6f  /.static Mem *co
17da9 6c 75 6d 6e 4d 65 6d 28 73 71 6c 69 74 65 33 5f  lumnMem(sqlite3_
17daa 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
17dab 20 69 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d   i){.  Vdbe *pVm
17dac 3b 0a 20 20 69 6e 74 20 76 61 6c 73 3b 0a 20 20  ;.  int vals;.  
17dad 4d 65 6d 20 2a 70 4f 75 74 3b 0a 0a 20 20 70 56  Mem *pOut;..  pV
17dae 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d  m = (Vdbe *)pStm
17daf 74 3b 0a 20 20 69 66 28 20 70 56 6d 20 26 26 20  t;.  if( pVm && 
17db0 70 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 21  pVm->pResultSet!
17db1 3d 30 20 26 26 20 69 3c 70 56 6d 2d 3e 6e 52 65  =0 && i<pVm->nRe
17db2 73 43 6f 6c 75 6d 6e 20 26 26 20 69 3e 3d 30 20  sColumn && i>=0 
17db3 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  ){.    sqlite3_m
17db4 75 74 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e  utex_enter(pVm->
17db5 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20  db->mutex);.    
17db6 76 61 6c 73 20 3d 20 73 71 6c 69 74 65 33 5f 64  vals = sqlite3_d
17db7 61 74 61 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29  ata_count(pStmt)
17db8 3b 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 70 56  ;.    pOut = &pV
17db9 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 5b 69 5d  m->pResultSet[i]
17dba 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
17dbb 74 61 74 69 63 20 63 6f 6e 73 74 20 4d 65 6d 20  tatic const Mem 
17dbc 6e 75 6c 6c 4d 65 6d 20 3d 20 7b 7b 30 7d 2c 20  nullMem = {{0}, 
17dbd 30 2e 30 2c 20 30 2c 20 22 22 2c 20 30 2c 20 4d  0.0, 0, "", 0, M
17dbe 45 4d 5f 4e 75 6c 6c 2c 20 53 51 4c 49 54 45 5f  EM_Null, SQLITE_
17dbf 4e 55 4c 4c 2c 20 30 2c 20 30 2c 20 30 20 7d 3b  NULL, 0, 0, 0 };
17dc0 0a 20 20 20 20 69 66 28 20 70 56 6d 2d 3e 64 62  .    if( pVm->db
17dc1 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
17dc2 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 56  3_mutex_enter(pV
17dc3 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  m->db->mutex);. 
17dc4 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
17dc5 72 28 70 56 6d 2d 3e 64 62 2c 20 53 51 4c 49 54  r(pVm->db, SQLIT
17dc6 45 5f 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 20  E_RANGE, 0);.   
17dc7 20 7d 0a 20 20 20 20 70 4f 75 74 20 3d 20 28 4d   }.    pOut = (M
17dc8 65 6d 2a 29 26 6e 75 6c 6c 4d 65 6d 3b 0a 20 20  em*)&nullMem;.  
17dc9 7d 0a 20 20 72 65 74 75 72 6e 20 70 4f 75 74 3b  }.  return pOut;
17dca 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
17dcb 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
17dcc 64 20 61 66 74 65 72 20 69 6e 76 6f 6b 69 6e 67  d after invoking
17dcd 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   an sqlite3_valu
17dce 65 5f 58 58 58 20 66 75 6e 63 74 69 6f 6e 20 6f  e_XXX function o
17dcf 6e 20 61 20 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 76  n a .** column v
17dd0 61 6c 75 65 20 28 69 2e 65 2e 20 61 20 76 61 6c  alue (i.e. a val
17dd1 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 65  ue returned by e
17dd2 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 53 51 4c  valuating an SQL
17dd3 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74   expression in t
17dd4 68 65 0a 2a 2a 20 73 65 6c 65 63 74 20 6c 69 73  he.** select lis
17dd5 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74  t of a SELECT st
17dd6 61 74 65 6d 65 6e 74 29 20 74 68 61 74 20 6d 61  atement) that ma
17dd7 79 20 63 61 75 73 65 20 61 20 6d 61 6c 6c 6f 63  y cause a malloc
17dd8 28 29 20 66 61 69 6c 75 72 65 2e 20 49 66 20 0a  () failure. If .
17dd9 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20  ** malloc() has 
17dda 66 61 69 6c 65 64 2c 20 74 68 65 20 74 68 72 65  failed, the thre
17ddb 61 64 73 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  ads mallocFailed
17ddc 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 65 64   flag is cleared
17ddd 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 0a   and the result.
17dde 2a 2a 20 63 6f 64 65 20 6f 66 20 73 74 61 74 65  ** code of state
17ddf 6d 65 6e 74 20 70 53 74 6d 74 20 73 65 74 20 74  ment pStmt set t
17de0 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a  o SQLITE_NOMEM..
17de1 2a 2a 0a 2a 2a 20 53 70 65 63 69 66 69 63 61 6c  **.** Specifical
17de2 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61 6c 6c  ly, this is call
17de3 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 3a 0a  ed from within:.
17de4 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65  **.**     sqlite
17de5 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 29 0a 2a  3_column_int().*
17de6 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f  *     sqlite3_co
17de7 6c 75 6d 6e 5f 69 6e 74 36 34 28 29 0a 2a 2a 20  lumn_int64().** 
17de8 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75      sqlite3_colu
17de9 6d 6e 5f 74 65 78 74 28 29 0a 2a 2a 20 20 20 20  mn_text().**    
17dea 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
17deb 74 65 78 74 31 36 28 29 0a 2a 2a 20 20 20 20 20  text16().**     
17dec 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 72  sqlite3_column_r
17ded 65 61 6c 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c  eal().**     sql
17dee 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
17def 73 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  s().**     sqlit
17df0 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31  e3_column_bytes1
17df1 36 28 29 0a 2a 2a 0a 2a 2a 20 42 75 74 20 6e 6f  6().**.** But no
17df2 74 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f  t for sqlite3_co
17df3 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 77 68 69  lumn_blob(), whi
17df4 63 68 20 6e 65 76 65 72 20 63 61 6c 6c 73 20 6d  ch never calls m
17df5 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74  alloc()..*/.stat
17df6 69 63 20 76 6f 69 64 20 63 6f 6c 75 6d 6e 4d 61  ic void columnMa
17df7 6c 6c 6f 63 46 61 69 6c 75 72 65 28 73 71 6c 69  llocFailure(sqli
17df8 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29  te3_stmt *pStmt)
17df9 0a 7b 0a 20 20 2f 2a 20 49 66 20 6d 61 6c 6c 6f  .{.  /* If mallo
17dfa 63 28 29 20 66 61 69 6c 65 64 20 64 75 72 69 6e  c() failed durin
17dfb 67 20 61 6e 20 65 6e 63 6f 64 69 6e 67 20 63 6f  g an encoding co
17dfc 6e 76 65 72 73 69 6f 6e 20 77 69 74 68 69 6e 20  nversion within 
17dfd 61 6e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f  an.  ** sqlite3_
17dfe 63 6f 6c 75 6d 6e 5f 58 58 58 20 41 50 49 2c 20  column_XXX API, 
17dff 74 68 65 6e 20 73 65 74 20 74 68 65 20 72 65 74  then set the ret
17e00 75 72 6e 20 63 6f 64 65 20 6f 66 20 74 68 65 20  urn code of the 
17e01 73 74 61 74 65 6d 65 6e 74 20 74 6f 0a 20 20 2a  statement to.  *
17e02 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20  * SQLITE_NOMEM. 
17e03 54 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f  The next call to
17e04 20 5f 73 74 65 70 28 29 20 28 69 66 20 61 6e 79   _step() (if any
17e05 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 51  ) will return SQ
17e06 4c 49 54 45 5f 45 52 52 4f 52 0a 20 20 2a 2a 20  LITE_ERROR.  ** 
17e07 61 6e 64 20 5f 66 69 6e 61 6c 69 7a 65 28 29 20  and _finalize() 
17e08 77 69 6c 6c 20 72 65 74 75 72 6e 20 4e 4f 4d 45  will return NOME
17e09 4d 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65 20 2a  M..  */.  Vdbe *
17e0a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d  p = (Vdbe *)pStm
17e0b 74 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  t;.  if( p ){.  
17e0c 20 20 70 2d 3e 72 63 20 3d 20 73 71 6c 69 74 65    p->rc = sqlite
17e0d 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20  3ApiExit(p->db, 
17e0e 70 2d 3e 72 63 29 3b 0a 20 20 20 20 73 71 6c 69  p->rc);.    sqli
17e0f 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
17e10 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  p->db->mutex);. 
17e11 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a   }.}../*********
17e12 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17e13 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  *** sqlite3_colu
17e14 6d 6e 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  mn_  ***********
17e15 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17e16 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ****.** The foll
17e17 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 61  owing routines a
17e18 72 65 20 75 73 65 64 20 74 6f 20 61 63 63 65 73  re used to acces
17e19 73 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68  s elements of th
17e1a 65 20 63 75 72 72 65 6e 74 20 72 6f 77 0a 2a 2a  e current row.**
17e1b 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
17e1c 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  et..*/.SQLITE_AP
17e1d 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
17e1e 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
17e1f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  b(sqlite3_stmt *
17e20 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20  pStmt, int i){. 
17e21 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 76 61 6c   const void *val
17e22 3b 0a 20 20 76 61 6c 20 3d 20 73 71 6c 69 74 65  ;.  val = sqlite
17e23 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 20 63 6f  3_value_blob( co
17e24 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29  lumnMem(pStmt,i)
17e25 20 29 3b 0a 20 20 2f 2a 20 45 76 65 6e 20 74 68   );.  /* Even th
17e26 6f 75 67 68 20 74 68 65 72 65 20 69 73 20 6e 6f  ough there is no
17e27 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72   encoding conver
17e28 73 69 6f 6e 2c 20 76 61 6c 75 65 5f 62 6c 6f 62  sion, value_blob
17e29 28 29 20 6d 69 67 68 74 0a 20 20 2a 2a 20 6e 65  () might.  ** ne
17e2a 65 64 20 74 6f 20 63 61 6c 6c 20 6d 61 6c 6c 6f  ed to call mallo
17e2b 63 28 29 20 74 6f 20 65 78 70 61 6e 64 20 74 68  c() to expand th
17e2c 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 7a 65  e result of a ze
17e2d 72 6f 62 6c 6f 62 28 29 20 0a 20 20 2a 2a 20 65  roblob() .  ** e
17e2e 78 70 72 65 73 73 69 6f 6e 2e 20 0a 20 20 2a 2f  xpression. .  */
17e2f 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46  .  columnMallocF
17e30 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20  ailure(pStmt);. 
17e31 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53   return val;.}.S
17e32 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
17e33 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
17e34 65 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  es(sqlite3_stmt 
17e35 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a  *pStmt, int i){.
17e36 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69    int val = sqli
17e37 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
17e38 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74   columnMem(pStmt
17e39 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d  ,i) );.  columnM
17e3a 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74  allocFailure(pSt
17e3b 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61  mt);.  return va
17e3c 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  l;.}.SQLITE_API 
17e3d 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  int sqlite3_colu
17e3e 6d 6e 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74  mn_bytes16(sqlit
17e3f 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
17e40 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61  int i){.  int va
17e41 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  l = sqlite3_valu
17e42 65 5f 62 79 74 65 73 31 36 28 20 63 6f 6c 75 6d  e_bytes16( colum
17e43 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b  nMem(pStmt,i) );
17e44 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46  .  columnMallocF
17e45 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20  ailure(pStmt);. 
17e46 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53   return val;.}.S
17e47 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65  QLITE_API double
17e48 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
17e49 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73  double(sqlite3_s
17e4a 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
17e4b 69 29 7b 0a 20 20 64 6f 75 62 6c 65 20 76 61 6c  i){.  double val
17e4c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
17e4d 5f 64 6f 75 62 6c 65 28 20 63 6f 6c 75 6d 6e 4d  _double( columnM
17e4e 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20  em(pStmt,i) );. 
17e4f 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69   columnMallocFai
17e50 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72  lure(pStmt);.  r
17e51 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c  eturn val;.}.SQL
17e52 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
17e53 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73  te3_column_int(s
17e54 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
17e55 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e  mt, int i){.  in
17e56 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f  t val = sqlite3_
17e57 76 61 6c 75 65 5f 69 6e 74 28 20 63 6f 6c 75 6d  value_int( colum
17e58 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b  nMem(pStmt,i) );
17e59 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46  .  columnMallocF
17e5a 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20  ailure(pStmt);. 
17e5b 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53   return val;.}.S
17e5c 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
17e5d 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 63  _int64 sqlite3_c
17e5e 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c 69  olumn_int64(sqli
17e5f 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
17e60 20 69 6e 74 20 69 29 7b 0a 20 20 73 71 6c 69 74   int i){.  sqlit
17e61 65 5f 69 6e 74 36 34 20 76 61 6c 20 3d 20 73 71  e_int64 val = sq
17e62 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36  lite3_value_int6
17e63 34 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74  4( columnMem(pSt
17e64 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d  mt,i) );.  colum
17e65 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70  nMallocFailure(p
17e66 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  Stmt);.  return 
17e67 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  val;.}.SQLITE_AP
17e68 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  I const unsigned
17e69 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
17e6a 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74  olumn_text(sqlit
17e6b 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
17e6c 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74 20  int i){.  const 
17e6d 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 76  unsigned char *v
17e6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  al = sqlite3_val
17e6f 75 65 5f 74 65 78 74 28 20 63 6f 6c 75 6d 6e 4d  ue_text( columnM
17e70 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20  em(pStmt,i) );. 
17e71 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69   columnMallocFai
17e72 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72  lure(pStmt);.  r
17e73 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c  eturn val;.}.SQL
17e74 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
17e75 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 5f 63  value *sqlite3_c
17e76 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 73 71 6c 69  olumn_value(sqli
17e77 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
17e78 20 69 6e 74 20 69 29 7b 0a 20 20 4d 65 6d 20 2a   int i){.  Mem *
17e79 70 4f 75 74 20 3d 20 63 6f 6c 75 6d 6e 4d 65 6d  pOut = columnMem
17e7a 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20 69 66  (pStmt, i);.  if
17e7b 28 20 70 4f 75 74 2d 3e 66 6c 61 67 73 26 4d 45  ( pOut->flags&ME
17e7c 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20  M_Static ){.    
17e7d 70 4f 75 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e  pOut->flags &= ~
17e7e 4d 45 4d 5f 53 74 61 74 69 63 3b 0a 20 20 20 20  MEM_Static;.    
17e7f 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d  pOut->flags |= M
17e80 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d 0a 20 20  EM_Ephem;.  }.  
17e81 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c  columnMallocFail
17e82 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65  ure(pStmt);.  re
17e83 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f 76 61  turn (sqlite3_va
17e84 6c 75 65 20 2a 29 70 4f 75 74 3b 0a 7d 0a 23 69  lue *)pOut;.}.#i
17e85 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
17e86 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41  T_UTF16.SQLITE_A
17e87 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
17e88 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
17e89 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d  xt16(sqlite3_stm
17e8a 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
17e8b 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  {.  const void *
17e8c 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  val = sqlite3_va
17e8d 6c 75 65 5f 74 65 78 74 31 36 28 20 63 6f 6c 75  lue_text16( colu
17e8e 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29  mnMem(pStmt,i) )
17e8f 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63  ;.  columnMalloc
17e90 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a  Failure(pStmt);.
17e91 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a    return val;.}.
17e92 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
17e93 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53  _OMIT_UTF16 */.S
17e94 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
17e95 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
17e96 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
17e97 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20  pStmt, int i){. 
17e98 20 69 6e 74 20 69 54 79 70 65 20 3d 20 73 71 6c   int iType = sql
17e99 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
17e9a 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74   columnMem(pStmt
17e9b 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d  ,i) );.  columnM
17e9c 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74  allocFailure(pSt
17e9d 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 54  mt);.  return iT
17e9e 79 70 65 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66  ype;.}../* The f
17e9f 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f  ollowing functio
17ea0 6e 20 69 73 20 65 78 70 65 72 69 6d 65 6e 74 61  n is experimenta
17ea1 6c 20 61 6e 64 20 73 75 62 6a 65 63 74 20 74 6f  l and subject to
17ea2 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 72 65   change or.** re
17ea3 6d 6f 76 61 6c 20 2a 2f 0a 2f 2a 69 6e 74 20 73  moval */./*int s
17ea4 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 75  qlite3_column_nu
17ea5 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69 74  meric_type(sqlit
17ea6 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
17ea7 69 6e 74 20 69 29 7b 0a 2a 2a 20 20 72 65 74 75  int i){.**  retu
17ea8 72 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  rn sqlite3_value
17ea9 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 20 63  _numeric_type( c
17eaa 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69  olumnMem(pStmt,i
17eab 29 20 29 3b 0a 2a 2a 7d 0a 2a 2f 0a 0a 2f 2a 0a  ) );.**}.*/../*.
17eac 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 4e  ** Convert the N
17ead 2d 74 68 20 65 6c 65 6d 65 6e 74 20 6f 66 20 70  -th element of p
17eae 53 74 6d 74 2d 3e 70 43 6f 6c 4e 61 6d 65 5b 5d  Stmt->pColName[]
17eaf 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 75   into a string u
17eb0 73 69 6e 67 0a 2a 2a 20 78 46 75 6e 63 28 29 20  sing.** xFunc() 
17eb1 74 68 65 6e 20 72 65 74 75 72 6e 20 74 68 61 74  then return that
17eb2 20 73 74 72 69 6e 67 2e 20 20 49 66 20 4e 20 69   string.  If N i
17eb3 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20  s out of range, 
17eb4 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20  return 0..**.** 
17eb5 54 68 65 72 65 20 61 72 65 20 75 70 20 74 6f 20  There are up to 
17eb6 35 20 6e 61 6d 65 73 20 66 6f 72 20 65 61 63 68  5 names for each
17eb7 20 63 6f 6c 75 6d 6e 2e 20 20 75 73 65 54 79 70   column.  useTyp
17eb8 65 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69  e determines whi
17eb9 63 68 0a 2a 2a 20 6e 61 6d 65 20 69 73 20 72 65  ch.** name is re
17eba 74 75 72 6e 65 64 2e 20 20 48 65 72 65 20 61 72  turned.  Here ar
17ebb 65 20 74 68 65 20 6e 61 6d 65 73 3a 0a 2a 2a 0a  e the names:.**.
17ebc 2a 2a 20 20 20 20 30 20 20 20 20 20 20 54 68 65  **    0      The
17ebd 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 61 73 20   column name as 
17ebe 69 74 20 73 68 6f 75 6c 64 20 62 65 20 64 69 73  it should be dis
17ebf 70 6c 61 79 65 64 20 66 6f 72 20 6f 75 74 70 75  played for outpu
17ec0 74 0a 2a 2a 20 20 20 20 31 20 20 20 20 20 20 54  t.**    1      T
17ec1 68 65 20 64 61 74 61 74 79 70 65 20 6e 61 6d 65  he datatype name
17ec2 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 0a   for the column.
17ec3 2a 2a 20 20 20 20 32 20 20 20 20 20 20 54 68 65  **    2      The
17ec4 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   name of the dat
17ec5 61 62 61 73 65 20 74 68 61 74 20 74 68 65 20 63  abase that the c
17ec6 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72  olumn derives fr
17ec7 6f 6d 0a 2a 2a 20 20 20 20 33 20 20 20 20 20 20  om.**    3      
17ec8 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  The name of the 
17ec9 74 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 63  table that the c
17eca 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72  olumn derives fr
17ecb 6f 6d 0a 2a 2a 20 20 20 20 34 20 20 20 20 20 20  om.**    4      
17ecc 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  The name of the 
17ecd 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61  table column tha
17ece 74 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c  t the result col
17ecf 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72 6f 6d  umn derives from
17ed0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65  .**.** If the re
17ed1 73 75 6c 74 20 69 73 20 6e 6f 74 20 61 20 73 69  sult is not a si
17ed2 6d 70 6c 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65  mple column refe
17ed3 72 65 6e 63 65 20 28 69 66 20 69 74 20 69 73 20  rence (if it is 
17ed4 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a  an expression.**
17ed5 20 6f 72 20 61 20 63 6f 6e 73 74 61 6e 74 29 20   or a constant) 
17ed6 74 68 65 6e 20 75 73 65 54 79 70 65 73 20 32 2c  then useTypes 2,
17ed7 20 33 2c 20 61 6e 64 20 34 20 72 65 74 75 72 6e   3, and 4 return
17ed8 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63   NULL..*/.static
17ed9 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 63 6f 6c   const void *col
17eda 75 6d 6e 4e 61 6d 65 28 0a 20 20 73 71 6c 69 74  umnName(.  sqlit
17edb 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 0a  e3_stmt *pStmt,.
17edc 20 20 69 6e 74 20 4e 2c 0a 20 20 63 6f 6e 73 74    int N,.  const
17edd 20 76 6f 69 64 20 2a 28 2a 78 46 75 6e 63 29 28   void *(*xFunc)(
17ede 4d 65 6d 2a 29 2c 0a 20 20 69 6e 74 20 75 73 65  Mem*),.  int use
17edf 54 79 70 65 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  Type.){.  const 
17ee0 76 6f 69 64 20 2a 72 65 74 20 3d 20 30 3b 0a 20  void *ret = 0;. 
17ee1 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65   Vdbe *p = (Vdbe
17ee2 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20   *)pStmt;.  int 
17ee3 6e 3b 0a 20 20 0a 0a 20 20 69 66 28 20 70 21 3d  n;.  ..  if( p!=
17ee4 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 73 71 6c  0 ){.    n = sql
17ee5 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e  ite3_column_coun
17ee6 74 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 69 66  t(pStmt);.    if
17ee7 28 20 4e 3c 6e 20 26 26 20 4e 3e 3d 30 20 29 7b  ( N<n && N>=0 ){
17ee8 0a 20 20 20 20 20 20 4e 20 2b 3d 20 75 73 65 54  .      N += useT
17ee9 79 70 65 2a 6e 3b 0a 20 20 20 20 20 20 73 71 6c  ype*n;.      sql
17eea 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
17eeb 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  (p->db->mutex);.
17eec 20 20 20 20 20 20 72 65 74 20 3d 20 78 46 75 6e        ret = xFun
17eed 63 28 26 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b 4e  c(&p->aColName[N
17eee 5d 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 41 20  ]);..      /* A 
17eef 6d 61 6c 6c 6f 63 20 6d 61 79 20 68 61 76 65 20  malloc may have 
17ef0 66 61 69 6c 65 64 20 69 6e 73 69 64 65 20 6f 66  failed inside of
17ef1 20 74 68 65 20 78 46 75 6e 63 28 29 20 63 61 6c   the xFunc() cal
17ef2 6c 2e 20 49 66 20 74 68 69 73 0a 20 20 20 20 20  l. If this.     
17ef3 20 2a 2a 20 69 73 20 74 68 65 20 63 61 73 65 2c   ** is the case,
17ef4 20 63 6c 65 61 72 20 74 68 65 20 6d 61 6c 6c 6f   clear the mallo
17ef5 63 46 61 69 6c 65 64 20 66 6c 61 67 20 61 6e 64  cFailed flag and
17ef6 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 20 20   return NULL..  
17ef7 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
17ef8 20 70 2d 3e 64 62 20 26 26 20 70 2d 3e 64 62 2d   p->db && p->db-
17ef9 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
17efa 0a 20 20 20 20 20 20 20 20 70 2d 3e 64 62 2d 3e  .        p->db->
17efb 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30  mallocFailed = 0
17efc 3b 0a 20 20 20 20 20 20 20 20 72 65 74 20 3d 20  ;.        ret = 
17efd 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
17efe 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
17eff 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  eave(p->db->mute
17f00 78 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  x);.    }.  }.  
17f01 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f  return ret;.}../
17f02 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
17f03 6e 61 6d 65 20 6f 66 20 74 68 65 20 4e 74 68 20  name of the Nth 
17f04 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65  column of the re
17f05 73 75 6c 74 20 73 65 74 20 72 65 74 75 72 6e 65  sult set returne
17f06 64 20 62 79 20 53 51 4c 0a 2a 2a 20 73 74 61 74  d by SQL.** stat
17f07 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a  ement pStmt..*/.
17f08 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
17f09 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
17f0a 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 74  olumn_name(sqlit
17f0b 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
17f0c 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e  int N){.  return
17f0d 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20   columnName(.   
17f0e 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f     pStmt, N, (co
17f0f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d  nst void*(*)(Mem
17f10 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *))sqlite3_value
17f11 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  _text, COLNAME_N
17f12 41 4d 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20  AME);.}.#ifndef 
17f13 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
17f14 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  6.SQLITE_API con
17f15 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
17f16 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73  _column_name16(s
17f17 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
17f18 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65  mt, int N){.  re
17f19 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28  turn columnName(
17f1a 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c  .      pStmt, N,
17f1b 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29   (const void*(*)
17f1c 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76  (Mem*))sqlite3_v
17f1d 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c  alue_text16, COL
17f1e 4e 41 4d 45 5f 4e 41 4d 45 29 3b 0a 7d 0a 23 65  NAME_NAME);.}.#e
17f1f 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73  ndif../*.** Cons
17f20 74 72 61 69 6e 74 3a 20 20 49 66 20 79 6f 75 20  traint:  If you 
17f21 68 61 76 65 20 45 4e 41 42 4c 45 5f 43 4f 4c 55  have ENABLE_COLU
17f22 4d 4e 5f 4d 45 54 41 44 41 54 41 20 74 68 65 6e  MN_METADATA then
17f23 20 79 6f 75 20 6d 75 73 74 0a 2a 2a 20 6e 6f 74   you must.** not
17f24 20 64 65 66 69 6e 65 20 4f 4d 49 54 5f 44 45 43   define OMIT_DEC
17f25 4c 54 59 50 45 2e 0a 2a 2f 0a 23 69 66 20 64 65  LTYPE..*/.#if de
17f26 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
17f27 54 5f 44 45 43 4c 54 59 50 45 29 20 26 26 20 64  T_DECLTYPE) && d
17f28 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
17f29 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41  ABLE_COLUMN_META
17f2a 44 41 54 41 29 0a 23 20 65 72 72 6f 72 20 22 4d  DATA).# error "M
17f2b 75 73 74 20 6e 6f 74 20 64 65 66 69 6e 65 20 62  ust not define b
17f2c 6f 74 68 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  oth SQLITE_OMIT_
17f2d 44 45 43 4c 54 59 50 45 20 5c 0a 20 20 20 20 20  DECLTYPE \.     
17f2e 20 20 20 20 61 6e 64 20 53 51 4c 49 54 45 5f 45      and SQLITE_E
17f2f 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54  NABLE_COLUMN_MET
17f30 41 44 41 54 41 22 0a 23 65 6e 64 69 66 0a 0a 23  ADATA".#endif..#
17f31 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
17f32 49 54 5f 44 45 43 4c 54 59 50 45 0a 2f 2a 0a 2a  IT_DECLTYPE./*.*
17f33 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 6f 6c  * Return the col
17f34 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  umn declaration 
17f35 74 79 70 65 20 28 69 66 20 61 70 70 6c 69 63 61  type (if applica
17f36 62 6c 65 29 20 6f 66 20 74 68 65 20 27 69 27 74  ble) of the 'i't
17f37 68 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 66 20 74  h column.** of t
17f38 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  he result set of
17f39 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 70   SQL statement p
17f3a 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  Stmt..*/.SQLITE_
17f3b 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  API const char *
17f3c 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
17f3d 65 63 6c 74 79 70 65 28 73 71 6c 69 74 65 33 5f  ecltype(sqlite3_
17f3e 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
17f3f 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f   N){.  return co
17f40 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20  lumnName(.      
17f41 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74  pStmt, N, (const
17f42 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29   void*(*)(Mem*))
17f43 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
17f44 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c  xt, COLNAME_DECL
17f45 54 59 50 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66  TYPE);.}.#ifndef
17f46 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
17f47 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  16.SQLITE_API co
17f48 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
17f49 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70  3_column_decltyp
17f4a 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  e16(sqlite3_stmt
17f4b 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b   *pStmt, int N){
17f4c 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e  .  return column
17f4d 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d  Name(.      pStm
17f4e 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69  t, N, (const voi
17f4f 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69  d*(*)(Mem*))sqli
17f50 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
17f51 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59  , COLNAME_DECLTY
17f52 50 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  PE);.}.#endif /*
17f53 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
17f54 31 36 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20  16 */.#endif /* 
17f55 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c  SQLITE_OMIT_DECL
17f56 54 59 50 45 20 2a 2f 0a 0a 23 69 66 64 65 66 20  TYPE */..#ifdef 
17f57 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f  SQLITE_ENABLE_CO
17f58 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 2f 2a  LUMN_METADATA./*
17f59 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
17f5a 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
17f5b 61 73 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61  ase from which a
17f5c 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64   result column d
17f5d 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20  erives..** NULL 
17f5e 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74  is returned if t
17f5f 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  he result column
17f60 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f   is an expressio
17f61 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 6f 72  n or constant or
17f62 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c 73  .** anything els
17f63 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20 61  e which is not a
17f64 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72 65 66  n unabiguous ref
17f65 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61 74 61  erence to a data
17f66 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a  base column..*/.
17f67 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
17f68 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
17f69 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e  olumn_database_n
17f6a 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ame(sqlite3_stmt
17f6b 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b   *pStmt, int N){
17f6c 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e  .  return column
17f6d 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d  Name(.      pStm
17f6e 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69  t, N, (const voi
17f6f 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69  d*(*)(Mem*))sqli
17f70 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20  te3_value_text, 
17f71 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45  COLNAME_DATABASE
17f72 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  );.}.#ifndef SQL
17f73 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53  ITE_OMIT_UTF16.S
17f74 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
17f75 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f  void *sqlite3_co
17f76 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61  lumn_database_na
17f77 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d  me16(sqlite3_stm
17f78 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29  t *pStmt, int N)
17f79 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d  {.  return colum
17f7a 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74  nName(.      pSt
17f7b 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f  mt, N, (const vo
17f7c 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c  id*(*)(Mem*))sql
17f7d 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
17f7e 36 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42  6, COLNAME_DATAB
17f7f 41 53 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  ASE);.}.#endif /
17f80 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  * SQLITE_OMIT_UT
17f81 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65  F16 */../*.** Re
17f82 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66  turn the name of
17f83 20 74 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20   the table from 
17f84 77 68 69 63 68 20 61 20 72 65 73 75 6c 74 20 63  which a result c
17f85 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e 0a 2a  olumn derives..*
17f86 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e  * NULL is return
17f87 65 64 20 69 66 20 74 68 65 20 72 65 73 75 6c 74  ed if the result
17f88 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78   column is an ex
17f89 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73  pression or cons
17f8a 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 68  tant or.** anyth
17f8b 69 6e 67 20 65 6c 73 65 20 77 68 69 63 68 20 69  ing else which i
17f8c 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69 67 75  s not an unabigu
17f8d 6f 75 73 20 72 65 66 65 72 65 6e 63 65 20 74 6f  ous reference to
17f8e 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6c 75   a database colu
17f8f 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  mn..*/.SQLITE_AP
17f90 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  I const char *sq
17f91 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62  lite3_column_tab
17f92 6c 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f  le_name(sqlite3_
17f93 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
17f94 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f   N){.  return co
17f95 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20  lumnName(.      
17f96 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74  pStmt, N, (const
17f97 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29   void*(*)(Mem*))
17f98 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
17f99 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c  xt, COLNAME_TABL
17f9a 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51  E);.}.#ifndef SQ
17f9b 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
17f9c 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
17f9d 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63   void *sqlite3_c
17f9e 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65  olumn_table_name
17f9f 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  16(sqlite3_stmt 
17fa0 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a  *pStmt, int N){.
17fa1 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e    return columnN
17fa2 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74  ame(.      pStmt
17fa3 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64  , N, (const void
17fa4 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74  *(*)(Mem*))sqlit
17fa5 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c  e3_value_text16,
17fa6 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 29 3b   COLNAME_TABLE);
17fa7 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
17fa8 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a  ITE_OMIT_UTF16 *
17fa9 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  /../*.** Return 
17faa 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
17fab 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 66 72 6f  table column fro
17fac 6d 20 77 68 69 63 68 20 61 20 72 65 73 75 6c 74  m which a result
17fad 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e   column derives.
17fae 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  .** NULL is retu
17faf 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 73 75  rned if the resu
17fb0 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20  lt column is an 
17fb1 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f  expression or co
17fb2 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79  nstant or.** any
17fb3 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 63 68  thing else which
17fb4 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69   is not an unabi
17fb5 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 65 20  guous reference 
17fb6 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 63 6f  to a database co
17fb7 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lumn..*/.SQLITE_
17fb8 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  API const char *
17fb9 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f  sqlite3_column_o
17fba 72 69 67 69 6e 5f 6e 61 6d 65 28 73 71 6c 69 74  rigin_name(sqlit
17fbb 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
17fbc 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e  int N){.  return
17fbd 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20   columnName(.   
17fbe 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f     pStmt, N, (co
17fbf 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d  nst void*(*)(Mem
17fc0 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *))sqlite3_value
17fc1 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 43  _text, COLNAME_C
17fc2 4f 4c 55 4d 4e 29 3b 0a 7d 0a 23 69 66 6e 64 65  OLUMN);.}.#ifnde
17fc3 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
17fc4 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  F16.SQLITE_API c
17fc5 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
17fc6 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e  e3_column_origin
17fc7 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f  _name16(sqlite3_
17fc8 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
17fc9 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f   N){.  return co
17fca 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20  lumnName(.      
17fcb 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74  pStmt, N, (const
17fcc 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29   void*(*)(Mem*))
17fcd 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
17fce 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f  xt16, COLNAME_CO
17fcf 4c 55 4d 4e 29 3b 0a 7d 0a 23 65 6e 64 69 66 20  LUMN);.}.#endif 
17fd0 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  /* SQLITE_OMIT_U
17fd1 54 46 31 36 20 2a 2f 0a 23 65 6e 64 69 66 20 2f  TF16 */.#endif /
17fd2 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  * SQLITE_ENABLE_
17fd3 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 20  COLUMN_METADATA 
17fd4 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */.../**********
17fd5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17fd6 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69  ***** sqlite3_bi
17fd7 6e 64 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nd_  ***********
17fd8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17fd9 0a 2a 2a 20 0a 2a 2a 20 52 6f 75 74 69 6e 65 73  .** .** Routines
17fda 20 75 73 65 64 20 74 6f 20 61 74 74 61 63 68 20   used to attach 
17fdb 76 61 6c 75 65 73 20 74 6f 20 77 69 6c 64 63 61  values to wildca
17fdc 72 64 73 20 69 6e 20 61 20 63 6f 6d 70 69 6c 65  rds in a compile
17fdd 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  d SQL statement.
17fde 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 55 6e 62 69 6e 64  .*/./*.** Unbind
17fdf 20 74 68 65 20 76 61 6c 75 65 20 62 6f 75 6e 64   the value bound
17fe0 20 74 6f 20 76 61 72 69 61 62 6c 65 20 69 20 69   to variable i i
17fe1 6e 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  n virtual machin
17fe2 65 20 70 2e 20 54 68 69 73 20 69 73 20 74 68 65  e p. This is the
17fe3 20 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73   .** the same as
17fe4 20 62 69 6e 64 69 6e 67 20 61 20 4e 55 4c 4c 20   binding a NULL 
17fe5 76 61 6c 75 65 20 74 6f 20 74 68 65 20 63 6f 6c  value to the col
17fe6 75 6d 6e 2e 20 49 66 20 74 68 65 20 22 69 22 20  umn. If the "i" 
17fe7 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20  parameter is.** 
17fe8 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 68  out of range, th
17fe9 65 6e 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 20  en SQLITE_RANGE 
17fea 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68  is returned. Oth
17feb 65 77 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e  ewise SQLITE_OK.
17fec 0a 2a 2a 0a 2a 2a 20 41 20 73 75 63 63 65 73 73  .**.** A success
17fed 66 75 6c 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f  ful evaluation o
17fee 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61  f this routine a
17fef 63 71 75 69 72 65 73 20 74 68 65 20 6d 75 74 65  cquires the mute
17ff0 78 20 6f 6e 20 70 2e 0a 2a 2a 20 74 68 65 20 6d  x on p..** the m
17ff1 75 74 65 78 20 69 73 20 72 65 6c 65 61 73 65 64  utex is released
17ff2 20 69 66 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20   if any kind of 
17ff3 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a  error occurs..**
17ff4 0a 2a 2a 20 54 68 65 20 65 72 72 6f 72 20 63 6f  .** The error co
17ff5 64 65 20 73 74 6f 72 65 64 20 69 6e 20 64 61 74  de stored in dat
17ff6 61 62 61 73 65 20 70 2d 3e 64 62 20 69 73 20 6f  abase p->db is o
17ff7 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 20  verwritten with 
17ff8 74 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61  the return.** va
17ff9 6c 75 65 20 69 6e 20 61 6e 79 20 63 61 73 65 2e  lue in any case.
17ffa 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76  .*/.static int v
17ffb 64 62 65 55 6e 62 69 6e 64 28 56 64 62 65 20 2a  dbeUnbind(Vdbe *
17ffc 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 4d 65 6d  p, int i){.  Mem
17ffd 20 2a 70 56 61 72 3b 0a 20 20 69 66 28 20 70 3d   *pVar;.  if( p=
17ffe 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  =0 ) return SQLI
17fff 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 73 71 6c  TE_MISUSE;.  sql
18000 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
18001 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  (p->db->mutex);.
18002 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d    if( p->magic!=
18003 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 7c  VDBE_MAGIC_RUN |
18004 7c 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20  | p->pc>=0 ){.  
18005 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70    sqlite3Error(p
18006 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 4d 49 53  ->db, SQLITE_MIS
18007 55 53 45 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  USE, 0);.    sql
18008 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
18009 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  (p->db->mutex);.
1800a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1800b 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
1800c 69 66 28 20 69 3c 31 20 7c 7c 20 69 3e 70 2d 3e  if( i<1 || i>p->
1800d 6e 56 61 72 20 29 7b 0a 20 20 20 20 73 71 6c 69  nVar ){.    sqli
1800e 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20  te3Error(p->db, 
1800f 53 51 4c 49 54 45 5f 52 41 4e 47 45 2c 20 30 29  SQLITE_RANGE, 0)
18010 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
18011 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d  tex_leave(p->db-
18012 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 65 74  >mutex);.    ret
18013 75 72 6e 20 53 51 4c 49 54 45 5f 52 41 4e 47 45  urn SQLITE_RANGE
18014 3b 0a 20 20 7d 0a 20 20 69 2d 2d 3b 0a 20 20 70  ;.  }.  i--;.  p
18015 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 72 5b 69  Var = &p->aVar[i
18016 5d 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ];.  sqlite3Vdbe
18017 4d 65 6d 52 65 6c 65 61 73 65 28 70 56 61 72 29  MemRelease(pVar)
18018 3b 0a 20 20 70 56 61 72 2d 3e 66 6c 61 67 73 20  ;.  pVar->flags 
18019 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 73 71  = MEM_Null;.  sq
1801a 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62  lite3Error(p->db
1801b 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b  , SQLITE_OK, 0);
1801c 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1801d 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 69  _OK;.}../*.** Bi
1801e 6e 64 20 61 20 74 65 78 74 20 6f 72 20 42 4c 4f  nd a text or BLO
1801f 42 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74  B value..*/.stat
18020 69 63 20 69 6e 74 20 62 69 6e 64 54 65 78 74 28  ic int bindText(
18021 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
18022 2a 70 53 74 6d 74 2c 20 20 20 2f 2a 20 54 68 65  *pStmt,   /* The
18023 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 69   statement to bi
18024 6e 64 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20  nd against */.  
18025 69 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20 20  int i,          
18026 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
18027 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  of the parameter
18028 20 74 6f 20 62 69 6e 64 20 2a 2f 0a 20 20 63 6f   to bind */.  co
18029 6e 73 74 20 76 6f 69 64 20 2a 7a 44 61 74 61 2c  nst void *zData,
1802a 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
1802b 74 6f 20 74 68 65 20 64 61 74 61 20 74 6f 20 62  to the data to b
1802c 65 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 69 6e 74  e bound */.  int
1802d 20 6e 44 61 74 61 2c 20 20 20 20 20 20 20 20 20   nData,         
1802e 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1802f 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 74   bytes of data t
18030 6f 20 62 65 20 62 6f 75 6e 64 20 2a 2f 0a 20 20  o be bound */.  
18031 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69  void (*xDel)(voi
18032 64 2a 29 2c 20 20 20 2f 2a 20 44 65 73 74 72 75  d*),   /* Destru
18033 63 74 6f 72 20 66 6f 72 20 74 68 65 20 64 61 74  ctor for the dat
18034 61 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 63 6f 64  a */.  int encod
18035 69 6e 67 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ing           /*
18036 20 45 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68   Encoding for th
18037 65 20 64 61 74 61 20 2a 2f 0a 29 7b 0a 20 20 56  e data */.){.  V
18038 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a  dbe *p = (Vdbe *
18039 29 70 53 74 6d 74 3b 0a 20 20 4d 65 6d 20 2a 70  )pStmt;.  Mem *p
1803a 56 61 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  Var;.  int rc;..
1803b 20 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e    rc = vdbeUnbin
1803c 64 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72  d(p, i);.  if( r
1803d 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1803e 20 20 20 20 69 66 28 20 7a 44 61 74 61 21 3d 30      if( zData!=0
1803f 20 29 7b 0a 20 20 20 20 20 20 70 56 61 72 20 3d   ){.      pVar =
18040 20 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 3b 0a   &p->aVar[i-1];.
18041 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
18042 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
18043 70 56 61 72 2c 20 7a 44 61 74 61 2c 20 6e 44 61  pVar, zData, nDa
18044 74 61 2c 20 65 6e 63 6f 64 69 6e 67 2c 20 78 44  ta, encoding, xD
18045 65 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  el);.      if( r
18046 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
18047 65 6e 63 6f 64 69 6e 67 21 3d 30 20 29 7b 0a 20  encoding!=0 ){. 
18048 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
18049 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63  te3VdbeChangeEnc
1804a 6f 64 69 6e 67 28 70 56 61 72 2c 20 45 4e 43 28  oding(pVar, ENC(
1804b 70 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20 20 7d  p->db));.      }
1804c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
1804d 72 6f 72 28 70 2d 3e 64 62 2c 20 72 63 2c 20 30  ror(p->db, rc, 0
1804e 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  );.      rc = sq
1804f 6c 69 74 65 33 41 70 69 45 78 69 74 28 70 2d 3e  lite3ApiExit(p->
18050 64 62 2c 20 72 63 29 3b 0a 20 20 20 20 7d 0a 20  db, rc);.    }. 
18051 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
18052 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75  _leave(p->db->mu
18053 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  tex);.  }.  retu
18054 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  rn rc;.}.../*.**
18055 20 42 69 6e 64 20 61 20 62 6c 6f 62 20 76 61 6c   Bind a blob val
18056 75 65 20 74 6f 20 61 6e 20 53 51 4c 20 73 74 61  ue to an SQL sta
18057 74 65 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 2e  tement variable.
18058 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
18059 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
1805a 62 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33 5f  blob(.  sqlite3_
1805b 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20  stmt *pStmt, .  
1805c 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20  int i, .  const 
1805d 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a 20 20  void *zData, .  
1805e 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f  int nData, .  vo
1805f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a  id (*xDel)(void*
18060 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69  ).){.  return bi
18061 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c  ndText(pStmt, i,
18062 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78   zData, nData, x
18063 44 65 6c 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54  Del, 0);.}.SQLIT
18064 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
18065 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 28 73 71  3_bind_double(sq
18066 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
18067 74 2c 20 69 6e 74 20 69 2c 20 64 6f 75 62 6c 65  t, int i, double
18068 20 72 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20   rValue){.  int 
18069 72 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20  rc;.  Vdbe *p = 
1806a 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20  (Vdbe *)pStmt;. 
1806b 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64   rc = vdbeUnbind
1806c 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63  (p, i);.  if( rc
1806d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1806e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1806f 6d 53 65 74 44 6f 75 62 6c 65 28 26 70 2d 3e 61  mSetDouble(&p->a
18070 56 61 72 5b 69 2d 31 5d 2c 20 72 56 61 6c 75 65  Var[i-1], rValue
18071 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
18072 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62  utex_leave(p->db
18073 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20  ->mutex);.  }.  
18074 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c  return rc;.}.SQL
18075 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
18076 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 71 6c  te3_bind_int(sql
18077 69 74 65 33 5f 73 74 6d 74 20 2a 70 2c 20 69 6e  ite3_stmt *p, in
18078 74 20 69 2c 20 69 6e 74 20 69 56 61 6c 75 65 29  t i, int iValue)
18079 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
1807a 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 2c  e3_bind_int64(p,
1807b 20 69 2c 20 28 69 36 34 29 69 56 61 6c 75 65 29   i, (i64)iValue)
1807c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ;.}.SQLITE_API i
1807d 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
1807e 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74  int64(sqlite3_st
1807f 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
18080 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  , sqlite_int64 i
18081 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63  Value){.  int rc
18082 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56  ;.  Vdbe *p = (V
18083 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72  dbe *)pStmt;.  r
18084 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70  c = vdbeUnbind(p
18085 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  , i);.  if( rc==
18086 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
18087 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
18088 65 74 49 6e 74 36 34 28 26 70 2d 3e 61 56 61 72  etInt64(&p->aVar
18089 5b 69 2d 31 5d 2c 20 69 56 61 6c 75 65 29 3b 0a  [i-1], iValue);.
1808a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1808b 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d  x_leave(p->db->m
1808c 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74  utex);.  }.  ret
1808d 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45  urn rc;.}.SQLITE
1808e 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1808f 5f 62 69 6e 64 5f 6e 75 6c 6c 28 73 71 6c 69 74  _bind_null(sqlit
18090 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
18091 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 72 63  int i){.  int rc
18092 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56  ;.  Vdbe *p = (V
18093 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63  dbe*)pStmt;.  rc
18094 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c   = vdbeUnbind(p,
18095 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53   i);.  if( rc==S
18096 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
18097 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
18098 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  ave(p->db->mutex
18099 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
1809a 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  rc;.}.SQLITE_API
1809b 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
1809c 64 5f 74 65 78 74 28 20 0a 20 20 73 71 6c 69 74  d_text( .  sqlit
1809d 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1809e 0a 20 20 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e  .  int i, .  con
1809f 73 74 20 63 68 61 72 20 2a 7a 44 61 74 61 2c 20  st char *zData, 
180a0 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20  .  int nData, . 
180a1 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
180a2 69 64 2a 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e  id*).){.  return
180a3 20 62 69 6e 64 54 65 78 74 28 70 53 74 6d 74 2c   bindText(pStmt,
180a4 20 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 61   i, zData, nData
180a5 2c 20 78 44 65 6c 2c 20 53 51 4c 49 54 45 5f 55  , xDel, SQLITE_U
180a6 54 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20  TF8);.}.#ifndef 
180a7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
180a8 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  6.SQLITE_API int
180a9 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65   sqlite3_bind_te
180aa 78 74 31 36 28 0a 20 20 73 71 6c 69 74 65 33 5f  xt16(.  sqlite3_
180ab 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20  stmt *pStmt, .  
180ac 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20  int i, .  const 
180ad 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a 20 20  void *zData, .  
180ae 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f  int nData, .  vo
180af 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a  id (*xDel)(void*
180b0 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69  ).){.  return bi
180b1 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c  ndText(pStmt, i,
180b2 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78   zData, nData, x
180b3 44 65 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  Del, SQLITE_UTF1
180b4 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 23 65 6e 64  6NATIVE);.}.#end
180b5 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
180b6 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54  T_UTF16 */.SQLIT
180b7 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
180b8 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 71 6c  3_bind_value(sql
180b9 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
180ba 2c 20 69 6e 74 20 69 2c 20 63 6f 6e 73 74 20 73  , int i, const s
180bb 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
180bc 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b  alue){.  int rc;
180bd 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64  .  Vdbe *p = (Vd
180be 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63  be *)pStmt;.  rc
180bf 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c   = vdbeUnbind(p,
180c0 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53   i);.  if( rc==S
180c1 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
180c2 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
180c3 4d 65 6d 43 6f 70 79 28 26 70 2d 3e 61 56 61 72  MemCopy(&p->aVar
180c4 5b 69 2d 31 5d 2c 20 70 56 61 6c 75 65 29 3b 0a  [i-1], pValue);.
180c5 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
180c6 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
180c7 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  c = sqlite3VdbeC
180c8 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 70  hangeEncoding(&p
180c9 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c 20 45 4e 43  ->aVar[i-1], ENC
180ca 28 70 2d 3e 64 62 29 29 3b 0a 20 20 20 20 7d 0a  (p->db));.    }.
180cb 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
180cc 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d  x_leave(p->db->m
180cd 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 63 20  utex);.  }.  rc 
180ce 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
180cf 28 70 2d 3e 64 62 2c 20 72 63 29 3b 0a 20 20 72  (p->db, rc);.  r
180d0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49  eturn rc;.}.SQLI
180d1 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
180d2 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62  e3_bind_zeroblob
180d3 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
180d4 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20 69 6e 74  Stmt, int i, int
180d5 20 6e 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20   n){.  int rc;. 
180d6 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65   Vdbe *p = (Vdbe
180d7 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d   *)pStmt;.  rc =
180d8 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69   vdbeUnbind(p, i
180d9 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
180da 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71  ITE_OK ){.    sq
180db 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a  lite3VdbeMemSetZ
180dc 65 72 6f 42 6c 6f 62 28 26 70 2d 3e 61 56 61 72  eroBlob(&p->aVar
180dd 5b 69 2d 31 5d 2c 20 6e 29 3b 0a 20 20 20 20 73  [i-1], n);.    s
180de 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
180df 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ve(p->db->mutex)
180e0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
180e1 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  c;.}../*.** Retu
180e2 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
180e3 20 77 69 6c 64 63 61 72 64 73 20 74 68 61 74 20   wildcards that 
180e4 63 61 6e 20 62 65 20 70 6f 74 65 6e 74 69 61 6c  can be potential
180e5 6c 79 20 62 6f 75 6e 64 20 74 6f 2e 0a 2a 2a 20  ly bound to..** 
180e6 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
180e7 61 64 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74  added to support
180e8 20 44 42 44 3a 3a 53 51 4c 69 74 65 2e 20 20 0a   DBD::SQLite.  .
180e9 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
180ea 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70  t sqlite3_bind_p
180eb 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 73  arameter_count(s
180ec 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
180ed 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d  mt){.  Vdbe *p =
180ee 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20   (Vdbe*)pStmt;. 
180ef 20 72 65 74 75 72 6e 20 70 20 3f 20 70 2d 3e 6e   return p ? p->n
180f0 56 61 72 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  Var : 0;.}../*.*
180f1 2a 20 43 72 65 61 74 65 20 61 20 6d 61 70 70 69  * Create a mappi
180f2 6e 67 20 66 72 6f 6d 20 76 61 72 69 61 62 6c 65  ng from variable
180f3 20 6e 75 6d 62 65 72 73 20 74 6f 20 76 61 72 69   numbers to vari
180f4 61 62 6c 65 20 6e 61 6d 65 73 0a 2a 2a 20 69 6e  able names.** in
180f5 20 74 68 65 20 56 64 62 65 2e 61 7a 56 61 72 5b   the Vdbe.azVar[
180f6 5d 20 61 72 72 61 79 2c 20 69 66 20 73 75 63 68  ] array, if such
180f7 20 61 20 6d 61 70 70 69 6e 67 20 64 6f 65 73 20   a mapping does 
180f8 6e 6f 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 65  not already.** e
180f9 78 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  xist..*/.static 
180fa 76 6f 69 64 20 63 72 65 61 74 65 56 61 72 4d 61  void createVarMa
180fb 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 66  p(Vdbe *p){.  if
180fc 28 20 21 70 2d 3e 6f 6b 56 61 72 20 29 7b 0a 20  ( !p->okVar ){. 
180fd 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
180fe 5f 65 6e 74 65 72 28 70 2d 3e 64 62 2d 3e 6d 75  _enter(p->db->mu
180ff 74 65 78 29 3b 0a 20 20 20 20 69 66 28 20 21 70  tex);.    if( !p
18100 2d 3e 6f 6b 56 61 72 20 29 7b 0a 20 20 20 20 20  ->okVar ){.     
18101 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 4f 70   int j;.      Op
18102 20 2a 70 4f 70 3b 0a 20 20 20 20 20 20 66 6f 72   *pOp;.      for
18103 28 6a 3d 30 2c 20 70 4f 70 3d 70 2d 3e 61 4f 70  (j=0, pOp=p->aOp
18104 3b 20 6a 3c 70 2d 3e 6e 4f 70 3b 20 6a 2b 2b 2c  ; j<p->nOp; j++,
18105 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20 20   pOp++){.       
18106 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
18107 3d 3d 4f 50 5f 56 61 72 69 61 62 6c 65 20 29 7b  ==OP_Variable ){
18108 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
18109 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20  t( pOp->p1>0 && 
1810a 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 56 61 72  pOp->p1<=p->nVar
1810b 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d   );.          p-
1810c 3e 61 7a 56 61 72 5b 70 4f 70 2d 3e 70 31 2d 31  >azVar[pOp->p1-1
1810d 5d 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20  ] = pOp->p4.z;. 
1810e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1810f 0a 20 20 20 20 20 20 70 2d 3e 6f 6b 56 61 72 20  .      p->okVar 
18110 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  = 1;.    }.    s
18111 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
18112 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ve(p->db->mutex)
18113 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ;.  }.}../*.** R
18114 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f  eturn the name o
18115 66 20 61 20 77 69 6c 64 63 61 72 64 20 70 61 72  f a wildcard par
18116 61 6d 65 74 65 72 2e 20 20 52 65 74 75 72 6e 20  ameter.  Return 
18117 4e 55 4c 4c 20 69 66 20 74 68 65 20 69 6e 64 65  NULL if the inde
18118 78 0a 2a 2a 20 69 73 20 6f 75 74 20 6f 66 20 72  x.** is out of r
18119 61 6e 67 65 20 6f 72 20 69 66 20 74 68 65 20 77  ange or if the w
1811a 69 6c 64 63 61 72 64 20 69 73 20 75 6e 6e 61 6d  ildcard is unnam
1811b 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  ed..**.** The re
1811c 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20 55  sult is always U
1811d 54 46 2d 38 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  TF-8..*/.SQLITE_
1811e 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  API const char *
1811f 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
18120 61 6d 65 74 65 72 5f 6e 61 6d 65 28 73 71 6c 69  ameter_name(sqli
18121 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
18122 20 69 6e 74 20 69 29 7b 0a 20 20 56 64 62 65 20   int i){.  Vdbe 
18123 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d  *p = (Vdbe*)pStm
18124 74 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c  t;.  if( p==0 ||
18125 20 69 3c 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61   i<1 || i>p->nVa
18126 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  r ){.    return 
18127 30 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 65 56  0;.  }.  createV
18128 61 72 4d 61 70 28 70 29 3b 0a 20 20 72 65 74 75  arMap(p);.  retu
18129 72 6e 20 70 2d 3e 61 7a 56 61 72 5b 69 2d 31 5d  rn p->azVar[i-1]
1812a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e  ;.}../*.** Given
1812b 20 61 20 77 69 6c 64 63 61 72 64 20 70 61 72 61   a wildcard para
1812c 6d 65 74 65 72 20 6e 61 6d 65 2c 20 72 65 74 75  meter name, retu
1812d 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  rn the index of 
1812e 74 68 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20  the variable.** 
1812f 77 69 74 68 20 74 68 61 74 20 6e 61 6d 65 2e 20  with that name. 
18130 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
18131 76 61 72 69 61 62 6c 65 20 77 69 74 68 20 74 68  variable with th
18132 65 20 67 69 76 65 6e 20 6e 61 6d 65 2c 0a 2a 2a  e given name,.**
18133 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51   return 0..*/.SQ
18134 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
18135 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
18136 74 65 72 5f 69 6e 64 65 78 28 73 71 6c 69 74 65  ter_index(sqlite
18137 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 63  3_stmt *pStmt, c
18138 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
18139 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28  ){.  Vdbe *p = (
1813a 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69  Vdbe*)pStmt;.  i
1813b 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30  nt i;.  if( p==0
1813c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
1813d 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 65 56 61  ;.  }.  createVa
1813e 72 4d 61 70 28 70 29 3b 20 0a 20 20 69 66 28 20  rMap(p); .  if( 
1813f 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 66 6f 72  zName ){.    for
18140 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 72 3b  (i=0; i<p->nVar;
18141 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 6f 6e   i++){.      con
18142 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 2d 3e  st char *z = p->
18143 61 7a 56 61 72 5b 69 5d 3b 0a 20 20 20 20 20 20  azVar[i];.      
18144 69 66 28 20 7a 20 26 26 20 73 74 72 63 6d 70 28  if( z && strcmp(
18145 7a 2c 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20  z,zName)==0 ){. 
18146 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b         return i+
18147 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
18148 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
18149 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66  .}../*.** Transf
1814a 65 72 20 61 6c 6c 20 62 69 6e 64 69 6e 67 73 20  er all bindings 
1814b 66 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20 73  from the first s
1814c 74 61 74 65 6d 65 6e 74 20 6f 76 65 72 20 74 6f  tatement over to
1814d 20 74 68 65 20 73 65 63 6f 6e 64 2e 0a 2a 2a 20   the second..** 
1814e 49 66 20 74 68 65 20 74 77 6f 20 73 74 61 74 65  If the two state
1814f 6d 65 6e 74 73 20 63 6f 6e 74 61 69 6e 20 61 20  ments contain a 
18150 64 69 66 66 65 72 65 6e 74 20 6e 75 6d 62 65 72  different number
18151 20 6f 66 20 62 69 6e 64 69 6e 67 73 2c 20 74 68   of bindings, th
18152 65 6e 0a 2a 2a 20 61 6e 20 53 51 4c 49 54 45 5f  en.** an SQLITE_
18153 45 52 52 4f 52 20 69 73 20 72 65 74 75 72 6e 65  ERROR is returne
18154 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
18155 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
18156 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73  TransferBindings
18157 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
18158 46 72 6f 6d 53 74 6d 74 2c 20 73 71 6c 69 74 65  FromStmt, sqlite
18159 33 5f 73 74 6d 74 20 2a 70 54 6f 53 74 6d 74 29  3_stmt *pToStmt)
1815a 7b 0a 20 20 56 64 62 65 20 2a 70 46 72 6f 6d 20  {.  Vdbe *pFrom 
1815b 3d 20 28 56 64 62 65 2a 29 70 46 72 6f 6d 53 74  = (Vdbe*)pFromSt
1815c 6d 74 3b 0a 20 20 56 64 62 65 20 2a 70 54 6f 20  mt;.  Vdbe *pTo 
1815d 3d 20 28 56 64 62 65 2a 29 70 54 6f 53 74 6d 74  = (Vdbe*)pToStmt
1815e 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63 20 3d 20  ;.  int i, rc = 
1815f 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
18160 20 28 70 46 72 6f 6d 2d 3e 6d 61 67 69 63 21 3d   (pFrom->magic!=
18161 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26  VDBE_MAGIC_RUN &
18162 26 20 70 46 72 6f 6d 2d 3e 6d 61 67 69 63 21 3d  & pFrom->magic!=
18163 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 29  VDBE_MAGIC_HALT)
18164 0a 20 20 20 20 7c 7c 20 28 70 54 6f 2d 3e 6d 61  .    || (pTo->ma
18165 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f  gic!=VDBE_MAGIC_
18166 52 55 4e 20 26 26 20 70 54 6f 2d 3e 6d 61 67 69  RUN && pTo->magi
18167 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 48 41  c!=VDBE_MAGIC_HA
18168 4c 54 29 0a 20 20 20 20 7c 7c 20 70 54 6f 2d 3e  LT).    || pTo->
18169 64 62 21 3d 70 46 72 6f 6d 2d 3e 64 62 20 29 7b  db!=pFrom->db ){
1816a 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1816b 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20  TE_MISUSE;.  }. 
1816c 20 69 66 28 20 70 46 72 6f 6d 2d 3e 6e 56 61 72   if( pFrom->nVar
1816d 21 3d 70 54 6f 2d 3e 6e 56 61 72 20 29 7b 0a 20  !=pTo->nVar ){. 
1816e 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1816f 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 73 71  _ERROR;.  }.  sq
18170 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
18171 72 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78  r(pTo->db->mutex
18172 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63  );.  for(i=0; rc
18173 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
18174 3c 70 46 72 6f 6d 2d 3e 6e 56 61 72 3b 20 69 2b  <pFrom->nVar; i+
18175 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  +){.    sqlite3V
18176 64 62 65 4d 65 6d 4d 6f 76 65 28 26 70 54 6f 2d  dbeMemMove(&pTo-
18177 3e 61 56 61 72 5b 69 5d 2c 20 26 70 46 72 6f 6d  >aVar[i], &pFrom
18178 2d 3e 61 56 61 72 5b 69 5d 29 3b 0a 20 20 7d 0a  ->aVar[i]);.  }.
18179 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1817a 6c 65 61 76 65 28 70 54 6f 2d 3e 64 62 2d 3e 6d  leave(pTo->db->m
1817b 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28  utex);.  assert(
1817c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc==SQLITE_OK |
1817d 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  | rc==SQLITE_NOM
1817e 45 4d 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  EM );.  return r
1817f 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  c;.}..#ifndef SQ
18180 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43  LITE_OMIT_DEPREC
18181 41 54 45 44 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65  ATED./*.** Depre
18182 63 61 74 65 64 20 65 78 74 65 72 6e 61 6c 20 69  cated external i
18183 6e 74 65 72 66 61 63 65 2e 20 20 49 6e 74 65 72  nterface.  Inter
18184 6e 61 6c 2f 63 6f 72 65 20 53 51 4c 69 74 65 20  nal/core SQLite 
18185 63 6f 64 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 63  code.** should c
18186 61 6c 6c 20 73 71 6c 69 74 65 33 54 72 61 6e 73  all sqlite3Trans
18187 66 65 72 42 69 6e 64 69 6e 67 73 2e 0a 2a 2f 0a  ferBindings..*/.
18188 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
18189 71 6c 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f  qlite3_transfer_
1818a 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33  bindings(sqlite3
1818b 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53 74 6d 74  _stmt *pFromStmt
1818c 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  , sqlite3_stmt *
1818d 70 54 6f 53 74 6d 74 29 7b 0a 20 20 72 65 74 75  pToStmt){.  retu
1818e 72 6e 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66  rn sqlite3Transf
1818f 65 72 42 69 6e 64 69 6e 67 73 28 70 46 72 6f 6d  erBindings(pFrom
18190 53 74 6d 74 2c 20 70 54 6f 53 74 6d 74 29 3b 0a  Stmt, pToStmt);.
18191 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
18192 52 65 74 75 72 6e 20 74 68 65 20 73 71 6c 69 74  Return the sqlit
18193 65 33 2a 20 64 61 74 61 62 61 73 65 20 68 61 6e  e3* database han
18194 64 6c 65 20 74 6f 20 77 68 69 63 68 20 74 68 65  dle to which the
18195 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
18196 65 6e 74 20 67 69 76 65 6e 0a 2a 2a 20 69 6e 20  ent given.** in 
18197 74 68 65 20 61 72 67 75 6d 65 6e 74 20 62 65 6c  the argument bel
18198 6f 6e 67 73 2e 20 20 54 68 69 73 20 69 73 20 74  ongs.  This is t
18199 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
1819a 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77 61 73   handle that was
1819b 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 61 72  .** the first ar
1819c 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 73 71  gument to the sq
1819d 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20  lite3_prepare() 
1819e 74 68 61 74 20 77 61 73 20 75 73 65 64 20 74 6f  that was used to
1819f 20 63 72 65 61 74 65 0a 2a 2a 20 74 68 65 20 73   create.** the s
181a0 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20  tatement in the 
181a1 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2f 0a  first place..*/.
181a2 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
181a3 65 33 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f 68  e3 *sqlite3_db_h
181a4 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 73 74  andle(sqlite3_st
181a5 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 72 65  mt *pStmt){.  re
181a6 74 75 72 6e 20 70 53 74 6d 74 20 3f 20 28 28 56  turn pStmt ? ((V
181a7 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e 64 62 20  dbe*)pStmt)->db 
181a8 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  : 0;.}../*.** Re
181a9 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
181aa 6f 20 74 68 65 20 6e 65 78 74 20 70 72 65 70 61  o the next prepa
181ab 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61 66  red statement af
181ac 74 65 72 20 70 53 74 6d 74 20 61 73 73 6f 63 69  ter pStmt associ
181ad 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 64 61 74  ated.** with dat
181ae 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
181af 20 70 44 62 2e 20 20 49 66 20 70 53 74 6d 74 20   pDb.  If pStmt 
181b0 69 73 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20  is NULL, return 
181b1 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 72 65  the first.** pre
181b2 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20  pared statement 
181b3 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65  for the database
181b4 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 52 65   connection.  Re
181b5 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65  turn NULL if the
181b6 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 6d 6f 72  re.** are no mor
181b7 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
181b8 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73   sqlite3_stmt *s
181b9 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74  qlite3_next_stmt
181ba 28 73 71 6c 69 74 65 33 20 2a 70 44 62 2c 20 73  (sqlite3 *pDb, s
181bb 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
181bc 6d 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73  mt){.  sqlite3_s
181bd 74 6d 74 20 2a 70 4e 65 78 74 3b 0a 20 20 73 71  tmt *pNext;.  sq
181be 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
181bf 72 28 70 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  r(pDb->mutex);. 
181c0 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29 7b   if( pStmt==0 ){
181c1 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 28 73 71  .    pNext = (sq
181c2 6c 69 74 65 33 5f 73 74 6d 74 2a 29 70 44 62 2d  lite3_stmt*)pDb-
181c3 3e 70 56 64 62 65 3b 0a 20 20 7d 65 6c 73 65 7b  >pVdbe;.  }else{
181c4 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 28 73 71  .    pNext = (sq
181c5 6c 69 74 65 33 5f 73 74 6d 74 2a 29 28 28 56 64  lite3_stmt*)((Vd
181c6 62 65 2a 29 70 53 74 6d 74 29 2d 3e 70 4e 65 78  be*)pStmt)->pNex
181c7 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  t;.  }.  sqlite3
181c8 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 44 62  _mutex_leave(pDb
181c9 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
181ca 72 6e 20 70 4e 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a  rn pNext;.}../*.
181cb 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 61  ** Return the va
181cc 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 20  lue of a status 
181cd 63 6f 75 6e 74 65 72 20 66 6f 72 20 61 20 70 72  counter for a pr
181ce 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
181cf 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
181d0 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f  nt sqlite3_stmt_
181d1 73 74 61 74 75 73 28 73 71 6c 69 74 65 33 5f 73  status(sqlite3_s
181d2 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
181d3 6f 70 2c 20 69 6e 74 20 72 65 73 65 74 46 6c 61  op, int resetFla
181d4 67 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 64 62  g){.  Vdbe *pVdb
181d5 65 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74  e = (Vdbe*)pStmt
181d6 3b 0a 20 20 69 6e 74 20 76 20 3d 20 70 56 64 62  ;.  int v = pVdb
181d7 65 2d 3e 61 43 6f 75 6e 74 65 72 5b 6f 70 2d 31  e->aCounter[op-1
181d8 5d 3b 0a 20 20 69 66 28 20 72 65 73 65 74 46 6c  ];.  if( resetFl
181d9 61 67 20 29 20 70 56 64 62 65 2d 3e 61 43 6f 75  ag ) pVdbe->aCou
181da 6e 74 65 72 5b 6f 70 2d 31 5d 20 3d 20 30 3b 0a  nter[op-1] = 0;.
181db 20 20 72 65 74 75 72 6e 20 76 3b 0a 7d 0a 0a 2f    return v;.}../
181dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
181dd 6e 64 20 6f 66 20 76 64 62 65 61 70 69 2e 63 20  nd of vdbeapi.c 
181de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
181df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
181e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
181e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
181e2 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 2e 63  egin file vdbe.c
181e3 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
181e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
181e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
181e6 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d  *.** 2001 Septem
181e7 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 15.**.** The
181e8 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
181e9 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
181ea 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
181eb 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
181ec 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
181ed 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
181ee 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
181ef 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
181f0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
181f1 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
181f2 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
181f3 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
181f4 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
181f5 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
181f6 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
181f7 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
181f8 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
181f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
181fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
181fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
181fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
181fd 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64  *****.** The cod
181fe 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69  e in this file i
181ff 6d 70 6c 65 6d 65 6e 74 73 20 65 78 65 63 75 74  mplements execut
18200 69 6f 6e 20 6d 65 74 68 6f 64 20 6f 66 20 74 68  ion method of th
18201 65 20 0a 2a 2a 20 56 69 72 74 75 61 6c 20 44 61  e .** Virtual Da
18202 74 61 62 61 73 65 20 45 6e 67 69 6e 65 20 28 56  tabase Engine (V
18203 44 42 45 29 2e 20 20 41 20 73 65 70 61 72 61 74  DBE).  A separat
18204 65 20 66 69 6c 65 20 28 22 76 64 62 65 61 75 78  e file ("vdbeaux
18205 2e 63 22 29 0a 2a 2a 20 68 61 6e 64 6c 65 73 20  .c").** handles 
18206 68 6f 75 73 65 6b 65 65 70 69 6e 67 20 64 65 74  housekeeping det
18207 61 69 6c 73 20 73 75 63 68 20 61 73 20 63 72 65  ails such as cre
18208 61 74 69 6e 67 20 61 6e 64 20 64 65 6c 65 74 69  ating and deleti
18209 6e 67 0a 2a 2a 20 56 44 42 45 20 69 6e 73 74 61  ng.** VDBE insta
1820a 6e 63 65 73 2e 20 20 54 68 69 73 20 66 69 6c 65  nces.  This file
1820b 20 69 73 20 73 6f 6c 65 6c 79 20 69 6e 74 65 72   is solely inter
1820c 65 73 74 65 64 20 69 6e 20 65 78 65 63 75 74 69  ested in executi
1820d 6e 67 0a 2a 2a 20 74 68 65 20 56 44 42 45 20 70  ng.** the VDBE p
1820e 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 49 6e  rogram..**.** In
1820f 20 74 68 65 20 65 78 74 65 72 6e 61 6c 20 69 6e   the external in
18210 74 65 72 66 61 63 65 2c 20 61 6e 20 22 73 71 6c  terface, an "sql
18211 69 74 65 33 5f 73 74 6d 74 2a 22 20 69 73 20 61  ite3_stmt*" is a
18212 6e 20 6f 70 61 71 75 65 20 70 6f 69 6e 74 65 72  n opaque pointer
18213 0a 2a 2a 20 74 6f 20 61 20 56 44 42 45 2e 0a 2a  .** to a VDBE..*
18214 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 20 70 61 72  *.** The SQL par
18215 73 65 72 20 67 65 6e 65 72 61 74 65 73 20 61 20  ser generates a 
18216 70 72 6f 67 72 61 6d 20 77 68 69 63 68 20 69 73  program which is
18217 20 74 68 65 6e 20 65 78 65 63 75 74 65 64 20 62   then executed b
18218 79 0a 2a 2a 20 74 68 65 20 56 44 42 45 20 74 6f  y.** the VDBE to
18219 20 64 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66 20   do the work of 
1821a 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
1821b 74 2e 20 20 56 44 42 45 20 70 72 6f 67 72 61 6d  t.  VDBE program
1821c 73 20 61 72 65 20 0a 2a 2a 20 73 69 6d 69 6c 61  s are .** simila
1821d 72 20 69 6e 20 66 6f 72 6d 20 74 6f 20 61 73 73  r in form to ass
1821e 65 6d 62 6c 79 20 6c 61 6e 67 75 61 67 65 2e 20  embly language. 
1821f 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 6e   The program con
18220 73 69 73 74 73 20 6f 66 0a 2a 2a 20 61 20 6c 69  sists of.** a li
18221 6e 65 61 72 20 73 65 71 75 65 6e 63 65 20 6f 66  near sequence of
18222 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 45 61   operations.  Ea
18223 63 68 20 6f 70 65 72 61 74 69 6f 6e 20 68 61 73  ch operation has
18224 20 61 6e 20 6f 70 63 6f 64 65 20 0a 2a 2a 20 61   an opcode .** a
18225 6e 64 20 35 20 6f 70 65 72 61 6e 64 73 2e 20 20  nd 5 operands.  
18226 4f 70 65 72 61 6e 64 73 20 50 31 2c 20 50 32 2c  Operands P1, P2,
18227 20 61 6e 64 20 50 33 20 61 72 65 20 69 6e 74 65   and P3 are inte
18228 67 65 72 73 2e 20 20 4f 70 65 72 61 6e 64 20 50  gers.  Operand P
18229 34 20 0a 2a 2a 20 69 73 20 61 20 6e 75 6c 6c 2d  4 .** is a null-
1822a 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e  terminated strin
1822b 67 2e 20 20 4f 70 65 72 61 6e 64 20 50 35 20 69  g.  Operand P5 i
1822c 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 63 68  s an unsigned ch
1822d 61 72 61 63 74 65 72 2e 0a 2a 2a 20 46 65 77 20  aracter..** Few 
1822e 6f 70 63 6f 64 65 73 20 75 73 65 20 61 6c 6c 20  opcodes use all 
1822f 35 20 6f 70 65 72 61 6e 64 73 2e 0a 2a 2a 0a 2a  5 operands..**.*
18230 2a 20 43 6f 6d 70 75 74 61 74 69 6f 6e 20 72 65  * Computation re
18231 73 75 6c 74 73 20 61 72 65 20 73 74 6f 72 65 64  sults are stored
18232 20 6f 6e 20 61 20 73 65 74 20 6f 66 20 72 65 67   on a set of reg
18233 69 73 74 65 72 73 20 6e 75 6d 62 65 72 65 64 20  isters numbered 
18234 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69 74  beginning.** wit
18235 68 20 31 20 61 6e 64 20 67 6f 69 6e 67 20 75 70  h 1 and going up
18236 20 74 6f 20 56 64 62 65 2e 6e 4d 65 6d 2e 20 20   to Vdbe.nMem.  
18237 45 61 63 68 20 72 65 67 69 73 74 65 72 20 63 61  Each register ca
18238 6e 20 73 74 6f 72 65 0a 2a 2a 20 65 69 74 68 65  n store.** eithe
18239 72 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 61 20  r an integer, a 
1823a 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20  null-terminated 
1823b 73 74 72 69 6e 67 2c 20 61 20 66 6c 6f 61 74 69  string, a floati
1823c 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 6e 75 6d 62  ng point.** numb
1823d 65 72 2c 20 6f 72 20 74 68 65 20 53 51 4c 20 22  er, or the SQL "
1823e 4e 55 4c 4c 22 20 76 61 6c 75 65 2e 20 20 41 6e  NULL" value.  An
1823f 20 69 6d 70 6c 69 63 69 74 20 63 6f 6e 76 65 72   implicit conver
18240 73 69 6f 6e 20 66 72 6f 6d 20 6f 6e 65 0a 2a 2a  sion from one.**
18241 20 74 79 70 65 20 74 6f 20 74 68 65 20 6f 74 68   type to the oth
18242 65 72 20 6f 63 63 75 72 73 20 61 73 20 6e 65 63  er occurs as nec
18243 65 73 73 61 72 79 2e 0a 2a 2a 20 0a 2a 2a 20 4d  essary..** .** M
18244 6f 73 74 20 6f 66 20 74 68 65 20 63 6f 64 65 20  ost of the code 
18245 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20  in this file is 
18246 74 61 6b 65 6e 20 75 70 20 62 79 20 74 68 65 20  taken up by the 
18247 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28  sqlite3VdbeExec(
18248 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 68  ).** function wh
18249 69 63 68 20 64 6f 65 73 20 74 68 65 20 77 6f 72  ich does the wor
1824a 6b 20 6f 66 20 69 6e 74 65 72 70 72 65 74 69 6e  k of interpretin
1824b 67 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d  g a VDBE program
1824c 2e 0a 2a 2a 20 42 75 74 20 6f 74 68 65 72 20 72  ..** But other r
1824d 6f 75 74 69 6e 65 73 20 61 72 65 20 61 6c 73 6f  outines are also
1824e 20 70 72 6f 76 69 64 65 64 20 74 6f 20 68 65 6c   provided to hel
1824f 70 20 69 6e 20 62 75 69 6c 64 69 6e 67 20 75 70  p in building up
18250 0a 2a 2a 20 61 20 70 72 6f 67 72 61 6d 20 69 6e  .** a program in
18251 73 74 72 75 63 74 69 6f 6e 20 62 79 20 69 6e 73  struction by ins
18252 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  truction..**.** 
18253 56 61 72 69 6f 75 73 20 73 63 72 69 70 74 73 20  Various scripts 
18254 73 63 61 6e 20 74 68 69 73 20 73 6f 75 72 63 65  scan this source
18255 20 66 69 6c 65 20 69 6e 20 6f 72 64 65 72 20 74   file in order t
18256 6f 20 67 65 6e 65 72 61 74 65 20 48 54 4d 4c 0a  o generate HTML.
18257 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e  ** documentation
18258 2c 20 68 65 61 64 65 72 73 20 66 69 6c 65 73 2c  , headers files,
18259 20 6f 72 20 6f 74 68 65 72 20 64 65 72 69 76 65   or other derive
1825a 64 20 66 69 6c 65 73 2e 20 20 54 68 65 20 66 6f  d files.  The fo
1825b 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 66 20 74  rmatting.** of t
1825c 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20  he code in this 
1825d 66 69 6c 65 20 69 73 2c 20 74 68 65 72 65 66 6f  file is, therefo
1825e 72 65 2c 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20  re, important.  
1825f 53 65 65 20 6f 74 68 65 72 20 63 6f 6d 6d 65 6e  See other commen
18260 74 73 0a 2a 2a 20 69 6e 20 74 68 69 73 20 66 69  ts.** in this fi
18261 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 20  le for details. 
18262 20 49 66 20 69 6e 20 64 6f 75 62 74 2c 20 64 6f   If in doubt, do
18263 20 6e 6f 74 20 64 65 76 69 61 74 65 20 66 72 6f   not deviate fro
18264 6d 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 63 6f  m existing.** co
18265 6d 6d 65 6e 74 69 6e 67 20 61 6e 64 20 69 6e 64  mmenting and ind
18266 65 6e 74 61 74 69 6f 6e 20 70 72 61 63 74 69 63  entation practic
18267 65 73 20 77 68 65 6e 20 63 68 61 6e 67 69 6e 67  es when changing
18268 20 6f 72 20 61 64 64 69 6e 67 20 63 6f 64 65 2e   or adding code.
18269 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65  .**.** $Id: vdbe
1826a 2e 63 2c 76 20 31 2e 37 38 36 20 32 30 30 38 2f  .c,v 1.786 2008/
1826b 31 31 2f 30 35 20 31 36 3a 33 37 3a 33 35 20 64  11/05 16:37:35 d
1826c 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a  rh Exp $.*/../*.
1826d 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
1826e 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
1826f 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20   is incremented 
18270 65 76 65 72 79 20 74 69 6d 65 20 61 20 63 75 72  every time a cur
18271 73 6f 72 0a 2a 2a 20 6d 6f 76 65 73 2c 20 65 69  sor.** moves, ei
18272 74 68 65 72 20 62 79 20 74 68 65 20 4f 50 5f 4d  ther by the OP_M
18273 6f 76 65 58 58 2c 20 4f 50 5f 4e 65 78 74 2c 20  oveXX, OP_Next, 
18274 6f 72 20 4f 50 5f 50 72 65 76 20 6f 70 63 6f 64  or OP_Prev opcod
18275 65 73 2e 20 20 54 68 65 20 74 65 73 74 0a 2a 2a  es.  The test.**
18276 20 70 72 6f 63 65 64 75 72 65 73 20 75 73 65 20   procedures use 
18277 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  this information
18278 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
18279 61 74 20 69 6e 64 69 63 65 73 20 61 72 65 0a 2a  at indices are.*
1827a 2a 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72 65 63  * working correc
1827b 74 6c 79 2e 20 20 54 68 69 73 20 76 61 72 69 61  tly.  This varia
1827c 62 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e 63 74  ble has no funct
1827d 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74  ion other than t
1827e 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69 66 79  o.** help verify
1827f 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65   the correct ope
18280 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69  ration of the li
18281 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66  brary..*/.#ifdef
18282 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
18283 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
18284 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74  te3_search_count
18285 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a   = 0;.#endif../*
18286 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 67 6c  .** When this gl
18287 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73  obal variable is
18288 20 70 6f 73 69 74 69 76 65 2c 20 69 74 20 67 65   positive, it ge
18289 74 73 20 64 65 63 72 65 6d 65 6e 74 65 64 20 6f  ts decremented o
1828a 6e 63 65 20 62 65 66 6f 72 65 0a 2a 2a 20 65 61  nce before.** ea
1828b 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69  ch instruction i
1828c 6e 20 74 68 65 20 56 44 42 45 2e 20 20 57 68 65  n the VDBE.  Whe
1828d 6e 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 20  n reaches zero, 
1828e 74 68 65 20 75 31 2e 69 73 49 6e 74 65 72 72 75  the u1.isInterru
1828f 70 74 65 64 0a 2a 2a 20 66 69 65 6c 64 20 6f 66  pted.** field of
18290 20 74 68 65 20 73 71 6c 69 74 65 33 20 73 74 72   the sqlite3 str
18291 75 63 74 75 72 65 20 69 73 20 73 65 74 20 69 6e  ucture is set in
18292 20 6f 72 64 65 72 20 74 6f 20 73 69 6d 75 6c 61   order to simula
18293 74 65 20 61 6e 64 20 69 6e 74 65 72 72 75 70 74  te and interrupt
18294 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 61 63  ..**.** This fac
18295 69 6c 69 74 79 20 69 73 20 75 73 65 64 20 66 6f  ility is used fo
18296 72 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73  r testing purpos
18297 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 64 6f 65  es only.  It doe
18298 73 20 6e 6f 74 20 66 75 6e 63 74 69 6f 6e 0a 2a  s not function.*
18299 2a 20 69 6e 20 61 6e 20 6f 72 64 69 6e 61 72 79  * in an ordinary
1829a 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 64 65   build..*/.#ifde
1829b 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
1829c 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1829d 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63  ite3_interrupt_c
1829e 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66  ount = 0;.#endif
1829f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74  ../*.** The next
182a0 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
182a1 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20   is incremented 
182a2 65 61 63 68 20 74 79 70 65 20 74 68 65 20 4f 50  each type the OP
182a3 5f 53 6f 72 74 20 6f 70 63 6f 64 65 0a 2a 2a 20  _Sort opcode.** 
182a4 69 73 20 65 78 65 63 75 74 65 64 2e 20 20 54 68  is executed.  Th
182a5 65 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65  e test procedure
182a6 73 20 75 73 65 20 74 68 69 73 20 69 6e 66 6f 72  s use this infor
182a7 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73  mation to make s
182a8 75 72 65 20 74 68 61 74 0a 2a 2a 20 73 6f 72 74  ure that.** sort
182a9 69 6e 67 20 69 73 20 6f 63 63 75 72 72 69 6e 67  ing is occurring
182aa 20 6f 72 20 6e 6f 74 20 6f 63 63 75 72 72 69 6e   or not occurrin
182ab 67 20 61 74 20 61 70 70 72 6f 70 72 69 61 74 65  g at appropriate
182ac 20 74 69 6d 65 73 2e 20 20 20 54 68 69 73 20 76   times.   This v
182ad 61 72 69 61 62 6c 65 0a 2a 2a 20 68 61 73 20 6e  ariable.** has n
182ae 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72  o function other
182af 20 74 68 61 6e 20 74 6f 20 68 65 6c 70 20 76 65   than to help ve
182b0 72 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 74  rify the correct
182b1 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68   operation of th
182b2 65 0a 2a 2a 20 6c 69 62 72 61 72 79 2e 0a 2a 2f  e.** library..*/
182b3 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
182b4 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  EST.SQLITE_API i
182b5 6e 74 20 73 71 6c 69 74 65 33 5f 73 6f 72 74 5f  nt sqlite3_sort_
182b6 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69  count = 0;.#endi
182b7 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78  f../*.** The nex
182b8 74 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  t global variabl
182b9 65 20 72 65 63 6f 72 64 73 20 74 68 65 20 73 69  e records the si
182ba 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73  ze of the larges
182bb 74 20 4d 45 4d 5f 42 6c 6f 62 0a 2a 2a 20 6f 72  t MEM_Blob.** or
182bc 20 4d 45 4d 5f 53 74 72 20 74 68 61 74 20 68 61   MEM_Str that ha
182bd 73 20 62 65 65 6e 20 75 73 65 64 20 62 79 20 61  s been used by a
182be 20 56 44 42 45 20 6f 70 63 6f 64 65 2e 20 20 54   VDBE opcode.  T
182bf 68 65 20 74 65 73 74 20 70 72 6f 63 65 64 75 72  he test procedur
182c0 65 73 0a 2a 2a 20 75 73 65 20 74 68 69 73 20 69  es.** use this i
182c1 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61  nformation to ma
182c2 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65  ke sure that the
182c3 20 7a 65 72 6f 2d 62 6c 6f 62 20 66 75 6e 63 74   zero-blob funct
182c4 69 6f 6e 61 6c 69 74 79 0a 2a 2a 20 69 73 20 77  ionality.** is w
182c5 6f 72 6b 69 6e 67 20 63 6f 72 72 65 63 74 6c 79  orking correctly
182c6 2e 20 20 20 54 68 69 73 20 76 61 72 69 61 62 6c  .   This variabl
182c7 65 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 6f  e has no functio
182c8 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 6f 0a  n other than to.
182c9 2a 2a 20 68 65 6c 70 20 76 65 72 69 66 79 20 74  ** help verify t
182ca 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61  he correct opera
182cb 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 72  tion of the libr
182cc 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ary..*/.#ifdef S
182cd 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
182ce 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
182cf 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d  3_max_blobsize =
182d0 20 30 3b 0a 73 74 61 74 69 63 20 76 6f 69 64 20   0;.static void 
182d1 75 70 64 61 74 65 4d 61 78 42 6c 6f 62 73 69 7a  updateMaxBlobsiz
182d2 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 66 28  e(Mem *p){.  if(
182d3 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45   (p->flags & (ME
182d4 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29  M_Str|MEM_Blob))
182d5 21 3d 30 20 26 26 20 70 2d 3e 6e 3e 73 71 6c 69  !=0 && p->n>sqli
182d6 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65  te3_max_blobsize
182d7 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
182d8 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 20 70  max_blobsize = p
182d9 2d 3e 6e 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69  ->n;.  }.}.#endi
182da 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 61 20  f../*.** Test a 
182db 72 65 67 69 73 74 65 72 20 74 6f 20 73 65 65 20  register to see 
182dc 69 66 20 69 74 20 65 78 63 65 65 64 73 20 74 68  if it exceeds th
182dd 65 20 63 75 72 72 65 6e 74 20 6d 61 78 69 6d 75  e current maximu
182de 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2a 20  m blob size..** 
182df 49 66 20 69 74 20 64 6f 65 73 2c 20 72 65 63 6f  If it does, reco
182e0 72 64 20 74 68 65 20 6e 65 77 20 6d 61 78 69 6d  rd the new maxim
182e1 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2f  um blob size..*/
182e2 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
182e3 49 54 45 5f 54 45 53 54 29 20 26 26 20 21 64 65  ITE_TEST) && !de
182e4 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
182e5 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 29 0a  T_BUILTIN_TEST).
182e6 23 20 64 65 66 69 6e 65 20 55 50 44 41 54 45 5f  # define UPDATE_
182e7 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 50 29 20  MAX_BLOBSIZE(P) 
182e8 20 75 70 64 61 74 65 4d 61 78 42 6c 6f 62 73 69   updateMaxBlobsi
182e9 7a 65 28 50 29 0a 23 65 6c 73 65 0a 23 20 64 65  ze(P).#else.# de
182ea 66 69 6e 65 20 55 50 44 41 54 45 5f 4d 41 58 5f  fine UPDATE_MAX_
182eb 42 4c 4f 42 53 49 5a 45 28 50 29 0a 23 65 6e 64  BLOBSIZE(P).#end
182ec 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  if../*.** Conver
182ed 74 20 74 68 65 20 67 69 76 65 6e 20 72 65 67 69  t the given regi
182ee 73 74 65 72 20 69 6e 74 6f 20 61 20 73 74 72 69  ster into a stri
182ef 6e 67 20 69 66 20 69 74 20 69 73 6e 27 74 20 6f  ng if it isn't o
182f0 6e 65 0a 2a 2a 20 61 6c 72 65 61 64 79 2e 20 52  ne.** already. R
182f1 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69  eturn non-zero i
182f2 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  f a malloc() fai
182f3 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ls..*/.#define S
182f4 74 72 69 6e 67 69 66 79 28 50 2c 20 65 6e 63 29  tringify(P, enc)
182f5 20 5c 0a 20 20 20 69 66 28 28 28 50 29 2d 3e 66   \.   if(((P)->f
182f6 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45  lags&(MEM_Str|ME
182f7 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20 26 26 20 73  M_Blob))==0 && s
182f8 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72  qlite3VdbeMemStr
182f9 69 6e 67 69 66 79 28 50 2c 65 6e 63 29 29 20 5c  ingify(P,enc)) \
182fa 0a 20 20 20 20 20 7b 20 67 6f 74 6f 20 6e 6f 5f  .     { goto no_
182fb 6d 65 6d 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e  mem; }../*.** An
182fc 20 65 70 68 65 6d 65 72 61 6c 20 73 74 72 69 6e   ephemeral strin
182fd 67 20 76 61 6c 75 65 20 28 73 69 67 6e 69 66 69  g value (signifi
182fe 65 64 20 62 79 20 74 68 65 20 4d 45 4d 5f 45 70  ed by the MEM_Ep
182ff 68 65 6d 20 66 6c 61 67 29 20 63 6f 6e 74 61 69  hem flag) contai
18300 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20  ns.** a pointer 
18301 74 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79  to a dynamically
18302 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 69 6e   allocated strin
18303 67 20 77 68 65 72 65 20 73 6f 6d 65 20 6f 74 68  g where some oth
18304 65 72 20 65 6e 74 69 74 79 0a 2a 2a 20 69 73 20  er entity.** is 
18305 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
18306 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68 61  deallocating tha
18307 74 20 73 74 72 69 6e 67 2e 20 20 42 65 63 61 75  t string.  Becau
18308 73 65 20 74 68 65 20 72 65 67 69 73 74 65 72 0a  se the register.
18309 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74  ** does not cont
1830a 72 6f 6c 20 74 68 65 20 73 74 72 69 6e 67 2c 20  rol the string, 
1830b 69 74 20 6d 69 67 68 74 20 62 65 20 64 65 6c 65  it might be dele
1830c 74 65 64 20 77 69 74 68 6f 75 74 20 74 68 65 20  ted without the 
1830d 72 65 67 69 73 74 65 72 0a 2a 2a 20 6b 6e 6f 77  register.** know
1830e 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  ing it..**.** Th
1830f 69 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e 76 65  is routine conve
18310 72 74 73 20 61 6e 20 65 70 68 65 6d 65 72 61 6c  rts an ephemeral
18311 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 64   string into a d
18312 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63  ynamically alloc
18313 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 74  ated.** string t
18314 68 61 74 20 74 68 65 20 72 65 67 69 73 74 65 72  hat the register
18315 20 69 74 73 65 6c 66 20 63 6f 6e 74 72 6f 6c 73   itself controls
18316 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
18317 73 2c 20 69 74 0a 2a 2a 20 63 6f 6e 76 65 72 74  s, it.** convert
18318 73 20 61 6e 20 4d 45 4d 5f 45 70 68 65 6d 20 73  s an MEM_Ephem s
18319 74 72 69 6e 67 20 69 6e 74 6f 20 61 6e 20 4d 45  tring into an ME
1831a 4d 5f 44 79 6e 20 73 74 72 69 6e 67 2e 0a 2a 2f  M_Dyn string..*/
1831b 0a 23 64 65 66 69 6e 65 20 44 65 65 70 68 65 6d  .#define Deephem
1831c 65 72 61 6c 69 7a 65 28 50 29 20 5c 0a 20 20 20  eralize(P) \.   
1831d 69 66 28 20 28 28 50 29 2d 3e 66 6c 61 67 73 26  if( ((P)->flags&
1831e 4d 45 4d 5f 45 70 68 65 6d 29 21 3d 30 20 5c 0a  MEM_Ephem)!=0 \.
1831f 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65         && sqlite
18320 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74  3VdbeMemMakeWrit
18321 65 61 62 6c 65 28 50 29 20 29 7b 20 67 6f 74 6f  eable(P) ){ goto
18322 20 6e 6f 5f 6d 65 6d 3b 7d 0a 0a 2f 2a 0a 2a 2a   no_mem;}../*.**
18323 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 56 64 62   Call sqlite3Vdb
18324 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 29  eMemExpandBlob()
18325 20 6f 6e 20 74 68 65 20 73 75 70 70 6c 69 65 64   on the supplied
18326 20 76 61 6c 75 65 20 28 74 79 70 65 20 4d 65 6d   value (type Mem
18327 2a 29 0a 2a 2a 20 50 20 69 66 20 72 65 71 75 69  *).** P if requi
18328 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  red..*/.#define 
18329 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 20 28 28  ExpandBlob(P) ((
1832a 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 5a  (P)->flags&MEM_Z
1832b 65 72 6f 29 3f 73 71 6c 69 74 65 33 56 64 62 65  ero)?sqlite3Vdbe
1832c 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 50 29  MemExpandBlob(P)
1832d 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d  :0)../*.** Argum
1832e 65 6e 74 20 70 4d 65 6d 20 70 6f 69 6e 74 73 20  ent pMem points 
1832f 61 74 20 61 20 72 65 67 69 73 74 65 72 20 74 68  at a register th
18330 61 74 20 77 69 6c 6c 20 62 65 20 70 61 73 73 65  at will be passe
18331 64 20 74 6f 20 61 0a 2a 2a 20 75 73 65 72 2d 64  d to a.** user-d
18332 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20  efined function 
18333 6f 72 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74  or returned to t
18334 68 65 20 75 73 65 72 20 61 73 20 74 68 65 20 72  he user as the r
18335 65 73 75 6c 74 20 6f 66 20 61 20 71 75 65 72 79  esult of a query
18336 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  ..** The second 
18337 61 72 67 75 6d 65 6e 74 2c 20 27 64 62 5f 65 6e  argument, 'db_en
18338 63 27 20 69 73 20 74 68 65 20 74 65 78 74 20 65  c' is the text e
18339 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79 20  ncoding used by 
1833a 74 68 65 20 76 64 62 65 20 66 6f 72 0a 2a 2a 20  the vdbe for.** 
1833b 72 65 67 69 73 74 65 72 20 76 61 72 69 61 62 6c  register variabl
1833c 65 73 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  es.  This routin
1833d 65 20 73 65 74 73 20 74 68 65 20 70 4d 65 6d 2d  e sets the pMem-
1833e 3e 65 6e 63 20 61 6e 64 20 70 4d 65 6d 2d 3e 74  >enc and pMem->t
1833f 79 70 65 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73  ype.** variables
18340 20 75 73 65 64 20 62 79 20 74 68 65 20 73 71 6c   used by the sql
18341 69 74 65 33 5f 76 61 6c 75 65 5f 2a 28 29 20 72  ite3_value_*() r
18342 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 23 64 65 66  outines..*/.#def
18343 69 6e 65 20 73 74 6f 72 65 54 79 70 65 49 6e 66  ine storeTypeInf
18344 6f 28 41 2c 42 29 20 5f 73 74 6f 72 65 54 79 70  o(A,B) _storeTyp
18345 65 49 6e 66 6f 28 41 29 0a 73 74 61 74 69 63 20  eInfo(A).static 
18346 76 6f 69 64 20 5f 73 74 6f 72 65 54 79 70 65 49  void _storeTypeI
18347 6e 66 6f 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a  nfo(Mem *pMem){.
18348 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 4d    int flags = pM
18349 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28  em->flags;.  if(
1834a 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c   flags & MEM_Nul
1834b 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74  l ){.    pMem->t
1834c 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c  ype = SQLITE_NUL
1834d 4c 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 20 69 66  L;.  }.  else if
1834e 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ( flags & MEM_In
1834f 74 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74  t ){.    pMem->t
18350 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ype = SQLITE_INT
18351 45 47 45 52 3b 0a 20 20 7d 0a 20 20 65 6c 73 65  EGER;.  }.  else
18352 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d   if( flags & MEM
18353 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 70 4d 65  _Real ){.    pMe
18354 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  m->type = SQLITE
18355 5f 46 4c 4f 41 54 3b 0a 20 20 7d 0a 20 20 65 6c  _FLOAT;.  }.  el
18356 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d  se if( flags & M
18357 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 70 4d  EM_Str ){.    pM
18358 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54  em->type = SQLIT
18359 45 5f 54 45 58 54 3b 0a 20 20 7d 65 6c 73 65 7b  E_TEXT;.  }else{
1835a 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20  .    pMem->type 
1835b 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20  = SQLITE_BLOB;. 
1835c 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 70   }.}../*.** Prop
1835d 65 72 74 69 65 73 20 6f 66 20 6f 70 63 6f 64 65  erties of opcode
1835e 73 2e 20 20 54 68 65 20 4f 50 46 4c 47 5f 49 4e  s.  The OPFLG_IN
1835f 49 54 49 41 4c 49 5a 45 52 20 6d 61 63 72 6f 20  ITIALIZER macro 
18360 69 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79  is.** created by
18361 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 64   mkopcodeh.awk d
18362 75 72 69 6e 67 20 63 6f 6d 70 69 6c 61 74 69 6f  uring compilatio
18363 6e 2e 20 20 44 61 74 61 20 69 73 20 6f 62 74 61  n.  Data is obta
18364 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65  ined.** from the
18365 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f 77   comments follow
18366 69 6e 67 20 74 68 65 20 22 63 61 73 65 20 4f 50  ing the "case OP
18367 5f 78 78 78 78 3a 22 20 73 74 61 74 65 6d 65 6e  _xxxx:" statemen
18368 74 73 20 69 6e 0a 2a 2a 20 74 68 69 73 20 66 69  ts in.** this fi
18369 6c 65 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20  le.  .*/.static 
1836a 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
1836b 68 61 72 20 6f 70 63 6f 64 65 50 72 6f 70 65 72  har opcodeProper
1836c 74 79 5b 5d 20 3d 20 4f 50 46 4c 47 5f 49 4e 49  ty[] = OPFLG_INI
1836d 54 49 41 4c 49 5a 45 52 3b 0a 0a 2f 2a 0a 2a 2a  TIALIZER;../*.**
1836e 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
1836f 61 6e 20 6f 70 63 6f 64 65 20 68 61 73 20 61 6e  an opcode has an
18370 79 20 6f 66 20 74 68 65 20 4f 50 46 4c 47 5f 78  y of the OPFLG_x
18371 78 78 20 70 72 6f 70 65 72 74 69 65 73 0a 2a 2a  xx properties.**
18372 20 73 70 65 63 69 66 69 65 64 20 62 79 20 6d 61   specified by ma
18373 73 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  sk..*/.SQLITE_PR
18374 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
18375 33 56 64 62 65 4f 70 63 6f 64 65 48 61 73 50 72  3VdbeOpcodeHasPr
18376 6f 70 65 72 74 79 28 69 6e 74 20 6f 70 63 6f 64  operty(int opcod
18377 65 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a 20 20  e, int mask){.  
18378 61 73 73 65 72 74 28 20 6f 70 63 6f 64 65 3e 30  assert( opcode>0
18379 20 26 26 20 6f 70 63 6f 64 65 3c 73 69 7a 65 6f   && opcode<sizeo
1837a 66 28 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79  f(opcodeProperty
1837b 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 6f  ) );.  return (o
1837c 70 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 6f 70  pcodeProperty[op
1837d 63 6f 64 65 5d 26 6d 61 73 6b 29 21 3d 30 3b 0a  code]&mask)!=0;.
1837e 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  }../*.** Allocat
1837f 65 20 56 64 62 65 43 75 72 73 6f 72 20 6e 75 6d  e VdbeCursor num
18380 62 65 72 20 69 43 75 72 2e 20 20 52 65 74 75 72  ber iCur.  Retur
18381 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  n a pointer to i
18382 74 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 0a  t.  Return NULL.
18383 2a 2a 20 69 66 20 77 65 20 72 75 6e 20 6f 75 74  ** if we run out
18384 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73   of memory..*/.s
18385 74 61 74 69 63 20 56 64 62 65 43 75 72 73 6f 72  tatic VdbeCursor
18386 20 2a 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72   *allocateCursor
18387 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20  (.  Vdbe *p,    
18388 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
18389 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
1838a 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20   */.  int iCur, 
1838b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
1838c 6e 64 65 78 20 6f 66 20 74 68 65 20 6e 65 77 20  ndex of the new 
1838d 56 64 62 65 43 75 72 73 6f 72 20 2a 2f 0a 20 20  VdbeCursor */.  
1838e 4f 70 20 2a 70 4f 70 2c 20 20 20 20 20 20 20 20  Op *pOp,        
1838f 20 20 20 20 20 20 2f 2a 20 2a 2f 0a 20 20 69 6e        /* */.  in
18390 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 20 20  t iDb,          
18391 20 20 20 20 2f 2a 20 2a 2f 0a 20 20 69 6e 74 20      /* */.  int 
18392 69 73 42 74 72 65 65 43 75 72 73 6f 72 20 20 20  isBtreeCursor   
18393 20 20 2f 2a 20 2a 2f 0a 29 7b 0a 20 20 2f 2a 20    /* */.){.  /* 
18394 46 69 6e 64 20 74 68 65 20 6d 65 6d 6f 72 79 20  Find the memory 
18395 63 65 6c 6c 20 74 68 61 74 20 77 69 6c 6c 20 62  cell that will b
18396 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  e used to store 
18397 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f  the blob of memo
18398 72 79 0a 20 20 2a 2a 20 72 65 71 75 69 72 65 64  ry.  ** required
18399 20 66 6f 72 20 74 68 69 73 20 56 64 62 65 43 75   for this VdbeCu
1839a 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 2e 20  rsor structure. 
1839b 49 74 20 69 73 20 63 6f 6e 76 65 6e 69 65 6e 74  It is convenient
1839c 20 74 6f 20 75 73 65 20 61 20 0a 20 20 2a 2a 20   to use a .  ** 
1839d 76 64 62 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  vdbe memory cell
1839e 20 74 6f 20 6d 61 6e 61 67 65 20 74 68 65 20 6d   to manage the m
1839f 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
183a0 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 0a   required for a.
183a1 20 20 2a 2a 20 56 64 62 65 43 75 72 73 6f 72 20    ** VdbeCursor 
183a2 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 74 68  structure for th
183a3 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 61 73  e following reas
183a4 6f 6e 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  ons:.  **.  **  
183a5 20 2a 20 53 6f 6d 65 74 69 6d 65 73 20 63 75 72   * Sometimes cur
183a6 73 6f 72 20 6e 75 6d 62 65 72 73 20 61 72 65 20  sor numbers are 
183a7 75 73 65 64 20 66 6f 72 20 61 20 63 6f 75 70 6c  used for a coupl
183a8 65 20 6f 66 20 64 69 66 66 65 72 65 6e 74 0a 20  e of different. 
183a9 20 2a 2a 20 20 20 20 20 70 75 72 70 6f 73 65 73   **     purposes
183aa 20 69 6e 20 61 20 76 64 62 65 20 70 72 6f 67 72   in a vdbe progr
183ab 61 6d 2e 20 54 68 65 20 64 69 66 66 65 72 65 6e  am. The differen
183ac 74 20 75 73 65 73 20 6d 69 67 68 74 20 72 65 71  t uses might req
183ad 75 69 72 65 0a 20 20 2a 2a 20 20 20 20 20 64 69  uire.  **     di
183ae 66 66 65 72 65 6e 74 20 73 69 7a 65 64 20 61 6c  fferent sized al
183af 6c 6f 63 61 74 69 6f 6e 73 2e 20 4d 65 6d 6f 72  locations. Memor
183b0 79 20 63 65 6c 6c 73 20 70 72 6f 76 69 64 65 20  y cells provide 
183b1 67 72 6f 77 61 62 6c 65 0a 20 20 2a 2a 20 20 20  growable.  **   
183b2 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20    allocations.. 
183b3 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65   **.  **   * Whe
183b4 6e 20 75 73 69 6e 67 20 45 4e 41 42 4c 45 5f 4d  n using ENABLE_M
183b5 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54  EMORY_MANAGEMENT
183b6 2c 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 62 75  , memory cell bu
183b7 66 66 65 72 73 20 63 61 6e 0a 20 20 2a 2a 20 20  ffers can.  **  
183b8 20 20 20 62 65 20 66 72 65 65 64 20 6c 61 7a 69     be freed lazi
183b9 6c 79 20 76 69 61 20 74 68 65 20 73 71 6c 69 74  ly via the sqlit
183ba 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72  e3_release_memor
183bb 79 28 29 20 41 50 49 2e 20 54 68 69 73 0a 20 20  y() API. This.  
183bc 2a 2a 20 20 20 20 20 6d 69 6e 69 6d 69 7a 65 73  **     minimizes
183bd 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d   the number of m
183be 61 6c 6c 6f 63 20 63 61 6c 6c 73 20 6d 61 64 65  alloc calls made
183bf 20 62 79 20 74 68 65 20 73 79 73 74 65 6d 2e 0a   by the system..
183c0 20 20 2a 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79    **.  ** Memory
183c1 20 63 65 6c 6c 73 20 66 6f 72 20 63 75 72 73 6f   cells for curso
183c2 72 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64  rs are allocated
183c3 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66 20 74   at the top of t
183c4 68 65 20 61 64 64 72 65 73 73 0a 20 20 2a 2a 20  he address.  ** 
183c5 73 70 61 63 65 2e 20 4d 65 6d 6f 72 79 20 63 65  space. Memory ce
183c6 6c 6c 20 28 70 2d 3e 6e 4d 65 6d 29 20 63 6f 72  ll (p->nMem) cor
183c7 72 65 73 70 6f 6e 64 73 20 74 6f 20 63 75 72 73  responds to curs
183c8 6f 72 20 30 2e 20 53 70 61 63 65 20 66 6f 72 0a  or 0. Space for.
183c9 20 20 2a 2a 20 63 75 72 73 6f 72 20 31 20 69 73    ** cursor 1 is
183ca 20 6d 61 6e 61 67 65 64 20 62 79 20 6d 65 6d 6f   managed by memo
183cb 72 79 20 63 65 6c 6c 20 28 70 2d 3e 6e 4d 65 6d  ry cell (p->nMem
183cc 2d 31 29 2c 20 65 74 63 2e 0a 20 20 2a 2f 0a 20  -1), etc..  */. 
183cd 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 26 70 2d   Mem *pMem = &p-
183ce 3e 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 2d 69 43  >aMem[p->nMem-iC
183cf 75 72 5d 3b 0a 0a 20 20 69 6e 74 20 6e 42 79 74  ur];..  int nByt
183d0 65 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  e;.  VdbeCursor 
183d1 2a 70 43 78 20 3d 20 30 3b 0a 20 20 2f 2a 20 49  *pCx = 0;.  /* I
183d2 66 20 74 68 65 20 6f 70 63 6f 64 65 20 6f 66 20  f the opcode of 
183d3 70 4f 70 20 69 73 20 4f 50 5f 53 65 74 4e 75 6d  pOp is OP_SetNum
183d4 43 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 70 4f  Columns, then pO
183d5 70 2d 3e 70 32 20 63 6f 6e 74 61 69 6e 73 0a 20  p->p2 contains. 
183d6 20 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   ** the number o
183d7 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20  f fields in the 
183d8 72 65 63 6f 72 64 73 20 63 6f 6e 74 61 69 6e 65  records containe
183d9 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 6f  d in the table o
183da 72 0a 20 20 2a 2a 20 69 6e 64 65 78 20 62 65 69  r.  ** index bei
183db 6e 67 20 6f 70 65 6e 65 64 2e 20 55 73 65 20 74  ng opened. Use t
183dc 68 69 73 20 74 6f 20 72 65 73 65 72 76 65 20 73  his to reserve s
183dd 70 61 63 65 20 66 6f 72 20 74 68 65 20 0a 20 20  pace for the .  
183de 2a 2a 20 56 64 62 65 43 75 72 73 6f 72 2e 61 54  ** VdbeCursor.aT
183df 79 70 65 5b 5d 20 61 72 72 61 79 2e 0a 20 20 2a  ype[] array..  *
183e0 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 20 3d  /.  int nField =
183e1 20 30 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f   0;.  if( pOp->o
183e2 70 63 6f 64 65 3d 3d 4f 50 5f 53 65 74 4e 75 6d  pcode==OP_SetNum
183e3 43 6f 6c 75 6d 6e 73 20 7c 7c 20 70 4f 70 2d 3e  Columns || pOp->
183e4 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e 45  opcode==OP_OpenE
183e5 70 68 65 6d 65 72 61 6c 20 29 7b 0a 20 20 20 20  phemeral ){.    
183e6 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 32  nField = pOp->p2
183e7 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20  ;.  }.  nByte = 
183e8 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 56 64  .      sizeof(Vd
183e9 62 65 43 75 72 73 6f 72 29 20 2b 20 0a 20 20 20  beCursor) + .   
183ea 20 20 20 28 69 73 42 74 72 65 65 43 75 72 73 6f     (isBtreeCurso
183eb 72 3f 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  r?sqlite3BtreeCu
183ec 72 73 6f 72 53 69 7a 65 28 29 3a 30 29 20 2b 20  rsorSize():0) + 
183ed 0a 20 20 20 20 20 20 32 2a 6e 46 69 65 6c 64 2a  .      2*nField*
183ee 73 69 7a 65 6f 66 28 75 33 32 29 3b 0a 0a 20 20  sizeof(u32);..  
183ef 61 73 73 65 72 74 28 20 69 43 75 72 3c 70 2d 3e  assert( iCur<p->
183f0 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 69 66 28  nCursor );.  if(
183f1 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 20   p->apCsr[iCur] 
183f2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
183f3 62 65 46 72 65 65 43 75 72 73 6f 72 28 70 2c 20  beFreeCursor(p, 
183f4 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 29 3b  p->apCsr[iCur]);
183f5 0a 20 20 20 20 70 2d 3e 61 70 43 73 72 5b 69 43  .    p->apCsr[iC
183f6 75 72 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69  ur] = 0;.  }.  i
183f7 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71  f( SQLITE_OK==sq
183f8 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77  lite3VdbeMemGrow
183f9 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 30 29  (pMem, nByte, 0)
183fa 20 29 7b 0a 20 20 20 20 70 2d 3e 61 70 43 73 72   ){.    p->apCsr
183fb 5b 69 43 75 72 5d 20 3d 20 70 43 78 20 3d 20 28  [iCur] = pCx = (
183fc 56 64 62 65 43 75 72 73 6f 72 2a 29 70 4d 65 6d  VdbeCursor*)pMem
183fd 2d 3e 7a 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  ->z;.    memset(
183fe 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e 42 79 74  pMem->z, 0, nByt
183ff 65 29 3b 0a 20 20 20 20 70 43 78 2d 3e 69 44 62  e);.    pCx->iDb
18400 20 3d 20 69 44 62 3b 0a 20 20 20 20 70 43 78 2d   = iDb;.    pCx-
18401 3e 6e 46 69 65 6c 64 20 3d 20 6e 46 69 65 6c 64  >nField = nField
18402 3b 0a 20 20 20 20 69 66 28 20 6e 46 69 65 6c 64  ;.    if( nField
18403 20 29 7b 0a 20 20 20 20 20 20 70 43 78 2d 3e 61   ){.      pCx->a
18404 54 79 70 65 20 3d 20 28 75 33 32 20 2a 29 26 70  Type = (u32 *)&p
18405 4d 65 6d 2d 3e 7a 5b 73 69 7a 65 6f 66 28 56 64  Mem->z[sizeof(Vd
18406 62 65 43 75 72 73 6f 72 29 5d 3b 0a 20 20 20 20  beCursor)];.    
18407 7d 0a 20 20 20 20 69 66 28 20 69 73 42 74 72 65  }.    if( isBtre
18408 65 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 20  eCursor ){.     
18409 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 3d 20   pCx->pCursor = 
1840a 28 42 74 43 75 72 73 6f 72 2a 29 0a 20 20 20 20  (BtCursor*).    
1840b 20 20 20 20 20 20 26 70 4d 65 6d 2d 3e 7a 5b 73        &pMem->z[s
1840c 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72  izeof(VdbeCursor
1840d 29 2b 32 2a 6e 46 69 65 6c 64 2a 73 69 7a 65 6f  )+2*nField*sizeo
1840e 66 28 75 33 32 29 5d 3b 0a 20 20 20 20 7d 0a 20  f(u32)];.    }. 
1840f 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 78 3b   }.  return pCx;
18410 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f  .}../*.** Try to
18411 20 63 6f 6e 76 65 72 74 20 61 20 76 61 6c 75 65   convert a value
18412 20 69 6e 74 6f 20 61 20 6e 75 6d 65 72 69 63 20   into a numeric 
18413 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69  representation i
18414 66 20 77 65 20 63 61 6e 0a 2a 2a 20 64 6f 20 73  f we can.** do s
18415 6f 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f  o without loss o
18416 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20  f information.  
18417 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
18418 69 66 20 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a  if the string.**
18419 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75   looks like a nu
1841a 6d 62 65 72 2c 20 63 6f 6e 76 65 72 74 20 69 74  mber, convert it
1841b 20 69 6e 74 6f 20 61 20 6e 75 6d 62 65 72 2e 20   into a number. 
1841c 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 0a   If it does not.
1841d 2a 2a 20 6c 6f 6f 6b 20 6c 69 6b 65 20 61 20 6e  ** look like a n
1841e 75 6d 62 65 72 2c 20 6c 65 61 76 65 20 69 74 20  umber, leave it 
1841f 61 6c 6f 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  alone..*/.static
18420 20 76 6f 69 64 20 61 70 70 6c 79 4e 75 6d 65 72   void applyNumer
18421 69 63 41 66 66 69 6e 69 74 79 28 4d 65 6d 20 2a  icAffinity(Mem *
18422 70 52 65 63 29 7b 0a 20 20 69 66 28 20 28 70 52  pRec){.  if( (pR
18423 65 63 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d  ec->flags & (MEM
18424 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29 3d  _Real|MEM_Int))=
18425 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65  =0 ){.    int re
18426 61 6c 6e 75 6d 3b 0a 20 20 20 20 73 71 6c 69 74  alnum;.    sqlit
18427 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d  e3VdbeMemNulTerm
18428 69 6e 61 74 65 28 70 52 65 63 29 3b 0a 20 20 20  inate(pRec);.   
18429 20 69 66 28 20 28 70 52 65 63 2d 3e 66 6c 61 67   if( (pRec->flag
1842a 73 26 4d 45 4d 5f 53 74 72 29 0a 20 20 20 20 20  s&MEM_Str).     
1842b 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 49 73      && sqlite3Is
1842c 4e 75 6d 62 65 72 28 70 52 65 63 2d 3e 7a 2c 20  Number(pRec->z, 
1842d 26 72 65 61 6c 6e 75 6d 2c 20 70 52 65 63 2d 3e  &realnum, pRec->
1842e 65 6e 63 29 20 29 7b 0a 20 20 20 20 20 20 69 36  enc) ){.      i6
1842f 34 20 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 73  4 value;.      s
18430 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
18431 45 6e 63 6f 64 69 6e 67 28 70 52 65 63 2c 20 53  Encoding(pRec, S
18432 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 20  QLITE_UTF8);.   
18433 20 20 20 69 66 28 20 21 72 65 61 6c 6e 75 6d 20     if( !realnum 
18434 26 26 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34  && sqlite3Atoi64
18435 28 70 52 65 63 2d 3e 7a 2c 20 26 76 61 6c 75 65  (pRec->z, &value
18436 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 52 65  ) ){.        pRe
18437 63 2d 3e 75 2e 69 20 3d 20 76 61 6c 75 65 3b 0a  c->u.i = value;.
18438 20 20 20 20 20 20 20 20 4d 65 6d 53 65 74 54 79          MemSetTy
18439 70 65 46 6c 61 67 28 70 52 65 63 2c 20 4d 45 4d  peFlag(pRec, MEM
1843a 5f 49 6e 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c  _Int);.      }el
1843b 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
1843c 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66  te3VdbeMemRealif
1843d 79 28 70 52 65 63 29 3b 0a 20 20 20 20 20 20 7d  y(pRec);.      }
1843e 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
1843f 0a 2a 2a 20 50 72 6f 63 65 73 73 69 6e 67 20 69  .** Processing i
18440 73 20 64 65 74 65 72 6d 69 6e 65 20 62 79 20 74  s determine by t
18441 68 65 20 61 66 66 69 6e 69 74 79 20 70 61 72 61  he affinity para
18442 6d 65 74 65 72 3a 0a 2a 2a 0a 2a 2a 20 53 51 4c  meter:.**.** SQL
18443 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3a  ITE_AFF_INTEGER:
18444 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 52  .** SQLITE_AFF_R
18445 45 41 4c 3a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41  EAL:.** SQLITE_A
18446 46 46 5f 4e 55 4d 45 52 49 43 3a 0a 2a 2a 20 20  FF_NUMERIC:.**  
18447 20 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 72 74    Try to convert
18448 20 70 52 65 63 20 74 6f 20 61 6e 20 69 6e 74 65   pRec to an inte
18449 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 74 69  ger representati
1844a 6f 6e 20 6f 72 20 61 20 0a 2a 2a 20 20 20 20 66  on or a .**    f
1844b 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 72 65  loating-point re
1844c 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 66 20  presentation if 
1844d 61 6e 20 69 6e 74 65 67 65 72 20 72 65 70 72 65  an integer repre
1844e 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 20 20 20  sentation.**    
1844f 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e  is not possible.
18450 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20    Note that the 
18451 69 6e 74 65 67 65 72 20 72 65 70 72 65 73 65 6e  integer represen
18452 74 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 20 20 20  tation is.**    
18453 61 6c 77 61 79 73 20 70 72 65 66 65 72 72 65 64  always preferred
18454 2c 20 65 76 65 6e 20 69 66 20 74 68 65 20 61 66  , even if the af
18455 66 69 6e 69 74 79 20 69 73 20 52 45 41 4c 2c 20  finity is REAL, 
18456 62 65 63 61 75 73 65 0a 2a 2a 20 20 20 20 61 6e  because.**    an
18457 20 69 6e 74 65 67 65 72 20 72 65 70 72 65 73 65   integer represe
18458 6e 74 61 74 69 6f 6e 20 69 73 20 6d 6f 72 65 20  ntation is more 
18459 73 70 61 63 65 20 65 66 66 69 63 69 65 6e 74 20  space efficient 
1845a 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 53  on disk..**.** S
1845b 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a  QLITE_AFF_TEXT:.
1845c 2a 2a 20 20 20 20 43 6f 6e 76 65 72 74 20 70 52  **    Convert pR
1845d 65 63 20 74 6f 20 61 20 74 65 78 74 20 72 65 70  ec to a text rep
1845e 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a  resentation..**.
1845f 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f  ** SQLITE_AFF_NO
18460 4e 45 3a 0a 2a 2a 20 20 20 20 4e 6f 2d 6f 70 2e  NE:.**    No-op.
18461 20 20 70 52 65 63 20 69 73 20 75 6e 63 68 61 6e    pRec is unchan
18462 67 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ged..*/.static v
18463 6f 69 64 20 61 70 70 6c 79 41 66 66 69 6e 69 74  oid applyAffinit
18464 79 28 0a 20 20 4d 65 6d 20 2a 70 52 65 63 2c 20  y(.  Mem *pRec, 
18465 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
18466 76 61 6c 75 65 20 74 6f 20 61 70 70 6c 79 20 61  value to apply a
18467 66 66 69 6e 69 74 79 20 74 6f 20 2a 2f 0a 20 20  ffinity to */.  
18468 63 68 61 72 20 61 66 66 69 6e 69 74 79 2c 20 20  char affinity,  
18469 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e      /* The affin
1846a 69 74 79 20 74 6f 20 62 65 20 61 70 70 6c 69 65  ity to be applie
1846b 64 20 2a 2f 0a 20 20 75 38 20 65 6e 63 20 20 20  d */.  u8 enc   
1846c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
1846d 65 20 74 68 69 73 20 74 65 78 74 20 65 6e 63 6f  e this text enco
1846e 64 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 66 28  ding */.){.  if(
1846f 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54   affinity==SQLIT
18470 45 5f 41 46 46 5f 54 45 58 54 20 29 7b 0a 20 20  E_AFF_TEXT ){.  
18471 20 20 2f 2a 20 4f 6e 6c 79 20 61 74 74 65 6d 70    /* Only attemp
18472 74 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  t the conversion
18473 20 74 6f 20 54 45 58 54 20 69 66 20 74 68 65 72   to TEXT if ther
18474 65 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20  e is an integer 
18475 6f 72 20 72 65 61 6c 0a 20 20 20 20 2a 2a 20 72  or real.    ** r
18476 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 28 62  epresentation (b
18477 6c 6f 62 20 61 6e 64 20 4e 55 4c 4c 20 64 6f 20  lob and NULL do 
18478 6e 6f 74 20 67 65 74 20 63 6f 6e 76 65 72 74 65  not get converte
18479 64 29 20 62 75 74 20 6e 6f 20 73 74 72 69 6e 67  d) but no string
1847a 0a 20 20 20 20 2a 2a 20 72 65 70 72 65 73 65 6e  .    ** represen
1847b 74 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20  tation..    */. 
1847c 20 20 20 69 66 28 20 30 3d 3d 28 70 52 65 63 2d     if( 0==(pRec-
1847d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29 20  >flags&MEM_Str) 
1847e 26 26 20 28 70 52 65 63 2d 3e 66 6c 61 67 73 26  && (pRec->flags&
1847f 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e  (MEM_Real|MEM_In
18480 74 29 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  t)) ){.      sql
18481 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e  ite3VdbeMemStrin
18482 67 69 66 79 28 70 52 65 63 2c 20 65 6e 63 29 3b  gify(pRec, enc);
18483 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 65 63 2d  .    }.    pRec-
18484 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f  >flags &= ~(MEM_
18485 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 3b 0a 20  Real|MEM_Int);. 
18486 20 7d 65 6c 73 65 20 69 66 28 20 61 66 66 69 6e   }else if( affin
18487 69 74 79 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f  ity!=SQLITE_AFF_
18488 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 61 73 73 65  NONE ){.    asse
18489 72 74 28 20 61 66 66 69 6e 69 74 79 3d 3d 53 51  rt( affinity==SQ
1848a 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52  LITE_AFF_INTEGER
1848b 20 7c 7c 20 61 66 66 69 6e 69 74 79 3d 3d 53 51   || affinity==SQ
1848c 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 20 20  LITE_AFF_REAL.  
1848d 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 61 66             || af
1848e 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41  finity==SQLITE_A
1848f 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20 20  FF_NUMERIC );.  
18490 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66    applyNumericAf
18491 66 69 6e 69 74 79 28 70 52 65 63 29 3b 0a 20 20  finity(pRec);.  
18492 20 20 69 66 28 20 70 52 65 63 2d 3e 66 6c 61 67    if( pRec->flag
18493 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a  s & MEM_Real ){.
18494 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
18495 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79  eIntegerAffinity
18496 28 70 52 65 63 29 3b 0a 20 20 20 20 7d 0a 20 20  (pRec);.    }.  
18497 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74  }.}../*.** Try t
18498 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20 74 79  o convert the ty
18499 70 65 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e  pe of a function
1849a 20 61 72 67 75 6d 65 6e 74 20 6f 72 20 61 20 72   argument or a r
1849b 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20  esult column.** 
1849c 69 6e 74 6f 20 61 20 6e 75 6d 65 72 69 63 20 72  into a numeric r
1849d 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 20 20  epresentation.  
1849e 55 73 65 20 65 69 74 68 65 72 20 49 4e 54 45 47  Use either INTEG
1849f 45 52 20 6f 72 20 52 45 41 4c 20 77 68 69 63 68  ER or REAL which
184a0 65 76 65 72 0a 2a 2a 20 69 73 20 61 70 70 72 6f  ever.** is appro
184a1 70 72 69 61 74 65 2e 20 20 42 75 74 20 6f 6e 6c  priate.  But onl
184a2 79 20 64 6f 20 74 68 65 20 63 6f 6e 76 65 72 73  y do the convers
184a3 69 6f 6e 20 69 66 20 69 74 20 69 73 20 70 6f 73  ion if it is pos
184a4 73 69 62 6c 65 20 77 69 74 68 6f 75 74 0a 2a 2a  sible without.**
184a5 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61   loss of informa
184a6 74 69 6f 6e 20 61 6e 64 20 72 65 74 75 72 6e 20  tion and return 
184a7 74 68 65 20 72 65 76 69 73 65 64 20 74 79 70 65  the revised type
184a8 20 6f 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74   of the argument
184a9 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ..**.** This is 
184aa 61 6e 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 20  an EXPERIMENTAL 
184ab 61 70 69 20 61 6e 64 20 69 73 20 73 75 62 6a 65  api and is subje
184ac 63 74 20 74 6f 20 63 68 61 6e 67 65 20 6f 72 20  ct to change or 
184ad 72 65 6d 6f 76 61 6c 2e 0a 2a 2f 0a 53 51 4c 49  removal..*/.SQLI
184ae 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
184af 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63  e3_value_numeric
184b0 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61  _type(sqlite3_va
184b1 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 4d 65  lue *pVal){.  Me
184b2 6d 20 2a 70 4d 65 6d 20 3d 20 28 4d 65 6d 2a 29  m *pMem = (Mem*)
184b3 70 56 61 6c 3b 0a 20 20 61 70 70 6c 79 4e 75 6d  pVal;.  applyNum
184b4 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 4d 65  ericAffinity(pMe
184b5 6d 29 3b 0a 20 20 73 74 6f 72 65 54 79 70 65 49  m);.  storeTypeI
184b6 6e 66 6f 28 70 4d 65 6d 2c 20 30 29 3b 0a 20 20  nfo(pMem, 0);.  
184b7 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 74 79 70  return pMem->typ
184b8 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 70 6f  e;.}../*.** Expo
184b9 72 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20  rted version of 
184ba 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 29 2e  applyAffinity().
184bb 20 54 68 69 73 20 6f 6e 65 20 77 6f 72 6b 73 20   This one works 
184bc 6f 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  on sqlite3_value
184bd 2a 2c 20 0a 2a 2a 20 6e 6f 74 20 74 68 65 20 69  *, .** not the i
184be 6e 74 65 72 6e 61 6c 20 4d 65 6d 2a 20 74 79 70  nternal Mem* typ
184bf 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
184c0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
184c1 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e  3ValueApplyAffin
184c2 69 74 79 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  ity(.  sqlite3_v
184c3 61 6c 75 65 20 2a 70 56 61 6c 2c 20 0a 20 20 75  alue *pVal, .  u
184c4 38 20 61 66 66 69 6e 69 74 79 2c 20 0a 20 20 75  8 affinity, .  u
184c5 38 20 65 6e 63 0a 29 7b 0a 20 20 61 70 70 6c 79  8 enc.){.  apply
184c6 41 66 66 69 6e 69 74 79 28 28 4d 65 6d 20 2a 29  Affinity((Mem *)
184c7 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c 20  pVal, affinity, 
184c8 65 6e 63 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20  enc);.}..#ifdef 
184c9 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a  SQLITE_DEBUG./*.
184ca 2a 2a 20 57 72 69 74 65 20 61 20 6e 69 63 65 20  ** Write a nice 
184cb 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74  string represent
184cc 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 6e  ation of the con
184cd 74 65 6e 74 73 20 6f 66 20 63 65 6c 6c 20 70 4d  tents of cell pM
184ce 65 6d 0a 2a 2a 20 69 6e 74 6f 20 62 75 66 66 65  em.** into buffe
184cf 72 20 7a 42 75 66 2c 20 6c 65 6e 67 74 68 20 6e  r zBuf, length n
184d0 42 75 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  Buf..*/.SQLITE_P
184d1 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
184d2 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79  te3VdbeMemPretty
184d3 50 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c  Print(Mem *pMem,
184d4 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20   char *zBuf){.  
184d5 63 68 61 72 20 2a 7a 43 73 72 20 3d 20 7a 42 75  char *zCsr = zBu
184d6 66 3b 0a 20 20 69 6e 74 20 66 20 3d 20 70 4d 65  f;.  int f = pMe
184d7 6d 2d 3e 66 6c 61 67 73 3b 0a 0a 20 20 73 74 61  m->flags;..  sta
184d8 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
184d9 63 6f 6e 73 74 20 65 6e 63 6e 61 6d 65 73 5b 5d  const encnames[]
184da 20 3d 20 7b 22 28 58 29 22 2c 20 22 28 38 29 22   = {"(X)", "(8)"
184db 2c 20 22 28 31 36 4c 45 29 22 2c 20 22 28 31 36  , "(16LE)", "(16
184dc 42 45 29 22 7d 3b 0a 0a 20 20 69 66 28 20 66 26  BE)"};..  if( f&
184dd 4d 45 4d 5f 42 6c 6f 62 20 29 7b 0a 20 20 20 20  MEM_Blob ){.    
184de 69 6e 74 20 69 3b 0a 20 20 20 20 63 68 61 72 20  int i;.    char 
184df 63 3b 0a 20 20 20 20 69 66 28 20 66 20 26 20 4d  c;.    if( f & M
184e0 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20 20  EM_Dyn ){.      
184e1 63 20 3d 20 27 7a 27 3b 0a 20 20 20 20 20 20 61  c = 'z';.      a
184e2 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d  ssert( (f & (MEM
184e3 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70 68 65  _Static|MEM_Ephe
184e4 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65  m))==0 );.    }e
184e5 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f  lse if( f & MEM_
184e6 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 20 20  Static ){.      
184e7 63 20 3d 20 27 74 27 3b 0a 20 20 20 20 20 20 61  c = 't';.      a
184e8 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d  ssert( (f & (MEM
184e9 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 29  _Dyn|MEM_Ephem))
184ea 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65  ==0 );.    }else
184eb 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 45 70 68   if( f & MEM_Eph
184ec 65 6d 20 29 7b 0a 20 20 20 20 20 20 63 20 3d 20  em ){.      c = 
184ed 27 65 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72  'e';.      asser
184ee 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61  t( (f & (MEM_Sta
184ef 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30  tic|MEM_Dyn))==0
184f0 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   );.    }else{. 
184f1 20 20 20 20 20 63 20 3d 20 27 73 27 3b 0a 20 20       c = 's';.  
184f2 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33    }..    sqlite3
184f3 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a  _snprintf(100, z
184f4 43 73 72 2c 20 22 25 63 22 2c 20 63 29 3b 0a 20  Csr, "%c", c);. 
184f5 20 20 20 7a 43 73 72 20 2b 3d 20 73 74 72 6c 65     zCsr += strle
184f6 6e 28 7a 43 73 72 29 3b 0a 20 20 20 20 73 71 6c  n(zCsr);.    sql
184f7 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30  ite3_snprintf(10
184f8 30 2c 20 7a 43 73 72 2c 20 22 25 64 5b 22 2c 20  0, zCsr, "%d[", 
184f9 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 7a 43  pMem->n);.    zC
184fa 73 72 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 43 73  sr += strlen(zCs
184fb 72 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  r);.    for(i=0;
184fc 20 69 3c 31 36 20 26 26 20 69 3c 70 4d 65 6d 2d   i<16 && i<pMem-
184fd 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  >n; i++){.      
184fe 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
184ff 28 31 30 30 2c 20 7a 43 73 72 2c 20 22 25 30 32  (100, zCsr, "%02
18500 58 22 2c 20 28 28 69 6e 74 29 70 4d 65 6d 2d 3e  X", ((int)pMem->
18501 7a 5b 69 5d 20 26 20 30 78 46 46 29 29 3b 0a 20  z[i] & 0xFF));. 
18502 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 74 72       zCsr += str
18503 6c 65 6e 28 7a 43 73 72 29 3b 0a 20 20 20 20 7d  len(zCsr);.    }
18504 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
18505 31 36 20 26 26 20 69 3c 70 4d 65 6d 2d 3e 6e 3b  16 && i<pMem->n;
18506 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 61   i++){.      cha
18507 72 20 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 69 5d  r z = pMem->z[i]
18508 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 3c 33 32  ;.      if( z<32
18509 20 7c 7c 20 7a 3e 31 32 36 20 29 20 2a 7a 43 73   || z>126 ) *zCs
1850a 72 2b 2b 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20  r++ = '.';.     
1850b 20 65 6c 73 65 20 2a 7a 43 73 72 2b 2b 20 3d 20   else *zCsr++ = 
1850c 7a 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71  z;.    }..    sq
1850d 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31  lite3_snprintf(1
1850e 30 30 2c 20 7a 43 73 72 2c 20 22 5d 25 73 22 2c  00, zCsr, "]%s",
1850f 20 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e   encnames[pMem->
18510 65 6e 63 5d 29 3b 0a 20 20 20 20 7a 43 73 72 20  enc]);.    zCsr 
18511 2b 3d 20 73 74 72 6c 65 6e 28 7a 43 73 72 29 3b  += strlen(zCsr);
18512 0a 20 20 20 20 69 66 28 20 66 20 26 20 4d 45 4d  .    if( f & MEM
18513 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 73  _Zero ){.      s
18514 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
18515 31 30 30 2c 20 7a 43 73 72 2c 22 2b 25 6c 6c 64  100, zCsr,"+%lld
18516 7a 22 2c 70 4d 65 6d 2d 3e 75 2e 69 29 3b 0a 20  z",pMem->u.i);. 
18517 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 74 72       zCsr += str
18518 6c 65 6e 28 7a 43 73 72 29 3b 0a 20 20 20 20 7d  len(zCsr);.    }
18519 0a 20 20 20 20 2a 7a 43 73 72 20 3d 20 27 5c 30  .    *zCsr = '\0
1851a 27 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66  ';.  }else if( f
1851b 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20   & MEM_Str ){.  
1851c 20 20 69 6e 74 20 6a 2c 20 6b 3b 0a 20 20 20 20    int j, k;.    
1851d 7a 42 75 66 5b 30 5d 20 3d 20 27 20 27 3b 0a 20  zBuf[0] = ' ';. 
1851e 20 20 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 44     if( f & MEM_D
1851f 79 6e 20 29 7b 0a 20 20 20 20 20 20 7a 42 75 66  yn ){.      zBuf
18520 5b 31 5d 20 3d 20 27 7a 27 3b 0a 20 20 20 20 20  [1] = 'z';.     
18521 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d   assert( (f & (M
18522 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70  EM_Static|MEM_Ep
18523 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20  hem))==0 );.    
18524 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45  }else if( f & ME
18525 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20  M_Static ){.    
18526 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 74 27 3b    zBuf[1] = 't';
18527 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28  .      assert( (
18528 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d  f & (MEM_Dyn|MEM
18529 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20  _Ephem))==0 );. 
1852a 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26     }else if( f &
1852b 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b 0a 20 20   MEM_Ephem ){.  
1852c 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 65      zBuf[1] = 'e
1852d 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ';.      assert(
1852e 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 69   (f & (MEM_Stati
1852f 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30 20 29  c|MEM_Dyn))==0 )
18530 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
18531 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 73 27     zBuf[1] = 's'
18532 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6b 20 3d 20  ;.    }.    k = 
18533 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  2;.    sqlite3_s
18534 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 26 7a 42  nprintf(100, &zB
18535 75 66 5b 6b 5d 2c 20 22 25 64 22 2c 20 70 4d 65  uf[k], "%d", pMe
18536 6d 2d 3e 6e 29 3b 0a 20 20 20 20 6b 20 2b 3d 20  m->n);.    k += 
18537 73 74 72 6c 65 6e 28 26 7a 42 75 66 5b 6b 5d 29  strlen(&zBuf[k])
18538 3b 0a 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20  ;.    zBuf[k++] 
18539 3d 20 27 5b 27 3b 0a 20 20 20 20 66 6f 72 28 6a  = '[';.    for(j
1853a 3d 30 3b 20 6a 3c 31 35 20 26 26 20 6a 3c 70 4d  =0; j<15 && j<pM
1853b 65 6d 2d 3e 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20  em->n; j++){.   
1853c 20 20 20 75 38 20 63 20 3d 20 70 4d 65 6d 2d 3e     u8 c = pMem->
1853d 7a 5b 6a 5d 3b 0a 20 20 20 20 20 20 69 66 28 20  z[j];.      if( 
1853e 63 3e 3d 30 78 32 30 20 26 26 20 63 3c 30 78 37  c>=0x20 && c<0x7
1853f 66 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 42 75  f ){.        zBu
18540 66 5b 6b 2b 2b 5d 20 3d 20 63 3b 0a 20 20 20 20  f[k++] = c;.    
18541 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
18542 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 2e 27   zBuf[k++] = '.'
18543 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
18544 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20      zBuf[k++] = 
18545 27 5d 27 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ']';.    sqlite3
18546 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 26 7a  _snprintf(100,&z
18547 42 75 66 5b 6b 5d 2c 20 65 6e 63 6e 61 6d 65 73  Buf[k], encnames
18548 5b 70 4d 65 6d 2d 3e 65 6e 63 5d 29 3b 0a 20 20  [pMem->enc]);.  
18549 20 20 6b 20 2b 3d 20 73 74 72 6c 65 6e 28 26 7a    k += strlen(&z
1854a 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a 42 75  Buf[k]);.    zBu
1854b 66 5b 6b 2b 2b 5d 20 3d 20 30 3b 0a 20 20 7d 0a  f[k++] = 0;.  }.
1854c 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  }.#endif..#ifdef
1854d 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a   SQLITE_DEBUG./*
1854e 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65 20 76 61  .** Print the va
1854f 6c 75 65 20 6f 66 20 61 20 72 65 67 69 73 74 65  lue of a registe
18550 72 20 66 6f 72 20 74 72 61 63 69 6e 67 20 70 75  r for tracing pu
18551 72 70 6f 73 65 73 3a 0a 2a 2f 0a 73 74 61 74 69  rposes:.*/.stati
18552 63 20 76 6f 69 64 20 6d 65 6d 54 72 61 63 65 50  c void memTraceP
18553 72 69 6e 74 28 46 49 4c 45 20 2a 6f 75 74 2c 20  rint(FILE *out, 
18554 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70  Mem *p){.  if( p
18555 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  ->flags & MEM_Nu
18556 6c 6c 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74  ll ){.    fprint
18557 66 28 6f 75 74 2c 20 22 20 4e 55 4c 4c 22 29 3b  f(out, " NULL");
18558 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 2d  .  }else if( (p-
18559 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 49 6e  >flags & (MEM_In
1855a 74 7c 4d 45 4d 5f 53 74 72 29 29 3d 3d 28 4d 45  t|MEM_Str))==(ME
1855b 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 20 29  M_Int|MEM_Str) )
1855c 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75  {.    fprintf(ou
1855d 74 2c 20 22 20 73 69 3a 25 6c 6c 64 22 2c 20 70  t, " si:%lld", p
1855e 2d 3e 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 20  ->u.i);.  }else 
1855f 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d  if( p->flags & M
18560 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 66 70  EM_Int ){.    fp
18561 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 69 3a 25  rintf(out, " i:%
18562 6c 6c 64 22 2c 20 70 2d 3e 75 2e 69 29 3b 0a 20  lld", p->u.i);. 
18563 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c   }else if( p->fl
18564 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29  ags & MEM_Real )
18565 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75  {.    fprintf(ou
18566 74 2c 20 22 20 72 3a 25 67 22 2c 20 70 2d 3e 72  t, " r:%g", p->r
18567 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
18568 63 68 61 72 20 7a 42 75 66 5b 32 30 30 5d 3b 0a  char zBuf[200];.
18569 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1856a 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28 70 2c  emPrettyPrint(p,
1856b 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 70 72 69   zBuf);.    fpri
1856c 6e 74 66 28 6f 75 74 2c 20 22 20 22 29 3b 0a 20  ntf(out, " ");. 
1856d 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
1856e 22 25 73 22 2c 20 7a 42 75 66 29 3b 0a 20 20 7d  "%s", zBuf);.  }
1856f 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  .}.static void r
18570 65 67 69 73 74 65 72 54 72 61 63 65 28 46 49 4c  egisterTrace(FIL
18571 45 20 2a 6f 75 74 2c 20 69 6e 74 20 69 52 65 67  E *out, int iReg
18572 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20 66 70 72  , Mem *p){.  fpr
18573 69 6e 74 66 28 6f 75 74 2c 20 22 52 45 47 5b 25  intf(out, "REG[%
18574 64 5d 20 3d 20 22 2c 20 69 52 65 67 29 3b 0a 20  d] = ", iReg);. 
18575 20 6d 65 6d 54 72 61 63 65 50 72 69 6e 74 28 6f   memTracePrint(o
18576 75 74 2c 20 70 29 3b 0a 20 20 66 70 72 69 6e 74  ut, p);.  fprint
18577 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a 7d 0a  f(out, "\n");.}.
18578 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
18579 51 4c 49 54 45 5f 44 45 42 55 47 0a 23 20 20 64  QLITE_DEBUG.#  d
1857a 65 66 69 6e 65 20 52 45 47 49 53 54 45 52 5f 54  efine REGISTER_T
1857b 52 41 43 45 28 52 2c 4d 29 20 69 66 28 70 2d 3e  RACE(R,M) if(p->
1857c 74 72 61 63 65 29 72 65 67 69 73 74 65 72 54 72  trace)registerTr
1857d 61 63 65 28 70 2d 3e 74 72 61 63 65 2c 52 2c 4d  ace(p->trace,R,M
1857e 29 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69 6e  ).#else.#  defin
1857f 65 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45  e REGISTER_TRACE
18580 28 52 2c 4d 29 0a 23 65 6e 64 69 66 0a 0a 0a 23  (R,M).#endif...#
18581 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49  ifdef VDBE_PROFI
18582 4c 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d  LE../* .** hwtim
18583 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c  e.h contains inl
18584 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f  ine assembler co
18585 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74  de for implement
18586 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72  ing .** high-per
18587 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20  formance timing 
18588 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a  routines..*/./**
18589 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
1858a 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e  lude hwtime.h in
1858b 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 76   the middle of v
1858c 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dbe.c **********
1858d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
1858e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
1858f 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68  in file hwtime.h
18590 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
18591 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18592 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
18593 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a  ** 2008 May 27.*
18594 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
18595 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
18596 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
18597 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
18598 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
18599 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
1859a 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
1859b 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
1859c 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
1859d 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
1859e 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
1859f 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
185a0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
185a1 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
185a2 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
185a3 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
185a4 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
185a5 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
185a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
185a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
185a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
185a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
185aa 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
185ab 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69  le contains inli
185ac 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20  ne asm code for 
185ad 72 65 74 72 69 65 76 69 6e 67 20 22 68 69 67 68  retrieving "high
185ae 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a  -performance".**
185af 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38   counters for x8
185b0 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2a  6 class CPUs..**
185b1 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 6d 65 2e  .** $Id: hwtime.
185b2 68 2c 76 20 31 2e 33 20 32 30 30 38 2f 30 38 2f  h,v 1.3 2008/08/
185b3 30 31 20 31 34 3a 33 33 3a 31 35 20 73 68 61 6e  01 14:33:15 shan
185b4 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64  e Exp $.*/.#ifnd
185b5 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64  ef _HWTIME_H_.#d
185b6 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f  efine _HWTIME_H_
185b7 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
185b8 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e  owing routine on
185b9 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74  ly works on pent
185ba 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65  ium-class (or ne
185bb 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72 73 2e  wer) processors.
185bc 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68 65 20  .** It uses the 
185bd 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74 6f 20  RDTSC opcode to 
185be 72 65 61 64 20 74 68 65 20 63 79 63 6c 65 20 63  read the cycle c
185bf 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 6f  ount value out o
185c0 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73  f the.** process
185c1 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74  or and returns t
185c2 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68 69 73  hat value.  This
185c3 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72   can be used for
185c4 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f   high-res.** pro
185c5 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28  filing..*/.#if (
185c6 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f  defined(__GNUC__
185c7 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53  ) || defined(_MS
185c8 43 5f 56 45 52 29 29 20 26 26 20 5c 0a 20 20 20  C_VER)) && \.   
185c9 20 20 20 28 64 65 66 69 6e 65 64 28 69 33 38 36     (defined(i386
185ca 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69  ) || defined(__i
185cb 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65  386__) || define
185cc 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23  d(_M_IX86))..  #
185cd 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55  if defined(__GNU
185ce 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65  C__)..  __inline
185cf 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  __ sqlite_uint64
185d0 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76   sqlite3Hwtime(v
185d1 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 67  oid){.     unsig
185d2 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a  ned int lo, hi;.
185d3 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76       __asm__ __v
185d4 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73  olatile__ ("rdts
185d5 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20  c" : "=a" (lo), 
185d6 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 20 20 20  "=d" (hi));.    
185d7 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f   return (sqlite_
185d8 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20  uint64)hi << 32 
185d9 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c  | lo;.  }..  #el
185da 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f  if defined(_MSC_
185db 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70  VER)..  __declsp
185dc 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69  ec(naked) __inli
185dd 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  ne sqlite_uint64
185de 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 33   __cdecl sqlite3
185df 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20  Hwtime(void){.  
185e0 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20     __asm {.     
185e1 20 20 20 72 64 74 73 63 0a 20 20 20 20 20 20 20     rdtsc.       
185e2 20 72 65 74 20 20 20 20 20 20 20 3b 20 72 65 74   ret       ; ret
185e3 75 72 6e 20 76 61 6c 75 65 20 61 74 20 45 44 58  urn value at EDX
185e4 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a  :EAX.     }.  }.
185e5 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66  .  #endif..#elif
185e6 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43   (defined(__GNUC
185e7 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f  __) && defined(_
185e8 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f  _x86_64__))..  _
185e9 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65  _inline__ sqlite
185ea 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48  _uint64 sqlite3H
185eb 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20  wtime(void){.   
185ec 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67     unsigned long
185ed 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73   val;.      __as
185ee 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f  m__ __volatile__
185ef 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 41 22   ("rdtsc" : "=A"
185f0 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 72   (val));.      r
185f1 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20  eturn val;.  }. 
185f2 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28  .#elif (defined(
185f3 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66  __GNUC__) && def
185f4 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a  ined(__ppc__))..
185f5 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c    __inline__ sql
185f6 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74  ite_uint64 sqlit
185f7 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a  e3Hwtime(void){.
185f8 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c        unsigned l
185f9 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b  ong long retval;
185fa 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  .      unsigned 
185fb 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20  long junk;.     
185fc 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74   __asm__ __volat
185fd 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20  ile__ ("\n\.    
185fe 20 20 20 20 20 20 31 3a 20 20 20 20 20 20 6d 66        1:      mf
185ff 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20  tbu   %1\n\.    
18600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66                mf
18601 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20  tb    %L0\n\.   
18602 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d                 m
18603 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20  ftbu   %0\n\.   
18604 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
18605 6d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a  mpw    %0,%1\n\.
18606 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18607 20 20 62 6e 65 20 20 20 20 20 31 62 22 0a 20 20    bne     1b".  
18608 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18609 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c 29 2c  : "=r" (retval),
1860a 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20   "=r" (junk));. 
1860b 20 20 20 20 20 72 65 74 75 72 6e 20 72 65 74 76       return retv
1860c 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a  al;.  }..#else..
1860d 20 20 23 65 72 72 6f 72 20 4e 65 65 64 20 69 6d    #error Need im
1860e 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
1860f 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20  sqlite3Hwtime() 
18610 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72  for your platfor
18611 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f  m...  /*.  ** To
18612 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74   compile without
18613 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71   implementing sq
18614 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f  lite3Hwtime() fo
18615 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c  r your platform,
18616 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65  .  ** you can re
18617 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 65 20 23  move the above #
18618 65 72 72 6f 72 20 61 6e 64 20 75 73 65 20 74 68  error and use th
18619 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a  e following.  **
1861a 20 73 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20   stub function. 
1861b 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74   You will lose t
1861c 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 20 66 6f  iming support fo
1861d 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74  r many.  ** of t
1861e 68 65 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64  he debugging and
1861f 20 74 65 73 74 69 6e 67 20 75 74 69 6c 69 74 69   testing utiliti
18620 65 73 2c 20 62 75 74 20 69 74 20 73 68 6f 75 6c  es, but it shoul
18621 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20  d at.  ** least 
18622 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e  compile and run.
18623 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  .  */.SQLITE_PRI
18624 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69  VATE   sqlite_ui
18625 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69  nt64 sqlite3Hwti
18626 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  me(void){ return
18627 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34   ((sqlite_uint64
18628 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a  )0); }..#endif..
18629 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
1862a 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a  ed(_HWTIME_H_) *
1862b 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
1862c 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69 6d 65  ** End of hwtime
1862d 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
1862e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1862f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18630 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
18631 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
18632 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
18633 69 6e 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a  in vdbe.c ******
18634 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18635 2a 2f 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  */..#endif../*.*
18636 2a 20 54 68 65 20 43 48 45 43 4b 5f 46 4f 52 5f  * The CHECK_FOR_
18637 49 4e 54 45 52 52 55 50 54 20 6d 61 63 72 6f 20  INTERRUPT macro 
18638 64 65 66 69 6e 65 64 20 68 65 72 65 20 6c 6f 6f  defined here loo
18639 6b 73 20 74 6f 20 73 65 65 20 69 66 20 74 68 65  ks to see if the
1863a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e 74 65  .** sqlite3_inte
1863b 72 72 75 70 74 28 29 20 72 6f 75 74 69 6e 65 20  rrupt() routine 
1863c 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2e  has been called.
1863d 20 20 49 66 20 69 74 20 68 61 73 20 62 65 65 6e    If it has been
1863e 2c 20 74 68 65 6e 0a 2a 2a 20 70 72 6f 63 65 73  , then.** proces
1863f 73 69 6e 67 20 6f 66 20 74 68 65 20 56 44 42 45  sing of the VDBE
18640 20 70 72 6f 67 72 61 6d 20 69 73 20 69 6e 74 65   program is inte
18641 72 72 75 70 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  rrupted..**.** T
18642 68 69 73 20 6d 61 63 72 6f 20 61 64 64 65 64 20  his macro added 
18643 74 6f 20 65 76 65 72 79 20 69 6e 73 74 72 75 63  to every instruc
18644 74 69 6f 6e 20 74 68 61 74 20 64 6f 65 73 20 61  tion that does a
18645 20 6a 75 6d 70 20 69 6e 20 6f 72 64 65 72 20 74   jump in order t
18646 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 61  o.** implement a
18647 20 6c 6f 6f 70 2e 20 20 54 68 69 73 20 74 65 73   loop.  This tes
18648 74 20 75 73 65 64 20 74 6f 20 62 65 20 6f 6e 20  t used to be on 
18649 65 76 65 72 79 20 73 69 6e 67 6c 65 20 69 6e 73  every single ins
1864a 74 72 75 63 74 69 6f 6e 2c 0a 2a 2a 20 62 75 74  truction,.** but
1864b 20 74 68 61 74 20 6d 65 61 6e 74 20 77 65 20 6d   that meant we m
1864c 6f 72 65 20 74 65 73 74 69 6e 67 20 74 68 61 74  ore testing that
1864d 20 77 65 20 6e 65 65 64 65 64 2e 20 20 42 79 20   we needed.  By 
1864e 6f 6e 6c 79 20 74 65 73 74 69 6e 67 20 74 68 65  only testing the
1864f 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 6a 75 6d 70  .** flag on jump
18650 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 2c 20 77   instructions, w
18651 65 20 67 65 74 20 61 20 28 73 6d 61 6c 6c 29 20  e get a (small) 
18652 73 70 65 65 64 20 69 6d 70 72 6f 76 65 6d 65 6e  speed improvemen
18653 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 48  t..*/.#define CH
18654 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50  ECK_FOR_INTERRUP
18655 54 20 5c 0a 20 20 20 69 66 28 20 64 62 2d 3e 75  T \.   if( db->u
18656 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20  1.isInterrupted 
18657 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
18658 5f 74 6f 5f 69 6e 74 65 72 72 75 70 74 3b 0a 0a  _to_interrupt;..
18659 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
1865a 42 55 47 0a 73 74 61 74 69 63 20 69 6e 74 20 66  BUG.static int f
1865b 69 6c 65 45 78 69 73 74 73 28 73 71 6c 69 74 65  ileExists(sqlite
1865c 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
1865d 72 20 2a 7a 46 69 6c 65 29 7b 0a 20 20 69 6e 74  r *zFile){.  int
1865e 20 72 65 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20   res = 0;.  int 
1865f 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
18660 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
18661 53 54 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72  ST.  /* If we ar
18662 65 20 63 75 72 72 65 6e 74 6c 79 20 74 65 73 74  e currently test
18663 69 6e 67 20 49 4f 20 65 72 72 6f 72 73 2c 20 74  ing IO errors, t
18664 68 65 6e 20 64 6f 20 6e 6f 74 20 63 61 6c 6c 20  hen do not call 
18665 4f 73 41 63 63 65 73 73 28 29 20 74 6f 0a 20 20  OsAccess() to.  
18666 2a 2a 20 74 65 73 74 20 66 6f 72 20 74 68 65 20  ** test for the 
18667 70 72 65 73 65 6e 63 65 20 6f 66 20 7a 46 69 6c  presence of zFil
18668 65 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75  e. This is becau
18669 73 65 20 61 6e 79 20 49 4f 20 65 72 72 6f 72 20  se any IO error 
1866a 74 68 61 74 0a 20 20 2a 2a 20 6f 63 63 75 72 73  that.  ** occurs
1866b 20 68 65 72 65 20 77 69 6c 6c 20 6e 6f 74 20 62   here will not b
1866c 65 20 72 65 70 6f 72 74 65 64 2c 20 63 61 75 73  e reported, caus
1866d 69 6e 67 20 74 68 65 20 74 65 73 74 20 74 6f 20  ing the test to 
1866e 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20 65 78 74  fail..  */.  ext
1866f 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  ern int sqlite3_
18670 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67  io_error_pending
18671 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  ;.  if( sqlite3_
18672 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67  io_error_pending
18673 3c 3d 30 20 29 0a 23 65 6e 64 69 66 0a 20 20 20  <=0 ).#endif.   
18674 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41   rc = sqlite3OsA
18675 63 63 65 73 73 28 64 62 2d 3e 70 56 66 73 2c 20  ccess(db->pVfs, 
18676 7a 46 69 6c 65 2c 20 53 51 4c 49 54 45 5f 41 43  zFile, SQLITE_AC
18677 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26 72 65  CESS_EXISTS, &re
18678 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 72 65  s);.  return (re
18679 73 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f  s && rc==SQLITE_
1867a 4f 4b 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  OK);.}.#endif../
1867b 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 61 73 20  *.** Execute as 
1867c 6d 75 63 68 20 6f 66 20 61 20 56 44 42 45 20 70  much of a VDBE p
1867d 72 6f 67 72 61 6d 20 61 73 20 77 65 20 63 61 6e  rogram as we can
1867e 20 74 68 65 6e 20 72 65 74 75 72 6e 2e 0a 2a 2a   then return..**
1867f 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 4d  .** sqlite3VdbeM
18680 61 6b 65 52 65 61 64 79 28 29 20 6d 75 73 74 20  akeReady() must 
18681 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65  be called before
18682 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6e   this routine in
18683 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 6c 6f   order to.** clo
18684 73 65 20 74 68 65 20 70 72 6f 67 72 61 6d 20 77  se the program w
18685 69 74 68 20 61 20 66 69 6e 61 6c 20 4f 50 5f 48  ith a final OP_H
18686 61 6c 74 20 61 6e 64 20 74 6f 20 73 65 74 20 75  alt and to set u
18687 70 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 73 0a  p the callbacks.
18688 2a 2a 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72  ** and the error
18689 20 6d 65 73 73 61 67 65 20 70 6f 69 6e 74 65 72   message pointer
1868a 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 65 76 65 72  ..**.** Whenever
1868b 20 61 20 72 6f 77 20 6f 72 20 72 65 73 75 6c 74   a row or result
1868c 20 64 61 74 61 20 69 73 20 61 76 61 69 6c 61 62   data is availab
1868d 6c 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  le, this routine
1868e 20 77 69 6c 6c 20 65 69 74 68 65 72 0a 2a 2a 20   will either.** 
1868f 69 6e 76 6f 6b 65 20 74 68 65 20 72 65 73 75 6c  invoke the resul
18690 74 20 63 61 6c 6c 62 61 63 6b 20 28 69 66 20 74  t callback (if t
18691 68 65 72 65 20 69 73 20 6f 6e 65 29 20 6f 72 20  here is one) or 
18692 72 65 74 75 72 6e 20 77 69 74 68 0a 2a 2a 20 53  return with.** S
18693 51 4c 49 54 45 5f 52 4f 57 2e 0a 2a 2a 0a 2a 2a  QLITE_ROW..**.**
18694 20 49 66 20 61 6e 20 61 74 74 65 6d 70 74 20 69   If an attempt i
18695 73 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e 20 61  s made to open a
18696 20 6c 6f 63 6b 65 64 20 64 61 74 61 62 61 73 65   locked database
18697 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74  , then this rout
18698 69 6e 65 0a 2a 2a 20 77 69 6c 6c 20 65 69 74 68  ine.** will eith
18699 65 72 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75  er invoke the bu
1869a 73 79 20 63 61 6c 6c 62 61 63 6b 20 28 69 66 20  sy callback (if 
1869b 74 68 65 72 65 20 69 73 20 6f 6e 65 29 20 6f 72  there is one) or
1869c 20 69 74 20 77 69 6c 6c 0a 2a 2a 20 72 65 74 75   it will.** retu
1869d 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a  rn SQLITE_BUSY..
1869e 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f  **.** If an erro
1869f 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 65 72 72  r occurs, an err
186a0 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 77 72  or message is wr
186a1 69 74 74 65 6e 20 74 6f 20 6d 65 6d 6f 72 79 20  itten to memory 
186a2 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d  obtained.** from
186a3 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
186a4 29 20 61 6e 64 20 70 2d 3e 7a 45 72 72 4d 73 67  ) and p->zErrMsg
186a5 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e   is made to poin
186a6 74 20 74 6f 20 74 68 61 74 20 6d 65 6d 6f 72 79  t to that memory
186a7 2e 0a 2a 2a 20 54 68 65 20 65 72 72 6f 72 20 63  ..** The error c
186a8 6f 64 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ode is stored in
186a9 20 70 2d 3e 72 63 20 61 6e 64 20 74 68 69 73 20   p->rc and this 
186aa 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
186ab 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a  SQLITE_ERROR..**
186ac 0a 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 62  .** If the callb
186ad 61 63 6b 20 65 76 65 72 20 72 65 74 75 72 6e 73  ack ever returns
186ae 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20   non-zero, then 
186af 74 68 65 20 70 72 6f 67 72 61 6d 20 65 78 69 74  the program exit
186b0 73 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79  s.** immediately
186b1 2e 20 20 54 68 65 72 65 20 77 69 6c 6c 20 62 65  .  There will be
186b2 20 6e 6f 20 65 72 72 6f 72 20 6d 65 73 73 61 67   no error messag
186b3 65 20 62 75 74 20 74 68 65 20 70 2d 3e 72 63 20  e but the p->rc 
186b4 66 69 65 6c 64 20 69 73 0a 2a 2a 20 73 65 74 20  field is.** set 
186b5 74 6f 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20  to SQLITE_ABORT 
186b6 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65  and this routine
186b7 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 51 4c   will return SQL
186b8 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a  ITE_ERROR..**.**
186b9 20 41 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   A memory alloca
186ba 74 69 6f 6e 20 65 72 72 6f 72 20 63 61 75 73 65  tion error cause
186bb 73 20 70 2d 3e 72 63 20 74 6f 20 62 65 20 73 65  s p->rc to be se
186bc 74 20 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  t to SQLITE_NOME
186bd 4d 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20 72 6f  M and this.** ro
186be 75 74 69 6e 65 20 74 6f 20 72 65 74 75 72 6e 20  utine to return 
186bf 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a  SQLITE_ERROR..**
186c0 0a 2a 2a 20 4f 74 68 65 72 20 66 61 74 61 6c 20  .** Other fatal 
186c1 65 72 72 6f 72 73 20 72 65 74 75 72 6e 20 53 51  errors return SQ
186c2 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a  LITE_ERROR..**.*
186c3 2a 20 41 66 74 65 72 20 74 68 69 73 20 72 6f 75  * After this rou
186c4 74 69 6e 65 20 68 61 73 20 66 69 6e 69 73 68 65  tine has finishe
186c5 64 2c 20 73 71 6c 69 74 65 33 56 64 62 65 46 69  d, sqlite3VdbeFi
186c6 6e 61 6c 69 7a 65 28 29 20 73 68 6f 75 6c 64 20  nalize() should 
186c7 62 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 63 6c  be.** used to cl
186c8 65 61 6e 20 75 70 20 74 68 65 20 6d 65 73 73 20  ean up the mess 
186c9 74 68 61 74 20 77 61 73 20 6c 65 66 74 20 62 65  that was left be
186ca 68 69 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  hind..*/.SQLITE_
186cb 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
186cc 74 65 33 56 64 62 65 45 78 65 63 28 0a 20 20 56  te3VdbeExec(.  V
186cd 64 62 65 20 2a 70 20 20 20 20 20 20 20 20 20 20  dbe *p          
186ce 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
186cf 20 56 44 42 45 20 2a 2f 0a 29 7b 0a 20 20 69 6e   VDBE */.){.  in
186d0 74 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 20  t pc;           
186d1 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
186d2 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20  program counter 
186d3 2a 2f 0a 20 20 4f 70 20 2a 70 4f 70 3b 20 20 20  */.  Op *pOp;   
186d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
186d5 2f 2a 20 43 75 72 72 65 6e 74 20 6f 70 65 72 61  /* Current opera
186d6 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63  tion */.  int rc
186d7 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
186d8 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f       /* Value to
186d9 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71 6c   return */.  sql
186da 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62  ite3 *db = p->db
186db 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64  ;       /* The d
186dc 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20  atabase */.  u8 
186dd 65 6e 63 6f 64 69 6e 67 20 3d 20 45 4e 43 28 64  encoding = ENC(d
186de 62 29 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 64  b);     /* The d
186df 61 74 61 62 61 73 65 20 65 6e 63 6f 64 69 6e 67  atabase encoding
186e0 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 31 2c   */.  Mem *pIn1,
186e1 20 2a 70 49 6e 32 2c 20 2a 70 49 6e 33 3b 20 20   *pIn2, *pIn3;  
186e2 20 2f 2a 20 49 6e 70 75 74 20 6f 70 65 72 61 6e   /* Input operan
186e3 64 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4f 75  ds */.  Mem *pOu
186e4 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
186e5 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 6f 70 65     /* Output ope
186e6 72 61 6e 64 20 2a 2f 0a 20 20 75 38 20 6f 70 50  rand */.  u8 opP
186e7 72 6f 70 65 72 74 79 3b 0a 20 20 69 6e 74 20 69  roperty;.  int i
186e8 43 6f 6d 70 61 72 65 20 3d 20 30 3b 20 20 20 20  Compare = 0;    
186e9 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20        /* Result 
186ea 6f 66 20 6c 61 73 74 20 4f 50 5f 43 6f 6d 70 61  of last OP_Compa
186eb 72 65 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a  re operation */.
186ec 20 20 69 6e 74 20 2a 61 50 65 72 6d 75 74 65 20    int *aPermute 
186ed 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  = 0;         /* 
186ee 50 65 72 6d 75 61 74 69 6f 6e 20 6f 66 20 63 6f  Permuation of co
186ef 6c 75 6d 6e 73 20 66 6f 72 20 4f 50 5f 43 6f 6d  lumns for OP_Com
186f0 70 61 72 65 20 2a 2f 0a 23 69 66 64 65 66 20 56  pare */.#ifdef V
186f1 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 75 36  DBE_PROFILE.  u6
186f2 34 20 73 74 61 72 74 3b 20 20 20 20 20 20 20 20  4 start;        
186f3 20 20 20 20 20 20 20 20 20 2f 2a 20 43 50 55 20           /* CPU 
186f4 63 6c 6f 63 6b 20 63 6f 75 6e 74 20 61 74 20 73  clock count at s
186f5 74 61 72 74 20 6f 66 20 6f 70 63 6f 64 65 20 2a  tart of opcode *
186f6 2f 0a 20 20 69 6e 74 20 6f 72 69 67 50 63 3b 20  /.  int origPc; 
186f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
186f8 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65  * Program counte
186f9 72 20 61 74 20 73 74 61 72 74 20 6f 66 20 6f 70  r at start of op
186fa 63 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 23  code */.#endif.#
186fb 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
186fc 49 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c  IT_PROGRESS_CALL
186fd 42 41 43 4b 0a 20 20 69 6e 74 20 6e 50 72 6f 67  BACK.  int nProg
186fe 72 65 73 73 4f 70 73 20 3d 20 30 3b 20 20 20 20  ressOps = 0;    
186ff 20 20 2f 2a 20 4f 70 63 6f 64 65 73 20 65 78 65    /* Opcodes exe
18700 63 75 74 65 64 20 73 69 6e 63 65 20 70 72 6f 67  cuted since prog
18701 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 2e 20 2a  ress callback. *
18702 2f 0a 23 65 6e 64 69 66 0a 20 20 55 6e 70 61 63  /.#endif.  Unpac
18703 6b 65 64 52 65 63 6f 72 64 20 61 54 65 6d 70 52  kedRecord aTempR
18704 65 63 5b 31 36 5d 3b 20 2f 2a 20 53 70 61 63 65  ec[16]; /* Space
18705 20 74 6f 20 68 6f 6c 64 20 61 20 74 72 61 6e 73   to hold a trans
18706 69 65 6e 74 20 55 6e 70 61 63 6b 65 64 52 65 63  ient UnpackedRec
18707 6f 72 64 20 2a 2f 0a 0a 0a 20 20 61 73 73 65 72  ord */...  asser
18708 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42  t( p->magic==VDB
18709 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 3b 20 20  E_MAGIC_RUN );  
1870a 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  /* sqlite3_step(
1870b 29 20 76 65 72 69 66 69 65 73 20 74 68 69 73 20  ) verifies this 
1870c 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d  */.  assert( db-
1870d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d  >magic==SQLITE_M
1870e 41 47 49 43 5f 42 55 53 59 20 29 3b 0a 20 20 73  AGIC_BUSY );.  s
1870f 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78  qlite3BtreeMutex
18710 41 72 72 61 79 45 6e 74 65 72 28 26 70 2d 3e 61  ArrayEnter(&p->a
18711 4d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 70 2d  Mutex);.  if( p-
18712 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45  >rc==SQLITE_NOME
18713 4d 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73  M ){.    /* This
18714 20 68 61 70 70 65 6e 73 20 69 66 20 61 20 6d 61   happens if a ma
18715 6c 6c 6f 63 28 29 20 69 6e 73 69 64 65 20 61 20  lloc() inside a 
18716 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
18717 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72  column_text() or
18718 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f  .    ** sqlite3_
18719 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20  column_text16() 
1871a 66 61 69 6c 65 64 2e 20 20 2a 2f 0a 20 20 20 20  failed.  */.    
1871b 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d  goto no_mem;.  }
1871c 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63  .  assert( p->rc
1871d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70  ==SQLITE_OK || p
1871e 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53  ->rc==SQLITE_BUS
1871f 59 20 29 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 53  Y );.  p->rc = S
18720 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65  QLITE_OK;.  asse
18721 72 74 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d  rt( p->explain==
18722 30 20 29 3b 0a 20 20 70 2d 3e 70 52 65 73 75 6c  0 );.  p->pResul
18723 74 53 65 74 20 3d 20 30 3b 0a 20 20 64 62 2d 3e  tSet = 0;.  db->
18724 62 75 73 79 48 61 6e 64 6c 65 72 2e 6e 42 75 73  busyHandler.nBus
18725 79 20 3d 20 30 3b 0a 20 20 43 48 45 43 4b 5f 46  y = 0;.  CHECK_F
18726 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20  OR_INTERRUPT;.  
18727 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61  sqlite3VdbeIOTra
18728 63 65 53 71 6c 28 70 29 3b 0a 23 69 66 64 65 66  ceSql(p);.#ifdef
18729 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
1872a 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69  sqlite3BeginBeni
1872b 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 69 66  gnMalloc();.  if
1872c 28 20 70 2d 3e 70 63 3d 3d 30 20 0a 20 20 20 26  ( p->pc==0 .   &
1872d 26 20 28 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73  & ((p->db->flags
1872e 20 26 20 53 51 4c 49 54 45 5f 56 64 62 65 4c 69   & SQLITE_VdbeLi
1872f 73 74 69 6e 67 29 20 7c 7c 20 66 69 6c 65 45 78  sting) || fileEx
18730 69 73 74 73 28 64 62 2c 20 22 76 64 62 65 5f 65  ists(db, "vdbe_e
18731 78 70 6c 61 69 6e 22 29 29 0a 20 20 29 7b 0a 20  xplain")).  ){. 
18732 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 70 72     int i;.    pr
18733 69 6e 74 66 28 22 56 44 42 45 20 50 72 6f 67 72  intf("VDBE Progr
18734 61 6d 20 4c 69 73 74 69 6e 67 3a 5c 6e 22 29 3b  am Listing:\n");
18735 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
18736 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20 20  PrintSql(p);.   
18737 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e   for(i=0; i<p->n
18738 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  Op; i++){.      
18739 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74  sqlite3VdbePrint
1873a 4f 70 28 73 74 64 6f 75 74 2c 20 69 2c 20 26 70  Op(stdout, i, &p
1873b 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 20 7d  ->aOp[i]);.    }
1873c 0a 20 20 7d 0a 20 20 69 66 28 20 66 69 6c 65 45  .  }.  if( fileE
1873d 78 69 73 74 73 28 64 62 2c 20 22 76 64 62 65 5f  xists(db, "vdbe_
1873e 74 72 61 63 65 22 29 20 29 7b 0a 20 20 20 20 70  trace") ){.    p
1873f 2d 3e 74 72 61 63 65 20 3d 20 73 74 64 6f 75 74  ->trace = stdout
18740 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45  ;.  }.  sqlite3E
18741 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  ndBenignMalloc()
18742 3b 0a 23 65 6e 64 69 66 0a 20 20 66 6f 72 28 70  ;.#endif.  for(p
18743 63 3d 70 2d 3e 70 63 3b 20 72 63 3d 3d 53 51 4c  c=p->pc; rc==SQL
18744 49 54 45 5f 4f 4b 3b 20 70 63 2b 2b 29 7b 0a 20  ITE_OK; pc++){. 
18745 20 20 20 61 73 73 65 72 74 28 20 70 63 3e 3d 30     assert( pc>=0
18746 20 26 26 20 70 63 3c 70 2d 3e 6e 4f 70 20 29 3b   && pc<p->nOp );
18747 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  .    if( db->mal
18748 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f  locFailed ) goto
18749 20 6e 6f 5f 6d 65 6d 3b 0a 23 69 66 64 65 66 20   no_mem;.#ifdef 
1874a 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 20  VDBE_PROFILE.   
1874b 20 6f 72 69 67 50 63 20 3d 20 70 63 3b 0a 20 20   origPc = pc;.  
1874c 20 20 73 74 61 72 74 20 3d 20 73 71 6c 69 74 65    start = sqlite
1874d 33 48 77 74 69 6d 65 28 29 3b 0a 23 65 6e 64 69  3Hwtime();.#endi
1874e 66 0a 20 20 20 20 70 4f 70 20 3d 20 26 70 2d 3e  f.    pOp = &p->
1874f 61 4f 70 5b 70 63 5d 3b 0a 0a 20 20 20 20 2f 2a  aOp[pc];..    /*
18750 20 4f 6e 6c 79 20 61 6c 6c 6f 77 20 74 72 61 63   Only allow trac
18751 69 6e 67 20 69 66 20 53 51 4c 49 54 45 5f 44 45  ing if SQLITE_DE
18752 42 55 47 20 69 73 20 64 65 66 69 6e 65 64 2e 0a  BUG is defined..
18753 20 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51      */.#ifdef SQ
18754 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 69  LITE_DEBUG.    i
18755 66 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20  f( p->trace ){. 
18756 20 20 20 20 20 69 66 28 20 70 63 3d 3d 30 20 29       if( pc==0 )
18757 7b 0a 20 20 20 20 20 20 20 20 70 72 69 6e 74 66  {.        printf
18758 28 22 56 44 42 45 20 45 78 65 63 75 74 69 6f 6e  ("VDBE Execution
18759 20 54 72 61 63 65 3a 5c 6e 22 29 3b 0a 20 20 20   Trace:\n");.   
1875a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1875b 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20 20  PrintSql(p);.   
1875c 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
1875d 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 70 2d  e3VdbePrintOp(p-
1875e 3e 74 72 61 63 65 2c 20 70 63 2c 20 70 4f 70 29  >trace, pc, pOp)
1875f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
18760 70 2d 3e 74 72 61 63 65 3d 3d 30 20 26 26 20 70  p->trace==0 && p
18761 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  c==0 ){.      sq
18762 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e  lite3BeginBenign
18763 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20  Malloc();.      
18764 69 66 28 20 66 69 6c 65 45 78 69 73 74 73 28 64  if( fileExists(d
18765 62 2c 20 22 76 64 62 65 5f 73 71 6c 74 72 61 63  b, "vdbe_sqltrac
18766 65 22 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73  e") ){.        s
18767 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 53  qlite3VdbePrintS
18768 71 6c 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ql(p);.      }. 
18769 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42       sqlite3EndB
1876a 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20  enignMalloc();. 
1876b 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
1876c 20 20 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b    ..    /* Check
1876d 20 74 6f 20 73 65 65 20 69 66 20 77 65 20 6e 65   to see if we ne
1876e 65 64 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61  ed to simulate a
1876f 6e 20 69 6e 74 65 72 72 75 70 74 2e 20 20 54 68  n interrupt.  Th
18770 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 0a  is only happens.
18771 20 20 20 20 2a 2a 20 69 66 20 77 65 20 68 61 76      ** if we hav
18772 65 20 61 20 73 70 65 63 69 61 6c 20 74 65 73 74  e a special test
18773 20 62 75 69 6c 64 2e 0a 20 20 20 20 2a 2f 0a 23   build..    */.#
18774 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
18775 54 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  T.    if( sqlite
18776 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e  3_interrupt_coun
18777 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  t>0 ){.      sql
18778 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63  ite3_interrupt_c
18779 6f 75 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 69 66  ount--;.      if
1877a 28 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72  ( sqlite3_interr
1877b 75 70 74 5f 63 6f 75 6e 74 3d 3d 30 20 29 7b 0a  upt_count==0 ){.
1877c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1877d 69 6e 74 65 72 72 75 70 74 28 64 62 29 3b 0a 20  interrupt(db);. 
1877e 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e       }.    }.#en
1877f 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
18780 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53  ITE_OMIT_PROGRES
18781 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 20 20 2f  S_CALLBACK.    /
18782 2a 20 43 61 6c 6c 20 74 68 65 20 70 72 6f 67 72  * Call the progr
18783 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 69 66 20  ess callback if 
18784 69 74 20 69 73 20 63 6f 6e 66 69 67 75 72 65 64  it is configured
18785 20 61 6e 64 20 74 68 65 20 72 65 71 75 69 72 65   and the require
18786 64 20 6e 75 6d 62 65 72 0a 20 20 20 20 2a 2a 20  d number.    ** 
18787 6f 66 20 56 44 42 45 20 6f 70 73 20 68 61 76 65  of VDBE ops have
18788 20 62 65 65 6e 20 65 78 65 63 75 74 65 64 20 28   been executed (
18789 65 69 74 68 65 72 20 73 69 6e 63 65 20 74 68 69  either since thi
1878a 73 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 0a  s invocation of.
1878b 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64      ** sqlite3Vd
1878c 62 65 45 78 65 63 28 29 20 6f 72 20 73 69 6e 63  beExec() or sinc
1878d 65 20 6c 61 73 74 20 74 69 6d 65 20 74 68 65 20  e last time the 
1878e 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63  progress callbac
1878f 6b 20 77 61 73 20 63 61 6c 6c 65 64 29 2e 0a 20  k was called).. 
18790 20 20 20 2a 2a 20 49 66 20 74 68 65 20 70 72 6f     ** If the pro
18791 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 72  gress callback r
18792 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  eturns non-zero,
18793 20 65 78 69 74 20 74 68 65 20 76 69 72 74 75 61   exit the virtua
18794 6c 20 6d 61 63 68 69 6e 65 20 77 69 74 68 0a 20  l machine with. 
18795 20 20 20 2a 2a 20 61 20 72 65 74 75 72 6e 20 63     ** a return c
18796 6f 64 65 20 53 51 4c 49 54 45 5f 41 42 4f 52 54  ode SQLITE_ABORT
18797 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
18798 20 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 20 29   db->xProgress )
18799 7b 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e  {.      if( db->
1879a 6e 50 72 6f 67 72 65 73 73 4f 70 73 3d 3d 6e 50  nProgressOps==nP
1879b 72 6f 67 72 65 73 73 4f 70 73 20 29 7b 0a 20 20  rogressOps ){.  
1879c 20 20 20 20 20 20 69 6e 74 20 70 72 63 3b 0a 20        int prc;. 
1879d 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
1879e 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20  e3SafetyOff(db) 
1879f 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
187a0 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20  _to_misuse;.    
187a1 20 20 20 20 70 72 63 20 3d 64 62 2d 3e 78 50 72      prc =db->xPr
187a2 6f 67 72 65 73 73 28 64 62 2d 3e 70 50 72 6f 67  ogress(db->pProg
187a3 72 65 73 73 41 72 67 29 3b 0a 20 20 20 20 20 20  ressArg);.      
187a4 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
187a5 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f  etyOn(db) ) goto
187a6 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69   abort_due_to_mi
187a7 73 75 73 65 3b 0a 20 20 20 20 20 20 20 20 69 66  suse;.        if
187a8 28 20 70 72 63 21 3d 30 20 29 7b 0a 20 20 20 20  ( prc!=0 ){.    
187a9 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
187aa 45 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 20  E_INTERRUPT;.   
187ab 20 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65         goto vdbe
187ac 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 20 20 20  _error_halt;.   
187ad 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e       }.        n
187ae 50 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b  ProgressOps = 0;
187af 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e  .      }.      n
187b0 50 72 6f 67 72 65 73 73 4f 70 73 2b 2b 3b 0a 20  ProgressOps++;. 
187b1 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
187b2 20 2f 2a 20 44 6f 20 63 6f 6d 6d 6f 6e 20 73 65   /* Do common se
187b3 74 75 70 20 70 72 6f 63 65 73 73 69 6e 67 20 66  tup processing f
187b4 6f 72 20 61 6e 79 20 6f 70 63 6f 64 65 20 74 68  or any opcode th
187b5 61 74 20 69 73 20 6d 61 72 6b 65 64 0a 20 20 20  at is marked.   
187b6 20 2a 2a 20 77 69 74 68 20 74 68 65 20 22 6f 75   ** with the "ou
187b7 74 32 2d 70 72 65 72 65 6c 65 61 73 65 22 20 74  t2-prerelease" t
187b8 61 67 2e 20 20 53 75 63 68 20 6f 70 63 6f 64 65  ag.  Such opcode
187b9 73 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 0a  s have a single.
187ba 20 20 20 20 2a 2a 20 6f 75 74 70 75 74 20 77 68      ** output wh
187bb 69 63 68 20 69 73 20 73 70 65 63 69 66 69 65 64  ich is specified
187bc 20 62 79 20 74 68 65 20 50 32 20 70 61 72 61 6d   by the P2 param
187bd 65 74 65 72 2e 20 20 54 68 65 20 50 32 20 72 65  eter.  The P2 re
187be 67 69 73 74 65 72 0a 20 20 20 20 2a 2a 20 69 73  gister.    ** is
187bf 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20   initialized to 
187c0 61 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20  a NULL..    */. 
187c1 20 20 20 6f 70 50 72 6f 70 65 72 74 79 20 3d 20     opProperty = 
187c2 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 70  opcodeProperty[p
187c3 4f 70 2d 3e 6f 70 63 6f 64 65 5d 3b 0a 20 20 20  Op->opcode];.   
187c4 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74 79   if( (opProperty
187c5 20 26 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50 52   & OPFLG_OUT2_PR
187c6 45 52 45 4c 45 41 53 45 29 21 3d 30 20 29 7b 0a  ERELEASE)!=0 ){.
187c7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
187c8 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 20  p->p2>0 );.     
187c9 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32   assert( pOp->p2
187ca 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20  <=p->nMem );.   
187cb 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d     pOut = &p->aM
187cc 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20  em[pOp->p2];.   
187cd 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
187ce 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c  mReleaseExternal
187cf 28 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 70 4f  (pOut);.      pO
187d0 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  ut->flags = MEM_
187d1 4e 75 6c 6c 3b 0a 20 20 20 20 7d 65 6c 73 65 0a  Null;.    }else.
187d2 20 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f 6d 6d   .    /* Do comm
187d3 6f 6e 20 73 65 74 75 70 20 66 6f 72 20 6f 70 63  on setup for opc
187d4 6f 64 65 73 20 6d 61 72 6b 65 64 20 77 69 74 68  odes marked with
187d5 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
187d6 6f 77 69 6e 67 0a 20 20 20 20 2a 2a 20 63 6f 6d  owing.    ** com
187d7 62 69 6e 61 74 69 6f 6e 73 20 6f 66 20 70 72 6f  binations of pro
187d8 70 65 72 74 69 65 73 2e 0a 20 20 20 20 2a 2a 0a  perties..    **.
187d9 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
187da 20 69 6e 31 0a 20 20 20 20 2a 2a 20 20 20 20 20   in1.    **     
187db 20 20 20 20 20 20 69 6e 31 20 69 6e 32 0a 20 20        in1 in2.  
187dc 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69    **           i
187dd 6e 31 20 69 6e 32 20 6f 75 74 33 0a 20 20 20 20  n1 in2 out3.    
187de 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69 6e 31  **           in1
187df 20 69 6e 33 0a 20 20 20 20 2a 2a 0a 20 20 20 20   in3.    **.    
187e0 2a 2a 20 56 61 72 69 61 62 6c 65 73 20 70 49 6e  ** Variables pIn
187e1 31 2c 20 70 49 6e 32 2c 20 61 6e 64 20 70 49 6e  1, pIn2, and pIn
187e2 33 20 61 72 65 20 6d 61 64 65 20 74 6f 20 70 6f  3 are made to po
187e3 69 6e 74 20 74 6f 20 61 70 70 72 6f 70 72 69 61  int to appropria
187e4 74 65 0a 20 20 20 20 2a 2a 20 72 65 67 69 73 74  te.    ** regist
187e5 65 72 73 20 66 6f 72 20 69 6e 70 75 74 73 2e 20  ers for inputs. 
187e6 20 56 61 72 69 61 62 6c 65 20 70 4f 75 74 20 70   Variable pOut p
187e7 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6f 75 74  oints to the out
187e8 70 75 74 20 72 65 67 69 73 74 65 72 2e 0a 20 20  put register..  
187e9 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 6f 70    */.    if( (op
187ea 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47  Property & OPFLG
187eb 5f 49 4e 31 29 21 3d 30 20 29 7b 0a 20 20 20 20  _IN1)!=0 ){.    
187ec 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
187ed 31 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  1>0 );.      ass
187ee 65 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d  ert( pOp->p1<=p-
187ef 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70  >nMem );.      p
187f0 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  In1 = &p->aMem[p
187f1 4f 70 2d 3e 70 31 5d 3b 0a 20 20 20 20 20 20 52  Op->p1];.      R
187f2 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
187f3 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20  p->p1, pIn1);.  
187f4 20 20 20 20 69 66 28 20 28 6f 70 50 72 6f 70 65      if( (opPrope
187f5 72 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 32 29  rty & OPFLG_IN2)
187f6 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61  !=0 ){.        a
187f7 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30  ssert( pOp->p2>0
187f8 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65   );.        asse
187f9 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e  rt( pOp->p2<=p->
187fa 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20  nMem );.        
187fb 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pIn2 = &p->aMem[
187fc 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20  pOp->p2];.      
187fd 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
187fe 28 70 4f 70 2d 3e 70 32 2c 20 70 49 6e 32 29 3b  (pOp->p2, pIn2);
187ff 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 6f 70  .        if( (op
18800 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47  Property & OPFLG
18801 5f 4f 55 54 33 29 21 3d 30 20 29 7b 0a 20 20 20  _OUT3)!=0 ){.   
18802 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
18803 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20  Op->p3>0 );.    
18804 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
18805 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p3<=p->nMem )
18806 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 75 74  ;.          pOut
18807 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
18808 3e 70 33 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a  >p3];.        }.
18809 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
1880a 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50  (opProperty & OP
1880b 46 4c 47 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20  FLG_IN3)!=0 ){. 
1880c 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1880d 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20  Op->p3>0 );.    
1880e 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
1880f 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p3<=p->nMem );.
18810 20 20 20 20 20 20 20 20 70 49 6e 33 20 3d 20 26          pIn3 = &
18811 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  p->aMem[pOp->p3]
18812 3b 0a 20 20 20 20 20 20 20 20 52 45 47 49 53 54  ;.        REGIST
18813 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33  ER_TRACE(pOp->p3
18814 2c 20 70 49 6e 33 29 3b 0a 20 20 20 20 20 20 7d  , pIn3);.      }
18815 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28  .    }else if( (
18816 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46  opProperty & OPF
18817 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b 0a 20 20  LG_IN2)!=0 ){.  
18818 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
18819 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 20 20 61  >p2>0 );.      a
1881a 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d  ssert( pOp->p2<=
1881b 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20  p->nMem );.     
1881c 20 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d   pIn2 = &p->aMem
1881d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20  [pOp->p2];.     
1881e 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28   REGISTER_TRACE(
1881f 70 4f 70 2d 3e 70 32 2c 20 70 49 6e 32 29 3b 0a  pOp->p2, pIn2);.
18820 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6f      }else if( (o
18821 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c  pProperty & OPFL
18822 47 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20 20 20  G_IN3)!=0 ){.   
18823 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
18824 70 33 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73  p3>0 );.      as
18825 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70  sert( pOp->p3<=p
18826 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20  ->nMem );.      
18827 70 49 6e 33 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pIn3 = &p->aMem[
18828 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20  pOp->p3];.      
18829 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
1882a 4f 70 2d 3e 70 33 2c 20 70 49 6e 33 29 3b 0a 20  Op->p3, pIn3);. 
1882b 20 20 20 7d 0a 0a 20 20 20 20 73 77 69 74 63 68     }..    switch
1882c 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b  ( pOp->opcode ){
1882d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
1882e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1882f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18831 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18832 0a 2a 2a 20 57 68 61 74 20 66 6f 6c 6c 6f 77 73  .** What follows
18833 20 69 73 20 61 20 6d 61 73 73 69 76 65 20 73 77   is a massive sw
18834 69 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20 77  itch statement w
18835 68 65 72 65 20 65 61 63 68 20 63 61 73 65 20 69  here each case i
18836 6d 70 6c 65 6d 65 6e 74 73 20 61 0a 2a 2a 20 73  mplements a.** s
18837 65 70 61 72 61 74 65 20 69 6e 73 74 72 75 63 74  eparate instruct
18838 69 6f 6e 20 69 6e 20 74 68 65 20 76 69 72 74 75  ion in the virtu
18839 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 49 66 20  al machine.  If 
1883a 77 65 20 66 6f 6c 6c 6f 77 20 74 68 65 20 75 73  we follow the us
1883b 75 61 6c 0a 2a 2a 20 69 6e 64 65 6e 74 61 74 69  ual.** indentati
1883c 6f 6e 20 63 6f 6e 76 65 6e 74 69 6f 6e 73 2c 20  on conventions, 
1883d 65 61 63 68 20 63 61 73 65 20 73 68 6f 75 6c 64  each case should
1883e 20 62 65 20 69 6e 64 65 6e 74 65 64 20 62 79 20   be indented by 
1883f 36 20 73 70 61 63 65 73 2e 20 20 42 75 74 0a 2a  6 spaces.  But.*
18840 2a 20 74 68 61 74 20 69 73 20 61 20 6c 6f 74 20  * that is a lot 
18841 6f 66 20 77 61 73 74 65 64 20 73 70 61 63 65 20  of wasted space 
18842 6f 6e 20 74 68 65 20 6c 65 66 74 20 6d 61 72 67  on the left marg
18843 69 6e 2e 20 20 53 6f 20 74 68 65 20 63 6f 64 65  in.  So the code
18844 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 73   within.** the s
18845 77 69 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20  witch statement 
18846 77 69 6c 6c 20 62 72 65 61 6b 20 77 69 74 68 20  will break with 
18847 63 6f 6e 76 65 6e 74 69 6f 6e 20 61 6e 64 20 62  convention and b
18848 65 20 66 6c 75 73 68 2d 6c 65 66 74 2e 20 41 6e  e flush-left. An
18849 6f 74 68 65 72 0a 2a 2a 20 62 69 67 20 63 6f 6d  other.** big com
1884a 6d 65 6e 74 20 28 73 69 6d 69 6c 61 72 20 74 6f  ment (similar to
1884b 20 74 68 69 73 20 6f 6e 65 29 20 77 69 6c 6c 20   this one) will 
1884c 6d 61 72 6b 20 74 68 65 20 70 6f 69 6e 74 20 69  mark the point i
1884d 6e 20 74 68 65 20 63 6f 64 65 20 77 68 65 72 65  n the code where
1884e 0a 2a 2a 20 77 65 20 74 72 61 6e 73 69 74 69 6f  .** we transitio
1884f 6e 20 62 61 63 6b 20 74 6f 20 6e 6f 72 6d 61 6c  n back to normal
18850 20 69 6e 64 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a   indentation..**
18851 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 74 69  .** The formatti
18852 6e 67 20 6f 66 20 65 61 63 68 20 63 61 73 65 20  ng of each case 
18853 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20 54  is important.  T
18854 68 65 20 6d 61 6b 65 66 69 6c 65 20 66 6f 72 20  he makefile for 
18855 53 51 4c 69 74 65 0a 2a 2a 20 67 65 6e 65 72 61  SQLite.** genera
18856 74 65 73 20 74 77 6f 20 43 20 66 69 6c 65 73 20  tes two C files 
18857 22 6f 70 63 6f 64 65 73 2e 68 22 20 61 6e 64 20  "opcodes.h" and 
18858 22 6f 70 63 6f 64 65 73 2e 63 22 20 62 79 20 73  "opcodes.c" by s
18859 63 61 6e 6e 69 6e 67 20 74 68 69 73 0a 2a 2a 20  canning this.** 
1885a 66 69 6c 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72  file looking for
1885b 20 6c 69 6e 65 73 20 74 68 61 74 20 62 65 67 69   lines that begi
1885c 6e 20 77 69 74 68 20 22 63 61 73 65 20 4f 50 5f  n with "case OP_
1885d 22 2e 20 20 54 68 65 20 6f 70 63 6f 64 65 73 2e  ".  The opcodes.
1885e 68 20 66 69 6c 65 73 0a 2a 2a 20 77 69 6c 6c 20  h files.** will 
1885f 62 65 20 66 69 6c 6c 65 64 20 77 69 74 68 20 23  be filled with #
18860 64 65 66 69 6e 65 73 20 74 68 61 74 20 67 69 76  defines that giv
18861 65 20 75 6e 69 71 75 65 20 69 6e 74 65 67 65 72  e unique integer
18862 20 76 61 6c 75 65 73 20 74 6f 20 65 61 63 68 0a   values to each.
18863 2a 2a 20 6f 70 63 6f 64 65 20 61 6e 64 20 74 68  ** opcode and th
18864 65 20 6f 70 63 6f 64 65 73 2e 63 20 66 69 6c 65  e opcodes.c file
18865 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20   is filled with 
18866 61 6e 20 61 72 72 61 79 20 6f 66 20 73 74 72 69  an array of stri
18867 6e 67 73 20 77 68 65 72 65 0a 2a 2a 20 65 61 63  ngs where.** eac
18868 68 20 73 74 72 69 6e 67 20 69 73 20 74 68 65 20  h string is the 
18869 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 66 6f  symbolic name fo
1886a 72 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  r the correspond
1886b 69 6e 67 20 6f 70 63 6f 64 65 2e 20 20 49 66 20  ing opcode.  If 
1886c 74 68 65 0a 2a 2a 20 63 61 73 65 20 73 74 61 74  the.** case stat
1886d 65 6d 65 6e 74 20 69 73 20 66 6f 6c 6c 6f 77 65  ement is followe
1886e 64 20 62 79 20 61 20 63 6f 6d 6d 65 6e 74 20 6f  d by a comment o
1886f 66 20 74 68 65 20 66 6f 72 6d 20 22 2f 23 20 73  f the form "/# s
18870 61 6d 65 20 61 73 20 2e 2e 2e 20 23 2f 22 0a 2a  ame as ... #/".*
18871 2a 20 74 68 61 74 20 63 6f 6d 6d 65 6e 74 20 69  * that comment i
18872 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  s used to determ
18873 69 6e 65 20 74 68 65 20 70 61 72 74 69 63 75 6c  ine the particul
18874 61 72 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  ar value of the 
18875 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74  opcode..**.** Ot
18876 68 65 72 20 6b 65 79 77 6f 72 64 73 20 69 6e 20  her keywords in 
18877 74 68 65 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74  the comment that
18878 20 66 6f 6c 6c 6f 77 73 20 65 61 63 68 20 63 61   follows each ca
18879 73 65 20 61 72 65 20 75 73 65 64 20 74 6f 0a 2a  se are used to.*
1887a 2a 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20  * construct the 
1887b 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45  OPFLG_INITIALIZE
1887c 52 20 76 61 6c 75 65 20 74 68 61 74 20 69 6e 69  R value that ini
1887d 74 69 61 6c 69 7a 65 73 20 6f 70 63 6f 64 65 50  tializes opcodeP
1887e 72 6f 70 65 72 74 79 5b 5d 2e 0a 2a 2a 20 4b 65  roperty[]..** Ke
1887f 79 77 6f 72 64 73 20 69 6e 63 6c 75 64 65 3a 20  ywords include: 
18880 69 6e 31 2c 20 69 6e 32 2c 20 69 6e 33 2c 20 6f  in1, in2, in3, o
18881 75 74 32 5f 70 72 65 72 65 6c 65 61 73 65 2c 20  ut2_prerelease, 
18882 6f 75 74 32 2c 20 6f 75 74 33 2e 20 20 53 65 65  out2, out3.  See
18883 0a 2a 2a 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65  .** the mkopcode
18884 68 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72  h.awk script for
18885 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
18886 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44  rmation..**.** D
18887 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 61 62 6f  ocumentation abo
18888 75 74 20 56 44 42 45 20 6f 70 63 6f 64 65 73 20  ut VDBE opcodes 
18889 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20  is generated by 
1888a 73 63 61 6e 6e 69 6e 67 20 74 68 69 73 20 66 69  scanning this fi
1888b 6c 65 0a 2a 2a 20 66 6f 72 20 6c 69 6e 65 73 20  le.** for lines 
1888c 6f 66 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 20  of that contain 
1888d 22 4f 70 63 6f 64 65 3a 22 2e 20 20 54 68 61 74  "Opcode:".  That
1888e 20 6c 69 6e 65 20 61 6e 64 20 61 6c 6c 20 73 75   line and all su
1888f 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 6f 6d 6d  bsequent.** comm
18890 65 6e 74 20 6c 69 6e 65 73 20 61 72 65 20 75 73  ent lines are us
18891 65 64 20 69 6e 20 74 68 65 20 67 65 6e 65 72 61  ed in the genera
18892 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f 70 63 6f  tion of the opco
18893 64 65 2e 68 74 6d 6c 20 64 6f 63 75 6d 65 6e 74  de.html document
18894 61 74 69 6f 6e 0a 2a 2a 20 66 69 6c 65 2e 0a 2a  ation.** file..*
18895 2a 0a 2a 2a 20 53 55 4d 4d 41 52 59 3a 0a 2a 2a  *.** SUMMARY:.**
18896 0a 2a 2a 20 20 20 20 20 46 6f 72 6d 61 74 74 69  .**     Formatti
18897 6e 67 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20  ng is important 
18898 74 6f 20 73 63 72 69 70 74 73 20 74 68 61 74 20  to scripts that 
18899 73 63 61 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a  scan this file..
1889a 2a 2a 20 20 20 20 20 44 6f 20 6e 6f 74 20 64 65  **     Do not de
1889b 76 69 61 74 65 20 66 72 6f 6d 20 74 68 65 20 66  viate from the f
1889c 6f 72 6d 61 74 74 69 6e 67 20 73 74 79 6c 65 20  ormatting style 
1889d 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65  currently in use
1889e 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
1889f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
188a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
188a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
188a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
188a3 2a 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  **/../* Opcode: 
188a4 20 47 6f 74 6f 20 2a 20 50 32 20 2a 20 2a 20 2a   Goto * P2 * * *
188a5 0a 2a 2a 0a 2a 2a 20 41 6e 20 75 6e 63 6f 6e 64  .**.** An uncond
188a6 69 74 69 6f 6e 61 6c 20 6a 75 6d 70 20 74 6f 20  itional jump to 
188a7 61 64 64 72 65 73 73 20 50 32 2e 0a 2a 2a 20 54  address P2..** T
188a8 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74  he next instruct
188a9 69 6f 6e 20 65 78 65 63 75 74 65 64 20 77 69 6c  ion executed wil
188aa 6c 20 62 65 20 0a 2a 2a 20 74 68 65 20 6f 6e 65  l be .** the one
188ab 20 61 74 20 69 6e 64 65 78 20 50 32 20 66 72 6f   at index P2 fro
188ac 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  m the beginning 
188ad 6f 66 0a 2a 2a 20 74 68 65 20 70 72 6f 67 72 61  of.** the progra
188ae 6d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f  m..*/.case OP_Go
188af 74 6f 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  to: {           
188b0 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 43    /* jump */.  C
188b1 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55  HECK_FOR_INTERRU
188b2 50 54 3b 0a 20 20 70 63 20 3d 20 70 4f 70 2d 3e  PT;.  pc = pOp->
188b3 70 32 20 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b  p2 - 1;.  break;
188b4 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20  .}../* Opcode:  
188b5 47 6f 73 75 62 20 50 31 20 50 32 20 2a 20 2a 20  Gosub P1 P2 * * 
188b6 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68  *.**.** Write th
188b7 65 20 63 75 72 72 65 6e 74 20 61 64 64 72 65 73  e current addres
188b8 73 20 6f 6e 74 6f 20 72 65 67 69 73 74 65 72 20  s onto register 
188b9 50 31 0a 2a 2a 20 61 6e 64 20 74 68 65 6e 20 6a  P1.** and then j
188ba 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73 20 50  ump to address P
188bb 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f  2..*/.case OP_Go
188bc 73 75 62 3a 20 7b 20 20 20 20 20 20 20 20 20 20  sub: {          
188bd 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 61    /* jump */.  a
188be 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30  ssert( pOp->p1>0
188bf 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
188c0 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p1<=p->nMem )
188c1 3b 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61  ;.  pIn1 = &p->a
188c2 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Mem[pOp->p1];.  
188c3 61 73 73 65 72 74 28 20 28 70 49 6e 31 2d 3e 66  assert( (pIn1->f
188c4 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 3d  lags & MEM_Dyn)=
188c5 3d 30 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c  =0 );.  pIn1->fl
188c6 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20  ags = MEM_Int;. 
188c7 20 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 63 3b   pIn1->u.i = pc;
188c8 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43  .  REGISTER_TRAC
188c9 45 28 70 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29  E(pOp->p1, pIn1)
188ca 3b 0a 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32  ;.  pc = pOp->p2
188cb 20 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d   - 1;.  break;.}
188cc 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 52 65  ../* Opcode:  Re
188cd 74 75 72 6e 20 50 31 20 2a 20 2a 20 2a 20 2a 0a  turn P1 * * * *.
188ce 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 74 68  **.** Jump to th
188cf 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69  e next instructi
188d0 6f 6e 20 61 66 74 65 72 20 74 68 65 20 61 64 64  on after the add
188d1 72 65 73 73 20 69 6e 20 72 65 67 69 73 74 65 72  ress in register
188d2 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f   P1..*/.case OP_
188d3 52 65 74 75 72 6e 3a 20 7b 20 20 20 20 20 20 20  Return: {       
188d4 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20      /* in1 */.  
188d5 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c  assert( pIn1->fl
188d6 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b  ags & MEM_Int );
188d7 0a 20 20 70 63 20 3d 20 70 49 6e 31 2d 3e 75 2e  .  pc = pIn1->u.
188d8 69 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  i;.  break;.}../
188d9 2a 20 4f 70 63 6f 64 65 3a 20 20 59 69 65 6c 64  * Opcode:  Yield
188da 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a   P1 * * * *.**.*
188db 2a 20 53 77 61 70 20 74 68 65 20 70 72 6f 67 72  * Swap the progr
188dc 61 6d 20 63 6f 75 6e 74 65 72 20 77 69 74 68 20  am counter with 
188dd 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
188de 69 73 74 65 72 20 50 31 2e 0a 2a 2f 0a 63 61 73  ister P1..*/.cas
188df 65 20 4f 50 5f 59 69 65 6c 64 3a 20 7b 0a 20 20  e OP_Yield: {.  
188e0 69 6e 74 20 70 63 44 65 73 74 3b 0a 20 20 61 73  int pcDest;.  as
188e1 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20  sert( pOp->p1>0 
188e2 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
188e3 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b  ->p1<=p->nMem );
188e4 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d  .  pIn1 = &p->aM
188e5 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61  em[pOp->p1];.  a
188e6 73 73 65 72 74 28 20 28 70 49 6e 31 2d 3e 66 6c  ssert( (pIn1->fl
188e7 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 3d 3d  ags & MEM_Dyn)==
188e8 30 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61  0 );.  pIn1->fla
188e9 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20  gs = MEM_Int;.  
188ea 70 63 44 65 73 74 20 3d 20 70 49 6e 31 2d 3e 75  pcDest = pIn1->u
188eb 2e 69 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20  .i;.  pIn1->u.i 
188ec 3d 20 70 63 3b 0a 20 20 52 45 47 49 53 54 45 52  = pc;.  REGISTER
188ed 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20  _TRACE(pOp->p1, 
188ee 70 49 6e 31 29 3b 0a 20 20 70 63 20 3d 20 70 63  pIn1);.  pc = pc
188ef 44 65 73 74 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Dest;.  break;.}
188f0 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 48  .../* Opcode:  H
188f1 61 6c 74 20 50 31 20 50 32 20 2a 20 50 34 20 2a  alt P1 P2 * P4 *
188f2 0a 2a 2a 0a 2a 2a 20 45 78 69 74 20 69 6d 6d 65  .**.** Exit imme
188f3 64 69 61 74 65 6c 79 2e 20 20 41 6c 6c 20 6f 70  diately.  All op
188f4 65 6e 20 63 75 72 73 6f 72 73 2c 20 46 69 66 6f  en cursors, Fifo
188f5 73 2c 20 65 74 63 20 61 72 65 20 63 6c 6f 73 65  s, etc are close
188f6 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c  d.** automatical
188f7 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20  ly..**.** P1 is 
188f8 74 68 65 20 72 65 73 75 6c 74 20 63 6f 64 65 20  the result code 
188f9 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69  returned by sqli
188fa 74 65 33 5f 65 78 65 63 28 29 2c 20 73 71 6c 69  te3_exec(), sqli
188fb 74 65 33 5f 72 65 73 65 74 28 29 2c 0a 2a 2a 20  te3_reset(),.** 
188fc 6f 72 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  or sqlite3_final
188fd 69 7a 65 28 29 2e 20 20 46 6f 72 20 61 20 6e 6f  ize().  For a no
188fe 72 6d 61 6c 20 68 61 6c 74 2c 20 74 68 69 73 20  rmal halt, this 
188ff 73 68 6f 75 6c 64 20 62 65 20 53 51 4c 49 54 45  should be SQLITE
18900 5f 4f 4b 20 28 30 29 2e 0a 2a 2a 20 46 6f 72 20  _OK (0)..** For 
18901 65 72 72 6f 72 73 2c 20 69 74 20 63 61 6e 20 62  errors, it can b
18902 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 76 61 6c  e some other val
18903 75 65 2e 20 20 49 66 20 50 31 21 3d 30 20 74 68  ue.  If P1!=0 th
18904 65 6e 20 50 32 20 77 69 6c 6c 20 64 65 74 65 72  en P2 will deter
18905 6d 69 6e 65 0a 2a 2a 20 77 68 65 74 68 65 72 20  mine.** whether 
18906 6f 72 20 6e 6f 74 20 74 6f 20 72 6f 6c 6c 62 61  or not to rollba
18907 63 6b 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  ck the current t
18908 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 44 6f 20  ransaction.  Do 
18909 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20  not rollback.** 
1890a 69 66 20 50 32 3d 3d 4f 45 5f 46 61 69 6c 2e 20  if P2==OE_Fail. 
1890b 44 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  Do the rollback 
1890c 69 66 20 50 32 3d 3d 4f 45 5f 52 6f 6c 6c 62 61  if P2==OE_Rollba
1890d 63 6b 2e 20 20 49 66 20 50 32 3d 3d 4f 45 5f 41  ck.  If P2==OE_A
1890e 62 6f 72 74 2c 0a 2a 2a 20 74 68 65 6e 20 62 61  bort,.** then ba
1890f 63 6b 20 6f 75 74 20 61 6c 6c 20 63 68 61 6e 67  ck out all chang
18910 65 73 20 74 68 61 74 20 68 61 76 65 20 6f 63 63  es that have occ
18911 75 72 72 65 64 20 64 75 72 69 6e 67 20 74 68 69  urred during thi
18912 73 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74  s execution of t
18913 68 65 0a 2a 2a 20 56 44 42 45 2c 20 62 75 74 20  he.** VDBE, but 
18914 64 6f 20 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20  do not rollback 
18915 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  the transaction.
18916 20 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73   .**.** If P4 is
18917 20 6e 6f 74 20 6e 75 6c 6c 20 74 68 65 6e 20 69   not null then i
18918 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 6d 65  t is an error me
18919 73 73 61 67 65 20 73 74 72 69 6e 67 2e 0a 2a 2a  ssage string..**
1891a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 6e 20  .** There is an 
1891b 69 6d 70 6c 69 65 64 20 22 48 61 6c 74 20 30 20  implied "Halt 0 
1891c 30 20 30 22 20 69 6e 73 74 72 75 63 74 69 6f 6e  0 0" instruction
1891d 20 69 6e 73 65 72 74 65 64 20 61 74 20 74 68 65   inserted at the
1891e 20 76 65 72 79 20 65 6e 64 20 6f 66 0a 2a 2a 20   very end of.** 
1891f 65 76 65 72 79 20 70 72 6f 67 72 61 6d 2e 20 20  every program.  
18920 53 6f 20 61 20 6a 75 6d 70 20 70 61 73 74 20 74  So a jump past t
18921 68 65 20 6c 61 73 74 20 69 6e 73 74 72 75 63 74  he last instruct
18922 69 6f 6e 20 6f 66 20 74 68 65 20 70 72 6f 67 72  ion of the progr
18923 61 6d 0a 2a 2a 20 69 73 20 74 68 65 20 73 61 6d  am.** is the sam
18924 65 20 61 73 20 65 78 65 63 75 74 69 6e 67 20 48  e as executing H
18925 61 6c 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  alt..*/.case OP_
18926 48 61 6c 74 3a 20 7b 0a 20 20 70 2d 3e 72 63 20  Halt: {.  p->rc 
18927 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 70 2d 3e  = pOp->p1;.  p->
18928 70 63 20 3d 20 70 63 3b 0a 20 20 70 2d 3e 65 72  pc = pc;.  p->er
18929 72 6f 72 41 63 74 69 6f 6e 20 3d 20 70 4f 70 2d  rorAction = pOp-
1892a 3e 70 32 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e  >p2;.  if( pOp->
1892b 70 34 2e 7a 20 29 7b 0a 20 20 20 20 73 71 6c 69  p4.z ){.    sqli
1892c 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
1892d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25  >zErrMsg, db, "%
1892e 73 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a  s", pOp->p4.z);.
1892f 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74    }.  rc = sqlit
18930 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20  e3VdbeHalt(p);. 
18931 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
18932 49 54 45 5f 42 55 53 59 20 7c 7c 20 72 63 3d 3d  ITE_BUSY || rc==
18933 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69  SQLITE_OK );.  i
18934 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55  f( rc==SQLITE_BU
18935 53 59 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20  SY ){.    p->rc 
18936 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55  = rc = SQLITE_BU
18937 53 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  SY;.  }else{.   
18938 20 72 63 20 3d 20 70 2d 3e 72 63 20 3f 20 53 51   rc = p->rc ? SQ
18939 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20 53 51 4c  LITE_ERROR : SQL
1893a 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20  ITE_DONE;.  }.  
1893b 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e  goto vdbe_return
1893c 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1893d 49 6e 74 65 67 65 72 20 50 31 20 50 32 20 2a 20  Integer P1 P2 * 
1893e 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 33 32  * *.**.** The 32
1893f 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76 61 6c  -bit integer val
18940 75 65 20 50 31 20 69 73 20 77 72 69 74 74 65 6e  ue P1 is written
18941 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50   into register P
18942 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e  2..*/.case OP_In
18943 74 65 67 65 72 3a 20 7b 20 20 20 20 20 20 20 20  teger: {        
18944 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65   /* out2-prerele
18945 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 2d 3e 66  ase */.  pOut->f
18946 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
18947 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 70 4f    pOut->u.i = pO
18948 70 2d 3e 70 31 3b 0a 20 20 62 72 65 61 6b 3b 0a  p->p1;.  break;.
18949 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e  }../* Opcode: In
1894a 74 36 34 20 2a 20 50 32 20 2a 20 50 34 20 2a 0a  t64 * P2 * P4 *.
1894b 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f  **.** P4 is a po
1894c 69 6e 74 65 72 20 74 6f 20 61 20 36 34 2d 62 69  inter to a 64-bi
1894d 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 2e  t integer value.
1894e 0a 2a 2a 20 57 72 69 74 65 20 74 68 61 74 20 76  .** Write that v
1894f 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74  alue into regist
18950 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f  er P2..*/.case O
18951 50 5f 49 6e 74 36 34 3a 20 7b 20 20 20 20 20 20  P_Int64: {      
18952 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65       /* out2-pre
18953 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73  release */.  ass
18954 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 70 49 36  ert( pOp->p4.pI6
18955 34 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d 3e  4!=0 );.  pOut->
18956 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
18957 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 2a  .  pOut->u.i = *
18958 70 4f 70 2d 3e 70 34 2e 70 49 36 34 3b 0a 20 20  pOp->p4.pI64;.  
18959 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1895a 6f 64 65 3a 20 52 65 61 6c 20 2a 20 50 32 20 2a  ode: Real * P2 *
1895b 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69   P4 *.**.** P4 i
1895c 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
1895d 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67   64-bit floating
1895e 20 70 6f 69 6e 74 20 76 61 6c 75 65 2e 0a 2a 2a   point value..**
1895f 20 57 72 69 74 65 20 74 68 61 74 20 76 61 6c 75   Write that valu
18960 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20  e into register 
18961 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52  P2..*/.case OP_R
18962 65 61 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20  eal: {          
18963 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
18964 46 4c 4f 41 54 2c 20 6f 75 74 32 2d 70 72 65 72  FLOAT, out2-prer
18965 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74  elease */.  pOut
18966 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52 65  ->flags = MEM_Re
18967 61 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 21 73  al;.  assert( !s
18968 71 6c 69 74 65 33 49 73 4e 61 4e 28 2a 70 4f 70  qlite3IsNaN(*pOp
18969 2d 3e 70 34 2e 70 52 65 61 6c 29 20 29 3b 0a 20  ->p4.pReal) );. 
1896a 20 70 4f 75 74 2d 3e 72 20 3d 20 2a 70 4f 70 2d   pOut->r = *pOp-
1896b 3e 70 34 2e 70 52 65 61 6c 3b 0a 20 20 62 72 65  >p4.pReal;.  bre
1896c 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1896d 3a 20 53 74 72 69 6e 67 38 20 2a 20 50 32 20 2a  : String8 * P2 *
1896e 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 70   P4 *.**.** P4 p
1896f 6f 69 6e 74 73 20 74 6f 20 61 20 6e 75 6c 20 74  oints to a nul t
18970 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20  erminated UTF-8 
18971 73 74 72 69 6e 67 2e 20 54 68 69 73 20 6f 70 63  string. This opc
18972 6f 64 65 20 69 73 20 74 72 61 6e 73 66 6f 72 6d  ode is transform
18973 65 64 20 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20 4f  ed .** into an O
18974 50 5f 53 74 72 69 6e 67 20 62 65 66 6f 72 65 20  P_String before 
18975 69 74 20 69 73 20 65 78 65 63 75 74 65 64 20 66  it is executed f
18976 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 69 6d  or the first tim
18977 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 74  e..*/.case OP_St
18978 72 69 6e 67 38 3a 20 7b 20 20 20 20 20 20 20 20  ring8: {        
18979 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53   /* same as TK_S
1897a 54 52 49 4e 47 2c 20 6f 75 74 32 2d 70 72 65 72  TRING, out2-prer
1897b 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65  elease */.  asse
1897c 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30  rt( pOp->p4.z!=0
1897d 20 29 3b 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f 64   );.  pOp->opcod
1897e 65 20 3d 20 4f 50 5f 53 74 72 69 6e 67 3b 0a 20  e = OP_String;. 
1897f 20 70 4f 70 2d 3e 70 31 20 3d 20 73 74 72 6c 65   pOp->p1 = strle
18980 6e 28 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 0a 23  n(pOp->p4.z);..#
18981 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
18982 49 54 5f 55 54 46 31 36 0a 20 20 69 66 28 20 65  IT_UTF16.  if( e
18983 6e 63 6f 64 69 6e 67 21 3d 53 51 4c 49 54 45 5f  ncoding!=SQLITE_
18984 55 54 46 38 20 29 7b 0a 20 20 20 20 73 71 6c 69  UTF8 ){.    sqli
18985 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72  te3VdbeMemSetStr
18986 28 70 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a  (pOut, pOp->p4.z
18987 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46  , -1, SQLITE_UTF
18988 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  8, SQLITE_STATIC
18989 29 3b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54  );.    if( SQLIT
1898a 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 56 64 62  E_OK!=sqlite3Vdb
1898b 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28  eChangeEncoding(
1898c 70 4f 75 74 2c 20 65 6e 63 6f 64 69 6e 67 29 20  pOut, encoding) 
1898d 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20  ) goto no_mem;. 
1898e 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b     if( SQLITE_OK
1898f 21 3d 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  !=sqlite3VdbeMem
18990 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 4f  MakeWriteable(pO
18991 75 74 29 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65  ut) ) goto no_me
18992 6d 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61  m;.    pOut->zMa
18993 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 70 4f  lloc = 0;.    pO
18994 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d  ut->flags |= MEM
18995 5f 53 74 61 74 69 63 3b 0a 20 20 20 20 70 4f 75  _Static;.    pOu
18996 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d  t->flags &= ~MEM
18997 5f 44 79 6e 3b 0a 20 20 20 20 69 66 28 20 70 4f  _Dyn;.    if( pO
18998 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 44 59  p->p4type==P4_DY
18999 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 20 20 73  NAMIC ){.      s
1899a 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1899b 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20   pOp->p4.z);.   
1899c 20 7d 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79   }.    pOp->p4ty
1899d 70 65 20 3d 20 50 34 5f 44 59 4e 41 4d 49 43 3b  pe = P4_DYNAMIC;
1899e 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d  .    pOp->p4.z =
1899f 20 70 4f 75 74 2d 3e 7a 3b 0a 20 20 20 20 70 4f   pOut->z;.    pO
189a0 70 2d 3e 70 31 20 3d 20 70 4f 75 74 2d 3e 6e 3b  p->p1 = pOut->n;
189a1 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 31  .    if( pOp->p1
189a2 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  >db->aLimit[SQLI
189a3 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d  TE_LIMIT_LENGTH]
189a4 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74   ){.      goto t
189a5 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20  oo_big;.    }.  
189a6 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f    UPDATE_MAX_BLO
189a7 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 20  BSIZE(pOut);.   
189a8 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 23 65 6e 64   break;.  }.#end
189a9 69 66 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31  if.  if( pOp->p1
189aa 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  >db->aLimit[SQLI
189ab 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d  TE_LIMIT_LENGTH]
189ac 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f   ){.    goto too
189ad 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 2f 2a 20 46  _big;.  }.  /* F
189ae 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74  all through to t
189af 68 65 20 6e 65 78 74 20 63 61 73 65 2c 20 4f 50  he next case, OP
189b0 5f 53 74 72 69 6e 67 20 2a 2f 0a 7d 0a 20 20 0a  _String */.}.  .
189b1 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 74 72 69 6e  /* Opcode: Strin
189b2 67 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a  g P1 P2 * P4 *.*
189b3 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20  *.** The string 
189b4 76 61 6c 75 65 20 50 34 20 6f 66 20 6c 65 6e 67  value P4 of leng
189b5 74 68 20 50 31 20 28 62 79 74 65 73 29 20 69 73  th P1 (bytes) is
189b6 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
189b7 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20  ter P2..*/.case 
189b8 4f 50 5f 53 74 72 69 6e 67 3a 20 7b 20 20 20 20  OP_String: {    
189b9 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
189ba 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73  erelease */.  as
189bb 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 21  sert( pOp->p4.z!
189bc 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c  =0 );.  pOut->fl
189bd 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45  ags = MEM_Str|ME
189be 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 54 65 72  M_Static|MEM_Ter
189bf 6d 3b 0a 20 20 70 4f 75 74 2d 3e 7a 20 3d 20 70  m;.  pOut->z = p
189c0 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 70 4f 75 74  Op->p4.z;.  pOut
189c1 2d 3e 6e 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20  ->n = pOp->p1;. 
189c2 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e 63   pOut->enc = enc
189c3 6f 64 69 6e 67 3b 0a 20 20 55 50 44 41 54 45 5f  oding;.  UPDATE_
189c4 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75  MAX_BLOBSIZE(pOu
189c5 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  t);.  break;.}..
189c6 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 75 6c 6c 20  /* Opcode: Null 
189c7 2a 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  * P2 * * *.**.**
189c8 20 57 72 69 74 65 20 61 20 4e 55 4c 4c 20 69 6e   Write a NULL in
189c9 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a  to register P2..
189ca 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 75 6c 6c 3a  */.case OP_Null:
189cb 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20   {           /* 
189cc 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
189cd 2a 2f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a  */.  break;.}...
189ce 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
189cf 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c  MIT_BLOB_LITERAL
189d0 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 6c 6f 62  ./* Opcode: Blob
189d1 20 50 31 20 50 32 20 2a 20 50 34 0a 2a 2a 0a 2a   P1 P2 * P4.**.*
189d2 2a 20 50 34 20 70 6f 69 6e 74 73 20 74 6f 20 61  * P4 points to a
189d3 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 50 31   blob of data P1
189d4 20 62 79 74 65 73 20 6c 6f 6e 67 2e 20 20 53 74   bytes long.  St
189d5 6f 72 65 20 74 68 69 73 0a 2a 2a 20 62 6c 6f 62  ore this.** blob
189d6 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e   in register P2.
189d7 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   This instructio
189d8 6e 20 69 73 20 6e 6f 74 20 63 6f 64 65 64 20 64  n is not coded d
189d9 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 74 68  irectly.** by th
189da 65 20 63 6f 6d 70 69 6c 65 72 2e 20 49 6e 73 74  e compiler. Inst
189db 65 61 64 2c 20 74 68 65 20 63 6f 6d 70 69 6c 65  ead, the compile
189dc 72 20 6c 61 79 65 72 20 73 70 65 63 69 66 69 65  r layer specifie
189dd 73 0a 2a 2a 20 61 6e 20 4f 50 5f 48 65 78 42 6c  s.** an OP_HexBl
189de 6f 62 20 6f 70 63 6f 64 65 2c 20 77 69 74 68 20  ob opcode, with 
189df 74 68 65 20 68 65 78 20 73 74 72 69 6e 67 20 72  the hex string r
189e0 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
189e1 0a 2a 2a 20 74 68 65 20 62 6c 6f 62 20 61 73 20  .** the blob as 
189e2 50 34 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20  P4. This opcode 
189e3 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 74  is transformed t
189e4 6f 20 61 6e 20 4f 50 5f 42 6c 6f 62 0a 2a 2a 20  o an OP_Blob.** 
189e5 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 69  the first time i
189e6 74 20 69 73 20 65 78 65 63 75 74 65 64 2e 0a 2a  t is executed..*
189e7 2f 0a 63 61 73 65 20 4f 50 5f 42 6c 6f 62 3a 20  /.case OP_Blob: 
189e8 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
189e9 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65   /* out2-prerele
189ea 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ase */.  assert(
189eb 20 70 4f 70 2d 3e 70 31 20 3c 3d 20 53 51 4c 49   pOp->p1 <= SQLI
189ec 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29 3b  TE_MAX_LENGTH );
189ed 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
189ee 6d 53 65 74 53 74 72 28 70 4f 75 74 2c 20 70 4f  mSetStr(pOut, pO
189ef 70 2d 3e 70 34 2e 7a 2c 20 70 4f 70 2d 3e 70 31  p->p4.z, pOp->p1
189f0 2c 20 30 2c 20 30 29 3b 0a 20 20 70 4f 75 74 2d  , 0, 0);.  pOut-
189f1 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b  >enc = encoding;
189f2 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c  .  UPDATE_MAX_BL
189f3 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20  OBSIZE(pOut);.  
189f4 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20  break;.}.#endif 
189f5 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  /* SQLITE_OMIT_B
189f6 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 2a 2f 0a 0a  LOB_LITERAL */..
189f7 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61 72 69 61  /* Opcode: Varia
189f8 62 6c 65 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  ble P1 P2 * * *.
189f9 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20  **.** The value 
189fa 6f 66 20 76 61 72 69 61 62 6c 65 20 50 31 20 69  of variable P1 i
189fb 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 72  s written into r
189fc 65 67 69 73 74 65 72 20 50 32 2e 20 41 20 76 61  egister P2. A va
189fd 72 69 61 62 6c 65 20 69 73 0a 2a 2a 20 61 6e 20  riable is.** an 
189fe 75 6e 6b 6e 6f 77 6e 20 69 6e 20 74 68 65 20 6f  unknown in the o
189ff 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 72 69  riginal SQL stri
18a00 6e 67 20 61 73 20 68 61 6e 64 65 64 20 74 6f 20  ng as handed to 
18a01 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 28  sqlite3_compile(
18a02 29 2e 0a 2a 2a 20 41 6e 79 20 6f 63 63 75 72 72  )..** Any occurr
18a03 65 6e 63 65 20 6f 66 20 74 68 65 20 27 3f 27 20  ence of the '?' 
18a04 63 68 61 72 61 63 74 65 72 20 69 6e 20 74 68 65  character in the
18a05 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 69 73   original SQL is
18a06 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a 2a 20 61   considered.** a
18a07 20 76 61 72 69 61 62 6c 65 2e 20 20 56 61 72 69   variable.  Vari
18a08 61 62 6c 65 73 20 69 6e 20 74 68 65 20 53 51 4c  ables in the SQL
18a09 20 73 74 72 69 6e 67 20 61 72 65 20 6e 75 6d 62   string are numb
18a0a 65 72 20 66 72 6f 6d 20 6c 65 66 74 20 74 6f 0a  er from left to.
18a0b 2a 2a 20 72 69 67 68 74 20 62 65 67 69 6e 6e 69  ** right beginni
18a0c 6e 67 20 77 69 74 68 20 31 2e 20 20 54 68 65 20  ng with 1.  The 
18a0d 76 61 6c 75 65 73 20 6f 66 20 76 61 72 69 61 62  values of variab
18a0e 6c 65 73 20 61 72 65 20 73 65 74 20 75 73 69 6e  les are set usin
18a0f 67 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33  g the.** sqlite3
18a10 5f 62 69 6e 64 28 29 20 41 50 49 2e 0a 2a 2f 0a  _bind() API..*/.
18a11 63 61 73 65 20 4f 50 5f 56 61 72 69 61 62 6c 65  case OP_Variable
18a12 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  : {           /*
18a13 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65   out2-prerelease
18a14 20 2a 2f 0a 20 20 69 6e 74 20 6a 20 3d 20 70 4f   */.  int j = pO
18a15 70 2d 3e 70 31 20 2d 20 31 3b 0a 20 20 4d 65 6d  p->p1 - 1;.  Mem
18a16 20 2a 70 56 61 72 3b 0a 20 20 61 73 73 65 72 74   *pVar;.  assert
18a17 28 20 6a 3e 3d 30 20 26 26 20 6a 3c 70 2d 3e 6e  ( j>=0 && j<p->n
18a18 56 61 72 20 29 3b 0a 0a 20 20 70 56 61 72 20 3d  Var );..  pVar =
18a19 20 26 70 2d 3e 61 56 61 72 5b 6a 5d 3b 0a 20 20   &p->aVar[j];.  
18a1a 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d  if( sqlite3VdbeM
18a1b 65 6d 54 6f 6f 42 69 67 28 70 56 61 72 29 20 29  emTooBig(pVar) )
18a1c 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62  {.    goto too_b
18a1d 69 67 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ig;.  }.  sqlite
18a1e 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43  3VdbeMemShallowC
18a1f 6f 70 79 28 70 4f 75 74 2c 20 26 70 2d 3e 61 56  opy(pOut, &p->aV
18a20 61 72 5b 6a 5d 2c 20 4d 45 4d 5f 53 74 61 74 69  ar[j], MEM_Stati
18a21 63 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58  c);.  UPDATE_MAX
18a22 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b  _BLOBSIZE(pOut);
18a23 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
18a24 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 20 50 31 20  Opcode: Move P1 
18a25 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  P2 P3 * *.**.** 
18a26 4d 6f 76 65 20 74 68 65 20 76 61 6c 75 65 73 20  Move the values 
18a27 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e 2e  in register P1..
18a28 50 31 2b 50 33 2d 31 20 6f 76 65 72 20 69 6e 74  P1+P3-1 over int
18a29 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20 50  o.** registers P
18a2a 32 2e 2e 50 32 2b 50 33 2d 31 2e 20 20 52 65 67  2..P2+P3-1.  Reg
18a2b 69 73 74 65 72 73 20 50 31 2e 2e 50 31 2b 50 31  isters P1..P1+P1
18a2c 2d 31 20 61 72 65 0a 2a 2a 20 6c 65 66 74 20 68  -1 are.** left h
18a2d 6f 6c 64 69 6e 67 20 61 20 4e 55 4c 4c 2e 20 20  olding a NULL.  
18a2e 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 66  It is an error f
18a2f 6f 72 20 72 65 67 69 73 74 65 72 20 72 61 6e 67  or register rang
18a30 65 73 0a 2a 2a 20 50 31 2e 2e 50 31 2b 50 33 2d  es.** P1..P1+P3-
18a31 31 20 61 6e 64 20 50 32 2e 2e 50 32 2b 50 33 2d  1 and P2..P2+P3-
18a32 31 20 74 6f 20 6f 76 65 72 6c 61 70 2e 0a 2a 2f  1 to overlap..*/
18a33 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65 3a 20 7b  .case OP_Move: {
18a34 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63  .  char *zMalloc
18a35 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d  ;.  int n = pOp-
18a36 3e 70 33 3b 0a 20 20 69 6e 74 20 70 31 20 3d 20  >p3;.  int p1 = 
18a37 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 6e 74 20 70  pOp->p1;.  int p
18a38 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 61  2 = pOp->p2;.  a
18a39 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20  ssert( n>0 );.  
18a3a 61 73 73 65 72 74 28 20 70 31 3e 30 20 29 3b 0a  assert( p1>0 );.
18a3b 20 20 61 73 73 65 72 74 28 20 70 31 2b 6e 3c 70    assert( p1+n<p
18a3c 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 49 6e 31  ->nMem );.  pIn1
18a3d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 31 5d 3b   = &p->aMem[p1];
18a3e 0a 20 20 61 73 73 65 72 74 28 20 70 32 3e 30 20  .  assert( p2>0 
18a3f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 32 2b  );.  assert( p2+
18a40 6e 3c 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70  n<p->nMem );.  p
18a41 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  Out = &p->aMem[p
18a42 32 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 31  2];.  assert( p1
18a43 2b 6e 3c 3d 70 32 20 7c 7c 20 70 32 2b 6e 3c 3d  +n<=p2 || p2+n<=
18a44 70 31 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e  p1 );.  while( n
18a45 2d 2d 20 29 7b 0a 20 20 20 20 7a 4d 61 6c 6c 6f  -- ){.    zMallo
18a46 63 20 3d 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f  c = pOut->zMallo
18a47 63 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61  c;.    pOut->zMa
18a48 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 73 71  lloc = 0;.    sq
18a49 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65  lite3VdbeMemMove
18a4a 28 70 4f 75 74 2c 20 70 49 6e 31 29 3b 0a 20 20  (pOut, pIn1);.  
18a4b 20 20 70 49 6e 31 2d 3e 7a 4d 61 6c 6c 6f 63 20    pIn1->zMalloc 
18a4c 3d 20 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 20 20 52  = zMalloc;.    R
18a4d 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 32  EGISTER_TRACE(p2
18a4e 2b 2b 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 70  ++, pOut);.    p
18a4f 49 6e 31 2b 2b 3b 0a 20 20 20 20 70 4f 75 74 2b  In1++;.    pOut+
18a50 2b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  +;.  }.  break;.
18a51 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f  }../* Opcode: Co
18a52 70 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  py P1 P2 * * *.*
18a53 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79  *.** Make a copy
18a54 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20   of register P1 
18a55 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32  into register P2
18a56 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73  ..**.** This ins
18a57 74 72 75 63 74 69 6f 6e 20 6d 61 6b 65 73 20 61  truction makes a
18a58 20 64 65 65 70 20 63 6f 70 79 20 6f 66 20 74 68   deep copy of th
18a59 65 20 76 61 6c 75 65 2e 20 20 41 20 64 75 70 6c  e value.  A dupl
18a5a 69 63 61 74 65 0a 2a 2a 20 69 73 20 6d 61 64 65  icate.** is made
18a5b 20 6f 66 20 61 6e 79 20 73 74 72 69 6e 67 20 6f   of any string o
18a5c 72 20 62 6c 6f 62 20 63 6f 6e 73 74 61 6e 74 2e  r blob constant.
18a5d 20 20 53 65 65 20 61 6c 73 6f 20 4f 50 5f 53 43    See also OP_SC
18a5e 6f 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  opy..*/.case OP_
18a5f 43 6f 70 79 3a 20 7b 0a 20 20 61 73 73 65 72 74  Copy: {.  assert
18a60 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20  ( pOp->p1>0 );. 
18a61 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31   assert( pOp->p1
18a62 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70  <=p->nMem );.  p
18a63 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  In1 = &p->aMem[p
18a64 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
18a65 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a  t( pOp->p2>0 );.
18a66 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
18a67 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  2<=p->nMem );.  
18a68 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pOut = &p->aMem[
18a69 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65  pOp->p2];.  asse
18a6a 72 74 28 20 70 4f 75 74 21 3d 70 49 6e 31 20 29  rt( pOut!=pIn1 )
18a6b 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
18a6c 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f  emShallowCopy(pO
18a6d 75 74 2c 20 70 49 6e 31 2c 20 4d 45 4d 5f 45 70  ut, pIn1, MEM_Ep
18a6e 68 65 6d 29 3b 0a 20 20 44 65 65 70 68 65 6d 65  hem);.  Deepheme
18a6f 72 61 6c 69 7a 65 28 70 4f 75 74 29 3b 0a 20 20  ralize(pOut);.  
18a70 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
18a71 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20  Op->p2, pOut);. 
18a72 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
18a73 63 6f 64 65 3a 20 53 43 6f 70 79 20 50 31 20 50  code: SCopy P1 P
18a74 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 61  2 * * *.**.** Ma
18a75 6b 65 20 61 20 73 68 61 6c 6c 6f 77 20 63 6f 70  ke a shallow cop
18a76 79 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31  y of register P1
18a77 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50   into register P
18a78 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  2..**.** This in
18a79 73 74 72 75 63 74 69 6f 6e 20 6d 61 6b 65 73 20  struction makes 
18a7a 61 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20 6f  a shallow copy o
18a7b 66 20 74 68 65 20 76 61 6c 75 65 2e 20 20 49 66  f the value.  If
18a7c 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 73   the value.** is
18a7d 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f   a string or blo
18a7e 62 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 70 79  b, then the copy
18a7f 20 69 73 20 6f 6e 6c 79 20 61 20 70 6f 69 6e 74   is only a point
18a80 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 6f 72 69  er to the.** ori
18a81 67 69 6e 61 6c 20 61 6e 64 20 68 65 6e 63 65 20  ginal and hence 
18a82 69 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  if the original 
18a83 63 68 61 6e 67 65 73 20 73 6f 20 77 69 6c 6c 20  changes so will 
18a84 74 68 65 20 63 6f 70 79 2e 0a 2a 2a 20 57 6f 72  the copy..** Wor
18a85 73 65 2c 20 69 66 20 74 68 65 20 6f 72 69 67 69  se, if the origi
18a86 6e 61 6c 20 69 73 20 64 65 61 6c 6c 6f 63 61 74  nal is deallocat
18a87 65 64 2c 20 74 68 65 20 63 6f 70 79 20 62 65 63  ed, the copy bec
18a88 6f 6d 65 73 20 69 6e 76 61 6c 69 64 2e 0a 2a 2a  omes invalid..**
18a89 20 54 68 75 73 20 74 68 65 20 70 72 6f 67 72 61   Thus the progra
18a8a 6d 20 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65  m must guarantee
18a8b 20 74 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e   that the origin
18a8c 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 63 68 61 6e  al will not chan
18a8d 67 65 0a 2a 2a 20 64 75 72 69 6e 67 20 74 68 65  ge.** during the
18a8e 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65   lifetime of the
18a8f 20 63 6f 70 79 2e 20 20 55 73 65 20 4f 50 5f 43   copy.  Use OP_C
18a90 6f 70 79 20 74 6f 20 6d 61 6b 65 20 61 20 63 6f  opy to make a co
18a91 6d 70 6c 65 74 65 0a 2a 2a 20 63 6f 70 79 2e 0a  mplete.** copy..
18a92 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 43 6f 70 79  */.case OP_SCopy
18a93 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  : {.  assert( pO
18a94 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 61 73 73  p->p1>0 );.  ass
18a95 65 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d  ert( pOp->p1<=p-
18a96 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 49 6e 31 20  >nMem );.  pIn1 
18a97 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  = &p->aMem[pOp->
18a98 70 31 5d 3b 0a 20 20 52 45 47 49 53 54 45 52 5f  p1];.  REGISTER_
18a99 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70  TRACE(pOp->p1, p
18a9a 49 6e 31 29 3b 0a 20 20 61 73 73 65 72 74 28 20  In1);.  assert( 
18a9b 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 61  pOp->p2>0 );.  a
18a9c 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d  ssert( pOp->p2<=
18a9d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75  p->nMem );.  pOu
18a9e 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  t = &p->aMem[pOp
18a9f 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p2];.  assert(
18aa0 20 70 4f 75 74 21 3d 70 49 6e 31 20 29 3b 0a 20   pOut!=pIn1 );. 
18aa1 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
18aa2 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74 2c  hallowCopy(pOut,
18aa3 20 70 49 6e 31 2c 20 4d 45 4d 5f 45 70 68 65 6d   pIn1, MEM_Ephem
18aa4 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52  );.  REGISTER_TR
18aa5 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75  ACE(pOp->p2, pOu
18aa6 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  t);.  break;.}..
18aa7 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 73 75 6c  /* Opcode: Resul
18aa8 74 52 6f 77 20 50 31 20 50 32 20 2a 20 2a 20 2a  tRow P1 P2 * * *
18aa9 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67 69 73  .**.** The regis
18aaa 74 65 72 73 20 50 31 20 74 68 72 6f 75 67 68 20  ters P1 through 
18aab 50 31 2b 50 32 2d 31 20 63 6f 6e 74 61 69 6e 20  P1+P2-1 contain 
18aac 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 0a  a single row of.
18aad 2a 2a 20 72 65 73 75 6c 74 73 2e 20 54 68 69 73  ** results. This
18aae 20 6f 70 63 6f 64 65 20 63 61 75 73 65 73 20 74   opcode causes t
18aaf 68 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  he sqlite3_step(
18ab0 29 20 63 61 6c 6c 20 74 6f 20 74 65 72 6d 69 6e  ) call to termin
18ab1 61 74 65 0a 2a 2a 20 77 69 74 68 20 61 6e 20 53  ate.** with an S
18ab2 51 4c 49 54 45 5f 52 4f 57 20 72 65 74 75 72 6e  QLITE_ROW return
18ab3 20 63 6f 64 65 20 61 6e 64 20 69 74 20 73 65 74   code and it set
18ab4 73 20 75 70 20 74 68 65 20 73 71 6c 69 74 65 33  s up the sqlite3
18ab5 5f 73 74 6d 74 0a 2a 2a 20 73 74 72 75 63 74 75  _stmt.** structu
18ab6 72 65 20 74 6f 20 70 72 6f 76 69 64 65 20 61 63  re to provide ac
18ab7 63 65 73 73 20 74 6f 20 74 68 65 20 74 6f 70 20  cess to the top 
18ab8 50 31 20 76 61 6c 75 65 73 20 61 73 20 74 68 65  P1 values as the
18ab9 20 72 65 73 75 6c 74 0a 2a 2a 20 72 6f 77 2e 0a   result.** row..
18aba 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 73 75 6c  */.case OP_Resul
18abb 74 52 6f 77 3a 20 7b 0a 20 20 4d 65 6d 20 2a 70  tRow: {.  Mem *p
18abc 4d 65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  Mem;.  int i;.  
18abd 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 73 43  assert( p->nResC
18abe 6f 6c 75 6d 6e 3d 3d 70 4f 70 2d 3e 70 32 20 29  olumn==pOp->p2 )
18abf 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
18ac0 3e 70 31 3e 30 20 29 3b 0a 20 20 61 73 73 65 72  >p1>0 );.  asser
18ac1 74 28 20 70 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e  t( pOp->p1+pOp->
18ac2 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 0a  p2<=p->nMem );..
18ac3 20 20 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20    /* Invalidate 
18ac4 61 6c 6c 20 65 70 68 65 6d 65 72 61 6c 20 63 75  all ephemeral cu
18ac5 72 73 6f 72 20 72 6f 77 20 63 61 63 68 65 73 20  rsor row caches 
18ac6 2a 2f 0a 20 20 70 2d 3e 63 61 63 68 65 43 74 72  */.  p->cacheCtr
18ac7 20 3d 20 28 70 2d 3e 63 61 63 68 65 43 74 72 20   = (p->cacheCtr 
18ac8 2b 20 32 29 7c 31 3b 0a 0a 20 20 2f 2a 20 4d 61  + 2)|1;..  /* Ma
18ac9 6b 65 20 73 75 72 65 20 74 68 65 20 72 65 73 75  ke sure the resu
18aca 6c 74 73 20 6f 66 20 74 68 65 20 63 75 72 72 65  lts of the curre
18acb 6e 74 20 72 6f 77 20 61 72 65 20 5c 30 30 30 20  nt row are \000 
18acc 74 65 72 6d 69 6e 61 74 65 64 0a 20 20 2a 2a 20  terminated.  ** 
18acd 61 6e 64 20 68 61 76 65 20 61 6e 20 61 73 73 69  and have an assi
18ace 67 6e 65 64 20 74 79 70 65 2e 20 20 54 68 65 20  gned type.  The 
18acf 72 65 73 75 6c 74 73 20 61 72 65 20 64 65 2d 65  results are de-e
18ad0 70 68 65 6d 65 72 61 6c 69 7a 65 64 20 61 73 0a  phemeralized as.
18ad1 20 20 2a 2a 20 61 73 20 73 69 64 65 20 65 66 66    ** as side eff
18ad2 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 70 4d 65 6d  ect..  */.  pMem
18ad3 20 3d 20 70 2d 3e 70 52 65 73 75 6c 74 53 65 74   = p->pResultSet
18ad4 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
18ad5 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  >p1];.  for(i=0;
18ad6 20 69 3c 70 4f 70 2d 3e 70 32 3b 20 69 2b 2b 29   i<pOp->p2; i++)
18ad7 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
18ad8 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65  eMemNulTerminate
18ad9 28 26 70 4d 65 6d 5b 69 5d 29 3b 0a 20 20 20 20  (&pMem[i]);.    
18ada 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 26 70  storeTypeInfo(&p
18adb 4d 65 6d 5b 69 5d 2c 20 65 6e 63 6f 64 69 6e 67  Mem[i], encoding
18adc 29 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f  );.    REGISTER_
18add 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2b 69 2c  TRACE(pOp->p1+i,
18ade 20 26 70 4d 65 6d 5b 69 5d 29 3b 0a 20 20 7d 0a   &pMem[i]);.  }.
18adf 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
18ae0 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f  Failed ) goto no
18ae1 5f 6d 65 6d 3b 0a 0a 20 20 2f 2a 20 52 65 74 75  _mem;..  /* Retu
18ae2 72 6e 20 53 51 4c 49 54 45 5f 52 4f 57 0a 20 20  rn SQLITE_ROW.  
18ae3 2a 2f 0a 20 20 70 2d 3e 6e 43 61 6c 6c 62 61 63  */.  p->nCallbac
18ae4 6b 2b 2b 3b 0a 20 20 70 2d 3e 70 63 20 3d 20 70  k++;.  p->pc = p
18ae5 63 20 2b 20 31 3b 0a 20 20 72 63 20 3d 20 53 51  c + 1;.  rc = SQ
18ae6 4c 49 54 45 5f 52 4f 57 3b 0a 20 20 67 6f 74 6f  LITE_ROW;.  goto
18ae7 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 7d 0a   vdbe_return;.}.
18ae8 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6e 63  ./* Opcode: Conc
18ae9 61 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  at P1 P2 P3 * *.
18aea 2a 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 74 65  **.** Add the te
18aeb 78 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  xt in register P
18aec 31 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f  1 onto the end o
18aed 66 20 74 68 65 20 74 65 78 74 20 69 6e 0a 2a 2a  f the text in.**
18aee 20 72 65 67 69 73 74 65 72 20 50 32 20 61 6e 64   register P2 and
18aef 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   store the resul
18af0 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  t in register P3
18af1 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 74  ..** If either t
18af2 68 65 20 50 31 20 6f 72 20 50 32 20 74 65 78 74  he P1 or P2 text
18af3 20 61 72 65 20 4e 55 4c 4c 20 74 68 65 6e 20 73   are NULL then s
18af4 74 6f 72 65 20 4e 55 4c 4c 20 69 6e 20 50 33 2e  tore NULL in P3.
18af5 0a 2a 2a 0a 2a 2a 20 20 20 50 33 20 3d 20 50 32  .**.**   P3 = P2
18af6 20 7c 7c 20 50 31 0a 2a 2a 0a 2a 2a 20 49 74 20   || P1.**.** It 
18af7 69 73 20 69 6c 6c 65 67 61 6c 20 66 6f 72 20 50  is illegal for P
18af8 31 20 61 6e 64 20 50 33 20 74 6f 20 62 65 20 74  1 and P3 to be t
18af9 68 65 20 73 61 6d 65 20 72 65 67 69 73 74 65 72  he same register
18afa 2e 20 53 6f 6d 65 74 69 6d 65 73 2c 0a 2a 2a 20  . Sometimes,.** 
18afb 69 66 20 50 33 20 69 73 20 74 68 65 20 73 61 6d  if P3 is the sam
18afc 65 20 72 65 67 69 73 74 65 72 20 61 73 20 50 32  e register as P2
18afd 2c 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  , the implementa
18afe 74 69 6f 6e 20 69 73 20 61 62 6c 65 0a 2a 2a 20  tion is able.** 
18aff 74 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d 63 70  to avoid a memcp
18b00 79 28 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  y()..*/.case OP_
18b01 43 6f 6e 63 61 74 3a 20 7b 20 20 20 20 20 20 20  Concat: {       
18b02 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
18b03 4b 5f 43 4f 4e 43 41 54 2c 20 69 6e 31 2c 20 69  K_CONCAT, in1, i
18b04 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 20 20 69 36  n2, out3 */.  i6
18b05 34 20 6e 42 79 74 65 3b 0a 0a 20 20 61 73 73 65  4 nByte;..  asse
18b06 72 74 28 20 70 49 6e 31 21 3d 70 4f 75 74 20 29  rt( pIn1!=pOut )
18b07 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66  ;.  if( (pIn1->f
18b08 6c 61 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61  lags | pIn2->fla
18b09 67 73 29 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29  gs) & MEM_Null )
18b0a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
18b0b 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74  eMemSetNull(pOut
18b0c 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  );.    break;.  
18b0d 7d 0a 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70  }.  ExpandBlob(p
18b0e 49 6e 31 29 3b 0a 20 20 53 74 72 69 6e 67 69 66  In1);.  Stringif
18b0f 79 28 70 49 6e 31 2c 20 65 6e 63 6f 64 69 6e 67  y(pIn1, encoding
18b10 29 3b 0a 20 20 45 78 70 61 6e 64 42 6c 6f 62 28  );.  ExpandBlob(
18b11 70 49 6e 32 29 3b 0a 20 20 53 74 72 69 6e 67 69  pIn2);.  Stringi
18b12 66 79 28 70 49 6e 32 2c 20 65 6e 63 6f 64 69 6e  fy(pIn2, encodin
18b13 67 29 3b 0a 20 20 6e 42 79 74 65 20 3d 20 70 49  g);.  nByte = pI
18b14 6e 31 2d 3e 6e 20 2b 20 70 49 6e 32 2d 3e 6e 3b  n1->n + pIn2->n;
18b15 0a 20 20 69 66 28 20 6e 42 79 74 65 3e 64 62 2d  .  if( nByte>db-
18b16 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
18b17 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a  IMIT_LENGTH] ){.
18b18 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67      goto too_big
18b19 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54 79  ;.  }.  MemSetTy
18b1a 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d  peFlag(pOut, MEM
18b1b 5f 53 74 72 29 3b 0a 20 20 69 66 28 20 73 71 6c  _Str);.  if( sql
18b1c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
18b1d 70 4f 75 74 2c 20 6e 42 79 74 65 2b 32 2c 20 70  pOut, nByte+2, p
18b1e 4f 75 74 3d 3d 70 49 6e 32 29 20 29 7b 0a 20 20  Out==pIn2) ){.  
18b1f 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20    goto no_mem;. 
18b20 20 7d 0a 20 20 69 66 28 20 70 4f 75 74 21 3d 70   }.  if( pOut!=p
18b21 49 6e 32 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70  In2 ){.    memcp
18b22 79 28 70 4f 75 74 2d 3e 7a 2c 20 70 49 6e 32 2d  y(pOut->z, pIn2-
18b23 3e 7a 2c 20 70 49 6e 32 2d 3e 6e 29 3b 0a 20 20  >z, pIn2->n);.  
18b24 7d 0a 20 20 6d 65 6d 63 70 79 28 26 70 4f 75 74  }.  memcpy(&pOut
18b25 2d 3e 7a 5b 70 49 6e 32 2d 3e 6e 5d 2c 20 70 49  ->z[pIn2->n], pI
18b26 6e 31 2d 3e 7a 2c 20 70 49 6e 31 2d 3e 6e 29 3b  n1->z, pIn1->n);
18b27 0a 20 20 70 4f 75 74 2d 3e 7a 5b 6e 42 79 74 65  .  pOut->z[nByte
18b28 5d 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d 3e 7a  ] = 0;.  pOut->z
18b29 5b 6e 42 79 74 65 2b 31 5d 20 3d 20 30 3b 0a 20  [nByte+1] = 0;. 
18b2a 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20   pOut->flags |= 
18b2b 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 70 4f 75 74  MEM_Term;.  pOut
18b2c 2d 3e 6e 20 3d 20 6e 42 79 74 65 3b 0a 20 20 70  ->n = nByte;.  p
18b2d 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64  Out->enc = encod
18b2e 69 6e 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41  ing;.  UPDATE_MA
18b2f 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29  X_BLOBSIZE(pOut)
18b30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
18b31 20 4f 70 63 6f 64 65 3a 20 41 64 64 20 50 31 20   Opcode: Add P1 
18b32 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  P2 P3 * *.**.** 
18b33 41 64 64 20 74 68 65 20 76 61 6c 75 65 20 69 6e  Add the value in
18b34 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20   register P1 to 
18b35 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
18b36 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20  ister P2.** and 
18b37 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  store the result
18b38 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e   in register P3.
18b39 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e  .** If either in
18b3a 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65  put is NULL, the
18b3b 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e   result is NULL.
18b3c 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d  .*/./* Opcode: M
18b3d 75 6c 74 69 70 6c 79 20 50 31 20 50 32 20 50 33  ultiply P1 P2 P3
18b3e 20 2a 20 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 75   * *.**.**.** Mu
18b3f 6c 74 69 70 6c 79 20 74 68 65 20 76 61 6c 75 65  ltiply the value
18b40 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
18b41 62 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  by the value in 
18b42 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61  register P2.** a
18b43 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73  nd store the res
18b44 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20  ult in register 
18b45 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72  P3..** If either
18b46 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20   input is NULL, 
18b47 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55  the result is NU
18b48 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  LL..*/./* Opcode
18b49 3a 20 53 75 62 74 72 61 63 74 20 50 31 20 50 32  : Subtract P1 P2
18b4a 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 75   P3 * *.**.** Su
18b4b 62 74 72 61 63 74 20 74 68 65 20 76 61 6c 75 65  btract the value
18b4c 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
18b4d 66 72 6f 6d 20 74 68 65 20 76 61 6c 75 65 20 69  from the value i
18b4e 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a  n register P2.**
18b4f 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72   and store the r
18b50 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65  esult in registe
18b51 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68  r P3..** If eith
18b52 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c  er input is NULL
18b53 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  , the result is 
18b54 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f  NULL..*/./* Opco
18b55 64 65 3a 20 44 69 76 69 64 65 20 50 31 20 50 32  de: Divide P1 P2
18b56 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 69   P3 * *.**.** Di
18b57 76 69 64 65 20 74 68 65 20 76 61 6c 75 65 20 69  vide the value i
18b58 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 62 79  n register P1 by
18b59 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
18b5a 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64  gister P2.** and
18b5b 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   store the resul
18b5c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  t in register P3
18b5d 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 20  .  If the value 
18b5e 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a  in register P2.*
18b5f 2a 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20  * is zero, then 
18b60 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55  the result is NU
18b61 4c 4c 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72  LL..** If either
18b62 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20   input is NULL, 
18b63 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55  the result is NU
18b64 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  LL..*/./* Opcode
18b65 3a 20 52 65 6d 61 69 6e 64 65 72 20 50 31 20 50  : Remainder P1 P
18b66 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43  2 P3 * *.**.** C
18b67 6f 6d 70 75 74 65 20 74 68 65 20 72 65 6d 61 69  ompute the remai
18b68 6e 64 65 72 20 61 66 74 65 72 20 69 6e 74 65 67  nder after integ
18b69 65 72 20 64 69 76 69 73 69 6f 6e 20 6f 66 20 74  er division of t
18b6a 68 65 20 76 61 6c 75 65 20 69 6e 0a 2a 2a 20 72  he value in.** r
18b6b 65 67 69 73 74 65 72 20 50 31 20 62 79 20 74 68  egister P1 by th
18b6c 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
18b6d 74 65 72 20 50 32 20 61 6e 64 20 73 74 6f 72 65  ter P2 and store
18b6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 50   the result in P
18b6f 33 2e 20 0a 2a 2a 20 49 66 20 74 68 65 20 76 61  3. .** If the va
18b70 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
18b71 50 32 20 69 73 20 7a 65 72 6f 20 74 68 65 20 72  P2 is zero the r
18b72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a  esult is NULL..*
18b73 2a 20 49 66 20 65 69 74 68 65 72 20 6f 70 65 72  * If either oper
18b74 61 6e 64 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65  and is NULL, the
18b75 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e   result is NULL.
18b76 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 64 64 3a  .*/.case OP_Add:
18b77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18b78 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
18b79 5f 50 4c 55 53 2c 20 69 6e 31 2c 20 69 6e 32 2c  _PLUS, in1, in2,
18b7a 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50   out3 */.case OP
18b7b 5f 53 75 62 74 72 61 63 74 3a 20 20 20 20 20 20  _Subtract:      
18b7c 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
18b7d 61 73 20 54 4b 5f 4d 49 4e 55 53 2c 20 69 6e 31  as TK_MINUS, in1
18b7e 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63  , in2, out3 */.c
18b7f 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a  ase OP_Multiply:
18b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
18b81 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 41 52   same as TK_STAR
18b82 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33  , in1, in2, out3
18b83 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 69 76 69   */.case OP_Divi
18b84 64 65 3a 20 20 20 20 20 20 20 20 20 20 20 20 20  de:             
18b85 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
18b86 5f 53 4c 41 53 48 2c 20 69 6e 31 2c 20 69 6e 32  _SLASH, in1, in2
18b87 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f  , out3 */.case O
18b88 50 5f 52 65 6d 61 69 6e 64 65 72 3a 20 7b 20 20  P_Remainder: {  
18b89 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
18b8a 20 61 73 20 54 4b 5f 52 45 4d 2c 20 69 6e 31 2c   as TK_REM, in1,
18b8b 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 20 20   in2, out3 */.  
18b8c 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 61 70 70  int flags;.  app
18b8d 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74  lyNumericAffinit
18b8e 79 28 70 49 6e 31 29 3b 0a 20 20 61 70 70 6c 79  y(pIn1);.  apply
18b8f 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28  NumericAffinity(
18b90 70 49 6e 32 29 3b 0a 20 20 66 6c 61 67 73 20 3d  pIn2);.  flags =
18b91 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70   pIn1->flags | p
18b92 49 6e 32 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66  In2->flags;.  if
18b93 28 20 28 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e  ( (flags & MEM_N
18b94 75 6c 6c 29 21 3d 30 20 29 20 67 6f 74 6f 20 61  ull)!=0 ) goto a
18b95 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74  rithmetic_result
18b96 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 69 66 28 20  _is_null;.  if( 
18b97 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 70  (pIn1->flags & p
18b98 49 6e 32 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In2->flags & MEM
18b99 5f 49 6e 74 29 3d 3d 4d 45 4d 5f 49 6e 74 20 29  _Int)==MEM_Int )
18b9a 7b 0a 20 20 20 20 69 36 34 20 61 2c 20 62 3b 0a  {.    i64 a, b;.
18b9b 20 20 20 20 61 20 3d 20 70 49 6e 31 2d 3e 75 2e      a = pIn1->u.
18b9c 69 3b 0a 20 20 20 20 62 20 3d 20 70 49 6e 32 2d  i;.    b = pIn2-
18b9d 3e 75 2e 69 3b 0a 20 20 20 20 73 77 69 74 63 68  >u.i;.    switch
18b9e 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b  ( pOp->opcode ){
18b9f 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 41  .      case OP_A
18ba0 64 64 3a 20 20 20 20 20 20 20 20 20 62 20 2b 3d  dd:         b +=
18ba1 20 61 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b   a;       break;
18ba2 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 53  .      case OP_S
18ba3 75 62 74 72 61 63 74 3a 20 20 20 20 62 20 2d 3d  ubtract:    b -=
18ba4 20 61 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b   a;       break;
18ba5 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 4d  .      case OP_M
18ba6 75 6c 74 69 70 6c 79 3a 20 20 20 20 62 20 2a 3d  ultiply:    b *=
18ba7 20 61 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b   a;       break;
18ba8 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 44  .      case OP_D
18ba9 69 76 69 64 65 3a 20 7b 0a 20 20 20 20 20 20 20  ivide: {.       
18baa 20 69 66 28 20 61 3d 3d 30 20 29 20 67 6f 74 6f   if( a==0 ) goto
18bab 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75   arithmetic_resu
18bac 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20  lt_is_null;.    
18bad 20 20 20 20 2f 2a 20 44 69 76 69 64 69 6e 67 20      /* Dividing 
18bae 74 68 65 20 6c 61 72 67 65 73 74 20 70 6f 73 73  the largest poss
18baf 69 62 6c 65 20 6e 65 67 61 74 69 76 65 20 36 34  ible negative 64
18bb0 2d 62 69 74 20 69 6e 74 65 67 65 72 20 28 31 3c  -bit integer (1<
18bb1 3c 36 33 29 20 62 79 20 0a 20 20 20 20 20 20 20  <63) by .       
18bb2 20 2a 2a 20 2d 31 20 72 65 74 75 72 6e 73 20 61   ** -1 returns a
18bb3 6e 20 69 6e 74 65 67 65 72 20 74 6f 6f 20 6c 61  n integer too la
18bb4 72 67 65 20 74 6f 20 73 74 6f 72 65 20 69 6e 20  rge to store in 
18bb5 61 20 36 34 2d 62 69 74 20 64 61 74 61 2d 74 79  a 64-bit data-ty
18bb6 70 65 2e 20 4f 6e 0a 20 20 20 20 20 20 20 20 2a  pe. On.        *
18bb7 2a 20 73 6f 6d 65 20 61 72 63 68 69 74 65 63 74  * some architect
18bb8 75 72 65 73 2c 20 74 68 65 20 76 61 6c 75 65 20  ures, the value 
18bb9 6f 76 65 72 66 6c 6f 77 73 20 74 6f 20 28 31 3c  overflows to (1<
18bba 3c 36 33 29 2e 20 4f 6e 20 6f 74 68 65 72 73 2c  <63). On others,
18bbb 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 53 49  .        ** a SI
18bbc 47 46 50 45 20 69 73 20 69 73 73 75 65 64 2e 20  GFPE is issued. 
18bbd 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  The following st
18bbe 61 74 65 6d 65 6e 74 20 6e 6f 72 6d 61 6c 69 7a  atement normaliz
18bbf 65 73 20 74 68 69 73 0a 20 20 20 20 20 20 20 20  es this.        
18bc0 2a 2a 20 62 65 68 61 76 69 6f 72 20 73 6f 20 74  ** behavior so t
18bc1 68 61 74 20 61 6c 6c 20 61 72 63 68 69 74 65 63  hat all architec
18bc2 74 75 72 65 73 20 62 65 68 61 76 65 20 61 73 20  tures behave as 
18bc3 69 66 20 69 6e 74 65 67 65 72 20 0a 20 20 20 20  if integer .    
18bc4 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20      ** overflow 
18bc5 6f 63 63 75 72 72 65 64 2e 0a 20 20 20 20 20 20  occurred..      
18bc6 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28    */.        if(
18bc7 20 61 3d 3d 2d 31 20 26 26 20 62 3d 3d 53 4d 41   a==-1 && b==SMA
18bc8 4c 4c 45 53 54 5f 49 4e 54 36 34 20 29 20 61 20  LLEST_INT64 ) a 
18bc9 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 62 20 2f  = 1;.        b /
18bca 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 62 72 65  = a;.        bre
18bcb 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
18bcc 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
18bcd 20 20 20 20 20 69 66 28 20 61 3d 3d 30 20 29 20       if( a==0 ) 
18bce 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f  goto arithmetic_
18bcf 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a  result_is_null;.
18bd0 20 20 20 20 20 20 20 20 69 66 28 20 61 3d 3d 2d          if( a==-
18bd1 31 20 29 20 61 20 3d 20 31 3b 0a 20 20 20 20 20  1 ) a = 1;.     
18bd2 20 20 20 62 20 25 3d 20 61 3b 0a 20 20 20 20 20     b %= a;.     
18bd3 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
18bd4 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f 75 74  }.    }.    pOut
18bd5 2d 3e 75 2e 69 20 3d 20 62 3b 0a 20 20 20 20 4d  ->u.i = b;.    M
18bd6 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f  emSetTypeFlag(pO
18bd7 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20  ut, MEM_Int);.  
18bd8 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 75 62 6c  }else{.    doubl
18bd9 65 20 61 2c 20 62 3b 0a 20 20 20 20 61 20 3d 20  e a, b;.    a = 
18bda 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56  sqlite3VdbeRealV
18bdb 61 6c 75 65 28 70 49 6e 31 29 3b 0a 20 20 20 20  alue(pIn1);.    
18bdc 62 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52  b = sqlite3VdbeR
18bdd 65 61 6c 56 61 6c 75 65 28 70 49 6e 32 29 3b 0a  ealValue(pIn2);.
18bde 20 20 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d      switch( pOp-
18bdf 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 20  >opcode ){.     
18be0 20 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20   case OP_Add:   
18be1 20 20 20 20 20 20 62 20 2b 3d 20 61 3b 20 20 20        b += a;   
18be2 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
18be3 20 63 61 73 65 20 4f 50 5f 53 75 62 74 72 61 63   case OP_Subtrac
18be4 74 3a 20 20 20 20 62 20 2d 3d 20 61 3b 20 20 20  t:    b -= a;   
18be5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
18be6 20 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c   case OP_Multipl
18be7 79 3a 20 20 20 20 62 20 2a 3d 20 61 3b 20 20 20  y:    b *= a;   
18be8 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
18be9 20 63 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a   case OP_Divide:
18bea 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61   {.        if( a
18beb 3d 3d 30 2e 30 20 29 20 67 6f 74 6f 20 61 72 69  ==0.0 ) goto ari
18bec 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69  thmetic_result_i
18bed 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20  s_null;.        
18bee 62 20 2f 3d 20 61 3b 0a 20 20 20 20 20 20 20 20  b /= a;.        
18bef 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
18bf0 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a       default: {.
18bf1 20 20 20 20 20 20 20 20 69 36 34 20 69 61 20 3d          i64 ia =
18bf2 20 28 69 36 34 29 61 3b 0a 20 20 20 20 20 20 20   (i64)a;.       
18bf3 20 69 36 34 20 69 62 20 3d 20 28 69 36 34 29 62   i64 ib = (i64)b
18bf4 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 61  ;.        if( ia
18bf5 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68  ==0 ) goto arith
18bf6 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f  metic_result_is_
18bf7 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 66  null;.        if
18bf8 28 20 69 61 3d 3d 2d 31 20 29 20 69 61 20 3d 20  ( ia==-1 ) ia = 
18bf9 31 3b 0a 20 20 20 20 20 20 20 20 62 20 3d 20 69  1;.        b = i
18bfa 62 20 25 20 69 61 3b 0a 20 20 20 20 20 20 20 20  b % ia;.        
18bfb 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
18bfc 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
18bfd 69 74 65 33 49 73 4e 61 4e 28 62 29 20 29 7b 0a  ite3IsNaN(b) ){.
18bfe 20 20 20 20 20 20 67 6f 74 6f 20 61 72 69 74 68        goto arith
18bff 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f  metic_result_is_
18c00 6e 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20  null;.    }.    
18c01 70 4f 75 74 2d 3e 72 20 3d 20 62 3b 0a 20 20 20  pOut->r = b;.   
18c02 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28   MemSetTypeFlag(
18c03 70 4f 75 74 2c 20 4d 45 4d 5f 52 65 61 6c 29 3b  pOut, MEM_Real);
18c04 0a 20 20 20 20 69 66 28 20 28 66 6c 61 67 73 20  .    if( (flags 
18c05 26 20 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 29  & MEM_Real)==0 )
18c06 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
18c07 64 62 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69  dbeIntegerAffini
18c08 74 79 28 70 4f 75 74 29 3b 0a 20 20 20 20 7d 0a  ty(pOut);.    }.
18c09 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 0a 61 72    }.  break;..ar
18c0a 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f  ithmetic_result_
18c0b 69 73 5f 6e 75 6c 6c 3a 0a 20 20 73 71 6c 69 74  is_null:.  sqlit
18c0c 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c  e3VdbeMemSetNull
18c0d 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b  (pOut);.  break;
18c0e 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43  .}../* Opcode: C
18c0f 6f 6c 6c 53 65 71 20 2a 20 2a 20 50 34 0a 2a 2a  ollSeq * * P4.**
18c10 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e  .** P4 is a poin
18c11 74 65 72 20 74 6f 20 61 20 43 6f 6c 6c 53 65 71  ter to a CollSeq
18c12 20 73 74 72 75 63 74 2e 20 49 66 20 74 68 65 20   struct. If the 
18c13 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 61 20 75  next call to a u
18c14 73 65 72 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  ser function.** 
18c15 6f 72 20 61 67 67 72 65 67 61 74 65 20 63 61 6c  or aggregate cal
18c16 6c 73 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e  ls sqlite3GetFun
18c17 63 43 6f 6c 6c 53 65 71 28 29 2c 20 74 68 69 73  cCollSeq(), this
18c18 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
18c19 6e 63 65 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 72  nce will.** be r
18c1a 65 74 75 72 6e 65 64 2e 20 54 68 69 73 20 69 73  eturned. This is
18c1b 20 75 73 65 64 20 62 79 20 74 68 65 20 62 75 69   used by the bui
18c1c 6c 74 2d 69 6e 20 6d 69 6e 28 29 2c 20 6d 61 78  lt-in min(), max
18c1d 28 29 20 61 6e 64 20 6e 75 6c 6c 69 66 28 29 0a  () and nullif().
18c1e 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a  ** functions..**
18c1f 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63  .** The interfac
18c20 65 20 75 73 65 64 20 62 79 20 74 68 65 20 69 6d  e used by the im
18c21 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
18c22 74 68 65 20 61 66 6f 72 65 6d 65 6e 74 69 6f 6e  the aforemention
18c23 65 64 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20  ed functions.** 
18c24 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 20  to retrieve the 
18c25 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
18c26 63 65 20 73 65 74 20 62 79 20 74 68 69 73 20 6f  ce set by this o
18c27 70 63 6f 64 65 20 69 73 20 6e 6f 74 20 61 76 61  pcode is not ava
18c28 69 6c 61 62 6c 65 0a 2a 2a 20 70 75 62 6c 69 63  ilable.** public
18c29 6c 79 2c 20 6f 6e 6c 79 20 74 6f 20 75 73 65 72  ly, only to user
18c2a 20 66 75 6e 63 74 69 6f 6e 73 20 64 65 66 69 6e   functions defin
18c2b 65 64 20 69 6e 20 66 75 6e 63 2e 63 2e 0a 2a 2f  ed in func.c..*/
18c2c 0a 63 61 73 65 20 4f 50 5f 43 6f 6c 6c 53 65 71  .case OP_CollSeq
18c2d 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  : {.  assert( pO
18c2e 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f  p->p4type==P4_CO
18c2f 4c 4c 53 45 51 20 29 3b 0a 20 20 62 72 65 61 6b  LLSEQ );.  break
18c30 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
18c31 46 75 6e 63 74 69 6f 6e 20 50 31 20 50 32 20 50  Function P1 P2 P
18c32 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 49 6e  3 P4 P5.**.** In
18c33 76 6f 6b 65 20 61 20 75 73 65 72 20 66 75 6e 63  voke a user func
18c34 74 69 6f 6e 20 28 50 34 20 69 73 20 61 20 70 6f  tion (P4 is a po
18c35 69 6e 74 65 72 20 74 6f 20 61 20 46 75 6e 63 74  inter to a Funct
18c36 69 6f 6e 20 73 74 72 75 63 74 75 72 65 20 74 68  ion structure th
18c37 61 74 0a 2a 2a 20 64 65 66 69 6e 65 73 20 74 68  at.** defines th
18c38 65 20 66 75 6e 63 74 69 6f 6e 29 20 77 69 74 68  e function) with
18c39 20 50 35 20 61 72 67 75 6d 65 6e 74 73 20 74 61   P5 arguments ta
18c3a 6b 65 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65  ken from registe
18c3b 72 20 50 32 20 61 6e 64 0a 2a 2a 20 73 75 63 63  r P2 and.** succ
18c3c 65 73 73 6f 72 73 2e 20 20 54 68 65 20 72 65 73  essors.  The res
18c3d 75 6c 74 20 6f 66 20 74 68 65 20 66 75 6e 63 74  ult of the funct
18c3e 69 6f 6e 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ion is stored in
18c3f 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a   register P3..**
18c40 20 52 65 67 69 73 74 65 72 20 50 33 20 6d 75 73   Register P3 mus
18c41 74 20 6e 6f 74 20 62 65 20 6f 6e 65 20 6f 66 20  t not be one of 
18c42 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e 70  the function inp
18c43 75 74 73 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73  uts..**.** P1 is
18c44 20 61 20 33 32 2d 62 69 74 20 62 69 74 6d 61 73   a 32-bit bitmas
18c45 6b 20 69 6e 64 69 63 61 74 69 6e 67 20 77 68 65  k indicating whe
18c46 74 68 65 72 20 6f 72 20 6e 6f 74 20 65 61 63 68  ther or not each
18c47 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
18c48 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 61   .** function wa
18c49 73 20 64 65 74 65 72 6d 69 6e 65 64 20 74 6f 20  s determined to 
18c4a 62 65 20 63 6f 6e 73 74 61 6e 74 20 61 74 20 63  be constant at c
18c4b 6f 6d 70 69 6c 65 20 74 69 6d 65 2e 20 49 66 20  ompile time. If 
18c4c 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67  the first.** arg
18c4d 75 6d 65 6e 74 20 77 61 73 20 63 6f 6e 73 74 61  ument was consta
18c4e 6e 74 20 74 68 65 6e 20 62 69 74 20 30 20 6f 66  nt then bit 0 of
18c4f 20 50 31 20 69 73 20 73 65 74 2e 20 54 68 69 73   P1 is set. This
18c50 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65   is used to dete
18c51 72 6d 69 6e 65 0a 2a 2a 20 77 68 65 74 68 65 72  rmine.** whether
18c52 20 6d 65 74 61 20 64 61 74 61 20 61 73 73 6f 63   meta data assoc
18c53 69 61 74 65 64 20 77 69 74 68 20 61 20 75 73 65  iated with a use
18c54 72 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d  r function argum
18c55 65 6e 74 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a  ent using the.**
18c56 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78   sqlite3_set_aux
18c57 64 61 74 61 28 29 20 41 50 49 20 6d 61 79 20 62  data() API may b
18c58 65 20 73 61 66 65 6c 79 20 72 65 74 61 69 6e 65  e safely retaine
18c59 64 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74  d until the next
18c5a 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  .** invocation o
18c5b 66 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 0a 2a  f this opcode..*
18c5c 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 41  *.** See also: A
18c5d 67 67 53 74 65 70 20 61 6e 64 20 41 67 67 46 69  ggStep and AggFi
18c5e 6e 61 6c 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 46  nal.*/.case OP_F
18c5f 75 6e 63 74 69 6f 6e 3a 20 7b 0a 20 20 69 6e 74  unction: {.  int
18c60 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 41 72 67 3b   i;.  Mem *pArg;
18c61 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
18c62 78 74 20 63 74 78 3b 0a 20 20 73 71 6c 69 74 65  xt ctx;.  sqlite
18c63 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c 3b  3_value **apVal;
18c64 0a 20 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e  .  int n = pOp->
18c65 70 35 3b 0a 0a 20 20 61 70 56 61 6c 20 3d 20 70  p5;..  apVal = p
18c66 2d 3e 61 70 41 72 67 3b 0a 20 20 61 73 73 65 72  ->apArg;.  asser
18c67 74 28 20 61 70 56 61 6c 20 7c 7c 20 6e 3d 3d 30  t( apVal || n==0
18c68 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e   );..  assert( n
18c69 3d 3d 30 20 7c 7c 20 28 70 4f 70 2d 3e 70 32 3e  ==0 || (pOp->p2>
18c6a 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 6e 3c 3d  0 && pOp->p2+n<=
18c6b 70 2d 3e 6e 4d 65 6d 29 20 29 3b 0a 20 20 61 73  p->nMem) );.  as
18c6c 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 70 4f  sert( pOp->p3<pO
18c6d 70 2d 3e 70 32 20 7c 7c 20 70 4f 70 2d 3e 70 33  p->p2 || pOp->p3
18c6e 3e 3d 70 4f 70 2d 3e 70 32 2b 6e 20 29 3b 0a 20  >=pOp->p2+n );. 
18c6f 20 70 41 72 67 20 3d 20 26 70 2d 3e 61 4d 65 6d   pArg = &p->aMem
18c70 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 66 6f 72  [pOp->p2];.  for
18c71 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 2c 20  (i=0; i<n; i++, 
18c72 70 41 72 67 2b 2b 29 7b 0a 20 20 20 20 61 70 56  pArg++){.    apV
18c73 61 6c 5b 69 5d 20 3d 20 70 41 72 67 3b 0a 20 20  al[i] = pArg;.  
18c74 20 20 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28    storeTypeInfo(
18c75 70 41 72 67 2c 20 65 6e 63 6f 64 69 6e 67 29 3b  pArg, encoding);
18c76 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52  .    REGISTER_TR
18c77 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 41 72  ACE(pOp->p2, pAr
18c78 67 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  g);.  }..  asser
18c79 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d  t( pOp->p4type==
18c7a 50 34 5f 46 55 4e 43 44 45 46 20 7c 7c 20 70 4f  P4_FUNCDEF || pO
18c7b 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56 44  p->p4type==P4_VD
18c7c 42 45 46 55 4e 43 20 29 3b 0a 20 20 69 66 28 20  BEFUNC );.  if( 
18c7d 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
18c7e 46 55 4e 43 44 45 46 20 29 7b 0a 20 20 20 20 63  FUNCDEF ){.    c
18c7f 74 78 2e 70 46 75 6e 63 20 3d 20 70 4f 70 2d 3e  tx.pFunc = pOp->
18c80 70 34 2e 70 46 75 6e 63 3b 0a 20 20 20 20 63 74  p4.pFunc;.    ct
18c81 78 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 30 3b  x.pVdbeFunc = 0;
18c82 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 74  .  }else{.    ct
18c83 78 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 28 56  x.pVdbeFunc = (V
18c84 64 62 65 46 75 6e 63 2a 29 70 4f 70 2d 3e 70 34  dbeFunc*)pOp->p4
18c85 2e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 20 20  .pVdbeFunc;.    
18c86 63 74 78 2e 70 46 75 6e 63 20 3d 20 63 74 78 2e  ctx.pFunc = ctx.
18c87 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63  pVdbeFunc->pFunc
18c88 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  ;.  }..  assert(
18c89 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f   pOp->p3>0 && pO
18c8a 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p3<=p->nMem )
18c8b 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61  ;.  pOut = &p->a
18c8c 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20  Mem[pOp->p3];.  
18c8d 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45  ctx.s.flags = ME
18c8e 4d 5f 4e 75 6c 6c 3b 0a 20 20 63 74 78 2e 73 2e  M_Null;.  ctx.s.
18c8f 64 62 20 3d 20 64 62 3b 0a 20 20 63 74 78 2e 73  db = db;.  ctx.s
18c90 2e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 63 74 78  .xDel = 0;.  ctx
18c91 2e 73 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a  .s.zMalloc = 0;.
18c92 0a 20 20 2f 2a 20 54 68 65 20 6f 75 74 70 75 74  .  /* The output
18c93 20 63 65 6c 6c 20 6d 61 79 20 61 6c 72 65 61 64   cell may alread
18c94 79 20 68 61 76 65 20 61 20 62 75 66 66 65 72 20  y have a buffer 
18c95 61 6c 6c 6f 63 61 74 65 64 2e 20 4d 6f 76 65 0a  allocated. Move.
18c96 20 20 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72    ** the pointer
18c97 20 74 6f 20 63 74 78 2e 73 20 73 6f 20 69 6e 20   to ctx.s so in 
18c98 63 61 73 65 20 74 68 65 20 75 73 65 72 2d 66 75  case the user-fu
18c99 6e 63 74 69 6f 6e 20 63 61 6e 20 75 73 65 0a 20  nction can use. 
18c9a 20 2a 2a 20 74 68 65 20 61 6c 72 65 61 64 79 20   ** the already 
18c9b 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72  allocated buffer
18c9c 20 69 6e 73 74 65 61 64 20 6f 66 20 61 6c 6c 6f   instead of allo
18c9d 63 61 74 69 6e 67 20 61 20 6e 65 77 20 6f 6e 65  cating a new one
18c9e 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
18c9f 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 63 74 78  VdbeMemMove(&ctx
18ca0 2e 73 2c 20 70 4f 75 74 29 3b 0a 20 20 4d 65 6d  .s, pOut);.  Mem
18ca1 53 65 74 54 79 70 65 46 6c 61 67 28 26 63 74 78  SetTypeFlag(&ctx
18ca2 2e 73 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a  .s, MEM_Null);..
18ca3 20 20 63 74 78 2e 69 73 45 72 72 6f 72 20 3d 20    ctx.isError = 
18ca4 30 3b 0a 20 20 69 66 28 20 63 74 78 2e 70 46 75  0;.  if( ctx.pFu
18ca5 6e 63 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  nc->flags & SQLI
18ca6 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c  TE_FUNC_NEEDCOLL
18ca7 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
18ca8 70 4f 70 3e 70 2d 3e 61 4f 70 20 29 3b 0a 20 20  pOp>p->aOp );.  
18ca9 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31    assert( pOp[-1
18caa 5d 2e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c  ].p4type==P4_COL
18cab 4c 53 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65  LSEQ );.    asse
18cac 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f  rt( pOp[-1].opco
18cad 64 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29  de==OP_CollSeq )
18cae 3b 0a 20 20 20 20 63 74 78 2e 70 43 6f 6c 6c 20  ;.    ctx.pColl 
18caf 3d 20 70 4f 70 5b 2d 31 5d 2e 70 34 2e 70 43 6f  = pOp[-1].p4.pCo
18cb0 6c 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71  ll;.  }.  if( sq
18cb1 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
18cb2 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  b) ) goto abort_
18cb3 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20  due_to_misuse;. 
18cb4 20 28 2a 63 74 78 2e 70 46 75 6e 63 2d 3e 78 46   (*ctx.pFunc->xF
18cb5 75 6e 63 29 28 26 63 74 78 2c 20 6e 2c 20 61 70  unc)(&ctx, n, ap
18cb6 56 61 6c 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  Val);.  if( sqli
18cb7 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20  te3SafetyOn(db) 
18cb8 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
18cb9 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 63 74  beMemRelease(&ct
18cba 78 2e 73 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61  x.s);.    goto a
18cbb 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75  bort_due_to_misu
18cbc 73 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62  se;.  }.  if( db
18cbd 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
18cbe 7b 0a 20 20 20 20 2f 2a 20 45 76 65 6e 20 74 68  {.    /* Even th
18cbf 6f 75 67 68 20 61 20 6d 61 6c 6c 6f 63 28 29 20  ough a malloc() 
18cc0 68 61 73 20 66 61 69 6c 65 64 2c 20 74 68 65 20  has failed, the 
18cc1 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
18cc2 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 75 73 65  f the.    ** use
18cc3 72 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 68  r function may h
18cc4 61 76 65 20 63 61 6c 6c 65 64 20 61 6e 20 73 71  ave called an sq
18cc5 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 58 58 58  lite3_result_XXX
18cc6 28 29 20 66 75 6e 63 74 69 6f 6e 0a 20 20 20 20  () function.    
18cc7 2a 2a 20 74 6f 20 72 65 74 75 72 6e 20 61 20 76  ** to return a v
18cc8 61 6c 75 65 2e 20 54 68 65 20 66 6f 6c 6c 6f 77  alue. The follow
18cc9 69 6e 67 20 63 61 6c 6c 20 72 65 6c 65 61 73 65  ing call release
18cca 73 20 61 6e 79 20 72 65 73 6f 75 72 63 65 73 0a  s any resources.
18ccb 20 20 20 20 2a 2a 20 61 73 73 6f 63 69 61 74 65      ** associate
18ccc 64 20 77 69 74 68 20 73 75 63 68 20 61 20 76 61  d with such a va
18ccd 6c 75 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  lue..    **.    
18cce 2a 2a 20 4e 6f 74 65 3a 20 4d 61 79 62 65 20 4d  ** Note: Maybe M
18ccf 65 6d 52 65 6c 65 61 73 65 28 29 20 73 68 6f 75  emRelease() shou
18cd0 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 69 66 20  ld be called if 
18cd1 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
18cd2 29 0a 20 20 20 20 2a 2a 20 66 61 69 6c 73 20 61  ).    ** fails a
18cd3 6c 73 6f 20 28 74 68 65 20 69 66 28 2e 2e 2e 29  lso (the if(...)
18cd4 20 73 74 61 74 65 6d 65 6e 74 20 61 62 6f 76 65   statement above
18cd5 29 2e 20 42 75 74 20 69 66 20 70 65 6f 70 6c 65  ). But if people
18cd6 20 61 72 65 0a 20 20 20 20 2a 2a 20 6d 69 73 75   are.    ** misu
18cd7 73 69 6e 67 20 73 71 6c 69 74 65 2c 20 74 68 65  sing sqlite, the
18cd8 79 20 68 61 76 65 20 62 69 67 67 65 72 20 70 72  y have bigger pr
18cd9 6f 62 6c 65 6d 73 20 74 68 61 6e 20 61 20 6c 65  oblems than a le
18cda 61 6b 65 64 20 76 61 6c 75 65 2e 0a 20 20 20 20  aked value..    
18cdb 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  */.    sqlite3Vd
18cdc 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 63 74  beMemRelease(&ct
18cdd 78 2e 73 29 3b 0a 20 20 20 20 67 6f 74 6f 20 6e  x.s);.    goto n
18cde 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  o_mem;.  }..  /*
18cdf 20 49 66 20 61 6e 79 20 61 75 78 69 6c 69 61 72   If any auxiliar
18ce0 79 20 64 61 74 61 20 66 75 6e 63 74 69 6f 6e 73  y data functions
18ce1 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c 6c 65   have been calle
18ce2 64 20 62 79 20 74 68 69 73 20 75 73 65 72 20 66  d by this user f
18ce3 75 6e 63 74 69 6f 6e 2c 0a 20 20 2a 2a 20 69 6d  unction,.  ** im
18ce4 6d 65 64 69 61 74 65 6c 79 20 63 61 6c 6c 20 74  mediately call t
18ce5 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f  he destructor fo
18ce6 72 20 61 6e 79 20 6e 6f 6e 2d 73 74 61 74 69 63  r any non-static
18ce7 20 76 61 6c 75 65 73 2e 0a 20 20 2a 2f 0a 20 20   values..  */.  
18ce8 69 66 28 20 63 74 78 2e 70 56 64 62 65 46 75 6e  if( ctx.pVdbeFun
18ce9 63 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  c ){.    sqlite3
18cea 56 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 74  VdbeDeleteAuxDat
18ceb 61 28 63 74 78 2e 70 56 64 62 65 46 75 6e 63 2c  a(ctx.pVdbeFunc,
18cec 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 20 20 70   pOp->p1);.    p
18ced 4f 70 2d 3e 70 34 2e 70 56 64 62 65 46 75 6e 63  Op->p4.pVdbeFunc
18cee 20 3d 20 63 74 78 2e 70 56 64 62 65 46 75 6e 63   = ctx.pVdbeFunc
18cef 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70  ;.    pOp->p4typ
18cf0 65 20 3d 20 50 34 5f 56 44 42 45 46 55 4e 43 3b  e = P4_VDBEFUNC;
18cf1 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68  .  }..  /* If th
18cf2 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  e function retur
18cf3 6e 65 64 20 61 6e 20 65 72 72 6f 72 2c 20 74 68  ned an error, th
18cf4 72 6f 77 20 61 6e 20 65 78 63 65 70 74 69 6f 6e  row an exception
18cf5 20 2a 2f 0a 20 20 69 66 28 20 63 74 78 2e 69 73   */.  if( ctx.is
18cf6 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c  Error ){.    sql
18cf7 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70  ite3SetString(&p
18cf8 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22  ->zErrMsg, db, "
18cf9 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  %s", sqlite3_val
18cfa 75 65 5f 74 65 78 74 28 26 63 74 78 2e 73 29 29  ue_text(&ctx.s))
18cfb 3b 0a 20 20 20 20 72 63 20 3d 20 63 74 78 2e 69  ;.    rc = ctx.i
18cfc 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f  sError;.  }..  /
18cfd 2a 20 43 6f 70 79 20 74 68 65 20 72 65 73 75 6c  * Copy the resul
18cfe 74 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f  t of the functio
18cff 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20  n into register 
18d00 50 33 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  P3 */.  sqlite3V
18d01 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e  dbeChangeEncodin
18d02 67 28 26 63 74 78 2e 73 2c 20 65 6e 63 6f 64 69  g(&ctx.s, encodi
18d03 6e 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ng);.  sqlite3Vd
18d04 62 65 4d 65 6d 4d 6f 76 65 28 70 4f 75 74 2c 20  beMemMove(pOut, 
18d05 26 63 74 78 2e 73 29 3b 0a 20 20 69 66 28 20 73  &ctx.s);.  if( s
18d06 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f  qlite3VdbeMemToo
18d07 42 69 67 28 70 4f 75 74 29 20 29 7b 0a 20 20 20  Big(pOut) ){.   
18d08 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20   goto too_big;. 
18d09 20 7d 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52   }.  REGISTER_TR
18d0a 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 4f 75  ACE(pOp->p3, pOu
18d0b 74 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58  t);.  UPDATE_MAX
18d0c 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b  _BLOBSIZE(pOut);
18d0d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
18d0e 4f 70 63 6f 64 65 3a 20 42 69 74 41 6e 64 20 50  Opcode: BitAnd P
18d0f 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
18d10 2a 20 54 61 6b 65 20 74 68 65 20 62 69 74 2d 77  * Take the bit-w
18d11 69 73 65 20 41 4e 44 20 6f 66 20 74 68 65 20 76  ise AND of the v
18d12 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65  alues in registe
18d13 72 20 50 31 20 61 6e 64 20 50 32 20 61 6e 64 0a  r P1 and P2 and.
18d14 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 72 65 73  ** store the res
18d15 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20  ult in register 
18d16 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72  P3..** If either
18d17 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20   input is NULL, 
18d18 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55  the result is NU
18d19 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  LL..*/./* Opcode
18d1a 3a 20 42 69 74 4f 72 20 50 31 20 50 32 20 50 33  : BitOr P1 P2 P3
18d1b 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20   * *.**.** Take 
18d1c 74 68 65 20 62 69 74 2d 77 69 73 65 20 4f 52 20  the bit-wise OR 
18d1d 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e  of the values in
18d1e 20 72 65 67 69 73 74 65 72 20 50 31 20 61 6e 64   register P1 and
18d1f 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74 6f 72 65   P2 and.** store
18d20 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72   the result in r
18d21 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49  egister P3..** I
18d22 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69  f either input i
18d23 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75  s NULL, the resu
18d24 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f  lt is NULL..*/./
18d25 2a 20 4f 70 63 6f 64 65 3a 20 53 68 69 66 74 4c  * Opcode: ShiftL
18d26 65 66 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a  eft P1 P2 P3 * *
18d27 0a 2a 2a 0a 2a 2a 20 53 68 69 66 74 20 74 68 65  .**.** Shift the
18d28 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69   integer value i
18d29 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 74 6f  n register P2 to
18d2a 20 74 68 65 20 6c 65 66 74 20 62 79 20 74 68 65   the left by the
18d2b 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 69  .** number of bi
18d2c 74 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20  ts specified by 
18d2d 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e 20 72  the integer in r
18d2e 65 67 69 73 65 72 20 50 31 2e 0a 2a 2a 20 53 74  egiser P1..** St
18d2f 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69  ore the result i
18d30 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  n register P3..*
18d31 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75  * If either inpu
18d32 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72  t is NULL, the r
18d33 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a  esult is NULL..*
18d34 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 68 69  /./* Opcode: Shi
18d35 66 74 52 69 67 68 74 20 50 31 20 50 32 20 50 33  ftRight P1 P2 P3
18d36 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 68 69 66 74   * *.**.** Shift
18d37 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c   the integer val
18d38 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
18d39 32 20 74 6f 20 74 68 65 20 72 69 67 68 74 20 62  2 to the right b
18d3a 79 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20  y the.** number 
18d3b 6f 66 20 62 69 74 73 20 73 70 65 63 69 66 69 65  of bits specifie
18d3c 64 20 62 79 20 74 68 65 20 69 6e 74 65 67 65 72  d by the integer
18d3d 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e   in register P1.
18d3e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 72 65  .** Store the re
18d3f 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72  sult in register
18d40 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65   P3..** If eithe
18d41 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c  r input is NULL,
18d42 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e   the result is N
18d43 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ULL..*/.case OP_
18d44 42 69 74 41 6e 64 3a 20 20 20 20 20 20 20 20 20  BitAnd:         
18d45 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
18d46 61 73 20 54 4b 5f 42 49 54 41 4e 44 2c 20 69 6e  as TK_BITAND, in
18d47 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a  1, in2, out3 */.
18d48 63 61 73 65 20 4f 50 5f 42 69 74 4f 72 3a 20 20  case OP_BitOr:  
18d49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18d4a 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49  /* same as TK_BI
18d4b 54 4f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f  TOR, in1, in2, o
18d4c 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  ut3 */.case OP_S
18d4d 68 69 66 74 4c 65 66 74 3a 20 20 20 20 20 20 20  hiftLeft:       
18d4e 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
18d4f 73 20 54 4b 5f 4c 53 48 49 46 54 2c 20 69 6e 31  s TK_LSHIFT, in1
18d50 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63  , in2, out3 */.c
18d51 61 73 65 20 4f 50 5f 53 68 69 66 74 52 69 67 68  ase OP_ShiftRigh
18d52 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f  t: {           /
18d53 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 53 48  * same as TK_RSH
18d54 49 46 54 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f  IFT, in1, in2, o
18d55 75 74 33 20 2a 2f 0a 20 20 69 36 34 20 61 2c 20  ut3 */.  i64 a, 
18d56 62 3b 0a 0a 20 20 69 66 28 20 28 70 49 6e 31 2d  b;..  if( (pIn1-
18d57 3e 66 6c 61 67 73 20 7c 20 70 49 6e 32 2d 3e 66  >flags | pIn2->f
18d58 6c 61 67 73 29 20 26 20 4d 45 4d 5f 4e 75 6c 6c  lags) & MEM_Null
18d59 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
18d5a 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f  dbeMemSetNull(pO
18d5b 75 74 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a  ut);.    break;.
18d5c 20 20 7d 0a 20 20 61 20 3d 20 73 71 6c 69 74 65    }.  a = sqlite
18d5d 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49  3VdbeIntValue(pI
18d5e 6e 32 29 3b 0a 20 20 62 20 3d 20 73 71 6c 69 74  n2);.  b = sqlit
18d5f 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70  e3VdbeIntValue(p
18d60 49 6e 31 29 3b 0a 20 20 73 77 69 74 63 68 28 20  In1);.  switch( 
18d61 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20  pOp->opcode ){. 
18d62 20 20 20 63 61 73 65 20 4f 50 5f 42 69 74 41 6e     case OP_BitAn
18d63 64 3a 20 20 20 20 20 20 61 20 26 3d 20 62 3b 20  d:      a &= b; 
18d64 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
18d65 61 73 65 20 4f 50 5f 42 69 74 4f 72 3a 20 20 20  ase OP_BitOr:   
18d66 20 20 20 20 61 20 7c 3d 20 62 3b 20 20 20 20 20      a |= b;     
18d67 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
18d68 4f 50 5f 53 68 69 66 74 4c 65 66 74 3a 20 20 20  OP_ShiftLeft:   
18d69 61 20 3c 3c 3d 20 62 3b 20 20 20 20 62 72 65 61  a <<= b;    brea
18d6a 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  k;.    default: 
18d6b 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70   assert( pOp->op
18d6c 63 6f 64 65 3d 3d 4f 50 5f 53 68 69 66 74 52 69  code==OP_ShiftRi
18d6d 67 68 74 20 29 3b 0a 20 20 20 20 20 20 20 20 20  ght );.         
18d6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18d6f 61 20 3e 3e 3d 20 62 3b 20 20 20 20 62 72 65 61  a >>= b;    brea
18d70 6b 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 75  k;.  }.  pOut->u
18d71 2e 69 20 3d 20 61 3b 0a 20 20 4d 65 6d 53 65 74  .i = a;.  MemSet
18d72 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d  TypeFlag(pOut, M
18d73 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b  EM_Int);.  break
18d74 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
18d75 41 64 64 49 6d 6d 20 20 50 31 20 50 32 20 2a 20  AddImm  P1 P2 * 
18d76 2a 20 2a 0a 2a 2a 20 0a 2a 2a 20 41 64 64 20 74  * *.** .** Add t
18d77 68 65 20 63 6f 6e 73 74 61 6e 74 20 50 32 20 74  he constant P2 t
18d78 6f 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  o the value in r
18d79 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 20 54  egister P1..** T
18d7a 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c 77  he result is alw
18d7b 61 79 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a  ays an integer..
18d7c 2a 2a 0a 2a 2a 20 54 6f 20 66 6f 72 63 65 20 61  **.** To force a
18d7d 6e 79 20 72 65 67 69 73 74 65 72 20 74 6f 20 62  ny register to b
18d7e 65 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 6a 75  e an integer, ju
18d7f 73 74 20 61 64 64 20 30 2e 0a 2a 2f 0a 63 61 73  st add 0..*/.cas
18d80 65 20 4f 50 5f 41 64 64 49 6d 6d 3a 20 7b 20 20  e OP_AddImm: {  
18d81 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31            /* in1
18d82 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
18d83 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70  eMemIntegerify(p
18d84 49 6e 31 29 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e  In1);.  pIn1->u.
18d85 69 20 2b 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20  i += pOp->p2;.  
18d86 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
18d87 6f 64 65 3a 20 46 6f 72 63 65 49 6e 74 20 50 31  ode: ForceInt P1
18d88 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
18d89 20 43 6f 6e 76 65 72 74 20 76 61 6c 75 65 20 69   Convert value i
18d8a 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69 6e  n register P1 in
18d8b 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20  to an integer.  
18d8c 49 66 20 74 68 65 20 76 61 6c 75 65 20 0a 2a 2a  If the value .**
18d8d 20 69 6e 20 50 31 20 69 73 20 6e 6f 74 20 6e 75   in P1 is not nu
18d8e 6d 65 72 69 63 20 28 6d 65 61 6e 69 6e 67 20 74  meric (meaning t
18d8f 68 61 74 20 69 73 20 69 73 20 61 20 4e 55 4c 4c  hat is is a NULL
18d90 20 6f 72 20 61 20 73 74 72 69 6e 67 20 74 68 61   or a string tha
18d91 74 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 6c 6f  t.** does not lo
18d92 6f 6b 20 6c 69 6b 65 20 61 6e 20 69 6e 74 65 67  ok like an integ
18d93 65 72 20 6f 72 20 66 6c 6f 61 74 69 6e 67 20 70  er or floating p
18d94 6f 69 6e 74 20 6e 75 6d 62 65 72 29 20 74 68 65  oint number) the
18d95 6e 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 50 32 2e  n.** jump to P2.
18d96 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69    If the value i
18d97 6e 20 50 31 20 69 73 20 6e 75 6d 65 72 69 63 20  n P1 is numeric 
18d98 74 68 65 6e 0a 2a 2a 20 63 6f 6e 76 65 72 74 20  then.** convert 
18d99 69 74 20 69 6e 74 6f 20 74 68 65 20 6c 65 61 73  it into the leas
18d9a 74 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 69  t integer that i
18d9b 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f  s greater than o
18d9c 72 20 65 71 75 61 6c 20 74 6f 20 69 74 73 0a 2a  r equal to its.*
18d9d 2a 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20  * current value 
18d9e 69 66 20 50 33 3d 3d 30 2c 20 6f 72 20 74 6f 20  if P3==0, or to 
18d9f 74 68 65 20 6c 65 61 73 74 20 69 6e 74 65 67 65  the least intege
18da0 72 20 74 68 61 74 20 69 73 20 73 74 72 69 63 74  r that is strict
18da1 6c 79 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68  ly.** greater th
18da2 61 6e 20 69 74 73 20 63 75 72 72 65 6e 74 20 76  an its current v
18da3 61 6c 75 65 20 69 66 20 50 33 3d 3d 31 2e 0a 2a  alue if P3==1..*
18da4 2f 0a 63 61 73 65 20 4f 50 5f 46 6f 72 63 65 49  /.case OP_ForceI
18da5 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  nt: {           
18da6 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f   /* jump, in1 */
18da7 0a 20 20 69 36 34 20 76 3b 0a 20 20 61 70 70 6c  .  i64 v;.  appl
18da8 79 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20  yAffinity(pIn1, 
18da9 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52  SQLITE_AFF_NUMER
18daa 49 43 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20  IC, encoding);. 
18dab 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67   if( (pIn1->flag
18dac 73 20 26 20 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d  s & (MEM_Int|MEM
18dad 5f 52 65 61 6c 29 29 3d 3d 30 20 29 7b 0a 20 20  _Real))==0 ){.  
18dae 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
18daf 20 31 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20   1;.    break;. 
18db0 20 7d 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66   }.  if( pIn1->f
18db1 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29  lags & MEM_Int )
18db2 7b 0a 20 20 20 20 76 20 3d 20 70 49 6e 31 2d 3e  {.    v = pIn1->
18db3 75 2e 69 20 2b 20 28 70 4f 70 2d 3e 70 33 21 3d  u.i + (pOp->p3!=
18db4 30 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  0);.  }else{.   
18db5 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66   assert( pIn1->f
18db6 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20  lags & MEM_Real 
18db7 29 3b 0a 20 20 20 20 76 20 3d 20 28 73 71 6c 69  );.    v = (sqli
18db8 74 65 33 5f 69 6e 74 36 34 29 70 49 6e 31 2d 3e  te3_int64)pIn1->
18db9 72 3b 0a 20 20 20 20 69 66 28 20 70 49 6e 31 2d  r;.    if( pIn1-
18dba 3e 72 3e 28 64 6f 75 62 6c 65 29 76 20 29 20 76  >r>(double)v ) v
18dbb 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d  ++;.    if( pOp-
18dbc 3e 70 33 20 26 26 20 70 49 6e 31 2d 3e 72 3d 3d  >p3 && pIn1->r==
18dbd 28 64 6f 75 62 6c 65 29 76 20 29 20 76 2b 2b 3b  (double)v ) v++;
18dbe 0a 20 20 7d 0a 20 20 70 49 6e 31 2d 3e 75 2e 69  .  }.  pIn1->u.i
18dbf 20 3d 20 76 3b 0a 20 20 4d 65 6d 53 65 74 54 79   = v;.  MemSetTy
18dc0 70 65 46 6c 61 67 28 70 49 6e 31 2c 20 4d 45 4d  peFlag(pIn1, MEM
18dc1 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  _Int);.  break;.
18dc2 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 75  }../* Opcode: Mu
18dc3 73 74 42 65 49 6e 74 20 50 31 20 50 32 20 2a 20  stBeInt P1 P2 * 
18dc4 2a 20 2a 0a 2a 2a 20 0a 2a 2a 20 46 6f 72 63 65  * *.** .** Force
18dc5 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
18dc6 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20  gister P1 to be 
18dc7 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 49 66 20  an integer.  If 
18dc8 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20  the value.** in 
18dc9 50 31 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74  P1 is not an int
18dca 65 67 65 72 20 61 6e 64 20 63 61 6e 6e 6f 74 20  eger and cannot 
18dcb 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74  be converted int
18dcc 6f 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20  o an integer.** 
18dcd 77 69 74 68 6f 75 74 20 64 61 74 61 20 6c 6f 73  without data los
18dce 73 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d  s, then jump imm
18dcf 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2c 20  ediately to P2, 
18dd0 6f 72 20 69 66 20 50 32 3d 3d 30 0a 2a 2a 20 72  or if P2==0.** r
18dd1 61 69 73 65 20 61 6e 20 53 51 4c 49 54 45 5f 4d  aise an SQLITE_M
18dd2 49 53 4d 41 54 43 48 20 65 78 63 65 70 74 69 6f  ISMATCH exceptio
18dd3 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 75  n..*/.case OP_Mu
18dd4 73 74 42 65 49 6e 74 3a 20 7b 20 20 20 20 20 20  stBeInt: {      
18dd5 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69        /* jump, i
18dd6 6e 31 20 2a 2f 0a 20 20 61 70 70 6c 79 41 66 66  n1 */.  applyAff
18dd7 69 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 4c 49  inity(pIn1, SQLI
18dd8 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 2c 20  TE_AFF_NUMERIC, 
18dd9 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 69 66 28  encoding);.  if(
18dda 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20   (pIn1->flags & 
18ddb 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20  MEM_Int)==0 ){. 
18ddc 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d     if( pOp->p2==
18ddd 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
18dde 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 3b  SQLITE_MISMATCH;
18ddf 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72  .      goto abor
18de0 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
18de1 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
18de2 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
18de3 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  1;.    }.  }else
18de4 7b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65  {.    MemSetType
18de5 46 6c 61 67 28 70 49 6e 31 2c 20 4d 45 4d 5f 49  Flag(pIn1, MEM_I
18de6 6e 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  nt);.  }.  break
18de7 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
18de8 52 65 61 6c 41 66 66 69 6e 69 74 79 20 50 31 20  RealAffinity P1 
18de9 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66  * * * *.**.** If
18dea 20 72 65 67 69 73 74 65 72 20 50 31 20 68 6f 6c   register P1 hol
18deb 64 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f  ds an integer co
18dec 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 72 65  nvert it to a re
18ded 61 6c 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20  al value..**.** 
18dee 54 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 75  This opcode is u
18def 73 65 64 20 77 68 65 6e 20 65 78 74 72 61 63 74  sed when extract
18df0 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ing information 
18df1 66 72 6f 6d 20 61 20 63 6f 6c 75 6d 6e 20 74 68  from a column th
18df2 61 74 0a 2a 2a 20 68 61 73 20 52 45 41 4c 20 61  at.** has REAL a
18df3 66 66 69 6e 69 74 79 2e 20 20 53 75 63 68 20 63  ffinity.  Such c
18df4 6f 6c 75 6d 6e 20 76 61 6c 75 65 73 20 6d 61 79  olumn values may
18df5 20 73 74 69 6c 6c 20 62 65 20 73 74 6f 72 65 64   still be stored
18df6 20 61 73 0a 2a 2a 20 69 6e 74 65 67 65 72 73 2c   as.** integers,
18df7 20 66 6f 72 20 73 70 61 63 65 20 65 66 66 69 63   for space effic
18df8 69 65 6e 63 79 2c 20 62 75 74 20 61 66 74 65 72  iency, but after
18df9 20 65 78 74 72 61 63 74 69 6f 6e 20 77 65 20 77   extraction we w
18dfa 61 6e 74 20 74 68 65 6d 0a 2a 2a 20 74 6f 20 68  ant them.** to h
18dfb 61 76 65 20 6f 6e 6c 79 20 61 20 72 65 61 6c 20  ave only a real 
18dfc 76 61 6c 75 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f  value..*/.case O
18dfd 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 3a 20  P_RealAffinity: 
18dfe 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
18dff 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 69     /* in1 */.  i
18e00 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  f( pIn1->flags &
18e01 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20   MEM_Int ){.    
18e02 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
18e03 61 6c 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d  alify(pIn1);.  }
18e04 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66  .  break;.}..#if
18e05 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
18e06 5f 43 41 53 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a  _CAST./* Opcode:
18e07 20 54 6f 54 65 78 74 20 50 31 20 2a 20 2a 20 2a   ToText P1 * * *
18e08 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74   *.**.** Force t
18e09 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
18e0a 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 74 65  ster P1 to be te
18e0b 78 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61  xt..** If the va
18e0c 6c 75 65 20 69 73 20 6e 75 6d 65 72 69 63 2c 20  lue is numeric, 
18e0d 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20  convert it to a 
18e0e 73 74 72 69 6e 67 20 75 73 69 6e 67 20 74 68 65  string using the
18e0f 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f  .** equivalent o
18e10 66 20 70 72 69 6e 74 66 28 29 2e 20 20 42 6c 6f  f printf().  Blo
18e11 62 20 76 61 6c 75 65 73 20 61 72 65 20 75 6e 63  b values are unc
18e12 68 61 6e 67 65 64 20 61 6e 64 0a 2a 2a 20 61 72  hanged and.** ar
18e13 65 20 61 66 74 65 72 77 61 72 64 73 20 73 69 6d  e afterwards sim
18e14 70 6c 79 20 69 6e 74 65 72 70 72 65 74 65 64 20  ply interpreted 
18e15 61 73 20 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 41  as text..**.** A
18e16 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e   NULL value is n
18e17 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68  ot changed by th
18e18 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20  is routine.  It 
18e19 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f  remains NULL..*/
18e1a 0a 63 61 73 65 20 4f 50 5f 54 6f 54 65 78 74 3a  .case OP_ToText:
18e1b 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
18e1c 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
18e1d 4b 5f 54 4f 5f 54 45 58 54 2c 20 69 6e 31 20 2a  K_TO_TEXT, in1 *
18e1e 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c  /.  if( pIn1->fl
18e1f 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29  ags & MEM_Null )
18e20 20 62 72 65 61 6b 3b 0a 20 20 61 73 73 65 72 74   break;.  assert
18e21 28 20 4d 45 4d 5f 53 74 72 3d 3d 28 4d 45 4d 5f  ( MEM_Str==(MEM_
18e22 42 6c 6f 62 3e 3e 33 29 20 29 3b 0a 20 20 70 49  Blob>>3) );.  pI
18e23 6e 31 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 49  n1->flags |= (pI
18e24 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c  n1->flags&MEM_Bl
18e25 6f 62 29 3e 3e 33 3b 0a 20 20 61 70 70 6c 79 41  ob)>>3;.  applyA
18e26 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 53 51  ffinity(pIn1, SQ
18e27 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 65  LITE_AFF_TEXT, e
18e28 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 72 63 20 3d  ncoding);.  rc =
18e29 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31   ExpandBlob(pIn1
18e2a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e  );.  assert( pIn
18e2b 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53  1->flags & MEM_S
18e2c 74 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  tr || db->malloc
18e2d 46 61 69 6c 65 64 20 29 3b 0a 20 20 70 49 6e 31  Failed );.  pIn1
18e2e 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d  ->flags &= ~(MEM
18e2f 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45  _Int|MEM_Real|ME
18e30 4d 5f 42 6c 6f 62 29 3b 0a 20 20 55 50 44 41 54  M_Blob);.  UPDAT
18e31 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70  E_MAX_BLOBSIZE(p
18e32 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  In1);.  break;.}
18e33 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 42  ../* Opcode: ToB
18e34 6c 6f 62 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a  lob P1 * * * *.*
18e35 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76  *.** Force the v
18e36 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
18e37 20 50 31 20 74 6f 20 62 65 20 61 20 42 4c 4f 42   P1 to be a BLOB
18e38 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75  ..** If the valu
18e39 65 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 63 6f  e is numeric, co
18e3a 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 73 74  nvert it to a st
18e3b 72 69 6e 67 20 66 69 72 73 74 2e 0a 2a 2a 20 53  ring first..** S
18e3c 74 72 69 6e 67 73 20 61 72 65 20 73 69 6d 70 6c  trings are simpl
18e3d 79 20 72 65 69 6e 74 65 72 70 72 65 74 65 64 20  y reinterpreted 
18e3e 61 73 20 62 6c 6f 62 73 20 77 69 74 68 20 6e 6f  as blobs with no
18e3f 20 63 68 61 6e 67 65 0a 2a 2a 20 74 6f 20 74 68   change.** to th
18e40 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74  e underlying dat
18e41 61 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20  a..**.** A NULL 
18e42 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61  value is not cha
18e43 6e 67 65 64 20 62 79 20 74 68 69 73 20 72 6f 75  nged by this rou
18e44 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e  tine.  It remain
18e45 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20  s NULL..*/.case 
18e46 4f 50 5f 54 6f 42 6c 6f 62 3a 20 7b 20 20 20 20  OP_ToBlob: {    
18e47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
18e48 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 42   same as TK_TO_B
18e49 4c 4f 42 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66  LOB, in1 */.  if
18e4a 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20  ( pIn1->flags & 
18e4b 4d 45 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b  MEM_Null ) break
18e4c 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66  ;.  if( (pIn1->f
18e4d 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 29  lags & MEM_Blob)
18e4e 3d 3d 30 20 29 7b 0a 20 20 20 20 61 70 70 6c 79  ==0 ){.    apply
18e4f 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 53  Affinity(pIn1, S
18e50 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20  QLITE_AFF_TEXT, 
18e51 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 61  encoding);.    a
18e52 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61  ssert( pIn1->fla
18e53 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 7c 7c 20  gs & MEM_Str || 
18e54 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
18e55 20 29 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 74   );.  }.  MemSet
18e56 54 79 70 65 46 6c 61 67 28 70 49 6e 31 2c 20 4d  TypeFlag(pIn1, M
18e57 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 55 50 44 41  EM_Blob);.  UPDA
18e58 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28  TE_MAX_BLOBSIZE(
18e59 70 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  pIn1);.  break;.
18e5a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f  }../* Opcode: To
18e5b 4e 75 6d 65 72 69 63 20 50 31 20 2a 20 2a 20 2a  Numeric P1 * * *
18e5c 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74   *.**.** Force t
18e5d 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
18e5e 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 6e 75  ster P1 to be nu
18e5f 6d 65 72 69 63 20 28 65 69 74 68 65 72 20 61 6e  meric (either an
18e60 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6f 72 20 61  .** integer or a
18e61 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20   floating-point 
18e62 6e 75 6d 62 65 72 2e 29 0a 2a 2a 20 49 66 20 74  number.).** If t
18e63 68 65 20 76 61 6c 75 65 20 69 73 20 74 65 78 74  he value is text
18e64 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f   or blob, try to
18e65 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61   convert it to a
18e66 6e 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65  n using the.** e
18e67 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f  quivalent of ato
18e68 69 28 29 20 6f 72 20 61 74 6f 66 28 29 20 61 6e  i() or atof() an
18e69 64 20 73 74 6f 72 65 20 30 20 69 66 20 6e 6f 20  d store 0 if no 
18e6a 73 75 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20  such conversion 
18e6b 0a 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 2e  .** is possible.
18e6c 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61  .**.** A NULL va
18e6d 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67  lue is not chang
18e6e 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69  ed by this routi
18e6f 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20  ne.  It remains 
18e70 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  NULL..*/.case OP
18e71 5f 54 6f 4e 75 6d 65 72 69 63 3a 20 7b 20 20 20  _ToNumeric: {   
18e72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
18e73 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f  * same as TK_TO_
18e74 4e 55 4d 45 52 49 43 2c 20 69 6e 31 20 2a 2f 0a  NUMERIC, in1 */.
18e75 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61    if( (pIn1->fla
18e76 67 73 20 26 20 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d  gs & (MEM_Null|M
18e77 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29  EM_Int|MEM_Real)
18e78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  )==0 ){.    sqli
18e79 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69  te3VdbeMemNumeri
18e7a 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20  fy(pIn1);.  }.  
18e7b 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20  break;.}.#endif 
18e7c 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  /* SQLITE_OMIT_C
18e7d 41 53 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64  AST */../* Opcod
18e7e 65 3a 20 54 6f 49 6e 74 20 50 31 20 2a 20 2a 20  e: ToInt P1 * * 
18e7f 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20  * *.**.** Force 
18e80 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
18e81 69 73 74 65 72 20 50 31 20 62 65 20 61 6e 20 69  ister P1 be an i
18e82 6e 74 65 67 65 72 2e 20 20 49 66 0a 2a 2a 20 54  nteger.  If.** T
18e83 68 65 20 76 61 6c 75 65 20 69 73 20 63 75 72 72  he value is curr
18e84 65 6e 74 6c 79 20 61 20 72 65 61 6c 20 6e 75 6d  ently a real num
18e85 62 65 72 2c 20 64 72 6f 70 20 69 74 73 20 66 72  ber, drop its fr
18e86 61 63 74 69 6f 6e 61 6c 20 70 61 72 74 2e 0a 2a  actional part..*
18e87 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69  * If the value i
18e88 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20  s text or blob, 
18e89 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69  try to convert i
18e8a 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20  t to an integer 
18e8b 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75  using the.** equ
18e8c 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69 28  ivalent of atoi(
18e8d 29 20 61 6e 64 20 73 74 6f 72 65 20 30 20 69 66  ) and store 0 if
18e8e 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 73   no such convers
18e8f 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 2e  ion is possible.
18e90 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61  .**.** A NULL va
18e91 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67  lue is not chang
18e92 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69  ed by this routi
18e93 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20  ne.  It remains 
18e94 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  NULL..*/.case OP
18e95 5f 54 6f 49 6e 74 3a 20 7b 20 20 20 20 20 20 20  _ToInt: {       
18e96 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
18e97 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 2c  me as TK_TO_INT,
18e98 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70   in1 */.  if( (p
18e99 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
18e9a 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20  _Null)==0 ){.   
18e9b 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49   sqlite3VdbeMemI
18e9c 6e 74 65 67 65 72 69 66 79 28 70 49 6e 31 29 3b  ntegerify(pIn1);
18e9d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
18e9e 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
18e9f 4f 4d 49 54 5f 43 41 53 54 0a 2f 2a 20 4f 70 63  OMIT_CAST./* Opc
18ea0 6f 64 65 3a 20 54 6f 52 65 61 6c 20 50 31 20 2a  ode: ToReal P1 *
18ea1 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72   * * *.**.** For
18ea2 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  ce the value in 
18ea3 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 62  register P1 to b
18ea4 65 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  e a floating poi
18ea5 6e 74 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 49 66  nt number..** If
18ea6 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 63 75   The value is cu
18ea7 72 72 65 6e 74 6c 79 20 61 6e 20 69 6e 74 65 67  rrently an integ
18ea8 65 72 2c 20 63 6f 6e 76 65 72 74 20 69 74 2e 0a  er, convert it..
18ea9 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20  ** If the value 
18eaa 69 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c  is text or blob,
18eab 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20   try to convert 
18eac 69 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72  it to an integer
18ead 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71   using the.** eq
18eae 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69  uivalent of atoi
18eaf 28 29 20 61 6e 64 20 73 74 6f 72 65 20 30 2e 30  () and store 0.0
18eb0 20 69 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76   if no such conv
18eb1 65 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62  ersion is possib
18eb2 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c  le..**.** A NULL
18eb3 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68   value is not ch
18eb4 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 72 6f  anged by this ro
18eb5 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69  utine.  It remai
18eb6 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65  ns NULL..*/.case
18eb7 20 4f 50 5f 54 6f 52 65 61 6c 3a 20 7b 20 20 20   OP_ToReal: {   
18eb8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
18eb9 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f  * same as TK_TO_
18eba 52 45 41 4c 2c 20 69 6e 31 20 2a 2f 0a 20 20 69  REAL, in1 */.  i
18ebb 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20  f( (pIn1->flags 
18ebc 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29  & MEM_Null)==0 )
18ebd 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
18ebe 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 49 6e 31  eMemRealify(pIn1
18ebf 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  );.  }.  break;.
18ec0 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
18ec1 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a  TE_OMIT_CAST */.
18ec2 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 74 20 50  ./* Opcode: Lt P
18ec3 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a  1 P2 P3 P4 P5.**
18ec4 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20  .** Compare the 
18ec5 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74  values in regist
18ec6 65 72 20 50 31 20 61 6e 64 20 50 33 2e 20 20 49  er P1 and P3.  I
18ec7 66 20 72 65 67 28 50 33 29 3c 72 65 67 28 50 31  f reg(P3)<reg(P1
18ec8 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 74  ) then.** jump t
18ec9 6f 20 61 64 64 72 65 73 73 20 50 32 2e 20 20 0a  o address P2.  .
18eca 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c  **.** If the SQL
18ecb 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 62  ITE_JUMPIFNULL b
18ecc 69 74 20 6f 66 20 50 35 20 69 73 20 73 65 74 20  it of P5 is set 
18ecd 61 6e 64 20 65 69 74 68 65 72 20 72 65 67 28 50  and either reg(P
18ece 31 29 20 6f 72 0a 2a 2a 20 72 65 67 28 50 33 29  1) or.** reg(P3)
18ecf 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61   is NULL then ta
18ed0 6b 65 20 74 68 65 20 6a 75 6d 70 2e 20 20 49 66  ke the jump.  If
18ed1 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50   the SQLITE_JUMP
18ed2 49 46 4e 55 4c 4c 20 0a 2a 2a 20 62 69 74 20 69  IFNULL .** bit i
18ed3 73 20 63 6c 65 61 72 20 74 68 65 6e 20 66 61 6c  s clear then fal
18ed4 6c 20 74 68 72 75 20 69 66 20 65 69 74 68 65 72  l thru if either
18ed5 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c   operand is NULL
18ed6 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  ..**.** The SQLI
18ed7 54 45 5f 41 46 46 5f 4d 41 53 4b 20 70 6f 72 74  TE_AFF_MASK port
18ed8 69 6f 6e 20 6f 66 20 50 35 20 6d 75 73 74 20 62  ion of P5 must b
18ed9 65 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68  e an affinity ch
18eda 61 72 61 63 74 65 72 20 2d 0a 2a 2a 20 53 51 4c  aracter -.** SQL
18edb 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 53 51  ITE_AFF_TEXT, SQ
18edc 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52  LITE_AFF_INTEGER
18edd 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20  , and so forth. 
18ede 41 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61  An attempt is ma
18edf 64 65 20 0a 2a 2a 20 74 6f 20 63 6f 65 72 63 65  de .** to coerce
18ee0 20 62 6f 74 68 20 69 6e 70 75 74 73 20 61 63 63   both inputs acc
18ee1 6f 72 64 69 6e 67 20 74 6f 20 74 68 69 73 20 61  ording to this a
18ee2 66 66 69 6e 69 74 79 20 62 65 66 6f 72 65 20 74  ffinity before t
18ee3 68 65 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e  he.** comparison
18ee4 20 69 73 20 6d 61 64 65 2e 20 49 66 20 74 68 65   is made. If the
18ee5 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b   SQLITE_AFF_MASK
18ee6 20 69 73 20 30 78 30 30 2c 20 74 68 65 6e 20 6e   is 0x00, then n
18ee7 75 6d 65 72 69 63 0a 2a 2a 20 61 66 66 69 6e 69  umeric.** affini
18ee8 74 79 20 69 73 20 75 73 65 64 2e 20 4e 6f 74 65  ty is used. Note
18ee9 20 74 68 61 74 20 74 68 65 20 61 66 66 69 6e 69   that the affini
18eea 74 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61  ty conversions a
18eeb 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 62 61 63  re stored.** bac
18eec 6b 20 69 6e 74 6f 20 74 68 65 20 69 6e 70 75 74  k into the input
18eed 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e   registers P1 an
18eee 64 20 50 33 2e 20 20 53 6f 20 74 68 69 73 20 6f  d P3.  So this o
18eef 70 63 6f 64 65 20 63 61 6e 20 63 61 75 73 65 0a  pcode can cause.
18ef0 2a 2a 20 70 65 72 73 69 73 74 65 6e 74 20 63 68  ** persistent ch
18ef1 61 6e 67 65 73 20 74 6f 20 72 65 67 69 73 74 65  anges to registe
18ef2 72 73 20 50 31 20 61 6e 64 20 50 33 2e 0a 2a 2a  rs P1 and P3..**
18ef3 0a 2a 2a 20 4f 6e 63 65 20 61 6e 79 20 63 6f 6e  .** Once any con
18ef4 76 65 72 73 69 6f 6e 73 20 68 61 76 65 20 74 61  versions have ta
18ef5 6b 65 6e 20 70 6c 61 63 65 2c 20 61 6e 64 20 6e  ken place, and n
18ef6 65 69 74 68 65 72 20 76 61 6c 75 65 20 69 73 20  either value is 
18ef7 4e 55 4c 4c 2c 20 0a 2a 2a 20 74 68 65 20 76 61  NULL, .** the va
18ef8 6c 75 65 73 20 61 72 65 20 63 6f 6d 70 61 72 65  lues are compare
18ef9 64 2e 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65  d. If both value
18efa 73 20 61 72 65 20 62 6c 6f 62 73 20 74 68 65 6e  s are blobs then
18efb 20 6d 65 6d 63 6d 70 28 29 20 69 73 0a 2a 2a 20   memcmp() is.** 
18efc 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e  used to determin
18efd 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66  e the results of
18efe 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e   the comparison.
18eff 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73    If both values
18f00 0a 2a 2a 20 61 72 65 20 74 65 78 74 2c 20 74 68  .** are text, th
18f01 65 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  en the appropria
18f02 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e  te collating fun
18f03 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20  ction specified 
18f04 69 6e 0a 2a 2a 20 50 34 20 69 73 20 20 75 73 65  in.** P4 is  use
18f05 64 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 70  d to do the comp
18f06 61 72 69 73 6f 6e 2e 20 20 49 66 20 50 34 20 69  arison.  If P4 i
18f07 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20  s not specified 
18f08 74 68 65 6e 0a 2a 2a 20 6d 65 6d 63 6d 70 28 29  then.** memcmp()
18f09 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70   is used to comp
18f0a 61 72 65 20 74 65 78 74 20 73 74 72 69 6e 67 2e  are text string.
18f0b 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73    If both values
18f0c 20 61 72 65 0a 2a 2a 20 6e 75 6d 65 72 69 63 2c   are.** numeric,
18f0d 20 74 68 65 6e 20 61 20 6e 75 6d 65 72 69 63 20   then a numeric 
18f0e 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 75 73  comparison is us
18f0f 65 64 2e 20 49 66 20 74 68 65 20 74 77 6f 20 76  ed. If the two v
18f10 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 6f 66 20  alues.** are of 
18f11 64 69 66 66 65 72 65 6e 74 20 74 79 70 65 73 2c  different types,
18f12 20 74 68 65 6e 20 6e 75 6d 62 65 72 73 20 61 72   then numbers ar
18f13 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73  e considered les
18f14 73 20 74 68 61 6e 0a 2a 2a 20 73 74 72 69 6e 67  s than.** string
18f15 73 20 61 6e 64 20 73 74 72 69 6e 67 73 20 61 72  s and strings ar
18f16 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73  e considered les
18f17 73 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2a  s than blobs..**
18f18 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54  .** If the SQLIT
18f19 45 5f 53 54 4f 52 45 50 32 20 62 69 74 20 6f 66  E_STOREP2 bit of
18f1a 20 50 35 20 69 73 20 73 65 74 2c 20 74 68 65 6e   P5 is set, then
18f1b 20 64 6f 20 6e 6f 74 20 6a 75 6d 70 2e 20 20 49   do not jump.  I
18f1c 6e 73 74 65 61 64 2c 0a 2a 2a 20 73 74 6f 72 65  nstead,.** store
18f1d 20 61 20 62 6f 6f 6c 65 61 6e 20 72 65 73 75 6c   a boolean resul
18f1e 74 20 28 65 69 74 68 65 72 20 30 2c 20 6f 72 20  t (either 0, or 
18f1f 31 2c 20 6f 72 20 4e 55 4c 4c 29 20 69 6e 20 72  1, or NULL) in r
18f20 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 2f  egister P2..*/./
18f21 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 20 50 31 20  * Opcode: Ne P1 
18f22 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a  P2 P3 P4 P5.**.*
18f23 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73  * This works jus
18f24 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70  t like the Lt op
18f25 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 74  code except that
18f26 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b   the jump is tak
18f27 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 6f 70 65  en if.** the ope
18f28 72 61 6e 64 73 20 69 6e 20 72 65 67 69 73 74 65  rands in registe
18f29 72 73 20 50 31 20 61 6e 64 20 50 33 20 61 72 65  rs P1 and P3 are
18f2a 20 6e 6f 74 20 65 71 75 61 6c 2e 20 20 53 65 65   not equal.  See
18f2b 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66   the Lt opcode f
18f2c 6f 72 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c  or.** additional
18f2d 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f   information..*/
18f2e 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 45 71 20 50  ./* Opcode: Eq P
18f2f 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a  1 P2 P3 P4 P5.**
18f30 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a  .** This works j
18f31 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20  ust like the Lt 
18f32 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 68  opcode except th
18f33 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74  at the jump is t
18f34 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 6f  aken if.** the o
18f35 70 65 72 61 6e 64 73 20 69 6e 20 72 65 67 69 73  perands in regis
18f36 74 65 72 73 20 50 31 20 61 6e 64 20 50 33 20 61  ters P1 and P3 a
18f37 72 65 20 65 71 75 61 6c 2e 0a 2a 2a 20 53 65 65  re equal..** See
18f38 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66   the Lt opcode f
18f39 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
18f3a 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a  formation..*/./*
18f3b 20 4f 70 63 6f 64 65 3a 20 4c 65 20 50 31 20 50   Opcode: Le P1 P
18f3c 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a  2 P3 P4 P5.**.**
18f3d 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74   This works just
18f3e 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63   like the Lt opc
18f3f 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 74 20  ode except that 
18f40 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65  the jump is take
18f41 6e 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74  n if.** the cont
18f42 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20  ent of register 
18f43 50 33 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20  P3 is less than 
18f44 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20  or equal to the 
18f45 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65  content of.** re
18f46 67 69 73 74 65 72 20 50 31 2e 20 20 53 65 65 20  gister P1.  See 
18f47 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f  the Lt opcode fo
18f48 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  r additional inf
18f49 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20  ormation..*/./* 
18f4a 4f 70 63 6f 64 65 3a 20 47 74 20 50 31 20 50 32  Opcode: Gt P1 P2
18f4b 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20   P3 P4 P5.**.** 
18f4c 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20  This works just 
18f4d 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f  like the Lt opco
18f4e 64 65 20 65 78 63 65 70 74 20 74 68 61 74 20 74  de except that t
18f4f 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e  he jump is taken
18f50 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65   if.** the conte
18f51 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50  nt of register P
18f52 33 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  3 is greater tha
18f53 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  n the content of
18f54 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31 2e  .** register P1.
18f55 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63    See the Lt opc
18f56 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  ode for addition
18f57 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  al information..
18f58 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 47 65  */./* Opcode: Ge
18f59 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a   P1 P2 P3 P4 P5.
18f5a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73  **.** This works
18f5b 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c   just like the L
18f5c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20  t opcode except 
18f5d 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73  that the jump is
18f5e 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65   taken if.** the
18f5f 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69   content of regi
18f60 73 74 65 72 20 50 33 20 69 73 20 67 72 65 61 74  ster P3 is great
18f61 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  er than or equal
18f62 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20   to the content 
18f63 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50  of.** register P
18f64 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f  1.  See the Lt o
18f65 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69  pcode for additi
18f66 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
18f67 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 45 71 3a  ..*/.case OP_Eq:
18f68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
18f69 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 45 51 2c  * same as TK_EQ,
18f6a 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20   jump, in1, in3 
18f6b 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 3a 20 20  */.case OP_Ne:  
18f6c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
18f6d 73 61 6d 65 20 61 73 20 54 4b 5f 4e 45 2c 20 6a  same as TK_NE, j
18f6e 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f  ump, in1, in3 */
18f6f 0a 63 61 73 65 20 4f 50 5f 4c 74 3a 20 20 20 20  .case OP_Lt:    
18f70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
18f71 6d 65 20 61 73 20 54 4b 5f 4c 54 2c 20 6a 75 6d  me as TK_LT, jum
18f72 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63  p, in1, in3 */.c
18f73 61 73 65 20 4f 50 5f 4c 65 3a 20 20 20 20 20 20  ase OP_Le:      
18f74 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
18f75 20 61 73 20 54 4b 5f 4c 45 2c 20 6a 75 6d 70 2c   as TK_LE, jump,
18f76 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73   in1, in3 */.cas
18f77 65 20 4f 50 5f 47 74 3a 20 20 20 20 20 20 20 20  e OP_Gt:        
18f78 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
18f79 73 20 54 4b 5f 47 54 2c 20 6a 75 6d 70 2c 20 69  s TK_GT, jump, i
18f7a 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20  n1, in3 */.case 
18f7b 4f 50 5f 47 65 3a 20 7b 20 20 20 20 20 20 20 20  OP_Ge: {        
18f7c 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
18f7d 54 4b 5f 47 45 2c 20 6a 75 6d 70 2c 20 69 6e 31  TK_GE, jump, in1
18f7e 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20 66  , in3 */.  int f
18f7f 6c 61 67 73 3b 0a 20 20 69 6e 74 20 72 65 73 3b  lags;.  int res;
18f80 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79  .  char affinity
18f81 3b 0a 0a 20 20 66 6c 61 67 73 20 3d 20 70 49 6e  ;..  flags = pIn
18f82 31 2d 3e 66 6c 61 67 73 7c 70 49 6e 33 2d 3e 66  1->flags|pIn3->f
18f83 6c 61 67 73 3b 0a 0a 20 20 69 66 28 20 66 6c 61  lags;..  if( fla
18f84 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20  gs&MEM_Null ){. 
18f85 20 20 20 2f 2a 20 49 66 20 65 69 74 68 65 72 20     /* If either 
18f86 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20  operand is NULL 
18f87 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20  then the result 
18f88 69 73 20 61 6c 77 61 79 73 20 4e 55 4c 4c 2e 0a  is always NULL..
18f89 20 20 20 20 2a 2a 20 54 68 65 20 6a 75 6d 70 20      ** The jump 
18f8a 69 73 20 74 61 6b 65 6e 20 69 66 20 74 68 65 20  is taken if the 
18f8b 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c  SQLITE_JUMPIFNUL
18f8c 4c 20 62 69 74 20 69 73 20 73 65 74 2e 0a 20 20  L bit is set..  
18f8d 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f 70    */.    if( pOp
18f8e 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 53 54  ->p5 & SQLITE_ST
18f8f 4f 52 45 50 32 20 29 7b 0a 20 20 20 20 20 20 70  OREP2 ){.      p
18f90 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  Out = &p->aMem[p
18f91 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 4d  Op->p2];.      M
18f92 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f  emSetTypeFlag(pO
18f93 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20  ut, MEM_Null);. 
18f94 20 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52       REGISTER_TR
18f95 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75  ACE(pOp->p2, pOu
18f96 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  t);.    }else if
18f97 28 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49  ( pOp->p5 & SQLI
18f98 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 29 7b  TE_JUMPIFNULL ){
18f99 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  .      pc = pOp-
18f9a 3e 70 32 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20  >p2-1;.    }.   
18f9b 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 61   break;.  }..  a
18f9c 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70  ffinity = pOp->p
18f9d 35 20 26 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d  5 & SQLITE_AFF_M
18f9e 41 53 4b 3b 0a 20 20 69 66 28 20 61 66 66 69 6e  ASK;.  if( affin
18f9f 69 74 79 20 29 7b 0a 20 20 20 20 61 70 70 6c 79  ity ){.    apply
18fa0 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 61  Affinity(pIn1, a
18fa1 66 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 69 6e  ffinity, encodin
18fa2 67 29 3b 0a 20 20 20 20 61 70 70 6c 79 41 66 66  g);.    applyAff
18fa3 69 6e 69 74 79 28 70 49 6e 33 2c 20 61 66 66 69  inity(pIn3, affi
18fa4 6e 69 74 79 2c 20 65 6e 63 6f 64 69 6e 67 29 3b  nity, encoding);
18fa5 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
18fa6 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
18fa7 43 4f 4c 4c 53 45 51 20 7c 7c 20 70 4f 70 2d 3e  COLLSEQ || pOp->
18fa8 70 34 2e 70 43 6f 6c 6c 3d 3d 30 20 29 3b 0a 20  p4.pColl==0 );. 
18fa9 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31   ExpandBlob(pIn1
18faa 29 3b 0a 20 20 45 78 70 61 6e 64 42 6c 6f 62 28  );.  ExpandBlob(
18fab 70 49 6e 33 29 3b 0a 20 20 72 65 73 20 3d 20 73  pIn3);.  res = s
18fac 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65  qlite3MemCompare
18fad 28 70 49 6e 33 2c 20 70 49 6e 31 2c 20 70 4f 70  (pIn3, pIn1, pOp
18fae 2d 3e 70 34 2e 70 43 6f 6c 6c 29 3b 0a 20 20 73  ->p4.pColl);.  s
18faf 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f  witch( pOp->opco
18fb0 64 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 4f  de ){.    case O
18fb1 50 5f 45 71 3a 20 20 20 20 72 65 73 20 3d 20 72  P_Eq:    res = r
18fb2 65 73 3d 3d 30 3b 20 20 20 20 20 62 72 65 61 6b  es==0;     break
18fb3 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4e 65  ;.    case OP_Ne
18fb4 3a 20 20 20 20 72 65 73 20 3d 20 72 65 73 21 3d  :    res = res!=
18fb5 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  0;     break;.  
18fb6 20 20 63 61 73 65 20 4f 50 5f 4c 74 3a 20 20 20    case OP_Lt:   
18fb7 20 72 65 73 20 3d 20 72 65 73 3c 30 3b 20 20 20   res = res<0;   
18fb8 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
18fb9 73 65 20 4f 50 5f 4c 65 3a 20 20 20 20 72 65 73  se OP_Le:    res
18fba 20 3d 20 72 65 73 3c 3d 30 3b 20 20 20 20 20 62   = res<=0;     b
18fbb 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f  reak;.    case O
18fbc 50 5f 47 74 3a 20 20 20 20 72 65 73 20 3d 20 72  P_Gt:    res = r
18fbd 65 73 3e 30 3b 20 20 20 20 20 20 62 72 65 61 6b  es>0;      break
18fbe 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20  ;.    default:  
18fbf 20 20 20 20 20 72 65 73 20 3d 20 72 65 73 3e 3d       res = res>=
18fc0 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  0;     break;.  
18fc1 7d 0a 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35  }..  if( pOp->p5
18fc2 20 26 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50   & SQLITE_STOREP
18fc3 32 20 29 7b 0a 20 20 20 20 70 4f 75 74 20 3d 20  2 ){.    pOut = 
18fc4 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32  &p->aMem[pOp->p2
18fc5 5d 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70  ];.    MemSetTyp
18fc6 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f  eFlag(pOut, MEM_
18fc7 49 6e 74 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e  Int);.    pOut->
18fc8 75 2e 69 20 3d 20 72 65 73 3b 0a 20 20 20 20 52  u.i = res;.    R
18fc9 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
18fca 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20  p->p2, pOut);.  
18fcb 7d 65 6c 73 65 20 69 66 28 20 72 65 73 20 29 7b  }else if( res ){
18fcc 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70  .    pc = pOp->p
18fcd 32 2d 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  2-1;.  }.  break
18fce 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
18fcf 50 65 72 6d 75 74 61 74 69 6f 6e 20 2a 20 2a 20  Permutation * * 
18fd0 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 53 65 74  * P4 *.**.** Set
18fd1 20 74 68 65 20 70 65 72 6d 75 61 74 69 6f 6e 20   the permuation 
18fd2 75 73 65 64 20 62 79 20 74 68 65 20 4f 50 5f 43  used by the OP_C
18fd3 6f 6d 70 61 72 65 20 6f 70 65 72 61 74 6f 72 20  ompare operator 
18fd4 74 6f 20 62 65 20 74 68 65 20 61 72 72 61 79 0a  to be the array.
18fd5 2a 2a 20 6f 66 20 69 6e 74 65 67 65 72 73 20 69  ** of integers i
18fd6 6e 20 50 34 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  n P4..**.** The 
18fd7 70 65 72 6d 75 74 61 74 69 6f 6e 20 69 73 20 6f  permutation is o
18fd8 6e 6c 79 20 76 61 6c 69 64 20 75 6e 74 69 6c 20  nly valid until 
18fd9 74 68 65 20 6e 65 78 74 20 4f 50 5f 50 65 72 6d  the next OP_Perm
18fda 75 74 61 74 69 6f 6e 2c 20 4f 50 5f 43 6f 6d 70  utation, OP_Comp
18fdb 61 72 65 2c 0a 2a 2a 20 4f 50 5f 48 61 6c 74 2c  are,.** OP_Halt,
18fdc 20 6f 72 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77   or OP_ResultRow
18fdd 2e 20 20 54 79 70 69 63 61 6c 6c 79 20 74 68 65  .  Typically the
18fde 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 20   OP_Permutation 
18fdf 73 68 6f 75 6c 64 20 6f 63 63 75 72 0a 2a 2a 20  should occur.** 
18fe0 69 6d 6d 65 64 69 61 74 65 6c 79 20 70 72 69 6f  immediately prio
18fe1 72 20 74 6f 20 74 68 65 20 4f 50 5f 43 6f 6d 70  r to the OP_Comp
18fe2 61 72 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  are..*/.case OP_
18fe3 50 65 72 6d 75 74 61 74 69 6f 6e 3a 20 7b 0a 20  Permutation: {. 
18fe4 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34   assert( pOp->p4
18fe5 74 79 70 65 3d 3d 50 34 5f 49 4e 54 41 52 52 41  type==P4_INTARRA
18fe6 59 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  Y );.  assert( p
18fe7 4f 70 2d 3e 70 34 2e 61 69 20 29 3b 0a 20 20 61  Op->p4.ai );.  a
18fe8 50 65 72 6d 75 74 65 20 3d 20 70 4f 70 2d 3e 70  Permute = pOp->p
18fe9 34 2e 61 69 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  4.ai;.  break;.}
18fea 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6d  ../* Opcode: Com
18feb 70 61 72 65 20 50 31 20 50 32 20 50 33 20 50 34  pare P1 P2 P3 P4
18fec 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65   *.**.** Compare
18fed 20 74 6f 20 76 65 63 74 6f 72 73 20 6f 66 20 72   to vectors of r
18fee 65 67 69 73 74 65 72 73 20 69 6e 20 72 65 67 28  egisters in reg(
18fef 50 31 29 2e 2e 72 65 67 28 50 31 2b 50 33 2d 31  P1)..reg(P1+P3-1
18ff0 29 20 28 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 6f  ) (all this.** o
18ff1 6e 65 20 22 41 22 29 20 61 6e 64 20 69 6e 20 72  ne "A") and in r
18ff2 65 67 28 50 32 29 2e 2e 72 65 67 28 50 32 2b 50  eg(P2)..reg(P2+P
18ff3 33 2d 31 29 20 28 22 42 22 29 2e 20 20 53 61 76  3-1) ("B").  Sav
18ff4 65 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a  e the result of.
18ff5 2a 2a 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  ** the compariso
18ff6 6e 20 66 6f 72 20 75 73 65 20 62 79 20 74 68 65  n for use by the
18ff7 20 6e 65 78 74 20 4f 50 5f 4a 75 6d 70 20 69 6e   next OP_Jump in
18ff8 73 74 72 75 63 74 2e 0a 2a 2a 0a 2a 2a 20 50 34  struct..**.** P4
18ff9 20 69 73 20 61 20 4b 65 79 49 6e 66 6f 20 73 74   is a KeyInfo st
18ffa 72 75 63 74 75 72 65 20 74 68 61 74 20 64 65 66  ructure that def
18ffb 69 6e 65 73 20 63 6f 6c 6c 61 74 69 6e 67 20 73  ines collating s
18ffc 65 71 75 65 6e 63 65 73 20 61 6e 64 20 73 6f 72  equences and sor
18ffd 74 0a 2a 2a 20 6f 72 64 65 72 73 20 66 6f 72 20  t.** orders for 
18ffe 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20  the comparison. 
18fff 20 54 68 65 20 70 65 72 6d 75 74 61 74 69 6f 6e   The permutation
19000 20 61 70 70 6c 69 65 73 20 74 6f 20 72 65 67 69   applies to regi
19001 73 74 65 72 73 0a 2a 2a 20 6f 6e 6c 79 2e 20 20  sters.** only.  
19002 54 68 65 20 4b 65 79 49 6e 66 6f 20 65 6c 65 6d  The KeyInfo elem
19003 65 6e 74 73 20 61 72 65 20 75 73 65 64 20 73 65  ents are used se
19004 71 75 65 6e 74 69 61 6c 6c 79 2e 0a 2a 2a 0a 2a  quentially..**.*
19005 2a 20 54 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  * The comparison
19006 20 69 73 20 61 20 73 6f 72 74 20 63 6f 6d 70 61   is a sort compa
19007 72 69 73 6f 6e 2c 20 73 6f 20 4e 55 4c 4c 73 20  rison, so NULLs 
19008 63 6f 6d 70 61 72 65 20 65 71 75 61 6c 2c 0a 2a  compare equal,.*
19009 2a 20 4e 55 4c 4c 73 20 61 72 65 20 6c 65 73 73  * NULLs are less
1900a 20 74 68 61 6e 20 6e 75 6d 62 65 72 73 2c 20 6e   than numbers, n
1900b 75 6d 62 65 72 73 20 61 72 65 20 6c 65 73 73 20  umbers are less 
1900c 74 68 61 6e 20 73 74 72 69 6e 67 73 2c 0a 2a 2a  than strings,.**
1900d 20 61 6e 64 20 73 74 72 69 6e 67 73 20 61 72 65   and strings are
1900e 20 6c 65 73 73 20 74 68 61 6e 20 62 6c 6f 62 73   less than blobs
1900f 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6d  ..*/.case OP_Com
19010 70 61 72 65 3a 20 7b 0a 20 20 69 6e 74 20 6e 20  pare: {.  int n 
19011 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 69 6e 74  = pOp->p3;.  int
19012 20 69 2c 20 70 31 2c 20 70 32 3b 0a 20 20 63 6f   i, p1, p2;.  co
19013 6e 73 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65  nst KeyInfo *pKe
19014 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e  yInfo = pOp->p4.
19015 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 61 73 73 65  pKeyInfo;.  asse
19016 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73  rt( n>0 );.  ass
19017 65 72 74 28 20 70 4b 65 79 49 6e 66 6f 21 3d 30  ert( pKeyInfo!=0
19018 20 29 3b 0a 20 20 70 31 20 3d 20 70 4f 70 2d 3e   );.  p1 = pOp->
19019 70 31 3b 0a 20 20 61 73 73 65 72 74 28 20 70 31  p1;.  assert( p1
1901a 3e 30 20 26 26 20 70 31 2b 6e 2d 31 3c 70 2d 3e  >0 && p1+n-1<p->
1901b 6e 4d 65 6d 20 29 3b 0a 20 20 70 32 20 3d 20 70  nMem );.  p2 = p
1901c 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74  Op->p2;.  assert
1901d 28 20 70 32 3e 30 20 26 26 20 70 32 2b 6e 2d 31  ( p2>0 && p2+n-1
1901e 3c 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 66 6f  <p->nMem );.  fo
1901f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29  r(i=0; i<n; i++)
19020 7b 0a 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20  {.    int idx = 
19021 61 50 65 72 6d 75 74 65 20 3f 20 61 50 65 72 6d  aPermute ? aPerm
19022 75 74 65 5b 69 5d 20 3a 20 69 3b 0a 20 20 20 20  ute[i] : i;.    
19023 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20  CollSeq *pColl; 
19024 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20     /* Collating 
19025 73 65 71 75 65 6e 63 65 20 74 6f 20 75 73 65 20  sequence to use 
19026 6f 6e 20 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a  on this term */.
19027 20 20 20 20 69 6e 74 20 62 52 65 76 3b 20 20 20      int bRev;   
19028 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66         /* True f
19029 6f 72 20 44 45 53 43 45 4e 44 49 4e 47 20 73 6f  or DESCENDING so
1902a 72 74 20 6f 72 64 65 72 20 2a 2f 0a 20 20 20 20  rt order */.    
1902b 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
1902c 31 2b 69 64 78 2c 20 26 70 2d 3e 61 4d 65 6d 5b  1+idx, &p->aMem[
1902d 70 31 2b 69 64 78 5d 29 3b 0a 20 20 20 20 52 45  p1+idx]);.    RE
1902e 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 32 2b  GISTER_TRACE(p2+
1902f 69 64 78 2c 20 26 70 2d 3e 61 4d 65 6d 5b 70 32  idx, &p->aMem[p2
19030 2b 69 64 78 5d 29 3b 0a 20 20 20 20 61 73 73 65  +idx]);.    asse
19031 72 74 28 20 69 3c 70 4b 65 79 49 6e 66 6f 2d 3e  rt( i<pKeyInfo->
19032 6e 46 69 65 6c 64 20 29 3b 0a 20 20 20 20 70 43  nField );.    pC
19033 6f 6c 6c 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e  oll = pKeyInfo->
19034 61 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 62 52  aColl[i];.    bR
19035 65 76 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61  ev = pKeyInfo->a
19036 53 6f 72 74 4f 72 64 65 72 5b 69 5d 3b 0a 20 20  SortOrder[i];.  
19037 20 20 69 43 6f 6d 70 61 72 65 20 3d 20 73 71 6c    iCompare = sql
19038 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 26  ite3MemCompare(&
19039 70 2d 3e 61 4d 65 6d 5b 70 31 2b 69 64 78 5d 2c  p->aMem[p1+idx],
1903a 20 26 70 2d 3e 61 4d 65 6d 5b 70 32 2b 69 64 78   &p->aMem[p2+idx
1903b 5d 2c 20 70 43 6f 6c 6c 29 3b 0a 20 20 20 20 69  ], pColl);.    i
1903c 66 28 20 69 43 6f 6d 70 61 72 65 20 29 7b 0a 20  f( iCompare ){. 
1903d 20 20 20 20 20 69 66 28 20 62 52 65 76 20 29 20       if( bRev ) 
1903e 69 43 6f 6d 70 61 72 65 20 3d 20 2d 69 43 6f 6d  iCompare = -iCom
1903f 70 61 72 65 3b 0a 20 20 20 20 20 20 62 72 65 61  pare;.      brea
19040 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61  k;.    }.  }.  a
19041 50 65 72 6d 75 74 65 20 3d 20 30 3b 0a 20 20 62  Permute = 0;.  b
19042 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
19043 64 65 3a 20 4a 75 6d 70 20 50 31 20 50 32 20 50  de: Jump P1 P2 P
19044 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70  3 * *.**.** Jump
19045 20 74 6f 20 74 68 65 20 69 6e 73 74 72 75 63 74   to the instruct
19046 69 6f 6e 20 61 74 20 61 64 64 72 65 73 73 20 50  ion at address P
19047 31 2c 20 50 32 2c 20 6f 72 20 50 33 20 64 65 70  1, P2, or P3 dep
19048 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65  ending on whethe
19049 72 0a 2a 2a 20 69 6e 20 74 68 65 20 6d 6f 73 74  r.** in the most
1904a 20 72 65 63 65 6e 74 20 4f 50 5f 43 6f 6d 70 61   recent OP_Compa
1904b 72 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74  re instruction t
1904c 68 65 20 50 31 20 76 65 63 74 6f 72 20 77 61 73  he P1 vector was
1904d 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 65 71   less than.** eq
1904e 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74  ual to, or great
1904f 65 72 20 74 68 61 6e 20 74 68 65 20 50 32 20 76  er than the P2 v
19050 65 63 74 6f 72 2c 20 72 65 73 70 65 63 74 69 76  ector, respectiv
19051 65 6c 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ely..*/.case OP_
19052 4a 75 6d 70 3a 20 7b 20 20 20 20 20 20 20 20 20  Jump: {         
19053 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20      /* jump */. 
19054 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3c 30 20   if( iCompare<0 
19055 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  ){.    pc = pOp-
19056 3e 70 31 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65  >p1 - 1;.  }else
19057 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3d 3d 30   if( iCompare==0
19058 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70   ){.    pc = pOp
19059 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73  ->p2 - 1;.  }els
1905a 65 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  e{.    pc = pOp-
1905b 3e 70 33 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62  >p3 - 1;.  }.  b
1905c 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1905d 64 65 3a 20 41 6e 64 20 50 31 20 50 32 20 50 33  de: And P1 P2 P3
1905e 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20   * *.**.** Take 
1905f 74 68 65 20 6c 6f 67 69 63 61 6c 20 41 4e 44 20  the logical AND 
19060 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e  of the values in
19061 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e   registers P1 an
19062 64 20 50 32 20 61 6e 64 0a 2a 2a 20 77 72 69 74  d P2 and.** writ
19063 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74  e the result int
19064 6f 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  o register P3..*
19065 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 50  *.** If either P
19066 31 20 6f 72 20 50 32 20 69 73 20 30 20 28 66 61  1 or P2 is 0 (fa
19067 6c 73 65 29 20 74 68 65 6e 20 74 68 65 20 72 65  lse) then the re
19068 73 75 6c 74 20 69 73 20 30 20 65 76 65 6e 20 69  sult is 0 even i
19069 66 0a 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 69  f.** the other i
1906a 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41  nput is NULL.  A
1906b 20 4e 55 4c 4c 20 61 6e 64 20 74 72 75 65 20 6f   NULL and true o
1906c 72 20 74 77 6f 20 4e 55 4c 4c 73 20 67 69 76 65  r two NULLs give
1906d 0a 2a 2a 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75  .** a NULL outpu
1906e 74 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  t..*/./* Opcode:
1906f 20 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a   Or P1 P2 P3 * *
19070 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20  .**.** Take the 
19071 6c 6f 67 69 63 61 6c 20 4f 52 20 6f 66 20 74 68  logical OR of th
19072 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69  e values in regi
19073 73 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61  ster P1 and P2 a
19074 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20  nd.** store the 
19075 61 6e 73 77 65 72 20 69 6e 20 72 65 67 69 73 74  answer in regist
19076 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  er P3..**.** If 
19077 65 69 74 68 65 72 20 50 31 20 6f 72 20 50 32 20  either P1 or P2 
19078 69 73 20 6e 6f 6e 7a 65 72 6f 20 28 74 72 75 65  is nonzero (true
19079 29 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  ) then the resul
1907a 74 20 69 73 20 31 20 28 74 72 75 65 29 0a 2a 2a  t is 1 (true).**
1907b 20 65 76 65 6e 20 69 66 20 74 68 65 20 6f 74 68   even if the oth
1907c 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c  er input is NULL
1907d 2e 20 20 41 20 4e 55 4c 4c 20 61 6e 64 20 66 61  .  A NULL and fa
1907e 6c 73 65 20 6f 72 20 74 77 6f 20 4e 55 4c 4c 73  lse or two NULLs
1907f 0a 2a 2a 20 67 69 76 65 20 61 20 4e 55 4c 4c 20  .** give a NULL 
19080 6f 75 74 70 75 74 2e 0a 2a 2f 0a 63 61 73 65 20  output..*/.case 
19081 4f 50 5f 41 6e 64 3a 20 20 20 20 20 20 20 20 20  OP_And:         
19082 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
19083 54 4b 5f 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32  TK_AND, in1, in2
19084 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f  , out3 */.case O
19085 50 5f 4f 72 3a 20 7b 20 20 20 20 20 20 20 20 20  P_Or: {         
19086 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
19087 4b 5f 4f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20  K_OR, in1, in2, 
19088 6f 75 74 33 20 2a 2f 0a 20 20 69 6e 74 20 76 31  out3 */.  int v1
19089 2c 20 76 32 3b 20 20 20 20 2f 2a 20 30 3d 3d 46  , v2;    /* 0==F
1908a 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32  ALSE, 1==TRUE, 2
1908b 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c  ==UNKNOWN or NUL
1908c 4c 20 2a 2f 0a 0a 20 20 69 66 28 20 70 49 6e 31  L */..  if( pIn1
1908d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  ->flags & MEM_Nu
1908e 6c 6c 20 29 7b 0a 20 20 20 20 76 31 20 3d 20 32  ll ){.    v1 = 2
1908f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 76  ;.  }else{.    v
19090 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49  1 = sqlite3VdbeI
19091 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30  ntValue(pIn1)!=0
19092 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 6e 32  ;.  }.  if( pIn2
19093 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  ->flags & MEM_Nu
19094 6c 6c 20 29 7b 0a 20 20 20 20 76 32 20 3d 20 32  ll ){.    v2 = 2
19095 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 76  ;.  }else{.    v
19096 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49  2 = sqlite3VdbeI
19097 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 21 3d 30  ntValue(pIn2)!=0
19098 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d  ;.  }.  if( pOp-
19099 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41 6e 64 20  >opcode==OP_And 
1909a 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f  ){.    static co
1909b 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
1909c 72 20 61 6e 64 5f 6c 6f 67 69 63 5b 5d 20 3d 20  r and_logic[] = 
1909d 7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c  { 0, 0, 0, 0, 1,
1909e 20 32 2c 20 30 2c 20 32 2c 20 32 20 7d 3b 0a 20   2, 0, 2, 2 };. 
1909f 20 20 20 76 31 20 3d 20 61 6e 64 5f 6c 6f 67 69     v1 = and_logi
190a0 63 5b 76 31 2a 33 2b 76 32 5d 3b 0a 20 20 7d 65  c[v1*3+v2];.  }e
190a1 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 20  lse{.    static 
190a2 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
190a3 68 61 72 20 6f 72 5f 6c 6f 67 69 63 5b 5d 20 3d  har or_logic[] =
190a4 20 7b 20 30 2c 20 31 2c 20 32 2c 20 31 2c 20 31   { 0, 1, 2, 1, 1
190a5 2c 20 31 2c 20 32 2c 20 31 2c 20 32 20 7d 3b 0a  , 1, 2, 1, 2 };.
190a6 20 20 20 20 76 31 20 3d 20 6f 72 5f 6c 6f 67 69      v1 = or_logi
190a7 63 5b 76 31 2a 33 2b 76 32 5d 3b 0a 20 20 7d 0a  c[v1*3+v2];.  }.
190a8 20 20 69 66 28 20 76 31 3d 3d 32 20 29 7b 0a 20    if( v1==2 ){. 
190a9 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61     MemSetTypeFla
190aa 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c  g(pOut, MEM_Null
190ab 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
190ac 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 76 31 3b 0a  pOut->u.i = v1;.
190ad 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c      MemSetTypeFl
190ae 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74  ag(pOut, MEM_Int
190af 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  );.  }.  break;.
190b0 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f  }../* Opcode: No
190b1 74 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a  t P1 * * * *.**.
190b2 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68 65  ** Interpret the
190b3 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
190b4 65 72 20 50 31 20 61 73 20 61 20 62 6f 6f 6c 65  er P1 as a boole
190b5 61 6e 20 76 61 6c 75 65 2e 20 20 52 65 70 6c 61  an value.  Repla
190b6 63 65 20 69 74 0a 2a 2a 20 77 69 74 68 20 69 74  ce it.** with it
190b7 73 20 63 6f 6d 70 6c 65 6d 65 6e 74 2e 20 20 49  s complement.  I
190b8 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  f the value in r
190b9 65 67 69 73 74 65 72 20 50 31 20 69 73 20 4e 55  egister P1 is NU
190ba 4c 4c 20 69 74 73 20 76 61 6c 75 65 0a 2a 2a 20  LL its value.** 
190bb 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f  is unchanged..*/
190bc 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 3a 20 7b 20  .case OP_Not: { 
190bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
190be 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54  * same as TK_NOT
190bf 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 70  , in1 */.  if( p
190c0 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
190c1 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b 20 20  _Null ) break;  
190c2 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20 74 6f  /* Do nothing to
190c3 20 4e 55 4c 4c 73 20 2a 2f 0a 20 20 73 71 6c 69   NULLs */.  sqli
190c4 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65  te3VdbeMemIntege
190c5 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 70 49  rify(pIn1);.  pI
190c6 6e 31 2d 3e 75 2e 69 20 3d 20 21 70 49 6e 31 2d  n1->u.i = !pIn1-
190c7 3e 75 2e 69 3b 0a 20 20 61 73 73 65 72 74 28 20  >u.i;.  assert( 
190c8 70 49 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pIn1->flags&MEM_
190c9 49 6e 74 20 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  Int );.  break;.
190ca 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69  }../* Opcode: Bi
190cb 74 4e 6f 74 20 50 31 20 2a 20 2a 20 2a 20 2a 0a  tNot P1 * * * *.
190cc 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20  **.** Interpret 
190cd 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72  the content of r
190ce 65 67 69 73 74 65 72 20 50 31 20 61 73 20 61 6e  egister P1 as an
190cf 20 69 6e 74 65 67 65 72 2e 20 20 52 65 70 6c 61   integer.  Repla
190d0 63 65 20 69 74 0a 2a 2a 20 77 69 74 68 20 69 74  ce it.** with it
190d1 73 20 6f 6e 65 73 2d 63 6f 6d 70 6c 65 6d 65 6e  s ones-complemen
190d2 74 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75 65  t.  If the value
190d3 20 69 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 4e   is originally N
190d4 55 4c 4c 2c 20 6c 65 61 76 65 0a 2a 2a 20 69 74  ULL, leave.** it
190d5 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 63   unchanged..*/.c
190d6 61 73 65 20 4f 50 5f 42 69 74 4e 6f 74 3a 20 7b  ase OP_BitNot: {
190d7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
190d8 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f  same as TK_BITNO
190d9 54 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20  T, in1 */.  if( 
190da 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
190db 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b 20  M_Null ) break; 
190dc 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20 74   /* Do nothing t
190dd 6f 20 4e 55 4c 4c 73 20 2a 2f 0a 20 20 73 71 6c  o NULLs */.  sql
190de 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67  ite3VdbeMemInteg
190df 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 70  erify(pIn1);.  p
190e0 49 6e 31 2d 3e 75 2e 69 20 3d 20 7e 70 49 6e 31  In1->u.i = ~pIn1
190e1 2d 3e 75 2e 69 3b 0a 20 20 61 73 73 65 72 74 28  ->u.i;.  assert(
190e2 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d   pIn1->flags&MEM
190e3 5f 49 6e 74 20 29 3b 0a 20 20 62 72 65 61 6b 3b  _Int );.  break;
190e4 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49  .}../* Opcode: I
190e5 66 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  f P1 P2 P3 * *.*
190e6 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20  *.** Jump to P2 
190e7 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  if the value in 
190e8 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 74  register P1 is t
190e9 72 75 65 2e 20 20 54 68 65 20 76 61 6c 75 65 20  rue.  The value 
190ea 69 73 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65  is.** is conside
190eb 72 65 64 20 74 72 75 65 20 69 66 20 69 74 20 69  red true if it i
190ec 73 20 6e 75 6d 65 72 69 63 20 61 6e 64 20 6e 6f  s numeric and no
190ed 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 74 68 65 20  n-zero.  If the 
190ee 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20 69  value.** in P1 i
190ef 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b 65  s NULL then take
190f0 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50 33 20   the jump if P3 
190f1 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 2f 2a 20 4f  is true..*/./* O
190f2 70 63 6f 64 65 3a 20 49 66 4e 6f 74 20 50 31 20  pcode: IfNot P1 
190f3 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  P2 P3 * *.**.** 
190f4 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 74 68  Jump to P2 if th
190f5 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
190f6 74 65 72 20 50 31 20 69 73 20 46 61 6c 73 65 2e  ter P1 is False.
190f7 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 0a 2a    The value is.*
190f8 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  * is considered 
190f9 74 72 75 65 20 69 66 20 69 74 20 68 61 73 20 61  true if it has a
190fa 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20 6f   numeric value o
190fb 66 20 7a 65 72 6f 2e 20 20 49 66 20 74 68 65 20  f zero.  If the 
190fc 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20 69  value.** in P1 i
190fd 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b 65  s NULL then take
190fe 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50 33 20   the jump if P3 
190ff 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 63 61 73 65  is true..*/.case
19100 20 4f 50 5f 49 66 3a 20 20 20 20 20 20 20 20 20   OP_If:         
19101 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c          /* jump,
19102 20 69 6e 31 20 2a 2f 0a 63 61 73 65 20 4f 50 5f   in1 */.case OP_
19103 49 66 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 20  IfNot: {        
19104 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31      /* jump, in1
19105 20 2a 2f 0a 20 20 69 6e 74 20 63 3b 0a 20 20 69   */.  int c;.  i
19106 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  f( pIn1->flags &
19107 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20   MEM_Null ){.   
19108 20 63 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20   c = pOp->p3;.  
19109 7d 65 6c 73 65 7b 0a 23 69 66 64 65 66 20 53 51  }else{.#ifdef SQ
1910a 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
1910b 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 63 20 3d  NG_POINT.    c =
1910c 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56   sqlite3VdbeIntV
1910d 61 6c 75 65 28 70 49 6e 31 29 3b 0a 23 65 6c 73  alue(pIn1);.#els
1910e 65 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65  e.    c = sqlite
1910f 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70  3VdbeRealValue(p
19110 49 6e 31 29 21 3d 30 2e 30 3b 0a 23 65 6e 64 69  In1)!=0.0;.#endi
19111 66 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f  f.    if( pOp->o
19112 70 63 6f 64 65 3d 3d 4f 50 5f 49 66 4e 6f 74 20  pcode==OP_IfNot 
19113 29 20 63 20 3d 20 21 63 3b 0a 20 20 7d 0a 20 20  ) c = !c;.  }.  
19114 69 66 28 20 63 20 29 7b 0a 20 20 20 20 70 63 20  if( c ){.    pc 
19115 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d  = pOp->p2-1;.  }
19116 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
19117 4f 70 63 6f 64 65 3a 20 49 73 4e 75 6c 6c 20 50  Opcode: IsNull P
19118 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
19119 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20  * Jump to P2 if 
1911a 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
1911b 69 73 74 65 72 20 50 31 20 69 73 20 4e 55 4c 4c  ister P1 is NULL
1911c 2e 20 20 49 66 20 50 33 20 69 73 20 67 72 65 61  .  If P3 is grea
1911d 74 65 72 0a 2a 2a 20 74 68 61 6e 20 7a 65 72 6f  ter.** than zero
1911e 2c 20 74 68 65 6e 20 63 68 65 63 6b 20 61 6c 6c  , then check all
1911f 20 76 61 6c 75 65 73 20 72 65 67 28 50 31 29 2c   values reg(P1),
19120 20 72 65 67 28 50 31 2b 31 29 2c 20 0a 2a 2a 20   reg(P1+1), .** 
19121 72 65 67 28 50 31 2b 32 29 2c 20 2e 2e 2e 2c 20  reg(P1+2), ..., 
19122 72 65 67 28 50 31 2b 50 33 2d 31 29 2e 0a 2a 2f  reg(P1+P3-1)..*/
19123 0a 63 61 73 65 20 4f 50 5f 49 73 4e 75 6c 6c 3a  .case OP_IsNull:
19124 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a   {            /*
19125 20 73 61 6d 65 20 61 73 20 54 4b 5f 49 53 4e 55   same as TK_ISNU
19126 4c 4c 2c 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f  LL, jump, in1 */
19127 0a 20 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e  .  int n = pOp->
19128 70 33 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  p3;.  assert( pO
19129 70 2d 3e 70 33 3d 3d 30 20 7c 7c 20 70 4f 70 2d  p->p3==0 || pOp-
1912a 3e 70 31 3e 30 20 29 3b 0a 20 20 64 6f 7b 0a 20  >p1>0 );.  do{. 
1912b 20 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c     if( (pIn1->fl
1912c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21  ags & MEM_Null)!
1912d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d  =0 ){.      pc =
1912e 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
1912f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
19130 0a 20 20 20 20 70 49 6e 31 2b 2b 3b 0a 20 20 7d  .    pIn1++;.  }
19131 77 68 69 6c 65 28 20 2d 2d 6e 20 3e 20 30 20 29  while( --n > 0 )
19132 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
19133 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 4e 75 6c 6c   Opcode: NotNull
19134 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
19135 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66  ** Jump to P2 if
19136 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
19137 67 69 73 74 65 72 20 50 31 20 69 73 20 6e 6f 74  gister P1 is not
19138 20 4e 55 4c 4c 2e 20 20 0a 2a 2f 0a 63 61 73 65   NULL.  .*/.case
19139 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 3a 20 7b 20 20   OP_NotNull: {  
1913a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
1913b 65 20 61 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c  e as TK_NOTNULL,
1913c 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20   jump, in1 */.  
1913d 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73  if( (pIn1->flags
1913e 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20   & MEM_Null)==0 
1913f 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  ){.    pc = pOp-
19140 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62  >p2 - 1;.  }.  b
19141 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
19142 64 65 3a 20 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e  de: SetNumColumn
19143 73 20 2a 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a  s * P2 * * *.**.
19144 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 73  ** This opcode s
19145 65 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ets the number o
19146 66 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 74 68  f columns for th
19147 65 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20  e cursor opened 
19148 62 79 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77  by the.** follow
19149 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ing instruction 
1914a 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20  to P2..**.** An 
1914b 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73  OP_SetNumColumns
1914c 20 69 73 20 6f 6e 6c 79 20 75 73 65 66 75 6c 20   is only useful 
1914d 69 66 20 69 74 20 6f 63 63 75 72 73 20 69 6d 6d  if it occurs imm
1914e 65 64 69 61 74 65 6c 79 20 62 65 66 6f 72 65 20  ediately before 
1914f 0a 2a 2a 20 6f 6e 65 20 6f 66 20 74 68 65 20 66  .** one of the f
19150 6f 6c 6c 6f 77 69 6e 67 20 6f 70 63 6f 64 65 73  ollowing opcodes
19151 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4f 70 65 6e  :.**.**     Open
19152 52 65 61 64 0a 2a 2a 20 20 20 20 20 4f 70 65 6e  Read.**     Open
19153 57 72 69 74 65 0a 2a 2a 20 20 20 20 20 4f 70 65  Write.**     Ope
19154 6e 50 73 65 75 64 6f 0a 2a 2a 0a 2a 2a 20 49 66  nPseudo.**.** If
19155 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f   the OP_Column o
19156 70 63 6f 64 65 20 69 73 20 74 6f 20 62 65 20 65  pcode is to be e
19157 78 65 63 75 74 65 64 20 6f 6e 20 61 20 63 75 72  xecuted on a cur
19158 73 6f 72 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69  sor, then.** thi
19159 73 20 6f 70 63 6f 64 65 20 6d 75 73 74 20 62 65  s opcode must be
1915a 20 70 72 65 73 65 6e 74 20 69 6d 6d 65 64 69 61   present immedia
1915b 74 65 6c 79 20 62 65 66 6f 72 65 20 74 68 65 20  tely before the 
1915c 6f 70 63 6f 64 65 20 74 68 61 74 0a 2a 2a 20 6f  opcode that.** o
1915d 70 65 6e 73 20 74 68 65 20 63 75 72 73 6f 72 2e  pens the cursor.
1915e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 74 4e  .*/.case OP_SetN
1915f 75 6d 43 6f 6c 75 6d 6e 73 3a 20 7b 0a 20 20 62  umColumns: {.  b
19160 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
19161 64 65 3a 20 43 6f 6c 75 6d 6e 20 50 31 20 50 32  de: Column P1 P2
19162 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49   P3 P4 *.**.** I
19163 6e 74 65 72 70 72 65 74 20 74 68 65 20 64 61 74  nterpret the dat
19164 61 20 74 68 61 74 20 63 75 72 73 6f 72 20 50 31  a that cursor P1
19165 20 70 6f 69 6e 74 73 20 74 6f 20 61 73 20 61 20   points to as a 
19166 73 74 72 75 63 74 75 72 65 20 62 75 69 6c 74 20  structure built 
19167 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 4d 61 6b  using.** the Mak
19168 65 52 65 63 6f 72 64 20 69 6e 73 74 72 75 63 74  eRecord instruct
19169 69 6f 6e 2e 20 20 28 53 65 65 20 74 68 65 20 4d  ion.  (See the M
1916a 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65  akeRecord opcode
1916b 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a   for additional.
1916c 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  ** information a
1916d 62 6f 75 74 20 74 68 65 20 66 6f 72 6d 61 74 20  bout the format 
1916e 6f 66 20 74 68 65 20 64 61 74 61 2e 29 20 20 45  of the data.)  E
1916f 78 74 72 61 63 74 20 74 68 65 20 50 32 2d 74 68  xtract the P2-th
19170 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 66 72 6f 6d 20   column.** from 
19171 74 68 69 73 20 72 65 63 6f 72 64 2e 20 20 49 66  this record.  If
19172 20 74 68 65 72 65 20 61 72 65 20 6c 65 73 73 20   there are less 
19173 74 68 61 74 20 28 50 32 2b 31 29 20 0a 2a 2a 20  that (P2+1) .** 
19174 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 72 65  values in the re
19175 63 6f 72 64 2c 20 65 78 74 72 61 63 74 20 61 20  cord, extract a 
19176 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  NULL..**.** The 
19177 76 61 6c 75 65 20 65 78 74 72 61 63 74 65 64 20  value extracted 
19178 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67  is stored in reg
19179 69 73 74 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20  ister P3..**.** 
1917a 49 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 6f  If the column co
1917b 6e 74 61 69 6e 73 20 66 65 77 65 72 20 74 68 61  ntains fewer tha
1917c 6e 20 50 32 20 66 69 65 6c 64 73 2c 20 74 68 65  n P2 fields, the
1917d 6e 20 65 78 74 72 61 63 74 20 61 20 4e 55 4c 4c  n extract a NULL
1917e 2e 20 20 4f 72 2c 0a 2a 2a 20 69 66 20 74 68 65  .  Or,.** if the
1917f 20 50 34 20 61 72 67 75 6d 65 6e 74 20 69 73 20   P4 argument is 
19180 61 20 50 34 5f 4d 45 4d 20 75 73 65 20 74 68 65  a P4_MEM use the
19181 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 34   value of the P4
19182 20 61 72 67 75 6d 65 6e 74 20 61 73 0a 2a 2a 20   argument as.** 
19183 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2f 0a 63  the result..*/.c
19184 61 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e 3a 20 7b  ase OP_Column: {
19185 0a 20 20 75 33 32 20 70 61 79 6c 6f 61 64 53 69  .  u32 payloadSi
19186 7a 65 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  ze;   /* Number 
19187 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20  of bytes in the 
19188 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20  record */.  int 
19189 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 20 2f  p1 = pOp->p1;  /
1918a 2a 20 50 31 20 76 61 6c 75 65 20 6f 66 20 74 68  * P1 value of th
1918b 65 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e  e opcode */.  in
1918c 74 20 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 20  t p2 = pOp->p2; 
1918d 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65   /* column numbe
1918e 72 20 74 6f 20 72 65 74 72 69 65 76 65 20 2a 2f  r to retrieve */
1918f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
19190 43 20 3d 20 30 3b 2f 2a 20 54 68 65 20 56 44 42  C = 0;/* The VDB
19191 45 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 63 68  E cursor */.  ch
19192 61 72 20 2a 7a 52 65 63 3b 20 20 20 20 20 20 20  ar *zRec;       
19193 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 63   /* Pointer to c
19194 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 64 2d 64  omplete record-d
19195 61 74 61 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f  ata */.  BtCurso
19196 72 20 2a 70 43 72 73 72 3b 20 20 20 2f 2a 20 54  r *pCrsr;   /* T
19197 68 65 20 42 54 72 65 65 20 63 75 72 73 6f 72 20  he BTree cursor 
19198 2a 2f 0a 20 20 75 33 32 20 2a 61 54 79 70 65 3b  */.  u32 *aType;
19199 20 20 20 20 20 20 20 20 2f 2a 20 61 54 79 70 65          /* aType
1919a 5b 69 5d 20 68 6f 6c 64 73 20 74 68 65 20 6e 75  [i] holds the nu
1919b 6d 65 72 69 63 20 74 79 70 65 20 6f 66 20 74 68  meric type of th
1919c 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f  e i-th column */
1919d 0a 20 20 75 33 32 20 2a 61 4f 66 66 73 65 74 3b  .  u32 *aOffset;
1919e 20 20 20 20 20 20 2f 2a 20 61 4f 66 66 73 65 74        /* aOffset
1919f 5b 69 5d 20 69 73 20 6f 66 66 73 65 74 20 74 6f  [i] is offset to
191a0 20 73 74 61 72 74 20 6f 66 20 64 61 74 61 20 66   start of data f
191a1 6f 72 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a  or i-th column *
191a2 2f 0a 20 20 75 33 32 20 6e 46 69 65 6c 64 3b 20  /.  u32 nField; 
191a3 20 20 20 20 20 20 20 2f 2a 20 6e 75 6d 62 65 72         /* number
191a4 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68   of fields in th
191a5 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e  e record */.  in
191a6 74 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20  t len;          
191a7 20 2f 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 6f   /* The length o
191a8 66 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64  f the serialized
191a9 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 63 6f   data for the co
191aa 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  lumn */.  int i;
191ab 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
191ac 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
191ad 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b 20 20    char *zData;  
191ae 20 20 20 20 20 2f 2a 20 50 61 72 74 20 6f 66 20       /* Part of 
191af 74 68 65 20 72 65 63 6f 72 64 20 62 65 69 6e 67  the record being
191b0 20 64 65 63 6f 64 65 64 20 2a 2f 0a 20 20 4d 65   decoded */.  Me
191b1 6d 20 2a 70 44 65 73 74 3b 20 20 20 20 20 20 20  m *pDest;       
191b2 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 77 72 69   /* Where to wri
191b3 74 65 20 74 68 65 20 65 78 74 72 61 63 74 65 64  te the extracted
191b4 20 76 61 6c 75 65 20 2a 2f 0a 20 20 4d 65 6d 20   value */.  Mem 
191b5 73 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f  sMem;          /
191b6 2a 20 46 6f 72 20 73 74 6f 72 69 6e 67 20 74 68  * For storing th
191b7 65 20 72 65 63 6f 72 64 20 62 65 69 6e 67 20 64  e record being d
191b8 65 63 6f 64 65 64 20 2a 2f 0a 0a 20 20 73 4d 65  ecoded */..  sMe
191b9 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 73  m.flags = 0;.  s
191ba 4d 65 6d 2e 64 62 20 3d 20 30 3b 0a 20 20 73 4d  Mem.db = 0;.  sM
191bb 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a  em.zMalloc = 0;.
191bc 20 20 61 73 73 65 72 74 28 20 70 31 3c 70 2d 3e    assert( p1<p->
191bd 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73  nCursor );.  ass
191be 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26  ert( pOp->p3>0 &
191bf 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d  & pOp->p3<=p->nM
191c0 65 6d 20 29 3b 0a 20 20 70 44 65 73 74 20 3d 20  em );.  pDest = 
191c1 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33  &p->aMem[pOp->p3
191c2 5d 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46  ];.  MemSetTypeF
191c3 6c 61 67 28 70 44 65 73 74 2c 20 4d 45 4d 5f 4e  lag(pDest, MEM_N
191c4 75 6c 6c 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73  ull);..  /* This
191c5 20 62 6c 6f 63 6b 20 73 65 74 73 20 74 68 65 20   block sets the 
191c6 76 61 72 69 61 62 6c 65 20 70 61 79 6c 6f 61 64  variable payload
191c7 53 69 7a 65 20 74 6f 20 62 65 20 74 68 65 20 74  Size to be the t
191c8 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 0a 20  otal number of. 
191c9 20 2a 2a 20 62 79 74 65 73 20 69 6e 20 74 68 65   ** bytes in the
191ca 20 72 65 63 6f 72 64 2e 0a 20 20 2a 2a 0a 20 20   record..  **.  
191cb 2a 2a 20 7a 52 65 63 20 69 73 20 73 65 74 20 74  ** zRec is set t
191cc 6f 20 62 65 20 74 68 65 20 63 6f 6d 70 6c 65 74  o be the complet
191cd 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 72 65  e text of the re
191ce 63 6f 72 64 20 69 66 20 69 74 20 69 73 20 61 76  cord if it is av
191cf 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2a 20 54 68  ailable..  ** Th
191d0 65 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72  e complete recor
191d1 64 20 74 65 78 74 20 69 73 20 61 6c 77 61 79 73  d text is always
191d2 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 70   available for p
191d3 73 65 75 64 6f 2d 74 61 62 6c 65 73 0a 20 20 2a  seudo-tables.  *
191d4 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20  * If the record 
191d5 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 63  is stored in a c
191d6 75 72 73 6f 72 2c 20 74 68 65 20 63 6f 6d 70 6c  ursor, the compl
191d7 65 74 65 20 72 65 63 6f 72 64 20 74 65 78 74 0a  ete record text.
191d8 20 20 2a 2a 20 6d 69 67 68 74 20 62 65 20 61 76    ** might be av
191d9 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 20  ailable in the  
191da 70 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20  pC->aRow cache. 
191db 20 4f 72 20 69 74 20 6d 69 67 68 74 20 6e 6f 74   Or it might not
191dc 20 62 65 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65   be..  ** If the
191dd 20 64 61 74 61 20 69 73 20 75 6e 61 76 61 69 6c   data is unavail
191de 61 62 6c 65 2c 20 20 7a 52 65 63 20 69 73 20 73  able,  zRec is s
191df 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 2a 2a  et to NULL..  **
191e0 0a 20 20 2a 2a 20 57 65 20 61 6c 73 6f 20 63 6f  .  ** We also co
191e1 6d 70 75 74 65 20 74 68 65 20 6e 75 6d 62 65 72  mpute the number
191e2 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
191e3 68 65 20 72 65 63 6f 72 64 2e 20 20 46 6f 72 20  he record.  For 
191e4 63 75 72 73 6f 72 73 2c 0a 20 20 2a 2a 20 74 68  cursors,.  ** th
191e5 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
191e6 6d 6e 73 20 69 73 20 73 74 6f 72 65 64 20 69 6e  mns is stored in
191e7 20 74 68 65 20 56 64 62 65 43 75 72 73 6f 72 2e   the VdbeCursor.
191e8 6e 46 69 65 6c 64 20 65 6c 65 6d 65 6e 74 2e 0a  nField element..
191e9 20 20 2a 2f 0a 20 20 70 43 20 3d 20 70 2d 3e 61    */.  pC = p->a
191ea 70 43 73 72 5b 70 31 5d 3b 0a 20 20 61 73 73 65  pCsr[p1];.  asse
191eb 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 23 69 66  rt( pC!=0 );.#if
191ec 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
191ed 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
191ee 61 73 73 65 72 74 28 20 70 43 2d 3e 70 56 74 61  assert( pC->pVta
191ef 62 43 75 72 73 6f 72 3d 3d 30 20 29 3b 0a 23 65  bCursor==0 );.#e
191f0 6e 64 69 66 0a 20 20 69 66 28 20 70 43 2d 3e 70  ndif.  if( pC->p
191f1 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a 20 20 20  Cursor!=0 ){.   
191f2 20 2f 2a 20 54 68 65 20 72 65 63 6f 72 64 20 69   /* The record i
191f3 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 42 2d  s stored in a B-
191f4 54 72 65 65 20 2a 2f 0a 20 20 20 20 72 63 20 3d  Tree */.    rc =
191f5 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73   sqlite3VdbeCurs
191f6 6f 72 4d 6f 76 65 74 6f 28 70 43 29 3b 0a 20 20  orMoveto(pC);.  
191f7 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20    if( rc ) goto 
191f8 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
191f9 6f 72 3b 0a 20 20 20 20 7a 52 65 63 20 3d 20 30  or;.    zRec = 0
191fa 3b 0a 20 20 20 20 70 43 72 73 72 20 3d 20 70 43  ;.    pCrsr = pC
191fb 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 20 20 69  ->pCursor;.    i
191fc 66 28 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29  f( pC->nullRow )
191fd 7b 0a 20 20 20 20 20 20 70 61 79 6c 6f 61 64 53  {.      payloadS
191fe 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  ize = 0;.    }el
191ff 73 65 20 69 66 28 20 70 43 2d 3e 63 61 63 68 65  se if( pC->cache
19200 53 74 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68 65  Status==p->cache
19201 43 74 72 20 29 7b 0a 20 20 20 20 20 20 70 61 79  Ctr ){.      pay
19202 6c 6f 61 64 53 69 7a 65 20 3d 20 70 43 2d 3e 70  loadSize = pC->p
19203 61 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20  ayloadSize;.    
19204 20 20 7a 52 65 63 20 3d 20 28 63 68 61 72 2a 29    zRec = (char*)
19205 70 43 2d 3e 61 52 6f 77 3b 0a 20 20 20 20 7d 65  pC->aRow;.    }e
19206 6c 73 65 20 69 66 28 20 70 43 2d 3e 69 73 49 6e  lse if( pC->isIn
19207 64 65 78 20 29 7b 0a 20 20 20 20 20 20 69 36 34  dex ){.      i64
19208 20 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 0a   payloadSize64;.
19209 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
1920a 65 65 4b 65 79 53 69 7a 65 28 70 43 72 73 72 2c  eeKeySize(pCrsr,
1920b 20 26 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 29   &payloadSize64)
1920c 3b 0a 20 20 20 20 20 20 70 61 79 6c 6f 61 64 53  ;.      payloadS
1920d 69 7a 65 20 3d 20 70 61 79 6c 6f 61 64 53 69 7a  ize = payloadSiz
1920e 65 36 34 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  e64;.    }else{.
1920f 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
19210 65 65 44 61 74 61 53 69 7a 65 28 70 43 72 73 72  eeDataSize(pCrsr
19211 2c 20 26 70 61 79 6c 6f 61 64 53 69 7a 65 29 3b  , &payloadSize);
19212 0a 20 20 20 20 7d 0a 20 20 20 20 6e 46 69 65 6c  .    }.    nFiel
19213 64 20 3d 20 70 43 2d 3e 6e 46 69 65 6c 64 3b 0a  d = pC->nField;.
19214 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
19215 65 72 74 28 20 70 43 2d 3e 70 73 65 75 64 6f 54  ert( pC->pseudoT
19216 61 62 6c 65 20 29 3b 0a 20 20 20 20 2f 2a 20 54  able );.    /* T
19217 68 65 20 72 65 63 6f 72 64 20 69 73 20 74 68 65  he record is the
19218 20 73 6f 6c 65 20 65 6e 74 72 79 20 6f 66 20 61   sole entry of a
19219 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 2a 2f   pseudo-table */
1921a 0a 20 20 20 20 70 61 79 6c 6f 61 64 53 69 7a 65  .    payloadSize
1921b 20 3d 20 70 43 2d 3e 6e 44 61 74 61 3b 0a 20 20   = pC->nData;.  
1921c 20 20 7a 52 65 63 20 3d 20 70 43 2d 3e 70 44 61    zRec = pC->pDa
1921d 74 61 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68  ta;.    pC->cach
1921e 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f  eStatus = CACHE_
1921f 53 54 41 4c 45 3b 0a 20 20 20 20 61 73 73 65 72  STALE;.    asser
19220 74 28 20 70 61 79 6c 6f 61 64 53 69 7a 65 3d 3d  t( payloadSize==
19221 30 20 7c 7c 20 7a 52 65 63 21 3d 30 20 29 3b 0a  0 || zRec!=0 );.
19222 20 20 20 20 6e 46 69 65 6c 64 20 3d 20 70 43 2d      nField = pC-
19223 3e 6e 46 69 65 6c 64 3b 0a 20 20 20 20 70 43 72  >nField;.    pCr
19224 73 72 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  sr = 0;.  }..  /
19225 2a 20 49 66 20 70 61 79 6c 6f 61 64 53 69 7a 65  * If payloadSize
19226 20 69 73 20 30 2c 20 74 68 65 6e 20 6a 75 73 74   is 0, then just
19227 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 2a 2f   store a NULL */
19228 0a 20 20 69 66 28 20 70 61 79 6c 6f 61 64 53 69  .  if( payloadSi
19229 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73  ze==0 ){.    ass
1922a 65 72 74 28 20 70 44 65 73 74 2d 3e 66 6c 61 67  ert( pDest->flag
1922b 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20  s&MEM_Null );.  
1922c 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e    goto op_column
1922d 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20  _out;.  }.  if( 
1922e 70 61 79 6c 6f 61 64 53 69 7a 65 3e 64 62 2d 3e  payloadSize>db->
1922f 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
19230 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20  MIT_LENGTH] ){. 
19231 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b     goto too_big;
19232 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
19233 70 32 3c 6e 46 69 65 6c 64 20 29 3b 0a 0a 20 20  p2<nField );..  
19234 2f 2a 20 52 65 61 64 20 61 6e 64 20 70 61 72 73  /* Read and pars
19235 65 20 74 68 65 20 74 61 62 6c 65 20 68 65 61 64  e the table head
19236 65 72 2e 20 20 53 74 6f 72 65 20 74 68 65 20 72  er.  Store the r
19237 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 70 61  esults of the pa
19238 72 73 65 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68  rse.  ** into th
19239 65 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 20  e record header 
1923a 63 61 63 68 65 20 66 69 65 6c 64 73 20 6f 66 20  cache fields of 
1923b 74 68 65 20 63 75 72 73 6f 72 2e 0a 20 20 2a 2f  the cursor..  */
1923c 0a 20 20 61 54 79 70 65 20 3d 20 70 43 2d 3e 61  .  aType = pC->a
1923d 54 79 70 65 3b 0a 20 20 69 66 28 20 70 43 2d 3e  Type;.  if( pC->
1923e 63 61 63 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e  cacheStatus==p->
1923f 63 61 63 68 65 43 74 72 20 29 7b 0a 20 20 20 20  cacheCtr ){.    
19240 61 4f 66 66 73 65 74 20 3d 20 70 43 2d 3e 61 4f  aOffset = pC->aO
19241 66 66 73 65 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ffset;.  }else{.
19242 20 20 20 20 75 38 20 2a 7a 49 64 78 3b 20 20 20      u8 *zIdx;   
19243 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e       /* Index in
19244 74 6f 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20  to header */.   
19245 20 75 38 20 2a 7a 45 6e 64 48 64 72 3b 20 20 20   u8 *zEndHdr;   
19246 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
19247 66 69 72 73 74 20 62 79 74 65 20 61 66 74 65 72  first byte after
19248 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20   the header */. 
19249 20 20 20 75 33 32 20 6f 66 66 73 65 74 3b 20 20     u32 offset;  
1924a 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e      /* Offset in
1924b 74 6f 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20  to the data */. 
1924c 20 20 20 69 6e 74 20 73 7a 48 64 72 53 7a 3b 20     int szHdrSz; 
1924d 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74      /* Size of t
1924e 68 65 20 68 65 61 64 65 72 20 73 69 7a 65 20 66  he header size f
1924f 69 65 6c 64 20 61 74 20 73 74 61 72 74 20 6f 66  ield at start of
19250 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 69   record */.    i
19251 6e 74 20 61 76 61 69 6c 3b 20 20 20 20 20 20 20  nt avail;       
19252 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
19253 65 73 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20  es of available 
19254 64 61 74 61 20 2a 2f 0a 0a 20 20 20 20 61 73 73  data */..    ass
19255 65 72 74 28 61 54 79 70 65 29 3b 0a 20 20 20 20  ert(aType);.    
19256 70 43 2d 3e 61 4f 66 66 73 65 74 20 3d 20 61 4f  pC->aOffset = aO
19257 66 66 73 65 74 20 3d 20 26 61 54 79 70 65 5b 6e  ffset = &aType[n
19258 46 69 65 6c 64 5d 3b 0a 20 20 20 20 70 43 2d 3e  Field];.    pC->
19259 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 70 61  payloadSize = pa
1925a 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 70  yloadSize;.    p
1925b 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  C->cacheStatus =
1925c 20 70 2d 3e 63 61 63 68 65 43 74 72 3b 0a 0a 20   p->cacheCtr;.. 
1925d 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74     /* Figure out
1925e 20 68 6f 77 20 6d 61 6e 79 20 62 79 74 65 73 20   how many bytes 
1925f 61 72 65 20 69 6e 20 74 68 65 20 68 65 61 64 65  are in the heade
19260 72 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a 52 65  r */.    if( zRe
19261 63 20 29 7b 0a 20 20 20 20 20 20 7a 44 61 74 61  c ){.      zData
19262 20 3d 20 7a 52 65 63 3b 0a 20 20 20 20 7d 65 6c   = zRec;.    }el
19263 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43  se{.      if( pC
19264 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20  ->isIndex ){.   
19265 20 20 20 20 20 7a 44 61 74 61 20 3d 20 28 63 68       zData = (ch
19266 61 72 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65  ar*)sqlite3Btree
19267 4b 65 79 46 65 74 63 68 28 70 43 72 73 72 2c 20  KeyFetch(pCrsr, 
19268 26 61 76 61 69 6c 29 3b 0a 20 20 20 20 20 20 7d  &avail);.      }
19269 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 44  else{.        zD
1926a 61 74 61 20 3d 20 28 63 68 61 72 2a 29 73 71 6c  ata = (char*)sql
1926b 69 74 65 33 42 74 72 65 65 44 61 74 61 46 65 74  ite3BtreeDataFet
1926c 63 68 28 70 43 72 73 72 2c 20 26 61 76 61 69 6c  ch(pCrsr, &avail
1926d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1926e 20 2f 2a 20 49 66 20 4b 65 79 46 65 74 63 68 28   /* If KeyFetch(
1926f 29 2f 44 61 74 61 46 65 74 63 68 28 29 20 6d 61  )/DataFetch() ma
19270 6e 61 67 65 64 20 74 6f 20 67 65 74 20 74 68 65  naged to get the
19271 20 65 6e 74 69 72 65 20 70 61 79 6c 6f 61 64 2c   entire payload,
19272 0a 20 20 20 20 20 20 2a 2a 20 73 61 76 65 20 74  .      ** save t
19273 68 65 20 70 61 79 6c 6f 61 64 20 69 6e 20 74 68  he payload in th
19274 65 20 70 43 2d 3e 61 52 6f 77 20 63 61 63 68 65  e pC->aRow cache
19275 2e 20 20 54 68 61 74 20 77 69 6c 6c 20 73 61 76  .  That will sav
19276 65 20 75 73 20 66 72 6f 6d 0a 20 20 20 20 20 20  e us from.      
19277 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 6d 61 6b  ** having to mak
19278 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c  e additional cal
19279 6c 73 20 74 6f 20 66 65 74 63 68 20 74 68 65 20  ls to fetch the 
1927a 63 6f 6e 74 65 6e 74 20 70 6f 72 74 69 6f 6e 20  content portion 
1927b 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  of.      ** the 
1927c 72 65 63 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f  record..      */
1927d 0a 20 20 20 20 20 20 69 66 28 20 61 76 61 69 6c  .      if( avail
1927e 3e 3d 70 61 79 6c 6f 61 64 53 69 7a 65 20 29 7b  >=payloadSize ){
1927f 0a 20 20 20 20 20 20 20 20 7a 52 65 63 20 3d 20  .        zRec = 
19280 7a 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20 70  zData;.        p
19281 43 2d 3e 61 52 6f 77 20 3d 20 28 75 38 2a 29 7a  C->aRow = (u8*)z
19282 44 61 74 61 3b 0a 20 20 20 20 20 20 7d 65 6c 73  Data;.      }els
19283 65 7b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 61  e{.        pC->a
19284 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  Row = 0;.      }
19285 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 54 68  .    }.    /* Th
19286 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65  e following asse
19287 72 74 20 69 73 20 74 72 75 65 20 69 6e 20 61 6c  rt is true in al
19288 6c 20 63 61 73 65 73 20 61 63 63 65 70 74 20 77  l cases accept w
19289 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 64  hen.    ** the d
1928a 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 73  atabase file has
1928b 20 62 65 65 6e 20 63 6f 72 72 75 70 74 65 64 20   been corrupted 
1928c 65 78 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 20 20  externally..    
1928d 2a 2a 20 20 20 20 61 73 73 65 72 74 28 20 7a 52  **    assert( zR
1928e 65 63 21 3d 30 20 7c 7c 20 61 76 61 69 6c 3e 3d  ec!=0 || avail>=
1928f 70 61 79 6c 6f 61 64 53 69 7a 65 20 7c 7c 20 61  payloadSize || a
19290 76 61 69 6c 3e 3d 39 20 29 3b 20 2a 2f 0a 20 20  vail>=9 ); */.  
19291 20 20 73 7a 48 64 72 53 7a 20 3d 20 67 65 74 56    szHdrSz = getV
19292 61 72 69 6e 74 33 32 28 28 75 38 2a 29 7a 44 61  arint32((u8*)zDa
19293 74 61 2c 20 6f 66 66 73 65 74 29 3b 0a 0a 20 20  ta, offset);..  
19294 20 20 2f 2a 20 54 68 65 20 4b 65 79 46 65 74 63    /* The KeyFetc
19295 68 28 29 20 6f 72 20 44 61 74 61 46 65 74 63 68  h() or DataFetch
19296 28 29 20 61 62 6f 76 65 20 61 72 65 20 66 61 73  () above are fas
19297 74 20 61 6e 64 20 77 69 6c 6c 20 67 65 74 20 74  t and will get t
19298 68 65 20 65 6e 74 69 72 65 0a 20 20 20 20 2a 2a  he entire.    **
19299 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 20 69   record header i
1929a 6e 20 6d 6f 73 74 20 63 61 73 65 73 2e 20 20 42  n most cases.  B
1929b 75 74 20 74 68 65 79 20 77 69 6c 6c 20 66 61 69  ut they will fai
1929c 6c 20 74 6f 20 67 65 74 20 74 68 65 20 63 6f 6d  l to get the com
1929d 70 6c 65 74 65 0a 20 20 20 20 2a 2a 20 72 65 63  plete.    ** rec
1929e 6f 72 64 20 68 65 61 64 65 72 20 69 66 20 74 68  ord header if th
1929f 65 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 20  e record header 
192a0 64 6f 65 73 20 6e 6f 74 20 66 69 74 20 6f 6e 20  does not fit on 
192a1 61 20 73 69 6e 67 6c 65 20 70 61 67 65 0a 20 20  a single page.  
192a2 20 20 2a 2a 20 69 6e 20 74 68 65 20 42 2d 54 72    ** in the B-Tr
192a3 65 65 2e 20 20 57 68 65 6e 20 74 68 61 74 20 68  ee.  When that h
192a4 61 70 70 65 6e 73 2c 20 75 73 65 20 73 71 6c 69  appens, use sqli
192a5 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74  te3VdbeMemFromBt
192a6 72 65 65 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20  ree() to.    ** 
192a7 61 63 71 75 69 72 65 20 74 68 65 20 63 6f 6d 70  acquire the comp
192a8 6c 65 74 65 20 68 65 61 64 65 72 20 74 65 78 74  lete header text
192a9 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
192aa 20 21 7a 52 65 63 20 26 26 20 61 76 61 69 6c 3c   !zRec && avail<
192ab 6f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 20 20  offset ){.      
192ac 73 4d 65 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a  sMem.flags = 0;.
192ad 20 20 20 20 20 20 73 4d 65 6d 2e 64 62 20 3d 20        sMem.db = 
192ae 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  0;.      rc = sq
192af 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d  lite3VdbeMemFrom
192b0 42 74 72 65 65 28 70 43 72 73 72 2c 20 30 2c 20  Btree(pCrsr, 0, 
192b1 6f 66 66 73 65 74 2c 20 70 43 2d 3e 69 73 49 6e  offset, pC->isIn
192b2 64 65 78 2c 20 26 73 4d 65 6d 29 3b 0a 20 20 20  dex, &sMem);.   
192b3 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
192b4 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
192b5 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f  goto op_column_o
192b6 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ut;.      }.    
192b7 20 20 7a 44 61 74 61 20 3d 20 73 4d 65 6d 2e 7a    zData = sMem.z
192b8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 45 6e 64  ;.    }.    zEnd
192b9 48 64 72 20 3d 20 28 75 38 20 2a 29 26 7a 44 61  Hdr = (u8 *)&zDa
192ba 74 61 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 20 20  ta[offset];.    
192bb 7a 49 64 78 20 3d 20 28 75 38 20 2a 29 26 7a 44  zIdx = (u8 *)&zD
192bc 61 74 61 5b 73 7a 48 64 72 53 7a 5d 3b 0a 0a 20  ata[szHdrSz];.. 
192bd 20 20 20 2f 2a 20 53 63 61 6e 20 74 68 65 20 68     /* Scan the h
192be 65 61 64 65 72 20 61 6e 64 20 75 73 65 20 69 74  eader and use it
192bf 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20   to fill in the 
192c0 61 54 79 70 65 5b 5d 20 61 6e 64 20 61 4f 66 66  aType[] and aOff
192c1 73 65 74 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72  set[].    ** arr
192c2 61 79 73 2e 20 20 61 54 79 70 65 5b 69 5d 20 77  ays.  aType[i] w
192c3 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68 65 20  ill contain the 
192c4 74 79 70 65 20 69 6e 74 65 67 65 72 20 66 6f 72  type integer for
192c5 20 74 68 65 20 69 2d 74 68 0a 20 20 20 20 2a 2a   the i-th.    **
192c6 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 61 4f 66 66   column and aOff
192c7 73 65 74 5b 69 5d 20 77 69 6c 6c 20 63 6f 6e 74  set[i] will cont
192c8 61 69 6e 20 74 68 65 20 6f 66 66 73 65 74 20 66  ain the offset f
192c9 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e  rom the beginnin
192ca 67 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20  g.    ** of the 
192cb 72 65 63 6f 72 64 20 74 6f 20 74 68 65 20 73 74  record to the st
192cc 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 20  art of the data 
192cd 66 6f 72 20 74 68 65 20 69 2d 74 68 20 63 6f 6c  for the i-th col
192ce 75 6d 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66  umn.    */.    f
192cf 6f 72 28 69 3d 30 3b 20 69 3c 6e 46 69 65 6c 64  or(i=0; i<nField
192d0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
192d1 28 20 7a 49 64 78 3c 7a 45 6e 64 48 64 72 20 29  ( zIdx<zEndHdr )
192d2 7b 0a 20 20 20 20 20 20 20 20 61 4f 66 66 73 65  {.        aOffse
192d3 74 5b 69 5d 20 3d 20 6f 66 66 73 65 74 3b 0a 20  t[i] = offset;. 
192d4 20 20 20 20 20 20 20 7a 49 64 78 20 2b 3d 20 67         zIdx += g
192d5 65 74 56 61 72 69 6e 74 33 32 28 7a 49 64 78 2c  etVarint32(zIdx,
192d6 20 61 54 79 70 65 5b 69 5d 29 3b 0a 20 20 20 20   aType[i]);.    
192d7 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20 73 71      offset += sq
192d8 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54  lite3VdbeSerialT
192d9 79 70 65 4c 65 6e 28 61 54 79 70 65 5b 69 5d 29  ypeLen(aType[i])
192da 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
192db 20 20 20 20 20 20 20 2f 2a 20 49 66 20 69 20 69         /* If i i
192dc 73 20 6c 65 73 73 20 74 68 61 74 20 6e 46 69 65  s less that nFie
192dd 6c 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 61  ld, then there a
192de 72 65 20 6c 65 73 73 20 66 69 65 6c 64 73 20 69  re less fields i
192df 6e 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a  n this.        *
192e0 2a 20 72 65 63 6f 72 64 20 74 68 61 6e 20 53 65  * record than Se
192e1 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 69 6e 64 69  tNumColumns indi
192e2 63 61 74 65 64 20 74 68 65 72 65 20 61 72 65 20  cated there are 
192e3 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 0a 20  columns in the. 
192e4 20 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 2e         ** table.
192e5 20 53 65 74 20 74 68 65 20 6f 66 66 73 65 74 20   Set the offset 
192e6 66 6f 72 20 61 6e 79 20 65 78 74 72 61 20 63 6f  for any extra co
192e7 6c 75 6d 6e 73 20 6e 6f 74 20 70 72 65 73 65 6e  lumns not presen
192e8 74 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  t in.        ** 
192e9 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 30 2e  the record to 0.
192ea 20 54 68 69 73 20 74 65 6c 6c 73 20 63 6f 64 65   This tells code
192eb 20 62 65 6c 6f 77 20 74 6f 20 73 74 6f 72 65 20   below to store 
192ec 61 20 4e 55 4c 4c 0a 20 20 20 20 20 20 20 20 2a  a NULL.        *
192ed 2a 20 69 6e 73 74 65 61 64 20 6f 66 20 64 65 73  * instead of des
192ee 65 72 69 61 6c 69 7a 69 6e 67 20 61 20 76 61 6c  erializing a val
192ef 75 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f  ue from the reco
192f0 72 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  rd..        */. 
192f1 20 20 20 20 20 20 20 61 4f 66 66 73 65 74 5b 69         aOffset[i
192f2 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  ] = 0;.      }. 
192f3 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
192f4 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26  VdbeMemRelease(&
192f5 73 4d 65 6d 29 3b 0a 20 20 20 20 73 4d 65 6d 2e  sMem);.    sMem.
192f6 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
192f7 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20  ;..    /* If we 
192f8 68 61 76 65 20 72 65 61 64 20 6d 6f 72 65 20 68  have read more h
192f9 65 61 64 65 72 20 64 61 74 61 20 74 68 61 6e 20  eader data than 
192fa 77 61 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e  was contained in
192fb 20 74 68 65 20 68 65 61 64 65 72 2c 0a 20 20 20   the header,.   
192fc 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 65 6e   ** or if the en
192fd 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66 69  d of the last fi
192fe 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20 62  eld appears to b
192ff 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f  e past the end o
19300 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 63  f the.    ** rec
19301 6f 72 64 2c 20 6f 72 20 69 66 20 74 68 65 20 65  ord, or if the e
19302 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66  nd of the last f
19303 69 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20  ield appears to 
19304 62 65 20 62 65 66 6f 72 65 20 74 68 65 20 65 6e  be before the en
19305 64 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20  d.    ** of the 
19306 72 65 63 6f 72 64 20 28 77 68 65 6e 20 61 6c 6c  record (when all
19307 20 66 69 65 6c 64 73 20 70 72 65 73 65 6e 74 29   fields present)
19308 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 74 20 62  , then we must b
19309 65 20 64 65 61 6c 69 6e 67 20 0a 20 20 20 20 2a  e dealing .    *
1930a 2a 20 77 69 74 68 20 61 20 63 6f 72 72 75 70 74  * with a corrupt
1930b 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a   database..    *
1930c 2f 0a 20 20 20 20 69 66 28 20 7a 49 64 78 3e 7a  /.    if( zIdx>z
1930d 45 6e 64 48 64 72 20 7c 7c 20 6f 66 66 73 65 74  EndHdr || offset
1930e 3e 70 61 79 6c 6f 61 64 53 69 7a 65 20 0a 20 20  >payloadSize .  
1930f 20 20 20 7c 7c 20 28 7a 49 64 78 3d 3d 7a 45 6e     || (zIdx==zEn
19310 64 48 64 72 20 26 26 20 6f 66 66 73 65 74 21 3d  dHdr && offset!=
19311 70 61 79 6c 6f 61 64 53 69 7a 65 29 20 29 7b 0a  payloadSize) ){.
19312 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
19313 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
19314 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f        goto op_co
19315 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  lumn_out;.    }.
19316 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68    }..  /* Get th
19317 65 20 63 6f 6c 75 6d 6e 20 69 6e 66 6f 72 6d 61  e column informa
19318 74 69 6f 6e 2e 20 49 66 20 61 4f 66 66 73 65 74  tion. If aOffset
19319 5b 70 32 5d 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  [p2] is non-zero
1931a 2c 20 74 68 65 6e 20 0a 20 20 2a 2a 20 64 65 73  , then .  ** des
1931b 65 72 69 61 6c 69 7a 65 20 74 68 65 20 76 61 6c  erialize the val
1931c 75 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f  ue from the reco
1931d 72 64 2e 20 49 66 20 61 4f 66 66 73 65 74 5b 70  rd. If aOffset[p
1931e 32 5d 20 69 73 20 7a 65 72 6f 2c 0a 20 20 2a 2a  2] is zero,.  **
1931f 20 74 68 65 6e 20 74 68 65 72 65 20 61 72 65 20   then there are 
19320 6e 6f 74 20 65 6e 6f 75 67 68 20 66 69 65 6c 64  not enough field
19321 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20  s in the record 
19322 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 0a 20  to satisfy the. 
19323 20 2a 2a 20 72 65 71 75 65 73 74 2e 20 20 49 6e   ** request.  In
19324 20 74 68 69 73 20 63 61 73 65 2c 20 73 65 74 20   this case, set 
19325 74 68 65 20 76 61 6c 75 65 20 4e 55 4c 4c 20 6f  the value NULL o
19326 72 20 74 6f 20 50 34 20 69 66 20 50 34 20 69 73  r to P4 if P4 is
19327 0a 20 20 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20  .  ** a pointer 
19328 74 6f 20 61 20 4d 65 6d 20 6f 62 6a 65 63 74 2e  to a Mem object.
19329 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61 4f 66 66  .  */.  if( aOff
1932a 73 65 74 5b 70 32 5d 20 29 7b 0a 20 20 20 20 61  set[p2] ){.    a
1932b 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
1932c 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69 66 28 20  E_OK );.    if( 
1932d 7a 52 65 63 20 29 7b 0a 20 20 20 20 20 20 73 71  zRec ){.      sq
1932e 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
1932f 61 73 65 45 78 74 65 72 6e 61 6c 28 70 44 65 73  aseExternal(pDes
19330 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
19331 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 28  3VdbeSerialGet((
19332 75 38 20 2a 29 26 7a 52 65 63 5b 61 4f 66 66 73  u8 *)&zRec[aOffs
19333 65 74 5b 70 32 5d 5d 2c 20 61 54 79 70 65 5b 70  et[p2]], aType[p
19334 32 5d 2c 20 70 44 65 73 74 29 3b 0a 20 20 20 20  2], pDest);.    
19335 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6c 65 6e  }else{.      len
19336 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65   = sqlite3VdbeSe
19337 72 69 61 6c 54 79 70 65 4c 65 6e 28 61 54 79 70  rialTypeLen(aTyp
19338 65 5b 70 32 5d 29 3b 0a 20 20 20 20 20 20 73 71  e[p2]);.      sq
19339 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65  lite3VdbeMemMove
1933a 28 26 73 4d 65 6d 2c 20 70 44 65 73 74 29 3b 0a  (&sMem, pDest);.
1933b 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1933c 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72  e3VdbeMemFromBtr
1933d 65 65 28 70 43 72 73 72 2c 20 61 4f 66 66 73 65  ee(pCrsr, aOffse
1933e 74 5b 70 32 5d 2c 20 6c 65 6e 2c 20 70 43 2d 3e  t[p2], len, pC->
1933f 69 73 49 6e 64 65 78 2c 20 26 73 4d 65 6d 29 3b  isIndex, &sMem);
19340 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
19341 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
19342 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75      goto op_colu
19343 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a  mn_out;.      }.
19344 20 20 20 20 20 20 7a 44 61 74 61 20 3d 20 73 4d        zData = sM
19345 65 6d 2e 7a 3b 0a 20 20 20 20 20 20 73 71 6c 69  em.z;.      sqli
19346 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 74  te3VdbeSerialGet
19347 28 28 75 38 2a 29 7a 44 61 74 61 2c 20 61 54 79  ((u8*)zData, aTy
19348 70 65 5b 70 32 5d 2c 20 70 44 65 73 74 29 3b 0a  pe[p2], pDest);.
19349 20 20 20 20 7d 0a 20 20 20 20 70 44 65 73 74 2d      }.    pDest-
1934a 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b  >enc = encoding;
1934b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66  .  }else{.    if
1934c 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50  ( pOp->p4type==P
1934d 34 5f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 73  4_MEM ){.      s
1934e 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61  qlite3VdbeMemSha
1934f 6c 6c 6f 77 43 6f 70 79 28 70 44 65 73 74 2c 20  llowCopy(pDest, 
19350 70 4f 70 2d 3e 70 34 2e 70 4d 65 6d 2c 20 4d 45  pOp->p4.pMem, ME
19351 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20 7d  M_Static);.    }
19352 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65  else{.      asse
19353 72 74 28 20 70 44 65 73 74 2d 3e 66 6c 61 67 73  rt( pDest->flags
19354 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20  &MEM_Null );.   
19355 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
19356 77 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61  we dynamically a
19357 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 74  llocated space t
19358 6f 20 68 6f 6c 64 20 74 68 65 20 64 61 74 61 20  o hold the data 
19359 28 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c  (in the.  ** sql
1935a 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42  ite3VdbeMemFromB
1935b 74 72 65 65 28 29 20 63 61 6c 6c 20 61 62 6f 76  tree() call abov
1935c 65 29 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72  e) then transfer
1935d 20 63 6f 6e 74 72 6f 6c 20 6f 66 20 74 68 61 74   control of that
1935e 0a 20 20 2a 2a 20 64 79 6e 61 6d 69 63 61 6c 6c  .  ** dynamicall
1935f 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63  y allocated spac
19360 65 20 6f 76 65 72 20 74 6f 20 74 68 65 20 70 44  e over to the pD
19361 65 73 74 20 73 74 72 75 63 74 75 72 65 2e 0a 20  est structure.. 
19362 20 2a 2a 20 54 68 69 73 20 70 72 65 76 65 6e 74   ** This prevent
19363 73 20 61 20 6d 65 6d 6f 72 79 20 63 6f 70 79 2e  s a memory copy.
19364 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 4d 65 6d  .  */.  if( sMem
19365 2e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20  .zMalloc ){.    
19366 61 73 73 65 72 74 28 20 73 4d 65 6d 2e 7a 3d 3d  assert( sMem.z==
19367 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a  sMem.zMalloc );.
19368 20 20 20 20 61 73 73 65 72 74 28 20 21 28 70 44      assert( !(pD
19369 65 73 74 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  est->flags & MEM
1936a 5f 44 79 6e 29 20 29 3b 0a 20 20 20 20 61 73 73  _Dyn) );.    ass
1936b 65 72 74 28 20 21 28 70 44 65 73 74 2d 3e 66 6c  ert( !(pDest->fl
1936c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c  ags & (MEM_Blob|
1936d 4d 45 4d 5f 53 74 72 29 29 20 7c 7c 20 70 44 65  MEM_Str)) || pDe
1936e 73 74 2d 3e 7a 3d 3d 73 4d 65 6d 2e 7a 20 29 3b  st->z==sMem.z );
1936f 0a 20 20 20 20 70 44 65 73 74 2d 3e 66 6c 61 67  .    pDest->flag
19370 73 20 26 3d 20 7e 28 4d 45 4d 5f 45 70 68 65 6d  s &= ~(MEM_Ephem
19371 7c 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20  |MEM_Static);.  
19372 20 20 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 7c    pDest->flags |
19373 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20  = MEM_Term;.    
19374 70 44 65 73 74 2d 3e 7a 20 3d 20 73 4d 65 6d 2e  pDest->z = sMem.
19375 7a 3b 0a 20 20 20 20 70 44 65 73 74 2d 3e 7a 4d  z;.    pDest->zM
19376 61 6c 6c 6f 63 20 3d 20 73 4d 65 6d 2e 7a 4d 61  alloc = sMem.zMa
19377 6c 6c 6f 63 3b 0a 20 20 7d 0a 0a 20 20 72 63 20  lloc;.  }..  rc 
19378 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  = sqlite3VdbeMem
19379 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 44  MakeWriteable(pD
1937a 65 73 74 29 3b 0a 0a 6f 70 5f 63 6f 6c 75 6d 6e  est);..op_column
1937b 5f 6f 75 74 3a 0a 20 20 55 50 44 41 54 45 5f 4d  _out:.  UPDATE_M
1937c 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 44 65 73  AX_BLOBSIZE(pDes
1937d 74 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54  t);.  REGISTER_T
1937e 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 44  RACE(pOp->p3, pD
1937f 65 73 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  est);.  break;.}
19380 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 66 66  ../* Opcode: Aff
19381 69 6e 69 74 79 20 50 31 20 50 32 20 2a 20 50 34  inity P1 P2 * P4
19382 20 2a 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 79 20 61   *.**.** Apply a
19383 66 66 69 6e 69 74 69 65 73 20 74 6f 20 61 20 72  ffinities to a r
19384 61 6e 67 65 20 6f 66 20 50 32 20 72 65 67 69 73  ange of P2 regis
19385 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 77 69  ters starting wi
19386 74 68 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 50 34 20  th P1..**.** P4 
19387 69 73 20 61 20 73 74 72 69 6e 67 20 74 68 61 74  is a string that
19388 20 69 73 20 50 32 20 63 68 61 72 61 63 74 65 72   is P2 character
19389 73 20 6c 6f 6e 67 2e 20 54 68 65 20 6e 74 68 20  s long. The nth 
1938a 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 65  character of the
1938b 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 64 69 63  .** string indic
1938c 61 74 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20  ates the column 
1938d 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68  affinity that sh
1938e 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72  ould be used for
1938f 20 74 68 65 20 6e 74 68 0a 2a 2a 20 6d 65 6d 6f   the nth.** memo
19390 72 79 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 72  ry cell in the r
19391 61 6e 67 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  ange..*/.case OP
19392 5f 41 66 66 69 6e 69 74 79 3a 20 7b 0a 20 20 63  _Affinity: {.  c
19393 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 20 3d  har *zAffinity =
19394 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 4d 65   pOp->p4.z;.  Me
19395 6d 20 2a 70 44 61 74 61 30 20 3d 20 26 70 2d 3e  m *pData0 = &p->
19396 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  aMem[pOp->p1];. 
19397 20 4d 65 6d 20 2a 70 4c 61 73 74 20 3d 20 26 70   Mem *pLast = &p
19398 44 61 74 61 30 5b 70 4f 70 2d 3e 70 32 2d 31 5d  Data0[pOp->p2-1]
19399 3b 0a 20 20 4d 65 6d 20 2a 70 52 65 63 3b 0a 0a  ;.  Mem *pRec;..
1939a 20 20 66 6f 72 28 70 52 65 63 3d 70 44 61 74 61    for(pRec=pData
1939b 30 3b 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b 20  0; pRec<=pLast; 
1939c 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 45 78 70  pRec++){.    Exp
1939d 61 6e 64 42 6c 6f 62 28 70 52 65 63 29 3b 0a 20  andBlob(pRec);. 
1939e 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79     applyAffinity
1939f 28 70 52 65 63 2c 20 7a 41 66 66 69 6e 69 74 79  (pRec, zAffinity
193a0 5b 70 52 65 63 2d 70 44 61 74 61 30 5d 2c 20 65  [pRec-pData0], e
193a1 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a 20 20  ncoding);.  }.  
193a2 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
193a3 6f 64 65 3a 20 4d 61 6b 65 52 65 63 6f 72 64 20  ode: MakeRecord 
193a4 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a  P1 P2 P3 P4 *.**
193a5 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 50 32 20 72  .** Convert P2 r
193a6 65 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69  egisters beginni
193a7 6e 67 20 77 69 74 68 20 50 31 20 69 6e 74 6f 20  ng with P1 into 
193a8 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 0a 2a  a single entry.*
193a9 2a 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75  * suitable for u
193aa 73 65 20 61 73 20 61 20 64 61 74 61 20 72 65 63  se as a data rec
193ab 6f 72 64 20 69 6e 20 61 20 64 61 74 61 62 61 73  ord in a databas
193ac 65 20 74 61 62 6c 65 20 6f 72 20 61 73 20 61 20  e table or as a 
193ad 6b 65 79 0a 2a 2a 20 69 6e 20 61 6e 20 69 6e 64  key.** in an ind
193ae 65 78 2e 20 20 54 68 65 20 64 65 74 61 69 6c 73  ex.  The details
193af 20 6f 66 20 74 68 65 20 66 6f 72 6d 61 74 20 61   of the format a
193b0 72 65 20 69 72 72 65 6c 65 76 61 6e 74 20 61 73  re irrelevant as
193b1 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 74 68 65 20   long as.** the 
193b2 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65  OP_Column opcode
193b3 20 63 61 6e 20 64 65 63 6f 64 65 20 74 68 65 20   can decode the 
193b4 72 65 63 6f 72 64 20 6c 61 74 65 72 2e 0a 2a 2a  record later..**
193b5 20 52 65 66 65 72 20 74 6f 20 73 6f 75 72 63 65   Refer to source
193b6 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 73 20 66   code comments f
193b7 6f 72 20 74 68 65 20 64 65 74 61 69 6c 73 20 6f  or the details o
193b8 66 20 74 68 65 20 72 65 63 6f 72 64 0a 2a 2a 20  f the record.** 
193b9 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 50 34  format..**.** P4
193ba 20 6d 61 79 20 62 65 20 61 20 73 74 72 69 6e 67   may be a string
193bb 20 74 68 61 74 20 69 73 20 50 32 20 63 68 61 72   that is P2 char
193bc 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 54 68  acters long.  Th
193bd 65 20 6e 74 68 20 63 68 61 72 61 63 74 65 72 20  e nth character 
193be 6f 66 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67  of the.** string
193bf 20 69 6e 64 69 63 61 74 65 73 20 74 68 65 20 63   indicates the c
193c0 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 74  olumn affinity t
193c1 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73  hat should be us
193c2 65 64 20 66 6f 72 20 74 68 65 20 6e 74 68 0a 2a  ed for the nth.*
193c3 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 69  * field of the i
193c4 6e 64 65 78 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20  ndex key..**.** 
193c5 54 68 65 20 6d 61 70 70 69 6e 67 20 66 72 6f 6d  The mapping from
193c6 20 63 68 61 72 61 63 74 65 72 20 74 6f 20 61 66   character to af
193c7 66 69 6e 69 74 79 20 69 73 20 67 69 76 65 6e 20  finity is given 
193c8 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f 41 46  by the SQLITE_AF
193c9 46 5f 0a 2a 2a 20 6d 61 63 72 6f 73 20 64 65 66  F_.** macros def
193ca 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 49 6e  ined in sqliteIn
193cb 74 2e 68 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34  t.h..**.** If P4
193cc 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 61 6c   is NULL then al
193cd 6c 20 69 6e 64 65 78 20 66 69 65 6c 64 73 20 68  l index fields h
193ce 61 76 65 20 74 68 65 20 61 66 66 69 6e 69 74 79  ave the affinity
193cf 20 4e 4f 4e 45 2e 0a 2a 2f 0a 63 61 73 65 20 4f   NONE..*/.case O
193d0 50 5f 4d 61 6b 65 52 65 63 6f 72 64 3a 20 7b 0a  P_MakeRecord: {.
193d1 20 20 2f 2a 20 41 73 73 75 6d 69 6e 67 20 74 68    /* Assuming th
193d2 65 20 72 65 63 6f 72 64 20 63 6f 6e 74 61 69 6e  e record contain
193d3 73 20 4e 20 66 69 65 6c 64 73 2c 20 74 68 65 20  s N fields, the 
193d4 72 65 63 6f 72 64 20 66 6f 72 6d 61 74 20 6c 6f  record format lo
193d5 6f 6b 73 0a 20 20 2a 2a 20 6c 69 6b 65 20 74 68  oks.  ** like th
193d6 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 2d 2d  is:.  **.  ** --
193d7 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
193d8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
193d9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
193da 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
193db 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20 7c 20 68 64  ------.  ** | hd
193dc 72 2d 73 69 7a 65 20 7c 20 74 79 70 65 20 30 20  r-size | type 0 
193dd 7c 20 74 79 70 65 20 31 20 7c 20 2e 2e 2e 20 7c  | type 1 | ... |
193de 20 74 79 70 65 20 4e 2d 31 20 7c 20 64 61 74 61   type N-1 | data
193df 30 20 7c 20 2e 2e 2e 20 7c 20 64 61 74 61 20 4e  0 | ... | data N
193e0 2d 31 20 7c 20 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d  -1 | .  ** -----
193e1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
193e2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
193e3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
193e4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
193e5 2d 2d 2d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 44 61  ---.  **.  ** Da
193e6 74 61 28 30 29 20 69 73 20 74 61 6b 65 6e 20 66  ta(0) is taken f
193e7 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50 31 2e  rom register P1.
193e8 20 20 44 61 74 61 28 31 29 20 63 6f 6d 65 73 20    Data(1) comes 
193e9 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50 31  from register P1
193ea 2b 31 0a 20 20 2a 2a 20 61 6e 64 20 73 6f 20 66  +1.  ** and so f
193eb 72 6f 74 68 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  roth..  **.  ** 
193ec 45 61 63 68 20 74 79 70 65 20 66 69 65 6c 64 20  Each type field 
193ed 69 73 20 61 20 76 61 72 69 6e 74 20 72 65 70 72  is a varint repr
193ee 65 73 65 6e 74 69 6e 67 20 74 68 65 20 73 65 72  esenting the ser
193ef 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65 20  ial type of the 
193f0 0a 20 20 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64  .  ** correspond
193f1 69 6e 67 20 64 61 74 61 20 65 6c 65 6d 65 6e 74  ing data element
193f2 20 28 73 65 65 20 73 71 6c 69 74 65 33 56 64 62   (see sqlite3Vdb
193f3 65 53 65 72 69 61 6c 54 79 70 65 28 29 29 2e 20  eSerialType()). 
193f4 54 68 65 0a 20 20 2a 2a 20 68 64 72 2d 73 69 7a  The.  ** hdr-siz
193f5 65 20 66 69 65 6c 64 20 69 73 20 61 6c 73 6f 20  e field is also 
193f6 61 20 76 61 72 69 6e 74 20 77 68 69 63 68 20 69  a varint which i
193f7 73 20 74 68 65 20 6f 66 66 73 65 74 20 66 72 6f  s the offset fro
193f8 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a  m the beginning.
193f9 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 63 6f    ** of the reco
193fa 72 64 20 74 6f 20 64 61 74 61 30 2e 0a 20 20 2a  rd to data0..  *
193fb 2f 0a 20 20 75 38 20 2a 7a 4e 65 77 52 65 63 6f  /.  u8 *zNewReco
193fc 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 20  rd;        /* A 
193fd 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74  buffer to hold t
193fe 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 20  he data for the 
193ff 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  new record */.  
19400 4d 65 6d 20 2a 70 52 65 63 3b 20 20 20 20 20 20  Mem *pRec;      
19401 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65         /* The ne
19402 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 36  w record */.  u6
19403 34 20 6e 44 61 74 61 20 3d 20 30 3b 20 20 20 20  4 nData = 0;    
19404 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
19405 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20  f bytes of data 
19406 73 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  space */.  int n
19407 48 64 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Hdr = 0;        
19408 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
19409 79 74 65 73 20 6f 66 20 68 65 61 64 65 72 20 73  ytes of header s
1940a 70 61 63 65 20 2a 2f 0a 20 20 75 36 34 20 6e 42  pace */.  u64 nB
1940b 79 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20  yte = 0;        
1940c 20 2f 2a 20 44 61 74 61 20 73 70 61 63 65 20 72   /* Data space r
1940d 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69 73  equired for this
1940e 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74   record */.  int
1940f 20 6e 5a 65 72 6f 20 3d 20 30 3b 20 20 20 20 20   nZero = 0;     
19410 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
19411 20 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20 74   zero bytes at t
19412 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72 65  he end of the re
19413 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 56  cord */.  int nV
19414 61 72 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20  arint;          
19415 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
19416 74 65 73 20 69 6e 20 61 20 76 61 72 69 6e 74 20  tes in a varint 
19417 2a 2f 0a 20 20 75 33 32 20 73 65 72 69 61 6c 5f  */.  u32 serial_
19418 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 54  type;       /* T
19419 79 70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 4d  ype field */.  M
1941a 65 6d 20 2a 70 44 61 74 61 30 3b 20 20 20 20 20  em *pData0;     
1941b 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 66        /* First f
1941c 69 65 6c 64 20 74 6f 20 62 65 20 63 6f 6d 62 69  ield to be combi
1941d 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 72 65 63  ned into the rec
1941e 6f 72 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4c  ord */.  Mem *pL
1941f 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ast;            
19420 2f 2a 20 4c 61 73 74 20 66 69 65 6c 64 20 6f 66  /* Last field of
19421 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20   the record */. 
19422 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20   int nField;    
19423 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
19424 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74  r of fields in t
19425 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63  he record */.  c
19426 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20  har *zAffinity; 
19427 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66        /* The aff
19428 69 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72  inity string for
19429 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20   the record */. 
1942a 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74   int file_format
1942b 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20  ;       /* File 
1942c 66 6f 72 6d 61 74 20 74 6f 20 75 73 65 20 66 6f  format to use fo
1942d 72 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20  r encoding */.  
1942e 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
1942f 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20         /* Space 
19430 75 73 65 64 20 69 6e 20 7a 4e 65 77 52 65 63 6f  used in zNewReco
19431 72 64 5b 5d 20 2a 2f 0a 0a 20 20 6e 46 69 65 6c  rd[] */..  nFiel
19432 64 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 7a  d = pOp->p1;.  z
19433 41 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e  Affinity = pOp->
19434 70 34 2e 7a 3b 0a 20 20 61 73 73 65 72 74 28 20  p4.z;.  assert( 
19435 6e 46 69 65 6c 64 3e 30 20 26 26 20 70 4f 70 2d  nField>0 && pOp-
19436 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32  >p2>0 && pOp->p2
19437 2b 6e 46 69 65 6c 64 3c 3d 70 2d 3e 6e 4d 65 6d  +nField<=p->nMem
19438 20 29 3b 0a 20 20 70 44 61 74 61 30 20 3d 20 26   );.  pData0 = &
19439 70 2d 3e 61 4d 65 6d 5b 6e 46 69 65 6c 64 5d 3b  p->aMem[nField];
1943a 0a 20 20 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d  .  nField = pOp-
1943b 3e 70 32 3b 0a 20 20 70 4c 61 73 74 20 3d 20 26  >p2;.  pLast = &
1943c 70 44 61 74 61 30 5b 6e 46 69 65 6c 64 2d 31 5d  pData0[nField-1]
1943d 3b 0a 20 20 66 69 6c 65 5f 66 6f 72 6d 61 74 20  ;.  file_format 
1943e 3d 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c  = p->minWriteFil
1943f 65 46 6f 72 6d 61 74 3b 0a 0a 20 20 2f 2a 20 4c  eFormat;..  /* L
19440 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20  oop through the 
19441 65 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 77 69  elements that wi
19442 6c 6c 20 6d 61 6b 65 20 75 70 20 74 68 65 20 72  ll make up the r
19443 65 63 6f 72 64 20 74 6f 20 66 69 67 75 72 65 0a  ecord to figure.
19444 20 20 2a 2a 20 6f 75 74 20 68 6f 77 20 6d 75 63    ** out how muc
19445 68 20 73 70 61 63 65 20 69 73 20 72 65 71 75 69  h space is requi
19446 72 65 64 20 66 6f 72 20 74 68 65 20 6e 65 77 20  red for the new 
19447 72 65 63 6f 72 64 2e 0a 20 20 2a 2f 0a 20 20 66  record..  */.  f
19448 6f 72 28 70 52 65 63 3d 70 44 61 74 61 30 3b 20  or(pRec=pData0; 
19449 70 52 65 63 3c 3d 70 4c 61 73 74 3b 20 70 52 65  pRec<=pLast; pRe
1944a 63 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6c 65  c++){.    int le
1944b 6e 3b 0a 20 20 20 20 69 66 28 20 7a 41 66 66 69  n;.    if( zAffi
1944c 6e 69 74 79 20 29 7b 0a 20 20 20 20 20 20 61 70  nity ){.      ap
1944d 70 6c 79 41 66 66 69 6e 69 74 79 28 70 52 65 63  plyAffinity(pRec
1944e 2c 20 7a 41 66 66 69 6e 69 74 79 5b 70 52 65 63  , zAffinity[pRec
1944f 2d 70 44 61 74 61 30 5d 2c 20 65 6e 63 6f 64 69  -pData0], encodi
19450 6e 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ng);.    }.    i
19451 66 28 20 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d  f( pRec->flags&M
19452 45 4d 5f 5a 65 72 6f 20 26 26 20 70 52 65 63 2d  EM_Zero && pRec-
19453 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71  >n>0 ){.      sq
19454 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61  lite3VdbeMemExpa
19455 6e 64 42 6c 6f 62 28 70 52 65 63 29 3b 0a 20 20  ndBlob(pRec);.  
19456 20 20 7d 0a 20 20 20 20 73 65 72 69 61 6c 5f 74    }.    serial_t
19457 79 70 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ype = sqlite3Vdb
19458 65 53 65 72 69 61 6c 54 79 70 65 28 70 52 65 63  eSerialType(pRec
19459 2c 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a  , file_format);.
1945a 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65      len = sqlite
1945b 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c  3VdbeSerialTypeL
1945c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 29 3b  en(serial_type);
1945d 0a 20 20 20 20 6e 44 61 74 61 20 2b 3d 20 6c 65  .    nData += le
1945e 6e 3b 0a 20 20 20 20 6e 48 64 72 20 2b 3d 20 73  n;.    nHdr += s
1945f 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28  qlite3VarintLen(
19460 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20  serial_type);.  
19461 20 20 69 66 28 20 70 52 65 63 2d 3e 66 6c 61 67    if( pRec->flag
19462 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a  s & MEM_Zero ){.
19463 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 70 75        /* Only pu
19464 72 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 42  re zero-filled B
19465 4c 4f 42 73 20 63 61 6e 20 62 65 20 69 6e 70 75  LOBs can be inpu
19466 74 20 74 6f 20 74 68 69 73 20 4f 70 63 6f 64 65  t to this Opcode
19467 2e 0a 20 20 20 20 20 20 2a 2a 20 57 65 20 64 6f  ..      ** We do
19468 20 6e 6f 74 20 61 6c 6c 6f 77 20 62 6c 6f 62 73   not allow blobs
19469 20 77 69 74 68 20 61 20 70 72 65 66 69 78 20 61   with a prefix a
1946a 6e 64 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64  nd a zero-filled
1946b 20 74 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 20 20   tail. */.      
1946c 6e 5a 65 72 6f 20 2b 3d 20 70 52 65 63 2d 3e 75  nZero += pRec->u
1946d 2e 69 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  .i;.    }else if
1946e 28 20 6c 65 6e 20 29 7b 0a 20 20 20 20 20 20 6e  ( len ){.      n
1946f 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  Zero = 0;.    }.
19470 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68    }..  /* Add th
19471 65 20 69 6e 69 74 69 61 6c 20 68 65 61 64 65 72  e initial header
19472 20 76 61 72 69 6e 74 20 61 6e 64 20 74 6f 74 61   varint and tota
19473 6c 20 74 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20  l the size */.  
19474 6e 48 64 72 20 2b 3d 20 6e 56 61 72 69 6e 74 20  nHdr += nVarint 
19475 3d 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c  = sqlite3VarintL
19476 65 6e 28 6e 48 64 72 29 3b 0a 20 20 69 66 28 20  en(nHdr);.  if( 
19477 6e 56 61 72 69 6e 74 3c 73 71 6c 69 74 65 33 56  nVarint<sqlite3V
19478 61 72 69 6e 74 4c 65 6e 28 6e 48 64 72 29 20 29  arintLen(nHdr) )
19479 7b 0a 20 20 20 20 6e 48 64 72 2b 2b 3b 0a 20 20  {.    nHdr++;.  
1947a 7d 0a 20 20 6e 42 79 74 65 20 3d 20 6e 48 64 72  }.  nByte = nHdr
1947b 2b 6e 44 61 74 61 2d 6e 5a 65 72 6f 3b 0a 20 20  +nData-nZero;.  
1947c 69 66 28 20 6e 42 79 74 65 3e 64 62 2d 3e 61 4c  if( nByte>db->aL
1947d 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
1947e 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20  T_LENGTH] ){.   
1947f 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20   goto too_big;. 
19480 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75   }..  /* Make su
19481 72 65 20 74 68 65 20 6f 75 74 70 75 74 20 72 65  re the output re
19482 67 69 73 74 65 72 20 68 61 73 20 61 20 62 75 66  gister has a buf
19483 66 65 72 20 6c 61 72 67 65 20 65 6e 6f 75 67 68  fer large enough
19484 20 74 6f 20 73 74 6f 72 65 20 0a 20 20 2a 2a 20   to store .  ** 
19485 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 2e 20  the new record. 
19486 54 68 65 20 6f 75 74 70 75 74 20 72 65 67 69 73  The output regis
19487 74 65 72 20 28 70 4f 70 2d 3e 70 33 29 20 69 73  ter (pOp->p3) is
19488 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 0a   not allowed to.
19489 20 20 2a 2a 20 62 65 20 6f 6e 65 20 6f 66 20 74    ** be one of t
1948a 68 65 20 69 6e 70 75 74 20 72 65 67 69 73 74 65  he input registe
1948b 72 73 20 28 62 65 63 61 75 73 65 20 74 68 65 20  rs (because the 
1948c 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 74  following call t
1948d 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64  o.  ** sqlite3Vd
1948e 62 65 4d 65 6d 47 72 6f 77 28 29 20 63 6f 75 6c  beMemGrow() coul
1948f 64 20 63 6c 6f 62 62 65 72 20 74 68 65 20 76 61  d clobber the va
19490 6c 75 65 20 62 65 66 6f 72 65 20 69 74 20 69 73  lue before it is
19491 20 75 73 65 64 29 2e 0a 20 20 2a 2f 0a 20 20 61   used)..  */.  a
19492 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 70  ssert( pOp->p3<p
19493 4f 70 2d 3e 70 31 20 7c 7c 20 70 4f 70 2d 3e 70  Op->p1 || pOp->p
19494 33 3e 3d 70 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e  3>=pOp->p1+pOp->
19495 70 32 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26  p2 );.  pOut = &
19496 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  p->aMem[pOp->p3]
19497 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56  ;.  if( sqlite3V
19498 64 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c  dbeMemGrow(pOut,
19499 20 6e 42 79 74 65 2c 20 30 29 20 29 7b 0a 20 20   nByte, 0) ){.  
1949a 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20    goto no_mem;. 
1949b 20 7d 0a 20 20 7a 4e 65 77 52 65 63 6f 72 64 20   }.  zNewRecord 
1949c 3d 20 28 75 38 20 2a 29 70 4f 75 74 2d 3e 7a 3b  = (u8 *)pOut->z;
1949d 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65  ..  /* Write the
1949e 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 20 3d   record */.  i =
1949f 20 70 75 74 56 61 72 69 6e 74 33 32 28 7a 4e 65   putVarint32(zNe
194a0 77 52 65 63 6f 72 64 2c 20 6e 48 64 72 29 3b 0a  wRecord, nHdr);.
194a1 20 20 66 6f 72 28 70 52 65 63 3d 70 44 61 74 61    for(pRec=pData
194a2 30 3b 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b 20  0; pRec<=pLast; 
194a3 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 73 65 72  pRec++){.    ser
194a4 69 61 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 74  ial_type = sqlit
194a5 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
194a6 28 70 52 65 63 2c 20 66 69 6c 65 5f 66 6f 72 6d  (pRec, file_form
194a7 61 74 29 3b 0a 20 20 20 20 69 20 2b 3d 20 70 75  at);.    i += pu
194a8 74 56 61 72 69 6e 74 33 32 28 26 7a 4e 65 77 52  tVarint32(&zNewR
194a9 65 63 6f 72 64 5b 69 5d 2c 20 73 65 72 69 61 6c  ecord[i], serial
194aa 5f 74 79 70 65 29 3b 20 20 20 20 20 20 2f 2a 20  _type);      /* 
194ab 73 65 72 69 61 6c 20 74 79 70 65 20 2a 2f 0a 20  serial type */. 
194ac 20 7d 0a 20 20 66 6f 72 28 70 52 65 63 3d 70 44   }.  for(pRec=pD
194ad 61 74 61 30 3b 20 70 52 65 63 3c 3d 70 4c 61 73  ata0; pRec<=pLas
194ae 74 3b 20 70 52 65 63 2b 2b 29 7b 20 20 2f 2a 20  t; pRec++){  /* 
194af 73 65 72 69 61 6c 20 64 61 74 61 20 2a 2f 0a 20  serial data */. 
194b0 20 20 20 69 20 2b 3d 20 73 71 6c 69 74 65 33 56     i += sqlite3V
194b1 64 62 65 53 65 72 69 61 6c 50 75 74 28 26 7a 4e  dbeSerialPut(&zN
194b2 65 77 52 65 63 6f 72 64 5b 69 5d 2c 20 6e 42 79  ewRecord[i], nBy
194b3 74 65 2d 69 2c 20 70 52 65 63 2c 20 66 69 6c 65  te-i, pRec, file
194b4 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 7d 0a 20 20  _format);.  }.  
194b5 61 73 73 65 72 74 28 20 69 3d 3d 6e 42 79 74 65  assert( i==nByte
194b6 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70   );..  assert( p
194b7 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d  Op->p3>0 && pOp-
194b8 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p3<=p->nMem );.
194b9 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 6e 42 79 74    pOut->n = nByt
194ba 65 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73  e;.  pOut->flags
194bb 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20 4d 45   = MEM_Blob | ME
194bc 4d 5f 44 79 6e 3b 0a 20 20 70 4f 75 74 2d 3e 78  M_Dyn;.  pOut->x
194bd 44 65 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e  Del = 0;.  if( n
194be 5a 65 72 6f 20 29 7b 0a 20 20 20 20 70 4f 75 74  Zero ){.    pOut
194bf 2d 3e 75 2e 69 20 3d 20 6e 5a 65 72 6f 3b 0a 20  ->u.i = nZero;. 
194c0 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c     pOut->flags |
194c1 3d 20 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 7d 0a  = MEM_Zero;.  }.
194c2 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 53 51    pOut->enc = SQ
194c3 4c 49 54 45 5f 55 54 46 38 3b 20 20 2f 2a 20 49  LITE_UTF8;  /* I
194c4 6e 20 63 61 73 65 20 74 68 65 20 62 6c 6f 62 20  n case the blob 
194c5 69 73 20 65 76 65 72 20 63 6f 6e 76 65 72 74 65  is ever converte
194c6 64 20 74 6f 20 74 65 78 74 20 2a 2f 0a 20 20 52  d to text */.  R
194c7 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
194c8 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20 20  p->p3, pOut);.  
194c9 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53  UPDATE_MAX_BLOBS
194ca 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65  IZE(pOut);.  bre
194cb 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
194cc 3a 20 53 74 61 74 65 6d 65 6e 74 20 50 31 20 2a  : Statement P1 *
194cd 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 65 67   * * *.**.** Beg
194ce 69 6e 20 61 6e 20 69 6e 64 69 76 69 64 75 61 6c  in an individual
194cf 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73   statement trans
194d0 61 63 74 69 6f 6e 20 77 68 69 63 68 20 69 73 20  action which is 
194d1 70 61 72 74 20 6f 66 20 61 20 6c 61 72 67 65 72  part of a larger
194d2 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  .** transaction.
194d3 20 20 54 68 69 73 20 69 73 20 6e 65 65 64 65 64    This is needed
194d4 20 73 6f 20 74 68 61 74 20 74 68 65 20 73 74 61   so that the sta
194d5 74 65 6d 65 6e 74 0a 2a 2a 20 63 61 6e 20 62 65  tement.** can be
194d6 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 66 74   rolled back aft
194d7 65 72 20 61 6e 20 65 72 72 6f 72 20 77 69 74 68  er an error with
194d8 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 72 6f  out having to ro
194d9 6c 6c 20 62 61 63 6b 20 74 68 65 0a 2a 2a 20 65  ll back the.** e
194da 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f  ntire transactio
194db 6e 2e 20 20 54 68 65 20 73 74 61 74 65 6d 65 6e  n.  The statemen
194dc 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69  t transaction wi
194dd 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ll automatically
194de 0a 2a 2a 20 63 6f 6d 6d 69 74 20 77 68 65 6e 20  .** commit when 
194df 74 68 65 20 56 44 42 45 20 68 61 6c 74 73 2e 0a  the VDBE halts..
194e0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74  **.** If the dat
194e1 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
194e2 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e   is currently in
194e3 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65   autocommit mode
194e4 20 28 74 68 61 74 20 0a 2a 2a 20 69 73 20 74 6f   (that .** is to
194e5 20 73 61 79 2c 20 69 66 20 69 74 20 69 73 20 69   say, if it is i
194e6 6e 20 62 65 74 77 65 65 6e 20 42 45 47 49 4e 20  n between BEGIN 
194e7 61 6e 64 20 43 4f 4d 4d 49 54 29 0a 2a 2a 20 61  and COMMIT).** a
194e8 6e 64 20 69 66 20 74 68 65 72 65 20 61 72 65 20  nd if there are 
194e9 6e 6f 20 6f 74 68 65 72 20 61 63 74 69 76 65 20  no other active 
194ea 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 20 74 68  statements on th
194eb 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 0a  e same database.
194ec 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74  ** connection, t
194ed 68 65 6e 20 74 68 69 73 20 6f 70 65 72 61 74 69  hen this operati
194ee 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20  on is a no-op.  
194ef 4e 6f 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61  No statement tra
194f0 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 6e  nsaction.** is n
194f1 65 65 64 65 64 20 73 69 6e 63 65 20 61 6e 79 20  eeded since any 
194f2 65 72 72 6f 72 20 63 61 6e 20 75 73 65 20 74 68  error can use th
194f3 65 20 6e 6f 72 6d 61 6c 20 52 4f 4c 4c 42 41 43  e normal ROLLBAC
194f4 4b 20 70 72 6f 63 65 73 73 20 74 6f 0a 2a 2a 20  K process to.** 
194f5 75 6e 64 6f 20 63 68 61 6e 67 65 73 2e 0a 2a 2a  undo changes..**
194f6 0a 2a 2a 20 49 66 20 61 20 73 74 61 74 65 6d 65  .** If a stateme
194f7 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  nt transaction i
194f8 73 20 73 74 61 72 74 65 64 2c 20 74 68 65 6e 20  s started, then 
194f9 61 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72  a statement jour
194fa 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 77 69 6c 6c  nal file.** will
194fb 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e   be allocated an
194fc 64 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a  d initialized..*
194fd 2a 0a 2a 2a 20 54 68 65 20 73 74 61 74 65 6d 65  *.** The stateme
194fe 6e 74 20 69 73 20 62 65 67 75 6e 20 6f 6e 20 74  nt is begun on t
194ff 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
19500 20 77 69 74 68 20 69 6e 64 65 78 20 50 31 2e 20   with index P1. 
19501 20 54 68 65 20 6d 61 69 6e 0a 2a 2a 20 64 61 74   The main.** dat
19502 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 61  abase file has a
19503 6e 20 69 6e 64 65 78 20 6f 66 20 30 20 61 6e 64  n index of 0 and
19504 20 74 68 65 20 66 69 6c 65 20 75 73 65 64 20 66   the file used f
19505 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  or temporary tab
19506 6c 65 73 0a 2a 2a 20 68 61 73 20 61 6e 20 69 6e  les.** has an in
19507 64 65 78 20 6f 66 20 31 2e 0a 2a 2f 0a 63 61 73  dex of 1..*/.cas
19508 65 20 4f 50 5f 53 74 61 74 65 6d 65 6e 74 3a 20  e OP_Statement: 
19509 7b 0a 20 20 69 66 28 20 64 62 2d 3e 61 75 74 6f  {.  if( db->auto
1950a 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 62 2d  Commit==0 || db-
1950b 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e 31  >activeVdbeCnt>1
1950c 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20   ){.    int i = 
1950d 70 4f 70 2d 3e 70 31 3b 0a 20 20 20 20 42 74 72  pOp->p1;.    Btr
1950e 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 61 73 73  ee *pBt;.    ass
1950f 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 64  ert( i>=0 && i<d
19510 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 20 20 61 73  b->nDb );.    as
19511 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b 69 5d  sert( db->aDb[i]
19512 2e 70 42 74 21 3d 30 20 29 3b 0a 20 20 20 20 70  .pBt!=0 );.    p
19513 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e  Bt = db->aDb[i].
19514 70 42 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28  pBt;.    assert(
19515 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49   sqlite3BtreeIsI
19516 6e 54 72 61 6e 73 28 70 42 74 29 20 29 3b 0a 20  nTrans(pBt) );. 
19517 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62     assert( (p->b
19518 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69  treeMask & (1<<i
19519 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  ))!=0 );.    if(
1951a 20 21 73 71 6c 69 74 65 33 42 74 72 65 65 49 73   !sqlite3BtreeIs
1951b 49 6e 53 74 6d 74 28 70 42 74 29 20 29 7b 0a 20  InStmt(pBt) ){. 
1951c 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1951d 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28  3BtreeBeginStmt(
1951e 70 42 74 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f  pBt);.      p->o
1951f 70 65 6e 65 64 53 74 61 74 65 6d 65 6e 74 20 3d  penedStatement =
19520 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
19521 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
19522 6f 64 65 3a 20 41 75 74 6f 43 6f 6d 6d 69 74 20  ode: AutoCommit 
19523 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
19524 2a 20 53 65 74 20 74 68 65 20 64 61 74 61 62 61  * Set the databa
19525 73 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66  se auto-commit f
19526 6c 61 67 20 74 6f 20 50 31 20 28 31 20 6f 72 20  lag to P1 (1 or 
19527 30 29 2e 20 49 66 20 50 32 20 69 73 20 74 72 75  0). If P2 is tru
19528 65 2c 20 72 6f 6c 6c 0a 2a 2a 20 62 61 63 6b 20  e, roll.** back 
19529 61 6e 79 20 63 75 72 72 65 6e 74 6c 79 20 61 63  any currently ac
1952a 74 69 76 65 20 62 74 72 65 65 20 74 72 61 6e 73  tive btree trans
1952b 61 63 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 72  actions. If ther
1952c 65 20 61 72 65 20 61 6e 79 20 61 63 74 69 76 65  e are any active
1952d 0a 2a 2a 20 56 4d 73 20 28 61 70 61 72 74 20 66  .** VMs (apart f
1952e 72 6f 6d 20 74 68 69 73 20 6f 6e 65 29 2c 20 74  rom this one), t
1952f 68 65 6e 20 74 68 65 20 43 4f 4d 4d 49 54 20 6f  hen the COMMIT o
19530 72 20 52 4f 4c 4c 42 41 43 4b 20 73 74 61 74 65  r ROLLBACK state
19531 6d 65 6e 74 20 66 61 69 6c 73 2e 0a 2a 2a 0a 2a  ment fails..**.*
19532 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69  * This instructi
19533 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20 56 4d  on causes the VM
19534 20 74 6f 20 68 61 6c 74 2e 0a 2a 2f 0a 63 61 73   to halt..*/.cas
19535 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 3a  e OP_AutoCommit:
19536 20 7b 0a 20 20 69 6e 74 20 64 65 73 69 72 65 64   {.  int desired
19537 41 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 70 4f 70  AutoCommit = pOp
19538 2d 3e 70 31 3b 0a 20 20 69 6e 74 20 72 6f 6c 6c  ->p1;.  int roll
19539 62 61 63 6b 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a  back = pOp->p2;.
1953a 20 20 69 6e 74 20 74 75 72 6e 4f 6e 41 43 20 3d    int turnOnAC =
1953b 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d   desiredAutoComm
1953c 69 74 20 26 26 20 21 64 62 2d 3e 61 75 74 6f 43  it && !db->autoC
1953d 6f 6d 6d 69 74 3b 0a 0a 20 20 61 73 73 65 72 74  ommit;..  assert
1953e 28 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d  ( desiredAutoCom
1953f 6d 69 74 3d 3d 31 20 7c 7c 20 64 65 73 69 72 65  mit==1 || desire
19540 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29  dAutoCommit==0 )
19541 3b 0a 20 20 61 73 73 65 72 74 28 20 64 65 73 69  ;.  assert( desi
19542 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31  redAutoCommit==1
19543 20 7c 7c 20 72 6f 6c 6c 62 61 63 6b 3d 3d 30 20   || rollback==0 
19544 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62  );..  assert( db
19545 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e  ->activeVdbeCnt>
19546 30 20 29 3b 20 20 2f 2a 20 41 74 20 6c 65 61 73  0 );  /* At leas
19547 74 20 74 68 69 73 20 6f 6e 65 20 56 4d 20 69 73  t this one VM is
19548 20 61 63 74 69 76 65 20 2a 2f 0a 0a 20 20 69 66   active */..  if
19549 28 20 74 75 72 6e 4f 6e 41 43 20 26 26 20 72 6f  ( turnOnAC && ro
1954a 6c 6c 62 61 63 6b 20 26 26 20 64 62 2d 3e 61 63  llback && db->ac
1954b 74 69 76 65 56 64 62 65 43 6e 74 3e 31 20 29 7b  tiveVdbeCnt>1 ){
1954c 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20  .    /* If this 
1954d 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c  instruction impl
1954e 65 6d 65 6e 74 73 20 61 20 52 4f 4c 4c 42 41 43  ements a ROLLBAC
1954f 4b 20 61 6e 64 20 6f 74 68 65 72 20 56 4d 73 20  K and other VMs 
19550 61 72 65 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c  are.    ** still
19551 20 72 75 6e 6e 69 6e 67 2c 20 61 6e 64 20 61 20   running, and a 
19552 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61  transaction is a
19553 63 74 69 76 65 2c 20 72 65 74 75 72 6e 20 61 6e  ctive, return an
19554 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74 69 6e   error indicatin
19555 67 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68  g.    ** that th
19556 65 20 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 74  e other VMs must
19557 20 63 6f 6d 70 6c 65 74 65 20 66 69 72 73 74 2e   complete first.
19558 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c   .    */.    sql
19559 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70  ite3SetString(&p
1955a 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22  ->zErrMsg, db, "
1955b 63 61 6e 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20  cannot rollback 
1955c 74 72 61 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a  transaction - ".
1955d 20 20 20 20 20 20 20 20 22 53 51 4c 20 73 74 61          "SQL sta
1955e 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72  tements in progr
1955f 65 73 73 22 29 3b 0a 20 20 20 20 72 63 20 3d 20  ess");.    rc = 
19560 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d  SQLITE_BUSY;.  }
19561 65 6c 73 65 20 69 66 28 20 74 75 72 6e 4f 6e 41  else if( turnOnA
19562 43 20 26 26 20 21 72 6f 6c 6c 62 61 63 6b 20 26  C && !rollback &
19563 26 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43  & db->writeVdbeC
19564 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49  nt>1 ){.    /* I
19565 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69  f this instructi
19566 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20  on implements a 
19567 43 4f 4d 4d 49 54 20 61 6e 64 20 6f 74 68 65 72  COMMIT and other
19568 20 56 4d 73 20 61 72 65 20 77 72 69 74 69 6e 67   VMs are writing
19569 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 61  .    ** return a
1956a 6e 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74 69  n error indicati
1956b 6e 67 20 74 68 61 74 20 74 68 65 20 6f 74 68 65  ng that the othe
1956c 72 20 56 4d 73 20 6d 75 73 74 20 63 6f 6d 70 6c  r VMs must compl
1956d 65 74 65 20 66 69 72 73 74 2e 20 0a 20 20 20 20  ete first. .    
1956e 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  */.    sqlite3Se
1956f 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72  tString(&p->zErr
19570 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74  Msg, db, "cannot
19571 20 63 6f 6d 6d 69 74 20 74 72 61 6e 73 61 63 74   commit transact
19572 69 6f 6e 20 2d 20 22 0a 20 20 20 20 20 20 20 20  ion - ".        
19573 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20  "SQL statements 
19574 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b 0a 20  in progress");. 
19575 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
19576 55 53 59 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  USY;.  }else if(
19577 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d   desiredAutoComm
19578 69 74 21 3d 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d  it!=db->autoComm
19579 69 74 20 29 7b 0a 20 20 20 20 69 66 28 20 70 4f  it ){.    if( pO
1957a 70 2d 3e 70 32 20 29 7b 0a 20 20 20 20 20 20 61  p->p2 ){.      a
1957b 73 73 65 72 74 28 20 64 65 73 69 72 65 64 41 75  ssert( desiredAu
1957c 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 29 3b 0a 20  toCommit==1 );. 
1957d 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c       sqlite3Roll
1957e 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20  backAll(db);.   
1957f 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69     db->autoCommi
19580 74 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65  t = 1;.    }else
19581 7b 0a 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f  {.      db->auto
19582 43 6f 6d 6d 69 74 20 3d 20 64 65 73 69 72 65 64  Commit = desired
19583 41 75 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 20 20  AutoCommit;.    
19584 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
19585 65 48 61 6c 74 28 70 29 3d 3d 53 51 4c 49 54 45  eHalt(p)==SQLITE
19586 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20  _BUSY ){.       
19587 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20   p->pc = pc;.   
19588 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d       db->autoCom
19589 6d 69 74 20 3d 20 31 2d 64 65 73 69 72 65 64 41  mit = 1-desiredA
1958a 75 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 20 20 20  utoCommit;.     
1958b 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20     p->rc = rc = 
1958c 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20  SQLITE_BUSY;.   
1958d 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72       goto vdbe_r
1958e 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
1958f 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e     }.    if( p->
19590 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
19591 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
19592 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 65 6c  TE_DONE;.    }el
19593 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  se{.      rc = S
19594 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
19595 20 7d 0a 20 20 20 20 67 6f 74 6f 20 76 64 62 65   }.    goto vdbe
19596 5f 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65  _return;.  }else
19597 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74  {.    sqlite3Set
19598 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d  String(&p->zErrM
19599 73 67 2c 20 64 62 2c 0a 20 20 20 20 20 20 20 20  sg, db,.        
1959a 28 21 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d  (!desiredAutoCom
1959b 6d 69 74 29 3f 22 63 61 6e 6e 6f 74 20 73 74 61  mit)?"cannot sta
1959c 72 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  rt a transaction
1959d 20 77 69 74 68 69 6e 20 61 20 74 72 61 6e 73 61   within a transa
1959e 63 74 69 6f 6e 22 3a 28 0a 20 20 20 20 20 20 20  ction":(.       
1959f 20 28 72 6f 6c 6c 62 61 63 6b 29 3f 22 63 61 6e   (rollback)?"can
195a0 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 2d 20 6e  not rollback - n
195a1 6f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  o transaction is
195a2 20 61 63 74 69 76 65 22 3a 0a 20 20 20 20 20 20   active":.      
195a3 20 20 20 20 20 20 20 20 20 20 20 20 20 22 63 61               "ca
195a4 6e 6e 6f 74 20 63 6f 6d 6d 69 74 20 2d 20 6e 6f  nnot commit - no
195a5 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
195a6 61 63 74 69 76 65 22 29 29 3b 0a 20 20 20 20 20  active"));.     
195a7 20 20 20 20 0a 20 20 20 20 72 63 20 3d 20 53 51      .    rc = SQ
195a8 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
195a9 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
195aa 70 63 6f 64 65 3a 20 54 72 61 6e 73 61 63 74 69  pcode: Transacti
195ab 6f 6e 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  on P1 P2 * * *.*
195ac 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20 74 72 61  *.** Begin a tra
195ad 6e 73 61 63 74 69 6f 6e 2e 20 20 54 68 65 20 74  nsaction.  The t
195ae 72 61 6e 73 61 63 74 69 6f 6e 20 65 6e 64 73 20  ransaction ends 
195af 77 68 65 6e 20 61 20 43 6f 6d 6d 69 74 20 6f 72  when a Commit or
195b0 20 52 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 70 63   Rollback.** opc
195b1 6f 64 65 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  ode is encounter
195b2 65 64 2e 20 20 44 65 70 65 6e 64 69 6e 67 20 6f  ed.  Depending o
195b3 6e 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43  n the ON CONFLIC
195b4 54 20 73 65 74 74 69 6e 67 2c 20 74 68 65 0a 2a  T setting, the.*
195b5 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 69  * transaction mi
195b6 67 68 74 20 61 6c 73 6f 20 62 65 20 72 6f 6c 6c  ght also be roll
195b7 65 64 20 62 61 63 6b 20 69 66 20 61 6e 20 65 72  ed back if an er
195b8 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  ror is encounter
195b9 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20  ed..**.** P1 is 
195ba 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
195bb 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f   database file o
195bc 6e 20 77 68 69 63 68 20 74 68 65 20 74 72 61 6e  n which the tran
195bd 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 73 74  saction is.** st
195be 61 72 74 65 64 2e 20 20 49 6e 64 65 78 20 30 20  arted.  Index 0 
195bf 69 73 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  is the main data
195c0 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 69 6e  base file and in
195c1 64 65 78 20 31 20 69 73 20 74 68 65 0a 2a 2a 20  dex 1 is the.** 
195c2 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20 74 65  file used for te
195c3 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20  mporary tables. 
195c4 20 49 6e 64 69 63 65 73 20 6f 66 20 32 20 6f 72   Indices of 2 or
195c5 20 6d 6f 72 65 20 61 72 65 20 75 73 65 64 20 66   more are used f
195c6 6f 72 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 64  or.** attached d
195c7 61 74 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20  atabases..**.** 
195c8 49 66 20 50 32 20 69 73 20 6e 6f 6e 2d 7a 65 72  If P2 is non-zer
195c9 6f 2c 20 74 68 65 6e 20 61 20 77 72 69 74 65 2d  o, then a write-
195ca 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73  transaction is s
195cb 74 61 72 74 65 64 2e 20 20 41 20 52 45 53 45 52  tarted.  A RESER
195cc 56 45 44 20 6c 6f 63 6b 20 69 73 0a 2a 2a 20 6f  VED lock is.** o
195cd 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 64  btained on the d
195ce 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 68 65  atabase file whe
195cf 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61  n a write-transa
195d0 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 65 64  ction is started
195d1 2e 20 20 4e 6f 0a 2a 2a 20 6f 74 68 65 72 20 70  .  No.** other p
195d2 72 6f 63 65 73 73 20 63 61 6e 20 73 74 61 72 74  rocess can start
195d3 20 61 6e 6f 74 68 65 72 20 77 72 69 74 65 20 74   another write t
195d4 72 61 6e 73 61 63 74 69 6f 6e 20 77 68 69 6c 65  ransaction while
195d5 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f   this transactio
195d6 6e 20 69 73 0a 2a 2a 20 75 6e 64 65 72 77 61 79  n is.** underway
195d7 2e 20 20 53 74 61 72 74 69 6e 67 20 61 20 77 72  .  Starting a wr
195d8 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ite transaction 
195d9 61 6c 73 6f 20 63 72 65 61 74 65 73 20 61 20 72  also creates a r
195da 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e  ollback journal.
195db 20 41 0a 2a 2a 20 77 72 69 74 65 20 74 72 61 6e   A.** write tran
195dc 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20  saction must be 
195dd 73 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 61  started before a
195de 6e 79 20 63 68 61 6e 67 65 73 20 63 61 6e 20 62  ny changes can b
195df 65 20 6d 61 64 65 20 74 6f 20 74 68 65 0a 2a 2a  e made to the.**
195e0 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 20 50   database.  If P
195e1 32 20 69 73 20 32 20 6f 72 20 67 72 65 61 74 65  2 is 2 or greate
195e2 72 20 74 68 65 6e 20 61 6e 20 45 58 43 4c 55 53  r then an EXCLUS
195e3 49 56 45 20 6c 6f 63 6b 20 69 73 20 61 6c 73 6f  IVE lock is also
195e4 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20   obtained.** on 
195e5 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  the file..**.** 
195e6 49 66 20 50 32 20 69 73 20 7a 65 72 6f 2c 20 74  If P2 is zero, t
195e7 68 65 6e 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20  hen a read-lock 
195e8 69 73 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74  is obtained on t
195e9 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
195ea 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 72 61  ..*/.case OP_Tra
195eb 6e 73 61 63 74 69 6f 6e 3a 20 7b 0a 20 20 69 6e  nsaction: {.  in
195ec 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20  t i = pOp->p1;. 
195ed 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 0a 20 20   Btree *pBt;..  
195ee 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
195ef 69 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61  i<db->nDb );.  a
195f0 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65  ssert( (p->btree
195f1 4d 61 73 6b 20 26 20 28 31 3c 3c 69 29 29 21 3d  Mask & (1<<i))!=
195f2 30 20 29 3b 0a 20 20 70 42 74 20 3d 20 64 62 2d  0 );.  pBt = db-
195f3 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 0a 20 20  >aDb[i].pBt;..  
195f4 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 72  if( pBt ){.    r
195f5 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
195f6 42 65 67 69 6e 54 72 61 6e 73 28 70 42 74 2c 20  BeginTrans(pBt, 
195f7 70 4f 70 2d 3e 70 32 29 3b 0a 20 20 20 20 69 66  pOp->p2);.    if
195f8 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53  ( rc==SQLITE_BUS
195f9 59 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 63  Y ){.      p->pc
195fa 20 3d 20 70 63 3b 0a 20 20 20 20 20 20 70 2d 3e   = pc;.      p->
195fb 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45  rc = rc = SQLITE
195fc 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 67 6f 74  _BUSY;.      got
195fd 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20  o vdbe_return;. 
195fe 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21     }.    if( rc!
195ff 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63  =SQLITE_OK && rc
19600 21 3d 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c  !=SQLITE_READONL
19601 59 20 2f 2a 20 26 26 20 72 63 21 3d 53 51 4c 49  Y /* && rc!=SQLI
19602 54 45 5f 42 55 53 59 20 2a 2f 20 29 7b 0a 20 20  TE_BUSY */ ){.  
19603 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64      goto abort_d
19604 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
19605 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a   }.  }.  break;.
19606 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65  }../* Opcode: Re
19607 61 64 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 50  adCookie P1 P2 P
19608 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64  3 * *.**.** Read
19609 20 63 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50   cookie number P
1960a 33 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20  3 from database 
1960b 50 31 20 61 6e 64 20 77 72 69 74 65 20 69 74 20  P1 and write it 
1960c 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32  into register P2
1960d 2e 0a 2a 2a 20 50 33 3d 3d 30 20 69 73 20 74 68  ..** P3==0 is th
1960e 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e  e schema version
1960f 2e 20 20 50 33 3d 3d 31 20 69 73 20 74 68 65 20  .  P3==1 is the 
19610 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61 74 2e  database format.
19611 0a 2a 2a 20 50 33 3d 3d 32 20 69 73 20 74 68 65  .** P3==2 is the
19612 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67   recommended pag
19613 65 72 20 63 61 63 68 65 20 73 69 7a 65 2c 20 61  er cache size, a
19614 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 31  nd so forth.  P1
19615 3d 3d 30 20 69 73 0a 2a 2a 20 74 68 65 20 6d 61  ==0 is.** the ma
19616 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
19617 20 61 6e 64 20 50 31 3d 3d 31 20 69 73 20 74 68   and P1==1 is th
19618 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
19619 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a  used to store.**
1961a 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
1961b 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 31 20 69  s..**.** If P1 i
1961c 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e  s negative, then
1961d 20 74 68 69 73 20 69 73 20 61 20 72 65 71 75 65   this is a reque
1961e 73 74 20 74 6f 20 72 65 61 64 20 74 68 65 20 73  st to read the s
1961f 69 7a 65 20 6f 66 20 61 0a 2a 2a 20 64 61 74 61  ize of a.** data
19620 62 61 73 65 73 20 66 72 65 65 2d 6c 69 73 74 2e  bases free-list.
19621 20 50 33 20 6d 75 73 74 20 62 65 20 73 65 74 20   P3 must be set 
19622 74 6f 20 31 20 69 6e 20 74 68 69 73 20 63 61 73  to 1 in this cas
19623 65 2e 20 54 68 65 20 61 63 74 75 61 6c 0a 2a 2a  e. The actual.**
19624 20 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73   database access
19625 65 64 20 69 73 20 28 28 50 31 2b 31 29 2a 2d 31  ed is ((P1+1)*-1
19626 29 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  ). For example, 
19627 61 20 50 31 20 70 61 72 61 6d 65 74 65 72 20 6f  a P1 parameter o
19628 66 20 2d 31 0a 2a 2a 20 63 6f 72 72 65 73 70 6f  f -1.** correspo
19629 6e 64 73 20 74 6f 20 64 61 74 61 62 61 73 65 20  nds to database 
1962a 30 20 28 22 6d 61 69 6e 22 29 2c 20 61 20 50 31  0 ("main"), a P1
1962b 20 6f 66 20 2d 32 20 69 73 20 64 61 74 61 62 61   of -2 is databa
1962c 73 65 20 31 20 28 22 74 65 6d 70 22 29 2e 0a 2a  se 1 ("temp")..*
1962d 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20  *.** There must 
1962e 62 65 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f  be a read-lock o
1962f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28  n the database (
19630 65 69 74 68 65 72 20 61 20 74 72 61 6e 73 61 63  either a transac
19631 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20  tion.** must be 
19632 73 74 61 72 74 65 64 20 6f 72 20 74 68 65 72 65  started or there
19633 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 65 6e   must be an open
19634 20 63 75 72 73 6f 72 29 20 62 65 66 6f 72 65 0a   cursor) before.
19635 2a 2a 20 65 78 65 63 75 74 69 6e 67 20 74 68 69  ** executing thi
19636 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  s instruction..*
19637 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 64 43 6f  /.case OP_ReadCo
19638 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 20 20 20  okie: {         
19639 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
1963a 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e  erelease */.  in
1963b 74 20 69 4d 65 74 61 3b 0a 20 20 69 6e 74 20 69  t iMeta;.  int i
1963c 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20  Db = pOp->p1;.  
1963d 69 6e 74 20 69 43 6f 6f 6b 69 65 20 3d 20 70 4f  int iCookie = pO
1963e 70 2d 3e 70 33 3b 0a 0a 20 20 61 73 73 65 72 74  p->p3;..  assert
1963f 28 20 70 4f 70 2d 3e 70 33 3c 53 51 4c 49 54 45  ( pOp->p3<SQLITE
19640 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41 20 29 3b  _N_BTREE_META );
19641 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29 7b 0a  .  if( iDb<0 ){.
19642 20 20 20 20 69 44 62 20 3d 20 28 2d 31 2a 28 69      iDb = (-1*(i
19643 44 62 2b 31 29 29 3b 0a 20 20 20 20 69 43 6f 6f  Db+1));.    iCoo
19644 6b 69 65 20 2a 3d 20 2d 31 3b 0a 20 20 7d 0a 20  kie *= -1;.  }. 
19645 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20   assert( iDb>=0 
19646 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29  && iDb<db->nDb )
19647 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e  ;.  assert( db->
19648 61 44 62 5b 69 44 62 5d 2e 70 42 74 21 3d 30 20  aDb[iDb].pBt!=0 
19649 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d  );.  assert( (p-
1964a 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c  >btreeMask & (1<
1964b 3c 69 44 62 29 29 21 3d 30 20 29 3b 0a 20 20 2f  <iDb))!=0 );.  /
1964c 2a 20 54 68 65 20 69 6e 64 65 78 69 6e 67 20 6f  * The indexing o
1964d 66 20 6d 65 74 61 20 76 61 6c 75 65 73 20 61 74  f meta values at
1964e 20 74 68 65 20 73 63 68 65 6d 61 20 6c 61 79 65   the schema laye
1964f 72 20 69 73 20 6f 66 66 20 62 79 20 6f 6e 65 20  r is off by one 
19650 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 69 6e  from.  ** the in
19651 64 65 78 69 6e 67 20 69 6e 20 74 68 65 20 62 74  dexing in the bt
19652 72 65 65 20 6c 61 79 65 72 2e 20 20 54 68 65 20  ree layer.  The 
19653 62 74 72 65 65 20 63 6f 6e 73 69 64 65 72 73 20  btree considers 
19654 6d 65 74 61 5b 30 5d 20 74 6f 0a 20 20 2a 2a 20  meta[0] to.  ** 
19655 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  be the number of
19656 20 66 72 65 65 20 70 61 67 65 73 20 69 6e 20 74   free pages in t
19657 68 65 20 64 61 74 61 62 61 73 65 20 28 61 20 72  he database (a r
19658 65 61 64 2d 6f 6e 6c 79 20 76 61 6c 75 65 29 0a  ead-only value).
19659 20 20 2a 2a 20 61 6e 64 20 6d 65 74 61 5b 31 5d    ** and meta[1]
1965a 20 74 6f 20 62 65 20 74 68 65 20 73 63 68 65 6d   to be the schem
1965b 61 20 63 6f 6f 6b 69 65 2e 20 20 54 68 65 20 73  a cookie.  The s
1965c 63 68 65 6d 61 20 6c 61 79 65 72 20 63 6f 6e 73  chema layer cons
1965d 69 64 65 72 73 0a 20 20 2a 2a 20 6d 65 74 61 5b  iders.  ** meta[
1965e 31 5d 20 74 6f 20 62 65 20 74 68 65 20 73 63 68  1] to be the sch
1965f 65 6d 61 20 63 6f 6f 6b 69 65 2e 20 20 53 6f 20  ema cookie.  So 
19660 77 65 20 68 61 76 65 20 74 6f 20 73 68 69 66 74  we have to shift
19661 20 74 68 65 20 69 6e 64 65 78 0a 20 20 2a 2a 20   the index.  ** 
19662 62 79 20 6f 6e 65 20 69 6e 20 74 68 65 20 66 6f  by one in the fo
19663 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e  llowing statemen
19664 74 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73  t..  */.  rc = s
19665 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65  qlite3BtreeGetMe
19666 74 61 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  ta(db->aDb[iDb].
19667 70 42 74 2c 20 31 20 2b 20 69 43 6f 6f 6b 69 65  pBt, 1 + iCookie
19668 2c 20 28 75 33 32 20 2a 29 26 69 4d 65 74 61 29  , (u32 *)&iMeta)
19669 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20  ;.  pOut->u.i = 
1966a 69 4d 65 74 61 3b 0a 20 20 4d 65 6d 53 65 74 54  iMeta;.  MemSetT
1966b 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45  ypeFlag(pOut, ME
1966c 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b  M_Int);.  break;
1966d 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53  .}../* Opcode: S
1966e 65 74 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 50  etCookie P1 P2 P
1966f 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74  3 * *.**.** Writ
19670 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  e the content of
19671 20 72 65 67 69 73 74 65 72 20 50 33 20 28 69 6e   register P3 (in
19672 74 65 72 70 72 65 74 65 64 20 61 73 20 61 6e 20  terpreted as an 
19673 69 6e 74 65 67 65 72 29 0a 2a 2a 20 69 6e 74 6f  integer).** into
19674 20 63 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50   cookie number P
19675 32 20 6f 66 20 64 61 74 61 62 61 73 65 20 50 31  2 of database P1
19676 2e 0a 2a 2a 20 50 32 3d 3d 30 20 69 73 20 74 68  ..** P2==0 is th
19677 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e  e schema version
19678 2e 20 20 50 32 3d 3d 31 20 69 73 20 74 68 65 20  .  P2==1 is the 
19679 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61 74 2e  database format.
1967a 0a 2a 2a 20 50 32 3d 3d 32 20 69 73 20 74 68 65  .** P2==2 is the
1967b 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67   recommended pag
1967c 65 72 20 63 61 63 68 65 20 73 69 7a 65 2c 20 61  er cache size, a
1967d 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 31  nd so forth.  P1
1967e 3d 3d 30 20 69 73 0a 2a 2a 20 74 68 65 20 6d 61  ==0 is.** the ma
1967f 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
19680 20 61 6e 64 20 50 31 3d 3d 31 20 69 73 20 74 68   and P1==1 is th
19681 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
19682 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a  used to store.**
19683 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
19684 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61 6e 73  s..**.** A trans
19685 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73  action must be s
19686 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 65 78  tarted before ex
19687 65 63 75 74 69 6e 67 20 74 68 69 73 20 6f 70 63  ecuting this opc
19688 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ode..*/.case OP_
19689 53 65 74 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20  SetCookie: {    
1968a 20 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a 20 20 44     /* in3 */.  D
1968b 62 20 2a 70 44 62 3b 0a 20 20 61 73 73 65 72 74  b *pDb;.  assert
1968c 28 20 70 4f 70 2d 3e 70 32 3c 53 51 4c 49 54 45  ( pOp->p2<SQLITE
1968d 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41 20 29 3b  _N_BTREE_META );
1968e 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1968f 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31  p1>=0 && pOp->p1
19690 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73  <db->nDb );.  as
19691 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d  sert( (p->btreeM
19692 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70  ask & (1<<pOp->p
19693 31 29 29 21 3d 30 20 29 3b 0a 20 20 70 44 62 20  1))!=0 );.  pDb 
19694 3d 20 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e  = &db->aDb[pOp->
19695 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  p1];.  assert( p
19696 44 62 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20  Db->pBt!=0 );.  
19697 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e  sqlite3VdbeMemIn
19698 74 65 67 65 72 69 66 79 28 70 49 6e 33 29 3b 0a  tegerify(pIn3);.
19699 20 20 2f 2a 20 53 65 65 20 6e 6f 74 65 20 61 62    /* See note ab
1969a 6f 75 74 20 69 6e 64 65 78 20 73 68 69 66 74 69  out index shifti
1969b 6e 67 20 6f 6e 20 4f 50 5f 52 65 61 64 43 6f 6f  ng on OP_ReadCoo
1969c 6b 69 65 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71  kie */.  rc = sq
1969d 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65  lite3BtreeUpdate
1969e 4d 65 74 61 28 70 44 62 2d 3e 70 42 74 2c 20 31  Meta(pDb->pBt, 1
1969f 2b 70 4f 70 2d 3e 70 32 2c 20 28 69 6e 74 29 70  +pOp->p2, (int)p
196a0 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 69 66 28  In3->u.i);.  if(
196a1 20 70 4f 70 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20   pOp->p2==0 ){. 
196a2 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65 20 73     /* When the s
196a3 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63 68 61  chema cookie cha
196a4 6e 67 65 73 2c 20 72 65 63 6f 72 64 20 74 68 65  nges, record the
196a5 20 6e 65 77 20 63 6f 6f 6b 69 65 20 69 6e 74 65   new cookie inte
196a6 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 70 44  rnally */.    pD
196a7 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65  b->pSchema->sche
196a8 6d 61 5f 63 6f 6f 6b 69 65 20 3d 20 70 49 6e 33  ma_cookie = pIn3
196a9 2d 3e 75 2e 69 3b 0a 20 20 20 20 64 62 2d 3e 66  ->u.i;.    db->f
196aa 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49  lags |= SQLITE_I
196ab 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20 20  nternChanges;.  
196ac 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70  }else if( pOp->p
196ad 32 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 52  2==1 ){.    /* R
196ae 65 63 6f 72 64 20 63 68 61 6e 67 65 73 20 69 6e  ecord changes in
196af 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74   the file format
196b0 20 2a 2f 0a 20 20 20 20 70 44 62 2d 3e 70 53 63   */.    pDb->pSc
196b1 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61  hema->file_forma
196b2 74 20 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20  t = pIn3->u.i;. 
196b3 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31   }.  if( pOp->p1
196b4 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e  ==1 ){.    /* In
196b5 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 70 72 65  validate all pre
196b6 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73  pared statements
196b7 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 54 45   whenever the TE
196b8 4d 50 20 64 61 74 61 62 61 73 65 0a 20 20 20 20  MP database.    
196b9 2a 2a 20 73 63 68 65 6d 61 20 69 73 20 63 68 61  ** schema is cha
196ba 6e 67 65 64 2e 20 20 54 69 63 6b 65 74 20 23 31  nged.  Ticket #1
196bb 36 34 34 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  644 */.    sqlit
196bc 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64  e3ExpirePrepared
196bd 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a  Statements(db);.
196be 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
196bf 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 65 72 69 66  /* Opcode: Verif
196c0 79 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 2a 0a  yCookie P1 P2 *.
196c1 2a 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20  **.** Check the 
196c2 76 61 6c 75 65 20 6f 66 20 67 6c 6f 62 61 6c 20  value of global 
196c3 64 61 74 61 62 61 73 65 20 70 61 72 61 6d 65 74  database paramet
196c4 65 72 20 6e 75 6d 62 65 72 20 30 20 28 74 68 65  er number 0 (the
196c5 0a 2a 2a 20 73 63 68 65 6d 61 20 76 65 72 73 69  .** schema versi
196c6 6f 6e 29 20 61 6e 64 20 6d 61 6b 65 20 73 75 72  on) and make sur
196c7 65 20 69 74 20 69 73 20 65 71 75 61 6c 20 74 6f  e it is equal to
196c8 20 50 32 2e 20 20 0a 2a 2a 20 50 31 20 69 73 20   P2.  .** P1 is 
196c9 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 75 6d  the database num
196ca 62 65 72 20 77 68 69 63 68 20 69 73 20 30 20 66  ber which is 0 f
196cb 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  or the main data
196cc 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 61 6e 64  base file.** and
196cd 20 31 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20   1 for the file 
196ce 68 6f 6c 64 69 6e 67 20 74 65 6d 70 6f 72 61 72  holding temporar
196cf 79 20 74 61 62 6c 65 73 20 61 6e 64 20 73 6f 6d  y tables and som
196d0 65 20 68 69 67 68 65 72 20 6e 75 6d 62 65 72 0a  e higher number.
196d1 2a 2a 20 66 6f 72 20 61 75 78 69 6c 69 61 72 79  ** for auxiliary
196d2 20 64 61 74 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a   databases..**.*
196d3 2a 20 54 68 65 20 63 6f 6f 6b 69 65 20 63 68 61  * The cookie cha
196d4 6e 67 65 73 20 69 74 73 20 76 61 6c 75 65 20 77  nges its value w
196d5 68 65 6e 65 76 65 72 20 74 68 65 20 64 61 74 61  henever the data
196d6 62 61 73 65 20 73 63 68 65 6d 61 20 63 68 61 6e  base schema chan
196d7 67 65 73 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 65  ges..** This ope
196d8 72 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74  ration is used t
196d9 6f 20 64 65 74 65 63 74 20 77 68 65 6e 20 74 68  o detect when th
196da 61 74 20 74 68 65 20 63 6f 6f 6b 69 65 20 68 61  at the cookie ha
196db 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 61 6e 64  s changed.** and
196dc 20 74 68 61 74 20 74 68 65 20 63 75 72 72 65 6e   that the curren
196dd 74 20 70 72 6f 63 65 73 73 20 6e 65 65 64 73 20  t process needs 
196de 74 6f 20 72 65 72 65 61 64 20 74 68 65 20 73 63  to reread the sc
196df 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 45 69 74 68  hema..**.** Eith
196e0 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  er a transaction
196e1 20 6e 65 65 64 73 20 74 6f 20 68 61 76 65 20 62   needs to have b
196e2 65 65 6e 20 73 74 61 72 74 65 64 20 6f 72 20 61  een started or a
196e3 6e 20 4f 50 5f 4f 70 65 6e 20 6e 65 65 64 73 0a  n OP_Open needs.
196e4 2a 2a 20 74 6f 20 62 65 20 65 78 65 63 75 74 65  ** to be execute
196e5 64 20 28 74 6f 20 65 73 74 61 62 6c 69 73 68 20  d (to establish 
196e6 61 20 72 65 61 64 20 6c 6f 63 6b 29 20 62 65 66  a read lock) bef
196e7 6f 72 65 20 74 68 69 73 20 6f 70 63 6f 64 65 20  ore this opcode 
196e8 69 73 0a 2a 2a 20 69 6e 76 6f 6b 65 64 2e 0a 2a  is.** invoked..*
196e9 2f 0a 63 61 73 65 20 4f 50 5f 56 65 72 69 66 79  /.case OP_Verify
196ea 43 6f 6f 6b 69 65 3a 20 7b 0a 20 20 69 6e 74 20  Cookie: {.  int 
196eb 69 4d 65 74 61 3b 0a 20 20 42 74 72 65 65 20 2a  iMeta;.  Btree *
196ec 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70  pBt;.  assert( p
196ed 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70  Op->p1>=0 && pOp
196ee 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a  ->p1<db->nDb );.
196ef 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74    assert( (p->bt
196f0 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f  reeMask & (1<<pO
196f1 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20  p->p1))!=0 );.  
196f2 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 70 4f  pBt = db->aDb[pO
196f3 70 2d 3e 70 31 5d 2e 70 42 74 3b 0a 20 20 69 66  p->p1].pBt;.  if
196f4 28 20 70 42 74 20 29 7b 0a 20 20 20 20 72 63 20  ( pBt ){.    rc 
196f5 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
196f6 74 4d 65 74 61 28 70 42 74 2c 20 31 2c 20 28 75  tMeta(pBt, 1, (u
196f7 33 32 20 2a 29 26 69 4d 65 74 61 29 3b 0a 20 20  32 *)&iMeta);.  
196f8 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
196f9 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 69  SQLITE_OK;.    i
196fa 4d 65 74 61 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  Meta = 0;.  }.  
196fb 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
196fc 4b 20 26 26 20 69 4d 65 74 61 21 3d 70 4f 70 2d  K && iMeta!=pOp-
196fd 3e 70 32 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  >p2 ){.    sqlit
196fe 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
196ff 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d  zErrMsg);.    p-
19700 3e 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74  >zErrMsg = sqlit
19701 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 22  e3DbStrDup(db, "
19702 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
19703 68 61 73 20 63 68 61 6e 67 65 64 22 29 3b 0a 20  has changed");. 
19704 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 63 68     /* If the sch
19705 65 6d 61 2d 63 6f 6f 6b 69 65 20 66 72 6f 6d 20  ema-cookie from 
19706 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
19707 65 20 6d 61 74 63 68 65 73 20 74 68 65 20 63 6f  e matches the co
19708 6f 6b 69 65 20 0a 20 20 20 20 2a 2a 20 73 74 6f  okie .    ** sto
19709 72 65 64 20 77 69 74 68 20 74 68 65 20 69 6e 2d  red with the in-
1970a 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74  memory represent
1970b 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 63 68  ation of the sch
1970c 65 6d 61 2c 20 64 6f 0a 20 20 20 20 2a 2a 20 6e  ema, do.    ** n
1970d 6f 74 20 72 65 6c 6f 61 64 20 74 68 65 20 73 63  ot reload the sc
1970e 68 65 6d 61 20 66 72 6f 6d 20 74 68 65 20 64 61  hema from the da
1970f 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20  tabase file..   
19710 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 76 69   **.    ** If vi
19711 72 74 75 61 6c 2d 74 61 62 6c 65 73 20 61 72 65  rtual-tables are
19712 20 69 6e 20 75 73 65 2c 20 74 68 69 73 20 69 73   in use, this is
19713 20 6e 6f 74 20 6a 75 73 74 20 61 6e 20 6f 70 74   not just an opt
19714 69 6d 69 7a 61 74 69 6f 6e 2e 0a 20 20 20 20 2a  imization..    *
19715 2a 20 4f 66 74 65 6e 2c 20 76 2d 74 61 62 6c 65  * Often, v-table
19716 73 20 73 74 6f 72 65 20 74 68 65 69 72 20 64 61  s store their da
19717 74 61 20 69 6e 20 6f 74 68 65 72 20 53 51 4c 69  ta in other SQLi
19718 74 65 20 74 61 62 6c 65 73 2c 20 77 68 69 63 68  te tables, which
19719 0a 20 20 20 20 2a 2a 20 61 72 65 20 71 75 65 72  .    ** are quer
1971a 69 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  ied from within 
1971b 78 4e 65 78 74 28 29 20 61 6e 64 20 6f 74 68 65  xNext() and othe
1971c 72 20 76 2d 74 61 62 6c 65 20 6d 65 74 68 6f 64  r v-table method
1971d 73 20 75 73 69 6e 67 0a 20 20 20 20 2a 2a 20 70  s using.    ** p
1971e 72 65 70 61 72 65 64 20 71 75 65 72 69 65 73 2e  repared queries.
1971f 20 49 66 20 73 75 63 68 20 61 20 71 75 65 72 79   If such a query
19720 20 69 73 20 6f 75 74 2d 6f 66 2d 64 61 74 65 2c   is out-of-date,
19721 20 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20   we do not want 
19722 74 6f 0a 20 20 20 20 2a 2a 20 64 69 73 63 61 72  to.    ** discar
19723 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  d the database s
19724 63 68 65 6d 61 2c 20 61 73 20 74 68 65 20 75 73  chema, as the us
19725 65 72 20 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e  er code implemen
19726 74 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20  ting the.    ** 
19727 76 2d 74 61 62 6c 65 20 77 6f 75 6c 64 20 68 61  v-table would ha
19728 76 65 20 74 6f 20 62 65 20 72 65 61 64 79 20 66  ve to be ready f
19729 6f 72 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76  or the sqlite3_v
1972a 74 61 62 20 73 74 72 75 63 74 75 72 65 20 69 74  tab structure it
1972b 73 65 6c 66 0a 20 20 20 20 2a 2a 20 74 6f 20 62  self.    ** to b
1972c 65 20 69 6e 76 61 6c 69 64 61 74 65 64 20 77 68  e invalidated wh
1972d 65 6e 65 76 65 72 20 73 71 6c 69 74 65 33 5f 73  enever sqlite3_s
1972e 74 65 70 28 29 20 69 73 20 63 61 6c 6c 65 64 20  tep() is called 
1972f 66 72 6f 6d 20 77 69 74 68 69 6e 20 0a 20 20 20  from within .   
19730 20 2a 2a 20 61 20 76 2d 74 61 62 6c 65 20 6d 65   ** a v-table me
19731 74 68 6f 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  thod..    */.   
19732 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 70 4f 70   if( db->aDb[pOp
19733 2d 3e 70 31 5d 2e 70 53 63 68 65 6d 61 2d 3e 73  ->p1].pSchema->s
19734 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 21 3d 69 4d  chema_cookie!=iM
19735 65 74 61 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  eta ){.      sql
19736 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
19737 6c 53 63 68 65 6d 61 28 64 62 2c 20 70 4f 70 2d  lSchema(db, pOp-
19738 3e 70 31 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  >p1);.    }..   
19739 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72   sqlite3ExpirePr
1973a 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73  eparedStatements
1973b 28 64 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 53  (db);.    rc = S
1973c 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20  QLITE_SCHEMA;.  
1973d 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1973e 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 52 65 61   Opcode: OpenRea
1973f 64 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35  d P1 P2 P3 P4 P5
19740 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 72 65  .**.** Open a re
19741 61 64 2d 6f 6e 6c 79 20 63 75 72 73 6f 72 20 66  ad-only cursor f
19742 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20  or the database 
19743 74 61 62 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74  table whose root
19744 20 70 61 67 65 20 69 73 0a 2a 2a 20 50 32 20 69   page is.** P2 i
19745 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c  n a database fil
19746 65 2e 20 20 54 68 65 20 64 61 74 61 62 61 73 65  e.  The database
19747 20 66 69 6c 65 20 69 73 20 64 65 74 65 72 6d 69   file is determi
19748 6e 65 64 20 62 79 20 50 33 2e 20 0a 2a 2a 20 50  ned by P3. .** P
19749 33 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65 20 6d  3==0 means the m
1974a 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20 50 33  ain database, P3
1974b 3d 3d 31 20 6d 65 61 6e 73 20 74 68 65 20 64 61  ==1 means the da
1974c 74 61 62 61 73 65 20 75 73 65 64 20 66 6f 72 20  tabase used for 
1974d 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61  .** temporary ta
1974e 62 6c 65 73 2c 20 61 6e 64 20 50 33 3e 31 20 6d  bles, and P3>1 m
1974f 65 61 6e 73 20 75 73 65 64 20 74 68 65 20 63 6f  eans used the co
19750 72 72 65 73 70 6f 6e 64 69 6e 67 20 61 74 74 61  rresponding atta
19751 63 68 65 64 0a 2a 2a 20 64 61 74 61 62 61 73 65  ched.** database
19752 2e 20 20 47 69 76 65 20 74 68 65 20 6e 65 77 20  .  Give the new 
19753 63 75 72 73 6f 72 20 61 6e 20 69 64 65 6e 74 69  cursor an identi
19754 66 69 65 72 20 6f 66 20 50 31 2e 20 20 54 68 65  fier of P1.  The
19755 20 50 31 0a 2a 2a 20 76 61 6c 75 65 73 20 6e 65   P1.** values ne
19756 65 64 20 6e 6f 74 20 62 65 20 63 6f 6e 74 69 67  ed not be contig
19757 75 6f 75 73 20 62 75 74 20 61 6c 6c 20 50 31 20  uous but all P1 
19758 76 61 6c 75 65 73 20 73 68 6f 75 6c 64 20 62 65  values should be
19759 20 73 6d 61 6c 6c 20 69 6e 74 65 67 65 72 73 2e   small integers.
1975a 0a 2a 2a 20 49 74 20 69 73 20 61 6e 20 65 72 72  .** It is an err
1975b 6f 72 20 66 6f 72 20 50 31 20 74 6f 20 62 65 20  or for P1 to be 
1975c 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20  negative..**.** 
1975d 49 66 20 50 35 21 3d 30 20 74 68 65 6e 20 75 73  If P5!=0 then us
1975e 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  e the content of
1975f 20 72 65 67 69 73 74 65 72 20 50 32 20 61 73 20   register P2 as 
19760 74 68 65 20 72 6f 6f 74 20 70 61 67 65 2c 20 6e  the root page, n
19761 6f 74 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20  ot.** the value 
19762 6f 66 20 50 32 20 69 74 73 65 6c 66 2e 0a 2a 2a  of P2 itself..**
19763 0a 2a 2a 20 54 68 65 72 65 20 77 69 6c 6c 20 62  .** There will b
19764 65 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e  e a read lock on
19765 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 68   the database wh
19766 65 6e 65 76 65 72 20 74 68 65 72 65 20 69 73 20  enever there is 
19767 61 6e 0a 2a 2a 20 6f 70 65 6e 20 63 75 72 73 6f  an.** open curso
19768 72 2e 20 20 49 66 20 74 68 65 20 64 61 74 61 62  r.  If the datab
19769 61 73 65 20 77 61 73 20 75 6e 6c 6f 63 6b 65 64  ase was unlocked
1976a 20 70 72 69 6f 72 20 74 6f 20 74 68 69 73 20 69   prior to this i
1976b 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 74 68  nstruction.** th
1976c 65 6e 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 69  en a read lock i
1976d 73 20 61 63 71 75 69 72 65 64 20 61 73 20 70 61  s acquired as pa
1976e 72 74 20 6f 66 20 74 68 69 73 20 69 6e 73 74 72  rt of this instr
1976f 75 63 74 69 6f 6e 2e 20 20 41 20 72 65 61 64 0a  uction.  A read.
19770 2a 2a 20 6c 6f 63 6b 20 61 6c 6c 6f 77 73 20 6f  ** lock allows o
19771 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 74  ther processes t
19772 6f 20 72 65 61 64 20 74 68 65 20 64 61 74 61 62  o read the datab
19773 61 73 65 20 62 75 74 20 70 72 6f 68 69 62 69 74  ase but prohibit
19774 73 0a 2a 2a 20 61 6e 79 20 6f 74 68 65 72 20 70  s.** any other p
19775 72 6f 63 65 73 73 20 66 72 6f 6d 20 6d 6f 64 69  rocess from modi
19776 66 79 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  fying the databa
19777 73 65 2e 20 20 54 68 65 20 72 65 61 64 20 6c 6f  se.  The read lo
19778 63 6b 20 69 73 0a 2a 2a 20 72 65 6c 65 61 73 65  ck is.** release
19779 64 20 77 68 65 6e 20 61 6c 6c 20 63 75 72 73 6f  d when all curso
1977a 72 73 20 61 72 65 20 63 6c 6f 73 65 64 2e 20 20  rs are closed.  
1977b 49 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74  If this instruct
1977c 69 6f 6e 20 61 74 74 65 6d 70 74 73 0a 2a 2a 20  ion attempts.** 
1977d 74 6f 20 67 65 74 20 61 20 72 65 61 64 20 6c 6f  to get a read lo
1977e 63 6b 20 62 75 74 20 66 61 69 6c 73 2c 20 74 68  ck but fails, th
1977f 65 20 73 63 72 69 70 74 20 74 65 72 6d 69 6e 61  e script termina
19780 74 65 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20 53  tes with an.** S
19781 51 4c 49 54 45 5f 42 55 53 59 20 65 72 72 6f 72  QLITE_BUSY error
19782 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   code..**.** The
19783 20 50 34 20 76 61 6c 75 65 20 69 73 20 61 20 70   P4 value is a p
19784 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49  ointer to a KeyI
19785 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68  nfo structure th
19786 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 0a 2a  at defines the.*
19787 2a 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f  * content and co
19788 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
19789 20 6f 66 20 69 6e 64 69 63 65 73 2e 20 20 50 34   of indices.  P4
1978a 20 69 73 20 4e 55 4c 4c 20 66 6f 72 20 63 75 72   is NULL for cur
1978b 73 6f 72 73 0a 2a 2a 20 74 68 61 74 20 61 72 65  sors.** that are
1978c 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f   not pointing to
1978d 20 69 6e 64 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20   indices..**.** 
1978e 53 65 65 20 61 6c 73 6f 20 4f 70 65 6e 57 72 69  See also OpenWri
1978f 74 65 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  te..*/./* Opcode
19790 3a 20 4f 70 65 6e 57 72 69 74 65 20 50 31 20 50  : OpenWrite P1 P
19791 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a  2 P3 P4 P5.**.**
19792 20 4f 70 65 6e 20 61 20 72 65 61 64 2f 77 72 69   Open a read/wri
19793 74 65 20 63 75 72 73 6f 72 20 6e 61 6d 65 64 20  te cursor named 
19794 50 31 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20  P1 on the table 
19795 6f 72 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72  or index whose r
19796 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 50  oot.** page is P
19797 32 2e 20 20 4f 72 20 69 66 20 50 35 21 3d 30 20  2.  Or if P5!=0 
19798 75 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  use the content 
19799 6f 66 20 72 65 67 69 73 74 65 72 20 50 32 20 74  of register P2 t
1979a 6f 20 66 69 6e 64 20 74 68 65 0a 2a 2a 20 72 6f  o find the.** ro
1979b 6f 74 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54  ot page..**.** T
1979c 68 65 20 50 34 20 76 61 6c 75 65 20 69 73 20 61  he P4 value is a
1979d 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b 65   pointer to a Ke
1979e 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
1979f 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 65  that defines the
197a0 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20  .** content and 
197a1 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
197a2 63 65 20 6f 66 20 69 6e 64 69 63 65 73 2e 20 20  ce of indices.  
197a3 50 34 20 69 73 20 4e 55 4c 4c 20 66 6f 72 20 63  P4 is NULL for c
197a4 75 72 73 6f 72 73 0a 2a 2a 20 74 68 61 74 20 61  ursors.** that a
197a5 72 65 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20  re not pointing 
197a6 74 6f 20 69 6e 64 69 63 65 73 2e 0a 2a 2a 0a 2a  to indices..**.*
197a7 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69  * This instructi
197a8 6f 6e 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69  on works just li
197a9 6b 65 20 4f 70 65 6e 52 65 61 64 20 65 78 63 65  ke OpenRead exce
197aa 70 74 20 74 68 61 74 20 69 74 20 6f 70 65 6e 73  pt that it opens
197ab 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69   the cursor.** i
197ac 6e 20 72 65 61 64 2f 77 72 69 74 65 20 6d 6f 64  n read/write mod
197ad 65 2e 20 20 46 6f 72 20 61 20 67 69 76 65 6e 20  e.  For a given 
197ae 74 61 62 6c 65 2c 20 74 68 65 72 65 20 63 61 6e  table, there can
197af 20 62 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20   be one or more 
197b0 72 65 61 64 2d 6f 6e 6c 79 0a 2a 2a 20 63 75 72  read-only.** cur
197b1 73 6f 72 73 20 6f 72 20 61 20 73 69 6e 67 6c 65  sors or a single
197b2 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73   read/write curs
197b3 6f 72 20 62 75 74 20 6e 6f 74 20 62 6f 74 68 2e  or but not both.
197b4 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20  .**.** See also 
197b5 4f 70 65 6e 52 65 61 64 2e 0a 2a 2f 0a 63 61 73  OpenRead..*/.cas
197b6 65 20 4f 50 5f 4f 70 65 6e 52 65 61 64 3a 0a 63  e OP_OpenRead:.c
197b7 61 73 65 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65  ase OP_OpenWrite
197b8 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f  : {.  int i = pO
197b9 70 2d 3e 70 31 3b 0a 20 20 69 6e 74 20 70 32 20  p->p1;.  int p2 
197ba 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 69 6e 74  = pOp->p2;.  int
197bb 20 69 44 62 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a   iDb = pOp->p3;.
197bc 20 20 69 6e 74 20 77 72 46 6c 61 67 3b 0a 20 20    int wrFlag;.  
197bd 42 74 72 65 65 20 2a 70 58 3b 0a 20 20 56 64 62  Btree *pX;.  Vdb
197be 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20  eCursor *pCur;. 
197bf 20 44 62 20 2a 70 44 62 3b 0a 20 20 0a 20 20 61   Db *pDb;.  .  a
197c0 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26  ssert( iDb>=0 &&
197c1 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a   iDb<db->nDb );.
197c2 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74    assert( (p->bt
197c3 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 44  reeMask & (1<<iD
197c4 62 29 29 21 3d 30 20 29 3b 0a 20 20 70 44 62 20  b))!=0 );.  pDb 
197c5 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b  = &db->aDb[iDb];
197c6 0a 20 20 70 58 20 3d 20 70 44 62 2d 3e 70 42 74  .  pX = pDb->pBt
197c7 3b 0a 20 20 61 73 73 65 72 74 28 20 70 58 21 3d  ;.  assert( pX!=
197c8 30 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e  0 );.  if( pOp->
197c9 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e 57  opcode==OP_OpenW
197ca 72 69 74 65 20 29 7b 0a 20 20 20 20 77 72 46 6c  rite ){.    wrFl
197cb 61 67 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20  ag = 1;.    if( 
197cc 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69  pDb->pSchema->fi
197cd 6c 65 5f 66 6f 72 6d 61 74 20 3c 20 70 2d 3e 6d  le_format < p->m
197ce 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61  inWriteFileForma
197cf 74 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6d 69  t ){.      p->mi
197d0 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74  nWriteFileFormat
197d1 20 3d 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d   = pDb->pSchema-
197d2 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 0a 20 20  >file_format;.  
197d3 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
197d4 20 77 72 46 6c 61 67 20 3d 20 30 3b 0a 20 20 7d   wrFlag = 0;.  }
197d5 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29  .  if( pOp->p5 )
197d6 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 32  {.    assert( p2
197d7 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  >0 );.    assert
197d8 28 20 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b  ( p2<=p->nMem );
197d9 0a 20 20 20 20 70 49 6e 32 20 3d 20 26 70 2d 3e  .    pIn2 = &p->
197da 61 4d 65 6d 5b 70 32 5d 3b 0a 20 20 20 20 73 71  aMem[p2];.    sq
197db 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65  lite3VdbeMemInte
197dc 67 65 72 69 66 79 28 70 49 6e 32 29 3b 0a 20 20  gerify(pIn2);.  
197dd 20 20 70 32 20 3d 20 70 49 6e 32 2d 3e 75 2e 69    p2 = pIn2->u.i
197de 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 32  ;.    assert( p2
197df 3e 3d 32 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73  >=2 );.  }.  ass
197e0 65 72 74 28 20 69 3e 3d 30 20 29 3b 0a 20 20 70  ert( i>=0 );.  p
197e1 43 75 72 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75  Cur = allocateCu
197e2 72 73 6f 72 28 70 2c 20 69 2c 20 26 70 4f 70 5b  rsor(p, i, &pOp[
197e3 2d 31 5d 2c 20 69 44 62 2c 20 31 29 3b 0a 20 20  -1], iDb, 1);.  
197e4 69 66 28 20 70 43 75 72 3d 3d 30 20 29 20 67 6f  if( pCur==0 ) go
197e5 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 70 43 75  to no_mem;.  pCu
197e6 72 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a  r->nullRow = 1;.
197e7 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
197e8 72 65 65 43 75 72 73 6f 72 28 70 58 2c 20 70 32  reeCursor(pX, p2
197e9 2c 20 77 72 46 6c 61 67 2c 20 70 4f 70 2d 3e 70  , wrFlag, pOp->p
197ea 34 2e 70 2c 20 70 43 75 72 2d 3e 70 43 75 72 73  4.p, pCur->pCurs
197eb 6f 72 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e  or);.  if( pOp->
197ec 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e  p4type==P4_KEYIN
197ed 46 4f 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e  FO ){.    pCur->
197ee 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e  pKeyInfo = pOp->
197ef 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20  p4.pKeyInfo;.   
197f0 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 2d   pCur->pKeyInfo-
197f1 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e 64 62  >enc = ENC(p->db
197f2 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
197f3 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d  pCur->pKeyInfo =
197f4 20 30 3b 0a 20 20 7d 0a 20 20 73 77 69 74 63 68   0;.  }.  switch
197f5 28 20 72 63 20 29 7b 0a 20 20 20 20 63 61 73 65  ( rc ){.    case
197f6 20 53 51 4c 49 54 45 5f 42 55 53 59 3a 20 7b 0a   SQLITE_BUSY: {.
197f7 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63        p->pc = pc
197f8 3b 0a 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20  ;.      p->rc = 
197f9 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
197fa 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62  ;.      goto vdb
197fb 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a  e_return;.    }.
197fc 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
197fd 4f 4b 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  OK: {.      int 
197fe 66 6c 61 67 73 20 3d 20 73 71 6c 69 74 65 33 42  flags = sqlite3B
197ff 74 72 65 65 46 6c 61 67 73 28 70 43 75 72 2d 3e  treeFlags(pCur->
19800 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20  pCursor);.      
19801 2f 2a 20 53 61 6e 69 74 79 20 63 68 65 63 6b 69  /* Sanity checki
19802 6e 67 2e 20 20 4f 6e 6c 79 20 74 68 65 20 6c 6f  ng.  Only the lo
19803 77 65 72 20 66 6f 75 72 20 62 69 74 73 20 6f 66  wer four bits of
19804 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 20   the flags byte 
19805 73 68 6f 75 6c 64 0a 20 20 20 20 20 20 2a 2a 20  should.      ** 
19806 62 65 20 75 73 65 64 2e 20 20 42 69 74 20 33 20  be used.  Bit 3 
19807 28 6d 61 73 6b 20 30 78 30 38 29 20 69 73 20 75  (mask 0x08) is u
19808 6e 70 72 65 64 69 63 74 61 62 6c 65 2e 20 20 54  npredictable.  T
19809 68 65 20 6c 6f 77 65 72 20 33 20 62 69 74 73 0a  he lower 3 bits.
1980a 20 20 20 20 20 20 2a 2a 20 28 6d 61 73 6b 20 30        ** (mask 0
1980b 78 30 37 29 20 73 68 6f 75 6c 64 20 62 65 20 65  x07) should be e
1980c 69 74 68 65 72 20 35 20 28 69 6e 74 6b 65 79 2b  ither 5 (intkey+
1980d 6c 65 61 66 64 61 74 61 20 66 6f 72 20 74 61 62  leafdata for tab
1980e 6c 65 73 29 20 6f 72 0a 20 20 20 20 20 20 2a 2a  les) or.      **
1980f 20 32 20 28 7a 65 72 6f 64 61 74 61 20 66 6f 72   2 (zerodata for
19810 20 69 6e 64 69 63 65 73 29 2e 20 20 49 66 20 74   indices).  If t
19811 68 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20  hese conditions 
19812 61 72 65 20 6e 6f 74 20 6d 65 74 20 69 74 20 63  are not met it c
19813 61 6e 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 6c 79  an.      ** only
19814 20 6d 65 61 6e 20 74 68 61 74 20 77 65 20 61 72   mean that we ar
19815 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61  e dealing with a
19816 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73   corrupt databas
19817 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2f 0a  e file.      */.
19818 20 20 20 20 20 20 69 66 28 20 28 66 6c 61 67 73        if( (flags
19819 20 26 20 30 78 66 30 29 21 3d 30 20 7c 7c 20 28   & 0xf0)!=0 || (
1981a 28 66 6c 61 67 73 20 26 20 30 78 30 37 29 21 3d  (flags & 0x07)!=
1981b 35 20 26 26 20 28 66 6c 61 67 73 20 26 20 30 78  5 && (flags & 0x
1981c 30 37 29 21 3d 32 29 20 29 7b 0a 20 20 20 20 20  07)!=2) ){.     
1981d 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43     rc = SQLITE_C
1981e 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
1981f 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f       goto abort_
19820 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
19821 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 75 72      }.      pCur
19822 2d 3e 69 73 54 61 62 6c 65 20 3d 20 28 66 6c 61  ->isTable = (fla
19823 67 73 20 26 20 42 54 52 45 45 5f 49 4e 54 4b 45  gs & BTREE_INTKE
19824 59 29 21 3d 30 3b 0a 20 20 20 20 20 20 70 43 75  Y)!=0;.      pCu
19825 72 2d 3e 69 73 49 6e 64 65 78 20 3d 20 28 66 6c  r->isIndex = (fl
19826 61 67 73 20 26 20 42 54 52 45 45 5f 5a 45 52 4f  ags & BTREE_ZERO
19827 44 41 54 41 29 21 3d 30 3b 0a 20 20 20 20 20 20  DATA)!=0;.      
19828 2f 2a 20 49 66 20 50 34 3d 3d 30 20 69 74 20 6d  /* If P4==0 it m
19829 65 61 6e 73 20 77 65 20 61 72 65 20 65 78 70 65  eans we are expe
1982a 63 74 65 64 20 74 6f 20 6f 70 65 6e 20 61 20 74  cted to open a t
1982b 61 62 6c 65 2e 20 20 49 66 20 50 34 21 3d 30 20  able.  If P4!=0 
1982c 74 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 77 65  then.      ** we
1982d 20 65 78 70 65 63 74 20 74 6f 20 62 65 20 6f 70   expect to be op
1982e 65 6e 69 6e 67 20 61 6e 20 69 6e 64 65 78 2e 20  ening an index. 
1982f 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20   If this is not 
19830 77 68 61 74 20 68 61 70 70 65 6e 65 64 2c 0a 20  what happened,. 
19831 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65       ** then the
19832 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f 72   database is cor
19833 72 75 70 74 0a 20 20 20 20 20 20 2a 2f 0a 20 20  rupt.      */.  
19834 20 20 20 20 69 66 28 20 28 70 43 75 72 2d 3e 69      if( (pCur->i
19835 73 54 61 62 6c 65 20 26 26 20 70 4f 70 2d 3e 70  sTable && pOp->p
19836 34 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46  4type==P4_KEYINF
19837 4f 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 43  O).       || (pC
19838 75 72 2d 3e 69 73 49 6e 64 65 78 20 26 26 20 70  ur->isIndex && p
19839 4f 70 2d 3e 70 34 74 79 70 65 21 3d 50 34 5f 4b  Op->p4type!=P4_K
1983a 45 59 49 4e 46 4f 29 20 29 7b 0a 20 20 20 20 20  EYINFO) ){.     
1983b 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43     rc = SQLITE_C
1983c 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
1983d 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f       goto abort_
1983e 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
1983f 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
19840 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
19841 65 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 3a 20  e SQLITE_EMPTY: 
19842 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 69 73  {.      pCur->is
19843 54 61 62 6c 65 20 3d 20 70 4f 70 2d 3e 70 34 74  Table = pOp->p4t
19844 79 70 65 21 3d 50 34 5f 4b 45 59 49 4e 46 4f 3b  ype!=P4_KEYINFO;
19845 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 69 73 49  .      pCur->isI
19846 6e 64 65 78 20 3d 20 21 70 43 75 72 2d 3e 69 73  ndex = !pCur->is
19847 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 70 43 75  Table;.      pCu
19848 72 2d 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a  r->pCursor = 0;.
19849 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1984a 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61  E_OK;.      brea
1984b 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66  k;.    }.    def
1984c 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 67 6f  ault: {.      go
1984d 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1984e 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d  error;.    }.  }
1984f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
19850 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 45 70 68 65  Opcode: OpenEphe
19851 6d 65 72 61 6c 20 50 31 20 50 32 20 2a 20 50 34  meral P1 P2 * P4
19852 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20   *.**.** Open a 
19853 6e 65 77 20 63 75 72 73 6f 72 20 50 31 20 74 6f  new cursor P1 to
19854 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62   a transient tab
19855 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f  le..** The curso
19856 72 20 69 73 20 61 6c 77 61 79 73 20 6f 70 65 6e  r is always open
19857 65 64 20 72 65 61 64 2f 77 72 69 74 65 20 65 76  ed read/write ev
19858 65 6e 20 69 66 20 0a 2a 2a 20 74 68 65 20 6d 61  en if .** the ma
19859 69 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 72  in database is r
1985a 65 61 64 2d 6f 6e 6c 79 2e 20 20 54 68 65 20 74  ead-only.  The t
1985b 72 61 6e 73 69 65 6e 74 20 6f 72 20 76 69 72 74  ransient or virt
1985c 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20  ual.** table is 
1985d 64 65 6c 65 74 65 64 20 61 75 74 6f 6d 61 74 69  deleted automati
1985e 63 61 6c 6c 79 20 77 68 65 6e 20 74 68 65 20 63  cally when the c
1985f 75 72 73 6f 72 20 69 73 20 63 6c 6f 73 65 64 2e  ursor is closed.
19860 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20 74 68 65  .**.** P2 is the
19861 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
19862 6e 73 20 69 6e 20 74 68 65 20 76 69 72 74 75 61  ns in the virtua
19863 6c 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20  l table..** The 
19864 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 6f  cursor points to
19865 20 61 20 42 54 72 65 65 20 74 61 62 6c 65 20 69   a BTree table i
19866 66 20 50 34 3d 3d 30 20 61 6e 64 20 74 6f 20 61  f P4==0 and to a
19867 20 42 54 72 65 65 20 69 6e 64 65 78 0a 2a 2a 20   BTree index.** 
19868 69 66 20 50 34 20 69 73 20 6e 6f 74 20 30 2e 20  if P4 is not 0. 
19869 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20 4e 55   If P4 is not NU
1986a 4c 4c 2c 20 69 74 20 70 6f 69 6e 74 73 20 74 6f  LL, it points to
1986b 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63   a KeyInfo struc
1986c 74 75 72 65 0a 2a 2a 20 74 68 61 74 20 64 65 66  ture.** that def
1986d 69 6e 65 73 20 74 68 65 20 66 6f 72 6d 61 74 20  ines the format 
1986e 6f 66 20 6b 65 79 73 20 69 6e 20 74 68 65 20 69  of keys in the i
1986f 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ndex..**.** This
19870 20 6f 70 63 6f 64 65 20 77 61 73 20 6f 6e 63 65   opcode was once
19871 20 63 61 6c 6c 65 64 20 4f 70 65 6e 54 65 6d 70   called OpenTemp
19872 2e 20 20 42 75 74 20 74 68 61 74 20 63 72 65 61  .  But that crea
19873 74 65 64 0a 2a 2a 20 63 6f 6e 66 75 73 69 6f 6e  ted.** confusion
19874 20 62 65 63 61 75 73 65 20 74 68 65 20 74 65 72   because the ter
19875 6d 20 22 74 65 6d 70 20 74 61 62 6c 65 22 2c 20  m "temp table", 
19876 6d 69 67 68 74 20 72 65 66 65 72 20 65 69 74 68  might refer eith
19877 65 72 0a 2a 2a 20 74 6f 20 61 20 54 45 4d 50 20  er.** to a TEMP 
19878 74 61 62 6c 65 20 61 74 20 74 68 65 20 53 51 4c  table at the SQL
19879 20 6c 65 76 65 6c 2c 20 6f 72 20 74 6f 20 61 20   level, or to a 
1987a 74 61 62 6c 65 20 6f 70 65 6e 65 64 20 62 79 0a  table opened by.
1987b 2a 2a 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 20  ** this opcode. 
1987c 20 54 68 65 6e 20 74 68 69 73 20 6f 70 63 6f 64   Then this opcod
1987d 65 20 77 61 73 20 63 61 6c 6c 20 4f 70 65 6e 56  e was call OpenV
1987e 69 72 74 75 61 6c 2e 20 20 42 75 74 0a 2a 2a 20  irtual.  But.** 
1987f 74 68 61 74 20 63 72 65 61 74 65 64 20 63 6f 6e  that created con
19880 66 75 73 69 6f 6e 20 77 69 74 68 20 74 68 65 20  fusion with the 
19881 77 68 6f 6c 65 20 76 69 72 74 75 61 6c 2d 74 61  whole virtual-ta
19882 62 6c 65 20 69 64 65 61 2e 0a 2a 2f 0a 63 61 73  ble idea..*/.cas
19883 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  e OP_OpenEphemer
19884 61 6c 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20  al: {.  int i = 
19885 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43  pOp->p1;.  VdbeC
19886 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20 73 74  ursor *pCx;.  st
19887 61 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 6f  atic const int o
19888 70 65 6e 46 6c 61 67 73 20 3d 20 0a 20 20 20 20  penFlags = .    
19889 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45    SQLITE_OPEN_RE
1988a 41 44 57 52 49 54 45 20 7c 0a 20 20 20 20 20 20  ADWRITE |.      
1988b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
1988c 54 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54  TE |.      SQLIT
1988d 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45  E_OPEN_EXCLUSIVE
1988e 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f   |.      SQLITE_
1988f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f  OPEN_DELETEONCLO
19890 53 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54  SE |.      SQLIT
19891 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54  E_OPEN_TRANSIENT
19892 5f 44 42 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  _DB;..  assert( 
19893 69 3e 3d 30 20 29 3b 0a 20 20 70 43 78 20 3d 20  i>=0 );.  pCx = 
19894 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70  allocateCursor(p
19895 2c 20 69 2c 20 70 4f 70 2c 20 2d 31 2c 20 31 29  , i, pOp, -1, 1)
19896 3b 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 20 29  ;.  if( pCx==0 )
19897 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20   goto no_mem;.  
19898 70 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31  pCx->nullRow = 1
19899 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1989a 42 74 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c  BtreeFactory(db,
1989b 20 30 2c 20 31 2c 20 53 51 4c 49 54 45 5f 44 45   0, 1, SQLITE_DE
1989c 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45  FAULT_TEMP_CACHE
1989d 5f 53 49 5a 45 2c 20 6f 70 65 6e 46 6c 61 67 73  _SIZE, openFlags
1989e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1989f 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70 43               &pC
198a0 78 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28 20 72  x->pBt);.  if( r
198a1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
198a2 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
198a3 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28  BtreeBeginTrans(
198a4 70 43 78 2d 3e 70 42 74 2c 20 31 29 3b 0a 20 20  pCx->pBt, 1);.  
198a5 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  }.  if( rc==SQLI
198a6 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20  TE_OK ){.    /* 
198a7 49 66 20 61 20 74 72 61 6e 73 69 65 6e 74 20 69  If a transient i
198a8 6e 64 65 78 20 69 73 20 72 65 71 75 69 72 65 64  ndex is required
198a9 2c 20 63 72 65 61 74 65 20 69 74 20 62 79 20 63  , create it by c
198aa 61 6c 6c 69 6e 67 0a 20 20 20 20 2a 2a 20 73 71  alling.    ** sq
198ab 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65  lite3BtreeCreate
198ac 54 61 62 6c 65 28 29 20 77 69 74 68 20 74 68 65  Table() with the
198ad 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 20   BTREE_ZERODATA 
198ae 66 6c 61 67 20 62 65 66 6f 72 65 0a 20 20 20 20  flag before.    
198af 2a 2a 20 6f 70 65 6e 69 6e 67 20 69 74 2e 20 49  ** opening it. I
198b0 66 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74 61  f a transient ta
198b1 62 6c 65 20 69 73 20 72 65 71 75 69 72 65 64 2c  ble is required,
198b2 20 6a 75 73 74 20 75 73 65 20 74 68 65 0a 20 20   just use the.  
198b3 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c    ** automatical
198b4 6c 79 20 63 72 65 61 74 65 64 20 74 61 62 6c 65  ly created table
198b5 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20   with root-page 
198b6 31 20 28 61 6e 20 49 4e 54 4b 45 59 20 74 61 62  1 (an INTKEY tab
198b7 6c 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  le)..    */.    
198b8 69 66 28 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79  if( pOp->p4.pKey
198b9 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 69 6e  Info ){.      in
198ba 74 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20 61 73  t pgno;.      as
198bb 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70  sert( pOp->p4typ
198bc 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 29 3b  e==P4_KEYINFO );
198bd 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
198be 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61  te3BtreeCreateTa
198bf 62 6c 65 28 70 43 78 2d 3e 70 42 74 2c 20 26 70  ble(pCx->pBt, &p
198c0 67 6e 6f 2c 20 42 54 52 45 45 5f 5a 45 52 4f 44  gno, BTREE_ZEROD
198c1 41 54 41 29 3b 20 0a 20 20 20 20 20 20 69 66 28  ATA); .      if(
198c2 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
198c3 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
198c4 28 20 70 67 6e 6f 3d 3d 4d 41 53 54 45 52 5f 52  ( pgno==MASTER_R
198c5 4f 4f 54 2b 31 20 29 3b 0a 20 20 20 20 20 20 20  OOT+1 );.       
198c6 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
198c7 65 65 43 75 72 73 6f 72 28 70 43 78 2d 3e 70 42  eeCursor(pCx->pB
198c8 74 2c 20 70 67 6e 6f 2c 20 31 2c 20 0a 20 20 20  t, pgno, 1, .   
198c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
198ca 20 20 20 20 20 20 20 20 20 20 20 20 20 28 4b 65               (Ke
198cb 79 49 6e 66 6f 2a 29 70 4f 70 2d 3e 70 34 2e 7a  yInfo*)pOp->p4.z
198cc 2c 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 29 3b  , pCx->pCursor);
198cd 0a 20 20 20 20 20 20 20 20 70 43 78 2d 3e 70 4b  .        pCx->pK
198ce 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34  eyInfo = pOp->p4
198cf 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20  .pKeyInfo;.     
198d0 20 20 20 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f     pCx->pKeyInfo
198d1 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e 64  ->enc = ENC(p->d
198d2 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  b);.      }.    
198d3 20 20 70 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d    pCx->isTable =
198d4 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   0;.    }else{. 
198d5 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
198d6 33 42 74 72 65 65 43 75 72 73 6f 72 28 70 43 78  3BtreeCursor(pCx
198d7 2d 3e 70 42 74 2c 20 4d 41 53 54 45 52 5f 52 4f  ->pBt, MASTER_RO
198d8 4f 54 2c 20 31 2c 20 30 2c 20 70 43 78 2d 3e 70  OT, 1, 0, pCx->p
198d9 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 70  Cursor);.      p
198da 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20 31 3b  Cx->isTable = 1;
198db 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 43 78  .    }.  }.  pCx
198dc 2d 3e 69 73 49 6e 64 65 78 20 3d 20 21 70 43 78  ->isIndex = !pCx
198dd 2d 3e 69 73 54 61 62 6c 65 3b 0a 20 20 62 72 65  ->isTable;.  bre
198de 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
198df 3a 20 4f 70 65 6e 50 73 65 75 64 6f 20 50 31 20  : OpenPseudo P1 
198e0 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4f  P2 * * *.**.** O
198e1 70 65 6e 20 61 20 6e 65 77 20 63 75 72 73 6f 72  pen a new cursor
198e2 20 74 68 61 74 20 70 6f 69 6e 74 73 20 74 6f 20   that points to 
198e3 61 20 66 61 6b 65 20 74 61 62 6c 65 20 74 68 61  a fake table tha
198e4 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e  t contains a sin
198e5 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 66 20 64 61  gle.** row of da
198e6 74 61 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74  ta.  Any attempt
198e7 20 74 6f 20 77 72 69 74 65 20 61 20 73 65 63 6f   to write a seco
198e8 6e 64 20 72 6f 77 20 6f 66 20 64 61 74 61 20 63  nd row of data c
198e9 61 75 73 65 73 20 74 68 65 0a 2a 2a 20 66 69 72  auses the.** fir
198ea 73 74 20 72 6f 77 20 74 6f 20 62 65 20 64 65 6c  st row to be del
198eb 65 74 65 64 2e 20 20 41 6c 6c 20 64 61 74 61 20  eted.  All data 
198ec 69 73 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20  is deleted when 
198ed 74 68 65 20 63 75 72 73 6f 72 20 69 73 0a 2a 2a  the cursor is.**
198ee 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 41   closed..**.** A
198ef 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 63 72   pseudo-table cr
198f0 65 61 74 65 64 20 62 79 20 74 68 69 73 20 6f 70  eated by this op
198f1 63 6f 64 65 20 69 73 20 75 73 65 66 75 6c 20 66  code is useful f
198f2 6f 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a 2a  or holding the.*
198f3 2a 20 4e 45 57 20 6f 72 20 4f 4c 44 20 74 61 62  * NEW or OLD tab
198f4 6c 65 73 20 69 6e 20 61 20 74 72 69 67 67 65 72  les in a trigger
198f5 2e 20 20 41 6c 73 6f 20 75 73 65 64 20 74 6f 20  .  Also used to 
198f6 68 6f 6c 64 20 74 68 65 20 61 20 73 69 6e 67 6c  hold the a singl
198f7 65 0a 2a 2a 20 72 6f 77 20 6f 75 74 70 75 74 20  e.** row output 
198f8 66 72 6f 6d 20 74 68 65 20 73 6f 72 74 65 72 20  from the sorter 
198f9 73 6f 20 74 68 61 74 20 74 68 65 20 72 6f 77 20  so that the row 
198fa 63 61 6e 20 62 65 20 64 65 63 6f 6d 70 6f 73 65  can be decompose
198fb 64 20 69 6e 74 6f 0a 2a 2a 20 69 6e 64 69 76 69  d into.** indivi
198fc 64 75 61 6c 20 63 6f 6c 75 6d 6e 73 20 75 73 69  dual columns usi
198fd 6e 67 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e  ng the OP_Column
198fe 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 57   opcode..**.** W
198ff 68 65 6e 20 4f 50 5f 49 6e 73 65 72 74 20 69 73  hen OP_Insert is
19900 20 65 78 65 63 75 74 65 64 20 74 6f 20 69 6e 73   executed to ins
19901 65 72 74 20 61 20 72 6f 77 20 69 6e 20 74 6f 20  ert a row in to 
19902 74 68 65 20 70 73 65 75 64 6f 20 74 61 62 6c 65  the pseudo table
19903 2c 0a 2a 2a 20 74 68 65 20 70 73 65 75 64 6f 2d  ,.** the pseudo-
19904 74 61 62 6c 65 20 63 75 72 73 6f 72 20 6d 61 79  table cursor may
19905 20 6f 72 20 6d 61 79 20 6e 6f 74 20 6d 61 6b 65   or may not make
19906 20 69 74 27 73 20 6f 77 6e 20 63 6f 70 79 20 6f   it's own copy o
19907 66 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61  f the.** origina
19908 6c 20 72 6f 77 20 64 61 74 61 2e 20 49 66 20 50  l row data. If P
19909 32 20 69 73 20 30 2c 20 74 68 65 6e 20 74 68 65  2 is 0, then the
1990a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 77 69   pseudo-table wi
1990b 6c 6c 20 63 6f 70 79 20 74 68 65 0a 2a 2a 20 6f  ll copy the.** o
1990c 72 69 67 69 6e 61 6c 20 72 6f 77 20 64 61 74 61  riginal row data
1990d 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 20 70  . Otherwise, a p
1990e 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6f 72  ointer to the or
1990f 69 67 69 6e 61 6c 20 6d 65 6d 6f 72 79 20 63 65  iginal memory ce
19910 6c 6c 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 2e  ll.** is stored.
19911 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 74   In this case, t
19912 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20  he vdbe program 
19913 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68 61 74  must ensure that
19914 20 74 68 65 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20   the .** memory 
19915 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 69 6e 67 20  cell containing 
19916 74 68 65 20 72 6f 77 20 64 61 74 61 20 69 73 20  the row data is 
19917 6e 6f 74 20 6f 76 65 72 77 72 69 74 74 65 6e 20  not overwritten 
19918 75 6e 74 69 6c 20 74 68 65 0a 2a 2a 20 70 73 65  until the.** pse
19919 75 64 6f 20 74 61 62 6c 65 20 69 73 20 63 6c 6f  udo table is clo
1991a 73 65 64 20 28 6f 72 20 61 20 6e 65 77 20 72 6f  sed (or a new ro
1991b 77 20 69 73 20 69 6e 73 65 72 74 65 64 20 69 6e  w is inserted in
1991c 74 6f 20 69 74 29 2e 0a 2a 2f 0a 63 61 73 65 20  to it)..*/.case 
1991d 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 3a 20 7b  OP_OpenPseudo: {
1991e 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e  .  int i = pOp->
1991f 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72  p1;.  VdbeCursor
19920 20 2a 70 43 78 3b 0a 20 20 61 73 73 65 72 74 28   *pCx;.  assert(
19921 20 69 3e 3d 30 20 29 3b 0a 20 20 70 43 78 20 3d   i>=0 );.  pCx =
19922 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28   allocateCursor(
19923 70 2c 20 69 2c 20 26 70 4f 70 5b 2d 31 5d 2c 20  p, i, &pOp[-1], 
19924 2d 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 43  -1, 0);.  if( pC
19925 78 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d  x==0 ) goto no_m
19926 65 6d 3b 0a 20 20 70 43 78 2d 3e 6e 75 6c 6c 52  em;.  pCx->nullR
19927 6f 77 20 3d 20 31 3b 0a 20 20 70 43 78 2d 3e 70  ow = 1;.  pCx->p
19928 73 65 75 64 6f 54 61 62 6c 65 20 3d 20 31 3b 0a  seudoTable = 1;.
19929 20 20 70 43 78 2d 3e 65 70 68 65 6d 50 73 65 75    pCx->ephemPseu
1992a 64 6f 54 61 62 6c 65 20 3d 20 70 4f 70 2d 3e 70  doTable = pOp->p
1992b 32 3b 0a 20 20 70 43 78 2d 3e 69 73 54 61 62 6c  2;.  pCx->isTabl
1992c 65 20 3d 20 31 3b 0a 20 20 70 43 78 2d 3e 69 73  e = 1;.  pCx->is
1992d 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 62 72 65  Index = 0;.  bre
1992e 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1992f 3a 20 43 6c 6f 73 65 20 50 31 20 2a 20 2a 20 2a  : Close P1 * * *
19930 20 2a 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61   *.**.** Close a
19931 20 63 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73   cursor previous
19932 6c 79 20 6f 70 65 6e 65 64 20 61 73 20 50 31 2e  ly opened as P1.
19933 20 20 49 66 20 50 31 20 69 73 20 6e 6f 74 0a 2a    If P1 is not.*
19934 2a 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e  * currently open
19935 2c 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69  , this instructi
19936 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  on is a no-op..*
19937 2f 0a 63 61 73 65 20 4f 50 5f 43 6c 6f 73 65 3a  /.case OP_Close:
19938 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70   {.  int i = pOp
19939 2d 3e 70 31 3b 0a 20 20 61 73 73 65 72 74 28 20  ->p1;.  assert( 
1993a 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75  i>=0 && i<p->nCu
1993b 72 73 6f 72 20 29 3b 0a 20 20 73 71 6c 69 74 65  rsor );.  sqlite
1993c 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28  3VdbeFreeCursor(
1993d 70 2c 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 3b  p, p->apCsr[i]);
1993e 0a 20 20 70 2d 3e 61 70 43 73 72 5b 69 5d 20 3d  .  p->apCsr[i] =
1993f 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a   0;.  break;.}..
19940 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 47  /* Opcode: MoveG
19941 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a  e P1 P2 P3 P4 *.
19942 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20  **.** If cursor 
19943 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20  P1 refers to an 
19944 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65  SQL table (B-Tre
19945 65 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65  e that uses inte
19946 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75  ger keys), .** u
19947 73 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 76  se the integer v
19948 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
19949 20 50 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66   P3 as a key. If
1994a 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72   cursor P1 refer
1994b 73 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20  s .** to an SQL 
1994c 69 6e 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69  index, then P3 i
1994d 73 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61  s the first in a
1994e 6e 20 61 72 72 61 79 20 6f 66 20 50 34 20 72 65  n array of P4 re
1994f 67 69 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74  gisters .** that
19950 20 61 72 65 20 75 73 65 64 20 61 73 20 61 6e 20   are used as an 
19951 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b  unpacked index k
19952 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73  ey. .**.** Repos
19953 69 74 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20  ition cursor P1 
19954 73 6f 20 74 68 61 74 20 20 69 74 20 70 6f 69 6e  so that  it poin
19955 74 73 20 74 6f 20 74 68 65 20 73 6d 61 6c 6c 65  ts to the smalle
19956 73 74 20 65 6e 74 72 79 20 74 68 61 74 20 0a 2a  st entry that .*
19957 2a 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  * is greater tha
19958 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68  n or equal to th
19959 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20  e key value. If 
1995a 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63  there are no rec
1995b 6f 72 64 73 20 0a 2a 2a 20 67 72 65 61 74 65 72  ords .** greater
1995c 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
1995d 6f 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32  o the key and P2
1995e 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68   is not zero, th
1995f 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a  en jump to P2..*
19960 2a 0a 2a 2a 20 41 20 73 70 65 63 69 61 6c 20 66  *.** A special f
19961 65 61 74 75 72 65 20 6f 66 20 74 68 69 73 20 6f  eature of this o
19962 70 63 6f 64 65 20 28 61 6e 64 20 64 69 66 66 65  pcode (and diffe
19963 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 0a 2a 2a  rent from the.**
19964 20 72 65 6c 61 74 65 64 20 4f 50 5f 4d 6f 76 65   related OP_Move
19965 47 74 2c 20 4f 50 5f 4d 6f 76 65 4c 74 2c 20 61  Gt, OP_MoveLt, a
19966 6e 64 20 4f 50 5f 4d 6f 76 65 4c 65 29 20 69 73  nd OP_MoveLe) is
19967 20 74 68 61 74 20 69 66 20 50 32 20 69 73 0a 2a   that if P2 is.*
19968 2a 20 7a 65 72 6f 20 61 6e 64 20 50 31 20 69 73  * zero and P1 is
19969 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 61   an SQL table (a
1996a 20 62 2d 74 72 65 65 20 77 69 74 68 20 69 6e 74   b-tree with int
1996b 65 67 65 72 20 6b 65 79 73 29 20 74 68 65 6e 0a  eger keys) then.
1996c 2a 2a 20 74 68 65 20 73 65 65 6b 20 69 73 20 64  ** the seek is d
1996d 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20 69 74  eferred until it
1996e 20 69 73 20 61 63 74 75 61 6c 6c 79 20 6e 65 65   is actually nee
1996f 64 65 64 2e 20 20 49 74 20 6d 69 67 68 74 20 62  ded.  It might b
19970 65 0a 2a 2a 20 74 68 65 20 63 61 73 65 20 74 68  e.** the case th
19971 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  at the cursor is
19972 20 6e 65 76 65 72 20 61 63 63 65 73 73 65 64 2e   never accessed.
19973 20 20 42 79 20 64 65 66 65 72 72 69 6e 67 20 74    By deferring t
19974 68 65 0a 2a 2a 20 73 65 65 6b 2c 20 77 65 20 61  he.** seek, we a
19975 76 6f 69 64 20 75 6e 6e 65 63 65 73 73 61 72 79  void unnecessary
19976 20 73 65 65 6b 73 2e 0a 2a 2a 0a 2a 2a 20 53 65   seeks..**.** Se
19977 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e  e also: Found, N
19978 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63  otFound, Distinc
19979 74 2c 20 4d 6f 76 65 4c 74 2c 20 4d 6f 76 65 47  t, MoveLt, MoveG
1997a 74 2c 20 4d 6f 76 65 4c 65 0a 2a 2f 0a 2f 2a 20  t, MoveLe.*/./* 
1997b 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 47 74 20 50  Opcode: MoveGt P
1997c 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a  1 P2 P3 P4 *.**.
1997d 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31 20  ** If cursor P1 
1997e 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51 4c  refers to an SQL
1997f 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20 74   table (B-Tree t
19980 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 65 72  hat uses integer
19981 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20   keys), .** use 
19982 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75  the integer valu
19983 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  e in register P3
19984 20 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63 75   as a key. If cu
19985 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a  rsor P1 refers .
19986 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64  ** to an SQL ind
19987 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74  ex, then P3 is t
19988 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61  he first in an a
19989 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 73  rray of P4 regis
1998a 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72  ters .** that ar
1998b 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70  e used as an unp
1998c 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e  acked index key.
1998d 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69   .**.** Repositi
1998e 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20  on cursor P1 so 
1998f 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20  that  it points 
19990 74 6f 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20  to the smallest 
19991 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69  entry that .** i
19992 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74  s greater than t
19993 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 49 66  he key value. If
19994 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65   there are no re
19995 63 6f 72 64 73 20 67 72 65 61 74 65 72 20 74 68  cords greater th
19996 61 6e 20 0a 2a 2a 20 74 68 65 20 6b 65 79 20 61  an .** the key a
19997 6e 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72  nd P2 is not zer
19998 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20  o, then jump to 
19999 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  P2..**.** See al
1999a 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f  so: Found, NotFo
1999b 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 4d  und, Distinct, M
1999c 6f 76 65 4c 74 2c 20 4d 6f 76 65 47 65 2c 20 4d  oveLt, MoveGe, M
1999d 6f 76 65 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f  oveLe.*/./* Opco
1999e 64 65 3a 20 4d 6f 76 65 4c 74 20 50 31 20 50 32  de: MoveLt P1 P2
1999f 20 50 33 20 50 34 20 2a 20 0a 2a 2a 0a 2a 2a 20   P3 P4 * .**.** 
199a0 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66  If cursor P1 ref
199a1 65 72 73 20 74 6f 20 61 6e 20 53 51 4c 20 74 61  ers to an SQL ta
199a2 62 6c 65 20 28 42 2d 54 72 65 65 20 74 68 61 74  ble (B-Tree that
199a3 20 75 73 65 73 20 69 6e 74 65 67 65 72 20 6b 65   uses integer ke
199a4 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65  ys), .** use the
199a5 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69   integer value i
199a6 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73  n register P3 as
199a7 20 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f   a key. If curso
199a8 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20  r P1 refers .** 
199a9 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c  to an SQL index,
199aa 20 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20   then P3 is the 
199ab 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61  first in an arra
199ac 79 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72  y of P4 register
199ad 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75  s .** that are u
199ae 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b  sed as an unpack
199af 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a  ed index key. .*
199b0 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20  *.** Reposition 
199b1 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61  cursor P1 so tha
199b2 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20  t  it points to 
199b3 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72  the largest entr
199b4 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 6c 65  y that .** is le
199b5 73 73 20 74 68 61 6e 20 74 68 65 20 6b 65 79 20  ss than the key 
199b6 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 20  value. If there 
199b7 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 6c  are no records l
199b8 65 73 73 20 74 68 61 6e 20 0a 2a 2a 20 74 68 65  ess than .** the
199b9 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e   key and P2 is n
199ba 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75  ot zero, then ju
199bb 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20  mp to P2..**.** 
199bc 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c  See also: Found,
199bd 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69   NotFound, Disti
199be 6e 63 74 2c 20 4d 6f 76 65 47 74 2c 20 4d 6f 76  nct, MoveGt, Mov
199bf 65 47 65 2c 20 4d 6f 76 65 4c 65 0a 2a 2f 0a 2f  eGe, MoveLe.*/./
199c0 2a 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 4c 65  * Opcode: MoveLe
199c1 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a   P1 P2 P3 P4 *.*
199c2 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50  *.** If cursor P
199c3 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53  1 refers to an S
199c4 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65  QL table (B-Tree
199c5 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67   that uses integ
199c6 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73  er keys), .** us
199c7 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61  e the integer va
199c8 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
199c9 50 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66 20  P3 as a key. If 
199ca 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73  cursor P1 refers
199cb 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69   .** to an SQL i
199cc 6e 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73  ndex, then P3 is
199cd 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e   the first in an
199ce 20 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 67   array of P4 reg
199cf 69 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20  isters .** that 
199d0 61 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 75  are used as an u
199d1 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65  npacked index ke
199d2 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69  y. .**.** Reposi
199d3 74 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73  tion cursor P1 s
199d4 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73  o that it points
199d5 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20   to the largest 
199d6 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69  entry that .** i
199d7 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65  s less than or e
199d8 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20  qual to the key 
199d9 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 20  value. If there 
199da 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 0a  are no records .
199db 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20  ** less than or 
199dc 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79  equal to the key
199dd 20 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 20 7a   and P2 is not z
199de 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74  ero, then jump t
199df 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  o P2..**.** See 
199e0 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74  also: Found, Not
199e1 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c  Found, Distinct,
199e2 20 4d 6f 76 65 47 74 2c 20 4d 6f 76 65 47 65 2c   MoveGt, MoveGe,
199e3 20 4d 6f 76 65 4c 74 0a 2a 2f 0a 63 61 73 65 20   MoveLt.*/.case 
199e4 4f 50 5f 4d 6f 76 65 4c 74 3a 20 20 20 20 20 20  OP_MoveLt:      
199e5 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20     /* jump, in3 
199e6 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65 4c  */.case OP_MoveL
199e7 65 3a 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75  e:         /* ju
199e8 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20  mp, in3 */.case 
199e9 4f 50 5f 4d 6f 76 65 47 65 3a 20 20 20 20 20 20  OP_MoveGe:      
199ea 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20     /* jump, in3 
199eb 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65 47  */.case OP_MoveG
199ec 74 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6a 75  t: {       /* ju
199ed 6d 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74  mp, in3 */.  int
199ee 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20   i = pOp->p1;.  
199ef 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
199f0 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20  .  assert( i>=0 
199f1 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  && i<p->nCursor 
199f2 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43  );.  pC = p->apC
199f3 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28  sr[i];.  assert(
199f4 20 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20   pC!=0 );.  if( 
199f5 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29  pC->pCursor!=0 )
199f6 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 2c 20 6f  {.    int res, o
199f7 63 3b 0a 20 20 20 20 6f 63 20 3d 20 70 4f 70 2d  c;.    oc = pOp-
199f8 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20 70 43 2d  >opcode;.    pC-
199f9 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20  >nullRow = 0;.  
199fa 20 20 69 66 28 20 70 43 2d 3e 69 73 54 61 62 6c    if( pC->isTabl
199fb 65 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 69  e ){.      i64 i
199fc 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62  Key = sqlite3Vdb
199fd 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 33 29 3b  eIntValue(pIn3);
199fe 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  .      if( pOp->
199ff 70 32 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  p2==0 ){.       
19a00 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70   assert( pOp->op
19a01 63 6f 64 65 3d 3d 4f 50 5f 4d 6f 76 65 47 65 20  code==OP_MoveGe 
19a02 29 3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 6d  );.        pC->m
19a03 6f 76 65 74 6f 54 61 72 67 65 74 20 3d 20 69 4b  ovetoTarget = iK
19a04 65 79 3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e  ey;.        pC->
19a05 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30  rowidIsValid = 0
19a06 3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 64 65  ;.        pC->de
19a07 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 31  ferredMoveto = 1
19a08 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
19a09 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
19a0a 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
19a0b 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70  MovetoUnpacked(p
19a0c 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28  C->pCursor, 0, (
19a0d 75 36 34 29 69 4b 65 79 2c 20 30 2c 20 26 72 65  u64)iKey, 0, &re
19a0e 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  s);.      if( rc
19a0f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
19a10 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72         goto abor
19a11 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
19a12 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43        }.      pC
19a13 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 69 4b  ->lastRowid = iK
19a14 65 79 3b 0a 20 20 20 20 20 20 70 43 2d 3e 72 6f  ey;.      pC->ro
19a15 77 69 64 49 73 56 61 6c 69 64 20 3d 20 72 65 73  widIsValid = res
19a16 3d 3d 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ==0;.    }else{.
19a17 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65        UnpackedRe
19a18 63 6f 72 64 20 72 3b 0a 20 20 20 20 20 20 69 6e  cord r;.      in
19a19 74 20 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e  t nField = pOp->
19a1a 70 34 2e 69 3b 0a 20 20 20 20 20 20 61 73 73 65  p4.i;.      asse
19a1b 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d  rt( pOp->p4type=
19a1c 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20  =P4_INT32 );.   
19a1d 20 20 20 61 73 73 65 72 74 28 20 6e 46 69 65 6c     assert( nFiel
19a1e 64 3e 30 20 29 3b 0a 20 20 20 20 20 20 72 2e 70  d>0 );.      r.p
19a1f 4b 65 79 49 6e 66 6f 20 3d 20 70 43 2d 3e 70 4b  KeyInfo = pC->pK
19a20 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 72 2e  eyInfo;.      r.
19a21 6e 46 69 65 6c 64 20 3d 20 6e 46 69 65 6c 64 3b  nField = nField;
19a22 0a 20 20 20 20 20 20 69 66 28 20 6f 63 3d 3d 4f  .      if( oc==O
19a23 50 5f 4d 6f 76 65 47 74 20 7c 7c 20 6f 63 3d 3d  P_MoveGt || oc==
19a24 4f 50 5f 4d 6f 76 65 4c 65 20 29 7b 0a 20 20 20  OP_MoveLe ){.   
19a25 20 20 20 20 20 72 2e 66 6c 61 67 73 20 3d 20 55       r.flags = U
19a26 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 3b  NPACKED_INCRKEY;
19a27 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
19a28 20 20 20 20 20 20 72 2e 66 6c 61 67 73 20 3d 20        r.flags = 
19a29 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
19a2a 20 72 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d   r.aMem = &p->aM
19a2b 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20  em[pOp->p3];.   
19a2c 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
19a2d 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b  treeMovetoUnpack
19a2e 65 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20  ed(pC->pCursor, 
19a2f 26 72 2c 20 30 2c 20 30 2c 20 26 72 65 73 29 3b  &r, 0, 0, &res);
19a30 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
19a31 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
19a32 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64      goto abort_d
19a33 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
19a34 20 20 20 7d 0a 20 20 20 20 20 20 70 43 2d 3e 72     }.      pC->r
19a35 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b  owidIsValid = 0;
19a36 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 2d 3e 64  .    }.    pC->d
19a37 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20  eferredMoveto = 
19a38 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 65  0;.    pC->cache
19a39 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53  Status = CACHE_S
19a3a 54 41 4c 45 3b 0a 23 69 66 64 65 66 20 53 51 4c  TALE;.#ifdef SQL
19a3b 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c  ITE_TEST.    sql
19a3c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e  ite3_search_coun
19a3d 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  t++;.#endif.    
19a3e 69 66 28 20 6f 63 3d 3d 4f 50 5f 4d 6f 76 65 47  if( oc==OP_MoveG
19a3f 65 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 4d 6f 76 65  e || oc==OP_Move
19a40 47 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  Gt ){.      if( 
19a41 72 65 73 3c 30 20 29 7b 0a 20 20 20 20 20 20 20  res<0 ){.       
19a42 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
19a43 65 65 4e 65 78 74 28 70 43 2d 3e 70 43 75 72 73  eeNext(pC->pCurs
19a44 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20  or, &res);.     
19a45 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
19a46 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72  E_OK ) goto abor
19a47 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
19a48 20 20 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69          pC->rowi
19a49 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20  dIsValid = 0;.  
19a4a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
19a4b 20 20 20 72 65 73 20 3d 20 30 3b 0a 20 20 20 20     res = 0;.    
19a4c 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
19a4d 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 63 3d       assert( oc=
19a4e 3d 4f 50 5f 4d 6f 76 65 4c 74 20 7c 7c 20 6f 63  =OP_MoveLt || oc
19a4f 3d 3d 4f 50 5f 4d 6f 76 65 4c 65 20 29 3b 0a 20  ==OP_MoveLe );. 
19a50 20 20 20 20 20 69 66 28 20 72 65 73 3e 3d 30 20       if( res>=0 
19a51 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
19a52 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76  sqlite3BtreePrev
19a53 69 6f 75 73 28 70 43 2d 3e 70 43 75 72 73 6f 72  ious(pC->pCursor
19a54 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20 20  , &res);.       
19a55 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
19a56 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  OK ) goto abort_
19a57 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
19a58 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 49        pC->rowidI
19a59 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20  sValid = 0;.    
19a5a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
19a5b 20 2f 2a 20 72 65 73 20 6d 69 67 68 74 20 62 65   /* res might be
19a5c 20 6e 65 67 61 74 69 76 65 20 62 65 63 61 75 73   negative becaus
19a5d 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65  e the table is e
19a5e 6d 70 74 79 2e 20 20 43 68 65 63 6b 20 74 6f 0a  mpty.  Check to.
19a5f 20 20 20 20 20 20 20 20 2a 2a 20 73 65 65 20 69          ** see i
19a60 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61  f this is the ca
19a61 73 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  se..        */. 
19a62 20 20 20 20 20 20 20 72 65 73 20 3d 20 73 71 6c         res = sql
19a63 69 74 65 33 42 74 72 65 65 45 6f 66 28 70 43 2d  ite3BtreeEof(pC-
19a64 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20  >pCursor);.     
19a65 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73   }.    }.    ass
19a66 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29  ert( pOp->p2>0 )
19a67 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b  ;.    if( res ){
19a68 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  .      pc = pOp-
19a69 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20  >p2 - 1;.    }. 
19a6a 20 7d 65 6c 73 65 20 69 66 28 20 21 70 43 2d 3e   }else if( !pC->
19a6b 70 73 65 75 64 6f 54 61 62 6c 65 20 29 7b 0a 20  pseudoTable ){. 
19a6c 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65     /* This happe
19a6d 6e 73 20 77 68 65 6e 20 61 74 74 65 6d 70 74 69  ns when attempti
19a6e 6e 67 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 73  ng to open the s
19a6f 71 6c 69 74 65 33 5f 6d 61 73 74 65 72 20 74 61  qlite3_master ta
19a70 62 6c 65 0a 20 20 20 20 2a 2a 20 66 6f 72 20 72  ble.    ** for r
19a71 65 61 64 20 61 63 63 65 73 73 20 72 65 74 75 72  ead access retur
19a72 6e 73 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 2e  ns SQLITE_EMPTY.
19a73 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 6c   In this case al
19a74 77 61 79 73 0a 20 20 20 20 2a 2a 20 74 61 6b 65  ways.    ** take
19a75 20 74 68 65 20 6a 75 6d 70 20 28 73 69 6e 63 65   the jump (since
19a76 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65   there are no re
19a77 63 6f 72 64 73 20 69 6e 20 74 68 65 20 74 61 62  cords in the tab
19a78 6c 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  le)..    */.    
19a79 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
19a7a 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
19a7b 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 6f 75  ../* Opcode: Fou
19a7c 6e 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  nd P1 P2 P3 * *.
19a7d 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50  **.** Register P
19a7e 33 20 68 6f 6c 64 73 20 61 20 62 6c 6f 62 20 63  3 holds a blob c
19a7f 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 4d 61  onstructed by Ma
19a80 6b 65 52 65 63 6f 72 64 2e 20 20 50 31 20 69 73  keRecord.  P1 is
19a81 20 61 6e 20 69 6e 64 65 78 2e 0a 2a 2a 20 49 66   an index..** If
19a82 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 20 6d   an entry that m
19a83 61 74 63 68 65 73 20 74 68 65 20 76 61 6c 75 65  atches the value
19a84 20 69 6e 20 72 65 67 69 73 74 65 72 20 70 33 20   in register p3 
19a85 65 78 69 73 74 73 20 69 6e 20 50 31 20 74 68 65  exists in P1 the
19a86 6e 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 50 32 2e  n.** jump to P2.
19a87 20 20 49 66 20 74 68 65 20 50 33 20 76 61 6c 75    If the P3 valu
19a88 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68  e does not match
19a89 20 61 6e 79 20 65 6e 74 72 79 20 69 6e 20 50 31   any entry in P1
19a8a 0a 2a 2a 20 74 68 65 6e 20 66 61 6c 6c 20 74 68  .** then fall th
19a8b 72 75 2e 20 20 54 68 65 20 50 31 20 63 75 72 73  ru.  The P1 curs
19a8c 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74  or is left point
19a8d 69 6e 67 20 61 74 20 74 68 65 20 6d 61 74 63 68  ing at the match
19a8e 69 6e 67 20 65 6e 74 72 79 0a 2a 2a 20 69 66 20  ing entry.** if 
19a8f 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a  it exists..**.**
19a90 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   This instructio
19a91 6e 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70  n is used to imp
19a92 6c 65 6d 65 6e 74 20 74 68 65 20 49 4e 20 6f 70  lement the IN op
19a93 65 72 61 74 6f 72 20 77 68 65 72 65 20 74 68 65  erator where the
19a94 0a 2a 2a 20 6c 65 66 74 2d 68 61 6e 64 20 73 69  .** left-hand si
19a95 64 65 20 69 73 20 61 20 53 45 4c 45 43 54 20 73  de is a SELECT s
19a96 74 61 74 65 6d 65 6e 74 2e 20 20 50 31 20 6d 61  tatement.  P1 ma
19a97 79 20 62 65 20 61 20 74 72 75 65 20 69 6e 64 65  y be a true inde
19a98 78 2c 20 6f 72 20 69 74 0a 2a 2a 20 6d 61 79 20  x, or it.** may 
19a99 62 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 69  be a temporary i
19a9a 6e 64 65 78 20 74 68 61 74 20 68 6f 6c 64 73 20  ndex that holds 
19a9b 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74  the results of t
19a9c 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 73 74 61  he SELECT.** sta
19a9d 74 65 6d 65 6e 74 2e 20 20 20 54 68 69 73 20 69  tement.   This i
19a9e 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 6c  nstruction is al
19a9f 73 6f 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  so used to imple
19aa0 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 44 49 53 54  ment the.** DIST
19aa1 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 69 6e 20  INCT keyword in 
19aa2 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
19aa3 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  s..**.** This in
19aa4 73 74 72 75 63 74 69 6f 6e 20 63 68 65 63 6b 73  struction checks
19aa5 20 69 66 20 69 6e 64 65 78 20 50 31 20 63 6f 6e   if index P1 con
19aa6 74 61 69 6e 73 20 61 20 72 65 63 6f 72 64 20 66  tains a record f
19aa7 6f 72 20 77 68 69 63 68 20 0a 2a 2a 20 74 68 65  or which .** the
19aa8 20 66 69 72 73 74 20 4e 20 73 65 72 69 61 6c 69   first N seriali
19aa9 7a 65 64 20 76 61 6c 75 65 73 20 65 78 61 63 74  zed values exact
19aaa 6c 79 20 6d 61 74 63 68 20 74 68 65 20 4e 20 73  ly match the N s
19aab 65 72 69 61 6c 69 7a 65 64 20 76 61 6c 75 65 73  erialized values
19aac 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 63 6f 72  .** in the recor
19aad 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  d in register P3
19aae 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65  , where N is the
19aaf 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
19ab0 20 76 61 6c 75 65 73 20 69 6e 0a 2a 2a 20 74 68   values in.** th
19ab1 65 20 50 33 20 72 65 63 6f 72 64 20 28 74 68 65  e P3 record (the
19ab2 20 50 33 20 72 65 63 6f 72 64 20 69 73 20 61 20   P3 record is a 
19ab3 70 72 65 66 69 78 20 6f 66 20 74 68 65 20 50 31  prefix of the P1
19ab4 20 72 65 63 6f 72 64 29 2e 20 0a 2a 2a 0a 2a 2a   record). .**.**
19ab5 20 53 65 65 20 61 6c 73 6f 3a 20 4e 6f 74 46 6f   See also: NotFo
19ab6 75 6e 64 2c 20 49 73 55 6e 69 71 75 65 2c 20 4e  und, IsUnique, N
19ab7 6f 74 45 78 69 73 74 73 0a 2a 2f 0a 2f 2a 20 4f  otExists.*/./* O
19ab8 70 63 6f 64 65 3a 20 4e 6f 74 46 6f 75 6e 64 20  pcode: NotFound 
19ab9 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
19aba 2a 2a 20 52 65 67 69 73 74 65 72 20 50 33 20 68  ** Register P3 h
19abb 6f 6c 64 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73  olds a blob cons
19abc 74 72 75 63 74 65 64 20 62 79 20 4d 61 6b 65 52  tructed by MakeR
19abd 65 63 6f 72 64 2e 20 20 50 31 20 69 73 0a 2a 2a  ecord.  P1 is.**
19abe 20 61 6e 20 69 6e 64 65 78 2e 20 20 49 66 20 6e   an index.  If n
19abf 6f 20 65 6e 74 72 79 20 65 78 69 73 74 73 20 69  o entry exists i
19ac0 6e 20 50 31 20 74 68 61 74 20 6d 61 74 63 68 65  n P1 that matche
19ac1 73 20 74 68 65 20 62 6c 6f 62 20 74 68 65 6e 20  s the blob then 
19ac2 6a 75 6d 70 0a 2a 2a 20 74 6f 20 50 32 2e 20 20  jump.** to P2.  
19ac3 49 66 20 61 6e 20 65 6e 74 72 79 20 64 6f 65 73  If an entry does
19ac4 20 65 78 69 73 74 69 6e 67 2c 20 66 61 6c 6c 20   existing, fall 
19ac5 74 68 72 6f 75 67 68 2e 20 20 54 68 65 20 63 75  through.  The cu
19ac6 72 73 6f 72 20 69 73 20 6c 65 66 74 0a 2a 2a 20  rsor is left.** 
19ac7 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20  pointing to the 
19ac8 65 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63 68  entry that match
19ac9 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  es..**.** See al
19aca 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 45 78  so: Found, NotEx
19acb 69 73 74 73 2c 20 49 73 55 6e 69 71 75 65 0a 2a  ists, IsUnique.*
19acc 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 46 6f 75  /.case OP_NotFou
19acd 6e 64 3a 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d  nd:       /* jum
19ace 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f  p, in3 */.case O
19acf 50 5f 46 6f 75 6e 64 3a 20 7b 20 20 20 20 20 20  P_Found: {      
19ad0 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a    /* jump, in3 *
19ad1 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d  /.  int i = pOp-
19ad2 3e 70 31 3b 0a 20 20 69 6e 74 20 61 6c 72 65 61  >p1;.  int alrea
19ad3 64 79 45 78 69 73 74 73 20 3d 20 30 3b 0a 20 20  dyExists = 0;.  
19ad4 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
19ad5 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
19ad6 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  & i<p->nCursor )
19ad7 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61  ;.  assert( p->a
19ad8 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20  pCsr[i]!=0 );.  
19ad9 69 66 28 20 28 70 43 20 3d 20 70 2d 3e 61 70 43  if( (pC = p->apC
19ada 73 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 21  sr[i])->pCursor!
19adb 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65  =0 ){.    int re
19adc 73 3b 0a 20 20 20 20 55 6e 70 61 63 6b 65 64 52  s;.    UnpackedR
19add 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b 0a  ecord *pIdxKey;.
19ade 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d  .    assert( pC-
19adf 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20  >isTable==0 );. 
19ae0 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d     assert( pIn3-
19ae1 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f  >flags & MEM_Blo
19ae2 62 20 29 3b 0a 20 20 20 20 70 49 64 78 4b 65 79  b );.    pIdxKey
19ae3 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65   = sqlite3VdbeRe
19ae4 63 6f 72 64 55 6e 70 61 63 6b 28 70 43 2d 3e 70  cordUnpack(pC->p
19ae5 4b 65 79 49 6e 66 6f 2c 20 70 49 6e 33 2d 3e 6e  KeyInfo, pIn3->n
19ae6 2c 20 70 49 6e 33 2d 3e 7a 2c 0a 20 20 20 20 20  , pIn3->z,.     
19ae7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19ae8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19ae9 20 61 54 65 6d 70 52 65 63 2c 20 73 69 7a 65 6f   aTempRec, sizeo
19aea 66 28 61 54 65 6d 70 52 65 63 29 29 3b 0a 20 20  f(aTempRec));.  
19aeb 20 20 69 66 28 20 70 49 64 78 4b 65 79 3d 3d 30    if( pIdxKey==0
19aec 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e   ){.      goto n
19aed 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20  o_mem;.    }.   
19aee 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
19aef 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 20  ==OP_Found ){.  
19af0 20 20 20 20 70 49 64 78 4b 65 79 2d 3e 66 6c 61      pIdxKey->fla
19af1 67 73 20 7c 3d 20 55 4e 50 41 43 4b 45 44 5f 50  gs |= UNPACKED_P
19af2 52 45 46 49 58 5f 4d 41 54 43 48 3b 0a 20 20 20  REFIX_MATCH;.   
19af3 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69   }.    rc = sqli
19af4 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e  te3BtreeMovetoUn
19af5 70 61 63 6b 65 64 28 70 43 2d 3e 70 43 75 72 73  packed(pC->pCurs
19af6 6f 72 2c 20 70 49 64 78 4b 65 79 2c 20 30 2c 20  or, pIdxKey, 0, 
19af7 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 73 71  0, &res);.    sq
19af8 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55  lite3VdbeDeleteU
19af9 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 70 49  npackedRecord(pI
19afa 64 78 4b 65 79 29 3b 0a 20 20 20 20 69 66 28 20  dxKey);.    if( 
19afb 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
19afc 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
19afd 20 20 7d 0a 20 20 20 20 61 6c 72 65 61 64 79 45    }.    alreadyE
19afe 78 69 73 74 73 20 3d 20 28 72 65 73 3d 3d 30 29  xists = (res==0)
19aff 3b 0a 20 20 20 20 70 43 2d 3e 64 65 66 65 72 72  ;.    pC->deferr
19b00 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20  edMoveto = 0;.  
19b01 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75    pC->cacheStatu
19b02 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b  s = CACHE_STALE;
19b03 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e  .  }.  if( pOp->
19b04 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 6f 75 6e 64  opcode==OP_Found
19b05 20 29 7b 0a 20 20 20 20 69 66 28 20 61 6c 72 65   ){.    if( alre
19b06 61 64 79 45 78 69 73 74 73 20 29 20 70 63 20 3d  adyExists ) pc =
19b07 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
19b08 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 21  }else{.    if( !
19b09 61 6c 72 65 61 64 79 45 78 69 73 74 73 20 29 20  alreadyExists ) 
19b0a 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
19b0b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
19b0c 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 73 55  ../* Opcode: IsU
19b0d 6e 69 71 75 65 20 50 31 20 50 32 20 50 33 20 50  nique P1 P2 P3 P
19b0e 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 33  4 *.**.** The P3
19b0f 20 72 65 67 69 73 74 65 72 20 63 6f 6e 74 61 69   register contai
19b10 6e 73 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65  ns an integer re
19b11 63 6f 72 64 20 6e 75 6d 62 65 72 2e 20 20 43 61  cord number.  Ca
19b12 6c 6c 20 74 68 69 73 0a 2a 2a 20 72 65 63 6f 72  ll this.** recor
19b13 64 20 6e 75 6d 62 65 72 20 52 2e 20 20 54 68 65  d number R.  The
19b14 20 50 34 20 72 65 67 69 73 74 65 72 20 63 6f 6e   P4 register con
19b15 74 61 69 6e 73 20 61 6e 20 69 6e 64 65 78 20 6b  tains an index k
19b16 65 79 20 63 72 65 61 74 65 64 0a 2a 2a 20 75 73  ey created.** us
19b17 69 6e 67 20 4d 61 6b 65 52 65 63 6f 72 64 2e 20  ing MakeRecord. 
19b18 20 43 61 6c 6c 20 69 74 20 4b 2e 0a 2a 2a 0a 2a   Call it K..**.*
19b19 2a 20 50 31 20 69 73 20 61 6e 20 69 6e 64 65 78  * P1 is an index
19b1a 2e 20 20 53 6f 20 69 74 20 68 61 73 20 6e 6f 20  .  So it has no 
19b1b 64 61 74 61 20 61 6e 64 20 69 74 73 20 6b 65 79  data and its key
19b1c 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 0a 2a   consists of a.*
19b1d 2a 20 72 65 63 6f 72 64 20 67 65 6e 65 72 61 74  * record generat
19b1e 65 64 20 62 79 20 4f 50 5f 4d 61 6b 65 52 65 63  ed by OP_MakeRec
19b1f 6f 72 64 20 77 68 65 72 65 20 74 68 65 20 6c 61  ord where the la
19b20 73 74 20 66 69 65 6c 64 20 69 73 20 74 68 65 20  st field is the 
19b21 0a 2a 2a 20 72 6f 77 69 64 20 6f 66 20 74 68 65  .** rowid of the
19b22 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20   entry that the 
19b23 69 6e 64 65 78 20 72 65 66 65 72 73 20 74 6f 2e  index refers to.
19b24 0a 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 69 6e 73  .** .** This ins
19b25 74 72 75 63 74 69 6f 6e 20 61 73 6b 73 20 69 66  truction asks if
19b26 20 74 68 65 72 65 20 69 73 20 61 6e 20 65 6e 74   there is an ent
19b27 72 79 20 69 6e 20 50 31 20 77 68 65 72 65 20 74  ry in P1 where t
19b28 68 65 0a 2a 2a 20 66 69 65 6c 64 73 20 6d 61 74  he.** fields mat
19b29 63 68 65 73 20 4b 20 62 75 74 20 74 68 65 20 72  ches K but the r
19b2a 6f 77 69 64 20 69 73 20 64 69 66 66 65 72 65 6e  owid is differen
19b2b 74 20 66 72 6f 6d 20 52 2e 0a 2a 2a 20 49 66 20  t from R..** If 
19b2c 74 68 65 72 65 20 69 73 20 6e 6f 20 73 75 63 68  there is no such
19b2d 20 65 6e 74 72 79 2c 20 74 68 65 6e 20 74 68 65   entry, then the
19b2e 72 65 20 69 73 20 61 6e 20 69 6d 6d 65 64 69 61  re is an immedia
19b2f 74 65 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 50 32  te.** jump to P2
19b30 2e 20 20 49 66 20 61 6e 79 20 65 6e 74 72 79 20  .  If any entry 
19b31 64 6f 65 73 20 65 78 69 73 74 20 77 68 65 72 65  does exist where
19b32 20 74 68 65 20 69 6e 64 65 78 20 73 74 72 69 6e   the index strin
19b33 67 0a 2a 2a 20 6d 61 74 63 68 65 73 20 4b 20 62  g.** matches K b
19b34 75 74 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75  ut the record nu
19b35 6d 62 65 72 20 69 73 20 6e 6f 74 20 52 2c 20 74  mber is not R, t
19b36 68 65 6e 20 74 68 65 20 72 65 63 6f 72 64 0a 2a  hen the record.*
19b37 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61  * number for tha
19b38 74 20 65 6e 74 72 79 20 69 73 20 77 72 69 74 74  t entry is writt
19b39 65 6e 20 69 6e 74 6f 20 50 33 20 61 6e 64 20 63  en into P3 and c
19b3a 6f 6e 74 72 6f 6c 0a 2a 2a 20 66 61 6c 6c 73 20  ontrol.** falls 
19b3b 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e  through to the n
19b3c 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  ext instruction.
19b3d 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
19b3e 20 4e 6f 74 46 6f 75 6e 64 2c 20 4e 6f 74 45 78   NotFound, NotEx
19b3f 69 73 74 73 2c 20 46 6f 75 6e 64 0a 2a 2f 0a 63  ists, Found.*/.c
19b40 61 73 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 3a  ase OP_IsUnique:
19b41 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d   {        /* jum
19b42 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20  p, in3 */.  int 
19b43 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56  i = pOp->p1;.  V
19b44 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a  dbeCursor *pCx;.
19b45 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73    BtCursor *pCrs
19b46 72 3b 0a 20 20 4d 65 6d 20 2a 70 4b 3b 0a 20 20  r;.  Mem *pK;.  
19b47 69 36 34 20 52 3b 0a 0a 20 20 2f 2a 20 50 6f 70  i64 R;..  /* Pop
19b48 20 74 68 65 20 76 61 6c 75 65 20 52 20 6f 66 66   the value R off
19b49 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20   the top of the 
19b4a 73 74 61 63 6b 0a 20 20 2a 2f 0a 20 20 61 73 73  stack.  */.  ass
19b4b 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65  ert( pOp->p4type
19b4c 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20  ==P4_INT32 );.  
19b4d 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e  assert( pOp->p4.
19b4e 69 3e 30 20 26 26 20 70 4f 70 2d 3e 70 34 2e 69  i>0 && pOp->p4.i
19b4f 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70  <=p->nMem );.  p
19b50 4b 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  K = &p->aMem[pOp
19b51 2d 3e 70 34 2e 69 5d 3b 0a 20 20 73 71 6c 69 74  ->p4.i];.  sqlit
19b52 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72  e3VdbeMemInteger
19b53 69 66 79 28 70 49 6e 33 29 3b 0a 20 20 52 20 3d  ify(pIn3);.  R =
19b54 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 61 73   pIn3->u.i;.  as
19b55 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
19b56 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
19b57 70 43 78 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69  pCx = p->apCsr[i
19b58 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 78  ];.  assert( pCx
19b59 21 3d 30 20 29 3b 0a 20 20 70 43 72 73 72 20 3d  !=0 );.  pCrsr =
19b5a 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 3b 0a 20   pCx->pCursor;. 
19b5b 20 69 66 28 20 70 43 72 73 72 21 3d 30 20 29 7b   if( pCrsr!=0 ){
19b5c 0a 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20  .    int res;.  
19b5d 20 20 69 36 34 20 76 3b 20 20 20 20 20 20 20 20    i64 v;        
19b5e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19b5f 54 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  The record numbe
19b60 72 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 4b  r that matches K
19b61 20 2a 2f 0a 20 20 20 20 55 6e 70 61 63 6b 65 64   */.    Unpacked
19b62 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b  Record *pIdxKey;
19b63 20 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 76     /* Unpacked v
19b64 65 72 73 69 6f 6e 20 6f 66 20 50 34 20 2a 2f 0a  ersion of P4 */.
19b65 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72  .    /* Make sur
19b66 65 20 4b 20 69 73 20 61 20 73 74 72 69 6e 67 20  e K is a string 
19b67 61 6e 64 20 6d 61 6b 65 20 7a 4b 65 79 20 70 6f  and make zKey po
19b68 69 6e 74 20 74 6f 20 4b 0a 20 20 20 20 2a 2f 0a  int to K.    */.
19b69 20 20 20 20 61 73 73 65 72 74 28 20 70 4b 2d 3e      assert( pK->
19b6a 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62  flags & MEM_Blob
19b6b 20 29 3b 0a 20 20 20 20 70 49 64 78 4b 65 79 20   );.    pIdxKey 
19b6c 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63  = sqlite3VdbeRec
19b6d 6f 72 64 55 6e 70 61 63 6b 28 70 43 78 2d 3e 70  ordUnpack(pCx->p
19b6e 4b 65 79 49 6e 66 6f 2c 20 70 4b 2d 3e 6e 2c 20  KeyInfo, pK->n, 
19b6f 70 4b 2d 3e 7a 2c 0a 20 20 20 20 20 20 20 20 20  pK->z,.         
19b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19b71 20 20 20 20 20 20 20 20 20 20 20 20 20 61 54 65               aTe
19b72 6d 70 52 65 63 2c 20 73 69 7a 65 6f 66 28 61 54  mpRec, sizeof(aT
19b73 65 6d 70 52 65 63 29 29 3b 0a 20 20 20 20 69 66  empRec));.    if
19b74 28 20 70 49 64 78 4b 65 79 3d 3d 30 20 29 7b 0a  ( pIdxKey==0 ){.
19b75 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65        goto no_me
19b76 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 64  m;.    }.    pId
19b77 78 4b 65 79 2d 3e 66 6c 61 67 73 20 7c 3d 20 55  xKey->flags |= U
19b78 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52  NPACKED_IGNORE_R
19b79 4f 57 49 44 3b 0a 0a 20 20 20 20 2f 2a 20 53 65  OWID;..    /* Se
19b7a 61 72 63 68 20 66 6f 72 20 61 6e 20 65 6e 74 72  arch for an entr
19b7b 79 20 69 6e 20 50 31 20 77 68 65 72 65 20 61 6c  y in P1 where al
19b7c 6c 20 62 75 74 20 74 68 65 20 6c 61 73 74 20 72  l but the last r
19b7d 6f 77 69 64 20 6d 61 74 63 68 20 4b 0a 20 20 20  owid match K.   
19b7e 20 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   ** If there is 
19b7f 6e 6f 20 73 75 63 68 20 65 6e 74 72 79 2c 20 6a  no such entry, j
19b80 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  ump immediately 
19b81 74 6f 20 50 32 2e 0a 20 20 20 20 2a 2f 0a 20 20  to P2..    */.  
19b82 20 20 61 73 73 65 72 74 28 20 70 43 78 2d 3e 64    assert( pCx->d
19b83 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30  eferredMoveto==0
19b84 20 29 3b 0a 20 20 20 20 70 43 78 2d 3e 63 61 63   );.    pCx->cac
19b85 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
19b86 5f 53 54 41 4c 45 3b 0a 20 20 20 20 72 63 20 3d  _STALE;.    rc =
19b87 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
19b88 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 72 73  etoUnpacked(pCrs
19b89 72 2c 20 70 49 64 78 4b 65 79 2c 20 30 2c 20 30  r, pIdxKey, 0, 0
19b8a 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28  , &res);.    if(
19b8b 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
19b8c 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
19b8d 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65  dbeDeleteUnpacke
19b8e 64 52 65 63 6f 72 64 28 70 49 64 78 4b 65 79 29  dRecord(pIdxKey)
19b8f 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f  ;.      goto abo
19b90 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
19b91 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
19b92 65 73 3c 30 20 29 7b 0a 20 20 20 20 20 20 72 63  es<0 ){.      rc
19b93 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e   = sqlite3BtreeN
19b94 65 78 74 28 70 43 72 73 72 2c 20 26 72 65 73 29  ext(pCrsr, &res)
19b95 3b 0a 20 20 20 20 20 20 69 66 28 20 72 65 73 20  ;.      if( res 
19b96 29 7b 0a 20 20 20 20 20 20 20 20 70 63 20 3d 20  ){.        pc = 
19b97 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20  pOp->p2 - 1;.   
19b98 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
19b99 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65  DeleteUnpackedRe
19b9a 63 6f 72 64 28 70 49 64 78 4b 65 79 29 3b 0a 20  cord(pIdxKey);. 
19b9b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
19b9c 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
19b9d 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
19b9e 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 70 43  IdxKeyCompare(pC
19b9f 78 2c 20 70 49 64 78 4b 65 79 2c 20 26 72 65 73  x, pIdxKey, &res
19ba0 29 3b 20 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ); .    sqlite3V
19ba1 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65  dbeDeleteUnpacke
19ba2 64 52 65 63 6f 72 64 28 70 49 64 78 4b 65 79 29  dRecord(pIdxKey)
19ba3 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
19ba4 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61  LITE_OK ) goto a
19ba5 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
19ba6 72 3b 0a 20 20 20 20 69 66 28 20 72 65 73 3e 30  r;.    if( res>0
19ba7 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70   ){.      pc = p
19ba8 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20  Op->p2 - 1;.    
19ba9 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a    break;.    }..
19baa 20 20 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70      /* At this p
19bab 6f 69 6e 74 2c 20 70 43 72 73 72 20 69 73 20 70  oint, pCrsr is p
19bac 6f 69 6e 74 69 6e 67 20 74 6f 20 61 6e 20 65 6e  ointing to an en
19bad 74 72 79 20 69 6e 20 50 31 20 77 68 65 72 65 20  try in P1 where 
19bae 61 6c 6c 20 62 75 74 0a 20 20 20 20 2a 2a 20 74  all but.    ** t
19baf 68 65 20 66 69 6e 61 6c 20 65 6e 74 72 79 20 28  he final entry (
19bb0 74 68 65 20 72 6f 77 69 64 29 20 6d 61 74 63 68  the rowid) match
19bb1 65 73 20 4b 2e 20 20 43 68 65 63 6b 20 74 6f 20  es K.  Check to 
19bb2 73 65 65 20 69 66 20 74 68 65 0a 20 20 20 20 2a  see if the.    *
19bb3 2a 20 66 69 6e 61 6c 20 72 6f 77 69 64 20 63 6f  * final rowid co
19bb4 6c 75 6d 6e 20 69 73 20 64 69 66 66 65 72 65 6e  lumn is differen
19bb5 74 20 66 72 6f 6d 20 52 2e 20 20 49 66 20 69 74  t from R.  If it
19bb6 20 65 71 75 61 6c 73 20 52 20 74 68 65 6e 20 6a   equals R then j
19bb7 75 6d 70 0a 20 20 20 20 2a 2a 20 69 6d 6d 65 64  ump.    ** immed
19bb8 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 20 20  iately to P2..  
19bb9 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71    */.    rc = sq
19bba 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69  lite3VdbeIdxRowi
19bbb 64 28 70 43 72 73 72 2c 20 26 76 29 3b 0a 20 20  d(pCrsr, &v);.  
19bbc 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
19bbd 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  _OK ){.      got
19bbe 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
19bbf 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rror;.    }.    
19bc0 69 66 28 20 76 3d 3d 52 20 29 7b 0a 20 20 20 20  if( v==R ){.    
19bc1 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
19bc2 20 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   1;.      break;
19bc3 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54  .    }..    /* T
19bc4 68 65 20 66 69 6e 61 6c 20 76 61 72 69 6e 74 20  he final varint 
19bc5 6f 66 20 74 68 65 20 6b 65 79 20 69 73 20 64 69  of the key is di
19bc6 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 52 2e 20  fferent from R. 
19bc7 20 53 74 6f 72 65 20 69 74 20 62 61 63 6b 0a 20   Store it back. 
19bc8 20 20 20 2a 2a 20 69 6e 74 6f 20 72 65 67 69 73     ** into regis
19bc9 74 65 72 20 52 33 2e 20 20 28 54 68 65 20 72 65  ter R3.  (The re
19bca 63 6f 72 64 20 6e 75 6d 62 65 72 20 6f 66 20 61  cord number of a
19bcb 6e 20 65 6e 74 72 79 20 74 68 61 74 20 76 69 6f  n entry that vio
19bcc 6c 61 74 65 73 0a 20 20 20 20 2a 2a 20 61 20 55  lates.    ** a U
19bcd 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74  NIQUE constraint
19bce 2e 29 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 49  .).    */.    pI
19bcf 6e 33 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 20  n3->u.i = v;.   
19bd0 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d 3e 66   assert( pIn3->f
19bd1 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a  lags&MEM_Int );.
19bd2 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
19bd3 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 45 78  /* Opcode: NotEx
19bd4 69 73 74 73 20 50 31 20 50 32 20 50 33 20 2a 20  ists P1 P2 P3 * 
19bd5 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20  *.**.** Use the 
19bd6 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73  content of regis
19bd7 74 65 72 20 50 33 20 61 73 20 61 20 69 6e 74 65  ter P3 as a inte
19bd8 67 65 72 20 6b 65 79 2e 20 20 49 66 20 61 20 72  ger key.  If a r
19bd9 65 63 6f 72 64 20 0a 2a 2a 20 77 69 74 68 20 74  ecord .** with t
19bda 68 61 74 20 6b 65 79 20 64 6f 65 73 20 6e 6f 74  hat key does not
19bdb 20 65 78 69 73 74 20 69 6e 20 74 61 62 6c 65 20   exist in table 
19bdc 6f 66 20 50 31 2c 20 74 68 65 6e 20 6a 75 6d 70  of P1, then jump
19bdd 20 74 6f 20 50 32 2e 20 0a 2a 2a 20 49 66 20 74   to P2. .** If t
19bde 68 65 20 72 65 63 6f 72 64 20 64 6f 65 73 20 65  he record does e
19bdf 78 69 73 74 2c 20 74 68 65 6e 20 66 61 6c 6c 20  xist, then fall 
19be0 74 68 72 75 2e 20 20 54 68 65 20 63 75 72 73 6f  thru.  The curso
19be1 72 20 69 73 20 6c 65 66 74 20 0a 2a 2a 20 70 6f  r is left .** po
19be2 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 72 65  inting to the re
19be3 63 6f 72 64 20 69 66 20 69 74 20 65 78 69 73 74  cord if it exist
19be4 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 66  s..**.** The dif
19be5 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20  ference between 
19be6 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 61  this operation a
19be7 6e 64 20 4e 6f 74 46 6f 75 6e 64 20 69 73 20 74  nd NotFound is t
19be8 68 61 74 20 74 68 69 73 0a 2a 2a 20 6f 70 65 72  hat this.** oper
19be9 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68  ation assumes th
19bea 65 20 6b 65 79 20 69 73 20 61 6e 20 69 6e 74 65  e key is an inte
19beb 67 65 72 20 61 6e 64 20 74 68 61 74 20 50 31 20  ger and that P1 
19bec 69 73 20 61 20 74 61 62 6c 65 20 77 68 65 72 65  is a table where
19bed 61 73 0a 2a 2a 20 4e 6f 74 46 6f 75 6e 64 20 61  as.** NotFound a
19bee 73 73 75 6d 65 73 20 6b 65 79 20 69 73 20 61 20  ssumes key is a 
19bef 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 64  blob constructed
19bf0 20 66 72 6f 6d 20 4d 61 6b 65 52 65 63 6f 72 64   from MakeRecord
19bf1 20 61 6e 64 0a 2a 2a 20 50 31 20 69 73 20 61 6e   and.** P1 is an
19bf2 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 53 65   index..**.** Se
19bf3 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e  e also: Found, N
19bf4 6f 74 46 6f 75 6e 64 2c 20 49 73 55 6e 69 71 75  otFound, IsUniqu
19bf5 65 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74  e.*/.case OP_Not
19bf6 45 78 69 73 74 73 3a 20 7b 20 20 20 20 20 20 20  Exists: {       
19bf7 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f   /* jump, in3 */
19bf8 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e  .  int i = pOp->
19bf9 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72  p1;.  VdbeCursor
19bfa 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72   *pC;.  BtCursor
19bfb 20 2a 70 43 72 73 72 3b 0a 20 20 61 73 73 65 72   *pCrsr;.  asser
19bfc 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e  t( i>=0 && i<p->
19bfd 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73  nCursor );.  ass
19bfe 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d  ert( p->apCsr[i]
19bff 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43  !=0 );.  if( (pC
19c00 72 73 72 20 3d 20 28 70 43 20 3d 20 70 2d 3e 61  rsr = (pC = p->a
19c01 70 43 73 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f  pCsr[i])->pCurso
19c02 72 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  r)!=0 ){.    int
19c03 20 72 65 73 3b 0a 20 20 20 20 75 36 34 20 69 4b   res;.    u64 iK
19c04 65 79 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ey;.    assert( 
19c05 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn3->flags & ME
19c06 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 61 73 73  M_Int );.    ass
19c07 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d  ert( p->apCsr[i]
19c08 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20  ->isTable );.   
19c09 20 69 4b 65 79 20 3d 20 69 6e 74 54 6f 4b 65 79   iKey = intToKey
19c0a 28 70 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 20  (pIn3->u.i);.   
19c0b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
19c0c 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64  eeMovetoUnpacked
19c0d 28 70 43 72 73 72 2c 20 30 2c 20 69 4b 65 79 2c  (pCrsr, 0, iKey,
19c0e 20 30 2c 26 72 65 73 29 3b 0a 20 20 20 20 70 43   0,&res);.    pC
19c0f 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 49  ->lastRowid = pI
19c10 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20 20 70 43 2d  n3->u.i;.    pC-
19c11 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20  >rowidIsValid = 
19c12 72 65 73 3d 3d 30 3b 0a 20 20 20 20 70 43 2d 3e  res==0;.    pC->
19c13 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 20  nullRow = 0;.   
19c14 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73   pC->cacheStatus
19c15 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a   = CACHE_STALE;.
19c16 20 20 20 20 2f 2a 20 72 65 73 20 6d 69 67 68 74      /* res might
19c17 20 62 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65   be uninitialize
19c18 64 20 69 66 20 72 63 21 3d 53 51 4c 49 54 45 5f  d if rc!=SQLITE_
19c19 4f 4b 2e 20 20 42 75 74 20 69 66 20 72 63 21 3d  OK.  But if rc!=
19c1a 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 20 20 2a 2a  SQLITE_OK.    **
19c1b 20 70 72 6f 63 65 73 73 69 6e 67 20 69 73 20 61   processing is a
19c1c 62 6f 75 74 20 74 6f 20 61 62 6f 72 74 20 73 6f  bout to abort so
19c1d 20 77 65 20 72 65 61 6c 6c 79 20 64 6f 20 6e 6f   we really do no
19c1e 74 20 63 61 72 65 20 77 68 65 74 68 65 72 20 6f  t care whether o
19c1f 72 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 74 68 65  r not.    ** the
19c20 20 66 6f 6c 6c 6f 77 69 6e 67 20 6a 75 6d 70 20   following jump 
19c21 69 73 20 74 61 6b 65 6e 2e 20 20 28 49 6e 20 6f  is taken.  (In o
19c22 74 68 65 72 20 77 6f 72 64 73 2c 20 64 6f 20 6e  ther words, do n
19c23 6f 74 20 73 74 72 65 73 73 20 6f 76 65 72 0a 20  ot stress over. 
19c24 20 20 20 2a 2a 20 74 68 65 20 65 72 72 6f 72 20     ** the error 
19c25 74 68 61 74 20 76 61 6c 67 72 69 6e 64 20 73 6f  that valgrind so
19c26 6d 65 74 69 6d 65 73 20 73 68 6f 77 73 20 6f 6e  metimes shows on
19c27 20 74 68 65 20 6e 65 78 74 20 73 74 61 74 65 6d   the next statem
19c28 65 6e 74 20 77 68 65 6e 0a 20 20 20 20 2a 2a 20  ent when.    ** 
19c29 72 75 6e 6e 69 6e 67 20 69 6f 65 72 72 2e 74 65  running ioerr.te
19c2a 73 74 20 61 6e 64 20 73 69 6d 69 6c 61 72 20 66  st and similar f
19c2b 61 69 6c 75 72 65 2d 72 65 63 6f 76 65 72 79 20  ailure-recovery 
19c2c 74 65 73 74 20 73 63 72 69 70 74 73 2e 29 20 2a  test scripts.) *
19c2d 2f 0a 20 20 20 20 69 66 28 20 72 65 73 21 3d 30  /.    if( res!=0
19c2e 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70   ){.      pc = p
19c2f 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20  Op->p2 - 1;.    
19c30 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 72 6f    assert( pC->ro
19c31 77 69 64 49 73 56 61 6c 69 64 3d 3d 30 20 29 3b  widIsValid==0 );
19c32 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
19c33 66 28 20 21 70 43 2d 3e 70 73 65 75 64 6f 54 61  f( !pC->pseudoTa
19c34 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  ble ){.    /* Th
19c35 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20  is happens when 
19c36 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 6f 70  an attempt to op
19c37 65 6e 20 61 20 72 65 61 64 20 63 75 72 73 6f 72  en a read cursor
19c38 20 6f 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20   on the .    ** 
19c39 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
19c3a 62 6c 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49  ble returns SQLI
19c3b 54 45 5f 45 4d 50 54 59 2e 0a 20 20 20 20 2a 2f  TE_EMPTY..    */
19c3c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d  .    assert( pC-
19c3d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20  >isTable );.    
19c3e 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
19c3f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  ;.    assert( pC
19c40 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 3d 3d  ->rowidIsValid==
19c41 30 20 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  0 );.  }.  break
19c42 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
19c43 53 65 71 75 65 6e 63 65 20 50 31 20 50 32 20 2a  Sequence P1 P2 *
19c44 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 20   * *.**.** Find 
19c45 74 68 65 20 6e 65 78 74 20 61 76 61 69 6c 61 62  the next availab
19c46 6c 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62  le sequence numb
19c47 65 72 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31  er for cursor P1
19c48 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 73  ..** Write the s
19c49 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 69  equence number i
19c4a 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e  nto register P2.
19c4b 0a 2a 2a 20 54 68 65 20 73 65 71 75 65 6e 63 65  .** The sequence
19c4c 20 6e 75 6d 62 65 72 20 6f 6e 20 74 68 65 20 63   number on the c
19c4d 75 72 73 6f 72 20 69 73 20 69 6e 63 72 65 6d 65  ursor is increme
19c4e 6e 74 65 64 20 61 66 74 65 72 20 74 68 69 73 0a  nted after this.
19c4f 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20  ** instruction. 
19c50 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 71   .*/.case OP_Seq
19c51 75 65 6e 63 65 3a 20 7b 20 20 20 20 20 20 20 20  uence: {        
19c52 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65     /* out2-prere
19c53 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69  lease */.  int i
19c54 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73   = pOp->p1;.  as
19c55 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
19c56 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
19c57 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72  assert( p->apCsr
19c58 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74  [i]!=0 );.  pOut
19c59 2d 3e 75 2e 69 20 3d 20 70 2d 3e 61 70 43 73 72  ->u.i = p->apCsr
19c5a 5b 69 5d 2d 3e 73 65 71 43 6f 75 6e 74 2b 2b 3b  [i]->seqCount++;
19c5b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61  .  MemSetTypeFla
19c5c 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29  g(pOut, MEM_Int)
19c5d 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f  ;.  break;.}.../
19c5e 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 77 52 6f 77  * Opcode: NewRow
19c5f 69 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  id P1 P2 P3 * *.
19c60 2a 2a 0a 2a 2a 20 47 65 74 20 61 20 6e 65 77 20  **.** Get a new 
19c61 69 6e 74 65 67 65 72 20 72 65 63 6f 72 64 20 6e  integer record n
19c62 75 6d 62 65 72 20 28 61 2e 6b 2e 61 20 22 72 6f  umber (a.k.a "ro
19c63 77 69 64 22 29 20 75 73 65 64 20 61 73 20 74 68  wid") used as th
19c64 65 20 6b 65 79 20 74 6f 20 61 20 74 61 62 6c 65  e key to a table
19c65 2e 0a 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 20  ..** The record 
19c66 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74 20 70 72  number is not pr
19c67 65 76 69 6f 75 73 6c 79 20 75 73 65 64 20 61 73  eviously used as
19c68 20 61 20 6b 65 79 20 69 6e 20 74 68 65 20 64 61   a key in the da
19c69 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20  tabase.** table 
19c6a 74 68 61 74 20 63 75 72 73 6f 72 20 50 31 20 70  that cursor P1 p
19c6b 6f 69 6e 74 73 20 74 6f 2e 20 20 54 68 65 20 6e  oints to.  The n
19c6c 65 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  ew record number
19c6d 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 77   is written.** w
19c6e 72 69 74 74 65 6e 20 74 6f 20 72 65 67 69 73 74  ritten to regist
19c6f 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  er P2..**.** If 
19c70 50 33 3e 30 20 74 68 65 6e 20 50 33 20 69 73 20  P3>0 then P3 is 
19c71 61 20 72 65 67 69 73 74 65 72 20 74 68 61 74 20  a register that 
19c72 68 6f 6c 64 73 20 74 68 65 20 6c 61 72 67 65 73  holds the larges
19c73 74 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20  t previously.** 
19c74 67 65 6e 65 72 61 74 65 64 20 72 65 63 6f 72 64  generated record
19c75 20 6e 75 6d 62 65 72 2e 20 20 4e 6f 20 6e 65 77   number.  No new
19c76 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 73 20   record numbers 
19c77 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62  are allowed to b
19c78 65 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 74  e less.** than t
19c79 68 69 73 20 76 61 6c 75 65 2e 20 20 57 68 65 6e  his value.  When
19c7a 20 74 68 69 73 20 76 61 6c 75 65 20 72 65 61 63   this value reac
19c7b 68 65 73 20 69 74 73 20 6d 61 78 69 6d 75 6d 2c  hes its maximum,
19c7c 20 61 20 53 51 4c 49 54 45 5f 46 55 4c 4c 0a 2a   a SQLITE_FULL.*
19c7d 2a 20 65 72 72 6f 72 20 69 73 20 67 65 6e 65 72  * error is gener
19c7e 61 74 65 64 2e 20 20 54 68 65 20 50 33 20 72 65  ated.  The P3 re
19c7f 67 69 73 74 65 72 20 69 73 20 75 70 64 61 74 65  gister is update
19c80 64 20 77 69 74 68 20 74 68 65 20 67 65 6e 65 72  d with the gener
19c81 61 74 65 64 0a 2a 2a 20 72 65 63 6f 72 64 20 6e  ated.** record n
19c82 75 6d 62 65 72 2e 20 20 54 68 69 73 20 50 33 20  umber.  This P3 
19c83 6d 65 63 68 61 6e 69 73 6d 20 69 73 20 75 73 65  mechanism is use
19c84 64 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d  d to help implem
19c85 65 6e 74 20 74 68 65 0a 2a 2a 20 41 55 54 4f 49  ent the.** AUTOI
19c86 4e 43 52 45 4d 45 4e 54 20 66 65 61 74 75 72 65  NCREMENT feature
19c87 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 77  ..*/.case OP_New
19c88 52 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20  Rowid: {        
19c89 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65     /* out2-prere
19c8a 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69  lease */.  int i
19c8b 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 36   = pOp->p1;.  i6
19c8c 34 20 76 20 3d 20 30 3b 0a 20 20 56 64 62 65 43  4 v = 0;.  VdbeC
19c8d 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73  ursor *pC;.  ass
19c8e 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70  ert( i>=0 && i<p
19c8f 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61  ->nCursor );.  a
19c90 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b  ssert( p->apCsr[
19c91 69 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28  i]!=0 );.  if( (
19c92 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d  pC = p->apCsr[i]
19c93 29 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20 29 7b  )->pCursor==0 ){
19c94 0a 20 20 20 20 2f 2a 20 54 68 65 20 7a 65 72 6f  .    /* The zero
19c95 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
19c96 61 62 6f 76 65 20 69 73 20 61 6c 6c 20 74 68 61  above is all tha
19c97 74 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20  t is needed */. 
19c98 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54   }else{.    /* T
19c99 68 65 20 6e 65 78 74 20 72 6f 77 69 64 20 6f 72  he next rowid or
19c9a 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 28   record number (
19c9b 64 69 66 66 65 72 65 6e 74 20 74 65 72 6d 73 20  different terms 
19c9c 66 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20  for the same.   
19c9d 20 2a 2a 20 74 68 69 6e 67 29 20 69 73 20 6f 62   ** thing) is ob
19c9e 74 61 69 6e 65 64 20 69 6e 20 61 20 74 77 6f 2d  tained in a two-
19c9f 73 74 65 70 20 61 6c 67 6f 72 69 74 68 6d 2e 0a  step algorithm..
19ca0 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 69      **.    ** Fi
19ca1 72 73 74 20 77 65 20 61 74 74 65 6d 70 74 20 74  rst we attempt t
19ca2 6f 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65  o find the large
19ca3 73 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69  st existing rowi
19ca4 64 20 61 6e 64 20 61 64 64 20 6f 6e 65 0a 20 20  d and add one.  
19ca5 20 20 2a 2a 20 74 6f 20 74 68 61 74 2e 20 20 42    ** to that.  B
19ca6 75 74 20 69 66 20 74 68 65 20 6c 61 72 67 65 73  ut if the larges
19ca7 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64  t existing rowid
19ca8 20 69 73 20 61 6c 72 65 61 64 79 20 74 68 65 20   is already the 
19ca9 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 70  maximum.    ** p
19caa 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 2c  ositive integer,
19cab 20 77 65 20 68 61 76 65 20 74 6f 20 66 61 6c 6c   we have to fall
19cac 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20   through to the 
19cad 73 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20 70 72  second.    ** pr
19cae 6f 62 61 62 69 6c 69 73 74 69 63 20 61 6c 67 6f  obabilistic algo
19caf 72 69 74 68 6d 0a 20 20 20 20 2a 2a 0a 20 20 20  rithm.    **.   
19cb0 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61   ** The second a
19cb1 6c 67 6f 72 69 74 68 6d 20 69 73 20 74 6f 20 73  lgorithm is to s
19cb2 65 6c 65 63 74 20 61 20 72 6f 77 69 64 20 61 74  elect a rowid at
19cb3 20 72 61 6e 64 6f 6d 20 61 6e 64 20 73 65 65 20   random and see 
19cb4 69 66 0a 20 20 20 20 2a 2a 20 69 74 20 61 6c 72  if.    ** it alr
19cb5 65 61 64 79 20 65 78 69 73 74 73 20 69 6e 20 74  eady exists in t
19cb6 68 65 20 74 61 62 6c 65 2e 20 20 49 66 20 69 74  he table.  If it
19cb7 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c   does not exist,
19cb8 20 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20   we have.    ** 
19cb9 73 75 63 63 65 65 64 65 64 2e 20 20 49 66 20 74  succeeded.  If t
19cba 68 65 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20  he random rowid 
19cbb 64 6f 65 73 20 65 78 69 73 74 2c 20 77 65 20 73  does exist, we s
19cbc 65 6c 65 63 74 20 61 20 6e 65 77 20 6f 6e 65 0a  elect a new one.
19cbd 20 20 20 20 2a 2a 20 61 6e 64 20 74 72 79 20 61      ** and try a
19cbe 67 61 69 6e 2c 20 75 70 20 74 6f 20 31 30 30 30  gain, up to 1000
19cbf 20 74 69 6d 65 73 2e 0a 20 20 20 20 2a 2a 0a 20   times..    **. 
19cc0 20 20 20 2a 2a 20 46 6f 72 20 61 20 74 61 62 6c     ** For a tabl
19cc1 65 20 77 69 74 68 20 6c 65 73 73 20 74 68 61 6e  e with less than
19cc2 20 32 20 62 69 6c 6c 69 6f 6e 20 65 6e 74 72 69   2 billion entri
19cc3 65 73 2c 20 74 68 65 20 70 72 6f 62 61 62 69 6c  es, the probabil
19cc4 69 74 79 0a 20 20 20 20 2a 2a 20 6f 66 20 6e 6f  ity.    ** of no
19cc5 74 20 66 69 6e 64 69 6e 67 20 61 20 75 6e 75 73  t finding a unus
19cc6 65 64 20 72 6f 77 69 64 20 69 73 20 61 62 6f 75  ed rowid is abou
19cc7 74 20 31 2e 30 65 2d 33 30 30 2e 20 20 54 68 69  t 1.0e-300.  Thi
19cc8 73 20 69 73 20 61 20 0a 20 20 20 20 2a 2a 20 6e  s is a .    ** n
19cc9 6f 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62 69 6c  on-zero probabil
19cca 69 74 79 2c 20 62 75 74 20 69 74 20 69 73 20 73  ity, but it is s
19ccb 74 69 6c 6c 20 76 61 6e 69 73 68 69 6e 67 6c 79  till vanishingly
19ccc 20 73 6d 61 6c 6c 20 61 6e 64 20 73 68 6f 75 6c   small and shoul
19ccd 64 0a 20 20 20 20 2a 2a 20 6e 65 76 65 72 20 63  d.    ** never c
19cce 61 75 73 65 20 61 20 70 72 6f 62 6c 65 6d 2e 20  ause a problem. 
19ccf 20 59 6f 75 20 61 72 65 20 6d 75 63 68 2c 20 6d   You are much, m
19cd0 75 63 68 20 6d 6f 72 65 20 6c 69 6b 65 6c 79 20  uch more likely 
19cd1 74 6f 20 68 61 76 65 20 61 0a 20 20 20 20 2a 2a  to have a.    **
19cd2 20 68 61 72 64 77 61 72 65 20 66 61 69 6c 75 72   hardware failur
19cd3 65 20 74 68 61 6e 20 66 6f 72 20 74 68 69 73 20  e than for this 
19cd4 61 6c 67 6f 72 69 74 68 6d 20 74 6f 20 66 61 69  algorithm to fai
19cd5 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  l..    **.    **
19cd6 20 54 68 65 20 61 6e 61 6c 79 73 69 73 20 69 6e   The analysis in
19cd7 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 61   the previous pa
19cd8 72 61 67 72 61 70 68 20 61 73 73 75 6d 65 73 20  ragraph assumes 
19cd9 74 68 61 74 20 79 6f 75 20 68 61 76 65 20 61 20  that you have a 
19cda 67 6f 6f 64 0a 20 20 20 20 2a 2a 20 73 6f 75 72  good.    ** sour
19cdb 63 65 20 6f 66 20 72 61 6e 64 6f 6d 20 6e 75 6d  ce of random num
19cdc 62 65 72 73 2e 20 20 49 73 20 61 20 6c 69 62 72  bers.  Is a libr
19cdd 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 6c 69 6b  ary function lik
19cde 65 20 6c 72 61 6e 64 34 38 28 29 0a 20 20 20 20  e lrand48().    
19cdf 2a 2a 20 67 6f 6f 64 20 65 6e 6f 75 67 68 3f 20  ** good enough? 
19ce0 20 4d 61 79 62 65 2e 20 4d 61 79 62 65 20 6e 6f   Maybe. Maybe no
19ce1 74 2e 20 49 74 27 73 20 68 61 72 64 20 74 6f 20  t. It's hard to 
19ce2 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 74 68 65  know whether the
19ce3 72 65 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74 20  re.    ** might 
19ce4 62 65 20 73 75 62 74 6c 65 20 62 75 67 73 20 69  be subtle bugs i
19ce5 73 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e 74  s some implement
19ce6 61 74 69 6f 6e 73 20 6f 66 20 6c 72 61 6e 64 34  ations of lrand4
19ce7 38 28 29 20 74 68 61 74 0a 20 20 20 20 2a 2a 20  8() that.    ** 
19ce8 63 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62  could cause prob
19ce9 6c 65 6d 73 2e 20 54 6f 20 61 76 6f 69 64 20 75  lems. To avoid u
19cea 6e 63 65 72 74 61 69 6e 74 79 2c 20 53 51 4c 69  ncertainty, SQLi
19ceb 74 65 20 75 73 65 73 20 69 74 73 20 6f 77 6e 20  te uses its own 
19cec 0a 20 20 20 20 2a 2a 20 72 61 6e 64 6f 6d 20 6e  .    ** random n
19ced 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20  umber generator 
19cee 62 61 73 65 64 20 6f 6e 20 74 68 65 20 52 43 34  based on the RC4
19cef 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 20 20 20 20   algorithm..    
19cf0 2a 2a 0a 20 20 20 20 2a 2a 20 54 6f 20 70 72 6f  **.    ** To pro
19cf1 6d 6f 74 65 20 6c 6f 63 61 6c 69 74 79 20 6f 66  mote locality of
19cf2 20 72 65 66 65 72 65 6e 63 65 20 66 6f 72 20 72   reference for r
19cf3 65 70 65 74 69 74 69 76 65 20 69 6e 73 65 72 74  epetitive insert
19cf4 73 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 69  s, the.    ** fi
19cf5 72 73 74 20 66 65 77 20 61 74 74 65 6d 70 74 73  rst few attempts
19cf6 20 61 74 20 63 68 6f 6f 73 69 6e 67 20 61 20 72   at choosing a r
19cf7 61 6e 64 6f 6d 20 72 6f 77 69 64 20 70 69 63 6b  andom rowid pick
19cf8 20 76 61 6c 75 65 73 20 6a 75 73 74 20 61 20 6c   values just a l
19cf9 69 74 74 6c 65 0a 20 20 20 20 2a 2a 20 6c 61 72  ittle.    ** lar
19cfa 67 65 72 20 74 68 61 6e 20 74 68 65 20 70 72 65  ger than the pre
19cfb 76 69 6f 75 73 20 72 6f 77 69 64 2e 20 20 54 68  vious rowid.  Th
19cfc 69 73 20 68 61 73 20 62 65 65 6e 20 73 68 6f 77  is has been show
19cfd 6e 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 6c 79  n experimentally
19cfe 0a 20 20 20 20 2a 2a 20 74 6f 20 64 6f 75 62 6c  .    ** to doubl
19cff 65 20 74 68 65 20 73 70 65 65 64 20 6f 66 20 74  e the speed of t
19d00 68 65 20 43 4f 50 59 20 6f 70 65 72 61 74 69 6f  he COPY operatio
19d01 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e  n..    */.    in
19d02 74 20 72 65 73 2c 20 72 78 3d 53 51 4c 49 54 45  t res, rx=SQLITE
19d03 5f 4f 4b 2c 20 63 6e 74 3b 0a 20 20 20 20 69 36  _OK, cnt;.    i6
19d04 34 20 78 3b 0a 20 20 20 20 63 6e 74 20 3d 20 30  4 x;.    cnt = 0
19d05 3b 0a 20 20 20 20 69 66 28 20 28 73 71 6c 69 74  ;.    if( (sqlit
19d06 65 33 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d  e3BtreeFlags(pC-
19d07 3e 70 43 75 72 73 6f 72 29 26 28 42 54 52 45 45  >pCursor)&(BTREE
19d08 5f 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f 5a 45  _INTKEY|BTREE_ZE
19d09 52 4f 44 41 54 41 29 29 20 21 3d 0a 20 20 20 20  RODATA)) !=.    
19d0a 20 20 20 20 20 20 42 54 52 45 45 5f 49 4e 54 4b        BTREE_INTK
19d0b 45 59 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  EY ){.      rc =
19d0c 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
19d0d 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f  BKPT;.      goto
19d0e 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
19d0f 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  ror;.    }.    a
19d10 73 73 65 72 74 28 20 28 73 71 6c 69 74 65 33 42  ssert( (sqlite3B
19d11 74 72 65 65 46 6c 61 67 73 28 70 43 2d 3e 70 43  treeFlags(pC->pC
19d12 75 72 73 6f 72 29 20 26 20 42 54 52 45 45 5f 49  ursor) & BTREE_I
19d13 4e 54 4b 45 59 29 21 3d 30 20 29 3b 0a 20 20 20  NTKEY)!=0 );.   
19d14 20 61 73 73 65 72 74 28 20 28 73 71 6c 69 74 65   assert( (sqlite
19d15 33 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d 3e  3BtreeFlags(pC->
19d16 70 43 75 72 73 6f 72 29 20 26 20 42 54 52 45 45  pCursor) & BTREE
19d17 5f 5a 45 52 4f 44 41 54 41 29 3d 3d 30 20 29 3b  _ZERODATA)==0 );
19d18 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
19d19 33 32 42 49 54 5f 52 4f 57 49 44 0a 23 20 20 20  32BIT_ROWID.#   
19d1a 64 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44  define MAX_ROWID
19d1b 20 30 78 37 66 66 66 66 66 66 66 0a 23 65 6c 73   0x7fffffff.#els
19d1c 65 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f  e.    /* Some co
19d1d 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 6e  mpilers complain
19d1e 20 61 62 6f 75 74 20 63 6f 6e 73 74 61 6e 74 73   about constants
19d1f 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 30 78 37   of the form 0x7
19d20 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 2e  fffffffffffffff.
19d21 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 73 20 63  .    ** Others c
19d22 6f 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 30 78  omplain about 0x
19d23 37 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66  7fffffffffffffff
19d24 66 4c 4c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  fLL.  The follow
19d25 69 6e 67 20 6d 61 63 72 6f 20 73 65 65 6d 73 0a  ing macro seems.
19d26 20 20 20 20 2a 2a 20 74 6f 20 70 72 6f 76 69 64      ** to provid
19d27 65 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 77  e the constant w
19d28 68 69 6c 65 20 6d 61 6b 69 6e 67 20 61 6c 6c 20  hile making all 
19d29 63 6f 6d 70 69 6c 65 72 73 20 68 61 70 70 79 2e  compilers happy.
19d2a 0a 20 20 20 20 2a 2f 0a 23 20 20 20 64 65 66 69  .    */.#   defi
19d2b 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20 20 28 20  ne MAX_ROWID  ( 
19d2c 28 28 28 75 36 34 29 30 78 37 66 66 66 66 66 66  (((u64)0x7ffffff
19d2d 66 29 3c 3c 33 32 29 20 7c 20 28 75 36 34 29 30  f)<<32) | (u64)0
19d2e 78 66 66 66 66 66 66 66 66 20 29 0a 23 65 6e 64  xffffffff ).#end
19d2f 69 66 0a 0a 20 20 20 20 69 66 28 20 21 70 43 2d  if..    if( !pC-
19d30 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 20  >useRandomRowid 
19d31 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 2d  ){.      if( pC-
19d32 3e 6e 65 78 74 52 6f 77 69 64 56 61 6c 69 64 20  >nextRowidValid 
19d33 29 7b 0a 20 20 20 20 20 20 20 20 76 20 3d 20 70  ){.        v = p
19d34 43 2d 3e 6e 65 78 74 52 6f 77 69 64 3b 0a 20 20  C->nextRowid;.  
19d35 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
19d36 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
19d37 74 72 65 65 4c 61 73 74 28 70 43 2d 3e 70 43 75  treeLast(pC->pCu
19d38 72 73 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20  rsor, &res);.   
19d39 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
19d3a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
19d3b 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64      goto abort_d
19d3c 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
19d3d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
19d3e 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20  f( res ){.      
19d3f 20 20 20 20 76 20 3d 20 31 3b 0a 20 20 20 20 20      v = 1;.     
19d40 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
19d41 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
19d42 4b 65 79 53 69 7a 65 28 70 43 2d 3e 70 43 75 72  KeySize(pC->pCur
19d43 73 6f 72 2c 20 26 76 29 3b 0a 20 20 20 20 20 20  sor, &v);.      
19d44 20 20 20 20 76 20 3d 20 6b 65 79 54 6f 49 6e 74      v = keyToInt
19d45 28 76 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  (v);.          i
19d46 66 28 20 76 3d 3d 4d 41 58 5f 52 4f 57 49 44 20  f( v==MAX_ROWID 
19d47 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  ){.            p
19d48 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69  C->useRandomRowi
19d49 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  d = 1;.         
19d4a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
19d4b 20 20 20 20 76 2b 2b 3b 0a 20 20 20 20 20 20 20      v++;.       
19d4c 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
19d4d 20 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20       }..#ifndef 
19d4e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
19d4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 20 20  INCREMENT.      
19d50 69 66 28 20 70 4f 70 2d 3e 70 33 20 29 7b 0a 20  if( pOp->p3 ){. 
19d51 20 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d         Mem *pMem
19d52 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
19d53 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70  ( pOp->p3>0 && p
19d54 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p3<=p->nMem 
19d55 29 3b 20 2f 2a 20 50 33 20 69 73 20 61 20 76 61  ); /* P3 is a va
19d56 6c 69 64 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  lid memory cell 
19d57 2a 2f 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 20  */.        pMem 
19d58 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  = &p->aMem[pOp->
19d59 70 33 5d 3b 0a 09 52 45 47 49 53 54 45 52 5f 54  p3];..REGISTER_T
19d5a 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 4d  RACE(pOp->p3, pM
19d5b 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  em);.        sql
19d5c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67  ite3VdbeMemInteg
19d5d 65 72 69 66 79 28 70 4d 65 6d 29 3b 0a 20 20 20  erify(pMem);.   
19d5e 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 4d       assert( (pM
19d5f 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
19d60 49 6e 74 29 21 3d 30 20 29 3b 20 20 2f 2a 20 6d  Int)!=0 );  /* m
19d61 65 6d 28 50 33 29 20 68 6f 6c 64 73 20 61 6e 20  em(P3) holds an 
19d62 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20  integer */.     
19d63 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 75 2e 69     if( pMem->u.i
19d64 3d 3d 4d 41 58 5f 52 4f 57 49 44 20 7c 7c 20 70  ==MAX_ROWID || p
19d65 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69  C->useRandomRowi
19d66 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  d ){.          r
19d67 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b  c = SQLITE_FULL;
19d68 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
19d69 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
19d6a 6f 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  or;.        }.  
19d6b 20 20 20 20 20 20 69 66 28 20 76 3c 70 4d 65 6d        if( v<pMem
19d6c 2d 3e 75 2e 69 2b 31 20 29 7b 0a 20 20 20 20 20  ->u.i+1 ){.     
19d6d 20 20 20 20 20 76 20 3d 20 70 4d 65 6d 2d 3e 75       v = pMem->u
19d6e 2e 69 20 2b 20 31 3b 0a 20 20 20 20 20 20 20 20  .i + 1;.        
19d6f 7d 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e  }.        pMem->
19d70 75 2e 69 20 3d 20 76 3b 0a 20 20 20 20 20 20 7d  u.i = v;.      }
19d71 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 69  .#endif..      i
19d72 66 28 20 76 3c 4d 41 58 5f 52 4f 57 49 44 20 29  f( v<MAX_ROWID )
19d73 7b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 6e 65  {.        pC->ne
19d74 78 74 52 6f 77 69 64 56 61 6c 69 64 20 3d 20 31  xtRowidValid = 1
19d75 3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 6e 65  ;.        pC->ne
19d76 78 74 52 6f 77 69 64 20 3d 20 76 2b 31 3b 0a 20  xtRowid = v+1;. 
19d77 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
19d78 20 20 20 20 70 43 2d 3e 6e 65 78 74 52 6f 77 69      pC->nextRowi
19d79 64 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20  dValid = 0;.    
19d7a 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
19d7b 28 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52  ( pC->useRandomR
19d7c 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 61 73  owid ){.      as
19d7d 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3d 3d 30  sert( pOp->p3==0
19d7e 20 29 3b 20 20 2f 2a 20 53 51 4c 49 54 45 5f 46   );  /* SQLITE_F
19d7f 55 4c 4c 20 6d 75 73 74 20 68 61 76 65 20 6f 63  ULL must have oc
19d80 63 75 72 72 65 64 20 70 72 69 6f 72 20 74 6f 20  curred prior to 
19d81 74 68 69 73 20 2a 2f 0a 20 20 20 20 20 20 76 20  this */.      v 
19d82 3d 20 64 62 2d 3e 70 72 69 6f 72 4e 65 77 52 6f  = db->priorNewRo
19d83 77 69 64 3b 0a 20 20 20 20 20 20 63 6e 74 20 3d  wid;.      cnt =
19d84 20 30 3b 0a 20 20 20 20 20 20 64 6f 7b 0a 20 20   0;.      do{.  
19d85 20 20 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30        if( cnt==0
19d86 20 26 26 20 28 76 26 30 78 66 66 66 66 66 66 29   && (v&0xffffff)
19d87 3d 3d 76 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==v ){.         
19d88 20 76 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65   v++;.        }e
19d89 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73  lse{.          s
19d8a 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73  qlite3_randomnes
19d8b 73 28 73 69 7a 65 6f 66 28 76 29 2c 20 26 76 29  s(sizeof(v), &v)
19d8c 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
19d8d 63 6e 74 3c 35 20 29 20 76 20 26 3d 20 30 78 66  cnt<5 ) v &= 0xf
19d8e 66 66 66 66 66 3b 0a 20 20 20 20 20 20 20 20 7d  fffff;.        }
19d8f 0a 20 20 20 20 20 20 20 20 69 66 28 20 76 3d 3d  .        if( v==
19d90 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
19d91 20 20 20 20 20 20 78 20 3d 20 69 6e 74 54 6f 4b        x = intToK
19d92 65 79 28 76 29 3b 0a 20 20 20 20 20 20 20 20 72  ey(v);.        r
19d93 78 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  x = sqlite3Btree
19d94 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70  MovetoUnpacked(p
19d95 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28  C->pCursor, 0, (
19d96 75 36 34 29 78 2c 20 30 2c 20 26 72 65 73 29 3b  u64)x, 0, &res);
19d97 0a 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a  .        cnt++;.
19d98 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 63 6e        }while( cn
19d99 74 3c 31 30 30 20 26 26 20 72 78 3d 3d 53 51 4c  t<100 && rx==SQL
19d9a 49 54 45 5f 4f 4b 20 26 26 20 72 65 73 3d 3d 30  ITE_OK && res==0
19d9b 20 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 70 72   );.      db->pr
19d9c 69 6f 72 4e 65 77 52 6f 77 69 64 20 3d 20 76 3b  iorNewRowid = v;
19d9d 0a 20 20 20 20 20 20 69 66 28 20 72 78 3d 3d 53  .      if( rx==S
19d9e 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 65 73 3d  QLITE_OK && res=
19d9f 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  =0 ){.        rc
19da0 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a   = SQLITE_FULL;.
19da1 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f          goto abo
19da2 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
19da3 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
19da4 20 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61     pC->rowidIsVa
19da5 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d  lid = 0;.    pC-
19da6 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20  >deferredMoveto 
19da7 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63  = 0;.    pC->cac
19da8 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
19da9 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 4d 65  _STALE;.  }.  Me
19daa 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75  mSetTypeFlag(pOu
19dab 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 70  t, MEM_Int);.  p
19dac 4f 75 74 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20  Out->u.i = v;.  
19dad 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
19dae 6f 64 65 3a 20 49 6e 73 65 72 74 20 50 31 20 50  ode: Insert P1 P
19daf 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a  2 P3 P4 P5.**.**
19db0 20 57 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20   Write an entry 
19db1 69 6e 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6f  into the table o
19db2 66 20 63 75 72 73 6f 72 20 50 31 2e 20 20 41 20  f cursor P1.  A 
19db3 6e 65 77 20 65 6e 74 72 79 20 69 73 0a 2a 2a 20  new entry is.** 
19db4 63 72 65 61 74 65 64 20 69 66 20 69 74 20 64 6f  created if it do
19db5 65 73 6e 27 74 20 61 6c 72 65 61 64 79 20 65 78  esn't already ex
19db6 69 73 74 20 6f 72 20 74 68 65 20 64 61 74 61 20  ist or the data 
19db7 66 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 0a  for an existing.
19db8 2a 2a 20 65 6e 74 72 79 20 69 73 20 6f 76 65 72  ** entry is over
19db9 77 72 69 74 74 65 6e 2e 20 20 54 68 65 20 64 61  written.  The da
19dba 74 61 20 69 73 20 74 68 65 20 76 61 6c 75 65 20  ta is the value 
19dbb 73 74 6f 72 65 64 20 72 65 67 69 73 74 65 72 0a  stored register.
19dbc 2a 2a 20 6e 75 6d 62 65 72 20 50 32 2e 20 54 68  ** number P2. Th
19dbd 65 20 6b 65 79 20 69 73 20 73 74 6f 72 65 64 20  e key is stored 
19dbe 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 20  in register P3. 
19dbf 54 68 65 20 6b 65 79 20 6d 75 73 74 0a 2a 2a 20  The key must.** 
19dc0 62 65 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a  be an integer..*
19dc1 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 4c  *.** If the OPFL
19dc2 41 47 5f 4e 43 48 41 4e 47 45 20 66 6c 61 67 20  AG_NCHANGE flag 
19dc3 6f 66 20 50 35 20 69 73 20 73 65 74 2c 20 74 68  of P5 is set, th
19dc4 65 6e 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67  en the row chang
19dc5 65 20 63 6f 75 6e 74 20 69 73 0a 2a 2a 20 69 6e  e count is.** in
19dc6 63 72 65 6d 65 6e 74 65 64 20 28 6f 74 68 65 72  cremented (other
19dc7 77 69 73 65 20 6e 6f 74 29 2e 20 20 49 66 20 74  wise not).  If t
19dc8 68 65 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f  he OPFLAG_LASTRO
19dc9 57 49 44 20 66 6c 61 67 20 6f 66 20 50 35 20 69  WID flag of P5 i
19dca 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20 72  s set,.** then r
19dcb 6f 77 69 64 20 69 73 20 73 74 6f 72 65 64 20 66  owid is stored f
19dcc 6f 72 20 73 75 62 73 65 71 75 65 6e 74 20 72 65  or subsequent re
19dcd 74 75 72 6e 20 62 79 20 74 68 65 0a 2a 2a 20 73  turn by the.** s
19dce 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
19dcf 72 74 5f 72 6f 77 69 64 28 29 20 66 75 6e 63 74  rt_rowid() funct
19dd0 69 6f 6e 20 28 6f 74 68 65 72 77 69 73 65 20 69  ion (otherwise i
19dd1 74 20 69 73 20 75 6e 6d 6f 64 69 66 69 65 64 29  t is unmodified)
19dd2 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65  ..**.** Paramete
19dd3 72 20 50 34 20 6d 61 79 20 70 6f 69 6e 74 20 74  r P4 may point t
19dd4 6f 20 61 20 73 74 72 69 6e 67 20 63 6f 6e 74 61  o a string conta
19dd5 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2d  ining the table-
19dd6 6e 61 6d 65 2c 20 6f 72 0a 2a 2a 20 6d 61 79 20  name, or.** may 
19dd7 62 65 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69  be NULL. If it i
19dd8 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  s not NULL, then
19dd9 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b   the update-hook
19dda 20 0a 2a 2a 20 28 73 71 6c 69 74 65 33 2e 78 55   .** (sqlite3.xU
19ddb 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 29 20 69  pdateCallback) i
19ddc 73 20 69 6e 76 6f 6b 65 64 20 66 6f 6c 6c 6f 77  s invoked follow
19ddd 69 6e 67 20 61 20 73 75 63 63 65 73 73 66 75 6c  ing a successful
19dde 20 69 6e 73 65 72 74 2e 0a 2a 2a 0a 2a 2a 20 28   insert..**.** (
19ddf 57 41 52 4e 49 4e 47 2f 54 4f 44 4f 3a 20 49 66  WARNING/TODO: If
19de0 20 50 31 20 69 73 20 61 20 70 73 65 75 64 6f 2d   P1 is a pseudo-
19de1 63 75 72 73 6f 72 20 61 6e 64 20 50 32 20 69 73  cursor and P2 is
19de2 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20   dynamically.** 
19de3 61 6c 6c 6f 63 61 74 65 64 2c 20 74 68 65 6e 20  allocated, then 
19de4 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 50 32 20  ownership of P2 
19de5 69 73 20 74 72 61 6e 73 66 65 72 72 65 64 20 74  is transferred t
19de6 6f 20 74 68 65 20 70 73 65 75 64 6f 2d 63 75 72  o the pseudo-cur
19de7 73 6f 72 0a 2a 2a 20 61 6e 64 20 72 65 67 69 73  sor.** and regis
19de8 74 65 72 20 50 32 20 62 65 63 6f 6d 65 73 20 65  ter P2 becomes e
19de9 70 68 65 6d 65 72 61 6c 2e 20 20 49 66 20 74 68  phemeral.  If th
19dea 65 20 63 75 72 73 6f 72 20 69 73 20 63 68 61 6e  e cursor is chan
19deb 67 65 64 2c 20 74 68 65 0a 2a 2a 20 76 61 6c 75  ged, the.** valu
19dec 65 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 32  e of register P2
19ded 20 77 69 6c 6c 20 74 68 65 6e 20 63 68 61 6e 67   will then chang
19dee 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68  e.  Make sure th
19def 69 73 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63  is does not.** c
19df0 61 75 73 65 20 61 6e 79 20 70 72 6f 62 6c 65 6d  ause any problem
19df1 73 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  s.).**.** This i
19df2 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 20  nstruction only 
19df3 77 6f 72 6b 73 20 6f 6e 20 74 61 62 6c 65 73 2e  works on tables.
19df4 20 20 54 68 65 20 65 71 75 69 76 61 6c 65 6e 74    The equivalent
19df5 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20   instruction.** 
19df6 66 6f 72 20 69 6e 64 69 63 65 73 20 69 73 20 4f  for indices is O
19df7 50 5f 49 64 78 49 6e 73 65 72 74 2e 0a 2a 2f 0a  P_IdxInsert..*/.
19df8 63 61 73 65 20 4f 50 5f 49 6e 73 65 72 74 3a 20  case OP_Insert: 
19df9 7b 0a 20 20 4d 65 6d 20 2a 70 44 61 74 61 20 3d  {.  Mem *pData =
19dfa 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
19dfb 32 5d 3b 0a 20 20 4d 65 6d 20 2a 70 4b 65 79 20  2];.  Mem *pKey 
19dfc 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  = &p->aMem[pOp->
19dfd 70 33 5d 3b 0a 0a 20 20 69 36 34 20 69 4b 65 79  p3];..  i64 iKey
19dfe 3b 20 20 20 2f 2a 20 54 68 65 20 69 6e 74 65 67  ;   /* The integ
19dff 65 72 20 52 4f 57 49 44 20 6f 72 20 6b 65 79 20  er ROWID or key 
19e00 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74  for the record t
19e01 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f  o be inserted */
19e02 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e  .  int i = pOp->
19e03 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72  p1;.  VdbeCursor
19e04 20 2a 70 43 3b 0a 20 20 61 73 73 65 72 74 28 20   *pC;.  assert( 
19e05 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75  i>=0 && i<p->nCu
19e06 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70  rsor );.  pC = p
19e07 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73  ->apCsr[i];.  as
19e08 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20  sert( pC!=0 );. 
19e09 20 61 73 73 65 72 74 28 20 70 43 2d 3e 70 43 75   assert( pC->pCu
19e0a 72 73 6f 72 21 3d 30 20 7c 7c 20 70 43 2d 3e 70  rsor!=0 || pC->p
19e0b 73 65 75 64 6f 54 61 62 6c 65 20 29 3b 0a 20 20  seudoTable );.  
19e0c 61 73 73 65 72 74 28 20 70 4b 65 79 2d 3e 66 6c  assert( pKey->fl
19e0d 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b  ags & MEM_Int );
19e0e 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69  .  assert( pC->i
19e0f 73 54 61 62 6c 65 20 29 3b 0a 20 20 52 45 47 49  sTable );.  REGI
19e10 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
19e11 70 32 2c 20 70 44 61 74 61 29 3b 0a 20 20 52 45  p2, pData);.  RE
19e12 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70  GISTER_TRACE(pOp
19e13 2d 3e 70 33 2c 20 70 4b 65 79 29 3b 0a 0a 20 20  ->p3, pKey);..  
19e14 69 4b 65 79 20 3d 20 69 6e 74 54 6f 4b 65 79 28  iKey = intToKey(
19e15 70 4b 65 79 2d 3e 75 2e 69 29 3b 0a 20 20 69 66  pKey->u.i);.  if
19e16 28 20 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c  ( pOp->p5 & OPFL
19e17 41 47 5f 4e 43 48 41 4e 47 45 20 29 20 70 2d 3e  AG_NCHANGE ) p->
19e18 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 20 69 66 28  nChange++;.  if(
19e19 20 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41   pOp->p5 & OPFLA
19e1a 47 5f 4c 41 53 54 52 4f 57 49 44 20 29 20 64 62  G_LASTROWID ) db
19e1b 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 4b  ->lastRowid = pK
19e1c 65 79 2d 3e 75 2e 69 3b 0a 20 20 69 66 28 20 70  ey->u.i;.  if( p
19e1d 43 2d 3e 6e 65 78 74 52 6f 77 69 64 56 61 6c 69  C->nextRowidVali
19e1e 64 20 26 26 20 70 4b 65 79 2d 3e 75 2e 69 3e 3d  d && pKey->u.i>=
19e1f 70 43 2d 3e 6e 65 78 74 52 6f 77 69 64 20 29 7b  pC->nextRowid ){
19e20 0a 20 20 20 20 70 43 2d 3e 6e 65 78 74 52 6f 77  .    pC->nextRow
19e21 69 64 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d  idValid = 0;.  }
19e22 0a 20 20 69 66 28 20 70 44 61 74 61 2d 3e 66 6c  .  if( pData->fl
19e23 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29  ags & MEM_Null )
19e24 7b 0a 20 20 20 20 70 44 61 74 61 2d 3e 7a 20 3d  {.    pData->z =
19e25 20 30 3b 0a 20 20 20 20 70 44 61 74 61 2d 3e 6e   0;.    pData->n
19e26 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
19e27 20 20 20 61 73 73 65 72 74 28 20 70 44 61 74 61     assert( pData
19e28 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42  ->flags & (MEM_B
19e29 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 20 29 3b 0a  lob|MEM_Str) );.
19e2a 20 20 7d 0a 20 20 69 66 28 20 70 43 2d 3e 70 73    }.  if( pC->ps
19e2b 65 75 64 6f 54 61 62 6c 65 20 29 7b 0a 20 20 20  eudoTable ){.   
19e2c 20 69 66 28 20 21 70 43 2d 3e 65 70 68 65 6d 50   if( !pC->ephemP
19e2d 73 65 75 64 6f 54 61 62 6c 65 20 29 7b 0a 20 20  seudoTable ){.  
19e2e 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
19e2f 65 28 64 62 2c 20 70 43 2d 3e 70 44 61 74 61 29  e(db, pC->pData)
19e30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 2d 3e  ;.    }.    pC->
19e31 69 4b 65 79 20 3d 20 69 4b 65 79 3b 0a 20 20 20  iKey = iKey;.   
19e32 20 70 43 2d 3e 6e 44 61 74 61 20 3d 20 70 44 61   pC->nData = pDa
19e33 74 61 2d 3e 6e 3b 0a 20 20 20 20 69 66 28 20 70  ta->n;.    if( p
19e34 44 61 74 61 2d 3e 7a 3d 3d 70 44 61 74 61 2d 3e  Data->z==pData->
19e35 7a 4d 61 6c 6c 6f 63 20 7c 7c 20 70 43 2d 3e 65  zMalloc || pC->e
19e36 70 68 65 6d 50 73 65 75 64 6f 54 61 62 6c 65 20  phemPseudoTable 
19e37 29 7b 0a 20 20 20 20 20 20 70 43 2d 3e 70 44 61  ){.      pC->pDa
19e38 74 61 20 3d 20 70 44 61 74 61 2d 3e 7a 3b 0a 20  ta = pData->z;. 
19e39 20 20 20 20 20 69 66 28 20 21 70 43 2d 3e 65 70       if( !pC->ep
19e3a 68 65 6d 50 73 65 75 64 6f 54 61 62 6c 65 20 29  hemPseudoTable )
19e3b 7b 0a 20 20 20 20 20 20 20 20 70 44 61 74 61 2d  {.        pData-
19e3c 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 44  >flags &= ~MEM_D
19e3d 79 6e 3b 0a 20 20 20 20 20 20 20 20 70 44 61 74  yn;.        pDat
19e3e 61 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f  a->flags |= MEM_
19e3f 45 70 68 65 6d 3b 0a 20 20 20 20 20 20 20 20 70  Ephem;.        p
19e40 44 61 74 61 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20  Data->zMalloc = 
19e41 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  0;.      }.    }
19e42 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 43 2d 3e  else{.      pC->
19e43 70 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 4d  pData = sqlite3M
19e44 61 6c 6c 6f 63 28 20 70 43 2d 3e 6e 44 61 74 61  alloc( pC->nData
19e45 2b 32 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  +2 );.      if( 
19e46 21 70 43 2d 3e 70 44 61 74 61 20 29 20 67 6f 74  !pC->pData ) got
19e47 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 20 20  o no_mem;.      
19e48 6d 65 6d 63 70 79 28 70 43 2d 3e 70 44 61 74 61  memcpy(pC->pData
19e49 2c 20 70 44 61 74 61 2d 3e 7a 2c 20 70 43 2d 3e  , pData->z, pC->
19e4a 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 20 70 43  nData);.      pC
19e4b 2d 3e 70 44 61 74 61 5b 70 43 2d 3e 6e 44 61 74  ->pData[pC->nDat
19e4c 61 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 43  a] = 0;.      pC
19e4d 2d 3e 70 44 61 74 61 5b 70 43 2d 3e 6e 44 61 74  ->pData[pC->nDat
19e4e 61 2b 31 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  a+1] = 0;.    }.
19e4f 20 20 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20      pC->nullRow 
19e50 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 0;.  }else{.  
19e51 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 0a 20 20 20    int nZero;.   
19e52 20 69 66 28 20 70 44 61 74 61 2d 3e 66 6c 61 67   if( pData->flag
19e53 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a  s & MEM_Zero ){.
19e54 20 20 20 20 20 20 6e 5a 65 72 6f 20 3d 20 70 44        nZero = pD
19e55 61 74 61 2d 3e 75 2e 69 3b 0a 20 20 20 20 7d 65  ata->u.i;.    }e
19e56 6c 73 65 7b 0a 20 20 20 20 20 20 6e 5a 65 72 6f  lse{.      nZero
19e57 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
19e58 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
19e59 65 49 6e 73 65 72 74 28 70 43 2d 3e 70 43 75 72  eInsert(pC->pCur
19e5a 73 6f 72 2c 20 30 2c 20 69 4b 65 79 2c 0a 20 20  sor, 0, iKey,.  
19e5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19e5c 20 20 20 20 20 20 20 20 20 20 70 44 61 74 61 2d            pData-
19e5d 3e 7a 2c 20 70 44 61 74 61 2d 3e 6e 2c 20 6e 5a  >z, pData->n, nZ
19e5e 65 72 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ero,.           
19e5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19e60 20 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41   pOp->p5 & OPFLA
19e61 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 7d 0a 20  G_APPEND);.  }. 
19e62 20 0a 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56   .  pC->rowidIsV
19e63 61 6c 69 64 20 3d 20 30 3b 0a 20 20 70 43 2d 3e  alid = 0;.  pC->
19e64 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d  deferredMoveto =
19e65 20 30 3b 0a 20 20 70 43 2d 3e 63 61 63 68 65 53   0;.  pC->cacheS
19e66 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54  tatus = CACHE_ST
19e67 41 4c 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b  ALE;..  /* Invok
19e68 65 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f  e the update-hoo
19e69 6b 20 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a  k if required. *
19e6a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
19e6b 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70  TE_OK && db->xUp
19e6c 64 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20  dateCallback && 
19e6d 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20  pOp->p4.z ){.   
19e6e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
19e6f 20 3d 20 64 62 2d 3e 61 44 62 5b 70 43 2d 3e 69   = db->aDb[pC->i
19e70 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63  Db].zName;.    c
19e71 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 62 6c 20  onst char *zTbl 
19e72 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20  = pOp->p4.z;.   
19e73 20 69 6e 74 20 6f 70 20 3d 20 28 28 70 4f 70 2d   int op = ((pOp-
19e74 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 49 53 55  >p5 & OPFLAG_ISU
19e75 50 44 41 54 45 29 20 3f 20 53 51 4c 49 54 45 5f  PDATE) ? SQLITE_
19e76 55 50 44 41 54 45 20 3a 20 53 51 4c 49 54 45 5f  UPDATE : SQLITE_
19e77 49 4e 53 45 52 54 29 3b 0a 20 20 20 20 61 73 73  INSERT);.    ass
19e78 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c 65  ert( pC->isTable
19e79 20 29 3b 0a 20 20 20 20 64 62 2d 3e 78 55 70 64   );.    db->xUpd
19e7a 61 74 65 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e  ateCallback(db->
19e7b 70 55 70 64 61 74 65 41 72 67 2c 20 6f 70 2c 20  pUpdateArg, op, 
19e7c 7a 44 62 2c 20 7a 54 62 6c 2c 20 69 4b 65 79 29  zDb, zTbl, iKey)
19e7d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  ;.    assert( pC
19e7e 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a  ->iDb>=0 );.  }.
19e7f 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
19e80 70 63 6f 64 65 3a 20 44 65 6c 65 74 65 20 50 31  pcode: Delete P1
19e81 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a   P2 * P4 *.**.**
19e82 20 44 65 6c 65 74 65 20 74 68 65 20 72 65 63 6f   Delete the reco
19e83 72 64 20 61 74 20 77 68 69 63 68 20 74 68 65 20  rd at which the 
19e84 50 31 20 63 75 72 73 6f 72 20 69 73 20 63 75 72  P1 cursor is cur
19e85 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e  rently pointing.
19e86 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f  .**.** The curso
19e87 72 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 20 70  r will be left p
19e88 6f 69 6e 74 69 6e 67 20 61 74 20 65 69 74 68 65  ointing at eithe
19e89 72 20 74 68 65 20 6e 65 78 74 20 6f 72 20 74 68  r the next or th
19e8a 65 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20 72 65  e previous.** re
19e8b 63 6f 72 64 20 69 6e 20 74 68 65 20 74 61 62 6c  cord in the tabl
19e8c 65 2e 20 49 66 20 69 74 20 69 73 20 6c 65 66 74  e. If it is left
19e8d 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65   pointing at the
19e8e 20 6e 65 78 74 20 72 65 63 6f 72 64 2c 20 74 68   next record, th
19e8f 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 78 74 20 4e  en.** the next N
19e90 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ext instruction 
19e91 77 69 6c 6c 20 62 65 20 61 20 6e 6f 2d 6f 70 2e  will be a no-op.
19e92 20 20 48 65 6e 63 65 20 69 74 20 69 73 20 4f 4b    Hence it is OK
19e93 20 74 6f 20 64 65 6c 65 74 65 0a 2a 2a 20 61 20   to delete.** a 
19e94 72 65 63 6f 72 64 20 66 72 6f 6d 20 77 69 74 68  record from with
19e95 69 6e 20 61 6e 20 4e 65 78 74 20 6c 6f 6f 70 2e  in an Next loop.
19e96 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50  .**.** If the OP
19e97 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 66 6c 61  FLAG_NCHANGE fla
19e98 67 20 6f 66 20 50 32 20 69 73 20 73 65 74 2c 20  g of P2 is set, 
19e99 74 68 65 6e 20 74 68 65 20 72 6f 77 20 63 68 61  then the row cha
19e9a 6e 67 65 20 63 6f 75 6e 74 20 69 73 0a 2a 2a 20  nge count is.** 
19e9b 69 6e 63 72 65 6d 65 6e 74 65 64 20 28 6f 74 68  incremented (oth
19e9c 65 72 77 69 73 65 20 6e 6f 74 29 2e 0a 2a 2a 0a  erwise not)..**.
19e9d 2a 2a 20 50 31 20 6d 75 73 74 20 6e 6f 74 20 62  ** P1 must not b
19e9e 65 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 20  e pseudo-table. 
19e9f 20 49 74 20 68 61 73 20 74 6f 20 62 65 20 61 20   It has to be a 
19ea0 72 65 61 6c 20 74 61 62 6c 65 20 77 69 74 68 0a  real table with.
19ea1 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77 73  ** multiple rows
19ea2 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73  ..**.** If P4 is
19ea3 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   not NULL, then 
19ea4 69 74 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  it is the name o
19ea5 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74  f the table that
19ea6 20 50 31 20 69 73 0a 2a 2a 20 70 6f 69 6e 74 69   P1 is.** pointi
19ea7 6e 67 20 74 6f 2e 20 20 54 68 65 20 75 70 64 61  ng to.  The upda
19ea8 74 65 20 68 6f 6f 6b 20 77 69 6c 6c 20 62 65 20  te hook will be 
19ea9 69 6e 76 6f 6b 65 64 2c 20 69 66 20 69 74 20 65  invoked, if it e
19eaa 78 69 73 74 73 2e 0a 2a 2a 20 49 66 20 50 34 20  xists..** If P4 
19eab 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e  is not NULL then
19eac 20 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d   the P1 cursor m
19ead 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 70 6f  ust have been po
19eae 73 69 74 69 6f 6e 65 64 0a 2a 2a 20 75 73 69 6e  sitioned.** usin
19eaf 67 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 70 72  g OP_NotFound pr
19eb0 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20  ior to invoking 
19eb1 74 68 69 73 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a  this opcode..*/.
19eb2 63 61 73 65 20 4f 50 5f 44 65 6c 65 74 65 3a 20  case OP_Delete: 
19eb3 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d  {.  int i = pOp-
19eb4 3e 70 31 3b 0a 20 20 69 36 34 20 69 4b 65 79 3b  >p1;.  i64 iKey;
19eb5 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
19eb6 43 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e  C;..  assert( i>
19eb7 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73  =0 && i<p->nCurs
19eb8 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e  or );.  pC = p->
19eb9 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65  apCsr[i];.  asse
19eba 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 61  rt( pC!=0 );.  a
19ebb 73 73 65 72 74 28 20 70 43 2d 3e 70 43 75 72 73  ssert( pC->pCurs
19ebc 6f 72 21 3d 30 20 29 3b 20 20 2f 2a 20 4f 6e 6c  or!=0 );  /* Onl
19ebd 79 20 76 61 6c 69 64 20 66 6f 72 20 72 65 61 6c  y valid for real
19ebe 20 74 61 62 6c 65 73 2c 20 6e 6f 20 70 73 65 75   tables, no pseu
19ebf 64 6f 74 61 62 6c 65 73 20 2a 2f 0a 0a 20 20 2f  dotables */..  /
19ec0 2a 20 49 66 20 74 68 65 20 75 70 64 61 74 65 2d  * If the update-
19ec1 68 6f 6f 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76  hook will be inv
19ec2 6f 6b 65 64 2c 20 73 65 74 20 69 4b 65 79 20 74  oked, set iKey t
19ec3 6f 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74  o the rowid of t
19ec4 68 65 0a 20 20 2a 2a 20 72 6f 77 20 62 65 69 6e  he.  ** row bein
19ec5 67 20 64 65 6c 65 74 65 64 2e 0a 20 20 2a 2f 0a  g deleted..  */.
19ec6 20 20 69 66 28 20 64 62 2d 3e 78 55 70 64 61 74    if( db->xUpdat
19ec7 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70  eCallback && pOp
19ec8 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 61 73  ->p4.z ){.    as
19ec9 73 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c  sert( pC->isTabl
19eca 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  e );.    assert(
19ecb 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69   pC->rowidIsVali
19ecc 64 20 29 3b 20 20 2f 2a 20 6c 61 73 74 52 6f 77  d );  /* lastRow
19ecd 69 64 20 73 65 74 20 62 79 20 70 72 65 76 69 6f  id set by previo
19ece 75 73 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 2a  us OP_NotFound *
19ecf 2f 0a 20 20 20 20 69 4b 65 79 20 3d 20 70 43 2d  /.    iKey = pC-
19ed0 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 7d 0a  >lastRowid;.  }.
19ed1 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56  .  rc = sqlite3V
19ed2 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28  dbeCursorMoveto(
19ed3 70 43 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20  pC);.  if( rc ) 
19ed4 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
19ed5 6f 5f 65 72 72 6f 72 3b 0a 20 20 72 63 20 3d 20  o_error;.  rc = 
19ed6 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65  sqlite3BtreeDele
19ed7 74 65 28 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b  te(pC->pCursor);
19ed8 0a 20 20 70 43 2d 3e 6e 65 78 74 52 6f 77 69 64  .  pC->nextRowid
19ed9 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 70 43 2d  Valid = 0;.  pC-
19eda 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43  >cacheStatus = C
19edb 41 43 48 45 5f 53 54 41 4c 45 3b 0a 0a 20 20 2f  ACHE_STALE;..  /
19edc 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 75 70 64  * Invoke the upd
19edd 61 74 65 2d 68 6f 6f 6b 20 69 66 20 72 65 71 75  ate-hook if requ
19ede 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 72  ired. */.  if( r
19edf 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
19ee0 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62  db->xUpdateCallb
19ee1 61 63 6b 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a  ack && pOp->p4.z
19ee2 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68   ){.    const ch
19ee3 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44  ar *zDb = db->aD
19ee4 62 5b 70 43 2d 3e 69 44 62 5d 2e 7a 4e 61 6d 65  b[pC->iDb].zName
19ee5 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
19ee6 20 2a 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e 70 34   *zTbl = pOp->p4
19ee7 2e 7a 3b 0a 20 20 20 20 64 62 2d 3e 78 55 70 64  .z;.    db->xUpd
19ee8 61 74 65 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e  ateCallback(db->
19ee9 70 55 70 64 61 74 65 41 72 67 2c 20 53 51 4c 49  pUpdateArg, SQLI
19eea 54 45 5f 44 45 4c 45 54 45 2c 20 7a 44 62 2c 20  TE_DELETE, zDb, 
19eeb 7a 54 62 6c 2c 20 69 4b 65 79 29 3b 0a 20 20 20  zTbl, iKey);.   
19eec 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 44 62   assert( pC->iDb
19eed 3e 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28  >=0 );.  }.  if(
19eee 20 70 4f 70 2d 3e 70 32 20 26 20 4f 50 46 4c 41   pOp->p2 & OPFLA
19eef 47 5f 4e 43 48 41 4e 47 45 20 29 20 70 2d 3e 6e  G_NCHANGE ) p->n
19ef0 43 68 61 6e 67 65 2b 2b 3b 0a 20 20 62 72 65 61  Change++;.  brea
19ef1 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
19ef2 20 52 65 73 65 74 43 6f 75 6e 74 20 50 31 20 2a   ResetCount P1 *
19ef3 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70   *.**.** This op
19ef4 63 6f 64 65 20 72 65 73 65 74 73 20 74 68 65 20  code resets the 
19ef5 56 4d 73 20 69 6e 74 65 72 6e 61 6c 20 63 68 61  VMs internal cha
19ef6 6e 67 65 20 63 6f 75 6e 74 65 72 20 74 6f 20 30  nge counter to 0
19ef7 2e 20 49 66 20 50 31 20 69 73 20 74 72 75 65 2c  . If P1 is true,
19ef8 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 76 61 6c  .** then the val
19ef9 75 65 20 6f 66 20 74 68 65 20 63 68 61 6e 67 65  ue of the change
19efa 20 63 6f 75 6e 74 65 72 20 69 73 20 63 6f 70 69   counter is copi
19efb 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ed to the databa
19efc 73 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 63 68 61  se handle.** cha
19efd 6e 67 65 20 63 6f 75 6e 74 65 72 20 28 72 65 74  nge counter (ret
19efe 75 72 6e 65 64 20 62 79 20 73 75 62 73 65 71 75  urned by subsequ
19eff 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c  ent calls to sql
19f00 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 29 0a  ite3_changes()).
19f01 2a 2a 20 62 65 66 6f 72 65 20 69 74 20 69 73 20  ** before it is 
19f02 72 65 73 65 74 2e 20 54 68 69 73 20 69 73 20 75  reset. This is u
19f03 73 65 64 20 62 79 20 74 72 69 67 67 65 72 20 70  sed by trigger p
19f04 72 6f 67 72 61 6d 73 2e 0a 2a 2f 0a 63 61 73 65  rograms..*/.case
19f05 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 3a 20   OP_ResetCount: 
19f06 7b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 20  {.  if( pOp->p1 
19f07 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
19f08 62 65 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c  beSetChanges(db,
19f09 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b 0a 20 20   p->nChange);.  
19f0a 7d 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d  }.  p->nChange =
19f0b 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a   0;.  break;.}..
19f0c 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 44 61  /* Opcode: RowDa
19f0d 74 61 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  ta P1 P2 * * *.*
19f0e 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20  *.** Write into 
19f0f 72 65 67 69 73 74 65 72 20 50 32 20 74 68 65 20  register P2 the 
19f10 63 6f 6d 70 6c 65 74 65 20 72 6f 77 20 64 61 74  complete row dat
19f11 61 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e  a for cursor P1.
19f12 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20  .** There is no 
19f13 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e 20 6f  interpretation o
19f14 66 20 74 68 65 20 64 61 74 61 2e 20 20 0a 2a 2a  f the data.  .**
19f15 20 49 74 20 69 73 20 6a 75 73 74 20 63 6f 70 69   It is just copi
19f16 65 64 20 6f 6e 74 6f 20 74 68 65 20 50 32 20 72  ed onto the P2 r
19f17 65 67 69 73 74 65 72 20 65 78 61 63 74 6c 79 20  egister exactly 
19f18 61 73 20 0a 2a 2a 20 69 74 20 69 73 20 66 6f 75  as .** it is fou
19f19 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  nd in the databa
19f1a 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49  se file..**.** I
19f1b 66 20 74 68 65 20 50 31 20 63 75 72 73 6f 72 20  f the P1 cursor 
19f1c 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 69 6e 67  must be pointing
19f1d 20 74 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 20   to a valid row 
19f1e 28 6e 6f 74 20 61 20 4e 55 4c 4c 20 72 6f 77 29  (not a NULL row)
19f1f 0a 2a 2a 20 6f 66 20 61 20 72 65 61 6c 20 74 61  .** of a real ta
19f20 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64  ble, not a pseud
19f21 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a 20 4f  o-table..*/./* O
19f22 70 63 6f 64 65 3a 20 52 6f 77 4b 65 79 20 50 31  pcode: RowKey P1
19f23 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
19f24 57 72 69 74 65 20 69 6e 74 6f 20 72 65 67 69 73  Write into regis
19f25 74 65 72 20 50 32 20 74 68 65 20 63 6f 6d 70 6c  ter P2 the compl
19f26 65 74 65 20 72 6f 77 20 6b 65 79 20 66 6f 72 20  ete row key for 
19f27 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 54 68  cursor P1..** Th
19f28 65 72 65 20 69 73 20 6e 6f 20 69 6e 74 65 72 70  ere is no interp
19f29 72 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  retation of the 
19f2a 64 61 74 61 2e 20 20 0a 2a 2a 20 54 68 65 20 6b  data.  .** The k
19f2b 65 79 20 69 73 20 63 6f 70 69 65 64 20 6f 6e 74  ey is copied ont
19f2c 6f 20 74 68 65 20 50 33 20 72 65 67 69 73 74 65  o the P3 registe
19f2d 72 20 65 78 61 63 74 6c 79 20 61 73 20 0a 2a 2a  r exactly as .**
19f2e 20 69 74 20 69 73 20 66 6f 75 6e 64 20 69 6e 20   it is found in 
19f2f 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
19f30 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
19f31 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62  P1 cursor must b
19f32 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20  e pointing to a 
19f33 76 61 6c 69 64 20 72 6f 77 20 28 6e 6f 74 20 61  valid row (not a
19f34 20 4e 55 4c 4c 20 72 6f 77 29 0a 2a 2a 20 6f 66   NULL row).** of
19f35 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e   a real table, n
19f36 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c  ot a pseudo-tabl
19f37 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f  e..*/.case OP_Ro
19f38 77 4b 65 79 3a 0a 63 61 73 65 20 4f 50 5f 52 6f  wKey:.case OP_Ro
19f39 77 44 61 74 61 3a 20 7b 0a 20 20 69 6e 74 20 69  wData: {.  int i
19f3a 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64   = pOp->p1;.  Vd
19f3b 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20  beCursor *pC;.  
19f3c 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b  BtCursor *pCrsr;
19f3d 0a 20 20 75 33 32 20 6e 3b 0a 0a 20 20 70 4f 75  .  u32 n;..  pOu
19f3e 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  t = &p->aMem[pOp
19f3f 2d 3e 70 32 5d 3b 0a 0a 20 20 2f 2a 20 4e 6f 74  ->p2];..  /* Not
19f40 65 20 74 68 61 74 20 52 6f 77 4b 65 79 20 61 6e  e that RowKey an
19f41 64 20 52 6f 77 44 61 74 61 20 61 72 65 20 72 65  d RowData are re
19f42 61 6c 6c 79 20 65 78 61 63 74 6c 79 20 74 68 65  ally exactly the
19f43 20 73 61 6d 65 20 69 6e 73 74 72 75 63 74 69 6f   same instructio
19f44 6e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69  n */.  assert( i
19f45 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72  >=0 && i<p->nCur
19f46 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d  sor );.  pC = p-
19f47 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73  >apCsr[i];.  ass
19f48 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c 65  ert( pC->isTable
19f49 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d   || pOp->opcode=
19f4a 3d 4f 50 5f 52 6f 77 4b 65 79 20 29 3b 0a 20 20  =OP_RowKey );.  
19f4b 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 49 6e  assert( pC->isIn
19f4c 64 65 78 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f  dex || pOp->opco
19f4d 64 65 3d 3d 4f 50 5f 52 6f 77 44 61 74 61 20 29  de==OP_RowData )
19f4e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d  ;.  assert( pC!=
19f4f 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
19f50 43 2d 3e 6e 75 6c 6c 52 6f 77 3d 3d 30 20 29 3b  C->nullRow==0 );
19f51 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 70  .  assert( pC->p
19f52 73 65 75 64 6f 54 61 62 6c 65 3d 3d 30 20 29 3b  seudoTable==0 );
19f53 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 70  .  assert( pC->p
19f54 43 75 72 73 6f 72 21 3d 30 20 29 3b 0a 20 20 70  Cursor!=0 );.  p
19f55 43 72 73 72 20 3d 20 70 43 2d 3e 70 43 75 72 73  Crsr = pC->pCurs
19f56 6f 72 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  or;.  rc = sqlit
19f57 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65  e3VdbeCursorMove
19f58 74 6f 28 70 43 29 3b 0a 20 20 69 66 28 20 72 63  to(pC);.  if( rc
19f59 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
19f5a 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 69 66  e_to_error;.  if
19f5b 28 20 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b  ( pC->isIndex ){
19f5c 0a 20 20 20 20 69 36 34 20 6e 36 34 3b 0a 20 20  .    i64 n64;.  
19f5d 20 20 61 73 73 65 72 74 28 20 21 70 43 2d 3e 69    assert( !pC->i
19f5e 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 73 71  sTable );.    sq
19f5f 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a  lite3BtreeKeySiz
19f60 65 28 70 43 72 73 72 2c 20 26 6e 36 34 29 3b 0a  e(pCrsr, &n64);.
19f61 20 20 20 20 69 66 28 20 6e 36 34 3e 64 62 2d 3e      if( n64>db->
19f62 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
19f63 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20  MIT_LENGTH] ){. 
19f64 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69       goto too_bi
19f65 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 20 3d  g;.    }.    n =
19f66 20 6e 36 34 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   n64;.  }else{. 
19f67 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 44     sqlite3BtreeD
19f68 61 74 61 53 69 7a 65 28 70 43 72 73 72 2c 20 26  ataSize(pCrsr, &
19f69 6e 29 3b 0a 20 20 20 20 69 66 28 20 6e 3e 64 62  n);.    if( n>db
19f6a 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
19f6b 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b  LIMIT_LENGTH] ){
19f6c 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f  .      goto too_
19f6d 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  big;.    }.  }. 
19f6e 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65   if( sqlite3Vdbe
19f6f 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20 6e 2c  MemGrow(pOut, n,
19f70 20 30 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20   0) ){.    goto 
19f71 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 70 4f  no_mem;.  }.  pO
19f72 75 74 2d 3e 6e 20 3d 20 6e 3b 0a 20 20 4d 65 6d  ut->n = n;.  Mem
19f73 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74  SetTypeFlag(pOut
19f74 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 69  , MEM_Blob);.  i
19f75 66 28 20 70 43 2d 3e 69 73 49 6e 64 65 78 20 29  f( pC->isIndex )
19f76 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
19f77 65 33 42 74 72 65 65 4b 65 79 28 70 43 72 73 72  e3BtreeKey(pCrsr
19f78 2c 20 30 2c 20 6e 2c 20 70 4f 75 74 2d 3e 7a 29  , 0, n, pOut->z)
19f79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
19f7a 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
19f7b 44 61 74 61 28 70 43 72 73 72 2c 20 30 2c 20 6e  Data(pCrsr, 0, n
19f7c 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d 0a  , pOut->z);.  }.
19f7d 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 53 51    pOut->enc = SQ
19f7e 4c 49 54 45 5f 55 54 46 38 3b 20 20 2f 2a 20 49  LITE_UTF8;  /* I
19f7f 6e 20 63 61 73 65 20 74 68 65 20 62 6c 6f 62 20  n case the blob 
19f80 69 73 20 65 76 65 72 20 63 61 73 74 20 74 6f 20  is ever cast to 
19f81 74 65 78 74 20 2a 2f 0a 20 20 55 50 44 41 54 45  text */.  UPDATE
19f82 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f  _MAX_BLOBSIZE(pO
19f83 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  ut);.  break;.}.
19f84 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 69  ./* Opcode: Rowi
19f85 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  d P1 P2 * * *.**
19f86 0a 2a 2a 20 53 74 6f 72 65 20 69 6e 20 72 65 67  .** Store in reg
19f87 69 73 74 65 72 20 50 32 20 61 6e 20 69 6e 74 65  ister P2 an inte
19f88 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65  ger which is the
19f89 20 6b 65 79 20 6f 66 20 74 68 65 20 74 61 62 6c   key of the tabl
19f8a 65 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20  e entry that.** 
19f8b 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  P1 is currently 
19f8c 70 6f 69 6e 74 20 74 6f 2e 0a 2a 2f 0a 63 61 73  point to..*/.cas
19f8d 65 20 4f 50 5f 52 6f 77 69 64 3a 20 7b 20 20 20  e OP_Rowid: {   
19f8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19f8f 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65   out2-prerelease
19f90 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f   */.  int i = pO
19f91 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72  p->p1;.  VdbeCur
19f92 73 6f 72 20 2a 70 43 3b 0a 20 20 69 36 34 20 76  sor *pC;.  i64 v
19f93 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d  ;..  assert( i>=
19f94 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f  0 && i<p->nCurso
19f95 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61  r );.  pC = p->a
19f96 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72  pCsr[i];.  asser
19f97 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 72 63  t( pC!=0 );.  rc
19f98 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
19f99 72 73 6f 72 4d 6f 76 65 74 6f 28 70 43 29 3b 0a  rsorMoveto(pC);.
19f9a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20    if( rc ) goto 
19f9b 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
19f9c 6f 72 3b 0a 20 20 69 66 28 20 70 43 2d 3e 72 6f  or;.  if( pC->ro
19f9d 77 69 64 49 73 56 61 6c 69 64 20 29 7b 0a 20 20  widIsValid ){.  
19f9e 20 20 76 20 3d 20 70 43 2d 3e 6c 61 73 74 52 6f    v = pC->lastRo
19f9f 77 69 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  wid;.  }else if(
19fa0 20 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65   pC->pseudoTable
19fa1 20 29 7b 0a 20 20 20 20 76 20 3d 20 6b 65 79 54   ){.    v = keyT
19fa2 6f 49 6e 74 28 70 43 2d 3e 69 4b 65 79 29 3b 0a  oInt(pC->iKey);.
19fa3 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43 2d 3e    }else if( pC->
19fa4 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 2f  nullRow ){.    /
19fa5 2a 20 4c 65 61 76 65 20 74 68 65 20 72 6f 77 69  * Leave the rowi
19fa6 64 20 73 65 74 20 74 6f 20 61 20 4e 55 4c 4c 20  d set to a NULL 
19fa7 2a 2f 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  */.    break;.  
19fa8 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
19fa9 74 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d  t( pC->pCursor!=
19faa 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 );.    sqlite3
19fab 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 2d  BtreeKeySize(pC-
19fac 3e 70 43 75 72 73 6f 72 2c 20 26 76 29 3b 0a 20  >pCursor, &v);. 
19fad 20 20 20 76 20 3d 20 6b 65 79 54 6f 49 6e 74 28     v = keyToInt(
19fae 76 29 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e  v);.  }.  pOut->
19faf 75 2e 69 20 3d 20 76 3b 0a 20 20 4d 65 6d 53 65  u.i = v;.  MemSe
19fb0 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20  tTypeFlag(pOut, 
19fb1 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61  MEM_Int);.  brea
19fb2 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
19fb3 20 4e 75 6c 6c 52 6f 77 20 50 31 20 2a 20 2a 20   NullRow P1 * * 
19fb4 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 20 74  * *.**.** Move t
19fb5 68 65 20 63 75 72 73 6f 72 20 50 31 20 74 6f 20  he cursor P1 to 
19fb6 61 20 6e 75 6c 6c 20 72 6f 77 2e 20 20 41 6e 79  a null row.  Any
19fb7 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 65 72 61   OP_Column opera
19fb8 74 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 6f 63  tions.** that oc
19fb9 63 75 72 20 77 68 69 6c 65 20 74 68 65 20 63 75  cur while the cu
19fba 72 73 6f 72 20 69 73 20 6f 6e 20 74 68 65 20 6e  rsor is on the n
19fbb 75 6c 6c 20 72 6f 77 20 77 69 6c 6c 20 61 6c 77  ull row will alw
19fbc 61 79 73 0a 2a 2a 20 77 72 69 74 65 20 61 20 4e  ays.** write a N
19fbd 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ULL..*/.case OP_
19fbe 4e 75 6c 6c 52 6f 77 3a 20 7b 0a 20 20 69 6e 74  NullRow: {.  int
19fbf 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20   i = pOp->p1;.  
19fc0 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
19fc1 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20  .  assert( i>=0 
19fc2 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  && i<p->nCursor 
19fc3 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43  );.  pC = p->apC
19fc4 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28  sr[i];.  assert(
19fc5 20 70 43 21 3d 30 20 29 3b 0a 20 20 70 43 2d 3e   pC!=0 );.  pC->
19fc6 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 70  nullRow = 1;.  p
19fc7 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20  C->rowidIsValid 
19fc8 3d 20 30 3b 0a 20 20 69 66 28 20 70 43 2d 3e 70  = 0;.  if( pC->p
19fc9 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71  Cursor ){.    sq
19fca 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43  lite3BtreeClearC
19fcb 75 72 73 6f 72 28 70 43 2d 3e 70 43 75 72 73 6f  ursor(pC->pCurso
19fcc 72 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  r);.  }.  break;
19fcd 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c  .}../* Opcode: L
19fce 61 73 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  ast P1 P2 * * *.
19fcf 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 75  **.** The next u
19fd0 73 65 20 6f 66 20 74 68 65 20 52 6f 77 69 64 20  se of the Rowid 
19fd1 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65 78  or Column or Nex
19fd2 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f  t instruction fo
19fd3 72 20 50 31 20 0a 2a 2a 20 77 69 6c 6c 20 72 65  r P1 .** will re
19fd4 66 65 72 20 74 6f 20 74 68 65 20 6c 61 73 74 20  fer to the last 
19fd5 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 74  entry in the dat
19fd6 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69  abase table or i
19fd7 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 74 68 65 20  ndex..** If the 
19fd8 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69  table or index i
19fd9 73 20 65 6d 70 74 79 20 61 6e 64 20 50 32 3e 30  s empty and P2>0
19fda 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65  , then jump imme
19fdb 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a  diately to P2..*
19fdc 2a 20 49 66 20 50 32 20 69 73 20 30 20 6f 72 20  * If P2 is 0 or 
19fdd 69 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20  if the table or 
19fde 69 6e 64 65 78 20 69 73 20 6e 6f 74 20 65 6d 70  index is not emp
19fdf 74 79 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68  ty, fall through
19fe0 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f  .** to the follo
19fe1 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e  wing instruction
19fe2 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 61 73  ..*/.case OP_Las
19fe3 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a  t: {        /* j
19fe4 75 6d 70 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d  ump */.  int i =
19fe5 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65   pOp->p1;.  Vdbe
19fe6 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74  Cursor *pC;.  Bt
19fe7 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20  Cursor *pCrsr;. 
19fe8 20 69 6e 74 20 72 65 73 3b 0a 0a 20 20 61 73 73   int res;..  ass
19fe9 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70  ert( i>=0 && i<p
19fea 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70  ->nCursor );.  p
19feb 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b  C = p->apCsr[i];
19fec 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d 30  .  assert( pC!=0
19fed 20 29 3b 0a 20 20 70 43 72 73 72 20 3d 20 70 43   );.  pCrsr = pC
19fee 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 61 73 73  ->pCursor;.  ass
19fef 65 72 74 28 20 70 43 72 73 72 21 3d 30 20 29 3b  ert( pCrsr!=0 );
19ff0 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
19ff1 74 72 65 65 4c 61 73 74 28 70 43 72 73 72 2c 20  treeLast(pCrsr, 
19ff2 26 72 65 73 29 3b 0a 20 20 70 43 2d 3e 6e 75 6c  &res);.  pC->nul
19ff3 6c 52 6f 77 20 3d 20 72 65 73 3b 0a 20 20 70 43  lRow = res;.  pC
19ff4 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f  ->deferredMoveto
19ff5 20 3d 20 30 3b 0a 20 20 70 43 2d 3e 63 61 63 68   = 0;.  pC->cach
19ff6 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f  eStatus = CACHE_
19ff7 53 54 41 4c 45 3b 0a 20 20 69 66 28 20 72 65 73  STALE;.  if( res
19ff8 20 26 26 20 70 4f 70 2d 3e 70 32 3e 30 20 29 7b   && pOp->p2>0 ){
19ff9 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70  .    pc = pOp->p
19ffa 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65  2 - 1;.  }.  bre
19ffb 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64  ak;.}.../* Opcod
19ffc 65 3a 20 53 6f 72 74 20 50 31 20 50 32 20 2a 20  e: Sort P1 P2 * 
19ffd 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f  * *.**.** This o
19ffe 70 63 6f 64 65 20 64 6f 65 73 20 65 78 61 63 74  pcode does exact
19fff 6c 79 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e  ly the same thin
1a000 67 20 61 73 20 4f 50 5f 52 65 77 69 6e 64 20 65  g as OP_Rewind e
1a001 78 63 65 70 74 20 74 68 61 74 0a 2a 2a 20 69 74  xcept that.** it
1a002 20 69 6e 63 72 65 6d 65 6e 74 73 20 61 6e 20 75   increments an u
1a003 6e 64 6f 63 75 6d 65 6e 74 65 64 20 67 6c 6f 62  ndocumented glob
1a004 61 6c 20 76 61 72 69 61 62 6c 65 20 75 73 65 64  al variable used
1a005 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2a   for testing..**
1a006 0a 2a 2a 20 53 6f 72 74 69 6e 67 20 69 73 20 61  .** Sorting is a
1a007 63 63 6f 6d 70 6c 69 73 68 65 64 20 62 79 20 77  ccomplished by w
1a008 72 69 74 69 6e 67 20 72 65 63 6f 72 64 73 20 69  riting records i
1a009 6e 74 6f 20 61 20 73 6f 72 74 69 6e 67 20 69 6e  nto a sorting in
1a00a 64 65 78 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 77  dex,.** then rew
1a00b 69 6e 64 69 6e 67 20 74 68 61 74 20 69 6e 64 65  inding that inde
1a00c 78 20 61 6e 64 20 70 6c 61 79 69 6e 67 20 69 74  x and playing it
1a00d 20 62 61 63 6b 20 66 72 6f 6d 20 62 65 67 69 6e   back from begin
1a00e 6e 69 6e 67 20 74 6f 0a 2a 2a 20 65 6e 64 2e 20  ning to.** end. 
1a00f 20 57 65 20 75 73 65 20 74 68 65 20 4f 50 5f 53   We use the OP_S
1a010 6f 72 74 20 6f 70 63 6f 64 65 20 69 6e 73 74 65  ort opcode inste
1a011 61 64 20 6f 66 20 4f 50 5f 52 65 77 69 6e 64 20  ad of OP_Rewind 
1a012 74 6f 20 64 6f 20 74 68 65 0a 2a 2a 20 72 65 77  to do the.** rew
1a013 69 6e 64 69 6e 67 20 73 6f 20 74 68 61 74 20 74  inding so that t
1a014 68 65 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62  he global variab
1a015 6c 65 20 77 69 6c 6c 20 62 65 20 69 6e 63 72 65  le will be incre
1a016 6d 65 6e 74 65 64 20 61 6e 64 0a 2a 2a 20 72 65  mented and.** re
1a017 67 72 65 73 73 69 6f 6e 20 74 65 73 74 73 20 63  gression tests c
1a018 61 6e 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65  an determine whe
1a019 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
1a01a 6f 70 74 69 6d 69 7a 65 72 20 69 73 0a 2a 2a 20  optimizer is.** 
1a01b 63 6f 72 72 65 63 74 6c 79 20 6f 70 74 69 6d 69  correctly optimi
1a01c 7a 69 6e 67 20 6f 75 74 20 73 6f 72 74 73 2e 0a  zing out sorts..
1a01d 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 6f 72 74 3a  */.case OP_Sort:
1a01e 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d   {        /* jum
1a01f 70 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  p */.#ifdef SQLI
1a020 54 45 5f 54 45 53 54 0a 20 20 73 71 6c 69 74 65  TE_TEST.  sqlite
1a021 33 5f 73 6f 72 74 5f 63 6f 75 6e 74 2b 2b 3b 0a  3_sort_count++;.
1a022 20 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68    sqlite3_search
1a023 5f 63 6f 75 6e 74 2d 2d 3b 0a 23 65 6e 64 69 66  _count--;.#endif
1a024 0a 20 20 70 2d 3e 61 43 6f 75 6e 74 65 72 5b 53  .  p->aCounter[S
1a025 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53  QLITE_STMTSTATUS
1a026 5f 53 4f 52 54 2d 31 5d 2b 2b 3b 0a 20 20 2f 2a  _SORT-1]++;.  /*
1a027 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e   Fall through in
1a028 74 6f 20 4f 50 5f 52 65 77 69 6e 64 20 2a 2f 0a  to OP_Rewind */.
1a029 7d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 77  }./* Opcode: Rew
1a02a 69 6e 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  ind P1 P2 * * *.
1a02b 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 75  **.** The next u
1a02c 73 65 20 6f 66 20 74 68 65 20 52 6f 77 69 64 20  se of the Rowid 
1a02d 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65 78  or Column or Nex
1a02e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f  t instruction fo
1a02f 72 20 50 31 20 0a 2a 2a 20 77 69 6c 6c 20 72 65  r P1 .** will re
1a030 66 65 72 20 74 6f 20 74 68 65 20 66 69 72 73 74  fer to the first
1a031 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61   entry in the da
1a032 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20  tabase table or 
1a033 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 74 68 65  index..** If the
1a034 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20   table or index 
1a035 69 73 20 65 6d 70 74 79 20 61 6e 64 20 50 32 3e  is empty and P2>
1a036 30 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d  0, then jump imm
1a037 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a  ediately to P2..
1a038 2a 2a 20 49 66 20 50 32 20 69 73 20 30 20 6f 72  ** If P2 is 0 or
1a039 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72   if the table or
1a03a 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20 65 6d   index is not em
1a03b 70 74 79 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67  pty, fall throug
1a03c 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c  h.** to the foll
1a03d 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f  owing instructio
1a03e 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65  n..*/.case OP_Re
1a03f 77 69 6e 64 3a 20 7b 20 20 20 20 20 20 20 20 2f  wind: {        /
1a040 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 69 6e 74 20  * jump */.  int 
1a041 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56  i = pOp->p1;.  V
1a042 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20  dbeCursor *pC;. 
1a043 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72   BtCursor *pCrsr
1a044 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 0a 20 20  ;.  int res;..  
1a045 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
1a046 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  i<p->nCursor );.
1a047 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b    pC = p->apCsr[
1a048 69 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43  i];.  assert( pC
1a049 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43  !=0 );.  if( (pC
1a04a 72 73 72 20 3d 20 70 43 2d 3e 70 43 75 72 73 6f  rsr = pC->pCurso
1a04b 72 29 21 3d 30 20 29 7b 0a 20 20 20 20 72 63 20  r)!=0 ){.    rc 
1a04c 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 69  = sqlite3BtreeFi
1a04d 72 73 74 28 70 43 72 73 72 2c 20 26 72 65 73 29  rst(pCrsr, &res)
1a04e 3b 0a 20 20 20 20 70 43 2d 3e 61 74 46 69 72 73  ;.    pC->atFirs
1a04f 74 20 3d 20 72 65 73 3d 3d 30 3b 0a 20 20 20 20  t = res==0;.    
1a050 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65  pC->deferredMove
1a051 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e  to = 0;.    pC->
1a052 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41  cacheStatus = CA
1a053 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 65 6c  CHE_STALE;.  }el
1a054 73 65 7b 0a 20 20 20 20 72 65 73 20 3d 20 31 3b  se{.    res = 1;
1a055 0a 20 20 7d 0a 20 20 70 43 2d 3e 6e 75 6c 6c 52  .  }.  pC->nullR
1a056 6f 77 20 3d 20 72 65 73 3b 0a 20 20 61 73 73 65  ow = res;.  asse
1a057 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26  rt( pOp->p2>0 &&
1a058 20 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4f 70 20   pOp->p2<p->nOp 
1a059 29 3b 0a 20 20 69 66 28 20 72 65 73 20 29 7b 0a  );.  if( res ){.
1a05a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32      pc = pOp->p2
1a05b 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61   - 1;.  }.  brea
1a05c 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1a05d 20 4e 65 78 74 20 50 31 20 50 32 20 2a 20 2a 20   Next P1 P2 * * 
1a05e 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20  *.**.** Advance 
1a05f 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61  cursor P1 so tha
1a060 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74  t it points to t
1a061 68 65 20 6e 65 78 74 20 6b 65 79 2f 64 61 74 61  he next key/data
1a062 20 70 61 69 72 20 69 6e 20 69 74 73 0a 2a 2a 20   pair in its.** 
1a063 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 20  table or index. 
1a064 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f   If there are no
1a065 20 6d 6f 72 65 20 6b 65 79 2f 76 61 6c 75 65 20   more key/value 
1a066 70 61 69 72 73 20 74 68 65 6e 20 66 61 6c 6c 20  pairs then fall 
1a067 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68  through.** to th
1a068 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74  e following inst
1a069 72 75 63 74 69 6f 6e 2e 20 20 42 75 74 20 69 66  ruction.  But if
1a06a 20 74 68 65 20 63 75 72 73 6f 72 20 61 64 76 61   the cursor adva
1a06b 6e 63 65 20 77 61 73 20 73 75 63 63 65 73 73 66  nce was successf
1a06c 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 69 6d 6d 65  ul,.** jump imme
1a06d 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a  diately to P2..*
1a06e 2a 0a 2a 2a 20 54 68 65 20 50 31 20 63 75 72 73  *.** The P1 curs
1a06f 6f 72 20 6d 75 73 74 20 62 65 20 66 6f 72 20 61  or must be for a
1a070 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f 74   real table, not
1a071 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e   a pseudo-table.
1a072 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
1a073 20 50 72 65 76 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f   Prev.*/./* Opco
1a074 64 65 3a 20 50 72 65 76 20 50 31 20 50 32 20 2a  de: Prev P1 P2 *
1a075 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 61 63 6b 20   * *.**.** Back 
1a076 75 70 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20  up cursor P1 so 
1a077 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74  that it points t
1a078 6f 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 6b  o the previous k
1a079 65 79 2f 64 61 74 61 20 70 61 69 72 20 69 6e 20  ey/data pair in 
1a07a 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20  its.** table or 
1a07b 69 6e 64 65 78 2e 20 20 49 66 20 74 68 65 72 65  index.  If there
1a07c 20 69 73 20 6e 6f 20 70 72 65 76 69 6f 75 73 20   is no previous 
1a07d 6b 65 79 2f 76 61 6c 75 65 20 70 61 69 72 73 20  key/value pairs 
1a07e 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67  then fall throug
1a07f 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c  h.** to the foll
1a080 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f  owing instructio
1a081 6e 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 63  n.  But if the c
1a082 75 72 73 6f 72 20 62 61 63 6b 75 70 20 77 61 73  ursor backup was
1a083 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20   successful,.** 
1a084 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79  jump immediately
1a085 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68   to P2..**.** Th
1a086 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74  e P1 cursor must
1a087 20 62 65 20 66 6f 72 20 61 20 72 65 61 6c 20 74   be for a real t
1a088 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75  able, not a pseu
1a089 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73  do-table..*/.cas
1a08a 65 20 4f 50 5f 50 72 65 76 3a 20 20 20 20 20 20  e OP_Prev:      
1a08b 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 63      /* jump */.c
1a08c 61 73 65 20 4f 50 5f 4e 65 78 74 3a 20 7b 20 20  ase OP_Next: {  
1a08d 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f        /* jump */
1a08e 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
1a08f 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  C;.  BtCursor *p
1a090 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b  Crsr;.  int res;
1a091 0a 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e  ..  CHECK_FOR_IN
1a092 54 45 52 52 55 50 54 3b 0a 20 20 61 73 73 65 72  TERRUPT;.  asser
1a093 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26  t( pOp->p1>=0 &&
1a094 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72   pOp->p1<p->nCur
1a095 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d  sor );.  pC = p-
1a096 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
1a097 0a 20 20 69 66 28 20 70 43 3d 3d 30 20 29 7b 0a  .  if( pC==0 ){.
1a098 20 20 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20 53      break;  /* S
1a099 65 65 20 74 69 63 6b 65 74 20 23 32 32 37 33 20  ee ticket #2273 
1a09a 2a 2f 0a 20 20 7d 0a 20 20 70 43 72 73 72 20 3d  */.  }.  pCrsr =
1a09b 20 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20   pC->pCursor;.  
1a09c 61 73 73 65 72 74 28 20 70 43 72 73 72 20 29 3b  assert( pCrsr );
1a09d 0a 20 20 72 65 73 20 3d 20 31 3b 0a 20 20 61 73  .  res = 1;.  as
1a09e 73 65 72 74 28 20 70 43 2d 3e 64 65 66 65 72 72  sert( pC->deferr
1a09f 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20  edMoveto==0 );. 
1a0a0 20 72 63 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64   rc = pOp->opcod
1a0a1 65 3d 3d 4f 50 5f 4e 65 78 74 20 3f 20 73 71 6c  e==OP_Next ? sql
1a0a2 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 70 43  ite3BtreeNext(pC
1a0a3 72 73 72 2c 20 26 72 65 73 29 20 3a 0a 20 20 20  rsr, &res) :.   
1a0a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a0a5 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1a0a6 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28  e3BtreePrevious(
1a0a7 70 43 72 73 72 2c 20 26 72 65 73 29 3b 0a 20 20  pCrsr, &res);.  
1a0a8 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 72 65  pC->nullRow = re
1a0a9 73 3b 0a 20 20 70 43 2d 3e 63 61 63 68 65 53 74  s;.  pC->cacheSt
1a0aa 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41  atus = CACHE_STA
1a0ab 4c 45 3b 0a 20 20 69 66 28 20 72 65 73 3d 3d 30  LE;.  if( res==0
1a0ac 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70   ){.    pc = pOp
1a0ad 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 69 66  ->p2 - 1;.    if
1a0ae 28 20 70 4f 70 2d 3e 70 35 20 29 20 70 2d 3e 61  ( pOp->p5 ) p->a
1a0af 43 6f 75 6e 74 65 72 5b 70 4f 70 2d 3e 70 35 2d  Counter[pOp->p5-
1a0b0 31 5d 2b 2b 3b 0a 23 69 66 64 65 66 20 53 51 4c  1]++;.#ifdef SQL
1a0b1 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c  ITE_TEST.    sql
1a0b2 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e  ite3_search_coun
1a0b3 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a  t++;.#endif.  }.
1a0b4 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c    pC->rowidIsVal
1a0b5 69 64 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b  id = 0;.  break;
1a0b6 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49  .}../* Opcode: I
1a0b7 64 78 49 6e 73 65 72 74 20 50 31 20 50 32 20 50  dxInsert P1 P2 P
1a0b8 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 67 69  3 * *.**.** Regi
1a0b9 73 74 65 72 20 50 32 20 68 6f 6c 64 73 20 61 20  ster P2 holds a 
1a0ba 53 51 4c 20 69 6e 64 65 78 20 6b 65 79 20 6d 61  SQL index key ma
1a0bb 64 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  de using the.** 
1a0bc 4d 61 6b 65 49 64 78 52 65 63 20 69 6e 73 74 72  MakeIdxRec instr
1a0bd 75 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 6f  uctions.  This o
1a0be 70 63 6f 64 65 20 77 72 69 74 65 73 20 74 68 61  pcode writes tha
1a0bf 74 20 6b 65 79 0a 2a 2a 20 69 6e 74 6f 20 74 68  t key.** into th
1a0c0 65 20 69 6e 64 65 78 20 50 31 2e 20 20 44 61 74  e index P1.  Dat
1a0c1 61 20 66 6f 72 20 74 68 65 20 65 6e 74 72 79 20  a for the entry 
1a0c2 69 73 20 6e 69 6c 2e 0a 2a 2a 0a 2a 2a 20 50 33  is nil..**.** P3
1a0c3 20 69 73 20 61 20 66 6c 61 67 20 74 68 61 74 20   is a flag that 
1a0c4 70 72 6f 76 69 64 65 73 20 61 20 68 69 6e 74 20  provides a hint 
1a0c5 74 6f 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61  to the b-tree la
1a0c6 79 65 72 20 74 68 61 74 20 74 68 69 73 0a 2a 2a  yer that this.**
1a0c7 20 69 6e 73 65 72 74 20 69 73 20 6c 69 6b 65 6c   insert is likel
1a0c8 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65 6e  y to be an appen
1a0c9 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  d..**.** This in
1a0ca 73 74 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 20 77  struction only w
1a0cb 6f 72 6b 73 20 66 6f 72 20 69 6e 64 69 63 65 73  orks for indices
1a0cc 2e 20 20 54 68 65 20 65 71 75 69 76 61 6c 65 6e  .  The equivalen
1a0cd 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a  t instruction.**
1a0ce 20 66 6f 72 20 74 61 62 6c 65 73 20 69 73 20 4f   for tables is O
1a0cf 50 5f 49 6e 73 65 72 74 2e 0a 2a 2f 0a 63 61 73  P_Insert..*/.cas
1a0d0 65 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 3a 20  e OP_IdxInsert: 
1a0d1 7b 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20  {        /* in2 
1a0d2 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70  */.  int i = pOp
1a0d3 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73  ->p1;.  VdbeCurs
1a0d4 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73  or *pC;.  BtCurs
1a0d5 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 61 73 73  or *pCrsr;.  ass
1a0d6 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70  ert( i>=0 && i<p
1a0d7 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61  ->nCursor );.  a
1a0d8 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b  ssert( p->apCsr[
1a0d9 69 5d 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  i]!=0 );.  asser
1a0da 74 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26  t( pIn2->flags &
1a0db 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 69   MEM_Blob );.  i
1a0dc 66 28 20 28 70 43 72 73 72 20 3d 20 28 70 43 20  f( (pCrsr = (pC 
1a0dd 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 2d 3e  = p->apCsr[i])->
1a0de 70 43 75 72 73 6f 72 29 21 3d 30 20 29 7b 0a 20  pCursor)!=0 ){. 
1a0df 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69     assert( pC->i
1a0e0 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20  sTable==0 );.   
1a0e1 20 72 63 20 3d 20 45 78 70 61 6e 64 42 6c 6f 62   rc = ExpandBlob
1a0e2 28 70 49 6e 32 29 3b 0a 20 20 20 20 69 66 28 20  (pIn2);.    if( 
1a0e3 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1a0e4 0a 20 20 20 20 20 20 69 6e 74 20 6e 4b 65 79 20  .      int nKey 
1a0e5 3d 20 70 49 6e 32 2d 3e 6e 3b 0a 20 20 20 20 20  = pIn2->n;.     
1a0e6 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4b 65   const char *zKe
1a0e7 79 20 3d 20 70 49 6e 32 2d 3e 7a 3b 0a 20 20 20  y = pIn2->z;.   
1a0e8 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1a0e9 74 72 65 65 49 6e 73 65 72 74 28 70 43 72 73 72  treeInsert(pCrsr
1a0ea 2c 20 7a 4b 65 79 2c 20 6e 4b 65 79 2c 20 22 22  , zKey, nKey, ""
1a0eb 2c 20 30 2c 20 30 2c 20 70 4f 70 2d 3e 70 33 29  , 0, 0, pOp->p3)
1a0ec 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1a0ed 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65  pC->deferredMove
1a0ee 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  to==0 );.      p
1a0ef 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  C->cacheStatus =
1a0f0 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20   CACHE_STALE;.  
1a0f1 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b    }.  }.  break;
1a0f2 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49  .}../* Opcode: I
1a0f3 64 78 44 65 6c 65 74 65 20 50 31 20 50 32 20 50  dxDelete P1 P2 P
1a0f4 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20  3 * *.**.** The 
1a0f5 63 6f 6e 74 65 6e 74 20 6f 66 20 50 33 20 72 65  content of P3 re
1a0f6 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e 67  gisters starting
1a0f7 20 61 74 20 72 65 67 69 73 74 65 72 20 50 32 20   at register P2 
1a0f8 66 6f 72 6d 0a 2a 2a 20 61 6e 20 75 6e 70 61 63  form.** an unpac
1a0f9 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 54  ked index key. T
1a0fa 68 69 73 20 6f 70 63 6f 64 65 20 72 65 6d 6f 76  his opcode remov
1a0fb 65 73 20 74 68 61 74 20 65 6e 74 72 79 20 66 72  es that entry fr
1a0fc 6f 6d 20 74 68 65 20 0a 2a 2a 20 69 6e 64 65 78  om the .** index
1a0fd 20 6f 70 65 6e 65 64 20 62 79 20 63 75 72 73 6f   opened by curso
1a0fe 72 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  r P1..*/.case OP
1a0ff 5f 49 64 78 44 65 6c 65 74 65 3a 20 7b 0a 20 20  _IdxDelete: {.  
1a100 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b  int i = pOp->p1;
1a101 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
1a102 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  C;.  BtCursor *p
1a103 43 72 73 72 3b 0a 20 20 61 73 73 65 72 74 28 20  Crsr;.  assert( 
1a104 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 61  pOp->p3>0 );.  a
1a105 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30  ssert( pOp->p2>0
1a106 20 26 26 20 70 4f 70 2d 3e 70 32 2b 70 4f 70 2d   && pOp->p2+pOp-
1a107 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p3<=p->nMem );.
1a108 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
1a109 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  & i<p->nCursor )
1a10a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61  ;.  assert( p->a
1a10b 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20  pCsr[i]!=0 );.  
1a10c 69 66 28 20 28 70 43 72 73 72 20 3d 20 28 70 43  if( (pCrsr = (pC
1a10d 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 2d   = p->apCsr[i])-
1a10e 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29 7b 0a  >pCursor)!=0 ){.
1a10f 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20      int res;.   
1a110 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
1a111 72 3b 0a 20 20 20 20 72 2e 70 4b 65 79 49 6e 66  r;.    r.pKeyInf
1a112 6f 20 3d 20 70 43 2d 3e 70 4b 65 79 49 6e 66 6f  o = pC->pKeyInfo
1a113 3b 0a 20 20 20 20 72 2e 6e 46 69 65 6c 64 20 3d  ;.    r.nField =
1a114 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 72 2e   pOp->p3;.    r.
1a115 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 72  flags = 0;.    r
1a116 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d  .aMem = &p->aMem
1a117 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 72  [pOp->p2];.    r
1a118 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1a119 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70  MovetoUnpacked(p
1a11a 43 72 73 72 2c 20 26 72 2c 20 30 2c 20 30 2c 20  Crsr, &r, 0, 0, 
1a11b 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20 72  &res);.    if( r
1a11c 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
1a11d 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  res==0 ){.      
1a11e 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1a11f 65 44 65 6c 65 74 65 28 70 43 72 73 72 29 3b 0a  eDelete(pCrsr);.
1a120 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
1a121 28 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f  ( pC->deferredMo
1a122 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 70  veto==0 );.    p
1a123 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  C->cacheStatus =
1a124 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20   CACHE_STALE;.  
1a125 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1a126 20 4f 70 63 6f 64 65 3a 20 49 64 78 52 6f 77 69   Opcode: IdxRowi
1a127 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  d P1 P2 * * *.**
1a128 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 72  .** Write into r
1a129 65 67 69 73 74 65 72 20 50 32 20 61 6e 20 69 6e  egister P2 an in
1a12a 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74  teger which is t
1a12b 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e  he last entry in
1a12c 20 74 68 65 20 72 65 63 6f 72 64 20 61 74 0a 2a   the record at.*
1a12d 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  * the end of the
1a12e 20 69 6e 64 65 78 20 6b 65 79 20 70 6f 69 6e 74   index key point
1a12f 65 64 20 74 6f 20 62 79 20 63 75 72 73 6f 72 20  ed to by cursor 
1a130 50 31 2e 20 20 54 68 69 73 20 69 6e 74 65 67 65  P1.  This intege
1a131 72 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 74  r should be.** t
1a132 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20  he rowid of the 
1a133 74 61 62 6c 65 20 65 6e 74 72 79 20 74 6f 20 77  table entry to w
1a134 68 69 63 68 20 74 68 69 73 20 69 6e 64 65 78 20  hich this index 
1a135 65 6e 74 72 79 20 70 6f 69 6e 74 73 2e 0a 2a 2a  entry points..**
1a136 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 52 6f  .** See also: Ro
1a137 77 69 64 2c 20 4d 61 6b 65 49 64 78 52 65 63 2e  wid, MakeIdxRec.
1a138 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 52  .*/.case OP_IdxR
1a139 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20  owid: {         
1a13a 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65       /* out2-pre
1a13b 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74  release */.  int
1a13c 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20   i = pOp->p1;.  
1a13d 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b  BtCursor *pCrsr;
1a13e 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
1a13f 43 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e  C;..  assert( i>
1a140 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73  =0 && i<p->nCurs
1a141 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  or );.  assert( 
1a142 70 2d 3e 61 70 43 73 72 5b 69 5d 21 3d 30 20 29  p->apCsr[i]!=0 )
1a143 3b 0a 20 20 69 66 28 20 28 70 43 72 73 72 20 3d  ;.  if( (pCrsr =
1a144 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b   (pC = p->apCsr[
1a145 69 5d 29 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30  i])->pCursor)!=0
1a146 20 29 7b 0a 20 20 20 20 69 36 34 20 72 6f 77 69   ){.    i64 rowi
1a147 64 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20  d;..    assert( 
1a148 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65  pC->deferredMove
1a149 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73  to==0 );.    ass
1a14a 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c 65  ert( pC->isTable
1a14b 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 21  ==0 );.    if( !
1a14c 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20  pC->nullRow ){. 
1a14d 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1a14e 33 56 64 62 65 49 64 78 52 6f 77 69 64 28 70 43  3VdbeIdxRowid(pC
1a14f 72 73 72 2c 20 26 72 6f 77 69 64 29 3b 0a 20 20  rsr, &rowid);.  
1a150 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1a151 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1a152 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1a153 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20  to_error;.      
1a154 7d 0a 20 20 20 20 20 20 4d 65 6d 53 65 74 54 79  }.      MemSetTy
1a155 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d  peFlag(pOut, MEM
1a156 5f 49 6e 74 29 3b 0a 20 20 20 20 20 20 70 4f 75  _Int);.      pOu
1a157 74 2d 3e 75 2e 69 20 3d 20 72 6f 77 69 64 3b 0a  t->u.i = rowid;.
1a158 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61      }.  }.  brea
1a159 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1a15a 20 49 64 78 47 45 20 50 31 20 50 32 20 50 33 20   IdxGE P1 P2 P3 
1a15b 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  P4 P5.**.** The 
1a15c 50 34 20 72 65 67 69 73 74 65 72 20 76 61 6c 75  P4 register valu
1a15d 65 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74  es beginning wit
1a15e 68 20 50 33 20 66 6f 72 6d 20 61 6e 20 75 6e 70  h P3 form an unp
1a15f 61 63 6b 65 64 20 69 6e 64 65 78 20 0a 2a 2a 20  acked index .** 
1a160 6b 65 79 20 74 68 61 74 20 6f 6d 69 74 73 20 74  key that omits t
1a161 68 65 20 52 4f 57 49 44 2e 20 20 43 6f 6d 70 61  he ROWID.  Compa
1a162 72 65 20 74 68 69 73 20 6b 65 79 20 76 61 6c 75  re this key valu
1a163 65 20 61 67 61 69 6e 73 74 20 74 68 65 20 69 6e  e against the in
1a164 64 65 78 20 0a 2a 2a 20 74 68 61 74 20 50 31 20  dex .** that P1 
1a165 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  is currently poi
1a166 6e 74 69 6e 67 20 74 6f 2c 20 69 67 6e 6f 72 69  nting to, ignori
1a167 6e 67 20 74 68 65 20 52 4f 57 49 44 20 6f 6e 20  ng the ROWID on 
1a168 74 68 65 20 50 31 20 69 6e 64 65 78 2e 0a 2a 2a  the P1 index..**
1a169 0a 2a 2a 20 49 66 20 74 68 65 20 50 31 20 69 6e  .** If the P1 in
1a16a 64 65 78 20 65 6e 74 72 79 20 69 73 20 67 72 65  dex entry is gre
1a16b 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75  ater than or equ
1a16c 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 76 61  al to the key va
1a16d 6c 75 65 0a 2a 2a 20 74 68 65 6e 20 6a 75 6d 70  lue.** then jump
1a16e 20 74 6f 20 50 32 2e 20 20 4f 74 68 65 72 77 69   to P2.  Otherwi
1a16f 73 65 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20  se fall through 
1a170 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74  to the next inst
1a171 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49  ruction..**.** I
1a172 66 20 50 35 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  f P5 is non-zero
1a173 20 74 68 65 6e 20 74 68 65 20 6b 65 79 20 76 61   then the key va
1a174 6c 75 65 20 69 73 20 69 6e 63 72 65 61 73 65 64  lue is increased
1a175 20 62 79 20 61 6e 20 65 70 73 69 6c 6f 6e 20 0a   by an epsilon .
1a176 2a 2a 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20  ** prior to the 
1a177 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 54 68 69  comparison.  Thi
1a178 73 20 6d 61 6b 65 20 74 68 65 20 6f 70 63 6f 64  s make the opcod
1a179 65 20 77 6f 72 6b 20 6c 69 6b 65 20 49 64 78 47  e work like IdxG
1a17a 54 20 65 78 63 65 70 74 0a 2a 2a 20 74 68 61 74  T except.** that
1a17b 20 69 66 20 74 68 65 20 6b 65 79 20 66 72 6f 6d   if the key from
1a17c 20 72 65 67 69 73 74 65 72 20 50 33 20 69 73 20   register P3 is 
1a17d 61 20 70 72 65 66 69 78 20 6f 66 20 74 68 65 20  a prefix of the 
1a17e 6b 65 79 20 69 6e 20 74 68 65 20 63 75 72 73 6f  key in the curso
1a17f 72 2c 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74  r,.** the result
1a180 20 69 73 20 66 61 6c 73 65 20 77 68 65 72 65 61   is false wherea
1a181 73 20 69 74 20 77 6f 75 6c 64 20 62 65 20 74 72  s it would be tr
1a182 75 65 20 77 69 74 68 20 49 64 78 47 54 2e 0a 2a  ue with IdxGT..*
1a183 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78  /./* Opcode: Idx
1a184 4c 54 20 50 31 20 50 32 20 50 33 20 2a 20 50 35  LT P1 P2 P3 * P5
1a185 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20 72 65  .**.** The P4 re
1a186 67 69 73 74 65 72 20 76 61 6c 75 65 73 20 62 65  gister values be
1a187 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 50 33 20  ginning with P3 
1a188 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64  form an unpacked
1a189 20 69 6e 64 65 78 20 0a 2a 2a 20 6b 65 79 20 74   index .** key t
1a18a 68 61 74 20 6f 6d 69 74 73 20 74 68 65 20 52 4f  hat omits the RO
1a18b 57 49 44 2e 20 20 43 6f 6d 70 61 72 65 20 74 68  WID.  Compare th
1a18c 69 73 20 6b 65 79 20 76 61 6c 75 65 20 61 67 61  is key value aga
1a18d 69 6e 73 74 20 74 68 65 20 69 6e 64 65 78 20 0a  inst the index .
1a18e 2a 2a 20 74 68 61 74 20 50 31 20 69 73 20 63 75  ** that P1 is cu
1a18f 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67  rrently pointing
1a190 20 74 6f 2c 20 69 67 6e 6f 72 69 6e 67 20 74 68   to, ignoring th
1a191 65 20 52 4f 57 49 44 20 6f 6e 20 74 68 65 20 50  e ROWID on the P
1a192 31 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49  1 index..**.** I
1a193 66 20 74 68 65 20 50 31 20 69 6e 64 65 78 20 65  f the P1 index e
1a194 6e 74 72 79 20 69 73 20 6c 65 73 73 20 74 68 61  ntry is less tha
1a195 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 20  n the key value 
1a196 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e  then jump to P2.
1a197 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 66 61  .** Otherwise fa
1a198 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68  ll through to th
1a199 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69  e next instructi
1a19a 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20  on..**.** If P5 
1a19b 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e  is non-zero then
1a19c 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 20 69   the key value i
1a19d 73 20 69 6e 63 72 65 61 73 65 64 20 62 79 20 61  s increased by a
1a19e 6e 20 65 70 73 69 6c 6f 6e 20 70 72 69 6f 72 20  n epsilon prior 
1a19f 0a 2a 2a 20 74 6f 20 74 68 65 20 63 6f 6d 70 61  .** to the compa
1a1a0 72 69 73 6f 6e 2e 20 20 54 68 69 73 20 6d 61 6b  rison.  This mak
1a1a1 65 73 20 74 68 65 20 6f 70 63 6f 64 65 20 77 6f  es the opcode wo
1a1a2 72 6b 20 6c 69 6b 65 20 49 64 78 4c 45 2e 0a 2a  rk like IdxLE..*
1a1a3 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 4c 54 3a  /.case OP_IdxLT:
1a1a4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d            /* jum
1a1a5 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f  p, in3 */.case O
1a1a6 50 5f 49 64 78 47 45 3a 20 7b 20 20 20 20 20 20  P_IdxGE: {      
1a1a7 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a    /* jump, in3 *
1a1a8 2f 0a 20 20 69 6e 74 20 69 3d 20 70 4f 70 2d 3e  /.  int i= pOp->
1a1a9 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72  p1;.  VdbeCursor
1a1aa 20 2a 70 43 3b 0a 0a 20 20 61 73 73 65 72 74 28   *pC;..  assert(
1a1ab 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43   i>=0 && i<p->nC
1a1ac 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72  ursor );.  asser
1a1ad 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 21 3d  t( p->apCsr[i]!=
1a1ae 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43 20 3d  0 );.  if( (pC =
1a1af 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 2d 3e 70   p->apCsr[i])->p
1a1b0 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a 20 20 20  Cursor!=0 ){.   
1a1b1 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 55 6e   int res;.    Un
1a1b2 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a  packedRecord r;.
1a1b3 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e      assert( pC->
1a1b4 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d  deferredMoveto==
1a1b5 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
1a1b6 20 70 4f 70 2d 3e 70 35 3d 3d 30 20 7c 7c 20 70   pOp->p5==0 || p
1a1b7 4f 70 2d 3e 70 35 3d 3d 31 20 29 3b 0a 20 20 20  Op->p5==1 );.   
1a1b8 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34   assert( pOp->p4
1a1b9 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29  type==P4_INT32 )
1a1ba 3b 0a 20 20 20 20 72 2e 70 4b 65 79 49 6e 66 6f  ;.    r.pKeyInfo
1a1bb 20 3d 20 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 3b   = pC->pKeyInfo;
1a1bc 0a 20 20 20 20 72 2e 6e 46 69 65 6c 64 20 3d 20  .    r.nField = 
1a1bd 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 69  pOp->p4.i;.    i
1a1be 66 28 20 70 4f 70 2d 3e 70 35 20 29 7b 0a 20 20  f( pOp->p5 ){.  
1a1bf 20 20 20 20 72 2e 66 6c 61 67 73 20 3d 20 55 4e      r.flags = UN
1a1c0 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 7c  PACKED_INCRKEY |
1a1c1 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45   UNPACKED_IGNORE
1a1c2 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d 65 6c 73  _ROWID;.    }els
1a1c3 65 7b 0a 20 20 20 20 20 20 72 2e 66 6c 61 67 73  e{.      r.flags
1a1c4 20 3d 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f   = UNPACKED_IGNO
1a1c5 52 45 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d 0a  RE_ROWID;.    }.
1a1c6 20 20 20 20 72 2e 61 4d 65 6d 20 3d 20 26 70 2d      r.aMem = &p-
1a1c7 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  >aMem[pOp->p3];.
1a1c8 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1a1c9 56 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 72  VdbeIdxKeyCompar
1a1ca 65 28 70 43 2c 20 26 72 2c 20 26 72 65 73 29 3b  e(pC, &r, &res);
1a1cb 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70  .    if( pOp->op
1a1cc 63 6f 64 65 3d 3d 4f 50 5f 49 64 78 4c 54 20 29  code==OP_IdxLT )
1a1cd 7b 0a 20 20 20 20 20 20 72 65 73 20 3d 20 2d 72  {.      res = -r
1a1ce 65 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  es;.    }else{. 
1a1cf 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
1a1d0 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 64 78  ->opcode==OP_Idx
1a1d1 47 45 20 29 3b 0a 20 20 20 20 20 20 72 65 73 2b  GE );.      res+
1a1d2 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  +;.    }.    if(
1a1d3 20 72 65 73 3e 30 20 29 7b 0a 20 20 20 20 20 20   res>0 ){.      
1a1d4 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
1a1d5 20 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62   ;.    }.  }.  b
1a1d6 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1a1d7 64 65 3a 20 44 65 73 74 72 6f 79 20 50 31 20 50  de: Destroy P1 P
1a1d8 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44  2 P3 * *.**.** D
1a1d9 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20  elete an entire 
1a1da 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f  database table o
1a1db 72 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f  r index whose ro
1a1dc 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 64  ot page in the d
1a1dd 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20  atabase.** file 
1a1de 69 73 20 67 69 76 65 6e 20 62 79 20 50 31 2e 0a  is given by P1..
1a1df 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20  **.** The table 
1a1e0 62 65 69 6e 67 20 64 65 73 74 72 6f 79 65 64 20  being destroyed 
1a1e1 69 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64  is in the main d
1a1e2 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20  atabase file if 
1a1e3 50 33 3d 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 33  P3==0.  If.** P3
1a1e4 3d 3d 31 20 74 68 65 6e 20 74 68 65 20 74 61 62  ==1 then the tab
1a1e5 6c 65 20 74 6f 20 62 65 20 63 6c 65 61 72 20 69  le to be clear i
1a1e6 73 20 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61  s in the auxilia
1a1e7 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ry database file
1a1e8 0a 2a 2a 20 74 68 61 74 20 69 73 20 75 73 65 64  .** that is used
1a1e9 20 74 6f 20 73 74 6f 72 65 20 74 61 62 6c 65 73   to store tables
1a1ea 20 63 72 65 61 74 65 20 75 73 69 6e 67 20 43 52   create using CR
1a1eb 45 41 54 45 20 54 45 4d 50 4f 52 41 52 59 20 54  EATE TEMPORARY T
1a1ec 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41  ABLE..**.** If A
1a1ed 55 54 4f 56 41 43 55 55 4d 20 69 73 20 65 6e 61  UTOVACUUM is ena
1a1ee 62 6c 65 64 20 74 68 65 6e 20 69 74 20 69 73 20  bled then it is 
1a1ef 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e  possible that an
1a1f0 6f 74 68 65 72 20 72 6f 6f 74 20 70 61 67 65 0a  other root page.
1a1f1 2a 2a 20 6d 69 67 68 74 20 62 65 20 6d 6f 76 65  ** might be move
1a1f2 64 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 6c 79  d into the newly
1a1f3 20 64 65 6c 65 74 65 64 20 72 6f 6f 74 20 70 61   deleted root pa
1a1f4 67 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b  ge in order to k
1a1f5 65 65 70 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20  eep all.** root 
1a1f6 70 61 67 65 73 20 63 6f 6e 74 69 67 75 6f 75 73  pages contiguous
1a1f7 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   at the beginnin
1a1f8 67 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  g of the databas
1a1f9 65 2e 20 20 54 68 65 20 66 6f 72 6d 65 72 0a 2a  e.  The former.*
1a1fa 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72  * value of the r
1a1fb 6f 6f 74 20 70 61 67 65 20 74 68 61 74 20 6d 6f  oot page that mo
1a1fc 76 65 64 20 2d 20 69 74 73 20 76 61 6c 75 65 20  ved - its value 
1a1fd 62 65 66 6f 72 65 20 74 68 65 20 6d 6f 76 65 20  before the move 
1a1fe 6f 63 63 75 72 72 65 64 20 2d 0a 2a 2a 20 69 73  occurred -.** is
1a1ff 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
1a200 74 65 72 20 50 32 2e 20 20 49 66 20 6e 6f 20 70  ter P2.  If no p
1a201 61 67 65 20 0a 2a 2a 20 6d 6f 76 65 6d 65 6e 74  age .** movement
1a202 20 77 61 73 20 72 65 71 75 69 72 65 64 20 28 62   was required (b
1a203 65 63 61 75 73 65 20 74 68 65 20 74 61 62 6c 65  ecause the table
1a204 20 62 65 69 6e 67 20 64 72 6f 70 70 65 64 20 77   being dropped w
1a205 61 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a 20 74  as already .** t
1a206 68 65 20 6c 61 73 74 20 6f 6e 65 20 69 6e 20 74  he last one in t
1a207 68 65 20 64 61 74 61 62 61 73 65 29 20 74 68 65  he database) the
1a208 6e 20 61 20 7a 65 72 6f 20 69 73 20 73 74 6f 72  n a zero is stor
1a209 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ed in register P
1a20a 32 2e 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41 43  2..** If AUTOVAC
1a20b 55 55 4d 20 69 73 20 64 69 73 61 62 6c 65 64 20  UUM is disabled 
1a20c 74 68 65 6e 20 61 20 7a 65 72 6f 20 69 73 20 73  then a zero is s
1a20d 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65  tored in registe
1a20e 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  r P2..**.** See 
1a20f 61 6c 73 6f 3a 20 43 6c 65 61 72 0a 2a 2f 0a 63  also: Clear.*/.c
1a210 61 73 65 20 4f 50 5f 44 65 73 74 72 6f 79 3a 20  ase OP_Destroy: 
1a211 7b 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72  {     /* out2-pr
1a212 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e  erelease */.  in
1a213 74 20 69 4d 6f 76 65 64 3b 0a 20 20 69 6e 74 20  t iMoved;.  int 
1a214 69 43 6e 74 3b 0a 23 69 66 6e 64 65 66 20 53 51  iCnt;.#ifndef SQ
1a215 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1a216 4c 54 41 42 4c 45 0a 20 20 56 64 62 65 20 2a 70  LTABLE.  Vdbe *p
1a217 56 64 62 65 3b 0a 20 20 69 43 6e 74 20 3d 20 30  Vdbe;.  iCnt = 0
1a218 3b 0a 20 20 66 6f 72 28 70 56 64 62 65 3d 64 62  ;.  for(pVdbe=db
1a219 2d 3e 70 56 64 62 65 3b 20 70 56 64 62 65 3b 20  ->pVdbe; pVdbe; 
1a21a 70 56 64 62 65 3d 70 56 64 62 65 2d 3e 70 4e 65  pVdbe=pVdbe->pNe
1a21b 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 56 64  xt){.    if( pVd
1a21c 62 65 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f  be->magic==VDBE_
1a21d 4d 41 47 49 43 5f 52 55 4e 20 26 26 20 70 56 64  MAGIC_RUN && pVd
1a21e 62 65 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64  be->inVtabMethod
1a21f 3c 32 20 26 26 20 70 56 64 62 65 2d 3e 70 63 3e  <2 && pVdbe->pc>
1a220 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 43 6e 74  =0 ){.      iCnt
1a221 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  ++;.    }.  }.#e
1a222 6c 73 65 0a 20 20 69 43 6e 74 20 3d 20 64 62 2d  lse.  iCnt = db-
1a223 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3b 0a  >activeVdbeCnt;.
1a224 23 65 6e 64 69 66 0a 20 20 69 66 28 20 69 43 6e  #endif.  if( iCn
1a225 74 3e 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  t>1 ){.    rc = 
1a226 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20  SQLITE_LOCKED;. 
1a227 20 20 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f     p->errorActio
1a228 6e 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20  n = OE_Abort;.  
1a229 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 69  }else{.    int i
1a22a 44 62 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20  Db = pOp->p3;.  
1a22b 20 20 61 73 73 65 72 74 28 20 69 43 6e 74 3d 3d    assert( iCnt==
1a22c 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  1 );.    assert(
1a22d 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26   (p->btreeMask &
1a22e 20 28 31 3c 3c 69 44 62 29 29 21 3d 30 20 29 3b   (1<<iDb))!=0 );
1a22f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1a230 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28  3BtreeDropTable(
1a231 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74  db->aDb[iDb].pBt
1a232 2c 20 70 4f 70 2d 3e 70 31 2c 20 26 69 4d 6f 76  , pOp->p1, &iMov
1a233 65 64 29 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54  ed);.    MemSetT
1a234 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45  ypeFlag(pOut, ME
1a235 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 70 4f 75 74  M_Int);.    pOut
1a236 2d 3e 75 2e 69 20 3d 20 69 4d 6f 76 65 64 3b 0a  ->u.i = iMoved;.
1a237 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1a238 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
1a239 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
1a23a 45 5f 4f 4b 20 26 26 20 69 4d 6f 76 65 64 21 3d  E_OK && iMoved!=
1a23b 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
1a23c 65 33 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28  e3RootPageMoved(
1a23d 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2c 20 69  &db->aDb[iDb], i
1a23e 4d 6f 76 65 64 2c 20 70 4f 70 2d 3e 70 31 29 3b  Moved, pOp->p1);
1a23f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
1a240 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1a241 20 4f 70 63 6f 64 65 3a 20 43 6c 65 61 72 20 50   Opcode: Clear P
1a242 31 20 50 32 20 50 33 0a 2a 2a 0a 2a 2a 20 44 65  1 P2 P3.**.** De
1a243 6c 65 74 65 20 61 6c 6c 20 63 6f 6e 74 65 6e 74  lete all content
1a244 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  s of the databas
1a245 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  e table or index
1a246 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 65   whose root page
1a247 0a 2a 2a 20 69 6e 20 74 68 65 20 64 61 74 61 62  .** in the datab
1a248 61 73 65 20 66 69 6c 65 20 69 73 20 67 69 76 65  ase file is give
1a249 6e 20 62 79 20 50 31 2e 20 20 42 75 74 2c 20 75  n by P1.  But, u
1a24a 6e 6c 69 6b 65 20 44 65 73 74 72 6f 79 2c 20 64  nlike Destroy, d
1a24b 6f 20 6e 6f 74 0a 2a 2a 20 72 65 6d 6f 76 65 20  o not.** remove 
1a24c 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  the table or ind
1a24d 65 78 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  ex from the data
1a24e 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  base file..**.**
1a24f 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   The table being
1a250 20 63 6c 65 61 72 20 69 73 20 69 6e 20 74 68 65   clear is in the
1a251 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66   main database f
1a252 69 6c 65 20 69 66 20 50 32 3d 3d 30 2e 20 20 49  ile if P2==0.  I
1a253 66 0a 2a 2a 20 50 32 3d 3d 31 20 74 68 65 6e 20  f.** P2==1 then 
1a254 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20  the table to be 
1a255 63 6c 65 61 72 20 69 73 20 69 6e 20 74 68 65 20  clear is in the 
1a256 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61  auxiliary databa
1a257 73 65 20 66 69 6c 65 0a 2a 2a 20 74 68 61 74 20  se file.** that 
1a258 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  is used to store
1a259 20 74 61 62 6c 65 73 20 63 72 65 61 74 65 20 75   tables create u
1a25a 73 69 6e 67 20 43 52 45 41 54 45 20 54 45 4d 50  sing CREATE TEMP
1a25b 4f 52 41 52 59 20 54 41 42 4c 45 2e 0a 2a 2a 0a  ORARY TABLE..**.
1a25c 2a 2a 20 49 66 20 74 68 65 20 50 33 20 76 61 6c  ** If the P3 val
1a25d 75 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  ue is non-zero, 
1a25e 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 72  then the table r
1a25f 65 66 65 72 65 64 20 74 6f 20 6d 75 73 74 20 62  efered to must b
1a260 65 20 61 6e 0a 2a 2a 20 69 6e 74 6b 65 79 20 74  e an.** intkey t
1a261 61 62 6c 65 20 28 61 6e 20 53 51 4c 20 74 61 62  able (an SQL tab
1a262 6c 65 2c 20 6e 6f 74 20 61 6e 20 69 6e 64 65 78  le, not an index
1a263 29 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  ). In this case 
1a264 74 68 65 20 72 6f 77 20 63 68 61 6e 67 65 20 0a  the row change .
1a265 2a 2a 20 63 6f 75 6e 74 20 69 73 20 69 6e 63 72  ** count is incr
1a266 65 6d 65 6e 74 65 64 20 62 79 20 74 68 65 20 6e  emented by the n
1a267 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e  umber of rows in
1a268 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
1a269 20 63 6c 65 61 72 65 64 2e 20 0a 2a 2a 20 49 66   cleared. .** If
1a26a 20 50 33 20 69 73 20 67 72 65 61 74 65 72 20 74   P3 is greater t
1a26b 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74  han zero, then t
1a26c 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20  he value stored 
1a26d 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 69  in register P3 i
1a26e 73 0a 2a 2a 20 61 6c 73 6f 20 69 6e 63 72 65 6d  s.** also increm
1a26f 65 6e 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d  ented by the num
1a270 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74  ber of rows in t
1a271 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63  he table being c
1a272 6c 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65  leared..**.** Se
1a273 65 20 61 6c 73 6f 3a 20 44 65 73 74 72 6f 79 0a  e also: Destroy.
1a274 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6c 65 61 72  */.case OP_Clear
1a275 3a 20 7b 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67  : {.  int nChang
1a276 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28  e = 0;.  assert(
1a277 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26   (p->btreeMask &
1a278 20 28 31 3c 3c 70 4f 70 2d 3e 70 32 29 29 21 3d   (1<<pOp->p2))!=
1a279 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  0 );.  rc = sqli
1a27a 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62  te3BtreeClearTab
1a27b 6c 65 28 0a 20 20 20 20 20 20 64 62 2d 3e 61 44  le(.      db->aD
1a27c 62 5b 70 4f 70 2d 3e 70 32 5d 2e 70 42 74 2c 20  b[pOp->p2].pBt, 
1a27d 70 4f 70 2d 3e 70 31 2c 20 28 70 4f 70 2d 3e 70  pOp->p1, (pOp->p
1a27e 33 20 3f 20 26 6e 43 68 61 6e 67 65 20 3a 20 30  3 ? &nChange : 0
1a27f 29 0a 20 20 29 3b 0a 20 20 69 66 28 20 70 4f 70  ).  );.  if( pOp
1a280 2d 3e 70 33 20 29 7b 0a 20 20 20 20 70 2d 3e 6e  ->p3 ){.    p->n
1a281 43 68 61 6e 67 65 20 2b 3d 20 6e 43 68 61 6e 67  Change += nChang
1a282 65 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  e;.    if( pOp->
1a283 70 33 3e 30 20 29 7b 0a 20 20 20 20 20 20 70 2d  p3>0 ){.      p-
1a284 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 2e 75  >aMem[pOp->p3].u
1a285 2e 69 20 2b 3d 20 6e 43 68 61 6e 67 65 3b 0a 20  .i += nChange;. 
1a286 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b     }.  }.  break
1a287 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1a288 43 72 65 61 74 65 54 61 62 6c 65 20 50 31 20 50  CreateTable P1 P
1a289 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6c  2 * * *.**.** Al
1a28a 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 74 61 62  locate a new tab
1a28b 6c 65 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64  le in the main d
1a28c 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20  atabase file if 
1a28d 50 31 3d 3d 30 20 6f 72 20 69 6e 20 74 68 65 0a  P1==0 or in the.
1a28e 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64 61 74  ** auxiliary dat
1a28f 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 31  abase file if P1
1a290 3d 3d 31 20 6f 72 20 69 6e 20 61 6e 20 61 74 74  ==1 or in an att
1a291 61 63 68 65 64 20 64 61 74 61 62 61 73 65 20 69  ached database i
1a292 66 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 72 69 74  f.** P1>1.  Writ
1a293 65 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  e the root page 
1a294 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65  number of the ne
1a295 77 20 74 61 62 6c 65 20 69 6e 74 6f 0a 2a 2a 20  w table into.** 
1a296 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 0a 2a  register P2.**.*
1a297 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65  * The difference
1a298 20 62 65 74 77 65 65 6e 20 61 20 74 61 62 6c 65   between a table
1a299 20 61 6e 64 20 61 6e 20 69 6e 64 65 78 20 69 73   and an index is
1a29a 20 74 68 69 73 3a 20 20 41 20 74 61 62 6c 65 20   this:  A table 
1a29b 6d 75 73 74 0a 2a 2a 20 68 61 76 65 20 61 20 34  must.** have a 4
1a29c 2d 62 79 74 65 20 69 6e 74 65 67 65 72 20 6b 65  -byte integer ke
1a29d 79 20 61 6e 64 20 63 61 6e 20 68 61 76 65 20 61  y and can have a
1a29e 72 62 69 74 72 61 72 79 20 64 61 74 61 2e 20 20  rbitrary data.  
1a29f 41 6e 20 69 6e 64 65 78 0a 2a 2a 20 68 61 73 20  An index.** has 
1a2a0 61 6e 20 61 72 62 69 74 72 61 72 79 20 6b 65 79  an arbitrary key
1a2a1 20 62 75 74 20 6e 6f 20 64 61 74 61 2e 0a 2a 2a   but no data..**
1a2a2 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 43 72  .** See also: Cr
1a2a3 65 61 74 65 49 6e 64 65 78 0a 2a 2f 0a 2f 2a 20  eateIndex.*/./* 
1a2a4 4f 70 63 6f 64 65 3a 20 43 72 65 61 74 65 49 6e  Opcode: CreateIn
1a2a5 64 65 78 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  dex P1 P2 * * *.
1a2a6 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61  **.** Allocate a
1a2a7 20 6e 65 77 20 69 6e 64 65 78 20 69 6e 20 74 68   new index in th
1a2a8 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
1a2a9 66 69 6c 65 20 69 66 20 50 31 3d 3d 30 20 6f 72  file if P1==0 or
1a2aa 20 69 6e 20 74 68 65 0a 2a 2a 20 61 75 78 69 6c   in the.** auxil
1a2ab 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66 69  iary database fi
1a2ac 6c 65 20 69 66 20 50 31 3d 3d 31 20 6f 72 20 69  le if P1==1 or i
1a2ad 6e 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61  n an attached da
1a2ae 74 61 62 61 73 65 20 69 66 0a 2a 2a 20 50 31 3e  tabase if.** P1>
1a2af 31 2e 20 20 57 72 69 74 65 20 74 68 65 20 72 6f  1.  Write the ro
1a2b0 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  ot page number o
1a2b1 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20  f the new table 
1a2b2 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72  into.** register
1a2b3 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 64   P2..**.** See d
1a2b4 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 6e 20  ocumentation on 
1a2b5 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 66  OP_CreateTable f
1a2b6 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
1a2b7 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61  formation..*/.ca
1a2b8 73 65 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65  se OP_CreateInde
1a2b9 78 3a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  x:            /*
1a2ba 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65   out2-prerelease
1a2bb 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 72 65 61   */.case OP_Crea
1a2bc 74 65 54 61 62 6c 65 3a 20 7b 20 20 20 20 20 20  teTable: {      
1a2bd 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72      /* out2-prer
1a2be 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20  elease */.  int 
1a2bf 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 66 6c 61 67  pgno;.  int flag
1a2c0 73 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20 20  s;.  Db *pDb;.  
1a2c1 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1a2c2 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62  =0 && pOp->p1<db
1a2c3 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72  ->nDb );.  asser
1a2c4 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b  t( (p->btreeMask
1a2c5 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29   & (1<<pOp->p1))
1a2c6 21 3d 30 20 29 3b 0a 20 20 70 44 62 20 3d 20 26  !=0 );.  pDb = &
1a2c7 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d  db->aDb[pOp->p1]
1a2c8 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 62 2d  ;.  assert( pDb-
1a2c9 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 69 66 28  >pBt!=0 );.  if(
1a2ca 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
1a2cb 5f 43 72 65 61 74 65 54 61 62 6c 65 20 29 7b 0a  _CreateTable ){.
1a2cc 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 3d 20 42      /* flags = B
1a2cd 54 52 45 45 5f 49 4e 54 4b 45 59 3b 20 2a 2f 0a  TREE_INTKEY; */.
1a2ce 20 20 20 20 66 6c 61 67 73 20 3d 20 42 54 52 45      flags = BTRE
1a2cf 45 5f 4c 45 41 46 44 41 54 41 7c 42 54 52 45 45  E_LEAFDATA|BTREE
1a2d0 5f 49 4e 54 4b 45 59 3b 0a 20 20 7d 65 6c 73 65  _INTKEY;.  }else
1a2d1 7b 0a 20 20 20 20 66 6c 61 67 73 20 3d 20 42 54  {.    flags = BT
1a2d2 52 45 45 5f 5a 45 52 4f 44 41 54 41 3b 0a 20 20  REE_ZERODATA;.  
1a2d3 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  }.  rc = sqlite3
1a2d4 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65  BtreeCreateTable
1a2d5 28 70 44 62 2d 3e 70 42 74 2c 20 26 70 67 6e 6f  (pDb->pBt, &pgno
1a2d6 2c 20 66 6c 61 67 73 29 3b 0a 20 20 69 66 28 20  , flags);.  if( 
1a2d7 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1a2d8 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d  .    pOut->u.i =
1a2d9 20 70 67 6e 6f 3b 0a 20 20 20 20 4d 65 6d 53 65   pgno;.    MemSe
1a2da 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20  tTypeFlag(pOut, 
1a2db 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 0a 20 20  MEM_Int);.  }.  
1a2dc 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1a2dd 6f 64 65 3a 20 50 61 72 73 65 53 63 68 65 6d 61  ode: ParseSchema
1a2de 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a   P1 P2 * P4 *.**
1a2df 0a 2a 2a 20 52 65 61 64 20 61 6e 64 20 70 61 72  .** Read and par
1a2e0 73 65 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 66  se all entries f
1a2e1 72 6f 6d 20 74 68 65 20 53 51 4c 49 54 45 5f 4d  rom the SQLITE_M
1a2e2 41 53 54 45 52 20 74 61 62 6c 65 20 6f 66 20 64  ASTER table of d
1a2e3 61 74 61 62 61 73 65 20 50 31 0a 2a 2a 20 74 68  atabase P1.** th
1a2e4 61 74 20 6d 61 74 63 68 20 74 68 65 20 57 48 45  at match the WHE
1a2e5 52 45 20 63 6c 61 75 73 65 20 50 34 2e 20 20 50  RE clause P4.  P
1a2e6 32 20 69 73 20 74 68 65 20 22 66 6f 72 63 65 22  2 is the "force"
1a2e7 20 66 6c 61 67 2e 20 20 20 41 6c 77 61 79 73 20   flag.   Always 
1a2e8 64 6f 0a 2a 2a 20 74 68 65 20 70 61 72 73 69 6e  do.** the parsin
1a2e9 67 20 69 66 20 50 32 20 69 73 20 74 72 75 65 2e  g if P2 is true.
1a2ea 20 20 49 66 20 50 32 20 69 73 20 66 61 6c 73 65    If P2 is false
1a2eb 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74  , then this rout
1a2ec 69 6e 65 20 69 73 20 61 0a 2a 2a 20 6e 6f 2d 6f  ine is a.** no-o
1a2ed 70 20 69 66 20 74 68 65 20 73 63 68 65 6d 61 20  p if the schema 
1a2ee 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79  is not currently
1a2ef 20 6c 6f 61 64 65 64 2e 20 20 49 6e 20 6f 74 68   loaded.  In oth
1a2f0 65 72 20 77 6f 72 64 73 2c 20 69 66 20 50 32 0a  er words, if P2.
1a2f1 2a 2a 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65  ** is false, the
1a2f2 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74   SQLITE_MASTER t
1a2f3 61 62 6c 65 20 69 73 20 6f 6e 6c 79 20 70 61 72  able is only par
1a2f4 73 65 64 20 69 66 20 74 68 65 20 72 65 73 74 20  sed if the rest 
1a2f5 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61  of the.** schema
1a2f6 20 69 73 20 61 6c 72 65 61 64 79 20 6c 6f 61 64   is already load
1a2f7 65 64 20 69 6e 74 6f 20 74 68 65 20 73 79 6d 62  ed into the symb
1a2f8 6f 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  ol table..**.** 
1a2f9 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f  This opcode invo
1a2fa 6b 65 73 20 74 68 65 20 70 61 72 73 65 72 20 74  kes the parser t
1a2fb 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 76  o create a new v
1a2fc 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2c 0a  irtual machine,.
1a2fd 2a 2a 20 74 68 65 6e 20 72 75 6e 73 20 74 68 65  ** then runs the
1a2fe 20 6e 65 77 20 76 69 72 74 75 61 6c 20 6d 61 63   new virtual mac
1a2ff 68 69 6e 65 2e 20 20 49 74 20 69 73 20 74 68 75  hine.  It is thu
1a300 73 20 61 20 72 65 2d 65 6e 74 72 61 6e 74 20 6f  s a re-entrant o
1a301 70 63 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f  pcode..*/.case O
1a302 50 5f 50 61 72 73 65 53 63 68 65 6d 61 3a 20 7b  P_ParseSchema: {
1a303 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20  .  char *zSql;. 
1a304 20 69 6e 74 20 69 44 62 20 3d 20 70 4f 70 2d 3e   int iDb = pOp->
1a305 70 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  p1;.  const char
1a306 20 2a 7a 4d 61 73 74 65 72 3b 0a 20 20 49 6e 69   *zMaster;.  Ini
1a307 74 44 61 74 61 20 69 6e 69 74 44 61 74 61 3b 0a  tData initData;.
1a308 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d  .  assert( iDb>=
1a309 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62  0 && iDb<db->nDb
1a30a 20 29 3b 0a 20 20 69 66 28 20 21 70 4f 70 2d 3e   );.  if( !pOp->
1a30b 70 32 20 26 26 20 21 44 62 48 61 73 50 72 6f 70  p2 && !DbHasProp
1a30c 65 72 74 79 28 64 62 2c 20 69 44 62 2c 20 44 42  erty(db, iDb, DB
1a30d 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 20 29  _SchemaLoaded) )
1a30e 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d  {.    break;.  }
1a30f 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 53 43 48  .  zMaster = SCH
1a310 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a  EMA_TABLE(iDb);.
1a311 20 20 69 6e 69 74 44 61 74 61 2e 64 62 20 3d 20    initData.db = 
1a312 64 62 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 69  db;.  initData.i
1a313 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20  Db = pOp->p1;.  
1a314 69 6e 69 74 44 61 74 61 2e 70 7a 45 72 72 4d 73  initData.pzErrMs
1a315 67 20 3d 20 26 70 2d 3e 7a 45 72 72 4d 73 67 3b  g = &p->zErrMsg;
1a316 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65  .  zSql = sqlite
1a317 33 4d 50 72 69 6e 74 66 28 64 62 2c 0a 20 20 20  3MPrintf(db,.   
1a318 20 20 22 53 45 4c 45 43 54 20 6e 61 6d 65 2c 20    "SELECT name, 
1a319 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c 20 46 52  rootpage, sql FR
1a31a 4f 4d 20 27 25 71 27 2e 25 73 20 57 48 45 52 45  OM '%q'.%s WHERE
1a31b 20 25 73 22 2c 0a 20 20 20 20 20 64 62 2d 3e 61   %s",.     db->a
1a31c 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a  Db[iDb].zName, z
1a31d 4d 61 73 74 65 72 2c 20 70 4f 70 2d 3e 70 34 2e  Master, pOp->p4.
1a31e 7a 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 3d 3d  z);.  if( zSql==
1a31f 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b  0 ) goto no_mem;
1a320 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33  .  (void)sqlite3
1a321 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20  SafetyOff(db);. 
1a322 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69   assert( db->ini
1a323 74 2e 62 75 73 79 3d 3d 30 20 29 3b 0a 20 20 64  t.busy==0 );.  d
1a324 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 31  b->init.busy = 1
1a325 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 72 63 20  ;.  initData.rc 
1a326 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61  = SQLITE_OK;.  a
1a327 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c  ssert( !db->mall
1a328 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 72 63  ocFailed );.  rc
1a329 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28   = sqlite3_exec(
1a32a 64 62 2c 20 7a 53 71 6c 2c 20 73 71 6c 69 74 65  db, zSql, sqlite
1a32b 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2c 20 26  3InitCallback, &
1a32c 69 6e 69 74 44 61 74 61 2c 20 30 29 3b 0a 20 20  initData, 0);.  
1a32d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1a32e 4b 20 29 20 72 63 20 3d 20 69 6e 69 74 44 61 74  K ) rc = initDat
1a32f 61 2e 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 44  a.rc;.  sqlite3D
1a330 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c 29 3b  bFree(db, zSql);
1a331 0a 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  .  db->init.busy
1a332 20 3d 20 30 3b 0a 20 20 28 76 6f 69 64 29 73 71   = 0;.  (void)sq
1a333 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
1a334 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
1a335 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20  ITE_NOMEM ){.   
1a336 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20   goto no_mem;.  
1a337 7d 0a 20 20 62 72 65 61 6b 3b 20 20 0a 7d 0a 0a  }.  break;  .}..
1a338 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
1a339 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45  ITE_OMIT_ANALYZE
1a33a 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
1a33b 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53 45 52  LITE_OMIT_PARSER
1a33c 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 6f 61  )./* Opcode: Loa
1a33d 64 41 6e 61 6c 79 73 69 73 20 50 31 20 2a 20 2a  dAnalysis P1 * *
1a33e 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20   * *.**.** Read 
1a33f 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31  the sqlite_stat1
1a340 20 74 61 62 6c 65 20 66 6f 72 20 64 61 74 61 62   table for datab
1a341 61 73 65 20 50 31 20 61 6e 64 20 6c 6f 61 64 20  ase P1 and load 
1a342 74 68 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 6f  the content.** o
1a343 66 20 74 68 61 74 20 74 61 62 6c 65 20 69 6e 74  f that table int
1a344 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 69  o the internal i
1a345 6e 64 65 78 20 68 61 73 68 20 74 61 62 6c 65 2e  ndex hash table.
1a346 20 20 54 68 69 73 20 77 69 6c 6c 20 63 61 75 73    This will caus
1a347 65 0a 2a 2a 20 74 68 65 20 61 6e 61 6c 79 73 69  e.** the analysi
1a348 73 20 74 6f 20 62 65 20 75 73 65 64 20 77 68 65  s to be used whe
1a349 6e 20 70 72 65 70 61 72 69 6e 67 20 61 6c 6c 20  n preparing all 
1a34a 73 75 62 73 65 71 75 65 6e 74 20 71 75 65 72 69  subsequent queri
1a34b 65 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c  es..*/.case OP_L
1a34c 6f 61 64 41 6e 61 6c 79 73 69 73 3a 20 7b 0a 20  oadAnalysis: {. 
1a34d 20 69 6e 74 20 69 44 62 20 3d 20 70 4f 70 2d 3e   int iDb = pOp->
1a34e 70 31 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44  p1;.  assert( iD
1a34f 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e  b>=0 && iDb<db->
1a350 6e 44 62 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  nDb );.  rc = sq
1a351 6c 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61  lite3AnalysisLoa
1a352 64 28 64 62 2c 20 69 44 62 29 3b 0a 20 20 62 72  d(db, iDb);.  br
1a353 65 61 6b 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20  eak;  .}.#endif 
1a354 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
1a355 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 29  TE_OMIT_ANALYZE)
1a356 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c   && !defined(SQL
1a357 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53 45 52 29  ITE_OMIT_PARSER)
1a358 20 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a    */../* Opcode:
1a359 20 44 72 6f 70 54 61 62 6c 65 20 50 31 20 2a 20   DropTable P1 * 
1a35a 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d  * P4 *.**.** Rem
1a35b 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  ove the internal
1a35c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74   (in-memory) dat
1a35d 61 20 73 74 72 75 63 74 75 72 65 73 20 74 68 61  a structures tha
1a35e 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20 74 68  t describe.** th
1a35f 65 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 50 34  e table named P4
1a360 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e   in database P1.
1a361 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64    This is called
1a362 20 61 66 74 65 72 20 61 20 74 61 62 6c 65 0a 2a   after a table.*
1a363 2a 20 69 73 20 64 72 6f 70 70 65 64 20 69 6e 20  * is dropped in 
1a364 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74 68  order to keep th
1a365 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65  e internal repre
1a366 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  sentation of the
1a367 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e 73 69  .** schema consi
1a368 73 74 65 6e 74 20 77 69 74 68 20 77 68 61 74 20  stent with what 
1a369 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63  is on disk..*/.c
1a36a 61 73 65 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65  ase OP_DropTable
1a36b 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c  : {.  sqlite3Unl
1a36c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61 62 6c  inkAndDeleteTabl
1a36d 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70  e(db, pOp->p1, p
1a36e 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65  Op->p4.z);.  bre
1a36f 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1a370 3a 20 44 72 6f 70 49 6e 64 65 78 20 50 31 20 2a  : DropIndex P1 *
1a371 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65   * P4 *.**.** Re
1a372 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e 61  move the interna
1a373 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61  l (in-memory) da
1a374 74 61 20 73 74 72 75 63 74 75 72 65 73 20 74 68  ta structures th
1a375 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20 74  at describe.** t
1a376 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 64 20 50  he index named P
1a377 34 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 31  4 in database P1
1a378 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65  .  This is calle
1a379 64 20 61 66 74 65 72 20 61 6e 20 69 6e 64 65 78  d after an index
1a37a 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20 69  .** is dropped i
1a37b 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20  n order to keep 
1a37c 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70  the internal rep
1a37d 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  resentation of t
1a37e 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e  he.** schema con
1a37f 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 68 61  sistent with wha
1a380 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f  t is on disk..*/
1a381 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 49 6e 64  .case OP_DropInd
1a382 65 78 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 55  ex: {.  sqlite3U
1a383 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e  nlinkAndDeleteIn
1a384 64 65 78 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c  dex(db, pOp->p1,
1a385 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62   pOp->p4.z);.  b
1a386 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1a387 64 65 3a 20 44 72 6f 70 54 72 69 67 67 65 72 20  de: DropTrigger 
1a388 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a  P1 * * P4 *.**.*
1a389 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 69 6e 74  * Remove the int
1a38a 65 72 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79  ernal (in-memory
1a38b 29 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65  ) data structure
1a38c 73 20 74 68 61 74 20 64 65 73 63 72 69 62 65 0a  s that describe.
1a38d 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72 20 6e  ** the trigger n
1a38e 61 6d 65 64 20 50 34 20 69 6e 20 64 61 74 61 62  amed P4 in datab
1a38f 61 73 65 20 50 31 2e 20 20 54 68 69 73 20 69 73  ase P1.  This is
1a390 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61 20   called after a 
1a391 74 72 69 67 67 65 72 0a 2a 2a 20 69 73 20 64 72  trigger.** is dr
1a392 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72 20 74  opped in order t
1a393 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74 65 72  o keep the inter
1a394 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69  nal representati
1a395 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68  on of the.** sch
1a396 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74 20 77  ema consistent w
1a397 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e 20 64  ith what is on d
1a398 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  isk..*/.case OP_
1a399 44 72 6f 70 54 72 69 67 67 65 72 3a 20 7b 0a 20  DropTrigger: {. 
1a39a 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e   sqlite3UnlinkAn
1a39b 64 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 64  dDeleteTrigger(d
1a39c 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d  b, pOp->p1, pOp-
1a39d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b  >p4.z);.  break;
1a39e 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  .}...#ifndef SQL
1a39f 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49  ITE_OMIT_INTEGRI
1a3a0 54 59 5f 43 48 45 43 4b 0a 2f 2a 20 4f 70 63 6f  TY_CHECK./* Opco
1a3a1 64 65 3a 20 49 6e 74 65 67 72 69 74 79 43 6b 20  de: IntegrityCk 
1a3a2 50 31 20 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a  P1 P2 P3 * P5.**
1a3a3 0a 2a 2a 20 44 6f 20 61 6e 20 61 6e 61 6c 79 73  .** Do an analys
1a3a4 69 73 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  is of the curren
1a3a5 74 6c 79 20 6f 70 65 6e 20 64 61 74 61 62 61 73  tly open databas
1a3a6 65 2e 20 20 53 74 6f 72 65 20 69 6e 0a 2a 2a 20  e.  Store in.** 
1a3a7 72 65 67 69 73 74 65 72 20 50 31 20 74 68 65 20  register P1 the 
1a3a8 74 65 78 74 20 6f 66 20 61 6e 20 65 72 72 6f 72  text of an error
1a3a9 20 6d 65 73 73 61 67 65 20 64 65 73 63 72 69 62   message describ
1a3aa 69 6e 67 20 61 6e 79 20 70 72 6f 62 6c 65 6d 73  ing any problems
1a3ab 2e 0a 2a 2a 20 49 66 20 6e 6f 20 70 72 6f 62 6c  ..** If no probl
1a3ac 65 6d 73 20 61 72 65 20 66 6f 75 6e 64 2c 20 73  ems are found, s
1a3ad 74 6f 72 65 20 61 20 4e 55 4c 4c 20 69 6e 20 72  tore a NULL in r
1a3ae 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 0a 2a  egister P1..**.*
1a3af 2a 20 54 68 65 20 72 65 67 69 73 74 65 72 20 50  * The register P
1a3b0 33 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6d  3 contains the m
1a3b1 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  aximum number of
1a3b2 20 61 6c 6c 6f 77 65 64 20 65 72 72 6f 72 73 2e   allowed errors.
1a3b3 0a 2a 2a 20 41 74 20 6d 6f 73 74 20 72 65 67 28  .** At most reg(
1a3b4 50 33 29 20 65 72 72 6f 72 73 20 77 69 6c 6c 20  P3) errors will 
1a3b5 62 65 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 20  be reported..** 
1a3b6 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
1a3b7 74 68 65 20 61 6e 61 6c 79 73 69 73 20 73 74 6f  the analysis sto
1a3b8 70 73 20 61 73 20 73 6f 6f 6e 20 61 73 20 72 65  ps as soon as re
1a3b9 67 28 50 31 29 20 65 72 72 6f 72 73 20 61 72 65  g(P1) errors are
1a3ba 20 0a 2a 2a 20 73 65 65 6e 2e 20 20 52 65 67 28   .** seen.  Reg(
1a3bb 50 31 29 20 69 73 20 75 70 64 61 74 65 64 20 77  P1) is updated w
1a3bc 69 74 68 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ith the number o
1a3bd 66 20 65 72 72 6f 72 73 20 72 65 6d 61 69 6e 69  f errors remaini
1a3be 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 6f  ng..**.** The ro
1a3bf 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20  ot page numbers 
1a3c0 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e  of all tables in
1a3c1 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 72   the database ar
1a3c2 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 73 74 6f  e integer.** sto
1a3c3 72 65 64 20 69 6e 20 72 65 67 28 50 31 29 2c 20  red in reg(P1), 
1a3c4 72 65 67 28 50 31 2b 31 29 2c 20 72 65 67 28 50  reg(P1+1), reg(P
1a3c5 31 2b 32 29 2c 20 2e 2e 2e 2e 20 20 54 68 65 72  1+2), ....  Ther
1a3c6 65 20 61 72 65 20 50 32 20 74 61 62 6c 65 73 0a  e are P2 tables.
1a3c7 2a 2a 20 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a 2a 20  ** total..**.** 
1a3c8 49 66 20 50 35 20 69 73 20 6e 6f 74 20 7a 65 72  If P5 is not zer
1a3c9 6f 2c 20 74 68 65 20 63 68 65 63 6b 20 69 73 20  o, the check is 
1a3ca 64 6f 6e 65 20 6f 6e 20 74 68 65 20 61 75 78 69  done on the auxi
1a3cb 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 0a 2a  liary database.*
1a3cc 2a 20 66 69 6c 65 2c 20 6e 6f 74 20 74 68 65 20  * file, not the 
1a3cd 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
1a3ce 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f  le..**.** This o
1a3cf 70 63 6f 64 65 20 69 73 20 75 73 65 64 20 74 6f  pcode is used to
1a3d0 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 69   implement the i
1a3d1 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b 20 70  ntegrity_check p
1a3d2 72 61 67 6d 61 2e 0a 2a 2f 0a 63 61 73 65 20 4f  ragma..*/.case O
1a3d3 50 5f 49 6e 74 65 67 72 69 74 79 43 6b 3a 20 7b  P_IntegrityCk: {
1a3d4 0a 20 20 69 6e 74 20 6e 52 6f 6f 74 3b 20 20 20  .  int nRoot;   
1a3d5 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1a3d6 74 61 62 6c 65 73 20 74 6f 20 63 68 65 63 6b 2e  tables to check.
1a3d7 20 20 28 4e 75 6d 62 65 72 20 6f 66 20 72 6f 6f    (Number of roo
1a3d8 74 20 70 61 67 65 73 2e 29 20 2a 2f 0a 20 20 69  t pages.) */.  i
1a3d9 6e 74 20 2a 61 52 6f 6f 74 3b 20 20 20 20 20 2f  nt *aRoot;     /
1a3da 2a 20 41 72 72 61 79 20 6f 66 20 72 6f 6f 74 70  * Array of rootp
1a3db 61 67 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 20  age numbers for 
1a3dc 74 61 62 6c 65 73 20 74 6f 20 62 65 20 63 68 65  tables to be che
1a3dd 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b  cked */.  int j;
1a3de 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
1a3df 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69  p counter */.  i
1a3e0 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 2f  nt nErr;       /
1a3e1 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f  * Number of erro
1a3e2 72 73 20 72 65 70 6f 72 74 65 64 20 2a 2f 0a 20  rs reported */. 
1a3e3 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20   char *z;       
1a3e4 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20   /* Text of the 
1a3e5 65 72 72 6f 72 20 72 65 70 6f 72 74 20 2a 2f 0a  error report */.
1a3e6 20 20 4d 65 6d 20 2a 70 6e 45 72 72 3b 20 20 20    Mem *pnErr;   
1a3e7 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 6b 65    /* Register ke
1a3e8 65 70 69 6e 67 20 74 72 61 63 6b 20 6f 66 20 65  eping track of e
1a3e9 72 72 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67 20  rrors remaining 
1a3ea 2a 2f 0a 20 20 0a 20 20 6e 52 6f 6f 74 20 3d 20  */.  .  nRoot = 
1a3eb 70 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72  pOp->p2;.  asser
1a3ec 74 28 20 6e 52 6f 6f 74 3e 30 20 29 3b 0a 20 20  t( nRoot>0 );.  
1a3ed 61 52 6f 6f 74 20 3d 20 73 71 6c 69 74 65 33 44  aRoot = sqlite3D
1a3ee 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73  bMallocRaw(db, s
1a3ef 69 7a 65 6f 66 28 69 6e 74 29 2a 28 6e 52 6f 6f  izeof(int)*(nRoo
1a3f0 74 2b 31 29 20 29 3b 0a 20 20 69 66 28 20 61 52  t+1) );.  if( aR
1a3f1 6f 6f 74 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f  oot==0 ) goto no
1a3f2 5f 6d 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 20  _mem;.  assert( 
1a3f3 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70  pOp->p3>0 && pOp
1a3f4 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b  ->p3<=p->nMem );
1a3f5 0a 20 20 70 6e 45 72 72 20 3d 20 26 70 2d 3e 61  .  pnErr = &p->a
1a3f6 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20  Mem[pOp->p3];.  
1a3f7 61 73 73 65 72 74 28 20 28 70 6e 45 72 72 2d 3e  assert( (pnErr->
1a3f8 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29  flags & MEM_Int)
1a3f9 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
1a3fa 20 28 70 6e 45 72 72 2d 3e 66 6c 61 67 73 20 26   (pnErr->flags &
1a3fb 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c   (MEM_Str|MEM_Bl
1a3fc 6f 62 29 29 3d 3d 30 20 29 3b 0a 20 20 70 49 6e  ob))==0 );.  pIn
1a3fd 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  1 = &p->aMem[pOp
1a3fe 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 6a 3d 30  ->p1];.  for(j=0
1a3ff 3b 20 6a 3c 6e 52 6f 6f 74 3b 20 6a 2b 2b 29 7b  ; j<nRoot; j++){
1a400 0a 20 20 20 20 61 52 6f 6f 74 5b 6a 5d 20 3d 20  .    aRoot[j] = 
1a401 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61  sqlite3VdbeIntVa
1a402 6c 75 65 28 26 70 49 6e 31 5b 6a 5d 29 3b 0a 20  lue(&pIn1[j]);. 
1a403 20 7d 0a 20 20 61 52 6f 6f 74 5b 6a 5d 20 3d 20   }.  aRoot[j] = 
1a404 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  0;.  assert( pOp
1a405 2d 3e 70 35 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a  ->p5<db->nDb );.
1a406 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74    assert( (p->bt
1a407 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f  reeMask & (1<<pO
1a408 70 2d 3e 70 35 29 29 21 3d 30 20 29 3b 0a 20 20  p->p5))!=0 );.  
1a409 7a 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  z = sqlite3Btree
1a40a 49 6e 74 65 67 72 69 74 79 43 68 65 63 6b 28 64  IntegrityCheck(d
1a40b 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 35 5d 2e  b->aDb[pOp->p5].
1a40c 70 42 74 2c 20 61 52 6f 6f 74 2c 20 6e 52 6f 6f  pBt, aRoot, nRoo
1a40d 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
1a40e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a40f 20 20 20 20 70 6e 45 72 72 2d 3e 75 2e 69 2c 20      pnErr->u.i, 
1a410 26 6e 45 72 72 29 3b 0a 20 20 73 71 6c 69 74 65  &nErr);.  sqlite
1a411 33 44 62 46 72 65 65 28 64 62 2c 20 61 52 6f 6f  3DbFree(db, aRoo
1a412 74 29 3b 0a 20 20 70 6e 45 72 72 2d 3e 75 2e 69  t);.  pnErr->u.i
1a413 20 2d 3d 20 6e 45 72 72 3b 0a 20 20 73 71 6c 69   -= nErr;.  sqli
1a414 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c  te3VdbeMemSetNul
1a415 6c 28 70 49 6e 31 29 3b 0a 20 20 69 66 28 20 6e  l(pIn1);.  if( n
1a416 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73  Err==0 ){.    as
1a417 73 65 72 74 28 20 7a 3d 3d 30 20 29 3b 0a 20 20  sert( z==0 );.  
1a418 7d 65 6c 73 65 20 69 66 28 20 7a 3d 3d 30 20 29  }else if( z==0 )
1a419 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65  {.    goto no_me
1a41a 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  m;.  }else{.    
1a41b 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1a41c 74 53 74 72 28 70 49 6e 31 2c 20 7a 2c 20 2d 31  tStr(pIn1, z, -1
1a41d 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 73  , SQLITE_UTF8, s
1a41e 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20  qlite3_free);.  
1a41f 7d 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42  }.  UPDATE_MAX_B
1a420 4c 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20  LOBSIZE(pIn1);. 
1a421 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1a422 67 65 45 6e 63 6f 64 69 6e 67 28 70 49 6e 31 2c  geEncoding(pIn1,
1a423 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 62 72   encoding);.  br
1a424 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  eak;.}.#endif /*
1a425 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54   SQLITE_OMIT_INT
1a426 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a  EGRITY_CHECK */.
1a427 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 69 66 6f  ./* Opcode: Fifo
1a428 57 72 69 74 65 20 50 31 20 2a 20 2a 20 2a 20 2a  Write P1 * * * *
1a429 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65  .**.** Write the
1a42a 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 72 65   integer from re
1a42b 67 69 73 74 65 72 20 50 31 20 69 6e 74 6f 20 74  gister P1 into t
1a42c 68 65 20 46 69 66 6f 2e 0a 2a 2f 0a 63 61 73 65  he Fifo..*/.case
1a42d 20 4f 50 5f 46 69 66 6f 57 72 69 74 65 3a 20 7b   OP_FifoWrite: {
1a42e 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a          /* in1 *
1a42f 2f 0a 20 20 70 2d 3e 73 46 69 66 6f 2e 64 62 20  /.  p->sFifo.db 
1a430 3d 20 64 62 3b 0a 20 20 69 66 28 20 73 71 6c 69  = db;.  if( sqli
1a431 74 65 33 56 64 62 65 46 69 66 6f 50 75 73 68 28  te3VdbeFifoPush(
1a432 26 70 2d 3e 73 46 69 66 6f 2c 20 73 71 6c 69 74  &p->sFifo, sqlit
1a433 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70  e3VdbeIntValue(p
1a434 49 6e 31 29 29 3d 3d 53 51 4c 49 54 45 5f 4e 4f  In1))==SQLITE_NO
1a435 4d 45 4d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  MEM ){.    goto 
1a436 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 62 72  no_mem;.  }.  br
1a437 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1a438 65 3a 20 46 69 66 6f 52 65 61 64 20 50 31 20 50  e: FifoRead P1 P
1a439 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 74  2 * * *.**.** At
1a43a 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 61 20  tempt to read a 
1a43b 73 69 6e 67 6c 65 20 69 6e 74 65 67 65 72 20 66  single integer f
1a43c 72 6f 6d 20 74 68 65 20 46 69 66 6f 2e 20 20 53  rom the Fifo.  S
1a43d 74 6f 72 65 20 74 68 61 74 0a 2a 2a 20 69 6e 74  tore that.** int
1a43e 65 67 65 72 20 69 6e 20 72 65 67 69 73 74 65 72  eger in register
1a43f 20 50 31 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74   P1..** .** If t
1a440 68 65 20 46 69 66 6f 20 69 73 20 65 6d 70 74 79  he Fifo is empty
1a441 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2f 0a   jump to P2..*/.
1a442 63 61 73 65 20 4f 50 5f 46 69 66 6f 52 65 61 64  case OP_FifoRead
1a443 3a 20 7b 20 20 20 20 20 20 20 20 20 2f 2a 20 6a  : {         /* j
1a444 75 6d 70 20 2a 2f 0a 20 20 43 48 45 43 4b 5f 46  ump */.  CHECK_F
1a445 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20  OR_INTERRUPT;.  
1a446 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1a447 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d  0 && pOp->p1<=p-
1a448 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74 20  >nMem );.  pOut 
1a449 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  = &p->aMem[pOp->
1a44a 70 31 5d 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70  p1];.  MemSetTyp
1a44b 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f  eFlag(pOut, MEM_
1a44c 49 6e 74 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  Int);.  if( sqli
1a44d 74 65 33 56 64 62 65 46 69 66 6f 50 6f 70 28 26  te3VdbeFifoPop(&
1a44e 70 2d 3e 73 46 69 66 6f 2c 20 26 70 4f 75 74 2d  p->sFifo, &pOut-
1a44f 3e 75 2e 69 29 3d 3d 53 51 4c 49 54 45 5f 44 4f  >u.i)==SQLITE_DO
1a450 4e 45 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70  NE ){.    pc = p
1a451 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a  Op->p2 - 1;.  }.
1a452 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e    break;.}..#ifn
1a453 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1a454 54 52 49 47 47 45 52 0a 2f 2a 20 4f 70 63 6f 64  TRIGGER./* Opcod
1a455 65 3a 20 43 6f 6e 74 65 78 74 50 75 73 68 20 2a  e: ContextPush *
1a456 20 2a 20 2a 20 0a 2a 2a 0a 2a 2a 20 53 61 76 65   * * .**.** Save
1a457 20 74 68 65 20 63 75 72 72 65 6e 74 20 56 64 62   the current Vdb
1a458 65 20 63 6f 6e 74 65 78 74 20 73 75 63 68 20 74  e context such t
1a459 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 72 65  hat it can be re
1a45a 73 74 6f 72 65 64 20 62 79 20 61 20 43 6f 6e 74  stored by a Cont
1a45b 65 78 74 50 6f 70 0a 2a 2a 20 6f 70 63 6f 64 65  extPop.** opcode
1a45c 2e 20 54 68 65 20 63 6f 6e 74 65 78 74 20 73 74  . The context st
1a45d 6f 72 65 73 20 74 68 65 20 6c 61 73 74 20 69 6e  ores the last in
1a45e 73 65 72 74 20 72 6f 77 20 69 64 2c 20 74 68 65  sert row id, the
1a45f 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74 20   last statement 
1a460 63 68 61 6e 67 65 0a 2a 2a 20 63 6f 75 6e 74 2c  change.** count,
1a461 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74   and the current
1a462 20 73 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67   statement chang
1a463 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 63 61 73 65  e count..*/.case
1a464 20 4f 50 5f 43 6f 6e 74 65 78 74 50 75 73 68 3a   OP_ContextPush:
1a465 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 2d 3e   {.  int i = p->
1a466 63 6f 6e 74 65 78 74 53 74 61 63 6b 54 6f 70 2b  contextStackTop+
1a467 2b 3b 0a 20 20 43 6f 6e 74 65 78 74 20 2a 70 43  +;.  Context *pC
1a468 6f 6e 74 65 78 74 3b 0a 0a 20 20 61 73 73 65 72  ontext;..  asser
1a469 74 28 20 69 3e 3d 30 20 29 3b 0a 20 20 2f 2a 20  t( i>=0 );.  /* 
1a46a 46 49 58 20 4d 45 3a 20 54 68 69 73 20 73 68 6f  FIX ME: This sho
1a46b 75 6c 64 20 62 65 20 61 6c 6c 6f 63 61 74 65 64  uld be allocated
1a46c 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20   as part of the 
1a46d 76 64 62 65 20 61 74 20 63 6f 6d 70 69 6c 65 2d  vdbe at compile-
1a46e 74 69 6d 65 20 2a 2f 0a 20 20 69 66 28 20 69 3e  time */.  if( i>
1a46f 3d 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b  =p->contextStack
1a470 44 65 70 74 68 20 29 7b 0a 20 20 20 20 70 2d 3e  Depth ){.    p->
1a471 63 6f 6e 74 65 78 74 53 74 61 63 6b 44 65 70 74  contextStackDept
1a472 68 20 3d 20 69 2b 31 3b 0a 20 20 20 20 70 2d 3e  h = i+1;.    p->
1a473 63 6f 6e 74 65 78 74 53 74 61 63 6b 20 3d 20 73  contextStack = s
1a474 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f  qlite3DbReallocO
1a475 72 46 72 65 65 28 64 62 2c 20 70 2d 3e 63 6f 6e  rFree(db, p->con
1a476 74 65 78 74 53 74 61 63 6b 2c 0a 20 20 20 20 20  textStack,.     
1a477 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a478 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a479 20 20 20 20 20 73 69 7a 65 6f 66 28 43 6f 6e 74       sizeof(Cont
1a47a 65 78 74 29 2a 28 69 2b 31 29 29 3b 0a 20 20 20  ext)*(i+1));.   
1a47b 20 69 66 28 20 70 2d 3e 63 6f 6e 74 65 78 74 53   if( p->contextS
1a47c 74 61 63 6b 3d 3d 30 20 29 20 67 6f 74 6f 20 6e  tack==0 ) goto n
1a47d 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 70 43 6f  o_mem;.  }.  pCo
1a47e 6e 74 65 78 74 20 3d 20 26 70 2d 3e 63 6f 6e 74  ntext = &p->cont
1a47f 65 78 74 53 74 61 63 6b 5b 69 5d 3b 0a 20 20 70  extStack[i];.  p
1a480 43 6f 6e 74 65 78 74 2d 3e 6c 61 73 74 52 6f 77  Context->lastRow
1a481 69 64 20 3d 20 64 62 2d 3e 6c 61 73 74 52 6f 77  id = db->lastRow
1a482 69 64 3b 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e  id;.  pContext->
1a483 6e 43 68 61 6e 67 65 20 3d 20 70 2d 3e 6e 43 68  nChange = p->nCh
1a484 61 6e 67 65 3b 0a 20 20 70 43 6f 6e 74 65 78 74  ange;.  pContext
1a485 2d 3e 73 46 69 66 6f 20 3d 20 70 2d 3e 73 46 69  ->sFifo = p->sFi
1a486 66 6f 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  fo;.  sqlite3Vdb
1a487 65 46 69 66 6f 49 6e 69 74 28 26 70 2d 3e 73 46  eFifoInit(&p->sF
1a488 69 66 6f 2c 20 64 62 29 3b 0a 20 20 62 72 65 61  ifo, db);.  brea
1a489 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1a48a 20 43 6f 6e 74 65 78 74 50 6f 70 20 2a 20 2a 20   ContextPop * * 
1a48b 2a 20 0a 2a 2a 0a 2a 2a 20 52 65 73 74 6f 72 65  * .**.** Restore
1a48c 20 74 68 65 20 56 64 62 65 20 63 6f 6e 74 65 78   the Vdbe contex
1a48d 74 20 74 6f 20 74 68 65 20 73 74 61 74 65 20 69  t to the state i
1a48e 74 20 77 61 73 20 69 6e 20 77 68 65 6e 20 63 6f  t was in when co
1a48f 6e 74 65 78 74 50 75 73 68 20 77 61 73 20 6c 61  ntextPush was la
1a490 73 74 0a 2a 2a 20 65 78 65 63 75 74 65 64 2e 20  st.** executed. 
1a491 54 68 65 20 63 6f 6e 74 65 78 74 20 73 74 6f 72  The context stor
1a492 65 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65  es the last inse
1a493 72 74 20 72 6f 77 20 69 64 2c 20 74 68 65 20 6c  rt row id, the l
1a494 61 73 74 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a  ast statement.**
1a495 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 2c 20 61   change count, a
1a496 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  nd the current s
1a497 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 20  tatement change 
1a498 63 6f 75 6e 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f  count..*/.case O
1a499 50 5f 43 6f 6e 74 65 78 74 50 6f 70 3a 20 7b 0a  P_ContextPop: {.
1a49a 20 20 43 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74    Context *pCont
1a49b 65 78 74 20 3d 20 26 70 2d 3e 63 6f 6e 74 65 78  ext = &p->contex
1a49c 74 53 74 61 63 6b 5b 2d 2d 70 2d 3e 63 6f 6e 74  tStack[--p->cont
1a49d 65 78 74 53 74 61 63 6b 54 6f 70 5d 3b 0a 20 20  extStackTop];.  
1a49e 61 73 73 65 72 74 28 20 70 2d 3e 63 6f 6e 74 65  assert( p->conte
1a49f 78 74 53 74 61 63 6b 54 6f 70 3e 3d 30 20 29 3b  xtStackTop>=0 );
1a4a0 0a 20 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64  .  db->lastRowid
1a4a1 20 3d 20 70 43 6f 6e 74 65 78 74 2d 3e 6c 61 73   = pContext->las
1a4a2 74 52 6f 77 69 64 3b 0a 20 20 70 2d 3e 6e 43 68  tRowid;.  p->nCh
1a4a3 61 6e 67 65 20 3d 20 70 43 6f 6e 74 65 78 74 2d  ange = pContext-
1a4a4 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 73 71 6c 69  >nChange;.  sqli
1a4a5 74 65 33 56 64 62 65 46 69 66 6f 43 6c 65 61 72  te3VdbeFifoClear
1a4a6 28 26 70 2d 3e 73 46 69 66 6f 29 3b 0a 20 20 70  (&p->sFifo);.  p
1a4a7 2d 3e 73 46 69 66 6f 20 3d 20 70 43 6f 6e 74 65  ->sFifo = pConte
1a4a8 78 74 2d 3e 73 46 69 66 6f 3b 0a 20 20 62 72 65  xt->sFifo;.  bre
1a4a9 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ak;.}.#endif /* 
1a4aa 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1a4ab 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a 0a  MIT_TRIGGER */..
1a4ac 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1a4ad 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e  MIT_AUTOINCREMEN
1a4ae 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 65 6d  T./* Opcode: Mem
1a4af 4d 61 78 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  Max P1 P2 * * *.
1a4b0 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 61  **.** Set the va
1a4b1 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 72 20  lue of register 
1a4b2 50 31 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75  P1 to the maximu
1a4b3 6d 20 6f 66 20 69 74 73 20 63 75 72 72 65 6e 74  m of its current
1a4b4 20 76 61 6c 75 65 0a 2a 2a 20 61 6e 64 20 74 68   value.** and th
1a4b5 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1a4b6 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68  ter P2..**.** Th
1a4b7 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74  is instruction t
1a4b8 68 72 6f 77 73 20 61 6e 20 65 72 72 6f 72 20 69  hrows an error i
1a4b9 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c  f the memory cel
1a4ba 6c 20 69 73 20 6e 6f 74 20 69 6e 69 74 69 61 6c  l is not initial
1a4bb 6c 79 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72  ly.** an integer
1a4bc 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 65 6d  ..*/.case OP_Mem
1a4bd 4d 61 78 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a  Max: {        /*
1a4be 20 69 6e 31 2c 20 69 6e 32 20 2a 2f 0a 20 20 73   in1, in2 */.  s
1a4bf 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74  qlite3VdbeMemInt
1a4c0 65 67 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20  egerify(pIn1);. 
1a4c1 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49   sqlite3VdbeMemI
1a4c2 6e 74 65 67 65 72 69 66 79 28 70 49 6e 32 29 3b  ntegerify(pIn2);
1a4c3 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e 69  .  if( pIn1->u.i
1a4c4 3c 70 49 6e 32 2d 3e 75 2e 69 29 7b 0a 20 20 20  <pIn2->u.i){.   
1a4c5 20 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 49 6e   pIn1->u.i = pIn
1a4c6 32 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 62 72  2->u.i;.  }.  br
1a4c7 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  eak;.}.#endif /*
1a4c8 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
1a4c9 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 0a 2f  OINCREMENT */../
1a4ca 2a 20 4f 70 63 6f 64 65 3a 20 49 66 50 6f 73 20  * Opcode: IfPos 
1a4cb 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
1a4cc 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f  * If the value o
1a4cd 66 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73  f register P1 is
1a4ce 20 31 20 6f 72 20 67 72 65 61 74 65 72 2c 20 6a   1 or greater, j
1a4cf 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a  ump to P2..**.**
1a4d0 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74   It is illegal t
1a4d1 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 74 72  o use this instr
1a4d2 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69  uction on a regi
1a4d3 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a 2a  ster that does.*
1a4d4 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e  * not contain an
1a4d5 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 73   integer.  An as
1a4d6 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69  sertion fault wi
1a4d7 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f 75  ll result if you
1a4d8 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50   try..*/.case OP
1a4d9 5f 49 66 50 6f 73 3a 20 7b 20 20 20 20 20 20 20  _IfPos: {       
1a4da 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f   /* jump, in1 */
1a4db 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d  .  assert( pIn1-
1a4dc 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29  >flags&MEM_Int )
1a4dd 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e  ;.  if( pIn1->u.
1a4de 69 3e 30 20 29 7b 0a 20 20 20 20 20 70 63 20 3d  i>0 ){.     pc =
1a4df 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
1a4e0 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1a4e1 20 4f 70 63 6f 64 65 3a 20 49 66 4e 65 67 20 50   Opcode: IfNeg P
1a4e2 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1a4e3 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66   If the value of
1a4e4 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20   register P1 is 
1a4e5 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20  less than zero, 
1a4e6 6a 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a  jump to P2. .**.
1a4e7 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c  ** It is illegal
1a4e8 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73   to use this ins
1a4e9 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65  truction on a re
1a4ea 67 69 73 74 65 72 20 74 68 61 74 20 64 6f 65 73  gister that does
1a4eb 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20  .** not contain 
1a4ec 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20  an integer.  An 
1a4ed 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20  assertion fault 
1a4ee 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79  will result if y
1a4ef 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20  ou try..*/.case 
1a4f0 4f 50 5f 49 66 4e 65 67 3a 20 7b 20 20 20 20 20  OP_IfNeg: {     
1a4f1 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20     /* jump, in1 
1a4f2 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e  */.  assert( pIn
1a4f3 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74  1->flags&MEM_Int
1a4f4 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e   );.  if( pIn1->
1a4f5 75 2e 69 3c 30 20 29 7b 0a 20 20 20 20 20 70 63  u.i<0 ){.     pc
1a4f6 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1a4f7 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1a4f8 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 5a 65 72  /* Opcode: IfZer
1a4f9 6f 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  o P1 P2 * * *.**
1a4fa 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65  .** If the value
1a4fb 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20   of register P1 
1a4fc 69 73 20 65 78 61 63 74 6c 79 20 30 2c 20 6a 75  is exactly 0, ju
1a4fd 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a 2a 2a  mp to P2. .**.**
1a4fe 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74   It is illegal t
1a4ff 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 74 72  o use this instr
1a500 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69  uction on a regi
1a501 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a 2a  ster that does.*
1a502 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e  * not contain an
1a503 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 73   integer.  An as
1a504 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69  sertion fault wi
1a505 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f 75  ll result if you
1a506 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50   try..*/.case OP
1a507 5f 49 66 5a 65 72 6f 3a 20 7b 20 20 20 20 20 20  _IfZero: {      
1a508 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a    /* jump, in1 *
1a509 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31  /.  assert( pIn1
1a50a 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20  ->flags&MEM_Int 
1a50b 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75  );.  if( pIn1->u
1a50c 2e 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20 70 63  .i==0 ){.     pc
1a50d 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1a50e 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1a50f 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 67 53 74  /* Opcode: AggSt
1a510 65 70 20 2a 20 50 32 20 50 33 20 50 34 20 50 35  ep * P2 P3 P4 P5
1a511 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 74  .**.** Execute t
1a512 68 65 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e  he step function
1a513 20 66 6f 72 20 61 6e 20 61 67 67 72 65 67 61 74   for an aggregat
1a514 65 2e 20 20 54 68 65 0a 2a 2a 20 66 75 6e 63 74  e.  The.** funct
1a515 69 6f 6e 20 68 61 73 20 50 35 20 61 72 67 75 6d  ion has P5 argum
1a516 65 6e 74 73 2e 20 20 20 50 34 20 69 73 20 61 20  ents.   P4 is a 
1a517 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46  pointer to the F
1a518 75 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75 63 74  uncDef.** struct
1a519 75 72 65 20 74 68 61 74 20 73 70 65 63 69 66 69  ure that specifi
1a51a 65 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e  es the function.
1a51b 20 20 55 73 65 20 72 65 67 69 73 74 65 72 0a 2a    Use register.*
1a51c 2a 20 50 33 20 61 73 20 74 68 65 20 61 63 63 75  * P3 as the accu
1a51d 6d 75 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54  mulator..**.** T
1a51e 68 65 20 50 35 20 61 72 67 75 6d 65 6e 74 73 20  he P5 arguments 
1a51f 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72  are taken from r
1a520 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 20 69  egister P2 and i
1a521 74 73 0a 2a 2a 20 73 75 63 63 65 73 73 6f 72 73  ts.** successors
1a522 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 67 67  ..*/.case OP_Agg
1a523 53 74 65 70 3a 20 7b 0a 20 20 69 6e 74 20 6e 20  Step: {.  int n 
1a524 3d 20 70 4f 70 2d 3e 70 35 3b 0a 20 20 69 6e 74  = pOp->p5;.  int
1a525 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 2c   i;.  Mem *pMem,
1a526 20 2a 70 52 65 63 3b 0a 20 20 73 71 6c 69 74 65   *pRec;.  sqlite
1a527 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20  3_context ctx;. 
1a528 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
1a529 2a 61 70 56 61 6c 3b 0a 0a 20 20 61 73 73 65 72  *apVal;..  asser
1a52a 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 70 52 65  t( n>=0 );.  pRe
1a52b 63 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  c = &p->aMem[pOp
1a52c 2d 3e 70 32 5d 3b 0a 20 20 61 70 56 61 6c 20 3d  ->p2];.  apVal =
1a52d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 61 73 73   p->apArg;.  ass
1a52e 65 72 74 28 20 61 70 56 61 6c 20 7c 7c 20 6e 3d  ert( apVal || n=
1a52f 3d 30 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  =0 );.  for(i=0;
1a530 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 52 65 63 2b   i<n; i++, pRec+
1a531 2b 29 7b 0a 20 20 20 20 61 70 56 61 6c 5b 69 5d  +){.    apVal[i]
1a532 20 3d 20 70 52 65 63 3b 0a 20 20 20 20 73 74 6f   = pRec;.    sto
1a533 72 65 54 79 70 65 49 6e 66 6f 28 70 52 65 63 2c  reTypeInfo(pRec,
1a534 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a   encoding);.  }.
1a535 20 20 63 74 78 2e 70 46 75 6e 63 20 3d 20 70 4f    ctx.pFunc = pO
1a536 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20 61  p->p4.pFunc;.  a
1a537 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30  ssert( pOp->p3>0
1a538 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e   && pOp->p3<=p->
1a539 6e 4d 65 6d 20 29 3b 0a 20 20 63 74 78 2e 70 4d  nMem );.  ctx.pM
1a53a 65 6d 20 3d 20 70 4d 65 6d 20 3d 20 26 70 2d 3e  em = pMem = &p->
1a53b 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20  aMem[pOp->p3];. 
1a53c 20 70 4d 65 6d 2d 3e 6e 2b 2b 3b 0a 20 20 63 74   pMem->n++;.  ct
1a53d 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  x.s.flags = MEM_
1a53e 4e 75 6c 6c 3b 0a 20 20 63 74 78 2e 73 2e 7a 20  Null;.  ctx.s.z 
1a53f 3d 20 30 3b 0a 20 20 63 74 78 2e 73 2e 7a 4d 61  = 0;.  ctx.s.zMa
1a540 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 63 74 78 2e  lloc = 0;.  ctx.
1a541 73 2e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 63 74  s.xDel = 0;.  ct
1a542 78 2e 73 2e 64 62 20 3d 20 64 62 3b 0a 20 20 63  x.s.db = db;.  c
1a543 74 78 2e 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a  tx.isError = 0;.
1a544 20 20 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 30 3b    ctx.pColl = 0;
1a545 0a 20 20 69 66 28 20 63 74 78 2e 70 46 75 6e 63  .  if( ctx.pFunc
1a546 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
1a547 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29  _FUNC_NEEDCOLL )
1a548 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f  {.    assert( pO
1a549 70 3e 70 2d 3e 61 4f 70 20 29 3b 0a 20 20 20 20  p>p->aOp );.    
1a54a 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e  assert( pOp[-1].
1a54b 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53  p4type==P4_COLLS
1a54c 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  EQ );.    assert
1a54d 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65  ( pOp[-1].opcode
1a54e 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29 3b 0a  ==OP_CollSeq );.
1a54f 20 20 20 20 63 74 78 2e 70 43 6f 6c 6c 20 3d 20      ctx.pColl = 
1a550 70 4f 70 5b 2d 31 5d 2e 70 34 2e 70 43 6f 6c 6c  pOp[-1].p4.pColl
1a551 3b 0a 20 20 7d 0a 20 20 28 63 74 78 2e 70 46 75  ;.  }.  (ctx.pFu
1a552 6e 63 2d 3e 78 53 74 65 70 29 28 26 63 74 78 2c  nc->xStep)(&ctx,
1a553 20 6e 2c 20 61 70 56 61 6c 29 3b 0a 20 20 69 66   n, apVal);.  if
1a554 28 20 63 74 78 2e 69 73 45 72 72 6f 72 20 29 7b  ( ctx.isError ){
1a555 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
1a556 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
1a557 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c  g, db, "%s", sql
1a558 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
1a559 26 63 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63  &ctx.s));.    rc
1a55a 20 3d 20 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a   = ctx.isError;.
1a55b 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
1a55c 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 63 74 78  eMemRelease(&ctx
1a55d 2e 73 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .s);.  break;.}.
1a55e 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 67 46  ./* Opcode: AggF
1a55f 69 6e 61 6c 20 50 31 20 50 32 20 2a 20 50 34 20  inal P1 P2 * P4 
1a560 2a 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20  *.**.** Execute 
1a561 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 66 75  the finalizer fu
1a562 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e 20 61 67  nction for an ag
1a563 67 72 65 67 61 74 65 2e 20 20 50 31 20 69 73 0a  gregate.  P1 is.
1a564 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f  ** the memory lo
1a565 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 74  cation that is t
1a566 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 66  he accumulator f
1a567 6f 72 20 74 68 65 20 61 67 67 72 65 67 61 74 65  or the aggregate
1a568 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20 74 68  ..**.** P2 is th
1a569 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  e number of argu
1a56a 6d 65 6e 74 73 20 74 68 61 74 20 74 68 65 20 73  ments that the s
1a56b 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b  tep function tak
1a56c 65 73 20 61 6e 64 0a 2a 2a 20 50 34 20 69 73 20  es and.** P4 is 
1a56d 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
1a56e 20 46 75 6e 63 44 65 66 20 66 6f 72 20 74 68 69   FuncDef for thi
1a56f 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65  s function.  The
1a570 20 50 32 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20   P2.** argument 
1a571 69 73 20 6e 6f 74 20 75 73 65 64 20 62 79 20 74  is not used by t
1a572 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 49 74 20  his opcode.  It 
1a573 69 73 20 6f 6e 6c 79 20 74 68 65 72 65 20 74 6f  is only there to
1a574 20 64 69 73 61 6d 62 69 67 75 61 74 65 0a 2a 2a   disambiguate.**
1a575 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20   functions that 
1a576 63 61 6e 20 74 61 6b 65 20 76 61 72 79 69 6e 67  can take varying
1a577 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 72 67 75   numbers of argu
1a578 6d 65 6e 74 73 2e 20 20 54 68 65 0a 2a 2a 20 50  ments.  The.** P
1a579 34 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e  4 argument is on
1a57a 6c 79 20 6e 65 65 64 65 64 20 66 6f 72 20 74 68  ly needed for th
1a57b 65 20 64 65 67 65 6e 65 72 61 74 65 20 63 61 73  e degenerate cas
1a57c 65 20 77 68 65 72 65 0a 2a 2a 20 74 68 65 20 73  e where.** the s
1a57d 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 77 61 73  tep function was
1a57e 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20   not previously 
1a57f 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 63 61 73 65 20  called..*/.case 
1a580 4f 50 5f 41 67 67 46 69 6e 61 6c 3a 20 7b 0a 20  OP_AggFinal: {. 
1a581 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 61 73   Mem *pMem;.  as
1a582 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20  sert( pOp->p1>0 
1a583 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e  && pOp->p1<=p->n
1a584 4d 65 6d 20 29 3b 0a 20 20 70 4d 65 6d 20 3d 20  Mem );.  pMem = 
1a585 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31  &p->aMem[pOp->p1
1a586 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d  ];.  assert( (pM
1a587 65 6d 2d 3e 66 6c 61 67 73 20 26 20 7e 28 4d 45  em->flags & ~(ME
1a588 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 41 67 67 29 29  M_Null|MEM_Agg))
1a589 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  ==0 );.  rc = sq
1a58a 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61  lite3VdbeMemFina
1a58b 6c 69 7a 65 28 70 4d 65 6d 2c 20 70 4f 70 2d 3e  lize(pMem, pOp->
1a58c 70 34 2e 70 46 75 6e 63 29 3b 0a 20 20 69 66 28  p4.pFunc);.  if(
1a58d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 52 52 4f   rc==SQLITE_ERRO
1a58e 52 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  R ){.    sqlite3
1a58f 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
1a590 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c  rrMsg, db, "%s",
1a591 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
1a592 65 78 74 28 70 4d 65 6d 29 29 3b 0a 20 20 7d 0a  ext(pMem));.  }.
1a593 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1a594 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d  ngeEncoding(pMem
1a595 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 55  , encoding);.  U
1a596 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49  PDATE_MAX_BLOBSI
1a597 5a 45 28 70 4d 65 6d 29 3b 0a 20 20 69 66 28 20  ZE(pMem);.  if( 
1a598 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f  sqlite3VdbeMemTo
1a599 6f 42 69 67 28 70 4d 65 6d 29 20 29 7b 0a 20 20  oBig(pMem) ){.  
1a59a 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a    goto too_big;.
1a59b 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1a59c 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
1a59d 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d  LITE_OMIT_VACUUM
1a59e 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
1a59f 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48  LITE_OMIT_ATTACH
1a5a0 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61 63  )./* Opcode: Vac
1a5a1 75 75 6d 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a  uum * * * * *.**
1a5a2 0a 2a 2a 20 56 61 63 75 75 6d 20 74 68 65 20 65  .** Vacuum the e
1a5a3 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 2e 20  ntire database. 
1a5a4 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 69 6c   This opcode wil
1a5a5 6c 20 63 61 75 73 65 20 6f 74 68 65 72 20 76 69  l cause other vi
1a5a6 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65  rtual.** machine
1a5a7 73 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20  s to be created 
1a5a8 61 6e 64 20 72 75 6e 2e 20 20 49 74 20 6d 61 79  and run.  It may
1a5a9 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 66   not be called f
1a5aa 72 6f 6d 20 77 69 74 68 69 6e 0a 2a 2a 20 61 20  rom within.** a 
1a5ab 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a  transaction..*/.
1a5ac 63 61 73 65 20 4f 50 5f 56 61 63 75 75 6d 3a 20  case OP_Vacuum: 
1a5ad 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  {.  if( sqlite3S
1a5ae 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67  afetyOff(db) ) g
1a5af 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1a5b0 5f 6d 69 73 75 73 65 3b 20 0a 20 20 72 63 20 3d  _misuse; .  rc =
1a5b1 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75 75   sqlite3RunVacuu
1a5b2 6d 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64  m(&p->zErrMsg, d
1a5b3 62 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  b);.  if( sqlite
1a5b4 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20  3SafetyOn(db) ) 
1a5b5 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1a5b6 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 62 72 65 61  o_misuse;.  brea
1a5b7 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  k;.}.#endif..#if
1a5b8 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1a5b9 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
1a5ba 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 63  )./* Opcode: Inc
1a5bb 72 56 61 63 75 75 6d 20 50 31 20 50 32 20 2a 20  rVacuum P1 P2 * 
1a5bc 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 65 72 66 6f 72  * *.**.** Perfor
1a5bd 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70 20  m a single step 
1a5be 6f 66 20 74 68 65 20 69 6e 63 72 65 6d 65 6e 74  of the increment
1a5bf 61 6c 20 76 61 63 75 75 6d 20 70 72 6f 63 65 64  al vacuum proced
1a5c0 75 72 65 20 6f 6e 0a 2a 2a 20 74 68 65 20 50 31  ure on.** the P1
1a5c1 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 74 68   database. If th
1a5c2 65 20 76 61 63 75 75 6d 20 68 61 73 20 66 69 6e  e vacuum has fin
1a5c3 69 73 68 65 64 2c 20 6a 75 6d 70 20 74 6f 20 69  ished, jump to i
1a5c4 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 50 32  nstruction.** P2
1a5c5 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61 6c  . Otherwise, fal
1a5c6 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65  l through to the
1a5c7 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f   next instructio
1a5c8 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e  n..*/.case OP_In
1a5c9 63 72 56 61 63 75 75 6d 3a 20 7b 20 20 20 20 20  crVacuum: {     
1a5ca 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20     /* jump */.  
1a5cb 42 74 72 65 65 20 2a 70 42 74 3b 0a 0a 20 20 61  Btree *pBt;..  a
1a5cc 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d  ssert( pOp->p1>=
1a5cd 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d  0 && pOp->p1<db-
1a5ce 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74  >nDb );.  assert
1a5cf 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20  ( (p->btreeMask 
1a5d0 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21  & (1<<pOp->p1))!
1a5d1 3d 30 20 29 3b 0a 20 20 70 42 74 20 3d 20 64 62  =0 );.  pBt = db
1a5d2 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70  ->aDb[pOp->p1].p
1a5d3 42 74 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  Bt;.  rc = sqlit
1a5d4 65 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75  e3BtreeIncrVacuu
1a5d5 6d 28 70 42 74 29 3b 0a 20 20 69 66 28 20 72 63  m(pBt);.  if( rc
1a5d6 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
1a5d7 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70  .    pc = pOp->p
1a5d8 32 20 2d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20  2 - 1;.    rc = 
1a5d9 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
1a5da 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1a5db 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 45 78 70  ../* Opcode: Exp
1a5dc 69 72 65 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a  ire P1 * * * *.*
1a5dd 2a 0a 2a 2a 20 43 61 75 73 65 20 70 72 65 63 6f  *.** Cause preco
1a5de 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74  mpiled statement
1a5df 73 20 74 6f 20 62 65 63 6f 6d 65 20 65 78 70 69  s to become expi
1a5e0 72 65 64 2e 20 41 6e 20 65 78 70 69 72 65 64 20  red. An expired 
1a5e1 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 66 61 69  statement.** fai
1a5e2 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72  ls with an error
1a5e3 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 5f   code of SQLITE_
1a5e4 53 43 48 45 4d 41 20 69 66 20 69 74 20 69 73 20  SCHEMA if it is 
1a5e5 65 76 65 72 20 65 78 65 63 75 74 65 64 20 0a 2a  ever executed .*
1a5e6 2a 20 28 76 69 61 20 73 71 6c 69 74 65 33 5f 73  * (via sqlite3_s
1a5e7 74 65 70 28 29 29 2e 0a 2a 2a 20 0a 2a 2a 20 49  tep())..** .** I
1a5e8 66 20 50 31 20 69 73 20 30 2c 20 74 68 65 6e 20  f P1 is 0, then 
1a5e9 61 6c 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  all SQL statemen
1a5ea 74 73 20 62 65 63 6f 6d 65 20 65 78 70 69 72 65  ts become expire
1a5eb 64 2e 20 49 66 20 50 31 20 69 73 20 6e 6f 6e 2d  d. If P1 is non-
1a5ec 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 6f 6e  zero,.** then on
1a5ed 6c 79 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79  ly the currently
1a5ee 20 65 78 65 63 75 74 69 6e 67 20 73 74 61 74 65   executing state
1a5ef 6d 65 6e 74 20 69 73 20 61 66 66 65 63 74 65 64  ment is affected
1a5f0 2e 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 45 78  . .*/.case OP_Ex
1a5f1 70 69 72 65 3a 20 7b 0a 20 20 69 66 28 20 21 70  pire: {.  if( !p
1a5f2 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 73 71  Op->p1 ){.    sq
1a5f3 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61  lite3ExpirePrepa
1a5f4 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62  redStatements(db
1a5f5 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1a5f6 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b 0a  p->expired = 1;.
1a5f7 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1a5f8 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1a5f9 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
1a5fa 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 61 62 6c  ./* Opcode: Tabl
1a5fb 65 4c 6f 63 6b 20 50 31 20 50 32 20 50 33 20 50  eLock P1 P2 P3 P
1a5fc 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 62 74 61 69 6e  4 *.**.** Obtain
1a5fd 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 70 61 72   a lock on a par
1a5fe 74 69 63 75 6c 61 72 20 74 61 62 6c 65 2e 20 54  ticular table. T
1a5ff 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1a600 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65  is only used whe
1a601 6e 0a 2a 2a 20 74 68 65 20 73 68 61 72 65 64 2d  n.** the shared-
1a602 63 61 63 68 65 20 66 65 61 74 75 72 65 20 69 73  cache feature is
1a603 20 65 6e 61 62 6c 65 64 2e 20 0a 2a 2a 0a 2a 2a   enabled. .**.**
1a604 20 49 66 20 50 31 20 69 73 20 20 74 68 65 20 69   If P1 is  the i
1a605 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
1a606 62 61 73 65 20 69 6e 20 73 71 6c 69 74 65 33 2e  base in sqlite3.
1a607 61 44 62 5b 5d 20 6f 66 20 74 68 65 20 64 61 74  aDb[] of the dat
1a608 61 62 61 73 65 0a 2a 2a 20 6f 6e 20 77 68 69 63  abase.** on whic
1a609 68 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 63  h the lock is ac
1a60a 71 75 69 72 65 64 2e 20 20 41 20 72 65 61 64 6c  quired.  A readl
1a60b 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20  ock is obtained 
1a60c 69 66 20 50 33 3d 3d 30 20 6f 72 0a 2a 2a 20 61  if P3==0 or.** a
1a60d 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 50   write lock if P
1a60e 33 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 63  3==1..**.** P2 c
1a60f 6f 6e 74 61 69 6e 73 20 74 68 65 20 72 6f 6f 74  ontains the root
1a610 2d 70 61 67 65 20 6f 66 20 74 68 65 20 74 61 62  -page of the tab
1a611 6c 65 20 74 6f 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a  le to lock..**.*
1a612 2a 20 50 34 20 63 6f 6e 74 61 69 6e 73 20 61 20  * P4 contains a 
1a613 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e  pointer to the n
1a614 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
1a615 20 62 65 69 6e 67 20 6c 6f 63 6b 65 64 2e 20 54   being locked. T
1a616 68 69 73 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 75  his is only.** u
1a617 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20  sed to generate 
1a618 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
1a619 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e   if the lock can
1a61a 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2e  not be obtained.
1a61b 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 61 62 6c  .*/.case OP_Tabl
1a61c 65 4c 6f 63 6b 3a 20 7b 0a 20 20 69 6e 74 20 70  eLock: {.  int p
1a61d 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 0a 20 20  1 = pOp->p1; .  
1a61e 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 20 3d  u8 isWriteLock =
1a61f 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 61 73 73 65   pOp->p3;.  asse
1a620 72 74 28 20 70 31 3e 3d 30 20 26 26 20 70 31 3c  rt( p1>=0 && p1<
1a621 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73  db->nDb );.  ass
1a622 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61  ert( (p->btreeMa
1a623 73 6b 20 26 20 28 31 3c 3c 70 31 29 29 21 3d 30  sk & (1<<p1))!=0
1a624 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73   );.  assert( is
1a625 57 72 69 74 65 4c 6f 63 6b 3d 3d 30 20 7c 7c 20  WriteLock==0 || 
1a626 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 31 20 29  isWriteLock==1 )
1a627 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1a628 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 64  BtreeLockTable(d
1a629 62 2d 3e 61 44 62 5b 70 31 5d 2e 70 42 74 2c 20  b->aDb[p1].pBt, 
1a62a 70 4f 70 2d 3e 70 32 2c 20 69 73 57 72 69 74 65  pOp->p2, isWrite
1a62b 4c 6f 63 6b 29 3b 0a 20 20 69 66 28 20 72 63 3d  Lock);.  if( rc=
1a62c 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 29  =SQLITE_LOCKED )
1a62d 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
1a62e 20 2a 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b   *z = pOp->p4.z;
1a62f 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
1a630 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
1a631 67 2c 20 64 62 2c 20 22 64 61 74 61 62 61 73 65  g, db, "database
1a632 20 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64   table is locked
1a633 3a 20 25 73 22 2c 20 7a 29 3b 0a 20 20 7d 0a 20  : %s", z);.  }. 
1a634 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1a635 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1a636 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a  SHARED_CACHE */.
1a637 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1a638 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1a639 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 42 65  E./* Opcode: VBe
1a63a 67 69 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a  gin * * * P4 *.*
1a63b 2a 0a 2a 2a 20 50 34 20 6d 61 79 20 62 65 20 61  *.** P4 may be a
1a63c 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73   pointer to an s
1a63d 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75  qlite3_vtab stru
1a63e 63 74 75 72 65 2e 20 49 66 20 73 6f 2c 20 63 61  cture. If so, ca
1a63f 6c 6c 20 74 68 65 20 0a 2a 2a 20 78 42 65 67 69  ll the .** xBegi
1a640 6e 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 61  n method for tha
1a641 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41  t table..**.** A
1a642 6c 73 6f 2c 20 77 68 65 74 68 65 72 20 6f 72 20  lso, whether or 
1a643 6e 6f 74 20 50 34 20 69 73 20 73 65 74 2c 20 63  not P4 is set, c
1a644 68 65 63 6b 20 74 68 61 74 20 74 68 69 73 20 69  heck that this i
1a645 73 20 6e 6f 74 20 62 65 69 6e 67 20 63 61 6c 6c  s not being call
1a646 65 64 20 66 72 6f 6d 0a 2a 2a 20 77 69 74 68 69  ed from.** withi
1a647 6e 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20  n a callback to 
1a648 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
1a649 78 53 79 6e 63 28 29 20 6d 65 74 68 6f 64 2e 20  xSync() method. 
1a64a 49 66 20 69 74 20 69 73 2c 20 73 65 74 20 74 68  If it is, set th
1a64b 65 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20  e.** error code 
1a64c 74 6f 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  to SQLITE_LOCKED
1a64d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 42 65  ..*/.case OP_VBe
1a64e 67 69 6e 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33  gin: {.  sqlite3
1a64f 5f 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20 70  _vtab *pVtab = p
1a650 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3b 0a 20 20  Op->p4.pVtab;.  
1a651 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62  rc = sqlite3Vtab
1a652 42 65 67 69 6e 28 64 62 2c 20 70 56 74 61 62 29  Begin(db, pVtab)
1a653 3b 0a 20 20 69 66 28 20 70 56 74 61 62 20 29 7b  ;.  if( pVtab ){
1a654 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
1a655 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73  ee(db, p->zErrMs
1a656 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d  g);.    p->zErrM
1a657 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72  sg = pVtab->zErr
1a658 4d 73 67 3b 0a 20 20 20 20 70 56 74 61 62 2d 3e  Msg;.    pVtab->
1a659 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 7d  zErrMsg = 0;.  }
1a65a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
1a65b 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1a65c 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a  T_VIRTUALTABLE *
1a65d 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
1a65e 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1a65f 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56  BLE./* Opcode: V
1a660 43 72 65 61 74 65 20 50 31 20 2a 20 2a 20 50 34  Create P1 * * P4
1a661 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 74   *.**.** P4 is t
1a662 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 76 69 72  he name of a vir
1a663 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 20 64 61  tual table in da
1a664 74 61 62 61 73 65 20 50 31 2e 20 43 61 6c 6c 20  tabase P1. Call 
1a665 74 68 65 20 78 43 72 65 61 74 65 20 6d 65 74 68  the xCreate meth
1a666 6f 64 0a 2a 2a 20 66 6f 72 20 74 68 61 74 20 74  od.** for that t
1a667 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  able..*/.case OP
1a668 5f 56 43 72 65 61 74 65 3a 20 7b 0a 20 20 72 63  _VCreate: {.  rc
1a669 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 43 61   = sqlite3VtabCa
1a66a 6c 6c 43 72 65 61 74 65 28 64 62 2c 20 70 4f 70  llCreate(db, pOp
1a66b 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c  ->p1, pOp->p4.z,
1a66c 20 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20   &p->zErrMsg);. 
1a66d 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1a66e 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1a66f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a  VIRTUALTABLE */.
1a670 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1a671 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1a672 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 44 65  E./* Opcode: VDe
1a673 73 74 72 6f 79 20 50 31 20 2a 20 2a 20 50 34 20  stroy P1 * * P4 
1a674 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 74 68  *.**.** P4 is th
1a675 65 20 6e 61 6d 65 20 6f 66 20 61 20 76 69 72 74  e name of a virt
1a676 75 61 6c 20 74 61 62 6c 65 20 69 6e 20 64 61 74  ual table in dat
1a677 61 62 61 73 65 20 50 31 2e 20 20 43 61 6c 6c 20  abase P1.  Call 
1a678 74 68 65 20 78 44 65 73 74 72 6f 79 20 6d 65 74  the xDestroy met
1a679 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 61 74 20 74  hod.** of that t
1a67a 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  able..*/.case OP
1a67b 5f 56 44 65 73 74 72 6f 79 3a 20 7b 0a 20 20 70  _VDestroy: {.  p
1a67c 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d  ->inVtabMethod =
1a67d 20 32 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   2;.  rc = sqlit
1a67e 65 33 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f  e3VtabCallDestro
1a67f 79 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70  y(db, pOp->p1, p
1a680 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 70 2d 3e  Op->p4.z);.  p->
1a681 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 30  inVtabMethod = 0
1a682 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e  ;.  break;.}.#en
1a683 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1a684 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20  IT_VIRTUALTABLE 
1a685 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
1a686 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
1a687 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ABLE./* Opcode: 
1a688 56 4f 70 65 6e 20 50 31 20 2a 20 2a 20 50 34 20  VOpen P1 * * P4 
1a689 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20  *.**.** P4 is a 
1a68a 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72  pointer to a vir
1a68b 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63  tual table objec
1a68c 74 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74  t, an sqlite3_vt
1a68d 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ab structure..**
1a68e 20 50 31 20 69 73 20 61 20 63 75 72 73 6f 72 20   P1 is a cursor 
1a68f 6e 75 6d 62 65 72 2e 20 20 54 68 69 73 20 6f 70  number.  This op
1a690 63 6f 64 65 20 6f 70 65 6e 73 20 61 20 63 75 72  code opens a cur
1a691 73 6f 72 20 74 6f 20 74 68 65 20 76 69 72 74 75  sor to the virtu
1a692 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20  al.** table and 
1a693 73 74 6f 72 65 73 20 74 68 61 74 20 63 75 72 73  stores that curs
1a694 6f 72 20 69 6e 20 50 31 2e 0a 2a 2f 0a 63 61 73  or in P1..*/.cas
1a695 65 20 4f 50 5f 56 4f 70 65 6e 3a 20 7b 0a 20 20  e OP_VOpen: {.  
1a696 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72  VdbeCursor *pCur
1a697 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f   = 0;.  sqlite3_
1a698 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74  vtab_cursor *pVt
1a699 61 62 43 75 72 73 6f 72 20 3d 20 30 3b 0a 0a 20  abCursor = 0;.. 
1a69a 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
1a69b 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70  Vtab = pOp->p4.p
1a69c 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Vtab;.  sqlite3_
1a69d 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 20  module *pModule 
1a69e 3d 20 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  = (sqlite3_modul
1a69f 65 20 2a 29 70 56 74 61 62 2d 3e 70 4d 6f 64 75  e *)pVtab->pModu
1a6a0 6c 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 70 56  le;..  assert(pV
1a6a1 74 61 62 20 26 26 20 70 4d 6f 64 75 6c 65 29 3b  tab && pModule);
1a6a2 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
1a6a3 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f  fetyOff(db) ) go
1a6a4 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1a6a5 6d 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 70  misuse;.  rc = p
1a6a6 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 70 56  Module->xOpen(pV
1a6a7 74 61 62 2c 20 26 70 56 74 61 62 43 75 72 73 6f  tab, &pVtabCurso
1a6a8 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  r);.  sqlite3DbF
1a6a9 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d  ree(db, p->zErrM
1a6aa 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73  sg);.  p->zErrMs
1a6ab 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d  g = pVtab->zErrM
1a6ac 73 67 3b 0a 20 20 70 56 74 61 62 2d 3e 7a 45 72  sg;.  pVtab->zEr
1a6ad 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 66 28 20  rMsg = 0;.  if( 
1a6ae 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
1a6af 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  db) ) goto abort
1a6b0 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a  _due_to_misuse;.
1a6b1 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d    if( SQLITE_OK=
1a6b2 3d 72 63 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e  =rc ){.    /* In
1a6b3 69 74 69 61 6c 69 7a 65 20 73 71 6c 69 74 65 33  itialize sqlite3
1a6b4 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 62 61 73  _vtab_cursor bas
1a6b5 65 20 63 6c 61 73 73 20 2a 2f 0a 20 20 20 20 70  e class */.    p
1a6b6 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61  VtabCursor->pVta
1a6b7 62 20 3d 20 70 56 74 61 62 3b 0a 0a 20 20 20 20  b = pVtab;..    
1a6b8 2f 2a 20 49 6e 69 74 69 61 6c 69 73 65 20 76 64  /* Initialise vd
1a6b9 62 65 20 63 75 72 73 6f 72 20 6f 62 6a 65 63 74  be cursor object
1a6ba 20 2a 2f 0a 20 20 20 20 70 43 75 72 20 3d 20 61   */.    pCur = a
1a6bb 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c  llocateCursor(p,
1a6bc 20 70 4f 70 2d 3e 70 31 2c 20 26 70 4f 70 5b 2d   pOp->p1, &pOp[-
1a6bd 31 5d 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 20 20  1], -1, 0);.    
1a6be 69 66 28 20 70 43 75 72 20 29 7b 0a 20 20 20 20  if( pCur ){.    
1a6bf 20 20 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72    pCur->pVtabCur
1a6c0 73 6f 72 20 3d 20 70 56 74 61 62 43 75 72 73 6f  sor = pVtabCurso
1a6c1 72 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70  r;.      pCur->p
1a6c2 4d 6f 64 75 6c 65 20 3d 20 70 56 74 61 62 43 75  Module = pVtabCu
1a6c3 72 73 6f 72 2d 3e 70 56 74 61 62 2d 3e 70 4d 6f  rsor->pVtab->pMo
1a6c4 64 75 6c 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  dule;.    }else{
1a6c5 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  .      db->mallo
1a6c6 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
1a6c7 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f     pModule->xClo
1a6c8 73 65 28 70 56 74 61 62 43 75 72 73 6f 72 29 3b  se(pVtabCursor);
1a6c9 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65  .    }.  }.  bre
1a6ca 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ak;.}.#endif /* 
1a6cb 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1a6cc 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66  UALTABLE */..#if
1a6cd 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1a6ce 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a  _VIRTUALTABLE./*
1a6cf 20 4f 70 63 6f 64 65 3a 20 56 46 69 6c 74 65 72   Opcode: VFilter
1a6d0 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a   P1 P2 P3 P4 *.*
1a6d1 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 63 75 72  *.** P1 is a cur
1a6d2 73 6f 72 20 6f 70 65 6e 65 64 20 75 73 69 6e 67  sor opened using
1a6d3 20 56 4f 70 65 6e 2e 20 20 50 32 20 69 73 20 61   VOpen.  P2 is a
1a6d4 6e 20 61 64 64 72 65 73 73 20 74 6f 20 6a 75 6d  n address to jum
1a6d5 70 20 74 6f 20 69 66 0a 2a 2a 20 74 68 65 20 66  p to if.** the f
1a6d6 69 6c 74 65 72 65 64 20 72 65 73 75 6c 74 20 73  iltered result s
1a6d7 65 74 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2a 0a  et is empty..**.
1a6d8 2a 2a 20 50 34 20 69 73 20 65 69 74 68 65 72 20  ** P4 is either 
1a6d9 4e 55 4c 4c 20 6f 72 20 61 20 73 74 72 69 6e 67  NULL or a string
1a6da 20 74 68 61 74 20 77 61 73 20 67 65 6e 65 72 61   that was genera
1a6db 74 65 64 20 62 79 20 74 68 65 20 78 42 65 73 74  ted by the xBest
1a6dc 49 6e 64 65 78 0a 2a 2a 20 6d 65 74 68 6f 64 20  Index.** method 
1a6dd 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20  of the module.  
1a6de 54 68 65 20 69 6e 74 65 72 70 72 65 74 61 74 69  The interpretati
1a6df 6f 6e 20 6f 66 20 74 68 65 20 50 34 20 73 74 72  on of the P4 str
1a6e0 69 6e 67 20 69 73 20 6c 65 66 74 0a 2a 2a 20 74  ing is left.** t
1a6e1 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 69 6d 70  o the module imp
1a6e2 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a  lementation..**.
1a6e3 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69  ** This opcode i
1a6e4 6e 76 6f 6b 65 73 20 74 68 65 20 78 46 69 6c 74  nvokes the xFilt
1a6e5 65 72 20 6d 65 74 68 6f 64 20 6f 6e 20 74 68 65  er method on the
1a6e6 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73   virtual table s
1a6e7 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 50  pecified.** by P
1a6e8 31 2e 20 20 54 68 65 20 69 6e 74 65 67 65 72 20  1.  The integer 
1a6e9 71 75 65 72 79 20 70 6c 61 6e 20 70 61 72 61 6d  query plan param
1a6ea 65 74 65 72 20 74 6f 20 78 46 69 6c 74 65 72 20  eter to xFilter 
1a6eb 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67  is stored in reg
1a6ec 69 73 74 65 72 0a 2a 2a 20 50 33 2e 20 52 65 67  ister.** P3. Reg
1a6ed 69 73 74 65 72 20 50 33 2b 31 20 73 74 6f 72 65  ister P3+1 store
1a6ee 73 20 74 68 65 20 61 72 67 63 20 70 61 72 61 6d  s the argc param
1a6ef 65 74 65 72 20 74 6f 20 62 65 20 70 61 73 73 65  eter to be passe
1a6f0 64 20 74 6f 20 74 68 65 0a 2a 2a 20 78 46 69 6c  d to the.** xFil
1a6f1 74 65 72 20 6d 65 74 68 6f 64 2e 20 52 65 67 69  ter method. Regi
1a6f2 73 74 65 72 73 20 50 33 2b 32 2e 2e 50 33 2b 31  sters P3+2..P3+1
1a6f3 2b 61 72 67 63 20 61 72 65 20 74 68 65 20 61 72  +argc are the ar
1a6f4 67 63 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c  gc.** additional
1a6f5 20 70 61 72 61 6d 65 74 65 72 73 20 77 68 69 63   parameters whic
1a6f6 68 20 61 72 65 20 70 61 73 73 65 64 20 74 6f 0a  h are passed to.
1a6f7 2a 2a 20 78 46 69 6c 74 65 72 20 61 73 20 61 72  ** xFilter as ar
1a6f8 67 76 2e 20 52 65 67 69 73 74 65 72 20 50 33 2b  gv. Register P3+
1a6f9 32 20 62 65 63 6f 6d 65 73 20 61 72 67 76 5b 30  2 becomes argv[0
1a6fa 5d 20 77 68 65 6e 20 70 61 73 73 65 64 20 74 6f  ] when passed to
1a6fb 20 78 46 69 6c 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   xFilter..**.** 
1a6fc 41 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 20 74  A jump is made t
1a6fd 6f 20 50 32 20 69 66 20 74 68 65 20 72 65 73 75  o P2 if the resu
1a6fe 6c 74 20 73 65 74 20 61 66 74 65 72 20 66 69 6c  lt set after fil
1a6ff 74 65 72 69 6e 67 20 77 6f 75 6c 64 20 62 65 20  tering would be 
1a700 65 6d 70 74 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f  empty..*/.case O
1a701 50 5f 56 46 69 6c 74 65 72 3a 20 7b 20 20 20 2f  P_VFilter: {   /
1a702 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 69 6e 74 20  * jump */.  int 
1a703 6e 41 72 67 3b 0a 20 20 69 6e 74 20 69 51 75 65  nArg;.  int iQue
1a704 72 79 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69  ry;.  const sqli
1a705 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
1a706 75 6c 65 3b 0a 20 20 4d 65 6d 20 2a 70 51 75 65  ule;.  Mem *pQue
1a707 72 79 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  ry = &p->aMem[pO
1a708 70 2d 3e 70 33 5d 3b 0a 20 20 4d 65 6d 20 2a 70  p->p3];.  Mem *p
1a709 41 72 67 63 20 3d 20 26 70 51 75 65 72 79 5b 31  Argc = &pQuery[1
1a70a 5d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  ];.  sqlite3_vta
1a70b 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43  b_cursor *pVtabC
1a70c 75 72 73 6f 72 3b 0a 20 20 73 71 6c 69 74 65 33  ursor;.  sqlite3
1a70d 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 0a 20  _vtab *pVtab;.. 
1a70e 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75   VdbeCursor *pCu
1a70f 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  r = p->apCsr[pOp
1a710 2d 3e 70 31 5d 3b 0a 0a 20 20 52 45 47 49 53 54  ->p1];..  REGIST
1a711 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33  ER_TRACE(pOp->p3
1a712 2c 20 70 51 75 65 72 79 29 3b 0a 20 20 61 73 73  , pQuery);.  ass
1a713 65 72 74 28 20 70 43 75 72 2d 3e 70 56 74 61 62  ert( pCur->pVtab
1a714 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 56 74 61  Cursor );.  pVta
1a715 62 43 75 72 73 6f 72 20 3d 20 70 43 75 72 2d 3e  bCursor = pCur->
1a716 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 70  pVtabCursor;.  p
1a717 56 74 61 62 20 3d 20 70 56 74 61 62 43 75 72 73  Vtab = pVtabCurs
1a718 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20 70 4d 6f  or->pVtab;.  pMo
1a719 64 75 6c 65 20 3d 20 70 56 74 61 62 2d 3e 70 4d  dule = pVtab->pM
1a71a 6f 64 75 6c 65 3b 0a 0a 20 20 2f 2a 20 47 72 61  odule;..  /* Gra
1a71b 62 20 74 68 65 20 69 6e 64 65 78 20 6e 75 6d 62  b the index numb
1a71c 65 72 20 61 6e 64 20 61 72 67 63 20 70 61 72 61  er and argc para
1a71d 6d 65 74 65 72 73 20 2a 2f 0a 20 20 61 73 73 65  meters */.  asse
1a71e 72 74 28 20 28 70 51 75 65 72 79 2d 3e 66 6c 61  rt( (pQuery->fla
1a71f 67 73 26 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 26  gs&MEM_Int)!=0 &
1a720 26 20 70 41 72 67 63 2d 3e 66 6c 61 67 73 3d 3d  & pArgc->flags==
1a721 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 6e 41 72  MEM_Int );.  nAr
1a722 67 20 3d 20 70 41 72 67 63 2d 3e 75 2e 69 3b 0a  g = pArgc->u.i;.
1a723 20 20 69 51 75 65 72 79 20 3d 20 70 51 75 65 72    iQuery = pQuer
1a724 79 2d 3e 75 2e 69 3b 0a 0a 20 20 2f 2a 20 49 6e  y->u.i;..  /* In
1a725 76 6f 6b 65 20 74 68 65 20 78 46 69 6c 74 65 72  voke the xFilter
1a726 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 7b 0a 20   method */.  {. 
1a727 20 20 20 69 6e 74 20 72 65 73 20 3d 20 30 3b 0a     int res = 0;.
1a728 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 4d      int i;.    M
1a729 65 6d 20 2a 2a 61 70 41 72 67 20 3d 20 70 2d 3e  em **apArg = p->
1a72a 61 70 41 72 67 3b 0a 20 20 20 20 66 6f 72 28 69  apArg;.    for(i
1a72b 20 3d 20 30 3b 20 69 3c 6e 41 72 67 3b 20 69 2b   = 0; i<nArg; i+
1a72c 2b 29 7b 0a 20 20 20 20 20 20 61 70 41 72 67 5b  +){.      apArg[
1a72d 69 5d 20 3d 20 26 70 41 72 67 63 5b 69 2b 31 5d  i] = &pArgc[i+1]
1a72e 3b 0a 20 20 20 20 20 20 73 74 6f 72 65 54 79 70  ;.      storeTyp
1a72f 65 49 6e 66 6f 28 61 70 41 72 67 5b 69 5d 2c 20  eInfo(apArg[i], 
1a730 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  0);.    }..    i
1a731 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1a732 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61  Off(db) ) goto a
1a733 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75  bort_due_to_misu
1a734 73 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  se;.    sqlite3V
1a735 74 61 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b 0a  tabLock(pVtab);.
1a736 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74      p->inVtabMet
1a737 68 6f 64 20 3d 20 31 3b 0a 20 20 20 20 72 63 20  hod = 1;.    rc 
1a738 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 46 69 6c 74  = pModule->xFilt
1a739 65 72 28 70 56 74 61 62 43 75 72 73 6f 72 2c 20  er(pVtabCursor, 
1a73a 69 51 75 65 72 79 2c 20 70 4f 70 2d 3e 70 34 2e  iQuery, pOp->p4.
1a73b 7a 2c 20 6e 41 72 67 2c 20 61 70 41 72 67 29 3b  z, nArg, apArg);
1a73c 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65  .    p->inVtabMe
1a73d 74 68 6f 64 20 3d 20 30 3b 0a 20 20 20 20 73 71  thod = 0;.    sq
1a73e 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1a73f 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20  p->zErrMsg);.   
1a740 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56   p->zErrMsg = pV
1a741 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20  tab->zErrMsg;.  
1a742 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67    pVtab->zErrMsg
1a743 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65   = 0;.    sqlite
1a744 33 56 74 61 62 55 6e 6c 6f 63 6b 28 64 62 2c 20  3VtabUnlock(db, 
1a745 70 56 74 61 62 29 3b 0a 20 20 20 20 69 66 28 20  pVtab);.    if( 
1a746 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1a747 0a 20 20 20 20 20 20 72 65 73 20 3d 20 70 4d 6f  .      res = pMo
1a748 64 75 6c 65 2d 3e 78 45 6f 66 28 70 56 74 61 62  dule->xEof(pVtab
1a749 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 7d 0a 20  Cursor);.    }. 
1a74a 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61     if( sqlite3Sa
1a74b 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74  fetyOn(db) ) got
1a74c 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d  o abort_due_to_m
1a74d 69 73 75 73 65 3b 0a 0a 20 20 20 20 69 66 28 20  isuse;..    if( 
1a74e 72 65 73 20 29 7b 0a 20 20 20 20 20 20 70 63 20  res ){.      pc 
1a74f 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1a750 20 20 20 7d 0a 20 20 7d 0a 20 20 70 43 75 72 2d     }.  }.  pCur-
1a751 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 0a 20  >nullRow = 0;.. 
1a752 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1a753 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1a754 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a  VIRTUALTABLE */.
1a755 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1a756 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1a757 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 52 6f  E./* Opcode: VRo
1a758 77 69 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  wid P1 P2 * * *.
1a759 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 69 6e 74 6f  **.** Store into
1a75a 20 72 65 67 69 73 74 65 72 20 50 32 20 20 74 68   register P2  th
1a75b 65 20 72 6f 77 69 64 20 6f 66 0a 2a 2a 20 74 68  e rowid of.** th
1a75c 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20  e virtual-table 
1a75d 74 68 61 74 20 74 68 65 20 50 31 20 63 75 72 73  that the P1 curs
1a75e 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74  or is pointing t
1a75f 6f 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 52  o..*/.case OP_VR
1a760 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20  owid: {         
1a761 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72      /* out2-prer
1a762 65 6c 65 61 73 65 20 2a 2f 0a 20 20 73 71 6c 69  elease */.  sqli
1a763 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b  te3_vtab *pVtab;
1a764 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
1a765 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65  _module *pModule
1a766 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  ;.  sqlite_int64
1a767 20 69 52 6f 77 3b 0a 20 20 56 64 62 65 43 75 72   iRow;.  VdbeCur
1a768 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e 61  sor *pCur = p->a
1a769 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 0a  pCsr[pOp->p1];..
1a76a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
1a76b 70 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20  pVtabCursor );. 
1a76c 20 69 66 28 20 70 43 75 72 2d 3e 6e 75 6c 6c 52   if( pCur->nullR
1a76d 6f 77 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b  ow ){.    break;
1a76e 0a 20 20 7d 0a 20 20 70 56 74 61 62 20 3d 20 70  .  }.  pVtab = p
1a76f 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72  Cur->pVtabCursor
1a770 2d 3e 70 56 74 61 62 3b 0a 20 20 70 4d 6f 64 75  ->pVtab;.  pModu
1a771 6c 65 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64  le = pVtab->pMod
1a772 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ule;.  assert( p
1a773 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64 20 29  Module->xRowid )
1a774 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ;.  if( sqlite3S
1a775 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67  afetyOff(db) ) g
1a776 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1a777 5f 6d 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20  _misuse;.  rc = 
1a778 70 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64 28  pModule->xRowid(
1a779 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f  pCur->pVtabCurso
1a77a 72 2c 20 26 69 52 6f 77 29 3b 0a 20 20 73 71 6c  r, &iRow);.  sql
1a77b 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1a77c 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d  ->zErrMsg);.  p-
1a77d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56 74 61 62  >zErrMsg = pVtab
1a77e 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 70 56 74  ->zErrMsg;.  pVt
1a77f 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b  ab->zErrMsg = 0;
1a780 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
1a781 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74  fetyOn(db) ) got
1a782 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d  o abort_due_to_m
1a783 69 73 75 73 65 3b 0a 20 20 4d 65 6d 53 65 74 54  isuse;.  MemSetT
1a784 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45  ypeFlag(pOut, ME
1a785 4d 5f 49 6e 74 29 3b 0a 20 20 70 4f 75 74 2d 3e  M_Int);.  pOut->
1a786 75 2e 69 20 3d 20 69 52 6f 77 3b 0a 20 20 62 72  u.i = iRow;.  br
1a787 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  eak;.}.#endif /*
1a788 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1a789 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69  TUALTABLE */..#i
1a78a 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1a78b 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f  T_VIRTUALTABLE./
1a78c 2a 20 4f 70 63 6f 64 65 3a 20 56 43 6f 6c 75 6d  * Opcode: VColum
1a78d 6e 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  n P1 P2 P3 * *.*
1a78e 2a 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 76  *.** Store the v
1a78f 61 6c 75 65 20 6f 66 20 74 68 65 20 50 32 2d 74  alue of the P2-t
1a790 68 20 63 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a 20 74  h column of.** t
1a791 68 65 20 72 6f 77 20 6f 66 20 74 68 65 20 76 69  he row of the vi
1a792 72 74 75 61 6c 2d 74 61 62 6c 65 20 74 68 61 74  rtual-table that
1a793 20 74 68 65 20 0a 2a 2a 20 50 31 20 63 75 72 73   the .** P1 curs
1a794 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74  or is pointing t
1a795 6f 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20  o into register 
1a796 50 33 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56  P3..*/.case OP_V
1a797 43 6f 6c 75 6d 6e 3a 20 7b 0a 20 20 73 71 6c 69  Column: {.  sqli
1a798 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b  te3_vtab *pVtab;
1a799 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
1a79a 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65  _module *pModule
1a79b 3b 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b 0a  ;.  Mem *pDest;.
1a79c 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
1a79d 74 20 73 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 56  t sContext;..  V
1a79e 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 20  dbeCursor *pCur 
1a79f 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e  = p->apCsr[pOp->
1a7a0 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  p1];.  assert( p
1a7a1 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72  Cur->pVtabCursor
1a7a2 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
1a7a3 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e  p->p3>0 && pOp->
1a7a4 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20  p3<=p->nMem );. 
1a7a5 20 70 44 65 73 74 20 3d 20 26 70 2d 3e 61 4d 65   pDest = &p->aMe
1a7a6 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66  m[pOp->p3];.  if
1a7a7 28 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20  ( pCur->nullRow 
1a7a8 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1a7a9 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 44 65  beMemSetNull(pDe
1a7aa 73 74 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a  st);.    break;.
1a7ab 20 20 7d 0a 20 20 70 56 74 61 62 20 3d 20 70 43    }.  pVtab = pC
1a7ac 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d  ur->pVtabCursor-
1a7ad 3e 70 56 74 61 62 3b 0a 20 20 70 4d 6f 64 75 6c  >pVtab;.  pModul
1a7ae 65 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75  e = pVtab->pModu
1a7af 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d  le;.  assert( pM
1a7b0 6f 64 75 6c 65 2d 3e 78 43 6f 6c 75 6d 6e 20 29  odule->xColumn )
1a7b1 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73 43 6f 6e  ;.  memset(&sCon
1a7b2 74 65 78 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28  text, 0, sizeof(
1a7b3 73 43 6f 6e 74 65 78 74 29 29 3b 0a 0a 20 20 2f  sContext));..  /
1a7b4 2a 20 54 68 65 20 6f 75 74 70 75 74 20 63 65 6c  * The output cel
1a7b5 6c 20 6d 61 79 20 61 6c 72 65 61 64 79 20 68 61  l may already ha
1a7b6 76 65 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f  ve a buffer allo
1a7b7 63 61 74 65 64 2e 20 4d 6f 76 65 0a 20 20 2a 2a  cated. Move.  **
1a7b8 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6e   the current con
1a7b9 74 65 6e 74 73 20 74 6f 20 73 43 6f 6e 74 65 78  tents to sContex
1a7ba 74 2e 73 20 73 6f 20 69 6e 20 63 61 73 65 20 74  t.s so in case t
1a7bb 68 65 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e  he user-function
1a7bc 20 0a 20 20 2a 2a 20 63 61 6e 20 75 73 65 20 74   .  ** can use t
1a7bd 68 65 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63  he already alloc
1a7be 61 74 65 64 20 62 75 66 66 65 72 20 69 6e 73 74  ated buffer inst
1a7bf 65 61 64 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6e  ead of allocatin
1a7c0 67 20 61 20 0a 20 20 2a 2a 20 6e 65 77 20 6f 6e  g a .  ** new on
1a7c1 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  e..  */.  sqlite
1a7c2 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 73 43  3VdbeMemMove(&sC
1a7c3 6f 6e 74 65 78 74 2e 73 2c 20 70 44 65 73 74 29  ontext.s, pDest)
1a7c4 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c  ;.  MemSetTypeFl
1a7c5 61 67 28 26 73 43 6f 6e 74 65 78 74 2e 73 2c 20  ag(&sContext.s, 
1a7c6 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 69 66  MEM_Null);..  if
1a7c7 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
1a7c8 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62  ff(db) ) goto ab
1a7c9 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
1a7ca 65 3b 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c  e;.  rc = pModul
1a7cb 65 2d 3e 78 43 6f 6c 75 6d 6e 28 70 43 75 72 2d  e->xColumn(pCur-
1a7cc 3e 70 56 74 61 62 43 75 72 73 6f 72 2c 20 26 73  >pVtabCursor, &s
1a7cd 43 6f 6e 74 65 78 74 2c 20 70 4f 70 2d 3e 70 32  Context, pOp->p2
1a7ce 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
1a7cf 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73  ee(db, p->zErrMs
1a7d0 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67  g);.  p->zErrMsg
1a7d1 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73   = pVtab->zErrMs
1a7d2 67 3b 0a 20 20 70 56 74 61 62 2d 3e 7a 45 72 72  g;.  pVtab->zErr
1a7d3 4d 73 67 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 43  Msg = 0;..  /* C
1a7d4 6f 70 79 20 74 68 65 20 72 65 73 75 6c 74 20 6f  opy the result o
1a7d5 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74  f the function t
1a7d6 6f 20 74 68 65 20 50 33 20 72 65 67 69 73 74 65  o the P3 registe
1a7d7 72 2e 20 57 65 0a 20 20 2a 2a 20 64 6f 20 74 68  r. We.  ** do th
1a7d8 69 73 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66  is regardless of
1a7d9 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
1a7da 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64  an error occured
1a7db 20 74 6f 20 65 6e 73 75 72 65 20 61 6e 79 0a 20   to ensure any. 
1a7dc 20 2a 2a 20 64 79 6e 61 6d 69 63 20 61 6c 6c 6f   ** dynamic allo
1a7dd 63 61 74 69 6f 6e 20 69 6e 20 73 43 6f 6e 74 65  cation in sConte
1a7de 78 74 2e 73 20 28 61 20 4d 65 6d 20 73 74 72 75  xt.s (a Mem stru
1a7df 63 74 29 20 69 73 20 20 72 65 6c 65 61 73 65 64  ct) is  released
1a7e0 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
1a7e1 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69  VdbeChangeEncodi
1a7e2 6e 67 28 26 73 43 6f 6e 74 65 78 74 2e 73 2c 20  ng(&sContext.s, 
1a7e3 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 52 45 47  encoding);.  REG
1a7e4 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
1a7e5 3e 70 33 2c 20 70 44 65 73 74 29 3b 0a 20 20 73  >p3, pDest);.  s
1a7e6 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76  qlite3VdbeMemMov
1a7e7 65 28 70 44 65 73 74 2c 20 26 73 43 6f 6e 74 65  e(pDest, &sConte
1a7e8 78 74 2e 73 29 3b 0a 20 20 55 50 44 41 54 45 5f  xt.s);.  UPDATE_
1a7e9 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 44 65  MAX_BLOBSIZE(pDe
1a7ea 73 74 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69  st);..  if( sqli
1a7eb 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20  te3SafetyOn(db) 
1a7ec 29 7b 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f 72  ){.    goto abor
1a7ed 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b  t_due_to_misuse;
1a7ee 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74  .  }.  if( sqlit
1a7ef 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28  e3VdbeMemTooBig(
1a7f0 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20 67 6f  pDest) ){.    go
1a7f1 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a  to too_big;.  }.
1a7f2 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69    break;.}.#endi
1a7f3 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1a7f4 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
1a7f5 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1a7f6 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1a7f7 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4e  LE./* Opcode: VN
1a7f8 65 78 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  ext P1 P2 * * *.
1a7f9 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 76 69  **.** Advance vi
1a7fa 72 74 75 61 6c 20 74 61 62 6c 65 20 50 31 20 74  rtual table P1 t
1a7fb 6f 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20 69  o the next row i
1a7fc 6e 20 69 74 73 20 72 65 73 75 6c 74 20 73 65 74  n its result set
1a7fd 20 61 6e 64 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20   and.** jump to 
1a7fe 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e 20  instruction P2. 
1a7ff 20 4f 72 2c 20 69 66 20 74 68 65 20 76 69 72 74   Or, if the virt
1a800 75 61 6c 20 74 61 62 6c 65 20 68 61 73 20 72 65  ual table has re
1a801 61 63 68 65 64 0a 2a 2a 20 74 68 65 20 65 6e 64  ached.** the end
1a802 20 6f 66 20 69 74 73 20 72 65 73 75 6c 74 20 73   of its result s
1a803 65 74 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74 68  et, then fall th
1a804 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78  rough to the nex
1a805 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  t instruction..*
1a806 2f 0a 63 61 73 65 20 4f 50 5f 56 4e 65 78 74 3a  /.case OP_VNext:
1a807 20 7b 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a   {   /* jump */.
1a808 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
1a809 70 56 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73  pVtab;.  const s
1a80a 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
1a80b 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 72 65  Module;.  int re
1a80c 73 20 3d 20 30 3b 0a 0a 20 20 56 64 62 65 43 75  s = 0;..  VdbeCu
1a80d 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e  rsor *pCur = p->
1a80e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  apCsr[pOp->p1];.
1a80f 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
1a810 70 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20  pVtabCursor );. 
1a811 20 69 66 28 20 70 43 75 72 2d 3e 6e 75 6c 6c 52   if( pCur->nullR
1a812 6f 77 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b  ow ){.    break;
1a813 0a 20 20 7d 0a 20 20 70 56 74 61 62 20 3d 20 70  .  }.  pVtab = p
1a814 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72  Cur->pVtabCursor
1a815 2d 3e 70 56 74 61 62 3b 0a 20 20 70 4d 6f 64 75  ->pVtab;.  pModu
1a816 6c 65 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64  le = pVtab->pMod
1a817 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ule;.  assert( p
1a818 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 20 29 3b  Module->xNext );
1a819 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68  ..  /* Invoke th
1a81a 65 20 78 4e 65 78 74 28 29 20 6d 65 74 68 6f 64  e xNext() method
1a81b 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20   of the module. 
1a81c 54 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20  There is no way 
1a81d 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 75 6e 64  for the.  ** und
1a81e 65 72 6c 79 69 6e 67 20 69 6d 70 6c 65 6d 65 6e  erlying implemen
1a81f 74 61 74 69 6f 6e 20 74 6f 20 72 65 74 75 72 6e  tation to return
1a820 20 61 6e 20 65 72 72 6f 72 20 69 66 20 6f 6e 65   an error if one
1a821 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 0a 20   occurs during. 
1a822 20 2a 2a 20 78 4e 65 78 74 28 29 2e 20 49 6e 73   ** xNext(). Ins
1a823 74 65 61 64 2c 20 69 66 20 61 6e 20 65 72 72 6f  tead, if an erro
1a824 72 20 6f 63 63 75 72 73 2c 20 74 72 75 65 20 69  r occurs, true i
1a825 73 20 72 65 74 75 72 6e 65 64 20 28 69 6e 64 69  s returned (indi
1a826 63 61 74 69 6e 67 20 74 68 61 74 20 0a 20 20 2a  cating that .  *
1a827 2a 20 64 61 74 61 20 69 73 20 61 76 61 69 6c 61  * data is availa
1a828 62 6c 65 29 20 61 6e 64 20 74 68 65 20 65 72 72  ble) and the err
1a829 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64  or code returned
1a82a 20 77 68 65 6e 20 78 43 6f 6c 75 6d 6e 20 6f 72   when xColumn or
1a82b 0a 20 20 2a 2a 20 73 6f 6d 65 20 6f 74 68 65 72  .  ** some other
1a82c 20 6d 65 74 68 6f 64 20 69 73 20 6e 65 78 74 20   method is next 
1a82d 69 6e 76 6f 6b 65 64 20 6f 6e 20 74 68 65 20 73  invoked on the s
1a82e 61 76 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ave virtual tabl
1a82f 65 20 63 75 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20  e cursor..  */. 
1a830 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
1a831 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f  tyOff(db) ) goto
1a832 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69   abort_due_to_mi
1a833 73 75 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 56  suse;.  sqlite3V
1a834 74 61 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b 0a  tabLock(pVtab);.
1a835 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f    p->inVtabMetho
1a836 64 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20 70 4d  d = 1;.  rc = pM
1a837 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43 75  odule->xNext(pCu
1a838 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 29 3b  r->pVtabCursor);
1a839 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68  .  p->inVtabMeth
1a83a 6f 64 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  od = 0;.  sqlite
1a83b 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a  3DbFree(db, p->z
1a83c 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45  ErrMsg);.  p->zE
1a83d 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a  rrMsg = pVtab->z
1a83e 45 72 72 4d 73 67 3b 0a 20 20 70 56 74 61 62 2d  ErrMsg;.  pVtab-
1a83f 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  >zErrMsg = 0;.  
1a840 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63  sqlite3VtabUnloc
1a841 6b 28 64 62 2c 20 70 56 74 61 62 29 3b 0a 20 20  k(db, pVtab);.  
1a842 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1a843 4b 20 29 7b 0a 20 20 20 20 72 65 73 20 3d 20 70  K ){.    res = p
1a844 4d 6f 64 75 6c 65 2d 3e 78 45 6f 66 28 70 43 75  Module->xEof(pCu
1a845 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 29 3b  r->pVtabCursor);
1a846 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74  .  }.  if( sqlit
1a847 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29  e3SafetyOn(db) )
1a848 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1a849 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 69 66  to_misuse;..  if
1a84a 28 20 21 72 65 73 20 29 7b 0a 20 20 20 20 2f 2a  ( !res ){.    /*
1a84b 20 49 66 20 74 68 65 72 65 20 69 73 20 64 61 74   If there is dat
1a84c 61 2c 20 6a 75 6d 70 20 74 6f 20 50 32 20 2a 2f  a, jump to P2 */
1a84d 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70  .    pc = pOp->p
1a84e 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65  2 - 1;.  }.  bre
1a84f 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ak;.}.#endif /* 
1a850 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1a851 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66  UALTABLE */..#if
1a852 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1a853 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a  _VIRTUALTABLE./*
1a854 20 4f 70 63 6f 64 65 3a 20 56 52 65 6e 61 6d 65   Opcode: VRename
1a855 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a   P1 * * P4 *.**.
1a856 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74  ** P4 is a point
1a857 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20  er to a virtual 
1a858 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 6e  table object, an
1a859 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74   sqlite3_vtab st
1a85a 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 69 73  ructure..** This
1a85b 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20   opcode invokes 
1a85c 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
1a85d 67 20 78 52 65 6e 61 6d 65 20 6d 65 74 68 6f 64  g xRename method
1a85e 2e 20 54 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69  . The value.** i
1a85f 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73  n register P1 is
1a860 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 7a   passed as the z
1a861 4e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 74 6f  Name argument to
1a862 20 74 68 65 20 78 52 65 6e 61 6d 65 20 6d 65 74   the xRename met
1a863 68 6f 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  hod..*/.case OP_
1a864 56 52 65 6e 61 6d 65 3a 20 7b 0a 20 20 73 71 6c  VRename: {.  sql
1a865 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
1a866 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62   = pOp->p4.pVtab
1a867 3b 0a 20 20 4d 65 6d 20 2a 70 4e 61 6d 65 20 3d  ;.  Mem *pName =
1a868 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1a869 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 56  1];.  assert( pV
1a86a 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52  tab->pModule->xR
1a86b 65 6e 61 6d 65 20 29 3b 0a 20 20 52 45 47 49 53  ename );.  REGIS
1a86c 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
1a86d 31 2c 20 70 4e 61 6d 65 29 3b 0a 0a 20 20 53 74  1, pName);..  St
1a86e 72 69 6e 67 69 66 79 28 70 4e 61 6d 65 2c 20 65  ringify(pName, e
1a86f 6e 63 6f 64 69 6e 67 29 3b 0a 0a 20 20 69 66 28  ncoding);..  if(
1a870 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
1a871 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  f(db) ) goto abo
1a872 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1a873 3b 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 4c  ;.  sqlite3VtabL
1a874 6f 63 6b 28 70 56 74 61 62 29 3b 0a 20 20 72 63  ock(pVtab);.  rc
1a875 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c   = pVtab->pModul
1a876 65 2d 3e 78 52 65 6e 61 6d 65 28 70 56 74 61 62  e->xRename(pVtab
1a877 2c 20 70 4e 61 6d 65 2d 3e 7a 29 3b 0a 20 20 73  , pName->z);.  s
1a878 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1a879 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20   p->zErrMsg);.  
1a87a 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56 74  p->zErrMsg = pVt
1a87b 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 70  ab->zErrMsg;.  p
1a87c 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  Vtab->zErrMsg = 
1a87d 30 3b 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62  0;.  sqlite3Vtab
1a87e 55 6e 6c 6f 63 6b 28 64 62 2c 20 70 56 74 61 62  Unlock(db, pVtab
1a87f 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
1a880 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67  SafetyOn(db) ) g
1a881 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1a882 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 62 72 65 61  _misuse;..  brea
1a883 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  k;.}.#endif..#if
1a884 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1a885 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a  _VIRTUALTABLE./*
1a886 20 4f 70 63 6f 64 65 3a 20 56 55 70 64 61 74 65   Opcode: VUpdate
1a887 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a   P1 P2 P3 P4 *.*
1a888 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69  *.** P4 is a poi
1a889 6e 74 65 72 20 74 6f 20 61 20 76 69 72 74 75 61  nter to a virtua
1a88a 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20  l table object, 
1a88b 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  an sqlite3_vtab 
1a88c 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68  structure..** Th
1a88d 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65  is opcode invoke
1a88e 73 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  s the correspond
1a88f 69 6e 67 20 78 55 70 64 61 74 65 20 6d 65 74 68  ing xUpdate meth
1a890 6f 64 2e 20 50 32 20 76 61 6c 75 65 73 0a 2a 2a  od. P2 values.**
1a891 20 61 72 65 20 63 6f 6e 74 69 67 75 6f 75 73 20   are contiguous 
1a892 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 73 74 61  memory cells sta
1a893 72 74 69 6e 67 20 61 74 20 50 33 20 74 6f 20 70  rting at P3 to p
1a894 61 73 73 20 74 6f 20 74 68 65 20 78 55 70 64 61  ass to the xUpda
1a895 74 65 20 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f  te .** invocatio
1a896 6e 2e 20 54 68 65 20 76 61 6c 75 65 20 69 6e 20  n. The value in 
1a897 72 65 67 69 73 74 65 72 20 28 50 33 2b 50 32 2d  register (P3+P2-
1a898 31 29 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74  1) corresponds t
1a899 6f 20 74 68 65 20 0a 2a 2a 20 70 32 74 68 20 65  o the .** p2th e
1a89a 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 72  lement of the ar
1a89b 67 76 20 61 72 72 61 79 20 70 61 73 73 65 64 20  gv array passed 
1a89c 74 6f 20 78 55 70 64 61 74 65 2e 0a 2a 2a 0a 2a  to xUpdate..**.*
1a89d 2a 20 54 68 65 20 78 55 70 64 61 74 65 20 6d 65  * The xUpdate me
1a89e 74 68 6f 64 20 77 69 6c 6c 20 64 6f 20 61 20 44  thod will do a D
1a89f 45 4c 45 54 45 20 6f 72 20 61 6e 20 49 4e 53 45  ELETE or an INSE
1a8a0 52 54 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20 54  RT or both..** T
1a8a1 68 65 20 61 72 67 76 5b 30 5d 20 65 6c 65 6d 65  he argv[0] eleme
1a8a2 6e 74 20 28 77 68 69 63 68 20 63 6f 72 72 65 73  nt (which corres
1a8a3 70 6f 6e 64 73 20 74 6f 20 6d 65 6d 6f 72 79 20  ponds to memory 
1a8a4 63 65 6c 6c 20 50 33 29 0a 2a 2a 20 69 73 20 74  cell P3).** is t
1a8a5 68 65 20 72 6f 77 69 64 20 6f 66 20 61 20 72 6f  he rowid of a ro
1a8a6 77 20 74 6f 20 64 65 6c 65 74 65 2e 20 20 49 66  w to delete.  If
1a8a7 20 61 72 67 76 5b 30 5d 20 69 73 20 4e 55 4c 4c   argv[0] is NULL
1a8a8 20 74 68 65 6e 20 6e 6f 20 0a 2a 2a 20 64 65 6c   then no .** del
1a8a9 65 74 69 6f 6e 20 6f 63 63 75 72 73 2e 20 20 54  etion occurs.  T
1a8aa 68 65 20 61 72 67 76 5b 31 5d 20 65 6c 65 6d 65  he argv[1] eleme
1a8ab 6e 74 20 69 73 20 74 68 65 20 72 6f 77 69 64 20  nt is the rowid 
1a8ac 6f 66 20 74 68 65 20 6e 65 77 20 0a 2a 2a 20 72  of the new .** r
1a8ad 6f 77 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65  ow.  This can be
1a8ae 20 4e 55 4c 4c 20 74 6f 20 68 61 76 65 20 74 68   NULL to have th
1a8af 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
1a8b0 73 65 6c 65 63 74 20 74 68 65 20 6e 65 77 20 0a  select the new .
1a8b1 2a 2a 20 72 6f 77 69 64 20 66 6f 72 20 69 74 73  ** rowid for its
1a8b2 65 6c 66 2e 20 20 54 68 65 20 73 75 62 73 65 71  elf.  The subseq
1a8b3 75 65 6e 74 20 65 6c 65 6d 65 6e 74 73 20 69 6e  uent elements in
1a8b4 20 74 68 65 20 61 72 72 61 79 20 61 72 65 20 0a   the array are .
1a8b5 2a 2a 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66  ** the values of
1a8b6 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
1a8b7 6e 65 77 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 49  new row..**.** I
1a8b8 66 20 50 32 3d 3d 31 20 74 68 65 6e 20 6e 6f 20  f P2==1 then no 
1a8b9 69 6e 73 65 72 74 20 69 73 20 70 65 72 66 6f 72  insert is perfor
1a8ba 6d 65 64 2e 20 20 61 72 67 76 5b 30 5d 20 69 73  med.  argv[0] is
1a8bb 20 74 68 65 20 72 6f 77 69 64 20 6f 66 0a 2a 2a   the rowid of.**
1a8bc 20 61 20 72 6f 77 20 74 6f 20 64 65 6c 65 74 65   a row to delete
1a8bd 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20  ..**.** P1 is a 
1a8be 62 6f 6f 6c 65 61 6e 20 66 6c 61 67 2e 20 49 66  boolean flag. If
1a8bf 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 74 72   it is set to tr
1a8c0 75 65 20 61 6e 64 20 74 68 65 20 78 55 70 64 61  ue and the xUpda
1a8c1 74 65 20 63 61 6c 6c 0a 2a 2a 20 69 73 20 73 75  te call.** is su
1a8c2 63 63 65 73 73 66 75 6c 2c 20 74 68 65 6e 20 74  ccessful, then t
1a8c3 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
1a8c4 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6c 61 73  d by sqlite3_las
1a8c5 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29  t_insert_rowid()
1a8c6 20 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74   .** is set to t
1a8c7 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
1a8c8 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 72 6f  rowid for the ro
1a8c9 77 20 6a 75 73 74 20 69 6e 73 65 72 74 65 64 2e  w just inserted.
1a8ca 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 55 70 64  .*/.case OP_VUpd
1a8cb 61 74 65 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33  ate: {.  sqlite3
1a8cc 5f 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20 70  _vtab *pVtab = p
1a8cd 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3b 0a 20 20  Op->p4.pVtab;.  
1a8ce 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a  sqlite3_module *
1a8cf 70 4d 6f 64 75 6c 65 20 3d 20 28 73 71 6c 69 74  pModule = (sqlit
1a8d0 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 70 56 74 61  e3_module *)pVta
1a8d1 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e  b->pModule;.  in
1a8d2 74 20 6e 41 72 67 20 3d 20 70 4f 70 2d 3e 70 32  t nArg = pOp->p2
1a8d3 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1a8d4 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56 54 41 42  >p4type==P4_VTAB
1a8d5 20 29 3b 0a 20 20 69 66 28 20 70 4d 6f 64 75 6c   );.  if( pModul
1a8d6 65 2d 3e 78 55 70 64 61 74 65 3d 3d 30 20 29 7b  e->xUpdate==0 ){
1a8d7 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
1a8d8 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
1a8d9 67 2c 20 64 62 2c 20 22 72 65 61 64 2d 6f 6e 6c  g, db, "read-onl
1a8da 79 20 74 61 62 6c 65 22 29 3b 0a 20 20 20 20 72  y table");.    r
1a8db 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
1a8dc 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
1a8dd 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c 69 74 65  nt i;.    sqlite
1a8de 5f 69 6e 74 36 34 20 72 6f 77 69 64 3b 0a 20 20  _int64 rowid;.  
1a8df 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 20 3d 20    Mem **apArg = 
1a8e0 70 2d 3e 61 70 41 72 67 3b 0a 20 20 20 20 4d 65  p->apArg;.    Me
1a8e1 6d 20 2a 70 58 20 3d 20 26 70 2d 3e 61 4d 65 6d  m *pX = &p->aMem
1a8e2 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 66  [pOp->p3];.    f
1a8e3 6f 72 28 69 3d 30 3b 20 69 3c 6e 41 72 67 3b 20  or(i=0; i<nArg; 
1a8e4 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 74 6f 72  i++){.      stor
1a8e5 65 54 79 70 65 49 6e 66 6f 28 70 58 2c 20 30 29  eTypeInfo(pX, 0)
1a8e6 3b 0a 20 20 20 20 20 20 61 70 41 72 67 5b 69 5d  ;.      apArg[i]
1a8e7 20 3d 20 70 58 3b 0a 20 20 20 20 20 20 70 58 2b   = pX;.      pX+
1a8e8 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  +;.    }.    if(
1a8e9 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
1a8ea 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  f(db) ) goto abo
1a8eb 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1a8ec 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74 61  ;.    sqlite3Vta
1a8ed 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b 0a 20 20  bLock(pVtab);.  
1a8ee 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e    rc = pModule->
1a8ef 78 55 70 64 61 74 65 28 70 56 74 61 62 2c 20 6e  xUpdate(pVtab, n
1a8f0 41 72 67 2c 20 61 70 41 72 67 2c 20 26 72 6f 77  Arg, apArg, &row
1a8f1 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  id);.    sqlite3
1a8f2 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45  DbFree(db, p->zE
1a8f3 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a  rrMsg);.    p->z
1a8f4 45 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e  ErrMsg = pVtab->
1a8f5 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 70 56 74  zErrMsg;.    pVt
1a8f6 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b  ab->zErrMsg = 0;
1a8f7 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62  .    sqlite3Vtab
1a8f8 55 6e 6c 6f 63 6b 28 64 62 2c 20 70 56 74 61 62  Unlock(db, pVtab
1a8f9 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  );.    if( sqlit
1a8fa 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29  e3SafetyOn(db) )
1a8fb 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1a8fc 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 69  to_misuse;.    i
1a8fd 66 28 20 70 4f 70 2d 3e 70 31 20 26 26 20 72 63  f( pOp->p1 && rc
1a8fe 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1a8ff 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 41 72       assert( nAr
1a900 67 3e 31 20 26 26 20 61 70 41 72 67 5b 30 5d 20  g>1 && apArg[0] 
1a901 26 26 20 28 61 70 41 72 67 5b 30 5d 2d 3e 66 6c  && (apArg[0]->fl
1a902 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 29 20 29 3b  ags&MEM_Null) );
1a903 0a 20 20 20 20 20 20 64 62 2d 3e 6c 61 73 74 52  .      db->lastR
1a904 6f 77 69 64 20 3d 20 72 6f 77 69 64 3b 0a 20 20  owid = rowid;.  
1a905 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 43 68 61 6e    }.    p->nChan
1a906 67 65 2b 2b 3b 0a 20 20 7d 0a 20 20 62 72 65 61  ge++;.  }.  brea
1a907 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  k;.}.#endif /* S
1a908 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1a909 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e  ALTABLE */..#ifn
1a90a 64 65 66 20 20 53 51 4c 49 54 45 5f 4f 4d 49 54  def  SQLITE_OMIT
1a90b 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 2f  _PAGER_PRAGMAS./
1a90c 2a 20 4f 70 63 6f 64 65 3a 20 50 61 67 65 63 6f  * Opcode: Pageco
1a90d 75 6e 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  unt P1 P2 * * *.
1a90e 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20  **.** Write the 
1a90f 63 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f  current number o
1a910 66 20 70 61 67 65 73 20 69 6e 20 64 61 74 61 62  f pages in datab
1a911 61 73 65 20 50 31 20 74 6f 20 6d 65 6d 6f 72 79  ase P1 to memory
1a912 20 63 65 6c 6c 20 50 32 2e 0a 2a 2f 0a 63 61 73   cell P2..*/.cas
1a913 65 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 3a 20  e OP_Pagecount: 
1a914 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  {            /* 
1a915 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
1a916 2a 2f 0a 20 20 69 6e 74 20 70 31 20 3d 20 70 4f  */.  int p1 = pO
1a917 70 2d 3e 70 31 3b 20 0a 20 20 69 6e 74 20 6e 50  p->p1; .  int nP
1a918 61 67 65 3b 0a 20 20 50 61 67 65 72 20 2a 70 50  age;.  Pager *pP
1a919 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74  ager = sqlite3Bt
1a91a 72 65 65 50 61 67 65 72 28 64 62 2d 3e 61 44 62  reePager(db->aDb
1a91b 5b 70 31 5d 2e 70 42 74 29 3b 0a 0a 20 20 72 63  [p1].pBt);..  rc
1a91c 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50   = sqlite3PagerP
1a91d 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c  agecount(pPager,
1a91e 20 26 6e 50 61 67 65 29 3b 0a 20 20 69 66 28 20   &nPage);.  if( 
1a91f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1a920 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73  .    pOut->flags
1a921 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20   = MEM_Int;.    
1a922 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 6e 50 61 67  pOut->u.i = nPag
1a923 65 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  e;.  }.  break;.
1a924 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  }.#endif..#ifnde
1a925 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
1a926 41 43 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54  ACE./* Opcode: T
1a927 72 61 63 65 20 2a 20 2a 20 2a 20 50 34 20 2a 0a  race * * * P4 *.
1a928 2a 2a 0a 2a 2a 20 49 66 20 74 72 61 63 69 6e 67  **.** If tracing
1a929 20 69 73 20 65 6e 61 62 6c 65 64 20 28 62 79 20   is enabled (by 
1a92a 74 68 65 20 73 71 6c 69 74 65 33 5f 74 72 61 63  the sqlite3_trac
1a92b 65 28 29 29 20 69 6e 74 65 72 66 61 63 65 2c 20  e()) interface, 
1a92c 74 68 65 6e 0a 2a 2a 20 74 68 65 20 55 54 46 2d  then.** the UTF-
1a92d 38 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e  8 string contain
1a92e 65 64 20 69 6e 20 50 34 20 69 73 20 65 6d 69 74  ed in P4 is emit
1a92f 74 65 64 20 6f 6e 20 74 68 65 20 74 72 61 63 65  ted on the trace
1a930 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 63 61   callback..*/.ca
1a931 73 65 20 4f 50 5f 54 72 61 63 65 3a 20 7b 0a 20  se OP_Trace: {. 
1a932 20 69 66 28 20 70 4f 70 2d 3e 70 34 2e 7a 20 29   if( pOp->p4.z )
1a933 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 54  {.    if( db->xT
1a934 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 64 62  race ){.      db
1a935 2d 3e 78 54 72 61 63 65 28 64 62 2d 3e 70 54 72  ->xTrace(db->pTr
1a936 61 63 65 41 72 67 2c 20 70 4f 70 2d 3e 70 34 2e  aceArg, pOp->p4.
1a937 7a 29 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66  z);.    }.#ifdef
1a938 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
1a939 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73    if( (db->flags
1a93a 20 26 20 53 51 4c 49 54 45 5f 53 71 6c 54 72 61   & SQLITE_SqlTra
1a93b 63 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  ce)!=0 ){.      
1a93c 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
1a93d 74 66 28 22 53 51 4c 2d 74 72 61 63 65 3a 20 25  tf("SQL-trace: %
1a93e 73 5c 6e 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29  s\n", pOp->p4.z)
1a93f 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f  ;.    }.#endif /
1a940 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2a  * SQLITE_DEBUG *
1a941 2f 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  /.  }.  break;.}
1a942 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 20 4f 70 63  .#endif.../* Opc
1a943 6f 64 65 3a 20 4e 6f 6f 70 20 2a 20 2a 20 2a 20  ode: Noop * * * 
1a944 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 6f 20 6e 6f 74  * *.**.** Do not
1a945 68 69 6e 67 2e 20 20 54 68 69 73 20 69 6e 73 74  hing.  This inst
1a946 72 75 63 74 69 6f 6e 20 69 73 20 6f 66 74 65 6e  ruction is often
1a947 20 75 73 65 66 75 6c 20 61 73 20 61 20 6a 75 6d   useful as a jum
1a948 70 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e  p.** destination
1a949 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d  ..*/./*.** The m
1a94a 61 67 69 63 20 45 78 70 6c 61 69 6e 20 6f 70 63  agic Explain opc
1a94b 6f 64 65 20 61 72 65 20 6f 6e 6c 79 20 69 6e 73  ode are only ins
1a94c 65 72 74 65 64 20 77 68 65 6e 20 65 78 70 6c 61  erted when expla
1a94d 69 6e 3d 3d 32 20 28 77 68 69 63 68 0a 2a 2a 20  in==2 (which.** 
1a94e 69 73 20 74 6f 20 73 61 79 20 77 68 65 6e 20 74  is to say when t
1a94f 68 65 20 45 58 50 4c 41 49 4e 20 51 55 45 52 59  he EXPLAIN QUERY
1a950 20 50 4c 41 4e 20 73 79 6e 74 61 78 20 69 73 20   PLAN syntax is 
1a951 75 73 65 64 2e 29 0a 2a 2a 20 54 68 69 73 20 6f  used.).** This o
1a952 70 63 6f 64 65 20 72 65 63 6f 72 64 73 20 69 6e  pcode records in
1a953 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74  formation from t
1a954 68 65 20 6f 70 74 69 6d 69 7a 65 72 2e 20 20 49  he optimizer.  I
1a955 74 20 69 73 20 74 68 65 0a 2a 2a 20 74 68 65 20  t is the.** the 
1a956 73 61 6d 65 20 61 73 20 61 20 6e 6f 2d 6f 70 2e  same as a no-op.
1a957 20 20 54 68 69 73 20 6f 70 63 6f 64 65 73 6e 65    This opcodesne
1a958 76 65 72 20 61 70 70 65 61 72 73 20 69 6e 20 61  ver appears in a
1a959 20 72 65 61 6c 20 56 4d 20 70 72 6f 67 72 61 6d   real VM program
1a95a 2e 0a 2a 2f 0a 64 65 66 61 75 6c 74 3a 20 7b 20  ..*/.default: { 
1a95b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73           /* This
1a95c 20 69 73 20 72 65 61 6c 6c 79 20 4f 50 5f 4e 6f   is really OP_No
1a95d 6f 70 20 61 6e 64 20 4f 50 5f 45 78 70 6c 61 69  op and OP_Explai
1a95e 6e 20 2a 2f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  n */.  break;.}.
1a95f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1a960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a961 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a962 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a963 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1a964 2a 2a 20 54 68 65 20 63 61 73 65 73 20 6f 66 20  ** The cases of 
1a965 74 68 65 20 73 77 69 74 63 68 20 73 74 61 74 65  the switch state
1a966 6d 65 6e 74 20 61 62 6f 76 65 20 74 68 69 73 20  ment above this 
1a967 6c 69 6e 65 20 73 68 6f 75 6c 64 20 61 6c 6c 20  line should all 
1a968 62 65 20 69 6e 64 65 6e 74 65 64 0a 2a 2a 20 62  be indented.** b
1a969 79 20 36 20 73 70 61 63 65 73 2e 20 20 42 75 74  y 6 spaces.  But
1a96a 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 36   the left-most 6
1a96b 20 73 70 61 63 65 73 20 68 61 76 65 20 62 65 65   spaces have bee
1a96c 6e 20 72 65 6d 6f 76 65 64 20 74 6f 20 69 6d 70  n removed to imp
1a96d 72 6f 76 65 20 74 68 65 0a 2a 2a 20 72 65 61 64  rove the.** read
1a96e 61 62 69 6c 69 74 79 2e 20 20 46 72 6f 6d 20 74  ability.  From t
1a96f 68 69 73 20 70 6f 69 6e 74 20 6f 6e 20 64 6f 77  his point on dow
1a970 6e 2c 20 74 68 65 20 6e 6f 72 6d 61 6c 20 69 6e  n, the normal in
1a971 64 65 6e 74 61 74 69 6f 6e 20 72 75 6c 65 73 20  dentation rules 
1a972 61 72 65 0a 2a 2a 20 72 65 73 74 6f 72 65 64 2e  are.** restored.
1a973 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
1a974 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a975 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a976 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a977 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1a978 20 20 20 20 7d 0a 0a 23 69 66 64 65 66 20 56 44      }..#ifdef VD
1a979 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 20 20 7b  BE_PROFILE.    {
1a97a 0a 20 20 20 20 20 20 75 36 34 20 65 6c 61 70 73  .      u64 elaps
1a97b 65 64 20 3d 20 73 71 6c 69 74 65 33 48 77 74 69  ed = sqlite3Hwti
1a97c 6d 65 28 29 20 2d 20 73 74 61 72 74 3b 0a 20 20  me() - start;.  
1a97d 20 20 20 20 70 4f 70 2d 3e 63 79 63 6c 65 73 20      pOp->cycles 
1a97e 2b 3d 20 65 6c 61 70 73 65 64 3b 0a 20 20 20 20  += elapsed;.    
1a97f 20 20 70 4f 70 2d 3e 63 6e 74 2b 2b 3b 0a 23 69    pOp->cnt++;.#i
1a980 66 20 30 0a 20 20 20 20 20 20 20 20 66 70 72 69  f 0.        fpri
1a981 6e 74 66 28 73 74 64 6f 75 74 2c 20 22 25 31 30  ntf(stdout, "%10
1a982 6c 6c 75 20 22 2c 20 65 6c 61 70 73 65 64 29 3b  llu ", elapsed);
1a983 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1a984 56 64 62 65 50 72 69 6e 74 4f 70 28 73 74 64 6f  VdbePrintOp(stdo
1a985 75 74 2c 20 6f 72 69 67 50 63 2c 20 26 70 2d 3e  ut, origPc, &p->
1a986 61 4f 70 5b 6f 72 69 67 50 63 5d 29 3b 0a 23 65  aOp[origPc]);.#e
1a987 6e 64 69 66 0a 20 20 20 20 7d 0a 23 65 6e 64 69  ndif.    }.#endi
1a988 66 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f  f..    /* The fo
1a989 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 61 64 64  llowing code add
1a98a 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 65  s nothing to the
1a98b 20 61 63 74 75 61 6c 20 66 75 6e 63 74 69 6f 6e   actual function
1a98c 61 6c 69 74 79 0a 20 20 20 20 2a 2a 20 6f 66 20  ality.    ** of 
1a98d 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 49 74  the program.  It
1a98e 20 69 73 20 6f 6e 6c 79 20 68 65 72 65 20 66 6f   is only here fo
1a98f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65  r testing and de
1a990 62 75 67 67 69 6e 67 2e 0a 20 20 20 20 2a 2a 20  bugging..    ** 
1a991 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e  On the other han
1a992 64 2c 20 69 74 20 64 6f 65 73 20 62 75 72 6e 20  d, it does burn 
1a993 43 50 55 20 63 79 63 6c 65 73 20 65 76 65 72 79  CPU cycles every
1a994 20 74 69 6d 65 20 74 68 72 6f 75 67 68 0a 20 20   time through.  
1a995 20 20 2a 2a 20 74 68 65 20 65 76 61 6c 75 61 74    ** the evaluat
1a996 6f 72 20 6c 6f 6f 70 2e 20 20 53 6f 20 77 65 20  or loop.  So we 
1a997 63 61 6e 20 6c 65 61 76 65 20 69 74 20 6f 75 74  can leave it out
1a998 20 77 68 65 6e 20 4e 44 45 42 55 47 20 69 73 20   when NDEBUG is 
1a999 64 65 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a  defined..    */.
1a99a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
1a99b 20 20 20 61 73 73 65 72 74 28 20 70 63 3e 3d 2d     assert( pc>=-
1a99c 31 20 26 26 20 70 63 3c 70 2d 3e 6e 4f 70 20 29  1 && pc<p->nOp )
1a99d 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
1a99e 5f 44 45 42 55 47 0a 20 20 20 20 69 66 28 20 70  _DEBUG.    if( p
1a99f 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20  ->trace ){.     
1a9a0 20 69 66 28 20 72 63 21 3d 30 20 29 20 66 70 72   if( rc!=0 ) fpr
1a9a1 69 6e 74 66 28 70 2d 3e 74 72 61 63 65 2c 22 72  intf(p->trace,"r
1a9a2 63 3d 25 64 5c 6e 22 2c 72 63 29 3b 0a 20 20 20  c=%d\n",rc);.   
1a9a3 20 20 20 69 66 28 20 6f 70 50 72 6f 70 65 72 74     if( opPropert
1a9a4 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50  y & OPFLG_OUT2_P
1a9a5 52 45 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 20  RERELEASE ){.   
1a9a6 20 20 20 20 20 72 65 67 69 73 74 65 72 54 72 61       registerTra
1a9a7 63 65 28 70 2d 3e 74 72 61 63 65 2c 20 70 4f 70  ce(p->trace, pOp
1a9a8 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 20  ->p2, pOut);.   
1a9a9 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6f     }.      if( o
1a9aa 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c  pProperty & OPFL
1a9ab 47 5f 4f 55 54 33 20 29 7b 0a 20 20 20 20 20 20  G_OUT3 ){.      
1a9ac 20 20 72 65 67 69 73 74 65 72 54 72 61 63 65 28    registerTrace(
1a9ad 70 2d 3e 74 72 61 63 65 2c 20 70 4f 70 2d 3e 70  p->trace, pOp->p
1a9ae 33 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 20 20  3, pOut);.      
1a9af 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 20  }.    }.#endif  
1a9b0 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  /* SQLITE_DEBUG 
1a9b1 2a 2f 0a 23 65 6e 64 69 66 20 20 2f 2a 20 4e 44  */.#endif  /* ND
1a9b2 45 42 55 47 20 2a 2f 0a 20 20 7d 20 20 2f 2a 20  EBUG */.  }  /* 
1a9b3 54 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66  The end of the f
1a9b4 6f 72 28 3b 3b 29 20 6c 6f 6f 70 20 74 68 65 20  or(;;) loop the 
1a9b5 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 6f 70  loops through op
1a9b6 63 6f 64 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 49  codes */..  /* I
1a9b7 66 20 77 65 20 72 65 61 63 68 20 74 68 69 73 20  f we reach this 
1a9b8 70 6f 69 6e 74 2c 20 69 74 20 6d 65 61 6e 73 20  point, it means 
1a9b9 74 68 61 74 20 65 78 65 63 75 74 69 6f 6e 20 69  that execution i
1a9ba 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 0a  s finished with.
1a9bb 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 66    ** an error of
1a9bc 20 73 6f 6d 65 20 6b 69 6e 64 2e 0a 20 20 2a 2f   some kind..  */
1a9bd 0a 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74  .vdbe_error_halt
1a9be 3a 0a 20 20 61 73 73 65 72 74 28 20 72 63 20 29  :.  assert( rc )
1a9bf 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a  ;.  p->rc = rc;.
1a9c0 20 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c    sqlite3VdbeHal
1a9c1 74 28 70 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  t(p);.  if( rc==
1a9c2 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d  SQLITE_IOERR_NOM
1a9c3 45 4d 20 29 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  EM ) db->mallocF
1a9c4 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 72 63 20  ailed = 1;.  rc 
1a9c5 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
1a9c6 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68  .  /* This is th
1a9c7 65 20 6f 6e 6c 79 20 77 61 79 20 6f 75 74 20 6f  e only way out o
1a9c8 66 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 65  f this procedure
1a9c9 2e 20 20 57 65 20 68 61 76 65 20 74 6f 0a 20 20  .  We have to.  
1a9ca 2a 2a 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d  ** release the m
1a9cb 75 74 65 78 65 73 20 6f 6e 20 62 74 72 65 65 73  utexes on btrees
1a9cc 20 74 68 61 74 20 77 65 72 65 20 61 63 71 75 69   that were acqui
1a9cd 72 65 64 20 61 74 20 74 68 65 0a 20 20 2a 2a 20  red at the.  ** 
1a9ce 74 6f 70 2e 20 2a 2f 0a 76 64 62 65 5f 72 65 74  top. */.vdbe_ret
1a9cf 75 72 6e 3a 0a 20 20 73 71 6c 69 74 65 33 42 74  urn:.  sqlite3Bt
1a9d0 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61  reeMutexArrayLea
1a9d1 76 65 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a  ve(&p->aMutex);.
1a9d2 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20    return rc;..  
1a9d3 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20  /* Jump to here 
1a9d4 69 66 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62  if a string or b
1a9d5 6c 6f 62 20 6c 61 72 67 65 72 20 74 68 61 6e 20  lob larger than 
1a9d6 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54  SQLITE_MAX_LENGT
1a9d7 48 0a 20 20 2a 2a 20 69 73 20 65 6e 63 6f 75 6e  H.  ** is encoun
1a9d8 74 65 72 65 64 2e 0a 20 20 2a 2f 0a 74 6f 6f 5f  tered..  */.too_
1a9d9 62 69 67 3a 0a 20 20 73 71 6c 69 74 65 33 53 65  big:.  sqlite3Se
1a9da 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72  tString(&p->zErr
1a9db 4d 73 67 2c 20 64 62 2c 20 22 73 74 72 69 6e 67  Msg, db, "string
1a9dc 20 6f 72 20 62 6c 6f 62 20 74 6f 6f 20 62 69 67   or blob too big
1a9dd 22 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54  ");.  rc = SQLIT
1a9de 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 67 6f 74 6f  E_TOOBIG;.  goto
1a9df 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74   vdbe_error_halt
1a9e0 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20  ;..  /* Jump to 
1a9e1 68 65 72 65 20 69 66 20 61 20 6d 61 6c 6c 6f 63  here if a malloc
1a9e2 28 29 20 66 61 69 6c 73 2e 0a 20 20 2a 2f 0a 6e  () fails..  */.n
1a9e3 6f 5f 6d 65 6d 3a 0a 20 20 64 62 2d 3e 6d 61 6c  o_mem:.  db->mal
1a9e4 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
1a9e5 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
1a9e6 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64  g(&p->zErrMsg, d
1a9e7 62 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72  b, "out of memor
1a9e8 79 22 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49  y");.  rc = SQLI
1a9e9 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 67 6f 74 6f  TE_NOMEM;.  goto
1a9ea 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74   vdbe_error_halt
1a9eb 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20  ;..  /* Jump to 
1a9ec 68 65 72 65 20 66 6f 72 20 61 6e 20 53 51 4c 49  here for an SQLI
1a9ed 54 45 5f 4d 49 53 55 53 45 20 65 72 72 6f 72 2e  TE_MISUSE error.
1a9ee 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f  .  */.abort_due_
1a9ef 74 6f 5f 6d 69 73 75 73 65 3a 0a 20 20 72 63 20  to_misuse:.  rc 
1a9f0 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  = SQLITE_MISUSE;
1a9f1 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20  .  /* Fall thru 
1a9f2 69 6e 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  into abort_due_t
1a9f3 6f 5f 65 72 72 6f 72 20 2a 2f 0a 0a 20 20 2f 2a  o_error */..  /*
1a9f4 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 66 6f   Jump to here fo
1a9f5 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64  r any other kind
1a9f6 20 6f 66 20 66 61 74 61 6c 20 65 72 72 6f 72 2e   of fatal error.
1a9f7 20 20 54 68 65 20 22 72 63 22 20 76 61 72 69 61    The "rc" varia
1a9f8 62 6c 65 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20  ble.  ** should 
1a9f9 68 6f 6c 64 20 74 68 65 20 65 72 72 6f 72 20 6e  hold the error n
1a9fa 75 6d 62 65 72 2e 0a 20 20 2a 2f 0a 61 62 6f 72  umber..  */.abor
1a9fb 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3a 0a  t_due_to_error:.
1a9fc 20 20 61 73 73 65 72 74 28 20 70 2d 3e 7a 45 72    assert( p->zEr
1a9fd 72 4d 73 67 3d 3d 30 20 29 3b 0a 20 20 69 66 28  rMsg==0 );.  if(
1a9fe 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1a9ff 64 20 29 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  d ) rc = SQLITE_
1aa00 4e 4f 4d 45 4d 3b 0a 20 20 69 66 28 20 72 63 21  NOMEM;.  if( rc!
1aa01 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f  =SQLITE_IOERR_NO
1aa02 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  MEM ){.    sqlit
1aa03 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e  e3SetString(&p->
1aa04 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73  zErrMsg, db, "%s
1aa05 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72  ", sqlite3ErrStr
1aa06 28 72 63 29 29 3b 0a 20 20 7d 0a 20 20 67 6f 74  (rc));.  }.  got
1aa07 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c  o vdbe_error_hal
1aa08 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f  t;..  /* Jump to
1aa09 20 68 65 72 65 20 69 66 20 74 68 65 20 73 71 6c   here if the sql
1aa0a 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29  ite3_interrupt()
1aa0b 20 41 50 49 20 73 65 74 73 20 74 68 65 20 69 6e   API sets the in
1aa0c 74 65 72 72 75 70 74 0a 20 20 2a 2a 20 66 6c 61  terrupt.  ** fla
1aa0d 67 2e 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75  g..  */.abort_du
1aa0e 65 5f 74 6f 5f 69 6e 74 65 72 72 75 70 74 3a 0a  e_to_interrupt:.
1aa0f 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 75 31    assert( db->u1
1aa10 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 29  .isInterrupted )
1aa11 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  ;.  rc = SQLITE_
1aa12 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 70 2d 3e  INTERRUPT;.  p->
1aa13 72 63 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69 74  rc = rc;.  sqlit
1aa14 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e  e3SetString(&p->
1aa15 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73  zErrMsg, db, "%s
1aa16 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72  ", sqlite3ErrStr
1aa17 28 72 63 29 29 3b 0a 20 20 67 6f 74 6f 20 76 64  (rc));.  goto vd
1aa18 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 7d  be_error_halt;.}
1aa19 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
1aa1a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 2e 63 20  * End of vdbe.c 
1aa1b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aa1c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aa1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aa1e 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
1aa1f 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62  * Begin file vdb
1aa20 65 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  eblob.c ********
1aa21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aa22 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aa23 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 79  /./*.** 2007 May
1aa24 20 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74   1.**.** The aut
1aa25 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
1aa26 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
1aa27 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
1aa28 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
1aa29 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
1aa2a 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
1aa2b 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
1aa2c 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
1aa2d 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
1aa2e 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
1aa2f 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
1aa30 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
1aa31 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
1aa32 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
1aa33 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
1aa34 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
1aa35 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
1aa36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aa37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aa38 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aa39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aa3a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
1aa3b 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
1aa3c 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
1aa3d 74 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 42 4c  t incremental BL
1aa3e 4f 42 20 49 2f 4f 2e 0a 2a 2a 0a 2a 2a 20 24 49  OB I/O..**.** $I
1aa3f 64 3a 20 76 64 62 65 62 6c 6f 62 2e 63 2c 76 20  d: vdbeblob.c,v 
1aa40 31 2e 32 36 20 32 30 30 38 2f 31 30 2f 30 32 20  1.26 2008/10/02 
1aa41 31 34 3a 34 39 3a 30 32 20 64 61 6e 69 65 6c 6b  14:49:02 danielk
1aa42 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a  1977 Exp $.*/...
1aa43 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1aa44 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 0a 2f 2a  MIT_INCRBLOB../*
1aa45 0a 2a 2a 20 56 61 6c 69 64 20 73 71 6c 69 74 65  .** Valid sqlite
1aa46 33 5f 62 6c 6f 62 2a 20 68 61 6e 64 6c 65 73 20  3_blob* handles 
1aa47 70 6f 69 6e 74 20 74 6f 20 49 6e 63 72 62 6c 6f  point to Incrblo
1aa48 62 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f  b structures..*/
1aa49 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
1aa4a 49 6e 63 72 62 6c 6f 62 20 49 6e 63 72 62 6c 6f  Incrblob Incrblo
1aa4b 62 3b 0a 73 74 72 75 63 74 20 49 6e 63 72 62 6c  b;.struct Incrbl
1aa4c 6f 62 20 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73  ob {.  int flags
1aa4d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1aa4e 2a 20 43 6f 70 79 20 6f 66 20 22 66 6c 61 67 73  * Copy of "flags
1aa4f 22 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69  " passed to sqli
1aa50 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 20  te3_blob_open() 
1aa51 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20  */.  int nByte; 
1aa52 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1aa53 53 69 7a 65 20 6f 66 20 6f 70 65 6e 20 62 6c 6f  Size of open blo
1aa54 62 2c 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  b, in bytes */. 
1aa55 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 20 20   int iOffset;   
1aa56 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65           /* Byte
1aa57 20 6f 66 66 73 65 74 20 6f 66 20 62 6c 6f 62 20   offset of blob 
1aa58 69 6e 20 63 75 72 73 6f 72 20 64 61 74 61 20 2a  in cursor data *
1aa59 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  /.  BtCursor *pC
1aa5a 73 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43  sr;         /* C
1aa5b 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61  ursor pointing a
1aa5c 74 20 62 6c 6f 62 20 72 6f 77 20 2a 2f 0a 20 20  t blob row */.  
1aa5d 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1aa5e 74 6d 74 3b 20 20 20 20 2f 2a 20 53 74 61 74 65  tmt;    /* State
1aa5f 6d 65 6e 74 20 68 6f 6c 64 69 6e 67 20 63 75 72  ment holding cur
1aa60 73 6f 72 20 6f 70 65 6e 20 2a 2f 0a 20 20 73 71  sor open */.  sq
1aa61 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
1aa62 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 73 73        /* The ass
1aa63 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65  ociated database
1aa64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4f 70   */.};../*.** Op
1aa65 65 6e 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65  en a blob handle
1aa66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1aa67 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  int sqlite3_blob
1aa68 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33  _open(.  sqlite3
1aa69 2a 20 64 62 2c 20 20 20 20 20 20 20 20 20 20 20  * db,           
1aa6a 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
1aa6b 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
1aa6c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
1aa6d 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ,        /* The 
1aa6e 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
1aa6f 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
1aa70 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74   blob */.  const
1aa71 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c 20 20   char *zTable,  
1aa72 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
1aa73 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 62  containing the b
1aa74 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  lob */.  const c
1aa75 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 2c 20 20 20  har *zColumn,   
1aa76 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 63   /* The column c
1aa77 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 62 6c  ontaining the bl
1aa78 6f 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69  ob */.  sqlite_i
1aa79 6e 74 36 34 20 69 52 6f 77 2c 20 20 20 20 20 20  nt64 iRow,      
1aa7a 2f 2a 20 54 68 65 20 72 6f 77 20 63 6f 6e 74 61  /* The row conta
1aa7b 69 6e 69 6e 67 20 74 68 65 20 67 6c 6f 62 20 2a  ining the glob *
1aa7c 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  /.  int flags,  
1aa7d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1aa7e 72 75 65 20 2d 3e 20 72 65 61 64 2f 77 72 69 74  rue -> read/writ
1aa7f 65 20 61 63 63 65 73 73 2c 20 66 61 6c 73 65 20  e access, false 
1aa80 2d 3e 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a  -> read-only */.
1aa81 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a    sqlite3_blob *
1aa82 2a 70 70 42 6c 6f 62 20 20 20 2f 2a 20 48 61 6e  *ppBlob   /* Han
1aa83 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e  dle for accessin
1aa84 67 20 74 68 65 20 62 6c 6f 62 20 72 65 74 75 72  g the blob retur
1aa85 6e 65 64 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  ned here */.){. 
1aa86 20 69 6e 74 20 6e 41 74 74 65 6d 70 74 20 3d 20   int nAttempt = 
1aa87 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20  0;.  int iCol;  
1aa88 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1aa89 49 6e 64 65 78 20 6f 66 20 7a 43 6f 6c 75 6d 6e  Index of zColumn
1aa8a 20 69 6e 20 72 6f 77 2d 72 65 63 6f 72 64 20 2a   in row-record *
1aa8b 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 56 44 42  /..  /* This VDB
1aa8c 45 20 70 72 6f 67 72 61 6d 20 73 65 65 6b 73 20  E program seeks 
1aa8d 61 20 62 74 72 65 65 20 63 75 72 73 6f 72 20 74  a btree cursor t
1aa8e 6f 20 74 68 65 20 69 64 65 6e 74 69 66 69 65 64  o the identified
1aa8f 20 0a 20 20 2a 2a 20 64 62 2f 74 61 62 6c 65 2f   .  ** db/table/
1aa90 72 6f 77 20 65 6e 74 72 79 2e 20 54 68 65 20 72  row entry. The r
1aa91 65 61 73 6f 6e 20 66 6f 72 20 75 73 69 6e 67 20  eason for using 
1aa92 61 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 69  a vdbe program i
1aa93 6e 73 74 65 61 64 0a 20 20 2a 2a 20 6f 66 20 77  nstead.  ** of w
1aa94 72 69 74 69 6e 67 20 63 6f 64 65 20 74 6f 20 75  riting code to u
1aa95 73 65 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61  se the b-tree la
1aa96 79 65 72 20 64 69 72 65 63 74 6c 79 20 69 73 20  yer directly is 
1aa97 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 76 64  that the.  ** vd
1aa98 62 65 20 70 72 6f 67 72 61 6d 20 77 69 6c 6c 20  be program will 
1aa99 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20 6f  take advantage o
1aa9a 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 74 72  f the various tr
1aa9b 61 6e 73 61 63 74 69 6f 6e 2c 0a 20 20 2a 2a 20  ansaction,.  ** 
1aa9c 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 65 72 72 6f  locking and erro
1aa9d 72 20 68 61 6e 64 6c 69 6e 67 20 69 6e 66 72 61  r handling infra
1aa9e 73 74 72 75 63 74 75 72 65 20 62 75 69 6c 74 20  structure built 
1aa9f 69 6e 74 6f 20 74 68 65 20 76 64 62 65 2e 0a 20  into the vdbe.. 
1aaa0 20 2a 2a 0a 20 20 2a 2a 20 41 66 74 65 72 20 73   **.  ** After s
1aaa1 65 65 6b 69 6e 67 20 74 68 65 20 63 75 72 73 6f  eeking the curso
1aaa2 72 2c 20 74 68 65 20 76 64 62 65 20 65 78 65 63  r, the vdbe exec
1aaa3 75 74 65 73 20 61 6e 20 4f 50 5f 52 65 73 75 6c  utes an OP_Resul
1aaa4 74 52 6f 77 2e 0a 20 20 2a 2a 20 43 6f 64 65 20  tRow..  ** Code 
1aaa5 65 78 74 65 72 6e 61 6c 20 74 6f 20 74 68 65 20  external to the 
1aaa6 56 64 62 65 20 74 68 65 6e 20 22 62 6f 72 72 6f  Vdbe then "borro
1aaa7 77 73 22 20 74 68 65 20 62 2d 74 72 65 65 20 63  ws" the b-tree c
1aaa8 75 72 73 6f 72 20 61 6e 64 0a 20 20 2a 2a 20 75  ursor and.  ** u
1aaa9 73 65 73 20 69 74 20 74 6f 20 69 6d 70 6c 65 6d  ses it to implem
1aaaa 65 6e 74 20 74 68 65 20 62 6c 6f 62 5f 72 65 61  ent the blob_rea
1aaab 64 28 29 2c 20 62 6c 6f 62 5f 77 72 69 74 65 28  d(), blob_write(
1aaac 29 20 61 6e 64 20 0a 20 20 2a 2a 20 62 6c 6f 62  ) and .  ** blob
1aaad 5f 62 79 74 65 73 28 29 20 66 75 6e 63 74 69 6f  _bytes() functio
1aaae 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  ns..  **.  ** Th
1aaaf 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63  e sqlite3_blob_c
1aab0 6c 6f 73 65 28 29 20 66 75 6e 63 74 69 6f 6e 20  lose() function 
1aab1 66 69 6e 61 6c 69 7a 65 73 20 74 68 65 20 76 64  finalizes the vd
1aab2 62 65 20 70 72 6f 67 72 61 6d 2c 0a 20 20 2a 2a  be program,.  **
1aab3 20 77 68 69 63 68 20 63 6c 6f 73 65 73 20 74 68   which closes th
1aab4 65 20 62 2d 74 72 65 65 20 63 75 72 73 6f 72 20  e b-tree cursor 
1aab5 61 6e 64 20 28 70 6f 73 73 69 62 6c 79 29 20 63  and (possibly) c
1aab6 6f 6d 6d 69 74 73 20 74 68 65 20 0a 20 20 2a 2a  ommits the .  **
1aab7 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20   transaction..  
1aab8 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  */.  static cons
1aab9 74 20 56 64 62 65 4f 70 4c 69 73 74 20 6f 70 65  t VdbeOpList ope
1aaba 6e 42 6c 6f 62 5b 5d 20 3d 20 7b 0a 20 20 20 20  nBlob[] = {.    
1aabb 7b 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c  {OP_Transaction,
1aabc 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 2f   0, 0, 0},     /
1aabd 2a 20 30 3a 20 53 74 61 72 74 20 61 20 74 72 61  * 0: Start a tra
1aabe 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20  nsaction */.    
1aabf 7b 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65  {OP_VerifyCookie
1aac0 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 2f  , 0, 0, 0},    /
1aac1 2a 20 31 3a 20 43 68 65 63 6b 20 74 68 65 20 73  * 1: Check the s
1aac2 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 2a 2f 0a  chema cookie */.
1aac3 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74  .    /* One of t
1aac4 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f  he following two
1aac5 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 69 73   instructions is
1aac6 20 72 65 70 6c 61 63 65 64 20 62 79 20 61 6e 0a   replaced by an.
1aac7 20 20 20 20 2a 2a 20 4f 50 5f 4e 6f 6f 70 20 62      ** OP_Noop b
1aac8 65 66 6f 72 65 20 65 78 65 63 74 69 6f 6e 2e 0a  efore exection..
1aac9 20 20 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 53      */.    {OP_S
1aaca 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c  etNumColumns, 0,
1aacb 20 30 2c 20 30 7d 2c 20 20 20 2f 2a 20 32 3a 20   0, 0},   /* 2: 
1aacc 4e 75 6d 20 63 6f 6c 73 20 66 6f 72 20 63 75 72  Num cols for cur
1aacd 73 6f 72 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4f  sor */.    {OP_O
1aace 70 65 6e 52 65 61 64 2c 20 30 2c 20 30 2c 20 30  penRead, 0, 0, 0
1aacf 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20 33 3a 20  },        /* 3: 
1aad0 4f 70 65 6e 20 63 75 72 73 6f 72 20 30 20 66 6f  Open cursor 0 fo
1aad1 72 20 72 65 61 64 69 6e 67 20 2a 2f 0a 20 20 20  r reading */.   
1aad2 20 7b 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d   {OP_SetNumColum
1aad3 6e 73 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20  ns, 0, 0, 0},   
1aad4 2f 2a 20 34 3a 20 4e 75 6d 20 63 6f 6c 73 20 66  /* 4: Num cols f
1aad5 6f 72 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20  or cursor */.   
1aad6 20 7b 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20   {OP_OpenWrite, 
1aad7 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20  0, 0, 0},       
1aad8 2f 2a 20 35 3a 20 4f 70 65 6e 20 63 75 72 73 6f  /* 5: Open curso
1aad9 72 20 30 20 66 6f 72 20 72 65 61 64 2f 77 72 69  r 0 for read/wri
1aada 74 65 20 2a 2f 0a 0a 20 20 20 20 7b 4f 50 5f 56  te */..    {OP_V
1aadb 61 72 69 61 62 6c 65 2c 20 31 2c 20 31 2c 20 30  ariable, 1, 1, 0
1aadc 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20 36 3a 20  },        /* 6: 
1aadd 50 75 73 68 20 74 68 65 20 72 6f 77 69 64 20 74  Push the rowid t
1aade 6f 20 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a 20  o the stack */. 
1aadf 20 20 20 7b 4f 50 5f 4e 6f 74 45 78 69 73 74 73     {OP_NotExists
1aae0 2c 20 30 2c 20 31 30 2c 20 31 7d 2c 20 20 20 20  , 0, 10, 1},    
1aae1 20 20 2f 2a 20 37 3a 20 53 65 65 6b 20 74 68 65    /* 7: Seek the
1aae2 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 7b   cursor */.    {
1aae3 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 2c  OP_Column, 0, 0,
1aae4 20 31 7d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a   1},          /*
1aae5 20 38 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 52   8  */.    {OP_R
1aae6 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 30 2c 20  esultRow, 1, 0, 
1aae7 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20 39 20 20  0},       /* 9  
1aae8 2a 2f 0a 20 20 20 20 7b 4f 50 5f 43 6c 6f 73 65  */.    {OP_Close
1aae9 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20  , 0, 0, 0},     
1aaea 20 20 20 20 20 20 2f 2a 20 31 30 20 20 2a 2f 0a        /* 10  */.
1aaeb 20 20 20 20 7b 4f 50 5f 48 61 6c 74 2c 20 30 2c      {OP_Halt, 0,
1aaec 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 20 20   0, 0},         
1aaed 20 20 20 2f 2a 20 31 31 20 2a 2f 0a 20 20 7d 3b     /* 11 */.  };
1aaee 0a 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 30 3b  ..  Vdbe *v = 0;
1aaef 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
1aaf0 54 45 5f 4f 4b 3b 0a 20 20 63 68 61 72 20 7a 45  TE_OK;.  char zE
1aaf1 72 72 5b 31 32 38 5d 3b 0a 0a 20 20 7a 45 72 72  rr[128];..  zErr
1aaf2 5b 30 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  [0] = 0;.  sqlit
1aaf3 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
1aaf4 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 6f 20  b->mutex);.  do 
1aaf5 7b 0a 20 20 20 20 50 61 72 73 65 20 73 50 61 72  {.    Parse sPar
1aaf6 73 65 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70  se;.    Table *p
1aaf7 54 61 62 3b 0a 0a 20 20 20 20 6d 65 6d 73 65 74  Tab;..    memset
1aaf8 28 26 73 50 61 72 73 65 2c 20 30 2c 20 73 69 7a  (&sParse, 0, siz
1aaf9 65 6f 66 28 50 61 72 73 65 29 29 3b 0a 20 20 20  eof(Parse));.   
1aafa 20 73 50 61 72 73 65 2e 64 62 20 3d 20 64 62 3b   sParse.db = db;
1aafb 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ..    if( sqlite
1aafc 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b  3SafetyOn(db) ){
1aafd 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
1aafe 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
1aaff 75 74 65 78 29 3b 0a 20 20 20 20 20 20 72 65 74  utex);.      ret
1ab00 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  urn SQLITE_MISUS
1ab01 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71  E;.    }..    sq
1ab02 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41  lite3BtreeEnterA
1ab03 6c 6c 28 64 62 29 3b 0a 20 20 20 20 70 54 61 62  ll(db);.    pTab
1ab04 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65   = sqlite3Locate
1ab05 54 61 62 6c 65 28 26 73 50 61 72 73 65 2c 20 30  Table(&sParse, 0
1ab06 2c 20 7a 54 61 62 6c 65 2c 20 7a 44 62 29 3b 0a  , zTable, zDb);.
1ab07 20 20 20 20 69 66 28 20 70 54 61 62 20 26 26 20      if( pTab && 
1ab08 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
1ab09 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20  ){.      pTab = 
1ab0a 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  0;.      sqlite3
1ab0b 45 72 72 6f 72 4d 73 67 28 26 73 50 61 72 73 65  ErrorMsg(&sParse
1ab0c 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76  , "cannot open v
1ab0d 69 72 74 75 61 6c 20 74 61 62 6c 65 3a 20 25 73  irtual table: %s
1ab0e 22 2c 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20  ", zTable);.    
1ab0f 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
1ab10 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 20 20 69  _OMIT_VIEW.    i
1ab11 66 28 20 70 54 61 62 20 26 26 20 70 54 61 62 2d  f( pTab && pTab-
1ab12 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20  >pSelect ){.    
1ab13 20 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20    pTab = 0;.    
1ab14 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1ab15 67 28 26 73 50 61 72 73 65 2c 20 22 63 61 6e 6e  g(&sParse, "cann
1ab16 6f 74 20 6f 70 65 6e 20 76 69 65 77 3a 20 25 73  ot open view: %s
1ab17 22 2c 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20  ", zTable);.    
1ab18 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28  }.#endif.    if(
1ab19 20 21 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20   !pTab ){.      
1ab1a 69 66 28 20 73 50 61 72 73 65 2e 7a 45 72 72 4d  if( sParse.zErrM
1ab1b 73 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  sg ){.        sq
1ab1c 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
1ab1d 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72  izeof(zErr), zEr
1ab1e 72 2c 20 22 25 73 22 2c 20 73 50 61 72 73 65 2e  r, "%s", sParse.
1ab1f 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20  zErrMsg);.      
1ab20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  }.      sqlite3D
1ab21 62 46 72 65 65 28 64 62 2c 20 73 50 61 72 73 65  bFree(db, sParse
1ab22 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20  .zErrMsg);.     
1ab23 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
1ab24 4f 52 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29  OR;.      (void)
1ab25 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
1ab26 28 64 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  (db);.      sqli
1ab27 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c  te3BtreeLeaveAll
1ab28 28 64 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f  (db);.      goto
1ab29 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a   blob_open_out;.
1ab2a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4e 6f      }..    /* No
1ab2b 77 20 73 65 61 72 63 68 20 70 54 61 62 20 66 6f  w search pTab fo
1ab2c 72 20 74 68 65 20 65 78 61 63 74 20 63 6f 6c 75  r the exact colu
1ab2d 6d 6e 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69  mn. */.    for(i
1ab2e 43 6f 6c 3d 30 3b 20 69 43 6f 6c 20 3c 20 70 54  Col=0; iCol < pT
1ab2f 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b  ab->nCol; iCol++
1ab30 29 20 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71  ) {.      if( sq
1ab31 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 54 61  lite3StrICmp(pTa
1ab32 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e  b->aCol[iCol].zN
1ab33 61 6d 65 2c 20 7a 43 6f 6c 75 6d 6e 29 3d 3d 30  ame, zColumn)==0
1ab34 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61   ){.        brea
1ab35 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
1ab36 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70  .    if( iCol==p
1ab37 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20  Tab->nCol ){.   
1ab38 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
1ab39 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29  ntf(sizeof(zErr)
1ab3a 2c 20 7a 45 72 72 2c 20 22 6e 6f 20 73 75 63 68  , zErr, "no such
1ab3b 20 63 6f 6c 75 6d 6e 3a 20 5c 22 25 73 5c 22 22   column: \"%s\""
1ab3c 2c 20 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  , zColumn);.    
1ab3d 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
1ab3e 52 4f 52 3b 0a 20 20 20 20 20 20 28 76 6f 69 64  ROR;.      (void
1ab3f 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66  )sqlite3SafetyOf
1ab40 66 28 64 62 29 3b 0a 20 20 20 20 20 20 73 71 6c  f(db);.      sql
1ab41 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c  ite3BtreeLeaveAl
1ab42 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 67 6f 74  l(db);.      got
1ab43 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b  o blob_open_out;
1ab44 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49  .    }..    /* I
1ab45 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 62  f the value is b
1ab46 65 69 6e 67 20 6f 70 65 6e 65 64 20 66 6f 72 20  eing opened for 
1ab47 77 72 69 74 69 6e 67 2c 20 63 68 65 63 6b 20 74  writing, check t
1ab48 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 63  hat the.    ** c
1ab49 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 69 6e 64  olumn is not ind
1ab4a 65 78 65 64 2e 20 49 74 20 69 73 20 61 67 61 69  exed. It is agai
1ab4b 6e 73 74 20 74 68 65 20 72 75 6c 65 73 20 74 6f  nst the rules to
1ab4c 20 6f 70 65 6e 20 61 6e 0a 20 20 20 20 2a 2a 20   open an.    ** 
1ab4d 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20 66  indexed column f
1ab4e 6f 72 20 77 72 69 74 69 6e 67 2e 0a 20 20 20 20  or writing..    
1ab4f 2a 2f 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73  */.    if( flags
1ab50 20 29 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20   ){.      Index 
1ab51 2a 70 49 64 78 3b 0a 20 20 20 20 20 20 66 6f 72  *pIdx;.      for
1ab52 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64  (pIdx=pTab->pInd
1ab53 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
1ab54 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Idx->pNext){.   
1ab55 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20       int j;.    
1ab56 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70      for(j=0; j<p
1ab57 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b  Idx->nColumn; j+
1ab58 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
1ab59 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  ( pIdx->aiColumn
1ab5a 5b 6a 5d 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20  [j]==iCol ){.   
1ab5b 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1ab5c 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66  _snprintf(sizeof
1ab5d 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 0a 20 20  (zErr), zErr,.  
1ab5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ab5f 20 20 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e             "cann
1ab60 6f 74 20 6f 70 65 6e 20 69 6e 64 65 78 65 64 20  ot open indexed 
1ab61 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 72 69 74 69  column for writi
1ab62 6e 67 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ng");.          
1ab63 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
1ab64 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ROR;.           
1ab65 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
1ab66 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20  fetyOff(db);.   
1ab67 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1ab68 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62  BtreeLeaveAll(db
1ab69 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67  );.            g
1ab6a 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75  oto blob_open_ou
1ab6b 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  t;.          }. 
1ab6c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1ab6d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 76 20 3d 20  .    }..    v = 
1ab6e 73 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 74  sqlite3VdbeCreat
1ab6f 65 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 76  e(db);.    if( v
1ab70 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44   ){.      int iD
1ab71 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
1ab72 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61  aToIndex(db, pTa
1ab73 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20  b->pSchema);.   
1ab74 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1ab75 64 4f 70 4c 69 73 74 28 76 2c 20 73 69 7a 65 6f  dOpList(v, sizeo
1ab76 66 28 6f 70 65 6e 42 6c 6f 62 29 2f 73 69 7a 65  f(openBlob)/size
1ab77 6f 66 28 56 64 62 65 4f 70 4c 69 73 74 29 2c 20  of(VdbeOpList), 
1ab78 6f 70 65 6e 42 6c 6f 62 29 3b 0a 0a 20 20 20 20  openBlob);..    
1ab79 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20 74    /* Configure t
1ab7a 68 65 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f  he OP_Transactio
1ab7b 6e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74  n */.      sqlit
1ab7c 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76  e3VdbeChangeP1(v
1ab7d 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  , 0, iDb);.     
1ab7e 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1ab7f 67 65 50 32 28 76 2c 20 30 2c 20 28 66 6c 61 67  geP2(v, 0, (flag
1ab80 73 20 3f 20 31 20 3a 20 30 29 29 3b 0a 0a 20 20  s ? 1 : 0));..  
1ab81 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65      /* Configure
1ab82 20 74 68 65 20 4f 50 5f 56 65 72 69 66 79 43 6f   the OP_VerifyCo
1ab83 6f 6b 69 65 20 2a 2f 0a 20 20 20 20 20 20 73 71  okie */.      sq
1ab84 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1ab85 31 28 76 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20  1(v, 1, iDb);.  
1ab86 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
1ab87 68 61 6e 67 65 50 32 28 76 2c 20 31 2c 20 70 54  hangeP2(v, 1, pT
1ab88 61 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63 68  ab->pSchema->sch
1ab89 65 6d 61 5f 63 6f 6f 6b 69 65 29 3b 0a 0a 20 20  ema_cookie);..  
1ab8a 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65      /* Make sure
1ab8b 20 61 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64   a mutex is held
1ab8c 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 74 6f   on the table to
1ab8d 20 62 65 20 61 63 63 65 73 73 65 64 20 2a 2f 0a   be accessed */.
1ab8e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1ab8f 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44  eUsesBtree(v, iD
1ab90 62 29 3b 20 0a 0a 20 20 20 20 20 20 2f 2a 20 52  b); ..      /* R
1ab91 65 6d 6f 76 65 20 65 69 74 68 65 72 20 74 68 65  emove either the
1ab92 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 6f 72   OP_OpenWrite or
1ab93 20 4f 70 65 6e 52 65 61 64 2e 20 53 65 74 20 74   OpenRead. Set t
1ab94 68 65 20 50 32 20 0a 20 20 20 20 20 20 2a 2a 20  he P2 .      ** 
1ab95 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74 68 65  parameter of the
1ab96 20 6f 74 68 65 72 20 74 6f 20 70 54 61 62 2d 3e   other to pTab->
1ab97 74 6e 75 6d 2e 20 0a 20 20 20 20 20 20 2a 2f 0a  tnum. .      */.
1ab98 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1ab99 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c  eChangeToNoop(v,
1ab9a 20 28 66 6c 61 67 73 20 3f 20 33 20 3a 20 35 29   (flags ? 3 : 5)
1ab9b 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
1ab9c 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28  te3VdbeChangeP2(
1ab9d 76 2c 20 28 66 6c 61 67 73 20 3f 20 35 20 3a 20  v, (flags ? 5 : 
1ab9e 33 29 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 29 3b  3), pTab->tnum);
1ab9f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1aba0 62 65 43 68 61 6e 67 65 50 33 28 76 2c 20 28 66  beChangeP3(v, (f
1aba1 6c 61 67 73 20 3f 20 35 20 3a 20 33 29 2c 20 69  lags ? 5 : 3), i
1aba2 44 62 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43  Db);..      /* C
1aba3 6f 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50 5f  onfigure the OP_
1aba4 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2e 20 43  SetNumColumns. C
1aba5 6f 6e 66 69 67 75 72 65 20 74 68 65 20 63 75 72  onfigure the cur
1aba6 73 6f 72 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20  sor to.      ** 
1aba7 74 68 69 6e 6b 20 74 68 61 74 20 74 68 65 20 74  think that the t
1aba8 61 62 6c 65 20 68 61 73 20 6f 6e 65 20 6d 6f 72  able has one mor
1aba9 65 20 63 6f 6c 75 6d 6e 20 74 68 61 6e 20 69 74  e column than it
1abaa 20 72 65 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a   really.      **
1abab 20 64 6f 65 73 2e 20 41 6e 20 4f 50 5f 43 6f 6c   does. An OP_Col
1abac 75 6d 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20  umn to retrieve 
1abad 74 68 69 73 20 69 6d 61 67 69 6e 61 72 79 20 63  this imaginary c
1abae 6f 6c 75 6d 6e 20 77 69 6c 6c 0a 20 20 20 20 20  olumn will.     
1abaf 20 2a 2a 20 61 6c 77 61 79 73 20 72 65 74 75 72   ** always retur
1abb0 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 54  n an SQL NULL. T
1abb1 68 69 73 20 69 73 20 75 73 65 66 75 6c 20 62 65  his is useful be
1abb2 63 61 75 73 65 20 69 74 20 6d 65 61 6e 73 0a 20  cause it means. 
1abb3 20 20 20 20 20 2a 2a 20 77 65 20 63 61 6e 20 69       ** we can i
1abb4 6e 76 6f 6b 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20  nvoke OP_Column 
1abb5 74 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 76  to fill in the v
1abb6 64 62 65 20 63 75 72 73 6f 72 73 20 74 79 70 65  dbe cursors type
1abb7 20 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6f   .      ** and o
1abb8 66 66 73 65 74 20 63 61 63 68 65 20 77 69 74 68  ffset cache with
1abb9 6f 75 74 20 63 61 75 73 69 6e 67 20 61 6e 79 20  out causing any 
1abba 49 4f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  IO..      */.   
1abbb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
1abbc 61 6e 67 65 50 32 28 76 2c 20 66 6c 61 67 73 20  angeP2(v, flags 
1abbd 3f 20 34 20 3a 20 32 2c 20 70 54 61 62 2d 3e 6e  ? 4 : 2, pTab->n
1abbe 43 6f 6c 2b 31 29 3b 0a 20 20 20 20 20 20 73 71  Col+1);.      sq
1abbf 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1abc0 32 28 76 2c 20 38 2c 20 70 54 61 62 2d 3e 6e 43  2(v, 8, pTab->nC
1abc1 6f 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  ol);.      if( !
1abc2 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1abc3 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1abc4 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79  te3VdbeMakeReady
1abc5 28 76 2c 20 31 2c 20 31 2c 20 31 2c 20 30 29 3b  (v, 1, 1, 1, 0);
1abc6 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1abc7 20 20 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74    .    sqlite3Bt
1abc8 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b  reeLeaveAll(db);
1abc9 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1abca 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
1abcb 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1abcc 54 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c  TE_OK || db->mal
1abcd 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
1abce 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65     goto blob_ope
1abcf 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20  n_out;.    }..  
1abd0 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
1abd1 6e 74 36 34 28 28 73 71 6c 69 74 65 33 5f 73 74  nt64((sqlite3_st
1abd2 6d 74 20 2a 29 76 2c 20 31 2c 20 69 52 6f 77 29  mt *)v, 1, iRow)
1abd3 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
1abd4 65 33 5f 73 74 65 70 28 28 73 71 6c 69 74 65 33  e3_step((sqlite3
1abd5 5f 73 74 6d 74 20 2a 29 76 29 3b 0a 20 20 20 20  _stmt *)v);.    
1abd6 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52  if( rc!=SQLITE_R
1abd7 4f 57 20 29 7b 0a 20 20 20 20 20 20 6e 41 74 74  OW ){.      nAtt
1abd8 65 6d 70 74 2b 2b 3b 0a 20 20 20 20 20 20 72 63  empt++;.      rc
1abd9 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c   = sqlite3_final
1abda 69 7a 65 28 28 73 71 6c 69 74 65 33 5f 73 74 6d  ize((sqlite3_stm
1abdb 74 20 2a 29 76 29 3b 0a 20 20 20 20 20 20 73 71  t *)v);.      sq
1abdc 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
1abdd 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72  izeof(zErr), zEr
1abde 72 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  r, sqlite3_errms
1abdf 67 28 64 62 29 29 3b 0a 20 20 20 20 20 20 76 20  g(db));.      v 
1abe0 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 77  = 0;.    }.  } w
1abe1 68 69 6c 65 28 20 6e 41 74 74 65 6d 70 74 3c 35  hile( nAttempt<5
1abe2 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53   && rc==SQLITE_S
1abe3 43 48 45 4d 41 20 29 3b 0a 0a 20 20 69 66 28 20  CHEMA );..  if( 
1abe4 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  rc==SQLITE_ROW )
1abe5 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77  {.    /* The row
1abe6 2d 72 65 63 6f 72 64 20 68 61 73 20 62 65 65 6e  -record has been
1abe7 20 6f 70 65 6e 65 64 20 73 75 63 63 65 73 73 66   opened successf
1abe8 75 6c 6c 79 2e 20 43 68 65 63 6b 20 74 68 61 74  ully. Check that
1abe9 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c 75   the.    ** colu
1abea 6d 6e 20 69 6e 20 71 75 65 73 74 69 6f 6e 20 63  mn in question c
1abeb 6f 6e 74 61 69 6e 73 20 74 65 78 74 20 6f 72 20  ontains text or 
1abec 61 20 62 6c 6f 62 2e 20 49 66 20 69 74 20 63 6f  a blob. If it co
1abed 6e 74 61 69 6e 73 0a 20 20 20 20 2a 2a 20 74 65  ntains.    ** te
1abee 78 74 2c 20 69 74 20 69 73 20 75 70 20 74 6f 20  xt, it is up to 
1abef 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 67 65  the caller to ge
1abf0 74 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 72  t the encoding r
1abf1 69 67 68 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ight..    */.   
1abf2 20 49 6e 63 72 62 6c 6f 62 20 2a 70 42 6c 6f 62   Incrblob *pBlob
1abf3 3b 0a 20 20 20 20 75 33 32 20 74 79 70 65 20 3d  ;.    u32 type =
1abf4 20 76 2d 3e 61 70 43 73 72 5b 30 5d 2d 3e 61 54   v->apCsr[0]->aT
1abf5 79 70 65 5b 69 43 6f 6c 5d 3b 0a 0a 20 20 20 20  ype[iCol];..    
1abf6 69 66 28 20 74 79 70 65 3c 31 32 20 29 7b 0a 20  if( type<12 ){. 
1abf7 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
1abf8 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72  rintf(sizeof(zEr
1abf9 72 29 2c 20 7a 45 72 72 2c 20 22 63 61 6e 6e 6f  r), zErr, "canno
1abfa 74 20 6f 70 65 6e 20 76 61 6c 75 65 20 6f 66 20  t open value of 
1abfb 74 79 70 65 20 25 73 22 2c 0a 20 20 20 20 20 20  type %s",.      
1abfc 20 20 20 20 74 79 70 65 3d 3d 30 3f 22 6e 75 6c      type==0?"nul
1abfd 6c 22 3a 20 74 79 70 65 3d 3d 37 3f 22 72 65 61  l": type==7?"rea
1abfe 6c 22 3a 20 22 69 6e 74 65 67 65 72 22 0a 20 20  l": "integer".  
1abff 20 20 20 20 29 3b 0a 20 20 20 20 20 20 72 63 20      );.      rc 
1ac00 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
1ac01 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f        goto blob_
1ac02 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  open_out;.    }.
1ac03 20 20 20 20 70 42 6c 6f 62 20 3d 20 28 49 6e 63      pBlob = (Inc
1ac04 72 62 6c 6f 62 20 2a 29 73 71 6c 69 74 65 33 44  rblob *)sqlite3D
1ac05 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
1ac06 73 69 7a 65 6f 66 28 49 6e 63 72 62 6c 6f 62 29  sizeof(Incrblob)
1ac07 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d  );.    if( db->m
1ac08 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1ac09 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
1ac0a 65 65 28 64 62 2c 20 70 42 6c 6f 62 29 3b 0a 20  ee(db, pBlob);. 
1ac0b 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f       goto blob_o
1ac0c 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  pen_out;.    }. 
1ac0d 20 20 20 70 42 6c 6f 62 2d 3e 66 6c 61 67 73 20     pBlob->flags 
1ac0e 3d 20 66 6c 61 67 73 3b 0a 20 20 20 20 70 42 6c  = flags;.    pBl
1ac0f 6f 62 2d 3e 70 43 73 72 20 3d 20 20 76 2d 3e 61  ob->pCsr =  v->a
1ac10 70 43 73 72 5b 30 5d 2d 3e 70 43 75 72 73 6f 72  pCsr[0]->pCursor
1ac11 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
1ac12 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 70 42  eeEnterCursor(pB
1ac13 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20  lob->pCsr);.    
1ac14 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 68  sqlite3BtreeCach
1ac15 65 4f 76 65 72 66 6c 6f 77 28 70 42 6c 6f 62 2d  eOverflow(pBlob-
1ac16 3e 70 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69  >pCsr);.    sqli
1ac17 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72  te3BtreeLeaveCur
1ac18 73 6f 72 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29  sor(pBlob->pCsr)
1ac19 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 70 53 74  ;.    pBlob->pSt
1ac1a 6d 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74  mt = (sqlite3_st
1ac1b 6d 74 20 2a 29 76 3b 0a 20 20 20 20 70 42 6c 6f  mt *)v;.    pBlo
1ac1c 62 2d 3e 69 4f 66 66 73 65 74 20 3d 20 76 2d 3e  b->iOffset = v->
1ac1d 61 70 43 73 72 5b 30 5d 2d 3e 61 4f 66 66 73 65  apCsr[0]->aOffse
1ac1e 74 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 70 42 6c  t[iCol];.    pBl
1ac1f 6f 62 2d 3e 6e 42 79 74 65 20 3d 20 73 71 6c 69  ob->nByte = sqli
1ac20 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
1ac21 65 4c 65 6e 28 74 79 70 65 29 3b 0a 20 20 20 20  eLen(type);.    
1ac22 70 42 6c 6f 62 2d 3e 64 62 20 3d 20 64 62 3b 0a  pBlob->db = db;.
1ac23 20 20 20 20 2a 70 70 42 6c 6f 62 20 3d 20 28 73      *ppBlob = (s
1ac24 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 29 70 42  qlite3_blob *)pB
1ac25 6c 6f 62 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  lob;.    rc = SQ
1ac26 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65  LITE_OK;.  }else
1ac27 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1ac28 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OK ){.    sqlite
1ac29 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
1ac2a 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 22  f(zErr), zErr, "
1ac2b 6e 6f 20 73 75 63 68 20 72 6f 77 69 64 3a 20 25  no such rowid: %
1ac2c 6c 6c 64 22 2c 20 69 52 6f 77 29 3b 0a 20 20 20  lld", iRow);.   
1ac2d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
1ac2e 4f 52 3b 0a 20 20 7d 0a 0a 62 6c 6f 62 5f 6f 70  OR;.  }..blob_op
1ac2f 65 6e 5f 6f 75 74 3a 0a 20 20 7a 45 72 72 5b 73  en_out:.  zErr[s
1ac30 69 7a 65 6f 66 28 7a 45 72 72 29 2d 31 5d 20 3d  izeof(zErr)-1] =
1ac31 20 27 5c 30 27 3b 0a 20 20 69 66 28 20 72 63 21   '\0';.  if( rc!
1ac32 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 64 62  =SQLITE_OK || db
1ac33 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1ac34 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69  {.    sqlite3_fi
1ac35 6e 61 6c 69 7a 65 28 28 73 71 6c 69 74 65 33 5f  nalize((sqlite3_
1ac36 73 74 6d 74 20 2a 29 76 29 3b 0a 20 20 7d 0a 20  stmt *)v);.  }. 
1ac37 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
1ac38 2c 20 72 63 2c 20 28 72 63 3d 3d 53 51 4c 49 54  , rc, (rc==SQLIT
1ac39 45 5f 4f 4b 3f 30 3a 7a 45 72 72 29 29 3b 0a 20  E_OK?0:zErr));. 
1ac3a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
1ac3b 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
1ac3c 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1ac3d 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
1ac3e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1ac3f 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 62 6c  /*.** Close a bl
1ac40 6f 62 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77  ob handle that w
1ac41 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 63 72  as previously cr
1ac42 65 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73  eated using.** s
1ac43 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e  qlite3_blob_open
1ac44 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ()..*/.SQLITE_AP
1ac45 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c  I int sqlite3_bl
1ac46 6f 62 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33  ob_close(sqlite3
1ac47 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20  _blob *pBlob){. 
1ac48 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28   Incrblob *p = (
1ac49 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62  Incrblob *)pBlob
1ac4a 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72  ;.  int rc;..  r
1ac4b 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  c = sqlite3_fina
1ac4c 6c 69 7a 65 28 70 2d 3e 70 53 74 6d 74 29 3b 0a  lize(p->pStmt);.
1ac4d 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1ac4e 70 2d 3e 64 62 2c 20 70 29 3b 0a 20 20 72 65 74  p->db, p);.  ret
1ac4f 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
1ac50 20 50 65 72 66 6f 72 6d 20 61 20 72 65 61 64 20   Perform a read 
1ac51 6f 72 20 77 72 69 74 65 20 6f 70 65 72 61 74 69  or write operati
1ac52 6f 6e 20 6f 6e 20 61 20 62 6c 6f 62 0a 2a 2f 0a  on on a blob.*/.
1ac53 73 74 61 74 69 63 20 69 6e 74 20 62 6c 6f 62 52  static int blobR
1ac54 65 61 64 57 72 69 74 65 28 0a 20 20 73 71 6c 69  eadWrite(.  sqli
1ac55 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c  te3_blob *pBlob,
1ac56 20 0a 20 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20   .  void *z, .  
1ac57 69 6e 74 20 6e 2c 20 0a 20 20 69 6e 74 20 69 4f  int n, .  int iO
1ac58 66 66 73 65 74 2c 20 0a 20 20 69 6e 74 20 28 2a  ffset, .  int (*
1ac59 78 43 61 6c 6c 29 28 42 74 43 75 72 73 6f 72 2a  xCall)(BtCursor*
1ac5a 2c 20 75 33 32 2c 20 75 33 32 2c 20 76 6f 69 64  , u32, u32, void
1ac5b 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  *).){.  int rc;.
1ac5c 20 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20    Incrblob *p = 
1ac5d 28 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f  (Incrblob *)pBlo
1ac5e 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  b;.  Vdbe *v;.  
1ac5f 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d  sqlite3 *db = p-
1ac60 3e 64 62 3b 20 20 0a 0a 20 20 73 71 6c 69 74 65  >db;  ..  sqlite
1ac61 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
1ac62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 76 20 3d 20  ->mutex);.  v = 
1ac63 28 56 64 62 65 2a 29 70 2d 3e 70 53 74 6d 74 3b  (Vdbe*)p->pStmt;
1ac64 0a 0a 20 20 69 66 28 20 6e 3c 30 20 7c 7c 20 69  ..  if( n<0 || i
1ac65 4f 66 66 73 65 74 3c 30 20 7c 7c 20 28 69 4f 66  Offset<0 || (iOf
1ac66 66 73 65 74 2b 6e 29 3e 70 2d 3e 6e 42 79 74 65  fset+n)>p->nByte
1ac67 20 29 7b 0a 20 20 20 20 2f 2a 20 52 65 71 75 65   ){.    /* Reque
1ac68 73 74 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e  st is out of ran
1ac69 67 65 2e 20 52 65 74 75 72 6e 20 61 20 74 72 61  ge. Return a tra
1ac6a 6e 73 69 65 6e 74 20 65 72 72 6f 72 2e 20 2a 2f  nsient error. */
1ac6b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1ac6c 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69  _ERROR;.    sqli
1ac6d 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c  te3Error(db, SQL
1ac6e 49 54 45 5f 45 52 52 4f 52 2c 20 30 29 3b 0a 20  ITE_ERROR, 0);. 
1ac6f 20 7d 20 65 6c 73 65 20 69 66 28 20 76 3d 3d 30   } else if( v==0
1ac70 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   ){.    /* If th
1ac71 65 72 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d  ere is no statem
1ac72 65 6e 74 20 68 61 6e 64 6c 65 2c 20 74 68 65 6e  ent handle, then
1ac73 20 74 68 65 20 62 6c 6f 62 2d 68 61 6e 64 6c 65   the blob-handle
1ac74 20 68 61 73 0a 20 20 20 20 2a 2a 20 61 6c 72 65   has.    ** alre
1ac75 61 64 79 20 62 65 65 6e 20 69 6e 76 61 6c 69 64  ady been invalid
1ac76 61 74 65 64 2e 20 52 65 74 75 72 6e 20 53 51 4c  ated. Return SQL
1ac77 49 54 45 5f 41 42 4f 52 54 20 69 6e 20 74 68 69  ITE_ABORT in thi
1ac78 73 20 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20  s case..    */. 
1ac79 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 41     rc = SQLITE_A
1ac7a 42 4f 52 54 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  BORT;.  }else{. 
1ac7b 20 20 20 2f 2a 20 43 61 6c 6c 20 65 69 74 68 65     /* Call eithe
1ac7c 72 20 42 74 72 65 65 44 61 74 61 28 29 20 6f 72  r BtreeData() or
1ac7d 20 42 74 72 65 65 50 75 74 44 61 74 61 28 29 2e   BtreePutData().
1ac7e 20 49 66 20 53 51 4c 49 54 45 5f 41 42 4f 52 54   If SQLITE_ABORT
1ac7f 20 69 73 0a 20 20 20 20 2a 2a 20 72 65 74 75 72   is.    ** retur
1ac80 6e 65 64 2c 20 63 6c 65 61 6e 2d 75 70 20 74 68  ned, clean-up th
1ac81 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64  e statement hand
1ac82 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  le..    */.    a
1ac83 73 73 65 72 74 28 20 64 62 20 3d 3d 20 76 2d 3e  ssert( db == v->
1ac84 64 62 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  db );.    sqlite
1ac85 33 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f  3BtreeEnterCurso
1ac86 72 28 70 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20  r(p->pCsr);.    
1ac87 72 63 20 3d 20 78 43 61 6c 6c 28 70 2d 3e 70 43  rc = xCall(p->pC
1ac88 73 72 2c 20 69 4f 66 66 73 65 74 2b 70 2d 3e 69  sr, iOffset+p->i
1ac89 4f 66 66 73 65 74 2c 20 6e 2c 20 7a 29 3b 0a 20  Offset, n, z);. 
1ac8a 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
1ac8b 65 61 76 65 43 75 72 73 6f 72 28 70 2d 3e 70 43  eaveCursor(p->pC
1ac8c 73 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  sr);.    if( rc=
1ac8d 3d 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 29 7b  =SQLITE_ABORT ){
1ac8e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1ac8f 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3b 0a 20  beFinalize(v);. 
1ac90 20 20 20 20 20 70 2d 3e 70 53 74 6d 74 20 3d 20       p->pStmt = 
1ac91 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  0;.    }else{.  
1ac92 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20      db->errCode 
1ac93 3d 20 72 63 3b 0a 20 20 20 20 20 20 76 2d 3e 72  = rc;.      v->r
1ac94 63 20 3d 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  c = rc;.    }.  
1ac95 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  }.  rc = sqlite3
1ac96 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b  ApiExit(db, rc);
1ac97 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
1ac98 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
1ac99 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
1ac9a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61  }../*.** Read da
1ac9b 74 61 20 66 72 6f 6d 20 61 20 62 6c 6f 62 20 68  ta from a blob h
1ac9c 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  andle..*/.SQLITE
1ac9d 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1ac9e 5f 62 6c 6f 62 5f 72 65 61 64 28 73 71 6c 69 74  _blob_read(sqlit
1ac9f 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20  e3_blob *pBlob, 
1aca0 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20  void *z, int n, 
1aca1 69 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a 20 20  int iOffset){.  
1aca2 72 65 74 75 72 6e 20 62 6c 6f 62 52 65 61 64 57  return blobReadW
1aca3 72 69 74 65 28 70 42 6c 6f 62 2c 20 7a 2c 20 6e  rite(pBlob, z, n
1aca4 2c 20 69 4f 66 66 73 65 74 2c 20 73 71 6c 69 74  , iOffset, sqlit
1aca5 65 33 42 74 72 65 65 44 61 74 61 29 3b 0a 7d 0a  e3BtreeData);.}.
1aca6 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74  ./*.** Write dat
1aca7 61 20 74 6f 20 61 20 62 6c 6f 62 20 68 61 6e 64  a to a blob hand
1aca8 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  le..*/.SQLITE_AP
1aca9 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c  I int sqlite3_bl
1acaa 6f 62 5f 77 72 69 74 65 28 73 71 6c 69 74 65 33  ob_write(sqlite3
1acab 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 63 6f  _blob *pBlob, co
1acac 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74  nst void *z, int
1acad 20 6e 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 29   n, int iOffset)
1acae 7b 0a 20 20 72 65 74 75 72 6e 20 62 6c 6f 62 52  {.  return blobR
1acaf 65 61 64 57 72 69 74 65 28 70 42 6c 6f 62 2c 20  eadWrite(pBlob, 
1acb0 28 76 6f 69 64 20 2a 29 7a 2c 20 6e 2c 20 69 4f  (void *)z, n, iO
1acb1 66 66 73 65 74 2c 20 73 71 6c 69 74 65 33 42 74  ffset, sqlite3Bt
1acb2 72 65 65 50 75 74 44 61 74 61 29 3b 0a 7d 0a 0a  reePutData);.}..
1acb3 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 61 20 62 6c  /*.** Query a bl
1acb4 6f 62 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68  ob handle for th
1acb5 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61  e size of the da
1acb6 74 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 6e  ta..**.** The In
1acb7 63 72 62 6c 6f 62 2e 6e 42 79 74 65 20 66 69 65  crblob.nByte fie
1acb8 6c 64 20 69 73 20 66 69 78 65 64 20 66 6f 72 20  ld is fixed for 
1acb9 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20  the lifetime of 
1acba 74 68 65 20 49 6e 63 72 62 6c 6f 62 0a 2a 2a 20  the Incrblob.** 
1acbb 73 6f 20 6e 6f 20 6d 75 74 65 78 20 69 73 20 72  so no mutex is r
1acbc 65 71 75 69 72 65 64 20 66 6f 72 20 61 63 63 65  equired for acce
1acbd 73 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ss..*/.SQLITE_AP
1acbe 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c  I int sqlite3_bl
1acbf 6f 62 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33  ob_bytes(sqlite3
1acc0 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20  _blob *pBlob){. 
1acc1 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28   Incrblob *p = (
1acc2 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62  Incrblob *)pBlob
1acc3 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 42  ;.  return p->nB
1acc4 79 74 65 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  yte;.}..#endif /
1acc5 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  * #ifndef SQLITE
1acc6 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 20 2a  _OMIT_INCRBLOB *
1acc7 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
1acc8 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 62 6c  ** End of vdbebl
1acc9 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ob.c ***********
1acca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1accb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1accc 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
1accd 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6a 6f  ** Begin file jo
1acce 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  urnal.c ********
1accf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1acd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1acd1 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75  */./*.** 2007 Au
1acd2 67 75 73 74 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68  gust 22.**.** Th
1acd3 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
1acd4 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
1acd5 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
1acd6 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
1acd7 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
1acd8 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
1acd9 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
1acda 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
1acdb 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
1acdc 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
1acdd 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
1acde 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
1acdf 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
1ace0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
1ace1 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
1ace2 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
1ace3 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
1ace4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ace5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ace6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ace7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ace8 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 40 28 23  ******.**.** @(#
1ace9 29 20 24 49 64 3a 20 6a 6f 75 72 6e 61 6c 2e 63  ) $Id: journal.c
1acea 2c 76 20 31 2e 38 20 32 30 30 38 2f 30 35 2f 30  ,v 1.8 2008/05/0
1aceb 31 20 31 38 3a 30 31 3a 34 37 20 64 72 68 20 45  1 18:01:47 drh E
1acec 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 64 65 66 20  xp $.*/..#ifdef 
1aced 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54  SQLITE_ENABLE_AT
1acee 4f 4d 49 43 5f 57 52 49 54 45 0a 0a 2f 2a 0a 2a  OMIC_WRITE../*.*
1acef 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c  * This file impl
1acf0 65 6d 65 6e 74 73 20 61 20 73 70 65 63 69 61 6c  ements a special
1acf1 20 6b 69 6e 64 20 6f 66 20 73 71 6c 69 74 65 33   kind of sqlite3
1acf2 5f 66 69 6c 65 20 6f 62 6a 65 63 74 20 75 73 65  _file object use
1acf3 64 0a 2a 2a 20 62 79 20 53 51 4c 69 74 65 20 74  d.** by SQLite t
1acf4 6f 20 63 72 65 61 74 65 20 6a 6f 75 72 6e 61 6c  o create journal
1acf5 20 66 69 6c 65 73 20 69 66 20 74 68 65 20 61 74   files if the at
1acf6 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d  omic-write optim
1acf7 69 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 65 6e  ization.** is en
1acf8 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  abled..**.** The
1acf9 20 64 69 73 74 69 6e 63 74 69 76 65 20 63 68 61   distinctive cha
1acfa 72 61 63 74 65 72 69 73 74 69 63 20 6f 66 20 74  racteristic of t
1acfb 68 69 73 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  his sqlite3_file
1acfc 20 69 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20   is that the.** 
1acfd 61 63 74 75 61 6c 20 6f 6e 20 64 69 73 6b 20 66  actual on disk f
1acfe 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 20 6c  ile is created l
1acff 61 7a 69 6c 79 2e 20 57 68 65 6e 20 74 68 65 20  azily. When the 
1ad00 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 2c  file is created,
1ad01 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 73  .** the caller s
1ad02 70 65 63 69 66 69 65 73 20 61 20 62 75 66 66 65  pecifies a buffe
1ad03 72 20 73 69 7a 65 20 66 6f 72 20 61 6e 20 69 6e  r size for an in
1ad04 2d 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74  -memory buffer t
1ad05 6f 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20  o.** be used to 
1ad06 73 65 72 76 69 63 65 20 72 65 61 64 28 29 20 61  service read() a
1ad07 6e 64 20 77 72 69 74 65 28 29 20 72 65 71 75 65  nd write() reque
1ad08 73 74 73 2e 20 54 68 65 20 61 63 74 75 61 6c 20  sts. The actual 
1ad09 66 69 6c 65 0a 2a 2a 20 6f 6e 20 64 69 73 6b 20  file.** on disk 
1ad0a 69 73 20 6e 6f 74 20 63 72 65 61 74 65 64 20 6f  is not created o
1ad0b 72 20 70 6f 70 75 6c 61 74 65 64 20 75 6e 74 69  r populated unti
1ad0c 6c 20 65 69 74 68 65 72 3a 0a 2a 2a 0a 2a 2a 20  l either:.**.** 
1ad0d 20 20 31 29 20 54 68 65 20 69 6e 2d 6d 65 6d 6f    1) The in-memo
1ad0e 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ry representatio
1ad0f 6e 20 67 72 6f 77 73 20 74 6f 6f 20 6c 61 72 67  n grows too larg
1ad10 65 20 66 6f 72 20 74 68 65 20 61 6c 6c 6f 63 61  e for the alloca
1ad11 74 65 64 20 0a 2a 2a 20 20 20 20 20 20 62 75 66  ted .**      buf
1ad12 66 65 72 2c 20 6f 72 0a 2a 2a 20 20 20 32 29 20  fer, or.**   2) 
1ad13 54 68 65 20 78 53 79 6e 63 28 29 20 6d 65 74 68  The xSync() meth
1ad14 6f 64 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f  od is called..*/
1ad15 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4a 6f 75 72  ..../*.** A Jour
1ad16 6e 61 6c 46 69 6c 65 20 6f 62 6a 65 63 74 20 69  nalFile object i
1ad17 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20  s a subclass of 
1ad18 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 75 73 65  sqlite3_file use
1ad19 64 20 62 79 0a 2a 2a 20 61 73 20 61 6e 20 6f 70  d by.** as an op
1ad1a 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 66  en file handle f
1ad1b 6f 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73  or journal files
1ad1c 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4a 6f 75 72  ..*/.struct Jour
1ad1d 6e 61 6c 46 69 6c 65 20 7b 0a 20 20 73 71 6c 69  nalFile {.  sqli
1ad1e 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a  te3_io_methods *
1ad1f 70 4d 65 74 68 6f 64 3b 20 20 20 20 2f 2a 20 49  pMethod;    /* I
1ad20 2f 4f 20 6d 65 74 68 6f 64 73 20 6f 6e 20 6a 6f  /O methods on jo
1ad21 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a 20  urnal files */. 
1ad22 20 69 6e 74 20 6e 42 75 66 3b 20 20 20 20 20 20   int nBuf;      
1ad23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ad24 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 42 75 66   /* Size of zBuf
1ad25 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  [] in bytes */. 
1ad26 20 63 68 61 72 20 2a 7a 42 75 66 3b 20 20 20 20   char *zBuf;    
1ad27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ad28 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 62 75 66   /* Space to buf
1ad29 66 65 72 20 6a 6f 75 72 6e 61 6c 20 77 72 69 74  fer journal writ
1ad2a 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 53 69 7a  es */.  int iSiz
1ad2b 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
1ad2c 20 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e          /* Amoun
1ad2d 74 20 6f 66 20 7a 42 75 66 5b 5d 20 63 75 72 72  t of zBuf[] curr
1ad2e 65 6e 74 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20  ently used */.  
1ad2f 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20  int flags;      
1ad30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ad31 2f 2a 20 78 4f 70 65 6e 20 66 6c 61 67 73 20 2a  /* xOpen flags *
1ad32 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  /.  sqlite3_vfs 
1ad33 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 20  *pVfs;          
1ad34 20 20 20 20 2f 2a 20 54 68 65 20 22 72 65 61 6c      /* The "real
1ad35 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 56 46 53  " underlying VFS
1ad36 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69   */.  sqlite3_fi
1ad37 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20 20 20 20  le *pReal;      
1ad38 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 72 65        /* The "re
1ad39 61 6c 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 66  al" underlying f
1ad3a 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a  ile descriptor *
1ad3b 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1ad3c 7a 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20  zJournal;       
1ad3d 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
1ad3e 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
1ad3f 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74  */.};.typedef st
1ad40 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46 69 6c 65  ruct JournalFile
1ad41 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 3b 0a 0a 2f   JournalFile;../
1ad42 2a 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 20  *.** If it does 
1ad43 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73  not already exis
1ad44 74 73 2c 20 63 72 65 61 74 65 20 61 6e 64 20 70  ts, create and p
1ad45 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 6e 2d 64  opulate the on-d
1ad46 69 73 6b 20 66 69 6c 65 20 0a 2a 2a 20 66 6f 72  isk file .** for
1ad47 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 70 2e 0a   JournalFile p..
1ad48 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 72  */.static int cr
1ad49 65 61 74 65 46 69 6c 65 28 4a 6f 75 72 6e 61 6c  eateFile(Journal
1ad4a 46 69 6c 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  File *p){.  int 
1ad4b 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1ad4c 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61 6c 20    if( !p->pReal 
1ad4d 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
1ad4e 69 6c 65 20 2a 70 52 65 61 6c 20 3d 20 28 73 71  ile *pReal = (sq
1ad4f 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 26 70 5b  lite3_file *)&p[
1ad50 31 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  1];.    rc = sql
1ad51 69 74 65 33 4f 73 4f 70 65 6e 28 70 2d 3e 70 56  ite3OsOpen(p->pV
1ad52 66 73 2c 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c  fs, p->zJournal,
1ad53 20 70 52 65 61 6c 2c 20 70 2d 3e 66 6c 61 67 73   pReal, p->flags
1ad54 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63  , 0);.    if( rc
1ad55 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1ad56 20 20 20 20 20 70 2d 3e 70 52 65 61 6c 20 3d 20       p->pReal = 
1ad57 70 52 65 61 6c 3b 0a 20 20 20 20 20 20 69 66 28  pReal;.      if(
1ad58 20 70 2d 3e 69 53 69 7a 65 3e 30 20 29 7b 0a 20   p->iSize>0 ){. 
1ad59 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70 2d         assert(p-
1ad5a 3e 69 53 69 7a 65 3c 3d 70 2d 3e 6e 42 75 66 29  >iSize<=p->nBuf)
1ad5b 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
1ad5c 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 2d  qlite3OsWrite(p-
1ad5d 3e 70 52 65 61 6c 2c 20 70 2d 3e 7a 42 75 66 2c  >pReal, p->zBuf,
1ad5e 20 70 2d 3e 69 53 69 7a 65 2c 20 30 29 3b 0a 20   p->iSize, 0);. 
1ad5f 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
1ad60 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1ad61 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65  ./*.** Close the
1ad62 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
1ad63 20 69 6e 74 20 6a 72 6e 6c 43 6c 6f 73 65 28 73   int jrnlClose(s
1ad64 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66  qlite3_file *pJf
1ad65 64 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c  d){.  JournalFil
1ad66 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46  e *p = (JournalF
1ad67 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66  ile *)pJfd;.  if
1ad68 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20  ( p->pReal ){.  
1ad69 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65    sqlite3OsClose
1ad6a 28 70 2d 3e 70 52 65 61 6c 29 3b 0a 20 20 7d 0a  (p->pReal);.  }.
1ad6b 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
1ad6c 2d 3e 7a 42 75 66 29 3b 0a 20 20 72 65 74 75 72  ->zBuf);.  retur
1ad6d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
1ad6e 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20  /*.** Read data 
1ad6f 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 2e 0a 2a  from the file..*
1ad70 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e  /.static int jrn
1ad71 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33  lRead(.  sqlite3
1ad72 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20  _file *pJfd,    
1ad73 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66  /* The journal f
1ad74 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 74  ile from which t
1ad75 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64  o read */.  void
1ad76 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 20 20   *zBuf,         
1ad77 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20 72 65     /* Put the re
1ad78 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20  sults here */.  
1ad79 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20  int iAmt,       
1ad7a 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1ad7b 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61   of bytes to rea
1ad7c 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e  d */.  sqlite_in
1ad7d 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a  t64 iOfst     /*
1ad7e 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61   Begin reading a
1ad7f 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f  t this offset */
1ad80 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
1ad81 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72  QLITE_OK;.  Jour
1ad82 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f  nalFile *p = (Jo
1ad83 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64  urnalFile *)pJfd
1ad84 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c  ;.  if( p->pReal
1ad85 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
1ad86 69 74 65 33 4f 73 52 65 61 64 28 70 2d 3e 70 52  ite3OsRead(p->pR
1ad87 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41 6d 74 2c  eal, zBuf, iAmt,
1ad88 20 69 4f 66 73 74 29 3b 0a 20 20 7d 65 6c 73 65   iOfst);.  }else
1ad89 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 41  {.    assert( iA
1ad8a 6d 74 2b 69 4f 66 73 74 3c 3d 70 2d 3e 69 53 69  mt+iOfst<=p->iSi
1ad8b 7a 65 20 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  ze );.    memcpy
1ad8c 28 7a 42 75 66 2c 20 26 70 2d 3e 7a 42 75 66 5b  (zBuf, &p->zBuf[
1ad8d 69 4f 66 73 74 5d 2c 20 69 41 6d 74 29 3b 0a 20  iOfst], iAmt);. 
1ad8e 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
1ad8f 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64  }../*.** Write d
1ad90 61 74 61 20 74 6f 20 74 68 65 20 66 69 6c 65 2e  ata to the file.
1ad91 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a  .*/.static int j
1ad92 72 6e 6c 57 72 69 74 65 28 0a 20 20 73 71 6c 69  rnlWrite(.  sqli
1ad93 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20  te3_file *pJfd, 
1ad94 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61     /* The journa
1ad95 6c 20 66 69 6c 65 20 69 6e 74 6f 20 77 68 69 63  l file into whic
1ad96 68 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20  h to write */.  
1ad97 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66  const void *zBuf
1ad98 2c 20 20 20 20 20 20 2f 2a 20 54 61 6b 65 20 64  ,      /* Take d
1ad99 61 74 61 20 74 6f 20 62 65 20 77 72 69 74 74 65  ata to be writte
1ad9a 6e 20 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 20  n from here */. 
1ad9b 20 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20   int iAmt,      
1ad9c 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1ad9d 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72  r of bytes to wr
1ad9e 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f  ite */.  sqlite_
1ad9f 69 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20  int64 iOfst     
1ada0 2f 2a 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67  /* Begin writing
1ada1 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20   at this offset 
1ada2 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f  into the file */
1ada3 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
1ada4 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72  QLITE_OK;.  Jour
1ada5 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f  nalFile *p = (Jo
1ada6 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64  urnalFile *)pJfd
1ada7 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61  ;.  if( !p->pRea
1ada8 6c 20 26 26 20 28 69 4f 66 73 74 2b 69 41 6d 74  l && (iOfst+iAmt
1ada9 29 3e 70 2d 3e 6e 42 75 66 20 29 7b 0a 20 20 20  )>p->nBuf ){.   
1adaa 20 72 63 20 3d 20 63 72 65 61 74 65 46 69 6c 65   rc = createFile
1adab 28 70 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  (p);.  }.  if( r
1adac 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1adad 20 20 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c      if( p->pReal
1adae 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
1adaf 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 2d  qlite3OsWrite(p-
1adb0 3e 70 52 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41  >pReal, zBuf, iA
1adb1 6d 74 2c 20 69 4f 66 73 74 29 3b 0a 20 20 20 20  mt, iOfst);.    
1adb2 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d  }else{.      mem
1adb3 63 70 79 28 26 70 2d 3e 7a 42 75 66 5b 69 4f 66  cpy(&p->zBuf[iOf
1adb4 73 74 5d 2c 20 7a 42 75 66 2c 20 69 41 6d 74 29  st], zBuf, iAmt)
1adb5 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69  ;.      if( p->i
1adb6 53 69 7a 65 3c 28 69 4f 66 73 74 2b 69 41 6d 74  Size<(iOfst+iAmt
1adb7 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  ) ){.        p->
1adb8 69 53 69 7a 65 20 3d 20 28 69 4f 66 73 74 2b 69  iSize = (iOfst+i
1adb9 41 6d 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Amt);.      }.  
1adba 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
1adbb 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72   rc;.}../*.** Tr
1adbc 75 6e 63 61 74 65 20 74 68 65 20 66 69 6c 65 2e  uncate the file.
1adbd 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a  .*/.static int j
1adbe 72 6e 6c 54 72 75 6e 63 61 74 65 28 73 71 6c 69  rnlTruncate(sqli
1adbf 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20  te3_file *pJfd, 
1adc0 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 69 7a  sqlite_int64 siz
1adc1 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  e){.  int rc = S
1adc2 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72  QLITE_OK;.  Jour
1adc3 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f  nalFile *p = (Jo
1adc4 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64  urnalFile *)pJfd
1adc5 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c  ;.  if( p->pReal
1adc6 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
1adc7 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70  ite3OsTruncate(p
1adc8 2d 3e 70 52 65 61 6c 2c 20 73 69 7a 65 29 3b 0a  ->pReal, size);.
1adc9 20 20 7d 65 6c 73 65 20 69 66 28 20 73 69 7a 65    }else if( size
1adca 3c 70 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20  <p->iSize ){.   
1adcb 20 70 2d 3e 69 53 69 7a 65 20 3d 20 73 69 7a 65   p->iSize = size
1adcc 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1adcd 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63  c;.}../*.** Sync
1adce 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74   the file..*/.st
1adcf 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 53 79 6e  atic int jrnlSyn
1add0 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  c(sqlite3_file *
1add1 70 4a 66 64 2c 20 69 6e 74 20 66 6c 61 67 73 29  pJfd, int flags)
1add2 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4a 6f  {.  int rc;.  Jo
1add3 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28  urnalFile *p = (
1add4 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a  JournalFile *)pJ
1add5 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65  fd;.  if( p->pRe
1add6 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  al ){.    rc = s
1add7 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 2d 3e  qlite3OsSync(p->
1add8 70 52 65 61 6c 2c 20 66 6c 61 67 73 29 3b 0a 20  pReal, flags);. 
1add9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d   }else{.    rc =
1adda 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
1addb 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1addc 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20  /*.** Query the 
1addd 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65  size of the file
1adde 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74   in bytes..*/.st
1addf 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 46 69 6c  atic int jrnlFil
1ade0 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69  eSize(sqlite3_fi
1ade1 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65  le *pJfd, sqlite
1ade2 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b 0a  _int64 *pSize){.
1ade3 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1ade4 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46  E_OK;.  JournalF
1ade5 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61  ile *p = (Journa
1ade6 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20  lFile *)pJfd;.  
1ade7 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a  if( p->pReal ){.
1ade8 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1ade9 4f 73 46 69 6c 65 53 69 7a 65 28 70 2d 3e 70 52  OsFileSize(p->pR
1adea 65 61 6c 2c 20 70 53 69 7a 65 29 3b 0a 20 20 7d  eal, pSize);.  }
1adeb 65 6c 73 65 7b 0a 20 20 20 20 2a 70 53 69 7a 65  else{.    *pSize
1adec 20 3d 20 28 73 71 6c 69 74 65 5f 69 6e 74 36 34   = (sqlite_int64
1aded 29 20 70 2d 3e 69 53 69 7a 65 3b 0a 20 20 7d 0a  ) p->iSize;.  }.
1adee 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1adef 2f 2a 0a 2a 2a 20 54 61 62 6c 65 20 6f 66 20 6d  /*.** Table of m
1adf0 65 74 68 6f 64 73 20 66 6f 72 20 4a 6f 75 72 6e  ethods for Journ
1adf1 61 6c 46 69 6c 65 20 73 71 6c 69 74 65 33 5f 66  alFile sqlite3_f
1adf2 69 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73  ile object..*/.s
1adf3 74 61 74 69 63 20 73 74 72 75 63 74 20 73 71 6c  tatic struct sql
1adf4 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
1adf5 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74 68 6f  JournalFileMetho
1adf6 64 73 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20  ds = {.  1,     
1adf7 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73          /* iVers
1adf8 69 6f 6e 20 2a 2f 0a 20 20 6a 72 6e 6c 43 6c 6f  ion */.  jrnlClo
1adf9 73 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73  se,     /* xClos
1adfa 65 20 2a 2f 0a 20 20 6a 72 6e 6c 52 65 61 64 2c  e */.  jrnlRead,
1adfb 20 20 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a        /* xRead *
1adfc 2f 0a 20 20 6a 72 6e 6c 57 72 69 74 65 2c 20 20  /.  jrnlWrite,  
1adfd 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f 0a     /* xWrite */.
1adfe 20 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65 2c 20    jrnlTruncate, 
1adff 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f   /* xTruncate */
1ae00 0a 20 20 6a 72 6e 6c 53 79 6e 63 2c 20 20 20 20  .  jrnlSync,    
1ae01 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 0a 20 20    /* xSync */.  
1ae02 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f  jrnlFileSize,  /
1ae03 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20  * xFileSize */. 
1ae04 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
1ae05 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c  /* xLock */.  0,
1ae06 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1ae07 78 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20  xUnlock */.  0, 
1ae08 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
1ae09 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
1ae0a 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20  k */.  0,       
1ae0b 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 43 6f        /* xFileCo
1ae0c 6e 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c 20 20 20  ntrol */.  0,   
1ae0d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65            /* xSe
1ae0e 63 74 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 30 20  ctorSize */.  0 
1ae0f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1ae10 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  xDeviceCharacter
1ae11 69 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  istics */.};../*
1ae12 20 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f 75 72   .** Open a jour
1ae13 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c  nal file..*/.SQL
1ae14 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1ae15 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70  sqlite3JournalOp
1ae16 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  en(.  sqlite3_vf
1ae17 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20  s *pVfs,        
1ae18 20 2f 2a 20 54 68 65 20 56 46 53 20 74 6f 20 75   /* The VFS to u
1ae19 73 65 20 66 6f 72 20 61 63 74 75 61 6c 20 66 69  se for actual fi
1ae1a 6c 65 20 49 2f 4f 20 2a 2f 0a 20 20 63 6f 6e 73  le I/O */.  cons
1ae1b 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20  t char *zName,  
1ae1c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
1ae1d 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
1ae1e 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  le */.  sqlite3_
1ae1f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 20  file *pJfd,     
1ae20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 74     /* Preallocat
1ae21 65 64 2c 20 62 6c 61 6e 6b 20 66 69 6c 65 20 68  ed, blank file h
1ae22 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66  andle */.  int f
1ae23 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20  lags,           
1ae24 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 69 6e 67        /* Opening
1ae25 20 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20   flags */.  int 
1ae26 6e 42 75 66 20 20 20 20 20 20 20 20 20 20 20 20  nBuf            
1ae27 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20         /* Bytes 
1ae28 62 75 66 66 65 72 65 64 20 62 65 66 6f 72 65 20  buffered before 
1ae29 6f 70 65 6e 69 6e 67 20 74 68 65 20 66 69 6c 65  opening the file
1ae2a 20 2a 2f 0a 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c   */.){.  Journal
1ae2b 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e  File *p = (Journ
1ae2c 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20  alFile *)pJfd;. 
1ae2d 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 71   memset(p, 0, sq
1ae2e 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65  lite3JournalSize
1ae2f 28 70 56 66 73 29 29 3b 0a 20 20 69 66 28 20 6e  (pVfs));.  if( n
1ae30 42 75 66 3e 30 20 29 7b 0a 20 20 20 20 70 2d 3e  Buf>0 ){.    p->
1ae31 7a 42 75 66 20 3d 20 73 71 6c 69 74 65 33 4d 61  zBuf = sqlite3Ma
1ae32 6c 6c 6f 63 5a 65 72 6f 28 6e 42 75 66 29 3b 0a  llocZero(nBuf);.
1ae33 20 20 20 20 69 66 28 20 21 70 2d 3e 7a 42 75 66      if( !p->zBuf
1ae34 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
1ae35 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
1ae36 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
1ae37 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
1ae38 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4e 61  OsOpen(pVfs, zNa
1ae39 6d 65 2c 20 70 4a 66 64 2c 20 66 6c 61 67 73 2c  me, pJfd, flags,
1ae3a 20 30 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4d   0);.  }.  p->pM
1ae3b 65 74 68 6f 64 20 3d 20 26 4a 6f 75 72 6e 61 6c  ethod = &Journal
1ae3c 46 69 6c 65 4d 65 74 68 6f 64 73 3b 0a 20 20 70  FileMethods;.  p
1ae3d 2d 3e 6e 42 75 66 20 3d 20 6e 42 75 66 3b 0a 20  ->nBuf = nBuf;. 
1ae3e 20 70 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67   p->flags = flag
1ae3f 73 3b 0a 20 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c  s;.  p->zJournal
1ae40 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 2d 3e 70   = zName;.  p->p
1ae41 56 66 73 20 3d 20 70 56 66 73 3b 0a 20 20 72 65  Vfs = pVfs;.  re
1ae42 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1ae43 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  }../*.** If the 
1ae44 61 72 67 75 6d 65 6e 74 20 70 20 70 6f 69 6e 74  argument p point
1ae45 73 20 74 6f 20 61 20 4a 6f 75 72 6e 61 6c 46 69  s to a JournalFi
1ae46 6c 65 20 73 74 72 75 63 74 75 72 65 2c 20 61 6e  le structure, an
1ae47 64 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  d the underlying
1ae48 0a 2a 2a 20 66 69 6c 65 20 68 61 73 20 6e 6f 74  .** file has not
1ae49 20 79 65 74 20 62 65 65 6e 20 63 72 65 61 74 65   yet been create
1ae4a 64 2c 20 63 72 65 61 74 65 20 69 74 20 6e 6f 77  d, create it now
1ae4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1ae4c 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a  ATE int sqlite3J
1ae4d 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 73 71 6c  ournalCreate(sql
1ae4e 69 74 65 33 5f 66 69 6c 65 20 2a 70 29 7b 0a 20  ite3_file *p){. 
1ae4f 20 69 66 28 20 70 2d 3e 70 4d 65 74 68 6f 64 73   if( p->pMethods
1ae50 21 3d 26 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65  !=&JournalFileMe
1ae51 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 72 65 74  thods ){.    ret
1ae52 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
1ae53 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 72 65 61   }.  return crea
1ae54 74 65 46 69 6c 65 28 28 4a 6f 75 72 6e 61 6c 46  teFile((JournalF
1ae55 69 6c 65 20 2a 29 70 29 3b 0a 7d 0a 0a 2f 2a 20  ile *)p);.}../* 
1ae56 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
1ae57 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72  umber of bytes r
1ae58 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65  equired to store
1ae59 20 61 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 74   a JournalFile t
1ae5a 68 61 74 20 75 73 65 73 20 76 66 73 0a 2a 2a 20  hat uses vfs.** 
1ae5b 70 56 66 73 20 74 6f 20 63 72 65 61 74 65 20 74  pVfs to create t
1ae5c 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 6e  he underlying on
1ae5d 2d 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a  -disk files..*/.
1ae5e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1ae5f 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61  nt sqlite3Journa
1ae60 6c 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 76 66  lSize(sqlite3_vf
1ae61 73 20 2a 70 56 66 73 29 7b 0a 20 20 72 65 74 75  s *pVfs){.  retu
1ae62 72 6e 20 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69  rn (pVfs->szOsFi
1ae63 6c 65 2b 73 69 7a 65 6f 66 28 4a 6f 75 72 6e 61  le+sizeof(Journa
1ae64 6c 46 69 6c 65 29 29 3b 0a 7d 0a 23 65 6e 64 69  lFile));.}.#endi
1ae65 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f../************
1ae66 2a 2a 20 45 6e 64 20 6f 66 20 6a 6f 75 72 6e 61  ** End of journa
1ae67 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  l.c ************
1ae68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ae69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ae6a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
1ae6b 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65  ** Begin file me
1ae6c 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a  mjournal.c *****
1ae6d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ae6e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ae6f 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63  */./*.** 2008 Oc
1ae70 74 6f 62 65 72 20 37 0a 2a 2a 0a 2a 2a 20 54 68  tober 7.**.** Th
1ae71 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
1ae72 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
1ae73 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
1ae74 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
1ae75 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
1ae76 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
1ae77 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
1ae78 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
1ae79 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
1ae7a 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
1ae7b 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
1ae7c 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
1ae7d 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
1ae7e 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
1ae7f 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
1ae80 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
1ae81 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
1ae82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ae83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ae84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ae85 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ae86 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
1ae87 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
1ae88 63 6f 64 65 20 75 73 65 20 74 6f 20 69 6d 70 6c  code use to impl
1ae89 65 6d 65 6e 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f  ement an in-memo
1ae8a 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  ry rollback jour
1ae8b 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 69 6e 2d 6d  nal..** The in-m
1ae8c 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a  emory rollback j
1ae8d 6f 75 72 6e 61 6c 20 69 73 20 75 73 65 64 20 74  ournal is used t
1ae8e 6f 20 6a 6f 75 72 6e 61 6c 20 74 72 61 6e 73 61  o journal transa
1ae8f 63 74 69 6f 6e 73 20 66 6f 72 0a 2a 2a 20 22 3a  ctions for.** ":
1ae90 6d 65 6d 6f 72 79 3a 22 20 64 61 74 61 62 61 73  memory:" databas
1ae91 65 73 20 61 6e 64 20 77 68 65 6e 20 74 68 65 20  es and when the 
1ae92 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4d 45 4d  journal_mode=MEM
1ae93 4f 52 59 20 70 72 61 67 6d 61 20 69 73 20 75 73  ORY pragma is us
1ae94 65 64 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24  ed..**.** @(#) $
1ae95 49 64 3a 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63  Id: memjournal.c
1ae96 2c 76 20 31 2e 32 20 32 30 30 38 2f 31 30 2f 32  ,v 1.2 2008/10/2
1ae97 38 20 31 38 3a 31 32 3a 33 36 20 64 72 68 20 45  8 18:12:36 drh E
1ae98 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 46 6f 72 77  xp $.*/../* Forw
1ae99 61 72 64 20 72 65 66 65 72 65 6e 63 65 73 20 74  ard references t
1ae9a 6f 20 69 6e 74 65 72 6e 61 6c 20 73 74 72 75 63  o internal struc
1ae9b 74 75 72 65 73 20 2a 2f 0a 74 79 70 65 64 65 66  tures */.typedef
1ae9c 20 73 74 72 75 63 74 20 4d 65 6d 4a 6f 75 72 6e   struct MemJourn
1ae9d 61 6c 20 4d 65 6d 4a 6f 75 72 6e 61 6c 3b 0a 74  al MemJournal;.t
1ae9e 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46 69  ypedef struct Fi
1ae9f 6c 65 50 6f 69 6e 74 20 46 69 6c 65 50 6f 69 6e  lePoint FilePoin
1aea0 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  t;.typedef struc
1aea1 74 20 46 69 6c 65 43 68 75 6e 6b 20 46 69 6c 65  t FileChunk File
1aea2 43 68 75 6e 6b 3b 0a 0a 2f 2a 20 53 70 61 63 65  Chunk;../* Space
1aea3 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 6f 6c   to hold the rol
1aea4 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73  lback journal is
1aea5 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 69 6e   allocated in in
1aea6 63 72 65 6d 65 6e 74 73 20 6f 66 0a 2a 2a 20 74  crements of.** t
1aea7 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 2e 0a  his many bytes..
1aea8 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e  */.#define JOURN
1aea9 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 31 30 32  AL_CHUNKSIZE 102
1aeaa 34 0a 0a 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 66  4../* Macro to f
1aeab 69 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20  ind the minimum 
1aeac 6f 66 20 74 77 6f 20 6e 75 6d 65 72 69 63 20 76  of two numeric v
1aead 61 6c 75 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  alues..*/.#defin
1aeae 65 20 4d 49 4e 28 78 2c 79 29 20 28 28 78 29 3c  e MIN(x,y) ((x)<
1aeaf 28 79 29 3f 28 78 29 3a 28 79 29 29 0a 0a 2f 2a  (y)?(x):(y))../*
1aeb0 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b  .** The rollback
1aeb1 20 6a 6f 75 72 6e 61 6c 20 69 73 20 63 6f 6d 70   journal is comp
1aeb2 6f 73 65 64 20 6f 66 20 61 20 6c 69 6e 6b 65 64  osed of a linked
1aeb3 20 6c 69 73 74 20 6f 66 20 74 68 65 73 65 20 73   list of these s
1aeb4 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 73 74  tructures..*/.st
1aeb5 72 75 63 74 20 46 69 6c 65 43 68 75 6e 6b 20 7b  ruct FileChunk {
1aeb6 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 4e  .  FileChunk *pN
1aeb7 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ext;            
1aeb8 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 75 6e 6b     /* Next chunk
1aeb9 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   in the journal 
1aeba 2a 2f 0a 20 20 75 38 20 7a 43 68 75 6e 6b 5b 4a  */.  u8 zChunk[J
1aebb 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45  OURNAL_CHUNKSIZE
1aebc 5d 3b 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20  ];   /* Content 
1aebd 6f 66 20 74 68 69 73 20 63 68 75 6e 6b 20 2a 2f  of this chunk */
1aebe 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  .};../*.** An in
1aebf 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f  stance of this o
1aec0 62 6a 65 63 74 20 73 65 72 76 65 73 20 61 73 20  bject serves as 
1aec1 61 20 63 75 72 73 6f 72 20 69 6e 74 6f 20 74 68  a cursor into th
1aec2 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  e rollback journ
1aec3 61 6c 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f  al..** The curso
1aec4 72 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20  r can be either 
1aec5 66 6f 72 20 72 65 61 64 69 6e 67 20 6f 72 20 77  for reading or w
1aec6 72 69 74 69 6e 67 2e 0a 2a 2f 0a 73 74 72 75 63  riting..*/.struc
1aec7 74 20 46 69 6c 65 50 6f 69 6e 74 20 7b 0a 20 20  t FilePoint {.  
1aec8 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f  sqlite3_int64 iO
1aec9 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20  ffset;          
1aeca 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f 6d 20 74  /* Offset from t
1aecb 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
1aecc 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 46 69  the file */.  Fi
1aecd 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b  leChunk *pChunk;
1aece 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1aecf 20 53 70 65 63 69 66 69 63 20 63 68 75 6e 6b 20   Specific chunk 
1aed0 69 6e 74 6f 20 77 68 69 63 68 20 63 75 72 73 6f  into which curso
1aed1 72 20 70 6f 69 6e 74 73 20 2a 2f 0a 7d 3b 0a 0a  r points */.};..
1aed2 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 75 62 63 6c  /*.** This subcl
1aed3 61 73 73 20 69 73 20 61 20 73 75 62 63 6c 61 73  ass is a subclas
1aed4 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c  s of sqlite3_fil
1aed5 65 2e 20 20 45 61 63 68 20 6f 70 65 6e 20 6d 65  e.  Each open me
1aed6 6d 6f 72 79 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20  mory-journal.** 
1aed7 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
1aed8 66 20 74 68 69 73 20 63 6c 61 73 73 2e 0a 2a 2f  f this class..*/
1aed9 0a 73 74 72 75 63 74 20 4d 65 6d 4a 6f 75 72 6e  .struct MemJourn
1aeda 61 6c 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69  al {.  sqlite3_i
1aedb 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68  o_methods *pMeth
1aedc 6f 64 3b 20 20 20 20 2f 2a 20 50 61 72 65 6e 74  od;    /* Parent
1aedd 20 63 6c 61 73 73 2e 20 4d 55 53 54 20 42 45 20   class. MUST BE 
1aede 46 49 52 53 54 20 2a 2f 0a 20 20 46 69 6c 65 43  FIRST */.  FileC
1aedf 68 75 6e 6b 20 2a 70 46 69 72 73 74 3b 20 20 20  hunk *pFirst;   
1aee0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 65             /* He
1aee1 61 64 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20  ad of in-memory 
1aee2 63 68 75 6e 6b 2d 6c 69 73 74 20 2a 2f 0a 20 20  chunk-list */.  
1aee3 46 69 6c 65 50 6f 69 6e 74 20 65 6e 64 70 6f 69  FilePoint endpoi
1aee4 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nt;             
1aee5 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68  /* Pointer to th
1aee6 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c  e end of the fil
1aee7 65 20 2a 2f 0a 20 20 46 69 6c 65 50 6f 69 6e 74  e */.  FilePoint
1aee8 20 72 65 61 64 70 6f 69 6e 74 3b 20 20 20 20 20   readpoint;     
1aee9 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
1aeea 72 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  r to the end of 
1aeeb 74 68 65 20 6c 61 73 74 20 78 52 65 61 64 28 29  the last xRead()
1aeec 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65   */.};../*.** Re
1aeed 61 64 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65  ad data from the
1aeee 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
1aeef 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 52 65 61 64   int memjrnlRead
1aef0 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  (.  sqlite3_file
1aef1 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68   *pJfd,    /* Th
1aef2 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66  e journal file f
1aef3 72 6f 6d 20 77 68 69 63 68 20 74 6f 20 72 65 61  rom which to rea
1aef4 64 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 7a 42 75  d */.  void *zBu
1aef5 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  f,            /*
1aef6 20 50 75 74 20 74 68 65 20 72 65 73 75 6c 74 73   Put the results
1aef7 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69   here */.  int i
1aef8 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  Amt,            
1aef9 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
1aefa 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a  ytes to read */.
1aefb 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
1aefc 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69  Ofst     /* Begi
1aefd 6e 20 72 65 61 64 69 6e 67 20 61 74 20 74 68 69  n reading at thi
1aefe 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20  s offset */.){. 
1aeff 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d   MemJournal *p =
1af00 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70   (MemJournal *)p
1af01 4a 66 64 3b 0a 20 20 75 38 20 2a 7a 4f 75 74 20  Jfd;.  u8 *zOut 
1af02 3d 20 7a 42 75 66 3b 0a 20 20 69 6e 74 20 6e 52  = zBuf;.  int nR
1af03 65 61 64 20 3d 20 69 41 6d 74 3b 0a 20 20 69 6e  ead = iAmt;.  in
1af04 74 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 3b 0a  t iChunkOffset;.
1af05 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68    FileChunk *pCh
1af06 75 6e 6b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  unk;..  assert( 
1af07 69 4f 66 73 74 2b 69 41 6d 74 3c 3d 70 2d 3e 65  iOfst+iAmt<=p->e
1af08 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20  ndpoint.iOffset 
1af09 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 72 65 61  );..  if( p->rea
1af0a 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 21 3d  dpoint.iOffset!=
1af0b 69 4f 66 73 74 20 7c 7c 20 69 4f 66 73 74 3d 3d  iOfst || iOfst==
1af0c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
1af0d 5f 69 6e 74 36 34 20 69 4f 66 66 20 3d 20 30 3b  _int64 iOff = 0;
1af0e 0a 20 20 20 20 66 6f 72 28 70 43 68 75 6e 6b 3d  .    for(pChunk=
1af0f 70 2d 3e 70 46 69 72 73 74 3b 20 0a 20 20 20 20  p->pFirst; .    
1af10 20 20 20 20 70 43 68 75 6e 6b 20 26 26 20 28 69      pChunk && (i
1af11 4f 66 66 2b 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e  Off+JOURNAL_CHUN
1af12 4b 53 49 5a 45 29 3c 3d 69 4f 66 73 74 3b 0a 20  KSIZE)<=iOfst;. 
1af13 20 20 20 20 20 20 20 70 43 68 75 6e 6b 3d 70 43         pChunk=pC
1af14 68 75 6e 6b 2d 3e 70 4e 65 78 74 0a 20 20 20 20  hunk->pNext.    
1af15 29 7b 0a 20 20 20 20 20 20 69 4f 66 66 20 2b 3d  ){.      iOff +=
1af16 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49   JOURNAL_CHUNKSI
1af17 5a 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  ZE;.    }.  }els
1af18 65 7b 0a 20 20 20 20 70 43 68 75 6e 6b 20 3d 20  e{.    pChunk = 
1af19 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 70 43 68  p->readpoint.pCh
1af1a 75 6e 6b 3b 0a 20 20 7d 0a 0a 20 20 69 43 68 75  unk;.  }..  iChu
1af1b 6e 6b 4f 66 66 73 65 74 20 3d 20 28 69 4f 66 73  nkOffset = (iOfs
1af1c 74 25 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53  t%JOURNAL_CHUNKS
1af1d 49 5a 45 29 3b 0a 20 20 64 6f 20 7b 0a 20 20 20  IZE);.  do {.   
1af1e 20 69 6e 74 20 69 53 70 61 63 65 20 3d 20 4a 4f   int iSpace = JO
1af1f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20  URNAL_CHUNKSIZE 
1af20 2d 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 3b 0a  - iChunkOffset;.
1af21 20 20 20 20 69 6e 74 20 6e 43 6f 70 79 20 3d 20      int nCopy = 
1af22 4d 49 4e 28 6e 52 65 61 64 2c 20 28 4a 4f 55 52  MIN(nRead, (JOUR
1af23 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20  NAL_CHUNKSIZE - 
1af24 69 43 68 75 6e 6b 4f 66 66 73 65 74 29 29 3b 0a  iChunkOffset));.
1af25 20 20 20 20 6d 65 6d 63 70 79 28 7a 4f 75 74 2c      memcpy(zOut,
1af26 20 26 70 43 68 75 6e 6b 2d 3e 7a 43 68 75 6e 6b   &pChunk->zChunk
1af27 5b 69 43 68 75 6e 6b 4f 66 66 73 65 74 5d 2c 20  [iChunkOffset], 
1af28 6e 43 6f 70 79 29 3b 0a 20 20 20 20 7a 4f 75 74  nCopy);.    zOut
1af29 20 2b 3d 20 6e 43 6f 70 79 3b 0a 20 20 20 20 6e   += nCopy;.    n
1af2a 52 65 61 64 20 2d 3d 20 69 53 70 61 63 65 3b 0a  Read -= iSpace;.
1af2b 20 20 20 20 69 43 68 75 6e 6b 4f 66 66 73 65 74      iChunkOffset
1af2c 20 3d 20 30 3b 0a 20 20 7d 20 77 68 69 6c 65 28   = 0;.  } while(
1af2d 20 6e 52 65 61 64 3e 3d 30 20 26 26 20 28 70 43   nRead>=0 && (pC
1af2e 68 75 6e 6b 3d 70 43 68 75 6e 6b 2d 3e 70 4e 65  hunk=pChunk->pNe
1af2f 78 74 29 20 26 26 20 6e 52 65 61 64 3e 30 20 29  xt) && nRead>0 )
1af30 3b 0a 20 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74  ;.  p->readpoint
1af31 2e 69 4f 66 66 73 65 74 20 3d 20 69 4f 66 73 74  .iOffset = iOfst
1af32 2b 69 41 6d 74 3b 0a 20 20 70 2d 3e 72 65 61 64  +iAmt;.  p->read
1af33 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 20 3d 20 70  point.pChunk = p
1af34 43 68 75 6e 6b 3b 0a 0a 20 20 72 65 74 75 72 6e  Chunk;..  return
1af35 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
1af36 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20  *.** Write data 
1af37 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a  to the file..*/.
1af38 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72  static int memjr
1af39 6e 6c 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74  nlWrite(.  sqlit
1af3a 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20  e3_file *pJfd,  
1af3b 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c    /* The journal
1af3c 20 66 69 6c 65 20 69 6e 74 6f 20 77 68 69 63 68   file into which
1af3d 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 63   to write */.  c
1af3e 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 2c  onst void *zBuf,
1af3f 20 20 20 20 20 20 2f 2a 20 54 61 6b 65 20 64 61        /* Take da
1af40 74 61 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e  ta to be written
1af41 20 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 20 20   from here */.  
1af42 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20  int iAmt,       
1af43 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1af44 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69   of bytes to wri
1af45 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69  te */.  sqlite_i
1af46 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f  nt64 iOfst     /
1af47 2a 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67 20  * Begin writing 
1af48 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 69  at this offset i
1af49 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a  nto the file */.
1af4a 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20  ){.  MemJournal 
1af4b 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c  *p = (MemJournal
1af4c 20 2a 29 70 4a 66 64 3b 0a 20 20 69 6e 74 20 6e   *)pJfd;.  int n
1af4d 57 72 69 74 65 20 3d 20 69 41 6d 74 3b 0a 20 20  Write = iAmt;.  
1af4e 75 38 20 2a 7a 57 72 69 74 65 20 3d 20 28 75 38  u8 *zWrite = (u8
1af4f 20 2a 29 7a 42 75 66 3b 0a 0a 20 20 2f 2a 20 41   *)zBuf;..  /* A
1af50 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72  n in-memory jour
1af51 6e 61 6c 20 66 69 6c 65 20 73 68 6f 75 6c 64 20  nal file should 
1af52 6f 6e 6c 79 20 65 76 65 72 20 62 65 20 61 70 70  only ever be app
1af53 65 6e 64 65 64 20 74 6f 2e 20 52 61 6e 64 6f 6d  ended to. Random
1af54 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 77 72 69  .  ** access wri
1af55 74 65 73 20 61 72 65 20 6e 6f 74 20 72 65 71 75  tes are not requ
1af56 69 72 65 64 20 62 79 20 73 71 6c 69 74 65 2e 0a  ired by sqlite..
1af57 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 69 4f    */.  assert(iO
1af58 66 73 74 3d 3d 70 2d 3e 65 6e 64 70 6f 69 6e 74  fst==p->endpoint
1af59 2e 69 4f 66 66 73 65 74 29 3b 0a 0a 20 20 77 68  .iOffset);..  wh
1af5a 69 6c 65 28 20 6e 57 72 69 74 65 3e 30 20 29 7b  ile( nWrite>0 ){
1af5b 0a 20 20 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a  .    FileChunk *
1af5c 70 43 68 75 6e 6b 20 3d 20 70 2d 3e 65 6e 64 70  pChunk = p->endp
1af5d 6f 69 6e 74 2e 70 43 68 75 6e 6b 3b 0a 20 20 20  oint.pChunk;.   
1af5e 20 69 6e 74 20 69 43 68 75 6e 6b 4f 66 66 73 65   int iChunkOffse
1af5f 74 20 3d 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e  t = p->endpoint.
1af60 69 4f 66 66 73 65 74 25 4a 4f 55 52 4e 41 4c 5f  iOffset%JOURNAL_
1af61 43 48 55 4e 4b 53 49 5a 45 3b 0a 20 20 20 20 69  CHUNKSIZE;.    i
1af62 6e 74 20 69 53 70 61 63 65 20 3d 20 4d 49 4e 28  nt iSpace = MIN(
1af63 6e 57 72 69 74 65 2c 20 4a 4f 55 52 4e 41 4c 5f  nWrite, JOURNAL_
1af64 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68 75  CHUNKSIZE - iChu
1af65 6e 6b 4f 66 66 73 65 74 29 3b 0a 0a 20 20 20 20  nkOffset);..    
1af66 69 66 28 20 69 43 68 75 6e 6b 4f 66 66 73 65 74  if( iChunkOffset
1af67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ==0 ){.      /* 
1af68 4e 65 77 20 63 68 75 6e 6b 20 69 73 20 72 65 71  New chunk is req
1af69 75 69 72 65 64 20 74 6f 20 65 78 74 65 6e 64 20  uired to extend 
1af6a 74 68 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20  the file. */.   
1af6b 20 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 4e     FileChunk *pN
1af6c 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  ew = sqlite3_mal
1af6d 6c 6f 63 28 73 69 7a 65 6f 66 28 46 69 6c 65 43  loc(sizeof(FileC
1af6e 68 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 69 66  hunk));.      if
1af6f 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 20  ( !pNew ){.     
1af70 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1af71 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20  _IOERR_NOMEM;.  
1af72 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4e 65 77      }.      pNew
1af73 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20  ->pNext = 0;.   
1af74 20 20 20 69 66 28 20 70 43 68 75 6e 6b 20 29 7b     if( pChunk ){
1af75 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1af76 20 70 2d 3e 70 46 69 72 73 74 20 29 3b 0a 20 20   p->pFirst );.  
1af77 20 20 20 20 20 20 70 43 68 75 6e 6b 2d 3e 70 4e        pChunk->pN
1af78 65 78 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20  ext = pNew;.    
1af79 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1af7a 20 61 73 73 65 72 74 28 20 21 70 2d 3e 70 46 69   assert( !p->pFi
1af7b 72 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20 70  rst );.        p
1af7c 2d 3e 70 46 69 72 73 74 20 3d 20 70 4e 65 77 3b  ->pFirst = pNew;
1af7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
1af7e 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e  ->endpoint.pChun
1af7f 6b 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 7d 0a  k = pNew;.    }.
1af80 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e  .    memcpy(&p->
1af81 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 2d  endpoint.pChunk-
1af82 3e 7a 43 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66  >zChunk[iChunkOf
1af83 66 73 65 74 5d 2c 20 7a 57 72 69 74 65 2c 20 69  fset], zWrite, i
1af84 53 70 61 63 65 29 3b 0a 20 20 20 20 7a 57 72 69  Space);.    zWri
1af85 74 65 20 2b 3d 20 69 53 70 61 63 65 3b 0a 20 20  te += iSpace;.  
1af86 20 20 6e 57 72 69 74 65 20 2d 3d 20 69 53 70 61    nWrite -= iSpa
1af87 63 65 3b 0a 20 20 20 20 70 2d 3e 65 6e 64 70 6f  ce;.    p->endpo
1af88 69 6e 74 2e 69 4f 66 66 73 65 74 20 2b 3d 20 69  int.iOffset += i
1af89 53 70 61 63 65 3b 0a 20 20 7d 0a 0a 20 20 72 65  Space;.  }..  re
1af8a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1af8b 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74  }../*.** Truncat
1af8c 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73  e the file..*/.s
1af8d 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e  tatic int memjrn
1af8e 6c 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65  lTruncate(sqlite
1af8f 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71  3_file *pJfd, sq
1af90 6c 69 74 65 5f 69 6e 74 36 34 20 73 69 7a 65 29  lite_int64 size)
1af91 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a  {.  MemJournal *
1af92 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20  p = (MemJournal 
1af93 2a 29 70 4a 66 64 3b 0a 20 20 46 69 6c 65 43 68  *)pJfd;.  FileCh
1af94 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 0a 20 20 61  unk *pChunk;.  a
1af95 73 73 65 72 74 28 73 69 7a 65 3d 3d 30 29 3b 0a  ssert(size==0);.
1af96 20 20 70 43 68 75 6e 6b 20 3d 20 70 2d 3e 70 46    pChunk = p->pF
1af97 69 72 73 74 3b 0a 20 20 77 68 69 6c 65 28 20 70  irst;.  while( p
1af98 43 68 75 6e 6b 20 29 7b 0a 20 20 20 20 46 69 6c  Chunk ){.    Fil
1af99 65 43 68 75 6e 6b 20 2a 70 54 6d 70 20 3d 20 70  eChunk *pTmp = p
1af9a 43 68 75 6e 6b 3b 0a 20 20 20 20 70 43 68 75 6e  Chunk;.    pChun
1af9b 6b 20 3d 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78  k = pChunk->pNex
1af9c 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  t;.    sqlite3_f
1af9d 72 65 65 28 70 54 6d 70 29 3b 0a 20 20 7d 0a 20  ree(pTmp);.  }. 
1af9e 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e   sqlite3MemJourn
1af9f 61 6c 4f 70 65 6e 28 70 4a 66 64 29 3b 0a 20 20  alOpen(pJfd);.  
1afa0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1afa1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  ;.}../*.** Close
1afa2 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74   the file..*/.st
1afa3 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c  atic int memjrnl
1afa4 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69  Close(sqlite3_fi
1afa5 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20 6d 65 6d  le *pJfd){.  mem
1afa6 6a 72 6e 6c 54 72 75 6e 63 61 74 65 28 70 4a 66  jrnlTruncate(pJf
1afa7 64 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20  d, 0);.  return 
1afa8 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f  SQLITE_OK;.}.../
1afa9 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 66 69  *.** Sync the fi
1afaa 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
1afab 74 20 6d 65 6d 6a 72 6e 6c 53 79 6e 63 28 73 71  t memjrnlSync(sq
1afac 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64  lite3_file *pJfd
1afad 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20  , int flags){.  
1afae 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1afaf 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79  ;.}../*.** Query
1afb0 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
1afb1 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 2e 0a   file in bytes..
1afb2 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65  */.static int me
1afb3 6d 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 28 73 71  mjrnlFileSize(sq
1afb4 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64  lite3_file *pJfd
1afb5 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a  , sqlite_int64 *
1afb6 70 53 69 7a 65 29 7b 0a 20 20 4d 65 6d 4a 6f 75  pSize){.  MemJou
1afb7 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f  rnal *p = (MemJo
1afb8 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20  urnal *)pJfd;.  
1afb9 2a 70 53 69 7a 65 20 3d 20 28 73 71 6c 69 74 65  *pSize = (sqlite
1afba 5f 69 6e 74 36 34 29 20 70 2d 3e 65 6e 64 70 6f  _int64) p->endpo
1afbb 69 6e 74 2e 69 4f 66 66 73 65 74 3b 0a 20 20 72  int.iOffset;.  r
1afbc 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1afbd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65 20  .}../*.** Table 
1afbe 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 4d  of methods for M
1afbf 65 6d 4a 6f 75 72 6e 61 6c 20 73 71 6c 69 74 65  emJournal sqlite
1afc0 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a  3_file object..*
1afc1 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20  /.static struct 
1afc2 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
1afc3 64 73 20 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74  ds MemJournalMet
1afc4 68 6f 64 73 20 3d 20 7b 0a 20 20 31 2c 20 20 20  hods = {.  1,   
1afc5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1afc6 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6d 65  iVersion */.  me
1afc7 6d 6a 72 6e 6c 43 6c 6f 73 65 2c 20 20 20 20 20  mjrnlClose,     
1afc8 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 0a 20 20 6d  /* xClose */.  m
1afc9 65 6d 6a 72 6e 6c 52 65 61 64 2c 20 20 20 20 20  emjrnlRead,     
1afca 20 2f 2a 20 78 52 65 61 64 20 2a 2f 0a 20 20 6d   /* xRead */.  m
1afcb 65 6d 6a 72 6e 6c 57 72 69 74 65 2c 20 20 20 20  emjrnlWrite,    
1afcc 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f 0a 20 20   /* xWrite */.  
1afcd 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 61 74 65 2c  memjrnlTruncate,
1afce 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a    /* xTruncate *
1afcf 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 53 79 6e 63 2c  /.  memjrnlSync,
1afd0 20 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a        /* xSync *
1afd1 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 46 69 6c 65 53  /.  memjrnlFileS
1afd2 69 7a 65 2c 20 20 2f 2a 20 78 46 69 6c 65 53 69  ize,  /* xFileSi
1afd3 7a 65 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20  ze */.  0,      
1afd4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f            /* xLo
1afd5 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20  ck */.  0,      
1afd6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e            /* xUn
1afd7 6c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20  lock */.  0,    
1afd8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
1afd9 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
1afda 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20  k */.  0,       
1afdb 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c           /* xFil
1afdc 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c  eControl */.  0,
1afdd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1afde 2f 2a 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a  /* xSectorSize *
1afdf 2f 0a 20 20 30 20 20 20 20 20 20 20 20 20 20 20  /.  0           
1afe0 20 20 20 20 20 20 2f 2a 20 78 44 65 76 69 63 65        /* xDevice
1afe1 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20  Characteristics 
1afe2 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70  */.};../* .** Op
1afe3 65 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  en a journal fil
1afe4 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1afe5 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1afe6 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28  3MemJournalOpen(
1afe7 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a  sqlite3_file *pJ
1afe8 66 64 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61  fd){.  MemJourna
1afe9 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e  l *p = (MemJourn
1afea 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 6d 65 6d  al *)pJfd;.  mem
1afeb 73 65 74 28 70 2c 20 30 2c 20 73 71 6c 69 74 65  set(p, 0, sqlite
1afec 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28  3MemJournalSize(
1afed 29 29 3b 0a 20 20 70 2d 3e 70 4d 65 74 68 6f 64  ));.  p->pMethod
1afee 20 3d 20 26 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65   = &MemJournalMe
1afef 74 68 6f 64 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  thods;.}../*.** 
1aff0 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74  Return true if t
1aff1 68 65 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20 70  he file-handle p
1aff2 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67 75  assed as an argu
1aff3 6d 65 6e 74 20 69 73 20 0a 2a 2a 20 61 6e 20 69  ment is .** an i
1aff4 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c  n-memory journal
1aff5 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
1aff6 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
1aff7 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69  sMemJournal(sqli
1aff8 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b  te3_file *pJfd){
1aff9 0a 20 20 72 65 74 75 72 6e 20 70 4a 66 64 2d 3e  .  return pJfd->
1affa 70 4d 65 74 68 6f 64 73 3d 3d 26 4d 65 6d 4a 6f  pMethods==&MemJo
1affb 75 72 6e 61 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a  urnalMethods;.}.
1affc 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ./* .** Return t
1affd 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
1affe 65 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 73  es required to s
1afff 74 6f 72 65 20 61 20 4d 65 6d 4a 6f 75 72 6e 61  tore a MemJourna
1b000 6c 20 74 68 61 74 20 75 73 65 73 20 76 66 73 0a  l that uses vfs.
1b001 2a 2a 20 70 56 66 73 20 74 6f 20 63 72 65 61 74  ** pVfs to creat
1b002 65 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  e the underlying
1b003 20 6f 6e 2d 64 69 73 6b 20 66 69 6c 65 73 2e 0a   on-disk files..
1b004 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1b005 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d  E int sqlite3Mem
1b006 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 29 7b 0a 20  JournalSize(){. 
1b007 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66 28 4d   return sizeof(M
1b008 65 6d 4a 6f 75 72 6e 61 6c 29 3b 0a 7d 0a 0a 2f  emJournal);.}../
1b009 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
1b00a 6e 64 20 6f 66 20 6d 65 6d 6a 6f 75 72 6e 61 6c  nd of memjournal
1b00b 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1b00c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b00d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
1b00e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
1b00f 65 67 69 6e 20 66 69 6c 65 20 77 61 6c 6b 65 72  egin file walker
1b010 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1b011 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b012 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
1b013 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 73 74  *.** 2008 August
1b014 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   16.**.** The au
1b015 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
1b016 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
1b017 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
1b018 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
1b019 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
1b01a 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
1b01b 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
1b01c 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
1b01d 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
1b01e 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
1b01f 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
1b020 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
1b021 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
1b022 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
1b023 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
1b024 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
1b025 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
1b026 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b027 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b028 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b029 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b02a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
1b02b 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74 69 6e 65  contains routine
1b02c 73 20 75 73 65 64 20 66 6f 72 20 77 61 6c 6b 69  s used for walki
1b02d 6e 67 20 74 68 65 20 70 61 72 73 65 72 20 74 72  ng the parser tr
1b02e 65 65 20 66 6f 72 0a 2a 2a 20 61 6e 20 53 51 4c  ee for.** an SQL
1b02f 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a   statement..**.*
1b030 2a 20 24 49 64 3a 20 77 61 6c 6b 65 72 2e 63 2c  * $Id: walker.c,
1b031 76 20 31 2e 31 20 32 30 30 38 2f 30 38 2f 32 30  v 1.1 2008/08/20
1b032 20 31 36 3a 33 35 3a 31 30 20 64 72 68 20 45 78   16:35:10 drh Ex
1b033 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 57  p $.*/.../*.** W
1b034 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 69 6f  alk an expressio
1b035 6e 20 74 72 65 65 2e 20 20 49 6e 76 6f 6b 65 20  n tree.  Invoke 
1b036 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 63  the callback onc
1b037 65 20 66 6f 72 20 65 61 63 68 20 6e 6f 64 65 0a  e for each node.
1b038 2a 2a 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  ** of the expres
1b039 73 69 6f 6e 2c 20 77 68 69 6c 65 20 64 65 63 65  sion, while dece
1b03a 6e 64 69 6e 67 2e 20 20 28 49 6e 20 6f 74 68 65  nding.  (In othe
1b03b 72 20 77 6f 72 64 73 2c 20 74 68 65 20 63 61 6c  r words, the cal
1b03c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 69 6e 76 6f  lback.** is invo
1b03d 6b 65 64 20 62 65 66 6f 72 65 20 76 69 73 69 74  ked before visit
1b03e 69 6e 67 20 63 68 69 6c 64 72 65 6e 2e 29 0a 2a  ing children.).*
1b03f 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20  *.** The return 
1b040 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 63  value from the c
1b041 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 20 62  allback should b
1b042 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 57 52 43  e one of the WRC
1b043 5f 2a 0a 2a 2a 20 63 6f 6e 73 74 61 6e 74 73 20  _*.** constants 
1b044 74 6f 20 73 70 65 63 69 66 79 20 68 6f 77 20 74  to specify how t
1b045 6f 20 70 72 6f 63 65 65 64 20 77 69 74 68 20 74  o proceed with t
1b046 68 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a 2a 20 20  he walk..**.**  
1b047 20 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 20    WRC_Continue  
1b048 20 20 20 20 43 6f 6e 74 69 6e 75 65 20 64 65 73      Continue des
1b049 63 65 6e 64 69 6e 67 20 64 6f 77 6e 20 74 68 65  cending down the
1b04a 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20   tree..**.**    
1b04b 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20 20 20  WRC_Prune       
1b04c 20 20 44 6f 20 6e 6f 74 20 64 65 73 63 65 6e 64    Do not descend
1b04d 20 69 6e 74 6f 20 63 68 69 6c 64 20 6e 6f 64 65   into child node
1b04e 73 2e 20 20 42 75 74 20 61 6c 6c 6f 77 0a 2a 2a  s.  But allow.**
1b04f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b050 20 20 20 20 20 20 74 68 65 20 77 61 6c 6b 20 74        the walk t
1b051 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20  o continue with 
1b052 73 69 62 6c 69 6e 67 20 6e 6f 64 65 73 2e 0a 2a  sibling nodes..*
1b053 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 41 62 6f 72  *.**    WRC_Abor
1b054 74 20 20 20 20 20 20 20 20 20 44 6f 20 6e 6f 20  t         Do no 
1b055 6d 6f 72 65 20 63 61 6c 6c 62 61 63 6b 73 2e 20  more callbacks. 
1b056 20 55 6e 77 69 6e 64 20 74 68 65 20 73 74 61 63   Unwind the stac
1b057 6b 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20  k and.**        
1b058 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65                re
1b059 74 75 72 6e 20 74 68 65 20 74 6f 70 2d 6c 65 76  turn the top-lev
1b05a 65 6c 20 77 61 6c 6b 20 63 61 6c 6c 2e 0a 2a 2a  el walk call..**
1b05b 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76  .** The return v
1b05c 61 6c 75 65 20 66 72 6f 6d 20 74 68 69 73 20 72  alue from this r
1b05d 6f 75 74 69 6e 65 20 69 73 20 57 52 43 5f 41 62  outine is WRC_Ab
1b05e 6f 72 74 20 74 6f 20 61 62 61 6e 64 6f 6e 20 74  ort to abandon t
1b05f 68 65 20 74 72 65 65 20 77 61 6c 6b 0a 2a 2a 20  he tree walk.** 
1b060 61 6e 64 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65  and WRC_Continue
1b061 20 74 6f 20 63 6f 6e 74 69 6e 75 65 2e 0a 2a 2f   to continue..*/
1b062 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1b063 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 45  int sqlite3WalkE
1b064 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c  xpr(Walker *pWal
1b065 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70 72  ker, Expr *pExpr
1b066 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69  ){.  int rc;.  i
1b067 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65  f( pExpr==0 ) re
1b068 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75  turn WRC_Continu
1b069 65 3b 0a 20 20 72 63 20 3d 20 70 57 61 6c 6b 65  e;.  rc = pWalke
1b06a 72 2d 3e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b  r->xExprCallback
1b06b 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 29  (pWalker, pExpr)
1b06c 3b 0a 20 20 69 66 28 20 72 63 3d 3d 57 52 43 5f  ;.  if( rc==WRC_
1b06d 43 6f 6e 74 69 6e 75 65 20 29 7b 0a 20 20 20 20  Continue ){.    
1b06e 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45  if( sqlite3WalkE
1b06f 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 45 78  xpr(pWalker, pEx
1b070 70 72 2d 3e 70 4c 65 66 74 29 20 29 20 72 65 74  pr->pLeft) ) ret
1b071 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
1b072 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61     if( sqlite3Wa
1b073 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20  lkExpr(pWalker, 
1b074 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29 20 29  pExpr->pRight) )
1b075 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
1b076 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  t;.    if( sqlit
1b077 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70  e3WalkExprList(p
1b078 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70  Walker, pExpr->p
1b079 4c 69 73 74 29 20 29 20 72 65 74 75 72 6e 20 57  List) ) return W
1b07a 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 66  RC_Abort;.    if
1b07b 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c  ( sqlite3WalkSel
1b07c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 45 78  ect(pWalker, pEx
1b07d 70 72 2d 3e 70 53 65 6c 65 63 74 29 20 29 7b 0a  pr->pSelect) ){.
1b07e 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
1b07f 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20  _Abort;.    }.  
1b080 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26 20  }.  return rc & 
1b081 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a  WRC_Abort;.}../*
1b082 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33  .** Call sqlite3
1b083 57 61 6c 6b 45 78 70 72 28 29 20 66 6f 72 20 65  WalkExpr() for e
1b084 76 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20  very expression 
1b085 69 6e 20 6c 69 73 74 20 70 20 6f 72 20 75 6e 74  in list p or unt
1b086 69 6c 0a 2a 2a 20 61 6e 20 61 62 6f 72 74 20 72  il.** an abort r
1b087 65 71 75 65 73 74 20 69 73 20 73 65 65 6e 2e 0a  equest is seen..
1b088 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1b089 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c  E int sqlite3Wal
1b08a 6b 45 78 70 72 4c 69 73 74 28 57 61 6c 6b 65 72  kExprList(Walker
1b08b 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 4c   *pWalker, ExprL
1b08c 69 73 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69  ist *p){.  int i
1b08d 2c 20 72 63 20 3d 20 57 52 43 5f 43 6f 6e 74 69  , rc = WRC_Conti
1b08e 6e 75 65 3b 0a 20 20 73 74 72 75 63 74 20 45 78  nue;.  struct Ex
1b08f 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  prList_item *pIt
1b090 65 6d 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  em;.  if( p ){. 
1b091 20 20 20 66 6f 72 28 69 3d 70 2d 3e 6e 45 78 70     for(i=p->nExp
1b092 72 2c 20 70 49 74 65 6d 3d 70 2d 3e 61 3b 20 69  r, pItem=p->a; i
1b093 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b  >0; i--, pItem++
1b094 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  ){.      if( sql
1b095 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61  ite3WalkExpr(pWa
1b096 6c 6b 65 72 2c 20 70 49 74 65 6d 2d 3e 70 45 78  lker, pItem->pEx
1b097 70 72 29 20 29 20 72 65 74 75 72 6e 20 57 52 43  pr) ) return WRC
1b098 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20  _Abort;.    }.  
1b099 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26 20  }.  return rc & 
1b09a 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a  WRC_Continue;.}.
1b09b 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6c 6c 20  ./*.** Walk all 
1b09c 65 78 70 72 65 73 73 69 6f 6e 73 20 61 73 73 6f  expressions asso
1b09d 63 69 61 74 65 64 20 77 69 74 68 20 53 45 4c 45  ciated with SELE
1b09e 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70 2e 20  CT statement p. 
1b09f 20 44 6f 0a 2a 2a 20 6e 6f 74 20 69 6e 76 6f 6b   Do.** not invok
1b0a0 65 20 74 68 65 20 53 45 4c 45 43 54 20 63 61 6c  e the SELECT cal
1b0a1 6c 62 61 63 6b 20 6f 6e 20 70 2c 20 62 75 74 20  lback on p, but 
1b0a2 64 6f 20 28 6f 66 20 63 6f 75 72 73 65 29 20 69  do (of course) i
1b0a3 6e 76 6f 6b 65 0a 2a 2a 20 61 6e 79 20 65 78 70  nvoke.** any exp
1b0a4 72 20 63 61 6c 6c 62 61 63 6b 73 20 61 6e 64 20  r callbacks and 
1b0a5 53 45 4c 45 43 54 20 63 61 6c 6c 62 61 63 6b 73  SELECT callbacks
1b0a6 20 74 68 61 74 20 63 6f 6d 65 20 66 72 6f 6d 20   that come from 
1b0a7 73 75 62 71 75 65 72 69 65 73 2e 0a 2a 2a 20 52  subqueries..** R
1b0a8 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 20  eturn WRC_Abort 
1b0a9 6f 72 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 2e  or WRC_Continue.
1b0aa 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1b0ab 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61  TE int sqlite3Wa
1b0ac 6c 6b 53 65 6c 65 63 74 45 78 70 72 28 57 61 6c  lkSelectExpr(Wal
1b0ad 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65  ker *pWalker, Se
1b0ae 6c 65 63 74 20 2a 70 29 7b 0a 20 20 69 66 28 20  lect *p){.  if( 
1b0af 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c  sqlite3WalkExprL
1b0b0 69 73 74 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e  ist(pWalker, p->
1b0b1 70 45 4c 69 73 74 29 20 29 20 72 65 74 75 72 6e  pEList) ) return
1b0b2 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66   WRC_Abort;.  if
1b0b3 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70  ( sqlite3WalkExp
1b0b4 72 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 57  r(pWalker, p->pW
1b0b5 68 65 72 65 29 20 29 20 72 65 74 75 72 6e 20 57  here) ) return W
1b0b6 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20  RC_Abort;.  if( 
1b0b7 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c  sqlite3WalkExprL
1b0b8 69 73 74 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e  ist(pWalker, p->
1b0b9 70 47 72 6f 75 70 42 79 29 20 29 20 72 65 74 75  pGroupBy) ) retu
1b0ba 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
1b0bb 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45  if( sqlite3WalkE
1b0bc 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e  xpr(pWalker, p->
1b0bd 70 48 61 76 69 6e 67 29 20 29 20 72 65 74 75 72  pHaving) ) retur
1b0be 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69  n WRC_Abort;.  i
1b0bf 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78  f( sqlite3WalkEx
1b0c0 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c 20  prList(pWalker, 
1b0c1 70 2d 3e 70 4f 72 64 65 72 42 79 29 20 29 20 72  p->pOrderBy) ) r
1b0c2 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
1b0c3 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61  .  if( sqlite3Wa
1b0c4 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20  lkExpr(pWalker, 
1b0c5 70 2d 3e 70 4c 69 6d 69 74 29 20 29 20 72 65 74  p->pLimit) ) ret
1b0c6 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
1b0c7 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b   if( sqlite3Walk
1b0c8 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 2d  Expr(pWalker, p-
1b0c9 3e 70 4f 66 66 73 65 74 29 20 29 20 72 65 74 75  >pOffset) ) retu
1b0ca 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
1b0cb 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69  return WRC_Conti
1b0cc 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61  nue;.}../*.** Wa
1b0cd 6c 6b 20 74 68 65 20 70 61 72 73 65 20 74 72 65  lk the parse tre
1b0ce 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  es associated wi
1b0cf 74 68 20 61 6c 6c 20 73 75 62 71 75 65 72 69 65  th all subquerie
1b0d0 73 20 69 6e 20 74 68 65 0a 2a 2a 20 46 52 4f 4d  s in the.** FROM
1b0d1 20 63 6c 61 75 73 65 20 6f 66 20 53 45 4c 45 43   clause of SELEC
1b0d2 54 20 73 74 61 74 65 6d 65 6e 74 20 70 2e 20 20  T statement p.  
1b0d3 44 6f 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 74 68  Do not invoke th
1b0d4 65 20 73 65 6c 65 63 74 0a 2a 2a 20 63 61 6c 6c  e select.** call
1b0d5 62 61 63 6b 20 6f 6e 20 70 2c 20 62 75 74 20 64  back on p, but d
1b0d6 6f 20 69 6e 76 6f 6b 65 20 69 74 20 6f 6e 20 65  o invoke it on e
1b0d7 61 63 68 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  ach FROM clause 
1b0d8 73 75 62 71 75 65 72 79 0a 2a 2a 20 61 6e 64 20  subquery.** and 
1b0d9 6f 6e 20 61 6e 79 20 73 75 62 71 75 65 72 69 65  on any subquerie
1b0da 73 20 66 75 72 74 68 65 72 20 64 6f 77 6e 20 69  s further down i
1b0db 6e 20 74 68 65 20 74 72 65 65 2e 20 20 52 65 74  n the tree.  Ret
1b0dc 75 72 6e 20 0a 2a 2a 20 57 52 43 5f 41 62 6f 72  urn .** WRC_Abor
1b0dd 74 20 6f 72 20 57 52 43 5f 43 6f 6e 74 69 6e 75  t or WRC_Continu
1b0de 65 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e;.*/.SQLITE_PRI
1b0df 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1b0e0 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28 57  WalkSelectFrom(W
1b0e1 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20  alker *pWalker, 
1b0e2 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 53 72  Select *p){.  Sr
1b0e3 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20 20 69  cList *pSrc;.  i
1b0e4 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 53  nt i;.  struct S
1b0e5 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  rcList_item *pIt
1b0e6 65 6d 3b 0a 0a 20 20 70 53 72 63 20 3d 20 70 2d  em;..  pSrc = p-
1b0e7 3e 70 53 72 63 3b 0a 20 20 69 66 28 20 70 53 72  >pSrc;.  if( pSr
1b0e8 63 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 70  c ){.    for(i=p
1b0e9 53 72 63 2d 3e 6e 53 72 63 2c 20 70 49 74 65 6d  Src->nSrc, pItem
1b0ea 3d 70 53 72 63 2d 3e 61 3b 20 69 3e 30 3b 20 69  =pSrc->a; i>0; i
1b0eb 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  --, pItem++){.  
1b0ec 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57      if( sqlite3W
1b0ed 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65  alkSelect(pWalke
1b0ee 72 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63  r, pItem->pSelec
1b0ef 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  t) ){.        re
1b0f0 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
1b0f1 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1b0f2 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43  }.  return WRC_C
1b0f3 6f 6e 74 69 6e 75 65 3b 0a 7d 20 0a 0a 2f 2a 0a  ontinue;.} ../*.
1b0f4 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 57  ** Call sqlite3W
1b0f5 61 6c 6b 45 78 70 72 28 29 20 66 6f 72 20 65 76  alkExpr() for ev
1b0f6 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69  ery expression i
1b0f7 6e 20 53 65 6c 65 63 74 20 73 74 61 74 65 6d 65  n Select stateme
1b0f8 6e 74 20 70 2e 0a 2a 2a 20 49 6e 76 6f 6b 65 20  nt p..** Invoke 
1b0f9 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
1b0fa 74 28 29 20 66 6f 72 20 73 75 62 71 75 65 72 69  t() for subqueri
1b0fb 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  es in the FROM c
1b0fc 6c 61 75 73 65 20 61 6e 64 0a 2a 2a 20 6f 6e 20  lause and.** on 
1b0fd 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c  the compound sel
1b0fe 65 63 74 20 63 68 61 69 6e 2c 20 70 2d 3e 70 50  ect chain, p->pP
1b0ff 72 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  rior..**.** Retu
1b100 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20  rn WRC_Continue 
1b101 75 6e 64 65 72 20 6e 6f 72 6d 61 6c 20 63 6f 6e  under normal con
1b102 64 69 74 69 6f 6e 73 2e 20 20 52 65 74 75 72 6e  ditions.  Return
1b103 20 57 52 43 5f 41 62 6f 72 74 20 69 66 0a 2a 2a   WRC_Abort if.**
1b104 20 74 68 65 72 65 20 69 73 20 61 6e 20 61 62 6f   there is an abo
1b105 72 74 20 72 65 71 75 65 73 74 2e 0a 2a 2a 0a 2a  rt request..**.*
1b106 2a 20 49 66 20 74 68 65 20 57 61 6c 6b 65 72 20  * If the Walker 
1b107 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 61 6e  does not have an
1b108 20 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b   xSelectCallback
1b109 28 29 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  () then this rou
1b10a 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d  tine.** is a no-
1b10b 6f 70 20 72 65 74 75 72 6e 69 6e 67 20 57 52 43  op returning WRC
1b10c 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51  _Continue..*/.SQ
1b10d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1b10e 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65   sqlite3WalkSele
1b10f 63 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b  ct(Walker *pWalk
1b110 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a  er, Select *p){.
1b111 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20    int rc;.  if( 
1b112 70 3d 3d 30 20 7c 7c 20 70 57 61 6c 6b 65 72 2d  p==0 || pWalker-
1b113 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b  >xSelectCallback
1b114 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43  ==0 ) return WRC
1b115 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 72 63 20  _Continue;.  rc 
1b116 3d 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a  = WRC_Continue;.
1b117 20 20 77 68 69 6c 65 28 20 70 20 20 29 7b 0a 20    while( p  ){. 
1b118 20 20 20 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d     rc = pWalker-
1b119 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b  >xSelectCallback
1b11a 28 70 57 61 6c 6b 65 72 2c 20 70 29 3b 0a 20 20  (pWalker, p);.  
1b11b 20 20 69 66 28 20 72 63 20 29 20 62 72 65 61 6b    if( rc ) break
1b11c 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
1b11d 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 70 72 28  3WalkSelectExpr(
1b11e 70 57 61 6c 6b 65 72 2c 20 70 29 20 29 20 72 65  pWalker, p) ) re
1b11f 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
1b120 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57      if( sqlite3W
1b121 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28 70 57  alkSelectFrom(pW
1b122 61 6c 6b 65 72 2c 20 70 29 20 29 20 72 65 74 75  alker, p) ) retu
1b123 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
1b124 20 20 70 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b    p = p->pPrior;
1b125 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1b126 20 26 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a   & WRC_Abort;.}.
1b127 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1b128 20 45 6e 64 20 6f 66 20 77 61 6c 6b 65 72 2e 63   End of walker.c
1b129 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1b12a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b12b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1b12c 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1b12d 20 42 65 67 69 6e 20 66 69 6c 65 20 72 65 73 6f   Begin file reso
1b12e 6c 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  lve.c **********
1b12f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1b131 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 75  ./*.** 2008 Augu
1b132 73 74 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20  st 18.**.** The 
1b133 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
1b134 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
1b135 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
1b136 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
1b137 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
1b138 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
1b139 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
1b13a 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
1b13b 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
1b13c 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
1b13d 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
1b13e 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
1b13f 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
1b140 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
1b141 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
1b142 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
1b143 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
1b144 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b145 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b146 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b147 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b148 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
1b149 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f  file contains ro
1b14a 75 74 69 6e 65 73 20 75 73 65 64 20 66 6f 72 20  utines used for 
1b14b 77 61 6c 6b 69 6e 67 20 74 68 65 20 70 61 72 73  walking the pars
1b14c 65 72 20 74 72 65 65 20 61 6e 64 0a 2a 2a 20 72  er tree and.** r
1b14d 65 73 6f 6c 76 65 20 61 6c 6c 20 69 64 65 6e 74  esolve all ident
1b14e 69 66 69 65 72 73 20 62 79 20 61 73 73 6f 63 69  ifiers by associ
1b14f 61 74 69 6e 67 20 74 68 65 6d 20 77 69 74 68 20  ating them with 
1b150 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20  a particular.** 
1b151 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e  table and column
1b152 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 65 73  ..**.** $Id: res
1b153 6f 6c 76 65 2e 63 2c 76 20 31 2e 31 30 20 32 30  olve.c,v 1.10 20
1b154 30 38 2f 31 30 2f 31 39 20 32 31 3a 30 33 3a 32  08/10/19 21:03:2
1b155 37 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  7 drh Exp $.*/..
1b156 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 68 65 20 70  /*.** Turn the p
1b157 45 78 70 72 20 65 78 70 72 65 73 73 69 6f 6e 20  Expr expression 
1b158 69 6e 74 6f 20 61 6e 20 61 6c 69 61 73 20 66 6f  into an alias fo
1b159 72 20 74 68 65 20 69 43 6f 6c 2d 74 68 20 63 6f  r the iCol-th co
1b15a 6c 75 6d 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 72  lumn of the.** r
1b15b 65 73 75 6c 74 20 73 65 74 20 69 6e 20 70 45 4c  esult set in pEL
1b15c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ist..**.** If th
1b15d 65 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c  e result set col
1b15e 75 6d 6e 20 69 73 20 61 20 73 69 6d 70 6c 65 20  umn is a simple 
1b15f 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65  column reference
1b160 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74  , then this rout
1b161 69 6e 65 0a 2a 2a 20 6d 61 6b 65 73 20 61 6e 20  ine.** makes an 
1b162 65 78 61 63 74 20 63 6f 70 79 2e 20 20 42 75 74  exact copy.  But
1b163 20 66 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 6b   for any other k
1b164 69 6e 64 20 6f 66 20 65 78 70 72 65 73 73 69 6f  ind of expressio
1b165 6e 2c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69  n, this.** routi
1b166 6e 65 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f  ne make a copy o
1b167 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  f the result set
1b168 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20 61   column as the a
1b169 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 0a 2a  rgument to the.*
1b16a 2a 20 54 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72  * TK_AS operator
1b16b 2e 20 20 54 68 65 20 54 4b 5f 41 53 20 6f 70 65  .  The TK_AS ope
1b16c 72 61 74 6f 72 20 63 61 75 73 65 73 20 74 68 65  rator causes the
1b16d 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62   expression to b
1b16e 65 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 6a  e.** evaluated j
1b16f 75 73 74 20 6f 6e 63 65 20 61 6e 64 20 74 68 65  ust once and the
1b170 6e 20 72 65 75 73 65 64 20 66 6f 72 20 65 61 63  n reused for eac
1b171 68 20 61 6c 69 61 73 2e 0a 2a 2a 0a 2a 2a 20 54  h alias..**.** T
1b172 68 65 20 72 65 61 73 6f 6e 20 66 6f 72 20 73 75  he reason for su
1b173 70 70 72 65 73 73 69 6e 67 20 74 68 65 20 54 4b  ppressing the TK
1b174 5f 41 53 20 74 65 72 6d 20 77 68 65 6e 20 74 68  _AS term when th
1b175 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
1b176 61 20 73 69 6d 70 6c 65 0a 2a 2a 20 63 6f 6c 75  a simple.** colu
1b177 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 69 73 20  mn reference is 
1b178 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75  so that the colu
1b179 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 77 69 6c  mn reference wil
1b17a 6c 20 62 65 20 72 65 63 6f 67 6e 69 7a 65 64 20  l be recognized 
1b17b 61 73 0a 2a 2a 20 75 73 61 62 6c 65 20 62 79 20  as.** usable by 
1b17c 69 6e 64 69 63 65 73 20 77 69 74 68 69 6e 20 74  indices within t
1b17d 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
1b17e 70 72 6f 63 65 73 73 69 6e 67 20 6c 6f 67 69 63  processing logic
1b17f 2e 20 0a 2a 2a 0a 2a 2a 20 48 61 63 6b 3a 20 20  . .**.** Hack:  
1b180 54 68 65 20 54 4b 5f 41 53 20 6f 70 65 72 61 74  The TK_AS operat
1b181 6f 72 20 69 73 20 69 6e 68 69 62 69 74 65 64 20  or is inhibited 
1b182 69 66 20 7a 54 79 70 65 5b 30 5d 3d 3d 27 47 27  if zType[0]=='G'
1b183 2e 20 20 54 68 69 73 20 6d 65 61 6e 73 0a 2a 2a  .  This means.**
1b184 20 74 68 61 74 20 69 6e 20 61 20 47 52 4f 55 50   that in a GROUP
1b185 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68 65 20   BY clause, the 
1b186 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 65 76  expression is ev
1b187 61 6c 75 61 74 65 64 20 74 77 69 63 65 2e 20 20  aluated twice.  
1b188 48 65 6e 63 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  Hence:.**.**    
1b189 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 28 29   SELECT random()
1b18a 25 35 20 41 53 20 78 2c 20 63 6f 75 6e 74 28 2a  %5 AS x, count(*
1b18b 29 20 46 52 4f 4d 20 74 61 62 20 47 52 4f 55 50  ) FROM tab GROUP
1b18c 20 42 59 20 78 0a 2a 2a 0a 2a 2a 20 49 73 20 65   BY x.**.** Is e
1b18d 71 75 69 76 61 6c 65 6e 74 20 74 6f 3a 0a 2a 2a  quivalent to:.**
1b18e 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 72  .**     SELECT r
1b18f 61 6e 64 6f 6d 28 29 25 35 20 41 53 20 78 2c 20  andom()%5 AS x, 
1b190 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 61  count(*) FROM ta
1b191 62 20 47 52 4f 55 50 20 42 59 20 72 61 6e 64 6f  b GROUP BY rando
1b192 6d 28 29 25 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  m()%5.**.** The 
1b193 72 65 73 75 6c 74 20 6f 66 20 72 61 6e 64 6f 6d  result of random
1b194 28 29 25 35 20 69 6e 20 74 68 65 20 47 52 4f 55  ()%5 in the GROU
1b195 50 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 70  P BY clause is p
1b196 72 6f 62 61 62 6c 79 20 64 69 66 66 65 72 65 6e  robably differen
1b197 74 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 72 65  t.** from the re
1b198 73 75 6c 74 20 69 6e 20 74 68 65 20 72 65 73 75  sult in the resu
1b199 6c 74 2d 73 65 74 2e 20 20 57 65 20 6d 69 67 68  lt-set.  We migh
1b19a 74 20 66 69 78 20 74 68 69 73 20 73 6f 6d 65 64  t fix this somed
1b19b 61 79 2e 20 20 4f 72 0a 2a 2a 20 74 68 65 6e 20  ay.  Or.** then 
1b19c 61 67 61 69 6e 2c 20 77 65 20 6d 69 67 68 74 20  again, we might 
1b19d 6e 6f 74 2e 2e 2e 0a 2a 2f 0a 73 74 61 74 69 63  not....*/.static
1b19e 20 76 6f 69 64 20 72 65 73 6f 6c 76 65 41 6c 69   void resolveAli
1b19f 61 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  as(.  Parse *pPa
1b1a0 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  rse,         /* 
1b1a1 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
1b1a2 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
1b1a3 45 4c 69 73 74 2c 20 20 20 20 20 20 2f 2a 20 41  EList,      /* A
1b1a4 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20   result set */. 
1b1a5 20 69 6e 74 20 69 43 6f 6c 2c 20 20 20 20 20 20   int iCol,      
1b1a6 20 20 20 20 20 20 20 20 2f 2a 20 41 20 63 6f 6c          /* A col
1b1a7 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c  umn in the resul
1b1a8 74 20 73 65 74 2e 20 20 30 2e 2e 70 45 4c 69 73  t set.  0..pELis
1b1a9 74 2d 3e 6e 45 78 70 72 2d 31 20 2a 2f 0a 20 20  t->nExpr-1 */.  
1b1aa 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20  Expr *pExpr,    
1b1ab 20 20 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 66         /* Transf
1b1ac 6f 72 6d 20 74 68 69 73 20 69 6e 74 6f 20 61 6e  orm this into an
1b1ad 20 61 6c 69 61 73 20 74 6f 20 74 68 65 20 72 65   alias to the re
1b1ae 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 63 6f  sult set */.  co
1b1af 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 20  nst char *zType 
1b1b0 20 20 20 20 20 2f 2a 20 22 47 52 4f 55 50 22 20       /* "GROUP" 
1b1b1 6f 72 20 22 4f 52 44 45 52 22 20 6f 72 20 22 22  or "ORDER" or ""
1b1b2 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70   */.){.  Expr *p
1b1b3 4f 72 69 67 3b 20 20 20 20 20 20 20 20 20 20 20  Orig;           
1b1b4 2f 2a 20 54 68 65 20 69 43 6f 6c 2d 74 68 20 63  /* The iCol-th c
1b1b5 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73  olumn of the res
1b1b6 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 45 78 70  ult set */.  Exp
1b1b7 72 20 2a 70 44 75 70 3b 20 20 20 20 20 20 20 20  r *pDup;        
1b1b8 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70      /* Copy of p
1b1b9 4f 72 69 67 20 2a 2f 0a 20 20 73 71 6c 69 74 65  Orig */.  sqlite
1b1ba 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20  3 *db;          
1b1bb 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
1b1bc 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a   connection */..
1b1bd 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d    assert( iCol>=
1b1be 30 20 26 26 20 69 43 6f 6c 3c 70 45 4c 69 73 74  0 && iCol<pEList
1b1bf 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20 70 4f 72  ->nExpr );.  pOr
1b1c0 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69  ig = pEList->a[i
1b1c1 43 6f 6c 5d 2e 70 45 78 70 72 3b 0a 20 20 61 73  Col].pExpr;.  as
1b1c2 73 65 72 74 28 20 70 4f 72 69 67 21 3d 30 20 29  sert( pOrig!=0 )
1b1c3 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 72 69  ;.  assert( pOri
1b1c4 67 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 52 65  g->flags & EP_Re
1b1c5 73 6f 6c 76 65 64 20 29 3b 0a 20 20 64 62 20 3d  solved );.  db =
1b1c6 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70   pParse->db;.  p
1b1c7 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70  Dup = sqlite3Exp
1b1c8 72 44 75 70 28 64 62 2c 20 70 4f 72 69 67 29 3b  rDup(db, pOrig);
1b1c9 0a 20 20 69 66 28 20 70 44 75 70 3d 3d 30 20 29  .  if( pDup==0 )
1b1ca 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70   return;.  if( p
1b1cb 44 75 70 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55  Dup->op!=TK_COLU
1b1cc 4d 4e 20 26 26 20 7a 54 79 70 65 5b 30 5d 21 3d  MN && zType[0]!=
1b1cd 27 47 27 20 29 7b 0a 20 20 20 20 70 44 75 70 20  'G' ){.    pDup 
1b1ce 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
1b1cf 50 61 72 73 65 2c 20 54 4b 5f 41 53 2c 20 70 44  Parse, TK_AS, pD
1b1d0 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69  up, 0, 0);.    i
1b1d1 66 28 20 70 44 75 70 3d 3d 30 20 29 20 72 65 74  f( pDup==0 ) ret
1b1d2 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 70 45 4c  urn;.    if( pEL
1b1d3 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c  ist->a[iCol].iAl
1b1d4 69 61 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ias==0 ){.      
1b1d5 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e  pEList->a[iCol].
1b1d6 69 41 6c 69 61 73 20 3d 20 2b 2b 70 50 61 72 73  iAlias = ++pPars
1b1d7 65 2d 3e 6e 41 6c 69 61 73 3b 0a 20 20 20 20 7d  e->nAlias;.    }
1b1d8 0a 20 20 20 20 70 44 75 70 2d 3e 69 54 61 62 6c  .    pDup->iTabl
1b1d9 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43  e = pEList->a[iC
1b1da 6f 6c 5d 2e 69 41 6c 69 61 73 3b 0a 20 20 7d 0a  ol].iAlias;.  }.
1b1db 20 20 69 66 28 20 70 45 78 70 72 2d 3e 66 6c 61    if( pExpr->fla
1b1dc 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61  gs & EP_ExpColla
1b1dd 74 65 20 29 7b 0a 20 20 20 20 70 44 75 70 2d 3e  te ){.    pDup->
1b1de 70 43 6f 6c 6c 20 3d 20 70 45 78 70 72 2d 3e 70  pColl = pExpr->p
1b1df 43 6f 6c 6c 3b 0a 20 20 20 20 70 44 75 70 2d 3e  Coll;.    pDup->
1b1e0 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43  flags |= EP_ExpC
1b1e1 6f 6c 6c 61 74 65 3b 0a 20 20 7d 0a 20 20 73 71  ollate;.  }.  sq
1b1e2 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 64  lite3ExprClear(d
1b1e3 62 2c 20 70 45 78 70 72 29 3b 0a 20 20 6d 65 6d  b, pExpr);.  mem
1b1e4 63 70 79 28 70 45 78 70 72 2c 20 70 44 75 70 2c  cpy(pExpr, pDup,
1b1e5 20 73 69 7a 65 6f 66 28 2a 70 45 78 70 72 29 29   sizeof(*pExpr))
1b1e6 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
1b1e7 65 28 64 62 2c 20 70 44 75 70 29 3b 0a 7d 0a 0a  e(db, pDup);.}..
1b1e8 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20  /*.** Given the 
1b1e9 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e  name of a column
1b1ea 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 58 2e 59   of the form X.Y
1b1eb 2e 5a 20 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73  .Z or Y.Z or jus
1b1ec 74 20 5a 2c 20 6c 6f 6f 6b 20 75 70 0a 2a 2a 20  t Z, look up.** 
1b1ed 74 68 61 74 20 6e 61 6d 65 20 69 6e 20 74 68 65  that name in the
1b1ee 20 73 65 74 20 6f 66 20 73 6f 75 72 63 65 20 74   set of source t
1b1ef 61 62 6c 65 73 20 69 6e 20 70 53 72 63 4c 69 73  ables in pSrcLis
1b1f0 74 20 61 6e 64 20 6d 61 6b 65 20 74 68 65 20 70  t and make the p
1b1f1 45 78 70 72 20 0a 2a 2a 20 65 78 70 72 65 73 73  Expr .** express
1b1f2 69 6f 6e 20 6e 6f 64 65 20 72 65 66 65 72 20 62  ion node refer b
1b1f3 61 63 6b 20 74 6f 20 74 68 61 74 20 73 6f 75 72  ack to that sour
1b1f4 63 65 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20  ce column.  The 
1b1f5 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 6e 67 65  following change
1b1f6 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 74 6f  s.** are made to
1b1f7 20 70 45 78 70 72 3a 0a 2a 2a 0a 2a 2a 20 20 20   pExpr:.**.**   
1b1f8 20 70 45 78 70 72 2d 3e 69 44 62 20 20 20 20 20   pExpr->iDb     
1b1f9 20 20 20 20 20 20 53 65 74 20 74 68 65 20 69 6e        Set the in
1b1fa 64 65 78 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d  dex in db->aDb[]
1b1fb 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1b1fc 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20   X.**           
1b1fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 65                (e
1b1fe 76 65 6e 20 69 66 20 58 20 69 73 20 69 6d 70 6c  ven if X is impl
1b1ff 69 65 64 29 2e 0a 2a 2a 20 20 20 20 70 45 78 70  ied)..**    pExp
1b200 72 2d 3e 69 54 61 62 6c 65 20 20 20 20 20 20 20  r->iTable       
1b201 20 53 65 74 20 74 6f 20 74 68 65 20 63 75 72 73   Set to the curs
1b202 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  or number for th
1b203 65 20 74 61 62 6c 65 20 6f 62 74 61 69 6e 65 64  e table obtained
1b204 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1b205 20 20 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d              from
1b206 20 70 53 72 63 4c 69 73 74 2e 0a 2a 2a 20 20 20   pSrcList..**   
1b207 20 70 45 78 70 72 2d 3e 70 54 61 62 20 20 20 20   pExpr->pTab    
1b208 20 20 20 20 20 20 50 6f 69 6e 74 73 20 74 6f 20        Points to 
1b209 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74  the Table struct
1b20a 75 72 65 20 6f 66 20 58 2e 59 20 28 65 76 65 6e  ure of X.Y (even
1b20b 20 69 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   if.**          
1b20c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 58                 X
1b20d 20 61 6e 64 2f 6f 72 20 59 20 61 72 65 20 69 6d   and/or Y are im
1b20e 70 6c 69 65 64 2e 29 0a 2a 2a 20 20 20 20 70 45  plied.).**    pE
1b20f 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 20 20 20  xpr->iColumn    
1b210 20 20 20 53 65 74 20 74 6f 20 74 68 65 20 63 6f     Set to the co
1b211 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68  lumn number with
1b212 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a  in the table..**
1b213 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 20 20      pExpr->op   
1b214 20 20 20 20 20 20 20 20 20 53 65 74 20 74 6f 20           Set to 
1b215 54 4b 5f 43 4f 4c 55 4d 4e 2e 0a 2a 2a 20 20 20  TK_COLUMN..**   
1b216 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 20 20   pExpr->pLeft   
1b217 20 20 20 20 20 20 41 6e 79 20 65 78 70 72 65 73        Any expres
1b218 73 69 6f 6e 20 74 68 69 73 20 70 6f 69 6e 74 73  sion this points
1b219 20 74 6f 20 69 73 20 64 65 6c 65 74 65 64 0a 2a   to is deleted.*
1b21a 2a 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67  *    pExpr->pRig
1b21b 68 74 20 20 20 20 20 20 20 20 41 6e 79 20 65 78  ht        Any ex
1b21c 70 72 65 73 73 69 6f 6e 20 74 68 69 73 20 70 6f  pression this po
1b21d 69 6e 74 73 20 74 6f 20 69 73 20 64 65 6c 65 74  ints to is delet
1b21e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 44  ed..**.** The pD
1b21f 62 54 6f 6b 65 6e 20 69 73 20 74 68 65 20 6e 61  bToken is the na
1b220 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
1b221 73 65 20 28 74 68 65 20 22 58 22 29 2e 20 20 54  se (the "X").  T
1b222 68 69 73 20 76 61 6c 75 65 20 6d 61 79 20 62 65  his value may be
1b223 0a 2a 2a 20 4e 55 4c 4c 20 6d 65 61 6e 69 6e 67  .** NULL meaning
1b224 20 74 68 61 74 20 6e 61 6d 65 20 69 73 20 6f 66   that name is of
1b225 20 74 68 65 20 66 6f 72 6d 20 59 2e 5a 20 6f 72   the form Y.Z or
1b226 20 5a 2e 20 20 41 6e 79 20 61 76 61 69 6c 61 62   Z.  Any availab
1b227 6c 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63  le database.** c
1b228 61 6e 20 62 65 20 75 73 65 64 2e 20 20 54 68 65  an be used.  The
1b229 20 70 54 61 62 6c 65 54 6f 6b 65 6e 20 69 73 20   pTableToken is 
1b22a 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
1b22b 74 61 62 6c 65 20 28 74 68 65 20 22 59 22 29 2e  table (the "Y").
1b22c 20 20 54 68 69 73 0a 2a 2a 20 76 61 6c 75 65 20    This.** value 
1b22d 63 61 6e 20 62 65 20 4e 55 4c 4c 20 69 66 20 70  can be NULL if p
1b22e 44 62 54 6f 6b 65 6e 20 69 73 20 61 6c 73 6f 20  DbToken is also 
1b22f 4e 55 4c 4c 2e 20 20 49 66 20 70 54 61 62 6c 65  NULL.  If pTable
1b230 54 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c 20 69 74  Token is NULL it
1b231 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20 74  .** means that t
1b232 68 65 20 66 6f 72 6d 20 6f 66 20 74 68 65 20 6e  he form of the n
1b233 61 6d 65 20 69 73 20 5a 20 61 6e 64 20 74 68 61  ame is Z and tha
1b234 74 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20 61  t columns from a
1b235 6e 79 20 74 61 62 6c 65 0a 2a 2a 20 63 61 6e 20  ny table.** can 
1b236 62 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  be used..**.** I
1b237 66 20 74 68 65 20 6e 61 6d 65 20 63 61 6e 6e 6f  f the name canno
1b238 74 20 62 65 20 72 65 73 6f 6c 76 65 64 20 75 6e  t be resolved un
1b239 61 6d 62 69 67 75 6f 75 73 6c 79 2c 20 6c 65 61  ambiguously, lea
1b23a 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  ve an error mess
1b23b 61 67 65 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65  age.** in pParse
1b23c 20 61 6e 64 20 72 65 74 75 72 6e 20 6e 6f 6e 2d   and return non-
1b23d 7a 65 72 6f 2e 20 20 52 65 74 75 72 6e 20 7a 65  zero.  Return ze
1b23e 72 6f 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a  ro on success..*
1b23f 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 6f  /.static int loo
1b240 6b 75 70 4e 61 6d 65 28 0a 20 20 50 61 72 73 65  kupName(.  Parse
1b241 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
1b242 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63  /* The parsing c
1b243 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65  ontext */.  Toke
1b244 6e 20 2a 70 44 62 54 6f 6b 65 6e 2c 20 20 20 20  n *pDbToken,    
1b245 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
1b246 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
1b247 69 6e 67 20 74 61 62 6c 65 2c 20 6f 72 20 4e 55  ing table, or NU
1b248 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  LL */.  Token *p
1b249 54 61 62 6c 65 54 6f 6b 65 6e 2c 20 20 2f 2a 20  TableToken,  /* 
1b24a 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 63 6f  Name of table co
1b24b 6e 74 61 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2c  ntaining column,
1b24c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f   or NULL */.  To
1b24d 6b 65 6e 20 2a 70 43 6f 6c 75 6d 6e 54 6f 6b 65  ken *pColumnToke
1b24e 6e 2c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68  n, /* Name of th
1b24f 65 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 4e  e column. */.  N
1b250 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c  ameContext *pNC,
1b251 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20      /* The name 
1b252 63 6f 6e 74 65 78 74 20 75 73 65 64 20 74 6f 20  context used to 
1b253 72 65 73 6f 6c 76 65 20 74 68 65 20 6e 61 6d 65  resolve the name
1b254 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70   */.  Expr *pExp
1b255 72 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61  r          /* Ma
1b256 6b 65 20 74 68 69 73 20 45 58 50 52 20 6e 6f 64  ke this EXPR nod
1b257 65 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73  e point to the s
1b258 65 6c 65 63 74 65 64 20 63 6f 6c 75 6d 6e 20 2a  elected column *
1b259 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 44 62  /.){.  char *zDb
1b25a 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e   = 0;       /* N
1b25b 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
1b25c 61 73 65 2e 20 20 54 68 65 20 22 58 22 20 69 6e  ase.  The "X" in
1b25d 20 58 2e 59 2e 5a 20 2a 2f 0a 20 20 63 68 61 72   X.Y.Z */.  char
1b25e 20 2a 7a 54 61 62 20 3d 20 30 3b 20 20 20 20 20   *zTab = 0;     
1b25f 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
1b260 74 61 62 6c 65 2e 20 20 54 68 65 20 22 59 22 20  table.  The "Y" 
1b261 69 6e 20 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20  in X.Y.Z or Y.Z 
1b262 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20  */.  char *zCol 
1b263 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d  = 0;      /* Nam
1b264 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e  e of the column.
1b265 20 20 54 68 65 20 22 5a 22 20 2a 2f 0a 20 20 69    The "Z" */.  i
1b266 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20  nt i, j;        
1b267 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
1b268 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e  ters */.  int cn
1b269 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  t = 0;          
1b26a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1b26b 75 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 69 6e  umber of matchin
1b26c 67 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a  g column names *
1b26d 2f 0a 20 20 69 6e 74 20 63 6e 74 54 61 62 20 3d  /.  int cntTab =
1b26e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
1b26f 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1b270 6f 66 20 6d 61 74 63 68 69 6e 67 20 74 61 62 6c  of matching tabl
1b271 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 73 71 6c  e names */.  sql
1b272 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
1b273 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20 2f  e->db;         /
1b274 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
1b275 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73  onnection */.  s
1b276 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
1b277 65 6d 20 2a 70 49 74 65 6d 3b 20 20 20 20 20 20  em *pItem;      
1b278 20 2f 2a 20 55 73 65 20 66 6f 72 20 6c 6f 6f 70   /* Use for loop
1b279 69 6e 67 20 6f 76 65 72 20 70 53 72 63 4c 69 73  ing over pSrcLis
1b27a 74 20 69 74 65 6d 73 20 2a 2f 0a 20 20 73 74 72  t items */.  str
1b27b 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
1b27c 20 2a 70 4d 61 74 63 68 20 3d 20 30 3b 20 20 2f   *pMatch = 0;  /
1b27d 2a 20 54 68 65 20 6d 61 74 63 68 69 6e 67 20 70  * The matching p
1b27e 53 72 63 4c 69 73 74 20 69 74 65 6d 20 2a 2f 0a  SrcList item */.
1b27f 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70    NameContext *p
1b280 54 6f 70 4e 43 20 3d 20 70 4e 43 3b 20 20 20 20  TopNC = pNC;    
1b281 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6e 61 6d      /* First nam
1b282 65 63 6f 6e 74 65 78 74 20 69 6e 20 74 68 65 20  econtext in the 
1b283 6c 69 73 74 20 2a 2f 0a 20 20 53 63 68 65 6d 61  list */.  Schema
1b284 20 2a 70 53 63 68 65 6d 61 20 3d 20 30 3b 20 20   *pSchema = 0;  
1b285 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
1b286 63 68 65 6d 61 20 6f 66 20 74 68 65 20 65 78 70  chema of the exp
1b287 72 65 73 73 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73  ression */..  as
1b288 73 65 72 74 28 20 70 43 6f 6c 75 6d 6e 54 6f 6b  sert( pColumnTok
1b289 65 6e 20 26 26 20 70 43 6f 6c 75 6d 6e 54 6f 6b  en && pColumnTok
1b28a 65 6e 2d 3e 7a 20 29 3b 20 2f 2a 20 54 68 65 20  en->z ); /* The 
1b28b 5a 20 69 6e 20 58 2e 59 2e 5a 20 63 61 6e 6e 6f  Z in X.Y.Z canno
1b28c 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 0a 20 20  t be NULL */..  
1b28d 2f 2a 20 44 65 71 75 6f 74 65 20 61 6e 64 20 7a  /* Dequote and z
1b28e 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 20 74 68  ero-terminate th
1b28f 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 7a 44 62  e names */.  zDb
1b290 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
1b291 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 44 62 54  omToken(db, pDbT
1b292 6f 6b 65 6e 29 3b 0a 20 20 7a 54 61 62 20 3d 20  oken);.  zTab = 
1b293 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
1b294 6f 6b 65 6e 28 64 62 2c 20 70 54 61 62 6c 65 54  oken(db, pTableT
1b295 6f 6b 65 6e 29 3b 0a 20 20 7a 43 6f 6c 20 3d 20  oken);.  zCol = 
1b296 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
1b297 6f 6b 65 6e 28 64 62 2c 20 70 43 6f 6c 75 6d 6e  oken(db, pColumn
1b298 54 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 64 62  Token);.  if( db
1b299 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1b29a 7b 0a 20 20 20 20 67 6f 74 6f 20 6c 6f 6f 6b 75  {.    goto looku
1b29b 70 6e 61 6d 65 5f 65 6e 64 3b 0a 20 20 7d 0a 0a  pname_end;.  }..
1b29c 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
1b29d 74 68 65 20 6e 6f 64 65 20 74 6f 20 6e 6f 2d 6d  the node to no-m
1b29e 61 74 63 68 20 2a 2f 0a 20 20 70 45 78 70 72 2d  atch */.  pExpr-
1b29f 3e 69 54 61 62 6c 65 20 3d 20 2d 31 3b 0a 20 20  >iTable = -1;.  
1b2a0 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b  pExpr->pTab = 0;
1b2a1 0a 0a 20 20 2f 2a 20 53 74 61 72 74 20 61 74 20  ..  /* Start at 
1b2a2 74 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 63  the inner-most c
1b2a3 6f 6e 74 65 78 74 20 61 6e 64 20 6d 6f 76 65 20  ontext and move 
1b2a4 6f 75 74 77 61 72 64 20 75 6e 74 69 6c 20 61 20  outward until a 
1b2a5 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 20 2a  match is found *
1b2a6 2f 0a 20 20 77 68 69 6c 65 28 20 70 4e 43 20 26  /.  while( pNC &
1b2a7 26 20 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20  & cnt==0 ){.    
1b2a8 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
1b2a9 3b 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70  ;.    SrcList *p
1b2aa 53 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70  SrcList = pNC->p
1b2ab 53 72 63 4c 69 73 74 3b 0a 0a 20 20 20 20 69 66  SrcList;..    if
1b2ac 28 20 70 53 72 63 4c 69 73 74 20 29 7b 0a 20 20  ( pSrcList ){.  
1b2ad 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74      for(i=0, pIt
1b2ae 65 6d 3d 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20  em=pSrcList->a; 
1b2af 69 3c 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63  i<pSrcList->nSrc
1b2b0 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
1b2b1 0a 20 20 20 20 20 20 20 20 54 61 62 6c 65 20 2a  .        Table *
1b2b2 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 69 6e  pTab;.        in
1b2b3 74 20 69 44 62 3b 0a 20 20 20 20 20 20 20 20 43  t iDb;.        C
1b2b4 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 0a  olumn *pCol;.  .
1b2b5 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70          pTab = p
1b2b6 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20  Item->pTab;.    
1b2b7 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62      assert( pTab
1b2b8 21 3d 30 20 26 26 20 70 54 61 62 2d 3e 7a 4e 61  !=0 && pTab->zNa
1b2b9 6d 65 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  me!=0 );.       
1b2ba 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
1b2bb 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
1b2bc 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
1b2bd 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1b2be 70 54 61 62 2d 3e 6e 43 6f 6c 3e 30 20 29 3b 0a  pTab->nCol>0 );.
1b2bf 20 20 20 20 20 20 20 20 69 66 28 20 7a 54 61 62          if( zTab
1b2c0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66   ){.          if
1b2c1 28 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20  ( pItem->zAlias 
1b2c2 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63  ){.            c
1b2c3 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20  har *zTabName = 
1b2c4 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20  pItem->zAlias;. 
1b2c5 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73             if( s
1b2c6 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54  qlite3StrICmp(zT
1b2c7 61 62 4e 61 6d 65 2c 20 7a 54 61 62 29 21 3d 30  abName, zTab)!=0
1b2c8 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
1b2c9 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
1b2ca 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
1b2cb 7a 54 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d  zTabName = pTab-
1b2cc 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20  >zName;.        
1b2cd 20 20 20 20 69 66 28 20 7a 54 61 62 4e 61 6d 65      if( zTabName
1b2ce 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 53 74  ==0 || sqlite3St
1b2cf 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65 2c 20  rICmp(zTabName, 
1b2d0 7a 54 61 62 29 21 3d 30 20 29 20 63 6f 6e 74 69  zTab)!=0 ) conti
1b2d1 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20  nue;.           
1b2d2 20 69 66 28 20 7a 44 62 21 3d 30 20 26 26 20 73   if( zDb!=0 && s
1b2d3 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 64 62  qlite3StrICmp(db
1b2d4 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
1b2d5 2c 20 7a 44 62 29 21 3d 30 20 29 7b 0a 20 20 20  , zDb)!=0 ){.   
1b2d6 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69             conti
1b2d7 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20  nue;.           
1b2d8 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20   }.          }. 
1b2d9 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1b2da 20 69 66 28 20 30 3d 3d 28 63 6e 74 54 61 62 2b   if( 0==(cntTab+
1b2db 2b 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  +) ){.          
1b2dc 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20  pExpr->iTable = 
1b2dd 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a  pItem->iCursor;.
1b2de 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
1b2df 3e 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20  >pTab = pTab;.  
1b2e0 20 20 20 20 20 20 20 20 70 53 63 68 65 6d 61 20          pSchema 
1b2e1 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b  = pTab->pSchema;
1b2e2 0a 20 20 20 20 20 20 20 20 20 20 70 4d 61 74 63  .          pMatc
1b2e3 68 20 3d 20 70 49 74 65 6d 3b 0a 20 20 20 20 20  h = pItem;.     
1b2e4 20 20 20 7d 0a 20 20 20 20 20 20 20 20 66 6f 72     }.        for
1b2e5 28 6a 3d 30 2c 20 70 43 6f 6c 3d 70 54 61 62 2d  (j=0, pCol=pTab-
1b2e6 3e 61 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e  >aCol; j<pTab->n
1b2e7 43 6f 6c 3b 20 6a 2b 2b 2c 20 70 43 6f 6c 2b 2b  Col; j++, pCol++
1b2e8 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
1b2e9 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
1b2ea 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f  pCol->zName, zCo
1b2eb 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  l)==0 ){.       
1b2ec 20 20 20 20 20 49 64 4c 69 73 74 20 2a 70 55 73       IdList *pUs
1b2ed 69 6e 67 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ing;.           
1b2ee 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20   cnt++;.        
1b2ef 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c      pExpr->iTabl
1b2f0 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73  e = pItem->iCurs
1b2f1 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  or;.            
1b2f2 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54  pExpr->pTab = pT
1b2f3 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ab;.            
1b2f4 70 4d 61 74 63 68 20 3d 20 70 49 74 65 6d 3b 0a  pMatch = pItem;.
1b2f5 20 20 20 20 20 20 20 20 20 20 20 20 70 53 63 68              pSch
1b2f6 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68  ema = pTab->pSch
1b2f7 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ema;.           
1b2f8 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20 74   /* Substitute t
1b2f9 68 65 20 72 6f 77 69 64 20 28 63 6f 6c 75 6d 6e  he rowid (column
1b2fa 20 2d 31 29 20 66 6f 72 20 74 68 65 20 49 4e 54   -1) for the INT
1b2fb 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
1b2fc 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
1b2fd 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d  pExpr->iColumn =
1b2fe 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20   j==pTab->iPKey 
1b2ff 3f 20 2d 31 20 3a 20 6a 3b 0a 20 20 20 20 20 20  ? -1 : j;.      
1b300 20 20 20 20 20 20 69 66 28 20 69 3c 70 53 72 63        if( i<pSrc
1b301 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 20 29 7b 0a  List->nSrc-1 ){.
1b302 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
1b303 28 20 70 49 74 65 6d 5b 31 5d 2e 6a 6f 69 6e 74  ( pItem[1].joint
1b304 79 70 65 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c  ype & JT_NATURAL
1b305 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1b306 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 6d      /* If this m
1b307 61 74 63 68 20 6f 63 63 75 72 72 65 64 20 69 6e  atch occurred in
1b308 20 74 68 65 20 6c 65 66 74 20 74 61 62 6c 65 20   the left table 
1b309 6f 66 20 61 20 6e 61 74 75 72 61 6c 20 6a 6f 69  of a natural joi
1b30a 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n,.             
1b30b 20 20 20 2a 2a 20 74 68 65 6e 20 73 6b 69 70 20     ** then skip 
1b30c 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20  the right table 
1b30d 74 6f 20 61 76 6f 69 64 20 61 20 64 75 70 6c 69  to avoid a dupli
1b30e 63 61 74 65 20 6d 61 74 63 68 20 2a 2f 0a 20 20  cate match */.  
1b30f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49                pI
1b310 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20  tem++;.         
1b311 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20         i++;.    
1b312 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20            }else 
1b313 69 66 28 20 28 70 55 73 69 6e 67 20 3d 20 70 49  if( (pUsing = pI
1b314 74 65 6d 5b 31 5d 2e 70 55 73 69 6e 67 29 21 3d  tem[1].pUsing)!=
1b315 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
1b316 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20       /* If this 
1b317 6d 61 74 63 68 20 6f 63 63 75 72 73 20 6f 6e 20  match occurs on 
1b318 61 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73  a column that is
1b319 20 69 6e 20 74 68 65 20 55 53 49 4e 47 20 63 6c   in the USING cl
1b31a 61 75 73 65 0a 20 20 20 20 20 20 20 20 20 20 20  ause.           
1b31b 20 20 20 20 20 2a 2a 20 6f 66 20 61 20 6a 6f 69       ** of a joi
1b31c 6e 2c 20 73 6b 69 70 20 74 68 65 20 73 65 61 72  n, skip the sear
1b31d 63 68 20 6f 66 20 74 68 65 20 72 69 67 68 74 20  ch of the right 
1b31e 74 61 62 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69  table of the joi
1b31f 6e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n.              
1b320 20 20 2a 2a 20 74 6f 20 61 76 6f 69 64 20 61 20    ** to avoid a 
1b321 64 75 70 6c 69 63 61 74 65 20 6d 61 74 63 68 20  duplicate match 
1b322 74 68 65 72 65 2e 20 2a 2f 0a 20 20 20 20 20 20  there. */.      
1b323 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b 3b            int k;
1b324 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1b325 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 55 73 69   for(k=0; k<pUsi
1b326 6e 67 2d 3e 6e 49 64 3b 20 6b 2b 2b 29 7b 0a 20  ng->nId; k++){. 
1b327 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b328 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
1b329 43 6d 70 28 70 55 73 69 6e 67 2d 3e 61 5b 6b 5d  Cmp(pUsing->a[k]
1b32a 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30  .zName, zCol)==0
1b32b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1b32c 20 20 20 20 20 20 20 20 70 49 74 65 6d 2b 2b 3b          pItem++;
1b32d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1b32e 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20       i++;.      
1b32f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72                br
1b330 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  eak;.           
1b331 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1b332 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1b333 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1b334 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1b335 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1b336 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1b337 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
1b338 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1b339 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20  OMIT_TRIGGER.   
1b33a 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 6e   /* If we have n
1b33b 6f 74 20 61 6c 72 65 61 64 79 20 72 65 73 6f 6c  ot already resol
1b33c 76 65 64 20 74 68 65 20 6e 61 6d 65 2c 20 74 68  ved the name, th
1b33d 65 6e 20 6d 61 79 62 65 20 0a 20 20 20 20 2a 2a  en maybe .    **
1b33e 20 69 74 20 69 73 20 61 20 6e 65 77 2e 2a 20 6f   it is a new.* o
1b33f 72 20 6f 6c 64 2e 2a 20 74 72 69 67 67 65 72 20  r old.* trigger 
1b340 61 72 67 75 6d 65 6e 74 20 72 65 66 65 72 65 6e  argument referen
1b341 63 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  ce.    */.    if
1b342 28 20 7a 44 62 3d 3d 30 20 26 26 20 7a 54 61 62  ( zDb==0 && zTab
1b343 21 3d 30 20 26 26 20 63 6e 74 3d 3d 30 20 26 26  !=0 && cnt==0 &&
1b344 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61   pParse->trigSta
1b345 63 6b 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 54  ck!=0 ){.      T
1b346 72 69 67 67 65 72 53 74 61 63 6b 20 2a 70 54 72  riggerStack *pTr
1b347 69 67 67 65 72 53 74 61 63 6b 20 3d 20 70 50 61  iggerStack = pPa
1b348 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 3b 0a  rse->trigStack;.
1b349 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61        Table *pTa
1b34a 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 75 33 32  b = 0;.      u32
1b34b 20 2a 70 69 43 6f 6c 4d 61 73 6b 3b 0a 20 20 20   *piColMask;.   
1b34c 20 20 20 69 66 28 20 70 54 72 69 67 67 65 72 53     if( pTriggerS
1b34d 74 61 63 6b 2d 3e 6e 65 77 49 64 78 20 21 3d 20  tack->newIdx != 
1b34e 2d 31 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  -1 && sqlite3Str
1b34f 49 43 6d 70 28 22 6e 65 77 22 2c 20 7a 54 61 62  ICmp("new", zTab
1b350 29 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 20 20  ) == 0 ){.      
1b351 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20    pExpr->iTable 
1b352 3d 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d  = pTriggerStack-
1b353 3e 6e 65 77 49 64 78 3b 0a 20 20 20 20 20 20 20  >newIdx;.       
1b354 20 61 73 73 65 72 74 28 20 70 54 72 69 67 67 65   assert( pTrigge
1b355 72 53 74 61 63 6b 2d 3e 70 54 61 62 20 29 3b 0a  rStack->pTab );.
1b356 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70          pTab = p
1b357 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 70 54  TriggerStack->pT
1b358 61 62 3b 0a 20 20 20 20 20 20 20 20 70 69 43 6f  ab;.        piCo
1b359 6c 4d 61 73 6b 20 3d 20 26 28 70 54 72 69 67 67  lMask = &(pTrigg
1b35a 65 72 53 74 61 63 6b 2d 3e 6e 65 77 43 6f 6c 4d  erStack->newColM
1b35b 61 73 6b 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ask);.      }els
1b35c 65 20 69 66 28 20 70 54 72 69 67 67 65 72 53 74  e if( pTriggerSt
1b35d 61 63 6b 2d 3e 6f 6c 64 49 64 78 20 21 3d 20 2d  ack->oldIdx != -
1b35e 31 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49  1 && sqlite3StrI
1b35f 43 6d 70 28 22 6f 6c 64 22 2c 20 7a 54 61 62 29  Cmp("old", zTab)
1b360 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ==0 ){.        p
1b361 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70  Expr->iTable = p
1b362 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 6f 6c  TriggerStack->ol
1b363 64 49 64 78 3b 0a 20 20 20 20 20 20 20 20 61 73  dIdx;.        as
1b364 73 65 72 74 28 20 70 54 72 69 67 67 65 72 53 74  sert( pTriggerSt
1b365 61 63 6b 2d 3e 70 54 61 62 20 29 3b 0a 20 20 20  ack->pTab );.   
1b366 20 20 20 20 20 70 54 61 62 20 3d 20 70 54 72 69       pTab = pTri
1b367 67 67 65 72 53 74 61 63 6b 2d 3e 70 54 61 62 3b  ggerStack->pTab;
1b368 0a 20 20 20 20 20 20 20 20 70 69 43 6f 6c 4d 61  .        piColMa
1b369 73 6b 20 3d 20 26 28 70 54 72 69 67 67 65 72 53  sk = &(pTriggerS
1b36a 74 61 63 6b 2d 3e 6f 6c 64 43 6f 6c 4d 61 73 6b  tack->oldColMask
1b36b 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  );.      }..    
1b36c 20 20 69 66 28 20 70 54 61 62 20 29 7b 20 0a 20    if( pTab ){ . 
1b36d 20 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 3b         int iCol;
1b36e 0a 20 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20  .        Column 
1b36f 2a 70 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43  *pCol = pTab->aC
1b370 6f 6c 3b 0a 0a 20 20 20 20 20 20 20 20 70 53 63  ol;..        pSc
1b371 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63  hema = pTab->pSc
1b372 68 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 63 6e  hema;.        cn
1b373 74 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20  tTab++;.        
1b374 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c  for(iCol=0; iCol
1b375 20 3c 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69   < pTab->nCol; i
1b376 43 6f 6c 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 20 7b  Col++, pCol++) {
1b377 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73  .          if( s
1b378 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43  qlite3StrICmp(pC
1b379 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29  ol->zName, zCol)
1b37a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
1b37b 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20     cnt++;.      
1b37c 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f        pExpr->iCo
1b37d 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3d 3d 70 54 61  lumn = iCol==pTa
1b37e 62 2d 3e 69 50 4b 65 79 20 3f 20 2d 31 20 3a 20  b->iPKey ? -1 : 
1b37f 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20  iCol;.          
1b380 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20    pExpr->pTab = 
1b381 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20  pTab;.          
1b382 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 29 7b    if( iCol>=0 ){
1b383 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74  .              t
1b384 65 73 74 63 61 73 65 28 20 69 43 6f 6c 3d 3d 33  estcase( iCol==3
1b385 31 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  1 );.           
1b386 20 20 20 74 65 73 74 63 61 73 65 28 20 69 43 6f     testcase( iCo
1b387 6c 3d 3d 33 32 20 29 3b 0a 20 20 20 20 20 20 20  l==32 );.       
1b388 20 20 20 20 20 20 20 2a 70 69 43 6f 6c 4d 61 73         *piColMas
1b389 6b 20 7c 3d 20 28 28 75 33 32 29 31 3c 3c 69 43  k |= ((u32)1<<iC
1b38a 6f 6c 29 20 7c 20 28 69 43 6f 6c 3e 3d 33 32 3f  ol) | (iCol>=32?
1b38b 30 78 66 66 66 66 66 66 66 66 3a 30 29 3b 0a 20  0xffffffff:0);. 
1b38c 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
1b38d 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
1b38e 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1b38f 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
1b390 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64    }.#endif /* !d
1b391 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
1b392 49 54 5f 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a  IT_TRIGGER) */..
1b393 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 50 65      /*.    ** Pe
1b394 72 68 61 70 73 20 74 68 65 20 6e 61 6d 65 20 69  rhaps the name i
1b395 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f  s a reference to
1b396 20 74 68 65 20 52 4f 57 49 44 0a 20 20 20 20 2a   the ROWID.    *
1b397 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30  /.    if( cnt==0
1b398 20 26 26 20 63 6e 74 54 61 62 3d 3d 31 20 26 26   && cntTab==1 &&
1b399 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28   sqlite3IsRowid(
1b39a 7a 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 63  zCol) ){.      c
1b39b 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 45  nt = 1;.      pE
1b39c 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d  xpr->iColumn = -
1b39d 31 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e  1;.      pExpr->
1b39e 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54  affinity = SQLIT
1b39f 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20  E_AFF_INTEGER;. 
1b3a0 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20     }..    /*.   
1b3a1 20 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74   ** If the input
1b3a2 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20   is of the form 
1b3a3 5a 20 28 6e 6f 74 20 59 2e 5a 20 6f 72 20 58 2e  Z (not Y.Z or X.
1b3a4 59 2e 5a 29 20 74 68 65 6e 20 74 68 65 20 6e 61  Y.Z) then the na
1b3a5 6d 65 20 5a 0a 20 20 20 20 2a 2a 20 6d 69 67 68  me Z.    ** migh
1b3a6 74 20 72 65 66 65 72 20 74 6f 20 61 6e 20 72 65  t refer to an re
1b3a7 73 75 6c 74 2d 73 65 74 20 61 6c 69 61 73 2e 20  sult-set alias. 
1b3a8 20 54 68 69 73 20 68 61 70 70 65 6e 73 2c 20 66   This happens, f
1b3a9 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e  or example, when
1b3aa 0a 20 20 20 20 2a 2a 20 77 65 20 61 72 65 20 72  .    ** we are r
1b3ab 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69  esolving names i
1b3ac 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  n the WHERE clau
1b3ad 73 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  se of the follow
1b3ae 69 6e 67 20 63 6f 6d 6d 61 6e 64 3a 0a 20 20 20  ing command:.   
1b3af 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 53   **.    **     S
1b3b0 45 4c 45 43 54 20 61 2b 62 20 41 53 20 78 20 46  ELECT a+b AS x F
1b3b1 52 4f 4d 20 74 61 62 6c 65 20 57 48 45 52 45 20  ROM table WHERE 
1b3b2 78 3c 31 30 3b 0a 20 20 20 20 2a 2a 0a 20 20 20  x<10;.    **.   
1b3b3 20 2a 2a 20 49 6e 20 63 61 73 65 73 20 6c 69 6b   ** In cases lik
1b3b4 65 20 74 68 69 73 2c 20 72 65 70 6c 61 63 65 20  e this, replace 
1b3b5 70 45 78 70 72 20 77 69 74 68 20 61 20 63 6f 70  pExpr with a cop
1b3b6 79 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73  y of the express
1b3b7 69 6f 6e 20 74 68 61 74 0a 20 20 20 20 2a 2a 20  ion that.    ** 
1b3b8 66 6f 72 6d 73 20 74 68 65 20 72 65 73 75 6c 74  forms the result
1b3b9 20 73 65 74 20 65 6e 74 72 79 20 28 22 61 2b 62   set entry ("a+b
1b3ba 22 20 69 6e 20 74 68 65 20 65 78 61 6d 70 6c 65  " in the example
1b3bb 29 20 61 6e 64 20 72 65 74 75 72 6e 20 69 6d 6d  ) and return imm
1b3bc 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a  ediately..    **
1b3bd 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 65   Note that the e
1b3be 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65  xpression in the
1b3bf 20 72 65 73 75 6c 74 20 73 65 74 20 73 68 6f 75   result set shou
1b3c0 6c 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 20  ld have already 
1b3c1 62 65 65 6e 0a 20 20 20 20 2a 2a 20 72 65 73 6f  been.    ** reso
1b3c2 6c 76 65 64 20 62 79 20 74 68 65 20 74 69 6d 65  lved by the time
1b3c3 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
1b3c4 65 20 69 73 20 72 65 73 6f 6c 76 65 64 2e 0a 20  e is resolved.. 
1b3c5 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e     */.    if( cn
1b3c6 74 3d 3d 30 20 26 26 20 28 70 45 4c 69 73 74 20  t==0 && (pEList 
1b3c7 3d 20 70 4e 43 2d 3e 70 45 4c 69 73 74 29 21 3d  = pNC->pEList)!=
1b3c8 30 20 26 26 20 7a 54 61 62 3d 3d 30 20 29 7b 0a  0 && zTab==0 ){.
1b3c9 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
1b3ca 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  <pEList->nExpr; 
1b3cb 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 68  j++){.        ch
1b3cc 61 72 20 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74  ar *zAs = pEList
1b3cd 2d 3e 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  ->a[j].zName;.  
1b3ce 20 20 20 20 20 20 69 66 28 20 7a 41 73 21 3d 30        if( zAs!=0
1b3cf 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43   && sqlite3StrIC
1b3d0 6d 70 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30  mp(zAs, zCol)==0
1b3d1 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 45 78   ){.          Ex
1b3d2 70 72 20 2a 70 4f 72 69 67 3b 0a 20 20 20 20 20  pr *pOrig;.     
1b3d3 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78       assert( pEx
1b3d4 70 72 2d 3e 70 4c 65 66 74 3d 3d 30 20 26 26 20  pr->pLeft==0 && 
1b3d5 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30  pExpr->pRight==0
1b3d6 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73   );.          as
1b3d7 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 4c 69  sert( pExpr->pLi
1b3d8 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  st==0 );.       
1b3d9 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
1b3da 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 0a  ->pSelect==0 );.
1b3db 20 20 20 20 20 20 20 20 20 20 70 4f 72 69 67 20            pOrig 
1b3dc 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70  = pEList->a[j].p
1b3dd 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20  Expr;.          
1b3de 69 66 28 20 21 70 4e 43 2d 3e 61 6c 6c 6f 77 41  if( !pNC->allowA
1b3df 67 67 20 26 26 20 45 78 70 72 48 61 73 50 72 6f  gg && ExprHasPro
1b3e0 70 65 72 74 79 28 70 4f 72 69 67 2c 20 45 50 5f  perty(pOrig, EP_
1b3e1 41 67 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20  Agg) ){.        
1b3e2 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1b3e3 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69 73  Msg(pParse, "mis
1b3e4 75 73 65 20 6f 66 20 61 6c 69 61 73 65 64 20 61  use of aliased a
1b3e5 67 67 72 65 67 61 74 65 20 25 73 22 2c 20 7a 41  ggregate %s", zA
1b3e6 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  s);.            
1b3e7 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1b3e8 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 20 20 20  , zCol);.       
1b3e9 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20       return 2;. 
1b3ea 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1b3eb 20 20 20 20 20 72 65 73 6f 6c 76 65 41 6c 69 61       resolveAlia
1b3ec 73 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74  s(pParse, pEList
1b3ed 2c 20 6a 2c 20 70 45 78 70 72 2c 20 22 22 29 3b  , j, pExpr, "");
1b3ee 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 20 3d  .          cnt =
1b3ef 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4d   1;.          pM
1b3f0 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 20 20  atch = 0;.      
1b3f1 20 20 20 20 61 73 73 65 72 74 28 20 7a 54 61 62      assert( zTab
1b3f2 3d 3d 30 20 26 26 20 7a 44 62 3d 3d 30 20 29 3b  ==0 && zDb==0 );
1b3f3 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
1b3f4 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 5f 32  lookupname_end_2
1b3f5 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1b3f6 20 20 7d 20 0a 20 20 20 20 7d 0a 0a 20 20 20 20    } .    }..    
1b3f7 2f 2a 20 41 64 76 61 6e 63 65 20 74 6f 20 74 68  /* Advance to th
1b3f8 65 20 6e 65 78 74 20 6e 61 6d 65 20 63 6f 6e 74  e next name cont
1b3f9 65 78 74 2e 20 20 54 68 65 20 6c 6f 6f 70 20 77  ext.  The loop w
1b3fa 69 6c 6c 20 65 78 69 74 20 77 68 65 6e 20 65 69  ill exit when ei
1b3fb 74 68 65 72 0a 20 20 20 20 2a 2a 20 77 65 20 68  ther.    ** we h
1b3fc 61 76 65 20 61 20 6d 61 74 63 68 20 28 63 6e 74  ave a match (cnt
1b3fd 3e 30 29 20 6f 72 20 77 68 65 6e 20 77 65 20 72  >0) or when we r
1b3fe 75 6e 20 6f 75 74 20 6f 66 20 6e 61 6d 65 20 63  un out of name c
1b3ff 6f 6e 74 65 78 74 73 2e 0a 20 20 20 20 2a 2f 0a  ontexts..    */.
1b400 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 29      if( cnt==0 )
1b401 7b 0a 20 20 20 20 20 20 70 4e 43 20 3d 20 70 4e  {.      pNC = pN
1b402 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a  C->pNext;.    }.
1b403 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 49    }..  /*.  ** I
1b404 66 20 58 20 61 6e 64 20 59 20 61 72 65 20 4e 55  f X and Y are NU
1b405 4c 4c 20 28 69 6e 20 6f 74 68 65 72 20 77 6f 72  LL (in other wor
1b406 64 73 20 69 66 20 6f 6e 6c 79 20 74 68 65 20 63  ds if only the c
1b407 6f 6c 75 6d 6e 20 6e 61 6d 65 20 5a 20 69 73 0a  olumn name Z is.
1b408 20 20 2a 2a 20 73 75 70 70 6c 69 65 64 29 20 61    ** supplied) a
1b409 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  nd the value of 
1b40a 5a 20 69 73 20 65 6e 63 6c 6f 73 65 64 20 69 6e  Z is enclosed in
1b40b 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 73 2c 20   double-quotes, 
1b40c 74 68 65 6e 0a 20 20 2a 2a 20 5a 20 69 73 20 61  then.  ** Z is a
1b40d 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 20   string literal 
1b40e 69 66 20 69 74 20 64 6f 65 73 6e 27 74 20 6d 61  if it doesn't ma
1b40f 74 63 68 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 6e  tch any column n
1b410 61 6d 65 73 2e 20 20 49 6e 20 74 68 61 74 0a 20  ames.  In that. 
1b411 20 2a 2a 20 63 61 73 65 2c 20 77 65 20 6e 65 65   ** case, we nee
1b412 64 20 74 6f 20 72 65 74 75 72 6e 20 72 69 67 68  d to return righ
1b413 74 20 61 77 61 79 20 61 6e 64 20 6e 6f 74 20 6d  t away and not m
1b414 61 6b 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20  ake any changes 
1b415 74 6f 0a 20 20 2a 2a 20 70 45 78 70 72 2e 0a 20  to.  ** pExpr.. 
1b416 20 2a 2a 0a 20 20 2a 2a 20 42 65 63 61 75 73 65   **.  ** Because
1b417 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 20 77 61   no reference wa
1b418 73 20 6d 61 64 65 20 74 6f 20 6f 75 74 65 72 20  s made to outer 
1b419 63 6f 6e 74 65 78 74 73 2c 20 74 68 65 20 70 4e  contexts, the pN
1b41a 43 2d 3e 6e 52 65 66 0a 20 20 2a 2a 20 66 69 65  C->nRef.  ** fie
1b41b 6c 64 73 20 61 72 65 20 6e 6f 74 20 63 68 61 6e  lds are not chan
1b41c 67 65 64 20 69 6e 20 61 6e 79 20 63 6f 6e 74 65  ged in any conte
1b41d 78 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63  xt..  */.  if( c
1b41e 6e 74 3d 3d 30 20 26 26 20 7a 54 61 62 3d 3d 30  nt==0 && zTab==0
1b41f 20 26 26 20 70 43 6f 6c 75 6d 6e 54 6f 6b 65 6e   && pColumnToken
1b420 2d 3e 7a 5b 30 5d 3d 3d 27 22 27 20 29 7b 0a 20  ->z[0]=='"' ){. 
1b421 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1b422 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20  (db, zCol);.    
1b423 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 53  pExpr->op = TK_S
1b424 54 52 49 4e 47 3b 0a 20 20 20 20 70 45 78 70 72  TRING;.    pExpr
1b425 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20  ->pTab = 0;.    
1b426 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20  return 0;.  }.. 
1b427 20 2f 2a 0a 20 20 2a 2a 20 63 6e 74 3d 3d 30 20   /*.  ** cnt==0 
1b428 6d 65 61 6e 73 20 74 68 65 72 65 20 77 61 73 20  means there was 
1b429 6e 6f 74 20 6d 61 74 63 68 2e 20 20 63 6e 74 3e  not match.  cnt>
1b42a 31 20 6d 65 61 6e 73 20 74 68 65 72 65 20 77 65  1 means there we
1b42b 72 65 20 74 77 6f 20 6f 72 0a 20 20 2a 2a 20 6d  re two or.  ** m
1b42c 6f 72 65 20 6d 61 74 63 68 65 73 2e 20 20 45 69  ore matches.  Ei
1b42d 74 68 65 72 20 77 61 79 2c 20 77 65 20 68 61 76  ther way, we hav
1b42e 65 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f  e an error..  */
1b42f 0a 20 20 69 66 28 20 63 6e 74 21 3d 31 20 29 7b  .  if( cnt!=1 ){
1b430 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
1b431 2a 7a 45 72 72 3b 0a 20 20 20 20 7a 45 72 72 20  *zErr;.    zErr 
1b432 3d 20 63 6e 74 3d 3d 30 20 3f 20 22 6e 6f 20 73  = cnt==0 ? "no s
1b433 75 63 68 20 63 6f 6c 75 6d 6e 22 20 3a 20 22 61  uch column" : "a
1b434 6d 62 69 67 75 6f 75 73 20 63 6f 6c 75 6d 6e 20  mbiguous column 
1b435 6e 61 6d 65 22 3b 0a 20 20 20 20 69 66 28 20 7a  name";.    if( z
1b436 44 62 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  Db ){.      sqli
1b437 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1b438 73 65 2c 20 22 25 73 3a 20 25 73 2e 25 73 2e 25  se, "%s: %s.%s.%
1b439 73 22 2c 20 7a 45 72 72 2c 20 7a 44 62 2c 20 7a  s", zErr, zDb, z
1b43a 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20  Tab, zCol);.    
1b43b 7d 65 6c 73 65 20 69 66 28 20 7a 54 61 62 20 29  }else if( zTab )
1b43c 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
1b43d 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1b43e 22 25 73 3a 20 25 73 2e 25 73 22 2c 20 7a 45 72  "%s: %s.%s", zEr
1b43f 72 2c 20 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a  r, zTab, zCol);.
1b440 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1b441 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1b442 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73  (pParse, "%s: %s
1b443 22 2c 20 7a 45 72 72 2c 20 7a 43 6f 6c 29 3b 0a  ", zErr, zCol);.
1b444 20 20 20 20 7d 0a 20 20 20 20 70 54 6f 70 4e 43      }.    pTopNC
1b445 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a 0a 20  ->nErr++;.  }.. 
1b446 20 2f 2a 20 49 66 20 61 20 63 6f 6c 75 6d 6e 20   /* If a column 
1b447 66 72 6f 6d 20 61 20 74 61 62 6c 65 20 69 6e 20  from a table in 
1b448 70 53 72 63 4c 69 73 74 20 69 73 20 72 65 66 65  pSrcList is refe
1b449 72 65 6e 63 65 64 2c 20 74 68 65 6e 20 72 65 63  renced, then rec
1b44a 6f 72 64 0a 20 20 2a 2a 20 74 68 69 73 20 66 61  ord.  ** this fa
1b44b 63 74 20 69 6e 20 74 68 65 20 70 53 72 63 4c 69  ct in the pSrcLi
1b44c 73 74 2e 61 5b 5d 2e 63 6f 6c 55 73 65 64 20 62  st.a[].colUsed b
1b44d 69 74 6d 61 73 6b 2e 20 20 43 6f 6c 75 6d 6e 20  itmask.  Column 
1b44e 30 20 63 61 75 73 65 73 0a 20 20 2a 2a 20 62 69  0 causes.  ** bi
1b44f 74 20 30 20 74 6f 20 62 65 20 73 65 74 2e 20 20  t 0 to be set.  
1b450 43 6f 6c 75 6d 6e 20 31 20 73 65 74 73 20 62 69  Column 1 sets bi
1b451 74 20 31 2e 20 20 41 6e 64 20 73 6f 20 66 6f 72  t 1.  And so for
1b452 74 68 2e 20 20 49 66 20 74 68 65 0a 20 20 2a 2a  th.  If the.  **
1b453 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 69   column number i
1b454 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74  s greater than t
1b455 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74  he number of bit
1b456 73 20 69 6e 20 74 68 65 20 62 69 74 6d 61 73 6b  s in the bitmask
1b457 0a 20 20 2a 2a 20 74 68 65 6e 20 73 65 74 20 74  .  ** then set t
1b458 68 65 20 68 69 67 68 2d 6f 72 64 65 72 20 62 69  he high-order bi
1b459 74 20 6f 66 20 74 68 65 20 62 69 74 6d 61 73 6b  t of the bitmask
1b45a 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 45 78  ..  */.  if( pEx
1b45b 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20 26  pr->iColumn>=0 &
1b45c 26 20 70 4d 61 74 63 68 21 3d 30 20 29 7b 0a 20  & pMatch!=0 ){. 
1b45d 20 20 20 69 6e 74 20 6e 20 3d 20 70 45 78 70 72     int n = pExpr
1b45e 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 74  ->iColumn;.    t
1b45f 65 73 74 63 61 73 65 28 20 6e 3d 3d 73 69 7a 65  estcase( n==size
1b460 6f 66 28 42 69 74 6d 61 73 6b 29 2a 38 2d 31 20  of(Bitmask)*8-1 
1b461 29 3b 0a 20 20 20 20 69 66 28 20 6e 3e 3d 73 69  );.    if( n>=si
1b462 7a 65 6f 66 28 42 69 74 6d 61 73 6b 29 2a 38 20  zeof(Bitmask)*8 
1b463 29 7b 0a 20 20 20 20 20 20 6e 20 3d 20 73 69 7a  ){.      n = siz
1b464 65 6f 66 28 42 69 74 6d 61 73 6b 29 2a 38 2d 31  eof(Bitmask)*8-1
1b465 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
1b466 72 74 28 20 70 4d 61 74 63 68 2d 3e 69 43 75 72  rt( pMatch->iCur
1b467 73 6f 72 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62  sor==pExpr->iTab
1b468 6c 65 20 29 3b 0a 20 20 20 20 70 4d 61 74 63 68  le );.    pMatch
1b469 2d 3e 63 6f 6c 55 73 65 64 20 7c 3d 20 28 28 42  ->colUsed |= ((B
1b46a 69 74 6d 61 73 6b 29 31 29 3c 3c 6e 3b 0a 20 20  itmask)1)<<n;.  
1b46b 7d 0a 0a 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e  }..lookupname_en
1b46c 64 3a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 75 70  d:.  /* Clean up
1b46d 20 61 6e 64 20 72 65 74 75 72 6e 0a 20 20 2a 2f   and return.  */
1b46e 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
1b46f 28 64 62 2c 20 7a 44 62 29 3b 0a 20 20 73 71 6c  (db, zDb);.  sql
1b470 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
1b471 54 61 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  Tab);.  sqlite3E
1b472 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45  xprDelete(db, pE
1b473 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 70  xpr->pLeft);.  p
1b474 45 78 70 72 2d 3e 70 4c 65 66 74 20 3d 20 30 3b  Expr->pLeft = 0;
1b475 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
1b476 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72 2d 3e  lete(db, pExpr->
1b477 70 52 69 67 68 74 29 3b 0a 20 20 70 45 78 70 72  pRight);.  pExpr
1b478 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20  ->pRight = 0;.  
1b479 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43  pExpr->op = TK_C
1b47a 4f 4c 55 4d 4e 3b 0a 6c 6f 6f 6b 75 70 6e 61 6d  OLUMN;.lookupnam
1b47b 65 5f 65 6e 64 5f 32 3a 0a 20 20 73 71 6c 69 74  e_end_2:.  sqlit
1b47c 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f  e3DbFree(db, zCo
1b47d 6c 29 3b 0a 20 20 69 66 28 20 63 6e 74 3d 3d 31  l);.  if( cnt==1
1b47e 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1b47f 70 4e 43 21 3d 30 20 29 3b 0a 20 20 20 20 73 71  pNC!=0 );.    sq
1b480 6c 69 74 65 33 41 75 74 68 52 65 61 64 28 70 50  lite3AuthRead(pP
1b481 61 72 73 65 2c 20 70 45 78 70 72 2c 20 70 53 63  arse, pExpr, pSc
1b482 68 65 6d 61 2c 20 70 4e 43 2d 3e 70 53 72 63 4c  hema, pNC->pSrcL
1b483 69 73 74 29 3b 0a 20 20 20 20 2f 2a 20 49 6e 63  ist);.    /* Inc
1b484 72 65 6d 65 6e 74 20 74 68 65 20 6e 52 65 66 20  rement the nRef 
1b485 76 61 6c 75 65 20 6f 6e 20 61 6c 6c 20 6e 61 6d  value on all nam
1b486 65 20 63 6f 6e 74 65 78 74 73 20 66 72 6f 6d 20  e contexts from 
1b487 54 6f 70 4e 43 20 75 70 20 74 6f 0a 20 20 20 20  TopNC up to.    
1b488 2a 2a 20 74 68 65 20 70 6f 69 6e 74 20 77 68 65  ** the point whe
1b489 72 65 20 74 68 65 20 6e 61 6d 65 20 6d 61 74 63  re the name matc
1b48a 68 65 64 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28  hed. */.    for(
1b48b 3b 3b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  ;;){.      asser
1b48c 74 28 20 70 54 6f 70 4e 43 21 3d 30 20 29 3b 0a  t( pTopNC!=0 );.
1b48d 20 20 20 20 20 20 70 54 6f 70 4e 43 2d 3e 6e 52        pTopNC->nR
1b48e 65 66 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20  ef++;.      if( 
1b48f 70 54 6f 70 4e 43 3d 3d 70 4e 43 20 29 20 62 72  pTopNC==pNC ) br
1b490 65 61 6b 3b 0a 20 20 20 20 20 20 70 54 6f 70 4e  eak;.      pTopN
1b491 43 20 3d 20 70 54 6f 70 4e 43 2d 3e 70 4e 65 78  C = pTopNC->pNex
1b492 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  t;.    }.    ret
1b493 75 72 6e 20 30 3b 0a 20 20 7d 20 65 6c 73 65 20  urn 0;.  } else 
1b494 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  {.    return 1;.
1b495 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69    }.}../*.** Thi
1b496 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
1b497 6c 62 61 63 6b 20 66 6f 72 20 73 71 6c 69 74 65  lback for sqlite
1b498 33 57 61 6c 6b 45 78 70 72 28 29 2e 0a 2a 2a 0a  3WalkExpr()..**.
1b499 2a 2a 20 52 65 73 6f 6c 76 65 20 73 79 6d 62 6f  ** Resolve symbo
1b49a 6c 69 63 20 6e 61 6d 65 73 20 69 6e 74 6f 20 54  lic names into T
1b49b 4b 5f 43 4f 4c 55 4d 4e 20 6f 70 65 72 61 74 6f  K_COLUMN operato
1b49c 72 73 20 66 6f 72 20 74 68 65 20 63 75 72 72 65  rs for the curre
1b49d 6e 74 0a 2a 2a 20 6e 6f 64 65 20 69 6e 20 74 68  nt.** node in th
1b49e 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  e expression tre
1b49f 65 2e 20 20 52 65 74 75 72 6e 20 30 20 74 6f 20  e.  Return 0 to 
1b4a0 63 6f 6e 74 69 6e 75 65 20 74 68 65 20 73 65 61  continue the sea
1b4a1 72 63 68 20 64 6f 77 6e 0a 2a 2a 20 74 68 65 20  rch down.** the 
1b4a2 74 72 65 65 20 6f 72 20 32 20 74 6f 20 61 62 6f  tree or 2 to abo
1b4a3 72 74 20 74 68 65 20 74 72 65 65 20 77 61 6c 6b  rt the tree walk
1b4a4 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
1b4a5 74 69 6e 65 20 61 6c 73 6f 20 64 6f 65 73 20 65  tine also does e
1b4a6 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 61 6e  rror checking an
1b4a7 64 20 6e 61 6d 65 20 72 65 73 6f 6c 75 74 69 6f  d name resolutio
1b4a8 6e 20 66 6f 72 0a 2a 2a 20 66 75 6e 63 74 69 6f  n for.** functio
1b4a9 6e 20 6e 61 6d 65 73 2e 20 20 54 68 65 20 6f 70  n names.  The op
1b4aa 65 72 61 74 6f 72 20 66 6f 72 20 61 67 67 72 65  erator for aggre
1b4ab 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 69  gate functions i
1b4ac 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 74 6f 20  s changed.** to 
1b4ad 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 2e  TK_AGG_FUNCTION.
1b4ae 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
1b4af 65 73 6f 6c 76 65 45 78 70 72 53 74 65 70 28 57  esolveExprStep(W
1b4b0 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20  alker *pWalker, 
1b4b1 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20  Expr *pExpr){.  
1b4b2 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43  NameContext *pNC
1b4b3 3b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  ;.  Parse *pPars
1b4b4 65 3b 0a 0a 20 20 70 4e 43 20 3d 20 70 57 61 6c  e;..  pNC = pWal
1b4b5 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20 61 73  ker->u.pNC;.  as
1b4b6 73 65 72 74 28 20 70 4e 43 21 3d 30 20 29 3b 0a  sert( pNC!=0 );.
1b4b7 20 20 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e    pParse = pNC->
1b4b8 70 50 61 72 73 65 3b 0a 20 20 61 73 73 65 72 74  pParse;.  assert
1b4b9 28 20 70 50 61 72 73 65 3d 3d 70 57 61 6c 6b 65  ( pParse==pWalke
1b4ba 72 2d 3e 70 50 61 72 73 65 20 29 3b 0a 0a 20 20  r->pParse );..  
1b4bb 69 66 28 20 45 78 70 72 48 61 73 41 6e 79 50 72  if( ExprHasAnyPr
1b4bc 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
1b4bd 5f 52 65 73 6f 6c 76 65 64 29 20 29 20 72 65 74  _Resolved) ) ret
1b4be 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20  urn WRC_Prune;. 
1b4bf 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79   ExprSetProperty
1b4c0 28 70 45 78 70 72 2c 20 45 50 5f 52 65 73 6f 6c  (pExpr, EP_Resol
1b4c1 76 65 64 29 3b 0a 23 69 66 6e 64 65 66 20 4e 44  ved);.#ifndef ND
1b4c2 45 42 55 47 0a 20 20 69 66 28 20 70 4e 43 2d 3e  EBUG.  if( pNC->
1b4c3 70 53 72 63 4c 69 73 74 20 26 26 20 70 4e 43 2d  pSrcList && pNC-
1b4c4 3e 70 53 72 63 4c 69 73 74 2d 3e 6e 41 6c 6c 6f  >pSrcList->nAllo
1b4c5 63 3e 30 20 29 7b 0a 20 20 20 20 53 72 63 4c 69  c>0 ){.    SrcLi
1b4c6 73 74 20 2a 70 53 72 63 4c 69 73 74 20 3d 20 70  st *pSrcList = p
1b4c7 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 20 20  NC->pSrcList;.  
1b4c8 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72    int i;.    for
1b4c9 28 69 3d 30 3b 20 69 3c 70 4e 43 2d 3e 70 53 72  (i=0; i<pNC->pSr
1b4ca 63 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  cList->nSrc; i++
1b4cb 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
1b4cc 20 70 53 72 63 4c 69 73 74 2d 3e 61 5b 69 5d 2e   pSrcList->a[i].
1b4cd 69 43 75 72 73 6f 72 3e 3d 30 20 26 26 20 70 53  iCursor>=0 && pS
1b4ce 72 63 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75  rcList->a[i].iCu
1b4cf 72 73 6f 72 3c 70 50 61 72 73 65 2d 3e 6e 54 61  rsor<pParse->nTa
1b4d0 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  b);.    }.  }.#e
1b4d1 6e 64 69 66 0a 20 20 73 77 69 74 63 68 28 20 70  ndif.  switch( p
1b4d2 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 0a 23 69 66  Expr->op ){..#if
1b4d3 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
1b4d4 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45  ENABLE_UPDATE_DE
1b4d5 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 21  LETE_LIMIT) && !
1b4d6 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
1b4d7 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a 20 20  MIT_SUBQUERY).  
1b4d8 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69 61 6c    /* The special
1b4d9 20 6f 70 65 72 61 74 6f 72 20 54 4b 5f 52 4f 57   operator TK_ROW
1b4da 20 6d 65 61 6e 73 20 75 73 65 20 74 68 65 20 72   means use the r
1b4db 6f 77 69 64 20 66 6f 72 20 74 68 65 20 66 69 72  owid for the fir
1b4dc 73 74 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e  st.    ** column
1b4dd 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
1b4de 75 73 65 2e 20 20 54 68 69 73 20 69 73 20 75 73  use.  This is us
1b4df 65 64 20 62 79 20 74 68 65 20 4c 49 4d 49 54 20  ed by the LIMIT 
1b4e0 61 6e 64 20 4f 52 44 45 52 20 42 59 0a 20 20 20  and ORDER BY.   
1b4e1 20 2a 2a 20 63 6c 61 75 73 65 20 70 72 6f 63 65   ** clause proce
1b4e2 73 73 69 6e 67 20 6f 6e 20 55 50 44 41 54 45 20  ssing on UPDATE 
1b4e3 61 6e 64 20 44 45 4c 45 54 45 20 73 74 61 74 65  and DELETE state
1b4e4 6d 65 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  ments..    */.  
1b4e5 20 20 63 61 73 65 20 54 4b 5f 52 4f 57 3a 20 7b    case TK_ROW: {
1b4e6 0a 20 20 20 20 20 20 53 72 63 4c 69 73 74 20 2a  .      SrcList *
1b4e7 70 53 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e  pSrcList = pNC->
1b4e8 70 53 72 63 4c 69 73 74 3b 0a 20 20 20 20 20 20  pSrcList;.      
1b4e9 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
1b4ea 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20  tem *pItem;.    
1b4eb 20 20 61 73 73 65 72 74 28 20 70 53 72 63 4c 69    assert( pSrcLi
1b4ec 73 74 20 26 26 20 70 53 72 63 4c 69 73 74 2d 3e  st && pSrcList->
1b4ed 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 20 20 20  nSrc==1 );.     
1b4ee 20 70 49 74 65 6d 20 3d 20 70 53 72 63 4c 69 73   pItem = pSrcLis
1b4ef 74 2d 3e 61 3b 20 0a 20 20 20 20 20 20 70 45 78  t->a; .      pEx
1b4f0 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4c 55  pr->op = TK_COLU
1b4f1 4d 4e 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d  MN;.      pExpr-
1b4f2 3e 70 54 61 62 20 3d 20 70 49 74 65 6d 2d 3e 70  >pTab = pItem->p
1b4f3 54 61 62 3b 0a 20 20 20 20 20 20 70 45 78 70 72  Tab;.      pExpr
1b4f4 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49 74 65 6d  ->iTable = pItem
1b4f5 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 20  ->iCursor;.     
1b4f6 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20   pExpr->iColumn 
1b4f7 3d 20 2d 31 3b 0a 20 20 20 20 20 20 70 45 78 70  = -1;.      pExp
1b4f8 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51  r->affinity = SQ
1b4f9 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52  LITE_AFF_INTEGER
1b4fa 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1b4fb 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 64     }.#endif /* d
1b4fc 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
1b4fd 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45  ABLE_UPDATE_DELE
1b4fe 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 64 65  TE_LIMIT) && !de
1b4ff 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
1b500 54 5f 53 55 42 51 55 45 52 59 29 20 2a 2f 0a 0a  T_SUBQUERY) */..
1b501 20 20 20 20 2f 2a 20 41 20 6c 6f 6e 65 20 69 64      /* A lone id
1b502 65 6e 74 69 66 69 65 72 20 69 73 20 74 68 65 20  entifier is the 
1b503 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e  name of a column
1b504 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  ..    */.    cas
1b505 65 20 54 4b 5f 49 44 3a 20 7b 0a 20 20 20 20 20  e TK_ID: {.     
1b506 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 61 72   lookupName(pPar
1b507 73 65 2c 20 30 2c 20 30 2c 20 26 70 45 78 70 72  se, 0, 0, &pExpr
1b508 2d 3e 74 6f 6b 65 6e 2c 20 70 4e 43 2c 20 70 45  ->token, pNC, pE
1b509 78 70 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75  xpr);.      retu
1b50a 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20  rn WRC_Prune;.  
1b50b 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 41 20    }.  .    /* A 
1b50c 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64 20 63  table name and c
1b50d 6f 6c 75 6d 6e 20 6e 61 6d 65 3a 20 20 20 20 20  olumn name:     
1b50e 49 44 2e 49 44 0a 20 20 20 20 2a 2a 20 4f 72 20  ID.ID.    ** Or 
1b50f 61 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c  a database, tabl
1b510 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 3a 20 20 49  e and column:  I
1b511 44 2e 49 44 2e 49 44 0a 20 20 20 20 2a 2f 0a 20  D.ID.ID.    */. 
1b512 20 20 20 63 61 73 65 20 54 4b 5f 44 4f 54 3a 20     case TK_DOT: 
1b513 7b 0a 20 20 20 20 20 20 54 6f 6b 65 6e 20 2a 70  {.      Token *p
1b514 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 54 6f  Column;.      To
1b515 6b 65 6e 20 2a 70 54 61 62 6c 65 3b 0a 20 20 20  ken *pTable;.   
1b516 20 20 20 54 6f 6b 65 6e 20 2a 70 44 62 3b 0a 20     Token *pDb;. 
1b517 20 20 20 20 20 45 78 70 72 20 2a 70 52 69 67 68       Expr *pRigh
1b518 74 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 69 66 28  t;..      /* if(
1b519 20 70 53 72 63 4c 69 73 74 3d 3d 30 20 29 20 62   pSrcList==0 ) b
1b51a 72 65 61 6b 3b 20 2a 2f 0a 20 20 20 20 20 20 70  reak; */.      p
1b51b 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 70  Right = pExpr->p
1b51c 52 69 67 68 74 3b 0a 20 20 20 20 20 20 69 66 28  Right;.      if(
1b51d 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f   pRight->op==TK_
1b51e 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44  ID ){.        pD
1b51f 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70  b = 0;.        p
1b520 54 61 62 6c 65 20 3d 20 26 70 45 78 70 72 2d 3e  Table = &pExpr->
1b521 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 20  pLeft->token;.  
1b522 20 20 20 20 20 20 70 43 6f 6c 75 6d 6e 20 3d 20        pColumn = 
1b523 26 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 3b 0a  &pRight->token;.
1b524 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1b525 20 20 20 20 20 61 73 73 65 72 74 28 20 70 52 69       assert( pRi
1b526 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20  ght->op==TK_DOT 
1b527 29 3b 0a 20 20 20 20 20 20 20 20 70 44 62 20 3d  );.        pDb =
1b528 20 26 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e   &pExpr->pLeft->
1b529 74 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 70  token;.        p
1b52a 54 61 62 6c 65 20 3d 20 26 70 52 69 67 68 74 2d  Table = &pRight-
1b52b 3e 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 3b 0a 20  >pLeft->token;. 
1b52c 20 20 20 20 20 20 20 70 43 6f 6c 75 6d 6e 20 3d         pColumn =
1b52d 20 26 70 52 69 67 68 74 2d 3e 70 52 69 67 68 74   &pRight->pRight
1b52e 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 7d  ->token;.      }
1b52f 0a 20 20 20 20 20 20 6c 6f 6f 6b 75 70 4e 61 6d  .      lookupNam
1b530 65 28 70 50 61 72 73 65 2c 20 70 44 62 2c 20 70  e(pParse, pDb, p
1b531 54 61 62 6c 65 2c 20 70 43 6f 6c 75 6d 6e 2c 20  Table, pColumn, 
1b532 70 4e 43 2c 20 70 45 78 70 72 29 3b 0a 20 20 20  pNC, pExpr);.   
1b533 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72     return WRC_Pr
1b534 75 6e 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  une;.    }..    
1b535 2f 2a 20 52 65 73 6f 6c 76 65 20 66 75 6e 63 74  /* Resolve funct
1b536 69 6f 6e 20 6e 61 6d 65 73 0a 20 20 20 20 2a 2f  ion names.    */
1b537 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e  .    case TK_CON
1b538 53 54 5f 46 55 4e 43 3a 0a 20 20 20 20 63 61 73  ST_FUNC:.    cas
1b539 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b  e TK_FUNCTION: {
1b53a 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20  .      ExprList 
1b53b 2a 70 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e  *pList = pExpr->
1b53c 70 4c 69 73 74 3b 20 20 20 20 2f 2a 20 54 68 65  pList;    /* The
1b53d 20 61 72 67 75 6d 65 6e 74 20 6c 69 73 74 20 2a   argument list *
1b53e 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20  /.      int n = 
1b53f 70 4c 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e  pList ? pList->n
1b540 45 78 70 72 20 3a 20 30 3b 20 20 2f 2a 20 4e 75  Expr : 0;  /* Nu
1b541 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
1b542 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e  s */.      int n
1b543 6f 5f 73 75 63 68 5f 66 75 6e 63 20 3d 20 30 3b  o_such_func = 0;
1b544 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
1b545 66 20 6e 6f 20 73 75 63 68 20 66 75 6e 63 74 69  f no such functi
1b546 6f 6e 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 20  on exists */.   
1b547 20 20 20 69 6e 74 20 77 72 6f 6e 67 5f 6e 75 6d     int wrong_num
1b548 5f 61 72 67 73 20 3d 20 30 3b 20 20 20 20 20 2f  _args = 0;     /
1b549 2a 20 54 72 75 65 20 69 66 20 77 72 6f 6e 67 20  * True if wrong 
1b54a 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  number of argume
1b54b 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  nts */.      int
1b54c 20 69 73 5f 61 67 67 20 3d 20 30 3b 20 20 20 20   is_agg = 0;    
1b54d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
1b54e 20 69 66 20 69 73 20 61 6e 20 61 67 67 72 65 67   if is an aggreg
1b54f 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  ate function */.
1b550 20 20 20 20 20 20 69 6e 74 20 61 75 74 68 3b 20        int auth; 
1b551 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b552 20 20 2f 2a 20 41 75 74 68 6f 72 69 7a 61 74 69    /* Authorizati
1b553 6f 6e 20 74 6f 20 75 73 65 20 74 68 65 20 66 75  on to use the fu
1b554 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  nction */.      
1b555 69 6e 74 20 6e 49 64 3b 20 20 20 20 20 20 20 20  int nId;        
1b556 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1b557 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
1b558 65 72 73 20 69 6e 20 66 75 6e 63 74 69 6f 6e 20  ers in function 
1b559 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 63 6f  name */.      co
1b55a 6e 73 74 20 63 68 61 72 20 2a 7a 49 64 3b 20 20  nst char *zId;  
1b55b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1b55c 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 2e 20   function name. 
1b55d 2a 2f 0a 20 20 20 20 20 20 46 75 6e 63 44 65 66  */.      FuncDef
1b55e 20 2a 70 44 65 66 3b 20 20 20 20 20 20 20 20 20   *pDef;         
1b55f 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74       /* Informat
1b560 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66 75  ion about the fu
1b561 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  nction */.      
1b562 69 6e 74 20 65 6e 63 20 3d 20 45 4e 43 28 70 50  int enc = ENC(pP
1b563 61 72 73 65 2d 3e 64 62 29 3b 20 20 2f 2a 20 54  arse->db);  /* T
1b564 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 63 6f  he database enco
1b565 64 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 20 20 7a  ding */..      z
1b566 49 64 20 3d 20 28 63 68 61 72 2a 29 70 45 78 70  Id = (char*)pExp
1b567 72 2d 3e 74 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 20  r->token.z;.    
1b568 20 20 6e 49 64 20 3d 20 70 45 78 70 72 2d 3e 74    nId = pExpr->t
1b569 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 20 20 70 44  oken.n;.      pD
1b56a 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ef = sqlite3Find
1b56b 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2d  Function(pParse-
1b56c 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 6e  >db, zId, nId, n
1b56d 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20  , enc, 0);.     
1b56e 20 69 66 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a   if( pDef==0 ){.
1b56f 20 20 20 20 20 20 20 20 70 44 65 66 20 3d 20 73          pDef = s
1b570 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69  qlite3FindFuncti
1b571 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a  on(pParse->db, z
1b572 49 64 2c 20 6e 49 64 2c 20 2d 31 2c 20 65 6e 63  Id, nId, -1, enc
1b573 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66  , 0);.        if
1b574 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a 20 20 20  ( pDef==0 ){.   
1b575 20 20 20 20 20 20 20 6e 6f 5f 73 75 63 68 5f 66         no_such_f
1b576 75 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  unc = 1;.       
1b577 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1b578 20 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73    wrong_num_args
1b579 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 1;.        }.
1b57a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1b57b 20 20 20 20 20 69 73 5f 61 67 67 20 3d 20 70 44       is_agg = pD
1b57c 65 66 2d 3e 78 46 75 6e 63 3d 3d 30 3b 0a 20 20  ef->xFunc==0;.  
1b57d 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
1b57e 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
1b57f 49 5a 41 54 49 4f 4e 0a 20 20 20 20 20 20 69 66  IZATION.      if
1b580 28 20 70 44 65 66 20 29 7b 0a 20 20 20 20 20 20  ( pDef ){.      
1b581 20 20 61 75 74 68 20 3d 20 73 71 6c 69 74 65 33    auth = sqlite3
1b582 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
1b583 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f  , SQLITE_FUNCTIO
1b584 4e 2c 20 30 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d  N, 0, pDef->zNam
1b585 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69  e, 0);.        i
1b586 66 28 20 61 75 74 68 21 3d 53 51 4c 49 54 45 5f  f( auth!=SQLITE_
1b587 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
1b588 69 66 28 20 61 75 74 68 3d 3d 53 51 4c 49 54 45  if( auth==SQLITE
1b589 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 20 20 20  _DENY ){.       
1b58a 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1b58b 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f  rMsg(pParse, "no
1b58c 74 20 61 75 74 68 6f 72 69 7a 65 64 20 74 6f 20  t authorized to 
1b58d 75 73 65 20 66 75 6e 63 74 69 6f 6e 3a 20 25 73  use function: %s
1b58e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
1b58f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b590 20 20 20 20 20 20 20 70 44 65 66 2d 3e 7a 4e 61         pDef->zNa
1b591 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  me);.           
1b592 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20   pNC->nErr++;.  
1b593 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1b594 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20      pExpr->op = 
1b595 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20  TK_NULL;.       
1b596 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72     return WRC_Pr
1b597 75 6e 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  une;.        }. 
1b598 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
1b599 20 20 20 20 69 66 28 20 69 73 5f 61 67 67 20 26      if( is_agg &
1b59a 26 20 21 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67  & !pNC->allowAgg
1b59b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1b59c 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1b59d 73 65 2c 20 22 6d 69 73 75 73 65 20 6f 66 20 61  se, "misuse of a
1b59e 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
1b59f 6e 20 25 2e 2a 73 28 29 22 2c 20 6e 49 64 2c 7a  n %.*s()", nId,z
1b5a0 49 64 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 43  Id);.        pNC
1b5a1 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20  ->nErr++;.      
1b5a2 20 20 69 73 5f 61 67 67 20 3d 20 30 3b 0a 20 20    is_agg = 0;.  
1b5a3 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 6f      }else if( no
1b5a4 5f 73 75 63 68 5f 66 75 6e 63 20 29 7b 0a 20 20  _such_func ){.  
1b5a5 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1b5a6 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
1b5a7 6f 20 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 3a  o such function:
1b5a8 20 25 2e 2a 73 22 2c 20 6e 49 64 2c 20 7a 49 64   %.*s", nId, zId
1b5a9 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e  );.        pNC->
1b5aa 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 7d 65  nErr++;.      }e
1b5ab 6c 73 65 20 69 66 28 20 77 72 6f 6e 67 5f 6e 75  lse if( wrong_nu
1b5ac 6d 5f 61 72 67 73 20 29 7b 0a 20 20 20 20 20 20  m_args ){.      
1b5ad 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1b5ae 67 28 70 50 61 72 73 65 2c 22 77 72 6f 6e 67 20  g(pParse,"wrong 
1b5af 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  number of argume
1b5b0 6e 74 73 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20  nts to function 
1b5b1 25 2e 2a 73 28 29 22 2c 0a 20 20 20 20 20 20 20  %.*s()",.       
1b5b2 20 20 20 20 20 20 6e 49 64 2c 20 7a 49 64 29 3b        nId, zId);
1b5b3 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45  .        pNC->nE
1b5b4 72 72 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rr++;.      }.  
1b5b5 20 20 20 20 69 66 28 20 69 73 5f 61 67 67 20 29      if( is_agg )
1b5b6 7b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d  {.        pExpr-
1b5b7 3e 6f 70 20 3d 20 54 4b 5f 41 47 47 5f 46 55 4e  >op = TK_AGG_FUN
1b5b8 43 54 49 4f 4e 3b 0a 20 20 20 20 20 20 20 20 70  CTION;.        p
1b5b9 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 31 3b 0a  NC->hasAgg = 1;.
1b5ba 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
1b5bb 28 20 69 73 5f 61 67 67 20 29 20 70 4e 43 2d 3e  ( is_agg ) pNC->
1b5bc 61 6c 6c 6f 77 41 67 67 20 3d 20 30 3b 0a 20 20  allowAgg = 0;.  
1b5bd 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45      sqlite3WalkE
1b5be 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c  xprList(pWalker,
1b5bf 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 69   pList);.      i
1b5c0 66 28 20 69 73 5f 61 67 67 20 29 20 70 4e 43 2d  f( is_agg ) pNC-
1b5c1 3e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 20  >allowAgg = 1;. 
1b5c2 20 20 20 20 20 2f 2a 20 46 49 58 20 4d 45 3a 20       /* FIX ME: 
1b5c3 20 43 6f 6d 70 75 74 65 20 70 45 78 70 72 2d 3e   Compute pExpr->
1b5c4 61 66 66 69 6e 69 74 79 20 62 61 73 65 64 20 6f  affinity based o
1b5c5 6e 20 74 68 65 20 65 78 70 65 63 74 65 64 20 72  n the expected r
1b5c6 65 74 75 72 6e 0a 20 20 20 20 20 20 2a 2a 20 74  eturn.      ** t
1b5c7 79 70 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74  ype of the funct
1b5c8 69 6f 6e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ion .      */.  
1b5c9 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50      return WRC_P
1b5ca 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 23 69 66 6e  rune;.    }.#ifn
1b5cb 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1b5cc 53 55 42 51 55 45 52 59 0a 20 20 20 20 63 61 73  SUBQUERY.    cas
1b5cd 65 20 54 4b 5f 53 45 4c 45 43 54 3a 0a 20 20 20  e TK_SELECT:.   
1b5ce 20 63 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a   case TK_EXISTS:
1b5cf 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65  .#endif.    case
1b5d0 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20   TK_IN: {.      
1b5d1 69 66 28 20 70 45 78 70 72 2d 3e 70 53 65 6c 65  if( pExpr->pSele
1b5d2 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  ct ){.        in
1b5d3 74 20 6e 52 65 66 20 3d 20 70 4e 43 2d 3e 6e 52  t nRef = pNC->nR
1b5d4 65 66 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ef;.#ifndef SQLI
1b5d5 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20  TE_OMIT_CHECK.  
1b5d6 20 20 20 20 20 20 69 66 28 20 70 4e 43 2d 3e 69        if( pNC->i
1b5d7 73 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 20 20  sCheck ){.      
1b5d8 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1b5d9 4d 73 67 28 70 50 61 72 73 65 2c 22 73 75 62 71  Msg(pParse,"subq
1b5da 75 65 72 69 65 73 20 70 72 6f 68 69 62 69 74 65  ueries prohibite
1b5db 64 20 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73 74  d in CHECK const
1b5dc 72 61 69 6e 74 73 22 29 3b 0a 20 20 20 20 20 20  raints");.      
1b5dd 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20    }.#endif.     
1b5de 20 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65     sqlite3WalkSe
1b5df 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 45  lect(pWalker, pE
1b5e0 78 70 72 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20  xpr->pSelect);. 
1b5e1 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1b5e2 4e 43 2d 3e 6e 52 65 66 3e 3d 6e 52 65 66 20 29  NC->nRef>=nRef )
1b5e3 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 52  ;.        if( nR
1b5e4 65 66 21 3d 70 4e 43 2d 3e 6e 52 65 66 20 29 7b  ef!=pNC->nRef ){
1b5e5 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72 53  .          ExprS
1b5e6 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  etProperty(pExpr
1b5e7 2c 20 45 50 5f 56 61 72 53 65 6c 65 63 74 29 3b  , EP_VarSelect);
1b5e8 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1b5e9 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
1b5ea 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
1b5eb 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a  LITE_OMIT_CHECK.
1b5ec 20 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49      case TK_VARI
1b5ed 41 42 4c 45 3a 20 7b 0a 20 20 20 20 20 20 69 66  ABLE: {.      if
1b5ee 28 20 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20 29  ( pNC->isCheck )
1b5ef 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1b5f0 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1b5f1 2c 22 70 61 72 61 6d 65 74 65 72 73 20 70 72 6f  ,"parameters pro
1b5f2 68 69 62 69 74 65 64 20 69 6e 20 43 48 45 43 4b  hibited in CHECK
1b5f3 20 63 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b 0a   constraints");.
1b5f4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
1b5f5 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
1b5f6 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  f.  }.  return (
1b5f7 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20  pParse->nErr || 
1b5f8 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c  pParse->db->mall
1b5f9 6f 63 46 61 69 6c 65 64 29 20 3f 20 57 52 43 5f  ocFailed) ? WRC_
1b5fa 41 62 6f 72 74 20 3a 20 57 52 43 5f 43 6f 6e 74  Abort : WRC_Cont
1b5fb 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70  inue;.}../*.** p
1b5fc 45 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74 20  EList is a list 
1b5fd 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 77  of expressions w
1b5fe 68 69 63 68 20 61 72 65 20 72 65 61 6c 6c 79 20  hich are really 
1b5ff 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f  the result set o
1b600 66 20 74 68 65 0a 2a 2a 20 61 20 53 45 4c 45 43  f the.** a SELEC
1b601 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 70 45  T statement.  pE
1b602 20 69 73 20 61 20 74 65 72 6d 20 69 6e 20 61 6e   is a term in an
1b603 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f   ORDER BY or GRO
1b604 55 50 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2a  UP BY clause..**
1b605 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68   This routine ch
1b606 65 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20 70  ecks to see if p
1b607 45 20 69 73 20 61 20 73 69 6d 70 6c 65 20 69 64  E is a simple id
1b608 65 6e 74 69 66 69 65 72 20 77 68 69 63 68 20 63  entifier which c
1b609 6f 72 72 65 73 70 6f 6e 64 73 0a 2a 2a 20 74 6f  orresponds.** to
1b60a 20 74 68 65 20 41 53 2d 6e 61 6d 65 20 6f 66 20   the AS-name of 
1b60b 6f 6e 65 20 6f 66 20 74 68 65 20 74 65 72 6d 73  one of the terms
1b60c 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
1b60d 6f 6e 20 6c 69 73 74 2e 20 20 49 66 20 69 74 20  on list.  If it 
1b60e 69 73 2c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74  is,.** this rout
1b60f 69 6e 65 20 72 65 74 75 72 6e 20 61 6e 20 69 6e  ine return an in
1b610 74 65 67 65 72 20 62 65 74 77 65 65 6e 20 31 20  teger between 1 
1b611 61 6e 64 20 4e 20 77 68 65 72 65 20 4e 20 69 73  and N where N is
1b612 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   the number of.*
1b613 2a 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 70 45  * elements in pE
1b614 4c 69 73 74 2c 20 63 6f 72 72 65 73 70 6f 6e 64  List, correspond
1b615 69 6e 67 20 74 6f 20 74 68 65 20 6d 61 74 63 68  ing to the match
1b616 69 6e 67 20 65 6e 74 72 79 2e 20 20 49 66 20 74  ing entry.  If t
1b617 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 20 6d 61  here is.** no ma
1b618 74 63 68 2c 20 6f 72 20 69 66 20 70 45 20 69 73  tch, or if pE is
1b619 20 6e 6f 74 20 61 20 73 69 6d 70 6c 65 20 69 64   not a simple id
1b61a 65 6e 74 69 66 69 65 72 2c 20 74 68 65 6e 20 74  entifier, then t
1b61b 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72  his routine.** r
1b61c 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 70  eturn 0..**.** p
1b61d 45 4c 69 73 74 20 68 61 73 20 62 65 65 6e 20 72  EList has been r
1b61e 65 73 6f 6c 76 65 64 2e 20 20 70 45 20 68 61 73  esolved.  pE has
1b61f 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   not..*/.static 
1b620 69 6e 74 20 72 65 73 6f 6c 76 65 41 73 4e 61 6d  int resolveAsNam
1b621 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  e(.  Parse *pPar
1b622 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69  se,     /* Parsi
1b623 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 65  ng context for e
1b624 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f  rror messages */
1b625 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
1b626 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66  ist,  /* List of
1b627 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 6f 20   expressions to 
1b628 73 63 61 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a  scan */.  Expr *
1b629 70 45 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  pE           /* 
1b62a 45 78 70 72 65 73 73 69 6f 6e 20 77 65 20 61 72  Expression we ar
1b62b 65 20 74 72 79 69 6e 67 20 74 6f 20 6d 61 74 63  e trying to matc
1b62c 68 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  h */.){.  int i;
1b62d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1b62e 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
1b62f 0a 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54  .  if( pE->op==T
1b630 4b 5f 49 44 20 7c 7c 20 28 70 45 2d 3e 6f 70 3d  K_ID || (pE->op=
1b631 3d 54 4b 5f 53 54 52 49 4e 47 20 26 26 20 70 45  =TK_STRING && pE
1b632 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 21 3d 27 5c  ->token.z[0]!='\
1b633 27 27 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  '') ){.    sqlit
1b634 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
1b635 3e 64 62 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a  >db;.    char *z
1b636 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  Col = sqlite3Nam
1b637 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 26  eFromToken(db, &
1b638 70 45 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 20 20  pE->token);.    
1b639 69 66 28 20 7a 43 6f 6c 3d 3d 30 20 29 7b 0a 20  if( zCol==0 ){. 
1b63a 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a       return -1;.
1b63b 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d      }.    for(i=
1b63c 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78  0; i<pEList->nEx
1b63d 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  pr; i++){.      
1b63e 63 68 61 72 20 2a 7a 41 73 20 3d 20 70 45 4c 69  char *zAs = pELi
1b63f 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a  st->a[i].zName;.
1b640 20 20 20 20 20 20 69 66 28 20 7a 41 73 21 3d 30        if( zAs!=0
1b641 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43   && sqlite3StrIC
1b642 6d 70 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30  mp(zAs, zCol)==0
1b643 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1b644 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43  te3DbFree(db, zC
1b645 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  ol);.        ret
1b646 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 20 20 7d  urn i+1;.      }
1b647 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
1b648 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f  e3DbFree(db, zCo
1b649 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  l);.  }.  return
1b64a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 20   0;.}../*.** pE 
1b64b 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
1b64c 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 77 68  an expression wh
1b64d 69 63 68 20 69 73 20 61 20 73 69 6e 67 6c 65 20  ich is a single 
1b64e 74 65 72 6d 20 69 6e 20 74 68 65 0a 2a 2a 20 4f  term in the.** O
1b64f 52 44 45 52 20 42 59 20 6f 66 20 61 20 63 6f 6d  RDER BY of a com
1b650 70 6f 75 6e 64 20 53 45 4c 45 43 54 2e 20 20 54  pound SELECT.  T
1b651 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 68 61  he expression ha
1b652 73 20 6e 6f 74 20 62 65 65 6e 0a 2a 2a 20 6e 61  s not been.** na
1b653 6d 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a 2a 0a  me resolved..**.
1b654 2a 2a 20 41 74 20 74 68 65 20 70 6f 69 6e 74 20  ** At the point 
1b655 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
1b656 63 61 6c 6c 65 64 2c 20 77 65 20 61 6c 72 65 61  called, we alrea
1b657 64 79 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65  dy know that the
1b658 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 74 65 72  .** ORDER BY ter
1b659 6d 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65  m is not an inte
1b65a 67 65 72 20 69 6e 64 65 78 20 69 6e 74 6f 20 74  ger index into t
1b65b 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20  he result set.  
1b65c 54 68 61 74 0a 2a 2a 20 63 61 73 65 20 69 73 20  That.** case is 
1b65d 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 20 63  handled by the c
1b65e 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 2e 0a  alling routine..
1b65f 2a 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f  **.** Attempt to
1b660 20 6d 61 74 63 68 20 70 45 20 61 67 61 69 6e 73   match pE agains
1b661 74 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c  t result set col
1b662 75 6d 6e 73 20 69 6e 20 74 68 65 20 6c 65 66 74  umns in the left
1b663 2d 6d 6f 73 74 0a 2a 2a 20 53 45 4c 45 43 54 20  -most.** SELECT 
1b664 73 74 61 74 65 6d 65 6e 74 2e 20 20 52 65 74 75  statement.  Retu
1b665 72 6e 20 74 68 65 20 69 6e 64 65 78 20 69 20 6f  rn the index i o
1b666 66 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 63  f the matching c
1b667 6f 6c 75 6d 6e 2c 0a 2a 2a 20 61 73 20 61 6e 20  olumn,.** as an 
1b668 69 6e 64 69 63 61 74 69 6f 6e 20 74 6f 20 74 68  indication to th
1b669 65 20 63 61 6c 6c 65 72 20 74 68 61 74 20 69 74  e caller that it
1b66a 20 73 68 6f 75 6c 64 20 73 6f 72 74 20 62 79 20   should sort by 
1b66b 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 2e  the i-th column.
1b66c 0a 2a 2a 20 54 68 65 20 6c 65 66 74 2d 6d 6f 73  .** The left-mos
1b66d 74 20 63 6f 6c 75 6d 6e 20 69 73 20 31 2e 20 20  t column is 1.  
1b66e 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
1b66f 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
1b670 65 64 20 69 73 20 74 68 65 0a 2a 2a 20 73 61 6d  ed is the.** sam
1b671 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  e integer value 
1b672 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20 75 73  that would be us
1b673 65 64 20 69 6e 20 74 68 65 20 53 51 4c 20 73 74  ed in the SQL st
1b674 61 74 65 6d 65 6e 74 20 74 6f 20 69 6e 64 69 63  atement to indic
1b675 61 74 65 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d  ate.** the colum
1b676 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72  n..**.** If ther
1b677 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2c 20 72  e is no match, r
1b678 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 6e  eturn 0.  Return
1b679 20 2d 31 20 69 66 20 61 6e 20 65 72 72 6f 72 20   -1 if an error 
1b67a 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69  occurs..*/.stati
1b67b 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 4f 72 64  c int resolveOrd
1b67c 65 72 42 79 54 65 72 6d 54 6f 45 78 70 72 4c 69  erByTermToExprLi
1b67d 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  st(.  Parse *pPa
1b67e 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73  rse,     /* Pars
1b67f 69 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f 72 20  ing context for 
1b680 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a  error messages *
1b681 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  /.  Select *pSel
1b682 65 63 74 2c 20 20 20 2f 2a 20 54 68 65 20 53 45  ect,   /* The SE
1b683 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 77  LECT statement w
1b684 69 74 68 20 74 68 65 20 4f 52 44 45 52 20 42 59  ith the ORDER BY
1b685 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70   clause */.  Exp
1b686 72 20 2a 70 45 20 20 20 20 20 20 20 20 20 20 20  r *pE           
1b687 2f 2a 20 54 68 65 20 73 70 65 63 69 66 69 63 20  /* The specific 
1b688 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 2a 2f  ORDER BY term */
1b689 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20  .){.  int i;    
1b68a 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
1b68b 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78   counter */.  Ex
1b68c 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20  prList *pEList; 
1b68d 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20   /* The columns 
1b68e 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  of the result se
1b68f 74 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65  t */.  NameConte
1b690 78 74 20 6e 63 3b 20 20 20 20 2f 2a 20 4e 61 6d  xt nc;    /* Nam
1b691 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 72 65  e context for re
1b692 73 6f 6c 76 69 6e 67 20 70 45 20 2a 2f 0a 0a 20  solving pE */.. 
1b693 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1b694 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 45  ExprIsInteger(pE
1b695 2c 20 26 69 29 3d 3d 30 20 29 3b 0a 20 20 70 45  , &i)==0 );.  pE
1b696 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e  List = pSelect->
1b697 70 45 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20 52 65  pEList;..  /* Re
1b698 73 6f 6c 76 65 20 61 6c 6c 20 6e 61 6d 65 73 20  solve all names 
1b699 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  in the ORDER BY 
1b69a 74 65 72 6d 20 65 78 70 72 65 73 73 69 6f 6e 0a  term expression.
1b69b 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 6e    */.  memset(&n
1b69c 63 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6e 63 29  c, 0, sizeof(nc)
1b69d 29 3b 0a 20 20 6e 63 2e 70 50 61 72 73 65 20 3d  );.  nc.pParse =
1b69e 20 70 50 61 72 73 65 3b 0a 20 20 6e 63 2e 70 53   pParse;.  nc.pS
1b69f 72 63 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74  rcList = pSelect
1b6a0 2d 3e 70 53 72 63 3b 0a 20 20 6e 63 2e 70 45 4c  ->pSrc;.  nc.pEL
1b6a1 69 73 74 20 3d 20 70 45 4c 69 73 74 3b 0a 20 20  ist = pEList;.  
1b6a2 6e 63 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b  nc.allowAgg = 1;
1b6a3 0a 20 20 6e 63 2e 6e 45 72 72 20 3d 20 30 3b 0a  .  nc.nErr = 0;.
1b6a4 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73    if( sqlite3Res
1b6a5 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 6e  olveExprNames(&n
1b6a6 63 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 73 71  c, pE) ){.    sq
1b6a7 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28  lite3ErrorClear(
1b6a8 70 50 61 72 73 65 29 3b 0a 20 20 20 20 72 65 74  pParse);.    ret
1b6a9 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  urn 0;.  }..  /*
1b6aa 20 54 72 79 20 74 6f 20 6d 61 74 63 68 20 74 68   Try to match th
1b6ab 65 20 4f 52 44 45 52 20 42 59 20 65 78 70 72 65  e ORDER BY expre
1b6ac 73 73 69 6f 6e 20 61 67 61 69 6e 73 74 20 61 6e  ssion against an
1b6ad 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a   expression.  **
1b6ae 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
1b6af 65 74 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 31  et.  Return an 1
1b6b0 2d 62 61 73 65 64 20 69 6e 64 65 78 20 6f 66 20  -based index of 
1b6b1 74 68 65 20 6d 61 74 63 68 69 6e 67 0a 20 20 2a  the matching.  *
1b6b2 2a 20 72 65 73 75 6c 74 2d 73 65 74 20 65 6e 74  * result-set ent
1b6b3 72 79 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  ry..  */.  for(i
1b6b4 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45  =0; i<pEList->nE
1b6b5 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  xpr; i++){.    i
1b6b6 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  f( sqlite3ExprCo
1b6b7 6d 70 61 72 65 28 70 45 4c 69 73 74 2d 3e 61 5b  mpare(pEList->a[
1b6b8 69 5d 2e 70 45 78 70 72 2c 20 70 45 29 20 29 7b  i].pExpr, pE) ){
1b6b9 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b  .      return i+
1b6ba 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  1;.    }.  }..  
1b6bb 2f 2a 20 49 66 20 6e 6f 20 6d 61 74 63 68 2c 20  /* If no match, 
1b6bc 72 65 74 75 72 6e 20 30 2e 20 2a 2f 0a 20 20 72  return 0. */.  r
1b6bd 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
1b6be 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 20 4f 52  * Generate an OR
1b6bf 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20  DER BY or GROUP 
1b6c0 42 59 20 74 65 72 6d 20 6f 75 74 2d 6f 66 2d 72  BY term out-of-r
1b6c1 61 6e 67 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73  ange error..*/.s
1b6c2 74 61 74 69 63 20 76 6f 69 64 20 72 65 73 6f 6c  tatic void resol
1b6c3 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 6f  veOutOfRangeErro
1b6c4 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  r(.  Parse *pPar
1b6c5 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  se,         /* T
1b6c6 68 65 20 65 72 72 6f 72 20 63 6f 6e 74 65 78 74  he error context
1b6c7 20 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 77   into which to w
1b6c8 72 69 74 65 20 74 68 65 20 65 72 72 6f 72 20 2a  rite the error *
1b6c9 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1b6ca 7a 54 79 70 65 2c 20 20 20 20 20 2f 2a 20 22 4f  zType,     /* "O
1b6cb 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22  RDER" or "GROUP"
1b6cc 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20 20   */.  int i,    
1b6cd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1b6ce 54 68 65 20 69 6e 64 65 78 20 28 31 2d 62 61 73  The index (1-bas
1b6cf 65 64 29 20 6f 66 20 74 68 65 20 74 65 72 6d 20  ed) of the term 
1b6d0 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2a 2f 0a  out of range */.
1b6d1 20 20 69 6e 74 20 6d 78 20 20 20 20 20 20 20 20    int mx        
1b6d2 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 72 67           /* Larg
1b6d3 65 73 74 20 70 65 72 6d 69 73 73 69 62 6c 65 20  est permissible 
1b6d4 76 61 6c 75 65 20 6f 66 20 69 20 2a 2f 0a 29 7b  value of i */.){
1b6d5 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d  .  sqlite3ErrorM
1b6d6 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  sg(pParse, .    
1b6d7 22 25 72 20 25 73 20 42 59 20 74 65 72 6d 20 6f  "%r %s BY term o
1b6d8 75 74 20 6f 66 20 72 61 6e 67 65 20 2d 20 73 68  ut of range - sh
1b6d9 6f 75 6c 64 20 62 65 20 22 0a 20 20 20 20 22 62  ould be ".    "b
1b6da 65 74 77 65 65 6e 20 31 20 61 6e 64 20 25 64 22  etween 1 and %d"
1b6db 2c 20 69 2c 20 7a 54 79 70 65 2c 20 6d 78 29 3b  , i, zType, mx);
1b6dc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 7a  .}../*.** Analyz
1b6dd 65 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  e the ORDER BY c
1b6de 6c 61 75 73 65 20 69 6e 20 61 20 63 6f 6d 70 6f  lause in a compo
1b6df 75 6e 64 20 53 45 4c 45 43 54 20 73 74 61 74 65  und SELECT state
1b6e0 6d 65 6e 74 2e 20 20 20 4d 6f 64 69 66 79 0a 2a  ment.   Modify.*
1b6e1 2a 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74  * each term of t
1b6e2 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
1b6e3 73 65 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74  se is a constant
1b6e4 20 69 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e   integer between
1b6e5 20 31 0a 2a 2a 20 61 6e 64 20 4e 20 77 68 65 72   1.** and N wher
1b6e6 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65  e N is the numbe
1b6e7 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
1b6e8 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c  the compound SEL
1b6e9 45 43 54 2e 0a 2a 2a 0a 2a 2a 20 4f 52 44 45 52  ECT..**.** ORDER
1b6ea 20 42 59 20 74 65 72 6d 73 20 74 68 61 74 20 61   BY terms that a
1b6eb 72 65 20 61 6c 72 65 61 64 79 20 61 6e 20 69 6e  re already an in
1b6ec 74 65 67 65 72 20 62 65 74 77 65 65 6e 20 31 20  teger between 1 
1b6ed 61 6e 64 20 4e 20 61 72 65 0a 2a 2a 20 75 6e 6d  and N are.** unm
1b6ee 6f 64 69 66 69 65 64 2e 20 20 4f 52 44 45 52 20  odified.  ORDER 
1b6ef 42 59 20 74 65 72 6d 73 20 74 68 61 74 20 61 72  BY terms that ar
1b6f0 65 20 69 6e 74 65 67 65 72 73 20 6f 75 74 73 69  e integers outsi
1b6f1 64 65 20 74 68 65 20 72 61 6e 67 65 20 6f 66 0a  de the range of.
1b6f2 2a 2a 20 31 20 74 68 72 6f 75 67 68 20 4e 20 67  ** 1 through N g
1b6f3 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72  enerate an error
1b6f4 2e 20 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d  .  ORDER BY term
1b6f5 73 20 74 68 61 74 20 61 72 65 20 65 78 70 72 65  s that are expre
1b6f6 73 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61  ssions.** are ma
1b6f7 74 63 68 65 64 20 61 67 61 69 6e 73 74 20 72 65  tched against re
1b6f8 73 75 6c 74 20 73 65 74 20 65 78 70 72 65 73 73  sult set express
1b6f9 69 6f 6e 73 20 6f 66 20 63 6f 6d 70 6f 75 6e 64  ions of compound
1b6fa 20 53 45 4c 45 43 54 0a 2a 2a 20 62 65 67 69 6e   SELECT.** begin
1b6fb 6e 69 6e 67 20 77 69 74 68 20 74 68 65 20 6c 65  ning with the le
1b6fc 66 74 2d 6d 6f 73 74 20 53 45 4c 45 43 54 20 61  ft-most SELECT a
1b6fd 6e 64 20 77 6f 72 6b 69 6e 67 20 74 6f 77 61 72  nd working towar
1b6fe 64 20 74 68 65 20 72 69 67 68 74 2e 0a 2a 2a 20  d the right..** 
1b6ff 41 74 20 74 68 65 20 66 69 72 73 74 20 6d 61 74  At the first mat
1b700 63 68 2c 20 74 68 65 20 4f 52 44 45 52 20 42 59  ch, the ORDER BY
1b701 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74   expression is t
1b702 72 61 6e 73 66 6f 72 6d 65 64 20 69 6e 74 6f 0a  ransformed into.
1b703 2a 2a 20 74 68 65 20 69 6e 74 65 67 65 72 20 63  ** the integer c
1b704 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a  olumn number..**
1b705 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
1b706 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20  umber of errors 
1b707 73 65 65 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  seen..*/.static 
1b708 69 6e 74 20 72 65 73 6f 6c 76 65 43 6f 6d 70 6f  int resolveCompo
1b709 75 6e 64 4f 72 64 65 72 42 79 28 0a 20 20 50 61  undOrderBy(.  Pa
1b70a 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
1b70b 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
1b70c 6f 6e 74 65 78 74 2e 20 20 4c 65 61 76 65 20 65  ontext.  Leave e
1b70d 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68 65  rror messages he
1b70e 72 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  re */.  Select *
1b70f 70 53 65 6c 65 63 74 20 20 20 20 20 20 20 2f 2a  pSelect       /*
1b710 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   The SELECT stat
1b711 65 6d 65 6e 74 20 63 6f 6e 74 61 69 6e 69 6e 67  ement containing
1b712 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 2a 2f   the ORDER BY */
1b713 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 45  .){.  int i;.  E
1b714 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42  xprList *pOrderB
1b715 79 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  y;.  ExprList *p
1b716 45 4c 69 73 74 3b 0a 20 20 73 71 6c 69 74 65 33  EList;.  sqlite3
1b717 20 2a 64 62 3b 0a 20 20 69 6e 74 20 6d 6f 72 65   *db;.  int more
1b718 54 6f 44 6f 20 3d 20 31 3b 0a 0a 20 20 70 4f 72  ToDo = 1;..  pOr
1b719 64 65 72 42 79 20 3d 20 70 53 65 6c 65 63 74 2d  derBy = pSelect-
1b71a 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 69 66 28  >pOrderBy;.  if(
1b71b 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 20 72   pOrderBy==0 ) r
1b71c 65 74 75 72 6e 20 30 3b 0a 20 20 64 62 20 3d 20  eturn 0;.  db = 
1b71d 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23 69 66 20  pParse->db;.#if 
1b71e 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d  SQLITE_MAX_COLUM
1b71f 4e 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79  N.  if( pOrderBy
1b720 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 4c 69 6d  ->nExpr>db->aLim
1b721 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
1b722 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73  COLUMN] ){.    s
1b723 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1b724 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79  Parse, "too many
1b725 20 74 65 72 6d 73 20 69 6e 20 4f 52 44 45 52 20   terms in ORDER 
1b726 42 59 20 63 6c 61 75 73 65 22 29 3b 0a 20 20 20  BY clause");.   
1b727 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23   return 1;.  }.#
1b728 65 6e 64 69 66 0a 20 20 66 6f 72 28 69 3d 30 3b  endif.  for(i=0;
1b729 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78   i<pOrderBy->nEx
1b72a 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 4f  pr; i++){.    pO
1b72b 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f 6e  rderBy->a[i].don
1b72c 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 53 65  e = 0;.  }.  pSe
1b72d 6c 65 63 74 2d 3e 70 4e 65 78 74 20 3d 20 30 3b  lect->pNext = 0;
1b72e 0a 20 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63  .  while( pSelec
1b72f 74 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20  t->pPrior ){.   
1b730 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72   pSelect->pPrior
1b731 2d 3e 70 4e 65 78 74 20 3d 20 70 53 65 6c 65 63  ->pNext = pSelec
1b732 74 3b 0a 20 20 20 20 70 53 65 6c 65 63 74 20 3d  t;.    pSelect =
1b733 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72   pSelect->pPrior
1b734 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 70  ;.  }.  while( p
1b735 53 65 6c 65 63 74 20 26 26 20 6d 6f 72 65 54 6f  Select && moreTo
1b736 44 6f 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74  Do ){.    struct
1b737 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
1b738 70 49 74 65 6d 3b 0a 20 20 20 20 6d 6f 72 65 54  pItem;.    moreT
1b739 6f 44 6f 20 3d 20 30 3b 0a 20 20 20 20 70 45 4c  oDo = 0;.    pEL
1b73a 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70  ist = pSelect->p
1b73b 45 4c 69 73 74 3b 0a 20 20 20 20 61 73 73 65 72  EList;.    asser
1b73c 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a  t( pEList!=0 );.
1b73d 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74      for(i=0, pIt
1b73e 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20  em=pOrderBy->a; 
1b73f 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  i<pOrderBy->nExp
1b740 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29  r; i++, pItem++)
1b741 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c  {.      int iCol
1b742 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 45 78 70   = -1;.      Exp
1b743 72 20 2a 70 45 2c 20 2a 70 44 75 70 3b 0a 20 20  r *pE, *pDup;.  
1b744 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 64      if( pItem->d
1b745 6f 6e 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  one ) continue;.
1b746 20 20 20 20 20 20 70 45 20 3d 20 70 49 74 65 6d        pE = pItem
1b747 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 69  ->pExpr;.      i
1b748 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73  f( sqlite3ExprIs
1b749 49 6e 74 65 67 65 72 28 70 45 2c 20 26 69 43 6f  Integer(pE, &iCo
1b74a 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  l) ){.        if
1b74b 28 20 69 43 6f 6c 3c 30 20 7c 7c 20 69 43 6f 6c  ( iCol<0 || iCol
1b74c 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29  >pEList->nExpr )
1b74d 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 6f  {.          reso
1b74e 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72  lveOutOfRangeErr
1b74f 6f 72 28 70 50 61 72 73 65 2c 20 22 4f 52 44 45  or(pParse, "ORDE
1b750 52 22 2c 20 69 2b 31 2c 20 70 45 4c 69 73 74 2d  R", i+1, pEList-
1b751 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20  >nExpr);.       
1b752 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
1b753 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
1b754 73 65 7b 0a 20 20 20 20 20 20 20 20 69 43 6f 6c  se{.        iCol
1b755 20 3d 20 72 65 73 6f 6c 76 65 41 73 4e 61 6d 65   = resolveAsName
1b756 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c  (pParse, pEList,
1b757 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20 69 66   pE);.        if
1b758 28 20 69 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20  ( iCol==0 ){.   
1b759 20 20 20 20 20 20 20 70 44 75 70 20 3d 20 73 71         pDup = sq
1b75a 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
1b75b 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20 20 20   pE);.          
1b75c 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  if( !db->mallocF
1b75d 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20  ailed ){.       
1b75e 20 20 20 20 20 61 73 73 65 72 74 28 70 44 75 70       assert(pDup
1b75f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  );.            i
1b760 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 65 4f 72 64  Col = resolveOrd
1b761 65 72 42 79 54 65 72 6d 54 6f 45 78 70 72 4c 69  erByTermToExprLi
1b762 73 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65  st(pParse, pSele
1b763 63 74 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 20  ct, pDup);.     
1b764 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
1b765 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
1b766 74 65 28 64 62 2c 20 70 44 75 70 29 3b 0a 20 20  te(db, pDup);.  
1b767 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1b768 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20  if( iCol<0 ){.  
1b769 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31          return 1
1b76a 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1b76b 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 43    }.      if( iC
1b76c 6f 6c 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  ol>0 ){.        
1b76d 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d  CollSeq *pColl =
1b76e 20 70 45 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20   pE->pColl;.    
1b76f 20 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20      int flags = 
1b770 70 45 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45  pE->flags & EP_E
1b771 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 20  xpCollate;.     
1b772 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
1b773 6c 65 74 65 28 64 62 2c 20 70 45 29 3b 0a 20 20  lete(db, pE);.  
1b774 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78        pItem->pEx
1b775 70 72 20 3d 20 70 45 20 3d 20 73 71 6c 69 74 65  pr = pE = sqlite
1b776 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 49 4e 54  3Expr(db, TK_INT
1b777 45 47 45 52 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  EGER, 0, 0, 0);.
1b778 20 20 20 20 20 20 20 20 69 66 28 20 70 45 3d 3d          if( pE==
1b779 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  0 ) return 1;.  
1b77a 20 20 20 20 20 20 70 45 2d 3e 70 43 6f 6c 6c 20        pE->pColl 
1b77b 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20  = pColl;.       
1b77c 20 70 45 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50   pE->flags |= EP
1b77d 5f 49 6e 74 56 61 6c 75 65 20 7c 20 66 6c 61 67  _IntValue | flag
1b77e 73 3b 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 69  s;.        pE->i
1b77f 54 61 62 6c 65 20 3d 20 69 43 6f 6c 3b 0a 20 20  Table = iCol;.  
1b780 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f        pItem->iCo
1b781 6c 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20  l = iCol;.      
1b782 20 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20    pItem->done = 
1b783 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  1;.      }else{.
1b784 20 20 20 20 20 20 20 20 6d 6f 72 65 54 6f 44 6f          moreToDo
1b785 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
1b786 20 20 7d 0a 20 20 20 20 70 53 65 6c 65 63 74 20    }.    pSelect 
1b787 3d 20 70 53 65 6c 65 63 74 2d 3e 70 4e 65 78 74  = pSelect->pNext
1b788 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b  ;.  }.  for(i=0;
1b789 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78   i<pOrderBy->nEx
1b78a 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  pr; i++){.    if
1b78b 28 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d  ( pOrderBy->a[i]
1b78c 2e 64 6f 6e 65 3d 3d 30 20 29 7b 0a 20 20 20 20  .done==0 ){.    
1b78d 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1b78e 67 28 70 50 61 72 73 65 2c 20 22 25 72 20 4f 52  g(pParse, "%r OR
1b78f 44 45 52 20 42 59 20 74 65 72 6d 20 64 6f 65 73  DER BY term does
1b790 20 6e 6f 74 20 6d 61 74 63 68 20 61 6e 79 20 22   not match any "
1b791 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 63 6f  .            "co
1b792 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75  lumn in the resu
1b793 6c 74 20 73 65 74 22 2c 20 69 2b 31 29 3b 0a 20  lt set", i+1);. 
1b794 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
1b795 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
1b796 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  n 0;.}../*.** Ch
1b797 65 63 6b 20 65 76 65 72 79 20 74 65 72 6d 20 69  eck every term i
1b798 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 6f  n the ORDER BY o
1b799 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  r GROUP BY claus
1b79a 65 20 70 4f 72 64 65 72 42 79 20 6f 66 0a 2a 2a  e pOrderBy of.**
1b79b 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
1b79c 65 6d 65 6e 74 20 70 53 65 6c 65 63 74 2e 20 20  ement pSelect.  
1b79d 49 66 20 61 6e 79 20 74 65 72 6d 20 69 73 20 72  If any term is r
1b79e 65 66 65 72 65 6e 63 65 20 74 6f 20 61 0a 2a 2a  eference to a.**
1b79f 20 72 65 73 75 6c 74 20 73 65 74 20 65 78 70 72   result set expr
1b7a0 65 73 73 69 6f 6e 20 28 61 73 20 64 65 74 65 72  ession (as deter
1b7a1 6d 69 6e 65 64 20 62 79 20 74 68 65 20 45 78 70  mined by the Exp
1b7a2 72 4c 69 73 74 2e 61 2e 69 43 6f 6c 20 66 69 65  rList.a.iCol fie
1b7a3 6c 64 29 0a 2a 2a 20 74 68 65 6e 20 63 6f 6e 76  ld).** then conv
1b7a4 65 72 74 20 74 68 61 74 20 74 65 72 6d 20 69 6e  ert that term in
1b7a5 74 6f 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  to a copy of the
1b7a6 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 72   corresponding r
1b7a7 65 73 75 6c 74 20 73 65 74 0a 2a 2a 20 63 6f 6c  esult set.** col
1b7a8 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  umn..**.** If an
1b7a9 79 20 65 72 72 6f 72 73 20 61 72 65 20 64 65 74  y errors are det
1b7aa 65 63 74 65 64 2c 20 61 64 64 20 61 6e 20 65 72  ected, add an er
1b7ab 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70  ror message to p
1b7ac 50 61 72 73 65 20 61 6e 64 0a 2a 2a 20 72 65 74  Parse and.** ret
1b7ad 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 52  urn non-zero.  R
1b7ae 65 74 75 72 6e 20 7a 65 72 6f 20 69 66 20 6e 6f  eturn zero if no
1b7af 20 65 72 72 6f 72 73 20 61 72 65 20 73 65 65 6e   errors are seen
1b7b0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1b7b1 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52  ATE int sqlite3R
1b7b2 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70  esolveOrderGroup
1b7b3 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  By(.  Parse *pPa
1b7b4 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  rse,        /* P
1b7b5 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20  arsing context. 
1b7b6 20 4c 65 61 76 65 20 65 72 72 6f 72 20 6d 65 73   Leave error mes
1b7b7 73 61 67 65 73 20 68 65 72 65 20 2a 2f 0a 20 20  sages here */.  
1b7b8 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c  Select *pSelect,
1b7b9 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c        /* The SEL
1b7ba 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 63 6f  ECT statement co
1b7bb 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 63 6c 61  ntaining the cla
1b7bc 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  use */.  ExprLis
1b7bd 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 2f  t *pOrderBy,   /
1b7be 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 6f  * The ORDER BY o
1b7bf 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  r GROUP BY claus
1b7c0 65 20 74 6f 20 62 65 20 70 72 6f 63 65 73 73 65  e to be processe
1b7c1 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  d */.  const cha
1b7c2 72 20 2a 7a 54 79 70 65 20 20 20 20 20 2f 2a 20  r *zType     /* 
1b7c3 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55  "ORDER" or "GROU
1b7c4 50 22 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  P" */.){.  int i
1b7c5 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
1b7c6 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
1b7c7 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
1b7c8 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c  ;.  struct ExprL
1b7c9 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
1b7ca 0a 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79  ..  if( pOrderBy
1b7cb 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64  ==0 || pParse->d
1b7cc 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1b7cd 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 20  ) return 0;.#if 
1b7ce 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d  SQLITE_MAX_COLUM
1b7cf 4e 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79  N.  if( pOrderBy
1b7d0 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 4c 69 6d  ->nExpr>db->aLim
1b7d1 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
1b7d2 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73  COLUMN] ){.    s
1b7d3 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1b7d4 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79  Parse, "too many
1b7d5 20 74 65 72 6d 73 20 69 6e 20 25 73 20 42 59 20   terms in %s BY 
1b7d6 63 6c 61 75 73 65 22 2c 20 7a 54 79 70 65 29 3b  clause", zType);
1b7d7 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
1b7d8 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 45 4c 69   }.#endif.  pELi
1b7d9 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45  st = pSelect->pE
1b7da 4c 69 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20  List;.  assert( 
1b7db 70 45 4c 69 73 74 21 3d 30 20 29 3b 20 20 2f 2a  pEList!=0 );  /*
1b7dc 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65   sqlite3SelectNe
1b7dd 77 28 29 20 67 75 61 72 61 6e 74 65 65 73 20 74  w() guarantees t
1b7de 68 69 73 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  his */.  for(i=0
1b7df 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79  , pItem=pOrderBy
1b7e0 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d  ->a; i<pOrderBy-
1b7e1 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74  >nExpr; i++, pIt
1b7e2 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70  em++){.    if( p
1b7e3 49 74 65 6d 2d 3e 69 43 6f 6c 20 29 7b 0a 20 20  Item->iCol ){.  
1b7e4 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69      if( pItem->i
1b7e5 43 6f 6c 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70  Col>pEList->nExp
1b7e6 72 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 73  r ){.        res
1b7e7 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72  olveOutOfRangeEr
1b7e8 72 6f 72 28 70 50 61 72 73 65 2c 20 7a 54 79 70  ror(pParse, zTyp
1b7e9 65 2c 20 69 2b 31 2c 20 70 45 4c 69 73 74 2d 3e  e, i+1, pEList->
1b7ea 6e 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20  nExpr);.        
1b7eb 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20  return 1;.      
1b7ec 7d 0a 20 20 20 20 20 20 72 65 73 6f 6c 76 65 41  }.      resolveA
1b7ed 6c 69 61 73 28 70 50 61 72 73 65 2c 20 70 45 4c  lias(pParse, pEL
1b7ee 69 73 74 2c 20 70 49 74 65 6d 2d 3e 69 43 6f 6c  ist, pItem->iCol
1b7ef 2d 31 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72  -1, pItem->pExpr
1b7f0 2c 20 7a 54 79 70 65 29 3b 0a 20 20 20 20 7d 0a  , zType);.    }.
1b7f1 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
1b7f2 7d 0a 0a 2f 2a 0a 2a 2a 20 70 4f 72 64 65 72 42  }../*.** pOrderB
1b7f3 79 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59  y is an ORDER BY
1b7f4 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61   or GROUP BY cla
1b7f5 75 73 65 20 69 6e 20 53 45 4c 45 43 54 20 73 74  use in SELECT st
1b7f6 61 74 65 6d 65 6e 74 20 70 53 65 6c 65 63 74 2e  atement pSelect.
1b7f7 0a 2a 2a 20 54 68 65 20 4e 61 6d 65 20 63 6f 6e  .** The Name con
1b7f8 74 65 78 74 20 6f 66 20 74 68 65 20 53 45 4c 45  text of the SELE
1b7f9 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  CT statement is 
1b7fa 70 4e 43 2e 20 20 7a 54 79 70 65 20 69 73 20 65  pNC.  zType is e
1b7fb 69 74 68 65 72 0a 2a 2a 20 22 4f 52 44 45 52 22  ither.** "ORDER"
1b7fc 20 6f 72 20 22 47 52 4f 55 50 22 20 64 65 70 65   or "GROUP" depe
1b7fd 6e 64 69 6e 67 20 6f 6e 20 77 68 69 63 68 20 74  nding on which t
1b7fe 79 70 65 20 6f 66 20 63 6c 61 75 73 65 20 70 4f  ype of clause pO
1b7ff 72 64 65 72 42 79 20 69 73 2e 0a 2a 2a 0a 2a 2a  rderBy is..**.**
1b800 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
1b801 73 6f 6c 76 65 73 20 65 61 63 68 20 74 65 72 6d  solves each term
1b802 20 6f 66 20 74 68 65 20 63 6c 61 75 73 65 20 69   of the clause i
1b803 6e 74 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f  nto an expressio
1b804 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6f 72 64  n..** If the ord
1b805 65 72 2d 62 79 20 74 65 72 6d 20 69 73 20 61 6e  er-by term is an
1b806 20 69 6e 74 65 67 65 72 20 49 20 62 65 74 77 65   integer I betwe
1b807 65 6e 20 31 20 61 6e 64 20 4e 20 28 77 68 65 72  en 1 and N (wher
1b808 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75  e N is the.** nu
1b809 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
1b80a 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
1b80b 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 29  t of the SELECT)
1b80c 20 74 68 65 6e 20 74 68 65 20 65 78 70 72 65 73   then the expres
1b80d 73 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 72  sion.** in the r
1b80e 65 73 6f 6c 75 74 69 6f 6e 20 69 73 20 61 20 63  esolution is a c
1b80f 6f 70 79 20 6f 66 20 74 68 65 20 49 2d 74 68 20  opy of the I-th 
1b810 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65  result-set expre
1b811 73 73 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 74 68  ssion.  If.** th
1b812 65 20 6f 72 64 65 72 2d 62 79 20 74 65 72 6d 20  e order-by term 
1b813 69 73 20 61 6e 20 69 64 65 6e 74 69 66 79 20 74  is an identify t
1b814 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20  hat corresponds 
1b815 74 6f 20 74 68 65 20 41 53 2d 6e 61 6d 65 20 6f  to the AS-name o
1b816 66 0a 2a 2a 20 61 20 72 65 73 75 6c 74 2d 73 65  f.** a result-se
1b817 74 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 74 68  t expression, th
1b818 65 6e 20 74 68 65 20 74 65 72 6d 20 72 65 73 6f  en the term reso
1b819 6c 76 65 73 20 74 6f 20 61 20 63 6f 70 79 20 6f  lves to a copy o
1b81a 66 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2d  f the.** result-
1b81b 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 20  set expression. 
1b81c 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20   Otherwise, the 
1b81d 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 72 65  expression is re
1b81e 73 6f 6c 76 65 64 20 69 6e 0a 2a 2a 20 74 68 65  solved in.** the
1b81f 20 75 73 75 61 6c 20 77 61 79 20 2d 20 75 73 69   usual way - usi
1b820 6e 67 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  ng sqlite3Resolv
1b821 65 45 78 70 72 4e 61 6d 65 73 28 29 2e 0a 2a 2a  eExprNames()..**
1b822 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1b823 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
1b824 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 20 20  ber of errors.  
1b825 49 66 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c  If errors occur,
1b826 20 74 68 65 6e 0a 2a 2a 20 61 6e 20 61 70 70 72   then.** an appr
1b827 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65  opriate error me
1b828 73 73 61 67 65 20 6d 69 67 68 74 20 62 65 20 6c  ssage might be l
1b829 65 66 74 20 69 6e 20 70 50 61 72 73 65 2e 20 20  eft in pParse.  
1b82a 28 4f 4f 4d 20 65 72 72 6f 72 73 0a 2a 2a 20 65  (OOM errors.** e
1b82b 78 63 65 70 74 65 64 2e 29 0a 2a 2f 0a 73 74 61  xcepted.).*/.sta
1b82c 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 4f  tic int resolveO
1b82d 72 64 65 72 47 72 6f 75 70 42 79 28 0a 20 20 4e  rderGroupBy(.  N
1b82e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c  ameContext *pNC,
1b82f 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65       /* The name
1b830 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20   context of the 
1b831 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
1b832 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53   */.  Select *pS
1b833 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 54  elect,      /* T
1b834 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
1b835 65 6e 74 20 68 6f 6c 64 69 6e 67 20 70 4f 72 64  ent holding pOrd
1b836 65 72 42 79 20 2a 2f 0a 20 20 45 78 70 72 4c 69  erBy */.  ExprLi
1b837 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20  st *pOrderBy,   
1b838 2f 2a 20 41 6e 20 4f 52 44 45 52 20 42 59 20 6f  /* An ORDER BY o
1b839 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  r GROUP BY claus
1b83a 65 20 74 6f 20 72 65 73 6f 6c 76 65 20 2a 2f 0a  e to resolve */.
1b83b 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
1b83c 79 70 65 20 20 20 20 20 2f 2a 20 45 69 74 68 65  ype     /* Eithe
1b83d 72 20 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52  r "ORDER" or "GR
1b83e 4f 55 50 22 2c 20 61 73 20 61 70 70 72 6f 70 72  OUP", as appropr
1b83f 69 61 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  iate */.){.  int
1b840 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
1b841 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1b842 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
1b843 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20   int iCol;      
1b844 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b845 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  /* Column number
1b846 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70   */.  struct Exp
1b847 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  rList_item *pIte
1b848 6d 3b 20 20 20 2f 2a 20 41 20 74 65 72 6d 20 6f  m;   /* A term o
1b849 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  f the ORDER BY c
1b84a 6c 61 75 73 65 20 2a 2f 0a 20 20 50 61 72 73 65  lause */.  Parse
1b84b 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20   *pParse;       
1b84c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
1b84d 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
1b84e 20 20 69 6e 74 20 6e 52 65 73 75 6c 74 3b 20 20    int nResult;  
1b84f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b850 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65   /* Number of te
1b851 72 6d 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c  rms in the resul
1b852 74 20 73 65 74 20 2a 2f 0a 0a 20 20 69 66 28 20  t set */..  if( 
1b853 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 20 72 65  pOrderBy==0 ) re
1b854 74 75 72 6e 20 30 3b 0a 20 20 6e 52 65 73 75 6c  turn 0;.  nResul
1b855 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c  t = pSelect->pEL
1b856 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 50  ist->nExpr;.  pP
1b857 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72  arse = pNC->pPar
1b858 73 65 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70  se;.  for(i=0, p
1b859 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61  Item=pOrderBy->a
1b85a 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45  ; i<pOrderBy->nE
1b85b 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b  xpr; i++, pItem+
1b85c 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45  +){.    Expr *pE
1b85d 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b   = pItem->pExpr;
1b85e 0a 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f  .    iCol = reso
1b85f 6c 76 65 41 73 4e 61 6d 65 28 70 50 61 72 73 65  lveAsName(pParse
1b860 2c 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73  , pSelect->pELis
1b861 74 2c 20 70 45 29 3b 0a 20 20 20 20 69 66 28 20  t, pE);.    if( 
1b862 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20  iCol<0 ){.      
1b863 72 65 74 75 72 6e 20 31 3b 20 20 2f 2a 20 4f 4f  return 1;  /* OO
1b864 4d 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 7d  M error */.    }
1b865 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 30 20  .    if( iCol>0 
1b866 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 61  ){.      /* If a
1b867 6e 20 41 53 2d 6e 61 6d 65 20 6d 61 74 63 68 20  n AS-name match 
1b868 69 73 20 66 6f 75 6e 64 2c 20 6d 61 72 6b 20 74  is found, mark t
1b869 68 69 73 20 4f 52 44 45 52 20 42 59 20 63 6f 6c  his ORDER BY col
1b86a 75 6d 6e 20 61 73 20 62 65 69 6e 67 0a 20 20 20  umn as being.   
1b86b 20 20 20 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20     ** a copy of 
1b86c 74 68 65 20 69 43 6f 6c 2d 74 68 20 72 65 73 75  the iCol-th resu
1b86d 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 2e 20 20  lt-set column.  
1b86e 54 68 65 20 73 75 62 73 65 71 75 65 6e 74 20 63  The subsequent c
1b86f 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20  all to.      ** 
1b870 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72  sqlite3ResolveOr
1b871 64 65 72 47 72 6f 75 70 42 79 28 29 20 77 69 6c  derGroupBy() wil
1b872 6c 20 63 6f 6e 76 65 72 74 20 74 68 65 20 65 78  l convert the ex
1b873 70 72 65 73 73 69 6f 6e 20 74 6f 20 61 0a 20 20  pression to a.  
1b874 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 66 20 74      ** copy of t
1b875 68 65 20 69 43 6f 6c 2d 74 68 20 72 65 73 75 6c  he iCol-th resul
1b876 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e  t-set expression
1b877 2e 20 2a 2f 0a 20 20 20 20 20 20 70 49 74 65 6d  . */.      pItem
1b878 2d 3e 69 43 6f 6c 20 3d 20 69 43 6f 6c 3b 0a 20  ->iCol = iCol;. 
1b879 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
1b87a 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
1b87b 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65  ite3ExprIsIntege
1b87c 72 28 70 45 2c 20 26 69 43 6f 6c 29 20 29 7b 0a  r(pE, &iCol) ){.
1b87d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44        /* The ORD
1b87e 45 52 20 42 59 20 74 65 72 6d 20 69 73 20 61 6e  ER BY term is an
1b87f 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e   integer constan
1b880 74 2e 20 20 41 67 61 69 6e 2c 20 73 65 74 20 74  t.  Again, set t
1b881 68 65 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 20 20  he column.      
1b882 2a 2a 20 6e 75 6d 62 65 72 20 73 6f 20 74 68 61  ** number so tha
1b883 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  t sqlite3Resolve
1b884 4f 72 64 65 72 47 72 6f 75 70 42 79 28 29 20 77  OrderGroupBy() w
1b885 69 6c 6c 20 63 6f 6e 76 65 72 74 20 74 68 65 0a  ill convert the.
1b886 20 20 20 20 20 20 2a 2a 20 6f 72 64 65 72 2d 62        ** order-b
1b887 79 20 74 65 72 6d 20 74 6f 20 61 20 63 6f 70 79  y term to a copy
1b888 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73   of the result-s
1b889 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  et expression */
1b88a 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c  .      if( iCol<
1b88b 31 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 73  1 ){.        res
1b88c 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72  olveOutOfRangeEr
1b88d 72 6f 72 28 70 50 61 72 73 65 2c 20 7a 54 79 70  ror(pParse, zTyp
1b88e 65 2c 20 69 2b 31 2c 20 6e 52 65 73 75 6c 74 29  e, i+1, nResult)
1b88f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
1b890 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
1b891 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20    pItem->iCol = 
1b892 69 43 6f 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 74  iCol;.      cont
1b893 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  inue;.    }..   
1b894 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74   /* Otherwise, t
1b895 72 65 61 74 20 74 68 65 20 4f 52 44 45 52 20 42  reat the ORDER B
1b896 59 20 74 65 72 6d 20 61 73 20 61 6e 20 6f 72 64  Y term as an ord
1b897 69 6e 61 72 79 20 65 78 70 72 65 73 73 69 6f 6e  inary expression
1b898 20 2a 2f 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69   */.    pItem->i
1b899 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 69 66 28  Col = 0;.    if(
1b89a 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
1b89b 78 70 72 4e 61 6d 65 73 28 70 4e 43 2c 20 70 45  xprNames(pNC, pE
1b89c 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
1b89d 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n 1;.    }.  }. 
1b89e 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 52   return sqlite3R
1b89f 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70  esolveOrderGroup
1b8a0 42 79 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65  By(pParse, pSele
1b8a1 63 74 2c 20 70 4f 72 64 65 72 42 79 2c 20 7a 54  ct, pOrderBy, zT
1b8a2 79 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ype);.}../*.** R
1b8a3 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20  esolve names in 
1b8a4 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  the SELECT state
1b8a5 6d 65 6e 74 20 70 20 61 6e 64 20 61 6c 6c 20 6f  ment p and all o
1b8a6 66 20 69 74 73 20 64 65 73 63 65 6e 64 65 6e 74  f its descendent
1b8a7 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
1b8a8 20 72 65 73 6f 6c 76 65 53 65 6c 65 63 74 53 74   resolveSelectSt
1b8a9 65 70 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b  ep(Walker *pWalk
1b8aa 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a  er, Select *p){.
1b8ab 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70    NameContext *p
1b8ac 4f 75 74 65 72 4e 43 3b 20 20 2f 2a 20 43 6f 6e  OuterNC;  /* Con
1b8ad 74 65 78 74 20 74 68 61 74 20 63 6f 6e 74 61 69  text that contai
1b8ae 6e 73 20 74 68 69 73 20 53 45 4c 45 43 54 20 2a  ns this SELECT *
1b8af 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  /.  NameContext 
1b8b0 73 4e 43 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  sNC;        /* N
1b8b1 61 6d 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 74  ame context of t
1b8b2 68 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20  his SELECT */.  
1b8b3 69 6e 74 20 69 73 43 6f 6d 70 6f 75 6e 64 3b 20  int isCompound; 
1b8b4 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
1b8b5 69 66 20 70 20 69 73 20 61 20 63 6f 6d 70 6f 75  if p is a compou
1b8b6 6e 64 20 73 65 6c 65 63 74 20 2a 2f 0a 20 20 69  nd select */.  i
1b8b7 6e 74 20 6e 43 6f 6d 70 6f 75 6e 64 3b 20 20 20  nt nCompound;   
1b8b8 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1b8b9 20 6f 66 20 63 6f 6d 70 6f 75 6e 64 20 74 65 72   of compound ter
1b8ba 6d 73 20 70 72 6f 63 65 73 73 65 64 20 73 6f 20  ms processed so 
1b8bb 66 61 72 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a  far */.  Parse *
1b8bc 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20  pParse;         
1b8bd 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
1b8be 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ext */.  ExprLis
1b8bf 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20  t *pEList;      
1b8c0 20 2f 2a 20 52 65 73 75 6c 74 20 73 65 74 20 65   /* Result set e
1b8c1 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 2a  xpression list *
1b8c2 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  /.  int i;      
1b8c3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1b8c4 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
1b8c5 20 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f 75   ExprList *pGrou
1b8c6 70 42 79 3b 20 20 20 20 20 2f 2a 20 54 68 65 20  pBy;     /* The 
1b8c7 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20  GROUP BY clause 
1b8c8 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 4c 65  */.  Select *pLe
1b8c9 66 74 6d 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20  ftmost;      /* 
1b8ca 4c 65 66 74 2d 6d 6f 73 74 20 6f 66 20 53 45 4c  Left-most of SEL
1b8cb 45 43 54 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e  ECT of a compoun
1b8cc 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  d */.  sqlite3 *
1b8cd 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  db;            /
1b8ce 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
1b8cf 63 74 69 6f 6e 20 2a 2f 0a 20 20 0a 0a 20 20 61  ction */.  ..  a
1b8d0 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20  ssert( p!=0 );. 
1b8d1 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73   if( p->selFlags
1b8d2 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 20 29   & SF_Resolved )
1b8d3 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43  {.    return WRC
1b8d4 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 20 70 4f  _Prune;.  }.  pO
1b8d5 75 74 65 72 4e 43 20 3d 20 70 57 61 6c 6b 65 72  uterNC = pWalker
1b8d6 2d 3e 75 2e 70 4e 43 3b 0a 20 20 70 50 61 72 73  ->u.pNC;.  pPars
1b8d7 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50 61  e = pWalker->pPa
1b8d8 72 73 65 3b 0a 20 20 64 62 20 3d 20 70 50 61 72  rse;.  db = pPar
1b8d9 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 4e 6f  se->db;..  /* No
1b8da 72 6d 61 6c 6c 79 20 73 71 6c 69 74 65 33 53 65  rmally sqlite3Se
1b8db 6c 65 63 74 45 78 70 61 6e 64 28 29 20 77 69 6c  lectExpand() wil
1b8dc 6c 20 62 65 20 63 61 6c 6c 65 64 20 66 69 72 73  l be called firs
1b8dd 74 20 61 6e 64 20 77 69 6c 6c 20 68 61 76 65 0a  t and will have.
1b8de 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 65 78 70    ** already exp
1b8df 61 6e 64 65 64 20 74 68 69 73 20 53 45 4c 45 43  anded this SELEC
1b8e0 54 2e 20 20 48 6f 77 65 76 65 72 2c 20 69 66 20  T.  However, if 
1b8e1 74 68 69 73 20 69 73 20 61 20 73 75 62 71 75 65  this is a subque
1b8e2 72 79 20 77 69 74 68 69 6e 0a 20 20 2a 2a 20 61  ry within.  ** a
1b8e3 6e 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 73 71  n expression, sq
1b8e4 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
1b8e5 4e 61 6d 65 73 28 29 20 77 69 6c 6c 20 62 65 20  Names() will be 
1b8e6 63 61 6c 6c 65 64 20 77 69 74 68 6f 75 74 20 61  called without a
1b8e7 0a 20 20 2a 2a 20 70 72 69 6f 72 20 63 61 6c 6c  .  ** prior call
1b8e8 20 74 6f 20 73 71 6c 69 74 65 33 53 65 6c 65 63   to sqlite3Selec
1b8e9 74 45 78 70 61 6e 64 28 29 2e 20 20 57 68 65 6e  tExpand().  When
1b8ea 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c 20 6c   that happens, l
1b8eb 65 74 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 53  et.  ** sqlite3S
1b8ec 65 6c 65 63 74 50 72 65 70 28 29 20 64 6f 20 61  electPrep() do a
1b8ed 6c 6c 20 6f 66 20 74 68 65 20 70 72 6f 63 65 73  ll of the proces
1b8ee 73 69 6e 67 20 66 6f 72 20 74 68 69 73 20 53 45  sing for this SE
1b8ef 4c 45 43 54 2e 0a 20 20 2a 2a 20 73 71 6c 69 74  LECT..  ** sqlit
1b8f0 65 33 53 65 6c 65 63 74 50 72 65 70 28 29 20 77  e3SelectPrep() w
1b8f1 69 6c 6c 20 69 6e 76 6f 6b 65 20 62 6f 74 68 20  ill invoke both 
1b8f2 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70  sqlite3SelectExp
1b8f3 61 6e 64 28 29 20 61 6e 64 0a 20 20 2a 2a 20 74  and() and.  ** t
1b8f4 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6e 20 74  his routine in t
1b8f5 68 65 20 63 6f 72 72 65 63 74 20 6f 72 64 65 72  he correct order
1b8f6 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 2d  ..  */.  if( (p-
1b8f7 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 45  >selFlags & SF_E
1b8f8 78 70 61 6e 64 65 64 29 3d 3d 30 20 29 7b 0a 20  xpanded)==0 ){. 
1b8f9 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
1b8fa 50 72 65 70 28 70 50 61 72 73 65 2c 20 70 2c 20  Prep(pParse, p, 
1b8fb 70 4f 75 74 65 72 4e 43 29 3b 0a 20 20 20 20 72  pOuterNC);.    r
1b8fc 65 74 75 72 6e 20 28 70 50 61 72 73 65 2d 3e 6e  eturn (pParse->n
1b8fd 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  Err || db->mallo
1b8fe 63 46 61 69 6c 65 64 29 20 3f 20 57 52 43 5f 41  cFailed) ? WRC_A
1b8ff 62 6f 72 74 20 3a 20 57 52 43 5f 50 72 75 6e 65  bort : WRC_Prune
1b900 3b 0a 20 20 7d 0a 0a 20 20 69 73 43 6f 6d 70 6f  ;.  }..  isCompo
1b901 75 6e 64 20 3d 20 70 2d 3e 70 50 72 69 6f 72 21  und = p->pPrior!
1b902 3d 30 3b 0a 20 20 6e 43 6f 6d 70 6f 75 6e 64 20  =0;.  nCompound 
1b903 3d 20 30 3b 0a 20 20 70 4c 65 66 74 6d 6f 73 74  = 0;.  pLeftmost
1b904 20 3d 20 70 3b 0a 20 20 77 68 69 6c 65 28 20 70   = p;.  while( p
1b905 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1b906 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53  (p->selFlags & S
1b907 46 5f 45 78 70 61 6e 64 65 64 29 21 3d 30 20 29  F_Expanded)!=0 )
1b908 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  ;.    assert( (p
1b909 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f  ->selFlags & SF_
1b90a 52 65 73 6f 6c 76 65 64 29 3d 3d 30 20 29 3b 0a  Resolved)==0 );.
1b90b 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20      p->selFlags 
1b90c 7c 3d 20 53 46 5f 52 65 73 6f 6c 76 65 64 3b 0a  |= SF_Resolved;.
1b90d 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20  .    /* Resolve 
1b90e 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  the expressions 
1b90f 69 6e 20 74 68 65 20 4c 49 4d 49 54 20 61 6e 64  in the LIMIT and
1b910 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65 73 2e   OFFSET clauses.
1b911 20 54 68 65 73 65 0a 20 20 20 20 2a 2a 20 61 72   These.    ** ar
1b912 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f  e not allowed to
1b913 20 72 65 66 65 72 20 74 6f 20 61 6e 79 20 6e 61   refer to any na
1b914 6d 65 73 2c 20 73 6f 20 70 61 73 73 20 61 6e 20  mes, so pass an 
1b915 65 6d 70 74 79 20 4e 61 6d 65 43 6f 6e 74 65 78  empty NameContex
1b916 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6d 65  t..    */.    me
1b917 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69  mset(&sNC, 0, si
1b918 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 20 20  zeof(sNC));.    
1b919 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50 61  sNC.pParse = pPa
1b91a 72 73 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  rse;.    if( sql
1b91b 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e  ite3ResolveExprN
1b91c 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 4c  ames(&sNC, p->pL
1b91d 69 6d 69 74 29 20 7c 7c 0a 20 20 20 20 20 20 20  imit) ||.       
1b91e 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
1b91f 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70  xprNames(&sNC, p
1b920 2d 3e 70 4f 66 66 73 65 74 29 20 29 7b 0a 20 20  ->pOffset) ){.  
1b921 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41      return WRC_A
1b922 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 0a 20  bort;.    }.  . 
1b923 20 20 20 2f 2a 20 53 65 74 20 75 70 20 74 68 65     /* Set up the
1b924 20 6c 6f 63 61 6c 20 6e 61 6d 65 2d 63 6f 6e 74   local name-cont
1b925 65 78 74 20 74 6f 20 70 61 73 73 20 74 6f 20 73  ext to pass to s
1b926 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
1b927 72 4e 61 6d 65 73 28 29 20 74 6f 0a 20 20 20 20  rNames() to.    
1b928 2a 2a 20 72 65 73 6f 6c 76 65 20 74 68 65 20 72  ** resolve the r
1b929 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73  esult-set expres
1b92a 73 69 6f 6e 20 6c 69 73 74 2e 0a 20 20 20 20 2a  sion list..    *
1b92b 2f 0a 20 20 20 20 73 4e 43 2e 61 6c 6c 6f 77 41  /.    sNC.allowA
1b92c 67 67 20 3d 20 31 3b 0a 20 20 20 20 73 4e 43 2e  gg = 1;.    sNC.
1b92d 70 53 72 63 4c 69 73 74 20 3d 20 70 2d 3e 70 53  pSrcList = p->pS
1b92e 72 63 3b 0a 20 20 20 20 73 4e 43 2e 70 4e 65 78  rc;.    sNC.pNex
1b92f 74 20 3d 20 70 4f 75 74 65 72 4e 43 3b 0a 20 20  t = pOuterNC;.  
1b930 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20  .    /* Resolve 
1b931 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 72 65 73  names in the res
1b932 75 6c 74 20 73 65 74 2e 20 2a 2f 0a 20 20 20 20  ult set. */.    
1b933 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69  pEList = p->pELi
1b934 73 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  st;.    assert( 
1b935 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 20  pEList!=0 );.   
1b936 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69   for(i=0; i<pELi
1b937 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  st->nExpr; i++){
1b938 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 58 20  .      Expr *pX 
1b939 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70  = pEList->a[i].p
1b93a 45 78 70 72 3b 0a 20 20 20 20 20 20 69 66 28 20  Expr;.      if( 
1b93b 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
1b93c 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 58  prNames(&sNC, pX
1b93d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  ) ){.        ret
1b93e 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
1b93f 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a       }.    }.  .
1b940 20 20 20 20 2f 2a 20 52 65 63 75 72 73 69 76 65      /* Recursive
1b941 6c 79 20 72 65 73 6f 6c 76 65 20 6e 61 6d 65 73  ly resolve names
1b942 20 69 6e 20 61 6c 6c 20 73 75 62 71 75 65 72 69   in all subqueri
1b943 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f  es.    */.    fo
1b944 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 70 53 72 63  r(i=0; i<p->pSrc
1b945 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20  ->nSrc; i++){.  
1b946 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69      struct SrcLi
1b947 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d  st_item *pItem =
1b948 20 26 70 2d 3e 70 53 72 63 2d 3e 61 5b 69 5d 3b   &p->pSrc->a[i];
1b949 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d  .      if( pItem
1b94a 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ->pSelect ){.   
1b94b 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
1b94c 2a 7a 53 61 76 65 64 43 6f 6e 74 65 78 74 20 3d  *zSavedContext =
1b94d 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f   pParse->zAuthCo
1b94e 6e 74 65 78 74 3b 0a 20 20 20 20 20 20 20 20 69  ntext;.        i
1b94f 66 28 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20  f( pItem->zName 
1b950 29 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43  ) pParse->zAuthC
1b951 6f 6e 74 65 78 74 20 3d 20 70 49 74 65 6d 2d 3e  ontext = pItem->
1b952 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 73  zName;.        s
1b953 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65 6c  qlite3ResolveSel
1b954 65 63 74 4e 61 6d 65 73 28 70 50 61 72 73 65 2c  ectNames(pParse,
1b955 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2c   pItem->pSelect,
1b956 20 26 73 4e 43 29 3b 0a 20 20 20 20 20 20 20 20   &sNC);.        
1b957 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e  pParse->zAuthCon
1b958 74 65 78 74 20 3d 20 7a 53 61 76 65 64 43 6f 6e  text = zSavedCon
1b959 74 65 78 74 3b 0a 20 20 20 20 20 20 20 20 69 66  text;.        if
1b95a 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c  ( pParse->nErr |
1b95b 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
1b95c 65 64 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  ed ) return WRC_
1b95d 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20  Abort;.      }. 
1b95e 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49     }.  .    /* I
1b95f 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 61  f there are no a
1b960 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
1b961 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ns in the result
1b962 2d 73 65 74 2c 20 61 6e 64 20 6e 6f 20 47 52 4f  -set, and no GRO
1b963 55 50 20 42 59 20 0a 20 20 20 20 2a 2a 20 65 78  UP BY .    ** ex
1b964 70 72 65 73 73 69 6f 6e 2c 20 64 6f 20 6e 6f 74  pression, do not
1b965 20 61 6c 6c 6f 77 20 61 67 67 72 65 67 61 74 65   allow aggregate
1b966 73 20 69 6e 20 61 6e 79 20 6f 66 20 74 68 65 20  s in any of the 
1b967 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e  other expression
1b968 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73  s..    */.    as
1b969 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61  sert( (p->selFla
1b96a 67 73 20 26 20 53 46 5f 41 67 67 72 65 67 61 74  gs & SF_Aggregat
1b96b 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 47 72  e)==0 );.    pGr
1b96c 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75  oupBy = p->pGrou
1b96d 70 42 79 3b 0a 20 20 20 20 69 66 28 20 70 47 72  pBy;.    if( pGr
1b96e 6f 75 70 42 79 20 7c 7c 20 73 4e 43 2e 68 61 73  oupBy || sNC.has
1b96f 41 67 67 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  Agg ){.      p->
1b970 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 41  selFlags |= SF_A
1b971 67 67 72 65 67 61 74 65 3b 0a 20 20 20 20 7d 65  ggregate;.    }e
1b972 6c 73 65 7b 0a 20 20 20 20 20 20 73 4e 43 2e 61  lse{.      sNC.a
1b973 6c 6c 6f 77 41 67 67 20 3d 20 30 3b 0a 20 20 20  llowAgg = 0;.   
1b974 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20   }.  .    /* If 
1b975 61 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 20  a HAVING clause 
1b976 69 73 20 70 72 65 73 65 6e 74 2c 20 74 68 65 6e  is present, then
1b977 20 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 61   there must be a
1b978 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65   GROUP BY clause
1b979 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1b97a 20 70 2d 3e 70 48 61 76 69 6e 67 20 26 26 20 21   p->pHaving && !
1b97b 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20  pGroupBy ){.    
1b97c 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1b97d 67 28 70 50 61 72 73 65 2c 20 22 61 20 47 52 4f  g(pParse, "a GRO
1b97e 55 50 20 42 59 20 63 6c 61 75 73 65 20 69 73 20  UP BY clause is 
1b97f 72 65 71 75 69 72 65 64 20 62 65 66 6f 72 65 20  required before 
1b980 48 41 56 49 4e 47 22 29 3b 0a 20 20 20 20 20 20  HAVING");.      
1b981 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
1b982 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  ;.    }.  .    /
1b983 2a 20 41 64 64 20 74 68 65 20 65 78 70 72 65 73  * Add the expres
1b984 73 69 6f 6e 20 6c 69 73 74 20 74 6f 20 74 68 65  sion list to the
1b985 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 62 65   name-context be
1b986 66 6f 72 65 20 70 61 72 73 69 6e 67 20 74 68 65  fore parsing the
1b987 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 65 78  .    ** other ex
1b988 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65  pressions in the
1b989 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1b98a 74 2e 20 54 68 69 73 20 69 73 20 73 6f 20 74 68  t. This is so th
1b98b 61 74 0a 20 20 20 20 2a 2a 20 65 78 70 72 65 73  at.    ** expres
1b98c 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 57 48 45  sions in the WHE
1b98d 52 45 20 63 6c 61 75 73 65 20 28 65 74 63 2e 29  RE clause (etc.)
1b98e 20 63 61 6e 20 72 65 66 65 72 20 74 6f 20 65 78   can refer to ex
1b98f 70 72 65 73 73 69 6f 6e 73 20 62 79 0a 20 20 20  pressions by.   
1b990 20 2a 2a 20 61 6c 69 61 73 65 73 20 69 6e 20 74   ** aliases in t
1b991 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 20  he result set.. 
1b992 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4d 69 6e     **.    ** Min
1b993 6f 72 20 70 6f 69 6e 74 3a 20 49 66 20 74 68 69  or point: If thi
1b994 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74  s is the case, t
1b995 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69  hen the expressi
1b996 6f 6e 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 2a  on will be.    *
1b997 2a 20 72 65 2d 65 76 61 6c 75 61 74 65 64 20 66  * re-evaluated f
1b998 6f 72 20 65 61 63 68 20 72 65 66 65 72 65 6e 63  or each referenc
1b999 65 20 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a  e to it..    */.
1b99a 20 20 20 20 73 4e 43 2e 70 45 4c 69 73 74 20 3d      sNC.pEList =
1b99b 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20   p->pEList;.    
1b99c 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c  if( sqlite3Resol
1b99d 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43  veExprNames(&sNC
1b99e 2c 20 70 2d 3e 70 57 68 65 72 65 29 20 7c 7c 0a  , p->pWhere) ||.
1b99f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65         sqlite3Re
1b9a0 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26  solveExprNames(&
1b9a1 73 4e 43 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29  sNC, p->pHaving)
1b9a2 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65  .    ){.      re
1b9a3 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
1b9a4 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68      }..    /* Th
1b9a5 65 20 4f 52 44 45 52 20 42 59 20 61 6e 64 20 47  e ORDER BY and G
1b9a6 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 73 20  ROUP BY clauses 
1b9a7 6d 61 79 20 6e 6f 74 20 72 65 66 65 72 20 74 6f  may not refer to
1b9a8 20 74 65 72 6d 73 20 69 6e 0a 20 20 20 20 2a 2a   terms in.    **
1b9a9 20 6f 75 74 65 72 20 71 75 65 72 69 65 73 20 0a   outer queries .
1b9aa 20 20 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 70      */.    sNC.p
1b9ab 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 73 4e  Next = 0;.    sN
1b9ac 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a  C.allowAgg = 1;.
1b9ad 0a 20 20 20 20 2f 2a 20 50 72 6f 63 65 73 73 20  .    /* Process 
1b9ae 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
1b9af 75 73 65 20 66 6f 72 20 73 69 6e 67 6c 65 74 6f  use for singleto
1b9b0 6e 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  n SELECT stateme
1b9b1 6e 74 73 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20  nts..    ** The 
1b9b2 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
1b9b3 66 6f 72 20 63 6f 6d 70 6f 75 6e 64 73 20 53 45  for compounds SE
1b9b4 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20  LECT statements 
1b9b5 69 73 20 68 61 6e 64 6c 65 64 0a 20 20 20 20 2a  is handled.    *
1b9b6 2a 20 62 65 6c 6f 77 2c 20 61 66 74 65 72 20 61  * below, after a
1b9b7 6c 6c 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  ll of the result
1b9b8 2d 73 65 74 73 20 66 6f 72 20 61 6c 6c 20 6f 66  -sets for all of
1b9b9 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66   the elements of
1b9ba 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 6f 6d 70  .    ** the comp
1b9bb 6f 75 6e 64 20 68 61 76 65 20 62 65 65 6e 20 72  ound have been r
1b9bc 65 73 6f 6c 76 65 64 2e 0a 20 20 20 20 2a 2f 0a  esolved..    */.
1b9bd 20 20 20 20 69 66 28 20 21 69 73 43 6f 6d 70 6f      if( !isCompo
1b9be 75 6e 64 20 26 26 20 72 65 73 6f 6c 76 65 4f 72  und && resolveOr
1b9bf 64 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43 2c  derGroupBy(&sNC,
1b9c0 20 70 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c   p, p->pOrderBy,
1b9c1 20 22 4f 52 44 45 52 22 29 20 29 7b 0a 20 20 20   "ORDER") ){.   
1b9c2 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62     return WRC_Ab
1b9c3 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ort;.    }.    i
1b9c4 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
1b9c5 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 72 65 74  led ){.      ret
1b9c6 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
1b9c7 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 52     }.  .    /* R
1b9c8 65 73 6f 6c 76 65 20 74 68 65 20 47 52 4f 55 50  esolve the GROUP
1b9c9 20 42 59 20 63 6c 61 75 73 65 2e 20 20 41 74 20   BY clause.  At 
1b9ca 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2c 20 6d  the same time, m
1b9cb 61 6b 65 20 73 75 72 65 20 0a 20 20 20 20 2a 2a  ake sure .    **
1b9cc 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c   the GROUP BY cl
1b9cd 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f  ause does not co
1b9ce 6e 74 61 69 6e 20 61 67 67 72 65 67 61 74 65 20  ntain aggregate 
1b9cf 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 20 20 2a  functions..    *
1b9d0 2f 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75 70  /.    if( pGroup
1b9d1 42 79 20 29 7b 0a 20 20 20 20 20 20 73 74 72 75  By ){.      stru
1b9d2 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d  ct ExprList_item
1b9d3 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 0a 20 20   *pItem;.    .  
1b9d4 20 20 20 20 69 66 28 20 72 65 73 6f 6c 76 65 4f      if( resolveO
1b9d5 72 64 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43  rderGroupBy(&sNC
1b9d6 2c 20 70 2c 20 70 47 72 6f 75 70 42 79 2c 20 22  , p, pGroupBy, "
1b9d7 47 52 4f 55 50 22 29 20 7c 7c 20 64 62 2d 3e 6d  GROUP") || db->m
1b9d8 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1b9d9 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52         return WR
1b9da 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d  C_Abort;.      }
1b9db 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20  .      for(i=0, 
1b9dc 70 49 74 65 6d 3d 70 47 72 6f 75 70 42 79 2d 3e  pItem=pGroupBy->
1b9dd 61 3b 20 69 3c 70 47 72 6f 75 70 42 79 2d 3e 6e  a; i<pGroupBy->n
1b9de 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  Expr; i++, pItem
1b9df 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
1b9e0 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
1b9e1 28 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 45  (pItem->pExpr, E
1b9e2 50 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20 20  P_Agg) ){.      
1b9e3 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1b9e4 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 67 67  Msg(pParse, "agg
1b9e5 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  regate functions
1b9e6 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64   are not allowed
1b9e7 20 69 6e 20 22 0a 20 20 20 20 20 20 20 20 20 20   in ".          
1b9e8 20 20 20 20 22 74 68 65 20 47 52 4f 55 50 20 42      "the GROUP B
1b9e9 59 20 63 6c 61 75 73 65 22 29 3b 0a 20 20 20 20  Y clause");.    
1b9ea 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
1b9eb 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 20 20  _Abort;.        
1b9ec 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
1b9ed 0a 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20  .    /* Advance 
1b9ee 74 6f 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d  to the next term
1b9ef 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64   of the compound
1b9f0 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 20 3d 20  .    */.    p = 
1b9f1 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 6e  p->pPrior;.    n
1b9f2 43 6f 6d 70 6f 75 6e 64 2b 2b 3b 0a 20 20 7d 0a  Compound++;.  }.
1b9f3 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68  .  /* Resolve th
1b9f4 65 20 4f 52 44 45 52 20 42 59 20 6f 6e 20 61 20  e ORDER BY on a 
1b9f5 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20  compound SELECT 
1b9f6 61 66 74 65 72 20 61 6c 6c 20 74 65 72 6d 73 20  after all terms 
1b9f7 6f 66 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6d 70  of.  ** the comp
1b9f8 6f 75 6e 64 20 68 61 76 65 20 62 65 65 6e 20 72  ound have been r
1b9f9 65 73 6f 6c 76 65 64 2e 0a 20 20 2a 2f 0a 20 20  esolved..  */.  
1b9fa 69 66 28 20 69 73 43 6f 6d 70 6f 75 6e 64 20 26  if( isCompound &
1b9fb 26 20 72 65 73 6f 6c 76 65 43 6f 6d 70 6f 75 6e  & resolveCompoun
1b9fc 64 4f 72 64 65 72 42 79 28 70 50 61 72 73 65 2c  dOrderBy(pParse,
1b9fd 20 70 4c 65 66 74 6d 6f 73 74 29 20 29 7b 0a 20   pLeftmost) ){. 
1b9fe 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62     return WRC_Ab
1b9ff 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  ort;.  }..  retu
1ba00 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 7d 0a  rn WRC_Prune;.}.
1ba01 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
1ba02 69 6e 65 20 77 61 6c 6b 73 20 61 6e 20 65 78 70  ine walks an exp
1ba03 72 65 73 73 69 6f 6e 20 74 72 65 65 20 61 6e 64  ression tree and
1ba04 20 72 65 73 6f 6c 76 65 73 20 72 65 66 65 72 65   resolves refere
1ba05 6e 63 65 73 20 74 6f 0a 2a 2a 20 74 61 62 6c 65  nces to.** table
1ba06 20 63 6f 6c 75 6d 6e 73 20 61 6e 64 20 72 65 73   columns and res
1ba07 75 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 73 2e  ult-set columns.
1ba08 20 20 41 74 20 74 68 65 20 73 61 6d 65 20 74 69    At the same ti
1ba09 6d 65 2c 20 64 6f 20 65 72 72 6f 72 0a 2a 2a 20  me, do error.** 
1ba0a 63 68 65 63 6b 69 6e 67 20 6f 6e 20 66 75 6e 63  checking on func
1ba0b 74 69 6f 6e 20 75 73 61 67 65 20 61 6e 64 20 73  tion usage and s
1ba0c 65 74 20 61 20 66 6c 61 67 20 69 66 20 61 6e 79  et a flag if any
1ba0d 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
1ba0e 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 73 65 65 6e  ions.** are seen
1ba0f 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c  ..**.** To resol
1ba10 76 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73  ve table columns
1ba11 20 72 65 66 65 72 65 6e 63 65 73 20 77 65 20 6c   references we l
1ba12 6f 6f 6b 20 66 6f 72 20 6e 6f 64 65 73 20 28 6f  ook for nodes (o
1ba13 72 20 73 75 62 74 72 65 65 73 29 20 6f 66 20 74  r subtrees) of t
1ba14 68 65 20 0a 2a 2a 20 66 6f 72 6d 20 58 2e 59 2e  he .** form X.Y.
1ba15 5a 20 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73 74  Z or Y.Z or just
1ba16 20 5a 20 77 68 65 72 65 0a 2a 2a 0a 2a 2a 20 20   Z where.**.**  
1ba17 20 20 20 20 58 3a 20 20 20 54 68 65 20 6e 61 6d      X:   The nam
1ba18 65 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 2e  e of a database.
1ba19 20 20 45 78 3a 20 20 22 6d 61 69 6e 22 20 6f 72    Ex:  "main" or
1ba1a 20 22 74 65 6d 70 22 20 6f 72 0a 2a 2a 20 20 20   "temp" or.**   
1ba1b 20 20 20 20 20 20 20 20 74 68 65 20 73 79 6d 62          the symb
1ba1c 6f 6c 69 63 20 6e 61 6d 65 20 61 73 73 69 67 6e  olic name assign
1ba1d 65 64 20 74 6f 20 61 6e 20 41 54 54 41 43 48 2d  ed to an ATTACH-
1ba1e 65 64 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a  ed database..**.
1ba1f 2a 2a 20 20 20 20 20 20 59 3a 20 20 20 54 68 65  **      Y:   The
1ba20 20 6e 61 6d 65 20 6f 66 20 61 20 74 61 62 6c 65   name of a table
1ba21 20 69 6e 20 61 20 46 52 4f 4d 20 63 6c 61 75 73   in a FROM claus
1ba22 65 2e 20 20 4f 72 20 69 6e 20 61 20 74 72 69 67  e.  Or in a trig
1ba23 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ger.**          
1ba24 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63   one of the spec
1ba25 69 61 6c 20 6e 61 6d 65 73 20 22 6f 6c 64 22 20  ial names "old" 
1ba26 6f 72 20 22 6e 65 77 22 2e 0a 2a 2a 0a 2a 2a 20  or "new"..**.** 
1ba27 20 20 20 20 20 5a 3a 20 20 20 54 68 65 20 6e 61       Z:   The na
1ba28 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69  me of a column i
1ba29 6e 20 74 61 62 6c 65 20 59 2e 0a 2a 2a 0a 2a 2a  n table Y..**.**
1ba2a 20 54 68 65 20 6e 6f 64 65 20 61 74 20 74 68 65   The node at the
1ba2b 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 73 75 62   root of the sub
1ba2c 74 72 65 65 20 69 73 20 6d 6f 64 69 66 69 65 64  tree is modified
1ba2d 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
1ba2e 2a 2a 20 20 20 20 45 78 70 72 2e 6f 70 20 20 20  **    Expr.op   
1ba2f 20 20 20 20 20 43 68 61 6e 67 65 64 20 74 6f 20       Changed to 
1ba30 54 4b 5f 43 4f 4c 55 4d 4e 0a 2a 2a 20 20 20 20  TK_COLUMN.**    
1ba31 45 78 70 72 2e 70 54 61 62 20 20 20 20 20 20 50  Expr.pTab      P
1ba32 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 54 61 62  oints to the Tab
1ba33 6c 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 58 2e  le object for X.
1ba34 59 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 43 6f  Y.**    Expr.iCo
1ba35 6c 75 6d 6e 20 20 20 54 68 65 20 63 6f 6c 75 6d  lumn   The colum
1ba36 6e 20 69 6e 64 65 78 20 69 6e 20 58 2e 59 2e 20  n index in X.Y. 
1ba37 20 2d 31 20 66 6f 72 20 74 68 65 20 72 6f 77 69   -1 for the rowi
1ba38 64 2e 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 54  d..**    Expr.iT
1ba39 61 62 6c 65 20 20 20 20 54 68 65 20 56 44 42 45  able    The VDBE
1ba3a 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66   cursor number f
1ba3b 6f 72 20 58 2e 59 0a 2a 2a 0a 2a 2a 0a 2a 2a 20  or X.Y.**.**.** 
1ba3c 54 6f 20 72 65 73 6f 6c 76 65 20 72 65 73 75 6c  To resolve resul
1ba3d 74 2d 73 65 74 20 72 65 66 65 72 65 6e 63 65 73  t-set references
1ba3e 2c 20 6c 6f 6f 6b 20 66 6f 72 20 65 78 70 72 65  , look for expre
1ba3f 73 73 69 6f 6e 20 6e 6f 64 65 73 20 6f 66 20 74  ssion nodes of t
1ba40 68 65 0a 2a 2a 20 66 6f 72 6d 20 5a 20 28 77 69  he.** form Z (wi
1ba41 74 68 20 6e 6f 20 58 20 61 6e 64 20 59 20 70 72  th no X and Y pr
1ba42 65 66 69 78 29 20 77 68 65 72 65 20 74 68 65 20  efix) where the 
1ba43 5a 20 6d 61 74 63 68 65 73 20 74 68 65 20 72 69  Z matches the ri
1ba44 67 68 74 2d 68 61 6e 64 0a 2a 2a 20 73 69 7a 65  ght-hand.** size
1ba45 20 6f 66 20 61 6e 20 41 53 20 63 6c 61 75 73 65   of an AS clause
1ba46 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 2d 73   in the result-s
1ba47 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 2e 20  et of a SELECT. 
1ba48 20 54 68 65 20 5a 20 65 78 70 72 65 73 73 69 6f   The Z expressio
1ba49 6e 0a 2a 2a 20 69 73 20 72 65 70 6c 61 63 65 64  n.** is replaced
1ba4a 20 62 79 20 61 20 63 6f 70 79 20 6f 66 20 74 68   by a copy of th
1ba4b 65 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65  e left-hand side
1ba4c 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73   of the result-s
1ba4d 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a  et expression..*
1ba4e 2a 20 54 61 62 6c 65 2d 6e 61 6d 65 20 61 6e 64  * Table-name and
1ba4f 20 66 75 6e 63 74 69 6f 6e 20 72 65 73 6f 6c 75   function resolu
1ba50 74 69 6f 6e 20 6f 63 63 75 72 73 20 6f 6e 20 74  tion occurs on t
1ba51 68 65 20 73 75 62 73 74 69 74 75 74 65 64 20 65  he substituted e
1ba52 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65  xpression.** tre
1ba53 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  e.  For example,
1ba54 20 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20   in:.**.**      
1ba55 53 45 4c 45 43 54 20 61 2b 62 20 41 53 20 78 2c  SELECT a+b AS x,
1ba56 20 63 2b 64 20 41 53 20 79 20 46 52 4f 4d 20 74   c+d AS y FROM t
1ba57 31 20 4f 52 44 45 52 20 42 59 20 78 3b 0a 2a 2a  1 ORDER BY x;.**
1ba58 0a 2a 2a 20 54 68 65 20 22 78 22 20 74 65 72 6d  .** The "x" term
1ba59 20 6f 66 20 74 68 65 20 6f 72 64 65 72 20 62 79   of the order by
1ba5a 20 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 20   is replaced by 
1ba5b 22 61 2b 62 22 20 74 6f 20 72 65 6e 64 65 72 3a  "a+b" to render:
1ba5c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 53 45 4c 45  .**.**      SELE
1ba5d 43 54 20 61 2b 62 20 41 53 20 78 2c 20 63 2b 64  CT a+b AS x, c+d
1ba5e 20 41 53 20 79 20 46 52 4f 4d 20 74 31 20 4f 52   AS y FROM t1 OR
1ba5f 44 45 52 20 42 59 20 61 2b 62 3b 0a 2a 2a 0a 2a  DER BY a+b;.**.*
1ba60 2a 20 46 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73  * Function calls
1ba61 20 61 72 65 20 63 68 65 63 6b 65 64 20 74 6f 20   are checked to 
1ba62 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74  make sure that t
1ba63 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 0a  he function is .
1ba64 2a 2a 20 64 65 66 69 6e 65 64 20 61 6e 64 20 74  ** defined and t
1ba65 68 61 74 20 74 68 65 20 63 6f 72 72 65 63 74 20  hat the correct 
1ba66 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  number of argume
1ba67 6e 74 73 20 61 72 65 20 73 70 65 63 69 66 69 65  nts are specifie
1ba68 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 75 6e  d..** If the fun
1ba69 63 74 69 6f 6e 20 69 73 20 61 6e 20 61 67 67 72  ction is an aggr
1ba6a 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20  egate function, 
1ba6b 74 68 65 6e 20 74 68 65 20 70 4e 43 2d 3e 68 61  then the pNC->ha
1ba6c 73 41 67 67 20 69 73 0a 2a 2a 20 73 65 74 20 61  sAgg is.** set a
1ba6d 6e 64 20 74 68 65 20 6f 70 63 6f 64 65 20 69 73  nd the opcode is
1ba6e 20 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 54 4b   changed from TK
1ba6f 5f 46 55 4e 43 54 49 4f 4e 20 74 6f 20 54 4b 5f  _FUNCTION to TK_
1ba70 41 47 47 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2a  AGG_FUNCTION..**
1ba71 20 49 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f   If an expressio
1ba72 6e 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65  n contains aggre
1ba73 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 74  gate functions t
1ba74 68 65 6e 20 74 68 65 20 45 50 5f 41 67 67 0a 2a  hen the EP_Agg.*
1ba75 2a 20 70 72 6f 70 65 72 74 79 20 6f 6e 20 74 68  * property on th
1ba76 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
1ba77 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 72  set..**.** An er
1ba78 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 6c  ror message is l
1ba79 65 66 74 20 69 6e 20 70 50 61 72 73 65 20 69 66  eft in pParse if
1ba7a 20 61 6e 79 74 68 69 6e 67 20 69 73 20 61 6d 69   anything is ami
1ba7b 73 73 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 0a  ss.  The number.
1ba7c 2a 2a 20 69 66 20 65 72 72 6f 72 73 20 69 73 20  ** if errors is 
1ba7d 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c  returned..*/.SQL
1ba7e 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1ba7f 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
1ba80 70 72 4e 61 6d 65 73 28 20 0a 20 20 4e 61 6d 65  prNames( .  Name
1ba81 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 20  Context *pNC,   
1ba82 20 20 20 20 2f 2a 20 4e 61 6d 65 73 70 61 63 65      /* Namespace
1ba83 20 74 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 72   to resolve expr
1ba84 65 73 73 69 6f 6e 73 20 69 6e 2e 20 2a 2f 0a 20  essions in. */. 
1ba85 20 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 20   Expr *pExpr    
1ba86 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1ba87 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65  expression to be
1ba88 20 61 6e 61 6c 79 7a 65 64 2e 20 2a 2f 0a 29 7b   analyzed. */.){
1ba89 0a 20 20 69 6e 74 20 73 61 76 65 64 48 61 73 41  .  int savedHasA
1ba8a 67 67 3b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a  gg;.  Walker w;.
1ba8b 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20  .  if( pExpr==0 
1ba8c 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 20  ) return 0;.#if 
1ba8d 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f  SQLITE_MAX_EXPR_
1ba8e 44 45 50 54 48 3e 30 0a 20 20 7b 0a 20 20 20 20  DEPTH>0.  {.    
1ba8f 50 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20  Parse *pParse = 
1ba90 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20  pNC->pParse;.   
1ba91 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72   if( sqlite3Expr
1ba92 43 68 65 63 6b 48 65 69 67 68 74 28 70 50 61 72  CheckHeight(pPar
1ba93 73 65 2c 20 70 45 78 70 72 2d 3e 6e 48 65 69 67  se, pExpr->nHeig
1ba94 68 74 2b 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e  ht+pNC->pParse->
1ba95 6e 48 65 69 67 68 74 29 20 29 7b 0a 20 20 20 20  nHeight) ){.    
1ba96 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
1ba97 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 48  }.    pParse->nH
1ba98 65 69 67 68 74 20 2b 3d 20 70 45 78 70 72 2d 3e  eight += pExpr->
1ba99 6e 48 65 69 67 68 74 3b 0a 20 20 7d 0a 23 65 6e  nHeight;.  }.#en
1ba9a 64 69 66 0a 20 20 73 61 76 65 64 48 61 73 41 67  dif.  savedHasAg
1ba9b 67 20 3d 20 70 4e 43 2d 3e 68 61 73 41 67 67 3b  g = pNC->hasAgg;
1ba9c 0a 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d  .  pNC->hasAgg =
1ba9d 20 30 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c   0;.  w.xExprCal
1ba9e 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 45  lback = resolveE
1ba9f 78 70 72 53 74 65 70 3b 0a 20 20 77 2e 78 53 65  xprStep;.  w.xSe
1baa0 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 72  lectCallback = r
1baa1 65 73 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 70  esolveSelectStep
1baa2 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70  ;.  w.pParse = p
1baa3 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 77 2e  NC->pParse;.  w.
1baa4 75 2e 70 4e 43 20 3d 20 70 4e 43 3b 0a 20 20 73  u.pNC = pNC;.  s
1baa5 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26  qlite3WalkExpr(&
1baa6 77 2c 20 70 45 78 70 72 29 3b 0a 23 69 66 20 53  w, pExpr);.#if S
1baa7 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44  QLITE_MAX_EXPR_D
1baa8 45 50 54 48 3e 30 0a 20 20 70 4e 43 2d 3e 70 50  EPTH>0.  pNC->pP
1baa9 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2d 3d  arse->nHeight -=
1baaa 20 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 3b   pExpr->nHeight;
1baab 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 4e  .#endif.  if( pN
1baac 43 2d 3e 6e 45 72 72 3e 30 20 29 7b 0a 20 20 20  C->nErr>0 ){.   
1baad 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79   ExprSetProperty
1baae 28 70 45 78 70 72 2c 20 45 50 5f 45 72 72 6f 72  (pExpr, EP_Error
1baaf 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e 43  );.  }.  if( pNC
1bab0 2d 3e 68 61 73 41 67 67 20 29 7b 0a 20 20 20 20  ->hasAgg ){.    
1bab1 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 28  ExprSetProperty(
1bab2 70 45 78 70 72 2c 20 45 50 5f 41 67 67 29 3b 0a  pExpr, EP_Agg);.
1bab3 20 20 7d 65 6c 73 65 20 69 66 28 20 73 61 76 65    }else if( save
1bab4 64 48 61 73 41 67 67 20 29 7b 0a 20 20 20 20 70  dHasAgg ){.    p
1bab5 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 31 3b 0a  NC->hasAgg = 1;.
1bab6 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 45 78 70    }.  return Exp
1bab7 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78  rHasProperty(pEx
1bab8 70 72 2c 20 45 50 5f 45 72 72 6f 72 29 3b 0a 7d  pr, EP_Error);.}
1bab9 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65  .../*.** Resolve
1baba 20 61 6c 6c 20 6e 61 6d 65 73 20 69 6e 20 61 6c   all names in al
1babb 6c 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66  l expressions of
1babc 20 61 20 53 45 4c 45 43 54 20 61 6e 64 20 69 6e   a SELECT and in
1babd 20 61 6c 6c 0a 2a 2a 20 64 65 63 65 6e 64 65 6e   all.** decenden
1babe 74 73 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  ts of the SELECT
1babf 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 6f 6d 70  , including comp
1bac0 6f 75 6e 64 73 20 6f 66 66 20 6f 66 20 70 2d 3e  ounds off of p->
1bac1 70 50 72 69 6f 72 2c 0a 2a 2a 20 73 75 62 71 75  pPrior,.** subqu
1bac2 65 72 69 65 73 20 69 6e 20 65 78 70 72 65 73 73  eries in express
1bac3 69 6f 6e 73 2c 20 61 6e 64 20 73 75 62 71 75 65  ions, and subque
1bac4 72 69 65 73 20 75 73 65 64 20 61 73 20 46 52 4f  ries used as FRO
1bac5 4d 20 63 6c 61 75 73 65 0a 2a 2a 20 74 65 72 6d  M clause.** term
1bac6 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 73 71 6c  s..**.** See sql
1bac7 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e  ite3ResolveExprN
1bac8 61 6d 65 73 28 29 20 66 6f 72 20 61 20 64 65 73  ames() for a des
1bac9 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20  cription of the 
1baca 6b 69 6e 64 73 20 6f 66 0a 2a 2a 20 74 72 61 6e  kinds of.** tran
1bacb 73 66 6f 72 6d 61 74 69 6f 6e 73 20 74 68 61 74  sformations that
1bacc 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a 2a 20 41 6c   occur..**.** Al
1bacd 6c 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  l SELECT stateme
1bace 6e 74 73 20 73 68 6f 75 6c 64 20 68 61 76 65 20  nts should have 
1bacf 62 65 65 6e 20 65 78 70 61 6e 64 65 64 20 75 73  been expanded us
1bad0 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 53 65  ing.** sqlite3Se
1bad1 6c 65 63 74 45 78 70 61 6e 64 28 29 20 70 72 69  lectExpand() pri
1bad2 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74  or to invoking t
1bad3 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a  his routine..*/.
1bad4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1bad5 6f 69 64 20 73 71 6c 69 74 65 33 52 65 73 6f 6c  oid sqlite3Resol
1bad6 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28 0a 20  veSelectNames(. 
1bad7 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
1bad8 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
1bad9 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
1bada 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20  .  Select *p,   
1badb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1badc 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1badd 74 20 62 65 69 6e 67 20 63 6f 64 65 64 2e 20 2a  t being coded. *
1bade 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  /.  NameContext 
1badf 2a 70 4f 75 74 65 72 4e 43 20 20 2f 2a 20 4e 61  *pOuterNC  /* Na
1bae0 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 70  me context for p
1bae1 61 72 65 6e 74 20 53 45 4c 45 43 54 20 73 74 61  arent SELECT sta
1bae2 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 57  tement */.){.  W
1bae3 61 6c 6b 65 72 20 77 3b 0a 0a 20 20 61 73 73 65  alker w;..  asse
1bae4 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 77 2e  rt( p!=0 );.  w.
1bae5 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20  xExprCallback = 
1bae6 72 65 73 6f 6c 76 65 45 78 70 72 53 74 65 70 3b  resolveExprStep;
1bae7 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c  .  w.xSelectCall
1bae8 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 53 65  back = resolveSe
1bae9 6c 65 63 74 53 74 65 70 3b 0a 20 20 77 2e 70 50  lectStep;.  w.pP
1baea 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20  arse = pParse;. 
1baeb 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4f 75 74 65   w.u.pNC = pOute
1baec 72 4e 43 3b 0a 20 20 73 71 6c 69 74 65 33 57 61  rNC;.  sqlite3Wa
1baed 6c 6b 53 65 6c 65 63 74 28 26 77 2c 20 70 29 3b  lkSelect(&w, p);
1baee 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
1baef 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 65 73 6f 6c  *** End of resol
1baf0 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve.c ***********
1baf1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1baf2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1baf3 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
1baf4 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 65  *** Begin file e
1baf5 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  xpr.c **********
1baf6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1baf7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1baf8 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
1baf9 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
1bafa 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
1bafb 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
1bafc 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
1bafd 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
1bafe 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
1baff 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
1bb00 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
1bb01 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
1bb02 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
1bb03 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
1bb04 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
1bb05 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
1bb06 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
1bb07 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
1bb08 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
1bb09 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
1bb0a 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
1bb0b 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
1bb0c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bb0d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bb0e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bb0f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
1bb10 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
1bb11 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 66   routines used f
1bb12 6f 72 20 61 6e 61 6c 79 7a 69 6e 67 20 65 78 70  or analyzing exp
1bb13 72 65 73 73 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20  ressions and.** 
1bb14 66 6f 72 20 67 65 6e 65 72 61 74 69 6e 67 20 56  for generating V
1bb15 44 42 45 20 63 6f 64 65 20 74 68 61 74 20 65 76  DBE code that ev
1bb16 61 6c 75 61 74 65 73 20 65 78 70 72 65 73 73 69  aluates expressi
1bb17 6f 6e 73 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a  ons in SQLite..*
1bb18 2a 0a 2a 2a 20 24 49 64 3a 20 65 78 70 72 2e 63  *.** $Id: expr.c
1bb19 2c 76 20 31 2e 34 30 31 20 32 30 30 38 2f 31 31  ,v 1.401 2008/11
1bb1a 2f 30 36 20 31 35 3a 33 33 3a 30 34 20 64 72 68  /06 15:33:04 drh
1bb1b 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   Exp $.*/../*.**
1bb1c 20 52 65 74 75 72 6e 20 74 68 65 20 27 61 66 66   Return the 'aff
1bb1d 69 6e 69 74 79 27 20 6f 66 20 74 68 65 20 65 78  inity' of the ex
1bb1e 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 69  pression pExpr i
1bb1f 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  f any..**.** If 
1bb20 70 45 78 70 72 20 69 73 20 61 20 63 6f 6c 75 6d  pExpr is a colum
1bb21 6e 2c 20 61 20 72 65 66 65 72 65 6e 63 65 20 74  n, a reference t
1bb22 6f 20 61 20 63 6f 6c 75 6d 6e 20 76 69 61 20 61  o a column via a
1bb23 6e 20 27 41 53 27 20 61 6c 69 61 73 2c 0a 2a 2a  n 'AS' alias,.**
1bb24 20 6f 72 20 61 20 73 75 62 2d 73 65 6c 65 63 74   or a sub-select
1bb25 20 77 69 74 68 20 61 20 63 6f 6c 75 6d 6e 20 61   with a column a
1bb26 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  s the return val
1bb27 75 65 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a 2a  ue, then the .**
1bb28 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 61   affinity of tha
1bb29 74 20 63 6f 6c 75 6d 6e 20 69 73 20 72 65 74 75  t column is retu
1bb2a 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c  rned. Otherwise,
1bb2b 20 30 78 30 30 20 69 73 20 72 65 74 75 72 6e 65   0x00 is returne
1bb2c 64 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67  d,.** indicating
1bb2d 20 6e 6f 20 61 66 66 69 6e 69 74 79 20 66 6f 72   no affinity for
1bb2e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e   the expression.
1bb2f 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 20  .**.** i.e. the 
1bb30 57 48 45 52 45 20 63 6c 61 75 73 65 20 65 78 70  WHERE clause exp
1bb31 72 65 73 73 73 69 6f 6e 73 20 69 6e 20 74 68 65  resssions in the
1bb32 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65   following state
1bb33 6d 65 6e 74 73 20 61 6c 6c 0a 2a 2a 20 68 61 76  ments all.** hav
1bb34 65 20 61 6e 20 61 66 66 69 6e 69 74 79 3a 0a 2a  e an affinity:.*
1bb35 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c  *.** CREATE TABL
1bb36 45 20 74 31 28 61 29 3b 0a 2a 2a 20 53 45 4c 45  E t1(a);.** SELE
1bb37 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45  CT * FROM t1 WHE
1bb38 52 45 20 61 3b 0a 2a 2a 20 53 45 4c 45 43 54 20  RE a;.** SELECT 
1bb39 61 20 41 53 20 62 20 46 52 4f 4d 20 74 31 20 57  a AS b FROM t1 W
1bb3a 48 45 52 45 20 62 3b 0a 2a 2a 20 53 45 4c 45 43  HERE b;.** SELEC
1bb3b 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 52  T * FROM t1 WHER
1bb3c 45 20 28 73 65 6c 65 63 74 20 61 20 66 72 6f 6d  E (select a from
1bb3d 20 74 31 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f   t1);.*/.SQLITE_
1bb3e 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c  PRIVATE char sql
1bb3f 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79  ite3ExprAffinity
1bb40 28 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20  (Expr *pExpr){. 
1bb41 20 69 6e 74 20 6f 70 20 3d 20 70 45 78 70 72 2d   int op = pExpr-
1bb42 3e 6f 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54  >op;.  if( op==T
1bb43 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20 20 20  K_SELECT ){.    
1bb44 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 45 78  return sqlite3Ex
1bb45 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72  prAffinity(pExpr
1bb46 2d 3e 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73  ->pSelect->pELis
1bb47 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a  t->a[0].pExpr);.
1bb48 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
1bb49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20 69  TE_OMIT_CAST.  i
1bb4a 66 28 20 6f 70 3d 3d 54 4b 5f 43 41 53 54 20 29  f( op==TK_CAST )
1bb4b 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c  {.    return sql
1bb4c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65  ite3AffinityType
1bb4d 28 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 29 3b  (&pExpr->token);
1bb4e 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66  .  }.#endif.  if
1bb4f 28 20 28 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e  ( (op==TK_COLUMN
1bb50 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53   || op==TK_REGIS
1bb51 54 45 52 29 20 26 26 20 70 45 78 70 72 2d 3e 70  TER) && pExpr->p
1bb52 54 61 62 21 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  Tab!=0 ){.    /*
1bb53 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52   op==TK_REGISTER
1bb54 20 26 26 20 70 45 78 70 72 2d 3e 70 54 61 62 21   && pExpr->pTab!
1bb55 3d 30 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20  =0 happens when 
1bb56 70 45 78 70 72 20 77 61 73 20 6f 72 69 67 69 6e  pExpr was origin
1bb57 61 6c 6c 79 0a 20 20 20 20 2a 2a 20 61 20 54 4b  ally.    ** a TK
1bb58 5f 43 4f 4c 55 4d 4e 20 62 75 74 20 77 61 73 20  _COLUMN but was 
1bb59 70 72 65 76 69 6f 75 73 6c 79 20 65 76 61 6c 75  previously evalu
1bb5a 61 74 65 64 20 61 6e 64 20 63 61 63 68 65 64 20  ated and cached 
1bb5b 69 6e 20 61 20 72 65 67 69 73 74 65 72 20 2a 2f  in a register */
1bb5c 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 45 78  .    int j = pEx
1bb5d 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20  pr->iColumn;.   
1bb5e 20 69 66 28 20 6a 3c 30 20 29 20 72 65 74 75 72   if( j<0 ) retur
1bb5f 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54  n SQLITE_AFF_INT
1bb60 45 47 45 52 3b 0a 20 20 20 20 61 73 73 65 72 74  EGER;.    assert
1bb61 28 20 70 45 78 70 72 2d 3e 70 54 61 62 20 26 26  ( pExpr->pTab &&
1bb62 20 6a 3c 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e   j<pExpr->pTab->
1bb63 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 72 65 74 75  nCol );.    retu
1bb64 72 6e 20 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e  rn pExpr->pTab->
1bb65 61 43 6f 6c 5b 6a 5d 2e 61 66 66 69 6e 69 74 79  aCol[j].affinity
1bb66 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
1bb67 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3b 0a  Expr->affinity;.
1bb68 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  }../*.** Set the
1bb69 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
1bb6a 6e 63 65 20 66 6f 72 20 65 78 70 72 65 73 73 69  nce for expressi
1bb6b 6f 6e 20 70 45 78 70 72 20 74 6f 20 62 65 20 74  on pExpr to be t
1bb6c 68 65 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20  he collating.** 
1bb6d 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 64 20 62  sequence named b
1bb6e 79 20 70 54 6f 6b 65 6e 2e 20 20 20 52 65 74 75  y pToken.   Retu
1bb6f 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
1bb70 74 68 65 20 72 65 76 69 73 65 64 20 65 78 70 72  the revised expr
1bb71 65 73 73 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 63  ession..** The c
1bb72 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
1bb73 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 22  e is marked as "
1bb74 65 78 70 6c 69 63 69 74 22 20 75 73 69 6e 67 20  explicit" using 
1bb75 74 68 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74  the EP_ExpCollat
1bb76 65 0a 2a 2a 20 66 6c 61 67 2e 20 20 41 6e 20 65  e.** flag.  An e
1bb77 78 70 6c 69 63 69 74 20 63 6f 6c 6c 61 74 69 6e  xplicit collatin
1bb78 67 20 73 65 71 75 65 6e 63 65 20 77 69 6c 6c 20  g sequence will 
1bb79 6f 76 65 72 72 69 64 65 20 69 6d 70 6c 69 63 69  override implici
1bb7a 74 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73  t.** collating s
1bb7b 65 71 75 65 6e 63 65 73 2e 0a 2a 2f 0a 53 51 4c  equences..*/.SQL
1bb7c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
1bb7d 20 2a 73 71 6c 69 74 65 33 45 78 70 72 53 65 74   *sqlite3ExprSet
1bb7e 43 6f 6c 6c 28 50 61 72 73 65 20 2a 70 50 61 72  Coll(Parse *pPar
1bb7f 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c  se, Expr *pExpr,
1bb80 20 54 6f 6b 65 6e 20 2a 70 43 6f 6c 6c 4e 61 6d   Token *pCollNam
1bb81 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c  e){.  char *zCol
1bb82 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  l = 0;          
1bb83 20 20 2f 2a 20 44 65 71 75 6f 74 65 64 20 6e 61    /* Dequoted na
1bb84 6d 65 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20  me of collation 
1bb85 73 65 71 75 65 6e 63 65 20 2a 2f 0a 20 20 43 6f  sequence */.  Co
1bb86 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20  llSeq *pColl;.  
1bb87 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
1bb88 61 72 73 65 2d 3e 64 62 3b 0a 20 20 7a 43 6f 6c  arse->db;.  zCol
1bb89 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  l = sqlite3NameF
1bb8a 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 43 6f  romToken(db, pCo
1bb8b 6c 6c 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70  llName);.  if( p
1bb8c 45 78 70 72 20 26 26 20 7a 43 6f 6c 6c 20 29 7b  Expr && zColl ){
1bb8d 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c  .    pColl = sql
1bb8e 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65  ite3LocateCollSe
1bb8f 71 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 2c  q(pParse, zColl,
1bb90 20 2d 31 29 3b 0a 20 20 20 20 69 66 28 20 70 43   -1);.    if( pC
1bb91 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 45 78  oll ){.      pEx
1bb92 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c  pr->pColl = pCol
1bb93 6c 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e  l;.      pExpr->
1bb94 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43  flags |= EP_ExpC
1bb95 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 7d 0a 20 20  ollate;.    }.  
1bb96 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  }.  sqlite3DbFre
1bb97 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20  e(db, zColl);.  
1bb98 72 65 74 75 72 6e 20 70 45 78 70 72 3b 0a 7d 0a  return pExpr;.}.
1bb99 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
1bb9a 65 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74  e default collat
1bb9b 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 6f 72  ion sequence for
1bb9c 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
1bb9d 70 45 78 70 72 2e 20 49 66 0a 2a 2a 20 74 68 65  pExpr. If.** the
1bb9e 72 65 20 69 73 20 6e 6f 20 64 65 66 61 75 6c 74  re is no default
1bb9f 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 2c   collation type,
1bba0 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51   return 0..*/.SQ
1bba1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c  LITE_PRIVATE Col
1bba2 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 45 78 70  lSeq *sqlite3Exp
1bba3 72 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a  rCollSeq(Parse *
1bba4 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45  pParse, Expr *pE
1bba5 78 70 72 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20  xpr){.  CollSeq 
1bba6 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 45 78  *pColl = 0;.  Ex
1bba7 70 72 20 2a 70 20 3d 20 70 45 78 70 72 3b 0a 20  pr *p = pExpr;. 
1bba8 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20   while( p ){.   
1bba9 20 69 6e 74 20 6f 70 3b 0a 20 20 20 20 70 43 6f   int op;.    pCo
1bbaa 6c 6c 20 3d 20 70 2d 3e 70 43 6f 6c 6c 3b 0a 20  ll = p->pColl;. 
1bbab 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 20 62     if( pColl ) b
1bbac 72 65 61 6b 3b 0a 20 20 20 20 6f 70 20 3d 20 70  reak;.    op = p
1bbad 2d 3e 6f 70 3b 0a 20 20 20 20 69 66 28 20 28 6f  ->op;.    if( (o
1bbae 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20  p==TK_COLUMN || 
1bbaf 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 29  op==TK_REGISTER)
1bbb0 20 26 26 20 70 2d 3e 70 54 61 62 21 3d 30 20 29   && p->pTab!=0 )
1bbb1 7b 0a 20 20 20 20 20 20 2f 2a 20 6f 70 3d 3d 54  {.      /* op==T
1bbb2 4b 5f 52 45 47 49 53 54 45 52 20 26 26 20 70 2d  K_REGISTER && p-
1bbb3 3e 70 54 61 62 21 3d 30 20 68 61 70 70 65 6e 73  >pTab!=0 happens
1bbb4 20 77 68 65 6e 20 70 45 78 70 72 20 77 61 73 20   when pExpr was 
1bbb5 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20 20  originally.     
1bbb6 20 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20   ** a TK_COLUMN 
1bbb7 62 75 74 20 77 61 73 20 70 72 65 76 69 6f 75 73  but was previous
1bbb8 6c 79 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64  ly evaluated and
1bbb9 20 63 61 63 68 65 64 20 69 6e 20 61 20 72 65 67   cached in a reg
1bbba 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 63  ister */.      c
1bbbb 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c  onst char *zColl
1bbbc 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 20 3d 20  ;.      int j = 
1bbbd 70 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  p->iColumn;.    
1bbbe 20 20 69 66 28 20 6a 3e 3d 30 20 29 7b 0a 20 20    if( j>=0 ){.  
1bbbf 20 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64        sqlite3 *d
1bbc0 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
1bbc1 20 20 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20          zColl = 
1bbc2 70 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d  p->pTab->aCol[j]
1bbc3 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20  .zColl;.        
1bbc4 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46  pColl = sqlite3F
1bbc5 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45  indCollSeq(db, E
1bbc6 4e 43 28 64 62 29 2c 20 7a 43 6f 6c 6c 2c 20 2d  NC(db), zColl, -
1bbc7 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70  1, 0);.        p
1bbc8 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43  Expr->pColl = pC
1bbc9 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  oll;.      }.   
1bbca 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1bbcb 20 20 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f 43      if( op!=TK_C
1bbcc 41 53 54 20 26 26 20 6f 70 21 3d 54 4b 5f 55 50  AST && op!=TK_UP
1bbcd 4c 55 53 20 29 7b 0a 20 20 20 20 20 20 62 72 65  LUS ){.      bre
1bbce 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20  ak;.    }.    p 
1bbcf 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 7d 0a  = p->pLeft;.  }.
1bbd0 20 20 69 66 28 20 73 71 6c 69 74 65 33 43 68 65    if( sqlite3Che
1bbd1 63 6b 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  ckCollSeq(pParse
1bbd2 2c 20 70 43 6f 6c 6c 29 20 29 7b 20 0a 20 20 20  , pColl) ){ .   
1bbd3 20 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 7d 0a   pColl = 0;.  }.
1bbd4 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a    return pColl;.
1bbd5 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69  }../*.** pExpr i
1bbd6 73 20 61 6e 20 6f 70 65 72 61 6e 64 20 6f 66 20  s an operand of 
1bbd7 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65  a comparison ope
1bbd8 72 61 74 6f 72 2e 20 20 61 66 66 32 20 69 73 20  rator.  aff2 is 
1bbd9 74 68 65 0a 2a 2a 20 74 79 70 65 20 61 66 66 69  the.** type affi
1bbda 6e 69 74 79 20 6f 66 20 74 68 65 20 6f 74 68 65  nity of the othe
1bbdb 72 20 6f 70 65 72 61 6e 64 2e 20 20 54 68 69 73  r operand.  This
1bbdc 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
1bbdd 20 74 68 65 0a 2a 2a 20 74 79 70 65 20 61 66 66   the.** type aff
1bbde 69 6e 69 74 79 20 74 68 61 74 20 73 68 6f 75 6c  inity that shoul
1bbdf 64 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68  d be used for th
1bbe0 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65  e comparison ope
1bbe1 72 61 74 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  rator..*/.SQLITE
1bbe2 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71  _PRIVATE char sq
1bbe3 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69  lite3CompareAffi
1bbe4 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72  nity(Expr *pExpr
1bbe5 2c 20 63 68 61 72 20 61 66 66 32 29 7b 0a 20 20  , char aff2){.  
1bbe6 63 68 61 72 20 61 66 66 31 20 3d 20 73 71 6c 69  char aff1 = sqli
1bbe7 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28  te3ExprAffinity(
1bbe8 70 45 78 70 72 29 3b 0a 20 20 69 66 28 20 61 66  pExpr);.  if( af
1bbe9 66 31 20 26 26 20 61 66 66 32 20 29 7b 0a 20 20  f1 && aff2 ){.  
1bbea 20 20 2f 2a 20 42 6f 74 68 20 73 69 64 65 73 20    /* Both sides 
1bbeb 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  of the compariso
1bbec 6e 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2e 20 49  n are columns. I
1bbed 66 20 6f 6e 65 20 68 61 73 20 6e 75 6d 65 72 69  f one has numeri
1bbee 63 0a 20 20 20 20 2a 2a 20 61 66 66 69 6e 69 74  c.    ** affinit
1bbef 79 2c 20 75 73 65 20 74 68 61 74 2e 20 4f 74 68  y, use that. Oth
1bbf0 65 72 77 69 73 65 20 75 73 65 20 6e 6f 20 61 66  erwise use no af
1bbf1 66 69 6e 69 74 79 2e 0a 20 20 20 20 2a 2f 0a 20  finity..    */. 
1bbf2 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73     if( sqlite3Is
1bbf3 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28  NumericAffinity(
1bbf4 61 66 66 31 29 20 7c 7c 20 73 71 6c 69 74 65 33  aff1) || sqlite3
1bbf5 49 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74  IsNumericAffinit
1bbf6 79 28 61 66 66 32 29 20 29 7b 0a 20 20 20 20 20  y(aff2) ){.     
1bbf7 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41   return SQLITE_A
1bbf8 46 46 5f 4e 55 4d 45 52 49 43 3b 0a 20 20 20 20  FF_NUMERIC;.    
1bbf9 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74  }else{.      ret
1bbfa 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  urn SQLITE_AFF_N
1bbfb 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  ONE;.    }.  }el
1bbfc 73 65 20 69 66 28 20 21 61 66 66 31 20 26 26 20  se if( !aff1 && 
1bbfd 21 61 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a 20  !aff2 ){.    /* 
1bbfe 4e 65 69 74 68 65 72 20 73 69 64 65 20 6f 66 20  Neither side of 
1bbff 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69  the comparison i
1bc00 73 20 61 20 63 6f 6c 75 6d 6e 2e 20 20 43 6f 6d  s a column.  Com
1bc01 70 61 72 65 20 74 68 65 0a 20 20 20 20 2a 2a 20  pare the.    ** 
1bc02 72 65 73 75 6c 74 73 20 64 69 72 65 63 74 6c 79  results directly
1bc03 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 74  ..    */.    ret
1bc04 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  urn SQLITE_AFF_N
1bc05 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ONE;.  }else{.  
1bc06 20 20 2f 2a 20 4f 6e 65 20 73 69 64 65 20 69 73    /* One side is
1bc07 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 20 6f   a column, the o
1bc08 74 68 65 72 20 69 73 20 6e 6f 74 2e 20 55 73 65  ther is not. Use
1bc09 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 61 66 66   the columns aff
1bc0a 69 6e 69 74 79 2e 20 2a 2f 0a 20 20 20 20 61 73  inity. */.    as
1bc0b 73 65 72 74 28 20 61 66 66 31 3d 3d 30 20 7c 7c  sert( aff1==0 ||
1bc0c 20 61 66 66 32 3d 3d 30 20 29 3b 0a 20 20 20 20   aff2==0 );.    
1bc0d 72 65 74 75 72 6e 20 28 61 66 66 31 20 2b 20 61  return (aff1 + a
1bc0e 66 66 32 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ff2);.  }.}../*.
1bc0f 2a 2a 20 70 45 78 70 72 20 69 73 20 61 20 63 6f  ** pExpr is a co
1bc10 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f  mparison operato
1bc11 72 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 74  r.  Return the t
1bc12 79 70 65 20 61 66 66 69 6e 69 74 79 20 74 68 61  ype affinity tha
1bc13 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 61  t should.** be a
1bc14 70 70 6c 69 65 64 20 74 6f 20 62 6f 74 68 20 6f  pplied to both o
1bc15 70 65 72 61 6e 64 73 20 70 72 69 6f 72 20 74 6f  perands prior to
1bc16 20 64 6f 69 6e 67 20 74 68 65 20 63 6f 6d 70 61   doing the compa
1bc17 72 69 73 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  rison..*/.static
1bc18 20 63 68 61 72 20 63 6f 6d 70 61 72 69 73 6f 6e   char comparison
1bc19 41 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70  Affinity(Expr *p
1bc1a 45 78 70 72 29 7b 0a 20 20 63 68 61 72 20 61 66  Expr){.  char af
1bc1b 66 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78  f;.  assert( pEx
1bc1c 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 51 20 7c 7c  pr->op==TK_EQ ||
1bc1d 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49   pExpr->op==TK_I
1bc1e 4e 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d  N || pExpr->op==
1bc1f 54 4b 5f 4c 54 20 7c 7c 0a 20 20 20 20 20 20 20  TK_LT ||.       
1bc20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b     pExpr->op==TK
1bc21 5f 47 54 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70  _GT || pExpr->op
1bc22 3d 3d 54 4b 5f 47 45 20 7c 7c 20 70 45 78 70 72  ==TK_GE || pExpr
1bc23 2d 3e 6f 70 3d 3d 54 4b 5f 4c 45 20 7c 7c 0a 20  ->op==TK_LE ||. 
1bc24 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e           pExpr->
1bc25 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 61  op==TK_NE );.  a
1bc26 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 4c  ssert( pExpr->pL
1bc27 65 66 74 20 29 3b 0a 20 20 61 66 66 20 3d 20 73  eft );.  aff = s
1bc28 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69  qlite3ExprAffini
1bc29 74 79 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 29  ty(pExpr->pLeft)
1bc2a 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 70  ;.  if( pExpr->p
1bc2b 52 69 67 68 74 20 29 7b 0a 20 20 20 20 61 66 66  Right ){.    aff
1bc2c 20 3d 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72   = sqlite3Compar
1bc2d 65 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d  eAffinity(pExpr-
1bc2e 3e 70 52 69 67 68 74 2c 20 61 66 66 29 3b 0a 20  >pRight, aff);. 
1bc2f 20 7d 0a 20 20 65 6c 73 65 20 69 66 28 20 70 45   }.  else if( pE
1bc30 78 70 72 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a  xpr->pSelect ){.
1bc31 20 20 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65      aff = sqlite
1bc32 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79  3CompareAffinity
1bc33 28 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 2d  (pExpr->pSelect-
1bc34 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  >pEList->a[0].pE
1bc35 78 70 72 2c 20 61 66 66 29 3b 0a 20 20 7d 0a 20  xpr, aff);.  }. 
1bc36 20 65 6c 73 65 20 69 66 28 20 21 61 66 66 20 29   else if( !aff )
1bc37 7b 0a 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49  {.    aff = SQLI
1bc38 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d  TE_AFF_NONE;.  }
1bc39 0a 20 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d  .  return aff;.}
1bc3a 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73  ../*.** pExpr is
1bc3b 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 65 78   a comparison ex
1bc3c 70 72 65 73 73 69 6f 6e 2c 20 65 67 2e 20 27 3d  pression, eg. '=
1bc3d 27 2c 20 27 3c 27 2c 20 49 4e 28 2e 2e 2e 29 20  ', '<', IN(...) 
1bc3e 65 74 63 2e 0a 2a 2a 20 69 64 78 5f 61 66 66 69  etc..** idx_affi
1bc3f 6e 69 74 79 20 69 73 20 74 68 65 20 61 66 66 69  nity is the affi
1bc40 6e 69 74 79 20 6f 66 20 61 6e 20 69 6e 64 65 78  nity of an index
1bc41 65 64 20 63 6f 6c 75 6d 6e 2e 20 52 65 74 75 72  ed column. Retur
1bc42 6e 20 74 72 75 65 0a 2a 2a 20 69 66 20 74 68 65  n true.** if the
1bc43 20 69 6e 64 65 78 20 77 69 74 68 20 61 66 66 69   index with affi
1bc44 6e 69 74 79 20 69 64 78 5f 61 66 66 69 6e 69 74  nity idx_affinit
1bc45 79 20 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f  y may be used to
1bc46 20 69 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 74 68   implement.** th
1bc47 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 20  e comparison in 
1bc48 70 45 78 70 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  pExpr..*/.SQLITE
1bc49 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1bc4a 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74  ite3IndexAffinit
1bc4b 79 4f 6b 28 45 78 70 72 20 2a 70 45 78 70 72 2c  yOk(Expr *pExpr,
1bc4c 20 63 68 61 72 20 69 64 78 5f 61 66 66 69 6e 69   char idx_affini
1bc4d 74 79 29 7b 0a 20 20 63 68 61 72 20 61 66 66 20  ty){.  char aff 
1bc4e 3d 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69  = comparisonAffi
1bc4f 6e 69 74 79 28 70 45 78 70 72 29 3b 0a 20 20 73  nity(pExpr);.  s
1bc50 77 69 74 63 68 28 20 61 66 66 20 29 7b 0a 20 20  witch( aff ){.  
1bc51 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 46    case SQLITE_AF
1bc52 46 5f 4e 4f 4e 45 3a 0a 20 20 20 20 20 20 72 65  F_NONE:.      re
1bc53 74 75 72 6e 20 31 3b 0a 20 20 20 20 63 61 73 65  turn 1;.    case
1bc54 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
1bc55 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  :.      return i
1bc56 64 78 5f 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c  dx_affinity==SQL
1bc57 49 54 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20  ITE_AFF_TEXT;.  
1bc58 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20    default:.     
1bc59 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 49   return sqlite3I
1bc5a 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79  sNumericAffinity
1bc5b 28 69 64 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a  (idx_affinity);.
1bc5c 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74    }.}../*.** Ret
1bc5d 75 72 6e 20 74 68 65 20 50 35 20 76 61 6c 75 65  urn the P5 value
1bc5e 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20   that should be 
1bc5f 75 73 65 64 20 66 6f 72 20 61 20 62 69 6e 61 72  used for a binar
1bc60 79 20 63 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20  y comparison.** 
1bc61 6f 70 63 6f 64 65 20 28 4f 50 5f 45 71 2c 20 4f  opcode (OP_Eq, O
1bc62 50 5f 47 65 20 65 74 63 2e 29 20 75 73 65 64 20  P_Ge etc.) used 
1bc63 74 6f 20 63 6f 6d 70 61 72 65 20 70 45 78 70 72  to compare pExpr
1bc64 31 20 61 6e 64 20 70 45 78 70 72 32 2e 0a 2a 2f  1 and pExpr2..*/
1bc65 0a 73 74 61 74 69 63 20 75 38 20 62 69 6e 61 72  .static u8 binar
1bc66 79 43 6f 6d 70 61 72 65 50 35 28 45 78 70 72 20  yCompareP5(Expr 
1bc67 2a 70 45 78 70 72 31 2c 20 45 78 70 72 20 2a 70  *pExpr1, Expr *p
1bc68 45 78 70 72 32 2c 20 69 6e 74 20 6a 75 6d 70 49  Expr2, int jumpI
1bc69 66 4e 75 6c 6c 29 7b 0a 20 20 75 38 20 61 66 66  fNull){.  u8 aff
1bc6a 20 3d 20 28 63 68 61 72 29 73 71 6c 69 74 65 33   = (char)sqlite3
1bc6b 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 45 78  ExprAffinity(pEx
1bc6c 70 72 32 29 3b 0a 20 20 61 66 66 20 3d 20 73 71  pr2);.  aff = sq
1bc6d 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69  lite3CompareAffi
1bc6e 6e 69 74 79 28 70 45 78 70 72 31 2c 20 61 66 66  nity(pExpr1, aff
1bc6f 29 20 7c 20 6a 75 6d 70 49 66 4e 75 6c 6c 3b 0a  ) | jumpIfNull;.
1bc70 20 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a    return aff;.}.
1bc71 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
1bc72 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63  pointer to the c
1bc73 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
1bc74 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65  e that should be
1bc75 20 75 73 65 64 20 62 79 0a 2a 2a 20 61 20 62 69   used by.** a bi
1bc76 6e 61 72 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20  nary comparison 
1bc77 6f 70 65 72 61 74 6f 72 20 63 6f 6d 70 61 72 69  operator compari
1bc78 6e 67 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69  ng pLeft and pRi
1bc79 67 68 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ght..**.** If th
1bc7a 65 20 6c 65 66 74 20 68 61 6e 64 20 65 78 70 72  e left hand expr
1bc7b 65 73 73 69 6f 6e 20 68 61 73 20 61 20 63 6f 6c  ession has a col
1bc7c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
1bc7d 74 79 70 65 2c 20 74 68 65 6e 20 69 74 20 69 73  type, then it is
1bc7e 0a 2a 2a 20 75 73 65 64 2e 20 4f 74 68 65 72 77  .** used. Otherw
1bc7f 69 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f  ise the collatio
1bc80 6e 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74  n sequence for t
1bc81 68 65 20 72 69 67 68 74 20 68 61 6e 64 20 65 78  he right hand ex
1bc82 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 75  pression.** is u
1bc83 73 65 64 2c 20 6f 72 20 74 68 65 20 64 65 66 61  sed, or the defa
1bc84 75 6c 74 20 28 42 49 4e 41 52 59 29 20 69 66 20  ult (BINARY) if 
1bc85 6e 65 69 74 68 65 72 20 65 78 70 72 65 73 73 69  neither expressi
1bc86 6f 6e 20 68 61 73 20 61 20 63 6f 6c 6c 61 74 69  on has a collati
1bc87 6e 67 0a 2a 2a 20 74 79 70 65 2e 0a 2a 2a 0a 2a  ng.** type..**.*
1bc88 2a 20 41 72 67 75 6d 65 6e 74 20 70 52 69 67 68  * Argument pRigh
1bc89 74 20 28 62 75 74 20 6e 6f 74 20 70 4c 65 66 74  t (but not pLeft
1bc8a 29 20 6d 61 79 20 62 65 20 61 20 6e 75 6c 6c 20  ) may be a null 
1bc8b 70 6f 69 6e 74 65 72 2e 20 49 6e 20 74 68 69 73  pointer. In this
1bc8c 20 63 61 73 65 2c 0a 2a 2a 20 69 74 20 69 73 20   case,.** it is 
1bc8d 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65 64 2e 0a  not considered..
1bc8e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1bc8f 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74  E CollSeq *sqlit
1bc90 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43  e3BinaryCompareC
1bc91 6f 6c 6c 53 65 71 28 0a 20 20 50 61 72 73 65 20  ollSeq(.  Parse 
1bc92 2a 70 50 61 72 73 65 2c 20 0a 20 20 45 78 70 72  *pParse, .  Expr
1bc93 20 2a 70 4c 65 66 74 2c 20 0a 20 20 45 78 70 72   *pLeft, .  Expr
1bc94 20 2a 70 52 69 67 68 74 0a 29 7b 0a 20 20 43 6f   *pRight.){.  Co
1bc95 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20  llSeq *pColl;.  
1bc96 61 73 73 65 72 74 28 20 70 4c 65 66 74 20 29 3b  assert( pLeft );
1bc97 0a 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 66 6c  .  if( pLeft->fl
1bc98 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c  ags & EP_ExpColl
1bc99 61 74 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ate ){.    asser
1bc9a 74 28 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 20  t( pLeft->pColl 
1bc9b 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70  );.    pColl = p
1bc9c 4c 65 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d  Left->pColl;.  }
1bc9d 65 6c 73 65 20 69 66 28 20 70 52 69 67 68 74 20  else if( pRight 
1bc9e 26 26 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 73  && pRight->flags
1bc9f 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65   & EP_ExpCollate
1bca0 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1bca1 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 20 29 3b  pRight->pColl );
1bca2 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 52 69  .    pColl = pRi
1bca3 67 68 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d 65  ght->pColl;.  }e
1bca4 6c 73 65 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d  lse{.    pColl =
1bca5 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c   sqlite3ExprColl
1bca6 53 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66  Seq(pParse, pLef
1bca7 74 29 3b 0a 20 20 20 20 69 66 28 20 21 70 43 6f  t);.    if( !pCo
1bca8 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c  ll ){.      pCol
1bca9 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  l = sqlite3ExprC
1bcaa 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70  ollSeq(pParse, p
1bcab 52 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20  Right);.    }.  
1bcac 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c  }.  return pColl
1bcad 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ;.}../*.** Gener
1bcae 61 74 65 20 74 68 65 20 6f 70 65 72 61 6e 64 73  ate the operands
1bcaf 20 66 6f 72 20 61 20 63 6f 6d 70 61 72 69 73 6f   for a compariso
1bcb0 6e 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 65  n operation.  Be
1bcb1 66 6f 72 65 0a 2a 2a 20 67 65 6e 65 72 61 74 69  fore.** generati
1bcb2 6e 67 20 74 68 65 20 63 6f 64 65 20 66 6f 72 20  ng the code for 
1bcb3 65 61 63 68 20 6f 70 65 72 61 6e 64 2c 20 73 65  each operand, se
1bcb4 74 20 74 68 65 20 45 50 5f 41 6e 79 41 66 66 0a  t the EP_AnyAff.
1bcb5 2a 2a 20 66 6c 61 67 20 6f 6e 20 74 68 65 20 65  ** flag on the e
1bcb6 78 70 72 65 73 73 69 6f 6e 20 73 6f 20 74 68 61  xpression so tha
1bcb7 74 20 69 74 20 77 69 6c 6c 20 62 65 20 61 62 6c  t it will be abl
1bcb8 65 20 74 6f 20 75 73 65 64 20 61 0a 2a 2a 20 63  e to used a.** c
1bcb9 61 63 68 65 64 20 63 6f 6c 75 6d 6e 20 76 61 6c  ached column val
1bcba 75 65 20 74 68 61 74 20 68 61 73 20 70 72 65 76  ue that has prev
1bcbb 69 6f 75 73 6c 79 20 75 6e 64 65 72 67 6f 6e 65  iously undergone
1bcbc 20 61 6e 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20   an.** affinity 
1bcbd 63 68 61 6e 67 65 2e 0a 2a 2f 0a 73 74 61 74 69  change..*/.stati
1bcbe 63 20 76 6f 69 64 20 63 6f 64 65 43 6f 6d 70 61  c void codeCompa
1bcbf 72 65 4f 70 65 72 61 6e 64 73 28 0a 20 20 50 61  reOperands(.  Pa
1bcc0 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
1bcc1 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63  /* Parsing and c
1bcc2 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63  ode generating c
1bcc3 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72  ontext */.  Expr
1bcc4 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a   *pLeft,      /*
1bcc5 20 54 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e   The left operan
1bcc6 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 67  d */.  int *pReg
1bcc7 4c 65 66 74 2c 20 20 20 20 2f 2a 20 52 65 67 69  Left,    /* Regi
1bcc8 73 74 65 72 20 77 68 65 72 65 20 6c 65 66 74 20  ster where left 
1bcc9 6f 70 65 72 61 6e 64 20 69 73 20 73 74 6f 72 65  operand is store
1bcca 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 46 72 65  d */.  int *pFre
1bccb 65 4c 65 66 74 2c 20 20 20 2f 2a 20 46 72 65 65  eLeft,   /* Free
1bccc 20 74 68 69 73 20 72 65 67 69 73 74 65 72 20 77   this register w
1bccd 68 65 6e 20 64 6f 6e 65 20 2a 2f 0a 20 20 45 78  hen done */.  Ex
1bcce 70 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20 20  pr *pRight,     
1bccf 2f 2a 20 54 68 65 20 72 69 67 68 74 20 6f 70 65  /* The right ope
1bcd0 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  rand */.  int *p
1bcd1 52 65 67 52 69 67 68 74 2c 20 20 20 2f 2a 20 52  RegRight,   /* R
1bcd2 65 67 69 73 74 65 72 20 77 68 65 72 65 20 72 69  egister where ri
1bcd3 67 68 74 20 6f 70 65 72 61 6e 64 20 69 73 20 73  ght operand is s
1bcd4 74 6f 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a  tored */.  int *
1bcd5 70 46 72 65 65 52 69 67 68 74 20 20 20 2f 2a 20  pFreeRight   /* 
1bcd6 57 72 69 74 65 20 74 65 6d 70 20 72 65 67 69 73  Write temp regis
1bcd7 74 65 72 20 66 6f 72 20 72 69 67 68 74 20 6f 70  ter for right op
1bcd8 65 72 61 6e 64 20 74 68 65 72 65 20 2a 2f 0a 29  erand there */.)
1bcd9 7b 0a 20 20 77 68 69 6c 65 28 20 70 4c 65 66 74  {.  while( pLeft
1bcda 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29  ->op==TK_UPLUS )
1bcdb 20 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 2d 3e   pLeft = pLeft->
1bcdc 70 4c 65 66 74 3b 0a 20 20 70 4c 65 66 74 2d 3e  pLeft;.  pLeft->
1bcdd 66 6c 61 67 73 20 7c 3d 20 45 50 5f 41 6e 79 41  flags |= EP_AnyA
1bcde 66 66 3b 0a 20 20 2a 70 52 65 67 4c 65 66 74 20  ff;.  *pRegLeft 
1bcdf 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
1bce0 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 4c  eTemp(pParse, pL
1bce1 65 66 74 2c 20 70 46 72 65 65 4c 65 66 74 29 3b  eft, pFreeLeft);
1bce2 0a 20 20 77 68 69 6c 65 28 20 70 52 69 67 68 74  .  while( pRight
1bce3 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29  ->op==TK_UPLUS )
1bce4 20 70 52 69 67 68 74 20 3d 20 70 52 69 67 68 74   pRight = pRight
1bce5 2d 3e 70 4c 65 66 74 3b 0a 20 20 70 52 69 67 68  ->pLeft;.  pRigh
1bce6 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 41  t->flags |= EP_A
1bce7 6e 79 41 66 66 3b 0a 20 20 2a 70 52 65 67 52 69  nyAff;.  *pRegRi
1bce8 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ght = sqlite3Exp
1bce9 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65  rCodeTemp(pParse
1bcea 2c 20 70 52 69 67 68 74 2c 20 70 46 72 65 65 52  , pRight, pFreeR
1bceb 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ight);.}../*.** 
1bcec 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f  Generate code fo
1bced 72 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f  r a comparison o
1bcee 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74  perator..*/.stat
1bcef 69 63 20 69 6e 74 20 63 6f 64 65 43 6f 6d 70 61  ic int codeCompa
1bcf0 72 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  re(.  Parse *pPa
1bcf1 72 73 65 2c 20 20 20 20 2f 2a 20 54 68 65 20 70  rse,    /* The p
1bcf2 61 72 73 69 6e 67 20 28 61 6e 64 20 63 6f 64 65  arsing (and code
1bcf3 20 67 65 6e 65 72 61 74 69 6e 67 29 20 63 6f 6e   generating) con
1bcf4 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a  text */.  Expr *
1bcf5 70 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a 20 54  pLeft,      /* T
1bcf6 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20  he left operand 
1bcf7 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68  */.  Expr *pRigh
1bcf8 74 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69  t,     /* The ri
1bcf9 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  ght operand */. 
1bcfa 20 69 6e 74 20 6f 70 63 6f 64 65 2c 20 20 20 20   int opcode,    
1bcfb 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 61 72     /* The compar
1bcfc 69 73 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20  ison opcode */. 
1bcfd 20 69 6e 74 20 69 6e 31 2c 20 69 6e 74 20 69 6e   int in1, int in
1bcfe 32 2c 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68  2, /* Register h
1bcff 6f 6c 64 69 6e 67 20 6f 70 65 72 61 6e 64 73 20  olding operands 
1bd00 2a 2f 0a 20 20 69 6e 74 20 64 65 73 74 2c 20 20  */.  int dest,  
1bd01 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68         /* Jump h
1bd02 65 72 65 20 69 66 20 74 72 75 65 2e 20 20 2a 2f  ere if true.  */
1bd03 0a 20 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c  .  int jumpIfNul
1bd04 6c 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c  l    /* If true,
1bd05 20 6a 75 6d 70 20 69 66 20 65 69 74 68 65 72 20   jump if either 
1bd06 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20  operand is NULL 
1bd07 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 70 35 3b 0a  */.){.  int p5;.
1bd08 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 43 6f    int addr;.  Co
1bd09 6c 6c 53 65 71 20 2a 70 34 3b 0a 0a 20 20 70 34  llSeq *p4;..  p4
1bd0a 20 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79   = sqlite3Binary
1bd0b 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70  CompareCollSeq(p
1bd0c 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52  Parse, pLeft, pR
1bd0d 69 67 68 74 29 3b 0a 20 20 70 35 20 3d 20 62 69  ight);.  p5 = bi
1bd0e 6e 61 72 79 43 6f 6d 70 61 72 65 50 35 28 70 4c  naryCompareP5(pL
1bd0f 65 66 74 2c 20 70 52 69 67 68 74 2c 20 6a 75 6d  eft, pRight, jum
1bd10 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 61 64 64 72  pIfNull);.  addr
1bd11 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
1bd12 64 4f 70 34 28 70 50 61 72 73 65 2d 3e 70 56 64  dOp4(pParse->pVd
1bd13 62 65 2c 20 6f 70 63 6f 64 65 2c 20 69 6e 32 2c  be, opcode, in2,
1bd14 20 64 65 73 74 2c 20 69 6e 31 2c 0a 20 20 20 20   dest, in1,.    
1bd15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bd16 20 20 20 20 20 20 20 28 76 6f 69 64 2a 29 70 34         (void*)p4
1bd17 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20  , P4_COLLSEQ);. 
1bd18 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1bd19 67 65 50 35 28 70 50 61 72 73 65 2d 3e 70 56 64  geP5(pParse->pVd
1bd1a 62 65 2c 20 70 35 29 3b 0a 20 20 69 66 28 20 28  be, p5);.  if( (
1bd1b 70 35 20 26 20 53 51 4c 49 54 45 5f 41 46 46 5f  p5 & SQLITE_AFF_
1bd1c 4d 41 53 4b 29 21 3d 53 51 4c 49 54 45 5f 41 46  MASK)!=SQLITE_AF
1bd1d 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 73 71  F_NONE ){.    sq
1bd1e 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66  lite3ExprCacheAf
1bd1f 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61  finityChange(pPa
1bd20 72 73 65 2c 20 69 6e 31 2c 20 31 29 3b 0a 20 20  rse, in1, 1);.  
1bd21 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
1bd22 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
1bd23 28 70 50 61 72 73 65 2c 20 69 6e 32 2c 20 31 29  (pParse, in2, 1)
1bd24 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 61  ;.  }.  return a
1bd25 64 64 72 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49  ddr;.}..#if SQLI
1bd26 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54  TE_MAX_EXPR_DEPT
1bd27 48 3e 30 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  H>0./*.** Check 
1bd28 74 68 61 74 20 61 72 67 75 6d 65 6e 74 20 6e 48  that argument nH
1bd29 65 69 67 68 74 20 69 73 20 6c 65 73 73 20 74 68  eight is less th
1bd2a 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74  an or equal to t
1bd2b 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 65 78  he maximum.** ex
1bd2c 70 72 65 73 73 69 6f 6e 20 64 65 70 74 68 20 61  pression depth a
1bd2d 6c 6c 6f 77 65 64 2e 20 49 66 20 69 74 20 69 73  llowed. If it is
1bd2e 20 6e 6f 74 2c 20 6c 65 61 76 65 20 61 6e 20 65   not, leave an e
1bd2f 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 0a  rror message in.
1bd30 2a 2a 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51  ** pParse..*/.SQ
1bd31 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1bd32 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 65 63   sqlite3ExprChec
1bd33 6b 48 65 69 67 68 74 28 50 61 72 73 65 20 2a 70  kHeight(Parse *p
1bd34 50 61 72 73 65 2c 20 69 6e 74 20 6e 48 65 69 67  Parse, int nHeig
1bd35 68 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ht){.  int rc = 
1bd36 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74  SQLITE_OK;.  int
1bd37 20 6d 78 48 65 69 67 68 74 20 3d 20 70 50 61 72   mxHeight = pPar
1bd38 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  se->db->aLimit[S
1bd39 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52  QLITE_LIMIT_EXPR
1bd3a 5f 44 45 50 54 48 5d 3b 0a 20 20 69 66 28 20 6e  _DEPTH];.  if( n
1bd3b 48 65 69 67 68 74 3e 6d 78 48 65 69 67 68 74 20  Height>mxHeight 
1bd3c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
1bd3d 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a  rorMsg(pParse, .
1bd3e 20 20 20 20 20 20 20 22 45 78 70 72 65 73 73 69         "Expressi
1bd3f 6f 6e 20 74 72 65 65 20 69 73 20 74 6f 6f 20 6c  on tree is too l
1bd40 61 72 67 65 20 28 6d 61 78 69 6d 75 6d 20 64 65  arge (maximum de
1bd41 70 74 68 20 25 64 29 22 2c 20 6d 78 48 65 69 67  pth %d)", mxHeig
1bd42 68 74 0a 20 20 20 20 29 3b 0a 20 20 20 20 72 63  ht.    );.    rc
1bd43 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
1bd44 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1bd45 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c  ;.}../* The foll
1bd46 6f 77 69 6e 67 20 74 68 72 65 65 20 66 75 6e 63  owing three func
1bd47 74 69 6f 6e 73 2c 20 68 65 69 67 68 74 4f 66 45  tions, heightOfE
1bd48 78 70 72 28 29 2c 20 68 65 69 67 68 74 4f 66 45  xpr(), heightOfE
1bd49 78 70 72 4c 69 73 74 28 29 0a 2a 2a 20 61 6e 64  xprList().** and
1bd4a 20 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28   heightOfSelect(
1bd4b 29 2c 20 61 72 65 20 75 73 65 64 20 74 6f 20 64  ), are used to d
1bd4c 65 74 65 72 6d 69 6e 65 20 74 68 65 20 6d 61 78  etermine the max
1bd4d 69 6d 75 6d 20 68 65 69 67 68 74 0a 2a 2a 20 6f  imum height.** o
1bd4e 66 20 61 6e 79 20 65 78 70 72 65 73 73 69 6f 6e  f any expression
1bd4f 20 74 72 65 65 20 72 65 66 65 72 65 6e 63 65 64   tree referenced
1bd50 20 62 79 20 74 68 65 20 73 74 72 75 63 74 75 72   by the structur
1bd51 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 0a  e passed as the.
1bd52 2a 2a 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  ** first argumen
1bd53 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73  t..**.** If this
1bd54 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 20   maximum height 
1bd55 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
1bd56 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75  the current valu
1bd57 65 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20  e pointed.** to 
1bd58 62 79 20 70 6e 48 65 69 67 68 74 2c 20 74 68 65  by pnHeight, the
1bd59 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
1bd5a 72 2c 20 74 68 65 6e 20 73 65 74 20 2a 70 6e 48  r, then set *pnH
1bd5b 65 69 67 68 74 20 74 6f 20 74 68 61 74 0a 2a 2a  eight to that.**
1bd5c 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69   value..*/.stati
1bd5d 63 20 76 6f 69 64 20 68 65 69 67 68 74 4f 66 45  c void heightOfE
1bd5e 78 70 72 28 45 78 70 72 20 2a 70 2c 20 69 6e 74  xpr(Expr *p, int
1bd5f 20 2a 70 6e 48 65 69 67 68 74 29 7b 0a 20 20 69   *pnHeight){.  i
1bd60 66 28 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20  f( p ){.    if( 
1bd61 70 2d 3e 6e 48 65 69 67 68 74 3e 2a 70 6e 48 65  p->nHeight>*pnHe
1bd62 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 2a 70  ight ){.      *p
1bd63 6e 48 65 69 67 68 74 20 3d 20 70 2d 3e 6e 48 65  nHeight = p->nHe
1bd64 69 67 68 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ight;.    }.  }.
1bd65 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 65  }.static void he
1bd66 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 45  ightOfExprList(E
1bd67 78 70 72 4c 69 73 74 20 2a 70 2c 20 69 6e 74 20  xprList *p, int 
1bd68 2a 70 6e 48 65 69 67 68 74 29 7b 0a 20 20 69 66  *pnHeight){.  if
1bd69 28 20 70 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  ( p ){.    int i
1bd6a 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
1bd6b 3c 70 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  <p->nExpr; i++){
1bd6c 0a 20 20 20 20 20 20 68 65 69 67 68 74 4f 66 45  .      heightOfE
1bd6d 78 70 72 28 70 2d 3e 61 5b 69 5d 2e 70 45 78 70  xpr(p->a[i].pExp
1bd6e 72 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20  r, pnHeight);.  
1bd6f 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63    }.  }.}.static
1bd70 20 76 6f 69 64 20 68 65 69 67 68 74 4f 66 53 65   void heightOfSe
1bd71 6c 65 63 74 28 53 65 6c 65 63 74 20 2a 70 2c 20  lect(Select *p, 
1bd72 69 6e 74 20 2a 70 6e 48 65 69 67 68 74 29 7b 0a  int *pnHeight){.
1bd73 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 68    if( p ){.    h
1bd74 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70  eightOfExpr(p->p
1bd75 57 68 65 72 65 2c 20 70 6e 48 65 69 67 68 74 29  Where, pnHeight)
1bd76 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78  ;.    heightOfEx
1bd77 70 72 28 70 2d 3e 70 48 61 76 69 6e 67 2c 20 70  pr(p->pHaving, p
1bd78 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65  nHeight);.    he
1bd79 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 4c  ightOfExpr(p->pL
1bd7a 69 6d 69 74 2c 20 70 6e 48 65 69 67 68 74 29 3b  imit, pnHeight);
1bd7b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70  .    heightOfExp
1bd7c 72 28 70 2d 3e 70 4f 66 66 73 65 74 2c 20 70 6e  r(p->pOffset, pn
1bd7d 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69  Height);.    hei
1bd7e 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d  ghtOfExprList(p-
1bd7f 3e 70 45 4c 69 73 74 2c 20 70 6e 48 65 69 67 68  >pEList, pnHeigh
1bd80 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66  t);.    heightOf
1bd81 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 47 72 6f  ExprList(p->pGro
1bd82 75 70 42 79 2c 20 70 6e 48 65 69 67 68 74 29 3b  upBy, pnHeight);
1bd83 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70  .    heightOfExp
1bd84 72 4c 69 73 74 28 70 2d 3e 70 4f 72 64 65 72 42  rList(p->pOrderB
1bd85 79 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20  y, pnHeight);.  
1bd86 20 20 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74    heightOfSelect
1bd87 28 70 2d 3e 70 50 72 69 6f 72 2c 20 70 6e 48 65  (p->pPrior, pnHe
1bd88 69 67 68 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ight);.  }.}../*
1bd89 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72  .** Set the Expr
1bd8a 2e 6e 48 65 69 67 68 74 20 76 61 72 69 61 62 6c  .nHeight variabl
1bd8b 65 20 69 6e 20 74 68 65 20 73 74 72 75 63 74 75  e in the structu
1bd8c 72 65 20 70 61 73 73 65 64 20 61 73 20 61 6e 20  re passed as an 
1bd8d 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 20 41 6e  .** argument. An
1bd8e 20 65 78 70 72 65 73 73 69 6f 6e 20 77 69 74 68   expression with
1bd8f 20 6e 6f 20 63 68 69 6c 64 72 65 6e 2c 20 45 78   no children, Ex
1bd90 70 72 2e 70 4c 69 73 74 20 6f 72 20 0a 2a 2a 20  pr.pList or .** 
1bd91 45 78 70 72 2e 70 53 65 6c 65 63 74 20 6d 65 6d  Expr.pSelect mem
1bd92 62 65 72 20 68 61 73 20 61 20 68 65 69 67 68 74  ber has a height
1bd93 20 6f 66 20 31 2e 20 41 6e 79 20 6f 74 68 65 72   of 1. Any other
1bd94 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 68   expression.** h
1bd95 61 73 20 61 20 68 65 69 67 68 74 20 65 71 75 61  as a height equa
1bd96 6c 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d  l to the maximum
1bd97 20 68 65 69 67 68 74 20 6f 66 20 61 6e 79 20 6f   height of any o
1bd98 74 68 65 72 20 0a 2a 2a 20 72 65 66 65 72 65 6e  ther .** referen
1bd99 63 65 64 20 45 78 70 72 20 70 6c 75 73 20 6f 6e  ced Expr plus on
1bd9a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
1bd9b 64 20 65 78 70 72 53 65 74 48 65 69 67 68 74 28  d exprSetHeight(
1bd9c 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 6e 74 20  Expr *p){.  int 
1bd9d 6e 48 65 69 67 68 74 20 3d 20 30 3b 0a 20 20 68  nHeight = 0;.  h
1bd9e 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70  eightOfExpr(p->p
1bd9f 4c 65 66 74 2c 20 26 6e 48 65 69 67 68 74 29 3b  Left, &nHeight);
1bda0 0a 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28  .  heightOfExpr(
1bda1 70 2d 3e 70 52 69 67 68 74 2c 20 26 6e 48 65 69  p->pRight, &nHei
1bda2 67 68 74 29 3b 0a 20 20 68 65 69 67 68 74 4f 66  ght);.  heightOf
1bda3 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 4c 69 73  ExprList(p->pLis
1bda4 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20  t, &nHeight);.  
1bda5 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70  heightOfSelect(p
1bda6 2d 3e 70 53 65 6c 65 63 74 2c 20 26 6e 48 65 69  ->pSelect, &nHei
1bda7 67 68 74 29 3b 0a 20 20 70 2d 3e 6e 48 65 69 67  ght);.  p->nHeig
1bda8 68 74 20 3d 20 6e 48 65 69 67 68 74 20 2b 20 31  ht = nHeight + 1
1bda9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  ;.}../*.** Set t
1bdaa 68 65 20 45 78 70 72 2e 6e 48 65 69 67 68 74 20  he Expr.nHeight 
1bdab 76 61 72 69 61 62 6c 65 20 75 73 69 6e 67 20 74  variable using t
1bdac 68 65 20 65 78 70 72 53 65 74 48 65 69 67 68 74  he exprSetHeight
1bdad 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 49 66 0a  () function. If.
1bdae 2a 2a 20 74 68 65 20 68 65 69 67 68 74 20 69 73  ** the height is
1bdaf 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68   greater than th
1bdb0 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65  e maximum allowe
1bdb1 64 20 65 78 70 72 65 73 73 69 6f 6e 20 64 65 70  d expression dep
1bdb2 74 68 2c 0a 2a 2a 20 6c 65 61 76 65 20 61 6e 20  th,.** leave an 
1bdb3 65 72 72 6f 72 20 69 6e 20 70 50 61 72 73 65 2e  error in pParse.
1bdb4 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1bdb5 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
1bdb6 78 70 72 53 65 74 48 65 69 67 68 74 28 50 61 72  xprSetHeight(Par
1bdb7 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
1bdb8 20 2a 70 29 7b 0a 20 20 65 78 70 72 53 65 74 48   *p){.  exprSetH
1bdb9 65 69 67 68 74 28 70 29 3b 0a 20 20 73 71 6c 69  eight(p);.  sqli
1bdba 74 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67  te3ExprCheckHeig
1bdbb 68 74 28 70 50 61 72 73 65 2c 20 70 2d 3e 6e 48  ht(pParse, p->nH
1bdbc 65 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  eight);.}../*.**
1bdbd 20 52 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69   Return the maxi
1bdbe 6d 75 6d 20 68 65 69 67 68 74 20 6f 66 20 61 6e  mum height of an
1bdbf 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  y expression tre
1bdc0 65 20 72 65 66 65 72 65 6e 63 65 64 0a 2a 2a 20  e referenced.** 
1bdc1 62 79 20 74 68 65 20 73 65 6c 65 63 74 20 73 74  by the select st
1bdc2 61 74 65 6d 65 6e 74 20 70 61 73 73 65 64 20 61  atement passed a
1bdc3 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a  s an argument..*
1bdc4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1bdc5 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c 65   int sqlite3Sele
1bdc6 63 74 45 78 70 72 48 65 69 67 68 74 28 53 65 6c  ctExprHeight(Sel
1bdc7 65 63 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e  ect *p){.  int n
1bdc8 48 65 69 67 68 74 20 3d 20 30 3b 0a 20 20 68 65  Height = 0;.  he
1bdc9 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70 2c 20  ightOfSelect(p, 
1bdca 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 72 65 74  &nHeight);.  ret
1bdcb 75 72 6e 20 6e 48 65 69 67 68 74 3b 0a 7d 0a 23  urn nHeight;.}.#
1bdcc 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 65  else.  #define e
1bdcd 78 70 72 53 65 74 48 65 69 67 68 74 28 79 29 0a  xprSetHeight(y).
1bdce 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1bdcf 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e  _MAX_EXPR_DEPTH>
1bdd0 30 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73  0 */../*.** Cons
1bdd1 74 72 75 63 74 20 61 20 6e 65 77 20 65 78 70 72  truct a new expr
1bdd2 65 73 73 69 6f 6e 20 6e 6f 64 65 20 61 6e 64 20  ession node and 
1bdd3 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  return a pointer
1bdd4 20 74 6f 20 69 74 2e 20 20 4d 65 6d 6f 72 79 0a   to it.  Memory.
1bdd5 2a 2a 20 66 6f 72 20 74 68 69 73 20 6e 6f 64 65  ** for this node
1bdd6 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f   is obtained fro
1bdd7 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  m sqlite3_malloc
1bdd8 28 29 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67  ().  The calling
1bdd9 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20   function.** is 
1bdda 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
1bddb 6d 61 6b 69 6e 67 20 73 75 72 65 20 74 68 65 20  making sure the 
1bddc 6e 6f 64 65 20 65 76 65 6e 74 75 61 6c 6c 79 20  node eventually 
1bddd 67 65 74 73 20 66 72 65 65 64 2e 0a 2a 2f 0a 53  gets freed..*/.S
1bdde 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78  QLITE_PRIVATE Ex
1bddf 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 28  pr *sqlite3Expr(
1bde0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
1bde1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61             /* Ha
1bde2 6e 64 6c 65 20 66 6f 72 20 73 71 6c 69 74 65 33  ndle for sqlite3
1bde3 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 29 20 28  DbMallocZero() (
1bde4 6d 61 79 20 62 65 20 6e 75 6c 6c 29 20 2a 2f 0a  may be null) */.
1bde5 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20    int op,       
1bde6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70            /* Exp
1bde7 72 65 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a  ression opcode *
1bde8 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c  /.  Expr *pLeft,
1bde9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1bdea 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  eft operand */. 
1bdeb 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20 20   Expr *pRight,  
1bdec 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68           /* Righ
1bded 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 63  t operand */.  c
1bdee 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b  onst Token *pTok
1bdef 65 6e 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65  en     /* Argume
1bdf0 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20  nt token */.){. 
1bdf1 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 70   Expr *pNew;.  p
1bdf2 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  New = sqlite3DbM
1bdf3 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
1bdf4 7a 65 6f 66 28 45 78 70 72 29 29 3b 0a 20 20 69  zeof(Expr));.  i
1bdf5 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20  f( pNew==0 ){.  
1bdf6 20 20 2f 2a 20 57 68 65 6e 20 6d 61 6c 6c 6f 63    /* When malloc
1bdf7 20 66 61 69 6c 73 2c 20 64 65 6c 65 74 65 20 70   fails, delete p
1bdf8 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 2e  Left and pRight.
1bdf9 20 45 78 70 72 65 73 73 69 6f 6e 73 20 70 61 73   Expressions pas
1bdfa 73 65 64 20 74 6f 20 0a 20 20 20 20 2a 2a 20 74  sed to .    ** t
1bdfb 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73  his function mus
1bdfc 74 20 61 6c 77 61 79 73 20 62 65 20 61 6c 6c 6f  t always be allo
1bdfd 63 61 74 65 64 20 77 69 74 68 20 73 71 6c 69 74  cated with sqlit
1bdfe 65 33 45 78 70 72 28 29 20 66 6f 72 20 74 68 69  e3Expr() for thi
1bdff 73 20 0a 20 20 20 20 2a 2a 20 72 65 61 73 6f 6e  s .    ** reason
1be00 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  . .    */.    sq
1be01 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
1be02 64 62 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20  db, pLeft);.    
1be03 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
1be04 65 28 64 62 2c 20 70 52 69 67 68 74 29 3b 0a 20  e(db, pRight);. 
1be05 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
1be06 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 6f 70  .  pNew->op = op
1be07 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20  ;.  pNew->pLeft 
1be08 3d 20 70 4c 65 66 74 3b 0a 20 20 70 4e 65 77 2d  = pLeft;.  pNew-
1be09 3e 70 52 69 67 68 74 20 3d 20 70 52 69 67 68 74  >pRight = pRight
1be0a 3b 0a 20 20 70 4e 65 77 2d 3e 69 41 67 67 20 3d  ;.  pNew->iAgg =
1be0b 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 73 70 61   -1;.  pNew->spa
1be0c 6e 2e 7a 20 3d 20 28 75 38 2a 29 22 22 3b 0a 20  n.z = (u8*)"";. 
1be0d 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a 20   if( pToken ){. 
1be0e 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65     assert( pToke
1be0f 6e 2d 3e 64 79 6e 3d 3d 30 20 29 3b 0a 20 20 20  n->dyn==0 );.   
1be10 20 70 4e 65 77 2d 3e 73 70 61 6e 20 3d 20 70 4e   pNew->span = pN
1be11 65 77 2d 3e 74 6f 6b 65 6e 20 3d 20 2a 70 54 6f  ew->token = *pTo
1be12 6b 65 6e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ken;.  }else if(
1be13 20 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 69 66   pLeft ){.    if
1be14 28 20 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20  ( pRight ){.    
1be15 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 73 70    if( pRight->sp
1be16 61 6e 2e 64 79 6e 3d 3d 30 20 26 26 20 70 4c 65  an.dyn==0 && pLe
1be17 66 74 2d 3e 73 70 61 6e 2e 64 79 6e 3d 3d 30 20  ft->span.dyn==0 
1be18 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1be19 65 33 45 78 70 72 53 70 61 6e 28 70 4e 65 77 2c  e3ExprSpan(pNew,
1be1a 20 26 70 4c 65 66 74 2d 3e 73 70 61 6e 2c 20 26   &pLeft->span, &
1be1b 70 52 69 67 68 74 2d 3e 73 70 61 6e 29 3b 0a 20  pRight->span);. 
1be1c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
1be1d 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26   pRight->flags &
1be1e 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29   EP_ExpCollate )
1be1f 7b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e  {.        pNew->
1be20 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43  flags |= EP_ExpC
1be21 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20 20 20  ollate;.        
1be22 70 4e 65 77 2d 3e 70 43 6f 6c 6c 20 3d 20 70 52  pNew->pColl = pR
1be23 69 67 68 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20  ight->pColl;.   
1be24 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
1be25 66 28 20 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20  f( pLeft->flags 
1be26 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20  & EP_ExpCollate 
1be27 29 7b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 66  ){.      pNew->f
1be28 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f  lags |= EP_ExpCo
1be29 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20 70 4e 65  llate;.      pNe
1be2a 77 2d 3e 70 43 6f 6c 6c 20 3d 20 70 4c 65 66 74  w->pColl = pLeft
1be2b 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 7d 0a 20  ->pColl;.    }. 
1be2c 20 7d 0a 0a 20 20 65 78 70 72 53 65 74 48 65 69   }..  exprSetHei
1be2d 67 68 74 28 70 4e 65 77 29 3b 0a 20 20 72 65 74  ght(pNew);.  ret
1be2e 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a  urn pNew;.}../*.
1be2f 2a 2a 20 57 6f 72 6b 73 20 6c 69 6b 65 20 73 71  ** Works like sq
1be30 6c 69 74 65 33 45 78 70 72 28 29 20 65 78 63 65  lite3Expr() exce
1be31 70 74 20 74 68 61 74 20 69 74 20 74 61 6b 65 73  pt that it takes
1be32 20 61 6e 20 65 78 74 72 61 20 50 61 72 73 65 2a   an extra Parse*
1be33 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 61 6e 64  .** argument and
1be34 20 6e 6f 74 69 66 69 65 73 20 74 68 65 20 61 73   notifies the as
1be35 73 6f 63 69 61 74 65 64 20 63 6f 6e 6e 65 63 74  sociated connect
1be36 69 6f 6e 20 6f 62 6a 65 63 74 20 69 66 20 6d 61  ion object if ma
1be37 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53  lloc fails..*/.S
1be38 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78  QLITE_PRIVATE Ex
1be39 70 72 20 2a 73 71 6c 69 74 65 33 50 45 78 70 72  pr *sqlite3PExpr
1be3a 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
1be3b 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  e,          /* P
1be3c 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
1be3d 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20  /.  int op,     
1be3e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
1be3f 78 70 72 65 73 73 69 6f 6e 20 6f 70 63 6f 64 65  xpression opcode
1be40 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65 66   */.  Expr *pLef
1be41 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t,            /*
1be42 20 4c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f   Left operand */
1be43 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c  .  Expr *pRight,
1be44 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69             /* Ri
1be45 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  ght operand */. 
1be46 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 54   const Token *pT
1be47 6f 6b 65 6e 20 20 20 20 20 2f 2a 20 41 72 67 75  oken     /* Argu
1be48 6d 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b  ment token */.){
1be49 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 73 71 6c  .  Expr *p = sql
1be4a 69 74 65 33 45 78 70 72 28 70 50 61 72 73 65 2d  ite3Expr(pParse-
1be4b 3e 64 62 2c 20 6f 70 2c 20 70 4c 65 66 74 2c 20  >db, op, pLeft, 
1be4c 70 52 69 67 68 74 2c 20 70 54 6f 6b 65 6e 29 3b  pRight, pToken);
1be4d 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
1be4e 73 71 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b  sqlite3ExprCheck
1be4f 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 70  Height(pParse, p
1be50 2d 3e 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a  ->nHeight);.  }.
1be51 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
1be52 2a 0a 2a 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20  *.** When doing 
1be53 61 20 6e 65 73 74 65 64 20 70 61 72 73 65 2c 20  a nested parse, 
1be54 79 6f 75 20 63 61 6e 20 69 6e 63 6c 75 64 65 20  you can include 
1be55 74 65 72 6d 73 20 69 6e 20 61 6e 20 65 78 70 72  terms in an expr
1be56 65 73 73 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 6c  ession.** that l
1be57 6f 6f 6b 20 6c 69 6b 65 20 74 68 69 73 3a 20 20  ook like this:  
1be58 20 23 31 20 23 32 20 2e 2e 2e 20 20 54 68 65 73   #1 #2 ...  Thes
1be59 65 20 74 65 72 6d 73 20 72 65 66 65 72 20 74 6f  e terms refer to
1be5a 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 69 6e   registers.** in
1be5b 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63   the virtual mac
1be5c 68 69 6e 65 2e 20 20 23 4e 20 69 73 20 74 68 65  hine.  #N is the
1be5d 20 4e 2d 74 68 20 72 65 67 69 73 74 65 72 2e 0a   N-th register..
1be5e 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
1be5f 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  ne is called by 
1be60 74 68 65 20 70 61 72 73 65 72 20 74 6f 20 64 65  the parser to de
1be61 61 6c 20 77 69 74 68 20 6f 6e 20 6f 66 20 74 68  al with on of th
1be62 6f 73 65 20 74 65 72 6d 73 2e 0a 2a 2a 20 49 74  ose terms..** It
1be63 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 67 65 6e   immediately gen
1be64 65 72 61 74 65 73 20 63 6f 64 65 20 74 6f 20 73  erates code to s
1be65 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 20 69  tore the value i
1be66 6e 20 61 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74  n a memory locat
1be67 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75  ion..** The retu
1be68 72 6e 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f  rns an expressio
1be69 6e 20 74 68 61 74 20 77 69 6c 6c 20 63 6f 64 65  n that will code
1be6a 20 74 6f 20 65 78 74 72 61 63 74 20 74 68 65 20   to extract the 
1be6b 76 61 6c 75 65 20 66 72 6f 6d 0a 2a 2a 20 74 68  value from.** th
1be6c 61 74 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69  at memory locati
1be6d 6f 6e 20 61 73 20 6e 65 65 64 65 64 2e 0a 2a 2f  on as needed..*/
1be6e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1be6f 45 78 70 72 20 2a 73 71 6c 69 74 65 33 52 65 67  Expr *sqlite3Reg
1be70 69 73 74 65 72 45 78 70 72 28 50 61 72 73 65 20  isterExpr(Parse 
1be71 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a  *pParse, Token *
1be72 70 54 6f 6b 65 6e 29 7b 0a 20 20 56 64 62 65 20  pToken){.  Vdbe 
1be73 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
1be74 62 65 3b 0a 20 20 45 78 70 72 20 2a 70 3b 0a 20  be;.  Expr *p;. 
1be75 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73   if( pParse->nes
1be76 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  ted==0 ){.    sq
1be77 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1be78 61 72 73 65 2c 20 22 6e 65 61 72 20 5c 22 25 54  arse, "near \"%T
1be79 5c 22 3a 20 73 79 6e 74 61 78 20 65 72 72 6f 72  \": syntax error
1be7a 22 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 20 20  ", pToken);.    
1be7b 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 45  return sqlite3PE
1be7c 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e  xpr(pParse, TK_N
1be7d 55 4c 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  ULL, 0, 0, 0);. 
1be7e 20 7d 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20   }.  if( v==0 ) 
1be7f 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20  return 0;.  p = 
1be80 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
1be81 72 73 65 2c 20 54 4b 5f 52 45 47 49 53 54 45 52  rse, TK_REGISTER
1be82 2c 20 30 2c 20 30 2c 20 70 54 6f 6b 65 6e 29 3b  , 0, 0, pToken);
1be83 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20  .  if( p==0 ){. 
1be84 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a     return 0;  /*
1be85 20 4d 61 6c 6c 6f 63 20 66 61 69 6c 65 64 20 2a   Malloc failed *
1be86 2f 0a 20 20 7d 0a 20 20 70 2d 3e 69 54 61 62 6c  /.  }.  p->iTabl
1be87 65 20 3d 20 61 74 6f 69 28 28 63 68 61 72 2a 29  e = atoi((char*)
1be88 26 70 54 6f 6b 65 6e 2d 3e 7a 5b 31 5d 29 3b 0a  &pToken->z[1]);.
1be89 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
1be8a 2a 0a 2a 2a 20 4a 6f 69 6e 20 74 77 6f 20 65 78  *.** Join two ex
1be8b 70 72 65 73 73 69 6f 6e 73 20 75 73 69 6e 67 20  pressions using 
1be8c 61 6e 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e  an AND operator.
1be8d 20 20 49 66 20 65 69 74 68 65 72 20 65 78 70 72    If either expr
1be8e 65 73 73 69 6f 6e 20 69 73 0a 2a 2a 20 4e 55 4c  ession is.** NUL
1be8f 4c 2c 20 74 68 65 6e 20 6a 75 73 74 20 72 65 74  L, then just ret
1be90 75 72 6e 20 74 68 65 20 6f 74 68 65 72 20 65 78  urn the other ex
1be91 70 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  pression..*/.SQL
1be92 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
1be93 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64   *sqlite3ExprAnd
1be94 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78  (sqlite3 *db, Ex
1be95 70 72 20 2a 70 4c 65 66 74 2c 20 45 78 70 72 20  pr *pLeft, Expr 
1be96 2a 70 52 69 67 68 74 29 7b 0a 20 20 69 66 28 20  *pRight){.  if( 
1be97 70 4c 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20  pLeft==0 ){.    
1be98 72 65 74 75 72 6e 20 70 52 69 67 68 74 3b 0a 20  return pRight;. 
1be99 20 7d 65 6c 73 65 20 69 66 28 20 70 52 69 67 68   }else if( pRigh
1be9a 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  t==0 ){.    retu
1be9b 72 6e 20 70 4c 65 66 74 3b 0a 20 20 7d 65 6c 73  rn pLeft;.  }els
1be9c 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71  e{.    return sq
1be9d 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b  lite3Expr(db, TK
1be9e 5f 41 4e 44 2c 20 70 4c 65 66 74 2c 20 70 52 69  _AND, pLeft, pRi
1be9f 67 68 74 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a  ght, 0);.  }.}..
1bea0 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78  /*.** Set the Ex
1bea1 70 72 2e 73 70 61 6e 20 66 69 65 6c 64 20 6f 66  pr.span field of
1bea2 20 74 68 65 20 67 69 76 65 6e 20 65 78 70 72 65   the given expre
1bea3 73 73 69 6f 6e 20 74 6f 20 73 70 61 6e 20 61 6c  ssion to span al
1bea4 6c 0a 2a 2a 20 74 65 78 74 20 62 65 74 77 65 65  l.** text betwee
1bea5 6e 20 74 68 65 20 74 77 6f 20 67 69 76 65 6e 20  n the two given 
1bea6 74 6f 6b 65 6e 73 2e 20 20 42 6f 74 68 20 74 6f  tokens.  Both to
1bea7 6b 65 6e 73 20 6d 75 73 74 20 62 65 20 70 6f 69  kens must be poi
1bea8 6e 74 69 6e 67 0a 2a 2a 20 61 74 20 74 68 65 20  nting.** at the 
1bea9 73 61 6d 65 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a  same string..*/.
1beaa 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1beab 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 53  oid sqlite3ExprS
1beac 70 61 6e 28 45 78 70 72 20 2a 70 45 78 70 72 2c  pan(Expr *pExpr,
1bead 20 54 6f 6b 65 6e 20 2a 70 4c 65 66 74 2c 20 54   Token *pLeft, T
1beae 6f 6b 65 6e 20 2a 70 52 69 67 68 74 29 7b 0a 20  oken *pRight){. 
1beaf 20 61 73 73 65 72 74 28 20 70 52 69 67 68 74 21   assert( pRight!
1beb0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1beb1 70 4c 65 66 74 21 3d 30 20 29 3b 0a 20 20 69 66  pLeft!=0 );.  if
1beb2 28 20 70 45 78 70 72 20 29 7b 0a 20 20 20 20 70  ( pExpr ){.    p
1beb3 45 78 70 72 2d 3e 73 70 61 6e 2e 7a 20 3d 20 70  Expr->span.z = p
1beb4 4c 65 66 74 2d 3e 7a 3b 0a 20 20 20 20 70 45 78  Left->z;.    pEx
1beb5 70 72 2d 3e 73 70 61 6e 2e 6e 20 3d 20 70 52 69  pr->span.n = pRi
1beb6 67 68 74 2d 3e 6e 20 2b 20 28 70 52 69 67 68 74  ght->n + (pRight
1beb7 2d 3e 7a 20 2d 20 70 4c 65 66 74 2d 3e 7a 29 3b  ->z - pLeft->z);
1beb8 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  .  }.}../*.** Co
1beb9 6e 73 74 72 75 63 74 20 61 20 6e 65 77 20 65 78  nstruct a new ex
1beba 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f  pression node fo
1bebb 72 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74  r a function wit
1bebc 68 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 61 72  h multiple.** ar
1bebd 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49  guments..*/.SQLI
1bebe 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
1bebf 2a 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63  *sqlite3ExprFunc
1bec0 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72  tion(Parse *pPar
1bec1 73 65 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c  se, ExprList *pL
1bec2 69 73 74 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b  ist, Token *pTok
1bec3 65 6e 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e 65  en){.  Expr *pNe
1bec4 77 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  w;.  sqlite3 *db
1bec5 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
1bec6 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 20   assert( pToken 
1bec7 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69  );.  pNew = sqli
1bec8 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
1bec9 64 62 2c 20 73 69 7a 65 6f 66 28 45 78 70 72 29  db, sizeof(Expr)
1beca 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d   );.  if( pNew==
1becb 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
1becc 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64  ExprListDelete(d
1becd 62 2c 20 70 4c 69 73 74 29 3b 20 2f 2a 20 41 76  b, pList); /* Av
1bece 6f 69 64 20 6c 65 61 6b 69 6e 67 20 6d 65 6d 6f  oid leaking memo
1becf 72 79 20 77 68 65 6e 20 6d 61 6c 6c 6f 63 20 66  ry when malloc f
1bed0 61 69 6c 73 20 2a 2f 0a 20 20 20 20 72 65 74 75  ails */.    retu
1bed1 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77  rn 0;.  }.  pNew
1bed2 2d 3e 6f 70 20 3d 20 54 4b 5f 46 55 4e 43 54 49  ->op = TK_FUNCTI
1bed3 4f 4e 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c 69 73  ON;.  pNew->pLis
1bed4 74 20 3d 20 70 4c 69 73 74 3b 0a 20 20 61 73 73  t = pList;.  ass
1bed5 65 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 64 79 6e  ert( pToken->dyn
1bed6 3d 3d 30 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 74  ==0 );.  pNew->t
1bed7 6f 6b 65 6e 20 3d 20 2a 70 54 6f 6b 65 6e 3b 0a  oken = *pToken;.
1bed8 20 20 70 4e 65 77 2d 3e 73 70 61 6e 20 3d 20 70    pNew->span = p
1bed9 4e 65 77 2d 3e 74 6f 6b 65 6e 3b 0a 0a 20 20 73  New->token;..  s
1beda 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69  qlite3ExprSetHei
1bedb 67 68 74 28 70 50 61 72 73 65 2c 20 70 4e 65 77  ght(pParse, pNew
1bedc 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77  );.  return pNew
1bedd 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 69 67  ;.}../*.** Assig
1bede 6e 20 61 20 76 61 72 69 61 62 6c 65 20 6e 75 6d  n a variable num
1bedf 62 65 72 20 74 6f 20 61 6e 20 65 78 70 72 65 73  ber to an expres
1bee0 73 69 6f 6e 20 74 68 61 74 20 65 6e 63 6f 64 65  sion that encode
1bee1 73 20 61 20 77 69 6c 64 63 61 72 64 0a 2a 2a 20  s a wildcard.** 
1bee2 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  in the original 
1bee3 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  SQL statement.  
1bee4 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73  .**.** Wildcards
1bee5 20 63 6f 6e 73 69 73 74 69 6e 67 20 6f 66 20 61   consisting of a
1bee6 20 73 69 6e 67 6c 65 20 22 3f 22 20 61 72 65 20   single "?" are 
1bee7 61 73 73 69 67 6e 65 64 20 74 68 65 20 6e 65 78  assigned the nex
1bee8 74 20 73 65 71 75 65 6e 74 69 61 6c 0a 2a 2a 20  t sequential.** 
1bee9 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 2e  variable number.
1beea 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73  .**.** Wildcards
1beeb 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 6e   of the form "?n
1beec 6e 6e 22 20 61 72 65 20 61 73 73 69 67 6e 65 64  nn" are assigned
1beed 20 74 68 65 20 6e 75 6d 62 65 72 20 22 6e 6e 6e   the number "nnn
1beee 22 2e 20 20 57 65 20 6d 61 6b 65 0a 2a 2a 20 73  ".  We make.** s
1beef 75 72 65 20 22 6e 6e 6e 22 20 69 73 20 6e 6f 74  ure "nnn" is not
1bef0 20 74 6f 6f 20 62 65 20 74 6f 20 61 76 6f 69 64   too be to avoid
1bef1 20 61 20 64 65 6e 69 61 6c 20 6f 66 20 73 65 72   a denial of ser
1bef2 76 69 63 65 20 61 74 74 61 63 6b 20 77 68 65 6e  vice attack when
1bef3 0a 2a 2a 20 74 68 65 20 53 51 4c 20 73 74 61 74  .** the SQL stat
1bef4 65 6d 65 6e 74 20 63 6f 6d 65 73 20 66 72 6f 6d  ement comes from
1bef5 20 61 6e 20 65 78 74 65 72 6e 61 6c 20 73 6f 75   an external sou
1bef6 72 63 65 2e 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63  rce..**.** Wildc
1bef7 61 72 64 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  ards of the form
1bef8 20 22 3a 61 61 61 22 20 6f 72 20 22 24 61 61 61   ":aaa" or "$aaa
1bef9 22 20 61 72 65 20 61 73 73 69 67 6e 65 64 20 74  " are assigned t
1befa 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 0a 2a  he same number.*
1befb 2a 20 61 73 20 74 68 65 20 70 72 65 76 69 6f 75  * as the previou
1befc 73 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  s instance of th
1befd 65 20 73 61 6d 65 20 77 69 6c 64 63 61 72 64 2e  e same wildcard.
1befe 20 20 4f 72 20 69 66 20 74 68 69 73 20 69 73 20    Or if this is 
1beff 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e 73  the first.** ins
1bf00 74 61 6e 63 65 20 6f 66 20 74 68 65 20 77 69 6c  tance of the wil
1bf01 64 63 61 72 64 2c 20 74 68 65 20 6e 65 78 74 20  dcard, the next 
1bf02 73 65 71 75 65 6e 69 61 6c 20 76 61 72 69 61 62  sequenial variab
1bf03 6c 65 20 6e 75 6d 62 65 72 20 69 73 0a 2a 2a 20  le number is.** 
1bf04 61 73 73 69 67 6e 65 64 2e 0a 2a 2f 0a 53 51 4c  assigned..*/.SQL
1bf05 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1bf06 20 73 71 6c 69 74 65 33 45 78 70 72 41 73 73 69   sqlite3ExprAssi
1bf07 67 6e 56 61 72 4e 75 6d 62 65 72 28 50 61 72 73  gnVarNumber(Pars
1bf08 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
1bf09 2a 70 45 78 70 72 29 7b 0a 20 20 54 6f 6b 65 6e  *pExpr){.  Token
1bf0a 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20 73 71 6c 69   *pToken;.  sqli
1bf0b 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
1bf0c 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 70 45 78  ->db;..  if( pEx
1bf0d 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  pr==0 ) return;.
1bf0e 20 20 70 54 6f 6b 65 6e 20 3d 20 26 70 45 78 70    pToken = &pExp
1bf0f 72 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 61 73 73 65  r->token;.  asse
1bf10 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 6e 3e 3d 31  rt( pToken->n>=1
1bf11 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54   );.  assert( pT
1bf12 6f 6b 65 6e 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20  oken->z!=0 );.  
1bf13 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 2d 3e  assert( pToken->
1bf14 7a 5b 30 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28  z[0]!=0 );.  if(
1bf15 20 70 54 6f 6b 65 6e 2d 3e 6e 3d 3d 31 20 29 7b   pToken->n==1 ){
1bf16 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64  .    /* Wildcard
1bf17 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 22   of the form "?"
1bf18 2e 20 20 41 73 73 69 67 6e 20 74 68 65 20 6e 65  .  Assign the ne
1bf19 78 74 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62  xt variable numb
1bf1a 65 72 20 2a 2f 0a 20 20 20 20 70 45 78 70 72 2d  er */.    pExpr-
1bf1b 3e 69 54 61 62 6c 65 20 3d 20 2b 2b 70 50 61 72  >iTable = ++pPar
1bf1c 73 65 2d 3e 6e 56 61 72 3b 0a 20 20 7d 65 6c 73  se->nVar;.  }els
1bf1d 65 20 69 66 28 20 70 54 6f 6b 65 6e 2d 3e 7a 5b  e if( pToken->z[
1bf1e 30 5d 3d 3d 27 3f 27 20 29 7b 0a 20 20 20 20 2f  0]=='?' ){.    /
1bf1f 2a 20 57 69 6c 64 63 61 72 64 20 6f 66 20 74 68  * Wildcard of th
1bf20 65 20 66 6f 72 6d 20 22 3f 6e 6e 6e 22 2e 20 20  e form "?nnn".  
1bf21 43 6f 6e 76 65 72 74 20 22 6e 6e 6e 22 20 74 6f  Convert "nnn" to
1bf22 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 0a   an integer and.
1bf23 20 20 20 20 2a 2a 20 75 73 65 20 69 74 20 61 73      ** use it as
1bf24 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 6e 75   the variable nu
1bf25 6d 62 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  mber */.    int 
1bf26 69 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 54  i;.    pExpr->iT
1bf27 61 62 6c 65 20 3d 20 69 20 3d 20 61 74 6f 69 28  able = i = atoi(
1bf28 28 63 68 61 72 2a 29 26 70 54 6f 6b 65 6e 2d 3e  (char*)&pToken->
1bf29 7a 5b 31 5d 29 3b 0a 20 20 20 20 74 65 73 74 63  z[1]);.    testc
1bf2a 61 73 65 28 20 69 3d 3d 30 20 29 3b 0a 20 20 20  ase( i==0 );.   
1bf2b 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 20   testcase( i==1 
1bf2c 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
1bf2d 20 69 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53   i==db->aLimit[S
1bf2e 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49  QLITE_LIMIT_VARI
1bf2f 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 2d 31 20 29  ABLE_NUMBER]-1 )
1bf30 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
1bf31 69 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  i==db->aLimit[SQ
1bf32 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41  LITE_LIMIT_VARIA
1bf33 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 3b 0a 20  BLE_NUMBER] );. 
1bf34 20 20 20 69 66 28 20 69 3c 31 20 7c 7c 20 69 3e     if( i<1 || i>
1bf35 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
1bf36 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45  E_LIMIT_VARIABLE
1bf37 5f 4e 55 4d 42 45 52 5d 20 29 7b 0a 20 20 20 20  _NUMBER] ){.    
1bf38 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1bf39 67 28 70 50 61 72 73 65 2c 20 22 76 61 72 69 61  g(pParse, "varia
1bf3a 62 6c 65 20 6e 75 6d 62 65 72 20 6d 75 73 74 20  ble number must 
1bf3b 62 65 20 62 65 74 77 65 65 6e 20 3f 31 20 61 6e  be between ?1 an
1bf3c 64 20 3f 25 64 22 2c 0a 20 20 20 20 20 20 20 20  d ?%d",.        
1bf3d 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c    db->aLimit[SQL
1bf3e 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42  ITE_LIMIT_VARIAB
1bf3f 4c 45 5f 4e 55 4d 42 45 52 5d 29 3b 0a 20 20 20  LE_NUMBER]);.   
1bf40 20 7d 0a 20 20 20 20 69 66 28 20 69 3e 70 50 61   }.    if( i>pPa
1bf41 72 73 65 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20  rse->nVar ){.   
1bf42 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 20     pParse->nVar 
1bf43 3d 20 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  = i;.    }.  }el
1bf44 73 65 7b 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63  se{.    /* Wildc
1bf45 61 72 64 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  ards of the form
1bf46 20 22 3a 61 61 61 22 20 6f 72 20 22 24 61 61 61   ":aaa" or "$aaa
1bf47 22 2e 20 20 52 65 75 73 65 20 74 68 65 20 73 61  ".  Reuse the sa
1bf48 6d 65 20 76 61 72 69 61 62 6c 65 0a 20 20 20 20  me variable.    
1bf49 2a 2a 20 6e 75 6d 62 65 72 20 61 73 20 74 68 65  ** number as the
1bf4a 20 70 72 69 6f 72 20 61 70 70 65 61 72 61 6e 63   prior appearanc
1bf4b 65 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6e 61  e of the same na
1bf4c 6d 65 2c 20 6f 72 20 69 66 20 74 68 65 20 6e 61  me, or if the na
1bf4d 6d 65 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e 65  me.    ** has ne
1bf4e 76 65 72 20 61 70 70 65 61 72 65 64 20 62 65 66  ver appeared bef
1bf4f 6f 72 65 2c 20 72 65 75 73 65 20 74 68 65 20 73  ore, reuse the s
1bf50 61 6d 65 20 76 61 72 69 61 62 6c 65 20 6e 75 6d  ame variable num
1bf51 62 65 72 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ber.    */.    i
1bf52 6e 74 20 69 2c 20 6e 3b 0a 20 20 20 20 6e 20 3d  nt i, n;.    n =
1bf53 20 70 54 6f 6b 65 6e 2d 3e 6e 3b 0a 20 20 20 20   pToken->n;.    
1bf54 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73  for(i=0; i<pPars
1bf55 65 2d 3e 6e 56 61 72 45 78 70 72 3b 20 69 2b 2b  e->nVarExpr; i++
1bf56 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  ){.      Expr *p
1bf57 45 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70 45  E;.      if( (pE
1bf58 20 3d 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72   = pParse->apVar
1bf59 45 78 70 72 5b 69 5d 29 21 3d 30 0a 20 20 20 20  Expr[i])!=0.    
1bf5a 20 20 20 20 20 20 26 26 20 70 45 2d 3e 74 6f 6b        && pE->tok
1bf5b 65 6e 2e 6e 3d 3d 6e 0a 20 20 20 20 20 20 20 20  en.n==n.        
1bf5c 20 20 26 26 20 6d 65 6d 63 6d 70 28 70 45 2d 3e    && memcmp(pE->
1bf5d 74 6f 6b 65 6e 2e 7a 2c 20 70 54 6f 6b 65 6e 2d  token.z, pToken-
1bf5e 3e 7a 2c 20 6e 29 3d 3d 30 20 29 7b 0a 20 20 20  >z, n)==0 ){.   
1bf5f 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62       pExpr->iTab
1bf60 6c 65 20 3d 20 70 45 2d 3e 69 54 61 62 6c 65 3b  le = pE->iTable;
1bf61 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
1bf62 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1bf63 20 20 69 66 28 20 69 3e 3d 70 50 61 72 73 65 2d    if( i>=pParse-
1bf64 3e 6e 56 61 72 45 78 70 72 20 29 7b 0a 20 20 20  >nVarExpr ){.   
1bf65 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65     pExpr->iTable
1bf66 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61   = ++pParse->nVa
1bf67 72 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61  r;.      if( pPa
1bf68 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 3e 3d 70  rse->nVarExpr>=p
1bf69 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41  Parse->nVarExprA
1bf6a 6c 6c 6f 63 2d 31 20 29 7b 0a 20 20 20 20 20 20  lloc-1 ){.      
1bf6b 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78    pParse->nVarEx
1bf6c 70 72 41 6c 6c 6f 63 20 2b 3d 20 70 50 61 72 73  prAlloc += pPars
1bf6d 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63  e->nVarExprAlloc
1bf6e 20 2b 20 31 30 3b 0a 20 20 20 20 20 20 20 20 70   + 10;.        p
1bf6f 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72  Parse->apVarExpr
1bf70 20 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73   =.            s
1bf71 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f  qlite3DbReallocO
1bf72 72 46 72 65 65 28 0a 20 20 20 20 20 20 20 20 20  rFree(.         
1bf73 20 20 20 20 20 64 62 2c 0a 20 20 20 20 20 20 20       db,.       
1bf74 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61         pParse->a
1bf75 70 56 61 72 45 78 70 72 2c 0a 20 20 20 20 20 20  pVarExpr,.      
1bf76 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e          pParse->
1bf77 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 2a 73 69  nVarExprAlloc*si
1bf78 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61 70 56  zeof(pParse->apV
1bf79 61 72 45 78 70 72 5b 30 5d 29 0a 20 20 20 20 20  arExpr[0]).     
1bf7a 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20         );.      
1bf7b 7d 0a 20 20 20 20 20 20 69 66 28 20 21 64 62 2d  }.      if( !db-
1bf7c 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
1bf7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1bf7e 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78   pParse->apVarEx
1bf7f 70 72 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  pr!=0 );.       
1bf80 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78   pParse->apVarEx
1bf81 70 72 5b 70 50 61 72 73 65 2d 3e 6e 56 61 72 45  pr[pParse->nVarE
1bf82 78 70 72 2b 2b 5d 20 3d 20 70 45 78 70 72 3b 0a  xpr++] = pExpr;.
1bf83 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1bf84 7d 20 0a 20 20 69 66 28 20 21 70 50 61 72 73 65  } .  if( !pParse
1bf85 2d 3e 6e 45 72 72 20 26 26 20 70 50 61 72 73 65  ->nErr && pParse
1bf86 2d 3e 6e 56 61 72 3e 64 62 2d 3e 61 4c 69 6d 69  ->nVar>db->aLimi
1bf87 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56  t[SQLITE_LIMIT_V
1bf88 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20  ARIABLE_NUMBER] 
1bf89 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
1bf8a 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1bf8b 74 6f 6f 20 6d 61 6e 79 20 53 51 4c 20 76 61 72  too many SQL var
1bf8c 69 61 62 6c 65 73 22 29 3b 0a 20 20 7d 0a 7d 0a  iables");.  }.}.
1bf8d 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6e 20  ./*.** Clear an 
1bf8e 65 78 70 72 65 73 73 69 6f 6e 20 73 74 72 75 63  expression struc
1bf8f 74 75 72 65 20 77 69 74 68 6f 75 74 20 64 65 6c  ture without del
1bf90 65 74 69 6e 67 20 74 68 65 20 73 74 72 75 63 74  eting the struct
1bf91 75 72 65 20 69 74 73 65 6c 66 2e 0a 2a 2a 20 53  ure itself..** S
1bf92 75 62 73 74 72 75 63 74 75 72 65 20 69 73 20 64  ubstructure is d
1bf93 65 6c 65 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  eleted..*/.SQLIT
1bf94 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1bf95 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72 28  qlite3ExprClear(
1bf96 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70  sqlite3 *db, Exp
1bf97 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e  r *p){.  if( p->
1bf98 73 70 61 6e 2e 64 79 6e 20 29 20 73 71 6c 69 74  span.dyn ) sqlit
1bf99 65 33 44 62 46 72 65 65 28 64 62 2c 20 28 63 68  e3DbFree(db, (ch
1bf9a 61 72 2a 29 70 2d 3e 73 70 61 6e 2e 7a 29 3b 0a  ar*)p->span.z);.
1bf9b 20 20 69 66 28 20 70 2d 3e 74 6f 6b 65 6e 2e 64    if( p->token.d
1bf9c 79 6e 20 29 20 73 71 6c 69 74 65 33 44 62 46 72  yn ) sqlite3DbFr
1bf9d 65 65 28 64 62 2c 20 28 63 68 61 72 2a 29 70 2d  ee(db, (char*)p-
1bf9e 3e 74 6f 6b 65 6e 2e 7a 29 3b 0a 20 20 73 71 6c  >token.z);.  sql
1bf9f 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
1bfa0 62 2c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20  b, p->pLeft);.  
1bfa1 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
1bfa2 65 28 64 62 2c 20 70 2d 3e 70 52 69 67 68 74 29  e(db, p->pRight)
1bfa3 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  ;.  sqlite3ExprL
1bfa4 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d  istDelete(db, p-
1bfa5 3e 70 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74  >pList);.  sqlit
1bfa6 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64  e3SelectDelete(d
1bfa7 62 2c 20 70 2d 3e 70 53 65 6c 65 63 74 29 3b 0a  b, p->pSelect);.
1bfa8 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 75 72 73 69  }../*.** Recursi
1bfa9 76 65 6c 79 20 64 65 6c 65 74 65 20 61 6e 20 65  vely delete an e
1bfaa 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 0a  xpression tree..
1bfab 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1bfac 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
1bfad 70 72 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33  prDelete(sqlite3
1bfae 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 29 7b 0a   *db, Expr *p){.
1bfaf 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74    if( p==0 ) ret
1bfb0 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  urn;.  sqlite3Ex
1bfb1 70 72 43 6c 65 61 72 28 64 62 2c 20 70 29 3b 0a  prClear(db, p);.
1bfb2 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1bfb3 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  db, p);.}../*.**
1bfb4 20 54 68 65 20 45 78 70 72 2e 74 6f 6b 65 6e 20   The Expr.token 
1bfb5 66 69 65 6c 64 20 6d 69 67 68 74 20 62 65 20 61  field might be a
1bfb6 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 20   string literal 
1bfb7 74 68 61 74 20 69 73 20 71 75 6f 74 65 64 2e 0a  that is quoted..
1bfb8 2a 2a 20 49 66 20 73 6f 2c 20 72 65 6d 6f 76 65  ** If so, remove
1bfb9 20 74 68 65 20 71 75 6f 74 61 74 69 6f 6e 20 6d   the quotation m
1bfba 61 72 6b 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  arks..*/.SQLITE_
1bfbb 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1bfbc 69 74 65 33 44 65 71 75 6f 74 65 45 78 70 72 28  ite3DequoteExpr(
1bfbd 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70  sqlite3 *db, Exp
1bfbe 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 45 78 70  r *p){.  if( Exp
1bfbf 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28  rHasAnyProperty(
1bfc0 70 2c 20 45 50 5f 44 65 71 75 6f 74 65 64 29 20  p, EP_Dequoted) 
1bfc1 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
1bfc2 20 7d 0a 20 20 45 78 70 72 53 65 74 50 72 6f 70   }.  ExprSetProp
1bfc3 65 72 74 79 28 70 2c 20 45 50 5f 44 65 71 75 6f  erty(p, EP_Dequo
1bfc4 74 65 64 29 3b 0a 20 20 69 66 28 20 70 2d 3e 74  ted);.  if( p->t
1bfc5 6f 6b 65 6e 2e 64 79 6e 3d 3d 30 20 29 7b 0a 20  oken.dyn==0 ){. 
1bfc6 20 20 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43     sqlite3TokenC
1bfc7 6f 70 79 28 64 62 2c 20 26 70 2d 3e 74 6f 6b 65  opy(db, &p->toke
1bfc8 6e 2c 20 26 70 2d 3e 74 6f 6b 65 6e 29 3b 0a 20  n, &p->token);. 
1bfc9 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 65 71 75   }.  sqlite3Dequ
1bfca 6f 74 65 28 28 63 68 61 72 2a 29 70 2d 3e 74 6f  ote((char*)p->to
1bfcb 6b 65 6e 2e 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ken.z);.}../*.**
1bfcc 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67   The following g
1bfcd 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73  roup of routines
1bfce 20 6d 61 6b 65 20 64 65 65 70 20 63 6f 70 69 65   make deep copie
1bfcf 73 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73  s of expressions
1bfd0 2c 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20  ,.** expression 
1bfd1 6c 69 73 74 73 2c 20 49 44 20 6c 69 73 74 73 2c  lists, ID lists,
1bfd2 20 61 6e 64 20 73 65 6c 65 63 74 20 73 74 61 74   and select stat
1bfd3 65 6d 65 6e 74 73 2e 20 20 54 68 65 20 63 6f 70  ements.  The cop
1bfd4 69 65 73 20 63 61 6e 0a 2a 2a 20 62 65 20 64 65  ies can.** be de
1bfd5 6c 65 74 65 64 20 28 62 79 20 62 65 69 6e 67 20  leted (by being 
1bfd6 70 61 73 73 65 64 20 74 6f 20 74 68 65 69 72 20  passed to their 
1bfd7 72 65 73 70 65 63 74 69 76 65 20 2e 2e 2e 44 65  respective ...De
1bfd8 6c 65 74 65 28 29 20 72 6f 75 74 69 6e 65 73 29  lete() routines)
1bfd9 0a 2a 2a 20 77 69 74 68 6f 75 74 20 65 66 66 65  .** without effe
1bfda 63 74 69 6e 67 20 74 68 65 20 6f 72 69 67 69 6e  cting the origin
1bfdb 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65  als..**.** The e
1bfdc 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2c 20  xpression list, 
1bfdd 49 44 2c 20 61 6e 64 20 73 6f 75 72 63 65 20 6c  ID, and source l
1bfde 69 73 74 73 20 72 65 74 75 72 6e 20 62 79 20 73  ists return by s
1bfdf 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75  qlite3ExprListDu
1bfe0 70 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 49  p(),.** sqlite3I
1bfe1 64 4c 69 73 74 44 75 70 28 29 2c 20 61 6e 64 20  dListDup(), and 
1bfe2 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75  sqlite3SrcListDu
1bfe3 70 28 29 20 63 61 6e 20 6e 6f 74 20 62 65 20 66  p() can not be f
1bfe4 75 72 74 68 65 72 20 65 78 70 61 6e 64 65 64 20  urther expanded 
1bfe5 0a 2a 2a 20 62 79 20 73 75 62 73 65 71 75 65 6e  .** by subsequen
1bfe6 74 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74  t calls to sqlit
1bfe7 65 2a 4c 69 73 74 41 70 70 65 6e 64 28 29 20 72  e*ListAppend() r
1bfe8 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 41  outines..**.** A
1bfe9 6e 79 20 74 61 62 6c 65 73 20 74 68 61 74 20 74  ny tables that t
1bfea 68 65 20 53 72 63 4c 69 73 74 20 6d 69 67 68 74  he SrcList might
1bfeb 20 70 6f 69 6e 74 20 74 6f 20 61 72 65 20 6e 6f   point to are no
1bfec 74 20 64 75 70 6c 69 63 61 74 65 64 2e 0a 2a 2f  t duplicated..*/
1bfed 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1bfee 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70  Expr *sqlite3Exp
1bfef 72 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62  rDup(sqlite3 *db
1bff0 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 45 78  , Expr *p){.  Ex
1bff1 70 72 20 2a 70 4e 65 77 3b 0a 20 20 69 66 28 20  pr *pNew;.  if( 
1bff2 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  p==0 ) return 0;
1bff3 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  pNew = sqlite
1bff4 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
1bff5 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20   sizeof(*p) );. 
1bff6 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72   if( pNew==0 ) r
1bff7 65 74 75 72 6e 20 30 3b 0a 20 20 6d 65 6d 63 70  eturn 0;.  memcp
1bff8 79 28 70 4e 65 77 2c 20 70 2c 20 73 69 7a 65 6f  y(pNew, p, sizeo
1bff9 66 28 2a 70 4e 65 77 29 29 3b 0a 20 20 69 66 28  f(*pNew));.  if(
1bffa 20 70 2d 3e 74 6f 6b 65 6e 2e 7a 21 3d 30 20 29   p->token.z!=0 )
1bffb 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 74 6f 6b 65  {.    pNew->toke
1bffc 6e 2e 7a 20 3d 20 28 75 38 2a 29 73 71 6c 69 74  n.z = (u8*)sqlit
1bffd 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20  e3DbStrNDup(db, 
1bffe 28 63 68 61 72 2a 29 70 2d 3e 74 6f 6b 65 6e 2e  (char*)p->token.
1bfff 7a 2c 20 70 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b 0a  z, p->token.n);.
1c000 20 20 20 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e      pNew->token.
1c001 64 79 6e 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  dyn = 1;.  }else
1c002 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e  {.    assert( pN
1c003 65 77 2d 3e 74 6f 6b 65 6e 2e 7a 3d 3d 30 20 29  ew->token.z==0 )
1c004 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 73 70  ;.  }.  pNew->sp
1c005 61 6e 2e 7a 20 3d 20 30 3b 0a 20 20 70 4e 65 77  an.z = 0;.  pNew
1c006 2d 3e 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65  ->pLeft = sqlite
1c007 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e  3ExprDup(db, p->
1c008 70 4c 65 66 74 29 3b 0a 20 20 70 4e 65 77 2d 3e  pLeft);.  pNew->
1c009 70 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33  pRight = sqlite3
1c00a 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70  ExprDup(db, p->p
1c00b 52 69 67 68 74 29 3b 0a 20 20 70 4e 65 77 2d 3e  Right);.  pNew->
1c00c 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45  pList = sqlite3E
1c00d 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70  xprListDup(db, p
1c00e 2d 3e 70 4c 69 73 74 29 3b 0a 20 20 70 4e 65 77  ->pList);.  pNew
1c00f 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69  ->pSelect = sqli
1c010 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c  te3SelectDup(db,
1c011 20 70 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20   p->pSelect);.  
1c012 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53  return pNew;.}.S
1c013 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1c014 69 64 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43  id sqlite3TokenC
1c015 6f 70 79 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  opy(sqlite3 *db,
1c016 20 54 6f 6b 65 6e 20 2a 70 54 6f 2c 20 54 6f 6b   Token *pTo, Tok
1c017 65 6e 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 66  en *pFrom){.  if
1c018 28 20 70 54 6f 2d 3e 64 79 6e 20 29 20 73 71 6c  ( pTo->dyn ) sql
1c019 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 28  ite3DbFree(db, (
1c01a 63 68 61 72 2a 29 70 54 6f 2d 3e 7a 29 3b 0a 20  char*)pTo->z);. 
1c01b 20 69 66 28 20 70 46 72 6f 6d 2d 3e 7a 20 29 7b   if( pFrom->z ){
1c01c 0a 20 20 20 20 70 54 6f 2d 3e 6e 20 3d 20 70 46  .    pTo->n = pF
1c01d 72 6f 6d 2d 3e 6e 3b 0a 20 20 20 20 70 54 6f 2d  rom->n;.    pTo-
1c01e 3e 7a 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65  >z = (u8*)sqlite
1c01f 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28  3DbStrNDup(db, (
1c020 63 68 61 72 2a 29 70 46 72 6f 6d 2d 3e 7a 2c 20  char*)pFrom->z, 
1c021 70 46 72 6f 6d 2d 3e 6e 29 3b 0a 20 20 20 20 70  pFrom->n);.    p
1c022 54 6f 2d 3e 64 79 6e 20 3d 20 31 3b 0a 20 20 7d  To->dyn = 1;.  }
1c023 65 6c 73 65 7b 0a 20 20 20 20 70 54 6f 2d 3e 7a  else{.    pTo->z
1c024 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49   = 0;.  }.}.SQLI
1c025 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c  TE_PRIVATE ExprL
1c026 69 73 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72  ist *sqlite3Expr
1c027 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 20  ListDup(sqlite3 
1c028 2a 64 62 2c 20 45 78 70 72 4c 69 73 74 20 2a 70  *db, ExprList *p
1c029 29 7b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  ){.  ExprList *p
1c02a 4e 65 77 3b 0a 20 20 73 74 72 75 63 74 20 45 78  New;.  struct Ex
1c02b 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  prList_item *pIt
1c02c 65 6d 2c 20 2a 70 4f 6c 64 49 74 65 6d 3b 0a 20  em, *pOldItem;. 
1c02d 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d   int i;.  if( p=
1c02e 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
1c02f 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   pNew = sqlite3D
1c030 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73  bMallocRaw(db, s
1c031 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a  izeof(*pNew) );.
1c032 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20    if( pNew==0 ) 
1c033 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77  return 0;.  pNew
1c034 2d 3e 69 45 43 75 72 73 6f 72 20 3d 20 30 3b 0a  ->iECursor = 0;.
1c035 20 20 70 4e 65 77 2d 3e 6e 45 78 70 72 20 3d 20    pNew->nExpr = 
1c036 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70  pNew->nAlloc = p
1c037 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 4e 65 77 2d  ->nExpr;.  pNew-
1c038 3e 61 20 3d 20 70 49 74 65 6d 20 3d 20 73 71 6c  >a = pItem = sql
1c039 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
1c03a 64 62 2c 20 20 70 2d 3e 6e 45 78 70 72 2a 73 69  db,  p->nExpr*si
1c03b 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 29 3b  zeof(p->a[0]) );
1c03c 0a 20 20 69 66 28 20 70 49 74 65 6d 3d 3d 30 20  .  if( pItem==0 
1c03d 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  ){.    sqlite3Db
1c03e 46 72 65 65 28 64 62 2c 20 70 4e 65 77 29 3b 0a  Free(db, pNew);.
1c03f 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
1c040 7d 20 0a 20 20 70 4f 6c 64 49 74 65 6d 20 3d 20  } .  pOldItem = 
1c041 70 2d 3e 61 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  p->a;.  for(i=0;
1c042 20 69 3c 70 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b   i<p->nExpr; i++
1c043 2c 20 70 49 74 65 6d 2b 2b 2c 20 70 4f 6c 64 49  , pItem++, pOldI
1c044 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72  tem++){.    Expr
1c045 20 2a 70 4e 65 77 45 78 70 72 2c 20 2a 70 4f 6c   *pNewExpr, *pOl
1c046 64 45 78 70 72 3b 0a 20 20 20 20 70 49 74 65 6d  dExpr;.    pItem
1c047 2d 3e 70 45 78 70 72 20 3d 20 70 4e 65 77 45 78  ->pExpr = pNewEx
1c048 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  pr = sqlite3Expr
1c049 44 75 70 28 64 62 2c 20 70 4f 6c 64 45 78 70 72  Dup(db, pOldExpr
1c04a 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 45 78   = pOldItem->pEx
1c04b 70 72 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c  pr);.    if( pOl
1c04c 64 45 78 70 72 2d 3e 73 70 61 6e 2e 7a 21 3d 30  dExpr->span.z!=0
1c04d 20 26 26 20 70 4e 65 77 45 78 70 72 20 29 7b 0a   && pNewExpr ){.
1c04e 20 20 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 20        /* Always 
1c04f 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74  make a copy of t
1c050 68 65 20 73 70 61 6e 20 66 6f 72 20 74 6f 70 2d  he span for top-
1c051 6c 65 76 65 6c 20 65 78 70 72 65 73 73 69 6f 6e  level expression
1c052 73 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20 2a  s in the.      *
1c053 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  * expression lis
1c054 74 2e 20 20 54 68 65 20 6c 6f 67 69 63 20 69 6e  t.  The logic in
1c055 20 53 45 4c 45 43 54 20 70 72 6f 63 65 73 73 69   SELECT processi
1c056 6e 67 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e  ng that determin
1c057 65 73 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  es.      ** the 
1c058 6e 61 6d 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73  names of columns
1c059 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
1c05a 65 74 20 6e 65 65 64 73 20 74 68 69 73 20 69 6e  et needs this in
1c05b 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 20  formation */.   
1c05c 20 20 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43     sqlite3TokenC
1c05d 6f 70 79 28 64 62 2c 20 26 70 4e 65 77 45 78 70  opy(db, &pNewExp
1c05e 72 2d 3e 73 70 61 6e 2c 20 26 70 4f 6c 64 45 78  r->span, &pOldEx
1c05f 70 72 2d 3e 73 70 61 6e 29 3b 0a 20 20 20 20 7d  pr->span);.    }
1c060 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65  .    assert( pNe
1c061 77 45 78 70 72 3d 3d 30 20 7c 7c 20 70 4e 65 77  wExpr==0 || pNew
1c062 45 78 70 72 2d 3e 73 70 61 6e 2e 7a 21 3d 30 20  Expr->span.z!=0 
1c063 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  .            || 
1c064 70 4f 6c 64 45 78 70 72 2d 3e 73 70 61 6e 2e 7a  pOldExpr->span.z
1c065 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 20  ==0.            
1c066 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
1c067 6c 65 64 20 29 3b 0a 20 20 20 20 70 49 74 65 6d  led );.    pItem
1c068 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65  ->zName = sqlite
1c069 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f  3DbStrDup(db, pO
1c06a 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a  ldItem->zName);.
1c06b 20 20 20 20 70 49 74 65 6d 2d 3e 73 6f 72 74 4f      pItem->sortO
1c06c 72 64 65 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d  rder = pOldItem-
1c06d 3e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20  >sortOrder;.    
1c06e 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20 30 3b  pItem->done = 0;
1c06f 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c  .    pItem->iCol
1c070 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 43 6f   = pOldItem->iCo
1c071 6c 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 41  l;.    pItem->iA
1c072 6c 69 61 73 20 3d 20 70 4f 6c 64 49 74 65 6d 2d  lias = pOldItem-
1c073 3e 69 41 6c 69 61 73 3b 0a 20 20 7d 0a 20 20 72  >iAlias;.  }.  r
1c074 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f  eturn pNew;.}../
1c075 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 73 2c  *.** If cursors,
1c076 20 74 72 69 67 67 65 72 73 2c 20 76 69 65 77 73   triggers, views
1c077 20 61 6e 64 20 73 75 62 71 75 65 72 69 65 73 20   and subqueries 
1c078 61 72 65 20 61 6c 6c 20 6f 6d 69 74 74 65 64 20  are all omitted 
1c079 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 62 75 69 6c  from.** the buil
1c07a 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 20  d, then none of 
1c07b 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f  the following ro
1c07c 75 74 69 6e 65 73 2c 20 65 78 63 65 70 74 20 66  utines, except f
1c07d 6f 72 20 0a 2a 2a 20 73 71 6c 69 74 65 33 53 65  or .** sqlite3Se
1c07e 6c 65 63 74 44 75 70 28 29 2c 20 63 61 6e 20 62  lectDup(), can b
1c07f 65 20 63 61 6c 6c 65 64 2e 20 73 71 6c 69 74 65  e called. sqlite
1c080 33 53 65 6c 65 63 74 44 75 70 28 29 20 69 73 20  3SelectDup() is 
1c081 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 63 61 6c  sometimes.** cal
1c082 6c 65 64 20 77 69 74 68 20 61 20 4e 55 4c 4c 20  led with a NULL 
1c083 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 69 66  argument..*/.#if
1c084 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1c085 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21  _OMIT_VIEW) || !
1c086 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
1c087 4d 49 54 5f 54 52 49 47 47 45 52 29 20 5c 0a 20  MIT_TRIGGER) \. 
1c088 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  || !defined(SQLI
1c089 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
1c08a 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ).SQLITE_PRIVATE
1c08b 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65   SrcList *sqlite
1c08c 33 53 72 63 4c 69 73 74 44 75 70 28 73 71 6c 69  3SrcListDup(sqli
1c08d 74 65 33 20 2a 64 62 2c 20 53 72 63 4c 69 73 74  te3 *db, SrcList
1c08e 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 20   *p){.  SrcList 
1c08f 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 69 3b 0a  *pNew;.  int i;.
1c090 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 69    int nByte;.  i
1c091 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
1c092 20 30 3b 0a 20 20 6e 42 79 74 65 20 3d 20 73 69   0;.  nByte = si
1c093 7a 65 6f 66 28 2a 70 29 20 2b 20 28 70 2d 3e 6e  zeof(*p) + (p->n
1c094 53 72 63 3e 30 20 3f 20 73 69 7a 65 6f 66 28 70  Src>0 ? sizeof(p
1c095 2d 3e 61 5b 30 5d 29 20 2a 20 28 70 2d 3e 6e 53  ->a[0]) * (p->nS
1c096 72 63 2d 31 29 20 3a 20 30 29 3b 0a 20 20 70 4e  rc-1) : 0);.  pN
1c097 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ew = sqlite3DbMa
1c098 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 42 79 74  llocRaw(db, nByt
1c099 65 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d  e );.  if( pNew=
1c09a 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
1c09b 20 70 4e 65 77 2d 3e 6e 53 72 63 20 3d 20 70 4e   pNew->nSrc = pN
1c09c 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e  ew->nAlloc = p->
1c09d 6e 53 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nSrc;.  for(i=0;
1c09e 20 69 3c 70 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29   i<p->nSrc; i++)
1c09f 7b 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63  {.    struct Src
1c0a0 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49  List_item *pNewI
1c0a1 74 65 6d 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69  tem = &pNew->a[i
1c0a2 5d 3b 0a 20 20 20 20 73 74 72 75 63 74 20 53 72  ];.    struct Sr
1c0a3 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4f 6c 64  cList_item *pOld
1c0a4 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b  Item = &p->a[i];
1c0a5 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
1c0a6 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e  ;.    pNewItem->
1c0a7 7a 44 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69  zDatabase = sqli
1c0a8 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
1c0a9 70 4f 6c 64 49 74 65 6d 2d 3e 7a 44 61 74 61 62  pOldItem->zDatab
1c0aa 61 73 65 29 3b 0a 20 20 20 20 70 4e 65 77 49 74  ase);.    pNewIt
1c0ab 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69  em->zName = sqli
1c0ac 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
1c0ad 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29  pOldItem->zName)
1c0ae 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e  ;.    pNewItem->
1c0af 7a 41 6c 69 61 73 20 3d 20 73 71 6c 69 74 65 33  zAlias = sqlite3
1c0b0 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c  DbStrDup(db, pOl
1c0b1 64 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a  dItem->zAlias);.
1c0b2 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 6a 6f      pNewItem->jo
1c0b3 69 6e 74 79 70 65 20 3d 20 70 4f 6c 64 49 74 65  intype = pOldIte
1c0b4 6d 2d 3e 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 20  m->jointype;.   
1c0b5 20 70 4e 65 77 49 74 65 6d 2d 3e 69 43 75 72 73   pNewItem->iCurs
1c0b6 6f 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69  or = pOldItem->i
1c0b7 43 75 72 73 6f 72 3b 0a 20 20 20 20 70 4e 65 77  Cursor;.    pNew
1c0b8 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65  Item->isPopulate
1c0b9 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 73  d = pOldItem->is
1c0ba 50 6f 70 75 6c 61 74 65 64 3b 0a 20 20 20 20 70  Populated;.    p
1c0bb 4e 65 77 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 20  NewItem->zIndex 
1c0bc 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
1c0bd 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e  p(db, pOldItem->
1c0be 7a 49 6e 64 65 78 29 3b 0a 20 20 20 20 70 4e 65  zIndex);.    pNe
1c0bf 77 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65  wItem->notIndexe
1c0c0 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 6e 6f  d = pOldItem->no
1c0c1 74 49 6e 64 65 78 65 64 3b 0a 20 20 20 20 70 4e  tIndexed;.    pN
1c0c2 65 77 49 74 65 6d 2d 3e 70 49 6e 64 65 78 20 3d  ewItem->pIndex =
1c0c3 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 49 6e 64 65   pOldItem->pInde
1c0c4 78 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 70 4e  x;.    pTab = pN
1c0c5 65 77 49 74 65 6d 2d 3e 70 54 61 62 20 3d 20 70  ewItem->pTab = p
1c0c6 4f 6c 64 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20  OldItem->pTab;. 
1c0c7 20 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20     if( pTab ){. 
1c0c8 20 20 20 20 20 70 54 61 62 2d 3e 6e 52 65 66 2b       pTab->nRef+
1c0c9 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65  +;.    }.    pNe
1c0ca 77 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d  wItem->pSelect =
1c0cb 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75   sqlite3SelectDu
1c0cc 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e  p(db, pOldItem->
1c0cd 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 70 4e  pSelect);.    pN
1c0ce 65 77 49 74 65 6d 2d 3e 70 4f 6e 20 3d 20 73 71  ewItem->pOn = sq
1c0cf 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
1c0d0 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 4f 6e 29 3b   pOldItem->pOn);
1c0d1 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70  .    pNewItem->p
1c0d2 55 73 69 6e 67 20 3d 20 73 71 6c 69 74 65 33 49  Using = sqlite3I
1c0d3 64 4c 69 73 74 44 75 70 28 64 62 2c 20 70 4f 6c  dListDup(db, pOl
1c0d4 64 49 74 65 6d 2d 3e 70 55 73 69 6e 67 29 3b 0a  dItem->pUsing);.
1c0d5 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 63 6f      pNewItem->co
1c0d6 6c 55 73 65 64 20 3d 20 70 4f 6c 64 49 74 65 6d  lUsed = pOldItem
1c0d7 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20 20 7d 0a 20  ->colUsed;.  }. 
1c0d8 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a   return pNew;.}.
1c0d9 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49  SQLITE_PRIVATE I
1c0da 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64  dList *sqlite3Id
1c0db 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 20  ListDup(sqlite3 
1c0dc 2a 64 62 2c 20 49 64 4c 69 73 74 20 2a 70 29 7b  *db, IdList *p){
1c0dd 0a 20 20 49 64 4c 69 73 74 20 2a 70 4e 65 77 3b  .  IdList *pNew;
1c0de 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
1c0df 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  p==0 ) return 0;
1c0e0 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  pNew = sqlite
1c0e1 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
1c0e2 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 29   sizeof(*pNew) )
1c0e3 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20  ;.  if( pNew==0 
1c0e4 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e  ) return 0;.  pN
1c0e5 65 77 2d 3e 6e 49 64 20 3d 20 70 4e 65 77 2d 3e  ew->nId = pNew->
1c0e6 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 49 64 3b  nAlloc = p->nId;
1c0e7 0a 20 20 70 4e 65 77 2d 3e 61 20 3d 20 73 71 6c  .  pNew->a = sql
1c0e8 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
1c0e9 64 62 2c 20 70 2d 3e 6e 49 64 2a 73 69 7a 65 6f  db, p->nId*sizeo
1c0ea 66 28 70 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20  f(p->a[0]) );.  
1c0eb 69 66 28 20 70 4e 65 77 2d 3e 61 3d 3d 30 20 29  if( pNew->a==0 )
1c0ec 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  {.    sqlite3DbF
1c0ed 72 65 65 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20  ree(db, pNew);. 
1c0ee 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
1c0ef 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d  .  for(i=0; i<p-
1c0f0 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nId; i++){.    
1c0f1 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f 69 74  struct IdList_it
1c0f2 65 6d 20 2a 70 4e 65 77 49 74 65 6d 20 3d 20 26  em *pNewItem = &
1c0f3 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20  pNew->a[i];.    
1c0f4 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f 69 74  struct IdList_it
1c0f5 65 6d 20 2a 70 4f 6c 64 49 74 65 6d 20 3d 20 26  em *pOldItem = &
1c0f6 70 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 70 4e 65  p->a[i];.    pNe
1c0f7 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73  wItem->zName = s
1c0f8 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
1c0f9 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61  b, pOldItem->zNa
1c0fa 6d 65 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65  me);.    pNewIte
1c0fb 6d 2d 3e 69 64 78 20 3d 20 70 4f 6c 64 49 74 65  m->idx = pOldIte
1c0fc 6d 2d 3e 69 64 78 3b 0a 20 20 7d 0a 20 20 72 65  m->idx;.  }.  re
1c0fd 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c  turn pNew;.}.SQL
1c0fe 49 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65  ITE_PRIVATE Sele
1c0ff 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63  ct *sqlite3Selec
1c100 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62  tDup(sqlite3 *db
1c101 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20  , Select *p){.  
1c102 53 65 6c 65 63 74 20 2a 70 4e 65 77 3b 0a 20 20  Select *pNew;.  
1c103 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
1c104 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71  n 0;.  pNew = sq
1c105 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
1c106 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 29 20  (db, sizeof(*p) 
1c107 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30  );.  if( pNew==0
1c108 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70   ) return 0;.  p
1c109 4e 65 77 2d 3e 70 45 4c 69 73 74 20 3d 20 73 71  New->pEList = sq
1c10a 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70  lite3ExprListDup
1c10b 28 64 62 2c 20 70 2d 3e 70 45 4c 69 73 74 29 3b  (db, p->pEList);
1c10c 0a 20 20 70 4e 65 77 2d 3e 70 53 72 63 20 3d 20  .  pNew->pSrc = 
1c10d 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75  sqlite3SrcListDu
1c10e 70 28 64 62 2c 20 70 2d 3e 70 53 72 63 29 3b 0a  p(db, p->pSrc);.
1c10f 20 20 70 4e 65 77 2d 3e 70 57 68 65 72 65 20 3d    pNew->pWhere =
1c110 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
1c111 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 29 3b 0a  db, p->pWhere);.
1c112 20 20 70 4e 65 77 2d 3e 70 47 72 6f 75 70 42 79    pNew->pGroupBy
1c113 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
1c114 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 47 72  stDup(db, p->pGr
1c115 6f 75 70 42 79 29 3b 0a 20 20 70 4e 65 77 2d 3e  oupBy);.  pNew->
1c116 70 48 61 76 69 6e 67 20 3d 20 73 71 6c 69 74 65  pHaving = sqlite
1c117 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e  3ExprDup(db, p->
1c118 70 48 61 76 69 6e 67 29 3b 0a 20 20 70 4e 65 77  pHaving);.  pNew
1c119 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 73 71 6c  ->pOrderBy = sql
1c11a 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
1c11b 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 29  db, p->pOrderBy)
1c11c 3b 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 70  ;.  pNew->op = p
1c11d 2d 3e 6f 70 3b 0a 20 20 70 4e 65 77 2d 3e 70 50  ->op;.  pNew->pP
1c11e 72 69 6f 72 20 3d 20 73 71 6c 69 74 65 33 53 65  rior = sqlite3Se
1c11f 6c 65 63 74 44 75 70 28 64 62 2c 20 70 2d 3e 70  lectDup(db, p->p
1c120 50 72 69 6f 72 29 3b 0a 20 20 70 4e 65 77 2d 3e  Prior);.  pNew->
1c121 70 4c 69 6d 69 74 20 3d 20 73 71 6c 69 74 65 33  pLimit = sqlite3
1c122 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70  ExprDup(db, p->p
1c123 4c 69 6d 69 74 29 3b 0a 20 20 70 4e 65 77 2d 3e  Limit);.  pNew->
1c124 70 4f 66 66 73 65 74 20 3d 20 73 71 6c 69 74 65  pOffset = sqlite
1c125 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e  3ExprDup(db, p->
1c126 70 4f 66 66 73 65 74 29 3b 0a 20 20 70 4e 65 77  pOffset);.  pNew
1c127 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20  ->iLimit = 0;.  
1c128 70 4e 65 77 2d 3e 69 4f 66 66 73 65 74 20 3d 20  pNew->iOffset = 
1c129 30 3b 0a 20 20 70 4e 65 77 2d 3e 73 65 6c 46 6c  0;.  pNew->selFl
1c12a 61 67 73 20 3d 20 70 2d 3e 73 65 6c 46 6c 61 67  ags = p->selFlag
1c12b 73 20 26 20 7e 53 46 5f 55 73 65 73 45 70 68 65  s & ~SF_UsesEphe
1c12c 6d 65 72 61 6c 3b 0a 20 20 70 4e 65 77 2d 3e 70  meral;.  pNew->p
1c12d 52 69 67 68 74 6d 6f 73 74 20 3d 20 30 3b 0a 20  Rightmost = 0;. 
1c12e 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45   pNew->addrOpenE
1c12f 70 68 6d 5b 30 5d 20 3d 20 2d 31 3b 0a 20 20 70  phm[0] = -1;.  p
1c130 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68  New->addrOpenEph
1c131 6d 5b 31 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65  m[1] = -1;.  pNe
1c132 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b  w->addrOpenEphm[
1c133 32 5d 20 3d 20 2d 31 3b 0a 20 20 72 65 74 75 72  2] = -1;.  retur
1c134 6e 20 70 4e 65 77 3b 0a 7d 0a 23 65 6c 73 65 0a  n pNew;.}.#else.
1c135 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53  SQLITE_PRIVATE S
1c136 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53 65  elect *sqlite3Se
1c137 6c 65 63 74 44 75 70 28 73 71 6c 69 74 65 33 20  lectDup(sqlite3 
1c138 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b  *db, Select *p){
1c139 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20  .  assert( p==0 
1c13a 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  );.  return 0;.}
1c13b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
1c13c 41 64 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e  Add a new elemen
1c13d 74 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  t to the end of 
1c13e 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  an expression li
1c13f 73 74 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73  st.  If pList is
1c140 0a 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 20 4e 55  .** initially NU
1c141 4c 4c 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20  LL, then create 
1c142 61 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e  a new expression
1c143 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45   list..*/.SQLITE
1c144 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73  _PRIVATE ExprLis
1c145 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69  t *sqlite3ExprLi
1c146 73 74 41 70 70 65 6e 64 28 0a 20 20 50 61 72 73  stAppend(.  Pars
1c147 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
1c148 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
1c149 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72  ontext */.  Expr
1c14a 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20  List *pList,    
1c14b 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 77      /* List to w
1c14c 68 69 63 68 20 74 6f 20 61 70 70 65 6e 64 2e 20  hich to append. 
1c14d 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f  Might be NULL */
1c14e 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20  .  Expr *pExpr, 
1c14f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78             /* Ex
1c150 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 61  pression to be a
1c151 70 70 65 6e 64 65 64 20 2a 2f 0a 20 20 54 6f 6b  ppended */.  Tok
1c152 65 6e 20 2a 70 4e 61 6d 65 20 20 20 20 20 20 20  en *pName       
1c153 20 20 20 20 20 2f 2a 20 41 53 20 6b 65 79 77 6f       /* AS keywo
1c154 72 64 20 66 6f 72 20 74 68 65 20 65 78 70 72 65  rd for the expre
1c155 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71  ssion */.){.  sq
1c156 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
1c157 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 4c  se->db;.  if( pL
1c158 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c  ist==0 ){.    pL
1c159 69 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  ist = sqlite3DbM
1c15a 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
1c15b 7a 65 6f 66 28 45 78 70 72 4c 69 73 74 29 20 29  zeof(ExprList) )
1c15c 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d  ;.    if( pList=
1c15d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  =0 ){.      goto
1c15e 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20   no_mem;.    }. 
1c15f 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74     assert( pList
1c160 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20  ->nAlloc==0 );. 
1c161 20 7d 0a 20 20 69 66 28 20 70 4c 69 73 74 2d 3e   }.  if( pList->
1c162 6e 41 6c 6c 6f 63 3c 3d 70 4c 69 73 74 2d 3e 6e  nAlloc<=pList->n
1c163 45 78 70 72 20 29 7b 0a 20 20 20 20 73 74 72 75  Expr ){.    stru
1c164 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d  ct ExprList_item
1c165 20 2a 61 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d   *a;.    int n =
1c166 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2a 32   pList->nAlloc*2
1c167 20 2b 20 34 3b 0a 20 20 20 20 61 20 3d 20 73 71   + 4;.    a = sq
1c168 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64  lite3DbRealloc(d
1c169 62 2c 20 70 4c 69 73 74 2d 3e 61 2c 20 6e 2a 73  b, pList->a, n*s
1c16a 69 7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30  izeof(pList->a[0
1c16b 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 61 3d 3d  ]));.    if( a==
1c16c 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  0 ){.      goto 
1c16d 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20  no_mem;.    }.  
1c16e 20 20 70 4c 69 73 74 2d 3e 61 20 3d 20 61 3b 0a    pList->a = a;.
1c16f 20 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f      pList->nAllo
1c170 63 20 3d 20 6e 3b 0a 20 20 7d 0a 20 20 61 73 73  c = n;.  }.  ass
1c171 65 72 74 28 20 70 4c 69 73 74 2d 3e 61 21 3d 30  ert( pList->a!=0
1c172 20 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 20   );.  if( pExpr 
1c173 7c 7c 20 70 4e 61 6d 65 20 29 7b 0a 20 20 20 20  || pName ){.    
1c174 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
1c175 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70  item *pItem = &p
1c176 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e  List->a[pList->n
1c177 45 78 70 72 2b 2b 5d 3b 0a 20 20 20 20 6d 65 6d  Expr++];.    mem
1c178 73 65 74 28 70 49 74 65 6d 2c 20 30 2c 20 73 69  set(pItem, 0, si
1c179 7a 65 6f 66 28 2a 70 49 74 65 6d 29 29 3b 0a 20  zeof(*pItem));. 
1c17a 20 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20     pItem->zName 
1c17b 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
1c17c 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65  mToken(db, pName
1c17d 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 70 45  );.    pItem->pE
1c17e 78 70 72 20 3d 20 70 45 78 70 72 3b 0a 20 20 20  xpr = pExpr;.   
1c17f 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 20 3d   pItem->iAlias =
1c180 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   0;.  }.  return
1c181 20 70 4c 69 73 74 3b 0a 0a 6e 6f 5f 6d 65 6d 3a   pList;..no_mem:
1c182 20 20 20 20 20 0a 20 20 2f 2a 20 41 76 6f 69 64       .  /* Avoid
1c183 20 6c 65 61 6b 69 6e 67 20 6d 65 6d 6f 72 79 20   leaking memory 
1c184 69 66 20 6d 61 6c 6c 6f 63 20 68 61 73 20 66 61  if malloc has fa
1c185 69 6c 65 64 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  iled. */.  sqlit
1c186 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
1c187 20 70 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74   pExpr);.  sqlit
1c188 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
1c189 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 72  (db, pList);.  r
1c18a 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
1c18b 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73  * If the express
1c18c 69 6f 6e 20 6c 69 73 74 20 70 45 4c 69 73 74 20  ion list pEList 
1c18d 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74 68  contains more th
1c18e 61 6e 20 69 4c 69 6d 69 74 20 65 6c 65 6d 65 6e  an iLimit elemen
1c18f 74 73 2c 0a 2a 2a 20 6c 65 61 76 65 20 61 6e 20  ts,.** leave an 
1c190 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e  error message in
1c191 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49   pParse..*/.SQLI
1c192 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1c193 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43  sqlite3ExprListC
1c194 68 65 63 6b 4c 65 6e 67 74 68 28 0a 20 20 50 61  heckLength(.  Pa
1c195 72 73 65 20 2a 70 50 61 72 73 65 2c 0a 20 20 45  rse *pParse,.  E
1c196 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c  xprList *pEList,
1c197 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1c198 4f 62 6a 65 63 74 0a 29 7b 0a 20 20 69 6e 74 20  Object.){.  int 
1c199 6d 78 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d  mx = pParse->db-
1c19a 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
1c19b 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 3b 0a 20 20  IMIT_COLUMN];.  
1c19c 74 65 73 74 63 61 73 65 28 20 70 45 4c 69 73 74  testcase( pEList
1c19d 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70   && pEList->nExp
1c19e 72 3d 3d 6d 78 20 29 3b 0a 20 20 74 65 73 74 63  r==mx );.  testc
1c19f 61 73 65 28 20 70 45 4c 69 73 74 20 26 26 20 70  ase( pEList && p
1c1a0 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78  EList->nExpr==mx
1c1a1 2b 31 20 29 3b 0a 20 20 69 66 28 20 70 45 4c 69  +1 );.  if( pELi
1c1a2 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45  st && pEList->nE
1c1a3 78 70 72 3e 6d 78 20 29 7b 0a 20 20 20 20 73 71  xpr>mx ){.    sq
1c1a4 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1c1a5 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20  arse, "too many 
1c1a6 63 6f 6c 75 6d 6e 73 20 69 6e 20 25 73 22 2c 20  columns in %s", 
1c1a7 7a 4f 62 6a 65 63 74 29 3b 0a 20 20 7d 0a 7d 0a  zObject);.  }.}.
1c1a8 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e  ./*.** Delete an
1c1a9 20 65 6e 74 69 72 65 20 65 78 70 72 65 73 73 69   entire expressi
1c1aa 6f 6e 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49  on list..*/.SQLI
1c1ab 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1c1ac 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
1c1ad 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64  elete(sqlite3 *d
1c1ae 62 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  b, ExprList *pLi
1c1af 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  st){.  int i;.  
1c1b0 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
1c1b1 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69  item *pItem;.  i
1c1b2 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65  f( pList==0 ) re
1c1b3 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20  turn;.  assert( 
1c1b4 70 4c 69 73 74 2d 3e 61 21 3d 30 20 7c 7c 20 28  pList->a!=0 || (
1c1b5 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 30 20  pList->nExpr==0 
1c1b6 26 26 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63  && pList->nAlloc
1c1b7 3d 3d 30 29 20 29 3b 0a 20 20 61 73 73 65 72 74  ==0) );.  assert
1c1b8 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3c 3d  ( pList->nExpr<=
1c1b9 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 29 3b  pList->nAlloc );
1c1ba 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69  .  for(pItem=pLi
1c1bb 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c  st->a, i=0; i<pL
1c1bc 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c  ist->nExpr; i++,
1c1bd 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 73   pItem++){.    s
1c1be 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
1c1bf 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70  (db, pItem->pExp
1c1c0 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  r);.    sqlite3D
1c1c1 62 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d  bFree(db, pItem-
1c1c2 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 73  >zName);.  }.  s
1c1c3 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1c1c4 20 70 4c 69 73 74 2d 3e 61 29 3b 0a 20 20 73 71   pList->a);.  sq
1c1c5 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1c1c6 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pList);.}../*.**
1c1c7 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
1c1c8 61 72 65 20 57 61 6c 6b 65 72 20 63 61 6c 6c 62  are Walker callb
1c1c9 61 63 6b 73 2e 20 20 57 61 6c 6b 65 72 2e 75 2e  acks.  Walker.u.
1c1ca 70 69 20 69 73 20 61 20 70 6f 69 6e 74 65 72 0a  pi is a pointer.
1c1cb 2a 2a 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72  ** to an integer
1c1cc 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  .  These routine
1c1cd 73 20 61 72 65 20 63 68 65 63 6b 69 6e 67 20 61  s are checking a
1c1ce 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20  n expression to 
1c1cf 73 65 65 0a 2a 2a 20 69 66 20 69 74 20 69 73 20  see.** if it is 
1c1d0 61 20 63 6f 6e 73 74 61 6e 74 2e 20 20 53 65 74  a constant.  Set
1c1d1 20 2a 57 61 6c 6b 65 72 2e 75 2e 70 69 20 74 6f   *Walker.u.pi to
1c1d2 20 30 20 69 66 20 74 68 65 20 65 78 70 72 65 73   0 if the expres
1c1d3 73 69 6f 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20 63  sion is.** not c
1c1d4 6f 6e 73 74 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  onstant..**.** T
1c1d5 68 65 73 65 20 63 61 6c 6c 62 61 63 6b 20 72 6f  hese callback ro
1c1d6 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20  utines are used 
1c1d7 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
1c1d8 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a   following:.**.*
1c1d9 2a 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  *     sqlite3Exp
1c1da 72 49 73 43 6f 6e 73 74 61 6e 74 28 29 0a 2a 2a  rIsConstant().**
1c1db 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1c1dc 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69  IsConstantNotJoi
1c1dd 6e 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  n().**     sqlit
1c1de 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74  e3ExprIsConstant
1c1df 4f 72 46 75 6e 63 74 69 6f 6e 28 29 0a 2a 2a 0a  OrFunction().**.
1c1e0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78  */.static int ex
1c1e1 70 72 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74  prNodeIsConstant
1c1e2 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72  (Walker *pWalker
1c1e3 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a  , Expr *pExpr){.
1c1e4 0a 20 20 2f 2a 20 49 66 20 70 57 61 6c 6b 65 72  .  /* If pWalker
1c1e5 2d 3e 75 2e 69 20 69 73 20 33 20 74 68 65 6e 20  ->u.i is 3 then 
1c1e6 61 6e 79 20 74 65 72 6d 20 6f 66 20 74 68 65 20  any term of the 
1c1e7 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20  expression that 
1c1e8 63 6f 6d 65 73 20 66 72 6f 6d 0a 20 20 2a 2a 20  comes from.  ** 
1c1e9 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20  the ON or USING 
1c1ea 63 6c 61 75 73 65 73 20 6f 66 20 61 20 6a 6f 69  clauses of a joi
1c1eb 6e 20 64 69 73 71 75 61 6c 69 66 69 65 73 20 74  n disqualifies t
1c1ec 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20  he expression.  
1c1ed 2a 2a 20 66 72 6f 6d 20 62 65 69 6e 67 20 63 6f  ** from being co
1c1ee 6e 73 69 64 65 72 65 64 20 63 6f 6e 73 74 61 6e  nsidered constan
1c1ef 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 57 61 6c  t. */.  if( pWal
1c1f0 6b 65 72 2d 3e 75 2e 69 3d 3d 33 20 26 26 20 45  ker->u.i==3 && E
1c1f1 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74  xprHasAnyPropert
1c1f2 79 28 70 45 78 70 72 2c 20 45 50 5f 46 72 6f 6d  y(pExpr, EP_From
1c1f3 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 20 70 57 61  Join) ){.    pWa
1c1f4 6c 6b 65 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20  lker->u.i = 0;. 
1c1f5 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62     return WRC_Ab
1c1f6 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 74  ort;.  }..  swit
1c1f7 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b  ch( pExpr->op ){
1c1f8 0a 20 20 20 20 2f 2a 20 43 6f 6e 73 69 64 65 72  .    /* Consider
1c1f9 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 62 65   functions to be
1c1fa 20 63 6f 6e 73 74 61 6e 74 20 69 66 20 61 6c 6c   constant if all
1c1fb 20 74 68 65 69 72 20 61 72 67 75 6d 65 6e 74 73   their arguments
1c1fc 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 0a 20 20   are constant.  
1c1fd 20 20 2a 2a 20 61 6e 64 20 70 57 61 6c 6b 65 72    ** and pWalker
1c1fe 2d 3e 75 2e 69 3d 3d 32 20 2a 2f 0a 20 20 20 20  ->u.i==2 */.    
1c1ff 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e  case TK_FUNCTION
1c200 3a 0a 20 20 20 20 20 20 69 66 28 20 70 57 61 6c  :.      if( pWal
1c201 6b 65 72 2d 3e 75 2e 69 3d 3d 32 20 29 20 72 65  ker->u.i==2 ) re
1c202 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 2f 2a  turn 0;.      /*
1c203 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f   Fall through */
1c204 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 44 3a  .    case TK_ID:
1c205 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c  .    case TK_COL
1c206 55 4d 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  UMN:.    case TK
1c207 5f 44 4f 54 3a 0a 20 20 20 20 63 61 73 65 20 54  _DOT:.    case T
1c208 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a  K_AGG_FUNCTION:.
1c209 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f      case TK_AGG_
1c20a 43 4f 4c 55 4d 4e 3a 0a 23 69 66 6e 64 65 66 20  COLUMN:.#ifndef 
1c20b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
1c20c 55 45 52 59 0a 20 20 20 20 63 61 73 65 20 54 4b  UERY.    case TK
1c20d 5f 53 45 4c 45 43 54 3a 0a 20 20 20 20 63 61 73  _SELECT:.    cas
1c20e 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a 20 20 20  e TK_EXISTS:.   
1c20f 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78     testcase( pEx
1c210 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43  pr->op==TK_SELEC
1c211 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  T );.      testc
1c212 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d  ase( pExpr->op==
1c213 54 4b 5f 45 58 49 53 54 53 20 29 3b 0a 23 65 6e  TK_EXISTS );.#en
1c214 64 69 66 0a 20 20 20 20 20 20 74 65 73 74 63 61  dif.      testca
1c215 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  se( pExpr->op==T
1c216 4b 5f 49 44 20 29 3b 0a 20 20 20 20 20 20 74 65  K_ID );.      te
1c217 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f  stcase( pExpr->o
1c218 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a  p==TK_COLUMN );.
1c219 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1c21a 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f  pExpr->op==TK_DO
1c21b 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  T );.      testc
1c21c 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d  ase( pExpr->op==
1c21d 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20  TK_AGG_FUNCTION 
1c21e 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1c21f 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  e( pExpr->op==TK
1c220 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20  _AGG_COLUMN );. 
1c221 20 20 20 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e       pWalker->u.
1c222 69 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74  i = 0;.      ret
1c223 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
1c224 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20     default:.    
1c225 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e    return WRC_Con
1c226 74 69 6e 75 65 3b 0a 20 20 7d 0a 7d 0a 73 74 61  tinue;.  }.}.sta
1c227 74 69 63 20 69 6e 74 20 73 65 6c 65 63 74 4e 6f  tic int selectNo
1c228 64 65 49 73 43 6f 6e 73 74 61 6e 74 28 57 61 6c  deIsConstant(Wal
1c229 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65  ker *pWalker, Se
1c22a 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a  lect *pSelect){.
1c22b 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d    pWalker->u.i =
1c22c 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 57 52 43   0;.  return WRC
1c22d 5f 41 62 6f 72 74 3b 0a 7d 0a 73 74 61 74 69 63  _Abort;.}.static
1c22e 20 69 6e 74 20 65 78 70 72 49 73 43 6f 6e 73 74   int exprIsConst
1c22f 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 69 6e  (Expr *p, int in
1c230 69 74 46 6c 61 67 29 7b 0a 20 20 57 61 6c 6b 65  itFlag){.  Walke
1c231 72 20 77 3b 0a 20 20 77 2e 75 2e 69 20 3d 20 69  r w;.  w.u.i = i
1c232 6e 69 74 46 6c 61 67 3b 0a 20 20 77 2e 78 45 78  nitFlag;.  w.xEx
1c233 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65 78 70  prCallback = exp
1c234 72 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 3b  rNodeIsConstant;
1c235 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c  .  w.xSelectCall
1c236 62 61 63 6b 20 3d 20 73 65 6c 65 63 74 4e 6f 64  back = selectNod
1c237 65 49 73 43 6f 6e 73 74 61 6e 74 3b 0a 20 20 73  eIsConstant;.  s
1c238 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26  qlite3WalkExpr(&
1c239 77 2c 20 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  w, p);.  return 
1c23a 77 2e 75 2e 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  w.u.i;.}../*.** 
1c23b 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 69  Walk an expressi
1c23c 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e  on tree.  Return
1c23d 20 31 20 69 66 20 74 68 65 20 65 78 70 72 65 73   1 if the expres
1c23e 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e 74  sion is constant
1c23f 0a 2a 2a 20 61 6e 64 20 30 20 69 66 20 69 74 20  .** and 0 if it 
1c240 69 6e 76 6f 6c 76 65 73 20 76 61 72 69 61 62 6c  involves variabl
1c241 65 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e 20 63  es or function c
1c242 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  alls..**.** For 
1c243 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20  the purposes of 
1c244 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61  this function, a
1c245 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 73   double-quoted s
1c246 74 72 69 6e 67 20 28 65 78 3a 20 22 61 62 63 22  tring (ex: "abc"
1c247 29 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72  ).** is consider
1c248 65 64 20 61 20 76 61 72 69 61 62 6c 65 20 62 75  ed a variable bu
1c249 74 20 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65  t a single-quote
1c24a 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20 27 61  d string (ex: 'a
1c24b 62 63 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f 6e  bc') is.** a con
1c24c 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  stant..*/.SQLITE
1c24d 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1c24e 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61  ite3ExprIsConsta
1c24f 6e 74 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 72  nt(Expr *p){.  r
1c250 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f 6e 73  eturn exprIsCons
1c251 74 28 70 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  t(p, 1);.}../*.*
1c252 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73  * Walk an expres
1c253 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75  sion tree.  Retu
1c254 72 6e 20 31 20 69 66 20 74 68 65 20 65 78 70 72  rn 1 if the expr
1c255 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61  ession is consta
1c256 6e 74 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20  nt.** that does 
1c257 6e 6f 20 6f 72 69 67 69 6e 61 74 65 20 66 72 6f  no originate fro
1c258 6d 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e  m the ON or USIN
1c259 47 20 63 6c 61 75 73 65 73 20 6f 66 20 61 20 6a  G clauses of a j
1c25a 6f 69 6e 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 30  oin..** Return 0
1c25b 20 69 66 20 69 74 20 69 6e 76 6f 6c 76 65 73 20   if it involves 
1c25c 76 61 72 69 61 62 6c 65 73 20 6f 72 20 66 75 6e  variables or fun
1c25d 63 74 69 6f 6e 20 63 61 6c 6c 73 20 6f 72 20 74  ction calls or t
1c25e 65 72 6d 73 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20  erms from.** an 
1c25f 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75  ON or USING clau
1c260 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  se..*/.SQLITE_PR
1c261 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1c262 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e  3ExprIsConstantN
1c263 6f 74 4a 6f 69 6e 28 45 78 70 72 20 2a 70 29 7b  otJoin(Expr *p){
1c264 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 49 73  .  return exprIs
1c265 43 6f 6e 73 74 28 70 2c 20 33 29 3b 0a 7d 0a 0a  Const(p, 3);.}..
1c266 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78  /*.** Walk an ex
1c267 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20  pression tree.  
1c268 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20  Return 1 if the 
1c269 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f  expression is co
1c26a 6e 73 74 61 6e 74 0a 2a 2a 20 6f 72 20 61 20 66  nstant.** or a f
1c26b 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 77 69 74  unction call wit
1c26c 68 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d  h constant argum
1c26d 65 6e 74 73 2e 20 20 52 65 74 75 72 6e 20 61 6e  ents.  Return an
1c26e 64 20 30 20 69 66 20 74 68 65 72 65 0a 2a 2a 20  d 0 if there.** 
1c26f 61 72 65 20 61 6e 79 20 76 61 72 69 61 62 6c 65  are any variable
1c270 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65  s..**.** For the
1c271 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69   purposes of thi
1c272 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64 6f  s function, a do
1c273 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69  uble-quoted stri
1c274 6e 67 20 28 65 78 3a 20 22 61 62 63 22 29 0a 2a  ng (ex: "abc").*
1c275 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  * is considered 
1c276 61 20 76 61 72 69 61 62 6c 65 20 62 75 74 20 61  a variable but a
1c277 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 64 20 73   single-quoted s
1c278 74 72 69 6e 67 20 28 65 78 3a 20 27 61 62 63 27  tring (ex: 'abc'
1c279 29 20 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 74 61  ) is.** a consta
1c27a 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nt..*/.SQLITE_PR
1c27b 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1c27c 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f  3ExprIsConstantO
1c27d 72 46 75 6e 63 74 69 6f 6e 28 45 78 70 72 20 2a  rFunction(Expr *
1c27e 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 70  p){.  return exp
1c27f 72 49 73 43 6f 6e 73 74 28 70 2c 20 32 29 3b 0a  rIsConst(p, 2);.
1c280 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  }../*.** If the 
1c281 65 78 70 72 65 73 73 69 6f 6e 20 70 20 63 6f 64  expression p cod
1c282 65 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 69 6e  es a constant in
1c283 74 65 67 65 72 20 74 68 61 74 20 69 73 20 73 6d  teger that is sm
1c284 61 6c 6c 20 65 6e 6f 75 67 68 0a 2a 2a 20 74 6f  all enough.** to
1c285 20 66 69 74 20 69 6e 20 61 20 33 32 2d 62 69 74   fit in a 32-bit
1c286 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e   integer, return
1c287 20 31 20 61 6e 64 20 70 75 74 20 74 68 65 20 76   1 and put the v
1c288 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65  alue of the inte
1c289 67 65 72 0a 2a 2a 20 69 6e 20 2a 70 56 61 6c 75  ger.** in *pValu
1c28a 65 2e 20 20 49 66 20 74 68 65 20 65 78 70 72 65  e.  If the expre
1c28b 73 73 69 6f 6e 20 69 73 20 6e 6f 74 20 61 6e 20  ssion is not an 
1c28c 69 6e 74 65 67 65 72 20 6f 72 20 69 66 20 69 74  integer or if it
1c28d 20 69 73 20 74 6f 6f 20 62 69 67 0a 2a 2a 20 74   is too big.** t
1c28e 6f 20 66 69 74 20 69 6e 20 61 20 73 69 67 6e 65  o fit in a signe
1c28f 64 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72  d 32-bit integer
1c290 2c 20 72 65 74 75 72 6e 20 30 20 61 6e 64 20 6c  , return 0 and l
1c291 65 61 76 65 20 2a 70 56 61 6c 75 65 20 75 6e 63  eave *pValue unc
1c292 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  hanged..*/.SQLIT
1c293 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1c294 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67  lite3ExprIsInteg
1c295 65 72 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20  er(Expr *p, int 
1c296 2a 70 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20  *pValue){.  int 
1c297 72 63 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 2d  rc = 0;.  if( p-
1c298 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 74 56  >flags & EP_IntV
1c299 61 6c 75 65 20 29 7b 0a 20 20 20 20 2a 70 56 61  alue ){.    *pVa
1c29a 6c 75 65 20 3d 20 70 2d 3e 69 54 61 62 6c 65 3b  lue = p->iTable;
1c29b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
1c29c 20 7d 0a 20 20 73 77 69 74 63 68 28 20 70 2d 3e   }.  switch( p->
1c29d 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54  op ){.    case T
1c29e 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20  K_INTEGER: {.   
1c29f 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 47     rc = sqlite3G
1c2a0 65 74 49 6e 74 33 32 28 28 63 68 61 72 2a 29 70  etInt32((char*)p
1c2a1 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 56 61 6c 75  ->token.z, pValu
1c2a2 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  e);.      break;
1c2a3 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1c2a4 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a 20 20 20 20  TK_UPLUS: {.    
1c2a5 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 45 78    rc = sqlite3Ex
1c2a6 70 72 49 73 49 6e 74 65 67 65 72 28 70 2d 3e 70  prIsInteger(p->p
1c2a7 4c 65 66 74 2c 20 70 56 61 6c 75 65 29 3b 0a 20  Left, pValue);. 
1c2a8 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1c2a9 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 4d  }.    case TK_UM
1c2aa 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e  INUS: {.      in
1c2ab 74 20 76 3b 0a 20 20 20 20 20 20 69 66 28 20 73  t v;.      if( s
1c2ac 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65  qlite3ExprIsInte
1c2ad 67 65 72 28 70 2d 3e 70 4c 65 66 74 2c 20 26 76  ger(p->pLeft, &v
1c2ae 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 56  ) ){.        *pV
1c2af 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 20 20 20 20  alue = -v;.     
1c2b0 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 20     rc = 1;.     
1c2b1 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
1c2b2 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
1c2b3 74 3a 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20  t: break;.  }.  
1c2b4 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 70 2d  if( rc ){.    p-
1c2b5 3e 6f 70 20 3d 20 54 4b 5f 49 4e 54 45 47 45 52  >op = TK_INTEGER
1c2b6 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 7c  ;.    p->flags |
1c2b7 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 3b 0a 20  = EP_IntValue;. 
1c2b8 20 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 2a     p->iTable = *
1c2b9 70 56 61 6c 75 65 3b 0a 20 20 7d 0a 20 20 72 65  pValue;.  }.  re
1c2ba 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1c2bb 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66  * Return TRUE if
1c2bc 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e   the given strin
1c2bd 67 20 69 73 20 61 20 72 6f 77 2d 69 64 20 63 6f  g is a row-id co
1c2be 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51  lumn name..*/.SQ
1c2bf 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1c2c0 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28   sqlite3IsRowid(
1c2c1 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a  const char *z){.
1c2c2 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
1c2c3 49 43 6d 70 28 7a 2c 20 22 5f 52 4f 57 49 44 5f  ICmp(z, "_ROWID_
1c2c4 22 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31  ")==0 ) return 1
1c2c5 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ;.  if( sqlite3S
1c2c6 74 72 49 43 6d 70 28 7a 2c 20 22 52 4f 57 49 44  trICmp(z, "ROWID
1c2c7 22 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31  ")==0 ) return 1
1c2c8 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ;.  if( sqlite3S
1c2c9 74 72 49 43 6d 70 28 7a 2c 20 22 4f 49 44 22 29  trICmp(z, "OID")
1c2ca 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  ==0 ) return 1;.
1c2cb 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 23    return 0;.}..#
1c2cc 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
1c2cd 54 0a 20 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  T.  int sqlite3_
1c2ce 65 6e 61 62 6c 65 5f 69 6e 5f 6f 70 74 20 3d 20  enable_in_opt = 
1c2cf 31 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69  1;.#else.  #defi
1c2d0 6e 65 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c  ne sqlite3_enabl
1c2d1 65 5f 69 6e 5f 6f 70 74 20 31 0a 23 65 6e 64 69  e_in_opt 1.#endi
1c2d2 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  f../*.** Return 
1c2d3 74 72 75 65 20 69 66 20 74 68 65 20 49 4e 20 6f  true if the IN o
1c2d4 70 65 72 61 74 6f 72 20 6f 70 74 69 6d 69 7a 61  perator optimiza
1c2d5 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 20  tion is enabled 
1c2d6 61 6e 64 0a 2a 2a 20 74 68 65 20 53 45 4c 45 43  and.** the SELEC
1c2d7 54 20 73 74 61 74 65 6d 65 6e 74 20 70 20 65 78  T statement p ex
1c2d8 69 73 74 73 20 61 6e 64 20 69 73 20 6f 66 20 74  ists and is of t
1c2d9 68 65 0a 2a 2a 20 73 69 6d 70 6c 65 20 66 6f 72  he.** simple for
1c2da 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c  m:.**.**     SEL
1c2db 45 43 54 20 3c 63 6f 6c 75 6d 6e 3e 20 46 52 4f  ECT <column> FRO
1c2dc 4d 20 3c 74 61 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20  M <table>.**.** 
1c2dd 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63  If this is the c
1c2de 61 73 65 2c 20 69 74 20 6d 61 79 20 62 65 20 70  ase, it may be p
1c2df 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65 20 61  ossible to use a
1c2e0 6e 20 65 78 69 73 74 69 6e 67 20 74 61 62 6c 65  n existing table
1c2e1 0a 2a 2a 20 6f 72 20 69 6e 64 65 78 20 69 6e 73  .** or index ins
1c2e2 74 65 61 64 20 6f 66 20 67 65 6e 65 72 61 74 69  tead of generati
1c2e3 6e 67 20 61 6e 20 65 70 68 65 72 65 6d 61 6c 20  ng an epheremal 
1c2e4 74 61 62 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65  table..*/.#ifnde
1c2e5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  f SQLITE_OMIT_SU
1c2e6 42 51 55 45 52 59 0a 73 74 61 74 69 63 20 69 6e  BQUERY.static in
1c2e7 74 20 69 73 43 61 6e 64 69 64 61 74 65 46 6f 72  t isCandidateFor
1c2e8 49 6e 4f 70 74 28 53 65 6c 65 63 74 20 2a 70 29  InOpt(Select *p)
1c2e9 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  {.  SrcList *pSr
1c2ea 63 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  c;.  ExprList *p
1c2eb 45 4c 69 73 74 3b 0a 20 20 54 61 62 6c 65 20 2a  EList;.  Table *
1c2ec 70 54 61 62 3b 0a 20 20 69 66 28 20 21 73 71 6c  pTab;.  if( !sql
1c2ed 69 74 65 33 5f 65 6e 61 62 6c 65 5f 69 6e 5f 6f  ite3_enable_in_o
1c2ee 70 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20 2f  pt ) return 0; /
1c2ef 2a 20 49 4e 20 6f 70 74 69 6d 69 7a 61 74 69 6f  * IN optimizatio
1c2f0 6e 20 6d 75 73 74 20 62 65 20 65 6e 61 62 6c 65  n must be enable
1c2f1 64 20 2a 2f 0a 20 20 69 66 28 20 70 3d 3d 30 20  d */.  if( p==0 
1c2f2 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20  ) return 0;     
1c2f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1c2f4 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65   right-hand side
1c2f5 20 6f 66 20 49 4e 20 69 73 20 53 45 4c 45 43 54   of IN is SELECT
1c2f6 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 50 72   */.  if( p->pPr
1c2f7 69 6f 72 20 29 20 72 65 74 75 72 6e 20 30 3b 20  ior ) return 0; 
1c2f8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c2f9 4e 6f 74 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53  Not a compound S
1c2fa 45 4c 45 43 54 20 2a 2f 0a 20 20 69 66 28 20 70  ELECT */.  if( p
1c2fb 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53 46  ->selFlags & (SF
1c2fc 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 67  _Distinct|SF_Agg
1c2fd 72 65 67 61 74 65 29 20 29 7b 0a 20 20 20 20 20  regate) ){.     
1c2fe 20 72 65 74 75 72 6e 20 30 3b 20 2f 2a 20 4e 6f   return 0; /* No
1c2ff 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72   DISTINCT keywor
1c300 64 20 61 6e 64 20 6e 6f 20 61 67 67 72 65 67 61  d and no aggrega
1c301 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a  te functions */.
1c302 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 47 72    }.  if( p->pGr
1c303 6f 75 70 42 79 20 29 20 72 65 74 75 72 6e 20 30  oupBy ) return 0
1c304 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
1c305 48 61 73 20 6e 6f 20 47 52 4f 55 50 20 42 59 20  Has no GROUP BY 
1c306 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 66 28 20  clause */.  if( 
1c307 70 2d 3e 70 4c 69 6d 69 74 20 29 20 72 65 74 75  p->pLimit ) retu
1c308 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20  rn 0;           
1c309 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 4c 49 4d     /* Has no LIM
1c30a 49 54 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69  IT clause */.  i
1c30b 66 28 20 70 2d 3e 70 4f 66 66 73 65 74 20 29 20  f( p->pOffset ) 
1c30c 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
1c30d 70 2d 3e 70 57 68 65 72 65 20 29 20 72 65 74 75  p->pWhere ) retu
1c30e 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20  rn 0;           
1c30f 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 57 48 45     /* Has no WHE
1c310 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 70  RE clause */.  p
1c311 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20  Src = p->pSrc;. 
1c312 20 69 66 28 20 70 53 72 63 3d 3d 30 20 29 20 72   if( pSrc==0 ) r
1c313 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20  eturn 0;        
1c314 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e          /* A sin
1c315 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65  gle table in the
1c316 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a   FROM clause */.
1c317 20 20 69 66 28 20 70 53 72 63 2d 3e 6e 53 72 63    if( pSrc->nSrc
1c318 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  !=1 ) return 0;.
1c319 20 20 69 66 28 20 70 53 72 63 2d 3e 61 5b 30 5d    if( pSrc->a[0]
1c31a 2e 70 53 65 6c 65 63 74 20 29 20 72 65 74 75 72  .pSelect ) retur
1c31b 6e 20 30 3b 20 20 20 20 20 2f 2a 20 46 52 4f 4d  n 0;     /* FROM
1c31c 20 63 6c 61 75 73 65 20 69 73 20 6e 6f 74 20 61   clause is not a
1c31d 20 73 75 62 71 75 65 72 79 20 2a 2f 0a 20 20 70   subquery */.  p
1c31e 54 61 62 20 3d 20 70 53 72 63 2d 3e 61 5b 30 5d  Tab = pSrc->a[0]
1c31f 2e 70 54 61 62 3b 0a 20 20 69 66 28 20 70 54 61  .pTab;.  if( pTa
1c320 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  b==0 ) return 0;
1c321 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65  .  if( pTab->pSe
1c322 6c 65 63 74 20 29 20 72 65 74 75 72 6e 20 30 3b  lect ) return 0;
1c323 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 52 4f            /* FRO
1c324 4d 20 63 6c 61 75 73 65 20 69 73 20 6e 6f 74 20  M clause is not 
1c325 61 20 76 69 65 77 20 2a 2f 0a 20 20 69 66 28 20  a view */.  if( 
1c326 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
1c327 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20  ) return 0;     
1c328 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73     /* FROM claus
1c329 65 20 6e 6f 74 20 61 20 76 69 72 74 75 61 6c 20  e not a virtual 
1c32a 74 61 62 6c 65 20 2a 2f 0a 20 20 70 45 4c 69 73  table */.  pELis
1c32b 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20  t = p->pEList;. 
1c32c 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 6e 45 78   if( pEList->nEx
1c32d 70 72 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30  pr!=1 ) return 0
1c32e 3b 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63  ;       /* One c
1c32f 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 73  olumn in the res
1c330 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 69 66 28  ult set */.  if(
1c331 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45   pEList->a[0].pE
1c332 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55  xpr->op!=TK_COLU
1c333 4d 4e 20 29 20 72 65 74 75 72 6e 20 30 3b 20 2f  MN ) return 0; /
1c334 2a 20 52 65 73 75 6c 74 20 69 73 20 61 20 63 6f  * Result is a co
1c335 6c 75 6d 6e 20 2a 2f 0a 20 20 72 65 74 75 72 6e  lumn */.  return
1c336 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20   1;.}.#endif /* 
1c337 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
1c338 55 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  UERY */../*.** T
1c339 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
1c33a 75 73 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c  used by the impl
1c33b 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
1c33c 65 20 49 4e 20 28 2e 2e 2e 29 20 6f 70 65 72 61  e IN (...) opera
1c33d 74 6f 72 2e 0a 2a 2a 20 49 74 27 73 20 6a 6f 62  tor..** It's job
1c33e 20 69 73 20 74 6f 20 66 69 6e 64 20 6f 72 20 63   is to find or c
1c33f 72 65 61 74 65 20 61 20 62 2d 74 72 65 65 20 73  reate a b-tree s
1c340 74 72 75 63 74 75 72 65 20 74 68 61 74 20 6d 61  tructure that ma
1c341 79 20 62 65 20 75 73 65 64 0a 2a 2a 20 65 69 74  y be used.** eit
1c342 68 65 72 20 74 6f 20 74 65 73 74 20 66 6f 72 20  her to test for 
1c343 6d 65 6d 62 65 72 73 68 69 70 20 6f 66 20 74 68  membership of th
1c344 65 20 28 2e 2e 2e 29 20 73 65 74 20 6f 72 20 74  e (...) set or t
1c345 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67  o iterate throug
1c346 68 0a 2a 2a 20 69 74 73 20 6d 65 6d 62 65 72 73  h.** its members
1c347 2c 20 73 6b 69 70 70 69 6e 67 20 64 75 70 6c 69  , skipping dupli
1c348 63 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  cates..**.** The
1c349 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 6f   cursor opened o
1c34a 6e 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20  n the structure 
1c34b 28 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2c  (database table,
1c34c 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 20   database index 
1c34d 0a 2a 2a 20 6f 72 20 65 70 68 65 72 6d 61 6c 20  .** or ephermal 
1c34e 74 61 62 6c 65 29 20 69 73 20 73 74 6f 72 65 64  table) is stored
1c34f 20 69 6e 20 70 58 2d 3e 69 54 61 62 6c 65 20 62   in pX->iTable b
1c350 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74  efore this funct
1c351 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 20  ion returns..** 
1c352 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c  The returned val
1c353 75 65 20 69 6e 64 69 63 61 74 65 73 20 74 68 65  ue indicates the
1c354 20 73 74 72 75 63 74 75 72 65 20 74 79 70 65 2c   structure type,
1c355 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
1c356 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 52 4f  **   IN_INDEX_RO
1c357 57 49 44 20 2d 20 54 68 65 20 63 75 72 73 6f 72  WID - The cursor
1c358 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61   was opened on a
1c359 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e   database table.
1c35a 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 49  .**   IN_INDEX_I
1c35b 4e 44 45 58 20 2d 20 54 68 65 20 63 75 72 73 6f  NDEX - The curso
1c35c 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20  r was opened on 
1c35d 61 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78  a database index
1c35e 2e 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f  ..**   IN_INDEX_
1c35f 45 50 48 20 2d 20 20 20 54 68 65 20 63 75 72 73  EPH -   The curs
1c360 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e  or was opened on
1c361 20 61 20 73 70 65 63 69 61 6c 6c 79 20 63 72 65   a specially cre
1c362 61 74 65 64 20 61 6e 64 0a 2a 2a 20 20 20 20 20  ated and.**     
1c363 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
1c364 6f 70 75 6c 61 74 65 64 20 65 70 68 65 72 65 6d  opulated epherem
1c365 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  al table..**.** 
1c366 41 6e 20 65 78 69 73 74 69 6e 67 20 73 74 72 75  An existing stru
1c367 63 74 75 72 65 20 6d 61 79 20 6f 6e 6c 79 20 62  cture may only b
1c368 65 20 75 73 65 64 20 69 66 20 74 68 65 20 53 45  e used if the SE
1c369 4c 45 43 54 20 69 73 20 6f 66 20 74 68 65 20 73  LECT is of the s
1c36a 69 6d 70 6c 65 0a 2a 2a 20 66 6f 72 6d 3a 0a 2a  imple.** form:.*
1c36b 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20  *.**     SELECT 
1c36c 3c 63 6f 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74  <column> FROM <t
1c36d 61 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 70  able>.**.** If p
1c36e 72 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61 6d 65  rNotFound parame
1c36f 74 65 72 20 69 73 20 30 2c 20 74 68 65 6e 20 74  ter is 0, then t
1c370 68 65 20 73 74 72 75 63 74 75 72 65 20 77 69 6c  he structure wil
1c371 6c 20 62 65 20 75 73 65 64 20 74 6f 20 69 74 65  l be used to ite
1c372 72 61 74 65 0a 2a 2a 20 74 68 72 6f 75 67 68 20  rate.** through 
1c373 74 68 65 20 73 65 74 20 6d 65 6d 62 65 72 73 2c  the set members,
1c374 20 73 6b 69 70 70 69 6e 67 20 61 6e 79 20 64 75   skipping any du
1c375 70 6c 69 63 61 74 65 73 2e 20 49 6e 20 74 68 69  plicates. In thi
1c376 73 20 63 61 73 65 20 61 6e 0a 2a 2a 20 65 70 68  s case an.** eph
1c377 65 72 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75 73  eremal table mus
1c378 74 20 62 65 20 75 73 65 64 20 75 6e 6c 65 73 73  t be used unless
1c379 20 74 68 65 20 73 65 6c 65 63 74 65 64 20 3c 63   the selected <c
1c37a 6f 6c 75 6d 6e 3e 20 69 73 20 67 75 61 72 61 6e  olumn> is guaran
1c37b 74 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20 75 6e  teed.** to be un
1c37c 69 71 75 65 20 2d 20 65 69 74 68 65 72 20 62 65  ique - either be
1c37d 63 61 75 73 65 20 69 74 20 69 73 20 61 6e 20 49  cause it is an I
1c37e 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
1c37f 45 59 20 6f 72 20 69 74 0a 2a 2a 20 69 73 20 75  EY or it.** is u
1c380 6e 69 71 75 65 20 62 79 20 76 69 72 74 75 65 20  nique by virtue 
1c381 6f 66 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20  of a constraint 
1c382 6f 72 20 69 6d 70 6c 69 63 69 74 20 69 6e 64 65  or implicit inde
1c383 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  x..**.** If the 
1c384 70 72 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61 6d  prNotFound param
1c385 65 74 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 74  eter is not 0, t
1c386 68 65 6e 20 74 68 65 20 73 74 72 75 63 74 75 72  hen the structur
1c387 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 0a  e will be used .
1c388 2a 2a 20 66 6f 72 20 66 61 73 74 20 73 65 74 20  ** for fast set 
1c389 6d 65 6d 62 65 72 73 68 69 70 20 74 65 73 74 73  membership tests
1c38a 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 61  . In this case a
1c38b 6e 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62 6c  n epheremal tabl
1c38c 65 20 6d 75 73 74 20 0a 2a 2a 20 62 65 20 75 73  e must .** be us
1c38d 65 64 20 75 6e 6c 65 73 73 20 3c 63 6f 6c 75 6d  ed unless <colum
1c38e 6e 3e 20 69 73 20 61 6e 20 49 4e 54 45 47 45 52  n> is an INTEGER
1c38f 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20   PRIMARY KEY or 
1c390 61 6e 20 69 6e 64 65 78 20 63 61 6e 20 0a 2a 2a  an index can .**
1c391 20 62 65 20 66 6f 75 6e 64 20 77 69 74 68 20 3c   be found with <
1c392 63 6f 6c 75 6d 6e 3e 20 61 73 20 69 74 73 20 6c  column> as its l
1c393 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 2e  eft-most column.
1c394 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20  .**.** When the 
1c395 73 74 72 75 63 74 75 72 65 20 69 73 20 62 65 69  structure is bei
1c396 6e 67 20 75 73 65 64 20 66 6f 72 20 73 65 74 20  ng used for set 
1c397 6d 65 6d 62 65 72 73 68 69 70 20 74 65 73 74 73  membership tests
1c398 2c 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 6e 65  , the user.** ne
1c399 65 64 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 74  eds to know whet
1c39a 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 73  her or not the s
1c39b 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
1c39c 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 0a 2a  s an SQL NULL .*
1c39d 2a 20 76 61 6c 75 65 20 69 6e 20 6f 72 64 65 72  * value in order
1c39e 20 74 6f 20 63 6f 72 72 65 63 74 6c 79 20 65 76   to correctly ev
1c39f 61 6c 75 61 74 65 20 65 78 70 72 65 73 73 69 6f  aluate expressio
1c3a0 6e 73 20 6c 69 6b 65 20 22 58 20 49 4e 20 28 59  ns like "X IN (Y
1c3a1 2c 20 5a 29 22 2e 0a 2a 2a 20 49 66 20 74 68 65  , Z)"..** If the
1c3a2 72 65 20 69 73 20 61 20 63 68 61 6e 63 65 20 74  re is a chance t
1c3a3 68 61 74 20 74 68 65 20 73 74 72 75 63 74 75 72  hat the structur
1c3a4 65 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61 20  e may contain a 
1c3a5 4e 55 4c 4c 20 76 61 6c 75 65 20 61 74 0a 2a 2a  NULL value at.**
1c3a6 20 72 75 6e 74 69 6d 65 2c 20 74 68 65 6e 20 61   runtime, then a
1c3a7 20 72 65 67 69 73 74 65 72 20 69 73 20 61 6c 6c   register is all
1c3a8 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 20 72  ocated and the r
1c3a9 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 77  egister number w
1c3aa 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 72  ritten.** to *pr
1c3ab 4e 6f 74 46 6f 75 6e 64 2e 20 49 66 20 74 68 65  NotFound. If the
1c3ac 72 65 20 69 73 20 6e 6f 20 63 68 61 6e 63 65 20  re is no chance 
1c3ad 74 68 61 74 20 74 68 65 20 73 74 72 75 63 74 75  that the structu
1c3ae 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 0a 2a 2a  re contains a.**
1c3af 20 4e 55 4c 4c 20 76 61 6c 75 65 2c 20 74 68 65   NULL value, the
1c3b0 6e 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 69 73  n *prNotFound is
1c3b1 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e   left unchanged.
1c3b2 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 72 65 67 69  .**.** If a regi
1c3b3 73 74 65 72 20 69 73 20 61 6c 6c 6f 63 61 74 65  ster is allocate
1c3b4 64 20 61 6e 64 20 69 74 73 20 6c 6f 63 61 74 69  d and its locati
1c3b5 6f 6e 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 72  on stored in *pr
1c3b6 4e 6f 74 46 6f 75 6e 64 2c 20 74 68 65 6e 0a 2a  NotFound, then.*
1c3b7 2a 20 69 74 73 20 69 6e 69 74 69 61 6c 20 76 61  * its initial va
1c3b8 6c 75 65 20 69 73 20 4e 55 4c 4c 2e 20 49 66 20  lue is NULL. If 
1c3b9 74 68 65 20 73 74 72 75 63 74 75 72 65 20 64 6f  the structure do
1c3ba 65 73 20 6e 6f 74 20 72 65 6d 61 69 6e 20 63 6f  es not remain co
1c3bb 6e 73 74 61 6e 74 0a 2a 2a 20 66 6f 72 20 74 68  nstant.** for th
1c3bc 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 68  e duration of th
1c3bd 65 20 71 75 65 72 79 20 28 69 2e 65 2e 20 74 68  e query (i.e. th
1c3be 65 20 73 65 74 20 69 73 20 61 20 63 6f 72 72 65  e set is a corre
1c3bf 6c 61 74 65 64 20 73 75 62 2d 73 65 6c 65 63 74  lated sub-select
1c3c0 29 2c 20 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65  ), .** the value
1c3c1 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 65   of the allocate
1c3c2 64 20 72 65 67 69 73 74 65 72 20 69 73 20 72 65  d register is re
1c3c3 73 65 74 20 74 6f 20 4e 55 4c 4c 20 65 61 63 68  set to NULL each
1c3c4 20 74 69 6d 65 20 74 68 65 20 0a 2a 2a 20 73 74   time the .** st
1c3c5 72 75 63 74 75 72 65 20 69 73 20 72 65 70 6f 70  ructure is repop
1c3c6 75 6c 61 74 65 64 2e 20 54 68 69 73 20 61 6c 6c  ulated. This all
1c3c7 6f 77 73 20 74 68 65 20 63 61 6c 6c 65 72 20 74  ows the caller t
1c3c8 6f 20 75 73 65 20 76 64 62 65 20 63 6f 64 65 20  o use vdbe code 
1c3c9 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 74  .** equivalent t
1c3ca 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  o the following:
1c3cb 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 72 65 67  .**.**   if( reg
1c3cc 69 73 74 65 72 3d 3d 4e 55 4c 4c 20 29 7b 0a 2a  ister==NULL ){.*
1c3cd 2a 20 20 20 20 20 68 61 73 5f 6e 75 6c 6c 20 3d  *     has_null =
1c3ce 20 3c 74 65 73 74 20 69 66 20 64 61 74 61 20 73   <test if data s
1c3cf 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
1c3d0 73 20 6e 75 6c 6c 3e 0a 2a 2a 20 20 20 20 20 72  s null>.**     r
1c3d1 65 67 69 73 74 65 72 20 3d 20 31 0a 2a 2a 20 20  egister = 1.**  
1c3d2 20 7d 0a 2a 2a 0a 2a 2a 20 69 6e 20 6f 72 64 65   }.**.** in orde
1c3d3 72 20 74 6f 20 61 76 6f 69 64 20 72 75 6e 6e 69  r to avoid runni
1c3d4 6e 67 20 74 68 65 20 3c 74 65 73 74 20 69 66 20  ng the <test if 
1c3d5 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 63  data structure c
1c3d6 6f 6e 74 61 69 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a  ontains null>.**
1c3d7 20 74 65 73 74 20 6d 6f 72 65 20 6f 66 74 65 6e   test more often
1c3d8 20 74 68 61 6e 20 69 73 20 6e 65 63 65 73 73 61   than is necessa
1c3d9 72 79 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ry..*/.#ifndef S
1c3da 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
1c3db 45 52 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ERY.SQLITE_PRIVA
1c3dc 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69  TE int sqlite3Fi
1c3dd 6e 64 49 6e 49 6e 64 65 78 28 50 61 72 73 65 20  ndInIndex(Parse 
1c3de 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
1c3df 58 2c 20 69 6e 74 20 2a 70 72 4e 6f 74 46 6f 75  X, int *prNotFou
1c3e0 6e 64 29 7b 0a 20 20 53 65 6c 65 63 74 20 2a 70  nd){.  Select *p
1c3e1 3b 0a 20 20 69 6e 74 20 65 54 79 70 65 20 3d 20  ;.  int eType = 
1c3e2 30 3b 0a 20 20 69 6e 74 20 69 54 61 62 20 3d 20  0;.  int iTab = 
1c3e3 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
1c3e4 20 20 69 6e 74 20 6d 75 73 74 42 65 55 6e 69 71    int mustBeUniq
1c3e5 75 65 20 3d 20 21 70 72 4e 6f 74 46 6f 75 6e 64  ue = !prNotFound
1c3e6 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c  ;..  /* The foll
1c3e7 77 69 6e 67 20 69 66 28 2e 2e 2e 29 20 65 78 70  wing if(...) exp
1c3e8 72 65 73 73 69 6f 6e 20 69 73 20 74 72 75 65 20  ression is true 
1c3e9 69 66 20 74 68 65 20 53 45 4c 45 43 54 20 69 73  if the SELECT is
1c3ea 20 6f 66 20 74 68 65 20 0a 20 20 2a 2a 20 73 69   of the .  ** si
1c3eb 6d 70 6c 65 20 66 6f 72 6d 3a 0a 20 20 2a 2a 0a  mple form:.  **.
1c3ec 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20    **     SELECT 
1c3ed 3c 63 6f 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74  <column> FROM <t
1c3ee 61 62 6c 65 3e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  able>.  **.  ** 
1c3ef 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63  If this is the c
1c3f0 61 73 65 2c 20 69 74 20 6d 61 79 20 62 65 20 70  ase, it may be p
1c3f1 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65 20 61  ossible to use a
1c3f2 6e 20 65 78 69 73 74 69 6e 67 20 74 61 62 6c 65  n existing table
1c3f3 0a 20 20 2a 2a 20 6f 72 20 69 6e 64 65 78 20 69  .  ** or index i
1c3f4 6e 73 74 65 61 64 20 6f 66 20 67 65 6e 65 72 61  nstead of genera
1c3f5 74 69 6e 67 20 61 6e 20 65 70 68 65 72 65 6d 61  ting an epherema
1c3f6 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20  l table..  */.  
1c3f7 70 20 3d 20 70 58 2d 3e 70 53 65 6c 65 63 74 3b  p = pX->pSelect;
1c3f8 0a 20 20 69 66 28 20 69 73 43 61 6e 64 69 64 61  .  if( isCandida
1c3f9 74 65 46 6f 72 49 6e 4f 70 74 28 70 29 20 29 7b  teForInOpt(p) ){
1c3fa 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62  .    sqlite3 *db
1c3fb 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
1c3fc 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a     Index *pIdx;.
1c3fd 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 20      Expr *pExpr 
1c3fe 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30  = p->pEList->a[0
1c3ff 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 69 6e 74  ].pExpr;.    int
1c400 20 69 43 6f 6c 20 3d 20 70 45 78 70 72 2d 3e 69   iCol = pExpr->i
1c401 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 56 64 62 65  Column;.    Vdbe
1c402 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74   *v = sqlite3Get
1c403 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 0a 20  Vdbe(pParse);.. 
1c404 20 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74     /* This funct
1c405 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c  ion is only call
1c406 65 64 20 66 72 6f 6d 20 74 77 6f 20 70 6c 61 63  ed from two plac
1c407 65 73 2e 20 49 6e 20 62 6f 74 68 20 63 61 73 65  es. In both case
1c408 73 20 74 68 65 20 76 64 62 65 0a 20 20 20 20 2a  s the vdbe.    *
1c409 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  * has already be
1c40a 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2e 20 53 6f  en allocated. So
1c40b 20 61 73 73 75 6d 65 20 73 71 6c 69 74 65 33 47   assume sqlite3G
1c40c 65 74 56 64 62 65 28 29 20 69 73 20 61 6c 77 61  etVdbe() is alwa
1c40d 79 73 0a 20 20 20 20 2a 2a 20 73 75 63 63 65 73  ys.    ** succes
1c40e 73 66 75 6c 20 68 65 72 65 2e 0a 20 20 20 20 2a  sful here..    *
1c40f 2f 0a 20 20 20 20 61 73 73 65 72 74 28 76 29 3b  /.    assert(v);
1c410 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20  .    if( iCol<0 
1c411 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 4d 65  ){.      int iMe
1c412 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  m = ++pParse->nM
1c413 65 6d 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 41  em;.      int iA
1c414 64 64 72 3b 0a 20 20 20 20 20 20 54 61 62 6c 65  ddr;.      Table
1c415 20 2a 70 54 61 62 20 3d 20 70 2d 3e 70 53 72 63   *pTab = p->pSrc
1c416 2d 3e 61 5b 30 5d 2e 70 54 61 62 3b 0a 20 20 20  ->a[0].pTab;.   
1c417 20 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c     int iDb = sql
1c418 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
1c419 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  x(db, pTab->pSch
1c41a 65 6d 61 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ema);.      sqli
1c41b 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65  te3VdbeUsesBtree
1c41c 28 76 2c 20 69 44 62 29 3b 0a 0a 20 20 20 20 20  (v, iDb);..     
1c41d 20 69 41 64 64 72 20 3d 20 73 71 6c 69 74 65 33   iAddr = sqlite3
1c41e 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
1c41f 5f 49 66 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20  _If, iMem);.    
1c420 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1c421 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
1c422 72 2c 20 31 2c 20 69 4d 65 6d 29 3b 0a 0a 20 20  r, 1, iMem);..  
1c423 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54      sqlite3OpenT
1c424 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 54 61  able(pParse, iTa
1c425 62 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50  b, iDb, pTab, OP
1c426 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 20 20  _OpenRead);.    
1c427 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44    eType = IN_IND
1c428 45 58 5f 52 4f 57 49 44 3b 0a 0a 20 20 20 20 20  EX_ROWID;..     
1c429 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
1c42a 48 65 72 65 28 76 2c 20 69 41 64 64 72 29 3b 0a  Here(v, iAddr);.
1c42b 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1c42c 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f   /* The collatio
1c42d 6e 20 73 65 71 75 65 6e 63 65 20 75 73 65 64 20  n sequence used 
1c42e 62 79 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  by the compariso
1c42f 6e 2e 20 49 66 20 61 6e 20 69 6e 64 65 78 20 69  n. If an index i
1c430 73 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 62  s to .      ** b
1c431 65 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20  e used in place 
1c432 6f 66 20 61 20 74 65 6d 70 2d 74 61 62 6c 65 2c  of a temp-table,
1c433 20 69 74 20 6d 75 73 74 20 62 65 20 6f 72 64 65   it must be orde
1c434 72 65 64 20 61 63 63 6f 72 64 69 6e 67 0a 20 20  red according.  
1c435 20 20 20 20 2a 2a 20 74 6f 20 74 68 69 73 20 63      ** to this c
1c436 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
1c437 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  e..      */.    
1c438 20 20 43 6f 6c 6c 53 65 71 20 2a 70 52 65 71 20    CollSeq *pReq 
1c439 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43  = sqlite3BinaryC
1c43a 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50  ompareCollSeq(pP
1c43b 61 72 73 65 2c 20 70 58 2d 3e 70 4c 65 66 74 2c  arse, pX->pLeft,
1c43c 20 70 45 78 70 72 29 3b 0a 0a 20 20 20 20 20 20   pExpr);..      
1c43d 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68  /* Check that th
1c43e 65 20 61 66 66 69 6e 69 74 79 20 74 68 61 74 20  e affinity that 
1c43f 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74 6f 20  will be used to 
1c440 70 65 72 66 6f 72 6d 20 74 68 65 20 0a 20 20 20  perform the .   
1c441 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e     ** comparison
1c442 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
1c443 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20  the affinity of 
1c444 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 49 66 0a 20  the column. If. 
1c445 20 20 20 20 20 2a 2a 20 69 74 20 69 73 20 6e 6f       ** it is no
1c446 74 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73  t, it is not pos
1c447 73 69 62 6c 65 20 74 6f 20 75 73 65 20 61 6e 79  sible to use any
1c448 20 69 6e 64 65 78 2e 0a 20 20 20 20 20 20 2a 2f   index..      */
1c449 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54  .      Table *pT
1c44a 61 62 20 3d 20 70 2d 3e 70 53 72 63 2d 3e 61 5b  ab = p->pSrc->a[
1c44b 30 5d 2e 70 54 61 62 3b 0a 20 20 20 20 20 20 63  0].pTab;.      c
1c44c 68 61 72 20 61 66 66 20 3d 20 63 6f 6d 70 61 72  har aff = compar
1c44d 69 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 58 29  isonAffinity(pX)
1c44e 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 66 66 69  ;.      int affi
1c44f 6e 69 74 79 5f 6f 6b 20 3d 20 28 70 54 61 62 2d  nity_ok = (pTab-
1c450 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 61 66 66 69  >aCol[iCol].affi
1c451 6e 69 74 79 3d 3d 61 66 66 7c 7c 61 66 66 3d 3d  nity==aff||aff==
1c452 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 29  SQLITE_AFF_NONE)
1c453 3b 0a 0a 20 20 20 20 20 20 66 6f 72 28 70 49 64  ;..      for(pId
1c454 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
1c455 70 49 64 78 20 26 26 20 65 54 79 70 65 3d 3d 30  pIdx && eType==0
1c456 20 26 26 20 61 66 66 69 6e 69 74 79 5f 6f 6b 3b   && affinity_ok;
1c457 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
1c458 74 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  t){.        if( 
1c459 28 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  (pIdx->aiColumn[
1c45a 30 5d 3d 3d 69 43 6f 6c 29 0a 20 20 20 20 20 20  0]==iCol).      
1c45b 20 20 20 26 26 20 28 70 52 65 71 3d 3d 73 71 6c     && (pReq==sql
1c45c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28  ite3FindCollSeq(
1c45d 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 70 49 64  db, ENC(db), pId
1c45e 78 2d 3e 61 7a 43 6f 6c 6c 5b 30 5d 2c 20 2d 31  x->azColl[0], -1
1c45f 2c 20 30 29 29 0a 20 20 20 20 20 20 20 20 20 26  , 0)).         &
1c460 26 20 28 21 6d 75 73 74 42 65 55 6e 69 71 75 65  & (!mustBeUnique
1c461 20 7c 7c 20 28 70 49 64 78 2d 3e 6e 43 6f 6c 75   || (pIdx->nColu
1c462 6d 6e 3d 3d 31 20 26 26 20 70 49 64 78 2d 3e 6f  mn==1 && pIdx->o
1c463 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 29  nError!=OE_None)
1c464 29 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20  ).        ){.   
1c465 20 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a         int iDb;.
1c466 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 4d            int iM
1c467 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  em = ++pParse->n
1c468 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 69  Mem;.          i
1c469 6e 74 20 69 41 64 64 72 3b 0a 20 20 20 20 20 20  nt iAddr;.      
1c46a 20 20 20 20 63 68 61 72 20 2a 70 4b 65 79 3b 0a      char *pKey;.
1c46b 20 20 0a 20 20 20 20 20 20 20 20 20 20 70 4b 65    .          pKe
1c46c 79 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69  y = (char *)sqli
1c46d 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28  te3IndexKeyinfo(
1c46e 70 50 61 72 73 65 2c 20 70 49 64 78 29 3b 0a 20  pParse, pIdx);. 
1c46f 20 20 20 20 20 20 20 20 20 69 44 62 20 3d 20 73           iDb = s
1c470 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
1c471 64 65 78 28 64 62 2c 20 70 49 64 78 2d 3e 70 53  dex(db, pIdx->pS
1c472 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 20 20  chema);.        
1c473 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65    sqlite3VdbeUse
1c474 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a  sBtree(v, iDb);.
1c475 0a 20 20 20 20 20 20 20 20 20 20 69 41 64 64 72  .          iAddr
1c476 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
1c477 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 69  dOp1(v, OP_If, i
1c478 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Mem);.          
1c479 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1c47a 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
1c47b 20 31 2c 20 69 4d 65 6d 29 3b 0a 20 20 0a 20 20   1, iMem);.  .  
1c47c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1c47d 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1c47e 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30  SetNumColumns, 0
1c47f 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 29  , pIdx->nColumn)
1c480 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
1c481 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
1c482 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 54   OP_OpenRead, iT
1c483 61 62 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20  ab, pIdx->tnum, 
1c484 69 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20  iDb,.           
1c485 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c486 20 20 20 20 70 4b 65 79 2c 50 34 5f 4b 45 59 49      pKey,P4_KEYI
1c487 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20  NFO_HANDOFF);.  
1c488 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d          VdbeComm
1c489 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70 49  ent((v, "%s", pI
1c48a 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20  dx->zName));.   
1c48b 20 20 20 20 20 20 20 65 54 79 70 65 20 3d 20 49         eType = I
1c48c 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58 3b 0a 0a  N_INDEX_INDEX;..
1c48d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1c48e 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
1c48f 20 69 41 64 64 72 29 3b 0a 20 20 20 20 20 20 20   iAddr);.       
1c490 20 20 20 69 66 28 20 70 72 4e 6f 74 46 6f 75 6e     if( prNotFoun
1c491 64 20 26 26 20 21 70 54 61 62 2d 3e 61 43 6f 6c  d && !pTab->aCol
1c492 5b 69 43 6f 6c 5d 2e 6e 6f 74 4e 75 6c 6c 20 29  [iCol].notNull )
1c493 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 70  {.            *p
1c494 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 2b 2b 70 50  rNotFound = ++pP
1c495 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
1c496 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1c497 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
1c498 20 20 7d 0a 0a 20 20 69 66 28 20 65 54 79 70 65    }..  if( eType
1c499 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72  ==0 ){.    int r
1c49a 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20 30 3b  MayHaveNull = 0;
1c49b 0a 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f  .    eType = IN_
1c49c 49 4e 44 45 58 5f 45 50 48 3b 0a 20 20 20 20 69  INDEX_EPH;.    i
1c49d 66 28 20 70 72 4e 6f 74 46 6f 75 6e 64 20 29 7b  f( prNotFound ){
1c49e 0a 20 20 20 20 20 20 2a 70 72 4e 6f 74 46 6f 75  .      *prNotFou
1c49f 6e 64 20 3d 20 72 4d 61 79 48 61 76 65 4e 75 6c  nd = rMayHaveNul
1c4a0 6c 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  l = ++pParse->nM
1c4a1 65 6d 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  em;.    }else if
1c4a2 28 20 70 58 2d 3e 70 4c 65 66 74 2d 3e 69 43 6f  ( pX->pLeft->iCo
1c4a3 6c 75 6d 6e 3c 30 20 26 26 20 70 58 2d 3e 70 53  lumn<0 && pX->pS
1c4a4 65 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20  elect==0 ){.    
1c4a5 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44    eType = IN_IND
1c4a6 45 58 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d 0a  EX_ROWID;.    }.
1c4a7 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 53      sqlite3CodeS
1c4a8 75 62 73 65 6c 65 63 74 28 70 50 61 72 73 65 2c  ubselect(pParse,
1c4a9 20 70 58 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c   pX, rMayHaveNul
1c4aa 6c 2c 20 65 54 79 70 65 3d 3d 49 4e 5f 49 4e 44  l, eType==IN_IND
1c4ab 45 58 5f 52 4f 57 49 44 29 3b 0a 20 20 7d 65 6c  EX_ROWID);.  }el
1c4ac 73 65 7b 0a 20 20 20 20 70 58 2d 3e 69 54 61 62  se{.    pX->iTab
1c4ad 6c 65 20 3d 20 69 54 61 62 3b 0a 20 20 7d 0a 20  le = iTab;.  }. 
1c4ae 20 72 65 74 75 72 6e 20 65 54 79 70 65 3b 0a 7d   return eType;.}
1c4af 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47  .#endif../*.** G
1c4b0 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
1c4b1 20 73 63 61 6c 61 72 20 73 75 62 71 75 65 72 69   scalar subqueri
1c4b2 65 73 20 75 73 65 64 20 61 73 20 61 6e 20 65 78  es used as an ex
1c4b3 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 61 6e 64 20  pression.** and 
1c4b4 49 4e 20 6f 70 65 72 61 74 6f 72 73 2e 20 20 45  IN operators.  E
1c4b5 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20  xamples:.**.**  
1c4b6 20 20 20 28 53 45 4c 45 43 54 20 61 20 46 52 4f     (SELECT a FRO
1c4b7 4d 20 62 29 20 20 20 20 20 20 20 20 20 20 2d 2d  M b)          --
1c4b8 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 20 20 20   subquery.**    
1c4b9 20 45 58 49 53 54 53 20 28 53 45 4c 45 43 54 20   EXISTS (SELECT 
1c4ba 61 20 46 52 4f 4d 20 62 29 20 20 20 2d 2d 20 45  a FROM b)   -- E
1c4bb 58 49 53 54 53 20 73 75 62 71 75 65 72 79 0a 2a  XISTS subquery.*
1c4bc 2a 20 20 20 20 20 78 20 49 4e 20 28 34 2c 35 2c  *     x IN (4,5,
1c4bd 31 31 29 20 20 20 20 20 20 20 20 20 20 20 20 20  11)             
1c4be 20 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f 72 20   -- IN operator 
1c4bf 77 69 74 68 20 6c 69 73 74 20 6f 6e 20 72 69 67  with list on rig
1c4c0 68 74 2d 68 61 6e 64 20 73 69 64 65 0a 2a 2a 20  ht-hand side.** 
1c4c1 20 20 20 20 78 20 49 4e 20 28 53 45 4c 45 43 54      x IN (SELECT
1c4c2 20 61 20 46 52 4f 4d 20 62 29 20 20 20 20 20 2d   a FROM b)     -
1c4c3 2d 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 77 69  - IN operator wi
1c4c4 74 68 20 73 75 62 71 75 65 72 79 20 6f 6e 20 74  th subquery on t
1c4c5 68 65 20 72 69 67 68 74 0a 2a 2a 0a 2a 2a 20 54  he right.**.** T
1c4c6 68 65 20 70 45 78 70 72 20 70 61 72 61 6d 65 74  he pExpr paramet
1c4c7 65 72 20 64 65 73 63 72 69 62 65 73 20 74 68 65  er describes the
1c4c8 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74   expression that
1c4c9 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 49 4e   contains the IN
1c4ca 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 6f 72 20  .** operator or 
1c4cb 73 75 62 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20  subquery..**.** 
1c4cc 49 66 20 70 61 72 61 6d 65 74 65 72 20 69 73 52  If parameter isR
1c4cd 6f 77 69 64 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  owid is non-zero
1c4ce 2c 20 74 68 65 6e 20 65 78 70 72 65 73 73 69 6f  , then expressio
1c4cf 6e 20 70 45 78 70 72 20 69 73 20 67 75 61 72 61  n pExpr is guara
1c4d0 6e 74 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20 6f  nteed.** to be o
1c4d1 66 20 74 68 65 20 66 6f 72 6d 20 22 3c 72 6f 77  f the form "<row
1c4d2 69 64 3e 20 49 4e 20 28 3f 2c 20 3f 2c 20 3f 29  id> IN (?, ?, ?)
1c4d3 22 2c 20 77 68 65 72 65 20 3c 72 6f 77 69 64 3e  ", where <rowid>
1c4d4 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 65 0a   is a reference.
1c4d5 2a 2a 20 74 6f 20 73 6f 6d 65 20 69 6e 74 65 67  ** to some integ
1c4d6 65 72 20 6b 65 79 20 63 6f 6c 75 6d 6e 20 6f 66  er key column of
1c4d7 20 61 20 74 61 62 6c 65 20 42 2d 54 72 65 65 2e   a table B-Tree.
1c4d8 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 75   In this case, u
1c4d9 73 65 20 61 6e 0a 2a 2a 20 69 6e 74 6b 65 79 20  se an.** intkey 
1c4da 42 2d 54 72 65 65 20 74 6f 20 73 74 6f 72 65 20  B-Tree to store 
1c4db 74 68 65 20 73 65 74 20 6f 66 20 49 4e 28 2e 2e  the set of IN(..
1c4dc 2e 29 20 76 61 6c 75 65 73 20 69 6e 73 74 65 61  .) values instea
1c4dd 64 20 6f 66 20 74 68 65 20 75 73 75 61 6c 0a 2a  d of the usual.*
1c4de 2a 20 28 73 6c 6f 77 65 72 29 20 76 61 72 69 61  * (slower) varia
1c4df 62 6c 65 20 6c 65 6e 67 74 68 20 6b 65 79 73 20  ble length keys 
1c4e0 42 2d 54 72 65 65 2e 0a 2a 2f 0a 23 69 66 6e 64  B-Tree..*/.#ifnd
1c4e1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
1c4e2 55 42 51 55 45 52 59 0a 53 51 4c 49 54 45 5f 50  UBQUERY.SQLITE_P
1c4e3 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1c4e4 74 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74  te3CodeSubselect
1c4e5 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
1c4e6 65 2c 20 0a 20 20 45 78 70 72 20 2a 70 45 78 70  e, .  Expr *pExp
1c4e7 72 2c 20 0a 20 20 69 6e 74 20 72 4d 61 79 48 61  r, .  int rMayHa
1c4e8 76 65 4e 75 6c 6c 2c 0a 20 20 69 6e 74 20 69 73  veNull,.  int is
1c4e9 52 6f 77 69 64 0a 29 7b 0a 20 20 69 6e 74 20 74  Rowid.){.  int t
1c4ea 65 73 74 41 64 64 72 20 3d 20 30 3b 20 20 20 20  estAddr = 0;    
1c4eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c4ec 20 20 20 2f 2a 20 4f 6e 65 2d 74 69 6d 65 20 74     /* One-time t
1c4ed 65 73 74 20 61 64 64 72 65 73 73 20 2a 2f 0a 20  est address */. 
1c4ee 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74   Vdbe *v = sqlit
1c4ef 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
1c4f0 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20  );.  if( v==0 ) 
1c4f1 72 65 74 75 72 6e 3b 0a 0a 0a 20 20 2f 2a 20 54  return;...  /* T
1c4f2 68 69 73 20 63 6f 64 65 20 6d 75 73 74 20 62 65  his code must be
1c4f3 20 72 75 6e 20 69 6e 20 69 74 73 20 65 6e 74 69   run in its enti
1c4f4 72 65 74 79 20 65 76 65 72 79 20 74 69 6d 65 20  rety every time 
1c4f5 69 74 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  it is encountere
1c4f6 64 0a 20 20 2a 2a 20 69 66 20 61 6e 79 20 6f 66  d.  ** if any of
1c4f7 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69   the following i
1c4f8 73 20 74 72 75 65 3a 0a 20 20 2a 2a 0a 20 20 2a  s true:.  **.  *
1c4f9 2a 20 20 20 20 2a 20 20 54 68 65 20 72 69 67 68  *    *  The righ
1c4fa 74 2d 68 61 6e 64 20 73 69 64 65 20 69 73 20 61  t-hand side is a
1c4fb 20 63 6f 72 72 65 6c 61 74 65 64 20 73 75 62 71   correlated subq
1c4fc 75 65 72 79 0a 20 20 2a 2a 20 20 20 20 2a 20 20  uery.  **    *  
1c4fd 54 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73  The right-hand s
1c4fe 69 64 65 20 69 73 20 61 6e 20 65 78 70 72 65 73  ide is an expres
1c4ff 73 69 6f 6e 20 6c 69 73 74 20 63 6f 6e 74 61 69  sion list contai
1c500 6e 69 6e 67 20 76 61 72 69 61 62 6c 65 73 0a 20  ning variables. 
1c501 20 2a 2a 20 20 20 20 2a 20 20 57 65 20 61 72 65   **    *  We are
1c502 20 69 6e 73 69 64 65 20 61 20 74 72 69 67 67 65   inside a trigge
1c503 72 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 61  r.  **.  ** If a
1c504 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20  ll of the above 
1c505 61 72 65 20 66 61 6c 73 65 2c 20 74 68 65 6e 20  are false, then 
1c506 77 65 20 63 61 6e 20 72 75 6e 20 74 68 69 73 20  we can run this 
1c507 63 6f 64 65 20 6a 75 73 74 20 6f 6e 63 65 0a 20  code just once. 
1c508 20 2a 2a 20 73 61 76 65 20 74 68 65 20 72 65 73   ** save the res
1c509 75 6c 74 73 2c 20 61 6e 64 20 72 65 75 73 65 20  ults, and reuse 
1c50a 74 68 65 20 73 61 6d 65 20 72 65 73 75 6c 74 20  the same result 
1c50b 6f 6e 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e  on subsequent in
1c50c 76 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a  vocations..  */.
1c50d 20 20 69 66 28 20 21 45 78 70 72 48 61 73 41 6e    if( !ExprHasAn
1c50e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  yProperty(pExpr,
1c50f 20 45 50 5f 56 61 72 53 65 6c 65 63 74 29 20 26   EP_VarSelect) &
1c510 26 20 21 70 50 61 72 73 65 2d 3e 74 72 69 67 53  & !pParse->trigS
1c511 74 61 63 6b 20 29 7b 0a 20 20 20 20 69 6e 74 20  tack ){.    int 
1c512 6d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  mem = ++pParse->
1c513 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65  nMem;.    sqlite
1c514 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
1c515 50 5f 49 66 2c 20 6d 65 6d 29 3b 0a 20 20 20 20  P_If, mem);.    
1c516 74 65 73 74 41 64 64 72 20 3d 20 73 71 6c 69 74  testAddr = sqlit
1c517 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1c518 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 6d  OP_Integer, 1, m
1c519 65 6d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  em);.    assert(
1c51a 20 74 65 73 74 41 64 64 72 3e 30 20 7c 7c 20 70   testAddr>0 || p
1c51b 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
1c51c 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 7d 0a 0a  cFailed );.  }..
1c51d 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d    switch( pExpr-
1c51e 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20  >op ){.    case 
1c51f 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 63  TK_IN: {.      c
1c520 68 61 72 20 61 66 66 69 6e 69 74 79 3b 0a 20 20  har affinity;.  
1c521 20 20 20 20 4b 65 79 49 6e 66 6f 20 6b 65 79 49      KeyInfo keyI
1c522 6e 66 6f 3b 0a 20 20 20 20 20 20 69 6e 74 20 61  nfo;.      int a
1c523 64 64 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 41  ddr;        /* A
1c524 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 4f 70 65  ddress of OP_Ope
1c525 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72  nEphemeral instr
1c526 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  uction */.      
1c527 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45  Expr *pLeft = pE
1c528 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 0a 20 20 20  xpr->pLeft;..   
1c529 20 20 20 69 66 28 20 72 4d 61 79 48 61 76 65 4e     if( rMayHaveN
1c52a 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ull ){.        s
1c52b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1c52c 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
1c52d 72 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20  rMayHaveNull);. 
1c52e 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 66       }..      af
1c52f 66 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33  finity = sqlite3
1c530 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 4c 65  ExprAffinity(pLe
1c531 66 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 57  ft);..      /* W
1c532 68 65 74 68 65 72 20 74 68 69 73 20 69 73 20 61  hether this is a
1c533 6e 20 27 78 20 49 4e 28 53 45 4c 45 43 54 2e 2e  n 'x IN(SELECT..
1c534 2e 29 27 20 6f 72 20 61 6e 20 27 78 20 49 4e 28  .)' or an 'x IN(
1c535 3c 65 78 70 72 6c 69 73 74 3e 29 27 0a 20 20 20  <exprlist>)'.   
1c536 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e     ** expression
1c537 20 69 74 20 69 73 20 68 61 6e 64 6c 65 64 20 74   it is handled t
1c538 68 65 20 73 61 6d 65 20 77 61 79 2e 20 41 20 76  he same way. A v
1c539 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 73 20  irtual table is 
1c53a 0a 20 20 20 20 20 20 2a 2a 20 66 69 6c 6c 65 64  .      ** filled
1c53b 20 77 69 74 68 20 73 69 6e 67 6c 65 2d 66 69 65   with single-fie
1c53c 6c 64 20 69 6e 64 65 78 20 6b 65 79 73 20 72 65  ld index keys re
1c53d 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 72  presenting the r
1c53e 65 73 75 6c 74 73 0a 20 20 20 20 20 20 2a 2a 20  esults.      ** 
1c53f 66 72 6f 6d 20 74 68 65 20 53 45 4c 45 43 54 20  from the SELECT 
1c540 6f 72 20 74 68 65 20 3c 65 78 70 72 6c 69 73 74  or the <exprlist
1c541 3e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  >..      **.    
1c542 20 20 2a 2a 20 49 66 20 74 68 65 20 27 78 27 20    ** If the 'x' 
1c543 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20  expression is a 
1c544 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 6f 72  column value, or
1c545 20 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e 0a 20   the SELECT.... 
1c546 20 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e       ** statemen
1c547 74 20 72 65 74 75 72 6e 73 20 61 20 63 6f 6c 75  t returns a colu
1c548 6d 6e 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74  mn value, then t
1c549 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74  he affinity of t
1c54a 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6c  hat.      ** col
1c54b 75 6d 6e 20 69 73 20 75 73 65 64 20 74 6f 20 62  umn is used to b
1c54c 75 69 6c 64 20 74 68 65 20 69 6e 64 65 78 20 6b  uild the index k
1c54d 65 79 73 2e 20 49 66 20 62 6f 74 68 20 27 78 27  eys. If both 'x'
1c54e 20 61 6e 64 20 74 68 65 0a 20 20 20 20 20 20 2a   and the.      *
1c54f 2a 20 53 45 4c 45 43 54 2e 2e 2e 20 73 74 61 74  * SELECT... stat
1c550 65 6d 65 6e 74 20 61 72 65 20 63 6f 6c 75 6d 6e  ement are column
1c551 73 2c 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 20  s, then numeric 
1c552 61 66 66 69 6e 69 74 79 20 69 73 20 75 73 65 64  affinity is used
1c553 0a 20 20 20 20 20 20 2a 2a 20 69 66 20 65 69 74  .      ** if eit
1c554 68 65 72 20 63 6f 6c 75 6d 6e 20 68 61 73 20 4e  her column has N
1c555 55 4d 45 52 49 43 20 6f 72 20 49 4e 54 45 47 45  UMERIC or INTEGE
1c556 52 20 61 66 66 69 6e 69 74 79 2e 20 49 66 20 6e  R affinity. If n
1c557 65 69 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20  either.      ** 
1c558 27 78 27 20 6e 6f 72 20 74 68 65 20 53 45 4c 45  'x' nor the SELE
1c559 43 54 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 20  CT... statement 
1c55a 61 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65  are columns, the
1c55b 6e 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69  n numeric affini
1c55c 74 79 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 75  ty.      ** is u
1c55d 73 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  sed..      */.  
1c55e 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c      pExpr->iTabl
1c55f 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  e = pParse->nTab
1c560 2b 2b 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d  ++;.      addr =
1c561 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1c562 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68  p2(v, OP_OpenEph
1c563 65 6d 65 72 61 6c 2c 20 70 45 78 70 72 2d 3e 69  emeral, pExpr->i
1c564 54 61 62 6c 65 2c 20 21 69 73 52 6f 77 69 64 29  Table, !isRowid)
1c565 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26  ;.      memset(&
1c566 6b 65 79 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65  keyInfo, 0, size
1c567 6f 66 28 6b 65 79 49 6e 66 6f 29 29 3b 0a 20 20  of(keyInfo));.  
1c568 20 20 20 20 6b 65 79 49 6e 66 6f 2e 6e 46 69 65      keyInfo.nFie
1c569 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20 69  ld = 1;..      i
1c56a 66 28 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 63  f( pExpr->pSelec
1c56b 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  t ){.        /* 
1c56c 43 61 73 65 20 31 3a 20 20 20 20 20 65 78 70 72  Case 1:     expr
1c56d 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29   IN (SELECT ...)
1c56e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
1c56f 20 20 20 20 2a 2a 20 47 65 6e 65 72 61 74 65 20      ** Generate 
1c570 63 6f 64 65 20 74 6f 20 77 72 69 74 65 20 74 68  code to write th
1c571 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65  e results of the
1c572 20 73 65 6c 65 63 74 20 69 6e 74 6f 20 74 68 65   select into the
1c573 20 74 65 6d 70 6f 72 61 72 79 0a 20 20 20 20 20   temporary.     
1c574 20 20 20 2a 2a 20 74 61 62 6c 65 20 61 6c 6c 6f     ** table allo
1c575 63 61 74 65 64 20 61 6e 64 20 6f 70 65 6e 65 64  cated and opened
1c576 20 61 62 6f 76 65 2e 0a 20 20 20 20 20 20 20 20   above..        
1c577 2a 2f 0a 20 20 20 20 20 20 20 20 53 65 6c 65 63  */.        Selec
1c578 74 44 65 73 74 20 64 65 73 74 3b 0a 20 20 20 20  tDest dest;.    
1c579 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45      ExprList *pE
1c57a 4c 69 73 74 3b 0a 0a 20 20 20 20 20 20 20 20 61  List;..        a
1c57b 73 73 65 72 74 28 20 21 69 73 52 6f 77 69 64 20  ssert( !isRowid 
1c57c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
1c57d 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74  e3SelectDestInit
1c57e 28 26 64 65 73 74 2c 20 53 52 54 5f 53 65 74 2c  (&dest, SRT_Set,
1c57f 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 29 3b   pExpr->iTable);
1c580 0a 20 20 20 20 20 20 20 20 64 65 73 74 2e 61 66  .        dest.af
1c581 66 69 6e 69 74 79 20 3d 20 28 69 6e 74 29 61 66  finity = (int)af
1c582 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 20 20  finity;.        
1c583 61 73 73 65 72 74 28 20 28 70 45 78 70 72 2d 3e  assert( (pExpr->
1c584 69 54 61 62 6c 65 26 30 78 30 30 30 30 46 46 46  iTable&0x0000FFF
1c585 46 29 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c  F)==pExpr->iTabl
1c586 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  e );.        if(
1c587 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70   sqlite3Select(p
1c588 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 53  Parse, pExpr->pS
1c589 65 6c 65 63 74 2c 20 26 64 65 73 74 29 20 29 7b  elect, &dest) ){
1c58a 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
1c58b 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  n;.        }.   
1c58c 20 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70 45       pEList = pE
1c58d 78 70 72 2d 3e 70 53 65 6c 65 63 74 2d 3e 70 45  xpr->pSelect->pE
1c58e 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20 69 66  List;.        if
1c58f 28 20 70 45 4c 69 73 74 20 26 26 20 70 45 4c 69  ( pEList && pELi
1c590 73 74 2d 3e 6e 45 78 70 72 3e 30 20 29 7b 20 0a  st->nExpr>0 ){ .
1c591 20 20 20 20 20 20 20 20 20 20 6b 65 79 49 6e 66            keyInf
1c592 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d 20 73 71 6c  o.aColl[0] = sql
1c593 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72  ite3BinaryCompar
1c594 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  eCollSeq(pParse,
1c595 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 0a 20   pExpr->pLeft,. 
1c596 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 4c               pEL
1c597 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 29  ist->a[0].pExpr)
1c598 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1c599 20 20 7d 65 6c 73 65 20 69 66 28 20 70 45 78 70    }else if( pExp
1c59a 72 2d 3e 70 4c 69 73 74 20 29 7b 0a 20 20 20 20  r->pList ){.    
1c59b 20 20 20 20 2f 2a 20 43 61 73 65 20 32 3a 20 20      /* Case 2:  
1c59c 20 20 20 65 78 70 72 20 49 4e 20 28 65 78 70 72     expr IN (expr
1c59d 6c 69 73 74 29 0a 20 20 20 20 20 20 20 20 2a 2a  list).        **
1c59e 0a 20 20 20 20 20 20 20 20 2a 2a 20 46 6f 72 20  .        ** For 
1c59f 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 2c  each expression,
1c5a0 20 62 75 69 6c 64 20 61 6e 20 69 6e 64 65 78 20   build an index 
1c5a1 6b 65 79 20 66 72 6f 6d 20 74 68 65 20 65 76 61  key from the eva
1c5a2 6c 75 61 74 69 6f 6e 20 61 6e 64 0a 20 20 20 20  luation and.    
1c5a3 20 20 20 20 2a 2a 20 73 74 6f 72 65 20 69 74 20      ** store it 
1c5a4 69 6e 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  in the temporary
1c5a5 20 74 61 62 6c 65 2e 20 49 66 20 3c 65 78 70 72   table. If <expr
1c5a6 3e 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 74  > is a column, t
1c5a7 68 65 6e 20 75 73 65 0a 20 20 20 20 20 20 20 20  hen use.        
1c5a8 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e 73 20  ** that columns 
1c5a9 61 66 66 69 6e 69 74 79 20 77 68 65 6e 20 62 75  affinity when bu
1c5aa 69 6c 64 69 6e 67 20 69 6e 64 65 78 20 6b 65 79  ilding index key
1c5ab 73 2e 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20  s. If <expr> is 
1c5ac 6e 6f 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  not.        ** a
1c5ad 20 63 6f 6c 75 6d 6e 2c 20 75 73 65 20 6e 75 6d   column, use num
1c5ae 65 72 69 63 20 61 66 66 69 6e 69 74 79 2e 0a 20  eric affinity.. 
1c5af 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
1c5b0 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20    int i;.       
1c5b1 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
1c5b2 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b   = pExpr->pList;
1c5b3 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20  .        struct 
1c5b4 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
1c5b5 49 74 65 6d 3b 0a 20 20 20 20 20 20 20 20 69 6e  Item;.        in
1c5b6 74 20 72 31 2c 20 72 32 2c 20 72 33 3b 0a 0a 20  t r1, r2, r3;.. 
1c5b7 20 20 20 20 20 20 20 69 66 28 20 21 61 66 66 69         if( !affi
1c5b8 6e 69 74 79 20 29 7b 0a 20 20 20 20 20 20 20 20  nity ){.        
1c5b9 20 20 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c    affinity = SQL
1c5ba 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20  ITE_AFF_NONE;.  
1c5bb 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1c5bc 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 5d  keyInfo.aColl[0]
1c5bd 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
1c5be 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 45  llSeq(pParse, pE
1c5bf 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 0a 20 20  xpr->pLeft);..  
1c5c0 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 68        /* Loop th
1c5c1 72 6f 75 67 68 20 65 61 63 68 20 65 78 70 72 65  rough each expre
1c5c2 73 73 69 6f 6e 20 69 6e 20 3c 65 78 70 72 6c 69  ssion in <exprli
1c5c3 73 74 3e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  st>. */.        
1c5c4 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r1 = sqlite3GetT
1c5c5 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
1c5c6 20 20 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c          r2 = sql
1c5c7 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
1c5c8 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20  Parse);.        
1c5c9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1c5ca 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
1c5cb 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20 66 6f   r2);.        fo
1c5cc 72 28 69 3d 70 4c 69 73 74 2d 3e 6e 45 78 70 72  r(i=pList->nExpr
1c5cd 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61  , pItem=pList->a
1c5ce 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65  ; i>0; i--, pIte
1c5cf 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  m++){.          
1c5d0 45 78 70 72 20 2a 70 45 32 20 3d 20 70 49 74 65  Expr *pE2 = pIte
1c5d1 6d 2d 3e 70 45 78 70 72 3b 0a 0a 20 20 20 20 20  m->pExpr;..     
1c5d2 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 65       /* If the e
1c5d3 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6e 6f 74  xpression is not
1c5d4 20 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e 20 77   constant then w
1c5d5 65 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 0a 20  e will need to. 
1c5d6 20 20 20 20 20 20 20 20 20 2a 2a 20 64 69 73 61           ** disa
1c5d7 62 6c 65 20 74 68 65 20 74 65 73 74 20 74 68 61  ble the test tha
1c5d8 74 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 20  t was generated 
1c5d9 61 62 6f 76 65 20 74 68 61 74 20 6d 61 6b 65 73  above that makes
1c5da 20 73 75 72 65 0a 20 20 20 20 20 20 20 20 20 20   sure.          
1c5db 2a 2a 20 74 68 69 73 20 63 6f 64 65 20 6f 6e 6c  ** this code onl
1c5dc 79 20 65 78 65 63 75 74 65 73 20 6f 6e 63 65 2e  y executes once.
1c5dd 20 20 42 65 63 61 75 73 65 20 66 6f 72 20 61 20    Because for a 
1c5de 6e 6f 6e 2d 63 6f 6e 73 74 61 6e 74 0a 20 20 20  non-constant.   
1c5df 20 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73         ** expres
1c5e0 73 69 6f 6e 20 77 65 20 6e 65 65 64 20 74 6f 20  sion we need to 
1c5e1 72 65 72 75 6e 20 74 68 69 73 20 63 6f 64 65 20  rerun this code 
1c5e2 65 61 63 68 20 74 69 6d 65 2e 0a 20 20 20 20 20  each time..     
1c5e3 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
1c5e4 20 20 69 66 28 20 74 65 73 74 41 64 64 72 20 26    if( testAddr &
1c5e5 26 20 21 73 71 6c 69 74 65 33 45 78 70 72 49 73  & !sqlite3ExprIs
1c5e6 43 6f 6e 73 74 61 6e 74 28 70 45 32 29 20 29 7b  Constant(pE2) ){
1c5e7 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
1c5e8 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f  ite3VdbeChangeTo
1c5e9 4e 6f 6f 70 28 76 2c 20 74 65 73 74 41 64 64 72  Noop(v, testAddr
1c5ea 2d 31 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20  -1, 2);.        
1c5eb 20 20 20 20 74 65 73 74 41 64 64 72 20 3d 20 30      testAddr = 0
1c5ec 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20  ;.          }.. 
1c5ed 20 20 20 20 20 20 20 20 20 2f 2a 20 45 76 61 6c           /* Eval
1c5ee 75 61 74 65 20 74 68 65 20 65 78 70 72 65 73 73  uate the express
1c5ef 69 6f 6e 20 61 6e 64 20 69 6e 73 65 72 74 20 69  ion and insert i
1c5f0 74 20 69 6e 74 6f 20 74 68 65 20 74 65 6d 70 20  t into the temp 
1c5f1 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20  table */.       
1c5f2 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62     pParse->disab
1c5f3 6c 65 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 20  leColCache++;.  
1c5f4 20 20 20 20 20 20 20 20 72 33 20 3d 20 73 71 6c          r3 = sql
1c5f5 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67  ite3ExprCodeTarg
1c5f6 65 74 28 70 50 61 72 73 65 2c 20 70 45 32 2c 20  et(pParse, pE2, 
1c5f7 72 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61  r1);.          a
1c5f8 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 64  ssert( pParse->d
1c5f9 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3e 30  isableColCache>0
1c5fa 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50   );.          pP
1c5fb 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c  arse->disableCol
1c5fc 43 61 63 68 65 2d 2d 3b 0a 0a 20 20 20 20 20 20  Cache--;..      
1c5fd 20 20 20 20 69 66 28 20 69 73 52 6f 77 69 64 20      if( isRowid 
1c5fe 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  ){.            s
1c5ff 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1c600 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74  (v, OP_MustBeInt
1c601 2c 20 72 33 2c 20 73 71 6c 69 74 65 33 56 64 62  , r3, sqlite3Vdb
1c602 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 2b  eCurrentAddr(v)+
1c603 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  2);.            
1c604 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1c605 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20  3(v, OP_Insert, 
1c606 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72  pExpr->iTable, r
1c607 32 2c 20 72 33 29 3b 0a 20 20 20 20 20 20 20 20  2, r3);.        
1c608 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1c609 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1c60a 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp4(v, OP_Mak
1c60b 65 52 65 63 6f 72 64 2c 20 72 33 2c 20 31 2c 20  eRecord, r3, 1, 
1c60c 72 32 2c 20 26 61 66 66 69 6e 69 74 79 2c 20 31  r2, &affinity, 1
1c60d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  );.            s
1c60e 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
1c60f 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50  ffinityChange(pP
1c610 61 72 73 65 2c 20 72 33 2c 20 31 29 3b 0a 20 20  arse, r3, 1);.  
1c611 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1c612 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1c613 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 70 45 78  P_IdxInsert, pEx
1c614 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72 32 29 3b  pr->iTable, r2);
1c615 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1c616 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73       }.        s
1c617 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
1c618 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29  pReg(pParse, r1)
1c619 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1c61a 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
1c61b 70 50 61 72 73 65 2c 20 72 32 29 3b 0a 20 20 20  pParse, r2);.   
1c61c 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21     }.      if( !
1c61d 69 73 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20  isRowid ){.     
1c61e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
1c61f 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2c 20  angeP4(v, addr, 
1c620 28 76 6f 69 64 20 2a 29 26 6b 65 79 49 6e 66 6f  (void *)&keyInfo
1c621 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20  , P4_KEYINFO);. 
1c622 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
1c623 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63  ak;.    }..    c
1c624 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a 20  ase TK_EXISTS:. 
1c625 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43     case TK_SELEC
1c626 54 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  T: {.      /* Th
1c627 69 73 20 68 61 73 20 74 6f 20 62 65 20 61 20 73  is has to be a s
1c628 63 61 6c 61 72 20 53 45 4c 45 43 54 2e 20 20 47  calar SELECT.  G
1c629 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
1c62a 70 75 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a  put the.      **
1c62b 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 73   value of this s
1c62c 65 6c 65 63 74 20 69 6e 20 61 20 6d 65 6d 6f 72  elect in a memor
1c62d 79 20 63 65 6c 6c 20 61 6e 64 20 72 65 63 6f 72  y cell and recor
1c62e 64 20 74 68 65 20 6e 75 6d 62 65 72 0a 20 20 20  d the number.   
1c62f 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 6d 65 6d     ** of the mem
1c630 6f 72 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f 6c  ory cell in iCol
1c631 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  umn..      */.  
1c632 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
1c633 20 54 6f 6b 65 6e 20 6f 6e 65 20 3d 20 7b 20 28   Token one = { (
1c634 75 38 2a 29 22 31 22 2c 20 30 2c 20 31 20 7d 3b  u8*)"1", 0, 1 };
1c635 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70  .      Select *p
1c636 53 65 6c 3b 0a 20 20 20 20 20 20 53 65 6c 65 63  Sel;.      Selec
1c637 74 44 65 73 74 20 64 65 73 74 3b 0a 0a 20 20 20  tDest dest;..   
1c638 20 20 20 70 53 65 6c 20 3d 20 70 45 78 70 72 2d     pSel = pExpr-
1c639 3e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 20 20  >pSelect;.      
1c63a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73  sqlite3SelectDes
1c63b 74 49 6e 69 74 28 26 64 65 73 74 2c 20 30 2c 20  tInit(&dest, 0, 
1c63c 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 29 3b  ++pParse->nMem);
1c63d 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72  .      if( pExpr
1c63e 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20  ->op==TK_SELECT 
1c63f 29 7b 0a 20 20 20 20 20 20 20 20 64 65 73 74 2e  ){.        dest.
1c640 65 44 65 73 74 20 3d 20 53 52 54 5f 4d 65 6d 3b  eDest = SRT_Mem;
1c641 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1c642 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1c643 5f 4e 75 6c 6c 2c 20 30 2c 20 64 65 73 74 2e 69  _Null, 0, dest.i
1c644 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 20 20 56  Parm);.        V
1c645 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
1c646 49 6e 69 74 20 73 75 62 71 75 65 72 79 20 72 65  Init subquery re
1c647 73 75 6c 74 22 29 29 3b 0a 20 20 20 20 20 20 7d  sult"));.      }
1c648 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 64 65  else{.        de
1c649 73 74 2e 65 44 65 73 74 20 3d 20 53 52 54 5f 45  st.eDest = SRT_E
1c64a 78 69 73 74 73 3b 0a 20 20 20 20 20 20 20 20 73  xists;.        s
1c64b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1c64c 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
1c64d 30 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a  0, dest.iParm);.
1c64e 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d          VdbeComm
1c64f 65 6e 74 28 28 76 2c 20 22 49 6e 69 74 20 45 58  ent((v, "Init EX
1c650 49 53 54 53 20 72 65 73 75 6c 74 22 29 29 3b 0a  ISTS result"));.
1c651 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
1c652 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
1c653 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 65 6c  pParse->db, pSel
1c654 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20  ->pLimit);.     
1c655 20 70 53 65 6c 2d 3e 70 4c 69 6d 69 74 20 3d 20   pSel->pLimit = 
1c656 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
1c657 72 73 65 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c  rse, TK_INTEGER,
1c658 20 30 2c 20 30 2c 20 26 6f 6e 65 29 3b 0a 20 20   0, 0, &one);.  
1c659 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
1c65a 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53  elect(pParse, pS
1c65b 65 6c 2c 20 26 64 65 73 74 29 20 29 7b 0a 20 20  el, &dest) ){.  
1c65c 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
1c65d 20 20 20 20 7d 0a 20 20 20 20 20 20 70 45 78 70      }.      pExp
1c65e 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 64 65 73  r->iColumn = des
1c65f 74 2e 69 50 61 72 6d 3b 0a 20 20 20 20 20 20 62  t.iParm;.      b
1c660 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
1c661 0a 20 20 69 66 28 20 74 65 73 74 41 64 64 72 20  .  if( testAddr 
1c662 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1c663 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 74 65  beJumpHere(v, te
1c664 73 74 41 64 64 72 2d 31 29 3b 0a 20 20 7d 0a 0a  stAddr-1);.  }..
1c665 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64    return;.}.#end
1c666 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1c667 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a 2f  T_SUBQUERY */../
1c668 2a 0a 2a 2a 20 44 75 70 6c 69 63 61 74 65 20 61  *.** Duplicate a
1c669 6e 20 38 2d 62 79 74 65 20 76 61 6c 75 65 0a 2a  n 8-byte value.*
1c66a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 64  /.static char *d
1c66b 75 70 38 62 79 74 65 73 28 56 64 62 65 20 2a 76  up8bytes(Vdbe *v
1c66c 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e  , const char *in
1c66d 29 7b 0a 20 20 63 68 61 72 20 2a 6f 75 74 20 3d  ){.  char *out =
1c66e 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
1c66f 52 61 77 28 73 71 6c 69 74 65 33 56 64 62 65 44  Raw(sqlite3VdbeD
1c670 62 28 76 29 2c 20 38 29 3b 0a 20 20 69 66 28 20  b(v), 8);.  if( 
1c671 6f 75 74 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70  out ){.    memcp
1c672 79 28 6f 75 74 2c 20 69 6e 2c 20 38 29 3b 0a 20  y(out, in, 8);. 
1c673 20 7d 0a 20 20 72 65 74 75 72 6e 20 6f 75 74 3b   }.  return out;
1c674 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
1c675 74 65 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f  te an instructio
1c676 6e 20 74 68 61 74 20 77 69 6c 6c 20 70 75 74 20  n that will put 
1c677 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  the floating poi
1c678 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 64 65 73 63  nt.** value desc
1c679 72 69 62 65 64 20 62 79 20 7a 5b 30 2e 2e 6e 2d  ribed by z[0..n-
1c67a 31 5d 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72  1] into register
1c67b 20 69 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   iMem..**.** The
1c67c 20 7a 5b 5d 20 73 74 72 69 6e 67 20 77 69 6c 6c   z[] string will
1c67d 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74 20 62 65   probably not be
1c67e 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
1c67f 2e 20 20 42 75 74 20 74 68 65 20 0a 2a 2a 20 7a  .  But the .** z
1c680 5b 6e 5d 20 63 68 61 72 61 63 74 65 72 20 69 73  [n] character is
1c681 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62   guaranteed to b
1c682 65 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74  e something that
1c683 20 64 6f 65 73 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a   does not look.*
1c684 2a 20 6c 69 6b 65 20 74 68 65 20 63 6f 6e 74 69  * like the conti
1c685 6e 75 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e  nuation of the n
1c686 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  umber..*/.static
1c687 20 76 6f 69 64 20 63 6f 64 65 52 65 61 6c 28 56   void codeReal(V
1c688 64 62 65 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68  dbe *v, const ch
1c689 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e  ar *z, int n, in
1c68a 74 20 6e 65 67 61 74 65 46 6c 61 67 2c 20 69 6e  t negateFlag, in
1c68b 74 20 69 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72  t iMem){.  asser
1c68c 74 28 20 7a 20 7c 7c 20 76 3d 3d 30 20 7c 7c 20  t( z || v==0 || 
1c68d 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29  sqlite3VdbeDb(v)
1c68e 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1c68f 3b 0a 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20  ;.  if( z ){.   
1c690 20 64 6f 75 62 6c 65 20 76 61 6c 75 65 3b 0a 20   double value;. 
1c691 20 20 20 63 68 61 72 20 2a 7a 56 3b 0a 20 20 20     char *zV;.   
1c692 20 61 73 73 65 72 74 28 20 21 69 73 64 69 67 69   assert( !isdigi
1c693 74 28 7a 5b 6e 5d 29 20 29 3b 0a 20 20 20 20 73  t(z[n]) );.    s
1c694 71 6c 69 74 65 33 41 74 6f 46 28 7a 2c 20 26 76  qlite3AtoF(z, &v
1c695 61 6c 75 65 29 3b 0a 20 20 20 20 69 66 28 20 73  alue);.    if( s
1c696 71 6c 69 74 65 33 49 73 4e 61 4e 28 76 61 6c 75  qlite3IsNaN(valu
1c697 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  e) ){.      sqli
1c698 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1c699 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 4d 65   OP_Null, 0, iMe
1c69a 6d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  m);.    }else{. 
1c69b 20 20 20 20 20 69 66 28 20 6e 65 67 61 74 65 46       if( negateF
1c69c 6c 61 67 20 29 20 76 61 6c 75 65 20 3d 20 2d 76  lag ) value = -v
1c69d 61 6c 75 65 3b 0a 20 20 20 20 20 20 7a 56 20 3d  alue;.      zV =
1c69e 20 64 75 70 38 62 79 74 65 73 28 76 2c 20 28 63   dup8bytes(v, (c
1c69f 68 61 72 2a 29 26 76 61 6c 75 65 29 3b 0a 20 20  har*)&value);.  
1c6a0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1c6a1 64 64 4f 70 34 28 76 2c 20 4f 50 5f 52 65 61 6c  ddOp4(v, OP_Real
1c6a2 2c 20 30 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a 56  , 0, iMem, 0, zV
1c6a3 2c 20 50 34 5f 52 45 41 4c 29 3b 0a 20 20 20 20  , P4_REAL);.    
1c6a4 7d 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  }.  }.}.../*.** 
1c6a5 47 65 6e 65 72 61 74 65 20 61 6e 20 69 6e 73 74  Generate an inst
1c6a6 72 75 63 74 69 6f 6e 20 74 68 61 74 20 77 69 6c  ruction that wil
1c6a7 6c 20 70 75 74 20 74 68 65 20 69 6e 74 65 67 65  l put the intege
1c6a8 72 20 64 65 73 63 72 69 62 65 20 62 79 0a 2a 2a  r describe by.**
1c6a9 20 74 65 78 74 20 7a 5b 30 2e 2e 6e 2d 31 5d 20   text z[0..n-1] 
1c6aa 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 69 4d  into register iM
1c6ab 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b  em..**.** The z[
1c6ac 5d 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 70 72  ] string will pr
1c6ad 6f 62 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65  obably not be ze
1c6ae 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20  ro-terminated.  
1c6af 42 75 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d  But the .** z[n]
1c6b0 20 63 68 61 72 61 63 74 65 72 20 69 73 20 67 75   character is gu
1c6b1 61 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20 73  aranteed to be s
1c6b2 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 20 64 6f  omething that do
1c6b3 65 73 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c  es not look.** l
1c6b4 69 6b 65 20 74 68 65 20 63 6f 6e 74 69 6e 75 61  ike the continua
1c6b5 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62  tion of the numb
1c6b6 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  er..*/.static vo
1c6b7 69 64 20 63 6f 64 65 49 6e 74 65 67 65 72 28 56  id codeInteger(V
1c6b8 64 62 65 20 2a 76 2c 20 45 78 70 72 20 2a 70 45  dbe *v, Expr *pE
1c6b9 78 70 72 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67  xpr, int negFlag
1c6ba 2c 20 69 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 63  , int iMem){.  c
1c6bb 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a 20 20  onst char *z;.  
1c6bc 69 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73  if( pExpr->flags
1c6bd 20 26 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 29   & EP_IntValue )
1c6be 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 70 45  {.    int i = pE
1c6bf 78 70 72 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20  xpr->iTable;.   
1c6c0 20 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 69   if( negFlag ) i
1c6c1 20 3d 20 2d 69 3b 0a 20 20 20 20 73 71 6c 69 74   = -i;.    sqlit
1c6c2 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1c6c3 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 69  OP_Integer, i, i
1c6c4 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  Mem);.  }else if
1c6c5 28 20 28 7a 20 3d 20 28 63 68 61 72 2a 29 70 45  ( (z = (char*)pE
1c6c6 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 29 21 3d 30  xpr->token.z)!=0
1c6c7 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
1c6c8 20 20 20 69 6e 74 20 6e 20 3d 20 70 45 78 70 72     int n = pExpr
1c6c9 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 61  ->token.n;.    a
1c6ca 73 73 65 72 74 28 20 21 69 73 64 69 67 69 74 28  ssert( !isdigit(
1c6cb 7a 5b 6e 5d 29 20 29 3b 0a 20 20 20 20 69 66 28  z[n]) );.    if(
1c6cc 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32   sqlite3GetInt32
1c6cd 28 7a 2c 20 26 69 29 20 29 7b 0a 20 20 20 20 20  (z, &i) ){.     
1c6ce 20 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 69   if( negFlag ) i
1c6cf 20 3d 20 2d 69 3b 0a 20 20 20 20 20 20 73 71 6c   = -i;.      sql
1c6d0 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1c6d1 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c  , OP_Integer, i,
1c6d2 20 69 4d 65 6d 29 3b 0a 20 20 20 20 7d 65 6c 73   iMem);.    }els
1c6d3 65 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 74  e if( sqlite3Fit
1c6d4 73 49 6e 36 34 42 69 74 73 28 7a 2c 20 6e 65 67  sIn64Bits(z, neg
1c6d5 46 6c 61 67 29 20 29 7b 0a 20 20 20 20 20 20 69  Flag) ){.      i
1c6d6 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 20 20 20  64 value;.      
1c6d7 63 68 61 72 20 2a 7a 56 3b 0a 20 20 20 20 20 20  char *zV;.      
1c6d8 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 7a 2c  sqlite3Atoi64(z,
1c6d9 20 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 20 20   &value);.      
1c6da 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 76 61  if( negFlag ) va
1c6db 6c 75 65 20 3d 20 2d 76 61 6c 75 65 3b 0a 20 20  lue = -value;.  
1c6dc 20 20 20 20 7a 56 20 3d 20 64 75 70 38 62 79 74      zV = dup8byt
1c6dd 65 73 28 76 2c 20 28 63 68 61 72 2a 29 26 76 61  es(v, (char*)&va
1c6de 6c 75 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  lue);.      sqli
1c6df 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
1c6e0 20 4f 50 5f 49 6e 74 36 34 2c 20 30 2c 20 69 4d   OP_Int64, 0, iM
1c6e1 65 6d 2c 20 30 2c 20 7a 56 2c 20 50 34 5f 49 4e  em, 0, zV, P4_IN
1c6e2 54 36 34 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  T64);.    }else{
1c6e3 0a 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c 28  .      codeReal(
1c6e4 76 2c 20 7a 2c 20 6e 2c 20 6e 65 67 46 6c 61 67  v, z, n, negFlag
1c6e5 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a 20  , iMem);.    }. 
1c6e6 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e   }.}.../*.** Gen
1c6e7 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20  erate code that 
1c6e8 77 69 6c 6c 20 65 78 74 72 61 63 74 20 74 68 65  will extract the
1c6e9 20 69 43 6f 6c 75 6d 6e 2d 74 68 20 63 6f 6c 75   iColumn-th colu
1c6ea 6d 6e 20 66 72 6f 6d 0a 2a 2a 20 74 61 62 6c 65  mn from.** table
1c6eb 20 70 54 61 62 20 61 6e 64 20 73 74 6f 72 65 20   pTab and store 
1c6ec 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65  the column value
1c6ed 20 69 6e 20 61 20 72 65 67 69 73 74 65 72 2e 20   in a register. 
1c6ee 20 41 6e 20 65 66 66 6f 72 74 0a 2a 2a 20 69 73   An effort.** is
1c6ef 20 6d 61 64 65 20 74 6f 20 73 74 6f 72 65 20 74   made to store t
1c6f0 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20  he column value 
1c6f1 69 6e 20 72 65 67 69 73 74 65 72 20 69 52 65 67  in register iReg
1c6f2 2c 20 62 75 74 20 74 68 69 73 20 69 73 0a 2a 2a  , but this is.**
1c6f3 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 64 2e   not guaranteed.
1c6f4 20 20 54 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f    The location o
1c6f5 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c  f the column val
1c6f6 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  ue is returned..
1c6f7 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74  **.** There must
1c6f8 20 62 65 20 61 6e 20 6f 70 65 6e 20 63 75 72 73   be an open curs
1c6f9 6f 72 20 74 6f 20 70 54 61 62 20 69 6e 20 69 54  or to pTab in iT
1c6fa 61 62 6c 65 20 77 68 65 6e 20 74 68 69 73 20 72  able when this r
1c6fb 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c  outine.** is cal
1c6fc 6c 65 64 2e 20 20 49 66 20 69 43 6f 6c 75 6d 6e  led.  If iColumn
1c6fd 3c 30 20 74 68 65 6e 20 63 6f 64 65 20 69 73 20  <0 then code is 
1c6fe 67 65 6e 65 72 61 74 65 64 20 74 68 61 74 20 65  generated that e
1c6ff 78 74 72 61 63 74 73 20 74 68 65 20 72 6f 77 69  xtracts the rowi
1c700 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  d..**.** This ro
1c701 75 74 69 6e 65 20 6d 69 67 68 74 20 61 74 74 65  utine might atte
1c702 6d 70 74 20 74 6f 20 72 65 75 73 65 20 74 68 65  mpt to reuse the
1c703 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 6f   value of the co
1c704 6c 75 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 61 73  lumn that.** has
1c705 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6c 6f   already been lo
1c706 61 64 65 64 20 69 6e 74 6f 20 61 20 72 65 67 69  aded into a regi
1c707 73 74 65 72 2e 20 20 54 68 65 20 76 61 6c 75 65  ster.  The value
1c708 20 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20   will always.** 
1c709 62 65 20 75 73 65 64 20 69 66 20 69 74 20 68 61  be used if it ha
1c70a 73 20 6e 6f 74 20 75 6e 64 65 72 67 6f 6e 65 20  s not undergone 
1c70b 61 6e 79 20 61 66 66 69 6e 69 74 79 20 63 68 61  any affinity cha
1c70c 6e 67 65 73 2e 20 20 42 75 74 20 69 66 0a 2a 2a  nges.  But if.**
1c70d 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68 61   an affinity cha
1c70e 6e 67 65 20 68 61 73 20 6f 63 63 75 72 72 65 64  nge has occurred
1c70f 2c 20 74 68 65 6e 20 74 68 65 20 63 61 63 68 65  , then the cache
1c710 64 20 76 61 6c 75 65 20 77 69 6c 6c 20 6f 6e 6c  d value will onl
1c711 79 20 62 65 0a 2a 2a 20 75 73 65 64 20 69 66 20  y be.** used if 
1c712 61 6c 6c 6f 77 41 66 66 43 68 6e 67 20 69 73 20  allowAffChng is 
1c713 74 72 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  true..*/.SQLITE_
1c714 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1c715 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f  te3ExprCodeGetCo
1c716 6c 75 6d 6e 28 0a 20 20 50 61 72 73 65 20 2a 70  lumn(.  Parse *p
1c717 50 61 72 73 65 2c 20 20 20 2f 2a 20 50 61 72 73  Parse,   /* Pars
1c718 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e  ing and code gen
1c719 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20  erating context 
1c71a 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
1c71b 2c 20 20 20 20 20 2f 2a 20 44 65 73 63 72 69 70  ,     /* Descrip
1c71c 74 69 6f 6e 20 6f 66 20 74 68 65 20 74 61 62 6c  tion of the tabl
1c71d 65 20 77 65 20 61 72 65 20 72 65 61 64 69 6e 67  e we are reading
1c71e 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 69   from */.  int i
1c71f 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 2f 2a 20 49  Column,     /* I
1c720 6e 64 65 78 20 6f 66 20 74 68 65 20 74 61 62 6c  ndex of the tabl
1c721 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e  e column */.  in
1c722 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 2f  t iTable,      /
1c723 2a 20 54 68 65 20 63 75 72 73 6f 72 20 70 6f 69  * The cursor poi
1c724 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 74 61 62  nting to the tab
1c725 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65 67  le */.  int iReg
1c726 2c 20 20 20 20 20 20 20 20 2f 2a 20 53 74 6f 72  ,        /* Stor
1c727 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a  e results here *
1c728 2f 0a 20 20 69 6e 74 20 61 6c 6c 6f 77 41 66 66  /.  int allowAff
1c729 43 68 6e 67 20 2f 2a 20 54 72 75 65 20 69 66 20  Chng /* True if 
1c72a 70 72 69 6f 72 20 61 66 66 69 6e 69 74 79 20 63  prior affinity c
1c72b 68 61 6e 67 65 73 20 61 72 65 20 4f 4b 20 2a 2f  hanges are OK */
1c72c 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  .){.  Vdbe *v = 
1c72d 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
1c72e 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74   int i;.  struct
1c72f 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 0a   yColCache *p;..
1c730 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61    for(i=0, p=pPa
1c731 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20  rse->aColCache; 
1c732 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61  i<pParse->nColCa
1c733 63 68 65 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a  che; i++, p++){.
1c734 20 20 20 20 69 66 28 20 70 2d 3e 69 54 61 62 6c      if( p->iTabl
1c735 65 3d 3d 69 54 61 62 6c 65 20 26 26 20 70 2d 3e  e==iTable && p->
1c736 69 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 75 6d 6e  iColumn==iColumn
1c737 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28  .           && (
1c738 21 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 7c 7c  !p->affChange ||
1c739 20 61 6c 6c 6f 77 41 66 66 43 68 6e 67 29 20 29   allowAffChng) )
1c73a 7b 0a 23 69 66 20 30 0a 20 20 20 20 20 20 73 71  {.#if 0.      sq
1c73b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28  lite3VdbeAddOp0(
1c73c 76 2c 20 4f 50 5f 4e 6f 6f 70 29 3b 0a 20 20 20  v, OP_Noop);.   
1c73d 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
1c73e 76 2c 20 22 4f 50 54 3a 20 74 61 62 25 64 2e 63  v, "OPT: tab%d.c
1c73f 6f 6c 25 64 20 2d 3e 20 72 25 64 22 2c 20 69 54  ol%d -> r%d", iT
1c740 61 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 70  able, iColumn, p
1c741 2d 3e 69 52 65 67 29 29 3b 0a 23 65 6e 64 69 66  ->iReg));.#endif
1c742 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 2d  .      return p-
1c743 3e 69 52 65 67 3b 0a 20 20 20 20 7d 0a 20 20 7d  >iReg;.    }.  }
1c744 20 20 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d    .  assert( v!=
1c745 30 20 29 3b 0a 20 20 69 66 28 20 69 43 6f 6c 75  0 );.  if( iColu
1c746 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  mn<0 ){.    int 
1c747 6f 70 20 3d 20 28 70 54 61 62 20 26 26 20 49 73  op = (pTab && Is
1c748 56 69 72 74 75 61 6c 28 70 54 61 62 29 29 20 3f  Virtual(pTab)) ?
1c749 20 4f 50 5f 56 52 6f 77 69 64 20 3a 20 4f 50 5f   OP_VRowid : OP_
1c74a 52 6f 77 69 64 3b 0a 20 20 20 20 73 71 6c 69 74  Rowid;.    sqlit
1c74b 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1c74c 6f 70 2c 20 69 54 61 62 6c 65 2c 20 69 52 65 67  op, iTable, iReg
1c74d 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  );.  }else if( p
1c74e 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  Tab==0 ){.    sq
1c74f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
1c750 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 54  v, OP_Column, iT
1c751 61 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69  able, iColumn, i
1c752 52 65 67 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Reg);.  }else{. 
1c753 20 20 20 69 6e 74 20 6f 70 20 3d 20 49 73 56 69     int op = IsVi
1c754 72 74 75 61 6c 28 70 54 61 62 29 20 3f 20 4f 50  rtual(pTab) ? OP
1c755 5f 56 43 6f 6c 75 6d 6e 20 3a 20 4f 50 5f 43 6f  _VColumn : OP_Co
1c756 6c 75 6d 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65  lumn;.    sqlite
1c757 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 6f  3VdbeAddOp3(v, o
1c758 70 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c 75  p, iTable, iColu
1c759 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 73  mn, iReg);.    s
1c75a 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61  qlite3ColumnDefa
1c75b 75 6c 74 28 76 2c 20 70 54 61 62 2c 20 69 43 6f  ult(v, pTab, iCo
1c75c 6c 75 6d 6e 29 3b 0a 23 69 66 6e 64 65 66 20 53  lumn);.#ifndef S
1c75d 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
1c75e 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 69 66  ING_POINT.    if
1c75f 28 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f  ( pTab->aCol[iCo
1c760 6c 75 6d 6e 5d 2e 61 66 66 69 6e 69 74 79 3d 3d  lumn].affinity==
1c761 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 20  SQLITE_AFF_REAL 
1c762 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1c763 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
1c764 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 2c 20 69  _RealAffinity, i
1c765 52 65 67 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  Reg);.    }.#end
1c766 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  if.  }.  if( pPa
1c767 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43  rse->disableColC
1c768 61 63 68 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69  ache==0 ){.    i
1c769 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 6f 6c 43   = pParse->iColC
1c76a 61 63 68 65 3b 0a 20 20 20 20 70 20 3d 20 26 70  ache;.    p = &p
1c76b 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65  Parse->aColCache
1c76c 5b 69 5d 3b 0a 20 20 20 20 70 2d 3e 69 54 61 62  [i];.    p->iTab
1c76d 6c 65 20 3d 20 69 54 61 62 6c 65 3b 0a 20 20 20  le = iTable;.   
1c76e 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 43   p->iColumn = iC
1c76f 6f 6c 75 6d 6e 3b 0a 20 20 20 20 70 2d 3e 69 52  olumn;.    p->iR
1c770 65 67 20 3d 20 69 52 65 67 3b 0a 20 20 20 20 70  eg = iReg;.    p
1c771 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d 20 30 3b  ->affChange = 0;
1c772 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 69 66  .    i++;.    if
1c773 28 20 69 3e 3d 41 72 72 61 79 53 69 7a 65 28 70  ( i>=ArraySize(p
1c774 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65  Parse->aColCache
1c775 29 20 29 20 69 20 3d 20 30 3b 0a 20 20 20 20 69  ) ) i = 0;.    i
1c776 66 28 20 69 3e 70 50 61 72 73 65 2d 3e 6e 43 6f  f( i>pParse->nCo
1c777 6c 43 61 63 68 65 20 29 20 70 50 61 72 73 65 2d  lCache ) pParse-
1c778 3e 6e 43 6f 6c 43 61 63 68 65 20 3d 20 69 3b 0a  >nColCache = i;.
1c779 20 20 20 20 70 50 61 72 73 65 2d 3e 69 43 6f 6c      pParse->iCol
1c77a 43 61 63 68 65 20 3d 20 69 3b 0a 20 20 7d 0a 20  Cache = i;.  }. 
1c77b 20 72 65 74 75 72 6e 20 69 52 65 67 3b 0a 7d 0a   return iReg;.}.
1c77c 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6c 6c  ./*.** Clear all
1c77d 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 65 6e   column cache en
1c77e 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 64  tries associated
1c77f 20 77 69 74 68 20 74 68 65 20 76 64 62 65 0a 2a   with the vdbe.*
1c780 2a 20 63 75 72 73 6f 72 20 77 69 74 68 20 63 75  * cursor with cu
1c781 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 54 61 62  rsor number iTab
1c782 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
1c783 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1c784 65 33 45 78 70 72 43 6c 65 61 72 43 6f 6c 75 6d  e3ExprClearColum
1c785 6e 43 61 63 68 65 28 50 61 72 73 65 20 2a 70 50  nCache(Parse *pP
1c786 61 72 73 65 2c 20 69 6e 74 20 69 54 61 62 6c 65  arse, int iTable
1c787 29 7b 0a 20 20 69 66 28 20 69 54 61 62 6c 65 3c  ){.  if( iTable<
1c788 30 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d  0 ){.    pParse-
1c789 3e 6e 43 6f 6c 43 61 63 68 65 20 3d 20 30 3b 0a  >nColCache = 0;.
1c78a 20 20 20 20 70 50 61 72 73 65 2d 3e 69 43 6f 6c      pParse->iCol
1c78b 43 61 63 68 65 20 3d 20 30 3b 0a 20 20 7d 65 6c  Cache = 0;.  }el
1c78c 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20  se{.    int i;. 
1c78d 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50     for(i=0; i<pP
1c78e 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b  arse->nColCache;
1c78f 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
1c790 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63   pParse->aColCac
1c791 68 65 5b 69 5d 2e 69 54 61 62 6c 65 3d 3d 69 54  he[i].iTable==iT
1c792 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20  able ){.        
1c793 74 65 73 74 63 61 73 65 28 20 69 3d 3d 70 50 61  testcase( i==pPa
1c794 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 2d 31  rse->nColCache-1
1c795 20 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72   );.        pPar
1c796 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 69 5d  se->aColCache[i]
1c797 20 3d 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43   = pParse->aColC
1c798 61 63 68 65 5b 2d 2d 70 50 61 72 73 65 2d 3e 6e  ache[--pParse->n
1c799 43 6f 6c 43 61 63 68 65 5d 3b 0a 20 20 20 20 20  ColCache];.     
1c79a 20 20 20 70 50 61 72 73 65 2d 3e 69 43 6f 6c 43     pParse->iColC
1c79b 61 63 68 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e  ache = pParse->n
1c79c 43 6f 6c 43 61 63 68 65 3b 0a 20 20 20 20 20 20  ColCache;.      
1c79d 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  }.    }.  }.}../
1c79e 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 74 68 65 20  *.** Record the 
1c79f 66 61 63 74 20 74 68 61 74 20 61 6e 20 61 66 66  fact that an aff
1c7a0 69 6e 69 74 79 20 63 68 61 6e 67 65 20 68 61 73  inity change has
1c7a1 20 6f 63 63 75 72 72 65 64 20 6f 6e 20 69 43 6f   occurred on iCo
1c7a2 75 6e 74 0a 2a 2a 20 72 65 67 69 73 74 65 72 73  unt.** registers
1c7a3 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 69   starting with i
1c7a4 53 74 61 72 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  Start..*/.SQLITE
1c7a5 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1c7a6 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66  lite3ExprCacheAf
1c7a7 66 69 6e 69 74 79 43 68 61 6e 67 65 28 50 61 72  finityChange(Par
1c7a8 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
1c7a9 69 53 74 61 72 74 2c 20 69 6e 74 20 69 43 6f 75  iStart, int iCou
1c7aa 6e 74 29 7b 0a 20 20 69 6e 74 20 69 45 6e 64 20  nt){.  int iEnd 
1c7ab 3d 20 69 53 74 61 72 74 20 2b 20 69 43 6f 75 6e  = iStart + iCoun
1c7ac 74 20 2d 20 31 3b 0a 20 20 69 6e 74 20 69 3b 0a  t - 1;.  int i;.
1c7ad 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61    for(i=0; i<pPa
1c7ae 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b 20  rse->nColCache; 
1c7af 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72 20  i++){.    int r 
1c7b0 3d 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61  = pParse->aColCa
1c7b1 63 68 65 5b 69 5d 2e 69 52 65 67 3b 0a 20 20 20  che[i].iReg;.   
1c7b2 20 69 66 28 20 72 3e 3d 69 53 74 61 72 74 20 26   if( r>=iStart &
1c7b3 26 20 72 3c 3d 69 45 6e 64 20 29 7b 0a 20 20 20  & r<=iEnd ){.   
1c7b4 20 20 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43     pParse->aColC
1c7b5 61 63 68 65 5b 69 5d 2e 61 66 66 43 68 61 6e 67  ache[i].affChang
1c7b6 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  e = 1;.    }.  }
1c7b7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
1c7b8 74 65 20 63 6f 64 65 20 74 6f 20 6d 6f 76 65 20  te code to move 
1c7b9 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 72 65 67  content from reg
1c7ba 69 73 74 65 72 73 20 69 46 72 6f 6d 2e 2e 2e 69  isters iFrom...i
1c7bb 46 72 6f 6d 2b 6e 52 65 67 2d 31 0a 2a 2a 20 6f  From+nReg-1.** o
1c7bc 76 65 72 20 74 6f 20 69 54 6f 2e 2e 69 54 6f 2b  ver to iTo..iTo+
1c7bd 6e 52 65 67 2d 31 2e 20 4b 65 65 70 20 74 68 65  nReg-1. Keep the
1c7be 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 75 70   column cache up
1c7bf 2d 74 6f 2d 64 61 74 65 2e 0a 2a 2f 0a 53 51 4c  -to-date..*/.SQL
1c7c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1c7c1 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
1c7c2 4d 6f 76 65 28 50 61 72 73 65 20 2a 70 50 61 72  Move(Parse *pPar
1c7c3 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69  se, int iFrom, i
1c7c4 6e 74 20 69 54 6f 2c 20 69 6e 74 20 6e 52 65 67  nt iTo, int nReg
1c7c5 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66  ){.  int i;.  if
1c7c6 28 20 69 46 72 6f 6d 3d 3d 69 54 6f 20 29 20 72  ( iFrom==iTo ) r
1c7c7 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33  eturn;.  sqlite3
1c7c8 56 64 62 65 41 64 64 4f 70 33 28 70 50 61 72 73  VdbeAddOp3(pPars
1c7c9 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 4d 6f 76  e->pVdbe, OP_Mov
1c7ca 65 2c 20 69 46 72 6f 6d 2c 20 69 54 6f 2c 20 6e  e, iFrom, iTo, n
1c7cb 52 65 67 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  Reg);.  for(i=0;
1c7cc 20 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43   i<pParse->nColC
1c7cd 61 63 68 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ache; i++){.    
1c7ce 69 6e 74 20 78 20 3d 20 70 50 61 72 73 65 2d 3e  int x = pParse->
1c7cf 61 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 69 52 65  aColCache[i].iRe
1c7d0 67 3b 0a 20 20 20 20 69 66 28 20 78 3e 3d 69 46  g;.    if( x>=iF
1c7d1 72 6f 6d 20 26 26 20 78 3c 69 46 72 6f 6d 2b 6e  rom && x<iFrom+n
1c7d2 52 65 67 20 29 7b 0a 20 20 20 20 20 20 70 50 61  Reg ){.      pPa
1c7d3 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 69  rse->aColCache[i
1c7d4 5d 2e 69 52 65 67 20 2b 3d 20 69 54 6f 2d 69 46  ].iReg += iTo-iF
1c7d5 72 6f 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  rom;.    }.  }.}
1c7d6 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
1c7d7 20 63 6f 64 65 20 74 6f 20 63 6f 70 79 20 63 6f   code to copy co
1c7d8 6e 74 65 6e 74 20 66 72 6f 6d 20 72 65 67 69 73  ntent from regis
1c7d9 74 65 72 73 20 69 46 72 6f 6d 2e 2e 2e 69 46 72  ters iFrom...iFr
1c7da 6f 6d 2b 6e 52 65 67 2d 31 0a 2a 2a 20 6f 76 65  om+nReg-1.** ove
1c7db 72 20 74 6f 20 69 54 6f 2e 2e 69 54 6f 2b 6e 52  r to iTo..iTo+nR
1c7dc 65 67 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  eg-1..*/.SQLITE_
1c7dd 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1c7de 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 70 79  ite3ExprCodeCopy
1c7df 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1c7e0 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20 69  int iFrom, int i
1c7e1 54 6f 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20  To, int nReg){. 
1c7e2 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 69 46   int i;.  if( iF
1c7e3 72 6f 6d 3d 3d 69 54 6f 20 29 20 72 65 74 75 72  rom==iTo ) retur
1c7e4 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  n;.  for(i=0; i<
1c7e5 6e 52 65 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nReg; i++){.    
1c7e6 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1c7e7 32 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c  2(pParse->pVdbe,
1c7e8 20 4f 50 5f 43 6f 70 79 2c 20 69 46 72 6f 6d 2b   OP_Copy, iFrom+
1c7e9 69 2c 20 69 54 6f 2b 69 29 3b 0a 20 20 7d 0a 7d  i, iTo+i);.  }.}
1c7ea 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
1c7eb 72 75 65 20 69 66 20 61 6e 79 20 72 65 67 69 73  rue if any regis
1c7ec 74 65 72 20 69 6e 20 74 68 65 20 72 61 6e 67 65  ter in the range
1c7ed 20 69 46 72 6f 6d 2e 2e 69 54 6f 20 28 69 6e 63   iFrom..iTo (inc
1c7ee 6c 75 73 69 76 65 29 0a 2a 2a 20 69 73 20 75 73  lusive).** is us
1c7ef 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68  ed as part of th
1c7f0 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 2e 0a  e column cache..
1c7f1 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 73  */.static int us
1c7f2 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63 68 65 28  edAsColumnCache(
1c7f3 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
1c7f4 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20 69 54  nt iFrom, int iT
1c7f5 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66  o){.  int i;.  f
1c7f6 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65  or(i=0; i<pParse
1c7f7 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b 20 69 2b 2b  ->nColCache; i++
1c7f8 29 7b 0a 20 20 20 20 69 6e 74 20 72 20 3d 20 70  ){.    int r = p
1c7f9 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65  Parse->aColCache
1c7fa 5b 69 5d 2e 69 52 65 67 3b 0a 20 20 20 20 69 66  [i].iReg;.    if
1c7fb 28 20 72 3e 3d 69 46 72 6f 6d 20 26 26 20 72 3c  ( r>=iFrom && r<
1c7fc 3d 69 54 6f 20 29 20 72 65 74 75 72 6e 20 31 3b  =iTo ) return 1;
1c7fd 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
1c7fe 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 72 65 73  .}../*.** Theres
1c7ff 20 69 73 20 61 20 76 61 6c 75 65 20 69 6e 20 72   is a value in r
1c800 65 67 69 73 74 65 72 20 69 43 75 72 72 65 6e 74  egister iCurrent
1c801 2e 20 20 57 65 20 75 6c 74 69 6d 61 74 65 6c 79  .  We ultimately
1c802 20 77 61 6e 74 0a 2a 2a 20 74 68 65 20 76 61 6c   want.** the val
1c803 75 65 20 74 6f 20 62 65 20 69 6e 20 72 65 67 69  ue to be in regi
1c804 73 74 65 72 20 69 54 61 72 67 65 74 2e 20 20 49  ster iTarget.  I
1c805 74 20 6d 69 67 68 74 20 62 65 20 74 68 61 74 0a  t might be that.
1c806 2a 2a 20 69 43 75 72 72 65 6e 74 20 61 6e 64 20  ** iCurrent and 
1c807 69 54 61 72 67 65 74 20 61 72 65 20 74 68 65 20  iTarget are the 
1c808 73 61 6d 65 20 72 65 67 69 73 74 65 72 2e 0a 2a  same register..*
1c809 2a 0a 2a 2a 20 57 65 20 61 72 65 20 67 6f 69 6e  *.** We are goin
1c80a 67 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20  g to modify the 
1c80b 76 61 6c 75 65 2c 20 73 6f 20 77 65 20 6e 65 65  value, so we nee
1c80c 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 69  d to make sure i
1c80d 74 0a 2a 2a 20 69 73 20 6e 6f 74 20 61 20 63 61  t.** is not a ca
1c80e 63 68 65 64 20 72 65 67 69 73 74 65 72 2e 20 20  ched register.  
1c80f 49 66 20 69 43 75 72 72 65 6e 74 20 69 73 20 61  If iCurrent is a
1c810 20 63 61 63 68 65 64 20 72 65 67 69 73 74 65 72   cached register
1c811 2c 0a 2a 2a 20 74 68 65 6e 20 74 72 79 20 74 6f  ,.** then try to
1c812 20 6d 6f 76 65 20 74 68 65 20 76 61 6c 75 65 20   move the value 
1c813 6f 76 65 72 20 74 6f 20 69 54 61 72 67 65 74 2e  over to iTarget.
1c814 20 20 49 66 20 69 54 61 72 67 65 74 20 69 73 20    If iTarget is 
1c815 61 0a 2a 2a 20 63 61 63 68 65 64 20 72 65 67 69  a.** cached regi
1c816 73 74 65 72 2c 20 74 68 65 6e 20 63 6c 65 61 72  ster, then clear
1c817 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
1c818 6e 67 20 63 61 63 68 65 20 6c 69 6e 65 2e 0a 2a  ng cache line..*
1c819 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1c81a 72 65 67 69 73 74 65 72 20 74 68 61 74 20 74 68  register that th
1c81b 65 20 76 61 6c 75 65 20 65 6e 64 73 20 75 70 20  e value ends up 
1c81c 69 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  in..*/.SQLITE_PR
1c81d 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1c81e 33 45 78 70 72 57 72 69 74 61 62 6c 65 52 65 67  3ExprWritableReg
1c81f 69 73 74 65 72 28 50 61 72 73 65 20 2a 70 50 61  ister(Parse *pPa
1c820 72 73 65 2c 20 69 6e 74 20 69 43 75 72 72 65 6e  rse, int iCurren
1c821 74 2c 20 69 6e 74 20 69 54 61 72 67 65 74 29 7b  t, int iTarget){
1c822 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65  .  int i;.  asse
1c823 72 74 28 20 70 50 61 72 73 65 2d 3e 70 56 64 62  rt( pParse->pVdb
1c824 65 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 21 75  e!=0 );.  if( !u
1c825 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63 68 65  sedAsColumnCache
1c826 28 70 50 61 72 73 65 2c 20 69 43 75 72 72 65 6e  (pParse, iCurren
1c827 74 2c 20 69 43 75 72 72 65 6e 74 29 20 29 7b 0a  t, iCurrent) ){.
1c828 20 20 20 20 72 65 74 75 72 6e 20 69 43 75 72 72      return iCurr
1c829 65 6e 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69  ent;.  }.  if( i
1c82a 43 75 72 72 65 6e 74 21 3d 69 54 61 72 67 65 74  Current!=iTarget
1c82b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1c82c 64 62 65 41 64 64 4f 70 32 28 70 50 61 72 73 65  dbeAddOp2(pParse
1c82d 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 53 43 6f 70  ->pVdbe, OP_SCop
1c82e 79 2c 20 69 43 75 72 72 65 6e 74 2c 20 69 54 61  y, iCurrent, iTa
1c82f 72 67 65 74 29 3b 0a 20 20 7d 0a 20 20 66 6f 72  rget);.  }.  for
1c830 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e  (i=0; i<pParse->
1c831 6e 43 6f 6c 43 61 63 68 65 3b 20 69 2b 2b 29 7b  nColCache; i++){
1c832 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d  .    if( pParse-
1c833 3e 61 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 69 52  >aColCache[i].iR
1c834 65 67 3d 3d 69 54 61 72 67 65 74 20 29 7b 0a 20  eg==iTarget ){. 
1c835 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 43 6f       pParse->aCo
1c836 6c 43 61 63 68 65 5b 69 5d 20 3d 20 70 50 61 72  lCache[i] = pPar
1c837 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 2d 2d  se->aColCache[--
1c838 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68  pParse->nColCach
1c839 65 5d 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65  e];.      pParse
1c83a 2d 3e 69 43 6f 6c 43 61 63 68 65 20 3d 20 70 50  ->iColCache = pP
1c83b 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b  arse->nColCache;
1c83c 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
1c83d 75 72 6e 20 69 54 61 72 67 65 74 3b 0a 7d 0a 0a  urn iTarget;.}..
1c83e 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 61 73  /*.** If the las
1c83f 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f  t instruction co
1c840 64 65 64 20 69 73 20 61 6e 20 65 70 68 65 6d 65  ded is an epheme
1c841 72 61 6c 20 63 6f 70 79 20 6f 66 20 61 6e 79 20  ral copy of any 
1c842 6f 66 0a 2a 2a 20 74 68 65 20 72 65 67 69 73 74  of.** the regist
1c843 65 72 73 20 69 6e 20 74 68 65 20 6e 52 65 67 20  ers in the nReg 
1c844 72 65 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e  registers beginn
1c845 69 6e 67 20 77 69 74 68 20 69 52 65 67 2c 20 74  ing with iReg, t
1c846 68 65 6e 0a 2a 2a 20 63 6f 6e 76 65 72 74 20 74  hen.** convert t
1c847 68 65 20 6c 61 73 74 20 69 6e 73 74 72 75 63 74  he last instruct
1c848 69 6f 6e 20 66 72 6f 6d 20 4f 50 5f 53 43 6f 70  ion from OP_SCop
1c849 79 20 74 6f 20 4f 50 5f 43 6f 70 79 2e 0a 2a 2f  y to OP_Copy..*/
1c84a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1c84b 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
1c84c 48 61 72 64 43 6f 70 79 28 50 61 72 73 65 20 2a  HardCopy(Parse *
1c84d 70 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67  pParse, int iReg
1c84e 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69  , int nReg){.  i
1c84f 6e 74 20 61 64 64 72 3b 0a 20 20 56 64 62 65 4f  nt addr;.  VdbeO
1c850 70 20 2a 70 4f 70 3b 0a 20 20 56 64 62 65 20 2a  p *pOp;.  Vdbe *
1c851 76 3b 0a 0a 20 20 76 20 3d 20 70 50 61 72 73 65  v;..  v = pParse
1c852 2d 3e 70 56 64 62 65 3b 0a 20 20 61 64 64 72 20  ->pVdbe;.  addr 
1c853 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
1c854 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 70  rentAddr(v);.  p
1c855 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  Op = sqlite3Vdbe
1c856 47 65 74 4f 70 28 76 2c 20 61 64 64 72 2d 31 29  GetOp(v, addr-1)
1c857 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 20  ;.  assert( pOp 
1c858 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d  || pParse->db->m
1c859 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
1c85a 20 69 66 28 20 70 4f 70 20 26 26 20 70 4f 70 2d   if( pOp && pOp-
1c85b 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53 43 6f 70  >opcode==OP_SCop
1c85c 79 20 26 26 20 70 4f 70 2d 3e 70 31 3e 3d 69 52  y && pOp->p1>=iR
1c85d 65 67 20 26 26 20 70 4f 70 2d 3e 70 31 3c 69 52  eg && pOp->p1<iR
1c85e 65 67 2b 6e 52 65 67 20 29 7b 0a 20 20 20 20 70  eg+nReg ){.    p
1c85f 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f  Op->opcode = OP_
1c860 43 6f 70 79 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  Copy;.  }.}../*.
1c861 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
1c862 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 76 61   to store the va
1c863 6c 75 65 20 6f 66 20 74 68 65 20 69 41 6c 69 61  lue of the iAlia
1c864 73 2d 74 68 20 61 6c 69 61 73 20 69 6e 20 72 65  s-th alias in re
1c865 67 69 73 74 65 72 0a 2a 2a 20 74 61 72 67 65 74  gister.** target
1c866 2e 20 20 54 68 65 20 66 69 72 73 74 20 74 69 6d  .  The first tim
1c867 65 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64  e this is called
1c868 2c 20 70 45 78 70 72 20 69 73 20 65 76 61 6c 75  , pExpr is evalu
1c869 61 74 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 0a  ated to compute.
1c86a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ** the value of 
1c86b 74 68 65 20 61 6c 69 61 73 2e 20 20 54 68 65 20  the alias.  The 
1c86c 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20  value is stored 
1c86d 69 6e 20 61 6e 20 61 75 78 69 6c 69 61 72 79 20  in an auxiliary 
1c86e 72 65 67 69 73 74 65 72 0a 2a 2a 20 61 6e 64 20  register.** and 
1c86f 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  the number of th
1c870 61 74 20 72 65 67 69 73 74 65 72 20 69 73 20 72  at register is r
1c871 65 74 75 72 6e 65 64 2e 20 20 4f 6e 20 73 75 62  eturned.  On sub
1c872 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 2c 0a 2a  sequent calls,.*
1c873 2a 20 74 68 65 20 72 65 67 69 73 74 65 72 20 6e  * the register n
1c874 75 6d 62 65 72 20 69 73 20 72 65 74 75 72 6e 65  umber is returne
1c875 64 20 77 69 74 68 6f 75 74 20 67 65 6e 65 72 61  d without genera
1c876 74 69 6e 67 20 61 6e 79 20 63 6f 64 65 2e 0a 2a  ting any code..*
1c877 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69  *.** Note that i
1c878 6e 20 6f 72 64 65 72 20 66 6f 72 20 74 68 69 73  n order for this
1c879 20 74 6f 20 77 6f 72 6b 2c 20 63 6f 64 65 20 6d   to work, code m
1c87a 75 73 74 20 62 65 20 67 65 6e 65 72 61 74 65 64  ust be generated
1c87b 20 69 6e 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20   in the.** same 
1c87c 6f 72 64 65 72 20 74 68 61 74 20 69 74 20 69 73  order that it is
1c87d 20 65 78 65 63 75 74 65 64 2e 0a 2a 2a 0a 2a 2a   executed..**.**
1c87e 20 41 6c 69 61 73 65 73 20 61 72 65 20 6e 75 6d   Aliases are num
1c87f 62 65 72 65 64 20 73 74 61 72 74 69 6e 67 20 77  bered starting w
1c880 69 74 68 20 31 2e 20 20 53 6f 20 69 41 6c 69 61  ith 1.  So iAlia
1c881 73 20 69 73 20 69 6e 20 74 68 65 20 72 61 6e 67  s is in the rang
1c882 65 0a 2a 2a 20 6f 66 20 31 20 74 6f 20 70 50 61  e.** of 1 to pPa
1c883 72 73 65 2d 3e 6e 41 6c 69 61 73 20 69 6e 63 6c  rse->nAlias incl
1c884 75 73 69 76 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 70  usive.  .**.** p
1c885 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 69 41  Parse->aAlias[iA
1c886 6c 69 61 73 2d 31 5d 20 72 65 63 6f 72 64 73 20  lias-1] records 
1c887 74 68 65 20 72 65 67 69 73 74 65 72 20 6e 75 6d  the register num
1c888 62 65 72 20 77 68 65 72 65 20 74 68 65 20 76 61  ber where the va
1c889 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 69 41  lue.** of the iA
1c88a 6c 69 61 73 2d 74 68 20 61 6c 69 61 73 20 69 73  lias-th alias is
1c88b 20 73 74 6f 72 65 64 2e 20 20 49 66 20 7a 65 72   stored.  If zer
1c88c 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  o, that means th
1c88d 61 74 20 74 68 65 0a 2a 2a 20 61 6c 69 61 73 20  at the.** alias 
1c88e 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e  has not yet been
1c88f 20 63 6f 6d 70 75 74 65 64 2e 0a 2a 2f 0a 73 74   computed..*/.st
1c890 61 74 69 63 20 69 6e 74 20 63 6f 64 65 41 6c 69  atic int codeAli
1c891 61 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  as(Parse *pParse
1c892 2c 20 69 6e 74 20 69 41 6c 69 61 73 2c 20 45 78  , int iAlias, Ex
1c893 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 74  pr *pExpr, int t
1c894 61 72 67 65 74 29 7b 0a 20 20 73 71 6c 69 74 65  arget){.  sqlite
1c895 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
1c896 64 62 3b 0a 20 20 69 6e 74 20 69 52 65 67 3b 0a  db;.  int iReg;.
1c897 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 61 41    if( pParse->aA
1c898 6c 69 61 73 3d 3d 30 20 29 7b 0a 20 20 20 20 70  lias==0 ){.    p
1c899 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 20 3d 20  Parse->aAlias = 
1c89a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
1c89b 65 72 6f 28 64 62 2c 20 0a 20 20 20 20 20 20 20  ero(db, .       
1c89c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c89d 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66            sizeof
1c89e 28 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b  (pParse->aAlias[
1c89f 30 5d 29 2a 70 50 61 72 73 65 2d 3e 6e 41 6c 69  0])*pParse->nAli
1c8a0 61 73 20 29 3b 0a 20 20 20 20 69 66 28 20 64 62  as );.    if( db
1c8a1 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1c8a2 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
1c8a3 20 61 73 73 65 72 74 28 20 69 41 6c 69 61 73 3e   assert( iAlias>
1c8a4 30 20 26 26 20 69 41 6c 69 61 73 3c 3d 70 50 61  0 && iAlias<=pPa
1c8a5 72 73 65 2d 3e 6e 41 6c 69 61 73 20 29 3b 0a 20  rse->nAlias );. 
1c8a6 20 69 52 65 67 20 3d 20 70 50 61 72 73 65 2d 3e   iReg = pParse->
1c8a7 61 41 6c 69 61 73 5b 69 41 6c 69 61 73 2d 31 5d  aAlias[iAlias-1]
1c8a8 3b 0a 20 20 69 66 28 20 69 52 65 67 3d 3d 30 20  ;.  if( iReg==0 
1c8a9 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 72 73  ){.    if( pPars
1c8aa 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63  e->disableColCac
1c8ab 68 65 20 29 7b 0a 20 20 20 20 20 20 69 52 65 67  he ){.      iReg
1c8ac 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
1c8ad 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c  deTarget(pParse,
1c8ae 20 70 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b   pExpr, target);
1c8af 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1c8b0 20 20 69 52 65 67 20 3d 20 2b 2b 70 50 61 72 73    iReg = ++pPars
1c8b1 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 73  e->nMem;.      s
1c8b2 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
1c8b3 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 69 52  Parse, pExpr, iR
1c8b4 65 67 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73  eg);.      pPars
1c8b5 65 2d 3e 61 41 6c 69 61 73 5b 69 41 6c 69 61 73  e->aAlias[iAlias
1c8b6 2d 31 5d 20 3d 20 69 52 65 67 3b 0a 20 20 20 20  -1] = iReg;.    
1c8b7 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69  }.  }.  return i
1c8b8 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  Reg;.}../*.** Ge
1c8b9 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e 74 6f  nerate code into
1c8ba 20 74 68 65 20 63 75 72 72 65 6e 74 20 56 64 62   the current Vdb
1c8bb 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 74 68  e to evaluate th
1c8bc 65 20 67 69 76 65 6e 0a 2a 2a 20 65 78 70 72 65  e given.** expre
1c8bd 73 73 69 6f 6e 2e 20 20 41 74 74 65 6d 70 74 20  ssion.  Attempt 
1c8be 74 6f 20 73 74 6f 72 65 20 74 68 65 20 72 65 73  to store the res
1c8bf 75 6c 74 73 20 69 6e 20 72 65 67 69 73 74 65 72  ults in register
1c8c0 20 22 74 61 72 67 65 74 22 2e 0a 2a 2a 20 52 65   "target"..** Re
1c8c1 74 75 72 6e 20 74 68 65 20 72 65 67 69 73 74 65  turn the registe
1c8c2 72 20 77 68 65 72 65 20 72 65 73 75 6c 74 73 20  r where results 
1c8c3 61 72 65 20 73 74 6f 72 65 64 2e 0a 2a 2a 0a 2a  are stored..**.*
1c8c4 2a 20 57 69 74 68 20 74 68 69 73 20 72 6f 75 74  * With this rout
1c8c5 69 6e 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f  ine, there is no
1c8c6 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20   guarantee that 
1c8c7 72 65 73 75 6c 74 73 20 77 69 6c 6c 0a 2a 2a 20  results will.** 
1c8c8 62 65 20 73 74 6f 72 65 64 20 69 6e 20 74 61 72  be stored in tar
1c8c9 67 65 74 2e 20 20 54 68 65 20 72 65 73 75 6c 74  get.  The result
1c8ca 20 6d 69 67 68 74 20 62 65 20 73 74 6f 72 65 64   might be stored
1c8cb 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a   in some other.*
1c8cc 2a 20 72 65 67 69 73 74 65 72 20 69 66 20 69 74  * register if it
1c8cd 20 69 73 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74   is convenient t
1c8ce 6f 20 64 6f 20 73 6f 2e 20 20 54 68 65 20 63 61  o do so.  The ca
1c8cf 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a  lling function.*
1c8d0 2a 20 6d 75 73 74 20 63 68 65 63 6b 20 74 68 65  * must check the
1c8d1 20 72 65 74 75 72 6e 20 63 6f 64 65 20 61 6e 64   return code and
1c8d2 20 6d 6f 76 65 20 74 68 65 20 72 65 73 75 6c 74   move the result
1c8d3 73 20 74 6f 20 74 68 65 20 64 65 73 69 72 65 64  s to the desired
1c8d4 0a 2a 2a 20 72 65 67 69 73 74 65 72 2e 0a 2a 2f  .** register..*/
1c8d5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1c8d6 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43  int sqlite3ExprC
1c8d7 6f 64 65 54 61 72 67 65 74 28 50 61 72 73 65 20  odeTarget(Parse 
1c8d8 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
1c8d9 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 74  Expr, int target
1c8da 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  ){.  Vdbe *v = p
1c8db 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 2f  Parse->pVdbe;  /
1c8dc 2a 20 54 68 65 20 56 4d 20 75 6e 64 65 72 20 63  * The VM under c
1c8dd 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20  onstruction */. 
1c8de 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20   int op;        
1c8df 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1c8e0 65 20 6f 70 63 6f 64 65 20 62 65 69 6e 67 20 63  e opcode being c
1c8e1 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 6e  oded */.  int in
1c8e2 52 65 67 20 3d 20 74 61 72 67 65 74 3b 20 20 20  Reg = target;   
1c8e3 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 73      /* Results s
1c8e4 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65  tored in registe
1c8e5 72 20 69 6e 52 65 67 20 2a 2f 0a 20 20 69 6e 74  r inReg */.  int
1c8e6 20 72 65 67 46 72 65 65 31 20 3d 20 30 3b 20 20   regFree1 = 0;  
1c8e7 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e         /* If non
1c8e8 2d 7a 65 72 6f 20 66 72 65 65 20 74 68 69 73 20  -zero free this 
1c8e9 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74  temporary regist
1c8ea 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 46  er */.  int regF
1c8eb 72 65 65 32 20 3d 20 30 3b 20 20 20 20 20 20 20  ree2 = 0;       
1c8ec 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f    /* If non-zero
1c8ed 20 66 72 65 65 20 74 68 69 73 20 74 65 6d 70 6f   free this tempo
1c8ee 72 61 72 79 20 72 65 67 69 73 74 65 72 20 2a 2f  rary register */
1c8ef 0a 20 20 69 6e 74 20 72 31 2c 20 72 32 2c 20 72  .  int r1, r2, r
1c8f0 33 2c 20 72 34 3b 20 20 20 20 20 20 20 2f 2a 20  3, r4;       /* 
1c8f1 56 61 72 69 6f 75 73 20 72 65 67 69 73 74 65 72  Various register
1c8f2 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20 73 71   numbers */.  sq
1c8f3 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 64 62  lite3 *db;..  db
1c8f4 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
1c8f5 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 7c 7c   assert( v!=0 ||
1c8f6 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1c8f7 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 74  d );.  assert( t
1c8f8 61 72 67 65 74 3e 30 20 26 26 20 74 61 72 67 65  arget>0 && targe
1c8f9 74 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20  t<=pParse->nMem 
1c8fa 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20  );.  if( v==0 ) 
1c8fb 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 69 66 28  return 0;..  if(
1c8fc 20 70 45 78 70 72 3d 3d 30 20 29 7b 0a 20 20 20   pExpr==0 ){.   
1c8fd 20 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20   op = TK_NULL;. 
1c8fe 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 70 20 3d   }else{.    op =
1c8ff 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 7d 0a   pExpr->op;.  }.
1c900 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a    switch( op ){.
1c901 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f      case TK_AGG_
1c902 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20  COLUMN: {.      
1c903 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66  AggInfo *pAggInf
1c904 6f 20 3d 20 70 45 78 70 72 2d 3e 70 41 67 67 49  o = pExpr->pAggI
1c905 6e 66 6f 3b 0a 20 20 20 20 20 20 73 74 72 75 63  nfo;.      struc
1c906 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70  t AggInfo_col *p
1c907 43 6f 6c 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d  Col = &pAggInfo-
1c908 3e 61 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 41 67  >aCol[pExpr->iAg
1c909 67 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70  g];.      if( !p
1c90a 41 67 67 49 6e 66 6f 2d 3e 64 69 72 65 63 74 4d  AggInfo->directM
1c90b 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 20 20 61  ode ){.        a
1c90c 73 73 65 72 74 28 20 70 43 6f 6c 2d 3e 69 4d 65  ssert( pCol->iMe
1c90d 6d 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69  m>0 );.        i
1c90e 6e 52 65 67 20 3d 20 70 43 6f 6c 2d 3e 69 4d 65  nReg = pCol->iMe
1c90f 6d 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  m;.        break
1c910 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
1c911 28 20 70 41 67 67 49 6e 66 6f 2d 3e 75 73 65 53  ( pAggInfo->useS
1c912 6f 72 74 69 6e 67 49 64 78 20 29 7b 0a 20 20 20  ortingIdx ){.   
1c913 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1c914 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
1c915 75 6d 6e 2c 20 70 41 67 67 49 6e 66 6f 2d 3e 73  umn, pAggInfo->s
1c916 6f 72 74 69 6e 67 49 64 78 2c 0a 20 20 20 20 20  ortingIdx,.     
1c917 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c918 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69           pCol->i
1c919 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 2c 20 74 61  SorterColumn, ta
1c91a 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 62  rget);.        b
1c91b 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
1c91c 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65      /* Otherwise
1c91d 2c 20 66 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f  , fall thru into
1c91e 20 74 68 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20 63   the TK_COLUMN c
1c91f 61 73 65 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20  ase */.    }.   
1c920 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a   case TK_COLUMN:
1c921 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78   {.      if( pEx
1c922 70 72 2d 3e 69 54 61 62 6c 65 3c 30 20 29 7b 0a  pr->iTable<0 ){.
1c923 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
1c924 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 77 68 65  only happens whe
1c925 6e 20 63 6f 64 69 6e 67 20 63 68 65 63 6b 20 63  n coding check c
1c926 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20  onstraints */.  
1c927 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
1c928 61 72 73 65 2d 3e 63 6b 42 61 73 65 3e 30 20 29  arse->ckBase>0 )
1c929 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 65 67 20  ;.        inReg 
1c92a 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  = pExpr->iColumn
1c92b 20 2b 20 70 50 61 72 73 65 2d 3e 63 6b 42 61 73   + pParse->ckBas
1c92c 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  e;.      }else{.
1c92d 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
1c92e 28 20 28 70 45 78 70 72 2d 3e 66 6c 61 67 73 20  ( (pExpr->flags 
1c92f 26 20 45 50 5f 41 6e 79 41 66 66 29 21 3d 30 20  & EP_AnyAff)!=0 
1c930 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 65 67  );.        inReg
1c931 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
1c932 64 65 47 65 74 43 6f 6c 75 6d 6e 28 70 50 61 72  deGetColumn(pPar
1c933 73 65 2c 20 70 45 78 70 72 2d 3e 70 54 61 62 2c  se, pExpr->pTab,
1c934 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1c935 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c936 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e    pExpr->iColumn
1c937 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c  , pExpr->iTable,
1c938 20 74 61 72 67 65 74 2c 0a 20 20 20 20 20 20 20   target,.       
1c939 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c93a 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
1c93b 3e 66 6c 61 67 73 20 26 20 45 50 5f 41 6e 79 41  >flags & EP_AnyA
1c93c 66 66 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ff);.      }.   
1c93d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1c93e 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 45      case TK_INTE
1c93f 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 63 6f 64  GER: {.      cod
1c940 65 49 6e 74 65 67 65 72 28 76 2c 20 70 45 78 70  eInteger(v, pExp
1c941 72 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20  r, 0, target);. 
1c942 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1c943 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 4c  }.    case TK_FL
1c944 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 63 6f 64  OAT: {.      cod
1c945 65 52 65 61 6c 28 76 2c 20 28 63 68 61 72 2a 29  eReal(v, (char*)
1c946 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20  pExpr->token.z, 
1c947 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2c 20  pExpr->token.n, 
1c948 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  0, target);.    
1c949 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1c94a 20 20 20 63 61 73 65 20 54 4b 5f 53 54 52 49 4e     case TK_STRIN
1c94b 47 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  G: {.      sqlit
1c94c 65 33 44 65 71 75 6f 74 65 45 78 70 72 28 64 62  e3DequoteExpr(db
1c94d 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20  , pExpr);.      
1c94e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1c94f 34 28 76 2c 4f 50 5f 53 74 72 69 6e 67 38 2c 20  4(v,OP_String8, 
1c950 30 2c 20 74 61 72 67 65 74 2c 20 30 2c 0a 20 20  0, target, 0,.  
1c951 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c952 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 45 78        (char*)pEx
1c953 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78  pr->token.z, pEx
1c954 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b 0a 20 20  pr->token.n);.  
1c955 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1c956 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 55 4c  .    case TK_NUL
1c957 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  L: {.      sqlit
1c958 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1c959 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 74 61 72 67  OP_Null, 0, targ
1c95a 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  et);.      break
1c95b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20  ;.    }.#ifndef 
1c95c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42  SQLITE_OMIT_BLOB
1c95d 5f 4c 49 54 45 52 41 4c 0a 20 20 20 20 63 61 73  _LITERAL.    cas
1c95e 65 20 54 4b 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20  e TK_BLOB: {.   
1c95f 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20 20     int n;.      
1c960 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a 20  const char *z;. 
1c961 20 20 20 20 20 63 68 61 72 20 2a 7a 42 6c 6f 62       char *zBlob
1c962 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1c963 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3e 3d  pExpr->token.n>=
1c964 33 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  3 );.      asser
1c965 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e  t( pExpr->token.
1c966 7a 5b 30 5d 3d 3d 27 78 27 20 7c 7c 20 70 45 78  z[0]=='x' || pEx
1c967 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d  pr->token.z[0]==
1c968 27 58 27 20 29 3b 0a 20 20 20 20 20 20 61 73 73  'X' );.      ass
1c969 65 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65  ert( pExpr->toke
1c96a 6e 2e 7a 5b 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a  n.z[1]=='\'' );.
1c96b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45        assert( pE
1c96c 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 70 45 78  xpr->token.z[pEx
1c96d 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2d 31 5d 3d 3d  pr->token.n-1]==
1c96e 27 5c 27 27 20 29 3b 0a 20 20 20 20 20 20 6e 20  '\'' );.      n 
1c96f 3d 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e  = pExpr->token.n
1c970 20 2d 20 33 3b 0a 20 20 20 20 20 20 7a 20 3d 20   - 3;.      z = 
1c971 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f  (char*)pExpr->to
1c972 6b 65 6e 2e 7a 20 2b 20 32 3b 0a 20 20 20 20 20  ken.z + 2;.     
1c973 20 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33   zBlob = sqlite3
1c974 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65  HexToBlob(sqlite
1c975 33 56 64 62 65 44 62 28 76 29 2c 20 7a 2c 20 6e  3VdbeDb(v), z, n
1c976 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1c977 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
1c978 5f 42 6c 6f 62 2c 20 6e 2f 32 2c 20 74 61 72 67  _Blob, n/2, targ
1c979 65 74 2c 20 30 2c 20 7a 42 6c 6f 62 2c 20 50 34  et, 0, zBlob, P4
1c97a 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20  _DYNAMIC);.     
1c97b 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
1c97c 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 54 4b  ndif.    case TK
1c97d 5f 56 41 52 49 41 42 4c 45 3a 20 7b 0a 20 20 20  _VARIABLE: {.   
1c97e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1c97f 64 4f 70 32 28 76 2c 20 4f 50 5f 56 61 72 69 61  dOp2(v, OP_Varia
1c980 62 6c 65 2c 20 70 45 78 70 72 2d 3e 69 54 61 62  ble, pExpr->iTab
1c981 6c 65 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20  le, target);.   
1c982 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 74 6f     if( pExpr->to
1c983 6b 65 6e 2e 6e 3e 31 20 29 7b 0a 20 20 20 20 20  ken.n>1 ){.     
1c984 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
1c985 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63  angeP4(v, -1, (c
1c986 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65  har*)pExpr->toke
1c987 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65  n.z, pExpr->toke
1c988 6e 2e 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  n.n);.      }.  
1c989 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1c98a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45 47  .    case TK_REG
1c98b 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20 20 69  ISTER: {.      i
1c98c 6e 52 65 67 20 3d 20 70 45 78 70 72 2d 3e 69 54  nReg = pExpr->iT
1c98d 61 62 6c 65 3b 0a 20 20 20 20 20 20 62 72 65 61  able;.      brea
1c98e 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
1c98f 65 20 54 4b 5f 41 53 3a 20 7b 0a 20 20 20 20 20  e TK_AS: {.     
1c990 20 69 6e 52 65 67 20 3d 20 63 6f 64 65 41 6c 69   inReg = codeAli
1c991 61 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  as(pParse, pExpr
1c992 2d 3e 69 54 61 62 6c 65 2c 20 70 45 78 70 72 2d  ->iTable, pExpr-
1c993 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b  >pLeft, target);
1c994 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1c995 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
1c996 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20 20  TE_OMIT_CAST.   
1c997 20 63 61 73 65 20 54 4b 5f 43 41 53 54 3a 20 7b   case TK_CAST: {
1c998 0a 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73  .      /* Expres
1c999 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 6f 72  sions of the for
1c99a 6d 3a 20 20 20 43 41 53 54 28 70 4c 65 66 74 20  m:   CAST(pLeft 
1c99b 41 53 20 74 6f 6b 65 6e 29 20 2a 2f 0a 20 20 20  AS token) */.   
1c99c 20 20 20 69 6e 74 20 61 66 66 2c 20 74 6f 5f 6f     int aff, to_o
1c99d 70 3b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d  p;.      inReg =
1c99e 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
1c99f 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70  Target(pParse, p
1c9a0 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72  Expr->pLeft, tar
1c9a1 67 65 74 29 3b 0a 20 20 20 20 20 20 61 66 66 20  get);.      aff 
1c9a2 3d 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74  = sqlite3Affinit
1c9a3 79 54 79 70 65 28 26 70 45 78 70 72 2d 3e 74 6f  yType(&pExpr->to
1c9a4 6b 65 6e 29 3b 0a 20 20 20 20 20 20 74 6f 5f 6f  ken);.      to_o
1c9a5 70 20 3d 20 61 66 66 20 2d 20 53 51 4c 49 54 45  p = aff - SQLITE
1c9a6 5f 41 46 46 5f 54 45 58 54 20 2b 20 4f 50 5f 54  _AFF_TEXT + OP_T
1c9a7 6f 54 65 78 74 3b 0a 20 20 20 20 20 20 61 73 73  oText;.      ass
1c9a8 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54  ert( to_op==OP_T
1c9a9 6f 54 65 78 74 20 20 20 20 7c 7c 20 61 66 66 21  oText    || aff!
1c9aa 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54  =SQLITE_AFF_TEXT
1c9ab 20 20 20 20 29 3b 0a 20 20 20 20 20 20 61 73 73      );.      ass
1c9ac 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54  ert( to_op==OP_T
1c9ad 6f 42 6c 6f 62 20 20 20 20 7c 7c 20 61 66 66 21  oBlob    || aff!
1c9ae 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45  =SQLITE_AFF_NONE
1c9af 20 20 20 20 29 3b 0a 20 20 20 20 20 20 61 73 73      );.      ass
1c9b0 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54  ert( to_op==OP_T
1c9b1 6f 4e 75 6d 65 72 69 63 20 7c 7c 20 61 66 66 21  oNumeric || aff!
1c9b2 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45  =SQLITE_AFF_NUME
1c9b3 52 49 43 20 29 3b 0a 20 20 20 20 20 20 61 73 73  RIC );.      ass
1c9b4 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54  ert( to_op==OP_T
1c9b5 6f 49 6e 74 20 20 20 20 20 7c 7c 20 61 66 66 21  oInt     || aff!
1c9b6 3d 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45  =SQLITE_AFF_INTE
1c9b7 47 45 52 20 29 3b 0a 20 20 20 20 20 20 61 73 73  GER );.      ass
1c9b8 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54  ert( to_op==OP_T
1c9b9 6f 52 65 61 6c 20 20 20 20 7c 7c 20 61 66 66 21  oReal    || aff!
1c9ba 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c  =SQLITE_AFF_REAL
1c9bb 20 20 20 20 29 3b 0a 20 20 20 20 20 20 74 65 73      );.      tes
1c9bc 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50  tcase( to_op==OP
1c9bd 5f 54 6f 54 65 78 74 20 29 3b 0a 20 20 20 20 20  _ToText );.     
1c9be 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f 70   testcase( to_op
1c9bf 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 20 29 3b 0a 20  ==OP_ToBlob );. 
1c9c0 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74       testcase( t
1c9c1 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 72  o_op==OP_ToNumer
1c9c2 69 63 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ic );.      test
1c9c3 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f  case( to_op==OP_
1c9c4 54 6f 49 6e 74 20 29 3b 0a 20 20 20 20 20 20 74  ToInt );.      t
1c9c5 65 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d  estcase( to_op==
1c9c6 4f 50 5f 54 6f 52 65 61 6c 20 29 3b 0a 20 20 20  OP_ToReal );.   
1c9c7 20 20 20 69 66 28 20 69 6e 52 65 67 21 3d 74 61     if( inReg!=ta
1c9c8 72 67 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20  rget ){.        
1c9c9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1c9ca 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 69  2(v, OP_SCopy, i
1c9cb 6e 52 65 67 2c 20 74 61 72 67 65 74 29 3b 0a 20  nReg, target);. 
1c9cc 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 74         inReg = t
1c9cd 61 72 67 65 74 3b 0a 20 20 20 20 20 20 7d 0a 20  arget;.      }. 
1c9ce 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1c9cf 41 64 64 4f 70 31 28 76 2c 20 74 6f 5f 6f 70 2c  AddOp1(v, to_op,
1c9d0 20 69 6e 52 65 67 29 3b 0a 20 20 20 20 20 20 74   inReg);.      t
1c9d1 65 73 74 63 61 73 65 28 20 75 73 65 64 41 73 43  estcase( usedAsC
1c9d2 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50 61 72 73  olumnCache(pPars
1c9d3 65 2c 20 69 6e 52 65 67 2c 20 69 6e 52 65 67 29  e, inReg, inReg)
1c9d4 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
1c9d5 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69  3ExprCacheAffini
1c9d6 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c  tyChange(pParse,
1c9d7 20 69 6e 52 65 67 2c 20 31 29 3b 0a 20 20 20 20   inReg, 1);.    
1c9d8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
1c9d9 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1c9da 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a 20 20 20  OMIT_CAST */.   
1c9db 20 63 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20   case TK_LT:.   
1c9dc 20 63 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20   case TK_LE:.   
1c9dd 20 63 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20   case TK_GT:.   
1c9de 20 63 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20   case TK_GE:.   
1c9df 20 63 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20   case TK_NE:.   
1c9e0 20 63 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20   case TK_EQ: {. 
1c9e1 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
1c9e2 4c 54 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 20 20 20  LT==OP_Lt );.   
1c9e3 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 45     assert( TK_LE
1c9e4 3d 3d 4f 50 5f 4c 65 20 29 3b 0a 20 20 20 20 20  ==OP_Le );.     
1c9e5 20 61 73 73 65 72 74 28 20 54 4b 5f 47 54 3d 3d   assert( TK_GT==
1c9e6 4f 50 5f 47 74 20 29 3b 0a 20 20 20 20 20 20 61  OP_Gt );.      a
1c9e7 73 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 4f 50  ssert( TK_GE==OP
1c9e8 5f 47 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73  _Ge );.      ass
1c9e9 65 72 74 28 20 54 4b 5f 45 51 3d 3d 4f 50 5f 45  ert( TK_EQ==OP_E
1c9ea 71 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  q );.      asser
1c9eb 74 28 20 54 4b 5f 4e 45 3d 3d 4f 50 5f 4e 65 20  t( TK_NE==OP_Ne 
1c9ec 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1c9ed 65 28 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b 0a  e( op==TK_LT );.
1c9ee 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1c9ef 6f 70 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20 20  op==TK_LE );.   
1c9f0 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
1c9f1 3d 54 4b 5f 47 54 20 29 3b 0a 20 20 20 20 20 20  =TK_GT );.      
1c9f2 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
1c9f3 5f 47 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73  _GE );.      tes
1c9f4 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 51  tcase( op==TK_EQ
1c9f5 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1c9f6 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b  se( op==TK_NE );
1c9f7 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61  .      codeCompa
1c9f8 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72 73  reOperands(pPars
1c9f9 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  e, pExpr->pLeft,
1c9fa 20 26 72 31 2c 20 26 72 65 67 46 72 65 65 31 2c   &r1, &regFree1,
1c9fb 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1c9fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c9fd 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74     pExpr->pRight
1c9fe 2c 20 26 72 32 2c 20 26 72 65 67 46 72 65 65 32  , &r2, &regFree2
1c9ff 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d  );.      codeCom
1ca00 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 78  pare(pParse, pEx
1ca01 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72  pr->pLeft, pExpr
1ca02 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20  ->pRight, op,.  
1ca03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ca04 72 31 2c 20 72 32 2c 20 69 6e 52 65 67 2c 20 53  r1, r2, inReg, S
1ca05 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a  QLITE_STOREP2);.
1ca06 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1ca07 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20  regFree1==0 );. 
1ca08 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
1ca09 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20  egFree2==0 );.  
1ca0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1ca0b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4e 44  .    case TK_AND
1ca0c 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52  :.    case TK_OR
1ca0d 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 50 4c  :.    case TK_PL
1ca0e 55 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  US:.    case TK_
1ca0f 53 54 41 52 3a 0a 20 20 20 20 63 61 73 65 20 54  STAR:.    case T
1ca10 4b 5f 4d 49 4e 55 53 3a 0a 20 20 20 20 63 61 73  K_MINUS:.    cas
1ca11 65 20 54 4b 5f 52 45 4d 3a 0a 20 20 20 20 63 61  e TK_REM:.    ca
1ca12 73 65 20 54 4b 5f 42 49 54 41 4e 44 3a 0a 20 20  se TK_BITAND:.  
1ca13 20 20 63 61 73 65 20 54 4b 5f 42 49 54 4f 52 3a    case TK_BITOR:
1ca14 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 4c 41  .    case TK_SLA
1ca15 53 48 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  SH:.    case TK_
1ca16 4c 53 48 49 46 54 3a 0a 20 20 20 20 63 61 73 65  LSHIFT:.    case
1ca17 20 54 4b 5f 52 53 48 49 46 54 3a 20 0a 20 20 20   TK_RSHIFT: .   
1ca18 20 63 61 73 65 20 54 4b 5f 43 4f 4e 43 41 54 3a   case TK_CONCAT:
1ca19 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
1ca1a 20 54 4b 5f 41 4e 44 3d 3d 4f 50 5f 41 6e 64 20   TK_AND==OP_And 
1ca1b 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
1ca1c 20 54 4b 5f 4f 52 3d 3d 4f 50 5f 4f 72 20 29 3b   TK_OR==OP_Or );
1ca1d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
1ca1e 4b 5f 50 4c 55 53 3d 3d 4f 50 5f 41 64 64 20 29  K_PLUS==OP_Add )
1ca1f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1ca20 54 4b 5f 4d 49 4e 55 53 3d 3d 4f 50 5f 53 75 62  TK_MINUS==OP_Sub
1ca21 74 72 61 63 74 20 29 3b 0a 20 20 20 20 20 20 61  tract );.      a
1ca22 73 73 65 72 74 28 20 54 4b 5f 52 45 4d 3d 3d 4f  ssert( TK_REM==O
1ca23 50 5f 52 65 6d 61 69 6e 64 65 72 20 29 3b 0a 20  P_Remainder );. 
1ca24 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
1ca25 42 49 54 41 4e 44 3d 3d 4f 50 5f 42 69 74 41 6e  BITAND==OP_BitAn
1ca26 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  d );.      asser
1ca27 74 28 20 54 4b 5f 42 49 54 4f 52 3d 3d 4f 50 5f  t( TK_BITOR==OP_
1ca28 42 69 74 4f 72 20 29 3b 0a 20 20 20 20 20 20 61  BitOr );.      a
1ca29 73 73 65 72 74 28 20 54 4b 5f 53 4c 41 53 48 3d  ssert( TK_SLASH=
1ca2a 3d 4f 50 5f 44 69 76 69 64 65 20 29 3b 0a 20 20  =OP_Divide );.  
1ca2b 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c      assert( TK_L
1ca2c 53 48 49 46 54 3d 3d 4f 50 5f 53 68 69 66 74 4c  SHIFT==OP_ShiftL
1ca2d 65 66 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73  eft );.      ass
1ca2e 65 72 74 28 20 54 4b 5f 52 53 48 49 46 54 3d 3d  ert( TK_RSHIFT==
1ca2f 4f 50 5f 53 68 69 66 74 52 69 67 68 74 20 29 3b  OP_ShiftRight );
1ca30 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
1ca31 4b 5f 43 4f 4e 43 41 54 3d 3d 4f 50 5f 43 6f 6e  K_CONCAT==OP_Con
1ca32 63 61 74 20 29 3b 0a 20 20 20 20 20 20 74 65 73  cat );.      tes
1ca33 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 41 4e  tcase( op==TK_AN
1ca34 44 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  D );.      testc
1ca35 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4f 52 20 29  ase( op==TK_OR )
1ca36 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1ca37 28 20 6f 70 3d 3d 54 4b 5f 50 4c 55 53 20 29 3b  ( op==TK_PLUS );
1ca38 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1ca39 20 6f 70 3d 3d 54 4b 5f 4d 49 4e 55 53 20 29 3b   op==TK_MINUS );
1ca3a 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1ca3b 20 6f 70 3d 3d 54 4b 5f 52 45 4d 20 29 3b 0a 20   op==TK_REM );. 
1ca3c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
1ca3d 70 3d 3d 54 4b 5f 42 49 54 41 4e 44 20 29 3b 0a  p==TK_BITAND );.
1ca3e 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1ca3f 6f 70 3d 3d 54 4b 5f 42 49 54 4f 52 20 29 3b 0a  op==TK_BITOR );.
1ca40 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1ca41 6f 70 3d 3d 54 4b 5f 53 4c 41 53 48 20 29 3b 0a  op==TK_SLASH );.
1ca42 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1ca43 6f 70 3d 3d 54 4b 5f 4c 53 48 49 46 54 20 29 3b  op==TK_LSHIFT );
1ca44 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1ca45 20 6f 70 3d 3d 54 4b 5f 52 53 48 49 46 54 20 29   op==TK_RSHIFT )
1ca46 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1ca47 28 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 43 41 54 20  ( op==TK_CONCAT 
1ca48 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71  );.      r1 = sq
1ca49 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d  lite3ExprCodeTem
1ca4a 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  p(pParse, pExpr-
1ca4b 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65  >pLeft, &regFree
1ca4c 31 29 3b 0a 20 20 20 20 20 20 72 32 20 3d 20 73  1);.      r2 = s
1ca4d 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65  qlite3ExprCodeTe
1ca4e 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  mp(pParse, pExpr
1ca4f 2d 3e 70 52 69 67 68 74 2c 20 26 72 65 67 46 72  ->pRight, &regFr
1ca50 65 65 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ee2);.      sqli
1ca51 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
1ca52 20 6f 70 2c 20 72 32 2c 20 72 31 2c 20 74 61 72   op, r2, r1, tar
1ca53 67 65 74 29 3b 0a 20 20 20 20 20 20 74 65 73 74  get);.      test
1ca54 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
1ca55 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  0 );.      testc
1ca56 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30  ase( regFree2==0
1ca57 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   );.      break;
1ca58 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1ca59 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20  TK_UMINUS: {.   
1ca5a 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d     Expr *pLeft =
1ca5b 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20   pExpr->pLeft;. 
1ca5c 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 65       assert( pLe
1ca5d 66 74 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ft );.      if( 
1ca5e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c  pLeft->op==TK_FL
1ca5f 4f 41 54 20 7c 7c 20 70 4c 65 66 74 2d 3e 6f 70  OAT || pLeft->op
1ca60 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a  ==TK_INTEGER ){.
1ca61 20 20 20 20 20 20 20 20 69 66 28 20 70 4c 65 66          if( pLef
1ca62 74 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20  t->op==TK_FLOAT 
1ca63 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 64  ){.          cod
1ca64 65 52 65 61 6c 28 76 2c 20 28 63 68 61 72 2a 29  eReal(v, (char*)
1ca65 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20  pLeft->token.z, 
1ca66 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 2e 6e 2c 20  pLeft->token.n, 
1ca67 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  1, target);.    
1ca68 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1ca69 20 20 20 20 20 63 6f 64 65 49 6e 74 65 67 65 72       codeInteger
1ca6a 28 76 2c 20 70 4c 65 66 74 2c 20 31 2c 20 74 61  (v, pLeft, 1, ta
1ca6b 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 7d  rget);.        }
1ca6c 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1ca6d 20 20 20 20 20 20 72 65 67 46 72 65 65 31 20 3d        regFree1 =
1ca6e 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r1 = sqlite3Get
1ca6f 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
1ca70 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1ca71 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1ca72 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 31 29  _Integer, 0, r1)
1ca73 3b 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 73  ;.        r2 = s
1ca74 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65  qlite3ExprCodeTe
1ca75 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  mp(pParse, pExpr
1ca76 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65  ->pLeft, &regFre
1ca77 65 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  e2);.        sql
1ca78 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
1ca79 2c 20 4f 50 5f 53 75 62 74 72 61 63 74 2c 20 72  , OP_Subtract, r
1ca7a 32 2c 20 72 31 2c 20 74 61 72 67 65 74 29 3b 0a  2, r1, target);.
1ca7b 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
1ca7c 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b  ( regFree2==0 );
1ca7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
1ca7e 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a 20  nReg = target;. 
1ca7f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1ca80 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 49  }.    case TK_BI
1ca81 54 4e 4f 54 3a 0a 20 20 20 20 63 61 73 65 20 54  TNOT:.    case T
1ca82 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 61  K_NOT: {.      a
1ca83 73 73 65 72 74 28 20 54 4b 5f 42 49 54 4e 4f 54  ssert( TK_BITNOT
1ca84 3d 3d 4f 50 5f 42 69 74 4e 6f 74 20 29 3b 0a 20  ==OP_BitNot );. 
1ca85 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
1ca86 4e 4f 54 3d 3d 4f 50 5f 4e 6f 74 20 29 3b 0a 20  NOT==OP_Not );. 
1ca87 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
1ca88 70 3d 3d 54 4b 5f 42 49 54 4e 4f 54 20 29 3b 0a  p==TK_BITNOT );.
1ca89 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1ca8a 6f 70 3d 3d 54 4b 5f 4e 4f 54 20 29 3b 0a 20 20  op==TK_NOT );.  
1ca8b 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69      inReg = sqli
1ca8c 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65  te3ExprCodeTarge
1ca8d 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  t(pParse, pExpr-
1ca8e 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b  >pLeft, target);
1ca8f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1ca90 20 69 6e 52 65 67 3d 3d 74 61 72 67 65 74 20 29   inReg==target )
1ca91 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1ca92 28 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61  ( usedAsColumnCa
1ca93 63 68 65 28 70 50 61 72 73 65 2c 20 69 6e 52 65  che(pParse, inRe
1ca94 67 2c 20 69 6e 52 65 67 29 20 29 3b 0a 20 20 20  g, inReg) );.   
1ca95 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74     inReg = sqlit
1ca96 65 33 45 78 70 72 57 72 69 74 61 62 6c 65 52 65  e3ExprWritableRe
1ca97 67 69 73 74 65 72 28 70 50 61 72 73 65 2c 20 69  gister(pParse, i
1ca98 6e 52 65 67 2c 20 74 61 72 67 65 74 29 3b 0a 20  nReg, target);. 
1ca99 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1ca9a 41 64 64 4f 70 31 28 76 2c 20 6f 70 2c 20 69 6e  AddOp1(v, op, in
1ca9b 52 65 67 29 3b 0a 20 20 20 20 20 20 62 72 65 61  Reg);.      brea
1ca9c 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
1ca9d 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20  e TK_ISNULL:.   
1ca9e 20 63 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c   case TK_NOTNULL
1ca9f 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 64  : {.      int ad
1caa0 64 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  dr;.      assert
1caa1 28 20 54 4b 5f 49 53 4e 55 4c 4c 3d 3d 4f 50 5f  ( TK_ISNULL==OP_
1caa2 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20  IsNull );.      
1caa3 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f 54 4e 55  assert( TK_NOTNU
1caa4 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29  LL==OP_NotNull )
1caa5 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1caa6 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20  ( op==TK_ISNULL 
1caa7 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1caa8 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c  e( op==TK_NOTNUL
1caa9 4c 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  L );.      sqlit
1caaa 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1caab 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 74  OP_Integer, 1, t
1caac 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 72 31  arget);.      r1
1caad 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
1caae 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70  deTemp(pParse, p
1caaf 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65  Expr->pLeft, &re
1cab0 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 74  gFree1);.      t
1cab1 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
1cab2 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 64  1==0 );.      ad
1cab3 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
1cab4 41 64 64 4f 70 31 28 76 2c 20 6f 70 2c 20 72 31  AddOp1(v, op, r1
1cab5 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1cab6 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1cab7 5f 41 64 64 49 6d 6d 2c 20 74 61 72 67 65 74 2c  _AddImm, target,
1cab8 20 2d 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69   -1);.      sqli
1cab9 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
1caba 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20  v, addr);.      
1cabb 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1cabc 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e   case TK_AGG_FUN
1cabd 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 41  CTION: {.      A
1cabe 67 67 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20  ggInfo *pInfo = 
1cabf 70 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 3b  pExpr->pAggInfo;
1cac0 0a 20 20 20 20 20 20 69 66 28 20 70 49 6e 66 6f  .      if( pInfo
1cac1 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ==0 ){.        s
1cac2 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1cac3 50 61 72 73 65 2c 20 22 6d 69 73 75 73 65 20 6f  Parse, "misuse o
1cac4 66 20 61 67 67 72 65 67 61 74 65 3a 20 25 54 22  f aggregate: %T"
1cac5 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 26 70  ,.            &p
1cac6 45 78 70 72 2d 3e 73 70 61 6e 29 3b 0a 20 20 20  Expr->span);.   
1cac7 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1cac8 20 20 69 6e 52 65 67 20 3d 20 70 49 6e 66 6f 2d    inReg = pInfo-
1cac9 3e 61 46 75 6e 63 5b 70 45 78 70 72 2d 3e 69 41  >aFunc[pExpr->iA
1caca 67 67 5d 2e 69 4d 65 6d 3b 0a 20 20 20 20 20 20  gg].iMem;.      
1cacb 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
1cacc 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
1cacd 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20  _CONST_FUNC:.   
1cace 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f   case TK_FUNCTIO
1cacf 4e 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c  N: {.      ExprL
1cad0 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78  ist *pList = pEx
1cad1 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 20 20 20  pr->pList;.     
1cad2 20 69 6e 74 20 6e 45 78 70 72 20 3d 20 70 4c 69   int nExpr = pLi
1cad3 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70  st ? pList->nExp
1cad4 72 20 3a 20 30 3b 0a 20 20 20 20 20 20 46 75 6e  r : 0;.      Fun
1cad5 63 44 65 66 20 2a 70 44 65 66 3b 0a 20 20 20 20  cDef *pDef;.    
1cad6 20 20 69 6e 74 20 6e 49 64 3b 0a 20 20 20 20 20    int nId;.     
1cad7 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 64   const char *zId
1cad8 3b 0a 20 20 20 20 20 20 69 6e 74 20 63 6f 6e 73  ;.      int cons
1cad9 74 4d 61 73 6b 20 3d 20 30 3b 0a 20 20 20 20 20  tMask = 0;.     
1cada 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 75 38   int i;.      u8
1cadb 20 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 0a   enc = ENC(db);.
1cadc 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70        CollSeq *p
1cadd 43 6f 6c 6c 20 3d 20 30 3b 0a 0a 20 20 20 20 20  Coll = 0;..     
1cade 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
1cadf 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 29 3b 0a  K_CONST_FUNC );.
1cae0 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1cae1 6f 70 3d 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e 20  op==TK_FUNCTION 
1cae2 29 3b 0a 20 20 20 20 20 20 7a 49 64 20 3d 20 28  );.      zId = (
1cae3 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b  char*)pExpr->tok
1cae4 65 6e 2e 7a 3b 0a 20 20 20 20 20 20 6e 49 64 20  en.z;.      nId 
1cae5 3d 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e  = pExpr->token.n
1cae6 3b 0a 20 20 20 20 20 20 70 44 65 66 20 3d 20 73  ;.      pDef = s
1cae7 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69  qlite3FindFuncti
1cae8 6f 6e 28 64 62 2c 20 7a 49 64 2c 20 6e 49 64 2c  on(db, zId, nId,
1cae9 20 6e 45 78 70 72 2c 20 65 6e 63 2c 20 30 29 3b   nExpr, enc, 0);
1caea 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1caeb 44 65 66 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  Def!=0 );.      
1caec 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20  if( pList ){.   
1caed 20 20 20 20 20 6e 45 78 70 72 20 3d 20 70 4c 69       nExpr = pLi
1caee 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20  st->nExpr;.     
1caef 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47     r1 = sqlite3G
1caf0 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  etTempRange(pPar
1caf1 73 65 2c 20 6e 45 78 70 72 29 3b 0a 20 20 20 20  se, nExpr);.    
1caf2 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
1caf3 6f 64 65 45 78 70 72 4c 69 73 74 28 70 50 61 72  odeExprList(pPar
1caf4 73 65 2c 20 70 4c 69 73 74 2c 20 72 31 2c 20 31  se, pList, r1, 1
1caf5 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
1caf6 20 20 20 20 20 20 20 20 6e 45 78 70 72 20 3d 20          nExpr = 
1caf7 72 31 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  r1 = 0;.      }.
1caf8 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1caf9 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1cafa 0a 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 62  .      /* Possib
1cafb 6c 79 20 6f 76 65 72 6c 6f 61 64 20 74 68 65 20  ly overload the 
1cafc 66 75 6e 63 74 69 6f 6e 20 69 66 20 74 68 65 20  function if the 
1cafd 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69  first argument i
1cafe 73 0a 20 20 20 20 20 20 2a 2a 20 61 20 76 69 72  s.      ** a vir
1caff 74 75 61 6c 20 74 61 62 6c 65 20 63 6f 6c 75 6d  tual table colum
1cb00 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  n..      **.    
1cb01 20 20 2a 2a 20 46 6f 72 20 69 6e 66 69 78 20 66    ** For infix f
1cb02 75 6e 63 74 69 6f 6e 73 20 28 4c 49 4b 45 2c 20  unctions (LIKE, 
1cb03 47 4c 4f 42 2c 20 52 45 47 45 58 50 2c 20 61 6e  GLOB, REGEXP, an
1cb04 64 20 4d 41 54 43 48 29 20 75 73 65 20 74 68 65  d MATCH) use the
1cb05 0a 20 20 20 20 20 20 2a 2a 20 73 65 63 6f 6e 64  .      ** second
1cb06 20 61 72 67 75 6d 65 6e 74 2c 20 6e 6f 74 20 74   argument, not t
1cb07 68 65 20 66 69 72 73 74 2c 20 61 73 20 74 68 65  he first, as the
1cb08 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 65 73   argument to tes
1cb09 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 65  t to.      ** se
1cb0a 65 20 69 66 20 69 74 20 69 73 20 61 20 63 6f 6c  e if it is a col
1cb0b 75 6d 6e 20 69 6e 20 61 20 76 69 72 74 75 61 6c  umn in a virtual
1cb0c 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73   table.  This is
1cb0d 20 64 6f 6e 65 20 62 65 63 61 75 73 65 0a 20 20   done because.  
1cb0e 20 20 20 20 2a 2a 20 74 68 65 20 6c 65 66 74 20      ** the left 
1cb0f 6f 70 65 72 61 6e 64 20 6f 66 20 69 6e 66 69 78  operand of infix
1cb10 20 66 75 6e 63 74 69 6f 6e 73 20 28 74 68 65 20   functions (the 
1cb11 6f 70 65 72 61 6e 64 20 77 65 20 77 61 6e 74 20  operand we want 
1cb12 74 6f 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74  to.      ** cont
1cb13 72 6f 6c 20 6f 76 65 72 6c 6f 61 64 69 6e 67 29  rol overloading)
1cb14 20 65 6e 64 73 20 75 70 20 61 73 20 74 68 65 20   ends up as the 
1cb15 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
1cb16 74 6f 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  to the.      ** 
1cb17 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 65  function.  The e
1cb18 78 70 72 65 73 73 69 6f 6e 20 22 41 20 67 6c 6f  xpression "A glo
1cb19 62 20 42 22 20 69 73 20 65 71 75 69 76 61 6c 65  b B" is equivale
1cb1a 6e 74 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20  nt to .      ** 
1cb1b 22 67 6c 6f 62 28 42 2c 41 29 2e 20 20 57 65 20  "glob(B,A).  We 
1cb1c 77 61 6e 74 20 74 6f 20 75 73 65 20 74 68 65 20  want to use the 
1cb1d 41 20 69 6e 20 22 41 20 67 6c 6f 62 20 42 22 20  A in "A glob B" 
1cb1e 74 6f 20 74 65 73 74 0a 20 20 20 20 20 20 2a 2a  to test.      **
1cb1f 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 20 6f 76   for function ov
1cb20 65 72 6c 6f 61 64 69 6e 67 2e 20 20 42 75 74 20  erloading.  But 
1cb21 77 65 20 75 73 65 20 74 68 65 20 42 20 74 65 72  we use the B ter
1cb22 6d 20 69 6e 20 22 67 6c 6f 62 28 42 2c 41 29 22  m in "glob(B,A)"
1cb23 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
1cb24 20 69 66 28 20 6e 45 78 70 72 3e 3d 32 20 26 26   if( nExpr>=2 &&
1cb25 20 28 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26   (pExpr->flags &
1cb26 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 29 20 29   EP_InfixFunc) )
1cb27 7b 0a 20 20 20 20 20 20 20 20 70 44 65 66 20 3d  {.        pDef =
1cb28 20 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72   sqlite3VtabOver
1cb29 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c  loadFunction(db,
1cb2a 20 70 44 65 66 2c 20 6e 45 78 70 72 2c 20 70 4c   pDef, nExpr, pL
1cb2b 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 29  ist->a[1].pExpr)
1cb2c 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
1cb2d 28 20 6e 45 78 70 72 3e 30 20 29 7b 0a 20 20 20  ( nExpr>0 ){.   
1cb2e 20 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69       pDef = sqli
1cb2f 74 65 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 46  te3VtabOverloadF
1cb30 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 44 65 66  unction(db, pDef
1cb31 2c 20 6e 45 78 70 72 2c 20 70 4c 69 73 74 2d 3e  , nExpr, pList->
1cb32 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20  a[0].pExpr);.   
1cb33 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
1cb34 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 45 78    for(i=0; i<nEx
1cb35 70 72 20 26 26 20 69 3c 33 32 3b 20 69 2b 2b 29  pr && i<32; i++)
1cb36 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71  {.        if( sq
1cb37 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
1cb38 61 6e 74 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  ant(pList->a[i].
1cb39 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20  pExpr) ){.      
1cb3a 20 20 20 20 63 6f 6e 73 74 4d 61 73 6b 20 7c 3d      constMask |=
1cb3b 20 28 31 3c 3c 69 29 3b 0a 20 20 20 20 20 20 20   (1<<i);.       
1cb3c 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 28   }.        if( (
1cb3d 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51  pDef->flags & SQ
1cb3e 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f  LITE_FUNC_NEEDCO
1cb3f 4c 4c 29 21 3d 30 20 26 26 20 21 70 43 6f 6c 6c  LL)!=0 && !pColl
1cb40 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43   ){.          pC
1cb41 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70  oll = sqlite3Exp
1cb42 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  rCollSeq(pParse,
1cb43 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78   pList->a[i].pEx
1cb44 70 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  pr);.        }. 
1cb45 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
1cb46 20 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53   pDef->flags & S
1cb47 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43  QLITE_FUNC_NEEDC
1cb48 4f 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 69  OLL ){.        i
1cb49 66 28 20 21 70 43 6f 6c 6c 20 29 20 70 43 6f 6c  f( !pColl ) pCol
1cb4a 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c  l = db->pDfltCol
1cb4b 6c 3b 20 0a 20 20 20 20 20 20 20 20 73 71 6c 69  l; .        sqli
1cb4c 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
1cb4d 20 4f 50 5f 43 6f 6c 6c 53 65 71 2c 20 30 2c 20   OP_CollSeq, 0, 
1cb4e 30 2c 20 30 2c 20 28 63 68 61 72 20 2a 29 70 43  0, 0, (char *)pC
1cb4f 6f 6c 6c 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29  oll, P4_COLLSEQ)
1cb50 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1cb51 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1cb52 34 28 76 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e  4(v, OP_Function
1cb53 2c 20 63 6f 6e 73 74 4d 61 73 6b 2c 20 72 31 2c  , constMask, r1,
1cb54 20 74 61 72 67 65 74 2c 0a 20 20 20 20 20 20 20   target,.       
1cb55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cb56 20 28 63 68 61 72 2a 29 70 44 65 66 2c 20 50 34   (char*)pDef, P4
1cb57 5f 46 55 4e 43 44 45 46 29 3b 0a 20 20 20 20 20  _FUNCDEF);.     
1cb58 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1cb59 67 65 50 35 28 76 2c 20 6e 45 78 70 72 29 3b 0a  geP5(v, nExpr);.
1cb5a 20 20 20 20 20 20 69 66 28 20 6e 45 78 70 72 20        if( nExpr 
1cb5b 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1cb5c 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e  e3ReleaseTempRan
1cb5d 67 65 28 70 50 61 72 73 65 2c 20 72 31 2c 20 6e  ge(pParse, r1, n
1cb5e 45 78 70 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Expr);.      }. 
1cb5f 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1cb60 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
1cb61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 31 2c 20  nge(pParse, r1, 
1cb62 6e 45 78 70 72 29 3b 0a 20 20 20 20 20 20 62 72  nExpr);.      br
1cb63 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64  eak;.    }.#ifnd
1cb64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
1cb65 55 42 51 55 45 52 59 0a 20 20 20 20 63 61 73 65  UBQUERY.    case
1cb66 20 54 4b 5f 45 58 49 53 54 53 3a 0a 20 20 20 20   TK_EXISTS:.    
1cb67 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a 20  case TK_SELECT: 
1cb68 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
1cb69 28 20 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20  ( op==TK_EXISTS 
1cb6a 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1cb6b 65 28 20 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54  e( op==TK_SELECT
1cb6c 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45   );.      if( pE
1cb6d 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 30 20  xpr->iColumn==0 
1cb6e 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1cb6f 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28  e3CodeSubselect(
1cb70 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 30  pParse, pExpr, 0
1cb71 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 0);.      }.  
1cb72 20 20 20 20 69 6e 52 65 67 20 3d 20 70 45 78 70      inReg = pExp
1cb73 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  r->iColumn;.    
1cb74 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1cb75 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a 20 7b     case TK_IN: {
1cb76 0a 20 20 20 20 20 20 69 6e 74 20 72 4e 6f 74 46  .      int rNotF
1cb77 6f 75 6e 64 20 3d 20 30 3b 0a 20 20 20 20 20 20  ound = 0;.      
1cb78 69 6e 74 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c  int rMayHaveNull
1cb79 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e 74 20   = 0;.      int 
1cb7a 6a 32 2c 20 6a 33 2c 20 6a 34 2c 20 6a 35 3b 0a  j2, j3, j4, j5;.
1cb7b 20 20 20 20 20 20 63 68 61 72 20 61 66 66 69 6e        char affin
1cb7c 69 74 79 3b 0a 20 20 20 20 20 20 69 6e 74 20 65  ity;.      int e
1cb7d 54 79 70 65 3b 0a 0a 20 20 20 20 20 20 56 64 62  Type;..      Vdb
1cb7e 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c  eNoopComment((v,
1cb7f 20 22 62 65 67 69 6e 20 49 4e 20 65 78 70 72 20   "begin IN expr 
1cb80 72 25 64 22 2c 20 74 61 72 67 65 74 29 29 3b 0a  r%d", target));.
1cb81 20 20 20 20 20 20 65 54 79 70 65 20 3d 20 73 71        eType = sq
1cb82 6c 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78  lite3FindInIndex
1cb83 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20  (pParse, pExpr, 
1cb84 26 72 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a  &rMayHaveNull);.
1cb85 20 20 20 20 20 20 69 66 28 20 72 4d 61 79 48 61        if( rMayHa
1cb86 76 65 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20  veNull ){.      
1cb87 20 20 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 2b 2b    rNotFound = ++
1cb88 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
1cb89 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
1cb8a 46 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 61  Figure out the a
1cb8b 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65 20 74  ffinity to use t
1cb8c 6f 20 63 72 65 61 74 65 20 61 20 6b 65 79 20 66  o create a key f
1cb8d 72 6f 6d 20 74 68 65 20 72 65 73 75 6c 74 73 0a  rom the results.
1cb8e 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20        ** of the 
1cb8f 65 78 70 72 65 73 73 69 6f 6e 2e 20 61 66 66 69  expression. affi
1cb90 6e 69 74 79 53 74 72 20 73 74 6f 72 65 73 20 61  nityStr stores a
1cb91 20 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 73   static string s
1cb92 75 69 74 61 62 6c 65 20 66 6f 72 0a 20 20 20 20  uitable for.    
1cb93 20 20 2a 2a 20 50 34 20 6f 66 20 4f 50 5f 4d 61    ** P4 of OP_Ma
1cb94 6b 65 52 65 63 6f 72 64 2e 0a 20 20 20 20 20 20  keRecord..      
1cb95 2a 2f 0a 20 20 20 20 20 20 61 66 66 69 6e 69 74  */.      affinit
1cb96 79 20 3d 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66  y = comparisonAf
1cb97 66 69 6e 69 74 79 28 70 45 78 70 72 29 3b 0a 0a  finity(pExpr);..
1cb98 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64 65 20 74  .      /* Code t
1cb99 68 65 20 3c 65 78 70 72 3e 20 66 72 6f 6d 20 22  he <expr> from "
1cb9a 3c 65 78 70 72 3e 20 49 4e 20 28 2e 2e 2e 29 22  <expr> IN (...)"
1cb9b 2e 20 54 68 65 20 74 65 6d 70 6f 72 61 72 79 20  . The temporary 
1cb9c 74 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 70  table.      ** p
1cb9d 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 63 6f 6e  Expr->iTable con
1cb9e 74 61 69 6e 73 20 74 68 65 20 76 61 6c 75 65 73  tains the values
1cb9f 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20 74 68   that make up th
1cba0 65 20 28 2e 2e 2e 29 20 73 65 74 2e 0a 20 20 20  e (...) set..   
1cba1 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 50 61 72     */.      pPar
1cba2 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61  se->disableColCa
1cba3 63 68 65 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c  che++;.      sql
1cba4 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
1cba5 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
1cba6 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  t, target);.    
1cba7 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c    pParse->disabl
1cba8 65 43 6f 6c 43 61 63 68 65 2d 2d 3b 0a 20 20 20  eColCache--;.   
1cba9 20 20 20 6a 32 20 3d 20 73 71 6c 69 74 65 33 56     j2 = sqlite3V
1cbaa 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
1cbab 49 73 4e 75 6c 6c 2c 20 74 61 72 67 65 74 29 3b  IsNull, target);
1cbac 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 70 65  .      if( eType
1cbad 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44  ==IN_INDEX_ROWID
1cbae 20 29 7b 0a 20 20 20 20 20 20 20 20 6a 33 20 3d   ){.        j3 =
1cbaf 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1cbb0 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49  p1(v, OP_MustBeI
1cbb1 6e 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20  nt, target);.   
1cbb2 20 20 20 20 20 6a 34 20 3d 20 73 71 6c 69 74 65       j4 = sqlite
1cbb3 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
1cbb4 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 70 45 78  P_NotExists, pEx
1cbb5 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 74  pr->iTable, 0, t
1cbb6 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20  arget);.        
1cbb7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1cbb8 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
1cbb9 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20   1, target);.   
1cbba 20 20 20 20 20 6a 35 20 3d 20 73 71 6c 69 74 65       j5 = sqlite
1cbbb 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f  3VdbeAddOp0(v, O
1cbbc 50 5f 47 6f 74 6f 29 3b 0a 20 20 20 20 20 20 20  P_Goto);.       
1cbbd 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
1cbbe 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a 20 20 20  Here(v, j3);.   
1cbbf 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1cbc0 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 34 29 3b  JumpHere(v, j4);
1cbc1 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1cbc2 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1cbc3 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 74 61 72  _Integer, 0, tar
1cbc4 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  get);.      }els
1cbc5 65 7b 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20  e{.        r2 = 
1cbc6 72 65 67 46 72 65 65 32 20 3d 20 73 71 6c 69 74  regFree2 = sqlit
1cbc7 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
1cbc8 72 73 65 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f  rse);..        /
1cbc9 2a 20 43 72 65 61 74 65 20 61 20 72 65 63 6f 72  * Create a recor
1cbca 64 20 61 6e 64 20 74 65 73 74 20 66 6f 72 20 73  d and test for s
1cbcb 65 74 20 6d 65 6d 62 65 72 73 68 69 70 2e 20 49  et membership. I
1cbcc 66 20 74 68 65 20 73 65 74 20 63 6f 6e 74 61 69  f the set contai
1cbcd 6e 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  ns.        ** th
1cbce 65 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 6a 75  e value, then ju
1cbcf 6d 70 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  mp to the end of
1cbd0 20 74 68 65 20 74 65 73 74 20 63 6f 64 65 2e 20   the test code. 
1cbd1 54 68 65 20 74 61 72 67 65 74 0a 20 20 20 20 20  The target.     
1cbd2 20 20 20 2a 2a 20 72 65 67 69 73 74 65 72 20 73     ** register s
1cbd3 74 69 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 74 68  till contains th
1cbd4 65 20 74 72 75 65 20 28 31 29 20 76 61 6c 75 65  e true (1) value
1cbd5 20 77 72 69 74 74 65 6e 20 74 6f 20 69 74 20 65   written to it e
1cbd6 61 72 6c 69 65 72 2e 0a 20 20 20 20 20 20 20 20  arlier..        
1cbd7 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  */.        sqlit
1cbd8 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
1cbd9 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 74  OP_MakeRecord, t
1cbda 61 72 67 65 74 2c 20 31 2c 20 72 32 2c 20 26 61  arget, 1, r2, &a
1cbdb 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20  ffinity, 1);.   
1cbdc 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1cbdd 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
1cbde 65 67 65 72 2c 20 31 2c 20 74 61 72 67 65 74 29  eger, 1, target)
1cbdf 3b 0a 20 20 20 20 20 20 20 20 6a 35 20 3d 20 73  ;.        j5 = s
1cbe0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
1cbe1 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 70 45  (v, OP_Found, pE
1cbe2 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20  xpr->iTable, 0, 
1cbe3 72 32 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a  r2);..        /*
1cbe4 20 49 66 20 74 68 65 20 73 65 74 20 6d 65 6d 62   If the set memb
1cbe5 65 72 73 68 69 70 20 74 65 73 74 20 66 61 69 6c  ership test fail
1cbe6 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  s, then the resu
1cbe7 6c 74 20 6f 66 20 74 68 65 20 0a 20 20 20 20 20  lt of the .     
1cbe8 20 20 20 2a 2a 20 22 78 20 49 4e 20 28 2e 2e 2e     ** "x IN (...
1cbe9 29 22 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 75  )" expression mu
1cbea 73 74 20 62 65 20 65 69 74 68 65 72 20 30 20 6f  st be either 0 o
1cbeb 72 20 4e 55 4c 4c 2e 20 49 66 20 74 68 65 20 73  r NULL. If the s
1cbec 65 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f  et.        ** co
1cbed 6e 74 61 69 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76  ntains no NULL v
1cbee 61 6c 75 65 73 2c 20 74 68 65 6e 20 74 68 65 20  alues, then the 
1cbef 72 65 73 75 6c 74 20 69 73 20 30 2e 20 49 66 20  result is 0. If 
1cbf0 74 68 65 20 73 65 74 20 0a 20 20 20 20 20 20 20  the set .       
1cbf1 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65   ** contains one
1cbf2 20 6f 72 20 6d 6f 72 65 20 4e 55 4c 4c 20 76 61   or more NULL va
1cbf3 6c 75 65 73 2c 20 74 68 65 6e 20 74 68 65 20 72  lues, then the r
1cbf4 65 73 75 6c 74 20 6f 66 20 74 68 65 0a 20 20 20  esult of the.   
1cbf5 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69       ** expressi
1cbf6 6f 6e 20 69 73 20 61 6c 73 6f 20 4e 55 4c 4c 2e  on is also NULL.
1cbf7 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
1cbf8 20 20 20 20 69 66 28 20 72 4e 6f 74 46 6f 75 6e      if( rNotFoun
1cbf9 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  d==0 ){.        
1cbfa 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 68    /* This branch
1cbfb 20 72 75 6e 73 20 69 66 20 69 74 20 69 73 20 6b   runs if it is k
1cbfc 6e 6f 77 6e 20 61 74 20 63 6f 6d 70 69 6c 65 20  nown at compile 
1cbfd 74 69 6d 65 20 28 6e 6f 77 29 20 74 68 61 74 20  time (now) that 
1cbfe 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68  .          ** th
1cbff 65 20 73 65 74 20 63 6f 6e 74 61 69 6e 73 20 6e  e set contains n
1cc00 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 73 2e 20 54  o NULL values. T
1cc01 68 69 73 20 68 61 70 70 65 6e 73 20 61 73 20 74  his happens as t
1cc02 68 65 20 72 65 73 75 6c 74 0a 20 20 20 20 20 20  he result.      
1cc03 20 20 20 20 2a 2a 20 6f 66 20 61 20 22 4e 4f 54      ** of a "NOT
1cc04 20 4e 55 4c 4c 22 20 63 6f 6e 73 74 72 61 69 6e   NULL" constrain
1cc05 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  t in the databas
1cc06 65 20 73 63 68 65 6d 61 2e 20 4e 6f 20 6e 65 65  e schema. No nee
1cc07 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74  d.          ** t
1cc08 6f 20 74 65 73 74 20 74 68 65 20 64 61 74 61 20  o test the data 
1cc09 73 74 72 75 63 74 75 72 65 20 61 74 20 72 75 6e  structure at run
1cc0a 74 69 6d 65 20 69 6e 20 74 68 69 73 20 63 61 73  time in this cas
1cc0b 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  e..          */.
1cc0c 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1cc0d 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1cc0e 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 74 61  P_Integer, 0, ta
1cc0f 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 7d  rget);.        }
1cc10 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
1cc11 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 70 6f  /* This block po
1cc12 70 75 6c 61 74 65 73 20 74 68 65 20 72 4e 6f 74  pulates the rNot
1cc13 46 6f 75 6e 64 20 72 65 67 69 73 74 65 72 20 77  Found register w
1cc14 69 74 68 20 65 69 74 68 65 72 20 4e 55 4c 4c 0a  ith either NULL.
1cc15 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20            ** or 
1cc16 30 20 28 61 6e 20 69 6e 74 65 67 65 72 20 76 61  0 (an integer va
1cc17 6c 75 65 29 2e 20 49 66 20 74 68 65 20 64 61 74  lue). If the dat
1cc18 61 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74  a structure cont
1cc19 61 69 6e 73 20 6f 6e 65 0a 20 20 20 20 20 20 20  ains one.       
1cc1a 20 20 20 2a 2a 20 6f 72 20 6d 6f 72 65 20 4e 55     ** or more NU
1cc1b 4c 4c 73 2c 20 74 68 65 6e 20 73 65 74 20 72 4e  LLs, then set rN
1cc1c 6f 74 46 6f 75 6e 64 20 74 6f 20 4e 55 4c 4c 2e  otFound to NULL.
1cc1d 20 4f 74 68 65 72 77 69 73 65 2c 20 73 65 74 20   Otherwise, set 
1cc1e 69 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  it.          ** 
1cc1f 74 6f 20 30 2e 20 49 66 20 72 65 67 69 73 74 65  to 0. If registe
1cc20 72 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 69  r rMayHaveNull i
1cc21 73 20 61 6c 72 65 61 64 79 20 73 65 74 20 74 6f  s already set to
1cc22 20 73 6f 6d 65 20 76 61 6c 75 65 0a 20 20 20 20   some value.    
1cc23 20 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 74        ** other t
1cc24 68 61 6e 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74  han NULL, then t
1cc25 68 65 20 74 65 73 74 20 68 61 73 20 61 6c 72 65  he test has alre
1cc26 61 64 79 20 62 65 65 6e 20 72 75 6e 20 61 6e 64  ady been run and
1cc27 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 72   .          ** r
1cc28 4e 6f 74 46 6f 75 6e 64 20 69 73 20 61 6c 72 65  NotFound is alre
1cc29 61 64 79 20 70 6f 70 75 6c 61 74 65 64 2e 0a 20  ady populated.. 
1cc2a 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
1cc2b 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e        static con
1cc2c 73 74 20 63 68 61 72 20 6e 75 6c 6c 52 65 63 6f  st char nullReco
1cc2d 72 64 5b 5d 20 3d 20 7b 20 30 78 30 32 2c 20 30  rd[] = { 0x02, 0
1cc2e 78 30 30 20 7d 3b 0a 20 20 20 20 20 20 20 20 20  x00 };.         
1cc2f 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j3 = sqlite3Vdb
1cc30 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f  eAddOp1(v, OP_No
1cc31 74 4e 75 6c 6c 2c 20 72 4d 61 79 48 61 76 65 4e  tNull, rMayHaveN
1cc32 75 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ull);.          
1cc33 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1cc34 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
1cc35 20 72 4e 6f 74 46 6f 75 6e 64 29 3b 0a 20 20 20   rNotFound);.   
1cc36 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1cc37 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 42  beAddOp4(v, OP_B
1cc38 6c 6f 62 2c 20 32 2c 20 72 4d 61 79 48 61 76 65  lob, 2, rMayHave
1cc39 4e 75 6c 6c 2c 20 30 2c 20 0a 20 20 20 20 20 20  Null, 0, .      
1cc3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cc3b 20 20 20 20 20 20 20 6e 75 6c 6c 52 65 63 6f 72         nullRecor
1cc3c 64 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  d, P4_STATIC);. 
1cc3d 20 20 20 20 20 20 20 20 20 6a 34 20 3d 20 73 71           j4 = sq
1cc3e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
1cc3f 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 78  v, OP_Found, pEx
1cc40 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 72  pr->iTable, 0, r
1cc41 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20  MayHaveNull);.  
1cc42 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1cc43 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1cc44 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 4e 6f 74  Integer, 0, rNot
1cc45 46 6f 75 6e 64 29 3b 0a 20 20 20 20 20 20 20 20  Found);.        
1cc46 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
1cc47 70 48 65 72 65 28 76 2c 20 6a 34 29 3b 0a 20 20  pHere(v, j4);.  
1cc48 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1cc49 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
1cc4a 33 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f  3);..          /
1cc4b 2a 20 43 6f 70 79 20 74 68 65 20 76 61 6c 75 65  * Copy the value
1cc4c 20 6f 66 20 72 65 67 69 73 74 65 72 20 72 4e 6f   of register rNo
1cc4d 74 46 6f 75 6e 64 20 28 77 68 69 63 68 20 69 73  tFound (which is
1cc4e 20 65 69 74 68 65 72 20 4e 55 4c 4c 20 6f 72 20   either NULL or 
1cc4f 30 29 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  0).          ** 
1cc50 69 6e 74 6f 20 74 68 65 20 74 61 72 67 65 74 20  into the target 
1cc51 72 65 67 69 73 74 65 72 2e 20 54 68 69 73 20 77  register. This w
1cc52 69 6c 6c 20 62 65 20 74 68 65 20 72 65 73 75 6c  ill be the resul
1cc53 74 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20  t of the.       
1cc54 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e     ** expression
1cc55 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
1cc56 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1cc57 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1cc58 5f 43 6f 70 79 2c 20 72 4e 6f 74 46 6f 75 6e 64  _Copy, rNotFound
1cc59 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
1cc5a 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1cc5b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
1cc5c 6d 70 48 65 72 65 28 76 2c 20 6a 32 29 3b 0a 20  mpHere(v, j2);. 
1cc5d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1cc5e 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 35 29 3b  JumpHere(v, j5);
1cc5f 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
1cc60 6e 74 28 28 76 2c 20 22 65 6e 64 20 49 4e 20 65  nt((v, "end IN e
1cc61 78 70 72 20 72 25 64 22 2c 20 74 61 72 67 65 74  xpr r%d", target
1cc62 29 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ));.      break;
1cc63 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
1cc64 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 20 20 20 78    /*.    **    x
1cc65 20 42 45 54 57 45 45 4e 20 79 20 41 4e 44 20 7a   BETWEEN y AND z
1cc66 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
1cc67 68 69 73 20 69 73 20 65 71 75 69 76 61 6c 65 6e  his is equivalen
1cc68 74 20 74 6f 0a 20 20 20 20 2a 2a 0a 20 20 20 20  t to.    **.    
1cc69 2a 2a 20 20 20 20 78 3e 3d 79 20 41 4e 44 20 78  **    x>=y AND x
1cc6a 3c 3d 7a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  <=z.    **.    *
1cc6b 2a 20 58 20 69 73 20 73 74 6f 72 65 64 20 69 6e  * X is stored in
1cc6c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2e 0a 20   pExpr->pLeft.. 
1cc6d 20 20 20 2a 2a 20 59 20 69 73 20 73 74 6f 72 65     ** Y is store
1cc6e 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73  d in pExpr->pLis
1cc6f 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2e 0a 20  t->a[0].pExpr.. 
1cc70 20 20 20 2a 2a 20 5a 20 69 73 20 73 74 6f 72 65     ** Z is store
1cc71 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73  d in pExpr->pLis
1cc72 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 2e 0a 20  t->a[1].pExpr.. 
1cc73 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54     */.    case T
1cc74 4b 5f 42 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20  K_BETWEEN: {.   
1cc75 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d     Expr *pLeft =
1cc76 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20   pExpr->pLeft;. 
1cc77 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72       struct Expr
1cc78 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c 49 74 65  List_item *pLIte
1cc79 6d 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74  m = pExpr->pList
1cc7a 2d 3e 61 3b 0a 20 20 20 20 20 20 45 78 70 72 20  ->a;.      Expr 
1cc7b 2a 70 52 69 67 68 74 20 3d 20 70 4c 49 74 65 6d  *pRight = pLItem
1cc7c 2d 3e 70 45 78 70 72 3b 0a 0a 20 20 20 20 20 20  ->pExpr;..      
1cc7d 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61  codeCompareOpera
1cc7e 6e 64 73 28 70 50 61 72 73 65 2c 20 70 4c 65 66  nds(pParse, pLef
1cc7f 74 2c 20 26 72 31 2c 20 26 72 65 67 46 72 65 65  t, &r1, &regFree
1cc80 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1,.             
1cc81 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cc82 20 20 20 20 20 70 52 69 67 68 74 2c 20 26 72 32       pRight, &r2
1cc83 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20  , &regFree2);.  
1cc84 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65      testcase( re
1cc85 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20  gFree1==0 );.   
1cc86 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
1cc87 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20  Free2==0 );.    
1cc88 20 20 72 33 20 3d 20 73 71 6c 69 74 65 33 47 65    r3 = sqlite3Ge
1cc89 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
1cc8a 3b 0a 20 20 20 20 20 20 72 34 20 3d 20 73 71 6c  ;.      r4 = sql
1cc8b 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
1cc8c 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 63 6f  Parse);.      co
1cc8d 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65  deCompare(pParse
1cc8e 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c  , pLeft, pRight,
1cc8f 20 4f 50 5f 47 65 2c 0a 20 20 20 20 20 20 20 20   OP_Ge,.        
1cc90 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 32            r1, r2
1cc91 2c 20 72 33 2c 20 53 51 4c 49 54 45 5f 53 54 4f  , r3, SQLITE_STO
1cc92 52 45 50 32 29 3b 0a 20 20 20 20 20 20 70 4c 49  REP2);.      pLI
1cc93 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 70 52 69  tem++;.      pRi
1cc94 67 68 74 20 3d 20 70 4c 49 74 65 6d 2d 3e 70 45  ght = pLItem->pE
1cc95 78 70 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  xpr;.      sqlit
1cc96 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
1cc97 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65  (pParse, regFree
1cc98 32 29 3b 0a 20 20 20 20 20 20 72 32 20 3d 20 73  2);.      r2 = s
1cc99 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65  qlite3ExprCodeTe
1cc9a 6d 70 28 70 50 61 72 73 65 2c 20 70 52 69 67 68  mp(pParse, pRigh
1cc9b 74 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20  t, &regFree2);. 
1cc9c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
1cc9d 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20  egFree2==0 );.  
1cc9e 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28      codeCompare(
1cc9f 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70  pParse, pLeft, p
1cca0 52 69 67 68 74 2c 20 4f 50 5f 4c 65 2c 20 72 31  Right, OP_Le, r1
1cca1 2c 20 72 32 2c 20 72 34 2c 20 53 51 4c 49 54 45  , r2, r4, SQLITE
1cca2 5f 53 54 4f 52 45 50 32 29 3b 0a 20 20 20 20 20  _STOREP2);.     
1cca3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1cca4 70 33 28 76 2c 20 4f 50 5f 41 6e 64 2c 20 72 33  p3(v, OP_And, r3
1cca5 2c 20 72 34 2c 20 74 61 72 67 65 74 29 3b 0a 20  , r4, target);. 
1cca6 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
1cca7 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
1cca8 65 2c 20 72 33 29 3b 0a 20 20 20 20 20 20 73 71  e, r3);.      sq
1cca9 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
1ccaa 52 65 67 28 70 50 61 72 73 65 2c 20 72 34 29 3b  Reg(pParse, r4);
1ccab 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1ccac 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
1ccad 55 50 4c 55 53 3a 20 7b 0a 20 20 20 20 20 20 69  UPLUS: {.      i
1ccae 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78  nReg = sqlite3Ex
1ccaf 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61  prCodeTarget(pPa
1ccb0 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
1ccb1 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  t, target);.    
1ccb2 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a    break;.    }..
1ccb3 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 46 6f      /*.    ** Fo
1ccb4 72 6d 20 41 3a 0a 20 20 20 20 2a 2a 20 20 20 43  rm A:.    **   C
1ccb5 41 53 45 20 78 20 57 48 45 4e 20 65 31 20 54 48  ASE x WHEN e1 TH
1ccb6 45 4e 20 72 31 20 57 48 45 4e 20 65 32 20 54 48  EN r1 WHEN e2 TH
1ccb7 45 4e 20 72 32 20 2e 2e 2e 20 57 48 45 4e 20 65  EN r2 ... WHEN e
1ccb8 4e 20 54 48 45 4e 20 72 4e 20 45 4c 53 45 20 79  N THEN rN ELSE y
1ccb9 20 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20   END.    **.    
1ccba 2a 2a 20 46 6f 72 6d 20 42 3a 0a 20 20 20 20 2a  ** Form B:.    *
1ccbb 2a 20 20 20 43 41 53 45 20 57 48 45 4e 20 65 31  *   CASE WHEN e1
1ccbc 20 54 48 45 4e 20 72 31 20 57 48 45 4e 20 65 32   THEN r1 WHEN e2
1ccbd 20 54 48 45 4e 20 72 32 20 2e 2e 2e 20 57 48 45   THEN r2 ... WHE
1ccbe 4e 20 65 4e 20 54 48 45 4e 20 72 4e 20 45 4c 53  N eN THEN rN ELS
1ccbf 45 20 79 20 45 4e 44 0a 20 20 20 20 2a 2a 0a 20  E y END.    **. 
1ccc0 20 20 20 2a 2a 20 46 6f 72 6d 20 41 20 69 73 20     ** Form A is 
1ccc1 63 61 6e 20 62 65 20 74 72 61 6e 73 66 6f 72 6d  can be transform
1ccc2 65 64 20 69 6e 74 6f 20 74 68 65 20 65 71 75 69  ed into the equi
1ccc3 76 61 6c 65 6e 74 20 66 6f 72 6d 20 42 20 61 73  valent form B as
1ccc4 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a   follows:.    **
1ccc5 20 20 20 43 41 53 45 20 57 48 45 4e 20 78 3d 65     CASE WHEN x=e
1ccc6 31 20 54 48 45 4e 20 72 31 20 57 48 45 4e 20 78  1 THEN r1 WHEN x
1ccc7 3d 65 32 20 54 48 45 4e 20 72 32 20 2e 2e 2e 0a  =e2 THEN r2 ....
1ccc8 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 57 48      **        WH
1ccc9 45 4e 20 78 3d 65 4e 20 54 48 45 4e 20 72 4e 20  EN x=eN THEN rN 
1ccca 45 4c 53 45 20 79 20 45 4e 44 0a 20 20 20 20 2a  ELSE y END.    *
1cccb 2a 0a 20 20 20 20 2a 2a 20 58 20 28 69 66 20 69  *.    ** X (if i
1cccc 74 20 65 78 69 73 74 73 29 20 69 73 20 69 6e 20  t exists) is in 
1cccd 70 45 78 70 72 2d 3e 70 4c 65 66 74 2e 0a 20 20  pExpr->pLeft..  
1ccce 20 20 2a 2a 20 59 20 69 73 20 69 6e 20 70 45 78    ** Y is in pEx
1cccf 70 72 2d 3e 70 52 69 67 68 74 2e 20 20 54 68 65  pr->pRight.  The
1ccd0 20 59 20 69 73 20 61 6c 73 6f 20 6f 70 74 69 6f   Y is also optio
1ccd1 6e 61 6c 2e 20 20 49 66 20 74 68 65 72 65 20 69  nal.  If there i
1ccd2 73 20 6e 6f 0a 20 20 20 20 2a 2a 20 45 4c 53 45  s no.    ** ELSE
1ccd3 20 63 6c 61 75 73 65 20 61 6e 64 20 6e 6f 20 6f   clause and no o
1ccd4 74 68 65 72 20 74 65 72 6d 20 6d 61 74 63 68 65  ther term matche
1ccd5 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  s, then the resu
1ccd6 6c 74 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a  lt of the.    **
1ccd7 20 65 78 70 72 73 73 69 6f 6e 20 69 73 20 4e 55   exprssion is NU
1ccd8 4c 4c 2e 0a 20 20 20 20 2a 2a 20 45 69 20 69 73  LL..    ** Ei is
1ccd9 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73 74   in pExpr->pList
1ccda 2d 3e 61 5b 69 2a 32 5d 20 61 6e 64 20 52 69 20  ->a[i*2] and Ri 
1ccdb 69 73 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d  is pExpr->pList-
1ccdc 3e 61 5b 69 2a 32 2b 31 5d 2e 0a 20 20 20 20 2a  >a[i*2+1]..    *
1ccdd 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 65 73  *.    ** The res
1ccde 75 6c 74 20 6f 66 20 74 68 65 20 65 78 70 72 65  ult of the expre
1ccdf 73 73 69 6f 6e 20 69 73 20 74 68 65 20 52 69 20  ssion is the Ri 
1cce0 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 6d 61  for the first ma
1cce1 74 63 68 69 6e 67 20 45 69 2c 0a 20 20 20 20 2a  tching Ei,.    *
1cce2 2a 20 6f 72 20 69 66 20 74 68 65 72 65 20 69 73  * or if there is
1cce3 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20 45 69 2c   no matching Ei,
1cce4 20 74 68 65 20 45 4c 53 45 20 74 65 72 6d 20 59   the ELSE term Y
1cce5 2c 20 6f 72 20 69 66 20 74 68 65 72 65 20 69 73  , or if there is
1cce6 0a 20 20 20 20 2a 2a 20 6e 6f 20 45 4c 53 45 20  .    ** no ELSE 
1cce7 74 65 72 6d 2c 20 4e 55 4c 4c 2e 0a 20 20 20 20  term, NULL..    
1cce8 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43  */.    case TK_C
1cce9 41 53 45 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  ASE: {.      int
1ccea 20 65 6e 64 4c 61 62 65 6c 3b 20 20 20 20 20 20   endLabel;      
1cceb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1ccec 2a 20 47 4f 54 4f 20 6c 61 62 65 6c 20 66 6f 72  * GOTO label for
1cced 20 65 6e 64 20 6f 66 20 43 41 53 45 20 73 74 6d   end of CASE stm
1ccee 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e  t */.      int n
1ccef 65 78 74 43 61 73 65 3b 20 20 20 20 20 20 20 20  extCase;        
1ccf0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1ccf1 47 4f 54 4f 20 6c 61 62 65 6c 20 66 6f 72 20 6e  GOTO label for n
1ccf2 65 78 74 20 57 48 45 4e 20 63 6c 61 75 73 65 20  ext WHEN clause 
1ccf3 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 45 78  */.      int nEx
1ccf4 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  pr;             
1ccf5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 78             /* 2x
1ccf6 20 6e 75 6d 62 65 72 20 6f 66 20 57 48 45 4e 20   number of WHEN 
1ccf7 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20 20 20 69  terms */.      i
1ccf8 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
1ccf9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ccfa 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
1ccfb 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 4c 69   */.      ExprLi
1ccfc 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20  st *pEList;     
1ccfd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1ccfe 69 73 74 20 6f 66 20 57 48 45 4e 20 74 65 72 6d  ist of WHEN term
1ccff 73 20 2a 2f 0a 20 20 20 20 20 20 73 74 72 75 63  s */.      struc
1cd00 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
1cd01 2a 61 4c 69 73 74 65 6c 65 6d 3b 20 20 2f 2a 20  *aListelem;  /* 
1cd02 41 72 72 61 79 20 6f 66 20 57 48 45 4e 20 74 65  Array of WHEN te
1cd03 72 6d 73 20 2a 2f 0a 20 20 20 20 20 20 45 78 70  rms */.      Exp
1cd04 72 20 6f 70 43 6f 6d 70 61 72 65 3b 20 20 20 20  r opCompare;    
1cd05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1cd06 2a 20 54 68 65 20 58 3d 3d 45 69 20 65 78 70 72  * The X==Ei expr
1cd07 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  ession */.      
1cd08 45 78 70 72 20 63 61 63 68 65 58 3b 20 20 20 20  Expr cacheX;    
1cd09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cd0a 20 20 2f 2a 20 43 61 63 68 65 64 20 65 78 70 72    /* Cached expr
1cd0b 65 73 73 69 6f 6e 20 58 20 2a 2f 0a 20 20 20 20  ession X */.    
1cd0c 20 20 45 78 70 72 20 2a 70 58 3b 20 20 20 20 20    Expr *pX;     
1cd0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cd0e 20 20 20 20 2f 2a 20 54 68 65 20 58 20 65 78 70      /* The X exp
1cd0f 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20  ression */.     
1cd10 20 45 78 70 72 20 2a 70 54 65 73 74 3b 20 20 20   Expr *pTest;   
1cd11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cd12 20 20 20 2f 2a 20 58 3d 3d 45 69 20 28 66 6f 72     /* X==Ei (for
1cd13 6d 20 41 29 20 6f 72 20 6a 75 73 74 20 45 69 20  m A) or just Ei 
1cd14 28 66 6f 72 6d 20 42 29 20 2a 2f 0a 0a 20 20 20  (form B) */..   
1cd15 20 20 20 61 73 73 65 72 74 28 70 45 78 70 72 2d     assert(pExpr-
1cd16 3e 70 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 61  >pList);.      a
1cd17 73 73 65 72 74 28 28 70 45 78 70 72 2d 3e 70 4c  ssert((pExpr->pL
1cd18 69 73 74 2d 3e 6e 45 78 70 72 20 25 20 32 29 20  ist->nExpr % 2) 
1cd19 3d 3d 20 30 29 3b 0a 20 20 20 20 20 20 61 73 73  == 0);.      ass
1cd1a 65 72 74 28 70 45 78 70 72 2d 3e 70 4c 69 73 74  ert(pExpr->pList
1cd1b 2d 3e 6e 45 78 70 72 20 3e 20 30 29 3b 0a 20 20  ->nExpr > 0);.  
1cd1c 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70 45 78      pEList = pEx
1cd1d 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 20 20 20  pr->pList;.     
1cd1e 20 61 4c 69 73 74 65 6c 65 6d 20 3d 20 70 45 4c   aListelem = pEL
1cd1f 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20 6e 45  ist->a;.      nE
1cd20 78 70 72 20 3d 20 70 45 4c 69 73 74 2d 3e 6e 45  xpr = pEList->nE
1cd21 78 70 72 3b 0a 20 20 20 20 20 20 65 6e 64 4c 61  xpr;.      endLa
1cd22 62 65 6c 20 3d 20 73 71 6c 69 74 65 33 56 64 62  bel = sqlite3Vdb
1cd23 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
1cd24 20 20 20 20 20 69 66 28 20 28 70 58 20 3d 20 70       if( (pX = p
1cd25 45 78 70 72 2d 3e 70 4c 65 66 74 29 21 3d 30 20  Expr->pLeft)!=0 
1cd26 29 7b 0a 20 20 20 20 20 20 20 20 63 61 63 68 65  ){.        cache
1cd27 58 20 3d 20 2a 70 58 3b 0a 20 20 20 20 20 20 20  X = *pX;.       
1cd28 20 74 65 73 74 63 61 73 65 28 20 70 58 2d 3e 6f   testcase( pX->o
1cd29 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20  p==TK_COLUMN || 
1cd2a 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53  pX->op==TK_REGIS
1cd2b 54 45 52 20 29 3b 0a 20 20 20 20 20 20 20 20 63  TER );.        c
1cd2c 61 63 68 65 58 2e 69 54 61 62 6c 65 20 3d 20 73  acheX.iTable = s
1cd2d 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65  qlite3ExprCodeTe
1cd2e 6d 70 28 70 50 61 72 73 65 2c 20 70 58 2c 20 26  mp(pParse, pX, &
1cd2f 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20  regFree1);.     
1cd30 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
1cd31 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20  Free1==0 );.    
1cd32 20 20 20 20 63 61 63 68 65 58 2e 6f 70 20 3d 20      cacheX.op = 
1cd33 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20  TK_REGISTER;.   
1cd34 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 6f       opCompare.o
1cd35 70 20 3d 20 54 4b 5f 45 51 3b 0a 20 20 20 20 20  p = TK_EQ;.     
1cd36 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 70 4c 65     opCompare.pLe
1cd37 66 74 20 3d 20 26 63 61 63 68 65 58 3b 0a 20 20  ft = &cacheX;.  
1cd38 20 20 20 20 20 20 70 54 65 73 74 20 3d 20 26 6f        pTest = &o
1cd39 70 43 6f 6d 70 61 72 65 3b 0a 20 20 20 20 20 20  pCompare;.      
1cd3a 7d 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  }.      pParse->
1cd3b 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 2b  disableColCache+
1cd3c 2b 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  +;.      for(i=0
1cd3d 3b 20 69 3c 6e 45 78 70 72 3b 20 69 3d 69 2b 32  ; i<nExpr; i=i+2
1cd3e 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ){.        if( p
1cd3f 58 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6f  X ){.          o
1cd40 70 43 6f 6d 70 61 72 65 2e 70 52 69 67 68 74 20  pCompare.pRight 
1cd41 3d 20 61 4c 69 73 74 65 6c 65 6d 5b 69 5d 2e 70  = aListelem[i].p
1cd42 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 7d 65  Expr;.        }e
1cd43 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70  lse{.          p
1cd44 54 65 73 74 20 3d 20 61 4c 69 73 74 65 6c 65 6d  Test = aListelem
1cd45 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  [i].pExpr;.     
1cd46 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 65 78     }.        nex
1cd47 74 43 61 73 65 20 3d 20 73 71 6c 69 74 65 33 56  tCase = sqlite3V
1cd48 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
1cd49 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
1cd4a 65 28 20 70 54 65 73 74 2d 3e 6f 70 3d 3d 54 4b  e( pTest->op==TK
1cd4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 54 65 73 74  _COLUMN || pTest
1cd4c 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45  ->op==TK_REGISTE
1cd4d 52 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  R );.        sql
1cd4e 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
1cd4f 70 50 61 72 73 65 2c 20 70 54 65 73 74 2c 20 6e  pParse, pTest, n
1cd50 65 78 74 43 61 73 65 2c 20 53 51 4c 49 54 45 5f  extCase, SQLITE_
1cd51 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20  JUMPIFNULL);.   
1cd52 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 61       testcase( a
1cd53 4c 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45  Listelem[i+1].pE
1cd54 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55  xpr->op==TK_COLU
1cd55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 20 20 74 65  MN );.        te
1cd56 73 74 63 61 73 65 28 20 61 4c 69 73 74 65 6c 65  stcase( aListele
1cd57 6d 5b 69 2b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70  m[i+1].pExpr->op
1cd58 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 29 3b  ==TK_REGISTER );
1cd59 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1cd5a 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
1cd5b 20 61 4c 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e   aListelem[i+1].
1cd5c 70 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a  pExpr, target);.
1cd5d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1cd5e 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1cd5f 47 6f 74 6f 2c 20 30 2c 20 65 6e 64 4c 61 62 65  Goto, 0, endLabe
1cd60 6c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  l);.        sqli
1cd61 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
1cd62 62 65 6c 28 76 2c 20 6e 65 78 74 43 61 73 65 29  bel(v, nextCase)
1cd63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1cd64 69 66 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68  if( pExpr->pRigh
1cd65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  t ){.        sql
1cd66 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
1cd67 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67  rse, pExpr->pRig
1cd68 68 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20  ht, target);.   
1cd69 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1cd6a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1cd6b 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
1cd6c 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  0, target);.    
1cd6d 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
1cd6e 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
1cd6f 6c 28 76 2c 20 65 6e 64 4c 61 62 65 6c 29 3b 0a  l(v, endLabel);.
1cd70 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
1cd71 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c  arse->disableCol
1cd72 43 61 63 68 65 3e 30 20 29 3b 0a 20 20 20 20 20  Cache>0 );.     
1cd73 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65   pParse->disable
1cd74 43 6f 6c 43 61 63 68 65 2d 2d 3b 0a 20 20 20 20  ColCache--;.    
1cd75 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
1cd76 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1cd77 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 63  IT_TRIGGER.    c
1cd78 61 73 65 20 54 4b 5f 52 41 49 53 45 3a 20 7b 0a  ase TK_RAISE: {.
1cd79 20 20 20 20 20 20 69 66 28 20 21 70 50 61 72 73        if( !pPars
1cd7a 65 2d 3e 74 72 69 67 53 74 61 63 6b 20 29 7b 0a  e->trigStack ){.
1cd7b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
1cd7c 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 0a  rrorMsg(pParse,.
1cd7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cd7e 20 20 20 20 20 20 20 22 52 41 49 53 45 28 29 20         "RAISE() 
1cd7f 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64  may only be used
1cd80 20 77 69 74 68 69 6e 20 61 20 74 72 69 67 67 65   within a trigge
1cd81 72 2d 70 72 6f 67 72 61 6d 22 29 3b 0a 20 20 20  r-program");.   
1cd82 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
1cd83 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
1cd84 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 21   pExpr->iColumn!
1cd85 3d 4f 45 5f 49 67 6e 6f 72 65 20 29 7b 0a 20 20  =OE_Ignore ){.  
1cd86 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1cd87 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 4f  Expr->iColumn==O
1cd88 45 5f 52 6f 6c 6c 62 61 63 6b 20 7c 7c 0a 20 20  E_Rollback ||.  
1cd89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
1cd8a 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 3d  Expr->iColumn ==
1cd8b 20 4f 45 5f 41 62 6f 72 74 20 7c 7c 0a 20 20 20   OE_Abort ||.   
1cd8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
1cd8d 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 3d 20  xpr->iColumn == 
1cd8e 4f 45 5f 46 61 69 6c 20 29 3b 0a 20 20 20 20 20  OE_Fail );.     
1cd8f 20 20 20 20 73 71 6c 69 74 65 33 44 65 71 75 6f      sqlite3Dequo
1cd90 74 65 45 78 70 72 28 64 62 2c 20 70 45 78 70 72  teExpr(db, pExpr
1cd91 29 3b 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69  );.         sqli
1cd92 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
1cd93 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45   OP_Halt, SQLITE
1cd94 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 70 45 78  _CONSTRAINT, pEx
1cd95 70 72 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 30 2c 0a  pr->iColumn, 0,.
1cd96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cd97 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70          (char*)p
1cd98 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70  Expr->token.z, p
1cd99 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b 0a  Expr->token.n);.
1cd9a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20        } else {. 
1cd9b 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1cd9c 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d  pExpr->iColumn =
1cd9d 3d 20 4f 45 5f 49 67 6e 6f 72 65 20 29 3b 0a 20  = OE_Ignore );. 
1cd9e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1cd9f 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1cda0 43 6f 6e 74 65 78 74 50 6f 70 2c 20 30 2c 20 30  ContextPop, 0, 0
1cda1 29 3b 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69  );.         sqli
1cda2 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1cda3 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 50 61   OP_Goto, 0, pPa
1cda4 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 2d 3e  rse->trigStack->
1cda5 69 67 6e 6f 72 65 4a 75 6d 70 29 3b 0a 20 20 20  ignoreJump);.   
1cda6 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
1cda7 74 28 28 76 2c 20 22 72 61 69 73 65 28 49 47 4e  t((v, "raise(IGN
1cda8 4f 52 45 29 22 29 29 3b 0a 20 20 20 20 20 20 7d  ORE)"));.      }
1cda9 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1cdaa 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20    }.#endif.  }. 
1cdab 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
1cdac 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
1cdad 65 67 46 72 65 65 31 29 3b 0a 20 20 73 71 6c 69  egFree1);.  sqli
1cdae 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
1cdaf 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 65  g(pParse, regFre
1cdb0 65 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 6e  e2);.  return in
1cdb1 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  Reg;.}../*.** Ge
1cdb2 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 65  nerate code to e
1cdb3 76 61 6c 75 61 74 65 20 61 6e 20 65 78 70 72 65  valuate an expre
1cdb4 73 73 69 6f 6e 20 61 6e 64 20 73 74 6f 72 65 20  ssion and store 
1cdb5 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 69  the results.** i
1cdb6 6e 74 6f 20 61 20 72 65 67 69 73 74 65 72 2e 20  nto a register. 
1cdb7 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 67 69   Return the regi
1cdb8 73 74 65 72 20 6e 75 6d 62 65 72 20 77 68 65 72  ster number wher
1cdb9 65 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a  e the results.**
1cdba 20 61 72 65 20 73 74 6f 72 65 64 2e 0a 2a 2a 0a   are stored..**.
1cdbb 2a 2a 20 49 66 20 74 68 65 20 72 65 67 69 73 74  ** If the regist
1cdbc 65 72 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72  er is a temporar
1cdbd 79 20 72 65 67 69 73 74 65 72 20 74 68 61 74 20  y register that 
1cdbe 63 61 6e 20 62 65 20 64 65 61 6c 6c 6f 63 61 74  can be deallocat
1cdbf 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 77 72 69 74  ed,.** then writ
1cdc0 65 20 69 74 73 20 6e 75 6d 62 65 72 20 69 6e 74  e its number int
1cdc1 6f 20 2a 70 52 65 67 2e 20 20 49 66 20 74 68 65  o *pReg.  If the
1cdc2 20 72 65 73 75 6c 74 20 72 65 67 69 73 74 65 72   result register
1cdc3 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 74 65 6d   is not.** a tem
1cdc4 70 6f 72 61 72 79 2c 20 74 68 65 6e 20 73 65 74  porary, then set
1cdc5 20 2a 70 52 65 67 20 74 6f 20 7a 65 72 6f 2e 0a   *pReg to zero..
1cdc6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1cdc7 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
1cdc8 72 43 6f 64 65 54 65 6d 70 28 50 61 72 73 65 20  rCodeTemp(Parse 
1cdc9 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
1cdca 45 78 70 72 2c 20 69 6e 74 20 2a 70 52 65 67 29  Expr, int *pReg)
1cdcb 7b 0a 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c  {.  int r1 = sql
1cdcc 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
1cdcd 50 61 72 73 65 29 3b 0a 20 20 69 6e 74 20 72 32  Parse);.  int r2
1cdce 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
1cdcf 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c  deTarget(pParse,
1cdd0 20 70 45 78 70 72 2c 20 72 31 29 3b 0a 20 20 69   pExpr, r1);.  i
1cdd1 66 28 20 72 32 3d 3d 72 31 20 29 7b 0a 20 20 20  f( r2==r1 ){.   
1cdd2 20 2a 70 52 65 67 20 3d 20 72 31 3b 0a 20 20 7d   *pReg = r1;.  }
1cdd3 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
1cdd4 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
1cdd5 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20  pParse, r1);.   
1cdd6 20 2a 70 52 65 67 20 3d 20 30 3b 0a 20 20 7d 0a   *pReg = 0;.  }.
1cdd7 20 20 72 65 74 75 72 6e 20 72 32 3b 0a 7d 0a 0a    return r2;.}..
1cdd8 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
1cdd9 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65 76  ode that will ev
1cdda 61 6c 75 61 74 65 20 65 78 70 72 65 73 73 69 6f  aluate expressio
1cddb 6e 20 70 45 78 70 72 20 61 6e 64 20 73 74 6f 72  n pExpr and stor
1cddc 65 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 73  e the.** results
1cddd 20 69 6e 20 72 65 67 69 73 74 65 72 20 74 61 72   in register tar
1cdde 67 65 74 2e 20 20 54 68 65 20 72 65 73 75 6c 74  get.  The result
1cddf 73 20 61 72 65 20 67 75 61 72 61 6e 74 65 65 64  s are guaranteed
1cde0 20 74 6f 20 61 70 70 65 61 72 0a 2a 2a 20 69 6e   to appear.** in
1cde1 20 72 65 67 69 73 74 65 72 20 74 61 72 67 65 74   register target
1cde2 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1cde3 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
1cde4 78 70 72 43 6f 64 65 28 50 61 72 73 65 20 2a 70  xprCode(Parse *p
1cde5 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78  Parse, Expr *pEx
1cde6 70 72 2c 20 69 6e 74 20 74 61 72 67 65 74 29 7b  pr, int target){
1cde7 0a 20 20 69 6e 74 20 69 6e 52 65 67 3b 0a 0a 20  .  int inReg;.. 
1cde8 20 61 73 73 65 72 74 28 20 74 61 72 67 65 74 3e   assert( target>
1cde9 30 20 26 26 20 74 61 72 67 65 74 3c 3d 70 50 61  0 && target<=pPa
1cdea 72 73 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 69  rse->nMem );.  i
1cdeb 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78  nReg = sqlite3Ex
1cdec 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61  prCodeTarget(pPa
1cded 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61 72 67  rse, pExpr, targ
1cdee 65 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  et);.  assert( p
1cdef 50 61 72 73 65 2d 3e 70 56 64 62 65 20 7c 7c 20  Parse->pVdbe || 
1cdf0 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c  pParse->db->mall
1cdf1 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66  ocFailed );.  if
1cdf2 28 20 69 6e 52 65 67 21 3d 74 61 72 67 65 74 20  ( inReg!=target 
1cdf3 26 26 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65  && pParse->pVdbe
1cdf4 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1cdf5 64 62 65 41 64 64 4f 70 32 28 70 50 61 72 73 65  dbeAddOp2(pParse
1cdf6 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 53 43 6f 70  ->pVdbe, OP_SCop
1cdf7 79 2c 20 69 6e 52 65 67 2c 20 74 61 72 67 65 74  y, inReg, target
1cdf8 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
1cdf9 74 61 72 67 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  target;.}../*.**
1cdfa 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
1cdfb 68 61 74 20 65 76 61 6c 75 74 65 73 20 74 68 65  hat evalutes the
1cdfc 20 67 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f   given expressio
1cdfd 6e 20 61 6e 64 20 70 75 74 73 20 74 68 65 20 72  n and puts the r
1cdfe 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 72 65 67 69  esult.** in regi
1cdff 73 74 65 72 20 74 61 72 67 65 74 2e 0a 2a 2a 0a  ster target..**.
1ce00 2a 2a 20 41 6c 73 6f 20 6d 61 6b 65 20 61 20 63  ** Also make a c
1ce01 6f 70 79 20 6f 66 20 74 68 65 20 65 78 70 72 65  opy of the expre
1ce02 73 73 69 6f 6e 20 72 65 73 75 6c 74 73 20 69 6e  ssion results in
1ce03 74 6f 20 61 6e 6f 74 68 65 72 20 22 63 61 63 68  to another "cach
1ce04 65 22 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 61  e" register.** a
1ce05 6e 64 20 6d 6f 64 69 66 79 20 74 68 65 20 65 78  nd modify the ex
1ce06 70 72 65 73 73 69 6f 6e 20 73 6f 20 74 68 61 74  pression so that
1ce07 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 20 69   the next time i
1ce08 74 20 69 73 20 65 76 61 6c 75 61 74 65 64 2c 0a  t is evaluated,.
1ce09 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  ** the result is
1ce0a 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63   a copy of the c
1ce0b 61 63 68 65 20 72 65 67 69 73 74 65 72 2e 0a 2a  ache register..*
1ce0c 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1ce0d 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 65 78  e is used for ex
1ce0e 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74 20 61  pressions that a
1ce0f 72 65 20 75 73 65 64 20 6d 75 6c 74 69 70 6c 65  re used multiple
1ce10 20 0a 2a 2a 20 74 69 6d 65 73 2e 20 20 54 68 65   .** times.  The
1ce11 79 20 61 72 65 20 65 76 61 6c 75 61 74 65 64 20  y are evaluated 
1ce12 6f 6e 63 65 20 61 6e 64 20 74 68 65 20 72 65 73  once and the res
1ce13 75 6c 74 73 20 6f 66 20 74 68 65 20 65 78 70 72  ults of the expr
1ce14 65 73 73 69 6f 6e 0a 2a 2a 20 61 72 65 20 72 65  ession.** are re
1ce15 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  used..*/.SQLITE_
1ce16 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1ce17 74 65 33 45 78 70 72 43 6f 64 65 41 6e 64 43 61  te3ExprCodeAndCa
1ce18 63 68 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  che(Parse *pPars
1ce19 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20  e, Expr *pExpr, 
1ce1a 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 20 20 56  int target){.  V
1ce1b 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
1ce1c 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 6e  >pVdbe;.  int in
1ce1d 52 65 67 3b 0a 20 20 69 6e 52 65 67 20 3d 20 73  Reg;.  inReg = s
1ce1e 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
1ce1f 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61  Parse, pExpr, ta
1ce20 72 67 65 74 29 3b 0a 20 20 61 73 73 65 72 74 28  rget);.  assert(
1ce21 20 74 61 72 67 65 74 3e 30 20 29 3b 0a 20 20 69   target>0 );.  i
1ce22 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b  f( pExpr->op!=TK
1ce23 5f 52 45 47 49 53 54 45 52 20 29 7b 20 20 0a 20  _REGISTER ){  . 
1ce24 20 20 20 69 6e 74 20 69 4d 65 6d 3b 0a 20 20 20     int iMem;.   
1ce25 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65   iMem = ++pParse
1ce26 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69  ->nMem;.    sqli
1ce27 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1ce28 20 4f 50 5f 43 6f 70 79 2c 20 69 6e 52 65 67 2c   OP_Copy, inReg,
1ce29 20 69 4d 65 6d 29 3b 0a 20 20 20 20 70 45 78 70   iMem);.    pExp
1ce2a 72 2d 3e 69 54 61 62 6c 65 20 3d 20 69 4d 65 6d  r->iTable = iMem
1ce2b 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20  ;.    pExpr->op 
1ce2c 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20  = TK_REGISTER;. 
1ce2d 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 6e 52 65   }.  return inRe
1ce2e 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  g;.}../*.** Retu
1ce2f 72 6e 20 54 52 55 45 20 69 66 20 70 45 78 70 72  rn TRUE if pExpr
1ce30 20 69 73 20 61 6e 20 63 6f 6e 73 74 61 6e 74 20   is an constant 
1ce31 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20  expression that 
1ce32 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a  is appropriate.*
1ce33 2a 20 66 6f 72 20 66 61 63 74 6f 72 69 6e 67 20  * for factoring 
1ce34 6f 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2e 20 20  out of a loop.  
1ce35 41 70 70 72 6f 70 72 69 61 74 65 20 65 78 70 72  Appropriate expr
1ce36 65 73 73 69 6f 6e 73 20 61 72 65 3a 0a 2a 2a 0a  essions are:.**.
1ce37 2a 2a 20 20 20 20 2a 20 20 41 6e 79 20 65 78 70  **    *  Any exp
1ce38 72 65 73 73 69 6f 6e 20 74 68 61 74 20 65 76 61  ression that eva
1ce39 6c 75 61 74 65 73 20 74 6f 20 74 77 6f 20 6f 72  luates to two or
1ce3a 20 6d 6f 72 65 20 6f 70 63 6f 64 65 73 2e 0a 2a   more opcodes..*
1ce3b 2a 0a 2a 2a 20 20 20 20 2a 20 20 41 6e 79 20 4f  *.**    *  Any O
1ce3c 50 5f 49 6e 74 65 67 65 72 2c 20 4f 50 5f 52 65  P_Integer, OP_Re
1ce3d 61 6c 2c 20 4f 50 5f 53 74 72 69 6e 67 2c 20 4f  al, OP_String, O
1ce3e 50 5f 42 6c 6f 62 2c 20 4f 50 5f 4e 75 6c 6c 2c  P_Blob, OP_Null,
1ce3f 20 0a 2a 2a 20 20 20 20 20 20 20 6f 72 20 4f 50   .**       or OP
1ce40 5f 56 61 72 69 61 62 6c 65 20 74 68 61 74 20 64  _Variable that d
1ce41 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20  oes not need to 
1ce42 62 65 20 70 6c 61 63 65 64 20 69 6e 20 61 20 0a  be placed in a .
1ce43 2a 2a 20 20 20 20 20 20 20 73 70 65 63 69 66 69  **       specifi
1ce44 63 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a 0a 2a  c register..**.*
1ce45 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 70 6f  * There is no po
1ce46 69 6e 74 20 69 6e 20 66 61 63 74 6f 72 69 6e 67  int in factoring
1ce47 20 6f 75 74 20 73 69 6e 67 6c 65 2d 69 6e 73 74   out single-inst
1ce48 72 75 63 74 69 6f 6e 20 63 6f 6e 73 74 61 6e 74  ruction constant
1ce49 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20  .** expressions 
1ce4a 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20  that need to be 
1ce4b 70 6c 61 63 65 64 20 69 6e 20 61 20 70 61 72 74  placed in a part
1ce4c 69 63 75 6c 61 72 20 72 65 67 69 73 74 65 72 2e  icular register.
1ce4d 20 20 0a 2a 2a 20 57 65 20 63 6f 75 6c 64 20 66    .** We could f
1ce4e 61 63 74 6f 72 20 74 68 65 6d 20 6f 75 74 2c 20  actor them out, 
1ce4f 62 75 74 20 74 68 65 6e 20 77 65 20 77 6f 75 6c  but then we woul
1ce50 64 20 65 6e 64 20 75 70 20 61 64 64 69 6e 67 20  d end up adding 
1ce51 61 6e 0a 2a 2a 20 4f 50 5f 53 43 6f 70 79 20 69  an.** OP_SCopy i
1ce52 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 6d 6f  nstruction to mo
1ce53 76 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 74  ve the value int
1ce54 6f 20 74 68 65 20 63 6f 72 72 65 63 74 20 72 65  o the correct re
1ce55 67 69 73 74 65 72 0a 2a 2a 20 6c 61 74 65 72 2e  gister.** later.
1ce56 20 20 57 65 20 6d 69 67 68 74 20 61 73 20 77 65    We might as we
1ce57 6c 6c 20 6a 75 73 74 20 75 73 65 20 74 68 65 20  ll just use the 
1ce58 6f 72 69 67 69 6e 61 6c 20 69 6e 73 74 72 75 63  original instruc
1ce59 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 61 76 6f 69  tion and.** avoi
1ce5a 64 20 74 68 65 20 4f 50 5f 53 43 6f 70 79 2e 0a  d the OP_SCopy..
1ce5b 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73  */.static int is
1ce5c 41 70 70 72 6f 70 72 69 61 74 65 46 6f 72 46 61  AppropriateForFa
1ce5d 63 74 6f 72 69 6e 67 28 45 78 70 72 20 2a 70 29  ctoring(Expr *p)
1ce5e 7b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33  {.  if( !sqlite3
1ce5f 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f  ExprIsConstantNo
1ce60 74 4a 6f 69 6e 28 70 29 20 29 7b 0a 20 20 20 20  tJoin(p) ){.    
1ce61 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 4f 6e  return 0;  /* On
1ce62 6c 79 20 63 6f 6e 73 74 61 6e 74 20 65 78 70 72  ly constant expr
1ce63 65 73 73 69 6f 6e 73 20 61 72 65 20 61 70 70 72  essions are appr
1ce64 6f 70 72 69 61 74 65 20 66 6f 72 20 66 61 63 74  opriate for fact
1ce65 6f 72 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 69  oring */.  }.  i
1ce66 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 45  f( (p->flags & E
1ce67 50 5f 46 69 78 65 64 44 65 73 74 29 3d 3d 30 20  P_FixedDest)==0 
1ce68 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  ){.    return 1;
1ce69 20 20 2f 2a 20 41 6e 79 20 63 6f 6e 73 74 61 6e    /* Any constan
1ce6a 74 20 77 69 74 68 6f 75 74 20 61 20 66 69 78 65  t without a fixe
1ce6b 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73  d destination is
1ce6c 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a 2f 0a   appropriate */.
1ce6d 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e    }.  while( p->
1ce6e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20 70  op==TK_UPLUS ) p
1ce6f 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 73   = p->pLeft;.  s
1ce70 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0a  witch( p->op ){.
1ce71 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1ce72 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c  MIT_BLOB_LITERAL
1ce73 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 4c 4f  .    case TK_BLO
1ce74 42 3a 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61  B:.#endif.    ca
1ce75 73 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 0a  se TK_VARIABLE:.
1ce76 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 45      case TK_INTE
1ce77 47 45 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  GER:.    case TK
1ce78 5f 46 4c 4f 41 54 3a 0a 20 20 20 20 63 61 73 65  _FLOAT:.    case
1ce79 20 54 4b 5f 4e 55 4c 4c 3a 0a 20 20 20 20 63 61   TK_NULL:.    ca
1ce7a 73 65 20 54 4b 5f 53 54 52 49 4e 47 3a 20 7b 0a  se TK_STRING: {.
1ce7b 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1ce7c 70 2d 3e 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 20 29  p->op==TK_BLOB )
1ce7d 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1ce7e 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 56 41 52 49  ( p->op==TK_VARI
1ce7f 41 42 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65  ABLE );.      te
1ce80 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54  stcase( p->op==T
1ce81 4b 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20 20 20  K_INTEGER );.   
1ce82 20 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e     testcase( p->
1ce83 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 3b 0a  op==TK_FLOAT );.
1ce84 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1ce85 70 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c 20 29  p->op==TK_NULL )
1ce86 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1ce87 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 53 54 52 49  ( p->op==TK_STRI
1ce88 4e 47 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 53  NG );.      /* S
1ce89 69 6e 67 6c 65 2d 69 6e 73 74 72 75 63 74 69 6f  ingle-instructio
1ce8a 6e 20 63 6f 6e 73 74 61 6e 74 73 20 77 69 74 68  n constants with
1ce8b 20 61 20 66 69 78 65 64 20 64 65 73 74 69 6e 61   a fixed destina
1ce8c 74 69 6f 6e 20 61 72 65 0a 20 20 20 20 20 20 2a  tion are.      *
1ce8d 2a 20 62 65 74 74 65 72 20 64 6f 6e 65 20 69 6e  * better done in
1ce8e 2d 6c 69 6e 65 2e 20 20 49 66 20 77 65 20 66 61  -line.  If we fa
1ce8f 63 74 6f 72 20 74 68 65 6d 2c 20 74 68 65 79 20  ctor them, they 
1ce90 77 69 6c 6c 20 6a 75 73 74 20 65 6e 64 0a 20 20  will just end.  
1ce91 20 20 20 20 2a 2a 20 75 70 20 67 65 6e 65 72 61      ** up genera
1ce92 74 69 6e 67 20 61 6e 20 4f 50 5f 53 43 6f 70 79  ting an OP_SCopy
1ce93 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 76 61 6c   to move the val
1ce94 75 65 20 74 6f 20 74 68 65 20 64 65 73 74 69 6e  ue to the destin
1ce95 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 72  ation.      ** r
1ce96 65 67 69 73 74 65 72 2e 20 2a 2f 0a 20 20 20 20  egister. */.    
1ce97 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
1ce98 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 4d  }.    case TK_UM
1ce99 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 20 69  INUS: {.       i
1ce9a 66 28 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d  f( p->pLeft->op=
1ce9b 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 70 2d 3e  =TK_FLOAT || p->
1ce9c 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e  pLeft->op==TK_IN
1ce9d 54 45 47 45 52 20 29 7b 0a 20 20 20 20 20 20 20  TEGER ){.       
1ce9e 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
1ce9f 20 20 20 7d 0a 20 20 20 20 20 20 20 62 72 65 61     }.       brea
1cea0 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66  k;.    }.    def
1cea1 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 62 72  ault: {.      br
1cea2 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
1cea3 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a   return 1;.}../*
1cea4 0a 2a 2a 20 49 66 20 70 45 78 70 72 20 69 73 20  .** If pExpr is 
1cea5 61 20 63 6f 6e 73 74 61 6e 74 20 65 78 70 72 65  a constant expre
1cea6 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 61 70  ssion that is ap
1cea7 70 72 6f 70 72 69 61 74 65 20 66 6f 72 0a 2a 2a  propriate for.**
1cea8 20 66 61 63 74 6f 72 69 6e 67 20 6f 75 74 20 6f   factoring out o
1cea9 66 20 61 20 6c 6f 6f 70 2c 20 74 68 65 6e 20 65  f a loop, then e
1ceaa 76 61 6c 75 61 74 65 20 74 68 65 20 65 78 70 72  valuate the expr
1ceab 65 73 73 69 6f 6e 0a 2a 2a 20 69 6e 74 6f 20 61  ession.** into a
1ceac 20 72 65 67 69 73 74 65 72 20 61 6e 64 20 63 6f   register and co
1cead 6e 76 65 72 74 20 74 68 65 20 65 78 70 72 65 73  nvert the expres
1ceae 73 69 6f 6e 20 69 6e 74 6f 20 61 20 54 4b 5f 52  sion into a TK_R
1ceaf 45 47 49 53 54 45 52 0a 2a 2a 20 65 78 70 72 65  EGISTER.** expre
1ceb0 73 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ssion..*/.static
1ceb1 20 69 6e 74 20 65 76 61 6c 43 6f 6e 73 74 45 78   int evalConstEx
1ceb2 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b  pr(Walker *pWalk
1ceb3 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29  er, Expr *pExpr)
1ceb4 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  {.  Parse *pPars
1ceb5 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50 61  e = pWalker->pPa
1ceb6 72 73 65 3b 0a 20 20 73 77 69 74 63 68 28 20 70  rse;.  switch( p
1ceb7 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20  Expr->op ){.    
1ceb8 63 61 73 65 20 54 4b 5f 52 45 47 49 53 54 45 52  case TK_REGISTER
1ceb9 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  : {.      return
1ceba 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   1;.    }.    ca
1cebb 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 0a  se TK_FUNCTION:.
1cebc 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f      case TK_AGG_
1cebd 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 63 61  FUNCTION:.    ca
1cebe 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43  se TK_CONST_FUNC
1cebf 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65  : {.      /* The
1cec0 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 61 20   arguments to a 
1cec1 66 75 6e 63 74 69 6f 6e 20 68 61 76 65 20 61 20  function have a 
1cec2 66 69 78 65 64 20 64 65 73 74 69 6e 61 74 69 6f  fixed destinatio
1cec3 6e 2e 0a 20 20 20 20 20 20 2a 2a 20 4d 61 72 6b  n..      ** Mark
1cec4 20 74 68 65 6d 20 74 68 69 73 20 77 61 79 20 74   them this way t
1cec5 6f 20 61 76 6f 69 64 20 67 65 6e 65 72 61 74 65  o avoid generate
1cec6 64 20 75 6e 6e 65 65 64 65 64 20 4f 50 5f 53 43  d unneeded OP_SC
1cec7 6f 70 79 0a 20 20 20 20 20 20 2a 2a 20 69 6e 73  opy.      ** ins
1cec8 74 72 75 63 74 69 6f 6e 73 2e 20 0a 20 20 20 20  tructions. .    
1cec9 20 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 4c    */.      ExprL
1ceca 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78  ist *pList = pEx
1cecb 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 20 20 20  pr->pList;.     
1cecc 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20   if( pList ){.  
1cecd 20 20 20 20 20 20 69 6e 74 20 69 20 3d 20 70 4c        int i = pL
1cece 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20  ist->nExpr;.    
1cecf 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
1ced0 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20  ist_item *pItem 
1ced1 3d 20 70 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20  = pList->a;.    
1ced2 20 20 20 20 66 6f 72 28 3b 20 69 3e 30 3b 20 69      for(; i>0; i
1ced3 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  --, pItem++){.  
1ced4 20 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65          if( pIte
1ced5 6d 2d 3e 70 45 78 70 72 20 29 20 70 49 74 65 6d  m->pExpr ) pItem
1ced6 2d 3e 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c  ->pExpr->flags |
1ced7 3d 20 45 50 5f 46 69 78 65 64 44 65 73 74 3b 0a  = EP_FixedDest;.
1ced8 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1ced9 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
1ceda 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 69     }.  }.  if( i
1cedb 73 41 70 70 72 6f 70 72 69 61 74 65 46 6f 72 46  sAppropriateForF
1cedc 61 63 74 6f 72 69 6e 67 28 70 45 78 70 72 29 20  actoring(pExpr) 
1cedd 29 7b 0a 20 20 20 20 69 6e 74 20 72 31 20 3d 20  ){.    int r1 = 
1cede 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
1cedf 20 20 20 20 69 6e 74 20 72 32 3b 0a 20 20 20 20      int r2;.    
1cee0 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  r2 = sqlite3Expr
1cee1 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73  CodeTarget(pPars
1cee2 65 2c 20 70 45 78 70 72 2c 20 72 31 29 3b 0a 20  e, pExpr, r1);. 
1cee3 20 20 20 69 66 28 20 72 31 21 3d 72 32 20 29 20     if( r1!=r2 ) 
1cee4 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
1cee5 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31  mpReg(pParse, r1
1cee6 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70  );.    pExpr->op
1cee7 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a   = TK_REGISTER;.
1cee8 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c      pExpr->iTabl
1cee9 65 20 3d 20 72 32 3b 0a 20 20 20 20 72 65 74 75  e = r2;.    retu
1ceea 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20  rn WRC_Prune;.  
1ceeb 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43  }.  return WRC_C
1ceec 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  ontinue;.}../*.*
1ceed 2a 20 50 72 65 65 76 61 6c 75 61 74 65 20 63 6f  * Preevaluate co
1ceee 6e 73 74 61 6e 74 20 73 75 62 65 78 70 72 65 73  nstant subexpres
1ceef 73 69 6f 6e 73 20 77 69 74 68 69 6e 20 70 45 78  sions within pEx
1cef0 70 72 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65  pr and store the
1cef1 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e 20 72  .** results in r
1cef2 65 67 69 73 74 65 72 73 2e 20 20 4d 6f 64 69 66  egisters.  Modif
1cef3 79 20 70 45 78 70 72 20 73 6f 20 74 68 61 74 20  y pExpr so that 
1cef4 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 73 75 62  the constant sub
1cef5 65 78 70 72 65 73 69 6f 6e 73 0a 2a 2a 20 61 72  expresions.** ar
1cef6 65 20 54 4b 5f 52 45 47 49 53 54 45 52 20 6f 70  e TK_REGISTER op
1cef7 63 6f 64 65 73 20 74 68 61 74 20 72 65 66 65 72  codes that refer
1cef8 20 74 6f 20 74 68 65 20 70 72 65 63 6f 6d 70 75   to the precompu
1cef9 74 65 64 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 53  ted values..*/.S
1cefa 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1cefb 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  id sqlite3ExprCo
1cefc 64 65 43 6f 6e 73 74 61 6e 74 73 28 50 61 72 73  deConstants(Pars
1cefd 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
1cefe 2a 70 45 78 70 72 29 7b 0a 20 20 57 61 6c 6b 65  *pExpr){.  Walke
1ceff 72 20 77 3b 0a 20 20 77 2e 78 45 78 70 72 43 61  r w;.  w.xExprCa
1cf00 6c 6c 62 61 63 6b 20 3d 20 65 76 61 6c 43 6f 6e  llback = evalCon
1cf01 73 74 45 78 70 72 3b 0a 20 20 77 2e 78 53 65 6c  stExpr;.  w.xSel
1cf02 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 30 3b  ectCallback = 0;
1cf03 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 50  .  w.pParse = pP
1cf04 61 72 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 57  arse;.  sqlite3W
1cf05 61 6c 6b 45 78 70 72 28 26 77 2c 20 70 45 78 70  alkExpr(&w, pExp
1cf06 72 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65  r);.}.../*.** Ge
1cf07 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
1cf08 20 70 75 73 68 65 73 20 74 68 65 20 76 61 6c 75   pushes the valu
1cf09 65 20 6f 66 20 65 76 65 72 79 20 65 6c 65 6d 65  e of every eleme
1cf0a 6e 74 20 6f 66 20 74 68 65 20 67 69 76 65 6e 0a  nt of the given.
1cf0b 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  ** expression li
1cf0c 73 74 20 69 6e 74 6f 20 61 20 73 65 71 75 65 6e  st into a sequen
1cf0d 63 65 20 6f 66 20 72 65 67 69 73 74 65 72 73 20  ce of registers 
1cf0e 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 74 61 72  beginning at tar
1cf0f 67 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  get..**.** Retur
1cf10 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
1cf11 65 6c 65 6d 65 6e 74 73 20 65 76 61 6c 75 61 74  elements evaluat
1cf12 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
1cf13 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1cf14 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73  3ExprCodeExprLis
1cf15 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  t(.  Parse *pPar
1cf16 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69  se,     /* Parsi
1cf17 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
1cf18 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c  ExprList *pList,
1cf19 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73     /* The expres
1cf1a 73 69 6f 6e 20 6c 69 73 74 20 74 6f 20 62 65 20  sion list to be 
1cf1b 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 74  coded */.  int t
1cf1c 61 72 67 65 74 2c 20 20 20 20 20 20 20 20 2f 2a  arget,        /*
1cf1d 20 57 68 65 72 65 20 74 6f 20 77 72 69 74 65 20   Where to write 
1cf1e 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 69 6e 74  results */.  int
1cf1f 20 64 6f 48 61 72 64 43 6f 70 79 20 20 20 20 20   doHardCopy     
1cf20 2f 2a 20 4d 61 6b 65 20 61 20 68 61 72 64 20 63  /* Make a hard c
1cf21 6f 70 79 20 6f 66 20 65 76 65 72 79 20 65 6c 65  opy of every ele
1cf22 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72  ment */.){.  str
1cf23 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
1cf24 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74 20  m *pItem;.  int 
1cf25 69 2c 20 6e 3b 0a 20 20 61 73 73 65 72 74 28 20  i, n;.  assert( 
1cf26 70 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 61 73  pList!=0 );.  as
1cf27 73 65 72 74 28 20 74 61 72 67 65 74 3e 30 20 29  sert( target>0 )
1cf28 3b 0a 20 20 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e  ;.  n = pList->n
1cf29 45 78 70 72 3b 0a 20 20 66 6f 72 28 70 49 74 65  Expr;.  for(pIte
1cf2a 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b  m=pList->a, i=0;
1cf2b 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 49 74 65 6d   i<n; i++, pItem
1cf2c 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74  ++){.    if( pIt
1cf2d 65 6d 2d 3e 69 41 6c 69 61 73 20 29 7b 0a 20 20  em->iAlias ){.  
1cf2e 20 20 20 20 69 6e 74 20 69 52 65 67 20 3d 20 63      int iReg = c
1cf2f 6f 64 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c  odeAlias(pParse,
1cf30 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 2c 20   pItem->iAlias, 
1cf31 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 74 61  pItem->pExpr, ta
1cf32 72 67 65 74 2b 69 29 3b 0a 20 20 20 20 20 20 56  rget+i);.      V
1cf33 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33  dbe *v = sqlite3
1cf34 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
1cf35 0a 20 20 20 20 20 20 69 66 28 20 69 52 65 67 21  .      if( iReg!
1cf36 3d 74 61 72 67 65 74 2b 69 20 29 7b 0a 20 20 20  =target+i ){.   
1cf37 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1cf38 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f  AddOp2(v, OP_SCo
1cf39 70 79 2c 20 69 52 65 67 2c 20 74 61 72 67 65 74  py, iReg, target
1cf3a 2b 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  +i);.      }.   
1cf3b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
1cf3c 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
1cf3d 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70 45 78  arse, pItem->pEx
1cf3e 70 72 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a 20  pr, target+i);. 
1cf3f 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64 6f 48     }.    if( doH
1cf40 61 72 64 43 6f 70 79 20 29 7b 0a 20 20 20 20 20  ardCopy ){.     
1cf41 20 73 71 6c 69 74 65 33 45 78 70 72 48 61 72 64   sqlite3ExprHard
1cf42 43 6f 70 79 28 70 50 61 72 73 65 2c 20 74 61 72  Copy(pParse, tar
1cf43 67 65 74 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20  get, n);.    }. 
1cf44 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d   }.  return n;.}
1cf45 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
1cf46 20 63 6f 64 65 20 66 6f 72 20 61 20 62 6f 6f 6c   code for a bool
1cf47 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 73  ean expression s
1cf48 75 63 68 20 74 68 61 74 20 61 20 6a 75 6d 70 20  uch that a jump 
1cf49 69 73 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 74 68  is made.** to th
1cf4a 65 20 6c 61 62 65 6c 20 22 64 65 73 74 22 20 69  e label "dest" i
1cf4b 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
1cf4c 20 69 73 20 74 72 75 65 20 62 75 74 20 65 78 65   is true but exe
1cf4d 63 75 74 69 6f 6e 0a 2a 2a 20 63 6f 6e 74 69 6e  cution.** contin
1cf4e 75 65 73 20 73 74 72 61 69 67 68 74 20 74 68 72  ues straight thr
1cf4f 75 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73  u if the express
1cf50 69 6f 6e 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2a  ion is false..**
1cf51 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65  .** If the expre
1cf52 73 73 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 20  ssion evaluates 
1cf53 74 6f 20 4e 55 4c 4c 20 28 6e 65 69 74 68 65 72  to NULL (neither
1cf54 20 74 72 75 65 20 6e 6f 72 20 66 61 6c 73 65 29   true nor false)
1cf55 2c 20 74 68 65 6e 0a 2a 2a 20 74 61 6b 65 20 74  , then.** take t
1cf56 68 65 20 6a 75 6d 70 20 69 66 20 74 68 65 20 6a  he jump if the j
1cf57 75 6d 70 49 66 4e 75 6c 6c 20 66 6c 61 67 20 69  umpIfNull flag i
1cf58 73 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  s SQLITE_JUMPIFN
1cf59 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ULL..**.** This 
1cf5a 63 6f 64 65 20 64 65 70 65 6e 64 73 20 6f 6e 20  code depends on 
1cf5b 74 68 65 20 66 61 63 74 20 74 68 61 74 20 63 65  the fact that ce
1cf5c 72 74 61 69 6e 20 74 6f 6b 65 6e 20 76 61 6c 75  rtain token valu
1cf5d 65 73 20 28 65 78 3a 20 54 4b 5f 45 51 29 0a 2a  es (ex: TK_EQ).*
1cf5e 2a 20 61 72 65 20 74 68 65 20 73 61 6d 65 20 61  * are the same a
1cf5f 73 20 6f 70 63 6f 64 65 20 76 61 6c 75 65 73 20  s opcode values 
1cf60 28 65 78 3a 20 4f 50 5f 45 71 29 20 74 68 61 74  (ex: OP_Eq) that
1cf61 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 63   implement the c
1cf62 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20  orresponding.** 
1cf63 6f 70 65 72 61 74 69 6f 6e 2e 20 20 53 70 65 63  operation.  Spec
1cf64 69 61 6c 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20  ial comments in 
1cf65 76 64 62 65 2e 63 20 61 6e 64 20 74 68 65 20 6d  vdbe.c and the m
1cf66 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 73 63 72  kopcodeh.awk scr
1cf67 69 70 74 20 69 6e 0a 2a 2a 20 74 68 65 20 6d 61  ipt in.** the ma
1cf68 6b 65 20 70 72 6f 63 65 73 73 20 63 61 75 73 65  ke process cause
1cf69 20 74 68 65 73 65 20 76 61 6c 75 65 73 20 74 6f   these values to
1cf6a 20 61 6c 69 67 6e 2e 20 20 41 73 73 65 72 74 28   align.  Assert(
1cf6b 29 73 20 69 6e 20 74 68 65 20 63 6f 64 65 0a 2a  )s in the code.*
1cf6c 2a 20 62 65 6c 6f 77 20 76 65 72 69 66 79 20 74  * below verify t
1cf6d 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 73 20  hat the numbers 
1cf6e 61 72 65 20 61 6c 69 67 6e 65 64 20 63 6f 72 72  are aligned corr
1cf6f 65 63 74 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ectly..*/.SQLITE
1cf70 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1cf71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28  lite3ExprIfTrue(
1cf72 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
1cf73 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20  xpr *pExpr, int 
1cf74 64 65 73 74 2c 20 69 6e 74 20 6a 75 6d 70 49 66  dest, int jumpIf
1cf75 4e 75 6c 6c 29 7b 0a 20 20 56 64 62 65 20 2a 76  Null){.  Vdbe *v
1cf76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
1cf77 3b 0a 20 20 69 6e 74 20 6f 70 20 3d 20 30 3b 0a  ;.  int op = 0;.
1cf78 20 20 69 6e 74 20 72 65 67 46 72 65 65 31 20 3d    int regFree1 =
1cf79 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 72 65   0;.  int regFre
1cf7a 65 32 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 31  e2 = 0;.  int r1
1cf7b 2c 20 72 32 3b 0a 0a 20 20 61 73 73 65 72 74 28  , r2;..  assert(
1cf7c 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 53 51 4c   jumpIfNull==SQL
1cf7d 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 7c  ITE_JUMPIFNULL |
1cf7e 7c 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20  | jumpIfNull==0 
1cf7f 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 7c 7c  );.  if( v==0 ||
1cf80 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75   pExpr==0 ) retu
1cf81 72 6e 3b 0a 20 20 6f 70 20 3d 20 70 45 78 70 72  rn;.  op = pExpr
1cf82 2d 3e 6f 70 3b 0a 20 20 73 77 69 74 63 68 28 20  ->op;.  switch( 
1cf83 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54  op ){.    case T
1cf84 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20 20 20 20 69  K_AND: {.      i
1cf85 6e 74 20 64 32 20 3d 20 73 71 6c 69 74 65 33 56  nt d2 = sqlite3V
1cf86 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
1cf87 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1cf88 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29   jumpIfNull==0 )
1cf89 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1cf8a 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c  ( pParse->disabl
1cf8b 65 43 6f 6c 43 61 63 68 65 3d 3d 30 20 29 3b 0a  eColCache==0 );.
1cf8c 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
1cf8d 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c  rIfFalse(pParse,
1cf8e 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64   pExpr->pLeft, d
1cf8f 32 2c 6a 75 6d 70 49 66 4e 75 6c 6c 5e 53 51 4c  2,jumpIfNull^SQL
1cf90 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b  ITE_JUMPIFNULL);
1cf91 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64  .      pParse->d
1cf92 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 2b 2b  isableColCache++
1cf93 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
1cf94 78 70 72 49 66 54 72 75 65 28 70 50 61 72 73 65  xprIfTrue(pParse
1cf95 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  , pExpr->pRight,
1cf96 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c   dest, jumpIfNul
1cf97 6c 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  l);.      assert
1cf98 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c  ( pParse->disabl
1cf99 65 43 6f 6c 43 61 63 68 65 3e 30 20 29 3b 0a 20  eColCache>0 );. 
1cf9a 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73       pParse->dis
1cf9b 61 62 6c 65 43 6f 6c 43 61 63 68 65 2d 2d 3b 0a  ableColCache--;.
1cf9c 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1cf9d 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
1cf9e 20 64 32 29 3b 0a 20 20 20 20 20 20 62 72 65 61   d2);.      brea
1cf9f 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
1cfa0 65 20 54 4b 5f 4f 52 3a 20 7b 0a 20 20 20 20 20  e TK_OR: {.     
1cfa1 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49   testcase( jumpI
1cfa2 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  fNull==0 );.    
1cfa3 20 20 74 65 73 74 63 61 73 65 28 20 70 50 61 72    testcase( pPar
1cfa4 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61  se->disableColCa
1cfa5 63 68 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  che==0 );.      
1cfa6 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75  sqlite3ExprIfTru
1cfa7 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
1cfa8 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a 75  >pLeft, dest, ju
1cfa9 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20  mpIfNull);.     
1cfaa 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65   pParse->disable
1cfab 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 20 20 20  ColCache++;.    
1cfac 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 54    sqlite3ExprIfT
1cfad 72 75 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  rue(pParse, pExp
1cfae 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c  r->pRight, dest,
1cfaf 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20   jumpIfNull);.  
1cfb0 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72      assert( pPar
1cfb1 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61  se->disableColCa
1cfb2 63 68 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 70  che>0 );.      p
1cfb3 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f  Parse->disableCo
1cfb4 6c 43 61 63 68 65 2d 2d 3b 0a 20 20 20 20 20 20  lCache--;.      
1cfb5 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1cfb6 20 63 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a   case TK_NOT: {.
1cfb7 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1cfb8 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b  jumpIfNull==0 );
1cfb9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
1cfba 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65  prIfFalse(pParse
1cfbb 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
1cfbc 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c  dest, jumpIfNull
1cfbd 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
1cfbe 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
1cfbf 4b 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65 20 54  K_LT:.    case T
1cfc0 4b 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54  K_LE:.    case T
1cfc1 4b 5f 47 54 3a 0a 20 20 20 20 63 61 73 65 20 54  K_GT:.    case T
1cfc2 4b 5f 47 45 3a 0a 20 20 20 20 63 61 73 65 20 54  K_GE:.    case T
1cfc3 4b 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65 20 54  K_NE:.    case T
1cfc4 4b 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20 61 73  K_EQ: {.      as
1cfc5 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f  sert( TK_LT==OP_
1cfc6 4c 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  Lt );.      asse
1cfc7 72 74 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65  rt( TK_LE==OP_Le
1cfc8 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
1cfc9 28 20 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29  ( TK_GT==OP_Gt )
1cfca 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1cfcb 54 4b 5f 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a  TK_GE==OP_Ge );.
1cfcc 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
1cfcd 5f 45 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20  _EQ==OP_Eq );.  
1cfce 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e      assert( TK_N
1cfcf 45 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20  E==OP_Ne );.    
1cfd0 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
1cfd1 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20 74  TK_LT );.      t
1cfd2 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
1cfd3 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  LE );.      test
1cfd4 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20  case( op==TK_GT 
1cfd5 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1cfd6 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a  e( op==TK_GE );.
1cfd7 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1cfd8 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20  op==TK_EQ );.   
1cfd9 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
1cfda 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20  =TK_NE );.      
1cfdb 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66  testcase( jumpIf
1cfdc 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20  Null==0 );.     
1cfdd 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72   codeCompareOper
1cfde 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 78  ands(pParse, pEx
1cfdf 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20  pr->pLeft, &r1, 
1cfe0 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20  &regFree1,.     
1cfe1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cfe2 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78               pEx
1cfe3 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c  pr->pRight, &r2,
1cfe4 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20   &regFree2);.   
1cfe5 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70     codeCompare(p
1cfe6 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
1cfe7 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67  eft, pExpr->pRig
1cfe8 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 20  ht, op,.        
1cfe9 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 32            r1, r2
1cfea 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75  , dest, jumpIfNu
1cfeb 6c 6c 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  ll);.      testc
1cfec 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30  ase( regFree1==0
1cfed 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1cfee 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20  se( regFree2==0 
1cfef 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
1cff0 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
1cff1 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61  K_ISNULL:.    ca
1cff2 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b  se TK_NOTNULL: {
1cff3 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
1cff4 4b 5f 49 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 4e  K_ISNULL==OP_IsN
1cff5 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20 61 73 73  ull );.      ass
1cff6 65 72 74 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d  ert( TK_NOTNULL=
1cff7 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20  =OP_NotNull );. 
1cff8 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
1cff9 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a  p==TK_ISNULL );.
1cffa 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1cffb 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29  op==TK_NOTNULL )
1cffc 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c  ;.      r1 = sql
1cffd 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70  ite3ExprCodeTemp
1cffe 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
1cfff 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 31  pLeft, &regFree1
1d000 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1d001 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 6f 70  VdbeAddOp2(v, op
1d002 2c 20 72 31 2c 20 64 65 73 74 29 3b 0a 20 20 20  , r1, dest);.   
1d003 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
1d004 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20  Free1==0 );.    
1d005 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1d006 20 20 20 63 61 73 65 20 54 4b 5f 42 45 54 57 45     case TK_BETWE
1d007 45 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 20  EN: {.      /*  
1d008 20 20 78 20 42 45 54 57 45 45 4e 20 79 20 41 4e    x BETWEEN y AN
1d009 44 20 7a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  D z.      **.   
1d00a 20 20 20 2a 2a 20 49 73 20 65 71 75 69 76 61 6c     ** Is equival
1d00b 65 6e 74 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a  ent to .      **
1d00c 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d  .      **    x>=
1d00d 79 20 41 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 20  y AND x<=z.     
1d00e 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 43 6f 64   **.      ** Cod
1d00f 65 20 69 74 20 61 73 20 73 75 63 68 2c 20 74 61  e it as such, ta
1d010 6b 69 6e 67 20 63 61 72 65 20 74 6f 20 64 6f 20  king care to do 
1d011 74 68 65 20 63 6f 6d 6d 6f 6e 20 73 75 62 65 78  the common subex
1d012 70 72 65 73 73 69 6f 6e 0a 20 20 20 20 20 20 2a  pression.      *
1d013 2a 20 65 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  * elementation o
1d014 66 20 78 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  f x..      */.  
1d015 20 20 20 20 45 78 70 72 20 65 78 70 72 41 6e 64      Expr exprAnd
1d016 3b 0a 20 20 20 20 20 20 45 78 70 72 20 63 6f 6d  ;.      Expr com
1d017 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 45 78 70  pLeft;.      Exp
1d018 72 20 63 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20  r compRight;.   
1d019 20 20 20 45 78 70 72 20 65 78 70 72 58 3b 0a 0a     Expr exprX;..
1d01a 20 20 20 20 20 20 65 78 70 72 58 20 3d 20 2a 70        exprX = *p
1d01b 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20  Expr->pLeft;.   
1d01c 20 20 20 65 78 70 72 41 6e 64 2e 6f 70 20 3d 20     exprAnd.op = 
1d01d 54 4b 5f 41 4e 44 3b 0a 20 20 20 20 20 20 65 78  TK_AND;.      ex
1d01e 70 72 41 6e 64 2e 70 4c 65 66 74 20 3d 20 26 63  prAnd.pLeft = &c
1d01f 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 65  ompLeft;.      e
1d020 78 70 72 41 6e 64 2e 70 52 69 67 68 74 20 3d 20  xprAnd.pRight = 
1d021 26 63 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20 20  &compRight;.    
1d022 20 20 63 6f 6d 70 4c 65 66 74 2e 6f 70 20 3d 20    compLeft.op = 
1d023 54 4b 5f 47 45 3b 0a 20 20 20 20 20 20 63 6f 6d  TK_GE;.      com
1d024 70 4c 65 66 74 2e 70 4c 65 66 74 20 3d 20 26 65  pLeft.pLeft = &e
1d025 78 70 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d 70  xprX;.      comp
1d026 4c 65 66 74 2e 70 52 69 67 68 74 20 3d 20 70 45  Left.pRight = pE
1d027 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d  xpr->pList->a[0]
1d028 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 63 6f  .pExpr;.      co
1d029 6d 70 52 69 67 68 74 2e 6f 70 20 3d 20 54 4b 5f  mpRight.op = TK_
1d02a 4c 45 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52 69  LE;.      compRi
1d02b 67 68 74 2e 70 4c 65 66 74 20 3d 20 26 65 78 70  ght.pLeft = &exp
1d02c 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52 69  rX;.      compRi
1d02d 67 68 74 2e 70 52 69 67 68 74 20 3d 20 70 45 78  ght.pRight = pEx
1d02e 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e  pr->pList->a[1].
1d02f 70 45 78 70 72 3b 0a 20 20 20 20 20 20 65 78 70  pExpr;.      exp
1d030 72 58 2e 69 54 61 62 6c 65 20 3d 20 73 71 6c 69  rX.iTable = sqli
1d031 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28  te3ExprCodeTemp(
1d032 70 50 61 72 73 65 2c 20 26 65 78 70 72 58 2c 20  pParse, &exprX, 
1d033 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20  &regFree1);.    
1d034 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46    testcase( regF
1d035 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ree1==0 );.     
1d036 20 65 78 70 72 58 2e 6f 70 20 3d 20 54 4b 5f 52   exprX.op = TK_R
1d037 45 47 49 53 54 45 52 3b 0a 20 20 20 20 20 20 74  EGISTER;.      t
1d038 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e  estcase( jumpIfN
1d039 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ull==0 );.      
1d03a 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75  sqlite3ExprIfTru
1d03b 65 28 70 50 61 72 73 65 2c 20 26 65 78 70 72 41  e(pParse, &exprA
1d03c 6e 64 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66  nd, dest, jumpIf
1d03d 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65  Null);.      bre
1d03e 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65  ak;.    }.    de
1d03f 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72  fault: {.      r
1d040 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  1 = sqlite3ExprC
1d041 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20  odeTemp(pParse, 
1d042 70 45 78 70 72 2c 20 26 72 65 67 46 72 65 65 31  pExpr, &regFree1
1d043 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1d044 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
1d045 5f 49 66 2c 20 72 31 2c 20 64 65 73 74 2c 20 6a  _If, r1, dest, j
1d046 75 6d 70 49 66 4e 75 6c 6c 21 3d 30 29 3b 0a 20  umpIfNull!=0);. 
1d047 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
1d048 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20  egFree1==0 );.  
1d049 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75      testcase( ju
1d04a 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20  mpIfNull==0 );. 
1d04b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1d04c 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52  }.  }.  sqlite3R
1d04d 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
1d04e 61 72 73 65 2c 20 72 65 67 46 72 65 65 31 29 3b  arse, regFree1);
1d04f 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
1d050 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
1d051 20 72 65 67 46 72 65 65 32 29 3b 20 20 0a 7d 0a   regFree2);  .}.
1d052 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
1d053 63 6f 64 65 20 66 6f 72 20 61 20 62 6f 6f 6c 65  code for a boole
1d054 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 73 75  an expression su
1d055 63 68 20 74 68 61 74 20 61 20 6a 75 6d 70 20 69  ch that a jump i
1d056 73 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 74 68 65  s made.** to the
1d057 20 6c 61 62 65 6c 20 22 64 65 73 74 22 20 69 66   label "dest" if
1d058 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
1d059 69 73 20 66 61 6c 73 65 20 62 75 74 20 65 78 65  is false but exe
1d05a 63 75 74 69 6f 6e 0a 2a 2a 20 63 6f 6e 74 69 6e  cution.** contin
1d05b 75 65 73 20 73 74 72 61 69 67 68 74 20 74 68 72  ues straight thr
1d05c 75 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73  u if the express
1d05d 69 6f 6e 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a  ion is true..**.
1d05e 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73  ** If the expres
1d05f 73 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 20 74  sion evaluates t
1d060 6f 20 4e 55 4c 4c 20 28 6e 65 69 74 68 65 72 20  o NULL (neither 
1d061 74 72 75 65 20 6e 6f 72 20 66 61 6c 73 65 29 20  true nor false) 
1d062 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 69 66 20  then.** jump if 
1d063 6a 75 6d 70 49 66 4e 75 6c 6c 20 69 73 20 53 51  jumpIfNull is SQ
1d064 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20  LITE_JUMPIFNULL 
1d065 6f 72 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20  or fall through 
1d066 69 66 20 6a 75 6d 70 49 66 4e 75 6c 6c 0a 2a 2a  if jumpIfNull.**
1d067 20 69 73 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45   is 0..*/.SQLITE
1d068 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1d069 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65  lite3ExprIfFalse
1d06a 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1d06b 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74  Expr *pExpr, int
1d06c 20 64 65 73 74 2c 20 69 6e 74 20 6a 75 6d 70 49   dest, int jumpI
1d06d 66 4e 75 6c 6c 29 7b 0a 20 20 56 64 62 65 20 2a  fNull){.  Vdbe *
1d06e 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
1d06f 65 3b 0a 20 20 69 6e 74 20 6f 70 20 3d 20 30 3b  e;.  int op = 0;
1d070 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65 31 20  .  int regFree1 
1d071 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 72  = 0;.  int regFr
1d072 65 65 32 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  ee2 = 0;.  int r
1d073 31 2c 20 72 32 3b 0a 0a 20 20 61 73 73 65 72 74  1, r2;..  assert
1d074 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 53 51  ( jumpIfNull==SQ
1d075 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20  LITE_JUMPIFNULL 
1d076 7c 7c 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30  || jumpIfNull==0
1d077 20 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 7c   );.  if( v==0 |
1d078 7c 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74  | pExpr==0 ) ret
1d079 75 72 6e 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 76  urn;..  /* The v
1d07a 61 6c 75 65 20 6f 66 20 70 45 78 70 72 2d 3e 6f  alue of pExpr->o
1d07b 70 20 61 6e 64 20 6f 70 20 61 72 65 20 72 65 6c  p and op are rel
1d07c 61 74 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  ated as follows:
1d07d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20  .  **.  **      
1d07e 20 70 45 78 70 72 2d 3e 6f 70 20 20 20 20 20 20   pExpr->op      
1d07f 20 20 20 20 20 20 6f 70 0a 20 20 2a 2a 20 20 20        op.  **   
1d080 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20      ---------   
1d081 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d         ---------
1d082 2d 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f  -.  **       TK_
1d083 49 53 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20  ISNULL          
1d084 4f 50 5f 4e 6f 74 4e 75 6c 6c 0a 20 20 2a 2a 20  OP_NotNull.  ** 
1d085 20 20 20 20 20 20 54 4b 5f 4e 4f 54 4e 55 4c 4c        TK_NOTNULL
1d086 20 20 20 20 20 20 20 20 20 4f 50 5f 49 73 4e 75           OP_IsNu
1d087 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b  ll.  **       TK
1d088 5f 4e 45 20 20 20 20 20 20 20 20 20 20 20 20 20  _NE             
1d089 20 4f 50 5f 45 71 0a 20 20 2a 2a 20 20 20 20 20   OP_Eq.  **     
1d08a 20 20 54 4b 5f 45 51 20 20 20 20 20 20 20 20 20    TK_EQ         
1d08b 20 20 20 20 20 4f 50 5f 4e 65 0a 20 20 2a 2a 20       OP_Ne.  ** 
1d08c 20 20 20 20 20 20 54 4b 5f 47 54 20 20 20 20 20        TK_GT     
1d08d 20 20 20 20 20 20 20 20 20 4f 50 5f 4c 65 0a 20           OP_Le. 
1d08e 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4c 45 20   **       TK_LE 
1d08f 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f               OP_
1d090 47 74 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b  Gt.  **       TK
1d091 5f 47 45 20 20 20 20 20 20 20 20 20 20 20 20 20  _GE             
1d092 20 4f 50 5f 4c 74 0a 20 20 2a 2a 20 20 20 20 20   OP_Lt.  **     
1d093 20 20 54 4b 5f 4c 54 20 20 20 20 20 20 20 20 20    TK_LT         
1d094 20 20 20 20 20 4f 50 5f 47 65 0a 20 20 2a 2a 0a       OP_Ge.  **.
1d095 20 20 2a 2a 20 46 6f 72 20 6f 74 68 65 72 20 76    ** For other v
1d096 61 6c 75 65 73 20 6f 66 20 70 45 78 70 72 2d 3e  alues of pExpr->
1d097 6f 70 2c 20 6f 70 20 69 73 20 75 6e 64 65 66 69  op, op is undefi
1d098 6e 65 64 20 61 6e 64 20 75 6e 75 73 65 64 2e 0a  ned and unused..
1d099 20 20 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f    ** The value o
1d09a 66 20 54 4b 5f 20 61 6e 64 20 4f 50 5f 20 63 6f  f TK_ and OP_ co
1d09b 6e 73 74 61 6e 74 73 20 61 72 65 20 61 72 72 61  nstants are arra
1d09c 6e 67 65 64 20 73 75 63 68 20 74 68 61 74 20 77  nged such that w
1d09d 65 0a 20 20 2a 2a 20 63 61 6e 20 63 6f 6d 70 75  e.  ** can compu
1d09e 74 65 20 74 68 65 20 6d 61 70 70 69 6e 67 20 61  te the mapping a
1d09f 62 6f 76 65 20 75 73 69 6e 67 20 74 68 65 20 66  bove using the f
1d0a0 6f 6c 6c 6f 77 69 6e 67 20 65 78 70 72 65 73 73  ollowing express
1d0a1 69 6f 6e 2e 0a 20 20 2a 2a 20 41 73 73 65 72 74  ion..  ** Assert
1d0a2 28 29 73 20 76 65 72 69 66 79 20 74 68 61 74 20  ()s verify that 
1d0a3 74 68 65 20 63 6f 6d 70 75 74 61 74 69 6f 6e 20  the computation 
1d0a4 69 73 20 63 6f 72 72 65 63 74 2e 0a 20 20 2a 2f  is correct..  */
1d0a5 0a 20 20 6f 70 20 3d 20 28 28 70 45 78 70 72 2d  .  op = ((pExpr-
1d0a6 3e 6f 70 2b 28 54 4b 5f 49 53 4e 55 4c 4c 26 31  >op+(TK_ISNULL&1
1d0a7 29 29 5e 31 29 2d 28 54 4b 5f 49 53 4e 55 4c 4c  ))^1)-(TK_ISNULL
1d0a8 26 31 29 3b 0a 0a 20 20 2f 2a 20 56 65 72 69 66  &1);..  /* Verif
1d0a9 79 20 63 6f 72 72 65 63 74 20 61 6c 69 67 6e 6d  y correct alignm
1d0aa 65 6e 74 20 6f 66 20 54 4b 5f 20 61 6e 64 20 4f  ent of TK_ and O
1d0ab 50 5f 20 63 6f 6e 73 74 61 6e 74 73 0a 20 20 2a  P_ constants.  *
1d0ac 2f 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70  /.  assert( pExp
1d0ad 72 2d 3e 6f 70 21 3d 54 4b 5f 49 53 4e 55 4c 4c  r->op!=TK_ISNULL
1d0ae 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e 6f 74 4e 75   || op==OP_NotNu
1d0af 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ll );.  assert( 
1d0b0 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4e 4f  pExpr->op!=TK_NO
1d0b1 54 4e 55 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f 50 5f  TNULL || op==OP_
1d0b2 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 61 73 73 65  IsNull );.  asse
1d0b3 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  rt( pExpr->op!=T
1d0b4 4b 5f 4e 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 45  K_NE || op==OP_E
1d0b5 71 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  q );.  assert( p
1d0b6 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 45 51 20  Expr->op!=TK_EQ 
1d0b7 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e 65 20 29 3b 0a  || op==OP_Ne );.
1d0b8 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
1d0b9 3e 6f 70 21 3d 54 4b 5f 4c 54 20 7c 7c 20 6f 70  >op!=TK_LT || op
1d0ba 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 61 73 73  ==OP_Ge );.  ass
1d0bb 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d  ert( pExpr->op!=
1d0bc 54 4b 5f 4c 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f  TK_LE || op==OP_
1d0bd 47 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Gt );.  assert( 
1d0be 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 47 54  pExpr->op!=TK_GT
1d0bf 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c 65 20 29 3b   || op==OP_Le );
1d0c0 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72  .  assert( pExpr
1d0c1 2d 3e 6f 70 21 3d 54 4b 5f 47 45 20 7c 7c 20 6f  ->op!=TK_GE || o
1d0c2 70 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 0a 20 20 73  p==OP_Lt );..  s
1d0c3 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70  witch( pExpr->op
1d0c4 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   ){.    case TK_
1d0c5 41 4e 44 3a 20 7b 0a 20 20 20 20 20 20 74 65 73  AND: {.      tes
1d0c6 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c  tcase( jumpIfNul
1d0c7 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65  l==0 );.      te
1d0c8 73 74 63 61 73 65 28 20 70 50 61 72 73 65 2d 3e  stcase( pParse->
1d0c9 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3d  disableColCache=
1d0ca 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  =0 );.      sqli
1d0cb 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70  te3ExprIfFalse(p
1d0cc 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
1d0cd 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49  eft, dest, jumpI
1d0ce 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 70 50  fNull);.      pP
1d0cf 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c  arse->disableCol
1d0d0 43 61 63 68 65 2b 2b 3b 0a 20 20 20 20 20 20 73  Cache++;.      s
1d0d1 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73  qlite3ExprIfFals
1d0d2 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
1d0d3 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a  >pRight, dest, j
1d0d4 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20  umpIfNull);.    
1d0d5 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
1d0d6 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68  ->disableColCach
1d0d7 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 70 50 61  e>0 );.      pPa
1d0d8 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43  rse->disableColC
1d0d9 61 63 68 65 2d 2d 3b 0a 20 20 20 20 20 20 62 72  ache--;.      br
1d0da 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
1d0db 61 73 65 20 54 4b 5f 4f 52 3a 20 7b 0a 20 20 20  ase TK_OR: {.   
1d0dc 20 20 20 69 6e 74 20 64 32 20 3d 20 73 71 6c 69     int d2 = sqli
1d0dd 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
1d0de 28 76 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  (v);.      testc
1d0df 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d  ase( jumpIfNull=
1d0e0 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  =0 );.      test
1d0e1 63 61 73 65 28 20 70 50 61 72 73 65 2d 3e 64 69  case( pParse->di
1d0e2 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3d 3d 30  sableColCache==0
1d0e3 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
1d0e4 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61 72  3ExprIfTrue(pPar
1d0e5 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
1d0e6 2c 20 64 32 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c  , d2, jumpIfNull
1d0e7 5e 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55  ^SQLITE_JUMPIFNU
1d0e8 4c 4c 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73  LL);.      pPars
1d0e9 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63  e->disableColCac
1d0ea 68 65 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c 69  he++;.      sqli
1d0eb 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70  te3ExprIfFalse(p
1d0ec 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52  Parse, pExpr->pR
1d0ed 69 67 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70  ight, dest, jump
1d0ee 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 61  IfNull);.      a
1d0ef 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 64  ssert( pParse->d
1d0f0 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3e 30  isableColCache>0
1d0f1 20 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65   );.      pParse
1d0f2 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68  ->disableColCach
1d0f3 65 2d 2d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  e--;.      sqlit
1d0f4 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
1d0f5 65 6c 28 76 2c 20 64 32 29 3b 0a 20 20 20 20 20  el(v, d2);.     
1d0f6 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1d0f7 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b    case TK_NOT: {
1d0f8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
1d0f9 70 72 49 66 54 72 75 65 28 70 50 61 72 73 65 2c  prIfTrue(pParse,
1d0fa 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64   pExpr->pLeft, d
1d0fb 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29  est, jumpIfNull)
1d0fc 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1d0fd 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
1d0fe 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _LT:.    case TK
1d0ff 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _LE:.    case TK
1d100 5f 47 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _GT:.    case TK
1d101 5f 47 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _GE:.    case TK
1d102 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _NE:.    case TK
1d103 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20 74 65 73  _EQ: {.      tes
1d104 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 54  tcase( op==TK_LT
1d105 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1d106 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 45 20 29 3b  se( op==TK_LE );
1d107 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1d108 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 3b 0a 20 20   op==TK_GT );.  
1d109 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
1d10a 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20 20 20 20 20  ==TK_GE );.     
1d10b 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
1d10c 4b 5f 45 51 20 29 3b 0a 20 20 20 20 20 20 74 65  K_EQ );.      te
1d10d 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e  stcase( op==TK_N
1d10e 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  E );.      testc
1d10f 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d  ase( jumpIfNull=
1d110 3d 30 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65  =0 );.      code
1d111 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28  CompareOperands(
1d112 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
1d113 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 65 67 46  Left, &r1, &regF
1d114 72 65 65 31 2c 0a 20 20 20 20 20 20 20 20 20 20  ree1,.          
1d115 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d116 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70          pExpr->p
1d117 52 69 67 68 74 2c 20 26 72 32 2c 20 26 72 65 67  Right, &r2, &reg
1d118 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 63 6f  Free2);.      co
1d119 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65  deCompare(pParse
1d11a 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
1d11b 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f  pExpr->pRight, o
1d11c 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  p,.             
1d11d 20 20 20 20 20 72 31 2c 20 72 32 2c 20 64 65 73       r1, r2, des
1d11e 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a  t, jumpIfNull);.
1d11f 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1d120 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20  regFree1==0 );. 
1d121 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
1d122 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20  egFree2==0 );.  
1d123 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1d124 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 53 4e  .    case TK_ISN
1d125 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  ULL:.    case TK
1d126 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20  _NOTNULL: {.    
1d127 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
1d128 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20  TK_ISNULL );.   
1d129 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
1d12a 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20  =TK_NOTNULL );. 
1d12b 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65       r1 = sqlite
1d12c 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
1d12d 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
1d12e 66 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a  ft, &regFree1);.
1d12f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1d130 65 41 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72  eAddOp2(v, op, r
1d131 31 2c 20 64 65 73 74 29 3b 0a 20 20 20 20 20 20  1, dest);.      
1d132 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
1d133 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62  e1==0 );.      b
1d134 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1d135 63 61 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a  case TK_BETWEEN:
1d136 20 7b 0a 20 20 20 20 20 20 2f 2a 20 20 20 20 78   {.      /*    x
1d137 20 42 45 54 57 45 45 4e 20 79 20 41 4e 44 20 7a   BETWEEN y AND z
1d138 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
1d139 2a 2a 20 49 73 20 65 71 75 69 76 61 6c 65 6e 74  ** Is equivalent
1d13a 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20   to .      **.  
1d13b 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d 79 20 41      **    x>=y A
1d13c 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 20 20 2a 2a  ND x<=z.      **
1d13d 0a 20 20 20 20 20 20 2a 2a 20 43 6f 64 65 20 69  .      ** Code i
1d13e 74 20 61 73 20 73 75 63 68 2c 20 74 61 6b 69 6e  t as such, takin
1d13f 67 20 63 61 72 65 20 74 6f 20 64 6f 20 74 68 65  g care to do the
1d140 20 63 6f 6d 6d 6f 6e 20 73 75 62 65 78 70 72 65   common subexpre
1d141 73 73 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 65  ssion.      ** e
1d142 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78  lementation of x
1d143 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
1d144 20 45 78 70 72 20 65 78 70 72 41 6e 64 3b 0a 20   Expr exprAnd;. 
1d145 20 20 20 20 20 45 78 70 72 20 63 6f 6d 70 4c 65       Expr compLe
1d146 66 74 3b 0a 20 20 20 20 20 20 45 78 70 72 20 63  ft;.      Expr c
1d147 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20  ompRight;.      
1d148 45 78 70 72 20 65 78 70 72 58 3b 0a 0a 20 20 20  Expr exprX;..   
1d149 20 20 20 65 78 70 72 58 20 3d 20 2a 70 45 78 70     exprX = *pExp
1d14a 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20  r->pLeft;.      
1d14b 65 78 70 72 41 6e 64 2e 6f 70 20 3d 20 54 4b 5f  exprAnd.op = TK_
1d14c 41 4e 44 3b 0a 20 20 20 20 20 20 65 78 70 72 41  AND;.      exprA
1d14d 6e 64 2e 70 4c 65 66 74 20 3d 20 26 63 6f 6d 70  nd.pLeft = &comp
1d14e 4c 65 66 74 3b 0a 20 20 20 20 20 20 65 78 70 72  Left;.      expr
1d14f 41 6e 64 2e 70 52 69 67 68 74 20 3d 20 26 63 6f  And.pRight = &co
1d150 6d 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 63  mpRight;.      c
1d151 6f 6d 70 4c 65 66 74 2e 6f 70 20 3d 20 54 4b 5f  ompLeft.op = TK_
1d152 47 45 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65  GE;.      compLe
1d153 66 74 2e 70 4c 65 66 74 20 3d 20 26 65 78 70 72  ft.pLeft = &expr
1d154 58 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66  X;.      compLef
1d155 74 2e 70 52 69 67 68 74 20 3d 20 70 45 78 70 72  t.pRight = pExpr
1d156 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  ->pList->a[0].pE
1d157 78 70 72 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52  xpr;.      compR
1d158 69 67 68 74 2e 6f 70 20 3d 20 54 4b 5f 4c 45 3b  ight.op = TK_LE;
1d159 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74  .      compRight
1d15a 2e 70 4c 65 66 74 20 3d 20 26 65 78 70 72 58 3b  .pLeft = &exprX;
1d15b 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74  .      compRight
1d15c 2e 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d  .pRight = pExpr-
1d15d 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78  >pList->a[1].pEx
1d15e 70 72 3b 0a 20 20 20 20 20 20 65 78 70 72 58 2e  pr;.      exprX.
1d15f 69 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33  iTable = sqlite3
1d160 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61  ExprCodeTemp(pPa
1d161 72 73 65 2c 20 26 65 78 70 72 58 2c 20 26 72 65  rse, &exprX, &re
1d162 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 74  gFree1);.      t
1d163 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
1d164 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 65 78  1==0 );.      ex
1d165 70 72 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 47 49  prX.op = TK_REGI
1d166 53 54 45 52 3b 0a 20 20 20 20 20 20 74 65 73 74  STER;.      test
1d167 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c  case( jumpIfNull
1d168 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==0 );.      sql
1d169 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
1d16a 70 50 61 72 73 65 2c 20 26 65 78 70 72 41 6e 64  pParse, &exprAnd
1d16b 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75  , dest, jumpIfNu
1d16c 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ll);.      break
1d16d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
1d16e 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 31 20  ult: {.      r1 
1d16f 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
1d170 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45  eTemp(pParse, pE
1d171 78 70 72 2c 20 26 72 65 67 46 72 65 65 31 29 3b  xpr, &regFree1);
1d172 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1d173 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49  beAddOp3(v, OP_I
1d174 66 4e 6f 74 2c 20 72 31 2c 20 64 65 73 74 2c 20  fNot, r1, dest, 
1d175 6a 75 6d 70 49 66 4e 75 6c 6c 21 3d 30 29 3b 0a  jumpIfNull!=0);.
1d176 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1d177 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20  regFree1==0 );. 
1d178 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a       testcase( j
1d179 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a  umpIfNull==0 );.
1d17a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1d17b 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
1d17c 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
1d17d 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 31 29  Parse, regFree1)
1d17e 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61  ;.  sqlite3Relea
1d17f 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
1d180 2c 20 72 65 67 46 72 65 65 32 29 3b 0a 7d 0a 0a  , regFree2);.}..
1d181 2f 2a 0a 2a 2a 20 44 6f 20 61 20 64 65 65 70 20  /*.** Do a deep 
1d182 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74 77  comparison of tw
1d183 6f 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  o expression tre
1d184 65 73 2e 20 20 52 65 74 75 72 6e 20 54 52 55 45  es.  Return TRUE
1d185 20 28 6e 6f 6e 2d 7a 65 72 6f 29 0a 2a 2a 20 69   (non-zero).** i
1d186 66 20 74 68 65 79 20 61 72 65 20 69 64 65 6e 74  f they are ident
1d187 69 63 61 6c 20 61 6e 64 20 72 65 74 75 72 6e 20  ical and return 
1d188 46 41 4c 53 45 20 69 66 20 74 68 65 79 20 64 69  FALSE if they di
1d189 66 66 65 72 20 69 6e 20 61 6e 79 20 77 61 79 2e  ffer in any way.
1d18a 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73  .**.** Sometimes
1d18b 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   this routine wi
1d18c 6c 6c 20 72 65 74 75 72 6e 20 46 41 4c 53 45 20  ll return FALSE 
1d18d 65 76 65 6e 20 69 66 20 74 68 65 20 74 77 6f 20  even if the two 
1d18e 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 72  expressions.** r
1d18f 65 61 6c 6c 79 20 61 72 65 20 65 71 75 69 76 61  eally are equiva
1d190 6c 65 6e 74 2e 20 20 49 66 20 77 65 20 63 61 6e  lent.  If we can
1d191 6e 6f 74 20 70 72 6f 76 65 20 74 68 61 74 20 74  not prove that t
1d192 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61  he expressions a
1d193 72 65 0a 2a 2a 20 69 64 65 6e 74 69 63 61 6c 2c  re.** identical,
1d194 20 77 65 20 72 65 74 75 72 6e 20 46 41 4c 53 45   we return FALSE
1d195 20 6a 75 73 74 20 74 6f 20 62 65 20 73 61 66 65   just to be safe
1d196 2e 20 20 53 6f 20 69 66 20 74 68 69 73 20 72 6f  .  So if this ro
1d197 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73  utine.** returns
1d198 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 79 6f 75   false, then you
1d199 20 64 6f 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6b   do not really k
1d19a 6e 6f 77 20 66 6f 72 20 63 65 72 74 61 69 6e 20  now for certain 
1d19b 69 66 20 74 68 65 20 74 77 6f 0a 2a 2a 20 65 78  if the two.** ex
1d19c 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20 74 68  pressions are th
1d19d 65 20 73 61 6d 65 2e 20 20 42 75 74 20 69 66 20  e same.  But if 
1d19e 79 6f 75 20 67 65 74 20 61 20 54 52 55 45 20 72  you get a TRUE r
1d19f 65 74 75 72 6e 2c 20 74 68 65 6e 20 79 6f 75 0a  eturn, then you.
1d1a0 2a 2a 20 63 61 6e 20 62 65 20 73 75 72 65 20 74  ** can be sure t
1d1a1 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61  he expressions a
1d1a2 72 65 20 74 68 65 20 73 61 6d 65 2e 20 20 49 6e  re the same.  In
1d1a3 20 74 68 65 20 70 6c 61 63 65 73 20 77 68 65 72   the places wher
1d1a4 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e  e.** this routin
1d1a5 65 20 69 73 20 75 73 65 64 2c 20 69 74 20 64 6f  e is used, it do
1d1a6 65 73 20 6e 6f 74 20 68 75 72 74 20 74 6f 20 67  es not hurt to g
1d1a7 65 74 20 61 6e 20 65 78 74 72 61 20 46 41 4c 53  et an extra FALS
1d1a8 45 20 2d 20 74 68 61 74 0a 2a 2a 20 6a 75 73 74  E - that.** just
1d1a9 20 6d 69 67 68 74 20 72 65 73 75 6c 74 20 69 6e   might result in
1d1aa 20 73 6f 6d 65 20 73 6c 69 67 68 74 6c 79 20 73   some slightly s
1d1ab 6c 6f 77 65 72 20 63 6f 64 65 2e 20 20 42 75 74  lower code.  But
1d1ac 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 61 6e   returning.** an
1d1ad 20 69 6e 63 6f 72 72 65 63 74 20 54 52 55 45 20   incorrect TRUE 
1d1ae 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f 20 61 20  could lead to a 
1d1af 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a  malfunction..*/.
1d1b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1d1b1 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  nt sqlite3ExprCo
1d1b2 6d 70 61 72 65 28 45 78 70 72 20 2a 70 41 2c 20  mpare(Expr *pA, 
1d1b3 45 78 70 72 20 2a 70 42 29 7b 0a 20 20 69 6e 74  Expr *pB){.  int
1d1b4 20 69 3b 0a 20 20 69 66 28 20 70 41 3d 3d 30 7c   i;.  if( pA==0|
1d1b5 7c 70 42 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  |pB==0 ){.    re
1d1b6 74 75 72 6e 20 70 42 3d 3d 70 41 3b 0a 20 20 7d  turn pB==pA;.  }
1d1b7 0a 20 20 69 66 28 20 70 41 2d 3e 6f 70 21 3d 70  .  if( pA->op!=p
1d1b8 42 2d 3e 6f 70 20 29 20 72 65 74 75 72 6e 20 30  B->op ) return 0
1d1b9 3b 0a 20 20 69 66 28 20 28 70 41 2d 3e 66 6c 61  ;.  if( (pA->fla
1d1ba 67 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63 74  gs & EP_Distinct
1d1bb 29 21 3d 28 70 42 2d 3e 66 6c 61 67 73 20 26 20  )!=(pB->flags & 
1d1bc 45 50 5f 44 69 73 74 69 6e 63 74 29 20 29 20 72  EP_Distinct) ) r
1d1bd 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 21  eturn 0;.  if( !
1d1be 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61  sqlite3ExprCompa
1d1bf 72 65 28 70 41 2d 3e 70 4c 65 66 74 2c 20 70 42  re(pA->pLeft, pB
1d1c0 2d 3e 70 4c 65 66 74 29 20 29 20 72 65 74 75 72  ->pLeft) ) retur
1d1c1 6e 20 30 3b 0a 20 20 69 66 28 20 21 73 71 6c 69  n 0;.  if( !sqli
1d1c2 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70  te3ExprCompare(p
1d1c3 41 2d 3e 70 52 69 67 68 74 2c 20 70 42 2d 3e 70  A->pRight, pB->p
1d1c4 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20  Right) ) return 
1d1c5 30 3b 0a 20 20 69 66 28 20 70 41 2d 3e 70 4c 69  0;.  if( pA->pLi
1d1c6 73 74 20 29 7b 0a 20 20 20 20 69 66 28 20 70 42  st ){.    if( pB
1d1c7 2d 3e 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74  ->pList==0 ) ret
1d1c8 75 72 6e 20 30 3b 0a 20 20 20 20 69 66 28 20 70  urn 0;.    if( p
1d1c9 41 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 21  A->pList->nExpr!
1d1ca 3d 70 42 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70  =pB->pList->nExp
1d1cb 72 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  r ) return 0;.  
1d1cc 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 2d    for(i=0; i<pA-
1d1cd 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69  >pList->nExpr; i
1d1ce 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21  ++){.      if( !
1d1cf 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61  sqlite3ExprCompa
1d1d0 72 65 28 70 41 2d 3e 70 4c 69 73 74 2d 3e 61 5b  re(pA->pList->a[
1d1d1 69 5d 2e 70 45 78 70 72 2c 20 70 42 2d 3e 70 4c  i].pExpr, pB->pL
1d1d2 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29  ist->a[i].pExpr)
1d1d3 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
1d1d4 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 0;.      }.  
1d1d5 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
1d1d6 70 42 2d 3e 70 4c 69 73 74 20 29 7b 0a 20 20 20  pB->pList ){.   
1d1d7 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
1d1d8 20 69 66 28 20 70 41 2d 3e 70 53 65 6c 65 63 74   if( pA->pSelect
1d1d9 20 7c 7c 20 70 42 2d 3e 70 53 65 6c 65 63 74 20   || pB->pSelect 
1d1da 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66  ) return 0;.  if
1d1db 28 20 70 41 2d 3e 69 54 61 62 6c 65 21 3d 70 42  ( pA->iTable!=pB
1d1dc 2d 3e 69 54 61 62 6c 65 20 7c 7c 20 70 41 2d 3e  ->iTable || pA->
1d1dd 69 43 6f 6c 75 6d 6e 21 3d 70 42 2d 3e 69 43 6f  iColumn!=pB->iCo
1d1de 6c 75 6d 6e 20 29 20 72 65 74 75 72 6e 20 30 3b  lumn ) return 0;
1d1df 0a 20 20 69 66 28 20 70 41 2d 3e 6f 70 21 3d 54  .  if( pA->op!=T
1d1e0 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 41 2d 3e  K_COLUMN && pA->
1d1e1 74 6f 6b 65 6e 2e 7a 20 29 7b 0a 20 20 20 20 69  token.z ){.    i
1d1e2 66 28 20 70 42 2d 3e 74 6f 6b 65 6e 2e 7a 3d 3d  f( pB->token.z==
1d1e3 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
1d1e4 20 20 69 66 28 20 70 42 2d 3e 74 6f 6b 65 6e 2e    if( pB->token.
1d1e5 6e 21 3d 70 41 2d 3e 74 6f 6b 65 6e 2e 6e 20 29  n!=pA->token.n )
1d1e6 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 69   return 0;.    i
1d1e7 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  f( sqlite3StrNIC
1d1e8 6d 70 28 28 63 68 61 72 2a 29 70 41 2d 3e 74 6f  mp((char*)pA->to
1d1e9 6b 65 6e 2e 7a 2c 28 63 68 61 72 2a 29 70 42 2d  ken.z,(char*)pB-
1d1ea 3e 74 6f 6b 65 6e 2e 7a 2c 70 42 2d 3e 74 6f 6b  >token.z,pB->tok
1d1eb 65 6e 2e 6e 29 21 3d 30 20 29 7b 0a 20 20 20 20  en.n)!=0 ){.    
1d1ec 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
1d1ed 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31  }.  }.  return 1
1d1ee 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20  ;.}.../*.** Add 
1d1ef 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f  a new element to
1d1f0 20 74 68 65 20 70 41 67 67 49 6e 66 6f 2d 3e 61   the pAggInfo->a
1d1f1 43 6f 6c 5b 5d 20 61 72 72 61 79 2e 20 20 52 65  Col[] array.  Re
1d1f2 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f  turn the index o
1d1f3 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 65 6c 65  f.** the new ele
1d1f4 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e 20 61 20  ment.  Return a 
1d1f5 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 20  negative number 
1d1f6 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e  if malloc fails.
1d1f7 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61  .*/.static int a
1d1f8 64 64 41 67 67 49 6e 66 6f 43 6f 6c 75 6d 6e 28  ddAggInfoColumn(
1d1f9 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 41 67 67  sqlite3 *db, Agg
1d1fa 49 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b 0a 20 20  Info *pInfo){.  
1d1fb 69 6e 74 20 69 3b 0a 20 20 70 49 6e 66 6f 2d 3e  int i;.  pInfo->
1d1fc 61 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 41 72  aCol = sqlite3Ar
1d1fd 72 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20 20 20  rayAllocate(.   
1d1fe 20 20 20 20 64 62 2c 0a 20 20 20 20 20 20 20 70      db,.       p
1d1ff 49 6e 66 6f 2d 3e 61 43 6f 6c 2c 0a 20 20 20 20  Info->aCol,.    
1d200 20 20 20 73 69 7a 65 6f 66 28 70 49 6e 66 6f 2d     sizeof(pInfo-
1d201 3e 61 43 6f 6c 5b 30 5d 29 2c 0a 20 20 20 20 20  >aCol[0]),.     
1d202 20 20 33 2c 0a 20 20 20 20 20 20 20 26 70 49 6e    3,.       &pIn
1d203 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 2c 0a 20 20 20  fo->nColumn,.   
1d204 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 43 6f 6c      &pInfo->nCol
1d205 75 6d 6e 41 6c 6c 6f 63 2c 0a 20 20 20 20 20 20  umnAlloc,.      
1d206 20 26 69 0a 20 20 29 3b 0a 20 20 72 65 74 75 72   &i.  );.  retur
1d207 6e 20 69 3b 0a 7d 20 20 20 20 0a 0a 2f 2a 0a 2a  n i;.}    ../*.*
1d208 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6c 65 6d  * Add a new elem
1d209 65 6e 74 20 74 6f 20 74 68 65 20 70 41 67 67 49  ent to the pAggI
1d20a 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d 20 61 72 72  nfo->aFunc[] arr
1d20b 61 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  ay.  Return the 
1d20c 69 6e 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20  index of.** the 
1d20d 6e 65 77 20 65 6c 65 6d 65 6e 74 2e 20 20 52 65  new element.  Re
1d20e 74 75 72 6e 20 61 20 6e 65 67 61 74 69 76 65 20  turn a negative 
1d20f 6e 75 6d 62 65 72 20 69 66 20 6d 61 6c 6c 6f 63  number if malloc
1d210 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69   fails..*/.stati
1d211 63 20 69 6e 74 20 61 64 64 41 67 67 49 6e 66 6f  c int addAggInfo
1d212 46 75 6e 63 28 73 71 6c 69 74 65 33 20 2a 64 62  Func(sqlite3 *db
1d213 2c 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66 6f  , AggInfo *pInfo
1d214 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 70 49  ){.  int i;.  pI
1d215 6e 66 6f 2d 3e 61 46 75 6e 63 20 3d 20 73 71 6c  nfo->aFunc = sql
1d216 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74  ite3ArrayAllocat
1d217 65 28 0a 20 20 20 20 20 20 20 64 62 2c 20 0a 20  e(.       db, . 
1d218 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 46 75        pInfo->aFu
1d219 6e 63 2c 0a 20 20 20 20 20 20 20 73 69 7a 65 6f  nc,.       sizeo
1d21a 66 28 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 30  f(pInfo->aFunc[0
1d21b 5d 29 2c 0a 20 20 20 20 20 20 20 33 2c 0a 20 20  ]),.       3,.  
1d21c 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 46 75       &pInfo->nFu
1d21d 6e 63 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66  nc,.       &pInf
1d21e 6f 2d 3e 6e 46 75 6e 63 41 6c 6c 6f 63 2c 0a 20  o->nFuncAlloc,. 
1d21f 20 20 20 20 20 20 26 69 0a 20 20 29 3b 0a 20 20        &i.  );.  
1d220 72 65 74 75 72 6e 20 69 3b 0a 7d 20 20 20 20 0a  return i;.}    .
1d221 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74  ./*.** This is t
1d222 68 65 20 78 45 78 70 72 43 61 6c 6c 62 61 63 6b  he xExprCallback
1d223 20 66 6f 72 20 61 20 74 72 65 65 20 77 61 6c 6b   for a tree walk
1d224 65 72 2e 20 20 49 74 20 69 73 20 75 73 65 64 20  er.  It is used 
1d225 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20  to.** implement 
1d226 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79  sqlite3ExprAnaly
1d227 7a 65 41 67 67 72 65 67 61 74 65 73 28 29 2e 20  zeAggregates(). 
1d228 20 53 65 65 20 73 71 6c 69 74 65 33 45 78 70 72   See sqlite3Expr
1d229 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65  AnalyzeAggregate
1d22a 73 0a 2a 2a 20 66 6f 72 20 61 64 64 69 74 69 6f  s.** for additio
1d22b 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
1d22c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61  .*/.static int a
1d22d 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 28  nalyzeAggregate(
1d22e 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c  Walker *pWalker,
1d22f 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20   Expr *pExpr){. 
1d230 20 69 6e 74 20 69 3b 0a 20 20 4e 61 6d 65 43 6f   int i;.  NameCo
1d231 6e 74 65 78 74 20 2a 70 4e 43 20 3d 20 70 57 61  ntext *pNC = pWa
1d232 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20 50  lker->u.pNC;.  P
1d233 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70  arse *pParse = p
1d234 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 53 72  NC->pParse;.  Sr
1d235 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 20  cList *pSrcList 
1d236 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b  = pNC->pSrcList;
1d237 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67  .  AggInfo *pAgg
1d238 49 6e 66 6f 20 3d 20 70 4e 43 2d 3e 70 41 67 67  Info = pNC->pAgg
1d239 49 6e 66 6f 3b 0a 0a 20 20 73 77 69 74 63 68 28  Info;..  switch(
1d23a 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20   pExpr->op ){.  
1d23b 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f    case TK_AGG_CO
1d23c 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 73 65 20 54  LUMN:.    case T
1d23d 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20  K_COLUMN: {.    
1d23e 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70    testcase( pExp
1d23f 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f  r->op==TK_AGG_CO
1d240 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 74 65  LUMN );.      te
1d241 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f  stcase( pExpr->o
1d242 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a  p==TK_COLUMN );.
1d243 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74        /* Check t
1d244 6f 20 73 65 65 20 69 66 20 74 68 65 20 63 6f 6c  o see if the col
1d245 75 6d 6e 20 69 73 20 69 6e 20 6f 6e 65 20 6f 66  umn is in one of
1d246 20 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20 74   the tables in t
1d247 68 65 20 46 52 4f 4d 0a 20 20 20 20 20 20 2a 2a  he FROM.      **
1d248 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 61   clause of the a
1d249 67 67 72 65 67 61 74 65 20 71 75 65 72 79 20 2a  ggregate query *
1d24a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 53 72 63  /.      if( pSrc
1d24b 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20  List ){.        
1d24c 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
1d24d 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70 53 72  tem *pItem = pSr
1d24e 63 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20  cList->a;.      
1d24f 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 53 72    for(i=0; i<pSr
1d250 63 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  cList->nSrc; i++
1d251 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
1d252 20 20 20 20 20 20 73 74 72 75 63 74 20 41 67 67        struct Agg
1d253 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 3b 0a  Info_col *pCol;.
1d254 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 45            if( pE
1d255 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 70 49 74  xpr->iTable==pIt
1d256 65 6d 2d 3e 69 43 75 72 73 6f 72 20 29 7b 0a 20  em->iCursor ){. 
1d257 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
1d258 20 77 65 20 72 65 61 63 68 20 74 68 69 73 20 70   we reach this p
1d259 6f 69 6e 74 2c 20 69 74 20 6d 65 61 6e 73 20 74  oint, it means t
1d25a 68 61 74 20 70 45 78 70 72 20 72 65 66 65 72 73  hat pExpr refers
1d25b 20 74 6f 20 61 20 74 61 62 6c 65 0a 20 20 20 20   to a table.    
1d25c 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20          ** that 
1d25d 69 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  is in the FROM c
1d25e 6c 61 75 73 65 20 6f 66 20 74 68 65 20 61 67 67  lause of the agg
1d25f 72 65 67 61 74 65 20 71 75 65 72 79 2e 20 20 0a  regate query.  .
1d260 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 0a 20              **. 
1d261 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 4d 61             ** Ma
1d262 6b 65 20 61 6e 20 65 6e 74 72 79 20 66 6f 72 20  ke an entry for 
1d263 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 20 70 41  the column in pA
1d264 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 69  ggInfo->aCol[] i
1d265 66 20 74 68 65 72 65 0a 20 20 20 20 20 20 20 20  f there.        
1d266 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 61 6e      ** is not an
1d267 20 65 6e 74 72 79 20 74 68 65 72 65 20 61 6c 72   entry there alr
1d268 65 61 64 79 2e 0a 20 20 20 20 20 20 20 20 20 20  eady..          
1d269 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20    */.           
1d26a 20 69 6e 74 20 6b 3b 0a 20 20 20 20 20 20 20 20   int k;.        
1d26b 20 20 20 20 70 43 6f 6c 20 3d 20 70 41 67 67 49      pCol = pAggI
1d26c 6e 66 6f 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20 20  nfo->aCol;.     
1d26d 20 20 20 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20         for(k=0; 
1d26e 6b 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 43 6f 6c  k<pAggInfo->nCol
1d26f 75 6d 6e 3b 20 6b 2b 2b 2c 20 70 43 6f 6c 2b 2b  umn; k++, pCol++
1d270 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
1d271 20 69 66 28 20 70 43 6f 6c 2d 3e 69 54 61 62 6c   if( pCol->iTabl
1d272 65 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  e==pExpr->iTable
1d273 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 20   &&.            
1d274 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 43 6f 6c        pCol->iCol
1d275 75 6d 6e 3d 3d 70 45 78 70 72 2d 3e 69 43 6f 6c  umn==pExpr->iCol
1d276 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20  umn ){.         
1d277 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1d278 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
1d279 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1d27a 20 20 20 20 20 20 20 20 69 66 28 20 28 6b 3e 3d          if( (k>=
1d27b 70 41 67 67 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d  pAggInfo->nColum
1d27c 6e 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n).             
1d27d 26 26 20 28 6b 20 3d 20 61 64 64 41 67 67 49 6e  && (k = addAggIn
1d27e 66 6f 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2d  foColumn(pParse-
1d27f 3e 64 62 2c 20 70 41 67 67 49 6e 66 6f 29 29 3e  >db, pAggInfo))>
1d280 3d 30 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  =0 .            
1d281 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
1d282 20 70 43 6f 6c 20 3d 20 26 70 41 67 67 49 6e 66   pCol = &pAggInf
1d283 6f 2d 3e 61 43 6f 6c 5b 6b 5d 3b 0a 20 20 20 20  o->aCol[k];.    
1d284 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e            pCol->
1d285 70 54 61 62 20 3d 20 70 45 78 70 72 2d 3e 70 54  pTab = pExpr->pT
1d286 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ab;.            
1d287 20 20 70 43 6f 6c 2d 3e 69 54 61 62 6c 65 20 3d    pCol->iTable =
1d288 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3b 0a   pExpr->iTable;.
1d289 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43                pC
1d28a 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 70 45  ol->iColumn = pE
1d28b 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20  xpr->iColumn;.  
1d28c 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c              pCol
1d28d 2d 3e 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73  ->iMem = ++pPars
1d28e 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20  e->nMem;.       
1d28f 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f         pCol->iSo
1d290 72 74 65 72 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b  rterColumn = -1;
1d291 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
1d292 43 6f 6c 2d 3e 70 45 78 70 72 20 3d 20 70 45 78  Col->pExpr = pEx
1d293 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  pr;.            
1d294 20 20 69 66 28 20 70 41 67 67 49 6e 66 6f 2d 3e    if( pAggInfo->
1d295 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20  pGroupBy ){.    
1d296 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
1d297 6a 2c 20 6e 3b 0a 20 20 20 20 20 20 20 20 20 20  j, n;.          
1d298 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a        ExprList *
1d299 70 47 42 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e  pGB = pAggInfo->
1d29a 70 47 72 6f 75 70 42 79 3b 0a 20 20 20 20 20 20  pGroupBy;.      
1d29b 20 20 20 20 20 20 20 20 20 20 73 74 72 75 63 74            struct
1d29c 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
1d29d 70 54 65 72 6d 20 3d 20 70 47 42 2d 3e 61 3b 0a  pTerm = pGB->a;.
1d29e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d29f 6e 20 3d 20 70 47 42 2d 3e 6e 45 78 70 72 3b 0a  n = pGB->nExpr;.
1d2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d2a1 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 3b 20 6a 2b  for(j=0; j<n; j+
1d2a2 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20  +, pTerm++){.   
1d2a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45                 E
1d2a4 78 70 72 20 2a 70 45 20 3d 20 70 54 65 72 6d 2d  xpr *pE = pTerm-
1d2a5 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20  >pExpr;.        
1d2a6 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 45            if( pE
1d2a7 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20  ->op==TK_COLUMN 
1d2a8 26 26 20 70 45 2d 3e 69 54 61 62 6c 65 3d 3d 70  && pE->iTable==p
1d2a9 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 26 26 0a  Expr->iTable &&.
1d2aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d2ab 20 20 20 20 20 20 70 45 2d 3e 69 43 6f 6c 75 6d        pE->iColum
1d2ac 6e 3d 3d 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d  n==pExpr->iColum
1d2ad 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  n ){.           
1d2ae 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69           pCol->i
1d2af 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 20 3d 20 6a  SorterColumn = j
1d2b0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
1d2b1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1d2b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d                 }
1d2b3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1d2b4 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   }.             
1d2b5 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   }.             
1d2b6 20 69 66 28 20 70 43 6f 6c 2d 3e 69 53 6f 72 74   if( pCol->iSort
1d2b7 65 72 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20  erColumn<0 ){.  
1d2b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43                pC
1d2b9 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d  ol->iSorterColum
1d2ba 6e 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e 6e 53  n = pAggInfo->nS
1d2bb 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 2b 2b 3b 0a  ortingColumn++;.
1d2bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a                }.
1d2bd 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
1d2be 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1d2bf 72 65 20 69 73 20 6e 6f 77 20 61 6e 20 65 6e 74  re is now an ent
1d2c0 72 79 20 66 6f 72 20 70 45 78 70 72 20 69 6e 20  ry for pExpr in 
1d2c1 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d  pAggInfo->aCol[]
1d2c2 20 28 65 69 74 68 65 72 0a 20 20 20 20 20 20 20   (either.       
1d2c3 20 20 20 20 20 2a 2a 20 62 65 63 61 75 73 65 20       ** because 
1d2c4 69 74 20 77 61 73 20 74 68 65 72 65 20 62 65 66  it was there bef
1d2c5 6f 72 65 20 6f 72 20 62 65 63 61 75 73 65 20 77  ore or because w
1d2c6 65 20 6a 75 73 74 20 63 72 65 61 74 65 64 20 69  e just created i
1d2c7 74 29 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20  t)..            
1d2c8 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 70  ** Convert the p
1d2c9 45 78 70 72 20 74 6f 20 62 65 20 61 20 54 4b 5f  Expr to be a TK_
1d2ca 41 47 47 5f 43 4f 4c 55 4d 4e 20 72 65 66 65 72  AGG_COLUMN refer
1d2cb 72 69 6e 67 20 74 6f 20 74 68 61 74 0a 20 20 20  ring to that.   
1d2cc 20 20 20 20 20 20 20 20 20 2a 2a 20 70 41 67 67           ** pAgg
1d2cd 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 65 6e 74  Info->aCol[] ent
1d2ce 72 79 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20  ry..            
1d2cf 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  */.            p
1d2d0 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 20 3d  Expr->pAggInfo =
1d2d1 20 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20   pAggInfo;.     
1d2d2 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70         pExpr->op
1d2d3 20 3d 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e   = TK_AGG_COLUMN
1d2d4 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45  ;.            pE
1d2d5 78 70 72 2d 3e 69 41 67 67 20 3d 20 6b 3b 0a 20  xpr->iAgg = k;. 
1d2d6 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
1d2d7 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 20 2f 2a  ;.          } /*
1d2d8 20 65 6e 64 69 66 20 70 45 78 70 72 2d 3e 69 54   endif pExpr->iT
1d2d9 61 62 6c 65 3d 3d 70 49 74 65 6d 2d 3e 69 43 75  able==pItem->iCu
1d2da 72 73 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20  rsor */.        
1d2db 7d 20 2f 2a 20 65 6e 64 20 6c 6f 6f 70 20 6f 76  } /* end loop ov
1d2dc 65 72 20 70 53 72 63 4c 69 73 74 20 2a 2f 0a 20  er pSrcList */. 
1d2dd 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74       }.      ret
1d2de 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20  urn WRC_Prune;. 
1d2df 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
1d2e0 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b  _AGG_FUNCTION: {
1d2e1 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 4e  .      /* The pN
1d2e2 43 2d 3e 6e 44 65 70 74 68 3d 3d 30 20 74 65 73  C->nDepth==0 tes
1d2e3 74 20 63 61 75 73 65 73 20 61 67 67 72 65 67 61  t causes aggrega
1d2e4 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20  te functions in 
1d2e5 73 75 62 71 75 65 72 69 65 73 0a 20 20 20 20 20  subqueries.     
1d2e6 20 2a 2a 20 74 6f 20 62 65 20 69 67 6e 6f 72 65   ** to be ignore
1d2e7 64 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70  d */.      if( p
1d2e8 4e 43 2d 3e 6e 44 65 70 74 68 3d 3d 30 20 29 7b  NC->nDepth==0 ){
1d2e9 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63  .        /* Chec
1d2ea 6b 20 74 6f 20 73 65 65 20 69 66 20 70 45 78 70  k to see if pExp
1d2eb 72 20 69 73 20 61 20 64 75 70 6c 69 63 61 74 65  r is a duplicate
1d2ec 20 6f 66 20 61 6e 6f 74 68 65 72 20 61 67 67 72   of another aggr
1d2ed 65 67 61 74 65 20 0a 20 20 20 20 20 20 20 20 2a  egate .        *
1d2ee 2a 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20  * function that 
1d2ef 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68  is already in th
1d2f0 65 20 70 41 67 67 49 6e 66 6f 20 73 74 72 75 63  e pAggInfo struc
1d2f1 74 75 72 65 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ture.        */.
1d2f2 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20 41          struct A
1d2f3 67 67 49 6e 66 6f 5f 66 75 6e 63 20 2a 70 49 74  ggInfo_func *pIt
1d2f4 65 6d 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e 61  em = pAggInfo->a
1d2f5 46 75 6e 63 3b 0a 20 20 20 20 20 20 20 20 66 6f  Func;.        fo
1d2f6 72 28 69 3d 30 3b 20 69 3c 70 41 67 67 49 6e 66  r(i=0; i<pAggInf
1d2f7 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20 70  o->nFunc; i++, p
1d2f8 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Item++){.       
1d2f9 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78     if( sqlite3Ex
1d2fa 70 72 43 6f 6d 70 61 72 65 28 70 49 74 65 6d 2d  prCompare(pItem-
1d2fb 3e 70 45 78 70 72 2c 20 70 45 78 70 72 29 20 29  >pExpr, pExpr) )
1d2fc 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72  {.            br
1d2fd 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  eak;.          }
1d2fe 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1d2ff 20 20 20 69 66 28 20 69 3e 3d 70 41 67 67 49 6e     if( i>=pAggIn
1d300 66 6f 2d 3e 6e 46 75 6e 63 20 29 7b 0a 20 20 20  fo->nFunc ){.   
1d301 20 20 20 20 20 20 20 2f 2a 20 70 45 78 70 72 20         /* pExpr 
1d302 69 73 20 6f 72 69 67 69 6e 61 6c 2e 20 20 4d 61  is original.  Ma
1d303 6b 65 20 61 20 6e 65 77 20 65 6e 74 72 79 20 69  ke a new entry i
1d304 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e  n pAggInfo->aFun
1d305 63 5b 5d 0a 20 20 20 20 20 20 20 20 20 20 2a 2f  c[].          */
1d306 0a 20 20 20 20 20 20 20 20 20 20 75 38 20 65 6e  .          u8 en
1d307 63 20 3d 20 45 4e 43 28 70 50 61 72 73 65 2d 3e  c = ENC(pParse->
1d308 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  db);.          i
1d309 20 3d 20 61 64 64 41 67 67 49 6e 66 6f 46 75 6e   = addAggInfoFun
1d30a 63 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 41  c(pParse->db, pA
1d30b 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20  ggInfo);.       
1d30c 20 20 20 69 66 28 20 69 3e 3d 30 20 29 7b 0a 20     if( i>=0 ){. 
1d30d 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d             pItem
1d30e 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61 46   = &pAggInfo->aF
1d30f 75 6e 63 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20  unc[i];.        
1d310 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72      pItem->pExpr
1d311 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20   = pExpr;.      
1d312 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 4d 65        pItem->iMe
1d313 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  m = ++pParse->nM
1d314 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  em;.            
1d315 70 49 74 65 6d 2d 3e 70 46 75 6e 63 20 3d 20 73  pItem->pFunc = s
1d316 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69  qlite3FindFuncti
1d317 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 0a 20  on(pParse->db,. 
1d318 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d319 20 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e    (char*)pExpr->
1d31a 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e  token.z, pExpr->
1d31b 74 6f 6b 65 6e 2e 6e 2c 0a 20 20 20 20 20 20 20  token.n,.       
1d31c 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70              pExp
1d31d 72 2d 3e 70 4c 69 73 74 20 3f 20 70 45 78 70 72  r->pList ? pExpr
1d31e 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a  ->pList->nExpr :
1d31f 20 30 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20   0, enc, 0);.   
1d320 20 20 20 20 20 20 20 20 20 69 66 28 20 70 45 78           if( pEx
1d321 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 44  pr->flags & EP_D
1d322 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 20  istinct ){.     
1d323 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e           pItem->
1d324 69 44 69 73 74 69 6e 63 74 20 3d 20 70 50 61 72  iDistinct = pPar
1d325 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20  se->nTab++;.    
1d326 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
1d327 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74               pIt
1d328 65 6d 2d 3e 69 44 69 73 74 69 6e 63 74 20 3d 20  em->iDistinct = 
1d329 2d 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  -1;.            
1d32a 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  }.          }.  
1d32b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1d32c 2f 2a 20 4d 61 6b 65 20 70 45 78 70 72 20 70 6f  /* Make pExpr po
1d32d 69 6e 74 20 74 6f 20 74 68 65 20 61 70 70 72 6f  int to the appro
1d32e 70 72 69 61 74 65 20 70 41 67 67 49 6e 66 6f 2d  priate pAggInfo-
1d32f 3e 61 46 75 6e 63 5b 5d 20 65 6e 74 72 79 0a 20  >aFunc[] entry. 
1d330 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
1d331 20 20 70 45 78 70 72 2d 3e 69 41 67 67 20 3d 20    pExpr->iAgg = 
1d332 69 3b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72  i;.        pExpr
1d333 2d 3e 70 41 67 67 49 6e 66 6f 20 3d 20 70 41 67  ->pAggInfo = pAg
1d334 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 72  gInfo;.        r
1d335 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b  eturn WRC_Prune;
1d336 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1d337 20 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f   }.  return WRC_
1d338 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 73 74 61 74  Continue;.}.stat
1d339 69 63 20 69 6e 74 20 61 6e 61 6c 79 7a 65 41 67  ic int analyzeAg
1d33a 67 72 65 67 61 74 65 73 49 6e 53 65 6c 65 63 74  gregatesInSelect
1d33b 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72  (Walker *pWalker
1d33c 2c 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63  , Select *pSelec
1d33d 74 29 7b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78  t){.  NameContex
1d33e 74 20 2a 70 4e 43 20 3d 20 70 57 61 6c 6b 65 72  t *pNC = pWalker
1d33f 2d 3e 75 2e 70 4e 43 3b 0a 20 20 69 66 28 20 70  ->u.pNC;.  if( p
1d340 4e 43 2d 3e 6e 44 65 70 74 68 3d 3d 30 20 29 7b  NC->nDepth==0 ){
1d341 0a 20 20 20 20 70 4e 43 2d 3e 6e 44 65 70 74 68  .    pNC->nDepth
1d342 2b 2b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 57  ++;.    sqlite3W
1d343 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65  alkSelect(pWalke
1d344 72 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20  r, pSelect);.   
1d345 20 70 4e 43 2d 3e 6e 44 65 70 74 68 2d 2d 3b 0a   pNC->nDepth--;.
1d346 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50      return WRC_P
1d347 72 75 6e 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  rune;.  }else{. 
1d348 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f     return WRC_Co
1d349 6e 74 69 6e 75 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ntinue;.  }.}../
1d34a 2a 0a 2a 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65  *.** Analyze the
1d34b 20 67 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f   given expressio
1d34c 6e 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 67  n looking for ag
1d34d 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
1d34e 73 20 61 6e 64 0a 2a 2a 20 66 6f 72 20 76 61 72  s and.** for var
1d34f 69 61 62 6c 65 73 20 74 68 61 74 20 6e 65 65 64  iables that need
1d350 20 74 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20   to be added to 
1d351 74 68 65 20 70 50 61 72 73 65 2d 3e 61 41 67 67  the pParse->aAgg
1d352 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a 20 4d 61 6b  [] array..** Mak
1d353 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 65 6e 74  e additional ent
1d354 72 69 65 73 20 74 6f 20 74 68 65 20 70 50 61 72  ries to the pPar
1d355 73 65 2d 3e 61 41 67 67 5b 5d 20 61 72 72 61 79  se->aAgg[] array
1d356 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a   as necessary..*
1d357 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1d358 65 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65  e should only be
1d359 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 74 68   called after th
1d35a 65 20 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73  e expression has
1d35b 20 62 65 65 6e 0a 2a 2a 20 61 6e 61 6c 79 7a 65   been.** analyze
1d35c 64 20 62 79 20 73 71 6c 69 74 65 33 52 65 73 6f  d by sqlite3Reso
1d35d 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29 2e 0a  lveExprNames()..
1d35e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1d35f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
1d360 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61  prAnalyzeAggrega
1d361 74 65 73 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20  tes(NameContext 
1d362 2a 70 4e 43 2c 20 45 78 70 72 20 2a 70 45 78 70  *pNC, Expr *pExp
1d363 72 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a  r){.  Walker w;.
1d364 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63    w.xExprCallbac
1d365 6b 20 3d 20 61 6e 61 6c 79 7a 65 41 67 67 72 65  k = analyzeAggre
1d366 67 61 74 65 3b 0a 20 20 77 2e 78 53 65 6c 65 63  gate;.  w.xSelec
1d367 74 43 61 6c 6c 62 61 63 6b 20 3d 20 61 6e 61 6c  tCallback = anal
1d368 79 7a 65 41 67 67 72 65 67 61 74 65 73 49 6e 53  yzeAggregatesInS
1d369 65 6c 65 63 74 3b 0a 20 20 77 2e 75 2e 70 4e 43  elect;.  w.u.pNC
1d36a 20 3d 20 70 4e 43 3b 0a 20 20 73 71 6c 69 74 65   = pNC;.  sqlite
1d36b 33 57 61 6c 6b 45 78 70 72 28 26 77 2c 20 70 45  3WalkExpr(&w, pE
1d36c 78 70 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  xpr);.}../*.** C
1d36d 61 6c 6c 20 73 71 6c 69 74 65 33 45 78 70 72 41  all sqlite3ExprA
1d36e 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73  nalyzeAggregates
1d36f 28 29 20 66 6f 72 20 65 76 65 72 79 20 65 78 70  () for every exp
1d370 72 65 73 73 69 6f 6e 20 69 6e 20 61 6e 0a 2a 2a  ression in an.**
1d371 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
1d372 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  .  Return the nu
1d373 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 0a  mber of errors..
1d374 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f  **.** If an erro
1d375 72 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 65 20  r is found, the 
1d376 61 6e 61 6c 79 73 69 73 20 69 73 20 63 75 74 20  analysis is cut 
1d377 73 68 6f 72 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  short..*/.SQLITE
1d378 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1d379 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65  lite3ExprAnalyze
1d37a 41 67 67 4c 69 73 74 28 4e 61 6d 65 43 6f 6e 74  AggList(NameCont
1d37b 65 78 74 20 2a 70 4e 43 2c 20 45 78 70 72 4c 69  ext *pNC, ExprLi
1d37c 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 73 74  st *pList){.  st
1d37d 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
1d37e 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74  em *pItem;.  int
1d37f 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 20   i;.  if( pList 
1d380 29 7b 0a 20 20 20 20 66 6f 72 28 70 49 74 65 6d  ){.    for(pItem
1d381 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20  =pList->a, i=0; 
1d382 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  i<pList->nExpr; 
1d383 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i++, pItem++){. 
1d384 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1d385 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65  AnalyzeAggregate
1d386 73 28 70 4e 43 2c 20 70 49 74 65 6d 2d 3e 70 45  s(pNC, pItem->pE
1d387 78 70 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  xpr);.    }.  }.
1d388 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  }../*.** Allocat
1d389 65 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 20  e or deallocate 
1d38a 74 65 6d 70 6f 72 61 72 79 20 75 73 65 20 72 65  temporary use re
1d38b 67 69 73 74 65 72 73 20 64 75 72 69 6e 67 20 63  gisters during c
1d38c 6f 64 65 20 67 65 6e 65 72 61 74 69 6f 6e 2e 0a  ode generation..
1d38d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1d38e 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74  E int sqlite3Get
1d38f 54 65 6d 70 52 65 67 28 50 61 72 73 65 20 2a 70  TempReg(Parse *p
1d390 50 61 72 73 65 29 7b 0a 20 20 69 66 28 20 70 50  Parse){.  if( pP
1d391 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 3d 3d  arse->nTempReg==
1d392 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
1d393 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
1d394 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 50 61    }.  return pPa
1d395 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 5b 2d 2d  rse->aTempReg[--
1d396 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67  pParse->nTempReg
1d397 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  ];.}.SQLITE_PRIV
1d398 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1d399 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 50  ReleaseTempReg(P
1d39a 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
1d39b 74 20 69 52 65 67 29 7b 0a 20 20 69 66 28 20 69  t iReg){.  if( i
1d39c 52 65 67 20 26 26 20 70 50 61 72 73 65 2d 3e 6e  Reg && pParse->n
1d39d 54 65 6d 70 52 65 67 3c 41 72 72 61 79 53 69 7a  TempReg<ArraySiz
1d39e 65 28 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 52  e(pParse->aTempR
1d39f 65 67 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  eg) ){.    sqlit
1d3a0 65 33 45 78 70 72 57 72 69 74 61 62 6c 65 52 65  e3ExprWritableRe
1d3a1 67 69 73 74 65 72 28 70 50 61 72 73 65 2c 20 69  gister(pParse, i
1d3a2 52 65 67 2c 20 69 52 65 67 29 3b 0a 20 20 20 20  Reg, iReg);.    
1d3a3 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67  pParse->aTempReg
1d3a4 5b 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65  [pParse->nTempRe
1d3a5 67 2b 2b 5d 20 3d 20 69 52 65 67 3b 0a 20 20 7d  g++] = iReg;.  }
1d3a6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  .}../*.** Alloca
1d3a7 74 65 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65  te or deallocate
1d3a8 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6e 52 65 67   a block of nReg
1d3a9 20 63 6f 6e 73 65 63 75 74 69 76 65 20 72 65 67   consecutive reg
1d3aa 69 73 74 65 72 73 0a 2a 2f 0a 53 51 4c 49 54 45  isters.*/.SQLITE
1d3ab 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1d3ac 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65  ite3GetTempRange
1d3ad 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1d3ae 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74  int nReg){.  int
1d3af 20 69 2c 20 6e 3b 0a 20 20 69 20 3d 20 70 50 61   i, n;.  i = pPa
1d3b0 72 73 65 2d 3e 69 52 61 6e 67 65 52 65 67 3b 0a  rse->iRangeReg;.
1d3b1 20 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 6e 52    n = pParse->nR
1d3b2 61 6e 67 65 52 65 67 3b 0a 20 20 69 66 28 20 6e  angeReg;.  if( n
1d3b3 52 65 67 3c 3d 6e 20 26 26 20 21 75 73 65 64 41  Reg<=n && !usedA
1d3b4 73 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50 61  sColumnCache(pPa
1d3b5 72 73 65 2c 20 69 2c 20 69 2b 6e 2d 31 29 20 29  rse, i, i+n-1) )
1d3b6 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 69 52  {.    pParse->iR
1d3b7 61 6e 67 65 52 65 67 20 2b 3d 20 6e 52 65 67 3b  angeReg += nReg;
1d3b8 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 52 61  .    pParse->nRa
1d3b9 6e 67 65 52 65 67 20 2d 3d 20 6e 52 65 67 3b 0a  ngeReg -= nReg;.
1d3ba 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 20 3d    }else{.    i =
1d3bb 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b   pParse->nMem+1;
1d3bc 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65  .    pParse->nMe
1d3bd 6d 20 2b 3d 20 6e 52 65 67 3b 0a 20 20 7d 0a 20  m += nReg;.  }. 
1d3be 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51 4c   return i;.}.SQL
1d3bf 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1d3c0 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
1d3c1 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65 20 2a  empRange(Parse *
1d3c2 70 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67  pParse, int iReg
1d3c3 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69  , int nReg){.  i
1d3c4 66 28 20 6e 52 65 67 3e 70 50 61 72 73 65 2d 3e  f( nReg>pParse->
1d3c5 6e 52 61 6e 67 65 52 65 67 20 29 7b 0a 20 20 20  nRangeReg ){.   
1d3c6 20 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52   pParse->nRangeR
1d3c7 65 67 20 3d 20 6e 52 65 67 3b 0a 20 20 20 20 70  eg = nReg;.    p
1d3c8 50 61 72 73 65 2d 3e 69 52 61 6e 67 65 52 65 67  Parse->iRangeReg
1d3c9 20 3d 20 69 52 65 67 3b 0a 20 20 7d 0a 7d 0a 0a   = iReg;.  }.}..
1d3ca 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1d3cb 45 6e 64 20 6f 66 20 65 78 70 72 2e 63 20 2a 2a  End of expr.c **
1d3cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d3cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d3ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1d3cf 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1d3d0 42 65 67 69 6e 20 66 69 6c 65 20 61 6c 74 65 72  Begin file alter
1d3d1 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1d3d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d3d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1d3d4 2f 2a 0a 2a 2a 20 32 30 30 35 20 46 65 62 72 75  /*.** 2005 Febru
1d3d5 61 72 79 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65  ary 15.**.** The
1d3d6 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
1d3d7 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
1d3d8 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
1d3d9 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
1d3da 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
1d3db 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
1d3dc 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
1d3dd 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
1d3de 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
1d3df 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
1d3e0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
1d3e1 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
1d3e2 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
1d3e3 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
1d3e4 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
1d3e5 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
1d3e6 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
1d3e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d3e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d3e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d3ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d3eb 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
1d3ec 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f  le contains C co
1d3ed 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74  de routines that
1d3ee 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74   used to generat
1d3ef 65 20 56 44 42 45 20 63 6f 64 65 0a 2a 2a 20 74  e VDBE code.** t
1d3f0 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  hat implements t
1d3f1 68 65 20 41 4c 54 45 52 20 54 41 42 4c 45 20 63  he ALTER TABLE c
1d3f2 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 24 49  ommand..**.** $I
1d3f3 64 3a 20 61 6c 74 65 72 2e 63 2c 76 20 31 2e 34  d: alter.c,v 1.4
1d3f4 39 20 32 30 30 38 2f 31 30 2f 33 30 20 31 37 3a  9 2008/10/30 17:
1d3f5 32 31 3a 31 33 20 64 61 6e 69 65 6c 6b 31 39 37  21:13 danielk197
1d3f6 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  7 Exp $.*/../*.*
1d3f7 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68  * The code in th
1d3f8 69 73 20 66 69 6c 65 20 6f 6e 6c 79 20 65 78 69  is file only exi
1d3f9 73 74 73 20 69 66 20 77 65 20 61 72 65 20 6e 6f  sts if we are no
1d3fa 74 20 6f 6d 69 74 74 69 6e 67 20 74 68 65 0a 2a  t omitting the.*
1d3fb 2a 20 41 4c 54 45 52 20 54 41 42 4c 45 20 6c 6f  * ALTER TABLE lo
1d3fc 67 69 63 20 66 72 6f 6d 20 74 68 65 20 62 75 69  gic from the bui
1d3fd 6c 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ld..*/.#ifndef S
1d3fe 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52  QLITE_OMIT_ALTER
1d3ff 54 41 42 4c 45 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  TABLE.../*.** Th
1d400 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
1d401 73 65 64 20 62 79 20 53 51 4c 20 67 65 6e 65 72  sed by SQL gener
1d402 61 74 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  ated to implemen
1d403 74 20 74 68 65 20 0a 2a 2a 20 41 4c 54 45 52 20  t the .** ALTER 
1d404 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e 20 54  TABLE command. T
1d405 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
1d406 74 20 69 73 20 74 68 65 20 74 65 78 74 20 6f 66  t is the text of
1d407 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20   a CREATE TABLE 
1d408 6f 72 0a 2a 2a 20 43 52 45 41 54 45 20 49 4e 44  or.** CREATE IND
1d409 45 58 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65 20  EX command. The 
1d40a 73 65 63 6f 6e 64 20 69 73 20 61 20 74 61 62 6c  second is a tabl
1d40b 65 20 6e 61 6d 65 2e 20 54 68 65 20 74 61 62 6c  e name. The tabl
1d40c 65 20 6e 61 6d 65 20 69 6e 20 0a 2a 2a 20 74 68  e name in .** th
1d40d 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f  e CREATE TABLE o
1d40e 72 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 73  r CREATE INDEX s
1d40f 74 61 74 65 6d 65 6e 74 20 69 73 20 72 65 70 6c  tatement is repl
1d410 61 63 65 64 20 77 69 74 68 20 74 68 65 20 74 68  aced with the th
1d411 69 72 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20  ird.** argument 
1d412 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 20 72  and the result r
1d413 65 74 75 72 6e 65 64 2e 20 45 78 61 6d 70 6c 65  eturned. Example
1d414 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 5f  s:.**.** sqlite_
1d415 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 27 43 52  rename_table('CR
1d416 45 41 54 45 20 54 41 42 4c 45 20 61 62 63 28 61  EATE TABLE abc(a
1d417 2c 20 62 2c 20 63 29 27 2c 20 27 64 65 66 27 29  , b, c)', 'def')
1d418 0a 2a 2a 20 20 20 20 20 2d 3e 20 27 43 52 45 41  .**     -> 'CREA
1d419 54 45 20 54 41 42 4c 45 20 64 65 66 28 61 2c 20  TE TABLE def(a, 
1d41a 62 2c 20 63 29 27 0a 2a 2a 0a 2a 2a 20 73 71 6c  b, c)'.**.** sql
1d41b 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65  ite_rename_table
1d41c 28 27 43 52 45 41 54 45 20 49 4e 44 45 58 20 69  ('CREATE INDEX i
1d41d 20 4f 4e 20 61 62 63 28 61 29 27 2c 20 27 64 65   ON abc(a)', 'de
1d41e 66 27 29 0a 2a 2a 20 20 20 20 20 2d 3e 20 27 43  f').**     -> 'C
1d41f 52 45 41 54 45 20 49 4e 44 45 58 20 69 20 4f 4e  REATE INDEX i ON
1d420 20 64 65 66 28 61 2c 20 62 2c 20 63 29 27 0a 2a   def(a, b, c)'.*
1d421 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  /.static void re
1d422 6e 61 6d 65 54 61 62 6c 65 46 75 6e 63 28 0a 20  nameTableFunc(. 
1d423 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1d424 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
1d425 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
1d426 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
1d427 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
1d428 20 63 6f 6e 73 74 20 2a 7a 53 71 6c 20 3d 20 73   const *zSql = s
1d429 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
1d42a 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 75 6e  t(argv[0]);.  un
1d42b 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73  signed char cons
1d42c 74 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 20 3d 20  t *zTableName = 
1d42d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1d42e 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 0a 20 20  xt(argv[1]);..  
1d42f 69 6e 74 20 74 6f 6b 65 6e 3b 0a 20 20 54 6f 6b  int token;.  Tok
1d430 65 6e 20 74 6e 61 6d 65 3b 0a 20 20 75 6e 73 69  en tname;.  unsi
1d431 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20  gned char const 
1d432 2a 7a 43 73 72 20 3d 20 7a 53 71 6c 3b 0a 20 20  *zCsr = zSql;.  
1d433 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 63  int len = 0;.  c
1d434 68 61 72 20 2a 7a 52 65 74 3b 0a 0a 20 20 73 71  har *zRet;..  sq
1d435 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69  lite3 *db = sqli
1d436 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
1d437 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a  andle(context);.
1d438 0a 20 20 2f 2a 20 54 68 65 20 70 72 69 6e 63 69  .  /* The princi
1d439 70 6c 65 20 75 73 65 64 20 74 6f 20 6c 6f 63 61  ple used to loca
1d43a 74 65 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d  te the table nam
1d43b 65 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 20  e in the CREATE 
1d43c 54 41 42 4c 45 20 0a 20 20 2a 2a 20 73 74 61 74  TABLE .  ** stat
1d43d 65 6d 65 6e 74 20 69 73 20 74 68 61 74 20 74 68  ement is that th
1d43e 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20  e table name is 
1d43f 74 68 65 20 66 69 72 73 74 20 6e 6f 6e 2d 73 70  the first non-sp
1d440 61 63 65 20 74 6f 6b 65 6e 20 74 68 61 74 0a 20  ace token that. 
1d441 20 2a 2a 20 69 73 20 69 6d 6d 65 64 69 61 74 65   ** is immediate
1d442 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61  ly followed by a
1d443 20 54 4b 5f 4c 50 20 6f 72 20 54 4b 5f 55 53 49   TK_LP or TK_USI
1d444 4e 47 20 74 6f 6b 65 6e 2e 0a 20 20 2a 2f 0a 20  NG token..  */. 
1d445 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20   if( zSql ){.   
1d446 20 64 6f 20 7b 0a 20 20 20 20 20 20 69 66 28 20   do {.      if( 
1d447 21 2a 7a 43 73 72 20 29 7b 0a 20 20 20 20 20 20  !*zCsr ){.      
1d448 20 20 2f 2a 20 52 61 6e 20 6f 75 74 20 6f 66 20    /* Ran out of 
1d449 69 6e 70 75 74 20 62 65 66 6f 72 65 20 66 69 6e  input before fin
1d44a 64 69 6e 67 20 61 6e 20 6f 70 65 6e 69 6e 67 20  ding an opening 
1d44b 62 72 61 63 6b 65 74 2e 20 52 65 74 75 72 6e 20  bracket. Return 
1d44c 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 20 20 20 20 20  NULL. */.       
1d44d 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
1d44e 0a 0a 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65  ..      /* Store
1d44f 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20   the token that 
1d450 7a 43 73 72 20 70 6f 69 6e 74 73 20 74 6f 20 69  zCsr points to i
1d451 6e 20 74 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20  n tname. */.    
1d452 20 20 74 6e 61 6d 65 2e 7a 20 3d 20 7a 43 73 72    tname.z = zCsr
1d453 3b 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e 6e 20  ;.      tname.n 
1d454 3d 20 6c 65 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a  = len;..      /*
1d455 20 41 64 76 61 6e 63 65 20 7a 43 73 72 20 74 6f   Advance zCsr to
1d456 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 2e   the next token.
1d457 20 53 74 6f 72 65 20 74 68 61 74 20 74 6f 6b 65   Store that toke
1d458 6e 20 74 79 70 65 20 69 6e 20 27 74 6f 6b 65 6e  n type in 'token
1d459 27 2c 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20  ',.      ** and 
1d45a 69 74 73 20 6c 65 6e 67 74 68 20 69 6e 20 27 6c  its length in 'l
1d45b 65 6e 27 20 28 74 6f 20 62 65 20 75 73 65 64 20  en' (to be used 
1d45c 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f  next iteration o
1d45d 66 20 74 68 69 73 20 6c 6f 6f 70 29 2e 0a 20 20  f this loop)..  
1d45e 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f 20      */.      do 
1d45f 7b 0a 20 20 20 20 20 20 20 20 7a 43 73 72 20 2b  {.        zCsr +
1d460 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 6c  = len;.        l
1d461 65 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  en = sqlite3GetT
1d462 6f 6b 65 6e 28 7a 43 73 72 2c 20 26 74 6f 6b 65  oken(zCsr, &toke
1d463 6e 29 3b 0a 20 20 20 20 20 20 7d 20 77 68 69 6c  n);.      } whil
1d464 65 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 53 50 41  e( token==TK_SPA
1d465 43 45 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  CE );.      asse
1d466 72 74 28 20 6c 65 6e 3e 30 20 29 3b 0a 20 20 20  rt( len>0 );.   
1d467 20 7d 20 77 68 69 6c 65 28 20 74 6f 6b 65 6e 21   } while( token!
1d468 3d 54 4b 5f 4c 50 20 26 26 20 74 6f 6b 65 6e 21  =TK_LP && token!
1d469 3d 54 4b 5f 55 53 49 4e 47 20 29 3b 0a 0a 20 20  =TK_USING );..  
1d46a 20 20 7a 52 65 74 20 3d 20 73 71 6c 69 74 65 33    zRet = sqlite3
1d46b 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 2e 2a  MPrintf(db, "%.*
1d46c 73 5c 22 25 77 5c 22 25 73 22 2c 20 74 6e 61 6d  s\"%w\"%s", tnam
1d46d 65 2e 7a 20 2d 20 7a 53 71 6c 2c 20 7a 53 71 6c  e.z - zSql, zSql
1d46e 2c 20 0a 20 20 20 20 20 20 20 7a 54 61 62 6c 65  , .       zTable
1d46f 4e 61 6d 65 2c 20 74 6e 61 6d 65 2e 7a 2b 74 6e  Name, tname.z+tn
1d470 61 6d 65 2e 6e 29 3b 0a 20 20 20 20 73 71 6c 69  ame.n);.    sqli
1d471 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
1d472 63 6f 6e 74 65 78 74 2c 20 7a 52 65 74 2c 20 2d  context, zRet, -
1d473 31 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49  1, SQLITE_DYNAMI
1d474 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64  C);.  }.}..#ifnd
1d475 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
1d476 52 49 47 47 45 52 0a 2f 2a 20 54 68 69 73 20 66  RIGGER./* This f
1d477 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
1d478 62 79 20 53 51 4c 20 67 65 6e 65 72 61 74 65 64  by SQL generated
1d479 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
1d47a 65 0a 2a 2a 20 41 4c 54 45 52 20 54 41 42 4c 45  e.** ALTER TABLE
1d47b 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65 20 66 69   command. The fi
1d47c 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20  rst argument is 
1d47d 74 68 65 20 74 65 78 74 20 6f 66 20 61 20 43 52  the text of a CR
1d47e 45 41 54 45 20 54 52 49 47 47 45 52 20 0a 2a 2a  EATE TRIGGER .**
1d47f 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 20   statement. The 
1d480 73 65 63 6f 6e 64 20 69 73 20 61 20 74 61 62 6c  second is a tabl
1d481 65 20 6e 61 6d 65 2e 20 54 68 65 20 74 61 62 6c  e name. The tabl
1d482 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 43 52  e name in the CR
1d483 45 41 54 45 20 0a 2a 2a 20 54 52 49 47 47 45 52  EATE .** TRIGGER
1d484 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 72 65   statement is re
1d485 70 6c 61 63 65 64 20 77 69 74 68 20 74 68 65 20  placed with the 
1d486 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 61  third argument a
1d487 6e 64 20 74 68 65 20 72 65 73 75 6c 74 20 0a 2a  nd the result .*
1d488 2a 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73  * returned. This
1d489 20 69 73 20 61 6e 61 6c 61 67 6f 75 73 20 74 6f   is analagous to
1d48a 20 72 65 6e 61 6d 65 54 61 62 6c 65 46 75 6e 63   renameTableFunc
1d48b 28 29 20 61 62 6f 76 65 2c 20 65 78 63 65 70 74  () above, except
1d48c 20 66 6f 72 20 43 52 45 41 54 45 0a 2a 2a 20 54   for CREATE.** T
1d48d 52 49 47 47 45 52 2c 20 6e 6f 74 20 43 52 45 41  RIGGER, not CREA
1d48e 54 45 20 49 4e 44 45 58 20 61 6e 64 20 43 52 45  TE INDEX and CRE
1d48f 41 54 45 20 54 41 42 4c 45 2e 0a 2a 2f 0a 73 74  ATE TABLE..*/.st
1d490 61 74 69 63 20 76 6f 69 64 20 72 65 6e 61 6d 65  atic void rename
1d491 54 72 69 67 67 65 72 46 75 6e 63 28 0a 20 20 73  TriggerFunc(.  s
1d492 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1d493 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
1d494 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
1d495 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
1d496 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63   unsigned char c
1d497 6f 6e 73 74 20 2a 7a 53 71 6c 20 3d 20 73 71 6c  onst *zSql = sql
1d498 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
1d499 61 72 67 76 5b 30 5d 29 3b 0a 20 20 75 6e 73 69  argv[0]);.  unsi
1d49a 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20  gned char const 
1d49b 2a 7a 54 61 62 6c 65 4e 61 6d 65 20 3d 20 73 71  *zTableName = sq
1d49c 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1d49d 28 61 72 67 76 5b 31 5d 29 3b 0a 0a 20 20 69 6e  (argv[1]);..  in
1d49e 74 20 74 6f 6b 65 6e 3b 0a 20 20 54 6f 6b 65 6e  t token;.  Token
1d49f 20 74 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 64 69   tname;.  int di
1d4a0 73 74 20 3d 20 33 3b 0a 20 20 75 6e 73 69 67 6e  st = 3;.  unsign
1d4a1 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a  ed char const *z
1d4a2 43 73 72 20 3d 20 7a 53 71 6c 3b 0a 20 20 69 6e  Csr = zSql;.  in
1d4a3 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 63 68 61  t len = 0;.  cha
1d4a4 72 20 2a 7a 52 65 74 3b 0a 0a 20 20 73 71 6c 69  r *zRet;..  sqli
1d4a5 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65  te3 *db = sqlite
1d4a6 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
1d4a7 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20  dle(context);.. 
1d4a8 20 2f 2a 20 54 68 65 20 70 72 69 6e 63 69 70 6c   /* The principl
1d4a9 65 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65  e used to locate
1d4aa 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20   the table name 
1d4ab 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 52  in the CREATE TR
1d4ac 49 47 47 45 52 20 0a 20 20 2a 2a 20 73 74 61 74  IGGER .  ** stat
1d4ad 65 6d 65 6e 74 20 69 73 20 74 68 61 74 20 74 68  ement is that th
1d4ae 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20  e table name is 
1d4af 74 68 65 20 66 69 72 73 74 20 74 6f 6b 65 6e 20  the first token 
1d4b0 74 68 61 74 20 69 73 20 69 6d 6d 65 64 69 61 74  that is immediat
1d4b1 65 64 6c 79 0a 20 20 2a 2a 20 70 72 65 63 65 64  edly.  ** preced
1d4b2 65 64 20 62 79 20 65 69 74 68 65 72 20 54 4b 5f  ed by either TK_
1d4b3 4f 4e 20 6f 72 20 54 4b 5f 44 4f 54 20 61 6e 64  ON or TK_DOT and
1d4b4 20 69 6d 6d 65 64 69 61 74 65 64 6c 79 20 66 6f   immediatedly fo
1d4b5 6c 6c 6f 77 65 64 20 62 79 20 6f 6e 65 0a 20 20  llowed by one.  
1d4b6 2a 2a 20 6f 66 20 54 4b 5f 57 48 45 4e 2c 20 54  ** of TK_WHEN, T
1d4b7 4b 5f 42 45 47 49 4e 20 6f 72 20 54 4b 5f 46 4f  K_BEGIN or TK_FO
1d4b8 52 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 53  R..  */.  if( zS
1d4b9 71 6c 20 29 7b 0a 20 20 20 20 64 6f 20 7b 0a 0a  ql ){.    do {..
1d4ba 20 20 20 20 20 20 69 66 28 20 21 2a 7a 43 73 72        if( !*zCsr
1d4bb 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52   ){.        /* R
1d4bc 61 6e 20 6f 75 74 20 6f 66 20 69 6e 70 75 74 20  an out of input 
1d4bd 62 65 66 6f 72 65 20 66 69 6e 64 69 6e 67 20 74  before finding t
1d4be 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 52  he table name. R
1d4bf 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20  eturn NULL. */. 
1d4c0 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
1d4c1 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
1d4c2 20 53 74 6f 72 65 20 74 68 65 20 74 6f 6b 65 6e   Store the token
1d4c3 20 74 68 61 74 20 7a 43 73 72 20 70 6f 69 6e 74   that zCsr point
1d4c4 73 20 74 6f 20 69 6e 20 74 6e 61 6d 65 2e 20 2a  s to in tname. *
1d4c5 2f 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e 7a 20  /.      tname.z 
1d4c6 3d 20 7a 43 73 72 3b 0a 20 20 20 20 20 20 74 6e  = zCsr;.      tn
1d4c7 61 6d 65 2e 6e 20 3d 20 6c 65 6e 3b 0a 0a 20 20  ame.n = len;..  
1d4c8 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 7a      /* Advance z
1d4c9 43 73 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20  Csr to the next 
1d4ca 74 6f 6b 65 6e 2e 20 53 74 6f 72 65 20 74 68 61  token. Store tha
1d4cb 74 20 74 6f 6b 65 6e 20 74 79 70 65 20 69 6e 20  t token type in 
1d4cc 27 74 6f 6b 65 6e 27 2c 0a 20 20 20 20 20 20 2a  'token',.      *
1d4cd 2a 20 61 6e 64 20 69 74 73 20 6c 65 6e 67 74 68  * and its length
1d4ce 20 69 6e 20 27 6c 65 6e 27 20 28 74 6f 20 62 65   in 'len' (to be
1d4cf 20 75 73 65 64 20 6e 65 78 74 20 69 74 65 72 61   used next itera
1d4d0 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6c 6f 6f  tion of this loo
1d4d1 70 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  p)..      */.   
1d4d2 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20     do {.        
1d4d3 7a 43 73 72 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20  zCsr += len;.   
1d4d4 20 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74       len = sqlit
1d4d5 65 33 47 65 74 54 6f 6b 65 6e 28 7a 43 73 72 2c  e3GetToken(zCsr,
1d4d6 20 26 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20   &token);.      
1d4d7 7d 77 68 69 6c 65 28 20 74 6f 6b 65 6e 3d 3d 54  }while( token==T
1d4d8 4b 5f 53 50 41 43 45 20 29 3b 0a 20 20 20 20 20  K_SPACE );.     
1d4d9 20 61 73 73 65 72 74 28 20 6c 65 6e 3e 30 20 29   assert( len>0 )
1d4da 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 56 61 72 69  ;..      /* Vari
1d4db 61 62 6c 65 20 27 64 69 73 74 27 20 73 74 6f 72  able 'dist' stor
1d4dc 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  es the number of
1d4dd 20 74 6f 6b 65 6e 73 20 72 65 61 64 20 73 69 6e   tokens read sin
1d4de 63 65 20 74 68 65 20 6d 6f 73 74 0a 20 20 20 20  ce the most.    
1d4df 20 20 2a 2a 20 72 65 63 65 6e 74 20 54 4b 5f 44    ** recent TK_D
1d4e0 4f 54 20 6f 72 20 54 4b 5f 4f 4e 2e 20 54 68 69  OT or TK_ON. Thi
1d4e1 73 20 6d 65 61 6e 73 20 74 68 61 74 20 77 68 65  s means that whe
1d4e2 6e 20 61 20 57 48 45 4e 2c 20 46 4f 52 20 6f 72  n a WHEN, FOR or
1d4e3 20 42 45 47 49 4e 20 0a 20 20 20 20 20 20 2a 2a   BEGIN .      **
1d4e4 20 74 6f 6b 65 6e 20 69 73 20 72 65 61 64 20 61   token is read a
1d4e5 6e 64 20 27 64 69 73 74 27 20 65 71 75 61 6c 73  nd 'dist' equals
1d4e6 20 32 2c 20 74 68 65 20 63 6f 6e 64 69 74 69 6f   2, the conditio
1d4e7 6e 20 73 74 61 74 65 64 20 61 62 6f 76 65 0a 20  n stated above. 
1d4e8 20 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 6d 65       ** to be me
1d4e9 74 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  t..      **.    
1d4ea 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 4f    ** Note that O
1d4eb 4e 20 63 61 6e 6e 6f 74 20 62 65 20 61 20 64 61  N cannot be a da
1d4ec 74 61 62 61 73 65 2c 20 74 61 62 6c 65 20 6f 72  tabase, table or
1d4ed 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2c 20 73 6f   column name, so
1d4ee 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 72 65 20  .      ** there 
1d4ef 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 77 6f  is no need to wo
1d4f0 72 72 79 20 61 62 6f 75 74 20 73 79 6e 74 61 78  rry about syntax
1d4f1 20 6c 69 6b 65 20 0a 20 20 20 20 20 20 2a 2a 20   like .      ** 
1d4f2 22 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20  "CREATE TRIGGER 
1d4f3 2e 2e 2e 20 4f 4e 20 4f 4e 2e 4f 4e 20 42 45 47  ... ON ON.ON BEG
1d4f4 49 4e 20 2e 2e 2e 22 20 65 74 63 2e 0a 20 20 20  IN ..." etc..   
1d4f5 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 69 73 74     */.      dist
1d4f6 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 74 6f  ++;.      if( to
1d4f7 6b 65 6e 3d 3d 54 4b 5f 44 4f 54 20 7c 7c 20 74  ken==TK_DOT || t
1d4f8 6f 6b 65 6e 3d 3d 54 4b 5f 4f 4e 20 29 7b 0a 20  oken==TK_ON ){. 
1d4f9 20 20 20 20 20 20 20 64 69 73 74 20 3d 20 30 3b         dist = 0;
1d4fa 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 77  .      }.    } w
1d4fb 68 69 6c 65 28 20 64 69 73 74 21 3d 32 20 7c 7c  hile( dist!=2 ||
1d4fc 20 28 74 6f 6b 65 6e 21 3d 54 4b 5f 57 48 45 4e   (token!=TK_WHEN
1d4fd 20 26 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 46 4f   && token!=TK_FO
1d4fe 52 20 26 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 42  R && token!=TK_B
1d4ff 45 47 49 4e 29 20 29 3b 0a 0a 20 20 20 20 2f 2a  EGIN) );..    /*
1d500 20 56 61 72 69 61 62 6c 65 20 74 6e 61 6d 65 20   Variable tname 
1d501 6e 6f 77 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  now contains the
1d502 20 74 6f 6b 65 6e 20 74 68 61 74 20 69 73 20 74   token that is t
1d503 68 65 20 6f 6c 64 20 74 61 62 6c 65 2d 6e 61 6d  he old table-nam
1d504 65 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20  e.    ** in the 
1d505 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 73  CREATE TRIGGER s
1d506 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f  tatement..    */
1d507 0a 20 20 20 20 7a 52 65 74 20 3d 20 73 71 6c 69  .    zRet = sqli
1d508 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
1d509 25 2e 2a 73 5c 22 25 77 5c 22 25 73 22 2c 20 74  %.*s\"%w\"%s", t
1d50a 6e 61 6d 65 2e 7a 20 2d 20 7a 53 71 6c 2c 20 7a  name.z - zSql, z
1d50b 53 71 6c 2c 20 0a 20 20 20 20 20 20 20 7a 54 61  Sql, .       zTa
1d50c 62 6c 65 4e 61 6d 65 2c 20 74 6e 61 6d 65 2e 7a  bleName, tname.z
1d50d 2b 74 6e 61 6d 65 2e 6e 29 3b 0a 20 20 20 20 73  +tname.n);.    s
1d50e 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
1d50f 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 52 65 74  xt(context, zRet
1d510 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 44 59 4e  , -1, SQLITE_DYN
1d511 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  AMIC);.  }.}.#en
1d512 64 69 66 20 20 20 2f 2a 20 21 53 51 4c 49 54 45  dif   /* !SQLITE
1d513 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f  _OMIT_TRIGGER */
1d514 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  ../*.** Register
1d515 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69   built-in functi
1d516 6f 6e 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70  ons used to help
1d517 20 69 6d 70 6c 65 6d 65 6e 74 20 41 4c 54 45 52   implement ALTER
1d518 20 54 41 42 4c 45 0a 2a 2f 0a 53 51 4c 49 54 45   TABLE.*/.SQLITE
1d519 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1d51a 6c 69 74 65 33 41 6c 74 65 72 46 75 6e 63 74 69  lite3AlterFuncti
1d51b 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ons(sqlite3 *db)
1d51c 7b 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74  {.  sqlite3Creat
1d51d 65 46 75 6e 63 28 64 62 2c 20 22 73 71 6c 69 74  eFunc(db, "sqlit
1d51e 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 22 2c  e_rename_table",
1d51f 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   2, SQLITE_UTF8,
1d520 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   0,.            
1d521 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6e               ren
1d522 61 6d 65 54 61 62 6c 65 46 75 6e 63 2c 20 30 2c  ameTableFunc, 0,
1d523 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c   0);.#ifndef SQL
1d524 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
1d525 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65  .  sqlite3Create
1d526 46 75 6e 63 28 64 62 2c 20 22 73 71 6c 69 74 65  Func(db, "sqlite
1d527 5f 72 65 6e 61 6d 65 5f 74 72 69 67 67 65 72 22  _rename_trigger"
1d528 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  , 2, SQLITE_UTF8
1d529 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
1d52a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65                re
1d52b 6e 61 6d 65 54 72 69 67 67 65 72 46 75 6e 63 2c  nameTriggerFunc,
1d52c 20 30 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 7d   0, 0);.#endif.}
1d52d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
1d52e 20 74 68 65 20 74 65 78 74 20 6f 66 20 61 20 57   the text of a W
1d52f 48 45 52 45 20 65 78 70 72 65 73 73 69 6f 6e 20  HERE expression 
1d530 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 65  which can be use
1d531 64 20 74 6f 20 73 65 6c 65 63 74 20 61 6c 6c 0a  d to select all.
1d532 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 72 69  ** temporary tri
1d533 67 67 65 72 73 20 6f 6e 20 74 61 62 6c 65 20 70  ggers on table p
1d534 54 61 62 20 66 72 6f 6d 20 74 68 65 20 73 71 6c  Tab from the sql
1d535 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 20  ite_temp_master 
1d536 74 61 62 6c 65 2e 20 49 66 0a 2a 2a 20 74 61 62  table. If.** tab
1d537 6c 65 20 70 54 61 62 20 68 61 73 20 6e 6f 20 74  le pTab has no t
1d538 65 6d 70 6f 72 61 72 79 20 74 72 69 67 67 65 72  emporary trigger
1d539 73 2c 20 6f 72 20 69 73 20 69 74 73 65 6c 66 20  s, or is itself 
1d53a 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 0a 2a  stored in the .*
1d53b 2a 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61  * temporary data
1d53c 62 61 73 65 2c 20 4e 55 4c 4c 20 69 73 20 72 65  base, NULL is re
1d53d 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  turned..*/.stati
1d53e 63 20 63 68 61 72 20 2a 77 68 65 72 65 54 65 6d  c char *whereTem
1d53f 70 54 72 69 67 67 65 72 73 28 50 61 72 73 65 20  pTriggers(Parse 
1d540 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a  *pParse, Table *
1d541 70 54 61 62 29 7b 0a 20 20 54 72 69 67 67 65 72  pTab){.  Trigger
1d542 20 2a 70 54 72 69 67 3b 0a 20 20 63 68 61 72 20   *pTrig;.  char 
1d543 2a 7a 57 68 65 72 65 20 3d 20 30 3b 0a 20 20 63  *zWhere = 0;.  c
1d544 68 61 72 20 2a 74 6d 70 20 3d 20 30 3b 0a 20 20  har *tmp = 0;.  
1d545 63 6f 6e 73 74 20 53 63 68 65 6d 61 20 2a 70 54  const Schema *pT
1d546 65 6d 70 53 63 68 65 6d 61 20 3d 20 70 50 61 72  empSchema = pPar
1d547 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 31 5d 2e 70  se->db->aDb[1].p
1d548 53 63 68 65 6d 61 3b 20 2f 2a 20 54 65 6d 70 20  Schema; /* Temp 
1d549 64 62 20 73 63 68 65 6d 61 20 2a 2f 0a 0a 20 20  db schema */..  
1d54a 2f 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20  /* If the table 
1d54b 69 73 20 6e 6f 74 20 6c 6f 63 61 74 65 64 20 69  is not located i
1d54c 6e 20 74 68 65 20 74 65 6d 70 2d 64 62 20 28 69  n the temp-db (i
1d54d 6e 20 77 68 69 63 68 20 63 61 73 65 20 4e 55 4c  n which case NUL
1d54e 4c 20 69 73 20 0a 20 20 2a 2a 20 72 65 74 75 72  L is .  ** retur
1d54f 6e 65 64 2c 20 6c 6f 6f 70 20 74 68 72 6f 75 67  ned, loop throug
1d550 68 20 74 68 65 20 74 61 62 6c 65 73 20 6c 69 73  h the tables lis
1d551 74 20 6f 66 20 74 72 69 67 67 65 72 73 2e 20 46  t of triggers. F
1d552 6f 72 20 65 61 63 68 20 74 72 69 67 67 65 72 0a  or each trigger.
1d553 20 20 2a 2a 20 74 68 61 74 20 69 73 20 6e 6f 74    ** that is not
1d554 20 70 61 72 74 20 6f 66 20 74 68 65 20 74 65 6d   part of the tem
1d555 70 2d 64 62 20 73 63 68 65 6d 61 2c 20 61 64 64  p-db schema, add
1d556 20 61 20 63 6c 61 75 73 65 20 74 6f 20 74 68 65   a clause to the
1d557 20 57 48 45 52 45 20 0a 20 20 2a 2a 20 65 78 70   WHERE .  ** exp
1d558 72 65 73 73 69 6f 6e 20 62 65 69 6e 67 20 62 75  ression being bu
1d559 69 6c 74 20 75 70 20 69 6e 20 7a 57 68 65 72 65  ilt up in zWhere
1d55a 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61  ..  */.  if( pTa
1d55b 62 2d 3e 70 53 63 68 65 6d 61 21 3d 70 54 65 6d  b->pSchema!=pTem
1d55c 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 73  pSchema ){.    s
1d55d 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
1d55e 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 66 6f 72  rse->db;.    for
1d55f 28 20 70 54 72 69 67 3d 70 54 61 62 2d 3e 70 54  ( pTrig=pTab->pT
1d560 72 69 67 67 65 72 3b 20 70 54 72 69 67 3b 20 70  rigger; pTrig; p
1d561 54 72 69 67 3d 70 54 72 69 67 2d 3e 70 4e 65 78  Trig=pTrig->pNex
1d562 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  t ){.      if( p
1d563 54 72 69 67 2d 3e 70 53 63 68 65 6d 61 3d 3d 70  Trig->pSchema==p
1d564 54 65 6d 70 53 63 68 65 6d 61 20 29 7b 0a 20 20  TempSchema ){.  
1d565 20 20 20 20 20 20 69 66 28 20 21 7a 57 68 65 72        if( !zWher
1d566 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a  e ){.          z
1d567 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 4d  Where = sqlite3M
1d568 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 61 6d 65  Printf(db, "name
1d569 3d 25 51 22 2c 20 70 54 72 69 67 2d 3e 6e 61 6d  =%Q", pTrig->nam
1d56a 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  e);.        }els
1d56b 65 7b 0a 20 20 20 20 20 20 20 20 20 20 74 6d 70  e{.          tmp
1d56c 20 3d 20 7a 57 68 65 72 65 3b 0a 20 20 20 20 20   = zWhere;.     
1d56d 20 20 20 20 20 7a 57 68 65 72 65 20 3d 20 73 71       zWhere = sq
1d56e 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
1d56f 20 22 25 73 20 4f 52 20 6e 61 6d 65 3d 25 51 22   "%s OR name=%Q"
1d570 2c 20 7a 57 68 65 72 65 2c 20 70 54 72 69 67 2d  , zWhere, pTrig-
1d571 3e 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  >name);.        
1d572 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1d573 64 62 2c 20 74 6d 70 29 3b 0a 20 20 20 20 20 20  db, tmp);.      
1d574 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1d575 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a  }.  }.  return z
1d576 57 68 65 72 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Where;.}../*.** 
1d577 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
1d578 20 64 72 6f 70 20 61 6e 64 20 72 65 6c 6f 61 64   drop and reload
1d579 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65   the internal re
1d57a 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20  presentation of 
1d57b 74 61 62 6c 65 0a 2a 2a 20 70 54 61 62 20 66 72  table.** pTab fr
1d57c 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2c  om the database,
1d57d 20 69 6e 63 6c 75 64 69 6e 67 20 74 72 69 67 67   including trigg
1d57e 65 72 73 20 61 6e 64 20 74 65 6d 70 6f 72 61 72  ers and temporar
1d57f 79 20 74 72 69 67 67 65 72 73 2e 0a 2a 2a 20 41  y triggers..** A
1d580 72 67 75 6d 65 6e 74 20 7a 4e 61 6d 65 20 69 73  rgument zName is
1d581 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
1d582 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64 61   table in the da
1d583 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 61 74  tabase schema at
1d584 0a 2a 2a 20 74 68 65 20 74 69 6d 65 20 74 68 65  .** the time the
1d585 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20   generated code 
1d586 69 73 20 65 78 65 63 75 74 65 64 2e 20 54 68 69  is executed. Thi
1d587 73 20 63 61 6e 20 62 65 20 64 69 66 66 65 72 65  s can be differe
1d588 6e 74 20 66 72 6f 6d 0a 2a 2a 20 70 54 61 62 2d  nt from.** pTab-
1d589 3e 7a 4e 61 6d 65 20 69 66 20 74 68 69 73 20 66  >zName if this f
1d58a 75 6e 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67  unction is being
1d58b 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 64 65 20   called to code 
1d58c 70 61 72 74 20 6f 66 20 61 6e 20 0a 2a 2a 20 22  part of an .** "
1d58d 41 4c 54 45 52 20 54 41 42 4c 45 20 52 45 4e 41  ALTER TABLE RENA
1d58e 4d 45 20 54 4f 22 20 73 74 61 74 65 6d 65 6e 74  ME TO" statement
1d58f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1d590 20 72 65 6c 6f 61 64 54 61 62 6c 65 53 63 68 65   reloadTableSche
1d591 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ma(Parse *pParse
1d592 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 63  , Table *pTab, c
1d593 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
1d594 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  ){.  Vdbe *v;.  
1d595 63 68 61 72 20 2a 7a 57 68 65 72 65 3b 0a 20 20  char *zWhere;.  
1d596 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
1d597 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
1d598 64 65 78 20 6f 66 20 64 61 74 61 62 61 73 65 20  dex of database 
1d599 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61 62 20  containing pTab 
1d59a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
1d59b 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20  E_OMIT_TRIGGER. 
1d59c 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b   Trigger *pTrig;
1d59d 0a 23 65 6e 64 69 66 0a 0a 20 20 76 20 3d 20 73  .#endif..  v = s
1d59e 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
1d59f 61 72 73 65 29 3b 0a 20 20 69 66 28 20 21 76 20  arse);.  if( !v 
1d5a0 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65  ) return;.  asse
1d5a1 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
1d5a2 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28  HoldsAllMutexes(
1d5a3 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20  pParse->db) );. 
1d5a4 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
1d5a5 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72  hemaToIndex(pPar
1d5a6 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53  se->db, pTab->pS
1d5a7 63 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74  chema);.  assert
1d5a8 28 20 69 44 62 3e 3d 30 20 29 3b 0a 0a 23 69 66  ( iDb>=0 );..#if
1d5a9 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1d5aa 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 44 72  _TRIGGER.  /* Dr
1d5ab 6f 70 20 61 6e 79 20 74 61 62 6c 65 20 74 72 69  op any table tri
1d5ac 67 67 65 72 73 20 66 72 6f 6d 20 74 68 65 20 69  ggers from the i
1d5ad 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 2e 20  nternal schema. 
1d5ae 2a 2f 0a 20 20 66 6f 72 28 70 54 72 69 67 3d 70  */.  for(pTrig=p
1d5af 54 61 62 2d 3e 70 54 72 69 67 67 65 72 3b 20 70  Tab->pTrigger; p
1d5b0 54 72 69 67 3b 20 70 54 72 69 67 3d 70 54 72 69  Trig; pTrig=pTri
1d5b1 67 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69  g->pNext){.    i
1d5b2 6e 74 20 69 54 72 69 67 44 62 20 3d 20 73 71 6c  nt iTrigDb = sql
1d5b3 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
1d5b4 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  x(pParse->db, pT
1d5b5 72 69 67 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  rig->pSchema);. 
1d5b6 20 20 20 61 73 73 65 72 74 28 20 69 54 72 69 67     assert( iTrig
1d5b7 44 62 3d 3d 69 44 62 20 7c 7c 20 69 54 72 69 67  Db==iDb || iTrig
1d5b8 44 62 3d 3d 31 20 29 3b 0a 20 20 20 20 73 71 6c  Db==1 );.    sql
1d5b9 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
1d5ba 2c 20 4f 50 5f 44 72 6f 70 54 72 69 67 67 65 72  , OP_DropTrigger
1d5bb 2c 20 69 54 72 69 67 44 62 2c 20 30 2c 20 30 2c  , iTrigDb, 0, 0,
1d5bc 20 70 54 72 69 67 2d 3e 6e 61 6d 65 2c 20 30 29   pTrig->name, 0)
1d5bd 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
1d5be 2f 2a 20 44 72 6f 70 20 74 68 65 20 74 61 62 6c  /* Drop the tabl
1d5bf 65 20 61 6e 64 20 69 6e 64 65 78 20 66 72 6f 6d  e and index from
1d5c0 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 63   the internal sc
1d5c1 68 65 6d 61 20 2a 2f 0a 20 20 73 71 6c 69 74 65  hema */.  sqlite
1d5c2 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
1d5c3 50 5f 44 72 6f 70 54 61 62 6c 65 2c 20 69 44 62  P_DropTable, iDb
1d5c4 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e  , 0, 0, pTab->zN
1d5c5 61 6d 65 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 52  ame, 0);..  /* R
1d5c6 65 6c 6f 61 64 20 74 68 65 20 74 61 62 6c 65 2c  eload the table,
1d5c7 20 69 6e 64 65 78 20 61 6e 64 20 70 65 72 6d 61   index and perma
1d5c8 6e 65 6e 74 20 74 72 69 67 67 65 72 20 73 63 68  nent trigger sch
1d5c9 65 6d 61 73 2e 20 2a 2f 0a 20 20 7a 57 68 65 72  emas. */.  zWher
1d5ca 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  e = sqlite3MPrin
1d5cb 74 66 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 22  tf(pParse->db, "
1d5cc 74 62 6c 5f 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e  tbl_name=%Q", zN
1d5cd 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 7a 57 68  ame);.  if( !zWh
1d5ce 65 72 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ere ) return;.  
1d5cf 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1d5d0 34 28 76 2c 20 4f 50 5f 50 61 72 73 65 53 63 68  4(v, OP_ParseSch
1d5d1 65 6d 61 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20  ema, iDb, 0, 0, 
1d5d2 7a 57 68 65 72 65 2c 20 50 34 5f 44 59 4e 41 4d  zWhere, P4_DYNAM
1d5d3 49 43 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  IC);..#ifndef SQ
1d5d4 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
1d5d5 52 0a 20 20 2f 2a 20 4e 6f 77 2c 20 69 66 20 74  R.  /* Now, if t
1d5d6 68 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20  he table is not 
1d5d7 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 74 65  stored in the te
1d5d8 6d 70 20 64 61 74 61 62 61 73 65 2c 20 72 65 6c  mp database, rel
1d5d9 6f 61 64 20 61 6e 79 20 74 65 6d 70 20 0a 20 20  oad any temp .  
1d5da 2a 2a 20 74 72 69 67 67 65 72 73 2e 20 44 6f 6e  ** triggers. Don
1d5db 27 74 20 75 73 65 20 49 4e 28 2e 2e 2e 29 20 69  't use IN(...) i
1d5dc 6e 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4f 4d  n case SQLITE_OM
1d5dd 49 54 5f 53 55 42 51 55 45 52 59 20 69 73 20 64  IT_SUBQUERY is d
1d5de 65 66 69 6e 65 64 2e 20 0a 20 20 2a 2f 0a 20 20  efined. .  */.  
1d5df 69 66 28 20 28 7a 57 68 65 72 65 3d 77 68 65 72  if( (zWhere=wher
1d5e0 65 54 65 6d 70 54 72 69 67 67 65 72 73 28 70 50  eTempTriggers(pP
1d5e1 61 72 73 65 2c 20 70 54 61 62 29 29 21 3d 30 20  arse, pTab))!=0 
1d5e2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1d5e3 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50  beAddOp4(v, OP_P
1d5e4 61 72 73 65 53 63 68 65 6d 61 2c 20 31 2c 20 30  arseSchema, 1, 0
1d5e5 2c 20 30 2c 20 7a 57 68 65 72 65 2c 20 50 34 5f  , 0, zWhere, P4_
1d5e6 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 23 65  DYNAMIC);.  }.#e
1d5e7 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  ndif.}../*.** Ge
1d5e8 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 69  nerate code to i
1d5e9 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 22 41 4c  mplement the "AL
1d5ea 54 45 52 20 54 41 42 4c 45 20 78 78 78 20 52 45  TER TABLE xxx RE
1d5eb 4e 41 4d 45 20 54 4f 20 79 79 79 22 20 0a 2a 2a  NAME TO yyy" .**
1d5ec 20 63 6f 6d 6d 61 6e 64 2e 20 0a 2a 2f 0a 53 51   command. .*/.SQ
1d5ed 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1d5ee 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 52 65  d sqlite3AlterRe
1d5ef 6e 61 6d 65 54 61 62 6c 65 28 0a 20 20 50 61 72  nameTable(.  Par
1d5f0 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
1d5f1 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72         /* Parser
1d5f2 20 63 6f 6e 74 65 78 74 2e 20 2a 2f 0a 20 20 53   context. */.  S
1d5f3 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20  rcList *pSrc,   
1d5f4 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1d5f5 74 61 62 6c 65 20 74 6f 20 72 65 6e 61 6d 65 2e  table to rename.
1d5f6 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
1d5f7 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  me              
1d5f8 2f 2a 20 54 68 65 20 6e 65 77 20 74 61 62 6c 65  /* The new table
1d5f9 20 6e 61 6d 65 2e 20 2a 2f 0a 29 7b 0a 20 20 69   name. */.){.  i
1d5fa 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
1d5fb 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
1d5fc 62 61 73 65 20 74 68 61 74 20 63 6f 6e 74 61 69  base that contai
1d5fd 6e 73 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  ns the table */.
1d5fe 20 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20    char *zDb;    
1d5ff 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1d600 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20  ame of database 
1d601 69 44 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  iDb */.  Table *
1d602 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20  pTab;           
1d603 20 20 20 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e     /* Table bein
1d604 67 20 72 65 6e 61 6d 65 64 20 2a 2f 0a 20 20 63  g renamed */.  c
1d605 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20  har *zName = 0; 
1d606 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 55 4c 4c           /* NULL
1d607 2d 74 65 72 6d 69 6e 61 74 65 64 20 76 65 72 73  -terminated vers
1d608 69 6f 6e 20 6f 66 20 70 4e 61 6d 65 20 2a 2f 20  ion of pName */ 
1d609 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1d60a 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 2f 2a 20   pParse->db; /* 
1d60b 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  Database connect
1d60c 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 61  ion */.  int nTa
1d60d 62 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20  bName;          
1d60e 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1d60f 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 73  UTF-8 characters
1d610 20 69 6e 20 7a 54 61 62 4e 61 6d 65 20 2a 2f 0a   in zTabName */.
1d611 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
1d612 61 62 4e 61 6d 65 3b 20 20 20 20 20 2f 2a 20 4f  abName;     /* O
1d613 72 69 67 69 6e 61 6c 20 6e 61 6d 65 20 6f 66 20  riginal name of 
1d614 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 56  the table */.  V
1d615 64 62 65 20 2a 76 3b 0a 23 69 66 6e 64 65 66 20  dbe *v;.#ifndef 
1d616 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
1d617 47 45 52 0a 20 20 63 68 61 72 20 2a 7a 57 68 65  GER.  char *zWhe
1d618 72 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  re = 0;         
1d619 2f 2a 20 57 68 65 72 65 20 63 6c 61 75 73 65 20  /* Where clause 
1d61a 74 6f 20 6c 6f 63 61 74 65 20 74 65 6d 70 20 74  to locate temp t
1d61b 72 69 67 67 65 72 73 20 2a 2f 0a 23 65 6e 64 69  riggers */.#endi
1d61c 66 0a 20 20 69 6e 74 20 69 73 56 69 72 74 75 61  f.  int isVirtua
1d61d 6c 52 65 6e 61 6d 65 20 3d 20 30 3b 20 20 2f 2a  lRename = 0;  /*
1d61e 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73   True if this is
1d61f 20 61 20 76 2d 74 61 62 6c 65 20 77 69 74 68 20   a v-table with 
1d620 61 6e 20 78 52 65 6e 61 6d 65 28 29 20 2a 2f 0a  an xRename() */.
1d621 20 20 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c    .  if( db->mal
1d622 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f  locFailed ) goto
1d623 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62   exit_rename_tab
1d624 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53  le;.  assert( pS
1d625 72 63 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20  rc->nSrc==1 );. 
1d626 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1d627 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74  BtreeHoldsAllMut
1d628 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29  exes(pParse->db)
1d629 20 29 3b 0a 0a 20 20 70 54 61 62 20 3d 20 73 71   );..  pTab = sq
1d62a 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
1d62b 28 70 50 61 72 73 65 2c 20 30 2c 20 70 53 72 63  (pParse, 0, pSrc
1d62c 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 53  ->a[0].zName, pS
1d62d 72 63 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61  rc->a[0].zDataba
1d62e 73 65 29 3b 0a 20 20 69 66 28 20 21 70 54 61 62  se);.  if( !pTab
1d62f 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e   ) goto exit_ren
1d630 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 69 44 62  ame_table;.  iDb
1d631 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
1d632 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e  ToIndex(pParse->
1d633 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
1d634 61 29 3b 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e  a);.  zDb = db->
1d635 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a  aDb[iDb].zName;.
1d636 0a 20 20 2f 2a 20 47 65 74 20 61 20 4e 55 4c 4c  .  /* Get a NULL
1d637 20 74 65 72 6d 69 6e 61 74 65 64 20 76 65 72 73   terminated vers
1d638 69 6f 6e 20 6f 66 20 74 68 65 20 6e 65 77 20 74  ion of the new t
1d639 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20  able name. */.  
1d63a 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e  zName = sqlite3N
1d63b 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
1d63c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21   pName);.  if( !
1d63d 7a 4e 61 6d 65 20 29 20 67 6f 74 6f 20 65 78 69  zName ) goto exi
1d63e 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a  t_rename_table;.
1d63f 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74  .  /* Check that
1d640 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65   a table or inde
1d641 78 20 6e 61 6d 65 64 20 27 7a 4e 61 6d 65 27 20  x named 'zName' 
1d642 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  does not already
1d643 20 65 78 69 73 74 0a 20 20 2a 2a 20 69 6e 20 64   exist.  ** in d
1d644 61 74 61 62 61 73 65 20 69 44 62 2e 20 49 66 20  atabase iDb. If 
1d645 73 6f 2c 20 74 68 69 73 20 69 73 20 61 6e 20 65  so, this is an e
1d646 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  rror..  */.  if(
1d647 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
1d648 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44 62  e(db, zName, zDb
1d649 29 20 7c 7c 20 73 71 6c 69 74 65 33 46 69 6e 64  ) || sqlite3Find
1d64a 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c  Index(db, zName,
1d64b 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 73 71 6c   zDb) ){.    sql
1d64c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1d64d 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 74  rse, .        "t
1d64e 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20  here is already 
1d64f 61 6e 6f 74 68 65 72 20 74 61 62 6c 65 20 6f 72  another table or
1d650 20 69 6e 64 65 78 20 77 69 74 68 20 74 68 69 73   index with this
1d651 20 6e 61 6d 65 3a 20 25 73 22 2c 20 7a 4e 61 6d   name: %s", zNam
1d652 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69  e);.    goto exi
1d653 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a  t_rename_table;.
1d654 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73    }..  /* Make s
1d655 75 72 65 20 69 74 20 69 73 20 6e 6f 74 20 61 20  ure it is not a 
1d656 73 79 73 74 65 6d 20 74 61 62 6c 65 20 62 65 69  system table bei
1d657 6e 67 20 61 6c 74 65 72 65 64 2c 20 6f 72 20 61  ng altered, or a
1d658 20 72 65 73 65 72 76 65 64 20 6e 61 6d 65 0a 20   reserved name. 
1d659 20 2a 2a 20 74 68 61 74 20 74 68 65 20 74 61 62   ** that the tab
1d65a 6c 65 20 69 73 20 62 65 69 6e 67 20 72 65 6e 61  le is being rena
1d65b 6d 65 64 20 74 6f 2e 0a 20 20 2a 2f 0a 20 20 69  med to..  */.  i
1d65c 66 28 20 73 74 72 6c 65 6e 28 70 54 61 62 2d 3e  f( strlen(pTab->
1d65d 7a 4e 61 6d 65 29 3e 36 20 26 26 20 30 3d 3d 73  zName)>6 && 0==s
1d65e 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 70  qlite3StrNICmp(p
1d65f 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c  Tab->zName, "sql
1d660 69 74 65 5f 22 2c 20 37 29 20 29 7b 0a 20 20 20  ite_", 7) ){.   
1d661 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1d662 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20  (pParse, "table 
1d663 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 6c  %s may not be al
1d664 74 65 72 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e  tered", pTab->zN
1d665 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  ame);.    goto e
1d666 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65  xit_rename_table
1d667 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c 49  ;.  }.  if( SQLI
1d668 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 43 68  TE_OK!=sqlite3Ch
1d669 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 70 50  eckObjectName(pP
1d66a 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a  arse, zName) ){.
1d66b 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65      goto exit_re
1d66c 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a  name_table;.  }.
1d66d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1d66e 4f 4d 49 54 5f 56 49 45 57 0a 20 20 69 66 28 20  OMIT_VIEW.  if( 
1d66f 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b  pTab->pSelect ){
1d670 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
1d671 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 69  rMsg(pParse, "vi
1d672 65 77 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65  ew %s may not be
1d673 20 61 6c 74 65 72 65 64 22 2c 20 70 54 61 62 2d   altered", pTab-
1d674 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74  >zName);.    got
1d675 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  o exit_rename_ta
1d676 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ble;.  }.#endif.
1d677 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1d678 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
1d679 4f 4e 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74  ON.  /* Invoke t
1d67a 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  he authorization
1d67b 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 20 20   callback. */.  
1d67c 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43  if( sqlite3AuthC
1d67d 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
1d67e 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45 2c  ITE_ALTER_TABLE,
1d67f 20 7a 44 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d   zDb, pTab->zNam
1d680 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f 74  e, 0) ){.    got
1d681 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  o exit_rename_ta
1d682 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ble;.  }.#endif.
1d683 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1d684 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1d685 45 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56  E.  if( sqlite3V
1d686 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65  iewGetColumnName
1d687 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 20  s(pParse, pTab) 
1d688 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  ){.    goto exit
1d689 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20  _rename_table;. 
1d68a 20 7d 0a 20 20 69 66 28 20 49 73 56 69 72 74 75   }.  if( IsVirtu
1d68b 61 6c 28 70 54 61 62 29 20 26 26 20 70 54 61 62  al(pTab) && pTab
1d68c 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d  ->pMod->pModule-
1d68d 3e 78 52 65 6e 61 6d 65 20 29 7b 0a 20 20 20 20  >xRename ){.    
1d68e 69 73 56 69 72 74 75 61 6c 52 65 6e 61 6d 65 20  isVirtualRename 
1d68f 3d 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  = 1;.  }.#endif.
1d690 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61 20 74 72  .  /* Begin a tr
1d691 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 63 6f  ansaction and co
1d692 64 65 20 74 68 65 20 56 65 72 69 66 79 43 6f 6f  de the VerifyCoo
1d693 6b 69 65 20 66 6f 72 20 64 61 74 61 62 61 73 65  kie for database
1d694 20 69 44 62 2e 20 0a 20 20 2a 2a 20 54 68 65 6e   iDb. .  ** Then
1d695 20 6d 6f 64 69 66 79 20 74 68 65 20 73 63 68 65   modify the sche
1d696 6d 61 20 63 6f 6f 6b 69 65 20 28 73 69 6e 63 65  ma cookie (since
1d697 20 74 68 65 20 41 4c 54 45 52 20 54 41 42 4c 45   the ALTER TABLE
1d698 20 6d 6f 64 69 66 69 65 73 20 74 68 65 0a 20 20   modifies the.  
1d699 2a 2a 20 73 63 68 65 6d 61 29 2e 20 4f 70 65 6e  ** schema). Open
1d69a 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61   a statement tra
1d69b 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68 65 20  nsaction if the 
1d69c 74 61 62 6c 65 20 69 73 20 61 20 76 69 72 74 75  table is a virtu
1d69d 61 6c 0a 20 20 2a 2a 20 74 61 62 6c 65 2e 0a 20  al.  ** table.. 
1d69e 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65   */.  v = sqlite
1d69f 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
1d6a0 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a  ;.  if( v==0 ){.
1d6a1 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65      goto exit_re
1d6a2 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a  name_table;.  }.
1d6a3 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72    sqlite3BeginWr
1d6a4 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61  iteOperation(pPa
1d6a5 72 73 65 2c 20 69 73 56 69 72 74 75 61 6c 52 65  rse, isVirtualRe
1d6a6 6e 61 6d 65 2c 20 69 44 62 29 3b 0a 20 20 73 71  name, iDb);.  sq
1d6a7 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69  lite3ChangeCooki
1d6a8 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a  e(pParse, iDb);.
1d6a9 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73  .  /* If this is
1d6aa 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
1d6ab 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 78 52 65  , invoke the xRe
1d6ac 6e 61 6d 65 28 29 20 66 75 6e 63 74 69 6f 6e 20  name() function 
1d6ad 69 66 0a 20 20 2a 2a 20 6f 6e 65 20 69 73 20 64  if.  ** one is d
1d6ae 65 66 69 6e 65 64 2e 20 54 68 65 20 78 52 65 6e  efined. The xRen
1d6af 61 6d 65 28 29 20 63 61 6c 6c 62 61 63 6b 20 77  ame() callback w
1d6b0 69 6c 6c 20 6d 6f 64 69 66 79 20 74 68 65 20 6e  ill modify the n
1d6b1 61 6d 65 73 0a 20 20 2a 2a 20 6f 66 20 61 6e 79  ames.  ** of any
1d6b2 20 72 65 73 6f 75 72 63 65 73 20 75 73 65 64 20   resources used 
1d6b3 62 79 20 74 68 65 20 76 2d 74 61 62 6c 65 20 69  by the v-table i
1d6b4 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 28 69  mplementation (i
1d6b5 6e 63 6c 75 64 69 6e 67 20 6f 74 68 65 72 0a 20  ncluding other. 
1d6b6 20 2a 2a 20 53 51 4c 69 74 65 20 74 61 62 6c 65   ** SQLite table
1d6b7 73 29 20 74 68 61 74 20 61 72 65 20 69 64 65 6e  s) that are iden
1d6b8 74 69 66 69 65 64 20 62 79 20 74 68 65 20 6e 61  tified by the na
1d6b9 6d 65 20 6f 66 20 74 68 65 20 76 69 72 74 75 61  me of the virtua
1d6ba 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 23 69  l table..  */.#i
1d6bb 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1d6bc 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
1d6bd 20 69 66 28 20 69 73 56 69 72 74 75 61 6c 52 65   if( isVirtualRe
1d6be 6e 61 6d 65 20 29 7b 0a 20 20 20 20 69 6e 74 20  name ){.    int 
1d6bf 69 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  i = ++pParse->nM
1d6c0 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  em;.    sqlite3V
1d6c1 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
1d6c2 53 74 72 69 6e 67 38 2c 20 30 2c 20 69 2c 20 30  String8, 0, i, 0
1d6c3 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  , zName, 0);.   
1d6c4 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1d6c5 70 34 28 76 2c 20 4f 50 5f 56 52 65 6e 61 6d 65  p4(v, OP_VRename
1d6c6 2c 20 69 2c 20 30 2c 20 30 2c 28 63 6f 6e 73 74  , i, 0, 0,(const
1d6c7 20 63 68 61 72 2a 29 70 54 61 62 2d 3e 70 56 74   char*)pTab->pVt
1d6c8 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20 20  ab, P4_VTAB);.  
1d6c9 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 66  }.#endif..  /* f
1d6ca 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61  igure out how ma
1d6cb 6e 79 20 55 54 46 2d 38 20 63 68 61 72 61 63 74  ny UTF-8 charact
1d6cc 65 72 73 20 61 72 65 20 69 6e 20 7a 4e 61 6d 65  ers are in zName
1d6cd 20 2a 2f 0a 20 20 7a 54 61 62 4e 61 6d 65 20 3d   */.  zTabName =
1d6ce 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20   pTab->zName;.  
1d6cf 6e 54 61 62 4e 61 6d 65 20 3d 20 73 71 6c 69 74  nTabName = sqlit
1d6d0 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 7a 54  e3Utf8CharLen(zT
1d6d1 61 62 4e 61 6d 65 2c 20 2d 31 29 3b 0a 0a 20 20  abName, -1);..  
1d6d2 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65 20 73 71  /* Modify the sq
1d6d3 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c  lite_master tabl
1d6d4 65 20 74 6f 20 75 73 65 20 74 68 65 20 6e 65 77  e to use the new
1d6d5 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f 0a   table name. */.
1d6d6 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50    sqlite3NestedP
1d6d7 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20  arse(pParse,.   
1d6d8 20 20 20 22 55 50 44 41 54 45 20 25 51 2e 25 73     "UPDATE %Q.%s
1d6d9 20 53 45 54 20 22 0a 23 69 66 64 65 66 20 53 51   SET ".#ifdef SQ
1d6da 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
1d6db 52 0a 20 20 20 20 20 20 20 20 20 20 22 73 71 6c  R.          "sql
1d6dc 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65   = sqlite_rename
1d6dd 5f 74 61 62 6c 65 28 73 71 6c 2c 20 25 51 29 2c  _table(sql, %Q),
1d6de 20 22 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20   ".#else.       
1d6df 20 20 20 22 73 71 6c 20 3d 20 43 41 53 45 20 22     "sql = CASE "
1d6e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 57 48  .            "WH
1d6e1 45 4e 20 74 79 70 65 20 3d 20 27 74 72 69 67 67  EN type = 'trigg
1d6e2 65 72 27 20 54 48 45 4e 20 73 71 6c 69 74 65 5f  er' THEN sqlite_
1d6e3 72 65 6e 61 6d 65 5f 74 72 69 67 67 65 72 28 73  rename_trigger(s
1d6e4 71 6c 2c 20 25 51 29 22 0a 20 20 20 20 20 20 20  ql, %Q)".       
1d6e5 20 20 20 20 20 22 45 4c 53 45 20 73 71 6c 69 74       "ELSE sqlit
1d6e6 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 73  e_rename_table(s
1d6e7 71 6c 2c 20 25 51 29 20 45 4e 44 2c 20 22 0a 23  ql, %Q) END, ".#
1d6e8 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20 20  endif.          
1d6e9 22 74 62 6c 5f 6e 61 6d 65 20 3d 20 25 51 2c 20  "tbl_name = %Q, 
1d6ea 22 0a 20 20 20 20 20 20 20 20 20 20 22 6e 61 6d  ".          "nam
1d6eb 65 20 3d 20 43 41 53 45 20 22 0a 20 20 20 20 20  e = CASE ".     
1d6ec 20 20 20 20 20 20 20 22 57 48 45 4e 20 74 79 70         "WHEN typ
1d6ed 65 3d 27 74 61 62 6c 65 27 20 54 48 45 4e 20 25  e='table' THEN %
1d6ee 51 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  Q ".            
1d6ef 22 57 48 45 4e 20 6e 61 6d 65 20 4c 49 4b 45 20  "WHEN name LIKE 
1d6f0 27 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65  'sqlite_autoinde
1d6f1 78 25 25 27 20 41 4e 44 20 74 79 70 65 3d 27 69  x%%' AND type='i
1d6f2 6e 64 65 78 27 20 54 48 45 4e 20 22 0a 20 20 20  ndex' THEN ".   
1d6f3 20 20 20 20 20 20 20 20 20 20 22 27 73 71 6c 69            "'sqli
1d6f4 74 65 5f 61 75 74 6f 69 6e 64 65 78 5f 27 20 7c  te_autoindex_' |
1d6f5 7c 20 25 51 20 7c 7c 20 73 75 62 73 74 72 28 6e  | %Q || substr(n
1d6f6 61 6d 65 2c 25 64 2b 31 38 29 20 22 0a 20 20 20  ame,%d+18) ".   
1d6f7 20 20 20 20 20 20 20 20 20 22 45 4c 53 45 20 6e           "ELSE n
1d6f8 61 6d 65 20 45 4e 44 20 22 0a 20 20 20 20 20 20  ame END ".      
1d6f9 22 57 48 45 52 45 20 74 62 6c 5f 6e 61 6d 65 3d  "WHERE tbl_name=
1d6fa 25 51 20 41 4e 44 20 22 0a 20 20 20 20 20 20 20  %Q AND ".       
1d6fb 20 20 20 22 28 74 79 70 65 3d 27 74 61 62 6c 65     "(type='table
1d6fc 27 20 4f 52 20 74 79 70 65 3d 27 69 6e 64 65 78  ' OR type='index
1d6fd 27 20 4f 52 20 74 79 70 65 3d 27 74 72 69 67 67  ' OR type='trigg
1d6fe 65 72 27 29 3b 22 2c 20 0a 20 20 20 20 20 20 7a  er');", .      z
1d6ff 44 62 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45  Db, SCHEMA_TABLE
1d700 28 69 44 62 29 2c 20 7a 4e 61 6d 65 2c 20 7a 4e  (iDb), zName, zN
1d701 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 0a 23 69 66  ame, zName, .#if
1d702 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1d703 5f 54 52 49 47 47 45 52 0a 20 20 20 20 20 20 7a  _TRIGGER.      z
1d704 4e 61 6d 65 2c 0a 23 65 6e 64 69 66 0a 20 20 20  Name,.#endif.   
1d705 20 20 20 7a 4e 61 6d 65 2c 20 6e 54 61 62 4e 61     zName, nTabNa
1d706 6d 65 2c 20 7a 54 61 62 4e 61 6d 65 0a 20 20 29  me, zTabName.  )
1d707 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
1d708 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45  E_OMIT_AUTOINCRE
1d709 4d 45 4e 54 0a 20 20 2f 2a 20 49 66 20 74 68 65  MENT.  /* If the
1d70a 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65   sqlite_sequence
1d70b 20 74 61 62 6c 65 20 65 78 69 73 74 73 20 69 6e   table exists in
1d70c 20 74 68 69 73 20 64 61 74 61 62 61 73 65 2c 20   this database, 
1d70d 74 68 65 6e 20 75 70 64 61 74 65 20 0a 20 20 2a  then update .  *
1d70e 2a 20 69 74 20 77 69 74 68 20 74 68 65 20 6e 65  * it with the ne
1d70f 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 0a 20 20  w table name..  
1d710 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
1d711 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 22 73  FindTable(db, "s
1d712 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 22 2c  qlite_sequence",
1d713 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 73 71 6c   zDb) ){.    sql
1d714 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28  ite3NestedParse(
1d715 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20  pParse,.        
1d716 22 55 50 44 41 54 45 20 5c 22 25 77 5c 22 2e 73  "UPDATE \"%w\".s
1d717 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 73  qlite_sequence s
1d718 65 74 20 6e 61 6d 65 20 3d 20 25 51 20 57 48 45  et name = %Q WHE
1d719 52 45 20 6e 61 6d 65 20 3d 20 25 51 22 2c 0a 20  RE name = %Q",. 
1d71a 20 20 20 20 20 20 20 7a 44 62 2c 20 7a 4e 61 6d         zDb, zNam
1d71b 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  e, pTab->zName);
1d71c 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  .  }.#endif..#if
1d71d 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1d71e 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 49 66  _TRIGGER.  /* If
1d71f 20 74 68 65 72 65 20 61 72 65 20 54 45 4d 50 20   there are TEMP 
1d720 74 72 69 67 67 65 72 73 20 6f 6e 20 74 68 69 73  triggers on this
1d721 20 74 61 62 6c 65 2c 20 6d 6f 64 69 66 79 20 74   table, modify t
1d722 68 65 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d  he sqlite_temp_m
1d723 61 73 74 65 72 0a 20 20 2a 2a 20 74 61 62 6c 65  aster.  ** table
1d724 2e 20 44 6f 6e 27 74 20 64 6f 20 74 68 69 73 20  . Don't do this 
1d725 69 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  if the table bei
1d726 6e 67 20 41 4c 54 45 52 65 64 20 69 73 20 69 74  ng ALTERed is it
1d727 73 65 6c 66 20 6c 6f 63 61 74 65 64 20 69 6e 0a  self located in.
1d728 20 20 2a 2a 20 74 68 65 20 74 65 6d 70 20 64 61    ** the temp da
1d729 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69  tabase..  */.  i
1d72a 66 28 20 28 7a 57 68 65 72 65 3d 77 68 65 72 65  f( (zWhere=where
1d72b 54 65 6d 70 54 72 69 67 67 65 72 73 28 70 50 61  TempTriggers(pPa
1d72c 72 73 65 2c 20 70 54 61 62 29 29 21 3d 30 20 29  rse, pTab))!=0 )
1d72d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73  {.    sqlite3Nes
1d72e 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c  tedParse(pParse,
1d72f 20 0a 20 20 20 20 20 20 20 20 22 55 50 44 41 54   .        "UPDAT
1d730 45 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61  E sqlite_temp_ma
1d731 73 74 65 72 20 53 45 54 20 22 0a 20 20 20 20 20  ster SET ".     
1d732 20 20 20 20 20 20 20 22 73 71 6c 20 3d 20 73 71         "sql = sq
1d733 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 72 69 67  lite_rename_trig
1d734 67 65 72 28 73 71 6c 2c 20 25 51 29 2c 20 22 0a  ger(sql, %Q), ".
1d735 20 20 20 20 20 20 20 20 20 20 20 20 22 74 62 6c              "tbl
1d736 5f 6e 61 6d 65 20 3d 20 25 51 20 22 0a 20 20 20  _name = %Q ".   
1d737 20 20 20 20 20 20 20 20 20 22 57 48 45 52 45 20           "WHERE 
1d738 25 73 3b 22 2c 20 7a 4e 61 6d 65 2c 20 7a 4e 61  %s;", zName, zNa
1d739 6d 65 2c 20 7a 57 68 65 72 65 29 3b 0a 20 20 20  me, zWhere);.   
1d73a 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1d73b 62 2c 20 7a 57 68 65 72 65 29 3b 0a 20 20 7d 0a  b, zWhere);.  }.
1d73c 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 44 72 6f  #endif..  /* Dro
1d73d 70 20 61 6e 64 20 72 65 6c 6f 61 64 20 74 68 65  p and reload the
1d73e 20 69 6e 74 65 72 6e 61 6c 20 74 61 62 6c 65 20   internal table 
1d73f 73 63 68 65 6d 61 2e 20 2a 2f 0a 20 20 72 65 6c  schema. */.  rel
1d740 6f 61 64 54 61 62 6c 65 53 63 68 65 6d 61 28 70  oadTableSchema(p
1d741 50 61 72 73 65 2c 20 70 54 61 62 2c 20 7a 4e 61  Parse, pTab, zNa
1d742 6d 65 29 3b 0a 0a 65 78 69 74 5f 72 65 6e 61 6d  me);..exit_renam
1d743 65 5f 74 61 62 6c 65 3a 0a 20 20 73 71 6c 69 74  e_table:.  sqlit
1d744 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28  e3SrcListDelete(
1d745 64 62 2c 20 70 53 72 63 29 3b 0a 20 20 73 71 6c  db, pSrc);.  sql
1d746 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
1d747 4e 61 6d 65 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  Name);.}.../*.**
1d748 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
1d749 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61  s called after a
1d74a 6e 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 2e  n "ALTER TABLE .
1d74b 2e 2e 20 41 44 44 22 20 73 74 61 74 65 6d 65 6e  .. ADD" statemen
1d74c 74 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 70 61  t.** has been pa
1d74d 72 73 65 64 2e 20 41 72 67 75 6d 65 6e 74 20 70  rsed. Argument p
1d74e 43 6f 6c 44 65 66 20 63 6f 6e 74 61 69 6e 73 20  ColDef contains 
1d74f 74 68 65 20 74 65 78 74 20 6f 66 20 74 68 65 20  the text of the 
1d750 6e 65 77 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 64 65  new.** column de
1d751 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  finition..**.** 
1d752 54 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74  The Table struct
1d753 75 72 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  ure pParse->pNew
1d754 54 61 62 6c 65 20 77 61 73 20 65 78 74 65 6e 64  Table was extend
1d755 65 64 20 74 6f 20 69 6e 63 6c 75 64 65 0a 2a 2a  ed to include.**
1d756 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20   the new column 
1d757 64 75 72 69 6e 67 20 70 61 72 73 69 6e 67 2e 0a  during parsing..
1d758 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1d759 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c  E void sqlite3Al
1d75a 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75  terFinishAddColu
1d75b 6d 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  mn(Parse *pParse
1d75c 2c 20 54 6f 6b 65 6e 20 2a 70 43 6f 6c 44 65 66  , Token *pColDef
1d75d 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77  ){.  Table *pNew
1d75e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1d75f 2a 20 43 6f 70 79 20 6f 66 20 70 50 61 72 73 65  * Copy of pParse
1d760 2d 3e 70 4e 65 77 54 61 62 6c 65 20 2a 2f 0a 20  ->pNewTable */. 
1d761 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20   Table *pTab;   
1d762 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61             /* Ta
1d763 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65  ble being altere
1d764 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  d */.  int iDb; 
1d765 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d766 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75 6d   /* Database num
1d767 62 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ber */.  const c
1d768 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20  har *zDb;       
1d769 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e     /* Database n
1d76a 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ame */.  const c
1d76b 68 61 72 20 2a 7a 54 61 62 3b 20 20 20 20 20 20  har *zTab;      
1d76c 20 20 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65     /* Table name
1d76d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c   */.  char *zCol
1d76e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1d76f 2f 2a 20 4e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74  /* Null-terminat
1d770 65 64 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69  ed column defini
1d771 74 69 6f 6e 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e  tion */.  Column
1d772 20 2a 70 43 6f 6c 3b 20 20 20 20 20 20 20 20 20   *pCol;         
1d773 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 63      /* The new c
1d774 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45 78 70 72 20  olumn */.  Expr 
1d775 2a 70 44 66 6c 74 3b 20 20 20 20 20 20 20 20 20  *pDflt;         
1d776 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20       /* Default 
1d777 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 6e 65  value for the ne
1d778 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 73 71  w column */.  sq
1d779 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
1d77a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
1d77b 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1d77c 6f 6e 3b 20 2a 2f 0a 0a 20 20 64 62 20 3d 20 70  on; */..  db = p
1d77d 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28  Parse->db;.  if(
1d77e 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c   pParse->nErr ||
1d77f 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1d780 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 4e  d ) return;.  pN
1d781 65 77 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65  ew = pParse->pNe
1d782 77 54 61 62 6c 65 3b 0a 20 20 61 73 73 65 72 74  wTable;.  assert
1d783 28 20 70 4e 65 77 20 29 3b 0a 0a 20 20 61 73 73  ( pNew );..  ass
1d784 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
1d785 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73  eHoldsAllMutexes
1d786 28 64 62 29 20 29 3b 0a 20 20 69 44 62 20 3d 20  (db) );.  iDb = 
1d787 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
1d788 6e 64 65 78 28 64 62 2c 20 70 4e 65 77 2d 3e 70  ndex(db, pNew->p
1d789 53 63 68 65 6d 61 29 3b 0a 20 20 7a 44 62 20 3d  Schema);.  zDb =
1d78a 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
1d78b 61 6d 65 3b 0a 20 20 7a 54 61 62 20 3d 20 70 4e  ame;.  zTab = pN
1d78c 65 77 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 70 43 6f  ew->zName;.  pCo
1d78d 6c 20 3d 20 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b  l = &pNew->aCol[
1d78e 70 4e 65 77 2d 3e 6e 43 6f 6c 2d 31 5d 3b 0a 20  pNew->nCol-1];. 
1d78f 20 70 44 66 6c 74 20 3d 20 70 43 6f 6c 2d 3e 70   pDflt = pCol->p
1d790 44 66 6c 74 3b 0a 20 20 70 54 61 62 20 3d 20 73  Dflt;.  pTab = s
1d791 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
1d792 64 62 2c 20 7a 54 61 62 2c 20 7a 44 62 29 3b 0a  db, zTab, zDb);.
1d793 20 20 61 73 73 65 72 74 28 20 70 54 61 62 20 29    assert( pTab )
1d794 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
1d795 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41  E_OMIT_AUTHORIZA
1d796 54 49 4f 4e 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65  TION.  /* Invoke
1d797 20 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69   the authorizati
1d798 6f 6e 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a  on callback. */.
1d799 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
1d79a 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53  hCheck(pParse, S
1d79b 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c  QLITE_ALTER_TABL
1d79c 45 2c 20 7a 44 62 2c 20 70 54 61 62 2d 3e 7a 4e  E, zDb, pTab->zN
1d79d 61 6d 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 72  ame, 0) ){.    r
1d79e 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69  eturn;.  }.#endi
1d79f 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64  f..  /* If the d
1d7a0 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72  efault value for
1d7a1 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20   the new column 
1d7a2 77 61 73 20 73 70 65 63 69 66 69 65 64 20 77 69  was specified wi
1d7a3 74 68 20 61 20 0a 20 20 2a 2a 20 6c 69 74 65 72  th a .  ** liter
1d7a4 61 6c 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 73 65  al NULL, then se
1d7a5 74 20 70 44 66 6c 74 20 74 6f 20 30 2e 20 54 68  t pDflt to 0. Th
1d7a6 69 73 20 73 69 6d 70 6c 69 66 69 65 73 20 63 68  is simplifies ch
1d7a7 65 63 6b 69 6e 67 0a 20 20 2a 2a 20 66 6f 72 20  ecking.  ** for 
1d7a8 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 64 65 66 61  an SQL NULL defa
1d7a9 75 6c 74 20 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a  ult below..  */.
1d7aa 20 20 69 66 28 20 70 44 66 6c 74 20 26 26 20 70    if( pDflt && p
1d7ab 44 66 6c 74 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c  Dflt->op==TK_NUL
1d7ac 4c 20 29 7b 0a 20 20 20 20 70 44 66 6c 74 20 3d  L ){.    pDflt =
1d7ad 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68   0;.  }..  /* Ch
1d7ae 65 63 6b 20 74 68 61 74 20 74 68 65 20 6e 65 77  eck that the new
1d7af 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 73   column is not s
1d7b0 70 65 63 69 66 69 65 64 20 61 73 20 50 52 49 4d  pecified as PRIM
1d7b1 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55  ARY KEY or UNIQU
1d7b2 45 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 72 65  E..  ** If there
1d7b3 20 69 73 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63   is a NOT NULL c
1d7b4 6f 6e 73 74 72 61 69 6e 74 2c 20 74 68 65 6e 20  onstraint, then 
1d7b5 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75  the default valu
1d7b6 65 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 63  e for the.  ** c
1d7b7 6f 6c 75 6d 6e 20 6d 75 73 74 20 6e 6f 74 20 62  olumn must not b
1d7b8 65 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 69  e NULL..  */.  i
1d7b9 66 28 20 70 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b  f( pCol->isPrimK
1d7ba 65 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ey ){.    sqlite
1d7bb 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1d7bc 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20  , "Cannot add a 
1d7bd 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75  PRIMARY KEY colu
1d7be 6d 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  mn");.    return
1d7bf 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e 65 77  ;.  }.  if( pNew
1d7c0 2d 3e 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20  ->pIndex ){.    
1d7c1 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1d7c2 70 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20  pParse, "Cannot 
1d7c3 61 64 64 20 61 20 55 4e 49 51 55 45 20 63 6f 6c  add a UNIQUE col
1d7c4 75 6d 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72  umn");.    retur
1d7c5 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 6f  n;.  }.  if( pCo
1d7c6 6c 2d 3e 6e 6f 74 4e 75 6c 6c 20 26 26 20 21 70  l->notNull && !p
1d7c7 44 66 6c 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  Dflt ){.    sqli
1d7c8 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1d7c9 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 43 61  se, .        "Ca
1d7ca 6e 6e 6f 74 20 61 64 64 20 61 20 4e 4f 54 20 4e  nnot add a NOT N
1d7cb 55 4c 4c 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20  ULL column with 
1d7cc 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 4e 55  default value NU
1d7cd 4c 4c 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  LL");.    return
1d7ce 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75  ;.  }..  /* Ensu
1d7cf 72 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 65  re the default e
1d7d0 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73 6f 6d  xpression is som
1d7d1 65 74 68 69 6e 67 20 74 68 61 74 20 73 71 6c 69  ething that sqli
1d7d2 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72  te3ValueFromExpr
1d7d3 28 29 0a 20 20 2a 2a 20 63 61 6e 20 68 61 6e 64  ().  ** can hand
1d7d4 6c 65 20 28 69 2e 65 2e 20 6e 6f 74 20 43 55 52  le (i.e. not CUR
1d7d5 52 45 4e 54 5f 54 49 4d 45 20 65 74 63 2e 29 0a  RENT_TIME etc.).
1d7d6 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 66 6c 74    */.  if( pDflt
1d7d7 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
1d7d8 76 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20 20 20  value *pVal;.   
1d7d9 20 69 66 28 20 73 71 6c 69 74 65 33 56 61 6c 75   if( sqlite3Valu
1d7da 65 46 72 6f 6d 45 78 70 72 28 64 62 2c 20 70 44  eFromExpr(db, pD
1d7db 66 6c 74 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  flt, SQLITE_UTF8
1d7dc 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  , SQLITE_AFF_NON
1d7dd 45 2c 20 26 70 56 61 6c 29 20 29 7b 0a 20 20 20  E, &pVal) ){.   
1d7de 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
1d7df 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 72  led = 1;.      r
1d7e0 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20  eturn;.    }.   
1d7e1 20 69 66 28 20 21 70 56 61 6c 20 29 7b 0a 20 20   if( !pVal ){.  
1d7e2 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1d7e3 4d 73 67 28 70 50 61 72 73 65 2c 20 22 43 61 6e  Msg(pParse, "Can
1d7e4 6e 6f 74 20 61 64 64 20 61 20 63 6f 6c 75 6d 6e  not add a column
1d7e5 20 77 69 74 68 20 6e 6f 6e 2d 63 6f 6e 73 74 61   with non-consta
1d7e6 6e 74 20 64 65 66 61 75 6c 74 22 29 3b 0a 20 20  nt default");.  
1d7e7 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
1d7e8 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c  }.    sqlite3Val
1d7e9 75 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20  ueFree(pVal);.  
1d7ea 7d 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66 79 20 74  }..  /* Modify t
1d7eb 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  he CREATE TABLE 
1d7ec 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20  statement. */.  
1d7ed 7a 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 44 62  zCol = sqlite3Db
1d7ee 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68 61  StrNDup(db, (cha
1d7ef 72 2a 29 70 43 6f 6c 44 65 66 2d 3e 7a 2c 20 70  r*)pColDef->z, p
1d7f0 43 6f 6c 44 65 66 2d 3e 6e 29 3b 0a 20 20 69 66  ColDef->n);.  if
1d7f1 28 20 7a 43 6f 6c 20 29 7b 0a 20 20 20 20 63 68  ( zCol ){.    ch
1d7f2 61 72 20 2a 7a 45 6e 64 20 3d 20 26 7a 43 6f 6c  ar *zEnd = &zCol
1d7f3 5b 70 43 6f 6c 44 65 66 2d 3e 6e 2d 31 5d 3b 0a  [pColDef->n-1];.
1d7f4 20 20 20 20 77 68 69 6c 65 28 20 28 7a 45 6e 64      while( (zEnd
1d7f5 3e 7a 43 6f 6c 20 26 26 20 2a 7a 45 6e 64 3d 3d  >zCol && *zEnd==
1d7f6 27 3b 27 29 20 7c 7c 20 69 73 73 70 61 63 65 28  ';') || isspace(
1d7f7 2a 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  *(unsigned char 
1d7f8 2a 29 7a 45 6e 64 29 20 29 7b 0a 20 20 20 20 20  *)zEnd) ){.     
1d7f9 20 2a 7a 45 6e 64 2d 2d 20 3d 20 27 5c 30 27 3b   *zEnd-- = '\0';
1d7fa 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
1d7fb 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50  e3NestedParse(pP
1d7fc 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22  arse, .        "
1d7fd 55 50 44 41 54 45 20 5c 22 25 77 5c 22 2e 25 73  UPDATE \"%w\".%s
1d7fe 20 53 45 54 20 22 0a 20 20 20 20 20 20 20 20 20   SET ".         
1d7ff 20 22 73 71 6c 20 3d 20 73 75 62 73 74 72 28 73   "sql = substr(s
1d800 71 6c 2c 31 2c 25 64 29 20 7c 7c 20 27 2c 20 27  ql,1,%d) || ', '
1d801 20 7c 7c 20 25 51 20 7c 7c 20 73 75 62 73 74 72   || %Q || substr
1d802 28 73 71 6c 2c 25 64 29 20 22 0a 20 20 20 20 20  (sql,%d) ".     
1d803 20 20 20 22 57 48 45 52 45 20 74 79 70 65 20 3d     "WHERE type =
1d804 20 27 74 61 62 6c 65 27 20 41 4e 44 20 6e 61 6d   'table' AND nam
1d805 65 20 3d 20 25 51 22 2c 20 0a 20 20 20 20 20 20  e = %Q", .      
1d806 7a 44 62 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c  zDb, SCHEMA_TABL
1d807 45 28 69 44 62 29 2c 20 70 4e 65 77 2d 3e 61 64  E(iDb), pNew->ad
1d808 64 43 6f 6c 4f 66 66 73 65 74 2c 20 7a 43 6f 6c  dColOffset, zCol
1d809 2c 20 70 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f 66  , pNew->addColOf
1d80a 66 73 65 74 2b 31 2c 0a 20 20 20 20 20 20 7a 54  fset+1,.      zT
1d80b 61 62 0a 20 20 20 20 29 3b 0a 20 20 20 20 73 71  ab.    );.    sq
1d80c 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1d80d 7a 43 6f 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  zCol);.  }..  /*
1d80e 20 49 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   If the default 
1d80f 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6e 65 77  value of the new
1d810 20 63 6f 6c 75 6d 6e 20 69 73 20 4e 55 4c 4c 2c   column is NULL,
1d811 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 66 69   then set the fi
1d812 6c 65 0a 20 20 2a 2a 20 66 6f 72 6d 61 74 20 74  le.  ** format t
1d813 6f 20 32 2e 20 49 66 20 74 68 65 20 64 65 66 61  o 2. If the defa
1d814 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65  ult value of the
1d815 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69 73 20 6e   new column is n
1d816 6f 74 20 4e 55 4c 4c 2c 0a 20 20 2a 2a 20 74 68  ot NULL,.  ** th
1d817 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 62 65  e file format be
1d818 63 6f 6d 65 73 20 33 2e 0a 20 20 2a 2f 0a 20 20  comes 3..  */.  
1d819 73 71 6c 69 74 65 33 4d 69 6e 69 6d 75 6d 46 69  sqlite3MinimumFi
1d81a 6c 65 46 6f 72 6d 61 74 28 70 50 61 72 73 65 2c  leFormat(pParse,
1d81b 20 69 44 62 2c 20 70 44 66 6c 74 20 3f 20 33 20   iDb, pDflt ? 3 
1d81c 3a 20 32 29 3b 0a 0a 20 20 2f 2a 20 52 65 6c 6f  : 2);..  /* Relo
1d81d 61 64 20 74 68 65 20 73 63 68 65 6d 61 20 6f 66  ad the schema of
1d81e 20 74 68 65 20 6d 6f 64 69 66 69 65 64 20 74 61   the modified ta
1d81f 62 6c 65 2e 20 2a 2f 0a 20 20 72 65 6c 6f 61 64  ble. */.  reload
1d820 54 61 62 6c 65 53 63 68 65 6d 61 28 70 50 61 72  TableSchema(pPar
1d821 73 65 2c 20 70 54 61 62 2c 20 70 54 61 62 2d 3e  se, pTab, pTab->
1d822 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  zName);.}../*.**
1d823 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
1d824 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  s called by the 
1d825 70 61 72 73 65 72 20 61 66 74 65 72 20 74 68 65  parser after the
1d826 20 74 61 62 6c 65 2d 6e 61 6d 65 20 69 6e 0a 2a   table-name in.*
1d827 2a 20 61 6e 20 22 41 4c 54 45 52 20 54 41 42 4c  * an "ALTER TABL
1d828 45 20 3c 74 61 62 6c 65 2d 6e 61 6d 65 3e 20 41  E <table-name> A
1d829 44 44 22 20 73 74 61 74 65 6d 65 6e 74 20 69 73  DD" statement is
1d82a 20 70 61 72 73 65 64 2e 20 41 72 67 75 6d 65 6e   parsed. Argumen
1d82b 74 20 0a 2a 2a 20 70 53 72 63 20 69 73 20 74 68  t .** pSrc is th
1d82c 65 20 66 75 6c 6c 2d 6e 61 6d 65 20 6f 66 20 74  e full-name of t
1d82d 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 61  he table being a
1d82e 6c 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  ltered..**.** Th
1d82f 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 6b 65 73  is routine makes
1d830 20 61 20 28 70 61 72 74 69 61 6c 29 20 63 6f 70   a (partial) cop
1d831 79 20 6f 66 20 74 68 65 20 54 61 62 6c 65 20 73  y of the Table s
1d832 74 72 75 63 74 75 72 65 0a 2a 2a 20 66 6f 72 20  tructure.** for 
1d833 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20  the table being 
1d834 61 6c 74 65 72 65 64 20 61 6e 64 20 73 65 74 73  altered and sets
1d835 20 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65   Parse.pNewTable
1d836 20 74 6f 20 70 6f 69 6e 74 0a 2a 2a 20 74 6f 20   to point.** to 
1d837 69 74 2e 20 52 6f 75 74 69 6e 65 73 20 63 61 6c  it. Routines cal
1d838 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65  led by the parse
1d839 72 20 61 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20  r as the column 
1d83a 64 65 66 69 6e 69 74 69 6f 6e 0a 2a 2a 20 69 73  definition.** is
1d83b 20 70 61 72 73 65 64 20 28 69 2e 65 2e 20 73 71   parsed (i.e. sq
1d83c 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28 29  lite3AddColumn()
1d83d 29 20 61 64 64 20 74 68 65 20 6e 65 77 20 43 6f  ) add the new Co
1d83e 6c 75 6d 6e 20 64 61 74 61 20 74 6f 20 0a 2a 2a  lumn data to .**
1d83f 20 74 68 65 20 63 6f 70 79 2e 20 54 68 65 20 63   the copy. The c
1d840 6f 70 79 20 6f 66 20 74 68 65 20 54 61 62 6c 65  opy of the Table
1d841 20 73 74 72 75 63 74 75 72 65 20 69 73 20 64 65   structure is de
1d842 6c 65 74 65 64 20 62 79 20 74 6f 6b 65 6e 69 7a  leted by tokeniz
1d843 65 2e 63 20 0a 2a 2a 20 61 66 74 65 72 20 70 61  e.c .** after pa
1d844 72 73 69 6e 67 20 69 73 20 66 69 6e 69 73 68 65  rsing is finishe
1d845 64 2e 0a 2a 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65  d..**.** Routine
1d846 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 69 6e   sqlite3AlterFin
1d847 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 29 20 77  ishAddColumn() w
1d848 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 74 6f  ill be called to
1d849 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 63 6f 64   complete.** cod
1d84a 69 6e 67 20 74 68 65 20 22 41 4c 54 45 52 20 54  ing the "ALTER T
1d84b 41 42 4c 45 20 2e 2e 2e 20 41 44 44 22 20 73 74  ABLE ... ADD" st
1d84c 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  atement..*/.SQLI
1d84d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1d84e 73 71 6c 69 74 65 33 41 6c 74 65 72 42 65 67 69  sqlite3AlterBegi
1d84f 6e 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65  nAddColumn(Parse
1d850 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73   *pParse, SrcLis
1d851 74 20 2a 70 53 72 63 29 7b 0a 20 20 54 61 62 6c  t *pSrc){.  Tabl
1d852 65 20 2a 70 4e 65 77 3b 0a 20 20 54 61 62 6c 65  e *pNew;.  Table
1d853 20 2a 70 54 61 62 3b 0a 20 20 56 64 62 65 20 2a   *pTab;.  Vdbe *
1d854 76 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20  v;.  int iDb;.  
1d855 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 41 6c  int i;.  int nAl
1d856 6c 6f 63 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  loc;.  sqlite3 *
1d857 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
1d858 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b 20 75 70 20 74  ..  /* Look up t
1d859 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 61  he table being a
1d85a 6c 74 65 72 65 64 2e 20 2a 2f 0a 20 20 61 73 73  ltered. */.  ass
1d85b 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65  ert( pParse->pNe
1d85c 77 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 61  wTable==0 );.  a
1d85d 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
1d85e 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78  reeHoldsAllMutex
1d85f 65 73 28 64 62 29 20 29 3b 0a 20 20 69 66 28 20  es(db) );.  if( 
1d860 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1d861 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67   ) goto exit_beg
1d862 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20  in_add_column;. 
1d863 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c   pTab = sqlite3L
1d864 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73  ocateTable(pPars
1d865 65 2c 20 30 2c 20 70 53 72 63 2d 3e 61 5b 30 5d  e, 0, pSrc->a[0]
1d866 2e 7a 4e 61 6d 65 2c 20 70 53 72 63 2d 3e 61 5b  .zName, pSrc->a[
1d867 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  0].zDatabase);. 
1d868 20 69 66 28 20 21 70 54 61 62 20 29 20 67 6f 74   if( !pTab ) got
1d869 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64  o exit_begin_add
1d86a 5f 63 6f 6c 75 6d 6e 3b 0a 0a 23 69 66 6e 64 65  _column;..#ifnde
1d86b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
1d86c 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28  RTUALTABLE.  if(
1d86d 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
1d86e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
1d86f 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1d870 22 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20  "virtual tables 
1d871 6d 61 79 20 6e 6f 74 20 62 65 20 61 6c 74 65 72  may not be alter
1d872 65 64 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  ed");.    goto e
1d873 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f  xit_begin_add_co
1d874 6c 75 6d 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  lumn;.  }.#endif
1d875 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
1d876 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 6e 20   this is not an 
1d877 61 74 74 65 6d 70 74 20 74 6f 20 41 4c 54 45 52  attempt to ALTER
1d878 20 61 20 76 69 65 77 2e 20 2a 2f 0a 20 20 69 66   a view. */.  if
1d879 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20  ( pTab->pSelect 
1d87a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
1d87b 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1d87c 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 63 6f 6c  Cannot add a col
1d87d 75 6d 6e 20 74 6f 20 61 20 76 69 65 77 22 29 3b  umn to a view");
1d87e 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 62  .    goto exit_b
1d87f 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b  egin_add_column;
1d880 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
1d881 70 54 61 62 2d 3e 61 64 64 43 6f 6c 4f 66 66 73  pTab->addColOffs
1d882 65 74 3e 30 20 29 3b 0a 20 20 69 44 62 20 3d 20  et>0 );.  iDb = 
1d883 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
1d884 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70  ndex(db, pTab->p
1d885 53 63 68 65 6d 61 29 3b 0a 0a 20 20 2f 2a 20 50  Schema);..  /* P
1d886 75 74 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  ut a copy of the
1d887 20 54 61 62 6c 65 20 73 74 72 75 63 74 20 69 6e   Table struct in
1d888 20 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65   Parse.pNewTable
1d889 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 73 71   for the.  ** sq
1d88a 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28 29  lite3AddColumn()
1d88b 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 66 72   function and fr
1d88c 69 65 6e 64 73 20 74 6f 20 6d 6f 64 69 66 79 2e  iends to modify.
1d88d 0a 20 20 2a 2f 0a 20 20 70 4e 65 77 20 3d 20 28  .  */.  pNew = (
1d88e 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 33 44 62  Table*)sqlite3Db
1d88f 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
1d890 69 7a 65 6f 66 28 54 61 62 6c 65 29 29 3b 0a 20  izeof(Table));. 
1d891 20 69 66 28 20 21 70 4e 65 77 20 29 20 67 6f 74   if( !pNew ) got
1d892 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64  o exit_begin_add
1d893 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 70 50 61 72 73  _column;.  pPars
1d894 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20 70  e->pNewTable = p
1d895 4e 65 77 3b 0a 20 20 70 4e 65 77 2d 3e 6e 52 65  New;.  pNew->nRe
1d896 66 20 3d 20 31 3b 0a 20 20 70 4e 65 77 2d 3e 64  f = 1;.  pNew->d
1d897 62 20 3d 20 64 62 3b 0a 20 20 70 4e 65 77 2d 3e  b = db;.  pNew->
1d898 6e 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 6e 43 6f  nCol = pTab->nCo
1d899 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65  l;.  assert( pNe
1d89a 77 2d 3e 6e 43 6f 6c 3e 30 20 29 3b 0a 20 20 6e  w->nCol>0 );.  n
1d89b 41 6c 6c 6f 63 20 3d 20 28 28 28 70 4e 65 77 2d  Alloc = (((pNew-
1d89c 3e 6e 43 6f 6c 2d 31 29 2f 38 29 2a 38 29 2b 38  >nCol-1)/8)*8)+8
1d89d 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 41 6c 6c  ;.  assert( nAll
1d89e 6f 63 3e 3d 70 4e 65 77 2d 3e 6e 43 6f 6c 20 26  oc>=pNew->nCol &
1d89f 26 20 6e 41 6c 6c 6f 63 25 38 3d 3d 30 20 26 26  & nAlloc%8==0 &&
1d8a0 20 6e 41 6c 6c 6f 63 2d 70 4e 65 77 2d 3e 6e 43   nAlloc-pNew->nC
1d8a1 6f 6c 3c 38 20 29 3b 0a 20 20 70 4e 65 77 2d 3e  ol<8 );.  pNew->
1d8a2 61 43 6f 6c 20 3d 20 28 43 6f 6c 75 6d 6e 2a 29  aCol = (Column*)
1d8a3 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
1d8a4 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 43  ero(db, sizeof(C
1d8a5 6f 6c 75 6d 6e 29 2a 6e 41 6c 6c 6f 63 29 3b 0a  olumn)*nAlloc);.
1d8a6 20 20 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20    pNew->zName = 
1d8a7 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
1d8a8 64 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  db, pTab->zName)
1d8a9 3b 0a 20 20 69 66 28 20 21 70 4e 65 77 2d 3e 61  ;.  if( !pNew->a
1d8aa 43 6f 6c 20 7c 7c 20 21 70 4e 65 77 2d 3e 7a 4e  Col || !pNew->zN
1d8ab 61 6d 65 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d  ame ){.    db->m
1d8ac 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
1d8ad 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 62  .    goto exit_b
1d8ae 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b  egin_add_column;
1d8af 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 70 4e  .  }.  memcpy(pN
1d8b0 65 77 2d 3e 61 43 6f 6c 2c 20 70 54 61 62 2d 3e  ew->aCol, pTab->
1d8b1 61 43 6f 6c 2c 20 73 69 7a 65 6f 66 28 43 6f 6c  aCol, sizeof(Col
1d8b2 75 6d 6e 29 2a 70 4e 65 77 2d 3e 6e 43 6f 6c 29  umn)*pNew->nCol)
1d8b3 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
1d8b4 4e 65 77 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  New->nCol; i++){
1d8b5 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f  .    Column *pCo
1d8b6 6c 20 3d 20 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b  l = &pNew->aCol[
1d8b7 69 5d 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e  i];.    pCol->zN
1d8b8 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ame = sqlite3DbS
1d8b9 74 72 44 75 70 28 64 62 2c 20 70 43 6f 6c 2d 3e  trDup(db, pCol->
1d8ba 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 43 6f 6c  zName);.    pCol
1d8bb 2d 3e 7a 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20  ->zColl = 0;.   
1d8bc 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20 30   pCol->zType = 0
1d8bd 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 70 44 66 6c  ;.    pCol->pDfl
1d8be 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65  t = 0;.  }.  pNe
1d8bf 77 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64 62 2d  w->pSchema = db-
1d8c0 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d  >aDb[iDb].pSchem
1d8c1 61 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 43 6f  a;.  pNew->addCo
1d8c2 6c 4f 66 66 73 65 74 20 3d 20 70 54 61 62 2d 3e  lOffset = pTab->
1d8c3 61 64 64 43 6f 6c 4f 66 66 73 65 74 3b 0a 20 20  addColOffset;.  
1d8c4 70 4e 65 77 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a  pNew->nRef = 1;.
1d8c5 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61 20 74 72  .  /* Begin a tr
1d8c6 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 69 6e  ansaction and in
1d8c7 63 72 65 6d 65 6e 74 20 74 68 65 20 73 63 68 65  crement the sche
1d8c8 6d 61 20 63 6f 6f 6b 69 65 2e 20 20 2a 2f 0a 20  ma cookie.  */. 
1d8c9 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69   sqlite3BeginWri
1d8ca 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72  teOperation(pPar
1d8cb 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 76  se, 0, iDb);.  v
1d8cc 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
1d8cd 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
1d8ce 20 21 76 20 29 20 67 6f 74 6f 20 65 78 69 74 5f   !v ) goto exit_
1d8cf 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e  begin_add_column
1d8d0 3b 0a 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67  ;.  sqlite3Chang
1d8d1 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20  eCookie(pParse, 
1d8d2 69 44 62 29 3b 0a 0a 65 78 69 74 5f 62 65 67 69  iDb);..exit_begi
1d8d3 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3a 0a 20 20  n_add_column:.  
1d8d4 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65  sqlite3SrcListDe
1d8d5 6c 65 74 65 28 64 62 2c 20 70 53 72 63 29 3b 0a  lete(db, pSrc);.
1d8d6 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64    return;.}.#end
1d8d7 69 66 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 4c  if  /* SQLITE_AL
1d8d8 54 45 52 5f 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a  TER_TABLE */../*
1d8d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
1d8da 64 20 6f 66 20 61 6c 74 65 72 2e 63 20 2a 2a 2a  d of alter.c ***
1d8db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d8dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d8dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1d8de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
1d8df 67 69 6e 20 66 69 6c 65 20 61 6e 61 6c 79 7a 65  gin file analyze
1d8e0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1d8e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d8e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1d8e3 0a 2a 2a 20 32 30 30 35 20 4a 75 6c 79 20 38 0a  .** 2005 July 8.
1d8e4 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
1d8e5 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
1d8e6 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
1d8e7 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
1d8e8 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
1d8e9 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
1d8ea 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
1d8eb 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
1d8ec 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
1d8ed 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
1d8ee 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
1d8ef 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
1d8f0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
1d8f1 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
1d8f2 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
1d8f3 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
1d8f4 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
1d8f5 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
1d8f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d8f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d8f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d8f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
1d8fa 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
1d8fb 61 69 6e 73 20 63 6f 64 65 20 61 73 73 6f 63 69  ains code associ
1d8fc 61 74 65 64 20 77 69 74 68 20 74 68 65 20 41 4e  ated with the AN
1d8fd 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a  ALYZE command..*
1d8fe 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 61  *.** @(#) $Id: a
1d8ff 6e 61 6c 79 7a 65 2e 63 2c 76 20 31 2e 34 34 20  nalyze.c,v 1.44 
1d900 32 30 30 38 2f 31 31 2f 30 33 20 32 30 3a 35 35  2008/11/03 20:55
1d901 3a 30 37 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :07 drh Exp $.*/
1d902 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1d903 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 0a 0a 2f 2a  OMIT_ANALYZE../*
1d904 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1d905 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20   generates code 
1d906 74 68 61 74 20 6f 70 65 6e 73 20 74 68 65 20 73  that opens the s
1d907 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c  qlite_stat1 tabl
1d908 65 20 6f 6e 20 63 75 72 73 6f 72 0a 2a 2a 20 69  e on cursor.** i
1d909 53 74 61 74 43 75 72 2e 0a 2a 2a 0a 2a 2a 20 49  StatCur..**.** I
1d90a 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  f the sqlite_sta
1d90b 74 31 20 74 61 62 6c 65 73 20 64 6f 65 73 20 6e  t1 tables does n
1d90c 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78  ot previously ex
1d90d 69 73 74 2c 20 69 74 20 69 73 20 63 72 65 61 74  ist, it is creat
1d90e 65 64 2e 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65  ed..** If it doe
1d90f 73 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69  s previously exi
1d910 73 74 2c 20 61 6c 6c 20 65 6e 74 69 72 65 73 20  st, all entires 
1d911 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
1d912 74 61 62 6c 65 20 7a 57 68 65 72 65 0a 2a 2a 20  table zWhere.** 
1d913 61 72 65 20 72 65 6d 6f 76 65 64 2e 20 20 49 66  are removed.  If
1d914 20 7a 57 68 65 72 65 3d 3d 30 20 74 68 65 6e 20   zWhere==0 then 
1d915 61 6c 6c 20 65 6e 74 72 69 65 73 20 61 72 65 20  all entries are 
1d916 72 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 73 74 61 74  removed..*/.stat
1d917 69 63 20 76 6f 69 64 20 6f 70 65 6e 53 74 61 74  ic void openStat
1d918 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a  Table(.  Parse *
1d919 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
1d91a 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
1d91b 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ext */.  int iDb
1d91c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1d91d 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
1d91e 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20   we are looking 
1d91f 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61  in */.  int iSta
1d920 74 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 20  tCur,           
1d921 2f 2a 20 4f 70 65 6e 20 74 68 65 20 73 71 6c 69  /* Open the sqli
1d922 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 6f  te_stat1 table o
1d923 6e 20 74 68 69 73 20 63 75 72 73 6f 72 20 2a 2f  n this cursor */
1d924 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1d925 57 68 65 72 65 20 20 20 20 20 20 2f 2a 20 44 65  Where      /* De
1d926 6c 65 74 65 20 65 6e 74 72 69 65 73 20 61 73 73  lete entries ass
1d927 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69  ociated with thi
1d928 73 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20  s table */.){.  
1d929 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
1d92a 61 72 73 65 2d 3e 64 62 3b 0a 20 20 44 62 20 2a  arse->db;.  Db *
1d92b 70 44 62 3b 0a 20 20 69 6e 74 20 69 52 6f 6f 74  pDb;.  int iRoot
1d92c 50 61 67 65 3b 0a 20 20 69 6e 74 20 63 72 65 61  Page;.  int crea
1d92d 74 65 53 74 61 74 31 20 3d 20 30 3b 0a 20 20 54  teStat1 = 0;.  T
1d92e 61 62 6c 65 20 2a 70 53 74 61 74 3b 0a 20 20 56  able *pStat;.  V
1d92f 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33  dbe *v = sqlite3
1d930 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
1d931 0a 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72  ..  if( v==0 ) r
1d932 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28  eturn;.  assert(
1d933 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
1d934 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29  dsAllMutexes(db)
1d935 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
1d936 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 3d 3d  lite3VdbeDb(v)==
1d937 64 62 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64  db );.  pDb = &d
1d938 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 69  b->aDb[iDb];.  i
1d939 66 28 20 28 70 53 74 61 74 20 3d 20 73 71 6c 69  f( (pStat = sqli
1d93a 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c  te3FindTable(db,
1d93b 20 22 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c   "sqlite_stat1",
1d93c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 29 3d 3d 30   pDb->zName))==0
1d93d 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 73   ){.    /* The s
1d93e 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c  qlite_stat1 tabl
1d93f 65 73 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  es does not exis
1d940 74 2e 20 20 43 72 65 61 74 65 20 69 74 2e 20 20  t.  Create it.  
1d941 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61  .    ** Note tha
1d942 74 20 61 20 73 69 64 65 2d 65 66 66 65 63 74 20  t a side-effect 
1d943 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 54 41  of the CREATE TA
1d944 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73  BLE statement is
1d945 20 74 6f 20 6c 65 61 76 65 0a 20 20 20 20 2a 2a   to leave.    **
1d946 20 74 68 65 20 72 6f 6f 74 70 61 67 65 20 6f 66   the rootpage of
1d947 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 69   the new table i
1d948 6e 20 72 65 67 69 73 74 65 72 20 70 50 61 72 73  n register pPars
1d949 65 2d 3e 72 65 67 52 6f 6f 74 2e 20 20 54 68 69  e->regRoot.  Thi
1d94a 73 20 69 73 0a 20 20 20 20 2a 2a 20 69 6d 70 6f  s is.    ** impo
1d94b 72 74 61 6e 74 20 62 65 63 61 75 73 65 20 74 68  rtant because th
1d94c 65 20 4f 70 65 6e 57 72 69 74 65 20 6f 70 63 6f  e OpenWrite opco
1d94d 64 65 20 62 65 6c 6f 77 20 77 69 6c 6c 20 62 65  de below will be
1d94e 20 6e 65 65 64 69 6e 67 20 69 74 2e 20 2a 2f 0a   needing it. */.
1d94f 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65      sqlite3Neste
1d950 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20  dParse(pParse,. 
1d951 20 20 20 20 20 22 43 52 45 41 54 45 20 54 41 42       "CREATE TAB
1d952 4c 45 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61  LE %Q.sqlite_sta
1d953 74 31 28 74 62 6c 2c 69 64 78 2c 73 74 61 74 29  t1(tbl,idx,stat)
1d954 22 2c 0a 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e  ",.      pDb->zN
1d955 61 6d 65 0a 20 20 20 20 29 3b 0a 20 20 20 20 69  ame.    );.    i
1d956 52 6f 6f 74 50 61 67 65 20 3d 20 70 50 61 72 73  RootPage = pPars
1d957 65 2d 3e 72 65 67 52 6f 6f 74 3b 0a 20 20 20 20  e->regRoot;.    
1d958 63 72 65 61 74 65 53 74 61 74 31 20 3d 20 31 3b  createStat1 = 1;
1d959 20 20 2f 2a 20 43 61 75 73 65 20 72 6f 6f 74 70    /* Cause rootp
1d95a 61 67 65 20 74 6f 20 62 65 20 74 61 6b 65 6e 20  age to be taken 
1d95b 66 72 6f 6d 20 74 6f 70 20 6f 66 20 73 74 61 63  from top of stac
1d95c 6b 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28  k */.  }else if(
1d95d 20 7a 57 68 65 72 65 20 29 7b 0a 20 20 20 20 2f   zWhere ){.    /
1d95e 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74 61  * The sqlite_sta
1d95f 74 31 20 74 61 62 6c 65 20 65 78 69 73 74 73 2e  t1 table exists.
1d960 20 20 44 65 6c 65 74 65 20 61 6c 6c 20 65 6e 74    Delete all ent
1d961 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  ries associated 
1d962 77 69 74 68 0a 20 20 20 20 2a 2a 20 74 68 65 20  with.    ** the 
1d963 74 61 62 6c 65 20 7a 57 68 65 72 65 2e 20 2a 2f  table zWhere. */
1d964 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74  .    sqlite3Nest
1d965 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a  edParse(pParse,.
1d966 20 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46         "DELETE F
1d967 52 4f 4d 20 25 51 2e 73 71 6c 69 74 65 5f 73 74  ROM %Q.sqlite_st
1d968 61 74 31 20 57 48 45 52 45 20 74 62 6c 3d 25 51  at1 WHERE tbl=%Q
1d969 22 2c 0a 20 20 20 20 20 20 20 70 44 62 2d 3e 7a  ",.       pDb->z
1d96a 4e 61 6d 65 2c 20 7a 57 68 65 72 65 0a 20 20 20  Name, zWhere.   
1d96b 20 29 3b 0a 20 20 20 20 69 52 6f 6f 74 50 61 67   );.    iRootPag
1d96c 65 20 3d 20 70 53 74 61 74 2d 3e 74 6e 75 6d 3b  e = pStat->tnum;
1d96d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
1d96e 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   The sqlite_stat
1d96f 31 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20  1 table already 
1d970 65 78 69 73 74 73 2e 20 20 44 65 6c 65 74 65 20  exists.  Delete 
1d971 61 6c 6c 20 72 6f 77 73 2e 20 2a 2f 0a 20 20 20  all rows. */.   
1d972 20 69 52 6f 6f 74 50 61 67 65 20 3d 20 70 53 74   iRootPage = pSt
1d973 61 74 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 73 71  at->tnum;.    sq
1d974 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1d975 76 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 70 53 74  v, OP_Clear, pSt
1d976 61 74 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a  at->tnum, iDb);.
1d977 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74    }..  /* Open t
1d978 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20  he sqlite_stat1 
1d979 74 61 62 6c 65 20 66 6f 72 20 77 72 69 74 69 6e  table for writin
1d97a 67 2e 20 55 6e 6c 65 73 73 20 69 74 20 77 61 73  g. Unless it was
1d97b 20 63 72 65 61 74 65 64 0a 20 20 2a 2a 20 62 79   created.  ** by
1d97c 20 74 68 69 73 20 76 64 62 65 20 70 72 6f 67 72   this vdbe progr
1d97d 61 6d 2c 20 6c 6f 63 6b 20 69 74 20 66 6f 72 20  am, lock it for 
1d97e 77 72 69 74 69 6e 67 20 61 74 20 74 68 65 20 73  writing at the s
1d97f 68 61 72 65 64 2d 63 61 63 68 65 20 6c 65 76 65  hared-cache leve
1d980 6c 2e 20 0a 20 20 2a 2a 20 49 66 20 74 68 69 73  l. .  ** If this
1d981 20 76 64 62 65 20 64 69 64 20 63 72 65 61 74 65   vdbe did create
1d982 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
1d983 31 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 69 74  1 table, then it
1d984 20 6d 75 73 74 20 68 61 76 65 20 0a 20 20 2a 2a   must have .  **
1d985 20 61 6c 72 65 61 64 79 20 6f 62 74 61 69 6e 65   already obtaine
1d986 64 20 61 20 73 63 68 65 6d 61 2d 6c 6f 63 6b 2c  d a schema-lock,
1d987 20 6d 61 6b 69 6e 67 20 74 68 65 20 77 72 69 74   making the writ
1d988 65 2d 6c 6f 63 6b 20 72 65 64 75 6e 64 61 6e 74  e-lock redundant
1d989 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 63 72  ..  */.  if( !cr
1d98a 65 61 74 65 53 74 61 74 31 20 29 7b 0a 20 20 20  eateStat1 ){.   
1d98b 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63   sqlite3TableLoc
1d98c 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 69  k(pParse, iDb, i
1d98d 52 6f 6f 74 50 61 67 65 2c 20 31 2c 20 22 73 71  RootPage, 1, "sq
1d98e 6c 69 74 65 5f 73 74 61 74 31 22 29 3b 0a 20 20  lite_stat1");.  
1d98f 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  }.  sqlite3VdbeA
1d990 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 74 4e  ddOp2(v, OP_SetN
1d991 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 33 29  umColumns, 0, 3)
1d992 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
1d993 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp3(v, OP_Open
1d994 57 72 69 74 65 2c 20 69 53 74 61 74 43 75 72 2c  Write, iStatCur,
1d995 20 69 52 6f 6f 74 50 61 67 65 2c 20 69 44 62 29   iRootPage, iDb)
1d996 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43  ;.  sqlite3VdbeC
1d997 68 61 6e 67 65 50 35 28 76 2c 20 63 72 65 61 74  hangeP5(v, creat
1d998 65 53 74 61 74 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  eStat1);.}../*.*
1d999 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
1d99a 74 6f 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69  to do an analysi
1d99b 73 20 6f 66 20 61 6c 6c 20 69 6e 64 69 63 65 73  s of all indices
1d99c 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
1d99d 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 74 61 62  .** a single tab
1d99e 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  le..*/.static vo
1d99f 69 64 20 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62  id analyzeOneTab
1d9a0 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  le(.  Parse *pPa
1d9a1 72 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 65 72  rse,   /* Parser
1d9a2 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61   context */.  Ta
1d9a3 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 2f  ble *pTab,     /
1d9a4 2a 20 54 61 62 6c 65 20 77 68 6f 73 65 20 69 6e  * Table whose in
1d9a5 64 69 63 65 73 20 61 72 65 20 74 6f 20 62 65 20  dices are to be 
1d9a6 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 69 6e  analyzed */.  in
1d9a7 74 20 69 53 74 61 74 43 75 72 2c 20 20 20 20 2f  t iStatCur,    /
1d9a8 2a 20 49 6e 64 65 78 20 6f 66 20 56 64 62 65 43  * Index of VdbeC
1d9a9 75 72 73 6f 72 20 74 68 61 74 20 77 72 69 74 65  ursor that write
1d9aa 73 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  s the sqlite_sta
1d9ab 74 31 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  t1 table */.  in
1d9ac 74 20 69 4d 65 6d 20 20 20 20 20 20 20 20 20 2f  t iMem         /
1d9ad 2a 20 41 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f  * Available memo
1d9ae 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 62 65 67  ry locations beg
1d9af 69 6e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  in here */.){.  
1d9b0 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20  Index *pIdx;    
1d9b1 20 2f 2a 20 41 6e 20 69 6e 64 65 78 20 74 6f 20   /* An index to 
1d9b2 62 65 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 2a  being analyzed *
1d9b3 2f 0a 20 20 69 6e 74 20 69 49 64 78 43 75 72 3b  /.  int iIdxCur;
1d9b4 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
1d9b5 20 56 64 62 65 43 75 72 73 6f 72 20 66 6f 72 20   VdbeCursor for 
1d9b6 69 6e 64 65 78 20 62 65 69 6e 67 20 61 6e 61 6c  index being anal
1d9b7 79 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  yzed */.  int nC
1d9b8 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ol;        /* Nu
1d9b9 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
1d9ba 69 6e 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a  in the index */.
1d9bb 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20    Vdbe *v;      
1d9bc 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
1d9bd 6c 20 6d 61 63 68 69 6e 65 20 62 65 69 6e 67 20  l machine being 
1d9be 62 75 69 6c 74 20 75 70 20 2a 2f 0a 20 20 69 6e  built up */.  in
1d9bf 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 2f  t i;           /
1d9c0 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
1d9c1 2f 0a 20 20 69 6e 74 20 74 6f 70 4f 66 4c 6f 6f  /.  int topOfLoo
1d9c2 70 3b 20 20 20 2f 2a 20 54 68 65 20 74 6f 70 20  p;   /* The top 
1d9c3 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20  of the loop */. 
1d9c4 20 69 6e 74 20 65 6e 64 4f 66 4c 6f 6f 70 3b 20   int endOfLoop; 
1d9c5 20 20 2f 2a 20 54 68 65 20 65 6e 64 20 6f 66 20    /* The end of 
1d9c6 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e  the loop */.  in
1d9c7 74 20 61 64 64 72 3b 20 20 20 20 20 20 20 20 2f  t addr;        /
1d9c8 2a 20 54 68 65 20 61 64 64 72 65 73 73 20 6f 66  * The address of
1d9c9 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20   an instruction 
1d9ca 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  */.  int iDb;   
1d9cb 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
1d9cc 66 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61  f database conta
1d9cd 69 6e 69 6e 67 20 70 54 61 62 20 2a 2f 0a 0a 20  ining pTab */.. 
1d9ce 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
1d9cf 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69  dbe(pParse);.  i
1d9d0 66 28 20 76 3d 3d 30 20 7c 7c 20 70 54 61 62 3d  f( v==0 || pTab=
1d9d1 3d 30 20 7c 7c 20 70 54 61 62 2d 3e 70 49 6e 64  =0 || pTab->pInd
1d9d2 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  ex==0 ){.    /* 
1d9d3 44 6f 20 6e 6f 20 61 6e 61 6c 79 73 69 73 20 66  Do no analysis f
1d9d4 6f 72 20 74 61 62 6c 65 73 20 74 68 61 74 20 68  or tables that h
1d9d5 61 76 65 20 6e 6f 20 69 6e 64 69 63 65 73 20 2a  ave no indices *
1d9d6 2f 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  /.    return;.  
1d9d7 7d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  }.  assert( sqli
1d9d8 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c  te3BtreeHoldsAll
1d9d9 4d 75 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e  Mutexes(pParse->
1d9da 64 62 29 20 29 3b 0a 20 20 69 44 62 20 3d 20 73  db) );.  iDb = s
1d9db 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
1d9dc 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  dex(pParse->db, 
1d9dd 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
1d9de 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30    assert( iDb>=0
1d9df 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   );.#ifndef SQLI
1d9e0 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
1d9e1 41 54 49 4f 4e 0a 20 20 69 66 28 20 73 71 6c 69  ATION.  if( sqli
1d9e2 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
1d9e3 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 4e 41 4c  rse, SQLITE_ANAL
1d9e4 59 5a 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  YZE, pTab->zName
1d9e5 2c 20 30 2c 0a 20 20 20 20 20 20 70 50 61 72 73  , 0,.      pPars
1d9e6 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  e->db->aDb[iDb].
1d9e7 7a 4e 61 6d 65 20 29 20 29 7b 0a 20 20 20 20 72  zName ) ){.    r
1d9e8 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69  eturn;.  }.#endi
1d9e9 66 0a 0a 20 20 2f 2a 20 45 73 74 61 62 6c 69 73  f..  /* Establis
1d9ea 68 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e  h a read-lock on
1d9eb 20 74 68 65 20 74 61 62 6c 65 20 61 74 20 74 68   the table at th
1d9ec 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c  e shared-cache l
1d9ed 65 76 65 6c 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  evel. */.  sqlit
1d9ee 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72  e3TableLock(pPar
1d9ef 73 65 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e 74  se, iDb, pTab->t
1d9f0 6e 75 6d 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e  num, 0, pTab->zN
1d9f1 61 6d 65 29 3b 0a 0a 20 20 69 49 64 78 43 75 72  ame);..  iIdxCur
1d9f2 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b   = pParse->nTab;
1d9f3 0a 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62  .  for(pIdx=pTab
1d9f4 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
1d9f5 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
1d9f6 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a  ){.    KeyInfo *
1d9f7 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e  pKey = sqlite3In
1d9f8 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73  dexKeyinfo(pPars
1d9f9 65 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 69 6e  e, pIdx);.    in
1d9fa 74 20 72 65 67 46 69 65 6c 64 73 3b 20 20 20 20  t regFields;    
1d9fb 2f 2a 20 52 65 67 69 73 74 65 72 20 62 6c 6f 63  /* Register bloc
1d9fc 6b 20 66 6f 72 20 62 75 69 6c 64 69 6e 67 20 72  k for building r
1d9fd 65 63 6f 72 64 73 20 2a 2f 0a 20 20 20 20 69 6e  ecords */.    in
1d9fe 74 20 72 65 67 52 65 63 3b 20 20 20 20 20 20 20  t regRec;       
1d9ff 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64  /* Register hold
1da00 69 6e 67 20 63 6f 6d 70 6c 65 74 65 64 20 72 65  ing completed re
1da01 63 6f 72 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20  cord */.    int 
1da02 72 65 67 54 65 6d 70 3b 20 20 20 20 20 20 2f 2a  regTemp;      /*
1da03 20 54 65 6d 70 6f 72 61 72 79 20 75 73 65 20 72   Temporary use r
1da04 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 69  egister */.    i
1da05 6e 74 20 72 65 67 43 6f 6c 3b 20 20 20 20 20 20  nt regCol;      
1da06 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 61   /* Content of a
1da07 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 74 68 65   column from the
1da08 20 74 61 62 6c 65 20 62 65 69 6e 67 20 61 6e 61   table being ana
1da09 6c 79 7a 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74  lyzed */.    int
1da0a 20 72 65 67 52 6f 77 69 64 3b 20 20 20 20 20 2f   regRowid;     /
1da0b 2a 20 52 6f 77 69 64 20 66 6f 72 20 74 68 65 20  * Rowid for the 
1da0c 69 6e 73 65 72 74 65 64 20 72 65 63 6f 72 64 20  inserted record 
1da0d 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65 67 46 32  */.    int regF2
1da0e 3b 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 61  ;..    /* Open a
1da0f 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 69   cursor to the i
1da10 6e 64 65 78 20 74 6f 20 62 65 20 61 6e 61 6c 79  ndex to be analy
1da11 7a 65 64 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  zed.    */.    a
1da12 73 73 65 72 74 28 20 69 44 62 3d 3d 73 71 6c 69  ssert( iDb==sqli
1da13 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
1da14 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 49 64  (pParse->db, pId
1da15 78 2d 3e 70 53 63 68 65 6d 61 29 20 29 3b 0a 20  x->pSchema) );. 
1da16 20 20 20 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e     nCol = pIdx->
1da17 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 73 71 6c  nColumn;.    sql
1da18 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1da19 2c 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d  , OP_SetNumColum
1da1a 6e 73 2c 20 30 2c 20 6e 43 6f 6c 2b 31 29 3b 0a  ns, 0, nCol+1);.
1da1b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1da1c 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp4(v, OP_Open
1da1d 52 65 61 64 2c 20 69 49 64 78 43 75 72 2c 20 70  Read, iIdxCur, p
1da1e 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a  Idx->tnum, iDb,.
1da1f 20 20 20 20 20 20 20 20 28 63 68 61 72 20 2a 29          (char *)
1da20 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f  pKey, P4_KEYINFO
1da21 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 56  _HANDOFF);.    V
1da22 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
1da23 25 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65  %s", pIdx->zName
1da24 29 29 3b 0a 20 20 20 20 72 65 67 46 69 65 6c 64  ));.    regField
1da25 73 20 3d 20 69 4d 65 6d 2b 6e 43 6f 6c 2a 32 3b  s = iMem+nCol*2;
1da26 0a 20 20 20 20 72 65 67 54 65 6d 70 20 3d 20 72  .    regTemp = r
1da27 65 67 52 6f 77 69 64 20 3d 20 72 65 67 43 6f 6c  egRowid = regCol
1da28 20 3d 20 72 65 67 46 69 65 6c 64 73 2b 33 3b 0a   = regFields+3;.
1da29 20 20 20 20 72 65 67 52 65 63 20 3d 20 72 65 67      regRec = reg
1da2a 43 6f 6c 2b 31 3b 0a 20 20 20 20 69 66 28 20 72  Col+1;.    if( r
1da2b 65 67 52 65 63 3e 70 50 61 72 73 65 2d 3e 6e 4d  egRec>pParse->nM
1da2c 65 6d 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72  em ){.      pPar
1da2d 73 65 2d 3e 6e 4d 65 6d 20 3d 20 72 65 67 52 65  se->nMem = regRe
1da2e 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  c;.    }..    /*
1da2f 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 61 72   Memory cells ar
1da30 65 20 75 73 65 64 20 61 73 20 66 6f 6c 6c 6f 77  e used as follow
1da31 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  s:.    **.    **
1da32 20 20 20 20 6d 65 6d 5b 69 4d 65 6d 5d 3a 20 20      mem[iMem]:  
1da33 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 74             The t
1da34 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72  otal number of r
1da35 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65  ows in the table
1da36 2e 0a 20 20 20 20 2a 2a 20 20 20 20 6d 65 6d 5b  ..    **    mem[
1da37 69 4d 65 6d 2b 31 5d 3a 20 20 20 20 20 20 20 20  iMem+1]:        
1da38 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 64 69 73     Number of dis
1da39 74 69 6e 63 74 20 76 61 6c 75 65 73 20 69 6e 20  tinct values in 
1da3a 63 6f 6c 75 6d 6e 20 31 0a 20 20 20 20 2a 2a 20  column 1.    ** 
1da3b 20 20 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20 20     ....    **   
1da3c 20 6d 65 6d 5b 69 4d 65 6d 2b 6e 43 6f 6c 5d 3a   mem[iMem+nCol]:
1da3d 20 20 20 20 20 20 20 20 4e 75 6d 62 65 72 20 6f          Number o
1da3e 66 20 64 69 73 74 69 6e 63 74 20 76 61 6c 75 65  f distinct value
1da3f 73 20 69 6e 20 63 6f 6c 75 6d 6e 20 4e 0a 20 20  s in column N.  
1da40 20 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d    **    mem[iMem
1da41 2b 6e 43 6f 6c 2b 31 5d 20 20 20 20 20 20 20 4c  +nCol+1]       L
1da42 61 73 74 20 6f 62 73 65 72 76 65 64 20 76 61 6c  ast observed val
1da43 75 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 31 0a 20  ue of column 1. 
1da44 20 20 20 2a 2a 20 20 20 20 2e 2e 2e 0a 20 20 20     **    ....   
1da45 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d 2b   **    mem[iMem+
1da46 6e 43 6f 6c 2b 6e 43 6f 6c 5d 3a 20 20 20 4c 61  nCol+nCol]:   La
1da47 73 74 20 6f 62 73 65 72 76 65 64 20 76 61 6c 75  st observed valu
1da48 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 4e 0a 20 20  e of column N.  
1da49 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 65 6c 6c    **.    ** Cell
1da4a 73 20 69 4d 65 6d 20 74 68 72 6f 75 67 68 20 69  s iMem through i
1da4b 4d 65 6d 2b 6e 43 6f 6c 20 61 72 65 20 69 6e 69  Mem+nCol are ini
1da4c 74 69 61 6c 69 7a 65 64 20 74 6f 20 30 2e 20 20  tialized to 0.  
1da4d 54 68 65 20 6f 74 68 65 72 73 0a 20 20 20 20 2a  The others.    *
1da4e 2a 20 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65  * are initialize
1da4f 64 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a  d to NULL..    *
1da50 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  /.    for(i=0; i
1da51 3c 3d 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  <=nCol; i++){.  
1da52 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1da53 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
1da54 67 65 72 2c 20 30 2c 20 69 4d 65 6d 2b 69 29 3b  ger, 0, iMem+i);
1da55 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69  .    }.    for(i
1da56 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29  =0; i<nCol; i++)
1da57 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
1da58 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1da59 4e 75 6c 6c 2c 20 30 2c 20 69 4d 65 6d 2b 6e 43  Null, 0, iMem+nC
1da5a 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 20 7d 0a 0a  ol+i+1);.    }..
1da5b 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 61 6e      /* Do the an
1da5c 61 6c 79 73 69 73 2e 0a 20 20 20 20 2a 2f 0a 20  alysis..    */. 
1da5d 20 20 20 65 6e 64 4f 66 4c 6f 6f 70 20 3d 20 73     endOfLoop = s
1da5e 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
1da5f 62 65 6c 28 76 29 3b 0a 20 20 20 20 73 71 6c 69  bel(v);.    sqli
1da60 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1da61 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 49 64 78   OP_Rewind, iIdx
1da62 43 75 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b  Cur, endOfLoop);
1da63 0a 20 20 20 20 74 6f 70 4f 66 4c 6f 6f 70 20 3d  .    topOfLoop =
1da64 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
1da65 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20  entAddr(v);.    
1da66 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1da67 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20  2(v, OP_AddImm, 
1da68 69 4d 65 6d 2c 20 31 29 3b 0a 20 20 20 20 66 6f  iMem, 1);.    fo
1da69 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69  r(i=0; i<nCol; i
1da6a 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
1da6b 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
1da6c 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 49 64 78 43  OP_Column, iIdxC
1da6d 75 72 2c 20 69 2c 20 72 65 67 43 6f 6c 29 3b 0a  ur, i, regCol);.
1da6e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1da6f 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65  eAddOp3(v, OP_Ne
1da70 2c 20 72 65 67 43 6f 6c 2c 20 30 2c 20 69 4d 65  , regCol, 0, iMe
1da71 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20  m+nCol+i+1);.   
1da72 20 20 20 2f 2a 2a 2a 2a 20 54 4f 44 4f 3a 20 20     /**** TODO:  
1da73 61 64 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  add collating se
1da74 71 75 65 6e 63 65 20 2a 2a 2a 2a 2a 2f 0a 20 20  quence *****/.  
1da75 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
1da76 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54  hangeP5(v, SQLIT
1da77 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20  E_JUMPIFNULL);. 
1da78 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
1da79 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1da7a 5f 47 6f 74 6f 2c 20 30 2c 20 65 6e 64 4f 66 4c  _Goto, 0, endOfL
1da7b 6f 6f 70 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  oop);.    for(i=
1da7c 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  0; i<nCol; i++){
1da7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1da7e 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 74 6f  beJumpHere(v, to
1da7f 70 4f 66 4c 6f 6f 70 20 2b 20 32 2a 28 69 20 2b  pOfLoop + 2*(i +
1da80 20 31 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69   1));.      sqli
1da81 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1da82 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 69 4d 65 6d   OP_AddImm, iMem
1da83 2b 69 2b 31 2c 20 31 29 3b 0a 20 20 20 20 20 20  +i+1, 1);.      
1da84 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1da85 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
1da86 69 49 64 78 43 75 72 2c 20 69 2c 20 69 4d 65 6d  iIdxCur, i, iMem
1da87 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 20  +nCol+i+1);.    
1da88 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  }.    sqlite3Vdb
1da89 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
1da8a 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20   endOfLoop);.   
1da8b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1da8c 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69  p2(v, OP_Next, i
1da8d 49 64 78 43 75 72 2c 20 74 6f 70 4f 66 4c 6f 6f  IdxCur, topOfLoo
1da8e 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  p);.    sqlite3V
1da8f 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
1da90 43 6c 6f 73 65 2c 20 69 49 64 78 43 75 72 29 3b  Close, iIdxCur);
1da91 0a 0a 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74  ..    /* Store t
1da92 68 65 20 72 65 73 75 6c 74 73 2e 20 20 0a 20 20  he results.  .  
1da93 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20    **.    ** The 
1da94 72 65 73 75 6c 74 20 69 73 20 61 20 73 69 6e 67  result is a sing
1da95 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 20 73 71  le row of the sq
1da96 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65  lite_stat1 table
1da97 2e 20 20 54 68 65 20 66 69 72 73 74 0a 20 20 20  .  The first.   
1da98 20 2a 2a 20 74 77 6f 20 63 6f 6c 75 6d 6e 73 20   ** two columns 
1da99 61 72 65 20 74 68 65 20 6e 61 6d 65 73 20 6f 66  are the names of
1da9a 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 69   the table and i
1da9b 6e 64 65 78 2e 20 20 54 68 65 20 74 68 69 72 64  ndex.  The third
1da9c 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2a 20 69   column.    ** i
1da9d 73 20 61 20 73 74 72 69 6e 67 20 63 6f 6d 70 6f  s a string compo
1da9e 73 65 64 20 6f 66 20 61 20 6c 69 73 74 20 6f 66  sed of a list of
1da9f 20 69 6e 74 65 67 65 72 20 73 74 61 74 69 73 74   integer statist
1daa0 69 63 73 20 61 62 6f 75 74 20 74 68 65 0a 20 20  ics about the.  
1daa1 20 20 2a 2a 20 69 6e 64 65 78 2e 20 20 54 68 65    ** index.  The
1daa2 20 66 69 72 73 74 20 69 6e 74 65 67 65 72 20 69   first integer i
1daa3 6e 20 74 68 65 20 6c 69 73 74 20 69 73 20 74 68  n the list is th
1daa4 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
1daa5 66 20 65 6e 74 69 72 65 73 0a 20 20 20 20 2a 2a  f entires.    **
1daa6 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e 20 20   in the index.  
1daa7 54 68 65 72 65 20 69 73 20 6f 6e 65 20 61 64 64  There is one add
1daa8 69 74 69 6f 6e 61 6c 20 69 6e 74 65 67 65 72 20  itional integer 
1daa9 69 6e 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20  in the list for 
1daaa 65 61 63 68 0a 20 20 20 20 2a 2a 20 63 6f 6c 75  each.    ** colu
1daab 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e  mn of the table.
1daac 20 20 54 68 69 73 20 61 64 64 69 74 69 6f 6e 61    This additiona
1daad 6c 20 69 6e 74 65 67 65 72 20 69 73 20 61 20 67  l integer is a g
1daae 75 65 73 73 20 6f 66 20 68 6f 77 20 6d 61 6e 79  uess of how many
1daaf 0a 20 20 20 20 2a 2a 20 72 6f 77 73 20 6f 66 20  .    ** rows of 
1dab0 74 68 65 20 74 61 62 6c 65 20 74 68 65 20 69 6e  the table the in
1dab1 64 65 78 20 77 69 6c 6c 20 73 65 6c 65 63 74 2e  dex will select.
1dab2 20 20 49 66 20 44 20 69 73 20 74 68 65 20 63 6f    If D is the co
1dab3 75 6e 74 20 6f 66 20 64 69 73 74 69 6e 63 74 0a  unt of distinct.
1dab4 20 20 20 20 2a 2a 20 76 61 6c 75 65 73 20 61 6e      ** values an
1dab5 64 20 4b 20 69 73 20 74 68 65 20 74 6f 74 61 6c  d K is the total
1dab6 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 2c   number of rows,
1dab7 20 74 68 65 6e 20 74 68 65 20 69 6e 74 65 67 65   then the intege
1dab8 72 20 69 73 20 63 6f 6d 70 75 74 65 64 0a 20 20  r is computed.  
1dab9 20 20 2a 2a 20 61 73 3a 0a 20 20 20 20 2a 2a 0a    ** as:.    **.
1daba 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 49 20      **        I 
1dabb 3d 20 28 4b 2b 44 2d 31 29 2f 44 0a 20 20 20 20  = (K+D-1)/D.    
1dabc 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 4b 3d 3d  **.    ** If K==
1dabd 30 20 74 68 65 6e 20 6e 6f 20 65 6e 74 72 79 20  0 then no entry 
1dabe 69 73 20 6d 61 64 65 20 69 6e 74 6f 20 74 68 65  is made into the
1dabf 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
1dac0 62 6c 65 2e 20 20 0a 20 20 20 20 2a 2a 20 49 66  ble.  .    ** If
1dac1 20 4b 3e 30 20 74 68 65 6e 20 69 74 20 69 73 20   K>0 then it is 
1dac2 61 6c 77 61 79 73 20 74 68 65 20 63 61 73 65 20  always the case 
1dac3 74 68 65 20 44 3e 30 20 73 6f 20 64 69 76 69 73  the D>0 so divis
1dac4 69 6f 6e 20 62 79 20 7a 65 72 6f 0a 20 20 20 20  ion by zero.    
1dac5 2a 2a 20 69 73 20 6e 65 76 65 72 20 70 6f 73 73  ** is never poss
1dac6 69 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ible..    */.   
1dac7 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
1dac8 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
1dac9 49 66 4e 6f 74 2c 20 69 4d 65 6d 29 3b 0a 20 20  IfNot, iMem);.  
1daca 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1dacb 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
1dacc 38 2c 20 30 2c 20 72 65 67 46 69 65 6c 64 73 2c  8, 0, regFields,
1dacd 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c   0, pTab->zName,
1dace 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
1dacf 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
1dad0 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67  _String8, 0, reg
1dad1 46 69 65 6c 64 73 2b 31 2c 20 30 2c 20 70 49 64  Fields+1, 0, pId
1dad2 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  x->zName, 0);.  
1dad3 20 20 72 65 67 46 32 20 3d 20 72 65 67 46 69 65    regF2 = regFie
1dad4 6c 64 73 2b 32 3b 0a 20 20 20 20 73 71 6c 69 74  lds+2;.    sqlit
1dad5 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1dad6 4f 50 5f 53 43 6f 70 79 2c 20 69 4d 65 6d 2c 20  OP_SCopy, iMem, 
1dad7 72 65 67 46 32 29 3b 0a 20 20 20 20 66 6f 72 28  regF2);.    for(
1dad8 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b  i=0; i<nCol; i++
1dad9 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1dada 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
1dadb 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67  _String8, 0, reg
1dadc 54 65 6d 70 2c 20 30 2c 20 22 20 22 2c 20 30 29  Temp, 0, " ", 0)
1dadd 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1dade 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
1dadf 43 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d 70 2c  Concat, regTemp,
1dae0 20 72 65 67 46 32 2c 20 72 65 67 46 32 29 3b 0a   regF2, regF2);.
1dae1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1dae2 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 64  eAddOp3(v, OP_Ad
1dae3 64 2c 20 69 4d 65 6d 2c 20 69 4d 65 6d 2b 69 2b  d, iMem, iMem+i+
1dae4 31 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20  1, regTemp);.   
1dae5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1dae6 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d  dOp2(v, OP_AddIm
1dae7 6d 2c 20 72 65 67 54 65 6d 70 2c 20 2d 31 29 3b  m, regTemp, -1);
1dae8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1dae9 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 44  beAddOp3(v, OP_D
1daea 69 76 69 64 65 2c 20 69 4d 65 6d 2b 69 2b 31 2c  ivide, iMem+i+1,
1daeb 20 72 65 67 54 65 6d 70 2c 20 72 65 67 54 65 6d   regTemp, regTem
1daec 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  p);.      sqlite
1daed 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
1daee 50 5f 54 6f 49 6e 74 2c 20 72 65 67 54 65 6d 70  P_ToInt, regTemp
1daef 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1daf0 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
1daf1 5f 43 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d 70  _Concat, regTemp
1daf2 2c 20 72 65 67 46 32 2c 20 72 65 67 46 32 29 3b  , regF2, regF2);
1daf3 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
1daf4 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
1daf5 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72  OP_MakeRecord, r
1daf6 65 67 46 69 65 6c 64 73 2c 20 33 2c 20 72 65 67  egFields, 3, reg
1daf7 52 65 63 2c 20 22 61 61 61 22 2c 20 30 29 3b 0a  Rec, "aaa", 0);.
1daf8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1daf9 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52  ddOp2(v, OP_NewR
1dafa 6f 77 69 64 2c 20 69 53 74 61 74 43 75 72 2c 20  owid, iStatCur, 
1dafb 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73  regRowid);.    s
1dafc 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
1dafd 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69  (v, OP_Insert, i
1dafe 53 74 61 74 43 75 72 2c 20 72 65 67 52 65 63 2c  StatCur, regRec,
1daff 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
1db00 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1db01 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50  eP5(v, OPFLAG_AP
1db02 50 45 4e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74  PEND);.    sqlit
1db03 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
1db04 2c 20 61 64 64 72 29 3b 0a 20 20 7d 0a 7d 0a 0a  , addr);.  }.}..
1db05 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
1db06 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 63 61  ode that will ca
1db07 75 73 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63  use the most rec
1db08 65 6e 74 20 69 6e 64 65 78 20 61 6e 61 6c 79 73  ent index analys
1db09 69 73 20 74 6f 0a 2a 2a 20 62 65 20 6c 61 6f 64  is to.** be laod
1db0a 65 64 20 69 6e 74 6f 20 69 6e 74 65 72 6e 61 6c  ed into internal
1db0b 20 68 61 73 68 20 74 61 62 6c 65 73 20 77 68 65   hash tables whe
1db0c 72 65 20 69 73 20 63 61 6e 20 62 65 20 75 73 65  re is can be use
1db0d 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
1db0e 64 20 6c 6f 61 64 41 6e 61 6c 79 73 69 73 28 50  d loadAnalysis(P
1db0f 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
1db10 74 20 69 44 62 29 7b 0a 20 20 56 64 62 65 20 2a  t iDb){.  Vdbe *
1db11 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
1db12 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
1db13 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ( v ){.    sqlit
1db14 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
1db15 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 2c  OP_LoadAnalysis,
1db16 20 69 44 62 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a   iDb);.  }.}../*
1db17 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
1db18 65 20 74 68 61 74 20 77 69 6c 6c 20 64 6f 20 61  e that will do a
1db19 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 61 6e  n analysis of an
1db1a 20 65 6e 74 69 72 65 20 64 61 74 61 62 61 73 65   entire database
1db1b 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1db1c 61 6e 61 6c 79 7a 65 44 61 74 61 62 61 73 65 28  analyzeDatabase(
1db1d 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
1db1e 6e 74 20 69 44 62 29 7b 0a 20 20 73 71 6c 69 74  nt iDb){.  sqlit
1db1f 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
1db20 3e 64 62 3b 0a 20 20 53 63 68 65 6d 61 20 2a 70  >db;.  Schema *p
1db21 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62  Schema = db->aDb
1db22 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b 20 20  [iDb].pSchema;  
1db23 20 20 2f 2a 20 53 63 68 65 6d 61 20 6f 66 20 64    /* Schema of d
1db24 61 74 61 62 61 73 65 20 69 44 62 20 2a 2f 0a 20  atabase iDb */. 
1db25 20 48 61 73 68 45 6c 65 6d 20 2a 6b 3b 0a 20 20   HashElem *k;.  
1db26 69 6e 74 20 69 53 74 61 74 43 75 72 3b 0a 20 20  int iStatCur;.  
1db27 69 6e 74 20 69 4d 65 6d 3b 0a 0a 20 20 73 71 6c  int iMem;..  sql
1db28 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
1db29 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
1db2a 30 2c 20 69 44 62 29 3b 0a 20 20 69 53 74 61 74  0, iDb);.  iStat
1db2b 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  Cur = pParse->nT
1db2c 61 62 2b 2b 3b 0a 20 20 6f 70 65 6e 53 74 61 74  ab++;.  openStat
1db2d 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44  Table(pParse, iD
1db2e 62 2c 20 69 53 74 61 74 43 75 72 2c 20 30 29 3b  b, iStatCur, 0);
1db2f 0a 20 20 69 4d 65 6d 20 3d 20 70 50 61 72 73 65  .  iMem = pParse
1db30 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 66 6f 72 28  ->nMem+1;.  for(
1db31 6b 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  k=sqliteHashFirs
1db32 74 28 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48  t(&pSchema->tblH
1db33 61 73 68 29 3b 20 6b 3b 20 6b 3d 73 71 6c 69 74  ash); k; k=sqlit
1db34 65 48 61 73 68 4e 65 78 74 28 6b 29 29 7b 0a 20  eHashNext(k)){. 
1db35 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
1db36 20 28 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 48   (Table*)sqliteH
1db37 61 73 68 44 61 74 61 28 6b 29 3b 0a 20 20 20 20  ashData(k);.    
1db38 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28  analyzeOneTable(
1db39 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 53  pParse, pTab, iS
1db3a 74 61 74 43 75 72 2c 20 69 4d 65 6d 29 3b 0a 20  tatCur, iMem);. 
1db3b 20 7d 0a 20 20 6c 6f 61 64 41 6e 61 6c 79 73 69   }.  loadAnalysi
1db3c 73 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a  s(pParse, iDb);.
1db3d 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
1db3e 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c  e code that will
1db3f 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 20   do an analysis 
1db40 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c  of a single tabl
1db41 65 20 69 6e 0a 2a 2a 20 61 20 64 61 74 61 62 61  e in.** a databa
1db42 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  se..*/.static vo
1db43 69 64 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28  id analyzeTable(
1db44 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
1db45 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 69  able *pTab){.  i
1db46 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 69 53  nt iDb;.  int iS
1db47 74 61 74 43 75 72 3b 0a 0a 20 20 61 73 73 65 72  tatCur;..  asser
1db48 74 28 20 70 54 61 62 21 3d 30 20 29 3b 0a 20 20  t( pTab!=0 );.  
1db49 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
1db4a 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65  treeHoldsAllMute
1db4b 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20  xes(pParse->db) 
1db4c 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74  );.  iDb = sqlit
1db4d 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
1db4e 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62  pParse->db, pTab
1db4f 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 73 71  ->pSchema);.  sq
1db50 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f  lite3BeginWriteO
1db51 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c  peration(pParse,
1db52 20 30 2c 20 69 44 62 29 3b 0a 20 20 69 53 74 61   0, iDb);.  iSta
1db53 74 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e  tCur = pParse->n
1db54 54 61 62 2b 2b 3b 0a 20 20 6f 70 65 6e 53 74 61  Tab++;.  openSta
1db55 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69  tTable(pParse, i
1db56 44 62 2c 20 69 53 74 61 74 43 75 72 2c 20 70 54  Db, iStatCur, pT
1db57 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 61 6e  ab->zName);.  an
1db58 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28 70 50  alyzeOneTable(pP
1db59 61 72 73 65 2c 20 70 54 61 62 2c 20 69 53 74 61  arse, pTab, iSta
1db5a 74 43 75 72 2c 20 70 50 61 72 73 65 2d 3e 6e 4d  tCur, pParse->nM
1db5b 65 6d 2b 31 29 3b 0a 20 20 6c 6f 61 64 41 6e 61  em+1);.  loadAna
1db5c 6c 79 73 69 73 28 70 50 61 72 73 65 2c 20 69 44  lysis(pParse, iD
1db5d 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  b);.}../*.** Gen
1db5e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74  erate code for t
1db5f 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61  he ANALYZE comma
1db60 6e 64 2e 20 20 54 68 65 20 70 61 72 73 65 72 20  nd.  The parser 
1db61 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69  calls this routi
1db62 6e 65 0a 2a 2a 20 77 68 65 6e 20 69 74 20 72 65  ne.** when it re
1db63 63 6f 67 6e 69 7a 65 73 20 61 6e 20 41 4e 41 4c  cognizes an ANAL
1db64 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a  YZE command..**.
1db65 2a 2a 20 20 20 20 20 20 20 20 41 4e 41 4c 59 5a  **        ANALYZ
1db66 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
1db67 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20               -- 
1db68 31 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e 41 4c  1.**        ANAL
1db69 59 5a 45 20 20 3c 64 61 74 61 62 61 73 65 3e 20  YZE  <database> 
1db6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d                 -
1db6b 2d 20 32 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e  - 2.**        AN
1db6c 41 4c 59 5a 45 20 20 3f 3c 64 61 74 61 62 61 73  ALYZE  ?<databas
1db6d 65 3e 2e 3f 3c 74 61 62 6c 65 6e 61 6d 65 3e 20  e>.?<tablename> 
1db6e 20 2d 2d 20 33 0a 2a 2a 0a 2a 2a 20 46 6f 72 6d   -- 3.**.** Form
1db6f 20 31 20 63 61 75 73 65 73 20 61 6c 6c 20 69 6e   1 causes all in
1db70 64 69 63 65 73 20 69 6e 20 61 6c 6c 20 61 74 74  dices in all att
1db71 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20  ached databases 
1db72 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e 0a  to be analyzed..
1db73 2a 2a 20 46 6f 72 6d 20 32 20 61 6e 61 6c 79 7a  ** Form 2 analyz
1db74 65 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 74  es all indices t
1db75 68 65 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61  he single databa
1db76 73 65 20 6e 61 6d 65 64 2e 0a 2a 2a 20 46 6f 72  se named..** For
1db77 6d 20 33 20 61 6e 61 6c 79 7a 65 73 20 61 6c 6c  m 3 analyzes all
1db78 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69 61   indices associa
1db79 74 65 64 20 77 69 74 68 20 74 68 65 20 6e 61 6d  ted with the nam
1db7a 65 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c  ed table..*/.SQL
1db7b 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1db7c 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28   sqlite3Analyze(
1db7d 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
1db7e 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 54 6f  oken *pName1, To
1db7f 6b 65 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a 20 20  ken *pName2){.  
1db80 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
1db81 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20  arse->db;.  int 
1db82 69 44 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  iDb;.  int i;.  
1db83 63 68 61 72 20 2a 7a 2c 20 2a 7a 44 62 3b 0a 20  char *z, *zDb;. 
1db84 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
1db85 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d  Token *pTableNam
1db86 65 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68  e;..  /* Read th
1db87 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
1db88 61 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  a. If an error o
1db89 63 63 75 72 73 2c 20 6c 65 61 76 65 20 61 6e 20  ccurs, leave an 
1db8a 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20  error message.  
1db8b 2a 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e 20 70  ** and code in p
1db8c 50 61 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e  Parse and return
1db8d 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73 73 65   NULL. */.  asse
1db8e 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
1db8f 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28  HoldsAllMutexes(
1db90 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20  pParse->db) );. 
1db91 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
1db92 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
1db93 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20  a(pParse) ){.   
1db94 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
1db95 69 66 28 20 70 4e 61 6d 65 31 3d 3d 30 20 29 7b  if( pName1==0 ){
1db96 0a 20 20 20 20 2f 2a 20 46 6f 72 6d 20 31 3a 20  .    /* Form 1: 
1db97 20 41 6e 61 6c 79 7a 65 20 65 76 65 72 79 74 68   Analyze everyth
1db98 69 6e 67 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69  ing */.    for(i
1db99 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
1db9a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69  ++){.      if( i
1db9b 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20  ==1 ) continue; 
1db9c 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 6e 61 6c 79   /* Do not analy
1db9d 7a 65 20 74 68 65 20 54 45 4d 50 20 64 61 74 61  ze the TEMP data
1db9e 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 61 6e  base */.      an
1db9f 61 6c 79 7a 65 44 61 74 61 62 61 73 65 28 70 50  alyzeDatabase(pP
1dba0 61 72 73 65 2c 20 69 29 3b 0a 20 20 20 20 7d 0a  arse, i);.    }.
1dba1 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4e 61 6d    }else if( pNam
1dba2 65 32 3d 3d 30 20 7c 7c 20 70 4e 61 6d 65 32 2d  e2==0 || pName2-
1dba3 3e 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  >n==0 ){.    /* 
1dba4 46 6f 72 6d 20 32 3a 20 20 41 6e 61 6c 79 7a 65  Form 2:  Analyze
1dba5 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72   the database or
1dba6 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 2a 2f 0a   table named */.
1dba7 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65      iDb = sqlite
1dba8 33 46 69 6e 64 44 62 28 64 62 2c 20 70 4e 61 6d  3FindDb(db, pNam
1dba9 65 31 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62  e1);.    if( iDb
1dbaa 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 6e 61  >=0 ){.      ana
1dbab 6c 79 7a 65 44 61 74 61 62 61 73 65 28 70 50 61  lyzeDatabase(pPa
1dbac 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 7d  rse, iDb);.    }
1dbad 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 20 3d 20  else{.      z = 
1dbae 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
1dbaf 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 31 29  oken(db, pName1)
1dbb0 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 20 29 7b  ;.      if( z ){
1dbb1 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20  .        pTab = 
1dbb2 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62  sqlite3LocateTab
1dbb3 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 7a 2c  le(pParse, 0, z,
1dbb4 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   0);.        sql
1dbb5 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
1dbb6 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
1dbb7 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Tab ){.         
1dbb8 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28 70 50   analyzeTable(pP
1dbb9 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20  arse, pTab);.   
1dbba 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
1dbbb 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
1dbbc 20 20 2f 2a 20 46 6f 72 6d 20 33 3a 20 41 6e 61    /* Form 3: Ana
1dbbd 6c 79 7a 65 20 74 68 65 20 66 75 6c 6c 79 20 71  lyze the fully q
1dbbe 75 61 6c 69 66 69 65 64 20 74 61 62 6c 65 20 6e  ualified table n
1dbbf 61 6d 65 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d  ame */.    iDb =
1dbc0 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e   sqlite3TwoPartN
1dbc1 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d  ame(pParse, pNam
1dbc2 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 54 61  e1, pName2, &pTa
1dbc3 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66  bleName);.    if
1dbc4 28 20 69 44 62 3e 3d 30 20 29 7b 0a 20 20 20 20  ( iDb>=0 ){.    
1dbc5 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b    zDb = db->aDb[
1dbc6 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  iDb].zName;.    
1dbc7 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d    z = sqlite3Nam
1dbc8 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70  eFromToken(db, p
1dbc9 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20  TableName);.    
1dbca 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20    if( z ){.     
1dbcb 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65     pTab = sqlite
1dbcc 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61  3LocateTable(pPa
1dbcd 72 73 65 2c 20 30 2c 20 7a 2c 20 7a 44 62 29 3b  rse, 0, z, zDb);
1dbce 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1dbcf 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20  DbFree(db, z);. 
1dbd0 20 20 20 20 20 20 20 69 66 28 20 70 54 61 62 20         if( pTab 
1dbd1 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 6e 61  ){.          ana
1dbd2 6c 79 7a 65 54 61 62 6c 65 28 70 50 61 72 73 65  lyzeTable(pParse
1dbd3 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20 20 20  , pTab);.       
1dbd4 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
1dbd5 20 20 20 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a     .  }.}../*.**
1dbd6 20 55 73 65 64 20 74 6f 20 70 61 73 73 20 69 6e   Used to pass in
1dbd7 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74  formation from t
1dbd8 68 65 20 61 6e 61 6c 79 7a 65 72 20 72 65 61 64  he analyzer read
1dbd9 65 72 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68  er through to th
1dbda 65 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 72 6f  e.** callback ro
1dbdb 75 74 69 6e 65 2e 0a 2a 2f 0a 74 79 70 65 64 65  utine..*/.typede
1dbdc 66 20 73 74 72 75 63 74 20 61 6e 61 6c 79 73 69  f struct analysi
1dbdd 73 49 6e 66 6f 20 61 6e 61 6c 79 73 69 73 49 6e  sInfo analysisIn
1dbde 66 6f 3b 0a 73 74 72 75 63 74 20 61 6e 61 6c 79  fo;.struct analy
1dbdf 73 69 73 49 6e 66 6f 20 7b 0a 20 20 73 71 6c 69  sisInfo {.  sqli
1dbe0 74 65 33 20 2a 64 62 3b 0a 20 20 63 6f 6e 73 74  te3 *db;.  const
1dbe1 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65   char *zDatabase
1dbe2 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ;.};../*.** This
1dbe3 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76   callback is inv
1dbe4 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61  oked once for ea
1dbe5 63 68 20 69 6e 64 65 78 20 77 68 65 6e 20 72 65  ch index when re
1dbe6 61 64 69 6e 67 20 74 68 65 0a 2a 2a 20 73 71 6c  ading the.** sql
1dbe7 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2e  ite_stat1 table.
1dbe8 20 20 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 72 67    .**.**     arg
1dbe9 76 5b 30 5d 20 3d 20 6e 61 6d 65 20 6f 66 20 74  v[0] = name of t
1dbea 68 65 20 69 6e 64 65 78 0a 2a 2a 20 20 20 20 20  he index.**     
1dbeb 61 72 67 76 5b 31 5d 20 3d 20 72 65 73 75 6c 74  argv[1] = result
1dbec 73 20 6f 66 20 61 6e 61 6c 79 73 69 73 20 2d 20  s of analysis - 
1dbed 6f 6e 20 69 6e 74 65 67 65 72 20 66 6f 72 20 65  on integer for e
1dbee 61 63 68 20 63 6f 6c 75 6d 6e 0a 2a 2f 0a 73 74  ach column.*/.st
1dbef 61 74 69 63 20 69 6e 74 20 61 6e 61 6c 79 73 69  atic int analysi
1dbf0 73 4c 6f 61 64 65 72 28 76 6f 69 64 20 2a 70 44  sLoader(void *pD
1dbf1 61 74 61 2c 20 69 6e 74 20 61 72 67 63 2c 20 63  ata, int argc, c
1dbf2 68 61 72 20 2a 2a 61 72 67 76 2c 20 63 68 61 72  har **argv, char
1dbf3 20 2a 2a 61 7a 4e 6f 74 55 73 65 64 29 7b 0a 20   **azNotUsed){. 
1dbf4 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 2a 70   analysisInfo *p
1dbf5 49 6e 66 6f 20 3d 20 28 61 6e 61 6c 79 73 69 73  Info = (analysis
1dbf6 49 6e 66 6f 2a 29 70 44 61 74 61 3b 0a 20 20 49  Info*)pData;.  I
1dbf7 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20  ndex *pIndex;.  
1dbf8 69 6e 74 20 69 2c 20 63 3b 0a 20 20 75 6e 73 69  int i, c;.  unsi
1dbf9 67 6e 65 64 20 69 6e 74 20 76 3b 0a 20 20 63 6f  gned int v;.  co
1dbfa 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20  nst char *z;..  
1dbfb 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 32 20  assert( argc==2 
1dbfc 29 3b 0a 20 20 69 66 28 20 61 72 67 76 3d 3d 30  );.  if( argv==0
1dbfd 20 7c 7c 20 61 72 67 76 5b 30 5d 3d 3d 30 20 7c   || argv[0]==0 |
1dbfe 7c 20 61 72 67 76 5b 31 5d 3d 3d 30 20 29 7b 0a  | argv[1]==0 ){.
1dbff 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
1dc00 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 71 6c  }.  pIndex = sql
1dc01 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 70 49  ite3FindIndex(pI
1dc02 6e 66 6f 2d 3e 64 62 2c 20 61 72 67 76 5b 30 5d  nfo->db, argv[0]
1dc03 2c 20 70 49 6e 66 6f 2d 3e 7a 44 61 74 61 62 61  , pInfo->zDataba
1dc04 73 65 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65  se);.  if( pInde
1dc05 78 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  x==0 ){.    retu
1dc06 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20  rn 0;.  }.  z = 
1dc07 61 72 67 76 5b 31 5d 3b 0a 20 20 66 6f 72 28 69  argv[1];.  for(i
1dc08 3d 30 3b 20 2a 7a 20 26 26 20 69 3c 3d 70 49 6e  =0; *z && i<=pIn
1dc09 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  dex->nColumn; i+
1dc0a 2b 29 7b 0a 20 20 20 20 76 20 3d 20 30 3b 0a 20  +){.    v = 0;. 
1dc0b 20 20 20 77 68 69 6c 65 28 20 28 63 3d 7a 5b 30     while( (c=z[0
1dc0c 5d 29 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39  ])>='0' && c<='9
1dc0d 27 20 29 7b 0a 20 20 20 20 20 20 76 20 3d 20 76  ' ){.      v = v
1dc0e 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20  *10 + c - '0';. 
1dc0f 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a       z++;.    }.
1dc10 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 69 52 6f      pIndex->aiRo
1dc11 77 45 73 74 5b 69 5d 20 3d 20 76 3b 0a 20 20 20  wEst[i] = v;.   
1dc12 20 69 66 28 20 2a 7a 3d 3d 27 20 27 20 29 20 7a   if( *z==' ' ) z
1dc13 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ++;.  }.  return
1dc14 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 61   0;.}../*.** Loa
1dc15 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  d the content of
1dc16 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
1dc17 31 20 74 61 62 6c 65 20 69 6e 74 6f 20 74 68 65  1 table into the
1dc18 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 62 6c   index hash tabl
1dc19 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  es..*/.SQLITE_PR
1dc1a 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1dc1b 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28 73 71  3AnalysisLoad(sq
1dc1c 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69  lite3 *db, int i
1dc1d 44 62 29 7b 0a 20 20 61 6e 61 6c 79 73 69 73 49  Db){.  analysisI
1dc1e 6e 66 6f 20 73 49 6e 66 6f 3b 0a 20 20 48 61 73  nfo sInfo;.  Has
1dc1f 68 45 6c 65 6d 20 2a 69 3b 0a 20 20 63 68 61 72  hElem *i;.  char
1dc20 20 2a 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 72 63   *zSql;.  int rc
1dc21 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  ;..  assert( iDb
1dc22 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e  >=0 && iDb<db->n
1dc23 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Db );.  assert( 
1dc24 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74  db->aDb[iDb].pBt
1dc25 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
1dc26 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
1dc27 64 73 4d 75 74 65 78 28 64 62 2d 3e 61 44 62 5b  dsMutex(db->aDb[
1dc28 69 44 62 5d 2e 70 42 74 29 20 29 3b 0a 0a 20 20  iDb].pBt) );..  
1dc29 2f 2a 20 43 6c 65 61 72 20 61 6e 79 20 70 72 69  /* Clear any pri
1dc2a 6f 72 20 73 74 61 74 69 73 74 69 63 73 20 2a 2f  or statistics */
1dc2b 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 48  .  for(i=sqliteH
1dc2c 61 73 68 46 69 72 73 74 28 26 64 62 2d 3e 61 44  ashFirst(&db->aD
1dc2d 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e  b[iDb].pSchema->
1dc2e 69 64 78 48 61 73 68 29 3b 69 3b 69 3d 73 71 6c  idxHash);i;i=sql
1dc2f 69 74 65 48 61 73 68 4e 65 78 74 28 69 29 29 7b  iteHashNext(i)){
1dc30 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78  .    Index *pIdx
1dc31 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61 74   = sqliteHashDat
1dc32 61 28 69 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  a(i);.    sqlite
1dc33 33 44 65 66 61 75 6c 74 52 6f 77 45 73 74 28 70  3DefaultRowEst(p
1dc34 49 64 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  Idx);.  }..  /* 
1dc35 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75  Check to make su
1dc36 72 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74  re the sqlite_st
1dc37 61 74 31 20 74 61 62 6c 65 20 65 78 69 73 74 73  at1 table exists
1dc38 73 20 2a 2f 0a 20 20 73 49 6e 66 6f 2e 64 62 20  s */.  sInfo.db 
1dc39 3d 20 64 62 3b 0a 20 20 73 49 6e 66 6f 2e 7a 44  = db;.  sInfo.zD
1dc3a 61 74 61 62 61 73 65 20 3d 20 64 62 2d 3e 61 44  atabase = db->aD
1dc3b 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  b[iDb].zName;.  
1dc3c 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54  if( sqlite3FindT
1dc3d 61 62 6c 65 28 64 62 2c 20 22 73 71 6c 69 74 65  able(db, "sqlite
1dc3e 5f 73 74 61 74 31 22 2c 20 73 49 6e 66 6f 2e 7a  _stat1", sInfo.z
1dc3f 44 61 74 61 62 61 73 65 29 3d 3d 30 20 29 7b 0a  Database)==0 ){.
1dc40 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
1dc41 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 0a  TE_ERROR;.  }...
1dc42 20 20 2f 2a 20 4c 6f 61 64 20 6e 65 77 20 73 74    /* Load new st
1dc43 61 74 69 73 74 69 63 73 20 6f 75 74 20 6f 66 20  atistics out of 
1dc44 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31  the sqlite_stat1
1dc45 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7a 53 71 6c   table */.  zSql
1dc46 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
1dc47 66 28 64 62 2c 20 22 53 45 4c 45 43 54 20 69 64  f(db, "SELECT id
1dc48 78 2c 20 73 74 61 74 20 46 52 4f 4d 20 25 51 2e  x, stat FROM %Q.
1dc49 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 0a 20  sqlite_stat1",. 
1dc4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dc4b 20 20 20 20 20 20 20 73 49 6e 66 6f 2e 7a 44 61         sInfo.zDa
1dc4c 74 61 62 61 73 65 29 3b 0a 20 20 28 76 6f 69 64  tabase);.  (void
1dc4d 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66  )sqlite3SafetyOf
1dc4e 66 28 64 62 29 3b 0a 20 20 72 63 20 3d 20 73 71  f(db);.  rc = sq
1dc4f 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a  lite3_exec(db, z
1dc50 53 71 6c 2c 20 61 6e 61 6c 79 73 69 73 4c 6f 61  Sql, analysisLoa
1dc51 64 65 72 2c 20 26 73 49 6e 66 6f 2c 20 30 29 3b  der, &sInfo, 0);
1dc52 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33  .  (void)sqlite3
1dc53 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20  SafetyOn(db);.  
1dc54 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1dc55 2c 20 7a 53 71 6c 29 3b 0a 20 20 72 65 74 75 72  , zSql);.  retur
1dc56 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 65 6e 64 69 66  n rc;.}...#endif
1dc57 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1dc58 41 4e 41 4c 59 5a 45 20 2a 2f 0a 0a 2f 2a 2a 2a  ANALYZE */../***
1dc59 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
1dc5a 6f 66 20 61 6e 61 6c 79 7a 65 2e 63 20 2a 2a 2a  of analyze.c ***
1dc5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dc5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dc5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
1dc5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
1dc5f 6e 20 66 69 6c 65 20 61 74 74 61 63 68 2e 63 20  n file attach.c 
1dc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dc61 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dc62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
1dc63 2a 20 32 30 30 33 20 41 70 72 69 6c 20 36 0a 2a  * 2003 April 6.*
1dc64 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
1dc65 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
1dc66 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
1dc67 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
1dc68 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
1dc69 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
1dc6a 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
1dc6b 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
1dc6c 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
1dc6d 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
1dc6e 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
1dc6f 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
1dc70 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
1dc71 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
1dc72 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
1dc73 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
1dc74 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
1dc75 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
1dc76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dc77 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dc78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dc79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
1dc7a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
1dc7b 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f  ins code used to
1dc7c 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 41   implement the A
1dc7d 54 54 41 43 48 20 61 6e 64 20 44 45 54 41 43 48  TTACH and DETACH
1dc7e 20 63 6f 6d 6d 61 6e 64 73 2e 0a 2a 2a 0a 2a 2a   commands..**.**
1dc7f 20 24 49 64 3a 20 61 74 74 61 63 68 2e 63 2c 76   $Id: attach.c,v
1dc80 20 31 2e 37 39 20 32 30 30 38 2f 31 30 2f 32 38   1.79 2008/10/28
1dc81 20 31 37 3a 35 32 3a 33 39 20 64 61 6e 69 65 6c   17:52:39 daniel
1dc82 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a  k1977 Exp $.*/..
1dc83 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1dc84 4d 49 54 5f 41 54 54 41 43 48 0a 2f 2a 0a 2a 2a  MIT_ATTACH./*.**
1dc85 20 52 65 73 6f 6c 76 65 20 61 6e 20 65 78 70 72   Resolve an expr
1dc86 65 73 73 69 6f 6e 20 74 68 61 74 20 77 61 73 20  ession that was 
1dc87 70 61 72 74 20 6f 66 20 61 6e 20 41 54 54 41 43  part of an ATTAC
1dc88 48 20 6f 72 20 44 45 54 41 43 48 20 73 74 61 74  H or DETACH stat
1dc89 65 6d 65 6e 74 2e 20 54 68 69 73 0a 2a 2a 20 69  ement. This.** i
1dc8a 73 20 73 6c 69 67 68 74 6c 79 20 64 69 66 66 65  s slightly diffe
1dc8b 72 65 6e 74 20 66 72 6f 6d 20 72 65 73 6f 6c 76  rent from resolv
1dc8c 69 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 53 51 4c  ing a normal SQL
1dc8d 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 62 65 63   expression, bec
1dc8e 61 75 73 65 20 73 69 6d 70 6c 65 0a 2a 2a 20 69  ause simple.** i
1dc8f 64 65 6e 74 69 66 69 65 72 73 20 61 72 65 20 74  dentifiers are t
1dc90 72 65 61 74 65 64 20 61 73 20 73 74 72 69 6e 67  reated as string
1dc91 73 2c 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  s, not possible 
1dc92 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 72 20  column names or 
1dc93 61 6c 69 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 69  aliases..**.** i
1dc94 2e 65 2e 20 69 66 20 74 68 65 20 70 61 72 73 65  .e. if the parse
1dc95 72 20 73 65 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  r sees:.**.**   
1dc96 20 20 41 54 54 41 43 48 20 44 41 54 41 42 41 53    ATTACH DATABAS
1dc97 45 20 61 62 63 20 41 53 20 64 65 66 0a 2a 2a 0a  E abc AS def.**.
1dc98 2a 2a 20 69 74 20 74 72 65 61 74 73 20 74 68 65  ** it treats the
1dc99 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e 73   two expressions
1dc9a 20 61 73 20 6c 69 74 65 72 61 6c 20 73 74 72 69   as literal stri
1dc9b 6e 67 73 20 27 61 62 63 27 20 61 6e 64 20 27 64  ngs 'abc' and 'd
1dc9c 65 66 27 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a  ef' instead of.*
1dc9d 2a 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 63 6f  * looking for co
1dc9e 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 73 61 6d  lumns of the sam
1dc9f 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  e name..**.** Th
1dca0 69 73 20 6f 6e 6c 79 20 61 70 70 6c 69 65 73 20  is only applies 
1dca1 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65  to the root node
1dca2 20 6f 66 20 70 45 78 70 72 2c 20 73 6f 20 74 68   of pExpr, so th
1dca3 65 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a  e statement:.**.
1dca4 2a 2a 20 20 20 20 20 41 54 54 41 43 48 20 44 41  **     ATTACH DA
1dca5 54 41 42 41 53 45 20 61 62 63 7c 7c 64 65 66 20  TABASE abc||def 
1dca6 41 53 20 27 64 62 32 27 0a 2a 2a 0a 2a 2a 20 77  AS 'db2'.**.** w
1dca7 69 6c 6c 20 66 61 69 6c 20 62 65 63 61 75 73 65  ill fail because
1dca8 20 6e 65 69 74 68 65 72 20 61 62 63 20 6f 72 20   neither abc or 
1dca9 64 65 66 20 63 61 6e 20 62 65 20 72 65 73 6f 6c  def can be resol
1dcaa 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ved..*/.static i
1dcab 6e 74 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68  nt resolveAttach
1dcac 45 78 70 72 28 4e 61 6d 65 43 6f 6e 74 65 78 74  Expr(NameContext
1dcad 20 2a 70 4e 61 6d 65 2c 20 45 78 70 72 20 2a 70   *pName, Expr *p
1dcae 45 78 70 72 29 0a 7b 0a 20 20 69 6e 74 20 72 63  Expr).{.  int rc
1dcaf 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1dcb0 69 66 28 20 70 45 78 70 72 20 29 7b 0a 20 20 20  if( pExpr ){.   
1dcb1 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d   if( pExpr->op!=
1dcb2 54 4b 5f 49 44 20 29 7b 0a 20 20 20 20 20 20 72  TK_ID ){.      r
1dcb3 63 20 3d 20 73 71 6c 69 74 65 33 52 65 73 6f 6c  c = sqlite3Resol
1dcb4 76 65 45 78 70 72 4e 61 6d 65 73 28 70 4e 61 6d  veExprNames(pNam
1dcb5 65 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20  e, pExpr);.     
1dcb6 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1dcb7 4f 4b 20 26 26 20 21 73 71 6c 69 74 65 33 45 78  OK && !sqlite3Ex
1dcb8 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 70 45 78  prIsConstant(pEx
1dcb9 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73  pr) ){.        s
1dcba 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1dcbb 4e 61 6d 65 2d 3e 70 50 61 72 73 65 2c 20 22 69  Name->pParse, "i
1dcbc 6e 76 61 6c 69 64 20 6e 61 6d 65 3a 20 5c 22 25  nvalid name: \"%
1dcbd 54 5c 22 22 2c 20 26 70 45 78 70 72 2d 3e 73 70  T\"", &pExpr->sp
1dcbe 61 6e 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  an);.        ret
1dcbf 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
1dcc0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
1dcc1 6c 73 65 7b 0a 20 20 20 20 20 20 70 45 78 70 72  lse{.      pExpr
1dcc2 2d 3e 6f 70 20 3d 20 54 4b 5f 53 54 52 49 4e 47  ->op = TK_STRING
1dcc3 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
1dcc4 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1dcc5 2a 20 41 6e 20 53 51 4c 20 75 73 65 72 2d 66 75  * An SQL user-fu
1dcc6 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65  nction registere
1dcc7 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b  d to do the work
1dcc8 20 6f 66 20 61 6e 20 41 54 54 41 43 48 20 73 74   of an ATTACH st
1dcc9 61 74 65 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20  atement. The.** 
1dcca 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 20  three arguments 
1dccb 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  to the function 
1dccc 63 6f 6d 65 20 64 69 72 65 63 74 6c 79 20 66 72  come directly fr
1dccd 6f 6d 20 61 6e 20 61 74 74 61 63 68 20 73 74 61  om an attach sta
1dcce 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20  tement:.**.**   
1dccf 20 20 41 54 54 41 43 48 20 44 41 54 41 42 41 53    ATTACH DATABAS
1dcd0 45 20 78 20 41 53 20 79 20 4b 45 59 20 7a 0a 2a  E x AS y KEY z.*
1dcd1 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20  *.**     SELECT 
1dcd2 73 71 6c 69 74 65 5f 61 74 74 61 63 68 28 78 2c  sqlite_attach(x,
1dcd3 20 79 2c 20 7a 29 0a 2a 2a 0a 2a 2a 20 49 66 20   y, z).**.** If 
1dcd4 74 68 65 20 6f 70 74 69 6f 6e 61 6c 20 22 4b 45  the optional "KE
1dcd5 59 20 7a 22 20 73 79 6e 74 61 78 20 69 73 20 6f  Y z" syntax is o
1dcd6 6d 69 74 74 65 64 2c 20 61 6e 20 53 51 4c 20 4e  mitted, an SQL N
1dcd7 55 4c 4c 20 69 73 20 70 61 73 73 65 64 20 61 73  ULL is passed as
1dcd8 20 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 61 72   the.** third ar
1dcd9 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69  gument..*/.stati
1dcda 63 20 76 6f 69 64 20 61 74 74 61 63 68 46 75 6e  c void attachFun
1dcdb 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
1dcdc 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
1dcdd 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
1dcde 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
1dcdf 76 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  v.){.  int i;.  
1dce0 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 73 71  int rc = 0;.  sq
1dce1 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69  lite3 *db = sqli
1dce2 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
1dce3 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a  andle(context);.
1dce4 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
1dce5 61 6d 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  ame;.  const cha
1dce6 72 20 2a 7a 46 69 6c 65 3b 0a 20 20 44 62 20 2a  r *zFile;.  Db *
1dce7 61 4e 65 77 3b 0a 20 20 63 68 61 72 20 2a 7a 45  aNew;.  char *zE
1dce8 72 72 44 79 6e 20 3d 20 30 3b 0a 20 20 63 68 61  rrDyn = 0;.  cha
1dce9 72 20 7a 45 72 72 5b 31 32 38 5d 3b 0a 0a 20 20  r zErr[128];..  
1dcea 7a 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63  zFile = (const c
1dceb 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  har *)sqlite3_va
1dcec 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
1dced 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 28 63 6f  );.  zName = (co
1dcee 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74  nst char *)sqlit
1dcef 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
1dcf0 67 76 5b 31 5d 29 3b 0a 20 20 69 66 28 20 7a 46  gv[1]);.  if( zF
1dcf1 69 6c 65 3d 3d 30 20 29 20 7a 46 69 6c 65 20 3d  ile==0 ) zFile =
1dcf2 20 22 22 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65   "";.  if( zName
1dcf3 3d 3d 30 20 29 20 7a 4e 61 6d 65 20 3d 20 22 22  ==0 ) zName = ""
1dcf4 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f  ;..  /* Check fo
1dcf5 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  r the following 
1dcf6 65 72 72 6f 72 73 3a 0a 20 20 2a 2a 0a 20 20 2a  errors:.  **.  *
1dcf7 2a 20 20 20 20 20 2a 20 54 6f 6f 20 6d 61 6e 79  *     * Too many
1dcf8 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
1dcf9 73 65 73 2c 0a 20 20 2a 2a 20 20 20 20 20 2a 20  ses,.  **     * 
1dcfa 54 72 61 6e 73 61 63 74 69 6f 6e 20 63 75 72 72  Transaction curr
1dcfb 65 6e 74 6c 79 20 6f 70 65 6e 0a 20 20 2a 2a 20  ently open.  ** 
1dcfc 20 20 20 20 2a 20 53 70 65 63 69 66 69 65 64 20      * Specified 
1dcfd 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 61 6c  database name al
1dcfe 72 65 61 64 79 20 62 65 69 6e 67 20 75 73 65 64  ready being used
1dcff 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d  ..  */.  if( db-
1dd00 3e 6e 44 62 3e 3d 64 62 2d 3e 61 4c 69 6d 69 74  >nDb>=db->aLimit
1dd01 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54  [SQLITE_LIMIT_AT
1dd02 54 41 43 48 45 44 5d 2b 32 20 29 7b 0a 20 20 20  TACHED]+2 ){.   
1dd03 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
1dd04 66 28 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28  f(.      sizeof(
1dd05 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 22 74 6f  zErr), zErr, "to
1dd06 6f 20 6d 61 6e 79 20 61 74 74 61 63 68 65 64 20  o many attached 
1dd07 64 61 74 61 62 61 73 65 73 20 2d 20 6d 61 78 20  databases - max 
1dd08 25 64 22 2c 20 0a 20 20 20 20 20 20 64 62 2d 3e  %d", .      db->
1dd09 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
1dd0a 4d 49 54 5f 41 54 54 41 43 48 45 44 5d 0a 20 20  MIT_ATTACHED].  
1dd0b 20 20 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 74    );.    goto at
1dd0c 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a  tach_error;.  }.
1dd0d 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74 6f 43    if( !db->autoC
1dd0e 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c  ommit ){.    sql
1dd0f 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
1dd10 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72  zeof(zErr), zErr
1dd11 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1dd12 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 41         "cannot A
1dd13 54 54 41 43 48 20 64 61 74 61 62 61 73 65 20 77  TTACH database w
1dd14 69 74 68 69 6e 20 74 72 61 6e 73 61 63 74 69 6f  ithin transactio
1dd15 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 74  n");.    goto at
1dd16 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a  tach_error;.  }.
1dd17 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
1dd18 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
1dd19 63 68 61 72 20 2a 7a 20 3d 20 64 62 2d 3e 61 44  char *z = db->aD
1dd1a 62 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  b[i].zName;.    
1dd1b 69 66 28 20 7a 20 26 26 20 7a 4e 61 6d 65 20 26  if( z && zName &
1dd1c 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  & sqlite3StrICmp
1dd1d 28 7a 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b  (z, zName)==0 ){
1dd1e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
1dd1f 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
1dd20 45 72 72 29 2c 20 7a 45 72 72 2c 20 0a 20 20 20  Err), zErr, .   
1dd21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dd22 20 20 20 20 22 64 61 74 61 62 61 73 65 20 25 73      "database %s
1dd23 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 75   is already in u
1dd24 73 65 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  se", zName);.   
1dd25 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65     goto attach_e
1dd26 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rror;.    }.  }.
1dd27 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74  .  /* Allocate t
1dd28 68 65 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 20  he new entry in 
1dd29 74 68 65 20 64 62 2d 3e 61 44 62 5b 5d 20 61 72  the db->aDb[] ar
1dd2a 72 61 79 20 61 6e 64 20 69 6e 69 74 69 61 6c 69  ray and initiali
1dd2b 73 65 20 74 68 65 20 73 63 68 65 6d 61 0a 20 20  se the schema.  
1dd2c 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 73 2e 0a  ** hash tables..
1dd2d 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 61    */.  if( db->a
1dd2e 44 62 3d 3d 64 62 2d 3e 61 44 62 53 74 61 74 69  Db==db->aDbStati
1dd2f 63 20 29 7b 0a 20 20 20 20 61 4e 65 77 20 3d 20  c ){.    aNew = 
1dd30 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
1dd31 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 64 62  aw(db, sizeof(db
1dd32 2d 3e 61 44 62 5b 30 5d 29 2a 33 20 29 3b 0a 20  ->aDb[0])*3 );. 
1dd33 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29     if( aNew==0 )
1dd34 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 6d 65 6d   return;.    mem
1dd35 63 70 79 28 61 4e 65 77 2c 20 64 62 2d 3e 61 44  cpy(aNew, db->aD
1dd36 62 2c 20 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44  b, sizeof(db->aD
1dd37 62 5b 30 5d 29 2a 32 29 3b 0a 20 20 7d 65 6c 73  b[0])*2);.  }els
1dd38 65 7b 0a 20 20 20 20 61 4e 65 77 20 3d 20 73 71  e{.    aNew = sq
1dd39 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64  lite3DbRealloc(d
1dd3a 62 2c 20 64 62 2d 3e 61 44 62 2c 20 73 69 7a 65  b, db->aDb, size
1dd3b 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d 29 2a 28  of(db->aDb[0])*(
1dd3c 64 62 2d 3e 6e 44 62 2b 31 29 20 29 3b 0a 20 20  db->nDb+1) );.  
1dd3d 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 20    if( aNew==0 ) 
1dd3e 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 64 62  return;.  }.  db
1dd3f 2d 3e 61 44 62 20 3d 20 61 4e 65 77 3b 0a 20 20  ->aDb = aNew;.  
1dd40 61 4e 65 77 20 3d 20 26 64 62 2d 3e 61 44 62 5b  aNew = &db->aDb[
1dd41 64 62 2d 3e 6e 44 62 2b 2b 5d 3b 0a 20 20 6d 65  db->nDb++];.  me
1dd42 6d 73 65 74 28 61 4e 65 77 2c 20 30 2c 20 73 69  mset(aNew, 0, si
1dd43 7a 65 6f 66 28 2a 61 4e 65 77 29 29 3b 0a 0a 20  zeof(*aNew));.. 
1dd44 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 64 61 74   /* Open the dat
1dd45 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74  abase file. If t
1dd46 68 65 20 62 74 72 65 65 20 69 73 20 73 75 63 63  he btree is succ
1dd47 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 2c  essfully opened,
1dd48 20 75 73 65 0a 20 20 2a 2a 20 69 74 20 74 6f 20   use.  ** it to 
1dd49 6f 62 74 61 69 6e 20 74 68 65 20 64 61 74 61 62  obtain the datab
1dd4a 61 73 65 20 73 63 68 65 6d 61 2e 20 41 74 20 74  ase schema. At t
1dd4b 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20 73 63  his point the sc
1dd4c 68 65 6d 61 20 6d 61 79 0a 20 20 2a 2a 20 6f 72  hema may.  ** or
1dd4d 20 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e 69 74   may not be init
1dd4e 69 61 6c 69 73 65 64 2e 0a 20 20 2a 2f 0a 20 20  ialised..  */.  
1dd4f 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1dd50 65 46 61 63 74 6f 72 79 28 64 62 2c 20 7a 46 69  eFactory(db, zFi
1dd51 6c 65 2c 20 30 2c 20 53 51 4c 49 54 45 5f 44 45  le, 0, SQLITE_DE
1dd52 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45  FAULT_CACHE_SIZE
1dd53 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1dd54 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d               db-
1dd55 3e 6f 70 65 6e 46 6c 61 67 73 20 7c 20 53 51 4c  >openFlags | SQL
1dd56 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42  ITE_OPEN_MAIN_DB
1dd57 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1dd58 20 20 20 20 20 20 20 20 20 20 20 20 20 26 61 4e               &aN
1dd59 65 77 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28 20  ew->pBt);.  if( 
1dd5a 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1dd5b 0a 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67  .    Pager *pPag
1dd5c 65 72 3b 0a 20 20 20 20 61 4e 65 77 2d 3e 70 53  er;.    aNew->pS
1dd5d 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 65 33 53  chema = sqlite3S
1dd5e 63 68 65 6d 61 47 65 74 28 64 62 2c 20 61 4e 65  chemaGet(db, aNe
1dd5f 77 2d 3e 70 42 74 29 3b 0a 20 20 20 20 69 66 28  w->pBt);.    if(
1dd60 20 21 61 4e 65 77 2d 3e 70 53 63 68 65 6d 61 20   !aNew->pSchema 
1dd61 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
1dd62 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
1dd63 7d 65 6c 73 65 20 69 66 28 20 61 4e 65 77 2d 3e  }else if( aNew->
1dd64 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f  pSchema->file_fo
1dd65 72 6d 61 74 20 26 26 20 61 4e 65 77 2d 3e 70 53  rmat && aNew->pS
1dd66 63 68 65 6d 61 2d 3e 65 6e 63 21 3d 45 4e 43 28  chema->enc!=ENC(
1dd67 64 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  db) ){.      sql
1dd68 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
1dd69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72  zeof(zErr), zErr
1dd6a 2c 20 0a 20 20 20 20 20 20 20 20 22 61 74 74 61  , .        "atta
1dd6b 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20 6d  ched databases m
1dd6c 75 73 74 20 75 73 65 20 74 68 65 20 73 61 6d 65  ust use the same
1dd6d 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 61   text encoding a
1dd6e 73 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 22  s main database"
1dd6f 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 74  );.      goto at
1dd70 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 20 20  tach_error;.    
1dd71 7d 0a 20 20 20 20 70 50 61 67 65 72 20 3d 20 73  }.    pPager = s
1dd72 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
1dd73 28 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 20  (aNew->pBt);.   
1dd74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63   sqlite3PagerLoc
1dd75 6b 69 6e 67 4d 6f 64 65 28 70 50 61 67 65 72 2c  kingMode(pPager,
1dd76 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64   db->dfltLockMod
1dd77 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50  e);.    sqlite3P
1dd78 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28  agerJournalMode(
1dd79 70 50 61 67 65 72 2c 20 64 62 2d 3e 64 66 6c 74  pPager, db->dflt
1dd7a 4a 6f 75 72 6e 61 6c 4d 6f 64 65 29 3b 0a 20 20  JournalMode);.  
1dd7b 7d 0a 20 20 61 4e 65 77 2d 3e 7a 4e 61 6d 65 20  }.  aNew->zName 
1dd7c 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
1dd7d 70 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  p(db, zName);.  
1dd7e 61 4e 65 77 2d 3e 73 61 66 65 74 79 5f 6c 65 76  aNew->safety_lev
1dd7f 65 6c 20 3d 20 33 3b 0a 0a 23 69 66 20 53 51 4c  el = 3;..#if SQL
1dd80 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20  ITE_HAS_CODEC.  
1dd81 7b 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e 74  {.    extern int
1dd82 20 73 71 6c 69 74 65 33 43 6f 64 65 63 41 74 74   sqlite3CodecAtt
1dd83 61 63 68 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e  ach(sqlite3*, in
1dd84 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20  t, const void*, 
1dd85 69 6e 74 29 3b 0a 20 20 20 20 65 78 74 65 72 6e  int);.    extern
1dd86 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64   void sqlite3Cod
1dd87 65 63 47 65 74 4b 65 79 28 73 71 6c 69 74 65 33  ecGetKey(sqlite3
1dd88 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 2a 2a 2c 20  *, int, void**, 
1dd89 69 6e 74 2a 29 3b 0a 20 20 20 20 69 6e 74 20 6e  int*);.    int n
1dd8a 4b 65 79 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a  Key;.    char *z
1dd8b 4b 65 79 3b 0a 20 20 20 20 69 6e 74 20 74 20 3d  Key;.    int t =
1dd8c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
1dd8d 79 70 65 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20  ype(argv[2]);.  
1dd8e 20 20 73 77 69 74 63 68 28 20 74 20 29 7b 0a 20    switch( t ){. 
1dd8f 20 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45       case SQLITE
1dd90 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 20 20  _INTEGER:.      
1dd91 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41  case SQLITE_FLOA
1dd92 54 3a 0a 20 20 20 20 20 20 20 20 7a 45 72 72 44  T:.        zErrD
1dd93 79 6e 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  yn = sqlite3DbSt
1dd94 72 44 75 70 28 64 62 2c 20 22 49 6e 76 61 6c 69  rDup(db, "Invali
1dd95 64 20 6b 65 79 20 76 61 6c 75 65 22 29 3b 0a 20  d key value");. 
1dd96 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
1dd97 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20  TE_ERROR;.      
1dd98 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
1dd99 20 0a 20 20 20 20 20 20 63 61 73 65 20 53 51 4c   .      case SQL
1dd9a 49 54 45 5f 54 45 58 54 3a 0a 20 20 20 20 20 20  ITE_TEXT:.      
1dd9b 63 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42  case SQLITE_BLOB
1dd9c 3a 0a 20 20 20 20 20 20 20 20 6e 4b 65 79 20 3d  :.        nKey =
1dd9d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
1dd9e 79 74 65 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20  ytes(argv[2]);. 
1dd9f 20 20 20 20 20 20 20 7a 4b 65 79 20 3d 20 28 63         zKey = (c
1dda0 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  har *)sqlite3_va
1dda1 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 32 5d  lue_blob(argv[2]
1dda2 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
1dda3 65 33 43 6f 64 65 63 41 74 74 61 63 68 28 64 62  e3CodecAttach(db
1dda4 2c 20 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a 4b 65  , db->nDb-1, zKe
1dda5 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20  y, nKey);.      
1dda6 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 20 20    break;..      
1dda7 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  case SQLITE_NULL
1dda8 3a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 20  :.        /* No 
1dda9 6b 65 79 20 73 70 65 63 69 66 69 65 64 2e 20 20  key specified.  
1ddaa 55 73 65 20 74 68 65 20 6b 65 79 20 66 72 6f 6d  Use the key from
1ddab 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
1ddac 73 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71  se */.        sq
1ddad 6c 69 74 65 33 43 6f 64 65 63 47 65 74 4b 65 79  lite3CodecGetKey
1ddae 28 64 62 2c 20 30 2c 20 28 76 6f 69 64 2a 2a 29  (db, 0, (void**)
1ddaf 26 7a 4b 65 79 2c 20 26 6e 4b 65 79 29 3b 0a 20  &zKey, &nKey);. 
1ddb0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f         sqlite3Co
1ddb1 64 65 63 41 74 74 61 63 68 28 64 62 2c 20 64 62  decAttach(db, db
1ddb2 2d 3e 6e 44 62 2d 31 2c 20 7a 4b 65 79 2c 20 6e  ->nDb-1, zKey, n
1ddb3 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 62 72  Key);.        br
1ddb4 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  eak;.    }.  }.#
1ddb5 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74  endif..  /* If t
1ddb6 68 65 20 66 69 6c 65 20 77 61 73 20 6f 70 65 6e  he file was open
1ddb7 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c  ed successfully,
1ddb8 20 72 65 61 64 20 74 68 65 20 73 63 68 65 6d 61   read the schema
1ddb9 20 66 6f 72 20 74 68 65 20 6e 65 77 20 64 61 74   for the new dat
1ddba 61 62 61 73 65 2e 0a 20 20 2a 2a 20 49 66 20 74  abase..  ** If t
1ddbb 68 69 73 20 66 61 69 6c 73 2c 20 6f 72 20 69 66  his fails, or if
1ddbc 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 66 69 6c   opening the fil
1ddbd 65 20 66 61 69 6c 65 64 2c 20 74 68 65 6e 20 63  e failed, then c
1ddbe 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 61 6e  lose the file an
1ddbf 64 20 0a 20 20 2a 2a 20 72 65 6d 6f 76 65 20 74  d .  ** remove t
1ddc0 68 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68  he entry from th
1ddc1 65 20 64 62 2d 3e 61 44 62 5b 5d 20 61 72 72 61  e db->aDb[] arra
1ddc2 79 2e 20 69 2e 65 2e 20 70 75 74 20 65 76 65 72  y. i.e. put ever
1ddc3 79 74 68 69 6e 67 20 62 61 63 6b 20 74 68 65 20  ything back the 
1ddc4 77 61 79 0a 20 20 2a 2a 20 77 65 20 66 6f 75 6e  way.  ** we foun
1ddc5 64 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  d it..  */.  if(
1ddc6 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1ddc7 7b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69  {.    (void)sqli
1ddc8 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b  te3SafetyOn(db);
1ddc9 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1ddca 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20  eEnterAll(db);. 
1ddcb 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49     rc = sqlite3I
1ddcc 6e 69 74 28 64 62 2c 20 26 7a 45 72 72 44 79 6e  nit(db, &zErrDyn
1ddcd 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  );.    sqlite3Bt
1ddce 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b  reeLeaveAll(db);
1ddcf 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74  .    (void)sqlit
1ddd0 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
1ddd1 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 20 29 7b  .  }.  if( rc ){
1ddd2 0a 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20 64  .    int iDb = d
1ddd3 62 2d 3e 6e 44 62 20 2d 20 31 3b 0a 20 20 20 20  b->nDb - 1;.    
1ddd4 61 73 73 65 72 74 28 20 69 44 62 3e 3d 32 20 29  assert( iDb>=2 )
1ddd5 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44  ;.    if( db->aD
1ddd6 62 5b 69 44 62 5d 2e 70 42 74 20 29 7b 0a 20 20  b[iDb].pBt ){.  
1ddd7 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1ddd8 43 6c 6f 73 65 28 64 62 2d 3e 61 44 62 5b 69 44  Close(db->aDb[iD
1ddd9 62 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20 20 64  b].pBt);.      d
1ddda 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 20  b->aDb[iDb].pBt 
1dddb 3d 20 30 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61  = 0;.      db->a
1dddc 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 20  Db[iDb].pSchema 
1dddd 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  = 0;.    }.    s
1ddde 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
1dddf 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29  nalSchema(db, 0)
1dde0 3b 0a 20 20 20 20 64 62 2d 3e 6e 44 62 20 3d 20  ;.    db->nDb = 
1dde1 69 44 62 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  iDb;.    if( rc=
1dde2 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c  =SQLITE_NOMEM ||
1dde3 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52   rc==SQLITE_IOER
1dde4 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20  R_NOMEM ){.     
1dde5 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1dde6 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 73 71 6c  d = 1;.      sql
1dde7 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
1dde8 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c  zeof(zErr),zErr,
1dde9 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22   "out of memory"
1ddea 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
1ddeb 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1ddec 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72  intf(sizeof(zErr
1dded 29 2c 7a 45 72 72 2c 20 22 75 6e 61 62 6c 65 20  ),zErr, "unable 
1ddee 74 6f 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65  to open database
1ddef 3a 20 25 73 22 2c 20 7a 46 69 6c 65 29 3b 0a 20  : %s", zFile);. 
1ddf0 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 61 74     }.    goto at
1ddf1 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a  tach_error;.  }.
1ddf2 20 20 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 61 74    .  return;..at
1ddf3 74 61 63 68 5f 65 72 72 6f 72 3a 0a 20 20 2f 2a  tach_error:.  /*
1ddf4 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   Return an error
1ddf5 20 69 66 20 77 65 20 67 65 74 20 68 65 72 65 20   if we get here 
1ddf6 2a 2f 0a 20 20 69 66 28 20 7a 45 72 72 44 79 6e  */.  if( zErrDyn
1ddf7 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
1ddf8 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e  result_error(con
1ddf9 74 65 78 74 2c 20 7a 45 72 72 44 79 6e 2c 20 2d  text, zErrDyn, -
1ddfa 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  1);.    sqlite3D
1ddfb 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 44 79  bFree(db, zErrDy
1ddfc 6e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  n);.  }else{.   
1ddfd 20 7a 45 72 72 5b 73 69 7a 65 6f 66 28 7a 45 72   zErr[sizeof(zEr
1ddfe 72 29 2d 31 5d 20 3d 20 30 3b 0a 20 20 20 20 73  r)-1] = 0;.    s
1ddff 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
1de00 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72  ror(context, zEr
1de01 72 2c 20 2d 31 29 3b 0a 20 20 7d 0a 20 20 69 66  r, -1);.  }.  if
1de02 28 20 72 63 20 29 20 73 71 6c 69 74 65 33 5f 72  ( rc ) sqlite3_r
1de03 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65  esult_error_code
1de04 28 63 6f 6e 74 65 78 74 2c 20 72 63 29 3b 0a 7d  (context, rc);.}
1de05 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20 75  ../*.** An SQL u
1de06 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 72 65 67  ser-function reg
1de07 69 73 74 65 72 65 64 20 74 6f 20 64 6f 20 74 68  istered to do th
1de08 65 20 77 6f 72 6b 20 6f 66 20 61 6e 20 44 45 54  e work of an DET
1de09 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 20 54  ACH statement. T
1de0a 68 65 0a 2a 2a 20 74 68 72 65 65 20 61 72 67 75  he.** three argu
1de0b 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75 6e  ments to the fun
1de0c 63 74 69 6f 6e 20 63 6f 6d 65 20 64 69 72 65 63  ction come direc
1de0d 74 6c 79 20 66 72 6f 6d 20 61 20 64 65 74 61 63  tly from a detac
1de0e 68 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a  h statement:.**.
1de0f 2a 2a 20 20 20 20 20 44 45 54 41 43 48 20 44 41  **     DETACH DA
1de10 54 41 42 41 53 45 20 78 0a 2a 2a 0a 2a 2a 20 20  TABASE x.**.**  
1de11 20 20 20 53 45 4c 45 43 54 20 73 71 6c 69 74 65     SELECT sqlite
1de12 5f 64 65 74 61 63 68 28 78 29 0a 2a 2f 0a 73 74  _detach(x).*/.st
1de13 61 74 69 63 20 76 6f 69 64 20 64 65 74 61 63 68  atic void detach
1de14 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
1de15 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
1de16 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
1de17 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
1de18 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  argv.){.  const 
1de19 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 28 63  char *zName = (c
1de1a 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69  onst char *)sqli
1de1b 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
1de1c 72 67 76 5b 30 5d 29 3b 0a 20 20 73 71 6c 69 74  rgv[0]);.  sqlit
1de1d 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
1de1e 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
1de1f 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 69  le(context);.  i
1de20 6e 74 20 69 3b 0a 20 20 44 62 20 2a 70 44 62 20  nt i;.  Db *pDb 
1de21 3d 20 30 3b 0a 20 20 63 68 61 72 20 7a 45 72 72  = 0;.  char zErr
1de22 5b 31 32 38 5d 3b 0a 0a 20 20 69 66 28 20 7a 4e  [128];..  if( zN
1de23 61 6d 65 3d 3d 30 20 29 20 7a 4e 61 6d 65 20 3d  ame==0 ) zName =
1de24 20 22 22 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20   "";.  for(i=0; 
1de25 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
1de26 0a 20 20 20 20 70 44 62 20 3d 20 26 64 62 2d 3e  .    pDb = &db->
1de27 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20  aDb[i];.    if( 
1de28 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 20 63 6f  pDb->pBt==0 ) co
1de29 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20  ntinue;.    if( 
1de2a 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
1de2b 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65  Db->zName, zName
1de2c 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20  )==0 ) break;.  
1de2d 7d 0a 0a 20 20 69 66 28 20 69 3e 3d 64 62 2d 3e  }..  if( i>=db->
1de2e 6e 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  nDb ){.    sqlit
1de2f 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65  e3_snprintf(size
1de30 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c 20 22  of(zErr),zErr, "
1de31 6e 6f 20 73 75 63 68 20 64 61 74 61 62 61 73 65  no such database
1de32 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  : %s", zName);. 
1de33 20 20 20 67 6f 74 6f 20 64 65 74 61 63 68 5f 65     goto detach_e
1de34 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rror;.  }.  if( 
1de35 69 3c 32 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  i<2 ){.    sqlit
1de36 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65  e3_snprintf(size
1de37 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c 20 22  of(zErr),zErr, "
1de38 63 61 6e 6e 6f 74 20 64 65 74 61 63 68 20 64 61  cannot detach da
1de39 74 61 62 61 73 65 20 25 73 22 2c 20 7a 4e 61 6d  tabase %s", zNam
1de3a 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65 74  e);.    goto det
1de3b 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20  ach_error;.  }. 
1de3c 20 69 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f   if( !db->autoCo
1de3d 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  mmit ){.    sqli
1de3e 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
1de3f 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c  eof(zErr), zErr,
1de40 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1de41 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 44 45        "cannot DE
1de42 54 41 43 48 20 64 61 74 61 62 61 73 65 20 77 69  TACH database wi
1de43 74 68 69 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e  thin transaction
1de44 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65 74  ");.    goto det
1de45 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20  ach_error;.  }. 
1de46 20 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 65   if( sqlite3Btre
1de47 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28 70  eIsInReadTrans(p
1de48 44 62 2d 3e 70 42 74 29 20 29 7b 0a 20 20 20 20  Db->pBt) ){.    
1de49 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1de4a 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45  (sizeof(zErr),zE
1de4b 72 72 2c 20 22 64 61 74 61 62 61 73 65 20 25 73  rr, "database %s
1de4c 20 69 73 20 6c 6f 63 6b 65 64 22 2c 20 7a 4e 61   is locked", zNa
1de4d 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65  me);.    goto de
1de4e 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a  tach_error;.  }.
1de4f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  .  sqlite3BtreeC
1de50 6c 6f 73 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a  lose(pDb->pBt);.
1de51 20 20 70 44 62 2d 3e 70 42 74 20 3d 20 30 3b 0a    pDb->pBt = 0;.
1de52 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 20 3d    pDb->pSchema =
1de53 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 73   0;.  sqlite3Res
1de54 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
1de55 28 64 62 2c 20 30 29 3b 0a 20 20 72 65 74 75 72  (db, 0);.  retur
1de56 6e 3b 0a 0a 64 65 74 61 63 68 5f 65 72 72 6f 72  n;..detach_error
1de57 3a 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  :.  sqlite3_resu
1de58 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74  lt_error(context
1de59 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 7d 0a 0a  , zErr, -1);.}..
1de5a 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 72 6f 63 65  /*.** This proce
1de5b 64 75 72 65 20 67 65 6e 65 72 61 74 65 73 20 56  dure generates V
1de5c 44 42 45 20 63 6f 64 65 20 66 6f 72 20 61 20 73  DBE code for a s
1de5d 69 6e 67 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e  ingle invocation
1de5e 20 6f 66 20 65 69 74 68 65 72 20 74 68 65 0a 2a   of either the.*
1de5f 2a 20 73 71 6c 69 74 65 5f 64 65 74 61 63 68 28  * sqlite_detach(
1de60 29 20 6f 72 20 73 71 6c 69 74 65 5f 61 74 74 61  ) or sqlite_atta
1de61 63 68 28 29 20 53 51 4c 20 75 73 65 72 20 66 75  ch() SQL user fu
1de62 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74  nctions..*/.stat
1de63 69 63 20 76 6f 69 64 20 63 6f 64 65 41 74 74 61  ic void codeAtta
1de64 63 68 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ch(.  Parse *pPa
1de65 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 68  rse,       /* Th
1de66 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
1de67 20 2a 2f 0a 20 20 69 6e 74 20 74 79 70 65 2c 20   */.  int type, 
1de68 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 69             /* Ei
1de69 74 68 65 72 20 53 51 4c 49 54 45 5f 41 54 54 41  ther SQLITE_ATTA
1de6a 43 48 20 6f 72 20 53 51 4c 49 54 45 5f 44 45 54  CH or SQLITE_DET
1de6b 41 43 48 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66  ACH */.  FuncDef
1de6c 20 2a 70 46 75 6e 63 2c 20 20 20 20 20 20 2f 2a   *pFunc,      /*
1de6d 20 46 75 6e 63 44 65 66 20 77 72 61 70 70 65 72   FuncDef wrapper
1de6e 20 66 6f 72 20 64 65 74 61 63 68 46 75 6e 63 28   for detachFunc(
1de6f 29 20 6f 72 20 61 74 74 61 63 68 46 75 6e 63 28  ) or attachFunc(
1de70 29 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 41 75  ) */.  Expr *pAu
1de71 74 68 41 72 67 2c 20 20 20 20 20 20 2f 2a 20 45  thArg,      /* E
1de72 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 70 61 73  xpression to pas
1de73 73 20 74 6f 20 61 75 74 68 6f 72 69 7a 61 74 69  s to authorizati
1de74 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20  on callback */. 
1de75 20 45 78 70 72 20 2a 70 46 69 6c 65 6e 61 6d 65   Expr *pFilename
1de76 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  ,     /* Name of
1de77 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a   database file *
1de78 2f 0a 20 20 45 78 70 72 20 2a 70 44 62 6e 61 6d  /.  Expr *pDbnam
1de79 65 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  e,       /* Name
1de7a 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1de7b 20 74 6f 20 75 73 65 20 69 6e 74 65 72 6e 61 6c   to use internal
1de7c 6c 79 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4b  ly */.  Expr *pK
1de7d 65 79 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ey           /* 
1de7e 44 61 74 61 62 61 73 65 20 6b 65 79 20 66 6f 72  Database key for
1de7f 20 65 6e 63 72 79 70 74 69 6f 6e 20 65 78 74 65   encryption exte
1de80 6e 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e  nsion */.){.  in
1de81 74 20 72 63 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74  t rc;.  NameCont
1de82 65 78 74 20 73 4e 61 6d 65 3b 0a 20 20 56 64 62  ext sName;.  Vdb
1de83 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 2a  e *v;.  sqlite3*
1de84 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62   db = pParse->db
1de85 3b 0a 20 20 69 6e 74 20 72 65 67 41 72 67 73 3b  ;.  int regArgs;
1de86 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1de87 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
1de88 49 4f 4e 0a 20 20 61 73 73 65 72 74 28 20 64 62  ION.  assert( db
1de89 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c  ->mallocFailed |
1de8a 7c 20 70 41 75 74 68 41 72 67 20 29 3b 0a 20 20  | pAuthArg );.  
1de8b 69 66 28 20 70 41 75 74 68 41 72 67 20 29 7b 0a  if( pAuthArg ){.
1de8c 20 20 20 20 63 68 61 72 20 2a 7a 41 75 74 68 41      char *zAuthA
1de8d 72 67 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65  rg = sqlite3Name
1de8e 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 26 70  FromToken(db, &p
1de8f 41 75 74 68 41 72 67 2d 3e 73 70 61 6e 29 3b 0a  AuthArg->span);.
1de90 20 20 20 20 69 66 28 20 21 7a 41 75 74 68 41 72      if( !zAuthAr
1de91 67 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  g ){.      goto 
1de92 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20 20 20  attach_end;.    
1de93 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  }.    rc = sqlit
1de94 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
1de95 73 65 2c 20 74 79 70 65 2c 20 7a 41 75 74 68 41  se, type, zAuthA
1de96 72 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  rg, 0, 0);.    s
1de97 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1de98 20 7a 41 75 74 68 41 72 67 29 3b 0a 20 20 20 20   zAuthArg);.    
1de99 69 66 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  if(rc!=SQLITE_OK
1de9a 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61   ){.      goto a
1de9b 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20 20 20 7d  ttach_end;.    }
1de9c 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
1de9d 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
1de9e 52 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 20 20 6d  RIZATION */..  m
1de9f 65 6d 73 65 74 28 26 73 4e 61 6d 65 2c 20 30 2c  emset(&sName, 0,
1dea0 20 73 69 7a 65 6f 66 28 4e 61 6d 65 43 6f 6e 74   sizeof(NameCont
1dea1 65 78 74 29 29 3b 0a 20 20 73 4e 61 6d 65 2e 70  ext));.  sName.p
1dea2 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a  Parse = pParse;.
1dea3 0a 20 20 69 66 28 20 0a 20 20 20 20 20 20 53 51  .  if( .      SQ
1dea4 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72  LITE_OK!=(rc = r
1dea5 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72  esolveAttachExpr
1dea6 28 26 73 4e 61 6d 65 2c 20 70 46 69 6c 65 6e 61  (&sName, pFilena
1dea7 6d 65 29 29 20 7c 7c 0a 20 20 20 20 20 20 53 51  me)) ||.      SQ
1dea8 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72  LITE_OK!=(rc = r
1dea9 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72  esolveAttachExpr
1deaa 28 26 73 4e 61 6d 65 2c 20 70 44 62 6e 61 6d 65  (&sName, pDbname
1deab 29 29 20 7c 7c 0a 20 20 20 20 20 20 53 51 4c 49  )) ||.      SQLI
1deac 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 73  TE_OK!=(rc = res
1dead 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72 28 26  olveAttachExpr(&
1deae 73 4e 61 6d 65 2c 20 70 4b 65 79 29 29 0a 20 20  sName, pKey)).  
1deaf 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  ){.    pParse->n
1deb0 45 72 72 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20  Err++;.    goto 
1deb1 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20 7d 0a  attach_end;.  }.
1deb2 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65  .  v = sqlite3Ge
1deb3 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
1deb4 20 72 65 67 41 72 67 73 20 3d 20 73 71 6c 69 74   regArgs = sqlit
1deb5 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70  e3GetTempRange(p
1deb6 50 61 72 73 65 2c 20 34 29 3b 0a 20 20 73 71 6c  Parse, 4);.  sql
1deb7 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
1deb8 72 73 65 2c 20 70 46 69 6c 65 6e 61 6d 65 2c 20  rse, pFilename, 
1deb9 72 65 67 41 72 67 73 29 3b 0a 20 20 73 71 6c 69  regArgs);.  sqli
1deba 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
1debb 73 65 2c 20 70 44 62 6e 61 6d 65 2c 20 72 65 67  se, pDbname, reg
1debc 41 72 67 73 2b 31 29 3b 0a 20 20 73 71 6c 69 74  Args+1);.  sqlit
1debd 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
1debe 65 2c 20 70 4b 65 79 2c 20 72 65 67 41 72 67 73  e, pKey, regArgs
1debf 2b 32 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  +2);..  assert( 
1dec0 76 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  v || db->mallocF
1dec1 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 76  ailed );.  if( v
1dec2 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1dec3 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
1dec4 46 75 6e 63 74 69 6f 6e 2c 20 30 2c 20 72 65 67  Function, 0, reg
1dec5 41 72 67 73 2b 33 2d 70 46 75 6e 63 2d 3e 6e 41  Args+3-pFunc->nA
1dec6 72 67 2c 20 72 65 67 41 72 67 73 2b 33 29 3b 0a  rg, regArgs+3);.
1dec7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
1dec8 68 61 6e 67 65 50 35 28 76 2c 20 70 46 75 6e 63  hangeP5(v, pFunc
1dec9 2d 3e 6e 41 72 67 29 3b 0a 20 20 20 20 73 71 6c  ->nArg);.    sql
1deca 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
1decb 28 76 2c 20 2d 31 2c 20 28 63 68 61 72 20 2a 29  (v, -1, (char *)
1decc 70 46 75 6e 63 2c 20 50 34 5f 46 55 4e 43 44 45  pFunc, P4_FUNCDE
1decd 46 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 64 65  F);..    /* Code
1dece 20 61 6e 20 4f 50 5f 45 78 70 69 72 65 2e 20 46   an OP_Expire. F
1decf 6f 72 20 61 6e 20 41 54 54 41 43 48 20 73 74 61  or an ATTACH sta
1ded0 74 65 6d 65 6e 74 2c 20 73 65 74 20 50 31 20 74  tement, set P1 t
1ded1 6f 20 74 72 75 65 20 28 65 78 70 69 72 65 20 74  o true (expire t
1ded2 68 69 73 0a 20 20 20 20 2a 2a 20 73 74 61 74 65  his.    ** state
1ded3 6d 65 6e 74 20 6f 6e 6c 79 29 2e 20 46 6f 72 20  ment only). For 
1ded4 44 45 54 41 43 48 2c 20 73 65 74 20 69 74 20 74  DETACH, set it t
1ded5 6f 20 66 61 6c 73 65 20 28 65 78 70 69 72 65 20  o false (expire 
1ded6 61 6c 6c 20 65 78 69 73 74 69 6e 67 0a 20 20 20  all existing.   
1ded7 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 29 2e   ** statements).
1ded8 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
1ded9 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
1deda 20 4f 50 5f 45 78 70 69 72 65 2c 20 28 74 79 70   OP_Expire, (typ
1dedb 65 3d 3d 53 51 4c 49 54 45 5f 41 54 54 41 43 48  e==SQLITE_ATTACH
1dedc 29 29 3b 0a 20 20 7d 0a 20 20 0a 61 74 74 61 63  ));.  }.  .attac
1dedd 68 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33  h_end:.  sqlite3
1dede 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
1dedf 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 73 71 6c  Filename);.  sql
1dee0 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
1dee1 62 2c 20 70 44 62 6e 61 6d 65 29 3b 0a 20 20 73  b, pDbname);.  s
1dee2 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
1dee3 28 64 62 2c 20 70 4b 65 79 29 3b 0a 7d 0a 0a 2f  (db, pKey);.}../
1dee4 2a 0a 2a 2a 20 43 61 6c 6c 65 64 20 62 79 20 74  *.** Called by t
1dee5 68 65 20 70 61 72 73 65 72 20 74 6f 20 63 6f 6d  he parser to com
1dee6 70 69 6c 65 20 61 20 44 45 54 41 43 48 20 73 74  pile a DETACH st
1dee7 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20  atement..**.**  
1dee8 20 20 20 44 45 54 41 43 48 20 70 44 62 6e 61 6d     DETACH pDbnam
1dee9 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  e.*/.SQLITE_PRIV
1deea 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1deeb 44 65 74 61 63 68 28 50 61 72 73 65 20 2a 70 50  Detach(Parse *pP
1deec 61 72 73 65 2c 20 45 78 70 72 20 2a 70 44 62 6e  arse, Expr *pDbn
1deed 61 6d 65 29 7b 0a 20 20 73 74 61 74 69 63 20 46  ame){.  static F
1deee 75 6e 63 44 65 66 20 64 65 74 61 63 68 5f 66 75  uncDef detach_fu
1deef 6e 63 20 3d 20 7b 0a 20 20 20 20 31 2c 20 20 20  nc = {.    1,   
1def0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1def1 6e 41 72 67 20 2a 2f 0a 20 20 20 20 53 51 4c 49  nArg */.    SQLI
1def2 54 45 5f 55 54 46 38 2c 20 20 20 20 20 20 2f 2a  TE_UTF8,      /*
1def3 20 69 50 72 65 66 45 6e 63 20 2a 2f 0a 20 20 20   iPrefEnc */.   
1def4 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
1def5 20 20 20 2f 2a 20 66 6c 61 67 73 20 2a 2f 0a 20     /* flags */. 
1def6 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
1def7 20 20 20 20 20 2f 2a 20 70 55 73 65 72 44 61 74       /* pUserDat
1def8 61 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20  a */.    0,     
1def9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e             /* pN
1defa 65 78 74 20 2a 2f 0a 20 20 20 20 64 65 74 61 63  ext */.    detac
1defb 68 46 75 6e 63 2c 20 20 20 20 20 20 20 2f 2a 20  hFunc,       /* 
1defc 78 46 75 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20  xFunc */.    0, 
1defd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1defe 2a 20 78 53 74 65 70 20 2a 2f 0a 20 20 20 20 30  * xStep */.    0
1deff 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1df00 20 2f 2a 20 78 46 69 6e 61 6c 69 7a 65 20 2a 2f   /* xFinalize */
1df01 0a 20 20 20 20 22 73 71 6c 69 74 65 5f 64 65 74  .    "sqlite_det
1df02 61 63 68 22 2c 20 20 2f 2a 20 7a 4e 61 6d 65 20  ach",  /* zName 
1df03 2a 2f 0a 20 20 20 20 30 20 20 20 20 20 20 20 20  */.    0        
1df04 20 20 20 20 20 20 20 20 20 2f 2a 20 70 48 61 73           /* pHas
1df05 68 20 2a 2f 0a 20 20 7d 3b 0a 20 20 63 6f 64 65  h */.  };.  code
1df06 41 74 74 61 63 68 28 70 50 61 72 73 65 2c 20 53  Attach(pParse, S
1df07 51 4c 49 54 45 5f 44 45 54 41 43 48 2c 20 26 64  QLITE_DETACH, &d
1df08 65 74 61 63 68 5f 66 75 6e 63 2c 20 70 44 62 6e  etach_func, pDbn
1df09 61 6d 65 2c 20 30 2c 20 30 2c 20 70 44 62 6e 61  ame, 0, 0, pDbna
1df0a 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61  me);.}../*.** Ca
1df0b 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
1df0c 65 72 20 74 6f 20 63 6f 6d 70 69 6c 65 20 61 6e  er to compile an
1df0d 20 41 54 54 41 43 48 20 73 74 61 74 65 6d 65 6e   ATTACH statemen
1df0e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54  t..**.**     ATT
1df0f 41 43 48 20 70 20 41 53 20 70 44 62 6e 61 6d 65  ACH p AS pDbname
1df10 20 4b 45 59 20 70 4b 65 79 0a 2a 2f 0a 53 51 4c   KEY pKey.*/.SQL
1df11 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1df12 20 73 71 6c 69 74 65 33 41 74 74 61 63 68 28 50   sqlite3Attach(P
1df13 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78  arse *pParse, Ex
1df14 70 72 20 2a 70 2c 20 45 78 70 72 20 2a 70 44 62  pr *p, Expr *pDb
1df15 6e 61 6d 65 2c 20 45 78 70 72 20 2a 70 4b 65 79  name, Expr *pKey
1df16 29 7b 0a 20 20 73 74 61 74 69 63 20 46 75 6e 63  ){.  static Func
1df17 44 65 66 20 61 74 74 61 63 68 5f 66 75 6e 63 20  Def attach_func 
1df18 3d 20 7b 0a 20 20 20 20 33 2c 20 20 20 20 20 20  = {.    3,      
1df19 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 41 72            /* nAr
1df1a 67 20 2a 2f 0a 20 20 20 20 53 51 4c 49 54 45 5f  g */.    SQLITE_
1df1b 55 54 46 38 2c 20 20 20 20 20 20 2f 2a 20 69 50  UTF8,      /* iP
1df1c 72 65 66 45 6e 63 20 2a 2f 0a 20 20 20 20 30 2c  refEnc */.    0,
1df1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1df1e 2f 2a 20 66 6c 61 67 73 20 2a 2f 0a 20 20 20 20  /* flags */.    
1df1f 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
1df20 20 20 2f 2a 20 70 55 73 65 72 44 61 74 61 20 2a    /* pUserData *
1df21 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  /.    0,        
1df22 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74          /* pNext
1df23 20 2a 2f 0a 20 20 20 20 61 74 74 61 63 68 46 75   */.    attachFu
1df24 6e 63 2c 20 20 20 20 20 20 20 2f 2a 20 78 46 75  nc,       /* xFu
1df25 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20  nc */.    0,    
1df26 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
1df27 53 74 65 70 20 2a 2f 0a 20 20 20 20 30 2c 20 20  Step */.    0,  
1df28 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1df29 20 78 46 69 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20   xFinalize */.  
1df2a 20 20 22 73 71 6c 69 74 65 5f 61 74 74 61 63 68    "sqlite_attach
1df2b 22 2c 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a  ",  /* zName */.
1df2c 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20      0           
1df2d 20 20 20 20 20 20 2f 2a 20 70 48 61 73 68 20 2a        /* pHash *
1df2e 2f 0a 20 20 7d 3b 0a 20 20 63 6f 64 65 41 74 74  /.  };.  codeAtt
1df2f 61 63 68 28 70 50 61 72 73 65 2c 20 53 51 4c 49  ach(pParse, SQLI
1df30 54 45 5f 41 54 54 41 43 48 2c 20 26 61 74 74 61  TE_ATTACH, &atta
1df31 63 68 5f 66 75 6e 63 2c 20 70 2c 20 70 2c 20 70  ch_func, p, p, p
1df32 44 62 6e 61 6d 65 2c 20 70 4b 65 79 29 3b 0a 7d  Dbname, pKey);.}
1df33 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1df34 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48 20 2a 2f  E_OMIT_ATTACH */
1df35 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  ../*.** Initiali
1df36 7a 65 20 61 20 44 62 46 69 78 65 72 20 73 74 72  ze a DbFixer str
1df37 75 63 74 75 72 65 2e 20 20 54 68 69 73 20 72 6f  ucture.  This ro
1df38 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61  utine must be ca
1df39 6c 6c 65 64 20 70 72 69 6f 72 0a 2a 2a 20 74 6f  lled prior.** to
1df3a 20 70 61 73 73 69 6e 67 20 74 68 65 20 73 74 72   passing the str
1df3b 75 63 74 75 72 65 20 74 6f 20 6f 6e 65 20 6f 66  ucture to one of
1df3c 20 74 68 65 20 73 71 6c 69 74 65 46 69 78 41 41   the sqliteFixAA
1df3d 41 41 28 29 20 72 6f 75 74 69 6e 65 73 20 62 65  AA() routines be
1df3e 6c 6f 77 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  low..**.** The r
1df3f 65 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 64 69  eturn value indi
1df40 63 61 74 65 73 20 77 68 65 74 68 65 72 20 6f 72  cates whether or
1df41 20 6e 6f 74 20 66 69 78 61 74 69 6f 6e 20 69 73   not fixation is
1df42 20 72 65 71 75 69 72 65 64 2e 20 20 54 52 55 45   required.  TRUE
1df43 0a 2a 2a 20 6d 65 61 6e 73 20 77 65 20 64 6f 20  .** means we do 
1df44 6e 65 65 64 20 74 6f 20 66 69 78 20 74 68 65 20  need to fix the 
1df45 64 61 74 61 62 61 73 65 20 72 65 66 65 72 65 6e  database referen
1df46 63 65 73 2c 20 46 41 4c 53 45 20 6d 65 61 6e 73  ces, FALSE means
1df47 20 77 65 20 64 6f 20 6e 6f 74 2e 0a 2a 2f 0a 53   we do not..*/.S
1df48 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1df49 74 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74  t sqlite3FixInit
1df4a 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46 69  (.  DbFixer *pFi
1df4b 78 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66  x,      /* The f
1df4c 69 78 65 72 20 74 6f 20 62 65 20 69 6e 69 74 69  ixer to be initi
1df4d 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 50 61 72 73  alized */.  Pars
1df4e 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
1df4f 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65  /* Error message
1df50 73 20 77 69 6c 6c 20 62 65 20 77 72 69 74 74 65  s will be writte
1df51 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20  n here */.  int 
1df52 69 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  iDb,            
1df53 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 64  /* This is the d
1df54 61 74 61 62 61 73 65 20 74 68 61 74 20 6d 75 73  atabase that mus
1df55 74 20 62 65 20 75 73 65 64 20 2a 2f 0a 20 20 63  t be used */.  c
1df56 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65  onst char *zType
1df57 2c 20 20 2f 2a 20 22 76 69 65 77 22 2c 20 22 74  ,  /* "view", "t
1df58 72 69 67 67 65 72 22 2c 20 6f 72 20 22 69 6e 64  rigger", or "ind
1df59 65 78 22 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54  ex" */.  const T
1df5a 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 20 2f 2a 20  oken *pName  /* 
1df5b 4e 61 6d 65 20 6f 66 20 74 68 65 20 76 69 65 77  Name of the view
1df5c 2c 20 74 72 69 67 67 65 72 2c 20 6f 72 20 69 6e  , trigger, or in
1df5d 64 65 78 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  dex */.){.  sqli
1df5e 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20  te3 *db;..  if( 
1df5f 69 44 62 3c 30 20 7c 7c 20 69 44 62 3d 3d 31 20  iDb<0 || iDb==1 
1df60 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 64 62  ) return 0;.  db
1df61 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
1df62 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6e 44 62   assert( db->nDb
1df63 3e 69 44 62 20 29 3b 0a 20 20 70 46 69 78 2d 3e  >iDb );.  pFix->
1df64 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b  pParse = pParse;
1df65 0a 20 20 70 46 69 78 2d 3e 7a 44 62 20 3d 20 64  .  pFix->zDb = d
1df66 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
1df67 65 3b 0a 20 20 70 46 69 78 2d 3e 7a 54 79 70 65  e;.  pFix->zType
1df68 20 3d 20 7a 54 79 70 65 3b 0a 20 20 70 46 69 78   = zType;.  pFix
1df69 2d 3e 70 4e 61 6d 65 20 3d 20 70 4e 61 6d 65 3b  ->pName = pName;
1df6a 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
1df6b 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
1df6c 69 6e 67 20 73 65 74 20 6f 66 20 72 6f 75 74 69  ing set of routi
1df6d 6e 65 73 20 77 61 6c 6b 20 74 68 72 6f 75 67 68  nes walk through
1df6e 20 74 68 65 20 70 61 72 73 65 20 74 72 65 65 20   the parse tree 
1df6f 61 6e 64 20 61 73 73 69 67 6e 0a 2a 2a 20 61 20  and assign.** a 
1df70 73 70 65 63 69 66 69 63 20 64 61 74 61 62 61 73  specific databas
1df71 65 20 74 6f 20 61 6c 6c 20 74 61 62 6c 65 20 72  e to all table r
1df72 65 66 65 72 65 6e 63 65 73 20 77 68 65 72 65 20  eferences where 
1df73 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d  the database nam
1df74 65 0a 2a 2a 20 77 61 73 20 6c 65 66 74 20 75 6e  e.** was left un
1df75 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65  specified in the
1df76 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74   original SQL st
1df77 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 70 46  atement.  The pF
1df78 69 78 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  ix structure.** 
1df79 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 69  must have been i
1df7a 6e 69 74 69 61 6c 69 7a 65 64 20 62 79 20 61 20  nitialized by a 
1df7b 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71  prior call to sq
1df7c 6c 69 74 65 33 46 69 78 49 6e 69 74 28 29 2e 0a  lite3FixInit()..
1df7d 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  **.** These rout
1df7e 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f  ines are used to
1df7f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   make sure that 
1df80 61 6e 20 69 6e 64 65 78 2c 20 74 72 69 67 67 65  an index, trigge
1df81 72 2c 20 6f 72 0a 2a 2a 20 76 69 65 77 20 69 6e  r, or.** view in
1df82 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 64 6f   one database do
1df83 65 73 20 6e 6f 74 20 72 65 66 65 72 20 74 6f 20  es not refer to 
1df84 6f 62 6a 65 63 74 73 20 69 6e 20 61 20 64 69 66  objects in a dif
1df85 66 65 72 65 6e 74 20 64 61 74 61 62 61 73 65 2e  ferent database.
1df86 0a 2a 2a 20 28 45 78 63 65 70 74 69 6f 6e 3a 20  .** (Exception: 
1df87 69 6e 64 69 63 65 73 2c 20 74 72 69 67 67 65 72  indices, trigger
1df88 73 2c 20 61 6e 64 20 76 69 65 77 73 20 69 6e 20  s, and views in 
1df89 74 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73  the TEMP databas
1df8a 65 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f 77 65 64  e are.** allowed
1df8b 20 74 6f 20 72 65 66 65 72 20 74 6f 20 61 6e 79   to refer to any
1df8c 74 68 69 6e 67 2e 29 20 20 49 66 20 61 20 72 65  thing.)  If a re
1df8d 66 65 72 65 6e 63 65 20 69 73 20 65 78 70 6c 69  ference is expli
1df8e 63 69 74 6c 79 20 6d 61 64 65 0a 2a 2a 20 74 6f  citly made.** to
1df8f 20 61 6e 20 6f 62 6a 65 63 74 20 69 6e 20 61 20   an object in a 
1df90 64 69 66 66 65 72 65 6e 74 20 64 61 74 61 62 61  different databa
1df91 73 65 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  se, an error mes
1df92 73 61 67 65 20 69 73 20 61 64 64 65 64 20 74 6f  sage is added to
1df93 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 7a 45 72 72  .** pParse->zErr
1df94 4d 73 67 20 61 6e 64 20 74 68 65 73 65 20 72 6f  Msg and these ro
1df95 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 6e 6f  utines return no
1df96 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 65 76 65 72  n-zero.  If ever
1df97 79 74 68 69 6e 67 0a 2a 2a 20 63 68 65 63 6b 73  ything.** checks
1df98 20 6f 75 74 2c 20 74 68 65 73 65 20 72 6f 75 74   out, these rout
1df99 69 6e 65 73 20 72 65 74 75 72 6e 20 30 2e 0a 2a  ines return 0..*
1df9a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1df9b 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53   int sqlite3FixS
1df9c 72 63 4c 69 73 74 28 0a 20 20 44 62 46 69 78 65  rcList(.  DbFixe
1df9d 72 20 2a 70 46 69 78 2c 20 20 20 20 20 20 20 2f  r *pFix,       /
1df9e 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65  * Context of the
1df9f 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 53   fixation */.  S
1dfa0 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 20 20 20  rcList *pList   
1dfa1 20 20 20 20 2f 2a 20 54 68 65 20 53 6f 75 72 63      /* The Sourc
1dfa2 65 20 6c 69 73 74 20 74 6f 20 63 68 65 63 6b 20  e list to check 
1dfa3 61 6e 64 20 6d 6f 64 69 66 79 20 2a 2f 0a 29 7b  and modify */.){
1dfa4 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e 73  .  int i;.  cons
1dfa5 74 20 63 68 61 72 20 2a 7a 44 62 3b 0a 20 20 73  t char *zDb;.  s
1dfa6 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
1dfa7 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 69 66  em *pItem;..  if
1dfa8 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74  ( pList==0 ) ret
1dfa9 75 72 6e 20 30 3b 0a 20 20 7a 44 62 20 3d 20 70  urn 0;.  zDb = p
1dfaa 46 69 78 2d 3e 7a 44 62 3b 0a 20 20 66 6f 72 28  Fix->zDb;.  for(
1dfab 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74  i=0, pItem=pList
1dfac 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 53  ->a; i<pList->nS
1dfad 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b  rc; i++, pItem++
1dfae 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74 65 6d  ){.    if( pItem
1dfaf 2d 3e 7a 44 61 74 61 62 61 73 65 3d 3d 30 20 29  ->zDatabase==0 )
1dfb0 7b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 7a  {.      pItem->z
1dfb1 44 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74  Database = sqlit
1dfb2 65 33 44 62 53 74 72 44 75 70 28 70 46 69 78 2d  e3DbStrDup(pFix-
1dfb3 3e 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 44 62  >pParse->db, zDb
1dfb4 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  );.    }else if(
1dfb5 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
1dfb6 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65  pItem->zDatabase
1dfb7 2c 7a 44 62 29 21 3d 30 20 29 7b 0a 20 20 20 20  ,zDb)!=0 ){.    
1dfb8 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1dfb9 67 28 70 46 69 78 2d 3e 70 50 61 72 73 65 2c 0a  g(pFix->pParse,.
1dfba 20 20 20 20 20 20 20 20 20 22 25 73 20 25 54 20           "%s %T 
1dfbb 63 61 6e 6e 6f 74 20 72 65 66 65 72 65 6e 63 65  cannot reference
1dfbc 20 6f 62 6a 65 63 74 73 20 69 6e 20 64 61 74 61   objects in data
1dfbd 62 61 73 65 20 25 73 22 2c 0a 20 20 20 20 20 20  base %s",.      
1dfbe 20 20 20 70 46 69 78 2d 3e 7a 54 79 70 65 2c 20     pFix->zType, 
1dfbf 70 46 69 78 2d 3e 70 4e 61 6d 65 2c 20 70 49 74  pFix->pName, pIt
1dfc0 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a  em->zDatabase);.
1dfc1 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
1dfc2 20 20 20 20 7d 0a 23 69 66 20 21 64 65 66 69 6e      }.#if !defin
1dfc3 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
1dfc4 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64  IEW) || !defined
1dfc5 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49  (SQLITE_OMIT_TRI
1dfc6 47 47 45 52 29 0a 20 20 20 20 69 66 28 20 73 71  GGER).    if( sq
1dfc7 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 70  lite3FixSelect(p
1dfc8 46 69 78 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c  Fix, pItem->pSel
1dfc9 65 63 74 29 20 29 20 72 65 74 75 72 6e 20 31 3b  ect) ) return 1;
1dfca 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1dfcb 46 69 78 45 78 70 72 28 70 46 69 78 2c 20 70 49  FixExpr(pFix, pI
1dfcc 74 65 6d 2d 3e 70 4f 6e 29 20 29 20 72 65 74 75  tem->pOn) ) retu
1dfcd 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  rn 1;.#endif.  }
1dfce 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23  .  return 0;.}.#
1dfcf 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
1dfd0 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c  TE_OMIT_VIEW) ||
1dfd1 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1dfd2 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 53  _OMIT_TRIGGER).S
1dfd3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1dfd4 74 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65  t sqlite3FixSele
1dfd5 63 74 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70  ct(.  DbFixer *p
1dfd6 46 69 78 2c 20 20 20 20 20 20 20 2f 2a 20 43 6f  Fix,       /* Co
1dfd7 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66 69 78  ntext of the fix
1dfd8 61 74 69 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65 63  ation */.  Selec
1dfd9 74 20 2a 70 53 65 6c 65 63 74 20 20 20 20 20 20  t *pSelect      
1dfda 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74  /* The SELECT st
1dfdb 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 20 66 69  atement to be fi
1dfdc 78 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61 62  xed to one datab
1dfdd 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c  ase */.){.  whil
1dfde 65 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20  e( pSelect ){.  
1dfdf 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78    if( sqlite3Fix
1dfe0 45 78 70 72 4c 69 73 74 28 70 46 69 78 2c 20 70  ExprList(pFix, p
1dfe1 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 29 20  Select->pEList) 
1dfe2 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
1dfe3 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  1;.    }.    if(
1dfe4 20 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69   sqlite3FixSrcLi
1dfe5 73 74 28 70 46 69 78 2c 20 70 53 65 6c 65 63 74  st(pFix, pSelect
1dfe6 2d 3e 70 53 72 63 29 20 29 7b 0a 20 20 20 20 20  ->pSrc) ){.     
1dfe7 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
1dfe8 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1dfe9 46 69 78 45 78 70 72 28 70 46 69 78 2c 20 70 53  FixExpr(pFix, pS
1dfea 65 6c 65 63 74 2d 3e 70 57 68 65 72 65 29 20 29  elect->pWhere) )
1dfeb 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
1dfec 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1dfed 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 70  sqlite3FixExpr(p
1dfee 46 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 48  Fix, pSelect->pH
1dfef 61 76 69 6e 67 29 20 29 7b 0a 20 20 20 20 20 20  aving) ){.      
1dff0 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
1dff1 20 20 20 20 70 53 65 6c 65 63 74 20 3d 20 70 53      pSelect = pS
1dff2 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 3b 0a 20  elect->pPrior;. 
1dff3 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
1dff4 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1dff5 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 45 78  int sqlite3FixEx
1dff6 70 72 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70  pr(.  DbFixer *p
1dff7 46 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f 6e 74  Fix,     /* Cont
1dff8 65 78 74 20 6f 66 20 74 68 65 20 66 69 78 61 74  ext of the fixat
1dff9 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ion */.  Expr *p
1dffa 45 78 70 72 20 20 20 20 20 20 20 20 2f 2a 20 54  Expr        /* T
1dffb 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f  he expression to
1dffc 20 62 65 20 66 69 78 65 64 20 74 6f 20 6f 6e 65   be fixed to one
1dffd 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b 0a   database */.){.
1dffe 20 20 77 68 69 6c 65 28 20 70 45 78 70 72 20 29    while( pExpr )
1dfff 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
1e000 33 46 69 78 53 65 6c 65 63 74 28 70 46 69 78 2c  3FixSelect(pFix,
1e001 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 29   pExpr->pSelect)
1e002 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
1e003 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   1;.    }.    if
1e004 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72  ( sqlite3FixExpr
1e005 4c 69 73 74 28 70 46 69 78 2c 20 70 45 78 70 72  List(pFix, pExpr
1e006 2d 3e 70 4c 69 73 74 29 20 29 7b 0a 20 20 20 20  ->pList) ){.    
1e007 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
1e008 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  }.    if( sqlite
1e009 33 46 69 78 45 78 70 72 28 70 46 69 78 2c 20 70  3FixExpr(pFix, p
1e00a 45 78 70 72 2d 3e 70 52 69 67 68 74 29 20 29 7b  Expr->pRight) ){
1e00b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
1e00c 0a 20 20 20 20 7d 0a 20 20 20 20 70 45 78 70 72  .    }.    pExpr
1e00d 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b   = pExpr->pLeft;
1e00e 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
1e00f 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
1e010 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78  E int sqlite3Fix
1e011 45 78 70 72 4c 69 73 74 28 0a 20 20 44 62 46 69  ExprList(.  DbFi
1e012 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20 2f  xer *pFix,     /
1e013 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65  * Context of the
1e014 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 45   fixation */.  E
1e015 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 20  xprList *pList  
1e016 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73    /* The express
1e017 69 6f 6e 20 74 6f 20 62 65 20 66 69 78 65 64 20  ion to be fixed 
1e018 74 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20  to one database 
1e019 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  */.){.  int i;. 
1e01a 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
1e01b 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20  _item *pItem;.  
1e01c 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72  if( pList==0 ) r
1e01d 65 74 75 72 6e 20 30 3b 0a 20 20 66 6f 72 28 69  eturn 0;.  for(i
1e01e 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d  =0, pItem=pList-
1e01f 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78  >a; i<pList->nEx
1e020 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b  pr; i++, pItem++
1e021 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
1e022 65 33 46 69 78 45 78 70 72 28 70 46 69 78 2c 20  e3FixExpr(pFix, 
1e023 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 20 29 7b  pItem->pExpr) ){
1e024 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
1e025 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
1e026 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a  urn 0;.}.#endif.
1e027 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1e028 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 53 51 4c  OMIT_TRIGGER.SQL
1e029 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1e02a 73 71 6c 69 74 65 33 46 69 78 54 72 69 67 67 65  sqlite3FixTrigge
1e02b 72 53 74 65 70 28 0a 20 20 44 62 46 69 78 65 72  rStep(.  DbFixer
1e02c 20 2a 70 46 69 78 2c 20 20 20 20 20 2f 2a 20 43   *pFix,     /* C
1e02d 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66 69  ontext of the fi
1e02e 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 54 72 69 67  xation */.  Trig
1e02f 67 65 72 53 74 65 70 20 2a 70 53 74 65 70 20 2f  gerStep *pStep /
1e030 2a 20 54 68 65 20 74 72 69 67 67 65 72 20 73 74  * The trigger st
1e031 65 70 20 62 65 20 66 69 78 65 64 20 74 6f 20 6f  ep be fixed to o
1e032 6e 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 29  ne database */.)
1e033 7b 0a 20 20 77 68 69 6c 65 28 20 70 53 74 65 70  {.  while( pStep
1e034 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   ){.    if( sqli
1e035 74 65 33 46 69 78 53 65 6c 65 63 74 28 70 46 69  te3FixSelect(pFi
1e036 78 2c 20 70 53 74 65 70 2d 3e 70 53 65 6c 65 63  x, pStep->pSelec
1e037 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  t) ){.      retu
1e038 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
1e039 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78  if( sqlite3FixEx
1e03a 70 72 28 70 46 69 78 2c 20 70 53 74 65 70 2d 3e  pr(pFix, pStep->
1e03b 70 57 68 65 72 65 29 20 29 7b 0a 20 20 20 20 20  pWhere) ){.     
1e03c 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
1e03d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1e03e 46 69 78 45 78 70 72 4c 69 73 74 28 70 46 69 78  FixExprList(pFix
1e03f 2c 20 70 53 74 65 70 2d 3e 70 45 78 70 72 4c 69  , pStep->pExprLi
1e040 73 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  st) ){.      ret
1e041 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
1e042 20 70 53 74 65 70 20 3d 20 70 53 74 65 70 2d 3e   pStep = pStep->
1e043 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74  pNext;.  }.  ret
1e044 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a  urn 0;.}.#endif.
1e045 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1e046 20 45 6e 64 20 6f 66 20 61 74 74 61 63 68 2e 63   End of attach.c
1e047 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1e048 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e049 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1e04a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1e04b 20 42 65 67 69 6e 20 66 69 6c 65 20 61 75 74 68   Begin file auth
1e04c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1e04d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e04e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1e04f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 4a 61 6e 75  ./*.** 2003 Janu
1e050 61 72 79 20 31 31 0a 2a 2a 0a 2a 2a 20 54 68 65  ary 11.**.** The
1e051 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
1e052 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
1e053 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
1e054 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
1e055 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
1e056 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
1e057 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
1e058 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
1e059 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
1e05a 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
1e05b 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
1e05c 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
1e05d 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
1e05e 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
1e05f 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
1e060 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
1e061 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
1e062 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e063 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e064 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e065 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e066 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
1e067 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
1e068 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
1e069 6e 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73  nt the sqlite3_s
1e06a 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 0a  et_authorizer().
1e06b 2a 2a 20 41 50 49 2e 20 20 54 68 69 73 20 66 61  ** API.  This fa
1e06c 63 69 6c 69 74 79 20 69 73 20 61 6e 20 6f 70 74  cility is an opt
1e06d 69 6f 6e 61 6c 20 66 65 61 74 75 72 65 20 6f 66  ional feature of
1e06e 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 20 45   the library.  E
1e06f 6d 62 65 64 64 65 64 0a 2a 2a 20 73 79 73 74 65  mbedded.** syste
1e070 6d 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 6e  ms that do not n
1e071 65 65 64 20 74 68 69 73 20 66 61 63 69 6c 69 74  eed this facilit
1e072 79 20 6d 61 79 20 6f 6d 69 74 20 69 74 20 62 79  y may omit it by
1e073 20 72 65 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20   recompiling.** 
1e074 74 68 65 20 6c 69 62 72 61 72 79 20 77 69 74 68  the library with
1e075 20 2d 44 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41   -DSQLITE_OMIT_A
1e076 55 54 48 4f 52 49 5a 41 54 49 4f 4e 3d 31 0a 2a  UTHORIZATION=1.*
1e077 2a 0a 2a 2a 20 24 49 64 3a 20 61 75 74 68 2e 63  *.** $Id: auth.c
1e078 2c 76 20 31 2e 32 39 20 32 30 30 37 2f 30 39 2f  ,v 1.29 2007/09/
1e079 31 38 20 31 35 3a 35 35 3a 30 37 20 64 72 68 20  18 15:55:07 drh 
1e07a 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
1e07b 41 6c 6c 20 6f 66 20 74 68 65 20 63 6f 64 65 20  All of the code 
1e07c 69 6e 20 74 68 69 73 20 66 69 6c 65 20 6d 61 79  in this file may
1e07d 20 62 65 20 6f 6d 69 74 74 65 64 20 62 79 20 64   be omitted by d
1e07e 65 66 69 6e 69 6e 67 20 61 20 73 69 6e 67 6c 65  efining a single
1e07f 0a 2a 2a 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69  .** macro..*/.#i
1e080 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1e081 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
1e082 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f 72 20 63 6c  ./*.** Set or cl
1e083 65 61 72 20 74 68 65 20 61 63 63 65 73 73 20 61  ear the access a
1e084 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e  uthorization fun
1e085 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ction..**.** The
1e086 20 61 63 63 65 73 73 20 61 75 74 68 6f 72 69 7a   access authoriz
1e087 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 69  ation function i
1e088 73 20 62 65 20 63 61 6c 6c 65 64 20 64 75 72 69  s be called duri
1e089 6e 67 20 74 68 65 20 63 6f 6d 70 69 6c 61 74 69  ng the compilati
1e08a 6f 6e 0a 2a 2a 20 70 68 61 73 65 20 74 6f 20 76  on.** phase to v
1e08b 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 75  erify that the u
1e08c 73 65 72 20 68 61 73 20 72 65 61 64 20 61 6e 64  ser has read and
1e08d 2f 6f 72 20 77 72 69 74 65 20 61 63 63 65 73 73  /or write access
1e08e 20 70 65 72 6d 69 73 73 69 6f 6e 20 6f 6e 0a 2a   permission on.*
1e08f 2a 20 76 61 72 69 6f 75 73 20 66 69 65 6c 64 73  * various fields
1e090 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1e091 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67  .  The first arg
1e092 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 61 75 74  ument to the aut
1e093 68 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73  h function.** is
1e094 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 33   a copy of the 3
1e095 72 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  rd argument to t
1e096 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 54 68  his routine.  Th
1e097 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
1e098 74 0a 2a 2a 20 74 6f 20 74 68 65 20 61 75 74 68  t.** to the auth
1e099 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 65   function is one
1e09a 20 6f 66 20 74 68 65 73 65 20 63 6f 6e 73 74 61   of these consta
1e09b 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  nts:.**.**      
1e09c 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 49   SQLITE_CREATE_I
1e09d 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51  NDEX.**       SQ
1e09e 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c  LITE_CREATE_TABL
1e09f 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  E.**       SQLIT
1e0a0 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49 4e  E_CREATE_TEMP_IN
1e0a1 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  DEX.**       SQL
1e0a2 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f  ITE_CREATE_TEMP_
1e0a3 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53  TABLE.**       S
1e0a4 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d  QLITE_CREATE_TEM
1e0a5 50 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20 20 20  P_TRIGGER.**    
1e0a6 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45     SQLITE_CREATE
1e0a7 5f 54 45 4d 50 5f 56 49 45 57 0a 2a 2a 20 20 20  _TEMP_VIEW.**   
1e0a8 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54      SQLITE_CREAT
1e0a9 45 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20 20 20  E_TRIGGER.**    
1e0aa 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45     SQLITE_CREATE
1e0ab 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 53  _VIEW.**       S
1e0ac 51 4c 49 54 45 5f 44 45 4c 45 54 45 0a 2a 2a 20  QLITE_DELETE.** 
1e0ad 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f        SQLITE_DRO
1e0ae 50 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20  P_INDEX.**      
1e0af 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 41 42   SQLITE_DROP_TAB
1e0b0 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  LE.**       SQLI
1e0b1 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49 4e 44  TE_DROP_TEMP_IND
1e0b2 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  EX.**       SQLI
1e0b3 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42  TE_DROP_TEMP_TAB
1e0b4 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  LE.**       SQLI
1e0b5 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 52 49  TE_DROP_TEMP_TRI
1e0b6 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 51  GGER.**       SQ
1e0b7 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 56  LITE_DROP_TEMP_V
1e0b8 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  IEW.**       SQL
1e0b9 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 47 45 52  ITE_DROP_TRIGGER
1e0ba 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
1e0bb 5f 44 52 4f 50 5f 56 49 45 57 0a 2a 2a 20 20 20  _DROP_VIEW.**   
1e0bc 20 20 20 20 53 51 4c 49 54 45 5f 49 4e 53 45 52      SQLITE_INSER
1e0bd 54 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  T.**       SQLIT
1e0be 45 5f 50 52 41 47 4d 41 0a 2a 2a 20 20 20 20 20  E_PRAGMA.**     
1e0bf 20 20 53 51 4c 49 54 45 5f 52 45 41 44 0a 2a 2a    SQLITE_READ.**
1e0c0 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 45         SQLITE_SE
1e0c1 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 53 51  LECT.**       SQ
1e0c2 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e  LITE_TRANSACTION
1e0c3 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
1e0c4 5f 55 50 44 41 54 45 0a 2a 2a 0a 2a 2a 20 54 68  _UPDATE.**.** Th
1e0c5 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72  e third and four
1e0c6 74 68 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  th arguments to 
1e0c7 74 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f  the auth functio
1e0c8 6e 20 61 72 65 20 74 68 65 20 6e 61 6d 65 20 6f  n are the name o
1e0c9 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 61  f.** the table a
1e0ca 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68  nd the column th
1e0cb 61 74 20 61 72 65 20 62 65 69 6e 67 20 61 63 63  at are being acc
1e0cc 65 73 73 65 64 2e 20 20 54 68 65 20 61 75 74 68  essed.  The auth
1e0cd 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 68 6f   function.** sho
1e0ce 75 6c 64 20 72 65 74 75 72 6e 20 65 69 74 68 65  uld return eithe
1e0cf 72 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 53 51 4c  r SQLITE_OK, SQL
1e0d0 49 54 45 5f 44 45 4e 59 2c 20 6f 72 20 53 51 4c  ITE_DENY, or SQL
1e0d1 49 54 45 5f 49 47 4e 4f 52 45 2e 20 20 49 66 0a  ITE_IGNORE.  If.
1e0d2 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  ** SQLITE_OK is 
1e0d3 72 65 74 75 72 6e 65 64 2c 20 69 74 20 6d 65 61  returned, it mea
1e0d4 6e 73 20 74 68 61 74 20 61 63 63 65 73 73 20 69  ns that access i
1e0d5 73 20 61 6c 6c 6f 77 65 64 2e 20 20 53 51 4c 49  s allowed.  SQLI
1e0d6 54 45 5f 44 45 4e 59 0a 2a 2a 20 6d 65 61 6e 73  TE_DENY.** means
1e0d7 20 74 68 61 74 20 74 68 65 20 53 51 4c 20 73 74   that the SQL st
1e0d8 61 74 65 6d 65 6e 74 20 77 69 6c 6c 20 6e 65 76  atement will nev
1e0d9 65 72 2d 72 75 6e 20 2d 20 74 68 65 20 73 71 6c  er-run - the sql
1e0da 69 74 65 33 5f 65 78 65 63 28 29 20 63 61 6c 6c  ite3_exec() call
1e0db 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75 72 6e 20  .** will return 
1e0dc 77 69 74 68 20 61 6e 20 65 72 72 6f 72 2e 20 20  with an error.  
1e0dd 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 6d 65  SQLITE_IGNORE me
1e0de 61 6e 73 20 74 68 61 74 20 74 68 65 20 53 51 4c  ans that the SQL
1e0df 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 73 68   statement.** sh
1e0e0 6f 75 6c 64 20 72 75 6e 20 62 75 74 20 61 74 74  ould run but att
1e0e1 65 6d 70 74 73 20 74 6f 20 72 65 61 64 20 74 68  empts to read th
1e0e2 65 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c 75  e specified colu
1e0e3 6d 6e 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 4e  mn will return N
1e0e4 55 4c 4c 0a 2a 2a 20 61 6e 64 20 61 74 74 65 6d  ULL.** and attem
1e0e5 70 74 73 20 74 6f 20 77 72 69 74 65 20 74 68 65  pts to write the
1e0e6 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20 62 65 20   column will be 
1e0e7 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 53  ignored..**.** S
1e0e8 65 74 74 69 6e 67 20 74 68 65 20 61 75 74 68 20  etting the auth 
1e0e9 66 75 6e 63 74 69 6f 6e 20 74 6f 20 4e 55 4c 4c  function to NULL
1e0ea 20 64 69 73 61 62 6c 65 73 20 74 68 69 73 20 68   disables this h
1e0eb 6f 6f 6b 2e 20 20 54 68 65 20 64 65 66 61 75 6c  ook.  The defaul
1e0ec 74 0a 2a 2a 20 73 65 74 74 69 6e 67 20 6f 66 20  t.** setting of 
1e0ed 74 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f  the auth functio
1e0ee 6e 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51  n is NULL..*/.SQ
1e0ef 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1e0f0 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69  ite3_set_authori
1e0f1 7a 65 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  zer(.  sqlite3 *
1e0f2 64 62 2c 0a 20 20 69 6e 74 20 28 2a 78 41 75 74  db,.  int (*xAut
1e0f3 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  h)(void*,int,con
1e0f4 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
1e0f5 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
1e0f6 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c 0a 20  ,const char*),. 
1e0f7 20 76 6f 69 64 20 2a 70 41 72 67 0a 29 7b 0a 20   void *pArg.){. 
1e0f8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
1e0f9 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
1e0fa 0a 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 78  .  db->xAuth = x
1e0fb 41 75 74 68 3b 0a 20 20 64 62 2d 3e 70 41 75 74  Auth;.  db->pAut
1e0fc 68 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73  hArg = pArg;.  s
1e0fd 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70  qlite3ExpirePrep
1e0fe 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64  aredStatements(d
1e0ff 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  b);.  sqlite3_mu
1e100 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
1e101 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  tex);.  return S
1e102 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
1e103 2a 2a 20 57 72 69 74 65 20 61 6e 20 65 72 72 6f  ** Write an erro
1e104 72 20 6d 65 73 73 61 67 65 20 69 6e 74 6f 20 70  r message into p
1e105 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 74  Parse->zErrMsg t
1e106 68 61 74 20 65 78 70 6c 61 69 6e 73 20 74 68 61  hat explains tha
1e107 74 20 74 68 65 0a 2a 2a 20 75 73 65 72 2d 73 75  t the.** user-su
1e108 70 70 6c 69 65 64 20 61 75 74 68 6f 72 69 7a 61  pplied authoriza
1e109 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 72 65  tion function re
1e10a 74 75 72 6e 65 64 20 61 6e 20 69 6c 6c 65 67 61  turned an illega
1e10b 6c 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74  l value..*/.stat
1e10c 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 41 75  ic void sqliteAu
1e10d 74 68 42 61 64 52 65 74 75 72 6e 43 6f 64 65 28  thBadReturnCode(
1e10e 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
1e10f 6e 74 20 72 63 29 7b 0a 20 20 73 71 6c 69 74 65  nt rc){.  sqlite
1e110 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1e111 2c 20 22 69 6c 6c 65 67 61 6c 20 72 65 74 75 72  , "illegal retur
1e112 6e 20 76 61 6c 75 65 20 28 25 64 29 20 66 72 6f  n value (%d) fro
1e113 6d 20 74 68 65 20 22 0a 20 20 20 20 22 61 75 74  m the ".    "aut
1e114 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74  horization funct
1e115 69 6f 6e 20 2d 20 73 68 6f 75 6c 64 20 62 65 20  ion - should be 
1e116 53 51 4c 49 54 45 5f 4f 4b 2c 20 53 51 4c 49 54  SQLITE_OK, SQLIT
1e117 45 5f 49 47 4e 4f 52 45 2c 20 22 0a 20 20 20 20  E_IGNORE, ".    
1e118 22 6f 72 20 53 51 4c 49 54 45 5f 44 45 4e 59 22  "or SQLITE_DENY"
1e119 2c 20 72 63 29 3b 0a 20 20 70 50 61 72 73 65 2d  , rc);.  pParse-
1e11a 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52  >rc = SQLITE_ERR
1e11b 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  OR;.}../*.** The
1e11c 20 70 45 78 70 72 20 73 68 6f 75 6c 64 20 62 65   pExpr should be
1e11d 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70   a TK_COLUMN exp
1e11e 72 65 73 73 69 6f 6e 2e 20 20 54 68 65 20 74 61  ression.  The ta
1e11f 62 6c 65 20 72 65 66 65 72 72 65 64 20 74 6f 0a  ble referred to.
1e120 2a 2a 20 69 73 20 69 6e 20 70 54 61 62 4c 69 73  ** is in pTabLis
1e121 74 20 6f 72 20 65 6c 73 65 20 69 74 20 69 73 20  t or else it is 
1e122 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c 44 20 74  the NEW or OLD t
1e123 61 62 6c 65 20 6f 66 20 61 20 74 72 69 67 67 65  able of a trigge
1e124 72 2e 20 20 0a 2a 2a 20 43 68 65 63 6b 20 74 6f  r.  .** Check to
1e125 20 73 65 65 20 69 66 20 69 74 20 69 73 20 4f 4b   see if it is OK
1e126 20 74 6f 20 72 65 61 64 20 74 68 69 73 20 70 61   to read this pa
1e127 72 74 69 63 75 6c 61 72 20 63 6f 6c 75 6d 6e 2e  rticular column.
1e128 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61 75  .**.** If the au
1e129 74 68 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  th function retu
1e12a 72 6e 73 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52  rns SQLITE_IGNOR
1e12b 45 2c 20 63 68 61 6e 67 65 20 74 68 65 20 54 4b  E, change the TK
1e12c 5f 43 4f 4c 55 4d 4e 20 0a 2a 2a 20 69 6e 73 74  _COLUMN .** inst
1e12d 72 75 63 74 69 6f 6e 20 69 6e 74 6f 20 61 20 54  ruction into a T
1e12e 4b 5f 4e 55 4c 4c 2e 20 20 49 66 20 74 68 65 20  K_NULL.  If the 
1e12f 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 72 65  auth function re
1e130 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 44 45 4e  turns SQLITE_DEN
1e131 59 2c 0a 2a 2a 20 74 68 65 6e 20 67 65 6e 65 72  Y,.** then gener
1e132 61 74 65 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f  ate an error..*/
1e133 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1e134 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 68  void sqlite3Auth
1e135 52 65 61 64 28 0a 20 20 50 61 72 73 65 20 2a 70  Read(.  Parse *p
1e136 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a  Parse,        /*
1e137 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74   The parser cont
1e138 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ext */.  Expr *p
1e139 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 2f  Expr,          /
1e13a 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e  * The expression
1e13b 20 74 6f 20 63 68 65 63 6b 20 61 75 74 68 6f 72   to check author
1e13c 69 7a 61 74 69 6f 6e 20 6f 6e 20 2a 2f 0a 20 20  ization on */.  
1e13d 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 2c  Schema *pSchema,
1e13e 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 63 68        /* The sch
1e13f 65 6d 61 20 6f 66 20 74 68 65 20 65 78 70 72 65  ema of the expre
1e140 73 73 69 6f 6e 20 2a 2f 0a 20 20 53 72 63 4c 69  ssion */.  SrcLi
1e141 73 74 20 2a 70 54 61 62 4c 69 73 74 20 20 20 20  st *pTabList    
1e142 20 2f 2a 20 41 6c 6c 20 74 61 62 6c 65 20 74 68   /* All table th
1e143 61 74 20 70 45 78 70 72 20 6d 69 67 68 74 20 72  at pExpr might r
1e144 65 66 65 72 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20  efer to */.){.  
1e145 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
1e146 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20  arse->db;.  int 
1e147 72 63 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  rc;.  Table *pTa
1e148 62 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 54  b = 0;      /* T
1e149 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 72  he table being r
1e14a 65 61 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ead */.  const c
1e14b 68 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 2f  har *zCol;     /
1e14c 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f  * Name of the co
1e14d 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c  lumn of the tabl
1e14e 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53 72 63 3b  e */.  int iSrc;
1e14f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1e150 49 6e 64 65 78 20 69 6e 20 70 54 61 62 4c 69 73  Index in pTabLis
1e151 74 2d 3e 61 5b 5d 20 6f 66 20 74 61 62 6c 65 20  t->a[] of table 
1e152 62 65 69 6e 67 20 72 65 61 64 20 2a 2f 0a 20 20  being read */.  
1e153 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 42 61  const char *zDBa
1e154 73 65 3b 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  se;   /* Name of
1e155 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 20   database being 
1e156 61 63 63 65 73 73 65 64 20 2a 2f 0a 20 20 54 72  accessed */.  Tr
1e157 69 67 67 65 72 53 74 61 63 6b 20 2a 70 53 74 61  iggerStack *pSta
1e158 63 6b 3b 20 2f 2a 20 54 68 65 20 73 74 61 63 6b  ck; /* The stack
1e159 20 6f 66 20 63 75 72 72 65 6e 74 20 74 72 69 67   of current trig
1e15a 67 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 44  gers */.  int iD
1e15b 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
1e15c 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20  /* The index of 
1e15d 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68 65  the database the
1e15e 20 65 78 70 72 65 73 73 69 6f 6e 20 72 65 66 65   expression refe
1e15f 72 73 20 74 6f 20 2a 2f 0a 0a 20 20 69 66 28 20  rs to */..  if( 
1e160 64 62 2d 3e 78 41 75 74 68 3d 3d 30 20 29 20 72  db->xAuth==0 ) r
1e161 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 45 78  eturn;.  if( pEx
1e162 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d  pr->op!=TK_COLUM
1e163 4e 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 44  N ) return;.  iD
1e164 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
1e165 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d  aToIndex(pParse-
1e166 3e 64 62 2c 20 70 53 63 68 65 6d 61 29 3b 0a 20  >db, pSchema);. 
1e167 20 69 66 28 20 69 44 62 3c 30 20 29 7b 0a 20 20   if( iDb<0 ){.  
1e168 20 20 2f 2a 20 41 6e 20 61 74 74 65 6d 70 74 20    /* An attempt 
1e169 74 6f 20 72 65 61 64 20 61 20 63 6f 6c 75 6d 6e  to read a column
1e16a 20 6f 75 74 20 6f 66 20 61 20 73 75 62 71 75 65   out of a subque
1e16b 72 79 20 6f 72 20 6f 74 68 65 72 0a 20 20 20 20  ry or other.    
1e16c 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  ** temporary tab
1e16d 6c 65 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  le. */.    retur
1e16e 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 53 72  n;.  }.  for(iSr
1e16f 63 3d 30 3b 20 70 54 61 62 4c 69 73 74 20 26 26  c=0; pTabList &&
1e170 20 69 53 72 63 3c 70 54 61 62 4c 69 73 74 2d 3e   iSrc<pTabList->
1e171 6e 53 72 63 3b 20 69 53 72 63 2b 2b 29 7b 0a 20  nSrc; iSrc++){. 
1e172 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 54     if( pExpr->iT
1e173 61 62 6c 65 3d 3d 70 54 61 62 4c 69 73 74 2d 3e  able==pTabList->
1e174 61 5b 69 53 72 63 5d 2e 69 43 75 72 73 6f 72 20  a[iSrc].iCursor 
1e175 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69  ) break;.  }.  i
1e176 66 28 20 69 53 72 63 3e 3d 30 20 26 26 20 70 54  f( iSrc>=0 && pT
1e177 61 62 4c 69 73 74 20 26 26 20 69 53 72 63 3c 70  abList && iSrc<p
1e178 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 20 29 7b  TabList->nSrc ){
1e179 0a 20 20 20 20 70 54 61 62 20 3d 20 70 54 61 62  .    pTab = pTab
1e17a 4c 69 73 74 2d 3e 61 5b 69 53 72 63 5d 2e 70 54  List->a[iSrc].pT
1e17b 61 62 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ab;.  }else if( 
1e17c 28 70 53 74 61 63 6b 20 3d 20 70 50 61 72 73 65  (pStack = pParse
1e17d 2d 3e 74 72 69 67 53 74 61 63 6b 29 21 3d 30 20  ->trigStack)!=0 
1e17e 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 6d  ){.    /* This m
1e17f 75 73 74 20 62 65 20 61 6e 20 61 74 74 65 6d 70  ust be an attemp
1e180 74 20 74 6f 20 72 65 61 64 20 74 68 65 20 4e 45  t to read the NE
1e181 57 20 6f 72 20 4f 4c 44 20 70 73 65 75 64 6f 2d  W or OLD pseudo-
1e182 74 61 62 6c 65 73 0a 20 20 20 20 2a 2a 20 6f 66  tables.    ** of
1e183 20 61 20 74 72 69 67 67 65 72 2e 0a 20 20 20 20   a trigger..    
1e184 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  */.    assert( p
1e185 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 70 53  Expr->iTable==pS
1e186 74 61 63 6b 2d 3e 6e 65 77 49 64 78 20 7c 7c 20  tack->newIdx || 
1e187 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 70  pExpr->iTable==p
1e188 53 74 61 63 6b 2d 3e 6f 6c 64 49 64 78 20 29 3b  Stack->oldIdx );
1e189 0a 20 20 20 20 70 54 61 62 20 3d 20 70 53 74 61  .    pTab = pSta
1e18a 63 6b 2d 3e 70 54 61 62 3b 0a 20 20 7d 0a 20 20  ck->pTab;.  }.  
1e18b 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20 72 65  if( pTab==0 ) re
1e18c 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 45 78 70  turn;.  if( pExp
1e18d 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20 29 7b  r->iColumn>=0 ){
1e18e 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78  .    assert( pEx
1e18f 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 70 54 61 62  pr->iColumn<pTab
1e190 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 7a 43  ->nCol );.    zC
1e191 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  ol = pTab->aCol[
1e192 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e  pExpr->iColumn].
1e193 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73 65 20 69  zName;.  }else i
1e194 66 28 20 70 54 61 62 2d 3e 69 50 4b 65 79 3e 3d  f( pTab->iPKey>=
1e195 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  0 ){.    assert(
1e196 20 70 54 61 62 2d 3e 69 50 4b 65 79 3c 70 54 61   pTab->iPKey<pTa
1e197 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 7a  b->nCol );.    z
1e198 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c  Col = pTab->aCol
1e199 5b 70 54 61 62 2d 3e 69 50 4b 65 79 5d 2e 7a 4e  [pTab->iPKey].zN
1e19a 61 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ame;.  }else{.  
1e19b 20 20 7a 43 6f 6c 20 3d 20 22 52 4f 57 49 44 22    zCol = "ROWID"
1e19c 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
1e19d 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62  iDb>=0 && iDb<db
1e19e 2d 3e 6e 44 62 20 29 3b 0a 20 20 7a 44 42 61 73  ->nDb );.  zDBas
1e19f 65 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  e = db->aDb[iDb]
1e1a0 2e 7a 4e 61 6d 65 3b 0a 20 20 72 63 20 3d 20 64  .zName;.  rc = d
1e1a1 62 2d 3e 78 41 75 74 68 28 64 62 2d 3e 70 41 75  b->xAuth(db->pAu
1e1a2 74 68 41 72 67 2c 20 53 51 4c 49 54 45 5f 52 45  thArg, SQLITE_RE
1e1a3 41 44 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  AD, pTab->zName,
1e1a4 20 7a 43 6f 6c 2c 20 7a 44 42 61 73 65 2c 20 0a   zCol, zDBase, .
1e1a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e1a6 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f   pParse->zAuthCo
1e1a7 6e 74 65 78 74 29 3b 0a 20 20 69 66 28 20 72 63  ntext);.  if( rc
1e1a8 3d 3d 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20  ==SQLITE_IGNORE 
1e1a9 29 7b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70  ){.    pExpr->op
1e1aa 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 7d 65   = TK_NULL;.  }e
1e1ab 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  lse if( rc==SQLI
1e1ac 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 69  TE_DENY ){.    i
1e1ad 66 28 20 64 62 2d 3e 6e 44 62 3e 32 20 7c 7c 20  f( db->nDb>2 || 
1e1ae 69 44 62 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  iDb!=0 ){.      
1e1af 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1e1b0 70 50 61 72 73 65 2c 20 22 61 63 63 65 73 73 20  pParse, "access 
1e1b1 74 6f 20 25 73 2e 25 73 2e 25 73 20 69 73 20 70  to %s.%s.%s is p
1e1b2 72 6f 68 69 62 69 74 65 64 22 2c 20 0a 20 20 20  rohibited", .   
1e1b3 20 20 20 20 20 20 7a 44 42 61 73 65 2c 20 70 54        zDBase, pT
1e1b4 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29  ab->zName, zCol)
1e1b5 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1e1b6 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1e1b7 73 67 28 70 50 61 72 73 65 2c 20 22 61 63 63 65  sg(pParse, "acce
1e1b8 73 73 20 74 6f 20 25 73 2e 25 73 20 69 73 20 70  ss to %s.%s is p
1e1b9 72 6f 68 69 62 69 74 65 64 22 2c 70 54 61 62 2d  rohibited",pTab-
1e1ba 3e 7a 4e 61 6d 65 2c 7a 43 6f 6c 29 3b 0a 20 20  >zName,zCol);.  
1e1bb 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e    }.    pParse->
1e1bc 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 55 54 48  rc = SQLITE_AUTH
1e1bd 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63  ;.  }else if( rc
1e1be 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1e1bf 20 20 20 73 71 6c 69 74 65 41 75 74 68 42 61 64     sqliteAuthBad
1e1c0 52 65 74 75 72 6e 43 6f 64 65 28 70 50 61 72 73  ReturnCode(pPars
1e1c1 65 2c 20 72 63 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  e, rc);.  }.}../
1e1c2 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 75 74 68 6f  *.** Do an autho
1e1c3 72 69 7a 61 74 69 6f 6e 20 63 68 65 63 6b 20 75  rization check u
1e1c4 73 69 6e 67 20 74 68 65 20 63 6f 64 65 20 61 6e  sing the code an
1e1c5 64 20 61 72 67 75 6d 65 6e 74 73 20 67 69 76 65  d arguments give
1e1c6 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 65 69  n.  Return.** ei
1e1c7 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b 20 28  ther SQLITE_OK (
1e1c8 7a 65 72 6f 29 20 6f 72 20 53 51 4c 49 54 45 5f  zero) or SQLITE_
1e1c9 49 47 4e 4f 52 45 20 6f 72 20 53 51 4c 49 54 45  IGNORE or SQLITE
1e1ca 5f 44 45 4e 59 2e 20 20 49 66 20 53 51 4c 49 54  _DENY.  If SQLIT
1e1cb 45 5f 44 45 4e 59 0a 2a 2a 20 69 73 20 72 65 74  E_DENY.** is ret
1e1cc 75 72 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 20  urned, then the 
1e1cd 65 72 72 6f 72 20 63 6f 75 6e 74 20 61 6e 64 20  error count and 
1e1ce 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e  error message in
1e1cf 20 70 50 61 72 73 65 20 61 72 65 0a 2a 2a 20 6d   pParse are.** m
1e1d0 6f 64 69 66 69 65 64 20 61 70 70 72 6f 70 72 69  odified appropri
1e1d1 61 74 65 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ately..*/.SQLITE
1e1d2 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1e1d3 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 0a 20  ite3AuthCheck(. 
1e1d4 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 0a   Parse *pParse,.
1e1d5 20 20 69 6e 74 20 63 6f 64 65 2c 0a 20 20 63 6f    int code,.  co
1e1d6 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67 31 2c  nst char *zArg1,
1e1d7 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1e1d8 41 72 67 32 2c 0a 20 20 63 6f 6e 73 74 20 63 68  Arg2,.  const ch
1e1d9 61 72 20 2a 7a 41 72 67 33 0a 29 7b 0a 20 20 73  ar *zArg3.){.  s
1e1da 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
1e1db 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72  rse->db;.  int r
1e1dc 63 3b 0a 0a 20 20 2f 2a 20 44 6f 6e 27 74 20 64  c;..  /* Don't d
1e1dd 6f 20 61 6e 79 20 61 75 74 68 6f 72 69 7a 61 74  o any authorizat
1e1de 69 6f 6e 20 63 68 65 63 6b 73 20 69 66 20 74 68  ion checks if th
1e1df 65 20 64 61 74 61 62 61 73 65 20 69 73 20 69 6e  e database is in
1e1e0 69 74 69 61 6c 69 73 69 6e 67 0a 20 20 2a 2a 20  itialising.  ** 
1e1e1 6f 72 20 69 66 20 74 68 65 20 70 61 72 73 65 72  or if the parser
1e1e2 20 69 73 20 62 65 69 6e 67 20 69 6e 76 6f 6b 65   is being invoke
1e1e3 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 73 71  d from within sq
1e1e4 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74  lite3_declare_vt
1e1e5 61 62 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64  ab..  */.  if( d
1e1e6 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c 7c 20  b->init.busy || 
1e1e7 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20  IN_DECLARE_VTAB 
1e1e8 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
1e1e9 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
1e1ea 69 66 28 20 64 62 2d 3e 78 41 75 74 68 3d 3d 30  if( db->xAuth==0
1e1eb 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
1e1ec 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
1e1ed 72 63 20 3d 20 64 62 2d 3e 78 41 75 74 68 28 64  rc = db->xAuth(d
1e1ee 62 2d 3e 70 41 75 74 68 41 72 67 2c 20 63 6f 64  b->pAuthArg, cod
1e1ef 65 2c 20 7a 41 72 67 31 2c 20 7a 41 72 67 32 2c  e, zArg1, zArg2,
1e1f0 20 7a 41 72 67 33 2c 20 70 50 61 72 73 65 2d 3e   zArg3, pParse->
1e1f1 7a 41 75 74 68 43 6f 6e 74 65 78 74 29 3b 0a 20  zAuthContext);. 
1e1f2 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1e1f3 44 45 4e 59 20 29 7b 0a 20 20 20 20 73 71 6c 69  DENY ){.    sqli
1e1f4 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1e1f5 73 65 2c 20 22 6e 6f 74 20 61 75 74 68 6f 72 69  se, "not authori
1e1f6 7a 65 64 22 29 3b 0a 20 20 20 20 70 50 61 72 73  zed");.    pPars
1e1f7 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 41  e->rc = SQLITE_A
1e1f8 55 54 48 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  UTH;.  }else if(
1e1f9 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc!=SQLITE_OK &
1e1fa 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 47 4e  & rc!=SQLITE_IGN
1e1fb 4f 52 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ORE ){.    rc = 
1e1fc 53 51 4c 49 54 45 5f 44 45 4e 59 3b 0a 20 20 20  SQLITE_DENY;.   
1e1fd 20 73 71 6c 69 74 65 41 75 74 68 42 61 64 52 65   sqliteAuthBadRe
1e1fe 74 75 72 6e 43 6f 64 65 28 70 50 61 72 73 65 2c  turnCode(pParse,
1e1ff 20 72 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75   rc);.  }.  retu
1e200 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1e201 50 75 73 68 20 61 6e 20 61 75 74 68 6f 72 69 7a  Push an authoriz
1e202 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74 2e 20 20  ation context.  
1e203 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 69  After this routi
1e204 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68  ne is called, th
1e205 65 0a 2a 2a 20 7a 41 72 67 33 20 61 72 67 75 6d  e.** zArg3 argum
1e206 65 6e 74 20 74 6f 20 61 75 74 68 6f 72 69 7a 61  ent to authoriza
1e207 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 73 20 77  tion callbacks w
1e208 69 6c 6c 20 62 65 20 7a 43 6f 6e 74 65 78 74 20  ill be zContext 
1e209 75 6e 74 69 6c 0a 2a 2a 20 70 6f 70 70 65 64 2e  until.** popped.
1e20a 20 20 4f 72 20 69 66 20 70 50 61 72 73 65 3d 3d    Or if pParse==
1e20b 30 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  0, this routine 
1e20c 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53  is a no-op..*/.S
1e20d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1e20e 69 64 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f  id sqlite3AuthCo
1e20f 6e 74 65 78 74 50 75 73 68 28 0a 20 20 50 61 72  ntextPush(.  Par
1e210 73 65 20 2a 70 50 61 72 73 65 2c 0a 20 20 41 75  se *pParse,.  Au
1e211 74 68 43 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74  thContext *pCont
1e212 65 78 74 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68  ext, .  const ch
1e213 61 72 20 2a 7a 43 6f 6e 74 65 78 74 0a 29 7b 0a  ar *zContext.){.
1e214 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72    pContext->pPar
1e215 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 69  se = pParse;.  i
1e216 66 28 20 70 50 61 72 73 65 20 29 7b 0a 20 20 20  f( pParse ){.   
1e217 20 70 43 6f 6e 74 65 78 74 2d 3e 7a 41 75 74 68   pContext->zAuth
1e218 43 6f 6e 74 65 78 74 20 3d 20 70 50 61 72 73 65  Context = pParse
1e219 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a  ->zAuthContext;.
1e21a 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 41 75 74      pParse->zAut
1e21b 68 43 6f 6e 74 65 78 74 20 3d 20 7a 43 6f 6e 74  hContext = zCont
1e21c 65 78 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ext;.  }.}../*.*
1e21d 2a 20 50 6f 70 20 61 6e 20 61 75 74 68 6f 72 69  * Pop an authori
1e21e 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 74  zation context t
1e21f 68 61 74 20 77 61 73 20 70 72 65 76 69 6f 75 73  hat was previous
1e220 6c 79 20 70 75 73 68 65 64 0a 2a 2a 20 62 79 20  ly pushed.** by 
1e221 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65  sqlite3AuthConte
1e222 78 74 50 75 73 68 0a 2a 2f 0a 53 51 4c 49 54 45  xtPush.*/.SQLITE
1e223 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1e224 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74  lite3AuthContext
1e225 50 6f 70 28 41 75 74 68 43 6f 6e 74 65 78 74 20  Pop(AuthContext 
1e226 2a 70 43 6f 6e 74 65 78 74 29 7b 0a 20 20 69 66  *pContext){.  if
1e227 28 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72  ( pContext->pPar
1e228 73 65 20 29 7b 0a 20 20 20 20 70 43 6f 6e 74 65  se ){.    pConte
1e229 78 74 2d 3e 70 50 61 72 73 65 2d 3e 7a 41 75 74  xt->pParse->zAut
1e22a 68 43 6f 6e 74 65 78 74 20 3d 20 70 43 6f 6e 74  hContext = pCont
1e22b 65 78 74 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78  ext->zAuthContex
1e22c 74 3b 0a 20 20 20 20 70 43 6f 6e 74 65 78 74 2d  t;.    pContext-
1e22d 3e 70 50 61 72 73 65 20 3d 20 30 3b 0a 20 20 7d  >pParse = 0;.  }
1e22e 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
1e22f 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
1e230 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 2f 2a 2a 2a  IZATION */../***
1e231 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
1e232 6f 66 20 61 75 74 68 2e 63 20 2a 2a 2a 2a 2a 2a  of auth.c ******
1e233 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e234 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e235 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
1e236 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
1e237 6e 20 66 69 6c 65 20 62 75 69 6c 64 2e 63 20 2a  n file build.c *
1e238 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e239 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e23a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
1e23b 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
1e23c 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
1e23d 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
1e23e 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
1e23f 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
1e240 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
1e241 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
1e242 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
1e243 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
1e244 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
1e245 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
1e246 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
1e247 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
1e248 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
1e249 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
1e24a 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
1e24b 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
1e24c 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
1e24d 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
1e24e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e24f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e251 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e252 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
1e253 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20  contains C code 
1e254 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61 72  routines that ar
1e255 65 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  e called by the 
1e256 53 51 4c 69 74 65 20 70 61 72 73 65 72 0a 2a 2a  SQLite parser.**
1e257 20 77 68 65 6e 20 73 79 6e 74 61 78 20 72 75 6c   when syntax rul
1e258 65 73 20 61 72 65 20 72 65 64 75 63 65 64 2e 20  es are reduced. 
1e259 20 54 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e   The routines in
1e25a 20 74 68 69 73 20 66 69 6c 65 20 68 61 6e 64 6c   this file handl
1e25b 65 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69  e the.** followi
1e25c 6e 67 20 6b 69 6e 64 73 20 6f 66 20 53 51 4c 20  ng kinds of SQL 
1e25d 73 79 6e 74 61 78 3a 0a 2a 2a 0a 2a 2a 20 20 20  syntax:.**.**   
1e25e 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 0a 2a    CREATE TABLE.*
1e25f 2a 20 20 20 20 20 44 52 4f 50 20 54 41 42 4c 45  *     DROP TABLE
1e260 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 49  .**     CREATE I
1e261 4e 44 45 58 0a 2a 2a 20 20 20 20 20 44 52 4f 50  NDEX.**     DROP
1e262 20 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 63 72   INDEX.**     cr
1e263 65 61 74 69 6e 67 20 49 44 20 6c 69 73 74 73 0a  eating ID lists.
1e264 2a 2a 20 20 20 20 20 42 45 47 49 4e 20 54 52 41  **     BEGIN TRA
1e265 4e 53 41 43 54 49 4f 4e 0a 2a 2a 20 20 20 20 20  NSACTION.**     
1e266 43 4f 4d 4d 49 54 0a 2a 2a 20 20 20 20 20 52 4f  COMMIT.**     RO
1e267 4c 4c 42 41 43 4b 0a 2a 2a 0a 2a 2a 20 24 49 64  LLBACK.**.** $Id
1e268 3a 20 62 75 69 6c 64 2e 63 2c 76 20 31 2e 35 30  : build.c,v 1.50
1e269 30 20 32 30 30 38 2f 31 31 2f 30 33 20 32 30 3a  0 2008/11/03 20:
1e26a 35 35 3a 30 37 20 64 72 68 20 45 78 70 20 24 0a  55:07 drh Exp $.
1e26b 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  */../*.** This r
1e26c 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
1e26d 20 77 68 65 6e 20 61 20 6e 65 77 20 53 51 4c 20   when a new SQL 
1e26e 73 74 61 74 65 6d 65 6e 74 20 69 73 20 62 65 67  statement is beg
1e26f 69 6e 6e 69 6e 67 20 74 6f 0a 2a 2a 20 62 65 20  inning to.** be 
1e270 70 61 72 73 65 64 2e 20 20 49 6e 69 74 69 61 6c  parsed.  Initial
1e271 69 7a 65 20 74 68 65 20 70 50 61 72 73 65 20 73  ize the pParse s
1e272 74 72 75 63 74 75 72 65 20 61 73 20 6e 65 65 64  tructure as need
1e273 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
1e274 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1e275 65 33 42 65 67 69 6e 50 61 72 73 65 28 50 61 72  e3BeginParse(Par
1e276 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
1e277 65 78 70 6c 61 69 6e 46 6c 61 67 29 7b 0a 20 20  explainFlag){.  
1e278 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e 20  pParse->explain 
1e279 3d 20 65 78 70 6c 61 69 6e 46 6c 61 67 3b 0a 20  = explainFlag;. 
1e27a 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 20 3d 20   pParse->nVar = 
1e27b 30 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  0;.}..#ifndef SQ
1e27c 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
1e27d 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 54 68 65  _CACHE./*.** The
1e27e 20 54 61 62 6c 65 4c 6f 63 6b 20 73 74 72 75 63   TableLock struc
1e27f 74 75 72 65 20 69 73 20 6f 6e 6c 79 20 75 73 65  ture is only use
1e280 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33  d by the sqlite3
1e281 54 61 62 6c 65 4c 6f 63 6b 28 29 20 61 6e 64 0a  TableLock() and.
1e282 2a 2a 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b  ** codeTableLock
1e283 73 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a  s() functions..*
1e284 2f 0a 73 74 72 75 63 74 20 54 61 62 6c 65 4c 6f  /.struct TableLo
1e285 63 6b 20 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20  ck {.  int iDb; 
1e286 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1e287 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74  he database cont
1e288 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65  aining the table
1e289 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f   to be locked */
1e28a 0a 20 20 69 6e 74 20 69 54 61 62 3b 20 20 20 20  .  int iTab;    
1e28b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
1e28c 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20  oot page of the 
1e28d 74 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b  table to be lock
1e28e 65 64 20 2a 2f 0a 20 20 75 38 20 69 73 57 72 69  ed */.  u8 isWri
1e28f 74 65 4c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20  teLock;      /* 
1e290 54 72 75 65 20 66 6f 72 20 77 72 69 74 65 20 6c  True for write l
1e291 6f 63 6b 2e 20 20 46 61 6c 73 65 20 66 6f 72 20  ock.  False for 
1e292 61 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20  a read lock */. 
1e293 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
1e294 6d 65 3b 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  me;   /* Name of
1e295 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 7d 3b   the table */.};
1e296 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 74  ../*.** Record t
1e297 68 65 20 66 61 63 74 20 74 68 61 74 20 77 65 20  he fact that we 
1e298 77 61 6e 74 20 74 6f 20 6c 6f 63 6b 20 61 20 74  want to lock a t
1e299 61 62 6c 65 20 61 74 20 72 75 6e 2d 74 69 6d 65  able at run-time
1e29a 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61  .  .**.** The ta
1e29b 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64  ble to be locked
1e29c 20 68 61 73 20 72 6f 6f 74 20 70 61 67 65 20 69   has root page i
1e29d 54 61 62 20 61 6e 64 20 69 73 20 66 6f 75 6e 64  Tab and is found
1e29e 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 44 62   in database iDb
1e29f 2e 0a 2a 2a 20 41 20 72 65 61 64 20 6f 72 20 61  ..** A read or a
1e2a0 20 77 72 69 74 65 20 6c 6f 63 6b 20 63 61 6e 20   write lock can 
1e2a1 62 65 20 74 61 6b 65 6e 20 64 65 70 65 6e 64 69  be taken dependi
1e2a2 6e 67 20 6f 6e 20 69 73 57 72 69 74 65 6c 6f 63  ng on isWriteloc
1e2a3 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  k..**.** This ro
1e2a4 75 74 69 6e 65 20 6a 75 73 74 20 72 65 63 6f 72  utine just recor
1e2a5 64 73 20 74 68 65 20 66 61 63 74 20 74 68 61 74  ds the fact that
1e2a6 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 64 65 73   the lock is des
1e2a7 69 72 65 64 2e 20 20 54 68 65 0a 2a 2a 20 63 6f  ired.  The.** co
1e2a8 64 65 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 6c  de to make the l
1e2a9 6f 63 6b 20 6f 63 63 75 72 20 69 73 20 67 65 6e  ock occur is gen
1e2aa 65 72 61 74 65 64 20 62 79 20 61 20 6c 61 74 65  erated by a late
1e2ab 72 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 63 6f 64  r call to.** cod
1e2ac 65 54 61 62 6c 65 4c 6f 63 6b 73 28 29 20 77 68  eTableLocks() wh
1e2ad 69 63 68 20 6f 63 63 75 72 73 20 64 75 72 69 6e  ich occurs durin
1e2ae 67 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43  g sqlite3FinishC
1e2af 6f 64 69 6e 67 28 29 2e 0a 2a 2f 0a 53 51 4c 49  oding()..*/.SQLI
1e2b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1e2b1 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b  sqlite3TableLock
1e2b2 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
1e2b3 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e  e,     /* Parsin
1e2b4 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69  g context */.  i
1e2b5 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 20  nt iDb,         
1e2b6 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68    /* Index of th
1e2b7 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61  e database conta
1e2b8 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20  ining the table 
1e2b9 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74  to lock */.  int
1e2ba 20 69 54 61 62 2c 20 20 20 20 20 20 20 20 20 20   iTab,          
1e2bb 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6e 75 6d  /* Root page num
1e2bc 62 65 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ber of the table
1e2bd 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f   to be locked */
1e2be 0a 20 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63  .  u8 isWriteLoc
1e2bf 6b 2c 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f  k,    /* True fo
1e2c0 72 20 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 2a  r a write lock *
1e2c1 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1e2c2 7a 4e 61 6d 65 20 20 2f 2a 20 4e 61 6d 65 20 6f  zName  /* Name o
1e2c3 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62  f the table to b
1e2c4 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 29 7b 0a 20  e locked */.){. 
1e2c5 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 42   int i;.  int nB
1e2c6 79 74 65 73 3b 0a 20 20 54 61 62 6c 65 4c 6f 63  ytes;.  TableLoc
1e2c7 6b 20 2a 70 3b 0a 0a 20 20 69 66 28 20 69 44 62  k *p;..  if( iDb
1e2c8 3c 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  <0 ){.    return
1e2c9 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69 3d 30  ;.  }..  for(i=0
1e2ca 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 54 61 62  ; i<pParse->nTab
1e2cb 6c 65 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20  leLock; i++){.  
1e2cc 20 20 70 20 3d 20 26 70 50 61 72 73 65 2d 3e 61    p = &pParse->a
1e2cd 54 61 62 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a 20 20  TableLock[i];.  
1e2ce 20 20 69 66 28 20 70 2d 3e 69 44 62 3d 3d 69 44    if( p->iDb==iD
1e2cf 62 20 26 26 20 70 2d 3e 69 54 61 62 3d 3d 69 54  b && p->iTab==iT
1e2d0 61 62 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69  ab ){.      p->i
1e2d1 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20 28 70 2d  sWriteLock = (p-
1e2d2 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20 7c 7c 20  >isWriteLock || 
1e2d3 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b 0a 20 20  isWriteLock);.  
1e2d4 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
1e2d5 7d 0a 20 20 7d 0a 0a 20 20 6e 42 79 74 65 73 20  }.  }..  nBytes 
1e2d6 3d 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 4c 6f  = sizeof(TableLo
1e2d7 63 6b 29 20 2a 20 28 70 50 61 72 73 65 2d 3e 6e  ck) * (pParse->n
1e2d8 54 61 62 6c 65 4c 6f 63 6b 2b 31 29 3b 0a 20 20  TableLock+1);.  
1e2d9 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f  pParse->aTableLo
1e2da 63 6b 20 3d 20 0a 20 20 20 20 20 20 73 71 6c 69  ck = .      sqli
1e2db 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72  te3DbReallocOrFr
1e2dc 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  ee(pParse->db, p
1e2dd 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63  Parse->aTableLoc
1e2de 6b 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 69 66  k, nBytes);.  if
1e2df 28 20 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65  ( pParse->aTable
1e2e0 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 20 3d 20  Lock ){.    p = 
1e2e1 26 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c  &pParse->aTableL
1e2e2 6f 63 6b 5b 70 50 61 72 73 65 2d 3e 6e 54 61 62  ock[pParse->nTab
1e2e3 6c 65 4c 6f 63 6b 2b 2b 5d 3b 0a 20 20 20 20 70  leLock++];.    p
1e2e4 2d 3e 69 44 62 20 3d 20 69 44 62 3b 0a 20 20 20  ->iDb = iDb;.   
1e2e5 20 70 2d 3e 69 54 61 62 20 3d 20 69 54 61 62 3b   p->iTab = iTab;
1e2e6 0a 20 20 20 20 70 2d 3e 69 73 57 72 69 74 65 4c  .    p->isWriteL
1e2e7 6f 63 6b 20 3d 20 69 73 57 72 69 74 65 4c 6f 63  ock = isWriteLoc
1e2e8 6b 3b 0a 20 20 20 20 70 2d 3e 7a 4e 61 6d 65 20  k;.    p->zName 
1e2e9 3d 20 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73 65  = zName;.  }else
1e2ea 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 54  {.    pParse->nT
1e2eb 61 62 6c 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20  ableLock = 0;.  
1e2ec 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61    pParse->db->ma
1e2ed 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
1e2ee 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64    }.}../*.** Cod
1e2ef 65 20 61 6e 20 4f 50 5f 54 61 62 6c 65 4c 6f 63  e an OP_TableLoc
1e2f0 6b 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f  k instruction fo
1e2f1 72 20 65 61 63 68 20 74 61 62 6c 65 20 6c 6f 63  r each table loc
1e2f2 6b 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 73 74  ked by the.** st
1e2f3 61 74 65 6d 65 6e 74 20 28 63 6f 6e 66 69 67 75  atement (configu
1e2f4 72 65 64 20 62 79 20 63 61 6c 6c 73 20 74 6f 20  red by calls to 
1e2f5 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b  sqlite3TableLock
1e2f6 28 29 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ())..*/.static v
1e2f7 6f 69 64 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63  oid codeTableLoc
1e2f8 6b 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ks(Parse *pParse
1e2f9 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 56 64  ){.  int i;.  Vd
1e2fa 62 65 20 2a 70 56 64 62 65 3b 20 0a 0a 20 20 69  be *pVdbe; ..  i
1e2fb 66 28 20 30 3d 3d 28 70 56 64 62 65 20 3d 20 73  f( 0==(pVdbe = s
1e2fc 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
1e2fd 61 72 73 65 29 29 20 29 7b 0a 20 20 20 20 72 65  arse)) ){.    re
1e2fe 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72  turn;.  }..  for
1e2ff 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e  (i=0; i<pParse->
1e300 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20 69 2b 2b 29  nTableLock; i++)
1e301 7b 0a 20 20 20 20 54 61 62 6c 65 4c 6f 63 6b 20  {.    TableLock 
1e302 2a 70 20 3d 20 26 70 50 61 72 73 65 2d 3e 61 54  *p = &pParse->aT
1e303 61 62 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 20  ableLock[i];.   
1e304 20 69 6e 74 20 70 31 20 3d 20 70 2d 3e 69 44 62   int p1 = p->iDb
1e305 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1e306 65 41 64 64 4f 70 34 28 70 56 64 62 65 2c 20 4f  eAddOp4(pVdbe, O
1e307 50 5f 54 61 62 6c 65 4c 6f 63 6b 2c 20 70 31 2c  P_TableLock, p1,
1e308 20 70 2d 3e 69 54 61 62 2c 20 70 2d 3e 69 73 57   p->iTab, p->isW
1e309 72 69 74 65 4c 6f 63 6b 2c 0a 20 20 20 20 20 20  riteLock,.      
1e30a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e30b 70 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41  p->zName, P4_STA
1e30c 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73  TIC);.  }.}.#els
1e30d 65 0a 20 20 23 64 65 66 69 6e 65 20 63 6f 64 65  e.  #define code
1e30e 54 61 62 6c 65 4c 6f 63 6b 73 28 78 29 0a 23 65  TableLocks(x).#e
1e30f 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ndif../*.** This
1e310 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
1e311 65 64 20 61 66 74 65 72 20 61 20 73 69 6e 67 6c  ed after a singl
1e312 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
1e313 68 61 73 20 62 65 65 6e 0a 2a 2a 20 70 61 72 73  has been.** pars
1e314 65 64 20 61 6e 64 20 61 20 56 44 42 45 20 70 72  ed and a VDBE pr
1e315 6f 67 72 61 6d 20 74 6f 20 65 78 65 63 75 74 65  ogram to execute
1e316 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 20   that statement 
1e317 68 61 73 20 62 65 65 6e 0a 2a 2a 20 70 72 65 70  has been.** prep
1e318 61 72 65 64 2e 20 20 54 68 69 73 20 72 6f 75 74  ared.  This rout
1e319 69 6e 65 20 70 75 74 73 20 74 68 65 20 66 69 6e  ine puts the fin
1e31a 69 73 68 69 6e 67 20 74 6f 75 63 68 65 73 20 6f  ishing touches o
1e31b 6e 20 74 68 65 0a 2a 2a 20 56 44 42 45 20 70 72  n the.** VDBE pr
1e31c 6f 67 72 61 6d 20 61 6e 64 20 72 65 73 65 74 73  ogram and resets
1e31d 20 74 68 65 20 70 50 61 72 73 65 20 73 74 72 75   the pParse stru
1e31e 63 74 75 72 65 20 66 6f 72 20 74 68 65 20 6e 65  cture for the ne
1e31f 78 74 0a 2a 2a 20 70 61 72 73 65 2e 0a 2a 2a 0a  xt.** parse..**.
1e320 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20  ** Note that if 
1e321 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65  an error occurre
1e322 64 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20 74  d, it might be t
1e323 68 65 20 63 61 73 65 20 74 68 61 74 0a 2a 2a 20  he case that.** 
1e324 6e 6f 20 56 44 42 45 20 63 6f 64 65 20 77 61 73  no VDBE code was
1e325 20 67 65 6e 65 72 61 74 65 64 2e 0a 2a 2f 0a 53   generated..*/.S
1e326 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1e327 69 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68  id sqlite3Finish
1e328 43 6f 64 69 6e 67 28 50 61 72 73 65 20 2a 70 50  Coding(Parse *pP
1e329 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33  arse){.  sqlite3
1e32a 20 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b   *db;.  Vdbe *v;
1e32b 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  ..  db = pParse-
1e32c 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d  >db;.  if( db->m
1e32d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65  allocFailed ) re
1e32e 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72  turn;.  if( pPar
1e32f 73 65 2d 3e 6e 65 73 74 65 64 20 29 20 72 65 74  se->nested ) ret
1e330 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72 73  urn;.  if( pPars
1e331 65 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72 6e  e->nErr ) return
1e332 3b 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 62 79  ;..  /* Begin by
1e333 20 67 65 6e 65 72 61 74 69 6e 67 20 73 6f 6d 65   generating some
1e334 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20 63 6f 64   termination cod
1e335 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20  e at the end of 
1e336 74 68 65 0a 20 20 2a 2a 20 76 64 62 65 20 70 72  the.  ** vdbe pr
1e337 6f 67 72 61 6d 0a 20 20 2a 2f 0a 20 20 76 20 3d  ogram.  */.  v =
1e338 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
1e339 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76  pParse);.  if( v
1e33a 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1e33b 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f  dbeAddOp0(v, OP_
1e33c 48 61 6c 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 54  Halt);..    /* T
1e33d 68 65 20 63 6f 6f 6b 69 65 20 6d 61 73 6b 20 63  he cookie mask c
1e33e 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 62 69 74 20  ontains one bit 
1e33f 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73  for each databas
1e340 65 20 66 69 6c 65 20 6f 70 65 6e 2e 0a 20 20 20  e file open..   
1e341 20 2a 2a 20 28 42 69 74 20 30 20 69 73 20 66 6f   ** (Bit 0 is fo
1e342 72 20 6d 61 69 6e 2c 20 62 69 74 20 31 20 69 73  r main, bit 1 is
1e343 20 66 6f 72 20 74 65 6d 70 2c 20 61 6e 64 20 73   for temp, and s
1e344 6f 20 66 6f 72 74 68 2e 29 20 20 42 69 74 73 20  o forth.)  Bits 
1e345 61 72 65 0a 20 20 20 20 2a 2a 20 73 65 74 20 66  are.    ** set f
1e346 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73 65  or each database
1e347 20 74 68 61 74 20 69 73 20 75 73 65 64 2e 20 20   that is used.  
1e348 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
1e349 20 73 74 61 72 74 20 61 0a 20 20 20 20 2a 2a 20   start a.    ** 
1e34a 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 65  transaction on e
1e34b 61 63 68 20 75 73 65 64 20 64 61 74 61 62 61 73  ach used databas
1e34c 65 20 61 6e 64 20 74 6f 20 76 65 72 69 66 79 20  e and to verify 
1e34d 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69  the schema cooki
1e34e 65 0a 20 20 20 20 2a 2a 20 6f 6e 20 65 61 63 68  e.    ** on each
1e34f 20 75 73 65 64 20 64 61 74 61 62 61 73 65 2e 0a   used database..
1e350 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
1e351 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74  Parse->cookieGot
1e352 6f 3e 30 20 29 7b 0a 20 20 20 20 20 20 75 33 32  o>0 ){.      u32
1e353 20 6d 61 73 6b 3b 0a 20 20 20 20 20 20 69 6e 74   mask;.      int
1e354 20 69 44 62 3b 0a 20 20 20 20 20 20 73 71 6c 69   iDb;.      sqli
1e355 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
1e356 76 2c 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69  v, pParse->cooki
1e357 65 47 6f 74 6f 2d 31 29 3b 0a 20 20 20 20 20 20  eGoto-1);.      
1e358 66 6f 72 28 69 44 62 3d 30 2c 20 6d 61 73 6b 3d  for(iDb=0, mask=
1e359 31 3b 20 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20  1; iDb<db->nDb; 
1e35a 6d 61 73 6b 3c 3c 3d 31 2c 20 69 44 62 2b 2b 29  mask<<=1, iDb++)
1e35b 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 6d  {.        if( (m
1e35c 61 73 6b 20 26 20 70 50 61 72 73 65 2d 3e 63 6f  ask & pParse->co
1e35d 6f 6b 69 65 4d 61 73 6b 29 3d 3d 30 20 29 20 63  okieMask)==0 ) c
1e35e 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
1e35f 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73   sqlite3VdbeUses
1e360 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 20  Btree(v, iDb);. 
1e361 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1e362 62 65 41 64 64 4f 70 32 28 76 2c 4f 50 5f 54 72  beAddOp2(v,OP_Tr
1e363 61 6e 73 61 63 74 69 6f 6e 2c 20 69 44 62 2c 20  ansaction, iDb, 
1e364 28 6d 61 73 6b 20 26 20 70 50 61 72 73 65 2d 3e  (mask & pParse->
1e365 77 72 69 74 65 4d 61 73 6b 29 21 3d 30 29 3b 0a  writeMask)!=0);.
1e366 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1e367 64 62 65 41 64 64 4f 70 32 28 76 2c 4f 50 5f 56  dbeAddOp2(v,OP_V
1e368 65 72 69 66 79 43 6f 6f 6b 69 65 2c 20 69 44 62  erifyCookie, iDb
1e369 2c 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65  , pParse->cookie
1e36a 56 61 6c 75 65 5b 69 44 62 5d 29 3b 0a 20 20 20  Value[iDb]);.   
1e36b 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
1e36c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1e36d 54 41 42 4c 45 0a 20 20 20 20 20 20 7b 0a 20 20  TABLE.      {.  
1e36e 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20        int i;.   
1e36f 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
1e370 70 50 61 72 73 65 2d 3e 6e 56 74 61 62 4c 6f 63  pParse->nVtabLoc
1e371 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  k; i++){.       
1e372 20 20 20 63 68 61 72 20 2a 76 74 61 62 20 3d 20     char *vtab = 
1e373 28 63 68 61 72 20 2a 29 70 50 61 72 73 65 2d 3e  (char *)pParse->
1e374 61 70 56 74 61 62 4c 6f 63 6b 5b 69 5d 2d 3e 70  apVtabLock[i]->p
1e375 56 74 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20  Vtab;.          
1e376 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1e377 34 28 76 2c 20 4f 50 5f 56 42 65 67 69 6e 2c 20  4(v, OP_VBegin, 
1e378 30 2c 20 30 2c 20 30 2c 20 76 74 61 62 2c 20 50  0, 0, 0, vtab, P
1e379 34 5f 56 54 41 42 29 3b 0a 20 20 20 20 20 20 20  4_VTAB);.       
1e37a 20 7d 0a 20 20 20 20 20 20 20 20 70 50 61 72 73   }.        pPars
1e37b 65 2d 3e 6e 56 74 61 62 4c 6f 63 6b 20 3d 20 30  e->nVtabLock = 0
1e37c 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  ;.      }.#endif
1e37d 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 6e 63 65 20  ..      /* Once 
1e37e 61 6c 6c 20 74 68 65 20 63 6f 6f 6b 69 65 73 20  all the cookies 
1e37f 68 61 76 65 20 62 65 65 6e 20 76 65 72 69 66 69  have been verifi
1e380 65 64 20 61 6e 64 20 74 72 61 6e 73 61 63 74 69  ed and transacti
1e381 6f 6e 73 20 6f 70 65 6e 65 64 2c 20 0a 20 20 20  ons opened, .   
1e382 20 20 20 2a 2a 20 6f 62 74 61 69 6e 20 74 68 65     ** obtain the
1e383 20 72 65 71 75 69 72 65 64 20 74 61 62 6c 65 2d   required table-
1e384 6c 6f 63 6b 73 2e 20 54 68 69 73 20 69 73 20 61  locks. This is a
1e385 20 6e 6f 2d 6f 70 20 75 6e 6c 65 73 73 20 74 68   no-op unless th
1e386 65 20 0a 20 20 20 20 20 20 2a 2a 20 73 68 61 72  e .      ** shar
1e387 65 64 2d 63 61 63 68 65 20 66 65 61 74 75 72 65  ed-cache feature
1e388 20 69 73 20 65 6e 61 62 6c 65 64 2e 0a 20 20 20   is enabled..   
1e389 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f 64 65     */.      code
1e38a 54 61 62 6c 65 4c 6f 63 6b 73 28 70 50 61 72 73  TableLocks(pPars
1e38b 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e);.      sqlite
1e38c 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1e38d 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 50 61 72 73  P_Goto, 0, pPars
1e38e 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 29 3b 0a  e->cookieGoto);.
1e38f 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53      }..#ifndef S
1e390 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45  QLITE_OMIT_TRACE
1e391 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e 69 6e  .    if( !db->in
1e392 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20  it.busy ){.     
1e393 20 2f 2a 20 43 68 61 6e 67 65 20 74 68 65 20 50   /* Change the P
1e394 34 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 74 68  4 argument of th
1e395 65 20 66 69 72 73 74 20 6f 70 63 6f 64 65 20 28  e first opcode (
1e396 77 68 69 63 68 20 77 69 6c 6c 20 61 6c 77 61 79  which will alway
1e397 73 20 62 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e  s be.      ** an
1e398 20 4f 50 5f 54 72 61 63 65 29 20 74 6f 20 62 65   OP_Trace) to be
1e399 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65   the complete te
1e39a 78 74 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  xt of the curren
1e39b 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  t SQL statement.
1e39c 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
1e39d 56 64 62 65 4f 70 20 2a 70 4f 70 20 3d 20 73 71  VdbeOp *pOp = sq
1e39e 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76  lite3VdbeGetOp(v
1e39f 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 0);.      if( 
1e3a0 70 4f 70 20 26 26 20 70 4f 70 2d 3e 6f 70 63 6f  pOp && pOp->opco
1e3a1 64 65 3d 3d 4f 50 5f 54 72 61 63 65 20 29 7b 0a  de==OP_Trace ){.
1e3a2 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1e3a3 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 30  dbeChangeP4(v, 0
1e3a4 2c 20 70 50 61 72 73 65 2d 3e 7a 53 71 6c 2c 20  , pParse->zSql, 
1e3a5 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 2d 70 50  pParse->zTail-pP
1e3a6 61 72 73 65 2d 3e 7a 53 71 6c 29 3b 0a 20 20 20  arse->zSql);.   
1e3a7 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69     }.    }.#endi
1e3a8 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1e3a9 5f 54 52 41 43 45 20 2a 2f 0a 20 20 7d 0a 0a 0a  _TRACE */.  }...
1e3aa 20 20 2f 2a 20 47 65 74 20 74 68 65 20 56 44 42    /* Get the VDB
1e3ab 45 20 70 72 6f 67 72 61 6d 20 72 65 61 64 79 20  E program ready 
1e3ac 66 6f 72 20 65 78 65 63 75 74 69 6f 6e 0a 20 20  for execution.  
1e3ad 2a 2f 0a 20 20 69 66 28 20 76 20 26 26 20 70 50  */.  if( v && pP
1e3ae 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 26 26  arse->nErr==0 &&
1e3af 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
1e3b0 65 64 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c  ed ){.#ifdef SQL
1e3b1 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 46 49  ITE_DEBUG.    FI
1e3b2 4c 45 20 2a 74 72 61 63 65 20 3d 20 28 64 62 2d  LE *trace = (db-
1e3b3 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
1e3b4 56 64 62 65 54 72 61 63 65 29 21 3d 30 20 3f 20  VdbeTrace)!=0 ? 
1e3b5 73 74 64 6f 75 74 20 3a 20 30 3b 0a 20 20 20 20  stdout : 0;.    
1e3b6 73 71 6c 69 74 65 33 56 64 62 65 54 72 61 63 65  sqlite3VdbeTrace
1e3b7 28 76 2c 20 74 72 61 63 65 29 3b 0a 23 65 6e 64  (v, trace);.#end
1e3b8 69 66 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  if.    assert( p
1e3b9 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f  Parse->disableCo
1e3ba 6c 43 61 63 68 65 3d 3d 30 20 29 3b 20 20 2f 2a  lCache==0 );  /*
1e3bb 20 44 69 73 61 62 6c 65 73 20 61 6e 64 20 72 65   Disables and re
1e3bc 2d 65 6e 61 62 6c 65 73 20 6d 61 74 63 68 20 2a  -enables match *
1e3bd 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
1e3be 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 70 50  eMakeReady(v, pP
1e3bf 61 72 73 65 2d 3e 6e 56 61 72 2c 20 70 50 61 72  arse->nVar, pPar
1e3c0 73 65 2d 3e 6e 4d 65 6d 2b 33 2c 0a 20 20 20 20  se->nMem+3,.    
1e3c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e3c2 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 61       pParse->nTa
1e3c3 62 2b 33 2c 20 70 50 61 72 73 65 2d 3e 65 78 70  b+3, pParse->exp
1e3c4 6c 61 69 6e 29 3b 0a 20 20 20 20 70 50 61 72 73  lain);.    pPars
1e3c5 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 44  e->rc = SQLITE_D
1e3c6 4f 4e 45 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  ONE;.    pParse-
1e3c7 3e 63 6f 6c 4e 61 6d 65 73 53 65 74 20 3d 20 30  >colNamesSet = 0
1e3c8 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50  ;.  }else if( pP
1e3c9 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45  arse->rc==SQLITE
1e3ca 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 72 73  _OK ){.    pPars
1e3cb 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45  e->rc = SQLITE_E
1e3cc 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 70 50 61 72  RROR;.  }.  pPar
1e3cd 73 65 2d 3e 6e 54 61 62 20 3d 20 30 3b 0a 20 20  se->nTab = 0;.  
1e3ce 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 30  pParse->nMem = 0
1e3cf 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 53 65 74  ;.  pParse->nSet
1e3d0 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e   = 0;.  pParse->
1e3d1 6e 56 61 72 20 3d 20 30 3b 0a 20 20 70 50 61 72  nVar = 0;.  pPar
1e3d2 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 3d  se->cookieMask =
1e3d3 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f   0;.  pParse->co
1e3d4 6f 6b 69 65 47 6f 74 6f 20 3d 20 30 3b 0a 7d 0a  okieGoto = 0;.}.
1e3d5 0a 2f 2a 0a 2a 2a 20 52 75 6e 20 74 68 65 20 70  ./*.** Run the p
1e3d6 61 72 73 65 72 20 61 6e 64 20 63 6f 64 65 20 67  arser and code g
1e3d7 65 6e 65 72 61 74 6f 72 20 72 65 63 75 72 73 69  enerator recursi
1e3d8 76 65 6c 79 20 69 6e 20 6f 72 64 65 72 20 74 6f  vely in order to
1e3d9 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64   generate.** cod
1e3da 65 20 66 6f 72 20 74 68 65 20 53 51 4c 20 73 74  e for the SQL st
1e3db 61 74 65 6d 65 6e 74 20 67 69 76 65 6e 20 6f 6e  atement given on
1e3dc 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  to the end of th
1e3dd 65 20 70 50 61 72 73 65 20 63 6f 6e 74 65 78 74  e pParse context
1e3de 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 75 6e  .** currently un
1e3df 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
1e3e0 2e 20 20 57 68 65 6e 20 74 68 65 20 70 61 72 73  .  When the pars
1e3e1 65 72 20 69 73 20 72 75 6e 20 72 65 63 75 72 73  er is run recurs
1e3e2 69 76 65 6c 79 0a 2a 2a 20 74 68 69 73 20 77 61  ively.** this wa
1e3e3 79 2c 20 74 68 65 20 66 69 6e 61 6c 20 4f 50 5f  y, the final OP_
1e3e4 48 61 6c 74 20 69 73 20 6e 6f 74 20 61 70 70 65  Halt is not appe
1e3e5 6e 64 65 64 20 61 6e 64 20 6f 74 68 65 72 20 69  nded and other i
1e3e6 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 0a 2a 2a  nitialization.**
1e3e7 20 61 6e 64 20 66 69 6e 61 6c 69 7a 61 74 69 6f   and finalizatio
1e3e8 6e 20 73 74 65 70 73 20 61 72 65 20 6f 6d 69 74  n steps are omit
1e3e9 74 65 64 20 62 65 63 61 75 73 65 20 74 68 6f 73  ted because thos
1e3ea 65 20 61 72 65 20 68 61 6e 64 6c 69 6e 67 20 62  e are handling b
1e3eb 79 20 74 68 65 0a 2a 2a 20 6f 75 74 65 72 6d 6f  y the.** outermo
1e3ec 73 74 20 70 61 72 73 65 72 2e 0a 2a 2a 0a 2a 2a  st parser..**.**
1e3ed 20 4e 6f 74 20 65 76 65 72 79 74 68 69 6e 67 20   Not everything 
1e3ee 69 73 20 6e 65 73 74 61 62 6c 65 2e 20 20 54 68  is nestable.  Th
1e3ef 69 73 20 66 61 63 69 6c 69 74 79 20 69 73 20 64  is facility is d
1e3f0 65 73 69 67 6e 65 64 20 74 6f 20 70 65 72 6d 69  esigned to permi
1e3f1 74 0a 2a 2a 20 49 4e 53 45 52 54 2c 20 55 50 44  t.** INSERT, UPD
1e3f2 41 54 45 2c 20 61 6e 64 20 44 45 4c 45 54 45 20  ATE, and DELETE 
1e3f3 6f 70 65 72 61 74 69 6f 6e 73 20 61 67 61 69 6e  operations again
1e3f4 73 74 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52  st SQLITE_MASTER
1e3f5 2e 20 20 55 73 65 0a 2a 2a 20 63 61 72 65 20 69  .  Use.** care i
1e3f6 66 20 79 6f 75 20 64 65 63 69 64 65 20 74 6f 20  f you decide to 
1e3f7 74 72 79 20 74 6f 20 75 73 65 20 74 68 69 73 20  try to use this 
1e3f8 72 6f 75 74 69 6e 65 20 66 6f 72 20 73 6f 6d 65  routine for some
1e3f9 20 6f 74 68 65 72 20 70 75 72 70 6f 73 65 73 2e   other purposes.
1e3fa 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1e3fb 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4e  TE void sqlite3N
1e3fc 65 73 74 65 64 50 61 72 73 65 28 50 61 72 73 65  estedParse(Parse
1e3fd 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20   *pParse, const 
1e3fe 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e  char *zFormat, .
1e3ff 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61  ..){.  va_list a
1e400 70 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b  p;.  char *zSql;
1e401 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67  .  char *zErrMsg
1e402 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 20   = 0;.  sqlite3 
1e403 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
1e404 3b 0a 23 20 64 65 66 69 6e 65 20 53 41 56 45 5f  ;.# define SAVE_
1e405 53 5a 20 20 28 73 69 7a 65 6f 66 28 50 61 72 73  SZ  (sizeof(Pars
1e406 65 29 20 2d 20 6f 66 66 73 65 74 6f 66 28 50 61  e) - offsetof(Pa
1e407 72 73 65 2c 6e 56 61 72 29 29 0a 20 20 63 68 61  rse,nVar)).  cha
1e408 72 20 73 61 76 65 42 75 66 5b 53 41 56 45 5f 53  r saveBuf[SAVE_S
1e409 5a 5d 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73  Z];..  if( pPars
1e40a 65 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72 6e  e->nErr ) return
1e40b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ;.  assert( pPar
1e40c 73 65 2d 3e 6e 65 73 74 65 64 3c 31 30 20 29 3b  se->nested<10 );
1e40d 20 20 2f 2a 20 4e 65 73 74 69 6e 67 20 73 68 6f    /* Nesting sho
1e40e 75 6c 64 20 6f 6e 6c 79 20 62 65 20 6f 66 20 6c  uld only be of l
1e40f 69 6d 69 74 65 64 20 64 65 70 74 68 20 2a 2f 0a  imited depth */.
1e410 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a    va_start(ap, z
1e411 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 53 71 6c 20  Format);.  zSql 
1e412 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74  = sqlite3VMPrint
1e413 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61  f(db, zFormat, a
1e414 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29  p);.  va_end(ap)
1e415 3b 0a 20 20 69 66 28 20 7a 53 71 6c 3d 3d 30 20  ;.  if( zSql==0 
1e416 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 20 20  ){.    return;  
1e417 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 20 6d 75 73   /* A malloc mus
1e418 74 20 68 61 76 65 20 66 61 69 6c 65 64 20 2a 2f  t have failed */
1e419 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e 6e  .  }.  pParse->n
1e41a 65 73 74 65 64 2b 2b 3b 0a 20 20 6d 65 6d 63 70  ested++;.  memcp
1e41b 79 28 73 61 76 65 42 75 66 2c 20 26 70 50 61 72  y(saveBuf, &pPar
1e41c 73 65 2d 3e 6e 56 61 72 2c 20 53 41 56 45 5f 53  se->nVar, SAVE_S
1e41d 5a 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70 50  Z);.  memset(&pP
1e41e 61 72 73 65 2d 3e 6e 56 61 72 2c 20 30 2c 20 53  arse->nVar, 0, S
1e41f 41 56 45 5f 53 5a 29 3b 0a 20 20 73 71 6c 69 74  AVE_SZ);.  sqlit
1e420 65 33 52 75 6e 50 61 72 73 65 72 28 70 50 61 72  e3RunParser(pPar
1e421 73 65 2c 20 7a 53 71 6c 2c 20 26 7a 45 72 72 4d  se, zSql, &zErrM
1e422 73 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  sg);.  sqlite3Db
1e423 46 72 65 65 28 64 62 2c 20 7a 45 72 72 4d 73 67  Free(db, zErrMsg
1e424 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
1e425 65 65 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20  ee(db, zSql);.  
1e426 6d 65 6d 63 70 79 28 26 70 50 61 72 73 65 2d 3e  memcpy(&pParse->
1e427 6e 56 61 72 2c 20 73 61 76 65 42 75 66 2c 20 53  nVar, saveBuf, S
1e428 41 56 45 5f 53 5a 29 3b 0a 20 20 70 50 61 72 73  AVE_SZ);.  pPars
1e429 65 2d 3e 6e 65 73 74 65 64 2d 2d 3b 0a 7d 0a 0a  e->nested--;.}..
1e42a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65  /*.** Locate the
1e42b 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63   in-memory struc
1e42c 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69  ture that descri
1e42d 62 65 73 20 61 20 70 61 72 74 69 63 75 6c 61 72  bes a particular
1e42e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62   database.** tab
1e42f 6c 65 20 67 69 76 65 6e 20 74 68 65 20 6e 61 6d  le given the nam
1e430 65 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 20  e of that table 
1e431 61 6e 64 20 28 6f 70 74 69 6f 6e 61 6c 6c 79 29  and (optionally)
1e432 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
1e433 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e  .** database con
1e434 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c  taining the tabl
1e435 65 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20  e.  Return NULL 
1e436 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a  if not found..**
1e437 0a 2a 2a 20 49 66 20 7a 44 61 74 61 62 61 73 65  .** If zDatabase
1e438 20 69 73 20 30 2c 20 61 6c 6c 20 64 61 74 61 62   is 0, all datab
1e439 61 73 65 73 20 61 72 65 20 73 65 61 72 63 68 65  ases are searche
1e43a 64 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20  d for the table 
1e43b 61 6e 64 20 74 68 65 0a 2a 2a 20 66 69 72 73 74  and the.** first
1e43c 20 6d 61 74 63 68 69 6e 67 20 74 61 62 6c 65 20   matching table 
1e43d 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 28 4e  is returned.  (N
1e43e 6f 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 64  o checking for d
1e43f 75 70 6c 69 63 61 74 65 20 74 61 62 6c 65 0a 2a  uplicate table.*
1e440 2a 20 6e 61 6d 65 73 20 69 73 20 64 6f 6e 65 2e  * names is done.
1e441 29 20 20 54 68 65 20 73 65 61 72 63 68 20 6f 72  )  The search or
1e442 64 65 72 20 69 73 20 54 45 4d 50 20 66 69 72 73  der is TEMP firs
1e443 74 2c 20 74 68 65 6e 20 4d 41 49 4e 2c 20 74 68  t, then MAIN, th
1e444 65 6e 20 61 6e 79 0a 2a 2a 20 61 75 78 69 6c 69  en any.** auxili
1e445 61 72 79 20 64 61 74 61 62 61 73 65 73 20 61 64  ary databases ad
1e446 64 65 64 20 75 73 69 6e 67 20 74 68 65 20 41 54  ded using the AT
1e447 54 41 43 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a  TACH command..**
1e448 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 6c  .** See also sql
1e449 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28  ite3LocateTable(
1e44a 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
1e44b 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69  VATE Table *sqli
1e44c 74 65 33 46 69 6e 64 54 61 62 6c 65 28 73 71 6c  te3FindTable(sql
1e44d 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
1e44e 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e  char *zName, con
1e44f 73 74 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61  st char *zDataba
1e450 73 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 20  se){.  Table *p 
1e451 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  = 0;.  int i;.  
1e452 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 61 73 73  int nName;.  ass
1e453 65 72 74 28 20 7a 4e 61 6d 65 21 3d 30 20 29 3b  ert( zName!=0 );
1e454 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74  .  nName = sqlit
1e455 65 33 53 74 72 6c 65 6e 28 64 62 2c 20 7a 4e 61  e3Strlen(db, zNa
1e456 6d 65 29 20 2b 20 31 3b 0a 20 20 66 6f 72 28 69  me) + 1;.  for(i
1e457 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69 3c  =OMIT_TEMPDB; i<
1e458 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20  db->nDb; i++){. 
1e459 20 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32 29     int j = (i<2)
1e45a 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20 20 2f 2a   ? i^1 : i;   /*
1e45b 20 53 65 61 72 63 68 20 54 45 4d 50 20 62 65 66   Search TEMP bef
1e45c 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 20 20  ore MAIN */.    
1e45d 69 66 28 20 7a 44 61 74 61 62 61 73 65 21 3d 30  if( zDatabase!=0
1e45e 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43   && sqlite3StrIC
1e45f 6d 70 28 7a 44 61 74 61 62 61 73 65 2c 20 64 62  mp(zDatabase, db
1e460 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 29 20  ->aDb[j].zName) 
1e461 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
1e462 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46  p = sqlite3HashF
1e463 69 6e 64 28 26 64 62 2d 3e 61 44 62 5b 6a 5d 2e  ind(&db->aDb[j].
1e464 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68  pSchema->tblHash
1e465 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b  , zName, nName);
1e466 0a 20 20 20 20 69 66 28 20 70 20 29 20 62 72 65  .    if( p ) bre
1e467 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ak;.  }.  return
1e468 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63   p;.}../*.** Loc
1e469 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72  ate the in-memor
1e46a 79 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  y structure that
1e46b 20 64 65 73 63 72 69 62 65 73 20 61 20 70 61 72   describes a par
1e46c 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65  ticular database
1e46d 0a 2a 2a 20 74 61 62 6c 65 20 67 69 76 65 6e 20  .** table given 
1e46e 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 61 74  the name of that
1e46f 20 74 61 62 6c 65 20 61 6e 64 20 28 6f 70 74 69   table and (opti
1e470 6f 6e 61 6c 6c 79 29 20 74 68 65 20 6e 61 6d 65  onally) the name
1e471 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62   of the.** datab
1e472 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ase containing t
1e473 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72  he table.  Retur
1e474 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 66 6f  n NULL if not fo
1e475 75 6e 64 2e 20 20 41 6c 73 6f 20 6c 65 61 76 65  und.  Also leave
1e476 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73   an.** error mes
1e477 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 2d 3e  sage in pParse->
1e478 7a 45 72 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 54  zErrMsg..**.** T
1e479 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 65  he difference be
1e47a 74 77 65 65 6e 20 74 68 69 73 20 72 6f 75 74 69  tween this routi
1e47b 6e 65 20 61 6e 64 20 73 71 6c 69 74 65 33 46 69  ne and sqlite3Fi
1e47c 6e 64 54 61 62 6c 65 28 29 20 69 73 20 74 68 61  ndTable() is tha
1e47d 74 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e  t this.** routin
1e47e 65 20 6c 65 61 76 65 73 20 61 6e 20 65 72 72 6f  e leaves an erro
1e47f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61  r message in pPa
1e480 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 77 68 65  rse->zErrMsg whe
1e481 72 65 0a 2a 2a 20 73 71 6c 69 74 65 33 46 69 6e  re.** sqlite3Fin
1e482 64 54 61 62 6c 65 28 29 20 64 6f 65 73 20 6e 6f  dTable() does no
1e483 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
1e484 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69  VATE Table *sqli
1e485 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 0a  te3LocateTable(.
1e486 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
1e487 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6e 74           /* cont
1e488 65 78 74 20 69 6e 20 77 68 69 63 68 20 74 6f 20  ext in which to 
1e489 72 65 70 6f 72 74 20 65 72 72 6f 72 73 20 2a 2f  report errors */
1e48a 0a 20 20 69 6e 74 20 69 73 56 69 65 77 2c 20 20  .  int isView,  
1e48b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
1e48c 65 20 69 66 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72  e if looking for
1e48d 20 61 20 56 49 45 57 20 72 61 74 68 65 72 20 74   a VIEW rather t
1e48e 68 61 6e 20 61 20 54 41 42 4c 45 20 2a 2f 0a 20  han a TABLE */. 
1e48f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
1e490 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  me,     /* Name 
1e491 6f 66 20 74 68 65 20 74 61 62 6c 65 20 77 65 20  of the table we 
1e492 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20  are looking for 
1e493 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
1e494 2a 7a 44 62 61 73 65 20 20 20 20 20 2f 2a 20 4e  *zDbase     /* N
1e495 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
1e496 61 73 65 2e 20 20 4d 69 67 68 74 20 62 65 20 4e  ase.  Might be N
1e497 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c  ULL */.){.  Tabl
1e498 65 20 2a 70 3b 0a 0a 20 20 2f 2a 20 52 65 61 64  e *p;..  /* Read
1e499 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
1e49a 68 65 6d 61 2e 20 49 66 20 61 6e 20 65 72 72 6f  hema. If an erro
1e49b 72 20 6f 63 63 75 72 73 2c 20 6c 65 61 76 65 20  r occurs, leave 
1e49c 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
1e49d 0a 20 20 2a 2a 20 61 6e 64 20 63 6f 64 65 20 69  .  ** and code i
1e49e 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72 65 74  n pParse and ret
1e49f 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 69  urn NULL. */.  i
1e4a0 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71  f( SQLITE_OK!=sq
1e4a1 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
1e4a2 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 72  pParse) ){.    r
1e4a3 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20  eturn 0;.  }..  
1e4a4 70 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54  p = sqlite3FindT
1e4a5 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  able(pParse->db,
1e4a6 20 7a 4e 61 6d 65 2c 20 7a 44 62 61 73 65 29 3b   zName, zDbase);
1e4a7 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20  .  if( p==0 ){. 
1e4a8 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
1e4a9 4d 73 67 20 3d 20 69 73 56 69 65 77 20 3f 20 22  Msg = isView ? "
1e4aa 6e 6f 20 73 75 63 68 20 76 69 65 77 22 20 3a 20  no such view" : 
1e4ab 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65 22 3b  "no such table";
1e4ac 0a 20 20 20 20 69 66 28 20 7a 44 62 61 73 65 20  .    if( zDbase 
1e4ad 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1e4ae 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
1e4af 20 22 25 73 3a 20 25 73 2e 25 73 22 2c 20 7a 4d   "%s: %s.%s", zM
1e4b0 73 67 2c 20 7a 44 62 61 73 65 2c 20 7a 4e 61 6d  sg, zDbase, zNam
1e4b1 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  e);.    }else{. 
1e4b2 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1e4b3 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73  rMsg(pParse, "%s
1e4b4 3a 20 25 73 22 2c 20 7a 4d 73 67 2c 20 7a 4e 61  : %s", zMsg, zNa
1e4b5 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  me);.    }.    p
1e4b6 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68 65  Parse->checkSche
1e4b7 6d 61 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65  ma = 1;.  }.  re
1e4b8 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
1e4b9 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e 2d 6d   Locate the in-m
1e4ba 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 20  emory structure 
1e4bb 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 0a  that describes .
1e4bc 2a 2a 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  ** a particular 
1e4bd 69 6e 64 65 78 20 67 69 76 65 6e 20 74 68 65 20  index given the 
1e4be 6e 61 6d 65 20 6f 66 20 74 68 61 74 20 69 6e 64  name of that ind
1e4bf 65 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e 61  ex.** and the na
1e4c0 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
1e4c1 73 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  se that contains
1e4c2 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 52   the index..** R
1e4c3 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f  eturn NULL if no
1e4c4 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 49  t found..**.** I
1e4c5 66 20 7a 44 61 74 61 62 61 73 65 20 69 73 20 30  f zDatabase is 0
1e4c6 2c 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20  , all databases 
1e4c7 61 72 65 20 73 65 61 72 63 68 65 64 20 66 6f 72  are searched for
1e4c8 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 61 6e   the.** table an
1e4c9 64 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 63  d the first matc
1e4ca 68 69 6e 67 20 69 6e 64 65 78 20 69 73 20 72 65  hing index is re
1e4cb 74 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63 68 65  turned.  (No che
1e4cc 63 6b 69 6e 67 0a 2a 2a 20 66 6f 72 20 64 75 70  cking.** for dup
1e4cd 6c 69 63 61 74 65 20 69 6e 64 65 78 20 6e 61 6d  licate index nam
1e4ce 65 73 20 69 73 20 64 6f 6e 65 2e 29 20 20 54 68  es is done.)  Th
1e4cf 65 20 73 65 61 72 63 68 20 6f 72 64 65 72 20 69  e search order i
1e4d0 73 0a 2a 2a 20 54 45 4d 50 20 66 69 72 73 74 2c  s.** TEMP first,
1e4d1 20 74 68 65 6e 20 4d 41 49 4e 2c 20 74 68 65 6e   then MAIN, then
1e4d2 20 61 6e 79 20 61 75 78 69 6c 69 61 72 79 20 64   any auxiliary d
1e4d3 61 74 61 62 61 73 65 73 20 61 64 64 65 64 0a 2a  atabases added.*
1e4d4 2a 20 75 73 69 6e 67 20 74 68 65 20 41 54 54 41  * using the ATTA
1e4d5 43 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 53  CH command..*/.S
1e4d6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 6e  QLITE_PRIVATE In
1e4d7 64 65 78 20 2a 73 71 6c 69 74 65 33 46 69 6e 64  dex *sqlite3Find
1e4d8 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64  Index(sqlite3 *d
1e4d9 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
1e4da 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  Name, const char
1e4db 20 2a 7a 44 62 29 7b 0a 20 20 49 6e 64 65 78 20   *zDb){.  Index 
1e4dc 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b  *p = 0;.  int i;
1e4dd 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73  .  int nName = s
1e4de 71 6c 69 74 65 33 53 74 72 6c 65 6e 28 64 62 2c  qlite3Strlen(db,
1e4df 20 7a 4e 61 6d 65 29 2b 31 3b 0a 20 20 66 6f 72   zName)+1;.  for
1e4e0 28 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20  (i=OMIT_TEMPDB; 
1e4e1 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
1e4e2 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c  .    int j = (i<
1e4e3 32 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20 2f  2) ? i^1 : i;  /
1e4e4 2a 20 53 65 61 72 63 68 20 54 45 4d 50 20 62 65  * Search TEMP be
1e4e5 66 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 20  fore MAIN */.   
1e4e6 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61   Schema *pSchema
1e4e7 20 3d 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53   = db->aDb[j].pS
1e4e8 63 68 65 6d 61 3b 0a 20 20 20 20 69 66 28 20 7a  chema;.    if( z
1e4e9 44 62 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  Db && sqlite3Str
1e4ea 49 43 6d 70 28 7a 44 62 2c 20 64 62 2d 3e 61 44  ICmp(zDb, db->aD
1e4eb 62 5b 6a 5d 2e 7a 4e 61 6d 65 29 20 29 20 63 6f  b[j].zName) ) co
1e4ec 6e 74 69 6e 75 65 3b 0a 20 20 20 20 61 73 73 65  ntinue;.    asse
1e4ed 72 74 28 20 70 53 63 68 65 6d 61 20 7c 7c 20 28  rt( pSchema || (
1e4ee 6a 3d 3d 31 20 26 26 20 21 64 62 2d 3e 61 44 62  j==1 && !db->aDb
1e4ef 5b 31 5d 2e 70 42 74 29 20 29 3b 0a 20 20 20 20  [1].pBt) );.    
1e4f0 69 66 28 20 70 53 63 68 65 6d 61 20 29 7b 0a 20  if( pSchema ){. 
1e4f1 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33       p = sqlite3
1e4f2 48 61 73 68 46 69 6e 64 28 26 70 53 63 68 65 6d  HashFind(&pSchem
1e4f3 61 2d 3e 69 64 78 48 61 73 68 2c 20 7a 4e 61 6d  a->idxHash, zNam
1e4f4 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 7d  e, nName);.    }
1e4f5 0a 20 20 20 20 69 66 28 20 70 20 29 20 62 72 65  .    if( p ) bre
1e4f6 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ak;.  }.  return
1e4f7 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63   p;.}../*.** Rec
1e4f8 6c 61 69 6d 20 74 68 65 20 6d 65 6d 6f 72 79 20  laim the memory 
1e4f9 75 73 65 64 20 62 79 20 61 6e 20 69 6e 64 65 78  used by an index
1e4fa 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1e4fb 66 72 65 65 49 6e 64 65 78 28 49 6e 64 65 78 20  freeIndex(Index 
1e4fc 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  *p){.  sqlite3 *
1e4fd 64 62 20 3d 20 70 2d 3e 70 54 61 62 6c 65 2d 3e  db = p->pTable->
1e4fe 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  db;.  sqlite3DbF
1e4ff 72 65 65 28 64 62 2c 20 70 2d 3e 7a 43 6f 6c 41  ree(db, p->zColA
1e500 66 66 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  ff);.  sqlite3Db
1e501 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a  Free(db, p);.}..
1e502 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65  /*.** Remove the
1e503 20 67 69 76 65 6e 20 69 6e 64 65 78 20 66 72 6f   given index fro
1e504 6d 20 74 68 65 20 69 6e 64 65 78 20 68 61 73 68  m the index hash
1e505 20 74 61 62 6c 65 2c 20 61 6e 64 20 66 72 65 65   table, and free
1e506 0a 2a 2a 20 69 74 73 20 6d 65 6d 6f 72 79 20 73  .** its memory s
1e507 74 72 75 63 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a  tructures..**.**
1e508 20 54 68 65 20 69 6e 64 65 78 20 69 73 20 72 65   The index is re
1e509 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 64  moved from the d
1e50a 61 74 61 62 61 73 65 20 68 61 73 68 20 74 61 62  atabase hash tab
1e50b 6c 65 73 20 62 75 74 0a 2a 2a 20 69 74 20 69 73  les but.** it is
1e50c 20 6e 6f 74 20 75 6e 6c 69 6e 6b 65 64 20 66 72   not unlinked fr
1e50d 6f 6d 20 74 68 65 20 54 61 62 6c 65 20 74 68 61  om the Table tha
1e50e 74 20 69 74 20 69 6e 64 65 78 65 73 2e 0a 2a 2a  t it indexes..**
1e50f 20 55 6e 6c 69 6e 6b 69 6e 67 20 66 72 6f 6d 20   Unlinking from 
1e510 74 68 65 20 54 61 62 6c 65 20 6d 75 73 74 20 62  the Table must b
1e511 65 20 64 6f 6e 65 20 62 79 20 74 68 65 20 63 61  e done by the ca
1e512 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a  lling function..
1e513 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
1e514 71 6c 69 74 65 44 65 6c 65 74 65 49 6e 64 65 78  qliteDeleteIndex
1e515 28 49 6e 64 65 78 20 2a 70 29 7b 0a 20 20 49 6e  (Index *p){.  In
1e516 64 65 78 20 2a 70 4f 6c 64 3b 0a 20 20 63 6f 6e  dex *pOld;.  con
1e517 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d  st char *zName =
1e518 20 70 2d 3e 7a 4e 61 6d 65 3b 0a 0a 20 20 70 4f   p->zName;..  pO
1e519 6c 64 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68  ld = sqlite3Hash
1e51a 49 6e 73 65 72 74 28 26 70 2d 3e 70 53 63 68 65  Insert(&p->pSche
1e51b 6d 61 2d 3e 69 64 78 48 61 73 68 2c 20 7a 4e 61  ma->idxHash, zNa
1e51c 6d 65 2c 20 73 74 72 6c 65 6e 28 7a 4e 61 6d 65  me, strlen(zName
1e51d 29 2b 31 2c 20 30 29 3b 0a 20 20 61 73 73 65 72  )+1, 0);.  asser
1e51e 74 28 20 70 4f 6c 64 3d 3d 30 20 7c 7c 20 70 4f  t( pOld==0 || pO
1e51f 6c 64 3d 3d 70 20 29 3b 0a 20 20 66 72 65 65 49  ld==p );.  freeI
1e520 6e 64 65 78 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  ndex(p);.}../*.*
1e521 2a 20 46 6f 72 20 74 68 65 20 69 6e 64 65 78 20  * For the index 
1e522 63 61 6c 6c 65 64 20 7a 49 64 78 4e 61 6d 65 20  called zIdxName 
1e523 77 68 69 63 68 20 69 73 20 66 6f 75 6e 64 20 69  which is found i
1e524 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  n the database i
1e525 44 62 2c 0a 2a 2a 20 75 6e 6c 69 6b 65 20 74 68  Db,.** unlike th
1e526 61 74 20 69 6e 64 65 78 20 66 72 6f 6d 20 69 74  at index from it
1e527 73 20 54 61 62 6c 65 20 74 68 65 6e 20 72 65 6d  s Table then rem
1e528 6f 76 65 20 74 68 65 20 69 6e 64 65 78 20 66 72  ove the index fr
1e529 6f 6d 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20  om.** the index 
1e52a 68 61 73 68 20 74 61 62 6c 65 20 61 6e 64 20 66  hash table and f
1e52b 72 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 73  ree all memory s
1e52c 74 72 75 63 74 75 72 65 73 20 61 73 73 6f 63 69  tructures associ
1e52d 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65  ated.** with the
1e52e 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54   index..*/.SQLIT
1e52f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1e530 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44  qlite3UnlinkAndD
1e531 65 6c 65 74 65 49 6e 64 65 78 28 73 71 6c 69 74  eleteIndex(sqlit
1e532 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c  e3 *db, int iDb,
1e533 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 64   const char *zId
1e534 78 4e 61 6d 65 29 7b 0a 20 20 49 6e 64 65 78 20  xName){.  Index 
1e535 2a 70 49 6e 64 65 78 3b 0a 20 20 69 6e 74 20 6c  *pIndex;.  int l
1e536 65 6e 3b 0a 20 20 48 61 73 68 20 2a 70 48 61 73  en;.  Hash *pHas
1e537 68 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62  h = &db->aDb[iDb
1e538 5d 2e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61  ].pSchema->idxHa
1e539 73 68 3b 0a 0a 20 20 6c 65 6e 20 3d 20 73 71 6c  sh;..  len = sql
1e53a 69 74 65 33 53 74 72 6c 65 6e 28 64 62 2c 20 7a  ite3Strlen(db, z
1e53b 49 64 78 4e 61 6d 65 29 3b 0a 20 20 70 49 6e 64  IdxName);.  pInd
1e53c 65 78 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68  ex = sqlite3Hash
1e53d 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 7a 49  Insert(pHash, zI
1e53e 64 78 4e 61 6d 65 2c 20 6c 65 6e 2b 31 2c 20 30  dxName, len+1, 0
1e53f 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78 20  );.  if( pIndex 
1e540 29 7b 0a 20 20 20 20 69 66 28 20 70 49 6e 64 65  ){.    if( pInde
1e541 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65  x->pTable->pInde
1e542 78 3d 3d 70 49 6e 64 65 78 20 29 7b 0a 20 20 20  x==pIndex ){.   
1e543 20 20 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c     pIndex->pTabl
1e544 65 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49 6e 64  e->pIndex = pInd
1e545 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d  ex->pNext;.    }
1e546 65 6c 73 65 7b 0a 20 20 20 20 20 20 49 6e 64 65  else{.      Inde
1e547 78 20 2a 70 3b 0a 20 20 20 20 20 20 66 6f 72 28  x *p;.      for(
1e548 70 3d 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65  p=pIndex->pTable
1e549 2d 3e 70 49 6e 64 65 78 3b 20 70 20 26 26 20 70  ->pIndex; p && p
1e54a 2d 3e 70 4e 65 78 74 21 3d 70 49 6e 64 65 78 3b  ->pNext!=pIndex;
1e54b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 7d 0a 20   p=p->pNext){}. 
1e54c 20 20 20 20 20 69 66 28 20 70 20 26 26 20 70 2d       if( p && p-
1e54d 3e 70 4e 65 78 74 3d 3d 70 49 6e 64 65 78 20 29  >pNext==pIndex )
1e54e 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65  {.        p->pNe
1e54f 78 74 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e 65  xt = pIndex->pNe
1e550 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  xt;.      }.    
1e551 7d 0a 20 20 20 20 66 72 65 65 49 6e 64 65 78 28  }.    freeIndex(
1e552 70 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 20 20 64  pIndex);.  }.  d
1e553 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49  b->flags |= SQLI
1e554 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73  TE_InternChanges
1e555 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65  ;.}../*.** Erase
1e556 20 61 6c 6c 20 73 63 68 65 6d 61 20 69 6e 66 6f   all schema info
1e557 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65  rmation from the
1e558 20 69 6e 2d 6d 65 6d 6f 72 79 20 68 61 73 68 20   in-memory hash 
1e559 74 61 62 6c 65 73 20 6f 66 0a 2a 2a 20 61 20 73  tables of.** a s
1e55a 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 2e 20  ingle database. 
1e55b 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
1e55c 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 63 6c 61   called to recla
1e55d 69 6d 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 66  im memory.** bef
1e55e 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ore the database
1e55f 20 63 6c 6f 73 65 73 2e 20 20 49 74 20 69 73 20   closes.  It is 
1e560 61 6c 73 6f 20 63 61 6c 6c 65 64 20 64 75 72 69  also called duri
1e561 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a  ng a rollback.**
1e562 20 69 66 20 74 68 65 72 65 20 77 65 72 65 20 73   if there were s
1e563 63 68 65 6d 61 20 63 68 61 6e 67 65 73 20 64 75  chema changes du
1e564 72 69 6e 67 20 74 68 65 20 74 72 61 6e 73 61 63  ring the transac
1e565 74 69 6f 6e 20 6f 72 20 69 66 20 61 0a 2a 2a 20  tion or if a.** 
1e566 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 20 6d 69  schema-cookie mi
1e567 73 6d 61 74 63 68 20 6f 63 63 75 72 73 2e 0a 2a  smatch occurs..*
1e568 2a 0a 2a 2a 20 49 66 20 69 44 62 3c 3d 30 20 74  *.** If iDb<=0 t
1e569 68 65 6e 20 72 65 73 65 74 20 74 68 65 20 69 6e  hen reset the in
1e56a 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 74 61  ternal schema ta
1e56b 62 6c 65 73 20 66 6f 72 20 61 6c 6c 20 64 61 74  bles for all dat
1e56c 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 20  abase.** files. 
1e56d 20 49 66 20 69 44 62 3e 3d 32 20 74 68 65 6e 20   If iDb>=2 then 
1e56e 72 65 73 65 74 20 74 68 65 20 69 6e 74 65 72 6e  reset the intern
1e56f 61 6c 20 73 63 68 65 6d 61 20 66 6f 72 20 6f 6e  al schema for on
1e570 6c 79 20 74 68 65 0a 2a 2a 20 73 69 6e 67 6c 65  ly the.** single
1e571 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 2e   file indicated.
1e572 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1e573 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
1e574 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65  esetInternalSche
1e575 6d 61 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ma(sqlite3 *db, 
1e576 69 6e 74 20 69 44 62 29 7b 0a 20 20 69 6e 74 20  int iDb){.  int 
1e577 69 2c 20 6a 3b 0a 20 20 61 73 73 65 72 74 28 20  i, j;.  assert( 
1e578 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62  iDb>=0 && iDb<db
1e579 2d 3e 6e 44 62 20 29 3b 0a 0a 20 20 69 66 28 20  ->nDb );..  if( 
1e57a 69 44 62 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  iDb==0 ){.    sq
1e57b 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41  lite3BtreeEnterA
1e57c 6c 6c 28 64 62 29 3b 0a 20 20 7d 0a 20 20 66 6f  ll(db);.  }.  fo
1e57d 72 28 69 3d 69 44 62 3b 20 69 3c 64 62 2d 3e 6e  r(i=iDb; i<db->n
1e57e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 44 62  Db; i++){.    Db
1e57f 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62   *pDb = &db->aDb
1e580 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 62  [i];.    if( pDb
1e581 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20  ->pSchema ){.   
1e582 20 20 20 61 73 73 65 72 74 28 69 3d 3d 31 20 7c     assert(i==1 |
1e583 7c 20 28 70 44 62 2d 3e 70 42 74 20 26 26 20 73  | (pDb->pBt && s
1e584 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
1e585 4d 75 74 65 78 28 70 44 62 2d 3e 70 42 74 29 29  Mutex(pDb->pBt))
1e586 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1e587 53 63 68 65 6d 61 46 72 65 65 28 70 44 62 2d 3e  SchemaFree(pDb->
1e588 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d 0a  pSchema);.    }.
1e589 20 20 20 20 69 66 28 20 69 44 62 3e 30 20 29 20      if( iDb>0 ) 
1e58a 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73  return;.  }.  as
1e58b 73 65 72 74 28 20 69 44 62 3d 3d 30 20 29 3b 0a  sert( iDb==0 );.
1e58c 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e    db->flags &= ~
1e58d 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61  SQLITE_InternCha
1e58e 6e 67 65 73 3b 0a 20 20 73 71 6c 69 74 65 33 42  nges;.  sqlite3B
1e58f 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29  treeLeaveAll(db)
1e590 3b 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 6f  ;..  /* If one o
1e591 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 61 75  r more of the au
1e592 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65  xiliary database
1e593 20 66 69 6c 65 73 20 68 61 73 20 62 65 65 6e 20   files has been 
1e594 63 6c 6f 73 65 64 2c 0a 20 20 2a 2a 20 74 68 65  closed,.  ** the
1e595 6e 20 72 65 6d 6f 76 65 20 74 68 65 6d 20 66 72  n remove them fr
1e596 6f 6d 20 74 68 65 20 61 75 78 69 6c 69 61 72 79  om the auxiliary
1e597 20 64 61 74 61 62 61 73 65 20 6c 69 73 74 2e 20   database list. 
1e598 20 57 65 20 74 61 6b 65 20 74 68 65 0a 20 20 2a   We take the.  *
1e599 2a 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f  * opportunity to
1e59a 20 64 6f 20 74 68 69 73 20 68 65 72 65 20 73 69   do this here si
1e59b 6e 63 65 20 77 65 20 68 61 76 65 20 6a 75 73 74  nce we have just
1e59c 20 64 65 6c 65 74 65 64 20 61 6c 6c 20 6f 66 20   deleted all of 
1e59d 74 68 65 0a 20 20 2a 2a 20 73 63 68 65 6d 61 20  the.  ** schema 
1e59e 68 61 73 68 20 74 61 62 6c 65 73 20 61 6e 64 20  hash tables and 
1e59f 74 68 65 72 65 66 6f 72 65 20 64 6f 20 6e 6f 74  therefore do not
1e5a0 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20 61 6e   have to make an
1e5a1 79 20 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20 74  y changes.  ** t
1e5a2 6f 20 61 6e 79 20 6f 66 20 74 68 6f 73 65 20 74  o any of those t
1e5a3 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f  ables..  */.  fo
1e5a4 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62  r(i=0; i<db->nDb
1e5a5 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75  ; i++){.    stru
1e5a6 63 74 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62  ct Db *pDb = &db
1e5a7 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 66  ->aDb[i];.    if
1e5a8 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 7b  ( pDb->pBt==0 ){
1e5a9 0a 20 20 20 20 20 20 69 66 28 20 70 44 62 2d 3e  .      if( pDb->
1e5aa 70 41 75 78 20 26 26 20 70 44 62 2d 3e 78 46 72  pAux && pDb->xFr
1e5ab 65 65 41 75 78 20 29 20 70 44 62 2d 3e 78 46 72  eeAux ) pDb->xFr
1e5ac 65 65 41 75 78 28 70 44 62 2d 3e 70 41 75 78 29  eeAux(pDb->pAux)
1e5ad 3b 0a 20 20 20 20 20 20 70 44 62 2d 3e 70 41 75  ;.      pDb->pAu
1e5ae 78 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  x = 0;.    }.  }
1e5af 0a 20 20 66 6f 72 28 69 3d 6a 3d 32 3b 20 69 3c  .  for(i=j=2; i<
1e5b0 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20  db->nDb; i++){. 
1e5b1 20 20 20 73 74 72 75 63 74 20 44 62 20 2a 70 44     struct Db *pD
1e5b2 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b  b = &db->aDb[i];
1e5b3 0a 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 42  .    if( pDb->pB
1e5b4 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  t==0 ){.      sq
1e5b5 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1e5b6 70 44 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  pDb->zName);.   
1e5b7 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 20 3d 20     pDb->zName = 
1e5b8 30 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75  0;.      continu
1e5b9 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  e;.    }.    if(
1e5ba 20 6a 3c 69 20 29 7b 0a 20 20 20 20 20 20 64 62   j<i ){.      db
1e5bb 2d 3e 61 44 62 5b 6a 5d 20 3d 20 64 62 2d 3e 61  ->aDb[j] = db->a
1e5bc 44 62 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20  Db[i];.    }.   
1e5bd 20 6a 2b 2b 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73   j++;.  }.  mems
1e5be 65 74 28 26 64 62 2d 3e 61 44 62 5b 6a 5d 2c 20  et(&db->aDb[j], 
1e5bf 30 2c 20 28 64 62 2d 3e 6e 44 62 2d 6a 29 2a 73  0, (db->nDb-j)*s
1e5c0 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 6a 5d  izeof(db->aDb[j]
1e5c1 29 29 3b 0a 20 20 64 62 2d 3e 6e 44 62 20 3d 20  ));.  db->nDb = 
1e5c2 6a 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6e 44 62  j;.  if( db->nDb
1e5c3 3c 3d 32 20 26 26 20 64 62 2d 3e 61 44 62 21 3d  <=2 && db->aDb!=
1e5c4 64 62 2d 3e 61 44 62 53 74 61 74 69 63 20 29 7b  db->aDbStatic ){
1e5c5 0a 20 20 20 20 6d 65 6d 63 70 79 28 64 62 2d 3e  .    memcpy(db->
1e5c6 61 44 62 53 74 61 74 69 63 2c 20 64 62 2d 3e 61  aDbStatic, db->a
1e5c7 44 62 2c 20 32 2a 73 69 7a 65 6f 66 28 64 62 2d  Db, 2*sizeof(db-
1e5c8 3e 61 44 62 5b 30 5d 29 29 3b 0a 20 20 20 20 73  >aDb[0]));.    s
1e5c9 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1e5ca 20 64 62 2d 3e 61 44 62 29 3b 0a 20 20 20 20 64   db->aDb);.    d
1e5cb 62 2d 3e 61 44 62 20 3d 20 64 62 2d 3e 61 44 62  b->aDb = db->aDb
1e5cc 53 74 61 74 69 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f  Static;.  }.}../
1e5cd 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1e5ce 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  e is called when
1e5cf 20 61 20 63 6f 6d 6d 69 74 20 6f 63 63 75 72 73   a commit occurs
1e5d0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1e5d1 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1e5d2 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68  CommitInternalCh
1e5d3 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20 2a 64  anges(sqlite3 *d
1e5d4 62 29 7b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20  b){.  db->flags 
1e5d5 26 3d 20 7e 53 51 4c 49 54 45 5f 49 6e 74 65 72  &= ~SQLITE_Inter
1e5d6 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a  nChanges;.}../*.
1e5d7 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63 6f 6c  ** Clear the col
1e5d8 75 6d 6e 20 6e 61 6d 65 73 20 66 72 6f 6d 20 61  umn names from a
1e5d9 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 2e 0a   table or view..
1e5da 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
1e5db 71 6c 69 74 65 52 65 73 65 74 43 6f 6c 75 6d 6e  qliteResetColumn
1e5dc 4e 61 6d 65 73 28 54 61 62 6c 65 20 2a 70 54 61  Names(Table *pTa
1e5dd 62 6c 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  ble){.  int i;. 
1e5de 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20   Column *pCol;. 
1e5df 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
1e5e0 54 61 62 6c 65 2d 3e 64 62 3b 0a 20 20 61 73 73  Table->db;.  ass
1e5e1 65 72 74 28 20 70 54 61 62 6c 65 21 3d 30 20 29  ert( pTable!=0 )
1e5e2 3b 0a 20 20 69 66 28 20 28 70 43 6f 6c 20 3d 20  ;.  if( (pCol = 
1e5e3 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 29 21 3d 30  pTable->aCol)!=0
1e5e4 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
1e5e5 20 69 3c 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3b   i<pTable->nCol;
1e5e6 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20   i++, pCol++){. 
1e5e7 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
1e5e8 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 4e 61  ee(db, pCol->zNa
1e5e9 6d 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  me);.      sqlit
1e5ea 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
1e5eb 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 29 3b 0a 20   pCol->pDflt);. 
1e5ec 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
1e5ed 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 54 79  ee(db, pCol->zTy
1e5ee 70 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  pe);.      sqlit
1e5ef 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 43 6f  e3DbFree(db, pCo
1e5f0 6c 2d 3e 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 7d  l->zColl);.    }
1e5f1 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
1e5f2 65 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 61  ee(db, pTable->a
1e5f3 43 6f 6c 29 3b 0a 20 20 7d 0a 20 20 70 54 61 62  Col);.  }.  pTab
1e5f4 6c 65 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20  le->aCol = 0;.  
1e5f5 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 30  pTable->nCol = 0
1e5f6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76  ;.}../*.** Remov
1e5f7 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 64 61 74  e the memory dat
1e5f8 61 20 73 74 72 75 63 74 75 72 65 73 20 61 73 73  a structures ass
1e5f9 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
1e5fa 20 67 69 76 65 6e 0a 2a 2a 20 54 61 62 6c 65 2e   given.** Table.
1e5fb 20 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 72 65    No changes are
1e5fc 20 6d 61 64 65 20 74 6f 20 64 69 73 6b 20 62 79   made to disk by
1e5fd 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a   this routine..*
1e5fe 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1e5ff 65 20 6a 75 73 74 20 64 65 6c 65 74 65 73 20 74  e just deletes t
1e600 68 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72  he data structur
1e601 65 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20  e.  It does not 
1e602 75 6e 6c 69 6e 6b 0a 2a 2a 20 74 68 65 20 74 61  unlink.** the ta
1e603 62 6c 65 20 64 61 74 61 20 73 74 72 75 63 74 75  ble data structu
1e604 72 65 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68  re from the hash
1e605 20 74 61 62 6c 65 2e 20 20 4e 6f 72 20 64 6f 65   table.  Nor doe
1e606 73 20 69 74 20 72 65 6d 6f 76 65 0a 2a 2a 20 66  s it remove.** f
1e607 6f 72 65 69 67 6e 20 6b 65 79 73 20 66 72 6f 6d  oreign keys from
1e608 20 74 68 65 20 73 71 6c 69 74 65 2e 61 46 4b 65   the sqlite.aFKe
1e609 79 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 42  y hash table.  B
1e60a 75 74 20 69 74 20 64 6f 65 73 20 64 65 73 74 72  ut it does destr
1e60b 6f 79 0a 2a 2a 20 6d 65 6d 6f 72 79 20 73 74 72  oy.** memory str
1e60c 75 63 74 75 72 65 73 20 6f 66 20 74 68 65 20 69  uctures of the i
1e60d 6e 64 69 63 65 73 20 61 6e 64 20 66 6f 72 65 69  ndices and forei
1e60e 67 6e 20 6b 65 79 73 20 61 73 73 6f 63 69 61 74  gn keys associat
1e60f 65 64 20 77 69 74 68 20 0a 2a 2a 20 74 68 65 20  ed with .** the 
1e610 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  table..*/.SQLITE
1e611 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1e612 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65  lite3DeleteTable
1e613 28 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 29 7b  (Table *pTable){
1e614 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78  .  Index *pIndex
1e615 2c 20 2a 70 4e 65 78 74 3b 0a 20 20 46 4b 65 79  , *pNext;.  FKey
1e616 20 2a 70 46 4b 65 79 2c 20 2a 70 4e 65 78 74 46   *pFKey, *pNextF
1e617 4b 65 79 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  Key;.  sqlite3 *
1e618 64 62 3b 0a 0a 20 20 69 66 28 20 70 54 61 62 6c  db;..  if( pTabl
1e619 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  e==0 ) return;. 
1e61a 20 64 62 20 3d 20 70 54 61 62 6c 65 2d 3e 64 62   db = pTable->db
1e61b 3b 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 64  ;..  /* Do not d
1e61c 65 6c 65 74 65 20 74 68 65 20 74 61 62 6c 65 20  elete the table 
1e61d 75 6e 74 69 6c 20 74 68 65 20 72 65 66 65 72 65  until the refere
1e61e 6e 63 65 20 63 6f 75 6e 74 20 72 65 61 63 68 65  nce count reache
1e61f 73 20 7a 65 72 6f 2e 20 2a 2f 0a 20 20 70 54 61  s zero. */.  pTa
1e620 62 6c 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69  ble->nRef--;.  i
1e621 66 28 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66 3e  f( pTable->nRef>
1e622 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  0 ){.    return;
1e623 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
1e624 54 61 62 6c 65 2d 3e 6e 52 65 66 3d 3d 30 20 29  Table->nRef==0 )
1e625 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 61  ;..  /* Delete a
1e626 6c 6c 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63  ll indices assoc
1e627 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20  iated with this 
1e628 74 61 62 6c 65 0a 20 20 2a 2f 0a 20 20 66 6f 72  table.  */.  for
1e629 28 70 49 6e 64 65 78 20 3d 20 70 54 61 62 6c 65  (pIndex = pTable
1e62a 2d 3e 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78  ->pIndex; pIndex
1e62b 3b 20 70 49 6e 64 65 78 3d 70 4e 65 78 74 29 7b  ; pIndex=pNext){
1e62c 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70 49 6e  .    pNext = pIn
1e62d 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  dex->pNext;.    
1e62e 61 73 73 65 72 74 28 20 70 49 6e 64 65 78 2d 3e  assert( pIndex->
1e62f 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 6c 65 2d  pSchema==pTable-
1e630 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20  >pSchema );.    
1e631 73 71 6c 69 74 65 44 65 6c 65 74 65 49 6e 64 65  sqliteDeleteInde
1e632 78 28 70 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 0a  x(pIndex);.  }..
1e633 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1e634 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a  MIT_FOREIGN_KEY.
1e635 20 20 2f 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20    /* Delete all 
1e636 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 73 73  foreign keys ass
1e637 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69  ociated with thi
1e638 73 20 74 61 62 6c 65 2e 20 20 54 68 65 20 6b 65  s table.  The ke
1e639 79 73 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 68  ys.  ** should h
1e63a 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e  ave already been
1e63b 20 75 6e 6c 69 6e 6b 65 64 20 66 72 6f 6d 20 74   unlinked from t
1e63c 68 65 20 70 53 63 68 65 6d 61 2d 3e 61 46 4b 65  he pSchema->aFKe
1e63d 79 20 68 61 73 68 20 74 61 62 6c 65 20 0a 20 20  y hash table .  
1e63e 2a 2f 0a 20 20 66 6f 72 28 70 46 4b 65 79 3d 70  */.  for(pFKey=p
1e63f 54 61 62 6c 65 2d 3e 70 46 4b 65 79 3b 20 70 46  Table->pFKey; pF
1e640 4b 65 79 3b 20 70 46 4b 65 79 3d 70 4e 65 78 74  Key; pFKey=pNext
1e641 46 4b 65 79 29 7b 0a 20 20 20 20 70 4e 65 78 74  FKey){.    pNext
1e642 46 4b 65 79 20 3d 20 70 46 4b 65 79 2d 3e 70 4e  FKey = pFKey->pN
1e643 65 78 74 46 72 6f 6d 3b 0a 20 20 20 20 61 73 73  extFrom;.    ass
1e644 65 72 74 28 20 73 71 6c 69 74 65 33 48 61 73 68  ert( sqlite3Hash
1e645 46 69 6e 64 28 26 70 54 61 62 6c 65 2d 3e 70 53  Find(&pTable->pS
1e646 63 68 65 6d 61 2d 3e 61 46 4b 65 79 2c 0a 20 20  chema->aFKey,.  
1e647 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e648 20 20 20 20 20 20 20 20 20 70 46 4b 65 79 2d 3e           pFKey->
1e649 7a 54 6f 2c 20 73 74 72 6c 65 6e 28 70 46 4b 65  zTo, strlen(pFKe
1e64a 79 2d 3e 7a 54 6f 29 2b 31 29 21 3d 70 46 4b 65  y->zTo)+1)!=pFKe
1e64b 79 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  y );.    sqlite3
1e64c 44 62 46 72 65 65 28 64 62 2c 20 70 46 4b 65 79  DbFree(db, pFKey
1e64d 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  );.  }.#endif.. 
1e64e 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 54   /* Delete the T
1e64f 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 69  able structure i
1e650 74 73 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20 73 71  tself..  */.  sq
1e651 6c 69 74 65 52 65 73 65 74 43 6f 6c 75 6d 6e 4e  liteResetColumnN
1e652 61 6d 65 73 28 70 54 61 62 6c 65 29 3b 0a 20 20  ames(pTable);.  
1e653 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1e654 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 29  , pTable->zName)
1e655 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
1e656 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 7a 43  e(db, pTable->zC
1e657 6f 6c 41 66 66 29 3b 0a 20 20 73 71 6c 69 74 65  olAff);.  sqlite
1e658 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
1e659 2c 20 70 54 61 62 6c 65 2d 3e 70 53 65 6c 65 63  , pTable->pSelec
1e65a 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  t);.#ifndef SQLI
1e65b 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20  TE_OMIT_CHECK.  
1e65c 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
1e65d 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 70 43  e(db, pTable->pC
1e65e 68 65 63 6b 29 3b 0a 23 65 6e 64 69 66 0a 20 20  heck);.#endif.  
1e65f 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65 61 72  sqlite3VtabClear
1e660 28 70 54 61 62 6c 65 29 3b 0a 20 20 73 71 6c 69  (pTable);.  sqli
1e661 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54  te3DbFree(db, pT
1e662 61 62 6c 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  able);.}../*.** 
1e663 55 6e 6c 69 6e 6b 20 74 68 65 20 67 69 76 65 6e  Unlink the given
1e664 20 74 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20   table from the 
1e665 68 61 73 68 20 74 61 62 6c 65 73 20 61 6e 64 20  hash tables and 
1e666 74 68 65 20 64 65 6c 65 74 65 20 74 68 65 0a 2a  the delete the.*
1e667 2a 20 74 61 62 6c 65 20 73 74 72 75 63 74 75 72  * table structur
1e668 65 20 77 69 74 68 20 61 6c 6c 20 69 74 73 20 69  e with all its i
1e669 6e 64 69 63 65 73 20 61 6e 64 20 66 6f 72 65 69  ndices and forei
1e66a 67 6e 20 6b 65 79 73 2e 0a 2a 2f 0a 53 51 4c 49  gn keys..*/.SQLI
1e66b 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1e66c 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64  sqlite3UnlinkAnd
1e66d 44 65 6c 65 74 65 54 61 62 6c 65 28 73 71 6c 69  DeleteTable(sqli
1e66e 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62  te3 *db, int iDb
1e66f 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54  , const char *zT
1e670 61 62 4e 61 6d 65 29 7b 0a 20 20 54 61 62 6c 65  abName){.  Table
1e671 20 2a 70 3b 0a 20 20 46 4b 65 79 20 2a 70 46 31   *p;.  FKey *pF1
1e672 2c 20 2a 70 46 32 3b 0a 20 20 44 62 20 2a 70 44  , *pF2;.  Db *pD
1e673 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62  b;..  assert( db
1e674 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
1e675 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64   iDb>=0 && iDb<d
1e676 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65  b->nDb );.  asse
1e677 72 74 28 20 7a 54 61 62 4e 61 6d 65 20 26 26 20  rt( zTabName && 
1e678 7a 54 61 62 4e 61 6d 65 5b 30 5d 20 29 3b 0a 20  zTabName[0] );. 
1e679 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b   pDb = &db->aDb[
1e67a 69 44 62 5d 3b 0a 20 20 70 20 3d 20 73 71 6c 69  iDb];.  p = sqli
1e67b 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70  te3HashInsert(&p
1e67c 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 74 62 6c  Db->pSchema->tbl
1e67d 48 61 73 68 2c 20 7a 54 61 62 4e 61 6d 65 2c 20  Hash, zTabName, 
1e67e 73 74 72 6c 65 6e 28 7a 54 61 62 4e 61 6d 65 29  strlen(zTabName)
1e67f 2b 31 2c 30 29 3b 0a 20 20 69 66 28 20 70 20 29  +1,0);.  if( p )
1e680 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
1e681 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45  _OMIT_FOREIGN_KE
1e682 59 0a 20 20 20 20 66 6f 72 28 70 46 31 3d 70 2d  Y.    for(pF1=p-
1e683 3e 70 46 4b 65 79 3b 20 70 46 31 3b 20 70 46 31  >pFKey; pF1; pF1
1e684 3d 70 46 31 2d 3e 70 4e 65 78 74 46 72 6f 6d 29  =pF1->pNextFrom)
1e685 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 54 6f 20  {.      int nTo 
1e686 3d 20 73 74 72 6c 65 6e 28 70 46 31 2d 3e 7a 54  = strlen(pF1->zT
1e687 6f 29 20 2b 20 31 3b 0a 20 20 20 20 20 20 70 46  o) + 1;.      pF
1e688 32 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46  2 = sqlite3HashF
1e689 69 6e 64 28 26 70 44 62 2d 3e 70 53 63 68 65 6d  ind(&pDb->pSchem
1e68a 61 2d 3e 61 46 4b 65 79 2c 20 70 46 31 2d 3e 7a  a->aFKey, pF1->z
1e68b 54 6f 2c 20 6e 54 6f 29 3b 0a 20 20 20 20 20 20  To, nTo);.      
1e68c 69 66 28 20 70 46 32 3d 3d 70 46 31 20 29 7b 0a  if( pF2==pF1 ){.
1e68d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 48          sqlite3H
1e68e 61 73 68 49 6e 73 65 72 74 28 26 70 44 62 2d 3e  ashInsert(&pDb->
1e68f 70 53 63 68 65 6d 61 2d 3e 61 46 4b 65 79 2c 20  pSchema->aFKey, 
1e690 70 46 31 2d 3e 7a 54 6f 2c 20 6e 54 6f 2c 20 70  pF1->zTo, nTo, p
1e691 46 31 2d 3e 70 4e 65 78 74 54 6f 29 3b 0a 20 20  F1->pNextTo);.  
1e692 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1e693 20 20 20 77 68 69 6c 65 28 20 70 46 32 20 26 26     while( pF2 &&
1e694 20 70 46 32 2d 3e 70 4e 65 78 74 54 6f 21 3d 70   pF2->pNextTo!=p
1e695 46 31 20 29 7b 20 70 46 32 3d 70 46 32 2d 3e 70  F1 ){ pF2=pF2->p
1e696 4e 65 78 74 54 6f 3b 20 7d 0a 20 20 20 20 20 20  NextTo; }.      
1e697 20 20 69 66 28 20 70 46 32 20 29 7b 0a 20 20 20    if( pF2 ){.   
1e698 20 20 20 20 20 20 20 70 46 32 2d 3e 70 4e 65 78         pF2->pNex
1e699 74 54 6f 20 3d 20 70 46 31 2d 3e 70 4e 65 78 74  tTo = pF1->pNext
1e69a 54 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  To;.        }.  
1e69b 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64      }.    }.#end
1e69c 69 66 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  if.    sqlite3De
1e69d 6c 65 74 65 54 61 62 6c 65 28 70 29 3b 0a 20 20  leteTable(p);.  
1e69e 7d 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d  }.  db->flags |=
1e69f 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68   SQLITE_InternCh
1e6a0 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  anges;.}../*.** 
1e6a1 47 69 76 65 6e 20 61 20 74 6f 6b 65 6e 2c 20 72  Given a token, r
1e6a2 65 74 75 72 6e 20 61 20 73 74 72 69 6e 67 20 74  eturn a string t
1e6a3 68 61 74 20 63 6f 6e 73 69 73 74 73 20 6f 66 20  hat consists of 
1e6a4 74 68 65 20 74 65 78 74 20 6f 66 20 74 68 61 74  the text of that
1e6a5 0a 2a 2a 20 74 6f 6b 65 6e 20 77 69 74 68 20 61  .** token with a
1e6a6 6e 79 20 71 75 6f 74 61 74 69 6f 6e 73 20 72 65  ny quotations re
1e6a7 6d 6f 76 65 64 2e 20 20 53 70 61 63 65 20 74 6f  moved.  Space to
1e6a8 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e   hold the return
1e6a9 65 64 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20  ed string.** is 
1e6aa 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
1e6ab 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64  liteMalloc() and
1e6ac 20 6d 75 73 74 20 62 65 20 66 72 65 65 64 20 62   must be freed b
1e6ad 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a  y the calling.**
1e6ae 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
1e6af 20 54 6f 6b 65 6e 73 20 61 72 65 20 6f 66 74 65   Tokens are ofte
1e6b0 6e 20 6a 75 73 74 20 70 6f 69 6e 74 65 72 73 20  n just pointers 
1e6b1 69 6e 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61  into the origina
1e6b2 6c 20 53 51 4c 20 74 65 78 74 20 61 6e 64 20 73  l SQL text and s
1e6b3 6f 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 5c 30 30  o.** are not \00
1e6b4 30 20 74 65 72 6d 69 6e 61 74 65 64 20 61 6e 64  0 terminated and
1e6b5 20 61 72 65 20 6e 6f 74 20 70 65 72 73 69 73 74   are not persist
1e6b6 65 6e 74 2e 20 20 54 68 65 20 72 65 74 75 72 6e  ent.  The return
1e6b7 65 64 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20  ed string.** is 
1e6b8 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 20  \000 terminated 
1e6b9 61 6e 64 20 69 73 20 70 65 72 73 69 73 74 65 6e  and is persisten
1e6ba 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
1e6bb 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
1e6bc 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
1e6bd 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 6f 6b  sqlite3 *db, Tok
1e6be 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 63 68  en *pName){.  ch
1e6bf 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69 66 28  ar *zName;.  if(
1e6c0 20 70 4e 61 6d 65 20 29 7b 0a 20 20 20 20 7a 4e   pName ){.    zN
1e6c1 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ame = sqlite3DbS
1e6c2 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72  trNDup(db, (char
1e6c3 2a 29 70 4e 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d  *)pName->z, pNam
1e6c4 65 2d 3e 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74  e->n);.    sqlit
1e6c5 65 33 44 65 71 75 6f 74 65 28 7a 4e 61 6d 65 29  e3Dequote(zName)
1e6c6 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a  ;.  }else{.    z
1e6c7 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  Name = 0;.  }.  
1e6c8 72 65 74 75 72 6e 20 7a 4e 61 6d 65 3b 0a 7d 0a  return zName;.}.
1e6c9 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20  ./*.** Open the 
1e6ca 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
1e6cb 62 6c 65 20 73 74 6f 72 65 64 20 69 6e 20 64 61  ble stored in da
1e6cc 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 69 44  tabase number iD
1e6cd 62 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67  b for.** writing
1e6ce 2e 20 54 68 65 20 74 61 62 6c 65 20 69 73 20 6f  . The table is o
1e6cf 70 65 6e 65 64 20 75 73 69 6e 67 20 63 75 72 73  pened using curs
1e6d0 6f 72 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  or 0..*/.SQLITE_
1e6d1 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1e6d2 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61  ite3OpenMasterTa
1e6d3 62 6c 65 28 50 61 72 73 65 20 2a 70 2c 20 69 6e  ble(Parse *p, in
1e6d4 74 20 69 44 62 29 7b 0a 20 20 56 64 62 65 20 2a  t iDb){.  Vdbe *
1e6d5 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
1e6d6 62 65 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  be(p);.  sqlite3
1e6d7 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 44 62  TableLock(p, iDb
1e6d8 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 31  , MASTER_ROOT, 1
1e6d9 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  , SCHEMA_TABLE(i
1e6da 44 62 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  Db));.  sqlite3V
1e6db 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1e6dc 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30  SetNumColumns, 0
1e6dd 2c 20 35 29 3b 2f 2a 20 73 71 6c 69 74 65 5f 6d  , 5);/* sqlite_m
1e6de 61 73 74 65 72 20 68 61 73 20 35 20 63 6f 6c 75  aster has 5 colu
1e6df 6d 6e 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  mns */.  sqlite3
1e6e0 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
1e6e1 5f 4f 70 65 6e 57 72 69 74 65 2c 20 30 2c 20 4d  _OpenWrite, 0, M
1e6e2 41 53 54 45 52 5f 52 4f 4f 54 2c 20 69 44 62 29  ASTER_ROOT, iDb)
1e6e3 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74  ;.}../*.** The t
1e6e4 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 63 6f 6e 74  oken *pName cont
1e6e5 61 69 6e 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  ains the name of
1e6e6 20 61 20 64 61 74 61 62 61 73 65 20 28 65 69 74   a database (eit
1e6e7 68 65 72 20 22 6d 61 69 6e 22 20 6f 72 0a 2a 2a  her "main" or.**
1e6e8 20 22 74 65 6d 70 22 20 6f 72 20 74 68 65 20 6e   "temp" or the n
1e6e9 61 6d 65 20 6f 66 20 61 6e 20 61 74 74 61 63 68  ame of an attach
1e6ea 65 64 20 64 62 29 2e 20 54 68 69 73 20 72 6f 75  ed db). This rou
1e6eb 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65  tine returns the
1e6ec 0a 2a 2a 20 69 6e 64 65 78 20 6f 66 20 74 68 65  .** index of the
1e6ed 20 6e 61 6d 65 64 20 64 61 74 61 62 61 73 65 20   named database 
1e6ee 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 2c 20 6f 72  in db->aDb[], or
1e6ef 20 2d 31 20 69 66 20 74 68 65 20 6e 61 6d 65 64   -1 if the named
1e6f0 20 64 62 20 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74   db .** does not
1e6f1 20 65 78 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54   exist..*/.SQLIT
1e6f2 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1e6f3 6c 69 74 65 33 46 69 6e 64 44 62 28 73 71 6c 69  lite3FindDb(sqli
1e6f4 74 65 33 20 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a  te3 *db, Token *
1e6f5 70 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 69 20  pName){.  int i 
1e6f6 3d 20 2d 31 3b 20 20 20 20 2f 2a 20 44 61 74 61  = -1;    /* Data
1e6f7 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20  base number */. 
1e6f8 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20   int n;         
1e6f9 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61  /* Number of cha
1e6fa 72 61 63 74 65 72 73 20 69 6e 20 74 68 65 20 6e  racters in the n
1e6fb 61 6d 65 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62  ame */.  Db *pDb
1e6fc 3b 20 20 20 20 20 20 20 2f 2a 20 41 20 64 61 74  ;       /* A dat
1e6fd 61 62 61 73 65 20 77 68 6f 73 65 20 6e 61 6d 65  abase whose name
1e6fe 20 73 70 61 63 65 20 69 73 20 62 65 69 6e 67 20   space is being 
1e6ff 73 65 61 72 63 68 65 64 20 2a 2f 0a 20 20 63 68  searched */.  ch
1e700 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 2f 2a 20  ar *zName;   /* 
1e701 4e 61 6d 65 20 77 65 20 61 72 65 20 73 65 61 72  Name we are sear
1e702 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0a 0a 20 20  ching for */..  
1e703 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e  zName = sqlite3N
1e704 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
1e705 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a   pName);.  if( z
1e706 4e 61 6d 65 20 29 7b 0a 20 20 20 20 6e 20 3d 20  Name ){.    n = 
1e707 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 3b 0a 20  strlen(zName);. 
1e708 20 20 20 66 6f 72 28 69 3d 28 64 62 2d 3e 6e 44     for(i=(db->nD
1e709 62 2d 31 29 2c 20 70 44 62 3d 26 64 62 2d 3e 61  b-1), pDb=&db->a
1e70a 44 62 5b 69 5d 3b 20 69 3e 3d 30 3b 20 69 2d 2d  Db[i]; i>=0; i--
1e70b 2c 20 70 44 62 2d 2d 29 7b 0a 20 20 20 20 20 20  , pDb--){.      
1e70c 69 66 28 20 28 21 4f 4d 49 54 5f 54 45 4d 50 44  if( (!OMIT_TEMPD
1e70d 42 20 7c 7c 20 69 21 3d 31 20 29 20 26 26 20 6e  B || i!=1 ) && n
1e70e 3d 3d 73 74 72 6c 65 6e 28 70 44 62 2d 3e 7a 4e  ==strlen(pDb->zN
1e70f 61 6d 65 29 20 26 26 20 0a 20 20 20 20 20 20 20  ame) && .       
1e710 20 20 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72     0==sqlite3Str
1e711 49 43 6d 70 28 70 44 62 2d 3e 7a 4e 61 6d 65 2c  ICmp(pDb->zName,
1e712 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20   zName) ){.     
1e713 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1e714 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
1e715 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e  te3DbFree(db, zN
1e716 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ame);.  }.  retu
1e717 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20  rn i;.}../* The 
1e718 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 6f 72  table or view or
1e719 20 74 72 69 67 67 65 72 20 6e 61 6d 65 20 69 73   trigger name is
1e71a 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20   passed to this 
1e71b 72 6f 75 74 69 6e 65 20 76 69 61 20 74 6f 6b 65  routine via toke
1e71c 6e 73 0a 2a 2a 20 70 4e 61 6d 65 31 20 61 6e 64  ns.** pName1 and
1e71d 20 70 4e 61 6d 65 32 2e 20 49 66 20 74 68 65 20   pName2. If the 
1e71e 74 61 62 6c 65 20 6e 61 6d 65 20 77 61 73 20 66  table name was f
1e71f 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 2c 20  ully qualified, 
1e720 66 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a  for example:.**.
1e721 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  ** CREATE TABLE 
1e722 78 78 78 2e 79 79 79 20 28 2e 2e 2e 29 3b 0a 2a  xxx.yyy (...);.*
1e723 2a 20 0a 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65  * .** Then pName
1e724 31 20 69 73 20 73 65 74 20 74 6f 20 22 78 78 78  1 is set to "xxx
1e725 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 22 79 79  " and pName2 "yy
1e726 79 22 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 72  y". On the other
1e727 20 68 61 6e 64 20 69 66 0a 2a 2a 20 74 68 65 20   hand if.** the 
1e728 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 6e 6f  table name is no
1e729 74 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65  t fully qualifie
1e72a 64 2c 20 69 2e 65 2e 3a 0a 2a 2a 0a 2a 2a 20 43  d, i.e.:.**.** C
1e72b 52 45 41 54 45 20 54 41 42 4c 45 20 79 79 79 28  REATE TABLE yyy(
1e72c 2e 2e 2e 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e  ...);.**.** Then
1e72d 20 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20 74   pName1 is set t
1e72e 6f 20 22 79 79 79 22 20 61 6e 64 20 70 4e 61 6d  o "yyy" and pNam
1e72f 65 32 20 69 73 20 22 22 2e 0a 2a 2a 0a 2a 2a 20  e2 is ""..**.** 
1e730 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74  This routine set
1e731 73 20 74 68 65 20 2a 70 70 55 6e 71 75 61 6c 20  s the *ppUnqual 
1e732 70 6f 69 6e 74 65 72 20 74 6f 20 70 6f 69 6e 74  pointer to point
1e733 20 61 74 20 74 68 65 20 74 6f 6b 65 6e 20 28 70   at the token (p
1e734 4e 61 6d 65 31 20 6f 72 0a 2a 2a 20 70 4e 61 6d  Name1 or.** pNam
1e735 65 32 29 20 74 68 61 74 20 73 74 6f 72 65 73 20  e2) that stores 
1e736 74 68 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20  the unqualified 
1e737 74 61 62 6c 65 20 6e 61 6d 65 2e 20 20 54 68 65  table name.  The
1e738 20 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a   index of the.**
1e739 20 64 61 74 61 62 61 73 65 20 22 78 78 78 22 20   database "xxx" 
1e73a 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  is returned..*/.
1e73b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1e73c 6e 74 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72  nt sqlite3TwoPar
1e73d 74 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a  tName(.  Parse *
1e73e 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20  pParse,      /* 
1e73f 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65  Parsing and code
1e740 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74   generating cont
1e741 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ext */.  Token *
1e742 70 4e 61 6d 65 31 2c 20 20 20 20 20 20 2f 2a 20  pName1,      /* 
1e743 54 68 65 20 22 78 78 78 22 20 69 6e 20 74 68 65  The "xxx" in the
1e744 20 6e 61 6d 65 20 22 78 78 78 2e 79 79 79 22 20   name "xxx.yyy" 
1e745 6f 72 20 22 78 78 78 22 20 2a 2f 0a 20 20 54 6f  or "xxx" */.  To
1e746 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20  ken *pName2,    
1e747 20 20 2f 2a 20 54 68 65 20 22 79 79 79 22 20 69    /* The "yyy" i
1e748 6e 20 74 68 65 20 6e 61 6d 65 20 22 78 78 78 2e  n the name "xxx.
1e749 79 79 79 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  yyy" */.  Token 
1e74a 2a 2a 70 55 6e 71 75 61 6c 20 20 20 20 20 2f 2a  **pUnqual     /*
1e74b 20 57 72 69 74 65 20 74 68 65 20 75 6e 71 75 61   Write the unqua
1e74c 6c 69 66 69 65 64 20 6f 62 6a 65 63 74 20 6e 61  lified object na
1e74d 6d 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  me here */.){.  
1e74e 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
1e74f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
1e750 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20  atabase holding 
1e751 74 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20  the object */.  
1e752 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
1e753 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28  arse->db;..  if(
1e754 20 70 4e 61 6d 65 32 20 26 26 20 70 4e 61 6d 65   pName2 && pName
1e755 32 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 61 73  2->n>0 ){.    as
1e756 73 65 72 74 28 20 21 64 62 2d 3e 69 6e 69 74 2e  sert( !db->init.
1e757 62 75 73 79 20 29 3b 0a 20 20 20 20 2a 70 55 6e  busy );.    *pUn
1e758 71 75 61 6c 20 3d 20 70 4e 61 6d 65 32 3b 0a 20  qual = pName2;. 
1e759 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33     iDb = sqlite3
1e75a 46 69 6e 64 44 62 28 64 62 2c 20 70 4e 61 6d 65  FindDb(db, pName
1e75b 31 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3c  1);.    if( iDb<
1e75c 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
1e75d 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1e75e 65 2c 20 22 75 6e 6b 6e 6f 77 6e 20 64 61 74 61  e, "unknown data
1e75f 62 61 73 65 20 25 54 22 2c 20 70 4e 61 6d 65 31  base %T", pName1
1e760 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  );.      pParse-
1e761 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 72  >nErr++;.      r
1e762 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a  eturn -1;.    }.
1e763 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
1e764 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e 69 44  ert( db->init.iD
1e765 62 3d 3d 30 20 7c 7c 20 64 62 2d 3e 69 6e 69 74  b==0 || db->init
1e766 2e 62 75 73 79 20 29 3b 0a 20 20 20 20 69 44 62  .busy );.    iDb
1e767 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3b   = db->init.iDb;
1e768 0a 20 20 20 20 2a 70 55 6e 71 75 61 6c 20 3d 20  .    *pUnqual = 
1e769 70 4e 61 6d 65 31 3b 0a 20 20 7d 0a 20 20 72 65  pName1;.  }.  re
1e76a 74 75 72 6e 20 69 44 62 3b 0a 7d 0a 0a 2f 2a 0a  turn iDb;.}../*.
1e76b 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1e76c 69 73 20 75 73 65 64 20 74 6f 20 63 68 65 63 6b  is used to check
1e76d 20 69 66 20 74 68 65 20 55 54 46 2d 38 20 73 74   if the UTF-8 st
1e76e 72 69 6e 67 20 7a 4e 61 6d 65 20 69 73 20 61 20  ring zName is a 
1e76f 6c 65 67 61 6c 0a 2a 2a 20 75 6e 71 75 61 6c 69  legal.** unquali
1e770 66 69 65 64 20 6e 61 6d 65 20 66 6f 72 20 61 20  fied name for a 
1e771 6e 65 77 20 73 63 68 65 6d 61 20 6f 62 6a 65 63  new schema objec
1e772 74 20 28 74 61 62 6c 65 2c 20 69 6e 64 65 78 2c  t (table, index,
1e773 20 76 69 65 77 20 6f 72 0a 2a 2a 20 74 72 69 67   view or.** trig
1e774 67 65 72 29 2e 20 41 6c 6c 20 6e 61 6d 65 73 20  ger). All names 
1e775 61 72 65 20 6c 65 67 61 6c 20 65 78 63 65 70 74  are legal except
1e776 20 74 68 6f 73 65 20 74 68 61 74 20 62 65 67 69   those that begi
1e777 6e 20 77 69 74 68 20 74 68 65 20 73 74 72 69 6e  n with the strin
1e778 67 0a 2a 2a 20 22 73 71 6c 69 74 65 5f 22 20 28  g.** "sqlite_" (
1e779 69 6e 20 75 70 70 65 72 2c 20 6c 6f 77 65 72 20  in upper, lower 
1e77a 6f 72 20 6d 69 78 65 64 20 63 61 73 65 29 2e 20  or mixed case). 
1e77b 54 68 69 73 20 70 6f 72 74 69 6f 6e 20 6f 66 20  This portion of 
1e77c 74 68 65 20 6e 61 6d 65 73 70 61 63 65 0a 2a 2a  the namespace.**
1e77d 20 69 73 20 72 65 73 65 72 76 65 64 20 66 6f 72   is reserved for
1e77e 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 2e 0a 2a   internal use..*
1e77f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1e780 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68 65 63   int sqlite3Chec
1e781 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 50 61 72 73  kObjectName(Pars
1e782 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74  e *pParse, const
1e783 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20   char *zName){. 
1e784 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e 64 62   if( !pParse->db
1e785 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20 70  ->init.busy && p
1e786 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30  Parse->nested==0
1e787 20 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 28   .          && (
1e788 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67  pParse->db->flag
1e789 73 20 26 20 53 51 4c 49 54 45 5f 57 72 69 74 65  s & SQLITE_Write
1e78a 53 63 68 65 6d 61 29 3d 3d 30 0a 20 20 20 20 20  Schema)==0.     
1e78b 20 20 20 20 20 26 26 20 30 3d 3d 73 71 6c 69 74       && 0==sqlit
1e78c 65 33 53 74 72 4e 49 43 6d 70 28 7a 4e 61 6d 65  e3StrNICmp(zName
1e78d 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29 20  , "sqlite_", 7) 
1e78e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
1e78f 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1e790 6f 62 6a 65 63 74 20 6e 61 6d 65 20 72 65 73 65  object name rese
1e791 72 76 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61  rved for interna
1e792 6c 20 75 73 65 3a 20 25 73 22 2c 20 7a 4e 61 6d  l use: %s", zNam
1e793 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  e);.    return S
1e794 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
1e795 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1e796 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65  _OK;.}../*.** Be
1e797 67 69 6e 20 63 6f 6e 73 74 72 75 63 74 69 6e 67  gin constructing
1e798 20 61 20 6e 65 77 20 74 61 62 6c 65 20 72 65 70   a new table rep
1e799 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 6e 20 6d  resentation in m
1e79a 65 6d 6f 72 79 2e 20 20 54 68 69 73 20 69 73 0a  emory.  This is.
1e79b 2a 2a 20 74 68 65 20 66 69 72 73 74 20 6f 66 20  ** the first of 
1e79c 73 65 76 65 72 61 6c 20 61 63 74 69 6f 6e 20 72  several action r
1e79d 6f 75 74 69 6e 65 73 20 74 68 61 74 20 67 65 74  outines that get
1e79e 20 63 61 6c 6c 65 64 20 69 6e 20 72 65 73 70 6f   called in respo
1e79f 6e 73 65 0a 2a 2a 20 74 6f 20 61 20 43 52 45 41  nse.** to a CREA
1e7a0 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  TE TABLE stateme
1e7a1 6e 74 2e 20 20 49 6e 20 70 61 72 74 69 63 75 6c  nt.  In particul
1e7a2 61 72 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ar, this routine
1e7a3 20 69 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 61 66   is called.** af
1e7a4 74 65 72 20 73 65 65 69 6e 67 20 74 6f 6b 65 6e  ter seeing token
1e7a5 73 20 22 43 52 45 41 54 45 22 20 61 6e 64 20 22  s "CREATE" and "
1e7a6 54 41 42 4c 45 22 20 61 6e 64 20 74 68 65 20 74  TABLE" and the t
1e7a7 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 69  able name. The i
1e7a8 73 54 65 6d 70 0a 2a 2a 20 66 6c 61 67 20 69 73  sTemp.** flag is
1e7a9 20 74 72 75 65 20 69 66 20 74 68 65 20 74 61 62   true if the tab
1e7aa 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 73 74 6f  le should be sto
1e7ab 72 65 64 20 69 6e 20 74 68 65 20 61 75 78 69 6c  red in the auxil
1e7ac 69 61 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a  iary database.**
1e7ad 20 66 69 6c 65 20 69 6e 73 74 65 61 64 20 6f 66   file instead of
1e7ae 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74   in the main dat
1e7af 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 69  abase file.  Thi
1e7b0 73 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 74 68  s is normally th
1e7b1 65 20 63 61 73 65 0a 2a 2a 20 77 68 65 6e 20 74  e case.** when t
1e7b2 68 65 20 22 54 45 4d 50 22 20 6f 72 20 22 54 45  he "TEMP" or "TE
1e7b3 4d 50 4f 52 41 52 59 22 20 6b 65 79 77 6f 72 64  MPORARY" keyword
1e7b4 20 6f 63 63 75 72 73 20 69 6e 20 62 65 74 77 65   occurs in betwe
1e7b5 65 6e 0a 2a 2a 20 43 52 45 41 54 45 20 61 6e 64  en.** CREATE and
1e7b6 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 54 68   TABLE..**.** Th
1e7b7 65 20 6e 65 77 20 74 61 62 6c 65 20 72 65 63 6f  e new table reco
1e7b8 72 64 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65  rd is initialize
1e7b9 64 20 61 6e 64 20 70 75 74 20 69 6e 20 70 50 61  d and put in pPa
1e7ba 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2e 0a  rse->pNewTable..
1e7bb 2a 2a 20 41 73 20 6d 6f 72 65 20 6f 66 20 74 68  ** As more of th
1e7bc 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  e CREATE TABLE s
1e7bd 74 61 74 65 6d 65 6e 74 20 69 73 20 70 61 72 73  tatement is pars
1e7be 65 64 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 61  ed, additional a
1e7bf 63 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65  ction.** routine
1e7c0 73 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64  s will be called
1e7c1 20 74 6f 20 61 64 64 20 6d 6f 72 65 20 69 6e 66   to add more inf
1e7c2 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 74 68 69 73  ormation to this
1e7c3 20 72 65 63 6f 72 64 2e 0a 2a 2a 20 41 74 20 74   record..** At t
1e7c4 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 43 52  he end of the CR
1e7c5 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
1e7c6 6d 65 6e 74 2c 20 74 68 65 20 73 71 6c 69 74 65  ment, the sqlite
1e7c7 33 45 6e 64 54 61 62 6c 65 28 29 20 72 6f 75 74  3EndTable() rout
1e7c8 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64  ine.** is called
1e7c9 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65   to complete the
1e7ca 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66   construction of
1e7cb 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 72   the new table r
1e7cc 65 63 6f 72 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ecord..*/.SQLITE
1e7cd 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1e7ce 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28  lite3StartTable(
1e7cf 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
1e7d0 2c 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f  ,   /* Parser co
1e7d1 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e  ntext */.  Token
1e7d2 20 2a 70 4e 61 6d 65 31 2c 20 20 20 2f 2a 20 46   *pName1,   /* F
1e7d3 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 68 65  irst part of the
1e7d4 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   name of the tab
1e7d5 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20  le or view */.  
1e7d6 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20  Token *pName2,  
1e7d7 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 74 20   /* Second part 
1e7d8 6f 66 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  of the name of t
1e7d9 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77  he table or view
1e7da 20 2a 2f 0a 20 20 69 6e 74 20 69 73 54 65 6d 70   */.  int isTemp
1e7db 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  ,      /* True i
1e7dc 66 20 74 68 69 73 20 69 73 20 61 20 54 45 4d 50  f this is a TEMP
1e7dd 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
1e7de 69 73 56 69 65 77 2c 20 20 20 20 20 20 2f 2a 20  isView,      /* 
1e7df 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20  True if this is 
1e7e0 61 20 56 49 45 57 20 2a 2f 0a 20 20 69 6e 74 20  a VIEW */.  int 
1e7e1 69 73 56 69 72 74 75 61 6c 2c 20 20 20 2f 2a 20  isVirtual,   /* 
1e7e2 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20  True if this is 
1e7e3 61 20 56 49 52 54 55 41 4c 20 74 61 62 6c 65 20  a VIRTUAL table 
1e7e4 2a 2f 0a 20 20 69 6e 74 20 6e 6f 45 72 72 20 20  */.  int noErr  
1e7e5 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68        /* Do noth
1e7e6 69 6e 67 20 69 66 20 74 61 62 6c 65 20 61 6c 72  ing if table alr
1e7e7 65 61 64 79 20 65 78 69 73 74 73 20 2a 2f 0a 29  eady exists */.)
1e7e8 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 6c  {.  Table *pTabl
1e7e9 65 3b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65  e;.  char *zName
1e7ea 20 3d 20 30 3b 20 2f 2a 20 54 68 65 20 6e 61 6d   = 0; /* The nam
1e7eb 65 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62  e of the new tab
1e7ec 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  le */.  sqlite3 
1e7ed 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
1e7ee 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69  ;.  Vdbe *v;.  i
1e7ef 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
1e7f0 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75 6d 62  /* Database numb
1e7f1 65 72 20 74 6f 20 63 72 65 61 74 65 20 74 68 65  er to create the
1e7f2 20 74 61 62 6c 65 20 69 6e 20 2a 2f 0a 20 20 54   table in */.  T
1e7f3 6f 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 20 20 20  oken *pName;    
1e7f4 2f 2a 20 55 6e 71 75 61 6c 69 66 69 65 64 20 6e  /* Unqualified n
1e7f5 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
1e7f6 20 74 6f 20 63 72 65 61 74 65 20 2a 2f 0a 0a 20   to create */.. 
1e7f7 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 72   /* The table or
1e7f8 20 76 69 65 77 20 6e 61 6d 65 20 74 6f 20 63 72   view name to cr
1e7f9 65 61 74 65 20 69 73 20 70 61 73 73 65 64 20 74  eate is passed t
1e7fa 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 76  o this routine v
1e7fb 69 61 20 74 6f 6b 65 6e 73 0a 20 20 2a 2a 20 70  ia tokens.  ** p
1e7fc 4e 61 6d 65 31 20 61 6e 64 20 70 4e 61 6d 65 32  Name1 and pName2
1e7fd 2e 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 6e  . If the table n
1e7fe 61 6d 65 20 77 61 73 20 66 75 6c 6c 79 20 71 75  ame was fully qu
1e7ff 61 6c 69 66 69 65 64 2c 20 66 6f 72 20 65 78 61  alified, for exa
1e800 6d 70 6c 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  mple:.  **.  ** 
1e801 43 52 45 41 54 45 20 54 41 42 4c 45 20 78 78 78  CREATE TABLE xxx
1e802 2e 79 79 79 20 28 2e 2e 2e 29 3b 0a 20 20 2a 2a  .yyy (...);.  **
1e803 20 0a 20 20 2a 2a 20 54 68 65 6e 20 70 4e 61 6d   .  ** Then pNam
1e804 65 31 20 69 73 20 73 65 74 20 74 6f 20 22 78 78  e1 is set to "xx
1e805 78 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 22 79  x" and pName2 "y
1e806 79 79 22 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65  yy". On the othe
1e807 72 20 68 61 6e 64 20 69 66 0a 20 20 2a 2a 20 74  r hand if.  ** t
1e808 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73  he table name is
1e809 20 6e 6f 74 20 66 75 6c 6c 79 20 71 75 61 6c 69   not fully quali
1e80a 66 69 65 64 2c 20 69 2e 65 2e 3a 0a 20 20 2a 2a  fied, i.e.:.  **
1e80b 0a 20 20 2a 2a 20 43 52 45 41 54 45 20 54 41 42  .  ** CREATE TAB
1e80c 4c 45 20 79 79 79 28 2e 2e 2e 29 3b 0a 20 20 2a  LE yyy(...);.  *
1e80d 2a 0a 20 20 2a 2a 20 54 68 65 6e 20 70 4e 61 6d  *.  ** Then pNam
1e80e 65 31 20 69 73 20 73 65 74 20 74 6f 20 22 79 79  e1 is set to "yy
1e80f 79 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 69 73  y" and pName2 is
1e810 20 22 22 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54   ""..  **.  ** T
1e811 68 65 20 63 61 6c 6c 20 62 65 6c 6f 77 20 73 65  he call below se
1e812 74 73 20 74 68 65 20 70 4e 61 6d 65 20 70 6f 69  ts the pName poi
1e813 6e 74 65 72 20 74 6f 20 70 6f 69 6e 74 20 61 74  nter to point at
1e814 20 74 68 65 20 74 6f 6b 65 6e 20 28 70 4e 61 6d   the token (pNam
1e815 65 31 20 6f 72 0a 20 20 2a 2a 20 70 4e 61 6d 65  e1 or.  ** pName
1e816 32 29 20 74 68 61 74 20 73 74 6f 72 65 73 20 74  2) that stores t
1e817 68 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74  he unqualified t
1e818 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 76  able name. The v
1e819 61 72 69 61 62 6c 65 20 69 44 62 20 69 73 0a 20  ariable iDb is. 
1e81a 20 2a 2a 20 73 65 74 20 74 6f 20 74 68 65 20 69   ** set to the i
1e81b 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
1e81c 62 61 73 65 20 74 68 61 74 20 74 68 65 20 74 61  base that the ta
1e81d 62 6c 65 20 6f 72 20 76 69 65 77 20 69 73 20 74  ble or view is t
1e81e 6f 20 62 65 0a 20 20 2a 2a 20 63 72 65 61 74 65  o be.  ** create
1e81f 64 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 69 44 62  d in..  */.  iDb
1e820 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72   = sqlite3TwoPar
1e821 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e  tName(pParse, pN
1e822 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70  ame1, pName2, &p
1e823 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 69 44 62  Name);.  if( iDb
1e824 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  <0 ) return;.  i
1e825 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20  f( !OMIT_TEMPDB 
1e826 26 26 20 69 73 54 65 6d 70 20 26 26 20 69 44 62  && isTemp && iDb
1e827 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20  >1 ){.    /* If 
1e828 63 72 65 61 74 69 6e 67 20 61 20 74 65 6d 70 20  creating a temp 
1e829 74 61 62 6c 65 2c 20 74 68 65 20 6e 61 6d 65 20  table, the name 
1e82a 6d 61 79 20 6e 6f 74 20 62 65 20 71 75 61 6c 69  may not be quali
1e82b 66 69 65 64 20 2a 2f 0a 20 20 20 20 73 71 6c 69  fied */.    sqli
1e82c 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1e82d 73 65 2c 20 22 74 65 6d 70 6f 72 61 72 79 20 74  se, "temporary t
1e82e 61 62 6c 65 20 6e 61 6d 65 20 6d 75 73 74 20 62  able name must b
1e82f 65 20 75 6e 71 75 61 6c 69 66 69 65 64 22 29 3b  e unqualified");
1e830 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
1e831 0a 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d  .  if( !OMIT_TEM
1e832 50 44 42 20 26 26 20 69 73 54 65 6d 70 20 29 20  PDB && isTemp ) 
1e833 69 44 62 20 3d 20 31 3b 0a 0a 20 20 70 50 61 72  iDb = 1;..  pPar
1e834 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 20 3d  se->sNameToken =
1e835 20 2a 70 4e 61 6d 65 3b 0a 20 20 7a 4e 61 6d 65   *pName;.  zName
1e836 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
1e837 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d  omToken(db, pNam
1e838 65 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d  e);.  if( zName=
1e839 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  =0 ) return;.  i
1e83a 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71  f( SQLITE_OK!=sq
1e83b 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74  lite3CheckObject
1e83c 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e 61  Name(pParse, zNa
1e83d 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  me) ){.    goto 
1e83e 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f  begin_table_erro
1e83f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d  r;.  }.  if( db-
1e840 3e 69 6e 69 74 2e 69 44 62 3d 3d 31 20 29 20 69  >init.iDb==1 ) i
1e841 73 54 65 6d 70 20 3d 20 31 3b 0a 23 69 66 6e 64  sTemp = 1;.#ifnd
1e842 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1e843 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 61  UTHORIZATION.  a
1e844 73 73 65 72 74 28 20 28 69 73 54 65 6d 70 20 26  ssert( (isTemp &
1e845 20 31 29 3d 3d 69 73 54 65 6d 70 20 29 3b 0a 20   1)==isTemp );. 
1e846 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64 65 3b   {.    int code;
1e847 0a 20 20 20 20 63 68 61 72 20 2a 7a 44 62 20 3d  .    char *zDb =
1e848 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
1e849 61 6d 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  ame;.    if( sql
1e84a 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
1e84b 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53  arse, SQLITE_INS
1e84c 45 52 54 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c  ERT, SCHEMA_TABL
1e84d 45 28 69 73 54 65 6d 70 29 2c 20 30 2c 20 7a 44  E(isTemp), 0, zD
1e84e 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  b) ){.      goto
1e84f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72   begin_table_err
1e850 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  or;.    }.    if
1e851 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20  ( isView ){.    
1e852 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50    if( !OMIT_TEMP
1e853 44 42 20 26 26 20 69 73 54 65 6d 70 20 29 7b 0a  DB && isTemp ){.
1e854 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53          code = S
1e855 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d  QLITE_CREATE_TEM
1e856 50 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 65  P_VIEW;.      }e
1e857 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64  lse{.        cod
1e858 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54  e = SQLITE_CREAT
1e859 45 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 0a  E_VIEW;.      }.
1e85a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1e85b 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44   if( !OMIT_TEMPD
1e85c 42 20 26 26 20 69 73 54 65 6d 70 20 29 7b 0a 20  B && isTemp ){. 
1e85d 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51         code = SQ
1e85e 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50  LITE_CREATE_TEMP
1e85f 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d 65  _TABLE;.      }e
1e860 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64  lse{.        cod
1e861 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54  e = SQLITE_CREAT
1e862 45 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d  E_TABLE;.      }
1e863 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21  .    }.    if( !
1e864 69 73 56 69 72 74 75 61 6c 20 26 26 20 73 71 6c  isVirtual && sql
1e865 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
1e866 61 72 73 65 2c 20 63 6f 64 65 2c 20 7a 4e 61 6d  arse, code, zNam
1e867 65 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20  e, 0, zDb) ){.  
1e868 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74      goto begin_t
1e869 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20  able_error;.    
1e86a 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  }.  }.#endif..  
1e86b 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  /* Make sure the
1e86c 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 20   new table name 
1e86d 64 6f 65 73 20 6e 6f 74 20 63 6f 6c 6c 69 64 65  does not collide
1e86e 20 77 69 74 68 20 61 6e 20 65 78 69 73 74 69 6e   with an existin
1e86f 67 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6f 72 20  g.  ** index or 
1e870 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68  table name in th
1e871 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 2e  e same database.
1e872 20 20 49 73 73 75 65 20 61 6e 20 65 72 72 6f 72    Issue an error
1e873 20 6d 65 73 73 61 67 65 20 69 66 0a 20 20 2a 2a   message if.  **
1e874 20 69 74 20 64 6f 65 73 2e 20 54 68 65 20 65 78   it does. The ex
1e875 63 65 70 74 69 6f 6e 20 69 73 20 69 66 20 74 68  ception is if th
1e876 65 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e  e statement bein
1e877 67 20 70 61 72 73 65 64 20 77 61 73 20 70 61 73  g parsed was pas
1e878 73 65 64 0a 20 20 2a 2a 20 74 6f 20 61 6e 20 73  sed.  ** to an s
1e879 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76  qlite3_declare_v
1e87a 74 61 62 28 29 20 63 61 6c 6c 2e 20 49 6e 20 74  tab() call. In t
1e87b 68 61 74 20 63 61 73 65 20 6f 6e 6c 79 20 74 68  hat case only th
1e87c 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 0a 20  e column names. 
1e87d 20 2a 2a 20 61 6e 64 20 74 79 70 65 73 20 77 69   ** and types wi
1e87e 6c 6c 20 62 65 20 75 73 65 64 2c 20 73 6f 20 74  ll be used, so t
1e87f 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20  here is no need 
1e880 74 6f 20 74 65 73 74 20 66 6f 72 20 6e 61 6d 65  to test for name
1e881 73 70 61 63 65 0a 20 20 2a 2a 20 63 6f 6c 6c 69  space.  ** colli
1e882 73 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  sions..  */.  if
1e883 28 20 21 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54  ( !IN_DECLARE_VT
1e884 41 42 20 29 7b 0a 20 20 20 20 69 66 28 20 53 51  AB ){.    if( SQ
1e885 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33  LITE_OK!=sqlite3
1e886 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
1e887 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  e) ){.      goto
1e888 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72   begin_table_err
1e889 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54  or;.    }.    pT
1e88a 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 46 69  able = sqlite3Fi
1e88b 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d  ndTable(db, zNam
1e88c 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  e, db->aDb[iDb].
1e88d 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  zName);.    if( 
1e88e 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20  pTable ){.      
1e88f 69 66 28 20 21 6e 6f 45 72 72 20 29 7b 0a 20 20  if( !noErr ){.  
1e890 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1e891 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74  orMsg(pParse, "t
1e892 61 62 6c 65 20 25 54 20 61 6c 72 65 61 64 79 20  able %T already 
1e893 65 78 69 73 74 73 22 2c 20 70 4e 61 6d 65 29 3b  exists", pName);
1e894 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67  .      }.      g
1e895 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f  oto begin_table_
1e896 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20  error;.    }.   
1e897 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64   if( sqlite3Find
1e898 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c  Index(db, zName,
1e899 20 30 29 21 3d 30 20 26 26 20 28 69 44 62 3d 3d   0)!=0 && (iDb==
1e89a 30 20 7c 7c 20 21 64 62 2d 3e 69 6e 69 74 2e 62  0 || !db->init.b
1e89b 75 73 79 29 20 29 7b 0a 20 20 20 20 20 20 73 71  usy) ){.      sq
1e89c 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1e89d 61 72 73 65 2c 20 22 74 68 65 72 65 20 69 73 20  arse, "there is 
1e89e 61 6c 72 65 61 64 79 20 61 6e 20 69 6e 64 65 78  already an index
1e89f 20 6e 61 6d 65 64 20 25 73 22 2c 20 7a 4e 61 6d   named %s", zNam
1e8a0 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62  e);.      goto b
1e8a1 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72  egin_table_error
1e8a2 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70  ;.    }.  }..  p
1e8a3 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 44  Table = sqlite3D
1e8a4 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
1e8a5 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 29 3b 0a  sizeof(Table));.
1e8a6 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d 30 20    if( pTable==0 
1e8a7 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  ){.    db->mallo
1e8a8 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
1e8a9 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51   pParse->rc = SQ
1e8aa 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
1e8ab 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a  pParse->nErr++;.
1e8ac 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74      goto begin_t
1e8ad 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a  able_error;.  }.
1e8ae 20 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 20    pTable->zName 
1e8af 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 54 61 62 6c  = zName;.  pTabl
1e8b0 65 2d 3e 69 50 4b 65 79 20 3d 20 2d 31 3b 0a 20  e->iPKey = -1;. 
1e8b1 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61   pTable->pSchema
1e8b2 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
1e8b3 70 53 63 68 65 6d 61 3b 0a 20 20 70 54 61 62 6c  pSchema;.  pTabl
1e8b4 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70  e->nRef = 1;.  p
1e8b5 54 61 62 6c 65 2d 3e 64 62 20 3d 20 64 62 3b 0a  Table->db = db;.
1e8b6 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 70 4e    if( pParse->pN
1e8b7 65 77 54 61 62 6c 65 20 29 20 73 71 6c 69 74 65  ewTable ) sqlite
1e8b8 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 50 61  3DeleteTable(pPa
1e8b9 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3b  rse->pNewTable);
1e8ba 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  .  pParse->pNewT
1e8bb 61 62 6c 65 20 3d 20 70 54 61 62 6c 65 3b 0a 0a  able = pTable;..
1e8bc 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
1e8bd 74 68 65 20 6d 61 67 69 63 20 73 71 6c 69 74 65  the magic sqlite
1e8be 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20  _sequence table 
1e8bf 75 73 65 64 20 62 79 20 61 75 74 6f 69 6e 63 72  used by autoincr
1e8c0 65 6d 65 6e 74 2c 0a 20 20 2a 2a 20 74 68 65 6e  ement,.  ** then
1e8c1 20 72 65 63 6f 72 64 20 61 20 70 6f 69 6e 74 65   record a pointe
1e8c2 72 20 74 6f 20 74 68 69 73 20 74 61 62 6c 65 20  r to this table 
1e8c3 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  in the main data
1e8c4 62 61 73 65 20 73 74 72 75 63 74 75 72 65 0a 20  base structure. 
1e8c5 20 2a 2a 20 73 6f 20 74 68 61 74 20 49 4e 53 45   ** so that INSE
1e8c6 52 54 20 63 61 6e 20 66 69 6e 64 20 74 68 65 20  RT can find the 
1e8c7 74 61 62 6c 65 20 65 61 73 69 6c 79 2e 0a 20 20  table easily..  
1e8c8 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
1e8c9 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45  E_OMIT_AUTOINCRE
1e8ca 4d 45 4e 54 0a 20 20 69 66 28 20 21 70 50 61 72  MENT.  if( !pPar
1e8cb 73 65 2d 3e 6e 65 73 74 65 64 20 26 26 20 73 74  se->nested && st
1e8cc 72 63 6d 70 28 7a 4e 61 6d 65 2c 20 22 73 71 6c  rcmp(zName, "sql
1e8cd 69 74 65 5f 73 65 71 75 65 6e 63 65 22 29 3d 3d  ite_sequence")==
1e8ce 30 20 29 7b 0a 20 20 20 20 70 54 61 62 6c 65 2d  0 ){.    pTable-
1e8cf 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 54 61  >pSchema->pSeqTa
1e8d0 62 20 3d 20 70 54 61 62 6c 65 3b 0a 20 20 7d 0a  b = pTable;.  }.
1e8d1 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65 67  #endif..  /* Beg
1e8d2 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20 74 68  in generating th
1e8d3 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c  e code that will
1e8d4 20 69 6e 73 65 72 74 20 74 68 65 20 74 61 62 6c   insert the tabl
1e8d5 65 20 72 65 63 6f 72 64 20 69 6e 74 6f 0a 20 20  e record into.  
1e8d6 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41  ** the SQLITE_MA
1e8d7 53 54 45 52 20 74 61 62 6c 65 2e 20 20 4e 6f 74  STER table.  Not
1e8d8 65 20 69 6e 20 70 61 72 74 69 63 75 6c 61 72 20  e in particular 
1e8d9 74 68 61 74 20 77 65 20 6d 75 73 74 20 67 6f 20  that we must go 
1e8da 61 68 65 61 64 0a 20 20 2a 2a 20 61 6e 64 20 61  ahead.  ** and a
1e8db 6c 6c 6f 63 61 74 65 20 74 68 65 20 72 65 63 6f  llocate the reco
1e8dc 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  rd number for th
1e8dd 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20 6e 6f  e table entry no
1e8de 77 2e 20 20 42 65 66 6f 72 65 20 61 6e 79 0a 20  w.  Before any. 
1e8df 20 2a 2a 20 50 52 49 4d 41 52 59 20 4b 45 59 20   ** PRIMARY KEY 
1e8e0 6f 72 20 55 4e 49 51 55 45 20 6b 65 79 77 6f 72  or UNIQUE keywor
1e8e1 64 73 20 61 72 65 20 70 61 72 73 65 64 2e 20 20  ds are parsed.  
1e8e2 54 68 6f 73 65 20 6b 65 79 77 6f 72 64 73 20 77  Those keywords w
1e8e3 69 6c 6c 20 63 61 75 73 65 0a 20 20 2a 2a 20 69  ill cause.  ** i
1e8e4 6e 64 69 63 65 73 20 74 6f 20 62 65 20 63 72 65  ndices to be cre
1e8e5 61 74 65 64 20 61 6e 64 20 74 68 65 20 74 61 62  ated and the tab
1e8e6 6c 65 20 72 65 63 6f 72 64 20 6d 75 73 74 20 63  le record must c
1e8e7 6f 6d 65 20 62 65 66 6f 72 65 20 74 68 65 20 0a  ome before the .
1e8e8 20 20 2a 2a 20 69 6e 64 69 63 65 73 2e 20 20 48    ** indices.  H
1e8e9 65 6e 63 65 2c 20 74 68 65 20 72 65 63 6f 72 64  ence, the record
1e8ea 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
1e8eb 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20 61 6c  table must be al
1e8ec 6c 6f 63 61 74 65 64 0a 20 20 2a 2a 20 6e 6f 77  located.  ** now
1e8ed 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62  ..  */.  if( !db
1e8ee 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20 28  ->init.busy && (
1e8ef 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
1e8f0 62 65 28 70 50 61 72 73 65 29 29 21 3d 30 20 29  be(pParse))!=0 )
1e8f1 7b 0a 20 20 20 20 69 6e 74 20 6a 31 3b 0a 20 20  {.    int j1;.  
1e8f2 20 20 69 6e 74 20 66 69 6c 65 46 6f 72 6d 61 74    int fileFormat
1e8f3 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 31 2c 20  ;.    int reg1, 
1e8f4 72 65 67 32 2c 20 72 65 67 33 3b 0a 20 20 20 20  reg2, reg3;.    
1e8f5 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74  sqlite3BeginWrit
1e8f6 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73  eOperation(pPars
1e8f7 65 2c 20 30 2c 20 69 44 62 29 3b 0a 0a 23 69 66  e, 0, iDb);..#if
1e8f8 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1e8f9 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
1e8fa 20 20 69 66 28 20 69 73 56 69 72 74 75 61 6c 20    if( isVirtual 
1e8fb 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1e8fc 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50  VdbeAddOp0(v, OP
1e8fd 5f 56 42 65 67 69 6e 29 3b 0a 20 20 20 20 7d 0a  _VBegin);.    }.
1e8fe 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 49  #endif..    /* I
1e8ff 66 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61  f the file forma
1e900 74 20 61 6e 64 20 65 6e 63 6f 64 69 6e 67 20 69  t and encoding i
1e901 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  n the database h
1e902 61 76 65 20 6e 6f 74 20 62 65 65 6e 20 73 65 74  ave not been set
1e903 2c 20 0a 20 20 20 20 2a 2a 20 73 65 74 20 74 68  , .    ** set th
1e904 65 6d 20 6e 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20  em now..    */. 
1e905 20 20 20 72 65 67 31 20 3d 20 70 50 61 72 73 65     reg1 = pParse
1e906 2d 3e 72 65 67 52 6f 77 69 64 20 3d 20 2b 2b 70  ->regRowid = ++p
1e907 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
1e908 20 72 65 67 32 20 3d 20 70 50 61 72 73 65 2d 3e   reg2 = pParse->
1e909 72 65 67 52 6f 6f 74 20 3d 20 2b 2b 70 50 61 72  regRoot = ++pPar
1e90a 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 72 65  se->nMem;.    re
1e90b 67 33 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  g3 = ++pParse->n
1e90c 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Mem;.    sqlite3
1e90d 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
1e90e 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 69 44 62  _ReadCookie, iDb
1e90f 2c 20 72 65 67 33 2c 20 31 29 3b 20 20 20 2f 2a  , reg3, 1);   /*
1e910 20 66 69 6c 65 5f 66 6f 72 6d 61 74 20 2a 2f 0a   file_format */.
1e911 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55      sqlite3VdbeU
1e912 73 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29  sesBtree(v, iDb)
1e913 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74  ;.    j1 = sqlit
1e914 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
1e915 4f 50 5f 49 66 2c 20 72 65 67 33 29 3b 0a 20 20  OP_If, reg3);.  
1e916 20 20 66 69 6c 65 46 6f 72 6d 61 74 20 3d 20 28    fileFormat = (
1e917 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
1e918 54 45 5f 4c 65 67 61 63 79 46 69 6c 65 46 6d 74  TE_LegacyFileFmt
1e919 29 21 3d 30 20 3f 0a 20 20 20 20 20 20 20 20 20  )!=0 ?.         
1e91a 20 20 20 20 20 20 20 20 20 31 20 3a 20 53 51 4c           1 : SQL
1e91b 49 54 45 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52  ITE_MAX_FILE_FOR
1e91c 4d 41 54 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  MAT;.    sqlite3
1e91d 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1e91e 5f 49 6e 74 65 67 65 72 2c 20 66 69 6c 65 46 6f  _Integer, fileFo
1e91f 72 6d 61 74 2c 20 72 65 67 33 29 3b 0a 20 20 20  rmat, reg3);.   
1e920 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1e921 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b  p3(v, OP_SetCook
1e922 69 65 2c 20 69 44 62 2c 20 31 2c 20 72 65 67 33  ie, iDb, 1, reg3
1e923 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1e924 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
1e925 6e 74 65 67 65 72 2c 20 45 4e 43 28 64 62 29 2c  nteger, ENC(db),
1e926 20 72 65 67 33 29 3b 0a 20 20 20 20 73 71 6c 69   reg3);.    sqli
1e927 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
1e928 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69   OP_SetCookie, i
1e929 44 62 2c 20 34 2c 20 72 65 67 33 29 3b 0a 20 20  Db, 4, reg3);.  
1e92a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
1e92b 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 0a 20  pHere(v, j1);.. 
1e92c 20 20 20 2f 2a 20 54 68 69 73 20 6a 75 73 74 20     /* This just 
1e92d 63 72 65 61 74 65 73 20 61 20 70 6c 61 63 65 2d  creates a place-
1e92e 68 6f 6c 64 65 72 20 72 65 63 6f 72 64 20 69 6e  holder record in
1e92f 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74   the sqlite_mast
1e930 65 72 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a  er table..    **
1e931 20 54 68 65 20 72 65 63 6f 72 64 20 63 72 65 61   The record crea
1e932 74 65 64 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e  ted does not con
1e933 74 61 69 6e 20 61 6e 79 74 68 69 6e 67 20 79 65  tain anything ye
1e934 74 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 20 72  t.  It will be r
1e935 65 70 6c 61 63 65 64 0a 20 20 20 20 2a 2a 20 62  eplaced.    ** b
1e936 79 20 74 68 65 20 72 65 61 6c 20 65 6e 74 72 79  y the real entry
1e937 20 69 6e 20 63 6f 64 65 20 67 65 6e 65 72 61 74   in code generat
1e938 65 64 20 61 74 20 73 71 6c 69 74 65 33 45 6e 64  ed at sqlite3End
1e939 54 61 62 6c 65 28 29 2e 0a 20 20 20 20 2a 2a 0a  Table()..    **.
1e93a 20 20 20 20 2a 2a 20 54 68 65 20 72 6f 77 69 64      ** The rowid
1e93b 20 66 6f 72 20 74 68 65 20 6e 65 77 20 65 6e 74   for the new ent
1e93c 72 79 20 69 73 20 6c 65 66 74 20 6f 6e 20 74 68  ry is left on th
1e93d 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61  e top of the sta
1e93e 63 6b 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 72  ck..    ** The r
1e93f 6f 77 69 64 20 76 61 6c 75 65 20 69 73 20 6e 65  owid value is ne
1e940 65 64 65 64 20 62 79 20 74 68 65 20 63 6f 64 65  eded by the code
1e941 20 74 68 61 74 20 73 71 6c 69 74 65 33 45 6e 64   that sqlite3End
1e942 54 61 62 6c 65 20 77 69 6c 6c 0a 20 20 20 20 2a  Table will.    *
1e943 2a 20 67 65 6e 65 72 61 74 65 2e 0a 20 20 20 20  * generate..    
1e944 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  */.#if !defined(
1e945 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
1e946 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51  ) || !defined(SQ
1e947 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1e948 4c 54 41 42 4c 45 29 0a 20 20 20 20 69 66 28 20  LTABLE).    if( 
1e949 69 73 56 69 65 77 20 7c 7c 20 69 73 56 69 72 74  isView || isVirt
1e94a 75 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ual ){.      sql
1e94b 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1e94c 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
1e94d 20 72 65 67 32 29 3b 0a 20 20 20 20 7d 65 6c 73   reg2);.    }els
1e94e 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20  e.#endif.    {. 
1e94f 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1e950 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 72 65  AddOp2(v, OP_Cre
1e951 61 74 65 54 61 62 6c 65 2c 20 69 44 62 2c 20 72  ateTable, iDb, r
1e952 65 67 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  eg2);.    }.    
1e953 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65  sqlite3OpenMaste
1e954 72 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69  rTable(pParse, i
1e955 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Db);.    sqlite3
1e956 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1e957 5f 4e 65 77 52 6f 77 69 64 2c 20 30 2c 20 72 65  _NewRowid, 0, re
1e958 67 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  g1);.    sqlite3
1e959 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1e95a 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 33 29 3b  _Null, 0, reg3);
1e95b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1e95c 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73  AddOp3(v, OP_Ins
1e95d 65 72 74 2c 20 30 2c 20 72 65 67 33 2c 20 72 65  ert, 0, reg3, re
1e95e 67 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  g1);.    sqlite3
1e95f 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
1e960 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a  OPFLAG_APPEND);.
1e961 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1e962 64 64 4f 70 30 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp0(v, OP_Clos
1e963 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f  e);.  }..  /* No
1e964 72 6d 61 6c 20 28 6e 6f 6e 2d 65 72 72 6f 72 29  rmal (non-error)
1e965 20 72 65 74 75 72 6e 2e 20 2a 2f 0a 20 20 72 65   return. */.  re
1e966 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 49 66 20 61  turn;..  /* If a
1e967 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
1e968 77 65 20 6a 75 6d 70 20 68 65 72 65 20 2a 2f 0a  we jump here */.
1e969 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f  begin_table_erro
1e96a 72 3a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  r:.  sqlite3DbFr
1e96b 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20  ee(db, zName);. 
1e96c 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a   return;.}../*.*
1e96d 2a 20 54 68 69 73 20 6d 61 63 72 6f 20 69 73 20  * This macro is 
1e96e 75 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 65 20  used to compare 
1e96f 74 77 6f 20 73 74 72 69 6e 67 73 20 69 6e 20 61  two strings in a
1e970 20 63 61 73 65 2d 69 6e 73 65 6e 73 69 74 69 76   case-insensitiv
1e971 65 20 6d 61 6e 6e 65 72 2e 0a 2a 2a 20 49 74 20  e manner..** It 
1e972 69 73 20 73 6c 69 67 68 74 6c 79 20 66 61 73 74  is slightly fast
1e973 65 72 20 74 68 61 6e 20 63 61 6c 6c 69 6e 67 20  er than calling 
1e974 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 29  sqlite3StrICmp()
1e975 20 64 69 72 65 63 74 6c 79 2c 20 62 75 74 0a 2a   directly, but.*
1e976 2a 20 70 72 6f 64 75 63 65 73 20 6c 61 72 67 65  * produces large
1e977 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 57 41  r code..**.** WA
1e978 52 4e 49 4e 47 3a 20 54 68 69 73 20 6d 61 63 72  RNING: This macr
1e979 6f 20 69 73 20 6e 6f 74 20 63 6f 6d 70 61 74 69  o is not compati
1e97a 62 6c 65 20 77 69 74 68 20 74 68 65 20 73 74 72  ble with the str
1e97b 63 6d 70 28 29 20 66 61 6d 69 6c 79 2e 20 49 74  cmp() family. It
1e97c 0a 2a 2a 20 72 65 74 75 72 6e 73 20 74 72 75 65  .** returns true
1e97d 20 69 66 20 74 68 65 20 74 77 6f 20 73 74 72 69   if the two stri
1e97e 6e 67 73 20 61 72 65 20 65 71 75 61 6c 2c 20 6f  ngs are equal, o
1e97f 74 68 65 72 77 69 73 65 20 66 61 6c 73 65 2e 0a  therwise false..
1e980 2a 2f 0a 23 64 65 66 69 6e 65 20 53 54 52 49 43  */.#define STRIC
1e981 4d 50 28 78 2c 20 79 29 20 28 5c 0a 73 71 6c 69  MP(x, y) (\.sqli
1e982 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  te3UpperToLower[
1e983 2a 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  *(unsigned char 
1e984 2a 29 28 78 29 5d 3d 3d 20 20 20 5c 0a 73 71 6c  *)(x)]==   \.sql
1e985 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72  ite3UpperToLower
1e986 5b 2a 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  [*(unsigned char
1e987 20 2a 29 28 79 29 5d 20 20 20 20 20 5c 0a 26 26   *)(y)]     \.&&
1e988 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
1e989 28 78 29 2b 31 2c 28 79 29 2b 31 29 3d 3d 30 20  (x)+1,(y)+1)==0 
1e98a 29 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e  )../*.** Add a n
1e98b 65 77 20 63 6f 6c 75 6d 6e 20 74 6f 20 74 68 65  ew column to the
1e98c 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79   table currently
1e98d 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74   being construct
1e98e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61  ed..**.** The pa
1e98f 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20  rser calls this 
1e990 72 6f 75 74 69 6e 65 20 6f 6e 63 65 20 66 6f 72  routine once for
1e991 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 64 65 63   each column dec
1e992 6c 61 72 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20 61  laration.** in a
1e993 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
1e994 61 74 65 6d 65 6e 74 2e 20 20 73 71 6c 69 74 65  atement.  sqlite
1e995 33 53 74 61 72 74 54 61 62 6c 65 28 29 20 67 65  3StartTable() ge
1e996 74 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 66 69 72  ts called.** fir
1e997 73 74 20 74 6f 20 67 65 74 20 74 68 69 6e 67 73  st to get things
1e998 20 67 6f 69 6e 67 2e 20 20 54 68 65 6e 20 74 68   going.  Then th
1e999 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
1e99a 6c 6c 65 64 20 66 6f 72 20 65 61 63 68 0a 2a 2a  lled for each.**
1e99b 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49   column..*/.SQLI
1e99c 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1e99d 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e  sqlite3AddColumn
1e99e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1e99f 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20  Token *pName){. 
1e9a0 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74   Table *p;.  int
1e9a1 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20   i;.  char *z;. 
1e9a2 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20   Column *pCol;. 
1e9a3 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
1e9a4 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28  Parse->db;.  if(
1e9a5 20 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e   (p = pParse->pN
1e9a6 65 77 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65  ewTable)==0 ) re
1e9a7 74 75 72 6e 3b 0a 23 69 66 20 53 51 4c 49 54 45  turn;.#if SQLITE
1e9a8 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66  _MAX_COLUMN.  if
1e9a9 28 20 70 2d 3e 6e 43 6f 6c 2b 31 3e 64 62 2d 3e  ( p->nCol+1>db->
1e9aa 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
1e9ab 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20  MIT_COLUMN] ){. 
1e9ac 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1e9ad 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20  sg(pParse, "too 
1e9ae 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 6f 6e 20  many columns on 
1e9af 25 73 22 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3b 0a  %s", p->zName);.
1e9b0 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
1e9b1 23 65 6e 64 69 66 0a 20 20 7a 20 3d 20 73 71 6c  #endif.  z = sql
1e9b2 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
1e9b3 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4e  n(pParse->db, pN
1e9b4 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 3d 3d 30  ame);.  if( z==0
1e9b5 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72   ) return;.  for
1e9b6 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b  (i=0; i<p->nCol;
1e9b7 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 53   i++){.    if( S
1e9b8 54 52 49 43 4d 50 28 7a 2c 20 70 2d 3e 61 43 6f  TRICMP(z, p->aCo
1e9b9 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20  l[i].zName) ){. 
1e9ba 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1e9bb 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 64 75  rMsg(pParse, "du
1e9bc 70 6c 69 63 61 74 65 20 63 6f 6c 75 6d 6e 20 6e  plicate column n
1e9bd 61 6d 65 3a 20 25 73 22 2c 20 7a 29 3b 0a 20 20  ame: %s", z);.  
1e9be 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1e9bf 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20  e(db, z);.      
1e9c0 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
1e9c1 7d 0a 20 20 69 66 28 20 28 70 2d 3e 6e 43 6f 6c  }.  if( (p->nCol
1e9c2 20 26 20 30 78 37 29 3d 3d 30 20 29 7b 0a 20 20   & 0x7)==0 ){.  
1e9c3 20 20 43 6f 6c 75 6d 6e 20 2a 61 4e 65 77 3b 0a    Column *aNew;.
1e9c4 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 74      aNew = sqlit
1e9c5 65 33 44 62 52 65 61 6c 6c 6f 63 28 70 50 61 72  e3DbRealloc(pPar
1e9c6 73 65 2d 3e 64 62 2c 70 2d 3e 61 43 6f 6c 2c 28  se->db,p->aCol,(
1e9c7 70 2d 3e 6e 43 6f 6c 2b 38 29 2a 73 69 7a 65 6f  p->nCol+8)*sizeo
1e9c8 66 28 70 2d 3e 61 43 6f 6c 5b 30 5d 29 29 3b 0a  f(p->aCol[0]));.
1e9c9 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20      if( aNew==0 
1e9ca 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1e9cb 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20  DbFree(db, z);. 
1e9cc 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
1e9cd 20 7d 0a 20 20 20 20 70 2d 3e 61 43 6f 6c 20 3d   }.    p->aCol =
1e9ce 20 61 4e 65 77 3b 0a 20 20 7d 0a 20 20 70 43 6f   aNew;.  }.  pCo
1e9cf 6c 20 3d 20 26 70 2d 3e 61 43 6f 6c 5b 70 2d 3e  l = &p->aCol[p->
1e9d0 6e 43 6f 6c 5d 3b 0a 20 20 6d 65 6d 73 65 74 28  nCol];.  memset(
1e9d1 70 43 6f 6c 2c 20 30 2c 20 73 69 7a 65 6f 66 28  pCol, 0, sizeof(
1e9d2 70 2d 3e 61 43 6f 6c 5b 30 5d 29 29 3b 0a 20 20  p->aCol[0]));.  
1e9d3 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 3b  pCol->zName = z;
1e9d4 0a 20 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  . .  /* If there
1e9d5 20 69 73 20 6e 6f 20 74 79 70 65 20 73 70 65 63   is no type spec
1e9d6 69 66 69 65 64 2c 20 63 6f 6c 75 6d 6e 73 20 68  ified, columns h
1e9d7 61 76 65 20 74 68 65 20 64 65 66 61 75 6c 74 20  ave the default 
1e9d8 61 66 66 69 6e 69 74 79 0a 20 20 2a 2a 20 27 4e  affinity.  ** 'N
1e9d9 4f 4e 45 27 2e 20 49 66 20 74 68 65 72 65 20 69  ONE'. If there i
1e9da 73 20 61 20 74 79 70 65 20 73 70 65 63 69 66 69  s a type specifi
1e9db 65 64 2c 20 74 68 65 6e 20 73 71 6c 69 74 65 33  ed, then sqlite3
1e9dc 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 29 20  AddColumnType() 
1e9dd 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 63 61 6c  will.  ** be cal
1e9de 6c 65 64 20 6e 65 78 74 20 74 6f 20 73 65 74 20  led next to set 
1e9df 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 63  pCol->affinity c
1e9e0 6f 72 72 65 63 74 6c 79 2e 0a 20 20 2a 2f 0a 20  orrectly..  */. 
1e9e1 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20   pCol->affinity 
1e9e2 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  = SQLITE_AFF_NON
1e9e3 45 3b 0a 20 20 70 2d 3e 6e 43 6f 6c 2b 2b 3b 0a  E;.  p->nCol++;.
1e9e4 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
1e9e5 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
1e9e6 62 79 20 74 68 65 20 70 61 72 73 65 72 20 77 68  by the parser wh
1e9e7 69 6c 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  ile in the middl
1e9e8 65 20 6f 66 0a 2a 2a 20 70 61 72 73 69 6e 67 20  e of.** parsing 
1e9e9 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  a CREATE TABLE s
1e9ea 74 61 74 65 6d 65 6e 74 2e 20 20 41 20 22 4e 4f  tatement.  A "NO
1e9eb 54 20 4e 55 4c 4c 22 20 63 6f 6e 73 74 72 61 69  T NULL" constrai
1e9ec 6e 74 20 68 61 73 0a 2a 2a 20 62 65 65 6e 20 73  nt has.** been s
1e9ed 65 65 6e 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 2e  een on a column.
1e9ee 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73    This routine s
1e9ef 65 74 73 20 74 68 65 20 6e 6f 74 4e 75 6c 6c 20  ets the notNull 
1e9f0 66 6c 61 67 20 6f 6e 0a 2a 2a 20 74 68 65 20 63  flag on.** the c
1e9f1 6f 6c 75 6d 6e 20 63 75 72 72 65 6e 74 6c 79 20  olumn currently 
1e9f2 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69  under constructi
1e9f3 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
1e9f4 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1e9f5 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50 61 72  e3AddNotNull(Par
1e9f6 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
1e9f7 6f 6e 45 72 72 6f 72 29 7b 0a 20 20 54 61 62 6c  onError){.  Tabl
1e9f8 65 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  e *p;.  int i;. 
1e9f9 20 69 66 28 20 28 70 20 3d 20 70 50 61 72 73 65   if( (p = pParse
1e9fa 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20  ->pNewTable)==0 
1e9fb 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 20 3d 20  ) return;.  i = 
1e9fc 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 69 66 28  p->nCol-1;.  if(
1e9fd 20 69 3e 3d 30 20 29 20 70 2d 3e 61 43 6f 6c 5b   i>=0 ) p->aCol[
1e9fe 69 5d 2e 6e 6f 74 4e 75 6c 6c 20 3d 20 6f 6e 45  i].notNull = onE
1e9ff 72 72 6f 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  rror;.}../*.** S
1ea00 63 61 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74  can the column t
1ea01 79 70 65 20 6e 61 6d 65 20 7a 54 79 70 65 20 28  ype name zType (
1ea02 6c 65 6e 67 74 68 20 6e 54 79 70 65 29 20 61 6e  length nType) an
1ea03 64 20 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20  d return the.** 
1ea04 61 73 73 6f 63 69 61 74 65 64 20 61 66 66 69 6e  associated affin
1ea05 69 74 79 20 74 79 70 65 2e 0a 2a 2a 0a 2a 2a 20  ity type..**.** 
1ea06 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65  This routine doe
1ea07 73 20 61 20 63 61 73 65 2d 69 6e 64 65 70 65 6e  s a case-indepen
1ea08 64 65 6e 74 20 73 65 61 72 63 68 20 6f 66 20 7a  dent search of z
1ea09 54 79 70 65 20 66 6f 72 20 74 68 65 20 0a 2a 2a  Type for the .**
1ea0a 20 73 75 62 73 74 72 69 6e 67 73 20 69 6e 20 74   substrings in t
1ea0b 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62  he following tab
1ea0c 6c 65 2e 20 49 66 20 6f 6e 65 20 6f 66 20 74 68  le. If one of th
1ea0d 65 20 73 75 62 73 74 72 69 6e 67 73 20 69 73 0a  e substrings is.
1ea0e 2a 2a 20 66 6f 75 6e 64 2c 20 74 68 65 20 63 6f  ** found, the co
1ea0f 72 72 65 73 70 6f 6e 64 69 6e 67 20 61 66 66 69  rresponding affi
1ea10 6e 69 74 79 20 69 73 20 72 65 74 75 72 6e 65 64  nity is returned
1ea11 2e 20 49 66 20 7a 54 79 70 65 20 63 6f 6e 74 61  . If zType conta
1ea12 69 6e 73 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e  ins.** more than
1ea13 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 75 62 73   one of the subs
1ea14 74 72 69 6e 67 73 2c 20 65 6e 74 72 69 65 73 20  trings, entries 
1ea15 74 6f 77 61 72 64 20 74 68 65 20 74 6f 70 20 6f  toward the top o
1ea16 66 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20  f .** the table 
1ea17 74 61 6b 65 20 70 72 69 6f 72 69 74 79 2e 20 46  take priority. F
1ea18 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 7a  or example, if z
1ea19 54 79 70 65 20 69 73 20 27 42 4c 4f 42 49 4e 54  Type is 'BLOBINT
1ea1a 27 2c 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46  ', .** SQLITE_AF
1ea1b 46 5f 49 4e 54 45 47 45 52 20 69 73 20 72 65 74  F_INTEGER is ret
1ea1c 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 75 62  urned..**.** Sub
1ea1d 73 74 72 69 6e 67 20 20 20 20 20 7c 20 41 66 66  string     | Aff
1ea1e 69 6e 69 74 79 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d  inity.** -------
1ea1f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1ea20 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 27 49 4e  ---------.** 'IN
1ea21 54 27 20 20 20 20 20 20 20 20 20 7c 20 53 51 4c  T'         | SQL
1ea22 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 0a  ITE_AFF_INTEGER.
1ea23 2a 2a 20 27 43 48 41 52 27 20 20 20 20 20 20 20  ** 'CHAR'       
1ea24 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45   | SQLITE_AFF_TE
1ea25 58 54 0a 2a 2a 20 27 43 4c 4f 42 27 20 20 20 20  XT.** 'CLOB'    
1ea26 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46      | SQLITE_AFF
1ea27 5f 54 45 58 54 0a 2a 2a 20 27 54 45 58 54 27 20  _TEXT.** 'TEXT' 
1ea28 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f         | SQLITE_
1ea29 41 46 46 5f 54 45 58 54 0a 2a 2a 20 27 42 4c 4f  AFF_TEXT.** 'BLO
1ea2a 42 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49  B'        | SQLI
1ea2b 54 45 5f 41 46 46 5f 4e 4f 4e 45 0a 2a 2a 20 27  TE_AFF_NONE.** '
1ea2c 52 45 41 4c 27 20 20 20 20 20 20 20 20 7c 20 53  REAL'        | S
1ea2d 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 2a  QLITE_AFF_REAL.*
1ea2e 2a 20 27 46 4c 4f 41 27 20 20 20 20 20 20 20 20  * 'FLOA'        
1ea2f 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41  | SQLITE_AFF_REA
1ea30 4c 0a 2a 2a 20 27 44 4f 55 42 27 20 20 20 20 20  L.** 'DOUB'     
1ea31 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f     | SQLITE_AFF_
1ea32 52 45 41 4c 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f  REAL.**.** If no
1ea33 6e 65 20 6f 66 20 74 68 65 20 73 75 62 73 74 72  ne of the substr
1ea34 69 6e 67 73 20 69 6e 20 74 68 65 20 61 62 6f 76  ings in the abov
1ea35 65 20 74 61 62 6c 65 20 61 72 65 20 66 6f 75 6e  e table are foun
1ea36 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46  d,.** SQLITE_AFF
1ea37 5f 4e 55 4d 45 52 49 43 20 69 73 20 72 65 74 75  _NUMERIC is retu
1ea38 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rned..*/.SQLITE_
1ea39 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c  PRIVATE char sql
1ea3a 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65  ite3AffinityType
1ea3b 28 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 54  (const Token *pT
1ea3c 79 70 65 29 7b 0a 20 20 75 33 32 20 68 20 3d 20  ype){.  u32 h = 
1ea3d 30 3b 0a 20 20 63 68 61 72 20 61 66 66 20 3d 20  0;.  char aff = 
1ea3e 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52  SQLITE_AFF_NUMER
1ea3f 49 43 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69  IC;.  const unsi
1ea40 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 20 3d  gned char *zIn =
1ea41 20 70 54 79 70 65 2d 3e 7a 3b 0a 20 20 63 6f 6e   pType->z;.  con
1ea42 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
1ea43 20 2a 7a 45 6e 64 20 3d 20 26 70 54 79 70 65 2d   *zEnd = &pType-
1ea44 3e 7a 5b 70 54 79 70 65 2d 3e 6e 5d 3b 0a 0a 20  >z[pType->n];.. 
1ea45 20 77 68 69 6c 65 28 20 7a 49 6e 21 3d 7a 45 6e   while( zIn!=zEn
1ea46 64 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c  d ){.    h = (h<
1ea47 3c 38 29 20 2b 20 73 71 6c 69 74 65 33 55 70 70  <8) + sqlite3Upp
1ea48 65 72 54 6f 4c 6f 77 65 72 5b 2a 7a 49 6e 5d 3b  erToLower[*zIn];
1ea49 0a 20 20 20 20 7a 49 6e 2b 2b 3b 0a 20 20 20 20  .    zIn++;.    
1ea4a 69 66 28 20 68 3d 3d 28 28 27 63 27 3c 3c 32 34  if( h==(('c'<<24
1ea4b 29 2b 28 27 68 27 3c 3c 31 36 29 2b 28 27 61 27  )+('h'<<16)+('a'
1ea4c 3c 3c 38 29 2b 27 72 27 29 20 29 7b 20 20 20 20  <<8)+'r') ){    
1ea4d 20 20 20 20 20 20 20 20 20 2f 2a 20 43 48 41 52           /* CHAR
1ea4e 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 3d 20   */.      aff = 
1ea4f 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3b  SQLITE_AFF_TEXT;
1ea50 20 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20   .    }else if( 
1ea51 68 3d 3d 28 28 27 63 27 3c 3c 32 34 29 2b 28 27  h==(('c'<<24)+('
1ea52 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29  l'<<16)+('o'<<8)
1ea53 2b 27 62 27 29 20 29 7b 20 20 20 20 20 20 20 2f  +'b') ){       /
1ea54 2a 20 43 4c 4f 42 20 2a 2f 0a 20 20 20 20 20 20  * CLOB */.      
1ea55 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  aff = SQLITE_AFF
1ea56 5f 54 45 58 54 3b 0a 20 20 20 20 7d 65 6c 73 65  _TEXT;.    }else
1ea57 20 69 66 28 20 68 3d 3d 28 28 27 74 27 3c 3c 32   if( h==(('t'<<2
1ea58 34 29 2b 28 27 65 27 3c 3c 31 36 29 2b 28 27 78  4)+('e'<<16)+('x
1ea59 27 3c 3c 38 29 2b 27 74 27 29 20 29 7b 20 20 20  '<<8)+'t') ){   
1ea5a 20 20 20 20 2f 2a 20 54 45 58 54 20 2a 2f 0a 20      /* TEXT */. 
1ea5b 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54       aff = SQLIT
1ea5c 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20  E_AFF_TEXT;.    
1ea5d 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27  }else if( h==(('
1ea5e 62 27 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 36  b'<<24)+('l'<<16
1ea5f 29 2b 28 27 6f 27 3c 3c 38 29 2b 27 62 27 29 20  )+('o'<<8)+'b') 
1ea60 20 20 20 20 20 20 20 20 20 2f 2a 20 42 4c 4f 42           /* BLOB
1ea61 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26 20 28   */.        && (
1ea62 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  aff==SQLITE_AFF_
1ea63 4e 55 4d 45 52 49 43 20 7c 7c 20 61 66 66 3d 3d  NUMERIC || aff==
1ea64 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 29  SQLITE_AFF_REAL)
1ea65 20 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20   ){.      aff = 
1ea66 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b  SQLITE_AFF_NONE;
1ea67 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1ea68 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
1ea69 49 4e 54 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  INT.    }else if
1ea6a 28 20 68 3d 3d 28 28 27 72 27 3c 3c 32 34 29 2b  ( h==(('r'<<24)+
1ea6b 28 27 65 27 3c 3c 31 36 29 2b 28 27 61 27 3c 3c  ('e'<<16)+('a'<<
1ea6c 38 29 2b 27 6c 27 29 20 20 20 20 20 20 20 20 20  8)+'l')         
1ea6d 20 2f 2a 20 52 45 41 4c 20 2a 2f 0a 20 20 20 20   /* REAL */.    
1ea6e 20 20 20 20 26 26 20 61 66 66 3d 3d 53 51 4c 49      && aff==SQLI
1ea6f 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29  TE_AFF_NUMERIC )
1ea70 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51  {.      aff = SQ
1ea71 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3b 0a 20  LITE_AFF_REAL;. 
1ea72 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d     }else if( h==
1ea73 28 28 27 66 27 3c 3c 32 34 29 2b 28 27 6c 27 3c  (('f'<<24)+('l'<
1ea74 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29 2b 27 61  <16)+('o'<<8)+'a
1ea75 27 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  ')          /* F
1ea76 4c 4f 41 20 2a 2f 0a 20 20 20 20 20 20 20 20 26  LOA */.        &
1ea77 26 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46  & aff==SQLITE_AF
1ea78 46 5f 4e 55 4d 45 52 49 43 20 29 7b 0a 20 20 20  F_NUMERIC ){.   
1ea79 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f     aff = SQLITE_
1ea7a 41 46 46 5f 52 45 41 4c 3b 0a 20 20 20 20 7d 65  AFF_REAL;.    }e
1ea7b 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 64 27  lse if( h==(('d'
1ea7c 3c 3c 32 34 29 2b 28 27 6f 27 3c 3c 31 36 29 2b  <<24)+('o'<<16)+
1ea7d 28 27 75 27 3c 3c 38 29 2b 27 62 27 29 20 20 20  ('u'<<8)+'b')   
1ea7e 20 20 20 20 20 20 20 2f 2a 20 44 4f 55 42 20 2a         /* DOUB *
1ea7f 2f 0a 20 20 20 20 20 20 20 20 26 26 20 61 66 66  /.        && aff
1ea80 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d  ==SQLITE_AFF_NUM
1ea81 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20 61 66  ERIC ){.      af
1ea82 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 52  f = SQLITE_AFF_R
1ea83 45 41 4c 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  EAL;.#endif.    
1ea84 7d 65 6c 73 65 20 69 66 28 20 28 68 26 30 78 30  }else if( (h&0x0
1ea85 30 46 46 46 46 46 46 29 3d 3d 28 28 27 69 27 3c  0FFFFFF)==(('i'<
1ea86 3c 31 36 29 2b 28 27 6e 27 3c 3c 38 29 2b 27 74  <16)+('n'<<8)+'t
1ea87 27 29 20 29 7b 20 20 20 20 2f 2a 20 49 4e 54 20  ') ){    /* INT 
1ea88 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53  */.      aff = S
1ea89 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45  QLITE_AFF_INTEGE
1ea8a 52 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  R;.      break;.
1ea8b 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
1ea8c 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a  urn aff;.}../*.*
1ea8d 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
1ea8e 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  s called by the 
1ea8f 70 61 72 73 65 72 20 77 68 69 6c 65 20 69 6e 20  parser while in 
1ea90 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a  the middle of.**
1ea91 20 70 61 72 73 69 6e 67 20 61 20 43 52 45 41 54   parsing a CREAT
1ea92 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  E TABLE statemen
1ea93 74 2e 20 20 54 68 65 20 70 46 69 72 73 74 20 74  t.  The pFirst t
1ea94 6f 6b 65 6e 20 69 73 20 74 68 65 20 66 69 72 73  oken is the firs
1ea95 74 0a 2a 2a 20 74 6f 6b 65 6e 20 69 6e 20 74 68  t.** token in th
1ea96 65 20 73 65 71 75 65 6e 63 65 20 6f 66 20 74 6f  e sequence of to
1ea97 6b 65 6e 73 20 74 68 61 74 20 64 65 73 63 72 69  kens that descri
1ea98 62 65 20 74 68 65 20 74 79 70 65 20 6f 66 20 74  be the type of t
1ea99 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 63 75 72  he.** column cur
1ea9a 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e  rently under con
1ea9b 73 74 72 75 63 74 69 6f 6e 2e 20 20 20 70 4c 61  struction.   pLa
1ea9c 73 74 20 69 73 20 74 68 65 20 6c 61 73 74 20 74  st is the last t
1ea9d 6f 6b 65 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 73  oken.** in the s
1ea9e 65 71 75 65 6e 63 65 2e 20 20 55 73 65 20 74 68  equence.  Use th
1ea9f 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74  is information t
1eaa0 6f 20 63 6f 6e 73 74 72 75 63 74 20 61 20 73 74  o construct a st
1eaa1 72 69 6e 67 0a 2a 2a 20 74 68 61 74 20 63 6f 6e  ring.** that con
1eaa2 74 61 69 6e 73 20 74 68 65 20 74 79 70 65 6e 61  tains the typena
1eaa3 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  me of the column
1eaa4 20 61 6e 64 20 73 74 6f 72 65 20 74 68 61 74 20   and store that 
1eaa5 73 74 72 69 6e 67 0a 2a 2a 20 69 6e 20 7a 54 79  string.** in zTy
1eaa6 70 65 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50  pe..*/ .SQLITE_P
1eaa7 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1eaa8 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65  te3AddColumnType
1eaa9 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1eaaa 54 6f 6b 65 6e 20 2a 70 54 79 70 65 29 7b 0a 20  Token *pType){. 
1eaab 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74   Table *p;.  int
1eaac 20 69 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43   i;.  Column *pC
1eaad 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ol;.  sqlite3 *d
1eaae 62 3b 0a 0a 20 20 69 66 28 20 28 70 20 3d 20 70  b;..  if( (p = p
1eaaf 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
1eab0 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  )==0 ) return;. 
1eab1 20 69 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a   i = p->nCol-1;.
1eab2 20 20 69 66 28 20 69 3c 30 20 29 20 72 65 74 75    if( i<0 ) retu
1eab3 72 6e 3b 0a 20 20 70 43 6f 6c 20 3d 20 26 70 2d  rn;.  pCol = &p-
1eab4 3e 61 43 6f 6c 5b 69 5d 3b 0a 20 20 64 62 20 3d  >aCol[i];.  db =
1eab5 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 73   pParse->db;.  s
1eab6 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1eab7 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 29 3b 0a 20   pCol->zType);. 
1eab8 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20 73   pCol->zType = s
1eab9 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
1eaba 6b 65 6e 28 64 62 2c 20 70 54 79 70 65 29 3b 0a  ken(db, pType);.
1eabb 20 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79    pCol->affinity
1eabc 20 3d 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69   = sqlite3Affini
1eabd 74 79 54 79 70 65 28 70 54 79 70 65 29 3b 0a 7d  tyType(pType);.}
1eabe 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 65 78 70 72  ../*.** The expr
1eabf 65 73 73 69 6f 6e 20 69 73 20 74 68 65 20 64 65  ession is the de
1eac0 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20  fault value for 
1eac1 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  the most recentl
1eac2 79 20 61 64 64 65 64 20 63 6f 6c 75 6d 6e 0a 2a  y added column.*
1eac3 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63  * of the table c
1eac4 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63  urrently under c
1eac5 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a  onstruction..**.
1eac6 2a 2a 20 44 65 66 61 75 6c 74 20 76 61 6c 75 65  ** Default value
1eac7 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6d 75 73   expressions mus
1eac8 74 20 62 65 20 63 6f 6e 73 74 61 6e 74 2e 20 20  t be constant.  
1eac9 52 61 69 73 65 20 61 6e 20 65 78 63 65 70 74 69  Raise an excepti
1eaca 6f 6e 20 69 66 20 74 68 69 73 0a 2a 2a 20 69 73  on if this.** is
1eacb 20 6e 6f 74 20 74 68 65 20 63 61 73 65 2e 0a 2a   not the case..*
1eacc 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1eacd 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74  e is called by t
1eace 68 65 20 70 61 72 73 65 72 20 77 68 69 6c 65 20  he parser while 
1eacf 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
1ead0 0a 2a 2a 20 70 61 72 73 69 6e 67 20 61 20 43 52  .** parsing a CR
1ead1 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
1ead2 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ment..*/.SQLITE_
1ead3 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1ead4 69 74 65 33 41 64 64 44 65 66 61 75 6c 74 56 61  ite3AddDefaultVa
1ead5 6c 75 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  lue(Parse *pPars
1ead6 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b  e, Expr *pExpr){
1ead7 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 43  .  Table *p;.  C
1ead8 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73  olumn *pCol;.  s
1ead9 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
1eada 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 28  rse->db;.  if( (
1eadb 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  p = pParse->pNew
1eadc 54 61 62 6c 65 29 21 3d 30 20 29 7b 0a 20 20 20  Table)!=0 ){.   
1eadd 20 70 43 6f 6c 20 3d 20 26 28 70 2d 3e 61 43 6f   pCol = &(p->aCo
1eade 6c 5b 70 2d 3e 6e 43 6f 6c 2d 31 5d 29 3b 0a 20  l[p->nCol-1]);. 
1eadf 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 45     if( !sqlite3E
1eae0 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46  xprIsConstantOrF
1eae1 75 6e 63 74 69 6f 6e 28 70 45 78 70 72 29 20 29  unction(pExpr) )
1eae2 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
1eae3 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1eae4 22 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f  "default value o
1eae5 66 20 63 6f 6c 75 6d 6e 20 5b 25 73 5d 20 69 73  f column [%s] is
1eae6 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 22 2c 0a   not constant",.
1eae7 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e            pCol->
1eae8 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73  zName);.    }els
1eae9 65 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  e{.      Expr *p
1eaea 43 6f 70 79 3b 0a 20 20 20 20 20 20 73 71 6c 69  Copy;.      sqli
1eaeb 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
1eaec 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 29 3b 0a  , pCol->pDflt);.
1eaed 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70 44 66 6c        pCol->pDfl
1eaee 74 20 3d 20 70 43 6f 70 79 20 3d 20 73 71 6c 69  t = pCopy = sqli
1eaef 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
1eaf0 45 78 70 72 29 3b 0a 20 20 20 20 20 20 69 66 28  Expr);.      if(
1eaf1 20 70 43 6f 70 79 20 29 7b 0a 20 20 20 20 20 20   pCopy ){.      
1eaf2 20 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f    sqlite3TokenCo
1eaf3 70 79 28 64 62 2c 20 26 70 43 6f 70 79 2d 3e 73  py(db, &pCopy->s
1eaf4 70 61 6e 2c 20 26 70 45 78 70 72 2d 3e 73 70 61  pan, &pExpr->spa
1eaf5 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  n);.      }.    
1eaf6 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45  }.  }.  sqlite3E
1eaf7 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45  xprDelete(db, pE
1eaf8 78 70 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  xpr);.}../*.** D
1eaf9 65 73 69 67 6e 61 74 65 20 74 68 65 20 50 52 49  esignate the PRI
1eafa 4d 41 52 59 20 4b 45 59 20 66 6f 72 20 74 68 65  MARY KEY for the
1eafb 20 74 61 62 6c 65 2e 20 20 70 4c 69 73 74 20 69   table.  pList i
1eafc 73 20 61 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65  s a list of name
1eafd 73 20 0a 2a 2a 20 6f 66 20 63 6f 6c 75 6d 6e 73  s .** of columns
1eafe 20 74 68 61 74 20 66 6f 72 6d 20 74 68 65 20 70   that form the p
1eaff 72 69 6d 61 72 79 20 6b 65 79 2e 20 20 49 66 20  rimary key.  If 
1eb00 70 4c 69 73 74 20 69 73 20 4e 55 4c 4c 2c 20 74  pList is NULL, t
1eb01 68 65 6e 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 20  hen the.** most 
1eb02 72 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20 63  recently added c
1eb03 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 62  olumn of the tab
1eb04 6c 65 20 69 73 20 74 68 65 20 70 72 69 6d 61 72  le is the primar
1eb05 79 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 74  y key..**.** A t
1eb06 61 62 6c 65 20 63 61 6e 20 68 61 76 65 20 61 74  able can have at
1eb07 20 6d 6f 73 74 20 6f 6e 65 20 70 72 69 6d 61 72   most one primar
1eb08 79 20 6b 65 79 2e 20 20 49 66 20 74 68 65 20 74  y key.  If the t
1eb09 61 62 6c 65 20 61 6c 72 65 61 64 79 20 68 61 73  able already has
1eb0a 0a 2a 2a 20 61 20 70 72 69 6d 61 72 79 20 6b 65  .** a primary ke
1eb0b 79 20 28 61 6e 64 20 74 68 69 73 20 69 73 20 74  y (and this is t
1eb0c 68 65 20 73 65 63 6f 6e 64 20 70 72 69 6d 61 72  he second primar
1eb0d 79 20 6b 65 79 29 20 74 68 65 6e 20 63 72 65 61  y key) then crea
1eb0e 74 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 2e 0a  te an.** error..
1eb0f 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 52 49  **.** If the PRI
1eb10 4d 41 52 59 20 4b 45 59 20 69 73 20 6f 6e 20 61  MARY KEY is on a
1eb11 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 77   single column w
1eb12 68 6f 73 65 20 64 61 74 61 74 79 70 65 20 69 73  hose datatype is
1eb13 20 49 4e 54 45 47 45 52 2c 0a 2a 2a 20 74 68 65   INTEGER,.** the
1eb14 6e 20 77 65 20 77 69 6c 6c 20 74 72 79 20 74 6f  n we will try to
1eb15 20 75 73 65 20 74 68 61 74 20 63 6f 6c 75 6d 6e   use that column
1eb16 20 61 73 20 74 68 65 20 72 6f 77 69 64 2e 20 20   as the rowid.  
1eb17 53 65 74 20 74 68 65 20 54 61 62 6c 65 2e 69 50  Set the Table.iP
1eb18 4b 65 79 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20  Key.** field of 
1eb19 74 68 65 20 74 61 62 6c 65 20 75 6e 64 65 72 20  the table under 
1eb1a 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20  construction to 
1eb1b 62 65 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  be the index of 
1eb1c 74 68 65 0a 2a 2a 20 49 4e 54 45 47 45 52 20 50  the.** INTEGER P
1eb1d 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d  RIMARY KEY colum
1eb1e 6e 2e 20 20 54 61 62 6c 65 2e 69 50 4b 65 79 20  n.  Table.iPKey 
1eb1f 69 73 20 73 65 74 20 74 6f 20 2d 31 20 69 66 20  is set to -1 if 
1eb20 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 20 49  there is.** no I
1eb21 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
1eb22 45 59 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  EY..**.** If the
1eb23 20 6b 65 79 20 69 73 20 6e 6f 74 20 61 6e 20 49   key is not an I
1eb24 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
1eb25 45 59 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20  EY, then create 
1eb26 61 20 75 6e 69 71 75 65 0a 2a 2a 20 69 6e 64 65  a unique.** inde
1eb27 78 20 66 6f 72 20 74 68 65 20 6b 65 79 2e 20 20  x for the key.  
1eb28 4e 6f 20 69 6e 64 65 78 20 69 73 20 63 72 65 61  No index is crea
1eb29 74 65 64 20 66 6f 72 20 49 4e 54 45 47 45 52 20  ted for INTEGER 
1eb2a 50 52 49 4d 41 52 59 20 4b 45 59 73 2e 0a 2a 2f  PRIMARY KEYs..*/
1eb2b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1eb2c 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 50  void sqlite3AddP
1eb2d 72 69 6d 61 72 79 4b 65 79 28 0a 20 20 50 61 72  rimaryKey(.  Par
1eb2e 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f  se *pParse,    /
1eb2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
1eb30 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  t */.  ExprList 
1eb31 2a 70 4c 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74  *pList,  /* List
1eb32 20 6f 66 20 66 69 65 6c 64 20 6e 61 6d 65 73 20   of field names 
1eb33 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f  to be indexed */
1eb34 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20  .  int onError, 
1eb35 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20       /* What to 
1eb36 64 6f 20 77 69 74 68 20 61 20 75 6e 69 71 75 65  do with a unique
1eb37 6e 65 73 73 20 63 6f 6e 66 6c 69 63 74 20 2a 2f  ness conflict */
1eb38 0a 20 20 69 6e 74 20 61 75 74 6f 49 6e 63 2c 20  .  int autoInc, 
1eb39 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
1eb3a 74 68 65 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e  the AUTOINCREMEN
1eb3b 54 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65  T keyword is pre
1eb3c 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 73 6f  sent */.  int so
1eb3d 72 74 4f 72 64 65 72 20 20 20 20 20 2f 2a 20 53  rtOrder     /* S
1eb3e 51 4c 49 54 45 5f 53 4f 5f 41 53 43 20 6f 72 20  QLITE_SO_ASC or 
1eb3f 53 51 4c 49 54 45 5f 53 4f 5f 44 45 53 43 20 2a  SQLITE_SO_DESC *
1eb40 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54  /.){.  Table *pT
1eb41 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65  ab = pParse->pNe
1eb42 77 54 61 62 6c 65 3b 0a 20 20 63 68 61 72 20 2a  wTable;.  char *
1eb43 7a 54 79 70 65 20 3d 20 30 3b 0a 20 20 69 6e 74  zType = 0;.  int
1eb44 20 69 43 6f 6c 20 3d 20 2d 31 2c 20 69 3b 0a 20   iCol = -1, i;. 
1eb45 20 69 66 28 20 70 54 61 62 3d 3d 30 20 7c 7c 20   if( pTab==0 || 
1eb46 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20  IN_DECLARE_VTAB 
1eb47 29 20 67 6f 74 6f 20 70 72 69 6d 61 72 79 5f 6b  ) goto primary_k
1eb48 65 79 5f 65 78 69 74 3b 0a 20 20 69 66 28 20 70  ey_exit;.  if( p
1eb49 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20  Tab->tabFlags & 
1eb4a 54 46 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79  TF_HasPrimaryKey
1eb4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
1eb4c 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1eb4d 0a 20 20 20 20 20 20 22 74 61 62 6c 65 20 5c 22  .      "table \"
1eb4e 25 73 5c 22 20 68 61 73 20 6d 6f 72 65 20 74 68  %s\" has more th
1eb4f 61 6e 20 6f 6e 65 20 70 72 69 6d 61 72 79 20 6b  an one primary k
1eb50 65 79 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ey", pTab->zName
1eb51 29 3b 0a 20 20 20 20 67 6f 74 6f 20 70 72 69 6d  );.    goto prim
1eb52 61 72 79 5f 6b 65 79 5f 65 78 69 74 3b 0a 20 20  ary_key_exit;.  
1eb53 7d 0a 20 20 70 54 61 62 2d 3e 74 61 62 46 6c 61  }.  pTab->tabFla
1eb54 67 73 20 7c 3d 20 54 46 5f 48 61 73 50 72 69 6d  gs |= TF_HasPrim
1eb55 61 72 79 4b 65 79 3b 0a 20 20 69 66 28 20 70 4c  aryKey;.  if( pL
1eb56 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 43  ist==0 ){.    iC
1eb57 6f 6c 20 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 20  ol = pTab->nCol 
1eb58 2d 20 31 3b 0a 20 20 20 20 70 54 61 62 2d 3e 61  - 1;.    pTab->a
1eb59 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73 50 72 69 6d  Col[iCol].isPrim
1eb5a 4b 65 79 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  Key = 1;.  }else
1eb5b 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  {.    for(i=0; i
1eb5c 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69  <pList->nExpr; i
1eb5d 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69  ++){.      for(i
1eb5e 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62  Col=0; iCol<pTab
1eb5f 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b  ->nCol; iCol++){
1eb60 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  .        if( sql
1eb61 69 74 65 33 53 74 72 49 43 6d 70 28 70 4c 69 73  ite3StrICmp(pLis
1eb62 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 70  t->a[i].zName, p
1eb63 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e  Tab->aCol[iCol].
1eb64 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20  zName)==0 ){.   
1eb65 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1eb66 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1eb67 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 70        if( iCol<p
1eb68 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20  Tab->nCol ){.   
1eb69 20 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b       pTab->aCol[
1eb6a 69 43 6f 6c 5d 2e 69 73 50 72 69 6d 4b 65 79 20  iCol].isPrimKey 
1eb6b 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
1eb6c 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74   }.    if( pList
1eb6d 2d 3e 6e 45 78 70 72 3e 31 20 29 20 69 43 6f 6c  ->nExpr>1 ) iCol
1eb6e 20 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 69 66 28   = -1;.  }.  if(
1eb6f 20 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c   iCol>=0 && iCol
1eb70 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20  <pTab->nCol ){. 
1eb71 20 20 20 7a 54 79 70 65 20 3d 20 70 54 61 62 2d     zType = pTab-
1eb72 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 54 79 70  >aCol[iCol].zTyp
1eb73 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 54 79  e;.  }.  if( zTy
1eb74 70 65 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  pe && sqlite3Str
1eb75 49 43 6d 70 28 7a 54 79 70 65 2c 20 22 49 4e 54  ICmp(zType, "INT
1eb76 45 47 45 52 22 29 3d 3d 30 0a 20 20 20 20 20 20  EGER")==0.      
1eb77 20 20 26 26 20 73 6f 72 74 4f 72 64 65 72 3d 3d    && sortOrder==
1eb78 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 20 29 7b  SQLITE_SO_ASC ){
1eb79 0a 20 20 20 20 70 54 61 62 2d 3e 69 50 4b 65 79  .    pTab->iPKey
1eb7a 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 70 54 61   = iCol;.    pTa
1eb7b 62 2d 3e 6b 65 79 43 6f 6e 66 20 3d 20 6f 6e 45  b->keyConf = onE
1eb7c 72 72 6f 72 3b 0a 20 20 20 20 61 73 73 65 72 74  rror;.    assert
1eb7d 28 20 61 75 74 6f 49 6e 63 3d 3d 30 20 7c 7c 20  ( autoInc==0 || 
1eb7e 61 75 74 6f 49 6e 63 3d 3d 31 20 29 3b 0a 20 20  autoInc==1 );.  
1eb7f 20 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73    pTab->tabFlags
1eb80 20 7c 3d 20 61 75 74 6f 49 6e 63 2a 54 46 5f 41   |= autoInc*TF_A
1eb81 75 74 6f 69 6e 63 72 65 6d 65 6e 74 3b 0a 20 20  utoincrement;.  
1eb82 7d 65 6c 73 65 20 69 66 28 20 61 75 74 6f 49 6e  }else if( autoIn
1eb83 63 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  c ){.#ifndef SQL
1eb84 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43  ITE_OMIT_AUTOINC
1eb85 52 45 4d 45 4e 54 0a 20 20 20 20 73 71 6c 69 74  REMENT.    sqlit
1eb86 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1eb87 65 2c 20 22 41 55 54 4f 49 4e 43 52 45 4d 45 4e  e, "AUTOINCREMEN
1eb88 54 20 69 73 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65  T is only allowe
1eb89 64 20 6f 6e 20 61 6e 20 22 0a 20 20 20 20 20 20  d on an ".      
1eb8a 20 22 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52   "INTEGER PRIMAR
1eb8b 59 20 4b 45 59 22 29 3b 0a 23 65 6e 64 69 66 0a  Y KEY");.#endif.
1eb8c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
1eb8d 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 28  ite3CreateIndex(
1eb8e 70 50 61 72 73 65 2c 20 30 2c 20 30 2c 20 30 2c  pParse, 0, 0, 0,
1eb8f 20 70 4c 69 73 74 2c 20 6f 6e 45 72 72 6f 72 2c   pList, onError,
1eb90 20 30 2c 20 30 2c 20 73 6f 72 74 4f 72 64 65 72   0, 0, sortOrder
1eb91 2c 20 30 29 3b 0a 20 20 20 20 70 4c 69 73 74 20  , 0);.    pList 
1eb92 3d 20 30 3b 0a 20 20 7d 0a 0a 70 72 69 6d 61 72  = 0;.  }..primar
1eb93 79 5f 6b 65 79 5f 65 78 69 74 3a 0a 20 20 73 71  y_key_exit:.  sq
1eb94 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
1eb95 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
1eb96 70 4c 69 73 74 29 3b 0a 20 20 72 65 74 75 72 6e  pList);.  return
1eb97 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61  ;.}../*.** Add a
1eb98 20 6e 65 77 20 43 48 45 43 4b 20 63 6f 6e 73 74   new CHECK const
1eb99 72 61 69 6e 74 20 74 6f 20 74 68 65 20 74 61 62  raint to the tab
1eb9a 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64  le currently und
1eb9b 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e  er construction.
1eb9c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1eb9d 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
1eb9e 64 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e  ddCheckConstrain
1eb9f 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  t(.  Parse *pPar
1eba0 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e  se,    /* Parsin
1eba1 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45  g context */.  E
1eba2 78 70 72 20 2a 70 43 68 65 63 6b 45 78 70 72 20  xpr *pCheckExpr 
1eba3 20 2f 2a 20 54 68 65 20 63 68 65 63 6b 20 65 78   /* The check ex
1eba4 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20  pression */.){. 
1eba5 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
1eba6 50 61 72 73 65 2d 3e 64 62 3b 0a 23 69 66 6e 64  Parse->db;.#ifnd
1eba7 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
1eba8 48 45 43 4b 0a 20 20 54 61 62 6c 65 20 2a 70 54  HECK.  Table *pT
1eba9 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65  ab = pParse->pNe
1ebaa 77 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 70 54  wTable;.  if( pT
1ebab 61 62 20 26 26 20 21 49 4e 5f 44 45 43 4c 41 52  ab && !IN_DECLAR
1ebac 45 5f 56 54 41 42 20 29 7b 0a 20 20 20 20 2f 2a  E_VTAB ){.    /*
1ebad 20 54 68 65 20 43 48 45 43 4b 20 65 78 70 72 65   The CHECK expre
1ebae 73 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 75  ssion must be du
1ebaf 70 6c 69 63 61 74 65 64 20 73 6f 20 74 68 61 74  plicated so that
1ebb0 20 74 6f 6b 65 6e 73 20 72 65 66 65 72 0a 20 20   tokens refer.  
1ebb1 20 20 2a 2a 20 74 6f 20 6d 61 6c 6c 6f 63 65 64    ** to malloced
1ebb2 20 73 70 61 63 65 20 61 6e 64 20 6e 6f 74 20 74   space and not t
1ebb3 68 65 20 28 65 70 68 65 6d 65 72 61 6c 29 20 74  he (ephemeral) t
1ebb4 65 78 74 20 6f 66 20 74 68 65 20 43 52 45 41 54  ext of the CREAT
1ebb5 45 20 54 41 42 4c 45 0a 20 20 20 20 2a 2a 20 73  E TABLE.    ** s
1ebb6 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20  tatement */.    
1ebb7 70 54 61 62 2d 3e 70 43 68 65 63 6b 20 3d 20 73  pTab->pCheck = s
1ebb8 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 64 62  qlite3ExprAnd(db
1ebb9 2c 20 70 54 61 62 2d 3e 70 43 68 65 63 6b 2c 20  , pTab->pCheck, 
1ebba 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1ebbb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ebbc 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 75     sqlite3ExprDu
1ebbd 70 28 64 62 2c 20 70 43 68 65 63 6b 45 78 70 72  p(db, pCheckExpr
1ebbe 29 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ));.  }.#endif. 
1ebbf 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
1ebc0 74 65 28 64 62 2c 20 70 43 68 65 63 6b 45 78 70  te(db, pCheckExp
1ebc1 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  r);.}../*.** Set
1ebc2 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66   the collation f
1ebc3 75 6e 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d  unction of the m
1ebc4 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 70 61 72  ost recently par
1ebc5 73 65 64 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e  sed table column
1ebc6 0a 2a 2a 20 74 6f 20 74 68 65 20 43 6f 6c 6c 53  .** to the CollS
1ebc7 65 71 20 67 69 76 65 6e 2e 0a 2a 2f 0a 53 51 4c  eq given..*/.SQL
1ebc8 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1ebc9 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61   sqlite3AddColla
1ebca 74 65 54 79 70 65 28 50 61 72 73 65 20 2a 70 50  teType(Parse *pP
1ebcb 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f  arse, Token *pTo
1ebcc 6b 65 6e 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  ken){.  Table *p
1ebcd 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 68 61  ;.  int i;.  cha
1ebce 72 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 20 20  r *zColl;       
1ebcf 20 20 20 20 20 20 20 2f 2a 20 44 65 71 75 6f 74         /* Dequot
1ebd0 65 64 20 6e 61 6d 65 20 6f 66 20 63 6f 6c 6c 61  ed name of colla
1ebd1 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 2a 2f  tion sequence */
1ebd2 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a  .  sqlite3 *db;.
1ebd3 0a 20 20 69 66 28 20 28 70 20 3d 20 70 50 61 72  .  if( (p = pPar
1ebd4 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d  se->pNewTable)==
1ebd5 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 20  0 ) return;.  i 
1ebd6 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 64  = p->nCol-1;.  d
1ebd7 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
1ebd8 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    zColl = sqlite
1ebd9 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
1ebda 62 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 69 66  b, pToken);.  if
1ebdb 28 20 21 7a 43 6f 6c 6c 20 29 20 72 65 74 75 72  ( !zColl ) retur
1ebdc 6e 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65  n;..  if( sqlite
1ebdd 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 70  3LocateCollSeq(p
1ebde 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 2c 20 2d 31  Parse, zColl, -1
1ebdf 29 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a  ) ){.    Index *
1ebe0 70 49 64 78 3b 0a 20 20 20 20 70 2d 3e 61 43 6f  pIdx;.    p->aCo
1ebe1 6c 5b 69 5d 2e 7a 43 6f 6c 6c 20 3d 20 7a 43 6f  l[i].zColl = zCo
1ebe2 6c 6c 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66  ll;.  .    /* If
1ebe3 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 64   the column is d
1ebe4 65 63 6c 61 72 65 64 20 61 73 20 22 3c 6e 61 6d  eclared as "<nam
1ebe5 65 3e 20 50 52 49 4d 41 52 59 20 4b 45 59 20 43  e> PRIMARY KEY C
1ebe6 4f 4c 4c 41 54 45 20 3c 74 79 70 65 3e 22 2c 0a  OLLATE <type>",.
1ebe7 20 20 20 20 2a 2a 20 74 68 65 6e 20 61 6e 20 69      ** then an i
1ebe8 6e 64 65 78 20 6d 61 79 20 68 61 76 65 20 62 65  ndex may have be
1ebe9 65 6e 20 63 72 65 61 74 65 64 20 6f 6e 20 74 68  en created on th
1ebea 69 73 20 63 6f 6c 75 6d 6e 20 62 65 66 6f 72 65  is column before
1ebeb 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c 6c   the.    ** coll
1ebec 61 74 69 6f 6e 20 74 79 70 65 20 77 61 73 20 61  ation type was a
1ebed 64 64 65 64 2e 20 43 6f 72 72 65 63 74 20 74 68  dded. Correct th
1ebee 69 73 20 69 66 20 69 74 20 69 73 20 74 68 65 20  is if it is the 
1ebef 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  case..    */.   
1ebf0 20 66 6f 72 28 70 49 64 78 3d 70 2d 3e 70 49 6e   for(pIdx=p->pIn
1ebf1 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
1ebf2 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
1ebf3 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78      assert( pIdx
1ebf4 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a  ->nColumn==1 );.
1ebf5 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e        if( pIdx->
1ebf6 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 69 20 29  aiColumn[0]==i )
1ebf7 7b 0a 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e  {.        pIdx->
1ebf8 61 7a 43 6f 6c 6c 5b 30 5d 20 3d 20 70 2d 3e 61  azColl[0] = p->a
1ebf9 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20  Col[i].zColl;.  
1ebfa 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65      }.    }.  }e
1ebfb 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
1ebfc 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c  DbFree(db, zColl
1ebfd 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
1ebfe 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
1ebff 74 75 72 6e 73 20 74 68 65 20 63 6f 6c 6c 61 74  turns the collat
1ec00 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 6f 72  ion sequence for
1ec01 20 64 61 74 61 62 61 73 65 20 6e 61 74 69 76 65   database native
1ec02 20 74 65 78 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e   text.** encodin
1ec03 67 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20  g identified by 
1ec04 74 68 65 20 73 74 72 69 6e 67 20 7a 4e 61 6d 65  the string zName
1ec05 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a  , length nName..
1ec06 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 71  **.** If the req
1ec07 75 65 73 74 65 64 20 63 6f 6c 6c 61 74 69 6f 6e  uested collation
1ec08 20 73 65 71 75 65 6e 63 65 20 69 73 20 6e 6f 74   sequence is not
1ec09 20 61 76 61 69 6c 61 62 6c 65 2c 20 6f 72 20 6e   available, or n
1ec0a 6f 74 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20  ot available.** 
1ec0b 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
1ec0c 6e 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 2c  native encoding,
1ec0d 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66   the collation f
1ec0e 61 63 74 6f 72 79 20 69 73 20 69 6e 76 6f 6b 65  actory is invoke
1ec0f 64 20 74 6f 0a 2a 2a 20 72 65 71 75 65 73 74 20  d to.** request 
1ec10 69 74 2e 20 49 66 20 74 68 65 20 63 6f 6c 6c 61  it. If the colla
1ec11 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 64 6f 65  tion factory doe
1ec12 73 20 6e 6f 74 20 73 75 70 70 6c 79 20 73 75 63  s not supply suc
1ec13 68 20 61 20 73 65 71 75 65 6e 63 65 2c 0a 2a 2a  h a sequence,.**
1ec14 20 61 6e 64 20 74 68 65 20 73 65 71 75 65 6e 63   and the sequenc
1ec15 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 69  e is available i
1ec16 6e 20 61 6e 6f 74 68 65 72 20 74 65 78 74 20 65  n another text e
1ec17 6e 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20 74 68  ncoding, then th
1ec18 61 74 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65  at is.** returne
1ec19 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a  d instead..**.**
1ec1a 20 49 66 20 6e 6f 20 76 65 72 73 69 6f 6e 73 20   If no versions 
1ec1b 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64  of the requested
1ec1c 20 63 6f 6c 6c 61 74 69 6f 6e 73 20 73 65 71 75   collations sequ
1ec1d 65 6e 63 65 20 61 72 65 20 61 76 61 69 6c 61 62  ence are availab
1ec1e 6c 65 2c 20 6f 72 0a 2a 2a 20 61 6e 6f 74 68 65  le, or.** anothe
1ec1f 72 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  r error occurs, 
1ec20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64  NULL is returned
1ec21 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65   and an error me
1ec22 73 73 61 67 65 20 77 72 69 74 74 65 6e 20 69 6e  ssage written in
1ec23 74 6f 0a 2a 2a 20 70 50 61 72 73 65 2e 0a 2a 2a  to.** pParse..**
1ec24 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1ec25 20 69 73 20 61 20 77 72 61 70 70 65 72 20 61 72   is a wrapper ar
1ec26 6f 75 6e 64 20 73 71 6c 69 74 65 33 46 69 6e 64  ound sqlite3Find
1ec27 43 6f 6c 6c 53 65 71 28 29 2e 20 20 54 68 69 73  CollSeq().  This
1ec28 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 6e 76 6f   routine.** invo
1ec29 6b 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f  kes the collatio
1ec2a 6e 20 66 61 63 74 6f 72 79 20 69 66 20 74 68 65  n factory if the
1ec2b 20 6e 61 6d 65 64 20 63 6f 6c 6c 61 74 69 6f 6e   named collation
1ec2c 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64   cannot be found
1ec2d 0a 2a 2a 20 61 6e 64 20 67 65 6e 65 72 61 74 65  .** and generate
1ec2e 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  s an error messa
1ec2f 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ge..*/.SQLITE_PR
1ec30 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73  IVATE CollSeq *s
1ec31 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c  qlite3LocateColl
1ec32 53 65 71 28 50 61 72 73 65 20 2a 70 50 61 72 73  Seq(Parse *pPars
1ec33 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  e, const char *z
1ec34 4e 61 6d 65 2c 20 69 6e 74 20 6e 4e 61 6d 65 29  Name, int nName)
1ec35 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
1ec36 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
1ec37 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 64 62 29  u8 enc = ENC(db)
1ec38 3b 0a 20 20 75 38 20 69 6e 69 74 62 75 73 79 20  ;.  u8 initbusy 
1ec39 3d 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3b  = db->init.busy;
1ec3a 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  .  CollSeq *pCol
1ec3b 6c 3b 0a 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71  l;..  pColl = sq
1ec3c 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
1ec3d 28 64 62 2c 20 65 6e 63 2c 20 7a 4e 61 6d 65 2c  (db, enc, zName,
1ec3e 20 6e 4e 61 6d 65 2c 20 69 6e 69 74 62 75 73 79   nName, initbusy
1ec3f 29 3b 0a 20 20 69 66 28 20 21 69 6e 69 74 62 75  );.  if( !initbu
1ec40 73 79 20 26 26 20 28 21 70 43 6f 6c 6c 20 7c 7c  sy && (!pColl ||
1ec41 20 21 70 43 6f 6c 6c 2d 3e 78 43 6d 70 29 20 29   !pColl->xCmp) )
1ec42 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71  {.    pColl = sq
1ec43 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71 28  lite3GetCollSeq(
1ec44 64 62 2c 20 70 43 6f 6c 6c 2c 20 7a 4e 61 6d 65  db, pColl, zName
1ec45 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66  , nName);.    if
1ec46 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  ( !pColl ){.    
1ec47 20 20 69 66 28 20 6e 4e 61 6d 65 3c 30 20 29 7b    if( nName<0 ){
1ec48 0a 20 20 20 20 20 20 20 20 6e 4e 61 6d 65 20 3d  .        nName =
1ec49 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 28 64   sqlite3Strlen(d
1ec4a 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  b, zName);.     
1ec4b 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
1ec4c 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
1ec4d 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 6c 61 74   "no such collat
1ec4e 69 6f 6e 20 73 65 71 75 65 6e 63 65 3a 20 25 2e  ion sequence: %.
1ec4f 2a 73 22 2c 20 6e 4e 61 6d 65 2c 20 7a 4e 61 6d  *s", nName, zNam
1ec50 65 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 20  e);.      pColl 
1ec51 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  = 0;.    }.  }..
1ec52 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a    return pColl;.
1ec53 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  }.../*.** Genera
1ec54 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c  te code that wil
1ec55 6c 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20  l increment the 
1ec56 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 0a 2a  schema cookie..*
1ec57 2a 0a 2a 2a 20 54 68 65 20 73 63 68 65 6d 61 20  *.** The schema 
1ec58 63 6f 6f 6b 69 65 20 69 73 20 75 73 65 64 20 74  cookie is used t
1ec59 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 6e  o determine when
1ec5a 20 74 68 65 20 73 63 68 65 6d 61 20 66 6f 72 20   the schema for 
1ec5b 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
1ec5c 63 68 61 6e 67 65 73 2e 20 20 41 66 74 65 72 20  changes.  After 
1ec5d 65 61 63 68 20 73 63 68 65 6d 61 20 63 68 61 6e  each schema chan
1ec5e 67 65 2c 20 74 68 65 20 63 6f 6f 6b 69 65 20 76  ge, the cookie v
1ec5f 61 6c 75 65 0a 2a 2a 20 63 68 61 6e 67 65 73 2e  alue.** changes.
1ec60 20 20 57 68 65 6e 20 61 20 70 72 6f 63 65 73 73    When a process
1ec61 20 66 69 72 73 74 20 72 65 61 64 73 20 74 68 65   first reads the
1ec62 20 73 63 68 65 6d 61 20 69 74 20 72 65 63 6f 72   schema it recor
1ec63 64 73 20 74 68 65 0a 2a 2a 20 63 6f 6f 6b 69 65  ds the.** cookie
1ec64 2e 20 20 54 68 65 72 65 61 66 74 65 72 2c 20 77  .  Thereafter, w
1ec65 68 65 6e 65 76 65 72 20 69 74 20 67 6f 65 73 20  henever it goes 
1ec66 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 64 61  to access the da
1ec67 74 61 62 61 73 65 2c 0a 2a 2a 20 69 74 20 63 68  tabase,.** it ch
1ec68 65 63 6b 73 20 74 68 65 20 63 6f 6f 6b 69 65 20  ecks the cookie 
1ec69 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65  to make sure the
1ec6a 20 73 63 68 65 6d 61 20 68 61 73 20 6e 6f 74 20   schema has not 
1ec6b 63 68 61 6e 67 65 64 0a 2a 2a 20 73 69 6e 63 65  changed.** since
1ec6c 20 69 74 20 77 61 73 20 6c 61 73 74 20 72 65 61   it was last rea
1ec6d 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 70 6c  d..**.** This pl
1ec6e 61 6e 20 69 73 20 6e 6f 74 20 63 6f 6d 70 6c 65  an is not comple
1ec6f 74 65 6c 79 20 62 75 6c 6c 65 74 2d 70 72 6f 6f  tely bullet-proo
1ec70 66 2e 20 20 49 74 20 69 73 20 70 6f 73 73 69 62  f.  It is possib
1ec71 6c 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 63  le for.** the sc
1ec72 68 65 6d 61 20 74 6f 20 63 68 61 6e 67 65 20 6d  hema to change m
1ec73 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 61 6e  ultiple times an
1ec74 64 20 66 6f 72 20 74 68 65 20 63 6f 6f 6b 69 65  d for the cookie
1ec75 20 74 6f 20 62 65 0a 2a 2a 20 73 65 74 20 62 61   to be.** set ba
1ec76 63 6b 20 74 6f 20 70 72 69 6f 72 20 76 61 6c 75  ck to prior valu
1ec77 65 2e 20 20 42 75 74 20 73 63 68 65 6d 61 20 63  e.  But schema c
1ec78 68 61 6e 67 65 73 20 61 72 65 20 69 6e 66 72 65  hanges are infre
1ec79 71 75 65 6e 74 0a 2a 2a 20 61 6e 64 20 74 68 65  quent.** and the
1ec7a 20 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20   probability of 
1ec7b 68 69 74 74 69 6e 67 20 74 68 65 20 73 61 6d 65  hitting the same
1ec7c 20 63 6f 6f 6b 69 65 20 76 61 6c 75 65 20 69 73   cookie value is
1ec7d 20 6f 6e 6c 79 0a 2a 2a 20 31 20 63 68 61 6e 63   only.** 1 chanc
1ec7e 65 20 69 6e 20 32 5e 33 32 2e 20 20 53 6f 20 77  e in 2^32.  So w
1ec7f 65 27 72 65 20 73 61 66 65 20 65 6e 6f 75 67 68  e're safe enough
1ec80 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1ec81 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1ec82 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 50 61 72  ChangeCookie(Par
1ec83 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
1ec84 69 44 62 29 7b 0a 20 20 69 6e 74 20 72 31 20 3d  iDb){.  int r1 =
1ec85 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
1ec86 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71  eg(pParse);.  sq
1ec87 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
1ec88 73 65 2d 3e 64 62 3b 0a 20 20 56 64 62 65 20 2a  se->db;.  Vdbe *
1ec89 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
1ec8a 65 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  e;.  sqlite3Vdbe
1ec8b 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
1ec8c 65 67 65 72 2c 20 64 62 2d 3e 61 44 62 5b 69 44  eger, db->aDb[iD
1ec8d 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65  b].pSchema->sche
1ec8e 6d 61 5f 63 6f 6f 6b 69 65 2b 31 2c 20 72 31 29  ma_cookie+1, r1)
1ec8f 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
1ec90 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43  ddOp3(v, OP_SetC
1ec91 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 30 2c 20 72  ookie, iDb, 0, r
1ec92 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c  1);.  sqlite3Rel
1ec93 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
1ec94 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  se, r1);.}../*.*
1ec95 2a 20 4d 65 61 73 75 72 65 20 74 68 65 20 6e 75  * Measure the nu
1ec96 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65  mber of characte
1ec97 72 73 20 6e 65 65 64 65 64 20 74 6f 20 6f 75 74  rs needed to out
1ec98 70 75 74 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a  put the given.**
1ec99 20 69 64 65 6e 74 69 66 69 65 72 2e 20 20 54 68   identifier.  Th
1ec9a 65 20 6e 75 6d 62 65 72 20 72 65 74 75 72 6e 65  e number returne
1ec9b 64 20 69 6e 63 6c 75 64 65 73 20 61 6e 79 20 71  d includes any q
1ec9c 75 6f 74 65 73 20 75 73 65 64 0a 2a 2a 20 62 75  uotes used.** bu
1ec9d 74 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75  t does not inclu
1ec9e 64 65 20 74 68 65 20 6e 75 6c 6c 20 74 65 72 6d  de the null term
1ec9f 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  inator..**.** Th
1eca0 65 20 65 73 74 69 6d 61 74 65 20 69 73 20 63 6f  e estimate is co
1eca1 6e 73 65 72 76 61 74 69 76 65 2e 20 20 49 74 20  nservative.  It 
1eca2 6d 69 67 68 74 20 62 65 20 6c 61 72 67 65 72 20  might be larger 
1eca3 74 68 61 74 20 77 68 61 74 20 69 73 0a 2a 2a 20  that what is.** 
1eca4 72 65 61 6c 6c 79 20 6e 65 65 64 65 64 2e 0a 2a  really needed..*
1eca5 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 64 65  /.static int ide
1eca6 6e 74 4c 65 6e 67 74 68 28 63 6f 6e 73 74 20 63  ntLength(const c
1eca7 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 6e  har *z){.  int n
1eca8 3b 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 2a 7a 3b  ;.  for(n=0; *z;
1eca9 20 6e 2b 2b 2c 20 7a 2b 2b 29 7b 0a 20 20 20 20   n++, z++){.    
1ecaa 69 66 28 20 2a 7a 3d 3d 27 22 27 20 29 7b 20 6e  if( *z=='"' ){ n
1ecab 2b 2b 3b 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75  ++; }.  }.  retu
1ecac 72 6e 20 6e 20 2b 20 32 3b 0a 7d 0a 0a 2f 2a 0a  rn n + 2;.}../*.
1ecad 2a 2a 20 57 72 69 74 65 20 61 6e 20 69 64 65 6e  ** Write an iden
1ecae 74 69 66 69 65 72 20 6f 6e 74 6f 20 74 68 65 20  tifier onto the 
1ecaf 65 6e 64 20 6f 66 20 74 68 65 20 67 69 76 65 6e  end of the given
1ecb0 20 73 74 72 69 6e 67 2e 20 20 41 64 64 0a 2a 2a   string.  Add.**
1ecb1 20 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72   quote character
1ecb2 73 20 61 73 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a  s as needed..*/.
1ecb3 73 74 61 74 69 63 20 76 6f 69 64 20 69 64 65 6e  static void iden
1ecb4 74 50 75 74 28 63 68 61 72 20 2a 7a 2c 20 69 6e  tPut(char *z, in
1ecb5 74 20 2a 70 49 64 78 2c 20 63 68 61 72 20 2a 7a  t *pIdx, char *z
1ecb6 53 69 67 6e 65 64 49 64 65 6e 74 29 7b 0a 20 20  SignedIdent){.  
1ecb7 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
1ecb8 49 64 65 6e 74 20 3d 20 28 75 6e 73 69 67 6e 65  Ident = (unsigne
1ecb9 64 20 63 68 61 72 2a 29 7a 53 69 67 6e 65 64 49  d char*)zSignedI
1ecba 64 65 6e 74 3b 0a 20 20 69 6e 74 20 69 2c 20 6a  dent;.  int i, j
1ecbb 2c 20 6e 65 65 64 51 75 6f 74 65 3b 0a 20 20 69  , needQuote;.  i
1ecbc 20 3d 20 2a 70 49 64 78 3b 0a 20 20 66 6f 72 28   = *pIdx;.  for(
1ecbd 6a 3d 30 3b 20 7a 49 64 65 6e 74 5b 6a 5d 3b 20  j=0; zIdent[j]; 
1ecbe 6a 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 21 69  j++){.    if( !i
1ecbf 73 61 6c 6e 75 6d 28 7a 49 64 65 6e 74 5b 6a 5d  salnum(zIdent[j]
1ecc0 29 20 26 26 20 7a 49 64 65 6e 74 5b 6a 5d 21 3d  ) && zIdent[j]!=
1ecc1 27 5f 27 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d  '_' ) break;.  }
1ecc2 0a 20 20 6e 65 65 64 51 75 6f 74 65 20 3d 20 20  .  needQuote =  
1ecc3 7a 49 64 65 6e 74 5b 6a 5d 21 3d 30 20 7c 7c 20  zIdent[j]!=0 || 
1ecc4 69 73 64 69 67 69 74 28 7a 49 64 65 6e 74 5b 30  isdigit(zIdent[0
1ecc5 5d 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ]).             
1ecc6 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 4b       || sqlite3K
1ecc7 65 79 77 6f 72 64 43 6f 64 65 28 7a 49 64 65 6e  eywordCode(zIden
1ecc8 74 2c 20 6a 29 21 3d 54 4b 5f 49 44 3b 0a 20 20  t, j)!=TK_ID;.  
1ecc9 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20  if( needQuote ) 
1ecca 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b 0a 20 20  z[i++] = '"';.  
1eccb 66 6f 72 28 6a 3d 30 3b 20 7a 49 64 65 6e 74 5b  for(j=0; zIdent[
1eccc 6a 5d 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 7a 5b  j]; j++){.    z[
1eccd 69 2b 2b 5d 20 3d 20 7a 49 64 65 6e 74 5b 6a 5d  i++] = zIdent[j]
1ecce 3b 0a 20 20 20 20 69 66 28 20 7a 49 64 65 6e 74  ;.    if( zIdent
1eccf 5b 6a 5d 3d 3d 27 22 27 20 29 20 7a 5b 69 2b 2b  [j]=='"' ) z[i++
1ecd0 5d 20 3d 20 27 22 27 3b 0a 20 20 7d 0a 20 20 69  ] = '"';.  }.  i
1ecd1 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20 7a  f( needQuote ) z
1ecd2 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b 0a 20 20 7a  [i++] = '"';.  z
1ecd3 5b 69 5d 20 3d 20 30 3b 0a 20 20 2a 70 49 64 78  [i] = 0;.  *pIdx
1ecd4 20 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47   = i;.}../*.** G
1ecd5 65 6e 65 72 61 74 65 20 61 20 43 52 45 41 54 45  enerate a CREATE
1ecd6 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
1ecd7 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72   appropriate for
1ecd8 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 74 61   the given.** ta
1ecd9 62 6c 65 2e 20 20 4d 65 6d 6f 72 79 20 74 6f 20  ble.  Memory to 
1ecda 68 6f 6c 64 20 74 68 65 20 74 65 78 74 20 6f 66  hold the text of
1ecdb 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69   the statement i
1ecdc 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72  s obtained.** fr
1ecdd 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28  om sqliteMalloc(
1ecde 29 20 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72  ) and must be fr
1ecdf 65 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69  eed by the calli
1ece0 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a  ng function..*/.
1ece1 73 74 61 74 69 63 20 63 68 61 72 20 2a 63 72 65  static char *cre
1ece2 61 74 65 54 61 62 6c 65 53 74 6d 74 28 73 71 6c  ateTableStmt(sql
1ece3 69 74 65 33 20 2a 64 62 2c 20 54 61 62 6c 65 20  ite3 *db, Table 
1ece4 2a 70 2c 20 69 6e 74 20 69 73 54 65 6d 70 29 7b  *p, int isTemp){
1ece5 0a 20 20 69 6e 74 20 69 2c 20 6b 2c 20 6e 3b 0a  .  int i, k, n;.
1ece6 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 0a 20    char *zStmt;. 
1ece7 20 63 68 61 72 20 2a 7a 53 65 70 2c 20 2a 7a 53   char *zSep, *zS
1ece8 65 70 32 2c 20 2a 7a 45 6e 64 2c 20 2a 7a 3b 0a  ep2, *zEnd, *z;.
1ece9 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a    Column *pCol;.
1ecea 20 20 6e 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70    n = 0;.  for(p
1eceb 43 6f 6c 20 3d 20 70 2d 3e 61 43 6f 6c 2c 20 69  Col = p->aCol, i
1ecec 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69  =0; i<p->nCol; i
1eced 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20  ++, pCol++){.   
1ecee 20 6e 20 2b 3d 20 69 64 65 6e 74 4c 65 6e 67 74   n += identLengt
1ecef 68 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a  h(pCol->zName);.
1ecf0 20 20 20 20 7a 20 3d 20 70 43 6f 6c 2d 3e 7a 54      z = pCol->zT
1ecf1 79 70 65 3b 0a 20 20 20 20 69 66 28 20 7a 20 29  ype;.    if( z )
1ecf2 7b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 28 73 74  {.      n += (st
1ecf3 72 6c 65 6e 28 7a 29 20 2b 20 31 29 3b 0a 20 20  rlen(z) + 1);.  
1ecf4 20 20 7d 0a 20 20 7d 0a 20 20 6e 20 2b 3d 20 69    }.  }.  n += i
1ecf5 64 65 6e 74 4c 65 6e 67 74 68 28 70 2d 3e 7a 4e  dentLength(p->zN
1ecf6 61 6d 65 29 3b 0a 20 20 69 66 28 20 6e 3c 35 30  ame);.  if( n<50
1ecf7 20 29 7b 0a 20 20 20 20 7a 53 65 70 20 3d 20 22   ){.    zSep = "
1ecf8 22 3b 0a 20 20 20 20 7a 53 65 70 32 20 3d 20 22  ";.    zSep2 = "
1ecf9 2c 22 3b 0a 20 20 20 20 7a 45 6e 64 20 3d 20 22  ,";.    zEnd = "
1ecfa 29 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  )";.  }else{.   
1ecfb 20 7a 53 65 70 20 3d 20 22 5c 6e 20 20 22 3b 0a   zSep = "\n  ";.
1ecfc 20 20 20 20 7a 53 65 70 32 20 3d 20 22 2c 5c 6e      zSep2 = ",\n
1ecfd 20 20 22 3b 0a 20 20 20 20 7a 45 6e 64 20 3d 20    ";.    zEnd = 
1ecfe 22 5c 6e 29 22 3b 0a 20 20 7d 0a 20 20 6e 20 2b  "\n)";.  }.  n +
1ecff 3d 20 33 35 20 2b 20 36 2a 70 2d 3e 6e 43 6f 6c  = 35 + 6*p->nCol
1ed00 3b 0a 20 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69  ;.  zStmt = sqli
1ed01 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a  te3Malloc( n );.
1ed02 20 20 69 66 28 20 7a 53 74 6d 74 3d 3d 30 20 29    if( zStmt==0 )
1ed03 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63  {.    db->malloc
1ed04 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
1ed05 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
1ed06 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1ed07 28 6e 2c 20 7a 53 74 6d 74 2c 0a 20 20 20 20 20  (n, zStmt,.     
1ed08 20 20 20 20 20 20 20 20 20 20 20 20 20 21 4f 4d               !OM
1ed09 49 54 5f 54 45 4d 50 44 42 26 26 69 73 54 65 6d  IT_TEMPDB&&isTem
1ed0a 70 20 3f 20 22 43 52 45 41 54 45 20 54 45 4d 50  p ? "CREATE TEMP
1ed0b 20 54 41 42 4c 45 20 22 3a 22 43 52 45 41 54 45   TABLE ":"CREATE
1ed0c 20 54 41 42 4c 45 20 22 29 3b 0a 20 20 6b 20 3d   TABLE ");.  k =
1ed0d 20 73 74 72 6c 65 6e 28 7a 53 74 6d 74 29 3b 0a   strlen(zStmt);.
1ed0e 20 20 69 64 65 6e 74 50 75 74 28 7a 53 74 6d 74    identPut(zStmt
1ed0f 2c 20 26 6b 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3b  , &k, p->zName);
1ed10 0a 20 20 7a 53 74 6d 74 5b 6b 2b 2b 5d 20 3d 20  .  zStmt[k++] = 
1ed11 27 28 27 3b 0a 20 20 66 6f 72 28 70 43 6f 6c 3d  '(';.  for(pCol=
1ed12 70 2d 3e 61 43 6f 6c 2c 20 69 3d 30 3b 20 69 3c  p->aCol, i=0; i<
1ed13 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43  p->nCol; i++, pC
1ed14 6f 6c 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74  ol++){.    sqlit
1ed15 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 2d 6b 2c  e3_snprintf(n-k,
1ed16 20 26 7a 53 74 6d 74 5b 6b 5d 2c 20 7a 53 65 70   &zStmt[k], zSep
1ed17 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 73 74 72 6c  );.    k += strl
1ed18 65 6e 28 26 7a 53 74 6d 74 5b 6b 5d 29 3b 0a 20  en(&zStmt[k]);. 
1ed19 20 20 20 7a 53 65 70 20 3d 20 7a 53 65 70 32 3b     zSep = zSep2;
1ed1a 0a 20 20 20 20 69 64 65 6e 74 50 75 74 28 7a 53  .    identPut(zS
1ed1b 74 6d 74 2c 20 26 6b 2c 20 70 43 6f 6c 2d 3e 7a  tmt, &k, pCol->z
1ed1c 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 28  Name);.    if( (
1ed1d 7a 20 3d 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 29  z = pCol->zType)
1ed1e 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 7a 53 74  !=0 ){.      zSt
1ed1f 6d 74 5b 6b 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20  mt[k++] = ' ';. 
1ed20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 74 72       assert( str
1ed21 6c 65 6e 28 7a 29 2b 6b 2b 31 3c 3d 6e 20 29 3b  len(z)+k+1<=n );
1ed22 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
1ed23 6e 70 72 69 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53  nprintf(n-k, &zS
1ed24 74 6d 74 5b 6b 5d 2c 20 22 25 73 22 2c 20 7a 29  tmt[k], "%s", z)
1ed25 3b 0a 20 20 20 20 20 20 6b 20 2b 3d 20 73 74 72  ;.      k += str
1ed26 6c 65 6e 28 7a 29 3b 0a 20 20 20 20 7d 0a 20 20  len(z);.    }.  
1ed27 7d 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72  }.  sqlite3_snpr
1ed28 69 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74  intf(n-k, &zStmt
1ed29 5b 6b 5d 2c 20 22 25 73 22 2c 20 7a 45 6e 64 29  [k], "%s", zEnd)
1ed2a 3b 0a 20 20 72 65 74 75 72 6e 20 7a 53 74 6d 74  ;.  return zStmt
1ed2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
1ed2c 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
1ed2d 64 20 74 6f 20 72 65 70 6f 72 74 20 74 68 65 20  d to report the 
1ed2e 66 69 6e 61 6c 20 22 29 22 20 74 68 61 74 20 74  final ")" that t
1ed2f 65 72 6d 69 6e 61 74 65 73 0a 2a 2a 20 61 20 43  erminates.** a C
1ed30 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
1ed31 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ement..**.** The
1ed32 20 74 61 62 6c 65 20 73 74 72 75 63 74 75 72 65   table structure
1ed33 20 74 68 61 74 20 6f 74 68 65 72 20 61 63 74 69   that other acti
1ed34 6f 6e 20 72 6f 75 74 69 6e 65 73 20 68 61 76 65  on routines have
1ed35 20 62 65 65 6e 20 62 75 69 6c 64 69 6e 67 0a 2a   been building.*
1ed36 2a 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68  * is added to th
1ed37 65 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 20  e internal hash 
1ed38 74 61 62 6c 65 73 2c 20 61 73 73 75 6d 69 6e 67  tables, assuming
1ed39 20 6e 6f 20 65 72 72 6f 72 73 20 68 61 76 65 0a   no errors have.
1ed3a 2a 2a 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 0a  ** occurred..**.
1ed3b 2a 2a 20 41 6e 20 65 6e 74 72 79 20 66 6f 72 20  ** An entry for 
1ed3c 74 68 65 20 74 61 62 6c 65 20 69 73 20 6d 61 64  the table is mad
1ed3d 65 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72 20  e in the master 
1ed3e 74 61 62 6c 65 20 6f 6e 20 64 69 73 6b 2c 20 75  table on disk, u
1ed3f 6e 6c 65 73 73 0a 2a 2a 20 74 68 69 73 20 69 73  nless.** this is
1ed40 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62   a temporary tab
1ed41 6c 65 20 6f 72 20 64 62 2d 3e 69 6e 69 74 2e 62  le or db->init.b
1ed42 75 73 79 3d 3d 31 2e 20 20 57 68 65 6e 20 64 62  usy==1.  When db
1ed43 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 31 0a 2a  ->init.busy==1.*
1ed44 2a 20 69 74 20 6d 65 61 6e 73 20 77 65 20 61 72  * it means we ar
1ed45 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 73 71  e reading the sq
1ed46 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c  lite_master tabl
1ed47 65 20 62 65 63 61 75 73 65 20 77 65 20 6a 75 73  e because we jus
1ed48 74 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 74  t.** connected t
1ed49 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f  o the database o
1ed4a 72 20 62 65 63 61 75 73 65 20 74 68 65 20 73 71  r because the sq
1ed4b 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c  lite_master tabl
1ed4c 65 20 68 61 73 0a 2a 2a 20 72 65 63 65 6e 74 6c  e has.** recentl
1ed4d 79 20 63 68 61 6e 67 65 64 2c 20 73 6f 20 74 68  y changed, so th
1ed4e 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68 69 73  e entry for this
1ed4f 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65   table already e
1ed50 78 69 73 74 73 20 69 6e 0a 2a 2a 20 74 68 65 20  xists in.** the 
1ed51 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
1ed52 62 6c 65 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20  ble.  We do not 
1ed53 77 61 6e 74 20 74 6f 20 63 72 65 61 74 65 20 69  want to create i
1ed54 74 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 49  t again..**.** I
1ed55 66 20 74 68 65 20 70 53 65 6c 65 63 74 20 61 72  f the pSelect ar
1ed56 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20 4e 55  gument is not NU
1ed57 4c 4c 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61  LL, it means tha
1ed58 74 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  t this routine.*
1ed59 2a 20 77 61 73 20 63 61 6c 6c 65 64 20 74 6f 20  * was called to 
1ed5a 63 72 65 61 74 65 20 61 20 74 61 62 6c 65 20 67  create a table g
1ed5b 65 6e 65 72 61 74 65 64 20 66 72 6f 6d 20 61 20  enerated from a 
1ed5c 0a 2a 2a 20 22 43 52 45 41 54 45 20 54 41 42 4c  .** "CREATE TABL
1ed5d 45 20 2e 2e 2e 20 41 53 20 53 45 4c 45 43 54 20  E ... AS SELECT 
1ed5e 2e 2e 2e 22 20 73 74 61 74 65 6d 65 6e 74 2e 20  ..." statement. 
1ed5f 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   The column name
1ed60 73 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20  s of.** the new 
1ed61 74 61 62 6c 65 20 77 69 6c 6c 20 6d 61 74 63 68  table will match
1ed62 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
1ed63 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2e 0a 2a  of the SELECT..*
1ed64 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1ed65 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e 64   void sqlite3End
1ed66 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a  Table(.  Parse *
1ed67 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
1ed68 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78   /* Parse contex
1ed69 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 43  t */.  Token *pC
1ed6a 6f 6e 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ons,           /
1ed6b 2a 20 54 68 65 20 27 2c 27 20 74 6f 6b 65 6e 20  * The ',' token 
1ed6c 61 66 74 65 72 20 74 68 65 20 6c 61 73 74 20 63  after the last c
1ed6d 6f 6c 75 6d 6e 20 64 65 66 6e 2e 20 2a 2f 0a 20  olumn defn. */. 
1ed6e 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20 20   Token *pEnd,   
1ed6f 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1ed70 66 69 6e 61 6c 20 27 29 27 20 74 6f 6b 65 6e 20  final ')' token 
1ed71 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 41  in the CREATE TA
1ed72 42 4c 45 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  BLE */.  Select 
1ed73 2a 70 53 65 6c 65 63 74 20 20 20 20 20 20 20 20  *pSelect        
1ed74 20 2f 2a 20 53 65 6c 65 63 74 20 66 72 6f 6d 20   /* Select from 
1ed75 61 20 22 43 52 45 41 54 45 20 2e 2e 2e 20 41 53  a "CREATE ... AS
1ed76 20 53 45 4c 45 43 54 22 20 2a 2f 0a 29 7b 0a 20   SELECT" */.){. 
1ed77 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 73 71 6c   Table *p;.  sql
1ed78 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
1ed79 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62  e->db;.  int iDb
1ed7a 3b 0a 0a 20 20 69 66 28 20 28 70 45 6e 64 3d 3d  ;..  if( (pEnd==
1ed7b 30 20 26 26 20 70 53 65 6c 65 63 74 3d 3d 30 29  0 && pSelect==0)
1ed7c 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72   || pParse->nErr
1ed7d 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
1ed7e 69 6c 65 64 20 29 20 7b 0a 20 20 20 20 72 65 74  iled ) {.    ret
1ed7f 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 70  urn;.  }.  p = p
1ed80 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
1ed81 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
1ed82 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74  eturn;..  assert
1ed83 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  ( !db->init.busy
1ed84 20 7c 7c 20 21 70 53 65 6c 65 63 74 20 29 3b 0a   || !pSelect );.
1ed85 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  .  iDb = sqlite3
1ed86 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62  SchemaToIndex(db
1ed87 2c 20 70 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a  , p->pSchema);..
1ed88 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1ed89 4d 49 54 5f 43 48 45 43 4b 0a 20 20 2f 2a 20 52  MIT_CHECK.  /* R
1ed8a 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20  esolve names in 
1ed8b 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72  all CHECK constr
1ed8c 61 69 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 73  aint expressions
1ed8d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  ..  */.  if( p->
1ed8e 70 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 53 72  pCheck ){.    Sr
1ed8f 63 4c 69 73 74 20 73 53 72 63 3b 20 20 20 20 20  cList sSrc;     
1ed90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ed91 20 46 61 6b 65 20 53 72 63 4c 69 73 74 20 66 6f   Fake SrcList fo
1ed92 72 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  r pParse->pNewTa
1ed93 62 6c 65 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43  ble */.    NameC
1ed94 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20  ontext sNC;     
1ed95 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
1ed96 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 70  me context for p
1ed97 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
1ed98 20 2a 2f 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28   */..    memset(
1ed99 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28  &sNC, 0, sizeof(
1ed9a 73 4e 43 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65  sNC));.    memse
1ed9b 74 28 26 73 53 72 63 2c 20 30 2c 20 73 69 7a 65  t(&sSrc, 0, size
1ed9c 6f 66 28 73 53 72 63 29 29 3b 0a 20 20 20 20 73  of(sSrc));.    s
1ed9d 53 72 63 2e 6e 53 72 63 20 3d 20 31 3b 0a 20 20  Src.nSrc = 1;.  
1ed9e 20 20 73 53 72 63 2e 61 5b 30 5d 2e 7a 4e 61 6d    sSrc.a[0].zNam
1ed9f 65 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b 0a 20 20  e = p->zName;.  
1eda0 20 20 73 53 72 63 2e 61 5b 30 5d 2e 70 54 61 62    sSrc.a[0].pTab
1eda1 20 3d 20 70 3b 0a 20 20 20 20 73 53 72 63 2e 61   = p;.    sSrc.a
1eda2 5b 30 5d 2e 69 43 75 72 73 6f 72 20 3d 20 2d 31  [0].iCursor = -1
1eda3 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65  ;.    sNC.pParse
1eda4 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 20 73   = pParse;.    s
1eda5 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 26 73  NC.pSrcList = &s
1eda6 53 72 63 3b 0a 20 20 20 20 73 4e 43 2e 69 73 43  Src;.    sNC.isC
1eda7 68 65 63 6b 20 3d 20 31 3b 0a 20 20 20 20 69 66  heck = 1;.    if
1eda8 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  ( sqlite3Resolve
1eda9 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20  ExprNames(&sNC, 
1edaa 70 2d 3e 70 43 68 65 63 6b 29 20 29 7b 0a 20 20  p->pCheck) ){.  
1edab 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
1edac 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  }.  }.#endif /* 
1edad 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
1edae 4f 4d 49 54 5f 43 48 45 43 4b 29 20 2a 2f 0a 0a  OMIT_CHECK) */..
1edaf 20 20 2f 2a 20 49 66 20 74 68 65 20 64 62 2d 3e    /* If the db->
1edb0 69 6e 69 74 2e 62 75 73 79 20 69 73 20 31 20 69  init.busy is 1 i
1edb1 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20 72  t means we are r
1edb2 65 61 64 69 6e 67 20 74 68 65 20 53 51 4c 20 6f  eading the SQL o
1edb3 66 66 20 74 68 65 0a 20 20 2a 2a 20 22 73 71 6c  ff the.  ** "sql
1edb4 69 74 65 5f 6d 61 73 74 65 72 22 20 6f 72 20 22  ite_master" or "
1edb5 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74  sqlite_temp_mast
1edb6 65 72 22 20 74 61 62 6c 65 20 6f 6e 20 74 68 65  er" table on the
1edb7 20 64 69 73 6b 2e 0a 20 20 2a 2a 20 53 6f 20 64   disk..  ** So d
1edb8 6f 20 6e 6f 74 20 77 72 69 74 65 20 74 6f 20 74  o not write to t
1edb9 68 65 20 64 69 73 6b 20 61 67 61 69 6e 2e 20 20  he disk again.  
1edba 45 78 74 72 61 63 74 20 74 68 65 20 72 6f 6f 74  Extract the root
1edbb 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a   page number.  *
1edbc 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20  * for the table 
1edbd 66 72 6f 6d 20 74 68 65 20 64 62 2d 3e 69 6e 69  from the db->ini
1edbe 74 2e 6e 65 77 54 6e 75 6d 20 66 69 65 6c 64 2e  t.newTnum field.
1edbf 20 20 28 54 68 65 20 70 61 67 65 20 6e 75 6d 62    (The page numb
1edc0 65 72 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 68  er.  ** should h
1edc1 61 76 65 20 62 65 65 6e 20 70 75 74 20 74 68 65  ave been put the
1edc2 72 65 20 62 79 20 74 68 65 20 73 71 6c 69 74 65  re by the sqlite
1edc3 4f 70 65 6e 43 62 20 72 6f 75 74 69 6e 65 2e 29  OpenCb routine.)
1edc4 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  .  */.  if( db->
1edc5 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20  init.busy ){.   
1edc6 20 70 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e 69   p->tnum = db->i
1edc7 6e 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20 7d  nit.newTnum;.  }
1edc8 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 74 20 69 6e  ..  /* If not in
1edc9 69 74 69 61 6c 69 7a 69 6e 67 2c 20 74 68 65 6e  itializing, then
1edca 20 63 72 65 61 74 65 20 61 20 72 65 63 6f 72 64   create a record
1edcb 20 66 6f 72 20 74 68 65 20 6e 65 77 20 74 61 62   for the new tab
1edcc 6c 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 53  le.  ** in the S
1edcd 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62  QLITE_MASTER tab
1edce 6c 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  le of the databa
1edcf 73 65 2e 20 20 54 68 65 20 72 65 63 6f 72 64 20  se.  The record 
1edd0 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 66 6f 72 20  number.  ** for 
1edd1 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 65 6e  the new table en
1edd2 74 72 79 20 73 68 6f 75 6c 64 20 61 6c 72 65 61  try should alrea
1edd3 64 79 20 62 65 20 6f 6e 20 74 68 65 20 73 74 61  dy be on the sta
1edd4 63 6b 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66  ck..  **.  ** If
1edd5 20 74 68 69 73 20 69 73 20 61 20 54 45 4d 50 4f   this is a TEMPO
1edd6 52 41 52 59 20 74 61 62 6c 65 2c 20 77 72 69 74  RARY table, writ
1edd7 65 20 74 68 65 20 65 6e 74 72 79 20 69 6e 74 6f  e the entry into
1edd8 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 0a 20   the auxiliary. 
1edd9 20 2a 2a 20 66 69 6c 65 20 69 6e 73 74 65 61 64   ** file instead
1edda 20 6f 66 20 69 6e 74 6f 20 74 68 65 20 6d 61 69   of into the mai
1eddb 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  n database file.
1eddc 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d  .  */.  if( !db-
1eddd 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20  >init.busy ){.  
1edde 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 56 64 62    int n;.    Vdb
1eddf 65 20 2a 76 3b 0a 20 20 20 20 63 68 61 72 20 2a  e *v;.    char *
1ede0 7a 54 79 70 65 3b 20 20 20 20 2f 2a 20 22 76 69  zType;    /* "vi
1ede1 65 77 22 20 6f 72 20 22 74 61 62 6c 65 22 20 2a  ew" or "table" *
1ede2 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 79 70  /.    char *zTyp
1ede3 65 32 3b 20 20 20 2f 2a 20 22 56 49 45 57 22 20  e2;   /* "VIEW" 
1ede4 6f 72 20 22 54 41 42 4c 45 22 20 2a 2f 0a 20 20  or "TABLE" */.  
1ede5 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 20 20    char *zStmt;  
1ede6 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65    /* Text of the
1ede7 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f 72   CREATE TABLE or
1ede8 20 43 52 45 41 54 45 20 56 49 45 57 20 73 74 61   CREATE VIEW sta
1ede9 74 65 6d 65 6e 74 20 2a 2f 0a 0a 20 20 20 20 76  tement */..    v
1edea 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
1edeb 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69  e(pParse);.    i
1edec 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( v==0 ) return
1eded 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ;..    sqlite3Vd
1edee 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43  beAddOp1(v, OP_C
1edef 6c 6f 73 65 2c 20 30 29 3b 0a 0a 20 20 20 20 2f  lose, 0);..    /
1edf0 2a 20 43 72 65 61 74 65 20 74 68 65 20 72 6f 6f  * Create the roo
1edf1 74 70 61 67 65 20 66 6f 72 20 74 68 65 20 6e 65  tpage for the ne
1edf2 77 20 74 61 62 6c 65 20 61 6e 64 20 70 75 73 68  w table and push
1edf3 20 69 74 20 6f 6e 74 6f 20 74 68 65 20 73 74 61   it onto the sta
1edf4 63 6b 2e 0a 20 20 20 20 2a 2a 20 41 20 76 69 65  ck..    ** A vie
1edf5 77 20 68 61 73 20 6e 6f 20 72 6f 6f 74 70 61 67  w has no rootpag
1edf6 65 2c 20 73 6f 20 6a 75 73 74 20 70 75 73 68 20  e, so just push 
1edf7 61 20 7a 65 72 6f 20 6f 6e 74 6f 20 74 68 65 20  a zero onto the 
1edf8 73 74 61 63 6b 20 66 6f 72 0a 20 20 20 20 2a 2a  stack for.    **
1edf9 20 76 69 65 77 73 2e 20 20 49 6e 69 74 69 61 6c   views.  Initial
1edfa 69 7a 65 20 7a 54 79 70 65 20 61 74 20 74 68 65  ize zType at the
1edfb 20 73 61 6d 65 20 74 69 6d 65 2e 0a 20 20 20 20   same time..    
1edfc 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 53  */.    if( p->pS
1edfd 65 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20  elect==0 ){.    
1edfe 20 20 2f 2a 20 41 20 72 65 67 75 6c 61 72 20 74    /* A regular t
1edff 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 7a 54  able */.      zT
1ee00 79 70 65 20 3d 20 22 74 61 62 6c 65 22 3b 0a 20  ype = "table";. 
1ee01 20 20 20 20 20 7a 54 79 70 65 32 20 3d 20 22 54       zType2 = "T
1ee02 41 42 4c 45 22 3b 0a 23 69 66 6e 64 65 66 20 53  ABLE";.#ifndef S
1ee03 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a  QLITE_OMIT_VIEW.
1ee04 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1ee05 20 2f 2a 20 41 20 76 69 65 77 20 2a 2f 0a 20 20   /* A view */.  
1ee06 20 20 20 20 7a 54 79 70 65 20 3d 20 22 76 69 65      zType = "vie
1ee07 77 22 3b 0a 20 20 20 20 20 20 7a 54 79 70 65 32  w";.      zType2
1ee08 20 3d 20 22 56 49 45 57 22 3b 0a 23 65 6e 64 69   = "VIEW";.#endi
1ee09 66 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  f.    }..    /* 
1ee0a 49 66 20 74 68 69 73 20 69 73 20 61 20 43 52 45  If this is a CRE
1ee0b 41 54 45 20 54 41 42 4c 45 20 78 78 20 41 53 20  ATE TABLE xx AS 
1ee0c 53 45 4c 45 43 54 20 2e 2e 2e 2c 20 65 78 65 63  SELECT ..., exec
1ee0d 75 74 65 20 74 68 65 20 53 45 4c 45 43 54 0a 20  ute the SELECT. 
1ee0e 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20     ** statement 
1ee0f 74 6f 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20  to populate the 
1ee10 6e 65 77 20 74 61 62 6c 65 2e 20 54 68 65 20 72  new table. The r
1ee11 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 20  oot-page number 
1ee12 66 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 6e  for the.    ** n
1ee13 65 77 20 74 61 62 6c 65 20 69 73 20 6f 6e 20 74  ew table is on t
1ee14 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 76 64  he top of the vd
1ee15 62 65 20 73 74 61 63 6b 2e 0a 20 20 20 20 2a 2a  be stack..    **
1ee16 0a 20 20 20 20 2a 2a 20 4f 6e 63 65 20 74 68 65  .    ** Once the
1ee17 20 53 45 4c 45 43 54 20 68 61 73 20 62 65 65 6e   SELECT has been
1ee18 20 63 6f 64 65 64 20 62 79 20 73 71 6c 69 74 65   coded by sqlite
1ee19 33 53 65 6c 65 63 74 28 29 2c 20 69 74 20 69 73  3Select(), it is
1ee1a 20 69 6e 20 61 0a 20 20 20 20 2a 2a 20 73 75 69   in a.    ** sui
1ee1b 74 61 62 6c 65 20 73 74 61 74 65 20 74 6f 20 71  table state to q
1ee1c 75 65 72 79 20 66 6f 72 20 74 68 65 20 63 6f 6c  uery for the col
1ee1d 75 6d 6e 20 6e 61 6d 65 73 20 61 6e 64 20 74 79  umn names and ty
1ee1e 70 65 73 20 74 6f 20 62 65 20 75 73 65 64 0a 20  pes to be used. 
1ee1f 20 20 20 2a 2a 20 62 79 20 74 68 65 20 6e 65 77     ** by the new
1ee20 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20   table..    **. 
1ee21 20 20 20 2a 2a 20 41 20 73 68 61 72 65 64 2d 63     ** A shared-c
1ee22 61 63 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20  ache write-lock 
1ee23 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20  is not required 
1ee24 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65 20  to write to the 
1ee25 6e 65 77 20 74 61 62 6c 65 2c 0a 20 20 20 20 2a  new table,.    *
1ee26 2a 20 61 73 20 61 20 73 63 68 65 6d 61 2d 6c 6f  * as a schema-lo
1ee27 63 6b 20 6d 75 73 74 20 68 61 76 65 20 61 6c 72  ck must have alr
1ee28 65 61 64 79 20 62 65 65 6e 20 6f 62 74 61 69 6e  eady been obtain
1ee29 65 64 20 74 6f 20 63 72 65 61 74 65 20 69 74 2e  ed to create it.
1ee2a 20 53 69 6e 63 65 0a 20 20 20 20 2a 2a 20 61 20   Since.    ** a 
1ee2b 73 63 68 65 6d 61 2d 6c 6f 63 6b 20 65 78 63 6c  schema-lock excl
1ee2c 75 64 65 73 20 61 6c 6c 20 6f 74 68 65 72 20 64  udes all other d
1ee2d 61 74 61 62 61 73 65 20 75 73 65 72 73 2c 20 74  atabase users, t
1ee2e 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20 77 6f  he write-lock wo
1ee2f 75 6c 64 0a 20 20 20 20 2a 2a 20 62 65 20 72 65  uld.    ** be re
1ee30 64 75 6e 64 61 6e 74 2e 0a 20 20 20 20 2a 2f 0a  dundant..    */.
1ee31 20 20 20 20 69 66 28 20 70 53 65 6c 65 63 74 20      if( pSelect 
1ee32 29 7b 0a 20 20 20 20 20 20 53 65 6c 65 63 74 44  ){.      SelectD
1ee33 65 73 74 20 64 65 73 74 3b 0a 20 20 20 20 20 20  est dest;.      
1ee34 54 61 62 6c 65 20 2a 70 53 65 6c 54 61 62 3b 0a  Table *pSelTab;.
1ee35 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 70 50  .      assert(pP
1ee36 61 72 73 65 2d 3e 6e 54 61 62 3d 3d 30 29 3b 0a  arse->nTab==0);.
1ee37 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1ee38 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70  eAddOp3(v, OP_Op
1ee39 65 6e 57 72 69 74 65 2c 20 31 2c 20 70 50 61 72  enWrite, 1, pPar
1ee3a 73 65 2d 3e 72 65 67 52 6f 6f 74 2c 20 69 44 62  se->regRoot, iDb
1ee3b 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1ee3c 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
1ee3d 31 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65  1);.      pParse
1ee3e 2d 3e 6e 54 61 62 20 3d 20 32 3b 0a 20 20 20 20  ->nTab = 2;.    
1ee3f 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
1ee40 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53  estInit(&dest, S
1ee41 52 54 5f 54 61 62 6c 65 2c 20 31 29 3b 0a 20 20  RT_Table, 1);.  
1ee42 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
1ee43 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63  t(pParse, pSelec
1ee44 74 2c 20 26 64 65 73 74 29 3b 0a 20 20 20 20 20  t, &dest);.     
1ee45 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1ee46 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p1(v, OP_Close, 
1ee47 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50  1);.      if( pP
1ee48 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 7b  arse->nErr==0 ){
1ee49 0a 20 20 20 20 20 20 20 20 70 53 65 6c 54 61 62  .        pSelTab
1ee4a 20 3d 20 73 71 6c 69 74 65 33 52 65 73 75 6c 74   = sqlite3Result
1ee4b 53 65 74 4f 66 53 65 6c 65 63 74 28 70 50 61 72  SetOfSelect(pPar
1ee4c 73 65 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20  se, pSelect);.  
1ee4d 20 20 20 20 20 20 69 66 28 20 70 53 65 6c 54 61        if( pSelTa
1ee4e 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  b==0 ) return;. 
1ee4f 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1ee50 2d 3e 61 43 6f 6c 3d 3d 30 20 29 3b 0a 20 20 20  ->aCol==0 );.   
1ee51 20 20 20 20 20 70 2d 3e 6e 43 6f 6c 20 3d 20 70       p->nCol = p
1ee52 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20  SelTab->nCol;.  
1ee53 20 20 20 20 20 20 70 2d 3e 61 43 6f 6c 20 3d 20        p->aCol = 
1ee54 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 3b 0a 20  pSelTab->aCol;. 
1ee55 20 20 20 20 20 20 20 70 53 65 6c 54 61 62 2d 3e         pSelTab->
1ee56 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20  nCol = 0;.      
1ee57 20 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 20    pSelTab->aCol 
1ee58 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  = 0;.        sql
1ee59 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28  ite3DeleteTable(
1ee5a 70 53 65 6c 54 61 62 29 3b 0a 20 20 20 20 20 20  pSelTab);.      
1ee5b 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
1ee5c 43 6f 6d 70 75 74 65 20 74 68 65 20 63 6f 6d 70  Compute the comp
1ee5d 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 65  lete text of the
1ee5e 20 43 52 45 41 54 45 20 73 74 61 74 65 6d 65 6e   CREATE statemen
1ee5f 74 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 53 65  t */.    if( pSe
1ee60 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 7a 53  lect ){.      zS
1ee61 74 6d 74 20 3d 20 63 72 65 61 74 65 54 61 62 6c  tmt = createTabl
1ee62 65 53 74 6d 74 28 64 62 2c 20 70 2c 20 70 2d 3e  eStmt(db, p, p->
1ee63 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 44 62  pSchema==db->aDb
1ee64 5b 31 5d 2e 70 53 63 68 65 6d 61 29 3b 0a 20 20  [1].pSchema);.  
1ee65 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e    }else{.      n
1ee66 20 3d 20 70 45 6e 64 2d 3e 7a 20 2d 20 70 50 61   = pEnd->z - pPa
1ee67 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e  rse->sNameToken.
1ee68 7a 20 2b 20 31 3b 0a 20 20 20 20 20 20 7a 53 74  z + 1;.      zSt
1ee69 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  mt = sqlite3MPri
1ee6a 6e 74 66 28 64 62 2c 20 0a 20 20 20 20 20 20 20  ntf(db, .       
1ee6b 20 20 20 22 43 52 45 41 54 45 20 25 73 20 25 2e     "CREATE %s %.
1ee6c 2a 73 22 2c 20 7a 54 79 70 65 32 2c 20 6e 2c 20  *s", zType2, n, 
1ee6d 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b  pParse->sNameTok
1ee6e 65 6e 2e 7a 0a 20 20 20 20 20 20 29 3b 0a 20 20  en.z.      );.  
1ee6f 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 20 73 6c    }..    /* A sl
1ee70 6f 74 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72  ot for the recor
1ee71 64 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  d has already be
1ee72 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20  en allocated in 
1ee73 74 68 65 20 0a 20 20 20 20 2a 2a 20 53 51 4c 49  the .    ** SQLI
1ee74 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e  TE_MASTER table.
1ee75 20 20 57 65 20 6a 75 73 74 20 6e 65 65 64 20 74    We just need t
1ee76 6f 20 75 70 64 61 74 65 20 74 68 61 74 20 73 6c  o update that sl
1ee77 6f 74 20 77 69 74 68 20 61 6c 6c 0a 20 20 20 20  ot with all.    
1ee78 2a 2a 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69  ** the informati
1ee79 6f 6e 20 77 65 27 76 65 20 63 6f 6c 6c 65 63 74  on we've collect
1ee7a 65 64 2e 20 20 54 68 65 20 72 6f 77 69 64 20 66  ed.  The rowid f
1ee7b 6f 72 20 74 68 65 20 70 72 65 61 6c 6c 6f 63 61  or the prealloca
1ee7c 74 65 64 0a 20 20 20 20 2a 2a 20 73 6c 6f 74 20  ted.    ** slot 
1ee7d 69 73 20 74 68 65 20 32 6e 64 20 69 74 65 6d 20  is the 2nd item 
1ee7e 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e 20 20 54  on the stack.  T
1ee7f 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74  he top of the st
1ee80 61 63 6b 20 69 73 20 74 68 65 0a 20 20 20 20 2a  ack is the.    *
1ee81 2a 20 72 6f 6f 74 20 70 61 67 65 20 66 6f 72 20  * root page for 
1ee82 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 28 6f  the new table (o
1ee83 72 20 61 20 30 20 69 66 20 74 68 69 73 20 69 73  r a 0 if this is
1ee84 20 61 20 76 69 65 77 29 2e 0a 20 20 20 20 2a 2f   a view)..    */
1ee85 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74  .    sqlite3Nest
1ee86 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a  edParse(pParse,.
1ee87 20 20 20 20 20 20 22 55 50 44 41 54 45 20 25 51        "UPDATE %Q
1ee88 2e 25 73 20 22 0a 20 20 20 20 20 20 20 20 20 22  .%s ".         "
1ee89 53 45 54 20 74 79 70 65 3d 27 25 73 27 2c 20 6e  SET type='%s', n
1ee8a 61 6d 65 3d 25 51 2c 20 74 62 6c 5f 6e 61 6d 65  ame=%Q, tbl_name
1ee8b 3d 25 51 2c 20 72 6f 6f 74 70 61 67 65 3d 23 25  =%Q, rootpage=#%
1ee8c 64 2c 20 73 71 6c 3d 25 51 20 22 0a 20 20 20 20  d, sql=%Q ".    
1ee8d 20 20 20 22 57 48 45 52 45 20 72 6f 77 69 64 3d     "WHERE rowid=
1ee8e 23 25 64 22 2c 0a 20 20 20 20 20 20 64 62 2d 3e  #%d",.      db->
1ee8f 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20  aDb[iDb].zName, 
1ee90 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62  SCHEMA_TABLE(iDb
1ee91 29 2c 0a 20 20 20 20 20 20 7a 54 79 70 65 2c 0a  ),.      zType,.
1ee92 20 20 20 20 20 20 70 2d 3e 7a 4e 61 6d 65 2c 0a        p->zName,.
1ee93 20 20 20 20 20 20 70 2d 3e 7a 4e 61 6d 65 2c 0a        p->zName,.
1ee94 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 65        pParse->re
1ee95 67 52 6f 6f 74 2c 0a 20 20 20 20 20 20 7a 53 74  gRoot,.      zSt
1ee96 6d 74 2c 0a 20 20 20 20 20 20 70 50 61 72 73 65  mt,.      pParse
1ee97 2d 3e 72 65 67 52 6f 77 69 64 0a 20 20 20 20 29  ->regRowid.    )
1ee98 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
1ee99 72 65 65 28 64 62 2c 20 7a 53 74 6d 74 29 3b 0a  ree(db, zStmt);.
1ee9a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67      sqlite3Chang
1ee9b 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20  eCookie(pParse, 
1ee9c 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  iDb);..#ifndef S
1ee9d 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
1ee9e 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 2f 2a 20  NCREMENT.    /* 
1ee9f 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
1eea0 77 65 20 6e 65 65 64 20 74 6f 20 63 72 65 61 74  we need to creat
1eea1 65 20 61 6e 20 73 71 6c 69 74 65 5f 73 65 71 75  e an sqlite_sequ
1eea2 65 6e 63 65 20 74 61 62 6c 65 20 66 6f 72 0a 20  ence table for. 
1eea3 20 20 20 2a 2a 20 6b 65 65 70 69 6e 67 20 74 72     ** keeping tr
1eea4 61 63 6b 20 6f 66 20 61 75 74 6f 69 6e 63 72 65  ack of autoincre
1eea5 6d 65 6e 74 20 6b 65 79 73 2e 0a 20 20 20 20 2a  ment keys..    *
1eea6 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 61 62  /.    if( p->tab
1eea7 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f 69  Flags & TF_Autoi
1eea8 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20  ncrement ){.    
1eea9 20 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62 2d    Db *pDb = &db-
1eeaa 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 20 20 20  >aDb[iDb];.     
1eeab 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d   if( pDb->pSchem
1eeac 61 2d 3e 70 53 65 71 54 61 62 3d 3d 30 20 29 7b  a->pSeqTab==0 ){
1eead 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1eeae 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72  NestedParse(pPar
1eeaf 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20 22 43  se,.          "C
1eeb0 52 45 41 54 45 20 54 41 42 4c 45 20 25 51 2e 73  REATE TABLE %Q.s
1eeb1 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 28 6e  qlite_sequence(n
1eeb2 61 6d 65 2c 73 65 71 29 22 2c 0a 20 20 20 20 20  ame,seq)",.     
1eeb3 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 0a       pDb->zName.
1eeb4 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20          );.     
1eeb5 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a   }.    }.#endif.
1eeb6 0a 20 20 20 20 2f 2a 20 52 65 70 61 72 73 65 20  .    /* Reparse 
1eeb7 65 76 65 72 79 74 68 69 6e 67 20 74 6f 20 75 70  everything to up
1eeb8 64 61 74 65 20 6f 75 72 20 69 6e 74 65 72 6e 61  date our interna
1eeb9 6c 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65  l data structure
1eeba 73 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  s */.    sqlite3
1eebb 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
1eebc 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44  _ParseSchema, iD
1eebd 62 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20  b, 0, 0,.       
1eebe 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
1eebf 64 62 2c 20 22 74 62 6c 5f 6e 61 6d 65 3d 27 25  db, "tbl_name='%
1eec0 71 27 22 2c 70 2d 3e 7a 4e 61 6d 65 29 2c 20 50  q'",p->zName), P
1eec1 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a  4_DYNAMIC);.  }.
1eec2 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 74  ..  /* Add the t
1eec3 61 62 6c 65 20 74 6f 20 74 68 65 20 69 6e 2d 6d  able to the in-m
1eec4 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61  emory representa
1eec5 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61  tion of the data
1eec6 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  base..  */.  if(
1eec7 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26   db->init.busy &
1eec8 26 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d  & pParse->nErr==
1eec9 30 20 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a  0 ){.    Table *
1eeca 70 4f 6c 64 3b 0a 20 20 20 20 46 4b 65 79 20 2a  pOld;.    FKey *
1eecb 70 46 4b 65 79 3b 20 0a 20 20 20 20 53 63 68 65  pFKey; .    Sche
1eecc 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 70 2d  ma *pSchema = p-
1eecd 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 70 4f  >pSchema;.    pO
1eece 6c 64 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68  ld = sqlite3Hash
1eecf 49 6e 73 65 72 74 28 26 70 53 63 68 65 6d 61 2d  Insert(&pSchema-
1eed0 3e 74 62 6c 48 61 73 68 2c 20 70 2d 3e 7a 4e 61  >tblHash, p->zNa
1eed1 6d 65 2c 20 73 74 72 6c 65 6e 28 70 2d 3e 7a 4e  me, strlen(p->zN
1eed2 61 6d 65 29 2b 31 2c 70 29 3b 0a 20 20 20 20 69  ame)+1,p);.    i
1eed3 66 28 20 70 4f 6c 64 20 29 7b 0a 20 20 20 20 20  f( pOld ){.     
1eed4 20 61 73 73 65 72 74 28 20 70 3d 3d 70 4f 6c 64   assert( p==pOld
1eed5 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d   );  /* Malloc m
1eed6 75 73 74 20 68 61 76 65 20 66 61 69 6c 65 64 20  ust have failed 
1eed7 69 6e 73 69 64 65 20 48 61 73 68 49 6e 73 65 72  inside HashInser
1eed8 74 28 29 20 2a 2f 0a 20 20 20 20 20 20 64 62 2d  t() */.      db-
1eed9 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
1eeda 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  1;.      return;
1eedb 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  .    }.#ifndef S
1eedc 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49  QLITE_OMIT_FOREI
1eedd 47 4e 5f 4b 45 59 0a 20 20 20 20 66 6f 72 28 70  GN_KEY.    for(p
1eede 46 4b 65 79 3d 70 2d 3e 70 46 4b 65 79 3b 20 70  FKey=p->pFKey; p
1eedf 46 4b 65 79 3b 20 70 46 4b 65 79 3d 70 46 4b 65  FKey; pFKey=pFKe
1eee0 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 29 7b 0a 20  y->pNextFrom){. 
1eee1 20 20 20 20 20 76 6f 69 64 20 2a 64 61 74 61 3b       void *data;
1eee2 0a 20 20 20 20 20 20 69 6e 74 20 6e 54 6f 20 3d  .      int nTo =
1eee3 20 73 74 72 6c 65 6e 28 70 46 4b 65 79 2d 3e 7a   strlen(pFKey->z
1eee4 54 6f 29 20 2b 20 31 3b 0a 20 20 20 20 20 20 70  To) + 1;.      p
1eee5 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 20 3d 20  FKey->pNextTo = 
1eee6 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28  sqlite3HashFind(
1eee7 26 70 53 63 68 65 6d 61 2d 3e 61 46 4b 65 79 2c  &pSchema->aFKey,
1eee8 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20 6e 54 6f   pFKey->zTo, nTo
1eee9 29 3b 0a 20 20 20 20 20 20 64 61 74 61 20 3d 20  );.      data = 
1eeea 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72  sqlite3HashInser
1eeeb 74 28 26 70 53 63 68 65 6d 61 2d 3e 61 46 4b 65  t(&pSchema->aFKe
1eeec 79 2c 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20 6e  y, pFKey->zTo, n
1eeed 54 6f 2c 20 70 46 4b 65 79 29 3b 0a 20 20 20 20  To, pFKey);.    
1eeee 20 20 69 66 28 20 64 61 74 61 3d 3d 28 76 6f 69    if( data==(voi
1eeef 64 20 2a 29 70 46 4b 65 79 20 29 7b 0a 20 20 20  d *)pFKey ){.   
1eef0 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46       db->mallocF
1eef1 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  ailed = 1;.     
1eef2 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a   }.    }.#endif.
1eef3 20 20 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77      pParse->pNew
1eef4 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20 20 20 64  Table = 0;.    d
1eef5 62 2d 3e 6e 54 61 62 6c 65 2b 2b 3b 0a 20 20 20  b->nTable++;.   
1eef6 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51   db->flags |= SQ
1eef7 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67  LITE_InternChang
1eef8 65 73 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  es;..#ifndef SQL
1eef9 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41  ITE_OMIT_ALTERTA
1eefa 42 4c 45 0a 20 20 20 20 69 66 28 20 21 70 2d 3e  BLE.    if( !p->
1eefb 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20  pSelect ){.     
1eefc 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
1eefd 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  me = (const char
1eefe 20 2a 29 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65   *)pParse->sName
1eeff 54 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 20 20 20 69  Token.z;.      i
1ef00 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 20 20 20 20  nt nName;.      
1ef01 61 73 73 65 72 74 28 20 21 70 53 65 6c 65 63 74  assert( !pSelect
1ef02 20 26 26 20 70 43 6f 6e 73 20 26 26 20 70 45 6e   && pCons && pEn
1ef03 64 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  d );.      if( p
1ef04 43 6f 6e 73 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20  Cons->z==0 ){.  
1ef05 20 20 20 20 20 20 70 43 6f 6e 73 20 3d 20 70 45        pCons = pE
1ef06 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  nd;.      }.    
1ef07 20 20 6e 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74    nName = (const
1ef08 20 63 68 61 72 20 2a 29 70 43 6f 6e 73 2d 3e 7a   char *)pCons->z
1ef09 20 2d 20 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20   - zName;.      
1ef0a 70 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 20  p->addColOffset 
1ef0b 3d 20 31 33 20 2b 20 73 71 6c 69 74 65 33 55 74  = 13 + sqlite3Ut
1ef0c 66 38 43 68 61 72 4c 65 6e 28 7a 4e 61 6d 65 2c  f8CharLen(zName,
1ef0d 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23   nName);.    }.#
1ef0e 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 23 69 66  endif.  }.}..#if
1ef0f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1ef10 5f 56 49 45 57 0a 2f 2a 0a 2a 2a 20 54 68 65 20  _VIEW./*.** The 
1ef11 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69  parser calls thi
1ef12 73 20 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64  s routine in ord
1ef13 65 72 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e  er to create a n
1ef14 65 77 20 56 49 45 57 0a 2a 2f 0a 53 51 4c 49 54  ew VIEW.*/.SQLIT
1ef15 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1ef16 71 6c 69 74 65 33 43 72 65 61 74 65 56 69 65 77  qlite3CreateView
1ef17 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
1ef18 65 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61  e,     /* The pa
1ef19 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
1ef1a 0a 20 20 54 6f 6b 65 6e 20 2a 70 42 65 67 69 6e  .  Token *pBegin
1ef1b 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 43 52 45  ,     /* The CRE
1ef1c 41 54 45 20 74 6f 6b 65 6e 20 74 68 61 74 20 62  ATE token that b
1ef1d 65 67 69 6e 73 20 74 68 65 20 73 74 61 74 65 6d  egins the statem
1ef1e 65 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ent */.  Token *
1ef1f 70 4e 61 6d 65 31 2c 20 20 20 20 20 2f 2a 20 54  pName1,     /* T
1ef20 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 6f  he token that ho
1ef21 6c 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  lds the name of 
1ef22 74 68 65 20 76 69 65 77 20 2a 2f 0a 20 20 54 6f  the view */.  To
1ef23 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20  ken *pName2,    
1ef24 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 20 74 68   /* The token th
1ef25 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d  at holds the nam
1ef26 65 20 6f 66 20 74 68 65 20 76 69 65 77 20 2a 2f  e of the view */
1ef27 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  .  Select *pSele
1ef28 63 74 2c 20 20 20 2f 2a 20 41 20 53 45 4c 45 43  ct,   /* A SELEC
1ef29 54 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  T statement that
1ef2a 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65   will become the
1ef2b 20 6e 65 77 20 76 69 65 77 20 2a 2f 0a 20 20 69   new view */.  i
1ef2c 6e 74 20 69 73 54 65 6d 70 2c 20 20 20 20 20 20  nt isTemp,      
1ef2d 20 20 2f 2a 20 54 52 55 45 20 66 6f 72 20 61 20    /* TRUE for a 
1ef2e 54 45 4d 50 4f 52 41 52 59 20 76 69 65 77 20 2a  TEMPORARY view *
1ef2f 2f 0a 20 20 69 6e 74 20 6e 6f 45 72 72 20 20 20  /.  int noErr   
1ef30 20 20 20 20 20 20 20 2f 2a 20 53 75 70 70 72 65         /* Suppre
1ef31 73 73 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  ss error message
1ef32 73 20 69 66 20 56 49 45 57 20 61 6c 72 65 61 64  s if VIEW alread
1ef33 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20  y exists */.){. 
1ef34 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74   Table *p;.  int
1ef35 20 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69   n;.  const unsi
1ef36 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20  gned char *z;.  
1ef37 54 6f 6b 65 6e 20 73 45 6e 64 3b 0a 20 20 44 62  Token sEnd;.  Db
1ef38 46 69 78 65 72 20 73 46 69 78 3b 0a 20 20 54 6f  Fixer sFix;.  To
1ef39 6b 65 6e 20 2a 70 4e 61 6d 65 3b 0a 20 20 69 6e  ken *pName;.  in
1ef3a 74 20 69 44 62 3b 0a 20 20 73 71 6c 69 74 65 33  t iDb;.  sqlite3
1ef3b 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
1ef3c 62 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65  b;..  if( pParse
1ef3d 2d 3e 6e 56 61 72 3e 30 20 29 7b 0a 20 20 20 20  ->nVar>0 ){.    
1ef3e 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1ef3f 70 50 61 72 73 65 2c 20 22 70 61 72 61 6d 65 74  pParse, "paramet
1ef40 65 72 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f  ers are not allo
1ef41 77 65 64 20 69 6e 20 76 69 65 77 73 22 29 3b 0a  wed in views");.
1ef42 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
1ef43 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c  tDelete(db, pSel
1ef44 65 63 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ect);.    return
1ef45 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53  ;.  }.  sqlite3S
1ef46 74 61 72 74 54 61 62 6c 65 28 70 50 61 72 73 65  tartTable(pParse
1ef47 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32  , pName1, pName2
1ef48 2c 20 69 73 54 65 6d 70 2c 20 31 2c 20 30 2c 20  , isTemp, 1, 0, 
1ef49 6e 6f 45 72 72 29 3b 0a 20 20 70 20 3d 20 70 50  noErr);.  p = pP
1ef4a 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b  arse->pNewTable;
1ef4b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 70  .  if( p==0 || p
1ef4c 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b 0a 20  Parse->nErr ){. 
1ef4d 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
1ef4e 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65  Delete(db, pSele
1ef4f 63 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ct);.    return;
1ef50 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 54 77  .  }.  sqlite3Tw
1ef51 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65  oPartName(pParse
1ef52 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32  , pName1, pName2
1ef53 2c 20 26 70 4e 61 6d 65 29 3b 0a 20 20 69 44 62  , &pName);.  iDb
1ef54 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
1ef55 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 2d 3e 70  ToIndex(db, p->p
1ef56 53 63 68 65 6d 61 29 3b 0a 20 20 69 66 28 20 73  Schema);.  if( s
1ef57 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28 26 73  qlite3FixInit(&s
1ef58 46 69 78 2c 20 70 50 61 72 73 65 2c 20 69 44 62  Fix, pParse, iDb
1ef59 2c 20 22 76 69 65 77 22 2c 20 70 4e 61 6d 65 29  , "view", pName)
1ef5a 0a 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 46  .    && sqlite3F
1ef5b 69 78 53 65 6c 65 63 74 28 26 73 46 69 78 2c 20  ixSelect(&sFix, 
1ef5c 70 53 65 6c 65 63 74 29 0a 20 20 29 7b 0a 20 20  pSelect).  ){.  
1ef5d 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
1ef5e 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63  elete(db, pSelec
1ef5f 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  t);.    return;.
1ef60 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 61    }..  /* Make a
1ef61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65 6e 74   copy of the ent
1ef62 69 72 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  ire SELECT state
1ef63 6d 65 6e 74 20 74 68 61 74 20 64 65 66 69 6e 65  ment that define
1ef64 73 20 74 68 65 20 76 69 65 77 2e 0a 20 20 2a 2a  s the view..  **
1ef65 20 54 68 69 73 20 77 69 6c 6c 20 66 6f 72 63 65   This will force
1ef66 20 61 6c 6c 20 74 68 65 20 45 78 70 72 2e 74 6f   all the Expr.to
1ef67 6b 65 6e 2e 7a 20 76 61 6c 75 65 73 20 74 6f 20  ken.z values to 
1ef68 62 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 20  be dynamically. 
1ef69 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 72 61   ** allocated ra
1ef6a 74 68 65 72 20 74 68 61 6e 20 70 6f 69 6e 74 20  ther than point 
1ef6b 74 6f 20 74 68 65 20 69 6e 70 75 74 20 73 74 72  to the input str
1ef6c 69 6e 67 20 2d 20 77 68 69 63 68 20 6d 65 61 6e  ing - which mean
1ef6d 73 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 65 79  s that.  ** they
1ef6e 20 77 69 6c 6c 20 70 65 72 73 69 73 74 20 61 66   will persist af
1ef6f 74 65 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  ter the current 
1ef70 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 63  sqlite3_exec() c
1ef71 61 6c 6c 20 72 65 74 75 72 6e 73 2e 0a 20 20 2a  all returns..  *
1ef72 2f 0a 20 20 70 2d 3e 70 53 65 6c 65 63 74 20 3d  /.  p->pSelect =
1ef73 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75   sqlite3SelectDu
1ef74 70 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a  p(db, pSelect);.
1ef75 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
1ef76 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63  elete(db, pSelec
1ef77 74 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  t);.  if( db->ma
1ef78 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
1ef79 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
1ef7a 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75  if( !db->init.bu
1ef7b 73 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  sy ){.    sqlite
1ef7c 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61  3ViewGetColumnNa
1ef7d 6d 65 73 28 70 50 61 72 73 65 2c 20 70 29 3b 0a  mes(pParse, p);.
1ef7e 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65    }..  /* Locate
1ef7f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
1ef80 43 52 45 41 54 45 20 56 49 45 57 20 73 74 61 74  CREATE VIEW stat
1ef81 65 6d 65 6e 74 2e 20 20 4d 61 6b 65 20 73 45 6e  ement.  Make sEn
1ef82 64 20 70 6f 69 6e 74 20 74 6f 0a 20 20 2a 2a 20  d point to.  ** 
1ef83 74 68 65 20 65 6e 64 2e 0a 20 20 2a 2f 0a 20 20  the end..  */.  
1ef84 73 45 6e 64 20 3d 20 70 50 61 72 73 65 2d 3e 73  sEnd = pParse->s
1ef85 4c 61 73 74 54 6f 6b 65 6e 3b 0a 20 20 69 66 28  LastToken;.  if(
1ef86 20 73 45 6e 64 2e 7a 5b 30 5d 21 3d 30 20 26 26   sEnd.z[0]!=0 &&
1ef87 20 73 45 6e 64 2e 7a 5b 30 5d 21 3d 27 3b 27 20   sEnd.z[0]!=';' 
1ef88 29 7b 0a 20 20 20 20 73 45 6e 64 2e 7a 20 2b 3d  ){.    sEnd.z +=
1ef89 20 73 45 6e 64 2e 6e 3b 0a 20 20 7d 0a 20 20 73   sEnd.n;.  }.  s
1ef8a 45 6e 64 2e 6e 20 3d 20 30 3b 0a 20 20 6e 20 3d  End.n = 0;.  n =
1ef8b 20 73 45 6e 64 2e 7a 20 2d 20 70 42 65 67 69 6e   sEnd.z - pBegin
1ef8c 2d 3e 7a 3b 0a 20 20 7a 20 3d 20 28 63 6f 6e 73  ->z;.  z = (cons
1ef8d 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a  t unsigned char*
1ef8e 29 70 42 65 67 69 6e 2d 3e 7a 3b 0a 20 20 77 68  )pBegin->z;.  wh
1ef8f 69 6c 65 28 20 6e 3e 30 20 26 26 20 28 7a 5b 6e  ile( n>0 && (z[n
1ef90 2d 31 5d 3d 3d 27 3b 27 20 7c 7c 20 69 73 73 70  -1]==';' || issp
1ef91 61 63 65 28 7a 5b 6e 2d 31 5d 29 29 20 29 7b 20  ace(z[n-1])) ){ 
1ef92 6e 2d 2d 3b 20 7d 0a 20 20 73 45 6e 64 2e 7a 20  n--; }.  sEnd.z 
1ef93 3d 20 26 7a 5b 6e 2d 31 5d 3b 0a 20 20 73 45 6e  = &z[n-1];.  sEn
1ef94 64 2e 6e 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 55  d.n = 1;..  /* U
1ef95 73 65 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62  se sqlite3EndTab
1ef96 6c 65 28 29 20 74 6f 20 61 64 64 20 74 68 65 20  le() to add the 
1ef97 76 69 65 77 20 74 6f 20 74 68 65 20 53 51 4c 49  view to the SQLI
1ef98 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20  TE_MASTER table 
1ef99 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 54  */.  sqlite3EndT
1ef9a 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20  able(pParse, 0, 
1ef9b 26 73 45 6e 64 2c 20 30 29 3b 0a 20 20 72 65 74  &sEnd, 0);.  ret
1ef9c 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  urn;.}.#endif /*
1ef9d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45   SQLITE_OMIT_VIE
1ef9e 57 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e  W */..#if !defin
1ef9f 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
1efa0 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64  IEW) || !defined
1efa1 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52  (SQLITE_OMIT_VIR
1efa2 54 55 41 4c 54 41 42 4c 45 29 0a 2f 2a 0a 2a 2a  TUALTABLE)./*.**
1efa3 20 54 68 65 20 54 61 62 6c 65 20 73 74 72 75 63   The Table struc
1efa4 74 75 72 65 20 70 54 61 62 6c 65 20 69 73 20 72  ture pTable is r
1efa5 65 61 6c 6c 79 20 61 20 56 49 45 57 2e 20 20 46  eally a VIEW.  F
1efa6 69 6c 6c 20 69 6e 20 74 68 65 20 6e 61 6d 65 73  ill in the names
1efa7 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d   of.** the colum
1efa8 6e 73 20 6f 66 20 74 68 65 20 76 69 65 77 20 69  ns of the view i
1efa9 6e 20 74 68 65 20 70 54 61 62 6c 65 20 73 74 72  n the pTable str
1efaa 75 63 74 75 72 65 2e 20 20 52 65 74 75 72 6e 20  ucture.  Return 
1efab 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66  the number.** of
1efac 20 65 72 72 6f 72 73 2e 20 20 49 66 20 61 6e 20   errors.  If an 
1efad 65 72 72 6f 72 20 69 73 20 73 65 65 6e 20 6c 65  error is seen le
1efae 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ave an error mes
1efaf 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 2d 3e  sage in pParse->
1efb0 7a 45 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49  zErrMsg..*/.SQLI
1efb1 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1efb2 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c  qlite3ViewGetCol
1efb3 75 6d 6e 4e 61 6d 65 73 28 50 61 72 73 65 20 2a  umnNames(Parse *
1efb4 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70  pParse, Table *p
1efb5 54 61 62 6c 65 29 7b 0a 20 20 54 61 62 6c 65 20  Table){.  Table 
1efb6 2a 70 53 65 6c 54 61 62 3b 20 20 20 2f 2a 20 41  *pSelTab;   /* A
1efb7 20 66 61 6b 65 20 74 61 62 6c 65 20 66 72 6f 6d   fake table from
1efb8 20 77 68 69 63 68 20 77 65 20 67 65 74 20 74 68   which we get th
1efb9 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a  e result set */.
1efba 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20    Select *pSel; 
1efbb 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 74      /* Copy of t
1efbc 68 65 20 53 45 4c 45 43 54 20 74 68 61 74 20 69  he SELECT that i
1efbd 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 76 69  mplements the vi
1efbe 65 77 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72  ew */.  int nErr
1efbf 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d   = 0;     /* Num
1efc0 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e  ber of errors en
1efc1 63 6f 75 6e 74 65 72 65 64 20 2a 2f 0a 20 20 69  countered */.  i
1efc2 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20  nt n;           
1efc3 20 2f 2a 20 54 65 6d 70 6f 72 61 72 69 6c 79 20   /* Temporarily 
1efc4 68 6f 6c 64 73 20 74 68 65 20 6e 75 6d 62 65 72  holds the number
1efc5 20 6f 66 20 63 75 72 73 6f 72 73 20 61 73 73 69   of cursors assi
1efc6 67 6e 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65  gned */.  sqlite
1efc7 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
1efc8 64 62 3b 20 20 2f 2a 20 44 61 74 61 62 61 73 65  db;  /* Database
1efc9 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 6f 72 20   connection for 
1efca 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20 2a 2f  malloc errors */
1efcb 0a 20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28  .  int (*xAuth)(
1efcc 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
1efcd 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
1efce 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  *,const char*,co
1efcf 6e 73 74 20 63 68 61 72 2a 29 3b 0a 0a 20 20 61  nst char*);..  a
1efd0 73 73 65 72 74 28 20 70 54 61 62 6c 65 20 29 3b  ssert( pTable );
1efd1 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1efd2 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1efd3 4c 45 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  LE.  if( sqlite3
1efd4 56 74 61 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28  VtabCallConnect(
1efd5 70 50 61 72 73 65 2c 20 70 54 61 62 6c 65 29 20  pParse, pTable) 
1efd6 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
1efd7 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
1efd8 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
1efd9 70 54 61 62 6c 65 29 20 29 20 72 65 74 75 72 6e  pTable) ) return
1efda 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e   0;.#endif..#ifn
1efdb 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1efdc 56 49 45 57 0a 20 20 2f 2a 20 41 20 70 6f 73 69  VIEW.  /* A posi
1efdd 74 69 76 65 20 6e 43 6f 6c 20 6d 65 61 6e 73 20  tive nCol means 
1efde 74 68 65 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65  the columns name
1efdf 73 20 66 6f 72 20 74 68 69 73 20 76 69 65 77 20  s for this view 
1efe0 61 72 65 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79  are.  ** already
1efe1 20 6b 6e 6f 77 6e 2e 0a 20 20 2a 2f 0a 20 20 69   known..  */.  i
1efe2 66 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3e  f( pTable->nCol>
1efe3 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20  0 ) return 0;.. 
1efe4 20 2f 2a 20 41 20 6e 65 67 61 74 69 76 65 20 6e   /* A negative n
1efe5 43 6f 6c 20 69 73 20 61 20 73 70 65 63 69 61 6c  Col is a special
1efe6 20 6d 61 72 6b 65 72 20 6d 65 61 6e 69 6e 67 20   marker meaning 
1efe7 74 68 61 74 20 77 65 20 61 72 65 20 63 75 72 72  that we are curr
1efe8 65 6e 74 6c 79 0a 20 20 2a 2a 20 74 72 79 69 6e  ently.  ** tryin
1efe9 67 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 65  g to compute the
1efea 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20 20   column names.  
1efeb 49 66 20 77 65 20 65 6e 74 65 72 20 74 68 69 73  If we enter this
1efec 20 72 6f 75 74 69 6e 65 20 77 69 74 68 0a 20 20   routine with.  
1efed 2a 2a 20 61 20 6e 65 67 61 74 69 76 65 20 6e 43  ** a negative nC
1efee 6f 6c 2c 20 69 74 20 6d 65 61 6e 73 20 74 77 6f  ol, it means two
1efef 20 6f 72 20 6d 6f 72 65 20 76 69 65 77 73 20 66   or more views f
1eff0 6f 72 6d 20 61 20 6c 6f 6f 70 2c 20 6c 69 6b 65  orm a loop, like
1eff1 20 74 68 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a   this:.  **.  **
1eff2 20 20 20 20 20 43 52 45 41 54 45 20 56 49 45 57       CREATE VIEW
1eff3 20 6f 6e 65 20 41 53 20 53 45 4c 45 43 54 20 2a   one AS SELECT *
1eff4 20 46 52 4f 4d 20 74 77 6f 3b 0a 20 20 2a 2a 20   FROM two;.  ** 
1eff5 20 20 20 20 43 52 45 41 54 45 20 56 49 45 57 20      CREATE VIEW 
1eff6 74 77 6f 20 41 53 20 53 45 4c 45 43 54 20 2a 20  two AS SELECT * 
1eff7 46 52 4f 4d 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20  FROM one;.  **. 
1eff8 20 2a 2a 20 41 63 74 75 61 6c 6c 79 2c 20 74 68   ** Actually, th
1eff9 69 73 20 65 72 72 6f 72 20 69 73 20 63 61 75 67  is error is caug
1effa 68 74 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6e  ht previously an
1effb 64 20 73 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  d so the followi
1effc 6e 67 20 74 65 73 74 0a 20 20 2a 2a 20 73 68 6f  ng test.  ** sho
1effd 75 6c 64 20 61 6c 77 61 79 73 20 66 61 69 6c 2e  uld always fail.
1effe 20 20 42 75 74 20 77 65 20 77 69 6c 6c 20 6c 65    But we will le
1efff 61 76 65 20 69 74 20 69 6e 20 70 6c 61 63 65 20  ave it in place 
1f000 6a 75 73 74 20 74 6f 20 62 65 20 73 61 66 65 2e  just to be safe.
1f001 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62  .  */.  if( pTab
1f002 6c 65 2d 3e 6e 43 6f 6c 3c 30 20 29 7b 0a 20 20  le->nCol<0 ){.  
1f003 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1f004 67 28 70 50 61 72 73 65 2c 20 22 76 69 65 77 20  g(pParse, "view 
1f005 25 73 20 69 73 20 63 69 72 63 75 6c 61 72 6c 79  %s is circularly
1f006 20 64 65 66 69 6e 65 64 22 2c 20 70 54 61 62 6c   defined", pTabl
1f007 65 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72  e->zName);.    r
1f008 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 61  eturn 1;.  }.  a
1f009 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 6e  ssert( pTable->n
1f00a 43 6f 6c 3e 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20  Col>=0 );..  /* 
1f00b 49 66 20 77 65 20 67 65 74 20 74 68 69 73 20 66  If we get this f
1f00c 61 72 2c 20 69 74 20 6d 65 61 6e 73 20 77 65 20  ar, it means we 
1f00d 6e 65 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 20  need to compute 
1f00e 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 73 2e  the table names.
1f00f 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  .  ** Note that 
1f010 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  the call to sqli
1f011 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65  te3ResultSetOfSe
1f012 6c 65 63 74 28 29 20 77 69 6c 6c 20 65 78 70 61  lect() will expa
1f013 6e 64 20 61 6e 79 0a 20 20 2a 2a 20 22 2a 22 20  nd any.  ** "*" 
1f014 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20  elements in the 
1f015 72 65 73 75 6c 74 73 20 73 65 74 20 6f 66 20 74  results set of t
1f016 68 65 20 76 69 65 77 20 61 6e 64 20 77 69 6c 6c  he view and will
1f017 20 61 73 73 69 67 6e 20 63 75 72 73 6f 72 73 0a   assign cursors.
1f018 20 20 2a 2a 20 74 6f 20 74 68 65 20 65 6c 65 6d    ** to the elem
1f019 65 6e 74 73 20 6f 66 20 74 68 65 20 46 52 4f 4d  ents of the FROM
1f01a 20 63 6c 61 75 73 65 2e 20 20 42 75 74 20 77 65   clause.  But we
1f01b 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68 65   do not want the
1f01c 73 65 20 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20  se changes.  ** 
1f01d 74 6f 20 62 65 20 70 65 72 6d 61 6e 65 6e 74 2e  to be permanent.
1f01e 20 20 53 6f 20 74 68 65 20 63 6f 6d 70 75 74 61    So the computa
1f01f 74 69 6f 6e 20 69 73 20 64 6f 6e 65 20 6f 6e 20  tion is done on 
1f020 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 53 45  a copy of the SE
1f021 4c 45 43 54 0a 20 20 2a 2a 20 73 74 61 74 65 6d  LECT.  ** statem
1f022 65 6e 74 20 74 68 61 74 20 64 65 66 69 6e 65 73  ent that defines
1f023 20 74 68 65 20 76 69 65 77 2e 0a 20 20 2a 2f 0a   the view..  */.
1f024 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65    assert( pTable
1f025 2d 3e 70 53 65 6c 65 63 74 20 29 3b 0a 20 20 70  ->pSelect );.  p
1f026 53 65 6c 20 3d 20 73 71 6c 69 74 65 33 53 65 6c  Sel = sqlite3Sel
1f027 65 63 74 44 75 70 28 64 62 2c 20 70 54 61 62 6c  ectDup(db, pTabl
1f028 65 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 69  e->pSelect);.  i
1f029 66 28 20 70 53 65 6c 20 29 7b 0a 20 20 20 20 6e  f( pSel ){.    n
1f02a 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b   = pParse->nTab;
1f02b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c  .    sqlite3SrcL
1f02c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73  istAssignCursors
1f02d 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2d 3e 70  (pParse, pSel->p
1f02e 53 72 63 29 3b 0a 20 20 20 20 70 54 61 62 6c 65  Src);.    pTable
1f02f 2d 3e 6e 43 6f 6c 20 3d 20 2d 31 3b 0a 23 69 66  ->nCol = -1;.#if
1f030 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1f031 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20  _AUTHORIZATION. 
1f032 20 20 20 78 41 75 74 68 20 3d 20 64 62 2d 3e 78     xAuth = db->x
1f033 41 75 74 68 3b 0a 20 20 20 20 64 62 2d 3e 78 41  Auth;.    db->xA
1f034 75 74 68 20 3d 20 30 3b 0a 20 20 20 20 70 53 65  uth = 0;.    pSe
1f035 6c 54 61 62 20 3d 20 73 71 6c 69 74 65 33 52 65  lTab = sqlite3Re
1f036 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28  sultSetOfSelect(
1f037 70 50 61 72 73 65 2c 20 70 53 65 6c 29 3b 0a 20  pParse, pSel);. 
1f038 20 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 78     db->xAuth = x
1f039 41 75 74 68 3b 0a 23 65 6c 73 65 0a 20 20 20 20  Auth;.#else.    
1f03a 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 74 65  pSelTab = sqlite
1f03b 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65  3ResultSetOfSele
1f03c 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 29  ct(pParse, pSel)
1f03d 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 50 61  ;.#endif.    pPa
1f03e 72 73 65 2d 3e 6e 54 61 62 20 3d 20 6e 3b 0a 20  rse->nTab = n;. 
1f03f 20 20 20 69 66 28 20 70 53 65 6c 54 61 62 20 29     if( pSelTab )
1f040 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
1f041 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 3d 3d 30 20  pTable->aCol==0 
1f042 29 3b 0a 20 20 20 20 20 20 70 54 61 62 6c 65 2d  );.      pTable-
1f043 3e 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d  >nCol = pSelTab-
1f044 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 70 54 61  >nCol;.      pTa
1f045 62 6c 65 2d 3e 61 43 6f 6c 20 3d 20 70 53 65 6c  ble->aCol = pSel
1f046 54 61 62 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20 20  Tab->aCol;.     
1f047 20 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 20 3d   pSelTab->nCol =
1f048 20 30 3b 0a 20 20 20 20 20 20 70 53 65 6c 54 61   0;.      pSelTa
1f049 62 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20  b->aCol = 0;.   
1f04a 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65     sqlite3Delete
1f04b 54 61 62 6c 65 28 70 53 65 6c 54 61 62 29 3b 0a  Table(pSelTab);.
1f04c 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 70 53        pTable->pS
1f04d 63 68 65 6d 61 2d 3e 66 6c 61 67 73 20 7c 3d 20  chema->flags |= 
1f04e 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 3b  DB_UnresetViews;
1f04f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1f050 20 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d    pTable->nCol =
1f051 20 30 3b 0a 20 20 20 20 20 20 6e 45 72 72 2b 2b   0;.      nErr++
1f052 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
1f053 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
1f054 64 62 2c 20 70 53 65 6c 29 3b 0a 20 20 7d 20 65  db, pSel);.  } e
1f055 6c 73 65 20 7b 0a 20 20 20 20 6e 45 72 72 2b 2b  lse {.    nErr++
1f056 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;.  }.#endif /* 
1f057 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
1f058 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 6e 45 72   */.  return nEr
1f059 72 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  r;  .}.#endif /*
1f05a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1f05b 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21  _OMIT_VIEW) || !
1f05c 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
1f05d 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1f05e 29 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  ) */..#ifndef SQ
1f05f 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 2f  LITE_OMIT_VIEW./
1f060 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63  *.** Clear the c
1f061 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 72 6f 6d  olumn names from
1f062 20 65 76 65 72 79 20 56 49 45 57 20 69 6e 20 64   every VIEW in d
1f063 61 74 61 62 61 73 65 20 69 64 78 2e 0a 2a 2f 0a  atabase idx..*/.
1f064 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69  static void sqli
1f065 74 65 56 69 65 77 52 65 73 65 74 41 6c 6c 28 73  teViewResetAll(s
1f066 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
1f067 69 64 78 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d  idx){.  HashElem
1f068 20 2a 69 3b 0a 20 20 69 66 28 20 21 44 62 48 61   *i;.  if( !DbHa
1f069 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 64  sProperty(db, id
1f06a 78 2c 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65  x, DB_UnresetVie
1f06b 77 73 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ws) ) return;.  
1f06c 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68  for(i=sqliteHash
1f06d 46 69 72 73 74 28 26 64 62 2d 3e 61 44 62 5b 69  First(&db->aDb[i
1f06e 64 78 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c  dx].pSchema->tbl
1f06f 48 61 73 68 29 3b 20 69 3b 69 3d 73 71 6c 69 74  Hash); i;i=sqlit
1f070 65 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20  eHashNext(i)){. 
1f071 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
1f072 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28   sqliteHashData(
1f073 69 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62  i);.    if( pTab
1f074 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ->pSelect ){.   
1f075 20 20 20 73 71 6c 69 74 65 52 65 73 65 74 43 6f     sqliteResetCo
1f076 6c 75 6d 6e 4e 61 6d 65 73 28 70 54 61 62 29 3b  lumnNames(pTab);
1f077 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 44 62 43  .    }.  }.  DbC
1f078 6c 65 61 72 50 72 6f 70 65 72 74 79 28 64 62 2c  learProperty(db,
1f079 20 69 64 78 2c 20 44 42 5f 55 6e 72 65 73 65 74   idx, DB_Unreset
1f07a 56 69 65 77 73 29 3b 0a 7d 0a 23 65 6c 73 65 0a  Views);.}.#else.
1f07b 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 56  # define sqliteV
1f07c 69 65 77 52 65 73 65 74 41 6c 6c 28 41 2c 42 29  iewResetAll(A,B)
1f07d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1f07e 45 5f 4f 4d 49 54 5f 56 49 45 57 20 2a 2f 0a 0a  E_OMIT_VIEW */..
1f07f 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
1f080 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79  ion is called by
1f081 20 74 68 65 20 56 44 42 45 20 74 6f 20 61 64 6a   the VDBE to adj
1f082 75 73 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  ust the internal
1f083 20 73 63 68 65 6d 61 0a 2a 2a 20 75 73 65 64 20   schema.** used 
1f084 62 79 20 53 51 4c 69 74 65 20 77 68 65 6e 20 74  by SQLite when t
1f085 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 6d  he btree layer m
1f086 6f 76 65 73 20 61 20 74 61 62 6c 65 20 72 6f 6f  oves a table roo
1f087 74 20 70 61 67 65 2e 20 54 68 65 0a 2a 2a 20 72  t page. The.** r
1f088 6f 6f 74 2d 70 61 67 65 20 6f 66 20 61 20 74 61  oot-page of a ta
1f089 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 6e 20  ble or index in 
1f08a 64 61 74 61 62 61 73 65 20 69 44 62 20 68 61 73  database iDb has
1f08b 20 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 69 46   changed from iF
1f08c 72 6f 6d 0a 2a 2a 20 74 6f 20 69 54 6f 2e 0a 2a  rom.** to iTo..*
1f08d 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 31 37 32  *.** Ticket #172
1f08e 38 3a 20 20 54 68 65 20 73 79 6d 62 6f 6c 20 74  8:  The symbol t
1f08f 61 62 6c 65 20 6d 69 67 68 74 20 73 74 69 6c 6c  able might still
1f090 20 63 6f 6e 74 61 69 6e 20 69 6e 66 6f 72 6d 61   contain informa
1f091 74 69 6f 6e 0a 2a 2a 20 6f 6e 20 74 61 62 6c 65  tion.** on table
1f092 73 20 61 6e 64 2f 6f 72 20 69 6e 64 69 63 65 73  s and/or indices
1f093 20 74 68 61 74 20 61 72 65 20 74 68 65 20 70 72   that are the pr
1f094 6f 63 65 73 73 20 6f 66 20 62 65 69 6e 67 20 64  ocess of being d
1f095 65 6c 65 74 65 64 2e 0a 2a 2a 20 49 66 20 79 6f  eleted..** If yo
1f096 75 20 61 72 65 20 75 6e 6c 75 63 6b 79 2c 20 6f  u are unlucky, o
1f097 6e 65 20 6f 66 20 74 68 6f 73 65 20 64 65 6c 65  ne of those dele
1f098 74 65 64 20 69 6e 64 69 63 65 73 20 6f 72 20 74  ted indices or t
1f099 61 62 6c 65 73 20 6d 69 67 68 74 0a 2a 2a 20 68  ables might.** h
1f09a 61 76 65 20 74 68 65 20 73 61 6d 65 20 72 6f 6f  ave the same roo
1f09b 74 70 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20  tpage number as 
1f09c 74 68 65 20 72 65 61 6c 20 74 61 62 6c 65 20 6f  the real table o
1f09d 72 20 69 6e 64 65 78 20 74 68 61 74 20 69 73 0a  r index that is.
1f09e 2a 2a 20 62 65 69 6e 67 20 6d 6f 76 65 64 2e 20  ** being moved. 
1f09f 20 53 6f 20 77 65 20 63 61 6e 6e 6f 74 20 73 74   So we cannot st
1f0a0 6f 70 20 73 65 61 72 63 68 69 6e 67 20 61 66 74  op searching aft
1f0a1 65 72 20 74 68 65 20 66 69 72 73 74 20 6d 61 74  er the first mat
1f0a2 63 68 20 0a 2a 2a 20 62 65 63 61 75 73 65 20 74  ch .** because t
1f0a3 68 65 20 66 69 72 73 74 20 6d 61 74 63 68 20 6d  he first match m
1f0a4 69 67 68 74 20 62 65 20 66 6f 72 20 6f 6e 65 20  ight be for one 
1f0a5 6f 66 20 74 68 65 20 64 65 6c 65 74 65 64 20 69  of the deleted i
1f0a6 6e 64 69 63 65 73 0a 2a 2a 20 6f 72 20 74 61 62  ndices.** or tab
1f0a7 6c 65 73 20 61 6e 64 20 6e 6f 74 20 74 68 65 20  les and not the 
1f0a8 74 61 62 6c 65 2f 69 6e 64 65 78 20 74 68 61 74  table/index that
1f0a9 20 69 73 20 61 63 74 75 61 6c 6c 79 20 62 65 69   is actually bei
1f0aa 6e 67 20 6d 6f 76 65 64 2e 0a 2a 2a 20 57 65 20  ng moved..** We 
1f0ab 6d 75 73 74 20 63 6f 6e 74 69 6e 75 65 20 6c 6f  must continue lo
1f0ac 6f 70 69 6e 67 20 75 6e 74 69 6c 20 61 6c 6c 20  oping until all 
1f0ad 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63  tables and indic
1f0ae 65 73 20 77 69 74 68 0a 2a 2a 20 72 6f 6f 74 70  es with.** rootp
1f0af 61 67 65 3d 3d 69 46 72 6f 6d 20 68 61 76 65 20  age==iFrom have 
1f0b0 62 65 65 6e 20 63 6f 6e 76 65 72 74 65 64 20 74  been converted t
1f0b1 6f 20 68 61 76 65 20 61 20 72 6f 6f 74 70 61 67  o have a rootpag
1f0b2 65 20 6f 66 20 69 54 6f 0a 2a 2a 20 69 6e 20 6f  e of iTo.** in o
1f0b3 72 64 65 72 20 74 6f 20 62 65 20 63 65 72 74 61  rder to be certa
1f0b4 69 6e 20 74 68 61 74 20 77 65 20 67 6f 74 20 74  in that we got t
1f0b5 68 65 20 72 69 67 68 74 20 6f 6e 65 2e 0a 2a 2f  he right one..*/
1f0b6 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1f0b7 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
1f0b8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1f0b9 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6f 74 50  oid sqlite3RootP
1f0ba 61 67 65 4d 6f 76 65 64 28 44 62 20 2a 70 44 62  ageMoved(Db *pDb
1f0bb 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74  , int iFrom, int
1f0bc 20 69 54 6f 29 7b 0a 20 20 48 61 73 68 45 6c 65   iTo){.  HashEle
1f0bd 6d 20 2a 70 45 6c 65 6d 3b 0a 20 20 48 61 73 68  m *pElem;.  Hash
1f0be 20 2a 70 48 61 73 68 3b 0a 0a 20 20 70 48 61 73   *pHash;..  pHas
1f0bf 68 20 3d 20 26 70 44 62 2d 3e 70 53 63 68 65 6d  h = &pDb->pSchem
1f0c0 61 2d 3e 74 62 6c 48 61 73 68 3b 0a 20 20 66 6f  a->tblHash;.  fo
1f0c1 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61  r(pElem=sqliteHa
1f0c2 73 68 46 69 72 73 74 28 70 48 61 73 68 29 3b 20  shFirst(pHash); 
1f0c3 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c  pElem; pElem=sql
1f0c4 69 74 65 48 61 73 68 4e 65 78 74 28 70 45 6c 65  iteHashNext(pEle
1f0c5 6d 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a  m)){.    Table *
1f0c6 70 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73  pTab = sqliteHas
1f0c7 68 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a 20 20  hData(pElem);.  
1f0c8 20 20 69 66 28 20 70 54 61 62 2d 3e 74 6e 75 6d    if( pTab->tnum
1f0c9 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20  ==iFrom ){.     
1f0ca 20 70 54 61 62 2d 3e 74 6e 75 6d 20 3d 20 69 54   pTab->tnum = iT
1f0cb 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  o;.    }.  }.  p
1f0cc 48 61 73 68 20 3d 20 26 70 44 62 2d 3e 70 53 63  Hash = &pDb->pSc
1f0cd 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0a 20  hema->idxHash;. 
1f0ce 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74   for(pElem=sqlit
1f0cf 65 48 61 73 68 46 69 72 73 74 28 70 48 61 73 68  eHashFirst(pHash
1f0d0 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d  ); pElem; pElem=
1f0d1 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70  sqliteHashNext(p
1f0d2 45 6c 65 6d 29 29 7b 0a 20 20 20 20 49 6e 64 65  Elem)){.    Inde
1f0d3 78 20 2a 70 49 64 78 20 3d 20 73 71 6c 69 74 65  x *pIdx = sqlite
1f0d4 48 61 73 68 44 61 74 61 28 70 45 6c 65 6d 29 3b  HashData(pElem);
1f0d5 0a 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 74  .    if( pIdx->t
1f0d6 6e 75 6d 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20  num==iFrom ){.  
1f0d7 20 20 20 20 70 49 64 78 2d 3e 74 6e 75 6d 20 3d      pIdx->tnum =
1f0d8 20 69 54 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   iTo;.    }.  }.
1f0d9 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
1f0da 57 72 69 74 65 20 63 6f 64 65 20 74 6f 20 65 72  Write code to er
1f0db 61 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69  ase the table wi
1f0dc 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61  th root-page iTa
1f0dd 62 6c 65 20 66 72 6f 6d 20 64 61 74 61 62 61 73  ble from databas
1f0de 65 20 69 44 62 2e 0a 2a 2a 20 41 6c 73 6f 20 77  e iDb..** Also w
1f0df 72 69 74 65 20 63 6f 64 65 20 74 6f 20 6d 6f 64  rite code to mod
1f0e0 69 66 79 20 74 68 65 20 73 71 6c 69 74 65 5f 6d  ify the sqlite_m
1f0e1 61 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20  aster table and 
1f0e2 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 0a  internal schema.
1f0e3 2a 2a 20 69 66 20 61 20 72 6f 6f 74 2d 70 61 67  ** if a root-pag
1f0e4 65 20 6f 66 20 61 6e 6f 74 68 65 72 20 74 61 62  e of another tab
1f0e5 6c 65 20 69 73 20 6d 6f 76 65 64 20 62 79 20 74  le is moved by t
1f0e6 68 65 20 62 74 72 65 65 2d 6c 61 79 65 72 20 77  he btree-layer w
1f0e7 68 69 6c 73 74 0a 2a 2a 20 65 72 61 73 69 6e 67  hilst.** erasing
1f0e8 20 69 54 61 62 6c 65 20 28 74 68 69 73 20 63 61   iTable (this ca
1f0e9 6e 20 68 61 70 70 65 6e 20 77 69 74 68 20 61 6e  n happen with an
1f0ea 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74   auto-vacuum dat
1f0eb 61 62 61 73 65 29 2e 0a 2a 2f 20 0a 73 74 61 74  abase)..*/ .stat
1f0ec 69 63 20 76 6f 69 64 20 64 65 73 74 72 6f 79 52  ic void destroyR
1f0ed 6f 6f 74 50 61 67 65 28 50 61 72 73 65 20 2a 70  ootPage(Parse *p
1f0ee 50 61 72 73 65 2c 20 69 6e 74 20 69 54 61 62 6c  Parse, int iTabl
1f0ef 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56  e, int iDb){.  V
1f0f0 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33  dbe *v = sqlite3
1f0f1 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
1f0f2 0a 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69  .  int r1 = sqli
1f0f3 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
1f0f4 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  arse);.  sqlite3
1f0f5 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
1f0f6 5f 44 65 73 74 72 6f 79 2c 20 69 54 61 62 6c 65  _Destroy, iTable
1f0f7 2c 20 72 31 2c 20 69 44 62 29 3b 0a 23 69 66 6e  , r1, iDb);.#ifn
1f0f8 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1f0f9 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 2f 2a 20  AUTOVACUUM.  /* 
1f0fa 4f 50 5f 44 65 73 74 72 6f 79 20 73 74 6f 72 65  OP_Destroy store
1f0fb 73 20 61 6e 20 69 6e 20 69 6e 74 65 67 65 72 20  s an in integer 
1f0fc 72 31 2e 20 49 66 20 74 68 69 73 20 69 6e 74 65  r1. If this inte
1f0fd 67 65 72 0a 20 20 2a 2a 20 69 73 20 6e 6f 6e 2d  ger.  ** is non-
1f0fe 7a 65 72 6f 2c 20 74 68 65 6e 20 69 74 20 69 73  zero, then it is
1f0ff 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e   the root page n
1f100 75 6d 62 65 72 20 6f 66 20 61 20 74 61 62 6c 65  umber of a table
1f101 20 6d 6f 76 65 64 20 74 6f 0a 20 20 2a 2a 20 6c   moved to.  ** l
1f102 6f 63 61 74 69 6f 6e 20 69 54 61 62 6c 65 2e 20  ocation iTable. 
1f103 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  The following co
1f104 64 65 20 6d 6f 64 69 66 69 65 73 20 74 68 65 20  de modifies the 
1f105 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
1f106 62 6c 65 20 74 6f 0a 20 20 2a 2a 20 72 65 66 6c  ble to.  ** refl
1f107 65 63 74 20 74 68 69 73 2e 0a 20 20 2a 2a 0a 20  ect this..  **. 
1f108 20 2a 2a 20 54 68 65 20 22 23 25 64 22 20 69 6e   ** The "#%d" in
1f109 20 74 68 65 20 53 51 4c 20 69 73 20 61 20 73 70   the SQL is a sp
1f10a 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20 74  ecial constant t
1f10b 68 61 74 20 6d 65 61 6e 73 20 77 68 61 74 65 76  hat means whatev
1f10c 65 72 20 76 61 6c 75 65 0a 20 20 2a 2a 20 69 73  er value.  ** is
1f10d 20 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74   on the top of t
1f10e 68 65 20 73 74 61 63 6b 2e 20 20 53 65 65 20 73  he stack.  See s
1f10f 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 45 78  qlite3RegisterEx
1f110 70 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  pr()..  */.  sql
1f111 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28  ite3NestedParse(
1f112 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 22 55  pParse, .     "U
1f113 50 44 41 54 45 20 25 51 2e 25 73 20 53 45 54 20  PDATE %Q.%s SET 
1f114 72 6f 6f 74 70 61 67 65 3d 25 64 20 57 48 45 52  rootpage=%d WHER
1f115 45 20 23 25 64 20 41 4e 44 20 72 6f 6f 74 70 61  E #%d AND rootpa
1f116 67 65 3d 23 25 64 22 2c 0a 20 20 20 20 20 70 50  ge=#%d",.     pP
1f117 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44  arse->db->aDb[iD
1f118 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41  b].zName, SCHEMA
1f119 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 69 54 61  _TABLE(iDb), iTa
1f11a 62 6c 65 2c 20 72 31 2c 20 72 31 29 3b 0a 23 65  ble, r1, r1);.#e
1f11b 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 52 65  ndif.  sqlite3Re
1f11c 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
1f11d 72 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a  rse, r1);.}../*.
1f11e 2a 2a 20 57 72 69 74 65 20 56 44 42 45 20 63 6f  ** Write VDBE co
1f11f 64 65 20 74 6f 20 65 72 61 73 65 20 74 61 62 6c  de to erase tabl
1f120 65 20 70 54 61 62 20 61 6e 64 20 61 6c 6c 20 61  e pTab and all a
1f121 73 73 6f 63 69 61 74 65 64 20 69 6e 64 69 63 65  ssociated indice
1f122 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 20 43 6f  s on disk..** Co
1f123 64 65 20 74 6f 20 75 70 64 61 74 65 20 74 68 65  de to update the
1f124 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
1f125 61 62 6c 65 73 20 61 6e 64 20 69 6e 74 65 72 6e  ables and intern
1f126 61 6c 20 73 63 68 65 6d 61 20 64 65 66 69 6e 69  al schema defini
1f127 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 63 61 73 65  tions.** in case
1f128 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 62 65 6c   a root-page bel
1f129 6f 6e 67 69 6e 67 20 74 6f 20 61 6e 6f 74 68 65  onging to anothe
1f12a 72 20 74 61 62 6c 65 20 69 73 20 6d 6f 76 65 64  r table is moved
1f12b 20 62 79 20 74 68 65 20 62 74 72 65 65 20 6c 61   by the btree la
1f12c 79 65 72 0a 2a 2a 20 69 73 20 61 6c 73 6f 20 61  yer.** is also a
1f12d 64 64 65 64 20 28 74 68 69 73 20 63 61 6e 20 68  dded (this can h
1f12e 61 70 70 65 6e 20 77 69 74 68 20 61 6e 20 61 75  appen with an au
1f12f 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61  to-vacuum databa
1f130 73 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  se)..*/.static v
1f131 6f 69 64 20 64 65 73 74 72 6f 79 54 61 62 6c 65  oid destroyTable
1f132 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1f133 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 23 69  Table *pTab){.#i
1f134 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
1f135 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 49 6e  _AUTOVACUUM.  In
1f136 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 69 6e 74  dex *pIdx;.  int
1f137 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
1f138 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72  hemaToIndex(pPar
1f139 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53  se->db, pTab->pS
1f13a 63 68 65 6d 61 29 3b 0a 20 20 64 65 73 74 72 6f  chema);.  destro
1f13b 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65  yRootPage(pParse
1f13c 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69 44  , pTab->tnum, iD
1f13d 62 29 3b 0a 20 20 66 6f 72 28 70 49 64 78 3d 70  b);.  for(pIdx=p
1f13e 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
1f13f 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
1f140 65 78 74 29 7b 0a 20 20 20 20 64 65 73 74 72 6f  ext){.    destro
1f141 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65  yRootPage(pParse
1f142 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44  , pIdx->tnum, iD
1f143 62 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20  b);.  }.#else.  
1f144 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61  /* If the databa
1f145 73 65 20 6d 61 79 20 62 65 20 61 75 74 6f 2d 76  se may be auto-v
1f146 61 63 75 75 6d 20 63 61 70 61 62 6c 65 20 28 69  acuum capable (i
1f147 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
1f148 54 4f 56 41 43 55 55 4d 0a 20 20 2a 2a 20 69 73  TOVACUUM.  ** is
1f149 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 74   not defined), t
1f14a 68 65 6e 20 69 74 20 69 73 20 69 6d 70 6f 72 74  hen it is import
1f14b 61 6e 74 20 74 6f 20 63 61 6c 6c 20 4f 50 5f 44  ant to call OP_D
1f14c 65 73 74 72 6f 79 20 6f 6e 20 74 68 65 0a 20 20  estroy on the.  
1f14d 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64  ** table and ind
1f14e 65 78 20 72 6f 6f 74 2d 70 61 67 65 73 20 69 6e  ex root-pages in
1f14f 20 6f 72 64 65 72 2c 20 73 74 61 72 74 69 6e 67   order, starting
1f150 20 77 69 74 68 20 74 68 65 20 6e 75 6d 65 72 69   with the numeri
1f151 63 61 6c 6c 79 20 0a 20 20 2a 2a 20 6c 61 72 67  cally .  ** larg
1f152 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75  est root-page nu
1f153 6d 62 65 72 2e 20 54 68 69 73 20 67 75 61 72 61  mber. This guara
1f154 6e 74 65 65 73 20 74 68 61 74 20 6e 6f 6e 65 20  ntees that none 
1f155 6f 66 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65  of the root-page
1f156 73 0a 20 20 2a 2a 20 74 6f 20 62 65 20 64 65 73  s.  ** to be des
1f157 74 72 6f 79 65 64 20 69 73 20 72 65 6c 6f 63 61  troyed is reloca
1f158 74 65 64 20 62 79 20 61 6e 20 65 61 72 6c 69 65  ted by an earlie
1f159 72 20 4f 50 5f 44 65 73 74 72 6f 79 2e 20 69 2e  r OP_Destroy. i.
1f15a 65 2e 20 69 66 20 74 68 65 0a 20 20 2a 2a 20 66  e. if the.  ** f
1f15b 6f 6c 6c 6f 77 69 6e 67 20 77 65 72 65 20 63 6f  ollowing were co
1f15c 64 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f  ded:.  **.  ** O
1f15d 50 5f 44 65 73 74 72 6f 79 20 34 20 30 0a 20 20  P_Destroy 4 0.  
1f15e 2a 2a 20 2e 2e 2e 0a 20 20 2a 2a 20 4f 50 5f 44  ** ....  ** OP_D
1f15f 65 73 74 72 6f 79 20 35 20 30 0a 20 20 2a 2a 0a  estroy 5 0.  **.
1f160 20 20 2a 2a 20 61 6e 64 20 72 6f 6f 74 20 70 61    ** and root pa
1f161 67 65 20 35 20 68 61 70 70 65 6e 65 64 20 74 6f  ge 5 happened to
1f162 20 62 65 20 74 68 65 20 6c 61 72 67 65 73 74 20   be the largest 
1f163 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72  root-page number
1f164 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74   in the.  ** dat
1f165 61 62 61 73 65 2c 20 74 68 65 6e 20 72 6f 6f 74  abase, then root
1f166 20 70 61 67 65 20 35 20 77 6f 75 6c 64 20 62 65   page 5 would be
1f167 20 6d 6f 76 65 64 20 74 6f 20 70 61 67 65 20 34   moved to page 4
1f168 20 62 79 20 74 68 65 20 0a 20 20 2a 2a 20 22 4f   by the .  ** "O
1f169 50 5f 44 65 73 74 72 6f 79 20 34 20 30 22 20 6f  P_Destroy 4 0" o
1f16a 70 63 6f 64 65 2e 20 54 68 65 20 73 75 62 73 65  pcode. The subse
1f16b 71 75 65 6e 74 20 22 4f 50 5f 44 65 73 74 72 6f  quent "OP_Destro
1f16c 79 20 35 20 30 22 20 77 6f 75 6c 64 20 68 69 74  y 5 0" would hit
1f16d 0a 20 20 2a 2a 20 61 20 66 72 65 65 2d 6c 69 73  .  ** a free-lis
1f16e 74 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69  t page..  */.  i
1f16f 6e 74 20 69 54 61 62 20 3d 20 70 54 61 62 2d 3e  nt iTab = pTab->
1f170 74 6e 75 6d 3b 0a 20 20 69 6e 74 20 69 44 65 73  tnum;.  int iDes
1f171 74 72 6f 79 65 64 20 3d 20 30 3b 0a 0a 20 20 77  troyed = 0;..  w
1f172 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 49  hile( 1 ){.    I
1f173 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20  ndex *pIdx;.    
1f174 69 6e 74 20 69 4c 61 72 67 65 73 74 20 3d 20 30  int iLargest = 0
1f175 3b 0a 0a 20 20 20 20 69 66 28 20 69 44 65 73 74  ;..    if( iDest
1f176 72 6f 79 65 64 3d 3d 30 20 7c 7c 20 69 54 61 62  royed==0 || iTab
1f177 3c 69 44 65 73 74 72 6f 79 65 64 20 29 7b 0a 20  <iDestroyed ){. 
1f178 20 20 20 20 20 69 4c 61 72 67 65 73 74 20 3d 20       iLargest = 
1f179 69 54 61 62 3b 0a 20 20 20 20 7d 0a 20 20 20 20  iTab;.    }.    
1f17a 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70  for(pIdx=pTab->p
1f17b 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
1f17c 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  x=pIdx->pNext){.
1f17d 20 20 20 20 20 20 69 6e 74 20 69 49 64 78 20 3d        int iIdx =
1f17e 20 70 49 64 78 2d 3e 74 6e 75 6d 3b 0a 20 20 20   pIdx->tnum;.   
1f17f 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d     assert( pIdx-
1f180 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e  >pSchema==pTab->
1f181 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 20  pSchema );.     
1f182 20 69 66 28 20 28 69 44 65 73 74 72 6f 79 65 64   if( (iDestroyed
1f183 3d 3d 30 20 7c 7c 20 28 69 49 64 78 3c 69 44 65  ==0 || (iIdx<iDe
1f184 73 74 72 6f 79 65 64 29 29 20 26 26 20 69 49 64  stroyed)) && iId
1f185 78 3e 69 4c 61 72 67 65 73 74 20 29 7b 0a 20 20  x>iLargest ){.  
1f186 20 20 20 20 20 20 69 4c 61 72 67 65 73 74 20 3d        iLargest =
1f187 20 69 49 64 78 3b 0a 20 20 20 20 20 20 7d 0a 20   iIdx;.      }. 
1f188 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 4c 61     }.    if( iLa
1f189 72 67 65 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20  rgest==0 ){.    
1f18a 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 65    return;.    }e
1f18b 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  lse{.      int i
1f18c 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
1f18d 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65  maToIndex(pParse
1f18e 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  ->db, pTab->pSch
1f18f 65 6d 61 29 3b 0a 20 20 20 20 20 20 64 65 73 74  ema);.      dest
1f190 72 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72  royRootPage(pPar
1f191 73 65 2c 20 69 4c 61 72 67 65 73 74 2c 20 69 44  se, iLargest, iD
1f192 62 29 3b 0a 20 20 20 20 20 20 69 44 65 73 74 72  b);.      iDestr
1f193 6f 79 65 64 20 3d 20 69 4c 61 72 67 65 73 74 3b  oyed = iLargest;
1f194 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
1f195 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  f.}../*.** This 
1f196 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
1f197 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b  d to do the work
1f198 20 6f 66 20 61 20 44 52 4f 50 20 54 41 42 4c 45   of a DROP TABLE
1f199 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 70   statement..** p
1f19a 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65  Name is the name
1f19b 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f   of the table to
1f19c 20 62 65 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a   be dropped..*/.
1f19d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1f19e 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54  oid sqlite3DropT
1f19f 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72  able(Parse *pPar
1f1a0 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e 61  se, SrcList *pNa
1f1a1 6d 65 2c 20 69 6e 74 20 69 73 56 69 65 77 2c 20  me, int isView, 
1f1a2 69 6e 74 20 6e 6f 45 72 72 29 7b 0a 20 20 54 61  int noErr){.  Ta
1f1a3 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 56 64 62  ble *pTab;.  Vdb
1f1a4 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 20  e *v;.  sqlite3 
1f1a5 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
1f1a6 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20 20  ;.  int iDb;..  
1f1a7 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
1f1a8 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
1f1a9 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f  iled ){.    goto
1f1aa 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65   exit_drop_table
1f1ab 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
1f1ac 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29  pName->nSrc==1 )
1f1ad 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74  ;.  pTab = sqlit
1f1ae 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50  e3LocateTable(pP
1f1af 61 72 73 65 2c 20 69 73 56 69 65 77 2c 20 0a 20  arse, isView, . 
1f1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f1b1 20 20 20 20 20 20 20 20 20 20 20 70 4e 61 6d 65             pName
1f1b2 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 4e  ->a[0].zName, pN
1f1b3 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62  ame->a[0].zDatab
1f1b4 61 73 65 29 3b 0a 0a 20 20 69 66 28 20 70 54 61  ase);..  if( pTa
1f1b5 62 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  b==0 ){.    if( 
1f1b6 6e 6f 45 72 72 20 29 7b 0a 20 20 20 20 20 20 73  noErr ){.      s
1f1b7 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72  qlite3ErrorClear
1f1b8 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 7d 0a  (pParse);.    }.
1f1b9 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72      goto exit_dr
1f1ba 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20  op_table;.  }.  
1f1bb 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
1f1bc 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70  emaToIndex(db, p
1f1bd 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  Tab->pSchema);. 
1f1be 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20   assert( iDb>=0 
1f1bf 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29  && iDb<db->nDb )
1f1c0 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 54 61 62 20  ;..  /* If pTab 
1f1c1 69 73 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  is a virtual tab
1f1c2 6c 65 2c 20 63 61 6c 6c 20 56 69 65 77 47 65 74  le, call ViewGet
1f1c3 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29 20 74 6f  ColumnNames() to
1f1c4 20 65 6e 73 75 72 65 0a 20 20 2a 2a 20 69 74 20   ensure.  ** it 
1f1c5 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a  is initialized..
1f1c6 20 20 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72    */.  if( IsVir
1f1c7 74 75 61 6c 28 70 54 61 62 29 20 26 26 20 73 71  tual(pTab) && sq
1f1c8 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75  lite3ViewGetColu
1f1c9 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20  mnNames(pParse, 
1f1ca 70 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74  pTab) ){.    got
1f1cb 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c  o exit_drop_tabl
1f1cc 65 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  e;.  }.#ifndef S
1f1cd 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
1f1ce 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20  RIZATION.  {.   
1f1cf 20 69 6e 74 20 63 6f 64 65 3b 0a 20 20 20 20 63   int code;.    c
1f1d0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20  onst char *zTab 
1f1d1 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  = SCHEMA_TABLE(i
1f1d2 44 62 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  Db);.    const c
1f1d3 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61  har *zDb = db->a
1f1d4 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20  Db[iDb].zName;. 
1f1d5 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
1f1d6 41 72 67 32 20 3d 20 30 3b 0a 20 20 20 20 69 66  Arg2 = 0;.    if
1f1d7 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  ( sqlite3AuthChe
1f1d8 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54  ck(pParse, SQLIT
1f1d9 45 5f 44 45 4c 45 54 45 2c 20 7a 54 61 62 2c 20  E_DELETE, zTab, 
1f1da 30 2c 20 7a 44 62 29 29 7b 0a 20 20 20 20 20 20  0, zDb)){.      
1f1db 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74  goto exit_drop_t
1f1dc 61 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20  able;.    }.    
1f1dd 69 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20  if( isView ){.  
1f1de 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45      if( !OMIT_TE
1f1df 4d 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29  MPDB && iDb==1 )
1f1e0 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d  {.        code =
1f1e1 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d   SQLITE_DROP_TEM
1f1e2 50 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 65  P_VIEW;.      }e
1f1e3 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64  lse{.        cod
1f1e4 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f  e = SQLITE_DROP_
1f1e5 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 0a 23 69  VIEW;.      }.#i
1f1e6 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1f1e7 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
1f1e8 20 20 20 7d 65 6c 73 65 20 69 66 28 20 49 73 56     }else if( IsV
1f1e9 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
1f1ea 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c        code = SQL
1f1eb 49 54 45 5f 44 52 4f 50 5f 56 54 41 42 4c 45 3b  ITE_DROP_VTABLE;
1f1ec 0a 20 20 20 20 20 20 7a 41 72 67 32 20 3d 20 70  .      zArg2 = p
1f1ed 54 61 62 2d 3e 70 4d 6f 64 2d 3e 7a 4e 61 6d 65  Tab->pMod->zName
1f1ee 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c  ;.#endif.    }el
1f1ef 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 21 4f  se{.      if( !O
1f1f0 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44  MIT_TEMPDB && iD
1f1f1 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  b==1 ){.        
1f1f2 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52  code = SQLITE_DR
1f1f3 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 3b 0a 20  OP_TEMP_TABLE;. 
1f1f4 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1f1f5 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54      code = SQLIT
1f1f6 45 5f 44 52 4f 50 5f 54 41 42 4c 45 3b 0a 20 20  E_DROP_TABLE;.  
1f1f7 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1f1f8 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43  if( sqlite3AuthC
1f1f9 68 65 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64  heck(pParse, cod
1f1fa 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  e, pTab->zName, 
1f1fb 7a 41 72 67 32 2c 20 7a 44 62 29 20 29 7b 0a 20  zArg2, zDb) ){. 
1f1fc 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64       goto exit_d
1f1fd 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 20 20 7d  rop_table;.    }
1f1fe 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1f1ff 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
1f200 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c  , SQLITE_DELETE,
1f201 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c   pTab->zName, 0,
1f202 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67   zDb) ){.      g
1f203 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61  oto exit_drop_ta
1f204 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  ble;.    }.  }.#
1f205 65 6e 64 69 66 0a 20 20 69 66 28 20 73 71 6c 69  endif.  if( sqli
1f206 74 65 33 53 74 72 4e 49 43 6d 70 28 70 54 61 62  te3StrNICmp(pTab
1f207 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65  ->zName, "sqlite
1f208 5f 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 20 20  _", 7)==0 ){.   
1f209 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1f20a 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20  (pParse, "table 
1f20b 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 64 72  %s may not be dr
1f20c 6f 70 70 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e  opped", pTab->zN
1f20d 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  ame);.    goto e
1f20e 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a  xit_drop_table;.
1f20f 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
1f210 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20  ITE_OMIT_VIEW.  
1f211 2f 2a 20 45 6e 73 75 72 65 20 44 52 4f 50 20 54  /* Ensure DROP T
1f212 41 42 4c 45 20 69 73 20 6e 6f 74 20 75 73 65 64  ABLE is not used
1f213 20 6f 6e 20 61 20 76 69 65 77 2c 20 61 6e 64 20   on a view, and 
1f214 44 52 4f 50 20 56 49 45 57 20 69 73 20 6e 6f 74  DROP VIEW is not
1f215 20 75 73 65 64 0a 20 20 2a 2a 20 6f 6e 20 61 20   used.  ** on a 
1f216 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  table..  */.  if
1f217 28 20 69 73 56 69 65 77 20 26 26 20 70 54 61 62  ( isView && pTab
1f218 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 7b 0a  ->pSelect==0 ){.
1f219 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1f21a 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75 73 65  Msg(pParse, "use
1f21b 20 44 52 4f 50 20 54 41 42 4c 45 20 74 6f 20 64   DROP TABLE to d
1f21c 65 6c 65 74 65 20 74 61 62 6c 65 20 25 73 22 2c  elete table %s",
1f21d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pTab->zName);. 
1f21e 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f     goto exit_dro
1f21f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69  p_table;.  }.  i
1f220 66 28 20 21 69 73 56 69 65 77 20 26 26 20 70 54  f( !isView && pT
1f221 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20  ab->pSelect ){. 
1f222 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1f223 73 67 28 70 50 61 72 73 65 2c 20 22 75 73 65 20  sg(pParse, "use 
1f224 44 52 4f 50 20 56 49 45 57 20 74 6f 20 64 65 6c  DROP VIEW to del
1f225 65 74 65 20 76 69 65 77 20 25 73 22 2c 20 70 54  ete view %s", pT
1f226 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ab->zName);.    
1f227 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74  goto exit_drop_t
1f228 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  able;.  }.#endif
1f229 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
1f22a 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65 20 74  code to remove t
1f22b 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 74 68  he table from th
1f22c 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 0a 20  e master table. 
1f22d 20 2a 2a 20 6f 6e 20 64 69 73 6b 2e 0a 20 20 2a   ** on disk..  *
1f22e 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  /.  v = sqlite3G
1f22f 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
1f230 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 54    if( v ){.    T
1f231 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72  rigger *pTrigger
1f232 3b 0a 20 20 20 20 44 62 20 2a 70 44 62 20 3d 20  ;.    Db *pDb = 
1f233 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20  &db->aDb[iDb];. 
1f234 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57     sqlite3BeginW
1f235 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50  riteOperation(pP
1f236 61 72 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a 0a  arse, 1, iDb);..
1f237 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1f238 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1f239 0a 20 20 20 20 69 66 28 20 49 73 56 69 72 74 75  .    if( IsVirtu
1f23a 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
1f23b 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69    Vdbe *v = sqli
1f23c 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
1f23d 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 76 20  e);.      if( v 
1f23e 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1f23f 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20  e3VdbeAddOp0(v, 
1f240 4f 50 5f 56 42 65 67 69 6e 29 3b 0a 20 20 20 20  OP_VBegin);.    
1f241 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66    }.    }.#endif
1f242 0a 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 61 6c  ..    /* Drop al
1f243 6c 20 74 72 69 67 67 65 72 73 20 61 73 73 6f 63  l triggers assoc
1f244 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 74  iated with the t
1f245 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70  able being dropp
1f246 65 64 2e 20 43 6f 64 65 0a 20 20 20 20 2a 2a 20  ed. Code.    ** 
1f247 69 73 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20  is generated to 
1f248 72 65 6d 6f 76 65 20 65 6e 74 72 69 65 73 20 66  remove entries f
1f249 72 6f 6d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  rom sqlite_maste
1f24a 72 20 61 6e 64 2f 6f 72 0a 20 20 20 20 2a 2a 20  r and/or.    ** 
1f24b 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74  sqlite_temp_mast
1f24c 65 72 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a  er if required..
1f24d 20 20 20 20 2a 2f 0a 20 20 20 20 70 54 72 69 67      */.    pTrig
1f24e 67 65 72 20 3d 20 70 54 61 62 2d 3e 70 54 72 69  ger = pTab->pTri
1f24f 67 67 65 72 3b 0a 20 20 20 20 77 68 69 6c 65 28  gger;.    while(
1f250 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20   pTrigger ){.   
1f251 20 20 20 61 73 73 65 72 74 28 20 70 54 72 69 67     assert( pTrig
1f252 67 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54  ger->pSchema==pT
1f253 61 62 2d 3e 70 53 63 68 65 6d 61 20 7c 7c 20 0a  ab->pSchema || .
1f254 20 20 20 20 20 20 20 20 20 20 70 54 72 69 67 67            pTrigg
1f255 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d  er->pSchema==db-
1f256 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20  >aDb[1].pSchema 
1f257 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1f258 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 70  DropTriggerPtr(p
1f259 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 29  Parse, pTrigger)
1f25a 3b 0a 20 20 20 20 20 20 70 54 72 69 67 67 65 72  ;.      pTrigger
1f25b 20 3d 20 70 54 72 69 67 67 65 72 2d 3e 70 4e 65   = pTrigger->pNe
1f25c 78 74 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64  xt;.    }..#ifnd
1f25d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1f25e 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20  UTOINCREMENT.   
1f25f 20 2f 2a 20 52 65 6d 6f 76 65 20 61 6e 79 20 65   /* Remove any e
1f260 6e 74 72 69 65 73 20 6f 66 20 74 68 65 20 73 71  ntries of the sq
1f261 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61  lite_sequence ta
1f262 62 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  ble associated w
1f263 69 74 68 0a 20 20 20 20 2a 2a 20 74 68 65 20 74  ith.    ** the t
1f264 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70  able being dropp
1f265 65 64 2e 20 54 68 69 73 20 69 73 20 64 6f 6e 65  ed. This is done
1f266 20 62 65 66 6f 72 65 20 74 68 65 20 74 61 62 6c   before the tabl
1f267 65 20 69 73 20 64 72 6f 70 70 65 64 0a 20 20 20  e is dropped.   
1f268 20 2a 2a 20 61 74 20 74 68 65 20 62 74 72 65 65   ** at the btree
1f269 20 6c 65 76 65 6c 2c 20 69 6e 20 63 61 73 65 20   level, in case 
1f26a 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65  the sqlite_seque
1f26b 6e 63 65 20 74 61 62 6c 65 20 6e 65 65 64 73 20  nce table needs 
1f26c 74 6f 0a 20 20 20 20 2a 2a 20 6d 6f 76 65 20 61  to.    ** move a
1f26d 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 74 68  s a result of th
1f26e 65 20 64 72 6f 70 20 28 63 61 6e 20 68 61 70 70  e drop (can happ
1f26f 65 6e 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75  en in auto-vacuu
1f270 6d 20 6d 6f 64 65 29 2e 0a 20 20 20 20 2a 2f 0a  m mode)..    */.
1f271 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e 74 61      if( pTab->ta
1f272 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f  bFlags & TF_Auto
1f273 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20 20  increment ){.   
1f274 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64     sqlite3Nested
1f275 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20  Parse(pParse,.  
1f276 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46 52        "DELETE FR
1f277 4f 4d 20 25 73 2e 73 71 6c 69 74 65 5f 73 65 71  OM %s.sqlite_seq
1f278 75 65 6e 63 65 20 57 48 45 52 45 20 6e 61 6d 65  uence WHERE name
1f279 3d 25 51 22 2c 0a 20 20 20 20 20 20 20 20 70 44  =%Q",.        pD
1f27a 62 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e  b->zName, pTab->
1f27b 7a 4e 61 6d 65 0a 20 20 20 20 20 20 29 3b 0a 20  zName.      );. 
1f27c 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
1f27d 20 2f 2a 20 44 72 6f 70 20 61 6c 6c 20 53 51 4c   /* Drop all SQL
1f27e 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65  ITE_MASTER table
1f27f 20 61 6e 64 20 69 6e 64 65 78 20 65 6e 74 72 69   and index entri
1f280 65 73 20 74 68 61 74 20 72 65 66 65 72 20 74 6f  es that refer to
1f281 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 61 62 6c   the.    ** tabl
1f282 65 2e 20 54 68 65 20 70 72 6f 67 72 61 6d 20 6e  e. The program n
1f283 61 6d 65 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67  ame loops throug
1f284 68 20 74 68 65 20 6d 61 73 74 65 72 20 74 61 62  h the master tab
1f285 6c 65 20 61 6e 64 20 64 65 6c 65 74 65 73 0a 20  le and deletes. 
1f286 20 20 20 2a 2a 20 65 76 65 72 79 20 72 6f 77 20     ** every row 
1f287 74 68 61 74 20 72 65 66 65 72 73 20 74 6f 20 61  that refers to a
1f288 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20 73 61   table of the sa
1f289 6d 65 20 6e 61 6d 65 20 61 73 20 74 68 65 20 6f  me name as the o
1f28a 6e 65 20 62 65 69 6e 67 0a 20 20 20 20 2a 2a 20  ne being.    ** 
1f28b 64 72 6f 70 70 65 64 2e 20 54 72 69 67 67 65 72  dropped. Trigger
1f28c 73 20 61 72 65 20 68 61 6e 64 6c 65 64 20 73 65  s are handled se
1f28d 70 65 72 61 74 65 6c 79 20 62 65 63 61 75 73 65  perately because
1f28e 20 61 20 74 72 69 67 67 65 72 20 63 61 6e 20 62   a trigger can b
1f28f 65 0a 20 20 20 20 2a 2a 20 63 72 65 61 74 65 64  e.    ** created
1f290 20 69 6e 20 74 68 65 20 74 65 6d 70 20 64 61 74   in the temp dat
1f291 61 62 61 73 65 20 74 68 61 74 20 72 65 66 65 72  abase that refer
1f292 73 20 74 6f 20 61 20 74 61 62 6c 65 20 69 6e 20  s to a table in 
1f293 61 6e 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 64  another.    ** d
1f294 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a  atabase..    */.
1f295 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65      sqlite3Neste
1f296 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a  dParse(pParse, .
1f297 20 20 20 20 20 20 20 20 22 44 45 4c 45 54 45 20          "DELETE 
1f298 46 52 4f 4d 20 25 51 2e 25 73 20 57 48 45 52 45  FROM %Q.%s WHERE
1f299 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 20 61 6e 64   tbl_name=%Q and
1f29a 20 74 79 70 65 21 3d 27 74 72 69 67 67 65 72 27   type!='trigger'
1f29b 22 2c 0a 20 20 20 20 20 20 20 20 70 44 62 2d 3e  ",.        pDb->
1f29c 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41  zName, SCHEMA_TA
1f29d 42 4c 45 28 69 44 62 29 2c 20 70 54 61 62 2d 3e  BLE(iDb), pTab->
1f29e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 20 20 2f 2a 20  zName);..    /* 
1f29f 44 72 6f 70 20 61 6e 79 20 73 74 61 74 69 73 74  Drop any statist
1f2a0 69 63 73 20 66 72 6f 6d 20 74 68 65 20 73 71 6c  ics from the sql
1f2a1 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2c  ite_stat1 table,
1f2a2 20 69 66 20 69 74 20 65 78 69 73 74 73 20 2a 2f   if it exists */
1f2a3 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1f2a4 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 22 73  FindTable(db, "s
1f2a5 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 20 64 62  qlite_stat1", db
1f2a6 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
1f2a7 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) ){.      sqlit
1f2a8 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50  e3NestedParse(pP
1f2a9 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 22 44  arse,.        "D
1f2aa 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 73 71  ELETE FROM %Q.sq
1f2ab 6c 69 74 65 5f 73 74 61 74 31 20 57 48 45 52 45  lite_stat1 WHERE
1f2ac 20 74 62 6c 3d 25 51 22 2c 20 70 44 62 2d 3e 7a   tbl=%Q", pDb->z
1f2ad 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  Name, pTab->zNam
1f2ae 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d  e.      );.    }
1f2af 0a 0a 20 20 20 20 69 66 28 20 21 69 73 56 69 65  ..    if( !isVie
1f2b0 77 20 26 26 20 21 49 73 56 69 72 74 75 61 6c 28  w && !IsVirtual(
1f2b1 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 64  pTab) ){.      d
1f2b2 65 73 74 72 6f 79 54 61 62 6c 65 28 70 50 61 72  estroyTable(pPar
1f2b3 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 7d  se, pTab);.    }
1f2b4 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20  ..    /* Remove 
1f2b5 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20  the table entry 
1f2b6 66 72 6f 6d 20 53 51 4c 69 74 65 27 73 20 69 6e  from SQLite's in
1f2b7 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 61 6e  ternal schema an
1f2b8 64 20 6d 6f 64 69 66 79 0a 20 20 20 20 2a 2a 20  d modify.    ** 
1f2b9 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69  the schema cooki
1f2ba 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  e..    */.    if
1f2bb 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  ( IsVirtual(pTab
1f2bc 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) ){.      sqlit
1f2bd 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
1f2be 4f 50 5f 56 44 65 73 74 72 6f 79 2c 20 69 44 62  OP_VDestroy, iDb
1f2bf 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e  , 0, 0, pTab->zN
1f2c0 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20  ame, 0);.    }. 
1f2c1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1f2c2 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54  dOp4(v, OP_DropT
1f2c3 61 62 6c 65 2c 20 69 44 62 2c 20 30 2c 20 30 2c  able, iDb, 0, 0,
1f2c4 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29   pTab->zName, 0)
1f2c5 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61  ;.    sqlite3Cha
1f2c6 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65  ngeCookie(pParse
1f2c7 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20 73 71  , iDb);.  }.  sq
1f2c8 6c 69 74 65 56 69 65 77 52 65 73 65 74 41 6c 6c  liteViewResetAll
1f2c9 28 64 62 2c 20 69 44 62 29 3b 0a 0a 65 78 69 74  (db, iDb);..exit
1f2ca 5f 64 72 6f 70 5f 74 61 62 6c 65 3a 0a 20 20 73  _drop_table:.  s
1f2cb 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c  qlite3SrcListDel
1f2cc 65 74 65 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a  ete(db, pName);.
1f2cd 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
1f2ce 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
1f2cf 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20  to create a new 
1f2d0 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 74  foreign key on t
1f2d1 68 65 20 74 61 62 6c 65 0a 2a 2a 20 63 75 72 72  he table.** curr
1f2d2 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73  ently under cons
1f2d3 74 72 75 63 74 69 6f 6e 2e 20 20 70 46 72 6f 6d  truction.  pFrom
1f2d4 43 6f 6c 20 64 65 74 65 72 6d 69 6e 65 73 20 77  Col determines w
1f2d5 68 69 63 68 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20  hich columns.** 
1f2d6 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  in the current t
1f2d7 61 62 6c 65 20 70 6f 69 6e 74 20 74 6f 20 74 68  able point to th
1f2d8 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 2e 20 20  e foreign key.  
1f2d9 49 66 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 74  If pFromCol==0 t
1f2da 68 65 6e 0a 2a 2a 20 63 6f 6e 6e 65 63 74 20 74  hen.** connect t
1f2db 68 65 20 6b 65 79 20 74 6f 20 74 68 65 20 6c 61  he key to the la
1f2dc 73 74 20 63 6f 6c 75 6d 6e 20 69 6e 73 65 72 74  st column insert
1f2dd 65 64 2e 20 20 70 54 6f 20 69 73 20 74 68 65 20  ed.  pTo is the 
1f2de 6e 61 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 74  name of.** the t
1f2df 61 62 6c 65 20 72 65 66 65 72 72 65 64 20 74 6f  able referred to
1f2e0 2e 20 20 70 54 6f 43 6f 6c 20 69 73 20 61 20 6c  .  pToCol is a l
1f2e1 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20 69 6e  ist of tables in
1f2e2 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 70 54   the other.** pT
1f2e3 6f 20 74 61 62 6c 65 20 74 68 61 74 20 74 68 65  o table that the
1f2e4 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 70 6f 69   foreign key poi
1f2e5 6e 74 73 20 74 6f 2e 20 20 66 6c 61 67 73 20 63  nts to.  flags c
1f2e6 6f 6e 74 61 69 6e 73 20 61 6c 6c 0a 2a 2a 20 69  ontains all.** i
1f2e7 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
1f2e8 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 20 72 65   the conflict re
1f2e9 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69 74  solution algorit
1f2ea 68 6d 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  hms specified.**
1f2eb 20 69 6e 20 74 68 65 20 4f 4e 20 44 45 4c 45 54   in the ON DELET
1f2ec 45 2c 20 4f 4e 20 55 50 44 41 54 45 20 61 6e 64  E, ON UPDATE and
1f2ed 20 4f 4e 20 49 4e 53 45 52 54 20 63 6c 61 75 73   ON INSERT claus
1f2ee 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 46 4b 65  es..**.** An FKe
1f2ef 79 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63  y structure is c
1f2f0 72 65 61 74 65 64 20 61 6e 64 20 61 64 64 65 64  reated and added
1f2f1 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75   to the table cu
1f2f2 72 72 65 6e 74 6c 79 0a 2a 2a 20 75 6e 64 65 72  rrently.** under
1f2f3 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e   construction in
1f2f4 20 74 68 65 20 70 50 61 72 73 65 2d 3e 70 4e 65   the pParse->pNe
1f2f5 77 54 61 62 6c 65 20 66 69 65 6c 64 2e 20 20 54  wTable field.  T
1f2f6 68 65 20 6e 65 77 20 46 4b 65 79 0a 2a 2a 20 69  he new FKey.** i
1f2f7 73 20 6e 6f 74 20 6c 69 6e 6b 65 64 20 69 6e 74  s not linked int
1f2f8 6f 20 64 62 2d 3e 61 46 4b 65 79 20 61 74 20 74  o db->aFKey at t
1f2f9 68 69 73 20 70 6f 69 6e 74 20 2d 20 74 68 61 74  his point - that
1f2fa 20 64 6f 65 73 20 6e 6f 74 20 68 61 70 70 65 6e   does not happen
1f2fb 0a 2a 2a 20 75 6e 74 69 6c 20 73 71 6c 69 74 65  .** until sqlite
1f2fc 33 45 6e 64 54 61 62 6c 65 28 29 2e 0a 2a 2a 0a  3EndTable()..**.
1f2fd 2a 2a 20 54 68 65 20 66 6f 72 65 69 67 6e 20 6b  ** The foreign k
1f2fe 65 79 20 69 73 20 73 65 74 20 66 6f 72 20 49 4d  ey is set for IM
1f2ff 4d 45 44 49 41 54 45 20 70 72 6f 63 65 73 73 69  MEDIATE processi
1f300 6e 67 2e 20 20 41 20 73 75 62 73 65 71 75 65 6e  ng.  A subsequen
1f301 74 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 73 71 6c  t call.** to sql
1f302 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e  ite3DeferForeign
1f303 4b 65 79 28 29 20 6d 69 67 68 74 20 63 68 61 6e  Key() might chan
1f304 67 65 20 74 68 69 73 20 74 6f 20 44 45 46 45 52  ge this to DEFER
1f305 52 45 44 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  RED..*/.SQLITE_P
1f306 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1f307 74 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e  te3CreateForeign
1f308 4b 65 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50  Key(.  Parse *pP
1f309 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50  arse,       /* P
1f30a 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
1f30b 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 46  /.  ExprList *pF
1f30c 72 6f 6d 43 6f 6c 2c 20 20 2f 2a 20 43 6f 6c 75  romCol,  /* Colu
1f30d 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c  mns in this tabl
1f30e 65 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20  e that point to 
1f30f 6f 74 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20  other table */. 
1f310 20 54 6f 6b 65 6e 20 2a 70 54 6f 2c 20 20 20 20   Token *pTo,    
1f311 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
1f312 20 74 68 65 20 6f 74 68 65 72 20 74 61 62 6c 65   the other table
1f313 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
1f314 70 54 6f 43 6f 6c 2c 20 20 20 20 2f 2a 20 43 6f  pToCol,    /* Co
1f315 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 6f 74 68  lumns in the oth
1f316 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  er table */.  in
1f317 74 20 66 6c 61 67 73 20 20 20 20 20 20 20 20 20  t flags         
1f318 20 20 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 20 72     /* Conflict r
1f319 65 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69  esolution algori
1f31a 74 68 6d 73 2e 20 2a 2f 0a 29 7b 0a 20 20 73 71  thms. */.){.  sq
1f31b 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
1f31c 73 65 2d 3e 64 62 3b 0a 23 69 66 6e 64 65 66 20  se->db;.#ifndef 
1f31d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45  SQLITE_OMIT_FORE
1f31e 49 47 4e 5f 4b 45 59 0a 20 20 46 4b 65 79 20 2a  IGN_KEY.  FKey *
1f31f 70 46 4b 65 79 20 3d 20 30 3b 0a 20 20 54 61 62  pFKey = 0;.  Tab
1f320 6c 65 20 2a 70 20 3d 20 70 50 61 72 73 65 2d 3e  le *p = pParse->
1f321 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 6e 74  pNewTable;.  int
1f322 20 6e 42 79 74 65 3b 0a 20 20 69 6e 74 20 69 3b   nByte;.  int i;
1f323 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 63  .  int nCol;.  c
1f324 68 61 72 20 2a 7a 3b 0a 0a 20 20 61 73 73 65 72  har *z;..  asser
1f325 74 28 20 70 54 6f 21 3d 30 20 29 3b 0a 20 20 69  t( pTo!=0 );.  i
1f326 66 28 20 70 3d 3d 30 20 7c 7c 20 70 50 61 72 73  f( p==0 || pPars
1f327 65 2d 3e 6e 45 72 72 20 7c 7c 20 49 4e 5f 44 45  e->nErr || IN_DE
1f328 43 4c 41 52 45 5f 56 54 41 42 20 29 20 67 6f 74  CLARE_VTAB ) got
1f329 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 69 66 28 20  o fk_end;.  if( 
1f32a 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 29 7b 0a 20  pFromCol==0 ){. 
1f32b 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 2d     int iCol = p-
1f32c 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 20 20 69 66 28  >nCol-1;.    if(
1f32d 20 69 43 6f 6c 3c 30 20 29 20 67 6f 74 6f 20 66   iCol<0 ) goto f
1f32e 6b 5f 65 6e 64 3b 0a 20 20 20 20 69 66 28 20 70  k_end;.    if( p
1f32f 54 6f 43 6f 6c 20 26 26 20 70 54 6f 43 6f 6c 2d  ToCol && pToCol-
1f330 3e 6e 45 78 70 72 21 3d 31 20 29 7b 0a 20 20 20  >nExpr!=1 ){.   
1f331 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1f332 73 67 28 70 50 61 72 73 65 2c 20 22 66 6f 72 65  sg(pParse, "fore
1f333 69 67 6e 20 6b 65 79 20 6f 6e 20 25 73 22 0a 20  ign key on %s". 
1f334 20 20 20 20 20 20 20 20 22 20 73 68 6f 75 6c 64          " should
1f335 20 72 65 66 65 72 65 6e 63 65 20 6f 6e 6c 79 20   reference only 
1f336 6f 6e 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61  one column of ta
1f337 62 6c 65 20 25 54 22 2c 0a 20 20 20 20 20 20 20  ble %T",.       
1f338 20 20 70 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e    p->aCol[iCol].
1f339 7a 4e 61 6d 65 2c 20 70 54 6f 29 3b 0a 20 20 20  zName, pTo);.   
1f33a 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a     goto fk_end;.
1f33b 20 20 20 20 7d 0a 20 20 20 20 6e 43 6f 6c 20 3d      }.    nCol =
1f33c 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   1;.  }else if( 
1f33d 70 54 6f 43 6f 6c 20 26 26 20 70 54 6f 43 6f 6c  pToCol && pToCol
1f33e 2d 3e 6e 45 78 70 72 21 3d 70 46 72 6f 6d 43 6f  ->nExpr!=pFromCo
1f33f 6c 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20  l->nExpr ){.    
1f340 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1f341 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20  pParse,.        
1f342 22 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d  "number of colum
1f343 6e 73 20 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65  ns in foreign ke
1f344 79 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68  y does not match
1f345 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 22   the number of "
1f346 0a 20 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e  .        "column
1f347 73 20 69 6e 20 74 68 65 20 72 65 66 65 72 65 6e  s in the referen
1f348 63 65 64 20 74 61 62 6c 65 22 29 3b 0a 20 20 20  ced table");.   
1f349 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20   goto fk_end;.  
1f34a 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 43 6f 6c 20  }else{.    nCol 
1f34b 3d 20 70 46 72 6f 6d 43 6f 6c 2d 3e 6e 45 78 70  = pFromCol->nExp
1f34c 72 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d  r;.  }.  nByte =
1f34d 20 73 69 7a 65 6f 66 28 2a 70 46 4b 65 79 29 20   sizeof(*pFKey) 
1f34e 2b 20 6e 43 6f 6c 2a 73 69 7a 65 6f 66 28 70 46  + nCol*sizeof(pF
1f34f 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d 29 20 2b 20  Key->aCol[0]) + 
1f350 70 54 6f 2d 3e 6e 20 2b 20 31 3b 0a 20 20 69 66  pTo->n + 1;.  if
1f351 28 20 70 54 6f 43 6f 6c 20 29 7b 0a 20 20 20 20  ( pToCol ){.    
1f352 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 6f 43 6f  for(i=0; i<pToCo
1f353 6c 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  l->nExpr; i++){.
1f354 20 20 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 73        nByte += s
1f355 74 72 6c 65 6e 28 70 54 6f 43 6f 6c 2d 3e 61 5b  trlen(pToCol->a[
1f356 69 5d 2e 7a 4e 61 6d 65 29 20 2b 20 31 3b 0a 20  i].zName) + 1;. 
1f357 20 20 20 7d 0a 20 20 7d 0a 20 20 70 46 4b 65 79     }.  }.  pFKey
1f358 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
1f359 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42 79 74 65  ocZero(db, nByte
1f35a 20 29 3b 0a 20 20 69 66 28 20 70 46 4b 65 79 3d   );.  if( pFKey=
1f35b 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66  =0 ){.    goto f
1f35c 6b 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20 70 46 4b  k_end;.  }.  pFK
1f35d 65 79 2d 3e 70 46 72 6f 6d 20 3d 20 70 3b 0a 20  ey->pFrom = p;. 
1f35e 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f   pFKey->pNextFro
1f35f 6d 20 3d 20 70 2d 3e 70 46 4b 65 79 3b 0a 20 20  m = p->pFKey;.  
1f360 7a 20 3d 20 28 63 68 61 72 2a 29 26 70 46 4b 65  z = (char*)&pFKe
1f361 79 5b 31 5d 3b 0a 20 20 70 46 4b 65 79 2d 3e 61  y[1];.  pFKey->a
1f362 43 6f 6c 20 3d 20 28 73 74 72 75 63 74 20 73 43  Col = (struct sC
1f363 6f 6c 4d 61 70 2a 29 7a 3b 0a 20 20 7a 20 2b 3d  olMap*)z;.  z +=
1f364 20 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 73   sizeof(struct s
1f365 43 6f 6c 4d 61 70 29 2a 6e 43 6f 6c 3b 0a 20 20  ColMap)*nCol;.  
1f366 70 46 4b 65 79 2d 3e 7a 54 6f 20 3d 20 7a 3b 0a  pFKey->zTo = z;.
1f367 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70 54 6f 2d    memcpy(z, pTo-
1f368 3e 7a 2c 20 70 54 6f 2d 3e 6e 29 3b 0a 20 20 7a  >z, pTo->n);.  z
1f369 5b 70 54 6f 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20  [pTo->n] = 0;.  
1f36a 7a 20 2b 3d 20 70 54 6f 2d 3e 6e 2b 31 3b 0a 20  z += pTo->n+1;. 
1f36b 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 20   pFKey->pNextTo 
1f36c 3d 20 30 3b 0a 20 20 70 46 4b 65 79 2d 3e 6e 43  = 0;.  pFKey->nC
1f36d 6f 6c 20 3d 20 6e 43 6f 6c 3b 0a 20 20 69 66 28  ol = nCol;.  if(
1f36e 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 29 7b 0a   pFromCol==0 ){.
1f36f 20 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b      pFKey->aCol[
1f370 30 5d 2e 69 46 72 6f 6d 20 3d 20 70 2d 3e 6e 43  0].iFrom = p->nC
1f371 6f 6c 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ol-1;.  }else{. 
1f372 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43     for(i=0; i<nC
1f373 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
1f374 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f 72  int j;.      for
1f375 28 6a 3d 30 3b 20 6a 3c 70 2d 3e 6e 43 6f 6c 3b  (j=0; j<p->nCol;
1f376 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69   j++){.        i
1f377 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
1f378 70 28 70 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61  p(p->aCol[j].zNa
1f379 6d 65 2c 20 70 46 72 6f 6d 43 6f 6c 2d 3e 61 5b  me, pFromCol->a[
1f37a 69 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a  i].zName)==0 ){.
1f37b 20 20 20 20 20 20 20 20 20 20 70 46 4b 65 79 2d            pFKey-
1f37c 3e 61 43 6f 6c 5b 69 5d 2e 69 46 72 6f 6d 20 3d  >aCol[i].iFrom =
1f37d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72   j;.          br
1f37e 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
1f37f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
1f380 20 6a 3e 3d 70 2d 3e 6e 43 6f 6c 20 29 7b 0a 20   j>=p->nCol ){. 
1f381 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
1f382 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a  rorMsg(pParse, .
1f383 20 20 20 20 20 20 20 20 20 20 22 75 6e 6b 6e 6f            "unkno
1f384 77 6e 20 63 6f 6c 75 6d 6e 20 5c 22 25 73 5c 22  wn column \"%s\"
1f385 20 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 20   in foreign key 
1f386 64 65 66 69 6e 69 74 69 6f 6e 22 2c 20 0a 20 20  definition", .  
1f387 20 20 20 20 20 20 20 20 70 46 72 6f 6d 43 6f 6c          pFromCol
1f388 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  ->a[i].zName);. 
1f389 20 20 20 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65         goto fk_e
1f38a 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  nd;.      }.    
1f38b 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 54 6f 43  }.  }.  if( pToC
1f38c 6f 6c 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  ol ){.    for(i=
1f38d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  0; i<nCol; i++){
1f38e 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 73  .      int n = s
1f38f 74 72 6c 65 6e 28 70 54 6f 43 6f 6c 2d 3e 61 5b  trlen(pToCol->a[
1f390 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  i].zName);.     
1f391 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 69 5d 2e   pFKey->aCol[i].
1f392 7a 43 6f 6c 20 3d 20 7a 3b 0a 20 20 20 20 20 20  zCol = z;.      
1f393 6d 65 6d 63 70 79 28 7a 2c 20 70 54 6f 43 6f 6c  memcpy(z, pToCol
1f394 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 6e 29  ->a[i].zName, n)
1f395 3b 0a 20 20 20 20 20 20 7a 5b 6e 5d 20 3d 20 30  ;.      z[n] = 0
1f396 3b 0a 20 20 20 20 20 20 7a 20 2b 3d 20 6e 2b 31  ;.      z += n+1
1f397 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 46  ;.    }.  }.  pF
1f398 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64 20  Key->isDeferred 
1f399 3d 20 30 3b 0a 20 20 70 46 4b 65 79 2d 3e 64 65  = 0;.  pFKey->de
1f39a 6c 65 74 65 43 6f 6e 66 20 3d 20 66 6c 61 67 73  leteConf = flags
1f39b 20 26 20 30 78 66 66 3b 0a 20 20 70 46 4b 65 79   & 0xff;.  pFKey
1f39c 2d 3e 75 70 64 61 74 65 43 6f 6e 66 20 3d 20 28  ->updateConf = (
1f39d 66 6c 61 67 73 20 3e 3e 20 38 20 29 20 26 20 30  flags >> 8 ) & 0
1f39e 78 66 66 3b 0a 20 20 70 46 4b 65 79 2d 3e 69 6e  xff;.  pFKey->in
1f39f 73 65 72 74 43 6f 6e 66 20 3d 20 28 66 6c 61 67  sertConf = (flag
1f3a0 73 20 3e 3e 20 31 36 20 29 20 26 20 30 78 66 66  s >> 16 ) & 0xff
1f3a1 3b 0a 0a 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65  ;..  /* Link the
1f3a2 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 74 6f 20   foreign key to 
1f3a3 74 68 65 20 74 61 62 6c 65 20 61 73 20 74 68 65  the table as the
1f3a4 20 6c 61 73 74 20 73 74 65 70 2e 0a 20 20 2a 2f   last step..  */
1f3a5 0a 20 20 70 2d 3e 70 46 4b 65 79 20 3d 20 70 46  .  p->pFKey = pF
1f3a6 4b 65 79 3b 0a 20 20 70 46 4b 65 79 20 3d 20 30  Key;.  pFKey = 0
1f3a7 3b 0a 0a 66 6b 5f 65 6e 64 3a 0a 20 20 73 71 6c  ;..fk_end:.  sql
1f3a8 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1f3a9 46 4b 65 79 29 3b 0a 23 65 6e 64 69 66 20 2f 2a  FKey);.#endif /*
1f3aa 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1f3ab 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45  _OMIT_FOREIGN_KE
1f3ac 59 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45  Y) */.  sqlite3E
1f3ad 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
1f3ae 2c 20 70 46 72 6f 6d 43 6f 6c 29 3b 0a 20 20 73  , pFromCol);.  s
1f3af 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
1f3b0 6c 65 74 65 28 64 62 2c 20 70 54 6f 43 6f 6c 29  lete(db, pToCol)
1f3b1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
1f3b2 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
1f3b3 64 20 77 68 65 6e 20 61 6e 20 49 4e 49 54 49 41  d when an INITIA
1f3b4 4c 4c 59 20 49 4d 4d 45 44 49 41 54 45 20 6f 72  LLY IMMEDIATE or
1f3b5 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52   INITIALLY DEFER
1f3b6 52 45 44 0a 2a 2a 20 63 6c 61 75 73 65 20 69 73  RED.** clause is
1f3b7 20 73 65 65 6e 20 61 73 20 70 61 72 74 20 6f 66   seen as part of
1f3b8 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64   a foreign key d
1f3b9 65 66 69 6e 69 74 69 6f 6e 2e 20 20 54 68 65 20  efinition.  The 
1f3ba 69 73 44 65 66 65 72 72 65 64 0a 2a 2a 20 70 61  isDeferred.** pa
1f3bb 72 61 6d 65 74 65 72 20 69 73 20 31 20 66 6f 72  rameter is 1 for
1f3bc 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52   INITIALLY DEFER
1f3bd 52 45 44 20 61 6e 64 20 30 20 66 6f 72 20 49 4e  RED and 0 for IN
1f3be 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49 41 54  ITIALLY IMMEDIAT
1f3bf 45 2e 0a 2a 2a 20 54 68 65 20 62 65 68 61 76 69  E..** The behavi
1f3c0 6f 72 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72  or of the most r
1f3c1 65 63 65 6e 74 6c 79 20 63 72 65 61 74 65 64 20  ecently created 
1f3c2 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 73 20 61  foreign key is a
1f3c3 64 6a 75 73 74 65 64 0a 2a 2a 20 61 63 63 6f 72  djusted.** accor
1f3c4 64 69 6e 67 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54  dingly..*/.SQLIT
1f3c5 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1f3c6 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65 69  qlite3DeferForei
1f3c7 67 6e 4b 65 79 28 50 61 72 73 65 20 2a 70 50 61  gnKey(Parse *pPa
1f3c8 72 73 65 2c 20 69 6e 74 20 69 73 44 65 66 65 72  rse, int isDefer
1f3c9 72 65 64 29 7b 0a 23 69 66 6e 64 65 66 20 53 51  red){.#ifndef SQ
1f3ca 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47  LITE_OMIT_FOREIG
1f3cb 4e 5f 4b 45 59 0a 20 20 54 61 62 6c 65 20 2a 70  N_KEY.  Table *p
1f3cc 54 61 62 3b 0a 20 20 46 4b 65 79 20 2a 70 46 4b  Tab;.  FKey *pFK
1f3cd 65 79 3b 0a 20 20 69 66 28 20 28 70 54 61 62 20  ey;.  if( (pTab 
1f3ce 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  = pParse->pNewTa
1f3cf 62 6c 65 29 3d 3d 30 20 7c 7c 20 28 70 46 4b 65  ble)==0 || (pFKe
1f3d0 79 20 3d 20 70 54 61 62 2d 3e 70 46 4b 65 79 29  y = pTab->pFKey)
1f3d1 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
1f3d2 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65  pFKey->isDeferre
1f3d3 64 20 3d 20 69 73 44 65 66 65 72 72 65 64 3b 0a  d = isDeferred;.
1f3d4 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
1f3d5 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  Generate code th
1f3d6 61 74 20 77 69 6c 6c 20 65 72 61 73 65 20 61 6e  at will erase an
1f3d7 64 20 72 65 66 69 6c 6c 20 69 6e 64 65 78 20 2a  d refill index *
1f3d8 70 49 64 78 2e 20 20 54 68 69 73 20 69 73 0a 2a  pIdx.  This is.*
1f3d9 2a 20 75 73 65 64 20 74 6f 20 69 6e 69 74 69 61  * used to initia
1f3da 6c 69 7a 65 20 61 20 6e 65 77 6c 79 20 63 72 65  lize a newly cre
1f3db 61 74 65 64 20 69 6e 64 65 78 20 6f 72 20 74 6f  ated index or to
1f3dc 20 72 65 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a   recompute the.*
1f3dd 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20  * content of an 
1f3de 69 6e 64 65 78 20 69 6e 20 72 65 73 70 6f 6e 73  index in respons
1f3df 65 20 74 6f 20 61 20 52 45 49 4e 44 45 58 20 63  e to a REINDEX c
1f3e0 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 69 66  ommand..**.** if
1f3e1 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20 69 73 20   memRootPage is 
1f3e2 6e 6f 74 20 6e 65 67 61 74 69 76 65 2c 20 69 74  not negative, it
1f3e3 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
1f3e4 69 6e 64 65 78 20 69 73 20 6e 65 77 6c 79 0a 2a  index is newly.*
1f3e5 2a 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20  * created.  The 
1f3e6 72 65 67 69 73 74 65 72 20 73 70 65 63 69 66 69  register specifi
1f3e7 65 64 20 62 79 20 6d 65 6d 52 6f 6f 74 50 61 67  ed by memRootPag
1f3e8 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 2a  e contains the.*
1f3e9 2a 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62  * root page numb
1f3ea 65 72 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e  er of the index.
1f3eb 20 20 49 66 20 6d 65 6d 52 6f 6f 74 50 61 67 65    If memRootPage
1f3ec 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68   is negative, th
1f3ed 65 6e 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20  en.** the index 
1f3ee 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 61  already exists a
1f3ef 6e 64 20 6d 75 73 74 20 62 65 20 63 6c 65 61 72  nd must be clear
1f3f0 65 64 20 62 65 66 6f 72 65 20 62 65 69 6e 67 20  ed before being 
1f3f1 72 65 66 69 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20  refilled and.** 
1f3f2 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75  the root page nu
1f3f3 6d 62 65 72 20 6f 66 20 74 68 65 20 69 6e 64 65  mber of the inde
1f3f4 78 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20  x is taken from 
1f3f5 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 2e 0a 2a 2f  pIndex->tnum..*/
1f3f6 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c  .static void sql
1f3f7 69 74 65 33 52 65 66 69 6c 6c 49 6e 64 65 78 28  ite3RefillIndex(
1f3f8 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 49  Parse *pParse, I
1f3f9 6e 64 65 78 20 2a 70 49 6e 64 65 78 2c 20 69 6e  ndex *pIndex, in
1f3fa 74 20 6d 65 6d 52 6f 6f 74 50 61 67 65 29 7b 0a  t memRootPage){.
1f3fb 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
1f3fc 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 3b 20  pIndex->pTable; 
1f3fd 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68   /* The table th
1f3fe 61 74 20 69 73 20 69 6e 64 65 78 65 64 20 2a 2f  at is indexed */
1f3ff 0a 20 20 69 6e 74 20 69 54 61 62 20 3d 20 70 50  .  int iTab = pP
1f400 61 72 73 65 2d 3e 6e 54 61 62 3b 20 20 20 20 20  arse->nTab;     
1f401 20 20 2f 2a 20 42 74 72 65 65 20 63 75 72 73 6f    /* Btree curso
1f402 72 20 75 73 65 64 20 66 6f 72 20 70 54 61 62 20  r used for pTab 
1f403 2a 2f 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20  */.  int iIdx = 
1f404 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 31 3b 20  pParse->nTab+1; 
1f405 20 20 20 20 2f 2a 20 42 74 72 65 65 20 63 75 72      /* Btree cur
1f406 73 6f 72 20 75 73 65 64 20 66 6f 72 20 70 49 6e  sor used for pIn
1f407 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  dex */.  int add
1f408 72 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  r1;             
1f409 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
1f40a 73 73 20 6f 66 20 74 6f 70 20 6f 66 20 6c 6f 6f  ss of top of loo
1f40b 70 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b  p */.  int tnum;
1f40c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f40d 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61        /* Root pa
1f40e 67 65 20 6f 66 20 69 6e 64 65 78 20 2a 2f 0a 20  ge of index */. 
1f40f 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20   Vdbe *v;       
1f410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f411 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
1f412 20 69 6e 74 6f 20 74 68 69 73 20 76 69 72 74 75   into this virtu
1f413 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20  al machine */.  
1f414 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 3b 20 20  KeyInfo *pKey;  
1f415 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1f416 2a 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 69 6e  * KeyInfo for in
1f417 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  dex */.  int reg
1f418 49 64 78 4b 65 79 3b 20 20 20 20 20 20 20 20 20  IdxKey;         
1f419 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
1f41a 74 65 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ters containing 
1f41b 74 68 65 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f  the index key */
1f41c 0a 20 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64  .  int regRecord
1f41d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1f41e 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f    /* Register ho
1f41f 6c 64 69 6e 67 20 61 73 73 65 6d 62 6c 69 65 64  lding assemblied
1f420 20 69 6e 64 65 78 20 72 65 63 6f 72 64 20 2a 2f   index record */
1f421 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1f422 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20   pParse->db;    
1f423 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
1f424 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
1f425 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69    int iDb = sqli
1f426 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
1f427 28 64 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53 63  (db, pIndex->pSc
1f428 68 65 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66 20  hema);..#ifndef 
1f429 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
1f42a 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28 20  ORIZATION.  if( 
1f42b 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
1f42c 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
1f42d 52 45 49 4e 44 45 58 2c 20 70 49 6e 64 65 78 2d  REINDEX, pIndex-
1f42e 3e 7a 4e 61 6d 65 2c 20 30 2c 0a 20 20 20 20 20  >zName, 0,.     
1f42f 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
1f430 61 6d 65 20 29 20 29 7b 0a 20 20 20 20 72 65 74  ame ) ){.    ret
1f431 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  urn;.  }.#endif.
1f432 0a 20 20 2f 2a 20 52 65 71 75 69 72 65 20 61 20  .  /* Require a 
1f433 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68  write-lock on th
1f434 65 20 74 61 62 6c 65 20 74 6f 20 70 65 72 66 6f  e table to perfo
1f435 72 6d 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f  rm this operatio
1f436 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 54 61  n */.  sqlite3Ta
1f437 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20  bleLock(pParse, 
1f438 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c  iDb, pTab->tnum,
1f439 20 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29   1, pTab->zName)
1f43a 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  ;..  v = sqlite3
1f43b 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
1f43c 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65  .  if( v==0 ) re
1f43d 74 75 72 6e 3b 0a 20 20 69 66 28 20 6d 65 6d 52  turn;.  if( memR
1f43e 6f 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20  ootPage>=0 ){.  
1f43f 20 20 74 6e 75 6d 20 3d 20 6d 65 6d 52 6f 6f 74    tnum = memRoot
1f440 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Page;.  }else{. 
1f441 20 20 20 74 6e 75 6d 20 3d 20 70 49 6e 64 65 78     tnum = pIndex
1f442 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 73 71 6c 69  ->tnum;.    sqli
1f443 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1f444 20 4f 50 5f 43 6c 65 61 72 2c 20 74 6e 75 6d 2c   OP_Clear, tnum,
1f445 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20 70 4b 65   iDb);.  }.  pKe
1f446 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78  y = sqlite3Index
1f447 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20  Keyinfo(pParse, 
1f448 70 49 6e 64 65 78 29 3b 0a 20 20 73 71 6c 69 74  pIndex);.  sqlit
1f449 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
1f44a 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 49  OP_OpenWrite, iI
1f44b 64 78 2c 20 74 6e 75 6d 2c 20 69 44 62 2c 20 0a  dx, tnum, iDb, .
1f44c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f44d 20 20 20 20 28 63 68 61 72 20 2a 29 70 4b 65 79      (char *)pKey
1f44e 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e  , P4_KEYINFO_HAN
1f44f 44 4f 46 46 29 3b 0a 20 20 69 66 28 20 6d 65 6d  DOFF);.  if( mem
1f450 52 6f 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20  RootPage>=0 ){. 
1f451 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
1f452 61 6e 67 65 50 35 28 76 2c 20 31 29 3b 0a 20 20  angeP5(v, 1);.  
1f453 7d 0a 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54  }.  sqlite3OpenT
1f454 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 54 61  able(pParse, iTa
1f455 62 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50  b, iDb, pTab, OP
1f456 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 61 64  _OpenRead);.  ad
1f457 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62  dr1 = sqlite3Vdb
1f458 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
1f459 77 69 6e 64 2c 20 69 54 61 62 2c 20 30 29 3b 0a  wind, iTab, 0);.
1f45a 20 20 72 65 67 52 65 63 6f 72 64 20 3d 20 73 71    regRecord = sq
1f45b 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
1f45c 70 50 61 72 73 65 29 3b 0a 20 20 72 65 67 49 64  pParse);.  regId
1f45d 78 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 47 65  xKey = sqlite3Ge
1f45e 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 70  nerateIndexKey(p
1f45f 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2c 20 69  Parse, pIndex, i
1f460 54 61 62 2c 20 72 65 67 52 65 63 6f 72 64 2c 20  Tab, regRecord, 
1f461 31 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78  1);.  if( pIndex
1f462 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f  ->onError!=OE_No
1f463 6e 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 31  ne ){.    int j1
1f464 2c 20 6a 32 3b 0a 20 20 20 20 69 6e 74 20 72 65  , j2;.    int re
1f465 67 52 6f 77 69 64 3b 0a 0a 20 20 20 20 72 65 67  gRowid;..    reg
1f466 52 6f 77 69 64 20 3d 20 72 65 67 49 64 78 4b 65  Rowid = regIdxKe
1f467 79 20 2b 20 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c  y + pIndex->nCol
1f468 75 6d 6e 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71  umn;.    j1 = sq
1f469 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
1f46a 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65  v, OP_IsNull, re
1f46b 67 49 64 78 4b 65 79 2c 20 30 2c 20 70 49 6e 64  gIdxKey, 0, pInd
1f46c 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20  ex->nColumn);.  
1f46d 20 20 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64    j2 = sqlite3Vd
1f46e 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49  beAddOp4(v, OP_I
1f46f 73 55 6e 69 71 75 65 2c 20 69 49 64 78 2c 0a 20  sUnique, iIdx,. 
1f470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f471 20 20 20 20 20 20 20 20 20 20 30 2c 20 72 65 67            0, reg
1f472 52 6f 77 69 64 2c 20 53 51 4c 49 54 45 5f 49 4e  Rowid, SQLITE_IN
1f473 54 5f 54 4f 5f 50 54 52 28 72 65 67 52 65 63 6f  T_TO_PTR(regReco
1f474 72 64 29 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a  rd), P4_INT32);.
1f475 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1f476 64 64 4f 70 34 28 76 2c 20 4f 50 5f 48 61 6c 74  ddOp4(v, OP_Halt
1f477 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  , SQLITE_CONSTRA
1f478 49 4e 54 2c 20 4f 45 5f 41 62 6f 72 74 2c 20 30  INT, OE_Abort, 0
1f479 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1f47a 20 20 20 20 20 20 22 69 6e 64 65 78 65 64 20 63        "indexed c
1f47b 6f 6c 75 6d 6e 73 20 61 72 65 20 6e 6f 74 20 75  olumns are not u
1f47c 6e 69 71 75 65 22 2c 20 50 34 5f 53 54 41 54 49  nique", P4_STATI
1f47d 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  C);.    sqlite3V
1f47e 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
1f47f 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
1f480 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
1f481 32 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  2);.  }.  sqlite
1f482 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1f483 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 49 64  P_IdxInsert, iId
1f484 78 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20  x, regRecord);. 
1f485 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
1f486 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
1f487 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c  egRecord);.  sql
1f488 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1f489 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 54 61 62 2c  , OP_Next, iTab,
1f48a 20 61 64 64 72 31 2b 31 29 3b 0a 20 20 73 71 6c   addr1+1);.  sql
1f48b 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
1f48c 28 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 73 71  (v, addr1);.  sq
1f48d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
1f48e 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 54 61  v, OP_Close, iTa
1f48f 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  b);.  sqlite3Vdb
1f490 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c  eAddOp1(v, OP_Cl
1f491 6f 73 65 2c 20 69 49 64 78 29 3b 0a 7d 0a 0a 2f  ose, iIdx);.}../
1f492 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  *.** Create a ne
1f493 77 20 69 6e 64 65 78 20 66 6f 72 20 61 6e 20 53  w index for an S
1f494 51 4c 20 74 61 62 6c 65 2e 20 20 70 4e 61 6d 65  QL table.  pName
1f495 31 2e 70 4e 61 6d 65 32 20 69 73 20 74 68 65 20  1.pName2 is the 
1f496 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65  name of the inde
1f497 78 20 0a 2a 2a 20 61 6e 64 20 70 54 62 6c 4c 69  x .** and pTblLi
1f498 73 74 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  st is the name o
1f499 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74  f the table that
1f49a 20 69 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65   is to be indexe
1f49b 64 2e 20 20 42 6f 74 68 20 77 69 6c 6c 20 0a 2a  d.  Both will .*
1f49c 2a 20 62 65 20 4e 55 4c 4c 20 66 6f 72 20 61 20  * be NULL for a 
1f49d 70 72 69 6d 61 72 79 20 6b 65 79 20 6f 72 20 61  primary key or a
1f49e 6e 20 69 6e 64 65 78 20 74 68 61 74 20 69 73 20  n index that is 
1f49f 63 72 65 61 74 65 64 20 74 6f 20 73 61 74 69 73  created to satis
1f4a0 66 79 20 61 0a 2a 2a 20 55 4e 49 51 55 45 20 63  fy a.** UNIQUE c
1f4a1 6f 6e 73 74 72 61 69 6e 74 2e 20 20 49 66 20 70  onstraint.  If p
1f4a2 54 61 62 6c 65 20 61 6e 64 20 70 49 6e 64 65 78  Table and pIndex
1f4a3 20 61 72 65 20 4e 55 4c 4c 2c 20 75 73 65 20 70   are NULL, use p
1f4a4 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
1f4a5 0a 2a 2a 20 61 73 20 74 68 65 20 74 61 62 6c 65  .** as the table
1f4a6 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e 20   to be indexed. 
1f4a7 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
1f4a8 6c 65 20 69 73 20 61 20 74 61 62 6c 65 20 74 68  le is a table th
1f4a9 61 74 20 69 73 0a 2a 2a 20 63 75 72 72 65 6e 74  at is.** current
1f4aa 6c 79 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75  ly being constru
1f4ab 63 74 65 64 20 62 79 20 61 20 43 52 45 41 54 45  cted by a CREATE
1f4ac 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
1f4ad 2e 0a 2a 2a 0a 2a 2a 20 70 4c 69 73 74 20 69 73  ..**.** pList is
1f4ae 20 61 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d   a list of colum
1f4af 6e 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64  ns to be indexed
1f4b0 2e 20 20 70 4c 69 73 74 20 77 69 6c 6c 20 62 65  .  pList will be
1f4b1 20 4e 55 4c 4c 20 69 66 20 74 68 69 73 0a 2a 2a   NULL if this.**
1f4b2 20 69 73 20 61 20 70 72 69 6d 61 72 79 20 6b 65   is a primary ke
1f4b3 79 20 6f 72 20 75 6e 69 71 75 65 2d 63 6f 6e 73  y or unique-cons
1f4b4 74 72 61 69 6e 74 20 6f 6e 20 74 68 65 20 6d 6f  traint on the mo
1f4b5 73 74 20 72 65 63 65 6e 74 20 63 6f 6c 75 6d 6e  st recent column
1f4b6 20 61 64 64 65 64 0a 2a 2a 20 74 6f 20 74 68 65   added.** to the
1f4b7 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79   table currently
1f4b8 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74   under construct
1f4b9 69 6f 6e 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45  ion.  .*/.SQLITE
1f4ba 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1f4bb 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78  lite3CreateIndex
1f4bc 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
1f4bd 65 2c 20 20 20 20 20 2f 2a 20 41 6c 6c 20 69 6e  e,     /* All in
1f4be 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
1f4bf 74 68 69 73 20 70 61 72 73 65 20 2a 2f 0a 20 20  this parse */.  
1f4c0 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20  Token *pName1,  
1f4c1 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 72 74     /* First part
1f4c2 20 6f 66 20 69 6e 64 65 78 20 6e 61 6d 65 2e 20   of index name. 
1f4c3 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20  May be NULL */. 
1f4c4 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20   Token *pName2, 
1f4c5 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61      /* Second pa
1f4c6 72 74 20 6f 66 20 69 6e 64 65 78 20 6e 61 6d 65  rt of index name
1f4c7 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f  . May be NULL */
1f4c8 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 62 6c  .  SrcList *pTbl
1f4c9 4e 61 6d 65 2c 20 2f 2a 20 54 61 62 6c 65 20 74  Name, /* Table t
1f4ca 6f 20 69 6e 64 65 78 2e 20 55 73 65 20 70 50 61  o index. Use pPa
1f4cb 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 69  rse->pNewTable i
1f4cc 66 20 30 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  f 0 */.  ExprLis
1f4cd 74 20 2a 70 4c 69 73 74 2c 20 20 20 2f 2a 20 41  t *pList,   /* A
1f4ce 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73   list of columns
1f4cf 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a   to be indexed *
1f4d0 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c  /.  int onError,
1f4d1 20 20 20 20 20 20 20 2f 2a 20 4f 45 5f 41 62 6f         /* OE_Abo
1f4d2 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f  rt, OE_Ignore, O
1f4d3 45 5f 52 65 70 6c 61 63 65 2c 20 6f 72 20 4f 45  E_Replace, or OE
1f4d4 5f 4e 6f 6e 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e  _None */.  Token
1f4d5 20 2a 70 53 74 61 72 74 2c 20 20 20 20 20 2f 2a   *pStart,     /*
1f4d6 20 54 68 65 20 43 52 45 41 54 45 20 74 6f 6b 65   The CREATE toke
1f4d7 6e 20 74 68 61 74 20 62 65 67 69 6e 73 20 74 68  n that begins th
1f4d8 69 73 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  is statement */.
1f4d9 20 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20    Token *pEnd,  
1f4da 20 20 20 20 20 2f 2a 20 54 68 65 20 22 29 22 20       /* The ")" 
1f4db 74 68 61 74 20 63 6c 6f 73 65 73 20 74 68 65 20  that closes the 
1f4dc 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 74 61  CREATE INDEX sta
1f4dd 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  tement */.  int 
1f4de 73 6f 72 74 4f 72 64 65 72 2c 20 20 20 20 20 2f  sortOrder,     /
1f4df 2a 20 53 6f 72 74 20 6f 72 64 65 72 20 6f 66 20  * Sort order of 
1f4e0 70 72 69 6d 61 72 79 20 6b 65 79 20 77 68 65 6e  primary key when
1f4e1 20 70 4c 69 73 74 3d 3d 4e 55 4c 4c 20 2a 2f 0a   pList==NULL */.
1f4e2 20 20 69 6e 74 20 69 66 4e 6f 74 45 78 69 73 74    int ifNotExist
1f4e3 20 20 20 20 20 2f 2a 20 4f 6d 69 74 20 65 72 72       /* Omit err
1f4e4 6f 72 20 69 66 20 69 6e 64 65 78 20 61 6c 72 65  or if index alre
1f4e5 61 64 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b  ady exists */.){
1f4e6 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d  .  Table *pTab =
1f4e7 20 30 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65   0;     /* Table
1f4e8 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a   to be indexed *
1f4e9 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65  /.  Index *pInde
1f4ea 78 20 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20  x = 0;   /* The 
1f4eb 69 6e 64 65 78 20 74 6f 20 62 65 20 63 72 65 61  index to be crea
1f4ec 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ted */.  char *z
1f4ed 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a  Name = 0;     /*
1f4ee 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64   Name of the ind
1f4ef 65 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d  ex */.  int nNam
1f4f0 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e;           /* 
1f4f1 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63  Number of charac
1f4f2 74 65 72 73 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f  ters in zName */
1f4f3 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 54  .  int i, j;.  T
1f4f4 6f 6b 65 6e 20 6e 75 6c 6c 49 64 3b 20 20 20 20  oken nullId;    
1f4f5 20 20 20 20 2f 2a 20 46 61 6b 65 20 74 6f 6b 65      /* Fake toke
1f4f6 6e 20 66 6f 72 20 61 6e 20 65 6d 70 74 79 20 49  n for an empty I
1f4f7 44 20 6c 69 73 74 20 2a 2f 0a 20 20 44 62 46 69  D list */.  DbFi
1f4f8 78 65 72 20 73 46 69 78 3b 20 20 20 20 20 20 20  xer sFix;       
1f4f9 20 2f 2a 20 46 6f 72 20 61 73 73 69 67 6e 69 6e   /* For assignin
1f4fa 67 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 73  g database names
1f4fb 20 74 6f 20 70 54 61 62 6c 65 20 2a 2f 0a 20 20   to pTable */.  
1f4fc 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 4d 61 73  int sortOrderMas
1f4fd 6b 3b 20 20 20 2f 2a 20 31 20 74 6f 20 68 6f 6e  k;   /* 1 to hon
1f4fe 6f 72 20 44 45 53 43 20 69 6e 20 69 6e 64 65 78  or DESC in index
1f4ff 2e 20 20 30 20 74 6f 20 69 67 6e 6f 72 65 2e 20  .  0 to ignore. 
1f500 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
1f501 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
1f502 20 44 62 20 2a 70 44 62 3b 20 20 20 20 20 20 20   Db *pDb;       
1f503 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65        /* The spe
1f504 63 69 66 69 63 20 74 61 62 6c 65 20 63 6f 6e 74  cific table cont
1f505 61 69 6e 69 6e 67 20 74 68 65 20 69 6e 64 65 78  aining the index
1f506 65 64 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  ed database */. 
1f507 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
1f508 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
1f509 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  f the database t
1f50a 68 61 74 20 69 73 20 62 65 69 6e 67 20 77 72 69  hat is being wri
1f50b 74 74 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  tten */.  Token 
1f50c 2a 70 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 2f  *pName = 0;    /
1f50d 2a 20 55 6e 71 75 61 6c 69 66 69 65 64 20 6e 61  * Unqualified na
1f50e 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  me of the index 
1f50f 74 6f 20 63 72 65 61 74 65 20 2a 2f 0a 20 20 73  to create */.  s
1f510 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
1f511 74 65 6d 20 2a 70 4c 69 73 74 49 74 65 6d 3b 20  tem *pListItem; 
1f512 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f  /* For looping o
1f513 76 65 72 20 70 4c 69 73 74 20 2a 2f 0a 20 20 69  ver pList */.  i
1f514 6e 74 20 6e 43 6f 6c 3b 0a 20 20 69 6e 74 20 6e  nt nCol;.  int n
1f515 45 78 74 72 61 20 3d 20 30 3b 0a 20 20 63 68 61  Extra = 0;.  cha
1f516 72 20 2a 7a 45 78 74 72 61 3b 0a 0a 20 20 69 66  r *zExtra;..  if
1f517 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c  ( pParse->nErr |
1f518 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
1f519 65 64 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45  ed || IN_DECLARE
1f51a 5f 56 54 41 42 20 29 7b 0a 20 20 20 20 67 6f 74  _VTAB ){.    got
1f51b 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
1f51c 64 65 78 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20  dex;.  }..  /*. 
1f51d 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 74 61 62   ** Find the tab
1f51e 6c 65 20 74 68 61 74 20 69 73 20 74 6f 20 62 65  le that is to be
1f51f 20 69 6e 64 65 78 65 64 2e 20 20 52 65 74 75 72   indexed.  Retur
1f520 6e 20 65 61 72 6c 79 20 69 66 20 6e 6f 74 20 66  n early if not f
1f521 6f 75 6e 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ound..  */.  if(
1f522 20 70 54 62 6c 4e 61 6d 65 21 3d 30 20 29 7b 0a   pTblName!=0 ){.
1f523 0a 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20  .    /* Use the 
1f524 74 77 6f 2d 70 61 72 74 20 69 6e 64 65 78 20 6e  two-part index n
1f525 61 6d 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  ame to determine
1f526 20 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 20   the database . 
1f527 20 20 20 2a 2a 20 74 6f 20 73 65 61 72 63 68 20     ** to search 
1f528 66 6f 72 20 74 68 65 20 74 61 62 6c 65 2e 20 27  for the table. '
1f529 46 69 78 27 20 74 68 65 20 74 61 62 6c 65 20 6e  Fix' the table n
1f52a 61 6d 65 20 74 6f 20 74 68 69 73 20 64 62 0a 20  ame to this db. 
1f52b 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 6c 6f 6f     ** before loo
1f52c 6b 69 6e 67 20 75 70 20 74 68 65 20 74 61 62 6c  king up the tabl
1f52d 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73  e..    */.    as
1f52e 73 65 72 74 28 20 70 4e 61 6d 65 31 20 26 26 20  sert( pName1 && 
1f52f 70 4e 61 6d 65 32 20 29 3b 0a 20 20 20 20 69 44  pName2 );.    iD
1f530 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61  b = sqlite3TwoPa
1f531 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70  rtName(pParse, p
1f532 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26  Name1, pName2, &
1f533 70 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  pName);.    if( 
1f534 69 44 62 3c 30 20 29 20 67 6f 74 6f 20 65 78 69  iDb<0 ) goto exi
1f535 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
1f536 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1f537 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 20 20 20 20  OMIT_TEMPDB.    
1f538 2f 2a 20 49 66 20 74 68 65 20 69 6e 64 65 78 20  /* If the index 
1f539 6e 61 6d 65 20 77 61 73 20 75 6e 71 75 61 6c 69  name was unquali
1f53a 66 69 65 64 2c 20 63 68 65 63 6b 20 69 66 20 74  fied, check if t
1f53b 68 65 20 74 68 65 20 74 61 62 6c 65 0a 20 20 20  he the table.   
1f53c 20 2a 2a 20 69 73 20 61 20 74 65 6d 70 20 74 61   ** is a temp ta
1f53d 62 6c 65 2e 20 49 66 20 73 6f 2c 20 73 65 74 20  ble. If so, set 
1f53e 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  the database to 
1f53f 31 2e 20 44 6f 20 6e 6f 74 20 64 6f 20 74 68 69  1. Do not do thi
1f540 73 0a 20 20 20 20 2a 2a 20 69 66 20 69 6e 69 74  s.    ** if init
1f541 69 61 6c 69 73 69 6e 67 20 61 20 64 61 74 61 62  ialising a datab
1f542 61 73 65 20 73 63 68 65 6d 61 2e 0a 20 20 20 20  ase schema..    
1f543 2a 2f 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e  */.    if( !db->
1f544 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20  init.busy ){.   
1f545 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65     pTab = sqlite
1f546 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70  3SrcListLookup(p
1f547 50 61 72 73 65 2c 20 70 54 62 6c 4e 61 6d 65 29  Parse, pTblName)
1f548 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 61 6d  ;.      if( pNam
1f549 65 32 20 26 26 20 70 4e 61 6d 65 32 2d 3e 6e 3d  e2 && pName2->n=
1f54a 3d 30 20 26 26 20 70 54 61 62 20 26 26 20 70 54  =0 && pTab && pT
1f54b 61 62 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d  ab->pSchema==db-
1f54c 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20  >aDb[1].pSchema 
1f54d 29 7b 0a 20 20 20 20 20 20 20 20 69 44 62 20 3d  ){.        iDb =
1f54e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
1f54f 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66  }.#endif..    if
1f550 28 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74  ( sqlite3FixInit
1f551 28 26 73 46 69 78 2c 20 70 50 61 72 73 65 2c 20  (&sFix, pParse, 
1f552 69 44 62 2c 20 22 69 6e 64 65 78 22 2c 20 70 4e  iDb, "index", pN
1f553 61 6d 65 29 20 26 26 0a 20 20 20 20 20 20 20 20  ame) &&.        
1f554 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73  sqlite3FixSrcLis
1f555 74 28 26 73 46 69 78 2c 20 70 54 62 6c 4e 61 6d  t(&sFix, pTblNam
1f556 65 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  e).    ){.      
1f557 2f 2a 20 42 65 63 61 75 73 65 20 74 68 65 20 70  /* Because the p
1f558 61 72 73 65 72 20 63 6f 6e 73 74 72 75 63 74 73  arser constructs
1f559 20 70 54 62 6c 4e 61 6d 65 20 66 72 6f 6d 20 61   pTblName from a
1f55a 20 73 69 6e 67 6c 65 20 69 64 65 6e 74 69 66 69   single identifi
1f55b 65 72 2c 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c  er,.      ** sql
1f55c 69 74 65 33 46 69 78 53 72 63 4c 69 73 74 20 63  ite3FixSrcList c
1f55d 61 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e 20 2a  an never fail. *
1f55e 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 30  /.      assert(0
1f55f 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 61  );.    }.    pTa
1f560 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74  b = sqlite3Locat
1f561 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30  eTable(pParse, 0
1f562 2c 20 70 54 62 6c 4e 61 6d 65 2d 3e 61 5b 30 5d  , pTblName->a[0]
1f563 2e 7a 4e 61 6d 65 2c 20 0a 20 20 20 20 20 20 20  .zName, .       
1f564 20 70 54 62 6c 4e 61 6d 65 2d 3e 61 5b 30 5d 2e   pTblName->a[0].
1f565 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20  zDatabase);.    
1f566 69 66 28 20 21 70 54 61 62 20 7c 7c 20 64 62 2d  if( !pTab || db-
1f567 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20  >mallocFailed ) 
1f568 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65  goto exit_create
1f569 5f 69 6e 64 65 78 3b 0a 20 20 20 20 61 73 73 65  _index;.    asse
1f56a 72 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  rt( db->aDb[iDb]
1f56b 2e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e  .pSchema==pTab->
1f56c 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 7d 65 6c  pSchema );.  }el
1f56d 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
1f56e 70 4e 61 6d 65 3d 3d 30 20 29 3b 0a 20 20 20 20  pName==0 );.    
1f56f 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70  pTab = pParse->p
1f570 4e 65 77 54 61 62 6c 65 3b 0a 20 20 20 20 69 66  NewTable;.    if
1f571 28 20 21 70 54 61 62 20 29 20 67 6f 74 6f 20 65  ( !pTab ) goto e
1f572 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
1f573 3b 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69  ;.    iDb = sqli
1f574 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
1f575 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  (db, pTab->pSche
1f576 6d 61 29 3b 0a 20 20 7d 0a 20 20 70 44 62 20 3d  ma);.  }.  pDb =
1f577 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a   &db->aDb[iDb];.
1f578 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 7c  .  if( pTab==0 |
1f579 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29  | pParse->nErr )
1f57a 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
1f57b 65 5f 69 6e 64 65 78 3b 0a 20 20 69 66 28 20 73  e_index;.  if( s
1f57c 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 70  qlite3StrNICmp(p
1f57d 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c  Tab->zName, "sql
1f57e 69 74 65 5f 22 2c 20 37 29 3d 3d 30 20 29 7b 0a  ite_", 7)==0 ){.
1f57f 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1f580 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62  Msg(pParse, "tab
1f581 6c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65  le %s may not be
1f582 20 69 6e 64 65 78 65 64 22 2c 20 70 54 61 62 2d   indexed", pTab-
1f583 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74  >zName);.    got
1f584 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
1f585 64 65 78 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66  dex;.  }.#ifndef
1f586 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45   SQLITE_OMIT_VIE
1f587 57 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53  W.  if( pTab->pS
1f588 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c  elect ){.    sql
1f589 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1f58a 72 73 65 2c 20 22 76 69 65 77 73 20 6d 61 79 20  rse, "views may 
1f58b 6e 6f 74 20 62 65 20 69 6e 64 65 78 65 64 22 29  not be indexed")
1f58c 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  ;.    goto exit_
1f58d 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
1f58e 7d 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  }.#endif.#ifndef
1f58f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1f590 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20  TUALTABLE.  if( 
1f591 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
1f592 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
1f593 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1f594 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d  virtual tables m
1f595 61 79 20 6e 6f 74 20 62 65 20 69 6e 64 65 78 65  ay not be indexe
1f596 64 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78  d");.    goto ex
1f597 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
1f598 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
1f599 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20  *.  ** Find the 
1f59a 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65  name of the inde
1f59b 78 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68  x.  Make sure th
1f59c 65 72 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61  ere is not alrea
1f59d 64 79 20 61 6e 6f 74 68 65 72 0a 20 20 2a 2a 20  dy another.  ** 
1f59e 69 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 20 77  index or table w
1f59f 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d  ith the same nam
1f5a0 65 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45  e.  .  **.  ** E
1f5a1 78 63 65 70 74 69 6f 6e 3a 20 20 49 66 20 77 65  xception:  If we
1f5a2 20 61 72 65 20 72 65 61 64 69 6e 67 20 74 68 65   are reading the
1f5a3 20 6e 61 6d 65 73 20 6f 66 20 70 65 72 6d 61 6e   names of perman
1f5a4 65 6e 74 20 69 6e 64 69 63 65 73 20 66 72 6f 6d  ent indices from
1f5a5 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65   the.  ** sqlite
1f5a6 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 28 62  _master table (b
1f5a7 65 63 61 75 73 65 20 73 6f 6d 65 20 6f 74 68 65  ecause some othe
1f5a8 72 20 70 72 6f 63 65 73 73 20 63 68 61 6e 67 65  r process change
1f5a9 64 20 74 68 65 20 73 63 68 65 6d 61 29 20 61 6e  d the schema) an
1f5aa 64 0a 20 20 2a 2a 20 6f 6e 65 20 6f 66 20 74 68  d.  ** one of th
1f5ab 65 20 69 6e 64 65 78 20 6e 61 6d 65 73 20 63 6f  e index names co
1f5ac 6c 6c 69 64 65 73 20 77 69 74 68 20 74 68 65 20  llides with the 
1f5ad 6e 61 6d 65 20 6f 66 20 61 20 74 65 6d 70 6f 72  name of a tempor
1f5ae 61 72 79 20 74 61 62 6c 65 20 6f 72 0a 20 20 2a  ary table or.  *
1f5af 2a 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 77 65  * index, then we
1f5b0 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74   will continue t
1f5b1 6f 20 70 72 6f 63 65 73 73 20 74 68 69 73 20 69  o process this i
1f5b2 6e 64 65 78 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ndex..  **.  ** 
1f5b3 49 66 20 70 4e 61 6d 65 3d 3d 30 20 69 74 20 6d  If pName==0 it m
1f5b4 65 61 6e 73 20 74 68 61 74 20 77 65 20 61 72 65  eans that we are
1f5b5 0a 20 20 2a 2a 20 64 65 61 6c 69 6e 67 20 77 69  .  ** dealing wi
1f5b6 74 68 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79  th a primary key
1f5b7 20 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e 73 74   or UNIQUE const
1f5b8 72 61 69 6e 74 2e 20 20 57 65 20 68 61 76 65 20  raint.  We have 
1f5b9 74 6f 20 69 6e 76 65 6e 74 20 6f 75 72 0a 20 20  to invent our.  
1f5ba 2a 2a 20 6f 77 6e 20 6e 61 6d 65 2e 0a 20 20 2a  ** own name..  *
1f5bb 2f 0a 20 20 69 66 28 20 70 4e 61 6d 65 20 29 7b  /.  if( pName ){
1f5bc 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c  .    zName = sql
1f5bd 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
1f5be 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20  n(db, pName);.  
1f5bf 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
1f5c0 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65  =sqlite3ReadSche
1f5c1 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74  ma(pParse) ) got
1f5c2 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
1f5c3 64 65 78 3b 0a 20 20 20 20 69 66 28 20 7a 4e 61  dex;.    if( zNa
1f5c4 6d 65 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78 69  me==0 ) goto exi
1f5c5 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
1f5c6 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f      if( SQLITE_O
1f5c7 4b 21 3d 73 71 6c 69 74 65 33 43 68 65 63 6b 4f  K!=sqlite3CheckO
1f5c8 62 6a 65 63 74 4e 61 6d 65 28 70 50 61 72 73 65  bjectName(pParse
1f5c9 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20  , zName) ){.    
1f5ca 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
1f5cb 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a  te_index;.    }.
1f5cc 20 20 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69      if( !db->ini
1f5cd 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20 20  t.busy ){.      
1f5ce 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73  if( SQLITE_OK!=s
1f5cf 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
1f5d0 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20  (pParse) ) goto 
1f5d1 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
1f5d2 78 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  x;.      if( sql
1f5d3 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62  ite3FindTable(db
1f5d4 2c 20 7a 4e 61 6d 65 2c 20 30 29 21 3d 30 20 29  , zName, 0)!=0 )
1f5d5 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1f5d6 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1f5d7 2c 20 22 74 68 65 72 65 20 69 73 20 61 6c 72 65  , "there is alre
1f5d8 61 64 79 20 61 20 74 61 62 6c 65 20 6e 61 6d 65  ady a table name
1f5d9 64 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  d %s", zName);. 
1f5da 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74         goto exit
1f5db 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
1f5dc 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1f5dd 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64   if( sqlite3Find
1f5de 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c  Index(db, zName,
1f5df 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 21 3d 30 20   pDb->zName)!=0 
1f5e0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 69 66  ){.      if( !if
1f5e1 4e 6f 74 45 78 69 73 74 20 29 7b 0a 20 20 20 20  NotExist ){.    
1f5e2 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1f5e3 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69 6e 64  Msg(pParse, "ind
1f5e4 65 78 20 25 73 20 61 6c 72 65 61 64 79 20 65 78  ex %s already ex
1f5e5 69 73 74 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  ists", zName);. 
1f5e6 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74       }.      got
1f5e7 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
1f5e8 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  dex;.    }.  }el
1f5e9 73 65 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20  se{.    int n;. 
1f5ea 20 20 20 49 6e 64 65 78 20 2a 70 4c 6f 6f 70 3b     Index *pLoop;
1f5eb 0a 20 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d 70  .    for(pLoop=p
1f5ec 54 61 62 2d 3e 70 49 6e 64 65 78 2c 20 6e 3d 31  Tab->pIndex, n=1
1f5ed 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70  ; pLoop; pLoop=p
1f5ee 4c 6f 6f 70 2d 3e 70 4e 65 78 74 2c 20 6e 2b 2b  Loop->pNext, n++
1f5ef 29 7b 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20  ){}.    zName = 
1f5f0 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
1f5f1 62 2c 20 22 73 71 6c 69 74 65 5f 61 75 74 6f 69  b, "sqlite_autoi
1f5f2 6e 64 65 78 5f 25 73 5f 25 64 22 2c 20 70 54 61  ndex_%s_%d", pTa
1f5f3 62 2d 3e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20  b->zName, n);.  
1f5f4 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29    if( zName==0 )
1f5f5 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69  {.      goto exi
1f5f6 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
1f5f7 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
1f5f8 43 68 65 63 6b 20 66 6f 72 20 61 75 74 68 6f 72  Check for author
1f5f9 69 7a 61 74 69 6f 6e 20 74 6f 20 63 72 65 61 74  ization to creat
1f5fa 65 20 61 6e 20 69 6e 64 65 78 2e 0a 20 20 2a 2f  e an index..  */
1f5fb 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1f5fc 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
1f5fd 4f 4e 0a 20 20 7b 0a 20 20 20 20 63 6f 6e 73 74  ON.  {.    const
1f5fe 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 70 44 62   char *zDb = pDb
1f5ff 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 69 66 28  ->zName;.    if(
1f600 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
1f601 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
1f602 5f 49 4e 53 45 52 54 2c 20 53 43 48 45 4d 41 5f  _INSERT, SCHEMA_
1f603 54 41 42 4c 45 28 69 44 62 29 2c 20 30 2c 20 7a  TABLE(iDb), 0, z
1f604 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  Db) ){.      got
1f605 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
1f606 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  dex;.    }.    i
1f607 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45   = SQLITE_CREATE
1f608 5f 49 4e 44 45 58 3b 0a 20 20 20 20 69 66 28 20  _INDEX;.    if( 
1f609 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20  !OMIT_TEMPDB && 
1f60a 69 44 62 3d 3d 31 20 29 20 69 20 3d 20 53 51 4c  iDb==1 ) i = SQL
1f60b 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f  ITE_CREATE_TEMP_
1f60c 49 4e 44 45 58 3b 0a 20 20 20 20 69 66 28 20 73  INDEX;.    if( s
1f60d 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
1f60e 70 50 61 72 73 65 2c 20 69 2c 20 7a 4e 61 6d 65  pParse, i, zName
1f60f 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a  , pTab->zName, z
1f610 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  Db) ){.      got
1f611 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
1f612 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  dex;.    }.  }.#
1f613 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 70  endif..  /* If p
1f614 4c 69 73 74 3d 3d 30 2c 20 69 74 20 6d 65 61 6e  List==0, it mean
1f615 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  s this routine w
1f616 61 73 20 63 61 6c 6c 65 64 20 74 6f 20 6d 61 6b  as called to mak
1f617 65 20 61 20 70 72 69 6d 61 72 79 0a 20 20 2a 2a  e a primary.  **
1f618 20 6b 65 79 20 6f 75 74 20 6f 66 20 74 68 65 20   key out of the 
1f619 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 61 64 64 65  last column adde
1f61a 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 75  d to the table u
1f61b 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
1f61c 6e 2e 0a 20 20 2a 2a 20 53 6f 20 63 72 65 61 74  n..  ** So creat
1f61d 65 20 61 20 66 61 6b 65 20 6c 69 73 74 20 74 6f  e a fake list to
1f61e 20 73 69 6d 75 6c 61 74 65 20 74 68 69 73 2e 0a   simulate this..
1f61f 20 20 2a 2f 0a 20 20 69 66 28 20 70 4c 69 73 74    */.  if( pList
1f620 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 75 6c 6c 49  ==0 ){.    nullI
1f621 64 2e 7a 20 3d 20 28 75 38 2a 29 70 54 61 62 2d  d.z = (u8*)pTab-
1f622 3e 61 43 6f 6c 5b 70 54 61 62 2d 3e 6e 43 6f 6c  >aCol[pTab->nCol
1f623 2d 31 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e  -1].zName;.    n
1f624 75 6c 6c 49 64 2e 6e 20 3d 20 73 74 72 6c 65 6e  ullId.n = strlen
1f625 28 28 63 68 61 72 2a 29 6e 75 6c 6c 49 64 2e 7a  ((char*)nullId.z
1f626 29 3b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73  );.    pList = s
1f627 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
1f628 70 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c 20  pend(pParse, 0, 
1f629 30 2c 20 26 6e 75 6c 6c 49 64 29 3b 0a 20 20 20  0, &nullId);.   
1f62a 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20   if( pList==0 ) 
1f62b 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65  goto exit_create
1f62c 5f 69 6e 64 65 78 3b 0a 20 20 20 20 70 4c 69 73  _index;.    pLis
1f62d 74 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 64 65  t->a[0].sortOrde
1f62e 72 20 3d 20 73 6f 72 74 4f 72 64 65 72 3b 0a 20  r = sortOrder;. 
1f62f 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20   }..  /* Figure 
1f630 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 62 79 74  out how many byt
1f631 65 73 20 6f 66 20 73 70 61 63 65 20 61 72 65 20  es of space are 
1f632 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72  required to stor
1f633 65 20 65 78 70 6c 69 63 69 74 6c 79 0a 20 20 2a  e explicitly.  *
1f634 2a 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c 6c  * specified coll
1f635 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e  ation sequence n
1f636 61 6d 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  ames..  */.  for
1f637 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  (i=0; i<pList->n
1f638 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
1f639 45 78 70 72 20 2a 70 45 78 70 72 3b 0a 20 20 20  Expr *pExpr;.   
1f63a 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
1f63b 0a 20 20 20 20 69 66 28 20 28 70 45 78 70 72 20  .    if( (pExpr 
1f63c 3d 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45  = pList->a[i].pE
1f63d 78 70 72 29 21 3d 30 20 26 26 20 28 70 43 6f 6c  xpr)!=0 && (pCol
1f63e 6c 20 3d 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c  l = pExpr->pColl
1f63f 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e 45  )!=0 ){.      nE
1f640 78 74 72 61 20 2b 3d 20 28 31 20 2b 20 73 74 72  xtra += (1 + str
1f641 6c 65 6e 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65  len(pColl->zName
1f642 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ));.    }.  }.. 
1f643 20 2f 2a 20 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61   /* .  ** Alloca
1f644 74 65 20 74 68 65 20 69 6e 64 65 78 20 73 74 72  te the index str
1f645 75 63 74 75 72 65 2e 20 0a 20 20 2a 2f 0a 20 20  ucture. .  */.  
1f646 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a  nName = strlen(z
1f647 4e 61 6d 65 29 3b 0a 20 20 6e 43 6f 6c 20 3d 20  Name);.  nCol = 
1f648 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20  pList->nExpr;.  
1f649 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33  pIndex = sqlite3
1f64a 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
1f64b 20 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 49   .      sizeof(I
1f64c 6e 64 65 78 29 20 2b 20 20 20 20 20 20 20 20 20  ndex) +         
1f64d 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 73 74       /* Index st
1f64e 72 75 63 74 75 72 65 20 20 2a 2f 0a 20 20 20 20  ructure  */.    
1f64f 20 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 43    sizeof(int)*nC
1f650 6f 6c 20 2b 20 20 20 20 20 20 20 20 20 20 20 2f  ol +           /
1f651 2a 20 49 6e 64 65 78 2e 61 69 43 6f 6c 75 6d 6e  * Index.aiColumn
1f652 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65     */.      size
1f653 6f 66 28 69 6e 74 29 2a 28 6e 43 6f 6c 2b 31 29  of(int)*(nCol+1)
1f654 20 2b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65   +       /* Inde
1f655 78 2e 61 69 52 6f 77 45 73 74 20 20 20 2a 2f 0a  x.aiRowEst   */.
1f656 20 20 20 20 20 20 73 69 7a 65 6f 66 28 63 68 61        sizeof(cha
1f657 72 20 2a 29 2a 6e 43 6f 6c 20 2b 20 20 20 20 20  r *)*nCol +     
1f658 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 7a 43 6f     /* Index.azCo
1f659 6c 6c 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  ll     */.      
1f65a 73 69 7a 65 6f 66 28 75 38 29 2a 6e 43 6f 6c 20  sizeof(u8)*nCol 
1f65b 2b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  +            /* 
1f65c 49 6e 64 65 78 2e 61 53 6f 72 74 4f 72 64 65 72  Index.aSortOrder
1f65d 20 2a 2f 0a 20 20 20 20 20 20 6e 4e 61 6d 65 20   */.      nName 
1f65e 2b 20 31 20 2b 20 20 20 20 20 20 20 20 20 20 20  + 1 +           
1f65f 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e         /* Index.
1f660 7a 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 20 20  zName      */.  
1f661 20 20 20 20 6e 45 78 74 72 61 20 20 20 20 20 20      nExtra      
1f662 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f663 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65   /* Collation se
1f664 71 75 65 6e 63 65 20 6e 61 6d 65 73 20 2a 2f 0a  quence names */.
1f665 20 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d    );.  if( db->m
1f666 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1f667 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65     goto exit_cre
1f668 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20  ate_index;.  }. 
1f669 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 20   pIndex->azColl 
1f66a 3d 20 28 63 68 61 72 2a 2a 29 28 26 70 49 6e 64  = (char**)(&pInd
1f66b 65 78 5b 31 5d 29 3b 0a 20 20 70 49 6e 64 65 78  ex[1]);.  pIndex
1f66c 2d 3e 61 69 43 6f 6c 75 6d 6e 20 3d 20 28 69 6e  ->aiColumn = (in
1f66d 74 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61 7a  t *)(&pIndex->az
1f66e 43 6f 6c 6c 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 70  Coll[nCol]);.  p
1f66f 49 6e 64 65 78 2d 3e 61 69 52 6f 77 45 73 74 20  Index->aiRowEst 
1f670 3d 20 28 75 6e 73 69 67 6e 65 64 20 2a 29 28 26  = (unsigned *)(&
1f671 70 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e  pIndex->aiColumn
1f672 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 70 49 6e 64 65  [nCol]);.  pInde
1f673 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20  x->aSortOrder = 
1f674 28 75 38 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e  (u8 *)(&pIndex->
1f675 61 69 52 6f 77 45 73 74 5b 6e 43 6f 6c 2b 31 5d  aiRowEst[nCol+1]
1f676 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61  );.  pIndex->zNa
1f677 6d 65 20 3d 20 28 63 68 61 72 20 2a 29 28 26 70  me = (char *)(&p
1f678 49 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72 64 65  Index->aSortOrde
1f679 72 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 7a 45 78 74  r[nCol]);.  zExt
1f67a 72 61 20 3d 20 28 63 68 61 72 20 2a 29 28 26 70  ra = (char *)(&p
1f67b 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 5b 6e 4e 61  Index->zName[nNa
1f67c 6d 65 2b 31 5d 29 3b 0a 20 20 6d 65 6d 63 70 79  me+1]);.  memcpy
1f67d 28 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20  (pIndex->zName, 
1f67e 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b  zName, nName+1);
1f67f 0a 20 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c  .  pIndex->pTabl
1f680 65 20 3d 20 70 54 61 62 3b 0a 20 20 70 49 6e 64  e = pTab;.  pInd
1f681 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 70 4c  ex->nColumn = pL
1f682 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 49  ist->nExpr;.  pI
1f683 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 20 3d 20  ndex->onError = 
1f684 6f 6e 45 72 72 6f 72 3b 0a 20 20 70 49 6e 64 65  onError;.  pInde
1f685 78 2d 3e 61 75 74 6f 49 6e 64 65 78 20 3d 20 70  x->autoIndex = p
1f686 4e 61 6d 65 3d 3d 30 3b 0a 20 20 70 49 6e 64 65  Name==0;.  pInde
1f687 78 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64 62 2d  x->pSchema = db-
1f688 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d  >aDb[iDb].pSchem
1f689 61 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74  a;..  /* Check t
1f68a 6f 20 73 65 65 20 69 66 20 77 65 20 73 68 6f 75  o see if we shou
1f68b 6c 64 20 68 6f 6e 6f 72 20 44 45 53 43 20 72 65  ld honor DESC re
1f68c 71 75 65 73 74 73 20 6f 6e 20 69 6e 64 65 78 20  quests on index 
1f68d 63 6f 6c 75 6d 6e 73 0a 20 20 2a 2f 0a 20 20 69  columns.  */.  i
1f68e 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  f( pDb->pSchema-
1f68f 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3e 3d 34 20  >file_format>=4 
1f690 29 7b 0a 20 20 20 20 73 6f 72 74 4f 72 64 65 72  ){.    sortOrder
1f691 4d 61 73 6b 20 3d 20 2d 31 3b 20 20 20 2f 2a 20  Mask = -1;   /* 
1f692 48 6f 6e 6f 72 20 44 45 53 43 20 2a 2f 0a 20 20  Honor DESC */.  
1f693 7d 65 6c 73 65 7b 0a 20 20 20 20 73 6f 72 74 4f  }else{.    sortO
1f694 72 64 65 72 4d 61 73 6b 20 3d 20 30 3b 20 20 20  rderMask = 0;   
1f695 20 2f 2a 20 49 67 6e 6f 72 65 20 44 45 53 43 20   /* Ignore DESC 
1f696 2a 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 63 61  */.  }..  /* Sca
1f697 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 74  n the names of t
1f698 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68  he columns of th
1f699 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 69 6e  e table to be in
1f69a 64 65 78 65 64 20 61 6e 64 0a 20 20 2a 2a 20 6c  dexed and.  ** l
1f69b 6f 61 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69  oad the column i
1f69c 6e 64 69 63 65 73 20 69 6e 74 6f 20 74 68 65 20  ndices into the 
1f69d 49 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 2e  Index structure.
1f69e 20 20 52 65 70 6f 72 74 20 61 6e 20 65 72 72 6f    Report an erro
1f69f 72 0a 20 20 2a 2a 20 69 66 20 61 6e 79 20 63 6f  r.  ** if any co
1f6a0 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 66 6f 75 6e  lumn is not foun
1f6a1 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  d..  */.  for(i=
1f6a2 30 2c 20 70 4c 69 73 74 49 74 65 6d 3d 70 4c 69  0, pListItem=pLi
1f6a3 73 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e  st->a; i<pList->
1f6a4 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 4c 69 73  nExpr; i++, pLis
1f6a5 74 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 63 6f  tItem++){.    co
1f6a6 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 4e 61  nst char *zColNa
1f6a7 6d 65 20 3d 20 70 4c 69 73 74 49 74 65 6d 2d 3e  me = pListItem->
1f6a8 7a 4e 61 6d 65 3b 0a 20 20 20 20 43 6f 6c 75 6d  zName;.    Colum
1f6a9 6e 20 2a 70 54 61 62 43 6f 6c 3b 0a 20 20 20 20  n *pTabCol;.    
1f6aa 69 6e 74 20 72 65 71 75 65 73 74 65 64 53 6f 72  int requestedSor
1f6ab 74 4f 72 64 65 72 3b 0a 20 20 20 20 63 68 61 72  tOrder;.    char
1f6ac 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20   *zColl;        
1f6ad 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
1f6ae 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
1f6af 20 6e 61 6d 65 20 2a 2f 0a 0a 20 20 20 20 66 6f   name */..    fo
1f6b0 72 28 6a 3d 30 2c 20 70 54 61 62 43 6f 6c 3d 70  r(j=0, pTabCol=p
1f6b1 54 61 62 2d 3e 61 43 6f 6c 3b 20 6a 3c 70 54 61  Tab->aCol; j<pTa
1f6b2 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 2c 20 70 54  b->nCol; j++, pT
1f6b3 61 62 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20  abCol++){.      
1f6b4 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
1f6b5 6d 70 28 7a 43 6f 6c 4e 61 6d 65 2c 20 70 54 61  mp(zColName, pTa
1f6b6 62 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20  bCol->zName)==0 
1f6b7 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20  ) break;.    }. 
1f6b8 20 20 20 69 66 28 20 6a 3e 3d 70 54 61 62 2d 3e     if( j>=pTab->
1f6b9 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 73 71  nCol ){.      sq
1f6ba 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1f6bb 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20  arse, "table %s 
1f6bc 68 61 73 20 6e 6f 20 63 6f 6c 75 6d 6e 20 6e 61  has no column na
1f6bd 6d 65 64 20 25 73 22 2c 0a 20 20 20 20 20 20 20  med %s",.       
1f6be 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43   pTab->zName, zC
1f6bf 6f 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 67  olName);.      g
1f6c0 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f  oto exit_create_
1f6c1 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20  index;.    }.   
1f6c2 20 2f 2a 20 54 4f 44 4f 3a 20 20 41 64 64 20 61   /* TODO:  Add a
1f6c3 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 73 75   test to make su
1f6c4 72 65 20 74 68 61 74 20 74 68 65 20 73 61 6d 65  re that the same
1f6c5 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 6e   column is not n
1f6c6 61 6d 65 64 0a 20 20 20 20 2a 2a 20 6d 6f 72 65  amed.    ** more
1f6c7 20 74 68 61 6e 20 6f 6e 63 65 20 77 69 74 68 69   than once withi
1f6c8 6e 20 74 68 65 20 73 61 6d 65 20 69 6e 64 65 78  n the same index
1f6c9 2e 20 20 4f 6e 6c 79 20 74 68 65 20 66 69 72 73  .  Only the firs
1f6ca 74 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 20 20  t instance of.  
1f6cb 20 20 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20    ** the column 
1f6cc 77 69 6c 6c 20 65 76 65 72 20 62 65 20 75 73 65  will ever be use
1f6cd 64 20 62 79 20 74 68 65 20 6f 70 74 69 6d 69 7a  d by the optimiz
1f6ce 65 72 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 75  er.  Note that u
1f6cf 73 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20  sing the.    ** 
1f6d0 73 61 6d 65 20 63 6f 6c 75 6d 6e 20 6d 6f 72 65  same column more
1f6d1 20 74 68 61 6e 20 6f 6e 63 65 20 63 61 6e 6e 6f   than once canno
1f6d2 74 20 62 65 20 61 6e 20 65 72 72 6f 72 20 62 65  t be an error be
1f6d3 63 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64  cause that would
1f6d4 20 0a 20 20 20 20 2a 2a 20 62 72 65 61 6b 20 62   .    ** break b
1f6d5 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69  ackwards compati
1f6d6 62 69 6c 69 74 79 20 2d 20 69 74 20 6e 65 65 64  bility - it need
1f6d7 73 20 74 6f 20 62 65 20 61 20 77 61 72 6e 69 6e  s to be a warnin
1f6d8 67 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 49  g..    */.    pI
1f6d9 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69  ndex->aiColumn[i
1f6da 5d 20 3d 20 6a 3b 0a 20 20 20 20 69 66 28 20 70  ] = j;.    if( p
1f6db 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78 70 72 20  ListItem->pExpr 
1f6dc 26 26 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45  && pListItem->pE
1f6dd 78 70 72 2d 3e 70 43 6f 6c 6c 20 29 7b 0a 20 20  xpr->pColl ){.  
1f6de 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73      assert( pLis
1f6df 74 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43  tItem->pExpr->pC
1f6e0 6f 6c 6c 20 29 3b 0a 20 20 20 20 20 20 7a 43 6f  oll );.      zCo
1f6e1 6c 6c 20 3d 20 7a 45 78 74 72 61 3b 0a 20 20 20  ll = zExtra;.   
1f6e2 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
1f6e3 6e 74 66 28 6e 45 78 74 72 61 2c 20 7a 45 78 74  ntf(nExtra, zExt
1f6e4 72 61 2c 20 22 25 73 22 2c 20 70 4c 69 73 74 49  ra, "%s", pListI
1f6e5 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c  tem->pExpr->pCol
1f6e6 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  l->zName);.     
1f6e7 20 7a 45 78 74 72 61 20 2b 3d 20 28 73 74 72 6c   zExtra += (strl
1f6e8 65 6e 28 7a 43 6f 6c 6c 29 20 2b 20 31 29 3b 0a  en(zColl) + 1);.
1f6e9 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1f6ea 20 7a 43 6f 6c 6c 20 3d 20 70 54 61 62 2d 3e 61   zColl = pTab->a
1f6eb 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20  Col[j].zColl;.  
1f6ec 20 20 20 20 69 66 28 20 21 7a 43 6f 6c 6c 20 29      if( !zColl )
1f6ed 7b 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 6c 20  {.        zColl 
1f6ee 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 2d  = db->pDfltColl-
1f6ef 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 7d 0a  >zName;.      }.
1f6f0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 64      }.    if( !d
1f6f1 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20  b->init.busy && 
1f6f2 21 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f  !sqlite3LocateCo
1f6f3 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 7a 43  llSeq(pParse, zC
1f6f4 6f 6c 6c 2c 20 2d 31 29 20 29 7b 0a 20 20 20 20  oll, -1) ){.    
1f6f5 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
1f6f6 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a  te_index;.    }.
1f6f7 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f      pIndex->azCo
1f6f8 6c 6c 5b 69 5d 20 3d 20 7a 43 6f 6c 6c 3b 0a 20  ll[i] = zColl;. 
1f6f9 20 20 20 72 65 71 75 65 73 74 65 64 53 6f 72 74     requestedSort
1f6fa 4f 72 64 65 72 20 3d 20 70 4c 69 73 74 49 74 65  Order = pListIte
1f6fb 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 20 26 20 73  m->sortOrder & s
1f6fc 6f 72 74 4f 72 64 65 72 4d 61 73 6b 3b 0a 20 20  ortOrderMask;.  
1f6fd 20 20 70 49 6e 64 65 78 2d 3e 61 53 6f 72 74 4f    pIndex->aSortO
1f6fe 72 64 65 72 5b 69 5d 20 3d 20 72 65 71 75 65 73  rder[i] = reques
1f6ff 74 65 64 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20  tedSortOrder;.  
1f700 7d 0a 20 20 73 71 6c 69 74 65 33 44 65 66 61 75  }.  sqlite3Defau
1f701 6c 74 52 6f 77 45 73 74 28 70 49 6e 64 65 78 29  ltRowEst(pIndex)
1f702 3b 0a 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 70  ;..  if( pTab==p
1f703 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
1f704 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20   ){.    /* This 
1f705 72 6f 75 74 69 6e 65 20 68 61 73 20 62 65 65 6e  routine has been
1f706 20 63 61 6c 6c 65 64 20 74 6f 20 63 72 65 61 74   called to creat
1f707 65 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 69  e an automatic i
1f708 6e 64 65 78 20 61 73 20 61 0a 20 20 20 20 2a 2a  ndex as a.    **
1f709 20 72 65 73 75 6c 74 20 6f 66 20 61 20 50 52 49   result of a PRI
1f70a 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51  MARY KEY or UNIQ
1f70b 55 45 20 63 6c 61 75 73 65 20 6f 6e 20 61 20 63  UE clause on a c
1f70c 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e  olumn definition
1f70d 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 61 20 50 52  , or.    ** a PR
1f70e 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49  IMARY KEY or UNI
1f70f 51 55 45 20 63 6c 61 75 73 65 20 66 6f 6c 6c 6f  QUE clause follo
1f710 77 69 6e 67 20 74 68 65 20 63 6f 6c 75 6d 6e 20  wing the column 
1f711 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a 20 20 20  definitions..   
1f712 20 2a 2a 20 69 2e 65 2e 20 6f 6e 65 20 6f 66 3a   ** i.e. one of:
1f713 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43  .    **.    ** C
1f714 52 45 41 54 45 20 54 41 42 4c 45 20 74 28 78 20  REATE TABLE t(x 
1f715 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 79 29 3b  PRIMARY KEY, y);
1f716 0a 20 20 20 20 2a 2a 20 43 52 45 41 54 45 20 54  .    ** CREATE T
1f717 41 42 4c 45 20 74 28 78 2c 20 79 2c 20 55 4e 49  ABLE t(x, y, UNI
1f718 51 55 45 28 78 2c 20 79 29 29 3b 0a 20 20 20 20  QUE(x, y));.    
1f719 2a 2a 0a 20 20 20 20 2a 2a 20 45 69 74 68 65 72  **.    ** Either
1f71a 20 77 61 79 2c 20 63 68 65 63 6b 20 74 6f 20 73   way, check to s
1f71b 65 65 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  ee if the table 
1f71c 61 6c 72 65 61 64 79 20 68 61 73 20 73 75 63 68  already has such
1f71d 20 61 6e 20 69 6e 64 65 78 2e 20 49 66 0a 20 20   an index. If.  
1f71e 20 20 2a 2a 20 73 6f 2c 20 64 6f 6e 27 74 20 62    ** so, don't b
1f71f 6f 74 68 65 72 20 63 72 65 61 74 69 6e 67 20 74  other creating t
1f720 68 69 73 20 6f 6e 65 2e 20 54 68 69 73 20 6f 6e  his one. This on
1f721 6c 79 20 61 70 70 6c 69 65 73 20 74 6f 0a 20 20  ly applies to.  
1f722 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c    ** automatical
1f723 6c 79 20 63 72 65 61 74 65 64 20 69 6e 64 69 63  ly created indic
1f724 65 73 2e 20 55 73 65 72 73 20 63 61 6e 20 64 6f  es. Users can do
1f725 20 61 73 20 74 68 65 79 20 77 69 73 68 20 77 69   as they wish wi
1f726 74 68 0a 20 20 20 20 2a 2a 20 65 78 70 6c 69 63  th.    ** explic
1f727 69 74 20 69 6e 64 69 63 65 73 2e 0a 20 20 20 20  it indices..    
1f728 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49  */.    Index *pI
1f729 64 78 3b 0a 20 20 20 20 66 6f 72 28 70 49 64 78  dx;.    for(pIdx
1f72a 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
1f72b 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
1f72c 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 6e  pNext){.      in
1f72d 74 20 6b 3b 0a 20 20 20 20 20 20 61 73 73 65 72  t k;.      asser
1f72e 74 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72  t( pIdx->onError
1f72f 21 3d 4f 45 5f 4e 6f 6e 65 20 29 3b 0a 20 20 20  !=OE_None );.   
1f730 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d     assert( pIdx-
1f731 3e 61 75 74 6f 49 6e 64 65 78 20 29 3b 0a 20 20  >autoIndex );.  
1f732 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 64      assert( pInd
1f733 65 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f  ex->onError!=OE_
1f734 4e 6f 6e 65 20 29 3b 0a 0a 20 20 20 20 20 20 69  None );..      i
1f735 66 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  f( pIdx->nColumn
1f736 21 3d 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d  !=pIndex->nColum
1f737 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  n ) continue;.  
1f738 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70      for(k=0; k<p
1f739 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b  Idx->nColumn; k+
1f73a 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73  +){.        cons
1f73b 74 20 63 68 61 72 20 2a 7a 31 20 3d 20 70 49 64  t char *z1 = pId
1f73c 78 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20  x->azColl[k];.  
1f73d 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
1f73e 20 2a 7a 32 20 3d 20 70 49 6e 64 65 78 2d 3e 61   *z2 = pIndex->a
1f73f 7a 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20  zColl[k];.      
1f740 20 20 69 66 28 20 70 49 64 78 2d 3e 61 69 43 6f    if( pIdx->aiCo
1f741 6c 75 6d 6e 5b 6b 5d 21 3d 70 49 6e 64 65 78 2d  lumn[k]!=pIndex-
1f742 3e 61 69 43 6f 6c 75 6d 6e 5b 6b 5d 20 29 20 62  >aiColumn[k] ) b
1f743 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 69 66  reak;.        if
1f744 28 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64  ( pIdx->aSortOrd
1f745 65 72 5b 6b 5d 21 3d 70 49 6e 64 65 78 2d 3e 61  er[k]!=pIndex->a
1f746 53 6f 72 74 4f 72 64 65 72 5b 6b 5d 20 29 20 62  SortOrder[k] ) b
1f747 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 69 66  reak;.        if
1f748 28 20 7a 31 21 3d 7a 32 20 26 26 20 73 71 6c 69  ( z1!=z2 && sqli
1f749 74 65 33 53 74 72 49 43 6d 70 28 7a 31 2c 20 7a  te3StrICmp(z1, z
1f74a 32 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  2) ) break;.    
1f74b 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6b 3d    }.      if( k=
1f74c 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29  =pIdx->nColumn )
1f74d 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49  {.        if( pI
1f74e 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 70 49 6e  dx->onError!=pIn
1f74f 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 20 29 7b 0a  dex->onError ){.
1f750 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69            /* Thi
1f751 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 72 65  s constraint cre
1f752 61 74 65 73 20 74 68 65 20 73 61 6d 65 20 69 6e  ates the same in
1f753 64 65 78 20 61 73 20 61 20 70 72 65 76 69 6f 75  dex as a previou
1f754 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63  s.          ** c
1f755 6f 6e 73 74 72 61 69 6e 74 20 73 70 65 63 69 66  onstraint specif
1f756 69 65 64 20 73 6f 6d 65 77 68 65 72 65 20 69 6e  ied somewhere in
1f757 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c   the CREATE TABL
1f758 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20  E statement..   
1f759 20 20 20 20 20 20 20 2a 2a 20 48 6f 77 65 76 65         ** Howeve
1f75a 72 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43  r the ON CONFLIC
1f75b 54 20 63 6c 61 75 73 65 73 20 61 72 65 20 64 69  T clauses are di
1f75c 66 66 65 72 65 6e 74 2e 20 49 66 20 62 6f 74 68  fferent. If both
1f75d 20 74 68 69 73 20 0a 20 20 20 20 20 20 20 20 20   this .         
1f75e 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 61   ** constraint a
1f75f 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  nd the previous 
1f760 65 71 75 69 76 61 6c 65 6e 74 20 63 6f 6e 73 74  equivalent const
1f761 72 61 69 6e 74 20 68 61 76 65 20 65 78 70 6c 69  raint have expli
1f762 63 69 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  cit.          **
1f763 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61   ON CONFLICT cla
1f764 75 73 65 73 20 74 68 69 73 20 69 73 20 61 6e 20  uses this is an 
1f765 65 72 72 6f 72 2e 20 4f 74 68 65 72 77 69 73 65  error. Otherwise
1f766 2c 20 75 73 65 20 74 68 65 0a 20 20 20 20 20 20  , use the.      
1f767 20 20 20 20 2a 2a 20 65 78 70 6c 69 63 69 74 6c      ** explicitl
1f768 79 20 73 70 65 63 69 66 69 65 64 20 62 65 68 61  y specified beha
1f769 76 69 6f 75 72 20 66 6f 72 20 74 68 65 20 69 6e  viour for the in
1f76a 64 65 78 2e 0a 20 20 20 20 20 20 20 20 20 20 2a  dex..          *
1f76b 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  /.          if( 
1f76c 21 28 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d  !(pIdx->onError=
1f76d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 7c 7c 20 70  =OE_Default || p
1f76e 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d  Index->onError==
1f76f 4f 45 5f 44 65 66 61 75 6c 74 29 20 29 7b 0a 20  OE_Default) ){. 
1f770 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1f771 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1f772 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  e, .            
1f773 20 20 20 20 22 63 6f 6e 66 6c 69 63 74 69 6e 67      "conflicting
1f774 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61   ON CONFLICT cla
1f775 75 73 65 73 20 73 70 65 63 69 66 69 65 64 22 2c  uses specified",
1f776 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d   0);.          }
1f777 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
1f778 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45  Idx->onError==OE
1f779 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20  _Default ){.    
1f77a 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e 6f 6e          pIdx->on
1f77b 45 72 72 6f 72 20 3d 20 70 49 6e 64 65 78 2d 3e  Error = pIndex->
1f77c 6f 6e 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 20  onError;.       
1f77d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
1f77e 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74         goto exit
1f77f 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
1f780 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
1f781 0a 0a 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20  ..  /* Link the 
1f782 6e 65 77 20 49 6e 64 65 78 20 73 74 72 75 63 74  new Index struct
1f783 75 72 65 20 74 6f 20 69 74 73 20 74 61 62 6c 65  ure to its table
1f784 20 61 6e 64 20 74 6f 20 74 68 65 20 6f 74 68 65   and to the othe
1f785 72 0a 20 20 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79  r.  ** in-memory
1f786 20 64 61 74 61 62 61 73 65 20 73 74 72 75 63 74   database struct
1f787 75 72 65 73 2e 20 0a 20 20 2a 2f 0a 20 20 69 66  ures. .  */.  if
1f788 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20  ( db->init.busy 
1f789 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 3b  ){.    Index *p;
1f78a 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
1f78b 48 61 73 68 49 6e 73 65 72 74 28 26 70 49 6e 64  HashInsert(&pInd
1f78c 65 78 2d 3e 70 53 63 68 65 6d 61 2d 3e 69 64 78  ex->pSchema->idx
1f78d 48 61 73 68 2c 20 0a 20 20 20 20 20 20 20 20 20  Hash, .         
1f78e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f78f 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 73  pIndex->zName, s
1f790 74 72 6c 65 6e 28 70 49 6e 64 65 78 2d 3e 7a 4e  trlen(pIndex->zN
1f791 61 6d 65 29 2b 31 2c 20 70 49 6e 64 65 78 29 3b  ame)+1, pIndex);
1f792 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20  .    if( p ){.  
1f793 20 20 20 20 61 73 73 65 72 74 28 20 70 3d 3d 70      assert( p==p
1f794 49 6e 64 65 78 20 29 3b 20 20 2f 2a 20 4d 61 6c  Index );  /* Mal
1f795 6c 6f 63 20 6d 75 73 74 20 68 61 76 65 20 66 61  loc must have fa
1f796 69 6c 65 64 20 2a 2f 0a 20 20 20 20 20 20 64 62  iled */.      db
1f797 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
1f798 20 31 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65   1;.      goto e
1f799 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
1f79a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e  ;.    }.    db->
1f79b 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f  flags |= SQLITE_
1f79c 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20  InternChanges;. 
1f79d 20 20 20 69 66 28 20 70 54 62 6c 4e 61 6d 65 21     if( pTblName!
1f79e 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 64  =0 ){.      pInd
1f79f 65 78 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e 69  ex->tnum = db->i
1f7a0 6e 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20 20  nit.newTnum;.   
1f7a1 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
1f7a2 74 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  the db->init.bus
1f7a3 79 20 69 73 20 30 20 74 68 65 6e 20 63 72 65 61  y is 0 then crea
1f7a4 74 65 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 20  te the index on 
1f7a5 64 69 73 6b 2e 20 20 54 68 69 73 0a 20 20 2a 2a  disk.  This.  **
1f7a6 20 69 6e 76 6f 6c 76 65 73 20 77 72 69 74 69 6e   involves writin
1f7a7 67 20 74 68 65 20 69 6e 64 65 78 20 69 6e 74 6f  g the index into
1f7a8 20 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c   the master tabl
1f7a9 65 20 61 6e 64 20 66 69 6c 6c 69 6e 67 20 69 6e  e and filling in
1f7aa 20 74 68 65 0a 20 20 2a 2a 20 69 6e 64 65 78 20   the.  ** index 
1f7ab 77 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74  with the current
1f7ac 20 74 61 62 6c 65 20 63 6f 6e 74 65 6e 74 73 2e   table contents.
1f7ad 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 64  .  **.  ** The d
1f7ae 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20  b->init.busy is 
1f7af 30 20 77 68 65 6e 20 74 68 65 20 75 73 65 72 20  0 when the user 
1f7b0 66 69 72 73 74 20 65 6e 74 65 72 73 20 61 20 43  first enters a C
1f7b1 52 45 41 54 45 20 49 4e 44 45 58 20 0a 20 20 2a  REATE INDEX .  *
1f7b2 2a 20 63 6f 6d 6d 61 6e 64 2e 20 20 64 62 2d 3e  * command.  db->
1f7b3 69 6e 69 74 2e 62 75 73 79 20 69 73 20 31 20 77  init.busy is 1 w
1f7b4 68 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 69  hen a database i
1f7b5 73 20 6f 70 65 6e 65 64 20 61 6e 64 20 0a 20 20  s opened and .  
1f7b6 2a 2a 20 43 52 45 41 54 45 20 49 4e 44 45 58 20  ** CREATE INDEX 
1f7b7 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 72  statements are r
1f7b8 65 61 64 20 6f 75 74 20 6f 66 20 74 68 65 20 6d  ead out of the m
1f7b9 61 73 74 65 72 20 74 61 62 6c 65 2e 20 20 49 6e  aster table.  In
1f7ba 0a 20 20 2a 2a 20 74 68 65 20 6c 61 74 74 65 72  .  ** the latter
1f7bb 20 63 61 73 65 20 74 68 65 20 69 6e 64 65 78 20   case the index 
1f7bc 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 6f  already exists o
1f7bd 6e 20 64 69 73 6b 2c 20 77 68 69 63 68 20 69 73  n disk, which is
1f7be 20 77 68 79 0a 20 20 2a 2a 20 77 65 20 64 6f 6e   why.  ** we don
1f7bf 27 74 20 77 61 6e 74 20 74 6f 20 72 65 63 72 65  't want to recre
1f7c0 61 74 65 20 69 74 2e 0a 20 20 2a 2a 0a 20 20 2a  ate it..  **.  *
1f7c1 2a 20 49 66 20 70 54 62 6c 4e 61 6d 65 3d 3d 30  * If pTblName==0
1f7c2 20 69 74 20 6d 65 61 6e 73 20 74 68 69 73 20 69   it means this i
1f7c3 6e 64 65 78 20 69 73 20 67 65 6e 65 72 61 74 65  ndex is generate
1f7c4 64 20 61 73 20 61 20 70 72 69 6d 61 72 79 20 6b  d as a primary k
1f7c5 65 79 0a 20 20 2a 2a 20 6f 72 20 55 4e 49 51 55  ey.  ** or UNIQU
1f7c6 45 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 66 20  E constraint of 
1f7c7 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  a CREATE TABLE s
1f7c8 74 61 74 65 6d 65 6e 74 2e 20 20 53 69 6e 63 65  tatement.  Since
1f7c9 20 74 68 65 20 74 61 62 6c 65 0a 20 20 2a 2a 20   the table.  ** 
1f7ca 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 63 72  has just been cr
1f7cb 65 61 74 65 64 2c 20 69 74 20 63 6f 6e 74 61 69  eated, it contai
1f7cc 6e 73 20 6e 6f 20 64 61 74 61 20 61 6e 64 20 74  ns no data and t
1f7cd 68 65 20 69 6e 64 65 78 20 69 6e 69 74 69 61 6c  he index initial
1f7ce 69 7a 61 74 69 6f 6e 0a 20 20 2a 2a 20 73 74 65  ization.  ** ste
1f7cf 70 20 63 61 6e 20 62 65 20 73 6b 69 70 70 65 64  p can be skipped
1f7d0 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66  ..  */.  else if
1f7d1 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d  ( db->init.busy=
1f7d2 3d 30 20 29 7b 0a 20 20 20 20 56 64 62 65 20 2a  =0 ){.    Vdbe *
1f7d3 76 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 74  v;.    char *zSt
1f7d4 6d 74 3b 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d  mt;.    int iMem
1f7d5 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
1f7d6 6d 3b 0a 0a 20 20 20 20 76 20 3d 20 73 71 6c 69  m;..    v = sqli
1f7d7 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
1f7d8 65 29 3b 0a 20 20 20 20 69 66 28 20 76 3d 3d 30  e);.    if( v==0
1f7d9 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65   ) goto exit_cre
1f7da 61 74 65 5f 69 6e 64 65 78 3b 0a 0a 0a 20 20 20  ate_index;...   
1f7db 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20 72   /* Create the r
1f7dc 6f 6f 74 70 61 67 65 20 66 6f 72 20 74 68 65 20  ootpage for the 
1f7dd 69 6e 64 65 78 0a 20 20 20 20 2a 2f 0a 20 20 20  index.    */.   
1f7de 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69   sqlite3BeginWri
1f7df 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72  teOperation(pPar
1f7e0 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 20  se, 1, iDb);.   
1f7e1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1f7e2 70 32 28 76 2c 20 4f 50 5f 43 72 65 61 74 65 49  p2(v, OP_CreateI
1f7e3 6e 64 65 78 2c 20 69 44 62 2c 20 69 4d 65 6d 29  ndex, iDb, iMem)
1f7e4 3b 0a 0a 20 20 20 20 2f 2a 20 47 61 74 68 65 72  ;..    /* Gather
1f7e5 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65   the complete te
1f7e6 78 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45  xt of the CREATE
1f7e7 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74   INDEX statement
1f7e8 20 69 6e 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65   into.    ** the
1f7e9 20 7a 53 74 6d 74 20 76 61 72 69 61 62 6c 65 0a   zStmt variable.
1f7ea 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
1f7eb 53 74 61 72 74 20 26 26 20 70 45 6e 64 20 29 7b  Start && pEnd ){
1f7ec 0a 20 20 20 20 20 20 2f 2a 20 41 20 6e 61 6d 65  .      /* A name
1f7ed 64 20 69 6e 64 65 78 20 77 69 74 68 20 61 6e 20  d index with an 
1f7ee 65 78 70 6c 69 63 69 74 20 43 52 45 41 54 45 20  explicit CREATE 
1f7ef 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74 20  INDEX statement 
1f7f0 2a 2f 0a 20 20 20 20 20 20 7a 53 74 6d 74 20 3d  */.      zStmt =
1f7f1 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
1f7f2 64 62 2c 20 22 43 52 45 41 54 45 25 73 20 49 4e  db, "CREATE%s IN
1f7f3 44 45 58 20 25 2e 2a 73 22 2c 0a 20 20 20 20 20  DEX %.*s",.     
1f7f4 20 20 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e     onError==OE_N
1f7f5 6f 6e 65 20 3f 20 22 22 20 3a 20 22 20 55 4e 49  one ? "" : " UNI
1f7f6 51 55 45 22 2c 0a 20 20 20 20 20 20 20 20 70 45  QUE",.        pE
1f7f7 6e 64 2d 3e 7a 20 2d 20 70 4e 61 6d 65 2d 3e 7a  nd->z - pName->z
1f7f8 20 2b 20 31 2c 0a 20 20 20 20 20 20 20 20 70 4e   + 1,.        pN
1f7f9 61 6d 65 2d 3e 7a 29 3b 0a 20 20 20 20 7d 65 6c  ame->z);.    }el
1f7fa 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6e 20  se{.      /* An 
1f7fb 61 75 74 6f 6d 61 74 69 63 20 69 6e 64 65 78 20  automatic index 
1f7fc 63 72 65 61 74 65 64 20 62 79 20 61 20 50 52 49  created by a PRI
1f7fd 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51  MARY KEY or UNIQ
1f7fe 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f  UE constraint */
1f7ff 0a 20 20 20 20 20 20 2f 2a 20 7a 53 74 6d 74 20  .      /* zStmt 
1f800 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
1f801 28 22 22 29 3b 20 2a 2f 0a 20 20 20 20 20 20 7a  (""); */.      z
1f802 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  Stmt = 0;.    }.
1f803 0a 20 20 20 20 2f 2a 20 41 64 64 20 61 6e 20 65  .    /* Add an e
1f804 6e 74 72 79 20 69 6e 20 73 71 6c 69 74 65 5f 6d  ntry in sqlite_m
1f805 61 73 74 65 72 20 66 6f 72 20 74 68 69 73 20 69  aster for this i
1f806 6e 64 65 78 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ndex.    */.    
1f807 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
1f808 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  se(pParse, .    
1f809 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f      "INSERT INTO
1f80a 20 25 51 2e 25 73 20 56 41 4c 55 45 53 28 27 69   %Q.%s VALUES('i
1f80b 6e 64 65 78 27 2c 25 51 2c 25 51 2c 23 25 64 2c  ndex',%Q,%Q,#%d,
1f80c 25 51 29 3b 22 2c 0a 20 20 20 20 20 20 20 20 64  %Q);",.        d
1f80d 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
1f80e 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28  e, SCHEMA_TABLE(
1f80f 69 44 62 29 2c 0a 20 20 20 20 20 20 20 20 70 49  iDb),.        pI
1f810 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20  ndex->zName,.   
1f811 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65       pTab->zName
1f812 2c 0a 20 20 20 20 20 20 20 20 69 4d 65 6d 2c 0a  ,.        iMem,.
1f813 20 20 20 20 20 20 20 20 7a 53 74 6d 74 0a 20 20          zStmt.  
1f814 20 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33    );.    sqlite3
1f815 44 62 46 72 65 65 28 64 62 2c 20 7a 53 74 6d 74  DbFree(db, zStmt
1f816 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6c 6c 20  );..    /* Fill 
1f817 74 68 65 20 69 6e 64 65 78 20 77 69 74 68 20 64  the index with d
1f818 61 74 61 20 61 6e 64 20 72 65 70 61 72 73 65 20  ata and reparse 
1f819 74 68 65 20 73 63 68 65 6d 61 2e 20 43 6f 64 65  the schema. Code
1f81a 20 61 6e 20 4f 50 5f 45 78 70 69 72 65 0a 20 20   an OP_Expire.  
1f81b 20 20 2a 2a 20 74 6f 20 69 6e 76 61 6c 69 64 61    ** to invalida
1f81c 74 65 20 61 6c 6c 20 70 72 65 2d 63 6f 6d 70 69  te all pre-compi
1f81d 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  led statements..
1f81e 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
1f81f 54 62 6c 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20  TblName ){.     
1f820 20 73 71 6c 69 74 65 33 52 65 66 69 6c 6c 49 6e   sqlite3RefillIn
1f821 64 65 78 28 70 50 61 72 73 65 2c 20 70 49 6e 64  dex(pParse, pInd
1f822 65 78 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 20  ex, iMem);.     
1f823 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f   sqlite3ChangeCo
1f824 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44 62  okie(pParse, iDb
1f825 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1f826 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
1f827 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44  _ParseSchema, iD
1f828 62 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20  b, 0, 0,.       
1f829 20 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66    sqlite3MPrintf
1f82a 28 64 62 2c 20 22 6e 61 6d 65 3d 27 25 71 27 22  (db, "name='%q'"
1f82b 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 29  , pIndex->zName)
1f82c 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  , P4_DYNAMIC);. 
1f82d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f82e 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 45 78 70  AddOp1(v, OP_Exp
1f82f 69 72 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20  ire, 0);.    }. 
1f830 20 7d 0a 0a 20 20 2f 2a 20 57 68 65 6e 20 61 64   }..  /* When ad
1f831 64 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 74 6f  ding an index to
1f832 20 74 68 65 20 6c 69 73 74 20 6f 66 20 69 6e 64   the list of ind
1f833 69 63 65 73 20 66 6f 72 20 61 20 74 61 62 6c 65  ices for a table
1f834 2c 20 6d 61 6b 65 0a 20 20 2a 2a 20 73 75 72 65  , make.  ** sure
1f835 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6c 61 62   all indices lab
1f836 65 6c 65 64 20 4f 45 5f 52 65 70 6c 61 63 65 20  eled OE_Replace 
1f837 63 6f 6d 65 20 61 66 74 65 72 20 61 6c 6c 20 74  come after all t
1f838 68 6f 73 65 20 6c 61 62 65 6c 65 64 0a 20 20 2a  hose labeled.  *
1f839 2a 20 4f 45 5f 49 67 6e 6f 72 65 2e 20 20 54 68  * OE_Ignore.  Th
1f83a 69 73 20 69 73 20 6e 65 63 65 73 73 61 72 79 20  is is necessary 
1f83b 66 6f 72 20 74 68 65 20 63 6f 72 72 65 63 74 20  for the correct 
1f83c 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 55 50 44  operation of UPD
1f83d 41 54 45 0a 20 20 2a 2a 20 61 6e 64 20 49 4e 53  ATE.  ** and INS
1f83e 45 52 54 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ERT..  */.  if( 
1f83f 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c 7c  db->init.busy ||
1f840 20 70 54 62 6c 4e 61 6d 65 3d 3d 30 20 29 7b 0a   pTblName==0 ){.
1f841 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 21      if( onError!
1f842 3d 4f 45 5f 52 65 70 6c 61 63 65 20 7c 7c 20 70  =OE_Replace || p
1f843 54 61 62 2d 3e 70 49 6e 64 65 78 3d 3d 30 0a 20  Tab->pIndex==0. 
1f844 20 20 20 20 20 20 20 20 7c 7c 20 70 54 61 62 2d          || pTab-
1f845 3e 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72  >pIndex->onError
1f846 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 29 7b 0a 20  ==OE_Replace){. 
1f847 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 4e 65       pIndex->pNe
1f848 78 74 20 3d 20 70 54 61 62 2d 3e 70 49 6e 64 65  xt = pTab->pInde
1f849 78 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 70  x;.      pTab->p
1f84a 49 6e 64 65 78 20 3d 20 70 49 6e 64 65 78 3b 0a  Index = pIndex;.
1f84b 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1f84c 20 49 6e 64 65 78 20 2a 70 4f 74 68 65 72 20 3d   Index *pOther =
1f84d 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 0a 20   pTab->pIndex;. 
1f84e 20 20 20 20 20 77 68 69 6c 65 28 20 70 4f 74 68       while( pOth
1f84f 65 72 2d 3e 70 4e 65 78 74 20 26 26 20 70 4f 74  er->pNext && pOt
1f850 68 65 72 2d 3e 70 4e 65 78 74 2d 3e 6f 6e 45 72  her->pNext->onEr
1f851 72 6f 72 21 3d 4f 45 5f 52 65 70 6c 61 63 65 20  ror!=OE_Replace 
1f852 29 7b 0a 20 20 20 20 20 20 20 20 70 4f 74 68 65  ){.        pOthe
1f853 72 20 3d 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78  r = pOther->pNex
1f854 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  t;.      }.     
1f855 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 20 3d   pIndex->pNext =
1f856 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 3b 0a   pOther->pNext;.
1f857 20 20 20 20 20 20 70 4f 74 68 65 72 2d 3e 70 4e        pOther->pN
1f858 65 78 74 20 3d 20 70 49 6e 64 65 78 3b 0a 20 20  ext = pIndex;.  
1f859 20 20 7d 0a 20 20 20 20 70 49 6e 64 65 78 20 3d    }.    pIndex =
1f85a 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c   0;.  }..  /* Cl
1f85b 65 61 6e 20 75 70 20 62 65 66 6f 72 65 20 65 78  ean up before ex
1f85c 69 74 69 6e 67 20 2a 2f 0a 65 78 69 74 5f 63 72  iting */.exit_cr
1f85d 65 61 74 65 5f 69 6e 64 65 78 3a 0a 20 20 69 66  eate_index:.  if
1f85e 28 20 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20  ( pIndex ){.    
1f85f 66 72 65 65 49 6e 64 65 78 28 70 49 6e 64 65 78  freeIndex(pIndex
1f860 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
1f861 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64  ExprListDelete(d
1f862 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 73 71 6c  b, pList);.  sql
1f863 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
1f864 65 28 64 62 2c 20 70 54 62 6c 4e 61 6d 65 29 3b  e(db, pTblName);
1f865 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
1f866 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 72  (db, zName);.  r
1f867 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  eturn;.}../*.** 
1f868 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
1f869 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 66   make sure the f
1f86a 69 6c 65 20 66 6f 72 6d 61 74 20 6e 75 6d 62 65  ile format numbe
1f86b 72 20 69 73 20 61 74 20 6c 65 61 73 74 20 6d 69  r is at least mi
1f86c 6e 46 6f 72 6d 61 74 2e 0a 2a 2a 20 54 68 65 20  nFormat..** The 
1f86d 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20 77  generated code w
1f86e 69 6c 6c 20 69 6e 63 72 65 61 73 65 20 74 68 65  ill increase the
1f86f 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 6e 75 6d   file format num
1f870 62 65 72 20 69 66 20 6e 65 63 65 73 73 61 72 79  ber if necessary
1f871 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1f872 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1f873 4d 69 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61  MinimumFileForma
1f874 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  t(Parse *pParse,
1f875 20 69 6e 74 20 69 44 62 2c 20 69 6e 74 20 6d 69   int iDb, int mi
1f876 6e 46 6f 72 6d 61 74 29 7b 0a 20 20 56 64 62 65  nFormat){.  Vdbe
1f877 20 2a 76 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74   *v;.  v = sqlit
1f878 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
1f879 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20  );.  if( v ){.  
1f87a 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74    int r1 = sqlit
1f87b 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
1f87c 72 73 65 29 3b 0a 20 20 20 20 69 6e 74 20 72 32  rse);.    int r2
1f87d 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
1f87e 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
1f87f 20 20 69 6e 74 20 6a 31 3b 0a 20 20 20 20 73 71    int j1;.    sq
1f880 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
1f881 76 2c 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65  v, OP_ReadCookie
1f882 2c 20 69 44 62 2c 20 72 31 2c 20 31 29 3b 0a 20  , iDb, r1, 1);. 
1f883 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73     sqlite3VdbeUs
1f884 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b  esBtree(v, iDb);
1f885 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1f886 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
1f887 65 67 65 72 2c 20 6d 69 6e 46 6f 72 6d 61 74 2c  eger, minFormat,
1f888 20 72 32 29 3b 0a 20 20 20 20 6a 31 20 3d 20 73   r2);.    j1 = s
1f889 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
1f88a 28 76 2c 20 4f 50 5f 47 65 2c 20 72 32 2c 20 30  (v, OP_Ge, r2, 0
1f88b 2c 20 72 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  , r1);.    sqlit
1f88c 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
1f88d 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44  OP_SetCookie, iD
1f88e 62 2c 20 31 2c 20 72 32 29 3b 0a 20 20 20 20 73  b, 1, r2);.    s
1f88f 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
1f890 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 73  re(v, j1);.    s
1f891 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
1f892 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29  pReg(pParse, r1)
1f893 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c  ;.    sqlite3Rel
1f894 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
1f895 73 65 2c 20 72 32 29 3b 0a 20 20 7d 0a 7d 0a 0a  se, r2);.  }.}..
1f896 2f 2a 0a 2a 2a 20 46 69 6c 6c 20 74 68 65 20 49  /*.** Fill the I
1f897 6e 64 65 78 2e 61 69 52 6f 77 45 73 74 5b 5d 20  ndex.aiRowEst[] 
1f898 61 72 72 61 79 20 77 69 74 68 20 64 65 66 61 75  array with defau
1f899 6c 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2d  lt information -
1f89a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20   information.** 
1f89b 74 6f 20 62 65 20 75 73 65 64 20 77 68 65 6e 20  to be used when 
1f89c 77 65 20 68 61 76 65 20 6e 6f 74 20 72 75 6e 20  we have not run 
1f89d 74 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d  the ANALYZE comm
1f89e 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 61 69 52 6f 77  and..**.** aiRow
1f89f 45 73 74 5b 30 5d 20 69 73 20 73 75 70 70 6f 73  Est[0] is suppos
1f8a0 65 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65  e to contain the
1f8a1 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   number of eleme
1f8a2 6e 74 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78  nts in the index
1f8a3 2e 0a 2a 2a 20 53 69 6e 63 65 20 77 65 20 64 6f  ..** Since we do
1f8a4 20 6e 6f 74 20 6b 6e 6f 77 2c 20 67 75 65 73 73   not know, guess
1f8a5 20 31 20 6d 69 6c 6c 69 6f 6e 2e 20 20 61 69 52   1 million.  aiR
1f8a6 6f 77 45 73 74 5b 31 5d 20 69 73 20 61 6e 20 65  owEst[1] is an e
1f8a7 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 0a 2a  stimate of the.*
1f8a8 2a 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  * number of rows
1f8a9 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 74 68   in the table th
1f8aa 61 74 20 6d 61 74 63 68 20 61 6e 79 20 70 61 72  at match any par
1f8ab 74 69 63 75 6c 61 72 20 76 61 6c 75 65 20 6f 66  ticular value of
1f8ac 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 63 6f   the.** first co
1f8ad 6c 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65  lumn of the inde
1f8ae 78 2e 20 20 61 69 52 6f 77 45 73 74 5b 32 5d 20  x.  aiRowEst[2] 
1f8af 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f  is an estimate o
1f8b0 66 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20  f the number.** 
1f8b1 6f 66 20 72 6f 77 73 20 74 68 61 74 20 6d 61 74  of rows that mat
1f8b2 63 68 20 61 6e 79 20 70 61 72 74 69 63 75 6c 61  ch any particula
1f8b3 72 20 63 6f 6d 62 69 6e 69 61 74 69 6f 6e 20 6f  r combiniation o
1f8b4 66 20 74 68 65 20 66 69 72 73 74 20 32 20 63 6f  f the first 2 co
1f8b5 6c 75 6d 6e 73 0a 2a 2a 20 6f 66 20 74 68 65 20  lumns.** of the 
1f8b6 69 6e 64 65 78 2e 20 20 41 6e 64 20 73 6f 20 66  index.  And so f
1f8b7 6f 72 74 68 2e 20 20 49 74 20 6d 75 73 74 20 61  orth.  It must a
1f8b8 6c 77 61 79 73 20 62 65 20 74 68 65 20 63 61 73  lways be the cas
1f8b9 65 20 74 68 61 74 0a 2a 0a 2a 2a 20 20 20 20 20  e that.*.**     
1f8ba 20 20 20 20 20 20 61 69 52 6f 77 45 73 74 5b 4e        aiRowEst[N
1f8bb 5d 3c 3d 61 69 52 6f 77 45 73 74 5b 4e 2d 31 5d  ]<=aiRowEst[N-1]
1f8bc 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 69  .**           ai
1f8bd 52 6f 77 45 73 74 5b 4e 5d 3e 3d 31 0a 2a 2a 0a  RowEst[N]>=1.**.
1f8be 2a 2a 20 41 70 61 72 74 20 66 72 6f 6d 20 74 68  ** Apart from th
1f8bf 61 74 2c 20 77 65 20 68 61 76 65 20 6c 69 74 74  at, we have litt
1f8c0 6c 65 20 74 6f 20 67 6f 20 6f 6e 20 62 65 73 69  le to go on besi
1f8c1 64 65 73 20 69 6e 74 75 69 74 69 6f 6e 20 61 73  des intuition as
1f8c2 20 74 6f 0a 2a 2a 20 68 6f 77 20 61 69 52 6f 77   to.** how aiRow
1f8c3 45 73 74 5b 5d 20 73 68 6f 75 6c 64 20 62 65 20  Est[] should be 
1f8c4 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 54 68  initialized.  Th
1f8c5 65 20 6e 75 6d 62 65 72 73 20 67 65 6e 65 72 61  e numbers genera
1f8c6 74 65 64 20 68 65 72 65 0a 2a 2a 20 61 72 65 20  ted here.** are 
1f8c7 62 61 73 65 64 20 6f 6e 20 74 79 70 69 63 61 6c  based on typical
1f8c8 20 76 61 6c 75 65 73 20 66 6f 75 6e 64 20 69 6e   values found in
1f8c9 20 61 63 74 75 61 6c 20 69 6e 64 69 63 65 73 2e   actual indices.
1f8ca 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1f8cb 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
1f8cc 65 66 61 75 6c 74 52 6f 77 45 73 74 28 49 6e 64  efaultRowEst(Ind
1f8cd 65 78 20 2a 70 49 64 78 29 7b 0a 20 20 75 6e 73  ex *pIdx){.  uns
1f8ce 69 67 6e 65 64 20 2a 61 20 3d 20 70 49 64 78 2d  igned *a = pIdx-
1f8cf 3e 61 69 52 6f 77 45 73 74 3b 0a 20 20 69 6e 74  >aiRowEst;.  int
1f8d0 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 61 21   i;.  assert( a!
1f8d1 3d 30 20 29 3b 0a 20 20 61 5b 30 5d 20 3d 20 31  =0 );.  a[0] = 1
1f8d2 30 30 30 30 30 30 3b 0a 20 20 66 6f 72 28 69 3d  000000;.  for(i=
1f8d3 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69  pIdx->nColumn; i
1f8d4 3e 3d 35 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 61  >=5; i--){.    a
1f8d5 5b 69 5d 20 3d 20 35 3b 0a 20 20 7d 0a 20 20 77  [i] = 5;.  }.  w
1f8d6 68 69 6c 65 28 20 69 3e 3d 31 20 29 7b 0a 20 20  hile( i>=1 ){.  
1f8d7 20 20 61 5b 69 5d 20 3d 20 31 31 20 2d 20 69 3b    a[i] = 11 - i;
1f8d8 0a 20 20 20 20 69 2d 2d 3b 0a 20 20 7d 0a 20 20  .    i--;.  }.  
1f8d9 69 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f  if( pIdx->onErro
1f8da 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 0a 20 20  r!=OE_None ){.  
1f8db 20 20 61 5b 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d    a[pIdx->nColum
1f8dc 6e 5d 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f  n] = 1;.  }.}../
1f8dd 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1f8de 65 20 77 69 6c 6c 20 64 72 6f 70 20 61 6e 20 65  e will drop an e
1f8df 78 69 73 74 69 6e 67 20 6e 61 6d 65 64 20 69 6e  xisting named in
1f8e0 64 65 78 2e 20 20 54 68 69 73 20 72 6f 75 74 69  dex.  This routi
1f8e1 6e 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 73  ne.** implements
1f8e2 20 74 68 65 20 44 52 4f 50 20 49 4e 44 45 58 20   the DROP INDEX 
1f8e3 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51  statement..*/.SQ
1f8e4 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1f8e5 64 20 73 71 6c 69 74 65 33 44 72 6f 70 49 6e 64  d sqlite3DropInd
1f8e6 65 78 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ex(Parse *pParse
1f8e7 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e 61 6d 65  , SrcList *pName
1f8e8 2c 20 69 6e 74 20 69 66 45 78 69 73 74 73 29 7b  , int ifExists){
1f8e9 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78  .  Index *pIndex
1f8ea 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73  ;.  Vdbe *v;.  s
1f8eb 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
1f8ec 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69  rse->db;.  int i
1f8ed 44 62 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73  Db;..  if( pPars
1f8ee 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d  e->nErr || db->m
1f8ef 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1f8f0 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f     goto exit_dro
1f8f1 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 61  p_index;.  }.  a
1f8f2 73 73 65 72 74 28 20 70 4e 61 6d 65 2d 3e 6e 53  ssert( pName->nS
1f8f3 72 63 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 53  rc==1 );.  if( S
1f8f4 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65  QLITE_OK!=sqlite
1f8f5 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
1f8f6 73 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  se) ){.    goto 
1f8f7 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b  exit_drop_index;
1f8f8 0a 20 20 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20  .  }.  pIndex = 
1f8f9 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78  sqlite3FindIndex
1f8fa 28 64 62 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d  (db, pName->a[0]
1f8fb 2e 7a 4e 61 6d 65 2c 20 70 4e 61 6d 65 2d 3e 61  .zName, pName->a
1f8fc 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a  [0].zDatabase);.
1f8fd 20 20 69 66 28 20 70 49 6e 64 65 78 3d 3d 30 20    if( pIndex==0 
1f8fe 29 7b 0a 20 20 20 20 69 66 28 20 21 69 66 45 78  ){.    if( !ifEx
1f8ff 69 73 74 73 20 29 7b 0a 20 20 20 20 20 20 73 71  ists ){.      sq
1f900 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1f901 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 69  arse, "no such i
1f902 6e 64 65 78 3a 20 25 53 22 2c 20 70 4e 61 6d 65  ndex: %S", pName
1f903 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , 0);.    }.    
1f904 70 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68  pParse->checkSch
1f905 65 6d 61 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74  ema = 1;.    got
1f906 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65  o exit_drop_inde
1f907 78 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 6e  x;.  }.  if( pIn
1f908 64 65 78 2d 3e 61 75 74 6f 49 6e 64 65 78 20 29  dex->autoIndex )
1f909 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
1f90a 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69  orMsg(pParse, "i
1f90b 6e 64 65 78 20 61 73 73 6f 63 69 61 74 65 64 20  ndex associated 
1f90c 77 69 74 68 20 55 4e 49 51 55 45 20 22 0a 20 20  with UNIQUE ".  
1f90d 20 20 20 20 22 6f 72 20 50 52 49 4d 41 52 59 20      "or PRIMARY 
1f90e 4b 45 59 20 63 6f 6e 73 74 72 61 69 6e 74 20 63  KEY constraint c
1f90f 61 6e 6e 6f 74 20 62 65 20 64 72 6f 70 70 65 64  annot be dropped
1f910 22 2c 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20  ", 0);.    goto 
1f911 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b  exit_drop_index;
1f912 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c  .  }.  iDb = sql
1f913 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
1f914 78 28 64 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53  x(db, pIndex->pS
1f915 63 68 65 6d 61 29 3b 0a 23 69 66 6e 64 65 66 20  chema);.#ifndef 
1f916 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
1f917 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20  ORIZATION.  {.  
1f918 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c    int code = SQL
1f919 49 54 45 5f 44 52 4f 50 5f 49 4e 44 45 58 3b 0a  ITE_DROP_INDEX;.
1f91a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20      Table *pTab 
1f91b 3d 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65  = pIndex->pTable
1f91c 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
1f91d 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b   *zDb = db->aDb[
1f91e 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  iDb].zName;.    
1f91f 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62  const char *zTab
1f920 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28   = SCHEMA_TABLE(
1f921 69 44 62 29 3b 0a 20 20 20 20 69 66 28 20 73 71  iDb);.    if( sq
1f922 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
1f923 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45  Parse, SQLITE_DE
1f924 4c 45 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20 7a  LETE, zTab, 0, z
1f925 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  Db) ){.      got
1f926 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65  o exit_drop_inde
1f927 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  x;.    }.    if(
1f928 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26   !OMIT_TEMPDB &&
1f929 20 69 44 62 20 29 20 63 6f 64 65 20 3d 20 53 51   iDb ) code = SQ
1f92a 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49  LITE_DROP_TEMP_I
1f92b 4e 44 45 58 3b 0a 20 20 20 20 69 66 28 20 73 71  NDEX;.    if( sq
1f92c 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
1f92d 50 61 72 73 65 2c 20 63 6f 64 65 2c 20 70 49 6e  Parse, code, pIn
1f92e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62  dex->zName, pTab
1f92f 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b  ->zName, zDb) ){
1f930 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
1f931 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 20  _drop_index;.   
1f932 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20   }.  }.#endif.. 
1f933 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
1f934 65 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 65 20  e to remove the 
1f935 69 6e 64 65 78 20 61 6e 64 20 66 72 6f 6d 20 74  index and from t
1f936 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 20  he master table 
1f937 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  */.  v = sqlite3
1f938 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
1f939 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20  .  if( v ){.    
1f93a 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74  sqlite3BeginWrit
1f93b 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73  eOperation(pPars
1f93c 65 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 20 20  e, 1, iDb);.    
1f93d 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
1f93e 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20  se(pParse,.     
1f93f 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25    "DELETE FROM %
1f940 51 2e 25 73 20 57 48 45 52 45 20 6e 61 6d 65 3d  Q.%s WHERE name=
1f941 25 51 22 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e  %Q",.       db->
1f942 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20  aDb[iDb].zName, 
1f943 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62  SCHEMA_TABLE(iDb
1f944 29 2c 0a 20 20 20 20 20 20 20 70 49 6e 64 65 78  ),.       pIndex
1f945 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 29 3b 0a 20  ->zName.    );. 
1f946 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
1f947 6e 64 54 61 62 6c 65 28 64 62 2c 20 22 73 71 6c  ndTable(db, "sql
1f948 69 74 65 5f 73 74 61 74 31 22 2c 20 64 62 2d 3e  ite_stat1", db->
1f949 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 20  aDb[iDb].zName) 
1f94a 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1f94b 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72  NestedParse(pPar
1f94c 73 65 2c 0a 20 20 20 20 20 20 20 20 22 44 45 4c  se,.        "DEL
1f94d 45 54 45 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69  ETE FROM %Q.sqli
1f94e 74 65 5f 73 74 61 74 31 20 57 48 45 52 45 20 69  te_stat1 WHERE i
1f94f 64 78 3d 25 51 22 2c 0a 20 20 20 20 20 20 20 20  dx=%Q",.        
1f950 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
1f951 6d 65 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d  me, pIndex->zNam
1f952 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d  e.      );.    }
1f953 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e  .    sqlite3Chan
1f954 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c  geCookie(pParse,
1f955 20 69 44 62 29 3b 0a 20 20 20 20 64 65 73 74 72   iDb);.    destr
1f956 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73  oyRootPage(pPars
1f957 65 2c 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 2c  e, pIndex->tnum,
1f958 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74   iDb);.    sqlit
1f959 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
1f95a 4f 50 5f 44 72 6f 70 49 6e 64 65 78 2c 20 69 44  OP_DropIndex, iD
1f95b 62 2c 20 30 2c 20 30 2c 20 70 49 6e 64 65 78 2d  b, 0, 0, pIndex-
1f95c 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a  >zName, 0);.  }.
1f95d 0a 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78  .exit_drop_index
1f95e 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69  :.  sqlite3SrcLi
1f95f 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4e 61  stDelete(db, pNa
1f960 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 41  me);.}../*.** pA
1f961 72 72 61 79 20 69 73 20 61 20 70 6f 69 6e 74 65  rray is a pointe
1f962 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66  r to an array of
1f963 20 6f 62 6a 65 63 74 73 2e 20 20 45 61 63 68 20   objects.  Each 
1f964 6f 62 6a 65 63 74 20 69 6e 20 74 68 65 0a 2a 2a  object in the.**
1f965 20 61 72 72 61 79 20 69 73 20 73 7a 45 6e 74 72   array is szEntr
1f966 79 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e  y bytes in size.
1f967 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61    This routine a
1f968 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a  llocates a new.*
1f969 2a 20 6f 62 6a 65 63 74 20 6f 6e 20 74 68 65 20  * object on the 
1f96a 65 6e 64 20 6f 66 20 74 68 65 20 61 72 72 61 79  end of the array
1f96b 2e 0a 2a 2a 0a 2a 2a 20 2a 70 6e 45 6e 74 72 79  ..**.** *pnEntry
1f96c 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
1f96d 66 20 65 6e 74 72 69 65 73 20 61 6c 72 65 61 64  f entries alread
1f96e 79 20 69 6e 20 75 73 65 2e 20 20 2a 70 6e 41 6c  y in use.  *pnAl
1f96f 6c 6f 63 20 69 73 0a 2a 2a 20 74 68 65 20 70 72  loc is.** the pr
1f970 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74  eviously allocat
1f971 65 64 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61  ed size of the a
1f972 72 72 61 79 2e 20 20 69 6e 69 74 53 69 7a 65 20  rray.  initSize 
1f973 69 73 20 74 68 65 0a 2a 2a 20 73 75 67 67 65 73  is the.** sugges
1f974 74 65 64 20 69 6e 69 74 69 61 6c 20 61 72 72 61  ted initial arra
1f975 79 20 73 69 7a 65 20 61 6c 6c 6f 63 61 74 69 6f  y size allocatio
1f976 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 64  n..**.** The ind
1f977 65 78 20 6f 66 20 74 68 65 20 6e 65 77 20 65 6e  ex of the new en
1f978 74 72 79 20 69 73 20 72 65 74 75 72 6e 65 64 20  try is returned 
1f979 69 6e 20 2a 70 49 64 78 2e 0a 2a 2a 0a 2a 2a 20  in *pIdx..**.** 
1f97a 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  This routine ret
1f97b 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  urns a pointer t
1f97c 6f 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 6f  o the array of o
1f97d 62 6a 65 63 74 73 2e 20 20 54 68 69 73 0a 2a 2a  bjects.  This.**
1f97e 20 6d 69 67 68 74 20 62 65 20 74 68 65 20 73 61   might be the sa
1f97f 6d 65 20 61 73 20 74 68 65 20 70 41 72 72 61 79  me as the pArray
1f980 20 70 61 72 61 6d 65 74 65 72 20 6f 72 20 69 74   parameter or it
1f981 20 6d 69 67 68 74 20 62 65 20 61 20 64 69 66 66   might be a diff
1f982 65 72 65 6e 74 0a 2a 2a 20 70 6f 69 6e 74 65 72  erent.** pointer
1f983 20 69 66 20 74 68 65 20 61 72 72 61 79 20 77 61   if the array wa
1f984 73 20 72 65 73 69 7a 65 64 2e 0a 2a 2f 0a 53 51  s resized..*/.SQ
1f985 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1f986 64 20 2a 73 71 6c 69 74 65 33 41 72 72 61 79 41  d *sqlite3ArrayA
1f987 6c 6c 6f 63 61 74 65 28 0a 20 20 73 71 6c 69 74  llocate(.  sqlit
1f988 65 33 20 2a 64 62 2c 20 20 20 20 20 20 2f 2a 20  e3 *db,      /* 
1f989 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f  Connection to no
1f98a 74 69 66 79 20 6f 66 20 6d 61 6c 6c 6f 63 20 66  tify of malloc f
1f98b 61 69 6c 75 72 65 73 20 2a 2f 0a 20 20 76 6f 69  ailures */.  voi
1f98c 64 20 2a 70 41 72 72 61 79 2c 20 20 20 20 20 2f  d *pArray,     /
1f98d 2a 20 41 72 72 61 79 20 6f 66 20 6f 62 6a 65 63  * Array of objec
1f98e 74 73 2e 20 20 4d 69 67 68 74 20 62 65 20 72 65  ts.  Might be re
1f98f 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 69  allocated */.  i
1f990 6e 74 20 73 7a 45 6e 74 72 79 2c 20 20 20 20 20  nt szEntry,     
1f991 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68   /* Size of each
1f992 20 6f 62 6a 65 63 74 20 69 6e 20 74 68 65 20 61   object in the a
1f993 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 69 6e  rray */.  int in
1f994 69 74 53 69 7a 65 2c 20 20 20 20 20 2f 2a 20 53  itSize,     /* S
1f995 75 67 67 65 73 74 65 64 20 69 6e 69 74 69 61 6c  uggested initial
1f996 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20   allocation, in 
1f997 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e  elements */.  in
1f998 74 20 2a 70 6e 45 6e 74 72 79 2c 20 20 20 20 20  t *pnEntry,     
1f999 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 62 6a  /* Number of obj
1f99a 65 63 74 73 20 63 75 72 72 65 6e 74 6c 79 20 69  ects currently i
1f99b 6e 20 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 2a  n use */.  int *
1f99c 70 6e 41 6c 6c 6f 63 2c 20 20 20 20 20 2f 2a 20  pnAlloc,     /* 
1f99d 43 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20  Current size of 
1f99e 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20  the allocation, 
1f99f 69 6e 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20  in elements */. 
1f9a0 20 69 6e 74 20 2a 70 49 64 78 20 20 20 20 20 20   int *pIdx      
1f9a1 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
1f9a2 69 6e 64 65 78 20 6f 66 20 61 20 6e 65 77 20 73  index of a new s
1f9a3 6c 6f 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  lot here */.){. 
1f9a4 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 66 28 20   char *z;.  if( 
1f9a5 2a 70 6e 45 6e 74 72 79 20 3e 3d 20 2a 70 6e 41  *pnEntry >= *pnA
1f9a6 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 76 6f 69 64  lloc ){.    void
1f9a7 20 2a 70 4e 65 77 3b 0a 20 20 20 20 69 6e 74 20   *pNew;.    int 
1f9a8 6e 65 77 53 69 7a 65 3b 0a 20 20 20 20 6e 65 77  newSize;.    new
1f9a9 53 69 7a 65 20 3d 20 28 2a 70 6e 41 6c 6c 6f 63  Size = (*pnAlloc
1f9aa 29 2a 32 20 2b 20 69 6e 69 74 53 69 7a 65 3b 0a  )*2 + initSize;.
1f9ab 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74      pNew = sqlit
1f9ac 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20  e3DbRealloc(db, 
1f9ad 70 41 72 72 61 79 2c 20 6e 65 77 53 69 7a 65 2a  pArray, newSize*
1f9ae 73 7a 45 6e 74 72 79 29 3b 0a 20 20 20 20 69 66  szEntry);.    if
1f9af 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20  ( pNew==0 ){.   
1f9b0 20 20 20 2a 70 49 64 78 20 3d 20 2d 31 3b 0a 20     *pIdx = -1;. 
1f9b1 20 20 20 20 20 72 65 74 75 72 6e 20 70 41 72 72       return pArr
1f9b2 61 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70  ay;.    }.    *p
1f9b3 6e 41 6c 6c 6f 63 20 3d 20 6e 65 77 53 69 7a 65  nAlloc = newSize
1f9b4 3b 0a 20 20 20 20 70 41 72 72 61 79 20 3d 20 70  ;.    pArray = p
1f9b5 4e 65 77 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 28  New;.  }.  z = (
1f9b6 63 68 61 72 2a 29 70 41 72 72 61 79 3b 0a 20 20  char*)pArray;.  
1f9b7 6d 65 6d 73 65 74 28 26 7a 5b 2a 70 6e 45 6e 74  memset(&z[*pnEnt
1f9b8 72 79 20 2a 20 73 7a 45 6e 74 72 79 5d 2c 20 30  ry * szEntry], 0
1f9b9 2c 20 73 7a 45 6e 74 72 79 29 3b 0a 20 20 2a 70  , szEntry);.  *p
1f9ba 49 64 78 20 3d 20 2a 70 6e 45 6e 74 72 79 3b 0a  Idx = *pnEntry;.
1f9bb 20 20 2b 2b 2a 70 6e 45 6e 74 72 79 3b 0a 20 20    ++*pnEntry;.  
1f9bc 72 65 74 75 72 6e 20 70 41 72 72 61 79 3b 0a 7d  return pArray;.}
1f9bd 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61  ../*.** Append a
1f9be 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20   new element to 
1f9bf 74 68 65 20 67 69 76 65 6e 20 49 64 4c 69 73 74  the given IdList
1f9c0 2e 20 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  .  Create a new 
1f9c1 49 64 4c 69 73 74 20 69 66 0a 2a 2a 20 6e 65 65  IdList if.** nee
1f9c2 64 20 62 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65  d be..**.** A ne
1f9c3 77 20 49 64 4c 69 73 74 20 69 73 20 72 65 74 75  w IdList is retu
1f9c4 72 6e 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66  rned, or NULL if
1f9c5 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e   malloc() fails.
1f9c6 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1f9c7 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74  TE IdList *sqlit
1f9c8 65 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28 73  e3IdListAppend(s
1f9c9 71 6c 69 74 65 33 20 2a 64 62 2c 20 49 64 4c 69  qlite3 *db, IdLi
1f9ca 73 74 20 2a 70 4c 69 73 74 2c 20 54 6f 6b 65 6e  st *pList, Token
1f9cb 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 69 6e 74   *pToken){.  int
1f9cc 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d   i;.  if( pList=
1f9cd 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69 73 74 20  =0 ){.    pList 
1f9ce 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
1f9cf 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
1f9d0 28 49 64 4c 69 73 74 29 20 29 3b 0a 20 20 20 20  (IdList) );.    
1f9d1 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72  if( pList==0 ) r
1f9d2 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 70 4c 69  eturn 0;.    pLi
1f9d3 73 74 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 30 3b 0a  st->nAlloc = 0;.
1f9d4 20 20 7d 0a 20 20 70 4c 69 73 74 2d 3e 61 20 3d    }.  pList->a =
1f9d5 20 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c 6c   sqlite3ArrayAll
1f9d6 6f 63 61 74 65 28 0a 20 20 20 20 20 20 64 62 2c  ocate(.      db,
1f9d7 0a 20 20 20 20 20 20 70 4c 69 73 74 2d 3e 61 2c  .      pList->a,
1f9d8 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 4c  .      sizeof(pL
1f9d9 69 73 74 2d 3e 61 5b 30 5d 29 2c 0a 20 20 20 20  ist->a[0]),.    
1f9da 20 20 35 2c 0a 20 20 20 20 20 20 26 70 4c 69 73    5,.      &pLis
1f9db 74 2d 3e 6e 49 64 2c 0a 20 20 20 20 20 20 26 70  t->nId,.      &p
1f9dc 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2c 0a 20 20  List->nAlloc,.  
1f9dd 20 20 20 20 26 69 0a 20 20 29 3b 0a 20 20 69 66      &i.  );.  if
1f9de 28 20 69 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c  ( i<0 ){.    sql
1f9df 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65  ite3IdListDelete
1f9e0 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20  (db, pList);.   
1f9e1 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
1f9e2 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61   pList->a[i].zNa
1f9e3 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65  me = sqlite3Name
1f9e4 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54  FromToken(db, pT
1f9e5 6f 6b 65 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20  oken);.  return 
1f9e6 70 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  pList;.}../*.** 
1f9e7 44 65 6c 65 74 65 20 61 6e 20 49 64 4c 69 73 74  Delete an IdList
1f9e8 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1f9e9 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1f9ea 49 64 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c  IdListDelete(sql
1f9eb 69 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 73 74  ite3 *db, IdList
1f9ec 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20   *pList){.  int 
1f9ed 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d  i;.  if( pList==
1f9ee 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f  0 ) return;.  fo
1f9ef 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e  r(i=0; i<pList->
1f9f0 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  nId; i++){.    s
1f9f1 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1f9f2 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61   pList->a[i].zNa
1f9f3 6d 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  me);.  }.  sqlit
1f9f4 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69  e3DbFree(db, pLi
1f9f5 73 74 2d 3e 61 29 3b 0a 20 20 73 71 6c 69 74 65  st->a);.  sqlite
1f9f6 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73  3DbFree(db, pLis
1f9f7 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  t);.}../*.** Ret
1f9f8 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 69 6e  urn the index in
1f9f9 20 70 4c 69 73 74 20 6f 66 20 74 68 65 20 69 64   pList of the id
1f9fa 65 6e 74 69 66 69 65 72 20 6e 61 6d 65 64 20 7a  entifier named z
1f9fb 49 64 2e 20 20 52 65 74 75 72 6e 20 2d 31 0a 2a  Id.  Return -1.*
1f9fc 2a 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a  * if not found..
1f9fd 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1f9fe 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 64 4c  E int sqlite3IdL
1f9ff 69 73 74 49 6e 64 65 78 28 49 64 4c 69 73 74 20  istIndex(IdList 
1fa00 2a 70 4c 69 73 74 2c 20 63 6f 6e 73 74 20 63 68  *pList, const ch
1fa01 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 6e  ar *zName){.  in
1fa02 74 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74  t i;.  if( pList
1fa03 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 2d 31 3b  ==0 ) return -1;
1fa04 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c  .  for(i=0; i<pL
1fa05 69 73 74 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a  ist->nId; i++){.
1fa06 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
1fa07 74 72 49 43 6d 70 28 70 4c 69 73 74 2d 3e 61 5b  trICmp(pList->a[
1fa08 69 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29  i].zName, zName)
1fa09 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 69 3b 0a  ==0 ) return i;.
1fa0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31 3b    }.  return -1;
1fa0b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64  .}../*.** Append
1fa0c 20 61 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d   a new table nam
1fa0d 65 20 74 6f 20 74 68 65 20 67 69 76 65 6e 20 53  e to the given S
1fa0e 72 63 4c 69 73 74 2e 20 20 43 72 65 61 74 65 20  rcList.  Create 
1fa0f 61 20 6e 65 77 20 53 72 63 4c 69 73 74 20 69 66  a new SrcList if
1fa10 0a 2a 2a 20 6e 65 65 64 20 62 65 2e 20 20 41 20  .** need be.  A 
1fa11 6e 65 77 20 65 6e 74 72 79 20 69 73 20 63 72 65  new entry is cre
1fa12 61 74 65 64 20 69 6e 20 74 68 65 20 53 72 63 4c  ated in the SrcL
1fa13 69 73 74 20 65 76 65 6e 20 69 66 20 70 54 6f 6b  ist even if pTok
1fa14 65 6e 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  en is NULL..**.*
1fa15 2a 20 41 20 6e 65 77 20 53 72 63 4c 69 73 74 20  * A new SrcList 
1fa16 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 72 20  is returned, or 
1fa17 4e 55 4c 4c 20 69 66 20 6d 61 6c 6c 6f 63 28 29  NULL if malloc()
1fa18 20 66 61 69 6c 73 2e 0a 2a 2a 0a 2a 2a 20 49 66   fails..**.** If
1fa19 20 70 44 61 74 61 62 61 73 65 20 69 73 20 6e 6f   pDatabase is no
1fa1a 74 20 6e 75 6c 6c 2c 20 69 74 20 6d 65 61 6e 73  t null, it means
1fa1b 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 20   that the table 
1fa1c 68 61 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 0a  has an optional.
1fa1d 2a 2a 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  ** database name
1fa1e 20 70 72 65 66 69 78 2e 20 20 4c 69 6b 65 20 74   prefix.  Like t
1fa1f 68 69 73 3a 20 20 22 64 61 74 61 62 61 73 65 2e  his:  "database.
1fa20 74 61 62 6c 65 22 2e 20 20 54 68 65 20 70 44 61  table".  The pDa
1fa21 74 61 62 61 73 65 0a 2a 2a 20 70 6f 69 6e 74 73  tabase.** points
1fa22 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6e 61   to the table na
1fa23 6d 65 20 61 6e 64 20 74 68 65 20 70 54 61 62 6c  me and the pTabl
1fa24 65 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  e points to the 
1fa25 64 61 74 61 62 61 73 65 20 6e 61 6d 65 2e 0a 2a  database name..*
1fa26 2a 20 54 68 65 20 53 72 63 4c 69 73 74 2e 61 5b  * The SrcList.a[
1fa27 5d 2e 7a 4e 61 6d 65 20 66 69 65 6c 64 20 69 73  ].zName field is
1fa28 20 66 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65   filled with the
1fa29 20 74 61 62 6c 65 20 6e 61 6d 65 20 77 68 69 63   table name whic
1fa2a 68 20 6d 69 67 68 74 0a 2a 2a 20 63 6f 6d 65 20  h might.** come 
1fa2b 66 72 6f 6d 20 70 54 61 62 6c 65 20 28 69 66 20  from pTable (if 
1fa2c 70 44 61 74 61 62 61 73 65 20 69 73 20 4e 55 4c  pDatabase is NUL
1fa2d 4c 29 20 6f 72 20 66 72 6f 6d 20 70 44 61 74 61  L) or from pData
1fa2e 62 61 73 65 2e 20 20 0a 2a 2a 20 53 72 63 4c 69  base.  .** SrcLi
1fa2f 73 74 2e 61 5b 5d 2e 7a 44 61 74 61 62 61 73 65  st.a[].zDatabase
1fa30 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20   is filled with 
1fa31 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d  the database nam
1fa32 65 20 66 72 6f 6d 20 70 54 61 62 6c 65 2c 0a 2a  e from pTable,.*
1fa33 2a 20 6f 72 20 77 69 74 68 20 4e 55 4c 4c 20 69  * or with NULL i
1fa34 66 20 6e 6f 20 64 61 74 61 62 61 73 65 20 69 73  f no database is
1fa35 20 73 70 65 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a   specified..**.*
1fa36 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  * In other words
1fa37 2c 20 69 66 20 63 61 6c 6c 20 6c 69 6b 65 20 74  , if call like t
1fa38 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  his:.**.**      
1fa39 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73     sqlite3SrcLis
1fa3a 74 41 70 70 65 6e 64 28 44 2c 41 2c 42 2c 30 29  tAppend(D,A,B,0)
1fa3b 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 42 20 69  ;.**.** Then B i
1fa3c 73 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 61  s a table name a
1fa3d 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20  nd the database 
1fa3e 6e 61 6d 65 20 69 73 20 75 6e 73 70 65 63 69 66  name is unspecif
1fa3f 69 65 64 2e 20 20 49 66 20 63 61 6c 6c 65 64 0a  ied.  If called.
1fa40 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  ** like this:.**
1fa41 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 71 6c 69  .**         sqli
1fa42 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
1fa43 28 44 2c 41 2c 42 2c 43 29 3b 0a 2a 2a 0a 2a 2a  (D,A,B,C);.**.**
1fa44 20 54 68 65 6e 20 43 20 69 73 20 74 68 65 20 74   Then C is the t
1fa45 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64 20 42 20  able name and B 
1fa46 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  is the database 
1fa47 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  name..*/.SQLITE_
1fa48 50 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20  PRIVATE SrcList 
1fa49 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41  *sqlite3SrcListA
1fa4a 70 70 65 6e 64 28 0a 20 20 73 71 6c 69 74 65 33  ppend(.  sqlite3
1fa4b 20 2a 64 62 2c 20 20 20 20 20 20 20 20 2f 2a 20   *db,        /* 
1fa4c 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f  Connection to no
1fa4d 74 69 66 79 20 6f 66 20 6d 61 6c 6c 6f 63 20 66  tify of malloc f
1fa4e 61 69 6c 75 72 65 73 20 2a 2f 0a 20 20 53 72 63  ailures */.  Src
1fa4f 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20  List *pList,    
1fa50 20 2f 2a 20 41 70 70 65 6e 64 20 74 6f 20 74 68   /* Append to th
1fa51 69 73 20 53 72 63 4c 69 73 74 2e 20 4e 55 4c 4c  is SrcList. NULL
1fa52 20 63 72 65 61 74 65 73 20 61 20 6e 65 77 20 53   creates a new S
1fa53 72 63 4c 69 73 74 20 2a 2f 0a 20 20 54 6f 6b 65  rcList */.  Toke
1fa54 6e 20 2a 70 54 61 62 6c 65 2c 20 20 20 20 20 20  n *pTable,      
1fa55 2f 2a 20 54 61 62 6c 65 20 74 6f 20 61 70 70 65  /* Table to appe
1fa56 6e 64 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  nd */.  Token *p
1fa57 44 61 74 61 62 61 73 65 20 20 20 20 2f 2a 20 44  Database    /* D
1fa58 61 74 61 62 61 73 65 20 6f 66 20 74 68 65 20 74  atabase of the t
1fa59 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73 74 72  able */.){.  str
1fa5a 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
1fa5b 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 20 70   *pItem;.  if( p
1fa5c 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70  List==0 ){.    p
1fa5d 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62  List = sqlite3Db
1fa5e 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
1fa5f 69 7a 65 6f 66 28 53 72 63 4c 69 73 74 29 20 29  izeof(SrcList) )
1fa60 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d  ;.    if( pList=
1fa61 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
1fa62 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63     pList->nAlloc
1fa63 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 1;.  }.  if( 
1fa64 70 4c 69 73 74 2d 3e 6e 53 72 63 3e 3d 70 4c 69  pList->nSrc>=pLi
1fa65 73 74 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20  st->nAlloc ){.  
1fa66 20 20 53 72 63 4c 69 73 74 20 2a 70 4e 65 77 3b    SrcList *pNew;
1fa67 0a 20 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c  .    pList->nAll
1fa68 6f 63 20 2a 3d 20 32 3b 0a 20 20 20 20 70 4e 65  oc *= 2;.    pNe
1fa69 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61  w = sqlite3DbRea
1fa6a 6c 6c 6f 63 28 64 62 2c 20 70 4c 69 73 74 2c 0a  lloc(db, pList,.
1fa6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
1fa6c 69 7a 65 6f 66 28 2a 70 4c 69 73 74 29 20 2b 20  izeof(*pList) + 
1fa6d 28 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2d 31  (pList->nAlloc-1
1fa6e 29 2a 73 69 7a 65 6f 66 28 70 4c 69 73 74 2d 3e  )*sizeof(pList->
1fa6f 61 5b 30 5d 29 20 29 3b 0a 20 20 20 20 69 66 28  a[0]) );.    if(
1fa70 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20   pNew==0 ){.    
1fa71 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
1fa72 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74  Delete(db, pList
1fa73 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
1fa74 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 69  0;.    }.    pLi
1fa75 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20  st = pNew;.  }. 
1fa76 20 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d   pItem = &pList-
1fa77 3e 61 5b 70 4c 69 73 74 2d 3e 6e 53 72 63 5d 3b  >a[pList->nSrc];
1fa78 0a 20 20 6d 65 6d 73 65 74 28 70 49 74 65 6d 2c  .  memset(pItem,
1fa79 20 30 2c 20 73 69 7a 65 6f 66 28 70 4c 69 73 74   0, sizeof(pList
1fa7a 2d 3e 61 5b 30 5d 29 29 3b 0a 20 20 69 66 28 20  ->a[0]));.  if( 
1fa7b 70 44 61 74 61 62 61 73 65 20 26 26 20 70 44 61  pDatabase && pDa
1fa7c 74 61 62 61 73 65 2d 3e 7a 3d 3d 30 20 29 7b 0a  tabase->z==0 ){.
1fa7d 20 20 20 20 70 44 61 74 61 62 61 73 65 20 3d 20      pDatabase = 
1fa7e 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 61  0;.  }.  if( pDa
1fa7f 74 61 62 61 73 65 20 26 26 20 70 54 61 62 6c 65  tabase && pTable
1fa80 20 29 7b 0a 20 20 20 20 54 6f 6b 65 6e 20 2a 70   ){.    Token *p
1fa81 54 65 6d 70 20 3d 20 70 44 61 74 61 62 61 73 65  Temp = pDatabase
1fa82 3b 0a 20 20 20 20 70 44 61 74 61 62 61 73 65 20  ;.    pDatabase 
1fa83 3d 20 70 54 61 62 6c 65 3b 0a 20 20 20 20 70 54  = pTable;.    pT
1fa84 61 62 6c 65 20 3d 20 70 54 65 6d 70 3b 0a 20 20  able = pTemp;.  
1fa85 7d 0a 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65  }.  pItem->zName
1fa86 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
1fa87 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 61 62  omToken(db, pTab
1fa88 6c 65 29 3b 0a 20 20 70 49 74 65 6d 2d 3e 7a 44  le);.  pItem->zD
1fa89 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65  atabase = sqlite
1fa8a 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
1fa8b 62 2c 20 70 44 61 74 61 62 61 73 65 29 3b 0a 20  b, pDatabase);. 
1fa8c 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20   pItem->iCursor 
1fa8d 3d 20 2d 31 3b 0a 20 20 70 4c 69 73 74 2d 3e 6e  = -1;.  pList->n
1fa8e 53 72 63 2b 2b 3b 0a 20 20 72 65 74 75 72 6e 20  Src++;.  return 
1fa8f 70 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  pList;.}../*.** 
1fa90 41 73 73 69 67 6e 20 56 64 62 65 43 75 72 73 6f  Assign VdbeCurso
1fa91 72 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 73 20  r index numbers 
1fa92 74 6f 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e  to all tables in
1fa93 20 61 20 53 72 63 4c 69 73 74 0a 2a 2f 0a 53 51   a SrcList.*/.SQ
1fa94 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1fa95 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  d sqlite3SrcList
1fa96 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28 50 61  AssignCursors(Pa
1fa97 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63  rse *pParse, Src
1fa98 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20  List *pList){.  
1fa99 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
1fa9a 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  SrcList_item *pI
1fa9b 74 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 70 4c  tem;.  assert(pL
1fa9c 69 73 74 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64  ist || pParse->d
1fa9d 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1fa9e 29 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 20 29  );.  if( pList )
1fa9f 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70  {.    for(i=0, p
1faa0 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69  Item=pList->a; i
1faa1 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b  <pList->nSrc; i+
1faa2 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  +, pItem++){.   
1faa3 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 43     if( pItem->iC
1faa4 75 72 73 6f 72 3e 3d 30 20 29 20 62 72 65 61 6b  ursor>=0 ) break
1faa5 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69  ;.      pItem->i
1faa6 43 75 72 73 6f 72 20 3d 20 70 50 61 72 73 65 2d  Cursor = pParse-
1faa7 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 69  >nTab++;.      i
1faa8 66 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63  f( pItem->pSelec
1faa9 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  t ){.        sql
1faaa 69 74 65 33 53 72 63 4c 69 73 74 41 73 73 69 67  ite3SrcListAssig
1faab 6e 43 75 72 73 6f 72 73 28 70 50 61 72 73 65 2c  nCursors(pParse,
1faac 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2d   pItem->pSelect-
1faad 3e 70 53 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a  >pSrc);.      }.
1faae 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
1faaf 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e 74  ** Delete an ent
1fab0 69 72 65 20 53 72 63 4c 69 73 74 20 69 6e 63 6c  ire SrcList incl
1fab1 75 64 69 6e 67 20 61 6c 6c 20 69 74 73 20 73 75  uding all its su
1fab2 62 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53  bstructure..*/.S
1fab3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1fab4 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  id sqlite3SrcLis
1fab5 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20  tDelete(sqlite3 
1fab6 2a 64 62 2c 20 53 72 63 4c 69 73 74 20 2a 70 4c  *db, SrcList *pL
1fab7 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  ist){.  int i;. 
1fab8 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
1fab9 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69  item *pItem;.  i
1faba 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65  f( pList==0 ) re
1fabb 74 75 72 6e 3b 0a 20 20 66 6f 72 28 70 49 74 65  turn;.  for(pIte
1fabc 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b  m=pList->a, i=0;
1fabd 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20   i<pList->nSrc; 
1fabe 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i++, pItem++){. 
1fabf 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1fac0 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74  (db, pItem->zDat
1fac1 61 62 61 73 65 29 3b 0a 20 20 20 20 73 71 6c 69  abase);.    sqli
1fac2 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49  te3DbFree(db, pI
1fac3 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  tem->zName);.   
1fac4 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1fac5 62 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73  b, pItem->zAlias
1fac6 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
1fac7 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e  Free(db, pItem->
1fac8 7a 49 6e 64 65 78 29 3b 0a 20 20 20 20 73 71 6c  zIndex);.    sql
1fac9 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28  ite3DeleteTable(
1faca 70 49 74 65 6d 2d 3e 70 54 61 62 29 3b 0a 20 20  pItem->pTab);.  
1facb 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
1facc 65 6c 65 74 65 28 64 62 2c 20 70 49 74 65 6d 2d  elete(db, pItem-
1facd 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 73  >pSelect);.    s
1face 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
1facf 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 4f 6e 29  (db, pItem->pOn)
1fad0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c  ;.    sqlite3IdL
1fad1 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 49  istDelete(db, pI
1fad2 74 65 6d 2d 3e 70 55 73 69 6e 67 29 3b 0a 20 20  tem->pUsing);.  
1fad3 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  }.  sqlite3DbFre
1fad4 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a  e(db, pList);.}.
1fad5 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
1fad6 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79  ine is called by
1fad7 20 74 68 65 20 70 61 72 73 65 72 20 74 6f 20 61   the parser to a
1fad8 64 64 20 61 20 6e 65 77 20 74 65 72 6d 20 74 6f  dd a new term to
1fad9 20 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 61   the.** end of a
1fada 20 67 72 6f 77 69 6e 67 20 46 52 4f 4d 20 63 6c   growing FROM cl
1fadb 61 75 73 65 2e 20 20 54 68 65 20 22 70 22 20 70  ause.  The "p" p
1fadc 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20  arameter is the 
1fadd 70 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20 46  part of.** the F
1fade 52 4f 4d 20 63 6c 61 75 73 65 20 74 68 61 74 20  ROM clause that 
1fadf 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
1fae0 20 63 6f 6e 73 74 72 75 63 74 65 64 2e 20 20 22   constructed.  "
1fae1 70 22 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 69 66  p" is NULL.** if
1fae2 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72   this is the fir
1fae3 73 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20 46  st term of the F
1fae4 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 70 54 61  ROM clause.  pTa
1fae5 62 6c 65 20 61 6e 64 20 70 44 61 74 61 62 61 73  ble and pDatabas
1fae6 65 0a 2a 2a 20 61 72 65 20 74 68 65 20 6e 61 6d  e.** are the nam
1fae7 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 61  e of the table a
1fae8 6e 64 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  nd database name
1fae9 64 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  d in the FROM cl
1faea 61 75 73 65 20 74 65 72 6d 2e 0a 2a 2a 20 70 44  ause term..** pD
1faeb 61 74 61 62 61 73 65 20 69 73 20 4e 55 4c 4c 20  atabase is NULL 
1faec 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  if the database 
1faed 6e 61 6d 65 20 71 75 61 6c 69 66 69 65 72 20 69  name qualifier i
1faee 73 20 6d 69 73 73 69 6e 67 20 2d 20 74 68 65 0a  s missing - the.
1faef 2a 2a 20 75 73 75 61 6c 20 63 61 73 65 2e 20 20  ** usual case.  
1faf0 49 66 20 74 68 65 20 74 65 72 6d 20 68 61 73 20  If the term has 
1faf1 61 20 61 6c 69 61 73 2c 20 74 68 65 6e 20 70 41  a alias, then pA
1faf2 6c 69 61 73 20 70 6f 69 6e 74 73 20 74 6f 20 74  lias points to t
1faf3 68 65 0a 2a 2a 20 61 6c 69 61 73 20 74 6f 6b 65  he.** alias toke
1faf4 6e 2e 20 20 49 66 20 74 68 65 20 74 65 72 6d 20  n.  If the term 
1faf5 69 73 20 61 20 73 75 62 71 75 65 72 79 2c 20 74  is a subquery, t
1faf6 68 65 6e 20 70 53 75 62 71 75 65 72 79 20 69 73  hen pSubquery is
1faf7 20 74 68 65 0a 2a 2a 20 53 45 4c 45 43 54 20 73   the.** SELECT s
1faf8 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 74 68  tatement that th
1faf9 65 20 73 75 62 71 75 65 72 79 20 65 6e 63 6f 64  e subquery encod
1fafa 65 73 2e 20 20 54 68 65 20 70 54 61 62 6c 65 20  es.  The pTable 
1fafb 61 6e 64 0a 2a 2a 20 70 44 61 74 61 62 61 73 65  and.** pDatabase
1fafc 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20   parameters are 
1fafd 4e 55 4c 4c 20 66 6f 72 20 73 75 62 71 75 65 72  NULL for subquer
1fafe 69 65 73 2e 20 20 54 68 65 20 70 4f 6e 20 61 6e  ies.  The pOn an
1faff 64 20 70 55 73 69 6e 67 0a 2a 2a 20 70 61 72 61  d pUsing.** para
1fb00 6d 65 74 65 72 73 20 61 72 65 20 74 68 65 20 63  meters are the c
1fb01 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 4f 4e  ontent of the ON
1fb02 20 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73   and USING claus
1fb03 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  es..**.** Return
1fb04 20 61 20 6e 65 77 20 53 72 63 4c 69 73 74 20 77   a new SrcList w
1fb05 68 69 63 68 20 65 6e 63 6f 64 65 73 20 69 73 20  hich encodes is 
1fb06 74 68 65 20 46 52 4f 4d 20 77 69 74 68 20 74 68  the FROM with th
1fb07 65 20 6e 65 77 0a 2a 2a 20 74 65 72 6d 20 61 64  e new.** term ad
1fb08 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ded..*/.SQLITE_P
1fb09 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a  RIVATE SrcList *
1fb0a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70  sqlite3SrcListAp
1fb0b 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28 0a 20 20  pendFromTerm(.  
1fb0c 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
1fb0d 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69          /* Parsi
1fb0e 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
1fb0f 53 72 63 4c 69 73 74 20 2a 70 2c 20 20 20 20 20  SrcList *p,     
1fb10 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c          /* The l
1fb11 65 66 74 20 70 61 72 74 20 6f 66 20 74 68 65 20  eft part of the 
1fb12 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 6c 72 65  FROM clause alre
1fb13 61 64 79 20 73 65 65 6e 20 2a 2f 0a 20 20 54 6f  ady seen */.  To
1fb14 6b 65 6e 20 2a 70 54 61 62 6c 65 2c 20 20 20 20  ken *pTable,    
1fb15 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
1fb16 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 61 64   the table to ad
1fb17 64 20 74 6f 20 74 68 65 20 46 52 4f 4d 20 63 6c  d to the FROM cl
1fb18 61 75 73 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  ause */.  Token 
1fb19 2a 70 44 61 74 61 62 61 73 65 2c 20 20 20 20 20  *pDatabase,     
1fb1a 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
1fb1b 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
1fb1c 6e 69 6e 67 20 70 54 61 62 6c 65 20 2a 2f 0a 20  ning pTable */. 
1fb1d 20 54 6f 6b 65 6e 20 2a 70 41 6c 69 61 73 2c 20   Token *pAlias, 
1fb1e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1fb1f 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20  right-hand side 
1fb20 6f 66 20 74 68 65 20 41 53 20 73 75 62 65 78 70  of the AS subexp
1fb21 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 53 65 6c  ression */.  Sel
1fb22 65 63 74 20 2a 70 53 75 62 71 75 65 72 79 2c 20  ect *pSubquery, 
1fb23 20 20 20 20 20 2f 2a 20 41 20 73 75 62 71 75 65       /* A subque
1fb24 72 79 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65  ry used in place
1fb25 20 6f 66 20 61 20 74 61 62 6c 65 20 6e 61 6d 65   of a table name
1fb26 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f 6e 2c   */.  Expr *pOn,
1fb27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1fb28 20 54 68 65 20 4f 4e 20 63 6c 61 75 73 65 20 6f   The ON clause o
1fb29 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 49 64  f a join */.  Id
1fb2a 4c 69 73 74 20 2a 70 55 73 69 6e 67 20 20 20 20  List *pUsing    
1fb2b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 55 53 49        /* The USI
1fb2c 4e 47 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a  NG clause of a j
1fb2d 6f 69 6e 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75  oin */.){.  stru
1fb2e 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
1fb2f 2a 70 49 74 65 6d 3b 0a 20 20 73 71 6c 69 74 65  *pItem;.  sqlite
1fb30 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
1fb31 64 62 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65  db;.  p = sqlite
1fb32 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 64  3SrcListAppend(d
1fb33 62 2c 20 70 2c 20 70 54 61 62 6c 65 2c 20 70 44  b, p, pTable, pD
1fb34 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20  atabase);.  if( 
1fb35 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 53 72 63 3d  p==0 || p->nSrc=
1fb36 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
1fb37 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
1fb38 70 4f 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  pOn);.    sqlite
1fb39 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62  3IdListDelete(db
1fb3a 2c 20 70 55 73 69 6e 67 29 3b 0a 20 20 20 20 73  , pUsing);.    s
1fb3b 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
1fb3c 74 65 28 64 62 2c 20 70 53 75 62 71 75 65 72 79  te(db, pSubquery
1fb3d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 3b  );.    return p;
1fb3e 0a 20 20 7d 0a 20 20 70 49 74 65 6d 20 3d 20 26  .  }.  pItem = &
1fb3f 70 2d 3e 61 5b 70 2d 3e 6e 53 72 63 2d 31 5d 3b  p->a[p->nSrc-1];
1fb40 0a 20 20 69 66 28 20 70 41 6c 69 61 73 20 26 26  .  if( pAlias &&
1fb41 20 70 41 6c 69 61 73 2d 3e 6e 20 29 7b 0a 20 20   pAlias->n ){.  
1fb42 20 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20    pItem->zAlias 
1fb43 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
1fb44 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 41 6c 69 61  mToken(db, pAlia
1fb45 73 29 3b 0a 20 20 7d 0a 20 20 70 49 74 65 6d 2d  s);.  }.  pItem-
1fb46 3e 70 53 65 6c 65 63 74 20 3d 20 70 53 75 62 71  >pSelect = pSubq
1fb47 75 65 72 79 3b 0a 20 20 70 49 74 65 6d 2d 3e 70  uery;.  pItem->p
1fb48 4f 6e 20 3d 20 70 4f 6e 3b 0a 20 20 70 49 74 65  On = pOn;.  pIte
1fb49 6d 2d 3e 70 55 73 69 6e 67 20 3d 20 70 55 73 69  m->pUsing = pUsi
1fb4a 6e 67 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a  ng;.  return p;.
1fb4b 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20  }../*.** Add an 
1fb4c 49 4e 44 45 58 45 44 20 42 59 20 6f 72 20 4e 4f  INDEXED BY or NO
1fb4d 54 20 49 4e 44 45 58 45 44 20 63 6c 61 75 73 65  T INDEXED clause
1fb4e 20 74 6f 20 74 68 65 20 6d 6f 73 74 20 72 65 63   to the most rec
1fb4f 65 6e 74 6c 79 20 61 64 64 65 64 20 0a 2a 2a 20  ently added .** 
1fb50 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 73  element of the s
1fb51 6f 75 72 63 65 2d 6c 69 73 74 20 70 61 73 73 65  ource-list passe
1fb52 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
1fb53 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c  argument..*/.SQL
1fb54 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1fb55 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 49   sqlite3SrcListI
1fb56 6e 64 65 78 65 64 42 79 28 50 61 72 73 65 20 2a  ndexedBy(Parse *
1fb57 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20  pParse, SrcList 
1fb58 2a 70 2c 20 54 6f 6b 65 6e 20 2a 70 49 6e 64 65  *p, Token *pInde
1fb59 78 65 64 42 79 29 7b 0a 20 20 69 66 28 20 70 49  xedBy){.  if( pI
1fb5a 6e 64 65 78 65 64 42 79 20 26 26 20 70 20 26 26  ndexedBy && p &&
1fb5b 20 70 2d 3e 6e 53 72 63 3e 30 20 29 7b 0a 20 20   p->nSrc>0 ){.  
1fb5c 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
1fb5d 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26  _item *pItem = &
1fb5e 70 2d 3e 61 5b 70 2d 3e 6e 53 72 63 2d 31 5d 3b  p->a[p->nSrc-1];
1fb5f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 74  .    assert( pIt
1fb60 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 3d 3d  em->notIndexed==
1fb61 30 20 26 26 20 70 49 74 65 6d 2d 3e 7a 49 6e 64  0 && pItem->zInd
1fb62 65 78 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  ex==0 );.    if(
1fb63 20 70 49 6e 64 65 78 65 64 42 79 2d 3e 6e 3d 3d   pIndexedBy->n==
1fb64 31 20 26 26 20 21 70 49 6e 64 65 78 65 64 42 79  1 && !pIndexedBy
1fb65 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ->z ){.      /* 
1fb66 41 20 22 4e 4f 54 20 49 4e 44 45 58 45 44 22 20  A "NOT INDEXED" 
1fb67 63 6c 61 75 73 65 20 77 61 73 20 73 75 70 70 6c  clause was suppl
1fb68 69 65 64 2e 20 53 65 65 20 70 61 72 73 65 2e 79  ied. See parse.y
1fb69 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73 74   .      ** const
1fb6a 72 75 63 74 20 22 69 6e 64 65 78 65 64 5f 6f 70  ruct "indexed_op
1fb6b 74 22 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 20  t" for details. 
1fb6c 2a 2f 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e  */.      pItem->
1fb6d 6e 6f 74 49 6e 64 65 78 65 64 20 3d 20 31 3b 0a  notIndexed = 1;.
1fb6e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1fb6f 20 70 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 20 3d   pItem->zIndex =
1fb70 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
1fb71 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d 3e 64 62  Token(pParse->db
1fb72 2c 20 70 49 6e 64 65 78 65 64 42 79 29 3b 0a 20  , pIndexedBy);. 
1fb73 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
1fb74 2a 20 57 68 65 6e 20 62 75 69 6c 64 69 6e 67 20  * When building 
1fb75 75 70 20 61 20 46 52 4f 4d 20 63 6c 61 75 73 65  up a FROM clause
1fb76 20 69 6e 20 74 68 65 20 70 61 72 73 65 72 2c 20   in the parser, 
1fb77 74 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f  the join operato
1fb78 72 0a 2a 2a 20 69 73 20 69 6e 69 74 69 61 6c 6c  r.** is initiall
1fb79 79 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68  y attached to th
1fb7a 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 2e 20  e left operand. 
1fb7b 20 42 75 74 20 74 68 65 20 63 6f 64 65 20 67 65   But the code ge
1fb7c 6e 65 72 61 74 6f 72 0a 2a 2a 20 65 78 70 65 63  nerator.** expec
1fb7d 74 73 20 74 68 65 20 6a 6f 69 6e 20 6f 70 65 72  ts the join oper
1fb7e 61 74 6f 72 20 74 6f 20 62 65 20 6f 6e 20 74 68  ator to be on th
1fb7f 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 2e  e right operand.
1fb80 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a    This routine.*
1fb81 2a 20 53 68 69 66 74 73 20 61 6c 6c 20 6a 6f 69  * Shifts all joi
1fb82 6e 20 6f 70 65 72 61 74 6f 72 73 20 66 72 6f 6d  n operators from
1fb83 20 6c 65 66 74 20 74 6f 20 72 69 67 68 74 20 66   left to right f
1fb84 6f 72 20 61 6e 20 65 6e 74 69 72 65 20 46 52 4f  or an entire FRO
1fb85 4d 0a 2a 2a 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a  M.** clause..**.
1fb86 2a 2a 20 45 78 61 6d 70 6c 65 3a 20 53 75 70 70  ** Example: Supp
1fb87 6f 73 65 20 74 68 65 20 6a 6f 69 6e 20 69 73 20  ose the join is 
1fb88 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a  like this:.**.**
1fb89 20 20 20 20 20 20 20 20 20 20 20 41 20 6e 61 74             A nat
1fb8a 75 72 61 6c 20 63 72 6f 73 73 20 6a 6f 69 6e 20  ural cross join 
1fb8b 42 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 65 72  B.**.** The oper
1fb8c 61 74 6f 72 20 69 73 20 22 6e 61 74 75 72 61 6c  ator is "natural
1fb8d 20 63 72 6f 73 73 20 6a 6f 69 6e 22 2e 20 20 54   cross join".  T
1fb8e 68 65 20 41 20 61 6e 64 20 42 20 6f 70 65 72 61  he A and B opera
1fb8f 6e 64 73 20 61 72 65 20 73 74 6f 72 65 64 0a 2a  nds are stored.*
1fb90 2a 20 69 6e 20 70 2d 3e 61 5b 30 5d 20 61 6e 64  * in p->a[0] and
1fb91 20 70 2d 3e 61 5b 31 5d 2c 20 72 65 73 70 65 63   p->a[1], respec
1fb92 74 69 76 65 6c 79 2e 20 20 54 68 65 20 70 61 72  tively.  The par
1fb93 73 65 72 20 69 6e 69 74 69 61 6c 6c 79 20 73 74  ser initially st
1fb94 6f 72 65 73 20 74 68 65 0a 2a 2a 20 6f 70 65 72  ores the.** oper
1fb95 61 74 6f 72 20 77 69 74 68 20 41 2e 20 20 54 68  ator with A.  Th
1fb96 69 73 20 72 6f 75 74 69 6e 65 20 73 68 69 66 74  is routine shift
1fb97 73 20 74 68 61 74 20 6f 70 65 72 61 74 6f 72 20  s that operator 
1fb98 6f 76 65 72 20 74 6f 20 42 2e 0a 2a 2f 0a 53 51  over to B..*/.SQ
1fb99 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1fb9a 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  d sqlite3SrcList
1fb9b 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28 53 72  ShiftJoinType(Sr
1fb9c 63 4c 69 73 74 20 2a 70 29 7b 0a 20 20 69 66 28  cList *p){.  if(
1fb9d 20 70 20 26 26 20 70 2d 3e 61 20 29 7b 0a 20 20   p && p->a ){.  
1fb9e 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72    int i;.    for
1fb9f 28 69 3d 70 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e  (i=p->nSrc-1; i>
1fba0 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 20 20 70  0; i--){.      p
1fba1 2d 3e 61 5b 69 5d 2e 6a 6f 69 6e 74 79 70 65 20  ->a[i].jointype 
1fba2 3d 20 70 2d 3e 61 5b 69 2d 31 5d 2e 6a 6f 69 6e  = p->a[i-1].join
1fba3 74 79 70 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20  type;.    }.    
1fba4 70 2d 3e 61 5b 30 5d 2e 6a 6f 69 6e 74 79 70 65  p->a[0].jointype
1fba5 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   = 0;.  }.}../*.
1fba6 2a 2a 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73  ** Begin a trans
1fba7 61 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45  action.*/.SQLITE
1fba8 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1fba9 6c 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73 61  lite3BeginTransa
1fbaa 63 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61  ction(Parse *pPa
1fbab 72 73 65 2c 20 69 6e 74 20 74 79 70 65 29 7b 0a  rse, int type){.
1fbac 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20    sqlite3 *db;. 
1fbad 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20   Vdbe *v;.  int 
1fbae 69 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65  i;..  if( pParse
1fbaf 3d 3d 30 20 7c 7c 20 28 64 62 3d 70 50 61 72 73  ==0 || (db=pPars
1fbb0 65 2d 3e 64 62 29 3d 3d 30 20 7c 7c 20 64 62 2d  e->db)==0 || db-
1fbb1 3e 61 44 62 5b 30 5d 2e 70 42 74 3d 3d 30 20 29  >aDb[0].pBt==0 )
1fbb2 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70   return;.  if( p
1fbb3 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64  Parse->nErr || d
1fbb4 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1fbb5 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
1fbb6 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
1fbb7 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
1fbb8 54 52 41 4e 53 41 43 54 49 4f 4e 2c 20 22 42 45  TRANSACTION, "BE
1fbb9 47 49 4e 22 2c 20 30 2c 20 30 29 20 29 20 72 65  GIN", 0, 0) ) re
1fbba 74 75 72 6e 3b 0a 0a 20 20 76 20 3d 20 73 71 6c  turn;..  v = sql
1fbbb 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
1fbbc 73 65 29 3b 0a 20 20 69 66 28 20 21 76 20 29 20  se);.  if( !v ) 
1fbbd 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 74 79  return;.  if( ty
1fbbe 70 65 21 3d 54 4b 5f 44 45 46 45 52 52 45 44 20  pe!=TK_DEFERRED 
1fbbf 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ){.    for(i=0; 
1fbc0 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
1fbc1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1fbc2 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 54  beAddOp2(v, OP_T
1fbc3 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 2c 20 28  ransaction, i, (
1fbc4 74 79 70 65 3d 3d 54 4b 5f 45 58 43 4c 55 53 49  type==TK_EXCLUSI
1fbc5 56 45 29 2b 31 29 3b 0a 20 20 20 20 20 20 73 71  VE)+1);.      sq
1fbc6 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72  lite3VdbeUsesBtr
1fbc7 65 65 28 76 2c 20 69 29 3b 0a 20 20 20 20 7d 0a  ee(v, i);.    }.
1fbc8 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
1fbc9 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 75  eAddOp2(v, OP_Au
1fbca 74 6f 43 6f 6d 6d 69 74 2c 20 30 2c 20 30 29 3b  toCommit, 0, 0);
1fbcb 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69 74  .}../*.** Commit
1fbcc 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a   a transaction.*
1fbcd 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1fbce 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d   void sqlite3Com
1fbcf 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e 28 50  mitTransaction(P
1fbd0 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20  arse *pParse){. 
1fbd1 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20   sqlite3 *db;.  
1fbd2 56 64 62 65 20 2a 76 3b 0a 0a 20 20 69 66 28 20  Vdbe *v;..  if( 
1fbd3 70 50 61 72 73 65 3d 3d 30 20 7c 7c 20 28 64 62  pParse==0 || (db
1fbd4 3d 70 50 61 72 73 65 2d 3e 64 62 29 3d 3d 30 20  =pParse->db)==0 
1fbd5 7c 7c 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42  || db->aDb[0].pB
1fbd6 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  t==0 ) return;. 
1fbd7 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
1fbd8 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  r || db->mallocF
1fbd9 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a  ailed ) return;.
1fbda 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
1fbdb 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53  hCheck(pParse, S
1fbdc 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49 4f  QLITE_TRANSACTIO
1fbdd 4e 2c 20 22 43 4f 4d 4d 49 54 22 2c 20 30 2c 20  N, "COMMIT", 0, 
1fbde 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20  0) ) return;..  
1fbdf 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
1fbe0 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
1fbe1 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ( v ){.    sqlit
1fbe2 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1fbe3 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c 20 31  OP_AutoCommit, 1
1fbe4 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  , 0);.  }.}../*.
1fbe5 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 20 74 72  ** Rollback a tr
1fbe6 61 6e 73 61 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c  ansaction.*/.SQL
1fbe7 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1fbe8 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b   sqlite3Rollback
1fbe9 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73  Transaction(Pars
1fbea 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71  e *pParse){.  sq
1fbeb 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 62  lite3 *db;.  Vdb
1fbec 65 20 2a 76 3b 0a 0a 20 20 69 66 28 20 70 50 61  e *v;..  if( pPa
1fbed 72 73 65 3d 3d 30 20 7c 7c 20 28 64 62 3d 70 50  rse==0 || (db=pP
1fbee 61 72 73 65 2d 3e 64 62 29 3d 3d 30 20 7c 7c 20  arse->db)==0 || 
1fbef 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 3d 3d  db->aDb[0].pBt==
1fbf0 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  0 ) return;.  if
1fbf1 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c  ( pParse->nErr |
1fbf2 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
1fbf3 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  ed ) return;.  i
1fbf4 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
1fbf5 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
1fbf6 54 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e 2c 20  TE_TRANSACTION, 
1fbf7 22 52 4f 4c 4c 42 41 43 4b 22 2c 20 30 2c 20 30  "ROLLBACK", 0, 0
1fbf8 29 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 76  ) ) return;..  v
1fbf9 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
1fbfa 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
1fbfb 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65   v ){.    sqlite
1fbfc 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1fbfd 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c 20 31 2c  P_AutoCommit, 1,
1fbfe 20 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a   1);.  }.}../*.*
1fbff 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20  * Make sure the 
1fc00 54 45 4d 50 20 64 61 74 61 62 61 73 65 20 69 73  TEMP database is
1fc01 20 6f 70 65 6e 20 61 6e 64 20 61 76 61 69 6c 61   open and availa
1fc02 62 6c 65 20 66 6f 72 20 75 73 65 2e 20 20 52 65  ble for use.  Re
1fc03 74 75 72 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62  turn.** the numb
1fc04 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 20 20 4c  er of errors.  L
1fc05 65 61 76 65 20 61 6e 79 20 65 72 72 6f 72 20 6d  eave any error m
1fc06 65 73 73 61 67 65 73 20 69 6e 20 74 68 65 20 70  essages in the p
1fc07 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65 2e  Parse structure.
1fc08 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1fc09 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 70  TE int sqlite3Op
1fc0a 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65 28 50  enTempDatabase(P
1fc0b 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20  arse *pParse){. 
1fc0c 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
1fc0d 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28  Parse->db;.  if(
1fc0e 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 3d   db->aDb[1].pBt=
1fc0f 3d 30 20 26 26 20 21 70 50 61 72 73 65 2d 3e 65  =0 && !pParse->e
1fc10 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 69 6e  xplain ){.    in
1fc11 74 20 72 63 3b 0a 20 20 20 20 73 74 61 74 69 63  t rc;.    static
1fc12 20 63 6f 6e 73 74 20 69 6e 74 20 66 6c 61 67 73   const int flags
1fc13 20 3d 20 0a 20 20 20 20 20 20 20 20 20 20 53 51   = .          SQ
1fc14 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
1fc15 49 54 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20  ITE |.          
1fc16 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
1fc17 54 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20 53  TE |.          S
1fc18 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55  QLITE_OPEN_EXCLU
1fc19 53 49 56 45 20 7c 0a 20 20 20 20 20 20 20 20 20  SIVE |.         
1fc1a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c   SQLITE_OPEN_DEL
1fc1b 45 54 45 4f 4e 43 4c 4f 53 45 20 7c 0a 20 20 20  ETEONCLOSE |.   
1fc1c 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
1fc1d 45 4e 5f 54 45 4d 50 5f 44 42 3b 0a 0a 20 20 20  EN_TEMP_DB;..   
1fc1e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1fc1f 65 65 46 61 63 74 6f 72 79 28 64 62 2c 20 30 2c  eeFactory(db, 0,
1fc20 20 30 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55   0, SQLITE_DEFAU
1fc21 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 2c 20 66  LT_CACHE_SIZE, f
1fc22 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20  lags,.          
1fc23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc24 20 20 20 20 20 20 20 26 64 62 2d 3e 61 44 62 5b         &db->aDb[
1fc25 31 5d 2e 70 42 74 29 3b 0a 20 20 20 20 69 66 28  1].pBt);.    if(
1fc26 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1fc27 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
1fc28 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1fc29 22 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20  "unable to open 
1fc2a 61 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61  a temporary data
1fc2b 62 61 73 65 20 22 0a 20 20 20 20 20 20 20 20 22  base ".        "
1fc2c 66 69 6c 65 20 66 6f 72 20 73 74 6f 72 69 6e 67  file for storing
1fc2d 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
1fc2e 73 22 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73  s");.      pPars
1fc2f 65 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20  e->rc = rc;.    
1fc30 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
1fc31 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 64  }.    assert( (d
1fc32 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
1fc33 45 5f 49 6e 54 72 61 6e 73 29 3d 3d 30 20 7c 7c  E_InTrans)==0 ||
1fc34 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20   db->autoCommit 
1fc35 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  );.    assert( d
1fc36 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d  b->aDb[1].pSchem
1fc37 61 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  a );.    sqlite3
1fc38 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65  PagerJournalMode
1fc39 28 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67  (sqlite3BtreePag
1fc3a 65 72 28 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42  er(db->aDb[1].pB
1fc3b 74 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  t),.            
1fc3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc3d 64 62 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d  db->dfltJournalM
1fc3e 6f 64 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ode);.  }.  retu
1fc3f 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  rn 0;.}../*.** G
1fc40 65 6e 65 72 61 74 65 20 56 44 42 45 20 63 6f 64  enerate VDBE cod
1fc41 65 20 74 68 61 74 20 77 69 6c 6c 20 76 65 72 69  e that will veri
1fc42 66 79 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f  fy the schema co
1fc43 6f 6b 69 65 20 61 6e 64 20 73 74 61 72 74 0a 2a  okie and start.*
1fc44 2a 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63  * a read-transac
1fc45 74 69 6f 6e 20 66 6f 72 20 61 6c 6c 20 6e 61 6d  tion for all nam
1fc46 65 64 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ed database file
1fc47 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69  s..**.** It is i
1fc48 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 61 6c  mportant that al
1fc49 6c 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 73  l schema cookies
1fc4a 20 62 65 20 76 65 72 69 66 69 65 64 20 61 6e 64   be verified and
1fc4b 20 61 6c 6c 0a 2a 2a 20 72 65 61 64 20 74 72 61   all.** read tra
1fc4c 6e 73 61 63 74 69 6f 6e 73 20 62 65 20 73 74 61  nsactions be sta
1fc4d 72 74 65 64 20 62 65 66 6f 72 65 20 61 6e 79 74  rted before anyt
1fc4e 68 69 6e 67 20 65 6c 73 65 20 68 61 70 70 65 6e  hing else happen
1fc4f 73 20 69 6e 0a 2a 2a 20 74 68 65 20 56 44 42 45  s in.** the VDBE
1fc50 20 70 72 6f 67 72 61 6d 2e 20 20 42 75 74 20 74   program.  But t
1fc51 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20  his routine can 
1fc52 62 65 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20  be called after 
1fc53 6d 75 63 68 20 6f 74 68 65 72 0a 2a 2a 20 63 6f  much other.** co
1fc54 64 65 20 68 61 73 20 62 65 65 6e 20 67 65 6e 65  de has been gene
1fc55 72 61 74 65 64 2e 20 20 53 6f 20 68 65 72 65 20  rated.  So here 
1fc56 69 73 20 77 68 61 74 20 77 65 20 64 6f 3a 0a 2a  is what we do:.*
1fc57 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74  *.** The first t
1fc58 69 6d 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ime this routine
1fc59 20 69 73 20 63 61 6c 6c 65 64 2c 20 77 65 20 63   is called, we c
1fc5a 6f 64 65 20 61 6e 20 4f 50 5f 47 6f 74 6f 20 74  ode an OP_Goto t
1fc5b 68 61 74 0a 2a 2a 20 77 69 6c 6c 20 6a 75 6d 70  hat.** will jump
1fc5c 20 74 6f 20 61 20 73 75 62 72 6f 75 74 69 6e 65   to a subroutine
1fc5d 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74   at the end of t
1fc5e 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 54 68 65  he program.  The
1fc5f 6e 20 77 65 0a 2a 2a 20 72 65 63 6f 72 64 20 65  n we.** record e
1fc60 76 65 72 79 20 64 61 74 61 62 61 73 65 20 74 68  very database th
1fc61 61 74 20 6e 65 65 64 73 20 69 74 73 20 73 63 68  at needs its sch
1fc62 65 6d 61 20 76 65 72 69 66 69 65 64 20 69 6e 20  ema verified in 
1fc63 74 68 65 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 63  the.** pParse->c
1fc64 6f 6f 6b 69 65 4d 61 73 6b 20 66 69 65 6c 64 2e  ookieMask field.
1fc65 20 20 4c 61 74 65 72 2c 20 61 66 74 65 72 20 61    Later, after a
1fc66 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65 20 68 61  ll other code ha
1fc67 73 20 62 65 65 6e 0a 2a 2a 20 67 65 6e 65 72 61  s been.** genera
1fc68 74 65 64 2c 20 74 68 65 20 73 75 62 72 6f 75 74  ted, the subrout
1fc69 69 6e 65 20 74 68 61 74 20 64 6f 65 73 20 74 68  ine that does th
1fc6a 65 20 63 6f 6f 6b 69 65 20 76 65 72 69 66 69 63  e cookie verific
1fc6b 61 74 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20 73 74  ations and.** st
1fc6c 61 72 74 73 20 74 68 65 20 74 72 61 6e 73 61 63  arts the transac
1fc6d 74 69 6f 6e 73 20 77 69 6c 6c 20 62 65 20 63 6f  tions will be co
1fc6e 64 65 64 20 61 6e 64 20 74 68 65 20 4f 50 5f 47  ded and the OP_G
1fc6f 6f 74 6f 20 50 32 20 76 61 6c 75 65 0a 2a 2a 20  oto P2 value.** 
1fc70 77 69 6c 6c 20 62 65 20 6d 61 64 65 20 74 6f 20  will be made to 
1fc71 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20 73 75  point to that su
1fc72 62 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 67  broutine.  The g
1fc73 65 6e 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  eneration of the
1fc74 0a 2a 2a 20 63 6f 6f 6b 69 65 20 76 65 72 69 66  .** cookie verif
1fc75 69 63 61 74 69 6f 6e 20 73 75 62 72 6f 75 74 69  ication subrouti
1fc76 6e 65 20 63 6f 64 65 20 68 61 70 70 65 6e 73 20  ne code happens 
1fc77 69 6e 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68  in sqlite3Finish
1fc78 43 6f 64 69 6e 67 28 29 2e 0a 2a 2a 0a 2a 2a 20  Coding()..**.** 
1fc79 49 66 20 69 44 62 3c 30 20 74 68 65 6e 20 63 6f  If iDb<0 then co
1fc7a 64 65 20 74 68 65 20 4f 50 5f 47 6f 74 6f 20 6f  de the OP_Goto o
1fc7b 6e 6c 79 20 2d 20 64 6f 6e 27 74 20 73 65 74 20  nly - don't set 
1fc7c 66 6c 61 67 20 74 6f 20 76 65 72 69 66 79 20 74  flag to verify t
1fc7d 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 6f 6e 20  he.** schema on 
1fc7e 61 6e 79 20 64 61 74 61 62 61 73 65 73 2e 20 20  any databases.  
1fc7f 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64  This can be used
1fc80 20 74 6f 20 70 6f 73 69 74 69 6f 6e 20 74 68 65   to position the
1fc81 20 4f 50 5f 47 6f 74 6f 0a 2a 2a 20 65 61 72 6c   OP_Goto.** earl
1fc82 79 20 69 6e 20 74 68 65 20 63 6f 64 65 2c 20 62  y in the code, b
1fc83 65 66 6f 72 65 20 77 65 20 6b 6e 6f 77 20 69 66  efore we know if
1fc84 20 61 6e 79 20 64 61 74 61 62 61 73 65 20 74 61   any database ta
1fc85 62 6c 65 73 20 77 69 6c 6c 20 62 65 20 75 73 65  bles will be use
1fc86 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
1fc87 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1fc88 33 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d  3CodeVerifySchem
1fc89 61 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  a(Parse *pParse,
1fc8a 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 73 71 6c   int iDb){.  sql
1fc8b 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 62 65  ite3 *db;.  Vdbe
1fc8c 20 2a 76 3b 0a 20 20 69 6e 74 20 6d 61 73 6b 3b   *v;.  int mask;
1fc8d 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  ..  v = sqlite3G
1fc8e 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
1fc8f 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74    if( v==0 ) ret
1fc90 75 72 6e 3b 20 20 2f 2a 20 54 68 69 73 20 6f 6e  urn;  /* This on
1fc91 6c 79 20 68 61 70 70 65 6e 73 20 69 66 20 74 68  ly happens if th
1fc92 65 72 65 20 77 61 73 20 61 20 70 72 69 6f 72 20  ere was a prior 
1fc93 65 72 72 6f 72 20 2a 2f 0a 20 20 64 62 20 3d 20  error */.  db = 
1fc94 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66  pParse->db;.  if
1fc95 28 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65  ( pParse->cookie
1fc96 47 6f 74 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 70  Goto==0 ){.    p
1fc97 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74  Parse->cookieGot
1fc98 6f 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  o = sqlite3VdbeA
1fc99 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
1fc9a 2c 20 30 2c 20 30 29 2b 31 3b 0a 20 20 7d 0a 20  , 0, 0)+1;.  }. 
1fc9b 20 69 66 28 20 69 44 62 3e 3d 30 20 29 7b 0a 20   if( iDb>=0 ){. 
1fc9c 20 20 20 61 73 73 65 72 74 28 20 69 44 62 3c 64     assert( iDb<d
1fc9d 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 20 20 61 73  b->nDb );.    as
1fc9e 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b 69 44  sert( db->aDb[iD
1fc9f 62 5d 2e 70 42 74 21 3d 30 20 7c 7c 20 69 44 62  b].pBt!=0 || iDb
1fca0 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==1 );.    asser
1fca1 74 28 20 69 44 62 3c 53 51 4c 49 54 45 5f 4d 41  t( iDb<SQLITE_MA
1fca2 58 5f 41 54 54 41 43 48 45 44 2b 32 20 29 3b 0a  X_ATTACHED+2 );.
1fca3 20 20 20 20 6d 61 73 6b 20 3d 20 31 3c 3c 69 44      mask = 1<<iD
1fca4 62 3b 0a 20 20 20 20 69 66 28 20 28 70 50 61 72  b;.    if( (pPar
1fca5 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 26  se->cookieMask &
1fca6 20 6d 61 73 6b 29 3d 3d 30 20 29 7b 0a 20 20 20   mask)==0 ){.   
1fca7 20 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69     pParse->cooki
1fca8 65 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20  eMask |= mask;. 
1fca9 20 20 20 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f       pParse->coo
1fcaa 6b 69 65 56 61 6c 75 65 5b 69 44 62 5d 20 3d 20  kieValue[iDb] = 
1fcab 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
1fcac 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f  hema->schema_coo
1fcad 6b 69 65 3b 0a 20 20 20 20 20 20 69 66 28 20 21  kie;.      if( !
1fcae 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69  OMIT_TEMPDB && i
1fcaf 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20  Db==1 ){.       
1fcb0 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70   sqlite3OpenTemp
1fcb1 44 61 74 61 62 61 73 65 28 70 50 61 72 73 65 29  Database(pParse)
1fcb2 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1fcb3 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e    }.}../*.** Gen
1fcb4 65 72 61 74 65 20 56 44 42 45 20 63 6f 64 65 20  erate VDBE code 
1fcb5 74 68 61 74 20 70 72 65 70 61 72 65 73 20 66 6f  that prepares fo
1fcb6 72 20 64 6f 69 6e 67 20 61 6e 20 6f 70 65 72 61  r doing an opera
1fcb7 74 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 6d 69 67  tion that.** mig
1fcb8 68 74 20 63 68 61 6e 67 65 20 74 68 65 20 64 61  ht change the da
1fcb9 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  tabase..**.** Th
1fcba 69 73 20 72 6f 75 74 69 6e 65 20 73 74 61 72 74  is routine start
1fcbb 73 20 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74  s a new transact
1fcbc 69 6f 6e 20 69 66 20 77 65 20 61 72 65 20 6e 6f  ion if we are no
1fcbd 74 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e  t already within
1fcbe 0a 2a 2a 20 61 20 74 72 61 6e 73 61 63 74 69 6f  .** a transactio
1fcbf 6e 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 6c  n.  If we are al
1fcc0 72 65 61 64 79 20 77 69 74 68 69 6e 20 61 20 74  ready within a t
1fcc1 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e  ransaction, then
1fcc2 20 61 20 63 68 65 63 6b 70 6f 69 6e 74 0a 2a 2a   a checkpoint.**
1fcc3 20 69 73 20 73 65 74 20 69 66 20 74 68 65 20 73   is set if the s
1fcc4 65 74 53 74 61 74 65 6d 65 6e 74 20 70 61 72 61  etStatement para
1fcc5 6d 65 74 65 72 20 69 73 20 74 72 75 65 2e 20 20  meter is true.  
1fcc6 41 20 63 68 65 63 6b 70 6f 69 6e 74 20 73 68 6f  A checkpoint sho
1fcc7 75 6c 64 0a 2a 2a 20 62 65 20 73 65 74 20 66 6f  uld.** be set fo
1fcc8 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 68 61  r operations tha
1fcc9 74 20 6d 69 67 68 74 20 66 61 69 6c 20 28 64 75  t might fail (du
1fcca 65 20 74 6f 20 61 20 63 6f 6e 73 74 72 61 69 6e  e to a constrain
1fccb 74 29 20 70 61 72 74 20 6f 66 0a 2a 2a 20 74 68  t) part of.** th
1fccc 65 20 77 61 79 20 74 68 72 6f 75 67 68 20 61 6e  e way through an
1fccd 64 20 77 68 69 63 68 20 77 69 6c 6c 20 6e 65 65  d which will nee
1fcce 64 20 74 6f 20 75 6e 64 6f 20 73 6f 6d 65 20 77  d to undo some w
1fccf 72 69 74 65 73 20 77 69 74 68 6f 75 74 20 68 61  rites without ha
1fcd0 76 69 6e 67 20 74 6f 0a 2a 2a 20 72 6f 6c 6c 62  ving to.** rollb
1fcd1 61 63 6b 20 74 68 65 20 77 68 6f 6c 65 20 74 72  ack the whole tr
1fcd2 61 6e 73 61 63 74 69 6f 6e 2e 20 20 46 6f 72 20  ansaction.  For 
1fcd3 6f 70 65 72 61 74 69 6f 6e 73 20 77 68 65 72 65  operations where
1fcd4 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74 73   all constraints
1fcd5 0a 2a 2a 20 63 61 6e 20 62 65 20 63 68 65 63 6b  .** can be check
1fcd6 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 63 68  ed before any ch
1fcd7 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20 74  anges are made t
1fcd8 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20  o the database, 
1fcd9 69 74 20 69 73 20 6e 65 76 65 72 0a 2a 2a 20 6e  it is never.** n
1fcda 65 63 65 73 73 61 72 79 20 74 6f 20 75 6e 64 6f  ecessary to undo
1fcdb 20 61 20 77 72 69 74 65 20 61 6e 64 20 74 68 65   a write and the
1fcdc 20 63 68 65 63 6b 70 6f 69 6e 74 20 73 68 6f 75   checkpoint shou
1fcdd 6c 64 20 6e 6f 74 20 62 65 20 73 65 74 2e 0a 2a  ld not be set..*
1fcde 2a 0a 2a 2a 20 4f 6e 6c 79 20 64 61 74 61 62 61  *.** Only databa
1fcdf 73 65 20 69 44 62 20 61 6e 64 20 74 68 65 20 74  se iDb and the t
1fce0 65 6d 70 20 64 61 74 61 62 61 73 65 20 61 72 65  emp database are
1fce1 20 6d 61 64 65 20 77 72 69 74 61 62 6c 65 20 62   made writable b
1fce2 79 20 74 68 69 73 20 63 61 6c 6c 2e 0a 2a 2a 20  y this call..** 
1fce3 49 66 20 69 44 62 3d 3d 30 2c 20 74 68 65 6e 20  If iDb==0, then 
1fce4 74 68 65 20 6d 61 69 6e 20 61 6e 64 20 74 65 6d  the main and tem
1fce5 70 20 64 61 74 61 62 61 73 65 73 20 61 72 65 20  p databases are 
1fce6 6d 61 64 65 20 77 72 69 74 61 62 6c 65 2e 20 20  made writable.  
1fce7 20 49 66 0a 2a 2a 20 69 44 62 3d 3d 31 20 74 68   If.** iDb==1 th
1fce8 65 6e 20 6f 6e 6c 79 20 74 68 65 20 74 65 6d 70  en only the temp
1fce9 20 64 61 74 61 62 61 73 65 20 69 73 20 6d 61 64   database is mad
1fcea 65 20 77 72 69 74 61 62 6c 65 2e 20 20 49 66 20  e writable.  If 
1fceb 69 44 62 3e 31 20 74 68 65 6e 20 74 68 65 0a 2a  iDb>1 then the.*
1fcec 2a 20 73 70 65 63 69 66 69 65 64 20 61 75 78 69  * specified auxi
1fced 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 61  liary database a
1fcee 6e 64 20 74 68 65 20 74 65 6d 70 20 64 61 74 61  nd the temp data
1fcef 62 61 73 65 20 61 72 65 20 6d 61 64 65 20 77 72  base are made wr
1fcf0 69 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54  itable..*/.SQLIT
1fcf1 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1fcf2 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
1fcf3 4f 70 65 72 61 74 69 6f 6e 28 50 61 72 73 65 20  Operation(Parse 
1fcf4 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 73 65 74  *pParse, int set
1fcf5 53 74 61 74 65 6d 65 6e 74 2c 20 69 6e 74 20 69  Statement, int i
1fcf6 44 62 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  Db){.  Vdbe *v =
1fcf7 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
1fcf8 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76  pParse);.  if( v
1fcf9 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
1fcfa 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66  sqlite3CodeVerif
1fcfb 79 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20  ySchema(pParse, 
1fcfc 69 44 62 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  iDb);.  pParse->
1fcfd 77 72 69 74 65 4d 61 73 6b 20 7c 3d 20 31 3c 3c  writeMask |= 1<<
1fcfe 69 44 62 3b 0a 20 20 69 66 28 20 73 65 74 53 74  iDb;.  if( setSt
1fcff 61 74 65 6d 65 6e 74 20 26 26 20 70 50 61 72 73  atement && pPars
1fd00 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 7b 0a  e->nested==0 ){.
1fd01 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1fd02 64 64 4f 70 31 28 76 2c 20 4f 50 5f 53 74 61 74  ddOp1(v, OP_Stat
1fd03 65 6d 65 6e 74 2c 20 69 44 62 29 3b 0a 20 20 7d  ement, iDb);.  }
1fd04 0a 20 20 69 66 28 20 28 4f 4d 49 54 5f 54 45 4d  .  if( (OMIT_TEM
1fd05 50 44 42 20 7c 7c 20 69 44 62 21 3d 31 29 20 26  PDB || iDb!=1) &
1fd06 26 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44  & pParse->db->aD
1fd07 62 5b 31 5d 2e 70 42 74 21 3d 30 20 29 7b 0a 20  b[1].pBt!=0 ){. 
1fd08 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57     sqlite3BeginW
1fd09 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50  riteOperation(pP
1fd0a 61 72 73 65 2c 20 73 65 74 53 74 61 74 65 6d 65  arse, setStateme
1fd0b 6e 74 2c 20 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  nt, 1);.  }.}../
1fd0c 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65  *.** Check to se
1fd0d 65 20 69 66 20 70 49 6e 64 65 78 20 75 73 65 73  e if pIndex uses
1fd0e 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73   the collating s
1fd0f 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 2e 20 20  equence pColl.  
1fd10 52 65 74 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69  Return.** true i
1fd11 66 20 69 74 20 64 6f 65 73 20 61 6e 64 20 66 61  f it does and fa
1fd12 6c 73 65 20 69 66 20 69 74 20 64 6f 65 73 20 6e  lse if it does n
1fd13 6f 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ot..*/.#ifndef S
1fd14 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44  QLITE_OMIT_REIND
1fd15 45 58 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f  EX.static int co
1fd16 6c 6c 61 74 69 6f 6e 4d 61 74 63 68 28 63 6f 6e  llationMatch(con
1fd17 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 2c 20  st char *zColl, 
1fd18 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 29 7b 0a  Index *pIndex){.
1fd19 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69    int i;.  for(i
1fd1a 3d 30 3b 20 69 3c 70 49 6e 64 65 78 2d 3e 6e 43  =0; i<pIndex->nC
1fd1b 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20  olumn; i++){.   
1fd1c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d   const char *z =
1fd1d 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b   pIndex->azColl[
1fd1e 69 5d 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 7a  i];.    if( z==z
1fd1f 43 6f 6c 6c 20 7c 7c 20 28 7a 20 26 26 20 7a 43  Coll || (z && zC
1fd20 6f 6c 6c 20 26 26 20 30 3d 3d 73 71 6c 69 74 65  oll && 0==sqlite
1fd21 33 53 74 72 49 43 6d 70 28 7a 2c 20 7a 43 6f 6c  3StrICmp(z, zCol
1fd22 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  l)) ){.      ret
1fd23 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 1;.    }.  }
1fd24 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23  .  return 0;.}.#
1fd25 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 63  endif../*.** Rec
1fd26 6f 6d 70 75 74 65 20 61 6c 6c 20 69 6e 64 69 63  ompute all indic
1fd27 65 73 20 6f 66 20 70 54 61 62 20 74 68 61 74 20  es of pTab that 
1fd28 75 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e  use the collatin
1fd29 67 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c  g sequence pColl
1fd2a 2e 0a 2a 2a 20 49 66 20 70 43 6f 6c 6c 3d 3d 30  ..** If pColl==0
1fd2b 20 74 68 65 6e 20 72 65 63 6f 6d 70 75 74 65 20   then recompute 
1fd2c 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 70  all indices of p
1fd2d 54 61 62 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  Tab..*/.#ifndef 
1fd2e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e  SQLITE_OMIT_REIN
1fd2f 44 45 58 0a 73 74 61 74 69 63 20 76 6f 69 64 20  DEX.static void 
1fd30 72 65 69 6e 64 65 78 54 61 62 6c 65 28 50 61 72  reindexTable(Par
1fd31 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c  se *pParse, Tabl
1fd32 65 20 2a 70 54 61 62 2c 20 63 68 61 72 20 63 6f  e *pTab, char co
1fd33 6e 73 74 20 2a 7a 43 6f 6c 6c 29 7b 0a 20 20 49  nst *zColl){.  I
1fd34 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 20 20 20  ndex *pIndex;   
1fd35 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e             /* An
1fd36 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61 74 65   index associate
1fd37 64 20 77 69 74 68 20 70 54 61 62 20 2a 2f 0a 0a  d with pTab */..
1fd38 20 20 66 6f 72 28 70 49 6e 64 65 78 3d 70 54 61    for(pIndex=pTa
1fd39 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 6e 64 65  b->pIndex; pInde
1fd3a 78 3b 20 70 49 6e 64 65 78 3d 70 49 6e 64 65 78  x; pIndex=pIndex
1fd3b 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66  ->pNext){.    if
1fd3c 28 20 7a 43 6f 6c 6c 3d 3d 30 20 7c 7c 20 63 6f  ( zColl==0 || co
1fd3d 6c 6c 61 74 69 6f 6e 4d 61 74 63 68 28 7a 43 6f  llationMatch(zCo
1fd3e 6c 6c 2c 20 70 49 6e 64 65 78 29 20 29 7b 0a 20  ll, pIndex) ){. 
1fd3f 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20 73       int iDb = s
1fd40 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
1fd41 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  dex(pParse->db, 
1fd42 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
1fd43 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67        sqlite3Beg
1fd44 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e  inWriteOperation
1fd45 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29  (pParse, 0, iDb)
1fd46 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  ;.      sqlite3R
1fd47 65 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72 73  efillIndex(pPars
1fd48 65 2c 20 70 49 6e 64 65 78 2c 20 2d 31 29 3b 0a  e, pIndex, -1);.
1fd49 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64      }.  }.}.#end
1fd4a 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 6d 70  if../*.** Recomp
1fd4b 75 74 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20  ute all indices 
1fd4c 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e  of all tables in
1fd4d 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20 77   all databases w
1fd4e 68 65 72 65 20 74 68 65 0a 2a 2a 20 69 6e 64 69  here the.** indi
1fd4f 63 65 73 20 75 73 65 20 74 68 65 20 63 6f 6c 6c  ces use the coll
1fd50 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 70  ating sequence p
1fd51 43 6f 6c 6c 2e 20 20 49 66 20 70 43 6f 6c 6c 3d  Coll.  If pColl=
1fd52 3d 30 20 74 68 65 6e 20 72 65 63 6f 6d 70 75 74  =0 then recomput
1fd53 65 0a 2a 2a 20 61 6c 6c 20 69 6e 64 69 63 65 73  e.** all indices
1fd54 20 65 76 65 72 79 77 68 65 72 65 2e 0a 2a 2f 0a   everywhere..*/.
1fd55 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1fd56 4d 49 54 5f 52 45 49 4e 44 45 58 0a 73 74 61 74  MIT_REINDEX.stat
1fd57 69 63 20 76 6f 69 64 20 72 65 69 6e 64 65 78 44  ic void reindexD
1fd58 61 74 61 62 61 73 65 73 28 50 61 72 73 65 20 2a  atabases(Parse *
1fd59 70 50 61 72 73 65 2c 20 63 68 61 72 20 63 6f 6e  pParse, char con
1fd5a 73 74 20 2a 7a 43 6f 6c 6c 29 7b 0a 20 20 44 62  st *zColl){.  Db
1fd5b 20 2a 70 44 62 3b 20 20 20 20 20 20 20 20 20 20   *pDb;          
1fd5c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73            /* A s
1fd5d 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 2a  ingle database *
1fd5e 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  /.  int iDb;    
1fd5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fd60 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
1fd61 69 6e 64 65 78 20 6e 75 6d 62 65 72 20 2a 2f 0a  index number */.
1fd62 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1fd63 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 2f 2a  pParse->db;   /*
1fd64 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
1fd65 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 48 61  nnection */.  Ha
1fd66 73 68 45 6c 65 6d 20 2a 6b 3b 20 20 20 20 20 20  shElem *k;      
1fd67 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
1fd68 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 74 61   looping over ta
1fd69 62 6c 65 73 20 69 6e 20 70 44 62 20 2a 2f 0a 20  bles in pDb */. 
1fd6a 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20   Table *pTab;   
1fd6b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1fd6c 41 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64  A table in the d
1fd6d 61 74 61 62 61 73 65 20 2a 2f 0a 0a 20 20 66 6f  atabase */..  fo
1fd6e 72 28 69 44 62 3d 30 2c 20 70 44 62 3d 64 62 2d  r(iDb=0, pDb=db-
1fd6f 3e 61 44 62 3b 20 69 44 62 3c 64 62 2d 3e 6e 44  >aDb; iDb<db->nD
1fd70 62 3b 20 69 44 62 2b 2b 2c 20 70 44 62 2b 2b 29  b; iDb++, pDb++)
1fd71 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 44  {.    assert( pD
1fd72 62 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72 28  b!=0 );.    for(
1fd73 6b 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  k=sqliteHashFirs
1fd74 74 28 26 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  t(&pDb->pSchema-
1fd75 3e 74 62 6c 48 61 73 68 29 3b 20 20 6b 3b 20 6b  >tblHash);  k; k
1fd76 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28  =sqliteHashNext(
1fd77 6b 29 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20  k)){.      pTab 
1fd78 3d 20 28 54 61 62 6c 65 2a 29 73 71 6c 69 74 65  = (Table*)sqlite
1fd79 48 61 73 68 44 61 74 61 28 6b 29 3b 0a 20 20 20  HashData(k);.   
1fd7a 20 20 20 72 65 69 6e 64 65 78 54 61 62 6c 65 28     reindexTable(
1fd7b 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 7a 43  pParse, pTab, zC
1fd7c 6f 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  oll);.    }.  }.
1fd7d 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
1fd7e 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f  Generate code fo
1fd7f 72 20 74 68 65 20 52 45 49 4e 44 45 58 20 63 6f  r the REINDEX co
1fd80 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  mmand..**.**    
1fd81 20 20 20 20 52 45 49 4e 44 45 58 20 20 20 20 20      REINDEX     
1fd82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fd83 20 20 20 20 20 20 20 2d 2d 20 31 0a 2a 2a 20 20         -- 1.**  
1fd84 20 20 20 20 20 20 52 45 49 4e 44 45 58 20 20 3c        REINDEX  <
1fd85 63 6f 6c 6c 61 74 69 6f 6e 3e 20 20 20 20 20 20  collation>      
1fd86 20 20 20 20 20 20 20 20 20 2d 2d 20 32 0a 2a 2a           -- 2.**
1fd87 20 20 20 20 20 20 20 20 52 45 49 4e 44 45 58 20          REINDEX 
1fd88 20 3f 3c 64 61 74 61 62 61 73 65 3e 2e 3f 3c 74   ?<database>.?<t
1fd89 61 62 6c 65 6e 61 6d 65 3e 20 20 2d 2d 20 33 0a  ablename>  -- 3.
1fd8a 2a 2a 20 20 20 20 20 20 20 20 52 45 49 4e 44 45  **        REINDE
1fd8b 58 20 20 3f 3c 64 61 74 61 62 61 73 65 3e 2e 3f  X  ?<database>.?
1fd8c 3c 69 6e 64 65 78 6e 61 6d 65 3e 20 20 2d 2d 20  <indexname>  -- 
1fd8d 34 0a 2a 2a 0a 2a 2a 20 46 6f 72 6d 20 31 20 63  4.**.** Form 1 c
1fd8e 61 75 73 65 73 20 61 6c 6c 20 69 6e 64 69 63 65  auses all indice
1fd8f 73 20 69 6e 20 61 6c 6c 20 61 74 74 61 63 68 65  s in all attache
1fd90 64 20 64 61 74 61 62 61 73 65 73 20 74 6f 20 62  d databases to b
1fd91 65 20 72 65 62 75 69 6c 74 2e 0a 2a 2a 20 46 6f  e rebuilt..** Fo
1fd92 72 6d 20 32 20 72 65 62 75 69 6c 64 73 20 61 6c  rm 2 rebuilds al
1fd93 6c 20 69 6e 64 69 63 65 73 20 69 6e 20 61 6c 6c  l indices in all
1fd94 20 64 61 74 61 62 61 73 65 73 20 74 68 61 74 20   databases that 
1fd95 75 73 65 20 74 68 65 20 6e 61 6d 65 64 0a 2a 2a  use the named.**
1fd96 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74   collating funct
1fd97 69 6f 6e 2e 20 20 46 6f 72 6d 73 20 33 20 61 6e  ion.  Forms 3 an
1fd98 64 20 34 20 72 65 62 75 69 6c 64 20 74 68 65 20  d 4 rebuild the 
1fd99 6e 61 6d 65 64 20 69 6e 64 65 78 20 6f 72 20 61  named index or a
1fd9a 6c 6c 0a 2a 2a 20 69 6e 64 69 63 65 73 20 61 73  ll.** indices as
1fd9b 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
1fd9c 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e 0a 2a  e named table..*
1fd9d 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1fd9e 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a 53 51  _OMIT_REINDEX.SQ
1fd9f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1fda0 64 20 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78  d sqlite3Reindex
1fda1 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1fda2 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 54  Token *pName1, T
1fda3 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a 20  oken *pName2){. 
1fda4 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
1fda5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1fda6 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  Collating sequen
1fda7 63 65 20 74 6f 20 62 65 20 72 65 69 6e 64 65 78  ce to be reindex
1fda8 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20  ed, or NULL */. 
1fda9 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20   char *z;       
1fdaa 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1fdab 4e 61 6d 65 20 6f 66 20 61 20 74 61 62 6c 65 20  Name of a table 
1fdac 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63 6f  or index */.  co
1fdad 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20  nst char *zDb;  
1fdae 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
1fdaf 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
1fdb0 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  e */.  Table *pT
1fdb1 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ab;             
1fdb2 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e     /* A table in
1fdb3 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f   the database */
1fdb4 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78  .  Index *pIndex
1fdb5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1fdb6 2a 20 41 6e 20 69 6e 64 65 78 20 61 73 73 6f 63  * An index assoc
1fdb7 69 61 74 65 64 20 77 69 74 68 20 70 54 61 62 20  iated with pTab 
1fdb8 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  */.  int iDb;   
1fdb9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fdba 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
1fdbb 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 20 2a 2f   index number */
1fdbc 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1fdbd 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 2f   pParse->db;   /
1fdbe 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
1fdbf 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54  onnection */.  T
1fdc0 6f 6b 65 6e 20 2a 70 4f 62 6a 4e 61 6d 65 3b 20  oken *pObjName; 
1fdc1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
1fdc2 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
1fdc3 6f 72 20 69 6e 64 65 78 20 74 6f 20 62 65 20 72  or index to be r
1fdc4 65 69 6e 64 65 78 65 64 20 2a 2f 0a 0a 20 20 2f  eindexed */..  /
1fdc5 2a 20 52 65 61 64 20 74 68 65 20 64 61 74 61 62  * Read the datab
1fdc6 61 73 65 20 73 63 68 65 6d 61 2e 20 49 66 20 61  ase schema. If a
1fdc7 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
1fdc8 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d  leave an error m
1fdc9 65 73 73 61 67 65 0a 20 20 2a 2a 20 61 6e 64 20  essage.  ** and 
1fdca 63 6f 64 65 20 69 6e 20 70 50 61 72 73 65 20 61  code in pParse a
1fdcb 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20  nd return NULL. 
1fdcc 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  */.  if( SQLITE_
1fdcd 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53  OK!=sqlite3ReadS
1fdce 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b  chema(pParse) ){
1fdcf 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
1fdd0 0a 0a 20 20 69 66 28 20 70 4e 61 6d 65 31 3d 3d  ..  if( pName1==
1fdd1 30 20 7c 7c 20 70 4e 61 6d 65 31 2d 3e 7a 3d 3d  0 || pName1->z==
1fdd2 30 20 29 7b 0a 20 20 20 20 72 65 69 6e 64 65 78  0 ){.    reindex
1fdd3 44 61 74 61 62 61 73 65 73 28 70 50 61 72 73 65  Databases(pParse
1fdd4 2c 20 30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  , 0);.    return
1fdd5 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4e  ;.  }else if( pN
1fdd6 61 6d 65 32 3d 3d 30 20 7c 7c 20 70 4e 61 6d 65  ame2==0 || pName
1fdd7 32 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 63  2->z==0 ){.    c
1fdd8 68 61 72 20 2a 7a 43 6f 6c 6c 3b 0a 20 20 20 20  har *zColl;.    
1fdd9 61 73 73 65 72 74 28 20 70 4e 61 6d 65 31 2d 3e  assert( pName1->
1fdda 7a 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 6c 20 3d  z );.    zColl =
1fddb 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
1fddc 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d 3e 64 62  Token(pParse->db
1fddd 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 69  , pName1);.    i
1fdde 66 28 20 21 7a 43 6f 6c 6c 20 29 20 72 65 74 75  f( !zColl ) retu
1fddf 72 6e 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20  rn;.    pColl = 
1fde0 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53  sqlite3FindCollS
1fde1 65 71 28 64 62 2c 20 45 4e 43 28 64 62 29 2c 20  eq(db, ENC(db), 
1fde2 7a 43 6f 6c 6c 2c 20 2d 31 2c 20 30 29 3b 0a 20  zColl, -1, 0);. 
1fde3 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a     if( pColl ){.
1fde4 20 20 20 20 20 20 69 66 28 20 7a 43 6f 6c 6c 20        if( zColl 
1fde5 29 7b 0a 20 20 20 20 20 20 20 20 72 65 69 6e 64  ){.        reind
1fde6 65 78 44 61 74 61 62 61 73 65 73 28 70 50 61 72  exDatabases(pPar
1fde7 73 65 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20  se, zColl);.    
1fde8 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1fde9 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20  e(db, zColl);.  
1fdea 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
1fdeb 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  rn;.    }.    sq
1fdec 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1fded 7a 43 6f 6c 6c 29 3b 0a 20 20 7d 0a 20 20 69 44  zColl);.  }.  iD
1fdee 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61  b = sqlite3TwoPa
1fdef 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70  rtName(pParse, p
1fdf0 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26  Name1, pName2, &
1fdf1 70 4f 62 6a 4e 61 6d 65 29 3b 0a 20 20 69 66 28  pObjName);.  if(
1fdf2 20 69 44 62 3c 30 20 29 20 72 65 74 75 72 6e 3b   iDb<0 ) return;
1fdf3 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61  .  z = sqlite3Na
1fdf4 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
1fdf5 70 4f 62 6a 4e 61 6d 65 29 3b 0a 20 20 69 66 28  pObjName);.  if(
1fdf6 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   z==0 ) return;.
1fdf7 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b    zDb = db->aDb[
1fdf8 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 70 54  iDb].zName;.  pT
1fdf9 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ab = sqlite3Find
1fdfa 54 61 62 6c 65 28 64 62 2c 20 7a 2c 20 7a 44 62  Table(db, z, zDb
1fdfb 29 3b 0a 20 20 69 66 28 20 70 54 61 62 20 29 7b  );.  if( pTab ){
1fdfc 0a 20 20 20 20 72 65 69 6e 64 65 78 54 61 62 6c  .    reindexTabl
1fdfd 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  e(pParse, pTab, 
1fdfe 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  0);.    sqlite3D
1fdff 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20  bFree(db, z);.  
1fe00 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
1fe01 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33  pIndex = sqlite3
1fe02 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 2c  FindIndex(db, z,
1fe03 20 7a 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 33   zDb);.  sqlite3
1fe04 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20  DbFree(db, z);. 
1fe05 20 69 66 28 20 70 49 6e 64 65 78 20 29 7b 0a 20   if( pIndex ){. 
1fe06 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57     sqlite3BeginW
1fe07 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50  riteOperation(pP
1fe08 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20  arse, 0, iDb);. 
1fe09 20 20 20 73 71 6c 69 74 65 33 52 65 66 69 6c 6c     sqlite3Refill
1fe0a 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 49  Index(pParse, pI
1fe0b 6e 64 65 78 2c 20 2d 31 29 3b 0a 20 20 20 20 72  ndex, -1);.    r
1fe0c 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c  eturn;.  }.  sql
1fe0d 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1fe0e 72 73 65 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20  rse, "unable to 
1fe0f 69 64 65 6e 74 69 66 79 20 74 68 65 20 6f 62 6a  identify the obj
1fe10 65 63 74 20 74 6f 20 62 65 20 72 65 69 6e 64 65  ect to be reinde
1fe11 78 65 64 22 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  xed");.}.#endif.
1fe12 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
1fe13 64 79 6e 61 6d 69 63 6c 79 20 61 6c 6c 6f 63 61  dynamicly alloca
1fe14 74 65 64 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  ted KeyInfo stru
1fe15 63 74 75 72 65 20 74 68 61 74 20 63 61 6e 20 62  cture that can b
1fe16 65 20 75 73 65 64 0a 2a 2a 20 77 69 74 68 20 4f  e used.** with O
1fe17 50 5f 4f 70 65 6e 52 65 61 64 20 6f 72 20 4f 50  P_OpenRead or OP
1fe18 5f 4f 70 65 6e 57 72 69 74 65 20 74 6f 20 61 63  _OpenWrite to ac
1fe19 63 65 73 73 20 64 61 74 61 62 61 73 65 20 69 6e  cess database in
1fe1a 64 65 78 20 70 49 64 78 2e 0a 2a 2a 0a 2a 2a 20  dex pIdx..**.** 
1fe1b 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 61  If successful, a
1fe1c 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
1fe1d 6e 65 77 20 73 74 72 75 63 74 75 72 65 20 69 73  new structure is
1fe1e 20 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68   returned. In th
1fe1f 69 73 20 63 61 73 65 0a 2a 2a 20 74 68 65 20 63  is case.** the c
1fe20 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73  aller is respons
1fe21 69 62 6c 65 20 66 6f 72 20 63 61 6c 6c 69 6e 67  ible for calling
1fe22 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1fe23 62 2c 20 29 20 6f 6e 20 74 68 65 20 72 65 74 75  b, ) on the retu
1fe24 72 6e 65 64 20 0a 2a 2a 20 70 6f 69 6e 74 65 72  rned .** pointer
1fe25 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  . If an error oc
1fe26 63 75 72 73 20 28 6f 75 74 20 6f 66 20 6d 65 6d  curs (out of mem
1fe27 6f 72 79 20 6f 72 20 6d 69 73 73 69 6e 67 20 63  ory or missing c
1fe28 6f 6c 6c 61 74 69 6f 6e 20 0a 2a 2a 20 73 65 71  ollation .** seq
1fe29 75 65 6e 63 65 29 2c 20 4e 55 4c 4c 20 69 73 20  uence), NULL is 
1fe2a 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65  returned and the
1fe2b 20 73 74 61 74 65 20 6f 66 20 70 50 61 72 73 65   state of pParse
1fe2c 20 75 70 64 61 74 65 64 20 74 6f 20 72 65 66 6c   updated to refl
1fe2d 65 63 74 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72  ect.** the error
1fe2e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1fe2f 41 54 45 20 4b 65 79 49 6e 66 6f 20 2a 73 71 6c  ATE KeyInfo *sql
1fe30 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f  ite3IndexKeyinfo
1fe31 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1fe32 49 6e 64 65 78 20 2a 70 49 64 78 29 7b 0a 20 20  Index *pIdx){.  
1fe33 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 43 6f  int i;.  int nCo
1fe34 6c 20 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  l = pIdx->nColum
1fe35 6e 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 20  n;.  int nBytes 
1fe36 3d 20 73 69 7a 65 6f 66 28 4b 65 79 49 6e 66 6f  = sizeof(KeyInfo
1fe37 29 20 2b 20 28 6e 43 6f 6c 2d 31 29 2a 73 69 7a  ) + (nCol-1)*siz
1fe38 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 20 2b 20  eof(CollSeq*) + 
1fe39 6e 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20  nCol;.  sqlite3 
1fe3a 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
1fe3b 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65  ;.  KeyInfo *pKe
1fe3c 79 20 3d 20 28 4b 65 79 49 6e 66 6f 20 2a 29 73  y = (KeyInfo *)s
1fe3d 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
1fe3e 72 6f 28 64 62 2c 20 6e 42 79 74 65 73 29 3b 0a  ro(db, nBytes);.
1fe3f 0a 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20  .  if( pKey ){. 
1fe40 20 20 20 70 4b 65 79 2d 3e 64 62 20 3d 20 70 50     pKey->db = pP
1fe41 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 70 4b  arse->db;.    pK
1fe42 65 79 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d  ey->aSortOrder =
1fe43 20 28 75 38 20 2a 29 26 28 70 4b 65 79 2d 3e 61   (u8 *)&(pKey->a
1fe44 43 6f 6c 6c 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 20  Coll[nCol]);.   
1fe45 20 61 73 73 65 72 74 28 20 26 70 4b 65 79 2d 3e   assert( &pKey->
1fe46 61 53 6f 72 74 4f 72 64 65 72 5b 6e 43 6f 6c 5d  aSortOrder[nCol]
1fe47 3d 3d 26 28 28 28 75 38 20 2a 29 70 4b 65 79 29  ==&(((u8 *)pKey)
1fe48 5b 6e 42 79 74 65 73 5d 29 20 29 3b 0a 20 20 20  [nBytes]) );.   
1fe49 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c   for(i=0; i<nCol
1fe4a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68  ; i++){.      ch
1fe4b 61 72 20 2a 7a 43 6f 6c 6c 20 3d 20 70 49 64 78  ar *zColl = pIdx
1fe4c 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20  ->azColl[i];.   
1fe4d 20 20 20 61 73 73 65 72 74 28 20 7a 43 6f 6c 6c     assert( zColl
1fe4e 20 29 3b 0a 20 20 20 20 20 20 70 4b 65 79 2d 3e   );.      pKey->
1fe4f 61 43 6f 6c 6c 5b 69 5d 20 3d 20 73 71 6c 69 74  aColl[i] = sqlit
1fe50 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28  e3LocateCollSeq(
1fe51 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 2c 20 2d  pParse, zColl, -
1fe52 31 29 3b 0a 20 20 20 20 20 20 70 4b 65 79 2d 3e  1);.      pKey->
1fe53 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20  aSortOrder[i] = 
1fe54 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72  pIdx->aSortOrder
1fe55 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  [i];.    }.    p
1fe56 4b 65 79 2d 3e 6e 46 69 65 6c 64 20 3d 20 6e 43  Key->nField = nC
1fe57 6f 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70  ol;.  }..  if( p
1fe58 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b 0a 20  Parse->nErr ){. 
1fe59 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1fe5a 28 64 62 2c 20 70 4b 65 79 29 3b 0a 20 20 20 20  (db, pKey);.    
1fe5b 70 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  pKey = 0;.  }.  
1fe5c 72 65 74 75 72 6e 20 70 4b 65 79 3b 0a 7d 0a 0a  return pKey;.}..
1fe5d 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1fe5e 45 6e 64 20 6f 66 20 62 75 69 6c 64 2e 63 20 2a  End of build.c *
1fe5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fe60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fe61 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1fe62 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1fe63 42 65 67 69 6e 20 66 69 6c 65 20 63 61 6c 6c 62  Begin file callb
1fe64 61 63 6b 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ack.c **********
1fe65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fe66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1fe67 2f 2a 0a 2a 2a 20 32 30 30 35 20 4d 61 79 20 32  /*.** 2005 May 2
1fe68 33 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  3 .**.** The aut
1fe69 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
1fe6a 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
1fe6b 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
1fe6c 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
1fe6d 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
1fe6e 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
1fe6f 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
1fe70 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
1fe71 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
1fe72 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
1fe73 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
1fe74 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
1fe75 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
1fe76 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
1fe77 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
1fe78 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
1fe79 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
1fe7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fe7b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fe7c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fe7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fe7e 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
1fe7f 65 20 63 6f 6e 74 61 69 6e 73 20 66 75 6e 63 74  e contains funct
1fe80 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 61 63 63  ions used to acc
1fe81 65 73 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  ess the internal
1fe82 20 68 61 73 68 20 74 61 62 6c 65 73 0a 2a 2a 20   hash tables.** 
1fe83 6f 66 20 75 73 65 72 20 64 65 66 69 6e 65 64 20  of user defined 
1fe84 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 63 6f  functions and co
1fe85 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
1fe86 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 63 61  s..**.** $Id: ca
1fe87 6c 6c 62 61 63 6b 2e 63 2c 76 20 31 2e 33 32 20  llback.c,v 1.32 
1fe88 32 30 30 38 2f 31 30 2f 31 30 20 31 37 3a 34 31  2008/10/10 17:41
1fe89 3a 32 39 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :29 drh Exp $.*/
1fe8a 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20  .../*.** Invoke 
1fe8b 74 68 65 20 27 63 6f 6c 6c 61 74 69 6f 6e 20 6e  the 'collation n
1fe8c 65 65 64 65 64 27 20 63 61 6c 6c 62 61 63 6b 20  eeded' callback 
1fe8d 74 6f 20 72 65 71 75 65 73 74 20 61 20 63 6f 6c  to request a col
1fe8e 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 0a  lation sequence.
1fe8f 2a 2a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ** in the databa
1fe90 73 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  se text encoding
1fe91 20 6f 66 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20   of name zName, 
1fe92 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a  length nName..**
1fe93 20 49 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f   If the collatio
1fe94 6e 20 73 65 71 75 65 6e 63 65 0a 2a 2f 0a 73 74  n sequence.*/.st
1fe95 61 74 69 63 20 76 6f 69 64 20 63 61 6c 6c 43 6f  atic void callCo
1fe96 6c 6c 4e 65 65 64 65 64 28 73 71 6c 69 74 65 33  llNeeded(sqlite3
1fe97 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   *db, const char
1fe98 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 6e 4e 61   *zName, int nNa
1fe99 6d 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21  me){.  assert( !
1fe9a 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20  db->xCollNeeded 
1fe9b 7c 7c 20 21 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65  || !db->xCollNee
1fe9c 64 65 64 31 36 20 29 3b 0a 20 20 69 66 28 20 6e  ded16 );.  if( n
1fe9d 4e 61 6d 65 3c 30 20 29 20 6e 4e 61 6d 65 20 3d  Name<0 ) nName =
1fe9e 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 28 64   sqlite3Strlen(d
1fe9f 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28  b, zName);.  if(
1fea0 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64   db->xCollNeeded
1fea1 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 45   ){.    char *zE
1fea2 78 74 65 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65  xternal = sqlite
1fea3 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 7a  3DbStrNDup(db, z
1fea4 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20  Name, nName);.  
1fea5 20 20 69 66 28 20 21 7a 45 78 74 65 72 6e 61 6c    if( !zExternal
1fea6 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 64   ) return;.    d
1fea7 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 28 64  b->xCollNeeded(d
1fea8 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72  b->pCollNeededAr
1fea9 67 2c 20 64 62 2c 20 28 69 6e 74 29 45 4e 43 28  g, db, (int)ENC(
1feaa 64 62 29 2c 20 7a 45 78 74 65 72 6e 61 6c 29 3b  db), zExternal);
1feab 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
1feac 65 65 28 64 62 2c 20 7a 45 78 74 65 72 6e 61 6c  ee(db, zExternal
1fead 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  );.  }.#ifndef S
1feae 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
1feaf 0a 20 20 69 66 28 20 64 62 2d 3e 78 43 6f 6c 6c  .  if( db->xColl
1feb0 4e 65 65 64 65 64 31 36 20 29 7b 0a 20 20 20 20  Needed16 ){.    
1feb1 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 45 78 74  char const *zExt
1feb2 65 72 6e 61 6c 3b 0a 20 20 20 20 73 71 6c 69 74  ernal;.    sqlit
1feb3 65 33 5f 76 61 6c 75 65 20 2a 70 54 6d 70 20 3d  e3_value *pTmp =
1feb4 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77   sqlite3ValueNew
1feb5 28 64 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  (db);.    sqlite
1feb6 33 56 61 6c 75 65 53 65 74 53 74 72 28 70 54 6d  3ValueSetStr(pTm
1feb7 70 2c 20 6e 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c  p, nName, zName,
1feb8 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51   SQLITE_UTF8, SQ
1feb9 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
1feba 20 20 7a 45 78 74 65 72 6e 61 6c 20 3d 20 73 71    zExternal = sq
1febb 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70  lite3ValueText(p
1febc 54 6d 70 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  Tmp, SQLITE_UTF1
1febd 36 4e 41 54 49 56 45 29 3b 0a 20 20 20 20 69 66  6NATIVE);.    if
1febe 28 20 7a 45 78 74 65 72 6e 61 6c 20 29 7b 0a 20  ( zExternal ){. 
1febf 20 20 20 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65       db->xCollNe
1fec0 65 64 65 64 31 36 28 64 62 2d 3e 70 43 6f 6c 6c  eded16(db->pColl
1fec1 4e 65 65 64 65 64 41 72 67 2c 20 64 62 2c 20 28  NeededArg, db, (
1fec2 69 6e 74 29 45 4e 43 28 64 62 29 2c 20 7a 45 78  int)ENC(db), zEx
1fec3 74 65 72 6e 61 6c 29 3b 0a 20 20 20 20 7d 0a 20  ternal);.    }. 
1fec4 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46     sqlite3ValueF
1fec5 72 65 65 28 70 54 6d 70 29 3b 0a 20 20 7d 0a 23  ree(pTmp);.  }.#
1fec6 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  endif.}../*.** T
1fec7 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
1fec8 61 6c 6c 65 64 20 69 66 20 74 68 65 20 63 6f 6c  alled if the col
1fec9 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 66  lation factory f
1feca 61 69 6c 73 20 74 6f 20 64 65 6c 69 76 65 72 20  ails to deliver 
1fecb 61 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 66  a.** collation f
1fecc 75 6e 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 62  unction in the b
1fecd 65 73 74 20 65 6e 63 6f 64 69 6e 67 20 62 75 74  est encoding but
1fece 20 74 68 65 72 65 20 6d 61 79 20 62 65 20 6f 74   there may be ot
1fecf 68 65 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20  her versions.** 
1fed0 6f 66 20 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f  of this collatio
1fed1 6e 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20  n function (for 
1fed2 6f 74 68 65 72 20 74 65 78 74 20 65 6e 63 6f 64  other text encod
1fed3 69 6e 67 73 29 20 61 76 61 69 6c 61 62 6c 65 2e  ings) available.
1fed4 20 55 73 65 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74   Use one.** of t
1fed5 68 65 73 65 20 69 6e 73 74 65 61 64 20 69 66 20  hese instead if 
1fed6 74 68 65 79 20 65 78 69 73 74 2e 20 41 76 6f 69  they exist. Avoi
1fed7 64 20 61 20 55 54 46 2d 38 20 3c 2d 3e 20 55 54  d a UTF-8 <-> UT
1fed8 46 2d 31 36 20 63 6f 6e 76 65 72 73 69 6f 6e 20  F-16 conversion 
1fed9 69 66 0a 2a 2a 20 70 6f 73 73 69 62 6c 65 2e 0a  if.** possible..
1feda 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 79  */.static int sy
1fedb 6e 74 68 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74  nthCollSeq(sqlit
1fedc 65 33 20 2a 64 62 2c 20 43 6f 6c 6c 53 65 71 20  e3 *db, CollSeq 
1fedd 2a 70 43 6f 6c 6c 29 7b 0a 20 20 43 6f 6c 6c 53  *pColl){.  CollS
1fede 65 71 20 2a 70 43 6f 6c 6c 32 3b 0a 20 20 63 68  eq *pColl2;.  ch
1fedf 61 72 20 2a 7a 20 3d 20 70 43 6f 6c 6c 2d 3e 7a  ar *z = pColl->z
1fee0 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 6e 20 3d 20  Name;.  int n = 
1fee1 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20 69 6e 74  strlen(z);.  int
1fee2 20 69 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   i;.  static con
1fee3 73 74 20 75 38 20 61 45 6e 63 5b 5d 20 3d 20 7b  st u8 aEnc[] = {
1fee4 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c   SQLITE_UTF16BE,
1fee5 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c   SQLITE_UTF16LE,
1fee6 20 53 51 4c 49 54 45 5f 55 54 46 38 20 7d 3b 0a   SQLITE_UTF8 };.
1fee7 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 33 3b 20    for(i=0; i<3; 
1fee8 69 2b 2b 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 32  i++){.    pColl2
1fee9 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f   = sqlite3FindCo
1feea 6c 6c 53 65 71 28 64 62 2c 20 61 45 6e 63 5b 69  llSeq(db, aEnc[i
1feeb 5d 2c 20 7a 2c 20 6e 2c 20 30 29 3b 0a 20 20 20  ], z, n, 0);.   
1feec 20 69 66 28 20 70 43 6f 6c 6c 32 2d 3e 78 43 6d   if( pColl2->xCm
1feed 70 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 65  p!=0 ){.      me
1feee 6d 63 70 79 28 70 43 6f 6c 6c 2c 20 70 43 6f 6c  mcpy(pColl, pCol
1feef 6c 32 2c 20 73 69 7a 65 6f 66 28 43 6f 6c 6c 53  l2, sizeof(CollS
1fef0 65 71 29 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c  eq));.      pCol
1fef1 6c 2d 3e 78 44 65 6c 20 3d 20 30 3b 20 20 20 20  l->xDel = 0;    
1fef2 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63       /* Do not c
1fef3 6f 70 79 20 74 68 65 20 64 65 73 74 72 75 63 74  opy the destruct
1fef4 6f 72 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75  or */.      retu
1fef5 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
1fef6 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
1fef7 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d   SQLITE_ERROR;.}
1fef8 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
1fef9 63 74 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73  ction is respons
1fefa 69 62 6c 65 20 66 6f 72 20 69 6e 76 6f 6b 69 6e  ible for invokin
1fefb 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  g the collation 
1fefc 66 61 63 74 6f 72 79 20 63 61 6c 6c 62 61 63 6b  factory callback
1fefd 0a 2a 2a 20 6f 72 20 73 75 62 73 74 69 74 75 74  .** or substitut
1fefe 69 6e 67 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20  ing a collation 
1feff 73 65 71 75 65 6e 63 65 20 6f 66 20 61 20 64 69  sequence of a di
1ff00 66 66 65 72 65 6e 74 20 65 6e 63 6f 64 69 6e 67  fferent encoding
1ff01 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 72 65 71   when the.** req
1ff02 75 65 73 74 65 64 20 63 6f 6c 6c 61 74 69 6f 6e  uested collation
1ff03 20 73 65 71 75 65 6e 63 65 20 69 73 20 6e 6f 74   sequence is not
1ff04 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68   available in th
1ff05 65 20 64 61 74 61 62 61 73 65 20 6e 61 74 69 76  e database nativ
1ff06 65 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a  e.** encoding..*
1ff07 2a 20 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e  * .** If it is n
1ff08 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 43  ot NULL, then pC
1ff09 6f 6c 6c 20 6d 75 73 74 20 70 6f 69 6e 74 20 74  oll must point t
1ff0a 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e  o the database n
1ff0b 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 20 0a  ative encoding .
1ff0c 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ** collation seq
1ff0d 75 65 6e 63 65 20 77 69 74 68 20 6e 61 6d 65 20  uence with name 
1ff0e 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 4e  zName, length nN
1ff0f 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  ame..**.** The r
1ff10 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 65  eturn value is e
1ff11 69 74 68 65 72 20 74 68 65 20 63 6f 6c 6c 61 74  ither the collat
1ff12 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 74 6f 20  ion sequence to 
1ff13 62 65 20 75 73 65 64 20 69 6e 20 64 61 74 61 62  be used in datab
1ff14 61 73 65 0a 2a 2a 20 64 62 20 66 6f 72 20 63 6f  ase.** db for co
1ff15 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 6e 61 6d  llation type nam
1ff16 65 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74 68 20  e zName, length 
1ff17 6e 4e 61 6d 65 2c 20 6f 72 20 4e 55 4c 4c 2c 20  nName, or NULL, 
1ff18 69 66 20 6e 6f 20 63 6f 6c 6c 61 74 69 6f 6e 0a  if no collation.
1ff19 2a 2a 20 73 65 71 75 65 6e 63 65 20 63 61 6e 20  ** sequence can 
1ff1a 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c  be found..*/.SQL
1ff1b 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c  ITE_PRIVATE Coll
1ff1c 53 65 71 20 2a 73 71 6c 69 74 65 33 47 65 74 43  Seq *sqlite3GetC
1ff1d 6f 6c 6c 53 65 71 28 0a 20 20 73 71 6c 69 74 65  ollSeq(.  sqlite
1ff1e 33 2a 20 64 62 2c 20 0a 20 20 43 6f 6c 6c 53 65  3* db, .  CollSe
1ff1f 71 20 2a 70 43 6f 6c 6c 2c 20 0a 20 20 63 6f 6e  q *pColl, .  con
1ff20 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
1ff21 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 0a 29 7b 0a  .  int nName.){.
1ff22 20 20 43 6f 6c 6c 53 65 71 20 2a 70 3b 0a 0a 20    CollSeq *p;.. 
1ff23 20 70 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 69 66   p = pColl;.  if
1ff24 28 20 21 70 20 29 7b 0a 20 20 20 20 70 20 3d 20  ( !p ){.    p = 
1ff25 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53  sqlite3FindCollS
1ff26 65 71 28 64 62 2c 20 45 4e 43 28 64 62 29 2c 20  eq(db, ENC(db), 
1ff27 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 30 29  zName, nName, 0)
1ff28 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 20 7c  ;.  }.  if( !p |
1ff29 7c 20 21 70 2d 3e 78 43 6d 70 20 29 7b 0a 20 20  | !p->xCmp ){.  
1ff2a 20 20 2f 2a 20 4e 6f 20 63 6f 6c 6c 61 74 69 6f    /* No collatio
1ff2b 6e 20 73 65 71 75 65 6e 63 65 20 6f 66 20 74 68  n sequence of th
1ff2c 69 73 20 74 79 70 65 20 66 6f 72 20 74 68 69 73  is type for this
1ff2d 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 72 65 67   encoding is reg
1ff2e 69 73 74 65 72 65 64 2e 0a 20 20 20 20 2a 2a 20  istered..    ** 
1ff2f 43 61 6c 6c 20 74 68 65 20 63 6f 6c 6c 61 74 69  Call the collati
1ff30 6f 6e 20 66 61 63 74 6f 72 79 20 74 6f 20 73 65  on factory to se
1ff31 65 20 69 66 20 69 74 20 63 61 6e 20 73 75 70 70  e if it can supp
1ff32 6c 79 20 75 73 20 77 69 74 68 20 6f 6e 65 2e 0a  ly us with one..
1ff33 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 6c 6c 43      */.    callC
1ff34 6f 6c 6c 4e 65 65 64 65 64 28 64 62 2c 20 7a 4e  ollNeeded(db, zN
1ff35 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20  ame, nName);.   
1ff36 20 70 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64   p = sqlite3Find
1ff37 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28  CollSeq(db, ENC(
1ff38 64 62 29 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  db), zName, nNam
1ff39 65 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28  e, 0);.  }.  if(
1ff3a 20 70 20 26 26 20 21 70 2d 3e 78 43 6d 70 20 26   p && !p->xCmp &
1ff3b 26 20 73 79 6e 74 68 43 6f 6c 6c 53 65 71 28 64  & synthCollSeq(d
1ff3c 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 70 20 3d  b, p) ){.    p =
1ff3d 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74   0;.  }.  assert
1ff3e 28 20 21 70 20 7c 7c 20 70 2d 3e 78 43 6d 70 20  ( !p || p->xCmp 
1ff3f 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d  );.  return p;.}
1ff40 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
1ff41 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 6f  tine is called o
1ff42 6e 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  n a collation se
1ff43 71 75 65 6e 63 65 20 62 65 66 6f 72 65 20 69 74  quence before it
1ff44 20 69 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63   is used to.** c
1ff45 68 65 63 6b 20 74 68 61 74 20 69 74 20 69 73 20  heck that it is 
1ff46 64 65 66 69 6e 65 64 2e 20 41 6e 20 75 6e 64 65  defined. An unde
1ff47 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20  fined collation 
1ff48 73 65 71 75 65 6e 63 65 20 65 78 69 73 74 73 20  sequence exists 
1ff49 77 68 65 6e 0a 2a 2a 20 61 20 64 61 74 61 62 61  when.** a databa
1ff4a 73 65 20 69 73 20 6c 6f 61 64 65 64 20 74 68 61  se is loaded tha
1ff4b 74 20 63 6f 6e 74 61 69 6e 73 20 72 65 66 65 72  t contains refer
1ff4c 65 6e 63 65 73 20 74 6f 20 63 6f 6c 6c 61 74 69  ences to collati
1ff4d 6f 6e 20 73 65 71 75 65 6e 63 65 73 0a 2a 2a 20  on sequences.** 
1ff4e 74 68 61 74 20 68 61 76 65 20 6e 6f 74 20 62 65  that have not be
1ff4f 65 6e 20 64 65 66 69 6e 65 64 20 62 79 20 73 71  en defined by sq
1ff50 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
1ff51 6c 61 74 69 6f 6e 28 29 20 65 74 63 2e 0a 2a 2a  lation() etc..**
1ff52 0a 2a 2a 20 49 66 20 72 65 71 75 69 72 65 64 2c  .** If required,
1ff53 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61   this routine ca
1ff54 6c 6c 73 20 74 68 65 20 27 63 6f 6c 6c 61 74 69  lls the 'collati
1ff55 6f 6e 20 6e 65 65 64 65 64 27 20 63 61 6c 6c 62  on needed' callb
1ff56 61 63 6b 20 74 6f 0a 2a 2a 20 72 65 71 75 65 73  ack to.** reques
1ff57 74 20 61 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f  t a definition o
1ff58 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20  f the collating 
1ff59 73 65 71 75 65 6e 63 65 2e 20 49 66 20 74 68 69  sequence. If thi
1ff5a 73 20 64 6f 65 73 6e 27 74 20 77 6f 72 6b 2c 20  s doesn't work, 
1ff5b 0a 2a 2a 20 61 6e 20 65 71 75 69 76 61 6c 65 6e  .** an equivalen
1ff5c 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  t collating sequ
1ff5d 65 6e 63 65 20 74 68 61 74 20 75 73 65 73 20 61  ence that uses a
1ff5e 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 64   text encoding d
1ff5f 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d  ifferent.** from
1ff60 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
1ff61 73 65 20 69 73 20 73 75 62 73 74 69 74 75 74 65  se is substitute
1ff62 64 2c 20 69 66 20 6f 6e 65 20 69 73 20 61 76 61  d, if one is ava
1ff63 69 6c 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54  ilable..*/.SQLIT
1ff64 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1ff65 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c 6c 53 65  lite3CheckCollSe
1ff66 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  q(Parse *pParse,
1ff67 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 29   CollSeq *pColl)
1ff68 7b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b  {.  if( pColl ){
1ff69 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
1ff6a 2a 7a 4e 61 6d 65 20 3d 20 70 43 6f 6c 6c 2d 3e  *zName = pColl->
1ff6b 7a 4e 61 6d 65 3b 0a 20 20 20 20 43 6f 6c 6c 53  zName;.    CollS
1ff6c 65 71 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 47  eq *p = sqlite3G
1ff6d 65 74 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  etCollSeq(pParse
1ff6e 2d 3e 64 62 2c 20 70 43 6f 6c 6c 2c 20 7a 4e 61  ->db, pColl, zNa
1ff6f 6d 65 2c 20 2d 31 29 3b 0a 20 20 20 20 69 66 28  me, -1);.    if(
1ff70 20 21 70 20 29 7b 0a 20 20 20 20 20 20 69 66 28   !p ){.      if(
1ff71 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30   pParse->nErr==0
1ff72 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1ff73 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1ff74 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c  se, "no such col
1ff75 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 3a  lation sequence:
1ff76 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20   %s", zName);.  
1ff77 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 72      }.      pPar
1ff78 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20  se->nErr++;.    
1ff79 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1ff7a 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20  ERROR;.    }.   
1ff7b 20 61 73 73 65 72 74 28 20 70 3d 3d 70 43 6f 6c   assert( p==pCol
1ff7c 6c 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  l );.  }.  retur
1ff7d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
1ff7e 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 61  ../*.** Locate a
1ff7f 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 6e 74  nd return an ent
1ff80 72 79 20 66 72 6f 6d 20 74 68 65 20 64 62 2e 61  ry from the db.a
1ff81 43 6f 6c 6c 53 65 71 20 68 61 73 68 20 74 61 62  CollSeq hash tab
1ff82 6c 65 2e 20 49 66 20 74 68 65 20 65 6e 74 72 79  le. If the entry
1ff83 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62 79  .** specified by
1ff84 20 7a 4e 61 6d 65 20 61 6e 64 20 6e 4e 61 6d 65   zName and nName
1ff85 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 20 61 6e   is not found an
1ff86 64 20 70 61 72 61 6d 65 74 65 72 20 27 63 72 65  d parameter 'cre
1ff87 61 74 65 27 20 69 73 0a 2a 2a 20 74 72 75 65 2c  ate' is.** true,
1ff88 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 20 6e   then create a n
1ff89 65 77 20 65 6e 74 72 79 2e 20 4f 74 68 65 72 77  ew entry. Otherw
1ff8a 69 73 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e  ise return NULL.
1ff8b 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 70 6f 69 6e  .**.** Each poin
1ff8c 74 65 72 20 73 74 6f 72 65 64 20 69 6e 20 74 68  ter stored in th
1ff8d 65 20 73 71 6c 69 74 65 33 2e 61 43 6f 6c 6c 53  e sqlite3.aCollS
1ff8e 65 71 20 68 61 73 68 20 74 61 62 6c 65 20 63 6f  eq hash table co
1ff8f 6e 74 61 69 6e 73 20 61 6e 0a 2a 2a 20 61 72 72  ntains an.** arr
1ff90 61 79 20 6f 66 20 74 68 72 65 65 20 43 6f 6c 6c  ay of three Coll
1ff91 53 65 71 20 73 74 72 75 63 74 75 72 65 73 2e 20  Seq structures. 
1ff92 54 68 65 20 66 69 72 73 74 20 69 73 20 74 68 65  The first is the
1ff93 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
1ff94 6e 63 65 0a 2a 2a 20 70 72 65 66 66 65 72 72 65  nce.** prefferre
1ff95 64 20 66 6f 72 20 55 54 46 2d 38 2c 20 74 68 65  d for UTF-8, the
1ff96 20 73 65 63 6f 6e 64 20 55 54 46 2d 31 36 6c 65   second UTF-16le
1ff97 2c 20 61 6e 64 20 74 68 65 20 74 68 69 72 64 20  , and the third 
1ff98 55 54 46 2d 31 36 62 65 2e 0a 2a 2a 0a 2a 2a 20  UTF-16be..**.** 
1ff99 53 74 6f 72 65 64 20 69 6d 6d 65 64 69 61 74 65  Stored immediate
1ff9a 6c 79 20 61 66 74 65 72 20 74 68 65 20 74 68 72  ly after the thr
1ff9b 65 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ee collation seq
1ff9c 75 65 6e 63 65 73 20 69 73 20 61 20 63 6f 70 79  uences is a copy
1ff9d 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 6c 61   of.** the colla
1ff9e 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61  tion sequence na
1ff9f 6d 65 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  me. A pointer to
1ffa0 20 74 68 69 73 20 73 74 72 69 6e 67 20 69 73 20   this string is 
1ffa1 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 65 61 63  stored in.** eac
1ffa2 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  h collation sequ
1ffa3 65 6e 63 65 20 73 74 72 75 63 74 75 72 65 2e 0a  ence structure..
1ffa4 2a 2f 0a 73 74 61 74 69 63 20 43 6f 6c 6c 53 65  */.static CollSe
1ffa5 71 20 2a 66 69 6e 64 43 6f 6c 6c 53 65 71 45 6e  q *findCollSeqEn
1ffa6 74 72 79 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  try(.  sqlite3 *
1ffa7 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  db,.  const char
1ffa8 20 2a 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e   *zName,.  int n
1ffa9 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 63 72 65 61  Name,.  int crea
1ffaa 74 65 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20  te.){.  CollSeq 
1ffab 2a 70 43 6f 6c 6c 3b 0a 20 20 69 66 28 20 6e 4e  *pColl;.  if( nN
1ffac 61 6d 65 3c 30 20 29 20 6e 4e 61 6d 65 20 3d 20  ame<0 ) nName = 
1ffad 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 28 64 62  sqlite3Strlen(db
1ffae 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 70 43 6f 6c  , zName);.  pCol
1ffaf 6c 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46  l = sqlite3HashF
1ffb0 69 6e 64 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65  ind(&db->aCollSe
1ffb1 71 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  q, zName, nName)
1ffb2 3b 0a 0a 20 20 69 66 28 20 30 3d 3d 70 43 6f 6c  ;..  if( 0==pCol
1ffb3 6c 20 26 26 20 63 72 65 61 74 65 20 29 7b 0a 20  l && create ){. 
1ffb4 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74     pColl = sqlit
1ffb5 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
1ffb6 62 2c 20 33 2a 73 69 7a 65 6f 66 28 2a 70 43 6f  b, 3*sizeof(*pCo
1ffb7 6c 6c 29 20 2b 20 6e 4e 61 6d 65 20 2b 20 31 20  ll) + nName + 1 
1ffb8 29 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c  );.    if( pColl
1ffb9 20 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65   ){.      CollSe
1ffba 71 20 2a 70 44 65 6c 20 3d 20 30 3b 0a 20 20 20  q *pDel = 0;.   
1ffbb 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d     pColl[0].zNam
1ffbc 65 20 3d 20 28 63 68 61 72 2a 29 26 70 43 6f 6c  e = (char*)&pCol
1ffbd 6c 5b 33 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c  l[3];.      pCol
1ffbe 6c 5b 30 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54  l[0].enc = SQLIT
1ffbf 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 70 43  E_UTF8;.      pC
1ffc0 6f 6c 6c 5b 31 5d 2e 7a 4e 61 6d 65 20 3d 20 28  oll[1].zName = (
1ffc1 63 68 61 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d 3b  char*)&pColl[3];
1ffc2 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 31 5d 2e  .      pColl[1].
1ffc3 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46  enc = SQLITE_UTF
1ffc4 31 36 4c 45 3b 0a 20 20 20 20 20 20 70 43 6f 6c  16LE;.      pCol
1ffc5 6c 5b 32 5d 2e 7a 4e 61 6d 65 20 3d 20 28 63 68  l[2].zName = (ch
1ffc6 61 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d 3b 0a 20  ar*)&pColl[3];. 
1ffc7 20 20 20 20 20 70 43 6f 6c 6c 5b 32 5d 2e 65 6e       pColl[2].en
1ffc8 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36  c = SQLITE_UTF16
1ffc9 42 45 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79  BE;.      memcpy
1ffca 28 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 2c  (pColl[0].zName,
1ffcb 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a   zName, nName);.
1ffcc 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e 7a        pColl[0].z
1ffcd 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d 20 30 3b  Name[nName] = 0;
1ffce 0a 20 20 20 20 20 20 70 44 65 6c 20 3d 20 73 71  .      pDel = sq
1ffcf 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
1ffd0 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 2c 20 70  &db->aCollSeq, p
1ffd1 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 6e  Coll[0].zName, n
1ffd2 4e 61 6d 65 2c 20 70 43 6f 6c 6c 29 3b 0a 0a 20  Name, pColl);.. 
1ffd3 20 20 20 20 20 2f 2a 20 49 66 20 61 20 6d 61 6c       /* If a mal
1ffd4 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20 6f 63  loc() failure oc
1ffd5 63 75 72 65 64 20 69 6e 20 73 71 6c 69 74 65 33  cured in sqlite3
1ffd6 48 61 73 68 49 6e 73 65 72 74 28 29 2c 20 69 74  HashInsert(), it
1ffd7 20 77 69 6c 6c 20 0a 20 20 20 20 20 20 2a 2a 20   will .      ** 
1ffd8 72 65 74 75 72 6e 20 74 68 65 20 70 43 6f 6c 6c  return the pColl
1ffd9 20 70 6f 69 6e 74 65 72 20 74 6f 20 62 65 20 64   pointer to be d
1ffda 65 6c 65 74 65 64 20 28 62 65 63 61 75 73 65 20  eleted (because 
1ffdb 69 74 20 77 61 73 6e 27 74 20 61 64 64 65 64 0a  it wasn't added.
1ffdc 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20        ** to the 
1ffdd 68 61 73 68 20 74 61 62 6c 65 29 2e 0a 20 20 20  hash table)..   
1ffde 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65     */.      asse
1ffdf 72 74 28 20 70 44 65 6c 3d 3d 30 20 7c 7c 20 70  rt( pDel==0 || p
1ffe0 44 65 6c 3d 3d 70 43 6f 6c 6c 20 29 3b 0a 20 20  Del==pColl );.  
1ffe1 20 20 20 20 69 66 28 20 70 44 65 6c 21 3d 30 20      if( pDel!=0 
1ffe2 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6d  ){.        db->m
1ffe3 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
1ffe4 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1ffe5 44 62 46 72 65 65 28 64 62 2c 20 70 44 65 6c 29  DbFree(db, pDel)
1ffe6 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20  ;.        pColl 
1ffe7 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
1ffe8 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1ffe9 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  pColl;.}../*.** 
1ffea 50 61 72 61 6d 65 74 65 72 20 7a 4e 61 6d 65 20  Parameter zName 
1ffeb 70 6f 69 6e 74 73 20 74 6f 20 61 20 55 54 46 2d  points to a UTF-
1ffec 38 20 65 6e 63 6f 64 65 64 20 73 74 72 69 6e 67  8 encoded string
1ffed 20 6e 4e 61 6d 65 20 62 79 74 65 73 20 6c 6f 6e   nName bytes lon
1ffee 67 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  g..** Return the
1ffef 20 43 6f 6c 6c 53 65 71 2a 20 70 6f 69 6e 74 65   CollSeq* pointe
1fff0 72 20 66 6f 72 20 74 68 65 20 63 6f 6c 6c 61 74  r for the collat
1fff1 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d  ion sequence nam
1fff2 65 64 20 7a 4e 61 6d 65 0a 2a 2a 20 66 6f 72 20  ed zName.** for 
1fff3 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 27 65 6e  the encoding 'en
1fff4 63 27 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  c' from the data
1fff5 62 61 73 65 20 27 64 62 27 2e 0a 2a 2a 0a 2a 2a  base 'db'..**.**
1fff6 20 49 66 20 74 68 65 20 65 6e 74 72 79 20 73 70   If the entry sp
1fff7 65 63 69 66 69 65 64 20 69 73 20 6e 6f 74 20 66  ecified is not f
1fff8 6f 75 6e 64 20 61 6e 64 20 27 63 72 65 61 74 65  ound and 'create
1fff9 27 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20  ' is true, then 
1fffa 63 72 65 61 74 65 20 61 0a 2a 2a 20 6e 65 77 20  create a.** new 
1fffb 65 6e 74 72 79 2e 20 20 4f 74 68 65 72 77 69 73  entry.  Otherwis
1fffc 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a  e return NULL..*
1fffd 2a 0a 2a 2a 20 41 20 73 65 70 61 72 61 74 65 20  *.** A separate 
1fffe 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 33  function sqlite3
1ffff 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 29 20  LocateCollSeq() 
20000 69 73 20 61 20 77 72 61 70 70 65 72 20 61 72 6f  is a wrapper aro
20001 75 6e 64 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74  und.** this rout
20002 69 6e 65 2e 20 20 73 71 6c 69 74 65 33 4c 6f 63  ine.  sqlite3Loc
20003 61 74 65 43 6f 6c 6c 53 65 71 28 29 20 69 6e 76  ateCollSeq() inv
20004 6f 6b 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69  okes the collati
20005 6f 6e 20 66 61 63 74 6f 72 79 0a 2a 2a 20 69 66  on factory.** if
20006 20 6e 65 63 65 73 73 61 72 79 20 61 6e 64 20 67   necessary and g
20007 65 6e 65 72 61 74 65 73 20 61 6e 20 65 72 72 6f  enerates an erro
20008 72 20 6d 65 73 73 61 67 65 20 69 66 20 74 68 65  r message if the
20009 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
2000a 6e 63 65 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65  nce.** cannot be
2000b 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54   found..*/.SQLIT
2000c 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65  E_PRIVATE CollSe
2000d 71 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 43 6f  q *sqlite3FindCo
2000e 6c 6c 53 65 71 28 0a 20 20 73 71 6c 69 74 65 33  llSeq(.  sqlite3
2000f 20 2a 64 62 2c 0a 20 20 75 38 20 65 6e 63 2c 0a   *db,.  u8 enc,.
20010 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
20011 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 4e 61 6d 65  ame,.  int nName
20012 2c 0a 20 20 69 6e 74 20 63 72 65 61 74 65 0a 29  ,.  int create.)
20013 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  {.  CollSeq *pCo
20014 6c 6c 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20  ll;.  if( zName 
20015 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 66  ){.    pColl = f
20016 69 6e 64 43 6f 6c 6c 53 65 71 45 6e 74 72 79 28  indCollSeqEntry(
20017 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  db, zName, nName
20018 2c 20 63 72 65 61 74 65 29 3b 0a 20 20 7d 65 6c  , create);.  }el
20019 73 65 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20  se{.    pColl = 
2001a 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20  db->pDfltColl;. 
2001b 20 7d 0a 20 20 61 73 73 65 72 74 28 20 53 51 4c   }.  assert( SQL
2001c 49 54 45 5f 55 54 46 38 3d 3d 31 20 26 26 20 53  ITE_UTF8==1 && S
2001d 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 3d 3d 32  QLITE_UTF16LE==2
2001e 20 26 26 20 53 51 4c 49 54 45 5f 55 54 46 31 36   && SQLITE_UTF16
2001f 42 45 3d 3d 33 20 29 3b 0a 20 20 61 73 73 65 72  BE==3 );.  asser
20020 74 28 20 65 6e 63 3e 3d 53 51 4c 49 54 45 5f 55  t( enc>=SQLITE_U
20021 54 46 38 20 26 26 20 65 6e 63 3c 3d 53 51 4c 49  TF8 && enc<=SQLI
20022 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20  TE_UTF16BE );.  
20023 69 66 28 20 70 43 6f 6c 6c 20 29 20 70 43 6f 6c  if( pColl ) pCol
20024 6c 20 2b 3d 20 65 6e 63 2d 31 3b 0a 20 20 72 65  l += enc-1;.  re
20025 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f  turn pColl;.}../
20026 2a 20 44 75 72 69 6e 67 20 74 68 65 20 73 65 61  * During the sea
20027 72 63 68 20 66 6f 72 20 74 68 65 20 62 65 73 74  rch for the best
20028 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69   function defini
20029 74 69 6f 6e 2c 20 74 68 69 73 20 70 72 6f 63 65  tion, this proce
2002a 64 75 72 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65  dure.** is calle
2002b 64 20 74 6f 20 74 65 73 74 20 68 6f 77 20 77 65  d to test how we
2002c 6c 6c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  ll the function 
2002d 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69  passed as the fi
2002e 72 73 74 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  rst argument.** 
2002f 6d 61 74 63 68 65 73 20 74 68 65 20 72 65 71 75  matches the requ
20030 65 73 74 20 66 6f 72 20 61 20 66 75 6e 63 74 69  est for a functi
20031 6f 6e 20 77 69 74 68 20 6e 41 72 67 20 61 72 67  on with nArg arg
20032 75 6d 65 6e 74 73 20 69 6e 20 61 20 73 79 73 74  uments in a syst
20033 65 6d 0a 2a 2a 20 74 68 61 74 20 75 73 65 73 20  em.** that uses 
20034 65 6e 63 6f 64 69 6e 67 20 65 6e 63 2e 20 54 68  encoding enc. Th
20035 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
20036 20 69 6e 64 69 63 61 74 65 73 20 68 6f 77 20 77   indicates how w
20037 65 6c 6c 20 74 68 65 0a 2a 2a 20 72 65 71 75 65  ell the.** reque
20038 73 74 20 69 73 20 6d 61 74 63 68 65 64 2e 20 41  st is matched. A
20039 20 68 69 67 68 65 72 20 76 61 6c 75 65 20 69 6e   higher value in
2003a 64 69 63 61 74 65 73 20 61 20 62 65 74 74 65 72  dicates a better
2003b 20 6d 61 74 63 68 2e 0a 2a 2a 0a 2a 2a 20 54 68   match..**.** Th
2003c 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65  e returned value
2003d 20 69 73 20 61 6c 77 61 79 73 20 62 65 74 77 65   is always betwe
2003e 65 6e 20 31 20 61 6e 64 20 36 2c 20 61 73 20 66  en 1 and 6, as f
2003f 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 31 3a  ollows:.**.** 1:
20040 20 41 20 76 61 72 69 61 62 6c 65 20 61 72 67 75   A variable argu
20041 6d 65 6e 74 73 20 66 75 6e 63 74 69 6f 6e 20 74  ments function t
20042 68 61 74 20 70 72 65 66 65 72 73 20 55 54 46 2d  hat prefers UTF-
20043 38 20 77 68 65 6e 20 61 20 55 54 46 2d 31 36 0a  8 when a UTF-16.
20044 2a 2a 20 20 20 20 65 6e 63 6f 64 69 6e 67 20 69  **    encoding i
20045 73 20 72 65 71 75 65 73 74 65 64 2c 20 6f 72 20  s requested, or 
20046 76 69 63 65 20 76 65 72 73 61 2e 0a 2a 2a 20 32  vice versa..** 2
20047 3a 20 41 20 76 61 72 69 61 62 6c 65 20 61 72 67  : A variable arg
20048 75 6d 65 6e 74 73 20 66 75 6e 63 74 69 6f 6e 20  uments function 
20049 74 68 61 74 20 75 73 65 73 20 55 54 46 2d 31 36  that uses UTF-16
2004a 42 45 20 77 68 65 6e 20 55 54 46 2d 31 36 4c 45  BE when UTF-16LE
2004b 20 69 73 0a 2a 2a 20 20 20 20 72 65 71 75 65 73   is.**    reques
2004c 74 65 64 2c 20 6f 72 20 76 69 63 65 20 76 65 72  ted, or vice ver
2004d 73 61 2e 0a 2a 2a 20 33 3a 20 41 20 76 61 72 69  sa..** 3: A vari
2004e 61 62 6c 65 20 61 72 67 75 6d 65 6e 74 73 20 66  able arguments f
2004f 75 6e 63 74 69 6f 6e 20 75 73 69 6e 67 20 74 68  unction using th
20050 65 20 73 61 6d 65 20 74 65 78 74 20 65 6e 63 6f  e same text enco
20051 64 69 6e 67 2e 0a 2a 2a 20 34 3a 20 41 20 66 75  ding..** 4: A fu
20052 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20  nction with the 
20053 65 78 61 63 74 20 6e 75 6d 62 65 72 20 6f 66 20  exact number of 
20054 61 72 67 75 6d 65 6e 74 73 20 72 65 71 75 65 73  arguments reques
20055 74 65 64 20 74 68 61 74 0a 2a 2a 20 20 20 20 70  ted that.**    p
20056 72 65 66 65 72 73 20 55 54 46 2d 38 20 77 68 65  refers UTF-8 whe
20057 6e 20 61 20 55 54 46 2d 31 36 20 65 6e 63 6f 64  n a UTF-16 encod
20058 69 6e 67 20 69 73 20 72 65 71 75 65 73 74 65 64  ing is requested
20059 2c 20 6f 72 20 76 69 63 65 20 76 65 72 73 61 2e  , or vice versa.
2005a 0a 2a 2a 20 35 3a 20 41 20 66 75 6e 63 74 69 6f  .** 5: A functio
2005b 6e 20 77 69 74 68 20 74 68 65 20 65 78 61 63 74  n with the exact
2005c 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   number of argum
2005d 65 6e 74 73 20 72 65 71 75 65 73 74 65 64 20 74  ents requested t
2005e 68 61 74 0a 2a 2a 20 20 20 20 70 72 65 66 65 72  hat.**    prefer
2005f 73 20 55 54 46 2d 31 36 4c 45 20 77 68 65 6e 20  s UTF-16LE when 
20060 55 54 46 2d 31 36 42 45 20 69 73 20 72 65 71 75  UTF-16BE is requ
20061 65 73 74 65 64 2c 20 6f 72 20 76 69 63 65 20 76  ested, or vice v
20062 65 72 73 61 2e 0a 2a 2a 20 36 3a 20 41 6e 20 65  ersa..** 6: An e
20063 78 61 63 74 20 6d 61 74 63 68 2e 0a 2a 2a 0a 2a  xact match..**.*
20064 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 61 74  /.static int mat
20065 63 68 51 75 61 6c 69 74 79 28 46 75 6e 63 44 65  chQuality(FuncDe
20066 66 20 2a 70 2c 20 69 6e 74 20 6e 41 72 67 2c 20  f *p, int nArg, 
20067 75 38 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20 6d  u8 enc){.  int m
20068 61 74 63 68 20 3d 20 30 3b 0a 20 20 69 66 28 20  atch = 0;.  if( 
20069 70 2d 3e 6e 41 72 67 3d 3d 2d 31 20 7c 7c 20 70  p->nArg==-1 || p
2006a 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 20 7c 7c 20  ->nArg==nArg || 
2006b 6e 41 72 67 3d 3d 2d 31 20 29 7b 0a 20 20 20 20  nArg==-1 ){.    
2006c 6d 61 74 63 68 20 3d 20 31 3b 0a 20 20 20 20 69  match = 1;.    i
2006d 66 28 20 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67  f( p->nArg==nArg
2006e 20 7c 7c 20 6e 41 72 67 3d 3d 2d 31 20 29 7b 0a   || nArg==-1 ){.
2006f 20 20 20 20 20 20 6d 61 74 63 68 20 3d 20 34 3b        match = 4;
20070 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65  .    }.    if( e
20071 6e 63 3d 3d 70 2d 3e 69 50 72 65 66 45 6e 63 20  nc==p->iPrefEnc 
20072 29 7b 0a 20 20 20 20 20 20 6d 61 74 63 68 20 2b  ){.      match +
20073 3d 20 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65  = 2;.    }.    e
20074 6c 73 65 20 69 66 28 20 28 65 6e 63 3d 3d 53 51  lse if( (enc==SQ
20075 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 26 26 20  LITE_UTF16LE && 
20076 70 2d 3e 69 50 72 65 66 45 6e 63 3d 3d 53 51 4c  p->iPrefEnc==SQL
20077 49 54 45 5f 55 54 46 31 36 42 45 29 20 7c 7c 0a  ITE_UTF16BE) ||.
20078 20 20 20 20 20 20 20 20 20 20 20 20 20 28 65 6e               (en
20079 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42  c==SQLITE_UTF16B
2007a 45 20 26 26 20 70 2d 3e 69 50 72 65 66 45 6e 63  E && p->iPrefEnc
2007b 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  ==SQLITE_UTF16LE
2007c 29 20 29 7b 0a 20 20 20 20 20 20 6d 61 74 63 68  ) ){.      match
2007d 20 2b 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d   += 1;.    }.  }
2007e 0a 20 20 72 65 74 75 72 6e 20 6d 61 74 63 68 3b  .  return match;
2007f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63 68  .}../*.** Search
20080 20 61 20 46 75 6e 63 44 65 66 48 61 73 68 20 66   a FuncDefHash f
20081 6f 72 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69  or a function wi
20082 74 68 20 74 68 65 20 67 69 76 65 6e 20 6e 61 6d  th the given nam
20083 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20  e.  Return.** a 
20084 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d  pointer to the m
20085 61 74 63 68 69 6e 67 20 46 75 6e 63 44 65 66 20  atching FuncDef 
20086 69 66 20 66 6f 75 6e 64 2c 20 6f 72 20 30 20 69  if found, or 0 i
20087 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61  f there is no ma
20088 74 63 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 46  tch..*/.static F
20089 75 6e 63 44 65 66 20 2a 66 75 6e 63 74 69 6f 6e  uncDef *function
2008a 53 65 61 72 63 68 28 0a 20 20 46 75 6e 63 44 65  Search(.  FuncDe
2008b 66 48 61 73 68 20 2a 70 48 61 73 68 2c 20 20 2f  fHash *pHash,  /
2008c 2a 20 48 61 73 68 20 74 61 62 6c 65 20 74 6f 20  * Hash table to 
2008d 73 65 61 72 63 68 20 2a 2f 0a 20 20 69 6e 74 20  search */.  int 
2008e 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h,              
2008f 20 2f 2a 20 48 61 73 68 20 6f 66 20 74 68 65 20   /* Hash of the 
20090 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  name */.  const 
20091 63 68 61 72 20 2a 7a 46 75 6e 63 2c 20 20 20 2f  char *zFunc,   /
20092 2a 20 4e 61 6d 65 20 6f 66 20 66 75 6e 63 74 69  * Name of functi
20093 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6e  on */.  int nFun
20094 63 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  c            /* 
20095 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
20096 69 6e 20 7a 46 75 6e 63 20 2a 2f 0a 29 7b 0a 20  in zFunc */.){. 
20097 20 46 75 6e 63 44 65 66 20 2a 70 3b 0a 20 20 66   FuncDef *p;.  f
20098 6f 72 28 70 3d 70 48 61 73 68 2d 3e 61 5b 68 5d  or(p=pHash->a[h]
20099 3b 20 70 3b 20 70 3d 70 2d 3e 70 48 61 73 68 29  ; p; p=p->pHash)
2009a 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
2009b 33 53 74 72 4e 49 43 6d 70 28 70 2d 3e 7a 4e 61  3StrNICmp(p->zNa
2009c 6d 65 2c 20 7a 46 75 6e 63 2c 20 6e 46 75 6e 63  me, zFunc, nFunc
2009d 29 3d 3d 30 20 26 26 20 70 2d 3e 7a 4e 61 6d 65  )==0 && p->zName
2009e 5b 6e 46 75 6e 63 5d 3d 3d 30 20 29 7b 0a 20 20  [nFunc]==0 ){.  
2009f 20 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20      return p;.  
200a0 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
200a1 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73   0;.}../*.** Ins
200a2 65 72 74 20 61 20 6e 65 77 20 46 75 6e 63 44 65  ert a new FuncDe
200a3 66 20 69 6e 74 6f 20 61 20 46 75 6e 63 44 65 66  f into a FuncDef
200a4 48 61 73 68 20 68 61 73 68 20 74 61 62 6c 65 2e  Hash hash table.
200a5 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
200a6 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
200a7 75 6e 63 44 65 66 49 6e 73 65 72 74 28 0a 20 20  uncDefInsert(.  
200a8 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61  FuncDefHash *pHa
200a9 73 68 2c 20 20 2f 2a 20 54 68 65 20 68 61 73 68  sh,  /* The hash
200aa 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63   table into whic
200ab 68 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a 20  h to insert */. 
200ac 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 20 20   FuncDef *pDef  
200ad 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6e        /* The fun
200ae 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e  ction definition
200af 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a 29 7b   to insert */.){
200b0 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 4f 74 68  .  FuncDef *pOth
200b1 65 72 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20  er;.  int nName 
200b2 3d 20 73 74 72 6c 65 6e 28 70 44 65 66 2d 3e 7a  = strlen(pDef->z
200b3 4e 61 6d 65 29 3b 0a 20 20 75 38 20 63 31 20 3d  Name);.  u8 c1 =
200b4 20 28 75 38 29 70 44 65 66 2d 3e 7a 4e 61 6d 65   (u8)pDef->zName
200b5 5b 30 5d 3b 0a 20 20 69 6e 74 20 68 20 3d 20 28  [0];.  int h = (
200b6 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f  sqlite3UpperToLo
200b7 77 65 72 5b 63 31 5d 20 2b 20 6e 4e 61 6d 65 29  wer[c1] + nName)
200b8 20 25 20 41 72 72 61 79 53 69 7a 65 28 70 48 61   % ArraySize(pHa
200b9 73 68 2d 3e 61 29 3b 0a 20 20 70 4f 74 68 65 72  sh->a);.  pOther
200ba 20 3d 20 66 75 6e 63 74 69 6f 6e 53 65 61 72 63   = functionSearc
200bb 68 28 70 48 61 73 68 2c 20 68 2c 20 70 44 65 66  h(pHash, h, pDef
200bc 2d 3e 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b  ->zName, nName);
200bd 0a 20 20 69 66 28 20 70 4f 74 68 65 72 20 29 7b  .  if( pOther ){
200be 0a 20 20 20 20 70 44 65 66 2d 3e 70 4e 65 78 74  .    pDef->pNext
200bf 20 3d 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74   = pOther->pNext
200c0 3b 0a 20 20 20 20 70 4f 74 68 65 72 2d 3e 70 4e  ;.    pOther->pN
200c1 65 78 74 20 3d 20 70 44 65 66 3b 0a 20 20 7d 65  ext = pDef;.  }e
200c2 6c 73 65 7b 0a 20 20 20 20 70 44 65 66 2d 3e 70  lse{.    pDef->p
200c3 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 70 44  Next = 0;.    pD
200c4 65 66 2d 3e 70 48 61 73 68 20 3d 20 70 48 61 73  ef->pHash = pHas
200c5 68 2d 3e 61 5b 68 5d 3b 0a 20 20 20 20 70 48 61  h->a[h];.    pHa
200c6 73 68 2d 3e 61 5b 68 5d 20 3d 20 70 44 65 66 3b  sh->a[h] = pDef;
200c7 0a 20 20 7d 0a 7d 0a 20 20 0a 20 20 0a 0a 2f 2a  .  }.}.  .  ../*
200c8 0a 2a 2a 20 4c 6f 63 61 74 65 20 61 20 75 73 65  .** Locate a use
200c9 72 20 66 75 6e 63 74 69 6f 6e 20 67 69 76 65 6e  r function given
200ca 20 61 20 6e 61 6d 65 2c 20 61 20 6e 75 6d 62 65   a name, a numbe
200cb 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 61  r of arguments a
200cc 6e 64 20 61 20 66 6c 61 67 0a 2a 2a 20 69 6e 64  nd a flag.** ind
200cd 69 63 61 74 69 6e 67 20 77 68 65 74 68 65 72 20  icating whether 
200ce 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 72 65  the function pre
200cf 66 65 72 73 20 55 54 46 2d 31 36 20 6f 76 65 72  fers UTF-16 over
200d0 20 55 54 46 2d 38 2e 20 20 52 65 74 75 72 6e 20   UTF-8.  Return 
200d1 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20  a.** pointer to 
200d2 74 68 65 20 46 75 6e 63 44 65 66 20 73 74 72 75  the FuncDef stru
200d3 63 74 75 72 65 20 74 68 61 74 20 64 65 66 69 6e  cture that defin
200d4 65 73 20 74 68 61 74 20 66 75 6e 63 74 69 6f 6e  es that function
200d5 2c 20 6f 72 20 72 65 74 75 72 6e 0a 2a 2a 20 4e  , or return.** N
200d6 55 4c 4c 20 69 66 20 74 68 65 20 66 75 6e 63 74  ULL if the funct
200d7 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 65 78 69  ion does not exi
200d8 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  st..**.** If the
200d9 20 63 72 65 61 74 65 46 6c 61 67 20 61 72 67 75   createFlag argu
200da 6d 65 6e 74 20 69 73 20 74 72 75 65 2c 20 74 68  ment is true, th
200db 65 6e 20 61 20 6e 65 77 20 28 62 6c 61 6e 6b 29  en a new (blank)
200dc 20 46 75 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75   FuncDef.** stru
200dd 63 74 75 72 65 20 69 73 20 63 72 65 61 74 65 64  cture is created
200de 20 61 6e 64 20 6c 69 6b 65 64 20 69 6e 74 6f 20   and liked into 
200df 74 68 65 20 22 64 62 22 20 73 74 72 75 63 74 75  the "db" structu
200e0 72 65 20 69 66 20 61 0a 2a 2a 20 6e 6f 20 6d 61  re if a.** no ma
200e1 74 63 68 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  tching function 
200e2 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69 73 74  previously exist
200e3 65 64 2e 20 20 57 68 65 6e 20 63 72 65 61 74 65  ed.  When create
200e4 46 6c 61 67 20 69 73 20 74 72 75 65 0a 2a 2a 20  Flag is true.** 
200e5 61 6e 64 20 74 68 65 20 6e 41 72 67 20 70 61 72  and the nArg par
200e6 61 6d 65 74 65 72 20 69 73 20 2d 31 2c 20 74 68  ameter is -1, th
200e7 65 6e 20 6f 6e 6c 79 20 61 20 66 75 6e 63 74 69  en only a functi
200e8 6f 6e 20 74 68 61 74 20 61 63 63 65 70 74 73 0a  on that accepts.
200e9 2a 2a 20 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66  ** any number of
200ea 20 61 72 67 75 6d 65 6e 74 73 20 77 69 6c 6c 20   arguments will 
200eb 62 65 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  be returned..**.
200ec 2a 2a 20 49 66 20 63 72 65 61 74 65 46 6c 61 67  ** If createFlag
200ed 20 69 73 20 66 61 6c 73 65 20 61 6e 64 20 6e 41   is false and nA
200ee 72 67 20 69 73 20 2d 31 2c 20 74 68 65 6e 20 74  rg is -1, then t
200ef 68 65 20 66 69 72 73 74 20 76 61 6c 69 64 0a 2a  he first valid.*
200f0 2a 20 66 75 6e 63 74 69 6f 6e 20 66 6f 75 6e 64  * function found
200f1 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 41   is returned.  A
200f2 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 76 61 6c   function is val
200f3 69 64 20 69 66 20 65 69 74 68 65 72 20 78 46 75  id if either xFu
200f4 6e 63 0a 2a 2a 20 6f 72 20 78 53 74 65 70 20 69  nc.** or xStep i
200f5 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a  s non-zero..**.*
200f6 2a 20 49 66 20 63 72 65 61 74 65 46 6c 61 67 20  * If createFlag 
200f7 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 61  is false, then a
200f8 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74   function with t
200f9 68 65 20 72 65 71 75 69 72 65 64 20 6e 61 6d 65  he required name
200fa 20 61 6e 64 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f   and.** number o
200fb 66 20 61 72 67 75 6d 65 6e 74 73 20 6d 61 79 20  f arguments may 
200fc 62 65 20 72 65 74 75 72 6e 65 64 20 65 76 65 6e  be returned even
200fd 20 69 66 20 74 68 65 20 65 54 65 78 74 52 65 70   if the eTextRep
200fe 20 66 6c 61 67 20 64 6f 65 73 20 6e 6f 74 0a 2a   flag does not.*
200ff 2a 20 6d 61 74 63 68 20 74 68 61 74 20 72 65 71  * match that req
20100 75 65 73 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  uested..*/.SQLIT
20101 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63 44 65  E_PRIVATE FuncDe
20102 66 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 46 75  f *sqlite3FindFu
20103 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65  nction(.  sqlite
20104 33 20 2a 64 62 2c 20 20 20 20 20 20 20 2f 2a 20  3 *db,       /* 
20105 41 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65  An open database
20106 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
20107 20 2a 7a 4e 61 6d 65 2c 20 2f 2a 20 4e 61 6d 65   *zName, /* Name
20108 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
20109 2e 20 20 4e 6f 74 20 6e 75 6c 6c 2d 74 65 72 6d  .  Not null-term
2010a 69 6e 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20  inated */.  int 
2010b 6e 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 2f  nName,         /
2010c 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72  * Number of char
2010d 61 63 74 65 72 73 20 69 6e 20 74 68 65 20 6e 61  acters in the na
2010e 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67  me */.  int nArg
2010f 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ,          /* Nu
20110 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
20111 73 2e 20 20 2d 31 20 6d 65 61 6e 73 20 61 6e 79  s.  -1 means any
20112 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 75 38 20   number */.  u8 
20113 65 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 20  enc,            
20114 2f 2a 20 50 72 65 66 65 72 72 65 64 20 74 65 78  /* Preferred tex
20115 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20  t encoding */.  
20116 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 20 20  int createFlag  
20117 20 20 20 2f 2a 20 43 72 65 61 74 65 20 6e 65 77     /* Create new
20118 20 65 6e 74 72 79 20 69 66 20 74 72 75 65 20 61   entry if true a
20119 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6f 74 68 65  nd does not othe
2011a 72 77 69 73 65 20 65 78 69 73 74 20 2a 2f 0a 29  rwise exist */.)
2011b 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 3b 20  {.  FuncDef *p; 
2011c 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61          /* Itera
2011d 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a  tor variable */.
2011e 20 20 46 75 6e 63 44 65 66 20 2a 70 42 65 73 74    FuncDef *pBest
2011f 20 3d 20 30 3b 20 2f 2a 20 42 65 73 74 20 6d 61   = 0; /* Best ma
20120 74 63 68 20 66 6f 75 6e 64 20 73 6f 20 66 61 72  tch found so far
20121 20 2a 2f 0a 20 20 69 6e 74 20 62 65 73 74 53 63   */.  int bestSc
20122 6f 72 65 20 3d 20 30 3b 20 20 2f 2a 20 53 63 6f  ore = 0;  /* Sco
20123 72 65 20 6f 66 20 62 65 73 74 20 6d 61 74 63 68  re of best match
20124 20 2a 2f 0a 20 20 69 6e 74 20 68 3b 20 20 20 20   */.  int h;    
20125 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73            /* Has
20126 68 20 76 61 6c 75 65 20 2a 2f 0a 0a 0a 20 20 61  h value */...  a
20127 73 73 65 72 74 28 20 65 6e 63 3d 3d 53 51 4c 49  ssert( enc==SQLI
20128 54 45 5f 55 54 46 38 20 7c 7c 20 65 6e 63 3d 3d  TE_UTF8 || enc==
20129 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 7c  SQLITE_UTF16LE |
2012a 7c 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  | enc==SQLITE_UT
2012b 46 31 36 42 45 20 29 3b 0a 20 20 69 66 28 20 6e  F16BE );.  if( n
2012c 41 72 67 3c 2d 31 20 29 20 6e 41 72 67 20 3d 20  Arg<-1 ) nArg = 
2012d 2d 31 3b 0a 20 20 68 20 3d 20 28 73 71 6c 69 74  -1;.  h = (sqlit
2012e 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28  e3UpperToLower[(
2012f 75 38 29 7a 4e 61 6d 65 5b 30 5d 5d 20 2b 20 6e  u8)zName[0]] + n
20130 4e 61 6d 65 29 20 25 20 41 72 72 61 79 53 69 7a  Name) % ArraySiz
20131 65 28 64 62 2d 3e 61 46 75 6e 63 2e 61 29 3b 0a  e(db->aFunc.a);.
20132 0a 20 20 2f 2a 20 46 69 72 73 74 20 73 65 61 72  .  /* First sear
20133 63 68 20 66 6f 72 20 61 20 6d 61 74 63 68 20 61  ch for a match a
20134 6d 6f 6e 67 73 74 20 74 68 65 20 61 70 70 6c 69  mongst the appli
20135 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66  cation-defined f
20136 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20  unctions..  */. 
20137 20 70 20 3d 20 66 75 6e 63 74 69 6f 6e 53 65 61   p = functionSea
20138 72 63 68 28 26 64 62 2d 3e 61 46 75 6e 63 2c 20  rch(&db->aFunc, 
20139 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  h, zName, nName)
2013a 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a  ;.  while( p ){.
2013b 20 20 20 20 69 6e 74 20 73 63 6f 72 65 20 3d 20      int score = 
2013c 6d 61 74 63 68 51 75 61 6c 69 74 79 28 70 2c 20  matchQuality(p, 
2013d 6e 41 72 67 2c 20 65 6e 63 29 3b 0a 20 20 20 20  nArg, enc);.    
2013e 69 66 28 20 73 63 6f 72 65 3e 62 65 73 74 53 63  if( score>bestSc
2013f 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 70 42 65  ore ){.      pBe
20140 73 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 62 65  st = p;.      be
20141 73 74 53 63 6f 72 65 20 3d 20 73 63 6f 72 65 3b  stScore = score;
20142 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70  .    }.    p = p
20143 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 0a 20 20  ->pNext;.  }..  
20144 2f 2a 20 49 66 20 6e 6f 20 6d 61 74 63 68 20 69  /* If no match i
20145 73 20 66 6f 75 6e 64 2c 20 73 65 61 72 63 68 20  s found, search 
20146 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e  the built-in fun
20147 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a  ctions..  **.  *
20148 2a 20 45 78 63 65 70 74 2c 20 69 66 20 63 72 65  * Except, if cre
20149 61 74 65 46 6c 61 67 20 69 73 20 74 72 75 65 2c  ateFlag is true,
2014a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74   that means that
2014b 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 20 74   we are trying t
2014c 6f 0a 20 20 2a 2a 20 69 6e 73 74 61 6c 6c 20 61  o.  ** install a
2014d 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 2e 20 20   new function.  
2014e 57 68 61 74 65 76 65 72 20 46 75 6e 63 44 65 66  Whatever FuncDef
2014f 20 73 74 72 75 63 74 75 72 65 20 69 73 20 72 65   structure is re
20150 74 75 72 6e 65 64 20 77 69 6c 6c 0a 20 20 2a 2a  turned will.  **
20151 20 68 61 76 65 20 66 69 65 6c 64 73 20 6f 76 65   have fields ove
20152 72 77 72 69 74 74 65 6e 20 77 69 74 68 20 6e 65  rwritten with ne
20153 77 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 70  w information ap
20154 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68  propriate for th
20155 65 0a 20 20 2a 2a 20 6e 65 77 20 66 75 6e 63 74  e.  ** new funct
20156 69 6f 6e 2e 20 20 42 75 74 20 74 68 65 20 46 75  ion.  But the Fu
20157 6e 63 44 65 66 73 20 66 6f 72 20 62 75 69 6c 74  ncDefs for built
20158 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 20 61 72  -in functions ar
20159 65 20 72 65 61 64 2d 6f 6e 6c 79 2e 0a 20 20 2a  e read-only..  *
2015a 2a 20 53 6f 20 77 65 20 6d 75 73 74 20 6e 6f 74  * So we must not
2015b 20 73 65 61 72 63 68 20 66 6f 72 20 62 75 69 6c   search for buil
2015c 74 2d 69 6e 73 20 77 68 65 6e 20 63 72 65 61 74  t-ins when creat
2015d 69 6e 67 20 61 20 6e 65 77 20 66 75 6e 63 74 69  ing a new functi
2015e 6f 6e 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 28 20  on..  */ .  if( 
2015f 21 63 72 65 61 74 65 46 6c 61 67 20 26 26 20 21  !createFlag && !
20160 70 42 65 73 74 20 29 7b 0a 20 20 20 20 46 75 6e  pBest ){.    Fun
20161 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68 20  cDefHash *pHash 
20162 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65  = &GLOBAL(FuncDe
20163 66 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47 6c  fHash, sqlite3Gl
20164 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a  obalFunctions);.
20165 20 20 20 20 70 20 3d 20 66 75 6e 63 74 69 6f 6e      p = function
20166 53 65 61 72 63 68 28 70 48 61 73 68 2c 20 68 2c  Search(pHash, h,
20167 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a   zName, nName);.
20168 20 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a      while( p ){.
20169 20 20 20 20 20 20 69 6e 74 20 73 63 6f 72 65 20        int score 
2016a 3d 20 6d 61 74 63 68 51 75 61 6c 69 74 79 28 70  = matchQuality(p
2016b 2c 20 6e 41 72 67 2c 20 65 6e 63 29 3b 0a 20 20  , nArg, enc);.  
2016c 20 20 20 20 69 66 28 20 73 63 6f 72 65 3e 62 65      if( score>be
2016d 73 74 53 63 6f 72 65 20 29 7b 0a 20 20 20 20 20  stScore ){.     
2016e 20 20 20 70 42 65 73 74 20 3d 20 70 3b 0a 20 20     pBest = p;.  
2016f 20 20 20 20 20 20 62 65 73 74 53 63 6f 72 65 20        bestScore 
20170 3d 20 73 63 6f 72 65 3b 0a 20 20 20 20 20 20 7d  = score;.      }
20171 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e  .      p = p->pN
20172 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ext;.    }.  }..
20173 20 20 2f 2a 20 49 66 20 74 68 65 20 63 72 65 61    /* If the crea
20174 74 65 46 6c 61 67 20 70 61 72 61 6d 65 74 65 72  teFlag parameter
20175 20 69 73 20 74 72 75 65 20 61 6e 64 20 74 68 65   is true and the
20176 20 73 65 61 72 63 68 20 64 69 64 20 6e 6f 74 20   search did not 
20177 72 65 76 65 61 6c 20 61 6e 0a 20 20 2a 2a 20 65  reveal an.  ** e
20178 78 61 63 74 20 6d 61 74 63 68 20 66 6f 72 20 74  xact match for t
20179 68 65 20 6e 61 6d 65 2c 20 6e 75 6d 62 65 72 20  he name, number 
2017a 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 61 6e 64  of arguments and
2017b 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20   encoding, then 
2017c 61 64 64 20 61 0a 20 20 2a 2a 20 6e 65 77 20 65  add a.  ** new e
2017d 6e 74 72 79 20 74 6f 20 74 68 65 20 68 61 73 68  ntry to the hash
2017e 20 74 61 62 6c 65 20 61 6e 64 20 72 65 74 75 72   table and retur
2017f 6e 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  n it..  */.  if(
20180 20 63 72 65 61 74 65 46 6c 61 67 20 26 26 20 28   createFlag && (
20181 62 65 73 74 53 63 6f 72 65 3c 36 20 7c 7c 20 70  bestScore<6 || p
20182 42 65 73 74 2d 3e 6e 41 72 67 21 3d 6e 41 72 67  Best->nArg!=nArg
20183 29 20 26 26 20 0a 20 20 20 20 20 20 28 70 42 65  ) && .      (pBe
20184 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  st = sqlite3DbMa
20185 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
20186 65 6f 66 28 2a 70 42 65 73 74 29 2b 6e 4e 61 6d  eof(*pBest)+nNam
20187 65 2b 31 29 29 21 3d 30 20 29 7b 0a 20 20 20 20  e+1))!=0 ){.    
20188 70 42 65 73 74 2d 3e 7a 4e 61 6d 65 20 3d 20 28  pBest->zName = (
20189 63 68 61 72 20 2a 29 26 70 42 65 73 74 5b 31 5d  char *)&pBest[1]
2018a 3b 0a 20 20 20 20 70 42 65 73 74 2d 3e 6e 41 72  ;.    pBest->nAr
2018b 67 20 3d 20 6e 41 72 67 3b 0a 20 20 20 20 70 42  g = nArg;.    pB
2018c 65 73 74 2d 3e 69 50 72 65 66 45 6e 63 20 3d 20  est->iPrefEnc = 
2018d 65 6e 63 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  enc;.    memcpy(
2018e 70 42 65 73 74 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e  pBest->zName, zN
2018f 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20  ame, nName);.   
20190 20 70 42 65 73 74 2d 3e 7a 4e 61 6d 65 5b 6e 4e   pBest->zName[nN
20191 61 6d 65 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71  ame] = 0;.    sq
20192 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e 73 65  lite3FuncDefInse
20193 72 74 28 26 64 62 2d 3e 61 46 75 6e 63 2c 20 70  rt(&db->aFunc, p
20194 42 65 73 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  Best);.  }..  if
20195 28 20 70 42 65 73 74 20 26 26 20 28 70 42 65 73  ( pBest && (pBes
20196 74 2d 3e 78 53 74 65 70 20 7c 7c 20 70 42 65 73  t->xStep || pBes
20197 74 2d 3e 78 46 75 6e 63 20 7c 7c 20 63 72 65 61  t->xFunc || crea
20198 74 65 46 6c 61 67 29 20 29 7b 0a 20 20 20 20 72  teFlag) ){.    r
20199 65 74 75 72 6e 20 70 42 65 73 74 3b 0a 20 20 7d  eturn pBest;.  }
2019a 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
2019b 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6c 6c 20 72  /*.** Free all r
2019c 65 73 6f 75 72 63 65 73 20 68 65 6c 64 20 62 79  esources held by
2019d 20 74 68 65 20 73 63 68 65 6d 61 20 73 74 72 75   the schema stru
2019e 63 74 75 72 65 2e 20 54 68 65 20 76 6f 69 64 2a  cture. The void*
2019f 20 61 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73   argument points
201a0 0a 2a 2a 20 61 74 20 61 20 53 63 68 65 6d 61 20  .** at a Schema 
201a1 73 74 72 75 63 74 2e 20 54 68 69 73 20 66 75 6e  struct. This fun
201a2 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 63  ction does not c
201a3 61 6c 6c 20 73 71 6c 69 74 65 33 44 62 46 72 65  all sqlite3DbFre
201a4 65 28 64 62 2c 20 29 20 6f 6e 20 74 68 65 20 0a  e(db, ) on the .
201a5 2a 2a 20 70 6f 69 6e 74 65 72 20 69 74 73 65 6c  ** pointer itsel
201a6 66 2c 20 69 74 20 6a 75 73 74 20 63 6c 65 61 6e  f, it just clean
201a7 73 20 75 70 20 73 75 62 73 69 64 75 61 72 79 20  s up subsiduary 
201a8 72 65 73 6f 75 72 63 65 73 20 28 69 2e 65 2e 20  resources (i.e. 
201a9 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20  the contents.** 
201aa 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20 68 61  of the schema ha
201ab 73 68 20 74 61 62 6c 65 73 29 2e 0a 2a 2a 0a 2a  sh tables)..**.*
201ac 2a 20 54 68 65 20 53 63 68 65 6d 61 2e 63 61 63  * The Schema.cac
201ad 68 65 5f 73 69 7a 65 20 76 61 72 69 61 62 6c 65  he_size variable
201ae 20 69 73 20 6e 6f 74 20 63 6c 65 61 72 65 64 2e   is not cleared.
201af 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
201b0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
201b1 63 68 65 6d 61 46 72 65 65 28 76 6f 69 64 20 2a  chemaFree(void *
201b2 70 29 7b 0a 20 20 48 61 73 68 20 74 65 6d 70 31  p){.  Hash temp1
201b3 3b 0a 20 20 48 61 73 68 20 74 65 6d 70 32 3b 0a  ;.  Hash temp2;.
201b4 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 45 6c 65    HashElem *pEle
201b5 6d 3b 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63  m;.  Schema *pSc
201b6 68 65 6d 61 20 3d 20 28 53 63 68 65 6d 61 20 2a  hema = (Schema *
201b7 29 70 3b 0a 0a 20 20 74 65 6d 70 31 20 3d 20 70  )p;..  temp1 = p
201b8 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 3b  Schema->tblHash;
201b9 0a 20 20 74 65 6d 70 32 20 3d 20 70 53 63 68 65  .  temp2 = pSche
201ba 6d 61 2d 3e 74 72 69 67 48 61 73 68 3b 0a 20 20  ma->trigHash;.  
201bb 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28  sqlite3HashInit(
201bc 26 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61  &pSchema->trigHa
201bd 73 68 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65  sh, 0);.  sqlite
201be 33 48 61 73 68 43 6c 65 61 72 28 26 70 53 63 68  3HashClear(&pSch
201bf 65 6d 61 2d 3e 61 46 4b 65 79 29 3b 0a 20 20 73  ema->aFKey);.  s
201c0 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28  qlite3HashClear(
201c1 26 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73  &pSchema->idxHas
201c2 68 29 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d  h);.  for(pElem=
201c3 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28  sqliteHashFirst(
201c4 26 74 65 6d 70 32 29 3b 20 70 45 6c 65 6d 3b 20  &temp2); pElem; 
201c5 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68  pElem=sqliteHash
201c6 4e 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20  Next(pElem)){.  
201c7 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54    sqlite3DeleteT
201c8 72 69 67 67 65 72 28 30 2c 20 28 54 72 69 67 67  rigger(0, (Trigg
201c9 65 72 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61  er*)sqliteHashDa
201ca 74 61 28 70 45 6c 65 6d 29 29 3b 0a 20 20 7d 0a  ta(pElem));.  }.
201cb 20 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65    sqlite3HashCle
201cc 61 72 28 26 74 65 6d 70 32 29 3b 0a 20 20 73 71  ar(&temp2);.  sq
201cd 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70  lite3HashInit(&p
201ce 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c  Schema->tblHash,
201cf 20 30 29 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d   0);.  for(pElem
201d0 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74  =sqliteHashFirst
201d1 28 26 74 65 6d 70 31 29 3b 20 70 45 6c 65 6d 3b  (&temp1); pElem;
201d2 20 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73   pElem=sqliteHas
201d3 68 4e 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20  hNext(pElem)){. 
201d4 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
201d5 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28   sqliteHashData(
201d6 70 45 6c 65 6d 29 3b 0a 20 20 20 20 73 71 6c 69  pElem);.    sqli
201d7 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70  te3DeleteTable(p
201d8 54 61 62 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  Tab);.  }.  sqli
201d9 74 65 33 48 61 73 68 43 6c 65 61 72 28 26 74 65  te3HashClear(&te
201da 6d 70 31 29 3b 0a 20 20 70 53 63 68 65 6d 61 2d  mp1);.  pSchema-
201db 3e 70 53 65 71 54 61 62 20 3d 20 30 3b 0a 20 20  >pSeqTab = 0;.  
201dc 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 20 26  pSchema->flags &
201dd 3d 20 7e 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64  = ~DB_SchemaLoad
201de 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e  ed;.}../*.** Fin
201df 64 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65  d and return the
201e0 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74   schema associat
201e1 65 64 20 77 69 74 68 20 61 20 42 54 72 65 65 2e  ed with a BTree.
201e2 20 20 43 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65    Create.** a ne
201e3 77 20 6f 6e 65 20 69 66 20 6e 65 63 65 73 73 61  w one if necessa
201e4 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ry..*/.SQLITE_PR
201e5 49 56 41 54 45 20 53 63 68 65 6d 61 20 2a 73 71  IVATE Schema *sq
201e6 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74 28 73  lite3SchemaGet(s
201e7 71 6c 69 74 65 33 20 2a 64 62 2c 20 42 74 72 65  qlite3 *db, Btre
201e8 65 20 2a 70 42 74 29 7b 0a 20 20 53 63 68 65 6d  e *pBt){.  Schem
201e9 61 20 2a 20 70 3b 0a 20 20 69 66 28 20 70 42 74  a * p;.  if( pBt
201ea 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 53 63 68   ){.    p = (Sch
201eb 65 6d 61 20 2a 29 73 71 6c 69 74 65 33 42 74 72  ema *)sqlite3Btr
201ec 65 65 53 63 68 65 6d 61 28 70 42 74 2c 20 73 69  eeSchema(pBt, si
201ed 7a 65 6f 66 28 53 63 68 65 6d 61 29 2c 20 73 71  zeof(Schema), sq
201ee 6c 69 74 65 33 53 63 68 65 6d 61 46 72 65 65 29  lite3SchemaFree)
201ef 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
201f0 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 73 71 6c   = (Schema *)sql
201f1 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73  ite3MallocZero(s
201f2 69 7a 65 6f 66 28 53 63 68 65 6d 61 29 29 3b 0a  izeof(Schema));.
201f3 20 20 7d 0a 20 20 69 66 28 20 21 70 20 29 7b 0a    }.  if( !p ){.
201f4 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
201f5 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73  iled = 1;.  }els
201f6 65 20 69 66 20 28 20 30 3d 3d 70 2d 3e 66 69 6c  e if ( 0==p->fil
201f7 65 5f 66 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20  e_format ){.    
201f8 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28  sqlite3HashInit(
201f9 26 70 2d 3e 74 62 6c 48 61 73 68 2c 20 30 29 3b  &p->tblHash, 0);
201fa 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68  .    sqlite3Hash
201fb 49 6e 69 74 28 26 70 2d 3e 69 64 78 48 61 73 68  Init(&p->idxHash
201fc 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
201fd 33 48 61 73 68 49 6e 69 74 28 26 70 2d 3e 74 72  3HashInit(&p->tr
201fe 69 67 48 61 73 68 2c 20 30 29 3b 0a 20 20 20 20  igHash, 0);.    
201ff 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28  sqlite3HashInit(
20200 26 70 2d 3e 61 46 4b 65 79 2c 20 31 29 3b 0a 20  &p->aFKey, 1);. 
20201 20 20 20 70 2d 3e 65 6e 63 20 3d 20 53 51 4c 49     p->enc = SQLI
20202 54 45 5f 55 54 46 38 3b 0a 20 20 7d 0a 20 20 72  TE_UTF8;.  }.  r
20203 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 2a 2a  eturn p;.}../***
20204 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
20205 6f 66 20 63 61 6c 6c 62 61 63 6b 2e 63 20 2a 2a  of callback.c **
20206 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20207 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20208 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
20209 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
2020a 6e 20 66 69 6c 65 20 64 65 6c 65 74 65 2e 63 20  n file delete.c 
2020b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2020c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2020d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
2020e 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
2020f 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
20210 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
20211 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
20212 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
20213 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
20214 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
20215 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
20216 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
20217 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
20218 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
20219 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
2021a 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
2021b 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
2021c 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
2021d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
2021e 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
2021f 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
20220 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
20221 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20222 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20223 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20224 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20225 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
20226 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20  contains C code 
20227 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61 72  routines that ar
20228 65 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  e called by the 
20229 70 61 72 73 65 72 0a 2a 2a 20 69 6e 20 6f 72 64  parser.** in ord
2022a 65 72 20 74 6f 20 67 65 6e 65 72 61 74 65 20 63  er to generate c
2022b 6f 64 65 20 66 6f 72 20 44 45 4c 45 54 45 20 46  ode for DELETE F
2022c 52 4f 4d 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  ROM statements..
2022d 2a 2a 0a 2a 2a 20 24 49 64 3a 20 64 65 6c 65 74  **.** $Id: delet
2022e 65 2e 63 2c 76 20 31 2e 31 38 36 20 32 30 30 38  e.c,v 1.186 2008
2022f 2f 31 30 2f 33 31 20 31 30 3a 35 33 3a 32 33 20  /10/31 10:53:23 
20230 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
20231 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b  $.*/../*.** Look
20232 20 75 70 20 65 76 65 72 79 20 74 61 62 6c 65 20   up every table 
20233 74 68 61 74 20 69 73 20 6e 61 6d 65 64 20 69 6e  that is named in
20234 20 70 53 72 63 2e 20 20 49 66 20 61 6e 79 20 74   pSrc.  If any t
20235 61 62 6c 65 20 69 73 20 6e 6f 74 20 66 6f 75 6e  able is not foun
20236 64 2c 0a 2a 2a 20 61 64 64 20 61 6e 20 65 72 72  d,.** add an err
20237 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 50  or message to pP
20238 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 61 6e  arse->zErrMsg an
20239 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 20  d return NULL.  
2023a 49 66 20 61 6c 6c 20 74 61 62 6c 65 73 0a 2a 2a  If all tables.**
2023b 20 61 72 65 20 66 6f 75 6e 64 2c 20 72 65 74 75   are found, retu
2023c 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
2023d 74 68 65 20 6c 61 73 74 20 74 61 62 6c 65 2e 0a  the last table..
2023e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2023f 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33  E Table *sqlite3
20240 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 50 61  SrcListLookup(Pa
20241 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63  rse *pParse, Src
20242 4c 69 73 74 20 2a 70 53 72 63 29 7b 0a 20 20 73  List *pSrc){.  s
20243 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
20244 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70 53 72 63  em *pItem = pSrc
20245 2d 3e 61 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54  ->a;.  Table *pT
20246 61 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49  ab;.  assert( pI
20247 74 65 6d 20 26 26 20 70 53 72 63 2d 3e 6e 53 72  tem && pSrc->nSr
20248 63 3d 3d 31 20 29 3b 0a 20 20 70 54 61 62 20 3d  c==1 );.  pTab =
20249 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61   sqlite3LocateTa
2024a 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70  ble(pParse, 0, p
2024b 49 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 70 49 74  Item->zName, pIt
2024c 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a  em->zDatabase);.
2024d 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54    sqlite3DeleteT
2024e 61 62 6c 65 28 70 49 74 65 6d 2d 3e 70 54 61 62  able(pItem->pTab
2024f 29 3b 0a 20 20 70 49 74 65 6d 2d 3e 70 54 61 62  );.  pItem->pTab
20250 20 3d 20 70 54 61 62 3b 0a 20 20 69 66 28 20 70   = pTab;.  if( p
20251 54 61 62 20 29 7b 0a 20 20 20 20 70 54 61 62 2d  Tab ){.    pTab-
20252 3e 6e 52 65 66 2b 2b 3b 0a 20 20 7d 0a 20 20 69  >nRef++;.  }.  i
20253 66 28 20 73 71 6c 69 74 65 33 49 6e 64 65 78 65  f( sqlite3Indexe
20254 64 42 79 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65  dByLookup(pParse
20255 2c 20 70 49 74 65 6d 29 20 29 7b 0a 20 20 20 20  , pItem) ){.    
20256 70 54 61 62 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  pTab = 0;.  }.  
20257 72 65 74 75 72 6e 20 70 54 61 62 3b 0a 7d 0a 0a  return pTab;.}..
20258 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 6d  /*.** Check to m
20259 61 6b 65 20 73 75 72 65 20 74 68 65 20 67 69 76  ake sure the giv
2025a 65 6e 20 74 61 62 6c 65 20 69 73 20 77 72 69 74  en table is writ
2025b 61 62 6c 65 2e 20 20 49 66 20 69 74 20 69 73 20  able.  If it is 
2025c 6e 6f 74 0a 2a 2a 20 77 72 69 74 61 62 6c 65 2c  not.** writable,
2025d 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72   generate an err
2025e 6f 72 20 6d 65 73 73 61 67 65 20 61 6e 64 20 72  or message and r
2025f 65 74 75 72 6e 20 31 2e 20 20 49 66 20 69 74 20  eturn 1.  If it 
20260 69 73 0a 2a 2a 20 77 72 69 74 61 62 6c 65 20 72  is.** writable r
20261 65 74 75 72 6e 20 30 3b 0a 2a 2f 0a 53 51 4c 49  eturn 0;.*/.SQLI
20262 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
20263 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79  qlite3IsReadOnly
20264 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
20265 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 69 6e 74  Table *pTab, int
20266 20 76 69 65 77 4f 6b 29 7b 0a 20 20 69 66 28 20   viewOk){.  if( 
20267 28 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73  ((pTab->tabFlags
20268 20 26 20 54 46 5f 52 65 61 64 6f 6e 6c 79 29 21   & TF_Readonly)!
20269 3d 30 0a 20 20 20 20 20 20 20 20 26 26 20 28 70  =0.        && (p
2026a 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73  Parse->db->flags
2026b 20 26 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53   & SQLITE_WriteS
2026c 63 68 65 6d 61 29 3d 3d 30 0a 20 20 20 20 20 20  chema)==0.      
2026d 20 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 65 73    && pParse->nes
2026e 74 65 64 3d 3d 30 29 20 0a 23 69 66 6e 64 65 66  ted==0) .#ifndef
2026f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
20270 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20  TUALTABLE.      
20271 7c 7c 20 28 70 54 61 62 2d 3e 70 4d 6f 64 20 26  || (pTab->pMod &
20272 26 20 70 54 61 62 2d 3e 70 4d 6f 64 2d 3e 70 4d  & pTab->pMod->pM
20273 6f 64 75 6c 65 2d 3e 78 55 70 64 61 74 65 3d 3d  odule->xUpdate==
20274 30 29 0a 23 65 6e 64 69 66 0a 20 20 29 7b 0a 20  0).#endif.  ){. 
20275 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
20276 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c  sg(pParse, "tabl
20277 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20  e %s may not be 
20278 6d 6f 64 69 66 69 65 64 22 2c 20 70 54 61 62 2d  modified", pTab-
20279 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74  >zName);.    ret
2027a 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 69 66 6e 64  urn 1;.  }.#ifnd
2027b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
2027c 49 45 57 0a 20 20 69 66 28 20 21 76 69 65 77 4f  IEW.  if( !viewO
2027d 6b 20 26 26 20 70 54 61 62 2d 3e 70 53 65 6c 65  k && pTab->pSele
2027e 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ct ){.    sqlite
2027f 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
20280 2c 22 63 61 6e 6e 6f 74 20 6d 6f 64 69 66 79 20  ,"cannot modify 
20281 25 73 20 62 65 63 61 75 73 65 20 69 74 20 69 73  %s because it is
20282 20 61 20 76 69 65 77 22 2c 70 54 61 62 2d 3e 7a   a view",pTab->z
20283 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72  Name);.    retur
20284 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  n 1;.  }.#endif.
20285 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
20286 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
20287 64 65 20 74 68 61 74 20 77 69 6c 6c 20 6f 70 65  de that will ope
20288 6e 20 61 20 74 61 62 6c 65 20 66 6f 72 20 72 65  n a table for re
20289 61 64 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ading..*/.SQLITE
2028a 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2028b 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 0a  lite3OpenTable(.
2028c 20 20 50 61 72 73 65 20 2a 70 2c 20 20 20 20 20    Parse *p,     
2028d 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f    /* Generate co
2028e 64 65 20 69 6e 74 6f 20 74 68 69 73 20 56 44 42  de into this VDB
2028f 45 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c  E */.  int iCur,
20290 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75         /* The cu
20291 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74  rsor number of t
20292 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  he table */.  in
20293 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 2f 2a  t iDb,        /*
20294 20 54 68 65 20 64 61 74 61 62 61 73 65 20 69 6e   The database in
20295 64 65 78 20 69 6e 20 73 71 6c 69 74 65 33 2e 61  dex in sqlite3.a
20296 44 62 5b 5d 20 2a 2f 0a 20 20 54 61 62 6c 65 20  Db[] */.  Table 
20297 2a 70 54 61 62 2c 20 20 20 20 2f 2a 20 54 68 65  *pTab,    /* The
20298 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6f 70 65   table to be ope
20299 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 63  ned */.  int opc
2029a 6f 64 65 20 20 20 20 20 20 2f 2a 20 4f 50 5f 4f  ode      /* OP_O
2029b 70 65 6e 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70  penRead or OP_Op
2029c 65 6e 57 72 69 74 65 20 2a 2f 0a 29 7b 0a 20 20  enWrite */.){.  
2029d 56 64 62 65 20 2a 76 3b 0a 20 20 69 66 28 20 49  Vdbe *v;.  if( I
2029e 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
2029f 20 72 65 74 75 72 6e 3b 0a 20 20 76 20 3d 20 73   return;.  v = s
202a0 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 29  qlite3GetVdbe(p)
202a1 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 63 6f  ;.  assert( opco
202a2 64 65 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69 74 65  de==OP_OpenWrite
202a3 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f   || opcode==OP_O
202a4 70 65 6e 52 65 61 64 20 29 3b 0a 20 20 73 71 6c  penRead );.  sql
202a5 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 2c  ite3TableLock(p,
202a6 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d   iDb, pTab->tnum
202a7 2c 20 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70  , (opcode==OP_Op
202a8 65 6e 57 72 69 74 65 29 2c 20 70 54 61 62 2d 3e  enWrite), pTab->
202a9 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65  zName);.  sqlite
202aa 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
202ab 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c  P_SetNumColumns,
202ac 20 30 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b   0, pTab->nCol);
202ad 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
202ae 64 4f 70 33 28 76 2c 20 6f 70 63 6f 64 65 2c 20  dOp3(v, opcode, 
202af 69 43 75 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d  iCur, pTab->tnum
202b0 2c 20 69 44 62 29 3b 0a 20 20 56 64 62 65 43 6f  , iDb);.  VdbeCo
202b1 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20  mment((v, "%s", 
202b2 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 7d  pTab->zName));.}
202b3 0a 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  ...#if !defined(
202b4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
202b5 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
202b6 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
202b7 52 29 0a 2f 2a 0a 2a 2a 20 45 76 61 6c 75 61 74  R)./*.** Evaluat
202b8 65 20 61 20 76 69 65 77 20 61 6e 64 20 73 74 6f  e a view and sto
202b9 72 65 20 69 74 73 20 72 65 73 75 6c 74 20 69 6e  re its result in
202ba 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 74 61   an ephemeral ta
202bb 62 6c 65 2e 20 20 54 68 65 0a 2a 2a 20 70 57 68  ble.  The.** pWh
202bc 65 72 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ere argument is 
202bd 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 57 48 45 52  an optional WHER
202be 45 20 63 6c 61 75 73 65 20 74 68 61 74 20 72 65  E clause that re
202bf 73 74 72 69 63 74 73 20 74 68 65 0a 2a 2a 20 73  stricts the.** s
202c0 65 74 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68  et of rows in th
202c1 65 20 76 69 65 77 20 74 68 61 74 20 61 72 65 20  e view that are 
202c2 74 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20 74  to be added to t
202c3 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62  he ephemeral tab
202c4 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
202c5 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
202c6 65 33 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65  e3MaterializeVie
202c7 77 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  w(.  Parse *pPar
202c8 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72  se,       /* Par
202c9 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
202ca 20 20 54 61 62 6c 65 20 2a 70 56 69 65 77 2c 20    Table *pView, 
202cb 20 20 20 20 20 20 20 2f 2a 20 56 69 65 77 20 64         /* View d
202cc 65 66 69 6e 69 74 69 6f 6e 20 2a 2f 0a 20 20 45  efinition */.  E
202cd 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20  xpr *pWhere,    
202ce 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20      /* Optional 
202cf 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 6f 20  WHERE clause to 
202d0 62 65 20 61 64 64 65 64 20 2a 2f 0a 20 20 69 6e  be added */.  in
202d1 74 20 69 43 75 72 20 20 20 20 20 20 20 20 20 20  t iCur          
202d2 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d     /* Cursor num
202d3 62 65 72 20 66 6f 72 20 65 70 68 65 6d 65 72 69  ber for ephemeri
202d4 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20  al table */.){. 
202d5 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74   SelectDest dest
202d6 3b 0a 20 20 53 65 6c 65 63 74 20 2a 70 44 75 70  ;.  Select *pDup
202d7 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
202d8 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20  = pParse->db;.. 
202d9 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 53   pDup = sqlite3S
202da 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 56 69  electDup(db, pVi
202db 65 77 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20  ew->pSelect);.  
202dc 69 66 28 20 70 57 68 65 72 65 20 29 7b 0a 20 20  if( pWhere ){.  
202dd 20 20 53 72 63 4c 69 73 74 20 2a 70 46 72 6f 6d    SrcList *pFrom
202de 3b 0a 20 20 20 20 54 6f 6b 65 6e 20 76 69 65 77  ;.    Token view
202df 4e 61 6d 65 3b 0a 20 20 20 20 0a 20 20 20 20 70  Name;.    .    p
202e0 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45  Where = sqlite3E
202e1 78 70 72 44 75 70 28 64 62 2c 20 70 57 68 65 72  xprDup(db, pWher
202e2 65 29 3b 0a 20 20 20 20 76 69 65 77 4e 61 6d 65  e);.    viewName
202e3 2e 7a 20 3d 20 28 75 38 2a 29 70 56 69 65 77 2d  .z = (u8*)pView-
202e4 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 76 69 65 77  >zName;.    view
202e5 4e 61 6d 65 2e 6e 20 3d 20 28 75 6e 73 69 67 6e  Name.n = (unsign
202e6 65 64 20 69 6e 74 29 73 74 72 6c 65 6e 28 28 63  ed int)strlen((c
202e7 6f 6e 73 74 20 63 68 61 72 2a 29 76 69 65 77 4e  onst char*)viewN
202e8 61 6d 65 2e 7a 29 3b 0a 20 20 20 20 70 46 72 6f  ame.z);.    pFro
202e9 6d 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69  m = sqlite3SrcLi
202ea 73 74 41 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d  stAppendFromTerm
202eb 28 70 50 61 72 73 65 2c 20 30 2c 20 30 2c 20 30  (pParse, 0, 0, 0
202ec 2c 20 26 76 69 65 77 4e 61 6d 65 2c 20 70 44 75  , &viewName, pDu
202ed 70 2c 20 30 2c 30 29 3b 0a 20 20 20 20 70 44 75  p, 0,0);.    pDu
202ee 70 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63  p = sqlite3Selec
202ef 74 4e 65 77 28 70 50 61 72 73 65 2c 20 30 2c 20  tNew(pParse, 0, 
202f0 70 46 72 6f 6d 2c 20 70 57 68 65 72 65 2c 20 30  pFrom, pWhere, 0
202f1 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29  , 0, 0, 0, 0, 0)
202f2 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53  ;.  }.  sqlite3S
202f3 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64  electDestInit(&d
202f4 65 73 74 2c 20 53 52 54 5f 45 70 68 65 6d 54 61  est, SRT_EphemTa
202f5 62 2c 20 69 43 75 72 29 3b 0a 20 20 73 71 6c 69  b, iCur);.  sqli
202f6 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
202f7 2c 20 70 44 75 70 2c 20 26 64 65 73 74 29 3b 0a  , pDup, &dest);.
202f8 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
202f9 65 6c 65 74 65 28 64 62 2c 20 70 44 75 70 29 3b  elete(db, pDup);
202fa 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  .}.#endif /* !de
202fb 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
202fc 54 5f 56 49 45 57 29 20 26 26 20 21 64 65 66 69  T_VIEW) && !defi
202fd 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
202fe 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a 23 69 66  TRIGGER) */..#if
202ff 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
20300 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45  ENABLE_UPDATE_DE
20301 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 21  LETE_LIMIT) && !
20302 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
20303 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a 2f 2a  MIT_SUBQUERY)./*
20304 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 20  .** Generate an 
20305 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20  expression tree 
20306 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
20307 20 57 48 45 52 45 2c 20 4f 52 44 45 52 20 42 59   WHERE, ORDER BY
20308 2c 0a 2a 2a 20 61 6e 64 20 4c 49 4d 49 54 2f 4f  ,.** and LIMIT/O
20309 46 46 53 45 54 20 70 6f 72 74 69 6f 6e 20 6f 66  FFSET portion of
2030a 20 44 45 4c 45 54 45 20 61 6e 64 20 55 50 44 41   DELETE and UPDA
2030b 54 45 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  TE statements..*
2030c 2a 0a 2a 2a 20 20 20 20 20 44 45 4c 45 54 45 20  *.**     DELETE 
2030d 46 52 4f 4d 20 74 61 62 6c 65 5f 77 78 79 7a 20  FROM table_wxyz 
2030e 57 48 45 52 45 20 61 3c 35 20 4f 52 44 45 52 20  WHERE a<5 ORDER 
2030f 42 59 20 61 20 4c 49 4d 49 54 20 31 3b 0a 2a 2a  BY a LIMIT 1;.**
20310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20311 20 20 20 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f              \___
20312 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
20313 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a 20 20 20 20 20  _______/.**     
20314 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20315 20 20 20 20 20 20 20 20 20 20 70 4c 69 6d 69 74            pLimit
20316 57 68 65 72 65 20 28 70 49 6e 43 6c 61 75 73 65  Where (pInClause
20317 29 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ).*/.SQLITE_PRIV
20318 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
20319 33 4c 69 6d 69 74 57 68 65 72 65 28 0a 20 20 50  3LimitWhere(.  P
2031a 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2031b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2031c 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78  he parser contex
2031d 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  t */.  SrcList *
2031e 70 53 72 63 2c 20 20 20 20 20 20 20 20 20 20 20  pSrc,           
2031f 20 20 20 20 2f 2a 20 74 68 65 20 46 52 4f 4d 20      /* the FROM 
20320 63 6c 61 75 73 65 20 2d 2d 20 77 68 69 63 68 20  clause -- which 
20321 74 61 62 6c 65 73 20 74 6f 20 73 63 61 6e 20 2a  tables to scan *
20322 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65  /.  Expr *pWhere
20323 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
20324 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c   /* The WHERE cl
20325 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e 75  ause.  May be nu
20326 6c 6c 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ll */.  ExprList
20327 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20   *pOrderBy,     
20328 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45       /* The ORDE
20329 52 20 42 59 20 63 6c 61 75 73 65 2e 20 20 4d 61  R BY clause.  Ma
2032a 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 45  y be null */.  E
2032b 78 70 72 20 2a 70 4c 69 6d 69 74 2c 20 20 20 20  xpr *pLimit,    
2032c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2032d 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e  he LIMIT clause.
2032e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f    May be null */
2032f 0a 20 20 45 78 70 72 20 2a 70 4f 66 66 73 65 74  .  Expr *pOffset
20330 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
20331 2f 2a 20 54 68 65 20 4f 46 46 53 45 54 20 63 6c  /* The OFFSET cl
20332 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e 75  ause.  May be nu
20333 6c 6c 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53  ll */.  char *zS
20334 74 6d 74 54 79 70 65 20 20 20 20 20 20 20 20 20  tmtType         
20335 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 44       /* Either D
20336 45 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 2e  ELETE or UPDATE.
20337 20 20 46 6f 72 20 65 72 72 6f 72 20 6d 65 73 73    For error mess
20338 61 67 65 73 2e 20 2a 2f 0a 29 7b 0a 20 20 45 78  ages. */.){.  Ex
20339 70 72 20 2a 70 57 68 65 72 65 52 6f 77 69 64 20  pr *pWhereRowid 
2033a 3d 20 4e 55 4c 4c 3b 20 20 20 20 2f 2a 20 57 48  = NULL;    /* WH
2033b 45 52 45 20 72 6f 77 69 64 20 2e 2e 20 2a 2f 0a  ERE rowid .. */.
2033c 20 20 45 78 70 72 20 2a 70 49 6e 43 6c 61 75 73    Expr *pInClaus
2033d 65 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20 2f  e = NULL;      /
2033e 2a 20 57 48 45 52 45 20 72 6f 77 69 64 20 49 4e  * WHERE rowid IN
2033f 20 28 20 73 65 6c 65 63 74 20 29 20 2a 2f 0a 20   ( select ) */. 
20340 20 45 78 70 72 20 2a 70 53 65 6c 65 63 74 52 6f   Expr *pSelectRo
20341 77 69 64 20 3d 20 4e 55 4c 4c 3b 20 20 20 2f 2a  wid = NULL;   /*
20342 20 53 45 4c 45 43 54 20 72 6f 77 69 64 20 2e 2e   SELECT rowid ..
20343 2e 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  . */.  ExprList 
20344 2a 70 45 4c 69 73 74 20 3d 20 4e 55 4c 4c 3b 20  *pEList = NULL; 
20345 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
20346 6e 20 6c 69 73 74 20 63 6f 6e 74 61 6e 69 6e 67  n list contaning
20347 20 6f 6e 6c 79 20 70 53 65 6c 65 63 74 52 6f 77   only pSelectRow
20348 69 64 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  id */.  SrcList 
20349 2a 70 53 65 6c 65 63 74 53 72 63 20 3d 20 4e 55  *pSelectSrc = NU
2034a 4c 4c 3b 20 20 2f 2a 20 53 45 4c 45 43 54 20 72  LL;  /* SELECT r
2034b 6f 77 69 64 20 46 52 4f 4d 20 78 20 2e 2e 2e 20  owid FROM x ... 
2034c 28 64 75 70 20 6f 66 20 70 53 72 63 29 20 2a 2f  (dup of pSrc) */
2034d 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  .  Select *pSele
2034e 63 74 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20  ct = NULL;      
2034f 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 53 45 4c 45  /* Complete SELE
20350 43 54 20 74 72 65 65 20 2a 2f 0a 0a 20 20 2f 2a  CT tree */..  /*
20351 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 72   Check that ther
20352 65 20 69 73 6e 27 74 20 61 6e 20 4f 52 44 45 52  e isn't an ORDER
20353 20 42 59 20 77 69 74 68 6f 75 74 20 61 20 4c 49   BY without a LI
20354 4d 49 54 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f  MIT clause..  */
20355 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20  .  if( pOrderBy 
20356 26 26 20 28 70 4c 69 6d 69 74 20 3d 3d 20 30 29  && (pLimit == 0)
20357 20 29 20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33   ) {.    sqlite3
20358 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
20359 20 22 4f 52 44 45 52 20 42 59 20 77 69 74 68 6f   "ORDER BY witho
2035a 75 74 20 4c 49 4d 49 54 20 6f 6e 20 25 73 22 2c  ut LIMIT on %s",
2035b 20 7a 53 74 6d 74 54 79 70 65 29 3b 0a 20 20 20   zStmtType);.   
2035c 20 70 50 61 72 73 65 2d 3e 70 61 72 73 65 45 72   pParse->parseEr
2035d 72 6f 72 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74  ror = 1;.    got
2035e 6f 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c  o limit_where_cl
2035f 65 61 6e 75 70 5f 32 3b 0a 20 20 7d 0a 0a 20 20  eanup_2;.  }..  
20360 2f 2a 20 57 65 20 6f 6e 6c 79 20 6e 65 65 64 20  /* We only need 
20361 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 73 65  to generate a se
20362 6c 65 63 74 20 65 78 70 72 65 73 73 69 6f 6e 20  lect expression 
20363 69 66 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73  if there.  ** is
20364 20 61 20 6c 69 6d 69 74 2f 6f 66 66 73 65 74 20   a limit/offset 
20365 74 65 72 6d 20 74 6f 20 65 6e 66 6f 72 63 65 2e  term to enforce.
20366 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4c 69 6d  .  */.  if( pLim
20367 69 74 20 3d 3d 20 30 20 29 20 7b 0a 20 20 20 20  it == 0 ) {.    
20368 2f 2a 20 69 66 20 70 4c 69 6d 69 74 20 69 73 20  /* if pLimit is 
20369 6e 75 6c 6c 2c 20 70 4f 66 66 73 65 74 20 77 69  null, pOffset wi
2036a 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 6e 75 6c  ll always be nul
2036b 6c 20 61 73 20 77 65 6c 6c 2e 20 2a 2f 0a 20 20  l as well. */.  
2036c 20 20 61 73 73 65 72 74 28 20 70 4f 66 66 73 65    assert( pOffse
2036d 74 20 3d 3d 20 30 20 29 3b 0a 20 20 20 20 72 65  t == 0 );.    re
2036e 74 75 72 6e 20 70 57 68 65 72 65 3b 0a 20 20 7d  turn pWhere;.  }
2036f 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
20370 61 20 73 65 6c 65 63 74 20 65 78 70 72 65 73 73  a select express
20371 69 6f 6e 20 74 72 65 65 20 74 6f 20 65 6e 66 6f  ion tree to enfo
20372 72 63 65 20 74 68 65 20 6c 69 6d 69 74 2f 6f 66  rce the limit/of
20373 66 73 65 74 20 0a 20 20 2a 2a 20 74 65 72 6d 20  fset .  ** term 
20374 66 6f 72 20 74 68 65 20 44 45 4c 45 54 45 20 6f  for the DELETE o
20375 72 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65  r UPDATE stateme
20376 6e 74 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65  nt.  For example
20377 3a 0a 20 20 2a 2a 20 20 20 44 45 4c 45 54 45 20  :.  **   DELETE 
20378 46 52 4f 4d 20 74 61 62 6c 65 5f 61 20 57 48 45  FROM table_a WHE
20379 52 45 20 63 6f 6c 31 3d 31 20 4f 52 44 45 52 20  RE col1=1 ORDER 
2037a 42 59 20 63 6f 6c 32 20 4c 49 4d 49 54 20 31 20  BY col2 LIMIT 1 
2037b 4f 46 46 53 45 54 20 31 0a 20 20 2a 2a 20 62 65  OFFSET 1.  ** be
2037c 63 6f 6d 65 73 3a 0a 20 20 2a 2a 20 20 20 44 45  comes:.  **   DE
2037d 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f  LETE FROM table_
2037e 61 20 57 48 45 52 45 20 72 6f 77 69 64 20 49 4e  a WHERE rowid IN
2037f 20 28 20 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c   ( .  **     SEL
20380 45 43 54 20 72 6f 77 69 64 20 46 52 4f 4d 20 74  ECT rowid FROM t
20381 61 62 6c 65 5f 61 20 57 48 45 52 45 20 63 6f 6c  able_a WHERE col
20382 31 3d 31 20 4f 52 44 45 52 20 42 59 20 63 6f 6c  1=1 ORDER BY col
20383 32 20 4c 49 4d 49 54 20 31 20 4f 46 46 53 45 54  2 LIMIT 1 OFFSET
20384 20 31 0a 20 20 2a 2a 20 20 20 29 3b 0a 20 20 2a   1.  **   );.  *
20385 2f 0a 0a 20 20 70 53 65 6c 65 63 74 52 6f 77 69  /..  pSelectRowi
20386 64 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28  d = sqlite3Expr(
20387 70 50 61 72 73 65 2d 3e 64 62 2c 20 54 4b 5f 52  pParse->db, TK_R
20388 4f 57 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20  OW, 0, 0, 0);.  
20389 69 66 28 20 70 53 65 6c 65 63 74 52 6f 77 69 64  if( pSelectRowid
2038a 20 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d   == 0 ) goto lim
2038b 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70  it_where_cleanup
2038c 5f 32 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 73  _2;.  pEList = s
2038d 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
2038e 70 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c 20  pend(pParse, 0, 
2038f 70 53 65 6c 65 63 74 52 6f 77 69 64 2c 20 30 29  pSelectRowid, 0)
20390 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 20 3d  ;.  if( pEList =
20391 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74  = 0 ) goto limit
20392 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32  _where_cleanup_2
20393 3b 0a 0a 20 20 2f 2a 20 64 75 70 6c 69 63 61 74  ;..  /* duplicat
20394 65 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  e the FROM claus
20395 65 20 61 73 20 69 74 20 69 73 20 6e 65 65 64 65  e as it is neede
20396 64 20 62 79 20 62 6f 74 68 20 74 68 65 20 44 45  d by both the DE
20397 4c 45 54 45 2f 55 50 44 41 54 45 20 74 72 65 65  LETE/UPDATE tree
20398 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 53 45  .  ** and the SE
20399 4c 45 43 54 20 73 75 62 74 72 65 65 2e 20 2a 2f  LECT subtree. */
2039a 0a 20 20 70 53 65 6c 65 63 74 53 72 63 20 3d 20  .  pSelectSrc = 
2039b 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75  sqlite3SrcListDu
2039c 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53  p(pParse->db, pS
2039d 72 63 29 3b 0a 20 20 69 66 28 20 70 53 65 6c 65  rc);.  if( pSele
2039e 63 74 53 72 63 20 3d 3d 20 30 20 29 20 7b 0a 20  ctSrc == 0 ) {. 
2039f 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
203a0 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  stDelete(pParse-
203a1 3e 64 62 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  >db, pEList);.  
203a2 20 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77 68 65    goto limit_whe
203a3 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b 0a 20 20  re_cleanup_2;.  
203a4 7d 0a 0a 20 20 2f 2a 20 67 65 6e 65 72 61 74 65  }..  /* generate
203a5 20 74 68 65 20 53 45 4c 45 43 54 20 65 78 70 72   the SELECT expr
203a6 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 2a 2f 0a  ession tree. */.
203a7 20 20 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69    pSelect = sqli
203a8 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61  te3SelectNew(pPa
203a9 72 73 65 2c 70 45 4c 69 73 74 2c 70 53 65 6c 65  rse,pEList,pSele
203aa 63 74 53 72 63 2c 70 57 68 65 72 65 2c 30 2c 30  ctSrc,pWhere,0,0
203ab 2c 70 4f 72 64 65 72 42 79 2c 30 2c 70 4c 69 6d  ,pOrderBy,0,pLim
203ac 69 74 2c 70 4f 66 66 73 65 74 29 3b 0a 20 20 69  it,pOffset);.  i
203ad 66 28 20 70 53 65 6c 65 63 74 20 3d 3d 20 30 20  f( pSelect == 0 
203ae 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f  ) return 0;..  /
203af 2a 20 6e 6f 77 20 67 65 6e 65 72 61 74 65 20 74  * now generate t
203b0 68 65 20 6e 65 77 20 57 48 45 52 45 20 72 6f 77  he new WHERE row
203b1 69 64 20 49 4e 20 63 6c 61 75 73 65 20 66 6f 72  id IN clause for
203b2 20 74 68 65 20 44 45 4c 45 54 45 2f 55 44 50 41   the DELETE/UDPA
203b3 54 45 20 2a 2f 0a 20 20 70 57 68 65 72 65 52 6f  TE */.  pWhereRo
203b4 77 69 64 20 3d 20 73 71 6c 69 74 65 33 45 78 70  wid = sqlite3Exp
203b5 72 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 54 4b  r(pParse->db, TK
203b6 5f 52 4f 57 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  _ROW, 0, 0, 0);.
203b7 20 20 69 66 28 20 70 57 68 65 72 65 52 6f 77 69    if( pWhereRowi
203b8 64 20 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69  d == 0 ) goto li
203b9 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75  mit_where_cleanu
203ba 70 5f 31 3b 0a 20 20 70 49 6e 43 6c 61 75 73 65  p_1;.  pInClause
203bb 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
203bc 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 70  pParse, TK_IN, p
203bd 57 68 65 72 65 52 6f 77 69 64 2c 20 30 2c 20 30  WhereRowid, 0, 0
203be 29 3b 0a 20 20 69 66 28 20 70 49 6e 43 6c 61 75  );.  if( pInClau
203bf 73 65 20 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c  se == 0 ) goto l
203c0 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e  imit_where_clean
203c1 75 70 5f 31 3b 0a 0a 20 20 70 49 6e 43 6c 61 75  up_1;..  pInClau
203c2 73 65 2d 3e 70 53 65 6c 65 63 74 20 3d 20 70 53  se->pSelect = pS
203c3 65 6c 65 63 74 3b 0a 20 20 73 71 6c 69 74 65 33  elect;.  sqlite3
203c4 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70 50  ExprSetHeight(pP
203c5 61 72 73 65 2c 20 70 49 6e 43 6c 61 75 73 65 29  arse, pInClause)
203c6 3b 0a 20 20 72 65 74 75 72 6e 20 70 49 6e 43 6c  ;.  return pInCl
203c7 61 75 73 65 3b 0a 0a 20 20 2f 2a 20 73 6f 6d 65  ause;..  /* some
203c8 74 68 69 6e 67 20 77 65 6e 74 20 77 72 6f 6e 67  thing went wrong
203c9 2e 20 63 6c 65 61 6e 20 75 70 20 61 6e 79 74 68  . clean up anyth
203ca 69 6e 67 20 61 6c 6c 6f 63 61 74 65 64 2e 20 2a  ing allocated. *
203cb 2f 0a 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c  /.limit_where_cl
203cc 65 61 6e 75 70 5f 31 3a 0a 20 20 73 71 6c 69 74  eanup_1:.  sqlit
203cd 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70  e3SelectDelete(p
203ce 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 65 6c 65  Parse->db, pSele
203cf 63 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b  ct);.  return 0;
203d0 0a 0a 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c  ..limit_where_cl
203d1 65 61 6e 75 70 5f 32 3a 0a 20 20 73 71 6c 69 74  eanup_2:.  sqlit
203d2 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 50 61  e3ExprDelete(pPa
203d3 72 73 65 2d 3e 64 62 2c 20 70 57 68 65 72 65 29  rse->db, pWhere)
203d4 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  ;.  sqlite3ExprL
203d5 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65  istDelete(pParse
203d6 2d 3e 64 62 2c 20 70 4f 72 64 65 72 42 79 29 3b  ->db, pOrderBy);
203d7 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
203d8 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
203d9 20 70 4c 69 6d 69 74 29 3b 0a 20 20 73 71 6c 69   pLimit);.  sqli
203da 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 50  te3ExprDelete(pP
203db 61 72 73 65 2d 3e 64 62 2c 20 70 4f 66 66 73 65  arse->db, pOffse
203dc 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  t);.  return 0;.
203dd 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69  }.#endif /* defi
203de 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
203df 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f  E_UPDATE_DELETE_
203e0 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e  LIMIT) && !defin
203e1 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ed(SQLITE_OMIT_S
203e2 55 42 51 55 45 52 59 29 20 2a 2f 0a 0a 2f 2a 0a  UBQUERY) */../*.
203e3 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
203e4 20 66 6f 72 20 61 20 44 45 4c 45 54 45 20 46 52   for a DELETE FR
203e5 4f 4d 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  OM statement..**
203e6 0a 2a 2a 20 20 20 20 20 44 45 4c 45 54 45 20 46  .**     DELETE F
203e7 52 4f 4d 20 74 61 62 6c 65 5f 77 78 79 7a 20 57  ROM table_wxyz W
203e8 48 45 52 45 20 61 3c 35 20 41 4e 44 20 62 20 4e  HERE a<5 AND b N
203e9 4f 54 20 4e 55 4c 4c 3b 0a 2a 2a 20 20 20 20 20  OT NULL;.**     
203ea 20 20 20 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f              \___
203eb 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20 5c 5f 5f  _____/       \__
203ec 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a  ______________/.
203ed 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
203ee 20 20 20 20 70 54 61 62 4c 69 73 74 20 20 20 20      pTabList    
203ef 20 20 20 20 20 20 20 20 20 20 70 57 68 65 72 65            pWhere
203f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
203f1 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
203f2 65 6c 65 74 65 46 72 6f 6d 28 0a 20 20 50 61 72  eleteFrom(.  Par
203f3 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
203f4 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
203f5 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53  r context */.  S
203f6 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74  rcList *pTabList
203f7 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62  ,     /* The tab
203f8 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 77 65  le from which we
203f9 20 73 68 6f 75 6c 64 20 64 65 6c 65 74 65 20 74   should delete t
203fa 68 69 6e 67 73 20 2a 2f 0a 20 20 45 78 70 72 20  hings */.  Expr 
203fb 2a 70 57 68 65 72 65 20 20 20 20 20 20 20 20 20  *pWhere         
203fc 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63    /* The WHERE c
203fd 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e  lause.  May be n
203fe 75 6c 6c 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65  ull */.){.  Vdbe
203ff 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20   *v;            
20400 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
20401 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e  l database engin
20402 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  e */.  Table *pT
20403 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ab;           /*
20404 20 54 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20   The table from 
20405 77 68 69 63 68 20 72 65 63 6f 72 64 73 20 77 69  which records wi
20406 6c 6c 20 62 65 20 64 65 6c 65 74 65 64 20 2a 2f  ll be deleted */
20407 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
20408 44 62 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  Db;       /* Nam
20409 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 68 6f  e of database ho
2040a 6c 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20  lding pTab */.  
2040b 69 6e 74 20 65 6e 64 2c 20 61 64 64 72 20 3d 20  int end, addr = 
2040c 30 3b 20 20 20 20 20 2f 2a 20 41 20 63 6f 75 70  0;     /* A coup
2040d 6c 65 20 61 64 64 72 65 73 73 65 73 20 6f 66 20  le addresses of 
2040e 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20 2a  generated code *
2040f 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  /.  int i;      
20410 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
20411 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
20412 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66  WhereInfo *pWInf
20413 6f 3b 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d  o;     /* Inform
20414 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20  ation about the 
20415 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
20416 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20    Index *pIdx;  
20417 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20           /* For 
20418 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69 6e 64  looping over ind
20419 69 63 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c  ices of the tabl
2041a 65 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b  e */.  int iCur;
2041b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2041c 20 56 44 42 45 20 43 75 72 73 6f 72 20 6e 75 6d   VDBE Cursor num
2041d 62 65 72 20 66 6f 72 20 70 54 61 62 20 2a 2f 0a  ber for pTab */.
2041e 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
2041f 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 69 6e           /* Main
20420 20 64 61 74 61 62 61 73 65 20 73 74 72 75 63 74   database struct
20421 75 72 65 20 2a 2f 0a 20 20 41 75 74 68 43 6f 6e  ure */.  AuthCon
20422 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b 20 20  text sContext;  
20423 2f 2a 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e  /* Authorization
20424 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e   context */.  in
20425 74 20 6f 6c 64 49 64 78 20 3d 20 2d 31 3b 20 20  t oldIdx = -1;  
20426 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 66       /* Cursor f
20427 6f 72 20 74 68 65 20 4f 4c 44 20 74 61 62 6c 65  or the OLD table
20428 20 6f 66 20 41 46 54 45 52 20 74 72 69 67 67 65   of AFTER trigge
20429 72 73 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74  rs */.  NameCont
2042a 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20 20 2f  ext sNC;       /
2042b 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 74  * Name context t
2042c 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 72 65 73  o resolve expres
2042d 73 69 6f 6e 73 20 69 6e 20 2a 2f 0a 20 20 69 6e  sions in */.  in
2042e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20  t iDb;          
2042f 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
20430 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74   number */.  int
20431 20 6d 65 6d 43 6e 74 20 3d 20 2d 31 3b 20 20 20   memCnt = -1;   
20432 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65      /* Memory ce
20433 6c 6c 20 75 73 65 64 20 66 6f 72 20 63 68 61 6e  ll used for chan
20434 67 65 20 63 6f 75 6e 74 69 6e 67 20 2a 2f 0a 20  ge counting */. 
20435 20 69 6e 74 20 72 63 61 75 74 68 3b 20 20 20 20   int rcauth;    
20436 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
20437 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 75 74   returned by aut
20438 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62  horization callb
20439 61 63 6b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  ack */..#ifndef 
2043a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
2043b 47 45 52 0a 20 20 69 6e 74 20 69 73 56 69 65 77  GER.  int isView
2043c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2043d 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 74     /* True if at
2043e 74 65 6d 70 74 69 6e 67 20 74 6f 20 64 65 6c 65  tempting to dele
2043f 74 65 20 66 72 6f 6d 20 61 20 76 69 65 77 20 2a  te from a view *
20440 2f 0a 20 20 69 6e 74 20 74 72 69 67 67 65 72 73  /.  int triggers
20441 5f 65 78 69 73 74 20 3d 20 30 3b 20 20 20 20 20  _exist = 0;     
20442 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 79 20   /* True if any 
20443 74 72 69 67 67 65 72 73 20 65 78 69 73 74 20 2a  triggers exist *
20444 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 69  /.#endif.  int i
20445 42 65 67 69 6e 41 66 74 65 72 54 72 69 67 67 65  BeginAfterTrigge
20446 72 3b 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65  r;      /* Addre
20447 73 73 20 6f 66 20 61 66 74 65 72 20 74 72 69 67  ss of after trig
20448 67 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20  ger program */. 
20449 20 69 6e 74 20 69 45 6e 64 41 66 74 65 72 54 72   int iEndAfterTr
2044a 69 67 67 65 72 3b 20 20 20 20 20 20 20 20 2f 2a  igger;        /*
2044b 20 45 78 69 74 20 6f 66 20 61 66 74 65 72 20 74   Exit of after t
2044c 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 2a  rigger program *
2044d 2f 0a 20 20 69 6e 74 20 69 42 65 67 69 6e 42 65  /.  int iBeginBe
2044e 66 6f 72 65 54 72 69 67 67 65 72 3b 20 20 20 20  foreTrigger;    
2044f 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 62   /* Address of b
20450 65 66 6f 72 65 20 74 72 69 67 67 65 72 20 70 72  efore trigger pr
20451 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 69  ogram */.  int i
20452 45 6e 64 42 65 66 6f 72 65 54 72 69 67 67 65 72  EndBeforeTrigger
20453 3b 20 20 20 20 20 20 20 2f 2a 20 45 78 69 74 20  ;       /* Exit 
20454 6f 66 20 62 65 66 6f 72 65 20 74 72 69 67 67 65  of before trigge
20455 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 75  r program */.  u
20456 33 32 20 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 20  32 old_col_mask 
20457 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d  = 0;        /* M
20458 61 73 6b 20 6f 66 20 4f 4c 44 2e 2a 20 63 6f 6c  ask of OLD.* col
20459 75 6d 6e 73 20 69 6e 20 75 73 65 20 2a 2f 0a 0a  umns in use */..
2045a 20 20 73 43 6f 6e 74 65 78 74 2e 70 50 61 72 73    sContext.pPars
2045b 65 20 3d 20 30 3b 0a 20 20 64 62 20 3d 20 70 50  e = 0;.  db = pP
2045c 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20  arse->db;.  if( 
2045d 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20  pParse->nErr || 
2045e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2045f 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c   ){.    goto del
20460 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70  ete_from_cleanup
20461 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
20462 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d  pTabList->nSrc==
20463 31 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74  1 );..  /* Locat
20464 65 20 74 68 65 20 74 61 62 6c 65 20 77 68 69 63  e the table whic
20465 68 20 77 65 20 77 61 6e 74 20 74 6f 20 64 65 6c  h we want to del
20466 65 74 65 2e 20 20 54 68 69 73 20 74 61 62 6c 65  ete.  This table
20467 20 68 61 73 20 74 6f 20 62 65 0a 20 20 2a 2a 20   has to be.  ** 
20468 70 75 74 20 69 6e 20 61 6e 20 53 72 63 4c 69 73  put in an SrcLis
20469 74 20 73 74 72 75 63 74 75 72 65 20 62 65 63 61  t structure beca
2046a 75 73 65 20 73 6f 6d 65 20 6f 66 20 74 68 65 20  use some of the 
2046b 73 75 62 72 6f 75 74 69 6e 65 73 20 77 65 0a 20  subroutines we. 
2046c 20 2a 2a 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c   ** will be call
2046d 69 6e 67 20 61 72 65 20 64 65 73 69 67 6e 65 64  ing are designed
2046e 20 74 6f 20 77 6f 72 6b 20 77 69 74 68 20 6d 75   to work with mu
2046f 6c 74 69 70 6c 65 20 74 61 62 6c 65 73 20 61 6e  ltiple tables an
20470 64 20 65 78 70 65 63 74 0a 20 20 2a 2a 20 61 6e  d expect.  ** an
20471 20 53 72 63 4c 69 73 74 2a 20 70 61 72 61 6d 65   SrcList* parame
20472 74 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 6a  ter instead of j
20473 75 73 74 20 61 20 54 61 62 6c 65 2a 20 70 61 72  ust a Table* par
20474 61 6d 65 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70  ameter..  */.  p
20475 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63  Tab = sqlite3Src
20476 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73  ListLookup(pPars
20477 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20 20  e, pTabList);.  
20478 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20 20 67  if( pTab==0 )  g
20479 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f  oto delete_from_
2047a 63 6c 65 61 6e 75 70 3b 0a 0a 20 20 2f 2a 20 46  cleanup;..  /* F
2047b 69 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20  igure out if we 
2047c 68 61 76 65 20 61 6e 79 20 74 72 69 67 67 65 72  have any trigger
2047d 73 20 61 6e 64 20 69 66 20 74 68 65 20 74 61 62  s and if the tab
2047e 6c 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 64 65  le being.  ** de
2047f 6c 65 74 65 64 20 66 72 6f 6d 20 69 73 20 61 20  leted from is a 
20480 76 69 65 77 0a 20 20 2a 2f 0a 23 69 66 6e 64 65  view.  */.#ifnde
20481 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
20482 49 47 47 45 52 0a 20 20 74 72 69 67 67 65 72 73  IGGER.  triggers
20483 5f 65 78 69 73 74 20 3d 20 73 71 6c 69 74 65 33  _exist = sqlite3
20484 54 72 69 67 67 65 72 73 45 78 69 73 74 28 70 50  TriggersExist(pP
20485 61 72 73 65 2c 20 70 54 61 62 2c 20 54 4b 5f 44  arse, pTab, TK_D
20486 45 4c 45 54 45 2c 20 30 29 3b 0a 20 20 69 73 56  ELETE, 0);.  isV
20487 69 65 77 20 3d 20 70 54 61 62 2d 3e 70 53 65 6c  iew = pTab->pSel
20488 65 63 74 21 3d 30 3b 0a 23 65 6c 73 65 0a 23 20  ect!=0;.#else.# 
20489 64 65 66 69 6e 65 20 74 72 69 67 67 65 72 73 5f  define triggers_
2048a 65 78 69 73 74 20 30 0a 23 20 64 65 66 69 6e 65  exist 0.# define
2048b 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66   isView 0.#endif
2048c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
2048d 4d 49 54 5f 56 49 45 57 0a 23 20 75 6e 64 65 66  MIT_VIEW.# undef
2048e 20 69 73 56 69 65 77 0a 23 20 64 65 66 69 6e 65   isView.# define
2048f 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66   isView 0.#endif
20490 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49  ..  if( sqlite3I
20491 73 52 65 61 64 4f 6e 6c 79 28 70 50 61 72 73 65  sReadOnly(pParse
20492 2c 20 70 54 61 62 2c 20 74 72 69 67 67 65 72 73  , pTab, triggers
20493 5f 65 78 69 73 74 29 20 29 7b 0a 20 20 20 20 67  _exist) ){.    g
20494 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f  oto delete_from_
20495 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69  cleanup;.  }.  i
20496 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
20497 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54  maToIndex(db, pT
20498 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
20499 61 73 73 65 72 74 28 20 69 44 62 3c 64 62 2d 3e  assert( iDb<db->
2049a 6e 44 62 20 29 3b 0a 20 20 7a 44 62 20 3d 20 64  nDb );.  zDb = d
2049b 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
2049c 65 3b 0a 20 20 72 63 61 75 74 68 20 3d 20 73 71  e;.  rcauth = sq
2049d 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
2049e 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45  Parse, SQLITE_DE
2049f 4c 45 54 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  LETE, pTab->zNam
204a0 65 2c 20 30 2c 20 7a 44 62 29 3b 0a 20 20 61 73  e, 0, zDb);.  as
204a1 73 65 72 74 28 20 72 63 61 75 74 68 3d 3d 53 51  sert( rcauth==SQ
204a2 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 61 75 74  LITE_OK || rcaut
204a3 68 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 7c  h==SQLITE_DENY |
204a4 7c 20 72 63 61 75 74 68 3d 3d 53 51 4c 49 54 45  | rcauth==SQLITE
204a5 5f 49 47 4e 4f 52 45 20 29 3b 0a 20 20 69 66 28  _IGNORE );.  if(
204a6 20 72 63 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f   rcauth==SQLITE_
204a7 44 45 4e 59 20 29 7b 0a 20 20 20 20 67 6f 74 6f  DENY ){.    goto
204a8 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65   delete_from_cle
204a9 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 61 73 73 65  anup;.  }.  asse
204aa 72 74 28 21 69 73 56 69 65 77 20 7c 7c 20 74 72  rt(!isView || tr
204ab 69 67 67 65 72 73 5f 65 78 69 73 74 29 3b 0a 0a  iggers_exist);..
204ac 20 20 2f 2a 20 49 66 20 70 54 61 62 20 69 73 20    /* If pTab is 
204ad 72 65 61 6c 6c 79 20 61 20 76 69 65 77 2c 20 6d  really a view, m
204ae 61 6b 65 20 73 75 72 65 20 69 74 20 68 61 73 20  ake sure it has 
204af 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64  been initialized
204b0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
204b1 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d  ite3ViewGetColum
204b2 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70  nNames(pParse, p
204b3 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  Tab) ){.    goto
204b4 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65   delete_from_cle
204b5 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  anup;.  }..  /* 
204b6 41 6c 6c 6f 63 61 74 65 20 61 20 63 75 72 73 6f  Allocate a curso
204b7 72 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  r used to store 
204b8 74 68 65 20 6f 6c 64 2e 2a 20 64 61 74 61 20 66  the old.* data f
204b9 6f 72 20 61 20 74 72 69 67 67 65 72 2e 0a 20 20  or a trigger..  
204ba 2a 2f 0a 20 20 69 66 28 20 74 72 69 67 67 65 72  */.  if( trigger
204bb 73 5f 65 78 69 73 74 20 29 7b 20 0a 20 20 20 20  s_exist ){ .    
204bc 6f 6c 64 49 64 78 20 3d 20 70 50 61 72 73 65 2d  oldIdx = pParse-
204bd 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 20 20  >nTab++;.  }..  
204be 2f 2a 20 41 73 73 69 67 6e 20 20 63 75 72 73 6f  /* Assign  curso
204bf 72 20 6e 75 6d 62 65 72 20 74 6f 20 74 68 65 20  r number to the 
204c0 74 61 62 6c 65 20 61 6e 64 20 61 6c 6c 20 69 74  table and all it
204c1 73 20 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2f 0a  s indices..  */.
204c2 20 20 61 73 73 65 72 74 28 20 70 54 61 62 4c 69    assert( pTabLi
204c3 73 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20  st->nSrc==1 );. 
204c4 20 69 43 75 72 20 3d 20 70 54 61 62 4c 69 73 74   iCur = pTabList
204c5 2d 3e 61 5b 30 5d 2e 69 43 75 72 73 6f 72 20 3d  ->a[0].iCursor =
204c6 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
204c7 0a 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62  .  for(pIdx=pTab
204c8 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
204c9 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
204ca 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  ){.    pParse->n
204cb 54 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  Tab++;.  }..  /*
204cc 20 53 74 61 72 74 20 74 68 65 20 76 69 65 77 20   Start the view 
204cd 63 6f 6e 74 65 78 74 0a 20 20 2a 2f 0a 20 20 69  context.  */.  i
204ce 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20  f( isView ){.   
204cf 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74   sqlite3AuthCont
204d0 65 78 74 50 75 73 68 28 70 50 61 72 73 65 2c 20  extPush(pParse, 
204d1 26 73 43 6f 6e 74 65 78 74 2c 20 70 54 61 62 2d  &sContext, pTab-
204d2 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 0a 20 20  >zName);.  }..  
204d3 2f 2a 20 42 65 67 69 6e 20 67 65 6e 65 72 61 74  /* Begin generat
204d4 69 6e 67 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20  ing code..  */. 
204d5 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
204d6 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69  dbe(pParse);.  i
204d7 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20 67  f( v==0 ){.    g
204d8 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f  oto delete_from_
204d9 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69  cleanup;.  }.  i
204da 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  f( pParse->neste
204db 64 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 56 64  d==0 ) sqlite3Vd
204dc 62 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 76  beCountChanges(v
204dd 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69  );.  sqlite3Begi
204de 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
204df 70 50 61 72 73 65 2c 20 74 72 69 67 67 65 72 73  pParse, triggers
204e0 5f 65 78 69 73 74 2c 20 69 44 62 29 3b 0a 0a 20  _exist, iDb);.. 
204e1 20 69 66 28 20 74 72 69 67 67 65 72 73 5f 65 78   if( triggers_ex
204e2 69 73 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f  ist ){.    int o
204e3 72 63 6f 6e 66 20 3d 20 28 28 70 50 61 72 73 65  rconf = ((pParse
204e4 2d 3e 74 72 69 67 53 74 61 63 6b 29 3f 70 50 61  ->trigStack)?pPa
204e5 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 2d 3e  rse->trigStack->
204e6 6f 72 63 6f 6e 66 3a 4f 45 5f 44 65 66 61 75 6c  orconf:OE_Defaul
204e7 74 29 3b 0a 20 20 20 20 69 6e 74 20 69 47 6f 74  t);.    int iGot
204e8 6f 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  o = sqlite3VdbeA
204e9 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp0(v, OP_Goto
204ea 29 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71  );.    addr = sq
204eb 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
204ec 65 6c 28 76 29 3b 0a 0a 20 20 20 20 69 42 65 67  el(v);..    iBeg
204ed 69 6e 42 65 66 6f 72 65 54 72 69 67 67 65 72 20  inBeforeTrigger 
204ee 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
204ef 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20  rentAddr(v);.   
204f0 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 43 6f   (void)sqlite3Co
204f1 64 65 52 6f 77 54 72 69 67 67 65 72 28 70 50 61  deRowTrigger(pPa
204f2 72 73 65 2c 20 54 4b 5f 44 45 4c 45 54 45 2c 20  rse, TK_DELETE, 
204f3 30 2c 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52  0, TRIGGER_BEFOR
204f4 45 2c 20 70 54 61 62 2c 0a 20 20 20 20 20 20 20  E, pTab,.       
204f5 20 2d 31 2c 20 6f 6c 64 49 64 78 2c 20 6f 72 63   -1, oldIdx, orc
204f6 6f 6e 66 2c 20 61 64 64 72 2c 20 26 6f 6c 64 5f  onf, addr, &old_
204f7 63 6f 6c 5f 6d 61 73 6b 2c 20 30 29 3b 0a 20 20  col_mask, 0);.  
204f8 20 20 69 45 6e 64 42 65 66 6f 72 65 54 72 69 67    iEndBeforeTrig
204f9 67 65 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ger = sqlite3Vdb
204fa 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f  eAddOp0(v, OP_Go
204fb 74 6f 29 3b 0a 0a 20 20 20 20 69 42 65 67 69 6e  to);..    iBegin
204fc 41 66 74 65 72 54 72 69 67 67 65 72 20 3d 20 73  AfterTrigger = s
204fd 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
204fe 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 28 76  tAddr(v);.    (v
204ff 6f 69 64 29 73 71 6c 69 74 65 33 43 6f 64 65 52  oid)sqlite3CodeR
20500 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73 65  owTrigger(pParse
20501 2c 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30 2c 20  , TK_DELETE, 0, 
20502 54 52 49 47 47 45 52 5f 41 46 54 45 52 2c 20 70  TRIGGER_AFTER, p
20503 54 61 62 2c 20 2d 31 2c 0a 20 20 20 20 20 20 20  Tab, -1,.       
20504 20 6f 6c 64 49 64 78 2c 20 6f 72 63 6f 6e 66 2c   oldIdx, orconf,
20505 20 61 64 64 72 2c 20 26 6f 6c 64 5f 63 6f 6c 5f   addr, &old_col_
20506 6d 61 73 6b 2c 20 30 29 3b 0a 20 20 20 20 69 45  mask, 0);.    iE
20507 6e 64 41 66 74 65 72 54 72 69 67 67 65 72 20 3d  ndAfterTrigger =
20508 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
20509 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a  p0(v, OP_Goto);.
2050a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2050b 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 47 6f 74  JumpHere(v, iGot
2050c 6f 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  o);.  }..  /* If
2050d 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 20 74   we are trying t
2050e 6f 20 64 65 6c 65 74 65 20 66 72 6f 6d 20 61 20  o delete from a 
2050f 76 69 65 77 2c 20 72 65 61 6c 69 7a 65 20 74 68  view, realize th
20510 61 74 20 76 69 65 77 20 69 6e 74 6f 0a 20 20 2a  at view into.  *
20511 2a 20 61 20 65 70 68 65 6d 65 72 61 6c 20 74 61  * a ephemeral ta
20512 62 6c 65 2e 0a 20 20 2a 2f 0a 23 69 66 20 21 64  ble..  */.#if !d
20513 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
20514 49 54 5f 56 49 45 57 29 20 26 26 20 21 64 65 66  IT_VIEW) && !def
20515 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
20516 5f 54 52 49 47 47 45 52 29 0a 20 20 69 66 28 20  _TRIGGER).  if( 
20517 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 73 71  isView ){.    sq
20518 6c 69 74 65 33 4d 61 74 65 72 69 61 6c 69 7a 65  lite3Materialize
20519 56 69 65 77 28 70 50 61 72 73 65 2c 20 70 54 61  View(pParse, pTa
2051a 62 2c 20 70 57 68 65 72 65 2c 20 69 43 75 72 29  b, pWhere, iCur)
2051b 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
2051c 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 63  /* Resolve the c
2051d 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 74  olumn names in t
2051e 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e  he WHERE clause.
2051f 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26  .  */.  memset(&
20520 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  sNC, 0, sizeof(s
20521 4e 43 29 29 3b 0a 20 20 73 4e 43 2e 70 50 61 72  NC));.  sNC.pPar
20522 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 73  se = pParse;.  s
20523 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 54  NC.pSrcList = pT
20524 61 62 4c 69 73 74 3b 0a 20 20 69 66 28 20 73 71  abList;.  if( sq
20525 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
20526 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 57 68 65  Names(&sNC, pWhe
20527 72 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  re) ){.    goto 
20528 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61  delete_from_clea
20529 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  nup;.  }..  /* I
2052a 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f  nitialize the co
2052b 75 6e 74 65 72 20 6f 66 20 74 68 65 20 6e 75 6d  unter of the num
2052c 62 65 72 20 6f 66 20 72 6f 77 73 20 64 65 6c 65  ber of rows dele
2052d 74 65 64 2c 20 69 66 0a 20 20 2a 2a 20 77 65 20  ted, if.  ** we 
2052e 61 72 65 20 63 6f 75 6e 74 69 6e 67 20 72 6f 77  are counting row
2052f 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62  s..  */.  if( db
20530 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
20531 5f 43 6f 75 6e 74 52 6f 77 73 20 29 7b 0a 20 20  _CountRows ){.  
20532 20 20 6d 65 6d 43 6e 74 20 3d 20 2b 2b 70 50 61    memCnt = ++pPa
20533 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73  rse->nMem;.    s
20534 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
20535 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
20536 30 2c 20 6d 65 6d 43 6e 74 29 3b 0a 20 20 7d 0a  0, memCnt);.  }.
20537 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
20538 4f 4d 49 54 5f 54 52 55 4e 43 41 54 45 5f 4f 50  OMIT_TRUNCATE_OP
20539 54 49 4d 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20  TIMIZATION.  /* 
2053a 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20 41 20  Special case: A 
2053b 44 45 4c 45 54 45 20 77 69 74 68 6f 75 74 20 61  DELETE without a
2053c 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 64 65   WHERE clause de
2053d 6c 65 74 65 73 20 65 76 65 72 79 74 68 69 6e 67  letes everything
2053e 2e 0a 20 20 2a 2a 20 49 74 20 69 73 20 65 61 73  ..  ** It is eas
2053f 69 65 72 20 6a 75 73 74 20 74 6f 20 65 72 61 73  ier just to eras
20540 65 20 74 68 65 20 77 68 6f 6c 65 20 74 61 62 6c  e the whole tabl
20541 65 2e 20 20 4e 6f 74 65 2c 20 68 6f 77 65 76 65  e.  Note, howeve
20542 72 2c 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 69  r, that.  ** thi
20543 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  s means that the
20544 20 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75 6e   row change coun
20545 74 20 77 69 6c 6c 20 62 65 20 69 6e 63 6f 72 72  t will be incorr
20546 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ect..  */.  if( 
20547 72 63 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f 4f  rcauth==SQLITE_O
20548 4b 20 26 26 20 70 57 68 65 72 65 3d 3d 30 20 26  K && pWhere==0 &
20549 26 20 21 74 72 69 67 67 65 72 73 5f 65 78 69 73  & !triggers_exis
2054a 74 20 26 26 20 21 49 73 56 69 72 74 75 61 6c 28  t && !IsVirtual(
2054b 70 54 61 62 29 20 29 7b 0a 20 20 20 20 61 73 73  pTab) ){.    ass
2054c 65 72 74 28 20 21 69 73 56 69 65 77 20 29 3b 0a  ert( !isView );.
2054d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2054e 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6c 65 61  ddOp3(v, OP_Clea
2054f 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69  r, pTab->tnum, i
20550 44 62 2c 20 6d 65 6d 43 6e 74 29 3b 0a 20 20 20  Db, memCnt);.   
20551 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e 6e 65   if( !pParse->ne
20552 73 74 65 64 20 29 7b 0a 20 20 20 20 20 20 73 71  sted ){.      sq
20553 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
20554 34 28 76 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a  4(v, -1, pTab->z
20555 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29  Name, P4_STATIC)
20556 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28  ;.    }.    for(
20557 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65  pIdx=pTab->pInde
20558 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49  x; pIdx; pIdx=pI
20559 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  dx->pNext){.    
2055a 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e    assert( pIdx->
2055b 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70  pSchema==pTab->p
2055c 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 20 20  Schema );.      
2055d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2055e 32 28 76 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 70  2(v, OP_Clear, p
2055f 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b  Idx->tnum, iDb);
20560 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23  .    }.  }else.#
20561 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
20562 4f 4d 49 54 5f 54 52 55 4e 43 41 54 45 5f 4f 50  OMIT_TRUNCATE_OP
20563 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 20 20  TIMIZATION */.  
20564 2f 2a 20 54 68 65 20 75 73 75 61 6c 20 63 61 73  /* The usual cas
20565 65 3a 20 54 68 65 72 65 20 69 73 20 61 20 57 48  e: There is a WH
20566 45 52 45 20 63 6c 61 75 73 65 20 73 6f 20 77 65  ERE clause so we
20567 20 68 61 76 65 20 74 6f 20 73 63 61 6e 20 74 68   have to scan th
20568 72 6f 75 67 68 0a 20 20 2a 2a 20 74 68 65 20 74  rough.  ** the t
20569 61 62 6c 65 20 61 6e 64 20 70 69 63 6b 20 77 68  able and pick wh
2056a 69 63 68 20 72 65 63 6f 72 64 73 20 74 6f 20 64  ich records to d
2056b 65 6c 65 74 65 2e 0a 20 20 2a 2f 0a 20 20 7b 0a  elete..  */.  {.
2056c 20 20 20 20 69 6e 74 20 69 52 6f 77 69 64 20 3d      int iRowid =
2056d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2056e 20 20 20 20 2f 2a 20 55 73 65 64 20 66 6f 72 20      /* Used for 
2056f 73 74 6f 72 69 6e 67 20 72 6f 77 69 64 20 76 61  storing rowid va
20570 6c 75 65 73 2e 20 2a 2f 0a 0a 20 20 20 20 2f 2a  lues. */..    /*
20571 20 42 65 67 69 6e 20 74 68 65 20 64 61 74 61 62   Begin the datab
20572 61 73 65 20 73 63 61 6e 0a 20 20 20 20 2a 2f 0a  ase scan.    */.
20573 20 20 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c      pWInfo = sql
20574 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 70  ite3WhereBegin(p
20575 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c  Parse, pTabList,
20576 20 70 57 68 65 72 65 2c 20 30 2c 20 30 29 3b 0a   pWhere, 0, 0);.
20577 20 20 20 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d      if( pWInfo==
20578 30 20 29 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f  0 ) goto delete_
20579 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 0a 20  from_cleanup;.. 
2057a 20 20 20 2f 2a 20 52 65 6d 65 6d 62 65 72 20 74     /* Remember t
2057b 68 65 20 72 6f 77 69 64 20 6f 66 20 65 76 65 72  he rowid of ever
2057c 79 20 69 74 65 6d 20 74 6f 20 62 65 20 64 65 6c  y item to be del
2057d 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  eted..    */.   
2057e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2057f 70 32 28 76 2c 20 49 73 56 69 72 74 75 61 6c 28  p2(v, IsVirtual(
20580 70 54 61 62 29 20 3f 20 4f 50 5f 56 52 6f 77 69  pTab) ? OP_VRowi
20581 64 20 3a 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43  d : OP_Rowid, iC
20582 75 72 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20 20  ur, iRowid);.   
20583 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
20584 70 31 28 76 2c 20 4f 50 5f 46 69 66 6f 57 72 69  p1(v, OP_FifoWri
20585 74 65 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20 20  te, iRowid);.   
20586 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26   if( db->flags &
20587 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77   SQLITE_CountRow
20588 73 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  s ){.      sqlit
20589 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2058a 4f 50 5f 41 64 64 49 6d 6d 2c 20 6d 65 6d 43 6e  OP_AddImm, memCn
2058b 74 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  t, 1);.    }..  
2058c 20 20 2f 2a 20 45 6e 64 20 74 68 65 20 64 61 74    /* End the dat
2058d 61 62 61 73 65 20 73 63 61 6e 20 6c 6f 6f 70 2e  abase scan loop.
2058e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
2058f 74 65 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e  te3WhereEnd(pWIn
20590 66 6f 29 3b 0a 0a 20 20 20 20 2f 2a 20 4f 70 65  fo);..    /* Ope
20591 6e 20 74 68 65 20 70 73 65 75 64 6f 2d 74 61 62  n the pseudo-tab
20592 6c 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  le used to store
20593 20 4f 4c 44 20 69 66 20 74 68 65 72 65 20 61 72   OLD if there ar
20594 65 20 74 72 69 67 67 65 72 73 2e 0a 20 20 20 20  e triggers..    
20595 2a 2f 0a 20 20 20 20 69 66 28 20 74 72 69 67 67  */.    if( trigg
20596 65 72 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20  ers_exist ){.   
20597 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
20598 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 74 4e 75  dOp2(v, OP_SetNu
20599 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 70 54 61  mColumns, 0, pTa
2059a 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20  b->nCol);.      
2059b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2059c 31 28 76 2c 20 4f 50 5f 4f 70 65 6e 50 73 65 75  1(v, OP_OpenPseu
2059d 64 6f 2c 20 6f 6c 64 49 64 78 29 3b 0a 20 20 20  do, oldIdx);.   
2059e 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74   }..    /* Delet
2059f 65 20 65 76 65 72 79 20 69 74 65 6d 20 77 68 6f  e every item who
205a0 73 65 20 6b 65 79 20 77 61 73 20 77 72 69 74 74  se key was writt
205a1 65 6e 20 74 6f 20 74 68 65 20 6c 69 73 74 20 64  en to the list d
205a2 75 72 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a  uring the.    **
205a3 20 64 61 74 61 62 61 73 65 20 73 63 61 6e 2e 20   database scan. 
205a4 20 57 65 20 68 61 76 65 20 74 6f 20 64 65 6c 65   We have to dele
205a5 74 65 20 69 74 65 6d 73 20 61 66 74 65 72 20 74  te items after t
205a6 68 65 20 73 63 61 6e 20 69 73 20 63 6f 6d 70 6c  he scan is compl
205a7 65 74 65 0a 20 20 20 20 2a 2a 20 62 65 63 61 75  ete.    ** becau
205a8 73 65 20 64 65 6c 65 74 69 6e 67 20 61 6e 20 69  se deleting an i
205a9 74 65 6d 20 63 61 6e 20 63 68 61 6e 67 65 20 74  tem can change t
205aa 68 65 20 73 63 61 6e 20 6f 72 64 65 72 2e 0a 20  he scan order.. 
205ab 20 20 20 2a 2f 0a 20 20 20 20 65 6e 64 20 3d 20     */.    end = 
205ac 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
205ad 61 62 65 6c 28 76 29 3b 0a 0a 20 20 20 20 69 66  abel(v);..    if
205ae 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20  ( !isView ){.   
205af 20 20 20 2f 2a 20 4f 70 65 6e 20 63 75 72 73 6f     /* Open curso
205b0 72 73 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  rs for the table
205b1 20 77 65 20 61 72 65 20 64 65 6c 65 74 69 6e 67   we are deleting
205b2 20 66 72 6f 6d 20 61 6e 64 20 0a 20 20 20 20 20   from and .     
205b3 20 2a 2a 20 61 6c 6c 20 69 74 73 20 69 6e 64 69   ** all its indi
205b4 63 65 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ces..      */.  
205b5 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54      sqlite3OpenT
205b6 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 70  ableAndIndices(p
205b7 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 43 75  Parse, pTab, iCu
205b8 72 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29  r, OP_OpenWrite)
205b9 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
205ba 54 68 69 73 20 69 73 20 74 68 65 20 62 65 67 69  This is the begi
205bb 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 64 65 6c  nning of the del
205bc 65 74 65 20 6c 6f 6f 70 2e 20 49 66 20 61 20 74  ete loop. If a t
205bd 72 69 67 67 65 72 20 65 6e 63 6f 75 6e 74 65 72  rigger encounter
205be 73 0a 20 20 20 20 2a 2a 20 61 6e 20 49 47 4e 4f  s.    ** an IGNO
205bf 52 45 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 69  RE constraint, i
205c0 74 20 6a 75 6d 70 73 20 62 61 63 6b 20 74 6f 20  t jumps back to 
205c1 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  here..    */.   
205c2 20 69 66 28 20 74 72 69 67 67 65 72 73 5f 65 78   if( triggers_ex
205c3 69 73 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ist ){.      sql
205c4 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
205c5 61 62 65 6c 28 76 2c 20 61 64 64 72 29 3b 0a 20  abel(v, addr);. 
205c6 20 20 20 7d 0a 20 20 20 20 61 64 64 72 20 3d 20     }.    addr = 
205c7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
205c8 32 28 76 2c 20 4f 50 5f 46 69 66 6f 52 65 61 64  2(v, OP_FifoRead
205c9 2c 20 69 52 6f 77 69 64 2c 20 65 6e 64 29 3b 0a  , iRowid, end);.
205ca 0a 20 20 20 20 69 66 28 20 74 72 69 67 67 65 72  .    if( trigger
205cb 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20 20  s_exist ){.     
205cc 20 69 6e 74 20 69 44 61 74 61 20 3d 20 2b 2b 70   int iData = ++p
205cd 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 20 20 20 2f  Parse->nMem;   /
205ce 2a 20 46 6f 72 20 73 74 6f 72 69 6e 67 20 72 6f  * For storing ro
205cf 77 20 64 61 74 61 20 6f 66 20 4f 4c 44 20 74 61  w data of OLD ta
205d0 62 6c 65 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a  ble */..      /*
205d1 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 69   If the record i
205d2 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 70 72 65 73  s no longer pres
205d3 65 6e 74 20 69 6e 20 74 68 65 20 74 61 62 6c 65  ent in the table
205d4 2c 20 6a 75 6d 70 20 74 6f 20 74 68 65 0a 20 20  , jump to the.  
205d5 20 20 20 20 2a 2a 20 6e 65 78 74 20 69 74 65 72      ** next iter
205d6 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 6f 6f  ation of the loo
205d7 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 63 6f  p through the co
205d8 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 66 69  ntents of the fi
205d9 66 6f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  fo..      */.   
205da 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
205db 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78  dOp3(v, OP_NotEx
205dc 69 73 74 73 2c 20 69 43 75 72 2c 20 61 64 64 72  ists, iCur, addr
205dd 2c 20 69 52 6f 77 69 64 29 3b 0a 0a 20 20 20 20  , iRowid);..    
205de 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68    /* Populate th
205df 65 20 4f 4c 44 2e 2a 20 70 73 65 75 64 6f 2d 74  e OLD.* pseudo-t
205e0 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 69 66  able */.      if
205e1 28 20 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 20 29  ( old_col_mask )
205e2 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
205e3 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
205e4 50 5f 52 6f 77 44 61 74 61 2c 20 69 43 75 72 2c  P_RowData, iCur,
205e5 20 69 44 61 74 61 29 3b 0a 20 20 20 20 20 20 7d   iData);.      }
205e6 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
205e7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
205e8 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69  v, OP_Null, 0, i
205e9 44 61 74 61 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Data);.      }. 
205ea 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
205eb 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73  AddOp3(v, OP_Ins
205ec 65 72 74 2c 20 6f 6c 64 49 64 78 2c 20 69 44 61  ert, oldIdx, iDa
205ed 74 61 2c 20 69 52 6f 77 69 64 29 3b 0a 0a 20 20  ta, iRowid);..  
205ee 20 20 20 20 2f 2a 20 4a 75 6d 70 20 62 61 63 6b      /* Jump back
205ef 20 61 6e 64 20 72 75 6e 20 74 68 65 20 42 45 46   and run the BEF
205f0 4f 52 45 20 74 72 69 67 67 65 72 73 20 2a 2f 0a  ORE triggers */.
205f1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
205f2 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
205f3 74 6f 2c 20 30 2c 20 69 42 65 67 69 6e 42 65 66  to, 0, iBeginBef
205f4 6f 72 65 54 72 69 67 67 65 72 29 3b 0a 20 20 20  oreTrigger);.   
205f5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
205f6 6d 70 48 65 72 65 28 76 2c 20 69 45 6e 64 42 65  mpHere(v, iEndBe
205f7 66 6f 72 65 54 72 69 67 67 65 72 29 3b 0a 20 20  foreTrigger);.  
205f8 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 21 69 73    }..    if( !is
205f9 56 69 65 77 20 29 7b 0a 20 20 20 20 20 20 2f 2a  View ){.      /*
205fa 20 44 65 6c 65 74 65 20 74 68 65 20 72 6f 77 20   Delete the row 
205fb 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
205fc 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
205fd 42 4c 45 0a 20 20 20 20 20 20 69 66 28 20 49 73  BLE.      if( Is
205fe 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b  Virtual(pTab) ){
205ff 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63  .        const c
20600 68 61 72 20 2a 70 56 74 61 62 20 3d 20 28 63 6f  har *pVtab = (co
20601 6e 73 74 20 63 68 61 72 20 2a 29 70 54 61 62 2d  nst char *)pTab-
20602 3e 70 56 74 61 62 3b 0a 20 20 20 20 20 20 20 20  >pVtab;.        
20603 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b 65 57  sqlite3VtabMakeW
20604 72 69 74 61 62 6c 65 28 70 50 61 72 73 65 2c 20  ritable(pParse, 
20605 70 54 61 62 29 3b 0a 20 20 20 20 20 20 20 20 73  pTab);.        s
20606 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
20607 28 76 2c 20 4f 50 5f 56 55 70 64 61 74 65 2c 20  (v, OP_VUpdate, 
20608 30 2c 20 31 2c 20 69 52 6f 77 69 64 2c 20 70 56  0, 1, iRowid, pV
20609 74 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20  tab, P4_VTAB);. 
2060a 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69       }else.#endi
2060b 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20  f.      {.      
2060c 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74    sqlite3Generat
2060d 65 52 6f 77 44 65 6c 65 74 65 28 70 50 61 72 73  eRowDelete(pPars
2060e 65 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20 69  e, pTab, iCur, i
2060f 52 6f 77 69 64 2c 20 70 50 61 72 73 65 2d 3e 6e  Rowid, pParse->n
20610 65 73 74 65 64 3d 3d 30 29 3b 0a 20 20 20 20 20  ested==0);.     
20611 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a   }.    }..    /*
20612 20 49 66 20 74 68 65 72 65 20 61 72 65 20 72 6f   If there are ro
20613 77 20 74 72 69 67 67 65 72 73 2c 20 63 6c 6f 73  w triggers, clos
20614 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 74 68  e all cursors th
20615 65 6e 20 69 6e 76 6f 6b 65 0a 20 20 20 20 2a 2a  en invoke.    **
20616 20 74 68 65 20 41 46 54 45 52 20 74 72 69 67 67   the AFTER trigg
20617 65 72 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ers.    */.    i
20618 66 28 20 74 72 69 67 67 65 72 73 5f 65 78 69 73  f( triggers_exis
20619 74 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4a 75  t ){.      /* Ju
2061a 6d 70 20 62 61 63 6b 20 61 6e 64 20 72 75 6e 20  mp back and run 
2061b 74 68 65 20 41 46 54 45 52 20 74 72 69 67 67 65  the AFTER trigge
2061c 72 73 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  rs */.      sqli
2061d 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2061e 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 42 65   OP_Goto, 0, iBe
2061f 67 69 6e 41 66 74 65 72 54 72 69 67 67 65 72 29  ginAfterTrigger)
20620 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
20621 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69  dbeJumpHere(v, i
20622 45 6e 64 41 66 74 65 72 54 72 69 67 67 65 72 29  EndAfterTrigger)
20623 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
20624 45 6e 64 20 6f 66 20 74 68 65 20 64 65 6c 65 74  End of the delet
20625 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20 73 71  e loop */.    sq
20626 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
20627 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61  v, OP_Goto, 0, a
20628 64 64 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ddr);.    sqlite
20629 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
2062a 6c 28 76 2c 20 65 6e 64 29 3b 0a 0a 20 20 20 20  l(v, end);..    
2062b 2f 2a 20 43 6c 6f 73 65 20 74 68 65 20 63 75 72  /* Close the cur
2062c 73 6f 72 73 20 61 66 74 65 72 20 74 68 65 20 6c  sors after the l
2062d 6f 6f 70 20 69 66 20 74 68 65 72 65 20 61 72 65  oop if there are
2062e 20 6e 6f 20 72 6f 77 20 74 72 69 67 67 65 72 73   no row triggers
2062f 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 69 73 56   */.    if( !isV
20630 69 65 77 20 20 26 26 20 21 49 73 56 69 72 74 75  iew  && !IsVirtu
20631 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
20632 20 20 66 6f 72 28 69 3d 31 2c 20 70 49 64 78 3d    for(i=1, pIdx=
20633 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
20634 64 78 3b 20 69 2b 2b 2c 20 70 49 64 78 3d 70 49  dx; i++, pIdx=pI
20635 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  dx->pNext){.    
20636 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
20637 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp2(v, OP_Clos
20638 65 2c 20 69 43 75 72 20 2b 20 69 2c 20 70 49 64  e, iCur + i, pId
20639 78 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20  x->tnum);.      
2063a 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  }.      sqlite3V
2063b 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2063c 43 6c 6f 73 65 2c 20 69 43 75 72 29 3b 0a 20 20  Close, iCur);.  
2063d 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20    }.  }..  /*.  
2063e 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
2063f 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61  mber of rows tha
20640 74 20 77 65 72 65 20 64 65 6c 65 74 65 64 2e 20  t were deleted. 
20641 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  If this routine 
20642 69 73 20 0a 20 20 2a 2a 20 67 65 6e 65 72 61 74  is .  ** generat
20643 69 6e 67 20 63 6f 64 65 20 62 65 63 61 75 73 65  ing code because
20644 20 6f 66 20 61 20 63 61 6c 6c 20 74 6f 20 73 71   of a call to sq
20645 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
20646 28 29 2c 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20  (), do not.  ** 
20647 69 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62  invoke the callb
20648 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20  ack function..  
20649 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61  */.  if( db->fla
2064a 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e  gs & SQLITE_Coun
2064b 74 52 6f 77 73 20 26 26 20 70 50 61 72 73 65 2d  tRows && pParse-
2064c 3e 6e 65 73 74 65 64 3d 3d 30 20 26 26 20 21 70  >nested==0 && !p
2064d 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b  Parse->trigStack
2064e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
2064f 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
20650 52 65 73 75 6c 74 52 6f 77 2c 20 6d 65 6d 43 6e  ResultRow, memCn
20651 74 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  t, 1);.    sqlit
20652 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
20653 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  (v, 1);.    sqli
20654 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
20655 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
20656 4e 41 4d 45 2c 20 22 72 6f 77 73 20 64 65 6c 65  NAME, "rows dele
20657 74 65 64 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ted", SQLITE_STA
20658 54 49 43 29 3b 0a 20 20 7d 0a 0a 64 65 6c 65 74  TIC);.  }..delet
20659 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3a 0a  e_from_cleanup:.
2065a 20 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e    sqlite3AuthCon
2065b 74 65 78 74 50 6f 70 28 26 73 43 6f 6e 74 65 78  textPop(&sContex
2065c 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 72 63  t);.  sqlite3Src
2065d 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
2065e 54 61 62 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69  TabList);.  sqli
2065f 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
20660 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 72 65 74  , pWhere);.  ret
20661 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  urn;.}../*.** Th
20662 69 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72  is routine gener
20663 61 74 65 73 20 56 44 42 45 20 63 6f 64 65 20 74  ates VDBE code t
20664 68 61 74 20 63 61 75 73 65 73 20 61 20 73 69 6e  hat causes a sin
20665 67 6c 65 20 72 6f 77 20 6f 66 20 61 0a 2a 2a 20  gle row of a.** 
20666 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 74 6f 20  single table to 
20667 62 65 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a 2a  be deleted..**.*
20668 2a 20 54 68 65 20 56 44 42 45 20 6d 75 73 74 20  * The VDBE must 
20669 62 65 20 69 6e 20 61 20 70 61 72 74 69 63 75 6c  be in a particul
2066a 61 72 20 73 74 61 74 65 20 77 68 65 6e 20 74 68  ar state when th
2066b 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
2066c 6c 6c 65 64 2e 0a 2a 2a 20 54 68 65 73 65 20 61  lled..** These a
2066d 72 65 20 74 68 65 20 72 65 71 75 69 72 65 6d 65  re the requireme
2066e 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20  nts:.**.**   1. 
2066f 20 41 20 72 65 61 64 2f 77 72 69 74 65 20 63 75   A read/write cu
20670 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f  rsor pointing to
20671 20 70 54 61 62 2c 20 74 68 65 20 74 61 62 6c 65   pTab, the table
20672 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
20673 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 74 6f 20  row.**       to 
20674 62 65 20 64 65 6c 65 74 65 64 2c 20 6d 75 73 74  be deleted, must
20675 20 62 65 20 6f 70 65 6e 65 64 20 61 73 20 63 75   be opened as cu
20676 72 73 6f 72 20 6e 75 6d 62 65 72 20 22 62 61 73  rsor number "bas
20677 65 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 20  e"..**.**   2.  
20678 52 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f  Read/write curso
20679 72 73 20 66 6f 72 20 61 6c 6c 20 69 6e 64 69 63  rs for all indic
2067a 65 73 20 6f 66 20 70 54 61 62 20 6d 75 73 74 20  es of pTab must 
2067b 62 65 20 6f 70 65 6e 20 61 73 0a 2a 2a 20 20 20  be open as.**   
2067c 20 20 20 20 63 75 72 73 6f 72 20 6e 75 6d 62 65      cursor numbe
2067d 72 20 62 61 73 65 2b 69 20 66 6f 72 20 74 68 65  r base+i for the
2067e 20 69 2d 74 68 20 69 6e 64 65 78 2e 0a 2a 2a 0a   i-th index..**.
2067f 2a 2a 20 20 20 33 2e 20 20 54 68 65 20 72 65 63  **   3.  The rec
20680 6f 72 64 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  ord number of th
20681 65 20 72 6f 77 20 74 6f 20 62 65 20 64 65 6c 65  e row to be dele
20682 74 65 64 20 6d 75 73 74 20 62 65 20 73 74 6f 72  ted must be stor
20683 65 64 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 6d  ed in.**       m
20684 65 6d 6f 72 79 20 63 65 6c 6c 20 69 52 6f 77 69  emory cell iRowi
20685 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  d..**.** This ro
20686 75 74 69 6e 65 20 70 6f 70 73 20 74 68 65 20 74  utine pops the t
20687 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20  op of the stack 
20688 74 6f 20 72 65 6d 6f 76 65 20 74 68 65 20 72 65  to remove the re
20689 63 6f 72 64 20 6e 75 6d 62 65 72 0a 2a 2a 20 61  cord number.** a
2068a 6e 64 20 74 68 65 6e 20 67 65 6e 65 72 61 74 65  nd then generate
2068b 73 20 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65  s code to remove
2068c 20 62 6f 74 68 20 74 68 65 20 74 61 62 6c 65 20   both the table 
2068d 72 65 63 6f 72 64 20 61 6e 64 20 61 6c 6c 20 69  record and all i
2068e 6e 64 65 78 0a 2a 2a 20 65 6e 74 72 69 65 73 20  ndex.** entries 
2068f 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 74 68  that point to th
20690 61 74 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 53 51  at record..*/.SQ
20691 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
20692 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74  d sqlite3Generat
20693 65 52 6f 77 44 65 6c 65 74 65 28 0a 20 20 50 61  eRowDelete(.  Pa
20694 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
20695 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
20696 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ext */.  Table *
20697 70 54 61 62 2c 20 20 20 20 20 20 20 2f 2a 20 54  pTab,       /* T
20698 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  able containing 
20699 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20 64 65  the row to be de
2069a 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  leted */.  int i
2069b 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Cur,          /*
2069c 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66   Cursor number f
2069d 6f 72 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  or the table */.
2069e 20 20 69 6e 74 20 69 52 6f 77 69 64 2c 20 20 20    int iRowid,   
2069f 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63       /* Memory c
206a0 65 6c 6c 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ell that contain
206a1 73 20 74 68 65 20 72 6f 77 69 64 20 74 6f 20 64  s the rowid to d
206a2 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 63  elete */.  int c
206a3 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20 2f 2a  ount          /*
206a4 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72   Increment the r
206a5 6f 77 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65  ow change counte
206a6 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 64  r */.){.  int ad
206a7 64 72 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a  dr;.  Vdbe *v;..
206a8 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56    v = pParse->pV
206a9 64 62 65 3b 0a 20 20 61 64 64 72 20 3d 20 73 71  dbe;.  addr = sq
206aa 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
206ab 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c  v, OP_NotExists,
206ac 20 69 43 75 72 2c 20 30 2c 20 69 52 6f 77 69 64   iCur, 0, iRowid
206ad 29 3b 0a 20 20 73 71 6c 69 74 65 33 47 65 6e 65  );.  sqlite3Gene
206ae 72 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65  rateRowIndexDele
206af 74 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  te(pParse, pTab,
206b0 20 69 43 75 72 2c 20 30 29 3b 0a 20 20 73 71 6c   iCur, 0);.  sql
206b1 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
206b2 2c 20 4f 50 5f 44 65 6c 65 74 65 2c 20 69 43 75  , OP_Delete, iCu
206b3 72 2c 20 28 63 6f 75 6e 74 3f 4f 50 46 4c 41 47  r, (count?OPFLAG
206b4 5f 4e 43 48 41 4e 47 45 3a 30 29 29 3b 0a 20 20  _NCHANGE:0));.  
206b5 69 66 28 20 63 6f 75 6e 74 20 29 7b 0a 20 20 20  if( count ){.   
206b6 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
206b7 67 65 50 34 28 76 2c 20 2d 31 2c 20 70 54 61 62  geP4(v, -1, pTab
206b8 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54  ->zName, P4_STAT
206b9 49 43 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  IC);.  }.  sqlit
206ba 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
206bb 2c 20 61 64 64 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  , addr);.}../*.*
206bc 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67  * This routine g
206bd 65 6e 65 72 61 74 65 73 20 56 44 42 45 20 63 6f  enerates VDBE co
206be 64 65 20 74 68 61 74 20 63 61 75 73 65 73 20 74  de that causes t
206bf 68 65 20 64 65 6c 65 74 69 6f 6e 20 6f 66 20 61  he deletion of a
206c0 6c 6c 0a 2a 2a 20 69 6e 64 65 78 20 65 6e 74 72  ll.** index entr
206c1 69 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ies associated w
206c2 69 74 68 20 61 20 73 69 6e 67 6c 65 20 72 6f 77  ith a single row
206c3 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62   of a single tab
206c4 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 44  le..**.** The VD
206c5 42 45 20 6d 75 73 74 20 62 65 20 69 6e 20 61 20  BE must be in a 
206c6 70 61 72 74 69 63 75 6c 61 72 20 73 74 61 74 65  particular state
206c7 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   when this routi
206c8 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a  ne is called..**
206c9 20 54 68 65 73 65 20 61 72 65 20 74 68 65 20 72   These are the r
206ca 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 0a  equirements:.**.
206cb 2a 2a 20 20 20 31 2e 20 20 41 20 72 65 61 64 2f  **   1.  A read/
206cc 77 72 69 74 65 20 63 75 72 73 6f 72 20 70 6f 69  write cursor poi
206cd 6e 74 69 6e 67 20 74 6f 20 70 54 61 62 2c 20 74  nting to pTab, t
206ce 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e  he table contain
206cf 69 6e 67 20 74 68 65 20 72 6f 77 0a 2a 2a 20 20  ing the row.**  
206d0 20 20 20 20 20 74 6f 20 62 65 20 64 65 6c 65 74       to be delet
206d1 65 64 2c 20 6d 75 73 74 20 62 65 20 6f 70 65 6e  ed, must be open
206d2 65 64 20 61 73 20 63 75 72 73 6f 72 20 6e 75 6d  ed as cursor num
206d3 62 65 72 20 22 69 43 75 72 22 2e 0a 2a 2a 0a 2a  ber "iCur"..**.*
206d4 2a 20 20 20 32 2e 20 20 52 65 61 64 2f 77 72 69  *   2.  Read/wri
206d5 74 65 20 63 75 72 73 6f 72 73 20 66 6f 72 20 61  te cursors for a
206d6 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 70 54  ll indices of pT
206d7 61 62 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20  ab must be open 
206d8 61 73 0a 2a 2a 20 20 20 20 20 20 20 63 75 72 73  as.**       curs
206d9 6f 72 20 6e 75 6d 62 65 72 20 69 43 75 72 2b 69  or number iCur+i
206da 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 69 6e   for the i-th in
206db 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20  dex..**.**   3. 
206dc 20 54 68 65 20 22 69 43 75 72 22 20 63 75 72 73   The "iCur" curs
206dd 6f 72 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74  or must be point
206de 69 6e 67 20 74 6f 20 74 68 65 20 72 6f 77 20 74  ing to the row t
206df 68 61 74 20 69 73 20 74 6f 20 62 65 0a 2a 2a 20  hat is to be.** 
206e0 20 20 20 20 20 20 64 65 6c 65 74 65 64 2e 0a 2a        deleted..*
206e1 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
206e2 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65 6e   void sqlite3Gen
206e3 65 72 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c  erateRowIndexDel
206e4 65 74 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ete(.  Parse *pP
206e5 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72  arse,     /* Par
206e6 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65  sing and code ge
206e7 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74  nerating context
206e8 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
206e9 62 2c 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c  b,       /* Tabl
206ea 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
206eb 20 72 6f 77 20 74 6f 20 62 65 20 64 65 6c 65 74   row to be delet
206ec 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72  ed */.  int iCur
206ed 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75  ,          /* Cu
206ee 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20  rsor number for 
206ef 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  the table */.  i
206f0 6e 74 20 2a 61 52 65 67 49 64 78 20 20 20 20 20  nt *aRegIdx     
206f1 20 20 2f 2a 20 4f 6e 6c 79 20 64 65 6c 65 74 65    /* Only delete
206f2 20 69 66 20 61 52 65 67 49 64 78 21 3d 30 20 26   if aRegIdx!=0 &
206f3 26 20 61 52 65 67 49 64 78 5b 69 5d 3e 30 20 2a  & aRegIdx[i]>0 *
206f4 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  /.){.  int i;.  
206f5 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 69  Index *pIdx;.  i
206f6 6e 74 20 72 31 3b 0a 0a 20 20 66 6f 72 28 69 3d  nt r1;..  for(i=
206f7 31 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  1, pIdx=pTab->pI
206f8 6e 64 65 78 3b 20 70 49 64 78 3b 20 69 2b 2b 2c  ndex; pIdx; i++,
206f9 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
206fa 74 29 7b 0a 20 20 20 20 69 66 28 20 61 52 65 67  t){.    if( aReg
206fb 49 64 78 21 3d 30 20 26 26 20 61 52 65 67 49 64  Idx!=0 && aRegId
206fc 78 5b 69 2d 31 5d 3d 3d 30 20 29 20 63 6f 6e 74  x[i-1]==0 ) cont
206fd 69 6e 75 65 3b 0a 20 20 20 20 72 31 20 3d 20 73  inue;.    r1 = s
206fe 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e  qlite3GenerateIn
206ff 64 65 78 4b 65 79 28 70 50 61 72 73 65 2c 20 70  dexKey(pParse, p
20700 49 64 78 2c 20 69 43 75 72 2c 20 30 2c 20 30 29  Idx, iCur, 0, 0)
20701 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
20702 65 41 64 64 4f 70 33 28 70 50 61 72 73 65 2d 3e  eAddOp3(pParse->
20703 70 56 64 62 65 2c 20 4f 50 5f 49 64 78 44 65 6c  pVdbe, OP_IdxDel
20704 65 74 65 2c 20 69 43 75 72 2b 69 2c 20 72 31 2c  ete, iCur+i, r1,
20705 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29  pIdx->nColumn+1)
20706 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  ;.  }.}../*.** G
20707 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
20708 74 20 77 69 6c 6c 20 61 73 73 65 6d 62 6c 65 20  t will assemble 
20709 61 6e 20 69 6e 64 65 78 20 6b 65 79 20 61 6e 64  an index key and
2070a 20 70 75 74 20 69 74 20 69 6e 20 72 65 67 69 73   put it in regis
2070b 74 65 72 0a 2a 2a 20 72 65 67 4f 75 74 2e 20 20  ter.** regOut.  
2070c 54 68 65 20 6b 65 79 20 77 69 74 68 20 62 65 20  The key with be 
2070d 66 6f 72 20 69 6e 64 65 78 20 70 49 64 78 20 77  for index pIdx w
2070e 68 69 63 68 20 69 73 20 61 6e 20 69 6e 64 65 78  hich is an index
2070f 20 6f 6e 20 70 54 61 62 2e 0a 2a 2a 20 69 43 75   on pTab..** iCu
20710 72 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f  r is the index o
20711 66 20 61 20 63 75 72 73 6f 72 20 6f 70 65 6e 20  f a cursor open 
20712 6f 6e 20 74 68 65 20 70 54 61 62 20 74 61 62 6c  on the pTab tabl
20713 65 20 61 6e 64 20 70 6f 69 6e 74 69 6e 67 20 74  e and pointing t
20714 6f 0a 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 74  o.** the entry t
20715 68 61 74 20 6e 65 65 64 73 20 69 6e 64 65 78 69  hat needs indexi
20716 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  ng..**.** Return
20717 20 61 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62   a register numb
20718 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20  er which is the 
20719 66 69 72 73 74 20 69 6e 20 61 20 62 6c 6f 63 6b  first in a block
2071a 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 73   of.** registers
2071b 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20   that holds the 
2071c 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20  elements of the 
2071d 69 6e 64 65 78 20 6b 65 79 2e 20 20 54 68 65 0a  index key.  The.
2071e 2a 2a 20 62 6c 6f 63 6b 20 6f 66 20 72 65 67 69  ** block of regi
2071f 73 74 65 72 73 20 68 61 73 20 61 6c 72 65 61 64  sters has alread
20720 79 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63 61 74  y been deallocat
20721 65 64 20 62 79 20 74 68 65 20 74 69 6d 65 0a 2a  ed by the time.*
20722 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * this routine r
20723 65 74 75 72 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54  eturns..*/.SQLIT
20724 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
20725 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64  lite3GenerateInd
20726 65 78 4b 65 79 28 0a 20 20 50 61 72 73 65 20 2a  exKey(.  Parse *
20727 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50  pParse,     /* P
20728 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
20729 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 2c  /.  Index *pIdx,
2072a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e         /* The in
2072b 64 65 78 20 66 6f 72 20 77 68 69 63 68 20 74 6f  dex for which to
2072c 20 67 65 6e 65 72 61 74 65 20 61 20 6b 65 79 20   generate a key 
2072d 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20  */.  int iCur,  
2072e 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f          /* Curso
2072f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  r number for the
20730 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 20 74 61   pIdx->pTable ta
20731 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ble */.  int reg
20732 4f 75 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 57  Out,        /* W
20733 72 69 74 65 20 74 68 65 20 6e 65 77 20 69 6e 64  rite the new ind
20734 65 78 20 6b 65 79 20 74 6f 20 74 68 69 73 20 72  ex key to this r
20735 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74  egister */.  int
20736 20 64 6f 4d 61 6b 65 52 65 63 20 20 20 20 20 20   doMakeRec      
20737 2f 2a 20 52 75 6e 20 74 68 65 20 4f 50 5f 4d 61  /* Run the OP_Ma
20738 6b 65 52 65 63 6f 72 64 20 69 6e 73 74 72 75 63  keRecord instruc
20739 74 69 6f 6e 20 69 66 20 74 72 75 65 20 2a 2f 0a  tion if true */.
2073a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  ){.  Vdbe *v = p
2073b 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
2073c 69 6e 74 20 6a 3b 0a 20 20 54 61 62 6c 65 20 2a  int j;.  Table *
2073d 70 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54 61  pTab = pIdx->pTa
2073e 62 6c 65 3b 0a 20 20 69 6e 74 20 72 65 67 42 61  ble;.  int regBa
2073f 73 65 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a  se;.  int nCol;.
20740 0a 20 20 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e  .  nCol = pIdx->
20741 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 72 65 67 42 61  nColumn;.  regBa
20742 73 65 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  se = sqlite3GetT
20743 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
20744 20 6e 43 6f 6c 2b 31 29 3b 0a 20 20 73 71 6c 69   nCol+1);.  sqli
20745 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
20746 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c   OP_Rowid, iCur,
20747 20 72 65 67 42 61 73 65 2b 6e 43 6f 6c 29 3b 0a   regBase+nCol);.
20748 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 43 6f    for(j=0; j<nCo
20749 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 69 6e 74  l; j++){.    int
2074a 20 69 64 78 20 3d 20 70 49 64 78 2d 3e 61 69 43   idx = pIdx->aiC
2074b 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20 20 69 66  olumn[j];.    if
2074c 28 20 69 64 78 3d 3d 70 54 61 62 2d 3e 69 50 4b  ( idx==pTab->iPK
2074d 65 79 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ey ){.      sqli
2074e 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2074f 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 42 61   OP_SCopy, regBa
20750 73 65 2b 6e 43 6f 6c 2c 20 72 65 67 42 61 73 65  se+nCol, regBase
20751 2b 6a 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  +j);.    }else{.
20752 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
20753 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
20754 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 69 64 78 2c  lumn, iCur, idx,
20755 20 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20 20 20   regBase+j);.   
20756 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e     sqlite3Column
20757 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62 2c  Default(v, pTab,
20758 20 69 64 78 29 3b 0a 20 20 20 20 7d 0a 20 20 7d   idx);.    }.  }
20759 0a 20 20 69 66 28 20 64 6f 4d 61 6b 65 52 65 63  .  if( doMakeRec
2075a 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
2075b 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2075c 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 42  MakeRecord, regB
2075d 61 73 65 2c 20 6e 43 6f 6c 2b 31 2c 20 72 65 67  ase, nCol+1, reg
2075e 4f 75 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Out);.    sqlite
2075f 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 53 74  3IndexAffinitySt
20760 72 28 76 2c 20 70 49 64 78 29 3b 0a 20 20 20 20  r(v, pIdx);.    
20761 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
20762 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70  AffinityChange(p
20763 50 61 72 73 65 2c 20 72 65 67 42 61 73 65 2c 20  Parse, regBase, 
20764 6e 43 6f 6c 2b 31 29 3b 0a 20 20 7d 0a 20 20 73  nCol+1);.  }.  s
20765 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
20766 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  pRange(pParse, r
20767 65 67 42 61 73 65 2c 20 6e 43 6f 6c 2b 31 29 3b  egBase, nCol+1);
20768 0a 20 20 72 65 74 75 72 6e 20 72 65 67 42 61 73  .  return regBas
20769 65 3b 0a 7d 0a 0a 2f 2a 20 4d 61 6b 65 20 73 75  e;.}../* Make su
2076a 72 65 20 22 69 73 56 69 65 77 22 20 67 65 74 73  re "isView" gets
2076b 20 75 6e 64 65 66 69 6e 65 64 20 69 6e 20 63 61   undefined in ca
2076c 73 65 20 74 68 69 73 20 66 69 6c 65 20 62 65 63  se this file bec
2076d 6f 6d 65 73 20 70 61 72 74 20 6f 66 0a 2a 2a 20  omes part of.** 
2076e 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e  the amalgamation
2076f 20 2d 20 73 6f 20 74 68 61 74 20 73 75 62 73 65   - so that subse
20770 71 75 65 6e 74 20 66 69 6c 65 73 20 64 6f 20 6e  quent files do n
20771 6f 74 20 73 65 65 20 69 73 56 69 65 77 20 61 73  ot see isView as
20772 20 61 0a 2a 2a 20 6d 61 63 72 6f 2e 20 2a 2f 0a   a.** macro. */.
20773 23 75 6e 64 65 66 20 69 73 56 69 65 77 0a 0a 2f  #undef isView../
20774 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
20775 6e 64 20 6f 66 20 64 65 6c 65 74 65 2e 63 20 2a  nd of delete.c *
20776 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20777 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20778 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
20779 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
2077a 65 67 69 6e 20 66 69 6c 65 20 66 75 6e 63 2e 63  egin file func.c
2077b 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2077c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2077d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
2077e 2a 0a 2a 2a 20 32 30 30 32 20 46 65 62 72 75 61  *.** 2002 Februa
2077f 72 79 20 32 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ry 23.**.** The 
20780 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
20781 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
20782 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
20783 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
20784 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
20785 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
20786 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
20787 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
20788 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
20789 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
2078a 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
2078b 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
2078c 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
2078d 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
2078e 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
2078f 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
20790 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
20791 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20792 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20793 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20794 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20795 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
20796 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43  e contains the C
20797 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20   functions that 
20798 69 6d 70 6c 65 6d 65 6e 74 20 76 61 72 69 6f 75  implement variou
20799 73 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f  s SQL.** functio
2079a 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 0a  ns of SQLite.  .
2079b 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6f  **.** There is o
2079c 6e 6c 79 20 6f 6e 65 20 65 78 70 6f 72 74 65 64  nly one exported
2079d 20 73 79 6d 62 6f 6c 20 69 6e 20 74 68 69 73 20   symbol in this 
2079e 66 69 6c 65 20 2d 20 74 68 65 20 66 75 6e 63 74  file - the funct
2079f 69 6f 6e 0a 2a 2a 20 73 71 6c 69 74 65 52 65 67  ion.** sqliteReg
207a0 69 73 74 65 72 42 75 69 6c 64 69 6e 46 75 6e 63  isterBuildinFunc
207a1 74 69 6f 6e 73 28 29 20 66 6f 75 6e 64 20 61 74  tions() found at
207a2 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74   the bottom of t
207a3 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 41 6c 6c 20  he file..** All 
207a4 6f 74 68 65 72 20 63 6f 64 65 20 68 61 73 20 66  other code has f
207a5 69 6c 65 20 73 63 6f 70 65 2e 0a 2a 2a 0a 2a 2a  ile scope..**.**
207a6 20 24 49 64 3a 20 66 75 6e 63 2e 63 2c 76 20 31   $Id: func.c,v 1
207a7 2e 32 30 34 20 32 30 30 38 2f 31 30 2f 32 38 20  .204 2008/10/28 
207a8 31 37 3a 35 32 3a 33 39 20 64 61 6e 69 65 6c 6b  17:52:39 danielk
207a9 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f  1977 Exp $.*/../
207aa 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
207ab 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69  collating functi
207ac 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  on associated wi
207ad 74 68 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  th a function..*
207ae 2f 0a 73 74 61 74 69 63 20 43 6f 6c 6c 53 65 71  /.static CollSeq
207af 20 2a 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63   *sqlite3GetFunc
207b0 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 5f  CollSeq(sqlite3_
207b1 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
207b2 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6e 74  ){.  return cont
207b3 65 78 74 2d 3e 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f  ext->pColl;.}../
207b4 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
207b5 69 6f 6e 20 6f 66 20 74 68 65 20 6e 6f 6e 2d 61  ion of the non-a
207b6 67 67 72 65 67 61 74 65 20 6d 69 6e 28 29 20 61  ggregate min() a
207b7 6e 64 20 6d 61 78 28 29 20 66 75 6e 63 74 69 6f  nd max() functio
207b8 6e 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ns.*/.static voi
207b9 64 20 6d 69 6e 6d 61 78 46 75 6e 63 28 0a 20 20  d minmaxFunc(.  
207ba 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
207bb 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
207bc 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
207bd 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
207be 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6d    int i;.  int m
207bf 61 73 6b 3b 20 20 20 20 2f 2a 20 30 20 66 6f 72  ask;    /* 0 for
207c0 20 6d 69 6e 28 29 20 6f 72 20 30 78 66 66 66 66   min() or 0xffff
207c1 66 66 66 66 20 66 6f 72 20 6d 61 78 28 29 20 2a  ffff for max() *
207c2 2f 0a 20 20 69 6e 74 20 69 42 65 73 74 3b 0a 20  /.  int iBest;. 
207c3 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
207c4 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 30 20  ..  if( argc==0 
207c5 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d 61 73 6b  ) return;.  mask
207c6 20 3d 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f   = sqlite3_user_
207c7 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3d 3d 30  data(context)==0
207c8 20 3f 20 30 20 3a 20 2d 31 3b 0a 20 20 70 43 6f   ? 0 : -1;.  pCo
207c9 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 46  ll = sqlite3GetF
207ca 75 6e 63 43 6f 6c 6c 53 65 71 28 63 6f 6e 74 65  uncCollSeq(conte
207cb 78 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  xt);.  assert( p
207cc 43 6f 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 74  Coll );.  assert
207cd 28 20 6d 61 73 6b 3d 3d 2d 31 20 7c 7c 20 6d 61  ( mask==-1 || ma
207ce 73 6b 3d 3d 30 20 29 3b 0a 20 20 69 42 65 73 74  sk==0 );.  iBest
207cf 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69   = 0;.  if( sqli
207d0 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
207d1 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f  rgv[0])==SQLITE_
207d2 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20  NULL ) return;. 
207d3 20 66 6f 72 28 69 3d 31 3b 20 69 3c 61 72 67 63   for(i=1; i<argc
207d4 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
207d5 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
207d6 70 65 28 61 72 67 76 5b 69 5d 29 3d 3d 53 51 4c  pe(argv[i])==SQL
207d7 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72  ITE_NULL ) retur
207d8 6e 3b 0a 20 20 20 20 69 66 28 20 28 73 71 6c 69  n;.    if( (sqli
207d9 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 61 72  te3MemCompare(ar
207da 67 76 5b 69 42 65 73 74 5d 2c 20 61 72 67 76 5b  gv[iBest], argv[
207db 69 5d 2c 20 70 43 6f 6c 6c 29 5e 6d 61 73 6b 29  i], pColl)^mask)
207dc 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 42 65  >=0 ){.      iBe
207dd 73 74 20 3d 20 69 3b 0a 20 20 20 20 7d 0a 20 20  st = i;.    }.  
207de 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  }.  sqlite3_resu
207df 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74  lt_value(context
207e0 2c 20 61 72 67 76 5b 69 42 65 73 74 5d 29 3b 0a  , argv[iBest]);.
207e1 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
207e2 74 68 65 20 74 79 70 65 20 6f 66 20 74 68 65 20  the type of the 
207e3 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61  argument..*/.sta
207e4 74 69 63 20 76 6f 69 64 20 74 79 70 65 6f 66 46  tic void typeofF
207e5 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
207e6 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
207e7 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
207e8 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
207e9 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63  rgv.){.  const c
207ea 68 61 72 20 2a 7a 20 3d 20 30 3b 0a 20 20 73 77  har *z = 0;.  sw
207eb 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61  itch( sqlite3_va
207ec 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
207ed 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  ) ){.    case SQ
207ee 4c 49 54 45 5f 4e 55 4c 4c 3a 20 20 20 20 7a 20  LITE_NULL:    z 
207ef 3d 20 22 6e 75 6c 6c 22 3b 20 20 20 20 62 72 65  = "null";    bre
207f0 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c  ak;.    case SQL
207f1 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 7a 20 3d  ITE_INTEGER: z =
207f2 20 22 69 6e 74 65 67 65 72 22 3b 20 62 72 65 61   "integer"; brea
207f3 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  k;.    case SQLI
207f4 54 45 5f 54 45 58 54 3a 20 20 20 20 7a 20 3d 20  TE_TEXT:    z = 
207f5 22 74 65 78 74 22 3b 20 20 20 20 62 72 65 61 6b  "text";    break
207f6 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
207f7 45 5f 46 4c 4f 41 54 3a 20 20 20 7a 20 3d 20 22  E_FLOAT:   z = "
207f8 72 65 61 6c 22 3b 20 20 20 20 62 72 65 61 6b 3b  real";    break;
207f9 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
207fa 5f 42 4c 4f 42 3a 20 20 20 20 7a 20 3d 20 22 62  _BLOB:    z = "b
207fb 6c 6f 62 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a  lob";    break;.
207fc 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65    }.  sqlite3_re
207fd 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
207fe 74 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45  t, z, -1, SQLITE
207ff 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 0a 2f 2a  _STATIC);.}.../*
20800 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
20801 6f 6e 20 6f 66 20 74 68 65 20 6c 65 6e 67 74 68  on of the length
20802 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a 73  () function.*/.s
20803 74 61 74 69 63 20 76 6f 69 64 20 6c 65 6e 67 74  tatic void lengt
20804 68 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  hFunc(.  sqlite3
20805 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
20806 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
20807 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
20808 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 6c  *argv.){.  int l
20809 65 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61  en;..  assert( a
2080a 72 67 63 3d 3d 31 20 29 3b 0a 20 20 73 77 69 74  rgc==1 );.  swit
2080b 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ch( sqlite3_valu
2080c 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 20  e_type(argv[0]) 
2080d 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  ){.    case SQLI
2080e 54 45 5f 42 4c 4f 42 3a 0a 20 20 20 20 63 61 73  TE_BLOB:.    cas
2080f 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  e SQLITE_INTEGER
20810 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  :.    case SQLIT
20811 45 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20  E_FLOAT: {.     
20812 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
20813 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c  int(context, sql
20814 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
20815 28 61 72 67 76 5b 30 5d 29 29 3b 0a 20 20 20 20  (argv[0]));.    
20816 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
20817 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54     case SQLITE_T
20818 45 58 54 3a 20 7b 0a 20 20 20 20 20 20 63 6f 6e  EXT: {.      con
20819 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
2081a 20 2a 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61   *z = sqlite3_va
2081b 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
2081c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 3d 3d  );.      if( z==
2081d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  0 ) return;.    
2081e 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20    len = 0;.     
2081f 20 77 68 69 6c 65 28 20 2a 7a 20 29 7b 0a 20 20   while( *z ){.  
20820 20 20 20 20 20 20 6c 65 6e 2b 2b 3b 0a 20 20 20        len++;.   
20821 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50       SQLITE_SKIP
20822 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 20  _UTF8(z);.      
20823 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  }.      sqlite3_
20824 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65  result_int(conte
20825 78 74 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 20 20  xt, len);.      
20826 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
20827 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
20828 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
20829 5f 6e 75 6c 6c 28 63 6f 6e 74 65 78 74 29 3b 0a  _null(context);.
2082a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2082b 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
2082c 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
2082d 66 20 74 68 65 20 61 62 73 28 29 20 66 75 6e 63  f the abs() func
2082e 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  tion.*/.static v
2082f 6f 69 64 20 61 62 73 46 75 6e 63 28 73 71 6c 69  oid absFunc(sqli
20830 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
20831 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20  text, int argc, 
20832 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
20833 61 72 67 76 29 7b 0a 20 20 61 73 73 65 72 74 28  argv){.  assert(
20834 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 73 77   argc==1 );.  sw
20835 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61  itch( sqlite3_va
20836 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
20837 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  ) ){.    case SQ
20838 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 7b 0a  LITE_INTEGER: {.
20839 20 20 20 20 20 20 69 36 34 20 69 56 61 6c 20 3d        i64 iVal =
2083a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
2083b 6e 74 36 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20  nt64(argv[0]);. 
2083c 20 20 20 20 20 69 66 28 20 69 56 61 6c 3c 30 20       if( iVal<0 
2083d 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28  ){.        if( (
2083e 69 56 61 6c 3c 3c 31 29 3d 3d 30 20 29 7b 0a 20  iVal<<1)==0 ){. 
2083f 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
20840 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f  _result_error(co
20841 6e 74 65 78 74 2c 20 22 69 6e 74 65 67 65 72 20  ntext, "integer 
20842 6f 76 65 72 66 6c 6f 77 22 2c 20 2d 31 29 3b 0a  overflow", -1);.
20843 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
20844 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
20845 20 20 20 20 69 56 61 6c 20 3d 20 2d 69 56 61 6c      iVal = -iVal
20846 3b 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20 20  ;.      } .     
20847 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
20848 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 69  int64(context, i
20849 56 61 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61  Val);.      brea
2084a 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
2084b 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3a 20 7b  e SQLITE_NULL: {
2084c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
2084d 65 73 75 6c 74 5f 6e 75 6c 6c 28 63 6f 6e 74 65  esult_null(conte
2084e 78 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  xt);.      break
2084f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
20850 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 64 6f 75  ult: {.      dou
20851 62 6c 65 20 72 56 61 6c 20 3d 20 73 71 6c 69 74  ble rVal = sqlit
20852 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28  e3_value_double(
20853 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20  argv[0]);.      
20854 69 66 28 20 72 56 61 6c 3c 30 20 29 20 72 56 61  if( rVal<0 ) rVa
20855 6c 20 3d 20 2d 72 56 61 6c 3b 0a 20 20 20 20 20  l = -rVal;.     
20856 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
20857 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20  double(context, 
20858 72 56 61 6c 29 3b 0a 20 20 20 20 20 20 62 72 65  rVal);.      bre
20859 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  ak;.    }.  }.}.
2085a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
2085b 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 75 62  ation of the sub
2085c 73 74 72 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a  str() function..
2085d 2a 2a 0a 2a 2a 20 73 75 62 73 74 72 28 78 2c 70  **.** substr(x,p
2085e 31 2c 70 32 29 20 20 72 65 74 75 72 6e 73 20 70  1,p2)  returns p
2085f 32 20 63 68 61 72 61 63 74 65 72 73 20 6f 66 20  2 characters of 
20860 78 5b 5d 20 62 65 67 69 6e 6e 69 6e 67 20 77 69  x[] beginning wi
20861 74 68 20 70 31 2e 0a 2a 2a 20 70 31 20 69 73 20  th p1..** p1 is 
20862 31 2d 69 6e 64 65 78 65 64 2e 20 20 53 6f 20 73  1-indexed.  So s
20863 75 62 73 74 72 28 78 2c 31 2c 31 29 20 72 65 74  ubstr(x,1,1) ret
20864 75 72 6e 73 20 74 68 65 20 66 69 72 73 74 20 63  urns the first c
20865 68 61 72 61 63 74 65 72 0a 2a 2a 20 6f 66 20 78  haracter.** of x
20866 2e 20 20 49 66 20 78 20 69 73 20 74 65 78 74 2c  .  If x is text,
20867 20 74 68 65 6e 20 77 65 20 61 63 74 75 61 6c 6c   then we actuall
20868 79 20 63 6f 75 6e 74 20 55 54 46 2d 38 20 63 68  y count UTF-8 ch
20869 61 72 61 63 74 65 72 73 2e 0a 2a 2a 20 49 66 20  aracters..** If 
2086a 78 20 69 73 20 61 20 62 6c 6f 62 2c 20 74 68 65  x is a blob, the
2086b 6e 20 77 65 20 63 6f 75 6e 74 20 62 79 74 65 73  n we count bytes
2086c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 31 20 69 73  ..**.** If p1 is
2086d 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20   negative, then 
2086e 77 65 20 62 65 67 69 6e 20 61 62 73 28 70 31 29  we begin abs(p1)
2086f 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66   from the end of
20870 20 78 5b 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   x[]..*/.static 
20871 76 6f 69 64 20 73 75 62 73 74 72 46 75 6e 63 28  void substrFunc(
20872 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
20873 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
20874 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
20875 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
20876 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  ){.  const unsig
20877 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 63  ned char *z;.  c
20878 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
20879 61 72 20 2a 7a 32 3b 0a 20 20 69 6e 74 20 6c 65  ar *z2;.  int le
2087a 6e 3b 0a 20 20 69 6e 74 20 70 30 74 79 70 65 3b  n;.  int p0type;
2087b 0a 20 20 69 36 34 20 70 31 2c 20 70 32 3b 0a 0a  .  i64 p1, p2;..
2087c 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d    assert( argc==
2087d 33 20 7c 7c 20 61 72 67 63 3d 3d 32 20 29 3b 0a  3 || argc==2 );.
2087e 20 20 70 30 74 79 70 65 20 3d 20 73 71 6c 69 74    p0type = sqlit
2087f 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
20880 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 70 30  gv[0]);.  if( p0
20881 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f  type==SQLITE_BLO
20882 42 20 29 7b 0a 20 20 20 20 6c 65 6e 20 3d 20 73  B ){.    len = s
20883 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
20884 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  es(argv[0]);.   
20885 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c   z = sqlite3_val
20886 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29  ue_blob(argv[0])
20887 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29  ;.    if( z==0 )
20888 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 61 73 73   return;.    ass
20889 65 72 74 28 20 6c 65 6e 3d 3d 73 71 6c 69 74 65  ert( len==sqlite
2088a 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
2088b 67 76 5b 30 5d 29 20 29 3b 0a 20 20 7d 65 6c 73  gv[0]) );.  }els
2088c 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74  e{.    z = sqlit
2088d 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
2088e 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28 20  gv[0]);.    if( 
2088f 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  z==0 ) return;. 
20890 20 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20     len = 0;.    
20891 66 6f 72 28 7a 32 3d 7a 3b 20 2a 7a 32 3b 20 6c  for(z2=z; *z2; l
20892 65 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 53 51 4c  en++){.      SQL
20893 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 32  ITE_SKIP_UTF8(z2
20894 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  );.    }.  }.  p
20895 31 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  1 = sqlite3_valu
20896 65 5f 69 6e 74 28 61 72 67 76 5b 31 5d 29 3b 0a  e_int(argv[1]);.
20897 20 20 69 66 28 20 61 72 67 63 3d 3d 33 20 29 7b    if( argc==3 ){
20898 0a 20 20 20 20 70 32 20 3d 20 73 71 6c 69 74 65  .    p2 = sqlite
20899 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76  3_value_int(argv
2089a 5b 32 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  [2]);.  }else{. 
2089b 20 20 20 70 32 20 3d 20 73 71 6c 69 74 65 33 5f     p2 = sqlite3_
2089c 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
2089d 65 28 63 6f 6e 74 65 78 74 29 2d 3e 61 4c 69 6d  e(context)->aLim
2089e 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
2089f 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a 20 20 69  LENGTH];.  }.  i
208a0 66 28 20 70 31 3c 30 20 29 7b 0a 20 20 20 20 70  f( p1<0 ){.    p
208a1 31 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 69 66  1 += len;.    if
208a2 28 20 70 31 3c 30 20 29 7b 0a 20 20 20 20 20 20  ( p1<0 ){.      
208a3 70 32 20 2b 3d 20 70 31 3b 0a 20 20 20 20 20 20  p2 += p1;.      
208a4 70 31 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  p1 = 0;.    }.  
208a5 7d 65 6c 73 65 20 69 66 28 20 70 31 3e 30 20 29  }else if( p1>0 )
208a6 7b 0a 20 20 20 20 70 31 2d 2d 3b 0a 20 20 7d 0a  {.    p1--;.  }.
208a7 20 20 69 66 28 20 70 31 2b 70 32 3e 6c 65 6e 20    if( p1+p2>len 
208a8 29 7b 0a 20 20 20 20 70 32 20 3d 20 6c 65 6e 2d  ){.    p2 = len-
208a9 70 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 30  p1;.  }.  if( p0
208aa 74 79 70 65 21 3d 53 51 4c 49 54 45 5f 42 4c 4f  type!=SQLITE_BLO
208ab 42 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20  B ){.    while( 
208ac 2a 7a 20 26 26 20 70 31 20 29 7b 0a 20 20 20 20  *z && p1 ){.    
208ad 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54    SQLITE_SKIP_UT
208ae 46 38 28 7a 29 3b 0a 20 20 20 20 20 20 70 31 2d  F8(z);.      p1-
208af 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  -;.    }.    for
208b0 28 7a 32 3d 7a 3b 20 2a 7a 32 20 26 26 20 70 32  (z2=z; *z2 && p2
208b1 3b 20 70 32 2d 2d 29 7b 0a 20 20 20 20 20 20 53  ; p2--){.      S
208b2 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28  QLITE_SKIP_UTF8(
208b3 7a 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  z2);.    }.    s
208b4 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
208b5 78 74 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61  xt(context, (cha
208b6 72 2a 29 7a 2c 20 7a 32 2d 7a 2c 20 53 51 4c 49  r*)z, z2-z, SQLI
208b7 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
208b8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20   }else{.    if( 
208b9 70 32 3c 30 20 29 20 70 32 20 3d 20 30 3b 0a 20  p2<0 ) p2 = 0;. 
208ba 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
208bb 74 5f 62 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20  t_blob(context, 
208bc 28 63 68 61 72 2a 29 26 7a 5b 70 31 5d 2c 20 70  (char*)&z[p1], p
208bd 32 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  2, SQLITE_TRANSI
208be 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ENT);.  }.}../*.
208bf 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
208c0 6e 20 6f 66 20 74 68 65 20 72 6f 75 6e 64 28 29  n of the round()
208c1 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a 73 74 61   function.*/.sta
208c2 74 69 63 20 76 6f 69 64 20 72 6f 75 6e 64 46 75  tic void roundFu
208c3 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  nc(sqlite3_conte
208c4 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74  xt *context, int
208c5 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
208c6 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20  alue **argv){.  
208c7 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 64 6f 75  int n = 0;.  dou
208c8 62 6c 65 20 72 3b 0a 20 20 63 68 61 72 20 7a 42  ble r;.  char zB
208c9 75 66 5b 35 30 30 5d 3b 20 20 2f 2a 20 6c 61 72  uf[500];  /* lar
208ca 67 65 72 20 74 68 61 6e 20 74 68 65 20 25 66 20  ger than the %f 
208cb 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f  representation o
208cc 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 64 6f  f the largest do
208cd 75 62 6c 65 20 2a 2f 0a 20 20 61 73 73 65 72 74  uble */.  assert
208ce 28 20 61 72 67 63 3d 3d 31 20 7c 7c 20 61 72 67  ( argc==1 || arg
208cf 63 3d 3d 32 20 29 3b 0a 20 20 69 66 28 20 61 72  c==2 );.  if( ar
208d0 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20 69 66 28  gc==2 ){.    if(
208d1 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73 71   SQLITE_NULL==sq
208d2 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
208d3 28 61 72 67 76 5b 31 5d 29 20 29 20 72 65 74 75  (argv[1]) ) retu
208d4 72 6e 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69  rn;.    n = sqli
208d5 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72  te3_value_int(ar
208d6 67 76 5b 31 5d 29 3b 0a 20 20 20 20 69 66 28 20  gv[1]);.    if( 
208d7 6e 3e 33 30 20 29 20 6e 20 3d 20 33 30 3b 0a 20  n>30 ) n = 30;. 
208d8 20 20 20 69 66 28 20 6e 3c 30 20 29 20 6e 20 3d     if( n<0 ) n =
208d9 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71   0;.  }.  if( sq
208da 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
208db 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54  (argv[0])==SQLIT
208dc 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b  E_NULL ) return;
208dd 0a 20 20 72 20 3d 20 73 71 6c 69 74 65 33 5f 76  .  r = sqlite3_v
208de 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76  alue_double(argv
208df 5b 30 5d 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  [0]);.  sqlite3_
208e0 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
208e1 7a 42 75 66 29 2c 7a 42 75 66 2c 22 25 2e 2a 66  zBuf),zBuf,"%.*f
208e2 22 2c 6e 2c 72 29 3b 0a 20 20 73 71 6c 69 74 65  ",n,r);.  sqlite
208e3 33 41 74 6f 46 28 7a 42 75 66 2c 20 26 72 29 3b  3AtoF(zBuf, &r);
208e4 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
208e5 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74  t_double(context
208e6 2c 20 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  , r);.}../*.** A
208e7 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 20 62 79  llocate nByte by
208e8 74 65 73 20 6f 66 20 73 70 61 63 65 20 75 73 69  tes of space usi
208e9 6e 67 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  ng sqlite3_mallo
208ea 63 28 29 2e 20 49 66 20 74 68 65 0a 2a 2a 20 61  c(). If the.** a
208eb 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c  llocation fails,
208ec 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 72 65   call sqlite3_re
208ed 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d  sult_error_nomem
208ee 28 29 20 74 6f 20 6e 6f 74 69 66 79 0a 2a 2a 20  () to notify.** 
208ef 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  the database han
208f0 64 6c 65 20 74 68 61 74 20 6d 61 6c 6c 6f 63 28  dle that malloc(
208f1 29 20 68 61 73 20 66 61 69 6c 65 64 2e 0a 2a 2f  ) has failed..*/
208f2 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 63 6f  .static void *co
208f3 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 73 71 6c 69  ntextMalloc(sqli
208f4 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
208f5 74 65 78 74 2c 20 69 36 34 20 6e 42 79 74 65 29  text, i64 nByte)
208f6 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69  {.  char *z;.  i
208f7 66 28 20 6e 42 79 74 65 3e 73 71 6c 69 74 65 33  f( nByte>sqlite3
208f8 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
208f9 6c 65 28 63 6f 6e 74 65 78 74 29 2d 3e 61 4c 69  le(context)->aLi
208fa 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
208fb 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20  _LENGTH] ){.    
208fc 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
208fd 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74  rror_toobig(cont
208fe 65 78 74 29 3b 0a 20 20 20 20 7a 20 3d 20 30 3b  ext);.    z = 0;
208ff 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20  .  }else{.    z 
20900 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  = sqlite3Malloc(
20901 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20  nByte);.    if( 
20902 21 7a 20 26 26 20 6e 42 79 74 65 3e 30 20 29 7b  !z && nByte>0 ){
20903 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
20904 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65  esult_error_nome
20905 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20  m(context);.    
20906 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a  }.  }.  return z
20907 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
20908 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
20909 20 75 70 70 65 72 28 29 20 61 6e 64 20 6c 6f 77   upper() and low
2090a 65 72 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f  er() SQL functio
2090b 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ns..*/.static vo
2090c 69 64 20 75 70 70 65 72 46 75 6e 63 28 73 71 6c  id upperFunc(sql
2090d 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
2090e 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
2090f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
20910 2a 61 72 67 76 29 7b 0a 20 20 63 68 61 72 20 2a  *argv){.  char *
20911 7a 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  z1;.  const char
20912 20 2a 7a 32 3b 0a 20 20 69 6e 74 20 69 2c 20 6e   *z2;.  int i, n
20913 3b 0a 20 20 69 66 28 20 61 72 67 63 3c 31 20 7c  ;.  if( argc<1 |
20914 7c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73  | SQLITE_NULL==s
20915 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
20916 65 28 61 72 67 76 5b 30 5d 29 20 29 20 72 65 74  e(argv[0]) ) ret
20917 75 72 6e 3b 0a 20 20 7a 32 20 3d 20 28 63 68 61  urn;.  z2 = (cha
20918 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  r*)sqlite3_value
20919 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
2091a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61    n = sqlite3_va
2091b 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30  lue_bytes(argv[0
2091c 5d 29 3b 0a 20 20 2f 2a 20 56 65 72 69 66 79 20  ]);.  /* Verify 
2091d 74 68 61 74 20 74 68 65 20 63 61 6c 6c 20 74 6f  that the call to
2091e 20 5f 62 79 74 65 73 28 29 20 64 6f 65 73 20 6e   _bytes() does n
2091f 6f 74 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68  ot invalidate th
20920 65 20 5f 74 65 78 74 28 29 20 70 6f 69 6e 74 65  e _text() pointe
20921 72 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 7a  r */.  assert( z
20922 32 3d 3d 28 63 68 61 72 2a 29 73 71 6c 69 74 65  2==(char*)sqlite
20923 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
20924 76 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 7a  v[0]) );.  if( z
20925 32 20 29 7b 0a 20 20 20 20 7a 31 20 3d 20 63 6f  2 ){.    z1 = co
20926 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74  ntextMalloc(cont
20927 65 78 74 2c 20 28 28 69 36 34 29 6e 29 2b 31 29  ext, ((i64)n)+1)
20928 3b 0a 20 20 20 20 69 66 28 20 7a 31 20 29 7b 0a  ;.    if( z1 ){.
20929 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 31 2c        memcpy(z1,
2092a 20 7a 32 2c 20 6e 2b 31 29 3b 0a 20 20 20 20 20   z2, n+1);.     
2092b 20 66 6f 72 28 69 3d 30 3b 20 7a 31 5b 69 5d 3b   for(i=0; z1[i];
2092c 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 7a   i++){.        z
2092d 31 5b 69 5d 20 3d 20 74 6f 75 70 70 65 72 28 7a  1[i] = toupper(z
2092e 31 5b 69 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20  1[i]);.      }. 
2092f 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
20930 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
20931 2c 20 7a 31 2c 20 2d 31 2c 20 73 71 6c 69 74 65  , z1, -1, sqlite
20932 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20  3_free);.    }. 
20933 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64   }.}.static void
20934 20 6c 6f 77 65 72 46 75 6e 63 28 73 71 6c 69 74   lowerFunc(sqlit
20935 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
20936 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
20937 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
20938 72 67 76 29 7b 0a 20 20 63 68 61 72 20 2a 7a 31  rgv){.  char *z1
20939 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
2093a 7a 32 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a  z2;.  int i, n;.
2093b 20 20 69 66 28 20 61 72 67 63 3c 31 20 7c 7c 20    if( argc<1 || 
2093c 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73 71 6c  SQLITE_NULL==sql
2093d 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
2093e 61 72 67 76 5b 30 5d 29 20 29 20 72 65 74 75 72  argv[0]) ) retur
2093f 6e 3b 0a 20 20 7a 32 20 3d 20 28 63 68 61 72 2a  n;.  z2 = (char*
20940 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
20941 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ext(argv[0]);.  
20942 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  n = sqlite3_valu
20943 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29  e_bytes(argv[0])
20944 3b 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68  ;.  /* Verify th
20945 61 74 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 5f  at the call to _
20946 62 79 74 65 73 28 29 20 64 6f 65 73 20 6e 6f 74  bytes() does not
20947 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20   invalidate the 
20948 5f 74 65 78 74 28 29 20 70 6f 69 6e 74 65 72 20  _text() pointer 
20949 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 7a 32 3d  */.  assert( z2=
2094a 3d 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f  =(char*)sqlite3_
2094b 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
2094c 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 7a 32 20  0]) );.  if( z2 
2094d 29 7b 0a 20 20 20 20 7a 31 20 3d 20 63 6f 6e 74  ){.    z1 = cont
2094e 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78  extMalloc(contex
2094f 74 2c 20 28 28 69 36 34 29 6e 29 2b 31 29 3b 0a  t, ((i64)n)+1);.
20950 20 20 20 20 69 66 28 20 7a 31 20 29 7b 0a 20 20      if( z1 ){.  
20951 20 20 20 20 6d 65 6d 63 70 79 28 7a 31 2c 20 7a      memcpy(z1, z
20952 32 2c 20 6e 2b 31 29 3b 0a 20 20 20 20 20 20 66  2, n+1);.      f
20953 6f 72 28 69 3d 30 3b 20 7a 31 5b 69 5d 3b 20 69  or(i=0; z1[i]; i
20954 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 7a 31 5b  ++){.        z1[
20955 69 5d 20 3d 20 74 6f 6c 6f 77 65 72 28 7a 31 5b  i] = tolower(z1[
20956 69 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  i]);.      }.   
20957 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
20958 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
20959 7a 31 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 5f  z1, -1, sqlite3_
2095a 66 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  free);.    }.  }
2095b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
2095c 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
2095d 49 46 4e 55 4c 4c 28 29 2c 20 4e 56 4c 28 29 2c  IFNULL(), NVL(),
2095e 20 61 6e 64 20 43 4f 41 4c 45 53 43 45 28 29 20   and COALESCE() 
2095f 66 75 6e 63 74 69 6f 6e 73 2e 20 20 0a 2a 2a 20  functions.  .** 
20960 41 6c 6c 20 74 68 72 65 65 20 64 6f 20 74 68 65  All three do the
20961 20 73 61 6d 65 20 74 68 69 6e 67 2e 20 20 54 68   same thing.  Th
20962 65 79 20 72 65 74 75 72 6e 20 74 68 65 20 66 69  ey return the fi
20963 72 73 74 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20  rst non-NULL.** 
20964 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61  argument..*/.sta
20965 74 69 63 20 76 6f 69 64 20 69 66 6e 75 6c 6c 46  tic void ifnullF
20966 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
20967 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
20968 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
20969 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
2096a 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  rgv.){.  int i;.
2096b 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 61 72 67    for(i=0; i<arg
2096c 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  c; i++){.    if(
2096d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 21 3d 73 71   SQLITE_NULL!=sq
2096e 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
2096f 28 61 72 67 76 5b 69 5d 29 20 29 7b 0a 20 20 20  (argv[i]) ){.   
20970 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
20971 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c  t_value(context,
20972 20 61 72 67 76 5b 69 5d 29 3b 0a 20 20 20 20 20   argv[i]);.     
20973 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
20974 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  }.}../*.** Imple
20975 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 72 61 6e  mentation of ran
20976 64 6f 6d 28 29 2e 20 20 52 65 74 75 72 6e 20 61  dom().  Return a
20977 20 72 61 6e 64 6f 6d 20 69 6e 74 65 67 65 72 2e   random integer.
20978 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69    .*/.static voi
20979 64 20 72 61 6e 64 6f 6d 46 75 6e 63 28 0a 20 20  d randomFunc(.  
2097a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
2097b 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
2097c 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
2097d 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
2097e 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 72    sqlite_int64 r
2097f 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64  ;.  sqlite3_rand
20980 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 72 29  omness(sizeof(r)
20981 2c 20 26 72 29 3b 0a 20 20 69 66 28 20 28 72 3c  , &r);.  if( (r<
20982 3c 31 29 3d 3d 30 20 29 20 72 20 3d 20 30 3b 20  <1)==0 ) r = 0; 
20983 20 2f 2a 20 50 72 65 76 65 6e 74 20 30 78 38 30   /* Prevent 0x80
20984 30 30 2e 2e 2e 2e 20 61 73 20 74 68 65 20 72 65  00.... as the re
20985 73 75 6c 74 20 73 6f 20 74 68 61 74 20 77 65 20  sult so that we 
20986 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  */.             
20987 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
20988 63 61 6e 20 61 6c 77 61 79 73 20 64 6f 20 61 62  can always do ab
20989 73 28 29 20 6f 66 20 74 68 65 20 72 65 73 75 6c  s() of the resul
2098a 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 72  t */.  sqlite3_r
2098b 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74  esult_int64(cont
2098c 65 78 74 2c 20 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  ext, r);.}../*.*
2098d 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
2098e 20 6f 66 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 4e   of randomblob(N
2098f 29 2e 20 20 52 65 74 75 72 6e 20 61 20 72 61 6e  ).  Return a ran
20990 64 6f 6d 20 62 6c 6f 62 0a 2a 2a 20 74 68 61 74  dom blob.** that
20991 20 69 73 20 4e 20 62 79 74 65 73 20 6c 6f 6e 67   is N bytes long
20992 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
20993 20 72 61 6e 64 6f 6d 42 6c 6f 62 28 0a 20 20 73   randomBlob(.  s
20994 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
20995 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
20996 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
20997 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
20998 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73 69 67 6e   int n;.  unsign
20999 65 64 20 63 68 61 72 20 2a 70 3b 0a 20 20 61 73  ed char *p;.  as
2099a 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b  sert( argc==1 );
2099b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76  .  n = sqlite3_v
2099c 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 30 5d  alue_int(argv[0]
2099d 29 3b 0a 20 20 69 66 28 20 6e 3c 31 20 29 7b 0a  );.  if( n<1 ){.
2099e 20 20 20 20 6e 20 3d 20 31 3b 0a 20 20 7d 0a 20      n = 1;.  }. 
2099f 20 70 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c   p = contextMall
209a0 6f 63 28 63 6f 6e 74 65 78 74 2c 20 6e 29 3b 0a  oc(context, n);.
209a1 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73    if( p ){.    s
209a2 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73  qlite3_randomnes
209a3 73 28 6e 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c  s(n, p);.    sql
209a4 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
209a5 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a  (context, (char*
209a6 29 70 2c 20 6e 2c 20 73 71 6c 69 74 65 33 5f 66  )p, n, sqlite3_f
209a7 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ree);.  }.}../*.
209a8 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
209a9 6e 20 6f 66 20 74 68 65 20 6c 61 73 74 5f 69 6e  n of the last_in
209aa 73 65 72 74 5f 72 6f 77 69 64 28 29 20 53 51 4c  sert_rowid() SQL
209ab 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20   function.  The 
209ac 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20  return.** value 
209ad 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  is the same as t
209ae 68 65 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f  he sqlite3_last_
209af 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 41  insert_rowid() A
209b0 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a  PI function..*/.
209b1 73 74 61 74 69 63 20 76 6f 69 64 20 6c 61 73 74  static void last
209b2 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 0a 20  _insert_rowid(. 
209b3 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
209b4 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e   *context, .  in
209b5 74 20 61 72 67 2c 20 0a 20 20 73 71 6c 69 74 65  t arg, .  sqlite
209b6 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
209b7 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
209b8 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  = sqlite3_contex
209b9 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74  t_db_handle(cont
209ba 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ext);.  sqlite3_
209bb 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e  result_int64(con
209bc 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 6c 61  text, sqlite3_la
209bd 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28  st_insert_rowid(
209be 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  db));.}../*.** I
209bf 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
209c0 20 74 68 65 20 63 68 61 6e 67 65 73 28 29 20 53   the changes() S
209c1 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  QL function.  Th
209c2 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69  e return value i
209c3 73 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61 73  s the.** same as
209c4 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 68 61   the sqlite3_cha
209c5 6e 67 65 73 28 29 20 41 50 49 20 66 75 6e 63 74  nges() API funct
209c6 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
209c7 6f 69 64 20 63 68 61 6e 67 65 73 28 0a 20 20 73  oid changes(.  s
209c8 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
209c9 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
209ca 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  rg,.  sqlite3_va
209cb 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
209cc 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71  sqlite3 *db = sq
209cd 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
209ce 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29  _handle(context)
209cf 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ;.  sqlite3_resu
209d0 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20  lt_int(context, 
209d1 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28  sqlite3_changes(
209d2 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  db));.}../*.** I
209d3 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
209d4 20 74 68 65 20 74 6f 74 61 6c 5f 63 68 61 6e 67   the total_chang
209d5 65 73 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f  es() SQL functio
209d6 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76  n.  The return v
209d7 61 6c 75 65 20 69 73 0a 2a 2a 20 74 68 65 20 73  alue is.** the s
209d8 61 6d 65 20 61 73 20 74 68 65 20 73 71 6c 69 74  ame as the sqlit
209d9 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
209da 28 29 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e  () API function.
209db 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
209dc 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 0a 20  total_changes(. 
209dd 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
209de 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
209df 20 61 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f   arg,.  sqlite3_
209e0 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
209e1 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
209e2 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
209e3 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78  db_handle(contex
209e4 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  t);.  sqlite3_re
209e5 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74  sult_int(context
209e6 2c 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f  , sqlite3_total_
209e7 63 68 61 6e 67 65 73 28 64 62 29 29 3b 0a 7d 0a  changes(db));.}.
209e8 0a 2f 2a 0a 2a 2a 20 41 20 73 74 72 75 63 74 75  ./*.** A structu
209e9 72 65 20 64 65 66 69 6e 69 6e 67 20 68 6f 77 20  re defining how 
209ea 74 6f 20 64 6f 20 47 4c 4f 42 2d 73 74 79 6c 65  to do GLOB-style
209eb 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a 2f   comparisons..*/
209ec 0a 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49  .struct compareI
209ed 6e 66 6f 20 7b 0a 20 20 75 38 20 6d 61 74 63 68  nfo {.  u8 match
209ee 41 6c 6c 3b 0a 20 20 75 38 20 6d 61 74 63 68 4f  All;.  u8 matchO
209ef 6e 65 3b 0a 20 20 75 38 20 6d 61 74 63 68 53 65  ne;.  u8 matchSe
209f0 74 3b 0a 20 20 75 38 20 6e 6f 43 61 73 65 3b 0a  t;.  u8 noCase;.
209f1 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 4c 49  };../*.** For LI
209f2 4b 45 20 61 6e 64 20 47 4c 4f 42 20 6d 61 74 63  KE and GLOB matc
209f3 68 69 6e 67 20 6f 6e 20 45 42 43 44 49 43 20 6d  hing on EBCDIC m
209f4 61 63 68 69 6e 65 73 2c 20 61 73 73 75 6d 65 20  achines, assume 
209f5 74 68 61 74 20 65 76 65 72 79 0a 2a 2a 20 63 68  that every.** ch
209f6 61 72 61 63 74 65 72 20 69 73 20 65 78 61 63 74  aracter is exact
209f7 6c 79 20 6f 6e 65 20 62 79 74 65 20 69 6e 20 73  ly one byte in s
209f8 69 7a 65 2e 20 20 41 6c 73 6f 2c 20 61 6c 6c 20  ize.  Also, all 
209f9 63 68 61 72 61 63 74 65 72 73 20 61 72 65 0a 2a  characters are.*
209fa 2a 20 61 62 6c 65 20 74 6f 20 70 61 72 74 69 63  * able to partic
209fb 69 70 61 74 65 20 69 6e 20 75 70 70 65 72 2d 63  ipate in upper-c
209fc 61 73 65 2d 74 6f 2d 6c 6f 77 65 72 2d 63 61 73  ase-to-lower-cas
209fd 65 20 6d 61 70 70 69 6e 67 73 20 69 6e 20 45 42  e mappings in EB
209fe 43 44 49 43 0a 2a 2a 20 77 68 65 72 65 61 73 20  CDIC.** whereas 
209ff 6f 6e 6c 79 20 63 68 61 72 61 63 74 65 72 73 20  only characters 
20a00 6c 65 73 73 20 74 68 61 6e 20 30 78 38 30 20 64  less than 0x80 d
20a01 6f 20 69 6e 20 41 53 43 49 49 2e 0a 2a 2f 0a 23  o in ASCII..*/.#
20a02 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
20a03 45 5f 45 42 43 44 49 43 29 0a 23 20 64 65 66 69  E_EBCDIC).# defi
20a04 6e 65 20 73 71 6c 69 74 65 33 55 74 66 38 52 65  ne sqlite3Utf8Re
20a05 61 64 28 41 2c 42 2c 43 29 20 20 28 2a 28 41 2b  ad(A,B,C)  (*(A+
20a06 2b 29 29 0a 23 20 64 65 66 69 6e 65 20 47 6c 6f  +)).# define Glo
20a07 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 41 29  gUpperToLower(A)
20a08 20 20 20 20 20 41 20 3d 20 73 71 6c 69 74 65 33       A = sqlite3
20a09 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 41 5d 0a  UpperToLower[A].
20a0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 47  #else.# define G
20a0b 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28  logUpperToLower(
20a0c 41 29 20 20 20 20 20 69 66 28 20 41 3c 30 78 38  A)     if( A<0x8
20a0d 30 20 29 7b 20 41 20 3d 20 73 71 6c 69 74 65 33  0 ){ A = sqlite3
20a0e 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 41 5d 3b  UpperToLower[A];
20a0f 20 7d 0a 23 65 6e 64 69 66 0a 0a 73 74 61 74 69   }.#endif..stati
20a10 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 63  c const struct c
20a11 6f 6d 70 61 72 65 49 6e 66 6f 20 67 6c 6f 62 49  ompareInfo globI
20a12 6e 66 6f 20 3d 20 7b 20 27 2a 27 2c 20 27 3f 27  nfo = { '*', '?'
20a13 2c 20 27 5b 27 2c 20 30 20 7d 3b 0a 2f 2a 20 54  , '[', 0 };./* T
20a14 68 65 20 63 6f 72 72 65 63 74 20 53 51 4c 2d 39  he correct SQL-9
20a15 32 20 62 65 68 61 76 69 6f 72 20 69 73 20 66 6f  2 behavior is fo
20a16 72 20 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 61  r the LIKE opera
20a17 74 6f 72 20 74 6f 20 69 67 6e 6f 72 65 0a 2a 2a  tor to ignore.**
20a18 20 63 61 73 65 2e 20 20 54 68 75 73 20 20 27 61   case.  Thus  'a
20a19 27 20 4c 49 4b 45 20 27 41 27 20 77 6f 75 6c 64  ' LIKE 'A' would
20a1a 20 62 65 20 74 72 75 65 2e 20 2a 2f 0a 73 74 61   be true. */.sta
20a1b 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74  tic const struct
20a1c 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 6c 69 6b   compareInfo lik
20a1d 65 49 6e 66 6f 4e 6f 72 6d 20 3d 20 7b 20 27 25  eInfoNorm = { '%
20a1e 27 2c 20 27 5f 27 2c 20 20 20 30 2c 20 31 20 7d  ', '_',   0, 1 }
20a1f 3b 0a 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f 43  ;./* If SQLITE_C
20a20 41 53 45 5f 53 45 4e 53 49 54 49 56 45 5f 4c 49  ASE_SENSITIVE_LI
20a21 4b 45 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74  KE is defined, t
20a22 68 65 6e 20 74 68 65 20 4c 49 4b 45 20 6f 70 65  hen the LIKE ope
20a23 72 61 74 6f 72 0a 2a 2a 20 69 73 20 63 61 73 65  rator.** is case
20a24 20 73 65 6e 73 69 74 69 76 65 20 63 61 75 73 69   sensitive causi
20a25 6e 67 20 27 61 27 20 4c 49 4b 45 20 27 41 27 20  ng 'a' LIKE 'A' 
20a26 74 6f 20 62 65 20 66 61 6c 73 65 20 2a 2f 0a 73  to be false */.s
20a27 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75  tatic const stru
20a28 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 6c  ct compareInfo l
20a29 69 6b 65 49 6e 66 6f 41 6c 74 20 3d 20 7b 20 27  ikeInfoAlt = { '
20a2a 25 27 2c 20 27 5f 27 2c 20 20 20 30 2c 20 30 20  %', '_',   0, 0 
20a2b 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72  };../*.** Compar
20a2c 65 20 74 77 6f 20 55 54 46 2d 38 20 73 74 72 69  e two UTF-8 stri
20a2d 6e 67 73 20 66 6f 72 20 65 71 75 61 6c 69 74 79  ngs for equality
20a2e 20 77 68 65 72 65 20 74 68 65 20 66 69 72 73 74   where the first
20a2f 20 73 74 72 69 6e 67 20 63 61 6e 0a 2a 2a 20 70   string can.** p
20a30 6f 74 65 6e 74 69 61 6c 6c 79 20 62 65 20 61 20  otentially be a 
20a31 22 67 6c 6f 62 22 20 65 78 70 72 65 73 73 69 6f  "glob" expressio
20a32 6e 2e 20 20 52 65 74 75 72 6e 20 74 72 75 65 20  n.  Return true 
20a33 28 31 29 20 69 66 20 74 68 65 79 0a 2a 2a 20 61  (1) if they.** a
20a34 72 65 20 74 68 65 20 73 61 6d 65 20 61 6e 64 20  re the same and 
20a35 66 61 6c 73 65 20 28 30 29 20 69 66 20 74 68 65  false (0) if the
20a36 79 20 61 72 65 20 64 69 66 66 65 72 65 6e 74 2e  y are different.
20a37 0a 2a 2a 0a 2a 2a 20 47 6c 6f 62 62 69 6e 67 20  .**.** Globbing 
20a38 72 75 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  rules:.**.**    
20a39 20 20 27 2a 27 20 20 20 20 20 20 20 4d 61 74 63    '*'       Matc
20a3a 68 65 73 20 61 6e 79 20 73 65 71 75 65 6e 63 65  hes any sequence
20a3b 20 6f 66 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65   of zero or more
20a3c 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a   characters..**.
20a3d 2a 2a 20 20 20 20 20 20 27 3f 27 20 20 20 20 20  **      '?'     
20a3e 20 20 4d 61 74 63 68 65 73 20 65 78 61 63 74 6c    Matches exactl
20a3f 79 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72 2e  y one character.
20a40 0a 2a 2a 0a 2a 2a 20 20 20 20 20 5b 2e 2e 2e 5d  .**.**     [...]
20a41 20 20 20 20 20 20 4d 61 74 63 68 65 73 20 6f 6e        Matches on
20a42 65 20 63 68 61 72 61 63 74 65 72 20 66 72 6f 6d  e character from
20a43 20 74 68 65 20 65 6e 63 6c 6f 73 65 64 20 6c 69   the enclosed li
20a44 73 74 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20  st of.**        
20a45 20 20 20 20 20 20 20 20 63 68 61 72 61 63 74 65          characte
20a46 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 5b 5e  rs..**.**     [^
20a47 2e 2e 2e 5d 20 20 20 20 20 4d 61 74 63 68 65 73  ...]     Matches
20a48 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72 20 6e   one character n
20a49 6f 74 20 69 6e 20 74 68 65 20 65 6e 63 6c 6f 73  ot in the enclos
20a4a 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 57  ed list..**.** W
20a4b 69 74 68 20 74 68 65 20 5b 2e 2e 2e 5d 20 61 6e  ith the [...] an
20a4c 64 20 5b 5e 2e 2e 2e 5d 20 6d 61 74 63 68 69 6e  d [^...] matchin
20a4d 67 2c 20 61 20 27 5d 27 20 63 68 61 72 61 63 74  g, a ']' charact
20a4e 65 72 20 63 61 6e 20 62 65 20 69 6e 63 6c 75 64  er can be includ
20a4f 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 6c 69 73  ed.** in the lis
20a50 74 20 62 79 20 6d 61 6b 69 6e 67 20 69 74 20 74  t by making it t
20a51 68 65 20 66 69 72 73 74 20 63 68 61 72 61 63 74  he first charact
20a52 65 72 20 61 66 74 65 72 20 27 5b 27 20 6f 72 20  er after '[' or 
20a53 27 5e 27 2e 20 20 41 0a 2a 2a 20 72 61 6e 67 65  '^'.  A.** range
20a54 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 63   of characters c
20a55 61 6e 20 62 65 20 73 70 65 63 69 66 69 65 64 20  an be specified 
20a56 75 73 69 6e 67 20 27 2d 27 2e 20 20 45 78 61 6d  using '-'.  Exam
20a57 70 6c 65 3a 0a 2a 2a 20 22 5b 61 2d 7a 5d 22 20  ple:.** "[a-z]" 
20a58 6d 61 74 63 68 65 73 20 61 6e 79 20 73 69 6e 67  matches any sing
20a59 6c 65 20 6c 6f 77 65 72 2d 63 61 73 65 20 6c 65  le lower-case le
20a5a 74 74 65 72 2e 20 20 54 6f 20 6d 61 74 63 68 20  tter.  To match 
20a5b 61 20 27 2d 27 2c 20 6d 61 6b 65 0a 2a 2a 20 69  a '-', make.** i
20a5c 74 20 74 68 65 20 6c 61 73 74 20 63 68 61 72 61  t the last chara
20a5d 63 74 65 72 20 69 6e 20 74 68 65 20 6c 69 73 74  cter in the list
20a5e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
20a5f 74 69 6e 65 20 69 73 20 75 73 75 61 6c 6c 79 20  tine is usually 
20a60 71 75 69 63 6b 2c 20 62 75 74 20 63 61 6e 20 62  quick, but can b
20a61 65 20 4e 2a 2a 32 20 69 6e 20 74 68 65 20 77 6f  e N**2 in the wo
20a62 72 73 74 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  rst case..**.** 
20a63 48 69 6e 74 73 3a 20 74 6f 20 6d 61 74 63 68 20  Hints: to match 
20a64 27 2a 27 20 6f 72 20 27 3f 27 2c 20 70 75 74 20  '*' or '?', put 
20a65 74 68 65 6d 20 69 6e 20 22 5b 5d 22 2e 20 20 4c  them in "[]".  L
20a66 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
20a67 20 20 20 20 20 20 20 20 61 62 63 5b 2a 5d 78 79          abc[*]xy
20a68 7a 20 20 20 20 20 20 20 20 4d 61 74 63 68 65 73  z        Matches
20a69 20 22 61 62 63 2a 78 79 7a 22 20 6f 6e 6c 79 0a   "abc*xyz" only.
20a6a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
20a6b 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 0a 20 20  tternCompare(.  
20a6c 63 6f 6e 73 74 20 75 38 20 2a 7a 50 61 74 74 65  const u8 *zPatte
20a6d 72 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  rn,             
20a6e 20 2f 2a 20 54 68 65 20 67 6c 6f 62 20 70 61 74   /* The glob pat
20a6f 74 65 72 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  tern */.  const 
20a70 75 38 20 2a 7a 53 74 72 69 6e 67 2c 20 20 20 20  u8 *zString,    
20a71 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
20a72 65 20 73 74 72 69 6e 67 20 74 6f 20 63 6f 6d 70  e string to comp
20a73 61 72 65 20 61 67 61 69 6e 73 74 20 74 68 65 20  are against the 
20a74 67 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  glob */.  const 
20a75 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e  struct compareIn
20a76 66 6f 20 2a 70 49 6e 66 6f 2c 20 2f 2a 20 49 6e  fo *pInfo, /* In
20a77 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
20a78 68 6f 77 20 74 6f 20 64 6f 20 74 68 65 20 63 6f  how to do the co
20a79 6d 70 61 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  mpare */.  const
20a7a 20 69 6e 74 20 65 73 63 20 20 20 20 20 20 20 20   int esc        
20a7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
20a7c 68 65 20 65 73 63 61 70 65 20 63 68 61 72 61 63  he escape charac
20a7d 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ter */.){.  int 
20a7e 63 2c 20 63 32 3b 0a 20 20 69 6e 74 20 69 6e 76  c, c2;.  int inv
20a7f 65 72 74 3b 0a 20 20 69 6e 74 20 73 65 65 6e 3b  ert;.  int seen;
20a80 0a 20 20 75 38 20 6d 61 74 63 68 4f 6e 65 20 3d  .  u8 matchOne =
20a81 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 4f 6e 65   pInfo->matchOne
20a82 3b 0a 20 20 75 38 20 6d 61 74 63 68 41 6c 6c 20  ;.  u8 matchAll 
20a83 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 41 6c  = pInfo->matchAl
20a84 6c 3b 0a 20 20 75 38 20 6d 61 74 63 68 53 65 74  l;.  u8 matchSet
20a85 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 53   = pInfo->matchS
20a86 65 74 3b 0a 20 20 75 38 20 6e 6f 43 61 73 65 20  et;.  u8 noCase 
20a87 3d 20 70 49 6e 66 6f 2d 3e 6e 6f 43 61 73 65 3b  = pInfo->noCase;
20a88 20 0a 20 20 69 6e 74 20 70 72 65 76 45 73 63 61   .  int prevEsca
20a89 70 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54  pe = 0;     /* T
20a8a 72 75 65 20 69 66 20 74 68 65 20 70 72 65 76 69  rue if the previ
20a8b 6f 75 73 20 63 68 61 72 61 63 74 65 72 20 77 61  ous character wa
20a8c 73 20 27 65 73 63 61 70 65 27 20 2a 2f 0a 0a 20  s 'escape' */.. 
20a8d 20 77 68 69 6c 65 28 20 28 63 20 3d 20 73 71 6c   while( (c = sql
20a8e 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61  ite3Utf8Read(zPa
20a8f 74 74 65 72 6e 2c 30 2c 26 7a 50 61 74 74 65 72  ttern,0,&zPatter
20a90 6e 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 66  n))!=0 ){.    if
20a91 28 20 21 70 72 65 76 45 73 63 61 70 65 20 26 26  ( !prevEscape &&
20a92 20 63 3d 3d 6d 61 74 63 68 41 6c 6c 20 29 7b 0a   c==matchAll ){.
20a93 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d        while( (c=
20a94 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28  sqlite3Utf8Read(
20a95 7a 50 61 74 74 65 72 6e 2c 30 2c 26 7a 50 61 74  zPattern,0,&zPat
20a96 74 65 72 6e 29 29 20 3d 3d 20 6d 61 74 63 68 41  tern)) == matchA
20a97 6c 6c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ll.             
20a98 20 20 7c 7c 20 63 20 3d 3d 20 6d 61 74 63 68 4f    || c == matchO
20a99 6e 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  ne ){.        if
20a9a 28 20 63 3d 3d 6d 61 74 63 68 4f 6e 65 20 26 26  ( c==matchOne &&
20a9b 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
20a9c 28 7a 53 74 72 69 6e 67 2c 20 30 2c 20 26 7a 53  (zString, 0, &zS
20a9d 74 72 69 6e 67 29 3d 3d 30 20 29 7b 0a 20 20 20  tring)==0 ){.   
20a9e 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b         return 0;
20a9f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
20aa0 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d   }.      if( c==
20aa1 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  0 ){.        ret
20aa2 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c  urn 1;.      }el
20aa3 73 65 20 69 66 28 20 63 3d 3d 65 73 63 20 29 7b  se if( c==esc ){
20aa4 0a 20 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c  .        c = sql
20aa5 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61  ite3Utf8Read(zPa
20aa6 74 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74  ttern, 0, &zPatt
20aa7 65 72 6e 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ern);.        if
20aa8 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ( c==0 ){.      
20aa9 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
20aaa 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
20aab 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74 63 68  lse if( c==match
20aac 53 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 61  Set ){.        a
20aad 73 73 65 72 74 28 20 65 73 63 3d 3d 30 20 29 3b  ssert( esc==0 );
20aae 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73           /* This
20aaf 20 69 73 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c 49   is GLOB, not LI
20ab0 4b 45 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73  KE */.        as
20ab1 73 65 72 74 28 20 6d 61 74 63 68 53 65 74 3c 30  sert( matchSet<0
20ab2 78 38 30 20 29 3b 20 20 2f 2a 20 27 5b 27 20 69  x80 );  /* '[' i
20ab3 73 20 61 20 73 69 6e 67 6c 65 2d 62 79 74 65 20  s a single-byte 
20ab4 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 20  character */.   
20ab5 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 53 74       while( *zSt
20ab6 72 69 6e 67 20 26 26 20 70 61 74 74 65 72 6e 43  ring && patternC
20ab7 6f 6d 70 61 72 65 28 26 7a 50 61 74 74 65 72 6e  ompare(&zPattern
20ab8 5b 2d 31 5d 2c 7a 53 74 72 69 6e 67 2c 70 49 6e  [-1],zString,pIn
20ab9 66 6f 2c 65 73 63 29 3d 3d 30 20 29 7b 0a 20 20  fo,esc)==0 ){.  
20aba 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53          SQLITE_S
20abb 4b 49 50 5f 55 54 46 38 28 7a 53 74 72 69 6e 67  KIP_UTF8(zString
20abc 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
20abd 20 20 20 20 20 72 65 74 75 72 6e 20 2a 7a 53 74       return *zSt
20abe 72 69 6e 67 21 3d 30 3b 0a 20 20 20 20 20 20 7d  ring!=0;.      }
20abf 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 63  .      while( (c
20ac0 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  2 = sqlite3Utf8R
20ac1 65 61 64 28 7a 53 74 72 69 6e 67 2c 30 2c 26 7a  ead(zString,0,&z
20ac2 53 74 72 69 6e 67 29 29 21 3d 30 20 29 7b 0a 20  String))!=0 ){. 
20ac3 20 20 20 20 20 20 20 69 66 28 20 6e 6f 43 61 73         if( noCas
20ac4 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 47  e ){.          G
20ac5 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28  logUpperToLower(
20ac6 63 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 47  c2);.          G
20ac7 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28  logUpperToLower(
20ac8 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20 77 68  c);.          wh
20ac9 69 6c 65 28 20 63 32 20 21 3d 20 30 20 26 26 20  ile( c2 != 0 && 
20aca 63 32 20 21 3d 20 63 20 29 7b 0a 20 20 20 20 20  c2 != c ){.     
20acb 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69         c2 = sqli
20acc 74 65 33 55 74 66 38 52 65 61 64 28 7a 53 74 72  te3Utf8Read(zStr
20acd 69 6e 67 2c 20 30 2c 20 26 7a 53 74 72 69 6e 67  ing, 0, &zString
20ace 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 47  );.            G
20acf 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28  logUpperToLower(
20ad0 63 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  c2);.          }
20ad1 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
20ad2 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28            while(
20ad3 20 63 32 20 21 3d 20 30 20 26 26 20 63 32 20 21   c2 != 0 && c2 !
20ad4 3d 20 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20  = c ){.         
20ad5 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55     c2 = sqlite3U
20ad6 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c  tf8Read(zString,
20ad7 20 30 2c 20 26 7a 53 74 72 69 6e 67 29 3b 0a 20   0, &zString);. 
20ad8 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
20ad9 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
20ada 20 63 32 3d 3d 30 20 29 20 72 65 74 75 72 6e 20   c2==0 ) return 
20adb 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  0;.        if( p
20adc 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 7a 50  atternCompare(zP
20add 61 74 74 65 72 6e 2c 7a 53 74 72 69 6e 67 2c 70  attern,zString,p
20ade 49 6e 66 6f 2c 65 73 63 29 20 29 20 72 65 74 75  Info,esc) ) retu
20adf 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 1;.      }.  
20ae0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
20ae1 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 72 65    }else if( !pre
20ae2 76 45 73 63 61 70 65 20 26 26 20 63 3d 3d 6d 61  vEscape && c==ma
20ae3 74 63 68 4f 6e 65 20 29 7b 0a 20 20 20 20 20 20  tchOne ){.      
20ae4 69 66 28 20 73 71 6c 69 74 65 33 55 74 66 38 52  if( sqlite3Utf8R
20ae5 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 30 2c 20  ead(zString, 0, 
20ae6 26 7a 53 74 72 69 6e 67 29 3d 3d 30 20 29 7b 0a  &zString)==0 ){.
20ae7 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30          return 0
20ae8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
20ae9 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74 63 68  lse if( c==match
20aea 53 65 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  Set ){.      int
20aeb 20 70 72 69 6f 72 5f 63 20 3d 20 30 3b 0a 20 20   prior_c = 0;.  
20aec 20 20 20 20 61 73 73 65 72 74 28 20 65 73 63 3d      assert( esc=
20aed 3d 30 20 29 3b 20 20 20 20 2f 2a 20 54 68 69 73  =0 );    /* This
20aee 20 6f 6e 6c 79 20 6f 63 63 75 72 73 20 66 6f 72   only occurs for
20aef 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c 49 4b 45 20   GLOB, not LIKE 
20af0 2a 2f 0a 20 20 20 20 20 20 73 65 65 6e 20 3d 20  */.      seen = 
20af1 30 3b 0a 20 20 20 20 20 20 69 6e 76 65 72 74 20  0;.      invert 
20af2 3d 20 30 3b 0a 20 20 20 20 20 20 63 20 3d 20 73  = 0;.      c = s
20af3 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
20af4 53 74 72 69 6e 67 2c 20 30 2c 20 26 7a 53 74 72  String, 0, &zStr
20af5 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ing);.      if( 
20af6 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  c==0 ) return 0;
20af7 0a 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69  .      c2 = sqli
20af8 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74  te3Utf8Read(zPat
20af9 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74 65  tern, 0, &zPatte
20afa 72 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63  rn);.      if( c
20afb 32 3d 3d 27 5e 27 20 29 7b 0a 20 20 20 20 20 20  2=='^' ){.      
20afc 20 20 69 6e 76 65 72 74 20 3d 20 31 3b 0a 20 20    invert = 1;.  
20afd 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74        c2 = sqlit
20afe 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74  e3Utf8Read(zPatt
20aff 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74 65 72  ern, 0, &zPatter
20b00 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  n);.      }.    
20b01 20 20 69 66 28 20 63 32 3d 3d 27 5d 27 20 29 7b    if( c2==']' ){
20b02 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d  .        if( c==
20b03 27 5d 27 20 29 20 73 65 65 6e 20 3d 20 31 3b 0a  ']' ) seen = 1;.
20b04 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c          c2 = sql
20b05 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61  ite3Utf8Read(zPa
20b06 74 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74  ttern, 0, &zPatt
20b07 65 72 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ern);.      }.  
20b08 20 20 20 20 77 68 69 6c 65 28 20 63 32 20 26 26      while( c2 &&
20b09 20 63 32 21 3d 27 5d 27 20 29 7b 0a 20 20 20 20   c2!=']' ){.    
20b0a 20 20 20 20 69 66 28 20 63 32 3d 3d 27 2d 27 20      if( c2=='-' 
20b0b 26 26 20 7a 50 61 74 74 65 72 6e 5b 30 5d 21 3d  && zPattern[0]!=
20b0c 27 5d 27 20 26 26 20 7a 50 61 74 74 65 72 6e 5b  ']' && zPattern[
20b0d 30 5d 21 3d 30 20 26 26 20 70 72 69 6f 72 5f 63  0]!=0 && prior_c
20b0e 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  >0 ){.          
20b0f 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38  c2 = sqlite3Utf8
20b10 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 20 30  Read(zPattern, 0
20b11 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20  , &zPattern);.  
20b12 20 20 20 20 20 20 20 20 69 66 28 20 63 3e 3d 70          if( c>=p
20b13 72 69 6f 72 5f 63 20 26 26 20 63 3c 3d 63 32 20  rior_c && c<=c2 
20b14 29 20 73 65 65 6e 20 3d 20 31 3b 0a 20 20 20 20  ) seen = 1;.    
20b15 20 20 20 20 20 20 70 72 69 6f 72 5f 63 20 3d 20        prior_c = 
20b16 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  0;.        }else
20b17 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
20b18 63 3d 3d 63 32 20 29 7b 0a 20 20 20 20 20 20 20  c==c2 ){.       
20b19 20 20 20 20 20 73 65 65 6e 20 3d 20 31 3b 0a 20       seen = 1;. 
20b1a 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
20b1b 20 20 20 20 20 70 72 69 6f 72 5f 63 20 3d 20 63       prior_c = c
20b1c 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  2;.        }.   
20b1d 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65       c2 = sqlite
20b1e 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 65  3Utf8Read(zPatte
20b1f 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74 65 72 6e  rn, 0, &zPattern
20b20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
20b21 20 69 66 28 20 63 32 3d 3d 30 20 7c 7c 20 28 73   if( c2==0 || (s
20b22 65 65 6e 20 5e 20 69 6e 76 65 72 74 29 3d 3d 30  een ^ invert)==0
20b23 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
20b24 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 0;.      }.  
20b25 20 20 7d 65 6c 73 65 20 69 66 28 20 65 73 63 3d    }else if( esc=
20b26 3d 63 20 26 26 20 21 70 72 65 76 45 73 63 61 70  =c && !prevEscap
20b27 65 20 29 7b 0a 20 20 20 20 20 20 70 72 65 76 45  e ){.      prevE
20b28 73 63 61 70 65 20 3d 20 31 3b 0a 20 20 20 20 7d  scape = 1;.    }
20b29 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 32 20 3d  else{.      c2 =
20b2a 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
20b2b 28 7a 53 74 72 69 6e 67 2c 20 30 2c 20 26 7a 53  (zString, 0, &zS
20b2c 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66  tring);.      if
20b2d 28 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20 20 20  ( noCase ){.    
20b2e 20 20 20 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c      GlogUpperToL
20b2f 6f 77 65 72 28 63 29 3b 0a 20 20 20 20 20 20 20  ower(c);.       
20b30 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65   GlogUpperToLowe
20b31 72 28 63 32 29 3b 0a 20 20 20 20 20 20 7d 0a 20  r(c2);.      }. 
20b32 20 20 20 20 20 69 66 28 20 63 21 3d 63 32 20 29       if( c!=c2 )
20b33 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
20b34 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
20b35 20 20 70 72 65 76 45 73 63 61 70 65 20 3d 20 30    prevEscape = 0
20b36 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
20b37 74 75 72 6e 20 2a 7a 53 74 72 69 6e 67 3d 3d 30  turn *zString==0
20b38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74  ;.}../*.** Count
20b39 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   the number of t
20b3a 69 6d 65 73 20 74 68 61 74 20 74 68 65 20 4c 49  imes that the LI
20b3b 4b 45 20 6f 70 65 72 61 74 6f 72 20 28 6f 72 20  KE operator (or 
20b3c 47 4c 4f 42 20 77 68 69 63 68 20 69 73 0a 2a 2a  GLOB which is.**
20b3d 20 6a 75 73 74 20 61 20 76 61 72 69 61 74 69 6f   just a variatio
20b3e 6e 20 6f 66 20 4c 49 4b 45 29 20 67 65 74 73 20  n of LIKE) gets 
20b3f 63 61 6c 6c 65 64 2e 20 20 54 68 69 73 20 69 73  called.  This is
20b40 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   used for testin
20b41 67 0a 2a 2a 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69  g.** only..*/.#i
20b42 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
20b43 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
20b44 73 71 6c 69 74 65 33 5f 6c 69 6b 65 5f 63 6f 75  sqlite3_like_cou
20b45 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  nt = 0;.#endif..
20b46 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
20b47 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 6b  ation of the lik
20b48 65 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  e() SQL function
20b49 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  .  This function
20b4a 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74   implements.** t
20b4b 68 65 20 62 75 69 6c 64 2d 69 6e 20 4c 49 4b 45  he build-in LIKE
20b4c 20 6f 70 65 72 61 74 6f 72 2e 20 20 54 68 65 20   operator.  The 
20b4d 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  first argument t
20b4e 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69  o the function i
20b4f 73 20 74 68 65 0a 2a 2a 20 70 61 74 74 65 72 6e  s the.** pattern
20b50 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20   and the second 
20b51 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
20b52 73 74 72 69 6e 67 2e 20 20 53 6f 2c 20 74 68 65  string.  So, the
20b53 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 3a   SQL statements:
20b54 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 41 20 4c  .**.**       A L
20b55 49 4b 45 20 42 0a 2a 2a 0a 2a 2a 20 69 73 20 69  IKE B.**.** is i
20b56 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 6c 69  mplemented as li
20b57 6b 65 28 42 2c 41 29 2e 0a 2a 2a 0a 2a 2a 20 54  ke(B,A)..**.** T
20b58 68 69 73 20 73 61 6d 65 20 66 75 6e 63 74 69 6f  his same functio
20b59 6e 20 28 77 69 74 68 20 61 20 64 69 66 66 65 72  n (with a differ
20b5a 65 6e 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20  ent compareInfo 
20b5b 73 74 72 75 63 74 75 72 65 29 20 63 6f 6d 70 75  structure) compu
20b5c 74 65 73 0a 2a 2a 20 74 68 65 20 47 4c 4f 42 20  tes.** the GLOB 
20b5d 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61  operator..*/.sta
20b5e 74 69 63 20 76 6f 69 64 20 6c 69 6b 65 46 75 6e  tic void likeFun
20b5f 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
20b60 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a  text *context, .
20b61 20 20 69 6e 74 20 61 72 67 63 2c 20 0a 20 20 73    int argc, .  s
20b62 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
20b63 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75  rgv.){.  const u
20b64 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 41  nsigned char *zA
20b65 2c 20 2a 7a 42 3b 0a 20 20 69 6e 74 20 65 73 63  , *zB;.  int esc
20b66 61 70 65 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  ape = 0;.  sqlit
20b67 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
20b68 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
20b69 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20  le(context);..  
20b6a 7a 42 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  zB = sqlite3_val
20b6b 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
20b6c 3b 0a 20 20 7a 41 20 3d 20 73 71 6c 69 74 65 33  ;.  zA = sqlite3
20b6d 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
20b6e 5b 31 5d 29 3b 0a 0a 20 20 2f 2a 20 4c 69 6d 69  [1]);..  /* Limi
20b6f 74 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20  t the length of 
20b70 74 68 65 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42  the LIKE or GLOB
20b71 20 70 61 74 74 65 72 6e 20 74 6f 20 61 76 6f 69   pattern to avoi
20b72 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20  d problems.  ** 
20b73 6f 66 20 64 65 65 70 20 72 65 63 75 72 73 69 6f  of deep recursio
20b74 6e 20 61 6e 64 20 4e 2a 4e 20 62 65 68 61 76 69  n and N*N behavi
20b75 6f 72 20 69 6e 20 70 61 74 74 65 72 6e 43 6f 6d  or in patternCom
20b76 70 61 72 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 69  pare()..  */.  i
20b77 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f( sqlite3_value
20b78 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 20  _bytes(argv[0]) 
20b79 3e 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 4c  >.        db->aL
20b7a 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
20b7b 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c  T_LIKE_PATTERN_L
20b7c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 73 71  ENGTH] ){.    sq
20b7d 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
20b7e 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 4c 49 4b  or(context, "LIK
20b7f 45 20 6f 72 20 47 4c 4f 42 20 70 61 74 74 65 72  E or GLOB patter
20b80 6e 20 74 6f 6f 20 63 6f 6d 70 6c 65 78 22 2c 20  n too complex", 
20b81 2d 31 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  -1);.    return;
20b82 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 7a  .  }.  assert( z
20b83 42 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  B==sqlite3_value
20b84 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29  _text(argv[0]) )
20b85 3b 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 64  ;  /* Encoding d
20b86 69 64 20 6e 6f 74 20 63 68 61 6e 67 65 20 2a 2f  id not change */
20b87 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 33 20  ..  if( argc==3 
20b88 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 73  ){.    /* The es
20b89 63 61 70 65 20 63 68 61 72 61 63 74 65 72 20 73  cape character s
20b8a 74 72 69 6e 67 20 6d 75 73 74 20 63 6f 6e 73 69  tring must consi
20b8b 73 74 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 55  st of a single U
20b8c 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e 0a  TF-8 character..
20b8d 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65      ** Otherwise
20b8e 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  , return an erro
20b8f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 6f  r..    */.    co
20b90 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
20b91 72 20 2a 7a 45 73 63 20 3d 20 73 71 6c 69 74 65  r *zEsc = sqlite
20b92 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
20b93 76 5b 32 5d 29 3b 0a 20 20 20 20 69 66 28 20 7a  v[2]);.    if( z
20b94 45 73 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  Esc==0 ) return;
20b95 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
20b96 55 74 66 38 43 68 61 72 4c 65 6e 28 28 63 68 61  Utf8CharLen((cha
20b97 72 2a 29 7a 45 73 63 2c 20 2d 31 29 21 3d 31 20  r*)zEsc, -1)!=1 
20b98 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
20b99 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f  _result_error(co
20b9a 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20 20 20  ntext, .        
20b9b 20 20 22 45 53 43 41 50 45 20 65 78 70 72 65 73    "ESCAPE expres
20b9c 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 61 20 73  sion must be a s
20b9d 69 6e 67 6c 65 20 63 68 61 72 61 63 74 65 72 22  ingle character"
20b9e 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 72 65 74  , -1);.      ret
20b9f 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65  urn;.    }.    e
20ba0 73 63 61 70 65 20 3d 20 73 71 6c 69 74 65 33 55  scape = sqlite3U
20ba1 74 66 38 52 65 61 64 28 7a 45 73 63 2c 20 30 2c  tf8Read(zEsc, 0,
20ba2 20 26 7a 45 73 63 29 3b 0a 20 20 7d 0a 20 20 69   &zEsc);.  }.  i
20ba3 66 28 20 7a 41 20 26 26 20 7a 42 20 29 7b 0a 20  f( zA && zB ){. 
20ba4 20 20 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72     struct compar
20ba5 65 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 73  eInfo *pInfo = s
20ba6 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
20ba7 28 63 6f 6e 74 65 78 74 29 3b 0a 23 69 66 64 65  (context);.#ifde
20ba8 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
20ba9 20 20 73 71 6c 69 74 65 33 5f 6c 69 6b 65 5f 63    sqlite3_like_c
20baa 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20  ount++;.#endif. 
20bab 20 20 20 0a 20 20 20 20 73 71 6c 69 74 65 33 5f     .    sqlite3_
20bac 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65  result_int(conte
20bad 78 74 2c 20 70 61 74 74 65 72 6e 43 6f 6d 70 61  xt, patternCompa
20bae 72 65 28 7a 42 2c 20 7a 41 2c 20 70 49 6e 66 6f  re(zB, zA, pInfo
20baf 2c 20 65 73 63 61 70 65 29 29 3b 0a 20 20 7d 0a  , escape));.  }.
20bb0 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
20bb1 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4e  ntation of the N
20bb2 55 4c 4c 49 46 28 78 2c 79 29 20 66 75 6e 63 74  ULLIF(x,y) funct
20bb3 69 6f 6e 2e 20 20 54 68 65 20 72 65 73 75 6c 74  ion.  The result
20bb4 20 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a   is the first.**
20bb5 20 61 72 67 75 6d 65 6e 74 20 69 66 20 74 68 65   argument if the
20bb6 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 64   arguments are d
20bb7 69 66 66 65 72 65 6e 74 2e 20 20 54 68 65 20 72  ifferent.  The r
20bb8 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 69 66  esult is NULL if
20bb9 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74   the.** argument
20bba 73 20 61 72 65 20 65 71 75 61 6c 20 74 6f 20 65  s are equal to e
20bbb 61 63 68 20 6f 74 68 65 72 2e 0a 2a 2f 0a 73 74  ach other..*/.st
20bbc 61 74 69 63 20 76 6f 69 64 20 6e 75 6c 6c 69 66  atic void nullif
20bbd 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
20bbe 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
20bbf 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
20bc0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
20bc1 61 72 67 76 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65  argv.){.  CollSe
20bc2 71 20 2a 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74  q *pColl = sqlit
20bc3 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71  e3GetFuncCollSeq
20bc4 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 69 66 28  (context);.  if(
20bc5 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61   sqlite3MemCompa
20bc6 72 65 28 61 72 67 76 5b 30 5d 2c 20 61 72 67 76  re(argv[0], argv
20bc7 5b 31 5d 2c 20 70 43 6f 6c 6c 29 21 3d 30 20 29  [1], pColl)!=0 )
20bc8 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
20bc9 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65  sult_value(conte
20bca 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0a 20 20  xt, argv[0]);.  
20bcb 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  }.}../*.** Imple
20bcc 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
20bcd 20 56 45 52 53 49 4f 4e 28 2a 29 20 66 75 6e 63   VERSION(*) func
20bce 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 73 75 6c  tion.  The resul
20bcf 74 20 69 73 20 74 68 65 20 76 65 72 73 69 6f 6e  t is the version
20bd0 0a 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c 69 74  .** of the SQLit
20bd1 65 20 6c 69 62 72 61 72 79 20 74 68 61 74 20 69  e library that i
20bd2 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2f 0a 73 74  s running..*/.st
20bd3 61 74 69 63 20 76 6f 69 64 20 76 65 72 73 69 6f  atic void versio
20bd4 6e 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  nFunc(.  sqlite3
20bd5 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
20bd6 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
20bd7 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
20bd8 2a 61 72 67 76 0a 29 7b 0a 20 20 73 71 6c 69 74  *argv.){.  sqlit
20bd9 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63  e3_result_text(c
20bda 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f  ontext, sqlite3_
20bdb 76 65 72 73 69 6f 6e 2c 20 2d 31 2c 20 53 51 4c  version, -1, SQL
20bdc 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a  ITE_STATIC);.}..
20bdd 2f 2a 20 41 72 72 61 79 20 66 6f 72 20 63 6f 6e  /* Array for con
20bde 76 65 72 74 69 6e 67 20 66 72 6f 6d 20 68 61 6c  verting from hal
20bdf 66 2d 62 79 74 65 73 20 28 6e 79 62 62 6c 65 73  f-bytes (nybbles
20be0 29 20 69 6e 74 6f 20 41 53 43 49 49 20 68 65 78  ) into ASCII hex
20be1 0a 2a 2a 20 64 69 67 69 74 73 2e 20 2a 2f 0a 73  .** digits. */.s
20be2 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
20be3 20 68 65 78 64 69 67 69 74 73 5b 5d 20 3d 20 7b   hexdigits[] = {
20be4 0a 20 20 27 30 27 2c 20 27 31 27 2c 20 27 32 27  .  '0', '1', '2'
20be5 2c 20 27 33 27 2c 20 27 34 27 2c 20 27 35 27 2c  , '3', '4', '5',
20be6 20 27 36 27 2c 20 27 37 27 2c 0a 20 20 27 38 27   '6', '7',.  '8'
20be7 2c 20 27 39 27 2c 20 27 41 27 2c 20 27 42 27 2c  , '9', 'A', 'B',
20be8 20 27 43 27 2c 20 27 44 27 2c 20 27 45 27 2c 20   'C', 'D', 'E', 
20be9 27 46 27 20 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45  'F' .};../*.** E
20bea 58 50 45 52 49 4d 45 4e 54 41 4c 20 2d 20 54 68  XPERIMENTAL - Th
20beb 69 73 20 69 73 20 6e 6f 74 20 61 6e 20 6f 66 66  is is not an off
20bec 69 63 69 61 6c 20 66 75 6e 63 74 69 6f 6e 2e 20  icial function. 
20bed 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 6d   The interface m
20bee 61 79 0a 2a 2a 20 63 68 61 6e 67 65 2e 20 20 54  ay.** change.  T
20bef 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79  his function may
20bf0 20 64 69 73 61 70 70 65 61 72 2e 20 20 44 6f 20   disappear.  Do 
20bf1 6e 6f 74 20 77 72 69 74 65 20 63 6f 64 65 20 74  not write code t
20bf2 68 61 74 20 64 65 70 65 6e 64 73 0a 2a 2a 20 6f  hat depends.** o
20bf3 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  n this function.
20bf4 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  .**.** Implement
20bf5 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 51 55 4f  ation of the QUO
20bf6 54 45 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20  TE() function.  
20bf7 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 61  This function ta
20bf8 6b 65 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20  kes a single.** 
20bf9 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 74 68  argument.  If th
20bfa 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 75  e argument is nu
20bfb 6d 65 72 69 63 2c 20 74 68 65 20 72 65 74 75 72  meric, the retur
20bfc 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 73  n value is the s
20bfd 61 6d 65 20 61 73 0a 2a 2a 20 74 68 65 20 61 72  ame as.** the ar
20bfe 67 75 6d 65 6e 74 2e 20 20 49 66 20 74 68 65 20  gument.  If the 
20bff 61 72 67 75 6d 65 6e 74 20 69 73 20 4e 55 4c 4c  argument is NULL
20c00 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  , the return val
20c01 75 65 20 69 73 20 74 68 65 20 73 74 72 69 6e 67  ue is the string
20c02 0a 2a 2a 20 22 4e 55 4c 4c 22 2e 20 20 4f 74 68  .** "NULL".  Oth
20c03 65 72 77 69 73 65 2c 20 74 68 65 20 61 72 67 75  erwise, the argu
20c04 6d 65 6e 74 20 69 73 20 65 6e 63 6c 6f 73 65 64  ment is enclosed
20c05 20 69 6e 20 73 69 6e 67 6c 65 20 71 75 6f 74 65   in single quote
20c06 73 20 77 69 74 68 0a 2a 2a 20 73 69 6e 67 6c 65  s with.** single
20c07 2d 71 75 6f 74 65 20 65 73 63 61 70 65 73 2e 0a  -quote escapes..
20c08 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 71  */.static void q
20c09 75 6f 74 65 46 75 6e 63 28 73 71 6c 69 74 65 33  uoteFunc(sqlite3
20c0a 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
20c0b 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
20c0c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
20c0d 76 29 7b 0a 20 20 69 66 28 20 61 72 67 63 3c 31  v){.  if( argc<1
20c0e 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 77 69   ) return;.  swi
20c0f 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61 6c  tch( sqlite3_val
20c10 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29  ue_type(argv[0])
20c11 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c   ){.    case SQL
20c12 49 54 45 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20  ITE_NULL: {.    
20c13 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
20c14 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 22  _text(context, "
20c15 4e 55 4c 4c 22 2c 20 34 2c 20 53 51 4c 49 54 45  NULL", 4, SQLITE
20c16 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
20c17 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
20c18 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54   case SQLITE_INT
20c19 45 47 45 52 3a 0a 20 20 20 20 63 61 73 65 20 53  EGER:.    case S
20c1a 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 7b 0a 20  QLITE_FLOAT: {. 
20c1b 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
20c1c 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78  ult_value(contex
20c1d 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  t, argv[0]);.   
20c1e 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
20c1f 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
20c20 42 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 63 68  BLOB: {.      ch
20c21 61 72 20 2a 7a 54 65 78 74 20 3d 20 30 3b 0a 20  ar *zText = 0;. 
20c22 20 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20       char const 
20c23 2a 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33  *zBlob = sqlite3
20c24 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76  _value_blob(argv
20c25 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 6e 74 20  [0]);.      int 
20c26 6e 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f  nBlob = sqlite3_
20c27 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
20c28 5b 30 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65  [0]);.      asse
20c29 72 74 28 20 7a 42 6c 6f 62 3d 3d 73 71 6c 69 74  rt( zBlob==sqlit
20c2a 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72  e3_value_blob(ar
20c2b 67 76 5b 30 5d 29 20 29 3b 20 2f 2a 20 4e 6f 20  gv[0]) ); /* No 
20c2c 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 20  encoding change 
20c2d 2a 2f 0a 20 20 20 20 20 20 7a 54 65 78 74 20 3d  */.      zText =
20c2e 20 28 63 68 61 72 20 2a 29 63 6f 6e 74 65 78 74   (char *)context
20c2f 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20  Malloc(context, 
20c30 28 32 2a 28 69 36 34 29 6e 42 6c 6f 62 29 2b 34  (2*(i64)nBlob)+4
20c31 29 3b 20 0a 20 20 20 20 20 20 69 66 28 20 7a 54  ); .      if( zT
20c32 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ext ){.        i
20c33 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 66 6f  nt i;.        fo
20c34 72 28 69 3d 30 3b 20 69 3c 6e 42 6c 6f 62 3b 20  r(i=0; i<nBlob; 
20c35 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
20c36 7a 54 65 78 74 5b 28 69 2a 32 29 2b 32 5d 20 3d  zText[(i*2)+2] =
20c37 20 68 65 78 64 69 67 69 74 73 5b 28 7a 42 6c 6f   hexdigits[(zBlo
20c38 62 5b 69 5d 3e 3e 34 29 26 30 78 30 46 5d 3b 0a  b[i]>>4)&0x0F];.
20c39 20 20 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b            zText[
20c3a 28 69 2a 32 29 2b 33 5d 20 3d 20 68 65 78 64 69  (i*2)+3] = hexdi
20c3b 67 69 74 73 5b 28 7a 42 6c 6f 62 5b 69 5d 29 26  gits[(zBlob[i])&
20c3c 30 78 30 46 5d 3b 0a 20 20 20 20 20 20 20 20 7d  0x0F];.        }
20c3d 0a 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b 28  .        zText[(
20c3e 6e 42 6c 6f 62 2a 32 29 2b 32 5d 20 3d 20 27 5c  nBlob*2)+2] = '\
20c3f 27 27 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 78  '';.        zTex
20c40 74 5b 28 6e 42 6c 6f 62 2a 32 29 2b 33 5d 20 3d  t[(nBlob*2)+3] =
20c41 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 7a   '\0';.        z
20c42 54 65 78 74 5b 30 5d 20 3d 20 27 58 27 3b 0a 20  Text[0] = 'X';. 
20c43 20 20 20 20 20 20 20 7a 54 65 78 74 5b 31 5d 20         zText[1] 
20c44 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20  = '\'';.        
20c45 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
20c46 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 54 65  ext(context, zTe
20c47 78 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54  xt, -1, SQLITE_T
20c48 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20  RANSIENT);.     
20c49 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
20c4a 7a 54 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a  zText);.      }.
20c4b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
20c4c 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49   }.    case SQLI
20c4d 54 45 5f 54 45 58 54 3a 20 7b 0a 20 20 20 20 20  TE_TEXT: {.     
20c4e 20 69 6e 74 20 69 2c 6a 3b 0a 20 20 20 20 20 20   int i,j;.      
20c4f 75 36 34 20 6e 3b 0a 20 20 20 20 20 20 63 6f 6e  u64 n;.      con
20c50 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
20c51 20 2a 7a 41 72 67 20 3d 20 73 71 6c 69 74 65 33   *zArg = sqlite3
20c52 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
20c53 5b 30 5d 29 3b 0a 20 20 20 20 20 20 63 68 61 72  [0]);.      char
20c54 20 2a 7a 3b 0a 0a 20 20 20 20 20 20 69 66 28 20   *z;..      if( 
20c55 7a 41 72 67 3d 3d 30 20 29 20 72 65 74 75 72 6e  zArg==0 ) return
20c56 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c  ;.      for(i=0,
20c57 20 6e 3d 30 3b 20 7a 41 72 67 5b 69 5d 3b 20 69   n=0; zArg[i]; i
20c58 2b 2b 29 7b 20 69 66 28 20 7a 41 72 67 5b 69 5d  ++){ if( zArg[i]
20c59 3d 3d 27 5c 27 27 20 29 20 6e 2b 2b 3b 20 7d 0a  =='\'' ) n++; }.
20c5a 20 20 20 20 20 20 7a 20 3d 20 63 6f 6e 74 65 78        z = contex
20c5b 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c  tMalloc(context,
20c5c 20 28 28 69 36 34 29 69 29 2b 28 28 69 36 34 29   ((i64)i)+((i64)
20c5d 6e 29 2b 33 29 3b 0a 20 20 20 20 20 20 69 66 28  n)+3);.      if(
20c5e 20 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 5b   z ){.        z[
20c5f 30 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20  0] = '\'';.     
20c60 20 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 31 3b     for(i=0, j=1;
20c61 20 7a 41 72 67 5b 69 5d 3b 20 69 2b 2b 29 7b 0a   zArg[i]; i++){.
20c62 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d            z[j++]
20c63 20 3d 20 7a 41 72 67 5b 69 5d 3b 0a 20 20 20 20   = zArg[i];.    
20c64 20 20 20 20 20 20 69 66 28 20 7a 41 72 67 5b 69        if( zArg[i
20c65 5d 3d 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20 20  ]=='\'' ){.     
20c66 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20         z[j++] = 
20c67 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 20 20  '\'';.          
20c68 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
20c69 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 5c 27      z[j++] = '\'
20c6a 27 3b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 5d 20  ';.        z[j] 
20c6b 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  = 0;.        sql
20c6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
20c6d 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 6a 2c 20  (context, z, j, 
20c6e 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20  sqlite3_free);. 
20c6f 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
20c70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65  .}../*.** The he
20c71 78 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 49  x() function.  I
20c72 6e 74 65 72 70 72 65 74 20 74 68 65 20 61 72 67  nterpret the arg
20c73 75 6d 65 6e 74 20 61 73 20 61 20 62 6c 6f 62 2e  ument as a blob.
20c74 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 68 65    Return.** a he
20c75 78 61 64 65 63 69 6d 61 6c 20 72 65 6e 64 65 72  xadecimal render
20c76 69 6e 67 20 61 73 20 74 65 78 74 2e 0a 2a 2f 0a  ing as text..*/.
20c77 73 74 61 74 69 63 20 76 6f 69 64 20 68 65 78 46  static void hexF
20c78 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
20c79 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
20c7a 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
20c7b 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
20c7c 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20  rgv.){.  int i, 
20c7d 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  n;.  const unsig
20c7e 6e 65 64 20 63 68 61 72 20 2a 70 42 6c 6f 62 3b  ned char *pBlob;
20c7f 0a 20 20 63 68 61 72 20 2a 7a 48 65 78 2c 20 2a  .  char *zHex, *
20c80 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 67  z;.  assert( arg
20c81 63 3d 3d 31 20 29 3b 0a 20 20 70 42 6c 6f 62 20  c==1 );.  pBlob 
20c82 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
20c83 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0a 20  blob(argv[0]);. 
20c84 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c   n = sqlite3_val
20c85 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
20c86 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 6c  );.  assert( pBl
20c87 6f 62 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75  ob==sqlite3_valu
20c88 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 20  e_blob(argv[0]) 
20c89 29 3b 20 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69  );  /* No encodi
20c8a 6e 67 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 7a  ng change */.  z
20c8b 20 3d 20 7a 48 65 78 20 3d 20 63 6f 6e 74 65 78   = zHex = contex
20c8c 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c  tMalloc(context,
20c8d 20 28 28 69 36 34 29 6e 29 2a 32 20 2b 20 31 29   ((i64)n)*2 + 1)
20c8e 3b 0a 20 20 69 66 28 20 7a 48 65 78 20 29 7b 0a  ;.  if( zHex ){.
20c8f 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
20c90 3b 20 69 2b 2b 2c 20 70 42 6c 6f 62 2b 2b 29 7b  ; i++, pBlob++){
20c91 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  .      unsigned 
20c92 63 68 61 72 20 63 20 3d 20 2a 70 42 6c 6f 62 3b  char c = *pBlob;
20c93 0a 20 20 20 20 20 20 2a 28 7a 2b 2b 29 20 3d 20  .      *(z++) = 
20c94 68 65 78 64 69 67 69 74 73 5b 28 63 3e 3e 34 29  hexdigits[(c>>4)
20c95 26 30 78 66 5d 3b 0a 20 20 20 20 20 20 2a 28 7a  &0xf];.      *(z
20c96 2b 2b 29 20 3d 20 68 65 78 64 69 67 69 74 73 5b  ++) = hexdigits[
20c97 63 26 30 78 66 5d 3b 0a 20 20 20 20 7d 0a 20 20  c&0xf];.    }.  
20c98 20 20 2a 7a 20 3d 20 30 3b 0a 20 20 20 20 73 71    *z = 0;.    sq
20c99 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
20c9a 74 28 63 6f 6e 74 65 78 74 2c 20 7a 48 65 78 2c  t(context, zHex,
20c9b 20 6e 2a 32 2c 20 73 71 6c 69 74 65 33 5f 66 72   n*2, sqlite3_fr
20c9c 65 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ee);.  }.}../*.*
20c9d 2a 20 54 68 65 20 7a 65 72 6f 62 6c 6f 62 28 4e  * The zeroblob(N
20c9e 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  ) function retur
20c9f 6e 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64  ns a zero-filled
20ca0 20 62 6c 6f 62 20 6f 66 20 73 69 7a 65 20 4e 20   blob of size N 
20ca1 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  bytes..*/.static
20ca2 20 76 6f 69 64 20 7a 65 72 6f 62 6c 6f 62 46 75   void zeroblobFu
20ca3 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
20ca4 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
20ca5 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
20ca6 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
20ca7 67 76 0a 29 7b 0a 20 20 69 36 34 20 6e 3b 0a 20  gv.){.  i64 n;. 
20ca8 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31   assert( argc==1
20ca9 20 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65   );.  n = sqlite
20caa 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72  3_value_int64(ar
20cab 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 6e 3e  gv[0]);.  if( n>
20cac 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54  SQLITE_MAX_LENGT
20cad 48 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  H ){.    sqlite3
20cae 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f  _result_error_to
20caf 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20  obig(context);. 
20cb0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
20cb1 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62  te3_result_zerob
20cb2 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20 6e 29 3b  lob(context, n);
20cb3 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
20cb4 65 20 72 65 70 6c 61 63 65 28 29 20 66 75 6e 63  e replace() func
20cb5 74 69 6f 6e 2e 20 20 54 68 72 65 65 20 61 72 67  tion.  Three arg
20cb6 75 6d 65 6e 74 73 20 61 72 65 20 61 6c 6c 20 73  uments are all s
20cb7 74 72 69 6e 67 73 3a 20 63 61 6c 6c 0a 2a 2a 20  trings: call.** 
20cb8 74 68 65 6d 20 41 2c 20 42 2c 20 61 6e 64 20 43  them A, B, and C
20cb9 2e 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20  . The result is 
20cba 61 6c 73 6f 20 61 20 73 74 72 69 6e 67 20 77 68  also a string wh
20cbb 69 63 68 20 69 73 20 64 65 72 69 76 65 64 0a 2a  ich is derived.*
20cbc 2a 20 66 72 6f 6d 20 41 20 62 79 20 72 65 70 6c  * from A by repl
20cbd 61 63 69 6e 67 20 65 76 65 72 79 20 6f 63 63 75  acing every occu
20cbe 72 61 6e 63 65 20 6f 66 20 42 20 77 69 74 68 20  rance of B with 
20cbf 43 2e 20 20 54 68 65 20 6d 61 74 63 68 0a 2a 2a  C.  The match.**
20cc0 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 2e 20   must be exact. 
20cc1 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   Collating seque
20cc2 6e 63 65 73 20 61 72 65 20 6e 6f 74 20 75 73 65  nces are not use
20cc3 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
20cc4 64 20 72 65 70 6c 61 63 65 46 75 6e 63 28 0a 20  d replaceFunc(. 
20cc5 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
20cc6 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
20cc7 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
20cc8 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
20cc9 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  .  const unsigne
20cca 64 20 63 68 61 72 20 2a 7a 53 74 72 3b 20 20 20  d char *zStr;   
20ccb 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 70 75       /* The inpu
20ccc 74 20 73 74 72 69 6e 67 20 41 20 2a 2f 0a 20 20  t string A */.  
20ccd 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
20cce 68 61 72 20 2a 7a 50 61 74 74 65 72 6e 3b 20 20  har *zPattern;  
20ccf 20 20 2f 2a 20 54 68 65 20 70 61 74 74 65 72 6e    /* The pattern
20cd0 20 73 74 72 69 6e 67 20 42 20 2a 2f 0a 20 20 63   string B */.  c
20cd1 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
20cd2 61 72 20 2a 7a 52 65 70 3b 20 20 20 20 20 20 20  ar *zRep;       
20cd3 20 2f 2a 20 54 68 65 20 72 65 70 6c 61 63 65 6d   /* The replacem
20cd4 65 6e 74 20 73 74 72 69 6e 67 20 43 20 2a 2f 0a  ent string C */.
20cd5 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
20cd6 2a 7a 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20  *zOut;          
20cd7 20 20 20 20 2f 2a 20 54 68 65 20 6f 75 74 70 75      /* The outpu
20cd8 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 74 72 3b  t */.  int nStr;
20cd9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20cda 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 53 74 72 20  /* Size of zStr 
20cdb 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 74 74 65 72  */.  int nPatter
20cdc 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  n;            /*
20cdd 20 53 69 7a 65 20 6f 66 20 7a 50 61 74 74 65 72   Size of zPatter
20cde 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 70 3b  n */.  int nRep;
20cdf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20ce0 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 52 65 70 20  /* Size of zRep 
20ce1 2a 2f 0a 20 20 69 36 34 20 6e 4f 75 74 3b 20 20  */.  i64 nOut;  
20ce2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
20ce3 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66   Maximum size of
20ce4 20 7a 4f 75 74 20 2a 2f 0a 20 20 69 6e 74 20 6c   zOut */.  int l
20ce5 6f 6f 70 4c 69 6d 69 74 3b 20 20 20 20 20 20 20  oopLimit;       
20ce6 20 20 20 20 2f 2a 20 4c 61 73 74 20 7a 53 74 72      /* Last zStr
20ce7 5b 5d 20 74 68 61 74 20 6d 69 67 68 74 20 6d 61  [] that might ma
20ce8 74 63 68 20 7a 50 61 74 74 65 72 6e 5b 5d 20 2a  tch zPattern[] *
20ce9 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20  /.  int i, j;   
20cea 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
20ceb 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f  Loop counters */
20cec 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  ..  assert( argc
20ced 3d 3d 33 20 29 3b 0a 20 20 7a 53 74 72 20 3d 20  ==3 );.  zStr = 
20cee 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
20cef 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69  xt(argv[0]);.  i
20cf0 66 28 20 7a 53 74 72 3d 3d 30 20 29 20 72 65 74  f( zStr==0 ) ret
20cf1 75 72 6e 3b 0a 20 20 6e 53 74 72 20 3d 20 73 71  urn;.  nStr = sq
20cf2 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
20cf3 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 61 73  s(argv[0]);.  as
20cf4 73 65 72 74 28 20 7a 53 74 72 3d 3d 73 71 6c 69  sert( zStr==sqli
20cf5 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
20cf6 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 4e  rgv[0]) );  /* N
20cf7 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67  o encoding chang
20cf8 65 20 2a 2f 0a 20 20 7a 50 61 74 74 65 72 6e 20  e */.  zPattern 
20cf9 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
20cfa 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20  text(argv[1]);. 
20cfb 20 69 66 28 20 7a 50 61 74 74 65 72 6e 3d 3d 30   if( zPattern==0
20cfc 20 7c 7c 20 7a 50 61 74 74 65 72 6e 5b 30 5d 3d   || zPattern[0]=
20cfd 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6e  =0 ) return;.  n
20cfe 50 61 74 74 65 72 6e 20 3d 20 73 71 6c 69 74 65  Pattern = sqlite
20cff 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
20d00 67 76 5b 31 5d 29 3b 0a 20 20 61 73 73 65 72 74  gv[1]);.  assert
20d01 28 20 7a 50 61 74 74 65 72 6e 3d 3d 73 71 6c 69  ( zPattern==sqli
20d02 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
20d03 72 67 76 5b 31 5d 29 20 29 3b 20 20 2f 2a 20 4e  rgv[1]) );  /* N
20d04 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67  o encoding chang
20d05 65 20 2a 2f 0a 20 20 7a 52 65 70 20 3d 20 73 71  e */.  zRep = sq
20d06 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
20d07 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 69 66 28  (argv[2]);.  if(
20d08 20 7a 52 65 70 3d 3d 30 20 29 20 72 65 74 75 72   zRep==0 ) retur
20d09 6e 3b 0a 20 20 6e 52 65 70 20 3d 20 73 71 6c 69  n;.  nRep = sqli
20d0a 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
20d0b 61 72 67 76 5b 32 5d 29 3b 0a 20 20 61 73 73 65  argv[2]);.  asse
20d0c 72 74 28 20 7a 52 65 70 3d 3d 73 71 6c 69 74 65  rt( zRep==sqlite
20d0d 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
20d0e 76 5b 32 5d 29 20 29 3b 0a 20 20 6e 4f 75 74 20  v[2]) );.  nOut 
20d0f 3d 20 6e 53 74 72 20 2b 20 31 3b 0a 20 20 61 73  = nStr + 1;.  as
20d10 73 65 72 74 28 20 6e 4f 75 74 3c 53 51 4c 49 54  sert( nOut<SQLIT
20d11 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29 3b 0a  E_MAX_LENGTH );.
20d12 20 20 7a 4f 75 74 20 3d 20 63 6f 6e 74 65 78 74    zOut = context
20d13 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20  Malloc(context, 
20d14 28 69 36 34 29 6e 4f 75 74 29 3b 0a 20 20 69 66  (i64)nOut);.  if
20d15 28 20 7a 4f 75 74 3d 3d 30 20 29 7b 0a 20 20 20  ( zOut==0 ){.   
20d16 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6c   return;.  }.  l
20d17 6f 6f 70 4c 69 6d 69 74 20 3d 20 6e 53 74 72 20  oopLimit = nStr 
20d18 2d 20 6e 50 61 74 74 65 72 6e 3b 20 20 0a 20 20  - nPattern;  .  
20d19 66 6f 72 28 69 3d 6a 3d 30 3b 20 69 3c 3d 6c 6f  for(i=j=0; i<=lo
20d1a 6f 70 4c 69 6d 69 74 3b 20 69 2b 2b 29 7b 0a 20  opLimit; i++){. 
20d1b 20 20 20 69 66 28 20 7a 53 74 72 5b 69 5d 21 3d     if( zStr[i]!=
20d1c 7a 50 61 74 74 65 72 6e 5b 30 5d 20 7c 7c 20 6d  zPattern[0] || m
20d1d 65 6d 63 6d 70 28 26 7a 53 74 72 5b 69 5d 2c 20  emcmp(&zStr[i], 
20d1e 7a 50 61 74 74 65 72 6e 2c 20 6e 50 61 74 74 65  zPattern, nPatte
20d1f 72 6e 29 20 29 7b 0a 20 20 20 20 20 20 7a 4f 75  rn) ){.      zOu
20d20 74 5b 6a 2b 2b 5d 20 3d 20 7a 53 74 72 5b 69 5d  t[j++] = zStr[i]
20d21 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
20d22 20 20 20 75 38 20 2a 7a 4f 6c 64 3b 0a 20 20 20     u8 *zOld;.   
20d23 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d     sqlite3 *db =
20d24 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
20d25 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65  _db_handle(conte
20d26 78 74 29 3b 0a 20 20 20 20 20 20 6e 4f 75 74 20  xt);.      nOut 
20d27 2b 3d 20 6e 52 65 70 20 2d 20 6e 50 61 74 74 65  += nRep - nPatte
20d28 72 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 4f  rn;.      if( nO
20d29 75 74 3e 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  ut>=db->aLimit[S
20d2a 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
20d2b 54 48 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 73  TH] ){.        s
20d2c 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
20d2d 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65  ror_toobig(conte
20d2e 78 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  xt);.        sql
20d2f 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
20d30 4f 75 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65  Out);.        re
20d31 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
20d32 20 20 20 20 7a 4f 6c 64 20 3d 20 7a 4f 75 74 3b      zOld = zOut;
20d33 0a 20 20 20 20 20 20 7a 4f 75 74 20 3d 20 73 71  .      zOut = sq
20d34 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 7a 4f  lite3_realloc(zO
20d35 75 74 2c 20 28 69 6e 74 29 6e 4f 75 74 29 3b 0a  ut, (int)nOut);.
20d36 20 20 20 20 20 20 69 66 28 20 7a 4f 75 74 3d 3d        if( zOut==
20d37 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  0 ){.        sql
20d38 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
20d39 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29  r_nomem(context)
20d3a 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
20d3b 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4f 6c 64  3DbFree(db, zOld
20d3c 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
20d3d 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  n;.      }.     
20d3e 20 6d 65 6d 63 70 79 28 26 7a 4f 75 74 5b 6a 5d   memcpy(&zOut[j]
20d3f 2c 20 7a 52 65 70 2c 20 6e 52 65 70 29 3b 0a 20  , zRep, nRep);. 
20d40 20 20 20 20 20 6a 20 2b 3d 20 6e 52 65 70 3b 0a       j += nRep;.
20d41 20 20 20 20 20 20 69 20 2b 3d 20 6e 50 61 74 74        i += nPatt
20d42 65 72 6e 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  ern-1;.    }.  }
20d43 0a 20 20 61 73 73 65 72 74 28 20 6a 2b 6e 53 74  .  assert( j+nSt
20d44 72 2d 69 2b 31 3d 3d 6e 4f 75 74 20 29 3b 0a 20  r-i+1==nOut );. 
20d45 20 6d 65 6d 63 70 79 28 26 7a 4f 75 74 5b 6a 5d   memcpy(&zOut[j]
20d46 2c 20 26 7a 53 74 72 5b 69 5d 2c 20 6e 53 74 72  , &zStr[i], nStr
20d47 2d 69 29 3b 0a 20 20 6a 20 2b 3d 20 6e 53 74 72  -i);.  j += nStr
20d48 20 2d 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20   - i;.  assert( 
20d49 6a 3c 3d 6e 4f 75 74 20 29 3b 0a 20 20 7a 4f 75  j<=nOut );.  zOu
20d4a 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69  t[j] = 0;.  sqli
20d4b 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
20d4c 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29  context, (char*)
20d4d 7a 4f 75 74 2c 20 6a 2c 20 73 71 6c 69 74 65 33  zOut, j, sqlite3
20d4e 5f 66 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  _free);.}../*.**
20d4f 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
20d50 6f 66 20 74 68 65 20 54 52 49 4d 28 29 2c 20 4c  of the TRIM(), L
20d51 54 52 49 4d 28 29 2c 20 61 6e 64 20 52 54 52 49  TRIM(), and RTRI
20d52 4d 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a  M() functions..*
20d53 2a 20 54 68 65 20 75 73 65 72 64 61 74 61 20 69  * The userdata i
20d54 73 20 30 78 31 20 66 6f 72 20 6c 65 66 74 20 74  s 0x1 for left t
20d55 72 69 6d 2c 20 30 78 32 20 66 6f 72 20 72 69 67  rim, 0x2 for rig
20d56 68 74 20 74 72 69 6d 2c 20 30 78 33 20 66 6f 72  ht trim, 0x3 for
20d57 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63   both..*/.static
20d58 20 76 6f 69 64 20 74 72 69 6d 46 75 6e 63 28 0a   void trimFunc(.
20d59 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
20d5a 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
20d5b 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
20d5c 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
20d5d 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  {.  const unsign
20d5e 65 64 20 63 68 61 72 20 2a 7a 49 6e 3b 20 20 20  ed char *zIn;   
20d5f 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 73        /* Input s
20d60 74 72 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74  tring */.  const
20d61 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
20d62 7a 43 68 61 72 53 65 74 3b 20 20 20 20 2f 2a 20  zCharSet;    /* 
20d63 53 65 74 20 6f 66 20 63 68 61 72 61 63 74 65 72  Set of character
20d64 73 20 74 6f 20 74 72 69 6d 20 2a 2f 0a 20 20 69  s to trim */.  i
20d65 6e 74 20 6e 49 6e 3b 20 20 20 20 20 20 20 20 20  nt nIn;         
20d66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20d67 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
20d68 74 65 73 20 69 6e 20 69 6e 70 75 74 20 2a 2f 0a  tes in input */.
20d69 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20    int flags;    
20d6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20d6b 20 20 20 20 2f 2a 20 31 3a 20 74 72 69 6d 6c 65      /* 1: trimle
20d6c 66 74 20 20 32 3a 20 74 72 69 6d 72 69 67 68 74  ft  2: trimright
20d6d 20 20 33 3a 20 74 72 69 6d 20 2a 2f 0a 20 20 69    3: trim */.  i
20d6e 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
20d6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20d70 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
20d71 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   */.  unsigned c
20d72 68 61 72 20 2a 61 4c 65 6e 3b 20 20 20 20 20 20  har *aLen;      
20d73 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
20d74 68 20 6f 66 20 65 61 63 68 20 63 68 61 72 61 63  h of each charac
20d75 74 65 72 20 69 6e 20 7a 43 68 61 72 53 65 74 20  ter in zCharSet 
20d76 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
20d77 61 72 20 2a 2a 61 7a 43 68 61 72 3b 20 20 20 20  ar **azChar;    
20d78 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 69 76 69         /* Indivi
20d79 64 75 61 6c 20 63 68 61 72 61 63 74 65 72 73 20  dual characters 
20d7a 69 6e 20 7a 43 68 61 72 53 65 74 20 2a 2f 0a 20  in zCharSet */. 
20d7b 20 69 6e 74 20 6e 43 68 61 72 3b 20 20 20 20 20   int nChar;     
20d7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20d7d 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
20d7e 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a 43  characters in zC
20d7f 68 61 72 53 65 74 20 2a 2f 0a 0a 20 20 69 66 28  harSet */..  if(
20d80 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
20d81 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51  ype(argv[0])==SQ
20d82 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20  LITE_NULL ){.   
20d83 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 7a   return;.  }.  z
20d84 49 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  In = sqlite3_val
20d85 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
20d86 3b 0a 20 20 69 66 28 20 7a 49 6e 3d 3d 30 20 29  ;.  if( zIn==0 )
20d87 20 72 65 74 75 72 6e 3b 0a 20 20 6e 49 6e 20 3d   return;.  nIn =
20d88 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
20d89 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20  ytes(argv[0]);. 
20d8a 20 61 73 73 65 72 74 28 20 7a 49 6e 3d 3d 73 71   assert( zIn==sq
20d8b 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
20d8c 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 69  (argv[0]) );.  i
20d8d 66 28 20 61 72 67 63 3d 3d 31 20 29 7b 0a 20 20  f( argc==1 ){.  
20d8e 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
20d8f 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6c 65 6e  nsigned char len
20d90 4f 6e 65 5b 5d 20 3d 20 7b 20 31 20 7d 3b 0a 20  One[] = { 1 };. 
20d91 20 20 20 73 74 61 74 69 63 20 75 6e 73 69 67 6e     static unsign
20d92 65 64 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20  ed char * const 
20d93 61 7a 4f 6e 65 5b 5d 20 3d 20 7b 20 28 75 38 2a  azOne[] = { (u8*
20d94 29 22 20 22 20 7d 3b 0a 20 20 20 20 6e 43 68 61  )" " };.    nCha
20d95 72 20 3d 20 31 3b 0a 20 20 20 20 61 4c 65 6e 20  r = 1;.    aLen 
20d96 3d 20 28 75 38 2a 29 6c 65 6e 4f 6e 65 3b 0a 20  = (u8*)lenOne;. 
20d97 20 20 20 61 7a 43 68 61 72 20 3d 20 28 75 6e 73     azChar = (uns
20d98 69 67 6e 65 64 20 63 68 61 72 20 2a 2a 29 61 7a  igned char **)az
20d99 4f 6e 65 3b 0a 20 20 20 20 7a 43 68 61 72 53 65  One;.    zCharSe
20d9a 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69  t = 0;.  }else i
20d9b 66 28 20 28 7a 43 68 61 72 53 65 74 20 3d 20 73  f( (zCharSet = s
20d9c 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
20d9d 74 28 61 72 67 76 5b 31 5d 29 29 3d 3d 30 20 29  t(argv[1]))==0 )
20d9e 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
20d9f 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74  }else{.    const
20da0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
20da1 7a 3b 0a 20 20 20 20 66 6f 72 28 7a 3d 7a 43 68  z;.    for(z=zCh
20da2 61 72 53 65 74 2c 20 6e 43 68 61 72 3d 30 3b 20  arSet, nChar=0; 
20da3 2a 7a 3b 20 6e 43 68 61 72 2b 2b 29 7b 0a 20 20  *z; nChar++){.  
20da4 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f      SQLITE_SKIP_
20da5 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 7d 0a 20  UTF8(z);.    }. 
20da6 20 20 20 69 66 28 20 6e 43 68 61 72 3e 30 20 29     if( nChar>0 )
20da7 7b 0a 20 20 20 20 20 20 61 7a 43 68 61 72 20 3d  {.      azChar =
20da8 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63   contextMalloc(c
20da9 6f 6e 74 65 78 74 2c 20 28 28 69 36 34 29 6e 43  ontext, ((i64)nC
20daa 68 61 72 29 2a 28 73 69 7a 65 6f 66 28 63 68 61  har)*(sizeof(cha
20dab 72 2a 29 2b 31 29 29 3b 0a 20 20 20 20 20 20 69  r*)+1));.      i
20dac 66 28 20 61 7a 43 68 61 72 3d 3d 30 20 29 7b 0a  f( azChar==0 ){.
20dad 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a          return;.
20dae 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 4c        }.      aL
20daf 65 6e 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63  en = (unsigned c
20db0 68 61 72 2a 29 26 61 7a 43 68 61 72 5b 6e 43 68  har*)&azChar[nCh
20db1 61 72 5d 3b 0a 20 20 20 20 20 20 66 6f 72 28 7a  ar];.      for(z
20db2 3d 7a 43 68 61 72 53 65 74 2c 20 6e 43 68 61 72  =zCharSet, nChar
20db3 3d 30 3b 20 2a 7a 3b 20 6e 43 68 61 72 2b 2b 29  =0; *z; nChar++)
20db4 7b 0a 20 20 20 20 20 20 20 20 61 7a 43 68 61 72  {.        azChar
20db5 5b 6e 43 68 61 72 5d 20 3d 20 28 75 6e 73 69 67  [nChar] = (unsig
20db6 6e 65 64 20 63 68 61 72 20 2a 29 7a 3b 0a 20 20  ned char *)z;.  
20db7 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49        SQLITE_SKI
20db8 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20  P_UTF8(z);.     
20db9 20 20 20 61 4c 65 6e 5b 6e 43 68 61 72 5d 20 3d     aLen[nChar] =
20dba 20 7a 20 2d 20 61 7a 43 68 61 72 5b 6e 43 68 61   z - azChar[nCha
20dbb 72 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  r];.      }.    
20dbc 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6e 43 68 61  }.  }.  if( nCha
20dbd 72 3e 30 20 29 7b 0a 20 20 20 20 66 6c 61 67 73  r>0 ){.    flags
20dbe 20 3d 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f   = SQLITE_PTR_TO
20dbf 5f 49 4e 54 28 73 71 6c 69 74 65 33 5f 75 73 65  _INT(sqlite3_use
20dc0 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 29  r_data(context))
20dc1 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20  ;.    if( flags 
20dc2 26 20 31 20 29 7b 0a 20 20 20 20 20 20 77 68 69  & 1 ){.      whi
20dc3 6c 65 28 20 6e 49 6e 3e 30 20 29 7b 0a 20 20 20  le( nIn>0 ){.   
20dc4 20 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20       int len;.  
20dc5 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
20dc6 3c 6e 43 68 61 72 3b 20 69 2b 2b 29 7b 0a 20 20  <nChar; i++){.  
20dc7 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 61 4c          len = aL
20dc8 65 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20  en[i];.         
20dc9 20 69 66 28 20 6d 65 6d 63 6d 70 28 7a 49 6e 2c   if( memcmp(zIn,
20dca 20 61 7a 43 68 61 72 5b 69 5d 2c 20 6c 65 6e 29   azChar[i], len)
20dcb 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ==0 ) break;.   
20dcc 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
20dcd 66 28 20 69 3e 3d 6e 43 68 61 72 20 29 20 62 72  f( i>=nChar ) br
20dce 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7a 49 6e  eak;.        zIn
20dcf 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20   += len;.       
20dd0 20 6e 49 6e 20 2d 3d 20 6c 65 6e 3b 0a 20 20 20   nIn -= len;.   
20dd1 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
20dd2 66 28 20 66 6c 61 67 73 20 26 20 32 20 29 7b 0a  f( flags & 2 ){.
20dd3 20 20 20 20 20 20 77 68 69 6c 65 28 20 6e 49 6e        while( nIn
20dd4 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  >0 ){.        in
20dd5 74 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 66  t len;.        f
20dd6 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 68 61 72 3b  or(i=0; i<nChar;
20dd7 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
20dd8 20 6c 65 6e 20 3d 20 61 4c 65 6e 5b 69 5d 3b 0a   len = aLen[i];.
20dd9 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6c 65            if( le
20dda 6e 3c 3d 6e 49 6e 20 26 26 20 6d 65 6d 63 6d 70  n<=nIn && memcmp
20ddb 28 26 7a 49 6e 5b 6e 49 6e 2d 6c 65 6e 5d 2c 61  (&zIn[nIn-len],a
20ddc 7a 43 68 61 72 5b 69 5d 2c 6c 65 6e 29 3d 3d 30  zChar[i],len)==0
20ddd 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
20dde 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
20ddf 69 3e 3d 6e 43 68 61 72 20 29 20 62 72 65 61 6b  i>=nChar ) break
20de0 3b 0a 20 20 20 20 20 20 20 20 6e 49 6e 20 2d 3d  ;.        nIn -=
20de1 20 6c 65 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20   len;.      }.  
20de2 20 20 7d 0a 20 20 20 20 69 66 28 20 7a 43 68 61    }.    if( zCha
20de3 72 53 65 74 20 29 7b 0a 20 20 20 20 20 20 73 71  rSet ){.      sq
20de4 6c 69 74 65 33 5f 66 72 65 65 28 61 7a 43 68 61  lite3_free(azCha
20de5 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  r);.    }.  }.  
20de6 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
20de7 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 28 63 68  ext(context, (ch
20de8 61 72 2a 29 7a 49 6e 2c 20 6e 49 6e 2c 20 53 51  ar*)zIn, nIn, SQ
20de9 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
20dea 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  .}...#ifdef SQLI
20deb 54 45 5f 53 4f 55 4e 44 45 58 0a 2f 2a 0a 2a 2a  TE_SOUNDEX./*.**
20dec 20 43 6f 6d 70 75 74 65 20 74 68 65 20 73 6f 75   Compute the sou
20ded 6e 64 65 78 20 65 6e 63 6f 64 69 6e 67 20 6f 66  ndex encoding of
20dee 20 61 20 77 6f 72 64 2e 0a 2a 2f 0a 73 74 61 74   a word..*/.stat
20def 69 63 20 76 6f 69 64 20 73 6f 75 6e 64 65 78 46  ic void soundexF
20df0 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
20df1 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
20df2 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
20df3 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
20df4 72 67 76 0a 29 7b 0a 20 20 63 68 61 72 20 7a 52  rgv.){.  char zR
20df5 65 73 75 6c 74 5b 38 5d 3b 0a 20 20 63 6f 6e 73  esult[8];.  cons
20df6 74 20 75 38 20 2a 7a 49 6e 3b 0a 20 20 69 6e 74  t u8 *zIn;.  int
20df7 20 69 2c 20 6a 3b 0a 20 20 73 74 61 74 69 63 20   i, j;.  static 
20df8 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
20df9 68 61 72 20 69 43 6f 64 65 5b 5d 20 3d 20 7b 0a  har iCode[] = {.
20dfa 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20      0, 0, 0, 0, 
20dfb 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
20dfc 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
20dfd 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c   0,.    0, 0, 0,
20dfe 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
20dff 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
20e00 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30  , 0, 0,.    0, 0
20e01 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
20e02 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
20e03 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20  0, 0, 0, 0,.    
20e04 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
20e05 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
20e06 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a   0, 0, 0, 0, 0,.
20e07 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20      0, 0, 1, 2, 
20e08 33 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20 30  3, 0, 1, 2, 0, 0
20e09 2c 20 32 2c 20 32 2c 20 34 2c 20 35 2c 20 35 2c  , 2, 2, 4, 5, 5,
20e0a 20 30 2c 0a 20 20 20 20 31 2c 20 32 2c 20 36 2c   0,.    1, 2, 6,
20e0b 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20 30 2c 20   2, 3, 0, 1, 0, 
20e0c 32 2c 20 30 2c 20 32 2c 20 30 2c 20 30 2c 20 30  2, 0, 2, 0, 0, 0
20e0d 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30  , 0, 0,.    0, 0
20e0e 2c 20 31 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c  , 1, 2, 3, 0, 1,
20e0f 20 32 2c 20 30 2c 20 30 2c 20 32 2c 20 32 2c 20   2, 0, 0, 2, 2, 
20e10 34 2c 20 35 2c 20 35 2c 20 30 2c 0a 20 20 20 20  4, 5, 5, 0,.    
20e11 31 2c 20 32 2c 20 36 2c 20 32 2c 20 33 2c 20 30  1, 2, 6, 2, 3, 0
20e12 2c 20 31 2c 20 30 2c 20 32 2c 20 30 2c 20 32 2c  , 1, 0, 2, 0, 2,
20e13 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a   0, 0, 0, 0, 0,.
20e14 20 20 7d 3b 0a 20 20 61 73 73 65 72 74 28 20 61    };.  assert( a
20e15 72 67 63 3d 3d 31 20 29 3b 0a 20 20 7a 49 6e 20  rgc==1 );.  zIn 
20e16 3d 20 28 75 38 2a 29 73 71 6c 69 74 65 33 5f 76  = (u8*)sqlite3_v
20e17 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
20e18 5d 29 3b 0a 20 20 69 66 28 20 7a 49 6e 3d 3d 30  ]);.  if( zIn==0
20e19 20 29 20 7a 49 6e 20 3d 20 28 75 38 2a 29 22 22   ) zIn = (u8*)""
20e1a 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 7a 49 6e  ;.  for(i=0; zIn
20e1b 5b 69 5d 20 26 26 20 21 69 73 61 6c 70 68 61 28  [i] && !isalpha(
20e1c 7a 49 6e 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a  zIn[i]); i++){}.
20e1d 20 20 69 66 28 20 7a 49 6e 5b 69 5d 20 29 7b 0a    if( zIn[i] ){.
20e1e 20 20 20 20 75 38 20 70 72 65 76 63 6f 64 65 20      u8 prevcode 
20e1f 3d 20 69 43 6f 64 65 5b 7a 49 6e 5b 69 5d 26 30  = iCode[zIn[i]&0
20e20 78 37 66 5d 3b 0a 20 20 20 20 7a 52 65 73 75 6c  x7f];.    zResul
20e21 74 5b 30 5d 20 3d 20 74 6f 75 70 70 65 72 28 7a  t[0] = toupper(z
20e22 49 6e 5b 69 5d 29 3b 0a 20 20 20 20 66 6f 72 28  In[i]);.    for(
20e23 6a 3d 31 3b 20 6a 3c 34 20 26 26 20 7a 49 6e 5b  j=1; j<4 && zIn[
20e24 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  i]; i++){.      
20e25 69 6e 74 20 63 6f 64 65 20 3d 20 69 43 6f 64 65  int code = iCode
20e26 5b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a 20  [zIn[i]&0x7f];. 
20e27 20 20 20 20 20 69 66 28 20 63 6f 64 65 3e 30 20       if( code>0 
20e28 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63  ){.        if( c
20e29 6f 64 65 21 3d 70 72 65 76 63 6f 64 65 20 29 7b  ode!=prevcode ){
20e2a 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 76 63  .          prevc
20e2b 6f 64 65 20 3d 20 63 6f 64 65 3b 0a 20 20 20 20  ode = code;.    
20e2c 20 20 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b        zResult[j+
20e2d 2b 5d 20 3d 20 63 6f 64 65 20 2b 20 27 30 27 3b  +] = code + '0';
20e2e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
20e2f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
20e30 70 72 65 76 63 6f 64 65 20 3d 20 30 3b 0a 20 20  prevcode = 0;.  
20e31 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
20e32 77 68 69 6c 65 28 20 6a 3c 34 20 29 7b 0a 20 20  while( j<4 ){.  
20e33 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b 2b 5d      zResult[j++]
20e34 20 3d 20 27 30 27 3b 0a 20 20 20 20 7d 0a 20 20   = '0';.    }.  
20e35 20 20 7a 52 65 73 75 6c 74 5b 6a 5d 20 3d 20 30    zResult[j] = 0
20e36 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
20e37 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
20e38 74 2c 20 7a 52 65 73 75 6c 74 2c 20 34 2c 20 53  t, zResult, 4, S
20e39 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
20e3a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
20e3b 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
20e3c 78 74 28 63 6f 6e 74 65 78 74 2c 20 22 3f 30 30  xt(context, "?00
20e3d 30 22 2c 20 34 2c 20 53 51 4c 49 54 45 5f 53 54  0", 4, SQLITE_ST
20e3e 41 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  ATIC);.  }.}.#en
20e3f 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
20e40 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58  ITE_OMIT_LOAD_EX
20e41 54 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 41 20  TENSION./*.** A 
20e42 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 6c 6f  function that lo
20e43 61 64 73 20 61 20 73 68 61 72 65 64 2d 6c 69 62  ads a shared-lib
20e44 72 61 72 79 20 65 78 74 65 6e 73 69 6f 6e 20 74  rary extension t
20e45 68 65 6e 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c  hen returns NULL
20e46 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
20e47 20 6c 6f 61 64 45 78 74 28 73 71 6c 69 74 65 33   loadExt(sqlite3
20e48 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
20e49 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
20e4a 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
20e4b 76 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  v){.  const char
20e4c 20 2a 7a 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74   *zFile = (const
20e4d 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f   char *)sqlite3_
20e4e 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
20e4f 30 5d 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  0]);.  const cha
20e50 72 20 2a 7a 50 72 6f 63 3b 0a 20 20 73 71 6c 69  r *zProc;.  sqli
20e51 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65  te3 *db = sqlite
20e52 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
20e53 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  dle(context);.  
20e54 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20  char *zErrMsg = 
20e55 30 3b 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d  0;..  if( argc==
20e56 32 20 29 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d  2 ){.    zProc =
20e57 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
20e58 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
20e59 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 7d 65  t(argv[1]);.  }e
20e5a 6c 73 65 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d  lse{.    zProc =
20e5b 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 46   0;.  }.  if( zF
20e5c 69 6c 65 20 26 26 20 73 71 6c 69 74 65 33 5f 6c  ile && sqlite3_l
20e5d 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 64 62  oad_extension(db
20e5e 2c 20 7a 46 69 6c 65 2c 20 7a 50 72 6f 63 2c 20  , zFile, zProc, 
20e5f 26 7a 45 72 72 4d 73 67 29 20 29 7b 0a 20 20 20  &zErrMsg) ){.   
20e60 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
20e61 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a  error(context, z
20e62 45 72 72 4d 73 67 2c 20 2d 31 29 3b 0a 20 20 20  ErrMsg, -1);.   
20e63 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45   sqlite3_free(zE
20e64 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 7d 0a 23 65  rrMsg);.  }.}.#e
20e65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  ndif.../*.** An 
20e66 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
20e67 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
20e68 75 72 65 20 68 6f 6c 64 73 20 74 68 65 20 63 6f  ure holds the co
20e69 6e 74 65 78 74 20 6f 66 20 61 0a 2a 2a 20 73 75  ntext of a.** su
20e6a 6d 28 29 20 6f 72 20 61 76 67 28 29 20 61 67 67  m() or avg() agg
20e6b 72 65 67 61 74 65 20 63 6f 6d 70 75 74 61 74 69  regate computati
20e6c 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  on..*/.typedef s
20e6d 74 72 75 63 74 20 53 75 6d 43 74 78 20 53 75 6d  truct SumCtx Sum
20e6e 43 74 78 3b 0a 73 74 72 75 63 74 20 53 75 6d 43  Ctx;.struct SumC
20e6f 74 78 20 7b 0a 20 20 64 6f 75 62 6c 65 20 72 53  tx {.  double rS
20e70 75 6d 3b 20 20 20 20 20 20 2f 2a 20 46 6c 6f 61  um;      /* Floa
20e71 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 6d 20 2a  ting point sum *
20e72 2f 0a 20 20 69 36 34 20 69 53 75 6d 3b 20 20 20  /.  i64 iSum;   
20e73 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72        /* Integer
20e74 20 73 75 6d 20 2a 2f 20 20 20 0a 20 20 69 36 34   sum */   .  i64
20e75 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 2f   cnt;          /
20e76 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d  * Number of elem
20e77 65 6e 74 73 20 73 75 6d 6d 65 64 20 2a 2f 0a 20  ents summed */. 
20e78 20 75 38 20 6f 76 65 72 66 6c 6f 77 3b 20 20 20   u8 overflow;   
20e79 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e     /* True if in
20e7a 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77 20 73  teger overflow s
20e7b 65 65 6e 20 2a 2f 0a 20 20 75 38 20 61 70 70 72  een */.  u8 appr
20e7c 6f 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72  ox;        /* Tr
20e7d 75 65 20 69 66 20 6e 6f 6e 2d 69 6e 74 65 67 65  ue if non-intege
20e7e 72 20 76 61 6c 75 65 20 77 61 73 20 69 6e 70 75  r value was inpu
20e7f 74 20 74 6f 20 74 68 65 20 73 75 6d 20 2a 2f 0a  t to the sum */.
20e80 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e  };../*.** Routin
20e81 65 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75  es used to compu
20e82 74 65 20 74 68 65 20 73 75 6d 2c 20 61 76 65 72  te the sum, aver
20e83 61 67 65 2c 20 61 6e 64 20 74 6f 74 61 6c 2e 0a  age, and total..
20e84 2a 2a 0a 2a 2a 20 54 68 65 20 53 55 4d 28 29 20  **.** The SUM() 
20e85 66 75 6e 63 74 69 6f 6e 20 66 6f 6c 6c 6f 77 73  function follows
20e86 20 74 68 65 20 28 62 72 6f 6b 65 6e 29 20 53 51   the (broken) SQ
20e87 4c 20 73 74 61 6e 64 61 72 64 20 77 68 69 63 68  L standard which
20e88 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 69   means.** that i
20e89 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69  t returns NULL i
20e8a 66 20 69 74 20 73 75 6d 73 20 6f 76 65 72 20 6e  f it sums over n
20e8b 6f 20 69 6e 70 75 74 73 2e 20 20 54 4f 54 41 4c  o inputs.  TOTAL
20e8c 20 72 65 74 75 72 6e 73 0a 2a 2a 20 30 2e 30 20   returns.** 0.0 
20e8d 69 6e 20 74 68 61 74 20 63 61 73 65 2e 20 20 49  in that case.  I
20e8e 6e 20 61 64 64 69 74 69 6f 6e 2c 20 54 4f 54 41  n addition, TOTA
20e8f 4c 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73  L always returns
20e90 20 61 20 66 6c 6f 61 74 20 77 68 65 72 65 0a 2a   a float where.*
20e91 2a 20 53 55 4d 20 6d 69 67 68 74 20 72 65 74 75  * SUM might retu
20e92 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 69 66  rn an integer if
20e93 20 69 74 20 6e 65 76 65 72 20 65 6e 63 6f 75 6e   it never encoun
20e94 74 65 72 73 20 61 20 66 6c 6f 61 74 69 6e 67 20  ters a floating 
20e95 70 6f 69 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20  point.** value. 
20e96 20 54 4f 54 41 4c 20 6e 65 76 65 72 20 66 61 69   TOTAL never fai
20e97 6c 73 2c 20 62 75 74 20 53 55 4d 20 6d 69 67 68  ls, but SUM migh
20e98 74 20 74 68 72 6f 75 67 68 20 61 6e 20 65 78 63  t through an exc
20e99 65 70 74 69 6f 6e 20 69 66 0a 2a 2a 20 69 74 20  eption if.** it 
20e9a 6f 76 65 72 66 6c 6f 77 73 20 61 6e 20 69 6e 74  overflows an int
20e9b 65 67 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  eger..*/.static 
20e9c 76 6f 69 64 20 73 75 6d 53 74 65 70 28 73 71 6c  void sumStep(sql
20e9d 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
20e9e 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
20e9f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
20ea0 2a 61 72 67 76 29 7b 0a 20 20 53 75 6d 43 74 78  *argv){.  SumCtx
20ea1 20 2a 70 3b 0a 20 20 69 6e 74 20 74 79 70 65 3b   *p;.  int type;
20ea2 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
20ea3 3d 31 20 29 3b 0a 20 20 70 20 3d 20 73 71 6c 69  =1 );.  p = sqli
20ea4 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
20ea5 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73  ntext(context, s
20ea6 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20 74 79  izeof(*p));.  ty
20ea7 70 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  pe = sqlite3_val
20ea8 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28  ue_numeric_type(
20ea9 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20  argv[0]);.  if( 
20eaa 70 20 26 26 20 74 79 70 65 21 3d 53 51 4c 49 54  p && type!=SQLIT
20eab 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 70 2d  E_NULL ){.    p-
20eac 3e 63 6e 74 2b 2b 3b 0a 20 20 20 20 69 66 28 20  >cnt++;.    if( 
20ead 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49 4e 54  type==SQLITE_INT
20eae 45 47 45 52 20 29 7b 0a 20 20 20 20 20 20 69 36  EGER ){.      i6
20eaf 34 20 76 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  4 v = sqlite3_va
20eb0 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30  lue_int64(argv[0
20eb1 5d 29 3b 0a 20 20 20 20 20 20 70 2d 3e 72 53 75  ]);.      p->rSu
20eb2 6d 20 2b 3d 20 76 3b 0a 20 20 20 20 20 20 69 66  m += v;.      if
20eb3 28 20 28 70 2d 3e 61 70 70 72 6f 78 7c 70 2d 3e  ( (p->approx|p->
20eb4 6f 76 65 72 66 6c 6f 77 29 3d 3d 30 20 29 7b 0a  overflow)==0 ){.
20eb5 20 20 20 20 20 20 20 20 69 36 34 20 69 4e 65 77          i64 iNew
20eb6 53 75 6d 20 3d 20 70 2d 3e 69 53 75 6d 20 2b 20  Sum = p->iSum + 
20eb7 76 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 73  v;.        int s
20eb8 31 20 3d 20 70 2d 3e 69 53 75 6d 20 3e 3e 20 28  1 = p->iSum >> (
20eb9 73 69 7a 65 6f 66 28 69 36 34 29 2a 38 2d 31 29  sizeof(i64)*8-1)
20eba 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 73 32  ;.        int s2
20ebb 20 3d 20 76 20 20 20 20 20 20 20 3e 3e 20 28 73   = v       >> (s
20ebc 69 7a 65 6f 66 28 69 36 34 29 2a 38 2d 31 29 3b  izeof(i64)*8-1);
20ebd 0a 20 20 20 20 20 20 20 20 69 6e 74 20 73 33 20  .        int s3 
20ebe 3d 20 69 4e 65 77 53 75 6d 20 3e 3e 20 28 73 69  = iNewSum >> (si
20ebf 7a 65 6f 66 28 69 36 34 29 2a 38 2d 31 29 3b 0a  zeof(i64)*8-1);.
20ec0 20 20 20 20 20 20 20 20 70 2d 3e 6f 76 65 72 66          p->overf
20ec1 6c 6f 77 20 3d 20 28 73 31 26 73 32 26 7e 73 33  low = (s1&s2&~s3
20ec2 29 20 7c 20 28 7e 73 31 26 7e 73 32 26 73 33 29  ) | (~s1&~s2&s3)
20ec3 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 53 75  ;.        p->iSu
20ec4 6d 20 3d 20 69 4e 65 77 53 75 6d 3b 0a 20 20 20  m = iNewSum;.   
20ec5 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
20ec6 20 20 20 20 20 20 70 2d 3e 72 53 75 6d 20 2b 3d        p->rSum +=
20ec7 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64   sqlite3_value_d
20ec8 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 3b 0a  ouble(argv[0]);.
20ec9 20 20 20 20 20 20 70 2d 3e 61 70 70 72 6f 78 20        p->approx 
20eca 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  = 1;.    }.  }.}
20ecb 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 75 6d  .static void sum
20ecc 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33  Finalize(sqlite3
20ecd 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
20ece 74 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b  t){.  SumCtx *p;
20ecf 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61  .  p = sqlite3_a
20ed0 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74  ggregate_context
20ed1 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20  (context, 0);.  
20ed2 69 66 28 20 70 20 26 26 20 70 2d 3e 63 6e 74 3e  if( p && p->cnt>
20ed3 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  0 ){.    if( p->
20ed4 6f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20  overflow ){.    
20ed5 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
20ed6 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 22  _error(context,"
20ed7 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77  integer overflow
20ed8 22 2c 2d 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65  ",-1);.    }else
20ed9 20 69 66 28 20 70 2d 3e 61 70 70 72 6f 78 20 29   if( p->approx )
20eda 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
20edb 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f  result_double(co
20edc 6e 74 65 78 74 2c 20 70 2d 3e 72 53 75 6d 29 3b  ntext, p->rSum);
20edd 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
20ede 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
20edf 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c 20  _int64(context, 
20ee0 70 2d 3e 69 53 75 6d 29 3b 0a 20 20 20 20 7d 0a  p->iSum);.    }.
20ee1 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69    }.}.static voi
20ee2 64 20 61 76 67 46 69 6e 61 6c 69 7a 65 28 73 71  d avgFinalize(sq
20ee3 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
20ee4 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75 6d 43 74  ontext){.  SumCt
20ee5 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69  x *p;.  p = sqli
20ee6 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
20ee7 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30  ntext(context, 0
20ee8 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d  );.  if( p && p-
20ee9 3e 63 6e 74 3e 30 20 29 7b 0a 20 20 20 20 73 71  >cnt>0 ){.    sq
20eea 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75  lite3_result_dou
20eeb 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70 2d 3e  ble(context, p->
20eec 72 53 75 6d 2f 28 64 6f 75 62 6c 65 29 70 2d 3e  rSum/(double)p->
20eed 63 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74  cnt);.  }.}.stat
20eee 69 63 20 76 6f 69 64 20 74 6f 74 61 6c 46 69 6e  ic void totalFin
20eef 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f  alize(sqlite3_co
20ef0 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b  ntext *context){
20ef1 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20 20  .  SumCtx *p;.  
20ef2 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72  p = sqlite3_aggr
20ef3 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f  egate_context(co
20ef4 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 73 71 6c  ntext, 0);.  sql
20ef5 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
20ef6 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70 20 3f 20  le(context, p ? 
20ef7 70 2d 3e 72 53 75 6d 20 3a 20 30 2e 30 29 3b 0a  p->rSum : 0.0);.
20ef8 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  }../*.** The fol
20ef9 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
20efa 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66 20   keeps track of 
20efb 73 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f  state informatio
20efc 6e 20 66 6f 72 20 74 68 65 0a 2a 2a 20 63 6f 75  n for the.** cou
20efd 6e 74 28 29 20 61 67 67 72 65 67 61 74 65 20 66  nt() aggregate f
20efe 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65  unction..*/.type
20eff 64 65 66 20 73 74 72 75 63 74 20 43 6f 75 6e 74  def struct Count
20f00 43 74 78 20 43 6f 75 6e 74 43 74 78 3b 0a 73 74  Ctx CountCtx;.st
20f01 72 75 63 74 20 43 6f 75 6e 74 43 74 78 20 7b 0a  ruct CountCtx {.
20f02 20 20 69 36 34 20 6e 3b 0a 7d 3b 0a 0a 2f 2a 0a    i64 n;.};../*.
20f03 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f 20 69  ** Routines to i
20f04 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 63 6f 75  mplement the cou
20f05 6e 74 28 29 20 61 67 67 72 65 67 61 74 65 20 66  nt() aggregate f
20f06 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  unction..*/.stat
20f07 69 63 20 76 6f 69 64 20 63 6f 75 6e 74 53 74 65  ic void countSte
20f08 70 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  p(sqlite3_contex
20f09 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20  t *context, int 
20f0a 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
20f0b 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 43  lue **argv){.  C
20f0c 6f 75 6e 74 43 74 78 20 2a 70 3b 0a 20 20 70 20  ountCtx *p;.  p 
20f0d 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67  = sqlite3_aggreg
20f0e 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74  ate_context(cont
20f0f 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29  ext, sizeof(*p))
20f10 3b 0a 20 20 69 66 28 20 28 61 72 67 63 3d 3d 30  ;.  if( (argc==0
20f11 20 7c 7c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 21   || SQLITE_NULL!
20f12 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  =sqlite3_value_t
20f13 79 70 65 28 61 72 67 76 5b 30 5d 29 29 20 26 26  ype(argv[0])) &&
20f14 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 2b 2b   p ){.    p->n++
20f15 3b 0a 20 20 7d 0a 7d 20 20 20 0a 73 74 61 74 69  ;.  }.}   .stati
20f16 63 20 76 6f 69 64 20 63 6f 75 6e 74 46 69 6e 61  c void countFina
20f17 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e  lize(sqlite3_con
20f18 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a  text *context){.
20f19 20 20 43 6f 75 6e 74 43 74 78 20 2a 70 3b 0a 20    CountCtx *p;. 
20f1a 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67   p = sqlite3_agg
20f1b 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63  regate_context(c
20f1c 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 73 71  ontext, 0);.  sq
20f1d 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
20f1e 36 34 28 63 6f 6e 74 65 78 74 2c 20 70 20 3f 20  64(context, p ? 
20f1f 70 2d 3e 6e 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a  p->n : 0);.}../*
20f20 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f 20  .** Routines to 
20f21 69 6d 70 6c 65 6d 65 6e 74 20 6d 69 6e 28 29 20  implement min() 
20f22 61 6e 64 20 6d 61 78 28 29 20 61 67 67 72 65 67  and max() aggreg
20f23 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a  ate functions..*
20f24 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 69  /.static void mi
20f25 6e 6d 61 78 53 74 65 70 28 73 71 6c 69 74 65 33  nmaxStep(sqlite3
20f26 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
20f27 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
20f28 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
20f29 76 29 7b 0a 20 20 4d 65 6d 20 2a 70 41 72 67 20  v){.  Mem *pArg 
20f2a 20 3d 20 28 4d 65 6d 20 2a 29 61 72 67 76 5b 30   = (Mem *)argv[0
20f2b 5d 3b 0a 20 20 4d 65 6d 20 2a 70 42 65 73 74 3b  ];.  Mem *pBest;
20f2c 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  ..  if( sqlite3_
20f2d 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
20f2e 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  0])==SQLITE_NULL
20f2f 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 42 65   ) return;.  pBe
20f30 73 74 20 3d 20 28 4d 65 6d 20 2a 29 73 71 6c 69  st = (Mem *)sqli
20f31 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
20f32 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73  ntext(context, s
20f33 69 7a 65 6f 66 28 2a 70 42 65 73 74 29 29 3b 0a  izeof(*pBest));.
20f34 20 20 69 66 28 20 21 70 42 65 73 74 20 29 20 72    if( !pBest ) r
20f35 65 74 75 72 6e 3b 0a 0a 20 20 69 66 28 20 70 42  eturn;..  if( pB
20f36 65 73 74 2d 3e 66 6c 61 67 73 20 29 7b 0a 20 20  est->flags ){.  
20f37 20 20 69 6e 74 20 6d 61 78 3b 0a 20 20 20 20 69    int max;.    i
20f38 6e 74 20 63 6d 70 3b 0a 20 20 20 20 43 6f 6c 6c  nt cmp;.    Coll
20f39 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 73 71 6c  Seq *pColl = sql
20f3a 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53  ite3GetFuncCollS
20f3b 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  eq(context);.   
20f3c 20 2f 2a 20 54 68 69 73 20 73 74 65 70 20 66 75   /* This step fu
20f3d 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 66  nction is used f
20f3e 6f 72 20 62 6f 74 68 20 74 68 65 20 6d 69 6e 28  or both the min(
20f3f 29 20 61 6e 64 20 6d 61 78 28 29 20 61 67 67 72  ) and max() aggr
20f40 65 67 61 74 65 73 2c 0a 20 20 20 20 2a 2a 20 74  egates,.    ** t
20f41 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e  he only differen
20f42 63 65 20 62 65 74 77 65 65 6e 20 74 68 65 20 74  ce between the t
20f43 77 6f 20 62 65 69 6e 67 20 74 68 61 74 20 74 68  wo being that th
20f44 65 20 73 65 6e 73 65 20 6f 66 20 74 68 65 0a 20  e sense of the. 
20f45 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e     ** comparison
20f46 20 69 73 20 69 6e 76 65 72 74 65 64 2e 20 46 6f   is inverted. Fo
20f47 72 20 74 68 65 20 6d 61 78 28 29 20 61 67 67 72  r the max() aggr
20f48 65 67 61 74 65 2c 20 74 68 65 0a 20 20 20 20 2a  egate, the.    *
20f49 2a 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64  * sqlite3_user_d
20f4a 61 74 61 28 29 20 66 75 6e 63 74 69 6f 6e 20 72  ata() function r
20f4b 65 74 75 72 6e 73 20 28 76 6f 69 64 20 2a 29 2d  eturns (void *)-
20f4c 31 2e 20 46 6f 72 20 6d 69 6e 28 29 20 69 74 0a  1. For min() it.
20f4d 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 28      ** returns (
20f4e 76 6f 69 64 20 2a 29 64 62 2c 20 77 68 65 72 65  void *)db, where
20f4f 20 64 62 20 69 73 20 74 68 65 20 73 71 6c 69 74   db is the sqlit
20f50 65 33 2a 20 64 61 74 61 62 61 73 65 20 70 6f 69  e3* database poi
20f51 6e 74 65 72 2e 0a 20 20 20 20 2a 2a 20 54 68 65  nter..    ** The
20f52 72 65 66 6f 72 65 20 74 68 65 20 6e 65 78 74 20  refore the next 
20f53 73 74 61 74 65 6d 65 6e 74 20 73 65 74 73 20 76  statement sets v
20f54 61 72 69 61 62 6c 65 20 27 6d 61 78 27 20 74 6f  ariable 'max' to
20f55 20 31 20 66 6f 72 20 74 68 65 20 6d 61 78 28 29   1 for the max()
20f56 0a 20 20 20 20 2a 2a 20 61 67 67 72 65 67 61 74  .    ** aggregat
20f57 65 2c 20 6f 72 20 30 20 66 6f 72 20 6d 69 6e 28  e, or 0 for min(
20f58 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6d 61  )..    */.    ma
20f59 78 20 3d 20 73 71 6c 69 74 65 33 5f 75 73 65 72  x = sqlite3_user
20f5a 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 21 3d  _data(context)!=
20f5b 30 3b 0a 20 20 20 20 63 6d 70 20 3d 20 73 71 6c  0;.    cmp = sql
20f5c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 70  ite3MemCompare(p
20f5d 42 65 73 74 2c 20 70 41 72 67 2c 20 70 43 6f 6c  Best, pArg, pCol
20f5e 6c 29 3b 0a 20 20 20 20 69 66 28 20 28 6d 61 78  l);.    if( (max
20f5f 20 26 26 20 63 6d 70 3c 30 29 20 7c 7c 20 28 21   && cmp<0) || (!
20f60 6d 61 78 20 26 26 20 63 6d 70 3e 30 29 20 29 7b  max && cmp>0) ){
20f61 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
20f62 62 65 4d 65 6d 43 6f 70 79 28 70 42 65 73 74 2c  beMemCopy(pBest,
20f63 20 70 41 72 67 29 3b 0a 20 20 20 20 7d 0a 20 20   pArg);.    }.  
20f64 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
20f65 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 70 42  e3VdbeMemCopy(pB
20f66 65 73 74 2c 20 70 41 72 67 29 3b 0a 20 20 7d 0a  est, pArg);.  }.
20f67 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 69  }.static void mi
20f68 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 28 73 71 6c  nMaxFinalize(sql
20f69 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
20f6a 6e 74 65 78 74 29 7b 0a 20 20 73 71 6c 69 74 65  ntext){.  sqlite
20f6b 33 5f 76 61 6c 75 65 20 2a 70 52 65 73 3b 0a 20  3_value *pRes;. 
20f6c 20 70 52 65 73 20 3d 20 28 73 71 6c 69 74 65 33   pRes = (sqlite3
20f6d 5f 76 61 6c 75 65 20 2a 29 73 71 6c 69 74 65 33  _value *)sqlite3
20f6e 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
20f6f 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a  xt(context, 0);.
20f70 20 20 69 66 28 20 70 52 65 73 20 29 7b 0a 20 20    if( pRes ){.  
20f71 20 20 69 66 28 20 70 52 65 73 2d 3e 66 6c 61 67    if( pRes->flag
20f72 73 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  s ){.      sqlit
20f73 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28  e3_result_value(
20f74 63 6f 6e 74 65 78 74 2c 20 70 52 65 73 29 3b 0a  context, pRes);.
20f75 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
20f76 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
20f77 70 52 65 73 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  pRes);.  }.}../*
20f78 0a 2a 2a 20 67 72 6f 75 70 5f 63 6f 6e 63 61 74  .** group_concat
20f79 28 45 58 50 52 2c 20 3f 53 45 50 41 52 41 54 4f  (EXPR, ?SEPARATO
20f7a 52 3f 29 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  R?).*/.static vo
20f7b 69 64 20 67 72 6f 75 70 43 6f 6e 63 61 74 53 74  id groupConcatSt
20f7c 65 70 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  ep(.  sqlite3_co
20f7d 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
20f7e 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
20f7f 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
20f80 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68  gv.){.  const ch
20f81 61 72 20 2a 7a 56 61 6c 3b 0a 20 20 53 74 72 41  ar *zVal;.  StrA
20f82 63 63 75 6d 20 2a 70 41 63 63 75 6d 3b 0a 20 20  ccum *pAccum;.  
20f83 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 65 70  const char *zSep
20f84 3b 0a 20 20 69 6e 74 20 6e 56 61 6c 2c 20 6e 53  ;.  int nVal, nS
20f85 65 70 2c 20 69 3b 0a 20 20 69 66 28 20 61 72 67  ep, i;.  if( arg
20f86 63 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  c==0 || sqlite3_
20f87 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
20f88 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  0])==SQLITE_NULL
20f89 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 41 63   ) return;.  pAc
20f8a 63 75 6d 20 3d 20 28 53 74 72 41 63 63 75 6d 2a  cum = (StrAccum*
20f8b 29 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61  )sqlite3_aggrega
20f8c 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65  te_context(conte
20f8d 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 41 63 63  xt, sizeof(*pAcc
20f8e 75 6d 29 29 3b 0a 0a 20 20 69 66 28 20 70 41 63  um));..  if( pAc
20f8f 63 75 6d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  cum ){.    sqlit
20f90 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
20f91 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
20f92 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  le(context);.   
20f93 20 70 41 63 63 75 6d 2d 3e 75 73 65 4d 61 6c 6c   pAccum->useMall
20f94 6f 63 20 3d 20 31 3b 0a 20 20 20 20 70 41 63 63  oc = 1;.    pAcc
20f95 75 6d 2d 3e 6d 78 41 6c 6c 6f 63 20 3d 20 64 62  um->mxAlloc = db
20f96 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
20f97 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20  LIMIT_LENGTH];. 
20f98 20 20 20 69 66 28 20 70 41 63 63 75 6d 2d 3e 6e     if( pAccum->n
20f99 43 68 61 72 20 29 7b 0a 20 20 20 20 20 20 69 66  Char ){.      if
20f9a 28 20 61 72 67 63 3e 31 20 29 7b 0a 20 20 20 20  ( argc>1 ){.    
20f9b 20 20 20 20 7a 53 65 70 20 3d 20 28 63 68 61 72      zSep = (char
20f9c 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
20f9d 74 65 78 74 28 61 72 67 76 5b 61 72 67 63 2d 31  text(argv[argc-1
20f9e 5d 29 3b 0a 20 20 20 20 20 20 20 20 6e 53 65 70  ]);.        nSep
20f9f 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
20fa0 5f 62 79 74 65 73 28 61 72 67 76 5b 61 72 67 63  _bytes(argv[argc
20fa1 2d 31 5d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  -1]);.      }els
20fa2 65 7b 0a 20 20 20 20 20 20 20 20 7a 53 65 70 20  e{.        zSep 
20fa3 3d 20 22 2c 22 3b 0a 20 20 20 20 20 20 20 20 6e  = ",";.        n
20fa4 53 65 70 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  Sep = 1;.      }
20fa5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74  .      sqlite3St
20fa6 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63  rAccumAppend(pAc
20fa7 63 75 6d 2c 20 7a 53 65 70 2c 20 6e 53 65 70 29  cum, zSep, nSep)
20fa8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 20  ;.    }.    i = 
20fa9 30 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20  0;.    do{.     
20faa 20 7a 56 61 6c 20 3d 20 28 63 68 61 72 2a 29 73   zVal = (char*)s
20fab 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
20fac 74 28 61 72 67 76 5b 69 5d 29 3b 0a 20 20 20 20  t(argv[i]);.    
20fad 20 20 6e 56 61 6c 20 3d 20 73 71 6c 69 74 65 33    nVal = sqlite3
20fae 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
20faf 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 73 71 6c  v[i]);.      sql
20fb0 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65  ite3StrAccumAppe
20fb1 6e 64 28 70 41 63 63 75 6d 2c 20 7a 56 61 6c 2c  nd(pAccum, zVal,
20fb2 20 6e 56 61 6c 29 3b 0a 20 20 20 20 20 20 69 2b   nVal);.      i+
20fb3 2b 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 69  +;.    }while( i
20fb4 3c 61 72 67 63 2d 31 20 29 3b 0a 20 20 7d 0a 7d  <argc-1 );.  }.}
20fb5 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67 72 6f  .static void gro
20fb6 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c 69 7a 65  upConcatFinalize
20fb7 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
20fb8 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 74   *context){.  St
20fb9 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 3b 0a  rAccum *pAccum;.
20fba 20 20 70 41 63 63 75 6d 20 3d 20 73 71 6c 69 74    pAccum = sqlit
20fbb 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
20fbc 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29  text(context, 0)
20fbd 3b 0a 20 20 69 66 28 20 70 41 63 63 75 6d 20 29  ;.  if( pAccum )
20fbe 7b 0a 20 20 20 20 69 66 28 20 70 41 63 63 75 6d  {.    if( pAccum
20fbf 2d 3e 74 6f 6f 42 69 67 20 29 7b 0a 20 20 20 20  ->tooBig ){.    
20fc0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
20fc1 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f  _error_toobig(co
20fc2 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d 65 6c 73  ntext);.    }els
20fc3 65 20 69 66 28 20 70 41 63 63 75 6d 2d 3e 6d 61  e if( pAccum->ma
20fc4 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
20fc5 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
20fc6 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63  lt_error_nomem(c
20fc7 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d 65 6c  ontext);.    }el
20fc8 73 65 7b 20 20 20 20 0a 20 20 20 20 20 20 73 71  se{    .      sq
20fc9 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
20fca 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74  t(context, sqlit
20fcb 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68  e3StrAccumFinish
20fcc 28 70 41 63 63 75 6d 29 2c 20 2d 31 2c 20 0a 20  (pAccum), -1, . 
20fcd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20fce 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
20fcf 5f 66 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20  _free);.    }.  
20fd0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
20fd1 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65  function registe
20fd2 72 65 64 20 61 6c 6c 20 6f 66 20 74 68 65 20 61  red all of the a
20fd3 62 6f 76 65 20 43 20 66 75 6e 63 74 69 6f 6e 73  bove C functions
20fd4 20 61 73 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74   as SQL.** funct
20fd5 69 6f 6e 73 2e 20 20 54 68 69 73 20 73 68 6f 75  ions.  This shou
20fd6 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c 79 20 72  ld be the only r
20fd7 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66  outine in this f
20fd8 69 6c 65 20 77 69 74 68 0a 2a 2a 20 65 78 74 65  ile with.** exte
20fd9 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2f  rnal linkage..*/
20fda 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20fdb 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69  void sqlite3Regi
20fdc 73 74 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74  sterBuiltinFunct
20fdd 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62  ions(sqlite3 *db
20fde 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ){.#ifndef SQLIT
20fdf 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c  E_OMIT_ALTERTABL
20fe0 45 0a 20 20 73 71 6c 69 74 65 33 41 6c 74 65 72  E.  sqlite3Alter
20fe1 46 75 6e 63 74 69 6f 6e 73 28 64 62 29 3b 0a 23  Functions(db);.#
20fe2 65 6e 64 69 66 0a 20 20 69 66 28 20 21 64 62 2d  endif.  if( !db-
20fe3 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
20fe4 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71  .    int rc = sq
20fe5 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66  lite3_overload_f
20fe6 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 4d 41 54  unction(db, "MAT
20fe7 43 48 22 2c 20 32 29 3b 0a 20 20 20 20 61 73 73  CH", 2);.    ass
20fe8 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
20fe9 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c  NOMEM || rc==SQL
20fea 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69 66  ITE_OK );.    if
20feb 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
20fec 45 4d 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e  EM ){.      db->
20fed 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
20fee 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66 64  ;.    }.  }.#ifd
20fef 65 66 20 53 51 4c 49 54 45 5f 53 53 45 0a 20 20  ef SQLITE_SSE.  
20ff0 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 73 65  (void)sqlite3Sse
20ff1 46 75 6e 63 74 69 6f 6e 73 28 64 62 29 3b 0a 23  Functions(db);.#
20ff2 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  endif.}../*.** S
20ff3 65 74 20 74 68 65 20 4c 49 4b 45 4f 50 54 20 66  et the LIKEOPT f
20ff4 6c 61 67 20 6f 6e 20 74 68 65 20 32 2d 61 72 67  lag on the 2-arg
20ff5 75 6d 65 6e 74 20 66 75 6e 63 74 69 6f 6e 20 77  ument function w
20ff6 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 6e 61  ith the given na
20ff7 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  me..*/.static vo
20ff8 69 64 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c 61  id setLikeOptFla
20ff9 67 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  g(sqlite3 *db, c
20ffa 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
20ffb 2c 20 69 6e 74 20 66 6c 61 67 56 61 6c 29 7b 0a  , int flagVal){.
20ffc 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b    FuncDef *pDef;
20ffd 0a 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65  .  pDef = sqlite
20ffe 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62  3FindFunction(db
20fff 2c 20 7a 4e 61 6d 65 2c 20 73 74 72 6c 65 6e 28  , zName, strlen(
21000 7a 4e 61 6d 65 29 2c 20 32 2c 20 53 51 4c 49 54  zName), 2, SQLIT
21001 45 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20 69 66  E_UTF8, 0);.  if
21002 28 20 70 44 65 66 20 29 7b 0a 20 20 20 20 70 44  ( pDef ){.    pD
21003 65 66 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67  ef->flags = flag
21004 56 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  Val;.  }.}../*.*
21005 2a 20 52 65 67 69 73 74 65 72 20 74 68 65 20 62  * Register the b
21006 75 69 6c 74 2d 69 6e 20 4c 49 4b 45 20 61 6e 64  uilt-in LIKE and
21007 20 47 4c 4f 42 20 66 75 6e 63 74 69 6f 6e 73 2e   GLOB functions.
21008 20 20 54 68 65 20 63 61 73 65 53 65 6e 73 69 74    The caseSensit
21009 69 76 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  ive.** parameter
2100a 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74   determines whet
2100b 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 4c  her or not the L
2100c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 69 73 20  IKE operator is 
2100d 63 61 73 65 0a 2a 2a 20 73 65 6e 73 69 74 69 76  case.** sensitiv
2100e 65 2e 20 20 47 4c 4f 42 20 69 73 20 61 6c 77 61  e.  GLOB is alwa
2100f 79 73 20 63 61 73 65 20 73 65 6e 73 69 74 69 76  ys case sensitiv
21010 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
21011 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
21012 33 52 65 67 69 73 74 65 72 4c 69 6b 65 46 75 6e  3RegisterLikeFun
21013 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a  ctions(sqlite3 *
21014 64 62 2c 20 69 6e 74 20 63 61 73 65 53 65 6e 73  db, int caseSens
21015 69 74 69 76 65 29 7b 0a 20 20 73 74 72 75 63 74  itive){.  struct
21016 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70 49   compareInfo *pI
21017 6e 66 6f 3b 0a 20 20 69 66 28 20 63 61 73 65 53  nfo;.  if( caseS
21018 65 6e 73 69 74 69 76 65 20 29 7b 0a 20 20 20 20  ensitive ){.    
21019 70 49 6e 66 6f 20 3d 20 28 73 74 72 75 63 74 20  pInfo = (struct 
2101a 63 6f 6d 70 61 72 65 49 6e 66 6f 2a 29 26 6c 69  compareInfo*)&li
2101b 6b 65 49 6e 66 6f 41 6c 74 3b 0a 20 20 7d 65 6c  keInfoAlt;.  }el
2101c 73 65 7b 0a 20 20 20 20 70 49 6e 66 6f 20 3d 20  se{.    pInfo = 
2101d 28 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49  (struct compareI
2101e 6e 66 6f 2a 29 26 6c 69 6b 65 49 6e 66 6f 4e 6f  nfo*)&likeInfoNo
2101f 72 6d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  rm;.  }.  sqlite
21020 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20  3CreateFunc(db, 
21021 22 6c 69 6b 65 22 2c 20 32 2c 20 53 51 4c 49 54  "like", 2, SQLIT
21022 45 5f 55 54 46 38 2c 20 70 49 6e 66 6f 2c 20 6c  E_UTF8, pInfo, l
21023 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a  ikeFunc, 0, 0);.
21024 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46    sqlite3CreateF
21025 75 6e 63 28 64 62 2c 20 22 6c 69 6b 65 22 2c 20  unc(db, "like", 
21026 33 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  3, SQLITE_UTF8, 
21027 70 49 6e 66 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c  pInfo, likeFunc,
21028 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65   0, 0);.  sqlite
21029 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20  3CreateFunc(db, 
2102a 22 67 6c 6f 62 22 2c 20 32 2c 20 53 51 4c 49 54  "glob", 2, SQLIT
2102b 45 5f 55 54 46 38 2c 20 0a 20 20 20 20 20 20 28  E_UTF8, .      (
2102c 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e  struct compareIn
2102d 66 6f 2a 29 26 67 6c 6f 62 49 6e 66 6f 2c 20 6c  fo*)&globInfo, l
2102e 69 6b 65 46 75 6e 63 2c 20 30 2c 30 29 3b 0a 20  ikeFunc, 0,0);. 
2102f 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c 61 67 28   setLikeOptFlag(
21030 64 62 2c 20 22 67 6c 6f 62 22 2c 20 53 51 4c 49  db, "glob", SQLI
21031 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 7c 20 53  TE_FUNC_LIKE | S
21032 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29  QLITE_FUNC_CASE)
21033 3b 0a 20 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c  ;.  setLikeOptFl
21034 61 67 28 64 62 2c 20 22 6c 69 6b 65 22 2c 20 0a  ag(db, "like", .
21035 20 20 20 20 20 20 63 61 73 65 53 65 6e 73 69 74        caseSensit
21036 69 76 65 20 3f 20 28 53 51 4c 49 54 45 5f 46 55  ive ? (SQLITE_FU
21037 4e 43 5f 4c 49 4b 45 20 7c 20 53 51 4c 49 54 45  NC_LIKE | SQLITE
21038 5f 46 55 4e 43 5f 43 41 53 45 29 20 3a 20 53 51  _FUNC_CASE) : SQ
21039 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29 3b  LITE_FUNC_LIKE);
2103a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20  .}../*.** pExpr 
2103b 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65 78 70  points to an exp
2103c 72 65 73 73 69 6f 6e 20 77 68 69 63 68 20 69 6d  ression which im
2103d 70 6c 65 6d 65 6e 74 73 20 61 20 66 75 6e 63 74  plements a funct
2103e 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 69 74 20 69  ion.  If.** it i
2103f 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 74 6f  s appropriate to
21040 20 61 70 70 6c 79 20 74 68 65 20 4c 49 4b 45 20   apply the LIKE 
21041 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 74 6f 20  optimization to 
21042 74 68 61 74 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  that function.**
21043 20 74 68 65 6e 20 73 65 74 20 61 57 63 5b 30 5d   then set aWc[0]
21044 20 74 68 72 6f 75 67 68 20 61 57 63 5b 32 5d 20   through aWc[2] 
21045 74 6f 20 74 68 65 20 77 69 6c 64 63 61 72 64 20  to the wildcard 
21046 63 68 61 72 61 63 74 65 72 73 20 61 6e 64 0a 2a  characters and.*
21047 2a 20 72 65 74 75 72 6e 20 54 52 55 45 2e 20 20  * return TRUE.  
21048 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  If the function 
21049 69 73 20 6e 6f 74 20 61 20 4c 49 4b 45 2d 73 74  is not a LIKE-st
2104a 79 6c 65 20 66 75 6e 63 74 69 6f 6e 20 74 68 65  yle function the
2104b 6e 0a 2a 2a 20 72 65 74 75 72 6e 20 46 41 4c 53  n.** return FALS
2104c 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  E..*/.SQLITE_PRI
2104d 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
2104e 49 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 28 73  IsLikeFunction(s
2104f 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72  qlite3 *db, Expr
21050 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 2a 70 49   *pExpr, int *pI
21051 73 4e 6f 63 61 73 65 2c 20 63 68 61 72 20 2a 61  sNocase, char *a
21052 57 63 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a  Wc){.  FuncDef *
21053 70 44 65 66 3b 0a 20 20 69 66 28 20 70 45 78 70  pDef;.  if( pExp
21054 72 2d 3e 6f 70 21 3d 54 4b 5f 46 55 4e 43 54 49  r->op!=TK_FUNCTI
21055 4f 4e 20 7c 7c 20 21 70 45 78 70 72 2d 3e 70 4c  ON || !pExpr->pL
21056 69 73 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ist ){.    retur
21057 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  n 0;.  }.  if( p
21058 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78  Expr->pList->nEx
21059 70 72 21 3d 32 20 29 7b 0a 20 20 20 20 72 65 74  pr!=2 ){.    ret
2105a 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 44 65  urn 0;.  }.  pDe
2105b 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46  f = sqlite3FindF
2105c 75 6e 63 74 69 6f 6e 28 64 62 2c 20 28 63 68 61  unction(db, (cha
2105d 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e  r*)pExpr->token.
2105e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e  z, pExpr->token.
2105f 6e 2c 20 32 2c 0a 20 20 20 20 20 20 20 20 20 20  n, 2,.          
21060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21061 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20     SQLITE_UTF8, 
21062 30 29 3b 0a 20 20 69 66 28 20 70 44 65 66 3d 3d  0);.  if( pDef==
21063 30 20 7c 7c 20 28 70 44 65 66 2d 3e 66 6c 61 67  0 || (pDef->flag
21064 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  s & SQLITE_FUNC_
21065 4c 49 4b 45 29 3d 3d 30 20 29 7b 0a 20 20 20 20  LIKE)==0 ){.    
21066 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20  return 0;.  }.. 
21067 20 2f 2a 20 54 68 65 20 6d 65 6d 63 70 79 28 29   /* The memcpy()
21068 20 73 74 61 74 65 6d 65 6e 74 20 61 73 73 75 6d   statement assum
21069 65 73 20 74 68 61 74 20 74 68 65 20 77 69 6c 64  es that the wild
2106a 63 61 72 64 20 63 68 61 72 61 63 74 65 72 73 20  card characters 
2106b 61 72 65 0a 20 20 2a 2a 20 74 68 65 20 66 69 72  are.  ** the fir
2106c 73 74 20 74 68 72 65 65 20 73 74 61 74 65 6d 65  st three stateme
2106d 6e 74 73 20 69 6e 20 74 68 65 20 63 6f 6d 70 61  nts in the compa
2106e 72 65 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  reInfo structure
2106f 2e 20 20 54 68 65 0a 20 20 2a 2a 20 61 73 73 65  .  The.  ** asse
21070 72 74 73 28 29 20 74 68 61 74 20 66 6f 6c 6c 6f  rts() that follo
21071 77 20 76 65 72 69 66 79 20 74 68 61 74 20 61 73  w verify that as
21072 73 75 6d 70 74 69 6f 6e 0a 20 20 2a 2f 0a 20 20  sumption.  */.  
21073 6d 65 6d 63 70 79 28 61 57 63 2c 20 70 44 65 66  memcpy(aWc, pDef
21074 2d 3e 70 55 73 65 72 44 61 74 61 2c 20 33 29 3b  ->pUserData, 3);
21075 0a 20 20 61 73 73 65 72 74 28 20 28 63 68 61 72  .  assert( (char
21076 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 20 3d  *)&likeInfoAlt =
21077 3d 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e  = (char*)&likeIn
21078 66 6f 41 6c 74 2e 6d 61 74 63 68 41 6c 6c 20 29  foAlt.matchAll )
21079 3b 0a 20 20 61 73 73 65 72 74 28 20 26 28 28 63  ;.  assert( &((c
2107a 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c  har*)&likeInfoAl
2107b 74 29 5b 31 5d 20 3d 3d 20 28 63 68 61 72 2a 29  t)[1] == (char*)
2107c 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e 6d 61 74  &likeInfoAlt.mat
2107d 63 68 4f 6e 65 20 29 3b 0a 20 20 61 73 73 65 72  chOne );.  asser
2107e 74 28 20 26 28 28 63 68 61 72 2a 29 26 6c 69 6b  t( &((char*)&lik
2107f 65 49 6e 66 6f 41 6c 74 29 5b 32 5d 20 3d 3d 20  eInfoAlt)[2] == 
21080 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f  (char*)&likeInfo
21081 41 6c 74 2e 6d 61 74 63 68 53 65 74 20 29 3b 0a  Alt.matchSet );.
21082 20 20 2a 70 49 73 4e 6f 63 61 73 65 20 3d 20 28    *pIsNocase = (
21083 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51  pDef->flags & SQ
21084 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 3d  LITE_FUNC_CASE)=
21085 3d 30 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a  =0;.  return 1;.
21086 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 61 6c 6c  }../*.** All all
21087 20 6f 66 20 74 68 65 20 46 75 6e 63 44 65 66 20   of the FuncDef 
21088 73 74 72 75 63 74 75 72 65 73 20 69 6e 20 74 68  structures in th
21089 65 20 61 42 75 69 6c 74 69 6e 46 75 6e 63 5b 5d  e aBuiltinFunc[]
2108a 20 61 72 72 61 79 20 61 62 6f 76 65 0a 2a 2a 20   array above.** 
2108b 74 6f 20 74 68 65 20 67 6c 6f 62 61 6c 20 66 75  to the global fu
2108c 6e 63 74 69 6f 6e 20 68 61 73 68 20 74 61 62 6c  nction hash tabl
2108d 65 2e 20 20 54 68 69 73 20 6f 63 63 75 72 73 20  e.  This occurs 
2108e 61 74 20 73 74 61 72 74 2d 74 69 6d 65 20 28 61  at start-time (a
2108f 73 0a 2a 2a 20 61 20 63 6f 6e 73 65 71 75 65 6e  s.** a consequen
21090 63 65 20 6f 66 20 63 61 6c 6c 69 6e 67 20 73 71  ce of calling sq
21091 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
21092 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72  ())..**.** After
21093 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75   this routine ru
21094 6e 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  ns.*/.SQLITE_PRI
21095 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
21096 33 52 65 67 69 73 74 65 72 47 6c 6f 62 61 6c 46  3RegisterGlobalF
21097 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 7b 0a  unctions(void){.
21098 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 66 6f    /*.  ** The fo
21099 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 20 68 6f  llowing array ho
2109a 6c 64 73 20 46 75 6e 63 44 65 66 20 73 74 72 75  lds FuncDef stru
2109b 63 74 75 72 65 73 20 66 6f 72 20 61 6c 6c 20 6f  ctures for all o
2109c 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 0a  f the functions.
2109d 20 20 2a 2a 20 64 65 66 69 6e 65 64 20 69 6e 20    ** defined in 
2109e 74 68 69 73 20 66 69 6c 65 2e 0a 20 20 2a 2a 0a  this file..  **.
2109f 20 20 2a 2a 20 54 68 65 20 61 72 72 61 79 20 63    ** The array c
210a0 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74 61 6e  annot be constan
210a1 74 20 73 69 6e 63 65 20 63 68 61 6e 67 65 73 20  t since changes 
210a2 61 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65 0a  are made to the.
210a3 20 20 2a 2a 20 46 75 6e 63 44 65 66 2e 70 48 61    ** FuncDef.pHa
210a4 73 68 20 65 6c 65 6d 65 6e 74 73 20 61 74 20 73  sh elements at s
210a5 74 61 72 74 2d 74 69 6d 65 2e 20 20 54 68 65 20  tart-time.  The 
210a6 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 69 73  elements of this
210a7 20 61 72 72 61 79 0a 20 20 2a 2a 20 61 72 65 20   array.  ** are 
210a8 72 65 61 64 2d 6f 6e 6c 79 20 61 66 74 65 72 20  read-only after 
210a9 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69  initialization i
210aa 73 20 63 6f 6d 70 6c 65 74 65 2e 0a 20 20 2a 2f  s complete..  */
210ab 0a 20 20 73 74 61 74 69 63 20 53 51 4c 49 54 45  .  static SQLITE
210ac 5f 57 53 44 20 46 75 6e 63 44 65 66 20 61 42 75  _WSD FuncDef aBu
210ad 69 6c 74 69 6e 46 75 6e 63 5b 5d 20 3d 20 7b 0a  iltinFunc[] = {.
210ae 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 74 72      FUNCTION(ltr
210af 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  im,             
210b0 20 31 2c 20 31 2c 20 30 2c 20 74 72 69 6d 46 75   1, 1, 0, trimFu
210b1 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20  nc         ),.  
210b2 20 20 46 55 4e 43 54 49 4f 4e 28 6c 74 72 69 6d    FUNCTION(ltrim
210b3 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32  ,              2
210b4 2c 20 31 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63  , 1, 0, trimFunc
210b5 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20           ),.    
210b6 46 55 4e 43 54 49 4f 4e 28 72 74 72 69 6d 2c 20  FUNCTION(rtrim, 
210b7 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
210b8 32 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20  2, 0, trimFunc  
210b9 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
210ba 4e 43 54 49 4f 4e 28 72 74 72 69 6d 2c 20 20 20  NCTION(rtrim,   
210bb 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 32 2c             2, 2,
210bc 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20   0, trimFunc    
210bd 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
210be 54 49 4f 4e 28 74 72 69 6d 2c 20 20 20 20 20 20  TION(trim,      
210bf 20 20 20 20 20 20 20 20 20 31 2c 20 33 2c 20 30           1, 3, 0
210c0 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20  , trimFunc      
210c1 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
210c2 4f 4e 28 74 72 69 6d 2c 20 20 20 20 20 20 20 20  ON(trim,        
210c3 20 20 20 20 20 20 20 32 2c 20 33 2c 20 30 2c 20         2, 3, 0, 
210c4 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20  trimFunc        
210c5 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
210c6 28 6d 69 6e 2c 20 20 20 20 20 20 20 20 20 20 20  (min,           
210c7 20 20 20 20 2d 31 2c 20 30 2c 20 31 2c 20 6d 69      -1, 0, 1, mi
210c8 6e 6d 61 78 46 75 6e 63 20 20 20 20 20 20 20 29  nmaxFunc       )
210c9 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d  ,.    FUNCTION(m
210ca 69 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  in,             
210cb 20 20 20 30 2c 20 30 2c 20 31 2c 20 30 20 20 20     0, 0, 1, 0   
210cc 20 20 20 20 20 20 20 20 20 20 20 20 20 29 2c 0a               ),.
210cd 20 20 20 20 41 47 47 52 45 47 41 54 45 28 6d 69      AGGREGATE(mi
210ce 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n,              
210cf 20 31 2c 20 30 2c 20 31 2c 20 6d 69 6e 6d 61 78   1, 0, 1, minmax
210d0 53 74 65 70 2c 20 20 20 20 20 20 6d 69 6e 4d 61  Step,      minMa
210d1 78 46 69 6e 61 6c 69 7a 65 20 29 2c 0a 20 20 20  xFinalize ),.   
210d2 20 46 55 4e 43 54 49 4f 4e 28 6d 61 78 2c 20 20   FUNCTION(max,  
210d3 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c               -1,
210d4 20 31 2c 20 31 2c 20 6d 69 6e 6d 61 78 46 75 6e   1, 1, minmaxFun
210d5 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46  c       ),.    F
210d6 55 4e 43 54 49 4f 4e 28 6d 61 78 2c 20 20 20 20  UNCTION(max,    
210d7 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 31              0, 1
210d8 2c 20 31 2c 20 30 20 20 20 20 20 20 20 20 20 20  , 1, 0          
210d9 20 20 20 20 20 20 29 2c 0a 20 20 20 20 41 47 47        ),.    AGG
210da 52 45 47 41 54 45 28 6d 61 78 2c 20 20 20 20 20  REGATE(max,     
210db 20 20 20 20 20 20 20 20 20 20 31 2c 20 31 2c 20            1, 1, 
210dc 31 2c 20 6d 69 6e 6d 61 78 53 74 65 70 2c 20 20  1, minmaxStep,  
210dd 20 20 20 20 6d 69 6e 4d 61 78 46 69 6e 61 6c 69      minMaxFinali
210de 7a 65 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49  ze ),.    FUNCTI
210df 4f 4e 28 74 79 70 65 6f 66 2c 20 20 20 20 20 20  ON(typeof,      
210e0 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20         1, 0, 0, 
210e1 74 79 70 65 6f 66 46 75 6e 63 20 20 20 20 20 20  typeofFunc      
210e2 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
210e3 28 6c 65 6e 67 74 68 2c 20 20 20 20 20 20 20 20  (length,        
210e4 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 6c 65       1, 0, 0, le
210e5 6e 67 74 68 46 75 6e 63 20 20 20 20 20 20 20 29  ngthFunc       )
210e6 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73  ,.    FUNCTION(s
210e7 75 62 73 74 72 2c 20 20 20 20 20 20 20 20 20 20  ubstr,          
210e8 20 20 20 32 2c 20 30 2c 20 30 2c 20 73 75 62 73     2, 0, 0, subs
210e9 74 72 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a  trFunc       ),.
210ea 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 75 62      FUNCTION(sub
210eb 73 74 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  str,            
210ec 20 33 2c 20 30 2c 20 30 2c 20 73 75 62 73 74 72   3, 0, 0, substr
210ed 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20  Func       ),.  
210ee 20 20 46 55 4e 43 54 49 4f 4e 28 61 62 73 2c 20    FUNCTION(abs, 
210ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
210f0 2c 20 30 2c 20 30 2c 20 61 62 73 46 75 6e 63 20  , 0, 0, absFunc 
210f1 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20           ),.    
210f2 46 55 4e 43 54 49 4f 4e 28 72 6f 75 6e 64 2c 20  FUNCTION(round, 
210f3 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
210f4 30 2c 20 30 2c 20 72 6f 75 6e 64 46 75 6e 63 20  0, 0, roundFunc 
210f5 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
210f6 4e 43 54 49 4f 4e 28 72 6f 75 6e 64 2c 20 20 20  NCTION(round,   
210f7 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 30 2c             2, 0,
210f8 20 30 2c 20 72 6f 75 6e 64 46 75 6e 63 20 20 20   0, roundFunc   
210f9 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
210fa 54 49 4f 4e 28 75 70 70 65 72 2c 20 20 20 20 20  TION(upper,     
210fb 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30           1, 0, 0
210fc 2c 20 75 70 70 65 72 46 75 6e 63 20 20 20 20 20  , upperFunc     
210fd 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
210fe 4f 4e 28 6c 6f 77 65 72 2c 20 20 20 20 20 20 20  ON(lower,       
210ff 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20         1, 0, 0, 
21100 6c 6f 77 65 72 46 75 6e 63 20 20 20 20 20 20 20  lowerFunc       
21101 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
21102 28 63 6f 61 6c 65 73 63 65 2c 20 20 20 20 20 20  (coalesce,      
21103 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 30 20       1, 0, 0, 0 
21104 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 29                 )
21105 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63  ,.    FUNCTION(c
21106 6f 61 6c 65 73 63 65 2c 20 20 20 20 20 20 20 20  oalesce,        
21107 20 20 2d 31 2c 20 30 2c 20 30 2c 20 69 66 6e 75    -1, 0, 0, ifnu
21108 6c 6c 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a  llFunc       ),.
21109 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 6f 61      FUNCTION(coa
2110a 6c 65 73 63 65 2c 20 20 20 20 20 20 20 20 20 20  lesce,          
2110b 20 30 2c 20 30 2c 20 30 2c 20 30 20 20 20 20 20   0, 0, 0, 0     
2110c 20 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20             ),.  
2110d 20 20 46 55 4e 43 54 49 4f 4e 28 68 65 78 2c 20    FUNCTION(hex, 
2110e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
2110f 2c 20 30 2c 20 30 2c 20 68 65 78 46 75 6e 63 20  , 0, 0, hexFunc 
21110 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20           ),.    
21111 46 55 4e 43 54 49 4f 4e 28 69 66 6e 75 6c 6c 2c  FUNCTION(ifnull,
21112 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 20               2, 
21113 30 2c 20 31 2c 20 69 66 6e 75 6c 6c 46 75 6e 63  0, 1, ifnullFunc
21114 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
21115 4e 43 54 49 4f 4e 28 72 61 6e 64 6f 6d 2c 20 20  NCTION(random,  
21116 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c            -1, 0,
21117 20 30 2c 20 72 61 6e 64 6f 6d 46 75 6e 63 20 20   0, randomFunc  
21118 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
21119 54 49 4f 4e 28 72 61 6e 64 6f 6d 62 6c 6f 62 2c  TION(randomblob,
2111a 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30           1, 0, 0
2111b 2c 20 72 61 6e 64 6f 6d 42 6c 6f 62 20 20 20 20  , randomBlob    
2111c 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
2111d 4f 4e 28 6e 75 6c 6c 69 66 2c 20 20 20 20 20 20  ON(nullif,      
2111e 20 20 20 20 20 20 20 32 2c 20 30 2c 20 31 2c 20         2, 0, 1, 
2111f 6e 75 6c 6c 69 66 46 75 6e 63 20 20 20 20 20 20  nullifFunc      
21120 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
21121 28 73 71 6c 69 74 65 5f 76 65 72 73 69 6f 6e 2c  (sqlite_version,
21122 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 76 65       0, 0, 0, ve
21123 72 73 69 6f 6e 46 75 6e 63 20 20 20 20 20 20 29  rsionFunc      )
21124 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 71  ,.    FUNCTION(q
21125 75 6f 74 65 2c 20 20 20 20 20 20 20 20 20 20 20  uote,           
21126 20 20 20 31 2c 20 30 2c 20 30 2c 20 71 75 6f 74     1, 0, 0, quot
21127 65 46 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a  eFunc        ),.
21128 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 61 73      FUNCTION(las
21129 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 2c 20  t_insert_rowid, 
2112a 20 30 2c 20 30 2c 20 30 2c 20 6c 61 73 74 5f 69   0, 0, 0, last_i
2112b 6e 73 65 72 74 5f 72 6f 77 69 64 29 2c 0a 20 20  nsert_rowid),.  
2112c 20 20 46 55 4e 43 54 49 4f 4e 28 63 68 61 6e 67    FUNCTION(chang
2112d 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 30  es,            0
2112e 2c 20 30 2c 20 30 2c 20 63 68 61 6e 67 65 73 20  , 0, 0, changes 
2112f 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20           ),.    
21130 46 55 4e 43 54 49 4f 4e 28 74 6f 74 61 6c 5f 63  FUNCTION(total_c
21131 68 61 6e 67 65 73 2c 20 20 20 20 20 20 30 2c 20  hanges,      0, 
21132 30 2c 20 30 2c 20 74 6f 74 61 6c 5f 63 68 61 6e  0, 0, total_chan
21133 67 65 73 20 20 20 20 29 2c 0a 20 20 20 20 46 55  ges    ),.    FU
21134 4e 43 54 49 4f 4e 28 72 65 70 6c 61 63 65 2c 20  NCTION(replace, 
21135 20 20 20 20 20 20 20 20 20 20 20 33 2c 20 30 2c             3, 0,
21136 20 30 2c 20 72 65 70 6c 61 63 65 46 75 6e 63 20   0, replaceFunc 
21137 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
21138 54 49 4f 4e 28 7a 65 72 6f 62 6c 6f 62 2c 20 20  TION(zeroblob,  
21139 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30           1, 0, 0
2113a 2c 20 7a 65 72 6f 62 6c 6f 62 46 75 6e 63 20 20  , zeroblobFunc  
2113b 20 20 20 29 2c 0a 20 20 23 69 66 64 65 66 20 53     ),.  #ifdef S
2113c 51 4c 49 54 45 5f 53 4f 55 4e 44 45 58 0a 20 20  QLITE_SOUNDEX.  
2113d 20 20 46 55 4e 43 54 49 4f 4e 28 73 6f 75 6e 64    FUNCTION(sound
2113e 65 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 31  ex,            1
2113f 2c 20 30 2c 20 30 2c 20 73 6f 75 6e 64 65 78 46  , 0, 0, soundexF
21140 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 23 65  unc      ),.  #e
21141 6e 64 69 66 0a 20 20 23 69 66 6e 64 65 66 20 53  ndif.  #ifndef S
21142 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f  QLITE_OMIT_LOAD_
21143 45 58 54 45 4e 53 49 4f 4e 0a 20 20 20 20 46 55  EXTENSION.    FU
21144 4e 43 54 49 4f 4e 28 6c 6f 61 64 5f 65 78 74 65  NCTION(load_exte
21145 6e 73 69 6f 6e 2c 20 20 20 20 20 31 2c 20 30 2c  nsion,     1, 0,
21146 20 30 2c 20 6c 6f 61 64 45 78 74 20 20 20 20 20   0, loadExt     
21147 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
21148 54 49 4f 4e 28 6c 6f 61 64 5f 65 78 74 65 6e 73  TION(load_extens
21149 69 6f 6e 2c 20 20 20 20 20 32 2c 20 30 2c 20 30  ion,     2, 0, 0
2114a 2c 20 6c 6f 61 64 45 78 74 20 20 20 20 20 20 20  , loadExt       
2114b 20 20 20 29 2c 0a 20 20 23 65 6e 64 69 66 0a 20     ),.  #endif. 
2114c 20 20 20 41 47 47 52 45 47 41 54 45 28 73 75 6d     AGGREGATE(sum
2114d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2114e 31 2c 20 30 2c 20 30 2c 20 73 75 6d 53 74 65 70  1, 0, 0, sumStep
2114f 2c 20 20 20 20 20 20 20 20 20 73 75 6d 46 69 6e  ,         sumFin
21150 61 6c 69 7a 65 20 20 20 20 29 2c 0a 20 20 20 20  alize    ),.    
21151 41 47 47 52 45 47 41 54 45 28 74 6f 74 61 6c 2c  AGGREGATE(total,
21152 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
21153 30 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c 20 20  0, 0, sumStep,  
21154 20 20 20 20 20 20 20 74 6f 74 61 6c 46 69 6e 61         totalFina
21155 6c 69 7a 65 20 20 20 20 29 2c 0a 20 20 20 20 41  lize    ),.    A
21156 47 47 52 45 47 41 54 45 28 61 76 67 2c 20 20 20  GGREGATE(avg,   
21157 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30              1, 0
21158 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c 20 20 20  , 0, sumStep,   
21159 20 20 20 20 20 20 61 76 67 46 69 6e 61 6c 69 7a        avgFinaliz
2115a 65 20 20 20 20 29 2c 0a 20 20 20 20 41 47 47 52  e    ),.    AGGR
2115b 45 47 41 54 45 28 63 6f 75 6e 74 2c 20 20 20 20  EGATE(count,    
2115c 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20 30           0, 0, 0
2115d 2c 20 63 6f 75 6e 74 53 74 65 70 2c 20 20 20 20  , countStep,    
2115e 20 20 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65     countFinalize
2115f 20 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41    ),.    AGGREGA
21160 54 45 28 63 6f 75 6e 74 2c 20 20 20 20 20 20 20  TE(count,       
21161 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 63        1, 0, 0, c
21162 6f 75 6e 74 53 74 65 70 2c 20 20 20 20 20 20 20  ountStep,       
21163 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 20 20 29  countFinalize  )
21164 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 28  ,.    AGGREGATE(
21165 67 72 6f 75 70 5f 63 6f 6e 63 61 74 2c 20 20 20  group_concat,   
21166 20 20 2d 31 2c 20 30 2c 20 30 2c 20 67 72 6f 75    -1, 0, 0, grou
21167 70 43 6f 6e 63 61 74 53 74 65 70 2c 20 67 72 6f  pConcatStep, gro
21168 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c 69 7a 65  upConcatFinalize
21169 29 2c 0a 20 20 0a 20 20 20 20 4c 49 4b 45 46 55  ),.  .    LIKEFU
2116a 4e 43 28 67 6c 6f 62 2c 20 32 2c 20 26 67 6c 6f  NC(glob, 2, &glo
2116b 62 49 6e 66 6f 2c 20 53 51 4c 49 54 45 5f 46 55  bInfo, SQLITE_FU
2116c 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49 54 45 5f 46  NC_LIKE|SQLITE_F
2116d 55 4e 43 5f 43 41 53 45 29 2c 0a 20 20 23 69 66  UNC_CASE),.  #if
2116e 64 65 66 20 53 51 4c 49 54 45 5f 43 41 53 45 5f  def SQLITE_CASE_
2116f 53 45 4e 53 49 54 49 56 45 5f 4c 49 4b 45 0a 20  SENSITIVE_LIKE. 
21170 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c 69 6b 65     LIKEFUNC(like
21171 2c 20 32 2c 20 26 6c 69 6b 65 49 6e 66 6f 41 6c  , 2, &likeInfoAl
21172 74 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c  t, SQLITE_FUNC_L
21173 49 4b 45 7c 53 51 4c 49 54 45 5f 46 55 4e 43 5f  IKE|SQLITE_FUNC_
21174 43 41 53 45 29 2c 0a 20 20 20 20 4c 49 4b 45 46  CASE),.    LIKEF
21175 55 4e 43 28 6c 69 6b 65 2c 20 33 2c 20 26 6c 69  UNC(like, 3, &li
21176 6b 65 49 6e 66 6f 41 6c 74 2c 20 53 51 4c 49 54  keInfoAlt, SQLIT
21177 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49  E_FUNC_LIKE|SQLI
21178 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 2c 0a 20  TE_FUNC_CASE),. 
21179 20 23 65 6c 73 65 0a 20 20 20 20 4c 49 4b 45 46   #else.    LIKEF
2117a 55 4e 43 28 6c 69 6b 65 2c 20 32 2c 20 26 6c 69  UNC(like, 2, &li
2117b 6b 65 49 6e 66 6f 4e 6f 72 6d 2c 20 53 51 4c 49  keInfoNorm, SQLI
2117c 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29 2c 0a 20  TE_FUNC_LIKE),. 
2117d 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c 69 6b 65     LIKEFUNC(like
2117e 2c 20 33 2c 20 26 6c 69 6b 65 49 6e 66 6f 4e 6f  , 3, &likeInfoNo
2117f 72 6d 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  rm, SQLITE_FUNC_
21180 4c 49 4b 45 29 2c 0a 20 20 23 65 6e 64 69 66 0a  LIKE),.  #endif.
21181 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 69 3b 0a 20    };..  int i;. 
21182 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70 48   FuncDefHash *pH
21183 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75  ash = &GLOBAL(Fu
21184 6e 63 44 65 66 48 61 73 68 2c 20 73 71 6c 69 74  ncDefHash, sqlit
21185 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e  e3GlobalFunction
21186 73 29 3b 0a 20 20 46 75 6e 63 44 65 66 20 2a 61  s);.  FuncDef *a
21187 46 75 6e 63 20 3d 20 28 46 75 6e 63 44 65 66 2a  Func = (FuncDef*
21188 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66  )&GLOBAL(FuncDef
21189 2c 20 61 42 75 69 6c 74 69 6e 46 75 6e 63 29 3b  , aBuiltinFunc);
2118a 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41  ..  for(i=0; i<A
2118b 72 72 61 79 53 69 7a 65 28 61 42 75 69 6c 74 69  rraySize(aBuilti
2118c 6e 46 75 6e 63 29 3b 20 69 2b 2b 29 7b 0a 20 20  nFunc); i++){.  
2118d 20 20 73 71 6c 69 74 65 33 46 75 6e 63 44 65 66    sqlite3FuncDef
2118e 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 26 61  Insert(pHash, &a
2118f 46 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20  Func[i]);.  }.  
21190 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 44  sqlite3RegisterD
21191 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73  ateTimeFunctions
21192 28 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ();.}../********
21193 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 75  ****** End of fu
21194 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nc.c ***********
21195 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21196 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21197 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
21198 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
21199 65 20 69 6e 73 65 72 74 2e 63 20 2a 2a 2a 2a 2a  e insert.c *****
2119a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2119b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2119c 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
2119d 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a  1 September 15.*
2119e 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
2119f 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
211a0 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
211a1 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
211a2 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
211a3 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
211a4 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
211a5 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
211a6 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
211a7 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
211a8 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
211a9 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
211aa 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
211ab 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
211ac 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
211ad 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
211ae 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
211af 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
211b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
211b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
211b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
211b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
211b4 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
211b5 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74 69  ins C code routi
211b6 6e 65 73 20 74 68 61 74 20 61 72 65 20 63 61 6c  nes that are cal
211b7 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65  led by the parse
211b8 72 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20 49  r.** to handle I
211b9 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 73  NSERT statements
211ba 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a   in SQLite..**.*
211bb 2a 20 24 49 64 3a 20 69 6e 73 65 72 74 2e 63 2c  * $Id: insert.c,
211bc 76 20 31 2e 32 35 31 20 32 30 30 38 2f 31 31 2f  v 1.251 2008/11/
211bd 30 33 20 32 30 3a 35 35 3a 30 37 20 64 72 68 20  03 20:55:07 drh 
211be 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
211bf 53 65 74 20 50 34 20 6f 66 20 74 68 65 20 6d 6f  Set P4 of the mo
211c0 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e 73 65  st recently inse
211c1 72 74 65 64 20 6f 70 63 6f 64 65 20 74 6f 20 61  rted opcode to a
211c2 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79   column affinity
211c3 0a 2a 2a 20 73 74 72 69 6e 67 20 66 6f 72 20 69  .** string for i
211c4 6e 64 65 78 20 70 49 64 78 2e 20 41 20 63 6f 6c  ndex pIdx. A col
211c5 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72  umn affinity str
211c6 69 6e 67 20 68 61 73 20 6f 6e 65 20 63 68 61 72  ing has one char
211c7 61 63 74 65 72 0a 2a 2a 20 66 6f 72 20 65 61 63  acter.** for eac
211c8 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20  h column in the 
211c9 74 61 62 6c 65 2c 20 61 63 63 6f 72 64 69 6e 67  table, according
211ca 20 74 6f 20 74 68 65 20 61 66 66 69 6e 69 74 79   to the affinity
211cb 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 3a 0a   of the column:.
211cc 2a 2a 0a 2a 2a 20 20 43 68 61 72 61 63 74 65 72  **.**  Character
211cd 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 61 66 66        Column aff
211ce 69 6e 69 74 79 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d  inity.**  ------
211cf 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
211d0 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 27 61 27  --------.**  'a'
211d1 20 20 20 20 20 20 20 20 20 20 20 20 54 45 58 54              TEXT
211d2 0a 2a 2a 20 20 27 62 27 20 20 20 20 20 20 20 20  .**  'b'        
211d3 20 20 20 20 4e 4f 4e 45 0a 2a 2a 20 20 27 63 27      NONE.**  'c'
211d4 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4d 45              NUME
211d5 52 49 43 0a 2a 2a 20 20 27 64 27 20 20 20 20 20  RIC.**  'd'     
211d6 20 20 20 20 20 20 20 49 4e 54 45 47 45 52 0a 2a         INTEGER.*
211d7 2a 20 20 27 65 27 20 20 20 20 20 20 20 20 20 20  *  'e'          
211d8 20 20 52 45 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20    REAL.**.** An 
211d9 65 78 74 72 61 20 27 62 27 20 69 73 20 61 70 70  extra 'b' is app
211da 65 6e 64 65 64 20 74 6f 20 74 68 65 20 65 6e 64  ended to the end
211db 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 74   of the string t
211dc 6f 20 63 6f 76 65 72 20 74 68 65 0a 2a 2a 20 72  o cover the.** r
211dd 6f 77 69 64 20 74 68 61 74 20 61 70 70 65 61 72  owid that appear
211de 73 20 61 73 20 74 68 65 20 6c 61 73 74 20 63 6f  s as the last co
211df 6c 75 6d 6e 20 69 6e 20 65 76 65 72 79 20 69 6e  lumn in every in
211e0 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  dex..*/.SQLITE_P
211e1 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
211e2 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79  te3IndexAffinity
211e3 53 74 72 28 56 64 62 65 20 2a 76 2c 20 49 6e 64  Str(Vdbe *v, Ind
211e4 65 78 20 2a 70 49 64 78 29 7b 0a 20 20 69 66 28  ex *pIdx){.  if(
211e5 20 21 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 20   !pIdx->zColAff 
211e6 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 69  ){.    /* The fi
211e7 72 73 74 20 74 69 6d 65 20 61 20 63 6f 6c 75 6d  rst time a colum
211e8 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e  n affinity strin
211e9 67 20 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c  g for a particul
211ea 61 72 20 69 6e 64 65 78 20 69 73 0a 20 20 20 20  ar index is.    
211eb 2a 2a 20 72 65 71 75 69 72 65 64 2c 20 69 74 20  ** required, it 
211ec 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  is allocated and
211ed 20 70 6f 70 75 6c 61 74 65 64 20 68 65 72 65 2e   populated here.
211ee 20 49 74 20 69 73 20 74 68 65 6e 20 73 74 6f 72   It is then stor
211ef 65 64 20 61 73 0a 20 20 20 20 2a 2a 20 61 20 6d  ed as.    ** a m
211f0 65 6d 62 65 72 20 6f 66 20 74 68 65 20 49 6e 64  ember of the Ind
211f1 65 78 20 73 74 72 75 63 74 75 72 65 20 66 6f 72  ex structure for
211f2 20 73 75 62 73 65 71 75 65 6e 74 20 75 73 65 2e   subsequent use.
211f3 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
211f4 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69  he column affini
211f5 74 79 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 65  ty string will e
211f6 76 65 6e 74 75 61 6c 6c 79 20 62 65 20 64 65 6c  ventually be del
211f7 65 74 65 64 20 62 79 0a 20 20 20 20 2a 2a 20 73  eted by.    ** s
211f8 71 6c 69 74 65 44 65 6c 65 74 65 49 6e 64 65 78  qliteDeleteIndex
211f9 28 29 20 77 68 65 6e 20 74 68 65 20 49 6e 64 65  () when the Inde
211fa 78 20 73 74 72 75 63 74 75 72 65 20 69 74 73 65  x structure itse
211fb 6c 66 20 69 73 20 63 6c 65 61 6e 65 64 0a 20 20  lf is cleaned.  
211fc 20 20 2a 2a 20 75 70 2e 0a 20 20 20 20 2a 2f 0a    ** up..    */.
211fd 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 54      int n;.    T
211fe 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 49 64  able *pTab = pId
211ff 78 2d 3e 70 54 61 62 6c 65 3b 0a 20 20 20 20 73  x->pTable;.    s
21200 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c  qlite3 *db = sql
21201 69 74 65 33 56 64 62 65 44 62 28 76 29 3b 0a 20  ite3VdbeDb(v);. 
21202 20 20 20 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66     pIdx->zColAff
21203 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74   = (char *)sqlit
21204 65 33 4d 61 6c 6c 6f 63 28 70 49 64 78 2d 3e 6e  e3Malloc(pIdx->n
21205 43 6f 6c 75 6d 6e 2b 32 29 3b 0a 20 20 20 20 69  Column+2);.    i
21206 66 28 20 21 70 49 64 78 2d 3e 7a 43 6f 6c 41 66  f( !pIdx->zColAf
21207 66 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d  f ){.      db->m
21208 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
21209 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
2120a 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 6e 3d 30     }.    for(n=0
2120b 3b 20 6e 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  ; n<pIdx->nColum
2120c 6e 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 70  n; n++){.      p
2120d 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 5b 6e 5d 20  Idx->zColAff[n] 
2120e 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 49 64  = pTab->aCol[pId
2120f 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 5d 5d 2e  x->aiColumn[n]].
21210 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 20 7d 0a  affinity;.    }.
21211 20 20 20 20 70 49 64 78 2d 3e 7a 43 6f 6c 41 66      pIdx->zColAf
21212 66 5b 6e 2b 2b 5d 20 3d 20 53 51 4c 49 54 45 5f  f[n++] = SQLITE_
21213 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 70 49  AFF_NONE;.    pI
21214 64 78 2d 3e 7a 43 6f 6c 41 66 66 5b 6e 5d 20 3d  dx->zColAff[n] =
21215 20 30 3b 0a 20 20 7d 0a 20 0a 20 20 73 71 6c 69   0;.  }. .  sqli
21216 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
21217 76 2c 20 2d 31 2c 20 70 49 64 78 2d 3e 7a 43 6f  v, -1, pIdx->zCo
21218 6c 41 66 66 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a  lAff, 0);.}../*.
21219 2a 2a 20 53 65 74 20 50 34 20 6f 66 20 74 68 65  ** Set P4 of the
2121a 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69   most recently i
2121b 6e 73 65 72 74 65 64 20 6f 70 63 6f 64 65 20 74  nserted opcode t
2121c 6f 20 61 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e  o a column affin
2121d 69 74 79 0a 2a 2a 20 73 74 72 69 6e 67 20 66 6f  ity.** string fo
2121e 72 20 74 61 62 6c 65 20 70 54 61 62 2e 20 41 20  r table pTab. A 
2121f 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20  column affinity 
21220 73 74 72 69 6e 67 20 68 61 73 20 6f 6e 65 20 63  string has one c
21221 68 61 72 61 63 74 65 72 0a 2a 2a 20 66 6f 72 20  haracter.** for 
21222 65 61 63 68 20 63 6f 6c 75 6d 6e 20 69 6e 64 65  each column inde
21223 78 65 64 20 62 79 20 74 68 65 20 69 6e 64 65 78  xed by the index
21224 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74  , according to t
21225 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74  he affinity of t
21226 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 3a 0a 2a 2a  he.** column:.**
21227 0a 2a 2a 20 20 43 68 61 72 61 63 74 65 72 20 20  .**  Character  
21228 20 20 20 20 43 6f 6c 75 6d 6e 20 61 66 66 69 6e      Column affin
21229 69 74 79 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d  ity.**  --------
2122a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2122b 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 27 61 27 20 20  ------.**  'a'  
2122c 20 20 20 20 20 20 20 20 20 20 54 45 58 54 0a 2a            TEXT.*
2122d 2a 20 20 27 62 27 20 20 20 20 20 20 20 20 20 20  *  'b'          
2122e 20 20 4e 4f 4e 45 0a 2a 2a 20 20 27 63 27 20 20    NONE.**  'c'  
2122f 20 20 20 20 20 20 20 20 20 20 4e 55 4d 45 52 49            NUMERI
21230 43 0a 2a 2a 20 20 27 64 27 20 20 20 20 20 20 20  C.**  'd'       
21231 20 20 20 20 20 49 4e 54 45 47 45 52 0a 2a 2a 20       INTEGER.** 
21232 20 27 65 27 20 20 20 20 20 20 20 20 20 20 20 20   'e'            
21233 52 45 41 4c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  REAL.*/.SQLITE_P
21234 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
21235 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79  te3TableAffinity
21236 53 74 72 28 56 64 62 65 20 2a 76 2c 20 54 61 62  Str(Vdbe *v, Tab
21237 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 2f 2a 20  le *pTab){.  /* 
21238 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 61  The first time a
21239 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79   column affinity
2123a 20 73 74 72 69 6e 67 20 66 6f 72 20 61 20 70 61   string for a pa
2123b 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 0a 20  rticular table. 
2123c 20 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 2c   ** is required,
2123d 20 69 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64   it is allocated
2123e 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 64 20 68   and populated h
2123f 65 72 65 2e 20 49 74 20 69 73 20 74 68 65 6e 20  ere. It is then 
21240 0a 20 20 2a 2a 20 73 74 6f 72 65 64 20 61 73 20  .  ** stored as 
21241 61 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20  a member of the 
21242 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20  Table structure 
21243 66 6f 72 20 73 75 62 73 65 71 75 65 6e 74 20 75  for subsequent u
21244 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  se..  **.  ** Th
21245 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74  e column affinit
21246 79 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 65 76  y string will ev
21247 65 6e 74 75 61 6c 6c 79 20 62 65 20 64 65 6c 65  entually be dele
21248 74 65 64 20 62 79 0a 20 20 2a 2a 20 73 71 6c 69  ted by.  ** sqli
21249 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 29  te3DeleteTable()
2124a 20 77 68 65 6e 20 74 68 65 20 54 61 62 6c 65 20   when the Table 
2124b 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66  structure itself
2124c 20 69 73 20 63 6c 65 61 6e 65 64 20 75 70 2e 0a   is cleaned up..
2124d 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 54 61 62    */.  if( !pTab
2124e 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20 20  ->zColAff ){.   
2124f 20 63 68 61 72 20 2a 7a 43 6f 6c 41 66 66 3b 0a   char *zColAff;.
21250 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 73      int i;.    s
21251 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c  qlite3 *db = sql
21252 69 74 65 33 56 64 62 65 44 62 28 76 29 3b 0a 0a  ite3VdbeDb(v);..
21253 20 20 20 20 7a 43 6f 6c 41 66 66 20 3d 20 28 63      zColAff = (c
21254 68 61 72 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c  har *)sqlite3Mal
21255 6c 6f 63 28 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31  loc(pTab->nCol+1
21256 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 43 6f 6c  );.    if( !zCol
21257 41 66 66 20 29 7b 0a 20 20 20 20 20 20 64 62 2d  Aff ){.      db-
21258 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
21259 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  1;.      return;
2125a 0a 20 20 20 20 7d 0a 0a 20 20 20 20 66 6f 72 28  .    }..    for(
2125b 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f  i=0; i<pTab->nCo
2125c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 7a  l; i++){.      z
2125d 43 6f 6c 41 66 66 5b 69 5d 20 3d 20 70 54 61 62  ColAff[i] = pTab
2125e 2d 3e 61 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69  ->aCol[i].affini
2125f 74 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 43  ty;.    }.    zC
21260 6f 6c 41 66 66 5b 70 54 61 62 2d 3e 6e 43 6f 6c  olAff[pTab->nCol
21261 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 20 20 70  ] = '\0';..    p
21262 54 61 62 2d 3e 7a 43 6f 6c 41 66 66 20 3d 20 7a  Tab->zColAff = z
21263 43 6f 6c 41 66 66 3b 0a 20 20 7d 0a 0a 20 20 73  ColAff;.  }..  s
21264 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
21265 50 34 28 76 2c 20 2d 31 2c 20 70 54 61 62 2d 3e  P4(v, -1, pTab->
21266 7a 43 6f 6c 41 66 66 2c 20 30 29 3b 0a 7d 0a 0a  zColAff, 0);.}..
21267 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e 6f 6e  /*.** Return non
21268 2d 7a 65 72 6f 20 69 66 20 74 68 65 20 74 61 62  -zero if the tab
21269 6c 65 20 70 54 61 62 20 69 6e 20 64 61 74 61 62  le pTab in datab
2126a 61 73 65 20 69 44 62 20 6f 72 20 61 6e 79 20 6f  ase iDb or any o
2126b 66 20 69 74 73 20 69 6e 64 69 63 65 73 0a 2a 2a  f its indices.**
2126c 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e 65   have been opene
2126d 64 20 61 74 20 61 6e 79 20 70 6f 69 6e 74 20 69  d at any point i
2126e 6e 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72  n the VDBE progr
2126f 61 6d 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20  am beginning at 
21270 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 53 74 61  location.** iSta
21271 72 74 41 64 64 72 20 74 68 72 6f 75 67 68 74 20  rtAddr throught 
21272 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70  the end of the p
21273 72 6f 67 72 61 6d 2e 20 20 54 68 69 73 20 69 73  rogram.  This is
21274 20 75 73 65 64 20 74 6f 20 73 65 65 20 69 66 20   used to see if 
21275 0a 2a 2a 20 61 20 73 74 61 74 65 6d 65 6e 74 20  .** a statement 
21276 6f 66 20 74 68 65 20 66 6f 72 6d 20 20 22 49 4e  of the form  "IN
21277 53 45 52 54 20 49 4e 54 4f 20 3c 69 44 62 2c 20  SERT INTO <iDb, 
21278 70 54 61 62 3e 20 53 45 4c 45 43 54 20 2e 2e 2e  pTab> SELECT ...
21279 22 20 63 61 6e 20 0a 2a 2a 20 72 75 6e 20 77 69  " can .** run wi
2127a 74 68 6f 75 74 20 75 73 69 6e 67 20 74 65 6d 70  thout using temp
2127b 6f 72 61 72 79 20 74 61 62 6c 65 20 66 6f 72 20  orary table for 
2127c 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74  the results of t
2127d 68 65 20 53 45 4c 45 43 54 2e 20 0a 2a 2f 0a 73  he SELECT. .*/.s
2127e 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 73 54  tatic int readsT
2127f 61 62 6c 65 28 56 64 62 65 20 2a 76 2c 20 69 6e  able(Vdbe *v, in
21280 74 20 69 53 74 61 72 74 41 64 64 72 2c 20 69 6e  t iStartAddr, in
21281 74 20 69 44 62 2c 20 54 61 62 6c 65 20 2a 70 54  t iDb, Table *pT
21282 61 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  ab){.  int i;.  
21283 69 6e 74 20 69 45 6e 64 20 3d 20 73 71 6c 69 74  int iEnd = sqlit
21284 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
21285 72 28 76 29 3b 0a 20 20 66 6f 72 28 69 3d 69 53  r(v);.  for(i=iS
21286 74 61 72 74 41 64 64 72 3b 20 69 3c 69 45 6e 64  tartAddr; i<iEnd
21287 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 56 64 62 65  ; i++){.    Vdbe
21288 4f 70 20 2a 70 4f 70 20 3d 20 73 71 6c 69 74 65  Op *pOp = sqlite
21289 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20 69 29  3VdbeGetOp(v, i)
2128a 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f  ;.    assert( pO
2128b 70 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  p!=0 );.    if( 
2128c 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
2128d 4f 70 65 6e 52 65 61 64 20 26 26 20 70 4f 70 2d  OpenRead && pOp-
2128e 3e 70 33 3d 3d 69 44 62 20 29 7b 0a 20 20 20 20  >p3==iDb ){.    
2128f 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b    Index *pIndex;
21290 0a 20 20 20 20 20 20 69 6e 74 20 74 6e 75 6d 20  .      int tnum 
21291 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 20 20 20  = pOp->p2;.     
21292 20 69 66 28 20 74 6e 75 6d 3d 3d 70 54 61 62 2d   if( tnum==pTab-
21293 3e 74 6e 75 6d 20 29 7b 0a 20 20 20 20 20 20 20  >tnum ){.       
21294 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
21295 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 70 49 6e   }.      for(pIn
21296 64 65 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78  dex=pTab->pIndex
21297 3b 20 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78  ; pIndex; pIndex
21298 3d 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 29 7b  =pIndex->pNext){
21299 0a 20 20 20 20 20 20 20 20 69 66 28 20 74 6e 75  .        if( tnu
2129a 6d 3d 3d 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 20  m==pIndex->tnum 
2129b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  ){.          ret
2129c 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  urn 1;.        }
2129d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23  .      }.    }.#
2129e 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2129f 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
212a0 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63      if( pOp->opc
212a1 6f 64 65 3d 3d 4f 50 5f 56 4f 70 65 6e 20 26 26  ode==OP_VOpen &&
212a2 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3d 3d   pOp->p4.pVtab==
212a3 70 54 61 62 2d 3e 70 56 74 61 62 20 29 7b 0a 20  pTab->pVtab ){. 
212a4 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
212a5 2d 3e 70 34 2e 70 56 74 61 62 21 3d 30 20 29 3b  ->p4.pVtab!=0 );
212a6 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
212a7 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56  Op->p4type==P4_V
212a8 54 41 42 20 29 3b 0a 20 20 20 20 20 20 72 65 74  TAB );.      ret
212a9 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e  urn 1;.    }.#en
212aa 64 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  dif.  }.  return
212ab 20 30 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53   0;.}..#ifndef S
212ac 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
212ad 4e 43 52 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 57  NCREMENT./*.** W
212ae 72 69 74 65 20 6f 75 74 20 63 6f 64 65 20 74 6f  rite out code to
212af 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   initialize the 
212b0 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 6c 6f  autoincrement lo
212b1 67 69 63 2e 20 20 54 68 69 73 20 63 6f 64 65 0a  gic.  This code.
212b2 2a 2a 20 6c 6f 6f 6b 73 20 75 70 20 74 68 65 20  ** looks up the 
212b3 63 75 72 72 65 6e 74 20 61 75 74 6f 69 6e 63 72  current autoincr
212b4 65 6d 65 6e 74 20 76 61 6c 75 65 20 69 6e 20 74  ement value in t
212b5 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  he sqlite_sequen
212b6 63 65 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20  ce.** table and 
212b7 73 74 6f 72 65 73 20 74 68 61 74 20 76 61 6c 75  stores that valu
212b8 65 20 69 6e 20 61 20 72 65 67 69 73 74 65 72 2e  e in a register.
212b9 20 20 43 6f 64 65 20 67 65 6e 65 72 61 74 65 64    Code generated
212ba 20 62 79 0a 2a 2a 20 61 75 74 6f 49 6e 63 53 74   by.** autoIncSt
212bb 65 70 28 29 20 77 69 6c 6c 20 6b 65 65 70 20 74  ep() will keep t
212bc 68 61 74 20 72 65 67 69 73 74 65 72 20 68 6f 6c  hat register hol
212bd 64 69 6e 67 20 74 68 65 20 6c 61 72 67 65 73 74  ding the largest
212be 0a 2a 2a 20 72 6f 77 69 64 20 76 61 6c 75 65 2e  .** rowid value.
212bf 20 20 43 6f 64 65 20 67 65 6e 65 72 61 74 65 64    Code generated
212c0 20 62 79 20 61 75 74 6f 49 6e 63 45 6e 64 28 29   by autoIncEnd()
212c1 20 77 69 6c 6c 20 77 72 69 74 65 20 74 68 65 20   will write the 
212c2 6e 65 77 0a 2a 2a 20 6c 61 72 67 65 73 74 20 76  new.** largest v
212c3 61 6c 75 65 20 6f 66 20 74 68 65 20 63 6f 75 6e  alue of the coun
212c4 74 65 72 20 62 61 63 6b 20 69 6e 74 6f 20 74 68  ter back into th
212c5 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  e sqlite_sequenc
212c6 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  e table..**.** T
212c7 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
212c8 72 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66  rns the index of
212c9 20 74 68 65 20 6d 65 6d 5b 5d 20 63 65 6c 6c 20   the mem[] cell 
212ca 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a  that contains.**
212cb 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77   the maximum row
212cc 69 64 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 0a 2a  id counter..**.*
212cd 2a 20 54 68 72 65 65 20 63 6f 6e 73 65 63 75 74  * Three consecut
212ce 69 76 65 20 72 65 67 69 73 74 65 72 73 20 61 72  ive registers ar
212cf 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74  e allocated by t
212d0 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 54 68  his routine.  Th
212d1 65 0a 2a 2a 20 66 69 72 73 74 20 74 77 6f 20 68  e.** first two h
212d2 6f 6c 64 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  old the name of 
212d3 74 68 65 20 74 61 72 67 65 74 20 74 61 62 6c 65  the target table
212d4 20 61 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d   and the maximum
212d5 20 72 6f 77 69 64 20 0a 2a 2a 20 69 6e 73 65 72   rowid .** inser
212d6 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74 61 72  ted into the tar
212d7 67 65 74 20 74 61 62 6c 65 2c 20 72 65 73 70 65  get table, respe
212d8 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 54 68 65 20  ctively..** The 
212d9 74 68 69 72 64 20 68 6f 6c 64 73 20 74 68 65 20  third holds the 
212da 72 6f 77 69 64 20 69 6e 20 73 71 6c 69 74 65 5f  rowid in sqlite_
212db 73 65 71 75 65 6e 63 65 20 77 68 65 72 65 20 77  sequence where w
212dc 65 20 77 69 6c 6c 0a 2a 2a 20 77 72 69 74 65 20  e will.** write 
212dd 62 61 63 6b 20 74 68 65 20 72 65 76 69 73 65 64  back the revised
212de 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 2e 20   maximum rowid. 
212df 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
212e0 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 69 6e 64  turns the.** ind
212e1 65 78 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64  ex of the second
212e2 20 6f 66 20 74 68 65 73 65 20 74 68 72 65 65 20   of these three 
212e3 72 65 67 69 73 74 65 72 73 2e 0a 2a 2f 0a 73 74  registers..*/.st
212e4 61 74 69 63 20 69 6e 74 20 61 75 74 6f 49 6e 63  atic int autoInc
212e5 42 65 67 69 6e 28 0a 20 20 50 61 72 73 65 20 2a  Begin(.  Parse *
212e6 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20  pParse,      /* 
212e7 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
212e8 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20  */.  int iDb,   
212e9 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
212ea 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  x of the databas
212eb 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a  e holding pTab *
212ec 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20  /.  Table *pTab 
212ed 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
212ee 61 62 6c 65 20 77 65 20 61 72 65 20 77 72 69 74  able we are writ
212ef 69 6e 67 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 69  ing to */.){.  i
212f0 6e 74 20 6d 65 6d 49 64 20 3d 20 30 3b 20 20 20  nt memId = 0;   
212f1 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68     /* Register h
212f2 6f 6c 64 69 6e 67 20 6d 61 78 69 6d 75 6d 20 72  olding maximum r
212f3 6f 77 69 64 20 2a 2f 0a 20 20 69 66 28 20 70 54  owid */.  if( pT
212f4 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54  ab->tabFlags & T
212f5 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20  F_Autoincrement 
212f6 29 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d  ){.    Vdbe *v =
212f7 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
212f8 20 20 20 20 44 62 20 2a 70 44 62 20 3d 20 26 70      Db *pDb = &p
212f9 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69  Parse->db->aDb[i
212fa 44 62 5d 3b 0a 20 20 20 20 69 6e 74 20 69 43 75  Db];.    int iCu
212fb 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  r = pParse->nTab
212fc 3b 0a 20 20 20 20 69 6e 74 20 61 64 64 72 3b 20  ;.    int addr; 
212fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
212fe 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   Address of the 
212ff 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20  top of the loop 
21300 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 76  */.    assert( v
21301 20 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e   );.    pParse->
21302 6e 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20 20  nMem++;         
21303 2f 2a 20 48 6f 6c 64 73 20 6e 61 6d 65 20 6f 66  /* Holds name of
21304 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 6d 65   table */.    me
21305 6d 49 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  mId = ++pParse->
21306 6e 4d 65 6d 3b 0a 20 20 20 20 70 50 61 72 73 65  nMem;.    pParse
21307 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20 20 20 73 71  ->nMem++;.    sq
21308 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70  lite3OpenTable(p
21309 50 61 72 73 65 2c 20 69 43 75 72 2c 20 69 44 62  Parse, iCur, iDb
2130a 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e  , pDb->pSchema->
2130b 70 53 65 71 54 61 62 2c 20 4f 50 5f 4f 70 65 6e  pSeqTab, OP_Open
2130c 52 65 61 64 29 3b 0a 20 20 20 20 61 64 64 72 20  Read);.    addr 
2130d 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
2130e 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20  rentAddr(v);.   
2130f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21310 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
21311 2c 20 30 2c 20 6d 65 6d 49 64 2d 31 2c 20 30 2c  , 0, memId-1, 0,
21312 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29   pTab->zName, 0)
21313 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
21314 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
21315 77 69 6e 64 2c 20 69 43 75 72 2c 20 61 64 64 72  wind, iCur, addr
21316 2b 39 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  +9);.    sqlite3
21317 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
21318 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 30  _Column, iCur, 0
21319 2c 20 6d 65 6d 49 64 29 3b 0a 20 20 20 20 73 71  , memId);.    sq
2131a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2131b 76 2c 20 4f 50 5f 4e 65 2c 20 6d 65 6d 49 64 2d  v, OP_Ne, memId-
2131c 31 2c 20 61 64 64 72 2b 37 2c 20 6d 65 6d 49 64  1, addr+7, memId
2131d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2131e 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 53 51  beChangeP5(v, SQ
2131f 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29  LITE_JUMPIFNULL)
21320 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
21321 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f  eAddOp2(v, OP_Ro
21322 77 69 64 2c 20 69 43 75 72 2c 20 6d 65 6d 49 64  wid, iCur, memId
21323 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  +1);.    sqlite3
21324 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
21325 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 31  _Column, iCur, 1
21326 2c 20 6d 65 6d 49 64 29 3b 0a 20 20 20 20 73 71  , memId);.    sq
21327 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
21328 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61  v, OP_Goto, 0, a
21329 64 64 72 2b 39 29 3b 0a 20 20 20 20 73 71 6c 69  ddr+9);.    sqli
2132a 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2132b 20 4f 50 5f 4e 65 78 74 2c 20 69 43 75 72 2c 20   OP_Next, iCur, 
2132c 61 64 64 72 2b 32 29 3b 0a 20 20 20 20 73 71 6c  addr+2);.    sql
2132d 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2132e 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
2132f 20 6d 65 6d 49 64 29 3b 0a 20 20 20 20 73 71 6c   memId);.    sql
21330 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
21331 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72  , OP_Close, iCur
21332 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , 0);.  }.  retu
21333 72 6e 20 6d 65 6d 49 64 3b 0a 7d 0a 0a 2f 2a 0a  rn memId;.}../*.
21334 2a 2a 20 55 70 64 61 74 65 20 74 68 65 20 6d 61  ** Update the ma
21335 78 69 6d 75 6d 20 72 6f 77 69 64 20 66 6f 72 20  ximum rowid for 
21336 61 6e 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74  an autoincrement
21337 20 63 61 6c 63 75 6c 61 74 69 6f 6e 2e 0a 2a 2a   calculation..**
21338 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
21339 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65   should be calle
2133a 64 20 77 68 65 6e 20 74 68 65 20 74 6f 70 20 6f  d when the top o
2133b 66 20 74 68 65 20 73 74 61 63 6b 20 68 6f 6c 64  f the stack hold
2133c 73 20 61 0a 2a 2a 20 6e 65 77 20 72 6f 77 69 64  s a.** new rowid
2133d 20 74 68 61 74 20 69 73 20 61 62 6f 75 74 20 74   that is about t
2133e 6f 20 62 65 20 69 6e 73 65 72 74 65 64 2e 20 20  o be inserted.  
2133f 49 66 20 74 68 61 74 20 6e 65 77 20 72 6f 77 69  If that new rowi
21340 64 20 69 73 0a 2a 2a 20 6c 61 72 67 65 72 20 74  d is.** larger t
21341 68 61 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  han the maximum 
21342 72 6f 77 69 64 20 69 6e 20 74 68 65 20 6d 65 6d  rowid in the mem
21343 49 64 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2c 20  Id memory cell, 
21344 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f  then the.** memo
21345 72 79 20 63 65 6c 6c 20 69 73 20 75 70 64 61 74  ry cell is updat
21346 65 64 2e 20 20 54 68 65 20 73 74 61 63 6b 20 69  ed.  The stack i
21347 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a  s unchanged..*/.
21348 73 74 61 74 69 63 20 76 6f 69 64 20 61 75 74 6f  static void auto
21349 49 6e 63 53 74 65 70 28 50 61 72 73 65 20 2a 70  IncStep(Parse *p
2134a 50 61 72 73 65 2c 20 69 6e 74 20 6d 65 6d 49 64  Parse, int memId
2134b 2c 20 69 6e 74 20 72 65 67 52 6f 77 69 64 29 7b  , int regRowid){
2134c 0a 20 20 69 66 28 20 6d 65 6d 49 64 3e 30 20 29  .  if( memId>0 )
2134d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
2134e 65 41 64 64 4f 70 32 28 70 50 61 72 73 65 2d 3e  eAddOp2(pParse->
2134f 70 56 64 62 65 2c 20 4f 50 5f 4d 65 6d 4d 61 78  pVdbe, OP_MemMax
21350 2c 20 6d 65 6d 49 64 2c 20 72 65 67 52 6f 77 69  , memId, regRowi
21351 64 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  d);.  }.}../*.**
21352 20 41 66 74 65 72 20 64 6f 69 6e 67 20 6f 6e 65   After doing one
21353 20 6f 72 20 6d 6f 72 65 20 69 6e 73 65 72 74 73   or more inserts
21354 2c 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f  , the maximum ro
21355 77 69 64 20 69 73 20 73 74 6f 72 65 64 0a 2a 2a  wid is stored.**
21356 20 69 6e 20 72 65 67 5b 6d 65 6d 49 64 5d 2e 20   in reg[memId]. 
21357 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
21358 6f 20 77 72 69 74 65 20 74 68 69 73 20 76 61 6c  o write this val
21359 75 65 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65  ue back into the
2135a 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 5f 73  .** the sqlite_s
2135b 65 71 75 65 6e 63 65 20 74 61 62 6c 65 2e 0a 2a  equence table..*
2135c 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 75  /.static void au
2135d 74 6f 49 6e 63 45 6e 64 28 0a 20 20 50 61 72 73  toIncEnd(.  Pars
2135e 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f  e *pParse,     /
2135f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f  * The parsing co
21360 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69  ntext */.  int i
21361 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  Db,           /*
21362 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61   Index of the da
21363 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 70  tabase holding p
21364 54 61 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  Tab */.  Table *
21365 70 54 61 62 2c 20 20 20 20 20 20 20 2f 2a 20 54  pTab,       /* T
21366 61 62 6c 65 20 77 65 20 61 72 65 20 69 6e 73 65  able we are inse
21367 72 74 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a 20 20  rting into */.  
21368 69 6e 74 20 6d 65 6d 49 64 20 20 20 20 20 20 20  int memId       
21369 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c     /* Memory cel
2136a 6c 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 6d 61  l holding the ma
2136b 78 69 6d 75 6d 20 72 6f 77 69 64 20 2a 2f 0a 29  ximum rowid */.)
2136c 7b 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 74 61  {.  if( pTab->ta
2136d 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f  bFlags & TF_Auto
2136e 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20 20  increment ){.   
2136f 20 69 6e 74 20 69 43 75 72 20 3d 20 70 50 61 72   int iCur = pPar
21370 73 65 2d 3e 6e 54 61 62 3b 0a 20 20 20 20 56 64  se->nTab;.    Vd
21371 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
21372 70 56 64 62 65 3b 0a 20 20 20 20 44 62 20 2a 70  pVdbe;.    Db *p
21373 44 62 20 3d 20 26 70 50 61 72 73 65 2d 3e 64 62  Db = &pParse->db
21374 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 20 20  ->aDb[iDb];.    
21375 69 6e 74 20 6a 31 3b 0a 20 20 20 20 69 6e 74 20  int j1;.    int 
21376 69 52 65 63 20 3d 20 2b 2b 70 50 61 72 73 65 2d  iRec = ++pParse-
21377 3e 6e 4d 65 6d 3b 20 20 20 20 2f 2a 20 4d 65 6d  >nMem;    /* Mem
21378 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 66 6f  ory cell used fo
21379 72 20 72 65 63 6f 72 64 20 2a 2f 0a 0a 20 20 20  r record */..   
2137a 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a 20 20   assert( v );.  
2137b 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62    sqlite3OpenTab
2137c 6c 65 28 70 50 61 72 73 65 2c 20 69 43 75 72 2c  le(pParse, iCur,
2137d 20 69 44 62 2c 20 70 44 62 2d 3e 70 53 63 68 65   iDb, pDb->pSche
2137e 6d 61 2d 3e 70 53 65 71 54 61 62 2c 20 4f 50 5f  ma->pSeqTab, OP_
2137f 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20 20 20 20  OpenWrite);.    
21380 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j1 = sqlite3Vdbe
21381 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74  AddOp1(v, OP_Not
21382 4e 75 6c 6c 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a  Null, memId+1);.
21383 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21384 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52  ddOp2(v, OP_NewR
21385 6f 77 69 64 2c 20 69 43 75 72 2c 20 6d 65 6d 49  owid, iCur, memI
21386 64 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  d+1);.    sqlite
21387 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
21388 20 6a 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   j1);.    sqlite
21389 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2138a 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 6d 65  P_MakeRecord, me
2138b 6d 49 64 2d 31 2c 20 32 2c 20 69 52 65 63 29 3b  mId-1, 2, iRec);
2138c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2138d 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73  AddOp3(v, OP_Ins
2138e 65 72 74 2c 20 69 43 75 72 2c 20 69 52 65 63 2c  ert, iCur, iRec,
2138f 20 6d 65 6d 49 64 2b 31 29 3b 0a 20 20 20 20 73   memId+1);.    s
21390 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
21391 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50  P5(v, OPFLAG_APP
21392 45 4e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  END);.    sqlite
21393 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
21394 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 29 3b 0a  P_Close, iCur);.
21395 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 2f 2a 0a 2a    }.}.#else./*.*
21396 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  * If SQLITE_OMIT
21397 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 69  _AUTOINCREMENT i
21398 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20  s defined, then 
21399 74 68 65 20 74 68 72 65 65 20 72 6f 75 74 69 6e  the three routin
2139a 65 73 0a 2a 2a 20 61 62 6f 76 65 20 61 72 65 20  es.** above are 
2139b 61 6c 6c 20 6e 6f 2d 6f 70 73 0a 2a 2f 0a 23 20  all no-ops.*/.# 
2139c 64 65 66 69 6e 65 20 61 75 74 6f 49 6e 63 42 65  define autoIncBe
2139d 67 69 6e 28 41 2c 42 2c 43 29 20 28 30 29 0a 23  gin(A,B,C) (0).#
2139e 20 64 65 66 69 6e 65 20 61 75 74 6f 49 6e 63 53   define autoIncS
2139f 74 65 70 28 41 2c 42 2c 43 29 0a 23 20 64 65 66  tep(A,B,C).# def
213a0 69 6e 65 20 61 75 74 6f 49 6e 63 45 6e 64 28 41  ine autoIncEnd(A
213a1 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 20 2f  ,B,C,D).#endif /
213a2 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  * SQLITE_OMIT_AU
213a3 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 0a  TOINCREMENT */..
213a4 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c  ./* Forward decl
213a5 61 72 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69  aration */.stati
213a6 63 20 69 6e 74 20 78 66 65 72 4f 70 74 69 6d 69  c int xferOptimi
213a7 7a 61 74 69 6f 6e 28 0a 20 20 50 61 72 73 65 20  zation(.  Parse 
213a8 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
213a9 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78  /* Parser contex
213aa 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 44  t */.  Table *pD
213ab 65 73 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  est,         /* 
213ac 54 68 65 20 74 61 62 6c 65 20 77 65 20 61 72 65  The table we are
213ad 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 74 6f 20   inserting into 
213ae 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65  */.  Select *pSe
213af 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 41 20  lect,      /* A 
213b0 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
213b1 20 74 6f 20 75 73 65 20 61 73 20 74 68 65 20 64   to use as the d
213b2 61 74 61 20 73 6f 75 72 63 65 20 2a 2f 0a 20 20  ata source */.  
213b3 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20 20 20  int onError,    
213b4 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20        /* How to 
213b5 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61 69 6e  handle constrain
213b6 74 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e  t errors */.  in
213b7 74 20 69 44 62 44 65 73 74 20 20 20 20 20 20 20  t iDbDest       
213b8 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
213b9 61 73 65 20 6f 66 20 70 44 65 73 74 20 2a 2f 0a  ase of pDest */.
213ba 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  );../*.** This r
213bb 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 20 74  outine is call t
213bc 6f 20 68 61 6e 64 6c 65 20 53 51 4c 20 6f 66 20  o handle SQL of 
213bd 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f  the following fo
213be 72 6d 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 69 6e  rms:.**.**    in
213bf 73 65 72 74 20 69 6e 74 6f 20 54 41 42 4c 45 20  sert into TABLE 
213c0 28 49 44 4c 49 53 54 29 20 76 61 6c 75 65 73 28  (IDLIST) values(
213c1 45 58 50 52 4c 49 53 54 29 0a 2a 2a 20 20 20 20  EXPRLIST).**    
213c2 69 6e 73 65 72 74 20 69 6e 74 6f 20 54 41 42 4c  insert into TABL
213c3 45 20 28 49 44 4c 49 53 54 29 20 73 65 6c 65 63  E (IDLIST) selec
213c4 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 44 4c 49  t.**.** The IDLI
213c5 53 54 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65  ST following the
213c6 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 61   table name is a
213c7 6c 77 61 79 73 20 6f 70 74 69 6f 6e 61 6c 2e 20  lways optional. 
213c8 20 49 66 20 6f 6d 69 74 74 65 64 2c 0a 2a 2a 20   If omitted,.** 
213c9 74 68 65 6e 20 61 20 6c 69 73 74 20 6f 66 20 61  then a list of a
213ca 6c 6c 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 74  ll columns for t
213cb 68 65 20 74 61 62 6c 65 20 69 73 20 73 75 62 73  he table is subs
213cc 74 69 74 75 74 65 64 2e 20 20 54 68 65 20 49 44  tituted.  The ID
213cd 4c 49 53 54 0a 2a 2a 20 61 70 70 65 61 72 73 20  LIST.** appears 
213ce 69 6e 20 74 68 65 20 70 43 6f 6c 75 6d 6e 20 70  in the pColumn p
213cf 61 72 61 6d 65 74 65 72 2e 20 20 70 43 6f 6c 75  arameter.  pColu
213d0 6d 6e 20 69 73 20 4e 55 4c 4c 20 69 66 20 49 44  mn is NULL if ID
213d1 4c 49 53 54 20 69 73 20 6f 6d 69 74 74 65 64 2e  LIST is omitted.
213d2 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4c 69 73 74  .**.** The pList
213d3 20 70 61 72 61 6d 65 74 65 72 20 68 6f 6c 64 73   parameter holds
213d4 20 45 58 50 52 4c 49 53 54 20 69 6e 20 74 68 65   EXPRLIST in the
213d5 20 66 69 72 73 74 20 66 6f 72 6d 20 6f 66 20 74   first form of t
213d6 68 65 20 49 4e 53 45 52 54 0a 2a 2a 20 73 74 61  he INSERT.** sta
213d7 74 65 6d 65 6e 74 20 61 62 6f 76 65 2c 20 61 6e  tement above, an
213d8 64 20 70 53 65 6c 65 63 74 20 69 73 20 4e 55 4c  d pSelect is NUL
213d9 4c 2e 20 20 46 6f 72 20 74 68 65 20 73 65 63 6f  L.  For the seco
213da 6e 64 20 66 6f 72 6d 2c 20 70 4c 69 73 74 20 69  nd form, pList i
213db 73 0a 2a 2a 20 4e 55 4c 4c 20 61 6e 64 20 70 53  s.** NULL and pS
213dc 65 6c 65 63 74 20 69 73 20 61 20 70 6f 69 6e 74  elect is a point
213dd 65 72 20 74 6f 20 74 68 65 20 73 65 6c 65 63 74  er to the select
213de 20 73 74 61 74 65 6d 65 6e 74 20 75 73 65 64 20   statement used 
213df 74 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 64  to generate.** d
213e0 61 74 61 20 66 6f 72 20 74 68 65 20 69 6e 73 65  ata for the inse
213e1 72 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  rt..**.** The co
213e2 64 65 20 67 65 6e 65 72 61 74 65 64 20 66 6f 6c  de generated fol
213e3 6c 6f 77 73 20 6f 6e 65 20 6f 66 20 66 6f 75 72  lows one of four
213e4 20 74 65 6d 70 6c 61 74 65 73 2e 20 20 46 6f 72   templates.  For
213e5 20 61 20 73 69 6d 70 6c 65 0a 2a 2a 20 73 65 6c   a simple.** sel
213e6 65 63 74 20 77 69 74 68 20 64 61 74 61 20 63 6f  ect with data co
213e7 6d 69 6e 67 20 66 72 6f 6d 20 61 20 56 41 4c 55  ming from a VALU
213e8 45 53 20 63 6c 61 75 73 65 2c 20 74 68 65 20 63  ES clause, the c
213e9 6f 64 65 20 65 78 65 63 75 74 65 73 0a 2a 2a 20  ode executes.** 
213ea 6f 6e 63 65 20 73 74 72 61 69 67 68 74 20 64 6f  once straight do
213eb 77 6e 20 74 68 72 6f 75 67 68 2e 20 20 50 73 65  wn through.  Pse
213ec 75 64 6f 2d 63 6f 64 65 20 66 6f 6c 6c 6f 77 73  udo-code follows
213ed 20 28 77 65 20 63 61 6c 6c 20 74 68 69 73 0a 2a   (we call this.*
213ee 2a 20 74 68 65 20 22 31 73 74 20 74 65 6d 70 6c  * the "1st templ
213ef 61 74 65 22 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ate"):.**.**    
213f0 20 20 20 20 20 6f 70 65 6e 20 77 72 69 74 65 20       open write 
213f1 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c 65  cursor to <table
213f2 3e 20 61 6e 64 20 69 74 73 20 69 6e 64 69 63 65  > and its indice
213f3 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 70 75 74  s.**         put
213f4 73 20 56 41 4c 55 45 53 20 63 6c 61 75 73 65 20  s VALUES clause 
213f5 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e 74 6f  expressions onto
213f6 20 74 68 65 20 73 74 61 63 6b 0a 2a 2a 20 20 20   the stack.**   
213f7 20 20 20 20 20 20 77 72 69 74 65 20 74 68 65 20        write the 
213f8 72 65 73 75 6c 74 69 6e 67 20 72 65 63 6f 72 64  resulting record
213f9 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a   into <table>.**
213fa 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75 70           cleanup
213fb 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 72 65 65  .**.** The three
213fc 20 72 65 6d 61 69 6e 69 6e 67 20 74 65 6d 70 6c   remaining templ
213fd 61 74 65 73 20 61 73 73 75 6d 65 20 74 68 65 20  ates assume the 
213fe 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6f 66 20  statement is of 
213ff 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20  the form.**.**  
21400 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 3c 74 61   INSERT INTO <ta
21401 62 6c 65 3e 20 53 45 4c 45 43 54 20 2e 2e 2e 0a  ble> SELECT ....
21402 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 45 4c  **.** If the SEL
21403 45 43 54 20 63 6c 61 75 73 65 20 69 73 20 6f 66  ECT clause is of
21404 20 74 68 65 20 72 65 73 74 72 69 63 74 65 64 20   the restricted 
21405 66 6f 72 6d 20 22 53 45 4c 45 43 54 20 2a 20 46  form "SELECT * F
21406 52 4f 4d 20 3c 74 61 62 6c 65 32 3e 22 20 2d 0a  ROM <table2>" -.
21407 2a 2a 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64  ** in other word
21408 73 20 69 66 20 74 68 65 20 53 45 4c 45 43 54 20  s if the SELECT 
21409 70 75 6c 6c 73 20 61 6c 6c 20 63 6f 6c 75 6d 6e  pulls all column
2140a 73 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20  s from a single 
2140b 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65  table.** and the
2140c 72 65 20 69 73 20 6e 6f 20 57 48 45 52 45 20 6f  re is no WHERE o
2140d 72 20 4c 49 4d 49 54 20 6f 72 20 47 52 4f 55 50  r LIMIT or GROUP
2140e 20 42 59 20 6f 72 20 4f 52 44 45 52 20 42 59 20   BY or ORDER BY 
2140f 63 6c 61 75 73 65 73 2c 20 61 6e 64 0a 2a 2a 20  clauses, and.** 
21410 69 66 20 3c 74 61 62 6c 65 32 3e 20 61 6e 64 20  if <table2> and 
21411 3c 74 61 62 6c 65 31 3e 20 61 72 65 20 64 69 73  <table1> are dis
21412 74 69 6e 63 74 20 74 61 62 6c 65 73 20 62 75 74  tinct tables but
21413 20 68 61 76 65 20 69 64 65 6e 74 69 63 61 6c 0a   have identical.
21414 2a 2a 20 73 63 68 65 6d 61 73 2c 20 69 6e 63 6c  ** schemas, incl
21415 75 64 69 6e 67 20 61 6c 6c 20 74 68 65 20 73 61  uding all the sa
21416 6d 65 20 69 6e 64 69 63 65 73 2c 20 74 68 65 6e  me indices, then
21417 20 61 20 73 70 65 63 69 61 6c 20 6f 70 74 69 6d   a special optim
21418 69 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e  ization.** is in
21419 76 6f 6b 65 64 20 74 68 61 74 20 63 6f 70 69 65  voked that copie
2141a 73 20 72 61 77 20 72 65 63 6f 72 64 73 20 66 72  s raw records fr
2141b 6f 6d 20 3c 74 61 62 6c 65 32 3e 20 6f 76 65 72  om <table2> over
2141c 20 74 6f 20 3c 74 61 62 6c 65 31 3e 2e 0a 2a 2a   to <table1>..**
2141d 20 53 65 65 20 74 68 65 20 78 66 65 72 4f 70 74   See the xferOpt
2141e 69 6d 69 7a 61 74 69 6f 6e 28 29 20 66 75 6e 63  imization() func
2141f 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 69 6d 70  tion for the imp
21420 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
21421 68 69 73 0a 2a 2a 20 74 65 6d 70 6c 61 74 65 2e  his.** template.
21422 20 20 54 68 69 73 20 69 73 20 74 68 65 20 32 6e    This is the 2n
21423 64 20 74 65 6d 70 6c 61 74 65 2e 0a 2a 2a 0a 2a  d template..**.*
21424 2a 20 20 20 20 20 20 20 20 20 6f 70 65 6e 20 61  *         open a
21425 20 77 72 69 74 65 20 63 75 72 73 6f 72 20 74 6f   write cursor to
21426 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20 20   <table>.**     
21427 20 20 20 20 6f 70 65 6e 20 72 65 61 64 20 63 75      open read cu
21428 72 73 6f 72 20 6f 6e 20 3c 74 61 62 6c 65 32 3e  rsor on <table2>
21429 0a 2a 2a 20 20 20 20 20 20 20 20 20 74 72 61 6e  .**         tran
2142a 73 66 65 72 20 61 6c 6c 20 72 65 63 6f 72 64 73  sfer all records
2142b 20 69 6e 20 3c 74 61 62 6c 65 32 3e 20 6f 76 65   in <table2> ove
2142c 72 20 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20  r to <table>.** 
2142d 20 20 20 20 20 20 20 20 63 6c 6f 73 65 20 63 75          close cu
2142e 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20 20 20 20  rsors.**        
2142f 20 66 6f 72 65 61 63 68 20 69 6e 64 65 78 20 6f   foreach index o
21430 6e 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20  n <table>.**    
21431 20 20 20 20 20 20 20 6f 70 65 6e 20 61 20 77 72         open a wr
21432 69 74 65 20 63 75 72 73 6f 72 20 6f 6e 20 74 68  ite cursor on th
21433 65 20 3c 74 61 62 6c 65 3e 20 69 6e 64 65 78 0a  e <table> index.
21434 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6f 70 65  **           ope
21435 6e 20 61 20 72 65 61 64 20 63 75 72 73 6f 72 20  n a read cursor 
21436 6f 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  on the correspon
21437 64 69 6e 67 20 3c 74 61 62 6c 65 32 3e 20 69 6e  ding <table2> in
21438 64 65 78 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  dex.**          
21439 20 74 72 61 6e 73 66 65 72 20 61 6c 6c 20 72 65   transfer all re
2143a 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 65 20 72  cords from the r
2143b 65 61 64 20 74 6f 20 74 68 65 20 77 72 69 74 65  ead to the write
2143c 20 63 75 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20   cursors.**     
2143d 20 20 20 20 20 20 63 6c 6f 73 65 20 63 75 72 73        close curs
2143e 6f 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 65  ors.**         e
2143f 6e 64 20 66 6f 72 65 61 63 68 0a 2a 2a 0a 2a 2a  nd foreach.**.**
21440 20 54 68 65 20 33 72 64 20 74 65 6d 70 6c 61 74   The 3rd templat
21441 65 20 69 73 20 66 6f 72 20 77 68 65 6e 20 74 68  e is for when th
21442 65 20 73 65 63 6f 6e 64 20 74 65 6d 70 6c 61 74  e second templat
21443 65 20 64 6f 65 73 20 6e 6f 74 20 61 70 70 6c 79  e does not apply
21444 0a 2a 2a 20 61 6e 64 20 74 68 65 20 53 45 4c 45  .** and the SELE
21445 43 54 20 63 6c 61 75 73 65 20 64 6f 65 73 20 6e  CT clause does n
21446 6f 74 20 72 65 61 64 20 66 72 6f 6d 20 3c 74 61  ot read from <ta
21447 62 6c 65 3e 20 61 74 20 61 6e 79 20 74 69 6d 65  ble> at any time
21448 2e 0a 2a 2a 20 54 68 65 20 67 65 6e 65 72 61 74  ..** The generat
21449 65 64 20 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20  ed code follows 
2144a 74 68 69 73 20 74 65 6d 70 6c 61 74 65 3a 0a 2a  this template:.*
2144b 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46  *.**         EOF
2144c 20 3c 2d 20 30 0a 2a 2a 20 20 20 20 20 20 20 20   <- 0.**        
2144d 20 58 20 3c 2d 20 41 0a 2a 2a 20 20 20 20 20 20   X <- A.**      
2144e 20 20 20 67 6f 74 6f 20 42 0a 2a 2a 20 20 20 20     goto B.**    
2144f 20 20 41 3a 20 73 65 74 75 70 20 66 6f 72 20 74    A: setup for t
21450 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20  he SELECT.**    
21451 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72 20 74       loop over t
21452 68 65 20 72 6f 77 73 20 69 6e 20 74 68 65 20 53  he rows in the S
21453 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20  ELECT.**        
21454 20 20 20 6c 6f 61 64 20 76 61 6c 75 65 73 20 69     load values i
21455 6e 74 6f 20 72 65 67 69 73 74 65 72 73 20 52 2e  nto registers R.
21456 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  .R+n.**         
21457 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20    yield X.**    
21458 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 2a 2a       end loop.**
21459 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75 70           cleanup
2145a 20 61 66 74 65 72 20 74 68 65 20 53 45 4c 45 43   after the SELEC
2145b 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46  T.**         EOF
2145c 20 3c 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20 20   <- 1.**        
2145d 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20   yield X.**     
2145e 20 20 20 20 67 6f 74 6f 20 41 0a 2a 2a 20 20 20      goto A.**   
2145f 20 20 20 42 3a 20 6f 70 65 6e 20 77 72 69 74 65     B: open write
21460 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c   cursor to <tabl
21461 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64 69 63  e> and its indic
21462 65 73 0a 2a 2a 20 20 20 20 20 20 43 3a 20 79 69  es.**      C: yi
21463 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20  eld X.**        
21464 20 69 66 20 45 4f 46 20 67 6f 74 6f 20 44 0a 2a   if EOF goto D.*
21465 2a 20 20 20 20 20 20 20 20 20 69 6e 73 65 72 74  *         insert
21466 20 74 68 65 20 73 65 6c 65 63 74 20 72 65 73 75   the select resu
21467 6c 74 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 20  lt into <table> 
21468 66 72 6f 6d 20 52 2e 2e 52 2b 6e 0a 2a 2a 20 20  from R..R+n.**  
21469 20 20 20 20 20 20 20 67 6f 74 6f 20 43 0a 2a 2a         goto C.**
2146a 20 20 20 20 20 20 44 3a 20 63 6c 65 61 6e 75 70        D: cleanup
2146b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 34 74 68 20 74  .**.** The 4th t
2146c 65 6d 70 6c 61 74 65 20 69 73 20 75 73 65 64 20  emplate is used 
2146d 69 66 20 74 68 65 20 69 6e 73 65 72 74 20 73 74  if the insert st
2146e 61 74 65 6d 65 6e 74 20 74 61 6b 65 73 20 69 74  atement takes it
2146f 73 0a 2a 2a 20 76 61 6c 75 65 73 20 66 72 6f 6d  s.** values from
21470 20 61 20 53 45 4c 45 43 54 20 62 75 74 20 74 68   a SELECT but th
21471 65 20 64 61 74 61 20 69 73 20 62 65 69 6e 67 20  e data is being 
21472 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 61 20  inserted into a 
21473 74 61 62 6c 65 0a 2a 2a 20 74 68 61 74 20 69 73  table.** that is
21474 20 61 6c 73 6f 20 72 65 61 64 20 61 73 20 70 61   also read as pa
21475 72 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  rt of the SELECT
21476 2e 20 20 49 6e 20 74 68 65 20 74 68 69 72 64 20  .  In the third 
21477 66 6f 72 6d 2c 0a 2a 2a 20 77 65 20 68 61 76 65  form,.** we have
21478 20 74 6f 20 75 73 65 20 61 20 69 6e 74 65 72 6d   to use a interm
21479 65 64 69 61 74 65 20 74 61 62 6c 65 20 74 6f 20  ediate table to 
2147a 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  store the result
2147b 73 20 6f 66 0a 2a 2a 20 74 68 65 20 73 65 6c 65  s of.** the sele
2147c 63 74 2e 20 20 54 68 65 20 74 65 6d 70 6c 61 74  ct.  The templat
2147d 65 20 69 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a  e is like this:.
2147e 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f  **.**         EO
2147f 46 20 3c 2d 20 30 0a 2a 2a 20 20 20 20 20 20 20  F <- 0.**       
21480 20 20 58 20 3c 2d 20 41 0a 2a 2a 20 20 20 20 20    X <- A.**     
21481 20 20 20 20 67 6f 74 6f 20 42 0a 2a 2a 20 20 20      goto B.**   
21482 20 20 20 41 3a 20 73 65 74 75 70 20 66 6f 72 20     A: setup for 
21483 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20  the SELECT.**   
21484 20 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72 20        loop over 
21485 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20 74 68  the tables in th
21486 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20  e SELECT.**     
21487 20 20 20 20 20 20 6c 6f 61 64 20 76 61 6c 75 65        load value
21488 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 52   into register R
21489 2e 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20 20 20 20  ..R+n.**        
2148a 20 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20     yield X.**   
2148b 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 2a        end loop.*
2148c 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75  *         cleanu
2148d 70 20 61 66 74 65 72 20 74 68 65 20 53 45 4c 45  p after the SELE
2148e 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f  CT.**         EO
2148f 46 20 3c 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20  F <- 1.**       
21490 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20    yield X.**    
21491 20 20 20 20 20 68 61 6c 74 2d 65 72 72 6f 72 0a       halt-error.
21492 2a 2a 20 20 20 20 20 20 42 3a 20 6f 70 65 6e 20  **      B: open 
21493 74 65 6d 70 20 74 61 62 6c 65 0a 2a 2a 20 20 20  temp table.**   
21494 20 20 20 4c 3a 20 79 69 65 6c 64 20 58 0a 2a 2a     L: yield X.**
21495 20 20 20 20 20 20 20 20 20 69 66 20 45 4f 46 20           if EOF 
21496 67 6f 74 6f 20 4d 0a 2a 2a 20 20 20 20 20 20 20  goto M.**       
21497 20 20 69 6e 73 65 72 74 20 72 6f 77 20 66 72 6f    insert row fro
21498 6d 20 52 2e 2e 52 2b 6e 20 69 6e 74 6f 20 74 65  m R..R+n into te
21499 6d 70 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20  mp table.**     
2149a 20 20 20 20 67 6f 74 6f 20 4c 0a 2a 2a 20 20 20      goto L.**   
2149b 20 20 20 4d 3a 20 6f 70 65 6e 20 77 72 69 74 65     M: open write
2149c 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c   cursor to <tabl
2149d 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64 69 63  e> and its indic
2149e 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 72 65  es.**         re
2149f 77 69 6e 64 20 74 65 6d 70 20 74 61 62 6c 65 0a  wind temp table.
214a0 2a 2a 20 20 20 20 20 20 43 3a 20 6c 6f 6f 70 20  **      C: loop 
214a1 6f 76 65 72 20 72 6f 77 73 20 6f 66 20 69 6e 74  over rows of int
214a2 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65 0a  ermediate table.
214a3 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 72 61  **           tra
214a4 6e 73 66 65 72 20 76 61 6c 75 65 73 20 66 6f 72  nsfer values for
214a5 6d 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 74  m intermediate t
214a6 61 62 6c 65 20 69 6e 74 6f 20 3c 74 61 62 6c 65  able into <table
214a7 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 65 6e 64  >.**         end
214a8 20 6c 6f 6f 70 0a 2a 2a 20 20 20 20 20 20 44 3a   loop.**      D:
214a9 20 63 6c 65 61 6e 75 70 0a 2a 2f 0a 53 51 4c 49   cleanup.*/.SQLI
214aa 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
214ab 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 0a 20  sqlite3Insert(. 
214ac 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
214ad 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72         /* Parser
214ae 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72   context */.  Sr
214af 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c  cList *pTabList,
214b0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
214b1 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20  able into which 
214b2 77 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67  we are inserting
214b3 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
214b4 70 4c 69 73 74 2c 20 20 20 20 20 20 2f 2a 20 4c  pList,      /* L
214b5 69 73 74 20 6f 66 20 76 61 6c 75 65 73 20 74 6f  ist of values to
214b6 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a   be inserted */.
214b7 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
214b8 74 2c 20 20 20 20 20 20 2f 2a 20 41 20 53 45 4c  t,      /* A SEL
214b9 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  ECT statement to
214ba 20 75 73 65 20 61 73 20 74 68 65 20 64 61 74 61   use as the data
214bb 20 73 6f 75 72 63 65 20 2a 2f 0a 20 20 49 64 4c   source */.  IdL
214bc 69 73 74 20 2a 70 43 6f 6c 75 6d 6e 2c 20 20 20  ist *pColumn,   
214bd 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d     /* Column nam
214be 65 73 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  es corresponding
214bf 20 74 6f 20 49 44 4c 49 53 54 2e 20 2a 2f 0a 20   to IDLIST. */. 
214c0 20 69 6e 74 20 6f 6e 45 72 72 6f 72 20 20 20 20   int onError    
214c1 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f         /* How to
214c2 20 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61 69   handle constrai
214c3 6e 74 20 65 72 72 6f 72 73 20 2a 2f 0a 29 7b 0a  nt errors */.){.
214c4 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
214c5 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d          /* The m
214c6 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 74 72  ain database str
214c7 75 63 74 75 72 65 20 2a 2f 0a 20 20 54 61 62 6c  ucture */.  Tabl
214c8 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20  e *pTab;        
214c9 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74    /* The table t
214ca 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 2e 20 20  o insert into.  
214cb 61 6b 61 20 54 41 42 4c 45 20 2a 2f 0a 20 20 63  aka TABLE */.  c
214cc 68 61 72 20 2a 7a 54 61 62 3b 20 20 20 20 20 20  har *zTab;      
214cd 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
214ce 74 68 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 77  the table into w
214cf 68 69 63 68 20 77 65 20 61 72 65 20 69 6e 73 65  hich we are inse
214d0 72 74 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74  rting */.  const
214d1 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20   char *zDb;     
214d2 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
214d3 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67  database holding
214d4 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20   this table */. 
214d5 20 69 6e 74 20 69 2c 20 6a 2c 20 69 64 78 3b 20   int i, j, idx; 
214d6 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
214d7 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 56 64 62  ounters */.  Vdb
214d8 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20  e *v;           
214d9 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63     /* Generate c
214da 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20 76 69  ode into this vi
214db 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f  rtual machine */
214dc 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20  .  Index *pIdx; 
214dd 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20           /* For 
214de 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69 6e 64  looping over ind
214df 69 63 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c  ices of the tabl
214e0 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75  e */.  int nColu
214e1 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  mn;          /* 
214e2 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  Number of column
214e3 73 20 69 6e 20 74 68 65 20 64 61 74 61 20 2a 2f  s in the data */
214e4 0a 20 20 69 6e 74 20 6e 48 69 64 64 65 6e 20 3d  .  int nHidden =
214e5 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62   0;      /* Numb
214e6 65 72 20 6f 66 20 68 69 64 64 65 6e 20 63 6f 6c  er of hidden col
214e7 75 6d 6e 73 20 69 66 20 54 41 42 4c 45 20 69 73  umns if TABLE is
214e8 20 76 69 72 74 75 61 6c 20 2a 2f 0a 20 20 69 6e   virtual */.  in
214e9 74 20 62 61 73 65 43 75 72 20 3d 20 30 3b 20 20  t baseCur = 0;  
214ea 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75 72 73      /* VDBE Curs
214eb 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 70 54  or number for pT
214ec 61 62 20 2a 2f 0a 20 20 69 6e 74 20 6b 65 79 43  ab */.  int keyC
214ed 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 20 20 20 2f 2a  olumn = -1;   /*
214ee 20 43 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20   Column that is 
214ef 74 68 65 20 49 4e 54 45 47 45 52 20 50 52 49 4d  the INTEGER PRIM
214f0 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 69 6e 74  ARY KEY */.  int
214f1 20 65 6e 64 4f 66 4c 6f 6f 70 3b 20 20 20 20 20   endOfLoop;     
214f2 20 20 20 2f 2a 20 4c 61 62 65 6c 20 66 6f 72 20     /* Label for 
214f3 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69  the end of the i
214f4 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f 70 20 2a 2f  nsertion loop */
214f5 0a 20 20 69 6e 74 20 75 73 65 54 65 6d 70 54 61  .  int useTempTa
214f6 62 6c 65 20 3d 20 30 3b 20 2f 2a 20 53 74 6f 72  ble = 0; /* Stor
214f7 65 20 53 45 4c 45 43 54 20 72 65 73 75 6c 74 73  e SELECT results
214f8 20 69 6e 20 69 6e 74 65 72 6d 65 64 69 61 74 65   in intermediate
214f9 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
214fa 73 72 63 54 61 62 20 3d 20 30 3b 20 20 20 20 20  srcTab = 0;     
214fb 20 20 2f 2a 20 44 61 74 61 20 63 6f 6d 65 73 20    /* Data comes 
214fc 66 72 6f 6d 20 74 68 69 73 20 74 65 6d 70 6f 72  from this tempor
214fd 61 72 79 20 63 75 72 73 6f 72 20 69 66 20 3e 3d  ary cursor if >=
214fe 30 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 49  0 */.  int addrI
214ff 6e 73 54 6f 70 20 3d 20 30 3b 20 20 20 2f 2a 20  nsTop = 0;   /* 
21500 4a 75 6d 70 20 74 6f 20 6c 61 62 65 6c 20 22 44  Jump to label "D
21501 22 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 43  " */.  int addrC
21502 6f 6e 74 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  ont = 0;     /* 
21503 54 6f 70 20 6f 66 20 69 6e 73 65 72 74 20 6c 6f  Top of insert lo
21504 6f 70 2e 20 4c 61 62 65 6c 20 22 43 22 20 69 6e  op. Label "C" in
21505 20 74 65 6d 70 6c 61 74 65 73 20 33 20 61 6e 64   templates 3 and
21506 20 34 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72   4 */.  int addr
21507 53 65 6c 65 63 74 20 3d 20 30 3b 20 20 20 2f 2a  Select = 0;   /*
21508 20 41 64 64 72 65 73 73 20 6f 66 20 63 6f 72 6f   Address of coro
21509 75 74 69 6e 65 20 74 68 61 74 20 69 6d 70 6c 65  utine that imple
2150a 6d 65 6e 74 73 20 74 68 65 20 53 45 4c 45 43 54  ments the SELECT
2150b 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74   */.  SelectDest
2150c 20 64 65 73 74 3b 20 20 20 20 20 20 2f 2a 20 44   dest;      /* D
2150d 65 73 74 69 6e 61 74 69 6f 6e 20 66 6f 72 20 53  estination for S
2150e 45 4c 45 43 54 20 6f 6e 20 72 68 73 20 6f 66 20  ELECT on rhs of 
2150f 49 4e 53 45 52 54 20 2a 2f 0a 20 20 69 6e 74 20  INSERT */.  int 
21510 6e 65 77 49 64 78 20 3d 20 2d 31 3b 20 20 20 20  newIdx = -1;    
21511 20 20 2f 2a 20 43 75 72 73 6f 72 20 66 6f 72 20    /* Cursor for 
21512 74 68 65 20 4e 45 57 20 70 73 65 75 64 6f 2d 74  the NEW pseudo-t
21513 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44  able */.  int iD
21514 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
21515 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64 61 74 61  /* Index of data
21516 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 54 41 42  base holding TAB
21517 4c 45 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62 3b  LE */.  Db *pDb;
21518 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
21519 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
2151a 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 20 62  ntaining table b
2151b 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20 69 6e  eing inserted in
2151c 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 61 70 70 65  to */.  int appe
2151d 6e 64 46 6c 61 67 20 3d 20 30 3b 20 20 20 2f 2a  ndFlag = 0;   /*
2151e 20 54 72 75 65 20 69 66 20 74 68 65 20 69 6e 73   True if the ins
2151f 65 72 74 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f  ert is likely to
21520 20 62 65 20 61 6e 20 61 70 70 65 6e 64 20 2a 2f   be an append */
21521 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20  ..  /* Register 
21522 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20  allocations */. 
21523 20 69 6e 74 20 72 65 67 46 72 6f 6d 53 65 6c 65   int regFromSele
21524 63 74 3b 20 20 20 20 2f 2a 20 42 61 73 65 20 72  ct;    /* Base r
21525 65 67 69 73 74 65 72 20 66 6f 72 20 64 61 74 61  egister for data
21526 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 53 45 4c   coming from SEL
21527 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ECT */.  int reg
21528 41 75 74 6f 69 6e 63 20 3d 20 30 3b 20 20 20 2f  Autoinc = 0;   /
21529 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69  * Register holdi
2152a 6e 67 20 74 68 65 20 41 55 54 4f 49 4e 43 52 45  ng the AUTOINCRE
2152b 4d 45 4e 54 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  MENT counter */.
2152c 20 20 69 6e 74 20 72 65 67 52 6f 77 43 6f 75 6e    int regRowCoun
2152d 74 20 3d 20 30 3b 20 20 2f 2a 20 4d 65 6d 6f 72  t = 0;  /* Memor
2152e 79 20 63 65 6c 6c 20 75 73 65 64 20 66 6f 72 20  y cell used for 
2152f 74 68 65 20 72 6f 77 20 63 6f 75 6e 74 65 72 20  the row counter 
21530 2a 2f 0a 20 20 69 6e 74 20 72 65 67 49 6e 73 3b  */.  int regIns;
21531 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6c             /* Bl
21532 6f 63 6b 20 6f 66 20 72 65 67 73 20 68 6f 6c 64  ock of regs hold
21533 69 6e 67 20 72 6f 77 69 64 2b 64 61 74 61 20 62  ing rowid+data b
21534 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20 2a 2f  eing inserted */
21535 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b  .  int regRowid;
21536 20 20 20 20 20 20 20 20 20 2f 2a 20 72 65 67 69           /* regi
21537 73 74 65 72 73 20 68 6f 6c 64 69 6e 67 20 69 6e  sters holding in
21538 73 65 72 74 20 72 6f 77 69 64 20 2a 2f 0a 20 20  sert rowid */.  
21539 69 6e 74 20 72 65 67 44 61 74 61 3b 20 20 20 20  int regData;    
2153a 20 20 20 20 20 20 2f 2a 20 72 65 67 69 73 74 65        /* registe
2153b 72 20 68 6f 6c 64 69 6e 67 20 66 69 72 73 74 20  r holding first 
2153c 63 6f 6c 75 6d 6e 20 74 6f 20 69 6e 73 65 72 74  column to insert
2153d 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 65 63   */.  int regRec
2153e 6f 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 48  ord;        /* H
2153f 6f 6c 64 73 20 74 68 65 20 61 73 73 65 6d 62 6c  olds the assembl
21540 69 65 64 20 72 6f 77 20 72 65 63 6f 72 64 20 2a  ied row record *
21541 2f 0a 20 20 69 6e 74 20 72 65 67 45 6f 66 3b 20  /.  int regEof; 
21542 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
21543 69 73 74 65 72 20 72 65 63 6f 72 64 69 6e 67 20  ister recording 
21544 65 6e 64 20 6f 66 20 53 45 4c 45 43 54 20 64 61  end of SELECT da
21545 74 61 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65  ta */.  int *aRe
21546 67 49 64 78 20 3d 20 30 3b 20 20 20 20 20 2f 2a  gIdx = 0;     /*
21547 20 4f 6e 65 20 72 65 67 69 73 74 65 72 20 61 6c   One register al
21548 6c 6f 63 61 74 65 64 20 74 6f 20 65 61 63 68 20  located to each 
21549 69 6e 64 65 78 20 2a 2f 0a 0a 0a 23 69 66 6e 64  index */...#ifnd
2154a 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
2154b 52 49 47 47 45 52 0a 20 20 69 6e 74 20 69 73 56  RIGGER.  int isV
2154c 69 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20  iew;            
2154d 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
2154e 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 69 6e  attempting to in
2154f 73 65 72 74 20 69 6e 74 6f 20 61 20 76 69 65 77  sert into a view
21550 20 2a 2f 0a 20 20 69 6e 74 20 74 72 69 67 67 65   */.  int trigge
21551 72 73 5f 65 78 69 73 74 20 3d 20 30 3b 20 20 20  rs_exist = 0;   
21552 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
21553 72 65 20 61 72 65 20 46 4f 52 20 45 41 43 48 20  re are FOR EACH 
21554 52 4f 57 20 74 72 69 67 67 65 72 73 20 2a 2f 0a  ROW triggers */.
21555 23 65 6e 64 69 66 0a 0a 20 20 64 62 20 3d 20 70  #endif..  db = p
21556 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28  Parse->db;.  if(
21557 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c   pParse->nErr ||
21558 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
21559 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e  d ){.    goto in
2155a 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  sert_cleanup;.  
2155b 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74  }..  /* Locate t
2155c 68 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68  he table into wh
2155d 69 63 68 20 77 65 20 77 69 6c 6c 20 62 65 20 69  ich we will be i
2155e 6e 73 65 72 74 69 6e 67 20 6e 65 77 20 69 6e 66  nserting new inf
2155f 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20  ormation..  */. 
21560 20 61 73 73 65 72 74 28 20 70 54 61 62 4c 69 73   assert( pTabLis
21561 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20  t->nSrc==1 );.  
21562 7a 54 61 62 20 3d 20 70 54 61 62 4c 69 73 74 2d  zTab = pTabList-
21563 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 69  >a[0].zName;.  i
21564 66 28 20 7a 54 61 62 3d 3d 30 20 29 20 67 6f 74  f( zTab==0 ) got
21565 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70  o insert_cleanup
21566 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74  ;.  pTab = sqlit
21567 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28  e3SrcListLookup(
21568 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74  pParse, pTabList
21569 29 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30  );.  if( pTab==0
2156a 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73   ){.    goto ins
2156b 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ert_cleanup;.  }
2156c 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  .  iDb = sqlite3
2156d 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62  SchemaToIndex(db
2156e 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29  , pTab->pSchema)
2156f 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3c  ;.  assert( iDb<
21570 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 70 44 62  db->nDb );.  pDb
21571 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d   = &db->aDb[iDb]
21572 3b 0a 20 20 7a 44 62 20 3d 20 70 44 62 2d 3e 7a  ;.  zDb = pDb->z
21573 4e 61 6d 65 3b 0a 20 20 69 66 28 20 73 71 6c 69  Name;.  if( sqli
21574 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
21575 72 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45  rse, SQLITE_INSE
21576 52 54 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  RT, pTab->zName,
21577 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20   0, zDb) ){.    
21578 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61  goto insert_clea
21579 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46  nup;.  }..  /* F
2157a 69 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20  igure out if we 
2157b 68 61 76 65 20 61 6e 79 20 74 72 69 67 67 65 72  have any trigger
2157c 73 20 61 6e 64 20 69 66 20 74 68 65 20 74 61 62  s and if the tab
2157d 6c 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 69 6e  le being.  ** in
2157e 73 65 72 74 65 64 20 69 6e 74 6f 20 69 73 20 61  serted into is a
2157f 20 76 69 65 77 0a 20 20 2a 2f 0a 23 69 66 6e 64   view.  */.#ifnd
21580 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
21581 52 49 47 47 45 52 0a 20 20 74 72 69 67 67 65 72  RIGGER.  trigger
21582 73 5f 65 78 69 73 74 20 3d 20 73 71 6c 69 74 65  s_exist = sqlite
21583 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28 70  3TriggersExist(p
21584 50 61 72 73 65 2c 20 70 54 61 62 2c 20 54 4b 5f  Parse, pTab, TK_
21585 49 4e 53 45 52 54 2c 20 30 29 3b 0a 20 20 69 73  INSERT, 0);.  is
21586 56 69 65 77 20 3d 20 70 54 61 62 2d 3e 70 53 65  View = pTab->pSe
21587 6c 65 63 74 21 3d 30 3b 0a 23 65 6c 73 65 0a 23  lect!=0;.#else.#
21588 20 64 65 66 69 6e 65 20 74 72 69 67 67 65 72 73   define triggers
21589 5f 65 78 69 73 74 20 30 0a 23 20 64 65 66 69 6e  _exist 0.# defin
2158a 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69  e isView 0.#endi
2158b 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
2158c 4f 4d 49 54 5f 56 49 45 57 0a 23 20 75 6e 64 65  OMIT_VIEW.# unde
2158d 66 20 69 73 56 69 65 77 0a 23 20 64 65 66 69 6e  f isView.# defin
2158e 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69  e isView 0.#endi
2158f 66 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 74  f..  /* Ensure t
21590 68 61 74 3a 0a 20 20 2a 20 20 28 61 29 20 74 68  hat:.  *  (a) th
21591 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 72  e table is not r
21592 65 61 64 2d 6f 6e 6c 79 2c 20 0a 20 20 2a 20 20  ead-only, .  *  
21593 28 62 29 20 74 68 61 74 20 69 66 20 69 74 20 69  (b) that if it i
21594 73 20 61 20 76 69 65 77 20 74 68 65 6e 20 4f 4e  s a view then ON
21595 20 49 4e 53 45 52 54 20 74 72 69 67 67 65 72 73   INSERT triggers
21596 20 65 78 69 73 74 0a 20 20 2a 2f 0a 20 20 69 66   exist.  */.  if
21597 28 20 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f  ( sqlite3IsReadO
21598 6e 6c 79 28 70 50 61 72 73 65 2c 20 70 54 61 62  nly(pParse, pTab
21599 2c 20 74 72 69 67 67 65 72 73 5f 65 78 69 73 74  , triggers_exist
2159a 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e  ) ){.    goto in
2159b 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  sert_cleanup;.  
2159c 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  }.  assert( pTab
2159d 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  !=0 );..  /* If 
2159e 70 54 61 62 20 69 73 20 72 65 61 6c 6c 79 20 61  pTab is really a
2159f 20 76 69 65 77 2c 20 6d 61 6b 65 20 73 75 72 65   view, make sure
215a0 20 69 74 20 68 61 73 20 62 65 65 6e 20 69 6e 69   it has been ini
215a1 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2a 20 56  tialized..  ** V
215a2 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65  iewGetColumnName
215a3 73 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69  s() is a no-op i
215a4 66 20 70 54 61 62 20 69 73 20 6e 6f 74 20 61 20  f pTab is not a 
215a5 76 69 65 77 20 28 6f 72 20 76 69 72 74 75 61 6c  view (or virtual
215a6 20 0a 20 20 2a 2a 20 6d 6f 64 75 6c 65 20 74 61   .  ** module ta
215a7 62 6c 65 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ble)..  */.  if(
215a8 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43   sqlite3ViewGetC
215a9 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73  olumnNames(pPars
215aa 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20  e, pTab) ){.    
215ab 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61  goto insert_clea
215ac 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41  nup;.  }..  /* A
215ad 6c 6c 6f 63 61 74 65 20 61 20 56 44 42 45 0a 20  llocate a VDBE. 
215ae 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65   */.  v = sqlite
215af 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
215b0 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67  ;.  if( v==0 ) g
215b1 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e  oto insert_clean
215b2 75 70 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65  up;.  if( pParse
215b3 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 20 73 71  ->nested==0 ) sq
215b4 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68  lite3VdbeCountCh
215b5 61 6e 67 65 73 28 76 29 3b 0a 20 20 73 71 6c 69  anges(v);.  sqli
215b6 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
215b7 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70  ration(pParse, p
215b8 53 65 6c 65 63 74 20 7c 7c 20 74 72 69 67 67 65  Select || trigge
215b9 72 73 5f 65 78 69 73 74 2c 20 69 44 62 29 3b 0a  rs_exist, iDb);.
215ba 0a 20 20 2f 2a 20 69 66 20 74 68 65 72 65 20 61  .  /* if there a
215bb 72 65 20 72 6f 77 20 74 72 69 67 67 65 72 73 2c  re row triggers,
215bc 20 61 6c 6c 6f 63 61 74 65 20 61 20 74 65 6d 70   allocate a temp
215bd 20 74 61 62 6c 65 20 66 6f 72 20 6e 65 77 2e 2a   table for new.*
215be 20 72 65 66 65 72 65 6e 63 65 73 2e 20 2a 2f 0a   references. */.
215bf 20 20 69 66 28 20 74 72 69 67 67 65 72 73 5f 65    if( triggers_e
215c0 78 69 73 74 20 29 7b 0a 20 20 20 20 6e 65 77 49  xist ){.    newI
215c1 64 78 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  dx = pParse->nTa
215c2 62 2b 2b 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65  b++;.  }..#ifnde
215c3 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46  f SQLITE_OMIT_XF
215c4 45 52 5f 4f 50 54 0a 20 20 2f 2a 20 49 66 20 74  ER_OPT.  /* If t
215c5 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  he statement is 
215c6 6f 66 20 74 68 65 20 66 6f 72 6d 0a 20 20 2a 2a  of the form.  **
215c7 0a 20 20 2a 2a 20 20 20 20 20 20 20 49 4e 53 45  .  **       INSE
215c8 52 54 20 49 4e 54 4f 20 3c 74 61 62 6c 65 31 3e  RT INTO <table1>
215c9 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 3c   SELECT * FROM <
215ca 74 61 62 6c 65 32 3e 3b 0a 20 20 2a 2a 0a 20 20  table2>;.  **.  
215cb 2a 2a 20 54 68 65 6e 20 73 70 65 63 69 61 6c 20  ** Then special 
215cc 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 63 61  optimizations ca
215cd 6e 20 62 65 20 61 70 70 6c 69 65 64 20 74 68 61  n be applied tha
215ce 74 20 6d 61 6b 65 20 74 68 65 20 74 72 61 6e 73  t make the trans
215cf 66 65 72 0a 20 20 2a 2a 20 76 65 72 79 20 66 61  fer.  ** very fa
215d0 73 74 20 61 6e 64 20 77 68 69 63 68 20 72 65 64  st and which red
215d1 75 63 65 20 66 72 61 67 6d 65 6e 74 61 74 69 6f  uce fragmentatio
215d2 6e 20 6f 66 20 69 6e 64 69 63 65 73 2e 0a 20 20  n of indices..  
215d3 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20  **.  ** This is 
215d4 74 68 65 20 32 6e 64 20 74 65 6d 70 6c 61 74 65  the 2nd template
215d5 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f  ..  */.  if( pCo
215d6 6c 75 6d 6e 3d 3d 30 20 26 26 20 78 66 65 72 4f  lumn==0 && xferO
215d7 70 74 69 6d 69 7a 61 74 69 6f 6e 28 70 50 61 72  ptimization(pPar
215d8 73 65 2c 20 70 54 61 62 2c 20 70 53 65 6c 65 63  se, pTab, pSelec
215d9 74 2c 20 6f 6e 45 72 72 6f 72 2c 20 69 44 62 29  t, onError, iDb)
215da 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
215db 21 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 20  !triggers_exist 
215dc 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
215dd 4c 69 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 67  List==0 );.    g
215de 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e  oto insert_clean
215df 75 70 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f  up;.  }.#endif /
215e0 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46  * SQLITE_OMIT_XF
215e1 45 52 5f 4f 50 54 20 2a 2f 0a 0a 20 20 2f 2a 20  ER_OPT */..  /* 
215e2 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 41 55  If this is an AU
215e3 54 4f 49 4e 43 52 45 4d 45 4e 54 20 74 61 62 6c  TOINCREMENT tabl
215e4 65 2c 20 6c 6f 6f 6b 20 75 70 20 74 68 65 20 73  e, look up the s
215e5 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 69  equence number i
215e6 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74  n the.  ** sqlit
215e7 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65  e_sequence table
215e8 20 61 6e 64 20 73 74 6f 72 65 20 69 74 20 69 6e   and store it in
215e9 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 72 65 67   memory cell reg
215ea 41 75 74 6f 69 6e 63 2e 0a 20 20 2a 2f 0a 20 20  Autoinc..  */.  
215eb 72 65 67 41 75 74 6f 69 6e 63 20 3d 20 61 75 74  regAutoinc = aut
215ec 6f 49 6e 63 42 65 67 69 6e 28 70 50 61 72 73 65  oIncBegin(pParse
215ed 2c 20 69 44 62 2c 20 70 54 61 62 29 3b 0a 0a 20  , iDb, pTab);.. 
215ee 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68   /* Figure out h
215ef 6f 77 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20  ow many columns 
215f0 6f 66 20 64 61 74 61 20 61 72 65 20 73 75 70 70  of data are supp
215f1 6c 69 65 64 2e 20 20 49 66 20 74 68 65 20 64 61  lied.  If the da
215f2 74 61 0a 20 20 2a 2a 20 69 73 20 63 6f 6d 69 6e  ta.  ** is comin
215f3 67 20 66 72 6f 6d 20 61 20 53 45 4c 45 43 54 20  g from a SELECT 
215f4 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20  statement, then 
215f5 67 65 6e 65 72 61 74 65 20 61 20 63 6f 2d 72 6f  generate a co-ro
215f6 75 74 69 6e 65 20 74 68 61 74 0a 20 20 2a 2a 20  utine that.  ** 
215f7 70 72 6f 64 75 63 65 73 20 61 20 73 69 6e 67 6c  produces a singl
215f8 65 20 72 6f 77 20 6f 66 20 74 68 65 20 53 45 4c  e row of the SEL
215f9 45 43 54 20 6f 6e 20 65 61 63 68 20 69 6e 76 6f  ECT on each invo
215fa 63 61 74 69 6f 6e 2e 20 20 54 68 65 0a 20 20 2a  cation.  The.  *
215fb 2a 20 63 6f 2d 72 6f 75 74 69 6e 65 20 69 73 20  * co-routine is 
215fc 74 68 65 20 63 6f 6d 6d 6f 6e 20 68 65 61 64 65  the common heade
215fd 72 20 74 6f 20 74 68 65 20 33 72 64 20 61 6e 64  r to the 3rd and
215fe 20 34 74 68 20 74 65 6d 70 6c 61 74 65 73 2e 0a   4th templates..
215ff 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 65 6c 65    */.  if( pSele
21600 63 74 20 29 7b 0a 20 20 20 20 2f 2a 20 44 61 74  ct ){.    /* Dat
21601 61 20 69 73 20 63 6f 6d 69 6e 67 20 66 72 6f 6d  a is coming from
21602 20 61 20 53 45 4c 45 43 54 2e 20 20 47 65 6e 65   a SELECT.  Gene
21603 72 61 74 65 20 63 6f 64 65 20 74 6f 20 69 6d 70  rate code to imp
21604 6c 65 6d 65 6e 74 20 74 68 61 74 20 53 45 4c 45  lement that SELE
21605 43 54 0a 20 20 20 20 2a 2a 20 61 73 20 61 20 63  CT.    ** as a c
21606 6f 2d 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 20  o-routine.  The 
21607 63 6f 64 65 20 69 73 20 63 6f 6d 6d 6f 6e 20 74  code is common t
21608 6f 20 62 6f 74 68 20 74 68 65 20 33 72 64 20 61  o both the 3rd a
21609 6e 64 20 34 74 68 0a 20 20 20 20 2a 2a 20 74 65  nd 4th.    ** te
2160a 6d 70 6c 61 74 65 73 3a 0a 20 20 20 20 2a 2a 0a  mplates:.    **.
2160b 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 45      **         E
2160c 4f 46 20 3c 2d 20 30 0a 20 20 20 20 2a 2a 20 20  OF <- 0.    **  
2160d 20 20 20 20 20 20 20 58 20 3c 2d 20 41 0a 20 20         X <- A.  
2160e 20 20 2a 2a 20 20 20 20 20 20 20 20 20 67 6f 74    **         got
2160f 6f 20 42 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  o B.    **      
21610 41 3a 20 73 65 74 75 70 20 66 6f 72 20 74 68 65  A: setup for the
21611 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 20   SELECT.    **  
21612 20 20 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72         loop over
21613 20 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20 74   the tables in t
21614 68 65 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a  he SELECT.    **
21615 20 20 20 20 20 20 20 20 20 20 20 6c 6f 61 64 20             load 
21616 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 73  value into regis
21617 74 65 72 20 52 2e 2e 52 2b 6e 0a 20 20 20 20 2a  ter R..R+n.    *
21618 2a 20 20 20 20 20 20 20 20 20 20 20 79 69 65 6c  *           yiel
21619 64 20 58 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  d X.    **      
2161a 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 20 20 20 20     end loop.    
2161b 2a 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e  **         clean
2161c 75 70 20 61 66 74 65 72 20 74 68 65 20 53 45 4c  up after the SEL
2161d 45 43 54 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  ECT.    **      
2161e 20 20 20 45 4f 46 20 3c 2d 20 31 0a 20 20 20 20     EOF <- 1.    
2161f 2a 2a 20 20 20 20 20 20 20 20 20 79 69 65 6c 64  **         yield
21620 20 58 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20   X.    **       
21621 20 20 68 61 6c 74 2d 65 72 72 6f 72 0a 20 20 20    halt-error.   
21622 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f 6e 20 65 61   **.    ** On ea
21623 63 68 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66  ch invocation of
21624 20 74 68 65 20 63 6f 2d 72 6f 75 74 69 6e 65 2c   the co-routine,
21625 20 69 74 20 70 75 74 73 20 61 20 73 69 6e 67 6c   it puts a singl
21626 65 20 72 6f 77 20 6f 66 20 74 68 65 0a 20 20 20  e row of the.   
21627 20 2a 2a 20 53 45 4c 45 43 54 20 72 65 73 75 6c   ** SELECT resul
21628 74 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 73  t into registers
21629 20 64 65 73 74 2e 69 4d 65 6d 2e 2e 2e 64 65 73   dest.iMem...des
2162a 74 2e 69 4d 65 6d 2b 64 65 73 74 2e 6e 4d 65 6d  t.iMem+dest.nMem
2162b 2d 31 2e 0a 20 20 20 20 2a 2a 20 28 54 68 65 73  -1..    ** (Thes
2162c 65 20 6f 75 74 70 75 74 20 72 65 67 69 73 74 65  e output registe
2162d 72 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64  rs are allocated
2162e 20 62 79 20 73 71 6c 69 74 65 33 53 65 6c 65 63   by sqlite3Selec
2162f 74 28 29 2e 29 20 20 57 68 65 6e 0a 20 20 20 20  t().)  When.    
21630 2a 2a 20 74 68 65 20 53 45 4c 45 43 54 20 63 6f  ** the SELECT co
21631 6d 70 6c 65 74 65 73 2c 20 69 74 20 73 65 74 73  mpletes, it sets
21632 20 74 68 65 20 45 4f 46 20 66 6c 61 67 20 73 74   the EOF flag st
21633 6f 72 65 64 20 69 6e 20 72 65 67 45 6f 66 2e 0a  ored in regEof..
21634 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72      */.    int r
21635 63 2c 20 6a 31 3b 0a 0a 20 20 20 20 72 65 67 45  c, j1;..    regE
21636 6f 66 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  of = ++pParse->n
21637 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Mem;.    sqlite3
21638 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
21639 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67  _Integer, 0, reg
2163a 45 6f 66 29 3b 20 20 20 20 20 20 2f 2a 20 45 4f  Eof);      /* EO
2163b 46 20 3c 2d 20 30 20 2a 2f 0a 20 20 20 20 56 64  F <- 0 */.    Vd
2163c 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 53  beComment((v, "S
2163d 45 4c 45 43 54 20 65 6f 66 20 66 6c 61 67 22 29  ELECT eof flag")
2163e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  );.    sqlite3Se
2163f 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65  lectDestInit(&de
21640 73 74 2c 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e  st, SRT_Coroutin
21641 65 2c 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65  e, ++pParse->nMe
21642 6d 29 3b 0a 20 20 20 20 61 64 64 72 53 65 6c 65  m);.    addrSele
21643 63 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  ct = sqlite3Vdbe
21644 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 2b 32  CurrentAddr(v)+2
21645 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
21646 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
21647 74 65 67 65 72 2c 20 61 64 64 72 53 65 6c 65 63  teger, addrSelec
21648 74 2d 31 2c 20 64 65 73 74 2e 69 50 61 72 6d 29  t-1, dest.iParm)
21649 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74  ;.    j1 = sqlit
2164a 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2164b 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30 29 3b 0a  OP_Goto, 0, 0);.
2164c 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
2164d 28 76 2c 20 22 4a 75 6d 70 20 6f 76 65 72 20 53  (v, "Jump over S
2164e 45 4c 45 43 54 20 63 6f 72 6f 75 74 69 6e 65 22  ELECT coroutine"
2164f 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f  ));..    /* Reso
21650 6c 76 65 20 74 68 65 20 65 78 70 72 65 73 73 69  lve the expressi
21651 6f 6e 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43  ons in the SELEC
21652 54 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20  T statement and 
21653 65 78 65 63 75 74 65 20 69 74 2e 20 2a 2f 0a 20  execute it. */. 
21654 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53     rc = sqlite3S
21655 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53  elect(pParse, pS
21656 65 6c 65 63 74 2c 20 26 64 65 73 74 29 3b 0a 20  elect, &dest);. 
21657 20 20 20 69 66 28 20 72 63 20 7c 7c 20 70 50 61     if( rc || pPa
21658 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d  rse->nErr || db-
21659 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
2165a 0a 20 20 20 20 20 20 67 6f 74 6f 20 69 6e 73 65  .      goto inse
2165b 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  rt_cleanup;.    
2165c 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  }.    sqlite3Vdb
2165d 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
2165e 74 65 67 65 72 2c 20 31 2c 20 72 65 67 45 6f 66  teger, 1, regEof
2165f 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 45 4f  );         /* EO
21660 46 20 3c 2d 20 31 20 2a 2f 0a 20 20 20 20 73 71  F <- 1 */.    sq
21661 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
21662 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 64 65 73  v, OP_Yield, des
21663 74 2e 69 50 61 72 6d 29 3b 20 20 20 2f 2a 20 79  t.iParm);   /* y
21664 69 65 6c 64 20 58 20 2a 2f 0a 20 20 20 20 73 71  ield X */.    sq
21665 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
21666 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49  v, OP_Halt, SQLI
21667 54 45 5f 49 4e 54 45 52 4e 41 4c 2c 20 4f 45 5f  TE_INTERNAL, OE_
21668 41 62 6f 72 74 29 3b 0a 20 20 20 20 56 64 62 65  Abort);.    Vdbe
21669 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 45 6e 64  Comment((v, "End
2166a 20 6f 66 20 53 45 4c 45 43 54 20 63 6f 72 6f 75   of SELECT corou
2166b 74 69 6e 65 22 29 29 3b 0a 20 20 20 20 73 71 6c  tine"));.    sql
2166c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
2166d 28 76 2c 20 6a 31 29 3b 20 20 20 20 20 20 20 20  (v, j1);        
2166e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2166f 20 20 2f 2a 20 6c 61 62 65 6c 20 42 3a 20 2a 2f    /* label B: */
21670 0a 0a 20 20 20 20 72 65 67 46 72 6f 6d 53 65 6c  ..    regFromSel
21671 65 63 74 20 3d 20 64 65 73 74 2e 69 4d 65 6d 3b  ect = dest.iMem;
21672 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 53 65  .    assert( pSe
21673 6c 65 63 74 2d 3e 70 45 4c 69 73 74 20 29 3b 0a  lect->pEList );.
21674 20 20 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 53      nColumn = pS
21675 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e  elect->pEList->n
21676 45 78 70 72 3b 0a 20 20 20 20 61 73 73 65 72 74  Expr;.    assert
21677 28 20 64 65 73 74 2e 6e 4d 65 6d 3d 3d 6e 43 6f  ( dest.nMem==nCo
21678 6c 75 6d 6e 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  lumn );..    /* 
21679 53 65 74 20 75 73 65 54 65 6d 70 54 61 62 6c 65  Set useTempTable
2167a 20 74 6f 20 54 52 55 45 20 69 66 20 74 68 65 20   to TRUE if the 
2167b 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 53 45  result of the SE
2167c 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 20  LECT statement. 
2167d 20 20 20 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20     ** should be 
2167e 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 74  written into a t
2167f 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 28  emporary table (
21680 74 65 6d 70 6c 61 74 65 20 34 29 2e 20 20 53 65  template 4).  Se
21681 74 20 74 6f 0a 20 20 20 20 2a 2a 20 46 41 4c 53  t to.    ** FALS
21682 45 20 69 66 20 65 61 63 68 2a 20 72 6f 77 20 6f  E if each* row o
21683 66 20 74 68 65 20 53 45 4c 45 43 54 20 63 61 6e  f the SELECT can
21684 20 62 65 20 77 72 69 74 74 65 6e 20 64 69 72 65   be written dire
21685 63 74 6c 79 20 69 6e 74 6f 0a 20 20 20 20 2a 2a  ctly into.    **
21686 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
21687 20 74 61 62 6c 65 20 28 74 65 6d 70 6c 61 74 65   table (template
21688 20 33 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20   3)..    **.    
21689 2a 2a 20 41 20 74 65 6d 70 20 74 61 62 6c 65 20  ** A temp table 
2168a 6d 75 73 74 20 62 65 20 75 73 65 64 20 69 66 20  must be used if 
2168b 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20  the table being 
2168c 75 70 64 61 74 65 64 20 69 73 20 61 6c 73 6f 20  updated is also 
2168d 6f 6e 65 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68  one.    ** of th
2168e 65 20 74 61 62 6c 65 73 20 62 65 69 6e 67 20 72  e tables being r
2168f 65 61 64 20 62 79 20 74 68 65 20 53 45 4c 45 43  ead by the SELEC
21690 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41 6c  T statement.  Al
21691 73 6f 20 75 73 65 20 61 20 0a 20 20 20 20 2a 2a  so use a .    **
21692 20 74 65 6d 70 20 74 61 62 6c 65 20 69 6e 20 74   temp table in t
21693 68 65 20 63 61 73 65 20 6f 66 20 72 6f 77 20 74  he case of row t
21694 72 69 67 67 65 72 73 2e 0a 20 20 20 20 2a 2f 0a  riggers..    */.
21695 20 20 20 20 69 66 28 20 74 72 69 67 67 65 72 73      if( triggers
21696 5f 65 78 69 73 74 20 7c 7c 20 72 65 61 64 73 54  _exist || readsT
21697 61 62 6c 65 28 76 2c 20 61 64 64 72 53 65 6c 65  able(v, addrSele
21698 63 74 2c 20 69 44 62 2c 20 70 54 61 62 29 20 29  ct, iDb, pTab) )
21699 7b 0a 20 20 20 20 20 20 75 73 65 54 65 6d 70 54  {.      useTempT
2169a 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  able = 1;.    }.
2169b 0a 20 20 20 20 69 66 28 20 75 73 65 54 65 6d 70  .    if( useTemp
2169c 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 2f  Table ){.      /
2169d 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 63 6f 72  * Invoke the cor
2169e 6f 75 74 69 6e 65 20 74 6f 20 65 78 74 72 61 63  outine to extrac
2169f 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72  t information fr
216a0 6f 6d 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20  om the SELECT.  
216a1 20 20 20 20 2a 2a 20 61 6e 64 20 61 64 64 20 69      ** and add i
216a2 74 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74  t to a transient
216a3 20 74 61 62 6c 65 20 73 72 63 54 61 62 2e 20 20   table srcTab.  
216a4 54 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74  The code generat
216a5 65 64 0a 20 20 20 20 20 20 2a 2a 20 68 65 72 65  ed.      ** here
216a6 20 69 73 20 66 72 6f 6d 20 74 68 65 20 34 74 68   is from the 4th
216a7 20 74 65 6d 70 6c 61 74 65 3a 0a 20 20 20 20 20   template:.     
216a8 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20   **.      **    
216a9 20 20 42 3a 20 6f 70 65 6e 20 74 65 6d 70 20 74    B: open temp t
216aa 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 20 20  able.      **   
216ab 20 20 20 4c 3a 20 79 69 65 6c 64 20 58 0a 20 20     L: yield X.  
216ac 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 69      **         i
216ad 66 20 45 4f 46 20 67 6f 74 6f 20 4d 0a 20 20 20  f EOF goto M.   
216ae 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 69 6e     **         in
216af 73 65 72 74 20 72 6f 77 20 66 72 6f 6d 20 52 2e  sert row from R.
216b0 2e 52 2b 6e 20 69 6e 74 6f 20 74 65 6d 70 20 74  .R+n into temp t
216b1 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 20 20  able.      **   
216b2 20 20 20 20 20 20 67 6f 74 6f 20 4c 0a 20 20 20        goto L.   
216b3 20 20 20 2a 2a 20 20 20 20 20 20 4d 3a 20 2e 2e     **      M: ..
216b4 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
216b5 20 69 6e 74 20 72 65 67 52 65 63 3b 20 20 20 20   int regRec;    
216b6 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f    /* Register to
216b7 20 68 6f 6c 64 20 70 61 63 6b 65 64 20 72 65 63   hold packed rec
216b8 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ord */.      int
216b9 20 72 65 67 52 6f 77 69 64 3b 20 20 20 20 2f 2a   regRowid;    /*
216ba 20 52 65 67 69 73 74 65 72 20 74 6f 20 68 6f 6c   Register to hol
216bb 64 20 74 65 6d 70 20 74 61 62 6c 65 20 52 4f 57  d temp table ROW
216bc 49 44 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  ID */.      int 
216bd 61 64 64 72 54 6f 70 3b 20 20 20 20 20 2f 2a 20  addrTop;     /* 
216be 4c 61 62 65 6c 20 22 4c 22 20 2a 2f 0a 20 20 20  Label "L" */.   
216bf 20 20 20 69 6e 74 20 61 64 64 72 49 66 3b 20 20     int addrIf;  
216c0 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
216c1 66 20 6a 75 6d 70 20 74 6f 20 4d 20 2a 2f 0a 0a  f jump to M */..
216c2 20 20 20 20 20 20 73 72 63 54 61 62 20 3d 20 70        srcTab = p
216c3 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
216c4 20 20 20 20 20 72 65 67 52 65 63 20 3d 20 73 71       regRec = sq
216c5 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
216c6 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 72  pParse);.      r
216c7 65 67 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65  egRowid = sqlite
216c8 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
216c9 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  se);.      sqlit
216ca 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
216cb 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
216cc 2c 20 73 72 63 54 61 62 2c 20 6e 43 6f 6c 75 6d  , srcTab, nColum
216cd 6e 29 3b 0a 20 20 20 20 20 20 61 64 64 72 54 6f  n);.      addrTo
216ce 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  p = sqlite3VdbeA
216cf 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c  ddOp1(v, OP_Yiel
216d0 64 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a  d, dest.iParm);.
216d1 20 20 20 20 20 20 61 64 64 72 49 66 20 3d 20 73        addrIf = s
216d2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
216d3 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f  (v, OP_If, regEo
216d4 66 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  f);.      sqlite
216d5 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
216d6 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65  P_MakeRecord, re
216d7 67 46 72 6f 6d 53 65 6c 65 63 74 2c 20 6e 43 6f  gFromSelect, nCo
216d8 6c 75 6d 6e 2c 20 72 65 67 52 65 63 29 3b 0a 20  lumn, regRec);. 
216d9 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
216da 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77  AddOp2(v, OP_New
216db 52 6f 77 69 64 2c 20 73 72 63 54 61 62 2c 20 72  Rowid, srcTab, r
216dc 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20  egRowid);.      
216dd 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
216de 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20  3(v, OP_Insert, 
216df 73 72 63 54 61 62 2c 20 72 65 67 52 65 63 2c 20  srcTab, regRec, 
216e0 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  regRowid);.     
216e1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
216e2 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
216e3 2c 20 61 64 64 72 54 6f 70 29 3b 0a 20 20 20 20  , addrTop);.    
216e4 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
216e5 70 48 65 72 65 28 76 2c 20 61 64 64 72 49 66 29  pHere(v, addrIf)
216e6 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  ;.      sqlite3R
216e7 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
216e8 61 72 73 65 2c 20 72 65 67 52 65 63 29 3b 0a 20  arse, regRec);. 
216e9 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
216ea 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
216eb 65 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  e, regRowid);.  
216ec 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
216ed 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 20   /* This is the 
216ee 63 61 73 65 20 69 66 20 74 68 65 20 64 61 74 61  case if the data
216ef 20 66 6f 72 20 74 68 65 20 49 4e 53 45 52 54 20   for the INSERT 
216f0 69 73 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 61  is coming from a
216f1 20 56 41 4c 55 45 53 0a 20 20 20 20 2a 2a 20 63   VALUES.    ** c
216f2 6c 61 75 73 65 0a 20 20 20 20 2a 2f 0a 20 20 20  lause.    */.   
216f3 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43   NameContext sNC
216f4 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 4e  ;.    memset(&sN
216f5 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43  C, 0, sizeof(sNC
216f6 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72  ));.    sNC.pPar
216f7 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20  se = pParse;.   
216f8 20 73 72 63 54 61 62 20 3d 20 2d 31 3b 0a 20 20   srcTab = -1;.  
216f9 20 20 61 73 73 65 72 74 28 20 75 73 65 54 65 6d    assert( useTem
216fa 70 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20  pTable==0 );.   
216fb 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 4c 69 73 74   nColumn = pList
216fc 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20   ? pList->nExpr 
216fd 3a 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  : 0;.    for(i=0
216fe 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b  ; i<nColumn; i++
216ff 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  ){.      if( sql
21700 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e  ite3ResolveExprN
21701 61 6d 65 73 28 26 73 4e 43 2c 20 70 4c 69 73 74  ames(&sNC, pList
21702 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20 29 7b  ->a[i].pExpr) ){
21703 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 69 6e  .        goto in
21704 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  sert_cleanup;.  
21705 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
21706 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20  .  /* Make sure 
21707 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  the number of co
21708 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 73 6f 75  lumns in the sou
21709 72 63 65 20 64 61 74 61 20 6d 61 74 63 68 65 73  rce data matches
2170a 20 74 68 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a   the number.  **
2170b 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 62   of columns to b
2170c 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20  e inserted into 
2170d 74 68 65 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a  the table..  */.
2170e 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
2170f 70 54 61 62 29 20 29 7b 0a 20 20 20 20 66 6f 72  pTab) ){.    for
21710 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43  (i=0; i<pTab->nC
21711 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
21712 6e 48 69 64 64 65 6e 20 2b 3d 20 28 49 73 48 69  nHidden += (IsHi
21713 64 64 65 6e 43 6f 6c 75 6d 6e 28 26 70 54 61 62  ddenColumn(&pTab
21714 2d 3e 61 43 6f 6c 5b 69 5d 29 20 3f 20 31 20 3a  ->aCol[i]) ? 1 :
21715 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20   0);.    }.  }. 
21716 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20   if( pColumn==0 
21717 26 26 20 6e 43 6f 6c 75 6d 6e 20 26 26 20 6e 43  && nColumn && nC
21718 6f 6c 75 6d 6e 21 3d 28 70 54 61 62 2d 3e 6e 43  olumn!=(pTab->nC
21719 6f 6c 2d 6e 48 69 64 64 65 6e 29 20 29 7b 0a 20  ol-nHidden) ){. 
2171a 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2171b 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  sg(pParse, .    
2171c 20 20 20 22 74 61 62 6c 65 20 25 53 20 68 61 73     "table %S has
2171d 20 25 64 20 63 6f 6c 75 6d 6e 73 20 62 75 74 20   %d columns but 
2171e 25 64 20 76 61 6c 75 65 73 20 77 65 72 65 20 73  %d values were s
2171f 75 70 70 6c 69 65 64 22 2c 0a 20 20 20 20 20 20  upplied",.      
21720 20 70 54 61 62 4c 69 73 74 2c 20 30 2c 20 70 54   pTabList, 0, pT
21721 61 62 2d 3e 6e 43 6f 6c 2c 20 6e 43 6f 6c 75 6d  ab->nCol, nColum
21722 6e 29 3b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73  n);.    goto ins
21723 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ert_cleanup;.  }
21724 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 21 3d  .  if( pColumn!=
21725 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 21 3d 70 43  0 && nColumn!=pC
21726 6f 6c 75 6d 6e 2d 3e 6e 49 64 20 29 7b 0a 20 20  olumn->nId ){.  
21727 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
21728 67 28 70 50 61 72 73 65 2c 20 22 25 64 20 76 61  g(pParse, "%d va
21729 6c 75 65 73 20 66 6f 72 20 25 64 20 63 6f 6c 75  lues for %d colu
2172a 6d 6e 73 22 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 70  mns", nColumn, p
2172b 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 29 3b 0a 20 20  Column->nId);.  
2172c 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c    goto insert_cl
2172d 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  eanup;.  }..  /*
2172e 20 49 66 20 74 68 65 20 49 4e 53 45 52 54 20 73   If the INSERT s
2172f 74 61 74 65 6d 65 6e 74 20 69 6e 63 6c 75 64 65  tatement include
21730 64 20 61 6e 20 49 44 4c 49 53 54 20 74 65 72 6d  d an IDLIST term
21731 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73 75 72 65  , then make sure
21732 0a 20 20 2a 2a 20 61 6c 6c 20 65 6c 65 6d 65 6e  .  ** all elemen
21733 74 73 20 6f 66 20 74 68 65 20 49 44 4c 49 53 54  ts of the IDLIST
21734 20 72 65 61 6c 6c 79 20 61 72 65 20 63 6f 6c 75   really are colu
21735 6d 6e 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65  mns of the table
21736 20 61 6e 64 20 0a 20 20 2a 2a 20 72 65 6d 65 6d   and .  ** remem
21737 62 65 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69  ber the column i
21738 6e 64 69 63 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a  ndices..  **.  *
21739 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 68  * If the table h
2173a 61 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52  as an INTEGER PR
2173b 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e  IMARY KEY column
2173c 20 61 6e 64 20 74 68 61 74 20 63 6f 6c 75 6d 6e   and that column
2173d 0a 20 20 2a 2a 20 69 73 20 6e 61 6d 65 64 20 69  .  ** is named i
2173e 6e 20 74 68 65 20 49 44 4c 49 53 54 2c 20 74 68  n the IDLIST, th
2173f 65 6e 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65  en record in the
21740 20 6b 65 79 43 6f 6c 75 6d 6e 20 76 61 72 69 61   keyColumn varia
21741 62 6c 65 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64  ble.  ** the ind
21742 65 78 20 69 6e 74 6f 20 49 44 4c 49 53 54 20 6f  ex into IDLIST o
21743 66 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65  f the primary ke
21744 79 20 63 6f 6c 75 6d 6e 2e 20 20 6b 65 79 43 6f  y column.  keyCo
21745 6c 75 6d 6e 20 69 73 0a 20 20 2a 2a 20 74 68 65  lumn is.  ** the
21746 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 70 72   index of the pr
21747 69 6d 61 72 79 20 6b 65 79 20 61 73 20 69 74 20  imary key as it 
21748 61 70 70 65 61 72 73 20 69 6e 20 49 44 4c 49 53  appears in IDLIS
21749 54 2c 20 6e 6f 74 20 61 73 0a 20 20 2a 2a 20 69  T, not as.  ** i
2174a 73 20 61 70 70 65 61 72 73 20 69 6e 20 74 68 65  s appears in the
2174b 20 6f 72 69 67 69 6e 61 6c 20 74 61 62 6c 65 2e   original table.
2174c 20 20 28 54 68 65 20 69 6e 64 65 78 20 6f 66 20    (The index of 
2174d 74 68 65 20 70 72 69 6d 61 72 79 0a 20 20 2a 2a  the primary.  **
2174e 20 6b 65 79 20 69 6e 20 74 68 65 20 6f 72 69 67   key in the orig
2174f 69 6e 61 6c 20 74 61 62 6c 65 20 69 73 20 70 54  inal table is pT
21750 61 62 2d 3e 69 50 4b 65 79 2e 29 0a 20 20 2a 2f  ab->iPKey.).  */
21751 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 20 29  .  if( pColumn )
21752 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  {.    for(i=0; i
21753 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 69  <pColumn->nId; i
21754 2b 2b 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 75  ++){.      pColu
21755 6d 6e 2d 3e 61 5b 69 5d 2e 69 64 78 20 3d 20 2d  mn->a[i].idx = -
21756 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  1;.    }.    for
21757 28 69 3d 30 3b 20 69 3c 70 43 6f 6c 75 6d 6e 2d  (i=0; i<pColumn-
21758 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nId; i++){.    
21759 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 54 61    for(j=0; j<pTa
2175a 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20  b->nCol; j++){. 
2175b 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
2175c 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 75 6d  e3StrICmp(pColum
2175d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 70  n->a[i].zName, p
2175e 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61  Tab->aCol[j].zNa
2175f 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  me)==0 ){.      
21760 20 20 20 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69      pColumn->a[i
21761 5d 2e 69 64 78 20 3d 20 6a 3b 0a 20 20 20 20 20  ].idx = j;.     
21762 20 20 20 20 20 69 66 28 20 6a 3d 3d 70 54 61 62       if( j==pTab
21763 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20  ->iPKey ){.     
21764 20 20 20 20 20 20 20 6b 65 79 43 6f 6c 75 6d 6e         keyColumn
21765 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20   = i;.          
21766 7d 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  }.          brea
21767 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
21768 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6a     }.      if( j
21769 3e 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a  >=pTab->nCol ){.
2176a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
2176b 74 65 33 49 73 52 6f 77 69 64 28 70 43 6f 6c 75  te3IsRowid(pColu
2176c 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 20  mn->a[i].zName) 
2176d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6b 65 79  ){.          key
2176e 43 6f 6c 75 6d 6e 20 3d 20 69 3b 0a 20 20 20 20  Column = i;.    
2176f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
21770 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
21771 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61  rMsg(pParse, "ta
21772 62 6c 65 20 25 53 20 68 61 73 20 6e 6f 20 63 6f  ble %S has no co
21773 6c 75 6d 6e 20 6e 61 6d 65 64 20 25 73 22 2c 0a  lumn named %s",.
21774 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54                pT
21775 61 62 4c 69 73 74 2c 20 30 2c 20 70 43 6f 6c 75  abList, 0, pColu
21776 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b  mn->a[i].zName);
21777 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73  .          pPars
21778 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20  e->nErr++;.     
21779 20 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74       goto insert
2177a 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20  _cleanup;.      
2177b 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
2177c 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  }.  }..  /* If t
2177d 68 65 72 65 20 69 73 20 6e 6f 20 49 44 4c 49 53  here is no IDLIS
2177e 54 20 74 65 72 6d 20 62 75 74 20 74 68 65 20 74  T term but the t
2177f 61 62 6c 65 20 68 61 73 20 61 6e 20 69 6e 74 65  able has an inte
21780 67 65 72 20 70 72 69 6d 61 72 79 0a 20 20 2a 2a  ger primary.  **
21781 20 6b 65 79 2c 20 74 68 65 20 73 65 74 20 74 68   key, the set th
21782 65 20 6b 65 79 43 6f 6c 75 6d 6e 20 76 61 72 69  e keyColumn vari
21783 61 62 6c 65 20 74 6f 20 74 68 65 20 70 72 69 6d  able to the prim
21784 61 72 79 20 6b 65 79 20 63 6f 6c 75 6d 6e 20 69  ary key column i
21785 6e 64 65 78 0a 20 20 2a 2a 20 69 6e 20 74 68 65  ndex.  ** in the
21786 20 6f 72 69 67 69 6e 61 6c 20 74 61 62 6c 65 20   original table 
21787 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 20 20 2a 2f  definition..  */
21788 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d  .  if( pColumn==
21789 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 3e 30 20 29  0 && nColumn>0 )
2178a 7b 0a 20 20 20 20 6b 65 79 43 6f 6c 75 6d 6e 20  {.    keyColumn 
2178b 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20  = pTab->iPKey;. 
2178c 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68   }..  /* Open th
2178d 65 20 74 65 6d 70 20 74 61 62 6c 65 20 66 6f 72  e temp table for
2178e 20 46 4f 52 20 45 41 43 48 20 52 4f 57 20 74 72   FOR EACH ROW tr
2178f 69 67 67 65 72 73 0a 20 20 2a 2f 0a 20 20 69 66  iggers.  */.  if
21790 28 20 74 72 69 67 67 65 72 73 5f 65 78 69 73 74  ( triggers_exist
21791 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
21792 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
21793 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30  SetNumColumns, 0
21794 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20  , pTab->nCol);. 
21795 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21796 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 50  dOp2(v, OP_OpenP
21797 73 65 75 64 6f 2c 20 6e 65 77 49 64 78 2c 20 30  seudo, newIdx, 0
21798 29 3b 0a 20 20 7d 0a 20 20 20 20 0a 20 20 2f 2a  );.  }.    .  /*
21799 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   Initialize the 
2179a 63 6f 75 6e 74 20 6f 66 20 72 6f 77 73 20 74 6f  count of rows to
2179b 20 62 65 20 69 6e 73 65 72 74 65 64 0a 20 20 2a   be inserted.  *
2179c 2f 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67  /.  if( db->flag
2179d 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74  s & SQLITE_Count
2179e 52 6f 77 73 20 29 7b 0a 20 20 20 20 72 65 67 52  Rows ){.    regR
2179f 6f 77 43 6f 75 6e 74 20 3d 20 2b 2b 70 50 61 72  owCount = ++pPar
217a0 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71  se->nMem;.    sq
217a1 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
217a2 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30  v, OP_Integer, 0
217a3 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74 29 3b 0a  , regRowCount);.
217a4 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69    }..  /* If thi
217a5 73 20 69 73 20 6e 6f 74 20 61 20 76 69 65 77 2c  s is not a view,
217a6 20 6f 70 65 6e 20 74 68 65 20 74 61 62 6c 65 20   open the table 
217a7 61 6e 64 20 61 6e 64 20 61 6c 6c 20 69 6e 64 69  and and all indi
217a8 63 65 73 20 2a 2f 0a 20 20 69 66 28 20 21 69 73  ces */.  if( !is
217a9 56 69 65 77 20 29 7b 0a 20 20 20 20 69 6e 74 20  View ){.    int 
217aa 6e 49 64 78 3b 0a 20 20 20 20 69 6e 74 20 69 3b  nIdx;.    int i;
217ab 0a 0a 20 20 20 20 62 61 73 65 43 75 72 20 3d 20  ..    baseCur = 
217ac 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a 20 20  pParse->nTab;.  
217ad 20 20 6e 49 64 78 20 3d 20 73 71 6c 69 74 65 33    nIdx = sqlite3
217ae 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69  OpenTableAndIndi
217af 63 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62  ces(pParse, pTab
217b0 2c 20 62 61 73 65 43 75 72 2c 20 4f 50 5f 4f 70  , baseCur, OP_Op
217b1 65 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 61 52  enWrite);.    aR
217b2 65 67 49 64 78 20 3d 20 73 71 6c 69 74 65 33 44  egIdx = sqlite3D
217b3 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73  bMallocRaw(db, s
217b4 69 7a 65 6f 66 28 69 6e 74 29 2a 28 6e 49 64 78  izeof(int)*(nIdx
217b5 2b 31 29 29 3b 0a 20 20 20 20 69 66 28 20 61 52  +1));.    if( aR
217b6 65 67 49 64 78 3d 3d 30 20 29 7b 0a 20 20 20 20  egIdx==0 ){.    
217b7 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c    goto insert_cl
217b8 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20  eanup;.    }.   
217b9 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 49 64 78   for(i=0; i<nIdx
217ba 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 52  ; i++){.      aR
217bb 65 67 49 64 78 5b 69 5d 20 3d 20 2b 2b 70 50 61  egIdx[i] = ++pPa
217bc 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 7d  rse->nMem;.    }
217bd 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73 20  .  }..  /* This 
217be 69 73 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  is the top of th
217bf 65 20 6d 61 69 6e 20 69 6e 73 65 72 74 69 6f 6e  e main insertion
217c0 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 66 28 20 75   loop */.  if( u
217c1 73 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20  seTempTable ){. 
217c2 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b     /* This block
217c3 20 63 6f 64 65 73 20 74 68 65 20 74 6f 70 20 6f   codes the top o
217c4 66 20 6c 6f 6f 70 20 6f 6e 6c 79 2e 20 20 54 68  f loop only.  Th
217c5 65 20 63 6f 6d 70 6c 65 74 65 20 6c 6f 6f 70 20  e complete loop 
217c6 69 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 6f  is the.    ** fo
217c7 6c 6c 6f 77 69 6e 67 20 70 73 65 75 64 6f 63 6f  llowing pseudoco
217c8 64 65 20 28 74 65 6d 70 6c 61 74 65 20 34 29 3a  de (template 4):
217c9 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
217ca 20 20 20 20 20 20 20 72 65 77 69 6e 64 20 74 65         rewind te
217cb 6d 70 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20  mp table.    ** 
217cc 20 20 20 20 20 43 3a 20 6c 6f 6f 70 20 6f 76 65       C: loop ove
217cd 72 20 72 6f 77 73 20 6f 66 20 69 6e 74 65 72 6d  r rows of interm
217ce 65 64 69 61 74 65 20 74 61 62 6c 65 0a 20 20 20  ediate table.   
217cf 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 72   **           tr
217d0 61 6e 73 66 65 72 20 76 61 6c 75 65 73 20 66 6f  ansfer values fo
217d1 72 6d 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  rm intermediate 
217d2 74 61 62 6c 65 20 69 6e 74 6f 20 3c 74 61 62 6c  table into <tabl
217d3 65 3e 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  e>.    **       
217d4 20 20 65 6e 64 20 6c 6f 6f 70 0a 20 20 20 20 2a    end loop.    *
217d5 2a 20 20 20 20 20 20 44 3a 20 2e 2e 2e 0a 20 20  *      D: ....  
217d6 20 20 2a 2f 0a 20 20 20 20 61 64 64 72 49 6e 73    */.    addrIns
217d7 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  Top = sqlite3Vdb
217d8 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65  eAddOp1(v, OP_Re
217d9 77 69 6e 64 2c 20 73 72 63 54 61 62 29 3b 0a 20  wind, srcTab);. 
217da 20 20 20 61 64 64 72 43 6f 6e 74 20 3d 20 73 71     addrCont = sq
217db 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
217dc 41 64 64 72 28 76 29 3b 0a 20 20 7d 65 6c 73 65  Addr(v);.  }else
217dd 20 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a   if( pSelect ){.
217de 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63      /* This bloc
217df 6b 20 63 6f 64 65 73 20 74 68 65 20 74 6f 70 20  k codes the top 
217e0 6f 66 20 6c 6f 6f 70 20 6f 6e 6c 79 2e 20 20 54  of loop only.  T
217e1 68 65 20 63 6f 6d 70 6c 65 74 65 20 6c 6f 6f 70  he complete loop
217e2 20 69 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 66   is the.    ** f
217e3 6f 6c 6c 6f 77 69 6e 67 20 70 73 65 75 64 6f 63  ollowing pseudoc
217e4 6f 64 65 20 28 74 65 6d 70 6c 61 74 65 20 33 29  ode (template 3)
217e5 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  :.    **.    ** 
217e6 20 20 20 20 20 43 3a 20 79 69 65 6c 64 20 58 0a       C: yield X.
217e7 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 69      **         i
217e8 66 20 45 4f 46 20 67 6f 74 6f 20 44 0a 20 20 20  f EOF goto D.   
217e9 20 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 73 65   **         inse
217ea 72 74 20 74 68 65 20 73 65 6c 65 63 74 20 72 65  rt the select re
217eb 73 75 6c 74 20 69 6e 74 6f 20 3c 74 61 62 6c 65  sult into <table
217ec 3e 20 66 72 6f 6d 20 52 2e 2e 52 2b 6e 0a 20 20  > from R..R+n.  
217ed 20 20 2a 2a 20 20 20 20 20 20 20 20 20 67 6f 74    **         got
217ee 6f 20 43 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  o C.    **      
217ef 44 3a 20 2e 2e 2e 0a 20 20 20 20 2a 2f 0a 20 20  D: ....    */.  
217f0 20 20 61 64 64 72 43 6f 6e 74 20 3d 20 73 71 6c    addrCont = sql
217f1 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
217f2 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 64 65 73 74  , OP_Yield, dest
217f3 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 61 64 64  .iParm);.    add
217f4 72 49 6e 73 54 6f 70 20 3d 20 73 71 6c 69 74 65  rInsTop = sqlite
217f5 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
217f6 50 5f 49 66 2c 20 72 65 67 45 6f 66 29 3b 0a 20  P_If, regEof);. 
217f7 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74   }..  /* Allocat
217f8 65 20 72 65 67 69 73 74 65 72 73 20 66 6f 72 20  e registers for 
217f9 68 6f 6c 64 69 6e 67 20 74 68 65 20 72 6f 77 69  holding the rowi
217fa 64 20 6f 66 20 74 68 65 20 6e 65 77 20 72 6f 77  d of the new row
217fb 2c 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6e 74 65  ,.  ** the conte
217fc 6e 74 20 6f 66 20 74 68 65 20 6e 65 77 20 72 6f  nt of the new ro
217fd 77 2c 20 61 6e 64 20 74 68 65 20 61 73 73 65 6d  w, and the assem
217fe 62 6c 69 65 64 20 72 6f 77 20 72 65 63 6f 72 64  blied row record
217ff 2e 0a 20 20 2a 2f 0a 20 20 72 65 67 52 65 63 6f  ..  */.  regReco
21800 72 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  rd = ++pParse->n
21801 4d 65 6d 3b 0a 20 20 72 65 67 52 6f 77 69 64 20  Mem;.  regRowid 
21802 3d 20 72 65 67 49 6e 73 20 3d 20 70 50 61 72 73  = regIns = pPars
21803 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 70 50 61  e->nMem+1;.  pPa
21804 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 54 61  rse->nMem += pTa
21805 62 2d 3e 6e 43 6f 6c 20 2b 20 31 3b 0a 20 20 69  b->nCol + 1;.  i
21806 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
21807 62 29 20 29 7b 0a 20 20 20 20 72 65 67 52 6f 77  b) ){.    regRow
21808 69 64 2b 2b 3b 0a 20 20 20 20 70 50 61 72 73 65  id++;.    pParse
21809 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20 7d 0a 20 20  ->nMem++;.  }.  
2180a 72 65 67 44 61 74 61 20 3d 20 72 65 67 52 6f 77  regData = regRow
2180b 69 64 2b 31 3b 0a 0a 20 20 2f 2a 20 52 75 6e 20  id+1;..  /* Run 
2180c 74 68 65 20 42 45 46 4f 52 45 20 61 6e 64 20 49  the BEFORE and I
2180d 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67 65  NSTEAD OF trigge
2180e 72 73 2c 20 69 66 20 74 68 65 72 65 20 61 72 65  rs, if there are
2180f 20 61 6e 79 0a 20 20 2a 2f 0a 20 20 65 6e 64 4f   any.  */.  endO
21810 66 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56  fLoop = sqlite3V
21811 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
21812 0a 20 20 69 66 28 20 74 72 69 67 67 65 72 73 5f  .  if( triggers_
21813 65 78 69 73 74 20 26 20 54 52 49 47 47 45 52 5f  exist & TRIGGER_
21814 42 45 46 4f 52 45 20 29 7b 0a 20 20 20 20 69 6e  BEFORE ){.    in
21815 74 20 72 65 67 52 6f 77 69 64 3b 0a 20 20 20 20  t regRowid;.    
21816 69 6e 74 20 72 65 67 43 6f 6c 73 3b 0a 20 20 20  int regCols;.   
21817 20 69 6e 74 20 72 65 67 52 65 63 3b 0a 0a 20 20   int regRec;..  
21818 20 20 2f 2a 20 62 75 69 6c 64 20 74 68 65 20 4e    /* build the N
21819 45 57 2e 2a 20 72 65 66 65 72 65 6e 63 65 20 72  EW.* reference r
2181a 6f 77 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 69  ow.  Note that i
2181b 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 49 4e  f there is an IN
2181c 54 45 47 45 52 0a 20 20 20 20 2a 2a 20 50 52 49  TEGER.    ** PRI
2181d 4d 41 52 59 20 4b 45 59 20 69 6e 74 6f 20 77 68  MARY KEY into wh
2181e 69 63 68 20 61 20 4e 55 4c 4c 20 69 73 20 62 65  ich a NULL is be
2181f 69 6e 67 20 69 6e 73 65 72 74 65 64 2c 20 74 68  ing inserted, th
21820 61 74 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 65 0a  at NULL will be.
21821 20 20 20 20 2a 2a 20 74 72 61 6e 73 6c 61 74 65      ** translate
21822 64 20 69 6e 74 6f 20 61 20 75 6e 69 71 75 65 20  d into a unique 
21823 49 44 20 66 6f 72 20 74 68 65 20 72 6f 77 2e 20  ID for the row. 
21824 20 42 75 74 20 6f 6e 20 61 20 42 45 46 4f 52 45   But on a BEFORE
21825 20 74 72 69 67 67 65 72 2c 0a 20 20 20 20 2a 2a   trigger,.    **
21826 20 77 65 20 64 6f 20 6e 6f 74 20 6b 6e 6f 77 20   we do not know 
21827 77 68 61 74 20 74 68 65 20 75 6e 69 71 75 65 20  what the unique 
21828 49 44 20 77 69 6c 6c 20 62 65 20 28 62 65 63 61  ID will be (beca
21829 75 73 65 20 74 68 65 20 69 6e 73 65 72 74 20 68  use the insert h
2182a 61 73 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 68 61  as.    ** not ha
2182b 70 70 65 6e 65 64 20 79 65 74 29 20 73 6f 20 77  ppened yet) so w
2182c 65 20 73 75 62 73 74 69 74 75 74 65 20 61 20 72  e substitute a r
2182d 6f 77 69 64 20 6f 66 20 2d 31 0a 20 20 20 20 2a  owid of -1.    *
2182e 2f 0a 20 20 20 20 72 65 67 52 6f 77 69 64 20 3d  /.    regRowid =
2182f 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
21830 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
21831 69 66 28 20 6b 65 79 43 6f 6c 75 6d 6e 3c 30 20  if( keyColumn<0 
21832 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
21833 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
21834 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20 72 65  _Integer, -1, re
21835 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 65 6c  gRowid);.    }el
21836 73 65 20 69 66 28 20 75 73 65 54 65 6d 70 54 61  se if( useTempTa
21837 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ble ){.      sql
21838 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
21839 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63  , OP_Column, src
2183a 54 61 62 2c 20 6b 65 79 43 6f 6c 75 6d 6e 2c 20  Tab, keyColumn, 
2183b 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d  regRowid);.    }
2183c 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20  else{.      int 
2183d 6a 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  j1;.      assert
2183e 28 20 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 20  ( pSelect==0 ); 
2183f 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 75 73   /* Otherwise us
21840 65 54 65 6d 70 54 61 62 6c 65 20 69 73 20 74 72  eTempTable is tr
21841 75 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  ue */.      sqli
21842 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
21843 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6b 65 79  se, pList->a[key
21844 43 6f 6c 75 6d 6e 5d 2e 70 45 78 70 72 2c 20 72  Column].pExpr, r
21845 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20  egRowid);.      
21846 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j1 = sqlite3Vdbe
21847 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74  AddOp1(v, OP_Not
21848 4e 75 6c 6c 2c 20 72 65 67 52 6f 77 69 64 29 3b  Null, regRowid);
21849 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2184a 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2184b 6e 74 65 67 65 72 2c 20 2d 31 2c 20 72 65 67 52  nteger, -1, regR
2184c 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 73 71 6c  owid);.      sql
2184d 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
2184e 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 20 20 73  (v, j1);.      s
2184f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
21850 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74  (v, OP_MustBeInt
21851 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
21852 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 61 6e 6e 6f   }..    /* Canno
21853 74 20 68 61 76 65 20 74 72 69 67 67 65 72 73 20  t have triggers 
21854 6f 6e 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  on a virtual tab
21855 6c 65 2e 20 49 66 20 69 74 20 77 65 72 65 20 70  le. If it were p
21856 6f 73 73 69 62 6c 65 2c 0a 20 20 20 20 2a 2a 20  ossible,.    ** 
21857 74 68 69 73 20 62 6c 6f 63 6b 20 77 6f 75 6c 64  this block would
21858 20 68 61 76 65 20 74 6f 20 61 63 63 6f 75 6e 74   have to account
21859 20 66 6f 72 20 68 69 64 64 65 6e 20 63 6f 6c 75   for hidden colu
2185a 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  mn..    */.    a
2185b 73 73 65 72 74 28 21 49 73 56 69 72 74 75 61 6c  ssert(!IsVirtual
2185c 28 70 54 61 62 29 29 3b 0a 0a 20 20 20 20 2f 2a  (pTab));..    /*
2185d 20 43 72 65 61 74 65 20 74 68 65 20 6e 65 77 20   Create the new 
2185e 63 6f 6c 75 6d 6e 20 64 61 74 61 0a 20 20 20 20  column data.    
2185f 2a 2f 0a 20 20 20 20 72 65 67 43 6f 6c 73 20 3d  */.    regCols =
21860 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
21861 61 6e 67 65 28 70 50 61 72 73 65 2c 20 70 54 61  ange(pParse, pTa
21862 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 66 6f  b->nCol);.    fo
21863 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e  r(i=0; i<pTab->n
21864 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
21865 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20   if( pColumn==0 
21866 29 7b 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 69  ){.        j = i
21867 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
21868 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20         for(j=0; 
21869 6a 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20  j<pColumn->nId; 
2186a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  j++){.          
2186b 69 66 28 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 6a  if( pColumn->a[j
2186c 5d 2e 69 64 78 3d 3d 69 20 29 20 62 72 65 61 6b  ].idx==i ) break
2186d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2186e 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 43    }.      if( pC
2186f 6f 6c 75 6d 6e 20 26 26 20 6a 3e 3d 70 43 6f 6c  olumn && j>=pCol
21870 75 6d 6e 2d 3e 6e 49 64 20 29 7b 0a 20 20 20 20  umn->nId ){.    
21871 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
21872 6f 64 65 28 70 50 61 72 73 65 2c 20 70 54 61 62  ode(pParse, pTab
21873 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 2c  ->aCol[i].pDflt,
21874 20 72 65 67 43 6f 6c 73 2b 69 29 3b 0a 20 20 20   regCols+i);.   
21875 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 73 65     }else if( use
21876 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20  TempTable ){.   
21877 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21878 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
21879 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6a 2c 20  umn, srcTab, j, 
2187a 72 65 67 43 6f 6c 73 2b 69 29 3b 20 0a 20 20 20  regCols+i); .   
2187b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2187c 20 20 61 73 73 65 72 74 28 20 70 53 65 6c 65 63    assert( pSelec
2187d 74 3d 3d 30 20 29 3b 20 2f 2a 20 4f 74 68 65 72  t==0 ); /* Other
2187e 77 69 73 65 20 75 73 65 54 65 6d 70 54 61 62 6c  wise useTempTabl
2187f 65 20 69 73 20 74 72 75 65 20 2a 2f 0a 20 20 20  e is true */.   
21880 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
21881 43 6f 64 65 41 6e 64 43 61 63 68 65 28 70 50 61  CodeAndCache(pPa
21882 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6a 5d  rse, pList->a[j]
21883 2e 70 45 78 70 72 2c 20 72 65 67 43 6f 6c 73 2b  .pExpr, regCols+
21884 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  i);.      }.    
21885 7d 0a 20 20 20 20 72 65 67 52 65 63 20 3d 20 73  }.    regRec = s
21886 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
21887 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 73 71  (pParse);.    sq
21888 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
21889 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  v, OP_MakeRecord
2188a 2c 20 72 65 67 43 6f 6c 73 2c 20 70 54 61 62 2d  , regCols, pTab-
2188b 3e 6e 43 6f 6c 2c 20 72 65 67 52 65 63 29 3b 0a  >nCol, regRec);.
2188c 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20  .    /* If this 
2188d 69 73 20 61 6e 20 49 4e 53 45 52 54 20 6f 6e 20  is an INSERT on 
2188e 61 20 76 69 65 77 20 77 69 74 68 20 61 6e 20 49  a view with an I
2188f 4e 53 54 45 41 44 20 4f 46 20 49 4e 53 45 52 54  NSTEAD OF INSERT
21890 20 74 72 69 67 67 65 72 2c 0a 20 20 20 20 2a 2a   trigger,.    **
21891 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20   do not attempt 
21892 61 6e 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20  any conversions 
21893 62 65 66 6f 72 65 20 61 73 73 65 6d 62 6c 69 6e  before assemblin
21894 67 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 20  g the record..  
21895 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20    ** If this is 
21896 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 61 74  a real table, at
21897 74 65 6d 70 74 20 63 6f 6e 76 65 72 73 69 6f 6e  tempt conversion
21898 73 20 61 73 20 72 65 71 75 69 72 65 64 20 62 79  s as required by
21899 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 61 62 6c   the.    ** tabl
2189a 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74  e column affinit
2189b 69 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ies..    */.    
2189c 69 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20  if( !isView ){. 
2189d 20 20 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c       sqlite3Tabl
2189e 65 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20  eAffinityStr(v, 
2189f 70 54 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20 20  pTab);.    }.   
218a0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
218a1 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c  p3(v, OP_Insert,
218a2 20 6e 65 77 49 64 78 2c 20 72 65 67 52 65 63 2c   newIdx, regRec,
218a3 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
218a4 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
218a5 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65  mpReg(pParse, re
218a6 67 52 65 63 29 3b 0a 20 20 20 20 73 71 6c 69 74  gRec);.    sqlit
218a7 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
218a8 28 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77 69  (pParse, regRowi
218a9 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52  d);.    sqlite3R
218aa 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28  eleaseTempRange(
218ab 70 50 61 72 73 65 2c 20 72 65 67 43 6f 6c 73 2c  pParse, regCols,
218ac 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 0a 20   pTab->nCol);.. 
218ad 20 20 20 2f 2a 20 46 69 72 65 20 42 45 46 4f 52     /* Fire BEFOR
218ae 45 20 6f 72 20 49 4e 53 54 45 41 44 20 4f 46 20  E or INSTEAD OF 
218af 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 20 20  triggers */.    
218b0 69 66 28 20 73 71 6c 69 74 65 33 43 6f 64 65 52  if( sqlite3CodeR
218b1 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73 65  owTrigger(pParse
218b2 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 30 2c 20  , TK_INSERT, 0, 
218b3 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c 20  TRIGGER_BEFORE, 
218b4 70 54 61 62 2c 20 0a 20 20 20 20 20 20 20 20 6e  pTab, .        n
218b5 65 77 49 64 78 2c 20 2d 31 2c 20 6f 6e 45 72 72  ewIdx, -1, onErr
218b6 6f 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 2c 20 30  or, endOfLoop, 0
218b7 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  , 0) ){.      go
218b8 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
218b9 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  p;.    }.  }..  
218ba 2f 2a 20 50 75 73 68 20 74 68 65 20 72 65 63 6f  /* Push the reco
218bb 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  rd number for th
218bc 65 20 6e 65 77 20 65 6e 74 72 79 20 6f 6e 74 6f  e new entry onto
218bd 20 74 68 65 20 73 74 61 63 6b 2e 20 20 54 68 65   the stack.  The
218be 0a 20 20 2a 2a 20 72 65 63 6f 72 64 20 6e 75 6d  .  ** record num
218bf 62 65 72 20 69 73 20 61 20 72 61 6e 64 6f 6d 6c  ber is a randoml
218c0 79 20 67 65 6e 65 72 61 74 65 20 69 6e 74 65 67  y generate integ
218c1 65 72 20 63 72 65 61 74 65 64 20 62 79 20 4e 65  er created by Ne
218c2 77 52 6f 77 69 64 0a 20 20 2a 2a 20 65 78 63 65  wRowid.  ** exce
218c3 70 74 20 77 68 65 6e 20 74 68 65 20 74 61 62 6c  pt when the tabl
218c4 65 20 68 61 73 20 61 6e 20 49 4e 54 45 47 45 52  e has an INTEGER
218c5 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c   PRIMARY KEY col
218c6 75 6d 6e 2c 20 69 6e 20 77 68 69 63 68 0a 20 20  umn, in which.  
218c7 2a 2a 20 63 61 73 65 20 74 68 65 20 72 65 63 6f  ** case the reco
218c8 72 64 20 6e 75 6d 62 65 72 20 69 73 20 74 68 65  rd number is the
218c9 20 73 61 6d 65 20 61 73 20 74 68 61 74 20 63 6f   same as that co
218ca 6c 75 6d 6e 2e 20 0a 20 20 2a 2f 0a 20 20 69 66  lumn. .  */.  if
218cb 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20  ( !isView ){.   
218cc 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70   if( IsVirtual(p
218cd 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a  Tab) ){.      /*
218ce 20 54 68 65 20 72 6f 77 20 74 68 61 74 20 74 68   The row that th
218cf 65 20 56 55 70 64 61 74 65 20 6f 70 63 6f 64 65  e VUpdate opcode
218d0 20 77 69 6c 6c 20 64 65 6c 65 74 65 3a 20 6e 6f   will delete: no
218d1 6e 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  ne */.      sqli
218d2 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
218d3 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67   OP_Null, 0, reg
218d4 49 6e 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Ins);.    }.    
218d5 69 66 28 20 6b 65 79 43 6f 6c 75 6d 6e 3e 3d 30  if( keyColumn>=0
218d6 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 75 73   ){.      if( us
218d7 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20  eTempTable ){.  
218d8 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
218d9 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
218da 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6b 65  lumn, srcTab, ke
218db 79 43 6f 6c 75 6d 6e 2c 20 72 65 67 52 6f 77 69  yColumn, regRowi
218dc 64 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  d);.      }else 
218dd 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20  if( pSelect ){. 
218de 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
218df 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
218e0 43 6f 70 79 2c 20 72 65 67 46 72 6f 6d 53 65 6c  Copy, regFromSel
218e1 65 63 74 2b 6b 65 79 43 6f 6c 75 6d 6e 2c 20 72  ect+keyColumn, r
218e2 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20  egRowid);.      
218e3 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 56  }else{.        V
218e4 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20  dbeOp *pOp;.    
218e5 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
218e6 6f 64 65 28 70 50 61 72 73 65 2c 20 70 4c 69 73  ode(pParse, pLis
218e7 74 2d 3e 61 5b 6b 65 79 43 6f 6c 75 6d 6e 5d 2e  t->a[keyColumn].
218e8 70 45 78 70 72 2c 20 72 65 67 52 6f 77 69 64 29  pExpr, regRowid)
218e9 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 20 3d 20  ;.        pOp = 
218ea 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70  sqlite3VdbeGetOp
218eb 28 76 2c 20 73 71 6c 69 74 65 33 56 64 62 65 43  (v, sqlite3VdbeC
218ec 75 72 72 65 6e 74 41 64 64 72 28 76 29 20 2d 20  urrentAddr(v) - 
218ed 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  1);.        if( 
218ee 70 4f 70 20 26 26 20 70 4f 70 2d 3e 6f 70 63 6f  pOp && pOp->opco
218ef 64 65 3d 3d 4f 50 5f 4e 75 6c 6c 20 26 26 20 21  de==OP_Null && !
218f0 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
218f1 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 70 70  ){.          app
218f2 65 6e 64 46 6c 61 67 20 3d 20 31 3b 0a 20 20 20  endFlag = 1;.   
218f3 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f         pOp->opco
218f4 64 65 20 3d 20 4f 50 5f 4e 65 77 52 6f 77 69 64  de = OP_NewRowid
218f5 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d  ;.          pOp-
218f6 3e 70 31 20 3d 20 62 61 73 65 43 75 72 3b 0a 20  >p1 = baseCur;. 
218f7 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 32           pOp->p2
218f8 20 3d 20 72 65 67 52 6f 77 69 64 3b 0a 20 20 20   = regRowid;.   
218f9 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 33 20 3d         pOp->p3 =
218fa 20 72 65 67 41 75 74 6f 69 6e 63 3b 0a 20 20 20   regAutoinc;.   
218fb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
218fc 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50       /* If the P
218fd 52 49 4d 41 52 59 20 4b 45 59 20 65 78 70 72 65  RIMARY KEY expre
218fe 73 73 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 74  ssion is NULL, t
218ff 68 65 6e 20 75 73 65 20 4f 50 5f 4e 65 77 52 6f  hen use OP_NewRo
21900 77 69 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20  wid.      ** to 
21901 67 65 6e 65 72 61 74 65 20 61 20 75 6e 69 71 75  generate a uniqu
21902 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 76 61  e primary key va
21903 6c 75 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  lue..      */.  
21904 20 20 20 20 69 66 28 20 21 61 70 70 65 6e 64 46      if( !appendF
21905 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20 69  lag ){.        i
21906 6e 74 20 6a 31 3b 0a 20 20 20 20 20 20 20 20 69  nt j1;.        i
21907 66 28 20 21 49 73 56 69 72 74 75 61 6c 28 70 54  f( !IsVirtual(pT
21908 61 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ab) ){.         
21909 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j1 = sqlite3Vdb
2190a 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f  eAddOp1(v, OP_No
2190b 74 4e 75 6c 6c 2c 20 72 65 67 52 6f 77 69 64 29  tNull, regRowid)
2190c 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
2190d 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2190e 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 62 61   OP_NewRowid, ba
2190f 73 65 43 75 72 2c 20 72 65 67 52 6f 77 69 64 2c  seCur, regRowid,
21910 20 72 65 67 41 75 74 6f 69 6e 63 29 3b 0a 20 20   regAutoinc);.  
21911 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
21912 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
21913 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  1);.        }els
21914 65 7b 0a 20 20 20 20 20 20 20 20 20 20 6a 31 20  e{.          j1 
21915 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
21916 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20  rentAddr(v);.   
21917 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
21918 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
21919 73 4e 75 6c 6c 2c 20 72 65 67 52 6f 77 69 64 2c  sNull, regRowid,
2191a 20 6a 31 2b 32 29 3b 0a 20 20 20 20 20 20 20 20   j1+2);.        
2191b 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
2191c 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2191d 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72 65 67  P_MustBeInt, reg
2191e 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 7d 0a  Rowid);.      }.
2191f 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 49 73      }else if( Is
21920 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b  Virtual(pTab) ){
21921 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
21922 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
21923 75 6c 6c 2c 20 30 2c 20 72 65 67 52 6f 77 69 64  ull, 0, regRowid
21924 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
21925 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21926 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65 77 52  ddOp3(v, OP_NewR
21927 6f 77 69 64 2c 20 62 61 73 65 43 75 72 2c 20 72  owid, baseCur, r
21928 65 67 52 6f 77 69 64 2c 20 72 65 67 41 75 74 6f  egRowid, regAuto
21929 69 6e 63 29 3b 0a 20 20 20 20 20 20 61 70 70 65  inc);.      appe
2192a 6e 64 46 6c 61 67 20 3d 20 31 3b 0a 20 20 20 20  ndFlag = 1;.    
2192b 7d 0a 20 20 20 20 61 75 74 6f 49 6e 63 53 74 65  }.    autoIncSte
2192c 70 28 70 50 61 72 73 65 2c 20 72 65 67 41 75 74  p(pParse, regAut
2192d 6f 69 6e 63 2c 20 72 65 67 52 6f 77 69 64 29 3b  oinc, regRowid);
2192e 0a 0a 20 20 20 20 2f 2a 20 50 75 73 68 20 6f 6e  ..    /* Push on
2192f 74 6f 20 74 68 65 20 73 74 61 63 6b 2c 20 64 61  to the stack, da
21930 74 61 20 66 6f 72 20 61 6c 6c 20 63 6f 6c 75 6d  ta for all colum
21931 6e 73 20 6f 66 20 74 68 65 20 6e 65 77 20 65 6e  ns of the new en
21932 74 72 79 2c 20 62 65 67 69 6e 6e 69 6e 67 0a 20  try, beginning. 
21933 20 20 20 2a 2a 20 77 69 74 68 20 74 68 65 20 66     ** with the f
21934 69 72 73 74 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20  irst column..   
21935 20 2a 2f 0a 20 20 20 20 6e 48 69 64 64 65 6e 20   */.    nHidden 
21936 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  = 0;.    for(i=0
21937 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  ; i<pTab->nCol; 
21938 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  i++){.      int 
21939 69 52 65 67 53 74 6f 72 65 20 3d 20 72 65 67 52  iRegStore = regR
2193a 6f 77 69 64 2b 31 2b 69 3b 0a 20 20 20 20 20 20  owid+1+i;.      
2193b 69 66 28 20 69 3d 3d 70 54 61 62 2d 3e 69 50 4b  if( i==pTab->iPK
2193c 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  ey ){.        /*
2193d 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   The value of th
2193e 65 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  e INTEGER PRIMAR
2193f 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 20 69 73 20  Y KEY column is 
21940 61 6c 77 61 79 73 20 61 20 4e 55 4c 4c 2e 0a 20  always a NULL.. 
21941 20 20 20 20 20 20 20 2a 2a 20 57 68 65 6e 65 76         ** Whenev
21942 65 72 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 69  er this column i
21943 73 20 72 65 61 64 2c 20 74 68 65 20 72 65 63 6f  s read, the reco
21944 72 64 20 6e 75 6d 62 65 72 20 77 69 6c 6c 20 62  rd number will b
21945 65 20 73 75 62 73 74 69 74 75 74 65 64 0a 20 20  e substituted.  
21946 20 20 20 20 20 20 2a 2a 20 69 6e 20 69 74 73 20        ** in its 
21947 70 6c 61 63 65 2e 20 20 53 6f 20 77 69 6c 6c 20  place.  So will 
21948 66 69 6c 6c 20 74 68 69 73 20 63 6f 6c 75 6d 6e  fill this column
21949 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 74 6f 20   with a NULL to 
2194a 61 76 6f 69 64 0a 20 20 20 20 20 20 20 20 2a 2a  avoid.        **
2194b 20 74 61 6b 69 6e 67 20 75 70 20 64 61 74 61 20   taking up data 
2194c 73 70 61 63 65 20 77 69 74 68 20 69 6e 66 6f 72  space with infor
2194d 6d 61 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c  mation that will
2194e 20 6e 65 76 65 72 20 62 65 20 75 73 65 64 2e 20   never be used. 
2194f 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  */.        sqlit
21950 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
21951 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 52 65 67  OP_Null, 0, iReg
21952 53 74 6f 72 65 29 3b 0a 20 20 20 20 20 20 20 20  Store);.        
21953 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
21954 7d 0a 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c  }.      if( pCol
21955 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  umn==0 ){.      
21956 20 20 69 66 28 20 49 73 48 69 64 64 65 6e 43 6f    if( IsHiddenCo
21957 6c 75 6d 6e 28 26 70 54 61 62 2d 3e 61 43 6f 6c  lumn(&pTab->aCol
21958 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20  [i]) ){.        
21959 20 20 61 73 73 65 72 74 28 20 49 73 56 69 72 74    assert( IsVirt
2195a 75 61 6c 28 70 54 61 62 29 20 29 3b 0a 20 20 20  ual(pTab) );.   
2195b 20 20 20 20 20 20 20 6a 20 3d 20 2d 31 3b 0a 20         j = -1;. 
2195c 20 20 20 20 20 20 20 20 20 6e 48 69 64 64 65 6e           nHidden
2195d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ++;.        }els
2195e 65 7b 0a 20 20 20 20 20 20 20 20 20 20 6a 20 3d  e{.          j =
2195f 20 69 20 2d 20 6e 48 69 64 64 65 6e 3b 0a 20 20   i - nHidden;.  
21960 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
21961 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72  lse{.        for
21962 28 6a 3d 30 3b 20 6a 3c 70 43 6f 6c 75 6d 6e 2d  (j=0; j<pColumn-
21963 3e 6e 49 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  >nId; j++){.    
21964 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 75 6d        if( pColum
21965 6e 2d 3e 61 5b 6a 5d 2e 69 64 78 3d 3d 69 20 29  n->a[j].idx==i )
21966 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
21967 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
21968 69 66 28 20 6a 3c 30 20 7c 7c 20 6e 43 6f 6c 75  if( j<0 || nColu
21969 6d 6e 3d 3d 30 20 7c 7c 20 28 70 43 6f 6c 75 6d  mn==0 || (pColum
2196a 6e 20 26 26 20 6a 3e 3d 70 43 6f 6c 75 6d 6e 2d  n && j>=pColumn-
2196b 3e 6e 49 64 29 20 29 7b 0a 20 20 20 20 20 20 20  >nId) ){.       
2196c 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2196d 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 61  (pParse, pTab->a
2196e 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 2c 20 69 52  Col[i].pDflt, iR
2196f 65 67 53 74 6f 72 65 29 3b 0a 20 20 20 20 20 20  egStore);.      
21970 7d 65 6c 73 65 20 69 66 28 20 75 73 65 54 65 6d  }else if( useTem
21971 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20  pTable ){.      
21972 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21973 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
21974 2c 20 73 72 63 54 61 62 2c 20 6a 2c 20 69 52 65  , srcTab, j, iRe
21975 67 53 74 6f 72 65 29 3b 20 0a 20 20 20 20 20 20  gStore); .      
21976 7d 65 6c 73 65 20 69 66 28 20 70 53 65 6c 65 63  }else if( pSelec
21977 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  t ){.        sql
21978 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
21979 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 46  , OP_SCopy, regF
2197a 72 6f 6d 53 65 6c 65 63 74 2b 6a 2c 20 69 52 65  romSelect+j, iRe
2197b 67 53 74 6f 72 65 29 3b 0a 20 20 20 20 20 20 7d  gStore);.      }
2197c 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
2197d 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
2197e 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6a  arse, pList->a[j
2197f 5d 2e 70 45 78 70 72 2c 20 69 52 65 67 53 74 6f  ].pExpr, iRegSto
21980 72 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  re);.      }.   
21981 20 7d 0a 0a 20 20 20 20 2f 2a 20 47 65 6e 65 72   }..    /* Gener
21982 61 74 65 20 63 6f 64 65 20 74 6f 20 63 68 65 63  ate code to chec
21983 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 6e  k constraints an
21984 64 20 67 65 6e 65 72 61 74 65 20 69 6e 64 65 78  d generate index
21985 20 6b 65 79 73 20 61 6e 64 0a 20 20 20 20 2a 2a   keys and.    **
21986 20 64 6f 20 74 68 65 20 69 6e 73 65 72 74 69 6f   do the insertio
21987 6e 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65  n..    */.#ifnde
21988 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
21989 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69  RTUALTABLE.    i
2198a 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
2198b 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  b) ){.      sqli
2198c 74 65 33 56 74 61 62 4d 61 6b 65 57 72 69 74 61  te3VtabMakeWrita
2198d 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62  ble(pParse, pTab
2198e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2198f 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
21990 5f 56 55 70 64 61 74 65 2c 20 31 2c 20 70 54 61  _VUpdate, 1, pTa
21991 62 2d 3e 6e 43 6f 6c 2b 32 2c 20 72 65 67 49 6e  b->nCol+2, regIn
21992 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  s,.             
21993 20 20 20 20 20 20 20 20 28 63 6f 6e 73 74 20 63          (const c
21994 68 61 72 2a 29 70 54 61 62 2d 3e 70 56 74 61 62  har*)pTab->pVtab
21995 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20 20 20 20  , P4_VTAB);.    
21996 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20  }else.#endif.   
21997 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
21998 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69  GenerateConstrai
21999 6e 74 43 68 65 63 6b 73 28 0a 20 20 20 20 20 20  ntChecks(.      
2199a 20 20 20 20 70 50 61 72 73 65 2c 0a 20 20 20 20      pParse,.    
2199b 20 20 20 20 20 20 70 54 61 62 2c 0a 20 20 20 20        pTab,.    
2199c 20 20 20 20 20 20 62 61 73 65 43 75 72 2c 0a 20        baseCur,. 
2199d 20 20 20 20 20 20 20 20 20 72 65 67 49 6e 73 2c           regIns,
2199e 0a 20 20 20 20 20 20 20 20 20 20 61 52 65 67 49  .          aRegI
2199f 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20 6b 65  dx,.          ke
219a0 79 43 6f 6c 75 6d 6e 3e 3d 30 2c 0a 20 20 20 20  yColumn>=0,.    
219a1 20 20 20 20 20 20 30 2c 0a 20 20 20 20 20 20 20        0,.       
219a2 20 20 20 6f 6e 45 72 72 6f 72 2c 0a 20 20 20 20     onError,.    
219a3 20 20 20 20 20 20 65 6e 64 4f 66 4c 6f 6f 70 0a        endOfLoop.
219a4 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 73        );.      s
219a5 71 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e  qlite3CompleteIn
219a6 73 65 72 74 69 6f 6e 28 0a 20 20 20 20 20 20 20  sertion(.       
219a7 20 20 20 70 50 61 72 73 65 2c 0a 20 20 20 20 20     pParse,.     
219a8 20 20 20 20 20 70 54 61 62 2c 0a 20 20 20 20 20       pTab,.     
219a9 20 20 20 20 20 62 61 73 65 43 75 72 2c 0a 20 20       baseCur,.  
219aa 20 20 20 20 20 20 20 20 72 65 67 49 6e 73 2c 0a          regIns,.
219ab 20 20 20 20 20 20 20 20 20 20 61 52 65 67 49 64            aRegId
219ac 78 2c 0a 20 20 20 20 20 20 20 20 20 20 30 2c 0a  x,.          0,.
219ad 20 20 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20            0,.   
219ae 20 20 20 20 20 20 20 28 74 72 69 67 67 65 72 73         (triggers
219af 5f 65 78 69 73 74 20 26 20 54 52 49 47 47 45 52  _exist & TRIGGER
219b0 5f 41 46 54 45 52 29 21 3d 30 20 3f 20 6e 65 77  _AFTER)!=0 ? new
219b1 49 64 78 20 3a 20 2d 31 2c 0a 20 20 20 20 20 20  Idx : -1,.      
219b2 20 20 20 20 61 70 70 65 6e 64 46 6c 61 67 0a 20      appendFlag. 
219b3 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20        );.    }. 
219b4 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20   }..  /* Update 
219b5 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 72 6f 77  the count of row
219b6 73 20 74 68 61 74 20 61 72 65 20 69 6e 73 65 72  s that are inser
219b7 74 65 64 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  ted.  */.  if( (
219b8 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
219b9 54 45 5f 43 6f 75 6e 74 52 6f 77 73 29 21 3d 30  TE_CountRows)!=0
219ba 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
219bb 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
219bc 41 64 64 49 6d 6d 2c 20 72 65 67 52 6f 77 43 6f  AddImm, regRowCo
219bd 75 6e 74 2c 20 31 29 3b 0a 20 20 7d 0a 0a 20 20  unt, 1);.  }..  
219be 69 66 28 20 74 72 69 67 67 65 72 73 5f 65 78 69  if( triggers_exi
219bf 73 74 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 64  st ){.    /* Cod
219c0 65 20 41 46 54 45 52 20 74 72 69 67 67 65 72 73  e AFTER triggers
219c1 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69   */.    if( sqli
219c2 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65  te3CodeRowTrigge
219c3 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 53  r(pParse, TK_INS
219c4 45 52 54 2c 20 30 2c 20 54 52 49 47 47 45 52 5f  ERT, 0, TRIGGER_
219c5 41 46 54 45 52 2c 20 70 54 61 62 2c 0a 20 20 20  AFTER, pTab,.   
219c6 20 20 20 20 20 20 20 6e 65 77 49 64 78 2c 20 2d         newIdx, -
219c7 31 2c 20 6f 6e 45 72 72 6f 72 2c 20 65 6e 64 4f  1, onError, endO
219c8 66 4c 6f 6f 70 2c 20 30 2c 20 30 29 20 29 7b 0a  fLoop, 0, 0) ){.
219c9 20 20 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72        goto inser
219ca 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d  t_cleanup;.    }
219cb 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 62  .  }..  /* The b
219cc 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20 6d 61 69  ottom of the mai
219cd 6e 20 69 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f 70  n insertion loop
219ce 2c 20 69 66 20 74 68 65 20 64 61 74 61 20 73 6f  , if the data so
219cf 75 72 63 65 0a 20 20 2a 2a 20 69 73 20 61 20 53  urce.  ** is a S
219d0 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
219d1 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  .  */.  sqlite3V
219d2 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
219d3 76 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20  v, endOfLoop);. 
219d4 20 69 66 28 20 75 73 65 54 65 6d 70 54 61 62 6c   if( useTempTabl
219d5 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  e ){.    sqlite3
219d6 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
219d7 5f 4e 65 78 74 2c 20 73 72 63 54 61 62 2c 20 61  _Next, srcTab, a
219d8 64 64 72 43 6f 6e 74 29 3b 0a 20 20 20 20 73 71  ddrCont);.    sq
219d9 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
219da 65 28 76 2c 20 61 64 64 72 49 6e 73 54 6f 70 29  e(v, addrInsTop)
219db 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
219dc 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c  eAddOp1(v, OP_Cl
219dd 6f 73 65 2c 20 73 72 63 54 61 62 29 3b 0a 20 20  ose, srcTab);.  
219de 7d 65 6c 73 65 20 69 66 28 20 70 53 65 6c 65 63  }else if( pSelec
219df 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
219e0 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
219e1 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 43 6f  _Goto, 0, addrCo
219e2 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  nt);.    sqlite3
219e3 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
219e4 61 64 64 72 49 6e 73 54 6f 70 29 3b 0a 20 20 7d  addrInsTop);.  }
219e5 0a 0a 20 20 69 66 28 20 21 49 73 56 69 72 74 75  ..  if( !IsVirtu
219e6 61 6c 28 70 54 61 62 29 20 26 26 20 21 69 73 56  al(pTab) && !isV
219e7 69 65 77 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6c  iew ){.    /* Cl
219e8 6f 73 65 20 61 6c 6c 20 74 61 62 6c 65 73 20 6f  ose all tables o
219e9 70 65 6e 65 64 20 2a 2f 0a 20 20 20 20 73 71 6c  pened */.    sql
219ea 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
219eb 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 62 61 73 65  , OP_Close, base
219ec 43 75 72 29 3b 0a 20 20 20 20 66 6f 72 28 69 64  Cur);.    for(id
219ed 78 3d 31 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e  x=1, pIdx=pTab->
219ee 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
219ef 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20  dx=pIdx->pNext, 
219f0 69 64 78 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  idx++){.      sq
219f1 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
219f2 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 64 78  v, OP_Close, idx
219f3 2b 62 61 73 65 43 75 72 29 3b 0a 20 20 20 20 7d  +baseCur);.    }
219f4 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74  .  }..  /* Updat
219f5 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71  e the sqlite_seq
219f6 75 65 6e 63 65 20 74 61 62 6c 65 20 62 79 20 73  uence table by s
219f7 74 6f 72 69 6e 67 20 74 68 65 20 63 6f 6e 74 65  toring the conte
219f8 6e 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 63  nt of the.  ** c
219f9 6f 75 6e 74 65 72 20 76 61 6c 75 65 20 69 6e 20  ounter value in 
219fa 6d 65 6d 6f 72 79 20 72 65 67 41 75 74 6f 69 6e  memory regAutoin
219fb 63 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20  c back into the 
219fc 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 0a  sqlite_sequence.
219fd 20 20 2a 2a 20 74 61 62 6c 65 2e 0a 20 20 2a 2f    ** table..  */
219fe 0a 20 20 61 75 74 6f 49 6e 63 45 6e 64 28 70 50  .  autoIncEnd(pP
219ff 61 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62 2c  arse, iDb, pTab,
21a00 20 72 65 67 41 75 74 6f 69 6e 63 29 3b 0a 0a 20   regAutoinc);.. 
21a01 20 2f 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20   /*.  ** Return 
21a02 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  the number of ro
21a03 77 73 20 69 6e 73 65 72 74 65 64 2e 20 49 66 20  ws inserted. If 
21a04 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
21a05 0a 20 20 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67  .  ** generating
21a06 20 63 6f 64 65 20 62 65 63 61 75 73 65 20 6f 66   code because of
21a07 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74   a call to sqlit
21a08 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 29 2c  e3NestedParse(),
21a09 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 69 6e 76   do not.  ** inv
21a0a 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  oke the callback
21a0b 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a   function..  */.
21a0c 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20    if( db->flags 
21a0d 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f  & SQLITE_CountRo
21a0e 77 73 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 65  ws && pParse->ne
21a0f 73 74 65 64 3d 3d 30 20 26 26 20 21 70 50 61 72  sted==0 && !pPar
21a10 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 20 29 7b  se->trigStack ){
21a11 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
21a12 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73  AddOp2(v, OP_Res
21a13 75 6c 74 52 6f 77 2c 20 72 65 67 52 6f 77 43 6f  ultRow, regRowCo
21a14 75 6e 74 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c  unt, 1);.    sql
21a15 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
21a16 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71  ls(v, 1);.    sq
21a17 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
21a18 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
21a19 45 5f 4e 41 4d 45 2c 20 22 72 6f 77 73 20 69 6e  E_NAME, "rows in
21a1a 73 65 72 74 65 64 22 2c 20 53 51 4c 49 54 45 5f  serted", SQLITE_
21a1b 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 0a 69 6e  STATIC);.  }..in
21a1c 73 65 72 74 5f 63 6c 65 61 6e 75 70 3a 0a 20 20  sert_cleanup:.  
21a1d 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65  sqlite3SrcListDe
21a1e 6c 65 74 65 28 64 62 2c 20 70 54 61 62 4c 69 73  lete(db, pTabLis
21a1f 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  t);.  sqlite3Exp
21a20 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
21a21 70 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65  pList);.  sqlite
21a22 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
21a23 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 73 71  , pSelect);.  sq
21a24 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74  lite3IdListDelet
21a25 65 28 64 62 2c 20 70 43 6f 6c 75 6d 6e 29 3b 0a  e(db, pColumn);.
21a26 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
21a27 64 62 2c 20 61 52 65 67 49 64 78 29 3b 0a 7d 0a  db, aRegIdx);.}.
21a28 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
21a29 63 6f 64 65 20 74 6f 20 64 6f 20 63 6f 6e 73 74  code to do const
21a2a 72 61 69 6e 74 20 63 68 65 63 6b 73 20 70 72 69  raint checks pri
21a2b 6f 72 20 74 6f 20 61 6e 20 49 4e 53 45 52 54 20  or to an INSERT 
21a2c 6f 72 20 61 6e 20 55 50 44 41 54 45 2e 0a 2a 2a  or an UPDATE..**
21a2d 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 69 73  .** The input is
21a2e 20 61 20 72 61 6e 67 65 20 6f 66 20 63 6f 6e 73   a range of cons
21a2f 65 63 75 74 69 76 65 20 72 65 67 69 73 74 65 72  ecutive register
21a30 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  s as follows:.**
21a31 0a 2a 2a 20 20 20 20 31 2e 20 20 54 68 65 20 72  .**    1.  The r
21a32 6f 77 69 64 20 6f 66 20 74 68 65 20 72 6f 77 20  owid of the row 
21a33 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 62 65  to be updated be
21a34 66 6f 72 65 20 74 68 65 20 75 70 64 61 74 65 2e  fore the update.
21a35 20 20 54 68 69 73 0a 2a 2a 20 20 20 20 20 20 20    This.**       
21a36 20 76 61 6c 75 65 20 69 73 20 6f 6d 69 74 74 65   value is omitte
21a37 64 20 75 6e 6c 65 73 73 20 77 65 20 61 72 65 20  d unless we are 
21a38 64 6f 69 6e 67 20 61 6e 20 55 50 44 41 54 45 20  doing an UPDATE 
21a39 74 68 61 74 20 69 6e 76 6f 6c 76 65 73 20 61 0a  that involves a.
21a3a 2a 2a 20 20 20 20 20 20 20 20 63 68 61 6e 67 65  **        change
21a3b 20 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20 6e   to the record n
21a3c 75 6d 62 65 72 20 6f 72 20 77 72 69 74 69 6e 67  umber or writing
21a3d 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 74 61   to a virtual ta
21a3e 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 32 2e  ble..**.**    2.
21a3f 20 20 54 68 65 20 72 6f 77 69 64 20 6f 66 20 74    The rowid of t
21a40 68 65 20 72 6f 77 20 61 66 74 65 72 20 74 68 65  he row after the
21a41 20 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20   update..**.**  
21a42 20 20 33 2e 20 20 54 68 65 20 64 61 74 61 20 69    3.  The data i
21a43 6e 20 74 68 65 20 66 69 72 73 74 20 63 6f 6c 75  n the first colu
21a44 6d 6e 20 6f 66 20 74 68 65 20 65 6e 74 72 79 20  mn of the entry 
21a45 61 66 74 65 72 20 74 68 65 20 75 70 64 61 74 65  after the update
21a46 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 69 2e 20 20 44  ..**.**    i.  D
21a47 61 74 61 20 66 72 6f 6d 20 6d 69 64 64 6c 65 20  ata from middle 
21a48 63 6f 6c 75 6d 6e 73 2e 2e 2e 0a 2a 2a 0a 2a 2a  columns....**.**
21a49 20 20 20 20 4e 2e 20 20 54 68 65 20 64 61 74 61      N.  The data
21a4a 20 69 6e 20 74 68 65 20 6c 61 73 74 20 63 6f 6c   in the last col
21a4b 75 6d 6e 20 6f 66 20 74 68 65 20 65 6e 74 72 79  umn of the entry
21a4c 20 61 66 74 65 72 20 74 68 65 20 75 70 64 61 74   after the updat
21a4d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67  e..**.** The reg
21a4e 52 6f 77 69 64 20 70 61 72 61 6d 65 74 65 72 20  Rowid parameter 
21a4f 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  is the index of 
21a50 74 68 65 20 72 65 67 69 73 74 65 72 20 63 6f 6e  the register con
21a51 74 61 69 6e 69 6e 67 20 28 32 29 2e 0a 2a 2a 0a  taining (2)..**.
21a52 2a 2a 20 54 68 65 20 6f 6c 64 20 72 6f 77 69 64  ** The old rowid
21a53 20 73 68 6f 77 6e 20 61 73 20 65 6e 74 72 79 20   shown as entry 
21a54 28 31 29 20 61 62 6f 76 65 20 69 73 20 6f 6d 69  (1) above is omi
21a55 74 74 65 64 20 75 6e 6c 65 73 73 20 62 6f 74 68  tted unless both
21a56 20 69 73 55 70 64 61 74 65 0a 2a 2a 20 61 6e 64   isUpdate.** and
21a57 20 72 6f 77 69 64 43 68 6e 67 20 61 72 65 20 31   rowidChng are 1
21a58 2e 20 20 69 73 55 70 64 61 74 65 20 69 73 20 74  .  isUpdate is t
21a59 72 75 65 20 66 6f 72 20 55 50 44 41 54 45 73 20  rue for UPDATEs 
21a5a 61 6e 64 20 66 61 6c 73 65 20 66 6f 72 0a 2a 2a  and false for.**
21a5b 20 49 4e 53 45 52 54 73 2e 20 20 52 6f 77 69 64   INSERTs.  Rowid
21a5c 43 68 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 20  Chng means that 
21a5d 74 68 65 20 6e 65 77 20 72 6f 77 69 64 20 69 73  the new rowid is
21a5e 20 65 78 70 6c 69 63 69 74 6c 79 20 73 70 65 63   explicitly spec
21a5f 69 66 69 65 64 20 62 79 0a 2a 2a 20 74 68 65 20  ified by.** the 
21a60 75 70 64 61 74 65 20 6f 72 20 69 6e 73 65 72 74  update or insert
21a61 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 66 20   statement.  If 
21a62 72 6f 77 69 64 43 68 6e 67 20 69 73 20 66 61 6c  rowidChng is fal
21a63 73 65 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61  se, it means tha
21a64 74 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64 20 69  t.** the rowid i
21a65 73 20 63 6f 6d 70 75 74 65 64 20 61 75 74 6f 6d  s computed autom
21a66 61 74 69 63 61 6c 6c 79 20 69 6e 20 61 6e 20 69  atically in an i
21a67 6e 73 65 72 74 20 6f 72 20 74 68 61 74 20 74 68  nsert or that th
21a68 65 20 72 6f 77 69 64 20 76 61 6c 75 65 0a 2a 2a  e rowid value.**
21a69 20 69 73 20 6e 6f 74 20 6d 6f 64 69 66 69 65 64   is not modified
21a6a 20 62 79 20 74 68 65 20 75 70 64 61 74 65 2e 0a   by the update..
21a6b 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 67  **.** The code g
21a6c 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 69 73  enerated by this
21a6d 20 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 20 6e   routine store n
21a6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72 69 65 73  ew index entries
21a6f 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65   into.** registe
21a70 72 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  rs identified by
21a71 20 61 52 65 67 49 64 78 5b 5d 2e 20 20 4e 6f 20   aRegIdx[].  No 
21a72 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20 63  index entry is c
21a73 72 65 61 74 65 64 20 66 6f 72 0a 2a 2a 20 69 6e  reated for.** in
21a74 64 69 63 65 73 20 77 68 65 72 65 20 61 52 65 67  dices where aReg
21a75 49 64 78 5b 69 5d 3d 3d 30 2e 20 20 54 68 65 20  Idx[i]==0.  The 
21a76 6f 72 64 65 72 20 6f 66 20 69 6e 64 69 63 65 73  order of indices
21a77 20 69 6e 20 61 52 65 67 49 64 78 5b 5d 20 69 73   in aRegIdx[] is
21a78 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73 20  .** the same as 
21a79 74 68 65 20 6f 72 64 65 72 20 6f 66 20 69 6e 64  the order of ind
21a7a 69 63 65 73 20 6f 6e 20 74 68 65 20 6c 69 6e 6b  ices on the link
21a7b 65 64 20 6c 69 73 74 20 6f 66 20 69 6e 64 69 63  ed list of indic
21a7c 65 73 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 74  es.** attached t
21a7d 6f 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a  o the table..**.
21a7e 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
21a7f 61 6c 73 6f 20 67 65 6e 65 72 61 74 65 73 20 63  also generates c
21a80 6f 64 65 20 74 6f 20 63 68 65 63 6b 20 63 6f 6e  ode to check con
21a81 73 74 72 61 69 6e 74 73 2e 20 20 4e 4f 54 20 4e  straints.  NOT N
21a82 55 4c 4c 2c 0a 2a 2a 20 43 48 45 43 4b 2c 20 61  ULL,.** CHECK, a
21a83 6e 64 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72  nd UNIQUE constr
21a84 61 69 6e 74 73 20 61 72 65 20 61 6c 6c 20 63 68  aints are all ch
21a85 65 63 6b 65 64 2e 20 20 49 66 20 61 20 63 6f 6e  ecked.  If a con
21a86 73 74 72 61 69 6e 74 20 66 61 69 6c 73 2c 0a 2a  straint fails,.*
21a87 2a 20 74 68 65 6e 20 74 68 65 20 61 70 70 72 6f  * then the appro
21a88 70 72 69 61 74 65 20 61 63 74 69 6f 6e 20 69 73  priate action is
21a89 20 70 65 72 66 6f 72 6d 65 64 2e 20 20 54 68 65   performed.  The
21a8a 72 65 20 61 72 65 20 66 69 76 65 20 70 6f 73 73  re are five poss
21a8b 69 62 6c 65 0a 2a 2a 20 61 63 74 69 6f 6e 73 3a  ible.** actions:
21a8c 20 52 4f 4c 4c 42 41 43 4b 2c 20 41 42 4f 52 54   ROLLBACK, ABORT
21a8d 2c 20 46 41 49 4c 2c 20 52 45 50 4c 41 43 45 2c  , FAIL, REPLACE,
21a8e 20 61 6e 64 20 49 47 4e 4f 52 45 2e 0a 2a 2a 0a   and IGNORE..**.
21a8f 2a 2a 20 20 43 6f 6e 73 74 72 61 69 6e 74 20 74  **  Constraint t
21a90 79 70 65 20 20 41 63 74 69 6f 6e 20 20 20 20 20  ype  Action     
21a91 20 20 57 68 61 74 20 48 61 70 70 65 6e 73 0a 2a    What Happens.*
21a92 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  *  -------------
21a93 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20  --  ----------  
21a94 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
21a95 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
21a96 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 61 6e  ---------.**  an
21a97 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 52  y              R
21a98 4f 4c 4c 42 41 43 4b 20 20 20 20 20 54 68 65 20  OLLBACK     The 
21a99 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
21a9a 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61  ion is rolled ba
21a9b 63 6b 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20  ck and.**       
21a9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21a9d 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
21a9e 5f 65 78 65 63 28 29 20 72 65 74 75 72 6e 73 20  _exec() returns 
21a9f 69 6d 6d 65 64 69 61 74 65 6c 79 20 77 69 74 68  immediately with
21aa0 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20   a.**           
21aa1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21aa2 20 20 20 20 20 72 65 74 75 72 6e 20 63 6f 64 65       return code
21aa3 20 6f 66 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54   of SQLITE_CONST
21aa4 52 41 49 4e 54 2e 0a 2a 2a 0a 2a 2a 20 20 61 6e  RAINT..**.**  an
21aa5 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 41  y              A
21aa6 42 4f 52 54 20 20 20 20 20 20 20 20 42 61 63 6b  BORT        Back
21aa7 20 6f 75 74 20 63 68 61 6e 67 65 73 20 66 72 6f   out changes fro
21aa8 6d 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f  m the current co
21aa9 6d 6d 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20  mmand.**        
21aaa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21aab 20 20 20 20 20 20 20 20 6f 6e 6c 79 20 28 64 6f          only (do
21aac 20 6e 6f 74 20 64 6f 20 61 20 63 6f 6d 70 6c 65   not do a comple
21aad 74 65 20 72 6f 6c 6c 62 61 63 6b 29 20 74 68 65  te rollback) the
21aae 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  n.**            
21aaf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21ab0 20 20 20 20 63 61 75 73 65 20 73 71 6c 69 74 65      cause sqlite
21ab1 33 5f 65 78 65 63 28 29 20 74 6f 20 72 65 74 75  3_exec() to retu
21ab2 72 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a  rn immediately.*
21ab3 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
21ab4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21ab5 20 77 69 74 68 20 53 51 4c 49 54 45 5f 43 4f 4e   with SQLITE_CON
21ab6 53 54 52 41 49 4e 54 2e 0a 2a 2a 0a 2a 2a 20 20  STRAINT..**.**  
21ab7 61 6e 79 20 20 20 20 20 20 20 20 20 20 20 20 20  any             
21ab8 20 46 41 49 4c 20 20 20 20 20 20 20 20 20 53 71   FAIL         Sq
21ab9 6c 69 74 65 5f 65 78 65 63 28 29 20 72 65 74 75  lite_exec() retu
21aba 72 6e 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  rns immediately 
21abb 77 69 74 68 20 61 0a 2a 2a 20 20 20 20 20 20 20  with a.**       
21abc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21abd 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
21abe 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 5f 43  code of SQLITE_C
21abf 4f 4e 53 54 52 41 49 4e 54 2e 20 20 54 68 65 0a  ONSTRAINT.  The.
21ac0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
21ac1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21ac2 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73    transaction is
21ac3 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b   not rolled back
21ac4 20 61 6e 64 20 61 6e 79 0a 2a 2a 20 20 20 20 20   and any.**     
21ac5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21ac6 20 20 20 20 20 20 20 20 20 20 20 70 72 69 6f 72             prior
21ac7 20 63 68 61 6e 67 65 73 20 61 72 65 20 72 65 74   changes are ret
21ac8 61 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 61 6e  ained..**.**  an
21ac9 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 49  y              I
21aca 47 4e 4f 52 45 20 20 20 20 20 20 20 54 68 65 20  GNORE       The 
21acb 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 61 6e  record number an
21acc 64 20 64 61 74 61 20 69 73 20 70 6f 70 70 65 64  d data is popped
21acd 20 66 72 6f 6d 0a 2a 2a 20 20 20 20 20 20 20 20   from.**        
21ace 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21acf 20 20 20 20 20 20 20 20 74 68 65 20 73 74 61 63          the stac
21ad0 6b 20 61 6e 64 20 74 68 65 72 65 20 69 73 20 61  k and there is a
21ad1 6e 20 69 6d 6d 65 64 69 61 74 65 20 6a 75 6d 70  n immediate jump
21ad2 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
21ad3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21ad4 20 20 20 74 6f 20 6c 61 62 65 6c 20 69 67 6e 6f     to label igno
21ad5 72 65 44 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 20 4e  reDest..**.**  N
21ad6 4f 54 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20  OT NULL         
21ad7 52 45 50 4c 41 43 45 20 20 20 20 20 20 54 68 65  REPLACE      The
21ad8 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 72   NULL value is r
21ad9 65 70 6c 61 63 65 20 62 79 20 74 68 65 20 64 65  eplace by the de
21ada 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20  fault.**        
21adb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21adc 20 20 20 20 20 20 20 20 76 61 6c 75 65 20 66 6f          value fo
21add 72 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 20 20  r that column.  
21ade 49 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 76  If the default v
21adf 61 6c 75 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  alue.**         
21ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21ae1 20 20 20 20 20 20 20 69 73 20 4e 55 4c 4c 2c 20         is NULL, 
21ae2 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20 74 68  the action is th
21ae3 65 20 73 61 6d 65 20 61 73 20 41 42 4f 52 54 2e  e same as ABORT.
21ae4 0a 2a 2a 0a 2a 2a 20 20 55 4e 49 51 55 45 20 20  .**.**  UNIQUE  
21ae5 20 20 20 20 20 20 20 20 20 52 45 50 4c 41 43 45           REPLACE
21ae6 20 20 20 20 20 20 54 68 65 20 6f 74 68 65 72 20        The other 
21ae7 72 6f 77 20 74 68 61 74 20 63 6f 6e 66 6c 69 63  row that conflic
21ae8 74 73 20 77 69 74 68 20 74 68 65 20 72 6f 77 0a  ts with the row.
21ae9 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
21aea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21aeb 20 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64    being inserted
21aec 20 69 73 20 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a   is removed..**.
21aed 2a 2a 20 20 43 48 45 43 4b 20 20 20 20 20 20 20  **  CHECK       
21aee 20 20 20 20 20 52 45 50 4c 41 43 45 20 20 20 20       REPLACE    
21aef 20 20 49 6c 6c 65 67 61 6c 2e 20 20 54 68 65 20    Illegal.  The 
21af0 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 20 65 78  results in an ex
21af1 63 65 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57  ception..**.** W
21af2 68 69 63 68 20 61 63 74 69 6f 6e 20 74 6f 20 74  hich action to t
21af3 61 6b 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65  ake is determine
21af4 64 20 62 79 20 74 68 65 20 6f 76 65 72 72 69 64  d by the overrid
21af5 65 45 72 72 6f 72 20 70 61 72 61 6d 65 74 65 72  eError parameter
21af6 2e 0a 2a 2a 20 4f 72 20 69 66 20 6f 76 65 72 72  ..** Or if overr
21af7 69 64 65 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66  ideError==OE_Def
21af8 61 75 6c 74 2c 20 74 68 65 6e 20 74 68 65 20 70  ault, then the p
21af9 50 61 72 73 65 2d 3e 6f 6e 45 72 72 6f 72 20 70  Parse->onError p
21afa 61 72 61 6d 65 74 65 72 0a 2a 2a 20 69 73 20 75  arameter.** is u
21afb 73 65 64 2e 20 20 4f 72 20 69 66 20 70 50 61 72  sed.  Or if pPar
21afc 73 65 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  se->onError==OE_
21afd 44 65 66 61 75 6c 74 20 74 68 65 6e 20 74 68 65  Default then the
21afe 20 6f 6e 45 72 72 6f 72 20 76 61 6c 75 65 0a 2a   onError value.*
21aff 2a 20 66 6f 72 20 74 68 65 20 63 6f 6e 73 74 72  * for the constr
21b00 61 69 6e 74 20 69 73 20 75 73 65 64 2e 0a 2a 2a  aint is used..**
21b01 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20  .** The calling 
21b02 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 6f 70 65  routine must ope
21b03 6e 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 63  n a read/write c
21b04 75 72 73 6f 72 20 66 6f 72 20 70 54 61 62 20 77  ursor for pTab w
21b05 69 74 68 0a 2a 2a 20 63 75 72 73 6f 72 20 6e 75  ith.** cursor nu
21b06 6d 62 65 72 20 22 62 61 73 65 43 75 72 22 2e 20  mber "baseCur". 
21b07 20 41 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20   All indices of 
21b08 70 54 61 62 20 6d 75 73 74 20 61 6c 73 6f 20 68  pTab must also h
21b09 61 76 65 20 6f 70 65 6e 0a 2a 2a 20 72 65 61 64  ave open.** read
21b0a 2f 77 72 69 74 65 20 63 75 72 73 6f 72 73 20 77  /write cursors w
21b0b 69 74 68 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  ith cursor numbe
21b0c 72 20 62 61 73 65 43 75 72 2b 69 20 66 6f 72 20  r baseCur+i for 
21b0d 74 68 65 20 69 2d 74 68 20 63 75 72 73 6f 72 2e  the i-th cursor.
21b0e 0a 2a 2a 20 45 78 63 65 70 74 2c 20 69 66 20 74  .** Except, if t
21b0f 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 73 73 69  here is no possi
21b10 62 69 6c 69 74 79 20 6f 66 20 61 20 52 45 50 4c  bility of a REPL
21b11 41 43 45 20 61 63 74 69 6f 6e 20 74 68 65 6e 0a  ACE action then.
21b12 2a 2a 20 63 75 72 73 6f 72 73 20 64 6f 20 6e 6f  ** cursors do no
21b13 74 20 6e 65 65 64 20 74 6f 20 62 65 20 6f 70 65  t need to be ope
21b14 6e 20 66 6f 72 20 69 6e 64 69 63 65 73 20 77 68  n for indices wh
21b15 65 72 65 20 61 52 65 67 49 64 78 5b 69 5d 3d 3d  ere aRegIdx[i]==
21b16 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  0..*/.SQLITE_PRI
21b17 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
21b18 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61  3GenerateConstra
21b19 69 6e 74 43 68 65 63 6b 73 28 0a 20 20 50 61 72  intChecks(.  Par
21b1a 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
21b1b 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
21b1c 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c  ontext */.  Tabl
21b1d 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20  e *pTab,        
21b1e 2f 2a 20 74 68 65 20 74 61 62 6c 65 20 69 6e 74  /* the table int
21b1f 6f 20 77 68 69 63 68 20 77 65 20 61 72 65 20 69  o which we are i
21b20 6e 73 65 72 74 69 6e 67 20 2a 2f 0a 20 20 69 6e  nserting */.  in
21b21 74 20 62 61 73 65 43 75 72 2c 20 20 20 20 20 20  t baseCur,      
21b22 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 61 20    /* Index of a 
21b23 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f  read/write curso
21b24 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 70 54  r pointing at pT
21b25 61 62 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52  ab */.  int regR
21b26 6f 77 69 64 2c 20 20 20 20 20 20 20 2f 2a 20 49  owid,       /* I
21b27 6e 64 65 78 20 6f 66 20 74 68 65 20 72 61 6e 67  ndex of the rang
21b28 65 20 6f 66 20 69 6e 70 75 74 20 72 65 67 69 73  e of input regis
21b29 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 61  ters */.  int *a
21b2a 52 65 67 49 64 78 2c 20 20 20 20 20 20 20 2f 2a  RegIdx,       /*
21b2b 20 52 65 67 69 73 74 65 72 20 75 73 65 64 20 62   Register used b
21b2c 79 20 65 61 63 68 20 69 6e 64 65 78 2e 20 20 30  y each index.  0
21b2d 20 66 6f 72 20 75 6e 75 73 65 64 20 69 6e 64 69   for unused indi
21b2e 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 6f 77  ces */.  int row
21b2f 69 64 43 68 6e 67 2c 20 20 20 20 20 20 2f 2a 20  idChng,      /* 
21b30 54 72 75 65 20 69 66 20 74 68 65 20 72 6f 77 69  True if the rowi
21b31 64 20 6d 69 67 68 74 20 63 6f 6c 6c 69 64 65 20  d might collide 
21b32 77 69 74 68 20 65 78 69 73 74 69 6e 67 20 65 6e  with existing en
21b33 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69 73 55  try */.  int isU
21b34 70 64 61 74 65 2c 20 20 20 20 20 20 20 2f 2a 20  pdate,       /* 
21b35 54 72 75 65 20 66 6f 72 20 55 50 44 41 54 45 2c  True for UPDATE,
21b36 20 46 61 6c 73 65 20 66 6f 72 20 49 4e 53 45 52   False for INSER
21b37 54 20 2a 2f 0a 20 20 69 6e 74 20 6f 76 65 72 72  T */.  int overr
21b38 69 64 65 45 72 72 6f 72 2c 20 20 2f 2a 20 4f 76  ideError,  /* Ov
21b39 65 72 72 69 64 65 20 6f 6e 45 72 72 6f 72 20 74  erride onError t
21b3a 6f 20 74 68 69 73 20 69 66 20 6e 6f 74 20 4f 45  o this if not OE
21b3b 5f 44 65 66 61 75 6c 74 20 2a 2f 0a 20 20 69 6e  _Default */.  in
21b3c 74 20 69 67 6e 6f 72 65 44 65 73 74 20 20 20 20  t ignoreDest    
21b3d 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 74 68 69    /* Jump to thi
21b3e 73 20 6c 61 62 65 6c 20 6f 6e 20 61 6e 20 4f 45  s label on an OE
21b3f 5f 49 67 6e 6f 72 65 20 72 65 73 6f 6c 75 74 69  _Ignore resoluti
21b40 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  on */.){.  int i
21b41 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69  ;.  Vdbe *v;.  i
21b42 6e 74 20 6e 43 6f 6c 3b 0a 20 20 69 6e 74 20 6f  nt nCol;.  int o
21b43 6e 45 72 72 6f 72 3b 0a 20 20 69 6e 74 20 6a 31  nError;.  int j1
21b44 2c 20 6a 32 2c 20 6a 33 3b 20 20 20 20 20 2f 2a  , j2, j3;     /*
21b45 20 41 64 64 72 65 73 73 65 73 20 6f 66 20 6a 75   Addresses of ju
21b46 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20  mp instructions 
21b47 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61  */.  int regData
21b48 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69  ;        /* Regi
21b49 73 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ster containing 
21b4a 66 69 72 73 74 20 64 61 74 61 20 63 6f 6c 75 6d  first data colum
21b4b 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b  n */.  int iCur;
21b4c 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a  .  Index *pIdx;.
21b4d 20 20 69 6e 74 20 73 65 65 6e 52 65 70 6c 61 63    int seenReplac
21b4e 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 68 61 73  e = 0;.  int has
21b4f 54 77 6f 52 6f 77 69 64 73 20 3d 20 28 69 73 55  TwoRowids = (isU
21b50 70 64 61 74 65 20 26 26 20 72 6f 77 69 64 43 68  pdate && rowidCh
21b51 6e 67 29 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69  ng);..  v = sqli
21b52 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
21b53 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21  e);.  assert( v!
21b54 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
21b55 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30  pTab->pSelect==0
21b56 20 29 3b 20 20 2f 2a 20 54 68 69 73 20 74 61 62   );  /* This tab
21b57 6c 65 20 69 73 20 6e 6f 74 20 61 20 56 49 45 57  le is not a VIEW
21b58 20 2a 2f 0a 20 20 6e 43 6f 6c 20 3d 20 70 54 61   */.  nCol = pTa
21b59 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 72 65 67 44 61  b->nCol;.  regDa
21b5a 74 61 20 3d 20 72 65 67 52 6f 77 69 64 20 2b 20  ta = regRowid + 
21b5b 31 3b 0a 0a 0a 20 20 2f 2a 20 54 65 73 74 20 61  1;...  /* Test a
21b5c 6c 6c 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73  ll NOT NULL cons
21b5d 74 72 61 69 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20  traints..  */.  
21b5e 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b  for(i=0; i<nCol;
21b5f 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69   i++){.    if( i
21b60 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b  ==pTab->iPKey ){
21b61 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b  .      continue;
21b62 0a 20 20 20 20 7d 0a 20 20 20 20 6f 6e 45 72 72  .    }.    onErr
21b63 6f 72 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  or = pTab->aCol[
21b64 69 5d 2e 6e 6f 74 4e 75 6c 6c 3b 0a 20 20 20 20  i].notNull;.    
21b65 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  if( onError==OE_
21b66 4e 6f 6e 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b  None ) continue;
21b67 0a 20 20 20 20 69 66 28 20 6f 76 65 72 72 69 64  .    if( overrid
21b68 65 45 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75  eError!=OE_Defau
21b69 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72  lt ){.      onEr
21b6a 72 6f 72 20 3d 20 6f 76 65 72 72 69 64 65 45 72  ror = overrideEr
21b6b 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ror;.    }else i
21b6c 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44  f( onError==OE_D
21b6d 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20  efault ){.      
21b6e 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f  onError = OE_Abo
21b6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  rt;.    }.    if
21b70 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65  ( onError==OE_Re
21b71 70 6c 61 63 65 20 26 26 20 70 54 61 62 2d 3e 61  place && pTab->a
21b72 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 3d 3d 30 20  Col[i].pDflt==0 
21b73 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72  ){.      onError
21b74 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20   = OE_Abort;.   
21b75 20 7d 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69   }.    j1 = sqli
21b76 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
21b77 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67   OP_NotNull, reg
21b78 44 61 74 61 2b 69 29 3b 0a 20 20 20 20 61 73 73  Data+i);.    ass
21b79 65 72 74 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45  ert( onError==OE
21b7a 5f 52 6f 6c 6c 62 61 63 6b 20 7c 7c 20 6f 6e 45  _Rollback || onE
21b7b 72 72 6f 72 3d 3d 4f 45 5f 41 62 6f 72 74 20 7c  rror==OE_Abort |
21b7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 46 61  | onError==OE_Fa
21b7d 69 6c 0a 20 20 20 20 20 20 20 20 7c 7c 20 6f 6e  il.        || on
21b7e 45 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65  Error==OE_Ignore
21b7f 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f   || onError==OE_
21b80 52 65 70 6c 61 63 65 20 29 3b 0a 20 20 20 20 73  Replace );.    s
21b81 77 69 74 63 68 28 20 6f 6e 45 72 72 6f 72 20 29  witch( onError )
21b82 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f  {.      case OE_
21b83 52 6f 6c 6c 62 61 63 6b 3a 0a 20 20 20 20 20 20  Rollback:.      
21b84 63 61 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a 20  case OE_Abort:. 
21b85 20 20 20 20 20 63 61 73 65 20 4f 45 5f 46 61 69       case OE_Fai
21b86 6c 3a 20 7b 0a 20 20 20 20 20 20 20 20 63 68 61  l: {.        cha
21b87 72 20 2a 7a 4d 73 67 3b 0a 20 20 20 20 20 20 20  r *zMsg;.       
21b88 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21b89 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53  p2(v, OP_Halt, S
21b8a 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
21b8b 2c 20 6f 6e 45 72 72 6f 72 29 3b 0a 20 20 20 20  , onError);.    
21b8c 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74      zMsg = sqlit
21b8d 65 33 4d 50 72 69 6e 74 66 28 70 50 61 72 73 65  e3MPrintf(pParse
21b8e 2d 3e 64 62 2c 20 22 25 73 2e 25 73 20 6d 61 79  ->db, "%s.%s may
21b8f 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 22 2c 0a 20   not be NULL",. 
21b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21b91 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54 61               pTa
21b92 62 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e  b->zName, pTab->
21b93 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a  aCol[i].zName);.
21b94 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
21b95 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d  dbeChangeP4(v, -
21b96 31 2c 20 7a 4d 73 67 2c 20 50 34 5f 44 59 4e 41  1, zMsg, P4_DYNA
21b97 4d 49 43 29 3b 0a 20 20 20 20 20 20 20 20 62 72  MIC);.        br
21b98 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
21b99 20 20 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72     case OE_Ignor
21b9a 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  e: {.        sql
21b9b 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
21b9c 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 67  , OP_Goto, 0, ig
21b9d 6e 6f 72 65 44 65 73 74 29 3b 0a 20 20 20 20 20  noreDest);.     
21b9e 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
21b9f 7d 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f  }.      case OE_
21ba0 52 65 70 6c 61 63 65 3a 20 7b 0a 20 20 20 20 20  Replace: {.     
21ba1 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
21ba2 64 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d  de(pParse, pTab-
21ba3 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 2c 20  >aCol[i].pDflt, 
21ba4 72 65 67 44 61 74 61 2b 69 29 3b 0a 20 20 20 20  regData+i);.    
21ba5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
21ba6 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c   }.    }.    sql
21ba7 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
21ba8 28 76 2c 20 6a 31 29 3b 0a 20 20 7d 0a 0a 20 20  (v, j1);.  }..  
21ba9 2f 2a 20 54 65 73 74 20 61 6c 6c 20 43 48 45 43  /* Test all CHEC
21baa 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20  K constraints.  
21bab 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
21bac 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 69  E_OMIT_CHECK.  i
21bad 66 28 20 70 54 61 62 2d 3e 70 43 68 65 63 6b 20  f( pTab->pCheck 
21bae 26 26 20 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e  && (pParse->db->
21baf 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 49  flags & SQLITE_I
21bb0 67 6e 6f 72 65 43 68 65 63 6b 73 29 3d 3d 30 20  gnoreChecks)==0 
21bb1 29 7b 0a 20 20 20 20 69 6e 74 20 61 6c 6c 4f 6b  ){.    int allOk
21bb2 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
21bb3 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20  keLabel(v);.    
21bb4 70 50 61 72 73 65 2d 3e 63 6b 42 61 73 65 20 3d  pParse->ckBase =
21bb5 20 72 65 67 44 61 74 61 3b 0a 20 20 20 20 73 71   regData;.    sq
21bb6 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28  lite3ExprIfTrue(
21bb7 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 70 43  pParse, pTab->pC
21bb8 68 65 63 6b 2c 20 61 6c 6c 4f 6b 2c 20 53 51 4c  heck, allOk, SQL
21bb9 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b  ITE_JUMPIFNULL);
21bba 0a 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 6f  .    onError = o
21bbb 76 65 72 72 69 64 65 45 72 72 6f 72 21 3d 4f 45  verrideError!=OE
21bbc 5f 44 65 66 61 75 6c 74 20 3f 20 6f 76 65 72 72  _Default ? overr
21bbd 69 64 65 45 72 72 6f 72 20 3a 20 4f 45 5f 41 62  ideError : OE_Ab
21bbe 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 6f 6e 45  ort;.    if( onE
21bbf 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20  rror==OE_Ignore 
21bc0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
21bc1 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
21bc2 5f 47 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f 72 65  _Goto, 0, ignore
21bc3 44 65 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Dest);.    }else
21bc4 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
21bc5 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
21bc6 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f 43 4f 4e  Halt, SQLITE_CON
21bc7 53 54 52 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72  STRAINT, onError
21bc8 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
21bc9 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
21bca 61 62 65 6c 28 76 2c 20 61 6c 6c 4f 6b 29 3b 0a  abel(v, allOk);.
21bcb 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64    }.#endif /* !d
21bcc 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
21bcd 49 54 5f 43 48 45 43 4b 29 20 2a 2f 0a 0a 20 20  IT_CHECK) */..  
21bce 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 61 6e  /* If we have an
21bcf 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
21bd0 20 4b 45 59 2c 20 6d 61 6b 65 20 73 75 72 65 20   KEY, make sure 
21bd1 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 0a  the primary key.
21bd2 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77 20    ** of the new 
21bd3 72 65 63 6f 72 64 20 64 6f 65 73 20 6e 6f 74 20  record does not 
21bd4 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69 73 74  previously exist
21bd5 2e 20 20 45 78 63 65 70 74 2c 20 69 66 20 74 68  .  Except, if th
21bd6 69 73 0a 20 20 2a 2a 20 69 73 20 61 6e 20 55 50  is.  ** is an UP
21bd7 44 41 54 45 20 61 6e 64 20 74 68 65 20 70 72 69  DATE and the pri
21bd8 6d 61 72 79 20 6b 65 79 20 69 73 20 6e 6f 74 20  mary key is not 
21bd9 63 68 61 6e 67 69 6e 67 2c 20 74 68 61 74 20 69  changing, that i
21bda 73 20 4f 4b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  s OK..  */.  if(
21bdb 20 72 6f 77 69 64 43 68 6e 67 20 29 7b 0a 20 20   rowidChng ){.  
21bdc 20 20 6f 6e 45 72 72 6f 72 20 3d 20 70 54 61 62    onError = pTab
21bdd 2d 3e 6b 65 79 43 6f 6e 66 3b 0a 20 20 20 20 69  ->keyConf;.    i
21bde 66 28 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72  f( overrideError
21bdf 21 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a  !=OE_Default ){.
21be0 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20        onError = 
21be1 6f 76 65 72 72 69 64 65 45 72 72 6f 72 3b 0a 20  overrideError;. 
21be2 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 6e 45     }else if( onE
21be3 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74  rror==OE_Default
21be4 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f   ){.      onErro
21be5 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20  r = OE_Abort;.  
21be6 20 20 7d 0a 20 20 20 20 0a 20 20 20 20 69 66 28    }.    .    if(
21be7 20 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 52 65 70   onError!=OE_Rep
21be8 6c 61 63 65 20 7c 7c 20 70 54 61 62 2d 3e 70 49  lace || pTab->pI
21be9 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 20 69 66  ndex ){.      if
21bea 28 20 69 73 55 70 64 61 74 65 20 29 7b 0a 20 20  ( isUpdate ){.  
21beb 20 20 20 20 20 20 6a 32 20 3d 20 73 71 6c 69 74        j2 = sqlit
21bec 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
21bed 4f 50 5f 45 71 2c 20 72 65 67 52 6f 77 69 64 2c  OP_Eq, regRowid,
21bee 20 30 2c 20 72 65 67 52 6f 77 69 64 2d 31 29 3b   0, regRowid-1);
21bef 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6a  .      }.      j
21bf0 33 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  3 = sqlite3VdbeA
21bf1 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45  ddOp3(v, OP_NotE
21bf2 78 69 73 74 73 2c 20 62 61 73 65 43 75 72 2c 20  xists, baseCur, 
21bf3 30 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  0, regRowid);.  
21bf4 20 20 20 20 73 77 69 74 63 68 28 20 6f 6e 45 72      switch( onEr
21bf5 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 20 20 64  ror ){.        d
21bf6 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
21bf7 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45      onError = OE
21bf8 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 20 20  _Abort;.        
21bf9 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 69    /* Fall thru i
21bfa 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73  nto the next cas
21bfb 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20  e */.        }. 
21bfc 20 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52         case OE_R
21bfd 6f 6c 6c 62 61 63 6b 3a 0a 20 20 20 20 20 20 20  ollback:.       
21bfe 20 63 61 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a   case OE_Abort:.
21bff 20 20 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f          case OE_
21c00 46 61 69 6c 3a 20 7b 0a 20 20 20 20 20 20 20 20  Fail: {.        
21c01 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21c02 4f 70 34 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20  Op4(v, OP_Halt, 
21c03 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
21c04 54 2c 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 0a 20  T, onError, 0,. 
21c05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c06 20 20 20 20 20 20 20 20 20 20 22 50 52 49 4d 41            "PRIMA
21c07 52 59 20 4b 45 59 20 6d 75 73 74 20 62 65 20 75  RY KEY must be u
21c08 6e 69 71 75 65 22 2c 20 50 34 5f 53 54 41 54 49  nique", P4_STATI
21c09 43 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72  C);.          br
21c0a 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
21c0b 20 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52         case OE_R
21c0c 65 70 6c 61 63 65 3a 20 7b 0a 20 20 20 20 20 20  eplace: {.      
21c0d 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72      sqlite3Gener
21c0e 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74  ateRowIndexDelet
21c0f 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  e(pParse, pTab, 
21c10 62 61 73 65 43 75 72 2c 20 30 29 3b 0a 20 20 20  baseCur, 0);.   
21c11 20 20 20 20 20 20 20 73 65 65 6e 52 65 70 6c 61         seenRepla
21c12 63 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ce = 1;.        
21c13 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
21c14 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20   }.        case 
21c15 4f 45 5f 49 67 6e 6f 72 65 3a 20 7b 0a 20 20 20  OE_Ignore: {.   
21c16 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73         assert( s
21c17 65 65 6e 52 65 70 6c 61 63 65 3d 3d 30 20 29 3b  eenReplace==0 );
21c18 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
21c19 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
21c1a 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f  OP_Goto, 0, igno
21c1b 72 65 44 65 73 74 29 3b 0a 20 20 20 20 20 20 20  reDest);.       
21c1c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
21c1d 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
21c1e 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
21c1f 70 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a 20 20  pHere(v, j3);.  
21c20 20 20 20 20 69 66 28 20 69 73 55 70 64 61 74 65      if( isUpdate
21c21 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
21c22 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
21c23 76 2c 20 6a 32 29 3b 0a 20 20 20 20 20 20 7d 0a  v, j2);.      }.
21c24 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
21c25 54 65 73 74 20 61 6c 6c 20 55 4e 49 51 55 45 20  Test all UNIQUE 
21c26 63 6f 6e 73 74 72 61 69 6e 74 73 20 62 79 20 63  constraints by c
21c27 72 65 61 74 69 6e 67 20 65 6e 74 72 69 65 73 20  reating entries 
21c28 66 6f 72 20 65 61 63 68 20 55 4e 49 51 55 45 0a  for each UNIQUE.
21c29 20 20 2a 2a 20 69 6e 64 65 78 20 61 6e 64 20 6d    ** index and m
21c2a 61 6b 69 6e 67 20 73 75 72 65 20 74 68 61 74 20  aking sure that 
21c2b 64 75 70 6c 69 63 61 74 65 20 65 6e 74 72 69 65  duplicate entrie
21c2c 73 20 64 6f 20 6e 6f 74 20 61 6c 72 65 61 64 79  s do not already
21c2d 20 65 78 69 73 74 2e 0a 20 20 2a 2a 20 41 64 64   exist..  ** Add
21c2e 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 73   the new records
21c2f 20 74 6f 20 74 68 65 20 69 6e 64 69 63 65 73 20   to the indices 
21c30 61 73 20 77 65 20 67 6f 2e 0a 20 20 2a 2f 0a 20  as we go..  */. 
21c31 20 66 6f 72 28 69 43 75 72 3d 30 2c 20 70 49 64   for(iCur=0, pId
21c32 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
21c33 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
21c34 3e 70 4e 65 78 74 2c 20 69 43 75 72 2b 2b 29 7b  >pNext, iCur++){
21c35 0a 20 20 20 20 69 6e 74 20 72 65 67 49 64 78 3b  .    int regIdx;
21c36 0a 20 20 20 20 69 6e 74 20 72 65 67 52 3b 0a 0a  .    int regR;..
21c37 20 20 20 20 69 66 28 20 61 52 65 67 49 64 78 5b      if( aRegIdx[
21c38 69 43 75 72 5d 3d 3d 30 20 29 20 63 6f 6e 74 69  iCur]==0 ) conti
21c39 6e 75 65 3b 20 20 2f 2a 20 53 6b 69 70 20 75 6e  nue;  /* Skip un
21c3a 75 73 65 64 20 69 6e 64 69 63 65 73 20 2a 2f 0a  used indices */.
21c3b 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61  .    /* Create a
21c3c 20 6b 65 79 20 66 6f 72 20 61 63 63 65 73 73 69   key for accessi
21c3d 6e 67 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74  ng the index ent
21c3e 72 79 20 2a 2f 0a 20 20 20 20 72 65 67 49 64 78  ry */.    regIdx
21c3f 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
21c40 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 70  pRange(pParse, p
21c41 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b  Idx->nColumn+1);
21c42 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
21c43 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69  pIdx->nColumn; i
21c44 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  ++){.      int i
21c45 64 78 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c  dx = pIdx->aiCol
21c46 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66  umn[i];.      if
21c47 28 20 69 64 78 3d 3d 70 54 61 62 2d 3e 69 50 4b  ( idx==pTab->iPK
21c48 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ey ){.        sq
21c49 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
21c4a 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67  v, OP_SCopy, reg
21c4b 52 6f 77 69 64 2c 20 72 65 67 49 64 78 2b 69 29  Rowid, regIdx+i)
21c4c 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
21c4d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
21c4e 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
21c4f 43 6f 70 79 2c 20 72 65 67 44 61 74 61 2b 69 64  Copy, regData+id
21c50 78 2c 20 72 65 67 49 64 78 2b 69 29 3b 0a 20 20  x, regIdx+i);.  
21c51 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
21c52 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21c53 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72  2(v, OP_SCopy, r
21c54 65 67 52 6f 77 69 64 2c 20 72 65 67 49 64 78 2b  egRowid, regIdx+
21c55 69 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  i);.    sqlite3V
21c56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
21c57 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 49  MakeRecord, regI
21c58 64 78 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  dx, pIdx->nColum
21c59 6e 2b 31 2c 20 61 52 65 67 49 64 78 5b 69 43 75  n+1, aRegIdx[iCu
21c5a 72 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  r]);.    sqlite3
21c5b 49 6e 64 65 78 41 66 66 69 6e 69 74 79 53 74 72  IndexAffinityStr
21c5c 28 76 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 73  (v, pIdx);.    s
21c5d 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
21c5e 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50  ffinityChange(pP
21c5f 61 72 73 65 2c 20 72 65 67 49 64 78 2c 20 70 49  arse, regIdx, pI
21c60 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a  dx->nColumn+1);.
21c61 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
21c62 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  seTempRange(pPar
21c63 73 65 2c 20 72 65 67 49 64 78 2c 20 70 49 64 78  se, regIdx, pIdx
21c64 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 0a 20  ->nColumn+1);.. 
21c65 20 20 20 2f 2a 20 46 69 6e 64 20 6f 75 74 20 77     /* Find out w
21c66 68 61 74 20 61 63 74 69 6f 6e 20 74 6f 20 74 61  hat action to ta
21c67 6b 65 20 69 6e 20 63 61 73 65 20 74 68 65 72 65  ke in case there
21c68 20 69 73 20 61 6e 20 69 6e 64 65 78 69 6e 67 20   is an indexing 
21c69 63 6f 6e 66 6c 69 63 74 20 2a 2f 0a 20 20 20 20  conflict */.    
21c6a 6f 6e 45 72 72 6f 72 20 3d 20 70 49 64 78 2d 3e  onError = pIdx->
21c6b 6f 6e 45 72 72 6f 72 3b 0a 20 20 20 20 69 66 28  onError;.    if(
21c6c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e   onError==OE_Non
21c6d 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20 2f  e ) continue;  /
21c6e 2a 20 70 49 64 78 20 69 73 20 6e 6f 74 20 61 20  * pIdx is not a 
21c6f 55 4e 49 51 55 45 20 69 6e 64 65 78 20 2a 2f 0a  UNIQUE index */.
21c70 20 20 20 20 69 66 28 20 6f 76 65 72 72 69 64 65      if( override
21c71 45 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75 6c  Error!=OE_Defaul
21c72 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72  t ){.      onErr
21c73 6f 72 20 3d 20 6f 76 65 72 72 69 64 65 45 72 72  or = overrideErr
21c74 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  or;.    }else if
21c75 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65  ( onError==OE_De
21c76 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f  fault ){.      o
21c77 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72  nError = OE_Abor
21c78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  t;.    }.    if(
21c79 20 73 65 65 6e 52 65 70 6c 61 63 65 20 29 7b 0a   seenReplace ){.
21c7a 20 20 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f        if( onErro
21c7b 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 29 20 6f  r==OE_Ignore ) o
21c7c 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 52 65 70 6c  nError = OE_Repl
21c7d 61 63 65 3b 0a 20 20 20 20 20 20 65 6c 73 65 20  ace;.      else 
21c7e 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  if( onError==OE_
21c7f 46 61 69 6c 20 29 20 6f 6e 45 72 72 6f 72 20 3d  Fail ) onError =
21c80 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d   OE_Abort;.    }
21c81 0a 20 20 20 20 0a 0a 20 20 20 20 2f 2a 20 43 68  .    ..    /* Ch
21c82 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68  eck to see if th
21c83 65 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72  e new index entr
21c84 79 20 77 69 6c 6c 20 62 65 20 75 6e 69 71 75 65  y will be unique
21c85 20 2a 2f 0a 20 20 20 20 6a 32 20 3d 20 73 71 6c   */.    j2 = sql
21c86 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
21c87 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67  , OP_IsNull, reg
21c88 49 64 78 2c 20 30 2c 20 70 49 64 78 2d 3e 6e 43  Idx, 0, pIdx->nC
21c89 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 72 65 67 52  olumn);.    regR
21c8a 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
21c8b 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
21c8c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21c8d 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c  Op2(v, OP_SCopy,
21c8e 20 72 65 67 52 6f 77 69 64 2d 68 61 73 54 77 6f   regRowid-hasTwo
21c8f 52 6f 77 69 64 73 2c 20 72 65 67 52 29 3b 0a 20  Rowids, regR);. 
21c90 20 20 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56     j3 = sqlite3V
21c91 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
21c92 49 73 55 6e 69 71 75 65 2c 20 62 61 73 65 43 75  IsUnique, baseCu
21c93 72 2b 69 43 75 72 2b 31 2c 20 30 2c 0a 20 20 20  r+iCur+1, 0,.   
21c94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c95 20 20 20 20 20 20 20 20 72 65 67 52 2c 20 53 51          regR, SQ
21c96 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28  LITE_INT_TO_PTR(
21c97 61 52 65 67 49 64 78 5b 69 43 75 72 5d 29 2c 0a  aRegIdx[iCur]),.
21c98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c99 20 20 20 20 20 20 20 20 20 20 20 50 34 5f 49 4e             P4_IN
21c9a 54 33 32 29 3b 0a 0a 20 20 20 20 2f 2a 20 47 65  T32);..    /* Ge
21c9b 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
21c9c 20 65 78 65 63 75 74 65 73 20 69 66 20 74 68 65   executes if the
21c9d 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72 79   new index entry
21c9e 20 69 73 20 6e 6f 74 20 75 6e 69 71 75 65 20 2a   is not unique *
21c9f 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f 6e  /.    assert( on
21ca0 45 72 72 6f 72 3d 3d 4f 45 5f 52 6f 6c 6c 62 61  Error==OE_Rollba
21ca1 63 6b 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f  ck || onError==O
21ca2 45 5f 41 62 6f 72 74 20 7c 7c 20 6f 6e 45 72 72  E_Abort || onErr
21ca3 6f 72 3d 3d 4f 45 5f 46 61 69 6c 0a 20 20 20 20  or==OE_Fail.    
21ca4 20 20 20 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d      || onError==
21ca5 4f 45 5f 49 67 6e 6f 72 65 20 7c 7c 20 6f 6e 45  OE_Ignore || onE
21ca6 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65  rror==OE_Replace
21ca7 20 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20   );.    switch( 
21ca8 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 20  onError ){.     
21ca9 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c 62 61 63   case OE_Rollbac
21caa 6b 3a 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45  k:.      case OE
21cab 5f 41 62 6f 72 74 3a 0a 20 20 20 20 20 20 63 61  _Abort:.      ca
21cac 73 65 20 4f 45 5f 46 61 69 6c 3a 20 7b 0a 20 20  se OE_Fail: {.  
21cad 20 20 20 20 20 20 69 6e 74 20 6a 2c 20 6e 31 2c        int j, n1,
21cae 20 6e 32 3b 0a 20 20 20 20 20 20 20 20 63 68 61   n2;.        cha
21caf 72 20 7a 45 72 72 4d 73 67 5b 32 30 30 5d 3b 0a  r zErrMsg[200];.
21cb0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
21cb1 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
21cb2 7a 45 72 72 4d 73 67 29 2c 20 7a 45 72 72 4d 73  zErrMsg), zErrMs
21cb3 67 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  g,.             
21cb4 20 20 20 20 20 20 20 20 20 20 20 20 70 49 64 78              pIdx
21cb5 2d 3e 6e 43 6f 6c 75 6d 6e 3e 31 20 3f 20 22 63  ->nColumn>1 ? "c
21cb6 6f 6c 75 6d 6e 73 20 22 20 3a 20 22 63 6f 6c 75  olumns " : "colu
21cb7 6d 6e 20 22 29 3b 0a 20 20 20 20 20 20 20 20 6e  mn ");.        n
21cb8 31 20 3d 20 73 74 72 6c 65 6e 28 7a 45 72 72 4d  1 = strlen(zErrM
21cb9 73 67 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  sg);.        for
21cba 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43  (j=0; j<pIdx->nC
21cbb 6f 6c 75 6d 6e 20 26 26 20 6e 31 3c 73 69 7a 65  olumn && n1<size
21cbc 6f 66 28 7a 45 72 72 4d 73 67 29 2d 33 30 3b 20  of(zErrMsg)-30; 
21cbd 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  j++){.          
21cbe 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 70 54 61  char *zCol = pTa
21cbf 62 2d 3e 61 43 6f 6c 5b 70 49 64 78 2d 3e 61 69  b->aCol[pIdx->ai
21cc0 43 6f 6c 75 6d 6e 5b 6a 5d 5d 2e 7a 4e 61 6d 65  Column[j]].zName
21cc1 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 32 20 3d  ;.          n2 =
21cc2 20 73 74 72 6c 65 6e 28 7a 43 6f 6c 29 3b 0a 20   strlen(zCol);. 
21cc3 20 20 20 20 20 20 20 20 20 69 66 28 20 6a 3e 30           if( j>0
21cc4 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
21cc5 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
21cc6 28 73 69 7a 65 6f 66 28 7a 45 72 72 4d 73 67 29  (sizeof(zErrMsg)
21cc7 2d 6e 31 2c 20 26 7a 45 72 72 4d 73 67 5b 6e 31  -n1, &zErrMsg[n1
21cc8 5d 2c 20 22 2c 20 22 29 3b 0a 20 20 20 20 20 20  ], ", ");.      
21cc9 20 20 20 20 20 20 6e 31 20 2b 3d 20 32 3b 0a 20        n1 += 2;. 
21cca 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
21ccb 20 20 20 20 20 69 66 28 20 6e 31 2b 6e 32 3e 73       if( n1+n2>s
21ccc 69 7a 65 6f 66 28 7a 45 72 72 4d 73 67 29 2d 33  izeof(zErrMsg)-3
21ccd 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
21cce 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
21ccf 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 4d 73 67  f(sizeof(zErrMsg
21cd0 29 2d 6e 31 2c 20 26 7a 45 72 72 4d 73 67 5b 6e  )-n1, &zErrMsg[n
21cd1 31 5d 2c 20 22 2e 2e 2e 22 29 3b 0a 20 20 20 20  1], "...");.    
21cd2 20 20 20 20 20 20 20 20 6e 31 20 2b 3d 20 33 3b          n1 += 3;
21cd3 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
21cd4 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65  ak;.          }e
21cd5 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
21cd6 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
21cd7 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 4d 73 67  f(sizeof(zErrMsg
21cd8 29 2d 6e 31 2c 20 26 7a 45 72 72 4d 73 67 5b 6e  )-n1, &zErrMsg[n
21cd9 31 5d 2c 20 22 25 73 22 2c 20 7a 43 6f 6c 29 3b  1], "%s", zCol);
21cda 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 31 20  .            n1 
21cdb 2b 3d 20 6e 32 3b 0a 20 20 20 20 20 20 20 20 20  += n2;.         
21cdc 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
21cdd 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
21cde 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72  rintf(sizeof(zEr
21cdf 72 4d 73 67 29 2d 6e 31 2c 20 26 7a 45 72 72 4d  rMsg)-n1, &zErrM
21ce0 73 67 5b 6e 31 5d 2c 20 0a 20 20 20 20 20 20 20  sg[n1], .       
21ce1 20 20 20 20 20 70 49 64 78 2d 3e 6e 43 6f 6c 75       pIdx->nColu
21ce2 6d 6e 3e 31 20 3f 20 22 20 61 72 65 20 6e 6f 74  mn>1 ? " are not
21ce3 20 75 6e 69 71 75 65 22 20 3a 20 22 20 69 73 20   unique" : " is 
21ce4 6e 6f 74 20 75 6e 69 71 75 65 22 29 3b 0a 20 20  not unique");.  
21ce5 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21ce6 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 48 61  eAddOp4(v, OP_Ha
21ce7 6c 74 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54  lt, SQLITE_CONST
21ce8 52 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 2c 20  RAINT, onError, 
21ce9 30 2c 20 7a 45 72 72 4d 73 67 2c 30 29 3b 0a 20  0, zErrMsg,0);. 
21cea 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
21ceb 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
21cec 20 4f 45 5f 49 67 6e 6f 72 65 3a 20 7b 0a 20 20   OE_Ignore: {.  
21ced 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 65        assert( se
21cee 65 6e 52 65 70 6c 61 63 65 3d 3d 30 20 29 3b 0a  enReplace==0 );.
21cef 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
21cf0 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
21cf1 47 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f 72 65 44  Goto, 0, ignoreD
21cf2 65 73 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72  est);.        br
21cf3 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
21cf4 20 20 20 63 61 73 65 20 4f 45 5f 52 65 70 6c 61     case OE_Repla
21cf5 63 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71  ce: {.        sq
21cf6 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77  lite3GenerateRow
21cf7 44 65 6c 65 74 65 28 70 50 61 72 73 65 2c 20 70  Delete(pParse, p
21cf8 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20 72 65  Tab, baseCur, re
21cf9 67 52 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  gR, 0);.        
21cfa 73 65 65 6e 52 65 70 6c 61 63 65 20 3d 20 31 3b  seenReplace = 1;
21cfb 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
21cfc 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
21cfd 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
21cfe 70 48 65 72 65 28 76 2c 20 6a 32 29 3b 0a 20 20  pHere(v, j2);.  
21cff 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
21d00 70 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a 20 20  pHere(v, j3);.  
21d01 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
21d02 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
21d03 72 65 67 52 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  regR);.  }.}../*
21d04 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
21d05 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20   generates code 
21d06 74 6f 20 66 69 6e 69 73 68 20 74 68 65 20 49 4e  to finish the IN
21d07 53 45 52 54 20 6f 72 20 55 50 44 41 54 45 20 6f  SERT or UPDATE o
21d08 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74  peration.** that
21d09 20 77 61 73 20 73 74 61 72 74 65 64 20 62 79 20   was started by 
21d0a 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20  a prior call to 
21d0b 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43  sqlite3GenerateC
21d0c 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73 2e  onstraintChecks.
21d0d 0a 2a 2a 20 41 20 63 6f 6e 73 65 63 75 74 69 76  .** A consecutiv
21d0e 65 20 72 61 6e 67 65 20 6f 66 20 72 65 67 69 73  e range of regis
21d0f 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 61 74  ters starting at
21d10 20 72 65 67 52 6f 77 69 64 20 63 6f 6e 74 61 69   regRowid contai
21d11 6e 73 20 74 68 65 0a 2a 2a 20 72 6f 77 69 64 20  ns the.** rowid 
21d12 61 6e 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  and the content 
21d13 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 2e 0a  to be inserted..
21d14 2a 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65  **.** The argume
21d15 6e 74 73 20 74 6f 20 74 68 69 73 20 72 6f 75 74  nts to this rout
21d16 69 6e 65 20 73 68 6f 75 6c 64 20 62 65 20 74 68  ine should be th
21d17 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 66 69  e same as the fi
21d18 72 73 74 20 73 69 78 0a 2a 2a 20 61 72 67 75 6d  rst six.** argum
21d19 65 6e 74 73 20 74 6f 20 73 71 6c 69 74 65 33 47  ents to sqlite3G
21d1a 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e  enerateConstrain
21d1b 74 43 68 65 63 6b 73 2e 0a 2a 2f 0a 53 51 4c 49  tChecks..*/.SQLI
21d1c 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
21d1d 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65 49  sqlite3CompleteI
21d1e 6e 73 65 72 74 69 6f 6e 28 0a 20 20 50 61 72 73  nsertion(.  Pars
21d1f 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
21d20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f  /* The parser co
21d21 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65  ntext */.  Table
21d22 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 2f   *pTab,        /
21d23 2a 20 74 68 65 20 74 61 62 6c 65 20 69 6e 74 6f  * the table into
21d24 20 77 68 69 63 68 20 77 65 20 61 72 65 20 69 6e   which we are in
21d25 73 65 72 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74  serting */.  int
21d26 20 62 61 73 65 43 75 72 2c 20 20 20 20 20 20 20   baseCur,       
21d27 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 61 20 72   /* Index of a r
21d28 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72  ead/write cursor
21d29 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 70 54 61   pointing at pTa
21d2a 62 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f  b */.  int regRo
21d2b 77 69 64 2c 20 20 20 20 20 20 20 2f 2a 20 52 61  wid,       /* Ra
21d2c 6e 67 65 20 6f 66 20 63 6f 6e 74 65 6e 74 20 2a  nge of content *
21d2d 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67 49 64 78  /.  int *aRegIdx
21d2e 2c 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73  ,       /* Regis
21d2f 74 65 72 20 75 73 65 64 20 62 79 20 65 61 63 68  ter used by each
21d30 20 69 6e 64 65 78 2e 20 20 30 20 66 6f 72 20 75   index.  0 for u
21d31 6e 75 73 65 64 20 69 6e 64 69 63 65 73 20 2a 2f  nused indices */
21d32 0a 20 20 69 6e 74 20 72 6f 77 69 64 43 68 6e 67  .  int rowidChng
21d33 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  ,      /* True i
21d34 66 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d  f the record num
21d35 62 65 72 20 77 69 6c 6c 20 63 68 61 6e 67 65 20  ber will change 
21d36 2a 2f 0a 20 20 69 6e 74 20 69 73 55 70 64 61 74  */.  int isUpdat
21d37 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  e,       /* True
21d38 20 66 6f 72 20 55 50 44 41 54 45 2c 20 46 61 6c   for UPDATE, Fal
21d39 73 65 20 66 6f 72 20 49 4e 53 45 52 54 20 2a 2f  se for INSERT */
21d3a 0a 20 20 69 6e 74 20 6e 65 77 49 64 78 2c 20 20  .  int newIdx,  
21d3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
21d3c 6f 66 20 4e 45 57 20 74 61 62 6c 65 20 66 6f 72  of NEW table for
21d3d 20 74 72 69 67 67 65 72 73 2e 20 20 2d 31 20 69   triggers.  -1 i
21d3e 66 20 6e 6f 6e 65 20 2a 2f 0a 20 20 69 6e 74 20  f none */.  int 
21d3f 61 70 70 65 6e 64 42 69 61 73 20 20 20 20 20 20  appendBias      
21d40 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20  /* True if this 
21d41 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20  is likely to be 
21d42 61 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 29 7b 0a  an append */.){.
21d43 20 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 20    int i;.  Vdbe 
21d44 2a 76 3b 0a 20 20 69 6e 74 20 6e 49 64 78 3b 0a  *v;.  int nIdx;.
21d45 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20    Index *pIdx;. 
21d46 20 69 6e 74 20 70 69 6b 5f 66 6c 61 67 73 3b 0a   int pik_flags;.
21d47 20 20 69 6e 74 20 72 65 67 44 61 74 61 3b 0a 20    int regData;. 
21d48 20 69 6e 74 20 72 65 67 52 65 63 3b 0a 0a 20 20   int regRec;..  
21d49 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
21d4a 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 61 73  be(pParse);.  as
21d4b 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20  sert( v!=0 );.  
21d4c 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e 70 53  assert( pTab->pS
21d4d 65 6c 65 63 74 3d 3d 30 20 29 3b 20 20 2f 2a 20  elect==0 );  /* 
21d4e 54 68 69 73 20 74 61 62 6c 65 20 69 73 20 6e 6f  This table is no
21d4f 74 20 61 20 56 49 45 57 20 2a 2f 0a 20 20 66 6f  t a VIEW */.  fo
21d50 72 28 6e 49 64 78 3d 30 2c 20 70 49 64 78 3d 70  r(nIdx=0, pIdx=p
21d51 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
21d52 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
21d53 65 78 74 2c 20 6e 49 64 78 2b 2b 29 7b 7d 0a 20  ext, nIdx++){}. 
21d54 20 66 6f 72 28 69 3d 6e 49 64 78 2d 31 3b 20 69   for(i=nIdx-1; i
21d55 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69  >=0; i--){.    i
21d56 66 28 20 61 52 65 67 49 64 78 5b 69 5d 3d 3d 30  f( aRegIdx[i]==0
21d57 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
21d58 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21d59 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65  p2(v, OP_IdxInse
21d5a 72 74 2c 20 62 61 73 65 43 75 72 2b 69 2b 31 2c  rt, baseCur+i+1,
21d5b 20 61 52 65 67 49 64 78 5b 69 5d 29 3b 0a 20 20   aRegIdx[i]);.  
21d5c 7d 0a 20 20 72 65 67 44 61 74 61 20 3d 20 72 65  }.  regData = re
21d5d 67 52 6f 77 69 64 20 2b 20 31 3b 0a 20 20 72 65  gRowid + 1;.  re
21d5e 67 52 65 63 20 3d 20 73 71 6c 69 74 65 33 47 65  gRec = sqlite3Ge
21d5f 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
21d60 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
21d61 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65  ddOp3(v, OP_Make
21d62 52 65 63 6f 72 64 2c 20 72 65 67 44 61 74 61 2c  Record, regData,
21d63 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 72 65 67   pTab->nCol, reg
21d64 52 65 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 54  Rec);.  sqlite3T
21d65 61 62 6c 65 41 66 66 69 6e 69 74 79 53 74 72 28  ableAffinityStr(
21d66 76 2c 20 70 54 61 62 29 3b 0a 20 20 73 71 6c 69  v, pTab);.  sqli
21d67 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69  te3ExprCacheAffi
21d68 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73  nityChange(pPars
21d69 65 2c 20 72 65 67 44 61 74 61 2c 20 70 54 61 62  e, regData, pTab
21d6a 2d 3e 6e 43 6f 6c 29 3b 0a 23 69 66 6e 64 65 66  ->nCol);.#ifndef
21d6b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
21d6c 47 47 45 52 0a 20 20 69 66 28 20 6e 65 77 49 64  GGER.  if( newId
21d6d 78 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  x>=0 ){.    sqli
21d6e 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
21d6f 20 4f 50 5f 49 6e 73 65 72 74 2c 20 6e 65 77 49   OP_Insert, newI
21d70 64 78 2c 20 72 65 67 52 65 63 2c 20 72 65 67 52  dx, regRec, regR
21d71 6f 77 69 64 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  owid);.  }.#endi
21d72 66 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  f.  if( pParse->
21d73 6e 65 73 74 65 64 20 29 7b 0a 20 20 20 20 70 69  nested ){.    pi
21d74 6b 5f 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 7d  k_flags = 0;.  }
21d75 65 6c 73 65 7b 0a 20 20 20 20 70 69 6b 5f 66 6c  else{.    pik_fl
21d76 61 67 73 20 3d 20 4f 50 46 4c 41 47 5f 4e 43 48  ags = OPFLAG_NCH
21d77 41 4e 47 45 3b 0a 20 20 20 20 70 69 6b 5f 66 6c  ANGE;.    pik_fl
21d78 61 67 73 20 7c 3d 20 28 69 73 55 70 64 61 74 65  ags |= (isUpdate
21d79 3f 4f 50 46 4c 41 47 5f 49 53 55 50 44 41 54 45  ?OPFLAG_ISUPDATE
21d7a 3a 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49  :OPFLAG_LASTROWI
21d7b 44 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 61 70  D);.  }.  if( ap
21d7c 70 65 6e 64 42 69 61 73 20 29 7b 0a 20 20 20 20  pendBias ){.    
21d7d 70 69 6b 5f 66 6c 61 67 73 20 7c 3d 20 4f 50 46  pik_flags |= OPF
21d7e 4c 41 47 5f 41 50 50 45 4e 44 3b 0a 20 20 7d 0a  LAG_APPEND;.  }.
21d7f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21d80 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74  Op3(v, OP_Insert
21d81 2c 20 62 61 73 65 43 75 72 2c 20 72 65 67 52 65  , baseCur, regRe
21d82 63 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  c, regRowid);.  
21d83 69 66 28 20 21 70 50 61 72 73 65 2d 3e 6e 65 73  if( !pParse->nes
21d84 74 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ted ){.    sqlit
21d85 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
21d86 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  , -1, pTab->zNam
21d87 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  e, P4_STATIC);. 
21d88 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65   }.  sqlite3Vdbe
21d89 43 68 61 6e 67 65 50 35 28 76 2c 20 70 69 6b 5f  ChangeP5(v, pik_
21d8a 66 6c 61 67 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  flags);.}../*.**
21d8b 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
21d8c 68 61 74 20 77 69 6c 6c 20 6f 70 65 6e 20 63 75  hat will open cu
21d8d 72 73 6f 72 73 20 66 6f 72 20 61 20 74 61 62 6c  rsors for a tabl
21d8e 65 20 61 6e 64 20 66 6f 72 20 61 6c 6c 0a 2a 2a  e and for all.**
21d8f 20 69 6e 64 69 63 65 73 20 6f 66 20 74 68 61 74   indices of that
21d90 20 74 61 62 6c 65 2e 20 20 54 68 65 20 22 62 61   table.  The "ba
21d91 73 65 43 75 72 22 20 70 61 72 61 6d 65 74 65 72  seCur" parameter
21d92 20 69 73 20 74 68 65 20 63 75 72 73 6f 72 20 6e   is the cursor n
21d93 75 6d 62 65 72 20 75 73 65 64 0a 2a 2a 20 66 6f  umber used.** fo
21d94 72 20 74 68 65 20 74 61 62 6c 65 2e 20 20 49 6e  r the table.  In
21d95 64 69 63 65 73 20 61 72 65 20 6f 70 65 6e 65 64  dices are opened
21d96 20 6f 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63   on subsequent c
21d97 75 72 73 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 52 65  ursors..**.** Re
21d98 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
21d99 6f 66 20 69 6e 64 69 63 65 73 20 6f 6e 20 74 68  of indices on th
21d9a 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49  e table..*/.SQLI
21d9b 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
21d9c 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41  qlite3OpenTableA
21d9d 6e 64 49 6e 64 69 63 65 73 28 0a 20 20 50 61 72  ndIndices(.  Par
21d9e 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a  se *pParse,   /*
21d9f 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
21da0 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
21da1 62 2c 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20  b,     /* Table 
21da2 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f 0a  to be opened */.
21da3 20 20 69 6e 74 20 62 61 73 65 43 75 72 2c 20 20    int baseCur,  
21da4 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d     /* Cursor num
21da5 62 65 72 20 61 73 73 69 67 6e 65 64 20 74 6f 20  ber assigned to 
21da6 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  the table */.  i
21da7 6e 74 20 6f 70 20 20 20 20 20 20 20 20 20 20 20  nt op           
21da8 2f 2a 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 6f  /* OP_OpenRead o
21da9 72 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 2a  r OP_OpenWrite *
21daa 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  /.){.  int i;.  
21dab 69 6e 74 20 69 44 62 3b 0a 20 20 49 6e 64 65 78  int iDb;.  Index
21dac 20 2a 70 49 64 78 3b 0a 20 20 56 64 62 65 20 2a   *pIdx;.  Vdbe *
21dad 76 3b 0a 0a 20 20 69 66 28 20 49 73 56 69 72 74  v;..  if( IsVirt
21dae 75 61 6c 28 70 54 61 62 29 20 29 20 72 65 74 75  ual(pTab) ) retu
21daf 72 6e 20 30 3b 0a 20 20 69 44 62 20 3d 20 73 71  rn 0;.  iDb = sq
21db0 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
21db1 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  ex(pParse->db, p
21db2 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  Tab->pSchema);. 
21db3 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
21db4 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 61  dbe(pParse);.  a
21db5 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20  ssert( v!=0 );. 
21db6 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c   sqlite3OpenTabl
21db7 65 28 70 50 61 72 73 65 2c 20 62 61 73 65 43 75  e(pParse, baseCu
21db8 72 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 6f 70  r, iDb, pTab, op
21db9 29 3b 0a 20 20 66 6f 72 28 69 3d 31 2c 20 70 49  );.  for(i=1, pI
21dba 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
21dbb 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
21dbc 2d 3e 70 4e 65 78 74 2c 20 69 2b 2b 29 7b 0a 20  ->pNext, i++){. 
21dbd 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79     KeyInfo *pKey
21dbe 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b   = sqlite3IndexK
21dbf 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70  eyinfo(pParse, p
21dc0 49 64 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74  Idx);.    assert
21dc1 28 20 70 49 64 78 2d 3e 70 53 63 68 65 6d 61 3d  ( pIdx->pSchema=
21dc2 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29  =pTab->pSchema )
21dc3 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
21dc4 65 41 64 64 4f 70 34 28 76 2c 20 6f 70 2c 20 69  eAddOp4(v, op, i
21dc5 2b 62 61 73 65 43 75 72 2c 20 70 49 64 78 2d 3e  +baseCur, pIdx->
21dc6 74 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20  tnum, iDb,.     
21dc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21dc8 20 28 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34   (char*)pKey, P4
21dc9 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
21dca 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  );.    VdbeComme
21dcb 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70 49 64  nt((v, "%s", pId
21dcc 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 7d 0a  x->zName));.  }.
21dcd 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 54    if( pParse->nT
21dce 61 62 3c 3d 62 61 73 65 43 75 72 2b 69 20 29 7b  ab<=baseCur+i ){
21dcf 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 61  .    pParse->nTa
21dd0 62 20 3d 20 62 61 73 65 43 75 72 2b 69 3b 0a 20  b = baseCur+i;. 
21dd1 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 2d 31 3b   }.  return i-1;
21dd2 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  .}...#ifdef SQLI
21dd3 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68  TE_TEST./*.** Th
21dd4 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62  e following glob
21dd5 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20 69  al variable is i
21dd6 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 65 6e 65  ncremented whene
21dd7 76 65 72 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73  ver the.** trans
21dd8 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  fer optimization
21dd9 20 69 73 20 75 73 65 64 2e 20 20 54 68 69 73 20   is used.  This 
21dda 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74  is used for test
21ddb 69 6e 67 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20  ing.** purposes 
21ddc 6f 6e 6c 79 20 2d 20 74 6f 20 6d 61 6b 65 20 73  only - to make s
21ddd 75 72 65 20 74 68 65 20 74 72 61 6e 73 66 65 72  ure the transfer
21dde 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 72 65   optimization re
21ddf 61 6c 6c 79 0a 2a 2a 20 69 73 20 68 61 70 70 65  ally.** is happe
21de0 6e 69 6e 67 20 77 68 65 6e 20 69 74 20 69 73 20  ning when it is 
21de1 73 75 70 70 6f 73 65 20 74 6f 2e 0a 2a 2f 0a 53  suppose to..*/.S
21de2 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
21de3 6c 69 74 65 33 5f 78 66 65 72 6f 70 74 5f 63 6f  lite3_xferopt_co
21de4 75 6e 74 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53  unt;.#endif /* S
21de5 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f 0a 0a 0a  QLITE_TEST */...
21de6 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
21de7 4d 49 54 5f 58 46 45 52 5f 4f 50 54 0a 2f 2a 0a  MIT_XFER_OPT./*.
21de8 2a 2a 20 43 68 65 63 6b 20 74 6f 20 63 6f 6c 6c  ** Check to coll
21de9 61 74 69 6f 6e 20 6e 61 6d 65 73 20 74 6f 20 73  ation names to s
21dea 65 65 20 69 66 20 74 68 65 79 20 61 72 65 20 63  ee if they are c
21deb 6f 6d 70 61 74 69 62 6c 65 2e 0a 2a 2f 0a 73 74  ompatible..*/.st
21dec 61 74 69 63 20 69 6e 74 20 78 66 65 72 43 6f 6d  atic int xferCom
21ded 70 61 74 69 62 6c 65 43 6f 6c 6c 61 74 69 6f 6e  patibleCollation
21dee 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 31 2c  (const char *z1,
21def 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 29   const char *z2)
21df0 7b 0a 20 20 69 66 28 20 7a 31 3d 3d 30 20 29 7b  {.  if( z1==0 ){
21df1 0a 20 20 20 20 72 65 74 75 72 6e 20 7a 32 3d 3d  .    return z2==
21df2 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 32 3d  0;.  }.  if( z2=
21df3 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
21df4 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   0;.  }.  return
21df5 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
21df6 7a 31 2c 20 7a 32 29 3d 3d 30 3b 0a 7d 0a 0a 0a  z1, z2)==0;.}...
21df7 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73  /*.** Check to s
21df8 65 65 20 69 66 20 69 6e 64 65 78 20 70 53 72 63  ee if index pSrc
21df9 20 69 73 20 63 6f 6d 70 61 74 69 62 6c 65 20 61   is compatible a
21dfa 73 20 61 20 73 6f 75 72 63 65 20 6f 66 20 64 61  s a source of da
21dfb 74 61 0a 2a 2a 20 66 6f 72 20 69 6e 64 65 78 20  ta.** for index 
21dfc 70 44 65 73 74 20 69 6e 20 61 6e 20 69 6e 73 65  pDest in an inse
21dfd 72 74 20 74 72 61 6e 73 66 65 72 20 6f 70 74 69  rt transfer opti
21dfe 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20 72  mization.  The r
21dff 75 6c 65 73 0a 2a 2a 20 66 6f 72 20 61 20 63 6f  ules.** for a co
21e00 6d 70 61 74 69 62 6c 65 20 69 6e 64 65 78 3a 0a  mpatible index:.
21e01 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 20 54 68 65  **.**    *   The
21e02 20 69 6e 64 65 78 20 69 73 20 6f 76 65 72 20 74   index is over t
21e03 68 65 20 73 61 6d 65 20 73 65 74 20 6f 66 20 63  he same set of c
21e04 6f 6c 75 6d 6e 73 0a 2a 2a 20 20 20 20 2a 20 20  olumns.**    *  
21e05 20 54 68 65 20 73 61 6d 65 20 44 45 53 43 20 61   The same DESC a
21e06 6e 64 20 41 53 43 20 6d 61 72 6b 69 6e 67 73 20  nd ASC markings 
21e07 6f 63 63 75 72 73 20 6f 6e 20 61 6c 6c 20 63 6f  occurs on all co
21e08 6c 75 6d 6e 73 0a 2a 2a 20 20 20 20 2a 20 20 20  lumns.**    *   
21e09 54 68 65 20 73 61 6d 65 20 6f 6e 45 72 72 6f 72  The same onError
21e0a 20 70 72 6f 63 65 73 73 69 6e 67 20 28 4f 45 5f   processing (OE_
21e0b 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65  Abort, OE_Ignore
21e0c 2c 20 65 74 63 29 0a 2a 2a 20 20 20 20 2a 20 20  , etc).**    *  
21e0d 20 54 68 65 20 73 61 6d 65 20 63 6f 6c 6c 61 74   The same collat
21e0e 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 6e 20  ing sequence on 
21e0f 65 61 63 68 20 63 6f 6c 75 6d 6e 0a 2a 2f 0a 73  each column.*/.s
21e10 74 61 74 69 63 20 69 6e 74 20 78 66 65 72 43 6f  tatic int xferCo
21e11 6d 70 61 74 69 62 6c 65 49 6e 64 65 78 28 49 6e  mpatibleIndex(In
21e12 64 65 78 20 2a 70 44 65 73 74 2c 20 49 6e 64 65  dex *pDest, Inde
21e13 78 20 2a 70 53 72 63 29 7b 0a 20 20 69 6e 74 20  x *pSrc){.  int 
21e14 69 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 65  i;.  assert( pDe
21e15 73 74 20 26 26 20 70 53 72 63 20 29 3b 0a 20 20  st && pSrc );.  
21e16 61 73 73 65 72 74 28 20 70 44 65 73 74 2d 3e 70  assert( pDest->p
21e17 54 61 62 6c 65 21 3d 70 53 72 63 2d 3e 70 54 61  Table!=pSrc->pTa
21e18 62 6c 65 20 29 3b 0a 20 20 69 66 28 20 70 44 65  ble );.  if( pDe
21e19 73 74 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d 70 53 72  st->nColumn!=pSr
21e1a 63 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20  c->nColumn ){.  
21e1b 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
21e1c 20 44 69 66 66 65 72 65 6e 74 20 6e 75 6d 62 65   Different numbe
21e1d 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a  r of columns */.
21e1e 20 20 7d 0a 20 20 69 66 28 20 70 44 65 73 74 2d    }.  if( pDest-
21e1f 3e 6f 6e 45 72 72 6f 72 21 3d 70 53 72 63 2d 3e  >onError!=pSrc->
21e20 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 72  onError ){.    r
21e21 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 44 69  eturn 0;   /* Di
21e22 66 66 65 72 65 6e 74 20 63 6f 6e 66 6c 69 63 74  fferent conflict
21e23 20 72 65 73 6f 6c 75 74 69 6f 6e 20 73 74 72 61   resolution stra
21e24 74 65 67 69 65 73 20 2a 2f 0a 20 20 7d 0a 20 20  tegies */.  }.  
21e25 66 6f 72 28 69 3d 30 3b 20 69 3c 70 53 72 63 2d  for(i=0; i<pSrc-
21e26 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a  >nColumn; i++){.
21e27 20 20 20 20 69 66 28 20 70 53 72 63 2d 3e 61 69      if( pSrc->ai
21e28 43 6f 6c 75 6d 6e 5b 69 5d 21 3d 70 44 65 73 74  Column[i]!=pDest
21e29 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 20 29 7b  ->aiColumn[i] ){
21e2a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
21e2b 20 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74 20     /* Different 
21e2c 63 6f 6c 75 6d 6e 73 20 69 6e 64 65 78 65 64 20  columns indexed 
21e2d 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  */.    }.    if(
21e2e 20 70 53 72 63 2d 3e 61 53 6f 72 74 4f 72 64 65   pSrc->aSortOrde
21e2f 72 5b 69 5d 21 3d 70 44 65 73 74 2d 3e 61 53 6f  r[i]!=pDest->aSo
21e30 72 74 4f 72 64 65 72 5b 69 5d 20 29 7b 0a 20 20  rtOrder[i] ){.  
21e31 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
21e32 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 73 6f 72  /* Different sor
21e33 74 20 6f 72 64 65 72 73 20 2a 2f 0a 20 20 20 20  t orders */.    
21e34 7d 0a 20 20 20 20 69 66 28 20 70 53 72 63 2d 3e  }.    if( pSrc->
21e35 61 7a 43 6f 6c 6c 5b 69 5d 21 3d 70 44 65 73 74  azColl[i]!=pDest
21e36 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 20 29 7b 0a 20  ->azColl[i] ){. 
21e37 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20       return 0;  
21e38 20 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 63 6f   /* Different co
21e39 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
21e3a 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  s */.    }.  }..
21e3b 20 20 2f 2a 20 49 66 20 6e 6f 20 74 65 73 74 20    /* If no test 
21e3c 61 62 6f 76 65 20 66 61 69 6c 73 20 74 68 65 6e  above fails then
21e3d 20 74 68 65 20 69 6e 64 69 63 65 73 20 6d 75 73   the indices mus
21e3e 74 20 62 65 20 63 6f 6d 70 61 74 69 62 6c 65 20  t be compatible 
21e3f 2a 2f 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d  */.  return 1;.}
21e40 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20  ../*.** Attempt 
21e41 74 68 65 20 74 72 61 6e 73 66 65 72 20 6f 70 74  the transfer opt
21e42 69 6d 69 7a 61 74 69 6f 6e 20 6f 6e 20 49 4e 53  imization on INS
21e43 45 52 54 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  ERTs of the form
21e44 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 4e 53 45 52  .**.**     INSER
21e45 54 20 49 4e 54 4f 20 74 61 62 31 20 53 45 4c 45  T INTO tab1 SELE
21e46 43 54 20 2a 20 46 52 4f 4d 20 74 61 62 32 3b 0a  CT * FROM tab2;.
21e47 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69 6d  **.** This optim
21e48 69 7a 61 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20  ization is only 
21e49 61 74 74 65 6d 70 74 65 64 20 69 66 0a 2a 2a 0a  attempted if.**.
21e4a 2a 2a 20 20 20 20 28 31 29 20 20 74 61 62 31 20  **    (1)  tab1 
21e4b 61 6e 64 20 74 61 62 32 20 68 61 76 65 20 69 64  and tab2 have id
21e4c 65 6e 74 69 63 61 6c 20 73 63 68 65 6d 61 73 20  entical schemas 
21e4d 69 6e 63 6c 75 64 69 6e 67 20 61 6c 6c 20 74 68  including all th
21e4e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 61 6d  e.**         sam
21e4f 65 20 69 6e 64 69 63 65 73 20 61 6e 64 20 63 6f  e indices and co
21e50 6e 73 74 72 61 69 6e 74 73 0a 2a 2a 0a 2a 2a 20  nstraints.**.** 
21e51 20 20 20 28 32 29 20 20 74 61 62 31 20 61 6e 64     (2)  tab1 and
21e52 20 74 61 62 32 20 61 72 65 20 64 69 66 66 65 72   tab2 are differ
21e53 65 6e 74 20 74 61 62 6c 65 73 0a 2a 2a 0a 2a 2a  ent tables.**.**
21e54 20 20 20 20 28 33 29 20 20 54 68 65 72 65 20 6d      (3)  There m
21e55 75 73 74 20 62 65 20 6e 6f 20 74 72 69 67 67 65  ust be no trigge
21e56 72 73 20 6f 6e 20 74 61 62 31 0a 2a 2a 0a 2a 2a  rs on tab1.**.**
21e57 20 20 20 20 28 34 29 20 20 54 68 65 20 72 65 73      (4)  The res
21e58 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20 53  ult set of the S
21e59 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
21e5a 69 73 20 22 2a 22 0a 2a 2a 0a 2a 2a 20 20 20 20  is "*".**.**    
21e5b 28 35 29 20 20 54 68 65 20 53 45 4c 45 43 54 20  (5)  The SELECT 
21e5c 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 6e 6f  statement has no
21e5d 20 57 48 45 52 45 2c 20 48 41 56 49 4e 47 2c 20   WHERE, HAVING, 
21e5e 4f 52 44 45 52 20 42 59 2c 20 47 52 4f 55 50 20  ORDER BY, GROUP 
21e5f 42 59 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f  BY,.**         o
21e60 72 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 0a  r LIMIT clause..
21e61 2a 2a 0a 2a 2a 20 20 20 20 28 36 29 20 20 54 68  **.**    (6)  Th
21e62 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
21e63 6e 74 20 69 73 20 61 20 73 69 6d 70 6c 65 20 28  nt is a simple (
21e64 6e 6f 74 20 61 20 63 6f 6d 70 6f 75 6e 64 29 20  not a compound) 
21e65 73 65 6c 65 63 74 20 74 68 61 74 0a 2a 2a 20 20  select that.**  
21e66 20 20 20 20 20 20 20 63 6f 6e 74 61 69 6e 73 20         contains 
21e67 6f 6e 6c 79 20 74 61 62 32 20 69 6e 20 69 74 73  only tab2 in its
21e68 20 46 52 4f 4d 20 63 6c 61 75 73 65 0a 2a 2a 0a   FROM clause.**.
21e69 2a 2a 20 54 68 69 73 20 6d 65 74 68 6f 64 20 66  ** This method f
21e6a 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20  or implementing 
21e6b 74 68 65 20 49 4e 53 45 52 54 20 74 72 61 6e 73  the INSERT trans
21e6c 66 65 72 73 20 72 61 77 20 72 65 63 6f 72 64 73  fers raw records
21e6d 20 66 72 6f 6d 0a 2a 2a 20 74 61 62 32 20 6f 76   from.** tab2 ov
21e6e 65 72 20 74 6f 20 74 61 62 31 2e 20 20 54 68 65  er to tab1.  The
21e6f 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 6e 6f 74   columns are not
21e70 20 64 65 63 6f 64 65 64 2e 20 20 52 61 77 20 72   decoded.  Raw r
21e71 65 63 6f 72 64 73 20 66 72 6f 6d 0a 2a 2a 20 74  ecords from.** t
21e72 68 65 20 69 6e 64 69 63 65 73 20 6f 66 20 74 61  he indices of ta
21e73 62 32 20 61 72 65 20 74 72 61 6e 73 66 65 72 65  b2 are transfere
21e74 64 20 74 6f 20 74 61 62 31 20 61 73 20 77 65 6c  d to tab1 as wel
21e75 6c 2e 20 20 49 6e 20 73 6f 20 64 6f 69 6e 67 2c  l.  In so doing,
21e76 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 69 6e  .** the resultin
21e77 67 20 74 61 62 31 20 68 61 73 20 6d 75 63 68 20  g tab1 has much 
21e78 6c 65 73 73 20 66 72 61 67 6d 65 6e 74 61 74 69  less fragmentati
21e79 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  on..**.** This r
21e7a 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 54  outine returns T
21e7b 52 55 45 20 69 66 20 74 68 65 20 6f 70 74 69 6d  RUE if the optim
21e7c 69 7a 61 74 69 6f 6e 20 69 73 20 61 74 74 65 6d  ization is attem
21e7d 70 74 65 64 2e 20 20 49 66 20 61 6e 79 0a 2a 2a  pted.  If any.**
21e7e 20 6f 66 20 74 68 65 20 63 6f 6e 64 69 74 69 6f   of the conditio
21e7f 6e 73 20 61 62 6f 76 65 20 66 61 69 6c 20 73 6f  ns above fail so
21e80 20 74 68 61 74 20 74 68 65 20 6f 70 74 69 6d 69   that the optimi
21e81 7a 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f  zation should no
21e82 74 0a 2a 2a 20 62 65 20 61 74 74 65 6d 70 74 65  t.** be attempte
21e83 64 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  d, then this rou
21e84 74 69 6e 65 20 72 65 74 75 72 6e 73 20 46 41 4c  tine returns FAL
21e85 53 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  SE..*/.static in
21e86 74 20 78 66 65 72 4f 70 74 69 6d 69 7a 61 74 69  t xferOptimizati
21e87 6f 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  on(.  Parse *pPa
21e88 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  rse,        /* P
21e89 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
21e8a 0a 20 20 54 61 62 6c 65 20 2a 70 44 65 73 74 2c  .  Table *pDest,
21e8b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
21e8c 74 61 62 6c 65 20 77 65 20 61 72 65 20 69 6e 73  table we are ins
21e8d 65 72 74 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a 20  erting into */. 
21e8e 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
21e8f 2c 20 20 20 20 20 20 2f 2a 20 41 20 53 45 4c 45  ,      /* A SELE
21e90 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20  CT statement to 
21e91 75 73 65 20 61 73 20 74 68 65 20 64 61 74 61 20  use as the data 
21e92 73 6f 75 72 63 65 20 2a 2f 0a 20 20 69 6e 74 20  source */.  int 
21e93 6f 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20  onError,        
21e94 20 20 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e 64    /* How to hand
21e95 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 72  le constraint er
21e96 72 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 44  rors */.  int iD
21e97 62 44 65 73 74 20 20 20 20 20 20 20 20 20 20 20  bDest           
21e98 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
21e99 6f 66 20 70 44 65 73 74 20 2a 2f 0a 29 7b 0a 20  of pDest */.){. 
21e9a 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
21e9b 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
21e9c 20 20 2f 2a 20 54 68 65 20 72 65 73 75 6c 74 20    /* The result 
21e9d 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43  set of the SELEC
21e9e 54 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 53  T */.  Table *pS
21e9f 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
21ea0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
21ea1 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d  able in the FROM
21ea2 20 63 6c 61 75 73 65 20 6f 66 20 53 45 4c 45 43   clause of SELEC
21ea3 54 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 53  T */.  Index *pS
21ea4 72 63 49 64 78 2c 20 2a 70 44 65 73 74 49 64 78  rcIdx, *pDestIdx
21ea5 3b 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63  ;       /* Sourc
21ea6 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f  e and destinatio
21ea7 6e 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 73  n indices */.  s
21ea8 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
21ea9 65 6d 20 2a 70 49 74 65 6d 3b 20 20 20 20 20 20  em *pItem;      
21eaa 2f 2a 20 41 6e 20 65 6c 65 6d 65 6e 74 20 6f 66  /* An element of
21eab 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 20 2a   pSelect->pSrc *
21eac 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  /.  int i;      
21ead 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21eae 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
21eaf 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69 44  nter */.  int iD
21eb0 62 53 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  bSrc;           
21eb1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
21eb2 65 20 64 61 74 61 62 61 73 65 20 6f 66 20 70 53  e database of pS
21eb3 72 63 20 2a 2f 0a 20 20 69 6e 74 20 69 53 72 63  rc */.  int iSrc
21eb4 2c 20 69 44 65 73 74 3b 20 20 20 20 20 20 20 20  , iDest;        
21eb5 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73           /* Curs
21eb6 6f 72 73 20 66 72 6f 6d 20 73 6f 75 72 63 65 20  ors from source 
21eb7 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  and destination 
21eb8 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 31 2c 20  */.  int addr1, 
21eb9 61 64 64 72 32 3b 20 20 20 20 20 20 20 20 20 20  addr2;          
21eba 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 61 64        /* Loop ad
21ebb 64 72 65 73 73 65 73 20 2a 2f 0a 20 20 69 6e 74  dresses */.  int
21ebc 20 65 6d 70 74 79 44 65 73 74 54 65 73 74 3b 20   emptyDestTest; 
21ebd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
21ebe 20 41 64 64 72 65 73 73 20 6f 66 20 74 65 73 74   Address of test
21ebf 20 66 6f 72 20 65 6d 70 74 79 20 70 44 65 73 74   for empty pDest
21ec0 20 2a 2f 0a 20 20 69 6e 74 20 65 6d 70 74 79 53   */.  int emptyS
21ec1 72 63 54 65 73 74 3b 20 20 20 20 20 20 20 20 20  rcTest;         
21ec2 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
21ec3 73 20 6f 66 20 74 65 73 74 20 66 6f 72 20 65 6d  s of test for em
21ec4 70 74 79 20 70 53 72 63 20 2a 2f 0a 20 20 56 64  pty pSrc */.  Vd
21ec5 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20  be *v;          
21ec6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
21ec7 2a 20 54 68 65 20 56 44 42 45 20 77 65 20 61 72  * The VDBE we ar
21ec8 65 20 62 75 69 6c 64 69 6e 67 20 2a 2f 0a 20 20  e building */.  
21ec9 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 3b 20 20  KeyInfo *pKey;  
21eca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21ecb 20 2f 2a 20 4b 65 79 20 69 6e 66 6f 72 6d 61 74   /* Key informat
21ecc 69 6f 6e 20 66 6f 72 20 61 6e 20 69 6e 64 65 78  ion for an index
21ecd 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 41 75 74   */.  int regAut
21ece 6f 69 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20  oinc;           
21ecf 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79         /* Memory
21ed0 20 72 65 67 69 73 74 65 72 20 75 73 65 64 20 62   register used b
21ed1 79 20 41 55 54 4f 49 4e 43 20 2a 2f 0a 20 20 69  y AUTOINC */.  i
21ed2 6e 74 20 64 65 73 74 48 61 73 55 6e 69 71 75 65  nt destHasUnique
21ed3 49 64 78 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Idx = 0;        
21ed4 2f 2a 20 54 72 75 65 20 69 66 20 70 44 65 73 74  /* True if pDest
21ed5 20 68 61 73 20 61 20 55 4e 49 51 55 45 20 69 6e   has a UNIQUE in
21ed6 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  dex */.  int reg
21ed7 44 61 74 61 2c 20 72 65 67 52 6f 77 69 64 3b 20  Data, regRowid; 
21ed8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
21ed9 69 73 74 65 72 73 20 68 6f 6c 64 69 6e 67 20 64  isters holding d
21eda 61 74 61 20 61 6e 64 20 72 6f 77 69 64 20 2a 2f  ata and rowid */
21edb 0a 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 3d  ..  if( pSelect=
21edc 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
21edd 20 30 3b 20 20 20 2f 2a 20 4d 75 73 74 20 62 65   0;   /* Must be
21ede 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 20 49 4e   of the form  IN
21edf 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45  SERT INTO ... SE
21ee0 4c 45 43 54 20 2e 2e 2e 20 2a 2f 0a 20 20 7d 0a  LECT ... */.  }.
21ee1 20 20 69 66 28 20 70 44 65 73 74 2d 3e 70 54 72    if( pDest->pTr
21ee2 69 67 67 65 72 20 29 7b 0a 20 20 20 20 72 65 74  igger ){.    ret
21ee3 75 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 31  urn 0;   /* tab1
21ee4 20 6d 75 73 74 20 6e 6f 74 20 68 61 76 65 20 74   must not have t
21ee5 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 7d 0a 23  riggers */.  }.#
21ee6 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
21ee7 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
21ee8 20 20 69 66 28 20 70 44 65 73 74 2d 3e 74 61 62    if( pDest->tab
21ee9 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74 75  Flags & TF_Virtu
21eea 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  al ){.    return
21eeb 20 30 3b 20 20 20 2f 2a 20 74 61 62 31 20 6d 75   0;   /* tab1 mu
21eec 73 74 20 6e 6f 74 20 62 65 20 61 20 76 69 72 74  st not be a virt
21eed 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7d  ual table */.  }
21eee 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 6f 6e  .#endif.  if( on
21eef 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c  Error==OE_Defaul
21ef0 74 20 29 7b 0a 20 20 20 20 6f 6e 45 72 72 6f 72  t ){.    onError
21ef1 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 7d   = OE_Abort;.  }
21ef2 0a 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 21 3d  .  if( onError!=
21ef3 4f 45 5f 41 62 6f 72 74 20 26 26 20 6f 6e 45 72  OE_Abort && onEr
21ef4 72 6f 72 21 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b  ror!=OE_Rollback
21ef5 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
21ef6 3b 20 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 64 6f  ;   /* Cannot do
21ef7 20 4f 52 20 52 45 50 4c 41 43 45 20 6f 72 20 4f   OR REPLACE or O
21ef8 52 20 49 47 4e 4f 52 45 20 6f 72 20 4f 52 20 46  R IGNORE or OR F
21ef9 41 49 4c 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 73  AIL */.  }.  ass
21efa 65 72 74 28 70 53 65 6c 65 63 74 2d 3e 70 53 72  ert(pSelect->pSr
21efb 63 29 3b 20 20 20 2f 2a 20 61 6c 6c 6f 63 61 74  c);   /* allocat
21efc 65 64 20 65 76 65 6e 20 69 66 20 74 68 65 72 65  ed even if there
21efd 20 69 73 20 6e 6f 20 46 52 4f 4d 20 63 6c 61 75   is no FROM clau
21efe 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 53 65 6c  se */.  if( pSel
21eff 65 63 74 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 21  ect->pSrc->nSrc!
21f00 3d 31 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =1 ){.    return
21f01 20 30 3b 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c   0;   /* FROM cl
21f02 61 75 73 65 20 6d 75 73 74 20 68 61 76 65 20 65  ause must have e
21f03 78 61 63 74 6c 79 20 6f 6e 65 20 74 65 72 6d 20  xactly one term 
21f04 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 65  */.  }.  if( pSe
21f05 6c 65 63 74 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d  lect->pSrc->a[0]
21f06 2e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20  .pSelect ){.    
21f07 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 46  return 0;   /* F
21f08 52 4f 4d 20 63 6c 61 75 73 65 20 63 61 6e 6e 6f  ROM clause canno
21f09 74 20 63 6f 6e 74 61 69 6e 20 61 20 73 75 62 71  t contain a subq
21f0a 75 65 72 79 20 2a 2f 0a 20 20 7d 0a 20 20 69 66  uery */.  }.  if
21f0b 28 20 70 53 65 6c 65 63 74 2d 3e 70 57 68 65 72  ( pSelect->pWher
21f0c 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
21f0d 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d  0;   /* SELECT m
21f0e 61 79 20 6e 6f 74 20 68 61 76 65 20 61 20 57 48  ay not have a WH
21f0f 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ERE clause */.  
21f10 7d 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d  }.  if( pSelect-
21f11 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20  >pOrderBy ){.   
21f12 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
21f13 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20 68  SELECT may not h
21f14 61 76 65 20 61 6e 20 4f 52 44 45 52 20 42 59 20  ave an ORDER BY 
21f15 63 6c 61 75 73 65 20 2a 2f 0a 20 20 7d 0a 20 20  clause */.  }.  
21f16 2f 2a 20 44 6f 20 6e 6f 74 20 6e 65 65 64 20 74  /* Do not need t
21f17 6f 20 74 65 73 74 20 66 6f 72 20 61 20 48 41 56  o test for a HAV
21f18 49 4e 47 20 63 6c 61 75 73 65 2e 20 20 49 66 20  ING clause.  If 
21f19 48 41 56 49 4e 47 20 69 73 20 70 72 65 73 65 6e  HAVING is presen
21f1a 74 20 62 75 74 0a 20 20 2a 2a 20 74 68 65 72 65  t but.  ** there
21f1b 20 69 73 20 6e 6f 20 4f 52 44 45 52 20 42 59 2c   is no ORDER BY,
21f1c 20 77 65 20 77 69 6c 6c 20 67 65 74 20 61 6e 20   we will get an 
21f1d 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 69 66 28 20  error. */.  if( 
21f1e 70 53 65 6c 65 63 74 2d 3e 70 47 72 6f 75 70 42  pSelect->pGroupB
21f1f 79 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  y ){.    return 
21f20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d  0;   /* SELECT m
21f21 61 79 20 6e 6f 74 20 68 61 76 65 20 61 20 47 52  ay not have a GR
21f22 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  OUP BY clause */
21f23 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 65 6c 65  .  }.  if( pSele
21f24 63 74 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20  ct->pLimit ){.  
21f25 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
21f26 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20   SELECT may not 
21f27 68 61 76 65 20 61 20 4c 49 4d 49 54 20 63 6c 61  have a LIMIT cla
21f28 75 73 65 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 73  use */.  }.  ass
21f29 65 72 74 28 20 70 53 65 6c 65 63 74 2d 3e 70 4f  ert( pSelect->pO
21f2a 66 66 73 65 74 3d 3d 30 20 29 3b 20 20 2f 2a 20  ffset==0 );  /* 
21f2b 4d 75 73 74 20 62 65 20 73 6f 20 69 66 20 70 4c  Must be so if pL
21f2c 69 6d 69 74 3d 3d 30 20 2a 2f 0a 20 20 69 66 28  imit==0 */.  if(
21f2d 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72   pSelect->pPrior
21f2e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
21f2f 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61  ;   /* SELECT ma
21f30 79 20 6e 6f 74 20 62 65 20 61 20 63 6f 6d 70 6f  y not be a compo
21f31 75 6e 64 20 71 75 65 72 79 20 2a 2f 0a 20 20 7d  und query */.  }
21f32 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e  .  if( pSelect->
21f33 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69  selFlags & SF_Di
21f34 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 72 65  stinct ){.    re
21f35 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c  turn 0;   /* SEL
21f36 45 43 54 20 6d 61 79 20 6e 6f 74 20 62 65 20 44  ECT may not be D
21f37 49 53 54 49 4e 43 54 20 2a 2f 0a 20 20 7d 0a 20  ISTINCT */.  }. 
21f38 20 70 45 4c 69 73 74 20 3d 20 70 53 65 6c 65 63   pEList = pSelec
21f39 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 61 73 73  t->pEList;.  ass
21f3a 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29  ert( pEList!=0 )
21f3b 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e  ;.  if( pEList->
21f3c 6e 45 78 70 72 21 3d 31 20 29 7b 0a 20 20 20 20  nExpr!=1 ){.    
21f3d 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 54  return 0;   /* T
21f3e 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6d 75  he result set mu
21f3f 73 74 20 68 61 76 65 20 65 78 61 63 74 6c 79 20  st have exactly 
21f40 6f 6e 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20  one column */.  
21f41 7d 0a 20 20 61 73 73 65 72 74 28 20 70 45 4c 69  }.  assert( pELi
21f42 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 20 29  st->a[0].pExpr )
21f43 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e  ;.  if( pEList->
21f44 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 21 3d  a[0].pExpr->op!=
21f45 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 72 65  TK_ALL ){.    re
21f46 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 54 68 65  turn 0;   /* The
21f47 20 72 65 73 75 6c 74 20 73 65 74 20 6d 75 73 74   result set must
21f48 20 62 65 20 74 68 65 20 73 70 65 63 69 61 6c 20   be the special 
21f49 6f 70 65 72 61 74 6f 72 20 22 2a 22 20 2a 2f 0a  operator "*" */.
21f4a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69    }..  /* At thi
21f4b 73 20 70 6f 69 6e 74 20 77 65 20 68 61 76 65 20  s point we have 
21f4c 65 73 74 61 62 6c 69 73 68 65 64 20 74 68 61 74  established that
21f4d 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69   the statement i
21f4e 73 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f  s of the.  ** co
21f4f 72 72 65 63 74 20 73 79 6e 74 61 63 74 69 63 20  rrect syntactic 
21f50 66 6f 72 6d 20 74 6f 20 70 61 72 74 69 63 69 70  form to particip
21f51 61 74 65 20 69 6e 20 74 68 69 73 20 6f 70 74 69  ate in this opti
21f52 6d 69 7a 61 74 69 6f 6e 2e 20 20 4e 6f 77 0a 20  mization.  Now. 
21f53 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 63   ** we have to c
21f54 68 65 63 6b 20 74 68 65 20 73 65 6d 61 6e 74 69  heck the semanti
21f55 63 73 2e 0a 20 20 2a 2f 0a 20 20 70 49 74 65 6d  cs..  */.  pItem
21f56 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63   = pSelect->pSrc
21f57 2d 3e 61 3b 0a 20 20 70 53 72 63 20 3d 20 73 71  ->a;.  pSrc = sq
21f58 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
21f59 28 70 50 61 72 73 65 2c 20 30 2c 20 70 49 74 65  (pParse, 0, pIte
21f5a 6d 2d 3e 7a 4e 61 6d 65 2c 20 70 49 74 65 6d 2d  m->zName, pItem-
21f5b 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 69  >zDatabase);.  i
21f5c 66 28 20 70 53 72 63 3d 3d 30 20 29 7b 0a 20 20  f( pSrc==0 ){.  
21f5d 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
21f5e 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 64 6f 65   FROM clause doe
21f5f 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20  s not contain a 
21f60 72 65 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  real table */.  
21f61 7d 0a 20 20 69 66 28 20 70 53 72 63 3d 3d 70 44  }.  if( pSrc==pD
21f62 65 73 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  est ){.    retur
21f63 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 31 20 61  n 0;   /* tab1 a
21f64 6e 64 20 74 61 62 32 20 6d 61 79 20 6e 6f 74 20  nd tab2 may not 
21f65 62 65 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c  be the same tabl
21f66 65 20 2a 2f 0a 20 20 7d 0a 23 69 66 6e 64 65 66  e */.  }.#ifndef
21f67 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
21f68 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20  TUALTABLE.  if( 
21f69 70 53 72 63 2d 3e 74 61 62 46 6c 61 67 73 20 26  pSrc->tabFlags &
21f6a 20 54 46 5f 56 69 72 74 75 61 6c 20 29 7b 0a 20   TF_Virtual ){. 
21f6b 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
21f6c 2a 20 74 61 62 32 20 6d 75 73 74 20 6e 6f 74 20  * tab2 must not 
21f6d 62 65 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  be a virtual tab
21f6e 6c 65 20 2a 2f 0a 20 20 7d 0a 23 65 6e 64 69 66  le */.  }.#endif
21f6f 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 70 53 65  .  if( pSrc->pSe
21f70 6c 65 63 74 20 29 7b 0a 20 20 20 20 72 65 74 75  lect ){.    retu
21f71 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 32 20  rn 0;   /* tab2 
21f72 6d 61 79 20 6e 6f 74 20 62 65 20 61 20 76 69 65  may not be a vie
21f73 77 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70  w */.  }.  if( p
21f74 44 65 73 74 2d 3e 6e 43 6f 6c 21 3d 70 53 72 63  Dest->nCol!=pSrc
21f75 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 72 65  ->nCol ){.    re
21f76 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 4e 75 6d  turn 0;   /* Num
21f77 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6d  ber of columns m
21f78 75 73 74 20 62 65 20 74 68 65 20 73 61 6d 65 20  ust be the same 
21f79 69 6e 20 74 61 62 31 20 61 6e 64 20 74 61 62 32  in tab1 and tab2
21f7a 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 44   */.  }.  if( pD
21f7b 65 73 74 2d 3e 69 50 4b 65 79 21 3d 70 53 72 63  est->iPKey!=pSrc
21f7c 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 72  ->iPKey ){.    r
21f7d 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 42 6f  eturn 0;   /* Bo
21f7e 74 68 20 74 61 62 6c 65 73 20 6d 75 73 74 20 68  th tables must h
21f7f 61 76 65 20 74 68 65 20 73 61 6d 65 20 49 4e 54  ave the same INT
21f80 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
21f81 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d   */.  }.  for(i=
21f82 30 3b 20 69 3c 70 44 65 73 74 2d 3e 6e 43 6f 6c  0; i<pDest->nCol
21f83 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
21f84 70 44 65 73 74 2d 3e 61 43 6f 6c 5b 69 5d 2e 61  pDest->aCol[i].a
21f85 66 66 69 6e 69 74 79 21 3d 70 53 72 63 2d 3e 61  ffinity!=pSrc->a
21f86 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79 20  Col[i].affinity 
21f87 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
21f88 30 3b 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74  0;    /* Affinit
21f89 79 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61  y must be the sa
21f8a 6d 65 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75 6d 6e  me on all column
21f8b 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69  s */.    }.    i
21f8c 66 28 20 21 78 66 65 72 43 6f 6d 70 61 74 69 62  f( !xferCompatib
21f8d 6c 65 43 6f 6c 6c 61 74 69 6f 6e 28 70 44 65 73  leCollation(pDes
21f8e 74 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c  t->aCol[i].zColl
21f8f 2c 20 70 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e  , pSrc->aCol[i].
21f90 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20  zColl) ){.      
21f91 72 65 74 75 72 6e 20 30 3b 20 20 20 20 2f 2a 20  return 0;    /* 
21f92 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  Collating sequen
21f93 63 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 73  ce must be the s
21f94 61 6d 65 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75 6d  ame on all colum
21f95 6e 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20  ns */.    }.    
21f96 69 66 28 20 70 44 65 73 74 2d 3e 61 43 6f 6c 5b  if( pDest->aCol[
21f97 69 5d 2e 6e 6f 74 4e 75 6c 6c 20 26 26 20 21 70  i].notNull && !p
21f98 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74  Src->aCol[i].not
21f99 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 72 65  Null ){.      re
21f9a 74 75 72 6e 20 30 3b 20 20 20 20 2f 2a 20 74 61  turn 0;    /* ta
21f9b 62 32 20 6d 75 73 74 20 62 65 20 4e 4f 54 20 4e  b2 must be NOT N
21f9c 55 4c 4c 20 69 66 20 74 61 62 31 20 69 73 20 2a  ULL if tab1 is *
21f9d 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f  /.    }.  }.  fo
21f9e 72 28 70 44 65 73 74 49 64 78 3d 70 44 65 73 74  r(pDestIdx=pDest
21f9f 2d 3e 70 49 6e 64 65 78 3b 20 70 44 65 73 74 49  ->pIndex; pDestI
21fa0 64 78 3b 20 70 44 65 73 74 49 64 78 3d 70 44 65  dx; pDestIdx=pDe
21fa1 73 74 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20  stIdx->pNext){. 
21fa2 20 20 20 69 66 28 20 70 44 65 73 74 49 64 78 2d     if( pDestIdx-
21fa3 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e  >onError!=OE_Non
21fa4 65 20 29 7b 0a 20 20 20 20 20 20 64 65 73 74 48  e ){.      destH
21fa5 61 73 55 6e 69 71 75 65 49 64 78 20 3d 20 31 3b  asUniqueIdx = 1;
21fa6 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 70  .    }.    for(p
21fa7 53 72 63 49 64 78 3d 70 53 72 63 2d 3e 70 49 6e  SrcIdx=pSrc->pIn
21fa8 64 65 78 3b 20 70 53 72 63 49 64 78 3b 20 70 53  dex; pSrcIdx; pS
21fa9 72 63 49 64 78 3d 70 53 72 63 49 64 78 2d 3e 70  rcIdx=pSrcIdx->p
21faa 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28  Next){.      if(
21fab 20 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 49   xferCompatibleI
21fac 6e 64 65 78 28 70 44 65 73 74 49 64 78 2c 20 70  ndex(pDestIdx, p
21fad 53 72 63 49 64 78 29 20 29 20 62 72 65 61 6b 3b  SrcIdx) ) break;
21fae 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
21faf 53 72 63 49 64 78 3d 3d 30 20 29 7b 0a 20 20 20  SrcIdx==0 ){.   
21fb0 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20     return 0;    
21fb1 2f 2a 20 70 44 65 73 74 49 64 78 20 68 61 73 20  /* pDestIdx has 
21fb2 6e 6f 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  no corresponding
21fb3 20 69 6e 64 65 78 20 69 6e 20 70 53 72 63 20 2a   index in pSrc *
21fb4 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66 6e  /.    }.  }.#ifn
21fb5 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
21fb6 43 48 45 43 4b 0a 20 20 69 66 28 20 70 44 65 73  CHECK.  if( pDes
21fb7 74 2d 3e 70 43 68 65 63 6b 20 26 26 20 21 73 71  t->pCheck && !sq
21fb8 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65  lite3ExprCompare
21fb9 28 70 53 72 63 2d 3e 70 43 68 65 63 6b 2c 20 70  (pSrc->pCheck, p
21fba 44 65 73 74 2d 3e 70 43 68 65 63 6b 29 20 29 7b  Dest->pCheck) ){
21fbb 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
21fbc 20 2f 2a 20 54 61 62 6c 65 73 20 68 61 76 65 20   /* Tables have 
21fbd 64 69 66 66 65 72 65 6e 74 20 43 48 45 43 4b 20  different CHECK 
21fbe 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 54 69  constraints.  Ti
21fbf 63 6b 65 74 20 23 32 32 35 32 20 2a 2f 0a 20 20  cket #2252 */.  
21fc0 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49  }.#endif..  /* I
21fc1 66 20 77 65 20 67 65 74 20 74 68 69 73 20 66 61  f we get this fa
21fc2 72 2c 20 69 74 20 6d 65 61 6e 73 20 65 69 74 68  r, it means eith
21fc3 65 72 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  er:.  **.  **   
21fc4 20 2a 20 20 20 57 65 20 63 61 6e 20 61 6c 77 61   *   We can alwa
21fc5 79 73 20 64 6f 20 74 68 65 20 74 72 61 6e 73 66  ys do the transf
21fc6 65 72 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  er if the table 
21fc7 63 6f 6e 74 61 69 6e 73 20 61 6e 0a 20 20 2a 2a  contains an.  **
21fc8 20 20 20 20 20 20 20 20 61 6e 20 69 6e 74 65 67          an integ
21fc9 65 72 20 70 72 69 6d 61 72 79 20 6b 65 79 0a 20  er primary key. 
21fca 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 2a 20 20 20   **.  **    *   
21fcb 57 65 20 63 61 6e 20 63 6f 6e 64 69 74 69 6f 6e  We can condition
21fcc 61 6c 6c 79 20 64 6f 20 74 68 65 20 74 72 61 6e  ally do the tran
21fcd 73 66 65 72 20 69 66 20 74 68 65 20 64 65 73 74  sfer if the dest
21fce 69 6e 61 74 69 6f 6e 0a 20 20 2a 2a 20 20 20 20  ination.  **    
21fcf 20 20 20 20 74 61 62 6c 65 20 69 73 20 65 6d 70      table is emp
21fd0 74 79 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20  ty..  */.#ifdef 
21fd1 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 71  SQLITE_TEST.  sq
21fd2 6c 69 74 65 33 5f 78 66 65 72 6f 70 74 5f 63 6f  lite3_xferopt_co
21fd3 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20  unt++;.#endif.  
21fd4 69 44 62 53 72 63 20 3d 20 73 71 6c 69 74 65 33  iDbSrc = sqlite3
21fd5 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50  SchemaToIndex(pP
21fd6 61 72 73 65 2d 3e 64 62 2c 20 70 53 72 63 2d 3e  arse->db, pSrc->
21fd7 70 53 63 68 65 6d 61 29 3b 0a 20 20 76 20 3d 20  pSchema);.  v = 
21fd8 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
21fd9 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65  Parse);.  sqlite
21fda 33 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d  3CodeVerifySchem
21fdb 61 28 70 50 61 72 73 65 2c 20 69 44 62 53 72 63  a(pParse, iDbSrc
21fdc 29 3b 0a 20 20 69 53 72 63 20 3d 20 70 50 61 72  );.  iSrc = pPar
21fdd 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 69 44  se->nTab++;.  iD
21fde 65 73 74 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  est = pParse->nT
21fdf 61 62 2b 2b 3b 0a 20 20 72 65 67 41 75 74 6f 69  ab++;.  regAutoi
21fe0 6e 63 20 3d 20 61 75 74 6f 49 6e 63 42 65 67 69  nc = autoIncBegi
21fe1 6e 28 70 50 61 72 73 65 2c 20 69 44 62 44 65 73  n(pParse, iDbDes
21fe2 74 2c 20 70 44 65 73 74 29 3b 0a 20 20 73 71 6c  t, pDest);.  sql
21fe3 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50  ite3OpenTable(pP
21fe4 61 72 73 65 2c 20 69 44 65 73 74 2c 20 69 44 62  arse, iDest, iDb
21fe5 44 65 73 74 2c 20 70 44 65 73 74 2c 20 4f 50 5f  Dest, pDest, OP_
21fe6 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20 20 69 66  OpenWrite);.  if
21fe7 28 20 28 70 44 65 73 74 2d 3e 69 50 4b 65 79 3c  ( (pDest->iPKey<
21fe8 30 20 26 26 20 70 44 65 73 74 2d 3e 70 49 6e 64  0 && pDest->pInd
21fe9 65 78 21 3d 30 29 20 7c 7c 20 64 65 73 74 48 61  ex!=0) || destHa
21fea 73 55 6e 69 71 75 65 49 64 78 20 29 7b 0a 20 20  sUniqueIdx ){.  
21feb 20 20 2f 2a 20 49 66 20 74 61 62 6c 65 73 20 64    /* If tables d
21fec 6f 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 49 4e  o not have an IN
21fed 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
21fee 59 20 61 6e 64 20 74 68 65 72 65 0a 20 20 20 20  Y and there.    
21fef 2a 2a 20 61 72 65 20 69 6e 64 69 63 65 73 20 74  ** are indices t
21ff0 6f 20 62 65 20 63 6f 70 69 65 64 20 61 6e 64 20  o be copied and 
21ff1 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
21ff2 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c 0a 20 20  is not empty,.  
21ff3 20 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20    ** we have to 
21ff4 64 69 73 61 6c 6c 6f 77 20 74 68 65 20 74 72 61  disallow the tra
21ff5 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69  nsfer optimizati
21ff6 6f 6e 20 62 65 63 61 75 73 65 20 74 68 65 0a 20  on because the. 
21ff7 20 20 20 2a 2a 20 74 68 65 20 72 6f 77 69 64 73     ** the rowids
21ff8 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 77 68   might change wh
21ff9 69 63 68 20 77 69 6c 6c 20 6d 65 73 73 20 75 70  ich will mess up
21ffa 20 69 6e 64 65 78 69 6e 67 2e 0a 20 20 20 20 2a   indexing..    *
21ffb 2a 0a 20 20 20 20 2a 2a 20 4f 72 20 69 66 20 74  *.    ** Or if t
21ffc 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 68  he destination h
21ffd 61 73 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65  as a UNIQUE inde
21ffe 78 20 61 6e 64 20 69 73 20 6e 6f 74 20 65 6d 70  x and is not emp
21fff 74 79 2c 0a 20 20 20 20 2a 2a 20 77 65 20 61 6c  ty,.    ** we al
22000 73 6f 20 64 69 73 61 6c 6c 6f 77 20 74 68 65 20  so disallow the 
22001 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a  transfer optimiz
22002 61 74 69 6f 6e 20 62 65 63 61 75 73 65 20 77 65  ation because we
22003 20 63 61 6e 6e 6f 74 0a 20 20 20 20 2a 2a 20 69   cannot.    ** i
22004 6e 73 75 72 65 20 74 68 61 74 20 61 6c 6c 20 65  nsure that all e
22005 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 75 6e  ntries in the un
22006 69 6f 6e 20 6f 66 20 44 45 53 54 20 61 6e 64 20  ion of DEST and 
22007 53 52 43 20 77 69 6c 6c 20 62 65 0a 20 20 20 20  SRC will be.    
22008 2a 2a 20 75 6e 69 71 75 65 2e 0a 20 20 20 20 2a  ** unique..    *
22009 2f 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73 71  /.    addr1 = sq
2200a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2200b 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 44  v, OP_Rewind, iD
2200c 65 73 74 2c 20 30 29 3b 0a 20 20 20 20 65 6d 70  est, 0);.    emp
2200d 74 79 44 65 73 74 54 65 73 74 20 3d 20 73 71 6c  tyDestTest = sql
2200e 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2200f 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30 29  , OP_Goto, 0, 0)
22010 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
22011 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
22012 72 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  r1);.  }else{.  
22013 20 20 65 6d 70 74 79 44 65 73 74 54 65 73 74 20    emptyDestTest 
22014 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  = 0;.  }.  sqlit
22015 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72  e3OpenTable(pPar
22016 73 65 2c 20 69 53 72 63 2c 20 69 44 62 53 72 63  se, iSrc, iDbSrc
22017 2c 20 70 53 72 63 2c 20 4f 50 5f 4f 70 65 6e 52  , pSrc, OP_OpenR
22018 65 61 64 29 3b 0a 20 20 65 6d 70 74 79 53 72 63  ead);.  emptySrc
22019 54 65 73 74 20 3d 20 73 71 6c 69 74 65 33 56 64  Test = sqlite3Vd
2201a 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
2201b 65 77 69 6e 64 2c 20 69 53 72 63 2c 20 30 29 3b  ewind, iSrc, 0);
2201c 0a 20 20 72 65 67 44 61 74 61 20 3d 20 73 71 6c  .  regData = sql
2201d 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
2201e 50 61 72 73 65 29 3b 0a 20 20 72 65 67 52 6f 77  Parse);.  regRow
2201f 69 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  id = sqlite3GetT
22020 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
22021 20 20 69 66 28 20 70 44 65 73 74 2d 3e 69 50 4b    if( pDest->iPK
22022 65 79 3e 3d 30 20 29 7b 0a 20 20 20 20 61 64 64  ey>=0 ){.    add
22023 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  r1 = sqlite3Vdbe
22024 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77  AddOp2(v, OP_Row
22025 69 64 2c 20 69 53 72 63 2c 20 72 65 67 52 6f 77  id, iSrc, regRow
22026 69 64 29 3b 0a 20 20 20 20 61 64 64 72 32 20 3d  id);.    addr2 =
22027 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22028 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73  p3(v, OP_NotExis
22029 74 73 2c 20 69 44 65 73 74 2c 20 30 2c 20 72 65  ts, iDest, 0, re
2202a 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c  gRowid);.    sql
2202b 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2202c 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54  , OP_Halt, SQLIT
2202d 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 6f 6e  E_CONSTRAINT, on
2202e 45 72 72 6f 72 2c 20 30 2c 0a 20 20 20 20 20 20  Error, 0,.      
2202f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22030 22 50 52 49 4d 41 52 59 20 4b 45 59 20 6d 75 73  "PRIMARY KEY mus
22031 74 20 62 65 20 75 6e 69 71 75 65 22 2c 20 50 34  t be unique", P4
22032 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71  _STATIC);.    sq
22033 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
22034 65 28 76 2c 20 61 64 64 72 32 29 3b 0a 20 20 20  e(v, addr2);.   
22035 20 61 75 74 6f 49 6e 63 53 74 65 70 28 70 50 61   autoIncStep(pPa
22036 72 73 65 2c 20 72 65 67 41 75 74 6f 69 6e 63 2c  rse, regAutoinc,
22037 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 7d 65   regRowid);.  }e
22038 6c 73 65 20 69 66 28 20 70 44 65 73 74 2d 3e 70  lse if( pDest->p
22039 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20  Index==0 ){.    
2203a 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56  addr1 = sqlite3V
2203b 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2203c 4e 65 77 52 6f 77 69 64 2c 20 69 44 65 73 74 2c  NewRowid, iDest,
2203d 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 7d 65   regRowid);.  }e
2203e 6c 73 65 7b 0a 20 20 20 20 61 64 64 72 31 20 3d  lse{.    addr1 =
2203f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22040 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20  p2(v, OP_Rowid, 
22041 69 53 72 63 2c 20 72 65 67 52 6f 77 69 64 29 3b  iSrc, regRowid);
22042 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 44  .    assert( (pD
22043 65 73 74 2d 3e 74 61 62 46 6c 61 67 73 20 26 20  est->tabFlags & 
22044 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74  TF_Autoincrement
22045 29 3d 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 73 71  )==0 );.  }.  sq
22046 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
22047 76 2c 20 4f 50 5f 52 6f 77 44 61 74 61 2c 20 69  v, OP_RowData, i
22048 53 72 63 2c 20 72 65 67 44 61 74 61 29 3b 0a 20  Src, regData);. 
22049 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2204a 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c  p3(v, OP_Insert,
2204b 20 69 44 65 73 74 2c 20 72 65 67 44 61 74 61 2c   iDest, regData,
2204c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 73 71   regRowid);.  sq
2204d 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
2204e 35 28 76 2c 20 4f 50 46 4c 41 47 5f 4e 43 48 41  5(v, OPFLAG_NCHA
2204f 4e 47 45 7c 4f 50 46 4c 41 47 5f 4c 41 53 54 52  NGE|OPFLAG_LASTR
22050 4f 57 49 44 7c 4f 50 46 4c 41 47 5f 41 50 50 45  OWID|OPFLAG_APPE
22051 4e 44 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ND);.  sqlite3Vd
22052 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31  beChangeP4(v, -1
22053 2c 20 70 44 65 73 74 2d 3e 7a 4e 61 6d 65 2c 20  , pDest->zName, 
22054 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  0);.  sqlite3Vdb
22055 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
22056 78 74 2c 20 69 53 72 63 2c 20 61 64 64 72 31 29  xt, iSrc, addr1)
22057 3b 0a 20 20 61 75 74 6f 49 6e 63 45 6e 64 28 70  ;.  autoIncEnd(p
22058 50 61 72 73 65 2c 20 69 44 62 44 65 73 74 2c 20  Parse, iDbDest, 
22059 70 44 65 73 74 2c 20 72 65 67 41 75 74 6f 69 6e  pDest, regAutoin
2205a 63 29 3b 0a 20 20 66 6f 72 28 70 44 65 73 74 49  c);.  for(pDestI
2205b 64 78 3d 70 44 65 73 74 2d 3e 70 49 6e 64 65 78  dx=pDest->pIndex
2205c 3b 20 70 44 65 73 74 49 64 78 3b 20 70 44 65 73  ; pDestIdx; pDes
2205d 74 49 64 78 3d 70 44 65 73 74 49 64 78 2d 3e 70  tIdx=pDestIdx->p
2205e 4e 65 78 74 29 7b 0a 20 20 20 20 66 6f 72 28 70  Next){.    for(p
2205f 53 72 63 49 64 78 3d 70 53 72 63 2d 3e 70 49 6e  SrcIdx=pSrc->pIn
22060 64 65 78 3b 20 70 53 72 63 49 64 78 3b 20 70 53  dex; pSrcIdx; pS
22061 72 63 49 64 78 3d 70 53 72 63 49 64 78 2d 3e 70  rcIdx=pSrcIdx->p
22062 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28  Next){.      if(
22063 20 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 49   xferCompatibleI
22064 6e 64 65 78 28 70 44 65 73 74 49 64 78 2c 20 70  ndex(pDestIdx, p
22065 53 72 63 49 64 78 29 20 29 20 62 72 65 61 6b 3b  SrcIdx) ) break;
22066 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
22067 74 28 20 70 53 72 63 49 64 78 20 29 3b 0a 20 20  t( pSrcIdx );.  
22068 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22069 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op2(v, OP_Close,
2206a 20 69 53 72 63 2c 20 30 29 3b 0a 20 20 20 20 73   iSrc, 0);.    s
2206b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2206c 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 44  (v, OP_Close, iD
2206d 65 73 74 2c 20 30 29 3b 0a 20 20 20 20 70 4b 65  est, 0);.    pKe
2206e 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78  y = sqlite3Index
2206f 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20  Keyinfo(pParse, 
22070 70 53 72 63 49 64 78 29 3b 0a 20 20 20 20 73 71  pSrcIdx);.    sq
22071 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
22072 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20  v, OP_OpenRead, 
22073 69 53 72 63 2c 20 70 53 72 63 49 64 78 2d 3e 74  iSrc, pSrcIdx->t
22074 6e 75 6d 2c 20 69 44 62 53 72 63 2c 0a 20 20 20  num, iDbSrc,.   
22075 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22076 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 2c 20     (char*)pKey, 
22077 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
22078 46 46 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d  FF);.    VdbeCom
22079 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70  ment((v, "%s", p
2207a 53 72 63 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b  SrcIdx->zName));
2207b 0a 20 20 20 20 70 4b 65 79 20 3d 20 73 71 6c 69  .    pKey = sqli
2207c 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28  te3IndexKeyinfo(
2207d 70 50 61 72 73 65 2c 20 70 44 65 73 74 49 64 78  pParse, pDestIdx
2207e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2207f 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f  beAddOp4(v, OP_O
22080 70 65 6e 57 72 69 74 65 2c 20 69 44 65 73 74 2c  penWrite, iDest,
22081 20 70 44 65 73 74 49 64 78 2d 3e 74 6e 75 6d 2c   pDestIdx->tnum,
22082 20 69 44 62 44 65 73 74 2c 0a 20 20 20 20 20 20   iDbDest,.      
22083 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22084 28 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f  (char*)pKey, P4_
22085 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29  KEYINFO_HANDOFF)
22086 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e  ;.    VdbeCommen
22087 74 28 28 76 2c 20 22 25 73 22 2c 20 70 44 65 73  t((v, "%s", pDes
22088 74 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20  tIdx->zName));. 
22089 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74     addr1 = sqlit
2208a 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2208b 4f 50 5f 52 65 77 69 6e 64 2c 20 69 53 72 63 2c  OP_Rewind, iSrc,
2208c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
2208d 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2208e 5f 52 6f 77 4b 65 79 2c 20 69 53 72 63 2c 20 72  _RowKey, iSrc, r
2208f 65 67 44 61 74 61 29 3b 0a 20 20 20 20 73 71 6c  egData);.    sql
22090 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
22091 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20  , OP_IdxInsert, 
22092 69 44 65 73 74 2c 20 72 65 67 44 61 74 61 2c 20  iDest, regData, 
22093 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
22094 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
22095 4e 65 78 74 2c 20 69 53 72 63 2c 20 61 64 64 72  Next, iSrc, addr
22096 31 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  1+1);.    sqlite
22097 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
22098 20 61 64 64 72 31 29 3b 0a 20 20 7d 0a 20 20 73   addr1);.  }.  s
22099 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
2209a 72 65 28 76 2c 20 65 6d 70 74 79 53 72 63 54 65  re(v, emptySrcTe
2209b 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65  st);.  sqlite3Re
2209c 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
2209d 72 73 65 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  rse, regRowid);.
2209e 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
2209f 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
220a0 72 65 67 44 61 74 61 29 3b 0a 20 20 73 71 6c 69  regData);.  sqli
220a1 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
220a2 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 53 72 63 2c   OP_Close, iSrc,
220a3 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64   0);.  sqlite3Vd
220a4 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
220a5 6c 6f 73 65 2c 20 69 44 65 73 74 2c 20 30 29 3b  lose, iDest, 0);
220a6 0a 20 20 69 66 28 20 65 6d 70 74 79 44 65 73 74  .  if( emptyDest
220a7 54 65 73 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  Test ){.    sqli
220a8 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
220a9 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45   OP_Halt, SQLITE
220aa 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  _OK, 0);.    sql
220ab 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
220ac 28 76 2c 20 65 6d 70 74 79 44 65 73 74 54 65 73  (v, emptyDestTes
220ad 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  t);.    sqlite3V
220ae 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
220af 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c 20 30 29  Close, iDest, 0)
220b0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
220b1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
220b2 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  urn 1;.  }.}.#en
220b3 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
220b4 49 54 5f 58 46 45 52 5f 4f 50 54 20 2a 2f 0a 0a  IT_XFER_OPT */..
220b5 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 22 69 73  /* Make sure "is
220b6 56 69 65 77 22 20 67 65 74 73 20 75 6e 64 65 66  View" gets undef
220b7 69 6e 65 64 20 69 6e 20 63 61 73 65 20 74 68 69  ined in case thi
220b8 73 20 66 69 6c 65 20 62 65 63 6f 6d 65 73 20 70  s file becomes p
220b9 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61 6d  art of.** the am
220ba 61 6c 67 61 6d 61 74 69 6f 6e 20 2d 20 73 6f 20  algamation - so 
220bb 74 68 61 74 20 73 75 62 73 65 71 75 65 6e 74 20  that subsequent 
220bc 66 69 6c 65 73 20 64 6f 20 6e 6f 74 20 73 65 65  files do not see
220bd 20 69 73 56 69 65 77 20 61 73 20 61 0a 2a 2a 20   isView as a.** 
220be 6d 61 63 72 6f 2e 20 2a 2f 0a 23 75 6e 64 65 66  macro. */.#undef
220bf 20 69 73 56 69 65 77 0a 0a 2f 2a 2a 2a 2a 2a 2a   isView../******
220c0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
220c1 69 6e 73 65 72 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a  insert.c *******
220c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
220c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
220c4 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
220c5 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
220c6 69 6c 65 20 6c 65 67 61 63 79 2e 63 20 2a 2a 2a  ile legacy.c ***
220c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
220c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
220c9 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
220ca 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
220cb 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
220cc 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
220cd 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
220ce 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
220cf 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
220d0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
220d1 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
220d2 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
220d3 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
220d4 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
220d5 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
220d6 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
220d7 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
220d8 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
220d9 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
220da 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
220db 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
220dc 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
220dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
220de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
220df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
220e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
220e1 2a 2a 20 4d 61 69 6e 20 66 69 6c 65 20 66 6f 72  ** Main file for
220e2 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
220e3 61 72 79 2e 20 20 54 68 65 20 72 6f 75 74 69 6e  ary.  The routin
220e4 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 0a  es in this file.
220e5 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  ** implement the
220e6 20 70 72 6f 67 72 61 6d 6d 65 72 20 69 6e 74 65   programmer inte
220e7 72 66 61 63 65 20 74 6f 20 74 68 65 20 6c 69 62  rface to the lib
220e8 72 61 72 79 2e 20 20 52 6f 75 74 69 6e 65 73 20  rary.  Routines 
220e9 69 6e 0a 2a 2a 20 6f 74 68 65 72 20 66 69 6c 65  in.** other file
220ea 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e  s are for intern
220eb 61 6c 20 75 73 65 20 62 79 20 53 51 4c 69 74 65  al use by SQLite
220ec 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20   and should not 
220ed 62 65 0a 2a 2a 20 61 63 63 65 73 73 65 64 20 62  be.** accessed b
220ee 79 20 75 73 65 72 73 20 6f 66 20 74 68 65 20 6c  y users of the l
220ef 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 24 49  ibrary..**.** $I
220f0 64 3a 20 6c 65 67 61 63 79 2e 63 2c 76 20 31 2e  d: legacy.c,v 1.
220f1 32 39 20 32 30 30 38 2f 30 38 2f 30 32 20 30 33  29 2008/08/02 03
220f2 3a 35 30 3a 33 39 20 64 72 68 20 45 78 70 20 24  :50:39 drh Exp $
220f3 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63  .*/.../*.** Exec
220f4 75 74 65 20 53 51 4c 20 63 6f 64 65 2e 20 20 52  ute SQL code.  R
220f5 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65  eturn one of the
220f6 20 53 51 4c 49 54 45 5f 20 73 75 63 63 65 73 73   SQLITE_ success
220f7 2f 66 61 69 6c 75 72 65 0a 2a 2a 20 63 6f 64 65  /failure.** code
220f8 73 2e 20 20 41 6c 73 6f 20 77 72 69 74 65 20 61  s.  Also write a
220f9 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
220fa 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61  into memory obta
220fb 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c  ined from.** mal
220fc 6c 6f 63 28 29 20 61 6e 64 20 6d 61 6b 65 20 2a  loc() and make *
220fd 70 7a 45 72 72 4d 73 67 20 70 6f 69 6e 74 20 74  pzErrMsg point t
220fe 6f 20 74 68 61 74 20 6d 65 73 73 61 67 65 2e 0a  o that message..
220ff 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c  **.** If the SQL
22100 20 69 73 20 61 20 71 75 65 72 79 2c 20 74 68 65   is a query, the
22101 6e 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 69  n for each row i
22102 6e 20 74 68 65 20 71 75 65 72 79 20 72 65 73 75  n the query resu
22103 6c 74 0a 2a 2a 20 74 68 65 20 78 43 61 6c 6c 62  lt.** the xCallb
22104 61 63 6b 28 29 20 66 75 6e 63 74 69 6f 6e 20 69  ack() function i
22105 73 20 63 61 6c 6c 65 64 2e 20 20 70 41 72 67 20  s called.  pArg 
22106 62 65 63 6f 6d 65 73 20 74 68 65 20 66 69 72 73  becomes the firs
22107 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f  t.** argument to
22108 20 78 43 61 6c 6c 62 61 63 6b 28 29 2e 20 20 49   xCallback().  I
22109 66 20 78 43 61 6c 6c 62 61 63 6b 3d 4e 55 4c 4c  f xCallback=NULL
2210a 20 74 68 65 6e 20 6e 6f 20 63 61 6c 6c 62 61 63   then no callbac
2210b 6b 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 2c  k.** is invoked,
2210c 20 65 76 65 6e 20 66 6f 72 20 71 75 65 72 69 65   even for querie
2210d 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
2210e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 65   int sqlite3_exe
2210f 63 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  c(.  sqlite3 *db
22110 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
22111 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
22112 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20 53 51   on which the SQ
22113 4c 20 65 78 65 63 75 74 65 73 20 2a 2f 0a 20 20  L executes */.  
22114 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
22115 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ,           /* T
22116 68 65 20 53 51 4c 20 74 6f 20 62 65 20 65 78 65  he SQL to be exe
22117 63 75 74 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74  cuted */.  sqlit
22118 65 33 5f 63 61 6c 6c 62 61 63 6b 20 78 43 61 6c  e3_callback xCal
22119 6c 62 61 63 6b 2c 20 2f 2a 20 49 6e 76 6f 6b 65  lback, /* Invoke
2211a 20 74 68 69 73 20 63 61 6c 6c 62 61 63 6b 20 72   this callback r
2211b 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 76 6f 69 64  outine */.  void
2211c 20 2a 70 41 72 67 2c 20 20 20 20 20 20 20 20 20   *pArg,         
2211d 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
2211e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 61   argument to xCa
2211f 6c 6c 62 61 63 6b 28 29 20 2a 2f 0a 20 20 63 68  llback() */.  ch
22120 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20  ar **pzErrMsg   
22121 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
22122 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  te error message
22123 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  s here */.){.  i
22124 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
22125 4b 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  K;.  const char 
22126 2a 7a 4c 65 66 74 6f 76 65 72 3b 0a 20 20 73 71  *zLeftover;.  sq
22127 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
22128 74 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 2a  t = 0;.  char **
22129 61 7a 43 6f 6c 73 20 3d 20 30 3b 0a 0a 20 20 69  azCols = 0;..  i
2212a 6e 74 20 6e 52 65 74 72 79 20 3d 20 30 3b 0a 20  nt nRetry = 0;. 
2212b 20 69 6e 74 20 6e 43 61 6c 6c 62 61 63 6b 3b 0a   int nCallback;.
2212c 0a 20 20 69 66 28 20 7a 53 71 6c 3d 3d 30 20 29  .  if( zSql==0 )
2212d 20 7a 53 71 6c 20 3d 20 22 22 3b 0a 0a 20 20 73   zSql = "";..  s
2212e 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
2212f 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
22130 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
22131 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b  , SQLITE_OK, 0);
22132 0a 20 20 77 68 69 6c 65 28 20 28 72 63 3d 3d 53  .  while( (rc==S
22133 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28 72 63 3d  QLITE_OK || (rc=
22134 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 26  =SQLITE_SCHEMA &
22135 26 20 28 2b 2b 6e 52 65 74 72 79 29 3c 32 29 29  & (++nRetry)<2))
22136 20 26 26 20 7a 53 71 6c 5b 30 5d 20 29 7b 0a 20   && zSql[0] ){. 
22137 20 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 20     int nCol;.   
22138 20 63 68 61 72 20 2a 2a 61 7a 56 61 6c 73 20 3d   char **azVals =
22139 20 30 3b 0a 0a 20 20 20 20 70 53 74 6d 74 20 3d   0;..    pStmt =
2213a 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   0;.    rc = sql
2213b 69 74 65 33 5f 70 72 65 70 61 72 65 28 64 62 2c  ite3_prepare(db,
2213c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d   zSql, -1, &pStm
2213d 74 2c 20 26 7a 4c 65 66 74 6f 76 65 72 29 3b 0a  t, &zLeftover);.
2213e 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
2213f 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 53 74  SQLITE_OK || pSt
22140 6d 74 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  mt==0 );.    if(
22141 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
22142 7b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65  {.      continue
22143 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
22144 21 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 20 20  !pStmt ){.      
22145 2f 2a 20 74 68 69 73 20 68 61 70 70 65 6e 73 20  /* this happens 
22146 66 6f 72 20 61 20 63 6f 6d 6d 65 6e 74 20 6f 72  for a comment or
22147 20 77 68 69 74 65 2d 73 70 61 63 65 20 2a 2f 0a   white-space */.
22148 20 20 20 20 20 20 7a 53 71 6c 20 3d 20 7a 4c 65        zSql = zLe
22149 66 74 6f 76 65 72 3b 0a 20 20 20 20 20 20 63 6f  ftover;.      co
2214a 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a 20  ntinue;.    }.. 
2214b 20 20 20 6e 43 61 6c 6c 62 61 63 6b 20 3d 20 30     nCallback = 0
2214c 3b 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 73 71 6c  ;.    nCol = sql
2214d 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e  ite3_column_coun
2214e 74 28 70 53 74 6d 74 29 3b 0a 0a 20 20 20 20 77  t(pStmt);..    w
2214f 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 20  hile( 1 ){.     
22150 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 72 63   int i;.      rc
22151 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
22152 70 53 74 6d 74 29 3b 0a 0a 20 20 20 20 20 20 2f  pStmt);..      /
22153 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c  * Invoke the cal
22154 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 69  lback function i
22155 66 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20  f required */.  
22156 20 20 20 20 69 66 28 20 78 43 61 6c 6c 62 61 63      if( xCallbac
22157 6b 20 26 26 20 28 53 51 4c 49 54 45 5f 52 4f 57  k && (SQLITE_ROW
22158 3d 3d 72 63 20 7c 7c 20 0a 20 20 20 20 20 20 20  ==rc || .       
22159 20 20 20 28 53 51 4c 49 54 45 5f 44 4f 4e 45 3d     (SQLITE_DONE=
2215a 3d 72 63 20 26 26 20 21 6e 43 61 6c 6c 62 61 63  =rc && !nCallbac
2215b 6b 20 26 26 20 64 62 2d 3e 66 6c 61 67 73 26 53  k && db->flags&S
2215c 51 4c 49 54 45 5f 4e 75 6c 6c 43 61 6c 6c 62 61  QLITE_NullCallba
2215d 63 6b 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ck)) ){.        
2215e 69 66 28 20 30 3d 3d 6e 43 61 6c 6c 62 61 63 6b  if( 0==nCallback
2215f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66   ){.          if
22160 28 20 61 7a 43 6f 6c 73 3d 3d 30 20 29 7b 0a 20  ( azCols==0 ){. 
22161 20 20 20 20 20 20 20 20 20 20 20 61 7a 43 6f 6c             azCol
22162 73 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  s = sqlite3DbMal
22163 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 32 2a 6e 43  locZero(db, 2*nC
22164 6f 6c 2a 73 69 7a 65 6f 66 28 63 6f 6e 73 74 20  ol*sizeof(const 
22165 63 68 61 72 2a 29 20 2b 20 31 29 3b 0a 20 20 20  char*) + 1);.   
22166 20 20 20 20 20 20 20 20 20 69 66 28 20 61 7a 43           if( azC
22167 6f 6c 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ols==0 ){.      
22168 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 65          goto exe
22169 63 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 20  c_out;.         
2216a 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d     }.          }
2216b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69  .          for(i
2216c 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29  =0; i<nCol; i++)
2216d 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 7a  {.            az
2216e 43 6f 6c 73 5b 69 5d 20 3d 20 28 63 68 61 72 20  Cols[i] = (char 
2216f 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  *)sqlite3_column
22170 5f 6e 61 6d 65 28 70 53 74 6d 74 2c 20 69 29 3b  _name(pStmt, i);
22171 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  .            /* 
22172 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
22173 6c 4e 61 6d 65 28 29 20 69 6e 73 74 61 6c 6c 73  lName() installs
22174 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 73   column names as
22175 20 55 54 46 38 0a 20 20 20 20 20 20 20 20 20 20   UTF8.          
22176 20 20 2a 2a 20 73 74 72 69 6e 67 73 20 73 6f 20    ** strings so 
22177 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20  there is no way 
22178 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  for sqlite3_colu
22179 6d 6e 5f 6e 61 6d 65 28 29 20 74 6f 20 66 61 69  mn_name() to fai
2217a 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  l. */.          
2217b 20 20 61 73 73 65 72 74 28 20 61 7a 43 6f 6c 73    assert( azCols
2217c 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  [i]!=0 );.      
2217d 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
2217e 6e 43 61 6c 6c 62 61 63 6b 2b 2b 3b 0a 20 20 20  nCallback++;.   
2217f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
22180 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f  f( rc==SQLITE_RO
22181 57 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  W ){.          a
22182 7a 56 61 6c 73 20 3d 20 26 61 7a 43 6f 6c 73 5b  zVals = &azCols[
22183 6e 43 6f 6c 5d 3b 0a 20 20 20 20 20 20 20 20 20  nCol];.         
22184 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c   for(i=0; i<nCol
22185 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
22186 20 20 20 20 61 7a 56 61 6c 73 5b 69 5d 20 3d 20      azVals[i] = 
22187 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f  (char *)sqlite3_
22188 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 70 53 74 6d  column_text(pStm
22189 74 2c 20 69 29 3b 0a 20 20 20 20 20 20 20 20 20  t, i);.         
2218a 20 20 20 69 66 28 20 21 61 7a 56 61 6c 73 5b 69     if( !azVals[i
2218b 5d 20 26 26 20 73 71 6c 69 74 65 33 5f 63 6f 6c  ] && sqlite3_col
2218c 75 6d 6e 5f 74 79 70 65 28 70 53 74 6d 74 2c 20  umn_type(pStmt, 
2218d 69 29 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20  i)!=SQLITE_NULL 
2218e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
2218f 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
22190 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  d = 1;.         
22191 20 20 20 20 20 67 6f 74 6f 20 65 78 65 63 5f 6f       goto exec_o
22192 75 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ut;.            
22193 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  }.          }.  
22194 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
22195 69 66 28 20 78 43 61 6c 6c 62 61 63 6b 28 70 41  if( xCallback(pA
22196 72 67 2c 20 6e 43 6f 6c 2c 20 61 7a 56 61 6c 73  rg, nCol, azVals
22197 2c 20 61 7a 43 6f 6c 73 29 20 29 7b 0a 20 20 20  , azCols) ){.   
22198 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
22199 54 45 5f 41 42 4f 52 54 3b 0a 20 20 20 20 20 20  TE_ABORT;.      
2219a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61      sqlite3_fina
2219b 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 20 20 20  lize(pStmt);.   
2219c 20 20 20 20 20 20 20 70 53 74 6d 74 20 3d 20 30         pStmt = 0
2219d 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
2219e 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c  te3Error(db, SQL
2219f 49 54 45 5f 41 42 4f 52 54 2c 20 30 29 3b 0a 20  ITE_ABORT, 0);. 
221a0 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78           goto ex
221a1 65 63 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20  ec_out;.        
221a2 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  }.      }..     
221a3 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
221a4 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ROW ){.        r
221a5 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  c = sqlite3_fina
221a6 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 20 20 20  lize(pStmt);.   
221a7 20 20 20 20 20 70 53 74 6d 74 20 3d 20 30 3b 0a       pStmt = 0;.
221a8 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
221a9 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 29 7b  SQLITE_SCHEMA ){
221aa 0a 20 20 20 20 20 20 20 20 20 20 6e 52 65 74 72  .          nRetr
221ab 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  y = 0;.         
221ac 20 7a 53 71 6c 20 3d 20 7a 4c 65 66 74 6f 76 65   zSql = zLeftove
221ad 72 3b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69  r;.          whi
221ae 6c 65 28 20 69 73 73 70 61 63 65 28 28 75 6e 73  le( isspace((uns
221af 69 67 6e 65 64 20 63 68 61 72 29 7a 53 71 6c 5b  igned char)zSql[
221b0 30 5d 29 20 29 20 7a 53 71 6c 2b 2b 3b 0a 20 20  0]) ) zSql++;.  
221b1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
221b2 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
221b3 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65     }..    sqlite
221b4 33 44 62 46 72 65 65 28 64 62 2c 20 61 7a 43 6f  3DbFree(db, azCo
221b5 6c 73 29 3b 0a 20 20 20 20 61 7a 43 6f 6c 73 20  ls);.    azCols 
221b6 3d 20 30 3b 0a 20 20 7d 0a 0a 65 78 65 63 5f 6f  = 0;.  }..exec_o
221b7 75 74 3a 0a 20 20 69 66 28 20 70 53 74 6d 74 20  ut:.  if( pStmt 
221b8 29 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69  ) sqlite3_finali
221b9 7a 65 28 70 53 74 6d 74 29 3b 0a 20 20 73 71 6c  ze(pStmt);.  sql
221ba 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 61  ite3DbFree(db, a
221bb 7a 43 6f 6c 73 29 3b 0a 0a 20 20 72 63 20 3d 20  zCols);..  rc = 
221bc 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
221bd 62 2c 20 72 63 29 3b 0a 20 20 69 66 28 20 72 63  b, rc);.  if( rc
221be 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72  !=SQLITE_OK && r
221bf 63 3d 3d 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  c==sqlite3_errco
221c0 64 65 28 64 62 29 20 26 26 20 70 7a 45 72 72 4d  de(db) && pzErrM
221c1 73 67 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 45  sg ){.    int nE
221c2 72 72 4d 73 67 20 3d 20 31 20 2b 20 73 74 72 6c  rrMsg = 1 + strl
221c3 65 6e 28 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  en(sqlite3_errms
221c4 67 28 64 62 29 29 3b 0a 20 20 20 20 2a 70 7a 45  g(db));.    *pzE
221c5 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d  rrMsg = sqlite3M
221c6 61 6c 6c 6f 63 28 6e 45 72 72 4d 73 67 29 3b 0a  alloc(nErrMsg);.
221c7 20 20 20 20 69 66 28 20 2a 70 7a 45 72 72 4d 73      if( *pzErrMs
221c8 67 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  g ){.      memcp
221c9 79 28 2a 70 7a 45 72 72 4d 73 67 2c 20 73 71 6c  y(*pzErrMsg, sql
221ca 69 74 65 33 5f 65 72 72 6d 73 67 28 64 62 29 2c  ite3_errmsg(db),
221cb 20 6e 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 7d   nErrMsg);.    }
221cc 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 7a 45  .  }else if( pzE
221cd 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 2a 70 7a  rrMsg ){.    *pz
221ce 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a  ErrMsg = 0;.  }.
221cf 0a 20 20 61 73 73 65 72 74 28 20 28 72 63 26 64  .  assert( (rc&d
221d0 62 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d 72 63 20  b->errMask)==rc 
221d1 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
221d2 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
221d3 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
221d4 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
221d5 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6c 65 67 61  **** End of lega
221d6 63 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  cy.c ***********
221d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
221d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
221d9 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
221da 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
221db 6c 6f 61 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a  loadext.c ******
221dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
221dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
221de 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20  ***/./*.** 2006 
221df 4a 75 6e 65 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65  June 7.**.** The
221e0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
221e1 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
221e2 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
221e3 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
221e4 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
221e5 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
221e6 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
221e7 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
221e8 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
221e9 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
221ea 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
221eb 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
221ec 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
221ed 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
221ee 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
221ef 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
221f0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
221f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
221f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
221f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
221f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
221f5 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
221f6 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
221f7 20 75 73 65 64 20 74 6f 20 64 79 6e 61 6d 69 63   used to dynamic
221f8 61 6c 6c 79 20 6c 6f 61 64 20 65 78 74 65 6e 73  ally load extens
221f9 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a 20 74 68 65  ions into.** the
221fa 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e   SQLite library.
221fb 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6c 6f 61 64  .**.** $Id: load
221fc 65 78 74 2e 63 2c 76 20 31 2e 35 36 20 32 30 30  ext.c,v 1.56 200
221fd 38 2f 31 30 2f 31 32 20 30 30 3a 32 37 3a 35 33  8/10/12 00:27:53
221fe 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a   shane Exp $.*/.
221ff 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
22200 43 4f 52 45 0a 20 20 23 64 65 66 69 6e 65 20 53  CORE.  #define S
22201 51 4c 49 54 45 5f 43 4f 52 45 20 31 20 20 2f 2a  QLITE_CORE 1  /*
22202 20 44 69 73 61 62 6c 65 20 74 68 65 20 41 50 49   Disable the API
22203 20 72 65 64 65 66 69 6e 69 74 69 6f 6e 20 69 6e   redefinition in
22204 20 73 71 6c 69 74 65 33 65 78 74 2e 68 20 2a 2f   sqlite3ext.h */
22205 0a 23 65 6e 64 69 66 0a 2f 2a 2a 2a 2a 2a 2a 2a  .#endif./*******
22206 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
22207 73 71 6c 69 74 65 33 65 78 74 2e 68 20 69 6e 20  sqlite3ext.h in 
22208 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6c 6f  the middle of lo
22209 61 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  adext.c ********
2220a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
2220b 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
2220c 6c 65 20 73 71 6c 69 74 65 33 65 78 74 2e 68 20  le sqlite3ext.h 
2220d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2220e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2220f 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
22210 30 36 20 4a 75 6e 65 20 37 0a 2a 2a 0a 2a 2a 20  06 June 7.**.** 
22211 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
22212 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
22213 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
22214 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
22215 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
22216 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
22217 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
22218 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
22219 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
2221a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
2221b 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
2221c 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
2221d 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
2221e 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
2221f 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
22220 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
22221 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
22222 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22223 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22224 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22225 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22226 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
22227 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65 66   header file def
22228 69 6e 65 73 20 74 68 65 20 53 51 4c 69 74 65 20  ines the SQLite 
22229 69 6e 74 65 72 66 61 63 65 20 66 6f 72 20 75 73  interface for us
2222a 65 20 62 79 0a 2a 2a 20 73 68 61 72 65 64 20 6c  e by.** shared l
2222b 69 62 72 61 72 69 65 73 20 74 68 61 74 20 77 61  ibraries that wa
2222c 6e 74 20 74 6f 20 62 65 20 69 6d 70 6f 72 74 65  nt to be importe
2222d 64 20 61 73 20 65 78 74 65 6e 73 69 6f 6e 73 20  d as extensions 
2222e 69 6e 74 6f 0a 2a 2a 20 61 6e 20 53 51 4c 69 74  into.** an SQLit
2222f 65 20 69 6e 73 74 61 6e 63 65 2e 20 20 53 68 61  e instance.  Sha
22230 72 65 64 20 6c 69 62 72 61 72 69 65 73 20 74 68  red libraries th
22231 61 74 20 69 6e 74 65 6e 64 20 74 6f 20 62 65 20  at intend to be 
22232 6c 6f 61 64 65 64 0a 2a 2a 20 61 73 20 65 78 74  loaded.** as ext
22233 65 6e 73 69 6f 6e 73 20 62 79 20 53 51 4c 69 74  ensions by SQLit
22234 65 20 73 68 6f 75 6c 64 20 23 69 6e 63 6c 75 64  e should #includ
22235 65 20 74 68 69 73 20 66 69 6c 65 20 69 6e 73 74  e this file inst
22236 65 61 64 20 6f 66 20 0a 2a 2a 20 73 71 6c 69 74  ead of .** sqlit
22237 65 33 2e 68 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29  e3.h..**.** @(#)
22238 20 24 49 64 3a 20 73 71 6c 69 74 65 33 65 78 74   $Id: sqlite3ext
22239 2e 68 2c 76 20 31 2e 32 35 20 32 30 30 38 2f 31  .h,v 1.25 2008/1
2223a 30 2f 31 32 20 30 30 3a 32 37 3a 35 34 20 73 68  0/12 00:27:54 sh
2223b 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66  ane Exp $.*/.#if
2223c 6e 64 65 66 20 5f 53 51 4c 49 54 45 33 45 58 54  ndef _SQLITE3EXT
2223d 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c  _H_.#define _SQL
2223e 49 54 45 33 45 58 54 5f 48 5f 0a 0a 74 79 70 65  ITE3EXT_H_..type
2223f 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
22240 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20  e3_api_routines 
22241 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74  sqlite3_api_rout
22242 69 6e 65 73 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ines;../*.** The
22243 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
22244 74 75 72 65 20 68 6f 6c 64 73 20 70 6f 69 6e 74  ture holds point
22245 65 72 73 20 74 6f 20 61 6c 6c 20 6f 66 20 74 68  ers to all of th
22246 65 20 53 51 4c 69 74 65 20 41 50 49 0a 2a 2a 20  e SQLite API.** 
22247 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20  routines..**.** 
22248 57 41 52 4e 49 4e 47 3a 20 20 49 6e 20 6f 72 64  WARNING:  In ord
22249 65 72 20 74 6f 20 6d 61 69 6e 74 61 69 6e 20 62  er to maintain b
2224a 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69  ackwards compati
2224b 62 69 6c 69 74 79 2c 20 61 64 64 20 6e 65 77 0a  bility, add new.
2224c 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 74 6f  ** interfaces to
2224d 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 69 73   the end of this
2224e 20 73 74 72 75 63 74 75 72 65 20 6f 6e 6c 79 2e   structure only.
2224f 20 20 49 66 20 79 6f 75 20 69 6e 73 65 72 74 20    If you insert 
22250 6e 65 77 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  new.** interface
22251 73 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  s in the middle 
22252 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
22253 65 2c 20 74 68 65 6e 20 6f 6c 64 65 72 20 64 69  e, then older di
22254 66 66 65 72 65 6e 74 0a 2a 2a 20 76 65 72 73 69  fferent.** versi
22255 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 77 69  ons of SQLite wi
22256 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74  ll not be able t
22257 6f 20 6c 6f 61 64 20 65 61 63 68 20 6f 74 68 65  o load each othe
22258 72 73 27 20 73 68 61 72 65 64 0a 2a 2a 20 6c 69  rs' shared.** li
22259 62 72 61 72 69 65 73 21 0a 2a 2f 0a 73 74 72 75  braries!.*/.stru
2225a 63 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  ct sqlite3_api_r
2225b 6f 75 74 69 6e 65 73 20 7b 0a 20 20 76 6f 69 64  outines {.  void
2225c 20 2a 20 28 2a 61 67 67 72 65 67 61 74 65 5f 63   * (*aggregate_c
2225d 6f 6e 74 65 78 74 29 28 73 71 6c 69 74 65 33 5f  ontext)(sqlite3_
2225e 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 20 6e 42 79  context*,int nBy
2225f 74 65 73 29 3b 0a 20 20 69 6e 74 20 20 28 2a 61  tes);.  int  (*a
22260 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 29 28  ggregate_count)(
22261 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
22262 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64  );.  int  (*bind
22263 5f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f 73  _blob)(sqlite3_s
22264 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  tmt*,int,const v
22265 6f 69 64 2a 2c 69 6e 74 20 6e 2c 76 6f 69 64 28  oid*,int n,void(
22266 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e  *)(void*));.  in
22267 74 20 20 28 2a 62 69 6e 64 5f 64 6f 75 62 6c 65  t  (*bind_double
22268 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
22269 69 6e 74 2c 64 6f 75 62 6c 65 29 3b 0a 20 20 69  int,double);.  i
2226a 6e 74 20 20 28 2a 62 69 6e 64 5f 69 6e 74 29 28  nt  (*bind_int)(
2226b 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
2226c 74 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28  t,int);.  int  (
2226d 2a 62 69 6e 64 5f 69 6e 74 36 34 29 28 73 71 6c  *bind_int64)(sql
2226e 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 73  ite3_stmt*,int,s
2226f 71 6c 69 74 65 5f 69 6e 74 36 34 29 3b 0a 20 20  qlite_int64);.  
22270 69 6e 74 20 20 28 2a 62 69 6e 64 5f 6e 75 6c 6c  int  (*bind_null
22271 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
22272 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62  int);.  int  (*b
22273 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f  ind_parameter_co
22274 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  unt)(sqlite3_stm
22275 74 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69  t*);.  int  (*bi
22276 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64  nd_parameter_ind
22277 65 78 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ex)(sqlite3_stmt
22278 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 7a 4e 61  *,const char*zNa
22279 6d 65 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  me);.  const cha
2227a 72 20 2a 20 28 2a 62 69 6e 64 5f 70 61 72 61 6d  r * (*bind_param
2227b 65 74 65 72 5f 6e 61 6d 65 29 28 73 71 6c 69 74  eter_name)(sqlit
2227c 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20  e3_stmt*,int);. 
2227d 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 74 65 78   int  (*bind_tex
2227e 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  t)(sqlite3_stmt*
2227f 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a  ,int,const char*
22280 2c 69 6e 74 20 6e 2c 76 6f 69 64 28 2a 29 28 76  ,int n,void(*)(v
22281 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28  oid*));.  int  (
22282 2a 62 69 6e 64 5f 74 65 78 74 31 36 29 28 73 71  *bind_text16)(sq
22283 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c  lite3_stmt*,int,
22284 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
22285 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
22286 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 76  .  int  (*bind_v
22287 61 6c 75 65 29 28 73 71 6c 69 74 65 33 5f 73 74  alue)(sqlite3_st
22288 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 73 71  mt*,int,const sq
22289 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20  lite3_value*);. 
2228a 20 69 6e 74 20 20 28 2a 62 75 73 79 5f 68 61 6e   int  (*busy_han
2228b 64 6c 65 72 29 28 73 71 6c 69 74 65 33 2a 2c 69  dler)(sqlite3*,i
2228c 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 29  nt(*)(void*,int)
2228d 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20  ,void*);.  int  
2228e 28 2a 62 75 73 79 5f 74 69 6d 65 6f 75 74 29 28  (*busy_timeout)(
2228f 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 6d 73 29  sqlite3*,int ms)
22290 3b 0a 20 20 69 6e 74 20 20 28 2a 63 68 61 6e 67  ;.  int  (*chang
22291 65 73 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20  es)(sqlite3*);. 
22292 20 69 6e 74 20 20 28 2a 63 6c 6f 73 65 29 28 73   int  (*close)(s
22293 71 6c 69 74 65 33 2a 29 3b 0a 20 20 69 6e 74 20  qlite3*);.  int 
22294 20 28 2a 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65   (*collation_nee
22295 64 65 64 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f  ded)(sqlite3*,vo
22296 69 64 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  id*,void(*)(void
22297 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65  *,sqlite3*,int e
22298 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68  TextRep,const ch
22299 61 72 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a  ar*));.  int  (*
2229a 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
2229b 31 36 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69  16)(sqlite3*,voi
2229c 64 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  d*,void(*)(void*
2229d 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54  ,sqlite3*,int eT
2229e 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69  extRep,const voi
2229f 64 2a 29 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  d*));.  const vo
222a0 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 62 6c  id * (*column_bl
222a1 6f 62 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ob)(sqlite3_stmt
222a2 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69  *,int iCol);.  i
222a3 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 62 79 74  nt  (*column_byt
222a4 65 73 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  es)(sqlite3_stmt
222a5 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69  *,int iCol);.  i
222a6 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 62 79 74  nt  (*column_byt
222a7 65 73 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74  es16)(sqlite3_st
222a8 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20  mt*,int iCol);. 
222a9 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 63   int  (*column_c
222aa 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74  ount)(sqlite3_st
222ab 6d 74 2a 70 53 74 6d 74 29 3b 0a 20 20 63 6f 6e  mt*pStmt);.  con
222ac 73 74 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75  st char * (*colu
222ad 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
222ae 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
222af 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  int);.  const vo
222b0 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 61  id * (*column_da
222b1 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 29 28 73  tabase_name16)(s
222b2 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
222b3 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  );.  const char 
222b4 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74  * (*column_declt
222b5 79 70 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ype)(sqlite3_stm
222b6 74 2a 2c 69 6e 74 20 69 29 3b 0a 20 20 63 6f 6e  t*,int i);.  con
222b7 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75  st void * (*colu
222b8 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 29 28 73  mn_decltype16)(s
222b9 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
222ba 29 3b 0a 20 20 64 6f 75 62 6c 65 20 20 28 2a 63  );.  double  (*c
222bb 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 29 28 73 71  olumn_double)(sq
222bc 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20  lite3_stmt*,int 
222bd 69 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a  iCol);.  int  (*
222be 63 6f 6c 75 6d 6e 5f 69 6e 74 29 28 73 71 6c 69  column_int)(sqli
222bf 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43  te3_stmt*,int iC
222c0 6f 6c 29 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e  ol);.  sqlite_in
222c1 74 36 34 20 20 28 2a 63 6f 6c 75 6d 6e 5f 69 6e  t64  (*column_in
222c2 74 36 34 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  t64)(sqlite3_stm
222c3 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20  t*,int iCol);.  
222c4 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 63  const char * (*c
222c5 6f 6c 75 6d 6e 5f 6e 61 6d 65 29 28 73 71 6c 69  olumn_name)(sqli
222c6 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a  te3_stmt*,int);.
222c7 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28    const void * (
222c8 2a 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 29 28  *column_name16)(
222c9 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
222ca 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  t);.  const char
222cb 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 6f 72 69 67   * (*column_orig
222cc 69 6e 5f 6e 61 6d 65 29 28 73 71 6c 69 74 65 33  in_name)(sqlite3
222cd 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63  _stmt*,int);.  c
222ce 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f  onst void * (*co
222cf 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
222d0 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  16)(sqlite3_stmt
222d1 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20  *,int);.  const 
222d2 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f  char * (*column_
222d3 74 61 62 6c 65 5f 6e 61 6d 65 29 28 73 71 6c 69  table_name)(sqli
222d4 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a  te3_stmt*,int);.
222d5 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28    const void * (
222d6 2a 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  *column_table_na
222d7 6d 65 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74  me16)(sqlite3_st
222d8 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73  mt*,int);.  cons
222d9 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
222da 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74 65 78 74 29  * (*column_text)
222db 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
222dc 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 63 6f 6e 73  nt iCol);.  cons
222dd 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d  t void * (*colum
222de 6e 5f 74 65 78 74 31 36 29 28 73 71 6c 69 74 65  n_text16)(sqlite
222df 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c  3_stmt*,int iCol
222e0 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75  );.  int  (*colu
222e1 6d 6e 5f 74 79 70 65 29 28 73 71 6c 69 74 65 33  mn_type)(sqlite3
222e2 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29  _stmt*,int iCol)
222e3 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ;.  sqlite3_valu
222e4 65 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 76 61 6c 75  e* (*column_valu
222e5 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  e)(sqlite3_stmt*
222e6 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 76 6f  ,int iCol);.  vo
222e7 69 64 20 2a 20 28 2a 63 6f 6d 6d 69 74 5f 68 6f  id * (*commit_ho
222e8 6f 6b 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74  ok)(sqlite3*,int
222e9 28 2a 29 28 76 6f 69 64 2a 29 2c 76 6f 69 64 2a  (*)(void*),void*
222ea 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6d 70  );.  int  (*comp
222eb 6c 65 74 65 29 28 63 6f 6e 73 74 20 63 68 61 72  lete)(const char
222ec 2a 73 71 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a  *sql);.  int  (*
222ed 63 6f 6d 70 6c 65 74 65 31 36 29 28 63 6f 6e 73  complete16)(cons
222ee 74 20 76 6f 69 64 2a 73 71 6c 29 3b 0a 20 20 69  t void*sql);.  i
222ef 6e 74 20 20 28 2a 63 72 65 61 74 65 5f 63 6f 6c  nt  (*create_col
222f0 6c 61 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 2a  lation)(sqlite3*
222f1 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74  ,const char*,int
222f2 2c 76 6f 69 64 2a 2c 69 6e 74 28 2a 29 28 76 6f  ,void*,int(*)(vo
222f3 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
222f4 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
222f5 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a  id*));.  int  (*
222f6 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
222f7 31 36 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  16)(sqlite3*,con
222f8 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69  st void*,int,voi
222f9 64 2a 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c  d*,int(*)(void*,
222fa 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
222fb 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29  int,const void*)
222fc 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 72 65 61  );.  int  (*crea
222fd 74 65 5f 66 75 6e 63 74 69 6f 6e 29 28 73 71 6c  te_function)(sql
222fe 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ite3*,const char
222ff 2a 2c 69 6e 74 2c 69 6e 74 2c 76 6f 69 64 2a 2c  *,int,int,void*,
22300 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71  void (*xFunc)(sq
22301 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
22302 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
22303 2a 2a 29 2c 76 6f 69 64 20 28 2a 78 53 74 65 70  **),void (*xStep
22304 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
22305 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
22306 61 6c 75 65 2a 2a 29 2c 76 6f 69 64 20 28 2a 78  alue**),void (*x
22307 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63  Final)(sqlite3_c
22308 6f 6e 74 65 78 74 2a 29 29 3b 0a 20 20 69 6e 74  ontext*));.  int
22309 20 20 28 2a 63 72 65 61 74 65 5f 66 75 6e 63 74    (*create_funct
2230a 69 6f 6e 31 36 29 28 73 71 6c 69 74 65 33 2a 2c  ion16)(sqlite3*,
2230b 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
2230c 69 6e 74 2c 76 6f 69 64 2a 2c 76 6f 69 64 20 28  int,void*,void (
2230d 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f  *xFunc)(sqlite3_
2230e 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
2230f 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 76 6f  ite3_value**),vo
22310 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69  id (*xStep)(sqli
22311 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
22312 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a  ,sqlite3_value**
22313 29 2c 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29  ),void (*xFinal)
22314 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
22315 2a 29 29 3b 0a 20 20 69 6e 74 20 28 2a 63 72 65  *));.  int (*cre
22316 61 74 65 5f 6d 6f 64 75 6c 65 29 28 73 71 6c 69  ate_module)(sqli
22317 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
22318 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d  ,const sqlite3_m
22319 6f 64 75 6c 65 2a 2c 76 6f 69 64 2a 29 3b 0a 20  odule*,void*);. 
2231a 20 69 6e 74 20 20 28 2a 64 61 74 61 5f 63 6f 75   int  (*data_cou
2231b 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  nt)(sqlite3_stmt
2231c 2a 70 53 74 6d 74 29 3b 0a 20 20 73 71 6c 69 74  *pStmt);.  sqlit
2231d 65 33 20 2a 20 28 2a 64 62 5f 68 61 6e 64 6c 65  e3 * (*db_handle
2231e 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29  )(sqlite3_stmt*)
2231f 3b 0a 20 20 69 6e 74 20 28 2a 64 65 63 6c 61 72  ;.  int (*declar
22320 65 5f 76 74 61 62 29 28 73 71 6c 69 74 65 33 2a  e_vtab)(sqlite3*
22321 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20  ,const char*);. 
22322 20 69 6e 74 20 20 28 2a 65 6e 61 62 6c 65 5f 73   int  (*enable_s
22323 68 61 72 65 64 5f 63 61 63 68 65 29 28 69 6e 74  hared_cache)(int
22324 29 3b 0a 20 20 69 6e 74 20 20 28 2a 65 72 72 63  );.  int  (*errc
22325 6f 64 65 29 28 73 71 6c 69 74 65 33 2a 64 62 29  ode)(sqlite3*db)
22326 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
22327 20 28 2a 65 72 72 6d 73 67 29 28 73 71 6c 69 74   (*errmsg)(sqlit
22328 65 33 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  e3*);.  const vo
22329 69 64 20 2a 20 28 2a 65 72 72 6d 73 67 31 36 29  id * (*errmsg16)
2232a 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 69 6e  (sqlite3*);.  in
2232b 74 20 20 28 2a 65 78 65 63 29 28 73 71 6c 69 74  t  (*exec)(sqlit
2232c 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e3*,const char*,
2232d 73 71 6c 69 74 65 33 5f 63 61 6c 6c 62 61 63 6b  sqlite3_callback
2232e 2c 76 6f 69 64 2a 2c 63 68 61 72 2a 2a 29 3b 0a  ,void*,char**);.
2232f 20 20 69 6e 74 20 20 28 2a 65 78 70 69 72 65 64    int  (*expired
22330 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29  )(sqlite3_stmt*)
22331 3b 0a 20 20 69 6e 74 20 20 28 2a 66 69 6e 61 6c  ;.  int  (*final
22332 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ize)(sqlite3_stm
22333 74 2a 70 53 74 6d 74 29 3b 0a 20 20 76 6f 69 64  t*pStmt);.  void
22334 20 20 28 2a 66 72 65 65 29 28 76 6f 69 64 2a 29    (*free)(void*)
22335 3b 0a 20 20 76 6f 69 64 20 20 28 2a 66 72 65 65  ;.  void  (*free
22336 5f 74 61 62 6c 65 29 28 63 68 61 72 2a 2a 72 65  _table)(char**re
22337 73 75 6c 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a  sult);.  int  (*
22338 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 29 28  get_autocommit)(
22339 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 76 6f 69  sqlite3*);.  voi
2233a 64 20 2a 20 28 2a 67 65 74 5f 61 75 78 64 61 74  d * (*get_auxdat
2233b 61 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  a)(sqlite3_conte
2233c 78 74 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20  xt*,int);.  int 
2233d 20 28 2a 67 65 74 5f 74 61 62 6c 65 29 28 73 71   (*get_table)(sq
2233e 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
2233f 72 2a 2c 63 68 61 72 2a 2a 2a 2c 69 6e 74 2a 2c  r*,char***,int*,
22340 69 6e 74 2a 2c 63 68 61 72 2a 2a 29 3b 0a 20 20  int*,char**);.  
22341 69 6e 74 20 20 28 2a 67 6c 6f 62 61 6c 5f 72 65  int  (*global_re
22342 63 6f 76 65 72 29 28 76 6f 69 64 29 3b 0a 20 20  cover)(void);.  
22343 76 6f 69 64 20 20 28 2a 69 6e 74 65 72 72 75 70  void  (*interrup
22344 74 78 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20  tx)(sqlite3*);. 
22345 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 20 28   sqlite_int64  (
22346 2a 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77  *last_insert_row
22347 69 64 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20  id)(sqlite3*);. 
22348 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a   const char * (*
22349 6c 69 62 76 65 72 73 69 6f 6e 29 28 76 6f 69 64  libversion)(void
2234a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 6c 69 62 76  );.  int  (*libv
2234b 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 29 28 76  ersion_number)(v
2234c 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a  oid);.  void *(*
2234d 6d 61 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a 20 20  malloc)(int);.  
2234e 63 68 61 72 20 2a 20 28 2a 6d 70 72 69 6e 74 66  char * (*mprintf
2234f 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e  )(const char*,..
22350 2e 29 3b 0a 20 20 69 6e 74 20 20 28 2a 6f 70 65  .);.  int  (*ope
22351 6e 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73  n)(const char*,s
22352 71 6c 69 74 65 33 2a 2a 29 3b 0a 20 20 69 6e 74  qlite3**);.  int
22353 20 20 28 2a 6f 70 65 6e 31 36 29 28 63 6f 6e 73    (*open16)(cons
22354 74 20 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a  t void*,sqlite3*
22355 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 70 72 65  *);.  int  (*pre
22356 70 61 72 65 29 28 73 71 6c 69 74 65 33 2a 2c 63  pare)(sqlite3*,c
22357 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 73  onst char*,int,s
22358 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f  qlite3_stmt**,co
22359 6e 73 74 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69  nst char**);.  i
2235a 6e 74 20 20 28 2a 70 72 65 70 61 72 65 31 36 29  nt  (*prepare16)
2235b 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
2235c 76 6f 69 64 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  void*,int,sqlite
2235d 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 76  3_stmt**,const v
2235e 6f 69 64 2a 2a 29 3b 0a 20 20 76 6f 69 64 20 2a  oid**);.  void *
2235f 20 28 2a 70 72 6f 66 69 6c 65 29 28 73 71 6c 69   (*profile)(sqli
22360 74 65 33 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69  te3*,void(*)(voi
22361 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73  d*,const char*,s
22362 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 2c 76 6f  qlite_uint64),vo
22363 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  id*);.  void  (*
22364 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72  progress_handler
22365 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 69  )(sqlite3*,int,i
22366 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 76 6f 69  nt(*)(void*),voi
22367 64 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 72  d*);.  void *(*r
22368 65 61 6c 6c 6f 63 29 28 76 6f 69 64 2a 2c 69 6e  ealloc)(void*,in
22369 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a 72 65 73  t);.  int  (*res
2236a 65 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  et)(sqlite3_stmt
2236b 2a 70 53 74 6d 74 29 3b 0a 20 20 76 6f 69 64 20  *pStmt);.  void 
2236c 20 28 2a 72 65 73 75 6c 74 5f 62 6c 6f 62 29 28   (*result_blob)(
2236d 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
2236e 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
2236f 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29  ,void(*)(void*))
22370 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75  ;.  void  (*resu
22371 6c 74 5f 64 6f 75 62 6c 65 29 28 73 71 6c 69 74  lt_double)(sqlit
22372 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 64 6f 75 62  e3_context*,doub
22373 6c 65 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72  le);.  void  (*r
22374 65 73 75 6c 74 5f 65 72 72 6f 72 29 28 73 71 6c  esult_error)(sql
22375 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f  ite3_context*,co
22376 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0a  nst char*,int);.
22377 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74    void  (*result
22378 5f 65 72 72 6f 72 31 36 29 28 73 71 6c 69 74 65  _error16)(sqlite
22379 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74  3_context*,const
2237a 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 0a 20 20 76   void*,int);.  v
2237b 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 69 6e  oid  (*result_in
2237c 74 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  t)(sqlite3_conte
2237d 78 74 2a 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 64  xt*,int);.  void
2237e 20 20 28 2a 72 65 73 75 6c 74 5f 69 6e 74 36 34    (*result_int64
2237f 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
22380 74 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34 29  t*,sqlite_int64)
22381 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75  ;.  void  (*resu
22382 6c 74 5f 6e 75 6c 6c 29 28 73 71 6c 69 74 65 33  lt_null)(sqlite3
22383 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 76 6f  _context*);.  vo
22384 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 74 65 78  id  (*result_tex
22385 74 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  t)(sqlite3_conte
22386 78 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  xt*,const char*,
22387 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  int,void(*)(void
22388 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72  *));.  void  (*r
22389 65 73 75 6c 74 5f 74 65 78 74 31 36 29 28 73 71  esult_text16)(sq
2238a 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63  lite3_context*,c
2238b 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76  onst void*,int,v
2238c 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
2238d 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74    void  (*result
2238e 5f 74 65 78 74 31 36 62 65 29 28 73 71 6c 69 74  _text16be)(sqlit
2238f 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73  e3_context*,cons
22390 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64  t void*,int,void
22391 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76  (*)(void*));.  v
22392 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 74 65  oid  (*result_te
22393 78 74 31 36 6c 65 29 28 73 71 6c 69 74 65 33 5f  xt16le)(sqlite3_
22394 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 76  context*,const v
22395 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29  oid*,int,void(*)
22396 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69 64  (void*));.  void
22397 20 20 28 2a 72 65 73 75 6c 74 5f 76 61 6c 75 65    (*result_value
22398 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
22399 74 2a 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  t*,sqlite3_value
2239a 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 72  *);.  void * (*r
2239b 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 29 28 73 71  ollback_hook)(sq
2239c 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a 29 28 76  lite3*,void(*)(v
2239d 6f 69 64 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20  oid*),void*);.  
2239e 69 6e 74 20 20 28 2a 73 65 74 5f 61 75 74 68 6f  int  (*set_autho
2239f 72 69 7a 65 72 29 28 73 71 6c 69 74 65 33 2a 2c  rizer)(sqlite3*,
223a0 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74  int(*)(void*,int
223a1 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
223a2 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
223a3 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
223a4 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64  ),void*);.  void
223a5 20 20 28 2a 73 65 74 5f 61 75 78 64 61 74 61 29    (*set_auxdata)
223a6 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
223a7 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 76 6f 69 64  *,int,void*,void
223a8 20 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20   (*)(void*));.  
223a9 63 68 61 72 20 2a 20 28 2a 73 6e 70 72 69 6e 74  char * (*snprint
223aa 66 29 28 69 6e 74 2c 63 68 61 72 2a 2c 63 6f 6e  f)(int,char*,con
223ab 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 20  st char*,...);. 
223ac 20 69 6e 74 20 20 28 2a 73 74 65 70 29 28 73 71   int  (*step)(sq
223ad 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20  lite3_stmt*);.  
223ae 69 6e 74 20 20 28 2a 74 61 62 6c 65 5f 63 6f 6c  int  (*table_col
223af 75 6d 6e 5f 6d 65 74 61 64 61 74 61 29 28 73 71  umn_metadata)(sq
223b0 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
223b1 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  r*,const char*,c
223b2 6f 6e 73 74 20 63 68 61 72 2a 2c 63 68 61 72 20  onst char*,char 
223b3 63 6f 6e 73 74 2a 2a 2c 63 68 61 72 20 63 6f 6e  const**,char con
223b4 73 74 2a 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69  st**,int*,int*,i
223b5 6e 74 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  nt*);.  void  (*
223b6 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 29 28  thread_cleanup)(
223b7 76 6f 69 64 29 3b 0a 20 20 69 6e 74 20 20 28 2a  void);.  int  (*
223b8 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 29 28 73  total_changes)(s
223b9 71 6c 69 74 65 33 2a 29 3b 0a 20 20 76 6f 69 64  qlite3*);.  void
223ba 20 2a 20 28 2a 74 72 61 63 65 29 28 73 71 6c 69   * (*trace)(sqli
223bb 74 65 33 2a 2c 76 6f 69 64 28 2a 78 54 72 61 63  te3*,void(*xTrac
223bc 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63  e)(void*,const c
223bd 68 61 72 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20  har*),void*);.  
223be 69 6e 74 20 20 28 2a 74 72 61 6e 73 66 65 72 5f  int  (*transfer_
223bf 62 69 6e 64 69 6e 67 73 29 28 73 71 6c 69 74 65  bindings)(sqlite
223c0 33 5f 73 74 6d 74 2a 2c 73 71 6c 69 74 65 33 5f  3_stmt*,sqlite3_
223c1 73 74 6d 74 2a 29 3b 0a 20 20 76 6f 69 64 20 2a  stmt*);.  void *
223c2 20 28 2a 75 70 64 61 74 65 5f 68 6f 6f 6b 29 28   (*update_hook)(
223c3 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a 29  sqlite3*,void(*)
223c4 28 76 6f 69 64 2a 2c 69 6e 74 20 2c 63 68 61 72  (void*,int ,char
223c5 20 63 6f 6e 73 74 2a 2c 63 68 61 72 20 63 6f 6e   const*,char con
223c6 73 74 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34  st*,sqlite_int64
223c7 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64  ),void*);.  void
223c8 20 2a 20 28 2a 75 73 65 72 5f 64 61 74 61 29 28   * (*user_data)(
223c9 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
223ca 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  );.  const void 
223cb 2a 20 28 2a 76 61 6c 75 65 5f 62 6c 6f 62 29 28  * (*value_blob)(
223cc 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
223cd 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f  .  int  (*value_
223ce 62 79 74 65 73 29 28 73 71 6c 69 74 65 33 5f 76  bytes)(sqlite3_v
223cf 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28  alue*);.  int  (
223d0 2a 76 61 6c 75 65 5f 62 79 74 65 73 31 36 29 28  *value_bytes16)(
223d1 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
223d2 0a 20 20 64 6f 75 62 6c 65 20 20 28 2a 76 61 6c  .  double  (*val
223d3 75 65 5f 64 6f 75 62 6c 65 29 28 73 71 6c 69 74  ue_double)(sqlit
223d4 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e  e3_value*);.  in
223d5 74 20 20 28 2a 76 61 6c 75 65 5f 69 6e 74 29 28  t  (*value_int)(
223d6 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
223d7 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
223d8 20 28 2a 76 61 6c 75 65 5f 69 6e 74 36 34 29 28   (*value_int64)(
223d9 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
223da 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f  .  int  (*value_
223db 6e 75 6d 65 72 69 63 5f 74 79 70 65 29 28 73 71  numeric_type)(sq
223dc 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20  lite3_value*);. 
223dd 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
223de 63 68 61 72 20 2a 20 28 2a 76 61 6c 75 65 5f 74  char * (*value_t
223df 65 78 74 29 28 73 71 6c 69 74 65 33 5f 76 61 6c  ext)(sqlite3_val
223e0 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  ue*);.  const vo
223e1 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f 74 65 78  id * (*value_tex
223e2 74 31 36 29 28 73 71 6c 69 74 65 33 5f 76 61 6c  t16)(sqlite3_val
223e3 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  ue*);.  const vo
223e4 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f 74 65 78  id * (*value_tex
223e5 74 31 36 62 65 29 28 73 71 6c 69 74 65 33 5f 76  t16be)(sqlite3_v
223e6 61 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74 20  alue*);.  const 
223e7 76 6f 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f 74  void * (*value_t
223e8 65 78 74 31 36 6c 65 29 28 73 71 6c 69 74 65 33  ext16le)(sqlite3
223e9 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20  _value*);.  int 
223ea 20 28 2a 76 61 6c 75 65 5f 74 79 70 65 29 28 73   (*value_type)(s
223eb 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
223ec 20 20 63 68 61 72 20 2a 28 2a 76 6d 70 72 69 6e    char *(*vmprin
223ed 74 66 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  tf)(const char*,
223ee 76 61 5f 6c 69 73 74 29 3b 0a 20 20 2f 2a 20 41  va_list);.  /* A
223ef 64 64 65 64 20 3f 3f 3f 20 2a 2f 0a 20 20 69 6e  dded ??? */.  in
223f0 74 20 28 2a 6f 76 65 72 6c 6f 61 64 5f 66 75 6e  t (*overload_fun
223f1 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 2a 2c  ction)(sqlite3*,
223f2 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75   const char *zFu
223f3 6e 63 4e 61 6d 65 2c 20 69 6e 74 20 6e 41 72 67  ncName, int nArg
223f4 29 3b 0a 20 20 2f 2a 20 41 64 64 65 64 20 62 79  );.  /* Added by
223f5 20 33 2e 33 2e 31 33 20 2a 2f 0a 20 20 69 6e 74   3.3.13 */.  int
223f6 20 28 2a 70 72 65 70 61 72 65 5f 76 32 29 28 73   (*prepare_v2)(s
223f7 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
223f8 61 72 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  ar*,int,sqlite3_
223f9 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 63 68 61  stmt**,const cha
223fa 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 70 72  r**);.  int (*pr
223fb 65 70 61 72 65 31 36 5f 76 32 29 28 73 71 6c 69  epare16_v2)(sqli
223fc 74 65 33 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  te3*,const void*
223fd 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 73 74 6d  ,int,sqlite3_stm
223fe 74 2a 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2a  t**,const void**
223ff 29 3b 0a 20 20 69 6e 74 20 28 2a 63 6c 65 61 72  );.  int (*clear
22400 5f 62 69 6e 64 69 6e 67 73 29 28 73 71 6c 69 74  _bindings)(sqlit
22401 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 2f 2a 20  e3_stmt*);.  /* 
22402 41 64 64 65 64 20 62 79 20 33 2e 34 2e 31 20 2a  Added by 3.4.1 *
22403 2f 0a 20 20 69 6e 74 20 28 2a 63 72 65 61 74 65  /.  int (*create
22404 5f 6d 6f 64 75 6c 65 5f 76 32 29 28 73 71 6c 69  _module_v2)(sqli
22405 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
22406 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d  ,const sqlite3_m
22407 6f 64 75 6c 65 2a 2c 76 6f 69 64 2a 2c 76 6f 69  odule*,void*,voi
22408 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f  d (*xDestroy)(vo
22409 69 64 20 2a 29 29 3b 0a 20 20 2f 2a 20 41 64 64  id *));.  /* Add
2240a 65 64 20 62 79 20 33 2e 35 2e 30 20 2a 2f 0a 20  ed by 3.5.0 */. 
2240b 20 69 6e 74 20 28 2a 62 69 6e 64 5f 7a 65 72 6f   int (*bind_zero
2240c 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f 73 74  blob)(sqlite3_st
2240d 6d 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20  mt*,int,int);.  
2240e 69 6e 74 20 28 2a 62 6c 6f 62 5f 62 79 74 65 73  int (*blob_bytes
2240f 29 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 29  )(sqlite3_blob*)
22410 3b 0a 20 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 63  ;.  int (*blob_c
22411 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 62 6c  lose)(sqlite3_bl
22412 6f 62 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 62 6c  ob*);.  int (*bl
22413 6f 62 5f 6f 70 65 6e 29 28 73 71 6c 69 74 65 33  ob_open)(sqlite3
22414 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  *,const char*,co
22415 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
22416 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 69 6e  char*,sqlite3_in
22417 74 36 34 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  t64,int,sqlite3_
22418 62 6c 6f 62 2a 2a 29 3b 0a 20 20 69 6e 74 20 28  blob**);.  int (
22419 2a 62 6c 6f 62 5f 72 65 61 64 29 28 73 71 6c 69  *blob_read)(sqli
2241a 74 65 33 5f 62 6c 6f 62 2a 2c 76 6f 69 64 2a 2c  te3_blob*,void*,
2241b 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20  int,int);.  int 
2241c 28 2a 62 6c 6f 62 5f 77 72 69 74 65 29 28 73 71  (*blob_write)(sq
2241d 6c 69 74 65 33 5f 62 6c 6f 62 2a 2c 63 6f 6e 73  lite3_blob*,cons
2241e 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 29  t void*,int,int)
2241f 3b 0a 20 20 69 6e 74 20 28 2a 63 72 65 61 74 65  ;.  int (*create
22420 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 29 28 73  _collation_v2)(s
22421 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
22422 61 72 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 69 6e  ar*,int,void*,in
22423 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  t(*)(void*,int,c
22424 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63  onst void*,int,c
22425 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 76 6f 69 64  onst void*),void
22426 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 69  (*)(void*));.  i
22427 6e 74 20 28 2a 66 69 6c 65 5f 63 6f 6e 74 72 6f  nt (*file_contro
22428 6c 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73  l)(sqlite3*,cons
22429 74 20 63 68 61 72 2a 2c 69 6e 74 2c 76 6f 69 64  t char*,int,void
2242a 2a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  *);.  sqlite3_in
2242b 74 36 34 20 28 2a 6d 65 6d 6f 72 79 5f 68 69 67  t64 (*memory_hig
2242c 68 77 61 74 65 72 29 28 69 6e 74 29 3b 0a 20 20  hwater)(int);.  
2242d 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 28 2a  sqlite3_int64 (*
2242e 6d 65 6d 6f 72 79 5f 75 73 65 64 29 28 76 6f 69  memory_used)(voi
2242f 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  d);.  sqlite3_mu
22430 74 65 78 20 2a 28 2a 6d 75 74 65 78 5f 61 6c 6c  tex *(*mutex_all
22431 6f 63 29 28 69 6e 74 29 3b 0a 20 20 76 6f 69 64  oc)(int);.  void
22432 20 28 2a 6d 75 74 65 78 5f 65 6e 74 65 72 29 28   (*mutex_enter)(
22433 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b  sqlite3_mutex*);
22434 0a 20 20 76 6f 69 64 20 28 2a 6d 75 74 65 78 5f  .  void (*mutex_
22435 66 72 65 65 29 28 73 71 6c 69 74 65 33 5f 6d 75  free)(sqlite3_mu
22436 74 65 78 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a  tex*);.  void (*
22437 6d 75 74 65 78 5f 6c 65 61 76 65 29 28 73 71 6c  mutex_leave)(sql
22438 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 20 20  ite3_mutex*);.  
22439 69 6e 74 20 28 2a 6d 75 74 65 78 5f 74 72 79 29  int (*mutex_try)
2243a 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29  (sqlite3_mutex*)
2243b 3b 0a 20 20 69 6e 74 20 28 2a 6f 70 65 6e 5f 76  ;.  int (*open_v
2243c 32 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73  2)(const char*,s
2243d 71 6c 69 74 65 33 2a 2a 2c 69 6e 74 2c 63 6f 6e  qlite3**,int,con
2243e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 69 6e 74  st char*);.  int
2243f 20 28 2a 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72   (*release_memor
22440 79 29 28 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20  y)(int);.  void 
22441 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e  (*result_error_n
22442 6f 6d 65 6d 29 28 73 71 6c 69 74 65 33 5f 63 6f  omem)(sqlite3_co
22443 6e 74 65 78 74 2a 29 3b 0a 20 20 76 6f 69 64 20  ntext*);.  void 
22444 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74  (*result_error_t
22445 6f 6f 62 69 67 29 28 73 71 6c 69 74 65 33 5f 63  oobig)(sqlite3_c
22446 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 69 6e 74 20  ontext*);.  int 
22447 28 2a 73 6c 65 65 70 29 28 69 6e 74 29 3b 0a 20  (*sleep)(int);. 
22448 20 76 6f 69 64 20 28 2a 73 6f 66 74 5f 68 65 61   void (*soft_hea
22449 70 5f 6c 69 6d 69 74 29 28 69 6e 74 29 3b 0a 20  p_limit)(int);. 
2244a 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 28 2a   sqlite3_vfs *(*
2244b 76 66 73 5f 66 69 6e 64 29 28 63 6f 6e 73 74 20  vfs_find)(const 
2244c 63 68 61 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  char*);.  int (*
2244d 76 66 73 5f 72 65 67 69 73 74 65 72 29 28 73 71  vfs_register)(sq
2244e 6c 69 74 65 33 5f 76 66 73 2a 2c 69 6e 74 29 3b  lite3_vfs*,int);
2244f 0a 20 20 69 6e 74 20 28 2a 76 66 73 5f 75 6e 72  .  int (*vfs_unr
22450 65 67 69 73 74 65 72 29 28 73 71 6c 69 74 65 33  egister)(sqlite3
22451 5f 76 66 73 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  _vfs*);.  int (*
22452 78 74 68 72 65 61 64 73 61 66 65 29 28 76 6f 69  xthreadsafe)(voi
22453 64 29 3b 0a 20 20 76 6f 69 64 20 28 2a 72 65 73  d);.  void (*res
22454 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 29 28 73 71  ult_zeroblob)(sq
22455 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
22456 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a 72 65  nt);.  void (*re
22457 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 29  sult_error_code)
22458 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
22459 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a  *,int);.  int (*
2245a 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 29 28 69 6e  test_control)(in
2245b 74 2c 20 2e 2e 2e 29 3b 0a 20 20 76 6f 69 64 20  t, ...);.  void 
2245c 28 2a 72 61 6e 64 6f 6d 6e 65 73 73 29 28 69 6e  (*randomness)(in
2245d 74 2c 76 6f 69 64 2a 29 3b 0a 20 20 73 71 6c 69  t,void*);.  sqli
2245e 74 65 33 20 2a 28 2a 63 6f 6e 74 65 78 74 5f 64  te3 *(*context_d
2245f 62 5f 68 61 6e 64 6c 65 29 28 73 71 6c 69 74 65  b_handle)(sqlite
22460 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 69  3_context*);.  i
22461 6e 74 20 28 2a 65 78 74 65 6e 64 65 64 5f 72 65  nt (*extended_re
22462 73 75 6c 74 5f 63 6f 64 65 73 29 28 73 71 6c 69  sult_codes)(sqli
22463 74 65 33 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74  te3*,int);.  int
22464 20 28 2a 6c 69 6d 69 74 29 28 73 71 6c 69 74 65   (*limit)(sqlite
22465 33 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 73  3*,int,int);.  s
22466 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 28 2a 6e  qlite3_stmt *(*n
22467 65 78 74 5f 73 74 6d 74 29 28 73 71 6c 69 74 65  ext_stmt)(sqlite
22468 33 2a 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  3*,sqlite3_stmt*
22469 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  );.  const char 
2246a 2a 28 2a 73 71 6c 29 28 73 71 6c 69 74 65 33 5f  *(*sql)(sqlite3_
2246b 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  stmt*);.  int (*
2246c 73 74 61 74 75 73 29 28 69 6e 74 2c 69 6e 74 2a  status)(int,int*
2246d 2c 69 6e 74 2a 2c 69 6e 74 29 3b 0a 7d 3b 0a 0a  ,int*,int);.};..
2246e 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
2246f 69 6e 67 20 6d 61 63 72 6f 73 20 72 65 64 65 66  ing macros redef
22470 69 6e 65 20 74 68 65 20 41 50 49 20 72 6f 75 74  ine the API rout
22471 69 6e 65 73 20 73 6f 20 74 68 61 74 20 74 68 65  ines so that the
22472 79 20 61 72 65 0a 2a 2a 20 72 65 64 69 72 65 63  y are.** redirec
22473 74 65 64 20 74 68 72 6f 75 67 68 74 20 74 68 65  ted throught the
22474 20 67 6c 6f 62 61 6c 20 73 71 6c 69 74 65 33 5f   global sqlite3_
22475 61 70 69 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  api structure..*
22476 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72  *.** This header
22477 20 66 69 6c 65 20 69 73 20 61 6c 73 6f 20 75 73   file is also us
22478 65 64 20 62 79 20 74 68 65 20 6c 6f 61 64 65 78  ed by the loadex
22479 74 2e 63 20 73 6f 75 72 63 65 20 66 69 6c 65 0a  t.c source file.
2247a 2a 2a 20 28 70 61 72 74 20 6f 66 20 74 68 65 20  ** (part of the 
2247b 6d 61 69 6e 20 53 51 4c 69 74 65 20 6c 69 62 72  main SQLite libr
2247c 61 72 79 20 2d 20 6e 6f 74 20 61 6e 20 65 78 74  ary - not an ext
2247d 65 6e 73 69 6f 6e 29 20 73 6f 20 74 68 61 74 0a  ension) so that.
2247e 2a 2a 20 69 74 20 63 61 6e 20 67 65 74 20 61 63  ** it can get ac
2247f 63 65 73 73 20 74 6f 20 74 68 65 20 73 71 6c 69  cess to the sqli
22480 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73  te3_api_routines
22481 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 64 65   structure.** de
22482 66 69 6e 69 74 69 6f 6e 2e 20 20 42 75 74 20 74  finition.  But t
22483 68 65 20 6d 61 69 6e 20 6c 69 62 72 61 72 79 20  he main library 
22484 64 6f 65 73 20 6e 6f 74 20 77 61 6e 74 20 74 6f  does not want to
22485 20 72 65 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65   redefine.** the
22486 20 41 50 49 2e 20 20 53 6f 20 74 68 65 20 72 65   API.  So the re
22487 64 65 66 69 6e 69 74 69 6f 6e 20 6d 61 63 72 6f  definition macro
22488 73 20 61 72 65 20 6f 6e 6c 79 20 76 61 6c 69 64  s are only valid
22489 20 69 66 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54   if the.** SQLIT
2248a 45 5f 43 4f 52 45 20 6d 61 63 72 6f 73 20 69 73  E_CORE macros is
2248b 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23   undefined..*/.#
2248c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f  ifndef SQLITE_CO
2248d 52 45 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  RE.#define sqlit
2248e 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
2248f 74 65 78 74 20 20 20 20 20 20 73 71 6c 69 74 65  text      sqlite
22490 33 5f 61 70 69 2d 3e 61 67 67 72 65 67 61 74 65  3_api->aggregate
22491 5f 63 6f 6e 74 65 78 74 0a 23 69 66 6e 64 65 66  _context.#ifndef
22492 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50   SQLITE_OMIT_DEP
22493 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e 65 20  RECATED.#define 
22494 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
22495 65 5f 63 6f 75 6e 74 20 20 20 20 20 20 20 20 73  e_count        s
22496 71 6c 69 74 65 33 5f 61 70 69 2d 3e 61 67 67 72  qlite3_api->aggr
22497 65 67 61 74 65 5f 63 6f 75 6e 74 0a 23 65 6e 64  egate_count.#end
22498 69 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  if.#define sqlit
22499 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 20 20 20  e3_bind_blob    
2249a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2249b 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 62 6c 6f 62  3_api->bind_blob
2249c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
2249d 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 20 20 20 20  _bind_double    
2249e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
2249f 61 70 69 2d 3e 62 69 6e 64 5f 64 6f 75 62 6c 65  api->bind_double
224a0 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
224a1 5f 62 69 6e 64 5f 69 6e 74 20 20 20 20 20 20 20  _bind_int       
224a2 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
224a3 61 70 69 2d 3e 62 69 6e 64 5f 69 6e 74 0a 23 64  api->bind_int.#d
224a4 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
224a5 6e 64 5f 69 6e 74 36 34 20 20 20 20 20 20 20 20  nd_int64        
224a6 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
224a7 2d 3e 62 69 6e 64 5f 69 6e 74 36 34 0a 23 64 65  ->bind_int64.#de
224a8 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e  fine sqlite3_bin
224a9 64 5f 6e 75 6c 6c 20 20 20 20 20 20 20 20 20 20  d_null          
224aa 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
224ab 3e 62 69 6e 64 5f 6e 75 6c 6c 0a 23 64 65 66 69  >bind_null.#defi
224ac 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ne sqlite3_bind_
224ad 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 20  parameter_count 
224ae 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
224af 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f  ind_parameter_co
224b0 75 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  unt.#define sqli
224b1 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
224b2 65 72 5f 69 6e 64 65 78 20 20 20 73 71 6c 69 74  er_index   sqlit
224b3 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 70 61 72  e3_api->bind_par
224b4 61 6d 65 74 65 72 5f 69 6e 64 65 78 0a 23 64 65  ameter_index.#de
224b5 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e  fine sqlite3_bin
224b6 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65  d_parameter_name
224b7 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
224b8 3e 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  >bind_parameter_
224b9 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  name.#define sql
224ba 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 20 20  ite3_bind_text  
224bb 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
224bc 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 74 65  te3_api->bind_te
224bd 78 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  xt.#define sqlit
224be 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 20 20  e3_bind_text16  
224bf 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
224c0 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 74 65 78 74  3_api->bind_text
224c1 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
224c2 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 20 20 20  e3_bind_value   
224c3 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
224c4 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 76 61 6c 75  3_api->bind_valu
224c5 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
224c6 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 20 20  3_busy_handler  
224c7 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
224c8 5f 61 70 69 2d 3e 62 75 73 79 5f 68 61 6e 64 6c  _api->busy_handl
224c9 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  er.#define sqlit
224ca 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 20  e3_busy_timeout 
224cb 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
224cc 33 5f 61 70 69 2d 3e 62 75 73 79 5f 74 69 6d 65  3_api->busy_time
224cd 6f 75 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  out.#define sqli
224ce 74 65 33 5f 63 68 61 6e 67 65 73 20 20 20 20 20  te3_changes     
224cf 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
224d0 65 33 5f 61 70 69 2d 3e 63 68 61 6e 67 65 73 0a  e3_api->changes.
224d1 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
224d2 63 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20 20  close           
224d3 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
224d4 70 69 2d 3e 63 6c 6f 73 65 0a 23 64 65 66 69 6e  pi->close.#defin
224d5 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74  e sqlite3_collat
224d6 69 6f 6e 5f 6e 65 65 64 65 64 20 20 20 20 20 20  ion_needed      
224d7 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
224d8 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 0a 23  llation_needed.#
224d9 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
224da 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31  ollation_needed1
224db 36 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70  6     sqlite3_ap
224dc 69 2d 3e 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  i->collation_nee
224dd 64 65 64 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ded16.#define sq
224de 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
224df 62 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  b            sql
224e0 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
224e1 5f 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73 71  _blob.#define sq
224e2 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
224e3 65 73 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  es           sql
224e4 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
224e5 5f 62 79 74 65 73 0a 23 64 65 66 69 6e 65 20 73  _bytes.#define s
224e6 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
224e7 74 65 73 31 36 20 20 20 20 20 20 20 20 20 73 71  tes16         sq
224e8 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d  lite3_api->colum
224e9 6e 5f 62 79 74 65 73 31 36 0a 23 64 65 66 69 6e  n_bytes16.#defin
224ea 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
224eb 5f 63 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20  _count          
224ec 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
224ed 6c 75 6d 6e 5f 63 6f 75 6e 74 0a 23 64 65 66 69  lumn_count.#defi
224ee 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
224ef 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 20  n_database_name 
224f0 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
224f1 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e  olumn_database_n
224f2 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ame.#define sqli
224f3 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
224f4 61 73 65 5f 6e 61 6d 65 31 36 20 73 71 6c 69 74  ase_name16 sqlit
224f5 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64  e3_api->column_d
224f6 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 0a 23  atabase_name16.#
224f7 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
224f8 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 20 20  olumn_decltype  
224f9 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
224fa 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79  i->column_declty
224fb 70 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  pe.#define sqlit
224fc 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79  e3_column_declty
224fd 70 65 31 36 20 20 20 20 20 20 73 71 6c 69 74 65  pe16      sqlite
224fe 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 65  3_api->column_de
224ff 63 6c 74 79 70 65 31 36 0a 23 64 65 66 69 6e 65  cltype16.#define
22500 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
22501 64 6f 75 62 6c 65 20 20 20 20 20 20 20 20 20 20  double          
22502 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c  sqlite3_api->col
22503 75 6d 6e 5f 64 6f 75 62 6c 65 0a 23 64 65 66 69  umn_double.#defi
22504 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
22505 6e 5f 69 6e 74 20 20 20 20 20 20 20 20 20 20 20  n_int           
22506 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
22507 6f 6c 75 6d 6e 5f 69 6e 74 0a 23 64 65 66 69 6e  olumn_int.#defin
22508 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
22509 5f 69 6e 74 36 34 20 20 20 20 20 20 20 20 20 20  _int64          
2250a 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
2250b 6c 75 6d 6e 5f 69 6e 74 36 34 0a 23 64 65 66 69  lumn_int64.#defi
2250c 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
2250d 6e 5f 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20  n_name          
2250e 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
2250f 6f 6c 75 6d 6e 5f 6e 61 6d 65 0a 23 64 65 66 69  olumn_name.#defi
22510 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
22511 6e 5f 6e 61 6d 65 31 36 20 20 20 20 20 20 20 20  n_name16        
22512 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
22513 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 0a 23 64 65  olumn_name16.#de
22514 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
22515 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 20  umn_origin_name 
22516 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
22517 3e 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  >column_origin_n
22518 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ame.#define sqli
22519 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69  te3_column_origi
2251a 6e 5f 6e 61 6d 65 31 36 20 20 20 73 71 6c 69 74  n_name16   sqlit
2251b 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 6f  e3_api->column_o
2251c 72 69 67 69 6e 5f 6e 61 6d 65 31 36 0a 23 64 65  rigin_name16.#de
2251d 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
2251e 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 20 20  umn_table_name  
2251f 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
22520 3e 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  >column_table_na
22521 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  me.#define sqlit
22522 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f  e3_column_table_
22523 6e 61 6d 65 31 36 20 20 20 20 73 71 6c 69 74 65  name16    sqlite
22524 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74 61  3_api->column_ta
22525 62 6c 65 5f 6e 61 6d 65 31 36 0a 23 64 65 66 69  ble_name16.#defi
22526 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
22527 6e 5f 74 65 78 74 20 20 20 20 20 20 20 20 20 20  n_text          
22528 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
22529 6f 6c 75 6d 6e 5f 74 65 78 74 0a 23 64 65 66 69  olumn_text.#defi
2252a 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
2252b 6e 5f 74 65 78 74 31 36 20 20 20 20 20 20 20 20  n_text16        
2252c 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
2252d 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 0a 23 64 65  olumn_text16.#de
2252e 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
2252f 75 6d 6e 5f 74 79 70 65 20 20 20 20 20 20 20 20  umn_type        
22530 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
22531 3e 63 6f 6c 75 6d 6e 5f 74 79 70 65 0a 23 64 65  >column_type.#de
22532 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
22533 75 6d 6e 5f 76 61 6c 75 65 20 20 20 20 20 20 20  umn_value       
22534 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
22535 3e 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 0a 23 64  >column_value.#d
22536 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
22537 6d 6d 69 74 5f 68 6f 6f 6b 20 20 20 20 20 20 20  mmit_hook       
22538 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
22539 2d 3e 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 0a 23 64  ->commit_hook.#d
2253a 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
2253b 6d 70 6c 65 74 65 20 20 20 20 20 20 20 20 20 20  mplete          
2253c 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
2253d 2d 3e 63 6f 6d 70 6c 65 74 65 0a 23 64 65 66 69  ->complete.#defi
2253e 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  ne sqlite3_compl
2253f 65 74 65 31 36 20 20 20 20 20 20 20 20 20 20 20  ete16           
22540 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
22541 6f 6d 70 6c 65 74 65 31 36 0a 23 64 65 66 69 6e  omplete16.#defin
22542 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
22543 5f 63 6f 6c 6c 61 74 69 6f 6e 20 20 20 20 20 20  _collation      
22544 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72   sqlite3_api->cr
22545 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 0a 23  eate_collation.#
22546 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
22547 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31  reate_collation1
22548 36 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70  6     sqlite3_ap
22549 69 2d 3e 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  i->create_collat
2254a 69 6f 6e 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ion16.#define sq
2254b 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
2254c 63 74 69 6f 6e 20 20 20 20 20 20 20 20 73 71 6c  ction        sql
2254d 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61 74 65  ite3_api->create
2254e 5f 66 75 6e 63 74 69 6f 6e 0a 23 64 65 66 69 6e  _function.#defin
2254f 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
22550 5f 66 75 6e 63 74 69 6f 6e 31 36 20 20 20 20 20  _function16     
22551 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72   sqlite3_api->cr
22552 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 0a  eate_function16.
22553 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
22554 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 20 20 20  create_module   
22555 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
22556 70 69 2d 3e 63 72 65 61 74 65 5f 6d 6f 64 75 6c  pi->create_modul
22557 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
22558 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f  3_create_module_
22559 76 32 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  v2       sqlite3
2255a 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 6d 6f 64  _api->create_mod
2255b 75 6c 65 5f 76 32 0a 23 64 65 66 69 6e 65 20 73  ule_v2.#define s
2255c 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e  qlite3_data_coun
2255d 74 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  t             sq
2255e 6c 69 74 65 33 5f 61 70 69 2d 3e 64 61 74 61 5f  lite3_api->data_
2255f 63 6f 75 6e 74 0a 23 64 65 66 69 6e 65 20 73 71  count.#define sq
22560 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 20  lite3_db_handle 
22561 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
22562 69 74 65 33 5f 61 70 69 2d 3e 64 62 5f 68 61 6e  ite3_api->db_han
22563 64 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  dle.#define sqli
22564 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62  te3_declare_vtab
22565 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
22566 65 33 5f 61 70 69 2d 3e 64 65 63 6c 61 72 65 5f  e3_api->declare_
22567 76 74 61 62 0a 23 64 65 66 69 6e 65 20 73 71 6c  vtab.#define sql
22568 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72  ite3_enable_shar
22569 65 64 5f 63 61 63 68 65 20 20 20 20 73 71 6c 69  ed_cache    sqli
2256a 74 65 33 5f 61 70 69 2d 3e 65 6e 61 62 6c 65 5f  te3_api->enable_
2256b 73 68 61 72 65 64 5f 63 61 63 68 65 0a 23 64 65  shared_cache.#de
2256c 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 72 72  fine sqlite3_err
2256d 63 6f 64 65 20 20 20 20 20 20 20 20 20 20 20 20  code            
2256e 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
2256f 3e 65 72 72 63 6f 64 65 0a 23 64 65 66 69 6e 65  >errcode.#define
22570 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 20   sqlite3_errmsg 
22571 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22572 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 72 72  sqlite3_api->err
22573 6d 73 67 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  msg.#define sqli
22574 74 65 33 5f 65 72 72 6d 73 67 31 36 20 20 20 20  te3_errmsg16    
22575 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
22576 65 33 5f 61 70 69 2d 3e 65 72 72 6d 73 67 31 36  e3_api->errmsg16
22577 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
22578 5f 65 78 65 63 20 20 20 20 20 20 20 20 20 20 20  _exec           
22579 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
2257a 61 70 69 2d 3e 65 78 65 63 0a 23 69 66 6e 64 65  api->exec.#ifnde
2257b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  f SQLITE_OMIT_DE
2257c 50 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e 65  PRECATED.#define
2257d 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65 64   sqlite3_expired
2257e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2257f 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 78 70  sqlite3_api->exp
22580 69 72 65 64 0a 23 65 6e 64 69 66 0a 23 64 65 66  ired.#endif.#def
22581 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  ine sqlite3_fina
22582 6c 69 7a 65 20 20 20 20 20 20 20 20 20 20 20 20  lize            
22583 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
22584 66 69 6e 61 6c 69 7a 65 0a 23 64 65 66 69 6e 65  finalize.#define
22585 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20 20 20   sqlite3_free   
22586 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22587 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 66 72 65  sqlite3_api->fre
22588 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
22589 33 5f 66 72 65 65 5f 74 61 62 6c 65 20 20 20 20  3_free_table    
2258a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2258b 5f 61 70 69 2d 3e 66 72 65 65 5f 74 61 62 6c 65  _api->free_table
2258c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
2258d 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 20  _get_autocommit 
2258e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
2258f 61 70 69 2d 3e 67 65 74 5f 61 75 74 6f 63 6f 6d  api->get_autocom
22590 6d 69 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  mit.#define sqli
22591 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 20  te3_get_auxdata 
22592 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
22593 65 33 5f 61 70 69 2d 3e 67 65 74 5f 61 75 78 64  e3_api->get_auxd
22594 61 74 61 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ata.#define sqli
22595 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 20 20 20  te3_get_table   
22596 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
22597 65 33 5f 61 70 69 2d 3e 67 65 74 5f 74 61 62 6c  e3_api->get_tabl
22598 65 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  e.#ifndef SQLITE
22599 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
2259a 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
2259b 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 20  _global_recover 
2259c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
2259d 61 70 69 2d 3e 67 6c 6f 62 61 6c 5f 72 65 63 6f  api->global_reco
2259e 76 65 72 0a 23 65 6e 64 69 66 0a 23 64 65 66 69  ver.#endif.#defi
2259f 6e 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  ne sqlite3_inter
225a0 72 75 70 74 20 20 20 20 20 20 20 20 20 20 20 20  rupt            
225a1 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 69    sqlite3_api->i
225a2 6e 74 65 72 72 75 70 74 78 0a 23 64 65 66 69 6e  nterruptx.#defin
225a3 65 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69  e sqlite3_last_i
225a4 6e 73 65 72 74 5f 72 6f 77 69 64 20 20 20 20 20  nsert_rowid     
225a5 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 61   sqlite3_api->la
225a6 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 0a  st_insert_rowid.
225a7 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
225a8 6c 69 62 76 65 72 73 69 6f 6e 20 20 20 20 20 20  libversion      
225a9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
225aa 70 69 2d 3e 6c 69 62 76 65 72 73 69 6f 6e 0a 23  pi->libversion.#
225ab 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c  define sqlite3_l
225ac 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72  ibversion_number
225ad 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
225ae 69 2d 3e 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75  i->libversion_nu
225af 6d 62 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c  mber.#define sql
225b0 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 20 20 20 20  ite3_malloc     
225b1 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
225b2 74 65 33 5f 61 70 69 2d 3e 6d 61 6c 6c 6f 63 0a  te3_api->malloc.
225b3 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
225b4 6d 70 72 69 6e 74 66 20 20 20 20 20 20 20 20 20  mprintf         
225b5 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
225b6 70 69 2d 3e 6d 70 72 69 6e 74 66 0a 23 64 65 66  pi->mprintf.#def
225b7 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  ine sqlite3_open
225b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
225b9 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
225ba 6f 70 65 6e 0a 23 64 65 66 69 6e 65 20 73 71 6c  open.#define sql
225bb 69 74 65 33 5f 6f 70 65 6e 31 36 20 20 20 20 20  ite3_open16     
225bc 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
225bd 74 65 33 5f 61 70 69 2d 3e 6f 70 65 6e 31 36 0a  te3_api->open16.
225be 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
225bf 70 72 65 70 61 72 65 20 20 20 20 20 20 20 20 20  prepare         
225c0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
225c1 70 69 2d 3e 70 72 65 70 61 72 65 0a 23 64 65 66  pi->prepare.#def
225c2 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70  ine sqlite3_prep
225c3 61 72 65 31 36 20 20 20 20 20 20 20 20 20 20 20  are16           
225c4 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
225c5 70 72 65 70 61 72 65 31 36 0a 23 64 65 66 69 6e  prepare16.#defin
225c6 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  e sqlite3_prepar
225c7 65 5f 76 32 20 20 20 20 20 20 20 20 20 20 20 20  e_v2            
225c8 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72   sqlite3_api->pr
225c9 65 70 61 72 65 5f 76 32 0a 23 64 65 66 69 6e 65  epare_v2.#define
225ca 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
225cb 31 36 5f 76 32 20 20 20 20 20 20 20 20 20 20 20  16_v2           
225cc 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65  sqlite3_api->pre
225cd 70 61 72 65 31 36 5f 76 32 0a 23 64 65 66 69 6e  pare16_v2.#defin
225ce 65 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c  e sqlite3_profil
225cf 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
225d0 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72   sqlite3_api->pr
225d1 6f 66 69 6c 65 0a 23 64 65 66 69 6e 65 20 73 71  ofile.#define sq
225d2 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68  lite3_progress_h
225d3 61 6e 64 6c 65 72 20 20 20 20 20 20 20 73 71 6c  andler       sql
225d4 69 74 65 33 5f 61 70 69 2d 3e 70 72 6f 67 72 65  ite3_api->progre
225d5 73 73 5f 68 61 6e 64 6c 65 72 0a 23 64 65 66 69  ss_handler.#defi
225d6 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  ne sqlite3_reall
225d7 6f 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20  oc              
225d8 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
225d9 65 61 6c 6c 6f 63 0a 23 64 65 66 69 6e 65 20 73  ealloc.#define s
225da 71 6c 69 74 65 33 5f 72 65 73 65 74 20 20 20 20  qlite3_reset    
225db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
225dc 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 65 74  lite3_api->reset
225dd 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
225de 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 20 20 20  _result_blob    
225df 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
225e0 61 70 69 2d 3e 72 65 73 75 6c 74 5f 62 6c 6f 62  api->result_blob
225e1 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
225e2 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 20 20  _result_double  
225e3 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
225e4 61 70 69 2d 3e 72 65 73 75 6c 74 5f 64 6f 75 62  api->result_doub
225e5 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  le.#define sqlit
225e6 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 20  e3_result_error 
225e7 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
225e8 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72  3_api->result_er
225e9 72 6f 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ror.#define sqli
225ea 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
225eb 31 36 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  16         sqlit
225ec 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65  e3_api->result_e
225ed 72 72 6f 72 31 36 0a 23 64 65 66 69 6e 65 20 73  rror16.#define s
225ee 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
225ef 74 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  t             sq
225f0 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
225f1 74 5f 69 6e 74 0a 23 64 65 66 69 6e 65 20 73 71  t_int.#define sq
225f2 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
225f3 36 34 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  64           sql
225f4 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74  ite3_api->result
225f5 5f 69 6e 74 36 34 0a 23 64 65 66 69 6e 65 20 73  _int64.#define s
225f6 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
225f7 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ll            sq
225f8 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
225f9 74 5f 6e 75 6c 6c 0a 23 64 65 66 69 6e 65 20 73  t_null.#define s
225fa 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
225fb 78 74 20 20 20 20 20 20 20 20 20 20 20 20 73 71  xt            sq
225fc 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
225fd 74 5f 74 65 78 74 0a 23 64 65 66 69 6e 65 20 73  t_text.#define s
225fe 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
225ff 78 74 31 36 20 20 20 20 20 20 20 20 20 20 73 71  xt16          sq
22600 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
22601 74 5f 74 65 78 74 31 36 0a 23 64 65 66 69 6e 65  t_text16.#define
22602 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
22603 74 65 78 74 31 36 62 65 20 20 20 20 20 20 20 20  text16be        
22604 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73  sqlite3_api->res
22605 75 6c 74 5f 74 65 78 74 31 36 62 65 0a 23 64 65  ult_text16be.#de
22606 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73  fine sqlite3_res
22607 75 6c 74 5f 74 65 78 74 31 36 6c 65 20 20 20 20  ult_text16le    
22608 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
22609 3e 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65  >result_text16le
2260a 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
2260b 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 20 20 20  _result_value   
2260c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
2260d 61 70 69 2d 3e 72 65 73 75 6c 74 5f 76 61 6c 75  api->result_valu
2260e 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
2260f 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 20  3_rollback_hook 
22610 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
22611 5f 61 70 69 2d 3e 72 6f 6c 6c 62 61 63 6b 5f 68  _api->rollback_h
22612 6f 6f 6b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ook.#define sqli
22613 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a  te3_set_authoriz
22614 65 72 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  er         sqlit
22615 65 33 5f 61 70 69 2d 3e 73 65 74 5f 61 75 74 68  e3_api->set_auth
22616 6f 72 69 7a 65 72 0a 23 64 65 66 69 6e 65 20 73  orizer.#define s
22617 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61  qlite3_set_auxda
22618 74 61 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ta            sq
22619 6c 69 74 65 33 5f 61 70 69 2d 3e 73 65 74 5f 61  lite3_api->set_a
2261a 75 78 64 61 74 61 0a 23 64 65 66 69 6e 65 20 73  uxdata.#define s
2261b 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 20  qlite3_snprintf 
2261c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
2261d 6c 69 74 65 33 5f 61 70 69 2d 3e 73 6e 70 72 69  lite3_api->snpri
2261e 6e 74 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ntf.#define sqli
2261f 74 65 33 5f 73 74 65 70 20 20 20 20 20 20 20 20  te3_step        
22620 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
22621 65 33 5f 61 70 69 2d 3e 73 74 65 70 0a 23 64 65  e3_api->step.#de
22622 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 61 62  fine sqlite3_tab
22623 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61  le_column_metada
22624 74 61 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d  ta  sqlite3_api-
22625 3e 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65  >table_column_me
22626 74 61 64 61 74 61 0a 23 64 65 66 69 6e 65 20 73  tadata.#define s
22627 71 6c 69 74 65 33 5f 74 68 72 65 61 64 5f 63 6c  qlite3_thread_cl
22628 65 61 6e 75 70 20 20 20 20 20 20 20 20 20 73 71  eanup         sq
22629 6c 69 74 65 33 5f 61 70 69 2d 3e 74 68 72 65 61  lite3_api->threa
2262a 64 5f 63 6c 65 61 6e 75 70 0a 23 64 65 66 69 6e  d_cleanup.#defin
2262b 65 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f  e sqlite3_total_
2262c 63 68 61 6e 67 65 73 20 20 20 20 20 20 20 20 20  changes         
2262d 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 6f   sqlite3_api->to
2262e 74 61 6c 5f 63 68 61 6e 67 65 73 0a 23 64 65 66  tal_changes.#def
2262f 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 72 61 63  ine sqlite3_trac
22630 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
22631 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
22632 74 72 61 63 65 0a 23 69 66 6e 64 65 66 20 53 51  trace.#ifndef SQ
22633 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43  LITE_OMIT_DEPREC
22634 41 54 45 44 0a 23 64 65 66 69 6e 65 20 73 71 6c  ATED.#define sql
22635 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69  ite3_transfer_bi
22636 6e 64 69 6e 67 73 20 20 20 20 20 20 73 71 6c 69  ndings      sqli
22637 74 65 33 5f 61 70 69 2d 3e 74 72 61 6e 73 66 65  te3_api->transfe
22638 72 5f 62 69 6e 64 69 6e 67 73 0a 23 65 6e 64 69  r_bindings.#endi
22639 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  f.#define sqlite
2263a 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 20 20 20  3_update_hook   
2263b 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2263c 5f 61 70 69 2d 3e 75 70 64 61 74 65 5f 68 6f 6f  _api->update_hoo
2263d 6b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  k.#define sqlite
2263e 33 5f 75 73 65 72 5f 64 61 74 61 20 20 20 20 20  3_user_data     
2263f 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
22640 5f 61 70 69 2d 3e 75 73 65 72 5f 64 61 74 61 0a  _api->user_data.
22641 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
22642 76 61 6c 75 65 5f 62 6c 6f 62 20 20 20 20 20 20  value_blob      
22643 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
22644 70 69 2d 3e 76 61 6c 75 65 5f 62 6c 6f 62 0a 23  pi->value_blob.#
22645 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
22646 61 6c 75 65 5f 62 79 74 65 73 20 20 20 20 20 20  alue_bytes      
22647 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
22648 69 2d 3e 76 61 6c 75 65 5f 62 79 74 65 73 0a 23  i->value_bytes.#
22649 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
2264a 61 6c 75 65 5f 62 79 74 65 73 31 36 20 20 20 20  alue_bytes16    
2264b 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
2264c 69 2d 3e 76 61 6c 75 65 5f 62 79 74 65 73 31 36  i->value_bytes16
2264d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
2264e 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 20 20  _value_double   
2264f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
22650 61 70 69 2d 3e 76 61 6c 75 65 5f 64 6f 75 62 6c  api->value_doubl
22651 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
22652 33 5f 76 61 6c 75 65 5f 69 6e 74 20 20 20 20 20  3_value_int     
22653 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
22654 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 69 6e 74 0a  _api->value_int.
22655 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
22656 76 61 6c 75 65 5f 69 6e 74 36 34 20 20 20 20 20  value_int64     
22657 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
22658 70 69 2d 3e 76 61 6c 75 65 5f 69 6e 74 36 34 0a  pi->value_int64.
22659 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
2265a 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79  value_numeric_ty
2265b 70 65 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  pe     sqlite3_a
2265c 70 69 2d 3e 76 61 6c 75 65 5f 6e 75 6d 65 72 69  pi->value_numeri
2265d 63 5f 74 79 70 65 0a 23 64 65 66 69 6e 65 20 73  c_type.#define s
2265e 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
2265f 74 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  t             sq
22660 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65  lite3_api->value
22661 5f 74 65 78 74 0a 23 64 65 66 69 6e 65 20 73 71  _text.#define sq
22662 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
22663 31 36 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  16           sql
22664 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f  ite3_api->value_
22665 74 65 78 74 31 36 0a 23 64 65 66 69 6e 65 20 73  text16.#define s
22666 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
22667 74 31 36 62 65 20 20 20 20 20 20 20 20 20 73 71  t16be         sq
22668 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65  lite3_api->value
22669 5f 74 65 78 74 31 36 62 65 0a 23 64 65 66 69 6e  _text16be.#defin
2266a 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  e sqlite3_value_
2266b 74 65 78 74 31 36 6c 65 20 20 20 20 20 20 20 20  text16le        
2266c 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61   sqlite3_api->va
2266d 6c 75 65 5f 74 65 78 74 31 36 6c 65 0a 23 64 65  lue_text16le.#de
2266e 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  fine sqlite3_val
2266f 75 65 5f 74 79 70 65 20 20 20 20 20 20 20 20 20  ue_type         
22670 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
22671 3e 76 61 6c 75 65 5f 74 79 70 65 0a 23 64 65 66  >value_type.#def
22672 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 6d 70 72  ine sqlite3_vmpr
22673 69 6e 74 66 20 20 20 20 20 20 20 20 20 20 20 20  intf            
22674 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
22675 76 6d 70 72 69 6e 74 66 0a 23 64 65 66 69 6e 65  vmprintf.#define
22676 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61   sqlite3_overloa
22677 64 5f 66 75 6e 63 74 69 6f 6e 20 20 20 20 20 20  d_function      
22678 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 76 65  sqlite3_api->ove
22679 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 0a 23  rload_function.#
2267a 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70  define sqlite3_p
2267b 72 65 70 61 72 65 5f 76 32 20 20 20 20 20 20 20  repare_v2       
2267c 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
2267d 69 2d 3e 70 72 65 70 61 72 65 5f 76 32 0a 23 64  i->prepare_v2.#d
2267e 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72  efine sqlite3_pr
2267f 65 70 61 72 65 31 36 5f 76 32 20 20 20 20 20 20  epare16_v2      
22680 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
22681 2d 3e 70 72 65 70 61 72 65 31 36 5f 76 32 0a 23  ->prepare16_v2.#
22682 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
22683 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 20 20 20  lear_bindings   
22684 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
22685 69 2d 3e 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67  i->clear_binding
22686 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  s.#define sqlite
22687 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 20  3_bind_zeroblob 
22688 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
22689 5f 61 70 69 2d 3e 62 69 6e 64 5f 7a 65 72 6f 62  _api->bind_zerob
2268a 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  lob.#define sqli
2268b 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 20 20  te3_blob_bytes  
2268c 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2268d 65 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f 62 79 74  e3_api->blob_byt
2268e 65 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  es.#define sqlit
2268f 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 20 20 20  e3_blob_close   
22690 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
22691 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f 63 6c 6f 73  3_api->blob_clos
22692 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
22693 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20 20 20 20 20  3_blob_open     
22694 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
22695 5f 61 70 69 2d 3e 62 6c 6f 62 5f 6f 70 65 6e 0a  _api->blob_open.
22696 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
22697 62 6c 6f 62 5f 72 65 61 64 20 20 20 20 20 20 20  blob_read       
22698 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
22699 70 69 2d 3e 62 6c 6f 62 5f 72 65 61 64 0a 23 64  pi->blob_read.#d
2269a 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c  efine sqlite3_bl
2269b 6f 62 5f 77 72 69 74 65 20 20 20 20 20 20 20 20  ob_write        
2269c 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
2269d 2d 3e 62 6c 6f 62 5f 77 72 69 74 65 0a 23 64 65  ->blob_write.#de
2269e 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65  fine sqlite3_cre
2269f 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32  ate_collation_v2
226a0 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
226a1 3e 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  >create_collatio
226a2 6e 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c  n_v2.#define sql
226a3 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f  ite3_file_contro
226a4 6c 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  l           sqli
226a5 74 65 33 5f 61 70 69 2d 3e 66 69 6c 65 5f 63 6f  te3_api->file_co
226a6 6e 74 72 6f 6c 0a 23 64 65 66 69 6e 65 20 73 71  ntrol.#define sq
226a7 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67  lite3_memory_hig
226a8 68 77 61 74 65 72 20 20 20 20 20 20 20 73 71 6c  hwater       sql
226a9 69 74 65 33 5f 61 70 69 2d 3e 6d 65 6d 6f 72 79  ite3_api->memory
226aa 5f 68 69 67 68 77 61 74 65 72 0a 23 64 65 66 69  _highwater.#defi
226ab 6e 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72  ne sqlite3_memor
226ac 79 5f 75 73 65 64 20 20 20 20 20 20 20 20 20 20  y_used          
226ad 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d    sqlite3_api->m
226ae 65 6d 6f 72 79 5f 75 73 65 64 0a 23 64 65 66 69  emory_used.#defi
226af 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
226b0 5f 61 6c 6c 6f 63 20 20 20 20 20 20 20 20 20 20  _alloc          
226b1 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d    sqlite3_api->m
226b2 75 74 65 78 5f 61 6c 6c 6f 63 0a 23 64 65 66 69  utex_alloc.#defi
226b3 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
226b4 5f 65 6e 74 65 72 20 20 20 20 20 20 20 20 20 20  _enter          
226b5 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d    sqlite3_api->m
226b6 75 74 65 78 5f 65 6e 74 65 72 0a 23 64 65 66 69  utex_enter.#defi
226b7 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
226b8 5f 66 72 65 65 20 20 20 20 20 20 20 20 20 20 20  _free           
226b9 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d    sqlite3_api->m
226ba 75 74 65 78 5f 66 72 65 65 0a 23 64 65 66 69 6e  utex_free.#defin
226bb 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
226bc 6c 65 61 76 65 20 20 20 20 20 20 20 20 20 20 20  leave           
226bd 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75   sqlite3_api->mu
226be 74 65 78 5f 6c 65 61 76 65 0a 23 64 65 66 69 6e  tex_leave.#defin
226bf 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
226c0 74 72 79 20 20 20 20 20 20 20 20 20 20 20 20 20  try             
226c1 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75   sqlite3_api->mu
226c2 74 65 78 5f 74 72 79 0a 23 64 65 66 69 6e 65 20  tex_try.#define 
226c3 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 20  sqlite3_open_v2 
226c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
226c5 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 70 65 6e  qlite3_api->open
226c6 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  _v2.#define sqli
226c7 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f  te3_release_memo
226c8 72 79 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  ry         sqlit
226c9 65 33 5f 61 70 69 2d 3e 72 65 6c 65 61 73 65 5f  e3_api->release_
226ca 6d 65 6d 6f 72 79 0a 23 64 65 66 69 6e 65 20 73  memory.#define s
226cb 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
226cc 72 6f 72 5f 6e 6f 6d 65 6d 20 20 20 20 20 73 71  ror_nomem     sq
226cd 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
226ce 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 0a 23 64  t_error_nomem.#d
226cf 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
226d0 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69  sult_error_toobi
226d1 67 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69  g    sqlite3_api
226d2 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74  ->result_error_t
226d3 6f 6f 62 69 67 0a 23 64 65 66 69 6e 65 20 73 71  oobig.#define sq
226d4 6c 69 74 65 33 5f 73 6c 65 65 70 20 20 20 20 20  lite3_sleep     
226d5 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
226d6 69 74 65 33 5f 61 70 69 2d 3e 73 6c 65 65 70 0a  ite3_api->sleep.
226d7 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
226d8 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 20  soft_heap_limit 
226d9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
226da 70 69 2d 3e 73 6f 66 74 5f 68 65 61 70 5f 6c 69  pi->soft_heap_li
226db 6d 69 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  mit.#define sqli
226dc 74 65 33 5f 76 66 73 5f 66 69 6e 64 20 20 20 20  te3_vfs_find    
226dd 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
226de 65 33 5f 61 70 69 2d 3e 76 66 73 5f 66 69 6e 64  e3_api->vfs_find
226df 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
226e0 5f 76 66 73 5f 72 65 67 69 73 74 65 72 20 20 20  _vfs_register   
226e1 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
226e2 61 70 69 2d 3e 76 66 73 5f 72 65 67 69 73 74 65  api->vfs_registe
226e3 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  r.#define sqlite
226e4 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72  3_vfs_unregister
226e5 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
226e6 5f 61 70 69 2d 3e 76 66 73 5f 75 6e 72 65 67 69  _api->vfs_unregi
226e7 73 74 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c  ster.#define sql
226e8 69 74 65 33 5f 74 68 72 65 61 64 73 61 66 65 20  ite3_threadsafe 
226e9 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
226ea 74 65 33 5f 61 70 69 2d 3e 78 74 68 72 65 61 64  te3_api->xthread
226eb 73 61 66 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  safe.#define sql
226ec 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f  ite3_result_zero
226ed 62 6c 6f 62 20 20 20 20 20 20 20 20 73 71 6c 69  blob        sqli
226ee 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f  te3_api->result_
226ef 7a 65 72 6f 62 6c 6f 62 0a 23 64 65 66 69 6e 65  zeroblob.#define
226f0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
226f1 65 72 72 6f 72 5f 63 6f 64 65 20 20 20 20 20 20  error_code      
226f2 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73  sqlite3_api->res
226f3 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 0a 23  ult_error_code.#
226f4 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74  define sqlite3_t
226f5 65 73 74 5f 63 6f 6e 74 72 6f 6c 20 20 20 20 20  est_control     
226f6 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
226f7 69 2d 3e 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 0a  i->test_control.
226f8 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
226f9 72 61 6e 64 6f 6d 6e 65 73 73 20 20 20 20 20 20  randomness      
226fa 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
226fb 70 69 2d 3e 72 61 6e 64 6f 6d 6e 65 73 73 0a 23  pi->randomness.#
226fc 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
226fd 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
226fe 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
226ff 69 2d 3e 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  i->context_db_ha
22700 6e 64 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  ndle.#define sql
22701 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65  ite3_extended_re
22702 73 75 6c 74 5f 63 6f 64 65 73 20 20 73 71 6c 69  sult_codes  sqli
22703 74 65 33 5f 61 70 69 2d 3e 65 78 74 65 6e 64 65  te3_api->extende
22704 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 0a 23  d_result_codes.#
22705 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c  define sqlite3_l
22706 69 6d 69 74 20 20 20 20 20 20 20 20 20 20 20 20  imit            
22707 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
22708 69 2d 3e 6c 69 6d 69 74 0a 23 64 65 66 69 6e 65  i->limit.#define
22709 20 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74   sqlite3_next_st
2270a 6d 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  mt              
2270b 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6e 65 78  sqlite3_api->nex
2270c 74 5f 73 74 6d 74 0a 23 64 65 66 69 6e 65 20 73  t_stmt.#define s
2270d 71 6c 69 74 65 33 5f 73 71 6c 20 20 20 20 20 20  qlite3_sql      
2270e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
2270f 6c 69 74 65 33 5f 61 70 69 2d 3e 73 71 6c 0a 23  lite3_api->sql.#
22710 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73  define sqlite3_s
22711 74 61 74 75 73 20 20 20 20 20 20 20 20 20 20 20  tatus           
22712 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
22713 69 2d 3e 73 74 61 74 75 73 0a 23 65 6e 64 69 66  i->status.#endif
22714 20 2f 2a 20 53 51 4c 49 54 45 5f 43 4f 52 45 20   /* SQLITE_CORE 
22715 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  */..#define SQLI
22716 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49  TE_EXTENSION_INI
22717 54 31 20 20 20 20 20 63 6f 6e 73 74 20 73 71 6c  T1     const sql
22718 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65  ite3_api_routine
22719 73 20 2a 73 71 6c 69 74 65 33 5f 61 70 69 20 3d  s *sqlite3_api =
2271a 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   0;.#define SQLI
2271b 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49  TE_EXTENSION_INI
2271c 54 32 28 76 29 20 20 73 71 6c 69 74 65 33 5f 61  T2(v)  sqlite3_a
2271d 70 69 20 3d 20 76 3b 0a 0a 23 65 6e 64 69 66 20  pi = v;..#endif 
2271e 2f 2a 20 5f 53 51 4c 49 54 45 33 45 58 54 5f 48  /* _SQLITE3EXT_H
2271f 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  _ */../*********
22720 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c  ***** End of sql
22721 69 74 65 33 65 78 74 2e 68 20 2a 2a 2a 2a 2a 2a  ite3ext.h ******
22722 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22723 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22724 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
22725 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
22726 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
22727 66 66 20 69 6e 20 6c 6f 61 64 65 78 74 2e 63 20  ff in loadext.c 
22728 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22729 2a 2a 2a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  ****/..#ifndef S
2272a 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f  QLITE_OMIT_LOAD_
2272b 45 58 54 45 4e 53 49 4f 4e 0a 0a 2f 2a 0a 2a 2a  EXTENSION../*.**
2272c 20 53 6f 6d 65 20 41 50 49 20 72 6f 75 74 69 6e   Some API routin
2272d 65 73 20 61 72 65 20 6f 6d 69 74 74 65 64 20 77  es are omitted w
2272e 68 65 6e 20 76 61 72 69 6f 75 73 20 66 65 61 74  hen various feat
2272f 75 72 65 73 20 61 72 65 0a 2a 2a 20 65 78 63 6c  ures are.** excl
22730 75 64 65 64 20 66 72 6f 6d 20 61 20 62 75 69 6c  uded from a buil
22731 64 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 53 75  d of SQLite.  Su
22732 62 73 74 69 74 75 74 65 20 61 20 4e 55 4c 4c 20  bstitute a NULL 
22733 70 6f 69 6e 74 65 72 0a 2a 2a 20 66 6f 72 20 61  pointer.** for a
22734 6e 79 20 6d 69 73 73 69 6e 67 20 41 50 49 73 2e  ny missing APIs.
22735 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
22736 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e  TE_ENABLE_COLUMN
22737 5f 4d 45 54 41 44 41 54 41 0a 23 20 64 65 66 69  _METADATA.# defi
22738 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
22739 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 20  n_database_name 
2273a 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
2273b 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61  ite3_column_data
2273c 62 61 73 65 5f 6e 61 6d 65 31 36 20 30 0a 23 20  base_name16 0.# 
2273d 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
2273e 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65  olumn_table_name
2273f 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
22740 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
22741 74 61 62 6c 65 5f 6e 61 6d 65 31 36 20 20 20 20  table_name16    
22742 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
22743 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e  e3_column_origin
22744 5f 6e 61 6d 65 20 20 20 20 20 30 0a 23 20 64 65  _name     0.# de
22745 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
22746 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31  umn_origin_name1
22747 36 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73  6   0.# define s
22748 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c  qlite3_table_col
22749 75 6d 6e 5f 6d 65 74 61 64 61 74 61 20 20 30 0a  umn_metadata  0.
2274a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
2274b 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
2274c 52 49 5a 41 54 49 4f 4e 0a 23 20 64 65 66 69 6e  RIZATION.# defin
2274d 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75  e sqlite3_set_au
2274e 74 68 6f 72 69 7a 65 72 20 20 20 20 20 20 20 20  thorizer        
2274f 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65   0.#endif..#ifde
22750 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
22751 46 31 36 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  F16.# define sql
22752 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36  ite3_bind_text16
22753 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 20              0.# 
22754 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
22755 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31  ollation_needed1
22756 36 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65  6     0.# define
22757 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
22758 64 65 63 6c 74 79 70 65 31 36 20 20 20 20 20 20  decltype16      
22759 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
2275a 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36  e3_column_name16
2275b 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65            0.# de
2275c 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
2275d 75 6d 6e 5f 74 65 78 74 31 36 20 20 20 20 20 20  umn_text16      
2275e 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
2275f 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31  qlite3_complete1
22760 36 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a  6             0.
22761 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
22762 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
22763 6e 31 36 20 20 20 20 20 30 0a 23 20 64 65 66 69  n16     0.# defi
22764 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ne sqlite3_creat
22765 65 5f 66 75 6e 63 74 69 6f 6e 31 36 20 20 20 20  e_function16    
22766 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
22767 69 74 65 33 5f 65 72 72 6d 73 67 31 36 20 20 20  ite3_errmsg16   
22768 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 20              0.# 
22769 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f  define sqlite3_o
2276a 70 65 6e 31 36 20 20 20 20 20 20 20 20 20 20 20  pen16           
2276b 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
2276c 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
2276d 31 36 20 20 20 20 20 20 20 20 20 20 20 20 20 20  16              
2276e 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
2276f 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 20  e3_prepare16_v2 
22770 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65            0.# de
22771 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73  fine sqlite3_res
22772 75 6c 74 5f 65 72 72 6f 72 31 36 20 20 20 20 20  ult_error16     
22773 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
22774 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
22775 78 74 31 36 20 20 20 20 20 20 20 20 20 20 30 0a  xt16          0.
22776 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
22777 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65  _result_text16be
22778 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69          0.# defi
22779 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ne sqlite3_resul
2277a 74 5f 74 65 78 74 31 36 6c 65 20 20 20 20 20 20  t_text16le      
2277b 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
2277c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
2277d 36 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 20  6           0.# 
2277e 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
2277f 61 6c 75 65 5f 74 65 78 74 31 36 62 65 20 20 20  alue_text16be   
22780 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
22781 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
22782 65 78 74 31 36 6c 65 20 20 20 20 20 20 20 20 20  ext16le         
22783 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
22784 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61  e3_column_databa
22785 73 65 5f 6e 61 6d 65 31 36 20 30 0a 23 20 64 65  se_name16 0.# de
22786 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
22787 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36  umn_table_name16
22788 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
22789 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72  qlite3_column_or
2278a 69 67 69 6e 5f 6e 61 6d 65 31 36 20 20 20 30 0a  igin_name16   0.
2278b 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
2278c 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4c  QLITE_OMIT_COMPL
2278d 45 54 45 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  ETE.# define sql
2278e 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 20 30 0a  ite3_complete 0.
2278f 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
22790 5f 63 6f 6d 70 6c 65 74 65 31 36 20 30 0a 23 65  _complete16 0.#e
22791 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
22792 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53  ITE_OMIT_PROGRES
22793 53 5f 43 41 4c 4c 42 41 43 4b 0a 23 20 64 65 66  S_CALLBACK.# def
22794 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 6f 67  ine sqlite3_prog
22795 72 65 73 73 5f 68 61 6e 64 6c 65 72 20 30 0a 23  ress_handler 0.#
22796 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
22797 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
22798 4c 54 41 42 4c 45 0a 23 20 64 65 66 69 6e 65 20  LTABLE.# define 
22799 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
2279a 6f 64 75 6c 65 20 30 0a 23 20 64 65 66 69 6e 65  odule 0.# define
2279b 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2279c 6d 6f 64 75 6c 65 5f 76 32 20 30 0a 23 20 64 65  module_v2 0.# de
2279d 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 64 65 63  fine sqlite3_dec
2279e 6c 61 72 65 5f 76 74 61 62 20 30 0a 23 65 6e 64  lare_vtab 0.#end
2279f 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
227a0 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
227a1 43 48 45 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  CHE.# define sql
227a2 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72  ite3_enable_shar
227a3 65 64 5f 63 61 63 68 65 20 30 0a 23 65 6e 64 69  ed_cache 0.#endi
227a4 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
227a5 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 23 20 64 65  _OMIT_TRACE.# de
227a6 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 6f  fine sqlite3_pro
227a7 66 69 6c 65 20 20 20 20 20 20 20 30 0a 23 20 64  file       0.# d
227a8 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 72  efine sqlite3_tr
227a9 61 63 65 20 20 20 20 20 20 20 20 20 30 0a 23 65  ace         0.#e
227aa 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
227ab 49 54 45 5f 4f 4d 49 54 5f 47 45 54 5f 54 41 42  ITE_OMIT_GET_TAB
227ac 4c 45 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  LE.# define sqli
227ad 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 20 20  te3_free_table  
227ae 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
227af 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 20 20  ite3_get_table  
227b0 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66     0.#endif..#if
227b1 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
227b2 49 4e 43 52 42 4c 4f 42 0a 23 64 65 66 69 6e 65  INCRBLOB.#define
227b3 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65   sqlite3_bind_ze
227b4 72 6f 62 6c 6f 62 20 20 30 0a 23 64 65 66 69 6e  roblob  0.#defin
227b5 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62  e sqlite3_blob_b
227b6 79 74 65 73 20 20 20 20 20 30 0a 23 64 65 66 69  ytes     0.#defi
227b7 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  ne sqlite3_blob_
227b8 63 6c 6f 73 65 20 20 20 20 20 30 0a 23 64 65 66  close     0.#def
227b9 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ine sqlite3_blob
227ba 5f 6f 70 65 6e 20 20 20 20 20 20 30 0a 23 64 65  _open      0.#de
227bb 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f  fine sqlite3_blo
227bc 62 5f 72 65 61 64 20 20 20 20 20 20 30 0a 23 64  b_read      0.#d
227bd 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c  efine sqlite3_bl
227be 6f 62 5f 77 72 69 74 65 20 20 20 20 20 30 0a 23  ob_write     0.#
227bf 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
227c0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
227c1 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 70 6f  ture contains po
227c2 69 6e 74 65 72 73 20 74 6f 20 61 6c 6c 20 53 51  inters to all SQ
227c3 4c 69 74 65 20 41 50 49 20 72 6f 75 74 69 6e 65  Lite API routine
227c4 73 2e 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20  s..** A pointer 
227c5 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72  to this structur
227c6 65 20 69 73 20 70 61 73 73 65 64 20 69 6e 74 6f  e is passed into
227c7 20 65 78 74 65 6e 73 69 6f 6e 73 20 77 68 65 6e   extensions when
227c8 20 74 68 65 79 20 61 72 65 0a 2a 2a 20 6c 6f 61   they are.** loa
227c9 64 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 20  ded so that the 
227ca 65 78 74 65 6e 73 69 6f 6e 20 63 61 6e 20 6d 61  extension can ma
227cb 6b 65 20 63 61 6c 6c 73 20 62 61 63 6b 20 69 6e  ke calls back in
227cc 74 6f 20 74 68 65 20 53 51 4c 69 74 65 0a 2a 2a  to the SQLite.**
227cd 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20   library..**.** 
227ce 57 68 65 6e 20 61 64 64 69 6e 67 20 6e 65 77 20  When adding new 
227cf 41 50 49 73 2c 20 61 64 64 20 74 68 65 6d 20 74  APIs, add them t
227d0 6f 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20  o the bottom of 
227d1 74 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a  this structure.*
227d2 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 72  * in order to pr
227d3 65 73 65 72 76 65 20 62 61 63 6b 77 61 72 64 73  eserve backwards
227d4 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a   compatibility..
227d5 2a 2a 0a 2a 2a 20 45 78 74 65 6e 73 69 6f 6e 73  **.** Extensions
227d6 20 74 68 61 74 20 75 73 65 20 6e 65 77 65 72 20   that use newer 
227d7 41 50 49 73 20 73 68 6f 75 6c 64 20 66 69 72 73  APIs should firs
227d8 74 20 63 61 6c 6c 20 74 68 65 0a 2a 2a 20 73 71  t call the.** sq
227d9 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e  lite3_libversion
227da 5f 6e 75 6d 62 65 72 28 29 20 74 6f 20 6d 61 6b  _number() to mak
227db 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20  e sure that the 
227dc 41 50 49 20 74 68 65 79 0a 2a 2a 20 69 6e 74 65  API they.** inte
227dd 6e 64 20 74 6f 20 75 73 65 20 69 73 20 73 75 70  nd to use is sup
227de 70 6f 72 74 65 64 20 62 79 20 74 68 65 20 6c 69  ported by the li
227df 62 72 61 72 79 2e 20 20 45 78 74 65 6e 73 69 6f  brary.  Extensio
227e0 6e 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 6c 73  ns should.** als
227e1 6f 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20  o check to make 
227e2 73 75 72 65 20 74 68 61 74 20 74 68 65 20 70 6f  sure that the po
227e3 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 75 6e  inter to the fun
227e4 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20  ction is.** not 
227e5 4e 55 4c 4c 20 62 65 66 6f 72 65 20 63 61 6c 6c  NULL before call
227e6 69 6e 67 20 69 74 2e 0a 2a 2f 0a 73 74 61 74 69  ing it..*/.stati
227e7 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
227e8 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 73 71 6c  api_routines sql
227e9 69 74 65 33 41 70 69 73 20 3d 20 7b 0a 20 20 73  ite3Apis = {.  s
227ea 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
227eb 5f 63 6f 6e 74 65 78 74 2c 0a 23 69 66 6e 64 65  _context,.#ifnde
227ec 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  f SQLITE_OMIT_DE
227ed 50 52 45 43 41 54 45 44 0a 20 20 73 71 6c 69 74  PRECATED.  sqlit
227ee 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75  e3_aggregate_cou
227ef 6e 74 2c 0a 23 65 6c 73 65 0a 20 20 30 2c 0a 23  nt,.#else.  0,.#
227f0 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f  endif.  sqlite3_
227f1 62 69 6e 64 5f 62 6c 6f 62 2c 0a 20 20 73 71 6c  bind_blob,.  sql
227f2 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65  ite3_bind_double
227f3 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ,.  sqlite3_bind
227f4 5f 69 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f  _int,.  sqlite3_
227f5 62 69 6e 64 5f 69 6e 74 36 34 2c 0a 20 20 73 71  bind_int64,.  sq
227f6 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 2c  lite3_bind_null,
227f7 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
227f8 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 2c  parameter_count,
227f9 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
227fa 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 2c  parameter_index,
227fb 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
227fc 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 2c 0a  parameter_name,.
227fd 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74    sqlite3_bind_t
227fe 65 78 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  ext,.  sqlite3_b
227ff 69 6e 64 5f 74 65 78 74 31 36 2c 0a 20 20 73 71  ind_text16,.  sq
22800 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65  lite3_bind_value
22801 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 75 73 79  ,.  sqlite3_busy
22802 5f 68 61 6e 64 6c 65 72 2c 0a 20 20 73 71 6c 69  _handler,.  sqli
22803 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74  te3_busy_timeout
22804 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 68 61 6e  ,.  sqlite3_chan
22805 67 65 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  ges,.  sqlite3_c
22806 6c 6f 73 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  lose,.  sqlite3_
22807 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
22808 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c  ,.  sqlite3_coll
22809 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 2c 0a  ation_needed16,.
2280a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
2280b 5f 62 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33  _blob,.  sqlite3
2280c 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 2c 0a 20  _column_bytes,. 
2280d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
2280e 62 79 74 65 73 31 36 2c 0a 20 20 73 71 6c 69 74  bytes16,.  sqlit
2280f 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 2c  e3_column_count,
22810 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
22811 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 2c  n_database_name,
22812 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
22813 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31  n_database_name1
22814 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  6,.  sqlite3_col
22815 75 6d 6e 5f 64 65 63 6c 74 79 70 65 2c 0a 20 20  umn_decltype,.  
22816 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
22817 65 63 6c 74 79 70 65 31 36 2c 0a 20 20 73 71 6c  ecltype16,.  sql
22818 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62  ite3_column_doub
22819 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  le,.  sqlite3_co
2281a 6c 75 6d 6e 5f 69 6e 74 2c 0a 20 20 73 71 6c 69  lumn_int,.  sqli
2281b 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34  te3_column_int64
2281c 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
2281d 6d 6e 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69 74  mn_name,.  sqlit
2281e 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36  e3_column_name16
2281f 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
22820 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 2c 0a  mn_origin_name,.
22821 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
22822 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 2c 0a  _origin_name16,.
22823 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
22824 5f 74 61 62 6c 65 5f 6e 61 6d 65 2c 0a 20 20 73  _table_name,.  s
22825 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61  qlite3_column_ta
22826 62 6c 65 5f 6e 61 6d 65 31 36 2c 0a 20 20 73 71  ble_name16,.  sq
22827 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
22828 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  t,.  sqlite3_col
22829 75 6d 6e 5f 74 65 78 74 31 36 2c 0a 20 20 73 71  umn_text16,.  sq
2282a 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
2282b 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  e,.  sqlite3_col
2282c 75 6d 6e 5f 76 61 6c 75 65 2c 0a 20 20 73 71 6c  umn_value,.  sql
2282d 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b  ite3_commit_hook
2282e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  ,.  sqlite3_comp
2282f 6c 65 74 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  lete,.  sqlite3_
22830 63 6f 6d 70 6c 65 74 65 31 36 2c 0a 20 20 73 71  complete16,.  sq
22831 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
22832 6c 61 74 69 6f 6e 2c 0a 20 20 73 71 6c 69 74 65  lation,.  sqlite
22833 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
22834 6f 6e 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f  on16,.  sqlite3_
22835 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 2c  create_function,
22836 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  .  sqlite3_creat
22837 65 5f 66 75 6e 63 74 69 6f 6e 31 36 2c 0a 20 20  e_function16,.  
22838 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
22839 6f 64 75 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33  odule,.  sqlite3
2283a 5f 64 61 74 61 5f 63 6f 75 6e 74 2c 0a 20 20 73  _data_count,.  s
2283b 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65  qlite3_db_handle
2283c 2c 0a 20 20 73 71 6c 69 74 65 33 5f 64 65 63 6c  ,.  sqlite3_decl
2283d 61 72 65 5f 76 74 61 62 2c 0a 20 20 73 71 6c 69  are_vtab,.  sqli
2283e 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65  te3_enable_share
2283f 64 5f 63 61 63 68 65 2c 0a 20 20 73 71 6c 69 74  d_cache,.  sqlit
22840 65 33 5f 65 72 72 63 6f 64 65 2c 0a 20 20 73 71  e3_errcode,.  sq
22841 6c 69 74 65 33 5f 65 72 72 6d 73 67 2c 0a 20 20  lite3_errmsg,.  
22842 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36  sqlite3_errmsg16
22843 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65 78 65 63  ,.  sqlite3_exec
22844 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ,.#ifndef SQLITE
22845 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
22846 0a 20 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72  .  sqlite3_expir
22847 65 64 2c 0a 23 65 6c 73 65 0a 20 20 30 2c 0a 23  ed,.#else.  0,.#
22848 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f  endif.  sqlite3_
22849 66 69 6e 61 6c 69 7a 65 2c 0a 20 20 73 71 6c 69  finalize,.  sqli
2284a 74 65 33 5f 66 72 65 65 2c 0a 20 20 73 71 6c 69  te3_free,.  sqli
2284b 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 2c 0a  te3_free_table,.
2284c 20 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75    sqlite3_get_au
2284d 74 6f 63 6f 6d 6d 69 74 2c 0a 20 20 73 71 6c 69  tocommit,.  sqli
2284e 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 2c  te3_get_auxdata,
2284f 0a 20 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  .  sqlite3_get_t
22850 61 62 6c 65 2c 0a 20 20 30 2c 20 20 20 20 20 2f  able,.  0,     /
22851 2a 20 57 61 73 20 73 71 6c 69 74 65 33 5f 67 6c  * Was sqlite3_gl
22852 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28 29 2c 20  obal_recover(), 
22853 62 75 74 20 74 68 61 74 20 66 75 6e 63 74 69 6f  but that functio
22854 6e 20 69 73 20 64 65 70 72 65 63 61 74 65 64 20  n is deprecated 
22855 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
22856 65 72 72 75 70 74 2c 0a 20 20 73 71 6c 69 74 65  errupt,.  sqlite
22857 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
22858 77 69 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6c  wid,.  sqlite3_l
22859 69 62 76 65 72 73 69 6f 6e 2c 0a 20 20 73 71 6c  ibversion,.  sql
2285a 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f  ite3_libversion_
2285b 6e 75 6d 62 65 72 2c 0a 20 20 73 71 6c 69 74 65  number,.  sqlite
2285c 33 5f 6d 61 6c 6c 6f 63 2c 0a 20 20 73 71 6c 69  3_malloc,.  sqli
2285d 74 65 33 5f 6d 70 72 69 6e 74 66 2c 0a 20 20 73  te3_mprintf,.  s
2285e 71 6c 69 74 65 33 5f 6f 70 65 6e 2c 0a 20 20 73  qlite3_open,.  s
2285f 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 2c 0a 20  qlite3_open16,. 
22860 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
22861 2c 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 65 70  ,.  sqlite3_prep
22862 61 72 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  are16,.  sqlite3
22863 5f 70 72 6f 66 69 6c 65 2c 0a 20 20 73 71 6c 69  _profile,.  sqli
22864 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e  te3_progress_han
22865 64 6c 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f  dler,.  sqlite3_
22866 72 65 61 6c 6c 6f 63 2c 0a 20 20 73 71 6c 69 74  realloc,.  sqlit
22867 65 33 5f 72 65 73 65 74 2c 0a 20 20 73 71 6c 69  e3_reset,.  sqli
22868 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 2c  te3_result_blob,
22869 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
2286a 74 5f 64 6f 75 62 6c 65 2c 0a 20 20 73 71 6c 69  t_double,.  sqli
2286b 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
2286c 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
2286d 6c 74 5f 65 72 72 6f 72 31 36 2c 0a 20 20 73 71  lt_error16,.  sq
2286e 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
2286f 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
22870 6c 74 5f 69 6e 74 36 34 2c 0a 20 20 73 71 6c 69  lt_int64,.  sqli
22871 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 2c  te3_result_null,
22872 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
22873 74 5f 74 65 78 74 2c 0a 20 20 73 71 6c 69 74 65  t_text,.  sqlite
22874 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 2c  3_result_text16,
22875 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
22876 74 5f 74 65 78 74 31 36 62 65 2c 0a 20 20 73 71  t_text16be,.  sq
22877 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
22878 74 31 36 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33  t16le,.  sqlite3
22879 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 2c 0a 20  _result_value,. 
2287a 20 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63   sqlite3_rollbac
2287b 6b 5f 68 6f 6f 6b 2c 0a 20 20 73 71 6c 69 74 65  k_hook,.  sqlite
2287c 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72  3_set_authorizer
2287d 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 65 74 5f  ,.  sqlite3_set_
2287e 61 75 78 64 61 74 61 2c 0a 20 20 73 71 6c 69 74  auxdata,.  sqlit
2287f 65 33 5f 73 6e 70 72 69 6e 74 66 2c 0a 20 20 73  e3_snprintf,.  s
22880 71 6c 69 74 65 33 5f 73 74 65 70 2c 0a 20 20 73  qlite3_step,.  s
22881 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c  qlite3_table_col
22882 75 6d 6e 5f 6d 65 74 61 64 61 74 61 2c 0a 23 69  umn_metadata,.#i
22883 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
22884 54 5f 44 45 50 52 45 43 41 54 45 44 0a 20 20 73  T_DEPRECATED.  s
22885 71 6c 69 74 65 33 5f 74 68 72 65 61 64 5f 63 6c  qlite3_thread_cl
22886 65 61 6e 75 70 2c 0a 23 65 6c 73 65 0a 20 20 30  eanup,.#else.  0
22887 2c 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74  ,.#endif.  sqlit
22888 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
22889 2c 0a 20 20 73 71 6c 69 74 65 33 5f 74 72 61 63  ,.  sqlite3_trac
2288a 65 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  e,.#ifndef SQLIT
2288b 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45  E_OMIT_DEPRECATE
2288c 44 0a 20 20 73 71 6c 69 74 65 33 5f 74 72 61 6e  D.  sqlite3_tran
2288d 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 2c 0a 23  sfer_bindings,.#
2288e 65 6c 73 65 0a 20 20 30 2c 0a 23 65 6e 64 69 66  else.  0,.#endif
2288f 0a 20 20 73 71 6c 69 74 65 33 5f 75 70 64 61 74  .  sqlite3_updat
22890 65 5f 68 6f 6f 6b 2c 0a 20 20 73 71 6c 69 74 65  e_hook,.  sqlite
22891 33 5f 75 73 65 72 5f 64 61 74 61 2c 0a 20 20 73  3_user_data,.  s
22892 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
22893 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  b,.  sqlite3_val
22894 75 65 5f 62 79 74 65 73 2c 0a 20 20 73 71 6c 69  ue_bytes,.  sqli
22895 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 31  te3_value_bytes1
22896 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  6,.  sqlite3_val
22897 75 65 5f 64 6f 75 62 6c 65 2c 0a 20 20 73 71 6c  ue_double,.  sql
22898 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 2c 0a  ite3_value_int,.
22899 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f    sqlite3_value_
2289a 69 6e 74 36 34 2c 0a 20 20 73 71 6c 69 74 65 33  int64,.  sqlite3
2289b 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74  _value_numeric_t
2289c 79 70 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  ype,.  sqlite3_v
2289d 61 6c 75 65 5f 74 65 78 74 2c 0a 20 20 73 71 6c  alue_text,.  sql
2289e 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
2289f 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  6,.  sqlite3_val
228a0 75 65 5f 74 65 78 74 31 36 62 65 2c 0a 20 20 73  ue_text16be,.  s
228a1 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
228a2 74 31 36 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33  t16le,.  sqlite3
228a3 5f 76 61 6c 75 65 5f 74 79 70 65 2c 0a 20 20 73  _value_type,.  s
228a4 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 2c  qlite3_vmprintf,
228a5 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6f  .  /*.  ** The o
228a6 72 69 67 69 6e 61 6c 20 41 50 49 20 73 65 74 20  riginal API set 
228a7 65 6e 64 73 20 68 65 72 65 2e 20 20 41 6c 6c 20  ends here.  All 
228a8 65 78 74 65 6e 73 69 6f 6e 73 20 63 61 6e 20 63  extensions can c
228a9 61 6c 6c 20 61 6e 79 0a 20 20 2a 2a 20 6f 66 20  all any.  ** of 
228aa 74 68 65 20 41 50 49 73 20 61 62 6f 76 65 20 70  the APIs above p
228ab 72 6f 76 69 64 65 64 20 74 68 61 74 20 74 68 65  rovided that the
228ac 20 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74 20   pointer is not 
228ad 4e 55 4c 4c 2e 20 20 42 75 74 0a 20 20 2a 2a 20  NULL.  But.  ** 
228ae 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 41  before calling A
228af 50 49 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 2c  PIs that follow,
228b0 20 65 78 74 65 6e 73 69 6f 6e 20 73 68 6f 75 6c   extension shoul
228b1 64 20 63 68 65 63 6b 20 74 68 65 0a 20 20 2a 2a  d check the.  **
228b2 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73   sqlite3_libvers
228b3 69 6f 6e 5f 6e 75 6d 62 65 72 28 29 20 74 6f 20  ion_number() to 
228b4 6d 61 6b 65 20 73 75 72 65 20 74 68 65 79 20 61  make sure they a
228b5 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 0a  re dealing with.
228b6 20 20 2a 2a 20 61 20 6c 69 62 72 61 72 79 20 74    ** a library t
228b7 68 61 74 20 69 73 20 6e 65 77 20 65 6e 6f 75 67  hat is new enoug
228b8 68 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68 61  h to support tha
228b9 74 20 41 50 49 2e 0a 20 20 2a 2a 2a 2a 2a 2a 2a  t API..  *******
228ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
228bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
228bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
228bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
228be 2a 2a 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  **.  */.  sqlite
228bf 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74  3_overload_funct
228c0 69 6f 6e 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  ion,..  /*.  ** 
228c1 41 64 64 65 64 20 61 66 74 65 72 20 33 2e 33 2e  Added after 3.3.
228c2 31 33 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  13.  */.  sqlite
228c3 33 5f 70 72 65 70 61 72 65 5f 76 32 2c 0a 20 20  3_prepare_v2,.  
228c4 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
228c5 36 5f 76 32 2c 0a 20 20 73 71 6c 69 74 65 33 5f  6_v2,.  sqlite3_
228c6 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 2c 0a  clear_bindings,.
228c7 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65 64  .  /*.  ** Added
228c8 20 66 6f 72 20 33 2e 34 2e 31 0a 20 20 2a 2f 0a   for 3.4.1.  */.
228c9 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
228ca 5f 6d 6f 64 75 6c 65 5f 76 32 2c 0a 0a 20 20 2f  _module_v2,..  /
228cb 2a 0a 20 20 2a 2a 20 41 64 64 65 64 20 66 6f 72  *.  ** Added for
228cc 20 33 2e 35 2e 30 0a 20 20 2a 2f 0a 20 20 73 71   3.5.0.  */.  sq
228cd 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62  lite3_bind_zerob
228ce 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  lob,.  sqlite3_b
228cf 6c 6f 62 5f 62 79 74 65 73 2c 0a 20 20 73 71 6c  lob_bytes,.  sql
228d0 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 2c  ite3_blob_close,
228d1 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  .  sqlite3_blob_
228d2 6f 70 65 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f  open,.  sqlite3_
228d3 62 6c 6f 62 5f 72 65 61 64 2c 0a 20 20 73 71 6c  blob_read,.  sql
228d4 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 2c  ite3_blob_write,
228d5 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  .  sqlite3_creat
228d6 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 2c 0a  e_collation_v2,.
228d7 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63    sqlite3_file_c
228d8 6f 6e 74 72 6f 6c 2c 0a 20 20 73 71 6c 69 74 65  ontrol,.  sqlite
228d9 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74  3_memory_highwat
228da 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 65  er,.  sqlite3_me
228db 6d 6f 72 79 5f 75 73 65 64 2c 0a 23 69 66 64 65  mory_used,.#ifde
228dc 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f  f SQLITE_MUTEX_O
228dd 4d 49 54 0a 20 20 30 2c 20 0a 20 20 30 2c 20 0a  MIT.  0, .  0, .
228de 20 20 30 2c 0a 20 20 30 2c 0a 20 20 30 2c 0a 23    0,.  0,.  0,.#
228df 65 6c 73 65 0a 20 20 73 71 6c 69 74 65 33 5f 6d  else.  sqlite3_m
228e0 75 74 65 78 5f 61 6c 6c 6f 63 2c 0a 20 20 73 71  utex_alloc,.  sq
228e1 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
228e2 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  r,.  sqlite3_mut
228e3 65 78 5f 66 72 65 65 2c 0a 20 20 73 71 6c 69 74  ex_free,.  sqlit
228e4 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 2c 0a  e3_mutex_leave,.
228e5 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
228e6 74 72 79 2c 0a 23 65 6e 64 69 66 0a 20 20 73 71  try,.#endif.  sq
228e7 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 2c 0a 20  lite3_open_v2,. 
228e8 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65   sqlite3_release
228e9 5f 6d 65 6d 6f 72 79 2c 0a 20 20 73 71 6c 69 74  _memory,.  sqlit
228ea 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
228eb 6e 6f 6d 65 6d 2c 0a 20 20 73 71 6c 69 74 65 33  nomem,.  sqlite3
228ec 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f  _result_error_to
228ed 6f 62 69 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f  obig,.  sqlite3_
228ee 73 6c 65 65 70 2c 0a 20 20 73 71 6c 69 74 65 33  sleep,.  sqlite3
228ef 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74  _soft_heap_limit
228f0 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f  ,.  sqlite3_vfs_
228f1 66 69 6e 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f  find,.  sqlite3_
228f2 76 66 73 5f 72 65 67 69 73 74 65 72 2c 0a 20 20  vfs_register,.  
228f3 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65  sqlite3_vfs_unre
228f4 67 69 73 74 65 72 2c 0a 0a 20 20 2f 2a 0a 20 20  gister,..  /*.  
228f5 2a 2a 20 41 64 64 65 64 20 66 6f 72 20 33 2e 35  ** Added for 3.5
228f6 2e 38 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  .8.  */.  sqlite
228f7 33 5f 74 68 72 65 61 64 73 61 66 65 2c 0a 20 20  3_threadsafe,.  
228f8 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a  sqlite3_result_z
228f9 65 72 6f 62 6c 6f 62 2c 0a 20 20 73 71 6c 69 74  eroblob,.  sqlit
228fa 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
228fb 63 6f 64 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  code,.  sqlite3_
228fc 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 2c 0a 20 20  test_control,.  
228fd 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65  sqlite3_randomne
228fe 73 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  ss,.  sqlite3_co
228ff 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 2c  ntext_db_handle,
22900 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65  ..  /*.  ** Adde
22901 64 20 66 6f 72 20 33 2e 36 2e 30 0a 20 20 2a 2f  d for 3.6.0.  */
22902 0a 20 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e  .  sqlite3_exten
22903 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73  ded_result_codes
22904 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69  ,.  sqlite3_limi
22905 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6e 65 78  t,.  sqlite3_nex
22906 74 5f 73 74 6d 74 2c 0a 20 20 73 71 6c 69 74 65  t_stmt,.  sqlite
22907 33 5f 73 71 6c 2c 0a 20 20 73 71 6c 69 74 65 33  3_sql,.  sqlite3
22908 5f 73 74 61 74 75 73 2c 0a 7d 3b 0a 0a 2f 2a 0a  _status,.};../*.
22909 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f  ** Attempt to lo
2290a 61 64 20 61 6e 20 53 51 4c 69 74 65 20 65 78 74  ad an SQLite ext
2290b 65 6e 73 69 6f 6e 20 6c 69 62 72 61 72 79 20 63  ension library c
2290c 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20  ontained in the 
2290d 66 69 6c 65 0a 2a 2a 20 7a 46 69 6c 65 2e 20 20  file.** zFile.  
2290e 54 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 20  The entry point 
2290f 69 73 20 7a 50 72 6f 63 2e 20 20 7a 50 72 6f 63  is zProc.  zProc
22910 20 6d 61 79 20 62 65 20 30 20 69 6e 20 77 68 69   may be 0 in whi
22911 63 68 20 63 61 73 65 20 61 0a 2a 2a 20 64 65 66  ch case a.** def
22912 61 75 6c 74 20 65 6e 74 72 79 20 70 6f 69 6e 74  ault entry point
22913 20 6e 61 6d 65 20 28 73 71 6c 69 74 65 33 5f 65   name (sqlite3_e
22914 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 29 20 69  xtension_init) i
22915 73 20 75 73 65 64 2e 20 20 55 73 65 0a 2a 2a 20  s used.  Use.** 
22916 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 6e  of the default n
22917 61 6d 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64  ame is recommend
22918 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  ed..**.** Return
22919 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75   SQLITE_OK on su
2291a 63 63 65 73 73 20 61 6e 64 20 53 51 4c 49 54 45  ccess and SQLITE
2291b 5f 45 52 52 4f 52 20 69 66 20 73 6f 6d 65 74 68  _ERROR if someth
2291c 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a  ing goes wrong..
2291d 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f  **.** If an erro
2291e 72 20 6f 63 63 75 72 73 20 61 6e 64 20 70 7a 45  r occurs and pzE
2291f 72 72 4d 73 67 20 69 73 20 6e 6f 74 20 30 2c 20  rrMsg is not 0, 
22920 74 68 65 6e 20 66 69 6c 6c 20 2a 70 7a 45 72 72  then fill *pzErr
22921 4d 73 67 20 77 69 74 68 20 0a 2a 2a 20 65 72 72  Msg with .** err
22922 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 2e  or message text.
22923 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75    The calling fu
22924 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 66 72  nction should fr
22925 65 65 20 74 68 69 73 20 6d 65 6d 6f 72 79 0a 2a  ee this memory.*
22926 2a 20 62 79 20 63 61 6c 6c 69 6e 67 20 73 71 6c  * by calling sql
22927 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 29  ite3DbFree(db, )
22928 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
22929 73 71 6c 69 74 65 33 4c 6f 61 64 45 78 74 65 6e  sqlite3LoadExten
2292a 73 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20  sion(.  sqlite3 
2292b 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  *db,          /*
2292c 20 4c 6f 61 64 20 74 68 65 20 65 78 74 65 6e 73   Load the extens
2292d 69 6f 6e 20 69 6e 74 6f 20 74 68 69 73 20 64 61  ion into this da
2292e 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2292f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  n */.  const cha
22930 72 20 2a 7a 46 69 6c 65 2c 20 20 20 20 2f 2a 20  r *zFile,    /* 
22931 4e 61 6d 65 20 6f 66 20 74 68 65 20 73 68 61 72  Name of the shar
22932 65 64 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 61  ed library conta
22933 69 6e 69 6e 67 20 65 78 74 65 6e 73 69 6f 6e 20  ining extension 
22934 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
22935 2a 7a 50 72 6f 63 2c 20 20 20 20 2f 2a 20 45 6e  *zProc,    /* En
22936 74 72 79 20 70 6f 69 6e 74 2e 20 20 55 73 65 20  try point.  Use 
22937 22 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69  "sqlite3_extensi
22938 6f 6e 5f 69 6e 69 74 22 20 69 66 20 30 20 2a 2f  on_init" if 0 */
22939 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d  .  char **pzErrM
2293a 73 67 20 20 20 20 20 20 20 2f 2a 20 50 75 74 20  sg       /* Put 
2293b 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 68 65  error message he
2293c 72 65 20 69 66 20 6e 6f 74 20 30 20 2a 2f 0a 29  re if not 0 */.)
2293d 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  {.  sqlite3_vfs 
2293e 2a 70 56 66 73 20 3d 20 64 62 2d 3e 70 56 66 73  *pVfs = db->pVfs
2293f 3b 0a 20 20 76 6f 69 64 20 2a 68 61 6e 64 6c 65  ;.  void *handle
22940 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69 74 29  ;.  int (*xInit)
22941 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a  (sqlite3*,char**
22942 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61  ,const sqlite3_a
22943 70 69 5f 72 6f 75 74 69 6e 65 73 2a 29 3b 0a 20  pi_routines*);. 
22944 20 63 68 61 72 20 2a 7a 45 72 72 6d 73 67 20 3d   char *zErrmsg =
22945 20 30 3b 0a 20 20 76 6f 69 64 20 2a 2a 61 48 61   0;.  void **aHa
22946 6e 64 6c 65 3b 0a 0a 20 20 2f 2a 20 54 69 63 6b  ndle;..  /* Tick
22947 65 74 20 23 31 38 36 33 2e 20 20 54 6f 20 61 76  et #1863.  To av
22948 6f 69 64 20 61 20 63 72 65 61 74 69 6e 67 20 73  oid a creating s
22949 65 63 75 72 69 74 79 20 70 72 6f 62 6c 65 6d 73  ecurity problems
2294a 20 66 6f 72 20 6f 6c 64 65 72 0a 20 20 2a 2a 20   for older.  ** 
2294b 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61  applications tha
2294c 74 20 72 65 6c 69 6e 6b 20 61 67 61 69 6e 73 74  t relink against
2294d 20 6e 65 77 65 72 20 76 65 72 73 69 6f 6e 73 20   newer versions 
2294e 6f 66 20 53 51 4c 69 74 65 2c 20 74 68 65 0a 20  of SQLite, the. 
2294f 20 2a 2a 20 61 62 69 6c 69 74 79 20 74 6f 20 72   ** ability to r
22950 75 6e 20 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f  un load_extensio
22951 6e 20 69 73 20 74 75 72 6e 65 64 20 6f 66 66 20  n is turned off 
22952 62 79 20 64 65 66 61 75 6c 74 2e 20 20 4f 6e 65  by default.  One
22953 0a 20 20 2a 2a 20 6d 75 73 74 20 63 61 6c 6c 20  .  ** must call 
22954 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c  sqlite3_enable_l
22955 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20  oad_extension() 
22956 74 6f 20 74 75 72 6e 20 6f 6e 20 65 78 74 65 6e  to turn on exten
22957 73 69 6f 6e 0a 20 20 2a 2a 20 6c 6f 61 64 69 6e  sion.  ** loadin
22958 67 2e 20 20 4f 74 68 65 72 77 69 73 65 20 79 6f  g.  Otherwise yo
22959 75 20 67 65 74 20 74 68 65 20 66 6f 6c 6c 6f 77  u get the follow
2295a 69 6e 67 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a  ing error..  */.
2295b 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73    if( (db->flags
2295c 20 26 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78   & SQLITE_LoadEx
2295d 74 65 6e 73 69 6f 6e 29 3d 3d 30 20 29 7b 0a 20  tension)==0 ){. 
2295e 20 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20     if( pzErrMsg 
2295f 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 4d  ){.      *pzErrM
22960 73 67 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  sg = sqlite3_mpr
22961 69 6e 74 66 28 22 6e 6f 74 20 61 75 74 68 6f 72  intf("not author
22962 69 7a 65 64 22 29 3b 0a 20 20 20 20 7d 0a 20 20  ized");.    }.  
22963 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
22964 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 69 66  ERROR;.  }..  if
22965 28 20 7a 50 72 6f 63 3d 3d 30 20 29 7b 0a 20 20  ( zProc==0 ){.  
22966 20 20 7a 50 72 6f 63 20 3d 20 22 73 71 6c 69 74    zProc = "sqlit
22967 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69  e3_extension_ini
22968 74 22 3b 0a 20 20 7d 0a 0a 20 20 68 61 6e 64 6c  t";.  }..  handl
22969 65 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 6c 4f  e = sqlite3OsDlO
2296a 70 65 6e 28 70 56 66 73 2c 20 7a 46 69 6c 65 29  pen(pVfs, zFile)
2296b 3b 0a 20 20 69 66 28 20 68 61 6e 64 6c 65 3d 3d  ;.  if( handle==
2296c 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 7a 45  0 ){.    if( pzE
2296d 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 63  rrMsg ){.      c
2296e 68 61 72 20 7a 45 72 72 5b 32 35 36 5d 3b 0a 20  har zErr[256];. 
2296f 20 20 20 20 20 7a 45 72 72 5b 73 69 7a 65 6f 66       zErr[sizeof
22970 28 7a 45 72 72 29 2d 31 5d 20 3d 20 27 5c 30 27  (zErr)-1] = '\0'
22971 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
22972 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
22973 7a 45 72 72 29 2d 31 2c 20 7a 45 72 72 2c 20 0a  zErr)-1, zErr, .
22974 20 20 20 20 20 20 20 20 20 20 22 75 6e 61 62 6c            "unabl
22975 65 20 74 6f 20 6f 70 65 6e 20 73 68 61 72 65 64  e to open shared
22976 20 6c 69 62 72 61 72 79 20 5b 25 73 5d 22 2c 20   library [%s]", 
22977 7a 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 73 71  zFile);.      sq
22978 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 28 70  lite3OsDlError(p
22979 56 66 73 2c 20 73 69 7a 65 6f 66 28 7a 45 72 72  Vfs, sizeof(zErr
2297a 29 2d 31 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20  )-1, zErr);.    
2297b 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71    *pzErrMsg = sq
2297c 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30 2c  lite3DbStrDup(0,
2297d 20 7a 45 72 72 29 3b 0a 20 20 20 20 7d 0a 20 20   zErr);.    }.  
2297e 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2297f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 78 49 6e  ERROR;.  }.  xIn
22980 69 74 20 3d 20 28 69 6e 74 28 2a 29 28 73 71 6c  it = (int(*)(sql
22981 69 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e  ite3*,char**,con
22982 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  st sqlite3_api_r
22983 6f 75 74 69 6e 65 73 2a 29 29 0a 20 20 20 20 20  outines*)).     
22984 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
22985 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28 70 56 66  lite3OsDlSym(pVf
22986 73 2c 20 68 61 6e 64 6c 65 2c 20 7a 50 72 6f 63  s, handle, zProc
22987 29 3b 0a 20 20 69 66 28 20 78 49 6e 69 74 3d 3d  );.  if( xInit==
22988 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 7a 45  0 ){.    if( pzE
22989 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 63  rrMsg ){.      c
2298a 68 61 72 20 7a 45 72 72 5b 32 35 36 5d 3b 0a 20  har zErr[256];. 
2298b 20 20 20 20 20 7a 45 72 72 5b 73 69 7a 65 6f 66       zErr[sizeof
2298c 28 7a 45 72 72 29 2d 31 5d 20 3d 20 27 5c 30 27  (zErr)-1] = '\0'
2298d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
2298e 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
2298f 7a 45 72 72 29 2d 31 2c 20 7a 45 72 72 2c 0a 20  zErr)-1, zErr,. 
22990 20 20 20 20 20 20 20 20 20 22 6e 6f 20 65 6e 74           "no ent
22991 72 79 20 70 6f 69 6e 74 20 5b 25 73 5d 20 69 6e  ry point [%s] in
22992 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20   shared library 
22993 5b 25 73 5d 22 2c 20 7a 50 72 6f 63 2c 7a 46 69  [%s]", zProc,zFi
22994 6c 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  le);.      sqlit
22995 65 33 4f 73 44 6c 45 72 72 6f 72 28 70 56 66 73  e3OsDlError(pVfs
22996 2c 20 73 69 7a 65 6f 66 28 7a 45 72 72 29 2d 31  , sizeof(zErr)-1
22997 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 2a  , zErr);.      *
22998 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74  pzErrMsg = sqlit
22999 65 33 44 62 53 74 72 44 75 70 28 30 2c 20 7a 45  e3DbStrDup(0, zE
2299a 72 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  rr);.      sqlit
2299b 65 33 4f 73 44 6c 43 6c 6f 73 65 28 70 56 66 73  e3OsDlClose(pVfs
2299c 2c 20 68 61 6e 64 6c 65 29 3b 0a 20 20 20 20 7d  , handle);.    }
2299d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
2299e 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73  TE_ERROR;.  }els
2299f 65 20 69 66 28 20 78 49 6e 69 74 28 64 62 2c 20  e if( xInit(db, 
229a0 26 7a 45 72 72 6d 73 67 2c 20 26 73 71 6c 69 74  &zErrmsg, &sqlit
229a1 65 33 41 70 69 73 29 20 29 7b 0a 20 20 20 20 69  e3Apis) ){.    i
229a2 66 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20  f( pzErrMsg ){. 
229a3 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d       *pzErrMsg =
229a4 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
229a5 28 22 65 72 72 6f 72 20 64 75 72 69 6e 67 20 69  ("error during i
229a6 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 3a 20 25  nitialization: %
229a7 73 22 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20  s", zErrmsg);.  
229a8 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
229a9 66 72 65 65 28 7a 45 72 72 6d 73 67 29 3b 0a 20  free(zErrmsg);. 
229aa 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c     sqlite3OsDlCl
229ab 6f 73 65 28 70 56 66 73 2c 20 68 61 6e 64 6c 65  ose(pVfs, handle
229ac 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
229ad 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
229ae 0a 20 20 2f 2a 20 41 70 70 65 6e 64 20 74 68 65  .  /* Append the
229af 20 6e 65 77 20 73 68 61 72 65 64 20 6c 69 62 72   new shared libr
229b0 61 72 79 20 68 61 6e 64 6c 65 20 74 6f 20 74 68  ary handle to th
229b1 65 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e  e db->aExtension
229b2 20 61 72 72 61 79 2e 20 2a 2f 0a 20 20 61 48 61   array. */.  aHa
229b3 6e 64 6c 65 20 3d 20 73 71 6c 69 74 65 33 44 62  ndle = sqlite3Db
229b4 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
229b5 69 7a 65 6f 66 28 68 61 6e 64 6c 65 29 2a 28 64  izeof(handle)*(d
229b6 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 2b 31 29  b->nExtension+1)
229b7 29 3b 0a 20 20 69 66 28 20 61 48 61 6e 64 6c 65  );.  if( aHandle
229b8 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
229b9 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
229ba 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6e 45    }.  if( db->nE
229bb 78 74 65 6e 73 69 6f 6e 3e 30 20 29 7b 0a 20 20  xtension>0 ){.  
229bc 20 20 6d 65 6d 63 70 79 28 61 48 61 6e 64 6c 65    memcpy(aHandle
229bd 2c 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e  , db->aExtension
229be 2c 20 73 69 7a 65 6f 66 28 68 61 6e 64 6c 65 29  , sizeof(handle)
229bf 2a 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 29  *db->nExtension)
229c0 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44  ;.  }.  sqlite3D
229c1 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e 61 45  bFree(db, db->aE
229c2 78 74 65 6e 73 69 6f 6e 29 3b 0a 20 20 64 62 2d  xtension);.  db-
229c3 3e 61 45 78 74 65 6e 73 69 6f 6e 20 3d 20 61 48  >aExtension = aH
229c4 61 6e 64 6c 65 3b 0a 0a 20 20 64 62 2d 3e 61 45  andle;..  db->aE
229c5 78 74 65 6e 73 69 6f 6e 5b 64 62 2d 3e 6e 45 78  xtension[db->nEx
229c6 74 65 6e 73 69 6f 6e 2b 2b 5d 20 3d 20 68 61 6e  tension++] = han
229c7 64 6c 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  dle;.  return SQ
229c8 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54  LITE_OK;.}.SQLIT
229c9 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
229ca 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  3_load_extension
229cb 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
229cc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 61            /* Loa
229cd 64 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20  d the extension 
229ce 69 6e 74 6f 20 74 68 69 73 20 64 61 74 61 62 61  into this databa
229cf 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
229d0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
229d1 46 69 6c 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65  File,    /* Name
229d2 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 6c   of the shared l
229d3 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 69 6e  ibrary containin
229d4 67 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20  g extension */. 
229d5 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 72   const char *zPr
229d6 6f 63 2c 20 20 20 20 2f 2a 20 45 6e 74 72 79 20  oc,    /* Entry 
229d7 70 6f 69 6e 74 2e 20 20 55 73 65 20 22 73 71 6c  point.  Use "sql
229d8 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69  ite3_extension_i
229d9 6e 69 74 22 20 69 66 20 30 20 2a 2f 0a 20 20 63  nit" if 0 */.  c
229da 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20  har **pzErrMsg  
229db 20 20 20 20 20 2f 2a 20 50 75 74 20 65 72 72 6f       /* Put erro
229dc 72 20 6d 65 73 73 61 67 65 20 68 65 72 65 20 69  r message here i
229dd 66 20 6e 6f 74 20 30 20 2a 2f 0a 29 7b 0a 20 20  f not 0 */.){.  
229de 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
229df 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
229e0 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 63 20 3d  ->mutex);.  rc =
229e1 20 73 71 6c 69 74 65 33 4c 6f 61 64 45 78 74 65   sqlite3LoadExte
229e2 6e 73 69 6f 6e 28 64 62 2c 20 7a 46 69 6c 65 2c  nsion(db, zFile,
229e3 20 7a 50 72 6f 63 2c 20 70 7a 45 72 72 4d 73 67   zProc, pzErrMsg
229e4 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
229e5 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
229e6 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
229e7 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20  ;.}../*.** Call 
229e8 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65  this routine whe
229e9 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  n the database c
229ea 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f  onnection is clo
229eb 73 69 6e 67 20 69 6e 20 6f 72 64 65 72 0a 2a 2a  sing in order.**
229ec 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 6c 6f 61   to clean up loa
229ed 64 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 0a 2a  ded extensions.*
229ee 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
229ef 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6c 6f   void sqlite3Clo
229f0 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c  seExtensions(sql
229f1 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74  ite3 *db){.  int
229f2 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   i;.  assert( sq
229f3 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
229f4 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
229f5 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
229f6 6e 45 78 74 65 6e 73 69 6f 6e 3b 20 69 2b 2b 29  nExtension; i++)
229f7 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44  {.    sqlite3OsD
229f8 6c 43 6c 6f 73 65 28 64 62 2d 3e 70 56 66 73 2c  lClose(db->pVfs,
229f9 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 5b   db->aExtension[
229fa 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  i]);.  }.  sqlit
229fb 65 33 44 62 46 72 65 65 28 64 62 2c 20 64 62 2d  e3DbFree(db, db-
229fc 3e 61 45 78 74 65 6e 73 69 6f 6e 29 3b 0a 7d 0a  >aExtension);.}.
229fd 0a 2f 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f 72  ./*.** Enable or
229fe 20 64 69 73 61 62 6c 65 20 65 78 74 65 6e 73 69   disable extensi
229ff 6f 6e 20 6c 6f 61 64 69 6e 67 2e 20 20 45 78 74  on loading.  Ext
22a00 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69  ension loading i
22a01 73 20 64 69 73 61 62 6c 65 64 20 62 79 0a 2a 2a  s disabled by.**
22a02 20 64 65 66 61 75 6c 74 20 73 6f 20 61 73 20 6e   default so as n
22a03 6f 74 20 74 6f 20 6f 70 65 6e 20 73 65 63 75 72  ot to open secur
22a04 69 74 79 20 68 6f 6c 65 73 20 69 6e 20 6f 6c 64  ity holes in old
22a05 65 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e  er applications.
22a06 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
22a07 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c  nt sqlite3_enabl
22a08 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  e_load_extension
22a09 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
22a0a 74 20 6f 6e 6f 66 66 29 7b 0a 20 20 73 71 6c 69  t onoff){.  sqli
22a0b 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
22a0c 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66  db->mutex);.  if
22a0d 28 20 6f 6e 6f 66 66 20 29 7b 0a 20 20 20 20 64  ( onoff ){.    d
22a0e 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49  b->flags |= SQLI
22a0f 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e  TE_LoadExtension
22a10 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64  ;.  }else{.    d
22a11 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c  b->flags &= ~SQL
22a12 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f  ITE_LoadExtensio
22a13 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  n;.  }.  sqlite3
22a14 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
22a15 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  >mutex);.  retur
22a16 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
22a17 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
22a18 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e  _OMIT_LOAD_EXTEN
22a19 53 49 4f 4e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  SION */../*.** T
22a1a 68 65 20 61 75 74 6f 2d 65 78 74 65 6e 73 69 6f  he auto-extensio
22a1b 6e 20 63 6f 64 65 20 61 64 64 65 64 20 72 65 67  n code added reg
22a1c 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68  ardless of wheth
22a1d 65 72 20 6f 72 20 6e 6f 74 20 65 78 74 65 6e 73  er or not extens
22a1e 69 6f 6e 0a 2a 2a 20 6c 6f 61 64 69 6e 67 20 69  ion.** loading i
22a1f 73 20 73 75 70 70 6f 72 74 65 64 2e 20 20 57 65  s supported.  We
22a20 20 6e 65 65 64 20 61 20 64 75 6d 6d 79 20 73 71   need a dummy sq
22a21 6c 69 74 65 33 41 70 69 73 20 70 6f 69 6e 74 65  lite3Apis pointe
22a22 72 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f  r for that.** co
22a23 64 65 20 69 66 20 72 65 67 75 6c 61 72 20 65 78  de if regular ex
22a24 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20  tension loading 
22a25 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65  is not available
22a26 2e 20 20 54 68 69 73 20 69 73 20 74 68 61 74 0a  .  This is that.
22a27 2a 2a 20 64 75 6d 6d 79 20 70 6f 69 6e 74 65 72  ** dummy pointer
22a28 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
22a29 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
22a2a 45 4e 53 49 4f 4e 0a 73 74 61 74 69 63 20 63 6f  ENSION.static co
22a2b 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f  nst sqlite3_api_
22a2c 72 6f 75 74 69 6e 65 73 20 73 71 6c 69 74 65 33  routines sqlite3
22a2d 41 70 69 73 20 3d 20 7b 20 30 20 7d 3b 0a 23 65  Apis = { 0 };.#e
22a2e 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ndif.../*.** The
22a2f 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 62 6a 65 63   following objec
22a30 74 20 68 6f 6c 64 73 20 74 68 65 20 6c 69 73 74  t holds the list
22a31 20 6f 66 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   of automaticall
22a32 79 20 6c 6f 61 64 65 64 0a 2a 2a 20 65 78 74 65  y loaded.** exte
22a33 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  nsions..**.** Th
22a34 69 73 20 6c 69 73 74 20 69 73 20 73 68 61 72 65  is list is share
22a35 64 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 73  d across threads
22a36 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 4d 55  .  The SQLITE_MU
22a37 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
22a38 52 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20  R.** mutex must 
22a39 62 65 20 68 65 6c 64 20 77 68 69 6c 65 20 61 63  be held while ac
22a3a 63 65 73 73 69 6e 67 20 74 68 69 73 20 6c 69 73  cessing this lis
22a3b 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  t..*/.typedef st
22a3c 72 75 63 74 20 73 71 6c 69 74 65 33 45 78 74 54  ruct sqlite3ExtT
22a3d 79 70 65 20 73 71 6c 69 74 65 33 45 78 74 54 79  ype sqlite3ExtTy
22a3e 70 65 3b 0a 73 74 61 74 69 63 20 53 51 4c 49 54  pe;.static SQLIT
22a3f 45 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 6c  E_WSD struct sql
22a40 69 74 65 33 45 78 74 54 79 70 65 20 7b 0a 20 20  ite3ExtType {.  
22a41 69 6e 74 20 6e 45 78 74 3b 20 20 20 20 20 20 20  int nExt;       
22a42 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
22a43 74 72 69 65 73 20 69 6e 20 61 45 78 74 5b 5d 20  tries in aExt[] 
22a44 2a 2f 20 20 20 20 20 20 20 20 20 20 0a 20 20 76  */          .  v
22a45 6f 69 64 20 2a 2a 61 45 78 74 3b 20 20 20 20 20  oid **aExt;     
22a46 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74  /* Pointers to t
22a47 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 69  he extension ini
22a48 74 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 7d  t functions */.}
22a49 20 73 71 6c 69 74 65 33 41 75 74 6f 65 78 74 20   sqlite3Autoext 
22a4a 3d 20 7b 20 30 2c 20 30 20 7d 3b 0a 0a 2f 2a 20  = { 0, 0 };../* 
22a4b 54 68 65 20 22 77 73 64 41 75 74 6f 65 78 74 22  The "wsdAutoext"
22a4c 20 6d 61 63 72 6f 20 77 69 6c 6c 20 72 65 73 6f   macro will reso
22a4d 6c 76 65 20 74 6f 20 74 68 65 20 61 75 74 6f 65  lve to the autoe
22a4e 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 73 74 61 74  xtension.** stat
22a4f 65 20 76 65 63 74 6f 72 2e 20 20 49 66 20 77 72  e vector.  If wr
22a50 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61  itable static da
22a51 74 61 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65  ta is unsupporte
22a52 64 20 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c  d on the target,
22a53 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c  .** we have to l
22a54 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 65 20  ocate the state 
22a55 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69  vector at run-ti
22a56 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65  me.  In the more
22a57 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20   common.** case 
22a58 77 68 65 72 65 20 77 72 69 74 61 62 6c 65 20 73  where writable s
22a59 74 61 74 69 63 20 64 61 74 61 20 69 73 20 73 75  tatic data is su
22a5a 70 70 6f 72 74 65 64 2c 20 77 73 64 53 74 61 74  pported, wsdStat
22a5b 20 63 61 6e 20 72 65 66 65 72 20 64 69 72 65 63   can refer direc
22a5c 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22 73  tly.** to the "s
22a5d 71 6c 69 74 65 33 41 75 74 6f 65 78 74 22 20 73  qlite3Autoext" s
22a5e 74 61 74 65 20 76 65 63 74 6f 72 20 64 65 63 6c  tate vector decl
22a5f 61 72 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23  ared above..*/.#
22a60 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
22a61 54 5f 57 53 44 0a 23 20 64 65 66 69 6e 65 20 77  T_WSD.# define w
22a62 73 64 41 75 74 6f 65 78 74 49 6e 69 74 20 5c 0a  sdAutoextInit \.
22a63 20 20 73 71 6c 69 74 65 33 45 78 74 54 79 70 65    sqlite3ExtType
22a64 20 2a 78 20 3d 20 26 47 4c 4f 42 41 4c 28 73 71   *x = &GLOBAL(sq
22a65 6c 69 74 65 33 45 78 74 54 79 70 65 2c 73 71 6c  lite3ExtType,sql
22a66 69 74 65 33 41 75 74 6f 65 78 74 29 0a 23 20 64  ite3Autoext).# d
22a67 65 66 69 6e 65 20 77 73 64 41 75 74 6f 65 78 74  efine wsdAutoext
22a68 20 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65   x[0].#else.# de
22a69 66 69 6e 65 20 77 73 64 41 75 74 6f 65 78 74 49  fine wsdAutoextI
22a6a 6e 69 74 0a 23 20 64 65 66 69 6e 65 20 77 73 64  nit.# define wsd
22a6b 41 75 74 6f 65 78 74 20 73 71 6c 69 74 65 33 41  Autoext sqlite3A
22a6c 75 74 6f 65 78 74 0a 23 65 6e 64 69 66 0a 0a 0a  utoext.#endif...
22a6d 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61  /*.** Register a
22a6e 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b   statically link
22a6f 65 64 20 65 78 74 65 6e 73 69 6f 6e 20 74 68 61  ed extension tha
22a70 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c  t is automatical
22a71 6c 79 0a 2a 2a 20 6c 6f 61 64 65 64 20 62 79 20  ly.** loaded by 
22a72 65 76 65 72 79 20 6e 65 77 20 64 61 74 61 62 61  every new databa
22a73 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a  se connection..*
22a74 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
22a75 20 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f 65 78   sqlite3_auto_ex
22a76 74 65 6e 73 69 6f 6e 28 76 6f 69 64 20 2a 78 49  tension(void *xI
22a77 6e 69 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  nit){.  int rc =
22a78 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 69 66 6e   SQLITE_OK;.#ifn
22a79 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
22a7a 41 55 54 4f 49 4e 49 54 0a 20 20 72 63 20 3d 20  AUTOINIT.  rc = 
22a7b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
22a7c 7a 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29  ze();.  if( rc )
22a7d 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
22a7e 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a  .  }else.#endif.
22a7f 20 20 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 23    {.    int i;.#
22a80 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
22a81 53 41 46 45 0a 20 20 20 20 73 71 6c 69 74 65 33  SAFE.    sqlite3
22a82 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20  _mutex *mutex = 
22a83 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
22a84 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
22a85 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23  TATIC_MASTER);.#
22a86 65 6e 64 69 66 0a 20 20 20 20 77 73 64 41 75 74  endif.    wsdAut
22a87 6f 65 78 74 49 6e 69 74 3b 0a 20 20 20 20 73 71  oextInit;.    sq
22a88 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
22a89 72 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 66 6f  r(mutex);.    fo
22a8a 72 28 69 3d 30 3b 20 69 3c 77 73 64 41 75 74 6f  r(i=0; i<wsdAuto
22a8b 65 78 74 2e 6e 45 78 74 3b 20 69 2b 2b 29 7b 0a  ext.nExt; i++){.
22a8c 20 20 20 20 20 20 69 66 28 20 77 73 64 41 75 74        if( wsdAut
22a8d 6f 65 78 74 2e 61 45 78 74 5b 69 5d 3d 3d 78 49  oext.aExt[i]==xI
22a8e 6e 69 74 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  nit ) break;.   
22a8f 20 7d 0a 20 20 20 20 69 66 28 20 69 3d 3d 77 73   }.    if( i==ws
22a90 64 41 75 74 6f 65 78 74 2e 6e 45 78 74 20 29 7b  dAutoext.nExt ){
22a91 0a 20 20 20 20 20 20 69 6e 74 20 6e 42 79 74 65  .      int nByte
22a92 20 3d 20 28 77 73 64 41 75 74 6f 65 78 74 2e 6e   = (wsdAutoext.n
22a93 45 78 74 2b 31 29 2a 73 69 7a 65 6f 66 28 77 73  Ext+1)*sizeof(ws
22a94 64 41 75 74 6f 65 78 74 2e 61 45 78 74 5b 30 5d  dAutoext.aExt[0]
22a95 29 3b 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 2a  );.      void **
22a96 61 4e 65 77 3b 0a 20 20 20 20 20 20 61 4e 65 77  aNew;.      aNew
22a97 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c   = sqlite3_reall
22a98 6f 63 28 77 73 64 41 75 74 6f 65 78 74 2e 61 45  oc(wsdAutoext.aE
22a99 78 74 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20  xt, nByte);.    
22a9a 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b    if( aNew==0 ){
22a9b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
22a9c 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
22a9d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
22a9e 20 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74   wsdAutoext.aExt
22a9f 20 3d 20 61 4e 65 77 3b 0a 20 20 20 20 20 20 20   = aNew;.       
22aa0 20 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74   wsdAutoext.aExt
22aa1 5b 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74  [wsdAutoext.nExt
22aa2 5d 20 3d 20 78 49 6e 69 74 3b 0a 20 20 20 20 20  ] = xInit;.     
22aa3 20 20 20 77 73 64 41 75 74 6f 65 78 74 2e 6e 45     wsdAutoext.nE
22aa4 78 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  xt++;.      }.  
22aa5 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
22aa6 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65  mutex_leave(mute
22aa7 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  x);.    assert( 
22aa8 28 72 63 26 30 78 66 66 29 3d 3d 72 63 20 29 3b  (rc&0xff)==rc );
22aa9 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
22aaa 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73    }.}../*.** Res
22aab 65 74 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63  et the automatic
22aac 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69   extension loadi
22aad 6e 67 20 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f  ng mechanism..*/
22aae 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
22aaf 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61   sqlite3_reset_a
22ab0 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f  uto_extension(vo
22ab1 69 64 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  id){.#ifndef SQL
22ab2 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49  ITE_OMIT_AUTOINI
22ab3 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  T.  if( sqlite3_
22ab4 69 6e 69 74 69 61 6c 69 7a 65 28 29 3d 3d 53 51  initialize()==SQ
22ab5 4c 49 54 45 5f 4f 4b 20 29 0a 23 65 6e 64 69 66  LITE_OK ).#endif
22ab6 0a 20 20 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f  .  {.#if SQLITE_
22ab7 54 48 52 45 41 44 53 41 46 45 0a 20 20 20 20 73  THREADSAFE.    s
22ab8 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
22ab9 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74  tex = sqlite3Mut
22aba 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
22abb 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
22abc 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  ER);.#endif.    
22abd 77 73 64 41 75 74 6f 65 78 74 49 6e 69 74 3b 0a  wsdAutoextInit;.
22abe 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
22abf 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a  x_enter(mutex);.
22ac0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
22ac1 28 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74  (wsdAutoext.aExt
22ac2 29 3b 0a 20 20 20 20 77 73 64 41 75 74 6f 65 78  );.    wsdAutoex
22ac3 74 2e 61 45 78 74 20 3d 20 30 3b 0a 20 20 20 20  t.aExt = 0;.    
22ac4 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74 20  wsdAutoext.nExt 
22ac5 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 0;.    sqlite3
22ac6 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74  _mutex_leave(mut
22ac7 65 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ex);.  }.}../*.*
22ac8 2a 20 4c 6f 61 64 20 61 6c 6c 20 61 75 74 6f 6d  * Load all autom
22ac9 61 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 73 2e  atic extensions.
22aca 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
22acb 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75  TE int sqlite3Au
22acc 74 6f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 73  toLoadExtensions
22acd 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
22ace 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 67 6f   int i;.  int go
22acf 20 3d 20 31 3b 0a 20 20 69 6e 74 20 72 63 20 3d   = 1;.  int rc =
22ad0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e   SQLITE_OK;.  in
22ad1 74 20 28 2a 78 49 6e 69 74 29 28 73 71 6c 69 74  t (*xInit)(sqlit
22ad2 65 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73 74  e3*,char**,const
22ad3 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75   sqlite3_api_rou
22ad4 74 69 6e 65 73 2a 29 3b 0a 0a 20 20 77 73 64 41  tines*);..  wsdA
22ad5 75 74 6f 65 78 74 49 6e 69 74 3b 0a 20 20 69 66  utoextInit;.  if
22ad6 28 20 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78  ( wsdAutoext.nEx
22ad7 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 43  t==0 ){.    /* C
22ad8 6f 6d 6d 6f 6e 20 63 61 73 65 3a 20 65 61 72 6c  ommon case: earl
22ad9 79 20 6f 75 74 20 77 69 74 68 6f 75 74 20 65 76  y out without ev
22ada 65 72 79 20 68 61 76 69 6e 67 20 74 6f 20 61 63  ery having to ac
22adb 71 75 69 72 65 20 61 20 6d 75 74 65 78 20 2a 2f  quire a mutex */
22adc 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
22add 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 66 6f 72  TE_OK;.  }.  for
22ade 28 69 3d 30 3b 20 67 6f 3b 20 69 2b 2b 29 7b 0a  (i=0; go; i++){.
22adf 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72 6d 73      char *zErrms
22ae0 67 20 3d 20 30 3b 0a 23 69 66 20 53 51 4c 49 54  g = 0;.#if SQLIT
22ae1 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 20  E_THREADSAFE.   
22ae2 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
22ae3 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d  mutex = sqlite3M
22ae4 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
22ae5 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
22ae6 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20  STER);.#endif.  
22ae7 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
22ae8 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20  enter(mutex);.  
22ae9 20 20 69 66 28 20 69 3e 3d 77 73 64 41 75 74 6f    if( i>=wsdAuto
22aea 65 78 74 2e 6e 45 78 74 20 29 7b 0a 20 20 20 20  ext.nExt ){.    
22aeb 20 20 78 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20    xInit = 0;.   
22aec 20 20 20 67 6f 20 3d 20 30 3b 0a 20 20 20 20 7d     go = 0;.    }
22aed 65 6c 73 65 7b 0a 20 20 20 20 20 20 78 49 6e 69  else{.      xIni
22aee 74 20 3d 20 28 69 6e 74 28 2a 29 28 73 71 6c 69  t = (int(*)(sqli
22aef 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73  te3*,char**,cons
22af0 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f  t sqlite3_api_ro
22af1 75 74 69 6e 65 73 2a 29 29 0a 20 20 20 20 20 20  utines*)).      
22af2 20 20 20 20 20 20 20 20 77 73 64 41 75 74 6f 65          wsdAutoe
22af3 78 74 2e 61 45 78 74 5b 69 5d 3b 0a 20 20 20 20  xt.aExt[i];.    
22af4 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  }.    sqlite3_mu
22af5 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29  tex_leave(mutex)
22af6 3b 0a 20 20 20 20 69 66 28 20 78 49 6e 69 74 20  ;.    if( xInit 
22af7 26 26 20 78 49 6e 69 74 28 64 62 2c 20 26 7a 45  && xInit(db, &zE
22af8 72 72 6d 73 67 2c 20 26 73 71 6c 69 74 65 33 41  rrmsg, &sqlite3A
22af9 70 69 73 29 20 29 7b 0a 20 20 20 20 20 20 73 71  pis) ){.      sq
22afa 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53  lite3Error(db, S
22afb 51 4c 49 54 45 5f 45 52 52 4f 52 2c 0a 20 20 20  QLITE_ERROR,.   
22afc 20 20 20 20 20 20 20 20 20 22 61 75 74 6f 6d 61           "automa
22afd 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f  tic extension lo
22afe 61 64 69 6e 67 20 66 61 69 6c 65 64 3a 20 25 73  ading failed: %s
22aff 22 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20  ", zErrmsg);.   
22b00 20 20 20 67 6f 20 3d 20 30 3b 0a 20 20 20 20 20     go = 0;.     
22b01 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
22b02 4f 52 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  OR;.      sqlite
22b03 33 5f 66 72 65 65 28 7a 45 72 72 6d 73 67 29 3b  3_free(zErrmsg);
22b04 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
22b05 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  urn rc;.}../****
22b06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
22b07 66 20 6c 6f 61 64 65 78 74 2e 63 20 2a 2a 2a 2a  f loadext.c ****
22b08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22b09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22b0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
22b0b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
22b0c 20 66 69 6c 65 20 70 72 61 67 6d 61 2e 63 20 2a   file pragma.c *
22b0d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22b0e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22b0f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
22b10 20 32 30 30 33 20 41 70 72 69 6c 20 36 0a 2a 2a   2003 April 6.**
22b11 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
22b12 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
22b13 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
22b14 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
22b15 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
22b16 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
22b17 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
22b18 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
22b19 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
22b1a 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
22b1b 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
22b1c 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
22b1d 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
22b1e 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
22b1f 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
22b20 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
22b21 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
22b22 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
22b23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22b24 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22b25 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22b26 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
22b27 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
22b28 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20  ns code used to 
22b29 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 50 52  implement the PR
22b2a 41 47 4d 41 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a  AGMA command..**
22b2b 0a 2a 2a 20 24 49 64 3a 20 70 72 61 67 6d 61 2e  .** $Id: pragma.
22b2c 63 2c 76 20 31 2e 31 39 32 20 32 30 30 38 2f 31  c,v 1.192 2008/1
22b2d 30 2f 33 31 20 31 30 3a 35 33 3a 32 33 20 64 61  0/31 10:53:23 da
22b2e 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a  nielk1977 Exp $.
22b2f 2a 2f 0a 0a 2f 2a 20 49 67 6e 6f 72 65 20 74 68  */../* Ignore th
22b30 69 73 20 77 68 6f 6c 65 20 66 69 6c 65 20 69 66  is whole file if
22b31 20 70 72 61 67 6d 61 73 20 61 72 65 20 64 69 73   pragmas are dis
22b32 61 62 6c 65 64 0a 2a 2f 0a 23 69 66 20 21 64 65  abled.*/.#if !de
22b33 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
22b34 54 5f 50 52 41 47 4d 41 29 20 26 26 20 21 64 65  T_PRAGMA) && !de
22b35 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
22b36 54 5f 50 41 52 53 45 52 29 0a 0a 2f 2a 0a 2a 2a  T_PARSER)../*.**
22b37 20 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 67   Interpret the g
22b38 69 76 65 6e 20 73 74 72 69 6e 67 20 61 73 20 61  iven string as a
22b39 20 73 61 66 65 74 79 20 6c 65 76 65 6c 2e 20 20   safety level.  
22b3a 52 65 74 75 72 6e 20 30 20 66 6f 72 20 4f 46 46  Return 0 for OFF
22b3b 2c 0a 2a 2a 20 31 20 66 6f 72 20 4f 4e 20 6f 72  ,.** 1 for ON or
22b3c 20 4e 4f 52 4d 41 4c 20 61 6e 64 20 32 20 66 6f   NORMAL and 2 fo
22b3d 72 20 46 55 4c 4c 2e 20 20 52 65 74 75 72 6e 20  r FULL.  Return 
22b3e 31 20 66 6f 72 20 61 6e 20 65 6d 70 74 79 20 6f  1 for an empty o
22b3f 72 20 0a 2a 2a 20 75 6e 72 65 63 6f 67 6e 69 7a  r .** unrecogniz
22b40 65 64 20 73 74 72 69 6e 67 20 61 72 67 75 6d 65  ed string argume
22b41 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74  nt..**.** Note t
22b42 68 61 74 20 74 68 65 20 76 61 6c 75 65 73 20 72  hat the values r
22b43 65 74 75 72 6e 65 64 20 61 72 65 20 6f 6e 65 20  eturned are one 
22b44 6c 65 73 73 20 74 68 61 74 20 74 68 65 20 76 61  less that the va
22b45 6c 75 65 73 20 74 68 61 74 0a 2a 2a 20 73 68 6f  lues that.** sho
22b46 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 69 6e  uld be passed in
22b47 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  to sqlite3BtreeS
22b48 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 29 2e  etSafetyLevel().
22b49 20 20 54 68 65 20 69 73 20 64 6f 6e 65 0a 2a 2a    The is done.**
22b4a 20 74 6f 20 73 75 70 70 6f 72 74 20 6c 65 67 61   to support lega
22b4b 63 79 20 53 51 4c 20 63 6f 64 65 2e 20 20 54 68  cy SQL code.  Th
22b4c 65 20 73 61 66 65 74 79 20 6c 65 76 65 6c 20 75  e safety level u
22b4d 73 65 64 20 74 6f 20 62 65 20 62 6f 6f 6c 65 61  sed to be boolea
22b4e 6e 0a 2a 2a 20 61 6e 64 20 6f 6c 64 65 72 20 73  n.** and older s
22b4f 63 72 69 70 74 73 20 6d 61 79 20 68 61 76 65 20  cripts may have 
22b50 75 73 65 64 20 6e 75 6d 62 65 72 73 20 30 20 66  used numbers 0 f
22b51 6f 72 20 4f 46 46 20 61 6e 64 20 31 20 66 6f 72  or OFF and 1 for
22b52 20 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69   ON..*/.static i
22b53 6e 74 20 67 65 74 53 61 66 65 74 79 4c 65 76 65  nt getSafetyLeve
22b54 6c 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29  l(const char *z)
22b55 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
22b56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
22b57 2a 20 31 32 33 34 35 36 37 38 39 20 31 32 33 34  * 123456789 1234
22b58 35 36 37 38 39 20 2a 2f 0a 20 20 73 74 61 74 69  56789 */.  stati
22b59 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 54 65  c const char zTe
22b5a 78 74 5b 5d 20 3d 20 22 6f 6e 6f 66 66 61 6c 73  xt[] = "onoffals
22b5b 65 79 65 73 74 72 75 65 66 75 6c 6c 22 3b 0a 20  eyestruefull";. 
22b5c 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38   static const u8
22b5d 20 69 4f 66 66 73 65 74 5b 5d 20 3d 20 7b 30 2c   iOffset[] = {0,
22b5e 20 31 2c 20 32 2c 20 34 2c 20 39 2c 20 31 32 2c   1, 2, 4, 9, 12,
22b5f 20 31 36 7d 3b 0a 20 20 73 74 61 74 69 63 20 63   16};.  static c
22b60 6f 6e 73 74 20 75 38 20 69 4c 65 6e 67 74 68 5b  onst u8 iLength[
22b61 5d 20 3d 20 7b 32 2c 20 32 2c 20 33 2c 20 35 2c  ] = {2, 2, 3, 5,
22b62 20 33 2c 20 34 2c 20 34 7d 3b 0a 20 20 73 74 61   3, 4, 4};.  sta
22b63 74 69 63 20 63 6f 6e 73 74 20 75 38 20 69 56 61  tic const u8 iVa
22b64 6c 75 65 5b 5d 20 3d 20 20 7b 31 2c 20 30 2c 20  lue[] =  {1, 0, 
22b65 30 2c 20 30 2c 20 31 2c 20 31 2c 20 32 7d 3b 0a  0, 0, 1, 1, 2};.
22b66 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 69 66    int i, n;.  if
22b67 28 20 69 73 64 69 67 69 74 28 2a 7a 29 20 29 7b  ( isdigit(*z) ){
22b68 0a 20 20 20 20 72 65 74 75 72 6e 20 61 74 6f 69  .    return atoi
22b69 28 7a 29 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20 73  (z);.  }.  n = s
22b6a 74 72 6c 65 6e 28 7a 29 3b 0a 20 20 66 6f 72 28  trlen(z);.  for(
22b6b 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 69 4c  i=0; i<sizeof(iL
22b6c 65 6e 67 74 68 29 3b 20 69 2b 2b 29 7b 0a 20 20  ength); i++){.  
22b6d 20 20 69 66 28 20 69 4c 65 6e 67 74 68 5b 69 5d    if( iLength[i]
22b6e 3d 3d 6e 20 26 26 20 73 71 6c 69 74 65 33 53 74  ==n && sqlite3St
22b6f 72 4e 49 43 6d 70 28 26 7a 54 65 78 74 5b 69 4f  rNICmp(&zText[iO
22b70 66 66 73 65 74 5b 69 5d 5d 2c 7a 2c 6e 29 3d 3d  ffset[i]],z,n)==
22b71 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
22b72 6e 20 69 56 61 6c 75 65 5b 69 5d 3b 0a 20 20 20  n iValue[i];.   
22b73 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
22b74 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  1;.}../*.** Inte
22b75 72 70 72 65 74 20 74 68 65 20 67 69 76 65 6e 20  rpret the given 
22b76 73 74 72 69 6e 67 20 61 73 20 61 20 62 6f 6f 6c  string as a bool
22b77 65 61 6e 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74  ean value..*/.st
22b78 61 74 69 63 20 69 6e 74 20 67 65 74 42 6f 6f 6c  atic int getBool
22b79 65 61 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ean(const char *
22b7a 7a 29 7b 0a 20 20 72 65 74 75 72 6e 20 67 65 74  z){.  return get
22b7b 53 61 66 65 74 79 4c 65 76 65 6c 28 7a 29 26 31  SafetyLevel(z)&1
22b7c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72  ;.}../*.** Inter
22b7d 70 72 65 74 20 74 68 65 20 67 69 76 65 6e 20 73  pret the given s
22b7e 74 72 69 6e 67 20 61 73 20 61 20 6c 6f 63 6b 69  tring as a locki
22b7f 6e 67 20 6d 6f 64 65 20 76 61 6c 75 65 2e 0a 2a  ng mode value..*
22b80 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
22b81 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 63 6f 6e 73  LockingMode(cons
22b82 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 66  t char *z){.  if
22b83 28 20 7a 20 29 7b 0a 20 20 20 20 69 66 28 20 30  ( z ){.    if( 0
22b84 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ==sqlite3StrICmp
22b85 28 7a 2c 20 22 65 78 63 6c 75 73 69 76 65 22 29  (z, "exclusive")
22b86 20 29 20 72 65 74 75 72 6e 20 50 41 47 45 52 5f   ) return PAGER_
22b87 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c  LOCKINGMODE_EXCL
22b88 55 53 49 56 45 3b 0a 20 20 20 20 69 66 28 20 30  USIVE;.    if( 0
22b89 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ==sqlite3StrICmp
22b8a 28 7a 2c 20 22 6e 6f 72 6d 61 6c 22 29 20 29 20  (z, "normal") ) 
22b8b 72 65 74 75 72 6e 20 50 41 47 45 52 5f 4c 4f 43  return PAGER_LOC
22b8c 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3b  KINGMODE_NORMAL;
22b8d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 50 41  .  }.  return PA
22b8e 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
22b8f 51 55 45 52 59 3b 0a 7d 0a 0a 23 69 66 6e 64 65  QUERY;.}..#ifnde
22b90 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
22b91 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 49  TOVACUUM./*.** I
22b92 6e 74 65 72 70 72 65 74 20 74 68 65 20 67 69 76  nterpret the giv
22b93 65 6e 20 73 74 72 69 6e 67 20 61 73 20 61 6e 20  en string as an 
22b94 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65  auto-vacuum mode
22b95 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   value..**.** Th
22b96 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 69  e following stri
22b97 6e 67 73 2c 20 22 6e 6f 6e 65 22 2c 20 22 66 75  ngs, "none", "fu
22b98 6c 6c 22 20 61 6e 64 20 22 69 6e 63 72 65 6d 65  ll" and "increme
22b99 6e 74 61 6c 22 20 61 72 65 20 0a 2a 2a 20 61 63  ntal" are .** ac
22b9a 63 65 70 74 61 62 6c 65 2c 20 61 73 20 61 72 65  ceptable, as are
22b9b 20 74 68 65 69 72 20 6e 75 6d 65 72 69 63 20 65   their numeric e
22b9c 71 75 69 76 61 6c 65 6e 74 73 3a 20 30 2c 20 31  quivalents: 0, 1
22b9d 20 61 6e 64 20 32 20 72 65 73 70 65 63 74 69 76   and 2 respectiv
22b9e 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ely..*/.static i
22b9f 6e 74 20 67 65 74 41 75 74 6f 56 61 63 75 75 6d  nt getAutoVacuum
22ba0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b  (const char *z){
22ba1 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
22ba2 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d  0==sqlite3StrICm
22ba3 70 28 7a 2c 20 22 6e 6f 6e 65 22 29 20 29 20 72  p(z, "none") ) r
22ba4 65 74 75 72 6e 20 42 54 52 45 45 5f 41 55 54 4f  eturn BTREE_AUTO
22ba5 56 41 43 55 55 4d 5f 4e 4f 4e 45 3b 0a 20 20 69  VACUUM_NONE;.  i
22ba6 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72  f( 0==sqlite3Str
22ba7 49 43 6d 70 28 7a 2c 20 22 66 75 6c 6c 22 29 20  ICmp(z, "full") 
22ba8 29 20 72 65 74 75 72 6e 20 42 54 52 45 45 5f 41  ) return BTREE_A
22ba9 55 54 4f 56 41 43 55 55 4d 5f 46 55 4c 4c 3b 0a  UTOVACUUM_FULL;.
22baa 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33    if( 0==sqlite3
22bab 53 74 72 49 43 6d 70 28 7a 2c 20 22 69 6e 63 72  StrICmp(z, "incr
22bac 65 6d 65 6e 74 61 6c 22 29 20 29 20 72 65 74 75  emental") ) retu
22bad 72 6e 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43  rn BTREE_AUTOVAC
22bae 55 55 4d 5f 49 4e 43 52 3b 0a 20 20 69 20 3d 20  UUM_INCR;.  i = 
22baf 61 74 6f 69 28 7a 29 3b 0a 20 20 72 65 74 75 72  atoi(z);.  retur
22bb0 6e 20 28 28 69 3e 3d 30 26 26 69 3c 3d 32 29 3f  n ((i>=0&&i<=2)?
22bb1 69 3a 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  i:0);.}.#endif /
22bb2 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  * ifndef SQLITE_
22bb3 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 20  OMIT_AUTOVACUUM 
22bb4 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
22bb5 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52  TE_OMIT_PAGER_PR
22bb6 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  AGMAS./*.** Inte
22bb7 72 70 72 65 74 20 74 68 65 20 67 69 76 65 6e 20  rpret the given 
22bb8 73 74 72 69 6e 67 20 61 73 20 61 20 74 65 6d 70  string as a temp
22bb9 20 64 62 20 6c 6f 63 61 74 69 6f 6e 2e 20 52 65   db location. Re
22bba 74 75 72 6e 20 31 20 66 6f 72 20 66 69 6c 65 0a  turn 1 for file.
22bbb 2a 2a 20 62 61 63 6b 65 64 20 74 65 6d 70 6f 72  ** backed tempor
22bbc 61 72 79 20 64 61 74 61 62 61 73 65 73 2c 20 32  ary databases, 2
22bbd 20 66 6f 72 20 74 68 65 20 52 65 64 2d 42 6c 61   for the Red-Bla
22bbe 63 6b 20 74 72 65 65 20 69 6e 20 6d 65 6d 6f 72  ck tree in memor
22bbf 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 61 6e  y database.** an
22bc0 64 20 30 20 74 6f 20 75 73 65 20 74 68 65 20 63  d 0 to use the c
22bc1 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 64 65 66 61  ompile-time defa
22bc2 75 6c 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ult..*/.static i
22bc3 6e 74 20 67 65 74 54 65 6d 70 53 74 6f 72 65 28  nt getTempStore(
22bc4 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a  const char *z){.
22bc5 20 20 69 66 28 20 7a 5b 30 5d 3e 3d 27 30 27 20    if( z[0]>='0' 
22bc6 26 26 20 7a 5b 30 5d 3c 3d 27 32 27 20 29 7b 0a  && z[0]<='2' ){.
22bc7 20 20 20 20 72 65 74 75 72 6e 20 7a 5b 30 5d 20      return z[0] 
22bc8 2d 20 27 30 27 3b 0a 20 20 7d 65 6c 73 65 20 69  - '0';.  }else i
22bc9 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
22bca 70 28 7a 2c 20 22 66 69 6c 65 22 29 3d 3d 30 20  p(z, "file")==0 
22bcb 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  ){.    return 1;
22bcc 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c  .  }else if( sql
22bcd 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22  ite3StrICmp(z, "
22bce 6d 65 6d 6f 72 79 22 29 3d 3d 30 20 29 7b 0a 20  memory")==0 ){. 
22bcf 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d     return 2;.  }
22bd0 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
22bd1 20 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66   0;.  }.}.#endif
22bd2 20 2f 2a 20 53 51 4c 49 54 45 5f 50 41 47 45 52   /* SQLITE_PAGER
22bd3 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66  _PRAGMAS */..#if
22bd4 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
22bd5 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 2f  _PAGER_PRAGMAS./
22bd6 2a 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 20  *.** Invalidate 
22bd7 74 65 6d 70 20 73 74 6f 72 61 67 65 2c 20 65 69  temp storage, ei
22bd8 74 68 65 72 20 77 68 65 6e 20 74 68 65 20 74 65  ther when the te
22bd9 6d 70 20 73 74 6f 72 61 67 65 20 69 73 20 63 68  mp storage is ch
22bda 61 6e 67 65 64 0a 2a 2a 20 66 72 6f 6d 20 64 65  anged.** from de
22bdb 66 61 75 6c 74 2c 20 6f 72 20 77 68 65 6e 20 27  fault, or when '
22bdc 66 69 6c 65 27 20 61 6e 64 20 74 68 65 20 74 65  file' and the te
22bdd 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f  mp_store_directo
22bde 72 79 20 68 61 73 20 63 68 61 6e 67 65 64 0a 2a  ry has changed.*
22bdf 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 76  /.static int inv
22be0 61 6c 69 64 61 74 65 54 65 6d 70 53 74 6f 72 61  alidateTempStora
22be1 67 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ge(Parse *pParse
22be2 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
22be3 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
22be4 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 31 5d 2e   if( db->aDb[1].
22be5 70 42 74 21 3d 30 20 29 7b 0a 20 20 20 20 69 66  pBt!=0 ){.    if
22be6 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69  ( !db->autoCommi
22be7 74 20 7c 7c 20 73 71 6c 69 74 65 33 42 74 72 65  t || sqlite3Btre
22be8 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28 64  eIsInReadTrans(d
22be9 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29 20 29  b->aDb[1].pBt) )
22bea 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
22beb 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
22bec 22 74 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61  "temporary stora
22bed 67 65 20 63 61 6e 6e 6f 74 20 62 65 20 63 68 61  ge cannot be cha
22bee 6e 67 65 64 20 22 0a 20 20 20 20 20 20 20 20 22  nged ".        "
22bef 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 20 74 72  from within a tr
22bf0 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20 20  ansaction");.   
22bf1 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
22bf2 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20  _ERROR;.    }.  
22bf3 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c    sqlite3BtreeCl
22bf4 6f 73 65 28 64 62 2d 3e 61 44 62 5b 31 5d 2e 70  ose(db->aDb[1].p
22bf5 42 74 29 3b 0a 20 20 20 20 64 62 2d 3e 61 44 62  Bt);.    db->aDb
22bf6 5b 31 5d 2e 70 42 74 20 3d 20 30 3b 0a 20 20 20  [1].pBt = 0;.   
22bf7 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
22bf8 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20  ernalSchema(db, 
22bf9 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  0);.  }.  return
22bfa 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65   SQLITE_OK;.}.#e
22bfb 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 50  ndif /* SQLITE_P
22bfc 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f 0a  AGER_PRAGMAS */.
22bfd 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
22bfe 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d  OMIT_PAGER_PRAGM
22bff 41 53 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  AS./*.** If the 
22c00 54 45 4d 50 20 64 61 74 61 62 61 73 65 20 69 73  TEMP database is
22c01 20 6f 70 65 6e 2c 20 63 6c 6f 73 65 20 69 74 20   open, close it 
22c02 61 6e 64 20 6d 61 72 6b 20 74 68 65 20 64 61 74  and mark the dat
22c03 61 62 61 73 65 20 73 63 68 65 6d 61 0a 2a 2a 20  abase schema.** 
22c04 61 73 20 6e 65 65 64 69 6e 67 20 72 65 6c 6f 61  as needing reloa
22c05 64 69 6e 67 2e 20 20 54 68 69 73 20 6d 75 73 74  ding.  This must
22c06 20 62 65 20 64 6f 6e 65 20 77 68 65 6e 20 75 73   be done when us
22c07 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45 5f 54  ing the SQLITE_T
22c08 45 4d 50 5f 53 54 4f 52 45 0a 2a 2a 20 6f 72 20  EMP_STORE.** or 
22c09 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f 53 54 4f  DEFAULT_TEMP_STO
22c0a 52 45 20 70 72 61 67 6d 61 73 2e 0a 2a 2f 0a 73  RE pragmas..*/.s
22c0b 74 61 74 69 63 20 69 6e 74 20 63 68 61 6e 67 65  tatic int change
22c0c 54 65 6d 70 53 74 6f 72 61 67 65 28 50 61 72 73  TempStorage(Pars
22c0d 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74  e *pParse, const
22c0e 20 63 68 61 72 20 2a 7a 53 74 6f 72 61 67 65 54   char *zStorageT
22c0f 79 70 65 29 7b 0a 20 20 69 6e 74 20 74 73 20 3d  ype){.  int ts =
22c10 20 67 65 74 54 65 6d 70 53 74 6f 72 65 28 7a 53   getTempStore(zS
22c11 74 6f 72 61 67 65 54 79 70 65 29 3b 0a 20 20 73  torageType);.  s
22c12 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
22c13 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 64  rse->db;.  if( d
22c14 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 3d 3d 74  b->temp_store==t
22c15 73 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  s ) return SQLIT
22c16 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 6e 76 61  E_OK;.  if( inva
22c17 6c 69 64 61 74 65 54 65 6d 70 53 74 6f 72 61 67  lidateTempStorag
22c18 65 28 20 70 50 61 72 73 65 20 29 20 21 3d 20 53  e( pParse ) != S
22c19 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
22c1a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
22c1b 52 4f 52 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 74  ROR;.  }.  db->t
22c1c 65 6d 70 5f 73 74 6f 72 65 20 3d 20 74 73 3b 0a  emp_store = ts;.
22c1d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
22c1e 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  OK;.}.#endif /* 
22c1f 53 51 4c 49 54 45 5f 50 41 47 45 52 5f 50 52 41  SQLITE_PAGER_PRA
22c20 47 4d 41 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47  GMAS */../*.** G
22c21 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
22c22 72 65 74 75 72 6e 20 61 20 73 69 6e 67 6c 65 20  return a single 
22c23 69 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 0a 2a  integer value..*
22c24 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  /.static void re
22c25 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 50 61  turnSingleInt(Pa
22c26 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e  rse *pParse, con
22c27 73 74 20 63 68 61 72 20 2a 7a 4c 61 62 65 6c 2c  st char *zLabel,
22c28 20 69 6e 74 20 76 61 6c 75 65 29 7b 0a 20 20 56   int value){.  V
22c29 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33  dbe *v = sqlite3
22c2a 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
22c2b 0a 20 20 69 6e 74 20 6d 65 6d 20 3d 20 2b 2b 70  .  int mem = ++p
22c2c 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 73  Parse->nMem;.  s
22c2d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
22c2e 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
22c2f 76 61 6c 75 65 2c 20 6d 65 6d 29 3b 0a 20 20 69  value, mem);.  i
22c30 66 28 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61  f( pParse->expla
22c31 69 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  in==0 ){.    sql
22c32 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
22c33 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71  ls(v, 1);.    sq
22c34 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
22c35 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
22c36 45 5f 4e 41 4d 45 2c 20 7a 4c 61 62 65 6c 2c 20  E_NAME, zLabel, 
22c37 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
22c38 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
22c39 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
22c3a 73 75 6c 74 52 6f 77 2c 20 6d 65 6d 2c 20 31 29  sultRow, mem, 1)
22c3b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
22c3c 49 54 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50 52  ITE_OMIT_FLAG_PR
22c3d 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 43 68 65 63  AGMAS./*.** Chec
22c3e 6b 20 74 6f 20 73 65 65 20 69 66 20 7a 52 69 67  k to see if zRig
22c3f 68 74 20 61 6e 64 20 7a 4c 65 66 74 20 72 65 66  ht and zLeft ref
22c40 65 72 20 74 6f 20 61 20 70 72 61 67 6d 61 20 74  er to a pragma t
22c41 68 61 74 20 71 75 65 72 69 65 73 0a 2a 2a 20 6f  hat queries.** o
22c42 72 20 63 68 61 6e 67 65 73 20 6f 6e 65 20 6f 66  r changes one of
22c43 20 74 68 65 20 66 6c 61 67 73 20 69 6e 20 64 62   the flags in db
22c44 2d 3e 66 6c 61 67 73 2e 20 20 52 65 74 75 72 6e  ->flags.  Return
22c45 20 31 20 69 66 20 73 6f 20 61 6e 64 20 30 20 69   1 if so and 0 i
22c46 66 20 6e 6f 74 2e 0a 2a 2a 20 41 6c 73 6f 2c 20  f not..** Also, 
22c47 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 70 72  implement the pr
22c48 61 67 6d 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  agma..*/.static 
22c49 69 6e 74 20 66 6c 61 67 50 72 61 67 6d 61 28 50  int flagPragma(P
22c4a 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f  arse *pParse, co
22c4b 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74 2c  nst char *zLeft,
22c4c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 69   const char *zRi
22c4d 67 68 74 29 7b 0a 20 20 73 74 61 74 69 63 20 63  ght){.  static c
22c4e 6f 6e 73 74 20 73 74 72 75 63 74 20 73 50 72 61  onst struct sPra
22c4f 67 6d 61 54 79 70 65 20 7b 0a 20 20 20 20 63 6f  gmaType {.    co
22c50 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  nst char *zName;
22c51 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
22c52 20 70 72 61 67 6d 61 20 2a 2f 0a 20 20 20 20 69   pragma */.    i
22c53 6e 74 20 6d 61 73 6b 3b 20 20 20 20 20 20 20 20  nt mask;        
22c54 20 20 20 2f 2a 20 4d 61 73 6b 20 66 6f 72 20 74     /* Mask for t
22c55 68 65 20 64 62 2d 3e 66 6c 61 67 73 20 76 61 6c  he db->flags val
22c56 75 65 20 2a 2f 0a 20 20 7d 20 61 50 72 61 67 6d  ue */.  } aPragm
22c57 61 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22 66  a[] = {.    { "f
22c58 75 6c 6c 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 73  ull_column_names
22c59 22 2c 20 20 20 20 20 20 20 20 53 51 4c 49 54 45  ",        SQLITE
22c5a 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 20 20 7d  _FullColNames  }
22c5b 2c 0a 20 20 20 20 7b 20 22 73 68 6f 72 74 5f 63  ,.    { "short_c
22c5c 6f 6c 75 6d 6e 5f 6e 61 6d 65 73 22 2c 20 20 20  olumn_names",   
22c5d 20 20 20 20 53 51 4c 49 54 45 5f 53 68 6f 72 74      SQLITE_Short
22c5e 43 6f 6c 4e 61 6d 65 73 20 7d 2c 0a 20 20 20 20  ColNames },.    
22c5f 7b 20 22 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73  { "count_changes
22c60 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 53 51  ",            SQ
22c61 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 20  LITE_CountRows  
22c62 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 65 6d 70     },.    { "emp
22c63 74 79 5f 72 65 73 75 6c 74 5f 63 61 6c 6c 62 61  ty_result_callba
22c64 63 6b 73 22 2c 20 20 20 53 51 4c 49 54 45 5f 4e  cks",   SQLITE_N
22c65 75 6c 6c 43 61 6c 6c 62 61 63 6b 20 20 7d 2c 0a  ullCallback  },.
22c66 20 20 20 20 7b 20 22 6c 65 67 61 63 79 5f 66 69      { "legacy_fi
22c67 6c 65 5f 66 6f 72 6d 61 74 22 2c 20 20 20 20 20  le_format",     
22c68 20 20 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46    SQLITE_LegacyF
22c69 69 6c 65 46 6d 74 20 7d 2c 0a 20 20 20 20 7b 20  ileFmt },.    { 
22c6a 22 66 75 6c 6c 66 73 79 6e 63 22 2c 20 20 20 20  "fullfsync",    
22c6b 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
22c6c 54 45 5f 46 75 6c 6c 46 53 79 6e 63 20 20 20 20  TE_FullFSync    
22c6d 20 7d 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54   },.#ifdef SQLIT
22c6e 45 5f 44 45 42 55 47 0a 20 20 20 20 7b 20 22 73  E_DEBUG.    { "s
22c6f 71 6c 5f 74 72 61 63 65 22 2c 20 20 20 20 20 20  ql_trace",      
22c70 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
22c71 5f 53 71 6c 54 72 61 63 65 20 20 20 20 20 20 7d  _SqlTrace      }
22c72 2c 0a 20 20 20 20 7b 20 22 76 64 62 65 5f 6c 69  ,.    { "vdbe_li
22c73 73 74 69 6e 67 22 2c 20 20 20 20 20 20 20 20 20  sting",         
22c74 20 20 20 20 53 51 4c 49 54 45 5f 56 64 62 65 4c      SQLITE_VdbeL
22c75 69 73 74 69 6e 67 20 20 20 7d 2c 0a 20 20 20 20  isting   },.    
22c76 7b 20 22 76 64 62 65 5f 74 72 61 63 65 22 2c 20  { "vdbe_trace", 
22c77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
22c78 4c 49 54 45 5f 56 64 62 65 54 72 61 63 65 20 20  LITE_VdbeTrace  
22c79 20 20 20 7d 2c 0a 23 65 6e 64 69 66 0a 23 69 66     },.#endif.#if
22c7a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
22c7b 5f 43 48 45 43 4b 0a 20 20 20 20 7b 20 22 69 67  _CHECK.    { "ig
22c7c 6e 6f 72 65 5f 63 68 65 63 6b 5f 63 6f 6e 73 74  nore_check_const
22c7d 72 61 69 6e 74 73 22 2c 20 53 51 4c 49 54 45 5f  raints", SQLITE_
22c7e 49 67 6e 6f 72 65 43 68 65 63 6b 73 20 20 7d 2c  IgnoreChecks  },
22c7f 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f 2a 20 54  .#endif.    /* T
22c80 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20  he following is 
22c81 56 45 52 59 20 65 78 70 65 72 69 6d 65 6e 74 61  VERY experimenta
22c82 6c 20 2a 2f 0a 20 20 20 20 7b 20 22 77 72 69 74  l */.    { "writ
22c83 61 62 6c 65 5f 73 63 68 65 6d 61 22 2c 20 20 20  able_schema",   
22c84 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 57 72         SQLITE_Wr
22c85 69 74 65 53 63 68 65 6d 61 7c 53 51 4c 49 54 45  iteSchema|SQLITE
22c86 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 20 7d 2c  _RecoveryMode },
22c87 0a 20 20 20 20 7b 20 22 6f 6d 69 74 5f 72 65 61  .    { "omit_rea
22c88 64 6c 6f 63 6b 22 2c 20 20 20 20 20 20 20 20 20  dlock",         
22c89 20 20 20 53 51 4c 49 54 45 5f 4e 6f 52 65 61 64     SQLITE_NoRead
22c8a 6c 6f 63 6b 20 20 20 20 7d 2c 0a 0a 20 20 20 20  lock    },..    
22c8b 2f 2a 20 54 4f 44 4f 3a 20 4d 61 79 62 65 20 69  /* TODO: Maybe i
22c8c 74 20 73 68 6f 75 6c 64 6e 27 74 20 62 65 20 70  t shouldn't be p
22c8d 6f 73 73 69 62 6c 65 20 74 6f 20 63 68 61 6e 67  ossible to chang
22c8e 65 20 74 68 65 20 52 65 61 64 55 6e 63 6f 6d 6d  e the ReadUncomm
22c8f 69 74 74 65 64 0a 20 20 20 20 2a 2a 20 66 6c 61  itted.    ** fla
22c90 67 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61  g if there are a
22c91 6e 79 20 61 63 74 69 76 65 20 73 74 61 74 65 6d  ny active statem
22c92 65 6e 74 73 2e 20 2a 2f 0a 20 20 20 20 7b 20 22  ents. */.    { "
22c93 72 65 61 64 5f 75 6e 63 6f 6d 6d 69 74 74 65 64  read_uncommitted
22c94 22 2c 20 20 20 20 20 20 20 20 20 53 51 4c 49 54  ",         SQLIT
22c95 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65  E_ReadUncommitte
22c96 64 20 7d 2c 0a 20 20 7d 3b 0a 20 20 69 6e 74 20  d },.  };.  int 
22c97 69 3b 0a 20 20 63 6f 6e 73 74 20 73 74 72 75 63  i;.  const struc
22c98 74 20 73 50 72 61 67 6d 61 54 79 70 65 20 2a 70  t sPragmaType *p
22c99 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 61  ;.  for(i=0, p=a
22c9a 50 72 61 67 6d 61 3b 20 69 3c 73 69 7a 65 6f 66  Pragma; i<sizeof
22c9b 28 61 50 72 61 67 6d 61 29 2f 73 69 7a 65 6f 66  (aPragma)/sizeof
22c9c 28 61 50 72 61 67 6d 61 5b 30 5d 29 3b 20 69 2b  (aPragma[0]); i+
22c9d 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28  +, p++){.    if(
22c9e 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
22c9f 7a 4c 65 66 74 2c 20 70 2d 3e 7a 4e 61 6d 65 29  zLeft, p->zName)
22ca0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
22ca1 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
22ca2 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 56 64 62  e->db;.      Vdb
22ca3 65 20 2a 76 3b 0a 20 20 20 20 20 20 76 20 3d 20  e *v;.      v = 
22ca4 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
22ca5 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 69 66  Parse);.      if
22ca6 28 20 76 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ( v ){.        i
22ca7 66 28 20 7a 52 69 67 68 74 3d 3d 30 20 29 7b 0a  f( zRight==0 ){.
22ca8 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
22ca9 53 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65  SingleInt(pParse
22caa 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 28 64 62 2d  , p->zName, (db-
22cab 3e 66 6c 61 67 73 20 26 20 70 2d 3e 6d 61 73 6b  >flags & p->mask
22cac 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  )!=0 );.        
22cad 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
22cae 20 69 66 28 20 67 65 74 42 6f 6f 6c 65 61 6e 28   if( getBoolean(
22caf 7a 52 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20  zRight) ){.     
22cb0 20 20 20 20 20 20 20 64 62 2d 3e 66 6c 61 67 73         db->flags
22cb1 20 7c 3d 20 70 2d 3e 6d 61 73 6b 3b 0a 20 20 20   |= p->mask;.   
22cb2 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
22cb3 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 66 6c            db->fl
22cb4 61 67 73 20 26 3d 20 7e 70 2d 3e 6d 61 73 6b 3b  ags &= ~p->mask;
22cb5 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  .          }..  
22cb6 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6e 79 20          /* Many 
22cb7 6f 66 20 74 68 65 20 66 6c 61 67 2d 70 72 61 67  of the flag-prag
22cb8 6d 61 73 20 6d 6f 64 69 66 79 20 74 68 65 20 63  mas modify the c
22cb9 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 62 79  ode generated by
22cba 20 74 68 65 20 53 51 4c 20 0a 20 20 20 20 20 20   the SQL .      
22cbb 20 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 72 20      ** compiler 
22cbc 28 65 67 2e 20 63 6f 75 6e 74 5f 63 68 61 6e 67  (eg. count_chang
22cbd 65 73 29 2e 20 53 6f 20 61 64 64 20 61 6e 20 6f  es). So add an o
22cbe 70 63 6f 64 65 20 74 6f 20 65 78 70 69 72 65 20  pcode to expire 
22cbf 61 6c 6c 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  all.          **
22cc0 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74   compiled SQL st
22cc1 61 74 65 6d 65 6e 74 73 20 61 66 74 65 72 20 6d  atements after m
22cc2 6f 64 69 66 79 69 6e 67 20 61 20 70 72 61 67 6d  odifying a pragm
22cc3 61 20 76 61 6c 75 65 2e 0a 20 20 20 20 20 20 20  a value..       
22cc4 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
22cc5 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22cc6 32 28 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20  2(v, OP_Expire, 
22cc7 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d  0, 0);.        }
22cc8 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
22cc9 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
22cca 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
22ccb 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
22ccc 54 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41  TE_OMIT_FLAG_PRA
22ccd 47 4d 41 53 20 2a 2f 0a 0a 73 74 61 74 69 63 20  GMAS */..static 
22cce 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 63 74 69  const char *acti
22ccf 6f 6e 4e 61 6d 65 28 75 38 20 61 63 74 69 6f 6e  onName(u8 action
22cd0 29 7b 0a 20 20 73 77 69 74 63 68 28 20 61 63 74  ){.  switch( act
22cd1 69 6f 6e 20 29 7b 0a 20 20 20 20 63 61 73 65 20  ion ){.    case 
22cd2 4f 45 5f 53 65 74 4e 75 6c 6c 3a 20 20 72 65 74  OE_SetNull:  ret
22cd3 75 72 6e 20 22 53 45 54 20 4e 55 4c 4c 22 3b 0a  urn "SET NULL";.
22cd4 20 20 20 20 63 61 73 65 20 4f 45 5f 53 65 74 44      case OE_SetD
22cd5 66 6c 74 3a 20 20 72 65 74 75 72 6e 20 22 53 45  flt:  return "SE
22cd6 54 20 44 45 46 41 55 4c 54 22 3b 0a 20 20 20 20  T DEFAULT";.    
22cd7 63 61 73 65 20 4f 45 5f 52 65 73 74 72 69 63 74  case OE_Restrict
22cd8 3a 20 72 65 74 75 72 6e 20 22 52 45 53 54 52 49  : return "RESTRI
22cd9 43 54 22 3b 0a 20 20 20 20 63 61 73 65 20 4f 45  CT";.    case OE
22cda 5f 43 61 73 63 61 64 65 3a 20 20 72 65 74 75 72  _Cascade:  retur
22cdb 6e 20 22 43 41 53 43 41 44 45 22 3b 0a 20 20 7d  n "CASCADE";.  }
22cdc 0a 20 20 72 65 74 75 72 6e 20 22 22 3b 0a 7d 0a  .  return "";.}.
22cdd 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 61  ./*.** Process a
22cde 20 70 72 61 67 6d 61 20 73 74 61 74 65 6d 65 6e   pragma statemen
22cdf 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 50 72 61 67 6d  t.  .**.** Pragm
22ce0 61 73 20 61 72 65 20 6f 66 20 74 68 69 73 20 66  as are of this f
22ce1 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  orm:.**.**      
22ce2 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
22ce3 2e 5d 69 64 20 5b 3d 20 76 61 6c 75 65 5d 0a 2a  .]id [= value].*
22ce4 2a 0a 2a 2a 20 54 68 65 20 69 64 65 6e 74 69 66  *.** The identif
22ce5 69 65 72 20 6d 69 67 68 74 20 61 6c 73 6f 20 62  ier might also b
22ce6 65 20 61 20 73 74 72 69 6e 67 2e 20 20 54 68 65  e a string.  The
22ce7 20 76 61 6c 75 65 20 69 73 20 61 20 73 74 72 69   value is a stri
22ce8 6e 67 2c 20 61 6e 64 0a 2a 2a 20 69 64 65 6e 74  ng, and.** ident
22ce9 69 66 69 65 72 2c 20 6f 72 20 61 20 6e 75 6d 62  ifier, or a numb
22cea 65 72 2e 20 20 49 66 20 6d 69 6e 75 73 46 6c 61  er.  If minusFla
22ceb 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20  g is true, then 
22cec 74 68 65 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20  the value is.** 
22ced 61 20 6e 75 6d 62 65 72 20 74 68 61 74 20 77 61  a number that wa
22cee 73 20 70 72 65 63 65 64 65 64 20 62 79 20 61 20  s preceded by a 
22cef 6d 69 6e 75 73 20 73 69 67 6e 2e 0a 2a 2a 0a 2a  minus sign..**.*
22cf0 2a 20 49 66 20 74 68 65 20 6c 65 66 74 20 73 69  * If the left si
22cf1 64 65 20 69 73 20 22 64 61 74 61 62 61 73 65 2e  de is "database.
22cf2 69 64 22 20 74 68 65 6e 20 70 49 64 31 20 69 73  id" then pId1 is
22cf3 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61   the database na
22cf4 6d 65 0a 2a 2a 20 61 6e 64 20 70 49 64 32 20 69  me.** and pId2 i
22cf5 73 20 74 68 65 20 69 64 2e 20 20 49 66 20 74 68  s the id.  If th
22cf6 65 20 6c 65 66 74 20 73 69 64 65 20 69 73 20 6a  e left side is j
22cf7 75 73 74 20 22 69 64 22 20 74 68 65 6e 20 70 49  ust "id" then pI
22cf8 64 31 20 69 73 20 74 68 65 0a 2a 2a 20 69 64 20  d1 is the.** id 
22cf9 61 6e 64 20 70 49 64 32 20 69 73 20 61 6e 79 20  and pId2 is any 
22cfa 65 6d 70 74 79 20 73 74 72 69 6e 67 2e 0a 2a 2f  empty string..*/
22cfb 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
22cfc 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 61 67  void sqlite3Prag
22cfd 6d 61 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ma(.  Parse *pPa
22cfe 72 73 65 2c 20 0a 20 20 54 6f 6b 65 6e 20 2a 70  rse, .  Token *p
22cff 49 64 31 2c 20 20 20 20 20 20 20 20 2f 2a 20 46  Id1,        /* F
22d00 69 72 73 74 20 70 61 72 74 20 6f 66 20 5b 64 61  irst part of [da
22d01 74 61 62 61 73 65 2e 5d 69 64 20 66 69 65 6c 64  tabase.]id field
22d02 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 49 64   */.  Token *pId
22d03 32 2c 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63  2,        /* Sec
22d04 6f 6e 64 20 70 61 72 74 20 6f 66 20 5b 64 61 74  ond part of [dat
22d05 61 62 61 73 65 2e 5d 69 64 20 66 69 65 6c 64 2c  abase.]id field,
22d06 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f   or NULL */.  To
22d07 6b 65 6e 20 2a 70 56 61 6c 75 65 2c 20 20 20 20  ken *pValue,    
22d08 20 20 2f 2a 20 54 6f 6b 65 6e 20 66 6f 72 20 3c    /* Token for <
22d09 76 61 6c 75 65 3e 2c 20 6f 72 20 4e 55 4c 4c 20  value>, or NULL 
22d0a 2a 2f 0a 20 20 69 6e 74 20 6d 69 6e 75 73 46 6c  */.  int minusFl
22d0b 61 67 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ag       /* True
22d0c 20 69 66 20 61 20 27 2d 27 20 73 69 67 6e 20 70   if a '-' sign p
22d0d 72 65 63 65 64 65 64 20 3c 76 61 6c 75 65 3e 20  receded <value> 
22d0e 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4c  */.){.  char *zL
22d0f 65 66 74 20 3d 20 30 3b 20 20 20 20 20 20 20 2f  eft = 0;       /
22d10 2a 20 4e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64  * Nul-terminated
22d11 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 3c 69   UTF-8 string <i
22d12 64 3e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 52  d> */.  char *zR
22d13 69 67 68 74 20 3d 20 30 3b 20 20 20 20 20 20 2f  ight = 0;      /
22d14 2a 20 4e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64  * Nul-terminated
22d15 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 3c 76   UTF-8 string <v
22d16 61 6c 75 65 3e 2c 20 6f 72 20 4e 55 4c 4c 20 2a  alue>, or NULL *
22d17 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
22d18 7a 44 62 20 3d 20 30 3b 20 20 20 2f 2a 20 54 68  zDb = 0;   /* Th
22d19 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  e database name 
22d1a 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 49 64 3b  */.  Token *pId;
22d1b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
22d1c 6f 69 6e 74 65 72 20 74 6f 20 3c 69 64 3e 20 74  ointer to <id> t
22d1d 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44  oken */.  int iD
22d1e 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
22d1f 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 6e 64   /* Database ind
22d20 65 78 20 66 6f 72 20 3c 64 61 74 61 62 61 73 65  ex for <database
22d21 3e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  > */.  sqlite3 *
22d22 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
22d23 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 56 64  .  Db *pDb;.  Vd
22d24 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
22d25 70 56 64 62 65 20 3d 20 73 71 6c 69 74 65 33 56  pVdbe = sqlite3V
22d26 64 62 65 43 72 65 61 74 65 28 64 62 29 3b 0a 20  dbeCreate(db);. 
22d27 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75   if( v==0 ) retu
22d28 72 6e 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d  rn;.  pParse->nM
22d29 65 6d 20 3d 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e  em = 2;..  /* In
22d2a 74 65 72 70 72 65 74 20 74 68 65 20 5b 64 61 74  terpret the [dat
22d2b 61 62 61 73 65 2e 5d 20 70 61 72 74 20 6f 66 20  abase.] part of 
22d2c 74 68 65 20 70 72 61 67 6d 61 20 73 74 61 74 65  the pragma state
22d2d 6d 65 6e 74 2e 20 69 44 62 20 69 73 20 74 68 65  ment. iDb is the
22d2e 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6f 66 20 74  .  ** index of t
22d2f 68 65 20 64 61 74 61 62 61 73 65 20 74 68 69 73  he database this
22d30 20 70 72 61 67 6d 61 20 69 73 20 62 65 69 6e 67   pragma is being
22d31 20 61 70 70 6c 69 65 64 20 74 6f 20 69 6e 20 64   applied to in d
22d32 62 2e 61 44 62 5b 5d 2e 20 2a 2f 0a 20 20 69 44  b.aDb[]. */.  iD
22d33 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61  b = sqlite3TwoPa
22d34 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70  rtName(pParse, p
22d35 49 64 31 2c 20 70 49 64 32 2c 20 26 70 49 64 29  Id1, pId2, &pId)
22d36 3b 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29 20  ;.  if( iDb<0 ) 
22d37 72 65 74 75 72 6e 3b 0a 20 20 70 44 62 20 3d 20  return;.  pDb = 
22d38 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a  &db->aDb[iDb];..
22d39 20 20 2f 2a 20 49 66 20 74 68 65 20 74 65 6d 70    /* If the temp
22d3a 20 64 61 74 61 62 61 73 65 20 68 61 73 20 62 65   database has be
22d3b 65 6e 20 65 78 70 6c 69 63 69 74 6c 79 20 6e 61  en explicitly na
22d3c 6d 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74  med as part of t
22d3d 68 65 20 0a 20 20 2a 2a 20 70 72 61 67 6d 61 2c  he .  ** pragma,
22d3e 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73   make sure it is
22d3f 20 6f 70 65 6e 2e 20 0a 20 20 2a 2f 0a 20 20 69   open. .  */.  i
22d40 66 28 20 69 44 62 3d 3d 31 20 26 26 20 73 71 6c  f( iDb==1 && sql
22d41 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 61  ite3OpenTempData
22d42 62 61 73 65 28 70 50 61 72 73 65 29 20 29 7b 0a  base(pParse) ){.
22d43 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
22d44 0a 20 20 7a 4c 65 66 74 20 3d 20 73 71 6c 69 74  .  zLeft = sqlit
22d45 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
22d46 64 62 2c 20 70 49 64 29 3b 0a 20 20 69 66 28 20  db, pId);.  if( 
22d47 21 7a 4c 65 66 74 20 29 20 72 65 74 75 72 6e 3b  !zLeft ) return;
22d48 0a 20 20 69 66 28 20 6d 69 6e 75 73 46 6c 61 67  .  if( minusFlag
22d49 20 29 7b 0a 20 20 20 20 7a 52 69 67 68 74 20 3d   ){.    zRight =
22d4a 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
22d4b 64 62 2c 20 22 2d 25 54 22 2c 20 70 56 61 6c 75  db, "-%T", pValu
22d4c 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  e);.  }else{.   
22d4d 20 7a 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65   zRight = sqlite
22d4e 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
22d4f 62 2c 20 70 56 61 6c 75 65 29 3b 0a 20 20 7d 0a  b, pValue);.  }.
22d50 0a 20 20 7a 44 62 20 3d 20 28 28 70 49 64 32 20  .  zDb = ((pId2 
22d51 26 26 20 70 49 64 32 2d 3e 6e 3e 30 29 3f 70 44  && pId2->n>0)?pD
22d52 62 2d 3e 7a 4e 61 6d 65 3a 30 29 3b 0a 20 20 69  b->zName:0);.  i
22d53 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
22d54 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
22d55 54 45 5f 50 52 41 47 4d 41 2c 20 7a 4c 65 66 74  TE_PRAGMA, zLeft
22d56 2c 20 7a 52 69 67 68 74 2c 20 7a 44 62 29 20 29  , zRight, zDb) )
22d57 7b 0a 20 20 20 20 67 6f 74 6f 20 70 72 61 67 6d  {.    goto pragm
22d58 61 5f 6f 75 74 3b 0a 20 20 7d 0a 20 0a 23 69 66  a_out;.  }. .#if
22d59 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
22d5a 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 20  _PAGER_PRAGMAS. 
22d5b 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41   /*.  **  PRAGMA
22d5c 20 5b 64 61 74 61 62 61 73 65 2e 5d 64 65 66 61   [database.]defa
22d5d 75 6c 74 5f 63 61 63 68 65 5f 73 69 7a 65 0a 20  ult_cache_size. 
22d5e 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
22d5f 61 62 61 73 65 2e 5d 64 65 66 61 75 6c 74 5f 63  abase.]default_c
22d60 61 63 68 65 5f 73 69 7a 65 3d 4e 0a 20 20 2a 2a  ache_size=N.  **
22d61 0a 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20  .  ** The first 
22d62 66 6f 72 6d 20 72 65 70 6f 72 74 73 20 74 68 65  form reports the
22d63 20 63 75 72 72 65 6e 74 20 70 65 72 73 69 73 74   current persist
22d64 65 6e 74 20 73 65 74 74 69 6e 67 20 66 6f 72 20  ent setting for 
22d65 74 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 63 61  the.  ** page ca
22d66 63 68 65 20 73 69 7a 65 2e 20 20 54 68 65 20 76  che size.  The v
22d67 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73  alue returned is
22d68 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   the maximum num
22d69 62 65 72 20 6f 66 0a 20 20 2a 2a 20 70 61 67 65  ber of.  ** page
22d6a 73 20 69 6e 20 74 68 65 20 70 61 67 65 20 63 61  s in the page ca
22d6b 63 68 65 2e 20 20 54 68 65 20 73 65 63 6f 6e 64  che.  The second
22d6c 20 66 6f 72 6d 20 73 65 74 73 20 62 6f 74 68 20   form sets both 
22d6d 74 68 65 20 63 75 72 72 65 6e 74 0a 20 20 2a 2a  the current.  **
22d6e 20 70 61 67 65 20 63 61 63 68 65 20 73 69 7a 65   page cache size
22d6f 20 76 61 6c 75 65 20 61 6e 64 20 74 68 65 20 70   value and the p
22d70 65 72 73 69 73 74 65 6e 74 20 70 61 67 65 20 63  ersistent page c
22d71 61 63 68 65 20 73 69 7a 65 20 76 61 6c 75 65 0a  ache size value.
22d72 20 20 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 74    ** stored in t
22d73 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
22d74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  **.  ** The 
22d75 64 65 66 61 75 6c 74 20 63 61 63 68 65 20 73 69  default cache si
22d76 7a 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  ze is stored in 
22d77 6d 65 74 61 2d 76 61 6c 75 65 20 32 20 6f 66 20  meta-value 2 of 
22d78 70 61 67 65 20 31 20 6f 66 20 74 68 65 0a 20 20  page 1 of the.  
22d79 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
22d7a 2e 20 20 54 68 65 20 63 61 63 68 65 20 73 69 7a  .  The cache siz
22d7b 65 20 69 73 20 61 63 74 75 61 6c 6c 79 20 74 68  e is actually th
22d7c 65 20 61 62 73 6f 6c 75 74 65 20 76 61 6c 75 65  e absolute value
22d7d 20 6f 66 0a 20 20 2a 2a 20 74 68 69 73 20 6d 65   of.  ** this me
22d7e 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 2e 20 20  mory location.  
22d7f 54 68 65 20 73 69 67 6e 20 6f 66 20 6d 65 74 61  The sign of meta
22d80 2d 76 61 6c 75 65 20 32 20 64 65 74 65 72 6d 69  -value 2 determi
22d81 6e 65 73 20 74 68 65 0a 20 20 2a 2a 20 73 79 6e  nes the.  ** syn
22d82 63 68 72 6f 6e 6f 75 73 20 73 65 74 74 69 6e 67  chronous setting
22d83 2e 20 20 41 20 6e 65 67 61 74 69 76 65 20 76 61  .  A negative va
22d84 6c 75 65 20 6d 65 61 6e 73 20 73 79 6e 63 68 72  lue means synchr
22d85 6f 6e 6f 75 73 20 69 73 20 6f 66 66 0a 20 20 2a  onous is off.  *
22d86 2a 20 61 6e 64 20 61 20 70 6f 73 69 74 69 76 65  * and a positive
22d87 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 73 79 6e   value means syn
22d88 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f 6e 2e 0a  chronous is on..
22d89 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
22d8a 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
22d8b 22 64 65 66 61 75 6c 74 5f 63 61 63 68 65 5f 73  "default_cache_s
22d8c 69 7a 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ize")==0 ){.    
22d8d 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62  static const Vdb
22d8e 65 4f 70 4c 69 73 74 20 67 65 74 43 61 63 68 65  eOpList getCache
22d8f 53 69 7a 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  Size[] = {.     
22d90 20 7b 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65   { OP_ReadCookie
22d91 2c 20 20 30 2c 20 31 2c 20 20 20 20 20 20 20 20  ,  0, 1,        
22d92 32 7d 2c 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20  2},  /* 0 */.   
22d93 20 20 20 7b 20 4f 50 5f 49 66 50 6f 73 2c 20 20     { OP_IfPos,  
22d94 20 20 20 20 20 31 2c 20 36 2c 20 20 20 20 20 20       1, 6,      
22d95 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50    0},.      { OP
22d96 5f 49 6e 74 65 67 65 72 2c 20 20 20 20 20 30 2c  _Integer,     0,
22d97 20 32 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20   2,        0},. 
22d98 20 20 20 20 20 7b 20 4f 50 5f 53 75 62 74 72 61       { OP_Subtra
22d99 63 74 2c 20 20 20 20 31 2c 20 32 2c 20 20 20 20  ct,    1, 2,    
22d9a 20 20 20 20 31 7d 2c 0a 20 20 20 20 20 20 7b 20      1},.      { 
22d9b 4f 50 5f 49 66 50 6f 73 2c 20 20 20 20 20 20 20  OP_IfPos,       
22d9c 31 2c 20 36 2c 20 20 20 20 20 20 20 20 30 7d 2c  1, 6,        0},
22d9d 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49 6e 74 65  .      { OP_Inte
22d9e 67 65 72 2c 20 20 20 20 20 30 2c 20 31 2c 20 20  ger,     0, 1,  
22d9f 20 20 20 20 20 20 30 7d 2c 20 20 2f 2a 20 35 20        0},  /* 5 
22da0 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 52 65  */.      { OP_Re
22da1 73 75 6c 74 52 6f 77 2c 20 20 20 31 2c 20 31 2c  sultRow,   1, 1,
22da2 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 20          0},.    
22da3 7d 3b 0a 20 20 20 20 69 6e 74 20 61 64 64 72 3b  };.    int addr;
22da4 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
22da5 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
22da6 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61  e) ) goto pragma
22da7 5f 6f 75 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  _out;.    sqlite
22da8 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76  3VdbeUsesBtree(v
22da9 2c 20 69 44 62 29 3b 0a 20 20 20 20 69 66 28 20  , iDb);.    if( 
22daa 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  !zRight ){.     
22dab 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
22dac 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20  umCols(v, 1);.  
22dad 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
22dae 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
22daf 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 63  COLNAME_NAME, "c
22db0 61 63 68 65 5f 73 69 7a 65 22 2c 20 53 51 4c 49  ache_size", SQLI
22db1 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
22db2 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b    pParse->nMem +
22db3 3d 20 32 3b 0a 20 20 20 20 20 20 61 64 64 72 20  = 2;.      addr 
22db4 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
22db5 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53  OpList(v, ArrayS
22db6 69 7a 65 28 67 65 74 43 61 63 68 65 53 69 7a 65  ize(getCacheSize
22db7 29 2c 20 67 65 74 43 61 63 68 65 53 69 7a 65 29  ), getCacheSize)
22db8 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
22db9 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61  dbeChangeP1(v, a
22dba 64 64 72 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  ddr, iDb);.     
22dbb 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
22dbc 67 65 50 31 28 76 2c 20 61 64 64 72 2b 35 2c 20  geP1(v, addr+5, 
22dbd 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43  SQLITE_DEFAULT_C
22dbe 41 43 48 45 5f 53 49 5a 45 29 3b 0a 20 20 20 20  ACHE_SIZE);.    
22dbf 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74  }else{.      int
22dc0 20 73 69 7a 65 20 3d 20 61 74 6f 69 28 7a 52 69   size = atoi(zRi
22dc1 67 68 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ght);.      if( 
22dc2 73 69 7a 65 3c 30 20 29 20 73 69 7a 65 20 3d 20  size<0 ) size = 
22dc3 2d 73 69 7a 65 3b 0a 20 20 20 20 20 20 73 71 6c  -size;.      sql
22dc4 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
22dc5 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
22dc6 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73  0, iDb);.      s
22dc7 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
22dc8 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
22dc9 73 69 7a 65 2c 20 31 29 3b 0a 20 20 20 20 20 20  size, 1);.      
22dca 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22dcb 33 28 76 2c 20 4f 50 5f 52 65 61 64 43 6f 6f 6b  3(v, OP_ReadCook
22dcc 69 65 2c 20 69 44 62 2c 20 32 2c 20 32 29 3b 0a  ie, iDb, 2, 2);.
22dcd 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
22dce 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
22dcf 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 32 2c 20 30  , OP_IfPos, 2, 0
22dd0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
22dd1 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
22dd2 5f 49 6e 74 65 67 65 72 2c 20 2d 73 69 7a 65 2c  _Integer, -size,
22dd3 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   1);.      sqlit
22dd4 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
22dd5 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20 73  , addr);.      s
22dd6 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
22dd7 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65  (v, OP_SetCookie
22dd8 2c 20 69 44 62 2c 20 32 2c 20 31 29 3b 0a 20 20  , iDb, 2, 1);.  
22dd9 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61      pDb->pSchema
22dda 2d 3e 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 73  ->cache_size = s
22ddb 69 7a 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ize;.      sqlit
22ddc 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53  e3BtreeSetCacheS
22ddd 69 7a 65 28 70 44 62 2d 3e 70 42 74 2c 20 70 44  ize(pDb->pBt, pD
22dde 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68  b->pSchema->cach
22ddf 65 5f 73 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20  e_size);.    }. 
22de0 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a   }else..  /*.  *
22de1 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62  *  PRAGMA [datab
22de2 61 73 65 2e 5d 70 61 67 65 5f 73 69 7a 65 0a 20  ase.]page_size. 
22de3 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
22de4 61 62 61 73 65 2e 5d 70 61 67 65 5f 73 69 7a 65  abase.]page_size
22de5 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  =N.  **.  ** The
22de6 20 66 69 72 73 74 20 66 6f 72 6d 20 72 65 70 6f   first form repo
22de7 72 74 73 20 74 68 65 20 63 75 72 72 65 6e 74 20  rts the current 
22de8 73 65 74 74 69 6e 67 20 66 6f 72 20 74 68 65 0a  setting for the.
22de9 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61    ** database pa
22dea 67 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73  ge size in bytes
22deb 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 66 6f  .  The second fo
22dec 72 6d 20 73 65 74 73 20 74 68 65 0a 20 20 2a 2a  rm sets the.  **
22ded 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 73   database page s
22dee 69 7a 65 20 76 61 6c 75 65 2e 20 20 54 68 65 20  ize value.  The 
22def 76 61 6c 75 65 20 63 61 6e 20 6f 6e 6c 79 20 62  value can only b
22df0 65 20 73 65 74 20 69 66 0a 20 20 2a 2a 20 74 68  e set if.  ** th
22df1 65 20 64 61 74 61 62 61 73 65 20 68 61 73 20 6e  e database has n
22df2 6f 74 20 79 65 74 20 62 65 65 6e 20 63 72 65 61  ot yet been crea
22df3 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ted..  */.  if( 
22df4 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
22df5 4c 65 66 74 2c 22 70 61 67 65 5f 73 69 7a 65 22  Left,"page_size"
22df6 29 3d 3d 30 20 29 7b 0a 20 20 20 20 42 74 72 65  )==0 ){.    Btre
22df7 65 20 2a 70 42 74 20 3d 20 70 44 62 2d 3e 70 42  e *pBt = pDb->pB
22df8 74 3b 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67  t;.    if( !zRig
22df9 68 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  ht ){.      int 
22dfa 73 69 7a 65 20 3d 20 70 42 74 20 3f 20 73 71 6c  size = pBt ? sql
22dfb 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
22dfc 53 69 7a 65 28 70 42 74 29 20 3a 20 30 3b 0a 20  Size(pBt) : 0;. 
22dfd 20 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c       returnSingl
22dfe 65 49 6e 74 28 70 50 61 72 73 65 2c 20 22 70 61  eInt(pParse, "pa
22dff 67 65 5f 73 69 7a 65 22 2c 20 73 69 7a 65 29 3b  ge_size", size);
22e00 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
22e01 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 61 79 20    /* Malloc may 
22e02 66 61 69 6c 20 77 68 65 6e 20 73 65 74 74 69 6e  fail when settin
22e03 67 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 2c  g the page-size,
22e04 20 61 73 20 74 68 65 72 65 20 69 73 20 61 6e 20   as there is an 
22e05 69 6e 74 65 72 6e 61 6c 0a 20 20 20 20 20 20 2a  internal.      *
22e06 2a 20 62 75 66 66 65 72 20 74 68 61 74 20 74 68  * buffer that th
22e07 65 20 70 61 67 65 72 20 6d 6f 64 75 6c 65 20 72  e pager module r
22e08 65 73 69 7a 65 73 20 75 73 69 6e 67 20 73 71 6c  esizes using sql
22e09 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a  ite3_realloc()..
22e0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64        */.      d
22e0b 62 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a 65 20  b->nextPagesize 
22e0c 3d 20 61 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a  = atoi(zRight);.
22e0d 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 45        if( SQLITE
22e0e 5f 4e 4f 4d 45 4d 3d 3d 73 71 6c 69 74 65 33 42  _NOMEM==sqlite3B
22e0f 74 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28  treeSetPageSize(
22e10 70 42 74 2c 20 64 62 2d 3e 6e 65 78 74 50 61 67  pBt, db->nextPag
22e11 65 73 69 7a 65 2c 20 2d 31 29 20 29 7b 0a 20 20  esize, -1) ){.  
22e12 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63        db->malloc
22e13 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
22e14 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73    }.    }.  }els
22e15 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52  e..  /*.  **  PR
22e16 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
22e17 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 0a 20  max_page_count. 
22e18 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
22e19 61 62 61 73 65 2e 5d 6d 61 78 5f 70 61 67 65 5f  abase.]max_page_
22e1a 63 6f 75 6e 74 3d 4e 0a 20 20 2a 2a 0a 20 20 2a  count=N.  **.  *
22e1b 2a 20 54 68 65 20 66 69 72 73 74 20 66 6f 72 6d  * The first form
22e1c 20 72 65 70 6f 72 74 73 20 74 68 65 20 63 75 72   reports the cur
22e1d 72 65 6e 74 20 73 65 74 74 69 6e 67 20 66 6f 72  rent setting for
22e1e 20 74 68 65 0a 20 20 2a 2a 20 6d 61 78 69 6d 75   the.  ** maximu
22e1f 6d 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  m number of page
22e20 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  s in the databas
22e21 65 20 66 69 6c 65 2e 20 20 54 68 65 20 0a 20 20  e file.  The .  
22e22 2a 2a 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20 61  ** second form a
22e23 74 74 65 6d 70 74 73 20 74 6f 20 63 68 61 6e 67  ttempts to chang
22e24 65 20 74 68 69 73 20 73 65 74 74 69 6e 67 2e 20  e this setting. 
22e25 20 42 6f 74 68 0a 20 20 2a 2a 20 66 6f 72 6d 73   Both.  ** forms
22e26 20 72 65 74 75 72 6e 20 74 68 65 20 63 75 72 72   return the curr
22e27 65 6e 74 20 73 65 74 74 69 6e 67 2e 0a 20 20 2a  ent setting..  *
22e28 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
22e29 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 6d 61  trICmp(zLeft,"ma
22e2a 78 5f 70 61 67 65 5f 63 6f 75 6e 74 22 29 3d 3d  x_page_count")==
22e2b 30 20 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a  0 ){.    Btree *
22e2c 70 42 74 20 3d 20 70 44 62 2d 3e 70 42 74 3b 0a  pBt = pDb->pBt;.
22e2d 20 20 20 20 69 6e 74 20 6e 65 77 4d 61 78 20 3d      int newMax =
22e2e 20 30 3b 0a 20 20 20 20 69 66 28 20 7a 52 69 67   0;.    if( zRig
22e2f 68 74 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4d  ht ){.      newM
22e30 61 78 20 3d 20 61 74 6f 69 28 7a 52 69 67 68 74  ax = atoi(zRight
22e31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
22e32 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 6e 65   pBt ){.      ne
22e33 77 4d 61 78 20 3d 20 73 71 6c 69 74 65 33 42 74  wMax = sqlite3Bt
22e34 72 65 65 4d 61 78 50 61 67 65 43 6f 75 6e 74 28  reeMaxPageCount(
22e35 70 42 74 2c 20 6e 65 77 4d 61 78 29 3b 0a 20 20  pBt, newMax);.  
22e36 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 53 69    }.    returnSi
22e37 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20  ngleInt(pParse, 
22e38 22 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 22  "max_page_count"
22e39 2c 20 6e 65 77 4d 61 78 29 3b 0a 20 20 7d 65 6c  , newMax);.  }el
22e3a 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50  se..  /*.  **  P
22e3b 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
22e3c 5d 70 61 67 65 5f 63 6f 75 6e 74 0a 20 20 2a 2a  ]page_count.  **
22e3d 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  .  ** Return the
22e3e 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
22e3f 20 69 6e 20 74 68 65 20 73 70 65 63 69 66 69 65   in the specifie
22e40 64 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f  d database..  */
22e41 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
22e42 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 70 61 67  rICmp(zLeft,"pag
22e43 65 5f 63 6f 75 6e 74 22 29 3d 3d 30 20 29 7b 0a  e_count")==0 ){.
22e44 20 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20      Vdbe *v;.   
22e45 20 69 6e 74 20 69 52 65 67 3b 0a 20 20 20 20 76   int iReg;.    v
22e46 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
22e47 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69  e(pParse);.    i
22e48 66 28 20 21 76 20 7c 7c 20 73 71 6c 69 74 65 33  f( !v || sqlite3
22e49 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
22e4a 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61  e) ) goto pragma
22e4b 5f 6f 75 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  _out;.    sqlite
22e4c 33 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d  3CodeVerifySchem
22e4d 61 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a  a(pParse, iDb);.
22e4e 20 20 20 20 69 52 65 67 20 3d 20 2b 2b 70 50 61      iReg = ++pPa
22e4f 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73  rse->nMem;.    s
22e50 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
22e51 28 76 2c 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74  (v, OP_Pagecount
22e52 2c 20 69 44 62 2c 20 69 52 65 67 29 3b 0a 20 20  , iDb, iReg);.  
22e53 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22e54 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
22e55 52 6f 77 2c 20 69 52 65 67 2c 20 31 29 3b 0a 20  Row, iReg, 1);. 
22e56 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
22e57 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a  tNumCols(v, 1);.
22e58 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
22e59 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
22e5a 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 70  COLNAME_NAME, "p
22e5b 61 67 65 5f 63 6f 75 6e 74 22 2c 20 53 51 4c 49  age_count", SQLI
22e5c 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 65  TE_STATIC);.  }e
22e5d 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20  lse..  /*.  **  
22e5e 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
22e5f 2e 5d 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 0a 20  .]locking_mode. 
22e60 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
22e61 61 62 61 73 65 2e 5d 6c 6f 63 6b 69 6e 67 5f 6d  abase.]locking_m
22e62 6f 64 65 20 3d 20 28 6e 6f 72 6d 61 6c 7c 65 78  ode = (normal|ex
22e63 63 6c 75 73 69 76 65 29 0a 20 20 2a 2f 0a 20 20  clusive).  */.  
22e64 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
22e65 6d 70 28 7a 4c 65 66 74 2c 22 6c 6f 63 6b 69 6e  mp(zLeft,"lockin
22e66 67 5f 6d 6f 64 65 22 29 3d 3d 30 20 29 7b 0a 20  g_mode")==0 ){. 
22e67 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
22e68 52 65 74 20 3d 20 22 6e 6f 72 6d 61 6c 22 3b 0a  Ret = "normal";.
22e69 20 20 20 20 69 6e 74 20 65 4d 6f 64 65 20 3d 20      int eMode = 
22e6a 67 65 74 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 7a  getLockingMode(z
22e6b 52 69 67 68 74 29 3b 0a 0a 20 20 20 20 69 66 28  Right);..    if(
22e6c 20 70 49 64 32 2d 3e 6e 3d 3d 30 20 26 26 20 65   pId2->n==0 && e
22e6d 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b  Mode==PAGER_LOCK
22e6e 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 20 29 7b  INGMODE_QUERY ){
22e6f 0a 20 20 20 20 20 20 2f 2a 20 53 69 6d 70 6c 65  .      /* Simple
22e70 20 22 50 52 41 47 4d 41 20 6c 6f 63 6b 69 6e 67   "PRAGMA locking
22e71 5f 6d 6f 64 65 3b 22 20 73 74 61 74 65 6d 65 6e  _mode;" statemen
22e72 74 2e 20 54 68 69 73 20 69 73 20 61 20 71 75 65  t. This is a que
22e73 72 79 20 66 6f 72 0a 20 20 20 20 20 20 2a 2a 20  ry for.      ** 
22e74 74 68 65 20 63 75 72 72 65 6e 74 20 64 65 66 61  the current defa
22e75 75 6c 74 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65  ult locking mode
22e76 20 28 77 68 69 63 68 20 6d 61 79 20 62 65 20 64   (which may be d
22e77 69 66 66 65 72 65 6e 74 20 74 6f 0a 20 20 20 20  ifferent to.    
22e78 20 20 2a 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67    ** the locking
22e79 2d 6d 6f 64 65 20 6f 66 20 74 68 65 20 6d 61 69  -mode of the mai
22e7a 6e 20 64 61 74 61 62 61 73 65 29 2e 0a 20 20 20  n database)..   
22e7b 20 20 20 2a 2f 0a 20 20 20 20 20 20 65 4d 6f 64     */.      eMod
22e7c 65 20 3d 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b  e = db->dfltLock
22e7d 4d 6f 64 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  Mode;.    }else{
22e7e 0a 20 20 20 20 20 20 50 61 67 65 72 20 2a 70 50  .      Pager *pP
22e7f 61 67 65 72 3b 0a 20 20 20 20 20 20 69 66 28 20  ager;.      if( 
22e80 70 49 64 32 2d 3e 6e 3d 3d 30 20 29 7b 0a 20 20  pId2->n==0 ){.  
22e81 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69 6e        /* This in
22e82 64 69 63 61 74 65 73 20 74 68 61 74 20 6e 6f 20  dicates that no 
22e83 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 77 61  database name wa
22e84 73 20 73 70 65 63 69 66 69 65 64 20 61 73 20 70  s specified as p
22e85 61 72 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f  art.        ** o
22e86 66 20 74 68 65 20 50 52 41 47 4d 41 20 63 6f 6d  f the PRAGMA com
22e87 6d 61 6e 64 2e 20 49 6e 20 74 68 69 73 20 63 61  mand. In this ca
22e88 73 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d  se the locking-m
22e89 6f 64 65 20 6d 75 73 74 20 62 65 0a 20 20 20 20  ode must be.    
22e8a 20 20 20 20 2a 2a 20 73 65 74 20 6f 6e 20 61 6c      ** set on al
22e8b 6c 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  l attached datab
22e8c 61 73 65 73 2c 20 61 73 20 77 65 6c 6c 20 61 73  ases, as well as
22e8d 20 74 68 65 20 6d 61 69 6e 20 64 62 20 66 69 6c   the main db fil
22e8e 65 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20  e..        **.  
22e8f 20 20 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 74        ** Also, t
22e90 68 65 20 73 71 6c 69 74 65 33 2e 64 66 6c 74 4c  he sqlite3.dfltL
22e91 6f 63 6b 4d 6f 64 65 20 76 61 72 69 61 62 6c 65  ockMode variable
22e92 20 69 73 20 73 65 74 20 73 6f 20 74 68 61 74 0a   is set so that.
22e93 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 79 20 73          ** any s
22e94 75 62 73 65 71 75 65 6e 74 6c 79 20 61 74 74 61  ubsequently atta
22e95 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20 61  ched databases a
22e96 6c 73 6f 20 75 73 65 20 74 68 65 20 73 70 65 63  lso use the spec
22e97 69 66 69 65 64 0a 20 20 20 20 20 20 20 20 2a 2a  ified.        **
22e98 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 2e 0a 20   locking mode.. 
22e99 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
22e9a 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20    int ii;.      
22e9b 20 20 61 73 73 65 72 74 28 70 44 62 3d 3d 26 64    assert(pDb==&d
22e9c 62 2d 3e 61 44 62 5b 30 5d 29 3b 0a 20 20 20 20  b->aDb[0]);.    
22e9d 20 20 20 20 66 6f 72 28 69 69 3d 32 3b 20 69 69      for(ii=2; ii
22e9e 3c 64 62 2d 3e 6e 44 62 3b 20 69 69 2b 2b 29 7b  <db->nDb; ii++){
22e9f 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65  .          pPage
22ea0 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  r = sqlite3Btree
22ea1 50 61 67 65 72 28 64 62 2d 3e 61 44 62 5b 69 69  Pager(db->aDb[ii
22ea2 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20  ].pBt);.        
22ea3 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f    sqlite3PagerLo
22ea4 63 6b 69 6e 67 4d 6f 64 65 28 70 50 61 67 65 72  ckingMode(pPager
22ea5 2c 20 65 4d 6f 64 65 29 3b 0a 20 20 20 20 20 20  , eMode);.      
22ea6 20 20 7d 0a 20 20 20 20 20 20 20 20 64 62 2d 3e    }.        db->
22ea7 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 20 3d 20 65  dfltLockMode = e
22ea8 4d 6f 64 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Mode;.      }.  
22ea9 20 20 20 20 70 50 61 67 65 72 20 3d 20 73 71 6c      pPager = sql
22eaa 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70  ite3BtreePager(p
22eab 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 20 20  Db->pBt);.      
22eac 65 4d 6f 64 65 20 3d 20 73 71 6c 69 74 65 33 50  eMode = sqlite3P
22ead 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28  agerLockingMode(
22eae 70 50 61 67 65 72 2c 20 65 4d 6f 64 65 29 3b 0a  pPager, eMode);.
22eaf 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 72      }..    asser
22eb0 74 28 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c  t(eMode==PAGER_L
22eb1 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41  OCKINGMODE_NORMA
22eb2 4c 7c 7c 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  L||eMode==PAGER_
22eb3 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c  LOCKINGMODE_EXCL
22eb4 55 53 49 56 45 29 3b 0a 20 20 20 20 69 66 28 20  USIVE);.    if( 
22eb5 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43  eMode==PAGER_LOC
22eb6 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49  KINGMODE_EXCLUSI
22eb7 56 45 20 29 7b 0a 20 20 20 20 20 20 7a 52 65 74  VE ){.      zRet
22eb8 20 3d 20 22 65 78 63 6c 75 73 69 76 65 22 3b 0a   = "exclusive";.
22eb9 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
22eba 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
22ebb 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  v, 1);.    sqlit
22ebc 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
22ebd 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
22ebe 41 4d 45 2c 20 22 6c 6f 63 6b 69 6e 67 5f 6d 6f  AME, "locking_mo
22ebf 64 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  de", SQLITE_STAT
22ec0 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  IC);.    sqlite3
22ec1 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
22ec2 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 31 2c 20  _String8, 0, 1, 
22ec3 30 2c 20 7a 52 65 74 2c 20 30 29 3b 0a 20 20 20  0, zRet, 0);.   
22ec4 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22ec5 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p2(v, OP_ResultR
22ec6 6f 77 2c 20 31 2c 20 31 29 3b 0a 20 20 7d 65 6c  ow, 1, 1);.  }el
22ec7 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50  se..  /*.  **  P
22ec8 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
22ec9 5d 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 0a 20 20  ]journal_mode.  
22eca 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  **  PRAGMA [data
22ecb 62 61 73 65 2e 5d 6a 6f 75 72 6e 61 6c 5f 6d 6f  base.]journal_mo
22ecc 64 65 20 3d 20 28 64 65 6c 65 74 65 7c 70 65 72  de = (delete|per
22ecd 73 69 73 74 7c 6d 65 6d 6f 72 79 7c 6f 66 66 29  sist|memory|off)
22ece 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
22ecf 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
22ed0 2c 22 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 22 29  ,"journal_mode")
22ed1 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 65  ==0 ){.    int e
22ed2 4d 6f 64 65 3b 0a 20 20 20 20 73 74 61 74 69 63  Mode;.    static
22ed3 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 61 7a   char * const az
22ed4 4d 6f 64 65 4e 61 6d 65 5b 5d 20 3d 20 7b 0a 20  ModeName[] = {. 
22ed5 20 20 20 20 20 22 64 65 6c 65 74 65 22 2c 20 22       "delete", "
22ed6 70 65 72 73 69 73 74 22 2c 20 22 6f 66 66 22 2c  persist", "off",
22ed7 20 22 74 72 75 6e 63 61 74 65 22 2c 20 22 6d 65   "truncate", "me
22ed8 6d 6f 72 79 22 0a 20 20 20 20 7d 3b 0a 0a 20 20  mory".    };..  
22ed9 20 20 69 66 28 20 7a 52 69 67 68 74 3d 3d 30 20    if( zRight==0 
22eda 29 7b 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d  ){.      eMode =
22edb 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f   PAGER_JOURNALMO
22edc 44 45 5f 51 55 45 52 59 3b 0a 20 20 20 20 7d 65  DE_QUERY;.    }e
22edd 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e  lse{.      int n
22ede 20 3d 20 73 74 72 6c 65 6e 28 7a 52 69 67 68 74   = strlen(zRight
22edf 29 3b 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d  );.      eMode =
22ee0 20 73 69 7a 65 6f 66 28 61 7a 4d 6f 64 65 4e 61   sizeof(azModeNa
22ee1 6d 65 29 2f 73 69 7a 65 6f 66 28 61 7a 4d 6f 64  me)/sizeof(azMod
22ee2 65 4e 61 6d 65 5b 30 5d 29 20 2d 20 31 3b 0a 20  eName[0]) - 1;. 
22ee3 20 20 20 20 20 77 68 69 6c 65 28 20 65 4d 6f 64       while( eMod
22ee4 65 3e 3d 30 20 26 26 20 73 71 6c 69 74 65 33 53  e>=0 && sqlite3S
22ee5 74 72 4e 49 43 6d 70 28 7a 52 69 67 68 74 2c 20  trNICmp(zRight, 
22ee6 61 7a 4d 6f 64 65 4e 61 6d 65 5b 65 4d 6f 64 65  azModeName[eMode
22ee7 5d 2c 20 6e 29 21 3d 30 20 29 7b 0a 20 20 20 20  ], n)!=0 ){.    
22ee8 20 20 20 20 65 4d 6f 64 65 2d 2d 3b 0a 20 20 20      eMode--;.   
22ee9 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
22eea 66 28 20 70 49 64 32 2d 3e 6e 3d 3d 30 20 26 26  f( pId2->n==0 &&
22eeb 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f   eMode==PAGER_JO
22eec 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 20  URNALMODE_QUERY 
22eed 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 69 6d 70  ){.      /* Simp
22eee 6c 65 20 22 50 52 41 47 4d 41 20 6a 6f 75 72 6e  le "PRAGMA journ
22eef 61 6c 5f 6d 6f 64 65 3b 22 20 73 74 61 74 65 6d  al_mode;" statem
22ef0 65 6e 74 2e 20 54 68 69 73 20 69 73 20 61 20 71  ent. This is a q
22ef1 75 65 72 79 20 66 6f 72 0a 20 20 20 20 20 20 2a  uery for.      *
22ef2 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 65  * the current de
22ef3 66 61 75 6c 74 20 6a 6f 75 72 6e 61 6c 20 6d 6f  fault journal mo
22ef4 64 65 20 28 77 68 69 63 68 20 6d 61 79 20 62 65  de (which may be
22ef5 20 64 69 66 66 65 72 65 6e 74 20 74 6f 0a 20 20   different to.  
22ef6 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e      ** the journ
22ef7 61 6c 2d 6d 6f 64 65 20 6f 66 20 74 68 65 20 6d  al-mode of the m
22ef8 61 69 6e 20 64 61 74 61 62 61 73 65 29 2e 0a 20  ain database).. 
22ef9 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 65 4d       */.      eM
22efa 6f 64 65 20 3d 20 64 62 2d 3e 64 66 6c 74 4a 6f  ode = db->dfltJo
22efb 75 72 6e 61 6c 4d 6f 64 65 3b 0a 20 20 20 20 7d  urnalMode;.    }
22efc 65 6c 73 65 7b 0a 20 20 20 20 20 20 50 61 67 65  else{.      Page
22efd 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20 20 20  r *pPager;.     
22efe 20 69 66 28 20 70 49 64 32 2d 3e 6e 3d 3d 30 20   if( pId2->n==0 
22eff 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
22f00 69 73 20 69 6e 64 69 63 61 74 65 73 20 74 68 61  is indicates tha
22f01 74 20 6e 6f 20 64 61 74 61 62 61 73 65 20 6e 61  t no database na
22f02 6d 65 20 77 61 73 20 73 70 65 63 69 66 69 65 64  me was specified
22f03 20 61 73 20 70 61 72 74 0a 20 20 20 20 20 20 20   as part.       
22f04 20 2a 2a 20 6f 66 20 74 68 65 20 50 52 41 47 4d   ** of the PRAGM
22f05 41 20 63 6f 6d 6d 61 6e 64 2e 20 49 6e 20 74 68  A command. In th
22f06 69 73 20 63 61 73 65 20 74 68 65 20 6a 6f 75 72  is case the jour
22f07 6e 61 6c 2d 6d 6f 64 65 20 6d 75 73 74 20 62 65  nal-mode must be
22f08 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 74 20  .        ** set 
22f09 6f 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20  on all attached 
22f0a 64 61 74 61 62 61 73 65 73 2c 20 61 73 20 77 65  databases, as we
22f0b 6c 6c 20 61 73 20 74 68 65 20 6d 61 69 6e 20 64  ll as the main d
22f0c 62 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 20 20  b file..        
22f0d 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 41 6c  **.        ** Al
22f0e 73 6f 2c 20 74 68 65 20 73 71 6c 69 74 65 33 2e  so, the sqlite3.
22f0f 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 20  dfltJournalMode 
22f10 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20  variable is set 
22f11 73 6f 20 74 68 61 74 0a 20 20 20 20 20 20 20 20  so that.        
22f12 2a 2a 20 61 6e 79 20 73 75 62 73 65 71 75 65 6e  ** any subsequen
22f13 74 6c 79 20 61 74 74 61 63 68 65 64 20 64 61 74  tly attached dat
22f14 61 62 61 73 65 73 20 61 6c 73 6f 20 75 73 65 20  abases also use 
22f15 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 20 20  the specified.  
22f16 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c        ** journal
22f17 20 6d 6f 64 65 2e 0a 20 20 20 20 20 20 20 20 2a   mode..        *
22f18 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 69  /.        int ii
22f19 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
22f1a 28 70 44 62 3d 3d 26 64 62 2d 3e 61 44 62 5b 30  (pDb==&db->aDb[0
22f1b 5d 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  ]);.        for(
22f1c 69 69 3d 31 3b 20 69 69 3c 64 62 2d 3e 6e 44 62  ii=1; ii<db->nDb
22f1d 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  ; ii++){.       
22f1e 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69     if( db->aDb[i
22f1f 69 5d 2e 70 42 74 20 29 7b 0a 20 20 20 20 20 20  i].pBt ){.      
22f20 20 20 20 20 20 20 70 50 61 67 65 72 20 3d 20 73        pPager = s
22f21 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
22f22 28 64 62 2d 3e 61 44 62 5b 69 69 5d 2e 70 42 74  (db->aDb[ii].pBt
22f23 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  );.            s
22f24 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e  qlite3PagerJourn
22f25 61 6c 4d 6f 64 65 28 70 50 61 67 65 72 2c 20 65  alMode(pPager, e
22f26 4d 6f 64 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Mode);.         
22f27 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
22f28 20 20 20 20 20 64 62 2d 3e 64 66 6c 74 4a 6f 75       db->dfltJou
22f29 72 6e 61 6c 4d 6f 64 65 20 3d 20 65 4d 6f 64 65  rnalMode = eMode
22f2a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
22f2b 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33  pPager = sqlite3
22f2c 42 74 72 65 65 50 61 67 65 72 28 70 44 62 2d 3e  BtreePager(pDb->
22f2d 70 42 74 29 3b 0a 20 20 20 20 20 20 65 4d 6f 64  pBt);.      eMod
22f2e 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  e = sqlite3Pager
22f2f 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 70 50 61 67  JournalMode(pPag
22f30 65 72 2c 20 65 4d 6f 64 65 29 3b 0a 20 20 20 20  er, eMode);.    
22f31 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 65 4d  }.    assert( eM
22f32 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
22f33 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 20 20  ALMODE_DELETE.  
22f34 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65              || e
22f35 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
22f36 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45  NALMODE_TRUNCATE
22f37 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c  .              |
22f38 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  | eMode==PAGER_J
22f39 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49  OURNALMODE_PERSI
22f3a 53 54 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ST.             
22f3b 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52   || eMode==PAGER
22f3c 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46  _JOURNALMODE_OFF
22f3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c  .              |
22f3e 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  | eMode==PAGER_J
22f3f 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52  OURNALMODE_MEMOR
22f40 59 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Y );.    sqlite3
22f41 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
22f42 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 1);.    sqlite
22f43 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
22f44 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 0, COLNAME_NA
22f45 4d 45 2c 20 22 6a 6f 75 72 6e 61 6c 5f 6d 6f 64  ME, "journal_mod
22f46 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  e", SQLITE_STATI
22f47 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  C);.    sqlite3V
22f48 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
22f49 53 74 72 69 6e 67 38 2c 20 30 2c 20 31 2c 20 30  String8, 0, 1, 0
22f4a 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 61 7a  , .           az
22f4b 4d 6f 64 65 4e 61 6d 65 5b 65 4d 6f 64 65 5d 2c  ModeName[eMode],
22f4c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P4_STATIC);.   
22f4d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22f4e 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p2(v, OP_ResultR
22f4f 6f 77 2c 20 31 2c 20 31 29 3b 0a 20 20 7d 65 6c  ow, 1, 1);.  }el
22f50 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50  se..  /*.  **  P
22f51 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
22f52 5d 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69  ]journal_size_li
22f53 6d 69 74 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41  mit.  **  PRAGMA
22f54 20 5b 64 61 74 61 62 61 73 65 2e 5d 6a 6f 75 72   [database.]jour
22f55 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74 3d 4e  nal_size_limit=N
22f56 0a 20 20 2a 2a 0a 20 20 2a 2a 20 47 65 74 20 6f  .  **.  ** Get o
22f57 72 20 73 65 74 20 74 68 65 20 28 62 6f 6f 6c 65  r set the (boole
22f58 61 6e 29 20 76 61 6c 75 65 20 6f 66 20 74 68 65  an) value of the
22f59 20 64 61 74 61 62 61 73 65 20 27 61 75 74 6f 2d   database 'auto-
22f5a 76 61 63 75 75 6d 27 20 70 61 72 61 6d 65 74 65  vacuum' paramete
22f5b 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  r..  */.  if( sq
22f5c 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
22f5d 66 74 2c 22 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65  ft,"journal_size
22f5e 5f 6c 69 6d 69 74 22 29 3d 3d 30 20 29 7b 0a 20  _limit")==0 ){. 
22f5f 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72     Pager *pPager
22f60 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50   = sqlite3BtreeP
22f61 61 67 65 72 28 70 44 62 2d 3e 70 42 74 29 3b 0a  ager(pDb->pBt);.
22f62 20 20 20 20 69 36 34 20 69 4c 69 6d 69 74 20 3d      i64 iLimit =
22f63 20 2d 32 3b 0a 20 20 20 20 69 66 28 20 7a 52 69   -2;.    if( zRi
22f64 67 68 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  ght ){.      int
22f65 20 69 4c 69 6d 69 74 33 32 20 3d 20 61 74 6f 69   iLimit32 = atoi
22f66 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20  (zRight);.      
22f67 69 66 28 20 69 4c 69 6d 69 74 33 32 3c 2d 31 20  if( iLimit32<-1 
22f68 29 7b 0a 20 20 20 20 20 20 20 20 69 4c 69 6d 69  ){.        iLimi
22f69 74 33 32 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20  t32 = -1;.      
22f6a 7d 0a 20 20 20 20 20 20 69 4c 69 6d 69 74 20 3d  }.      iLimit =
22f6b 20 69 4c 69 6d 69 74 33 32 3b 0a 20 20 20 20 7d   iLimit32;.    }
22f6c 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 73 71  .    iLimit = sq
22f6d 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61  lite3PagerJourna
22f6e 6c 53 69 7a 65 4c 69 6d 69 74 28 70 50 61 67 65  lSizeLimit(pPage
22f6f 72 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20  r, iLimit);.    
22f70 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28  returnSingleInt(
22f71 70 50 61 72 73 65 2c 20 22 6a 6f 75 72 6e 61 6c  pParse, "journal
22f72 5f 73 69 7a 65 5f 6c 69 6d 69 74 22 2c 20 28 69  _size_limit", (i
22f73 6e 74 29 69 4c 69 6d 69 74 29 3b 0a 20 20 7d 65  nt)iLimit);.  }e
22f74 6c 73 65 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  lse..#endif /* S
22f75 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52  QLITE_OMIT_PAGER
22f76 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 20 20 2f  _PRAGMAS */..  /
22f77 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  *.  **  PRAGMA [
22f78 64 61 74 61 62 61 73 65 2e 5d 61 75 74 6f 5f 76  database.]auto_v
22f79 61 63 75 75 6d 0a 20 20 2a 2a 20 20 50 52 41 47  acuum.  **  PRAG
22f7a 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 61 75  MA [database.]au
22f7b 74 6f 5f 76 61 63 75 75 6d 3d 4e 0a 20 20 2a 2a  to_vacuum=N.  **
22f7c 0a 20 20 2a 2a 20 47 65 74 20 6f 72 20 73 65 74  .  ** Get or set
22f7d 20 74 68 65 20 28 62 6f 6f 6c 65 61 6e 29 20 76   the (boolean) v
22f7e 61 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74 61  alue of the data
22f7f 62 61 73 65 20 27 61 75 74 6f 2d 76 61 63 75 75  base 'auto-vacuu
22f80 6d 27 20 70 61 72 61 6d 65 74 65 72 2e 0a 20 20  m' parameter..  
22f81 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
22f82 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
22f83 4d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  M.  if( sqlite3S
22f84 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 61 75  trICmp(zLeft,"au
22f85 74 6f 5f 76 61 63 75 75 6d 22 29 3d 3d 30 20 29  to_vacuum")==0 )
22f86 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42 74  {.    Btree *pBt
22f87 20 3d 20 70 44 62 2d 3e 70 42 74 3b 0a 20 20 20   = pDb->pBt;.   
22f88 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64   if( sqlite3Read
22f89 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
22f8a 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 72 61  {.      goto pra
22f8b 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  gma_out;.    }. 
22f8c 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29     if( !zRight )
22f8d 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 75 74 6f  {.      int auto
22f8e 5f 76 61 63 75 75 6d 20 3d 20 0a 20 20 20 20 20  _vacuum = .     
22f8f 20 20 20 20 20 70 42 74 20 3f 20 73 71 6c 69 74       pBt ? sqlit
22f90 65 33 42 74 72 65 65 47 65 74 41 75 74 6f 56 61  e3BtreeGetAutoVa
22f91 63 75 75 6d 28 70 42 74 29 20 3a 20 53 51 4c 49  cuum(pBt) : SQLI
22f92 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56  TE_DEFAULT_AUTOV
22f93 41 43 55 55 4d 3b 0a 20 20 20 20 20 20 72 65 74  ACUUM;.      ret
22f94 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61  urnSingleInt(pPa
22f95 72 73 65 2c 20 22 61 75 74 6f 5f 76 61 63 75 75  rse, "auto_vacuu
22f96 6d 22 2c 20 61 75 74 6f 5f 76 61 63 75 75 6d 29  m", auto_vacuum)
22f97 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
22f98 20 20 20 69 6e 74 20 65 41 75 74 6f 20 3d 20 67     int eAuto = g
22f99 65 74 41 75 74 6f 56 61 63 75 75 6d 28 7a 52 69  etAutoVacuum(zRi
22f9a 67 68 74 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e  ght);.      db->
22f9b 6e 65 78 74 41 75 74 6f 76 61 63 20 3d 20 65 41  nextAutovac = eA
22f9c 75 74 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 65  uto;.      if( e
22f9d 41 75 74 6f 3e 3d 30 20 29 7b 0a 20 20 20 20 20  Auto>=0 ){.     
22f9e 20 20 20 2f 2a 20 43 61 6c 6c 20 53 65 74 41 75     /* Call SetAu
22f9f 74 6f 56 61 63 75 75 6d 28 29 20 74 6f 20 73 65  toVacuum() to se
22fa0 74 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  t initialize the
22fa1 20 69 6e 74 65 72 6e 61 6c 20 61 75 74 6f 20 61   internal auto a
22fa2 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e  nd.        ** in
22fa3 63 72 2d 76 61 63 75 75 6d 20 66 6c 61 67 73 2e  cr-vacuum flags.
22fa4 20 54 68 69 73 20 69 73 20 72 65 71 75 69 72 65   This is require
22fa5 64 20 69 6e 20 63 61 73 65 20 74 68 69 73 20 63  d in case this c
22fa6 6f 6e 6e 65 63 74 69 6f 6e 0a 20 20 20 20 20 20  onnection.      
22fa7 20 20 2a 2a 20 63 72 65 61 74 65 73 20 74 68 65    ** creates the
22fa8 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
22fa9 49 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20  It is important 
22faa 74 68 61 74 20 69 74 20 69 73 20 63 72 65 61 74  that it is creat
22fab 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 73  ed.        ** as
22fac 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20   an auto-vacuum 
22fad 63 61 70 61 62 6c 65 20 64 62 2e 0a 20 20 20 20  capable db..    
22fae 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69      */.        i
22faf 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  nt rc = sqlite3B
22fb0 74 72 65 65 53 65 74 41 75 74 6f 56 61 63 75 75  treeSetAutoVacuu
22fb1 6d 28 70 42 74 2c 20 65 41 75 74 6f 29 3b 0a 20  m(pBt, eAuto);. 
22fb2 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
22fb3 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 65 41 75  QLITE_OK && (eAu
22fb4 74 6f 3d 3d 31 20 7c 7c 20 65 41 75 74 6f 3d 3d  to==1 || eAuto==
22fb5 32 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  2) ){.          
22fb6 2f 2a 20 57 68 65 6e 20 73 65 74 74 69 6e 67 20  /* When setting 
22fb7 74 68 65 20 61 75 74 6f 5f 76 61 63 75 75 6d 20  the auto_vacuum 
22fb8 6d 6f 64 65 20 74 6f 20 65 69 74 68 65 72 20 22  mode to either "
22fb9 66 75 6c 6c 22 20 6f 72 20 0a 20 20 20 20 20 20  full" or .      
22fba 20 20 20 20 2a 2a 20 22 69 6e 63 72 65 6d 65 6e      ** "incremen
22fbb 74 61 6c 22 2c 20 77 72 69 74 65 20 74 68 65 20  tal", write the 
22fbc 76 61 6c 75 65 20 6f 66 20 6d 65 74 61 5b 36 5d  value of meta[6]
22fbd 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
22fbe 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 69  .          ** fi
22fbf 6c 65 2e 20 42 65 66 6f 72 65 20 77 72 69 74 69  le. Before writi
22fc0 6e 67 20 74 6f 20 6d 65 74 61 5b 36 5d 2c 20 63  ng to meta[6], c
22fc1 68 65 63 6b 20 74 68 61 74 20 6d 65 74 61 5b 33  heck that meta[3
22fc2 5d 20 69 6e 64 69 63 61 74 65 73 0a 20 20 20 20  ] indicates.    
22fc3 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68        ** that th
22fc4 69 73 20 72 65 61 6c 6c 79 20 69 73 20 61 6e 20  is really is an 
22fc5 61 75 74 6f 2d 76 61 63 75 75 6d 20 63 61 70 61  auto-vacuum capa
22fc6 62 6c 65 20 64 61 74 61 62 61 73 65 2e 0a 20 20  ble database..  
22fc7 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
22fc8 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73       static cons
22fc9 74 20 56 64 62 65 4f 70 4c 69 73 74 20 73 65 74  t VdbeOpList set
22fca 4d 65 74 61 36 5b 5d 20 3d 20 7b 0a 20 20 20 20  Meta6[] = {.    
22fcb 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 54 72 61          { OP_Tra
22fcc 6e 73 61 63 74 69 6f 6e 2c 20 20 20 20 30 2c 20  nsaction,    0, 
22fcd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c                1,
22fce 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20 2f          0},    /
22fcf 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  * 0 */.         
22fd0 20 20 20 7b 20 4f 50 5f 52 65 61 64 43 6f 6f 6b     { OP_ReadCook
22fd1 69 65 2c 20 20 20 20 20 30 2c 20 20 20 20 20 20  ie,     0,      
22fd2 20 20 20 20 20 20 20 20 20 31 2c 20 20 20 20 20           1,     
22fd3 20 20 20 33 7d 2c 20 20 20 20 2f 2a 20 31 20 2a     3},    /* 1 *
22fd4 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  /.            { 
22fd5 4f 50 5f 49 66 2c 20 20 20 20 20 20 20 20 20 20  OP_If,          
22fd6 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20     1,           
22fd7 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 30 7d      0,        0}
22fd8 2c 20 20 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20  ,    /* 2 */.   
22fd9 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 48 61           { OP_Ha
22fda 6c 74 2c 20 20 20 20 20 20 20 20 20 20 20 53 51  lt,           SQ
22fdb 4c 49 54 45 5f 4f 4b 2c 20 20 20 20 20 20 20 4f  LITE_OK,       O
22fdc 45 5f 41 62 6f 72 74 2c 20 30 7d 2c 20 20 20 20  E_Abort, 0},    
22fdd 2f 2a 20 33 20 2a 2f 0a 20 20 20 20 20 20 20 20  /* 3 */.        
22fde 20 20 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72      { OP_Integer
22fdf 2c 20 20 20 20 20 20 20 20 30 2c 20 20 20 20 20  ,        0,     
22fe0 20 20 20 20 20 20 20 20 20 20 31 2c 20 20 20 20            1,    
22fe1 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20 34 20      0},    /* 4 
22fe2 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b  */.            {
22fe3 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 20   OP_SetCookie,  
22fe4 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20      0,          
22fe5 20 20 20 20 20 36 2c 20 20 20 20 20 20 20 20 31       6,        1
22fe6 7d 2c 20 20 20 20 2f 2a 20 35 20 2a 2f 0a 20 20  },    /* 5 */.  
22fe7 20 20 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20          };.     
22fe8 20 20 20 20 20 69 6e 74 20 69 41 64 64 72 3b 0a       int iAddr;.
22fe9 20 20 20 20 20 20 20 20 20 20 69 41 64 64 72 20            iAddr 
22fea 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
22feb 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53  OpList(v, ArrayS
22fec 69 7a 65 28 73 65 74 4d 65 74 61 36 29 2c 20 73  ize(setMeta6), s
22fed 65 74 4d 65 74 61 36 29 3b 0a 20 20 20 20 20 20  etMeta6);.      
22fee 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
22fef 68 61 6e 67 65 50 31 28 76 2c 20 69 41 64 64 72  hangeP1(v, iAddr
22ff0 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20  , iDb);.        
22ff1 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
22ff2 6e 67 65 50 31 28 76 2c 20 69 41 64 64 72 2b 31  ngeP1(v, iAddr+1
22ff3 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20  , iDb);.        
22ff4 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
22ff5 6e 67 65 50 32 28 76 2c 20 69 41 64 64 72 2b 32  ngeP2(v, iAddr+2
22ff6 2c 20 69 41 64 64 72 2b 34 29 3b 0a 20 20 20 20  , iAddr+4);.    
22ff7 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
22ff8 65 43 68 61 6e 67 65 50 31 28 76 2c 20 69 41 64  eChangeP1(v, iAd
22ff9 64 72 2b 34 2c 20 65 41 75 74 6f 2d 31 29 3b 0a  dr+4, eAuto-1);.
22ffa 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
22ffb 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c  3VdbeChangeP1(v,
22ffc 20 69 41 64 64 72 2b 35 2c 20 69 44 62 29 3b 0a   iAddr+5, iDb);.
22ffd 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
22ffe 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76  3VdbeUsesBtree(v
22fff 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20  , iDb);.        
23000 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
23001 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a    }else.#endif..
23002 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d    /*.  **  PRAGM
23003 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 69 6e 63  A [database.]inc
23004 72 65 6d 65 6e 74 61 6c 5f 76 61 63 75 75 6d 28  remental_vacuum(
23005 4e 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 44 6f 20  N).  **.  ** Do 
23006 4e 20 73 74 65 70 73 20 6f 66 20 69 6e 63 72 65  N steps of incre
23007 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 69 6e 67  mental vacuuming
23008 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 2e 0a   on a database..
23009 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c    */.#ifndef SQL
2300a 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
2300b 55 55 4d 0a 20 20 69 66 28 20 73 71 6c 69 74 65  UUM.  if( sqlite
2300c 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22  3StrICmp(zLeft,"
2300d 69 6e 63 72 65 6d 65 6e 74 61 6c 5f 76 61 63 75  incremental_vacu
2300e 75 6d 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69  um")==0 ){.    i
2300f 6e 74 20 69 4c 69 6d 69 74 2c 20 61 64 64 72 3b  nt iLimit, addr;
23010 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
23011 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
23012 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  e) ){.      goto
23013 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20   pragma_out;.   
23014 20 7d 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68   }.    if( zRigh
23015 74 3d 3d 30 20 7c 7c 20 21 73 71 6c 69 74 65 33  t==0 || !sqlite3
23016 47 65 74 49 6e 74 33 32 28 7a 52 69 67 68 74 2c  GetInt32(zRight,
23017 20 26 69 4c 69 6d 69 74 29 20 7c 7c 20 69 4c 69   &iLimit) || iLi
23018 6d 69 74 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20  mit<=0 ){.      
23019 69 4c 69 6d 69 74 20 3d 20 30 78 37 66 66 66 66  iLimit = 0x7ffff
2301a 66 66 66 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  fff;.    }.    s
2301b 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
2301c 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
2301d 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 73  , 0, iDb);.    s
2301e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2301f 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
23020 69 4c 69 6d 69 74 2c 20 31 29 3b 0a 20 20 20 20  iLimit, 1);.    
23021 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
23022 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
23023 6e 63 72 56 61 63 75 75 6d 2c 20 69 44 62 29 3b  ncrVacuum, iDb);
23024 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
23025 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 73  AddOp1(v, OP_Res
23026 75 6c 74 52 6f 77 2c 20 31 29 3b 0a 20 20 20 20  ultRow, 1);.    
23027 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23028 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20  2(v, OP_AddImm, 
23029 31 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69  1, -1);.    sqli
2302a 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2302b 20 4f 50 5f 49 66 50 6f 73 2c 20 31 2c 20 61 64   OP_IfPos, 1, ad
2302c 64 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  dr);.    sqlite3
2302d 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
2302e 61 64 64 72 29 3b 0a 20 20 7d 65 6c 73 65 0a 23  addr);.  }else.#
2302f 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53  endif..#ifndef S
23030 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52  QLITE_OMIT_PAGER
23031 5f 50 52 41 47 4d 41 53 0a 20 20 2f 2a 0a 20 20  _PRAGMAS.  /*.  
23032 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  **  PRAGMA [data
23033 62 61 73 65 2e 5d 63 61 63 68 65 5f 73 69 7a 65  base.]cache_size
23034 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
23035 61 74 61 62 61 73 65 2e 5d 63 61 63 68 65 5f 73  atabase.]cache_s
23036 69 7a 65 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ize=N.  **.  ** 
23037 54 68 65 20 66 69 72 73 74 20 66 6f 72 6d 20 72  The first form r
23038 65 70 6f 72 74 73 20 74 68 65 20 63 75 72 72 65  eports the curre
23039 6e 74 20 6c 6f 63 61 6c 20 73 65 74 74 69 6e 67  nt local setting
2303a 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 70 61   for the.  ** pa
2303b 67 65 20 63 61 63 68 65 20 73 69 7a 65 2e 20 20  ge cache size.  
2303c 54 68 65 20 6c 6f 63 61 6c 20 73 65 74 74 69 6e  The local settin
2303d 67 20 63 61 6e 20 62 65 20 64 69 66 66 65 72 65  g can be differe
2303e 6e 74 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65  nt from.  ** the
2303f 20 70 65 72 73 69 73 74 65 6e 74 20 63 61 63 68   persistent cach
23040 65 20 73 69 7a 65 20 76 61 6c 75 65 20 74 68 61  e size value tha
23041 74 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74  t is stored in t
23042 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a  he database.  **
23043 20 66 69 6c 65 20 69 74 73 65 6c 66 2e 20 20 54   file itself.  T
23044 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
23045 64 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d  d is the maximum
23046 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 2a 2a 20   number of.  ** 
23047 70 61 67 65 73 20 69 6e 20 74 68 65 20 70 61 67  pages in the pag
23048 65 20 63 61 63 68 65 2e 20 20 54 68 65 20 73 65  e cache.  The se
23049 63 6f 6e 64 20 66 6f 72 6d 20 73 65 74 73 20 74  cond form sets t
2304a 68 65 20 6c 6f 63 61 6c 0a 20 20 2a 2a 20 70 61  he local.  ** pa
2304b 67 65 20 63 61 63 68 65 20 73 69 7a 65 20 76 61  ge cache size va
2304c 6c 75 65 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f  lue.  It does no
2304d 74 20 63 68 61 6e 67 65 20 74 68 65 20 70 65 72  t change the per
2304e 73 69 73 74 65 6e 74 0a 20 20 2a 2a 20 63 61 63  sistent.  ** cac
2304f 68 65 20 73 69 7a 65 20 73 74 6f 72 65 64 20 6f  he size stored o
23050 6e 20 74 68 65 20 64 69 73 6b 20 73 6f 20 74 68  n the disk so th
23051 65 20 63 61 63 68 65 20 73 69 7a 65 20 77 69 6c  e cache size wil
23052 6c 20 72 65 76 65 72 74 0a 20 20 2a 2a 20 74 6f  l revert.  ** to
23053 20 69 74 73 20 64 65 66 61 75 6c 74 20 76 61 6c   its default val
23054 75 65 20 77 68 65 6e 20 74 68 65 20 64 61 74 61  ue when the data
23055 62 61 73 65 20 69 73 20 63 6c 6f 73 65 64 20 61  base is closed a
23056 6e 64 20 72 65 6f 70 65 6e 65 64 2e 0a 20 20 2a  nd reopened..  *
23057 2a 20 4e 20 73 68 6f 75 6c 64 20 62 65 20 61 20  * N should be a 
23058 70 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72  positive integer
23059 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
2305a 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
2305b 74 2c 22 63 61 63 68 65 5f 73 69 7a 65 22 29 3d  t,"cache_size")=
2305c 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71  =0 ){.    if( sq
2305d 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
2305e 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70  pParse) ) goto p
2305f 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 69  ragma_out;.    i
23060 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a 20 20  f( !zRight ){.  
23061 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65      returnSingle
23062 49 6e 74 28 70 50 61 72 73 65 2c 20 22 63 61 63  Int(pParse, "cac
23063 68 65 5f 73 69 7a 65 22 2c 20 70 44 62 2d 3e 70  he_size", pDb->p
23064 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69  Schema->cache_si
23065 7a 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ze);.    }else{.
23066 20 20 20 20 20 20 69 6e 74 20 73 69 7a 65 20 3d        int size =
23067 20 61 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20   atoi(zRight);. 
23068 20 20 20 20 20 69 66 28 20 73 69 7a 65 3c 30 20       if( size<0 
23069 29 20 73 69 7a 65 20 3d 20 2d 73 69 7a 65 3b 0a  ) size = -size;.
2306a 20 20 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65        pDb->pSche
2306b 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 20 3d  ma->cache_size =
2306c 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 73 71 6c   size;.      sql
2306d 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68  ite3BtreeSetCach
2306e 65 53 69 7a 65 28 70 44 62 2d 3e 70 42 74 2c 20  eSize(pDb->pBt, 
2306f 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61  pDb->pSchema->ca
23070 63 68 65 5f 73 69 7a 65 29 3b 0a 20 20 20 20 7d  che_size);.    }
23071 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20  .  }else..  /*. 
23072 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 74 65 6d   **   PRAGMA tem
23073 70 5f 73 74 6f 72 65 0a 20 20 2a 2a 20 20 20 50  p_store.  **   P
23074 52 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65  RAGMA temp_store
23075 20 3d 20 22 64 65 66 61 75 6c 74 22 7c 22 6d 65   = "default"|"me
23076 6d 6f 72 79 22 7c 22 66 69 6c 65 22 0a 20 20 2a  mory"|"file".  *
23077 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f 72  *.  ** Return or
23078 20 73 65 74 20 74 68 65 20 6c 6f 63 61 6c 20 76   set the local v
23079 61 6c 75 65 20 6f 66 20 74 68 65 20 74 65 6d 70  alue of the temp
2307a 5f 73 74 6f 72 65 20 66 6c 61 67 2e 20 20 43 68  _store flag.  Ch
2307b 61 6e 67 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20  anging.  ** the 
2307c 6c 6f 63 61 6c 20 76 61 6c 75 65 20 64 6f 65 73  local value does
2307d 20 6e 6f 74 20 6d 61 6b 65 20 63 68 61 6e 67 65   not make change
2307e 73 20 74 6f 20 74 68 65 20 64 69 73 6b 20 66 69  s to the disk fi
2307f 6c 65 20 61 6e 64 20 74 68 65 20 64 65 66 61 75  le and the defau
23080 6c 74 0a 20 20 2a 2a 20 76 61 6c 75 65 20 77 69  lt.  ** value wi
23081 6c 6c 20 62 65 20 72 65 73 74 6f 72 65 64 20 74  ll be restored t
23082 68 65 20 6e 65 78 74 20 74 69 6d 65 20 74 68 65  he next time the
23083 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65   database is ope
23084 6e 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e  ned..  **.  ** N
23085 6f 74 65 20 74 68 61 74 20 69 74 20 69 73 20 70  ote that it is p
23086 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 65 20  ossible for the 
23087 6c 69 62 72 61 72 79 20 63 6f 6d 70 69 6c 65 2d  library compile-
23088 74 69 6d 65 20 6f 70 74 69 6f 6e 73 20 74 6f 0a  time options to.
23089 20 20 2a 2a 20 6f 76 65 72 72 69 64 65 20 74 68    ** override th
2308a 69 73 20 73 65 74 74 69 6e 67 0a 20 20 2a 2f 0a  is setting.  */.
2308b 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
2308c 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 74 65 6d  ICmp(zLeft, "tem
2308d 70 5f 73 74 6f 72 65 22 29 3d 3d 30 20 29 7b 0a  p_store")==0 ){.
2308e 20 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20      if( !zRight 
2308f 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 53  ){.      returnS
23090 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c  ingleInt(pParse,
23091 20 22 74 65 6d 70 5f 73 74 6f 72 65 22 2c 20 64   "temp_store", d
23092 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 29 3b 0a  b->temp_store);.
23093 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
23094 20 63 68 61 6e 67 65 54 65 6d 70 53 74 6f 72 61   changeTempStora
23095 67 65 28 70 50 61 72 73 65 2c 20 7a 52 69 67 68  ge(pParse, zRigh
23096 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  t);.    }.  }els
23097 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50  e..  /*.  **   P
23098 52 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65  RAGMA temp_store
23099 5f 64 69 72 65 63 74 6f 72 79 0a 20 20 2a 2a 20  _directory.  ** 
2309a 20 20 50 52 41 47 4d 41 20 74 65 6d 70 5f 73 74    PRAGMA temp_st
2309b 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 20 3d 20  ore_directory = 
2309c 22 22 7c 22 64 69 72 65 63 74 6f 72 79 5f 6e 61  ""|"directory_na
2309d 6d 65 22 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65  me".  **.  ** Re
2309e 74 75 72 6e 20 6f 72 20 73 65 74 20 74 68 65 20  turn or set the 
2309f 6c 6f 63 61 6c 20 76 61 6c 75 65 20 6f 66 20 74  local value of t
230a0 68 65 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69  he temp_store_di
230a1 72 65 63 74 6f 72 79 20 66 6c 61 67 2e 20 20 43  rectory flag.  C
230a2 68 61 6e 67 69 6e 67 0a 20 20 2a 2a 20 74 68 65  hanging.  ** the
230a3 20 76 61 6c 75 65 20 73 65 74 73 20 61 20 73 70   value sets a sp
230a4 65 63 69 66 69 63 20 64 69 72 65 63 74 6f 72 79  ecific directory
230a5 20 74 6f 20 62 65 20 75 73 65 64 20 66 6f 72 20   to be used for 
230a6 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e  temporary files.
230a7 0a 20 20 2a 2a 20 53 65 74 74 69 6e 67 20 74 6f  .  ** Setting to
230a8 20 61 20 6e 75 6c 6c 20 73 74 72 69 6e 67 20 72   a null string r
230a9 65 76 65 72 74 73 20 74 6f 20 74 68 65 20 64 65  everts to the de
230aa 66 61 75 6c 74 20 74 65 6d 70 6f 72 61 72 79 20  fault temporary 
230ab 64 69 72 65 63 74 6f 72 79 20 73 65 61 72 63 68  directory search
230ac 2e 0a 20 20 2a 2a 20 49 66 20 74 65 6d 70 6f 72  ..  ** If tempor
230ad 61 72 79 20 64 69 72 65 63 74 6f 72 79 20 69 73  ary directory is
230ae 20 63 68 61 6e 67 65 64 2c 20 74 68 65 6e 20 69   changed, then i
230af 6e 76 61 6c 69 64 61 74 65 54 65 6d 70 53 74 6f  nvalidateTempSto
230b0 72 61 67 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2f 0a  rage..  **.  */.
230b1 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
230b2 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 74 65 6d  ICmp(zLeft, "tem
230b3 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72  p_store_director
230b4 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  y")==0 ){.    if
230b5 28 20 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20  ( !zRight ){.   
230b6 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 74     if( sqlite3_t
230b7 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 29 7b  emp_directory ){
230b8 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
230b9 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
230ba 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 1);.        sq
230bb 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
230bc 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
230bd 45 5f 4e 41 4d 45 2c 20 0a 20 20 20 20 20 20 20  E_NAME, .       
230be 20 20 20 20 20 22 74 65 6d 70 5f 73 74 6f 72 65       "temp_store
230bf 5f 64 69 72 65 63 74 6f 72 79 22 2c 20 53 51 4c  _directory", SQL
230c0 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
230c1 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
230c2 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
230c3 69 6e 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20 73  ing8, 0, 1, 0, s
230c4 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65  qlite3_temp_dire
230c5 63 74 6f 72 79 2c 20 30 29 3b 0a 20 20 20 20 20  ctory, 0);.     
230c6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
230c7 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
230c8 74 52 6f 77 2c 20 31 2c 20 31 29 3b 0a 20 20 20  tRow, 1, 1);.   
230c9 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
230ca 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
230cb 4d 49 54 5f 57 53 44 0a 20 20 20 20 20 20 69 66  MIT_WSD.      if
230cc 28 20 7a 52 69 67 68 74 5b 30 5d 20 29 7b 0a 20  ( zRight[0] ){. 
230cd 20 20 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20         int rc;. 
230ce 20 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a         int res;.
230cf 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
230d0 69 74 65 33 4f 73 41 63 63 65 73 73 28 64 62 2d  ite3OsAccess(db-
230d1 3e 70 56 66 73 2c 20 7a 52 69 67 68 74 2c 20 53  >pVfs, zRight, S
230d2 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41  QLITE_ACCESS_REA
230d3 44 57 52 49 54 45 2c 20 26 72 65 73 29 3b 0a 20  DWRITE, &res);. 
230d4 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
230d5 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 65 73 3d  QLITE_OK || res=
230d6 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
230d7 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
230d8 70 50 61 72 73 65 2c 20 22 6e 6f 74 20 61 20 77  pParse, "not a w
230d9 72 69 74 61 62 6c 65 20 64 69 72 65 63 74 6f 72  ritable director
230da 79 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67  y");.          g
230db 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a  oto pragma_out;.
230dc 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
230dd 7d 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49  }.      if( SQLI
230de 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 30  TE_TEMP_STORE==0
230df 0a 20 20 20 20 20 20 20 7c 7c 20 28 53 51 4c 49  .       || (SQLI
230e0 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 31  TE_TEMP_STORE==1
230e1 20 26 26 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f   && db->temp_sto
230e2 72 65 3c 3d 31 29 0a 20 20 20 20 20 20 20 7c 7c  re<=1).       ||
230e3 20 28 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54   (SQLITE_TEMP_ST
230e4 4f 52 45 3d 3d 32 20 26 26 20 64 62 2d 3e 74 65  ORE==2 && db->te
230e5 6d 70 5f 73 74 6f 72 65 3d 3d 31 29 0a 20 20 20  mp_store==1).   
230e6 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e     ){.        in
230e7 76 61 6c 69 64 61 74 65 54 65 6d 70 53 74 6f 72  validateTempStor
230e8 61 67 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20  age(pParse);.   
230e9 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
230ea 65 33 5f 66 72 65 65 28 73 71 6c 69 74 65 33 5f  e3_free(sqlite3_
230eb 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 29 3b  temp_directory);
230ec 0a 20 20 20 20 20 20 69 66 28 20 7a 52 69 67 68  .      if( zRigh
230ed 74 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 20 20  t[0] ){.        
230ee 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72  sqlite3_temp_dir
230ef 65 63 74 6f 72 79 20 3d 20 73 71 6c 69 74 65 33  ectory = sqlite3
230f0 44 62 53 74 72 44 75 70 28 30 2c 20 7a 52 69 67  DbStrDup(0, zRig
230f1 68 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  ht);.      }else
230f2 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
230f3 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79  3_temp_directory
230f4 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 65   = 0;.      }.#e
230f5 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
230f6 4d 49 54 5f 57 53 44 20 2a 2f 0a 20 20 20 20 7d  MIT_WSD */.    }
230f7 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20  .  }else..  /*. 
230f8 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61   **   PRAGMA [da
230f9 74 61 62 61 73 65 2e 5d 73 79 6e 63 68 72 6f 6e  tabase.]synchron
230fa 6f 75 73 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d  ous.  **   PRAGM
230fb 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 73 79 6e  A [database.]syn
230fc 63 68 72 6f 6e 6f 75 73 3d 4f 46 46 7c 4f 4e 7c  chronous=OFF|ON|
230fd 4e 4f 52 4d 41 4c 7c 46 55 4c 4c 0a 20 20 2a 2a  NORMAL|FULL.  **
230fe 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20  .  ** Return or 
230ff 73 65 74 20 74 68 65 20 6c 6f 63 61 6c 20 76 61  set the local va
23100 6c 75 65 20 6f 66 20 74 68 65 20 73 79 6e 63 68  lue of the synch
23101 72 6f 6e 6f 75 73 20 66 6c 61 67 2e 20 20 43 68  ronous flag.  Ch
23102 61 6e 67 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20  anging.  ** the 
23103 6c 6f 63 61 6c 20 76 61 6c 75 65 20 64 6f 65 73  local value does
23104 20 6e 6f 74 20 6d 61 6b 65 20 63 68 61 6e 67 65   not make change
23105 73 20 74 6f 20 74 68 65 20 64 69 73 6b 20 66 69  s to the disk fi
23106 6c 65 20 61 6e 64 20 74 68 65 0a 20 20 2a 2a 20  le and the.  ** 
23107 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 77 69  default value wi
23108 6c 6c 20 62 65 20 72 65 73 74 6f 72 65 64 20 74  ll be restored t
23109 68 65 20 6e 65 78 74 20 74 69 6d 65 20 74 68 65  he next time the
2310a 20 64 61 74 61 62 61 73 65 20 69 73 0a 20 20 2a   database is.  *
2310b 2a 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20  * opened..  */. 
2310c 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
2310d 43 6d 70 28 7a 4c 65 66 74 2c 22 73 79 6e 63 68  Cmp(zLeft,"synch
2310e 72 6f 6e 6f 75 73 22 29 3d 3d 30 20 29 7b 0a 20  ronous")==0 ){. 
2310f 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
23110 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
23111 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f   ) goto pragma_o
23112 75 74 3b 0a 20 20 20 20 69 66 28 20 21 7a 52 69  ut;.    if( !zRi
23113 67 68 74 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ght ){.      ret
23114 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61  urnSingleInt(pPa
23115 72 73 65 2c 20 22 73 79 6e 63 68 72 6f 6e 6f 75  rse, "synchronou
23116 73 22 2c 20 70 44 62 2d 3e 73 61 66 65 74 79 5f  s", pDb->safety_
23117 6c 65 76 65 6c 2d 31 29 3b 0a 20 20 20 20 7d 65  level-1);.    }e
23118 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 21  lse{.      if( !
23119 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29  db->autoCommit )
2311a 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2311b 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2311c 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 22  , .            "
2311d 53 61 66 65 74 79 20 6c 65 76 65 6c 20 6d 61 79  Safety level may
2311e 20 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 64 20   not be changed 
2311f 69 6e 73 69 64 65 20 61 20 74 72 61 6e 73 61 63  inside a transac
23120 74 69 6f 6e 22 29 3b 0a 20 20 20 20 20 20 7d 65  tion");.      }e
23121 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 44 62  lse{.        pDb
23122 2d 3e 73 61 66 65 74 79 5f 6c 65 76 65 6c 20 3d  ->safety_level =
23123 20 67 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28   getSafetyLevel(
23124 7a 52 69 67 68 74 29 2b 31 3b 0a 20 20 20 20 20  zRight)+1;.     
23125 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
23126 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
23127 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41  E_OMIT_PAGER_PRA
23128 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  GMAS */..#ifndef
23129 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 41   SQLITE_OMIT_FLA
2312a 47 5f 50 52 41 47 4d 41 53 0a 20 20 69 66 28 20  G_PRAGMAS.  if( 
2312b 66 6c 61 67 50 72 61 67 6d 61 28 70 50 61 72 73  flagPragma(pPars
2312c 65 2c 20 7a 4c 65 66 74 2c 20 7a 52 69 67 68 74  e, zLeft, zRight
2312d 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  ) ){.    /* The 
2312e 66 6c 61 67 50 72 61 67 6d 61 28 29 20 73 75 62  flagPragma() sub
2312f 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 67 65 6e  routine also gen
23130 65 72 61 74 65 73 20 61 6e 79 20 6e 65 63 65 73  erates any neces
23131 73 61 72 79 20 63 6f 64 65 0a 20 20 20 20 2a 2a  sary code.    **
23132 20 74 68 65 72 65 20 69 73 20 6e 6f 74 68 69 6e   there is nothin
23133 67 20 6d 6f 72 65 20 74 6f 20 64 6f 20 68 65 72  g more to do her
23134 65 20 2a 2f 0a 20 20 7d 65 6c 73 65 0a 23 65 6e  e */.  }else.#en
23135 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
23136 49 54 5f 46 4c 41 47 5f 50 52 41 47 4d 41 53 20  IT_FLAG_PRAGMAS 
23137 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
23138 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 50  TE_OMIT_SCHEMA_P
23139 52 41 47 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a  RAGMAS.  /*.  **
2313a 20 20 20 50 52 41 47 4d 41 20 74 61 62 6c 65 5f     PRAGMA table_
2313b 69 6e 66 6f 28 3c 74 61 62 6c 65 3e 29 0a 20 20  info(<table>).  
2313c 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 61  **.  ** Return a
2313d 20 73 69 6e 67 6c 65 20 72 6f 77 20 66 6f 72 20   single row for 
2313e 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  each column of t
2313f 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e 20  he named table. 
23140 54 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 0a 20  The columns of. 
23141 20 2a 2a 20 74 68 65 20 72 65 74 75 72 6e 65 64   ** the returned
23142 20 64 61 74 61 20 73 65 74 20 61 72 65 3a 0a 20   data set are:. 
23143 20 2a 2a 0a 20 20 2a 2a 20 63 69 64 3a 20 20 20   **.  ** cid:   
23144 20 20 20 20 20 43 6f 6c 75 6d 6e 20 69 64 20 28       Column id (
23145 6e 75 6d 62 65 72 65 64 20 66 72 6f 6d 20 6c 65  numbered from le
23146 66 74 20 74 6f 20 72 69 67 68 74 2c 20 73 74 61  ft to right, sta
23147 72 74 69 6e 67 20 61 74 20 30 29 0a 20 20 2a 2a  rting at 0).  **
23148 20 6e 61 6d 65 3a 20 20 20 20 20 20 20 43 6f 6c   name:       Col
23149 75 6d 6e 20 6e 61 6d 65 0a 20 20 2a 2a 20 74 79  umn name.  ** ty
2314a 70 65 3a 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e  pe:       Column
2314b 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70   declaration typ
2314c 65 2e 0a 20 20 2a 2a 20 6e 6f 74 6e 75 6c 6c 3a  e..  ** notnull:
2314d 20 20 20 20 54 72 75 65 20 69 66 20 27 4e 4f 54      True if 'NOT
2314e 20 4e 55 4c 4c 27 20 69 73 20 70 61 72 74 20 6f   NULL' is part o
2314f 66 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61  f column declara
23150 74 69 6f 6e 0a 20 20 2a 2a 20 64 66 6c 74 5f 76  tion.  ** dflt_v
23151 61 6c 75 65 3a 20 54 68 65 20 64 65 66 61 75 6c  alue: The defaul
23152 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  t value for the 
23153 63 6f 6c 75 6d 6e 2c 20 69 66 20 61 6e 79 2e 0a  column, if any..
23154 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
23155 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
23156 20 22 74 61 62 6c 65 5f 69 6e 66 6f 22 29 3d 3d   "table_info")==
23157 30 20 26 26 20 7a 52 69 67 68 74 20 29 7b 0a 20  0 && zRight ){. 
23158 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a     Table *pTab;.
23159 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
2315a 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
2315b 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f  ) ) goto pragma_
2315c 6f 75 74 3b 0a 20 20 20 20 70 54 61 62 20 3d 20  out;.    pTab = 
2315d 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65  sqlite3FindTable
2315e 28 64 62 2c 20 7a 52 69 67 68 74 2c 20 7a 44 62  (db, zRight, zDb
2315f 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 20  );.    if( pTab 
23160 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a  ){.      int i;.
23161 20 20 20 20 20 20 69 6e 74 20 6e 48 69 64 64 65        int nHidde
23162 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 43 6f 6c  n = 0;.      Col
23163 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 20 20 20  umn *pCol;.     
23164 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
23165 75 6d 43 6f 6c 73 28 76 2c 20 36 29 3b 0a 20 20  umCols(v, 6);.  
23166 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d      pParse->nMem
23167 20 3d 20 36 3b 0a 20 20 20 20 20 20 73 71 6c 69   = 6;.      sqli
23168 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
23169 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
2316a 4e 41 4d 45 2c 20 22 63 69 64 22 2c 20 53 51 4c  NAME, "cid", SQL
2316b 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
2316c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
2316d 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43  tColName(v, 1, C
2316e 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61  OLNAME_NAME, "na
2316f 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  me", SQLITE_STAT
23170 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
23171 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
23172 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 2, COLNAME_N
23173 41 4d 45 2c 20 22 74 79 70 65 22 2c 20 53 51 4c  AME, "type", SQL
23174 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
23175 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
23176 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 33 2c 20 43  tColName(v, 3, C
23177 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 6f  OLNAME_NAME, "no
23178 74 6e 75 6c 6c 22 2c 20 53 51 4c 49 54 45 5f 53  tnull", SQLITE_S
23179 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71  TATIC);.      sq
2317a 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
2317b 61 6d 65 28 76 2c 20 34 2c 20 43 4f 4c 4e 41 4d  ame(v, 4, COLNAM
2317c 45 5f 4e 41 4d 45 2c 20 22 64 66 6c 74 5f 76 61  E_NAME, "dflt_va
2317d 6c 75 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  lue", SQLITE_STA
2317e 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  TIC);.      sqli
2317f 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
23180 65 28 76 2c 20 35 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 5, COLNAME_
23181 4e 41 4d 45 2c 20 22 70 6b 22 2c 20 53 51 4c 49  NAME, "pk", SQLI
23182 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
23183 20 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74    sqlite3ViewGet
23184 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72  ColumnNames(pPar
23185 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20  se, pTab);.     
23186 20 66 6f 72 28 69 3d 30 2c 20 70 43 6f 6c 3d 70   for(i=0, pCol=p
23187 54 61 62 2d 3e 61 43 6f 6c 3b 20 69 3c 70 54 61  Tab->aCol; i<pTa
23188 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43  b->nCol; i++, pC
23189 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 63  ol++){.        c
2318a 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 44 66 6c  onst Token *pDfl
2318b 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 49  t;.        if( I
2318c 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 70 43  sHiddenColumn(pC
2318d 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ol) ){.         
2318e 20 6e 48 69 64 64 65 6e 2b 2b 3b 0a 20 20 20 20   nHidden++;.    
2318f 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
23190 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
23191 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
23192 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
23193 72 2c 20 69 2d 6e 48 69 64 64 65 6e 2c 20 31 29  r, i-nHidden, 1)
23194 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
23195 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
23196 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 32 2c  P_String8, 0, 2,
23197 20 30 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c   0, pCol->zName,
23198 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   0);.        sql
23199 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2319a 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
2319b 20 33 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20   3, 0,.         
2319c 20 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3f 20    pCol->zType ? 
2319d 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3a 20 22 22  pCol->zType : ""
2319e 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 0);.        sq
2319f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
231a0 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 28  v, OP_Integer, (
231a1 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c 20 3f 20  pCol->notNull ? 
231a2 31 20 3a 20 30 29 2c 20 34 29 3b 0a 20 20 20 20  1 : 0), 4);.    
231a3 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e 70 44      if( pCol->pD
231a4 66 6c 74 20 26 26 20 28 70 44 66 6c 74 20 3d 20  flt && (pDflt = 
231a5 26 70 43 6f 6c 2d 3e 70 44 66 6c 74 2d 3e 73 70  &pCol->pDflt->sp
231a6 61 6e 29 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20  an)->z ){.      
231a7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
231a8 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
231a9 6e 67 38 2c 20 30 2c 20 35 2c 20 30 2c 20 28 63  ng8, 0, 5, 0, (c
231aa 68 61 72 2a 29 70 44 66 6c 74 2d 3e 7a 2c 20 70  har*)pDflt->z, p
231ab 44 66 6c 74 2d 3e 6e 29 3b 0a 20 20 20 20 20 20  Dflt->n);.      
231ac 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
231ad 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
231ae 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
231af 20 30 2c 20 35 29 3b 0a 20 20 20 20 20 20 20 20   0, 5);.        
231b0 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
231b1 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
231b2 50 5f 49 6e 74 65 67 65 72 2c 20 70 43 6f 6c 2d  P_Integer, pCol-
231b3 3e 69 73 50 72 69 6d 4b 65 79 2c 20 36 29 3b 0a  >isPrimKey, 6);.
231b4 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
231b5 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
231b6 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 36 29  ResultRow, 1, 6)
231b7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
231b8 20 20 7d 65 6c 73 65 0a 0a 20 20 69 66 28 20 73    }else..  if( s
231b9 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
231ba 65 66 74 2c 20 22 69 6e 64 65 78 5f 69 6e 66 6f  eft, "index_info
231bb 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20  ")==0 && zRight 
231bc 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49  ){.    Index *pI
231bd 64 78 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70  dx;.    Table *p
231be 54 61 62 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  Tab;.    if( sql
231bf 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70  ite3ReadSchema(p
231c0 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72  Parse) ) goto pr
231c1 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70 49  agma_out;.    pI
231c2 64 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  dx = sqlite3Find
231c3 49 6e 64 65 78 28 64 62 2c 20 7a 52 69 67 68 74  Index(db, zRight
231c4 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20  , zDb);.    if( 
231c5 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20 69 6e  pIdx ){.      in
231c6 74 20 69 3b 0a 20 20 20 20 20 20 70 54 61 62 20  t i;.      pTab 
231c7 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 3b 0a  = pIdx->pTable;.
231c8 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
231c9 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 33  eSetNumCols(v, 3
231ca 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  );.      pParse-
231cb 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20 20  >nMem = 3;.     
231cc 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
231cd 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
231ce 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 6e  NAME_NAME, "seqn
231cf 6f 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  o", SQLITE_STATI
231d0 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  C);.      sqlite
231d1 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
231d2 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 1, COLNAME_NA
231d3 4d 45 2c 20 22 63 69 64 22 2c 20 53 51 4c 49 54  ME, "cid", SQLIT
231d4 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
231d5 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
231d6 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f 4c  olName(v, 2, COL
231d7 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65  NAME_NAME, "name
231d8 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
231d9 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  );.      for(i=0
231da 3b 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  ; i<pIdx->nColum
231db 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  n; i++){.       
231dc 20 69 6e 74 20 63 6e 75 6d 20 3d 20 70 49 64 78   int cnum = pIdx
231dd 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20  ->aiColumn[i];. 
231de 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
231df 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
231e0 6e 74 65 67 65 72 2c 20 69 2c 20 31 29 3b 0a 20  nteger, i, 1);. 
231e1 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
231e2 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
231e3 6e 74 65 67 65 72 2c 20 63 6e 75 6d 2c 20 32 29  nteger, cnum, 2)
231e4 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
231e5 28 20 70 54 61 62 2d 3e 6e 43 6f 6c 3e 63 6e 75  ( pTab->nCol>cnu
231e6 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  m );.        sql
231e7 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
231e8 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
231e9 20 33 2c 20 30 2c 20 70 54 61 62 2d 3e 61 43 6f   3, 0, pTab->aCo
231ea 6c 5b 63 6e 75 6d 5d 2e 7a 4e 61 6d 65 2c 20 30  l[cnum].zName, 0
231eb 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
231ec 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
231ed 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c  OP_ResultRow, 1,
231ee 20 33 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   3);.      }.   
231ef 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 69 66   }.  }else..  if
231f0 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
231f1 28 7a 4c 65 66 74 2c 20 22 69 6e 64 65 78 5f 6c  (zLeft, "index_l
231f2 69 73 74 22 29 3d 3d 30 20 26 26 20 7a 52 69 67  ist")==0 && zRig
231f3 68 74 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20  ht ){.    Index 
231f4 2a 70 49 64 78 3b 0a 20 20 20 20 54 61 62 6c 65  *pIdx;.    Table
231f5 20 2a 70 54 61 62 3b 0a 20 20 20 20 69 66 28 20   *pTab;.    if( 
231f6 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
231f7 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f  a(pParse) ) goto
231f8 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20   pragma_out;.   
231f9 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46   pTab = sqlite3F
231fa 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 52 69  indTable(db, zRi
231fb 67 68 74 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69  ght, zDb);.    i
231fc 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 20  f( pTab ){.     
231fd 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
231fe 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20  dbe(pParse);.   
231ff 20 20 20 70 49 64 78 20 3d 20 70 54 61 62 2d 3e     pIdx = pTab->
23200 70 49 6e 64 65 78 3b 0a 20 20 20 20 20 20 69 66  pIndex;.      if
23201 28 20 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20  ( pIdx ){.      
23202 20 20 69 6e 74 20 69 20 3d 20 30 3b 20 0a 20 20    int i = 0; .  
23203 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
23204 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 33  eSetNumCols(v, 3
23205 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73  );.        pPars
23206 65 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20  e->nMem = 3;.   
23207 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
23208 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
23209 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
2320a 73 65 71 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  seq", SQLITE_STA
2320b 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71  TIC);.        sq
2320c 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
2320d 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d  ame(v, 1, COLNAM
2320e 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22 2c 20  E_NAME, "name", 
2320f 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
23210 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
23211 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
23212 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   2, COLNAME_NAME
23213 2c 20 22 75 6e 69 71 75 65 22 2c 20 53 51 4c 49  , "unique", SQLI
23214 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
23215 20 20 20 20 77 68 69 6c 65 28 70 49 64 78 29 7b      while(pIdx){
23216 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
23217 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
23218 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 31  OP_Integer, i, 1
23219 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
2321a 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2321b 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
2321c 20 32 2c 20 30 2c 20 70 49 64 78 2d 3e 7a 4e 61   2, 0, pIdx->zNa
2321d 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  me, 0);.        
2321e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2321f 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
23220 72 2c 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72  r, pIdx->onError
23221 21 3d 4f 45 5f 4e 6f 6e 65 2c 20 33 29 3b 0a 20  !=OE_None, 3);. 
23222 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
23223 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
23224 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 33  _ResultRow, 1, 3
23225 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2b 2b 69  );.          ++i
23226 3b 0a 20 20 20 20 20 20 20 20 20 20 70 49 64 78  ;.          pIdx
23227 20 3d 20 70 49 64 78 2d 3e 70 4e 65 78 74 3b 0a   = pIdx->pNext;.
23228 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
23229 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  }.    }.  }else.
2322a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
2322b 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 64 61  rICmp(zLeft, "da
2322c 74 61 62 61 73 65 5f 6c 69 73 74 22 29 3d 3d 30  tabase_list")==0
2322d 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
2322e 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
2322f 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
23230 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f   ) goto pragma_o
23231 75 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ut;.    sqlite3V
23232 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
23233 20 33 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d   3);.    pParse-
23234 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20 73  >nMem = 3;.    s
23235 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
23236 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41  Name(v, 0, COLNA
23237 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22 2c 20  ME_NAME, "seq", 
23238 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
23239 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
2323a 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20  etColName(v, 1, 
2323b 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e  COLNAME_NAME, "n
2323c 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ame", SQLITE_STA
2323d 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  TIC);.    sqlite
2323e 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
2323f 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 2, COLNAME_NA
23240 4d 45 2c 20 22 66 69 6c 65 22 2c 20 53 51 4c 49  ME, "file", SQLI
23241 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
23242 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
23243 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  Db; i++){.      
23244 69 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70  if( db->aDb[i].p
23245 42 74 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  Bt==0 ) continue
23246 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
23247 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65  db->aDb[i].zName
23248 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  !=0 );.      sql
23249 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2324a 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c  , OP_Integer, i,
2324b 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   1);.      sqlit
2324c 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2324d 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 32  OP_String8, 0, 2
2324e 2c 20 30 2c 20 64 62 2d 3e 61 44 62 5b 69 5d 2e  , 0, db->aDb[i].
2324f 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20  zName, 0);.     
23250 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
23251 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
23252 2c 20 30 2c 20 33 2c 20 30 2c 0a 20 20 20 20 20  , 0, 3, 0,.     
23253 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
23254 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 64 62  eeGetFilename(db
23255 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 2c 20 30  ->aDb[i].pBt), 0
23256 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
23257 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
23258 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 33  _ResultRow, 1, 3
23259 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
2325a 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ..  if( sqlite3S
2325b 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 63  trICmp(zLeft, "c
2325c 6f 6c 6c 61 74 69 6f 6e 5f 6c 69 73 74 22 29 3d  ollation_list")=
2325d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 20  =0 ){.    int i 
2325e 3d 20 30 3b 0a 20 20 20 20 48 61 73 68 45 6c 65  = 0;.    HashEle
2325f 6d 20 2a 70 3b 0a 20 20 20 20 73 71 6c 69 74 65  m *p;.    sqlite
23260 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
23261 76 2c 20 32 29 3b 0a 20 20 20 20 70 50 61 72 73  v, 2);.    pPars
23262 65 2d 3e 6e 4d 65 6d 20 3d 20 32 3b 0a 20 20 20  e->nMem = 2;.   
23263 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
23264 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
23265 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22  NAME_NAME, "seq"
23266 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
23267 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
23268 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31  eSetColName(v, 1
23269 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
2326a 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53  "name", SQLITE_S
2326b 54 41 54 49 43 29 3b 0a 20 20 20 20 66 6f 72 28  TATIC);.    for(
2326c 70 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  p=sqliteHashFirs
2326d 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 29  t(&db->aCollSeq)
2326e 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65 48 61 73  ; p; p=sqliteHas
2326f 68 4e 65 78 74 28 70 29 29 7b 0a 20 20 20 20 20  hNext(p)){.     
23270 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20   CollSeq *pColl 
23271 3d 20 28 43 6f 6c 6c 53 65 71 20 2a 29 73 71 6c  = (CollSeq *)sql
23272 69 74 65 48 61 73 68 44 61 74 61 28 70 29 3b 0a  iteHashData(p);.
23273 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
23274 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
23275 74 65 67 65 72 2c 20 69 2b 2b 2c 20 31 29 3b 0a  teger, i++, 1);.
23276 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
23277 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74  eAddOp4(v, OP_St
23278 72 69 6e 67 38 2c 20 30 2c 20 32 2c 20 30 2c 20  ring8, 0, 2, 0, 
23279 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c 20 30 29  pColl->zName, 0)
2327a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2327b 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2327c 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 32 29  ResultRow, 1, 2)
2327d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  ;.    }.  }else.
2327e 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2327f 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 50 52 41  _OMIT_SCHEMA_PRA
23280 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  GMAS */..#ifndef
23281 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52   SQLITE_OMIT_FOR
23282 45 49 47 4e 5f 4b 45 59 0a 20 20 69 66 28 20 73  EIGN_KEY.  if( s
23283 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
23284 65 66 74 2c 20 22 66 6f 72 65 69 67 6e 5f 6b 65  eft, "foreign_ke
23285 79 5f 6c 69 73 74 22 29 3d 3d 30 20 26 26 20 7a  y_list")==0 && z
23286 52 69 67 68 74 20 29 7b 0a 20 20 20 20 46 4b 65  Right ){.    FKe
23287 79 20 2a 70 46 4b 3b 0a 20 20 20 20 54 61 62 6c  y *pFK;.    Tabl
23288 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 69 66 28  e *pTab;.    if(
23289 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
2328a 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74  ma(pParse) ) got
2328b 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20  o pragma_out;.  
2328c 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
2328d 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 52  FindTable(db, zR
2328e 69 67 68 74 2c 20 7a 44 62 29 3b 0a 20 20 20 20  ight, zDb);.    
2328f 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20  if( pTab ){.    
23290 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
23291 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
23292 20 20 20 20 70 46 4b 20 3d 20 70 54 61 62 2d 3e      pFK = pTab->
23293 70 46 4b 65 79 3b 0a 20 20 20 20 20 20 69 66 28  pFKey;.      if(
23294 20 70 46 4b 20 29 7b 0a 20 20 20 20 20 20 20 20   pFK ){.        
23295 69 6e 74 20 69 20 3d 20 30 3b 20 0a 20 20 20 20  int i = 0; .    
23296 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
23297 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 38 29 3b  etNumCols(v, 8);
23298 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d  .        pParse-
23299 3e 6e 4d 65 6d 20 3d 20 38 3b 0a 20 20 20 20 20  >nMem = 8;.     
2329a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
2329b 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
2329c 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 69 64  OLNAME_NAME, "id
2329d 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
2329e 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2329f 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
232a0 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 1, COLNAME_N
232a1 41 4d 45 2c 20 22 73 65 71 22 2c 20 53 51 4c 49  AME, "seq", SQLI
232a2 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
232a3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
232a4 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20  etColName(v, 2, 
232a5 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 74  COLNAME_NAME, "t
232a6 61 62 6c 65 22 2c 20 53 51 4c 49 54 45 5f 53 54  able", SQLITE_ST
232a7 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73  ATIC);.        s
232a8 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
232a9 4e 61 6d 65 28 76 2c 20 33 2c 20 43 4f 4c 4e 41  Name(v, 3, COLNA
232aa 4d 45 5f 4e 41 4d 45 2c 20 22 66 72 6f 6d 22 2c  ME_NAME, "from",
232ab 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
232ac 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
232ad 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
232ae 2c 20 34 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 4, COLNAME_NAM
232af 45 2c 20 22 74 6f 22 2c 20 53 51 4c 49 54 45 5f  E, "to", SQLITE_
232b0 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
232b1 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
232b2 6f 6c 4e 61 6d 65 28 76 2c 20 35 2c 20 43 4f 4c  olName(v, 5, COL
232b3 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6f 6e 5f 75  NAME_NAME, "on_u
232b4 70 64 61 74 65 22 2c 20 53 51 4c 49 54 45 5f 53  pdate", SQLITE_S
232b5 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
232b6 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
232b7 6c 4e 61 6d 65 28 76 2c 20 36 2c 20 43 4f 4c 4e  lName(v, 6, COLN
232b8 41 4d 45 5f 4e 41 4d 45 2c 20 22 6f 6e 5f 64 65  AME_NAME, "on_de
232b9 6c 65 74 65 22 2c 20 53 51 4c 49 54 45 5f 53 54  lete", SQLITE_ST
232ba 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73  ATIC);.        s
232bb 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
232bc 4e 61 6d 65 28 76 2c 20 37 2c 20 43 4f 4c 4e 41  Name(v, 7, COLNA
232bd 4d 45 5f 4e 41 4d 45 2c 20 22 6d 61 74 63 68 22  ME_NAME, "match"
232be 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
232bf 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  ;.        while(
232c0 70 46 4b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  pFK){.          
232c1 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 20  int j;.         
232c2 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 46 4b 2d   for(j=0; j<pFK-
232c3 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20  >nCol; j++){.   
232c4 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a           char *z
232c5 43 6f 6c 20 3d 20 70 46 4b 2d 3e 61 43 6f 6c 5b  Col = pFK->aCol[
232c6 6a 5d 2e 7a 43 6f 6c 3b 0a 20 20 20 20 20 20 20  j].zCol;.       
232c7 20 20 20 20 20 63 68 61 72 20 2a 7a 4f 6e 55 70       char *zOnUp
232c8 64 61 74 65 20 3d 20 28 63 68 61 72 20 2a 29 61  date = (char *)a
232c9 63 74 69 6f 6e 4e 61 6d 65 28 70 46 4b 2d 3e 75  ctionName(pFK->u
232ca 70 64 61 74 65 43 6f 6e 66 29 3b 0a 20 20 20 20  pdateConf);.    
232cb 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4f          char *zO
232cc 6e 44 65 6c 65 74 65 20 3d 20 28 63 68 61 72 20  nDelete = (char 
232cd 2a 29 61 63 74 69 6f 6e 4e 61 6d 65 28 70 46 4b  *)actionName(pFK
232ce 2d 3e 64 65 6c 65 74 65 43 6f 6e 66 29 3b 0a 20  ->deleteConf);. 
232cf 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
232d0 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
232d1 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 31  OP_Integer, i, 1
232d2 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  );.            s
232d3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
232d4 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
232d5 6a 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 20  j, 2);.         
232d6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
232d7 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
232d8 67 38 2c 20 30 2c 20 33 2c 20 30 2c 20 70 46 4b  g8, 0, 3, 0, pFK
232d9 2d 3e 7a 54 6f 2c 20 30 29 3b 0a 20 20 20 20 20  ->zTo, 0);.     
232da 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
232db 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
232dc 74 72 69 6e 67 38 2c 20 30 2c 20 34 2c 20 30 2c  tring8, 0, 4, 0,
232dd 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
232de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
232df 54 61 62 2d 3e 61 43 6f 6c 5b 70 46 4b 2d 3e 61  Tab->aCol[pFK->a
232e0 43 6f 6c 5b 6a 5d 2e 69 46 72 6f 6d 5d 2e 7a 4e  Col[j].iFrom].zN
232e1 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ame, 0);.       
232e2 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
232e3 41 64 64 4f 70 34 28 76 2c 20 7a 43 6f 6c 20 3f  AddOp4(v, zCol ?
232e4 20 4f 50 5f 53 74 72 69 6e 67 38 20 3a 20 4f 50   OP_String8 : OP
232e5 5f 4e 75 6c 6c 2c 20 30 2c 20 35 2c 20 30 2c 20  _Null, 0, 5, 0, 
232e6 7a 43 6f 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20  zCol, 0);.      
232e7 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
232e8 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74  eAddOp4(v, OP_St
232e9 72 69 6e 67 38 2c 20 30 2c 20 36 2c 20 30 2c 20  ring8, 0, 6, 0, 
232ea 7a 4f 6e 55 70 64 61 74 65 2c 20 30 29 3b 0a 20  zOnUpdate, 0);. 
232eb 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
232ec 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
232ed 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 37  OP_String8, 0, 7
232ee 2c 20 30 2c 20 7a 4f 6e 44 65 6c 65 74 65 2c 20  , 0, zOnDelete, 
232ef 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  0);.            
232f0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
232f1 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
232f2 20 30 2c 20 38 2c 20 30 2c 20 22 4e 4f 4e 45 22   0, 8, 0, "NONE"
232f3 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
232f4 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
232f5 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
232f6 52 6f 77 2c 20 31 2c 20 38 29 3b 0a 20 20 20 20  Row, 1, 8);.    
232f7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
232f8 20 20 2b 2b 69 3b 0a 20 20 20 20 20 20 20 20 20    ++i;.         
232f9 20 70 46 4b 20 3d 20 70 46 4b 2d 3e 70 4e 65 78   pFK = pFK->pNex
232fa 74 46 72 6f 6d 3b 0a 20 20 20 20 20 20 20 20 7d  tFrom;.        }
232fb 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
232fc 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a   }else.#endif /*
232fd 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
232fe 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45  _OMIT_FOREIGN_KE
232ff 59 29 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e  Y) */..#ifndef N
23300 44 45 42 55 47 0a 20 20 69 66 28 20 73 71 6c 69  DEBUG.  if( sqli
23301 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
23302 2c 20 22 70 61 72 73 65 72 5f 74 72 61 63 65 22  , "parser_trace"
23303 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  )==0 ){.    if( 
23304 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  zRight ){.      
23305 69 66 28 20 67 65 74 42 6f 6f 6c 65 61 6e 28 7a  if( getBoolean(z
23306 52 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20  Right) ){.      
23307 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54    sqlite3ParserT
23308 72 61 63 65 28 73 74 64 65 72 72 2c 20 22 70 61  race(stderr, "pa
23309 72 73 65 72 3a 20 22 29 3b 0a 20 20 20 20 20 20  rser: ");.      
2330a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
2330b 71 6c 69 74 65 33 50 61 72 73 65 72 54 72 61 63  qlite3ParserTrac
2330c 65 28 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d  e(0, 0);.      }
2330d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23  .    }.  }else.#
2330e 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 69 6e  endif..  /* Rein
2330f 73 74 61 6c 6c 20 74 68 65 20 4c 49 4b 45 20 61  stall the LIKE a
23310 6e 64 20 47 4c 4f 42 20 66 75 6e 63 74 69 6f 6e  nd GLOB function
23311 73 2e 20 20 54 68 65 20 76 61 72 69 61 6e 74 20  s.  The variant 
23312 6f 66 20 4c 49 4b 45 0a 20 20 2a 2a 20 75 73 65  of LIKE.  ** use
23313 64 20 77 69 6c 6c 20 62 65 20 63 61 73 65 20 73  d will be case s
23314 65 6e 73 69 74 69 76 65 20 6f 72 20 6e 6f 74 20  ensitive or not 
23315 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65  depending on the
23316 20 52 48 53 2e 0a 20 20 2a 2f 0a 20 20 69 66 28   RHS..  */.  if(
23317 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
23318 7a 4c 65 66 74 2c 20 22 63 61 73 65 5f 73 65 6e  zLeft, "case_sen
23319 73 69 74 69 76 65 5f 6c 69 6b 65 22 29 3d 3d 30  sitive_like")==0
2331a 20 29 7b 0a 20 20 20 20 69 66 28 20 7a 52 69 67   ){.    if( zRig
2331b 68 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ht ){.      sqli
2331c 74 65 33 52 65 67 69 73 74 65 72 4c 69 6b 65 46  te3RegisterLikeF
2331d 75 6e 63 74 69 6f 6e 73 28 64 62 2c 20 67 65 74  unctions(db, get
2331e 42 6f 6f 6c 65 61 6e 28 7a 52 69 67 68 74 29 29  Boolean(zRight))
2331f 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  ;.    }.  }else.
23320 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
23321 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 5f  INTEGRITY_CHECK_
23322 45 52 52 4f 52 5f 4d 41 58 0a 23 20 64 65 66 69  ERROR_MAX.# defi
23323 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 52  ne SQLITE_INTEGR
23324 49 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f 52 5f  ITY_CHECK_ERROR_
23325 4d 41 58 20 31 30 30 0a 23 65 6e 64 69 66 0a 0a  MAX 100.#endif..
23326 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
23327 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48  MIT_INTEGRITY_CH
23328 45 43 4b 0a 20 20 2f 2a 20 50 72 61 67 6d 61 20  ECK.  /* Pragma 
23329 22 71 75 69 63 6b 5f 63 68 65 63 6b 22 20 69 73  "quick_check" is
2332a 20 61 6e 20 65 78 70 65 72 69 6d 65 6e 74 61 6c   an experimental
2332b 20 72 65 64 75 63 65 64 20 76 65 72 73 69 6f 6e   reduced version
2332c 20 6f 66 20 0a 20 20 2a 2a 20 69 6e 74 65 67 72   of .  ** integr
2332d 69 74 79 5f 63 68 65 63 6b 20 64 65 73 69 67 6e  ity_check design
2332e 65 64 20 74 6f 20 64 65 74 65 63 74 20 6d 6f 73  ed to detect mos
2332f 74 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75  t database corru
23330 70 74 69 6f 6e 0a 20 20 2a 2a 20 77 69 74 68 6f  ption.  ** witho
23331 75 74 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 6f  ut most of the o
23332 76 65 72 68 65 61 64 20 6f 66 20 61 20 66 75 6c  verhead of a ful
23333 6c 20 69 6e 74 65 67 72 69 74 79 2d 63 68 65 63  l integrity-chec
23334 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  k..  */.  if( sq
23335 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
23336 66 74 2c 20 22 69 6e 74 65 67 72 69 74 79 5f 63  ft, "integrity_c
23337 68 65 63 6b 22 29 3d 3d 30 0a 20 20 20 7c 7c 20  heck")==0.   || 
23338 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
23339 4c 65 66 74 2c 20 22 71 75 69 63 6b 5f 63 68 65  Left, "quick_che
2333a 63 6b 22 29 3d 3d 30 20 0a 20 20 29 7b 0a 20 20  ck")==0 .  ){.  
2333b 20 20 69 6e 74 20 69 2c 20 6a 2c 20 61 64 64 72    int i, j, addr
2333c 2c 20 6d 78 45 72 72 3b 0a 0a 20 20 20 20 2f 2a  , mxErr;..    /*
2333d 20 43 6f 64 65 20 74 68 61 74 20 61 70 70 65 61   Code that appea
2333e 72 73 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  rs at the end of
2333f 20 74 68 65 20 69 6e 74 65 67 72 69 74 79 20 63   the integrity c
23340 68 65 63 6b 2e 20 20 49 66 20 6e 6f 20 65 72 72  heck.  If no err
23341 6f 72 0a 20 20 20 20 2a 2a 20 6d 65 73 73 61 67  or.    ** messag
23342 65 73 20 68 61 76 65 20 62 65 65 6e 20 67 65 6e  es have been gen
23343 65 72 61 74 65 64 2c 20 6f 75 74 70 75 74 20 4f  erated, output O
23344 4b 2e 20 20 4f 74 68 65 72 77 69 73 65 20 6f 75  K.  Otherwise ou
23345 74 70 75 74 20 74 68 65 0a 20 20 20 20 2a 2a 20  tput the.    ** 
23346 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20  error message.  
23347 20 20 2a 2f 0a 20 20 20 20 73 74 61 74 69 63 20    */.    static 
23348 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74  const VdbeOpList
23349 20 65 6e 64 43 6f 64 65 5b 5d 20 3d 20 7b 0a 20   endCode[] = {. 
2334a 20 20 20 20 20 7b 20 4f 50 5f 41 64 64 49 6d 6d       { OP_AddImm
2334b 2c 20 20 20 20 20 20 31 2c 20 30 2c 20 20 20 20  ,      1, 0,    
2334c 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20      0},    /* 0 
2334d 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49 66  */.      { OP_If
2334e 4e 65 67 2c 20 20 20 20 20 20 20 31 2c 20 30 2c  Neg,       1, 0,
2334f 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20 2f          0},    /
23350 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f  * 1 */.      { O
23351 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20 30  P_String8,     0
23352 2c 20 33 2c 20 20 20 20 20 20 20 20 30 7d 2c 20  , 3,        0}, 
23353 20 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 20     /* 2 */.     
23354 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c   { OP_ResultRow,
23355 20 20 20 33 2c 20 31 2c 20 20 20 20 20 20 20 20     3, 1,        
23356 30 7d 2c 0a 20 20 20 20 7d 3b 0a 0a 20 20 20 20  0},.    };..    
23357 69 6e 74 20 69 73 51 75 69 63 6b 20 3d 20 28 7a  int isQuick = (z
23358 4c 65 66 74 5b 30 5d 3d 3d 27 71 27 29 3b 0a 0a  Left[0]=='q');..
23359 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a      /* Initializ
2335a 65 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72  e the VDBE progr
2335b 61 6d 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71  am */.    if( sq
2335c 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
2335d 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70  pParse) ) goto p
2335e 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70  ragma_out;.    p
2335f 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 36 3b  Parse->nMem = 6;
23360 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
23361 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29  SetNumCols(v, 1)
23362 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
23363 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30  eSetColName(v, 0
23364 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
23365 22 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b  "integrity_check
23366 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
23367 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74  );..    /* Set t
23368 68 65 20 6d 61 78 69 6d 75 6d 20 65 72 72 6f 72  he maximum error
23369 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 6d 78   count */.    mx
2336a 45 72 72 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54  Err = SQLITE_INT
2336b 45 47 52 49 54 59 5f 43 48 45 43 4b 5f 45 52 52  EGRITY_CHECK_ERR
2336c 4f 52 5f 4d 41 58 3b 0a 20 20 20 20 69 66 28 20  OR_MAX;.    if( 
2336d 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  zRight ){.      
2336e 6d 78 45 72 72 20 3d 20 61 74 6f 69 28 7a 52 69  mxErr = atoi(zRi
2336f 67 68 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ght);.      if( 
23370 6d 78 45 72 72 3c 3d 30 20 29 7b 0a 20 20 20 20  mxErr<=0 ){.    
23371 20 20 20 20 6d 78 45 72 72 20 3d 20 53 51 4c 49      mxErr = SQLI
23372 54 45 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  TE_INTEGRITY_CHE
23373 43 4b 5f 45 52 52 4f 52 5f 4d 41 58 3b 0a 20 20  CK_ERROR_MAX;.  
23374 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
23375 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23376 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
23377 20 6d 78 45 72 72 2c 20 31 29 3b 20 20 2f 2a 20   mxErr, 1);  /* 
23378 72 65 67 5b 31 5d 20 68 6f 6c 64 73 20 65 72 72  reg[1] holds err
23379 6f 72 73 20 6c 65 66 74 20 2a 2f 0a 0a 20 20 20  ors left */..   
2337a 20 2f 2a 20 44 6f 20 61 6e 20 69 6e 74 65 67 72   /* Do an integr
2337b 69 74 79 20 63 68 65 63 6b 20 6f 6e 20 65 61 63  ity check on eac
2337c 68 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  h database file 
2337d 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  */.    for(i=0; 
2337e 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
2337f 0a 20 20 20 20 20 20 48 61 73 68 45 6c 65 6d 20  .      HashElem 
23380 2a 78 3b 0a 20 20 20 20 20 20 48 61 73 68 20 2a  *x;.      Hash *
23381 70 54 62 6c 73 3b 0a 20 20 20 20 20 20 69 6e 74  pTbls;.      int
23382 20 63 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20 20   cnt = 0;..     
23383 20 69 66 28 20 4f 4d 49 54 5f 54 45 4d 50 44 42   if( OMIT_TEMPDB
23384 20 26 26 20 69 3d 3d 31 20 29 20 63 6f 6e 74 69   && i==1 ) conti
23385 6e 75 65 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69  nue;..      sqli
23386 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68  te3CodeVerifySch
23387 65 6d 61 28 70 50 61 72 73 65 2c 20 69 29 3b 0a  ema(pParse, i);.
23388 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
23389 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2338a 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 31 29 3b 20  , OP_IfPos, 1); 
2338b 2f 2a 20 48 61 6c 74 20 69 66 20 6f 75 74 20 6f  /* Halt if out o
2338c 66 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20 20  f errors */.    
2338d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2338e 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20  Op2(v, OP_Halt, 
2338f 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c  0, 0);.      sql
23390 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
23391 28 76 2c 20 61 64 64 72 29 3b 0a 0a 20 20 20 20  (v, addr);..    
23392 20 20 2f 2a 20 44 6f 20 61 6e 20 69 6e 74 65 67    /* Do an integ
23393 72 69 74 79 20 63 68 65 63 6b 20 6f 66 20 74 68  rity check of th
23394 65 20 42 2d 54 72 65 65 0a 20 20 20 20 20 20 2a  e B-Tree.      *
23395 2a 0a 20 20 20 20 20 20 2a 2a 20 42 65 67 69 6e  *.      ** Begin
23396 20 62 79 20 66 69 6c 6c 69 6e 67 20 72 65 67 69   by filling regi
23397 73 74 65 72 73 20 32 2c 20 33 2c 20 2e 2e 2e 20  sters 2, 3, ... 
23398 77 69 74 68 20 74 68 65 20 72 6f 6f 74 20 70 61  with the root pa
23399 67 65 73 20 6e 75 6d 62 65 72 73 0a 20 20 20 20  ges numbers.    
2339a 20 20 2a 2a 20 66 6f 72 20 61 6c 6c 20 74 61 62    ** for all tab
2339b 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73 20  les and indices 
2339c 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  in the database.
2339d 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2339e 70 54 62 6c 73 20 3d 20 26 64 62 2d 3e 61 44 62  pTbls = &db->aDb
2339f 5b 69 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c  [i].pSchema->tbl
233a0 48 61 73 68 3b 0a 20 20 20 20 20 20 66 6f 72 28  Hash;.      for(
233a1 78 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  x=sqliteHashFirs
233a2 74 28 70 54 62 6c 73 29 3b 20 78 3b 20 78 3d 73  t(pTbls); x; x=s
233a3 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 78 29  qliteHashNext(x)
233a4 29 7b 0a 20 20 20 20 20 20 20 20 54 61 62 6c 65  ){.        Table
233a5 20 2a 70 54 61 62 20 3d 20 73 71 6c 69 74 65 48   *pTab = sqliteH
233a6 61 73 68 44 61 74 61 28 78 29 3b 0a 20 20 20 20  ashData(x);.    
233a7 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b      Index *pIdx;
233a8 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
233a9 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
233aa 5f 49 6e 74 65 67 65 72 2c 20 70 54 61 62 2d 3e  _Integer, pTab->
233ab 74 6e 75 6d 2c 20 32 2b 63 6e 74 29 3b 0a 20 20  tnum, 2+cnt);.  
233ac 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20        cnt++;.   
233ad 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54       for(pIdx=pT
233ae 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78  ab->pIndex; pIdx
233af 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65  ; pIdx=pIdx->pNe
233b0 78 74 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  xt){.          s
233b1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
233b2 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
233b3 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 32 2b 63 6e  pIdx->tnum, 2+cn
233b4 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6e  t);.          cn
233b5 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  t++;.        }. 
233b6 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
233b7 20 63 6e 74 3d 3d 30 20 29 20 63 6f 6e 74 69 6e   cnt==0 ) contin
233b8 75 65 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4d 61  ue;..      /* Ma
233b9 6b 65 20 73 75 72 65 20 73 75 66 66 69 63 69 65  ke sure sufficie
233ba 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 67  nt number of reg
233bb 69 73 74 65 72 73 20 68 61 76 65 20 62 65 65 6e  isters have been
233bc 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20   allocated */.  
233bd 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e      if( pParse->
233be 6e 4d 65 6d 20 3c 20 63 6e 74 2b 34 20 29 7b 0a  nMem < cnt+4 ){.
233bf 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e          pParse->
233c0 6e 4d 65 6d 20 3d 20 63 6e 74 2b 34 3b 0a 20 20  nMem = cnt+4;.  
233c1 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
233c2 44 6f 20 74 68 65 20 62 2d 74 72 65 65 20 69 6e  Do the b-tree in
233c3 74 65 67 72 69 74 79 20 63 68 65 63 6b 73 20 2a  tegrity checks *
233c4 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  /.      sqlite3V
233c5 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
233c6 49 6e 74 65 67 72 69 74 79 43 6b 2c 20 32 2c 20  IntegrityCk, 2, 
233c7 63 6e 74 2c 20 31 29 3b 0a 20 20 20 20 20 20 73  cnt, 1);.      s
233c8 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
233c9 50 35 28 76 2c 20 69 29 3b 0a 20 20 20 20 20 20  P5(v, i);.      
233ca 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
233cb 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
233cc 73 4e 75 6c 6c 2c 20 32 29 3b 0a 20 20 20 20 20  sNull, 2);.     
233cd 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
233ce 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
233cf 2c 20 30 2c 20 33 2c 20 30 2c 0a 20 20 20 20 20  , 0, 3, 0,.     
233d0 20 20 20 20 73 71 6c 69 74 65 33 4d 50 72 69 6e      sqlite3MPrin
233d1 74 66 28 64 62 2c 20 22 2a 2a 2a 20 69 6e 20 64  tf(db, "*** in d
233d2 61 74 61 62 61 73 65 20 25 73 20 2a 2a 2a 5c 6e  atabase %s ***\n
233d3 22 2c 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e  ", db->aDb[i].zN
233d4 61 6d 65 29 2c 0a 20 20 20 20 20 20 20 20 20 50  ame),.         P
233d5 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20  4_DYNAMIC);.    
233d6 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
233d7 4f 70 33 28 76 2c 20 4f 50 5f 4d 6f 76 65 2c 20  Op3(v, OP_Move, 
233d8 32 2c 20 34 2c 20 31 29 3b 0a 20 20 20 20 20 20  2, 4, 1);.      
233d9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
233da 33 28 76 2c 20 4f 50 5f 43 6f 6e 63 61 74 2c 20  3(v, OP_Concat, 
233db 34 2c 20 33 2c 20 32 29 3b 0a 20 20 20 20 20 20  4, 3, 2);.      
233dc 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
233dd 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f  2(v, OP_ResultRo
233de 77 2c 20 32 2c 20 31 29 3b 0a 20 20 20 20 20 20  w, 2, 1);.      
233df 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
233e0 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 0a 20  ere(v, addr);.. 
233e1 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72       /* Make sur
233e2 65 20 61 6c 6c 20 74 68 65 20 69 6e 64 69 63 65  e all the indice
233e3 73 20 61 72 65 20 63 6f 6e 73 74 72 75 63 74 65  s are constructe
233e4 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 20 20 20  d correctly..   
233e5 20 20 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28     */.      for(
233e6 78 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  x=sqliteHashFirs
233e7 74 28 70 54 62 6c 73 29 3b 20 78 20 26 26 20 21  t(pTbls); x && !
233e8 69 73 51 75 69 63 6b 3b 20 78 3d 73 71 6c 69 74  isQuick; x=sqlit
233e9 65 48 61 73 68 4e 65 78 74 28 78 29 29 7b 0a 20  eHashNext(x)){. 
233ea 20 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54         Table *pT
233eb 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44  ab = sqliteHashD
233ec 61 74 61 28 78 29 3b 0a 20 20 20 20 20 20 20 20  ata(x);.        
233ed 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20  Index *pIdx;.   
233ee 20 20 20 20 20 69 6e 74 20 6c 6f 6f 70 54 6f 70       int loopTop
233ef 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ;..        if( p
233f0 54 61 62 2d 3e 70 49 6e 64 65 78 3d 3d 30 20 29  Tab->pIndex==0 )
233f1 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
233f2 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
233f3 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
233f4 50 5f 49 66 50 6f 73 2c 20 31 29 3b 20 20 2f 2a  P_IfPos, 1);  /*
233f5 20 53 74 6f 70 20 69 66 20 6f 75 74 20 6f 66 20   Stop if out of 
233f6 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20 20 20 20  errors */.      
233f7 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
233f8 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20  Op2(v, OP_Halt, 
233f9 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73  0, 0);.        s
233fa 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
233fb 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 20  re(v, addr);.   
233fc 20 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e       sqlite3Open
233fd 54 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28  TableAndIndices(
233fe 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 31 2c  pParse, pTab, 1,
233ff 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20   OP_OpenRead);. 
23400 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
23401 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
23402 6e 74 65 67 65 72 2c 20 30 2c 20 32 29 3b 20 20  nteger, 0, 2);  
23403 2f 2a 20 72 65 67 28 32 29 20 77 69 6c 6c 20 63  /* reg(2) will c
23404 6f 75 6e 74 20 65 6e 74 72 69 65 73 20 2a 2f 0a  ount entries */.
23405 20 20 20 20 20 20 20 20 6c 6f 6f 70 54 6f 70 20          loopTop 
23406 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
23407 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64  Op2(v, OP_Rewind
23408 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  , 1, 0);.       
23409 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2340a 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c  p2(v, OP_AddImm,
2340b 20 32 2c 20 31 29 3b 20 20 20 2f 2a 20 69 6e 63   2, 1);   /* inc
2340c 72 65 6d 65 6e 74 20 65 6e 74 72 79 20 63 6f 75  rement entry cou
2340d 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f  nt */.        fo
2340e 72 28 6a 3d 30 2c 20 70 49 64 78 3d 70 54 61 62  r(j=0, pIdx=pTab
2340f 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
23410 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
23411 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  , j++){.        
23412 20 20 69 6e 74 20 6a 6d 70 32 3b 0a 20 20 20 20    int jmp2;.    
23413 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e        static con
23414 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 69 64  st VdbeOpList id
23415 78 45 72 72 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  xErr[] = {.     
23416 20 20 20 20 20 20 20 7b 20 4f 50 5f 41 64 64 49         { OP_AddI
23417 6d 6d 2c 20 20 20 20 20 20 31 2c 20 2d 31 2c 20  mm,      1, -1, 
23418 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20   0},.           
23419 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20   { OP_String8,  
2341a 20 20 20 30 2c 20 20 33 2c 20 20 30 7d 2c 20 20     0,  3,  0},  
2341b 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20    /* 1 */.      
2341c 20 20 20 20 20 20 7b 20 4f 50 5f 52 6f 77 69 64        { OP_Rowid
2341d 2c 20 20 20 20 20 20 20 31 2c 20 20 34 2c 20 20  ,       1,  4,  
2341e 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  0},.            
2341f 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20  { OP_String8,   
23420 20 20 30 2c 20 20 35 2c 20 20 30 7d 2c 20 20 20    0,  5,  0},   
23421 20 2f 2a 20 33 20 2a 2f 0a 20 20 20 20 20 20 20   /* 3 */.       
23422 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67       { OP_String
23423 38 2c 20 20 20 20 20 30 2c 20 20 36 2c 20 20 30  8,     0,  6,  0
23424 7d 2c 20 20 20 20 2f 2a 20 34 20 2a 2f 0a 20 20  },    /* 4 */.  
23425 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 43            { OP_C
23426 6f 6e 63 61 74 2c 20 20 20 20 20 20 34 2c 20 20  oncat,      4,  
23427 33 2c 20 20 33 7d 2c 0a 20 20 20 20 20 20 20 20  3,  3},.        
23428 20 20 20 20 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c      { OP_Concat,
23429 20 20 20 20 20 20 35 2c 20 20 33 2c 20 20 33 7d        5,  3,  3}
2342a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  ,.            { 
2342b 4f 50 5f 43 6f 6e 63 61 74 2c 20 20 20 20 20 20  OP_Concat,      
2342c 36 2c 20 20 33 2c 20 20 33 7d 2c 0a 20 20 20 20  6,  3,  3},.    
2342d 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 73          { OP_Res
2342e 75 6c 74 52 6f 77 2c 20 20 20 33 2c 20 20 31 2c  ultRow,   3,  1,
2342f 20 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20    0},.          
23430 20 20 7b 20 4f 50 5f 49 66 50 6f 73 2c 20 20 20    { OP_IfPos,   
23431 20 20 20 20 31 2c 20 20 30 2c 20 20 30 7d 2c 20      1,  0,  0}, 
23432 20 20 20 2f 2a 20 39 20 2a 2f 0a 20 20 20 20 20     /* 9 */.     
23433 20 20 20 20 20 20 20 7b 20 4f 50 5f 48 61 6c 74         { OP_Halt
23434 2c 20 20 20 20 20 20 20 20 30 2c 20 20 30 2c 20  ,        0,  0, 
23435 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 7d   0},.          }
23436 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
23437 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78  te3GenerateIndex
23438 4b 65 79 28 70 50 61 72 73 65 2c 20 70 49 64 78  Key(pParse, pIdx
23439 2c 20 31 2c 20 33 2c 20 31 29 3b 0a 20 20 20 20  , 1, 3, 1);.    
2343a 20 20 20 20 20 20 6a 6d 70 32 20 3d 20 73 71 6c        jmp2 = sql
2343b 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2343c 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 6a 2b 32 2c  , OP_Found, j+2,
2343d 20 30 2c 20 33 29 3b 0a 20 20 20 20 20 20 20 20   0, 3);.        
2343e 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
2343f 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c  VdbeAddOpList(v,
23440 20 41 72 72 61 79 53 69 7a 65 28 69 64 78 45 72   ArraySize(idxEr
23441 72 29 2c 20 69 64 78 45 72 72 29 3b 0a 20 20 20  r), idxErr);.   
23442 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
23443 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64  beChangeP4(v, ad
23444 64 72 2b 31 2c 20 22 72 6f 77 69 64 20 22 2c 20  dr+1, "rowid ", 
23445 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P4_STATIC);.    
23446 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
23447 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64  eChangeP4(v, add
23448 72 2b 33 2c 20 22 20 6d 69 73 73 69 6e 67 20 66  r+3, " missing f
23449 72 6f 6d 20 69 6e 64 65 78 20 22 2c 20 50 34 5f  rom index ", P4_
2344a 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
2344b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
2344c 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2b 34  angeP4(v, addr+4
2344d 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 50  , pIdx->zName, P
2344e 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  4_STATIC);.     
2344f 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
23450 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
23451 2b 39 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  +9);.          s
23452 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
23453 72 65 28 76 2c 20 6a 6d 70 32 29 3b 0a 20 20 20  re(v, jmp2);.   
23454 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73       }.        s
23455 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
23456 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 31 2c 20  (v, OP_Next, 1, 
23457 6c 6f 6f 70 54 6f 70 2b 31 29 3b 0a 20 20 20 20  loopTop+1);.    
23458 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
23459 75 6d 70 48 65 72 65 28 76 2c 20 6c 6f 6f 70 54  umpHere(v, loopT
2345a 6f 70 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  op);.        for
2345b 28 6a 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d  (j=0, pIdx=pTab-
2345c 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
2345d 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c  Idx=pIdx->pNext,
2345e 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   j++){.         
2345f 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64   static const Vd
23460 62 65 4f 70 4c 69 73 74 20 63 6e 74 49 64 78 5b  beOpList cntIdx[
23461 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20  ] = {.          
23462 20 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c     { OP_Integer,
23463 20 20 20 20 20 20 30 2c 20 20 33 2c 20 20 30 7d        0,  3,  0}
23464 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7b  ,.             {
23465 20 4f 50 5f 52 65 77 69 6e 64 2c 20 20 20 20 20   OP_Rewind,     
23466 20 20 30 2c 20 20 30 2c 20 20 30 7d 2c 20 20 2f    0,  0,  0},  /
23467 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  * 1 */.         
23468 20 20 20 20 7b 20 4f 50 5f 41 64 64 49 6d 6d 2c      { OP_AddImm,
23469 20 20 20 20 20 20 20 33 2c 20 20 31 2c 20 20 30         3,  1,  0
2346a 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  },.             
2346b 7b 20 4f 50 5f 4e 65 78 74 2c 20 20 20 20 20 20  { OP_Next,      
2346c 20 20 20 30 2c 20 20 30 2c 20 20 30 7d 2c 20 20     0,  0,  0},  
2346d 2f 2a 20 33 20 2a 2f 0a 20 20 20 20 20 20 20 20  /* 3 */.        
2346e 20 20 20 20 20 7b 20 4f 50 5f 45 71 2c 20 20 20       { OP_Eq,   
2346f 20 20 20 20 20 20 20 20 32 2c 20 20 30 2c 20 20          2,  0,  
23470 33 7d 2c 20 20 2f 2a 20 34 20 2a 2f 0a 20 20 20  3},  /* 4 */.   
23471 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 41            { OP_A
23472 64 64 49 6d 6d 2c 20 20 20 20 20 20 20 31 2c 20  ddImm,       1, 
23473 2d 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20  -1,  0},.       
23474 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e        { OP_Strin
23475 67 38 2c 20 20 20 20 20 20 30 2c 20 20 32 2c 20  g8,      0,  2, 
23476 20 30 7d 2c 20 20 2f 2a 20 36 20 2a 2f 0a 20 20   0},  /* 6 */.  
23477 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
23478 53 74 72 69 6e 67 38 2c 20 20 20 20 20 20 30 2c  String8,      0,
23479 20 20 33 2c 20 20 30 7d 2c 20 20 2f 2a 20 37 20    3,  0},  /* 7 
2347a 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  */.             
2347b 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 20 20 20  { OP_Concat,    
2347c 20 20 20 33 2c 20 20 32 2c 20 20 32 7d 2c 0a 20     3,  2,  2},. 
2347d 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
2347e 5f 52 65 73 75 6c 74 52 6f 77 2c 20 20 20 20 32  _ResultRow,    2
2347f 2c 20 20 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20  ,  1,  0},.     
23480 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 20 20       };.        
23481 20 20 69 66 28 20 70 49 64 78 2d 3e 74 6e 75 6d    if( pIdx->tnum
23482 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==0 ) continue;.
23483 20 20 20 20 20 20 20 20 20 20 61 64 64 72 20 3d            addr =
23484 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
23485 70 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20  p1(v, OP_IfPos, 
23486 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  1);.          sq
23487 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
23488 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30  v, OP_Halt, 0, 0
23489 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
2348a 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
2348b 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 20  (v, addr);.     
2348c 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69       addr = sqli
2348d 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
2348e 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28 63 6e  (v, ArraySize(cn
2348f 74 49 64 78 29 2c 20 63 6e 74 49 64 78 29 3b 0a  tIdx), cntIdx);.
23490 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
23491 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c  3VdbeChangeP1(v,
23492 20 61 64 64 72 2b 31 2c 20 6a 2b 32 29 3b 0a 20   addr+1, j+2);. 
23493 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
23494 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20  VdbeChangeP2(v, 
23495 61 64 64 72 2b 31 2c 20 61 64 64 72 2b 34 29 3b  addr+1, addr+4);
23496 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
23497 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76  e3VdbeChangeP1(v
23498 2c 20 61 64 64 72 2b 33 2c 20 6a 2b 32 29 3b 0a  , addr+3, j+2);.
23499 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2349a 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c  3VdbeChangeP2(v,
2349b 20 61 64 64 72 2b 33 2c 20 61 64 64 72 2b 32 29   addr+3, addr+2)
2349c 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
2349d 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
2349e 76 2c 20 61 64 64 72 2b 34 29 3b 0a 20 20 20 20  v, addr+4);.    
2349f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
234a0 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64  eChangeP4(v, add
234a1 72 2b 36 2c 20 0a 20 20 20 20 20 20 20 20 20 20  r+6, .          
234a2 20 20 20 20 20 20 20 20 20 20 20 22 77 72 6f 6e             "wron
234a3 67 20 23 20 6f 66 20 65 6e 74 72 69 65 73 20 69  g # of entries i
234a4 6e 20 69 6e 64 65 78 20 22 2c 20 50 34 5f 53 54  n index ", P4_ST
234a5 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 20  ATIC);.         
234a6 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
234a7 67 65 50 34 28 76 2c 20 61 64 64 72 2b 37 2c 20  geP4(v, addr+7, 
234a8 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f  pIdx->zName, P4_
234a9 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
234aa 20 7d 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20   }.      } .    
234ab 7d 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c  }.    addr = sql
234ac 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73  ite3VdbeAddOpLis
234ad 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28 65  t(v, ArraySize(e
234ae 6e 64 43 6f 64 65 29 2c 20 65 6e 64 43 6f 64 65  ndCode), endCode
234af 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
234b0 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 61 64  beChangeP2(v, ad
234b1 64 72 2c 20 2d 6d 78 45 72 72 29 3b 0a 20 20 20  dr, -mxErr);.   
234b2 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
234b3 48 65 72 65 28 76 2c 20 61 64 64 72 2b 31 29 3b  Here(v, addr+1);
234b4 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
234b5 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72  ChangeP4(v, addr
234b6 2b 32 2c 20 22 6f 6b 22 2c 20 50 34 5f 53 54 41  +2, "ok", P4_STA
234b7 54 49 43 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65  TIC);.  }else.#e
234b8 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
234b9 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48  MIT_INTEGRITY_CH
234ba 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  ECK */..#ifndef 
234bb 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
234bc 36 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52  6.  /*.  **   PR
234bd 41 47 4d 41 20 65 6e 63 6f 64 69 6e 67 0a 20 20  AGMA encoding.  
234be 2a 2a 20 20 20 50 52 41 47 4d 41 20 65 6e 63 6f  **   PRAGMA enco
234bf 64 69 6e 67 20 3d 20 22 75 74 66 2d 38 22 7c 22  ding = "utf-8"|"
234c0 75 74 66 2d 31 36 22 7c 22 75 74 66 2d 31 36 6c  utf-16"|"utf-16l
234c1 65 22 7c 22 75 74 66 2d 31 36 62 65 22 0a 20 20  e"|"utf-16be".  
234c2 2a 2a 0a 20 20 2a 2a 20 49 6e 20 69 74 73 20 66  **.  ** In its f
234c3 69 72 73 74 20 66 6f 72 6d 2c 20 74 68 69 73 20  irst form, this 
234c4 70 72 61 67 6d 61 20 72 65 74 75 72 6e 73 20 74  pragma returns t
234c5 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74  he encoding of t
234c6 68 65 20 6d 61 69 6e 0a 20 20 2a 2a 20 64 61 74  he main.  ** dat
234c7 61 62 61 73 65 2e 20 49 66 20 74 68 65 20 64 61  abase. If the da
234c8 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 69 6e  tabase is not in
234c9 69 74 69 61 6c 69 7a 65 64 2c 20 69 74 20 69 73  itialized, it is
234ca 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 6e 6f 77   initialized now
234cb 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  **.  ** The 
234cc 73 65 63 6f 6e 64 20 66 6f 72 6d 20 6f 66 20 74  second form of t
234cd 68 69 73 20 70 72 61 67 6d 61 20 69 73 20 61 20  his pragma is a 
234ce 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 6d 61 69  no-op if the mai
234cf 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a  n database file.
234d0 20 20 2a 2a 20 68 61 73 20 6e 6f 74 20 61 6c 72    ** has not alr
234d1 65 61 64 79 20 62 65 65 6e 20 69 6e 69 74 69 61  eady been initia
234d2 6c 69 7a 65 64 2e 20 49 6e 20 74 68 69 73 20 63  lized. In this c
234d3 61 73 65 20 69 74 20 73 65 74 73 20 74 68 65 20  ase it sets the 
234d4 64 65 66 61 75 6c 74 0a 20 20 2a 2a 20 65 6e 63  default.  ** enc
234d5 6f 64 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20  oding that will 
234d6 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 20  be used for the 
234d7 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
234d8 6c 65 20 69 66 20 61 20 6e 65 77 20 66 69 6c 65  le if a new file
234d9 0a 20 20 2a 2a 20 69 73 20 63 72 65 61 74 65 64  .  ** is created
234da 2e 20 49 66 20 61 6e 20 65 78 69 73 74 69 6e 67  . If an existing
234db 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66   main database f
234dc 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 2c 20 74  ile is opened, t
234dd 68 65 6e 20 74 68 65 0a 20 20 2a 2a 20 64 65 66  hen the.  ** def
234de 61 75 6c 74 20 74 65 78 74 20 65 6e 63 6f 64 69  ault text encodi
234df 6e 67 20 66 6f 72 20 74 68 65 20 65 78 69 73 74  ng for the exist
234e0 69 6e 67 20 64 61 74 61 62 61 73 65 20 69 73 20  ing database is 
234e1 75 73 65 64 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a  used..  ** .  **
234e2 20 49 6e 20 61 6c 6c 20 63 61 73 65 73 20 6e 65   In all cases ne
234e3 77 20 64 61 74 61 62 61 73 65 73 20 63 72 65 61  w databases crea
234e4 74 65 64 20 75 73 69 6e 67 20 74 68 65 20 41 54  ted using the AT
234e5 54 41 43 48 20 63 6f 6d 6d 61 6e 64 20 61 72 65  TACH command are
234e6 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20 74 6f  .  ** created to
234e7 20 75 73 65 20 74 68 65 20 73 61 6d 65 20 64 65   use the same de
234e8 66 61 75 6c 74 20 74 65 78 74 20 65 6e 63 6f 64  fault text encod
234e9 69 6e 67 20 61 73 20 74 68 65 20 6d 61 69 6e 20  ing as the main 
234ea 64 61 74 61 62 61 73 65 2e 20 49 66 0a 20 20 2a  database. If.  *
234eb 2a 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  * the main datab
234ec 61 73 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e  ase has not been
234ed 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 61 6e 64   initialized and
234ee 2f 6f 72 20 63 72 65 61 74 65 64 20 77 68 65 6e  /or created when
234ef 20 41 54 54 41 43 48 0a 20 20 2a 2a 20 69 73 20   ATTACH.  ** is 
234f0 65 78 65 63 75 74 65 64 2c 20 74 68 69 73 20 69  executed, this i
234f1 73 20 64 6f 6e 65 20 62 65 66 6f 72 65 20 74 68  s done before th
234f2 65 20 41 54 54 41 43 48 20 6f 70 65 72 61 74 69  e ATTACH operati
234f3 6f 6e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e  on..  **.  ** In
234f4 20 74 68 65 20 73 65 63 6f 6e 64 20 66 6f 72 6d   the second form
234f5 20 74 68 69 73 20 70 72 61 67 6d 61 20 73 65 74   this pragma set
234f6 73 20 74 68 65 20 74 65 78 74 20 65 6e 63 6f 64  s the text encod
234f7 69 6e 67 20 74 6f 20 62 65 20 75 73 65 64 20 69  ing to be used i
234f8 6e 0a 20 20 2a 2a 20 6e 65 77 20 64 61 74 61 62  n.  ** new datab
234f9 61 73 65 20 66 69 6c 65 73 20 63 72 65 61 74 65  ase files create
234fa 64 20 75 73 69 6e 67 20 74 68 69 73 20 64 61 74  d using this dat
234fb 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 49 74  abase handle. It
234fc 20 69 73 20 6f 6e 6c 79 0a 20 20 2a 2a 20 75 73   is only.  ** us
234fd 65 66 75 6c 20 69 66 20 69 6e 76 6f 6b 65 64 20  eful if invoked 
234fe 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65  immediately afte
234ff 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  r the main datab
23500 61 73 65 20 69 0a 20 20 2a 2f 0a 20 20 69 66 28  ase i.  */.  if(
23501 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
23502 7a 4c 65 66 74 2c 20 22 65 6e 63 6f 64 69 6e 67  zLeft, "encoding
23503 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 61  ")==0 ){.    sta
23504 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74  tic const struct
23505 20 45 6e 63 4e 61 6d 65 20 7b 0a 20 20 20 20 20   EncName {.     
23506 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20   char *zName;.  
23507 20 20 20 20 75 38 20 65 6e 63 3b 0a 20 20 20 20      u8 enc;.    
23508 7d 20 65 6e 63 6e 61 6d 65 73 5b 5d 20 3d 20 7b  } encnames[] = {
23509 0a 20 20 20 20 20 20 7b 20 22 55 54 46 2d 38 22  .      { "UTF-8"
2350a 2c 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38  ,    SQLITE_UTF8
2350b 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 20          },.     
2350c 20 7b 20 22 55 54 46 38 22 2c 20 20 20 20 20 53   { "UTF8",     S
2350d 51 4c 49 54 45 5f 55 54 46 38 20 20 20 20 20 20  QLITE_UTF8      
2350e 20 20 7d 2c 0a 20 20 20 20 20 20 7b 20 22 55 54    },.      { "UT
2350f 46 2d 31 36 6c 65 22 2c 20 53 51 4c 49 54 45 5f  F-16le", SQLITE_
23510 55 54 46 31 36 4c 45 20 20 20 20 20 7d 2c 0a 20  UTF16LE     },. 
23511 20 20 20 20 20 7b 20 22 55 54 46 31 36 6c 65 22       { "UTF16le"
23512 2c 20 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  ,  SQLITE_UTF16L
23513 45 20 20 20 20 20 7d 2c 0a 20 20 20 20 20 20 7b  E     },.      {
23514 20 22 55 54 46 2d 31 36 62 65 22 2c 20 53 51 4c   "UTF-16be", SQL
23515 49 54 45 5f 55 54 46 31 36 42 45 20 20 20 20 20  ITE_UTF16BE     
23516 7d 2c 0a 20 20 20 20 20 20 7b 20 22 55 54 46 31  },.      { "UTF1
23517 36 62 65 22 2c 20 20 53 51 4c 49 54 45 5f 55 54  6be",  SQLITE_UT
23518 46 31 36 42 45 20 20 20 20 20 7d 2c 0a 20 20 20  F16BE     },.   
23519 20 20 20 7b 20 22 55 54 46 2d 31 36 22 2c 20 20     { "UTF-16",  
2351a 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20   0              
2351b 20 20 20 20 7d 2c 20 2f 2a 20 53 51 4c 49 54 45      }, /* SQLITE
2351c 5f 55 54 46 31 36 4e 41 54 49 56 45 20 2a 2f 0a  _UTF16NATIVE */.
2351d 20 20 20 20 20 20 7b 20 22 55 54 46 31 36 22 2c        { "UTF16",
2351e 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20      0           
2351f 20 20 20 20 20 20 20 7d 2c 20 2f 2a 20 53 51 4c         }, /* SQL
23520 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20  ITE_UTF16NATIVE 
23521 2a 2f 0a 20 20 20 20 20 20 7b 20 30 2c 20 30 20  */.      { 0, 0 
23522 7d 0a 20 20 20 20 7d 3b 0a 20 20 20 20 63 6f 6e  }.    };.    con
23523 73 74 20 73 74 72 75 63 74 20 45 6e 63 4e 61 6d  st struct EncNam
23524 65 20 2a 70 45 6e 63 3b 0a 20 20 20 20 69 66 28  e *pEnc;.    if(
23525 20 21 7a 52 69 67 68 74 20 29 7b 20 20 20 20 2f   !zRight ){    /
23526 2a 20 22 50 52 41 47 4d 41 20 65 6e 63 6f 64 69  * "PRAGMA encodi
23527 6e 67 22 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  ng" */.      if(
23528 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
23529 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74  ma(pParse) ) got
2352a 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20  o pragma_out;.  
2352b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
2352c 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b  etNumCols(v, 1);
2352d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2352e 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
2352f 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
23530 20 22 65 6e 63 6f 64 69 6e 67 22 2c 20 53 51 4c   "encoding", SQL
23531 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
23532 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
23533 64 4f 70 32 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp2(v, OP_Strin
23534 67 38 2c 20 30 2c 20 31 29 3b 0a 20 20 20 20 20  g8, 0, 1);.     
23535 20 66 6f 72 28 70 45 6e 63 3d 26 65 6e 63 6e 61   for(pEnc=&encna
23536 6d 65 73 5b 30 5d 3b 20 70 45 6e 63 2d 3e 7a 4e  mes[0]; pEnc->zN
23537 61 6d 65 3b 20 70 45 6e 63 2b 2b 29 7b 0a 20 20  ame; pEnc++){.  
23538 20 20 20 20 20 20 69 66 28 20 70 45 6e 63 2d 3e        if( pEnc->
23539 65 6e 63 3d 3d 45 4e 43 28 70 50 61 72 73 65 2d  enc==ENC(pParse-
2353a 3e 64 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20  >db) ){.        
2353b 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
2353c 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 70 45 6e  ngeP4(v, -1, pEn
2353d 63 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41  c->zName, P4_STA
2353e 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 20 20  TIC);.          
2353f 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
23540 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
23541 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
23542 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
23543 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 7d 65 6c  , 1, 1);.    }el
23544 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  se{             
23545 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 22 50             /* "P
23546 52 41 47 4d 41 20 65 6e 63 6f 64 69 6e 67 20 3d  RAGMA encoding =
23547 20 58 58 58 22 20 2a 2f 0a 20 20 20 20 20 20 2f   XXX" */.      /
23548 2a 20 4f 6e 6c 79 20 63 68 61 6e 67 65 20 74 68  * Only change th
23549 65 20 76 61 6c 75 65 20 6f 66 20 73 71 6c 69 74  e value of sqlit
2354a 65 2e 65 6e 63 20 69 66 20 74 68 65 20 64 61 74  e.enc if the dat
2354b 61 62 61 73 65 20 68 61 6e 64 6c 65 20 69 73 20  abase handle is 
2354c 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20 69 6e 69  not.      ** ini
2354d 74 69 61 6c 69 7a 65 64 2e 20 49 66 20 74 68 65  tialized. If the
2354e 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 65   main database e
2354f 78 69 73 74 73 2c 20 74 68 65 20 6e 65 77 20 73  xists, the new s
23550 71 6c 69 74 65 2e 65 6e 63 20 76 61 6c 75 65 0a  qlite.enc value.
23551 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65        ** will be
23552 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 68 65   overwritten whe
23553 6e 20 74 68 65 20 73 63 68 65 6d 61 20 69 73 20  n the schema is 
23554 6e 65 78 74 20 6c 6f 61 64 65 64 2e 20 49 66 20  next loaded. If 
23555 69 74 20 64 6f 65 73 20 6e 6f 74 0a 20 20 20 20  it does not.    
23556 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 65 78 69    ** already exi
23557 73 74 73 2c 20 69 74 20 77 69 6c 6c 20 62 65 20  sts, it will be 
23558 63 72 65 61 74 65 64 20 74 6f 20 75 73 65 20 74  created to use t
23559 68 65 20 6e 65 77 20 65 6e 63 6f 64 69 6e 67 20  he new encoding 
2355a 76 61 6c 75 65 2e 0a 20 20 20 20 20 20 2a 2f 0a  value..      */.
2355b 20 20 20 20 20 20 69 66 28 20 0a 20 20 20 20 20        if( .     
2355c 20 20 20 21 28 44 62 48 61 73 50 72 6f 70 65 72     !(DbHasProper
2355d 74 79 28 64 62 2c 20 30 2c 20 44 42 5f 53 63 68  ty(db, 0, DB_Sch
2355e 65 6d 61 4c 6f 61 64 65 64 29 29 20 7c 7c 20 0a  emaLoaded)) || .
2355f 20 20 20 20 20 20 20 20 44 62 48 61 73 50 72 6f          DbHasPro
23560 70 65 72 74 79 28 64 62 2c 20 30 2c 20 44 42 5f  perty(db, 0, DB_
23561 45 6d 70 74 79 29 20 0a 20 20 20 20 20 20 29 7b  Empty) .      ){
23562 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70 45 6e  .        for(pEn
23563 63 3d 26 65 6e 63 6e 61 6d 65 73 5b 30 5d 3b 20  c=&encnames[0]; 
23564 70 45 6e 63 2d 3e 7a 4e 61 6d 65 3b 20 70 45 6e  pEnc->zName; pEn
23565 63 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  c++){.          
23566 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74  if( 0==sqlite3St
23567 72 49 43 6d 70 28 7a 52 69 67 68 74 2c 20 70 45  rICmp(zRight, pE
23568 6e 63 2d 3e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20  nc->zName) ){.  
23569 20 20 20 20 20 20 20 20 20 20 45 4e 43 28 70 50            ENC(pP
2356a 61 72 73 65 2d 3e 64 62 29 20 3d 20 70 45 6e 63  arse->db) = pEnc
2356b 2d 3e 65 6e 63 20 3f 20 70 45 6e 63 2d 3e 65 6e  ->enc ? pEnc->en
2356c 63 20 3a 20 53 51 4c 49 54 45 5f 55 54 46 31 36  c : SQLITE_UTF16
2356d 4e 41 54 49 56 45 3b 0a 20 20 20 20 20 20 20 20  NATIVE;.        
2356e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2356f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
23570 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70 45  .        if( !pE
23571 6e 63 2d 3e 7a 4e 61 6d 65 20 29 7b 0a 20 20 20  nc->zName ){.   
23572 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
23573 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
23574 75 6e 73 75 70 70 6f 72 74 65 64 20 65 6e 63 6f  unsupported enco
23575 64 69 6e 67 3a 20 25 73 22 2c 20 7a 52 69 67 68  ding: %s", zRigh
23576 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  t);.        }.  
23577 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65      }.    }.  }e
23578 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  lse.#endif /* SQ
23579 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
2357a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
2357b 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 56  TE_OMIT_SCHEMA_V
2357c 45 52 53 49 4f 4e 5f 50 52 41 47 4d 41 53 0a 20  ERSION_PRAGMAS. 
2357d 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d   /*.  **   PRAGM
2357e 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 73 63 68  A [database.]sch
2357f 65 6d 61 5f 76 65 72 73 69 6f 6e 0a 20 20 2a 2a  ema_version.  **
23580 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62     PRAGMA [datab
23581 61 73 65 2e 5d 73 63 68 65 6d 61 5f 76 65 72 73  ase.]schema_vers
23582 69 6f 6e 20 3d 20 3c 69 6e 74 65 67 65 72 3e 0a  ion = <integer>.
23583 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47    **.  **   PRAG
23584 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 75 73  MA [database.]us
23585 65 72 5f 76 65 72 73 69 6f 6e 0a 20 20 2a 2a 20  er_version.  ** 
23586 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
23587 73 65 2e 5d 75 73 65 72 5f 76 65 72 73 69 6f 6e  se.]user_version
23588 20 3d 20 3c 69 6e 74 65 67 65 72 3e 0a 20 20 2a   = <integer>.  *
23589 2a 0a 20 20 2a 2a 20 54 68 65 20 70 72 61 67 6d  *.  ** The pragm
2358a 61 27 73 20 73 63 68 65 6d 61 5f 76 65 72 73 69  a's schema_versi
2358b 6f 6e 20 61 6e 64 20 75 73 65 72 5f 76 65 72 73  on and user_vers
2358c 69 6f 6e 20 61 72 65 20 75 73 65 64 20 74 6f 20  ion are used to 
2358d 73 65 74 20 6f 72 20 67 65 74 0a 20 20 2a 2a 20  set or get.  ** 
2358e 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
2358f 20 73 63 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20   schema-version 
23590 61 6e 64 20 75 73 65 72 2d 76 65 72 73 69 6f 6e  and user-version
23591 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20  , respectively. 
23592 42 6f 74 68 0a 20 20 2a 2a 20 74 68 65 20 73 63  Both.  ** the sc
23593 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20 61 6e 64  hema-version and
23594 20 74 68 65 20 75 73 65 72 2d 76 65 72 73 69 6f   the user-versio
23595 6e 20 61 72 65 20 33 32 2d 62 69 74 20 73 69 67  n are 32-bit sig
23596 6e 65 64 20 69 6e 74 65 67 65 72 73 0a 20 20 2a  ned integers.  *
23597 2a 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  * stored in the 
23598 64 61 74 61 62 61 73 65 20 68 65 61 64 65 72 2e  database header.
23599 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73  .  **.  ** The s
2359a 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 20 69 73 20  chema-cookie is 
2359b 75 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 6d 61 6e  usually only man
2359c 69 70 75 6c 61 74 65 64 20 69 6e 74 65 72 6e 61  ipulated interna
2359d 6c 6c 79 20 62 79 20 53 51 4c 69 74 65 2e 20 49  lly by SQLite. I
2359e 74 0a 20 20 2a 2a 20 69 73 20 69 6e 63 72 65 6d  t.  ** is increm
2359f 65 6e 74 65 64 20 62 79 20 53 51 4c 69 74 65 20  ented by SQLite 
235a0 77 68 65 6e 65 76 65 72 20 74 68 65 20 64 61 74  whenever the dat
235a1 61 62 61 73 65 20 73 63 68 65 6d 61 20 69 73 20  abase schema is 
235a2 6d 6f 64 69 66 69 65 64 20 28 62 79 0a 20 20 2a  modified (by.  *
235a3 2a 20 63 72 65 61 74 69 6e 67 20 6f 72 20 64 72  * creating or dr
235a4 6f 70 70 69 6e 67 20 61 20 74 61 62 6c 65 20 6f  opping a table o
235a5 72 20 69 6e 64 65 78 29 2e 20 54 68 65 20 73 63  r index). The sc
235a6 68 65 6d 61 20 76 65 72 73 69 6f 6e 20 69 73 20  hema version is 
235a7 75 73 65 64 20 62 79 0a 20 20 2a 2a 20 53 51 4c  used by.  ** SQL
235a8 69 74 65 20 65 61 63 68 20 74 69 6d 65 20 61 20  ite each time a 
235a9 71 75 65 72 79 20 69 73 20 65 78 65 63 75 74 65  query is execute
235aa 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74  d to ensure that
235ab 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 63 61   the internal ca
235ac 63 68 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  che.  ** of the 
235ad 73 63 68 65 6d 61 20 75 73 65 64 20 77 68 65 6e  schema used when
235ae 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 53   compiling the S
235af 51 4c 20 71 75 65 72 79 20 6d 61 74 63 68 65 73  QL query matches
235b0 20 74 68 65 20 73 63 68 65 6d 61 20 6f 66 0a 20   the schema of. 
235b1 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65   ** the database
235b2 20 61 67 61 69 6e 73 74 20 77 68 69 63 68 20 74   against which t
235b3 68 65 20 63 6f 6d 70 69 6c 65 64 20 71 75 65 72  he compiled quer
235b4 79 20 69 73 20 61 63 74 75 61 6c 6c 79 20 65 78  y is actually ex
235b5 65 63 75 74 65 64 2e 0a 20 20 2a 2a 20 53 75 62  ecuted..  ** Sub
235b6 76 65 72 74 69 6e 67 20 74 68 69 73 20 6d 65 63  verting this mec
235b7 68 61 6e 69 73 6d 20 62 79 20 75 73 69 6e 67 20  hanism by using 
235b8 22 50 52 41 47 4d 41 20 73 63 68 65 6d 61 5f 76  "PRAGMA schema_v
235b9 65 72 73 69 6f 6e 22 20 74 6f 20 6d 6f 64 69 66  ersion" to modif
235ba 79 0a 20 20 2a 2a 20 74 68 65 20 73 63 68 65 6d  y.  ** the schem
235bb 61 2d 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 74  a-version is pot
235bc 65 6e 74 69 61 6c 6c 79 20 64 61 6e 67 65 72 6f  entially dangero
235bd 75 73 20 61 6e 64 20 6d 61 79 20 6c 65 61 64 20  us and may lead 
235be 74 6f 20 70 72 6f 67 72 61 6d 0a 20 20 2a 2a 20  to program.  ** 
235bf 63 72 61 73 68 65 73 20 6f 72 20 64 61 74 61 62  crashes or datab
235c0 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20  ase corruption. 
235c1 55 73 65 20 77 69 74 68 20 63 61 75 74 69 6f 6e  Use with caution
235c2 21 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  !.  **.  ** The 
235c3 75 73 65 72 2d 76 65 72 73 69 6f 6e 20 69 73 20  user-version is 
235c4 6e 6f 74 20 75 73 65 64 20 69 6e 74 65 72 6e 61  not used interna
235c5 6c 6c 79 20 62 79 20 53 51 4c 69 74 65 2e 20 49  lly by SQLite. I
235c6 74 20 6d 61 79 20 62 65 20 75 73 65 64 20 62 79  t may be used by
235c7 0a 20 20 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f  .  ** applicatio
235c8 6e 73 20 66 6f 72 20 61 6e 79 20 70 75 72 70 6f  ns for any purpo
235c9 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  se..  */.  if( s
235ca 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
235cb 65 66 74 2c 20 22 73 63 68 65 6d 61 5f 76 65 72  eft, "schema_ver
235cc 73 69 6f 6e 22 29 3d 3d 30 20 0a 20 20 20 7c 7c  sion")==0 .   ||
235cd 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
235ce 7a 4c 65 66 74 2c 20 22 75 73 65 72 5f 76 65 72  zLeft, "user_ver
235cf 73 69 6f 6e 22 29 3d 3d 30 20 0a 20 20 20 7c 7c  sion")==0 .   ||
235d0 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
235d1 7a 4c 65 66 74 2c 20 22 66 72 65 65 6c 69 73 74  zLeft, "freelist
235d2 5f 63 6f 75 6e 74 22 29 3d 3d 30 20 0a 20 20 29  _count")==0 .  )
235d3 7b 0a 20 20 20 20 69 6e 74 20 69 43 6f 6f 6b 69  {.    int iCooki
235d4 65 3b 20 20 20 2f 2a 20 43 6f 6f 6b 69 65 20 69  e;   /* Cookie i
235d5 6e 64 65 78 2e 20 30 20 66 6f 72 20 73 63 68 65  ndex. 0 for sche
235d6 6d 61 2d 63 6f 6f 6b 69 65 2c 20 36 20 66 6f 72  ma-cookie, 6 for
235d7 20 75 73 65 72 2d 63 6f 6f 6b 69 65 2e 20 2a 2f   user-cookie. */
235d8 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
235d9 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44 62  UsesBtree(v, iDb
235da 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 7a  );.    switch( z
235db 4c 65 66 74 5b 30 5d 20 29 7b 0a 20 20 20 20 20  Left[0] ){.     
235dc 20 63 61 73 65 20 27 73 27 3a 20 63 61 73 65 20   case 's': case 
235dd 27 53 27 3a 0a 20 20 20 20 20 20 20 20 69 43 6f  'S':.        iCo
235de 6f 6b 69 65 20 3d 20 30 3b 0a 20 20 20 20 20 20  okie = 0;.      
235df 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
235e0 61 73 65 20 27 66 27 3a 20 63 61 73 65 20 27 46  ase 'f': case 'F
235e1 27 3a 0a 20 20 20 20 20 20 20 20 69 43 6f 6f 6b  ':.        iCook
235e2 69 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ie = 1;.        
235e3 69 44 62 20 3d 20 28 2d 31 2a 28 69 44 62 2b 31  iDb = (-1*(iDb+1
235e4 29 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  ));.        asse
235e5 72 74 28 69 44 62 3c 3d 30 29 3b 0a 20 20 20 20  rt(iDb<=0);.    
235e6 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
235e7 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20   default:.      
235e8 20 20 69 43 6f 6f 6b 69 65 20 3d 20 35 3b 0a 20    iCookie = 5;. 
235e9 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
235ea 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 7a 52 69    }..    if( zRi
235eb 67 68 74 20 26 26 20 69 44 62 3e 3d 30 20 29 7b  ght && iDb>=0 ){
235ec 0a 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20  .      /* Write 
235ed 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63 6f  the specified co
235ee 6f 6b 69 65 20 76 61 6c 75 65 20 2a 2f 0a 20 20  okie value */.  
235ef 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
235f0 20 56 64 62 65 4f 70 4c 69 73 74 20 73 65 74 43   VdbeOpList setC
235f1 6f 6f 6b 69 65 5b 5d 20 3d 20 7b 0a 20 20 20 20  ookie[] = {.    
235f2 20 20 20 20 7b 20 4f 50 5f 54 72 61 6e 73 61 63      { OP_Transac
235f3 74 69 6f 6e 2c 20 20 20 20 30 2c 20 20 31 2c 20  tion,    0,  1, 
235f4 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a 2f 0a   0},    /* 0 */.
235f5 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 6e 74          { OP_Int
235f6 65 67 65 72 2c 20 20 20 20 20 20 20 20 30 2c 20  eger,        0, 
235f7 20 31 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 31   1,  0},    /* 1
235f8 20 2a 2f 0a 20 20 20 20 20 20 20 20 7b 20 4f 50   */.        { OP
235f9 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 20 20 20 20  _SetCookie,     
235fa 20 30 2c 20 20 30 2c 20 20 31 7d 2c 20 20 20 20   0,  0,  1},    
235fb 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 20 20 7d 3b  /* 2 */.      };
235fc 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 20  .      int addr 
235fd 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
235fe 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53  OpList(v, ArrayS
235ff 69 7a 65 28 73 65 74 43 6f 6f 6b 69 65 29 2c 20  ize(setCookie), 
23600 73 65 74 43 6f 6f 6b 69 65 29 3b 0a 20 20 20 20  setCookie);.    
23601 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
23602 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2c 20 69  ngeP1(v, addr, i
23603 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Db);.      sqlit
23604 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76  e3VdbeChangeP1(v
23605 2c 20 61 64 64 72 2b 31 2c 20 61 74 6f 69 28 7a  , addr+1, atoi(z
23606 52 69 67 68 74 29 29 3b 0a 20 20 20 20 20 20 73  Right));.      s
23607 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
23608 50 31 28 76 2c 20 61 64 64 72 2b 32 2c 20 69 44  P1(v, addr+2, iD
23609 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
2360a 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c  3VdbeChangeP2(v,
2360b 20 61 64 64 72 2b 32 2c 20 69 43 6f 6f 6b 69 65   addr+2, iCookie
2360c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
2360d 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20      /* Read the 
2360e 73 70 65 63 69 66 69 65 64 20 63 6f 6f 6b 69 65  specified cookie
2360f 20 76 61 6c 75 65 20 2a 2f 0a 20 20 20 20 20 20   value */.      
23610 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62  static const Vdb
23611 65 4f 70 4c 69 73 74 20 72 65 61 64 43 6f 6f 6b  eOpList readCook
23612 69 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20  ie[] = {.       
23613 20 7b 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65   { OP_ReadCookie
23614 2c 20 20 20 20 20 20 30 2c 20 20 31 2c 20 20 30  ,      0,  1,  0
23615 7d 2c 20 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20  },    /* 0 */.  
23616 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 73 75 6c        { OP_Resul
23617 74 52 6f 77 2c 20 20 20 20 20 20 20 31 2c 20 20  tRow,       1,  
23618 31 2c 20 20 30 7d 0a 20 20 20 20 20 20 7d 3b 0a  1,  0}.      };.
23619 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 20 3d        int addr =
2361a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2361b 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53 69  pList(v, ArraySi
2361c 7a 65 28 72 65 61 64 43 6f 6f 6b 69 65 29 2c 20  ze(readCookie), 
2361d 72 65 61 64 43 6f 6f 6b 69 65 29 3b 0a 20 20 20  readCookie);.   
2361e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
2361f 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2c 20  angeP1(v, addr, 
23620 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  iDb);.      sqli
23621 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28  te3VdbeChangeP3(
23622 76 2c 20 61 64 64 72 2c 20 69 43 6f 6f 6b 69 65  v, addr, iCookie
23623 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
23624 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
23625 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
23626 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
23627 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
23628 4e 41 4d 45 2c 20 7a 4c 65 66 74 2c 20 53 51 4c  NAME, zLeft, SQL
23629 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
2362a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65      }.  }else.#e
2362b 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2362c 4d 49 54 5f 53 43 48 45 4d 41 5f 56 45 52 53 49  MIT_SCHEMA_VERSI
2362d 4f 4e 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23  ON_PRAGMAS */..#
2362e 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
2362f 45 5f 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69  E_DEBUG) || defi
23630 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29  ned(SQLITE_TEST)
23631 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 70 6f 72  .  /*.  ** Repor
23632 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74  t the current st
23633 61 74 65 20 6f 66 20 66 69 6c 65 20 6c 6f 67 73  ate of file logs
23634 20 66 6f 72 20 61 6c 6c 20 64 61 74 61 62 61 73   for all databas
23635 65 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  es.  */.  if( sq
23636 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
23637 66 74 2c 20 22 6c 6f 63 6b 5f 73 74 61 74 75 73  ft, "lock_status
23638 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 61  ")==0 ){.    sta
23639 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
2363a 63 6f 6e 73 74 20 61 7a 4c 6f 63 6b 4e 61 6d 65  const azLockName
2363b 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 22 75 6e  [] = {.      "un
2363c 6c 6f 63 6b 65 64 22 2c 20 22 73 68 61 72 65 64  locked", "shared
2363d 22 2c 20 22 72 65 73 65 72 76 65 64 22 2c 20 22  ", "reserved", "
2363e 70 65 6e 64 69 6e 67 22 2c 20 22 65 78 63 6c 75  pending", "exclu
2363f 73 69 76 65 22 0a 20 20 20 20 7d 3b 0a 20 20 20  sive".    };.   
23640 20 69 6e 74 20 69 3b 0a 20 20 20 20 56 64 62 65   int i;.    Vdbe
23641 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74   *v = sqlite3Get
23642 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
23643 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
23644 4e 75 6d 43 6f 6c 73 28 76 2c 20 32 29 3b 0a 20  NumCols(v, 2);. 
23645 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20     pParse->nMem 
23646 3d 20 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 2;.    sqlite3
23647 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
23648 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 0, COLNAME_NAM
23649 45 2c 20 22 64 61 74 61 62 61 73 65 22 2c 20 53  E, "database", S
2364a 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
2364b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
2364c 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43  tColName(v, 1, C
2364d 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 74  OLNAME_NAME, "st
2364e 61 74 75 73 22 2c 20 53 51 4c 49 54 45 5f 53 54  atus", SQLITE_ST
2364f 41 54 49 43 29 3b 0a 20 20 20 20 66 6f 72 28 69  ATIC);.    for(i
23650 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
23651 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74 72 65 65  ++){.      Btree
23652 20 2a 70 42 74 3b 0a 20 20 20 20 20 20 50 61 67   *pBt;.      Pag
23653 65 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20 20  er *pPager;.    
23654 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
23655 74 61 74 65 20 3d 20 22 75 6e 6b 6e 6f 77 6e 22  tate = "unknown"
23656 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20  ;.      int j;. 
23657 20 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62       if( db->aDb
23658 5b 69 5d 2e 7a 4e 61 6d 65 3d 3d 30 20 29 20 63  [i].zName==0 ) c
23659 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 73  ontinue;.      s
2365a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2365b 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
2365c 30 2c 20 31 2c 20 30 2c 20 64 62 2d 3e 61 44 62  0, 1, 0, db->aDb
2365d 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54  [i].zName, P4_ST
2365e 41 54 49 43 29 3b 0a 20 20 20 20 20 20 70 42 74  ATIC);.      pBt
2365f 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
23660 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74  t;.      if( pBt
23661 3d 3d 30 20 7c 7c 20 28 70 50 61 67 65 72 20 3d  ==0 || (pPager =
23662 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67   sqlite3BtreePag
23663 65 72 28 70 42 74 29 29 3d 3d 30 20 29 7b 0a 20  er(pBt))==0 ){. 
23664 20 20 20 20 20 20 20 7a 53 74 61 74 65 20 3d 20         zState = 
23665 22 63 6c 6f 73 65 64 22 3b 0a 20 20 20 20 20 20  "closed";.      
23666 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65  }else if( sqlite
23667 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64  3_file_control(d
23668 62 2c 20 69 20 3f 20 64 62 2d 3e 61 44 62 5b 69  b, i ? db->aDb[i
23669 5d 2e 7a 4e 61 6d 65 20 3a 20 30 2c 20 0a 20 20  ].zName : 0, .  
2366a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2366b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2366c 20 20 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f     SQLITE_FCNTL_
2366d 4c 4f 43 4b 53 54 41 54 45 2c 20 26 6a 29 3d 3d  LOCKSTATE, &j)==
2366e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
2366f 20 20 20 20 20 20 7a 53 74 61 74 65 20 3d 20 61        zState = a
23670 7a 4c 6f 63 6b 4e 61 6d 65 5b 6a 5d 3b 0a 20 20  zLockName[j];.  
23671 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
23672 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
23673 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
23674 32 2c 20 30 2c 20 7a 53 74 61 74 65 2c 20 50 34  2, 0, zState, P4
23675 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
23676 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23677 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f  2(v, OP_ResultRo
23678 77 2c 20 31 2c 20 32 29 3b 0a 20 20 20 20 7d 0a  w, 1, 2);.    }.
23679 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a  .  }else.#endif.
2367a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53  .#ifdef SQLITE_S
2367b 53 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 68 65  SE.  /*.  ** Che
2367c 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65  ck to see if the
2367d 20 73 71 6c 69 74 65 5f 73 74 61 74 65 6d 65 6e   sqlite_statemen
2367e 74 73 20 74 61 62 6c 65 20 65 78 69 73 74 73 2e  ts table exists.
2367f 20 20 43 72 65 61 74 65 20 69 74 0a 20 20 2a 2a    Create it.  **
23680 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 2e   if it does not.
23681 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
23682 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
23683 2c 20 22 63 72 65 61 74 65 5f 73 71 6c 69 74 65  , "create_sqlite
23684 5f 73 74 61 74 65 6d 65 6e 74 5f 74 61 62 6c 65  _statement_table
23685 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 65 78 74  ")==0 ){.    ext
23686 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 43  ern int sqlite3C
23687 72 65 61 74 65 53 74 61 74 65 6d 65 6e 74 73 54  reateStatementsT
23688 61 62 6c 65 28 50 61 72 73 65 2a 29 3b 0a 20 20  able(Parse*);.  
23689 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 53    sqlite3CreateS
2368a 74 61 74 65 6d 65 6e 74 73 54 61 62 6c 65 28 70  tatementsTable(p
2368b 50 61 72 73 65 29 3b 0a 20 20 7d 65 6c 73 65 0a  Parse);.  }else.
2368c 23 65 6e 64 69 66 0a 0a 23 69 66 20 53 51 4c 49  #endif..#if SQLI
2368d 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 69  TE_HAS_CODEC.  i
2368e 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
2368f 70 28 7a 4c 65 66 74 2c 20 22 6b 65 79 22 29 3d  p(zLeft, "key")=
23690 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
23691 33 5f 6b 65 79 28 64 62 2c 20 7a 52 69 67 68 74  3_key(db, zRight
23692 2c 20 73 74 72 6c 65 6e 28 7a 52 69 67 68 74 29  , strlen(zRight)
23693 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69  );.  }else.#endi
23694 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 48 41 53  f.#if SQLITE_HAS
23695 5f 43 4f 44 45 43 20 7c 7c 20 64 65 66 69 6e 65  _CODEC || define
23696 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
23697 43 45 52 4f 44 29 0a 20 20 69 66 28 20 73 71 6c  CEROD).  if( sql
23698 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
23699 74 2c 20 22 61 63 74 69 76 61 74 65 5f 65 78 74  t, "activate_ext
2369a 65 6e 73 69 6f 6e 73 22 29 3d 3d 30 20 29 7b 0a  ensions")==0 ){.
2369b 23 69 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43  #if SQLITE_HAS_C
2369c 4f 44 45 43 0a 20 20 20 20 69 66 28 20 73 71 6c  ODEC.    if( sql
2369d 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 52 69  ite3StrNICmp(zRi
2369e 67 68 74 2c 20 22 73 65 65 2d 22 2c 20 34 29 3d  ght, "see-", 4)=
2369f 3d 30 20 29 7b 0a 20 20 20 20 20 20 65 78 74 65  =0 ){.      exte
236a0 72 6e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  rn void sqlite3_
236a1 61 63 74 69 76 61 74 65 5f 73 65 65 28 63 6f 6e  activate_see(con
236a2 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 20 20 20  st char*);.     
236a3 20 73 71 6c 69 74 65 33 5f 61 63 74 69 76 61 74   sqlite3_activat
236a4 65 5f 73 65 65 28 26 7a 52 69 67 68 74 5b 34 5d  e_see(&zRight[4]
236a5 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
236a6 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
236a7 41 42 4c 45 5f 43 45 52 4f 44 0a 20 20 20 20 69  ABLE_CEROD.    i
236a8 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  f( sqlite3StrNIC
236a9 6d 70 28 7a 52 69 67 68 74 2c 20 22 63 65 72 6f  mp(zRight, "cero
236aa 64 2d 22 2c 20 36 29 3d 3d 30 20 29 7b 0a 20 20  d-", 6)==0 ){.  
236ab 20 20 20 20 65 78 74 65 72 6e 20 76 6f 69 64 20      extern void 
236ac 73 71 6c 69 74 65 33 5f 61 63 74 69 76 61 74 65  sqlite3_activate
236ad 5f 63 65 72 6f 64 28 63 6f 6e 73 74 20 63 68 61  _cerod(const cha
236ae 72 2a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  r*);.      sqlit
236af 65 33 5f 61 63 74 69 76 61 74 65 5f 63 65 72 6f  e3_activate_cero
236b0 64 28 26 7a 52 69 67 68 74 5b 36 5d 29 3b 0a 20  d(&zRight[6]);. 
236b1 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a     }.#endif.  }.
236b2 23 65 6e 64 69 66 0a 0a 20 20 7b 7d 0a 0a 20 20  #endif..  {}..  
236b3 69 66 28 20 76 20 29 7b 0a 20 20 20 20 2f 2a 20  if( v ){.    /* 
236b4 43 6f 64 65 20 61 6e 20 4f 50 5f 45 78 70 69 72  Code an OP_Expir
236b5 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20  e at the end of 
236b6 65 61 63 68 20 50 52 41 47 4d 41 20 70 72 6f 67  each PRAGMA prog
236b7 72 61 6d 20 74 6f 20 63 61 75 73 65 0a 20 20 20  ram to cause.   
236b8 20 2a 2a 20 74 68 65 20 56 44 42 45 20 69 6d 70   ** the VDBE imp
236b9 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65 20 70 72  lementing the pr
236ba 61 67 6d 61 20 74 6f 20 65 78 70 69 72 65 2e 20  agma to expire. 
236bb 4d 6f 73 74 20 28 61 6c 6c 3f 29 20 70 72 61 67  Most (all?) prag
236bc 6d 61 73 0a 20 20 20 20 2a 2a 20 61 72 65 20 6f  mas.    ** are o
236bd 6e 6c 79 20 76 61 6c 69 64 20 66 6f 72 20 61 20  nly valid for a 
236be 73 69 6e 67 6c 65 20 65 78 65 63 75 74 69 6f 6e  single execution
236bf 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  ..    */.    sql
236c0 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
236c1 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20 31 2c 20  , OP_Expire, 1, 
236c2 30 29 3b 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20  0);..    /*.    
236c3 2a 2a 20 52 65 73 65 74 20 74 68 65 20 73 61 66  ** Reset the saf
236c4 65 74 79 20 6c 65 76 65 6c 2c 20 69 6e 20 63 61  ety level, in ca
236c5 73 65 20 74 68 65 20 66 75 6c 6c 66 73 79 6e 63  se the fullfsync
236c6 20 66 6c 61 67 20 6f 72 20 73 79 6e 63 68 72 6f   flag or synchro
236c7 6e 6f 75 73 0a 20 20 20 20 2a 2a 20 73 65 74 74  nous.    ** sett
236c8 69 6e 67 20 63 68 61 6e 67 65 64 2e 0a 20 20 20  ing changed..   
236c9 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
236ca 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52  TE_OMIT_PAGER_PR
236cb 41 47 4d 41 53 0a 20 20 20 20 69 66 28 20 64 62  AGMAS.    if( db
236cc 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a  ->autoCommit ){.
236cd 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
236ce 65 65 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c  eeSetSafetyLevel
236cf 28 70 44 62 2d 3e 70 42 74 2c 20 70 44 62 2d 3e  (pDb->pBt, pDb->
236d0 73 61 66 65 74 79 5f 6c 65 76 65 6c 2c 0a 20 20  safety_level,.  
236d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
236d2 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45  db->flags&SQLITE
236d3 5f 46 75 6c 6c 46 53 79 6e 63 29 21 3d 30 29 3b  _FullFSync)!=0);
236d4 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
236d5 7d 0a 70 72 61 67 6d 61 5f 6f 75 74 3a 0a 20 20  }.pragma_out:.  
236d6 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
236d7 2c 20 7a 4c 65 66 74 29 3b 0a 20 20 73 71 6c 69  , zLeft);.  sqli
236d8 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 52  te3DbFree(db, zR
236d9 69 67 68 74 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66  ight);.}..#endif
236da 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
236db 50 52 41 47 4d 41 20 7c 7c 20 53 51 4c 49 54 45  PRAGMA || SQLITE
236dc 5f 4f 4d 49 54 5f 50 41 52 53 45 52 20 2a 2f 0a  _OMIT_PARSER */.
236dd 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
236de 20 45 6e 64 20 6f 66 20 70 72 61 67 6d 61 2e 63   End of pragma.c
236df 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
236e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
236e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
236e2 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
236e3 20 42 65 67 69 6e 20 66 69 6c 65 20 70 72 65 70   Begin file prep
236e4 61 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  are.c **********
236e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
236e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
236e7 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4d 61 79 20  ./*.** 2005 May 
236e8 32 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  25.**.** The aut
236e9 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
236ea 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
236eb 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
236ec 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
236ed 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
236ee 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
236ef 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
236f0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
236f1 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
236f2 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
236f3 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
236f4 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
236f5 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
236f6 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
236f7 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
236f8 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
236f9 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
236fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
236fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
236fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
236fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
236fe 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
236ff 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d 70 6c  ontains the impl
23700 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
23701 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  e sqlite3_prepar
23702 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  e().** interface
23703 2c 20 61 6e 64 20 72 6f 75 74 69 6e 65 73 20 74  , and routines t
23704 68 61 74 20 63 6f 6e 74 72 69 62 75 74 65 20 74  hat contribute t
23705 6f 20 6c 6f 61 64 69 6e 67 20 74 68 65 20 64 61  o loading the da
23706 74 61 62 61 73 65 20 73 63 68 65 6d 61 0a 2a 2a  tabase schema.**
23707 20 66 72 6f 6d 20 64 69 73 6b 2e 0a 2a 2a 0a 2a   from disk..**.*
23708 2a 20 24 49 64 3a 20 70 72 65 70 61 72 65 2e 63  * $Id: prepare.c
23709 2c 76 20 31 2e 39 38 20 32 30 30 38 2f 31 30 2f  ,v 1.98 2008/10/
2370a 33 31 20 31 30 3a 35 33 3a 32 33 20 64 61 6e 69  31 10:53:23 dani
2370b 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f  elk1977 Exp $.*/
2370c 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c 6c 20 74 68 65  ../*.** Fill the
2370d 20 49 6e 69 74 44 61 74 61 20 73 74 72 75 63 74   InitData struct
2370e 75 72 65 20 77 69 74 68 20 61 6e 20 65 72 72 6f  ure with an erro
2370f 72 20 6d 65 73 73 61 67 65 20 74 68 61 74 20 69  r message that i
23710 6e 64 69 63 61 74 65 73 0a 2a 2a 20 74 68 61 74  ndicates.** that
23711 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
23712 20 63 6f 72 72 75 70 74 2e 0a 2a 2f 0a 73 74 61   corrupt..*/.sta
23713 74 69 63 20 76 6f 69 64 20 63 6f 72 72 75 70 74  tic void corrupt
23714 53 63 68 65 6d 61 28 0a 20 20 49 6e 69 74 44 61  Schema(.  InitDa
23715 74 61 20 2a 70 44 61 74 61 2c 20 20 20 20 20 2f  ta *pData,     /
23716 2a 20 49 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  * Initialization
23717 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f   context */.  co
23718 6e 73 74 20 63 68 61 72 20 2a 7a 4f 62 6a 2c 20  nst char *zObj, 
23719 20 20 20 2f 2a 20 4f 62 6a 65 63 74 20 62 65 69     /* Object bei
2371a 6e 67 20 70 61 72 73 65 64 20 61 74 20 74 68 65  ng parsed at the
2371b 20 70 6f 69 6e 74 20 6f 66 20 65 72 72 6f 72 20   point of error 
2371c 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
2371d 2a 7a 45 78 74 72 61 20 20 20 2f 2a 20 45 72 72  *zExtra   /* Err
2371e 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a  or information *
2371f 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  /.){.  sqlite3 *
23720 64 62 20 3d 20 70 44 61 74 61 2d 3e 64 62 3b 0a  db = pData->db;.
23721 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f    if( !db->mallo
23722 63 46 61 69 6c 65 64 20 26 26 20 28 64 62 2d 3e  cFailed && (db->
23723 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 52  flags & SQLITE_R
23724 65 63 6f 76 65 72 79 4d 6f 64 65 29 3d 3d 30 20  ecoveryMode)==0 
23725 29 7b 0a 20 20 20 20 69 66 28 20 7a 4f 62 6a 3d  ){.    if( zObj=
23726 3d 30 20 29 20 7a 4f 62 6a 20 3d 20 22 3f 22 3b  =0 ) zObj = "?";
23727 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
23728 74 72 69 6e 67 28 70 44 61 74 61 2d 3e 70 7a 45  tring(pData->pzE
23729 72 72 4d 73 67 2c 20 70 44 61 74 61 2d 3e 64 62  rrMsg, pData->db
2372a 2c 0a 20 20 20 20 20 20 20 22 6d 61 6c 66 6f 72  ,.       "malfor
2372b 6d 65 64 20 64 61 74 61 62 61 73 65 20 73 63 68  med database sch
2372c 65 6d 61 20 28 25 73 29 22 2c 20 7a 4f 62 6a 29  ema (%s)", zObj)
2372d 3b 0a 20 20 20 20 69 66 28 20 7a 45 78 74 72 61  ;.    if( zExtra
2372e 20 26 26 20 7a 45 78 74 72 61 5b 30 5d 20 29 7b   && zExtra[0] ){
2372f 0a 20 20 20 20 20 20 2a 70 44 61 74 61 2d 3e 70  .      *pData->p
23730 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65  zErrMsg = sqlite
23731 33 4d 41 70 70 65 6e 64 66 28 70 44 61 74 61 2d  3MAppendf(pData-
23732 3e 64 62 2c 20 2a 70 44 61 74 61 2d 3e 70 7a 45  >db, *pData->pzE
23733 72 72 4d 73 67 2c 20 22 25 73 20 2d 20 25 73 22  rrMsg, "%s - %s"
23734 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
23735 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23736 20 20 20 20 2a 70 44 61 74 61 2d 3e 70 7a 45 72      *pData->pzEr
23737 72 4d 73 67 2c 20 7a 45 78 74 72 61 29 3b 0a 20  rMsg, zExtra);. 
23738 20 20 20 7d 0a 20 20 7d 0a 20 20 70 44 61 74 61     }.  }.  pData
23739 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f  ->rc = SQLITE_CO
2373a 52 52 55 50 54 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  RRUPT;.}../*.** 
2373b 54 68 69 73 20 69 73 20 74 68 65 20 63 61 6c 6c  This is the call
2373c 62 61 63 6b 20 72 6f 75 74 69 6e 65 20 66 6f 72  back routine for
2373d 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 69   the code that i
2373e 6e 69 74 69 61 6c 69 7a 65 73 20 74 68 65 0a 2a  nitializes the.*
2373f 2a 20 64 61 74 61 62 61 73 65 2e 20 20 53 65 65  * database.  See
23740 20 73 71 6c 69 74 65 33 49 6e 69 74 28 29 20 62   sqlite3Init() b
23741 65 6c 6f 77 20 66 6f 72 20 61 64 64 69 74 69 6f  elow for additio
23742 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
23743 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
23744 20 69 73 20 61 6c 73 6f 20 63 61 6c 6c 65 64 20   is also called 
23745 66 72 6f 6d 20 74 68 65 20 4f 50 5f 50 61 72 73  from the OP_Pars
23746 65 53 63 68 65 6d 61 20 6f 70 63 6f 64 65 20 6f  eSchema opcode o
23747 66 20 74 68 65 20 56 44 42 45 2e 0a 2a 2a 0a 2a  f the VDBE..**.*
23748 2a 20 45 61 63 68 20 63 61 6c 6c 62 61 63 6b 20  * Each callback 
23749 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 66 6f 6c  contains the fol
2374a 6c 6f 77 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69  lowing informati
2374b 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 72  on:.**.**     ar
2374c 67 76 5b 30 5d 20 3d 20 6e 61 6d 65 20 6f 66 20  gv[0] = name of 
2374d 74 68 69 6e 67 20 62 65 69 6e 67 20 63 72 65 61  thing being crea
2374e 74 65 64 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b  ted.**     argv[
2374f 31 5d 20 3d 20 72 6f 6f 74 20 70 61 67 65 20 6e  1] = root page n
23750 75 6d 62 65 72 20 66 6f 72 20 74 61 62 6c 65 20  umber for table 
23751 6f 72 20 69 6e 64 65 78 2e 20 30 20 66 6f 72 20  or index. 0 for 
23752 74 72 69 67 67 65 72 20 6f 72 20 76 69 65 77 2e  trigger or view.
23753 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b 32 5d 20  .**     argv[2] 
23754 3d 20 53 51 4c 20 74 65 78 74 20 66 6f 72 20 74  = SQL text for t
23755 68 65 20 43 52 45 41 54 45 20 73 74 61 74 65 6d  he CREATE statem
23756 65 6e 74 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54  ent..**.*/.SQLIT
23757 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
23758 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63  lite3InitCallbac
23759 6b 28 76 6f 69 64 20 2a 70 49 6e 69 74 2c 20 69  k(void *pInit, i
2375a 6e 74 20 61 72 67 63 2c 20 63 68 61 72 20 2a 2a  nt argc, char **
2375b 61 72 67 76 2c 20 63 68 61 72 20 2a 2a 61 7a 43  argv, char **azC
2375c 6f 6c 4e 61 6d 65 29 7b 0a 20 20 49 6e 69 74 44  olName){.  InitD
2375d 61 74 61 20 2a 70 44 61 74 61 20 3d 20 28 49 6e  ata *pData = (In
2375e 69 74 44 61 74 61 2a 29 70 49 6e 69 74 3b 0a 20  itData*)pInit;. 
2375f 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
23760 44 61 74 61 2d 3e 64 62 3b 0a 20 20 69 6e 74 20  Data->db;.  int 
23761 69 44 62 20 3d 20 70 44 61 74 61 2d 3e 69 44 62  iDb = pData->iDb
23762 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
23763 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
23764 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
23765 44 62 43 6c 65 61 72 50 72 6f 70 65 72 74 79 28  DbClearProperty(
23766 64 62 2c 20 69 44 62 2c 20 44 42 5f 45 6d 70 74  db, iDb, DB_Empt
23767 79 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  y);.  if( db->ma
23768 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
23769 20 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61 28    corruptSchema(
2376a 70 44 61 74 61 2c 20 61 72 67 76 5b 30 5d 2c 20  pData, argv[0], 
2376b 30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  0);.    return S
2376c 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
2376d 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  ..  assert( argc
2376e 3d 3d 33 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==3 );.  assert(
2376f 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64   iDb>=0 && iDb<d
23770 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 69 66 28 20  b->nDb );.  if( 
23771 61 72 67 76 3d 3d 30 20 29 20 72 65 74 75 72 6e  argv==0 ) return
23772 20 30 3b 20 20 20 2f 2a 20 4d 69 67 68 74 20 68   0;   /* Might h
23773 61 70 70 65 6e 20 69 66 20 45 4d 50 54 59 5f 52  appen if EMPTY_R
23774 45 53 55 4c 54 5f 43 41 4c 4c 42 41 43 4b 53 20  ESULT_CALLBACKS 
23775 61 72 65 20 6f 6e 20 2a 2f 0a 20 20 69 66 28 20  are on */.  if( 
23776 61 72 67 76 5b 31 5d 3d 3d 30 20 29 7b 0a 20 20  argv[1]==0 ){.  
23777 20 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61 28    corruptSchema(
23778 70 44 61 74 61 2c 20 61 72 67 76 5b 30 5d 2c 20  pData, argv[0], 
23779 30 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  0);.  }else if( 
2377a 61 72 67 76 5b 32 5d 20 26 26 20 61 72 67 76 5b  argv[2] && argv[
2377b 32 5d 5b 30 5d 20 29 7b 0a 20 20 20 20 2f 2a 20  2][0] ){.    /* 
2377c 43 61 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20  Call the parser 
2377d 74 6f 20 70 72 6f 63 65 73 73 20 61 20 43 52 45  to process a CRE
2377e 41 54 45 20 54 41 42 4c 45 2c 20 49 4e 44 45 58  ATE TABLE, INDEX
2377f 20 6f 72 20 56 49 45 57 2e 0a 20 20 20 20 2a 2a   or VIEW..    **
23780 20 42 75 74 20 62 65 63 61 75 73 65 20 64 62 2d   But because db-
23781 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20 73 65  >init.busy is se
23782 74 20 74 6f 20 31 2c 20 6e 6f 20 56 44 42 45 20  t to 1, no VDBE 
23783 63 6f 64 65 20 69 73 20 67 65 6e 65 72 61 74 65  code is generate
23784 64 0a 20 20 20 20 2a 2a 20 6f 72 20 65 78 65 63  d.    ** or exec
23785 75 74 65 64 2e 20 20 41 6c 6c 20 74 68 65 20 70  uted.  All the p
23786 61 72 73 65 72 20 64 6f 65 73 20 69 73 20 62 75  arser does is bu
23787 69 6c 64 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  ild the internal
23788 20 64 61 74 61 0a 20 20 20 20 2a 2a 20 73 74 72   data.    ** str
23789 75 63 74 75 72 65 73 20 74 68 61 74 20 64 65 73  uctures that des
2378a 63 72 69 62 65 20 74 68 65 20 74 61 62 6c 65 2c  cribe the table,
2378b 20 69 6e 64 65 78 2c 20 6f 72 20 76 69 65 77 2e   index, or view.
2378c 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 68 61 72  .    */.    char
2378d 20 2a 7a 45 72 72 3b 0a 20 20 20 20 69 6e 74 20   *zErr;.    int 
2378e 72 63 3b 0a 20 20 20 20 75 38 20 6c 6f 6f 6b 61  rc;.    u8 looka
2378f 73 69 64 65 45 6e 61 62 6c 65 64 3b 0a 20 20 20  sideEnabled;.   
23790 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69   assert( db->ini
23791 74 2e 62 75 73 79 20 29 3b 0a 20 20 20 20 64 62  t.busy );.    db
23792 2d 3e 69 6e 69 74 2e 69 44 62 20 3d 20 69 44 62  ->init.iDb = iDb
23793 3b 0a 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 6e  ;.    db->init.n
23794 65 77 54 6e 75 6d 20 3d 20 61 74 6f 69 28 61 72  ewTnum = atoi(ar
23795 67 76 5b 31 5d 29 3b 0a 20 20 20 20 6c 6f 6f 6b  gv[1]);.    look
23796 61 73 69 64 65 45 6e 61 62 6c 65 64 20 3d 20 64  asideEnabled = d
23797 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e  b->lookaside.bEn
23798 61 62 6c 65 64 3b 0a 20 20 20 20 64 62 2d 3e 6c  abled;.    db->l
23799 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65  ookaside.bEnable
2379a 64 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20  d = 0;.    rc = 
2379b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c  sqlite3_exec(db,
2379c 20 61 72 67 76 5b 32 5d 2c 20 30 2c 20 30 2c 20   argv[2], 0, 0, 
2379d 26 7a 45 72 72 29 3b 0a 20 20 20 20 64 62 2d 3e  &zErr);.    db->
2379e 69 6e 69 74 2e 69 44 62 20 3d 20 30 3b 0a 20 20  init.iDb = 0;.  
2379f 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
237a0 62 45 6e 61 62 6c 65 64 20 3d 20 6c 6f 6f 6b 61  bEnabled = looka
237a1 73 69 64 65 45 6e 61 62 6c 65 64 3b 0a 20 20 20  sideEnabled;.   
237a2 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
237a3 49 54 45 5f 4f 4b 20 7c 7c 20 7a 45 72 72 3d 3d  ITE_OK || zErr==
237a4 30 20 29 3b 0a 20 20 20 20 69 66 28 20 53 51 4c  0 );.    if( SQL
237a5 49 54 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20 20  ITE_OK!=rc ){.  
237a6 20 20 20 20 70 44 61 74 61 2d 3e 72 63 20 3d 20      pData->rc = 
237a7 72 63 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  rc;.      if( rc
237a8 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
237a9 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61  {.        db->ma
237aa 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
237ab 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
237ac 72 63 21 3d 53 51 4c 49 54 45 5f 49 4e 54 45 52  rc!=SQLITE_INTER
237ad 52 55 50 54 20 29 7b 0a 20 20 20 20 20 20 20 20  RUPT ){.        
237ae 63 6f 72 72 75 70 74 53 63 68 65 6d 61 28 70 44  corruptSchema(pD
237af 61 74 61 2c 20 61 72 67 76 5b 30 5d 2c 20 7a 45  ata, argv[0], zE
237b0 72 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rr);.      }.   
237b1 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
237b2 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20  (db, zErr);.    
237b3 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 61 72  }.  }else if( ar
237b4 67 76 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20  gv[0]==0 ){.    
237b5 63 6f 72 72 75 70 74 53 63 68 65 6d 61 28 70 44  corruptSchema(pD
237b6 61 74 61 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 65  ata, 0, 0);.  }e
237b7 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74  lse{.    /* If t
237b8 68 65 20 53 51 4c 20 63 6f 6c 75 6d 6e 20 69 73  he SQL column is
237b9 20 62 6c 61 6e 6b 20 69 74 20 6d 65 61 6e 73 20   blank it means 
237ba 74 68 69 73 20 69 73 20 61 6e 20 69 6e 64 65 78  this is an index
237bb 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 77 61 73   that.    ** was
237bc 20 63 72 65 61 74 65 64 20 74 6f 20 62 65 20 74   created to be t
237bd 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f  he PRIMARY KEY o
237be 72 20 74 6f 20 66 75 6c 66 69 6c 6c 20 61 20 55  r to fulfill a U
237bf 4e 49 51 55 45 0a 20 20 20 20 2a 2a 20 63 6f 6e  NIQUE.    ** con
237c0 73 74 72 61 69 6e 74 20 66 6f 72 20 61 20 43 52  straint for a CR
237c1 45 41 54 45 20 54 41 42 4c 45 2e 20 20 54 68 65  EATE TABLE.  The
237c2 20 69 6e 64 65 78 20 73 68 6f 75 6c 64 20 68 61   index should ha
237c3 76 65 20 61 6c 72 65 61 64 79 0a 20 20 20 20 2a  ve already.    *
237c4 2a 20 62 65 65 6e 20 63 72 65 61 74 65 64 20 77  * been created w
237c5 68 65 6e 20 77 65 20 70 72 6f 63 65 73 73 65 64  hen we processed
237c6 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c   the CREATE TABL
237c7 45 2e 20 20 41 6c 6c 20 77 65 20 68 61 76 65 0a  E.  All we have.
237c8 20 20 20 20 2a 2a 20 74 6f 20 64 6f 20 68 65 72      ** to do her
237c9 65 20 69 73 20 72 65 63 6f 72 64 20 74 68 65 20  e is record the 
237ca 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72  root page number
237cb 20 66 6f 72 20 74 68 61 74 20 69 6e 64 65 78 2e   for that index.
237cc 0a 20 20 20 20 2a 2f 0a 20 20 20 20 49 6e 64 65  .    */.    Inde
237cd 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 20 20 70  x *pIndex;.    p
237ce 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 46  Index = sqlite3F
237cf 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 61 72 67  indIndex(db, arg
237d0 76 5b 30 5d 2c 20 64 62 2d 3e 61 44 62 5b 69 44  v[0], db->aDb[iD
237d1 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 69  b].zName);.    i
237d2 66 28 20 70 49 6e 64 65 78 3d 3d 30 20 7c 7c 20  f( pIndex==0 || 
237d3 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 21 3d 30 20  pIndex->tnum!=0 
237d4 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73  ){.      /* This
237d5 20 63 61 6e 20 6f 63 63 75 72 20 69 66 20 74 68   can occur if th
237d6 65 72 65 20 65 78 69 73 74 73 20 61 6e 20 69 6e  ere exists an in
237d7 64 65 78 20 6f 6e 20 61 20 54 45 4d 50 20 74 61  dex on a TEMP ta
237d8 62 6c 65 20 77 68 69 63 68 0a 20 20 20 20 20 20  ble which.      
237d9 2a 2a 20 68 61 73 20 74 68 65 20 73 61 6d 65 20  ** has the same 
237da 6e 61 6d 65 20 61 73 20 61 6e 6f 74 68 65 72 20  name as another 
237db 69 6e 64 65 78 20 6f 6e 20 61 20 70 65 72 6d 61  index on a perma
237dc 6e 65 6e 74 20 69 6e 64 65 78 2e 20 20 53 69 6e  nent index.  Sin
237dd 63 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  ce.      ** the 
237de 70 65 72 6d 61 6e 65 6e 74 20 74 61 62 6c 65 20  permanent table 
237df 69 73 20 68 69 64 64 65 6e 20 62 79 20 74 68 65  is hidden by the
237e0 20 54 45 4d 50 20 74 61 62 6c 65 2c 20 77 65 20   TEMP table, we 
237e1 63 61 6e 20 61 6c 73 6f 0a 20 20 20 20 20 20 2a  can also.      *
237e2 2a 20 73 61 66 65 6c 79 20 69 67 6e 6f 72 65 20  * safely ignore 
237e3 74 68 65 20 69 6e 64 65 78 20 6f 6e 20 74 68 65  the index on the
237e4 20 70 65 72 6d 61 6e 65 6e 74 20 74 61 62 6c 65   permanent table
237e5 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
237e6 20 2f 2a 20 44 6f 20 4e 6f 74 68 69 6e 67 20 2a   /* Do Nothing *
237e7 2f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  /;.    }else{.  
237e8 20 20 20 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d      pIndex->tnum
237e9 20 3d 20 61 74 6f 69 28 61 72 67 76 5b 31 5d 29   = atoi(argv[1])
237ea 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
237eb 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
237ec 20 41 74 74 65 6d 70 74 20 74 6f 20 72 65 61 64   Attempt to read
237ed 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
237ee 68 65 6d 61 20 61 6e 64 20 69 6e 69 74 69 61 6c  hema and initial
237ef 69 7a 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20  ize internal.** 
237f0 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20  data structures 
237f1 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 61 74  for a single dat
237f2 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65  abase file.  The
237f3 20 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a   index of the.**
237f4 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
237f5 73 20 67 69 76 65 6e 20 62 79 20 69 44 62 2e 20  s given by iDb. 
237f6 20 69 44 62 3d 3d 30 20 69 73 20 75 73 65 64 20   iDb==0 is used 
237f7 66 6f 72 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20  for the main.** 
237f8 64 61 74 61 62 61 73 65 2e 20 20 69 44 62 3d 3d  database.  iDb==
237f9 31 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62  1 should never b
237fa 65 20 75 73 65 64 2e 20 20 69 44 62 3e 3d 32 20  e used.  iDb>=2 
237fb 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 61  is used for.** a
237fc 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73  uxiliary databas
237fd 65 73 2e 20 20 52 65 74 75 72 6e 20 6f 6e 65 20  es.  Return one 
237fe 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 20 65  of the SQLITE_ e
237ff 72 72 6f 72 20 63 6f 64 65 73 20 74 6f 0a 2a 2a  rror codes to.**
23800 20 69 6e 64 69 63 61 74 65 20 73 75 63 63 65 73   indicate succes
23801 73 20 6f 72 20 66 61 69 6c 75 72 65 2e 0a 2a 2f  s or failure..*/
23802 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
23803 74 65 33 49 6e 69 74 4f 6e 65 28 73 71 6c 69 74  te3InitOne(sqlit
23804 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c  e3 *db, int iDb,
23805 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67   char **pzErrMsg
23806 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42  ){.  int rc;.  B
23807 74 43 75 72 73 6f 72 20 2a 63 75 72 4d 61 69 6e  tCursor *curMain
23808 3b 0a 20 20 69 6e 74 20 73 69 7a 65 3b 0a 20 20  ;.  int size;.  
23809 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 44  Table *pTab;.  D
2380a 62 20 2a 70 44 62 3b 0a 20 20 63 68 61 72 20 63  b *pDb;.  char c
2380b 6f 6e 73 74 20 2a 61 7a 41 72 67 5b 34 5d 3b 0a  onst *azArg[4];.
2380c 20 20 69 6e 74 20 6d 65 74 61 5b 31 30 5d 3b 0a    int meta[10];.
2380d 20 20 49 6e 69 74 44 61 74 61 20 69 6e 69 74 44    InitData initD
2380e 61 74 61 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73  ata;.  char cons
2380f 74 20 2a 7a 4d 61 73 74 65 72 53 63 68 65 6d 61  t *zMasterSchema
23810 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ;.  char const *
23811 7a 4d 61 73 74 65 72 4e 61 6d 65 20 3d 20 53 43  zMasterName = SC
23812 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b  HEMA_TABLE(iDb);
23813 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  /*.  ** The 
23814 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 65 20  master database 
23815 74 61 62 6c 65 20 68 61 73 20 61 20 73 74 72 75  table has a stru
23816 63 74 75 72 65 20 6c 69 6b 65 20 74 68 69 73 0a  cture like this.
23817 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f    */.  static co
23818 6e 73 74 20 63 68 61 72 20 6d 61 73 74 65 72 5f  nst char master_
23819 73 63 68 65 6d 61 5b 5d 20 3d 20 0a 20 20 20 20  schema[] = .    
2381a 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 73   "CREATE TABLE s
2381b 71 6c 69 74 65 5f 6d 61 73 74 65 72 28 5c 6e 22  qlite_master(\n"
2381c 0a 20 20 20 20 20 22 20 20 74 79 70 65 20 74 65  .     "  type te
2381d 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 6e  xt,\n".     "  n
2381e 61 6d 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20  ame text,\n".   
2381f 20 20 22 20 20 74 62 6c 5f 6e 61 6d 65 20 74 65    "  tbl_name te
23820 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 72  xt,\n".     "  r
23821 6f 6f 74 70 61 67 65 20 69 6e 74 65 67 65 72 2c  ootpage integer,
23822 5c 6e 22 0a 20 20 20 20 20 22 20 20 73 71 6c 20  \n".     "  sql 
23823 74 65 78 74 5c 6e 22 0a 20 20 20 20 20 22 29 22  text\n".     ")"
23824 0a 20 20 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  .  ;.#ifndef SQL
23825 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a  ITE_OMIT_TEMPDB.
23826 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
23827 68 61 72 20 74 65 6d 70 5f 6d 61 73 74 65 72 5f  har temp_master_
23828 73 63 68 65 6d 61 5b 5d 20 3d 20 0a 20 20 20 20  schema[] = .    
23829 20 22 43 52 45 41 54 45 20 54 45 4d 50 20 54 41   "CREATE TEMP TA
2382a 42 4c 45 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f  BLE sqlite_temp_
2382b 6d 61 73 74 65 72 28 5c 6e 22 0a 20 20 20 20 20  master(\n".     
2382c 22 20 20 74 79 70 65 20 74 65 78 74 2c 5c 6e 22  "  type text,\n"
2382d 0a 20 20 20 20 20 22 20 20 6e 61 6d 65 20 74 65  .     "  name te
2382e 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 74  xt,\n".     "  t
2382f 62 6c 5f 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22  bl_name text,\n"
23830 0a 20 20 20 20 20 22 20 20 72 6f 6f 74 70 61 67  .     "  rootpag
23831 65 20 69 6e 74 65 67 65 72 2c 5c 6e 22 0a 20 20  e integer,\n".  
23832 20 20 20 22 20 20 73 71 6c 20 74 65 78 74 5c 6e     "  sql text\n
23833 22 0a 20 20 20 20 20 22 29 22 0a 20 20 3b 0a 23  ".     ")".  ;.#
23834 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 74  else.  #define t
23835 65 6d 70 5f 6d 61 73 74 65 72 5f 73 63 68 65 6d  emp_master_schem
23836 61 20 30 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73  a 0.#endif..  as
23837 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20  sert( iDb>=0 && 
23838 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20  iDb<db->nDb );. 
23839 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62   assert( db->aDb
2383a 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 20 29 3b  [iDb].pSchema );
2383b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
2383c 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
2383d 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
2383e 73 65 72 74 28 20 69 44 62 3d 3d 31 20 7c 7c 20  sert( iDb==1 || 
2383f 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
23840 73 4d 75 74 65 78 28 64 62 2d 3e 61 44 62 5b 69  sMutex(db->aDb[i
23841 44 62 5d 2e 70 42 74 29 20 29 3b 0a 0a 20 20 2f  Db].pBt) );..  /
23842 2a 20 7a 4d 61 73 74 65 72 53 63 68 65 6d 61 20  * zMasterSchema 
23843 61 6e 64 20 7a 49 6e 69 74 53 63 72 69 70 74 20  and zInitScript 
23844 61 72 65 20 73 65 74 20 74 6f 20 70 6f 69 6e 74  are set to point
23845 20 61 74 20 74 68 65 20 6d 61 73 74 65 72 20 73   at the master s
23846 63 68 65 6d 61 0a 20 20 2a 2a 20 61 6e 64 20 69  chema.  ** and i
23847 6e 69 74 69 61 6c 69 73 61 74 69 6f 6e 20 73 63  nitialisation sc
23848 72 69 70 74 20 61 70 70 72 6f 70 72 69 61 74 65  ript appropriate
23849 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73   for the databas
2384a 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 69 6e 69  e being.  ** ini
2384b 74 69 61 6c 69 73 65 64 2e 20 7a 4d 61 73 74 65  tialised. zMaste
2384c 72 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d  rName is the nam
2384d 65 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20  e of the master 
2384e 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  table..  */.  if
2384f 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26  ( !OMIT_TEMPDB &
23850 26 20 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20  & iDb==1 ){.    
23851 7a 4d 61 73 74 65 72 53 63 68 65 6d 61 20 3d 20  zMasterSchema = 
23852 74 65 6d 70 5f 6d 61 73 74 65 72 5f 73 63 68 65  temp_master_sche
23853 6d 61 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ma;.  }else{.   
23854 20 7a 4d 61 73 74 65 72 53 63 68 65 6d 61 20 3d   zMasterSchema =
23855 20 6d 61 73 74 65 72 5f 73 63 68 65 6d 61 3b 0a   master_schema;.
23856 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 4e 61 6d    }.  zMasterNam
23857 65 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45  e = SCHEMA_TABLE
23858 28 69 44 62 29 3b 0a 0a 20 20 2f 2a 20 43 6f 6e  (iDb);..  /* Con
23859 73 74 72 75 63 74 20 74 68 65 20 73 63 68 65 6d  struct the schem
2385a 61 20 74 61 62 6c 65 73 2e 20 20 2a 2f 0a 20 20  a tables.  */.  
2385b 61 7a 41 72 67 5b 30 5d 20 3d 20 7a 4d 61 73 74  azArg[0] = zMast
2385c 65 72 4e 61 6d 65 3b 0a 20 20 61 7a 41 72 67 5b  erName;.  azArg[
2385d 31 5d 20 3d 20 22 31 22 3b 0a 20 20 61 7a 41 72  1] = "1";.  azAr
2385e 67 5b 32 5d 20 3d 20 7a 4d 61 73 74 65 72 53 63  g[2] = zMasterSc
2385f 68 65 6d 61 3b 0a 20 20 61 7a 41 72 67 5b 33 5d  hema;.  azArg[3]
23860 20 3d 20 30 3b 0a 20 20 69 6e 69 74 44 61 74 61   = 0;.  initData
23861 2e 64 62 20 3d 20 64 62 3b 0a 20 20 69 6e 69 74  .db = db;.  init
23862 44 61 74 61 2e 69 44 62 20 3d 20 69 44 62 3b 0a  Data.iDb = iDb;.
23863 20 20 69 6e 69 74 44 61 74 61 2e 72 63 20 3d 20    initData.rc = 
23864 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 69  SQLITE_OK;.  ini
23865 74 44 61 74 61 2e 70 7a 45 72 72 4d 73 67 20 3d  tData.pzErrMsg =
23866 20 70 7a 45 72 72 4d 73 67 3b 0a 20 20 28 76 6f   pzErrMsg;.  (vo
23867 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
23868 4f 66 66 28 64 62 29 3b 0a 20 20 73 71 6c 69 74  Off(db);.  sqlit
23869 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 28 26  e3InitCallback(&
2386a 69 6e 69 74 44 61 74 61 2c 20 33 2c 20 28 63 68  initData, 3, (ch
2386b 61 72 20 2a 2a 29 61 7a 41 72 67 2c 20 30 29 3b  ar **)azArg, 0);
2386c 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33  .  (void)sqlite3
2386d 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20  SafetyOn(db);.  
2386e 69 66 28 20 69 6e 69 74 44 61 74 61 2e 72 63 20  if( initData.rc 
2386f 29 7b 0a 20 20 20 20 72 63 20 3d 20 69 6e 69 74  ){.    rc = init
23870 44 61 74 61 2e 72 63 3b 0a 20 20 20 20 67 6f 74  Data.rc;.    got
23871 6f 20 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d  o error_out;.  }
23872 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
23873 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
23874 4d 61 73 74 65 72 4e 61 6d 65 2c 20 64 62 2d 3e  MasterName, db->
23875 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b  aDb[iDb].zName);
23876 0a 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20  .  if( pTab ){. 
23877 20 20 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67     pTab->tabFlag
23878 73 20 7c 3d 20 54 46 5f 52 65 61 64 6f 6e 6c 79  s |= TF_Readonly
23879 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61  ;.  }..  /* Crea
2387a 74 65 20 61 20 63 75 72 73 6f 72 20 74 6f 20 68  te a cursor to h
2387b 6f 6c 64 20 74 68 65 20 64 61 74 61 62 61 73 65  old the database
2387c 20 6f 70 65 6e 0a 20 20 2a 2f 0a 20 20 70 44 62   open.  */.  pDb
2387d 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d   = &db->aDb[iDb]
2387e 3b 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 42 74  ;.  if( pDb->pBt
2387f 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21  ==0 ){.    if( !
23880 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69  OMIT_TEMPDB && i
23881 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 44  Db==1 ){.      D
23882 62 53 65 74 50 72 6f 70 65 72 74 79 28 64 62 2c  bSetProperty(db,
23883 20 31 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61   1, DB_SchemaLoa
23884 64 65 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ded);.    }.    
23885 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
23886 3b 0a 20 20 7d 0a 20 20 63 75 72 4d 61 69 6e 20  ;.  }.  curMain 
23887 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a  = sqlite3MallocZ
23888 65 72 6f 28 73 71 6c 69 74 65 33 42 74 72 65 65  ero(sqlite3Btree
23889 43 75 72 73 6f 72 53 69 7a 65 28 29 29 3b 0a 20  CursorSize());. 
2388a 20 69 66 28 20 21 63 75 72 4d 61 69 6e 20 29 7b   if( !curMain ){
2388b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
2388c 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f  _NOMEM;.    goto
2388d 20 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a   error_out;.  }.
2388e 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
2388f 74 65 72 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20  ter(pDb->pBt);. 
23890 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
23891 65 65 43 75 72 73 6f 72 28 70 44 62 2d 3e 70 42  eeCursor(pDb->pB
23892 74 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20  t, MASTER_ROOT, 
23893 30 2c 20 30 2c 20 63 75 72 4d 61 69 6e 29 3b 0a  0, 0, curMain);.
23894 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
23895 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54  _OK && rc!=SQLIT
23896 45 5f 45 4d 50 54 59 20 29 7b 0a 20 20 20 20 73  E_EMPTY ){.    s
23897 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
23898 70 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25  pzErrMsg, db, "%
23899 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74  s", sqlite3ErrSt
2389a 72 28 72 63 29 29 3b 0a 20 20 20 20 67 6f 74 6f  r(rc));.    goto
2389b 20 69 6e 69 74 6f 6e 65 5f 65 72 72 6f 72 5f 6f   initone_error_o
2389c 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65  ut;.  }..  /* Ge
2389d 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d  t the database m
2389e 65 74 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  eta information.
2389f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d 65 74 61 20  .  **.  ** Meta 
238a0 76 61 6c 75 65 73 20 61 72 65 20 61 73 20 66 6f  values are as fo
238a1 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 20 20 20 20 6d  llows:.  **    m
238a2 65 74 61 5b 30 5d 20 20 20 53 63 68 65 6d 61 20  eta[0]   Schema 
238a3 63 6f 6f 6b 69 65 2e 20 20 43 68 61 6e 67 65 73  cookie.  Changes
238a4 20 77 69 74 68 20 65 61 63 68 20 73 63 68 65 6d   with each schem
238a5 61 20 63 68 61 6e 67 65 2e 0a 20 20 2a 2a 20 20  a change..  **  
238a6 20 20 6d 65 74 61 5b 31 5d 20 20 20 46 69 6c 65    meta[1]   File
238a7 20 66 6f 72 6d 61 74 20 6f 66 20 73 63 68 65 6d   format of schem
238a8 61 20 6c 61 79 65 72 2e 0a 20 20 2a 2a 20 20 20  a layer..  **   
238a9 20 6d 65 74 61 5b 32 5d 20 20 20 53 69 7a 65 20   meta[2]   Size 
238aa 6f 66 20 74 68 65 20 70 61 67 65 20 63 61 63 68  of the page cach
238ab 65 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b  e..  **    meta[
238ac 33 5d 20 20 20 55 73 65 20 66 72 65 65 6c 69 73  3]   Use freelis
238ad 74 20 69 66 20 30 2e 20 20 41 75 74 6f 76 61 63  t if 0.  Autovac
238ae 75 75 6d 20 69 66 20 67 72 65 61 74 65 72 20 74  uum if greater t
238af 68 61 6e 20 7a 65 72 6f 2e 0a 20 20 2a 2a 20 20  han zero..  **  
238b0 20 20 6d 65 74 61 5b 34 5d 20 20 20 44 62 20 74    meta[4]   Db t
238b1 65 78 74 20 65 6e 63 6f 64 69 6e 67 2e 20 31 3a  ext encoding. 1:
238b2 55 54 46 2d 38 20 32 3a 55 54 46 2d 31 36 4c 45  UTF-8 2:UTF-16LE
238b3 20 33 3a 55 54 46 2d 31 36 42 45 0a 20 20 2a 2a   3:UTF-16BE.  **
238b4 20 20 20 20 6d 65 74 61 5b 35 5d 20 20 20 54 68      meta[5]   Th
238b5 65 20 75 73 65 72 20 63 6f 6f 6b 69 65 2e 20 55  e user cookie. U
238b6 73 65 64 20 62 79 20 74 68 65 20 61 70 70 6c 69  sed by the appli
238b7 63 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 20 20 20  cation..  **    
238b8 6d 65 74 61 5b 36 5d 20 20 20 49 6e 63 72 65 6d  meta[6]   Increm
238b9 65 6e 74 61 6c 2d 76 61 63 75 75 6d 20 66 6c 61  ental-vacuum fla
238ba 67 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b  g..  **    meta[
238bb 37 5d 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b  7].  **    meta[
238bc 38 5d 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b  8].  **    meta[
238bd 39 5d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74  9].  **.  ** Not
238be 65 3a 20 54 68 65 20 23 64 65 66 69 6e 65 64 20  e: The #defined 
238bf 53 51 4c 49 54 45 5f 55 54 46 2a 20 73 79 6d 62  SQLITE_UTF* symb
238c0 6f 6c 73 20 69 6e 20 73 71 6c 69 74 65 49 6e 74  ols in sqliteInt
238c1 2e 68 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f  .h correspond to
238c2 0a 20 20 2a 2a 20 74 68 65 20 70 6f 73 73 69 62  .  ** the possib
238c3 6c 65 20 76 61 6c 75 65 73 20 6f 66 20 6d 65 74  le values of met
238c4 61 5b 34 5d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  a[4]..  */.  if(
238c5 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
238c6 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  {.    int i;.   
238c7 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65   for(i=0; i<size
238c8 6f 66 28 6d 65 74 61 29 2f 73 69 7a 65 6f 66 28  of(meta)/sizeof(
238c9 6d 65 74 61 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a  meta[0]); i++){.
238ca 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
238cb 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 70  e3BtreeGetMeta(p
238cc 44 62 2d 3e 70 42 74 2c 20 69 2b 31 2c 20 28 75  Db->pBt, i+1, (u
238cd 33 32 20 2a 29 26 6d 65 74 61 5b 69 5d 29 3b 0a  32 *)&meta[i]);.
238ce 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
238cf 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
238d0 65 74 53 74 72 69 6e 67 28 70 7a 45 72 72 4d 73  etString(pzErrMs
238d1 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c  g, db, "%s", sql
238d2 69 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b  ite3ErrStr(rc));
238d3 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 69 6e  .        goto in
238d4 69 74 6f 6e 65 5f 65 72 72 6f 72 5f 6f 75 74 3b  itone_error_out;
238d5 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
238d6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 73   }else{.    mems
238d7 65 74 28 6d 65 74 61 2c 20 30 2c 20 73 69 7a 65  et(meta, 0, size
238d8 6f 66 28 6d 65 74 61 29 29 3b 0a 20 20 7d 0a 20  of(meta));.  }. 
238d9 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73   pDb->pSchema->s
238da 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 20 3d 20 6d  chema_cookie = m
238db 65 74 61 5b 30 5d 3b 0a 0a 20 20 2f 2a 20 49 66  eta[0];..  /* If
238dc 20 6f 70 65 6e 69 6e 67 20 61 20 6e 6f 6e 2d 65   opening a non-e
238dd 6d 70 74 79 20 64 61 74 61 62 61 73 65 2c 20 63  mpty database, c
238de 68 65 63 6b 20 74 68 65 20 74 65 78 74 20 65 6e  heck the text en
238df 63 6f 64 69 6e 67 2e 20 46 6f 72 20 74 68 65 0a  coding. For the.
238e0 20 20 2a 2a 20 6d 61 69 6e 20 64 61 74 61 62 61    ** main databa
238e1 73 65 2c 20 73 65 74 20 73 71 6c 69 74 65 33 2e  se, set sqlite3.
238e2 65 6e 63 20 74 6f 20 74 68 65 20 65 6e 63 6f 64  enc to the encod
238e3 69 6e 67 20 6f 66 20 74 68 65 20 6d 61 69 6e 20  ing of the main 
238e4 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2a 20 46  database..  ** F
238e5 6f 72 20 61 6e 20 61 74 74 61 63 68 65 64 20 64  or an attached d
238e6 62 2c 20 69 74 20 69 73 20 61 6e 20 65 72 72 6f  b, it is an erro
238e7 72 20 69 66 20 74 68 65 20 65 6e 63 6f 64 69 6e  r if the encodin
238e8 67 20 69 73 20 6e 6f 74 20 74 68 65 20 73 61 6d  g is not the sam
238e9 65 0a 20 20 2a 2a 20 61 73 20 73 71 6c 69 74 65  e.  ** as sqlite
238ea 33 2e 65 6e 63 2e 0a 20 20 2a 2f 0a 20 20 69 66  3.enc..  */.  if
238eb 28 20 6d 65 74 61 5b 34 5d 20 29 7b 20 20 2f 2a  ( meta[4] ){  /*
238ec 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a   text encoding *
238ed 2f 0a 20 20 20 20 69 66 28 20 69 44 62 3d 3d 30  /.    if( iDb==0
238ee 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20   ){.      /* If 
238ef 6f 70 65 6e 69 6e 67 20 74 68 65 20 6d 61 69 6e  opening the main
238f0 20 64 61 74 61 62 61 73 65 2c 20 73 65 74 20 45   database, set E
238f1 4e 43 28 64 62 29 2e 20 2a 2f 0a 20 20 20 20 20  NC(db). */.     
238f2 20 45 4e 43 28 64 62 29 20 3d 20 28 75 38 29 6d   ENC(db) = (u8)m
238f3 65 74 61 5b 34 5d 3b 0a 20 20 20 20 20 20 64 62  eta[4];.      db
238f4 2d 3e 70 44 66 6c 74 43 6f 6c 6c 20 3d 20 73 71  ->pDfltColl = sq
238f5 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
238f6 28 64 62 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  (db, SQLITE_UTF8
238f7 2c 20 22 42 49 4e 41 52 59 22 2c 20 36 2c 20 30  , "BINARY", 6, 0
238f8 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
238f9 20 20 20 20 2f 2a 20 49 66 20 6f 70 65 6e 69 6e      /* If openin
238fa 67 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61  g an attached da
238fb 74 61 62 61 73 65 2c 20 74 68 65 20 65 6e 63 6f  tabase, the enco
238fc 64 69 6e 67 20 6d 75 63 68 20 6d 61 74 63 68 20  ding much match 
238fd 45 4e 43 28 64 62 29 20 2a 2f 0a 20 20 20 20 20  ENC(db) */.     
238fe 20 69 66 28 20 6d 65 74 61 5b 34 5d 21 3d 45 4e   if( meta[4]!=EN
238ff 43 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20 20  C(db) ){.       
23900 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
23901 67 28 70 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  g(pzErrMsg, db, 
23902 22 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61  "attached databa
23903 73 65 73 20 6d 75 73 74 20 75 73 65 20 74 68 65  ses must use the
23904 20 73 61 6d 65 22 0a 20 20 20 20 20 20 20 20 20   same".         
23905 20 20 20 22 20 74 65 78 74 20 65 6e 63 6f 64 69     " text encodi
23906 6e 67 20 61 73 20 6d 61 69 6e 20 64 61 74 61 62  ng as main datab
23907 61 73 65 22 29 3b 0a 20 20 20 20 20 20 20 20 72  ase");.        r
23908 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
23909 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 69  ;.        goto i
2390a 6e 69 74 6f 6e 65 5f 65 72 72 6f 72 5f 6f 75 74  nitone_error_out
2390b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2390c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 44 62 53    }else{.    DbS
2390d 65 74 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69  etProperty(db, i
2390e 44 62 2c 20 44 42 5f 45 6d 70 74 79 29 3b 0a 20  Db, DB_Empty);. 
2390f 20 7d 0a 20 20 70 44 62 2d 3e 70 53 63 68 65 6d   }.  pDb->pSchem
23910 61 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 64 62 29  a->enc = ENC(db)
23911 3b 0a 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 53  ;..  if( pDb->pS
23912 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a  chema->cache_siz
23913 65 3d 3d 30 20 29 7b 0a 20 20 20 20 73 69 7a 65  e==0 ){.    size
23914 20 3d 20 6d 65 74 61 5b 32 5d 3b 0a 20 20 20 20   = meta[2];.    
23915 69 66 28 20 73 69 7a 65 3d 3d 30 20 29 7b 20 73  if( size==0 ){ s
23916 69 7a 65 20 3d 20 53 51 4c 49 54 45 5f 44 45 46  ize = SQLITE_DEF
23917 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 3b  AULT_CACHE_SIZE;
23918 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65 3c   }.    if( size<
23919 30 20 29 20 73 69 7a 65 20 3d 20 2d 73 69 7a 65  0 ) size = -size
2391a 3b 0a 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65  ;.    pDb->pSche
2391b 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 20 3d  ma->cache_size =
2391c 20 73 69 7a 65 3b 0a 20 20 20 20 73 71 6c 69 74   size;.    sqlit
2391d 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53  e3BtreeSetCacheS
2391e 69 7a 65 28 70 44 62 2d 3e 70 42 74 2c 20 70 44  ize(pDb->pBt, pD
2391f 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68  b->pSchema->cach
23920 65 5f 73 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20  e_size);.  }..  
23921 2f 2a 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f 72  /*.  ** file_for
23922 6d 61 74 3d 3d 31 20 20 20 20 56 65 72 73 69 6f  mat==1    Versio
23923 6e 20 33 2e 30 2e 30 2e 0a 20 20 2a 2a 20 66 69  n 3.0.0..  ** fi
23924 6c 65 5f 66 6f 72 6d 61 74 3d 3d 32 20 20 20 20  le_format==2    
23925 56 65 72 73 69 6f 6e 20 33 2e 31 2e 33 2e 20 20  Version 3.1.3.  
23926 2f 2f 20 41 4c 54 45 52 20 54 41 42 4c 45 20 41  // ALTER TABLE A
23927 44 44 20 43 4f 4c 55 4d 4e 0a 20 20 2a 2a 20 66  DD COLUMN.  ** f
23928 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 33 20 20 20  ile_format==3   
23929 20 56 65 72 73 69 6f 6e 20 33 2e 31 2e 34 2e 20   Version 3.1.4. 
2392a 20 2f 2f 20 64 69 74 74 6f 20 62 75 74 20 77 69   // ditto but wi
2392b 74 68 20 6e 6f 6e 2d 4e 55 4c 4c 20 64 65 66 61  th non-NULL defa
2392c 75 6c 74 73 0a 20 20 2a 2a 20 66 69 6c 65 5f 66  ults.  ** file_f
2392d 6f 72 6d 61 74 3d 3d 34 20 20 20 20 56 65 72 73  ormat==4    Vers
2392e 69 6f 6e 20 33 2e 33 2e 30 2e 20 20 2f 2f 20 44  ion 3.3.0.  // D
2392f 45 53 43 20 69 6e 64 69 63 65 73 2e 20 20 42 6f  ESC indices.  Bo
23930 6f 6c 65 61 6e 20 63 6f 6e 73 74 61 6e 74 73 0a  olean constants.
23931 20 20 2a 2f 0a 20 20 70 44 62 2d 3e 70 53 63 68    */.  pDb->pSch
23932 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74  ema->file_format
23933 20 3d 20 6d 65 74 61 5b 31 5d 3b 0a 20 20 69 66   = meta[1];.  if
23934 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e  ( pDb->pSchema->
23935 66 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 30 20 29  file_format==0 )
23936 7b 0a 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65  {.    pDb->pSche
23937 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20  ma->file_format 
23938 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  = 1;.  }.  if( p
23939 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c  Db->pSchema->fil
2393a 65 5f 66 6f 72 6d 61 74 3e 53 51 4c 49 54 45 5f  e_format>SQLITE_
2393b 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20  MAX_FILE_FORMAT 
2393c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  ){.    sqlite3Se
2393d 74 53 74 72 69 6e 67 28 70 7a 45 72 72 4d 73 67  tString(pzErrMsg
2393e 2c 20 64 62 2c 20 22 75 6e 73 75 70 70 6f 72 74  , db, "unsupport
2393f 65 64 20 66 69 6c 65 20 66 6f 72 6d 61 74 22 29  ed file format")
23940 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
23941 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 67 6f 74  E_ERROR;.    got
23942 6f 20 69 6e 69 74 6f 6e 65 5f 65 72 72 6f 72 5f  o initone_error_
23943 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54  out;.  }..  /* T
23944 69 63 6b 65 74 20 23 32 38 30 34 3a 20 20 57 68  icket #2804:  Wh
23945 65 6e 20 77 65 20 6f 70 65 6e 20 61 20 64 61 74  en we open a dat
23946 61 62 61 73 65 20 69 6e 20 74 68 65 20 6e 65 77  abase in the new
23947 65 72 20 66 69 6c 65 20 66 6f 72 6d 61 74 2c 0a  er file format,.
23948 20 20 2a 2a 20 63 6c 65 61 72 20 74 68 65 20 6c    ** clear the l
23949 65 67 61 63 79 5f 66 69 6c 65 5f 66 6f 72 6d 61  egacy_file_forma
2394a 74 20 70 72 61 67 6d 61 20 66 6c 61 67 20 73 6f  t pragma flag so
2394b 20 74 68 61 74 20 61 20 56 41 43 55 55 4d 20 77   that a VACUUM w
2394c 69 6c 6c 0a 20 20 2a 2a 20 6e 6f 74 20 64 6f 77  ill.  ** not dow
2394d 6e 67 72 61 64 65 20 74 68 65 20 64 61 74 61 62  ngrade the datab
2394e 61 73 65 20 61 6e 64 20 74 68 75 73 20 69 6e 76  ase and thus inv
2394f 61 6c 69 64 61 74 65 20 61 6e 79 20 64 65 73 63  alidate any desc
23950 65 6e 64 69 6e 67 0a 20 20 2a 2a 20 69 6e 64 69  ending.  ** indi
23951 63 65 73 20 74 68 61 74 20 74 68 65 20 75 73 65  ces that the use
23952 72 20 6d 69 67 68 74 20 68 61 76 65 20 63 72 65  r might have cre
23953 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ated..  */.  if(
23954 20 69 44 62 3d 3d 30 20 26 26 20 6d 65 74 61 5b   iDb==0 && meta[
23955 31 5d 3e 3d 34 20 29 7b 0a 20 20 20 20 64 62 2d  1]>=4 ){.    db-
23956 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 54  >flags &= ~SQLIT
23957 45 5f 4c 65 67 61 63 79 46 69 6c 65 46 6d 74 3b  E_LegacyFileFmt;
23958 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20  .  }..  /* Read 
23959 74 68 65 20 73 63 68 65 6d 61 20 69 6e 66 6f 72  the schema infor
2395a 6d 61 74 69 6f 6e 20 6f 75 74 20 6f 66 20 74 68  mation out of th
2395b 65 20 73 63 68 65 6d 61 20 74 61 62 6c 65 73 0a  e schema tables.
2395c 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64    */.  assert( d
2395d 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 3b 0a  b->init.busy );.
2395e 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
2395f 5f 45 4d 50 54 59 20 29 7b 0a 20 20 20 20 2f 2a  _EMPTY ){.    /*
23960 20 46 6f 72 20 61 6e 20 65 6d 70 74 79 20 64 61   For an empty da
23961 74 61 62 61 73 65 2c 20 74 68 65 72 65 20 69 73  tabase, there is
23962 20 6e 6f 74 68 69 6e 67 20 74 6f 20 72 65 61 64   nothing to read
23963 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   */.    rc = SQL
23964 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b  ITE_OK;.  }else{
23965 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b  .    char *zSql;
23966 0a 20 20 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69  .    zSql = sqli
23967 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 0a  te3MPrintf(db, .
23968 20 20 20 20 20 20 20 20 22 53 45 4c 45 43 54 20          "SELECT 
23969 6e 61 6d 65 2c 20 72 6f 6f 74 70 61 67 65 2c 20  name, rootpage, 
2396a 73 71 6c 20 46 52 4f 4d 20 27 25 71 27 2e 25 73  sql FROM '%q'.%s
2396b 22 2c 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61  ",.        db->a
2396c 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a  Db[iDb].zName, z
2396d 4d 61 73 74 65 72 4e 61 6d 65 29 3b 0a 20 20 20  MasterName);.   
2396e 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
2396f 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 23 69 66  fetyOff(db);.#if
23970 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
23971 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20  _AUTHORIZATION. 
23972 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 28     {.      int (
23973 2a 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e  *xAuth)(void*,in
23974 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  t,const char*,co
23975 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
23976 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
23977 2a 29 3b 0a 20 20 20 20 20 20 78 41 75 74 68 20  *);.      xAuth 
23978 3d 20 64 62 2d 3e 78 41 75 74 68 3b 0a 20 20 20  = db->xAuth;.   
23979 20 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 30     db->xAuth = 0
2397a 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 72  ;.#endif.      r
2397b 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  c = sqlite3_exec
2397c 28 64 62 2c 20 7a 53 71 6c 2c 20 73 71 6c 69 74  (db, zSql, sqlit
2397d 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2c 20  e3InitCallback, 
2397e 26 69 6e 69 74 44 61 74 61 2c 20 30 29 3b 0a 23  &initData, 0);.#
2397f 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
23980 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
23981 0a 20 20 20 20 20 20 64 62 2d 3e 78 41 75 74 68  .      db->xAuth
23982 20 3d 20 78 41 75 74 68 3b 0a 20 20 20 20 7d 0a   = xAuth;.    }.
23983 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 72  #endif.    if( r
23984 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c==SQLITE_OK ) r
23985 63 20 3d 20 69 6e 69 74 44 61 74 61 2e 72 63 3b  c = initData.rc;
23986 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74  .    (void)sqlit
23987 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a  e3SafetyOn(db);.
23988 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
23989 65 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 23 69 66  e(db, zSql);.#if
2398a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2398b 5f 41 4e 41 4c 59 5a 45 0a 20 20 20 20 69 66 28  _ANALYZE.    if(
2398c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
2398d 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 41  {.      sqlite3A
2398e 6e 61 6c 79 73 69 73 4c 6f 61 64 28 64 62 2c 20  nalysisLoad(db, 
2398f 69 44 62 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  iDb);.    }.#end
23990 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d  if.  }.  if( db-
23991 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
23992 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
23993 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 73 71 6c 69  _NOMEM;.    sqli
23994 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c  te3ResetInternal
23995 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20  Schema(db, 0);. 
23996 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c   }.  if( rc==SQL
23997 49 54 45 5f 4f 4b 20 7c 7c 20 28 64 62 2d 3e 66  ITE_OK || (db->f
23998 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 63 6f  lags&SQLITE_Reco
23999 76 65 72 79 4d 6f 64 65 29 29 7b 0a 20 20 20 20  veryMode)){.    
2399a 2f 2a 20 42 6c 61 63 6b 20 6d 61 67 69 63 3a 20  /* Black magic: 
2399b 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 52 65  If the SQLITE_Re
2399c 63 6f 76 65 72 79 4d 6f 64 65 20 66 6c 61 67 20  coveryMode flag 
2399d 69 73 20 73 65 74 2c 20 74 68 65 6e 20 63 6f 6e  is set, then con
2399e 73 69 64 65 72 0a 20 20 20 20 2a 2a 20 74 68 65  sider.    ** the
2399f 20 73 63 68 65 6d 61 20 6c 6f 61 64 65 64 2c 20   schema loaded, 
239a0 65 76 65 6e 20 69 66 20 65 72 72 6f 72 73 20 6f  even if errors o
239a1 63 63 75 72 65 64 2e 20 49 6e 20 74 68 69 73 20  ccured. In this 
239a2 73 69 74 75 61 74 69 6f 6e 20 74 68 65 20 0a 20  situation the . 
239a3 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 73 71     ** current sq
239a4 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20  lite3_prepare() 
239a5 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c 6c 20 66  operation will f
239a6 61 69 6c 2c 20 62 75 74 20 74 68 65 20 66 6f 6c  ail, but the fol
239a7 6c 6f 77 69 6e 67 20 6f 6e 65 0a 20 20 20 20 2a  lowing one.    *
239a8 2a 20 77 69 6c 6c 20 61 74 74 65 6d 70 74 20 74  * will attempt t
239a9 6f 20 63 6f 6d 70 69 6c 65 20 74 68 65 20 73 75  o compile the su
239aa 70 70 6c 69 65 64 20 73 74 61 74 65 6d 65 6e 74  pplied statement
239ab 20 61 67 61 69 6e 73 74 20 77 68 61 74 65 76 65   against whateve
239ac 72 20 73 75 62 73 65 74 0a 20 20 20 20 2a 2a 20  r subset.    ** 
239ad 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20 77 61  of the schema wa
239ae 73 20 6c 6f 61 64 65 64 20 62 65 66 6f 72 65 20  s loaded before 
239af 74 68 65 20 65 72 72 6f 72 20 6f 63 63 75 72 65  the error occure
239b0 64 2e 20 54 68 65 20 70 72 69 6d 61 72 79 0a 20  d. The primary. 
239b1 20 20 20 2a 2a 20 70 75 72 70 6f 73 65 20 6f 66     ** purpose of
239b2 20 74 68 69 73 20 69 73 20 74 6f 20 61 6c 6c 6f   this is to allo
239b3 77 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20  w access to the 
239b4 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
239b5 62 6c 65 0a 20 20 20 20 2a 2a 20 65 76 65 6e 20  ble.    ** even 
239b6 77 68 65 6e 20 69 74 73 20 63 6f 6e 74 65 6e 74  when its content
239b7 73 20 68 61 76 65 20 62 65 65 6e 20 63 6f 72 72  s have been corr
239b8 75 70 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  upted..    */.  
239b9 20 20 44 62 53 65 74 50 72 6f 70 65 72 74 79 28    DbSetProperty(
239ba 64 62 2c 20 69 44 62 2c 20 44 42 5f 53 63 68 65  db, iDb, DB_Sche
239bb 6d 61 4c 6f 61 64 65 64 29 3b 0a 20 20 20 20 72  maLoaded);.    r
239bc 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
239bd 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65   }..  /* Jump he
239be 72 65 20 66 6f 72 20 61 6e 20 65 72 72 6f 72 20  re for an error 
239bf 74 68 61 74 20 6f 63 63 75 72 73 20 61 66 74 65  that occurs afte
239c0 72 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61  r successfully a
239c1 6c 6c 6f 63 61 74 69 6e 67 0a 20 20 2a 2a 20 63  llocating.  ** c
239c2 75 72 4d 61 69 6e 20 61 6e 64 20 63 61 6c 6c 69  urMain and calli
239c3 6e 67 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  ng sqlite3BtreeE
239c4 6e 74 65 72 28 29 2e 20 46 6f 72 20 61 6e 20 65  nter(). For an e
239c5 72 72 6f 72 20 74 68 61 74 20 6f 63 63 75 72 73  rror that occurs
239c6 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20 74 68 61  .  ** before tha
239c7 74 20 70 6f 69 6e 74 2c 20 6a 75 6d 70 20 74 6f  t point, jump to
239c8 20 65 72 72 6f 72 5f 6f 75 74 2e 0a 20 20 2a 2f   error_out..  */
239c9 0a 69 6e 69 74 6f 6e 65 5f 65 72 72 6f 72 5f 6f  .initone_error_o
239ca 75 74 3a 0a 20 20 73 71 6c 69 74 65 33 42 74 72  ut:.  sqlite3Btr
239cb 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 63 75  eeCloseCursor(cu
239cc 72 4d 61 69 6e 29 3b 0a 20 20 73 71 6c 69 74 65  rMain);.  sqlite
239cd 33 5f 66 72 65 65 28 63 75 72 4d 61 69 6e 29 3b  3_free(curMain);
239ce 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
239cf 65 61 76 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a  eave(pDb->pBt);.
239d0 0a 65 72 72 6f 72 5f 6f 75 74 3a 0a 20 20 69 66  .error_out:.  if
239d1 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
239d2 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45  EM || rc==SQLITE
239d3 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a  _IOERR_NOMEM ){.
239d4 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
239d5 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  iled = 1;.  }.  
239d6 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
239d7 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61  .** Initialize a
239d8 6c 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ll database file
239d9 73 20 2d 20 74 68 65 20 6d 61 69 6e 20 64 61 74  s - the main dat
239da 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 20  abase file, the 
239db 66 69 6c 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20  file.** used to 
239dc 73 74 6f 72 65 20 74 65 6d 70 6f 72 61 72 79 20  store temporary 
239dd 74 61 62 6c 65 73 2c 20 61 6e 64 20 61 6e 79 20  tables, and any 
239de 61 64 64 69 74 69 6f 6e 61 6c 20 64 61 74 61 62  additional datab
239df 61 73 65 20 66 69 6c 65 73 0a 2a 2a 20 63 72 65  ase files.** cre
239e0 61 74 65 64 20 75 73 69 6e 67 20 41 54 54 41 43  ated using ATTAC
239e1 48 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 52  H statements.  R
239e2 65 74 75 72 6e 20 61 20 73 75 63 63 65 73 73 20  eturn a success 
239e3 63 6f 64 65 2e 20 20 49 66 20 61 6e 0a 2a 2a 20  code.  If an.** 
239e4 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 77 72  error occurs, wr
239e5 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ite an error mes
239e6 73 61 67 65 20 69 6e 74 6f 20 2a 70 7a 45 72 72  sage into *pzErr
239e7 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72  Msg..**.** After
239e8 20 61 20 64 61 74 61 62 61 73 65 20 69 73 20 69   a database is i
239e9 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68 65 20  nitialized, the 
239ea 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 20  DB_SchemaLoaded 
239eb 62 69 74 20 69 73 20 73 65 74 0a 2a 2a 20 62 69  bit is set.** bi
239ec 74 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 20  t is set in the 
239ed 66 6c 61 67 73 20 66 69 65 6c 64 20 6f 66 20 74  flags field of t
239ee 68 65 20 44 62 20 73 74 72 75 63 74 75 72 65 2e  he Db structure.
239ef 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65   If the database
239f0 0a 2a 2a 20 66 69 6c 65 20 77 61 73 20 6f 66 20  .** file was of 
239f1 7a 65 72 6f 2d 6c 65 6e 67 74 68 2c 20 74 68 65  zero-length, the
239f2 6e 20 74 68 65 20 44 42 5f 45 6d 70 74 79 20 66  n the DB_Empty f
239f3 6c 61 67 20 69 73 20 61 6c 73 6f 20 73 65 74 2e  lag is also set.
239f4 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
239f5 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e  TE int sqlite3In
239f6 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  it(sqlite3 *db, 
239f7 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 29  char **pzErrMsg)
239f8 7b 0a 20 20 69 6e 74 20 69 2c 20 72 63 3b 0a 20  {.  int i, rc;. 
239f9 20 69 6e 74 20 63 6f 6d 6d 69 74 5f 69 6e 74 65   int commit_inte
239fa 72 6e 61 6c 20 3d 20 21 28 64 62 2d 3e 66 6c 61  rnal = !(db->fla
239fb 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e  gs&SQLITE_Intern
239fc 43 68 61 6e 67 65 73 29 3b 0a 20 20 0a 20 20 61  Changes);.  .  a
239fd 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
239fe 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75  utex_held(db->mu
239ff 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 64 62  tex) );.  if( db
23a00 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 20 72 65  ->init.busy ) re
23a01 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
23a02 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
23a03 3b 0a 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  ;.  db->init.bus
23a04 79 20 3d 20 31 3b 0a 20 20 66 6f 72 28 69 3d 30  y = 1;.  for(i=0
23a05 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ; rc==SQLITE_OK 
23a06 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  && i<db->nDb; i+
23a07 2b 29 7b 0a 20 20 20 20 69 66 28 20 44 62 48 61  +){.    if( DbHa
23a08 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 2c  sProperty(db, i,
23a09 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64   DB_SchemaLoaded
23a0a 29 20 7c 7c 20 69 3d 3d 31 20 29 20 63 6f 6e 74  ) || i==1 ) cont
23a0b 69 6e 75 65 3b 0a 20 20 20 20 72 63 20 3d 20 73  inue;.    rc = s
23a0c 71 6c 69 74 65 33 49 6e 69 74 4f 6e 65 28 64 62  qlite3InitOne(db
23a0d 2c 20 69 2c 20 70 7a 45 72 72 4d 73 67 29 3b 0a  , i, pzErrMsg);.
23a0e 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
23a0f 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 74      sqlite3Reset
23a10 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64  InternalSchema(d
23a11 62 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  b, i);.    }.  }
23a12 0a 0a 20 20 2f 2a 20 4f 6e 63 65 20 61 6c 6c 20  ..  /* Once all 
23a13 74 68 65 20 6f 74 68 65 72 20 64 61 74 61 62 61  the other databa
23a14 73 65 73 20 68 61 76 65 20 62 65 65 6e 20 69 6e  ses have been in
23a15 69 74 69 61 6c 69 73 65 64 2c 20 6c 6f 61 64 20  itialised, load 
23a16 74 68 65 20 73 63 68 65 6d 61 0a 20 20 2a 2a 20  the schema.  ** 
23a17 66 6f 72 20 74 68 65 20 54 45 4d 50 20 64 61 74  for the TEMP dat
23a18 61 62 61 73 65 2e 20 54 68 69 73 20 69 73 20 6c  abase. This is l
23a19 6f 61 64 65 64 20 6c 61 73 74 2c 20 61 73 20 74  oaded last, as t
23a1a 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65  he TEMP database
23a1b 0a 20 20 2a 2a 20 73 63 68 65 6d 61 20 6d 61 79  .  ** schema may
23a1c 20 63 6f 6e 74 61 69 6e 20 72 65 66 65 72 65 6e   contain referen
23a1d 63 65 73 20 74 6f 20 6f 62 6a 65 63 74 73 20 69  ces to objects i
23a1e 6e 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65  n other database
23a1f 73 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20  s..  */.#ifndef 
23a20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50  SQLITE_OMIT_TEMP
23a21 44 42 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  DB.  if( rc==SQL
23a22 49 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 6e 44  ITE_OK && db->nD
23a23 62 3e 31 20 26 26 20 21 44 62 48 61 73 50 72 6f  b>1 && !DbHasPro
23a24 70 65 72 74 79 28 64 62 2c 20 31 2c 20 44 42 5f  perty(db, 1, DB_
23a25 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 20 29 7b  SchemaLoaded) ){
23a26 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
23a27 33 49 6e 69 74 4f 6e 65 28 64 62 2c 20 31 2c 20  3InitOne(db, 1, 
23a28 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 69  pzErrMsg);.    i
23a29 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 73  f( rc ){.      s
23a2a 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
23a2b 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 31 29  nalSchema(db, 1)
23a2c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
23a2d 69 66 0a 0a 20 20 64 62 2d 3e 69 6e 69 74 2e 62  if..  db->init.b
23a2e 75 73 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 72  usy = 0;.  if( r
23a2f 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
23a30 63 6f 6d 6d 69 74 5f 69 6e 74 65 72 6e 61 6c 20  commit_internal 
23a31 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f  ){.    sqlite3Co
23a32 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e  mmitInternalChan
23a33 67 65 73 28 64 62 29 3b 0a 20 20 7d 0a 0a 20 20  ges(db);.  }..  
23a34 72 65 74 75 72 6e 20 72 63 3b 20 0a 7d 0a 0a 2f  return rc; .}../
23a35 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
23a36 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20  e is a no-op if 
23a37 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
23a38 65 6d 61 20 69 73 20 61 6c 72 65 61 64 79 20 69  ema is already i
23a39 6e 69 74 69 61 6c 69 73 65 64 2e 0a 2a 2a 20 4f  nitialised..** O
23a3a 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 73 63  therwise, the sc
23a3b 68 65 6d 61 20 69 73 20 6c 6f 61 64 65 64 2e 20  hema is loaded. 
23a3c 41 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  An error code is
23a3d 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51   returned..*/.SQ
23a3e 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
23a3f 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
23a40 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ma(Parse *pParse
23a41 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
23a42 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74  LITE_OK;.  sqlit
23a43 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
23a44 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 73  >db;.  assert( s
23a45 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
23a46 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
23a47 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e    if( !db->init.
23a48 62 75 73 79 20 29 7b 0a 20 20 20 20 72 63 20 3d  busy ){.    rc =
23a49 20 73 71 6c 69 74 65 33 49 6e 69 74 28 64 62 2c   sqlite3Init(db,
23a4a 20 26 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73   &pParse->zErrMs
23a4b 67 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63  g);.  }.  if( rc
23a4c 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
23a4d 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
23a4e 72 63 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  rc;.    pParse->
23a4f 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65  nErr++;.  }.  re
23a50 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
23a51 2a 2a 20 43 68 65 63 6b 20 73 63 68 65 6d 61 20  ** Check schema 
23a52 63 6f 6f 6b 69 65 73 20 69 6e 20 61 6c 6c 20 64  cookies in all d
23a53 61 74 61 62 61 73 65 73 2e 20 20 49 66 20 61 6e  atabases.  If an
23a54 79 20 63 6f 6f 6b 69 65 20 69 73 20 6f 75 74 0a  y cookie is out.
23a55 2a 2a 20 6f 66 20 64 61 74 65 2c 20 72 65 74 75  ** of date, retu
23a56 72 6e 20 30 2e 20 20 49 66 20 61 6c 6c 20 73 63  rn 0.  If all sc
23a57 68 65 6d 61 20 63 6f 6f 6b 69 65 73 20 61 72 65  hema cookies are
23a58 20 63 75 72 72 65 6e 74 2c 20 72 65 74 75 72 6e   current, return
23a59 20 31 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e   1..*/.static in
23a5a 74 20 73 63 68 65 6d 61 49 73 56 61 6c 69 64 28  t schemaIsValid(
23a5b 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
23a5c 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 72  int iDb;.  int r
23a5d 63 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 63  c;.  BtCursor *c
23a5e 75 72 54 65 6d 70 3b 0a 20 20 69 6e 74 20 63 6f  urTemp;.  int co
23a5f 6f 6b 69 65 3b 0a 20 20 69 6e 74 20 61 6c 6c 4f  okie;.  int allO
23a60 6b 20 3d 20 31 3b 0a 0a 20 20 63 75 72 54 65 6d  k = 1;..  curTem
23a61 70 20 3d 20 28 42 74 43 75 72 73 6f 72 20 2a 29  p = (BtCursor *)
23a62 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 73 71  sqlite3Malloc(sq
23a63 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
23a64 53 69 7a 65 28 29 29 3b 0a 20 20 69 66 28 20 63  Size());.  if( c
23a65 75 72 54 65 6d 70 20 29 7b 0a 20 20 20 20 61 73  urTemp ){.    as
23a66 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
23a67 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
23a68 65 78 29 20 29 3b 0a 20 20 20 20 66 6f 72 28 69  ex) );.    for(i
23a69 44 62 3d 30 3b 20 61 6c 6c 4f 6b 20 26 26 20 69  Db=0; allOk && i
23a6a 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 69 44 62 2b  Db<db->nDb; iDb+
23a6b 2b 29 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20  +){.      Btree 
23a6c 2a 70 42 74 3b 0a 20 20 20 20 20 20 70 42 74 20  *pBt;.      pBt 
23a6d 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  = db->aDb[iDb].p
23a6e 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42  Bt;.      if( pB
23a6f 74 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  t==0 ) continue;
23a70 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 63 75  .      memset(cu
23a71 72 54 65 6d 70 2c 20 30 2c 20 73 71 6c 69 74 65  rTemp, 0, sqlite
23a72 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65  3BtreeCursorSize
23a73 28 29 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  ());.      rc = 
23a74 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
23a75 6f 72 28 70 42 74 2c 20 4d 41 53 54 45 52 5f 52  or(pBt, MASTER_R
23a76 4f 4f 54 2c 20 30 2c 20 30 2c 20 63 75 72 54 65  OOT, 0, 0, curTe
23a77 6d 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  mp);.      if( r
23a78 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
23a79 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
23a7a 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61  ite3BtreeGetMeta
23a7b 28 70 42 74 2c 20 31 2c 20 28 75 33 32 20 2a 29  (pBt, 1, (u32 *)
23a7c 26 63 6f 6f 6b 69 65 29 3b 0a 20 20 20 20 20 20  &cookie);.      
23a7d 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
23a7e 5f 4f 4b 20 26 26 20 63 6f 6f 6b 69 65 21 3d 64  _OK && cookie!=d
23a7f 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68  b->aDb[iDb].pSch
23a80 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b  ema->schema_cook
23a81 69 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ie ){.          
23a82 61 6c 6c 4f 6b 20 3d 20 30 3b 0a 20 20 20 20 20  allOk = 0;.     
23a83 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c     }.        sql
23a84 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75  ite3BtreeCloseCu
23a85 72 73 6f 72 28 63 75 72 54 65 6d 70 29 3b 0a 20  rsor(curTemp);. 
23a86 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
23a87 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
23a88 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f  M || rc==SQLITE_
23a89 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20  IOERR_NOMEM ){. 
23a8a 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f         db->mallo
23a8b 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
23a8c 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
23a8d 71 6c 69 74 65 33 5f 66 72 65 65 28 63 75 72 54  qlite3_free(curT
23a8e 65 6d 70 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  emp);.  }else{. 
23a8f 20 20 20 61 6c 6c 4f 6b 20 3d 20 30 3b 0a 20 20     allOk = 0;.  
23a90 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
23a91 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 72  ed = 1;.  }..  r
23a92 65 74 75 72 6e 20 61 6c 6c 4f 6b 3b 0a 7d 0a 0a  eturn allOk;.}..
23a93 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20  /*.** Convert a 
23a94 73 63 68 65 6d 61 20 70 6f 69 6e 74 65 72 20 69  schema pointer i
23a95 6e 74 6f 20 74 68 65 20 69 44 62 20 69 6e 64 65  nto the iDb inde
23a96 78 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 73  x that indicates
23a97 0a 2a 2a 20 77 68 69 63 68 20 64 61 74 61 62 61  .** which databa
23a98 73 65 20 66 69 6c 65 20 69 6e 20 64 62 2d 3e 61  se file in db->a
23a99 44 62 5b 5d 20 74 68 65 20 73 63 68 65 6d 61 20  Db[] the schema 
23a9a 72 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a  refers to..**.**
23a9b 20 49 66 20 74 68 65 20 73 61 6d 65 20 64 61 74   If the same dat
23a9c 61 62 61 73 65 20 69 73 20 61 74 74 61 63 68 65  abase is attache
23a9d 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65  d more than once
23a9e 2c 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61  , the first.** a
23a9f 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
23aa0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
23aa1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
23aa2 69 6e 74 20 73 71 6c 69 74 65 33 53 63 68 65 6d  int sqlite3Schem
23aa3 61 54 6f 49 6e 64 65 78 28 73 71 6c 69 74 65 33  aToIndex(sqlite3
23aa4 20 2a 64 62 2c 20 53 63 68 65 6d 61 20 2a 70 53   *db, Schema *pS
23aa5 63 68 65 6d 61 29 7b 0a 20 20 69 6e 74 20 69 20  chema){.  int i 
23aa6 3d 20 2d 31 30 30 30 30 30 30 3b 0a 0a 20 20 2f  = -1000000;..  /
23aa7 2a 20 49 66 20 70 53 63 68 65 6d 61 20 69 73 20  * If pSchema is 
23aa8 4e 55 4c 4c 2c 20 74 68 65 6e 20 72 65 74 75 72  NULL, then retur
23aa9 6e 20 2d 31 30 30 30 30 30 30 2e 20 54 68 69 73  n -1000000. This
23aaa 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 63 6f   happens when co
23aab 64 65 20 69 6e 20 0a 20 20 2a 2a 20 65 78 70 72  de in .  ** expr
23aac 2e 63 20 69 73 20 74 72 79 69 6e 67 20 74 6f 20  .c is trying to 
23aad 72 65 73 6f 6c 76 65 20 61 20 72 65 66 65 72 65  resolve a refere
23aae 6e 63 65 20 74 6f 20 61 20 74 72 61 6e 73 69 65  nce to a transie
23aaf 6e 74 20 74 61 62 6c 65 20 28 69 2e 65 2e 20 6f  nt table (i.e. o
23ab0 6e 65 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20  ne.  ** created 
23ab1 62 79 20 61 20 73 75 62 2d 73 65 6c 65 63 74 29  by a sub-select)
23ab2 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
23ab3 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
23ab4 6f 66 20 74 68 69 73 20 0a 20 20 2a 2a 20 66 75  of this .  ** fu
23ab5 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65  nction should ne
23ab6 76 65 72 20 62 65 20 75 73 65 64 2e 0a 20 20 2a  ver be used..  *
23ab7 2a 0a 20 20 2a 2a 20 57 65 20 72 65 74 75 72 6e  *.  ** We return
23ab8 20 2d 31 30 30 30 30 30 30 20 69 6e 73 74 65 61   -1000000 instea
23ab9 64 20 6f 66 20 74 68 65 20 6d 6f 72 65 20 75 73  d of the more us
23aba 75 61 6c 20 2d 31 20 73 69 6d 70 6c 79 20 62 65  ual -1 simply be
23abb 63 61 75 73 65 20 75 73 69 6e 67 0a 20 20 2a 2a  cause using.  **
23abc 20 2d 31 30 30 30 30 30 30 20 61 73 20 69 6e 63   -1000000 as inc
23abd 6f 72 72 65 63 74 6c 79 20 75 73 69 6e 67 20 2d  orrectly using -
23abe 31 30 30 30 30 30 30 20 69 6e 64 65 78 20 69 6e  1000000 index in
23abf 74 6f 20 64 62 2d 3e 61 44 62 5b 5d 20 69 73 20  to db->aDb[] is 
23ac0 6d 75 63 68 20 0a 20 20 2a 2a 20 6d 6f 72 65 20  much .  ** more 
23ac1 6c 69 6b 65 6c 79 20 74 6f 20 63 61 75 73 65 20  likely to cause 
23ac2 61 20 73 65 67 66 61 75 6c 74 20 74 68 61 6e 20  a segfault than 
23ac3 2d 31 20 28 6f 66 20 63 6f 75 72 73 65 20 74 68  -1 (of course th
23ac4 65 72 65 20 61 72 65 20 61 73 73 65 72 74 28 29  ere are assert()
23ac5 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73  .  ** statements
23ac6 20 74 6f 6f 2c 20 62 75 74 20 69 74 20 6e 65 76   too, but it nev
23ac7 65 72 20 68 75 72 74 73 20 74 6f 20 70 6c 61 79  er hurts to play
23ac8 20 74 68 65 20 6f 64 64 73 29 2e 0a 20 20 2a 2f   the odds)..  */
23ac9 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
23aca 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
23acb 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66  ->mutex) );.  if
23acc 28 20 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20  ( pSchema ){.   
23acd 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
23ace 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  nDb; i++){.     
23acf 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e   if( db->aDb[i].
23ad0 70 53 63 68 65 6d 61 3d 3d 70 53 63 68 65 6d 61  pSchema==pSchema
23ad1 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61   ){.        brea
23ad2 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
23ad3 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d  .    assert( i>=
23ad4 30 20 26 26 69 3e 3d 30 20 26 26 20 20 69 3c 64  0 &&i>=0 &&  i<d
23ad5 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 7d 0a 20 20  b->nDb );.  }.  
23ad6 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a 0a  return i;.}../*.
23ad7 2a 2a 20 43 6f 6d 70 69 6c 65 20 74 68 65 20 55  ** Compile the U
23ad8 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c  TF-8 encoded SQL
23ad9 20 73 74 61 74 65 6d 65 6e 74 20 7a 53 71 6c 20   statement zSql 
23ada 69 6e 74 6f 20 61 20 73 74 61 74 65 6d 65 6e 74  into a statement
23adb 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74   handle..*/.stat
23adc 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 50 72  ic int sqlite3Pr
23add 65 70 61 72 65 28 0a 20 20 73 71 6c 69 74 65 33  epare(.  sqlite3
23ade 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
23adf 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68     /* Database h
23ae0 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 63 6f 6e 73  andle. */.  cons
23ae1 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20  t char *zSql,   
23ae2 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65        /* UTF-8 e
23ae3 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65  ncoded SQL state
23ae4 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e  ment. */.  int n
23ae5 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20  Bytes,          
23ae6 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
23ae7 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e  f zSql in bytes.
23ae8 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 53 71   */.  int saveSq
23ae9 6c 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20 20  lFlag,          
23aea 2f 2a 20 54 72 75 65 20 74 6f 20 63 6f 70 79 20  /* True to copy 
23aeb 53 51 4c 20 74 65 78 74 20 69 6e 74 6f 20 74 68  SQL text into th
23aec 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e sqlite3_stmt *
23aed 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
23aee 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a   **ppStmt,    /*
23aef 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20   OUT: A pointer 
23af0 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20  to the prepared 
23af1 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63  statement */.  c
23af2 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61  onst char **pzTa
23af3 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a  il       /* OUT:
23af4 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73   End of parsed s
23af5 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 50 61  tring */.){.  Pa
23af6 72 73 65 20 73 50 61 72 73 65 3b 0a 20 20 63 68  rse sParse;.  ch
23af7 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30 3b  ar *zErrMsg = 0;
23af8 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
23af9 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 3b 0a  TE_OK;.  int i;.
23afa 0a 20 20 61 73 73 65 72 74 28 20 70 70 53 74 6d  .  assert( ppStm
23afb 74 20 29 3b 0a 20 20 2a 70 70 53 74 6d 74 20 3d  t );.  *ppStmt =
23afc 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65   0;.  if( sqlite
23afd 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b  3SafetyOn(db) ){
23afe 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
23aff 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20  TE_MISUSE;.  }. 
23b00 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61   assert( !db->ma
23b01 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
23b02 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
23b03 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d  mutex_held(db->m
23b04 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 49  utex) );..  /* I
23b05 66 20 61 6e 79 20 61 74 74 61 63 68 65 64 20 64  f any attached d
23b06 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 73 20  atabase schemas 
23b07 61 72 65 20 6c 6f 63 6b 65 64 2c 20 64 6f 20 6e  are locked, do n
23b08 6f 74 20 70 72 6f 63 65 65 64 20 77 69 74 68 0a  ot proceed with.
23b09 20 20 2a 2a 20 63 6f 6d 70 69 6c 61 74 69 6f 6e    ** compilation
23b0a 2e 20 49 6e 73 74 65 61 64 20 72 65 74 75 72 6e  . Instead return
23b0b 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69   SQLITE_LOCKED i
23b0c 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20 2a 2f  mmediately..  */
23b0d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62  .  for(i=0; i<db
23b0e 2d 3e 6e 44 62 3b 20 69 2b 2b 29 20 7b 0a 20 20  ->nDb; i++) {.  
23b0f 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64    Btree *pBt = d
23b10 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20  b->aDb[i].pBt;. 
23b11 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20     if( pBt ){.  
23b12 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20      int rc;.    
23b13 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
23b14 72 65 65 53 63 68 65 6d 61 4c 6f 63 6b 65 64 28  reeSchemaLocked(
23b15 70 42 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  pBt);.      if( 
23b16 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f  rc ){.        co
23b17 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20  nst char *zDb = 
23b18 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65  db->aDb[i].zName
23b19 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
23b1a 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
23b1b 45 5f 4c 4f 43 4b 45 44 2c 20 22 64 61 74 61 62  E_LOCKED, "datab
23b1c 61 73 65 20 73 63 68 65 6d 61 20 69 73 20 6c 6f  ase schema is lo
23b1d 63 6b 65 64 3a 20 25 73 22 2c 20 7a 44 62 29 3b  cked: %s", zDb);
23b1e 0a 20 20 20 20 20 20 20 20 28 76 6f 69 64 29 73  .        (void)s
23b1f 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
23b20 64 62 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  db);.        ret
23b21 75 72 6e 20 73 71 6c 69 74 65 33 41 70 69 45 78  urn sqlite3ApiEx
23b22 69 74 28 64 62 2c 20 53 51 4c 49 54 45 5f 4c 4f  it(db, SQLITE_LO
23b23 43 4b 45 44 29 3b 0a 20 20 20 20 20 20 7d 0a 20  CKED);.      }. 
23b24 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 6d 65     }.  }.  .  me
23b25 6d 73 65 74 28 26 73 50 61 72 73 65 2c 20 30 2c  mset(&sParse, 0,
23b26 20 73 69 7a 65 6f 66 28 73 50 61 72 73 65 29 29   sizeof(sParse))
23b27 3b 0a 20 20 73 50 61 72 73 65 2e 64 62 20 3d 20  ;.  sParse.db = 
23b28 64 62 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73  db;.  if( nBytes
23b29 3e 3d 30 20 26 26 20 28 6e 42 79 74 65 73 3d 3d  >=0 && (nBytes==
23b2a 30 20 7c 7c 20 7a 53 71 6c 5b 6e 42 79 74 65 73  0 || zSql[nBytes
23b2b 2d 31 5d 21 3d 30 29 20 29 7b 0a 20 20 20 20 63  -1]!=0) ){.    c
23b2c 68 61 72 20 2a 7a 53 71 6c 43 6f 70 79 3b 0a 20  har *zSqlCopy;. 
23b2d 20 20 20 69 6e 74 20 6d 78 4c 65 6e 20 3d 20 64     int mxLen = d
23b2e 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
23b2f 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54  _LIMIT_SQL_LENGT
23b30 48 5d 3b 0a 20 20 20 20 69 66 28 20 6e 42 79 74  H];.    if( nByt
23b31 65 73 3e 6d 78 4c 65 6e 20 29 7b 0a 20 20 20 20  es>mxLen ){.    
23b32 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
23b33 62 2c 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47  b, SQLITE_TOOBIG
23b34 2c 20 22 73 74 61 74 65 6d 65 6e 74 20 74 6f 6f  , "statement too
23b35 20 6c 6f 6e 67 22 29 3b 0a 20 20 20 20 20 20 28   long");.      (
23b36 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
23b37 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20  tyOff(db);.     
23b38 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41   return sqlite3A
23b39 70 69 45 78 69 74 28 64 62 2c 20 53 51 4c 49 54  piExit(db, SQLIT
23b3a 45 5f 54 4f 4f 42 49 47 29 3b 0a 20 20 20 20 7d  E_TOOBIG);.    }
23b3b 0a 20 20 20 20 7a 53 71 6c 43 6f 70 79 20 3d 20  .    zSqlCopy = 
23b3c 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70  sqlite3DbStrNDup
23b3d 28 64 62 2c 20 7a 53 71 6c 2c 20 6e 42 79 74 65  (db, zSql, nByte
23b3e 73 29 3b 0a 20 20 20 20 69 66 28 20 7a 53 71 6c  s);.    if( zSql
23b3f 43 6f 70 79 20 29 7b 0a 20 20 20 20 20 20 73 71  Copy ){.      sq
23b40 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72 28 26  lite3RunParser(&
23b41 73 50 61 72 73 65 2c 20 7a 53 71 6c 43 6f 70 79  sParse, zSqlCopy
23b42 2c 20 26 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20  , &zErrMsg);.   
23b43 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
23b44 28 64 62 2c 20 7a 53 71 6c 43 6f 70 79 29 3b 0a  (db, zSqlCopy);.
23b45 20 20 20 20 20 20 73 50 61 72 73 65 2e 7a 54 61        sParse.zTa
23b46 69 6c 20 3d 20 26 7a 53 71 6c 5b 73 50 61 72 73  il = &zSql[sPars
23b47 65 2e 7a 54 61 69 6c 2d 7a 53 71 6c 43 6f 70 79  e.zTail-zSqlCopy
23b48 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  ];.    }else{.  
23b49 20 20 20 20 73 50 61 72 73 65 2e 7a 54 61 69 6c      sParse.zTail
23b4a 20 3d 20 26 7a 53 71 6c 5b 6e 42 79 74 65 73 5d   = &zSql[nBytes]
23b4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
23b4c 0a 20 20 20 20 73 71 6c 69 74 65 33 52 75 6e 50  .    sqlite3RunP
23b4d 61 72 73 65 72 28 26 73 50 61 72 73 65 2c 20 7a  arser(&sParse, z
23b4e 53 71 6c 2c 20 26 7a 45 72 72 4d 73 67 29 3b 0a  Sql, &zErrMsg);.
23b4f 20 20 7d 0a 0a 20 20 69 66 28 20 64 62 2d 3e 6d    }..  if( db->m
23b50 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
23b51 20 20 20 73 50 61 72 73 65 2e 72 63 20 3d 20 53     sParse.rc = S
23b52 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
23b53 0a 20 20 69 66 28 20 73 50 61 72 73 65 2e 72 63  .  if( sParse.rc
23b54 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20  ==SQLITE_DONE ) 
23b55 73 50 61 72 73 65 2e 72 63 20 3d 20 53 51 4c 49  sParse.rc = SQLI
23b56 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 73 50 61  TE_OK;.  if( sPa
23b57 72 73 65 2e 63 68 65 63 6b 53 63 68 65 6d 61 20  rse.checkSchema 
23b58 26 26 20 21 73 63 68 65 6d 61 49 73 56 61 6c 69  && !schemaIsVali
23b59 64 28 64 62 29 20 29 7b 0a 20 20 20 20 73 50 61  d(db) ){.    sPa
23b5a 72 73 65 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f  rse.rc = SQLITE_
23b5b 53 43 48 45 4d 41 3b 0a 20 20 7d 0a 20 20 69 66  SCHEMA;.  }.  if
23b5c 28 20 73 50 61 72 73 65 2e 72 63 3d 3d 53 51 4c  ( sParse.rc==SQL
23b5d 49 54 45 5f 53 43 48 45 4d 41 20 29 7b 0a 20 20  ITE_SCHEMA ){.  
23b5e 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
23b5f 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
23b60 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64   0);.  }.  if( d
23b61 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
23b62 29 7b 0a 20 20 20 20 73 50 61 72 73 65 2e 72 63  ){.    sParse.rc
23b63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
23b64 0a 20 20 7d 0a 20 20 69 66 28 20 70 7a 54 61 69  .  }.  if( pzTai
23b65 6c 20 29 7b 0a 20 20 20 20 2a 70 7a 54 61 69 6c  l ){.    *pzTail
23b66 20 3d 20 73 50 61 72 73 65 2e 7a 54 61 69 6c 3b   = sParse.zTail;
23b67 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 50 61 72  .  }.  rc = sPar
23b68 73 65 2e 72 63 3b 0a 0a 23 69 66 6e 64 65 66 20  se.rc;..#ifndef 
23b69 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c  SQLITE_OMIT_EXPL
23b6a 41 49 4e 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  AIN.  if( rc==SQ
23b6b 4c 49 54 45 5f 4f 4b 20 26 26 20 73 50 61 72 73  LITE_OK && sPars
23b6c 65 2e 70 56 64 62 65 20 26 26 20 73 50 61 72 73  e.pVdbe && sPars
23b6d 65 2e 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20  e.explain ){.   
23b6e 20 69 66 28 20 73 50 61 72 73 65 2e 65 78 70 6c   if( sParse.expl
23b6f 61 69 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20  ain==2 ){.      
23b70 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
23b71 6d 43 6f 6c 73 28 73 50 61 72 73 65 2e 70 56 64  mCols(sParse.pVd
23b72 62 65 2c 20 33 29 3b 0a 20 20 20 20 20 20 73 71  be, 3);.      sq
23b73 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
23b74 61 6d 65 28 73 50 61 72 73 65 2e 70 56 64 62 65  ame(sParse.pVdbe
23b75 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 0, COLNAME_NAM
23b76 45 2c 20 22 6f 72 64 65 72 22 2c 20 53 51 4c 49  E, "order", SQLI
23b77 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
23b78 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
23b79 43 6f 6c 4e 61 6d 65 28 73 50 61 72 73 65 2e 70  ColName(sParse.p
23b7a 56 64 62 65 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45  Vdbe, 1, COLNAME
23b7b 5f 4e 41 4d 45 2c 20 22 66 72 6f 6d 22 2c 20 53  _NAME, "from", S
23b7c 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
23b7d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
23b7e 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72 73  SetColName(sPars
23b7f 65 2e 70 56 64 62 65 2c 20 32 2c 20 43 4f 4c 4e  e.pVdbe, 2, COLN
23b80 41 4d 45 5f 4e 41 4d 45 2c 20 22 64 65 74 61 69  AME_NAME, "detai
23b81 6c 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  l", SQLITE_STATI
23b82 43 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  C);.    }else{. 
23b83 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
23b84 53 65 74 4e 75 6d 43 6f 6c 73 28 73 50 61 72 73  SetNumCols(sPars
23b85 65 2e 70 56 64 62 65 2c 20 38 29 3b 0a 20 20 20  e.pVdbe, 8);.   
23b86 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
23b87 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72 73 65 2e  tColName(sParse.
23b88 70 56 64 62 65 2c 20 30 2c 20 43 4f 4c 4e 41 4d  pVdbe, 0, COLNAM
23b89 45 5f 4e 41 4d 45 2c 20 22 61 64 64 72 22 2c 20  E_NAME, "addr", 
23b8a 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
23b8b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
23b8c 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72  eSetColName(sPar
23b8d 73 65 2e 70 56 64 62 65 2c 20 31 2c 20 43 4f 4c  se.pVdbe, 1, COL
23b8e 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6f 70 63 6f  NAME_NAME, "opco
23b8f 64 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  de", SQLITE_STAT
23b90 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
23b91 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
23b92 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20 32  (sParse.pVdbe, 2
23b93 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
23b94 22 70 31 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  "p1", SQLITE_STA
23b95 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  TIC);.      sqli
23b96 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
23b97 65 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20  e(sParse.pVdbe, 
23b98 33 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  3, COLNAME_NAME,
23b99 20 22 70 32 22 2c 20 53 51 4c 49 54 45 5f 53 54   "p2", SQLITE_ST
23b9a 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c  ATIC);.      sql
23b9b 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
23b9c 6d 65 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c  me(sParse.pVdbe,
23b9d 20 34 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   4, COLNAME_NAME
23b9e 2c 20 22 70 33 22 2c 20 53 51 4c 49 54 45 5f 53  , "p3", SQLITE_S
23b9f 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71  TATIC);.      sq
23ba0 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
23ba1 61 6d 65 28 73 50 61 72 73 65 2e 70 56 64 62 65  ame(sParse.pVdbe
23ba2 2c 20 35 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 5, COLNAME_NAM
23ba3 45 2c 20 22 70 34 22 2c 20 53 51 4c 49 54 45 5f  E, "p4", SQLITE_
23ba4 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73  STATIC);.      s
23ba5 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
23ba6 4e 61 6d 65 28 73 50 61 72 73 65 2e 70 56 64 62  Name(sParse.pVdb
23ba7 65 2c 20 36 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  e, 6, COLNAME_NA
23ba8 4d 45 2c 20 22 70 35 22 2c 20 53 51 4c 49 54 45  ME, "p5", SQLITE
23ba9 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
23baa 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
23bab 6c 4e 61 6d 65 28 73 50 61 72 73 65 2e 70 56 64  lName(sParse.pVd
23bac 62 65 2c 20 37 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  be, 7, COLNAME_N
23bad 41 4d 45 2c 20 22 63 6f 6d 6d 65 6e 74 22 2c 20  AME, "comment", 
23bae 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
23baf 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
23bb0 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ..  if( sqlite3S
23bb1 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 7b 0a  afetyOff(db) ){.
23bb2 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
23bb3 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 0a 20 20 69  MISUSE;.  }..  i
23bb4 66 28 20 73 61 76 65 53 71 6c 46 6c 61 67 20 29  f( saveSqlFlag )
23bb5 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
23bb6 65 53 65 74 53 71 6c 28 73 50 61 72 73 65 2e 70  eSetSql(sParse.p
23bb7 56 64 62 65 2c 20 7a 53 71 6c 2c 20 73 50 61 72  Vdbe, zSql, sPar
23bb8 73 65 2e 7a 54 61 69 6c 20 2d 20 7a 53 71 6c 29  se.zTail - zSql)
23bb9 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 3d  ;.  }.  if( rc!=
23bba 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 64 62 2d  SQLITE_OK || db-
23bbb 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
23bbc 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e  .    sqlite3_fin
23bbd 61 6c 69 7a 65 28 28 73 71 6c 69 74 65 33 5f 73  alize((sqlite3_s
23bbe 74 6d 74 2a 29 73 50 61 72 73 65 2e 70 56 64 62  tmt*)sParse.pVdb
23bbf 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 21  e);.    assert(!
23bc0 28 2a 70 70 53 74 6d 74 29 29 3b 0a 20 20 7d 65  (*ppStmt));.  }e
23bc1 6c 73 65 7b 0a 20 20 20 20 2a 70 70 53 74 6d 74  lse{.    *ppStmt
23bc2 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74   = (sqlite3_stmt
23bc3 2a 29 73 50 61 72 73 65 2e 70 56 64 62 65 3b 0a  *)sParse.pVdbe;.
23bc4 20 20 7d 0a 0a 20 20 69 66 28 20 7a 45 72 72 4d    }..  if( zErrM
23bc5 73 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  sg ){.    sqlite
23bc6 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 22  3Error(db, rc, "
23bc7 25 73 22 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20  %s", zErrMsg);. 
23bc8 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
23bc9 28 64 62 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20  (db, zErrMsg);. 
23bca 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
23bcb 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c  te3Error(db, rc,
23bcc 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d   0);.  }..  rc =
23bcd 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
23bce 64 62 2c 20 72 63 29 3b 0a 20 20 61 73 73 65 72  db, rc);.  asser
23bcf 74 28 20 28 72 63 26 64 62 2d 3e 65 72 72 4d 61  t( (rc&db->errMa
23bd0 73 6b 29 3d 3d 72 63 20 29 3b 0a 20 20 72 65 74  sk)==rc );.  ret
23bd1 75 72 6e 20 72 63 3b 0a 7d 0a 73 74 61 74 69 63  urn rc;.}.static
23bd2 20 69 6e 74 20 73 71 6c 69 74 65 33 4c 6f 63 6b   int sqlite3Lock
23bd3 41 6e 64 50 72 65 70 61 72 65 28 0a 20 20 73 71  AndPrepare(.  sq
23bd4 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
23bd5 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
23bd6 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20  ase handle. */. 
23bd7 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71   const char *zSq
23bd8 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54  l,         /* UT
23bd9 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c 20  F-8 encoded SQL 
23bda 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20  statement. */.  
23bdb 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20  int nBytes,     
23bdc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
23bdd 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62  gth of zSql in b
23bde 79 74 65 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73  ytes. */.  int s
23bdf 61 76 65 53 71 6c 46 6c 61 67 2c 20 20 20 20 20  aveSqlFlag,     
23be0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20       /* True to 
23be1 63 6f 70 79 20 53 51 4c 20 74 65 78 74 20 69 6e  copy SQL text in
23be2 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73  to the sqlite3_s
23be3 74 6d 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  tmt */.  sqlite3
23be4 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20  _stmt **ppStmt, 
23be5 20 20 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69     /* OUT: A poi
23be6 6e 74 65 72 20 74 6f 20 74 68 65 20 70 72 65 70  nter to the prep
23be7 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 2a  ared statement *
23be8 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
23be9 2a 70 7a 54 61 69 6c 20 20 20 20 20 20 20 2f 2a  *pzTail       /*
23bea 20 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70 61 72   OUT: End of par
23beb 73 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b  sed string */.){
23bec 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28  .  int rc;.  if(
23bed 20 21 73 71 6c 69 74 65 33 53 61 66 65 74 79 43   !sqlite3SafetyC
23bee 68 65 63 6b 4f 6b 28 64 62 29 20 29 7b 0a 20 20  heckOk(db) ){.  
23bef 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
23bf0 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73 71  MISUSE;.  }.  sq
23bf1 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
23bf2 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
23bf3 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
23bf4 72 41 6c 6c 28 64 62 29 3b 0a 20 20 72 63 20 3d  rAll(db);.  rc =
23bf5 20 73 71 6c 69 74 65 33 50 72 65 70 61 72 65 28   sqlite3Prepare(
23bf6 64 62 2c 20 7a 53 71 6c 2c 20 6e 42 79 74 65 73  db, zSql, nBytes
23bf7 2c 20 73 61 76 65 53 71 6c 46 6c 61 67 2c 20 70  , saveSqlFlag, p
23bf8 70 53 74 6d 74 2c 20 70 7a 54 61 69 6c 29 3b 0a  pStmt, pzTail);.
23bf9 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
23bfa 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 73 71  aveAll(db);.  sq
23bfb 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
23bfc 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
23bfd 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
23bfe 0a 2a 2a 20 52 65 72 75 6e 20 74 68 65 20 63 6f  .** Rerun the co
23bff 6d 70 69 6c 61 74 69 6f 6e 20 6f 66 20 61 20 73  mpilation of a s
23c00 74 61 74 65 6d 65 6e 74 20 61 66 74 65 72 20 61  tatement after a
23c01 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 2e 0a   schema change..
23c02 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  ** Return true i
23c03 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  f the statement 
23c04 77 61 73 20 72 65 63 6f 6d 70 69 6c 65 64 20 73  was recompiled s
23c05 75 63 63 65 73 73 66 75 6c 6c 79 2e 0a 2a 2a 20  uccessfully..** 
23c06 52 65 74 75 72 6e 20 66 61 6c 73 65 20 69 66 20  Return false if 
23c07 74 68 65 72 65 20 69 73 20 61 6e 20 65 72 72 6f  there is an erro
23c08 72 20 6f 66 20 73 6f 6d 65 20 6b 69 6e 64 2e 0a  r of some kind..
23c09 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
23c0a 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 70  E int sqlite3Rep
23c0b 72 65 70 61 72 65 28 56 64 62 65 20 2a 70 29 7b  repare(Vdbe *p){
23c0c 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c  .  int rc;.  sql
23c0d 69 74 65 33 5f 73 74 6d 74 20 2a 70 4e 65 77 3b  ite3_stmt *pNew;
23c0e 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
23c0f 53 71 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  Sql;.  sqlite3 *
23c10 64 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  db;..  assert( s
23c11 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
23c12 64 28 73 71 6c 69 74 65 33 56 64 62 65 44 62 28  d(sqlite3VdbeDb(
23c13 70 29 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  p)->mutex) );.  
23c14 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 73  zSql = sqlite3_s
23c15 71 6c 28 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ql((sqlite3_stmt
23c16 20 2a 29 70 29 3b 0a 20 20 61 73 73 65 72 74 28   *)p);.  assert(
23c17 20 7a 53 71 6c 21 3d 30 20 29 3b 20 20 2f 2a 20   zSql!=0 );  /* 
23c18 52 65 70 72 65 70 61 72 65 20 6f 6e 6c 79 20 63  Reprepare only c
23c19 61 6c 6c 65 64 20 66 6f 72 20 70 72 65 70 61 72  alled for prepar
23c1a 65 5f 76 32 28 29 20 73 74 61 74 65 6d 65 6e 74  e_v2() statement
23c1b 73 20 2a 2f 0a 20 20 64 62 20 3d 20 73 71 6c 69  s */.  db = sqli
23c1c 74 65 33 56 64 62 65 44 62 28 70 29 3b 0a 20 20  te3VdbeDb(p);.  
23c1d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
23c1e 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d  mutex_held(db->m
23c1f 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20  utex) );.  rc = 
23c20 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72  sqlite3LockAndPr
23c21 65 70 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20  epare(db, zSql, 
23c22 2d 31 2c 20 30 2c 20 26 70 4e 65 77 2c 20 30 29  -1, 0, &pNew, 0)
23c23 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20  ;.  if( rc ){.  
23c24 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
23c25 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20  _NOMEM ){.      
23c26 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
23c27 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 1;.    }.    
23c28 61 73 73 65 72 74 28 20 70 4e 65 77 3d 3d 30 20  assert( pNew==0 
23c29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
23c2a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
23c2b 73 65 72 74 28 20 70 4e 65 77 21 3d 30 20 29 3b  sert( pNew!=0 );
23c2c 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64  .  }.  sqlite3Vd
23c2d 62 65 53 77 61 70 28 28 56 64 62 65 2a 29 70 4e  beSwap((Vdbe*)pN
23c2e 65 77 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74 65  ew, p);.  sqlite
23c2f 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67  3TransferBinding
23c30 73 28 70 4e 65 77 2c 20 28 73 71 6c 69 74 65 33  s(pNew, (sqlite3
23c31 5f 73 74 6d 74 2a 29 70 29 3b 0a 20 20 73 71 6c  _stmt*)p);.  sql
23c32 69 74 65 33 56 64 62 65 52 65 73 65 74 53 74 65  ite3VdbeResetSte
23c33 70 52 65 73 75 6c 74 28 28 56 64 62 65 2a 29 70  pResult((Vdbe*)p
23c34 4e 65 77 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  New);.  sqlite3V
23c35 64 62 65 46 69 6e 61 6c 69 7a 65 28 28 56 64 62  dbeFinalize((Vdb
23c36 65 2a 29 70 4e 65 77 29 3b 0a 20 20 72 65 74 75  e*)pNew);.  retu
23c37 72 6e 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  rn 1;.}.../*.** 
23c38 54 77 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  Two versions of 
23c39 74 68 65 20 6f 66 66 69 63 69 61 6c 20 41 50 49  the official API
23c3a 2e 20 20 4c 65 67 61 63 79 20 61 6e 64 20 6e 65  .  Legacy and ne
23c3b 77 20 75 73 65 2e 20 20 49 6e 20 74 68 65 20 6c  w use.  In the l
23c3c 65 67 61 63 79 0a 2a 2a 20 76 65 72 73 69 6f 6e  egacy.** version
23c3d 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53  , the original S
23c3e 51 4c 20 74 65 78 74 20 69 73 20 6e 6f 74 20 73  QL text is not s
23c3f 61 76 65 64 20 69 6e 20 74 68 65 20 70 72 65 70  aved in the prep
23c40 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a  ared statement.*
23c41 2a 20 61 6e 64 20 73 6f 20 69 66 20 61 20 73 63  * and so if a sc
23c42 68 65 6d 61 20 63 68 61 6e 67 65 20 6f 63 63 75  hema change occu
23c43 72 73 2c 20 53 51 4c 49 54 45 5f 53 43 48 45 4d  rs, SQLITE_SCHEM
23c44 41 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 79  A is returned by
23c45 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70  .** sqlite3_step
23c46 28 29 2e 20 20 49 6e 20 74 68 65 20 6e 65 77 20  ().  In the new 
23c47 76 65 72 73 69 6f 6e 2c 20 74 68 65 20 6f 72 69  version, the ori
23c48 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 69  ginal SQL text i
23c49 73 20 72 65 74 61 69 6e 65 64 0a 2a 2a 20 61 6e  s retained.** an
23c4a 64 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  d the statement 
23c4b 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  is automatically
23c4c 20 72 65 63 6f 6d 70 69 6c 65 64 20 69 66 20 61   recompiled if a
23c4d 6e 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 0a  n schema change.
23c4e 2a 2a 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51  ** occurs..*/.SQ
23c4f 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
23c50 69 74 65 33 5f 70 72 65 70 61 72 65 28 0a 20 20  ite3_prepare(.  
23c51 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
23c52 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
23c53 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f  abase handle. */
23c54 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
23c55 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Sql,         /* 
23c56 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51  UTF-8 encoded SQ
23c57 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a  L statement. */.
23c58 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20    int nBytes,   
23c59 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
23c5a 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e  ength of zSql in
23c5b 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c   bytes. */.  sql
23c5c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74  ite3_stmt **ppSt
23c5d 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41  mt,    /* OUT: A
23c5e 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
23c5f 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
23c60 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  nt */.  const ch
23c61 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20  ar **pzTail     
23c62 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66    /* OUT: End of
23c63 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a   parsed string *
23c64 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  /.){.  int rc;. 
23c65 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63   rc = sqlite3Loc
23c66 6b 41 6e 64 50 72 65 70 61 72 65 28 64 62 2c 7a  kAndPrepare(db,z
23c67 53 71 6c 2c 6e 42 79 74 65 73 2c 30 2c 70 70 53  Sql,nBytes,0,ppS
23c68 74 6d 74 2c 70 7a 54 61 69 6c 29 3b 0a 20 20 61  tmt,pzTail);.  a
23c69 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
23c6a 45 5f 4f 4b 20 7c 7c 20 70 70 53 74 6d 74 3d 3d  E_OK || ppStmt==
23c6b 30 20 7c 7c 20 2a 70 70 53 74 6d 74 3d 3d 30 20  0 || *ppStmt==0 
23c6c 29 3b 20 20 2f 2a 20 56 45 52 49 46 59 3a 20 46  );  /* VERIFY: F
23c6d 31 33 30 32 31 20 2a 2f 0a 20 20 72 65 74 75 72  13021 */.  retur
23c6e 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  n rc;.}.SQLITE_A
23c6f 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70  PI int sqlite3_p
23c70 72 65 70 61 72 65 5f 76 32 28 0a 20 20 73 71 6c  repare_v2(.  sql
23c71 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
23c72 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
23c73 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20  se handle. */.  
23c74 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
23c75 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46  ,         /* UTF
23c76 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73  -8 encoded SQL s
23c77 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69  tatement. */.  i
23c78 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20  nt nBytes,      
23c79 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67           /* Leng
23c7a 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79  th of zSql in by
23c7b 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  tes. */.  sqlite
23c7c 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c  3_stmt **ppStmt,
23c7d 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f      /* OUT: A po
23c7e 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 72 65  inter to the pre
23c7f 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20  pared statement 
23c80 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
23c81 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 20 20 2f  **pzTail       /
23c82 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70 61  * OUT: End of pa
23c83 72 73 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29  rsed string */.)
23c84 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 72 63  {.  int rc;.  rc
23c85 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e   = sqlite3LockAn
23c86 64 50 72 65 70 61 72 65 28 64 62 2c 7a 53 71 6c  dPrepare(db,zSql
23c87 2c 6e 42 79 74 65 73 2c 31 2c 70 70 53 74 6d 74  ,nBytes,1,ppStmt
23c88 2c 70 7a 54 61 69 6c 29 3b 0a 20 20 61 73 73 65  ,pzTail);.  asse
23c89 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
23c8a 4b 20 7c 7c 20 70 70 53 74 6d 74 3d 3d 30 20 7c  K || ppStmt==0 |
23c8b 7c 20 2a 70 70 53 74 6d 74 3d 3d 30 20 29 3b 20  | *ppStmt==0 ); 
23c8c 20 2f 2a 20 56 45 52 49 46 59 3a 20 46 31 33 30   /* VERIFY: F130
23c8d 32 31 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72  21 */.  return r
23c8e 63 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53  c;.}...#ifndef S
23c8f 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
23c90 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 69 6c 65 20 74  ./*.** Compile t
23c91 68 65 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65  he UTF-16 encode
23c92 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  d SQL statement 
23c93 7a 53 71 6c 20 69 6e 74 6f 20 61 20 73 74 61 74  zSql into a stat
23c94 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2e 0a 2a 2f  ement handle..*/
23c95 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
23c96 74 65 33 50 72 65 70 61 72 65 31 36 28 0a 20 20  te3Prepare16(.  
23c97 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
23c98 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
23c99 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f  abase handle. */
23c9a 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a   .  const void *
23c9b 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a  zSql,         /*
23c9c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53   UTF-8 encoded S
23c9d 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f  QL statement. */
23c9e 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20  .  int nBytes,  
23c9f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
23ca0 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69  Length of zSql i
23ca1 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 69 6e  n bytes. */.  in
23ca2 74 20 73 61 76 65 53 71 6c 46 6c 61 67 2c 20 20  t saveSqlFlag,  
23ca3 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
23ca4 74 6f 20 73 61 76 65 20 53 51 4c 20 74 65 78 74  to save SQL text
23ca5 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65   into the sqlite
23ca6 33 5f 73 74 6d 74 20 2a 2f 0a 20 20 73 71 6c 69  3_stmt */.  sqli
23ca7 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d  te3_stmt **ppStm
23ca8 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41 20  t,    /* OUT: A 
23ca9 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70  pointer to the p
23caa 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
23cab 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69  t */.  const voi
23cac 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 20  d **pzTail      
23cad 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66 20   /* OUT: End of 
23cae 70 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a 2f  parsed string */
23caf 0a 29 7b 0a 20 20 2f 2a 20 54 68 69 73 20 66 75  .){.  /* This fu
23cb0 6e 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79  nction currently
23cb1 20 77 6f 72 6b 73 20 62 79 20 66 69 72 73 74 20   works by first 
23cb2 74 72 61 6e 73 66 6f 72 6d 69 6e 67 20 74 68 65  transforming the
23cb3 20 55 54 46 2d 31 36 0a 20 20 2a 2a 20 65 6e 63   UTF-16.  ** enc
23cb4 6f 64 65 64 20 73 74 72 69 6e 67 20 74 6f 20 55  oded string to U
23cb5 54 46 2d 38 2c 20 74 68 65 6e 20 69 6e 76 6f 6b  TF-8, then invok
23cb6 69 6e 67 20 73 71 6c 69 74 65 33 5f 70 72 65 70  ing sqlite3_prep
23cb7 61 72 65 28 29 2e 20 54 68 65 0a 20 20 2a 2a 20  are(). The.  ** 
23cb8 74 72 69 63 6b 79 20 62 69 74 20 69 73 20 66 69  tricky bit is fi
23cb9 67 75 72 69 6e 67 20 6f 75 74 20 74 68 65 20 70  guring out the p
23cba 6f 69 6e 74 65 72 20 74 6f 20 72 65 74 75 72 6e  ointer to return
23cbb 20 69 6e 20 2a 70 7a 54 61 69 6c 2e 0a 20 20 2a   in *pzTail..  *
23cbc 2f 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 38 3b  /.  char *zSql8;
23cbd 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
23cbe 54 61 69 6c 38 20 3d 20 30 3b 0a 20 20 69 6e 74  Tail8 = 0;.  int
23cbf 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
23cc0 0a 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33  ..  if( !sqlite3
23cc1 53 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 64 62  SafetyCheckOk(db
23cc2 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
23cc3 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
23cc4 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   }.  sqlite3_mut
23cc5 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
23cc6 65 78 29 3b 0a 20 20 7a 53 71 6c 38 20 3d 20 73  ex);.  zSql8 = s
23cc7 71 6c 69 74 65 33 55 74 66 31 36 74 6f 38 28 64  qlite3Utf16to8(d
23cc8 62 2c 20 7a 53 71 6c 2c 20 6e 42 79 74 65 73 29  b, zSql, nBytes)
23cc9 3b 0a 20 20 69 66 28 20 7a 53 71 6c 38 20 29 7b  ;.  if( zSql8 ){
23cca 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
23ccb 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72 65 28  3LockAndPrepare(
23ccc 64 62 2c 20 7a 53 71 6c 38 2c 20 2d 31 2c 20 73  db, zSql8, -1, s
23ccd 61 76 65 53 71 6c 46 6c 61 67 2c 20 70 70 53 74  aveSqlFlag, ppSt
23cce 6d 74 2c 20 26 7a 54 61 69 6c 38 29 3b 0a 20 20  mt, &zTail8);.  
23ccf 7d 0a 0a 20 20 69 66 28 20 7a 54 61 69 6c 38 20  }..  if( zTail8 
23cd0 26 26 20 70 7a 54 61 69 6c 20 29 7b 0a 20 20 20  && pzTail ){.   
23cd1 20 2f 2a 20 49 66 20 73 71 6c 69 74 65 33 5f 70   /* If sqlite3_p
23cd2 72 65 70 61 72 65 20 72 65 74 75 72 6e 73 20 61  repare returns a
23cd3 20 74 61 69 6c 20 70 6f 69 6e 74 65 72 2c 20 77   tail pointer, w
23cd4 65 20 63 61 6c 63 75 6c 61 74 65 20 74 68 65 0a  e calculate the.
23cd5 20 20 20 20 2a 2a 20 65 71 75 69 76 61 6c 65 6e      ** equivalen
23cd6 74 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 74  t pointer into t
23cd7 68 65 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67  he UTF-16 string
23cd8 20 62 79 20 63 6f 75 6e 74 69 6e 67 20 74 68 65   by counting the
23cd9 20 75 6e 69 63 6f 64 65 0a 20 20 20 20 2a 2a 20   unicode.    ** 
23cda 63 68 61 72 61 63 74 65 72 73 20 62 65 74 77 65  characters betwe
23cdb 65 6e 20 7a 53 71 6c 38 20 61 6e 64 20 7a 54 61  en zSql8 and zTa
23cdc 69 6c 38 2c 20 61 6e 64 20 74 68 65 6e 20 72 65  il8, and then re
23cdd 74 75 72 6e 69 6e 67 20 61 20 70 6f 69 6e 74 65  turning a pointe
23cde 72 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 61 6d  r.    ** the sam
23cdf 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72  e number of char
23ce0 61 63 74 65 72 73 20 69 6e 74 6f 20 74 68 65 20  acters into the 
23ce1 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2e 0a 20  UTF-16 string.. 
23ce2 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63 68     */.    int ch
23ce3 61 72 73 5f 70 61 72 73 65 64 20 3d 20 73 71 6c  ars_parsed = sql
23ce4 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28  ite3Utf8CharLen(
23ce5 7a 53 71 6c 38 2c 20 7a 54 61 69 6c 38 2d 7a 53  zSql8, zTail8-zS
23ce6 71 6c 38 29 3b 0a 20 20 20 20 2a 70 7a 54 61 69  ql8);.    *pzTai
23ce7 6c 20 3d 20 28 75 38 20 2a 29 7a 53 71 6c 20 2b  l = (u8 *)zSql +
23ce8 20 73 71 6c 69 74 65 33 55 74 66 31 36 42 79 74   sqlite3Utf16Byt
23ce9 65 4c 65 6e 28 7a 53 71 6c 2c 20 63 68 61 72 73  eLen(zSql, chars
23cea 5f 70 61 72 73 65 64 29 3b 0a 20 20 7d 0a 20 20  _parsed);.  }.  
23ceb 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
23cec 2c 20 7a 53 71 6c 38 29 3b 20 0a 20 20 72 63 20  , zSql8); .  rc 
23ced 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
23cee 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69  (db, rc);.  sqli
23cef 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
23cf0 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
23cf1 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
23cf2 2a 20 54 77 6f 20 76 65 72 73 69 6f 6e 73 20 6f  * Two versions o
23cf3 66 20 74 68 65 20 6f 66 66 69 63 69 61 6c 20 41  f the official A
23cf4 50 49 2e 20 20 4c 65 67 61 63 79 20 61 6e 64 20  PI.  Legacy and 
23cf5 6e 65 77 20 75 73 65 2e 20 20 49 6e 20 74 68 65  new use.  In the
23cf6 20 6c 65 67 61 63 79 0a 2a 2a 20 76 65 72 73 69   legacy.** versi
23cf7 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  on, the original
23cf8 20 53 51 4c 20 74 65 78 74 20 69 73 20 6e 6f 74   SQL text is not
23cf9 20 73 61 76 65 64 20 69 6e 20 74 68 65 20 70 72   saved in the pr
23cfa 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
23cfb 0a 2a 2a 20 61 6e 64 20 73 6f 20 69 66 20 61 20  .** and so if a 
23cfc 73 63 68 65 6d 61 20 63 68 61 6e 67 65 20 6f 63  schema change oc
23cfd 63 75 72 73 2c 20 53 51 4c 49 54 45 5f 53 43 48  curs, SQLITE_SCH
23cfe 45 4d 41 20 69 73 20 72 65 74 75 72 6e 65 64 20  EMA is returned 
23cff 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 74  by.** sqlite3_st
23d00 65 70 28 29 2e 20 20 49 6e 20 74 68 65 20 6e 65  ep().  In the ne
23d01 77 20 76 65 72 73 69 6f 6e 2c 20 74 68 65 20 6f  w version, the o
23d02 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74  riginal SQL text
23d03 20 69 73 20 72 65 74 61 69 6e 65 64 0a 2a 2a 20   is retained.** 
23d04 61 6e 64 20 74 68 65 20 73 74 61 74 65 6d 65 6e  and the statemen
23d05 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c  t is automatical
23d06 6c 79 20 72 65 63 6f 6d 70 69 6c 65 64 20 69 66  ly recompiled if
23d07 20 61 6e 20 73 63 68 65 6d 61 20 63 68 61 6e 67   an schema chang
23d08 65 0a 2a 2a 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a  e.** occurs..*/.
23d09 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
23d0a 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
23d0b 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
23d0c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
23d0d 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
23d0e 2e 20 2a 2f 20 0a 20 20 63 6f 6e 73 74 20 76 6f  . */ .  const vo
23d0f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20  id *zSql,       
23d10 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64    /* UTF-8 encod
23d11 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ed SQL statement
23d12 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  . */.  int nByte
23d13 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
23d14 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53   /* Length of zS
23d15 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a  ql in bytes. */.
23d16 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
23d17 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f  *ppStmt,    /* O
23d18 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  UT: A pointer to
23d19 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74   the prepared st
23d1a 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e  atement */.  con
23d1b 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c  st void **pzTail
23d1c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45         /* OUT: E
23d1d 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73 74 72  nd of parsed str
23d1e 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ing */.){.  int 
23d1f 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  rc;.  rc = sqlit
23d20 65 33 50 72 65 70 61 72 65 31 36 28 64 62 2c 7a  e3Prepare16(db,z
23d21 53 71 6c 2c 6e 42 79 74 65 73 2c 30 2c 70 70 53  Sql,nBytes,0,ppS
23d22 74 6d 74 2c 70 7a 54 61 69 6c 29 3b 0a 20 20 61  tmt,pzTail);.  a
23d23 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
23d24 45 5f 4f 4b 20 7c 7c 20 70 70 53 74 6d 74 3d 3d  E_OK || ppStmt==
23d25 30 20 7c 7c 20 2a 70 70 53 74 6d 74 3d 3d 30 20  0 || *ppStmt==0 
23d26 29 3b 20 20 2f 2a 20 56 45 52 49 46 59 3a 20 46  );  /* VERIFY: F
23d27 31 33 30 32 31 20 2a 2f 0a 20 20 72 65 74 75 72  13021 */.  retur
23d28 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  n rc;.}.SQLITE_A
23d29 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70  PI int sqlite3_p
23d2a 72 65 70 61 72 65 31 36 5f 76 32 28 0a 20 20 73  repare16_v2(.  s
23d2b 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
23d2c 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
23d2d 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 20  base handle. */ 
23d2e 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
23d2f 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Sql,         /* 
23d30 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51  UTF-8 encoded SQ
23d31 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a  L statement. */.
23d32 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20    int nBytes,   
23d33 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
23d34 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e  ength of zSql in
23d35 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c   bytes. */.  sql
23d36 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74  ite3_stmt **ppSt
23d37 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41  mt,    /* OUT: A
23d38 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
23d39 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
23d3a 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  nt */.  const vo
23d3b 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20  id **pzTail     
23d3c 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66    /* OUT: End of
23d3d 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a   parsed string *
23d3e 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  /.){.  int rc;. 
23d3f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 72 65   rc = sqlite3Pre
23d40 70 61 72 65 31 36 28 64 62 2c 7a 53 71 6c 2c 6e  pare16(db,zSql,n
23d41 42 79 74 65 73 2c 31 2c 70 70 53 74 6d 74 2c 70  Bytes,1,ppStmt,p
23d42 7a 54 61 69 6c 29 3b 0a 20 20 61 73 73 65 72 74  zTail);.  assert
23d43 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
23d44 7c 7c 20 70 70 53 74 6d 74 3d 3d 30 20 7c 7c 20  || ppStmt==0 || 
23d45 2a 70 70 53 74 6d 74 3d 3d 30 20 29 3b 20 20 2f  *ppStmt==0 );  /
23d46 2a 20 56 45 52 49 46 59 3a 20 46 31 33 30 32 31  * VERIFY: F13021
23d47 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   */.  return rc;
23d48 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
23d49 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
23d4a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
23d4b 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 72 65 70 61  *** End of prepa
23d4c 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  re.c ***********
23d4d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23d4e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23d4f 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
23d50 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73  *** Begin file s
23d51 65 6c 65 63 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  elect.c ********
23d52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23d53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23d54 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
23d55 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
23d56 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
23d57 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
23d58 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
23d59 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
23d5a 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
23d5b 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
23d5c 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
23d5d 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
23d5e 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
23d5f 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
23d60 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
23d61 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
23d62 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
23d63 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
23d64 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
23d65 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
23d66 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
23d67 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
23d68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23d69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23d6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23d6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
23d6c 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
23d6d 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65 73   C code routines
23d6e 20 74 68 61 74 20 61 72 65 20 63 61 6c 6c 65 64   that are called
23d6f 20 62 79 20 74 68 65 20 70 61 72 73 65 72 0a 2a   by the parser.*
23d70 2a 20 74 6f 20 68 61 6e 64 6c 65 20 53 45 4c 45  * to handle SELE
23d71 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e  CT statements in
23d72 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24   SQLite..**.** $
23d73 49 64 3a 20 73 65 6c 65 63 74 2e 63 2c 76 20 31  Id: select.c,v 1
23d74 2e 34 38 32 20 32 30 30 38 2f 31 30 2f 33 31 20  .482 2008/10/31 
23d75 31 30 3a 35 33 3a 32 33 20 64 61 6e 69 65 6c 6b  10:53:23 danielk
23d76 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a  1977 Exp $.*/...
23d77 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c  /*.** Delete all
23d78 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
23d79 61 20 53 65 6c 65 63 74 20 73 74 72 75 63 74 75  a Select structu
23d7a 72 65 20 62 75 74 20 64 6f 20 6e 6f 74 20 64 65  re but do not de
23d7b 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 74 68 65 20  allocate.** the 
23d7c 73 65 6c 65 63 74 20 73 74 72 75 63 74 75 72 65  select structure
23d7d 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a 73 74 61 74   itself..*/.stat
23d7e 69 63 20 76 6f 69 64 20 63 6c 65 61 72 53 65 6c  ic void clearSel
23d7f 65 63 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ect(sqlite3 *db,
23d80 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 73   Select *p){.  s
23d81 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
23d82 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 45 4c 69  lete(db, p->pELi
23d83 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 72  st);.  sqlite3Sr
23d84 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  cListDelete(db, 
23d85 70 2d 3e 70 53 72 63 29 3b 0a 20 20 73 71 6c 69  p->pSrc);.  sqli
23d86 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
23d87 2c 20 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20  , p->pWhere);.  
23d88 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
23d89 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 47 72  elete(db, p->pGr
23d8a 6f 75 70 42 79 29 3b 0a 20 20 73 71 6c 69 74 65  oupBy);.  sqlite
23d8b 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
23d8c 70 2d 3e 70 48 61 76 69 6e 67 29 3b 0a 20 20 73  p->pHaving);.  s
23d8d 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
23d8e 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4f 72 64  lete(db, p->pOrd
23d8f 65 72 42 79 29 3b 0a 20 20 73 71 6c 69 74 65 33  erBy);.  sqlite3
23d90 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
23d91 20 70 2d 3e 70 50 72 69 6f 72 29 3b 0a 20 20 73   p->pPrior);.  s
23d92 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
23d93 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 3b  (db, p->pLimit);
23d94 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
23d95 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4f 66 66  lete(db, p->pOff
23d96 73 65 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  set);.}../*.** I
23d97 6e 69 74 69 61 6c 69 7a 65 20 61 20 53 65 6c 65  nitialize a Sele
23d98 63 74 44 65 73 74 20 73 74 72 75 63 74 75 72 65  ctDest structure
23d99 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
23d9a 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
23d9b 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 53  SelectDestInit(S
23d9c 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73 74  electDest *pDest
23d9d 2c 20 69 6e 74 20 65 44 65 73 74 2c 20 69 6e 74  , int eDest, int
23d9e 20 69 50 61 72 6d 29 7b 0a 20 20 70 44 65 73 74   iParm){.  pDest
23d9f 2d 3e 65 44 65 73 74 20 3d 20 65 44 65 73 74 3b  ->eDest = eDest;
23da0 0a 20 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 20  .  pDest->iParm 
23da1 3d 20 69 50 61 72 6d 3b 0a 20 20 70 44 65 73 74  = iParm;.  pDest
23da2 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 30 3b 0a  ->affinity = 0;.
23da3 20 20 70 44 65 73 74 2d 3e 69 4d 65 6d 20 3d 20    pDest->iMem = 
23da4 30 3b 0a 20 20 70 44 65 73 74 2d 3e 6e 4d 65 6d  0;.  pDest->nMem
23da5 20 3d 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20   = 0;.}.../*.** 
23da6 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 53  Allocate a new S
23da7 65 6c 65 63 74 20 73 74 72 75 63 74 75 72 65 20  elect structure 
23da8 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69  and return a poi
23da9 6e 74 65 72 20 74 6f 20 74 68 61 74 0a 2a 2a 20  nter to that.** 
23daa 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51  structure..*/.SQ
23dab 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c  LITE_PRIVATE Sel
23dac 65 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65  ect *sqlite3Sele
23dad 63 74 4e 65 77 28 0a 20 20 50 61 72 73 65 20 2a  ctNew(.  Parse *
23dae 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f  pParse,        /
23daf 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
23db0 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  t */.  ExprList 
23db1 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20  *pEList,     /* 
23db2 77 68 69 63 68 20 63 6f 6c 75 6d 6e 73 20 74 6f  which columns to
23db3 20 69 6e 63 6c 75 64 65 20 69 6e 20 74 68 65 20   include in the 
23db4 72 65 73 75 6c 74 20 2a 2f 0a 20 20 53 72 63 4c  result */.  SrcL
23db5 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20  ist *pSrc,      
23db6 20 20 2f 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c    /* the FROM cl
23db7 61 75 73 65 20 2d 2d 20 77 68 69 63 68 20 74 61  ause -- which ta
23db8 62 6c 65 73 20 74 6f 20 73 63 61 6e 20 2a 2f 0a  bles to scan */.
23db9 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c 20    Expr *pWhere, 
23dba 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 57          /* the W
23dbb 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
23dbc 20 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f 75   ExprList *pGrou
23dbd 70 42 79 2c 20 20 20 2f 2a 20 74 68 65 20 47 52  pBy,   /* the GR
23dbe 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  OUP BY clause */
23dbf 0a 20 20 45 78 70 72 20 2a 70 48 61 76 69 6e 67  .  Expr *pHaving
23dc0 2c 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20  ,        /* the 
23dc1 48 41 56 49 4e 47 20 63 6c 61 75 73 65 20 2a 2f  HAVING clause */
23dc2 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72  .  ExprList *pOr
23dc3 64 65 72 42 79 2c 20 20 20 2f 2a 20 74 68 65 20  derBy,   /* the 
23dc4 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
23dc5 2a 2f 0a 20 20 69 6e 74 20 69 73 44 69 73 74 69  */.  int isDisti
23dc6 6e 63 74 2c 20 20 20 20 20 20 20 2f 2a 20 74 72  nct,       /* tr
23dc7 75 65 20 69 66 20 74 68 65 20 44 49 53 54 49 4e  ue if the DISTIN
23dc8 43 54 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72  CT keyword is pr
23dc9 65 73 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20  esent */.  Expr 
23dca 2a 70 4c 69 6d 69 74 2c 20 20 20 20 20 20 20 20  *pLimit,        
23dcb 20 2f 2a 20 4c 49 4d 49 54 20 76 61 6c 75 65 2e   /* LIMIT value.
23dcc 20 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74    NULL means not
23dcd 20 75 73 65 64 20 2a 2f 0a 20 20 45 78 70 72 20   used */.  Expr 
23dce 2a 70 4f 66 66 73 65 74 20 20 20 20 20 20 20 20  *pOffset        
23dcf 20 2f 2a 20 4f 46 46 53 45 54 20 76 61 6c 75 65   /* OFFSET value
23dd0 2e 20 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f  .  NULL means no
23dd1 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20   offset */.){.  
23dd2 53 65 6c 65 63 74 20 2a 70 4e 65 77 3b 0a 20 20  Select *pNew;.  
23dd3 53 65 6c 65 63 74 20 73 74 61 6e 64 69 6e 3b 0a  Select standin;.
23dd4 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
23dd5 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 4e  pParse->db;.  pN
23dd6 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ew = sqlite3DbMa
23dd7 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
23dd8 65 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20  eof(*pNew) );.  
23dd9 61 73 73 65 72 74 28 20 21 70 4f 66 66 73 65 74  assert( !pOffset
23dda 20 7c 7c 20 70 4c 69 6d 69 74 20 29 3b 20 20 20   || pLimit );   
23ddb 2f 2a 20 43 61 6e 27 74 20 68 61 76 65 20 4f 46  /* Can't have OF
23ddc 46 53 45 54 20 77 69 74 68 6f 75 74 20 4c 49 4d  FSET without LIM
23ddd 49 54 2e 20 2a 2f 0a 20 20 69 66 28 20 70 4e 65  IT. */.  if( pNe
23dde 77 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e 65 77  w==0 ){.    pNew
23ddf 20 3d 20 26 73 74 61 6e 64 69 6e 3b 0a 20 20 20   = &standin;.   
23de0 20 6d 65 6d 73 65 74 28 70 4e 65 77 2c 20 30 2c   memset(pNew, 0,
23de1 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b   sizeof(*pNew));
23de2 0a 20 20 7d 0a 20 20 69 66 28 20 70 45 4c 69 73  .  }.  if( pELis
23de3 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 45 4c 69  t==0 ){.    pELi
23de4 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
23de5 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
23de6 65 2c 20 30 2c 20 73 71 6c 69 74 65 33 45 78 70  e, 0, sqlite3Exp
23de7 72 28 64 62 2c 54 4b 5f 41 4c 4c 2c 30 2c 30 2c  r(db,TK_ALL,0,0,
23de8 30 29 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70 4e  0), 0);.  }.  pN
23de9 65 77 2d 3e 70 45 4c 69 73 74 20 3d 20 70 45 4c  ew->pEList = pEL
23dea 69 73 74 3b 0a 20 20 70 4e 65 77 2d 3e 70 53 72  ist;.  pNew->pSr
23deb 63 20 3d 20 70 53 72 63 3b 0a 20 20 70 4e 65 77  c = pSrc;.  pNew
23dec 2d 3e 70 57 68 65 72 65 20 3d 20 70 57 68 65 72  ->pWhere = pWher
23ded 65 3b 0a 20 20 70 4e 65 77 2d 3e 70 47 72 6f 75  e;.  pNew->pGrou
23dee 70 42 79 20 3d 20 70 47 72 6f 75 70 42 79 3b 0a  pBy = pGroupBy;.
23def 20 20 70 4e 65 77 2d 3e 70 48 61 76 69 6e 67 20    pNew->pHaving 
23df0 3d 20 70 48 61 76 69 6e 67 3b 0a 20 20 70 4e 65  = pHaving;.  pNe
23df1 77 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 70 4f  w->pOrderBy = pO
23df2 72 64 65 72 42 79 3b 0a 20 20 70 4e 65 77 2d 3e  rderBy;.  pNew->
23df3 73 65 6c 46 6c 61 67 73 20 3d 20 69 73 44 69 73  selFlags = isDis
23df4 74 69 6e 63 74 20 3f 20 53 46 5f 44 69 73 74 69  tinct ? SF_Disti
23df5 6e 63 74 20 3a 20 30 3b 0a 20 20 70 4e 65 77 2d  nct : 0;.  pNew-
23df6 3e 6f 70 20 3d 20 54 4b 5f 53 45 4c 45 43 54 3b  >op = TK_SELECT;
23df7 0a 20 20 70 4e 65 77 2d 3e 70 4c 69 6d 69 74 20  .  pNew->pLimit 
23df8 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 70 4e 65 77  = pLimit;.  pNew
23df9 2d 3e 70 4f 66 66 73 65 74 20 3d 20 70 4f 66 66  ->pOffset = pOff
23dfa 73 65 74 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64  set;.  pNew->add
23dfb 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20 2d  rOpenEphm[0] = -
23dfc 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f  1;.  pNew->addrO
23dfd 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d 31 3b  penEphm[1] = -1;
23dfe 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65  .  pNew->addrOpe
23dff 6e 45 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a 20  nEphm[2] = -1;. 
23e00 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
23e01 61 69 6c 65 64 20 29 20 7b 0a 20 20 20 20 63 6c  ailed ) {.    cl
23e02 65 61 72 53 65 6c 65 63 74 28 64 62 2c 20 70 4e  earSelect(db, pN
23e03 65 77 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 65  ew);.    if( pNe
23e04 77 21 3d 26 73 74 61 6e 64 69 6e 20 29 20 73 71  w!=&standin ) sq
23e05 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
23e06 70 4e 65 77 29 3b 0a 20 20 20 20 70 4e 65 77 20  pNew);.    pNew 
23e07 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 0;.  }.  retur
23e08 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pNew;.}../*.**
23e09 20 44 65 6c 65 74 65 20 74 68 65 20 67 69 76 65   Delete the give
23e0a 6e 20 53 65 6c 65 63 74 20 73 74 72 75 63 74 75  n Select structu
23e0b 72 65 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69 74  re and all of it
23e0c 73 20 73 75 62 73 74 72 75 63 74 75 72 65 73 2e  s substructures.
23e0d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
23e0e 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
23e0f 65 6c 65 63 74 44 65 6c 65 74 65 28 73 71 6c 69  electDelete(sqli
23e10 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74 20  te3 *db, Select 
23e11 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a  *p){.  if( p ){.
23e12 20 20 20 20 63 6c 65 61 72 53 65 6c 65 63 74 28      clearSelect(
23e13 64 62 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c 69  db, p);.    sqli
23e14 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29  te3DbFree(db, p)
23e15 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  ;.  }.}../*.** G
23e16 69 76 65 6e 20 31 20 74 6f 20 33 20 69 64 65 6e  iven 1 to 3 iden
23e17 74 69 66 69 65 72 73 20 70 72 65 63 65 65 64 69  tifiers preceedi
23e18 6e 67 20 74 68 65 20 4a 4f 49 4e 20 6b 65 79 77  ng the JOIN keyw
23e19 6f 72 64 2c 20 64 65 74 65 72 6d 69 6e 65 20 74  ord, determine t
23e1a 68 65 0a 2a 2a 20 74 79 70 65 20 6f 66 20 6a 6f  he.** type of jo
23e1b 69 6e 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 69  in.  Return an i
23e1c 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20  nteger constant 
23e1d 74 68 61 74 20 65 78 70 72 65 73 73 65 73 20 74  that expresses t
23e1e 68 61 74 20 74 79 70 65 0a 2a 2a 20 69 6e 20 74  hat type.** in t
23e1f 65 72 6d 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c  erms of the foll
23e20 6f 77 69 6e 67 20 62 69 74 20 76 61 6c 75 65 73  owing bit values
23e21 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4a 54 5f 49  :.**.**     JT_I
23e22 4e 4e 45 52 0a 2a 2a 20 20 20 20 20 4a 54 5f 43  NNER.**     JT_C
23e23 52 4f 53 53 0a 2a 2a 20 20 20 20 20 4a 54 5f 4f  ROSS.**     JT_O
23e24 55 54 45 52 0a 2a 2a 20 20 20 20 20 4a 54 5f 4e  UTER.**     JT_N
23e25 41 54 55 52 41 4c 0a 2a 2a 20 20 20 20 20 4a 54  ATURAL.**     JT
23e26 5f 4c 45 46 54 0a 2a 2a 20 20 20 20 20 4a 54 5f  _LEFT.**     JT_
23e27 52 49 47 48 54 0a 2a 2a 0a 2a 2a 20 41 20 66 75  RIGHT.**.** A fu
23e28 6c 6c 20 6f 75 74 65 72 20 6a 6f 69 6e 20 69 73  ll outer join is
23e29 20 74 68 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e   the combination
23e2a 20 6f 66 20 4a 54 5f 4c 45 46 54 20 61 6e 64 20   of JT_LEFT and 
23e2b 4a 54 5f 52 49 47 48 54 2e 0a 2a 2a 0a 2a 2a 20  JT_RIGHT..**.** 
23e2c 49 66 20 61 6e 20 69 6c 6c 65 67 61 6c 20 6f 72  If an illegal or
23e2d 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6a 6f 69   unsupported joi
23e2e 6e 20 74 79 70 65 20 69 73 20 73 65 65 6e 2c 20  n type is seen, 
23e2f 74 68 65 6e 20 73 74 69 6c 6c 20 72 65 74 75 72  then still retur
23e30 6e 0a 2a 2a 20 61 20 6a 6f 69 6e 20 74 79 70 65  n.** a join type
23e31 2c 20 62 75 74 20 70 75 74 20 61 6e 20 65 72 72  , but put an err
23e32 6f 72 20 69 6e 20 74 68 65 20 70 50 61 72 73 65  or in the pParse
23e33 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53   structure..*/.S
23e34 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
23e35 74 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70  t sqlite3JoinTyp
23e36 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
23e37 20 54 6f 6b 65 6e 20 2a 70 41 2c 20 54 6f 6b 65   Token *pA, Toke
23e38 6e 20 2a 70 42 2c 20 54 6f 6b 65 6e 20 2a 70 43  n *pB, Token *pC
23e39 29 7b 0a 20 20 69 6e 74 20 6a 6f 69 6e 74 79 70  ){.  int jointyp
23e3a 65 20 3d 20 30 3b 0a 20 20 54 6f 6b 65 6e 20 2a  e = 0;.  Token *
23e3b 61 70 41 6c 6c 5b 33 5d 3b 0a 20 20 54 6f 6b 65  apAll[3];.  Toke
23e3c 6e 20 2a 70 3b 0a 20 20 73 74 61 74 69 63 20 63  n *p;.  static c
23e3d 6f 6e 73 74 20 73 74 72 75 63 74 20 7b 0a 20 20  onst struct {.  
23e3e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 4b 65    const char zKe
23e3f 79 77 6f 72 64 5b 38 5d 3b 0a 20 20 20 20 75 38  yword[8];.    u8
23e40 20 6e 43 68 61 72 3b 0a 20 20 20 20 75 38 20 63   nChar;.    u8 c
23e41 6f 64 65 3b 0a 20 20 7d 20 6b 65 79 77 6f 72 64  ode;.  } keyword
23e42 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22 6e  s[] = {.    { "n
23e43 61 74 75 72 61 6c 22 2c 20 37 2c 20 4a 54 5f 4e  atural", 7, JT_N
23e44 41 54 55 52 41 4c 20 7d 2c 0a 20 20 20 20 7b 20  ATURAL },.    { 
23e45 22 6c 65 66 74 22 2c 20 20 20 20 34 2c 20 4a 54  "left",    4, JT
23e46 5f 4c 45 46 54 7c 4a 54 5f 4f 55 54 45 52 20 7d  _LEFT|JT_OUTER }
23e47 2c 0a 20 20 20 20 7b 20 22 72 69 67 68 74 22 2c  ,.    { "right",
23e48 20 20 20 35 2c 20 4a 54 5f 52 49 47 48 54 7c 4a     5, JT_RIGHT|J
23e49 54 5f 4f 55 54 45 52 20 7d 2c 0a 20 20 20 20 7b  T_OUTER },.    {
23e4a 20 22 66 75 6c 6c 22 2c 20 20 20 20 34 2c 20 4a   "full",    4, J
23e4b 54 5f 4c 45 46 54 7c 4a 54 5f 52 49 47 48 54 7c  T_LEFT|JT_RIGHT|
23e4c 4a 54 5f 4f 55 54 45 52 20 7d 2c 0a 20 20 20 20  JT_OUTER },.    
23e4d 7b 20 22 6f 75 74 65 72 22 2c 20 20 20 35 2c 20  { "outer",   5, 
23e4e 4a 54 5f 4f 55 54 45 52 20 7d 2c 0a 20 20 20 20  JT_OUTER },.    
23e4f 7b 20 22 69 6e 6e 65 72 22 2c 20 20 20 35 2c 20  { "inner",   5, 
23e50 4a 54 5f 49 4e 4e 45 52 20 7d 2c 0a 20 20 20 20  JT_INNER },.    
23e51 7b 20 22 63 72 6f 73 73 22 2c 20 20 20 35 2c 20  { "cross",   5, 
23e52 4a 54 5f 49 4e 4e 45 52 7c 4a 54 5f 43 52 4f 53  JT_INNER|JT_CROS
23e53 53 20 7d 2c 0a 20 20 7d 3b 0a 20 20 69 6e 74 20  S },.  };.  int 
23e54 69 2c 20 6a 3b 0a 20 20 61 70 41 6c 6c 5b 30 5d  i, j;.  apAll[0]
23e55 20 3d 20 70 41 3b 0a 20 20 61 70 41 6c 6c 5b 31   = pA;.  apAll[1
23e56 5d 20 3d 20 70 42 3b 0a 20 20 61 70 41 6c 6c 5b  ] = pB;.  apAll[
23e57 32 5d 20 3d 20 70 43 3b 0a 20 20 66 6f 72 28 69  2] = pC;.  for(i
23e58 3d 30 3b 20 69 3c 33 20 26 26 20 61 70 41 6c 6c  =0; i<3 && apAll
23e59 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70  [i]; i++){.    p
23e5a 20 3d 20 61 70 41 6c 6c 5b 69 5d 3b 0a 20 20 20   = apAll[i];.   
23e5b 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 73 69 7a 65   for(j=0; j<size
23e5c 6f 66 28 6b 65 79 77 6f 72 64 73 29 2f 73 69 7a  of(keywords)/siz
23e5d 65 6f 66 28 6b 65 79 77 6f 72 64 73 5b 30 5d 29  eof(keywords[0])
23e5e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; j++){.      if
23e5f 28 20 70 2d 3e 6e 3d 3d 6b 65 79 77 6f 72 64 73  ( p->n==keywords
23e60 5b 6a 5d 2e 6e 43 68 61 72 20 0a 20 20 20 20 20  [j].nChar .     
23e61 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 53       && sqlite3S
23e62 74 72 4e 49 43 6d 70 28 28 63 68 61 72 2a 29 70  trNICmp((char*)p
23e63 2d 3e 7a 2c 20 6b 65 79 77 6f 72 64 73 5b 6a 5d  ->z, keywords[j]
23e64 2e 7a 4b 65 79 77 6f 72 64 2c 20 70 2d 3e 6e 29  .zKeyword, p->n)
23e65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6a  ==0 ){.        j
23e66 6f 69 6e 74 79 70 65 20 7c 3d 20 6b 65 79 77 6f  ointype |= keywo
23e67 72 64 73 5b 6a 5d 2e 63 6f 64 65 3b 0a 20 20 20  rds[j].code;.   
23e68 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
23e69 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
23e6a 28 20 6a 3e 3d 73 69 7a 65 6f 66 28 6b 65 79 77  ( j>=sizeof(keyw
23e6b 6f 72 64 73 29 2f 73 69 7a 65 6f 66 28 6b 65 79  ords)/sizeof(key
23e6c 77 6f 72 64 73 5b 30 5d 29 20 29 7b 0a 20 20 20  words[0]) ){.   
23e6d 20 20 20 6a 6f 69 6e 74 79 70 65 20 7c 3d 20 4a     jointype |= J
23e6e 54 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 62  T_ERROR;.      b
23e6f 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
23e70 20 20 69 66 28 0a 20 20 20 20 20 28 6a 6f 69 6e    if(.     (join
23e71 74 79 70 65 20 26 20 28 4a 54 5f 49 4e 4e 45 52  type & (JT_INNER
23e72 7c 4a 54 5f 4f 55 54 45 52 29 29 3d 3d 28 4a 54  |JT_OUTER))==(JT
23e73 5f 49 4e 4e 45 52 7c 4a 54 5f 4f 55 54 45 52 29  _INNER|JT_OUTER)
23e74 20 7c 7c 0a 20 20 20 20 20 28 6a 6f 69 6e 74 79   ||.     (jointy
23e75 70 65 20 26 20 4a 54 5f 45 52 52 4f 52 29 21 3d  pe & JT_ERROR)!=
23e76 30 0a 20 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  0.  ){.    const
23e77 20 63 68 61 72 20 2a 7a 53 70 20 3d 20 22 20 22   char *zSp = " "
23e78 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42  ;.    assert( pB
23e79 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70  !=0 );.    if( p
23e7a 43 3d 3d 30 20 29 7b 20 7a 53 70 2b 2b 3b 20 7d  C==0 ){ zSp++; }
23e7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
23e7c 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75 6e  rMsg(pParse, "un
23e7d 6b 6e 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70 6f  known or unsuppo
23e7e 72 74 65 64 20 6a 6f 69 6e 20 74 79 70 65 3a 20  rted join type: 
23e7f 22 0a 20 20 20 20 20 20 20 22 25 54 20 25 54 25  ".       "%T %T%
23e80 73 25 54 22 2c 20 70 41 2c 20 70 42 2c 20 7a 53  s%T", pA, pB, zS
23e81 70 2c 20 70 43 29 3b 0a 20 20 20 20 6a 6f 69 6e  p, pC);.    join
23e82 74 79 70 65 20 3d 20 4a 54 5f 49 4e 4e 45 52 3b  type = JT_INNER;
23e83 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6a 6f 69  .  }else if( joi
23e84 6e 74 79 70 65 20 26 20 4a 54 5f 52 49 47 48 54  ntype & JT_RIGHT
23e85 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
23e86 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
23e87 0a 20 20 20 20 20 20 22 52 49 47 48 54 20 61 6e  .      "RIGHT an
23e88 64 20 46 55 4c 4c 20 4f 55 54 45 52 20 4a 4f 49  d FULL OUTER JOI
23e89 4e 73 20 61 72 65 20 6e 6f 74 20 63 75 72 72 65  Ns are not curre
23e8a 6e 74 6c 79 20 73 75 70 70 6f 72 74 65 64 22 29  ntly supported")
23e8b 3b 0a 20 20 20 20 6a 6f 69 6e 74 79 70 65 20 3d  ;.    jointype =
23e8c 20 4a 54 5f 49 4e 4e 45 52 3b 0a 20 20 7d 0a 20   JT_INNER;.  }. 
23e8d 20 72 65 74 75 72 6e 20 6a 6f 69 6e 74 79 70 65   return jointype
23e8e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
23e8f 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 61  n the index of a
23e90 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 74 61 62   column in a tab
23e91 6c 65 2e 20 20 52 65 74 75 72 6e 20 2d 31 20 69  le.  Return -1 i
23e92 66 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20  f the column.** 
23e93 69 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 65 64  is not contained
23e94 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a   in the table..*
23e95 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6c  /.static int col
23e96 75 6d 6e 49 6e 64 65 78 28 54 61 62 6c 65 20 2a  umnIndex(Table *
23e97 70 54 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72  pTab, const char
23e98 20 2a 7a 43 6f 6c 29 7b 0a 20 20 69 6e 74 20 69   *zCol){.  int i
23e99 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
23e9a 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  Tab->nCol; i++){
23e9b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
23e9c 53 74 72 49 43 6d 70 28 70 54 61 62 2d 3e 61 43  StrICmp(pTab->aC
23e9d 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f  ol[i].zName, zCo
23e9e 6c 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 69  l)==0 ) return i
23e9f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d  ;.  }.  return -
23ea0 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  1;.}../*.** Set 
23ea1 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 74  the value of a t
23ea2 6f 6b 65 6e 20 74 6f 20 61 20 27 5c 30 30 30 27  oken to a '\000'
23ea3 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69  -terminated stri
23ea4 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ng..*/.static vo
23ea5 69 64 20 73 65 74 54 6f 6b 65 6e 28 54 6f 6b 65  id setToken(Toke
23ea6 6e 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72  n *p, const char
23ea7 20 2a 7a 29 7b 0a 20 20 70 2d 3e 7a 20 3d 20 28   *z){.  p->z = (
23ea8 75 38 2a 29 7a 3b 0a 20 20 70 2d 3e 6e 20 3d 20  u8*)z;.  p->n = 
23ea9 7a 20 3f 20 73 74 72 6c 65 6e 28 7a 29 20 3a 20  z ? strlen(z) : 
23eaa 30 3b 0a 20 20 70 2d 3e 64 79 6e 20 3d 20 30 3b  0;.  p->dyn = 0;
23eab 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
23eac 65 20 74 6f 6b 65 6e 20 74 6f 20 74 68 65 20 64  e token to the d
23ead 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 61 6e 64  ouble-quoted and
23eae 20 65 73 63 61 70 65 64 20 76 65 72 73 69 6f 6e   escaped version
23eaf 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 70   of the string p
23eb0 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20  ointed.** to by 
23eb1 7a 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 3b 0a  z. For example;.
23eb2 2a 2a 0a 2a 2a 20 20 20 20 7b 61 22 62 63 7d 20  **.**    {a"bc} 
23eb3 20 2d 3e 20 20 7b 22 61 22 22 62 63 22 7d 0a 2a   ->  {"a""bc"}.*
23eb4 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65  /.static void se
23eb5 74 51 75 6f 74 65 64 54 6f 6b 65 6e 28 50 61 72  tQuotedToken(Par
23eb6 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65  se *pParse, Toke
23eb7 6e 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72  n *p, const char
23eb8 20 2a 7a 29 7b 0a 0a 20 20 2f 2a 20 43 68 65 63   *z){..  /* Chec
23eb9 6b 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 20  k if the string 
23eba 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 71 75  appears to be qu
23ebb 6f 74 65 64 20 75 73 69 6e 67 20 22 2e 2e 2e 22  oted using "..."
23ebc 20 6f 72 20 60 2e 2e 2e 60 0a 20 20 2a 2a 20 6f   or `...`.  ** o
23ebd 72 20 5b 2e 2e 2e 5d 20 6f 72 20 27 2e 2e 2e 27  r [...] or '...'
23ebe 20 6f 72 20 69 66 20 74 68 65 20 73 74 72 69 6e   or if the strin
23ebf 67 20 63 6f 6e 74 61 69 6e 73 20 61 6e 79 20 22  g contains any "
23ec0 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 0a 20   characters.  . 
23ec1 20 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 2c 20   ** If it does, 
23ec2 74 68 65 6e 20 72 65 63 6f 72 64 20 61 20 76 65  then record a ve
23ec3 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 72  rsion of the str
23ec4 69 6e 67 20 77 69 74 68 20 74 68 65 20 73 70 65  ing with the spe
23ec5 63 69 61 6c 0a 20 20 2a 2a 20 63 68 61 72 61 63  cial.  ** charac
23ec6 74 65 72 73 20 65 73 63 61 70 65 64 2e 0a 20 20  ters escaped..  
23ec7 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
23ec8 2a 7a 32 20 3d 20 7a 3b 0a 20 20 69 66 28 20 2a  *z2 = z;.  if( *
23ec9 7a 32 21 3d 27 5b 27 20 26 26 20 2a 7a 32 21 3d  z2!='[' && *z2!=
23eca 27 60 27 20 26 26 20 2a 7a 32 21 3d 27 5c 27 27  '`' && *z2!='\''
23ecb 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 2a   ){.    while( *
23ecc 7a 32 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  z2 ){.      if( 
23ecd 2a 7a 32 3d 3d 27 22 27 20 29 20 62 72 65 61 6b  *z2=='"' ) break
23ece 3b 0a 20 20 20 20 20 20 7a 32 2b 2b 3b 0a 20 20  ;.      z2++;.  
23ecf 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 2a    }.  }..  if( *
23ed0 7a 32 20 29 7b 0a 20 20 20 20 2f 2a 20 53 74 72  z2 ){.    /* Str
23ed1 69 6e 67 20 63 6f 6e 74 61 69 6e 73 20 22 20 63  ing contains " c
23ed2 68 61 72 61 63 74 65 72 73 20 2d 20 63 6f 70 79  haracters - copy
23ed3 20 61 6e 64 20 71 75 6f 74 65 20 74 68 65 20 73   and quote the s
23ed4 74 72 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 70 2d  tring. */.    p-
23ed5 3e 7a 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74  >z = (u8 *)sqlit
23ed6 65 33 4d 50 72 69 6e 74 66 28 70 50 61 72 73 65  e3MPrintf(pParse
23ed7 2d 3e 64 62 2c 20 22 5c 22 25 77 5c 22 22 2c 20  ->db, "\"%w\"", 
23ed8 7a 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 7a  z);.    if( p->z
23ed9 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 20 3d   ){.      p->n =
23eda 20 73 74 72 6c 65 6e 28 28 63 68 61 72 20 2a 29   strlen((char *)
23edb 70 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 2d 3e  p->z);.      p->
23edc 64 79 6e 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  dyn = 1;.    }. 
23edd 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 53   }else{.    /* S
23ede 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 73 20 6e  tring contains n
23edf 6f 20 22 20 63 68 61 72 61 63 74 65 72 73 20 2d  o " characters -
23ee0 20 63 6f 70 79 20 74 68 65 20 70 6f 69 6e 74 65   copy the pointe
23ee1 72 2e 20 2a 2f 0a 20 20 20 20 70 2d 3e 7a 20 3d  r. */.    p->z =
23ee2 20 28 75 38 2a 29 7a 3b 0a 20 20 20 20 70 2d 3e   (u8*)z;.    p->
23ee3 6e 20 3d 20 28 7a 32 20 2d 20 7a 29 3b 0a 20 20  n = (z2 - z);.  
23ee4 20 20 70 2d 3e 64 79 6e 20 3d 20 30 3b 0a 20 20    p->dyn = 0;.  
23ee5 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  }.}../*.** Creat
23ee6 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  e an expression 
23ee7 6e 6f 64 65 20 66 6f 72 20 61 6e 20 69 64 65 6e  node for an iden
23ee8 74 69 66 69 65 72 20 77 69 74 68 20 74 68 65 20  tifier with the 
23ee9 6e 61 6d 65 20 6f 66 20 7a 4e 61 6d 65 0a 2a 2f  name of zName.*/
23eea 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
23eeb 45 78 70 72 20 2a 73 71 6c 69 74 65 33 43 72 65  Expr *sqlite3Cre
23eec 61 74 65 49 64 45 78 70 72 28 50 61 72 73 65 20  ateIdExpr(Parse 
23eed 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63  *pParse, const c
23eee 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 54  har *zName){.  T
23eef 6f 6b 65 6e 20 64 75 6d 6d 79 3b 0a 20 20 73 65  oken dummy;.  se
23ef0 74 54 6f 6b 65 6e 28 26 64 75 6d 6d 79 2c 20 7a  tToken(&dummy, z
23ef1 4e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 20  Name);.  return 
23ef2 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
23ef3 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30  rse, TK_ID, 0, 0
23ef4 2c 20 26 64 75 6d 6d 79 29 3b 0a 7d 0a 0a 2f 2a  , &dummy);.}../*
23ef5 0a 2a 2a 20 41 64 64 20 61 20 74 65 72 6d 20 74  .** Add a term t
23ef6 6f 20 74 68 65 20 57 48 45 52 45 20 65 78 70 72  o the WHERE expr
23ef7 65 73 73 69 6f 6e 20 69 6e 20 2a 70 70 45 78 70  ession in *ppExp
23ef8 72 20 74 68 61 74 20 72 65 71 75 69 72 65 73 20  r that requires 
23ef9 74 68 65 0a 2a 2a 20 7a 43 6f 6c 20 63 6f 6c 75  the.** zCol colu
23efa 6d 6e 20 74 6f 20 62 65 20 65 71 75 61 6c 20 69  mn to be equal i
23efb 6e 20 74 68 65 20 74 77 6f 20 74 61 62 6c 65 73  n the two tables
23efc 20 70 54 61 62 31 20 61 6e 64 20 70 54 61 62 32   pTab1 and pTab2
23efd 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
23efe 20 61 64 64 57 68 65 72 65 54 65 72 6d 28 0a 20   addWhereTerm(. 
23eff 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
23f00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
23f01 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
23f02 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43    const char *zC
23f03 6f 6c 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  ol,        /* Na
23f04 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  me of the column
23f05 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 61 62 6c   */.  const Tabl
23f06 65 20 2a 70 54 61 62 31 2c 20 20 20 20 20 20 2f  e *pTab1,      /
23f07 2a 20 46 69 72 73 74 20 74 61 62 6c 65 20 2a 2f  * First table */
23f08 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
23f09 41 6c 69 61 73 31 2c 20 20 20 20 20 2f 2a 20 41  Alias1,     /* A
23f0a 6c 69 61 73 20 66 6f 72 20 66 69 72 73 74 20 74  lias for first t
23f0b 61 62 6c 65 2e 20 20 4d 61 79 20 62 65 20 4e 55  able.  May be NU
23f0c 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 61  LL */.  const Ta
23f0d 62 6c 65 20 2a 70 54 61 62 32 2c 20 20 20 20 20  ble *pTab2,     
23f0e 20 2f 2a 20 53 65 63 6f 6e 64 20 74 61 62 6c 65   /* Second table
23f0f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
23f10 20 2a 7a 41 6c 69 61 73 32 2c 20 20 20 20 20 2f   *zAlias2,     /
23f11 2a 20 41 6c 69 61 73 20 66 6f 72 20 73 65 63 6f  * Alias for seco
23f12 6e 64 20 74 61 62 6c 65 2e 20 20 4d 61 79 20 62  nd table.  May b
23f13 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20  e NULL */.  int 
23f14 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 2c  iRightJoinTable,
23f15 20 20 20 20 20 2f 2a 20 56 44 42 45 20 63 75 72       /* VDBE cur
23f16 73 6f 72 20 66 6f 72 20 74 68 65 20 72 69 67 68  sor for the righ
23f17 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70  t table */.  Exp
23f18 72 20 2a 2a 70 70 45 78 70 72 2c 20 20 20 20 20  r **ppExpr,     
23f19 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65        /* Add the
23f1a 20 65 71 75 61 6c 69 74 79 20 74 65 72 6d 20 74   equality term t
23f1b 6f 20 74 68 69 73 20 65 78 70 72 65 73 73 69 6f  o this expressio
23f1c 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4f 75 74  n */.  int isOut
23f1d 65 72 4a 6f 69 6e 20 20 20 20 20 20 20 20 20 20  erJoin          
23f1e 2f 2a 20 54 72 75 65 20 69 66 20 64 65 61 6c 69  /* True if deali
23f1f 6e 67 20 77 69 74 68 20 61 6e 20 4f 55 54 45 52  ng with an OUTER
23f20 20 6a 6f 69 6e 20 2a 2f 0a 29 7b 0a 20 20 45 78   join */.){.  Ex
23f21 70 72 20 2a 70 45 31 61 2c 20 2a 70 45 31 62 2c  pr *pE1a, *pE1b,
23f22 20 2a 70 45 31 63 3b 0a 20 20 45 78 70 72 20 2a   *pE1c;.  Expr *
23f23 70 45 32 61 2c 20 2a 70 45 32 62 2c 20 2a 70 45  pE2a, *pE2b, *pE
23f24 32 63 3b 0a 20 20 45 78 70 72 20 2a 70 45 3b 0a  2c;.  Expr *pE;.
23f25 0a 20 20 70 45 31 61 20 3d 20 73 71 6c 69 74 65  .  pE1a = sqlite
23f26 33 43 72 65 61 74 65 49 64 45 78 70 72 28 70 50  3CreateIdExpr(pP
23f27 61 72 73 65 2c 20 7a 43 6f 6c 29 3b 0a 20 20 70  arse, zCol);.  p
23f28 45 32 61 20 3d 20 73 71 6c 69 74 65 33 43 72 65  E2a = sqlite3Cre
23f29 61 74 65 49 64 45 78 70 72 28 70 50 61 72 73 65  ateIdExpr(pParse
23f2a 2c 20 7a 43 6f 6c 29 3b 0a 20 20 69 66 28 20 7a  , zCol);.  if( z
23f2b 41 6c 69 61 73 31 3d 3d 30 20 29 7b 0a 20 20 20  Alias1==0 ){.   
23f2c 20 7a 41 6c 69 61 73 31 20 3d 20 70 54 61 62 31   zAlias1 = pTab1
23f2d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 7d 0a 20 20 70  ->zName;.  }.  p
23f2e 45 31 62 20 3d 20 73 71 6c 69 74 65 33 43 72 65  E1b = sqlite3Cre
23f2f 61 74 65 49 64 45 78 70 72 28 70 50 61 72 73 65  ateIdExpr(pParse
23f30 2c 20 7a 41 6c 69 61 73 31 29 3b 0a 20 20 69 66  , zAlias1);.  if
23f31 28 20 7a 41 6c 69 61 73 32 3d 3d 30 20 29 7b 0a  ( zAlias2==0 ){.
23f32 20 20 20 20 7a 41 6c 69 61 73 32 20 3d 20 70 54      zAlias2 = pT
23f33 61 62 32 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 7d 0a  ab2->zName;.  }.
23f34 20 20 70 45 32 62 20 3d 20 73 71 6c 69 74 65 33    pE2b = sqlite3
23f35 43 72 65 61 74 65 49 64 45 78 70 72 28 70 50 61  CreateIdExpr(pPa
23f36 72 73 65 2c 20 7a 41 6c 69 61 73 32 29 3b 0a 20  rse, zAlias2);. 
23f37 20 70 45 31 63 20 3d 20 73 71 6c 69 74 65 33 50   pE1c = sqlite3P
23f38 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
23f39 44 4f 54 2c 20 70 45 31 62 2c 20 70 45 31 61 2c  DOT, pE1b, pE1a,
23f3a 20 30 29 3b 0a 20 20 70 45 32 63 20 3d 20 73 71   0);.  pE2c = sq
23f3b 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
23f3c 65 2c 20 54 4b 5f 44 4f 54 2c 20 70 45 32 62 2c  e, TK_DOT, pE2b,
23f3d 20 70 45 32 61 2c 20 30 29 3b 0a 20 20 70 45 20   pE2a, 0);.  pE 
23f3e 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
23f3f 50 61 72 73 65 2c 20 54 4b 5f 45 51 2c 20 70 45  Parse, TK_EQ, pE
23f40 31 63 2c 20 70 45 32 63 2c 20 30 29 3b 0a 20 20  1c, pE2c, 0);.  
23f41 69 66 28 20 70 45 20 26 26 20 69 73 4f 75 74 65  if( pE && isOute
23f42 72 4a 6f 69 6e 20 29 7b 0a 20 20 20 20 45 78 70  rJoin ){.    Exp
23f43 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 2c  rSetProperty(pE,
23f44 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 3b 0a 20   EP_FromJoin);. 
23f45 20 20 20 70 45 2d 3e 69 52 69 67 68 74 4a 6f 69     pE->iRightJoi
23f46 6e 54 61 62 6c 65 20 3d 20 69 52 69 67 68 74 4a  nTable = iRightJ
23f47 6f 69 6e 54 61 62 6c 65 3b 0a 20 20 7d 0a 20 20  oinTable;.  }.  
23f48 2a 70 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65  *ppExpr = sqlite
23f49 33 45 78 70 72 41 6e 64 28 70 50 61 72 73 65 2d  3ExprAnd(pParse-
23f4a 3e 64 62 2c 2a 70 70 45 78 70 72 2c 20 70 45 29  >db,*ppExpr, pE)
23f4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  ;.}../*.** Set t
23f4c 68 65 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 20 70  he EP_FromJoin p
23f4d 72 6f 70 65 72 74 79 20 6f 6e 20 61 6c 6c 20 74  roperty on all t
23f4e 65 72 6d 73 20 6f 66 20 74 68 65 20 67 69 76 65  erms of the give
23f4f 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a  n expression..**
23f50 20 41 6e 64 20 73 65 74 20 74 68 65 20 45 78 70   And set the Exp
23f51 72 2e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c  r.iRightJoinTabl
23f52 65 20 74 6f 20 69 54 61 62 6c 65 20 66 6f 72 20  e to iTable for 
23f53 65 76 65 72 79 20 74 65 72 6d 20 69 6e 20 74 68  every term in th
23f54 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e  e.** expression.
23f55 0a 2a 2a 0a 2a 2a 20 54 68 65 20 45 50 5f 46 72  .**.** The EP_Fr
23f56 6f 6d 4a 6f 69 6e 20 70 72 6f 70 65 72 74 79 20  omJoin property 
23f57 69 73 20 75 73 65 64 20 6f 6e 20 74 65 72 6d 73  is used on terms
23f58 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f   of an expressio
23f59 6e 20 74 6f 20 74 65 6c 6c 0a 2a 2a 20 74 68 65  n to tell.** the
23f5a 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e   LEFT OUTER JOIN
23f5b 20 70 72 6f 63 65 73 73 69 6e 67 20 6c 6f 67 69   processing logi
23f5c 63 20 74 68 61 74 20 74 68 69 73 20 74 65 72 6d  c that this term
23f5d 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 0a   is part of the.
23f5e 2a 2a 20 6a 6f 69 6e 20 72 65 73 74 72 69 63 74  ** join restrict
23f5f 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20 69 6e  ion specified in
23f60 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47   the ON or USING
23f61 20 63 6c 61 75 73 65 20 61 6e 64 20 6e 6f 74 20   clause and not 
23f62 61 20 70 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65  a part.** of the
23f63 20 6d 6f 72 65 20 67 65 6e 65 72 61 6c 20 57 48   more general WH
23f64 45 52 45 20 63 6c 61 75 73 65 2e 20 20 54 68 65  ERE clause.  The
23f65 73 65 20 74 65 72 6d 73 20 61 72 65 20 6d 6f 76  se terms are mov
23f66 65 64 20 6f 76 65 72 20 74 6f 20 74 68 65 0a 2a  ed over to the.*
23f67 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 64  * WHERE clause d
23f68 75 72 69 6e 67 20 6a 6f 69 6e 20 70 72 6f 63 65  uring join proce
23f69 73 73 69 6e 67 20 62 75 74 20 77 65 20 6e 65 65  ssing but we nee
23f6a 64 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 68  d to remember th
23f6b 61 74 20 74 68 65 79 0a 2a 2a 20 6f 72 69 67 69  at they.** origi
23f6c 6e 61 74 65 64 20 69 6e 20 74 68 65 20 4f 4e 20  nated in the ON 
23f6d 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 2e  or USING clause.
23f6e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 45 78 70 72 2e  .**.** The Expr.
23f6f 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20  iRightJoinTable 
23f70 74 65 6c 6c 73 20 74 68 65 20 57 48 45 52 45 20  tells the WHERE 
23f71 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e  clause processin
23f72 67 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 65 78  g that the.** ex
23f73 70 72 65 73 73 69 6f 6e 20 64 65 70 65 6e 64 73  pression depends
23f74 20 6f 6e 20 74 61 62 6c 65 20 69 52 69 67 68 74   on table iRight
23f75 4a 6f 69 6e 54 61 62 6c 65 20 65 76 65 6e 20 69  JoinTable even i
23f76 66 20 74 68 61 74 20 74 61 62 6c 65 20 69 73 20  f that table is 
23f77 6e 6f 74 0a 2a 2a 20 65 78 70 6c 69 63 69 74 6c  not.** explicitl
23f78 79 20 6d 65 6e 74 69 6f 6e 65 64 20 69 6e 20 74  y mentioned in t
23f79 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20  he expression.  
23f7a 54 68 61 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  That information
23f7b 20 69 73 20 6e 65 65 64 65 64 0a 2a 2a 20 66 6f   is needed.** fo
23f7c 72 20 63 61 73 65 73 20 6c 69 6b 65 20 74 68 69  r cases like thi
23f7d 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 45 4c 45  s:.**.**    SELE
23f7e 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 4c 45 46  CT * FROM t1 LEF
23f7f 54 20 4a 4f 49 4e 20 74 32 20 4f 4e 20 74 31 2e  T JOIN t2 ON t1.
23f80 61 3d 74 32 2e 62 20 41 4e 44 20 74 31 2e 78 3d  a=t2.b AND t1.x=
23f81 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 77 68 65 72  5.**.** The wher
23f82 65 20 63 6c 61 75 73 65 20 6e 65 65 64 73 20 74  e clause needs t
23f83 6f 20 64 65 66 65 72 20 74 68 65 20 68 61 6e 64  o defer the hand
23f84 6c 69 6e 67 20 6f 66 20 74 68 65 20 74 31 2e 78  ling of the t1.x
23f85 3d 35 0a 2a 2a 20 74 65 72 6d 20 75 6e 74 69 6c  =5.** term until
23f86 20 61 66 74 65 72 20 74 68 65 20 74 32 20 6c 6f   after the t2 lo
23f87 6f 70 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 20  op of the join. 
23f88 20 49 6e 20 74 68 61 74 20 77 61 79 2c 20 61 0a   In that way, a.
23f89 2a 2a 20 4e 55 4c 4c 20 74 32 20 72 6f 77 20 77  ** NULL t2 row w
23f8a 69 6c 6c 20 62 65 20 69 6e 73 65 72 74 65 64 20  ill be inserted 
23f8b 77 68 65 6e 65 76 65 72 20 74 31 2e 78 21 3d 35  whenever t1.x!=5
23f8c 2e 20 20 49 66 20 77 65 20 64 6f 20 6e 6f 74 0a  .  If we do not.
23f8d 2a 2a 20 64 65 66 65 72 20 74 68 65 20 68 61 6e  ** defer the han
23f8e 64 6c 69 6e 67 20 6f 66 20 74 31 2e 78 3d 35 2c  dling of t1.x=5,
23f8f 20 69 74 20 77 69 6c 6c 20 62 65 20 70 72 6f 63   it will be proc
23f90 65 73 73 65 64 20 69 6d 6d 65 64 69 61 74 65 6c  essed immediatel
23f91 79 0a 2a 2a 20 61 66 74 65 72 20 74 68 65 20 74  y.** after the t
23f92 31 20 6c 6f 6f 70 20 61 6e 64 20 72 6f 77 73 20  1 loop and rows 
23f93 77 69 74 68 20 74 31 2e 78 21 3d 35 20 77 69 6c  with t1.x!=5 wil
23f94 6c 20 6e 65 76 65 72 20 61 70 70 65 61 72 20 69  l never appear i
23f95 6e 0a 2a 2a 20 74 68 65 20 6f 75 74 70 75 74 2c  n.** the output,
23f96 20 77 68 69 63 68 20 69 73 20 69 6e 63 6f 72 72   which is incorr
23f97 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ect..*/.static v
23f98 6f 69 64 20 73 65 74 4a 6f 69 6e 45 78 70 72 28  oid setJoinExpr(
23f99 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 69 54 61  Expr *p, int iTa
23f9a 62 6c 65 29 7b 0a 20 20 77 68 69 6c 65 28 20 70  ble){.  while( p
23f9b 20 29 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50   ){.    ExprSetP
23f9c 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 46 72  roperty(p, EP_Fr
23f9d 6f 6d 4a 6f 69 6e 29 3b 0a 20 20 20 20 70 2d 3e  omJoin);.    p->
23f9e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20  iRightJoinTable 
23f9f 3d 20 69 54 61 62 6c 65 3b 0a 20 20 20 20 73 65  = iTable;.    se
23fa0 74 4a 6f 69 6e 45 78 70 72 28 70 2d 3e 70 4c 65  tJoinExpr(p->pLe
23fa1 66 74 2c 20 69 54 61 62 6c 65 29 3b 0a 20 20 20  ft, iTable);.   
23fa2 20 70 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a   p = p->pRight;.
23fa3 20 20 7d 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68    } .}../*.** Th
23fa4 69 73 20 72 6f 75 74 69 6e 65 20 70 72 6f 63 65  is routine proce
23fa5 73 73 65 73 20 74 68 65 20 6a 6f 69 6e 20 69 6e  sses the join in
23fa6 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20  formation for a 
23fa7 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
23fa8 2e 0a 2a 2a 20 4f 4e 20 61 6e 64 20 55 53 49 4e  ..** ON and USIN
23fa9 47 20 63 6c 61 75 73 65 73 20 61 72 65 20 63 6f  G clauses are co
23faa 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 65 78 74  nverted into ext
23fab 72 61 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20  ra terms of the 
23fac 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 2a 2a  WHERE clause..**
23fad 20 4e 41 54 55 52 41 4c 20 6a 6f 69 6e 73 20 61   NATURAL joins a
23fae 6c 73 6f 20 63 72 65 61 74 65 20 65 78 74 72 61  lso create extra
23faf 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65   WHERE clause te
23fb0 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74  rms..**.** The t
23fb1 65 72 6d 73 20 6f 66 20 61 20 46 52 4f 4d 20 63  erms of a FROM c
23fb2 6c 61 75 73 65 20 61 72 65 20 63 6f 6e 74 61 69  lause are contai
23fb3 6e 65 64 20 69 6e 20 74 68 65 20 53 65 6c 65 63  ned in the Selec
23fb4 74 2e 70 53 72 63 20 73 74 72 75 63 74 75 72 65  t.pSrc structure
23fb5 2e 0a 2a 2a 20 54 68 65 20 6c 65 66 74 20 6d 6f  ..** The left mo
23fb6 73 74 20 74 61 62 6c 65 20 69 73 20 74 68 65 20  st table is the 
23fb7 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20 53  first entry in S
23fb8 65 6c 65 63 74 2e 70 53 72 63 2e 20 20 54 68 65  elect.pSrc.  The
23fb9 20 72 69 67 68 74 2d 6d 6f 73 74 0a 2a 2a 20 74   right-most.** t
23fba 61 62 6c 65 20 69 73 20 74 68 65 20 6c 61 73 74  able is the last
23fbb 20 65 6e 74 72 79 2e 20 20 54 68 65 20 6a 6f 69   entry.  The joi
23fbc 6e 20 6f 70 65 72 61 74 6f 72 20 69 73 20 68 65  n operator is he
23fbd 6c 64 20 69 6e 20 74 68 65 20 65 6e 74 72 79 20  ld in the entry 
23fbe 74 6f 0a 2a 2a 20 74 68 65 20 6c 65 66 74 2e 20  to.** the left. 
23fbf 20 54 68 75 73 20 65 6e 74 72 79 20 30 20 63 6f   Thus entry 0 co
23fc0 6e 74 61 69 6e 73 20 74 68 65 20 6a 6f 69 6e 20  ntains the join 
23fc1 6f 70 65 72 61 74 6f 72 20 66 6f 72 20 74 68 65  operator for the
23fc2 20 6a 6f 69 6e 20 62 65 74 77 65 65 6e 0a 2a 2a   join between.**
23fc3 20 65 6e 74 72 69 65 73 20 30 20 61 6e 64 20 31   entries 0 and 1
23fc4 2e 20 20 41 6e 79 20 4f 4e 20 6f 72 20 55 53 49  .  Any ON or USI
23fc5 4e 47 20 63 6c 61 75 73 65 73 20 61 73 73 6f 63  NG clauses assoc
23fc6 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 6a  iated with the j
23fc7 6f 69 6e 20 61 72 65 0a 2a 2a 20 61 6c 73 6f 20  oin are.** also 
23fc8 61 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20  attached to the 
23fc9 6c 65 66 74 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a  left entry..**.*
23fca 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
23fcb 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65  eturns the numbe
23fcc 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 6f  r of errors enco
23fcd 75 6e 74 65 72 65 64 2e 0a 2a 2f 0a 73 74 61 74  untered..*/.stat
23fce 69 63 20 69 6e 74 20 73 71 6c 69 74 65 50 72 6f  ic int sqlitePro
23fcf 63 65 73 73 4a 6f 69 6e 28 50 61 72 73 65 20 2a  cessJoin(Parse *
23fd0 70 50 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a  pParse, Select *
23fd1 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70  p){.  SrcList *p
23fd2 53 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  Src;            
23fd3 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74 61 62        /* All tab
23fd4 6c 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  les in the FROM 
23fd5 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20  clause */.  int 
23fd6 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20  i, j;           
23fd7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
23fd8 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a  oop counters */.
23fd9 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
23fda 5f 69 74 65 6d 20 2a 70 4c 65 66 74 3b 20 20 20  _item *pLeft;   
23fdb 20 20 2f 2a 20 4c 65 66 74 20 74 61 62 6c 65 20    /* Left table 
23fdc 62 65 69 6e 67 20 6a 6f 69 6e 65 64 20 2a 2f 0a  being joined */.
23fdd 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
23fde 5f 69 74 65 6d 20 2a 70 52 69 67 68 74 3b 20 20  _item *pRight;  
23fdf 20 20 2f 2a 20 52 69 67 68 74 20 74 61 62 6c 65    /* Right table
23fe0 20 62 65 69 6e 67 20 6a 6f 69 6e 65 64 20 2a 2f   being joined */
23fe1 0a 0a 20 20 70 53 72 63 20 3d 20 70 2d 3e 70 53  ..  pSrc = p->pS
23fe2 72 63 3b 0a 20 20 70 4c 65 66 74 20 3d 20 26 70  rc;.  pLeft = &p
23fe3 53 72 63 2d 3e 61 5b 30 5d 3b 0a 20 20 70 52 69  Src->a[0];.  pRi
23fe4 67 68 74 20 3d 20 26 70 4c 65 66 74 5b 31 5d 3b  ght = &pLeft[1];
23fe5 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 53  .  for(i=0; i<pS
23fe6 72 63 2d 3e 6e 53 72 63 2d 31 3b 20 69 2b 2b 2c  rc->nSrc-1; i++,
23fe7 20 70 52 69 67 68 74 2b 2b 2c 20 70 4c 65 66 74   pRight++, pLeft
23fe8 2b 2b 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a  ++){.    Table *
23fe9 70 4c 65 66 74 54 61 62 20 3d 20 70 4c 65 66 74  pLeftTab = pLeft
23fea 2d 3e 70 54 61 62 3b 0a 20 20 20 20 54 61 62 6c  ->pTab;.    Tabl
23feb 65 20 2a 70 52 69 67 68 74 54 61 62 20 3d 20 70  e *pRightTab = p
23fec 52 69 67 68 74 2d 3e 70 54 61 62 3b 0a 20 20 20  Right->pTab;.   
23fed 20 69 6e 74 20 69 73 4f 75 74 65 72 3b 0a 0a 20   int isOuter;.. 
23fee 20 20 20 69 66 28 20 70 4c 65 66 74 54 61 62 3d     if( pLeftTab=
23fef 3d 30 20 7c 7c 20 70 52 69 67 68 74 54 61 62 3d  =0 || pRightTab=
23ff0 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  =0 ) continue;. 
23ff1 20 20 20 69 73 4f 75 74 65 72 20 3d 20 28 70 52     isOuter = (pR
23ff2 69 67 68 74 2d 3e 6a 6f 69 6e 74 79 70 65 20 26  ight->jointype &
23ff3 20 4a 54 5f 4f 55 54 45 52 29 21 3d 30 3b 0a 0a   JT_OUTER)!=0;..
23ff4 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65 20      /* When the 
23ff5 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f 72 64 20  NATURAL keyword 
23ff6 69 73 20 70 72 65 73 65 6e 74 2c 20 61 64 64 20  is present, add 
23ff7 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72  WHERE clause ter
23ff8 6d 73 20 66 6f 72 0a 20 20 20 20 2a 2a 20 65 76  ms for.    ** ev
23ff9 65 72 79 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20  ery column that 
23ffa 74 68 65 20 74 77 6f 20 74 61 62 6c 65 73 20 68  the two tables h
23ffb 61 76 65 20 69 6e 20 63 6f 6d 6d 6f 6e 2e 0a 20  ave in common.. 
23ffc 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 52     */.    if( pR
23ffd 69 67 68 74 2d 3e 6a 6f 69 6e 74 79 70 65 20 26  ight->jointype &
23ffe 20 4a 54 5f 4e 41 54 55 52 41 4c 20 29 7b 0a 20   JT_NATURAL ){. 
23fff 20 20 20 20 20 69 66 28 20 70 52 69 67 68 74 2d       if( pRight-
24000 3e 70 4f 6e 20 7c 7c 20 70 52 69 67 68 74 2d 3e  >pOn || pRight->
24001 70 55 73 69 6e 67 20 29 7b 0a 20 20 20 20 20 20  pUsing ){.      
24002 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
24003 67 28 70 50 61 72 73 65 2c 20 22 61 20 4e 41 54  g(pParse, "a NAT
24004 55 52 41 4c 20 6a 6f 69 6e 20 6d 61 79 20 6e 6f  URAL join may no
24005 74 20 68 61 76 65 20 22 0a 20 20 20 20 20 20 20  t have ".       
24006 20 20 20 20 22 61 6e 20 4f 4e 20 6f 72 20 55 53      "an ON or US
24007 49 4e 47 20 63 6c 61 75 73 65 22 2c 20 30 29 3b  ING clause", 0);
24008 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
24009 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
2400a 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4c 65 66   for(j=0; j<pLef
2400b 74 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29  tTab->nCol; j++)
2400c 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a  {.        char *
2400d 7a 4e 61 6d 65 20 3d 20 70 4c 65 66 74 54 61 62  zName = pLeftTab
2400e 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 3b  ->aCol[j].zName;
2400f 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 6f 6c  .        if( col
24010 75 6d 6e 49 6e 64 65 78 28 70 52 69 67 68 74 54  umnIndex(pRightT
24011 61 62 2c 20 7a 4e 61 6d 65 29 3e 3d 30 20 29 7b  ab, zName)>=0 ){
24012 0a 20 20 20 20 20 20 20 20 20 20 61 64 64 57 68  .          addWh
24013 65 72 65 54 65 72 6d 28 70 50 61 72 73 65 2c 20  ereTerm(pParse, 
24014 7a 4e 61 6d 65 2c 20 70 4c 65 66 74 54 61 62 2c  zName, pLeftTab,
24015 20 70 4c 65 66 74 2d 3e 7a 41 6c 69 61 73 2c 20   pLeft->zAlias, 
24016 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
24017 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
24018 52 69 67 68 74 54 61 62 2c 20 70 52 69 67 68 74  RightTab, pRight
24019 2d 3e 7a 41 6c 69 61 73 2c 0a 20 20 20 20 20 20  ->zAlias,.      
2401a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2401b 20 20 20 20 20 20 20 20 70 52 69 67 68 74 2d 3e          pRight->
2401c 69 43 75 72 73 6f 72 2c 20 26 70 2d 3e 70 57 68  iCursor, &p->pWh
2401d 65 72 65 2c 20 69 73 4f 75 74 65 72 29 3b 0a 20  ere, isOuter);. 
2401e 20 20 20 20 20 20 20 20 20 0a 20 20 20 20 20 20           .      
2401f 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
24020 7d 0a 0a 20 20 20 20 2f 2a 20 44 69 73 61 6c 6c  }..    /* Disall
24021 6f 77 20 62 6f 74 68 20 4f 4e 20 61 6e 64 20 55  ow both ON and U
24022 53 49 4e 47 20 63 6c 61 75 73 65 73 20 69 6e 20  SING clauses in 
24023 74 68 65 20 73 61 6d 65 20 6a 6f 69 6e 0a 20 20  the same join.  
24024 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 52 69    */.    if( pRi
24025 67 68 74 2d 3e 70 4f 6e 20 26 26 20 70 52 69 67  ght->pOn && pRig
24026 68 74 2d 3e 70 55 73 69 6e 67 20 29 7b 0a 20 20  ht->pUsing ){.  
24027 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
24028 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e  Msg(pParse, "can
24029 6e 6f 74 20 68 61 76 65 20 62 6f 74 68 20 4f 4e  not have both ON
2402a 20 61 6e 64 20 55 53 49 4e 47 20 22 0a 20 20 20   and USING ".   
2402b 20 20 20 20 20 22 63 6c 61 75 73 65 73 20 69 6e       "clauses in
2402c 20 74 68 65 20 73 61 6d 65 20 6a 6f 69 6e 22 29   the same join")
2402d 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  ;.      return 1
2402e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
2402f 41 64 64 20 74 68 65 20 4f 4e 20 63 6c 61 75 73  Add the ON claus
24030 65 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  e to the end of 
24031 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
24032 2c 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 0a 20  , connected by. 
24033 20 20 20 2a 2a 20 61 6e 20 41 4e 44 20 6f 70 65     ** an AND ope
24034 72 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20  rator..    */.  
24035 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 70 4f    if( pRight->pO
24036 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69  n ){.      if( i
24037 73 4f 75 74 65 72 20 29 20 73 65 74 4a 6f 69 6e  sOuter ) setJoin
24038 45 78 70 72 28 70 52 69 67 68 74 2d 3e 70 4f 6e  Expr(pRight->pOn
24039 2c 20 70 52 69 67 68 74 2d 3e 69 43 75 72 73 6f  , pRight->iCurso
2403a 72 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 57 68  r);.      p->pWh
2403b 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ere = sqlite3Exp
2403c 72 41 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c  rAnd(pParse->db,
2403d 20 70 2d 3e 70 57 68 65 72 65 2c 20 70 52 69 67   p->pWhere, pRig
2403e 68 74 2d 3e 70 4f 6e 29 3b 0a 20 20 20 20 20 20  ht->pOn);.      
2403f 70 52 69 67 68 74 2d 3e 70 4f 6e 20 3d 20 30 3b  pRight->pOn = 0;
24040 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43  .    }..    /* C
24041 72 65 61 74 65 20 65 78 74 72 61 20 74 65 72 6d  reate extra term
24042 73 20 6f 6e 20 74 68 65 20 57 48 45 52 45 20 63  s on the WHERE c
24043 6c 61 75 73 65 20 66 6f 72 20 65 61 63 68 20 63  lause for each c
24044 6f 6c 75 6d 6e 20 6e 61 6d 65 64 0a 20 20 20 20  olumn named.    
24045 2a 2a 20 69 6e 20 74 68 65 20 55 53 49 4e 47 20  ** in the USING 
24046 63 6c 61 75 73 65 2e 20 20 45 78 61 6d 70 6c 65  clause.  Example
24047 3a 20 49 66 20 74 68 65 20 74 77 6f 20 74 61 62  : If the two tab
24048 6c 65 73 20 74 6f 20 62 65 20 6a 6f 69 6e 65 64  les to be joined
24049 20 61 72 65 20 0a 20 20 20 20 2a 2a 20 41 20 61   are .    ** A a
2404a 6e 64 20 42 20 61 6e 64 20 74 68 65 20 55 53 49  nd B and the USI
2404b 4e 47 20 63 6c 61 75 73 65 20 6e 61 6d 65 73 20  NG clause names 
2404c 58 2c 20 59 2c 20 61 6e 64 20 5a 2c 20 74 68 65  X, Y, and Z, the
2404d 6e 20 61 64 64 20 74 68 69 73 0a 20 20 20 20 2a  n add this.    *
2404e 2a 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 63  * to the WHERE c
2404f 6c 61 75 73 65 3a 20 20 20 20 41 2e 58 3d 42 2e  lause:    A.X=B.
24050 58 20 41 4e 44 20 41 2e 59 3d 42 2e 59 20 41 4e  X AND A.Y=B.Y AN
24051 44 20 41 2e 5a 3d 42 2e 5a 0a 20 20 20 20 2a 2a  D A.Z=B.Z.    **
24052 20 52 65 70 6f 72 74 20 61 6e 20 65 72 72 6f 72   Report an error
24053 20 69 66 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 6d   if any column m
24054 65 6e 74 69 6f 6e 65 64 20 69 6e 20 74 68 65 20  entioned in the 
24055 55 53 49 4e 47 20 63 6c 61 75 73 65 20 69 73 0a  USING clause is.
24056 20 20 20 20 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61      ** not conta
24057 69 6e 65 64 20 69 6e 20 62 6f 74 68 20 74 61 62  ined in both tab
24058 6c 65 73 20 74 6f 20 62 65 20 6a 6f 69 6e 65 64  les to be joined
24059 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
2405a 20 70 52 69 67 68 74 2d 3e 70 55 73 69 6e 67 20   pRight->pUsing 
2405b 29 7b 0a 20 20 20 20 20 20 49 64 4c 69 73 74 20  ){.      IdList 
2405c 2a 70 4c 69 73 74 20 3d 20 70 52 69 67 68 74 2d  *pList = pRight-
2405d 3e 70 55 73 69 6e 67 3b 0a 20 20 20 20 20 20 66  >pUsing;.      f
2405e 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4c 69 73 74 2d  or(j=0; j<pList-
2405f 3e 6e 49 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  >nId; j++){.    
24060 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20      char *zName 
24061 3d 20 70 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 7a 4e  = pList->a[j].zN
24062 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ame;.        if(
24063 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 70 4c 65   columnIndex(pLe
24064 66 74 54 61 62 2c 20 7a 4e 61 6d 65 29 3c 30 20  ftTab, zName)<0 
24065 7c 7c 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 70  || columnIndex(p
24066 52 69 67 68 74 54 61 62 2c 20 7a 4e 61 6d 65 29  RightTab, zName)
24067 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  <0 ){.          
24068 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
24069 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20  pParse, "cannot 
2406a 6a 6f 69 6e 20 75 73 69 6e 67 20 63 6f 6c 75 6d  join using colum
2406b 6e 20 25 73 20 2d 20 63 6f 6c 75 6d 6e 20 22 0a  n %s - column ".
2406c 20 20 20 20 20 20 20 20 20 20 20 20 22 6e 6f 74              "not
2406d 20 70 72 65 73 65 6e 74 20 69 6e 20 62 6f 74 68   present in both
2406e 20 74 61 62 6c 65 73 22 2c 20 7a 4e 61 6d 65 29   tables", zName)
2406f 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  ;.          retu
24070 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a  rn 1;.        }.
24071 20 20 20 20 20 20 20 20 61 64 64 57 68 65 72 65          addWhere
24072 54 65 72 6d 28 70 50 61 72 73 65 2c 20 7a 4e 61  Term(pParse, zNa
24073 6d 65 2c 20 70 4c 65 66 74 54 61 62 2c 20 70 4c  me, pLeftTab, pL
24074 65 66 74 2d 3e 7a 41 6c 69 61 73 2c 20 0a 20 20  eft->zAlias, .  
24075 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24076 20 20 20 20 20 20 20 20 20 20 70 52 69 67 68 74            pRight
24077 54 61 62 2c 20 70 52 69 67 68 74 2d 3e 7a 41 6c  Tab, pRight->zAl
24078 69 61 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ias,.           
24079 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2407a 20 70 52 69 67 68 74 2d 3e 69 43 75 72 73 6f 72   pRight->iCursor
2407b 2c 20 26 70 2d 3e 70 57 68 65 72 65 2c 20 69 73  , &p->pWhere, is
2407c 4f 75 74 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a  Outer);.      }.
2407d 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
2407e 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  rn 0;.}../*.** I
2407f 6e 73 65 72 74 20 63 6f 64 65 20 69 6e 74 6f 20  nsert code into 
24080 22 76 22 20 74 68 61 74 20 77 69 6c 6c 20 70 75  "v" that will pu
24081 73 68 20 74 68 65 20 72 65 63 6f 72 64 20 6f 6e  sh the record on
24082 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 0a   the top of the.
24083 2a 2a 20 73 74 61 63 6b 20 69 6e 74 6f 20 74 68  ** stack into th
24084 65 20 73 6f 72 74 65 72 2e 0a 2a 2f 0a 73 74 61  e sorter..*/.sta
24085 74 69 63 20 76 6f 69 64 20 70 75 73 68 4f 6e 74  tic void pushOnt
24086 6f 53 6f 72 74 65 72 28 0a 20 20 50 61 72 73 65  oSorter(.  Parse
24087 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
24088 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74    /* Parser cont
24089 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ext */.  ExprLis
2408a 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20  t *pOrderBy,    
2408b 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20  /* The ORDER BY 
2408c 63 6c 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c 65  clause */.  Sele
2408d 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20  ct *pSelect,    
2408e 20 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20     /* The whole 
2408f 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
24090 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74   */.  int regDat
24091 61 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  a            /* 
24092 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67  Register holding
24093 20 64 61 74 61 20 74 6f 20 62 65 20 73 6f 72 74   data to be sort
24094 65 64 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20  ed */.){.  Vdbe 
24095 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
24096 62 65 3b 0a 20 20 69 6e 74 20 6e 45 78 70 72 20  be;.  int nExpr 
24097 3d 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  = pOrderBy->nExp
24098 72 3b 0a 20 20 69 6e 74 20 72 65 67 42 61 73 65  r;.  int regBase
24099 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
2409a 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e  pRange(pParse, n
2409b 45 78 70 72 2b 32 29 3b 0a 20 20 69 6e 74 20 72  Expr+2);.  int r
2409c 65 67 52 65 63 6f 72 64 20 3d 20 73 71 6c 69 74  egRecord = sqlit
2409d 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
2409e 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  rse);.  sqlite3E
2409f 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28  xprCodeExprList(
240a0 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42 79  pParse, pOrderBy
240a1 2c 20 72 65 67 42 61 73 65 2c 20 30 29 3b 0a 20  , regBase, 0);. 
240a2 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
240a3 70 32 28 76 2c 20 4f 50 5f 53 65 71 75 65 6e 63  p2(v, OP_Sequenc
240a4 65 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43  e, pOrderBy->iEC
240a5 75 72 73 6f 72 2c 20 72 65 67 42 61 73 65 2b 6e  ursor, regBase+n
240a6 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  Expr);.  sqlite3
240a7 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 70 50 61  ExprCodeMove(pPa
240a8 72 73 65 2c 20 72 65 67 44 61 74 61 2c 20 72 65  rse, regData, re
240a9 67 42 61 73 65 2b 6e 45 78 70 72 2b 31 2c 20 31  gBase+nExpr+1, 1
240aa 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
240ab 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp3(v, OP_Mak
240ac 65 52 65 63 6f 72 64 2c 20 72 65 67 42 61 73 65  eRecord, regBase
240ad 2c 20 6e 45 78 70 72 20 2b 20 32 2c 20 72 65 67  , nExpr + 2, reg
240ae 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c 69 74  Record);.  sqlit
240af 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
240b0 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 70 4f  OP_IdxInsert, pO
240b1 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72  rderBy->iECursor
240b2 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20  , regRecord);.  
240b3 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
240b4 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65  mpReg(pParse, re
240b5 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c 69  gRecord);.  sqli
240b6 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61  te3ReleaseTempRa
240b7 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 42  nge(pParse, regB
240b8 61 73 65 2c 20 6e 45 78 70 72 2b 32 29 3b 0a 20  ase, nExpr+2);. 
240b9 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 69 4c   if( pSelect->iL
240ba 69 6d 69 74 20 29 7b 0a 20 20 20 20 69 6e 74 20  imit ){.    int 
240bb 61 64 64 72 31 2c 20 61 64 64 72 32 3b 0a 20 20  addr1, addr2;.  
240bc 20 20 69 6e 74 20 69 4c 69 6d 69 74 3b 0a 20 20    int iLimit;.  
240bd 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 69    if( pSelect->i
240be 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 20 20  Offset ){.      
240bf 69 4c 69 6d 69 74 20 3d 20 70 53 65 6c 65 63 74  iLimit = pSelect
240c0 2d 3e 69 4f 66 66 73 65 74 2b 31 3b 0a 20 20 20  ->iOffset+1;.   
240c1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 4c   }else{.      iL
240c2 69 6d 69 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e  imit = pSelect->
240c3 69 4c 69 6d 69 74 3b 0a 20 20 20 20 7d 0a 20 20  iLimit;.    }.  
240c4 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65    addr1 = sqlite
240c5 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
240c6 50 5f 49 66 5a 65 72 6f 2c 20 69 4c 69 6d 69 74  P_IfZero, iLimit
240c7 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
240c8 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
240c9 64 64 49 6d 6d 2c 20 69 4c 69 6d 69 74 2c 20 2d  ddImm, iLimit, -
240ca 31 29 3b 0a 20 20 20 20 61 64 64 72 32 20 3d 20  1);.    addr2 = 
240cb 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
240cc 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20  0(v, OP_Goto);. 
240cd 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
240ce 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 31 29  mpHere(v, addr1)
240cf 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
240d0 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4c 61  eAddOp1(v, OP_La
240d1 73 74 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69 45  st, pOrderBy->iE
240d2 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 73 71 6c  Cursor);.    sql
240d3 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
240d4 2c 20 4f 50 5f 44 65 6c 65 74 65 2c 20 70 4f 72  , OP_Delete, pOr
240d5 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 29  derBy->iECursor)
240d6 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
240d7 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
240d8 72 32 29 3b 0a 20 20 20 20 70 53 65 6c 65 63 74  r2);.    pSelect
240d9 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20  ->iLimit = 0;.  
240da 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 63  }.}../*.** Add c
240db 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  ode to implement
240dc 20 74 68 65 20 4f 46 46 53 45 54 0a 2a 2f 0a 73   the OFFSET.*/.s
240dd 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 4f  tatic void codeO
240de 66 66 73 65 74 28 0a 20 20 56 64 62 65 20 2a 76  ffset(.  Vdbe *v
240df 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65  ,          /* Ge
240e0 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e 74 6f  nerate code into
240e1 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 53 65   this VM */.  Se
240e2 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20  lect *p,        
240e3 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74  /* The SELECT st
240e4 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f  atement being co
240e5 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f  ded */.  int iCo
240e6 6e 74 69 6e 75 65 20 20 20 20 20 2f 2a 20 4a 75  ntinue     /* Ju
240e7 6d 70 20 68 65 72 65 20 74 6f 20 73 6b 69 70 20  mp here to skip 
240e8 74 68 65 20 63 75 72 72 65 6e 74 20 72 65 63 6f  the current reco
240e9 72 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 70  rd */.){.  if( p
240ea 2d 3e 69 4f 66 66 73 65 74 20 26 26 20 69 43 6f  ->iOffset && iCo
240eb 6e 74 69 6e 75 65 21 3d 30 20 29 7b 0a 20 20 20  ntinue!=0 ){.   
240ec 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20 20 73   int addr;.    s
240ed 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
240ee 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 70  (v, OP_AddImm, p
240ef 2d 3e 69 4f 66 66 73 65 74 2c 20 2d 31 29 3b 0a  ->iOffset, -1);.
240f0 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
240f1 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
240f2 4f 50 5f 49 66 4e 65 67 2c 20 70 2d 3e 69 4f 66  OP_IfNeg, p->iOf
240f3 66 73 65 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  fset);.    sqlit
240f4 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
240f5 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 43 6f 6e  OP_Goto, 0, iCon
240f6 74 69 6e 75 65 29 3b 0a 20 20 20 20 56 64 62 65  tinue);.    Vdbe
240f7 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 73 6b 69  Comment((v, "ski
240f8 70 20 4f 46 46 53 45 54 20 72 65 63 6f 72 64 73  p OFFSET records
240f9 22 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  "));.    sqlite3
240fa 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
240fb 61 64 64 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  addr);.  }.}../*
240fc 0a 2a 2a 20 41 64 64 20 63 6f 64 65 20 74 68 61  .** Add code tha
240fd 74 20 77 69 6c 6c 20 63 68 65 63 6b 20 74 6f 20  t will check to 
240fe 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 4e 20  make sure the N 
240ff 72 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69  registers starti
24100 6e 67 20 61 74 20 69 4d 65 6d 0a 2a 2a 20 66 6f  ng at iMem.** fo
24101 72 6d 20 61 20 64 69 73 74 69 6e 63 74 20 65 6e  rm a distinct en
24102 74 72 79 2e 20 20 69 54 61 62 20 69 73 20 61 20  try.  iTab is a 
24103 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 74 68  sorting index th
24104 61 74 20 68 6f 6c 64 73 20 70 72 65 76 69 6f 75  at holds previou
24105 73 6c 79 0a 2a 2a 20 73 65 65 6e 20 63 6f 6d 62  sly.** seen comb
24106 69 6e 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20  inations of the 
24107 4e 20 76 61 6c 75 65 73 2e 20 20 41 20 6e 65 77  N values.  A new
24108 20 65 6e 74 72 79 20 69 73 20 6d 61 64 65 20 69   entry is made i
24109 6e 20 69 54 61 62 0a 2a 2a 20 69 66 20 74 68 65  n iTab.** if the
2410a 20 63 75 72 72 65 6e 74 20 4e 20 76 61 6c 75 65   current N value
2410b 73 20 61 72 65 20 6e 65 77 2e 0a 2a 2a 0a 2a 2a  s are new..**.**
2410c 20 41 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 52   A jump to addrR
2410d 65 70 65 61 74 20 69 73 20 6d 61 64 65 20 61 6e  epeat is made an
2410e 64 20 74 68 65 20 4e 2b 31 20 76 61 6c 75 65 73  d the N+1 values
2410f 20 61 72 65 20 70 6f 70 70 65 64 20 66 72 6f 6d   are popped from
24110 20 74 68 65 0a 2a 2a 20 73 74 61 63 6b 20 69 66   the.** stack if
24111 20 74 68 65 20 74 6f 70 20 4e 20 65 6c 65 6d 65   the top N eleme
24112 6e 74 73 20 61 72 65 20 6e 6f 74 20 64 69 73 74  nts are not dist
24113 69 6e 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  inct..*/.static 
24114 76 6f 69 64 20 63 6f 64 65 44 69 73 74 69 6e 63  void codeDistinc
24115 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  t(.  Parse *pPar
24116 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69  se,     /* Parsi
24117 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65  ng and code gene
24118 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  rating context *
24119 2f 0a 20 20 69 6e 74 20 69 54 61 62 2c 20 20 20  /.  int iTab,   
2411a 20 20 20 20 20 20 20 2f 2a 20 41 20 73 6f 72 74         /* A sort
2411b 69 6e 67 20 69 6e 64 65 78 20 75 73 65 64 20 74  ing index used t
2411c 6f 20 74 65 73 74 20 66 6f 72 20 64 69 73 74 69  o test for disti
2411d 6e 63 74 6e 65 73 73 20 2a 2f 0a 20 20 69 6e 74  nctness */.  int
2411e 20 61 64 64 72 52 65 70 65 61 74 2c 20 20 20 20   addrRepeat,    
2411f 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20  /* Jump to here 
24120 69 66 20 6e 6f 74 20 64 69 73 74 69 6e 63 74 20  if not distinct 
24121 2a 2f 0a 20 20 69 6e 74 20 4e 2c 20 20 20 20 20  */.  int N,     
24122 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
24123 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 2a 2f  r of elements */
24124 0a 20 20 69 6e 74 20 69 4d 65 6d 20 20 20 20 20  .  int iMem     
24125 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 65        /* First e
24126 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 56  lement */.){.  V
24127 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 72 31  dbe *v;.  int r1
24128 3b 0a 0a 20 20 76 20 3d 20 70 50 61 72 73 65 2d  ;..  v = pParse-
24129 3e 70 56 64 62 65 3b 0a 20 20 72 31 20 3d 20 73  >pVdbe;.  r1 = s
2412a 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
2412b 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69  (pParse);.  sqli
2412c 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2412d 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20   OP_MakeRecord, 
2412e 69 4d 65 6d 2c 20 4e 2c 20 72 31 29 3b 0a 20 20  iMem, N, r1);.  
2412f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24130 33 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 69  3(v, OP_Found, i
24131 54 61 62 2c 20 61 64 64 72 52 65 70 65 61 74 2c  Tab, addrRepeat,
24132 20 72 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56   r1);.  sqlite3V
24133 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
24134 49 64 78 49 6e 73 65 72 74 2c 20 69 54 61 62 2c  IdxInsert, iTab,
24135 20 72 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 52   r1);.  sqlite3R
24136 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
24137 61 72 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f 2a  arse, r1);.}../*
24138 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 20  .** Generate an 
24139 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 68  error message wh
2413a 65 6e 20 61 20 53 45 4c 45 43 54 20 69 73 20 75  en a SELECT is u
2413b 73 65 64 20 77 69 74 68 69 6e 20 61 20 73 75 62  sed within a sub
2413c 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 28 65  expression.** (e
2413d 78 61 6d 70 6c 65 3a 20 20 22 61 20 49 4e 20 28  xample:  "a IN (
2413e 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 61  SELECT * FROM ta
2413f 62 6c 65 29 22 29 20 62 75 74 20 69 74 20 68 61  ble)") but it ha
24140 73 20 6d 6f 72 65 20 74 68 61 6e 20 31 20 72 65  s more than 1 re
24141 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 20  sult.** column. 
24142 20 57 65 20 64 6f 20 74 68 69 73 20 69 6e 20 61   We do this in a
24143 20 73 75 62 72 6f 75 74 69 6e 65 20 62 65 63 61   subroutine beca
24144 75 73 65 20 74 68 65 20 65 72 72 6f 72 20 6f 63  use the error oc
24145 63 75 72 73 20 69 6e 20 6d 75 6c 74 69 70 6c 65  curs in multiple
24146 0a 2a 2a 20 70 6c 61 63 65 73 2e 0a 2a 2f 0a 73  .** places..*/.s
24147 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 46  tatic int checkF
24148 6f 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53 65 6c  orMultiColumnSel
24149 65 63 74 45 72 72 6f 72 28 0a 20 20 50 61 72 73  ectError(.  Pars
2414a 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2414b 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78   /* Parse contex
2414c 74 2e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65  t. */.  SelectDe
2414d 73 74 20 2a 70 44 65 73 74 2c 20 20 20 2f 2a 20  st *pDest,   /* 
2414e 44 65 73 74 69 6e 61 74 69 6f 6e 20 6f 66 20 53  Destination of S
2414f 45 4c 45 43 54 20 72 65 73 75 6c 74 73 20 2a 2f  ELECT results */
24150 0a 20 20 69 6e 74 20 6e 45 78 70 72 20 20 20 20  .  int nExpr    
24151 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
24152 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75  r of result colu
24153 6d 6e 73 20 72 65 74 75 72 6e 65 64 20 62 79 20  mns returned by 
24154 53 45 4c 45 43 54 20 2a 2f 0a 29 7b 0a 20 20 69  SELECT */.){.  i
24155 6e 74 20 65 44 65 73 74 20 3d 20 70 44 65 73 74  nt eDest = pDest
24156 2d 3e 65 44 65 73 74 3b 0a 20 20 69 66 28 20 6e  ->eDest;.  if( n
24157 45 78 70 72 3e 31 20 26 26 20 28 65 44 65 73 74  Expr>1 && (eDest
24158 3d 3d 53 52 54 5f 4d 65 6d 20 7c 7c 20 65 44 65  ==SRT_Mem || eDe
24159 73 74 3d 3d 53 52 54 5f 53 65 74 29 20 29 7b 0a  st==SRT_Set) ){.
2415a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2415b 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6f 6e 6c  Msg(pParse, "onl
2415c 79 20 61 20 73 69 6e 67 6c 65 20 72 65 73 75 6c  y a single resul
2415d 74 20 61 6c 6c 6f 77 65 64 20 66 6f 72 20 22 0a  t allowed for ".
2415e 20 20 20 20 20 20 20 22 61 20 53 45 4c 45 43 54         "a SELECT
2415f 20 74 68 61 74 20 69 73 20 70 61 72 74 20 6f 66   that is part of
24160 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 22 29   an expression")
24161 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
24162 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
24163 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  urn 0;.  }.}../*
24164 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
24165 20 67 65 6e 65 72 61 74 65 73 20 74 68 65 20 63   generates the c
24166 6f 64 65 20 66 6f 72 20 74 68 65 20 69 6e 73 69  ode for the insi
24167 64 65 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 20  de of the inner 
24168 6c 6f 6f 70 0a 2a 2a 20 6f 66 20 61 20 53 45 4c  loop.** of a SEL
24169 45 43 54 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 72  ECT..**.** If sr
2416a 63 54 61 62 20 61 6e 64 20 6e 43 6f 6c 75 6d 6e  cTab and nColumn
2416b 20 61 72 65 20 62 6f 74 68 20 7a 65 72 6f 2c 20   are both zero, 
2416c 74 68 65 6e 20 74 68 65 20 70 45 4c 69 73 74 20  then the pEList 
2416d 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 61  expressions.** a
2416e 72 65 20 65 76 61 6c 75 61 74 65 64 20 69 6e 20  re evaluated in 
2416f 6f 72 64 65 72 20 74 6f 20 67 65 74 20 74 68 65  order to get the
24170 20 64 61 74 61 20 66 6f 72 20 74 68 69 73 20 72   data for this r
24171 6f 77 2e 20 20 49 66 20 6e 43 6f 6c 75 6d 6e 3e  ow.  If nColumn>
24172 30 0a 2a 2a 20 74 68 65 6e 20 64 61 74 61 20 69  0.** then data i
24173 73 20 70 75 6c 6c 65 64 20 66 72 6f 6d 20 73 72  s pulled from sr
24174 63 54 61 62 20 61 6e 64 20 70 45 4c 69 73 74 20  cTab and pEList 
24175 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 74 6f 20  is used only to 
24176 67 65 74 20 74 68 65 0a 2a 2a 20 64 61 74 61 74  get the.** datat
24177 79 70 65 73 20 66 6f 72 20 65 61 63 68 20 63 6f  ypes for each co
24178 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lumn..*/.static 
24179 76 6f 69 64 20 73 65 6c 65 63 74 49 6e 6e 65 72  void selectInner
2417a 4c 6f 6f 70 28 0a 20 20 50 61 72 73 65 20 2a 70  Loop(.  Parse *p
2417b 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
2417c 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f  /* The parser co
2417d 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63  ntext */.  Selec
2417e 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  t *p,           
2417f 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65     /* The comple
24180 74 65 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d  te select statem
24181 65 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 64 20  ent being coded 
24182 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
24183 45 4c 69 73 74 2c 20 20 20 20 20 20 20 2f 2a 20  EList,       /* 
24184 4c 69 73 74 20 6f 66 20 76 61 6c 75 65 73 20 62  List of values b
24185 65 69 6e 67 20 65 78 74 72 61 63 74 65 64 20 2a  eing extracted *
24186 2f 0a 20 20 69 6e 74 20 73 72 63 54 61 62 2c 20  /.  int srcTab, 
24187 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
24188 75 6c 6c 20 64 61 74 61 20 66 72 6f 6d 20 74 68  ull data from th
24189 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  is table */.  in
2418a 74 20 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20  t nColumn,      
2418b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2418c 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
2418d 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a  e source table *
2418e 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f  /.  ExprList *pO
2418f 72 64 65 72 42 79 2c 20 20 20 20 20 2f 2a 20 49  rderBy,     /* I
24190 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 73 6f 72 74  f not NULL, sort
24191 20 72 65 73 75 6c 74 73 20 75 73 69 6e 67 20 74   results using t
24192 68 69 73 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74  his key */.  int
24193 20 64 69 73 74 69 6e 63 74 2c 20 20 20 20 20 20   distinct,      
24194 20 20 20 20 20 2f 2a 20 49 66 20 3e 3d 30 2c 20       /* If >=0, 
24195 6d 61 6b 65 20 73 75 72 65 20 72 65 73 75 6c 74  make sure result
24196 73 20 61 72 65 20 64 69 73 74 69 6e 63 74 20 2a  s are distinct *
24197 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a  /.  SelectDest *
24198 70 44 65 73 74 2c 20 20 20 20 20 20 2f 2a 20 48  pDest,      /* H
24199 6f 77 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66  ow to dispose of
2419a 20 74 68 65 20 72 65 73 75 6c 74 73 20 2a 2f 0a   the results */.
2419b 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65 2c    int iContinue,
2419c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d            /* Jum
2419d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e  p here to contin
2419e 75 65 20 77 69 74 68 20 6e 65 78 74 20 72 6f 77  ue with next row
2419f 20 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65 61 6b   */.  int iBreak
241a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
241a1 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 62 72   Jump here to br
241a2 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 69  eak out of the i
241a3 6e 6e 65 72 20 6c 6f 6f 70 20 2a 2f 0a 29 7b 0a  nner loop */.){.
241a4 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
241a5 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74  se->pVdbe;.  int
241a6 20 69 3b 0a 20 20 69 6e 74 20 68 61 73 44 69 73   i;.  int hasDis
241a7 74 69 6e 63 74 3b 20 20 20 20 20 20 20 20 2f 2a  tinct;        /*
241a8 20 54 72 75 65 20 69 66 20 74 68 65 20 44 49 53   True if the DIS
241a9 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 69 73  TINCT keyword is
241aa 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e   present */.  in
241ab 74 20 72 65 67 52 65 73 75 6c 74 3b 20 20 20 20  t regResult;    
241ac 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61            /* Sta
241ad 72 74 20 6f 66 20 6d 65 6d 6f 72 79 20 68 6f 6c  rt of memory hol
241ae 64 69 6e 67 20 72 65 73 75 6c 74 20 73 65 74 20  ding result set 
241af 2a 2f 0a 20 20 69 6e 74 20 65 44 65 73 74 20 3d  */.  int eDest =
241b0 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3b 20 20   pDest->eDest;  
241b1 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69 73 70 6f   /* How to dispo
241b2 73 65 20 6f 66 20 72 65 73 75 6c 74 73 20 2a 2f  se of results */
241b3 0a 20 20 69 6e 74 20 69 50 61 72 6d 20 3d 20 70  .  int iParm = p
241b4 44 65 73 74 2d 3e 69 50 61 72 6d 3b 20 20 20 2f  Dest->iParm;   /
241b5 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74  * First argument
241b6 20 74 6f 20 64 69 73 70 6f 73 61 6c 20 6d 65 74   to disposal met
241b7 68 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65  hod */.  int nRe
241b8 73 75 6c 74 43 6f 6c 3b 20 20 20 20 20 20 20 20  sultCol;        
241b9 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
241ba 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73  f result columns
241bb 20 2a 2f 0a 0a 20 20 69 66 28 20 76 3d 3d 30 20   */..  if( v==0 
241bc 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65  ) return;.  asse
241bd 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b  rt( pEList!=0 );
241be 0a 20 20 68 61 73 44 69 73 74 69 6e 63 74 20 3d  .  hasDistinct =
241bf 20 64 69 73 74 69 6e 63 74 3e 3d 30 3b 0a 20 20   distinct>=0;.  
241c0 69 66 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20  if( pOrderBy==0 
241c1 26 26 20 21 68 61 73 44 69 73 74 69 6e 63 74 20  && !hasDistinct 
241c2 29 7b 0a 20 20 20 20 63 6f 64 65 4f 66 66 73 65  ){.    codeOffse
241c3 74 28 76 2c 20 70 2c 20 69 43 6f 6e 74 69 6e 75  t(v, p, iContinu
241c4 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50 75  e);.  }..  /* Pu
241c5 6c 6c 20 74 68 65 20 72 65 71 75 65 73 74 65 64  ll the requested
241c6 20 63 6f 6c 75 6d 6e 73 2e 0a 20 20 2a 2f 0a 20   columns..  */. 
241c7 20 69 66 28 20 6e 43 6f 6c 75 6d 6e 3e 30 20 29   if( nColumn>0 )
241c8 7b 0a 20 20 20 20 6e 52 65 73 75 6c 74 43 6f 6c  {.    nResultCol
241c9 20 3d 20 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 7d 65   = nColumn;.  }e
241ca 6c 73 65 7b 0a 20 20 20 20 6e 52 65 73 75 6c 74  lse{.    nResult
241cb 43 6f 6c 20 3d 20 70 45 4c 69 73 74 2d 3e 6e 45  Col = pEList->nE
241cc 78 70 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  xpr;.  }.  if( p
241cd 44 65 73 74 2d 3e 69 4d 65 6d 3d 3d 30 20 29 7b  Dest->iMem==0 ){
241ce 0a 20 20 20 20 70 44 65 73 74 2d 3e 69 4d 65 6d  .    pDest->iMem
241cf 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b   = pParse->nMem+
241d0 31 3b 0a 20 20 20 20 70 44 65 73 74 2d 3e 6e 4d  1;.    pDest->nM
241d1 65 6d 20 3d 20 6e 52 65 73 75 6c 74 43 6f 6c 3b  em = nResultCol;
241d2 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65  .    pParse->nMe
241d3 6d 20 2b 3d 20 6e 52 65 73 75 6c 74 43 6f 6c 3b  m += nResultCol;
241d4 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 44 65  .  }else if( pDe
241d5 73 74 2d 3e 6e 4d 65 6d 21 3d 6e 52 65 73 75 6c  st->nMem!=nResul
241d6 74 43 6f 6c 20 29 7b 0a 20 20 20 20 2f 2a 20 54  tCol ){.    /* T
241d7 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e  his happens when
241d8 20 74 77 6f 20 53 45 4c 45 43 54 73 20 6f 66 20   two SELECTs of 
241d9 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43  a compound SELEC
241da 54 20 68 61 76 65 20 64 69 66 66 65 72 69 6e 67  T have differing
241db 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 73 20  .    ** numbers 
241dc 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  of result column
241dd 73 2e 20 20 54 68 65 20 65 72 72 6f 72 20 6d 65  s.  The error me
241de 73 73 61 67 65 20 77 69 6c 6c 20 62 65 20 67 65  ssage will be ge
241df 6e 65 72 61 74 65 64 20 62 79 0a 20 20 20 20 2a  nerated by.    *
241e0 2a 20 61 20 68 69 67 68 65 72 2d 6c 65 76 65 6c  * a higher-level
241e1 20 72 6f 75 74 69 6e 65 2e 20 2a 2f 0a 20 20 20   routine. */.   
241e2 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 72   return;.  }.  r
241e3 65 67 52 65 73 75 6c 74 20 3d 20 70 44 65 73 74  egResult = pDest
241e4 2d 3e 69 4d 65 6d 3b 0a 20 20 69 66 28 20 6e 43  ->iMem;.  if( nC
241e5 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20 20 66  olumn>0 ){.    f
241e6 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75 6d  or(i=0; i<nColum
241e7 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  n; i++){.      s
241e8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
241e9 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73  (v, OP_Column, s
241ea 72 63 54 61 62 2c 20 69 2c 20 72 65 67 52 65 73  rcTab, i, regRes
241eb 75 6c 74 2b 69 29 3b 0a 20 20 20 20 7d 0a 20 20  ult+i);.    }.  
241ec 7d 65 6c 73 65 20 69 66 28 20 65 44 65 73 74 21  }else if( eDest!
241ed 3d 53 52 54 5f 45 78 69 73 74 73 20 29 7b 0a 20  =SRT_Exists ){. 
241ee 20 20 20 2f 2a 20 49 66 20 74 68 65 20 64 65 73     /* If the des
241ef 74 69 6e 61 74 69 6f 6e 20 69 73 20 61 6e 20 45  tination is an E
241f0 58 49 53 54 53 28 2e 2e 2e 29 20 65 78 70 72 65  XISTS(...) expre
241f1 73 73 69 6f 6e 2c 20 74 68 65 20 61 63 74 75 61  ssion, the actua
241f2 6c 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 73 20  l.    ** values 
241f3 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20  returned by the 
241f4 53 45 4c 45 43 54 20 61 72 65 20 6e 6f 74 20 72  SELECT are not r
241f5 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a  equired..    */.
241f6 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
241f7 6f 64 65 45 78 70 72 4c 69 73 74 28 70 50 61 72  odeExprList(pPar
241f8 73 65 2c 20 70 45 4c 69 73 74 2c 20 72 65 67 52  se, pEList, regR
241f9 65 73 75 6c 74 2c 20 65 44 65 73 74 3d 3d 53 52  esult, eDest==SR
241fa 54 5f 4f 75 74 70 75 74 29 3b 0a 20 20 7d 0a 20  T_Output);.  }. 
241fb 20 6e 43 6f 6c 75 6d 6e 20 3d 20 6e 52 65 73 75   nColumn = nResu
241fc 6c 74 43 6f 6c 3b 0a 0a 20 20 2f 2a 20 49 66 20  ltCol;..  /* If 
241fd 74 68 65 20 44 49 53 54 49 4e 43 54 20 6b 65 79  the DISTINCT key
241fe 77 6f 72 64 20 77 61 73 20 70 72 65 73 65 6e 74  word was present
241ff 20 6f 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73   on the SELECT s
24200 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 61 6e  tatement.  ** an
24201 64 20 74 68 69 73 20 72 6f 77 20 68 61 73 20 62  d this row has b
24202 65 65 6e 20 73 65 65 6e 20 62 65 66 6f 72 65 2c  een seen before,
24203 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 6d 61 6b   then do not mak
24204 65 20 74 68 69 73 20 72 6f 77 0a 20 20 2a 2a 20  e this row.  ** 
24205 70 61 72 74 20 6f 66 20 74 68 65 20 72 65 73 75  part of the resu
24206 6c 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 68  lt..  */.  if( h
24207 61 73 44 69 73 74 69 6e 63 74 20 29 7b 0a 20 20  asDistinct ){.  
24208 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74    assert( pEList
24209 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  !=0 );.    asser
2420a 74 28 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  t( pEList->nExpr
2420b 3d 3d 6e 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 20  ==nColumn );.   
2420c 20 63 6f 64 65 44 69 73 74 69 6e 63 74 28 70 50   codeDistinct(pP
2420d 61 72 73 65 2c 20 64 69 73 74 69 6e 63 74 2c 20  arse, distinct, 
2420e 69 43 6f 6e 74 69 6e 75 65 2c 20 6e 43 6f 6c 75  iContinue, nColu
2420f 6d 6e 2c 20 72 65 67 52 65 73 75 6c 74 29 3b 0a  mn, regResult);.
24210 20 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79      if( pOrderBy
24211 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6f 64  ==0 ){.      cod
24212 65 4f 66 66 73 65 74 28 76 2c 20 70 2c 20 69 43  eOffset(v, p, iC
24213 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 20 20 7d 0a  ontinue);.    }.
24214 20 20 7d 0a 0a 20 20 69 66 28 20 63 68 65 63 6b    }..  if( check
24215 46 6f 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53 65  ForMultiColumnSe
24216 6c 65 63 74 45 72 72 6f 72 28 70 50 61 72 73 65  lectError(pParse
24217 2c 20 70 44 65 73 74 2c 20 70 45 4c 69 73 74 2d  , pDest, pEList-
24218 3e 6e 45 78 70 72 29 20 29 7b 0a 20 20 20 20 72  >nExpr) ){.    r
24219 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 73 77  eturn;.  }..  sw
2421a 69 74 63 68 28 20 65 44 65 73 74 20 29 7b 0a 20  itch( eDest ){. 
2421b 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 6d 6f     /* In this mo
2421c 64 65 2c 20 77 72 69 74 65 20 65 61 63 68 20 71  de, write each q
2421d 75 65 72 79 20 72 65 73 75 6c 74 20 74 6f 20 74  uery result to t
2421e 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 74 65  he key of the te
2421f 6d 70 6f 72 61 72 79 0a 20 20 20 20 2a 2a 20 74  mporary.    ** t
24220 61 62 6c 65 20 69 50 61 72 6d 2e 0a 20 20 20 20  able iParm..    
24221 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
24222 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f  E_OMIT_COMPOUND_
24223 53 45 4c 45 43 54 0a 20 20 20 20 63 61 73 65 20  SELECT.    case 
24224 53 52 54 5f 55 6e 69 6f 6e 3a 20 7b 0a 20 20 20  SRT_Union: {.   
24225 20 20 20 69 6e 74 20 72 31 3b 0a 20 20 20 20 20     int r1;.     
24226 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r1 = sqlite3Get
24227 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
24228 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
24229 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
2422a 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 52 65  akeRecord, regRe
2422b 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72  sult, nColumn, r
2422c 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
2422d 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2422e 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 50 61  P_IdxInsert, iPa
2422f 72 6d 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73  rm, r1);.      s
24230 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
24231 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29  pReg(pParse, r1)
24232 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
24233 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 6e     }..    /* Con
24234 73 74 72 75 63 74 20 61 20 72 65 63 6f 72 64 20  struct a record 
24235 66 72 6f 6d 20 74 68 65 20 71 75 65 72 79 20 72  from the query r
24236 65 73 75 6c 74 2c 20 62 75 74 20 69 6e 73 74 65  esult, but inste
24237 61 64 20 6f 66 0a 20 20 20 20 2a 2a 20 73 61 76  ad of.    ** sav
24238 69 6e 67 20 74 68 61 74 20 72 65 63 6f 72 64 2c  ing that record,
24239 20 75 73 65 20 69 74 20 61 73 20 61 20 6b 65 79   use it as a key
2423a 20 74 6f 20 64 65 6c 65 74 65 20 65 6c 65 6d 65   to delete eleme
2423b 6e 74 73 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20  nts from.    ** 
2423c 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61  the temporary ta
2423d 62 6c 65 20 69 50 61 72 6d 2e 0a 20 20 20 20 2a  ble iParm..    *
2423e 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 45  /.    case SRT_E
2423f 78 63 65 70 74 3a 20 7b 0a 20 20 20 20 20 20 73  xcept: {.      s
24240 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
24241 28 76 2c 20 4f 50 5f 49 64 78 44 65 6c 65 74 65  (v, OP_IdxDelete
24242 2c 20 69 50 61 72 6d 2c 20 72 65 67 52 65 73 75  , iParm, regResu
24243 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20  lt, nColumn);.  
24244 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
24245 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20  .#endif..    /* 
24246 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  Store the result
24247 20 61 73 20 64 61 74 61 20 75 73 69 6e 67 20 61   as data using a
24248 20 75 6e 69 71 75 65 20 6b 65 79 2e 0a 20 20 20   unique key..   
24249 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54   */.    case SRT
2424a 5f 54 61 62 6c 65 3a 0a 20 20 20 20 63 61 73 65  _Table:.    case
2424b 20 53 52 54 5f 45 70 68 65 6d 54 61 62 3a 20 7b   SRT_EphemTab: {
2424c 0a 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d 20  .      int r1 = 
2424d 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
2424e 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  g(pParse);.     
2424f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24250 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63  p3(v, OP_MakeRec
24251 6f 72 64 2c 20 72 65 67 52 65 73 75 6c 74 2c 20  ord, regResult, 
24252 6e 43 6f 6c 75 6d 6e 2c 20 72 31 29 3b 0a 20 20  nColumn, r1);.  
24253 20 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79      if( pOrderBy
24254 20 29 7b 0a 20 20 20 20 20 20 20 20 70 75 73 68   ){.        push
24255 4f 6e 74 6f 53 6f 72 74 65 72 28 70 50 61 72 73  OntoSorter(pPars
24256 65 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 2c 20  e, pOrderBy, p, 
24257 72 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  r1);.      }else
24258 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 32  {.        int r2
24259 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
2425a 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
2425b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2425c 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
2425d 77 52 6f 77 69 64 2c 20 69 50 61 72 6d 2c 20 72  wRowid, iParm, r
2425e 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  2);.        sqli
2425f 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
24260 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69 50 61 72   OP_Insert, iPar
24261 6d 2c 20 72 31 2c 20 72 32 29 3b 0a 20 20 20 20  m, r1, r2);.    
24262 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
24263 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41  hangeP5(v, OPFLA
24264 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 20  G_APPEND);.     
24265 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
24266 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
24267 20 72 32 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20   r2);.      }.  
24268 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
24269 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
2426a 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 62 72 65  , r1);.      bre
2426b 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64  ak;.    }..#ifnd
2426c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
2426d 55 42 51 55 45 52 59 0a 20 20 20 20 2f 2a 20 49  UBQUERY.    /* I
2426e 66 20 77 65 20 61 72 65 20 63 72 65 61 74 69 6e  f we are creatin
2426f 67 20 61 20 73 65 74 20 66 6f 72 20 61 6e 20 22  g a set for an "
24270 65 78 70 72 20 49 4e 20 28 53 45 4c 45 43 54 20  expr IN (SELECT 
24271 2e 2e 2e 29 22 20 63 6f 6e 73 74 72 75 63 74 2c  ...)" construct,
24272 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65  .    ** then the
24273 72 65 20 73 68 6f 75 6c 64 20 62 65 20 61 20 73  re should be a s
24274 69 6e 67 6c 65 20 69 74 65 6d 20 6f 6e 20 74 68  ingle item on th
24275 65 20 73 74 61 63 6b 2e 20 20 57 72 69 74 65 20  e stack.  Write 
24276 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 74 65 6d  this.    ** item
24277 20 69 6e 74 6f 20 74 68 65 20 73 65 74 20 74 61   into the set ta
24278 62 6c 65 20 77 69 74 68 20 62 6f 67 75 73 20 64  ble with bogus d
24279 61 74 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ata..    */.    
2427a 63 61 73 65 20 53 52 54 5f 53 65 74 3a 20 7b 0a  case SRT_Set: {.
2427b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 43        assert( nC
2427c 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20  olumn==1 );.    
2427d 20 20 70 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20    p->affinity = 
2427e 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66  sqlite3CompareAf
2427f 66 69 6e 69 74 79 28 70 45 4c 69 73 74 2d 3e 61  finity(pEList->a
24280 5b 30 5d 2e 70 45 78 70 72 2c 20 70 44 65 73 74  [0].pExpr, pDest
24281 2d 3e 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 20  ->affinity);.   
24282 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20     if( pOrderBy 
24283 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 74  ){.        /* At
24284 20 66 69 72 73 74 20 67 6c 61 6e 63 65 20 79 6f   first glance yo
24285 75 20 77 6f 75 6c 64 20 74 68 69 6e 6b 20 77 65  u would think we
24286 20 63 6f 75 6c 64 20 6f 70 74 69 6d 69 7a 65 20   could optimize 
24287 6f 75 74 20 74 68 65 0a 20 20 20 20 20 20 20 20  out the.        
24288 2a 2a 20 4f 52 44 45 52 20 42 59 20 69 6e 20 74  ** ORDER BY in t
24289 68 69 73 20 63 61 73 65 20 73 69 6e 63 65 20 74  his case since t
2428a 68 65 20 6f 72 64 65 72 20 6f 66 20 65 6e 74 72  he order of entr
2428b 69 65 73 20 69 6e 20 74 68 65 20 73 65 74 0a 20  ies in the set. 
2428c 20 20 20 20 20 20 20 2a 2a 20 64 6f 65 73 20 6e         ** does n
2428d 6f 74 20 6d 61 74 74 65 72 2e 20 20 42 75 74 20  ot matter.  But 
2428e 74 68 65 72 65 20 6d 69 67 68 74 20 62 65 20 61  there might be a
2428f 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2c 20 69   LIMIT clause, i
24290 6e 20 77 68 69 63 68 0a 20 20 20 20 20 20 20 20  n which.        
24291 2a 2a 20 63 61 73 65 20 74 68 65 20 6f 72 64 65  ** case the orde
24292 72 20 64 6f 65 73 20 6d 61 74 74 65 72 20 2a 2f  r does matter */
24293 0a 20 20 20 20 20 20 20 20 70 75 73 68 4f 6e 74  .        pushOnt
24294 6f 53 6f 72 74 65 72 28 70 50 61 72 73 65 2c 20  oSorter(pParse, 
24295 70 4f 72 64 65 72 42 79 2c 20 70 2c 20 72 65 67  pOrderBy, p, reg
24296 52 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 7d  Result);.      }
24297 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e  else{.        in
24298 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65  t r1 = sqlite3Ge
24299 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
2429a 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2429b 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
2429c 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65  P_MakeRecord, re
2429d 67 52 65 73 75 6c 74 2c 20 31 2c 20 72 31 2c 20  gResult, 1, r1, 
2429e 26 70 2d 3e 61 66 66 69 6e 69 74 79 2c 20 31 29  &p->affinity, 1)
2429f 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
242a0 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69  3ExprCacheAffini
242a1 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c  tyChange(pParse,
242a2 20 72 65 67 52 65 73 75 6c 74 2c 20 31 29 3b 0a   regResult, 1);.
242a3 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
242a4 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
242a5 49 64 78 49 6e 73 65 72 74 2c 20 69 50 61 72 6d  IdxInsert, iParm
242a6 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 73  , r1);.        s
242a7 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
242a8 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29  pReg(pParse, r1)
242a9 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
242aa 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
242ab 20 20 2f 2a 20 49 66 20 61 6e 79 20 72 6f 77 20    /* If any row 
242ac 65 78 69 73 74 20 69 6e 20 74 68 65 20 72 65 73  exist in the res
242ad 75 6c 74 20 73 65 74 2c 20 72 65 63 6f 72 64 20  ult set, record 
242ae 74 68 61 74 20 66 61 63 74 20 61 6e 64 20 61 62  that fact and ab
242af 6f 72 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ort..    */.    
242b0 63 61 73 65 20 53 52 54 5f 45 78 69 73 74 73 3a  case SRT_Exists:
242b1 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
242b2 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
242b3 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 50 61  _Integer, 1, iPa
242b4 72 6d 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68  rm);.      /* Th
242b5 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 77  e LIMIT clause w
242b6 69 6c 6c 20 74 65 72 6d 69 6e 61 74 65 20 74 68  ill terminate th
242b7 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f  e loop for us */
242b8 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
242b9 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74    }..    /* If t
242ba 68 69 73 20 69 73 20 61 20 73 63 61 6c 61 72 20  his is a scalar 
242bb 73 65 6c 65 63 74 20 74 68 61 74 20 69 73 20 70  select that is p
242bc 61 72 74 20 6f 66 20 61 6e 20 65 78 70 72 65 73  art of an expres
242bd 73 69 6f 6e 2c 20 74 68 65 6e 0a 20 20 20 20 2a  sion, then.    *
242be 2a 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75  * store the resu
242bf 6c 74 73 20 69 6e 20 74 68 65 20 61 70 70 72 6f  lts in the appro
242c0 70 72 69 61 74 65 20 6d 65 6d 6f 72 79 20 63 65  priate memory ce
242c1 6c 6c 20 61 6e 64 20 62 72 65 61 6b 20 6f 75 74  ll and break out
242c2 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 73  .    ** of the s
242c3 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f  can loop..    */
242c4 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 4d 65  .    case SRT_Me
242c5 6d 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72  m: {.      asser
242c6 74 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b  t( nColumn==1 );
242c7 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72 64 65  .      if( pOrde
242c8 72 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20 70  rBy ){.        p
242c9 75 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 70 50  ushOntoSorter(pP
242ca 61 72 73 65 2c 20 70 4f 72 64 65 72 42 79 2c 20  arse, pOrderBy, 
242cb 70 2c 20 72 65 67 52 65 73 75 6c 74 29 3b 0a 20  p, regResult);. 
242cc 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
242cd 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
242ce 6f 64 65 4d 6f 76 65 28 70 50 61 72 73 65 2c 20  odeMove(pParse, 
242cf 72 65 67 52 65 73 75 6c 74 2c 20 69 50 61 72 6d  regResult, iParm
242d0 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a  , 1);.        /*
242d1 20 54 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73   The LIMIT claus
242d2 65 20 77 69 6c 6c 20 6a 75 6d 70 20 6f 75 74 20  e will jump out 
242d3 6f 66 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20  of the loop for 
242d4 75 73 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20  us */.      }.  
242d5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
242d6 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64  .#endif /* #ifnd
242d7 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
242d8 55 42 51 55 45 52 59 20 2a 2f 0a 0a 20 20 20 20  UBQUERY */..    
242d9 2f 2a 20 53 65 6e 64 20 74 68 65 20 64 61 74 61  /* Send the data
242da 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   to the callback
242db 20 66 75 6e 63 74 69 6f 6e 20 6f 72 20 74 6f 20   function or to 
242dc 61 20 73 75 62 72 6f 75 74 69 6e 65 2e 20 20 49  a subroutine.  I
242dd 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 73  n the.    ** cas
242de 65 20 6f 66 20 61 20 73 75 62 72 6f 75 74 69 6e  e of a subroutin
242df 65 2c 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e  e, the subroutin
242e0 65 20 69 74 73 65 6c 66 20 69 73 20 72 65 73 70  e itself is resp
242e1 6f 6e 73 69 62 6c 65 20 66 6f 72 0a 20 20 20 20  onsible for.    
242e2 2a 2a 20 70 6f 70 70 69 6e 67 20 74 68 65 20 64  ** popping the d
242e3 61 74 61 20 66 72 6f 6d 20 74 68 65 20 73 74 61  ata from the sta
242e4 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  ck..    */.    c
242e5 61 73 65 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e  ase SRT_Coroutin
242e6 65 3a 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  e:.    case SRT_
242e7 4f 75 74 70 75 74 3a 20 7b 0a 20 20 20 20 20 20  Output: {.      
242e8 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a  if( pOrderBy ){.
242e9 20 20 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d          int r1 =
242ea 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
242eb 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
242ec 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
242ed 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65  ddOp3(v, OP_Make
242ee 52 65 63 6f 72 64 2c 20 72 65 67 52 65 73 75 6c  Record, regResul
242ef 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 31 29 3b  t, nColumn, r1);
242f0 0a 20 20 20 20 20 20 20 20 70 75 73 68 4f 6e 74  .        pushOnt
242f1 6f 53 6f 72 74 65 72 28 70 50 61 72 73 65 2c 20  oSorter(pParse, 
242f2 70 4f 72 64 65 72 42 79 2c 20 70 2c 20 72 31 29  pOrderBy, p, r1)
242f3 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
242f4 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
242f5 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20  pParse, r1);.   
242f6 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65 44 65     }else if( eDe
242f7 73 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69 6e  st==SRT_Coroutin
242f8 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  e ){.        sql
242f9 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
242fa 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 70 44 65 73  , OP_Yield, pDes
242fb 74 2d 3e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20  t->iParm);.     
242fc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
242fd 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
242fe 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f  2(v, OP_ResultRo
242ff 77 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 6e 43  w, regResult, nC
24300 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 20 20  olumn);.        
24301 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
24302 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70  AffinityChange(p
24303 50 61 72 73 65 2c 20 72 65 67 52 65 73 75 6c 74  Parse, regResult
24304 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  , nColumn);.    
24305 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
24306 0a 20 20 20 20 7d 0a 0a 23 69 66 20 21 64 65 66  .    }..#if !def
24307 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
24308 5f 54 52 49 47 47 45 52 29 0a 20 20 20 20 2f 2a  _TRIGGER).    /*
24309 20 44 69 73 63 61 72 64 20 74 68 65 20 72 65 73   Discard the res
2430a 75 6c 74 73 2e 20 20 54 68 69 73 20 69 73 20 75  ults.  This is u
2430b 73 65 64 20 66 6f 72 20 53 45 4c 45 43 54 20 73  sed for SELECT s
2430c 74 61 74 65 6d 65 6e 74 73 20 69 6e 73 69 64 65  tatements inside
2430d 0a 20 20 20 20 2a 2a 20 74 68 65 20 62 6f 64 79  .    ** the body
2430e 20 6f 66 20 61 20 54 52 49 47 47 45 52 2e 20 20   of a TRIGGER.  
2430f 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 73  The purpose of s
24310 75 63 68 20 73 65 6c 65 63 74 73 20 69 73 20 74  uch selects is t
24311 6f 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 75 73  o call.    ** us
24312 65 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  er-defined funct
24313 69 6f 6e 73 20 74 68 61 74 20 68 61 76 65 20 73  ions that have s
24314 69 64 65 20 65 66 66 65 63 74 73 2e 20 20 57 65  ide effects.  We
24315 20 64 6f 20 6e 6f 74 20 63 61 72 65 0a 20 20 20   do not care.   
24316 20 2a 2a 20 61 62 6f 75 74 20 74 68 65 20 61 63   ** about the ac
24317 74 75 61 6c 20 72 65 73 75 6c 74 73 20 6f 66 20  tual results of 
24318 74 68 65 20 73 65 6c 65 63 74 2e 0a 20 20 20 20  the select..    
24319 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  */.    default: 
2431a 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
2431b 65 44 65 73 74 3d 3d 53 52 54 5f 44 69 73 63 61  eDest==SRT_Disca
2431c 72 64 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61  rd );.      brea
2431d 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  k;.    }.#endif.
2431e 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74    }..  /* Jump t
2431f 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  o the end of the
24320 20 6c 6f 6f 70 20 69 66 20 74 68 65 20 4c 49 4d   loop if the LIM
24321 49 54 20 69 73 20 72 65 61 63 68 65 64 2e 0a 20  IT is reached.. 
24322 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 4c 69   */.  if( p->iLi
24323 6d 69 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72  mit ){.    asser
24324 74 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29  t( pOrderBy==0 )
24325 3b 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69  ;  /* If there i
24326 73 20 61 6e 20 4f 52 44 45 52 20 42 59 2c 20 74  s an ORDER BY, t
24327 68 65 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 20  he call to.     
24328 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24329 20 20 20 20 20 20 20 2a 2a 20 70 75 73 68 4f 6e         ** pushOn
2432a 74 6f 53 6f 72 74 65 72 28 29 20 77 6f 75 6c 64  toSorter() would
2432b 20 68 61 76 65 20 63 6c 65 61 72 65 64 20 70 2d   have cleared p-
2432c 3e 69 4c 69 6d 69 74 20 2a 2f 0a 20 20 20 20 73  >iLimit */.    s
2432d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2432e 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 70  (v, OP_AddImm, p
2432f 2d 3e 69 4c 69 6d 69 74 2c 20 2d 31 29 3b 0a 20  ->iLimit, -1);. 
24330 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
24331 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 5a 65 72  dOp2(v, OP_IfZer
24332 6f 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c 20 69 42  o, p->iLimit, iB
24333 72 65 61 6b 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  reak);.  }.}../*
24334 0a 2a 2a 20 47 69 76 65 6e 20 61 6e 20 65 78 70  .** Given an exp
24335 72 65 73 73 69 6f 6e 20 6c 69 73 74 2c 20 67 65  ression list, ge
24336 6e 65 72 61 74 65 20 61 20 4b 65 79 49 6e 66 6f  nerate a KeyInfo
24337 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20   structure that 
24338 72 65 63 6f 72 64 73 0a 2a 2a 20 74 68 65 20 63  records.** the c
24339 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
2433a 65 20 66 6f 72 20 65 61 63 68 20 65 78 70 72 65  e for each expre
2433b 73 73 69 6f 6e 20 69 6e 20 74 68 61 74 20 65 78  ssion in that ex
2433c 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a  pression list..*
2433d 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 78 70 72  *.** If the Expr
2433e 4c 69 73 74 20 69 73 20 61 6e 20 4f 52 44 45 52  List is an ORDER
2433f 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20   BY or GROUP BY 
24340 63 6c 61 75 73 65 20 74 68 65 6e 20 74 68 65 20  clause then the 
24341 72 65 73 75 6c 74 69 6e 67 0a 2a 2a 20 4b 65 79  resulting.** Key
24342 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 69  Info structure i
24343 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f  s appropriate fo
24344 72 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 61  r initializing a
24345 20 76 69 72 74 75 61 6c 20 69 6e 64 65 78 20 74   virtual index t
24346 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 74  o.** implement t
24347 68 61 74 20 63 6c 61 75 73 65 2e 20 20 49 66 20  hat clause.  If 
24348 74 68 65 20 45 78 70 72 4c 69 73 74 20 69 73 20  the ExprList is 
24349 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f  the result set o
2434a 66 20 61 20 53 45 4c 45 43 54 0a 2a 2a 20 74 68  f a SELECT.** th
2434b 65 6e 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73  en the KeyInfo s
2434c 74 72 75 63 74 75 72 65 20 69 73 20 61 70 70 72  tructure is appr
2434d 6f 70 72 69 61 74 65 20 66 6f 72 20 69 6e 69 74  opriate for init
2434e 69 61 6c 69 7a 69 6e 67 20 61 20 76 69 72 74 75  ializing a virtu
2434f 61 6c 0a 2a 2a 20 69 6e 64 65 78 20 74 6f 20 69  al.** index to i
24350 6d 70 6c 65 6d 65 6e 74 20 61 20 44 49 53 54 49  mplement a DISTI
24351 4e 43 54 20 74 65 73 74 2e 0a 2a 2a 0a 2a 2a 20  NCT test..**.** 
24352 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68  Space to hold th
24353 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74  e KeyInfo struct
24354 75 72 65 20 69 73 20 6f 62 74 61 69 6e 20 66 72  ure is obtain fr
24355 6f 6d 20 6d 61 6c 6c 6f 63 2e 20 20 54 68 65 20  om malloc.  The 
24356 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74  calling.** funct
24357 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73 69 62  ion is responsib
24358 6c 65 20 66 6f 72 20 73 65 65 69 6e 67 20 74 68  le for seeing th
24359 61 74 20 74 68 69 73 20 73 74 72 75 63 74 75 72  at this structur
2435a 65 20 69 73 20 65 76 65 6e 74 75 61 6c 6c 79 0a  e is eventually.
2435b 2a 2a 20 66 72 65 65 64 2e 20 20 41 64 64 20 74  ** freed.  Add t
2435c 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63  he KeyInfo struc
2435d 74 75 72 65 20 74 6f 20 74 68 65 20 50 34 20 66  ture to the P4 f
2435e 69 65 6c 64 20 6f 66 20 61 6e 20 6f 70 63 6f 64  ield of an opcod
2435f 65 20 75 73 69 6e 67 0a 2a 2a 20 50 34 5f 4b 45  e using.** P4_KE
24360 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 69 73  YINFO_HANDOFF is
24361 20 74 68 65 20 75 73 75 61 6c 20 77 61 79 20 6f   the usual way o
24362 66 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 74  f dealing with t
24363 68 69 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4b  his..*/.static K
24364 65 79 49 6e 66 6f 20 2a 6b 65 79 49 6e 66 6f 46  eyInfo *keyInfoF
24365 72 6f 6d 45 78 70 72 4c 69 73 74 28 50 61 72 73  romExprList(Pars
24366 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 4c  e *pParse, ExprL
24367 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 73  ist *pList){.  s
24368 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
24369 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 6e  rse->db;.  int n
2436a 45 78 70 72 3b 0a 20 20 4b 65 79 49 6e 66 6f 20  Expr;.  KeyInfo 
2436b 2a 70 49 6e 66 6f 3b 0a 20 20 73 74 72 75 63 74  *pInfo;.  struct
2436c 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
2436d 70 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a  pItem;.  int i;.
2436e 0a 20 20 6e 45 78 70 72 20 3d 20 70 4c 69 73 74  .  nExpr = pList
2436f 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 49 6e 66 6f  ->nExpr;.  pInfo
24370 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
24371 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
24372 66 28 2a 70 49 6e 66 6f 29 20 2b 20 6e 45 78 70  f(*pInfo) + nExp
24373 72 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65  r*(sizeof(CollSe
24374 71 2a 29 2b 31 29 20 29 3b 0a 20 20 69 66 28 20  q*)+1) );.  if( 
24375 70 49 6e 66 6f 20 29 7b 0a 20 20 20 20 70 49 6e  pInfo ){.    pIn
24376 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d  fo->aSortOrder =
24377 20 28 75 38 2a 29 26 70 49 6e 66 6f 2d 3e 61 43   (u8*)&pInfo->aC
24378 6f 6c 6c 5b 6e 45 78 70 72 5d 3b 0a 20 20 20 20  oll[nExpr];.    
24379 70 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 3d 20  pInfo->nField = 
2437a 6e 45 78 70 72 3b 0a 20 20 20 20 70 49 6e 66 6f  nExpr;.    pInfo
2437b 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b  ->enc = ENC(db);
2437c 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49  .    for(i=0, pI
2437d 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c  tem=pList->a; i<
2437e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65  nExpr; i++, pIte
2437f 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c  m++){.      Coll
24380 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20  Seq *pColl;.    
24381 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
24382 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61  3ExprCollSeq(pPa
24383 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70  rse, pItem->pExp
24384 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70  r);.      if( !p
24385 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  Coll ){.        
24386 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c  pColl = db->pDfl
24387 74 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20  tColl;.      }. 
24388 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c       pInfo->aCol
24389 6c 5b 69 5d 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20  l[i] = pColl;.  
2438a 20 20 20 20 70 49 6e 66 6f 2d 3e 61 53 6f 72 74      pInfo->aSort
2438b 4f 72 64 65 72 5b 69 5d 20 3d 20 70 49 74 65 6d  Order[i] = pItem
2438c 2d 3e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20  ->sortOrder;.   
2438d 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
2438e 70 49 6e 66 6f 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  pInfo;.}.../*.**
2438f 20 49 66 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f   If the inner lo
24390 6f 70 20 77 61 73 20 67 65 6e 65 72 61 74 65 64  op was generated
24391 20 75 73 69 6e 67 20 61 20 6e 6f 6e 2d 6e 75 6c   using a non-nul
24392 6c 20 70 4f 72 64 65 72 42 79 20 61 72 67 75 6d  l pOrderBy argum
24393 65 6e 74 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65  ent,.** then the
24394 20 72 65 73 75 6c 74 73 20 77 65 72 65 20 70 6c   results were pl
24395 61 63 65 64 20 69 6e 20 61 20 73 6f 72 74 65 72  aced in a sorter
24396 2e 20 20 41 66 74 65 72 20 74 68 65 20 6c 6f 6f  .  After the loo
24397 70 20 69 73 20 74 65 72 6d 69 6e 61 74 65 64 0a  p is terminated.
24398 2a 2a 20 77 65 20 6e 65 65 64 20 74 6f 20 72 75  ** we need to ru
24399 6e 20 74 68 65 20 73 6f 72 74 65 72 20 61 6e 64  n the sorter and
2439a 20 6f 75 74 70 75 74 20 74 68 65 20 72 65 73 75   output the resu
2439b 6c 74 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  lts.  The follow
2439c 69 6e 67 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 67  ing.** routine g
2439d 65 6e 65 72 61 74 65 73 20 74 68 65 20 63 6f 64  enerates the cod
2439e 65 20 6e 65 65 64 65 64 20 74 6f 20 64 6f 20 74  e needed to do t
2439f 68 61 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  hat..*/.static v
243a0 6f 69 64 20 67 65 6e 65 72 61 74 65 53 6f 72 74  oid generateSort
243a1 54 61 69 6c 28 0a 20 20 50 61 72 73 65 20 2a 70  Tail(.  Parse *p
243a2 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 50 61 72  Parse,    /* Par
243a3 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
243a4 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20    Select *p,    
243a5 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43      /* The SELEC
243a6 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  T statement */. 
243a7 20 56 64 62 65 20 2a 76 2c 20 20 20 20 20 20 20   Vdbe *v,       
243a8 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63     /* Generate c
243a9 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20 56 44  ode into this VD
243aa 42 45 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c  BE */.  int nCol
243ab 75 6d 6e 2c 20 20 20 20 20 20 2f 2a 20 4e 75 6d  umn,      /* Num
243ac 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f  ber of columns o
243ad 66 20 64 61 74 61 20 2a 2f 0a 20 20 53 65 6c 65  f data */.  Sele
243ae 63 74 44 65 73 74 20 2a 70 44 65 73 74 20 2f 2a  ctDest *pDest /*
243af 20 57 72 69 74 65 20 74 68 65 20 73 6f 72 74 65   Write the sorte
243b0 64 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a  d results here *
243b1 2f 0a 29 7b 0a 20 20 69 6e 74 20 62 72 6b 20 3d  /.){.  int brk =
243b2 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
243b3 4c 61 62 65 6c 28 76 29 3b 0a 20 20 69 6e 74 20  Label(v);.  int 
243b4 63 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 64  cont = sqlite3Vd
243b5 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
243b6 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 69 6e    int addr;.  in
243b7 74 20 69 54 61 62 3b 0a 20 20 69 6e 74 20 70 73  t iTab;.  int ps
243b8 65 75 64 6f 54 61 62 20 3d 20 30 3b 0a 20 20 45  eudoTab = 0;.  E
243b9 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42  xprList *pOrderB
243ba 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b  y = p->pOrderBy;
243bb 0a 0a 20 20 69 6e 74 20 65 44 65 73 74 20 3d 20  ..  int eDest = 
243bc 70 44 65 73 74 2d 3e 65 44 65 73 74 3b 0a 20 20  pDest->eDest;.  
243bd 69 6e 74 20 69 50 61 72 6d 20 3d 20 70 44 65 73  int iParm = pDes
243be 74 2d 3e 69 50 61 72 6d 3b 0a 0a 20 20 69 6e 74  t->iParm;..  int
243bf 20 72 65 67 52 6f 77 3b 0a 20 20 69 6e 74 20 72   regRow;.  int r
243c0 65 67 52 6f 77 69 64 3b 0a 0a 20 20 69 54 61 62  egRowid;..  iTab
243c1 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43   = pOrderBy->iEC
243c2 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 65 44 65  ursor;.  if( eDe
243c3 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 7c  st==SRT_Output |
243c4 7c 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72  | eDest==SRT_Cor
243c5 6f 75 74 69 6e 65 20 29 7b 0a 20 20 20 20 70 73  outine ){.    ps
243c6 65 75 64 6f 54 61 62 20 3d 20 70 50 61 72 73 65  eudoTab = pParse
243c7 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 73 71  ->nTab++;.    sq
243c8 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
243c9 76 2c 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75  v, OP_SetNumColu
243ca 6d 6e 73 2c 20 30 2c 20 6e 43 6f 6c 75 6d 6e 29  mns, 0, nColumn)
243cb 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
243cc 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70  eAddOp2(v, OP_Op
243cd 65 6e 50 73 65 75 64 6f 2c 20 70 73 65 75 64 6f  enPseudo, pseudo
243ce 54 61 62 2c 20 65 44 65 73 74 3d 3d 53 52 54 5f  Tab, eDest==SRT_
243cf 4f 75 74 70 75 74 29 3b 0a 20 20 7d 0a 20 20 61  Output);.  }.  a
243d0 64 64 72 20 3d 20 31 20 2b 20 73 71 6c 69 74 65  ddr = 1 + sqlite
243d1 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
243d2 50 5f 53 6f 72 74 2c 20 69 54 61 62 2c 20 62 72  P_Sort, iTab, br
243d3 6b 29 3b 0a 20 20 63 6f 64 65 4f 66 66 73 65 74  k);.  codeOffset
243d4 28 76 2c 20 70 2c 20 63 6f 6e 74 29 3b 0a 20 20  (v, p, cont);.  
243d5 72 65 67 52 6f 77 20 3d 20 73 71 6c 69 74 65 33  regRow = sqlite3
243d6 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
243d7 65 29 3b 0a 20 20 72 65 67 52 6f 77 69 64 20 3d  e);.  regRowid =
243d8 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
243d9 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71  eg(pParse);.  sq
243da 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
243db 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 54  v, OP_Column, iT
243dc 61 62 2c 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45  ab, pOrderBy->nE
243dd 78 70 72 20 2b 20 31 2c 20 72 65 67 52 6f 77 29  xpr + 1, regRow)
243de 3b 0a 20 20 73 77 69 74 63 68 28 20 65 44 65 73  ;.  switch( eDes
243df 74 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 52  t ){.    case SR
243e0 54 5f 54 61 62 6c 65 3a 0a 20 20 20 20 63 61 73  T_Table:.    cas
243e1 65 20 53 52 54 5f 45 70 68 65 6d 54 61 62 3a 20  e SRT_EphemTab: 
243e2 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
243e3 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
243e4 4e 65 77 52 6f 77 69 64 2c 20 69 50 61 72 6d 2c  NewRowid, iParm,
243e5 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
243e6 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
243e7 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74  Op3(v, OP_Insert
243e8 2c 20 69 50 61 72 6d 2c 20 72 65 67 52 6f 77 2c  , iParm, regRow,
243e9 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
243ea 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
243eb 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f  ngeP5(v, OPFLAG_
243ec 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 20 20 62  APPEND);.      b
243ed 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e  reak;.    }.#ifn
243ee 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
243ef 53 55 42 51 55 45 52 59 0a 20 20 20 20 63 61 73  SUBQUERY.    cas
243f0 65 20 53 52 54 5f 53 65 74 3a 20 7b 0a 20 20 20  e SRT_Set: {.   
243f1 20 20 20 61 73 73 65 72 74 28 20 6e 43 6f 6c 75     assert( nColu
243f2 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73  mn==1 );.      s
243f3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
243f4 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  (v, OP_MakeRecor
243f5 64 2c 20 72 65 67 52 6f 77 2c 20 31 2c 20 72 65  d, regRow, 1, re
243f6 67 52 6f 77 69 64 2c 20 26 70 2d 3e 61 66 66 69  gRowid, &p->affi
243f7 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20 20 20 20  nity, 1);.      
243f8 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
243f9 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70  AffinityChange(p
243fa 50 61 72 73 65 2c 20 72 65 67 52 6f 77 2c 20 31  Parse, regRow, 1
243fb 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
243fc 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
243fd 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 50 61 72  _IdxInsert, iPar
243fe 6d 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  m, regRowid);.  
243ff 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
24400 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 4d 65  .    case SRT_Me
24401 6d 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72  m: {.      asser
24402 74 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b  t( nColumn==1 );
24403 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
24404 70 72 43 6f 64 65 4d 6f 76 65 28 70 50 61 72 73  prCodeMove(pPars
24405 65 2c 20 72 65 67 52 6f 77 2c 20 69 50 61 72 6d  e, regRow, iParm
24406 2c 20 31 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54  , 1);.      /* T
24407 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20  he LIMIT clause 
24408 77 69 6c 6c 20 74 65 72 6d 69 6e 61 74 65 20 74  will terminate t
24409 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20 2a  he loop for us *
2440a 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  /.      break;. 
2440b 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
2440c 63 61 73 65 20 53 52 54 5f 4f 75 74 70 75 74 3a  case SRT_Output:
2440d 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 43 6f  .    case SRT_Co
2440e 72 6f 75 74 69 6e 65 3a 20 7b 0a 20 20 20 20 20  routine: {.     
2440f 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 73 71   int i;.      sq
24410 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
24411 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31  v, OP_Integer, 1
24412 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
24413 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
24414 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72  dOp3(v, OP_Inser
24415 74 2c 20 70 73 65 75 64 6f 54 61 62 2c 20 72 65  t, pseudoTab, re
24416 67 52 6f 77 2c 20 72 65 67 52 6f 77 69 64 29 3b  gRow, regRowid);
24417 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
24418 69 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  i<nColumn; i++){
24419 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2441a 20 72 65 67 52 6f 77 21 3d 70 44 65 73 74 2d 3e   regRow!=pDest->
2441b 69 4d 65 6d 2b 69 20 29 3b 0a 20 20 20 20 20 20  iMem+i );.      
2441c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2441d 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
2441e 2c 20 70 73 65 75 64 6f 54 61 62 2c 20 69 2c 20  , pseudoTab, i, 
2441f 70 44 65 73 74 2d 3e 69 4d 65 6d 2b 69 29 3b 0a  pDest->iMem+i);.
24420 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
24421 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74  ( eDest==SRT_Out
24422 70 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73  put ){.        s
24423 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
24424 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
24425 2c 20 70 44 65 73 74 2d 3e 69 4d 65 6d 2c 20 6e  , pDest->iMem, n
24426 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 20  Column);.       
24427 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
24428 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28  eAffinityChange(
24429 70 50 61 72 73 65 2c 20 70 44 65 73 74 2d 3e 69  pParse, pDest->i
2442a 4d 65 6d 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20  Mem, nColumn);. 
2442b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2442c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2442d 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c  ddOp1(v, OP_Yiel
2442e 64 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 29  d, pDest->iParm)
2442f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
24430 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
24431 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
24432 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20    /* Do nothing 
24433 2a 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  */.      break;.
24434 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69      }.  }.  sqli
24435 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
24436 67 28 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77  g(pParse, regRow
24437 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65  );.  sqlite3Rele
24438 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
24439 65 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 0a 20  e, regRowid);.. 
2443a 20 2f 2a 20 4c 49 4d 49 54 20 68 61 73 20 62 65   /* LIMIT has be
2443b 65 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62  en implemented b
2443c 79 20 74 68 65 20 70 75 73 68 4f 6e 74 6f 53 6f  y the pushOntoSo
2443d 72 74 65 72 28 29 20 72 6f 75 74 69 6e 65 2e 0a  rter() routine..
2443e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
2443f 2d 3e 69 4c 69 6d 69 74 3d 3d 30 20 29 3b 0a 0a  ->iLimit==0 );..
24440 20 20 2f 2a 20 54 68 65 20 62 6f 74 74 6f 6d 20    /* The bottom 
24441 6f 66 20 74 68 65 20 6c 6f 6f 70 0a 20 20 2a 2f  of the loop.  */
24442 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  .  sqlite3VdbeRe
24443 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 63 6f  solveLabel(v, co
24444 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  nt);.  sqlite3Vd
24445 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
24446 65 78 74 2c 20 69 54 61 62 2c 20 61 64 64 72 29  ext, iTab, addr)
24447 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52  ;.  sqlite3VdbeR
24448 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 62  esolveLabel(v, b
24449 72 6b 29 3b 0a 20 20 69 66 28 20 65 44 65 73 74  rk);.  if( eDest
2444a 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 7c 7c 20  ==SRT_Output || 
2444b 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f 75  eDest==SRT_Corou
2444c 74 69 6e 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  tine ){.    sqli
2444d 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2444e 20 4f 50 5f 43 6c 6f 73 65 2c 20 70 73 65 75 64   OP_Close, pseud
2444f 6f 54 61 62 2c 20 30 29 3b 0a 20 20 7d 0a 0a 7d  oTab, 0);.  }..}
24450 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
24451 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74   pointer to a st
24452 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ring containing 
24453 74 68 65 20 27 64 65 63 6c 61 72 61 74 69 6f 6e  the 'declaration
24454 20 74 79 70 65 27 20 6f 66 20 74 68 65 0a 2a 2a   type' of the.**
24455 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70   expression pExp
24456 72 2e 20 54 68 65 20 73 74 72 69 6e 67 20 6d 61  r. The string ma
24457 79 20 62 65 20 74 72 65 61 74 65 64 20 61 73 20  y be treated as 
24458 73 74 61 74 69 63 20 62 79 20 74 68 65 20 63 61  static by the ca
24459 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ller..**.** The 
2445a 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65  declaration type
2445b 20 69 73 20 74 68 65 20 65 78 61 63 74 20 64 61   is the exact da
2445c 74 61 74 79 70 65 20 64 65 66 69 6e 69 74 69 6f  tatype definitio
2445d 6e 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d  n extracted from
2445e 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c   the.** original
2445f 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
24460 61 74 65 6d 65 6e 74 20 69 66 20 74 68 65 20 65  atement if the e
24461 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 63  xpression is a c
24462 6f 6c 75 6d 6e 2e 20 54 68 65 0a 2a 2a 20 64 65  olumn. The.** de
24463 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 66  claration type f
24464 6f 72 20 61 20 52 4f 57 49 44 20 66 69 65 6c 64  or a ROWID field
24465 20 69 73 20 49 4e 54 45 47 45 52 2e 20 45 78 61   is INTEGER. Exa
24466 63 74 6c 79 20 77 68 65 6e 20 61 6e 20 65 78 70  ctly when an exp
24467 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f  ression.** is co
24468 6e 73 69 64 65 72 65 64 20 61 20 63 6f 6c 75 6d  nsidered a colum
24469 6e 20 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65 78  n can be complex
2446a 20 69 6e 20 74 68 65 20 70 72 65 73 65 6e 63 65   in the presence
2446b 20 6f 66 20 73 75 62 71 75 65 72 69 65 73 2e 20   of subqueries. 
2446c 54 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2d 73 65  The.** result-se
2446d 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20  t expression in 
2446e 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  all of the follo
2446f 77 69 6e 67 20 53 45 4c 45 43 54 20 73 74 61 74  wing SELECT stat
24470 65 6d 65 6e 74 73 20 69 73 20 0a 2a 2a 20 63 6f  ements is .** co
24471 6e 73 69 64 65 72 65 64 20 61 20 63 6f 6c 75 6d  nsidered a colum
24472 6e 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  n by this functi
24473 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45  on..**.**   SELE
24474 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62 6c 3b  CT col FROM tbl;
24475 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 28 53 45  .**   SELECT (SE
24476 4c 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62  LECT col FROM tb
24477 6c 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 28  l;.**   SELECT (
24478 53 45 4c 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20  SELECT col FROM 
24479 74 62 6c 29 3b 0a 2a 2a 20 20 20 53 45 4c 45 43  tbl);.**   SELEC
2447a 54 20 61 62 63 20 46 52 4f 4d 20 28 53 45 4c 45  T abc FROM (SELE
2447b 43 54 20 63 6f 6c 20 41 53 20 61 62 63 20 46 52  CT col AS abc FR
2447c 4f 4d 20 74 62 6c 29 3b 0a 2a 2a 20 0a 2a 2a 20  OM tbl);.** .** 
2447d 54 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  The declaration 
2447e 74 79 70 65 20 66 6f 72 20 61 6e 79 20 65 78 70  type for any exp
2447f 72 65 73 73 69 6f 6e 20 6f 74 68 65 72 20 74 68  ression other th
24480 61 6e 20 61 20 63 6f 6c 75 6d 6e 20 69 73 20 4e  an a column is N
24481 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ULL..*/.static c
24482 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6c 75 6d  onst char *colum
24483 6e 54 79 70 65 28 0a 20 20 4e 61 6d 65 43 6f 6e  nType(.  NameCon
24484 74 65 78 74 20 2a 70 4e 43 2c 20 0a 20 20 45 78  text *pNC, .  Ex
24485 70 72 20 2a 70 45 78 70 72 2c 0a 20 20 63 6f 6e  pr *pExpr,.  con
24486 73 74 20 63 68 61 72 20 2a 2a 70 7a 4f 72 69 67  st char **pzOrig
24487 69 6e 44 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68  inDb,.  const ch
24488 61 72 20 2a 2a 70 7a 4f 72 69 67 69 6e 54 61 62  ar **pzOriginTab
24489 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
2448a 2a 70 7a 4f 72 69 67 69 6e 43 6f 6c 0a 29 7b 0a  *pzOriginCol.){.
2448b 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54    char const *zT
2448c 79 70 65 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  ype = 0;.  char 
2448d 63 6f 6e 73 74 20 2a 7a 4f 72 69 67 69 6e 44 62  const *zOriginDb
2448e 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e   = 0;.  char con
2448f 73 74 20 2a 7a 4f 72 69 67 69 6e 54 61 62 20 3d  st *zOriginTab =
24490 20 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74   0;.  char const
24491 20 2a 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 30   *zOriginCol = 0
24492 3b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 69 66 28  ;.  int j;.  if(
24493 20 70 45 78 70 72 3d 3d 30 20 7c 7c 20 70 4e 43   pExpr==0 || pNC
24494 2d 3e 70 53 72 63 4c 69 73 74 3d 3d 30 20 29 20  ->pSrcList==0 ) 
24495 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 73 77 69  return 0;..  swi
24496 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29  tch( pExpr->op )
24497 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47  {.    case TK_AG
24498 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61  G_COLUMN:.    ca
24499 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a  se TK_COLUMN: {.
2449a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70        /* The exp
2449b 72 65 73 73 69 6f 6e 20 69 73 20 61 20 63 6f 6c  ression is a col
2449c 75 6d 6e 2e 20 4c 6f 63 61 74 65 20 74 68 65 20  umn. Locate the 
2449d 74 61 62 6c 65 20 74 68 65 20 63 6f 6c 75 6d 6e  table the column
2449e 20 69 73 20 62 65 69 6e 67 0a 20 20 20 20 20 20   is being.      
2449f 2a 2a 20 65 78 74 72 61 63 74 65 64 20 66 72 6f  ** extracted fro
244a0 6d 20 69 6e 20 4e 61 6d 65 43 6f 6e 74 65 78 74  m in NameContext
244a1 2e 70 53 72 63 4c 69 73 74 2e 20 54 68 69 73 20  .pSrcList. This 
244a2 74 61 62 6c 65 20 6d 61 79 20 62 65 20 72 65 61  table may be rea
244a3 6c 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62  l.      ** datab
244a4 61 73 65 20 74 61 62 6c 65 20 6f 72 20 61 20 73  ase table or a s
244a5 75 62 71 75 65 72 79 2e 0a 20 20 20 20 20 20 2a  ubquery..      *
244a6 2f 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70  /.      Table *p
244a7 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Tab = 0;        
244a8 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 73 74 72      /* Table str
244a9 75 63 74 75 72 65 20 63 6f 6c 75 6d 6e 20 69 73  ucture column is
244aa 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20   extracted from 
244ab 2a 2f 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20  */.      Select 
244ac 2a 70 53 20 3d 20 30 3b 20 20 20 20 20 20 20 20  *pS = 0;        
244ad 20 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 74       /* Select t
244ae 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 65 78 74  he column is ext
244af 72 61 63 74 65 64 20 66 72 6f 6d 20 2a 2f 0a 20  racted from */. 
244b0 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20       int iCol = 
244b1 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 20  pExpr->iColumn; 
244b2 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c   /* Index of col
244b3 75 6d 6e 20 69 6e 20 70 54 61 62 20 2a 2f 0a 20  umn in pTab */. 
244b4 20 20 20 20 20 77 68 69 6c 65 28 20 70 4e 43 20       while( pNC 
244b5 26 26 20 21 70 54 61 62 20 29 7b 0a 20 20 20 20  && !pTab ){.    
244b6 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61      SrcList *pTa
244b7 62 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72  bList = pNC->pSr
244b8 63 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20 66  cList;.        f
244b9 6f 72 28 6a 3d 30 3b 6a 3c 70 54 61 62 4c 69 73  or(j=0;j<pTabLis
244ba 74 2d 3e 6e 53 72 63 20 26 26 20 70 54 61 62 4c  t->nSrc && pTabL
244bb 69 73 74 2d 3e 61 5b 6a 5d 2e 69 43 75 72 73 6f  ist->a[j].iCurso
244bc 72 21 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  r!=pExpr->iTable
244bd 3b 6a 2b 2b 29 3b 0a 20 20 20 20 20 20 20 20 69  ;j++);.        i
244be 66 28 20 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e  f( j<pTabList->n
244bf 53 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Src ){.         
244c0 20 70 54 61 62 20 3d 20 70 54 61 62 4c 69 73 74   pTab = pTabList
244c1 2d 3e 61 5b 6a 5d 2e 70 54 61 62 3b 0a 20 20 20  ->a[j].pTab;.   
244c2 20 20 20 20 20 20 20 70 53 20 3d 20 70 54 61 62         pS = pTab
244c3 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 53 65 6c 65  List->a[j].pSele
244c4 63 74 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ct;.        }els
244c5 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4e 43  e{.          pNC
244c6 20 3d 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20   = pNC->pNext;. 
244c7 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
244c8 0a 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62  ..      if( pTab
244c9 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ==0 ){.        /
244ca 2a 20 46 49 58 20 4d 45 3a 0a 20 20 20 20 20 20  * FIX ME:.      
244cb 20 20 2a 2a 20 54 68 69 73 20 63 61 6e 20 6f 63    ** This can oc
244cc 63 75 72 73 20 69 66 20 79 6f 75 20 68 61 76 65  curs if you have
244cd 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69 6b 65 20   something like 
244ce 22 53 45 4c 45 43 54 20 6e 65 77 2e 78 3b 22 20  "SELECT new.x;" 
244cf 69 6e 73 69 64 65 0a 20 20 20 20 20 20 20 20 2a  inside.        *
244d0 2a 20 61 20 74 72 69 67 67 65 72 2e 20 20 49 6e  * a trigger.  In
244d1 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66   other words, if
244d2 20 79 6f 75 20 72 65 66 65 72 65 6e 63 65 20 74   you reference t
244d3 68 65 20 73 70 65 63 69 61 6c 20 22 6e 65 77 22  he special "new"
244d4 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62 6c  .        ** tabl
244d5 65 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  e in the result 
244d6 73 65 74 20 6f 66 20 61 20 73 65 6c 65 63 74 2e  set of a select.
244d7 20 20 57 65 20 64 6f 20 6e 6f 74 20 68 61 76 65    We do not have
244d8 20 61 20 67 6f 6f 64 20 77 61 79 0a 20 20 20 20   a good way.    
244d9 20 20 20 20 2a 2a 20 74 6f 20 66 69 6e 64 20 74      ** to find t
244da 68 65 20 61 63 74 75 61 6c 20 74 61 62 6c 65 20  he actual table 
244db 74 79 70 65 2c 20 73 6f 20 63 61 6c 6c 20 69 74  type, so call it
244dc 20 22 54 45 58 54 22 2e 20 20 54 68 69 73 20 69   "TEXT".  This i
244dd 73 20 72 65 61 6c 6c 79 0a 20 20 20 20 20 20 20  s really.       
244de 20 2a 2a 20 73 6f 6d 65 74 68 69 6e 67 20 6f 66   ** something of
244df 20 61 20 62 75 67 2c 20 62 75 74 20 49 20 64 6f   a bug, but I do
244e0 20 6e 6f 74 20 6b 6e 6f 77 20 68 6f 77 20 74 6f   not know how to
244e1 20 66 69 78 20 69 74 2e 0a 20 20 20 20 20 20 20   fix it..       
244e2 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54   **.        ** T
244e3 68 69 73 20 63 6f 64 65 20 64 6f 65 73 20 6e 6f  his code does no
244e4 74 20 70 72 6f 64 75 63 65 20 74 68 65 20 63 6f  t produce the co
244e5 72 72 65 63 74 20 61 6e 73 77 65 72 20 2d 20 69  rrect answer - i
244e6 74 20 6a 75 73 74 20 70 72 65 76 65 6e 74 73 0a  t just prevents.
244e7 20 20 20 20 20 20 20 20 2a 2a 20 61 20 73 65 67          ** a seg
244e8 66 61 75 6c 74 2e 20 20 53 65 65 20 74 69 63 6b  fault.  See tick
244e9 65 74 20 23 31 32 32 39 2e 0a 20 20 20 20 20 20  et #1229..      
244ea 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 7a 54 79    */.        zTy
244eb 70 65 20 3d 20 22 54 45 58 54 22 3b 0a 20 20 20  pe = "TEXT";.   
244ec 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
244ed 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 72    }..      asser
244ee 74 28 20 70 54 61 62 20 29 3b 0a 20 20 20 20 20  t( pTab );.     
244ef 20 69 66 28 20 70 53 20 29 7b 0a 20 20 20 20 20   if( pS ){.     
244f0 20 20 20 2f 2a 20 54 68 65 20 22 74 61 62 6c 65     /* The "table
244f1 22 20 69 73 20 61 63 74 75 61 6c 6c 79 20 61 20  " is actually a 
244f2 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 61 20  sub-select or a 
244f3 76 69 65 77 20 69 6e 20 74 68 65 20 46 52 4f 4d  view in the FROM
244f4 20 63 6c 61 75 73 65 0a 20 20 20 20 20 20 20 20   clause.        
244f5 2a 2a 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  ** of the SELECT
244f6 20 73 74 61 74 65 6d 65 6e 74 2e 20 52 65 74 75   statement. Retu
244f7 72 6e 20 74 68 65 20 64 65 63 6c 61 72 61 74 69  rn the declarati
244f8 6f 6e 20 74 79 70 65 20 61 6e 64 20 6f 72 69 67  on type and orig
244f9 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 61  in.        ** da
244fa 74 61 20 66 6f 72 20 74 68 65 20 72 65 73 75 6c  ta for the resul
244fb 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 20 6f 66 20  t-set column of 
244fc 74 68 65 20 73 75 62 2d 73 65 6c 65 63 74 2e 0a  the sub-select..
244fd 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
244fe 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 26     if( iCol>=0 &
244ff 26 20 69 43 6f 6c 3c 70 53 2d 3e 70 45 4c 69 73  & iCol<pS->pELis
24500 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20  t->nExpr ){.    
24501 20 20 20 20 20 20 2f 2a 20 49 66 20 69 43 6f 6c        /* If iCol
24502 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65   is less than ze
24503 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 65 78 70  ro, then the exp
24504 72 65 73 73 69 6f 6e 20 72 65 71 75 65 73 74 73  ression requests
24505 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a   the.          *
24506 2a 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 73  * rowid of the s
24507 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 76 69 65  ub-select or vie
24508 77 2e 20 54 68 69 73 20 65 78 70 72 65 73 73 69  w. This expressi
24509 6f 6e 20 69 73 20 6c 65 67 61 6c 20 28 73 65 65  on is legal (see
2450a 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74   .          ** t
2450b 65 73 74 20 63 61 73 65 20 6d 69 73 63 32 2e 32  est case misc2.2
2450c 2e 32 29 20 2d 20 69 74 20 61 6c 77 61 79 73 20  .2) - it always 
2450d 65 76 61 6c 75 61 74 65 73 20 74 6f 20 4e 55 4c  evaluates to NUL
2450e 4c 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  L..          */.
2450f 20 20 20 20 20 20 20 20 20 20 4e 61 6d 65 43 6f            NameCo
24510 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 20 20 20  ntext sNC;.     
24511 20 20 20 20 20 45 78 70 72 20 2a 70 20 3d 20 70       Expr *p = p
24512 53 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f  S->pEList->a[iCo
24513 6c 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  l].pExpr;.      
24514 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74      sNC.pSrcList
24515 20 3d 20 70 53 2d 3e 70 53 72 63 3b 0a 20 20 20   = pS->pSrc;.   
24516 20 20 20 20 20 20 20 73 4e 43 2e 70 4e 65 78 74         sNC.pNext
24517 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
24518 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 4e 43  sNC.pParse = pNC
24519 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 20 20 20  ->pParse;.      
2451a 20 20 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c 75      zType = colu
2451b 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20  mnType(&sNC, p, 
2451c 26 7a 4f 72 69 67 69 6e 44 62 2c 20 26 7a 4f 72  &zOriginDb, &zOr
2451d 69 67 69 6e 54 61 62 2c 20 26 7a 4f 72 69 67 69  iginTab, &zOrigi
2451e 6e 43 6f 6c 29 3b 20 0a 20 20 20 20 20 20 20 20  nCol); .        
2451f 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  }.      }else if
24520 28 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20  ( pTab->pSchema 
24521 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 20  ){.        /* A 
24522 72 65 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  real table */.  
24523 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
24524 53 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  S );.        if(
24525 20 69 43 6f 6c 3c 30 20 29 20 69 43 6f 6c 20 3d   iCol<0 ) iCol =
24526 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20 20   pTab->iPKey;.  
24527 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 43        assert( iC
24528 6f 6c 3d 3d 2d 31 20 7c 7c 20 28 69 43 6f 6c 3e  ol==-1 || (iCol>
24529 3d 30 20 26 26 20 69 43 6f 6c 3c 70 54 61 62 2d  =0 && iCol<pTab-
2452a 3e 6e 43 6f 6c 29 20 29 3b 0a 20 20 20 20 20 20  >nCol) );.      
2452b 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a    if( iCol<0 ){.
2452c 20 20 20 20 20 20 20 20 20 20 7a 54 79 70 65 20            zType 
2452d 3d 20 22 49 4e 54 45 47 45 52 22 3b 0a 20 20 20  = "INTEGER";.   
2452e 20 20 20 20 20 20 20 7a 4f 72 69 67 69 6e 43 6f         zOriginCo
2452f 6c 20 3d 20 22 72 6f 77 69 64 22 3b 0a 20 20 20  l = "rowid";.   
24530 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
24531 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 70 54        zType = pT
24532 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a  ab->aCol[iCol].z
24533 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 20 20  Type;.          
24534 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 70 54 61  zOriginCol = pTa
24535 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e  b->aCol[iCol].zN
24536 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ame;.        }. 
24537 20 20 20 20 20 20 20 7a 4f 72 69 67 69 6e 54 61         zOriginTa
24538 62 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b  b = pTab->zName;
24539 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4e 43  .        if( pNC
2453a 2d 3e 70 50 61 72 73 65 20 29 7b 0a 20 20 20 20  ->pParse ){.    
2453b 20 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20        int iDb = 
2453c 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
2453d 6e 64 65 78 28 70 4e 43 2d 3e 70 50 61 72 73 65  ndex(pNC->pParse
2453e 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  ->db, pTab->pSch
2453f 65 6d 61 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ema);.          
24540 7a 4f 72 69 67 69 6e 44 62 20 3d 20 70 4e 43 2d  zOriginDb = pNC-
24541 3e 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62  >pParse->db->aDb
24542 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  [iDb].zName;.   
24543 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
24544 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
24545 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
24546 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20  _OMIT_SUBQUERY. 
24547 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43     case TK_SELEC
24548 54 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  T: {.      /* Th
24549 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
2454a 61 20 73 75 62 2d 73 65 6c 65 63 74 2e 20 52 65  a sub-select. Re
2454b 74 75 72 6e 20 74 68 65 20 64 65 63 6c 61 72 61  turn the declara
2454c 74 69 6f 6e 20 74 79 70 65 20 61 6e 64 0a 20 20  tion type and.  
2454d 20 20 20 20 2a 2a 20 6f 72 69 67 69 6e 20 69 6e      ** origin in
2454e 66 6f 20 66 6f 72 20 74 68 65 20 73 69 6e 67 6c  fo for the singl
2454f 65 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20  e column in the 
24550 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68  result set of th
24551 65 20 53 45 4c 45 43 54 0a 20 20 20 20 20 20 2a  e SELECT.      *
24552 2a 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20  * statement..   
24553 20 20 20 2a 2f 0a 20 20 20 20 20 20 4e 61 6d 65     */.      Name
24554 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 20  Context sNC;.   
24555 20 20 20 53 65 6c 65 63 74 20 2a 70 53 20 3d 20     Select *pS = 
24556 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 3b 0a  pExpr->pSelect;.
24557 20 20 20 20 20 20 45 78 70 72 20 2a 70 20 3d 20        Expr *p = 
24558 70 53 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d  pS->pEList->a[0]
24559 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 73 4e  .pExpr;.      sN
2455a 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 53 2d  C.pSrcList = pS-
2455b 3e 70 53 72 63 3b 0a 20 20 20 20 20 20 73 4e 43  >pSrc;.      sNC
2455c 2e 70 4e 65 78 74 20 3d 20 70 4e 43 3b 0a 20 20  .pNext = pNC;.  
2455d 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d      sNC.pParse =
2455e 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20   pNC->pParse;.  
2455f 20 20 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c 75      zType = colu
24560 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20  mnType(&sNC, p, 
24561 26 7a 4f 72 69 67 69 6e 44 62 2c 20 26 7a 4f 72  &zOriginDb, &zOr
24562 69 67 69 6e 54 61 62 2c 20 26 7a 4f 72 69 67 69  iginTab, &zOrigi
24563 6e 43 6f 6c 29 3b 20 0a 20 20 20 20 20 20 62 72  nCol); .      br
24564 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
24565 66 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28 20 70  f.  }.  .  if( p
24566 7a 4f 72 69 67 69 6e 44 62 20 29 7b 0a 20 20 20  zOriginDb ){.   
24567 20 61 73 73 65 72 74 28 20 70 7a 4f 72 69 67 69   assert( pzOrigi
24568 6e 54 61 62 20 26 26 20 70 7a 4f 72 69 67 69 6e  nTab && pzOrigin
24569 43 6f 6c 20 29 3b 0a 20 20 20 20 2a 70 7a 4f 72  Col );.    *pzOr
2456a 69 67 69 6e 44 62 20 3d 20 7a 4f 72 69 67 69 6e  iginDb = zOrigin
2456b 44 62 3b 0a 20 20 20 20 2a 70 7a 4f 72 69 67 69  Db;.    *pzOrigi
2456c 6e 54 61 62 20 3d 20 7a 4f 72 69 67 69 6e 54 61  nTab = zOriginTa
2456d 62 3b 0a 20 20 20 20 2a 70 7a 4f 72 69 67 69 6e  b;.    *pzOrigin
2456e 43 6f 6c 20 3d 20 7a 4f 72 69 67 69 6e 43 6f 6c  Col = zOriginCol
2456f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a  ;.  }.  return z
24570 54 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  Type;.}../*.** G
24571 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
24572 74 20 77 69 6c 6c 20 74 65 6c 6c 20 74 68 65 20  t will tell the 
24573 56 44 42 45 20 74 68 65 20 64 65 63 6c 61 72 61  VDBE the declara
24574 74 69 6f 6e 20 74 79 70 65 73 20 6f 66 20 63 6f  tion types of co
24575 6c 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20  lumns.** in the 
24576 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 73  result set..*/.s
24577 74 61 74 69 63 20 76 6f 69 64 20 67 65 6e 65 72  tatic void gener
24578 61 74 65 43 6f 6c 75 6d 6e 54 79 70 65 73 28 0a  ateColumnTypes(.
24579 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2457a 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20        /* Parser 
2457b 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63  context */.  Src
2457c 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20  List *pTabList, 
2457d 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c   /* List of tabl
2457e 65 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  es */.  ExprList
2457f 20 2a 70 45 4c 69 73 74 20 20 20 20 2f 2a 20 45   *pEList    /* E
24580 78 70 72 65 73 73 69 6f 6e 73 20 64 65 66 69 6e  xpressions defin
24581 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 20 73  ing the result s
24582 65 74 20 2a 2f 0a 29 7b 0a 23 69 66 6e 64 65 66  et */.){.#ifndef
24583 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43   SQLITE_OMIT_DEC
24584 4c 54 59 50 45 0a 20 20 56 64 62 65 20 2a 76 20  LTYPE.  Vdbe *v 
24585 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
24586 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4e 61 6d 65  .  int i;.  Name
24587 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 73  Context sNC;.  s
24588 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 54  NC.pSrcList = pT
24589 61 62 4c 69 73 74 3b 0a 20 20 73 4e 43 2e 70 50  abList;.  sNC.pP
2458a 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20  arse = pParse;. 
2458b 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69   for(i=0; i<pELi
2458c 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  st->nExpr; i++){
2458d 0a 20 20 20 20 45 78 70 72 20 2a 70 20 3d 20 70  .    Expr *p = p
2458e 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70  EList->a[i].pExp
2458f 72 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  r;.    const cha
24590 72 20 2a 7a 54 79 70 65 3b 0a 23 69 66 64 65 66  r *zType;.#ifdef
24591 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43   SQLITE_ENABLE_C
24592 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 20  OLUMN_METADATA. 
24593 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
24594 4f 72 69 67 44 62 20 3d 20 30 3b 0a 20 20 20 20  OrigDb = 0;.    
24595 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 72 69  const char *zOri
24596 67 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 63 6f  gTab = 0;.    co
24597 6e 73 74 20 63 68 61 72 20 2a 7a 4f 72 69 67 43  nst char *zOrigC
24598 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 7a 54 79 70  ol = 0;.    zTyp
24599 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26  e = columnType(&
2459a 73 4e 43 2c 20 70 2c 20 26 7a 4f 72 69 67 44 62  sNC, p, &zOrigDb
2459b 2c 20 26 7a 4f 72 69 67 54 61 62 2c 20 26 7a 4f  , &zOrigTab, &zO
2459c 72 69 67 43 6f 6c 29 3b 0a 0a 20 20 20 20 2f 2a  rigCol);..    /*
2459d 20 54 68 65 20 76 64 62 65 20 6d 75 73 74 20 6d   The vdbe must m
2459e 61 6b 65 20 69 74 73 20 6f 77 6e 20 63 6f 70 79  ake its own copy
2459f 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2d 74   of the column-t
245a0 79 70 65 20 61 6e 64 20 6f 74 68 65 72 20 0a 20  ype and other . 
245a1 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 73 70 65     ** column spe
245a2 63 69 66 69 63 20 73 74 72 69 6e 67 73 2c 20 69  cific strings, i
245a3 6e 20 63 61 73 65 20 74 68 65 20 73 63 68 65 6d  n case the schem
245a4 61 20 69 73 20 72 65 73 65 74 20 62 65 66 6f 72  a is reset befor
245a5 65 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 76 69  e this.    ** vi
245a6 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 69 73  rtual machine is
245a7 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a 2f   deleted..    */
245a8 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
245a9 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c  SetColName(v, i,
245aa 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53   COLNAME_DATABAS
245ab 45 2c 20 7a 4f 72 69 67 44 62 2c 20 53 51 4c 49  E, zOrigDb, SQLI
245ac 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
245ad 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
245ae 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43  tColName(v, i, C
245af 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 2c 20 7a 4f  OLNAME_TABLE, zO
245b0 72 69 67 54 61 62 2c 20 53 51 4c 49 54 45 5f 54  rigTab, SQLITE_T
245b1 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 73  RANSIENT);.    s
245b2 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
245b3 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41  Name(v, i, COLNA
245b4 4d 45 5f 43 4f 4c 55 4d 4e 2c 20 7a 4f 72 69 67  ME_COLUMN, zOrig
245b5 43 6f 6c 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e  Col, SQLITE_TRAN
245b6 53 49 45 4e 54 29 3b 0a 23 65 6c 73 65 0a 20 20  SIENT);.#else.  
245b7 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c 75 6d 6e    zType = column
245b8 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20 30 2c  Type(&sNC, p, 0,
245b9 20 30 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20   0, 0);.#endif. 
245ba 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
245bb 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43  tColName(v, i, C
245bc 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 2c  OLNAME_DECLTYPE,
245bd 20 7a 54 79 70 65 2c 20 53 51 4c 49 54 45 5f 54   zType, SQLITE_T
245be 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 23  RANSIENT);.  }.#
245bf 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
245c0 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 20 2a 2f  OMIT_DECLTYPE */
245c1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
245c2 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c  te code that wil
245c3 6c 20 74 65 6c 6c 20 74 68 65 20 56 44 42 45 20  l tell the VDBE 
245c4 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 63 6f 6c  the names of col
245c5 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 72  umns.** in the r
245c6 65 73 75 6c 74 20 73 65 74 2e 20 20 54 68 69 73  esult set.  This
245c7 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20   information is 
245c8 75 73 65 64 20 74 6f 20 70 72 6f 76 69 64 65 20  used to provide 
245c9 74 68 65 0a 2a 2a 20 61 7a 43 6f 6c 5b 5d 20 76  the.** azCol[] v
245ca 61 6c 75 65 73 20 69 6e 20 74 68 65 20 63 61 6c  alues in the cal
245cb 6c 62 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63  lback..*/.static
245cc 20 76 6f 69 64 20 67 65 6e 65 72 61 74 65 43 6f   void generateCo
245cd 6c 75 6d 6e 4e 61 6d 65 73 28 0a 20 20 50 61 72  lumnNames(.  Par
245ce 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
245cf 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65   /* Parser conte
245d0 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  xt */.  SrcList 
245d1 2a 70 54 61 62 4c 69 73 74 2c 20 20 2f 2a 20 4c  *pTabList,  /* L
245d2 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20 2a 2f  ist of tables */
245d3 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
245d4 69 73 74 20 20 20 20 2f 2a 20 45 78 70 72 65 73  ist    /* Expres
245d5 73 69 6f 6e 73 20 64 65 66 69 6e 69 6e 67 20 74  sions defining t
245d6 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f  he result set */
245d7 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  .){.  Vdbe *v = 
245d8 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
245d9 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 71 6c   int i, j;.  sql
245da 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
245db 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 66 75 6c  e->db;.  int ful
245dc 6c 4e 61 6d 65 73 2c 20 73 68 6f 72 74 4e 61 6d  lNames, shortNam
245dd 65 73 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  es;..#ifndef SQL
245de 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e  ITE_OMIT_EXPLAIN
245df 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73  .  /* If this is
245e0 20 61 6e 20 45 58 50 4c 41 49 4e 2c 20 73 6b 69   an EXPLAIN, ski
245e1 70 20 74 68 69 73 20 73 74 65 70 20 2a 2f 0a 20  p this step */. 
245e2 20 69 66 28 20 70 50 61 72 73 65 2d 3e 65 78 70   if( pParse->exp
245e3 6c 61 69 6e 20 29 7b 0a 20 20 20 20 72 65 74 75  lain ){.    retu
245e4 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  rn;.  }.#endif..
245e5 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29    assert( v!=0 )
245e6 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
245e7 63 6f 6c 4e 61 6d 65 73 53 65 74 20 7c 7c 20 76  colNamesSet || v
245e8 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  ==0 || db->mallo
245e9 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e  cFailed ) return
245ea 3b 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f 6c 4e  ;.  pParse->colN
245eb 61 6d 65 73 53 65 74 20 3d 20 31 3b 0a 20 20 66  amesSet = 1;.  f
245ec 75 6c 6c 4e 61 6d 65 73 20 3d 20 28 64 62 2d 3e  ullNames = (db->
245ed 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46  flags & SQLITE_F
245ee 75 6c 6c 43 6f 6c 4e 61 6d 65 73 29 21 3d 30 3b  ullColNames)!=0;
245ef 0a 20 20 73 68 6f 72 74 4e 61 6d 65 73 20 3d 20  .  shortNames = 
245f0 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  (db->flags & SQL
245f1 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65  ITE_ShortColName
245f2 73 29 21 3d 30 3b 0a 20 20 73 71 6c 69 74 65 33  s)!=0;.  sqlite3
245f3 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
245f4 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29  , pEList->nExpr)
245f5 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
245f6 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b  EList->nExpr; i+
245f7 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 3b  +){.    Expr *p;
245f8 0a 20 20 20 20 70 20 3d 20 70 45 4c 69 73 74 2d  .    p = pEList-
245f9 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[i].pExpr;.   
245fa 20 69 66 28 20 70 3d 3d 30 20 29 20 63 6f 6e 74   if( p==0 ) cont
245fb 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 70 45  inue;.    if( pE
245fc 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  List->a[i].zName
245fd 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a   ){.      char *
245fe 7a 4e 61 6d 65 20 3d 20 70 45 4c 69 73 74 2d 3e  zName = pEList->
245ff 61 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  a[i].zName;.    
24600 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
24601 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f  ColName(v, i, CO
24602 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4e 61 6d  LNAME_NAME, zNam
24603 65 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  e, SQLITE_TRANSI
24604 45 4e 54 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  ENT);.    }else 
24605 69 66 28 20 28 70 2d 3e 6f 70 3d 3d 54 4b 5f 43  if( (p->op==TK_C
24606 4f 4c 55 4d 4e 20 7c 7c 20 70 2d 3e 6f 70 3d 3d  OLUMN || p->op==
24607 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 29 20 26  TK_AGG_COLUMN) &
24608 26 20 70 54 61 62 4c 69 73 74 20 29 7b 0a 20 20  & pTabList ){.  
24609 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b      Table *pTab;
2460a 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 43 6f  .      char *zCo
2460b 6c 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f  l;.      int iCo
2460c 6c 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a  l = p->iColumn;.
2460d 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
2460e 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 20  <pTabList->nSrc 
2460f 26 26 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a  && pTabList->a[j
24610 5d 2e 69 43 75 72 73 6f 72 21 3d 70 2d 3e 69 54  ].iCursor!=p->iT
24611 61 62 6c 65 3b 20 6a 2b 2b 29 7b 7d 0a 20 20 20  able; j++){}.   
24612 20 20 20 61 73 73 65 72 74 28 20 6a 3c 70 54 61     assert( j<pTa
24613 62 4c 69 73 74 2d 3e 6e 53 72 63 20 29 3b 0a 20  bList->nSrc );. 
24614 20 20 20 20 20 70 54 61 62 20 3d 20 70 54 61 62       pTab = pTab
24615 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 54 61 62 3b  List->a[j].pTab;
24616 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c  .      if( iCol<
24617 30 20 29 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d  0 ) iCol = pTab-
24618 3e 69 50 4b 65 79 3b 0a 20 20 20 20 20 20 61 73  >iPKey;.      as
24619 73 65 72 74 28 20 69 43 6f 6c 3d 3d 2d 31 20 7c  sert( iCol==-1 |
2461a 7c 20 28 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43  | (iCol>=0 && iC
2461b 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 29 20 29  ol<pTab->nCol) )
2461c 3b 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c  ;.      if( iCol
2461d 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 43  <0 ){.        zC
2461e 6f 6c 20 3d 20 22 72 6f 77 69 64 22 3b 0a 20 20  ol = "rowid";.  
2461f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
24620 20 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e     zCol = pTab->
24621 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65  aCol[iCol].zName
24622 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
24623 69 66 28 20 21 73 68 6f 72 74 4e 61 6d 65 73 20  if( !shortNames 
24624 26 26 20 21 66 75 6c 6c 4e 61 6d 65 73 20 29 7b  && !fullNames ){
24625 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
24626 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
24627 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , i, COLNAME_NAM
24628 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  E, .            
24629 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70  sqlite3DbStrNDup
2462a 28 64 62 2c 20 28 63 68 61 72 2a 29 70 2d 3e 73  (db, (char*)p->s
2462b 70 61 6e 2e 7a 2c 20 70 2d 3e 73 70 61 6e 2e 6e  pan.z, p->span.n
2462c 29 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49  ), SQLITE_DYNAMI
2462d 43 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  C);.      }else 
2462e 69 66 28 20 66 75 6c 6c 4e 61 6d 65 73 20 7c 7c  if( fullNames ||
2462f 20 28 21 73 68 6f 72 74 4e 61 6d 65 73 20 26 26   (!shortNames &&
24630 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3e   pTabList->nSrc>
24631 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68  1) ){.        ch
24632 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20  ar *zName = 0;. 
24633 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 61         char *zTa
24634 62 3b 0a 20 0a 20 20 20 20 20 20 20 20 7a 54 61  b;. .        zTa
24635 62 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b  b = pTabList->a[
24636 6a 5d 2e 7a 41 6c 69 61 73 3b 0a 20 20 20 20 20  j].zAlias;.     
24637 20 20 20 69 66 28 20 66 75 6c 6c 4e 61 6d 65 73     if( fullNames
24638 20 7c 7c 20 7a 54 61 62 3d 3d 30 20 29 20 7a 54   || zTab==0 ) zT
24639 61 62 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ab = pTab->zName
2463a 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 20  ;.        zName 
2463b 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
2463c 28 64 62 2c 20 22 25 73 2e 25 73 22 2c 20 7a 54  (db, "%s.%s", zT
2463d 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 20  ab, zCol);.     
2463e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
2463f 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43  tColName(v, i, C
24640 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4e 61  OLNAME_NAME, zNa
24641 6d 65 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d  me, SQLITE_DYNAM
24642 49 43 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  IC);.      }else
24643 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
24644 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
24645 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, i, COLNAME_NA
24646 4d 45 2c 20 7a 43 6f 6c 2c 20 53 51 4c 49 54 45  ME, zCol, SQLITE
24647 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20  _TRANSIENT);.   
24648 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
24649 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2464a 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69  eSetColName(v, i
2464b 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
2464c 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2464d 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20  e3DbStrNDup(db, 
2464e 28 63 68 61 72 2a 29 70 2d 3e 73 70 61 6e 2e 7a  (char*)p->span.z
2464f 2c 20 70 2d 3e 73 70 61 6e 2e 6e 29 2c 20 53 51  , p->span.n), SQ
24650 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  LITE_DYNAMIC);. 
24651 20 20 20 7d 0a 20 20 7d 0a 20 20 67 65 6e 65 72     }.  }.  gener
24652 61 74 65 43 6f 6c 75 6d 6e 54 79 70 65 73 28 70  ateColumnTypes(p
24653 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c  Parse, pTabList,
24654 20 70 45 4c 69 73 74 29 3b 0a 7d 0a 0a 23 69 66   pEList);.}..#if
24655 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
24656 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
24657 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f 66 20 74  ./*.** Name of t
24658 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 70  he connection op
24659 65 72 61 74 6f 72 2c 20 75 73 65 64 20 66 6f 72  erator, used for
2465a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 2e   error messages.
2465b 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
2465c 20 63 68 61 72 20 2a 73 65 6c 65 63 74 4f 70 4e   char *selectOpN
2465d 61 6d 65 28 69 6e 74 20 69 64 29 7b 0a 20 20 63  ame(int id){.  c
2465e 68 61 72 20 2a 7a 3b 0a 20 20 73 77 69 74 63 68  har *z;.  switch
2465f 28 20 69 64 20 29 7b 0a 20 20 20 20 63 61 73 65  ( id ){.    case
24660 20 54 4b 5f 41 4c 4c 3a 20 20 20 20 20 20 20 7a   TK_ALL:       z
24661 20 3d 20 22 55 4e 49 4f 4e 20 41 4c 4c 22 3b 20   = "UNION ALL"; 
24662 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
24663 65 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 3a 20  e TK_INTERSECT: 
24664 7a 20 3d 20 22 49 4e 54 45 52 53 45 43 54 22 3b  z = "INTERSECT";
24665 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
24666 73 65 20 54 4b 5f 45 58 43 45 50 54 3a 20 20 20  se TK_EXCEPT:   
24667 20 7a 20 3d 20 22 45 58 43 45 50 54 22 3b 20 20   z = "EXCEPT";  
24668 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64      break;.    d
24669 65 66 61 75 6c 74 3a 20 20 20 20 20 20 20 20 20  efault:         
2466a 20 20 7a 20 3d 20 22 55 4e 49 4f 4e 22 3b 20 20    z = "UNION";  
2466b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a       break;.  }.
2466c 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 23 65    return z;.}.#e
2466d 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2466e 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MIT_COMPOUND_SEL
2466f 45 43 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 69  ECT */../*.** Gi
24670 76 65 6e 20 61 20 61 6e 20 65 78 70 72 65 73 73  ven a an express
24671 69 6f 6e 20 6c 69 73 74 20 28 77 68 69 63 68 20  ion list (which 
24672 69 73 20 72 65 61 6c 6c 79 20 74 68 65 20 6c 69  is really the li
24673 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e  st of expression
24674 73 0a 2a 2a 20 74 68 61 74 20 66 6f 72 6d 20 74  s.** that form t
24675 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  he result set of
24676 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
24677 65 6e 74 29 20 63 6f 6d 70 75 74 65 20 61 70 70  ent) compute app
24678 72 6f 70 72 69 61 74 65 0a 2a 2a 20 63 6f 6c 75  ropriate.** colu
24679 6d 6e 20 6e 61 6d 65 73 20 66 6f 72 20 61 20 74  mn names for a t
2467a 61 62 6c 65 20 74 68 61 74 20 77 6f 75 6c 64 20  able that would 
2467b 68 6f 6c 64 20 74 68 65 20 65 78 70 72 65 73 73  hold the express
2467c 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  ion list..**.** 
2467d 41 6c 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  All column names
2467e 20 77 69 6c 6c 20 62 65 20 75 6e 69 71 75 65 2e   will be unique.
2467f 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20  .**.** Only the 
24680 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 72 65  column names are
24681 20 63 6f 6d 70 75 74 65 64 2e 20 20 43 6f 6c 75   computed.  Colu
24682 6d 6e 2e 7a 54 79 70 65 2c 20 43 6f 6c 75 6d 6e  mn.zType, Column
24683 2e 7a 43 6f 6c 6c 2c 0a 2a 2a 20 61 6e 64 20 6f  .zColl,.** and o
24684 74 68 65 72 20 66 69 65 6c 64 73 20 6f 66 20 43  ther fields of C
24685 6f 6c 75 6d 6e 20 61 72 65 20 7a 65 72 6f 65 64  olumn are zeroed
24686 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ..**.** Return S
24687 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63  QLITE_OK on succ
24688 65 73 73 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72  ess.  If a memor
24689 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72  y allocation err
2468a 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 73 74  or occurs,.** st
2468b 6f 72 65 20 4e 55 4c 4c 20 69 6e 20 2a 70 61 43  ore NULL in *paC
2468c 6f 6c 20 61 6e 64 20 30 20 69 6e 20 2a 70 6e 43  ol and 0 in *pnC
2468d 6f 6c 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51  ol and return SQ
2468e 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a 73  LITE_NOMEM..*/.s
2468f 74 61 74 69 63 20 69 6e 74 20 73 65 6c 65 63 74  tatic int select
24690 43 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78 70 72 4c  ColumnsFromExprL
24691 69 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ist(.  Parse *pP
24692 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  arse,          /
24693 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
24694 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  t */.  ExprList 
24695 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 20 20 2f  *pEList,       /
24696 2a 20 45 78 70 72 20 6c 69 73 74 20 66 72 6f 6d  * Expr list from
24697 20 77 68 69 63 68 20 74 6f 20 64 65 72 69 76 65   which to derive
24698 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a 2f   column names */
24699 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6c 2c 20 20  .  int *pnCol,  
2469a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
2469b 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ite the number o
2469c 66 20 63 6f 6c 75 6d 6e 73 20 68 65 72 65 20 2a  f columns here *
2469d 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 2a 70 61 43  /.  Column **paC
2469e 6f 6c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57  ol          /* W
2469f 72 69 74 65 20 74 68 65 20 6e 65 77 20 63 6f 6c  rite the new col
246a0 75 6d 6e 20 6c 69 73 74 20 68 65 72 65 20 2a 2f  umn list here */
246a1 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
246a2 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
246a3 20 20 69 6e 74 20 69 2c 20 6a 2c 20 63 6e 74 3b    int i, j, cnt;
246a4 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61 43 6f 6c 2c  .  Column *aCol,
246a5 20 2a 70 43 6f 6c 3b 0a 20 20 69 6e 74 20 6e 43   *pCol;.  int nC
246a6 6f 6c 3b 0a 20 20 45 78 70 72 20 2a 70 3b 0a 20  ol;.  Expr *p;. 
246a7 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20   char *zName;.  
246a8 69 6e 74 20 6e 4e 61 6d 65 3b 0a 0a 20 20 2a 70  int nName;..  *p
246a9 6e 43 6f 6c 20 3d 20 6e 43 6f 6c 20 3d 20 70 45  nCol = nCol = pE
246aa 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 61  List->nExpr;.  a
246ab 43 6f 6c 20 3d 20 2a 70 61 43 6f 6c 20 3d 20 73  Col = *paCol = s
246ac 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
246ad 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 61 43  ro(db, sizeof(aC
246ae 6f 6c 5b 30 5d 29 2a 6e 43 6f 6c 29 3b 0a 20 20  ol[0])*nCol);.  
246af 69 66 28 20 61 43 6f 6c 3d 3d 30 20 29 20 72 65  if( aCol==0 ) re
246b0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
246b1 4d 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 43  M;.  for(i=0, pC
246b2 6f 6c 3d 61 43 6f 6c 3b 20 69 3c 6e 43 6f 6c 3b  ol=aCol; i<nCol;
246b3 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20   i++, pCol++){. 
246b4 20 20 20 2f 2a 20 47 65 74 20 61 6e 20 61 70 70     /* Get an app
246b5 72 6f 70 72 69 61 74 65 20 6e 61 6d 65 20 66 6f  ropriate name fo
246b6 72 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 20 20 20  r the column.   
246b7 20 2a 2f 0a 20 20 20 20 70 20 3d 20 70 45 4c 69   */.    p = pELi
246b8 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a  st->a[i].pExpr;.
246b9 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
246ba 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70  Right==0 || p->p
246bb 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 2e 7a 3d 3d  Right->token.z==
246bc 30 20 7c 7c 20 70 2d 3e 70 52 69 67 68 74 2d 3e  0 || p->pRight->
246bd 74 6f 6b 65 6e 2e 7a 5b 30 5d 21 3d 30 20 29 3b  token.z[0]!=0 );
246be 0a 20 20 20 20 69 66 28 20 28 7a 4e 61 6d 65 20  .    if( (zName 
246bf 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a  = pEList->a[i].z
246c0 4e 61 6d 65 29 21 3d 30 20 29 7b 0a 20 20 20 20  Name)!=0 ){.    
246c1 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6f 6c 75    /* If the colu
246c2 6d 6e 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 22  mn contains an "
246c3 41 53 20 3c 6e 61 6d 65 3e 22 20 70 68 72 61 73  AS <name>" phras
246c4 65 2c 20 75 73 65 20 3c 6e 61 6d 65 3e 20 61 73  e, use <name> as
246c5 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20   the name */.   
246c6 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74     zName = sqlit
246c7 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a  e3DbStrDup(db, z
246c8 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Name);.    }else
246c9 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 43  {.      Expr *pC
246ca 6f 6c 20 3d 20 70 3b 0a 20 20 20 20 20 20 54 61  ol = p;.      Ta
246cb 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 20  ble *pTab;.     
246cc 20 77 68 69 6c 65 28 20 70 43 6f 6c 2d 3e 6f 70   while( pCol->op
246cd 3d 3d 54 4b 5f 44 4f 54 20 29 20 70 43 6f 6c 20  ==TK_DOT ) pCol 
246ce 3d 20 70 43 6f 6c 2d 3e 70 52 69 67 68 74 3b 0a  = pCol->pRight;.
246cf 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e        if( pCol->
246d0 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26  op==TK_COLUMN &&
246d1 20 28 70 54 61 62 20 3d 20 70 43 6f 6c 2d 3e 70   (pTab = pCol->p
246d2 54 61 62 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  Tab)!=0 ){.     
246d3 20 20 20 2f 2a 20 46 6f 72 20 63 6f 6c 75 6d 6e     /* For column
246d4 73 20 75 73 65 20 74 68 65 20 63 6f 6c 75 6d 6e  s use the column
246d5 20 6e 61 6d 65 20 6e 61 6d 65 20 2a 2f 0a 20 20   name name */.  
246d6 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d        int iCol =
246d7 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a   pCol->iColumn;.
246d8 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c          if( iCol
246d9 3c 30 20 29 20 69 43 6f 6c 20 3d 20 70 54 61 62  <0 ) iCol = pTab
246da 2d 3e 69 50 4b 65 79 3b 0a 20 20 20 20 20 20 20  ->iPKey;.       
246db 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   zName = sqlite3
246dc 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 22  MPrintf(db, "%s"
246dd 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
246de 20 20 20 69 43 6f 6c 3e 3d 30 20 3f 20 70 54 61     iCol>=0 ? pTa
246df 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e  b->aCol[iCol].zN
246e0 61 6d 65 20 3a 20 22 72 6f 77 69 64 22 29 3b 0a  ame : "rowid");.
246e1 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
246e2 20 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20       /* Use the 
246e3 6f 72 69 67 69 6e 61 6c 20 74 65 78 74 20 6f 66  original text of
246e4 20 74 68 65 20 63 6f 6c 75 6d 6e 20 65 78 70 72   the column expr
246e5 65 73 73 69 6f 6e 20 61 73 20 69 74 73 20 6e 61  ession as its na
246e6 6d 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 7a 4e  me */.        zN
246e7 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  ame = sqlite3MPr
246e8 69 6e 74 66 28 64 62 2c 20 22 25 54 22 2c 20 26  intf(db, "%T", &
246e9 70 43 6f 6c 2d 3e 73 70 61 6e 29 3b 0a 20 20 20  pCol->span);.   
246ea 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
246eb 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
246ec 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  led ){.      sql
246ed 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
246ee 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 62 72 65  Name);.      bre
246ef 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  ak;.    }.    sq
246f0 6c 69 74 65 33 44 65 71 75 6f 74 65 28 7a 4e 61  lite3Dequote(zNa
246f1 6d 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b  me);..    /* Mak
246f2 65 20 73 75 72 65 20 74 68 65 20 63 6f 6c 75 6d  e sure the colum
246f3 6e 20 6e 61 6d 65 20 69 73 20 75 6e 69 71 75 65  n name is unique
246f4 2e 20 20 49 66 20 74 68 65 20 6e 61 6d 65 20 69  .  If the name i
246f5 73 20 6e 6f 74 20 75 6e 69 71 75 65 2c 0a 20 20  s not unique,.  
246f6 20 20 2a 2a 20 61 70 70 65 6e 64 20 61 20 69 6e    ** append a in
246f7 74 65 67 65 72 20 74 6f 20 74 68 65 20 6e 61 6d  teger to the nam
246f8 65 20 73 6f 20 74 68 61 74 20 69 74 20 62 65 63  e so that it bec
246f9 6f 6d 65 73 20 75 6e 69 71 75 65 2e 0a 20 20 20  omes unique..   
246fa 20 2a 2f 0a 20 20 20 20 6e 4e 61 6d 65 20 3d 20   */.    nName = 
246fb 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 3b 0a 20  strlen(zName);. 
246fc 20 20 20 66 6f 72 28 6a 3d 63 6e 74 3d 30 3b 20     for(j=cnt=0; 
246fd 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  j<i; j++){.     
246fe 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
246ff 43 6d 70 28 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d  Cmp(aCol[j].zNam
24700 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a  e, zName)==0 ){.
24701 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e          char *zN
24702 65 77 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20  ewName;.        
24703 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d 20 30  zName[nName] = 0
24704 3b 0a 20 20 20 20 20 20 20 20 7a 4e 65 77 4e 61  ;.        zNewNa
24705 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  me = sqlite3MPri
24706 6e 74 66 28 64 62 2c 20 22 25 73 3a 25 64 22 2c  ntf(db, "%s:%d",
24707 20 7a 4e 61 6d 65 2c 20 2b 2b 63 6e 74 29 3b 0a   zName, ++cnt);.
24708 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
24709 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29  bFree(db, zName)
2470a 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 20  ;.        zName 
2470b 3d 20 7a 4e 65 77 4e 61 6d 65 3b 0a 20 20 20 20  = zNewName;.    
2470c 20 20 20 20 6a 20 3d 20 2d 31 3b 0a 20 20 20 20      j = -1;.    
2470d 20 20 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30      if( zName==0
2470e 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
2470f 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 6f 6c  }.    }.    pCol
24710 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b  ->zName = zName;
24711 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d  .  }.  if( db->m
24712 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
24713 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 66 6f     int j;.    fo
24714 72 28 6a 3d 30 3b 20 6a 3c 69 3b 20 6a 2b 2b 29  r(j=0; j<i; j++)
24715 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  {.      sqlite3D
24716 62 46 72 65 65 28 64 62 2c 20 61 43 6f 6c 5b 6a  bFree(db, aCol[j
24717 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a  ].zName);.    }.
24718 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
24719 65 28 64 62 2c 20 61 43 6f 6c 29 3b 0a 20 20 20  e(db, aCol);.   
2471a 20 2a 70 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20   *paCol = 0;.   
2471b 20 2a 70 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 20   *pnCol = 0;.   
2471c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
2471d 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 74 75  OMEM;.  }.  retu
2471e 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
2471f 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74 79 70 65 20  ./*.** Add type 
24720 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 69 6e  and collation in
24721 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 61 20 63  formation to a c
24722 6f 6c 75 6d 6e 20 6c 69 73 74 20 62 61 73 65 64  olumn list based
24723 20 6f 6e 0a 2a 2a 20 61 20 53 45 4c 45 43 54 20   on.** a SELECT 
24724 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 0a 2a  statement..** .*
24725 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6c 69 73  * The column lis
24726 74 20 70 72 65 73 75 6d 61 62 6c 79 20 63 61 6d  t presumably cam
24727 65 20 66 72 6f 6d 20 73 65 6c 65 63 74 43 6f 6c  e from selectCol
24728 75 6d 6e 4e 61 6d 65 73 46 72 6f 6d 45 78 70 72  umnNamesFromExpr
24729 4c 69 73 74 28 29 2e 0a 2a 2a 20 54 68 65 20 63  List()..** The c
2472a 6f 6c 75 6d 6e 20 6c 69 73 74 20 68 61 73 20 6f  olumn list has o
2472b 6e 6c 79 20 6e 61 6d 65 73 2c 20 6e 6f 74 20 74  nly names, not t
2472c 79 70 65 73 20 6f 72 20 63 6f 6c 6c 61 74 69 6f  ypes or collatio
2472d 6e 73 2e 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75  ns.  This.** rou
2472e 74 69 6e 65 20 67 6f 65 73 20 74 68 72 6f 75 67  tine goes throug
2472f 68 20 61 6e 64 20 61 64 64 73 20 74 68 65 20 74  h and adds the t
24730 79 70 65 73 20 61 6e 64 20 63 6f 6c 6c 61 74 69  ypes and collati
24731 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ons..**.** This 
24732 72 6f 75 74 69 6e 65 20 72 65 71 75 69 72 65 73  routine requires
24733 20 74 68 61 74 20 61 6c 6c 20 69 6e 64 65 6e 74   that all indent
24734 69 66 69 65 72 73 20 69 6e 20 74 68 65 20 53 45  ifiers in the SE
24735 4c 45 43 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  LECT.** statemen
24736 74 20 62 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a  t be resolved..*
24737 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65  /.static void se
24738 6c 65 63 74 41 64 64 43 6f 6c 75 6d 6e 54 79 70  lectAddColumnTyp
24739 65 41 6e 64 43 6f 6c 6c 61 74 69 6f 6e 28 0a 20  eAndCollation(. 
2473a 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2473b 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
2473c 67 20 63 6f 6e 74 65 78 74 73 20 2a 2f 0a 20 20  g contexts */.  
2473d 69 6e 74 20 6e 43 6f 6c 2c 20 20 20 20 20 20 20  int nCol,       
2473e 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2473f 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20  of columns */.  
24740 43 6f 6c 75 6d 6e 20 2a 61 43 6f 6c 2c 20 20 20  Column *aCol,   
24741 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66        /* List of
24742 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 53 65   columns */.  Se
24743 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 20 20  lect *pSelect   
24744 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 75 73      /* SELECT us
24745 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ed to determine 
24746 74 79 70 65 73 20 61 6e 64 20 63 6f 6c 6c 61 74  types and collat
24747 69 6f 6e 73 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  ions */.){.  sql
24748 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
24749 65 2d 3e 64 62 3b 0a 20 20 4e 61 6d 65 43 6f 6e  e->db;.  NameCon
2474a 74 65 78 74 20 73 4e 43 3b 0a 20 20 43 6f 6c 75  text sNC;.  Colu
2474b 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 43 6f 6c 6c  mn *pCol;.  Coll
2474c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 6e  Seq *pColl;.  in
2474d 74 20 69 3b 0a 20 20 45 78 70 72 20 2a 70 3b 0a  t i;.  Expr *p;.
2474e 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
2474f 74 5f 69 74 65 6d 20 2a 61 3b 0a 0a 20 20 61 73  t_item *a;..  as
24750 73 65 72 74 28 20 70 53 65 6c 65 63 74 21 3d 30  sert( pSelect!=0
24751 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   );.  assert( (p
24752 53 65 6c 65 63 74 2d 3e 73 65 6c 46 6c 61 67 73  Select->selFlags
24753 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 29 21   & SF_Resolved)!
24754 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
24755 6e 43 6f 6c 3d 3d 70 53 65 6c 65 63 74 2d 3e 70  nCol==pSelect->p
24756 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 7c 7c 20  EList->nExpr || 
24757 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
24758 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61   );.  if( db->ma
24759 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74  llocFailed ) ret
2475a 75 72 6e 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73  urn;.  memset(&s
2475b 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e  NC, 0, sizeof(sN
2475c 43 29 29 3b 0a 20 20 73 4e 43 2e 70 53 72 63 4c  C));.  sNC.pSrcL
2475d 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70  ist = pSelect->p
2475e 53 72 63 3b 0a 20 20 61 20 3d 20 70 53 65 6c 65  Src;.  a = pSele
2475f 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 61 3b 0a 20  ct->pEList->a;. 
24760 20 66 6f 72 28 69 3d 30 2c 20 70 43 6f 6c 3d 61   for(i=0, pCol=a
24761 43 6f 6c 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b  Col; i<nCol; i++
24762 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 70  , pCol++){.    p
24763 20 3d 20 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20   = a[i].pExpr;. 
24764 20 20 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d     pCol->zType =
24765 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
24766 28 64 62 2c 20 63 6f 6c 75 6d 6e 54 79 70 65 28  (db, columnType(
24767 26 73 4e 43 2c 20 70 2c 20 30 2c 20 30 2c 20 30  &sNC, p, 0, 0, 0
24768 29 29 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 61 66  ));.    pCol->af
24769 66 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33  finity = sqlite3
2476a 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 29 3b  ExprAffinity(p);
2476b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c  .    pColl = sql
2476c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28  ite3ExprCollSeq(
2476d 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 20 20  pParse, p);.    
2476e 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20  if( pColl ){.   
2476f 20 20 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 20 3d     pCol->zColl =
24770 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
24771 28 64 62 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d  (db, pColl->zNam
24772 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  e);.    }.  }.}.
24773 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 53  ./*.** Given a S
24774 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2c  ELECT statement,
24775 20 67 65 6e 65 72 61 74 65 20 61 20 54 61 62 6c   generate a Tabl
24776 65 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  e structure that
24777 20 64 65 73 63 72 69 62 65 73 0a 2a 2a 20 74 68   describes.** th
24778 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20  e result set of 
24779 74 68 61 74 20 53 45 4c 45 43 54 2e 0a 2a 2f 0a  that SELECT..*/.
2477a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54  SQLITE_PRIVATE T
2477b 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 52 65 73  able *sqlite3Res
2477c 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28 50  ultSetOfSelect(P
2477d 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 65  arse *pParse, Se
2477e 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a  lect *pSelect){.
2477f 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20    Table *pTab;. 
24780 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
24781 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74  Parse->db;.  int
24782 20 73 61 76 65 64 46 6c 61 67 73 3b 0a 0a 20 20   savedFlags;..  
24783 73 61 76 65 64 46 6c 61 67 73 20 3d 20 64 62 2d  savedFlags = db-
24784 3e 66 6c 61 67 73 3b 0a 20 20 64 62 2d 3e 66 6c  >flags;.  db->fl
24785 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 46  ags &= ~SQLITE_F
24786 75 6c 6c 43 6f 6c 4e 61 6d 65 73 3b 0a 20 20 64  ullColNames;.  d
24787 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49  b->flags |= SQLI
24788 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73  TE_ShortColNames
24789 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ;.  sqlite3Selec
2478a 74 50 72 65 70 28 70 50 61 72 73 65 2c 20 70 53  tPrep(pParse, pS
2478b 65 6c 65 63 74 2c 20 30 29 3b 0a 20 20 69 66 28  elect, 0);.  if(
2478c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 20   pParse->nErr ) 
2478d 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c  return 0;.  whil
2478e 65 28 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69  e( pSelect->pPri
2478f 6f 72 20 29 20 70 53 65 6c 65 63 74 20 3d 20 70  or ) pSelect = p
24790 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 3b 0a  Select->pPrior;.
24791 20 20 64 62 2d 3e 66 6c 61 67 73 20 3d 20 73 61    db->flags = sa
24792 76 65 64 46 6c 61 67 73 3b 0a 20 20 70 54 61 62  vedFlags;.  pTab
24793 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
24794 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
24795 66 28 54 61 62 6c 65 29 20 29 3b 0a 20 20 69 66  f(Table) );.  if
24796 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20  ( pTab==0 ){.   
24797 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
24798 20 70 54 61 62 2d 3e 64 62 20 3d 20 64 62 3b 0a   pTab->db = db;.
24799 20 20 70 54 61 62 2d 3e 6e 52 65 66 20 3d 20 31    pTab->nRef = 1
2479a 3b 0a 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 20  ;.  pTab->zName 
2479b 3d 20 30 3b 0a 20 20 73 65 6c 65 63 74 43 6f 6c  = 0;.  selectCol
2479c 75 6d 6e 73 46 72 6f 6d 45 78 70 72 4c 69 73 74  umnsFromExprList
2479d 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74  (pParse, pSelect
2479e 2d 3e 70 45 4c 69 73 74 2c 20 26 70 54 61 62 2d  ->pEList, &pTab-
2479f 3e 6e 43 6f 6c 2c 20 26 70 54 61 62 2d 3e 61 43  >nCol, &pTab->aC
247a0 6f 6c 29 3b 0a 20 20 73 65 6c 65 63 74 41 64 64  ol);.  selectAdd
247a1 43 6f 6c 75 6d 6e 54 79 70 65 41 6e 64 43 6f 6c  ColumnTypeAndCol
247a2 6c 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70  lation(pParse, p
247a3 54 61 62 2d 3e 6e 43 6f 6c 2c 20 70 54 61 62 2d  Tab->nCol, pTab-
247a4 3e 61 43 6f 6c 2c 20 70 53 65 6c 65 63 74 29 3b  >aCol, pSelect);
247a5 0a 20 20 70 54 61 62 2d 3e 69 50 4b 65 79 20 3d  .  pTab->iPKey =
247a6 20 2d 31 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d   -1;.  if( db->m
247a7 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
247a8 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65     sqlite3Delete
247a9 54 61 62 6c 65 28 70 54 61 62 29 3b 0a 20 20 20  Table(pTab);.   
247aa 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
247ab 20 72 65 74 75 72 6e 20 70 54 61 62 3b 0a 7d 0a   return pTab;.}.
247ac 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 56 44 42  ./*.** Get a VDB
247ad 45 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20  E for the given 
247ae 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 2e 20  parser context. 
247af 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 6f 6e   Create a new on
247b0 65 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a  e if necessary..
247b1 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  ** If an error o
247b2 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 4e 55  ccurs, return NU
247b3 4c 4c 20 61 6e 64 20 6c 65 61 76 65 20 61 20 6d  LL and leave a m
247b4 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65  essage in pParse
247b5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
247b6 41 54 45 20 56 64 62 65 20 2a 73 71 6c 69 74 65  ATE Vdbe *sqlite
247b7 33 47 65 74 56 64 62 65 28 50 61 72 73 65 20 2a  3GetVdbe(Parse *
247b8 70 50 61 72 73 65 29 7b 0a 20 20 56 64 62 65 20  pParse){.  Vdbe 
247b9 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
247ba 62 65 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29  be;.  if( v==0 )
247bb 7b 0a 20 20 20 20 76 20 3d 20 70 50 61 72 73 65  {.    v = pParse
247bc 2d 3e 70 56 64 62 65 20 3d 20 73 71 6c 69 74 65  ->pVdbe = sqlite
247bd 33 56 64 62 65 43 72 65 61 74 65 28 70 50 61 72  3VdbeCreate(pPar
247be 73 65 2d 3e 64 62 29 3b 0a 23 69 66 6e 64 65 66  se->db);.#ifndef
247bf 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41   SQLITE_OMIT_TRA
247c0 43 45 0a 20 20 20 20 69 66 28 20 76 20 29 7b 0a  CE.    if( v ){.
247c1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
247c2 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 54 72  eAddOp0(v, OP_Tr
247c3 61 63 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  ace);.    }.#end
247c4 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  if.  }.  return 
247c5 76 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d  v;.}.../*.** Com
247c6 70 75 74 65 20 74 68 65 20 69 4c 69 6d 69 74 20  pute the iLimit 
247c7 61 6e 64 20 69 4f 66 66 73 65 74 20 66 69 65 6c  and iOffset fiel
247c8 64 73 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  ds of the SELECT
247c9 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a   based on the.**
247ca 20 70 4c 69 6d 69 74 20 61 6e 64 20 70 4f 66 66   pLimit and pOff
247cb 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 73 2e  set expressions.
247cc 20 20 70 4c 69 6d 69 74 20 61 6e 64 20 70 4f 66    pLimit and pOf
247cd 66 73 65 74 20 68 6f 6c 64 20 74 68 65 20 65 78  fset hold the ex
247ce 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 74 68 61  pressions.** tha
247cf 74 20 61 70 70 65 61 72 20 69 6e 20 74 68 65 20  t appear in the 
247d0 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61  original SQL sta
247d1 74 65 6d 65 6e 74 20 61 66 74 65 72 20 74 68 65  tement after the
247d2 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45   LIMIT and OFFSE
247d3 54 0a 2a 2a 20 6b 65 79 77 6f 72 64 73 2e 20 20  T.** keywords.  
247d4 4f 72 20 4e 55 4c 4c 20 69 66 20 74 68 6f 73 65  Or NULL if those
247d5 20 6b 65 79 77 6f 72 64 73 20 61 72 65 20 6f 6d   keywords are om
247d6 69 74 74 65 64 2e 20 69 4c 69 6d 69 74 20 61 6e  itted. iLimit an
247d7 64 20 69 4f 66 66 73 65 74 20 0a 2a 2a 20 61 72  d iOffset .** ar
247d8 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 6d 65  e the integer me
247d9 6d 6f 72 79 20 72 65 67 69 73 74 65 72 20 6e 75  mory register nu
247da 6d 62 65 72 73 20 66 6f 72 20 63 6f 75 6e 74 65  mbers for counte
247db 72 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75  rs used to compu
247dc 74 65 20 0a 2a 2a 20 74 68 65 20 6c 69 6d 69 74  te .** the limit
247dd 20 61 6e 64 20 6f 66 66 73 65 74 2e 20 20 49 66   and offset.  If
247de 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6c 69 6d   there is no lim
247df 69 74 20 61 6e 64 2f 6f 72 20 6f 66 66 73 65 74  it and/or offset
247e0 2c 20 74 68 65 6e 20 0a 2a 2a 20 69 4c 69 6d 69  , then .** iLimi
247e1 74 20 61 6e 64 20 69 4f 66 66 73 65 74 20 61 72  t and iOffset ar
247e2 65 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a 2a  e negative..**.*
247e3 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
247e4 68 61 6e 67 65 73 20 74 68 65 20 76 61 6c 75 65  hanges the value
247e5 73 20 6f 66 20 69 4c 69 6d 69 74 20 61 6e 64 20  s of iLimit and 
247e6 69 4f 66 66 73 65 74 20 6f 6e 6c 79 20 69 66 0a  iOffset only if.
247e7 2a 2a 20 61 20 6c 69 6d 69 74 20 6f 72 20 6f 66  ** a limit or of
247e8 66 73 65 74 20 69 73 20 64 65 66 69 6e 65 64 20  fset is defined 
247e9 62 79 20 70 4c 69 6d 69 74 20 61 6e 64 20 70 4f  by pLimit and pO
247ea 66 66 73 65 74 2e 20 20 69 4c 69 6d 69 74 20 61  ffset.  iLimit a
247eb 6e 64 0a 2a 2a 20 69 4f 66 66 73 65 74 20 73 68  nd.** iOffset sh
247ec 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 70  ould have been p
247ed 72 65 73 65 74 20 74 6f 20 61 70 70 72 6f 70 72  reset to appropr
247ee 69 61 74 65 20 64 65 66 61 75 6c 74 20 76 61 6c  iate default val
247ef 75 65 73 0a 2a 2a 20 28 75 73 75 61 6c 6c 79 20  ues.** (usually 
247f0 62 75 74 20 6e 6f 74 20 61 6c 77 61 79 73 20 2d  but not always -
247f1 31 29 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c  1) prior to call
247f2 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ing this routine
247f3 2e 0a 2a 2a 20 4f 6e 6c 79 20 69 66 20 70 4c 69  ..** Only if pLi
247f4 6d 69 74 21 3d 30 20 6f 72 20 70 4f 66 66 73 65  mit!=0 or pOffse
247f5 74 21 3d 30 20 64 6f 20 74 68 65 20 6c 69 6d 69  t!=0 do the limi
247f6 74 20 72 65 67 69 73 74 65 72 73 20 67 65 74 0a  t registers get.
247f7 2a 2a 20 72 65 64 65 66 69 6e 65 64 2e 20 20 54  ** redefined.  T
247f8 68 65 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 70 65  he UNION ALL ope
247f9 72 61 74 6f 72 20 75 73 65 73 20 74 68 69 73 20  rator uses this 
247fa 70 72 6f 70 65 72 74 79 20 74 6f 20 66 6f 72 63  property to forc
247fb 65 0a 2a 2a 20 74 68 65 20 72 65 75 73 65 20 6f  e.** the reuse o
247fc 66 20 74 68 65 20 73 61 6d 65 20 6c 69 6d 69 74  f the same limit
247fd 20 61 6e 64 20 6f 66 66 73 65 74 20 72 65 67 69   and offset regi
247fe 73 74 65 72 73 20 61 63 72 6f 73 73 20 6d 75 6c  sters across mul
247ff 74 69 70 6c 65 0a 2a 2a 20 53 45 4c 45 43 54 20  tiple.** SELECT 
24800 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73  statements..*/.s
24801 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75  tatic void compu
24802 74 65 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73  teLimitRegisters
24803 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
24804 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 69  Select *p, int i
24805 42 72 65 61 6b 29 7b 0a 20 20 56 64 62 65 20 2a  Break){.  Vdbe *
24806 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 4c 69  v = 0;.  int iLi
24807 6d 69 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69  mit = 0;.  int i
24808 4f 66 66 73 65 74 3b 0a 20 20 69 6e 74 20 61 64  Offset;.  int ad
24809 64 72 31 3b 0a 20 20 69 66 28 20 70 2d 3e 69 4c  dr1;.  if( p->iL
2480a 69 6d 69 74 20 29 20 72 65 74 75 72 6e 3b 0a 0a  imit ) return;..
2480b 20 20 2f 2a 20 0a 20 20 2a 2a 20 22 4c 49 4d 49    /* .  ** "LIMI
2480c 54 20 2d 31 22 20 61 6c 77 61 79 73 20 73 68 6f  T -1" always sho
2480d 77 73 20 61 6c 6c 20 72 6f 77 73 2e 20 20 54 68  ws all rows.  Th
2480e 65 72 65 20 69 73 20 73 6f 6d 65 0a 20 20 2a 2a  ere is some.  **
2480f 20 63 6f 6e 74 72 61 76 65 72 73 79 20 61 62 6f   contraversy abo
24810 75 74 20 77 68 61 74 20 74 68 65 20 63 6f 72 72  ut what the corr
24811 65 63 74 20 62 65 68 61 76 69 6f 72 20 73 68 6f  ect behavior sho
24812 75 6c 64 20 62 65 2e 0a 20 20 2a 2a 20 54 68 65  uld be..  ** The
24813 20 63 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65   current impleme
24814 6e 74 61 74 69 6f 6e 20 69 6e 74 65 72 70 72 65  ntation interpre
24815 74 73 20 22 4c 49 4d 49 54 20 30 22 20 74 6f 20  ts "LIMIT 0" to 
24816 6d 65 61 6e 0a 20 20 2a 2a 20 6e 6f 20 72 6f 77  mean.  ** no row
24817 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d  s..  */.  if( p-
24818 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 70  >pLimit ){.    p
24819 2d 3e 69 4c 69 6d 69 74 20 3d 20 69 4c 69 6d 69  ->iLimit = iLimi
2481a 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  t = ++pParse->nM
2481b 65 6d 3b 0a 20 20 20 20 76 20 3d 20 73 71 6c 69  em;.    v = sqli
2481c 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
2481d 65 29 3b 0a 20 20 20 20 69 66 28 20 76 3d 3d 30  e);.    if( v==0
2481e 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 73   ) return;.    s
2481f 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
24820 50 61 72 73 65 2c 20 70 2d 3e 70 4c 69 6d 69 74  Parse, p->pLimit
24821 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 73  , iLimit);.    s
24822 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
24823 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74  (v, OP_MustBeInt
24824 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 56  , iLimit);.    V
24825 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
24826 4c 49 4d 49 54 20 63 6f 75 6e 74 65 72 22 29 29  LIMIT counter"))
24827 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
24828 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66  eAddOp2(v, OP_If
24829 5a 65 72 6f 2c 20 69 4c 69 6d 69 74 2c 20 69 42  Zero, iLimit, iB
2482a 72 65 61 6b 29 3b 0a 20 20 7d 0a 20 20 69 66 28  reak);.  }.  if(
2482b 20 70 2d 3e 70 4f 66 66 73 65 74 20 29 7b 0a 20   p->pOffset ){. 
2482c 20 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20     p->iOffset = 
2482d 69 4f 66 66 73 65 74 20 3d 20 2b 2b 70 50 61 72  iOffset = ++pPar
2482e 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 69 66  se->nMem;.    if
2482f 28 20 70 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20  ( p->pLimit ){. 
24830 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65       pParse->nMe
24831 6d 2b 2b 3b 20 20 20 2f 2a 20 41 6c 6c 6f 63 61  m++;   /* Alloca
24832 74 65 20 61 6e 20 65 78 74 72 61 20 72 65 67 69  te an extra regi
24833 73 74 65 72 20 66 6f 72 20 6c 69 6d 69 74 2b 6f  ster for limit+o
24834 66 66 73 65 74 20 2a 2f 0a 20 20 20 20 7d 0a 20  ffset */.    }. 
24835 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65     v = sqlite3Ge
24836 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
24837 20 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65     if( v==0 ) re
24838 74 75 72 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65  turn;.    sqlite
24839 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
2483a 2c 20 70 2d 3e 70 4f 66 66 73 65 74 2c 20 69 4f  , p->pOffset, iO
2483b 66 66 73 65 74 29 3b 0a 20 20 20 20 73 71 6c 69  ffset);.    sqli
2483c 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
2483d 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 69   OP_MustBeInt, i
2483e 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 56 64 62  Offset);.    Vdb
2483f 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4f 46  eComment((v, "OF
24840 46 53 45 54 20 63 6f 75 6e 74 65 72 22 29 29 3b  FSET counter"));
24841 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c  .    addr1 = sql
24842 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
24843 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 69 4f 66 66  , OP_IfPos, iOff
24844 73 65 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  set);.    sqlite
24845 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
24846 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 69 4f  P_Integer, 0, iO
24847 66 66 73 65 74 29 3b 0a 20 20 20 20 73 71 6c 69  ffset);.    sqli
24848 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
24849 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 20 20 69  v, addr1);.    i
2484a 66 28 20 70 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a  f( p->pLimit ){.
2484b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2484c 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 64  eAddOp3(v, OP_Ad
2484d 64 2c 20 69 4c 69 6d 69 74 2c 20 69 4f 66 66 73  d, iLimit, iOffs
2484e 65 74 2c 20 69 4f 66 66 73 65 74 2b 31 29 3b 0a  et, iOffset+1);.
2484f 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
24850 74 28 28 76 2c 20 22 4c 49 4d 49 54 2b 4f 46 46  t((v, "LIMIT+OFF
24851 53 45 54 22 29 29 3b 0a 20 20 20 20 20 20 61 64  SET"));.      ad
24852 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62  dr1 = sqlite3Vdb
24853 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
24854 50 6f 73 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20  Pos, iLimit);.  
24855 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
24856 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
24857 67 65 72 2c 20 2d 31 2c 20 69 4f 66 66 73 65 74  ger, -1, iOffset
24858 2b 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  +1);.      sqlit
24859 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
2485a 2c 20 61 64 64 72 31 29 3b 0a 20 20 20 20 7d 0a  , addr1);.    }.
2485b 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53    }.}..#ifndef S
2485c 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f  QLITE_OMIT_COMPO
2485d 55 4e 44 5f 53 45 4c 45 43 54 0a 2f 2a 0a 2a 2a  UND_SELECT./*.**
2485e 20 52 65 74 75 72 6e 20 74 68 65 20 61 70 70 72   Return the appr
2485f 6f 70 72 69 61 74 65 20 63 6f 6c 6c 61 74 69 6e  opriate collatin
24860 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74  g sequence for t
24861 68 65 20 69 43 6f 6c 2d 74 68 20 63 6f 6c 75 6d  he iCol-th colum
24862 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 72 65 73 75  n of.** the resu
24863 6c 74 20 73 65 74 20 66 6f 72 20 74 68 65 20 63  lt set for the c
24864 6f 6d 70 6f 75 6e 64 2d 73 65 6c 65 63 74 20 73  ompound-select s
24865 74 61 74 65 6d 65 6e 74 20 22 70 22 2e 20 20 52  tatement "p".  R
24866 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 0a 2a 2a  eturn NULL if.**
24867 20 74 68 65 20 63 6f 6c 75 6d 6e 20 68 61 73 20   the column has 
24868 6e 6f 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61  no default colla
24869 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 0a 2a  ting sequence..*
2486a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69  *.** The collati
2486b 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20  ng sequence for 
2486c 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c  the compound sel
2486d 65 63 74 20 69 73 20 74 61 6b 65 6e 20 66 72 6f  ect is taken fro
2486e 6d 20 74 68 65 0a 2a 2a 20 6c 65 66 74 2d 6d 6f  m the.** left-mo
2486f 73 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20 73  st term of the s
24870 65 6c 65 63 74 20 74 68 61 74 20 68 61 73 20 61  elect that has a
24871 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
24872 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 43  nce..*/.static C
24873 6f 6c 6c 53 65 71 20 2a 6d 75 6c 74 69 53 65 6c  ollSeq *multiSel
24874 65 63 74 43 6f 6c 6c 53 65 71 28 50 61 72 73 65  ectCollSeq(Parse
24875 20 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63 74   *pParse, Select
24876 20 2a 70 2c 20 69 6e 74 20 69 43 6f 6c 29 7b 0a   *p, int iCol){.
24877 20 20 43 6f 6c 6c 53 65 71 20 2a 70 52 65 74 3b    CollSeq *pRet;
24878 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 69 6f 72  .  if( p->pPrior
24879 20 29 7b 0a 20 20 20 20 70 52 65 74 20 3d 20 6d   ){.    pRet = m
2487a 75 6c 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65  ultiSelectCollSe
2487b 71 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 50 72  q(pParse, p->pPr
2487c 69 6f 72 2c 20 69 43 6f 6c 29 3b 0a 20 20 7d 65  ior, iCol);.  }e
2487d 6c 73 65 7b 0a 20 20 20 20 70 52 65 74 20 3d 20  lse{.    pRet = 
2487e 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 52 65  0;.  }.  if( pRe
2487f 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 52 65 74  t==0 ){.    pRet
24880 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
24881 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 2d  llSeq(pParse, p-
24882 3e 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d  >pEList->a[iCol]
24883 2e 70 45 78 70 72 29 3b 0a 20 20 7d 0a 20 20 72  .pExpr);.  }.  r
24884 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 23 65  eturn pRet;.}.#e
24885 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
24886 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MIT_COMPOUND_SEL
24887 45 43 54 20 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61  ECT */../* Forwa
24888 72 64 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a  rd reference */.
24889 73 74 61 74 69 63 20 69 6e 74 20 6d 75 6c 74 69  static int multi
2488a 53 65 6c 65 63 74 4f 72 64 65 72 42 79 28 0a 20  SelectOrderBy(. 
2488b 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2488c 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
2488d 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53  g context */.  S
2488e 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20  elect *p,       
2488f 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68       /* The righ
24890 74 2d 6d 6f 73 74 20 6f 66 20 53 45 4c 45 43 54  t-most of SELECT
24891 73 20 74 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f  s to be coded */
24892 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70  .  SelectDest *p
24893 44 65 73 74 20 20 20 20 20 2f 2a 20 57 68 61 74  Dest     /* What
24894 20 74 6f 20 64 6f 20 77 69 74 68 20 71 75 65 72   to do with quer
24895 79 20 72 65 73 75 6c 74 73 20 2a 2f 0a 29 3b 0a  y results */.);.
24896 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
24897 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53  _OMIT_COMPOUND_S
24898 45 4c 45 43 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ELECT./*.** This
24899 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
2489a 65 64 20 74 6f 20 70 72 6f 63 65 73 73 20 61 20  ed to process a 
2489b 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72 79 20 66  compound query f
2489c 6f 72 6d 20 66 72 6f 6d 0a 2a 2a 20 74 77 6f 20  orm from.** two 
2489d 6f 72 20 6d 6f 72 65 20 73 65 70 61 72 61 74 65  or more separate
2489e 20 71 75 65 72 69 65 73 20 75 73 69 6e 67 20 55   queries using U
2489f 4e 49 4f 4e 2c 20 55 4e 49 4f 4e 20 41 4c 4c 2c  NION, UNION ALL,
248a0 20 45 58 43 45 50 54 2c 20 6f 72 0a 2a 2a 20 49   EXCEPT, or.** I
248a1 4e 54 45 52 53 45 43 54 0a 2a 2a 0a 2a 2a 20 22  NTERSECT.**.** "
248a2 70 22 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  p" points to the
248a3 20 72 69 67 68 74 2d 6d 6f 73 74 20 6f 66 20 74   right-most of t
248a4 68 65 20 74 77 6f 20 71 75 65 72 69 65 73 2e 20  he two queries. 
248a5 20 74 68 65 20 71 75 65 72 79 20 6f 6e 20 74 68   the query on th
248a6 65 0a 2a 2a 20 6c 65 66 74 20 69 73 20 70 2d 3e  e.** left is p->
248a7 70 50 72 69 6f 72 2e 20 20 54 68 65 20 6c 65 66  pPrior.  The lef
248a8 74 20 71 75 65 72 79 20 63 6f 75 6c 64 20 61 6c  t query could al
248a9 73 6f 20 62 65 20 61 20 63 6f 6d 70 6f 75 6e 64  so be a compound
248aa 20 71 75 65 72 79 0a 2a 2a 20 69 6e 20 77 68 69   query.** in whi
248ab 63 68 20 63 61 73 65 20 74 68 69 73 20 72 6f 75  ch case this rou
248ac 74 69 6e 65 20 77 69 6c 6c 20 62 65 20 63 61 6c  tine will be cal
248ad 6c 65 64 20 72 65 63 75 72 73 69 76 65 6c 79 2e  led recursively.
248ae 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75   .**.** The resu
248af 6c 74 73 20 6f 66 20 74 68 65 20 74 6f 74 61 6c  lts of the total
248b0 20 71 75 65 72 79 20 61 72 65 20 74 6f 20 62 65   query are to be
248b1 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20   written into a 
248b2 64 65 73 74 69 6e 61 74 69 6f 6e 0a 2a 2a 20 6f  destination.** o
248b3 66 20 74 79 70 65 20 65 44 65 73 74 20 77 69 74  f type eDest wit
248b4 68 20 70 61 72 61 6d 65 74 65 72 20 69 50 61 72  h parameter iPar
248b5 6d 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65  m..**.** Example
248b6 20 31 3a 20 20 43 6f 6e 73 69 64 65 72 20 61 20   1:  Consider a 
248b7 74 68 72 65 65 2d 77 61 79 20 63 6f 6d 70 6f 75  three-way compou
248b8 6e 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  nd SQL statement
248b9 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45  ..**.**     SELE
248ba 43 54 20 61 20 46 52 4f 4d 20 74 31 20 55 4e 49  CT a FROM t1 UNI
248bb 4f 4e 20 53 45 4c 45 43 54 20 62 20 46 52 4f 4d  ON SELECT b FROM
248bc 20 74 32 20 55 4e 49 4f 4e 20 53 45 4c 45 43 54   t2 UNION SELECT
248bd 20 63 20 46 52 4f 4d 20 74 33 0a 2a 2a 0a 2a 2a   c FROM t3.**.**
248be 20 54 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20   This statement 
248bf 69 73 20 70 61 72 73 65 64 20 75 70 20 61 73 20  is parsed up as 
248c0 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20  follows:.**.**  
248c1 20 20 20 53 45 4c 45 43 54 20 63 20 46 52 4f 4d     SELECT c FROM
248c2 20 74 33 0a 2a 2a 20 20 20 20 20 20 7c 0a 2a 2a   t3.**      |.**
248c3 20 20 20 20 20 20 60 2d 2d 2d 2d 2d 3e 20 20 53        `----->  S
248c4 45 4c 45 43 54 20 62 20 46 52 4f 4d 20 74 32 0a  ELECT b FROM t2.
248c5 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
248c6 20 20 7c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20    |.**          
248c7 20 20 20 20 20 20 60 2d 2d 2d 2d 2d 2d 3e 20 20        `------>  
248c8 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 74 31  SELECT a FROM t1
248c9 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 72 72 6f 77  .**.** The arrow
248ca 73 20 69 6e 20 74 68 65 20 64 69 61 67 72 61 6d  s in the diagram
248cb 20 61 62 6f 76 65 20 72 65 70 72 65 73 65 6e 74   above represent
248cc 20 74 68 65 20 53 65 6c 65 63 74 2e 70 50 72 69   the Select.pPri
248cd 6f 72 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 20 53  or pointer..** S
248ce 6f 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e  o if this routin
248cf 65 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 68  e is called with
248d0 20 70 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20   p equal to the 
248d1 74 33 20 71 75 65 72 79 2c 20 74 68 65 6e 0a 2a  t3 query, then.*
248d2 2a 20 70 50 72 69 6f 72 20 77 69 6c 6c 20 62 65  * pPrior will be
248d3 20 74 68 65 20 74 32 20 71 75 65 72 79 2e 20 20   the t2 query.  
248d4 70 2d 3e 6f 70 20 77 69 6c 6c 20 62 65 20 54 4b  p->op will be TK
248d5 5f 55 4e 49 4f 4e 20 69 6e 20 74 68 69 73 20 63  _UNION in this c
248d6 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 69 63  ase..**.** Notic
248d7 65 20 74 68 61 74 20 62 65 63 61 75 73 65 20 6f  e that because o
248d8 66 20 74 68 65 20 77 61 79 20 53 51 4c 69 74 65  f the way SQLite
248d9 20 70 61 72 73 65 73 20 63 6f 6d 70 6f 75 6e 64   parses compound
248da 20 53 45 4c 45 43 54 73 2c 20 74 68 65 0a 2a 2a   SELECTs, the.**
248db 20 69 6e 64 69 76 69 64 75 61 6c 20 73 65 6c 65   individual sele
248dc 63 74 73 20 61 6c 77 61 79 73 20 67 72 6f 75 70  cts always group
248dd 20 66 72 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69   from left to ri
248de 67 68 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ght..*/.static i
248df 6e 74 20 6d 75 6c 74 69 53 65 6c 65 63 74 28 0a  nt multiSelect(.
248e0 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
248e1 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69          /* Parsi
248e2 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
248e3 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20  Select *p,      
248e4 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67        /* The rig
248e5 68 74 2d 6d 6f 73 74 20 6f 66 20 53 45 4c 45 43  ht-most of SELEC
248e6 54 73 20 74 6f 20 62 65 20 63 6f 64 65 64 20 2a  Ts to be coded *
248e7 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a  /.  SelectDest *
248e8 70 44 65 73 74 20 20 20 20 20 2f 2a 20 57 68 61  pDest     /* Wha
248e9 74 20 74 6f 20 64 6f 20 77 69 74 68 20 71 75 65  t to do with que
248ea 72 79 20 72 65 73 75 6c 74 73 20 2a 2f 0a 29 7b  ry results */.){
248eb 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
248ec 54 45 5f 4f 4b 3b 20 20 20 2f 2a 20 53 75 63 63  TE_OK;   /* Succ
248ed 65 73 73 20 63 6f 64 65 20 66 72 6f 6d 20 61 20  ess code from a 
248ee 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20  subroutine */.  
248ef 53 65 6c 65 63 74 20 2a 70 50 72 69 6f 72 3b 20  Select *pPrior; 
248f0 20 20 20 20 20 20 2f 2a 20 41 6e 6f 74 68 65 72        /* Another
248f1 20 53 45 4c 45 43 54 20 69 6d 6d 65 64 69 61 74   SELECT immediat
248f2 65 6c 79 20 74 6f 20 6f 75 72 20 6c 65 66 74 20  ely to our left 
248f3 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20  */.  Vdbe *v;   
248f4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65             /* Ge
248f5 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 74  nerate code to t
248f6 68 69 73 20 56 44 42 45 20 2a 2f 0a 20 20 53 65  his VDBE */.  Se
248f7 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b 20 20  lectDest dest;  
248f8 20 20 20 20 2f 2a 20 41 6c 74 65 72 6e 61 74 69      /* Alternati
248f9 76 65 20 64 61 74 61 20 64 65 73 74 69 6e 61 74  ve data destinat
248fa 69 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ion */.  Select 
248fb 2a 70 44 65 6c 65 74 65 20 3d 20 30 3b 20 20 2f  *pDelete = 0;  /
248fc 2a 20 43 68 61 69 6e 20 6f 66 20 73 69 6d 70 6c  * Chain of simpl
248fd 65 20 73 65 6c 65 63 74 73 20 74 6f 20 64 65 6c  e selects to del
248fe 65 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ete */.  sqlite3
248ff 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 2f   *db;          /
24900 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
24901 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 4d  ction */..  /* M
24902 61 6b 65 20 73 75 72 65 20 74 68 65 72 65 20 69  ake sure there i
24903 73 20 6e 6f 20 4f 52 44 45 52 20 42 59 20 6f 72  s no ORDER BY or
24904 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 6f 6e   LIMIT clause on
24905 20 70 72 69 6f 72 20 53 45 4c 45 43 54 73 2e 20   prior SELECTs. 
24906 20 4f 6e 6c 79 0a 20 20 2a 2a 20 74 68 65 20 6c   Only.  ** the l
24907 61 73 74 20 28 72 69 67 68 74 2d 6d 6f 73 74 29  ast (right-most)
24908 20 53 45 4c 45 43 54 20 69 6e 20 74 68 65 20 73   SELECT in the s
24909 65 72 69 65 73 20 6d 61 79 20 68 61 76 65 20 61  eries may have a
2490a 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20 4c 49  n ORDER BY or LI
2490b 4d 49 54 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  MIT..  */.  asse
2490c 72 74 28 20 70 20 26 26 20 70 2d 3e 70 50 72 69  rt( p && p->pPri
2490d 6f 72 20 29 3b 20 20 2f 2a 20 43 61 6c 6c 69 6e  or );  /* Callin
2490e 67 20 66 75 6e 63 74 69 6f 6e 20 67 75 61 72 61  g function guara
2490f 6e 74 65 65 73 20 74 68 69 73 20 6d 75 63 68 20  ntees this much 
24910 2a 2f 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65  */.  db = pParse
24911 2d 3e 64 62 3b 0a 20 20 70 50 72 69 6f 72 20 3d  ->db;.  pPrior =
24912 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 61 73   p->pPrior;.  as
24913 73 65 72 74 28 20 70 50 72 69 6f 72 2d 3e 70 52  sert( pPrior->pR
24914 69 67 68 74 6d 6f 73 74 21 3d 70 50 72 69 6f 72  ightmost!=pPrior
24915 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
24916 72 69 6f 72 2d 3e 70 52 69 67 68 74 6d 6f 73 74  rior->pRightmost
24917 3d 3d 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20  ==p->pRightmost 
24918 29 3b 0a 20 20 64 65 73 74 20 3d 20 2a 70 44 65  );.  dest = *pDe
24919 73 74 3b 0a 20 20 69 66 28 20 70 50 72 69 6f 72  st;.  if( pPrior
2491a 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ->pOrderBy ){.  
2491b 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2491c 67 28 70 50 61 72 73 65 2c 22 4f 52 44 45 52 20  g(pParse,"ORDER 
2491d 42 59 20 63 6c 61 75 73 65 20 73 68 6f 75 6c 64  BY clause should
2491e 20 63 6f 6d 65 20 61 66 74 65 72 20 25 73 20 6e   come after %s n
2491f 6f 74 20 62 65 66 6f 72 65 22 2c 0a 20 20 20 20  ot before",.    
24920 20 20 73 65 6c 65 63 74 4f 70 4e 61 6d 65 28 70    selectOpName(p
24921 2d 3e 6f 70 29 29 3b 0a 20 20 20 20 72 63 20 3d  ->op));.    rc =
24922 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6d 75 6c   1;.    goto mul
24923 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20  ti_select_end;. 
24924 20 7d 0a 20 20 69 66 28 20 70 50 72 69 6f 72 2d   }.  if( pPrior-
24925 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 73  >pLimit ){.    s
24926 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
24927 50 61 72 73 65 2c 22 4c 49 4d 49 54 20 63 6c 61  Parse,"LIMIT cla
24928 75 73 65 20 73 68 6f 75 6c 64 20 63 6f 6d 65 20  use should come 
24929 61 66 74 65 72 20 25 73 20 6e 6f 74 20 62 65 66  after %s not bef
2492a 6f 72 65 22 2c 0a 20 20 20 20 20 20 73 65 6c 65  ore",.      sele
2492b 63 74 4f 70 4e 61 6d 65 28 70 2d 3e 6f 70 29 29  ctOpName(p->op))
2492c 3b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20  ;.    rc = 1;.  
2492d 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c    goto multi_sel
2492e 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 0a 20 20  ect_end;.  }..  
2492f 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
24930 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 61 73  be(pParse);.  as
24931 73 65 72 74 28 20 76 21 3d 30 20 29 3b 20 20 2f  sert( v!=0 );  /
24932 2a 20 54 68 65 20 56 44 42 45 20 61 6c 72 65 61  * The VDBE alrea
24933 64 79 20 63 72 65 61 74 65 64 20 62 79 20 63 61  dy created by ca
24934 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a  lling function *
24935 2f 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 74  /..  /* Create t
24936 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 74  he destination t
24937 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 69  emporary table i
24938 66 20 6e 65 63 65 73 73 61 72 79 0a 20 20 2a 2f  f necessary.  */
24939 0a 20 20 69 66 28 20 64 65 73 74 2e 65 44 65 73  .  if( dest.eDes
2493a 74 3d 3d 53 52 54 5f 45 70 68 65 6d 54 61 62 20  t==SRT_EphemTab 
2493b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
2493c 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20 20 20  ->pEList );.    
2493d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2493e 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65  2(v, OP_OpenEphe
2493f 6d 65 72 61 6c 2c 20 64 65 73 74 2e 69 50 61 72  meral, dest.iPar
24940 6d 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45  m, p->pEList->nE
24941 78 70 72 29 3b 0a 20 20 20 20 64 65 73 74 2e 65  xpr);.    dest.e
24942 44 65 73 74 20 3d 20 53 52 54 5f 54 61 62 6c 65  Dest = SRT_Table
24943 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65  ;.  }..  /* Make
24944 20 73 75 72 65 20 61 6c 6c 20 53 45 4c 45 43 54   sure all SELECT
24945 73 20 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65  s in the stateme
24946 6e 74 20 68 61 76 65 20 74 68 65 20 73 61 6d 65  nt have the same
24947 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   number of eleme
24948 6e 74 73 0a 20 20 2a 2a 20 69 6e 20 74 68 65 69  nts.  ** in thei
24949 72 20 72 65 73 75 6c 74 20 73 65 74 73 2e 0a 20  r result sets.. 
2494a 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d   */.  assert( p-
2494b 3e 70 45 4c 69 73 74 20 26 26 20 70 50 72 69 6f  >pEList && pPrio
2494c 72 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20 69  r->pEList );.  i
2494d 66 28 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45  f( p->pEList->nE
2494e 78 70 72 21 3d 70 50 72 69 6f 72 2d 3e 70 45 4c  xpr!=pPrior->pEL
2494f 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20  ist->nExpr ){.  
24950 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
24951 67 28 70 50 61 72 73 65 2c 20 22 53 45 4c 45 43  g(pParse, "SELEC
24952 54 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 61  Ts to the left a
24953 6e 64 20 72 69 67 68 74 20 6f 66 20 25 73 22 0a  nd right of %s".
24954 20 20 20 20 20 20 22 20 64 6f 20 6e 6f 74 20 68        " do not h
24955 61 76 65 20 74 68 65 20 73 61 6d 65 20 6e 75 6d  ave the same num
24956 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f  ber of result co
24957 6c 75 6d 6e 73 22 2c 20 73 65 6c 65 63 74 4f 70  lumns", selectOp
24958 4e 61 6d 65 28 70 2d 3e 6f 70 29 29 3b 0a 20 20  Name(p->op));.  
24959 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 67 6f    rc = 1;.    go
2495a 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f  to multi_select_
2495b 65 6e 64 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  end;.  }..  /* C
2495c 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 73 20  ompound SELECTs 
2495d 74 68 61 74 20 68 61 76 65 20 61 6e 20 4f 52 44  that have an ORD
2495e 45 52 20 42 59 20 63 6c 61 75 73 65 20 61 72 65  ER BY clause are
2495f 20 68 61 6e 64 6c 65 64 20 73 65 70 61 72 61 74   handled separat
24960 65 6c 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ely..  */.  if( 
24961 70 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  p->pOrderBy ){. 
24962 20 20 20 72 65 74 75 72 6e 20 6d 75 6c 74 69 53     return multiS
24963 65 6c 65 63 74 4f 72 64 65 72 42 79 28 70 50 61  electOrderBy(pPa
24964 72 73 65 2c 20 70 2c 20 70 44 65 73 74 29 3b 0a  rse, p, pDest);.
24965 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61    }..  /* Genera
24966 74 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20  te code for the 
24967 6c 65 66 74 20 61 6e 64 20 72 69 67 68 74 20 53  left and right S
24968 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73  ELECT statements
24969 2e 0a 20 20 2a 2f 0a 20 20 73 77 69 74 63 68 28  ..  */.  switch(
2496a 20 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61   p->op ){.    ca
2496b 73 65 20 54 4b 5f 41 4c 4c 3a 20 7b 0a 20 20 20  se TK_ALL: {.   
2496c 20 20 20 69 6e 74 20 61 64 64 72 20 3d 20 30 3b     int addr = 0;
2496d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21  .      assert( !
2496e 70 50 72 69 6f 72 2d 3e 70 4c 69 6d 69 74 20 29  pPrior->pLimit )
2496f 3b 0a 20 20 20 20 20 20 70 50 72 69 6f 72 2d 3e  ;.      pPrior->
24970 70 4c 69 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d  pLimit = p->pLim
24971 69 74 3b 0a 20 20 20 20 20 20 70 50 72 69 6f 72  it;.      pPrior
24972 2d 3e 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e 70  ->pOffset = p->p
24973 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 72 63  Offset;.      rc
24974 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
24975 28 70 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c  (pParse, pPrior,
24976 20 26 64 65 73 74 29 3b 0a 20 20 20 20 20 20 70   &dest);.      p
24977 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20  ->pLimit = 0;.  
24978 20 20 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d      p->pOffset =
24979 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63   0;.      if( rc
2497a 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   ){.        goto
2497b 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e   multi_select_en
2497c 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  d;.      }.     
2497d 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0a   p->pPrior = 0;.
2497e 20 20 20 20 20 20 70 2d 3e 69 4c 69 6d 69 74 20        p->iLimit 
2497f 3d 20 70 50 72 69 6f 72 2d 3e 69 4c 69 6d 69 74  = pPrior->iLimit
24980 3b 0a 20 20 20 20 20 20 70 2d 3e 69 4f 66 66 73  ;.      p->iOffs
24981 65 74 20 3d 20 70 50 72 69 6f 72 2d 3e 69 4f 66  et = pPrior->iOf
24982 66 73 65 74 3b 0a 20 20 20 20 20 20 69 66 28 20  fset;.      if( 
24983 70 2d 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20  p->iLimit ){.   
24984 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69       addr = sqli
24985 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
24986 20 4f 50 5f 49 66 5a 65 72 6f 2c 20 70 2d 3e 69   OP_IfZero, p->i
24987 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 20 20  Limit);.        
24988 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
24989 22 4a 75 6d 70 20 61 68 65 61 64 20 69 66 20 4c  "Jump ahead if L
2498a 49 4d 49 54 20 72 65 61 63 68 65 64 22 29 29 3b  IMIT reached"));
2498b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
2498c 63 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63  c = sqlite3Selec
2498d 74 28 70 50 61 72 73 65 2c 20 70 2c 20 26 64 65  t(pParse, p, &de
2498e 73 74 29 3b 0a 20 20 20 20 20 20 70 44 65 6c 65  st);.      pDele
2498f 74 65 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a  te = p->pPrior;.
24990 20 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20        p->pPrior 
24991 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20  = pPrior;.      
24992 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
24993 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c    goto multi_sel
24994 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d  ect_end;.      }
24995 0a 20 20 20 20 20 20 69 66 28 20 61 64 64 72 20  .      if( addr 
24996 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
24997 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
24998 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20 7d  , addr);.      }
24999 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2499a 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
2499b 45 58 43 45 50 54 3a 0a 20 20 20 20 63 61 73 65  EXCEPT:.    case
2499c 20 54 4b 5f 55 4e 49 4f 4e 3a 20 7b 0a 20 20 20   TK_UNION: {.   
2499d 20 20 20 69 6e 74 20 75 6e 69 6f 6e 54 61 62 3b     int unionTab;
2499e 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75      /* Cursor nu
2499f 6d 62 65 72 20 6f 66 20 74 68 65 20 74 65 6d 70  mber of the temp
249a0 6f 72 61 72 79 20 74 61 62 6c 65 20 68 6f 6c 64  orary table hold
249a1 69 6e 67 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20  ing result */.  
249a2 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 30 3b 20      int op = 0; 
249a3 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74       /* One of t
249a4 68 65 20 53 52 54 5f 20 6f 70 65 72 61 74 69 6f  he SRT_ operatio
249a5 6e 73 20 74 6f 20 61 70 70 6c 79 20 74 6f 20 73  ns to apply to s
249a6 65 6c 66 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  elf */.      int
249a7 20 70 72 69 6f 72 4f 70 3b 20 20 20 20 20 2f 2a   priorOp;     /*
249a8 20 54 68 65 20 53 52 54 5f 20 6f 70 65 72 61 74   The SRT_ operat
249a9 69 6f 6e 20 74 6f 20 61 70 70 6c 79 20 74 6f 20  ion to apply to 
249aa 70 72 69 6f 72 20 73 65 6c 65 63 74 73 20 2a 2f  prior selects */
249ab 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 69  .      Expr *pLi
249ac 6d 69 74 2c 20 2a 70 4f 66 66 73 65 74 3b 20 2f  mit, *pOffset; /
249ad 2a 20 53 61 76 65 64 20 76 61 6c 75 65 73 20 6f  * Saved values o
249ae 66 20 70 2d 3e 6e 4c 69 6d 69 74 20 61 6e 64 20  f p->nLimit and 
249af 70 2d 3e 6e 4f 66 66 73 65 74 20 2a 2f 0a 20 20  p->nOffset */.  
249b0 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20      int addr;.  
249b1 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 75      SelectDest u
249b2 6e 69 6f 6e 64 65 73 74 3b 0a 0a 20 20 20 20 20  niondest;..     
249b3 20 70 72 69 6f 72 4f 70 20 3d 20 53 52 54 5f 55   priorOp = SRT_U
249b4 6e 69 6f 6e 3b 0a 20 20 20 20 20 20 69 66 28 20  nion;.      if( 
249b5 64 65 73 74 2e 65 44 65 73 74 3d 3d 70 72 69 6f  dest.eDest==prio
249b6 72 4f 70 20 26 26 20 21 70 2d 3e 70 4c 69 6d 69  rOp && !p->pLimi
249b7 74 20 26 26 20 21 70 2d 3e 70 4f 66 66 73 65 74  t && !p->pOffset
249b8 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 57   ){.        /* W
249b9 65 20 63 61 6e 20 72 65 75 73 65 20 61 20 74 65  e can reuse a te
249ba 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 67 65  mporary table ge
249bb 6e 65 72 61 74 65 64 20 62 79 20 61 20 53 45 4c  nerated by a SEL
249bc 45 43 54 20 74 6f 20 6f 75 72 0a 20 20 20 20 20  ECT to our.     
249bd 20 20 20 2a 2a 20 72 69 67 68 74 2e 0a 20 20 20     ** right..   
249be 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
249bf 75 6e 69 6f 6e 54 61 62 20 3d 20 64 65 73 74 2e  unionTab = dest.
249c0 69 50 61 72 6d 3b 0a 20 20 20 20 20 20 7d 65 6c  iParm;.      }el
249c1 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 57  se{.        /* W
249c2 65 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20 63  e will need to c
249c3 72 65 61 74 65 20 6f 75 72 20 6f 77 6e 20 74 65  reate our own te
249c4 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 74 6f  mporary table to
249c5 20 68 6f 6c 64 20 74 68 65 0a 20 20 20 20 20 20   hold the.      
249c6 20 20 2a 2a 20 69 6e 74 65 72 6d 65 64 69 61 74    ** intermediat
249c7 65 20 72 65 73 75 6c 74 73 2e 0a 20 20 20 20 20  e results..     
249c8 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 6e     */.        un
249c9 69 6f 6e 54 61 62 20 3d 20 70 50 61 72 73 65 2d  ionTab = pParse-
249ca 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20  >nTab++;.       
249cb 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4f 72 64   assert( p->pOrd
249cc 65 72 42 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20  erBy==0 );.     
249cd 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
249ce 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
249cf 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c  P_OpenEphemeral,
249d0 20 75 6e 69 6f 6e 54 61 62 2c 20 30 29 3b 0a 20   unionTab, 0);. 
249d1 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
249d2 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30  ->addrOpenEphm[0
249d3 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20 20 20 20 20  ] == -1 );.     
249d4 20 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70     p->addrOpenEp
249d5 68 6d 5b 30 5d 20 3d 20 61 64 64 72 3b 0a 20 20  hm[0] = addr;.  
249d6 20 20 20 20 20 20 70 2d 3e 70 52 69 67 68 74 6d        p->pRightm
249d7 6f 73 74 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d  ost->selFlags |=
249d8 20 53 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61   SF_UsesEphemera
249d9 6c 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  l;.        asser
249da 74 28 20 70 2d 3e 70 45 4c 69 73 74 20 29 3b 0a  t( p->pEList );.
249db 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
249dc 2a 20 43 6f 64 65 20 74 68 65 20 53 45 4c 45 43  * Code the SELEC
249dd 54 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20  T statements to 
249de 6f 75 72 20 6c 65 66 74 0a 20 20 20 20 20 20 2a  our left.      *
249df 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  /.      assert( 
249e0 21 70 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42  !pPrior->pOrderB
249e1 79 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  y );.      sqlit
249e2 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74  e3SelectDestInit
249e3 28 26 75 6e 69 6f 6e 64 65 73 74 2c 20 70 72 69  (&uniondest, pri
249e4 6f 72 4f 70 2c 20 75 6e 69 6f 6e 54 61 62 29 3b  orOp, unionTab);
249e5 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
249e6 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
249e7 2c 20 70 50 72 69 6f 72 2c 20 26 75 6e 69 6f 6e  , pPrior, &union
249e8 64 65 73 74 29 3b 0a 20 20 20 20 20 20 69 66 28  dest);.      if(
249e9 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 67   rc ){.        g
249ea 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74  oto multi_select
249eb 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  _end;.      }.. 
249ec 20 20 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 65       /* Code the
249ed 20 63 75 72 72 65 6e 74 20 53 45 4c 45 43 54 20   current SELECT 
249ee 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 20 20  statement.      
249ef 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e  */.      if( p->
249f0 6f 70 3d 3d 54 4b 5f 45 58 43 45 50 54 20 29 7b  op==TK_EXCEPT ){
249f1 0a 20 20 20 20 20 20 20 20 6f 70 20 3d 20 53 52  .        op = SR
249f2 54 5f 45 78 63 65 70 74 3b 0a 20 20 20 20 20 20  T_Except;.      
249f3 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61  }else{.        a
249f4 73 73 65 72 74 28 20 70 2d 3e 6f 70 3d 3d 54 4b  ssert( p->op==TK
249f5 5f 55 4e 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20  _UNION );.      
249f6 20 20 6f 70 20 3d 20 53 52 54 5f 55 6e 69 6f 6e    op = SRT_Union
249f7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
249f8 70 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20  p->pPrior = 0;. 
249f9 20 20 20 20 20 70 4c 69 6d 69 74 20 3d 20 70 2d       pLimit = p-
249fa 3e 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70  >pLimit;.      p
249fb 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20  ->pLimit = 0;.  
249fc 20 20 20 20 70 4f 66 66 73 65 74 20 3d 20 70 2d      pOffset = p-
249fd 3e 70 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20  >pOffset;.      
249fe 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a  p->pOffset = 0;.
249ff 20 20 20 20 20 20 75 6e 69 6f 6e 64 65 73 74 2e        uniondest.
24a00 65 44 65 73 74 20 3d 20 6f 70 3b 0a 20 20 20 20  eDest = op;.    
24a01 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 65    rc = sqlite3Se
24a02 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20  lect(pParse, p, 
24a03 26 75 6e 69 6f 6e 64 65 73 74 29 3b 0a 20 20 20  &uniondest);.   
24a04 20 20 20 2f 2a 20 51 75 65 72 79 20 66 6c 61 74     /* Query flat
24a05 74 65 6e 69 6e 67 20 69 6e 20 73 71 6c 69 74 65  tening in sqlite
24a06 33 53 65 6c 65 63 74 28 29 20 6d 69 67 68 74 20  3Select() might 
24a07 72 65 66 69 6c 6c 20 70 2d 3e 70 4f 72 64 65 72  refill p->pOrder
24a08 42 79 2e 0a 20 20 20 20 20 20 2a 2a 20 42 65 20  By..      ** Be 
24a09 73 75 72 65 20 74 6f 20 64 65 6c 65 74 65 20 70  sure to delete p
24a0a 2d 3e 70 4f 72 64 65 72 42 79 2c 20 74 68 65 72  ->pOrderBy, ther
24a0b 65 66 6f 72 65 2c 20 74 6f 20 61 76 6f 69 64 20  efore, to avoid 
24a0c 61 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 2e 20 2a  a memory leak. *
24a0d 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  /.      sqlite3E
24a0e 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
24a0f 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a  , p->pOrderBy);.
24a10 20 20 20 20 20 20 70 44 65 6c 65 74 65 20 3d 20        pDelete = 
24a11 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 20  p->pPrior;.     
24a12 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72   p->pPrior = pPr
24a13 69 6f 72 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f  ior;.      p->pO
24a14 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 20 20  rderBy = 0;.    
24a15 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
24a16 65 74 65 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69  ete(db, p->pLimi
24a17 74 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69  t);.      p->pLi
24a18 6d 69 74 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20  mit = pLimit;.  
24a19 20 20 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d      p->pOffset =
24a1a 20 70 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20   pOffset;.      
24a1b 70 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20  p->iLimit = 0;. 
24a1c 20 20 20 20 20 70 2d 3e 69 4f 66 66 73 65 74 20       p->iOffset 
24a1d 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 72  = 0;.      if( r
24a1e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74  c ){.        got
24a1f 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65  o multi_select_e
24a20 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 0a 20 20  nd;.      }...  
24a21 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74      /* Convert t
24a22 68 65 20 64 61 74 61 20 69 6e 20 74 68 65 20 74  he data in the t
24a23 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 69  emporary table i
24a24 6e 74 6f 20 77 68 61 74 65 76 65 72 20 66 6f 72  nto whatever for
24a25 6d 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 69 73  m.      ** it is
24a26 20 74 68 61 74 20 77 65 20 63 75 72 72 65 6e 74   that we current
24a27 6c 79 20 6e 65 65 64 2e 0a 20 20 20 20 20 20 2a  ly need..      *
24a28 2f 20 20 20 20 20 20 0a 20 20 20 20 20 20 69 66  /      .      if
24a29 28 20 64 65 73 74 2e 65 44 65 73 74 21 3d 70 72  ( dest.eDest!=pr
24a2a 69 6f 72 4f 70 20 7c 7c 20 75 6e 69 6f 6e 54 61  iorOp || unionTa
24a2b 62 21 3d 64 65 73 74 2e 69 50 61 72 6d 20 29 7b  b!=dest.iParm ){
24a2c 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 43 6f  .        int iCo
24a2d 6e 74 2c 20 69 42 72 65 61 6b 2c 20 69 53 74 61  nt, iBreak, iSta
24a2e 72 74 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  rt;.        asse
24a2f 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20 29 3b  rt( p->pEList );
24a30 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 65 73  .        if( des
24a31 74 2e 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74  t.eDest==SRT_Out
24a32 70 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  put ){.         
24a33 20 53 65 6c 65 63 74 20 2a 70 46 69 72 73 74 20   Select *pFirst 
24a34 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20 77  = p;.          w
24a35 68 69 6c 65 28 20 70 46 69 72 73 74 2d 3e 70 50  hile( pFirst->pP
24a36 72 69 6f 72 20 29 20 70 46 69 72 73 74 20 3d 20  rior ) pFirst = 
24a37 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 3b 0a  pFirst->pPrior;.
24a38 20 20 20 20 20 20 20 20 20 20 67 65 6e 65 72 61            genera
24a39 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50  teColumnNames(pP
24a3a 61 72 73 65 2c 20 30 2c 20 70 46 69 72 73 74 2d  arse, 0, pFirst-
24a3b 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 20 20  >pEList);.      
24a3c 20 20 7d 0a 20 20 20 20 20 20 20 20 69 42 72 65    }.        iBre
24a3d 61 6b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  ak = sqlite3Vdbe
24a3e 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20  MakeLabel(v);.  
24a3f 20 20 20 20 20 20 69 43 6f 6e 74 20 3d 20 73 71        iCont = sq
24a40 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
24a41 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 20 63  el(v);.        c
24a42 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65 67 69 73  omputeLimitRegis
24a43 74 65 72 73 28 70 50 61 72 73 65 2c 20 70 2c 20  ters(pParse, p, 
24a44 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 20  iBreak);.       
24a45 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24a46 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c  p2(v, OP_Rewind,
24a47 20 75 6e 69 6f 6e 54 61 62 2c 20 69 42 72 65 61   unionTab, iBrea
24a48 6b 29 3b 0a 20 20 20 20 20 20 20 20 69 53 74 61  k);.        iSta
24a49 72 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rt = sqlite3Vdbe
24a4a 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
24a4b 20 20 20 20 20 20 20 20 73 65 6c 65 63 74 49 6e          selectIn
24a4c 6e 65 72 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20  nerLoop(pParse, 
24a4d 70 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 75 6e  p, p->pEList, un
24a4e 69 6f 6e 54 61 62 2c 20 70 2d 3e 70 45 4c 69 73  ionTab, p->pELis
24a4f 74 2d 3e 6e 45 78 70 72 2c 0a 20 20 20 20 20 20  t->nExpr,.      
24a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24a51 20 20 30 2c 20 2d 31 2c 20 26 64 65 73 74 2c 20    0, -1, &dest, 
24a52 69 43 6f 6e 74 2c 20 69 42 72 65 61 6b 29 3b 0a  iCont, iBreak);.
24a53 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
24a54 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
24a55 76 2c 20 69 43 6f 6e 74 29 3b 0a 20 20 20 20 20  v, iCont);.     
24a56 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
24a57 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c  dOp2(v, OP_Next,
24a58 20 75 6e 69 6f 6e 54 61 62 2c 20 69 53 74 61 72   unionTab, iStar
24a59 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  t);.        sqli
24a5a 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
24a5b 62 65 6c 28 76 2c 20 69 42 72 65 61 6b 29 3b 0a  bel(v, iBreak);.
24a5c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
24a5d 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
24a5e 43 6c 6f 73 65 2c 20 75 6e 69 6f 6e 54 61 62 2c  Close, unionTab,
24a5f 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   0);.      }.   
24a60 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
24a61 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 45      case TK_INTE
24a62 52 53 45 43 54 3a 20 7b 0a 20 20 20 20 20 20 69  RSECT: {.      i
24a63 6e 74 20 74 61 62 31 2c 20 74 61 62 32 3b 0a 20  nt tab1, tab2;. 
24a64 20 20 20 20 20 69 6e 74 20 69 43 6f 6e 74 2c 20       int iCont, 
24a65 69 42 72 65 61 6b 2c 20 69 53 74 61 72 74 3b 0a  iBreak, iStart;.
24a66 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 69 6d        Expr *pLim
24a67 69 74 2c 20 2a 70 4f 66 66 73 65 74 3b 0a 20 20  it, *pOffset;.  
24a68 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20      int addr;.  
24a69 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 69      SelectDest i
24a6a 6e 74 65 72 73 65 63 74 64 65 73 74 3b 0a 20 20  ntersectdest;.  
24a6b 20 20 20 20 69 6e 74 20 72 31 3b 0a 0a 20 20 20      int r1;..   
24a6c 20 20 20 2f 2a 20 49 4e 54 45 52 53 45 43 54 20     /* INTERSECT 
24a6d 69 73 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f  is different fro
24a6e 6d 20 74 68 65 20 6f 74 68 65 72 73 20 73 69 6e  m the others sin
24a6f 63 65 20 69 74 20 72 65 71 75 69 72 65 73 0a 20  ce it requires. 
24a70 20 20 20 20 20 2a 2a 20 74 77 6f 20 74 65 6d 70       ** two temp
24a71 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20 48  orary tables.  H
24a72 65 6e 63 65 20 69 74 20 68 61 73 20 69 74 73 20  ence it has its 
24a73 6f 77 6e 20 63 61 73 65 2e 20 20 42 65 67 69 6e  own case.  Begin
24a74 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 61 6c 6c  .      ** by all
24a75 6f 63 61 74 69 6e 67 20 74 68 65 20 74 61 62 6c  ocating the tabl
24a76 65 73 20 77 65 20 77 69 6c 6c 20 6e 65 65 64 2e  es we will need.
24a77 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
24a78 74 61 62 31 20 3d 20 70 50 61 72 73 65 2d 3e 6e  tab1 = pParse->n
24a79 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 74 61 62  Tab++;.      tab
24a7a 32 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  2 = pParse->nTab
24a7b 2b 2b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ++;.      assert
24a7c 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 3d 3d 30  ( p->pOrderBy==0
24a7d 20 29 3b 0a 0a 20 20 20 20 20 20 61 64 64 72 20   );..      addr 
24a7e 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
24a7f 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70  Op2(v, OP_OpenEp
24a80 68 65 6d 65 72 61 6c 2c 20 74 61 62 31 2c 20 30  hemeral, tab1, 0
24a81 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
24a82 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d   p->addrOpenEphm
24a83 5b 30 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20 20 20  [0] == -1 );.   
24a84 20 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70     p->addrOpenEp
24a85 68 6d 5b 30 5d 20 3d 20 61 64 64 72 3b 0a 20 20  hm[0] = addr;.  
24a86 20 20 20 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73      p->pRightmos
24a87 74 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53  t->selFlags |= S
24a88 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c 3b  F_UsesEphemeral;
24a89 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
24a8a 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 0a 20 20 20  ->pEList );..   
24a8b 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 53     /* Code the S
24a8c 45 4c 45 43 54 73 20 74 6f 20 6f 75 72 20 6c 65  ELECTs to our le
24a8d 66 74 20 69 6e 74 6f 20 74 65 6d 70 6f 72 61 72  ft into temporar
24a8e 79 20 74 61 62 6c 65 20 22 74 61 62 31 22 2e 0a  y table "tab1"..
24a8f 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
24a90 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74  qlite3SelectDest
24a91 49 6e 69 74 28 26 69 6e 74 65 72 73 65 63 74 64  Init(&intersectd
24a92 65 73 74 2c 20 53 52 54 5f 55 6e 69 6f 6e 2c 20  est, SRT_Union, 
24a93 74 61 62 31 29 3b 0a 20 20 20 20 20 20 72 63 20  tab1);.      rc 
24a94 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28  = sqlite3Select(
24a95 70 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c 20  pParse, pPrior, 
24a96 26 69 6e 74 65 72 73 65 63 74 64 65 73 74 29 3b  &intersectdest);
24a97 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b  .      if( rc ){
24a98 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75  .        goto mu
24a99 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  lti_select_end;.
24a9a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
24a9b 2a 20 43 6f 64 65 20 74 68 65 20 63 75 72 72 65  * Code the curre
24a9c 6e 74 20 53 45 4c 45 43 54 20 69 6e 74 6f 20 74  nt SELECT into t
24a9d 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 22  emporary table "
24a9e 74 61 62 32 22 0a 20 20 20 20 20 20 2a 2f 0a 20  tab2".      */. 
24a9f 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69       addr = sqli
24aa0 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
24aa1 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
24aa2 6c 2c 20 74 61 62 32 2c 20 30 29 3b 0a 20 20 20  l, tab2, 0);.   
24aa3 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 64     assert( p->ad
24aa4 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 3d  drOpenEphm[1] ==
24aa5 20 2d 31 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e   -1 );.      p->
24aa6 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20  addrOpenEphm[1] 
24aa7 3d 20 61 64 64 72 3b 0a 20 20 20 20 20 20 70 2d  = addr;.      p-
24aa8 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20  >pPrior = 0;.   
24aa9 20 20 20 70 4c 69 6d 69 74 20 3d 20 70 2d 3e 70     pLimit = p->p
24aaa 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e  Limit;.      p->
24aab 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20  pLimit = 0;.    
24aac 20 20 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e 70    pOffset = p->p
24aad 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 70 2d  Offset;.      p-
24aae 3e 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20  >pOffset = 0;.  
24aaf 20 20 20 20 69 6e 74 65 72 73 65 63 74 64 65 73      intersectdes
24ab0 74 2e 69 50 61 72 6d 20 3d 20 74 61 62 32 3b 0a  t.iParm = tab2;.
24ab1 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
24ab2 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
24ab3 20 70 2c 20 26 69 6e 74 65 72 73 65 63 74 64 65   p, &intersectde
24ab4 73 74 29 3b 0a 20 20 20 20 20 20 70 44 65 6c 65  st);.      pDele
24ab5 74 65 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a  te = p->pPrior;.
24ab6 20 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20        p->pPrior 
24ab7 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20  = pPrior;.      
24ab8 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
24ab9 65 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29  e(db, p->pLimit)
24aba 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69  ;.      p->pLimi
24abb 74 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 20 20  t = pLimit;.    
24abc 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20 70    p->pOffset = p
24abd 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 69 66  Offset;.      if
24abe 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20  ( rc ){.        
24abf 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63  goto multi_selec
24ac0 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 0a  t_end;.      }..
24ac1 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74        /* Generat
24ac2 65 20 63 6f 64 65 20 74 6f 20 74 61 6b 65 20 74  e code to take t
24ac3 68 65 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20  he intersection 
24ac4 6f 66 20 74 68 65 20 74 77 6f 20 74 65 6d 70 6f  of the two tempo
24ac5 72 61 72 79 0a 20 20 20 20 20 20 2a 2a 20 74 61  rary.      ** ta
24ac6 62 6c 65 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  bles..      */. 
24ac7 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
24ac8 70 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 20 20  pEList );.      
24ac9 69 66 28 20 64 65 73 74 2e 65 44 65 73 74 3d 3d  if( dest.eDest==
24aca 53 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a 20 20  SRT_Output ){.  
24acb 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70 46        Select *pF
24acc 69 72 73 74 20 3d 20 70 3b 0a 20 20 20 20 20 20  irst = p;.      
24acd 20 20 77 68 69 6c 65 28 20 70 46 69 72 73 74 2d    while( pFirst-
24ace 3e 70 50 72 69 6f 72 20 29 20 70 46 69 72 73 74  >pPrior ) pFirst
24acf 20 3d 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f   = pFirst->pPrio
24ad0 72 3b 0a 20 20 20 20 20 20 20 20 67 65 6e 65 72  r;.        gener
24ad1 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70  ateColumnNames(p
24ad2 50 61 72 73 65 2c 20 30 2c 20 70 46 69 72 73 74  Parse, 0, pFirst
24ad3 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 20  ->pEList);.     
24ad4 20 7d 0a 20 20 20 20 20 20 69 42 72 65 61 6b 20   }.      iBreak 
24ad5 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
24ad6 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20  eLabel(v);.     
24ad7 20 69 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33   iCont = sqlite3
24ad8 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
24ad9 3b 0a 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4c  ;.      computeL
24ada 69 6d 69 74 52 65 67 69 73 74 65 72 73 28 70 50  imitRegisters(pP
24adb 61 72 73 65 2c 20 70 2c 20 69 42 72 65 61 6b 29  arse, p, iBreak)
24adc 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
24add 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
24ade 52 65 77 69 6e 64 2c 20 74 61 62 31 2c 20 69 42  Rewind, tab1, iB
24adf 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 72 31 20  reak);.      r1 
24ae0 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
24ae1 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
24ae2 20 20 20 69 53 74 61 72 74 20 3d 20 73 71 6c 69     iStart = sqli
24ae3 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
24ae4 20 4f 50 5f 52 6f 77 4b 65 79 2c 20 74 61 62 31   OP_RowKey, tab1
24ae5 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  , r1);.      sql
24ae6 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
24ae7 2c 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 2c 20 74  , OP_NotFound, t
24ae8 61 62 32 2c 20 69 43 6f 6e 74 2c 20 72 31 29 3b  ab2, iCont, r1);
24ae9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
24aea 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
24aeb 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20  rse, r1);.      
24aec 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28  selectInnerLoop(
24aed 70 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 45  pParse, p, p->pE
24aee 4c 69 73 74 2c 20 74 61 62 31 2c 20 70 2d 3e 70  List, tab1, p->p
24aef 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 0a 20 20  EList->nExpr,.  
24af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24af1 20 20 20 20 30 2c 20 2d 31 2c 20 26 64 65 73 74      0, -1, &dest
24af2 2c 20 69 43 6f 6e 74 2c 20 69 42 72 65 61 6b 29  , iCont, iBreak)
24af3 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
24af4 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
24af5 76 2c 20 69 43 6f 6e 74 29 3b 0a 20 20 20 20 20  v, iCont);.     
24af6 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24af7 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 74  p2(v, OP_Next, t
24af8 61 62 31 2c 20 69 53 74 61 72 74 29 3b 0a 20 20  ab1, iStart);.  
24af9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
24afa 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69  esolveLabel(v, i
24afb 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 73 71  Break);.      sq
24afc 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
24afd 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 74 61 62  v, OP_Close, tab
24afe 32 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c  2, 0);.      sql
24aff 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
24b00 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 74 61 62 31  , OP_Close, tab1
24b01 2c 20 30 29 3b 0a 20 20 20 20 20 20 62 72 65 61  , 0);.      brea
24b02 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  k;.    }.  }..  
24b03 2f 2a 20 43 6f 6d 70 75 74 65 20 63 6f 6c 6c 61  /* Compute colla
24b04 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 75  ting sequences u
24b05 73 65 64 20 62 79 20 0a 20 20 2a 2a 20 74 65 6d  sed by .  ** tem
24b06 70 6f 72 61 72 79 20 74 61 62 6c 65 73 20 6e 65  porary tables ne
24b07 65 64 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  eded to implemen
24b08 74 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73  t the compound s
24b09 65 6c 65 63 74 2e 0a 20 20 2a 2a 20 41 74 74 61  elect..  ** Atta
24b0a 63 68 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73  ch the KeyInfo s
24b0b 74 72 75 63 74 75 72 65 20 74 6f 20 61 6c 6c 20  tructure to all 
24b0c 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73  temporary tables
24b0d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73  ..  **.  ** This
24b0e 20 73 65 63 74 69 6f 6e 20 69 73 20 72 75 6e 20   section is run 
24b0f 62 79 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73  by the right-mos
24b10 74 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  t SELECT stateme
24b11 6e 74 20 6f 6e 6c 79 2e 0a 20 20 2a 2a 20 53 45  nt only..  ** SE
24b12 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20  LECT statements 
24b13 74 6f 20 74 68 65 20 6c 65 66 74 20 61 6c 77 61  to the left alwa
24b14 79 73 20 73 6b 69 70 20 74 68 69 73 20 70 61 72  ys skip this par
24b15 74 2e 20 20 54 68 65 20 72 69 67 68 74 2d 6d 6f  t.  The right-mo
24b16 73 74 0a 20 20 2a 2a 20 53 45 4c 45 43 54 20 6d  st.  ** SELECT m
24b17 69 67 68 74 20 61 6c 73 6f 20 73 6b 69 70 20 74  ight also skip t
24b18 68 69 73 20 70 61 72 74 20 69 66 20 69 74 20 68  his part if it h
24b19 61 73 20 6e 6f 20 4f 52 44 45 52 20 42 59 20 63  as no ORDER BY c
24b1a 6c 61 75 73 65 20 61 6e 64 0a 20 20 2a 2a 20 6e  lause and.  ** n
24b1b 6f 20 74 65 6d 70 20 74 61 62 6c 65 73 20 61 72  o temp tables ar
24b1c 65 20 72 65 71 75 69 72 65 64 2e 0a 20 20 2a 2f  e required..  */
24b1d 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61  .  if( p->selFla
24b1e 67 73 20 26 20 53 46 5f 55 73 65 73 45 70 68 65  gs & SF_UsesEphe
24b1f 6d 65 72 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74  meral ){.    int
24b20 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
24b21 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
24b22 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
24b23 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49    KeyInfo *pKeyI
24b24 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  nfo;            
24b25 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71  /* Collating seq
24b26 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20 72 65  uence for the re
24b27 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 20 20  sult set */.    
24b28 53 65 6c 65 63 74 20 2a 70 4c 6f 6f 70 3b 20 20  Select *pLoop;  
24b29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
24b2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72   For looping thr
24b2b 6f 75 67 68 20 53 45 4c 45 43 54 20 73 74 61 74  ough SELECT stat
24b2c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 43 6f  ements */.    Co
24b2d 6c 6c 53 65 71 20 2a 2a 61 70 43 6f 6c 6c 3b 20  llSeq **apColl; 
24b2e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
24b2f 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f 75  or looping throu
24b30 67 68 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f  gh pKeyInfo->aCo
24b31 6c 6c 5b 5d 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ll[] */.    int 
24b32 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  nCol;           
24b33 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
24b34 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
24b35 6e 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a  n result set */.
24b36 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
24b37 70 52 69 67 68 74 6d 6f 73 74 3d 3d 70 20 29 3b  pRightmost==p );
24b38 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 70 2d 3e 70  .    nCol = p->p
24b39 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20  EList->nExpr;.  
24b3a 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 73 71 6c    pKeyInfo = sql
24b3b 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
24b3c 28 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20  (db,.           
24b3d 20 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65              size
24b3e 6f 66 28 2a 70 4b 65 79 49 6e 66 6f 29 2b 6e 43  of(*pKeyInfo)+nC
24b3f 6f 6c 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53  ol*(sizeof(CollS
24b40 65 71 2a 29 20 2b 20 31 29 29 3b 0a 20 20 20 20  eq*) + 1));.    
24b41 69 66 28 20 21 70 4b 65 79 49 6e 66 6f 20 29 7b  if( !pKeyInfo ){
24b42 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
24b43 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
24b44 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63  goto multi_selec
24b45 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a 0a 20 20  t_end;.    }..  
24b46 20 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 20    pKeyInfo->enc 
24b47 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 20 20 70  = ENC(db);.    p
24b48 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20  KeyInfo->nField 
24b49 3d 20 6e 43 6f 6c 3b 0a 0a 20 20 20 20 66 6f 72  = nCol;..    for
24b4a 28 69 3d 30 2c 20 61 70 43 6f 6c 6c 3d 70 4b 65  (i=0, apColl=pKe
24b4b 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 3b 20 69 3c  yInfo->aColl; i<
24b4c 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 61 70 43 6f 6c  nCol; i++, apCol
24b4d 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 2a 61 70 43  l++){.      *apC
24b4e 6f 6c 6c 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63  oll = multiSelec
24b4f 74 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  tCollSeq(pParse,
24b50 20 70 2c 20 69 29 3b 0a 20 20 20 20 20 20 69 66   p, i);.      if
24b51 28 20 30 3d 3d 2a 61 70 43 6f 6c 6c 20 29 7b 0a  ( 0==*apColl ){.
24b52 20 20 20 20 20 20 20 20 2a 61 70 43 6f 6c 6c 20          *apColl 
24b53 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b  = db->pDfltColl;
24b54 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
24b55 20 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d 70 3b      for(pLoop=p;
24b56 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70 4c   pLoop; pLoop=pL
24b57 6f 6f 70 2d 3e 70 50 72 69 6f 72 29 7b 0a 20 20  oop->pPrior){.  
24b58 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32      for(i=0; i<2
24b59 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
24b5a 69 6e 74 20 61 64 64 72 20 3d 20 70 4c 6f 6f 70  int addr = pLoop
24b5b 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 69  ->addrOpenEphm[i
24b5c 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61  ];.        if( a
24b5d 64 64 72 3c 30 20 29 7b 0a 20 20 20 20 20 20 20  ddr<0 ){.       
24b5e 20 20 20 2f 2a 20 49 66 20 5b 30 5d 20 69 73 20     /* If [0] is 
24b5f 75 6e 75 73 65 64 20 74 68 65 6e 20 5b 31 5d 20  unused then [1] 
24b60 69 73 20 61 6c 73 6f 20 75 6e 75 73 65 64 2e 20  is also unused. 
24b61 20 53 6f 20 77 65 20 63 61 6e 0a 20 20 20 20 20   So we can.     
24b62 20 20 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 73       ** always s
24b63 61 66 65 6c 79 20 61 62 6f 72 74 20 61 73 20 73  afely abort as s
24b64 6f 6f 6e 20 61 73 20 74 68 65 20 66 69 72 73 74  oon as the first
24b65 20 75 6e 75 73 65 64 20 73 6c 6f 74 20 69 73 20   unused slot is 
24b66 66 6f 75 6e 64 20 2a 2f 0a 20 20 20 20 20 20 20  found */.       
24b67 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 6f 70     assert( pLoop
24b68 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31  ->addrOpenEphm[1
24b69 5d 3c 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20  ]<0 );.         
24b6a 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
24b6b 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
24b6c 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c  3VdbeChangeP2(v,
24b6d 20 61 64 64 72 2c 20 6e 43 6f 6c 29 3b 0a 20 20   addr, nCol);.  
24b6e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
24b6f 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64  eChangeP4(v, add
24b70 72 2c 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e  r, (char*)pKeyIn
24b71 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b  fo, P4_KEYINFO);
24b72 0a 20 20 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e  .        pLoop->
24b73 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 69 5d 20  addrOpenEphm[i] 
24b74 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  = -1;.      }.  
24b75 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44    }.    sqlite3D
24b76 62 46 72 65 65 28 64 62 2c 20 70 4b 65 79 49 6e  bFree(db, pKeyIn
24b77 66 6f 29 3b 0a 20 20 7d 0a 0a 6d 75 6c 74 69 5f  fo);.  }..multi_
24b78 73 65 6c 65 63 74 5f 65 6e 64 3a 0a 20 20 70 44  select_end:.  pD
24b79 65 73 74 2d 3e 69 4d 65 6d 20 3d 20 64 65 73 74  est->iMem = dest
24b7a 2e 69 4d 65 6d 3b 0a 20 20 70 44 65 73 74 2d 3e  .iMem;.  pDest->
24b7b 6e 4d 65 6d 20 3d 20 64 65 73 74 2e 6e 4d 65 6d  nMem = dest.nMem
24b7c 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ;.  sqlite3Selec
24b7d 74 44 65 6c 65 74 65 28 64 62 2c 20 70 44 65 6c  tDelete(db, pDel
24b7e 65 74 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ete);.  return r
24b7f 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  c;.}.#endif /* S
24b80 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f  QLITE_OMIT_COMPO
24b81 55 4e 44 5f 53 45 4c 45 43 54 20 2a 2f 0a 0a 2f  UND_SELECT */../
24b82 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 6f 75 74  *.** Code an out
24b83 70 75 74 20 73 75 62 72 6f 75 74 69 6e 65 20 66  put subroutine f
24b84 6f 72 20 61 20 63 6f 72 6f 75 74 69 6e 65 20 69  or a coroutine i
24b85 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
24b86 20 61 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61   a.** SELECT sta
24b87 74 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tment..**.** The
24b88 20 64 61 74 61 20 74 6f 20 62 65 20 6f 75 74 70   data to be outp
24b89 75 74 20 69 73 20 63 6f 6e 74 61 69 6e 65 64 20  ut is contained 
24b8a 69 6e 20 70 49 6e 2d 3e 69 4d 65 6d 2e 20 20 54  in pIn->iMem.  T
24b8b 68 65 72 65 20 61 72 65 0a 2a 2a 20 70 49 6e 2d  here are.** pIn-
24b8c 3e 6e 4d 65 6d 20 63 6f 6c 75 6d 6e 73 20 74 6f  >nMem columns to
24b8d 20 62 65 20 6f 75 74 70 75 74 2e 20 20 70 44 65   be output.  pDe
24b8e 73 74 20 69 73 20 77 68 65 72 65 20 74 68 65 20  st is where the 
24b8f 6f 75 74 70 75 74 20 73 68 6f 75 6c 64 0a 2a 2a  output should.**
24b90 20 62 65 20 73 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20   be sent..**.** 
24b91 72 65 67 52 65 74 75 72 6e 20 69 73 20 74 68 65  regReturn is the
24b92 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 72   number of the r
24b93 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20  egister holding 
24b94 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65 0a 2a  the subroutine.*
24b95 2a 20 72 65 74 75 72 6e 20 61 64 64 72 65 73 73  * return address
24b96 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 65 67 50 72  ..**.** If regPr
24b97 65 76 3e 30 20 74 68 65 6e 20 69 74 20 69 73 20  ev>0 then it is 
24b98 61 20 74 68 65 20 66 69 72 73 74 20 72 65 67 69  a the first regi
24b99 73 74 65 72 20 69 6e 20 61 20 76 65 63 74 6f 72  ster in a vector
24b9a 20 74 68 61 74 0a 2a 2a 20 72 65 63 6f 72 64 73   that.** records
24b9b 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 6f 75   the previous ou
24b9c 74 70 75 74 2e 20 20 6d 65 6d 5b 72 65 67 50 72  tput.  mem[regPr
24b9d 65 76 5d 20 69 73 20 61 20 66 6c 61 67 20 74 68  ev] is a flag th
24b9e 61 74 20 69 73 20 66 61 6c 73 65 0a 2a 2a 20 69  at is false.** i
24b9f 66 20 74 68 65 72 65 20 68 61 73 20 62 65 65 6e  f there has been
24ba0 20 6e 6f 20 70 72 65 76 69 6f 75 73 20 6f 75 74   no previous out
24ba1 70 75 74 2e 20 20 49 66 20 72 65 67 50 72 65 76  put.  If regPrev
24ba2 3e 30 20 74 68 65 6e 20 63 6f 64 65 20 69 73 0a  >0 then code is.
24ba3 2a 2a 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20  ** generated to 
24ba4 73 75 70 70 72 65 73 73 20 64 75 70 6c 69 63 61  suppress duplica
24ba5 74 65 73 2e 20 20 70 4b 65 79 49 6e 66 6f 20 69  tes.  pKeyInfo i
24ba6 73 20 75 73 65 64 20 66 6f 72 20 63 6f 6d 70 61  s used for compa
24ba7 72 69 6e 67 0a 2a 2a 20 6b 65 79 73 2e 0a 2a 2a  ring.** keys..**
24ba8 0a 2a 2a 20 49 66 20 74 68 65 20 4c 49 4d 49 54  .** If the LIMIT
24ba9 20 66 6f 75 6e 64 20 69 6e 20 70 2d 3e 69 4c 69   found in p->iLi
24baa 6d 69 74 20 69 73 20 72 65 61 63 68 65 64 2c 20  mit is reached, 
24bab 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79  jump immediately
24bac 20 74 6f 0a 2a 2a 20 69 42 72 65 61 6b 2e 0a 2a   to.** iBreak..*
24bad 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 6e  /.static int gen
24bae 65 72 61 74 65 4f 75 74 70 75 74 53 75 62 72 6f  erateOutputSubro
24baf 75 74 69 6e 65 28 0a 20 20 50 61 72 73 65 20 2a  utine(.  Parse *
24bb0 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
24bb1 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
24bb2 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ext */.  Select 
24bb3 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
24bb4 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73   /* The SELECT s
24bb5 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65  tatement */.  Se
24bb6 6c 65 63 74 44 65 73 74 20 2a 70 49 6e 2c 20 20  lectDest *pIn,  
24bb7 20 20 20 20 20 20 2f 2a 20 43 6f 72 6f 75 74 69        /* Corouti
24bb8 6e 65 20 73 75 70 70 6c 79 69 6e 67 20 64 61 74  ne supplying dat
24bb9 61 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73  a */.  SelectDes
24bba 74 20 2a 70 44 65 73 74 2c 20 20 20 20 20 20 2f  t *pDest,      /
24bbb 2a 20 57 68 65 72 65 20 74 6f 20 73 65 6e 64 20  * Where to send 
24bbc 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e  the data */.  in
24bbd 74 20 72 65 67 52 65 74 75 72 6e 2c 20 20 20 20  t regReturn,    
24bbe 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74        /* The ret
24bbf 75 72 6e 20 61 64 64 72 65 73 73 20 72 65 67 69  urn address regi
24bc0 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65  ster */.  int re
24bc1 67 50 72 65 76 2c 20 20 20 20 20 20 20 20 20 20  gPrev,          
24bc2 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 72 65    /* Previous re
24bc3 73 75 6c 74 20 72 65 67 69 73 74 65 72 2e 20 20  sult register.  
24bc4 4e 6f 20 75 6e 69 71 75 65 6e 65 73 73 20 69 66  No uniqueness if
24bc5 20 30 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20   0 */.  KeyInfo 
24bc6 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20  *pKeyInfo,      
24bc7 2f 2a 20 46 6f 72 20 63 6f 6d 70 61 72 69 6e 67  /* For comparing
24bc8 20 77 69 74 68 20 70 72 65 76 69 6f 75 73 20 65   with previous e
24bc9 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20 70 34  ntry */.  int p4
24bca 74 79 70 65 2c 20 20 20 20 20 20 20 20 20 20 20  type,           
24bcb 20 20 2f 2a 20 54 68 65 20 70 34 20 74 79 70 65    /* The p4 type
24bcc 20 66 6f 72 20 70 4b 65 79 49 6e 66 6f 20 2a 2f   for pKeyInfo */
24bcd 0a 20 20 69 6e 74 20 69 42 72 65 61 6b 20 20 20  .  int iBreak   
24bce 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75             /* Ju
24bcf 6d 70 20 68 65 72 65 20 69 66 20 77 65 20 68 69  mp here if we hi
24bd0 74 20 74 68 65 20 4c 49 4d 49 54 20 2a 2f 0a 29  t the LIMIT */.)
24bd1 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  {.  Vdbe *v = pP
24bd2 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
24bd3 6e 74 20 69 43 6f 6e 74 69 6e 75 65 3b 0a 20 20  nt iContinue;.  
24bd4 69 6e 74 20 61 64 64 72 3b 0a 0a 20 20 61 64 64  int addr;..  add
24bd5 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  r = sqlite3VdbeC
24bd6 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20  urrentAddr(v);. 
24bd7 20 69 43 6f 6e 74 69 6e 75 65 20 3d 20 73 71 6c   iContinue = sql
24bd8 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
24bd9 6c 28 76 29 3b 0a 0a 20 20 2f 2a 20 53 75 70 70  l(v);..  /* Supp
24bda 72 65 73 73 20 64 75 70 6c 69 63 61 74 65 73 20  ress duplicates 
24bdb 66 6f 72 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50  for UNION, EXCEP
24bdc 54 2c 20 61 6e 64 20 49 4e 54 45 52 53 45 43 54  T, and INTERSECT
24bdd 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 65 67   .  */.  if( reg
24bde 50 72 65 76 20 29 7b 0a 20 20 20 20 69 6e 74 20  Prev ){.    int 
24bdf 6a 31 2c 20 6a 32 3b 0a 20 20 20 20 6a 31 20 3d  j1, j2;.    j1 =
24be0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24be1 70 31 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20  p1(v, OP_IfNot, 
24be2 72 65 67 50 72 65 76 29 3b 0a 20 20 20 20 6a 32  regPrev);.    j2
24be3 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
24be4 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6d 70 61  dOp4(v, OP_Compa
24be5 72 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 72  re, pIn->iMem, r
24be6 65 67 50 72 65 76 2b 31 2c 20 70 49 6e 2d 3e 6e  egPrev+1, pIn->n
24be7 4d 65 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Mem,.           
24be8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24be9 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e     (char*)pKeyIn
24bea 66 6f 2c 20 70 34 74 79 70 65 29 3b 0a 20 20 20  fo, p4type);.   
24beb 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24bec 70 33 28 76 2c 20 4f 50 5f 4a 75 6d 70 2c 20 6a  p3(v, OP_Jump, j
24bed 32 2b 32 2c 20 69 43 6f 6e 74 69 6e 75 65 2c 20  2+2, iContinue, 
24bee 6a 32 2b 32 29 3b 0a 20 20 20 20 73 71 6c 69 74  j2+2);.    sqlit
24bef 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
24bf0 2c 20 6a 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  , j1);.    sqlit
24bf1 65 33 45 78 70 72 43 6f 64 65 43 6f 70 79 28 70  e3ExprCodeCopy(p
24bf2 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d  Parse, pIn->iMem
24bf3 2c 20 72 65 67 50 72 65 76 2b 31 2c 20 70 49 6e  , regPrev+1, pIn
24bf4 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 73 71 6c  ->nMem);.    sql
24bf5 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
24bf6 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c  , OP_Integer, 1,
24bf7 20 72 65 67 50 72 65 76 29 3b 0a 20 20 7d 0a 20   regPrev);.  }. 
24bf8 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62 2d   if( pParse->db-
24bf9 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20  >mallocFailed ) 
24bfa 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20  return 0;..  /* 
24bfb 53 75 70 70 72 65 73 73 20 74 68 65 20 74 68 65  Suppress the the
24bfc 20 66 69 72 73 74 20 4f 46 46 53 45 54 20 65 6e   first OFFSET en
24bfd 74 72 69 65 73 20 69 66 20 74 68 65 72 65 20 69  tries if there i
24bfe 73 20 61 6e 20 4f 46 46 53 45 54 20 63 6c 61 75  s an OFFSET clau
24bff 73 65 0a 20 20 2a 2f 0a 20 20 63 6f 64 65 4f 66  se.  */.  codeOf
24c00 66 73 65 74 28 76 2c 20 70 2c 20 69 43 6f 6e 74  fset(v, p, iCont
24c01 69 6e 75 65 29 3b 0a 0a 20 20 73 77 69 74 63 68  inue);..  switch
24c02 28 20 70 44 65 73 74 2d 3e 65 44 65 73 74 20 29  ( pDest->eDest )
24c03 7b 0a 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74  {.    /* Store t
24c04 68 65 20 72 65 73 75 6c 74 20 61 73 20 64 61 74  he result as dat
24c05 61 20 75 73 69 6e 67 20 61 20 75 6e 69 71 75 65  a using a unique
24c06 20 6b 65 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20   key..    */.   
24c07 20 63 61 73 65 20 53 52 54 5f 54 61 62 6c 65 3a   case SRT_Table:
24c08 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 45 70  .    case SRT_Ep
24c09 68 65 6d 54 61 62 3a 20 7b 0a 20 20 20 20 20 20  hemTab: {.      
24c0a 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33  int r1 = sqlite3
24c0b 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
24c0c 65 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 32  e);.      int r2
24c0d 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
24c0e 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
24c0f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
24c10 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65  ddOp3(v, OP_Make
24c11 52 65 63 6f 72 64 2c 20 70 49 6e 2d 3e 69 4d 65  Record, pIn->iMe
24c12 6d 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 2c 20 72 31  m, pIn->nMem, r1
24c13 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
24c14 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
24c15 5f 4e 65 77 52 6f 77 69 64 2c 20 70 44 65 73 74  _NewRowid, pDest
24c16 2d 3e 69 50 61 72 6d 2c 20 72 32 29 3b 0a 20 20  ->iParm, r2);.  
24c17 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
24c18 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65  ddOp3(v, OP_Inse
24c19 72 74 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  rt, pDest->iParm
24c1a 2c 20 72 31 2c 20 72 32 29 3b 0a 20 20 20 20 20  , r1, r2);.     
24c1b 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
24c1c 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41  geP5(v, OPFLAG_A
24c1d 50 50 45 4e 44 29 3b 0a 20 20 20 20 20 20 73 71  PPEND);.      sq
24c1e 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
24c1f 52 65 67 28 70 50 61 72 73 65 2c 20 72 32 29 3b  Reg(pParse, r2);
24c20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
24c21 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
24c22 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20  rse, r1);.      
24c23 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69  break;.    }..#i
24c24 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
24c25 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20 2f  T_SUBQUERY.    /
24c26 2a 20 49 66 20 77 65 20 61 72 65 20 63 72 65 61  * If we are crea
24c27 74 69 6e 67 20 61 20 73 65 74 20 66 6f 72 20 61  ting a set for a
24c28 6e 20 22 65 78 70 72 20 49 4e 20 28 53 45 4c 45  n "expr IN (SELE
24c29 43 54 20 2e 2e 2e 29 22 20 63 6f 6e 73 74 72 75  CT ...)" constru
24c2a 63 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20  ct,.    ** then 
24c2b 74 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20  there should be 
24c2c 61 20 73 69 6e 67 6c 65 20 69 74 65 6d 20 6f 6e  a single item on
24c2d 20 74 68 65 20 73 74 61 63 6b 2e 20 20 57 72 69   the stack.  Wri
24c2e 74 65 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 69  te this.    ** i
24c2f 74 65 6d 20 69 6e 74 6f 20 74 68 65 20 73 65 74  tem into the set
24c30 20 74 61 62 6c 65 20 77 69 74 68 20 62 6f 67 75   table with bogu
24c31 73 20 64 61 74 61 2e 0a 20 20 20 20 2a 2f 0a 20  s data..    */. 
24c32 20 20 20 63 61 73 65 20 53 52 54 5f 53 65 74 3a     case SRT_Set:
24c33 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 31 3b   {.      int r1;
24c34 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
24c35 49 6e 2d 3e 6e 4d 65 6d 3d 3d 31 20 29 3b 0a 20  In->nMem==1 );. 
24c36 20 20 20 20 20 70 2d 3e 61 66 66 69 6e 69 74 79       p->affinity
24c37 20 3d 20 0a 20 20 20 20 20 20 20 20 20 73 71 6c   = .         sql
24c38 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e  ite3CompareAffin
24c39 69 74 79 28 70 2d 3e 70 45 4c 69 73 74 2d 3e 61  ity(p->pEList->a
24c3a 5b 30 5d 2e 70 45 78 70 72 2c 20 70 44 65 73 74  [0].pExpr, pDest
24c3b 2d 3e 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 20  ->affinity);.   
24c3c 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47     r1 = sqlite3G
24c3d 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
24c3e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
24c3f 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
24c40 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 70 49 6e  _MakeRecord, pIn
24c41 2d 3e 69 4d 65 6d 2c 20 31 2c 20 72 31 2c 20 26  ->iMem, 1, r1, &
24c42 70 2d 3e 61 66 66 69 6e 69 74 79 2c 20 31 29 3b  p->affinity, 1);
24c43 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
24c44 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43  prCacheAffinityC
24c45 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 70 49  hange(pParse, pI
24c46 6e 2d 3e 69 4d 65 6d 2c 20 31 29 3b 0a 20 20 20  n->iMem, 1);.   
24c47 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
24c48 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e  dOp2(v, OP_IdxIn
24c49 73 65 72 74 2c 20 70 44 65 73 74 2d 3e 69 50 61  sert, pDest->iPa
24c4a 72 6d 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73  rm, r1);.      s
24c4b 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
24c4c 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29  pReg(pParse, r1)
24c4d 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
24c4e 20 20 20 7d 0a 0a 23 69 66 20 30 20 20 2f 2a 20     }..#if 0  /* 
24c4f 4e 65 76 65 72 20 6f 63 63 75 72 73 20 6f 6e 20  Never occurs on 
24c50 61 6e 20 4f 52 44 45 52 20 42 59 20 71 75 65 72  an ORDER BY quer
24c51 79 20 2a 2f 0a 20 20 20 20 2f 2a 20 49 66 20 61  y */.    /* If a
24c52 6e 79 20 72 6f 77 20 65 78 69 73 74 20 69 6e 20  ny row exist in 
24c53 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2c 20  the result set, 
24c54 72 65 63 6f 72 64 20 74 68 61 74 20 66 61 63 74  record that fact
24c55 20 61 6e 64 20 61 62 6f 72 74 2e 0a 20 20 20 20   and abort..    
24c56 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  */.    case SRT_
24c57 45 78 69 73 74 73 3a 20 7b 0a 20 20 20 20 20 20  Exists: {.      
24c58 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24c59 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
24c5a 20 31 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d   1, pDest->iParm
24c5b 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20  );.      /* The 
24c5c 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 77 69 6c  LIMIT clause wil
24c5d 6c 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20  l terminate the 
24c5e 6c 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20  loop for us */. 
24c5f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
24c60 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a  }.#endif..    /*
24c61 20 49 66 20 74 68 69 73 20 69 73 20 61 20 73 63   If this is a sc
24c62 61 6c 61 72 20 73 65 6c 65 63 74 20 74 68 61 74  alar select that
24c63 20 69 73 20 70 61 72 74 20 6f 66 20 61 6e 20 65   is part of an e
24c64 78 70 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e 0a  xpression, then.
24c65 20 20 20 20 2a 2a 20 73 74 6f 72 65 20 74 68 65      ** store the
24c66 20 72 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20   results in the 
24c67 61 70 70 72 6f 70 72 69 61 74 65 20 6d 65 6d 6f  appropriate memo
24c68 72 79 20 63 65 6c 6c 20 61 6e 64 20 62 72 65 61  ry cell and brea
24c69 6b 20 6f 75 74 0a 20 20 20 20 2a 2a 20 6f 66 20  k out.    ** of 
24c6a 74 68 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20  the scan loop.. 
24c6b 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53     */.    case S
24c6c 52 54 5f 4d 65 6d 3a 20 7b 0a 20 20 20 20 20 20  RT_Mem: {.      
24c6d 61 73 73 65 72 74 28 20 70 49 6e 2d 3e 6e 4d 65  assert( pIn->nMe
24c6e 6d 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71  m==1 );.      sq
24c6f 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76  lite3ExprCodeMov
24c70 65 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69  e(pParse, pIn->i
24c71 4d 65 6d 2c 20 70 44 65 73 74 2d 3e 69 50 61 72  Mem, pDest->iPar
24c72 6d 2c 20 31 29 3b 0a 20 20 20 20 20 20 2f 2a 20  m, 1);.      /* 
24c73 54 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65  The LIMIT clause
24c74 20 77 69 6c 6c 20 6a 75 6d 70 20 6f 75 74 20 6f   will jump out o
24c75 66 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75  f the loop for u
24c76 73 20 2a 2f 0a 20 20 20 20 20 20 62 72 65 61 6b  s */.      break
24c77 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f  ;.    }.#endif /
24c78 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  * #ifndef SQLITE
24c79 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a  _OMIT_SUBQUERY *
24c7a 2f 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65  /..    /* The re
24c7b 73 75 6c 74 73 20 61 72 65 20 73 74 6f 72 65 64  sults are stored
24c7c 20 69 6e 20 61 20 73 65 71 75 65 6e 63 65 20 6f   in a sequence o
24c7d 66 20 72 65 67 69 73 74 65 72 73 0a 20 20 20 20  f registers.    
24c7e 2a 2a 20 73 74 61 72 74 69 6e 67 20 61 74 20 70  ** starting at p
24c7f 44 65 73 74 2d 3e 69 4d 65 6d 2e 20 20 54 68 65  Dest->iMem.  The
24c80 6e 20 74 68 65 20 63 6f 2d 72 6f 75 74 69 6e 65  n the co-routine
24c81 20 79 69 65 6c 64 73 2e 0a 20 20 20 20 2a 2f 0a   yields..    */.
24c82 20 20 20 20 63 61 73 65 20 53 52 54 5f 43 6f 72      case SRT_Cor
24c83 6f 75 74 69 6e 65 3a 20 7b 0a 20 20 20 20 20 20  outine: {.      
24c84 69 66 28 20 70 44 65 73 74 2d 3e 69 4d 65 6d 3d  if( pDest->iMem=
24c85 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44  =0 ){.        pD
24c86 65 73 74 2d 3e 69 4d 65 6d 20 3d 20 73 71 6c 69  est->iMem = sqli
24c87 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28  te3GetTempRange(
24c88 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 6e 4d 65  pParse, pIn->nMe
24c89 6d 29 3b 0a 20 20 20 20 20 20 20 20 70 44 65 73  m);.        pDes
24c8a 74 2d 3e 6e 4d 65 6d 20 3d 20 70 49 6e 2d 3e 6e  t->nMem = pIn->n
24c8b 4d 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  Mem;.      }.   
24c8c 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
24c8d 64 65 4d 6f 76 65 28 70 50 61 72 73 65 2c 20 70  deMove(pParse, p
24c8e 49 6e 2d 3e 69 4d 65 6d 2c 20 70 44 65 73 74 2d  In->iMem, pDest-
24c8f 3e 69 4d 65 6d 2c 20 70 44 65 73 74 2d 3e 6e 4d  >iMem, pDest->nM
24c90 65 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  em);.      sqlit
24c91 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
24c92 4f 50 5f 59 69 65 6c 64 2c 20 70 44 65 73 74 2d  OP_Yield, pDest-
24c93 3e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 62  >iParm);.      b
24c94 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
24c95 20 2f 2a 20 52 65 73 75 6c 74 73 20 61 72 65 20   /* Results are 
24c96 73 74 6f 72 65 64 20 69 6e 20 61 20 73 65 71 75  stored in a sequ
24c97 65 6e 63 65 20 6f 66 20 72 65 67 69 73 74 65 72  ence of register
24c98 73 2e 20 20 54 68 65 6e 20 74 68 65 0a 20 20 20  s.  Then the.   
24c99 20 2a 2a 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77   ** OP_ResultRow
24c9a 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64 20   opcode is used 
24c9b 74 6f 20 63 61 75 73 65 20 73 71 6c 69 74 65 33  to cause sqlite3
24c9c 5f 73 74 65 70 28 29 20 74 6f 20 72 65 74 75 72  _step() to retur
24c9d 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 78  n.    ** the nex
24c9e 74 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 2e  t row of result.
24c9f 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
24ca0 20 53 52 54 5f 4f 75 74 70 75 74 3a 20 7b 0a 20   SRT_Output: {. 
24ca1 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
24ca2 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73  AddOp2(v, OP_Res
24ca3 75 6c 74 52 6f 77 2c 20 70 49 6e 2d 3e 69 4d 65  ultRow, pIn->iMe
24ca4 6d 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 29 3b 0a 20  m, pIn->nMem);. 
24ca5 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
24ca6 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
24ca7 6e 67 65 28 70 50 61 72 73 65 2c 20 70 49 6e 2d  nge(pParse, pIn-
24ca8 3e 69 4d 65 6d 2c 20 70 49 6e 2d 3e 6e 4d 65 6d  >iMem, pIn->nMem
24ca9 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
24caa 20 20 20 20 7d 0a 0a 23 69 66 20 21 64 65 66 69      }..#if !defi
24cab 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
24cac 54 52 49 47 47 45 52 29 0a 20 20 20 20 2f 2a 20  TRIGGER).    /* 
24cad 44 69 73 63 61 72 64 20 74 68 65 20 72 65 73 75  Discard the resu
24cae 6c 74 73 2e 20 20 54 68 69 73 20 69 73 20 75 73  lts.  This is us
24caf 65 64 20 66 6f 72 20 53 45 4c 45 43 54 20 73 74  ed for SELECT st
24cb0 61 74 65 6d 65 6e 74 73 20 69 6e 73 69 64 65 0a  atements inside.
24cb1 20 20 20 20 2a 2a 20 74 68 65 20 62 6f 64 79 20      ** the body 
24cb2 6f 66 20 61 20 54 52 49 47 47 45 52 2e 20 20 54  of a TRIGGER.  T
24cb3 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 73 75  he purpose of su
24cb4 63 68 20 73 65 6c 65 63 74 73 20 69 73 20 74 6f  ch selects is to
24cb5 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 75 73 65   call.    ** use
24cb6 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  r-defined functi
24cb7 6f 6e 73 20 74 68 61 74 20 68 61 76 65 20 73 69  ons that have si
24cb8 64 65 20 65 66 66 65 63 74 73 2e 20 20 57 65 20  de effects.  We 
24cb9 64 6f 20 6e 6f 74 20 63 61 72 65 0a 20 20 20 20  do not care.    
24cba 2a 2a 20 61 62 6f 75 74 20 74 68 65 20 61 63 74  ** about the act
24cbb 75 61 6c 20 72 65 73 75 6c 74 73 20 6f 66 20 74  ual results of t
24cbc 68 65 20 73 65 6c 65 63 74 2e 0a 20 20 20 20 2a  he select..    *
24cbd 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b  /.    default: {
24cbe 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
24cbf 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a    }.#endif.  }..
24cc0 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65    /* Jump to the
24cc1 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70   end of the loop
24cc2 20 69 66 20 74 68 65 20 4c 49 4d 49 54 20 69 73   if the LIMIT is
24cc3 20 72 65 61 63 68 65 64 2e 0a 20 20 2a 2f 0a 20   reached..  */. 
24cc4 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20 29   if( p->iLimit )
24cc5 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
24cc6 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64  eAddOp2(v, OP_Ad
24cc7 64 49 6d 6d 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c  dImm, p->iLimit,
24cc8 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   -1);.    sqlite
24cc9 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
24cca 50 5f 49 66 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69  P_IfZero, p->iLi
24ccb 6d 69 74 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20  mit, iBreak);.  
24ccc 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  }..  /* Generate
24ccd 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65 20   the subroutine 
24cce 72 65 74 75 72 6e 0a 20 20 2a 2f 0a 20 20 73 71  return.  */.  sq
24ccf 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
24cd0 4c 61 62 65 6c 28 76 2c 20 69 43 6f 6e 74 69 6e  Label(v, iContin
24cd1 75 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ue);.  sqlite3Vd
24cd2 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52  beAddOp1(v, OP_R
24cd3 65 74 75 72 6e 2c 20 72 65 67 52 65 74 75 72 6e  eturn, regReturn
24cd4 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 61 64 64  );..  return add
24cd5 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 74 65  r;.}../*.** Alte
24cd6 72 6e 61 74 69 76 65 20 63 6f 6d 70 6f 75 6e 64  rnative compound
24cd7 20 73 65 6c 65 63 74 20 63 6f 64 65 20 67 65 6e   select code gen
24cd8 65 72 61 74 6f 72 20 66 6f 72 20 63 61 73 65 73  erator for cases
24cd9 20 77 68 65 6e 20 74 68 65 72 65 0a 2a 2a 20 69   when there.** i
24cda 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c  s an ORDER BY cl
24cdb 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 61  ause..**.** We a
24cdc 73 73 75 6d 65 20 61 20 71 75 65 72 79 20 6f 66  ssume a query of
24cdd 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   the following f
24cde 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  orm:.**.**      
24cdf 3c 73 65 6c 65 63 74 41 3e 20 20 3c 6f 70 65 72  <selectA>  <oper
24ce0 61 74 6f 72 3e 20 20 3c 73 65 6c 65 63 74 42 3e  ator>  <selectB>
24ce1 20 20 4f 52 44 45 52 20 42 59 20 3c 6f 72 64 65    ORDER BY <orde
24ce2 72 62 79 6c 69 73 74 3e 0a 2a 2a 0a 2a 2a 20 3c  rbylist>.**.** <
24ce3 6f 70 65 72 61 74 6f 72 3e 20 69 73 20 6f 6e 65  operator> is one
24ce4 20 6f 66 20 55 4e 49 4f 4e 20 41 4c 4c 2c 20 55   of UNION ALL, U
24ce5 4e 49 4f 4e 2c 20 45 58 43 45 50 54 2c 20 6f 72  NION, EXCEPT, or
24ce6 20 49 4e 54 45 52 53 45 43 54 2e 20 20 54 68 65   INTERSECT.  The
24ce7 20 69 64 65 61 0a 2a 2a 20 69 73 20 74 6f 20 63   idea.** is to c
24ce8 6f 64 65 20 62 6f 74 68 20 3c 73 65 6c 65 63 74  ode both <select
24ce9 41 3e 20 61 6e 64 20 3c 73 65 6c 65 63 74 42 3e  A> and <selectB>
24cea 20 77 69 74 68 20 74 68 65 20 4f 52 44 45 52 20   with the ORDER 
24ceb 42 59 20 63 6c 61 75 73 65 20 61 73 0a 2a 2a 20  BY clause as.** 
24cec 63 6f 2d 72 6f 75 74 69 6e 65 73 2e 20 20 54 68  co-routines.  Th
24ced 65 6e 20 72 75 6e 20 74 68 65 20 63 6f 2d 72 6f  en run the co-ro
24cee 75 74 69 6e 65 73 20 69 6e 20 70 61 72 61 6c 6c  utines in parall
24cef 65 6c 20 61 6e 64 20 6d 65 72 67 65 20 74 68 65  el and merge the
24cf0 20 72 65 73 75 6c 74 73 0a 2a 2a 20 69 6e 74 6f   results.** into
24cf1 20 74 68 65 20 6f 75 74 70 75 74 2e 20 20 49 6e   the output.  In
24cf2 20 61 64 64 69 74 69 6f 6e 20 74 6f 20 74 68 65   addition to the
24cf3 20 74 77 6f 20 63 6f 72 6f 75 74 69 6e 65 73 20   two coroutines 
24cf4 28 63 61 6c 6c 65 64 20 73 65 6c 65 63 74 41 20  (called selectA 
24cf5 61 6e 64 0a 2a 2a 20 73 65 6c 65 63 74 42 29 20  and.** selectB) 
24cf6 74 68 65 72 65 20 61 72 65 20 37 20 73 75 62 72  there are 7 subr
24cf7 6f 75 74 69 6e 65 73 3a 0a 2a 2a 0a 2a 2a 20 20  outines:.**.**  
24cf8 20 20 6f 75 74 41 3a 20 20 20 20 4d 6f 76 65 20    outA:    Move 
24cf9 74 68 65 20 6f 75 74 70 75 74 20 6f 66 20 74 68  the output of th
24cfa 65 20 73 65 6c 65 63 74 41 20 63 6f 72 6f 75 74  e selectA corout
24cfb 69 6e 65 20 69 6e 74 6f 20 74 68 65 20 6f 75 74  ine into the out
24cfc 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  put.**          
24cfd 20 20 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75     of the compou
24cfe 6e 64 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20  nd query..**.** 
24cff 20 20 20 6f 75 74 42 3a 20 20 20 20 4d 6f 76 65     outB:    Move
24d00 20 74 68 65 20 6f 75 74 70 75 74 20 6f 66 20 74   the output of t
24d01 68 65 20 73 65 6c 65 63 74 42 20 63 6f 72 6f 75  he selectB corou
24d02 74 69 6e 65 20 69 6e 74 6f 20 74 68 65 20 6f 75  tine into the ou
24d03 74 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  tput.**         
24d04 20 20 20 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f      of the compo
24d05 75 6e 64 20 71 75 65 72 79 2e 20 20 28 4f 6e 6c  und query.  (Onl
24d06 79 20 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20  y generated for 
24d07 55 4e 49 4f 4e 20 61 6e 64 0a 2a 2a 20 20 20 20  UNION and.**    
24d08 20 20 20 20 20 20 20 20 20 55 4e 49 4f 4e 20 41           UNION A
24d09 4c 4c 2e 20 20 45 58 43 45 50 54 20 61 6e 64 20  LL.  EXCEPT and 
24d0a 49 4e 53 45 52 54 53 45 43 54 20 6e 65 76 65 72  INSERTSECT never
24d0b 20 6f 75 74 70 75 74 20 61 20 72 6f 77 20 74 68   output a row th
24d0c 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  at.**           
24d0d 20 20 61 70 70 65 61 72 73 20 6f 6e 6c 79 20 69    appears only i
24d0e 6e 20 42 2e 29 0a 2a 2a 0a 2a 2a 20 20 20 20 41  n B.).**.**    A
24d0f 6c 74 42 3a 20 20 20 20 43 61 6c 6c 65 64 20 77  ltB:    Called w
24d10 68 65 6e 20 74 68 65 72 65 20 69 73 20 64 61 74  hen there is dat
24d11 61 20 66 72 6f 6d 20 62 6f 74 68 20 63 6f 72 6f  a from both coro
24d12 75 74 69 6e 65 73 20 61 6e 64 20 41 3c 42 2e 0a  utines and A<B..
24d13 2a 2a 0a 2a 2a 20 20 20 20 41 65 71 42 3a 20 20  **.**    AeqB:  
24d14 20 20 43 61 6c 6c 65 64 20 77 68 65 6e 20 74 68    Called when th
24d15 65 72 65 20 69 73 20 64 61 74 61 20 66 72 6f 6d  ere is data from
24d16 20 62 6f 74 68 20 63 6f 72 6f 75 74 69 6e 65 73   both coroutines
24d17 20 61 6e 64 20 41 3d 3d 42 2e 0a 2a 2a 0a 2a 2a   and A==B..**.**
24d18 20 20 20 20 41 67 74 42 3a 20 20 20 20 43 61 6c      AgtB:    Cal
24d19 6c 65 64 20 77 68 65 6e 20 74 68 65 72 65 20 69  led when there i
24d1a 73 20 64 61 74 61 20 66 72 6f 6d 20 62 6f 74 68  s data from both
24d1b 20 63 6f 72 6f 75 74 69 6e 65 73 20 61 6e 64 20   coroutines and 
24d1c 41 3e 42 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 45 6f  A>B..**.**    Eo
24d1d 66 41 3a 20 20 20 20 43 61 6c 6c 65 64 20 77 68  fA:    Called wh
24d1e 65 6e 20 64 61 74 61 20 69 73 20 65 78 68 61 75  en data is exhau
24d1f 73 74 65 64 20 66 72 6f 6d 20 73 65 6c 65 63 74  sted from select
24d20 41 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 45 6f 66 42  A..**.**    EofB
24d21 3a 20 20 20 20 43 61 6c 6c 65 64 20 77 68 65 6e  :    Called when
24d22 20 64 61 74 61 20 69 73 20 65 78 68 61 75 73 74   data is exhaust
24d23 65 64 20 66 72 6f 6d 20 73 65 6c 65 63 74 42 2e  ed from selectB.
24d24 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65  .**.** The imple
24d25 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
24d26 20 6c 61 74 74 65 72 20 66 69 76 65 20 73 75 62   latter five sub
24d27 72 6f 75 74 69 6e 65 73 20 64 65 70 65 6e 64 20  routines depend 
24d28 6f 6e 20 77 68 69 63 68 20 0a 2a 2a 20 3c 6f 70  on which .** <op
24d29 65 72 61 74 6f 72 3e 20 69 73 20 75 73 65 64 3a  erator> is used:
24d2a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  .**.**.**       
24d2b 20 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 20        UNION ALL 
24d2c 20 20 20 20 20 20 20 20 55 4e 49 4f 4e 20 20 20          UNION   
24d2d 20 20 20 20 20 20 20 20 20 45 58 43 45 50 54 20           EXCEPT 
24d2e 20 20 20 20 20 20 20 20 20 49 4e 54 45 52 53 45           INTERSE
24d2f 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 2d  CT.**          -
24d30 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d  ------------  --
24d31 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20  --------------- 
24d32 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20   -------------- 
24d33 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
24d34 2d 2d 0a 2a 2a 20 20 20 41 6c 74 42 3a 20 20 20  --.**   AltB:   
24d35 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20  outA, nextA     
24d36 20 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20   outA, nextA    
24d37 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20 20     outA, nextA  
24d38 20 20 20 20 20 20 20 6e 65 78 74 41 0a 2a 2a 0a         nextA.**.
24d39 2a 2a 20 20 20 41 65 71 42 3a 20 20 20 6f 75 74  **   AeqB:   out
24d3a 41 2c 20 6e 65 78 74 41 20 20 20 20 20 20 20 20  A, nextA        
24d3b 20 6e 65 78 74 41 20 20 20 20 20 20 20 20 20 20   nextA          
24d3c 20 20 20 6e 65 78 74 41 20 20 20 20 20 20 20 20     nextA        
24d3d 20 6f 75 74 41 2c 20 6e 65 78 74 41 0a 2a 2a 0a   outA, nextA.**.
24d3e 2a 2a 20 20 20 41 67 74 42 3a 20 20 20 6f 75 74  **   AgtB:   out
24d3f 42 2c 20 6e 65 78 74 42 20 20 20 20 20 20 6f 75  B, nextB      ou
24d40 74 42 2c 20 6e 65 78 74 42 20 20 20 20 20 20 20  tB, nextB       
24d41 20 20 20 6e 65 78 74 42 20 20 20 20 20 20 20 20     nextB        
24d42 20 20 20 20 6e 65 78 74 42 0a 2a 2a 0a 2a 2a 20      nextB.**.** 
24d43 20 20 45 6f 66 41 3a 20 20 20 6f 75 74 42 2c 20    EofA:   outB, 
24d44 6e 65 78 74 42 20 20 20 20 20 20 6f 75 74 42 2c  nextB      outB,
24d45 20 6e 65 78 74 42 20 20 20 20 20 20 20 20 20 20   nextB          
24d46 68 61 6c 74 20 20 20 20 20 20 20 20 20 20 20 20  halt            
24d47 20 68 61 6c 74 0a 2a 2a 0a 2a 2a 20 20 20 45 6f   halt.**.**   Eo
24d48 66 42 3a 20 20 20 6f 75 74 41 2c 20 6e 65 78 74  fB:   outA, next
24d49 41 20 20 20 20 20 20 6f 75 74 41 2c 20 6e 65 78  A      outA, nex
24d4a 74 41 20 20 20 20 20 20 20 6f 75 74 41 2c 20 6e  tA       outA, n
24d4b 65 78 74 41 20 20 20 20 20 20 20 20 20 68 61 6c  extA         hal
24d4c 74 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 41  t.**.** In the A
24d4d 6c 74 42 2c 20 41 65 71 42 2c 20 61 6e 64 20 41  ltB, AeqB, and A
24d4e 67 74 42 20 73 75 62 72 6f 75 74 69 6e 65 73 2c  gtB subroutines,
24d4f 20 61 6e 20 45 4f 46 20 6f 6e 20 41 20 66 6f 6c   an EOF on A fol
24d50 6c 6f 77 69 6e 67 20 6e 65 78 74 41 0a 2a 2a 20  lowing nextA.** 
24d51 63 61 75 73 65 73 20 61 6e 20 69 6d 6d 65 64 69  causes an immedi
24d52 61 74 65 20 6a 75 6d 70 20 74 6f 20 45 6f 66 41  ate jump to EofA
24d53 20 61 6e 64 20 61 6e 20 45 4f 46 20 6f 6e 20 42   and an EOF on B
24d54 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 78 74 42   following nextB
24d55 20 63 61 75 73 65 73 0a 2a 2a 20 61 6e 20 69 6d   causes.** an im
24d56 6d 65 64 69 61 74 65 20 6a 75 6d 70 20 74 6f 20  mediate jump to 
24d57 45 6f 66 42 2e 20 20 57 69 74 68 69 6e 20 45 6f  EofB.  Within Eo
24d58 66 41 20 61 6e 64 20 45 6f 66 42 2c 20 61 6e 64  fA and EofB, and
24d59 20 45 4f 46 20 6f 6e 20 65 6e 74 72 79 20 6f 72   EOF on entry or
24d5a 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65  .** following ne
24d5b 78 74 58 20 63 61 75 73 65 73 20 61 20 6a 75 6d  xtX causes a jum
24d5c 70 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  p to the end of 
24d5d 74 68 65 20 73 65 6c 65 63 74 20 70 72 6f 63 65  the select proce
24d5e 73 73 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 44 75 70  ssing..**.** Dup
24d5f 6c 69 63 61 74 65 20 72 65 6d 6f 76 61 6c 20 69  licate removal i
24d60 6e 20 74 68 65 20 55 4e 49 4f 4e 2c 20 45 58 43  n the UNION, EXC
24d61 45 50 54 2c 20 61 6e 64 20 49 4e 54 45 52 53 45  EPT, and INTERSE
24d62 43 54 20 63 61 73 65 73 20 69 73 20 68 61 6e 64  CT cases is hand
24d63 6c 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68  led.** within th
24d64 65 20 6f 75 74 70 75 74 20 73 75 62 72 6f 75 74  e output subrout
24d65 69 6e 65 2e 20 20 54 68 65 20 72 65 67 50 72 65  ine.  The regPre
24d66 76 20 72 65 67 69 73 74 65 72 20 73 65 74 20 68  v register set h
24d67 6f 6c 64 73 20 74 68 65 20 70 72 65 76 69 6f 75  olds the previou
24d68 73 6c 79 0a 2a 2a 20 6f 75 74 70 75 74 20 76 61  sly.** output va
24d69 6c 75 65 2e 20 20 41 20 63 6f 6d 70 61 72 69 73  lue.  A comparis
24d6a 6f 6e 20 69 73 20 6d 61 64 65 20 61 67 61 69 6e  on is made again
24d6b 73 74 20 74 68 69 73 20 76 61 6c 75 65 20 61 6e  st this value an
24d6c 64 20 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20  d the output.** 
24d6d 69 73 20 73 6b 69 70 70 65 64 20 69 66 20 74 68  is skipped if th
24d6e 65 20 6e 65 78 74 20 72 65 73 75 6c 74 73 20 77  e next results w
24d6f 6f 75 6c 64 20 62 65 20 74 68 65 20 73 61 6d 65  ould be the same
24d70 20 61 73 20 74 68 65 20 70 72 65 76 69 6f 75 73   as the previous
24d71 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c  ..**.** The impl
24d72 65 6d 65 6e 74 61 74 69 6f 6e 20 70 6c 61 6e 20  ementation plan 
24d73 69 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  is to implement 
24d74 74 68 65 20 74 77 6f 20 63 6f 72 6f 75 74 69 6e  the two coroutin
24d75 65 73 20 61 6e 64 20 73 65 76 65 6e 0a 2a 2a 20  es and seven.** 
24d76 73 75 62 72 6f 75 74 69 6e 65 73 20 66 69 72 73  subroutines firs
24d77 74 2c 20 74 68 65 6e 20 70 75 74 20 74 68 65 20  t, then put the 
24d78 63 6f 6e 74 72 6f 6c 20 6c 6f 67 69 63 20 61 74  control logic at
24d79 20 74 68 65 20 62 6f 74 74 6f 6d 2e 20 20 4c 69   the bottom.  Li
24d7a 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  ke this:.**.**  
24d7b 20 20 20 20 20 20 20 20 67 6f 74 6f 20 49 6e 69          goto Ini
24d7c 74 0a 2a 2a 20 20 20 20 20 63 6f 41 3a 20 63 6f  t.**     coA: co
24d7d 72 6f 75 74 69 6e 65 20 66 6f 72 20 6c 65 66 74  routine for left
24d7e 20 71 75 65 72 79 20 28 41 29 0a 2a 2a 20 20 20   query (A).**   
24d7f 20 20 63 6f 42 3a 20 63 6f 72 6f 75 74 69 6e 65    coB: coroutine
24d80 20 66 6f 72 20 72 69 67 68 74 20 71 75 65 72 79   for right query
24d81 20 28 42 29 0a 2a 2a 20 20 20 20 6f 75 74 41 3a   (B).**    outA:
24d82 20 6f 75 74 70 75 74 20 6f 6e 65 20 72 6f 77 20   output one row 
24d83 6f 66 20 41 0a 2a 2a 20 20 20 20 6f 75 74 42 3a  of A.**    outB:
24d84 20 6f 75 74 70 75 74 20 6f 6e 65 20 72 6f 77 20   output one row 
24d85 6f 66 20 42 20 28 55 4e 49 4f 4e 20 61 6e 64 20  of B (UNION and 
24d86 55 4e 49 4f 4e 20 41 4c 4c 20 6f 6e 6c 79 29 0a  UNION ALL only).
24d87 2a 2a 20 20 20 20 45 6f 66 41 3a 20 2e 2e 2e 0a  **    EofA: ....
24d88 2a 2a 20 20 20 20 45 6f 66 42 3a 20 2e 2e 2e 0a  **    EofB: ....
24d89 2a 2a 20 20 20 20 41 6c 74 42 3a 20 2e 2e 2e 0a  **    AltB: ....
24d8a 2a 2a 20 20 20 20 41 65 71 42 3a 20 2e 2e 2e 0a  **    AeqB: ....
24d8b 2a 2a 20 20 20 20 41 67 74 42 3a 20 2e 2e 2e 0a  **    AgtB: ....
24d8c 2a 2a 20 20 20 20 49 6e 69 74 3a 20 69 6e 69 74  **    Init: init
24d8d 69 61 6c 69 7a 65 20 63 6f 72 6f 75 74 69 6e 65  ialize coroutine
24d8e 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 20 20   registers.**   
24d8f 20 20 20 20 20 20 20 79 69 65 6c 64 20 63 6f 41         yield coA
24d90 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 66 20  .**          if 
24d91 65 6f 66 28 41 29 20 67 6f 74 6f 20 45 6f 66 41  eof(A) goto EofA
24d92 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 79 69 65  .**          yie
24d93 6c 64 20 63 6f 42 0a 2a 2a 20 20 20 20 20 20 20  ld coB.**       
24d94 20 20 20 69 66 20 65 6f 66 28 42 29 20 67 6f 74     if eof(B) got
24d95 6f 20 45 6f 66 42 0a 2a 2a 20 20 20 20 43 6d 70  o EofB.**    Cmp
24d96 72 3a 20 43 6f 6d 70 61 72 65 20 41 2c 20 42 0a  r: Compare A, B.
24d97 2a 2a 20 20 20 20 20 20 20 20 20 20 4a 75 6d 70  **          Jump
24d98 20 41 6c 74 42 2c 20 41 65 71 42 2c 20 41 67 74   AltB, AeqB, Agt
24d99 42 0a 2a 2a 20 20 20 20 20 45 6e 64 3a 20 2e 2e  B.**     End: ..
24d9a 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 63 61 6c 6c 20  ..**.** We call 
24d9b 41 6c 74 42 2c 20 41 65 71 42 2c 20 41 67 74 42  AltB, AeqB, AgtB
24d9c 2c 20 45 6f 66 41 2c 20 61 6e 64 20 45 6f 66 42  , EofA, and EofB
24d9d 20 22 73 75 62 72 6f 75 74 69 6e 65 73 22 20 62   "subroutines" b
24d9e 75 74 20 74 68 65 79 20 61 72 65 20 6e 6f 74 0a  ut they are not.
24d9f 2a 2a 20 61 63 74 75 61 6c 6c 79 20 63 61 6c 6c  ** actually call
24da0 65 64 20 75 73 69 6e 67 20 47 6f 73 75 62 20 61  ed using Gosub a
24da1 6e 64 20 74 68 65 79 20 64 6f 20 6e 6f 74 20 52  nd they do not R
24da2 65 74 75 72 6e 2e 20 20 45 6f 66 41 20 61 6e 64  eturn.  EofA and
24da3 20 45 6f 66 42 20 6c 6f 6f 70 0a 2a 2a 20 75 6e   EofB loop.** un
24da4 74 69 6c 20 61 6c 6c 20 64 61 74 61 20 69 73 20  til all data is 
24da5 65 78 68 61 75 73 74 65 64 20 74 68 65 6e 20 6a  exhausted then j
24da6 75 6d 70 20 74 6f 20 74 68 65 20 22 65 6e 64 22  ump to the "end"
24da7 20 6c 61 62 65 2e 20 20 41 6c 74 42 2c 20 41 65   labe.  AltB, Ae
24da8 71 42 2c 0a 2a 2a 20 61 6e 64 20 41 67 74 42 20  qB,.** and AgtB 
24da9 6a 75 6d 70 20 74 6f 20 65 69 74 68 65 72 20 4c  jump to either L
24daa 32 20 6f 72 20 74 6f 20 6f 6e 65 20 6f 66 20 45  2 or to one of E
24dab 6f 66 41 20 6f 72 20 45 6f 66 42 2e 0a 2a 2f 0a  ofA or EofB..*/.
24dac 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
24dad 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MIT_COMPOUND_SEL
24dae 45 43 54 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  ECT.static int m
24daf 75 6c 74 69 53 65 6c 65 63 74 4f 72 64 65 72 42  ultiSelectOrderB
24db0 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  y(.  Parse *pPar
24db1 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61  se,        /* Pa
24db2 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
24db3 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20  .  Select *p,   
24db4 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
24db5 72 69 67 68 74 2d 6d 6f 73 74 20 6f 66 20 53 45  right-most of SE
24db6 4c 45 43 54 73 20 74 6f 20 62 65 20 63 6f 64 65  LECTs to be code
24db7 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73  d */.  SelectDes
24db8 74 20 2a 70 44 65 73 74 20 20 20 20 20 2f 2a 20  t *pDest     /* 
24db9 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20  What to do with 
24dba 71 75 65 72 79 20 72 65 73 75 6c 74 73 20 2a 2f  query results */
24dbb 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20  .){.  int i, j; 
24dbc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
24dbd 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a  oop counters */.
24dbe 20 20 53 65 6c 65 63 74 20 2a 70 50 72 69 6f 72    Select *pPrior
24dbf 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e 6f 74 68  ;       /* Anoth
24dc0 65 72 20 53 45 4c 45 43 54 20 69 6d 6d 65 64 69  er SELECT immedi
24dc1 61 74 65 6c 79 20 74 6f 20 6f 75 72 20 6c 65 66  ately to our lef
24dc2 74 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20  t */.  Vdbe *v; 
24dc3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
24dc4 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
24dc5 20 74 68 69 73 20 56 44 42 45 20 2a 2f 0a 20 20   this VDBE */.  
24dc6 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 41  SelectDest destA
24dc7 3b 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61  ;     /* Destina
24dc8 74 69 6f 6e 20 66 6f 72 20 63 6f 72 6f 75 74 69  tion for corouti
24dc9 6e 65 20 41 20 2a 2f 0a 20 20 53 65 6c 65 63 74  ne A */.  Select
24dca 44 65 73 74 20 64 65 73 74 42 3b 20 20 20 20 20  Dest destB;     
24dcb 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 66  /* Destination f
24dcc 6f 72 20 63 6f 72 6f 75 74 69 6e 65 20 42 20 2a  or coroutine B *
24dcd 2f 0a 20 20 69 6e 74 20 72 65 67 41 64 64 72 41  /.  int regAddrA
24dce 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64  ;         /* Add
24dcf 72 65 73 73 20 72 65 67 69 73 74 65 72 20 66 6f  ress register fo
24dd0 72 20 73 65 6c 65 63 74 2d 41 20 63 6f 72 6f 75  r select-A corou
24dd1 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65  tine */.  int re
24dd2 67 45 6f 66 41 3b 20 20 20 20 20 20 20 20 20 20  gEofA;          
24dd3 2f 2a 20 46 6c 61 67 20 74 6f 20 69 6e 64 69 63  /* Flag to indic
24dd4 61 74 65 20 77 68 65 6e 20 73 65 6c 65 63 74 2d  ate when select-
24dd5 41 20 69 73 20 63 6f 6d 70 6c 65 74 65 20 2a 2f  A is complete */
24dd6 0a 20 20 69 6e 74 20 72 65 67 41 64 64 72 42 3b  .  int regAddrB;
24dd7 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
24dd8 65 73 73 20 72 65 67 69 73 74 65 72 20 66 6f 72  ess register for
24dd9 20 73 65 6c 65 63 74 2d 42 20 63 6f 72 6f 75 74   select-B corout
24dda 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ine */.  int reg
24ddb 45 6f 66 42 3b 20 20 20 20 20 20 20 20 20 20 2f  EofB;          /
24ddc 2a 20 46 6c 61 67 20 74 6f 20 69 6e 64 69 63 61  * Flag to indica
24ddd 74 65 20 77 68 65 6e 20 73 65 6c 65 63 74 2d 42  te when select-B
24dde 20 69 73 20 63 6f 6d 70 6c 65 74 65 20 2a 2f 0a   is complete */.
24ddf 20 20 69 6e 74 20 61 64 64 72 53 65 6c 65 63 74    int addrSelect
24de0 41 3b 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65  A;      /* Addre
24de1 73 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74  ss of the select
24de2 2d 41 20 63 6f 72 6f 75 74 69 6e 65 20 2a 2f 0a  -A coroutine */.
24de3 20 20 69 6e 74 20 61 64 64 72 53 65 6c 65 63 74    int addrSelect
24de4 42 3b 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65  B;      /* Addre
24de5 73 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74  ss of the select
24de6 2d 42 20 63 6f 72 6f 75 74 69 6e 65 20 2a 2f 0a  -B coroutine */.
24de7 20 20 69 6e 74 20 72 65 67 4f 75 74 41 3b 20 20    int regOutA;  
24de8 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
24de9 73 73 20 72 65 67 69 73 74 65 72 20 66 6f 72 20  ss register for 
24dea 74 68 65 20 6f 75 74 70 75 74 2d 41 20 73 75 62  the output-A sub
24deb 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  routine */.  int
24dec 20 72 65 67 4f 75 74 42 3b 20 20 20 20 20 20 20   regOutB;       
24ded 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 72 65     /* Address re
24dee 67 69 73 74 65 72 20 66 6f 72 20 74 68 65 20 6f  gister for the o
24def 75 74 70 75 74 2d 42 20 73 75 62 72 6f 75 74 69  utput-B subrouti
24df0 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  ne */.  int addr
24df1 4f 75 74 41 3b 20 20 20 20 20 20 20 20 20 2f 2a  OutA;         /*
24df2 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   Address of the 
24df3 6f 75 74 70 75 74 2d 41 20 73 75 62 72 6f 75 74  output-A subrout
24df4 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  ine */.  int add
24df5 72 4f 75 74 42 3b 20 20 20 20 20 20 20 20 20 2f  rOutB;         /
24df6 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65  * Address of the
24df7 20 6f 75 74 70 75 74 2d 42 20 73 75 62 72 6f 75   output-B subrou
24df8 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64  tine */.  int ad
24df9 64 72 45 6f 66 41 3b 20 20 20 20 20 20 20 20 20  drEofA;         
24dfa 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68  /* Address of th
24dfb 65 20 73 65 6c 65 63 74 2d 41 2d 65 78 68 61 75  e select-A-exhau
24dfc 73 74 65 64 20 73 75 62 72 6f 75 74 69 6e 65 20  sted subroutine 
24dfd 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 45 6f 66  */.  int addrEof
24dfe 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64  B;         /* Ad
24dff 64 72 65 73 73 20 6f 66 20 74 68 65 20 73 65 6c  dress of the sel
24e00 65 63 74 2d 42 2d 65 78 68 61 75 73 74 65 64 20  ect-B-exhausted 
24e01 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20  subroutine */.  
24e02 69 6e 74 20 61 64 64 72 41 6c 74 42 3b 20 20 20  int addrAltB;   
24e03 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
24e04 20 6f 66 20 74 68 65 20 41 3c 42 20 73 75 62 72   of the A<B subr
24e05 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20  outine */.  int 
24e06 61 64 64 72 41 65 71 42 3b 20 20 20 20 20 20 20  addrAeqB;       
24e07 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
24e08 74 68 65 20 41 3d 3d 42 20 73 75 62 72 6f 75 74  the A==B subrout
24e09 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  ine */.  int add
24e0a 72 41 67 74 42 3b 20 20 20 20 20 20 20 20 20 2f  rAgtB;         /
24e0b 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65  * Address of the
24e0c 20 41 3e 42 20 73 75 62 72 6f 75 74 69 6e 65 20   A>B subroutine 
24e0d 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4c 69 6d 69  */.  int regLimi
24e0e 74 41 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 69  tA;        /* Li
24e0f 6d 69 74 20 72 65 67 69 73 74 65 72 20 66 6f 72  mit register for
24e10 20 73 65 6c 65 63 74 2d 41 20 2a 2f 0a 20 20 69   select-A */.  i
24e11 6e 74 20 72 65 67 4c 69 6d 69 74 42 3b 20 20 20  nt regLimitB;   
24e12 20 20 20 20 20 2f 2a 20 4c 69 6d 69 74 20 72 65       /* Limit re
24e13 67 69 73 74 65 72 20 66 6f 72 20 73 65 6c 65 63  gister for selec
24e14 74 2d 41 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  t-A */.  int reg
24e15 50 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 2f  Prev;          /
24e16 2a 20 41 20 72 61 6e 67 65 20 6f 66 20 72 65 67  * A range of reg
24e17 69 73 74 65 72 73 20 74 6f 20 68 6f 6c 64 20 70  isters to hold p
24e18 72 65 76 69 6f 75 73 20 6f 75 74 70 75 74 20 2a  revious output *
24e19 2f 0a 20 20 69 6e 74 20 73 61 76 65 64 4c 69 6d  /.  int savedLim
24e1a 69 74 3b 20 20 20 20 20 20 20 2f 2a 20 53 61 76  it;       /* Sav
24e1b 65 64 20 76 61 6c 75 65 20 6f 66 20 70 2d 3e 69  ed value of p->i
24e1c 4c 69 6d 69 74 20 2a 2f 0a 20 20 69 6e 74 20 73  Limit */.  int s
24e1d 61 76 65 64 4f 66 66 73 65 74 3b 20 20 20 20 20  avedOffset;     
24e1e 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65 20   /* Saved value 
24e1f 6f 66 20 70 2d 3e 69 4f 66 66 73 65 74 20 2a 2f  of p->iOffset */
24e20 0a 20 20 69 6e 74 20 6c 61 62 65 6c 43 6d 70 72  .  int labelCmpr
24e21 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 62 65  ;        /* Labe
24e22 6c 20 66 6f 72 20 74 68 65 20 73 74 61 72 74 20  l for the start 
24e23 6f 66 20 74 68 65 20 6d 65 72 67 65 20 61 6c 67  of the merge alg
24e24 6f 72 69 74 68 6d 20 2a 2f 0a 20 20 69 6e 74 20  orithm */.  int 
24e25 6c 61 62 65 6c 45 6e 64 3b 20 20 20 20 20 20 20  labelEnd;       
24e26 20 20 2f 2a 20 4c 61 62 65 6c 20 66 6f 72 20 74    /* Label for t
24e27 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6f 76  he end of the ov
24e28 65 72 61 6c 6c 20 53 45 4c 45 43 54 20 73 74 6d  erall SELECT stm
24e29 74 20 2a 2f 0a 20 20 69 6e 74 20 6a 31 3b 20 20  t */.  int j1;  
24e2a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
24e2b 4a 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e  Jump instruction
24e2c 73 20 74 68 61 74 20 67 65 74 20 72 65 74 61 72  s that get retar
24e2d 67 65 74 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20  getted */.  int 
24e2e 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
24e2f 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 41    /* One of TK_A
24e30 4c 4c 2c 20 54 4b 5f 55 4e 49 4f 4e 2c 20 54 4b  LL, TK_UNION, TK
24e31 5f 45 58 43 45 50 54 2c 20 54 4b 5f 49 4e 54 45  _EXCEPT, TK_INTE
24e32 52 53 45 43 54 20 2a 2f 0a 20 20 4b 65 79 49 6e  RSECT */.  KeyIn
24e33 66 6f 20 2a 70 4b 65 79 44 75 70 20 3d 20 30 3b  fo *pKeyDup = 0;
24e34 20 2f 2a 20 43 6f 6d 70 61 72 69 73 6f 6e 20 69   /* Comparison i
24e35 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 64  nformation for d
24e36 75 70 6c 69 63 61 74 65 20 72 65 6d 6f 76 61 6c  uplicate removal
24e37 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70   */.  KeyInfo *p
24e38 4b 65 79 4d 65 72 67 65 3b 20 20 20 2f 2a 20 43  KeyMerge;   /* C
24e39 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 66 6f 72 6d  omparison inform
24e3a 61 74 69 6f 6e 20 66 6f 72 20 6d 65 72 67 69 6e  ation for mergin
24e3b 67 20 72 6f 77 73 20 2a 2f 0a 20 20 73 71 6c 69  g rows */.  sqli
24e3c 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
24e3d 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
24e3e 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78  nnection */.  Ex
24e3f 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
24e40 3b 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52  ;   /* The ORDER
24e41 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20   BY clause */.  
24e42 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b 20 20 20  int nOrderBy;   
24e43 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
24e44 6f 66 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20  of terms in the 
24e45 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
24e46 2a 2f 0a 20 20 69 6e 74 20 2a 61 50 65 72 6d 75  */.  int *aPermu
24e47 74 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 61  te;        /* Ma
24e48 70 70 69 6e 67 20 66 72 6f 6d 20 4f 52 44 45 52  pping from ORDER
24e49 20 42 59 20 74 65 72 6d 73 20 74 6f 20 72 65 73   BY terms to res
24e4a 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20  ult set columns 
24e4b 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d  */..  assert( p-
24e4c 3e 70 4f 72 64 65 72 42 79 21 3d 30 20 29 3b 0a  >pOrderBy!=0 );.
24e4d 20 20 61 73 73 65 72 74 28 20 70 4b 65 79 44 75    assert( pKeyDu
24e4e 70 3d 3d 30 20 29 3b 20 2f 2a 20 22 4d 61 6e 61  p==0 ); /* "Mana
24e4f 67 65 64 22 20 63 6f 64 65 20 6e 65 65 64 73 20  ged" code needs 
24e50 74 68 69 73 2e 20 20 54 69 63 6b 65 74 20 23 33  this.  Ticket #3
24e51 33 38 32 2e 20 2a 2f 0a 20 20 64 62 20 3d 20 70  382. */.  db = p
24e52 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 76 20 3d  Parse->db;.  v =
24e53 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
24e54 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74    if( v==0 ) ret
24e55 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
24e56 3b 0a 20 20 6c 61 62 65 6c 45 6e 64 20 3d 20 73  ;.  labelEnd = s
24e57 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
24e58 62 65 6c 28 76 29 3b 0a 20 20 6c 61 62 65 6c 43  bel(v);.  labelC
24e59 6d 70 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  mpr = sqlite3Vdb
24e5a 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a  eMakeLabel(v);..
24e5b 0a 20 20 2f 2a 20 50 61 74 63 68 20 75 70 20 74  .  /* Patch up t
24e5c 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
24e5d 73 65 0a 20 20 2a 2f 0a 20 20 6f 70 20 3d 20 70  se.  */.  op = p
24e5e 2d 3e 6f 70 3b 20 20 0a 20 20 70 50 72 69 6f 72  ->op;  .  pPrior
24e5f 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20   = p->pPrior;.  
24e60 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 2d 3e  assert( pPrior->
24e61 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 3b 0a 20  pOrderBy==0 );. 
24e62 20 70 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e 70   pOrderBy = p->p
24e63 4f 72 64 65 72 42 79 3b 0a 20 20 61 73 73 65 72  OrderBy;.  asser
24e64 74 28 20 70 4f 72 64 65 72 42 79 20 29 3b 0a 20  t( pOrderBy );. 
24e65 20 6e 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64   nOrderBy = pOrd
24e66 65 72 42 79 2d 3e 6e 45 78 70 72 3b 0a 0a 20 20  erBy->nExpr;..  
24e67 2f 2a 20 46 6f 72 20 6f 70 65 72 61 74 6f 72 73  /* For operators
24e68 20 6f 74 68 65 72 20 74 68 61 6e 20 55 4e 49 4f   other than UNIO
24e69 4e 20 41 4c 4c 20 77 65 20 68 61 76 65 20 74 6f  N ALL we have to
24e6a 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a   make sure that.
24e6b 20 20 2a 2a 20 74 68 65 20 4f 52 44 45 52 20 42    ** the ORDER B
24e6c 59 20 63 6c 61 75 73 65 20 63 6f 76 65 72 73 20  Y clause covers 
24e6d 65 76 65 72 79 20 74 65 72 6d 20 6f 66 20 74 68  every term of th
24e6e 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 41  e result set.  A
24e6f 64 64 0a 20 20 2a 2a 20 74 65 72 6d 73 20 74 6f  dd.  ** terms to
24e70 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
24e71 61 75 73 65 20 61 73 20 6e 65 63 65 73 73 61 72  ause as necessar
24e72 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70  y..  */.  if( op
24e73 21 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20  !=TK_ALL ){.    
24e74 66 6f 72 28 69 3d 31 3b 20 64 62 2d 3e 6d 61 6c  for(i=1; db->mal
24e75 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 26 26 20  locFailed==0 && 
24e76 69 3c 3d 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45  i<=p->pEList->nE
24e77 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  xpr; i++){.     
24e78 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
24e79 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20  _item *pItem;.  
24e7a 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 74      for(j=0, pIt
24e7b 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20  em=pOrderBy->a; 
24e7c 6a 3c 6e 4f 72 64 65 72 42 79 3b 20 6a 2b 2b 2c  j<nOrderBy; j++,
24e7d 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
24e7e 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d     assert( pItem
24e7f 2d 3e 69 43 6f 6c 3e 30 20 29 3b 0a 20 20 20 20  ->iCol>0 );.    
24e80 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69      if( pItem->i
24e81 43 6f 6c 3d 3d 69 20 29 20 62 72 65 61 6b 3b 0a  Col==i ) break;.
24e82 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
24e83 28 20 6a 3d 3d 6e 4f 72 64 65 72 42 79 20 29 7b  ( j==nOrderBy ){
24e84 0a 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70  .        Expr *p
24e85 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 50 45 78  New = sqlite3PEx
24e86 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e  pr(pParse, TK_IN
24e87 54 45 47 45 52 2c 20 30 2c 20 30 2c 20 30 29 3b  TEGER, 0, 0, 0);
24e88 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4e 65  .        if( pNe
24e89 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51  w==0 ) return SQ
24e8a 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
24e8b 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20      pNew->flags 
24e8c 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 3b 0a  |= EP_IntValue;.
24e8d 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 54          pNew->iT
24e8e 61 62 6c 65 20 3d 20 69 3b 0a 20 20 20 20 20 20  able = i;.      
24e8f 20 20 70 4f 72 64 65 72 42 79 20 3d 20 73 71 6c    pOrderBy = sql
24e90 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
24e91 6e 64 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65  nd(pParse, pOrde
24e92 72 42 79 2c 20 70 4e 65 77 2c 20 30 29 3b 0a 20  rBy, pNew, 0);. 
24e93 20 20 20 20 20 20 20 70 4f 72 64 65 72 42 79 2d         pOrderBy-
24e94 3e 61 5b 6e 4f 72 64 65 72 42 79 2b 2b 5d 2e 69  >a[nOrderBy++].i
24e95 43 6f 6c 20 3d 20 69 3b 0a 20 20 20 20 20 20 7d  Col = i;.      }
24e96 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
24e97 20 43 6f 6d 70 75 74 65 20 74 68 65 20 63 6f 6d   Compute the com
24e98 70 61 72 69 73 6f 6e 20 70 65 72 6d 75 74 61 74  parison permutat
24e99 69 6f 6e 20 61 6e 64 20 6b 65 79 69 6e 66 6f 20  ion and keyinfo 
24e9a 74 68 61 74 20 69 73 20 75 73 65 64 20 77 69 74  that is used wit
24e9b 68 0a 20 20 2a 2a 20 74 68 65 20 70 65 72 6d 75  h.  ** the permu
24e9c 74 61 74 69 6f 6e 20 69 6e 20 6f 72 64 65 72 20  tation in order 
24e9d 74 6f 20 63 6f 6d 70 61 72 69 73 6f 6e 73 20 74  to comparisons t
24e9e 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74  o determine if t
24e9f 68 65 20 6e 65 78 74 0a 20 20 2a 2a 20 72 6f 77  he next.  ** row
24ea0 20 6f 66 20 72 65 73 75 6c 74 73 20 63 6f 6d 65   of results come
24ea1 73 20 66 72 6f 6d 20 73 65 6c 65 63 74 41 20 6f  s from selectA o
24ea2 72 20 73 65 6c 65 63 74 42 2e 20 20 41 6c 73 6f  r selectB.  Also
24ea3 20 61 64 64 20 65 78 70 6c 69 63 69 74 0a 20 20   add explicit.  
24ea4 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 73 20 74 6f  ** collations to
24ea5 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
24ea6 61 75 73 65 20 74 65 72 6d 73 20 73 6f 20 74 68  ause terms so th
24ea7 61 74 20 77 68 65 6e 20 74 68 65 20 73 75 62 71  at when the subq
24ea8 75 65 72 69 65 73 0a 20 20 2a 2a 20 74 6f 20 74  ueries.  ** to t
24ea9 68 65 20 72 69 67 68 74 20 61 6e 64 20 74 68 65  he right and the
24eaa 20 6c 65 66 74 20 61 72 65 20 65 76 61 6c 75 61   left are evalua
24eab 74 65 64 2c 20 74 68 65 79 20 75 73 65 20 74 68  ted, they use th
24eac 65 20 63 6f 72 72 65 63 74 0a 20 20 2a 2a 20 63  e correct.  ** c
24ead 6f 6c 6c 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20  ollation..  */. 
24eae 20 61 50 65 72 6d 75 74 65 20 3d 20 73 71 6c 69   aPermute = sqli
24eaf 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
24eb0 62 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e  b, sizeof(int)*n
24eb1 4f 72 64 65 72 42 79 29 3b 0a 20 20 69 66 28 20  OrderBy);.  if( 
24eb2 61 50 65 72 6d 75 74 65 20 29 7b 0a 20 20 20 20  aPermute ){.    
24eb3 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
24eb4 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20  item *pItem;.   
24eb5 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d   for(i=0, pItem=
24eb6 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c 6e  pOrderBy->a; i<n
24eb7 4f 72 64 65 72 42 79 3b 20 69 2b 2b 2c 20 70 49  OrderBy; i++, pI
24eb8 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73  tem++){.      as
24eb9 73 65 72 74 28 20 70 49 74 65 6d 2d 3e 69 43 6f  sert( pItem->iCo
24eba 6c 3e 30 20 20 26 26 20 70 49 74 65 6d 2d 3e 69  l>0  && pItem->i
24ebb 43 6f 6c 3c 3d 70 2d 3e 70 45 4c 69 73 74 2d 3e  Col<=p->pEList->
24ebc 6e 45 78 70 72 20 29 3b 0a 20 20 20 20 20 20 61  nExpr );.      a
24ebd 50 65 72 6d 75 74 65 5b 69 5d 20 3d 20 70 49 74  Permute[i] = pIt
24ebe 65 6d 2d 3e 69 43 6f 6c 20 2d 20 31 3b 0a 20 20  em->iCol - 1;.  
24ebf 20 20 7d 0a 20 20 20 20 70 4b 65 79 4d 65 72 67    }.    pKeyMerg
24ec0 65 20 3d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e =.      sqlite
24ec1 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
24ec2 20 73 69 7a 65 6f 66 28 2a 70 4b 65 79 4d 65 72   sizeof(*pKeyMer
24ec3 67 65 29 2b 6e 4f 72 64 65 72 42 79 2a 28 73 69  ge)+nOrderBy*(si
24ec4 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 2b 31  zeof(CollSeq*)+1
24ec5 29 29 3b 0a 20 20 20 20 69 66 28 20 70 4b 65 79  ));.    if( pKey
24ec6 4d 65 72 67 65 20 29 7b 0a 20 20 20 20 20 20 70  Merge ){.      p
24ec7 4b 65 79 4d 65 72 67 65 2d 3e 61 53 6f 72 74 4f  KeyMerge->aSortO
24ec8 72 64 65 72 20 3d 20 28 75 38 2a 29 26 70 4b 65  rder = (u8*)&pKe
24ec9 79 4d 65 72 67 65 2d 3e 61 43 6f 6c 6c 5b 6e 4f  yMerge->aColl[nO
24eca 72 64 65 72 42 79 5d 3b 0a 20 20 20 20 20 20 70  rderBy];.      p
24ecb 4b 65 79 4d 65 72 67 65 2d 3e 6e 46 69 65 6c 64  KeyMerge->nField
24ecc 20 3d 20 6e 4f 72 64 65 72 42 79 3b 0a 20 20 20   = nOrderBy;.   
24ecd 20 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e 65 6e     pKeyMerge->en
24ece 63 20 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 20  c = ENC(db);.   
24ecf 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f     for(i=0; i<nO
24ed0 72 64 65 72 42 79 3b 20 69 2b 2b 29 7b 0a 20 20  rderBy; i++){.  
24ed1 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70        CollSeq *p
24ed2 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 45 78  Coll;.        Ex
24ed3 70 72 20 2a 70 54 65 72 6d 20 3d 20 70 4f 72 64  pr *pTerm = pOrd
24ed4 65 72 42 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72  erBy->a[i].pExpr
24ed5 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54  ;.        if( pT
24ed6 65 72 6d 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  erm->flags & EP_
24ed7 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20  ExpCollate ){.  
24ed8 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20          pColl = 
24ed9 70 54 65 72 6d 2d 3e 70 43 6f 6c 6c 3b 0a 20 20  pTerm->pColl;.  
24eda 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
24edb 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 6d         pColl = m
24edc 75 6c 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65  ultiSelectCollSe
24edd 71 28 70 50 61 72 73 65 2c 20 70 2c 20 61 50 65  q(pParse, p, aPe
24ede 72 6d 75 74 65 5b 69 5d 29 3b 0a 20 20 20 20 20  rmute[i]);.     
24edf 20 20 20 20 20 70 54 65 72 6d 2d 3e 66 6c 61 67       pTerm->flag
24ee0 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61  s |= EP_ExpColla
24ee1 74 65 3b 0a 20 20 20 20 20 20 20 20 20 20 70 54  te;.          pT
24ee2 65 72 6d 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f  erm->pColl = pCo
24ee3 6c 6c 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ll;.        }.  
24ee4 20 20 20 20 20 20 70 4b 65 79 4d 65 72 67 65 2d        pKeyMerge-
24ee5 3e 61 43 6f 6c 6c 5b 69 5d 20 3d 20 70 43 6f 6c  >aColl[i] = pCol
24ee6 6c 3b 0a 20 20 20 20 20 20 20 20 70 4b 65 79 4d  l;.        pKeyM
24ee7 65 72 67 65 2d 3e 61 53 6f 72 74 4f 72 64 65 72  erge->aSortOrder
24ee8 5b 69 5d 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e  [i] = pOrderBy->
24ee9 61 5b 69 5d 2e 73 6f 72 74 4f 72 64 65 72 3b 0a  a[i].sortOrder;.
24eea 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
24eeb 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4b 65 79 4d  }else{.    pKeyM
24eec 65 72 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  erge = 0;.  }.. 
24eed 20 2f 2a 20 52 65 61 74 74 61 63 68 20 74 68 65   /* Reattach the
24eee 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
24eef 20 74 6f 20 74 68 65 20 71 75 65 72 79 2e 0a 20   to the query.. 
24ef0 20 2a 2f 0a 20 20 70 2d 3e 70 4f 72 64 65 72 42   */.  p->pOrderB
24ef1 79 20 3d 20 70 4f 72 64 65 72 42 79 3b 0a 20 20  y = pOrderBy;.  
24ef2 70 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79  pPrior->pOrderBy
24ef3 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
24ef4 73 74 44 75 70 28 70 50 61 72 73 65 2d 3e 64 62  stDup(pParse->db
24ef5 2c 20 70 4f 72 64 65 72 42 79 29 3b 0a 0a 20 20  , pOrderBy);..  
24ef6 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 72 61  /* Allocate a ra
24ef7 6e 67 65 20 6f 66 20 74 65 6d 70 6f 72 61 72 79  nge of temporary
24ef8 20 72 65 67 69 73 74 65 72 73 20 61 6e 64 20 74   registers and t
24ef9 68 65 20 4b 65 79 49 6e 66 6f 20 6e 65 65 64 65  he KeyInfo neede
24efa 64 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 6c  d.  ** for the l
24efb 6f 67 69 63 20 74 68 61 74 20 72 65 6d 6f 76 65  ogic that remove
24efc 73 20 64 75 70 6c 69 63 61 74 65 20 72 65 73 75  s duplicate resu
24efd 6c 74 20 72 6f 77 73 20 77 68 65 6e 20 74 68 65  lt rows when the
24efe 0a 20 20 2a 2a 20 6f 70 65 72 61 74 6f 72 20 69  .  ** operator i
24eff 73 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50 54 2c  s UNION, EXCEPT,
24f00 20 6f 72 20 49 4e 54 45 52 53 45 43 54 20 28 62   or INTERSECT (b
24f01 75 74 20 6e 6f 74 20 55 4e 49 4f 4e 20 41 4c 4c  ut not UNION ALL
24f02 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70  )..  */.  if( op
24f03 3d 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20  ==TK_ALL ){.    
24f04 72 65 67 50 72 65 76 20 3d 20 30 3b 0a 20 20 7d  regPrev = 0;.  }
24f05 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6e 45  else{.    int nE
24f06 78 70 72 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d  xpr = p->pEList-
24f07 3e 6e 45 78 70 72 3b 0a 20 20 20 20 61 73 73 65  >nExpr;.    asse
24f08 72 74 28 20 6e 4f 72 64 65 72 42 79 3e 3d 6e 45  rt( nOrderBy>=nE
24f09 78 70 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  xpr || db->mallo
24f0a 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 72  cFailed );.    r
24f0b 65 67 50 72 65 76 20 3d 20 73 71 6c 69 74 65 33  egPrev = sqlite3
24f0c 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61  GetTempRange(pPa
24f0d 72 73 65 2c 20 6e 45 78 70 72 2b 31 29 3b 0a 20  rse, nExpr+1);. 
24f0e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
24f0f 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
24f10 65 72 2c 20 30 2c 20 72 65 67 50 72 65 76 29 3b  er, 0, regPrev);
24f11 0a 20 20 20 20 70 4b 65 79 44 75 70 20 3d 20 73  .    pKeyDup = s
24f12 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
24f13 72 6f 28 64 62 2c 0a 20 20 20 20 20 20 20 20 20  ro(db,.         
24f14 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28           sizeof(
24f15 2a 70 4b 65 79 44 75 70 29 20 2b 20 6e 45 78 70  *pKeyDup) + nExp
24f16 72 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65  r*(sizeof(CollSe
24f17 71 2a 29 2b 31 29 20 29 3b 0a 20 20 20 20 69 66  q*)+1) );.    if
24f18 28 20 70 4b 65 79 44 75 70 20 29 7b 0a 20 20 20  ( pKeyDup ){.   
24f19 20 20 20 70 4b 65 79 44 75 70 2d 3e 61 53 6f 72     pKeyDup->aSor
24f1a 74 4f 72 64 65 72 20 3d 20 28 75 38 2a 29 26 70  tOrder = (u8*)&p
24f1b 4b 65 79 44 75 70 2d 3e 61 43 6f 6c 6c 5b 6e 45  KeyDup->aColl[nE
24f1c 78 70 72 5d 3b 0a 20 20 20 20 20 20 70 4b 65 79  xpr];.      pKey
24f1d 44 75 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 6e 45  Dup->nField = nE
24f1e 78 70 72 3b 0a 20 20 20 20 20 20 70 4b 65 79 44  xpr;.      pKeyD
24f1f 75 70 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 64 62  up->enc = ENC(db
24f20 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  );.      for(i=0
24f21 3b 20 69 3c 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  ; i<nExpr; i++){
24f22 0a 20 20 20 20 20 20 20 20 70 4b 65 79 44 75 70  .        pKeyDup
24f23 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3d 20 6d 75 6c  ->aColl[i] = mul
24f24 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28  tiSelectCollSeq(
24f25 70 50 61 72 73 65 2c 20 70 2c 20 69 29 3b 0a 20  pParse, p, i);. 
24f26 20 20 20 20 20 20 20 70 4b 65 79 44 75 70 2d 3e         pKeyDup->
24f27 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20  aSortOrder[i] = 
24f28 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  0;.      }.    }
24f29 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 53 65 70 61  .  }. .  /* Sepa
24f2a 72 61 74 65 20 74 68 65 20 6c 65 66 74 20 61 6e  rate the left an
24f2b 64 20 74 68 65 20 72 69 67 68 74 20 71 75 65 72  d the right quer
24f2c 79 20 66 72 6f 6d 20 6f 6e 65 20 61 6e 6f 74 68  y from one anoth
24f2d 65 72 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 50 72  er.  */.  p->pPr
24f2e 69 6f 72 20 3d 20 30 3b 0a 20 20 70 50 72 69 6f  ior = 0;.  pPrio
24f2f 72 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20  r->pRightmost = 
24f30 30 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 73 6f  0;.  sqlite3Reso
24f31 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28  lveOrderGroupBy(
24f32 70 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 4f  pParse, p, p->pO
24f33 72 64 65 72 42 79 2c 20 22 4f 52 44 45 52 22 29  rderBy, "ORDER")
24f34 3b 0a 20 20 69 66 28 20 70 50 72 69 6f 72 2d 3e  ;.  if( pPrior->
24f35 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20  pPrior==0 ){.   
24f36 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f   sqlite3ResolveO
24f37 72 64 65 72 47 72 6f 75 70 42 79 28 70 50 61 72  rderGroupBy(pPar
24f38 73 65 2c 20 70 50 72 69 6f 72 2c 20 70 50 72 69  se, pPrior, pPri
24f39 6f 72 2d 3e 70 4f 72 64 65 72 42 79 2c 20 22 4f  or->pOrderBy, "O
24f3a 52 44 45 52 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f  RDER");.  }..  /
24f3b 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 6c 69  * Compute the li
24f3c 6d 69 74 20 72 65 67 69 73 74 65 72 73 20 2a 2f  mit registers */
24f3d 0a 20 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52  .  computeLimitR
24f3e 65 67 69 73 74 65 72 73 28 70 50 61 72 73 65 2c  egisters(pParse,
24f3f 20 70 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20   p, labelEnd);. 
24f40 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20 26   if( p->iLimit &
24f41 26 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29 7b 0a  & op==TK_ALL ){.
24f42 20 20 20 20 72 65 67 4c 69 6d 69 74 41 20 3d 20      regLimitA = 
24f43 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
24f44 20 20 20 20 72 65 67 4c 69 6d 69 74 42 20 3d 20      regLimitB = 
24f45 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
24f46 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
24f47 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79  ddOp2(v, OP_Copy
24f48 2c 20 70 2d 3e 69 4f 66 66 73 65 74 20 3f 20 70  , p->iOffset ? p
24f49 2d 3e 69 4f 66 66 73 65 74 2b 31 20 3a 20 70 2d  ->iOffset+1 : p-
24f4a 3e 69 4c 69 6d 69 74 2c 0a 20 20 20 20 20 20 20  >iLimit,.       
24f4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f4c 20 20 20 20 20 20 20 20 20 20 20 72 65 67 4c 69             regLi
24f4d 6d 69 74 41 29 3b 0a 20 20 20 20 73 71 6c 69 74  mitA);.    sqlit
24f4e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
24f4f 4f 50 5f 43 6f 70 79 2c 20 72 65 67 4c 69 6d 69  OP_Copy, regLimi
24f50 74 41 2c 20 72 65 67 4c 69 6d 69 74 42 29 3b 0a  tA, regLimitB);.
24f51 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 67    }else{.    reg
24f52 4c 69 6d 69 74 41 20 3d 20 72 65 67 4c 69 6d 69  LimitA = regLimi
24f53 74 42 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71  tB = 0;.  }.  sq
24f54 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
24f55 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a  db, p->pLimit);.
24f56 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b    p->pLimit = 0;
24f57 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
24f58 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4f 66 66  lete(db, p->pOff
24f59 73 65 74 29 3b 0a 20 20 70 2d 3e 70 4f 66 66 73  set);.  p->pOffs
24f5a 65 74 20 3d 20 30 3b 0a 0a 20 20 72 65 67 41 64  et = 0;..  regAd
24f5b 64 72 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  drA = ++pParse->
24f5c 6e 4d 65 6d 3b 0a 20 20 72 65 67 45 6f 66 41 20  nMem;.  regEofA 
24f5d 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
24f5e 3b 0a 20 20 72 65 67 41 64 64 72 42 20 3d 20 2b  ;.  regAddrB = +
24f5f 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
24f60 20 72 65 67 45 6f 66 42 20 3d 20 2b 2b 70 50 61   regEofB = ++pPa
24f61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67  rse->nMem;.  reg
24f62 4f 75 74 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d  OutA = ++pParse-
24f63 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 4f 75 74 42  >nMem;.  regOutB
24f64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
24f65 6d 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65  m;.  sqlite3Sele
24f66 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74  ctDestInit(&dest
24f67 41 2c 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65  A, SRT_Coroutine
24f68 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 73  , regAddrA);.  s
24f69 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74  qlite3SelectDest
24f6a 49 6e 69 74 28 26 64 65 73 74 42 2c 20 53 52 54  Init(&destB, SRT
24f6b 5f 43 6f 72 6f 75 74 69 6e 65 2c 20 72 65 67 41  _Coroutine, regA
24f6c 64 64 72 42 29 3b 0a 0a 20 20 2f 2a 20 4a 75 6d  ddrB);..  /* Jum
24f6d 70 20 70 61 73 74 20 74 68 65 20 76 61 72 69 6f  p past the vario
24f6e 75 73 20 73 75 62 72 6f 75 74 69 6e 65 73 20 61  us subroutines a
24f6f 6e 64 20 63 6f 72 6f 75 74 69 6e 65 73 20 74 6f  nd coroutines to
24f70 20 74 68 65 20 6d 61 69 6e 0a 20 20 2a 2a 20 6d   the main.  ** m
24f71 65 72 67 65 20 6c 6f 6f 70 0a 20 20 2a 2f 0a 20  erge loop.  */. 
24f72 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j1 = sqlite3Vdb
24f73 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f  eAddOp0(v, OP_Go
24f74 74 6f 29 3b 0a 20 20 61 64 64 72 53 65 6c 65 63  to);.  addrSelec
24f75 74 41 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  tA = sqlite3Vdbe
24f76 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
24f77 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
24f78 61 20 63 6f 72 6f 75 74 69 6e 65 20 74 6f 20 65  a coroutine to e
24f79 76 61 6c 75 61 74 65 20 74 68 65 20 53 45 4c 45  valuate the SELE
24f7a 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20  CT statement to 
24f7b 74 68 65 0a 20 20 2a 2a 20 6c 65 66 74 20 6f 66  the.  ** left of
24f7c 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 6f 70   the compound op
24f7d 65 72 61 74 6f 72 20 2d 20 74 68 65 20 22 41 22  erator - the "A"
24f7e 20 73 65 6c 65 63 74 2e 0a 20 20 2a 2f 0a 20 20   select..  */.  
24f7f 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28  VdbeNoopComment(
24f80 28 76 2c 20 22 42 65 67 69 6e 20 63 6f 72 6f 75  (v, "Begin corou
24f81 74 69 6e 65 20 66 6f 72 20 6c 65 66 74 20 53 45  tine for left SE
24f82 4c 45 43 54 22 29 29 3b 0a 20 20 70 50 72 69 6f  LECT"));.  pPrio
24f83 72 2d 3e 69 4c 69 6d 69 74 20 3d 20 72 65 67 4c  r->iLimit = regL
24f84 69 6d 69 74 41 3b 0a 20 20 73 71 6c 69 74 65 33  imitA;.  sqlite3
24f85 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
24f86 50 72 69 6f 72 2c 20 26 64 65 73 74 41 29 3b 0a  Prior, &destA);.
24f87 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
24f88 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
24f89 72 2c 20 31 2c 20 72 65 67 45 6f 66 41 29 3b 0a  r, 1, regEofA);.
24f8a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
24f8b 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c  Op1(v, OP_Yield,
24f8c 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 56 64   regAddrA);.  Vd
24f8d 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76  beNoopComment((v
24f8e 2c 20 22 45 6e 64 20 63 6f 72 6f 75 74 69 6e 65  , "End coroutine
24f8f 20 66 6f 72 20 6c 65 66 74 20 53 45 4c 45 43 54   for left SELECT
24f90 22 29 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  "));..  /* Gener
24f91 61 74 65 20 61 20 63 6f 72 6f 75 74 69 6e 65 20  ate a coroutine 
24f92 74 6f 20 65 76 61 6c 75 61 74 65 20 74 68 65 20  to evaluate the 
24f93 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
24f94 20 6f 6e 20 0a 20 20 2a 2a 20 74 68 65 20 72 69   on .  ** the ri
24f95 67 68 74 20 2d 20 74 68 65 20 22 42 22 20 73 65  ght - the "B" se
24f96 6c 65 63 74 0a 20 20 2a 2f 0a 20 20 61 64 64 72  lect.  */.  addr
24f97 53 65 6c 65 63 74 42 20 3d 20 73 71 6c 69 74 65  SelectB = sqlite
24f98 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
24f99 28 76 29 3b 0a 20 20 56 64 62 65 4e 6f 6f 70 43  (v);.  VdbeNoopC
24f9a 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 42 65 67 69  omment((v, "Begi
24f9b 6e 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20  n coroutine for 
24f9c 72 69 67 68 74 20 53 45 4c 45 43 54 22 29 29 3b  right SELECT"));
24f9d 0a 20 20 73 61 76 65 64 4c 69 6d 69 74 20 3d 20  .  savedLimit = 
24f9e 70 2d 3e 69 4c 69 6d 69 74 3b 0a 20 20 73 61 76  p->iLimit;.  sav
24f9f 65 64 4f 66 66 73 65 74 20 3d 20 70 2d 3e 69 4f  edOffset = p->iO
24fa0 66 66 73 65 74 3b 0a 20 20 70 2d 3e 69 4c 69 6d  ffset;.  p->iLim
24fa1 69 74 20 3d 20 72 65 67 4c 69 6d 69 74 42 3b 0a  it = regLimitB;.
24fa2 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30    p->iOffset = 0
24fa3 3b 20 20 0a 20 20 73 71 6c 69 74 65 33 53 65 6c  ;  .  sqlite3Sel
24fa4 65 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20 26  ect(pParse, p, &
24fa5 64 65 73 74 42 29 3b 0a 20 20 70 2d 3e 69 4c 69  destB);.  p->iLi
24fa6 6d 69 74 20 3d 20 73 61 76 65 64 4c 69 6d 69 74  mit = savedLimit
24fa7 3b 0a 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d  ;.  p->iOffset =
24fa8 20 73 61 76 65 64 4f 66 66 73 65 74 3b 0a 20 20   savedOffset;.  
24fa9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24faa 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
24fab 20 31 2c 20 72 65 67 45 6f 66 42 29 3b 0a 20 20   1, regEofB);.  
24fac 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24fad 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72  1(v, OP_Yield, r
24fae 65 67 41 64 64 72 42 29 3b 0a 20 20 56 64 62 65  egAddrB);.  Vdbe
24faf 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  NoopComment((v, 
24fb0 22 45 6e 64 20 63 6f 72 6f 75 74 69 6e 65 20 66  "End coroutine f
24fb1 6f 72 20 72 69 67 68 74 20 53 45 4c 45 43 54 22  or right SELECT"
24fb2 29 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61  ));..  /* Genera
24fb3 74 65 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20  te a subroutine 
24fb4 74 68 61 74 20 6f 75 74 70 75 74 73 20 74 68 65  that outputs the
24fb5 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20   current row of 
24fb6 74 68 65 20 41 0a 20 20 2a 2a 20 73 65 6c 65 63  the A.  ** selec
24fb7 74 20 61 73 20 74 68 65 20 6e 65 78 74 20 6f 75  t as the next ou
24fb8 74 70 75 74 20 72 6f 77 20 6f 66 20 74 68 65 20  tput row of the 
24fb9 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2e  compound select.
24fba 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f 70  .  */.  VdbeNoop
24fbb 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4f 75 74  Comment((v, "Out
24fbc 70 75 74 20 72 6f 75 74 69 6e 65 20 66 6f 72 20  put routine for 
24fbd 41 22 29 29 3b 0a 20 20 61 64 64 72 4f 75 74 41  A"));.  addrOutA
24fbe 20 3d 20 67 65 6e 65 72 61 74 65 4f 75 74 70 75   = generateOutpu
24fbf 74 53 75 62 72 6f 75 74 69 6e 65 28 70 50 61 72  tSubroutine(pPar
24fc0 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  se,.            
24fc1 20 20 20 20 20 70 2c 20 26 64 65 73 74 41 2c 20       p, &destA, 
24fc2 70 44 65 73 74 2c 20 72 65 67 4f 75 74 41 2c 0a  pDest, regOutA,.
24fc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24fc4 20 72 65 67 50 72 65 76 2c 20 70 4b 65 79 44 75   regPrev, pKeyDu
24fc5 70 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41  p, P4_KEYINFO_HA
24fc6 4e 44 4f 46 46 2c 20 6c 61 62 65 6c 45 6e 64 29  NDOFF, labelEnd)
24fc7 3b 0a 20 20 0a 20 20 2f 2a 20 47 65 6e 65 72 61  ;.  .  /* Genera
24fc8 74 65 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20  te a subroutine 
24fc9 74 68 61 74 20 6f 75 74 70 75 74 73 20 74 68 65  that outputs the
24fca 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20   current row of 
24fcb 74 68 65 20 42 0a 20 20 2a 2a 20 73 65 6c 65 63  the B.  ** selec
24fcc 74 20 61 73 20 74 68 65 20 6e 65 78 74 20 6f 75  t as the next ou
24fcd 74 70 75 74 20 72 6f 77 20 6f 66 20 74 68 65 20  tput row of the 
24fce 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2e  compound select.
24fcf 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70 3d 3d  .  */.  if( op==
24fd0 54 4b 5f 41 4c 4c 20 7c 7c 20 6f 70 3d 3d 54 4b  TK_ALL || op==TK
24fd1 5f 55 4e 49 4f 4e 20 29 7b 0a 20 20 20 20 56 64  _UNION ){.    Vd
24fd2 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76  beNoopComment((v
24fd3 2c 20 22 4f 75 74 70 75 74 20 72 6f 75 74 69 6e  , "Output routin
24fd4 65 20 66 6f 72 20 42 22 29 29 3b 0a 20 20 20 20  e for B"));.    
24fd5 61 64 64 72 4f 75 74 42 20 3d 20 67 65 6e 65 72  addrOutB = gener
24fd6 61 74 65 4f 75 74 70 75 74 53 75 62 72 6f 75 74  ateOutputSubrout
24fd7 69 6e 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20  ine(pParse,.    
24fd8 20 20 20 20 20 20 20 20 20 20 20 20 20 70 2c 20               p, 
24fd9 26 64 65 73 74 42 2c 20 70 44 65 73 74 2c 20 72  &destB, pDest, r
24fda 65 67 4f 75 74 42 2c 0a 20 20 20 20 20 20 20 20  egOutB,.        
24fdb 20 20 20 20 20 20 20 20 20 72 65 67 50 72 65 76           regPrev
24fdc 2c 20 70 4b 65 79 44 75 70 2c 20 50 34 5f 4b 45  , pKeyDup, P4_KE
24fdd 59 49 4e 46 4f 5f 53 54 41 54 49 43 2c 20 6c 61  YINFO_STATIC, la
24fde 62 65 6c 45 6e 64 29 3b 0a 20 20 7d 0a 0a 20 20  belEnd);.  }..  
24fdf 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73 75  /* Generate a su
24fe0 62 72 6f 75 74 69 6e 65 20 74 6f 20 72 75 6e 20  broutine to run 
24fe1 77 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73  when the results
24fe2 20 66 72 6f 6d 20 73 65 6c 65 63 74 20 41 0a 20   from select A. 
24fe3 20 2a 2a 20 61 72 65 20 65 78 68 61 75 73 74 65   ** are exhauste
24fe4 64 20 61 6e 64 20 6f 6e 6c 79 20 64 61 74 61 20  d and only data 
24fe5 69 6e 20 73 65 6c 65 63 74 20 42 20 72 65 6d 61  in select B rema
24fe6 69 6e 73 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65  ins..  */.  Vdbe
24fe7 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  NoopComment((v, 
24fe8 22 65 6f 66 2d 41 20 73 75 62 72 6f 75 74 69 6e  "eof-A subroutin
24fe9 65 22 29 29 3b 0a 20 20 69 66 28 20 6f 70 3d 3d  e"));.  if( op==
24fea 54 4b 5f 45 58 43 45 50 54 20 7c 7c 20 6f 70 3d  TK_EXCEPT || op=
24feb 3d 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 29 7b  =TK_INTERSECT ){
24fec 0a 20 20 20 20 61 64 64 72 45 6f 66 41 20 3d 20  .    addrEofA = 
24fed 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24fee 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
24fef 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20 7d 65   labelEnd);.  }e
24ff0 6c 73 65 7b 20 20 0a 20 20 20 20 61 64 64 72 45  lse{  .    addrE
24ff1 6f 66 41 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ofA = sqlite3Vdb
24ff2 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66  eAddOp2(v, OP_If
24ff3 2c 20 72 65 67 45 6f 66 42 2c 20 6c 61 62 65 6c  , regEofB, label
24ff4 45 6e 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  End);.    sqlite
24ff5 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
24ff6 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74 42  P_Gosub, regOutB
24ff7 2c 20 61 64 64 72 4f 75 74 42 29 3b 0a 20 20 20  , addrOutB);.   
24ff8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24ff9 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20  p1(v, OP_Yield, 
24ffa 72 65 67 41 64 64 72 42 29 3b 0a 20 20 20 20 73  regAddrB);.    s
24ffb 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
24ffc 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
24ffd 61 64 64 72 45 6f 66 41 29 3b 0a 20 20 7d 0a 0a  addrEofA);.  }..
24ffe 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20    /* Generate a 
24fff 73 75 62 72 6f 75 74 69 6e 65 20 74 6f 20 72 75  subroutine to ru
25000 6e 20 77 68 65 6e 20 74 68 65 20 72 65 73 75 6c  n when the resul
25001 74 73 20 66 72 6f 6d 20 73 65 6c 65 63 74 20 42  ts from select B
25002 0a 20 20 2a 2a 20 61 72 65 20 65 78 68 61 75 73  .  ** are exhaus
25003 74 65 64 20 61 6e 64 20 6f 6e 6c 79 20 64 61 74  ted and only dat
25004 61 20 69 6e 20 73 65 6c 65 63 74 20 41 20 72 65  a in select A re
25005 6d 61 69 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  mains..  */.  if
25006 28 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53 45  ( op==TK_INTERSE
25007 43 54 20 29 7b 0a 20 20 20 20 61 64 64 72 45 6f  CT ){.    addrEo
25008 66 42 20 3d 20 61 64 64 72 45 6f 66 41 3b 0a 20  fB = addrEofA;. 
25009 20 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20 56 64   }else{  .    Vd
2500a 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76  beNoopComment((v
2500b 2c 20 22 65 6f 66 2d 42 20 73 75 62 72 6f 75 74  , "eof-B subrout
2500c 69 6e 65 22 29 29 3b 0a 20 20 20 20 61 64 64 72  ine"));.    addr
2500d 45 6f 66 42 20 3d 20 73 71 6c 69 74 65 33 56 64  EofB = sqlite3Vd
2500e 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2500f 66 2c 20 72 65 67 45 6f 66 41 2c 20 6c 61 62 65  f, regEofA, labe
25010 6c 45 6e 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  lEnd);.    sqlit
25011 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
25012 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74  OP_Gosub, regOut
25013 41 2c 20 61 64 64 72 4f 75 74 41 29 3b 0a 20 20  A, addrOutA);.  
25014 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
25015 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c  Op1(v, OP_Yield,
25016 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 20 20   regAddrA);.    
25017 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25018 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
25019 20 61 64 64 72 45 6f 66 42 29 3b 0a 20 20 7d 0a   addrEofB);.  }.
2501a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63  .  /* Generate c
2501b 6f 64 65 20 74 6f 20 68 61 6e 64 6c 65 20 74 68  ode to handle th
2501c 65 20 63 61 73 65 20 6f 66 20 41 3c 42 0a 20 20  e case of A<B.  
2501d 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d  */.  VdbeNoopCom
2501e 6d 65 6e 74 28 28 76 2c 20 22 41 2d 6c 74 2d 42  ment((v, "A-lt-B
2501f 20 73 75 62 72 6f 75 74 69 6e 65 22 29 29 3b 0a   subroutine"));.
25020 20 20 61 64 64 72 41 6c 74 42 20 3d 20 73 71 6c    addrAltB = sql
25021 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
25022 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f  , OP_Gosub, regO
25023 75 74 41 2c 20 61 64 64 72 4f 75 74 41 29 3b 0a  utA, addrOutA);.
25024 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
25025 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c  Op1(v, OP_Yield,
25026 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 73 71   regAddrA);.  sq
25027 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
25028 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66  v, OP_If, regEof
25029 41 2c 20 61 64 64 72 45 6f 66 41 29 3b 0a 20 20  A, addrEofA);.  
2502a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2502b 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
2502c 20 6c 61 62 65 6c 43 6d 70 72 29 3b 0a 0a 20 20   labelCmpr);..  
2502d 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
2502e 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 20 63   to handle the c
2502f 61 73 65 20 6f 66 20 41 3d 3d 42 0a 20 20 2a 2f  ase of A==B.  */
25030 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 41 4c  .  if( op==TK_AL
25031 4c 20 29 7b 0a 20 20 20 20 61 64 64 72 41 65 71  L ){.    addrAeq
25032 42 20 3d 20 61 64 64 72 41 6c 74 42 3b 0a 20 20  B = addrAltB;.  
25033 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b  }else if( op==TK
25034 5f 49 4e 54 45 52 53 45 43 54 20 29 7b 0a 20 20  _INTERSECT ){.  
25035 20 20 61 64 64 72 41 65 71 42 20 3d 20 61 64 64    addrAeqB = add
25036 72 41 6c 74 42 3b 0a 20 20 20 20 61 64 64 72 41  rAltB;.    addrA
25037 6c 74 42 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ltB++;.  }else{.
25038 20 20 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d      VdbeNoopComm
25039 65 6e 74 28 28 76 2c 20 22 41 2d 65 71 2d 42 20  ent((v, "A-eq-B 
2503a 73 75 62 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20  subroutine"));. 
2503b 20 20 20 61 64 64 72 41 65 71 42 20 3d 0a 20 20     addrAeqB =.  
2503c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2503d 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c  Op1(v, OP_Yield,
2503e 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 20 20   regAddrA);.    
2503f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25040 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45  2(v, OP_If, regE
25041 6f 66 41 2c 20 61 64 64 72 45 6f 66 41 29 3b 0a  ofA, addrEofA);.
25042 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
25043 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
25044 2c 20 30 2c 20 6c 61 62 65 6c 43 6d 70 72 29 3b  , 0, labelCmpr);
25045 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  .  }..  /* Gener
25046 61 74 65 20 63 6f 64 65 20 74 6f 20 68 61 6e 64  ate code to hand
25047 6c 65 20 74 68 65 20 63 61 73 65 20 6f 66 20 41  le the case of A
25048 3e 42 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e 6f  >B.  */.  VdbeNo
25049 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 41  opComment((v, "A
2504a 2d 67 74 2d 42 20 73 75 62 72 6f 75 74 69 6e 65  -gt-B subroutine
2504b 22 29 29 3b 0a 20 20 61 64 64 72 41 67 74 42 20  "));.  addrAgtB 
2504c 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
2504d 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 69  rentAddr(v);.  i
2504e 66 28 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 7c 7c  f( op==TK_ALL ||
2504f 20 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e 20 29 7b   op==TK_UNION ){
25050 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
25051 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73  AddOp2(v, OP_Gos
25052 75 62 2c 20 72 65 67 4f 75 74 42 2c 20 61 64 64  ub, regOutB, add
25053 72 4f 75 74 42 29 3b 0a 20 20 7d 0a 20 20 73 71  rOutB);.  }.  sq
25054 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
25055 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65 67  v, OP_Yield, reg
25056 41 64 64 72 42 29 3b 0a 20 20 73 71 6c 69 74 65  AddrB);.  sqlite
25057 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
25058 50 5f 49 66 2c 20 72 65 67 45 6f 66 42 2c 20 61  P_If, regEofB, a
25059 64 64 72 45 6f 66 42 29 3b 0a 20 20 73 71 6c 69  ddrEofB);.  sqli
2505a 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2505b 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 6c 61 62   OP_Goto, 0, lab
2505c 65 6c 43 6d 70 72 29 3b 0a 0a 20 20 2f 2a 20 54  elCmpr);..  /* T
2505d 68 69 73 20 63 6f 64 65 20 72 75 6e 73 20 6f 6e  his code runs on
2505e 63 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65  ce to initialize
2505f 20 65 76 65 72 79 74 68 69 6e 67 2e 0a 20 20 2a   everything..  *
25060 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a  /.  sqlite3VdbeJ
25061 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a  umpHere(v, j1);.
25062 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
25063 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
25064 72 2c 20 30 2c 20 72 65 67 45 6f 66 41 29 3b 0a  r, 0, regEofA);.
25065 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
25066 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
25067 72 2c 20 30 2c 20 72 65 67 45 6f 66 42 29 3b 0a  r, 0, regEofB);.
25068 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
25069 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c  Op2(v, OP_Gosub,
2506a 20 72 65 67 41 64 64 72 41 2c 20 61 64 64 72 53   regAddrA, addrS
2506b 65 6c 65 63 74 41 29 3b 0a 20 20 73 71 6c 69 74  electA);.  sqlit
2506c 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2506d 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 41 64 64  OP_Gosub, regAdd
2506e 72 42 2c 20 61 64 64 72 53 65 6c 65 63 74 42 29  rB, addrSelectB)
2506f 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
25070 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20  ddOp2(v, OP_If, 
25071 72 65 67 45 6f 66 41 2c 20 61 64 64 72 45 6f 66  regEofA, addrEof
25072 41 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  A);.  sqlite3Vdb
25073 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66  eAddOp2(v, OP_If
25074 2c 20 72 65 67 45 6f 66 42 2c 20 61 64 64 72 45  , regEofB, addrE
25075 6f 66 42 29 3b 0a 0a 20 20 2f 2a 20 49 6d 70 6c  ofB);..  /* Impl
25076 65 6d 65 6e 74 20 74 68 65 20 6d 61 69 6e 20 6d  ement the main m
25077 65 72 67 65 20 6c 6f 6f 70 0a 20 20 2a 2f 0a 20  erge loop.  */. 
25078 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
25079 6c 76 65 4c 61 62 65 6c 28 76 2c 20 6c 61 62 65  lveLabel(v, labe
2507a 6c 43 6d 70 72 29 3b 0a 20 20 73 71 6c 69 74 65  lCmpr);.  sqlite
2507b 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
2507c 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 2c 20 30  P_Permutation, 0
2507d 2c 20 30 2c 20 30 2c 20 28 63 68 61 72 2a 29 61  , 0, 0, (char*)a
2507e 50 65 72 6d 75 74 65 2c 20 50 34 5f 49 4e 54 41  Permute, P4_INTA
2507f 52 52 41 59 29 3b 0a 20 20 73 71 6c 69 74 65 33  RRAY);.  sqlite3
25080 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
25081 5f 43 6f 6d 70 61 72 65 2c 20 64 65 73 74 41 2e  _Compare, destA.
25082 69 4d 65 6d 2c 20 64 65 73 74 42 2e 69 4d 65 6d  iMem, destB.iMem
25083 2c 20 6e 4f 72 64 65 72 42 79 2c 0a 20 20 20 20  , nOrderBy,.    
25084 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25085 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79       (char*)pKey
25086 4d 65 72 67 65 2c 20 50 34 5f 4b 45 59 49 4e 46  Merge, P4_KEYINF
25087 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 73 71  O_HANDOFF);.  sq
25088 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
25089 76 2c 20 4f 50 5f 4a 75 6d 70 2c 20 61 64 64 72  v, OP_Jump, addr
2508a 41 6c 74 42 2c 20 61 64 64 72 41 65 71 42 2c 20  AltB, addrAeqB, 
2508b 61 64 64 72 41 67 74 42 29 3b 0a 0a 20 20 2f 2a  addrAgtB);..  /*
2508c 20 52 65 6c 65 61 73 65 20 74 65 6d 70 6f 72 61   Release tempora
2508d 72 79 20 72 65 67 69 73 74 65 72 73 0a 20 20 2a  ry registers.  *
2508e 2f 0a 20 20 69 66 28 20 72 65 67 50 72 65 76 20  /.  if( regPrev 
2508f 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65  ){.    sqlite3Re
25090 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70  leaseTempRange(p
25091 50 61 72 73 65 2c 20 72 65 67 50 72 65 76 2c 20  Parse, regPrev, 
25092 6e 4f 72 64 65 72 42 79 2b 31 29 3b 0a 20 20 7d  nOrderBy+1);.  }
25093 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 74  ..  /* Jump to t
25094 68 65 20 74 68 69 73 20 70 6f 69 6e 74 20 69 6e  he this point in
25095 20 6f 72 64 65 72 20 74 6f 20 74 65 72 6d 69 6e   order to termin
25096 61 74 65 20 74 68 65 20 71 75 65 72 79 2e 0a 20  ate the query.. 
25097 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
25098 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
25099 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 0a 20 20 2f   labelEnd);..  /
2509a 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72  * Set the number
2509b 20 6f 66 20 6f 75 74 70 75 74 20 63 6f 6c 75 6d   of output colum
2509c 6e 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44  ns.  */.  if( pD
2509d 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f  est->eDest==SRT_
2509e 4f 75 74 70 75 74 20 29 7b 0a 20 20 20 20 53 65  Output ){.    Se
2509f 6c 65 63 74 20 2a 70 46 69 72 73 74 20 3d 20 70  lect *pFirst = p
250a0 50 72 69 6f 72 3b 0a 20 20 20 20 77 68 69 6c 65  Prior;.    while
250a1 28 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72  ( pFirst->pPrior
250a2 20 29 20 70 46 69 72 73 74 20 3d 20 70 46 69 72   ) pFirst = pFir
250a3 73 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20  st->pPrior;.    
250a4 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61  generateColumnNa
250a5 6d 65 73 28 70 50 61 72 73 65 2c 20 30 2c 20 70  mes(pParse, 0, p
250a6 46 69 72 73 74 2d 3e 70 45 4c 69 73 74 29 3b 0a  First->pEList);.
250a7 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 73 73 65    }..  /* Reasse
250a8 6d 62 6c 79 20 74 68 65 20 63 6f 6d 70 6f 75 6e  mbly the compoun
250a9 64 20 71 75 65 72 79 20 73 6f 20 74 68 61 74 20  d query so that 
250aa 69 74 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64  it will be freed
250ab 20 63 6f 72 72 65 63 74 6c 79 0a 20 20 2a 2a 20   correctly.  ** 
250ac 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66  by the calling f
250ad 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28  unction */.  if(
250ae 20 70 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20   p->pPrior ){.  
250af 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
250b0 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 50 72  elete(db, p->pPr
250b1 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70  ior);.  }.  p->p
250b2 50 72 69 6f 72 20 3d 20 70 50 72 69 6f 72 3b 0a  Prior = pPrior;.
250b3 0a 20 20 2f 2a 2a 2a 20 54 42 44 3a 20 20 49 6e  .  /*** TBD:  In
250b4 73 65 72 74 20 73 75 62 72 6f 75 74 69 6e 65 20  sert subroutine 
250b5 63 61 6c 6c 73 20 74 6f 20 63 6c 6f 73 65 20 63  calls to close c
250b6 75 72 73 6f 72 73 20 6f 6e 20 69 6e 63 6f 6d 70  ursors on incomp
250b7 6c 65 74 65 0a 20 20 2a 2a 2a 2a 20 73 75 62 71  lete.  **** subq
250b8 75 65 72 69 65 73 20 2a 2a 2a 2a 2f 0a 20 20 72  ueries ****/.  r
250b9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
250ba 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21  .}.#endif..#if !
250bb 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
250bc 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c  MIT_SUBQUERY) ||
250bd 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
250be 5f 4f 4d 49 54 5f 56 49 45 57 29 0a 2f 2a 20 46  _OMIT_VIEW)./* F
250bf 6f 72 77 61 72 64 20 44 65 63 6c 61 72 61 74 69  orward Declarati
250c0 6f 6e 73 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ons */.static vo
250c1 69 64 20 73 75 62 73 74 45 78 70 72 4c 69 73 74  id substExprList
250c2 28 73 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 4c  (sqlite3*, ExprL
250c3 69 73 74 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c  ist*, int, ExprL
250c4 69 73 74 2a 29 3b 0a 73 74 61 74 69 63 20 76 6f  ist*);.static vo
250c5 69 64 20 73 75 62 73 74 53 65 6c 65 63 74 28 73  id substSelect(s
250c6 71 6c 69 74 65 33 2a 2c 20 53 65 6c 65 63 74 20  qlite3*, Select 
250c7 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73 74  *, int, ExprList
250c8 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 63 61 6e   *);../*.** Scan
250c9 20 74 68 72 6f 75 67 68 20 74 68 65 20 65 78 70   through the exp
250ca 72 65 73 73 69 6f 6e 20 70 45 78 70 72 2e 20 20  ression pExpr.  
250cb 52 65 70 6c 61 63 65 20 65 76 65 72 79 20 72 65  Replace every re
250cc 66 65 72 65 6e 63 65 20 74 6f 0a 2a 2a 20 61 20  ference to.** a 
250cd 63 6f 6c 75 6d 6e 20 69 6e 20 74 61 62 6c 65 20  column in table 
250ce 6e 75 6d 62 65 72 20 69 54 61 62 6c 65 20 77 69  number iTable wi
250cf 74 68 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  th a copy of the
250d0 20 69 43 6f 6c 75 6d 6e 2d 74 68 0a 2a 2a 20 65   iColumn-th.** e
250d1 6e 74 72 79 20 69 6e 20 70 45 4c 69 73 74 2e 20  ntry in pEList. 
250d2 20 28 42 75 74 20 6c 65 61 76 65 20 72 65 66 65   (But leave refe
250d3 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 52 4f  rences to the RO
250d4 57 49 44 20 63 6f 6c 75 6d 6e 20 0a 2a 2a 20 75  WID column .** u
250d5 6e 63 68 61 6e 67 65 64 2e 29 0a 2a 2a 0a 2a 2a  nchanged.).**.**
250d6 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
250d7 20 70 61 72 74 20 6f 66 20 74 68 65 20 66 6c 61   part of the fla
250d8 74 74 65 6e 69 6e 67 20 70 72 6f 63 65 64 75 72  ttening procedur
250d9 65 2e 20 20 41 20 73 75 62 71 75 65 72 79 0a 2a  e.  A subquery.*
250da 2a 20 77 68 6f 73 65 20 72 65 73 75 6c 74 20 73  * whose result s
250db 65 74 20 69 73 20 64 65 66 69 6e 65 64 20 62 79  et is defined by
250dc 20 70 45 4c 69 73 74 20 61 70 70 65 61 72 73 20   pEList appears 
250dd 61 73 20 65 6e 74 72 79 20 69 6e 20 74 68 65 0a  as entry in the.
250de 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  ** FROM clause o
250df 66 20 61 20 53 45 4c 45 43 54 20 73 75 63 68 20  f a SELECT such 
250e0 74 68 61 74 20 74 68 65 20 56 44 42 45 20 63 75  that the VDBE cu
250e1 72 73 6f 72 20 61 73 73 69 67 6e 65 64 20 74 6f  rsor assigned to
250e2 20 74 68 61 74 0a 2a 2a 20 46 4f 52 4d 20 63 6c   that.** FORM cl
250e3 61 75 73 65 20 65 6e 74 72 79 20 69 73 20 69 54  ause entry is iT
250e4 61 62 6c 65 2e 20 20 54 68 69 73 20 72 6f 75 74  able.  This rout
250e5 69 6e 65 20 6d 61 6b 65 20 74 68 65 20 6e 65 63  ine make the nec
250e6 65 73 73 61 72 79 20 0a 2a 2a 20 63 68 61 6e 67  essary .** chang
250e7 65 73 20 74 6f 20 70 45 78 70 72 20 73 6f 20 74  es to pExpr so t
250e8 68 61 74 20 69 74 20 72 65 66 65 72 73 20 64 69  hat it refers di
250e9 72 65 63 74 6c 79 20 74 6f 20 74 68 65 20 73 6f  rectly to the so
250ea 75 72 63 65 20 74 61 62 6c 65 0a 2a 2a 20 6f 66  urce table.** of
250eb 20 74 68 65 20 73 75 62 71 75 65 72 79 20 72 61   the subquery ra
250ec 74 68 65 72 20 74 68 65 20 72 65 73 75 6c 74 20  ther the result 
250ed 73 65 74 20 6f 66 20 74 68 65 20 73 75 62 71 75  set of the subqu
250ee 65 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ery..*/.static v
250ef 6f 69 64 20 73 75 62 73 74 45 78 70 72 28 0a 20  oid substExpr(. 
250f0 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
250f1 20 20 20 20 20 2f 2a 20 52 65 70 6f 72 74 20 6d       /* Report m
250f2 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20 74 6f 20  alloc errors to 
250f3 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  this connection 
250f4 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  */.  Expr *pExpr
250f5 2c 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72  ,        /* Expr
250f6 20 69 6e 20 77 68 69 63 68 20 73 75 62 73 74 69   in which substi
250f7 74 75 74 69 6f 6e 20 6f 63 63 75 72 73 20 2a 2f  tution occurs */
250f8 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20  .  int iTable,  
250f9 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20         /* Table 
250fa 74 6f 20 62 65 20 73 75 62 73 74 69 74 75 74 65  to be substitute
250fb 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  d */.  ExprList 
250fc 2a 70 45 4c 69 73 74 20 20 20 20 2f 2a 20 53 75  *pEList    /* Su
250fd 62 73 74 69 74 75 74 65 20 65 78 70 72 65 73 73  bstitute express
250fe 69 6f 6e 73 20 2a 2f 0a 29 7b 0a 20 20 69 66 28  ions */.){.  if(
250ff 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75   pExpr==0 ) retu
25100 72 6e 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d  rn;.  if( pExpr-
25101 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26  >op==TK_COLUMN &
25102 26 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d  & pExpr->iTable=
25103 3d 69 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 69  =iTable ){.    i
25104 66 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d  f( pExpr->iColum
25105 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 70 45 78  n<0 ){.      pEx
25106 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c  pr->op = TK_NULL
25107 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
25108 20 20 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20     Expr *pNew;. 
25109 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 4c       assert( pEL
2510a 69 73 74 21 3d 30 20 26 26 20 70 45 78 70 72 2d  ist!=0 && pExpr-
2510b 3e 69 43 6f 6c 75 6d 6e 3c 70 45 4c 69 73 74 2d  >iColumn<pEList-
2510c 3e 6e 45 78 70 72 20 29 3b 0a 20 20 20 20 20 20  >nExpr );.      
2510d 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70  assert( pExpr->p
2510e 4c 65 66 74 3d 3d 30 20 26 26 20 70 45 78 70 72  Left==0 && pExpr
2510f 2d 3e 70 52 69 67 68 74 3d 3d 30 20 26 26 20 70  ->pRight==0 && p
25110 45 78 70 72 2d 3e 70 4c 69 73 74 3d 3d 30 20 29  Expr->pList==0 )
25111 3b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 70  ;.      pNew = p
25112 45 4c 69 73 74 2d 3e 61 5b 70 45 78 70 72 2d 3e  EList->a[pExpr->
25113 69 43 6f 6c 75 6d 6e 5d 2e 70 45 78 70 72 3b 0a  iColumn].pExpr;.
25114 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4e        assert( pN
25115 65 77 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  ew!=0 );.      p
25116 45 78 70 72 2d 3e 6f 70 20 3d 20 70 4e 65 77 2d  Expr->op = pNew-
25117 3e 6f 70 3b 0a 20 20 20 20 20 20 61 73 73 65 72  >op;.      asser
25118 74 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3d  t( pExpr->pLeft=
25119 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 45 78 70  =0 );.      pExp
2511a 72 2d 3e 70 4c 65 66 74 20 3d 20 73 71 6c 69 74  r->pLeft = sqlit
2511b 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4e  e3ExprDup(db, pN
2511c 65 77 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20  ew->pLeft);.    
2511d 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
2511e 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a 20 20  >pRight==0 );.  
2511f 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68      pExpr->pRigh
25120 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  t = sqlite3ExprD
25121 75 70 28 64 62 2c 20 70 4e 65 77 2d 3e 70 52 69  up(db, pNew->pRi
25122 67 68 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65  ght);.      asse
25123 72 74 28 20 70 45 78 70 72 2d 3e 70 4c 69 73 74  rt( pExpr->pList
25124 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 45 78  ==0 );.      pEx
25125 70 72 2d 3e 70 4c 69 73 74 20 3d 20 73 71 6c 69  pr->pList = sqli
25126 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64  te3ExprListDup(d
25127 62 2c 20 70 4e 65 77 2d 3e 70 4c 69 73 74 29 3b  b, pNew->pList);
25128 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54  .      pExpr->iT
25129 61 62 6c 65 20 3d 20 70 4e 65 77 2d 3e 69 54 61  able = pNew->iTa
2512a 62 6c 65 3b 0a 20 20 20 20 20 20 70 45 78 70 72  ble;.      pExpr
2512b 2d 3e 70 54 61 62 20 3d 20 70 4e 65 77 2d 3e 70  ->pTab = pNew->p
2512c 54 61 62 3b 0a 20 20 20 20 20 20 70 45 78 70 72  Tab;.      pExpr
2512d 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 70 4e 65 77  ->iColumn = pNew
2512e 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20  ->iColumn;.     
2512f 20 70 45 78 70 72 2d 3e 69 41 67 67 20 3d 20 70   pExpr->iAgg = p
25130 4e 65 77 2d 3e 69 41 67 67 3b 0a 20 20 20 20 20  New->iAgg;.     
25131 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70   sqlite3TokenCop
25132 79 28 64 62 2c 20 26 70 45 78 70 72 2d 3e 74 6f  y(db, &pExpr->to
25133 6b 65 6e 2c 20 26 70 4e 65 77 2d 3e 74 6f 6b 65  ken, &pNew->toke
25134 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  n);.      sqlite
25135 33 54 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26  3TokenCopy(db, &
25136 70 45 78 70 72 2d 3e 73 70 61 6e 2c 20 26 70 4e  pExpr->span, &pN
25137 65 77 2d 3e 73 70 61 6e 29 3b 0a 20 20 20 20 20  ew->span);.     
25138 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 20   pExpr->pSelect 
25139 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  = sqlite3SelectD
2513a 75 70 28 64 62 2c 20 70 4e 65 77 2d 3e 70 53 65  up(db, pNew->pSe
2513b 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 70 45 78  lect);.      pEx
2513c 70 72 2d 3e 66 6c 61 67 73 20 3d 20 70 4e 65 77  pr->flags = pNew
2513d 2d 3e 66 6c 61 67 73 3b 0a 20 20 20 20 7d 0a 20  ->flags;.    }. 
2513e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 75 62 73   }else{.    subs
2513f 74 45 78 70 72 28 64 62 2c 20 70 45 78 70 72 2d  tExpr(db, pExpr-
25140 3e 70 4c 65 66 74 2c 20 69 54 61 62 6c 65 2c 20  >pLeft, iTable, 
25141 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 73 75 62  pEList);.    sub
25142 73 74 45 78 70 72 28 64 62 2c 20 70 45 78 70 72  stExpr(db, pExpr
25143 2d 3e 70 52 69 67 68 74 2c 20 69 54 61 62 6c 65  ->pRight, iTable
25144 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 73  , pEList);.    s
25145 75 62 73 74 53 65 6c 65 63 74 28 64 62 2c 20 70  ubstSelect(db, p
25146 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 2c 20 69  Expr->pSelect, i
25147 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a  Table, pEList);.
25148 20 20 20 20 73 75 62 73 74 45 78 70 72 4c 69 73      substExprLis
25149 74 28 64 62 2c 20 70 45 78 70 72 2d 3e 70 4c 69  t(db, pExpr->pLi
2514a 73 74 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69  st, iTable, pELi
2514b 73 74 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69  st);.  }.}.stati
2514c 63 20 76 6f 69 64 20 73 75 62 73 74 45 78 70 72  c void substExpr
2514d 4c 69 73 74 28 0a 20 20 73 71 6c 69 74 65 33 20  List(.  sqlite3 
2514e 2a 64 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  *db,         /* 
2514f 52 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20 65 72  Report malloc er
25150 72 6f 72 73 20 68 65 72 65 20 2a 2f 0a 20 20 45  rors here */.  E
25151 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20  xprList *pList, 
25152 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 73      /* List to s
25153 63 61 6e 20 61 6e 64 20 69 6e 20 77 68 69 63 68  can and in which
25154 20 74 6f 20 6d 61 6b 65 20 73 75 62 73 74 69 74   to make substit
25155 75 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 54  utes */.  int iT
25156 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 2f  able,          /
25157 2a 20 54 61 62 6c 65 20 74 6f 20 62 65 20 73 75  * Table to be su
25158 62 73 74 69 74 75 74 65 64 20 2a 2f 0a 20 20 45  bstituted */.  E
25159 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20  xprList *pEList 
2515a 20 20 20 20 2f 2a 20 53 75 62 73 74 69 74 75 74      /* Substitut
2515b 65 20 76 61 6c 75 65 73 20 2a 2f 0a 29 7b 0a 20  e values */.){. 
2515c 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 4c   int i;.  if( pL
2515d 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ist==0 ) return;
2515e 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c  .  for(i=0; i<pL
2515f 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  ist->nExpr; i++)
25160 7b 0a 20 20 20 20 73 75 62 73 74 45 78 70 72 28  {.    substExpr(
25161 64 62 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  db, pList->a[i].
25162 70 45 78 70 72 2c 20 69 54 61 62 6c 65 2c 20 70  pExpr, iTable, p
25163 45 4c 69 73 74 29 3b 0a 20 20 7d 0a 7d 0a 73 74  EList);.  }.}.st
25164 61 74 69 63 20 76 6f 69 64 20 73 75 62 73 74 53  atic void substS
25165 65 6c 65 63 74 28 0a 20 20 73 71 6c 69 74 65 33  elect(.  sqlite3
25166 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 2f 2a   *db,         /*
25167 20 52 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20 65   Report malloc e
25168 72 72 6f 72 73 20 68 65 72 65 20 2a 2f 0a 20 20  rrors here */.  
25169 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20  Select *p,      
2516a 20 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 73       /* SELECT s
2516b 74 61 74 65 6d 65 6e 74 20 69 6e 20 77 68 69 63  tatement in whic
2516c 68 20 74 6f 20 6d 61 6b 65 20 73 75 62 73 74 69  h to make substi
2516d 74 75 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74  tutions */.  int
2516e 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20   iTable,        
2516f 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 62 65    /* Table to be
25170 20 72 65 70 6c 61 63 65 64 20 2a 2f 0a 20 20 45   replaced */.  E
25171 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20  xprList *pEList 
25172 20 20 20 20 2f 2a 20 53 75 62 73 74 69 74 75 74      /* Substitut
25173 65 20 76 61 6c 75 65 73 20 2a 2f 0a 29 7b 0a 20  e values */.){. 
25174 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a   SrcList *pSrc;.
25175 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
25176 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20  _item *pItem;.  
25177 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21 70 20  int i;.  if( !p 
25178 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 75 62 73  ) return;.  subs
25179 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70 2d  tExprList(db, p-
2517a 3e 70 45 4c 69 73 74 2c 20 69 54 61 62 6c 65 2c  >pEList, iTable,
2517b 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 75 62 73   pEList);.  subs
2517c 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70 2d  tExprList(db, p-
2517d 3e 70 47 72 6f 75 70 42 79 2c 20 69 54 61 62 6c  >pGroupBy, iTabl
2517e 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 75  e, pEList);.  su
2517f 62 73 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20  bstExprList(db, 
25180 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 69 54 61  p->pOrderBy, iTa
25181 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  ble, pEList);.  
25182 73 75 62 73 74 45 78 70 72 28 64 62 2c 20 70 2d  substExpr(db, p-
25183 3e 70 48 61 76 69 6e 67 2c 20 69 54 61 62 6c 65  >pHaving, iTable
25184 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 75 62  , pEList);.  sub
25185 73 74 45 78 70 72 28 64 62 2c 20 70 2d 3e 70 57  stExpr(db, p->pW
25186 68 65 72 65 2c 20 69 54 61 62 6c 65 2c 20 70 45  here, iTable, pE
25187 4c 69 73 74 29 3b 0a 20 20 73 75 62 73 74 53 65  List);.  substSe
25188 6c 65 63 74 28 64 62 2c 20 70 2d 3e 70 50 72 69  lect(db, p->pPri
25189 6f 72 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69  or, iTable, pELi
2518a 73 74 29 3b 0a 20 20 70 53 72 63 20 3d 20 70 2d  st);.  pSrc = p-
2518b 3e 70 53 72 63 3b 0a 20 20 69 66 28 20 70 53 72  >pSrc;.  if( pSr
2518c 63 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 70  c ){.    for(i=p
2518d 53 72 63 2d 3e 6e 53 72 63 2c 20 70 49 74 65 6d  Src->nSrc, pItem
2518e 3d 70 53 72 63 2d 3e 61 3b 20 69 3e 30 3b 20 69  =pSrc->a; i>0; i
2518f 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  --, pItem++){.  
25190 20 20 20 20 73 75 62 73 74 53 65 6c 65 63 74 28      substSelect(
25191 64 62 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65  db, pItem->pSele
25192 63 74 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69  ct, iTable, pELi
25193 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  st);.    }.  }.}
25194 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
25195 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
25196 53 55 42 51 55 45 52 59 29 20 7c 7c 20 21 64 65  SUBQUERY) || !de
25197 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
25198 54 5f 56 49 45 57 29 20 2a 2f 0a 0a 23 69 66 20  T_VIEW) */..#if 
25199 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2519a 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20 7c  OMIT_SUBQUERY) |
2519b 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  | !defined(SQLIT
2519c 45 5f 4f 4d 49 54 5f 56 49 45 57 29 0a 2f 2a 0a  E_OMIT_VIEW)./*.
2519d 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2519e 61 74 74 65 6d 70 74 73 20 74 6f 20 66 6c 61 74  attempts to flat
2519f 74 65 6e 20 73 75 62 71 75 65 72 69 65 73 20 69  ten subqueries i
251a0 6e 20 6f 72 64 65 72 20 74 6f 20 73 70 65 65 64  n order to speed
251a1 0a 2a 2a 20 65 78 65 63 75 74 69 6f 6e 2e 20 20  .** execution.  
251a2 49 74 20 72 65 74 75 72 6e 73 20 31 20 69 66 20  It returns 1 if 
251a3 69 74 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73  it makes changes
251a4 20 61 6e 64 20 30 20 69 66 20 6e 6f 20 66 6c 61   and 0 if no fla
251a5 74 74 65 6e 69 6e 67 0a 2a 2a 20 6f 63 63 75 72  ttening.** occur
251a6 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 75 6e 64 65  s..**.** To unde
251a7 72 73 74 61 6e 64 20 74 68 65 20 63 6f 6e 63 65  rstand the conce
251a8 70 74 20 6f 66 20 66 6c 61 74 74 65 6e 69 6e 67  pt of flattening
251a9 2c 20 63 6f 6e 73 69 64 65 72 20 74 68 65 20 66  , consider the f
251aa 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 71 75 65 72  ollowing.** quer
251ab 79 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c  y:.**.**     SEL
251ac 45 43 54 20 61 20 46 52 4f 4d 20 28 53 45 4c 45  ECT a FROM (SELE
251ad 43 54 20 78 2b 79 20 41 53 20 61 20 46 52 4f 4d  CT x+y AS a FROM
251ae 20 74 31 20 57 48 45 52 45 20 7a 3c 31 30 30 29   t1 WHERE z<100)
251af 20 57 48 45 52 45 20 61 3e 35 0a 2a 2a 0a 2a 2a   WHERE a>5.**.**
251b0 20 54 68 65 20 64 65 66 61 75 6c 74 20 77 61 79   The default way
251b1 20 6f 66 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67   of implementing
251b2 20 74 68 69 73 20 71 75 65 72 79 20 69 73 20 74   this query is t
251b3 6f 20 65 78 65 63 75 74 65 20 74 68 65 0a 2a 2a  o execute the.**
251b4 20 73 75 62 71 75 65 72 79 20 66 69 72 73 74 20   subquery first 
251b5 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65  and store the re
251b6 73 75 6c 74 73 20 69 6e 20 61 20 74 65 6d 70 6f  sults in a tempo
251b7 72 61 72 79 20 74 61 62 6c 65 2c 20 74 68 65 6e  rary table, then
251b8 0a 2a 2a 20 72 75 6e 20 74 68 65 20 6f 75 74 65  .** run the oute
251b9 72 20 71 75 65 72 79 20 6f 6e 20 74 68 61 74 20  r query on that 
251ba 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2e  temporary table.
251bb 20 20 54 68 69 73 20 72 65 71 75 69 72 65 73 20    This requires 
251bc 74 77 6f 0a 2a 2a 20 70 61 73 73 65 73 20 6f 76  two.** passes ov
251bd 65 72 20 74 68 65 20 64 61 74 61 2e 20 20 46 75  er the data.  Fu
251be 72 74 68 65 72 6d 6f 72 65 2c 20 62 65 63 61 75  rthermore, becau
251bf 73 65 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  se the temporary
251c0 20 74 61 62 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f   table.** has no
251c1 20 69 6e 64 69 63 65 73 2c 20 74 68 65 20 57 48   indices, the WH
251c2 45 52 45 20 63 6c 61 75 73 65 20 6f 6e 20 74 68  ERE clause on th
251c3 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 63 61  e outer query ca
251c4 6e 6e 6f 74 20 62 65 0a 2a 2a 20 6f 70 74 69 6d  nnot be.** optim
251c5 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ized..**.** This
251c6 20 72 6f 75 74 69 6e 65 20 61 74 74 65 6d 70 74   routine attempt
251c7 73 20 74 6f 20 72 65 77 72 69 74 65 20 71 75 65  s to rewrite que
251c8 72 69 65 73 20 73 75 63 68 20 61 73 20 74 68 65  ries such as the
251c9 20 61 62 6f 76 65 20 69 6e 74 6f 0a 2a 2a 20 61   above into.** a
251ca 20 73 69 6e 67 6c 65 20 66 6c 61 74 20 73 65 6c   single flat sel
251cb 65 63 74 2c 20 6c 69 6b 65 20 74 68 69 73 3a 0a  ect, like this:.
251cc 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54  **.**     SELECT
251cd 20 78 2b 79 20 41 53 20 61 20 46 52 4f 4d 20 74   x+y AS a FROM t
251ce 31 20 57 48 45 52 45 20 7a 3c 31 30 30 20 41 4e  1 WHERE z<100 AN
251cf 44 20 61 3e 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  D a>5.**.** The 
251d0 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 66  code generated f
251d1 6f 72 20 74 68 69 73 20 73 69 6d 70 69 66 69 63  or this simpific
251d2 61 74 69 6f 6e 20 67 69 76 65 73 20 74 68 65 20  ation gives the 
251d3 73 61 6d 65 20 72 65 73 75 6c 74 0a 2a 2a 20 62  same result.** b
251d4 75 74 20 6f 6e 6c 79 20 68 61 73 20 74 6f 20 73  ut only has to s
251d5 63 61 6e 20 74 68 65 20 64 61 74 61 20 6f 6e 63  can the data onc
251d6 65 2e 20 20 41 6e 64 20 62 65 63 61 75 73 65 20  e.  And because 
251d7 69 6e 64 69 63 65 73 20 6d 69 67 68 74 20 0a 2a  indices might .*
251d8 2a 20 65 78 69 73 74 20 6f 6e 20 74 68 65 20 74  * exist on the t
251d9 61 62 6c 65 20 74 31 2c 20 61 20 63 6f 6d 70 6c  able t1, a compl
251da 65 74 65 20 73 63 61 6e 20 6f 66 20 74 68 65 20  ete scan of the 
251db 64 61 74 61 20 6d 69 67 68 74 20 62 65 0a 2a 2a  data might be.**
251dc 20 61 76 6f 69 64 65 64 2e 0a 2a 2a 0a 2a 2a 20   avoided..**.** 
251dd 46 6c 61 74 74 65 6e 69 6e 67 20 69 73 20 6f 6e  Flattening is on
251de 6c 79 20 61 74 74 65 6d 70 74 65 64 20 69 66 20  ly attempted if 
251df 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  all of the follo
251e0 77 69 6e 67 20 61 72 65 20 74 72 75 65 3a 0a 2a  wing are true:.*
251e1 2a 0a 2a 2a 20 20 20 28 31 29 20 20 54 68 65 20  *.**   (1)  The 
251e2 73 75 62 71 75 65 72 79 20 61 6e 64 20 74 68 65  subquery and the
251e3 20 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f 20   outer query do 
251e4 6e 6f 74 20 62 6f 74 68 20 75 73 65 20 61 67 67  not both use agg
251e5 72 65 67 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20  regates..**.**  
251e6 20 28 32 29 20 20 54 68 65 20 73 75 62 71 75 65   (2)  The subque
251e7 72 79 20 69 73 20 6e 6f 74 20 61 6e 20 61 67 67  ry is not an agg
251e8 72 65 67 61 74 65 20 6f 72 20 74 68 65 20 6f 75  regate or the ou
251e9 74 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f 74  ter query is not
251ea 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20 20   a join..**.**  
251eb 20 28 33 29 20 20 54 68 65 20 73 75 62 71 75 65   (3)  The subque
251ec 72 79 20 69 73 20 6e 6f 74 20 74 68 65 20 72 69  ry is not the ri
251ed 67 68 74 20 6f 70 65 72 61 6e 64 20 6f 66 20 61  ght operand of a
251ee 20 6c 65 66 74 20 6f 75 74 65 72 20 6a 6f 69 6e   left outer join
251ef 0a 2a 2a 20 20 20 20 20 20 20 20 28 4f 72 69 67  .**        (Orig
251f0 69 6e 61 6c 6c 79 20 74 69 63 6b 65 74 20 23 33  inally ticket #3
251f1 30 36 2e 20 20 53 74 72 65 6e 67 68 74 65 6e 65  06.  Strenghtene
251f2 64 20 62 79 20 74 69 63 6b 65 74 20 23 33 33 30  d by ticket #330
251f3 30 29 0a 2a 2a 0a 2a 2a 20 20 20 28 34 29 20 20  0).**.**   (4)  
251f4 54 68 65 20 73 75 62 71 75 65 72 79 20 69 73 20  The subquery is 
251f5 6e 6f 74 20 44 49 53 54 49 4e 43 54 20 6f 72 20  not DISTINCT or 
251f6 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
251f7 69 73 20 6e 6f 74 20 61 20 6a 6f 69 6e 2e 0a 2a  is not a join..*
251f8 2a 0a 2a 2a 20 20 20 28 35 29 20 20 54 68 65 20  *.**   (5)  The 
251f9 73 75 62 71 75 65 72 79 20 69 73 20 6e 6f 74 20  subquery is not 
251fa 44 49 53 54 49 4e 43 54 20 6f 72 20 74 68 65 20  DISTINCT or the 
251fb 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f 65 73  outer query does
251fc 20 6e 6f 74 20 75 73 65 0a 2a 2a 20 20 20 20 20   not use.**     
251fd 20 20 20 61 67 67 72 65 67 61 74 65 73 2e 0a 2a     aggregates..*
251fe 2a 0a 2a 2a 20 20 20 28 36 29 20 20 54 68 65 20  *.**   (6)  The 
251ff 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f  subquery does no
25200 74 20 75 73 65 20 61 67 67 72 65 67 61 74 65 73  t use aggregates
25201 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75   or the outer qu
25202 65 72 79 20 69 73 20 6e 6f 74 0a 2a 2a 20 20 20  ery is not.**   
25203 20 20 20 20 20 44 49 53 54 49 4e 43 54 2e 0a 2a       DISTINCT..*
25204 2a 0a 2a 2a 20 20 20 28 37 29 20 20 54 68 65 20  *.**   (7)  The 
25205 73 75 62 71 75 65 72 79 20 68 61 73 20 61 20 46  subquery has a F
25206 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a  ROM clause..**.*
25207 2a 20 20 20 28 38 29 20 20 54 68 65 20 73 75 62  *   (8)  The sub
25208 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75  query does not u
25209 73 65 20 4c 49 4d 49 54 20 6f 72 20 74 68 65 20  se LIMIT or the 
2520a 6f 75 74 65 72 20 71 75 65 72 79 20 69 73 20 6e  outer query is n
2520b 6f 74 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a  ot a join..**.**
2520c 20 20 20 28 39 29 20 20 54 68 65 20 73 75 62 71     (9)  The subq
2520d 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73  uery does not us
2520e 65 20 4c 49 4d 49 54 20 6f 72 20 74 68 65 20 6f  e LIMIT or the o
2520f 75 74 65 72 20 71 75 65 72 79 20 64 6f 65 73 20  uter query does 
25210 6e 6f 74 20 75 73 65 0a 2a 2a 20 20 20 20 20 20  not use.**      
25211 20 20 61 67 67 72 65 67 61 74 65 73 2e 0a 2a 2a    aggregates..**
25212 0a 2a 2a 20 20 28 31 30 29 20 20 54 68 65 20 73  .**  (10)  The s
25213 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74  ubquery does not
25214 20 75 73 65 20 61 67 67 72 65 67 61 74 65 73 20   use aggregates 
25215 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  or the outer que
25216 72 79 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 20  ry does not.**  
25217 20 20 20 20 20 20 75 73 65 20 4c 49 4d 49 54 2e        use LIMIT.
25218 0a 2a 2a 0a 2a 2a 20 20 28 31 31 29 20 20 54 68  .**.**  (11)  Th
25219 65 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 74  e subquery and t
2521a 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 64  he outer query d
2521b 6f 20 6e 6f 74 20 62 6f 74 68 20 68 61 76 65 20  o not both have 
2521c 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 73  ORDER BY clauses
2521d 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 32 29 20 20 4e  ..**.**  (12)  N
2521e 6f 74 20 69 6d 70 6c 65 6d 65 6e 74 65 64 2e 20  ot implemented. 
2521f 20 53 75 62 73 75 6d 65 64 20 69 6e 74 6f 20 72   Subsumed into r
25220 65 73 74 72 69 63 74 69 6f 6e 20 28 33 29 2e 20  estriction (3). 
25221 20 57 61 73 20 70 72 65 76 69 6f 75 73 6c 79 0a   Was previously.
25222 2a 2a 20 20 20 20 20 20 20 20 61 20 73 65 70 61  **        a sepa
25223 72 61 74 65 20 72 65 73 74 72 69 63 74 69 6f 6e  rate restriction
25224 20 64 65 72 69 76 69 6e 67 20 66 72 6f 6d 20 74   deriving from t
25225 69 63 6b 65 74 20 23 33 35 30 2e 0a 2a 2a 0a 2a  icket #350..**.*
25226 2a 20 20 28 31 33 29 20 20 54 68 65 20 73 75 62  *  (13)  The sub
25227 71 75 65 72 79 20 61 6e 64 20 6f 75 74 65 72 20  query and outer 
25228 71 75 65 72 79 20 64 6f 20 6e 6f 74 20 62 6f 74  query do not bot
25229 68 20 75 73 65 20 4c 49 4d 49 54 0a 2a 2a 0a 2a  h use LIMIT.**.*
2522a 2a 20 20 28 31 34 29 20 20 54 68 65 20 73 75 62  *  (14)  The sub
2522b 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75  query does not u
2522c 73 65 20 4f 46 46 53 45 54 0a 2a 2a 0a 2a 2a 20  se OFFSET.**.** 
2522d 20 28 31 35 29 20 20 54 68 65 20 6f 75 74 65 72   (15)  The outer
2522e 20 71 75 65 72 79 20 69 73 20 6e 6f 74 20 70 61   query is not pa
2522f 72 74 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64  rt of a compound
25230 20 73 65 6c 65 63 74 20 6f 72 20 74 68 65 0a 2a   select or the.*
25231 2a 20 20 20 20 20 20 20 20 73 75 62 71 75 65 72  *        subquer
25232 79 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20  y does not have 
25233 62 6f 74 68 20 61 6e 20 4f 52 44 45 52 20 42 59  both an ORDER BY
25234 20 61 6e 64 20 61 20 4c 49 4d 49 54 20 63 6c 61   and a LIMIT cla
25235 75 73 65 2e 0a 2a 2a 20 20 20 20 20 20 20 20 28  use..**        (
25236 53 65 65 20 74 69 63 6b 65 74 20 23 32 33 33 39  See ticket #2339
25237 29 0a 2a 2a 0a 2a 2a 20 20 28 31 36 29 20 20 54  ).**.**  (16)  T
25238 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 69  he outer query i
25239 73 20 6e 6f 74 20 61 6e 20 61 67 67 72 65 67 61  s not an aggrega
2523a 74 65 20 6f 72 20 74 68 65 20 73 75 62 71 75 65  te or the subque
2523b 72 79 20 64 6f 65 73 0a 2a 2a 20 20 20 20 20 20  ry does.**      
2523c 20 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 4f 52    not contain OR
2523d 44 45 52 20 42 59 2e 20 20 28 54 69 63 6b 65 74  DER BY.  (Ticket
2523e 20 23 32 39 34 32 29 20 20 54 68 69 73 20 75 73   #2942)  This us
2523f 65 64 20 74 6f 20 6e 6f 74 20 6d 61 74 74 65 72  ed to not matter
25240 0a 2a 2a 20 20 20 20 20 20 20 20 75 6e 74 69 6c  .**        until
25241 20 77 65 20 69 6e 74 72 6f 64 75 63 65 64 20 74   we introduced t
25242 68 65 20 67 72 6f 75 70 5f 63 6f 6e 63 61 74 28  he group_concat(
25243 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 0a 2a 2a  ) function.  .**
25244 0a 2a 2a 20 20 28 31 37 29 20 20 54 68 65 20 73  .**  (17)  The s
25245 75 62 2d 71 75 65 72 79 20 69 73 20 6e 6f 74 20  ub-query is not 
25246 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63  a compound selec
25247 74 2c 20 6f 72 20 69 74 20 69 73 20 61 20 55 4e  t, or it is a UN
25248 49 4f 4e 20 41 4c 4c 20 0a 2a 2a 20 20 20 20 20  ION ALL .**     
25249 20 20 20 63 6f 6d 70 6f 75 6e 64 20 63 6c 61 75     compound clau
2524a 73 65 20 6d 61 64 65 20 75 70 20 65 6e 74 69 72  se made up entir
2524b 65 6c 79 20 6f 66 20 6e 6f 6e 2d 61 67 67 72 65  ely of non-aggre
2524c 67 61 74 65 20 71 75 65 72 69 65 73 2c 20 61 6e  gate queries, an
2524d 64 20 0a 2a 2a 20 20 20 20 20 20 20 20 74 68 65  d .**        the
2524e 20 70 61 72 65 6e 74 20 71 75 65 72 79 3a 0a 2a   parent query:.*
2524f 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 2a 20  *.**          * 
25250 69 73 20 6e 6f 74 20 69 74 73 65 6c 66 20 70 61  is not itself pa
25251 72 74 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64  rt of a compound
25252 20 73 65 6c 65 63 74 2c 0a 2a 2a 20 20 20 20 20   select,.**     
25253 20 20 20 20 20 2a 20 69 73 20 6e 6f 74 20 61 6e       * is not an
25254 20 61 67 67 72 65 67 61 74 65 20 6f 72 20 44 49   aggregate or DI
25255 53 54 49 4e 43 54 20 71 75 65 72 79 2c 20 61 6e  STINCT query, an
25256 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 2a 20  d.**          * 
25257 68 61 73 20 6e 6f 20 6f 74 68 65 72 20 74 61 62  has no other tab
25258 6c 65 73 20 6f 72 20 73 75 62 2d 73 65 6c 65 63  les or sub-selec
25259 74 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  ts in the FROM c
2525a 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  lause..**.**    
2525b 20 20 20 20 54 68 65 20 70 61 72 65 6e 74 20 61      The parent a
2525c 6e 64 20 73 75 62 2d 71 75 65 72 79 20 6d 61 79  nd sub-query may
2525d 20 63 6f 6e 74 61 69 6e 20 57 48 45 52 45 20 63   contain WHERE c
2525e 6c 61 75 73 65 73 2e 20 53 75 62 6a 65 63 74 20  lauses. Subject 
2525f 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 72 75 6c  to.**        rul
25260 65 73 20 28 31 31 29 2c 20 28 31 33 29 20 61 6e  es (11), (13) an
25261 64 20 28 31 34 29 2c 20 74 68 65 79 20 6d 61 79  d (14), they may
25262 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 20 4f 52   also contain OR
25263 44 45 52 20 42 59 2c 0a 2a 2a 20 20 20 20 20 20  DER BY,.**      
25264 20 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53    LIMIT and OFFS
25265 45 54 20 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a  ET clauses..**.*
25266 2a 20 20 28 31 38 29 20 20 49 66 20 74 68 65 20  *  (18)  If the 
25267 73 75 62 2d 71 75 65 72 79 20 69 73 20 61 20 63  sub-query is a c
25268 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2c 20  ompound select, 
25269 74 68 65 6e 20 61 6c 6c 20 74 65 72 6d 73 20 6f  then all terms o
2526a 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  f the.**        
2526b 4f 52 44 45 52 20 62 79 20 63 6c 61 75 73 65 20  ORDER by clause 
2526c 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20 6d 75  of the parent mu
2526d 73 74 20 62 65 20 73 69 6d 70 6c 65 20 72 65 66  st be simple ref
2526e 65 72 65 6e 63 65 73 20 74 6f 20 0a 2a 2a 20 20  erences to .**  
2526f 20 20 20 20 20 20 63 6f 6c 75 6d 6e 73 20 6f 66        columns of
25270 20 74 68 65 20 73 75 62 2d 71 75 65 72 79 2e 0a   the sub-query..
25271 2a 2a 0a 2a 2a 20 20 28 31 39 29 20 20 54 68 65  **.**  (19)  The
25272 20 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e   subquery does n
25273 6f 74 20 75 73 65 20 4c 49 4d 49 54 20 6f 72 20  ot use LIMIT or 
25274 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
25275 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 20 20 20 20  does not.**     
25276 20 20 20 68 61 76 65 20 61 20 57 48 45 52 45 20     have a WHERE 
25277 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e  clause..**.** In
25278 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 74   this routine, t
25279 68 65 20 22 70 22 20 70 61 72 61 6d 65 74 65 72  he "p" parameter
2527a 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
2527b 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
2527c 2e 0a 2a 2a 20 54 68 65 20 73 75 62 71 75 65 72  ..** The subquer
2527d 79 20 69 73 20 70 2d 3e 70 53 72 63 2d 3e 61 5b  y is p->pSrc->a[
2527e 69 46 72 6f 6d 5d 2e 20 20 69 73 41 67 67 20 69  iFrom].  isAgg i
2527f 73 20 74 72 75 65 20 69 66 20 74 68 65 20 6f 75  s true if the ou
25280 74 65 72 20 71 75 65 72 79 0a 2a 2a 20 75 73 65  ter query.** use
25281 73 20 61 67 67 72 65 67 61 74 65 73 20 61 6e 64  s aggregates and
25282 20 73 75 62 71 75 65 72 79 49 73 41 67 67 20 69   subqueryIsAgg i
25283 73 20 74 72 75 65 20 69 66 20 74 68 65 20 73 75  s true if the su
25284 62 71 75 65 72 79 20 75 73 65 73 20 61 67 67 72  bquery uses aggr
25285 65 67 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  egates..**.** If
25286 20 66 6c 61 74 74 65 6e 69 6e 67 20 69 73 20 6e   flattening is n
25287 6f 74 20 61 74 74 65 6d 70 74 65 64 2c 20 74 68  ot attempted, th
25288 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
25289 6e 6f 2d 6f 70 20 61 6e 64 20 72 65 74 75 72 6e  no-op and return
2528a 73 20 30 2e 0a 2a 2a 20 49 66 20 66 6c 61 74 74  s 0..** If flatt
2528b 65 6e 69 6e 67 20 69 73 20 61 74 74 65 6d 70 74  ening is attempt
2528c 65 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ed this routine 
2528d 72 65 74 75 72 6e 73 20 31 2e 0a 2a 2a 0a 2a 2a  returns 1..**.**
2528e 20 41 6c 6c 20 6f 66 20 74 68 65 20 65 78 70 72   All of the expr
2528f 65 73 73 69 6f 6e 20 61 6e 61 6c 79 73 69 73 20  ession analysis 
25290 6d 75 73 74 20 6f 63 63 75 72 20 6f 6e 20 62 6f  must occur on bo
25291 74 68 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  th the outer que
25292 72 79 20 61 6e 64 0a 2a 2a 20 74 68 65 20 73 75  ry and.** the su
25293 62 71 75 65 72 79 20 62 65 66 6f 72 65 20 74 68  bquery before th
25294 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e 73 2e  is routine runs.
25295 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
25296 6c 61 74 74 65 6e 53 75 62 71 75 65 72 79 28 0a  lattenSubquery(.
25297 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
25298 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
25299 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53  g context */.  S
2529a 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20  elect *p,       
2529b 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 65 6e      /* The paren
2529c 74 20 6f 72 20 6f 75 74 65 72 20 53 45 4c 45 43  t or outer SELEC
2529d 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  T statement */. 
2529e 20 69 6e 74 20 69 46 72 6f 6d 2c 20 20 20 20 20   int iFrom,     
2529f 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69        /* Index i
252a0 6e 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 5d 20 6f  n p->pSrc->a[] o
252a1 66 20 74 68 65 20 69 6e 6e 65 72 20 73 75 62 71  f the inner subq
252a2 75 65 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69 73  uery */.  int is
252a3 41 67 67 2c 20 20 20 20 20 20 20 20 20 20 20 2f  Agg,           /
252a4 2a 20 54 72 75 65 20 69 66 20 6f 75 74 65 72 20  * True if outer 
252a5 53 45 4c 45 43 54 20 75 73 65 73 20 61 67 67 72  SELECT uses aggr
252a6 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20  egate functions 
252a7 2a 2f 0a 20 20 69 6e 74 20 73 75 62 71 75 65 72  */.  int subquer
252a8 79 49 73 41 67 67 20 20 20 20 2f 2a 20 54 72 75  yIsAgg    /* Tru
252a9 65 20 69 66 20 74 68 65 20 73 75 62 71 75 65 72  e if the subquer
252aa 79 20 75 73 65 73 20 61 67 67 72 65 67 61 74 65  y uses aggregate
252ab 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 29 7b   functions */.){
252ac 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
252ad 53 61 76 65 64 41 75 74 68 43 6f 6e 74 65 78 74  SavedAuthContext
252ae 20 3d 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68   = pParse->zAuth
252af 43 6f 6e 74 65 78 74 3b 0a 20 20 53 65 6c 65 63  Context;.  Selec
252b0 74 20 2a 70 50 61 72 65 6e 74 3b 0a 20 20 53 65  t *pParent;.  Se
252b1 6c 65 63 74 20 2a 70 53 75 62 3b 20 20 20 20 20  lect *pSub;     
252b2 20 20 2f 2a 20 54 68 65 20 69 6e 6e 65 72 20 71    /* The inner q
252b3 75 65 72 79 20 6f 72 20 22 73 75 62 71 75 65 72  uery or "subquer
252b4 79 22 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  y" */.  Select *
252b5 70 53 75 62 31 3b 20 20 20 20 20 20 2f 2a 20 50  pSub1;      /* P
252b6 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 69  ointer to the ri
252b7 67 68 74 6d 6f 73 74 20 73 65 6c 65 63 74 20 69  ghtmost select i
252b8 6e 20 73 75 62 2d 71 75 65 72 79 20 2a 2f 0a 20  n sub-query */. 
252b9 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 20   SrcList *pSrc; 
252ba 20 20 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d       /* The FROM
252bb 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 6f   clause of the o
252bc 75 74 65 72 20 71 75 65 72 79 20 2a 2f 0a 20 20  uter query */.  
252bd 53 72 63 4c 69 73 74 20 2a 70 53 75 62 53 72 63  SrcList *pSubSrc
252be 3b 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20  ;   /* The FROM 
252bf 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 73 75  clause of the su
252c0 62 71 75 65 72 79 20 2a 2f 0a 20 20 45 78 70 72  bquery */.  Expr
252c1 4c 69 73 74 20 2a 70 4c 69 73 74 3b 20 20 20 20  List *pList;    
252c2 2f 2a 20 54 68 65 20 72 65 73 75 6c 74 20 73 65  /* The result se
252c3 74 20 6f 66 20 74 68 65 20 6f 75 74 65 72 20 71  t of the outer q
252c4 75 65 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69 50  uery */.  int iP
252c5 61 72 65 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a  arent;        /*
252c6 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d   VDBE cursor num
252c7 62 65 72 20 6f 66 20 74 68 65 20 70 53 75 62 20  ber of the pSub 
252c8 72 65 73 75 6c 74 20 73 65 74 20 74 65 6d 70 20  result set temp 
252c9 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69  table */.  int i
252ca 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
252cb 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
252cc 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65  /.  Expr *pWhere
252cd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
252ce 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52       /* The WHER
252cf 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74  E clause */.  st
252d0 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
252d1 6d 20 2a 70 53 75 62 69 74 65 6d 3b 20 20 20 2f  m *pSubitem;   /
252d2 2a 20 54 68 65 20 73 75 62 71 75 65 72 79 20 2a  * The subquery *
252d3 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
252d4 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20  = pParse->db;.. 
252d5 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65   /* Check to see
252d6 20 69 66 20 66 6c 61 74 74 65 6e 69 6e 67 20 69   if flattening i
252d7 73 20 70 65 72 6d 69 74 74 65 64 2e 20 20 52 65  s permitted.  Re
252d8 74 75 72 6e 20 30 20 69 66 20 6e 6f 74 2e 0a 20  turn 0 if not.. 
252d9 20 2a 2f 0a 20 20 69 66 28 20 70 3d 3d 30 20 29   */.  if( p==0 )
252da 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 53 72   return 0;.  pSr
252db 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 61  c = p->pSrc;.  a
252dc 73 73 65 72 74 28 20 70 53 72 63 20 26 26 20 69  ssert( pSrc && i
252dd 46 72 6f 6d 3e 3d 30 20 26 26 20 69 46 72 6f 6d  From>=0 && iFrom
252de 3c 70 53 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 20  <pSrc->nSrc );. 
252df 20 70 53 75 62 69 74 65 6d 20 3d 20 26 70 53 72   pSubitem = &pSr
252e0 63 2d 3e 61 5b 69 46 72 6f 6d 5d 3b 0a 20 20 69  c->a[iFrom];.  i
252e1 50 61 72 65 6e 74 20 3d 20 70 53 75 62 69 74 65  Parent = pSubite
252e2 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 70 53  m->iCursor;.  pS
252e3 75 62 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e 70  ub = pSubitem->p
252e4 53 65 6c 65 63 74 3b 0a 20 20 61 73 73 65 72 74  Select;.  assert
252e5 28 20 70 53 75 62 21 3d 30 20 29 3b 0a 20 20 69  ( pSub!=0 );.  i
252e6 66 28 20 69 73 41 67 67 20 26 26 20 73 75 62 71  f( isAgg && subq
252e7 75 65 72 79 49 73 41 67 67 20 29 20 72 65 74 75  ueryIsAgg ) retu
252e8 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20  rn 0;           
252e9 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63        /* Restric
252ea 74 69 6f 6e 20 28 31 29 20 20 2a 2f 0a 20 20 69  tion (1)  */.  i
252eb 66 28 20 73 75 62 71 75 65 72 79 49 73 41 67 67  f( subqueryIsAgg
252ec 20 26 26 20 70 53 72 63 2d 3e 6e 53 72 63 3e 31   && pSrc->nSrc>1
252ed 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20   ) return 0;    
252ee 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63        /* Restric
252ef 74 69 6f 6e 20 28 32 29 20 20 2a 2f 0a 20 20 70  tion (2)  */.  p
252f0 53 75 62 53 72 63 20 3d 20 70 53 75 62 2d 3e 70  SubSrc = pSub->p
252f1 53 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70  Src;.  assert( p
252f2 53 75 62 53 72 63 20 29 3b 0a 20 20 2f 2a 20 50  SubSrc );.  /* P
252f3 72 69 6f 72 20 74 6f 20 76 65 72 73 69 6f 6e 20  rior to version 
252f4 33 2e 31 2e 32 2c 20 77 68 65 6e 20 4c 49 4d 49  3.1.2, when LIMI
252f5 54 20 61 6e 64 20 4f 46 46 53 45 54 20 68 61 64  T and OFFSET had
252f6 20 74 6f 20 62 65 20 73 69 6d 70 6c 65 20 63 6f   to be simple co
252f7 6e 73 74 61 6e 74 73 2c 0a 20 20 2a 2a 20 6e 6f  nstants,.  ** no
252f8 74 20 61 72 62 69 74 72 61 72 79 20 65 78 70 72  t arbitrary expr
252f9 65 73 73 73 69 6f 6e 73 2c 20 77 65 20 61 6c 6c  esssions, we all
252fa 6f 77 65 64 20 73 6f 6d 65 20 63 6f 6d 62 69 6e  owed some combin
252fb 69 6e 67 20 6f 66 20 4c 49 4d 49 54 20 61 6e 64  ing of LIMIT and
252fc 20 4f 46 46 53 45 54 0a 20 20 2a 2a 20 62 65 63   OFFSET.  ** bec
252fd 61 75 73 65 20 74 68 65 79 20 63 6f 75 6c 64 20  ause they could 
252fe 62 65 20 63 6f 6d 70 75 74 65 64 20 61 74 20 63  be computed at c
252ff 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 20 20 42 75  ompile-time.  Bu
25300 74 20 77 68 65 6e 20 4c 49 4d 49 54 20 61 6e 64  t when LIMIT and
25301 20 4f 46 46 53 45 54 0a 20 20 2a 2a 20 62 65 63   OFFSET.  ** bec
25302 61 6d 65 20 61 72 62 69 74 72 61 72 79 20 65 78  ame arbitrary ex
25303 70 72 65 73 73 69 6f 6e 73 2c 20 77 65 20 77 65  pressions, we we
25304 72 65 20 66 6f 72 63 65 64 20 74 6f 20 61 64 64  re forced to add
25305 20 72 65 73 74 72 69 63 74 69 6f 6e 73 20 28 31   restrictions (1
25306 33 29 0a 20 20 2a 2a 20 61 6e 64 20 28 31 34 29  3).  ** and (14)
25307 2e 20 2a 2f 0a 20 20 69 66 28 20 70 53 75 62 2d  . */.  if( pSub-
25308 3e 70 4c 69 6d 69 74 20 26 26 20 70 2d 3e 70 4c  >pLimit && p->pL
25309 69 6d 69 74 20 29 20 72 65 74 75 72 6e 20 30 3b  imit ) return 0;
2530a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2530b 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 33   Restriction (13
2530c 29 20 2a 2f 0a 20 20 69 66 28 20 70 53 75 62 2d  ) */.  if( pSub-
2530d 3e 70 4f 66 66 73 65 74 20 29 20 72 65 74 75 72  >pOffset ) retur
2530e 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  n 0;            
2530f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
25310 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 34   Restriction (14
25311 29 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 52  ) */.  if( p->pR
25312 69 67 68 74 6d 6f 73 74 20 26 26 20 70 53 75 62  ightmost && pSub
25313 2d 3e 70 4c 69 6d 69 74 20 26 26 20 70 53 75 62  ->pLimit && pSub
25314 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ->pOrderBy ){.  
25315 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20    return 0;     
25316 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25317 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25318 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69         /* Restri
25319 63 74 69 6f 6e 20 28 31 35 29 20 2a 2f 0a 20 20  ction (15) */.  
2531a 7d 0a 20 20 69 66 28 20 70 53 75 62 53 72 63 2d  }.  if( pSubSrc-
2531b 3e 6e 53 72 63 3d 3d 30 20 29 20 72 65 74 75 72  >nSrc==0 ) retur
2531c 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  n 0;            
2531d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
2531e 73 74 72 69 63 74 69 6f 6e 20 28 37 29 20 20 2a  striction (7)  *
2531f 2f 0a 20 20 69 66 28 20 28 28 70 53 75 62 2d 3e  /.  if( ((pSub->
25320 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69  selFlags & SF_Di
25321 73 74 69 6e 63 74 29 21 3d 30 20 7c 7c 20 70 53  stinct)!=0 || pS
25322 75 62 2d 3e 70 4c 69 6d 69 74 29 20 0a 20 20 20  ub->pLimit) .   
25323 20 20 20 20 20 20 26 26 20 28 70 53 72 63 2d 3e        && (pSrc->
25324 6e 53 72 63 3e 31 20 7c 7c 20 69 73 41 67 67 29  nSrc>1 || isAgg)
25325 20 29 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20   ){          /* 
25326 52 65 73 74 72 69 63 74 69 6f 6e 73 20 28 34 29  Restrictions (4)
25327 28 35 29 28 38 29 28 39 29 20 2a 2f 0a 20 20 20  (5)(8)(9) */.   
25328 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20    return 0;     
25329 20 20 0a 20 20 7d 0a 20 20 69 66 28 20 28 70 2d    .  }.  if( (p-
2532a 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44  >selFlags & SF_D
2532b 69 73 74 69 6e 63 74 29 21 3d 30 20 26 26 20 73  istinct)!=0 && s
2532c 75 62 71 75 65 72 79 49 73 41 67 67 20 29 7b 0a  ubqueryIsAgg ){.
2532d 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20       return 0;  
2532e 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69         /* Restri
2532f 63 74 69 6f 6e 20 28 36 29 20 20 2a 2f 0a 20 20  ction (6)  */.  
25330 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4f 72 64 65  }.  if( p->pOrde
25331 72 42 79 20 26 26 20 70 53 75 62 2d 3e 70 4f 72  rBy && pSub->pOr
25332 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20 72 65  derBy ){.     re
25333 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20  turn 0;         
25334 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25335 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25336 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e    /* Restriction
25337 20 28 31 31 29 20 2a 2f 0a 20 20 7d 0a 20 20 69   (11) */.  }.  i
25338 66 28 20 69 73 41 67 67 20 26 26 20 70 53 75 62  f( isAgg && pSub
25339 2d 3e 70 4f 72 64 65 72 42 79 20 29 20 72 65 74  ->pOrderBy ) ret
2533a 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20  urn 0;          
2533b 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63        /* Restric
2533c 74 69 6f 6e 20 28 31 36 29 20 2a 2f 0a 20 20 69  tion (16) */.  i
2533d 66 28 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20  f( pSub->pLimit 
2533e 26 26 20 70 2d 3e 70 57 68 65 72 65 20 29 20 72  && p->pWhere ) r
2533f 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20  eturn 0;        
25340 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63        /* Restric
25341 74 69 6f 6e 20 28 31 39 29 20 2a 2f 0a 0a 20 20  tion (19) */..  
25342 2f 2a 20 4f 42 53 4f 4c 45 54 45 20 43 4f 4d 4d  /* OBSOLETE COMM
25343 45 4e 54 20 31 3a 0a 20 20 2a 2a 20 52 65 73 74  ENT 1:.  ** Rest
25344 72 69 63 74 69 6f 6e 20 33 3a 20 20 49 66 20 74  riction 3:  If t
25345 68 65 20 73 75 62 71 75 65 72 79 20 69 73 20 61  he subquery is a
25346 20 6a 6f 69 6e 2c 20 6d 61 6b 65 20 73 75 72 65   join, make sure
25347 20 74 68 65 20 73 75 62 71 75 65 72 79 20 69 73   the subquery is
25348 20 0a 20 20 2a 2a 20 6e 6f 74 20 75 73 65 64 20   .  ** not used 
25349 61 73 20 74 68 65 20 72 69 67 68 74 20 6f 70 65  as the right ope
2534a 72 61 6e 64 20 6f 66 20 61 6e 20 6f 75 74 65 72  rand of an outer
2534b 20 6a 6f 69 6e 2e 20 20 45 78 61 6d 70 6c 65 73   join.  Examples
2534c 20 6f 66 20 77 68 79 20 74 68 69 73 0a 20 20 2a   of why this.  *
2534d 2a 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64  * is not allowed
2534e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  :.  **.  **     
2534f 20 20 20 20 74 31 20 4c 45 46 54 20 4f 55 54 45      t1 LEFT OUTE
25350 52 20 4a 4f 49 4e 20 28 74 32 20 4a 4f 49 4e 20  R JOIN (t2 JOIN 
25351 74 33 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66  t3).  **.  ** If
25352 20 77 65 20 66 6c 61 74 74 65 6e 20 74 68 65 20   we flatten the 
25353 61 62 6f 76 65 2c 20 77 65 20 77 6f 75 6c 64 20  above, we would 
25354 67 65 74 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  get.  **.  **   
25355 20 20 20 20 20 20 28 74 31 20 4c 45 46 54 20 4f        (t1 LEFT O
25356 55 54 45 52 20 4a 4f 49 4e 20 74 32 29 20 4a 4f  UTER JOIN t2) JO
25357 49 4e 20 74 33 0a 20 20 2a 2a 0a 20 20 2a 2a 20  IN t3.  **.  ** 
25358 77 68 69 63 68 20 69 73 20 6e 6f 74 20 61 74 20  which is not at 
25359 61 6c 6c 20 74 68 65 20 73 61 6d 65 20 74 68 69  all the same thi
2535a 6e 67 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f 42  ng..  **.  ** OB
2535b 53 4f 4c 45 54 45 20 43 4f 4d 4d 45 4e 54 20 32  SOLETE COMMENT 2
2535c 3a 0a 20 20 2a 2a 20 52 65 73 74 72 69 63 74 69  :.  ** Restricti
2535d 6f 6e 20 31 32 3a 20 20 49 66 20 74 68 65 20 73  on 12:  If the s
2535e 75 62 71 75 65 72 79 20 69 73 20 74 68 65 20 72  ubquery is the r
2535f 69 67 68 74 20 6f 70 65 72 61 6e 64 20 6f 66 20  ight operand of 
25360 61 20 6c 65 66 74 20 6f 75 74 65 72 0a 20 20 2a  a left outer.  *
25361 2a 20 6a 6f 69 6e 2c 20 6d 61 6b 65 20 73 75 72  * join, make sur
25362 65 20 74 68 65 20 73 75 62 71 75 65 72 79 20 68  e the subquery h
25363 61 73 20 6e 6f 20 57 48 45 52 45 20 63 6c 61 75  as no WHERE clau
25364 73 65 2e 0a 20 20 2a 2a 20 41 6e 20 65 78 61 6d  se..  ** An exam
25365 70 6c 65 73 20 6f 66 20 77 68 79 20 74 68 69 73  ples of why this
25366 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 3a   is not allowed:
25367 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20  .  **.  **      
25368 20 20 20 74 31 20 4c 45 46 54 20 4f 55 54 45 52     t1 LEFT OUTER
25369 20 4a 4f 49 4e 20 28 53 45 4c 45 43 54 20 2a 20   JOIN (SELECT * 
2536a 46 52 4f 4d 20 74 32 20 57 48 45 52 45 20 74 32  FROM t2 WHERE t2
2536b 2e 78 3e 30 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20  .x>0).  **.  ** 
2536c 49 66 20 77 65 20 66 6c 61 74 74 65 6e 20 74 68  If we flatten th
2536d 65 20 61 62 6f 76 65 2c 20 77 65 20 77 6f 75 6c  e above, we woul
2536e 64 20 67 65 74 0a 20 20 2a 2a 0a 20 20 2a 2a 20  d get.  **.  ** 
2536f 20 20 20 20 20 20 20 20 28 74 31 20 4c 45 46 54          (t1 LEFT
25370 20 4f 55 54 45 52 20 4a 4f 49 4e 20 74 32 29 20   OUTER JOIN t2) 
25371 57 48 45 52 45 20 74 32 2e 78 3e 30 0a 20 20 2a  WHERE t2.x>0.  *
25372 2a 0a 20 20 2a 2a 20 42 75 74 20 74 68 65 20 74  *.  ** But the t
25373 32 2e 78 3e 30 20 74 65 73 74 20 77 69 6c 6c 20  2.x>0 test will 
25374 61 6c 77 61 79 73 20 66 61 69 6c 20 6f 6e 20 61  always fail on a
25375 20 4e 55 4c 4c 20 72 6f 77 20 6f 66 20 74 32 2c   NULL row of t2,
25376 20 77 68 69 63 68 0a 20 20 2a 2a 20 65 66 66 65   which.  ** effe
25377 63 74 69 76 65 6c 79 20 63 6f 6e 76 65 72 74 73  ctively converts
25378 20 74 68 65 20 4f 55 54 45 52 20 4a 4f 49 4e 20   the OUTER JOIN 
25379 69 6e 74 6f 20 61 6e 20 49 4e 4e 45 52 20 4a 4f  into an INNER JO
2537a 49 4e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 48  IN..  **.  ** TH
2537b 49 53 20 4f 56 45 52 52 49 44 45 53 20 4f 42 53  IS OVERRIDES OBS
2537c 4f 4c 45 54 45 20 43 4f 4d 4d 45 4e 54 53 20 31  OLETE COMMENTS 1
2537d 20 41 4e 44 20 32 20 41 42 4f 56 45 3a 0a 20 20   AND 2 ABOVE:.  
2537e 2a 2a 20 54 69 63 6b 65 74 20 23 33 33 30 30 20  ** Ticket #3300 
2537f 73 68 6f 77 73 20 74 68 61 74 20 66 6c 61 74 74  shows that flatt
25380 65 6e 69 6e 67 20 74 68 65 20 72 69 67 68 74 20  ening the right 
25381 74 65 72 6d 20 6f 66 20 61 20 4c 45 46 54 20 4a  term of a LEFT J
25382 4f 49 4e 0a 20 20 2a 2a 20 69 73 20 66 72 61 75  OIN.  ** is frau
25383 67 68 74 20 77 69 74 68 20 64 61 6e 67 65 72 2e  ght with danger.
25384 20 20 42 65 73 74 20 74 6f 20 61 76 6f 69 64 20    Best to avoid 
25385 74 68 65 20 77 68 6f 6c 65 20 74 68 69 6e 67 2e  the whole thing.
25386 20 20 49 66 20 74 68 65 0a 20 20 2a 2a 20 73 75    If the.  ** su
25387 62 71 75 65 72 79 20 69 73 20 74 68 65 20 72 69  bquery is the ri
25388 67 68 74 20 74 65 72 6d 20 6f 66 20 61 20 4c 45  ght term of a LE
25389 46 54 20 4a 4f 49 4e 2c 20 74 68 65 6e 20 64 6f  FT JOIN, then do
2538a 20 6e 6f 74 20 66 6c 61 74 74 65 6e 2e 0a 20 20   not flatten..  
2538b 2a 2f 0a 20 20 69 66 28 20 28 70 53 75 62 69 74  */.  if( (pSubit
2538c 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20 4a  em->jointype & J
2538d 54 5f 4f 55 54 45 52 29 21 3d 30 20 29 7b 0a 20  T_OUTER)!=0 ){. 
2538e 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
2538f 0a 0a 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69  ..  /* Restricti
25390 6f 6e 20 31 37 3a 20 49 66 20 74 68 65 20 73 75  on 17: If the su
25391 62 2d 71 75 65 72 79 20 69 73 20 61 20 63 6f 6d  b-query is a com
25392 70 6f 75 6e 64 20 53 45 4c 45 43 54 2c 20 74 68  pound SELECT, th
25393 65 6e 20 69 74 20 6d 75 73 74 0a 20 20 2a 2a 20  en it must.  ** 
25394 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 55 4e 49  use only the UNI
25395 4f 4e 20 41 4c 4c 20 6f 70 65 72 61 74 6f 72 2e  ON ALL operator.
25396 20 41 6e 64 20 6e 6f 6e 65 20 6f 66 20 74 68 65   And none of the
25397 20 73 69 6d 70 6c 65 20 73 65 6c 65 63 74 20 71   simple select q
25398 75 65 72 69 65 73 0a 20 20 2a 2a 20 74 68 61 74  ueries.  ** that
25399 20 6d 61 6b 65 20 75 70 20 74 68 65 20 63 6f 6d   make up the com
2539a 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 61 72 65  pound SELECT are
2539b 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65 20 61   allowed to be a
2539c 67 67 72 65 67 61 74 65 20 6f 72 20 64 69 73 74  ggregate or dist
2539d 69 6e 63 74 0a 20 20 2a 2a 20 71 75 65 72 69 65  inct.  ** querie
2539e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53  s..  */.  if( pS
2539f 75 62 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20  ub->pPrior ){.  
253a0 20 20 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 20    if( p->pPrior 
253a1 7c 7c 20 69 73 41 67 67 20 7c 7c 20 28 70 2d 3e  || isAgg || (p->
253a2 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69  selFlags & SF_Di
253a3 73 74 69 6e 63 74 29 21 3d 30 20 7c 7c 20 70 53  stinct)!=0 || pS
253a4 72 63 2d 3e 6e 53 72 63 21 3d 31 20 29 7b 0a 20  rc->nSrc!=1 ){. 
253a5 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
253a6 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 70 53 75     }.    for(pSu
253a7 62 31 3d 70 53 75 62 3b 20 70 53 75 62 31 3b 20  b1=pSub; pSub1; 
253a8 70 53 75 62 31 3d 70 53 75 62 31 2d 3e 70 50 72  pSub1=pSub1->pPr
253a9 69 6f 72 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ior){.      if( 
253aa 28 70 53 75 62 31 2d 3e 73 65 6c 46 6c 61 67 73  (pSub1->selFlags
253ab 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74 7c   & (SF_Distinct|
253ac 53 46 5f 41 67 67 72 65 67 61 74 65 29 29 21 3d  SF_Aggregate))!=
253ad 30 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 53 75  0.       || (pSu
253ae 62 31 2d 3e 70 50 72 69 6f 72 20 26 26 20 70 53  b1->pPrior && pS
253af 75 62 31 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 29  ub1->op!=TK_ALL)
253b0 20 0a 20 20 20 20 20 20 20 7c 7c 20 21 70 53 75   .       || !pSu
253b1 62 31 2d 3e 70 53 72 63 20 7c 7c 20 70 53 75 62  b1->pSrc || pSub
253b2 31 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 21 3d 31  1->pSrc->nSrc!=1
253b3 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20  .      ){.      
253b4 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
253b5 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
253b6 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 31 38  * Restriction 18
253b7 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e  . */.    if( p->
253b8 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
253b9 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20    int ii;.      
253ba 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 2d 3e  for(ii=0; ii<p->
253bb 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b  pOrderBy->nExpr;
253bc 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20   ii++){.        
253bd 69 66 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 2d  if( p->pOrderBy-
253be 3e 61 5b 69 69 5d 2e 69 43 6f 6c 3d 3d 30 20 29  >a[ii].iCol==0 )
253bf 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
253c0 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
253c1 2f 2a 2a 2a 2a 2a 20 49 66 20 77 65 20 72 65 61  /***** If we rea
253c2 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 66  ch this point, f
253c3 6c 61 74 74 65 6e 69 6e 67 20 69 73 20 70 65 72  lattening is per
253c4 6d 69 74 74 65 64 2e 20 2a 2a 2a 2a 2a 2f 0a 0a  mitted. *****/..
253c5 20 20 2f 2a 20 41 75 74 68 6f 72 69 7a 65 20 74    /* Authorize t
253c6 68 65 20 73 75 62 71 75 65 72 79 20 2a 2f 0a 20  he subquery */. 
253c7 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f   pParse->zAuthCo
253c8 6e 74 65 78 74 20 3d 20 70 53 75 62 69 74 65 6d  ntext = pSubitem
253c9 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 73 71 6c 69 74  ->zName;.  sqlit
253ca 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
253cb 73 65 2c 20 53 51 4c 49 54 45 5f 53 45 4c 45 43  se, SQLITE_SELEC
253cc 54 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 70  T, 0, 0, 0);.  p
253cd 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74  Parse->zAuthCont
253ce 65 78 74 20 3d 20 7a 53 61 76 65 64 41 75 74 68  ext = zSavedAuth
253cf 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 2f 2a 20 49  Context;..  /* I
253d0 66 20 74 68 65 20 73 75 62 2d 71 75 65 72 79 20  f the sub-query 
253d1 69 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45  is a compound SE
253d2 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2c 20  LECT statement, 
253d3 74 68 65 6e 20 28 62 79 20 72 65 73 74 72 69 63  then (by restric
253d4 74 69 6f 6e 73 0a 20 20 2a 2a 20 31 37 20 61 6e  tions.  ** 17 an
253d5 64 20 31 38 20 61 62 6f 76 65 29 20 69 74 20 6d  d 18 above) it m
253d6 75 73 74 20 62 65 20 61 20 55 4e 49 4f 4e 20 41  ust be a UNION A
253d7 4c 4c 20 61 6e 64 20 74 68 65 20 70 61 72 65 6e  LL and the paren
253d8 74 20 71 75 65 72 79 20 6d 75 73 74 20 0a 20 20  t query must .  
253d9 2a 2a 20 62 65 20 6f 66 20 74 68 65 20 66 6f 72  ** be of the for
253da 6d 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  m:.  **.  **    
253db 20 53 45 4c 45 43 54 20 3c 65 78 70 72 2d 6c 69   SELECT <expr-li
253dc 73 74 3e 20 46 52 4f 4d 20 28 3c 73 75 62 2d 71  st> FROM (<sub-q
253dd 75 65 72 79 3e 29 20 3c 77 68 65 72 65 2d 63 6c  uery>) <where-cl
253de 61 75 73 65 3e 20 0a 20 20 2a 2a 0a 20 20 2a 2a  ause> .  **.  **
253df 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 6e 79   followed by any
253e0 20 4f 52 44 45 52 20 42 59 2c 20 4c 49 4d 49 54   ORDER BY, LIMIT
253e1 20 61 6e 64 2f 6f 72 20 4f 46 46 53 45 54 20 63   and/or OFFSET c
253e2 6c 61 75 73 65 73 2e 20 54 68 69 73 20 62 6c 6f  lauses. This blo
253e3 63 6b 0a 20 20 2a 2a 20 63 72 65 61 74 65 73 20  ck.  ** creates 
253e4 4e 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20  N copies of the 
253e5 70 61 72 65 6e 74 20 71 75 65 72 79 20 77 69 74  parent query wit
253e6 68 6f 75 74 20 61 6e 79 20 4f 52 44 45 52 20 42  hout any ORDER B
253e7 59 2c 20 4c 49 4d 49 54 20 6f 72 20 0a 20 20 2a  Y, LIMIT or .  *
253e8 2a 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65 73  * OFFSET clauses
253e9 20 61 6e 64 20 6a 6f 69 6e 73 20 74 68 65 6d 20   and joins them 
253ea 74 6f 20 74 68 65 20 6c 65 66 74 2d 68 61 6e 64  to the left-hand
253eb 2d 73 69 64 65 20 6f 66 20 74 68 65 20 6f 72 69  -side of the ori
253ec 67 69 6e 61 6c 0a 20 20 2a 2a 20 75 73 69 6e 67  ginal.  ** using
253ed 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 70 65 72 61   UNION ALL opera
253ee 74 6f 72 73 2e 20 49 6e 20 74 68 69 73 20 63 61  tors. In this ca
253ef 73 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62  se N is the numb
253f0 65 72 20 6f 66 20 73 69 6d 70 6c 65 0a 20 20 2a  er of simple.  *
253f1 2a 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65  * select stateme
253f2 6e 74 73 20 69 6e 20 74 68 65 20 63 6f 6d 70 6f  nts in the compo
253f3 75 6e 64 20 73 75 62 2d 71 75 65 72 79 2e 0a 20  und sub-query.. 
253f4 20 2a 2f 0a 20 20 66 6f 72 28 70 53 75 62 3d 70   */.  for(pSub=p
253f5 53 75 62 2d 3e 70 50 72 69 6f 72 3b 20 70 53 75  Sub->pPrior; pSu
253f6 62 3b 20 70 53 75 62 3d 70 53 75 62 2d 3e 70 50  b; pSub=pSub->pP
253f7 72 69 6f 72 29 7b 0a 20 20 20 20 53 65 6c 65 63  rior){.    Selec
253f8 74 20 2a 70 4e 65 77 3b 0a 20 20 20 20 45 78 70  t *pNew;.    Exp
253f9 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 20  rList *pOrderBy 
253fa 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20  = p->pOrderBy;. 
253fb 20 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 20     Expr *pLimit 
253fc 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20 20  = p->pLimit;.   
253fd 20 45 78 70 72 20 2a 70 4f 66 66 73 65 74 20 3d   Expr *pOffset =
253fe 20 70 2d 3e 70 4f 66 66 73 65 74 3b 0a 20 20 20   p->pOffset;.   
253ff 20 53 65 6c 65 63 74 20 2a 70 50 72 69 6f 72 20   Select *pPrior 
25400 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20  = p->pPrior;.   
25401 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 30   p->pOrderBy = 0
25402 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 20 3d 20  ;.    p->pSrc = 
25403 30 3b 0a 20 20 20 20 70 2d 3e 70 50 72 69 6f 72  0;.    p->pPrior
25404 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70 4c 69   = 0;.    p->pLi
25405 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 70 4e 65  mit = 0;.    pNe
25406 77 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63  w = sqlite3Selec
25407 74 44 75 70 28 64 62 2c 20 70 29 3b 0a 20 20 20  tDup(db, p);.   
25408 20 70 4e 65 77 2d 3e 70 50 72 69 6f 72 20 3d 20   pNew->pPrior = 
25409 70 50 72 69 6f 72 3b 0a 20 20 20 20 70 2d 3e 70  pPrior;.    p->p
2540a 50 72 69 6f 72 20 3d 20 70 4e 65 77 3b 0a 20 20  Prior = pNew;.  
2540b 20 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20    p->pOrderBy = 
2540c 70 4f 72 64 65 72 42 79 3b 0a 20 20 20 20 70 2d  pOrderBy;.    p-
2540d 3e 6f 70 20 3d 20 54 4b 5f 41 4c 4c 3b 0a 20 20  >op = TK_ALL;.  
2540e 20 20 70 2d 3e 70 53 72 63 20 3d 20 70 53 72 63    p->pSrc = pSrc
2540f 3b 0a 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20  ;.    p->pLimit 
25410 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 20 20 70 2d  = pLimit;.    p-
25411 3e 70 4f 66 66 73 65 74 20 3d 20 70 4f 66 66 73  >pOffset = pOffs
25412 65 74 3b 0a 20 20 20 20 70 2d 3e 70 52 69 67 68  et;.    p->pRigh
25413 74 6d 6f 73 74 20 3d 20 30 3b 0a 20 20 20 20 70  tmost = 0;.    p
25414 4e 65 77 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20  New->pRightmost 
25415 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42  = 0;.  }..  /* B
25416 65 67 69 6e 20 66 6c 61 74 74 65 6e 69 6e 67 20  egin flattening 
25417 74 68 65 20 69 46 72 6f 6d 2d 74 68 20 65 6e 74  the iFrom-th ent
25418 72 79 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63  ry of the FROM c
25419 6c 61 75 73 65 20 0a 20 20 2a 2a 20 69 6e 20 74  lause .  ** in t
2541a 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 2e 0a  he outer query..
2541b 20 20 2a 2f 0a 20 20 70 53 75 62 20 3d 20 70 53    */.  pSub = pS
2541c 75 62 31 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e  ub1 = pSubitem->
2541d 70 53 65 6c 65 63 74 3b 0a 20 20 66 6f 72 28 70  pSelect;.  for(p
2541e 50 61 72 65 6e 74 3d 70 3b 20 70 50 61 72 65 6e  Parent=p; pParen
2541f 74 3b 20 70 50 61 72 65 6e 74 3d 70 50 61 72 65  t; pParent=pPare
25420 6e 74 2d 3e 70 50 72 69 6f 72 2c 20 70 53 75 62  nt->pPrior, pSub
25421 3d 70 53 75 62 2d 3e 70 50 72 69 6f 72 29 7b 0a  =pSub->pPrior){.
25422 20 20 20 20 69 6e 74 20 6e 53 75 62 53 72 63 20      int nSubSrc 
25423 3d 20 70 53 75 62 53 72 63 2d 3e 6e 53 72 63 3b  = pSubSrc->nSrc;
25424 0a 20 20 20 20 69 6e 74 20 6a 6f 69 6e 74 79 70  .    int jointyp
25425 65 20 3d 20 30 3b 0a 20 20 20 20 70 53 75 62 53  e = 0;.    pSubS
25426 72 63 20 3d 20 70 53 75 62 2d 3e 70 53 72 63 3b  rc = pSub->pSrc;
25427 0a 20 20 20 20 70 53 72 63 20 3d 20 70 50 61 72  .    pSrc = pPar
25428 65 6e 74 2d 3e 70 53 72 63 3b 0a 0a 20 20 20 20  ent->pSrc;..    
25429 2f 2a 20 4d 6f 76 65 20 61 6c 6c 20 6f 66 20 74  /* Move all of t
2542a 68 65 20 46 52 4f 4d 20 65 6c 65 6d 65 6e 74 73  he FROM elements
2542b 20 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79   of the subquery
2542c 20 69 6e 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a   into the.    **
2542d 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2542e 20 6f 66 20 74 68 65 20 6f 75 74 65 72 20 71 75   of the outer qu
2542f 65 72 79 2e 20 20 42 65 66 6f 72 65 20 64 6f 69  ery.  Before doi
25430 6e 67 20 74 68 69 73 2c 20 72 65 6d 65 6d 62 65  ng this, remembe
25431 72 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 75 72  r.    ** the cur
25432 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  sor number for t
25433 68 65 20 6f 72 69 67 69 6e 61 6c 20 6f 75 74 65  he original oute
25434 72 20 71 75 65 72 79 20 46 52 4f 4d 20 65 6c 65  r query FROM ele
25435 6d 65 6e 74 20 69 6e 0a 20 20 20 20 2a 2a 20 69  ment in.    ** i
25436 50 61 72 65 6e 74 2e 20 20 54 68 65 20 69 50 61  Parent.  The iPa
25437 72 65 6e 74 20 63 75 72 73 6f 72 20 77 69 6c 6c  rent cursor will
25438 20 6e 65 76 65 72 20 62 65 20 75 73 65 64 2e 20   never be used. 
25439 20 53 75 62 73 65 71 75 65 6e 74 20 63 6f 64 65   Subsequent code
2543a 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 73 63 61  .    ** will sca
2543b 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6c 6f  n expressions lo
2543c 6f 6b 69 6e 67 20 66 6f 72 20 69 50 61 72 65 6e  oking for iParen
2543d 74 20 72 65 66 65 72 65 6e 63 65 73 20 61 6e 64  t references and
2543e 20 72 65 70 6c 61 63 65 0a 20 20 20 20 2a 2a 20   replace.    ** 
2543f 74 68 6f 73 65 20 72 65 66 65 72 65 6e 63 65 73  those references
25440 20 77 69 74 68 20 65 78 70 72 65 73 73 69 6f 6e   with expression
25441 73 20 74 68 61 74 20 72 65 73 6f 6c 76 65 20 74  s that resolve t
25442 6f 20 74 68 65 20 73 75 62 71 75 65 72 79 20 46  o the subquery F
25443 52 4f 4d 0a 20 20 20 20 2a 2a 20 65 6c 65 6d 65  ROM.    ** eleme
25444 6e 74 73 20 77 65 20 61 72 65 20 6e 6f 77 20 63  nts we are now c
25445 6f 70 79 69 6e 67 20 69 6e 2e 0a 20 20 20 20 2a  opying in..    *
25446 2f 0a 20 20 20 20 69 66 28 20 70 53 72 63 20 29  /.    if( pSrc )
25447 7b 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70  {.      Table *p
25448 54 61 62 54 6f 44 65 6c 3b 0a 20 20 20 20 20 20  TabToDel;.      
25449 70 53 75 62 69 74 65 6d 20 3d 20 26 70 53 72 63  pSubitem = &pSrc
2544a 2d 3e 61 5b 69 46 72 6f 6d 5d 3b 0a 20 20 20 20  ->a[iFrom];.    
2544b 20 20 6e 53 75 62 53 72 63 20 3d 20 70 53 75 62    nSubSrc = pSub
2544c 53 72 63 2d 3e 6e 53 72 63 3b 0a 20 20 20 20 20  Src->nSrc;.     
2544d 20 6a 6f 69 6e 74 79 70 65 20 3d 20 70 53 75 62   jointype = pSub
2544e 69 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 3b 0a  item->jointype;.
2544f 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
25450 72 65 65 28 64 62 2c 20 70 53 75 62 69 74 65 6d  ree(db, pSubitem
25451 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20  ->zDatabase);.  
25452 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
25453 65 28 64 62 2c 20 70 53 75 62 69 74 65 6d 2d 3e  e(db, pSubitem->
25454 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 73 71  zName);.      sq
25455 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
25456 70 53 75 62 69 74 65 6d 2d 3e 7a 41 6c 69 61 73  pSubitem->zAlias
25457 29 3b 0a 20 20 20 20 20 20 70 53 75 62 69 74 65  );.      pSubite
25458 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d 20 30  m->zDatabase = 0
25459 3b 0a 20 20 20 20 20 20 70 53 75 62 69 74 65 6d  ;.      pSubitem
2545a 2d 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20  ->zName = 0;.   
2545b 20 20 20 70 53 75 62 69 74 65 6d 2d 3e 7a 41 6c     pSubitem->zAl
2545c 69 61 73 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20  ias = 0;..      
2545d 2f 2a 20 49 66 20 74 68 65 20 46 52 4f 4d 20 65  /* If the FROM e
2545e 6c 65 6d 65 6e 74 20 69 73 20 61 20 73 75 62 71  lement is a subq
2545f 75 65 72 79 2c 20 64 65 66 65 72 20 64 65 6c 65  uery, defer dele
25460 74 69 6e 67 20 74 68 65 20 54 61 62 6c 65 0a 20  ting the Table. 
25461 20 20 20 20 20 2a 2a 20 6f 62 6a 65 63 74 20 61       ** object a
25462 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
25463 68 61 74 20 73 75 62 71 75 65 72 79 20 75 6e 74  hat subquery unt
25464 69 6c 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69  il code generati
25465 6f 6e 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 63  on is.      ** c
25466 6f 6d 70 6c 65 74 65 2c 20 73 69 6e 63 65 20 74  omplete, since t
25467 68 65 72 65 20 6d 61 79 20 73 74 69 6c 6c 20 65  here may still e
25468 78 69 73 74 20 45 78 70 72 2e 70 54 61 62 20 65  xist Expr.pTab e
25469 6e 74 69 72 65 73 20 74 68 61 74 0a 20 20 20 20  ntires that.    
2546a 20 20 2a 2a 20 72 65 66 65 72 20 74 6f 20 74 68    ** refer to th
2546b 65 20 73 75 62 71 75 65 72 79 20 65 76 65 6e 20  e subquery even 
2546c 61 66 74 65 72 20 66 6c 61 74 74 65 6e 69 6e 67  after flattening
2546d 2e 20 20 54 69 63 6b 65 74 20 23 33 33 34 36 2e  .  Ticket #3346.
2546e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2546f 69 66 28 20 28 70 54 61 62 54 6f 44 65 6c 20 3d  if( (pTabToDel =
25470 20 70 53 75 62 69 74 65 6d 2d 3e 70 54 61 62 29   pSubitem->pTab)
25471 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69  !=0 ){.        i
25472 66 28 20 70 54 61 62 54 6f 44 65 6c 2d 3e 6e 52  f( pTabToDel->nR
25473 65 66 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20  ef==1 ){.       
25474 20 20 20 70 54 61 62 54 6f 44 65 6c 2d 3e 70 4e     pTabToDel->pN
25475 65 78 74 5a 6f 6d 62 69 65 20 3d 20 70 50 61 72  extZombie = pPar
25476 73 65 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 3b 0a  se->pZombieTab;.
25477 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65            pParse
25478 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 20 3d 20 70  ->pZombieTab = p
25479 54 61 62 54 6f 44 65 6c 3b 0a 20 20 20 20 20 20  TabToDel;.      
2547a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2547b 20 20 20 70 54 61 62 54 6f 44 65 6c 2d 3e 6e 52     pTabToDel->nR
2547c 65 66 2d 2d 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ef--;.        }.
2547d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 53        }.      pS
2547e 75 62 69 74 65 6d 2d 3e 70 54 61 62 20 3d 20 30  ubitem->pTab = 0
2547f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
25480 6e 53 75 62 53 72 63 21 3d 31 20 7c 7c 20 21 70  nSubSrc!=1 || !p
25481 53 72 63 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  Src ){.      int
25482 20 65 78 74 72 61 20 3d 20 6e 53 75 62 53 72 63   extra = nSubSrc
25483 20 2d 20 31 3b 0a 20 20 20 20 20 20 66 6f 72 28   - 1;.      for(
25484 69 3d 28 70 53 72 63 3f 31 3a 30 29 3b 20 69 3c  i=(pSrc?1:0); i<
25485 6e 53 75 62 53 72 63 3b 20 69 2b 2b 29 7b 0a 20  nSubSrc; i++){. 
25486 20 20 20 20 20 20 20 70 53 72 63 20 3d 20 73 71         pSrc = sq
25487 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65  lite3SrcListAppe
25488 6e 64 28 64 62 2c 20 70 53 72 63 2c 20 30 2c 20  nd(db, pSrc, 0, 
25489 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  0);.        if( 
2548a 70 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20  pSrc==0 ){.     
2548b 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 53       pParent->pS
2548c 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  rc = 0;.        
2548d 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
2548e 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
2548f 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 53 72      pParent->pSr
25490 63 20 3d 20 70 53 72 63 3b 0a 20 20 20 20 20 20  c = pSrc;.      
25491 66 6f 72 28 69 3d 70 53 72 63 2d 3e 6e 53 72 63  for(i=pSrc->nSrc
25492 2d 31 3b 20 69 2d 65 78 74 72 61 3e 3d 69 46 72  -1; i-extra>=iFr
25493 6f 6d 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 20 20  om; i--){.      
25494 20 20 70 53 72 63 2d 3e 61 5b 69 5d 20 3d 20 70    pSrc->a[i] = p
25495 53 72 63 2d 3e 61 5b 69 2d 65 78 74 72 61 5d 3b  Src->a[i-extra];
25496 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
25497 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 53     for(i=0; i<nS
25498 75 62 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20  ubSrc; i++){.   
25499 20 20 20 70 53 72 63 2d 3e 61 5b 69 2b 69 46 72     pSrc->a[i+iFr
2549a 6f 6d 5d 20 3d 20 70 53 75 62 53 72 63 2d 3e 61  om] = pSubSrc->a
2549b 5b 69 5d 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65  [i];.      memse
2549c 74 28 26 70 53 75 62 53 72 63 2d 3e 61 5b 69 5d  t(&pSubSrc->a[i]
2549d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 53 75 62  , 0, sizeof(pSub
2549e 53 72 63 2d 3e 61 5b 69 5d 29 29 3b 0a 20 20 20  Src->a[i]));.   
2549f 20 7d 0a 20 20 20 20 70 53 72 63 2d 3e 61 5b 69   }.    pSrc->a[i
254a0 46 72 6f 6d 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d  From].jointype =
254a1 20 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 0a 20 20   jointype;.  .  
254a2 20 20 2f 2a 20 4e 6f 77 20 62 65 67 69 6e 20 73    /* Now begin s
254a3 75 62 73 74 69 74 75 74 69 6e 67 20 73 75 62 71  ubstituting subq
254a4 75 65 72 79 20 72 65 73 75 6c 74 20 73 65 74 20  uery result set 
254a5 65 78 70 72 65 73 73 69 6f 6e 73 20 66 6f 72 20  expressions for 
254a6 0a 20 20 20 20 2a 2a 20 72 65 66 65 72 65 6e 63  .    ** referenc
254a7 65 73 20 74 6f 20 74 68 65 20 69 50 61 72 65 6e  es to the iParen
254a8 74 20 69 6e 20 74 68 65 20 6f 75 74 65 72 20 71  t in the outer q
254a9 75 65 72 79 2e 0a 20 20 20 20 2a 2a 20 0a 20 20  uery..    ** .  
254aa 20 20 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 20 20    ** Example:.  
254ab 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 53 45    **.    **   SE
254ac 4c 45 43 54 20 61 2b 35 2c 20 62 2a 31 30 20 46  LECT a+5, b*10 F
254ad 52 4f 4d 20 28 53 45 4c 45 43 54 20 78 2a 33 20  ROM (SELECT x*3 
254ae 41 53 20 61 2c 20 79 2b 31 30 20 41 53 20 62 20  AS a, y+10 AS b 
254af 46 52 4f 4d 20 74 31 29 20 57 48 45 52 45 20 61  FROM t1) WHERE a
254b0 3e 62 3b 0a 20 20 20 20 2a 2a 20 20 20 5c 20 20  >b;.    **   \  
254b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
254b2 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f     \____________
254b3 5f 20 73 75 62 71 75 65 72 79 20 5f 5f 5f 5f 5f  _ subquery _____
254b4 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20 20 20 20  _____/          
254b5 2f 0a 20 20 20 20 2a 2a 20 20 20 20 5c 5f 5f 5f  /.    **    \___
254b6 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
254b7 5f 5f 20 6f 75 74 65 72 20 71 75 65 72 79 20 5f  __ outer query _
254b8 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
254b9 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 20  _____________/. 
254ba 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 65 20     **.    ** We 
254bb 6c 6f 6f 6b 20 61 74 20 65 76 65 72 79 20 65 78  look at every ex
254bc 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20  pression in the 
254bd 6f 75 74 65 72 20 71 75 65 72 79 20 61 6e 64 20  outer query and 
254be 65 76 65 72 79 20 70 6c 61 63 65 20 77 65 20 73  every place we s
254bf 65 65 0a 20 20 20 20 2a 2a 20 22 61 22 20 77 65  ee.    ** "a" we
254c0 20 73 75 62 73 74 69 74 75 74 65 20 22 78 2a 33   substitute "x*3
254c1 22 20 61 6e 64 20 65 76 65 72 79 20 70 6c 61 63  " and every plac
254c2 65 20 77 65 20 73 65 65 20 22 62 22 20 77 65 20  e we see "b" we 
254c3 73 75 62 73 74 69 74 75 74 65 20 22 79 2b 31 30  substitute "y+10
254c4 22 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4c  "..    */.    pL
254c5 69 73 74 20 3d 20 70 50 61 72 65 6e 74 2d 3e 70  ist = pParent->p
254c6 45 4c 69 73 74 3b 0a 20 20 20 20 66 6f 72 28 69  EList;.    for(i
254c7 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78  =0; i<pList->nEx
254c8 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  pr; i++){.      
254c9 45 78 70 72 20 2a 70 45 78 70 72 3b 0a 20 20 20  Expr *pExpr;.   
254ca 20 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 61 5b     if( pList->a[
254cb 69 5d 2e 7a 4e 61 6d 65 3d 3d 30 20 26 26 20 28  i].zName==0 && (
254cc 70 45 78 70 72 20 3d 20 70 4c 69 73 74 2d 3e 61  pExpr = pList->a
254cd 5b 69 5d 2e 70 45 78 70 72 29 2d 3e 73 70 61 6e  [i].pExpr)->span
254ce 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  .z!=0 ){.       
254cf 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61   pList->a[i].zNa
254d0 6d 65 20 3d 20 0a 20 20 20 20 20 20 20 20 20 20  me = .          
254d1 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 53 74       sqlite3DbSt
254d2 72 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a  rNDup(db, (char*
254d3 29 70 45 78 70 72 2d 3e 73 70 61 6e 2e 7a 2c 20  )pExpr->span.z, 
254d4 70 45 78 70 72 2d 3e 73 70 61 6e 2e 6e 29 3b 0a  pExpr->span.n);.
254d5 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
254d6 20 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28    substExprList(
254d7 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 45 4c  db, pParent->pEL
254d8 69 73 74 2c 20 69 50 61 72 65 6e 74 2c 20 70 53  ist, iParent, pS
254d9 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20  ub->pEList);.   
254da 20 69 66 28 20 69 73 41 67 67 20 29 7b 0a 20 20   if( isAgg ){.  
254db 20 20 20 20 73 75 62 73 74 45 78 70 72 4c 69 73      substExprLis
254dc 74 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70  t(db, pParent->p
254dd 47 72 6f 75 70 42 79 2c 20 69 50 61 72 65 6e 74  GroupBy, iParent
254de 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b  , pSub->pEList);
254df 0a 20 20 20 20 20 20 73 75 62 73 74 45 78 70 72  .      substExpr
254e0 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 48  (db, pParent->pH
254e1 61 76 69 6e 67 2c 20 69 50 61 72 65 6e 74 2c 20  aving, iParent, 
254e2 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20  pSub->pEList);. 
254e3 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 53 75     }.    if( pSu
254e4 62 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  b->pOrderBy ){. 
254e5 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
254e6 72 65 6e 74 2d 3e 70 4f 72 64 65 72 42 79 3d 3d  rent->pOrderBy==
254e7 30 20 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65  0 );.      pPare
254e8 6e 74 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 70  nt->pOrderBy = p
254e9 53 75 62 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20  Sub->pOrderBy;. 
254ea 20 20 20 20 20 70 53 75 62 2d 3e 70 4f 72 64 65       pSub->pOrde
254eb 72 42 79 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  rBy = 0;.    }el
254ec 73 65 20 69 66 28 20 70 50 61 72 65 6e 74 2d 3e  se if( pParent->
254ed 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
254ee 20 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28    substExprList(
254ef 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 4f 72  db, pParent->pOr
254f0 64 65 72 42 79 2c 20 69 50 61 72 65 6e 74 2c 20  derBy, iParent, 
254f1 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20  pSub->pEList);. 
254f2 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 53 75     }.    if( pSu
254f3 62 2d 3e 70 57 68 65 72 65 20 29 7b 0a 20 20 20  b->pWhere ){.   
254f4 20 20 20 70 57 68 65 72 65 20 3d 20 73 71 6c 69     pWhere = sqli
254f5 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
254f6 53 75 62 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20  Sub->pWhere);.  
254f7 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
254f8 57 68 65 72 65 20 3d 20 30 3b 0a 20 20 20 20 7d  Where = 0;.    }
254f9 0a 20 20 20 20 69 66 28 20 73 75 62 71 75 65 72  .    if( subquer
254fa 79 49 73 41 67 67 20 29 7b 0a 20 20 20 20 20 20  yIsAgg ){.      
254fb 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d  assert( pParent-
254fc 3e 70 48 61 76 69 6e 67 3d 3d 30 20 29 3b 0a 20  >pHaving==0 );. 
254fd 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 48       pParent->pH
254fe 61 76 69 6e 67 20 3d 20 70 50 61 72 65 6e 74 2d  aving = pParent-
254ff 3e 70 57 68 65 72 65 3b 0a 20 20 20 20 20 20 70  >pWhere;.      p
25500 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 20 3d  Parent->pWhere =
25501 20 70 57 68 65 72 65 3b 0a 20 20 20 20 20 20 73   pWhere;.      s
25502 75 62 73 74 45 78 70 72 28 64 62 2c 20 70 50 61  ubstExpr(db, pPa
25503 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67 2c 20 69  rent->pHaving, i
25504 50 61 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45  Parent, pSub->pE
25505 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 70 50 61  List);.      pPa
25506 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67 20 3d 20  rent->pHaving = 
25507 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 64  sqlite3ExprAnd(d
25508 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 48 61 76  b, pParent->pHav
25509 69 6e 67 2c 20 0a 20 20 20 20 20 20 20 20 20 20  ing, .          
2550a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2550b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2550c 78 70 72 44 75 70 28 64 62 2c 20 70 53 75 62 2d  xprDup(db, pSub-
2550d 3e 70 48 61 76 69 6e 67 29 29 3b 0a 20 20 20 20  >pHaving));.    
2550e 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e    assert( pParen
2550f 74 2d 3e 70 47 72 6f 75 70 42 79 3d 3d 30 20 29  t->pGroupBy==0 )
25510 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d  ;.      pParent-
25511 3e 70 47 72 6f 75 70 42 79 20 3d 20 73 71 6c 69  >pGroupBy = sqli
25512 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64  te3ExprListDup(d
25513 62 2c 20 70 53 75 62 2d 3e 70 47 72 6f 75 70 42  b, pSub->pGroupB
25514 79 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  y);.    }else{. 
25515 20 20 20 20 20 73 75 62 73 74 45 78 70 72 28 64       substExpr(d
25516 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 57 68 65  b, pParent->pWhe
25517 72 65 2c 20 69 50 61 72 65 6e 74 2c 20 70 53 75  re, iParent, pSu
25518 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20  b->pEList);.    
25519 20 20 70 50 61 72 65 6e 74 2d 3e 70 57 68 65 72    pParent->pWher
2551a 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  e = sqlite3ExprA
2551b 6e 64 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e  nd(db, pParent->
2551c 70 57 68 65 72 65 2c 20 70 57 68 65 72 65 29 3b  pWhere, pWhere);
2551d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a  .    }.  .    /*
2551e 20 54 68 65 20 66 6c 61 74 74 65 6e 65 64 20 71   The flattened q
2551f 75 65 72 79 20 69 73 20 64 69 73 74 69 6e 63 74  uery is distinct
25520 20 69 66 20 65 69 74 68 65 72 20 74 68 65 20 69   if either the i
25521 6e 6e 65 72 20 6f 72 20 74 68 65 0a 20 20 20 20  nner or the.    
25522 2a 2a 20 6f 75 74 65 72 20 71 75 65 72 79 20 69  ** outer query i
25523 73 20 64 69 73 74 69 6e 63 74 2e 20 0a 20 20 20  s distinct. .   
25524 20 2a 2f 0a 20 20 20 20 70 50 61 72 65 6e 74 2d   */.    pParent-
25525 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 70 53 75  >selFlags |= pSu
25526 62 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46  b->selFlags & SF
25527 5f 44 69 73 74 69 6e 63 74 3b 0a 20 20 0a 20 20  _Distinct;.  .  
25528 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 53 45 4c 45    /*.    ** SELE
25529 43 54 20 2e 2e 2e 20 46 52 4f 4d 20 28 53 45 4c  CT ... FROM (SEL
2552a 45 43 54 20 2e 2e 2e 20 4c 49 4d 49 54 20 61 20  ECT ... LIMIT a 
2552b 4f 46 46 53 45 54 20 62 29 20 4c 49 4d 49 54 20  OFFSET b) LIMIT 
2552c 78 20 4f 46 46 53 45 54 20 79 3b 0a 20 20 20 20  x OFFSET y;.    
2552d 2a 2a 0a 20 20 20 20 2a 2a 20 4f 6e 65 20 69 73  **.    ** One is
2552e 20 74 65 6d 70 74 65 64 20 74 6f 20 74 72 79 20   tempted to try 
2552f 74 6f 20 61 64 64 20 61 20 61 6e 64 20 62 20 74  to add a and b t
25530 6f 20 63 6f 6d 62 69 6e 65 20 74 68 65 20 6c 69  o combine the li
25531 6d 69 74 73 2e 20 20 42 75 74 20 74 68 69 73 0a  mits.  But this.
25532 20 20 20 20 2a 2a 20 64 6f 65 73 20 6e 6f 74 20      ** does not 
25533 77 6f 72 6b 20 69 66 20 65 69 74 68 65 72 20 6c  work if either l
25534 69 6d 69 74 20 69 73 20 6e 65 67 61 74 69 76 65  imit is negative
25535 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
25536 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20 29 7b   pSub->pLimit ){
25537 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e  .      pParent->
25538 70 4c 69 6d 69 74 20 3d 20 70 53 75 62 2d 3e 70  pLimit = pSub->p
25539 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 53 75  Limit;.      pSu
2553a 62 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20  b->pLimit = 0;. 
2553b 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46     }.  }..  /* F
2553c 69 6e 69 61 6c 6c 79 2c 20 64 65 6c 65 74 65 20  inially, delete 
2553d 77 68 61 74 20 69 73 20 6c 65 66 74 20 6f 66 20  what is left of 
2553e 74 68 65 20 73 75 62 71 75 65 72 79 20 61 6e 64  the subquery and
2553f 20 72 65 74 75 72 6e 0a 20 20 2a 2a 20 73 75 63   return.  ** suc
25540 63 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  cess..  */.  sql
25541 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
25542 28 64 62 2c 20 70 53 75 62 31 29 3b 0a 0a 20 20  (db, pSub1);..  
25543 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64  return 1;.}.#end
25544 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53  if /* !defined(S
25545 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
25546 45 52 59 29 20 7c 7c 20 21 64 65 66 69 6e 65 64  ERY) || !defined
25547 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45  (SQLITE_OMIT_VIE
25548 57 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61  W) */../*.** Ana
25549 6c 79 7a 65 20 74 68 65 20 53 45 4c 45 43 54 20  lyze the SELECT 
2554a 73 74 61 74 65 6d 65 6e 74 20 70 61 73 73 65 64  statement passed
2554b 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20   as an argument 
2554c 74 6f 20 73 65 65 20 69 66 20 69 74 0a 2a 2a 20  to see if it.** 
2554d 69 73 20 61 20 6d 69 6e 28 29 20 6f 72 20 6d 61  is a min() or ma
2554e 78 28 29 20 71 75 65 72 79 2e 20 52 65 74 75 72  x() query. Retur
2554f 6e 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f  n WHERE_ORDERBY_
25550 4d 49 4e 20 6f 72 20 57 48 45 52 45 5f 4f 52 44  MIN or WHERE_ORD
25551 45 52 42 59 5f 4d 41 58 20 69 66 20 0a 2a 2a 20  ERBY_MAX if .** 
25552 69 74 20 69 73 2c 20 6f 72 20 30 20 6f 74 68 65  it is, or 0 othe
25553 72 77 69 73 65 2e 20 41 74 20 70 72 65 73 65 6e  rwise. At presen
25554 74 2c 20 61 20 71 75 65 72 79 20 69 73 20 63 6f  t, a query is co
25555 6e 73 69 64 65 72 65 64 20 74 6f 20 62 65 0a 2a  nsidered to be.*
25556 2a 20 61 20 6d 69 6e 28 29 2f 6d 61 78 28 29 20  * a min()/max() 
25557 71 75 65 72 79 20 69 66 3a 0a 2a 2a 0a 2a 2a 20  query if:.**.** 
25558 20 20 31 2e 20 54 68 65 72 65 20 69 73 20 61 20    1. There is a 
25559 73 69 6e 67 6c 65 20 6f 62 6a 65 63 74 20 69 6e  single object in
2555a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2555b 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 54 68 65  ..**.**   2. The
2555c 72 65 20 69 73 20 61 20 73 69 6e 67 6c 65 20 65  re is a single e
2555d 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65  xpression in the
2555e 20 72 65 73 75 6c 74 20 73 65 74 2c 20 61 6e 64   result set, and
2555f 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 20 20 65   it is.**      e
25560 69 74 68 65 72 20 6d 69 6e 28 78 29 20 6f 72 20  ither min(x) or 
25561 6d 61 78 28 78 29 2c 20 77 68 65 72 65 20 78 20  max(x), where x 
25562 69 73 20 61 20 63 6f 6c 75 6d 6e 20 72 65 66 65  is a column refe
25563 72 65 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  rence..*/.static
25564 20 69 6e 74 20 6d 69 6e 4d 61 78 51 75 65 72 79   int minMaxQuery
25565 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
25566 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 45 78  Select *p){.  Ex
25567 70 72 20 2a 70 45 78 70 72 3b 0a 20 20 45 78 70  pr *pExpr;.  Exp
25568 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 3d 20  rList *pEList = 
25569 70 2d 3e 70 45 4c 69 73 74 3b 0a 0a 20 20 69 66  p->pEList;..  if
2556a 28 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21  ( pEList->nExpr!
2556b 3d 31 20 29 20 72 65 74 75 72 6e 20 57 48 45 52  =1 ) return WHER
2556c 45 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c  E_ORDERBY_NORMAL
2556d 3b 0a 20 20 70 45 78 70 72 20 3d 20 70 45 4c 69  ;.  pExpr = pELi
2556e 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a  st->a[0].pExpr;.
2556f 20 20 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72    pEList = pExpr
25570 2d 3e 70 4c 69 73 74 3b 0a 20 20 69 66 28 20 70  ->pList;.  if( p
25571 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 47 47  Expr->op!=TK_AGG
25572 5f 46 55 4e 43 54 49 4f 4e 20 7c 7c 20 70 45 4c  _FUNCTION || pEL
25573 69 73 74 3d 3d 30 20 7c 7c 20 70 45 4c 69 73 74  ist==0 || pEList
25574 2d 3e 6e 45 78 70 72 21 3d 31 20 29 20 72 65 74  ->nExpr!=1 ) ret
25575 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 45 4c  urn 0;.  if( pEL
25576 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d  ist->a[0].pExpr-
25577 3e 6f 70 21 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55  >op!=TK_AGG_COLU
25578 4d 4e 20 29 20 72 65 74 75 72 6e 20 57 48 45 52  MN ) return WHER
25579 45 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c  E_ORDERBY_NORMAL
2557a 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 74  ;.  if( pExpr->t
2557b 6f 6b 65 6e 2e 6e 21 3d 33 20 29 20 72 65 74 75  oken.n!=3 ) retu
2557c 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59  rn WHERE_ORDERBY
2557d 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 69 66 28 20 73  _NORMAL;.  if( s
2557e 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 28  qlite3StrNICmp((
2557f 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b  char*)pExpr->tok
25580 65 6e 2e 7a 2c 22 6d 69 6e 22 2c 33 29 3d 3d 30  en.z,"min",3)==0
25581 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57   ){.    return W
25582 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e  HERE_ORDERBY_MIN
25583 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71  ;.  }else if( sq
25584 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 28 63  lite3StrNICmp((c
25585 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65  har*)pExpr->toke
25586 6e 2e 7a 2c 22 6d 61 78 22 2c 33 29 3d 3d 30 20  n.z,"max",3)==0 
25587 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 48  ){.    return WH
25588 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 41 58 3b  ERE_ORDERBY_MAX;
25589 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 57 48  .  }.  return WH
2558a 45 52 45 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d  ERE_ORDERBY_NORM
2558b 41 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  AL;.}../*.** If 
2558c 74 68 65 20 73 6f 75 72 63 65 2d 6c 69 73 74 20  the source-list 
2558d 69 74 65 6d 20 70 61 73 73 65 64 20 61 73 20 61  item passed as a
2558e 6e 20 61 72 67 75 6d 65 6e 74 20 77 61 73 20 61  n argument was a
2558f 75 67 6d 65 6e 74 65 64 20 77 69 74 68 20 61 6e  ugmented with an
25590 0a 2a 2a 20 49 4e 44 45 58 45 44 20 42 59 20 63  .** INDEXED BY c
25591 6c 61 75 73 65 2c 20 74 68 65 6e 20 74 72 79 20  lause, then try 
25592 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 73 70  to locate the sp
25593 65 63 69 66 69 65 64 20 69 6e 64 65 78 2e 20 49  ecified index. I
25594 66 20 74 68 65 72 65 0a 2a 2a 20 77 61 73 20 73  f there.** was s
25595 75 63 68 20 61 20 63 6c 61 75 73 65 20 61 6e 64  uch a clause and
25596 20 74 68 65 20 6e 61 6d 65 64 20 69 6e 64 65 78   the named index
25597 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64   cannot be found
25598 2c 20 72 65 74 75 72 6e 20 0a 2a 2a 20 53 51 4c  , return .** SQL
25599 49 54 45 5f 45 52 52 4f 52 20 61 6e 64 20 6c 65  ITE_ERROR and le
2559a 61 76 65 20 61 6e 20 65 72 72 6f 72 20 69 6e 20  ave an error in 
2559b 70 50 61 72 73 65 2e 20 4f 74 68 65 72 77 69 73  pParse. Otherwis
2559c 65 2c 20 70 6f 70 75 6c 61 74 65 20 0a 2a 2a 20  e, populate .** 
2559d 70 46 72 6f 6d 2d 3e 70 49 6e 64 65 78 20 61 6e  pFrom->pIndex an
2559e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  d return SQLITE_
2559f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  OK..*/.SQLITE_PR
255a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
255a1 33 49 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75 70  3IndexedByLookup
255a2 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
255a3 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
255a4 74 65 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69  tem *pFrom){.  i
255a5 66 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62 20 26  f( pFrom->pTab &
255a6 26 20 70 46 72 6f 6d 2d 3e 7a 49 6e 64 65 78 20  & pFrom->zIndex 
255a7 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  ){.    Table *pT
255a8 61 62 20 3d 20 70 46 72 6f 6d 2d 3e 70 54 61 62  ab = pFrom->pTab
255a9 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 49 6e 64  ;.    char *zInd
255aa 65 78 20 3d 20 70 46 72 6f 6d 2d 3e 7a 49 6e 64  ex = pFrom->zInd
255ab 65 78 3b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70  ex;.    Index *p
255ac 49 64 78 3b 0a 20 20 20 20 66 6f 72 28 70 49 64  Idx;.    for(pId
255ad 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
255ae 0a 20 20 20 20 20 20 20 20 70 49 64 78 20 26 26  .        pIdx &&
255af 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
255b0 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 7a 49 6e  pIdx->zName, zIn
255b1 64 65 78 29 3b 20 0a 20 20 20 20 20 20 20 20 70  dex); .        p
255b2 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 0a  Idx=pIdx->pNext.
255b3 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 21      );.    if( !
255b4 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20 73 71  pIdx ){.      sq
255b5 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
255b6 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 69  arse, "no such i
255b7 6e 64 65 78 3a 20 25 73 22 2c 20 7a 49 6e 64 65  ndex: %s", zInde
255b8 78 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 65 74  x, 0);.      ret
255b9 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
255ba 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 46 72 6f  ;.    }.    pFro
255bb 6d 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49 64 78  m->pIndex = pIdx
255bc 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
255bd 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
255be 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
255bf 69 73 20 61 20 57 61 6c 6b 65 72 20 63 61 6c 6c  is a Walker call
255c0 62 61 63 6b 20 66 6f 72 20 22 65 78 70 61 6e 64  back for "expand
255c1 69 6e 67 22 20 61 20 53 45 4c 45 43 54 20 73 74  ing" a SELECT st
255c2 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 22 45 78 70  atement..** "Exp
255c3 61 6e 64 69 6e 67 22 20 6d 65 61 6e 73 20 74 6f  anding" means to
255c4 20 64 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   do the followin
255c5 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20  g:.**.**    (1) 
255c6 20 4d 61 6b 65 20 73 75 72 65 20 56 44 42 45 20   Make sure VDBE 
255c7 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 68  cursor numbers h
255c8 61 76 65 20 62 65 65 6e 20 61 73 73 69 67 6e 65  ave been assigne
255c9 64 20 74 6f 20 65 76 65 72 79 0a 2a 2a 20 20 20  d to every.**   
255ca 20 20 20 20 20 20 65 6c 65 6d 65 6e 74 20 6f 66        element of
255cb 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
255cc 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 20  ..**.**    (2)  
255cd 46 69 6c 6c 20 69 6e 20 74 68 65 20 70 54 61 62  Fill in the pTab
255ce 4c 69 73 74 2d 3e 61 5b 5d 2e 70 54 61 62 20 66  List->a[].pTab f
255cf 69 65 6c 64 73 20 69 6e 20 74 68 65 20 53 72 63  ields in the Src
255d0 4c 69 73 74 20 74 68 61 74 20 0a 2a 2a 20 20 20  List that .**   
255d1 20 20 20 20 20 20 64 65 66 69 6e 65 73 20 46 52        defines FR
255d2 4f 4d 20 63 6c 61 75 73 65 2e 20 20 57 68 65 6e  OM clause.  When
255d3 20 76 69 65 77 73 20 61 70 70 65 61 72 20 69 6e   views appear in
255d4 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
255d5 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 66 69 6c  ,.**         fil
255d6 6c 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 5d 2e  l pTabList->a[].
255d7 70 53 65 6c 65 63 74 20 77 69 74 68 20 61 20 63  pSelect with a c
255d8 6f 70 79 20 6f 66 20 74 68 65 20 53 45 4c 45 43  opy of the SELEC
255d9 54 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 20  T statement.**  
255da 20 20 20 20 20 20 20 74 68 61 74 20 69 6d 70 6c         that impl
255db 65 6d 65 6e 74 73 20 74 68 65 20 76 69 65 77 2e  ements the view.
255dc 20 20 41 20 63 6f 70 79 20 69 73 20 6d 61 64 65    A copy is made
255dd 20 6f 66 20 74 68 65 20 76 69 65 77 27 73 20 53   of the view's S
255de 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20  ELECT.**        
255df 20 73 74 61 74 65 6d 65 6e 74 20 73 6f 20 74 68   statement so th
255e0 61 74 20 77 65 20 63 61 6e 20 66 72 65 65 6c 79  at we can freely
255e1 20 6d 6f 64 69 66 79 20 6f 72 20 64 65 6c 65 74   modify or delet
255e2 65 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74  e that statement
255e3 0a 2a 2a 20 20 20 20 20 20 20 20 20 77 69 74 68  .**         with
255e4 6f 75 74 20 77 6f 72 72 79 69 6e 67 20 61 62 6f  out worrying abo
255e5 75 74 20 6d 65 73 73 69 6e 67 20 75 70 20 74 68  ut messing up th
255e6 65 20 70 72 65 73 69 73 74 65 6e 74 20 72 65 70  e presistent rep
255e7 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 20  resentation.**  
255e8 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 76 69         of the vi
255e9 65 77 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 33 29  ew..**.**    (3)
255ea 20 20 41 64 64 20 74 65 72 6d 73 20 74 6f 20 74    Add terms to t
255eb 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
255ec 74 6f 20 61 63 63 6f 6d 6f 64 61 74 65 20 74 68  to accomodate th
255ed 65 20 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f 72  e NATURAL keywor
255ee 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 6e 20  d.**         on 
255ef 6a 6f 69 6e 73 20 61 6e 64 20 74 68 65 20 4f 4e  joins and the ON
255f0 20 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73   and USING claus
255f1 65 20 6f 66 20 6a 6f 69 6e 73 2e 0a 2a 2a 0a 2a  e of joins..**.*
255f2 2a 20 20 20 20 28 34 29 20 20 53 63 61 6e 20 74  *    (4)  Scan t
255f3 68 65 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d  he list of colum
255f4 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ns in the result
255f5 20 73 65 74 20 28 70 45 4c 69 73 74 29 20 6c 6f   set (pEList) lo
255f6 6f 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20  oking.**        
255f7 20 66 6f 72 20 69 6e 73 74 61 6e 63 65 73 20 6f   for instances o
255f8 66 20 74 68 65 20 22 2a 22 20 6f 70 65 72 61 74  f the "*" operat
255f9 6f 72 20 6f 72 20 74 68 65 20 54 41 42 4c 45 2e  or or the TABLE.
255fa 2a 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 20 20  * operator..**  
255fb 20 20 20 20 20 20 20 49 66 20 66 6f 75 6e 64 2c         If found,
255fc 20 65 78 70 61 6e 64 20 65 61 63 68 20 22 2a 22   expand each "*"
255fd 20 74 6f 20 62 65 20 65 76 65 72 79 20 63 6f 6c   to be every col
255fe 75 6d 6e 20 69 6e 20 65 76 65 72 79 20 74 61 62  umn in every tab
255ff 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 61 6e  le.**         an
25600 64 20 54 41 42 4c 45 2e 2a 20 74 6f 20 62 65 20  d TABLE.* to be 
25601 65 76 65 72 79 20 63 6f 6c 75 6d 6e 20 69 6e 20  every column in 
25602 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2f 0a 73 74 61  TABLE..**.*/.sta
25603 74 69 63 20 69 6e 74 20 73 65 6c 65 63 74 45 78  tic int selectEx
25604 70 61 6e 64 65 72 28 57 61 6c 6b 65 72 20 2a 70  pander(Walker *p
25605 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a  Walker, Select *
25606 70 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61  p){.  Parse *pPa
25607 72 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70  rse = pWalker->p
25608 50 61 72 73 65 3b 0a 20 20 69 6e 74 20 69 2c 20  Parse;.  int i, 
25609 6a 2c 20 6b 3b 0a 20 20 53 72 63 4c 69 73 74 20  j, k;.  SrcList 
2560a 2a 70 54 61 62 4c 69 73 74 3b 0a 20 20 45 78 70  *pTabList;.  Exp
2560b 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20  rList *pEList;. 
2560c 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
2560d 69 74 65 6d 20 2a 70 46 72 6f 6d 3b 0a 20 20 73  item *pFrom;.  s
2560e 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2560f 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20  rse->db;..  if( 
25610 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
25611 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20    ){.    return 
25612 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 20  WRC_Abort;.  }. 
25613 20 69 66 28 20 70 2d 3e 70 53 72 63 3d 3d 30 20   if( p->pSrc==0 
25614 7c 7c 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20  || (p->selFlags 
25615 26 20 53 46 5f 45 78 70 61 6e 64 65 64 29 21 3d  & SF_Expanded)!=
25616 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
25617 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 20  WRC_Prune;.  }. 
25618 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20   p->selFlags |= 
25619 53 46 5f 45 78 70 61 6e 64 65 64 3b 0a 20 20 70  SF_Expanded;.  p
2561a 54 61 62 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72  TabList = p->pSr
2561b 63 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 2d  c;.  pEList = p-
2561c 3e 70 45 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20 4d  >pEList;..  /* M
2561d 61 6b 65 20 73 75 72 65 20 63 75 72 73 6f 72 20  ake sure cursor 
2561e 6e 75 6d 62 65 72 73 20 68 61 76 65 20 62 65 65  numbers have bee
2561f 6e 20 61 73 73 69 67 6e 65 64 20 74 6f 20 61 6c  n assigned to al
25620 6c 20 65 6e 74 72 69 65 73 20 69 6e 0a 20 20 2a  l entries in.  *
25621 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  * the FROM claus
25622 65 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20  e of the SELECT 
25623 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a  statement..  */.
25624 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
25625 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28 70 50  AssignCursors(pP
25626 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 29 3b  arse, pTabList);
25627 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b 20 75 70 20 65  ..  /* Look up e
25628 76 65 72 79 20 74 61 62 6c 65 20 6e 61 6d 65 64  very table named
25629 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
2562a 75 73 65 20 6f 66 20 74 68 65 20 73 65 6c 65 63  use of the selec
2562b 74 2e 20 20 49 66 0a 20 20 2a 2a 20 61 6e 20 65  t.  If.  ** an e
2562c 6e 74 72 79 20 6f 66 20 74 68 65 20 46 52 4f 4d  ntry of the FROM
2562d 20 63 6c 61 75 73 65 20 69 73 20 61 20 73 75 62   clause is a sub
2562e 71 75 65 72 79 20 69 6e 73 74 65 61 64 20 6f 66  query instead of
2562f 20 61 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77   a table or view
25630 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 63 72 65 61  ,.  ** then crea
25631 74 65 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74  te a transient t
25632 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 74  able structure t
25633 6f 20 64 65 73 63 72 69 62 65 20 74 68 65 20 73  o describe the s
25634 75 62 71 75 65 72 79 2e 0a 20 20 2a 2f 0a 20 20  ubquery..  */.  
25635 66 6f 72 28 69 3d 30 2c 20 70 46 72 6f 6d 3d 70  for(i=0, pFrom=p
25636 54 61 62 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 54  TabList->a; i<pT
25637 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b  abList->nSrc; i+
25638 2b 2c 20 70 46 72 6f 6d 2b 2b 29 7b 0a 20 20 20  +, pFrom++){.   
25639 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
2563a 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e 70 54 61    if( pFrom->pTa
2563b 62 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  b!=0 ){.      /*
2563c 20 54 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20   This statement 
2563d 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
2563e 20 70 72 65 70 61 72 65 64 2e 20 20 54 68 65 72   prepared.  Ther
2563f 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 20  e is no need.   
25640 20 20 20 2a 2a 20 74 6f 20 67 6f 20 66 75 72 74     ** to go furt
25641 68 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73  her. */.      as
25642 73 65 72 74 28 20 69 3d 3d 30 20 29 3b 0a 20 20  sert( i==0 );.  
25643 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50      return WRC_P
25644 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rune;.    }.    
25645 69 66 28 20 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65  if( pFrom->zName
25646 3d 3d 30 20 29 7b 0a 23 69 66 6e 64 65 66 20 53  ==0 ){.#ifndef S
25647 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
25648 45 52 59 0a 20 20 20 20 20 20 53 65 6c 65 63 74  ERY.      Select
25649 20 2a 70 53 65 6c 20 3d 20 70 46 72 6f 6d 2d 3e   *pSel = pFrom->
2564a 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 20 20 2f  pSelect;.      /
2564b 2a 20 41 20 73 75 62 2d 71 75 65 72 79 20 69 6e  * A sub-query in
2564c 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2564d 20 6f 66 20 61 20 53 45 4c 45 43 54 20 2a 2f 0a   of a SELECT */.
2564e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53        assert( pS
2564f 65 6c 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61  el!=0 );.      a
25650 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 70 54  ssert( pFrom->pT
25651 61 62 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73  ab==0 );.      s
25652 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74  qlite3WalkSelect
25653 28 70 57 61 6c 6b 65 72 2c 20 70 53 65 6c 29 3b  (pWalker, pSel);
25654 0a 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e 70 54  .      pFrom->pT
25655 61 62 20 3d 20 70 54 61 62 20 3d 20 73 71 6c 69  ab = pTab = sqli
25656 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
25657 64 62 2c 20 73 69 7a 65 6f 66 28 54 61 62 6c 65  db, sizeof(Table
25658 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54  ));.      if( pT
25659 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57  ab==0 ) return W
2565a 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20  RC_Abort;.      
2565b 70 54 61 62 2d 3e 64 62 20 3d 20 64 62 3b 0a 20  pTab->db = db;. 
2565c 20 20 20 20 20 70 54 61 62 2d 3e 6e 52 65 66 20       pTab->nRef 
2565d 3d 20 31 3b 0a 20 20 20 20 20 20 70 54 61 62 2d  = 1;.      pTab-
2565e 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  >zName = sqlite3
2565f 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 73 71 6c  MPrintf(db, "sql
25660 69 74 65 5f 73 75 62 71 75 65 72 79 5f 25 70 5f  ite_subquery_%p_
25661 22 2c 20 28 76 6f 69 64 2a 29 70 54 61 62 29 3b  ", (void*)pTab);
25662 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 53  .      while( pS
25663 65 6c 2d 3e 70 50 72 69 6f 72 20 29 7b 20 70 53  el->pPrior ){ pS
25664 65 6c 20 3d 20 70 53 65 6c 2d 3e 70 50 72 69 6f  el = pSel->pPrio
25665 72 3b 20 7d 0a 20 20 20 20 20 20 73 65 6c 65 63  r; }.      selec
25666 74 43 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78 70 72  tColumnsFromExpr
25667 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 53 65  List(pParse, pSe
25668 6c 2d 3e 70 45 4c 69 73 74 2c 20 26 70 54 61 62  l->pEList, &pTab
25669 2d 3e 6e 43 6f 6c 2c 20 26 70 54 61 62 2d 3e 61  ->nCol, &pTab->a
2566a 43 6f 6c 29 3b 0a 20 20 20 20 20 20 70 54 61 62  Col);.      pTab
2566b 2d 3e 69 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20  ->iPKey = -1;.  
2566c 20 20 20 20 70 54 61 62 2d 3e 74 61 62 46 6c 61      pTab->tabFla
2566d 67 73 20 7c 3d 20 54 46 5f 45 70 68 65 6d 65 72  gs |= TF_Ephemer
2566e 61 6c 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d  al;.#endif.    }
2566f 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41  else{.      /* A
25670 6e 20 6f 72 64 69 6e 61 72 79 20 74 61 62 6c 65  n ordinary table
25671 20 6f 72 20 76 69 65 77 20 6e 61 6d 65 20 69 6e   or view name in
25672 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
25673 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
25674 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3d 3d 30  ( pFrom->pTab==0
25675 20 29 3b 0a 20 20 20 20 20 20 70 46 72 6f 6d 2d   );.      pFrom-
25676 3e 70 54 61 62 20 3d 20 70 54 61 62 20 3d 20 0a  >pTab = pTab = .
25677 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4c          sqlite3L
25678 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73  ocateTable(pPars
25679 65 2c 30 2c 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65  e,0,pFrom->zName
2567a 2c 70 46 72 6f 6d 2d 3e 7a 44 61 74 61 62 61 73  ,pFrom->zDatabas
2567b 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54  e);.      if( pT
2567c 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57  ab==0 ) return W
2567d 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20  RC_Abort;.      
2567e 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 23 69  pTab->nRef++;.#i
2567f 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
25680 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20  E_OMIT_VIEW) || 
25681 21 64 65 66 69 6e 65 64 20 28 53 51 4c 49 54 45  !defined (SQLITE
25682 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
25683 4c 45 29 0a 20 20 20 20 20 20 69 66 28 20 70 54  LE).      if( pT
25684 61 62 2d 3e 70 53 65 6c 65 63 74 20 7c 7c 20 49  ab->pSelect || I
25685 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
25686 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20  {.        /* We 
25687 72 65 61 63 68 20 68 65 72 65 20 69 66 20 74 68  reach here if th
25688 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 20 69 73  e named table is
25689 20 61 20 72 65 61 6c 6c 79 20 61 20 76 69 65 77   a really a view
2568a 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
2568b 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f  sqlite3ViewGetCo
2568c 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65  lumnNames(pParse
2568d 2c 20 70 54 61 62 29 20 29 20 72 65 74 75 72 6e  , pTab) ) return
2568e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 0a 20 20 20   WRC_Abort;..   
2568f 20 20 20 20 20 2f 2a 20 49 66 20 70 46 72 6f 6d       /* If pFrom
25690 2d 3e 70 53 65 6c 65 63 74 21 3d 30 20 69 74 20  ->pSelect!=0 it 
25691 6d 65 61 6e 73 20 77 65 20 61 72 65 20 64 65 61  means we are dea
25692 6c 69 6e 67 20 77 69 74 68 20 61 0a 20 20 20 20  ling with a.    
25693 20 20 20 20 2a 2a 20 76 69 65 77 20 77 69 74 68      ** view with
25694 69 6e 20 61 20 76 69 65 77 2e 20 20 54 68 65 20  in a view.  The 
25695 53 45 4c 45 43 54 20 73 74 72 75 63 74 75 72 65  SELECT structure
25696 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
25697 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 70  n.        ** cop
25698 69 65 64 20 62 79 20 74 68 65 20 6f 75 74 65 72  ied by the outer
25699 20 76 69 65 77 20 73 6f 20 77 65 20 63 61 6e 20   view so we can 
2569a 73 6b 69 70 20 74 68 65 20 63 6f 70 79 20 73 74  skip the copy st
2569b 65 70 20 68 65 72 65 0a 20 20 20 20 20 20 20 20  ep here.        
2569c 2a 2a 20 69 6e 20 74 68 65 20 69 6e 6e 65 72 20  ** in the inner 
2569d 76 69 65 77 2e 0a 20 20 20 20 20 20 20 20 2a 2f  view..        */
2569e 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 46 72  .        if( pFr
2569f 6f 6d 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29  om->pSelect==0 )
256a0 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 72 6f  {.          pFro
256a1 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c  m->pSelect = sql
256a2 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62  ite3SelectDup(db
256a3 2c 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 29  , pTab->pSelect)
256a4 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
256a5 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57  te3WalkSelect(pW
256a6 61 6c 6b 65 72 2c 20 70 46 72 6f 6d 2d 3e 70 53  alker, pFrom->pS
256a7 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20  elect);.        
256a8 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  }.      }.#endif
256a9 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4c  .    }..    /* L
256aa 6f 63 61 74 65 20 74 68 65 20 69 6e 64 65 78 20  ocate the index 
256ab 6e 61 6d 65 64 20 62 79 20 74 68 65 20 49 4e 44  named by the IND
256ac 45 58 45 44 20 42 59 20 63 6c 61 75 73 65 2c 20  EXED BY clause, 
256ad 69 66 20 61 6e 79 2e 20 2a 2f 0a 20 20 20 20 69  if any. */.    i
256ae 66 28 20 73 71 6c 69 74 65 33 49 6e 64 65 78 65  f( sqlite3Indexe
256af 64 42 79 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65  dByLookup(pParse
256b0 2c 20 70 46 72 6f 6d 29 20 29 7b 0a 20 20 20 20  , pFrom) ){.    
256b1 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f    return WRC_Abo
256b2 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  rt;.    }.  }.. 
256b3 20 2f 2a 20 50 72 6f 63 65 73 73 20 4e 41 54 55   /* Process NATU
256b4 52 41 4c 20 6b 65 79 77 6f 72 64 73 2c 20 61 6e  RAL keywords, an
256b5 64 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 63  d ON and USING c
256b6 6c 61 75 73 65 73 20 6f 66 20 6a 6f 69 6e 73 2e  lauses of joins.
256b7 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  .  */.  if( db->
256b8 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20  mallocFailed || 
256b9 73 71 6c 69 74 65 50 72 6f 63 65 73 73 4a 6f 69  sqliteProcessJoi
256ba 6e 28 70 50 61 72 73 65 2c 20 70 29 20 29 7b 0a  n(pParse, p) ){.
256bb 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41      return WRC_A
256bc 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  bort;.  }..  /* 
256bd 46 6f 72 20 65 76 65 72 79 20 22 2a 22 20 74 68  For every "*" th
256be 61 74 20 6f 63 63 75 72 73 20 69 6e 20 74 68 65  at occurs in the
256bf 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 2c 20 69 6e   column list, in
256c0 73 65 72 74 20 74 68 65 20 6e 61 6d 65 73 20 6f  sert the names o
256c1 66 0a 20 20 2a 2a 20 61 6c 6c 20 63 6f 6c 75 6d  f.  ** all colum
256c2 6e 73 20 69 6e 20 61 6c 6c 20 74 61 62 6c 65 73  ns in all tables
256c3 2e 20 20 41 6e 64 20 66 6f 72 20 65 76 65 72 79  .  And for every
256c4 20 54 41 42 4c 45 2e 2a 20 69 6e 73 65 72 74 20   TABLE.* insert 
256c5 74 68 65 20 6e 61 6d 65 73 0a 20 20 2a 2a 20 6f  the names.  ** o
256c6 66 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 69 6e  f all columns in
256c7 20 54 41 42 4c 45 2e 20 20 54 68 65 20 70 61 72   TABLE.  The par
256c8 73 65 72 20 69 6e 73 65 72 74 65 64 20 61 20 73  ser inserted a s
256c9 70 65 63 69 61 6c 20 65 78 70 72 65 73 73 69 6f  pecial expressio
256ca 6e 0a 20 20 2a 2a 20 77 69 74 68 20 74 68 65 20  n.  ** with the 
256cb 54 4b 5f 41 4c 4c 20 6f 70 65 72 61 74 6f 72 20  TK_ALL operator 
256cc 66 6f 72 20 65 61 63 68 20 22 2a 22 20 74 68 61  for each "*" tha
256cd 74 20 69 74 20 66 6f 75 6e 64 20 69 6e 20 74 68  t it found in th
256ce 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 2e 0a 20  e column list.. 
256cf 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   ** The followin
256d0 67 20 63 6f 64 65 20 6a 75 73 74 20 68 61 73 20  g code just has 
256d1 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 54 4b  to locate the TK
256d2 5f 41 4c 4c 20 65 78 70 72 65 73 73 69 6f 6e 73  _ALL expressions
256d3 20 61 6e 64 20 65 78 70 61 6e 64 0a 20 20 2a 2a   and expand.  **
256d4 20 65 61 63 68 20 6f 6e 65 20 74 6f 20 74 68 65   each one to the
256d5 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63 6f 6c   list of all col
256d6 75 6d 6e 73 20 69 6e 20 61 6c 6c 20 74 61 62 6c  umns in all tabl
256d7 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  es..  **.  ** Th
256d8 65 20 66 69 72 73 74 20 6c 6f 6f 70 20 6a 75 73  e first loop jus
256d9 74 20 63 68 65 63 6b 73 20 74 6f 20 73 65 65 20  t checks to see 
256da 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79  if there are any
256db 20 22 2a 22 20 6f 70 65 72 61 74 6f 72 73 0a 20   "*" operators. 
256dc 20 2a 2a 20 74 68 61 74 20 6e 65 65 64 20 65 78   ** that need ex
256dd 70 61 6e 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20  panding..  */.  
256de 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 45 4c 69 73  for(k=0; k<pELis
256df 74 2d 3e 6e 45 78 70 72 3b 20 6b 2b 2b 29 7b 0a  t->nExpr; k++){.
256e0 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70      Expr *pE = p
256e1 45 4c 69 73 74 2d 3e 61 5b 6b 5d 2e 70 45 78 70  EList->a[k].pExp
256e2 72 3b 0a 20 20 20 20 69 66 28 20 70 45 2d 3e 6f  r;.    if( pE->o
256e3 70 3d 3d 54 4b 5f 41 4c 4c 20 29 20 62 72 65 61  p==TK_ALL ) brea
256e4 6b 3b 0a 20 20 20 20 69 66 28 20 70 45 2d 3e 6f  k;.    if( pE->o
256e5 70 3d 3d 54 4b 5f 44 4f 54 20 26 26 20 70 45 2d  p==TK_DOT && pE-
256e6 3e 70 52 69 67 68 74 20 26 26 20 70 45 2d 3e 70  >pRight && pE->p
256e7 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 41 4c  Right->op==TK_AL
256e8 4c 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 45  L.         && pE
256e9 2d 3e 70 4c 65 66 74 20 26 26 20 70 45 2d 3e 70  ->pLeft && pE->p
256ea 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20  Left->op==TK_ID 
256eb 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69  ) break;.  }.  i
256ec 66 28 20 6b 3c 70 45 4c 69 73 74 2d 3e 6e 45 78  f( k<pEList->nEx
256ed 70 72 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20  pr ){.    /*.   
256ee 20 2a 2a 20 49 66 20 77 65 20 67 65 74 20 68 65   ** If we get he
256ef 72 65 20 69 74 20 6d 65 61 6e 73 20 74 68 65 20  re it means the 
256f0 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6e 74 61  result set conta
256f1 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  ins one or more 
256f2 22 2a 22 0a 20 20 20 20 2a 2a 20 6f 70 65 72 61  "*".    ** opera
256f3 74 6f 72 73 20 74 68 61 74 20 6e 65 65 64 20 74  tors that need t
256f4 6f 20 62 65 20 65 78 70 61 6e 64 65 64 2e 20 20  o be expanded.  
256f5 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 65 61 63  Loop through eac
256f6 68 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 20  h expression.   
256f7 20 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c   ** in the resul
256f8 74 20 73 65 74 20 61 6e 64 20 65 78 70 61 6e 64  t set and expand
256f9 20 74 68 65 6d 20 6f 6e 65 20 62 79 20 6f 6e 65   them one by one
256fa 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 74 72  ..    */.    str
256fb 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
256fc 6d 20 2a 61 20 3d 20 70 45 4c 69 73 74 2d 3e 61  m *a = pEList->a
256fd 3b 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a  ;.    ExprList *
256fe 70 4e 65 77 20 3d 20 30 3b 0a 20 20 20 20 69 6e  pNew = 0;.    in
256ff 74 20 66 6c 61 67 73 20 3d 20 70 50 61 72 73 65  t flags = pParse
25700 2d 3e 64 62 2d 3e 66 6c 61 67 73 3b 0a 20 20 20  ->db->flags;.   
25701 20 69 6e 74 20 6c 6f 6e 67 4e 61 6d 65 73 20 3d   int longNames =
25702 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45   (flags & SQLITE
25703 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 29 21 3d  _FullColNames)!=
25704 30 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0.              
25705 20 20 20 20 20 20 20 20 26 26 20 28 66 6c 61 67          && (flag
25706 73 20 26 20 53 51 4c 49 54 45 5f 53 68 6f 72 74  s & SQLITE_Short
25707 43 6f 6c 4e 61 6d 65 73 29 3d 3d 30 3b 0a 0a 20  ColNames)==0;.. 
25708 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 45     for(k=0; k<pE
25709 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 6b 2b 2b  List->nExpr; k++
2570a 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  ){.      Expr *p
2570b 45 20 3d 20 61 5b 6b 5d 2e 70 45 78 70 72 3b 0a  E = a[k].pExpr;.
2570c 20 20 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70        if( pE->op
2570d 21 3d 54 4b 5f 41 4c 4c 20 26 26 0a 20 20 20 20  !=TK_ALL &&.    
2570e 20 20 20 20 20 20 20 28 70 45 2d 3e 6f 70 21 3d         (pE->op!=
2570f 54 4b 5f 44 4f 54 20 7c 7c 20 70 45 2d 3e 70 52  TK_DOT || pE->pR
25710 69 67 68 74 3d 3d 30 20 7c 7c 20 70 45 2d 3e 70  ight==0 || pE->p
25711 52 69 67 68 74 2d 3e 6f 70 21 3d 54 4b 5f 41 4c  Right->op!=TK_AL
25712 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  L) ){.        /*
25713 20 54 68 69 73 20 70 61 72 74 69 63 75 6c 61 72   This particular
25714 20 65 78 70 72 65 73 73 69 6f 6e 20 64 6f 65 73   expression does
25715 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20   not need to be 
25716 65 78 70 61 6e 64 65 64 2e 0a 20 20 20 20 20 20  expanded..      
25717 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4e 65    */.        pNe
25718 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  w = sqlite3ExprL
25719 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
2571a 2c 20 70 4e 65 77 2c 20 61 5b 6b 5d 2e 70 45 78  , pNew, a[k].pEx
2571b 70 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  pr, 0);.        
2571c 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20  if( pNew ){.    
2571d 20 20 20 20 20 20 70 4e 65 77 2d 3e 61 5b 70 4e        pNew->a[pN
2571e 65 77 2d 3e 6e 45 78 70 72 2d 31 5d 2e 7a 4e 61  ew->nExpr-1].zNa
2571f 6d 65 20 3d 20 61 5b 6b 5d 2e 7a 4e 61 6d 65 3b  me = a[k].zName;
25720 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
25721 20 20 20 61 5b 6b 5d 2e 70 45 78 70 72 20 3d 20     a[k].pExpr = 
25722 30 3b 0a 20 20 20 20 20 20 20 20 61 5b 6b 5d 2e  0;.        a[k].
25723 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20 20  zName = 0;.     
25724 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
25725 2f 2a 20 54 68 69 73 20 65 78 70 72 65 73 73 69  /* This expressi
25726 6f 6e 20 69 73 20 61 20 22 2a 22 20 6f 72 20 61  on is a "*" or a
25727 20 22 54 41 42 4c 45 2e 2a 22 20 61 6e 64 20 6e   "TABLE.*" and n
25728 65 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20 20  eeds to be.     
25729 20 20 20 2a 2a 20 65 78 70 61 6e 64 65 64 2e 20     ** expanded. 
2572a 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 74  */.        int t
2572b 61 62 6c 65 53 65 65 6e 20 3d 20 30 3b 20 20 20  ableSeen = 0;   
2572c 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 31 20 77     /* Set to 1 w
2572d 68 65 6e 20 54 41 42 4c 45 20 6d 61 74 63 68 65  hen TABLE matche
2572e 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 68 61  s */.        cha
2572f 72 20 2a 7a 54 4e 61 6d 65 3b 20 20 20 20 20 20  r *zTName;      
25730 20 20 20 20 20 20 2f 2a 20 74 65 78 74 20 6f 66        /* text of
25731 20 6e 61 6d 65 20 6f 66 20 54 41 42 4c 45 20 2a   name of TABLE *
25732 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 45  /.        if( pE
25733 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 26 26 20  ->op==TK_DOT && 
25734 70 45 2d 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20  pE->pLeft ){.   
25735 20 20 20 20 20 20 20 7a 54 4e 61 6d 65 20 3d 20         zTName = 
25736 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
25737 6f 6b 65 6e 28 64 62 2c 20 26 70 45 2d 3e 70 4c  oken(db, &pE->pL
25738 65 66 74 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 20  eft->token);.   
25739 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2573a 20 20 20 20 20 20 7a 54 4e 61 6d 65 20 3d 20 30        zTName = 0
2573b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2573c 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 46 72      for(i=0, pFr
2573d 6f 6d 3d 70 54 61 62 4c 69 73 74 2d 3e 61 3b 20  om=pTabList->a; 
2573e 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  i<pTabList->nSrc
2573f 3b 20 69 2b 2b 2c 20 70 46 72 6f 6d 2b 2b 29 7b  ; i++, pFrom++){
25740 0a 20 20 20 20 20 20 20 20 20 20 54 61 62 6c 65  .          Table
25741 20 2a 70 54 61 62 20 3d 20 70 46 72 6f 6d 2d 3e   *pTab = pFrom->
25742 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20  pTab;.          
25743 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d  char *zTabName =
25744 20 70 46 72 6f 6d 2d 3e 7a 41 6c 69 61 73 3b 0a   pFrom->zAlias;.
25745 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 54            if( zT
25746 61 62 4e 61 6d 65 3d 3d 30 20 7c 7c 20 7a 54 61  abName==0 || zTa
25747 62 4e 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 20 0a  bName[0]==0 ){ .
25748 20 20 20 20 20 20 20 20 20 20 20 20 7a 54 61 62              zTab
25749 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61  Name = pTab->zNa
2574a 6d 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  me;.          }.
2574b 20 20 20 20 20 20 20 20 20 20 69 66 28 20 64 62            if( db
2574c 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2574d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
2574e 20 20 69 66 28 20 7a 54 4e 61 6d 65 20 26 26 20    if( zTName && 
2574f 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
25750 54 4e 61 6d 65 2c 20 7a 54 61 62 4e 61 6d 65 29  TName, zTabName)
25751 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  !=0 ){.         
25752 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
25753 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
25754 20 20 20 74 61 62 6c 65 53 65 65 6e 20 3d 20 31     tableSeen = 1
25755 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28  ;.          for(
25756 6a 3d 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f  j=0; j<pTab->nCo
25757 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  l; j++){.       
25758 20 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72       Expr *pExpr
25759 2c 20 2a 70 52 69 67 68 74 3b 0a 20 20 20 20 20  , *pRight;.     
2575a 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61         char *zNa
2575b 6d 65 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  me = pTab->aCol[
2575c 6a 5d 2e 7a 4e 61 6d 65 3b 0a 0a 20 20 20 20 20  j].zName;..     
2575d 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 20 63         /* If a c
2575e 6f 6c 75 6d 6e 20 69 73 20 6d 61 72 6b 65 64 20  olumn is marked 
2575f 61 73 20 27 68 69 64 64 65 6e 27 20 28 63 75 72  as 'hidden' (cur
25760 72 65 6e 74 6c 79 20 6f 6e 6c 79 20 70 6f 73 73  rently only poss
25761 69 62 6c 65 0a 20 20 20 20 20 20 20 20 20 20 20  ible.           
25762 20 2a 2a 20 66 6f 72 20 76 69 72 74 75 61 6c 20   ** for virtual 
25763 74 61 62 6c 65 73 29 2c 20 64 6f 20 6e 6f 74 20  tables), do not 
25764 69 6e 63 6c 75 64 65 20 69 74 20 69 6e 20 74 68  include it in th
25765 65 20 65 78 70 61 6e 64 65 64 0a 20 20 20 20 20  e expanded.     
25766 20 20 20 20 20 20 20 2a 2a 20 72 65 73 75 6c 74         ** result
25767 2d 73 65 74 20 6c 69 73 74 2e 0a 20 20 20 20 20  -set list..     
25768 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
25769 20 20 20 20 20 20 69 66 28 20 49 73 48 69 64 64        if( IsHidd
2576a 65 6e 43 6f 6c 75 6d 6e 28 26 70 54 61 62 2d 3e  enColumn(&pTab->
2576b 61 43 6f 6c 5b 6a 5d 29 20 29 7b 0a 20 20 20 20  aCol[j]) ){.    
2576c 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
2576d 28 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29  (IsVirtual(pTab)
2576e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
2576f 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
25770 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
25771 20 20 20 20 20 20 69 66 28 20 69 3e 30 20 29 7b        if( i>0 ){
25772 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  .              s
25773 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
25774 65 6d 20 2a 70 4c 65 66 74 20 3d 20 26 70 54 61  em *pLeft = &pTa
25775 62 4c 69 73 74 2d 3e 61 5b 69 2d 31 5d 3b 0a 20  bList->a[i-1];. 
25776 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
25777 20 28 70 4c 65 66 74 5b 31 5d 2e 6a 6f 69 6e 74   (pLeft[1].joint
25778 79 70 65 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c  ype & JT_NATURAL
25779 29 21 3d 30 20 26 26 0a 20 20 20 20 20 20 20 20  )!=0 &&.        
2577a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2577b 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 70 4c 65 66  columnIndex(pLef
2577c 74 2d 3e 70 54 61 62 2c 20 7a 4e 61 6d 65 29 3e  t->pTab, zName)>
2577d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
2577e 20 20 20 20 20 20 2f 2a 20 49 6e 20 61 20 4e 41        /* In a NA
2577f 54 55 52 41 4c 20 6a 6f 69 6e 2c 20 6f 6d 69 74  TURAL join, omit
25780 20 74 68 65 20 6a 6f 69 6e 20 63 6f 6c 75 6d 6e   the join column
25781 73 20 66 72 6f 6d 20 74 68 65 20 0a 20 20 20 20  s from the .    
25782 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74              ** t
25783 61 62 6c 65 20 6f 6e 20 74 68 65 20 72 69 67 68  able on the righ
25784 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  t */.           
25785 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
25786 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
25787 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
25788 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 49 6e   sqlite3IdListIn
25789 64 65 78 28 70 4c 65 66 74 5b 31 5d 2e 70 55 73  dex(pLeft[1].pUs
2578a 69 6e 67 2c 20 7a 4e 61 6d 65 29 3e 3d 30 20 29  ing, zName)>=0 )
2578b 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
2578c 20 20 2f 2a 20 49 6e 20 61 20 6a 6f 69 6e 20 77    /* In a join w
2578d 69 74 68 20 61 20 55 53 49 4e 47 20 63 6c 61 75  ith a USING clau
2578e 73 65 2c 20 6f 6d 69 74 20 63 6f 6c 75 6d 6e 73  se, omit columns
2578f 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20   in the.        
25790 20 20 20 20 20 20 20 20 2a 2a 20 75 73 69 6e 67          ** using
25791 20 63 6c 61 75 73 65 20 66 72 6f 6d 20 74 68 65   clause from the
25792 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 20 72 69   table on the ri
25793 67 68 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ght. */.        
25794 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65          continue
25795 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
25796 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  }.            }.
25797 20 20 20 20 20 20 20 20 20 20 20 20 70 52 69 67              pRig
25798 68 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  ht = sqlite3PExp
25799 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c  r(pParse, TK_ID,
2579a 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20   0, 0, 0);.     
2579b 20 20 20 20 20 20 20 69 66 28 20 70 52 69 67 68         if( pRigh
2579c 74 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20  t==0 ) break;.  
2579d 20 20 20 20 20 20 20 20 20 20 73 65 74 51 75 6f            setQuo
2579e 74 65 64 54 6f 6b 65 6e 28 70 50 61 72 73 65 2c  tedToken(pParse,
2579f 20 26 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 2c   &pRight->token,
257a0 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20   zName);.       
257a1 20 20 20 20 20 69 66 28 20 6c 6f 6e 67 4e 61 6d       if( longNam
257a2 65 73 20 7c 7c 20 70 54 61 62 4c 69 73 74 2d 3e  es || pTabList->
257a3 6e 53 72 63 3e 31 20 29 7b 0a 20 20 20 20 20 20  nSrc>1 ){.      
257a4 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c          Expr *pL
257a5 65 66 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78  eft = sqlite3PEx
257a6 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44  pr(pParse, TK_ID
257a7 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  , 0, 0, 0);.    
257a8 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 20            pExpr 
257a9 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
257aa 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 70  Parse, TK_DOT, p
257ab 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 30 29  Left, pRight, 0)
257ac 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
257ad 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20 62  if( pExpr==0 ) b
257ae 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
257af 20 20 20 20 73 65 74 51 75 6f 74 65 64 54 6f 6b      setQuotedTok
257b0 65 6e 28 70 50 61 72 73 65 2c 20 26 70 4c 65 66  en(pParse, &pLef
257b1 74 2d 3e 74 6f 6b 65 6e 2c 20 7a 54 61 62 4e 61  t->token, zTabNa
257b2 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  me);.           
257b3 20 20 20 73 65 74 54 6f 6b 65 6e 28 26 70 45 78     setToken(&pEx
257b4 70 72 2d 3e 73 70 61 6e 2c 20 0a 20 20 20 20 20  pr->span, .     
257b5 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
257b6 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
257b7 22 25 73 2e 25 73 22 2c 20 7a 54 61 62 4e 61 6d  "%s.%s", zTabNam
257b8 65 2c 20 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20  e, zName));.    
257b9 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
257ba 3e 73 70 61 6e 2e 64 79 6e 20 3d 20 31 3b 0a 20  >span.dyn = 1;. 
257bb 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78               pEx
257bc 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 20 3d 20 30 3b  pr->token.z = 0;
257bd 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
257be 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 20 3d 20  Expr->token.n = 
257bf 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0;.             
257c0 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 64 79   pExpr->token.dy
257c1 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  n = 0;.         
257c2 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
257c3 20 20 20 20 20 20 20 20 70 45 78 70 72 20 3d 20          pExpr = 
257c4 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20  pRight;.        
257c5 20 20 20 20 20 20 70 45 78 70 72 2d 3e 73 70 61        pExpr->spa
257c6 6e 20 3d 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  n = pExpr->token
257c7 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
257c8 70 45 78 70 72 2d 3e 73 70 61 6e 2e 64 79 6e 20  pExpr->span.dyn 
257c9 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = 0;.           
257ca 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69   }.            i
257cb 66 28 20 6c 6f 6e 67 4e 61 6d 65 73 20 29 7b 0a  f( longNames ){.
257cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4e                pN
257cd 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ew = sqlite3Expr
257ce 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
257cf 65 2c 20 70 4e 65 77 2c 20 70 45 78 70 72 2c 20  e, pNew, pExpr, 
257d0 26 70 45 78 70 72 2d 3e 73 70 61 6e 29 3b 0a 20  &pExpr->span);. 
257d1 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
257d2 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
257d3 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78  pNew = sqlite3Ex
257d4 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
257d5 72 73 65 2c 20 70 4e 65 77 2c 20 70 45 78 70 72  rse, pNew, pExpr
257d6 2c 20 26 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e  , &pRight->token
257d7 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  );.            }
257d8 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
257d9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
257da 66 28 20 21 74 61 62 6c 65 53 65 65 6e 20 29 7b  f( !tableSeen ){
257db 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a  .          if( z
257dc 54 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20  TName ){.       
257dd 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
257de 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f  rMsg(pParse, "no
257df 20 73 75 63 68 20 74 61 62 6c 65 3a 20 25 73 22   such table: %s"
257e0 2c 20 7a 54 4e 61 6d 65 29 3b 0a 20 20 20 20 20  , zTName);.     
257e1 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
257e2 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
257e3 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
257e4 22 6e 6f 20 74 61 62 6c 65 73 20 73 70 65 63 69  "no tables speci
257e5 66 69 65 64 22 29 3b 0a 20 20 20 20 20 20 20 20  fied");.        
257e6 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
257e7 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
257e8 72 65 65 28 64 62 2c 20 7a 54 4e 61 6d 65 29 3b  ree(db, zTName);
257e9 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
257ea 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
257eb 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 45 4c  stDelete(db, pEL
257ec 69 73 74 29 3b 0a 20 20 20 20 70 2d 3e 70 45 4c  ist);.    p->pEL
257ed 69 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a  ist = pNew;.  }.
257ee 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43  #if SQLITE_MAX_C
257ef 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 70 2d 3e 70  OLUMN.  if( p->p
257f0 45 4c 69 73 74 20 26 26 20 70 2d 3e 70 45 4c 69  EList && p->pELi
257f1 73 74 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 4c  st->nExpr>db->aL
257f2 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
257f3 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20  T_COLUMN] ){.   
257f4 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
257f5 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61  (pParse, "too ma
257f6 6e 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 72 65  ny columns in re
257f7 73 75 6c 74 20 73 65 74 22 29 3b 0a 20 20 7d 0a  sult set");.  }.
257f8 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
257f9 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a  WRC_Continue;.}.
257fa 0a 2f 2a 0a 2a 2a 20 4e 6f 2d 6f 70 20 72 6f 75  ./*.** No-op rou
257fb 74 69 6e 65 20 66 6f 72 20 74 68 65 20 70 61 72  tine for the par
257fc 73 65 2d 74 72 65 65 20 77 61 6c 6b 65 72 2e 0a  se-tree walker..
257fd 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20  **.** When this 
257fe 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 57  routine is the W
257ff 61 6c 6b 65 72 2e 78 45 78 70 72 43 61 6c 6c 62  alker.xExprCallb
25800 61 63 6b 20 74 68 65 6e 20 65 78 70 72 65 73 73  ack then express
25801 69 6f 6e 20 74 72 65 65 73 0a 2a 2a 20 61 72 65  ion trees.** are
25802 20 77 61 6c 6b 65 64 20 77 69 74 68 6f 75 74 20   walked without 
25803 61 6e 79 20 61 63 74 69 6f 6e 73 20 62 65 69 6e  any actions bein
25804 67 20 74 61 6b 65 6e 20 61 74 20 65 61 63 68 20  g taken at each 
25805 6e 6f 64 65 2e 20 20 50 72 65 73 75 6d 61 62 6c  node.  Presumabl
25806 79 2c 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20  y,.** when this 
25807 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
25808 66 6f 72 20 57 61 6c 6b 65 72 2e 78 45 78 70 72  for Walker.xExpr
25809 43 61 6c 6c 62 61 63 6b 20 74 68 65 6e 20 0a 2a  Callback then .*
2580a 2a 20 57 61 6c 6b 65 72 2e 78 53 65 6c 65 63 74  * Walker.xSelect
2580b 43 61 6c 6c 62 61 63 6b 20 69 73 20 73 65 74 20  Callback is set 
2580c 74 6f 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20  to do something 
2580d 75 73 65 66 75 6c 20 66 6f 72 20 65 76 65 72 79  useful for every
2580e 20 0a 2a 2a 20 73 75 62 71 75 65 72 79 20 69 6e   .** subquery in
2580f 20 74 68 65 20 70 61 72 73 65 72 20 74 72 65 65   the parser tree
25810 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
25811 65 78 70 72 57 61 6c 6b 4e 6f 6f 70 28 57 61 6c  exprWalkNoop(Wal
25812 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78  ker *pWalker, Ex
25813 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 72 65  pr *pExpr){.  re
25814 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75  turn WRC_Continu
25815 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  e;.}../*.** This
25816 20 72 6f 75 74 69 6e 65 20 22 65 78 70 61 6e 64   routine "expand
25817 73 22 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  s" a SELECT stat
25818 65 6d 65 6e 74 20 61 6e 64 20 61 6c 6c 20 6f 66  ement and all of
25819 20 69 74 73 20 73 75 62 71 75 65 72 69 65 73 2e   its subqueries.
2581a 0a 2a 2a 20 46 6f 72 20 61 64 64 69 74 69 6f 6e  .** For addition
2581b 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f  al information o
2581c 6e 20 77 68 61 74 20 69 74 20 6d 65 61 6e 73 20  n what it means 
2581d 74 6f 20 22 65 78 70 61 6e 64 22 20 61 20 53 45  to "expand" a SE
2581e 4c 45 43 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  LECT.** statemen
2581f 74 2c 20 73 65 65 20 74 68 65 20 63 6f 6d 6d 65  t, see the comme
25820 6e 74 20 6f 6e 20 74 68 65 20 73 65 6c 65 63 74  nt on the select
25821 45 78 70 61 6e 64 20 77 6f 72 6b 65 72 20 63 61  Expand worker ca
25822 6c 6c 62 61 63 6b 20 61 62 6f 76 65 2e 0a 2a 2a  llback above..**
25823 0a 2a 2a 20 45 78 70 61 6e 64 69 6e 67 20 61 20  .** Expanding a 
25824 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
25825 20 69 73 20 74 68 65 20 66 69 72 73 74 20 73 74   is the first st
25826 65 70 20 69 6e 20 70 72 6f 63 65 73 73 69 6e 67  ep in processing
25827 20 61 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61   a.** SELECT sta
25828 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 53 45 4c  tement.  The SEL
25829 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 6d 75  ECT statement mu
2582a 73 74 20 62 65 20 65 78 70 61 6e 64 65 64 20 62  st be expanded b
2582b 65 66 6f 72 65 0a 2a 2a 20 6e 61 6d 65 20 72 65  efore.** name re
2582c 73 6f 6c 75 74 69 6f 6e 20 69 73 20 70 65 72 66  solution is perf
2582d 6f 72 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ormed..**.** If 
2582e 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72  anything goes wr
2582f 6f 6e 67 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65  ong, an error me
25830 73 73 61 67 65 20 69 73 20 77 72 69 74 74 65 6e  ssage is written
25831 20 69 6e 74 6f 20 70 50 61 72 73 65 2e 0a 2a 2a   into pParse..**
25832 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e   The calling fun
25833 63 74 69 6f 6e 20 63 61 6e 20 64 65 74 65 63 74  ction can detect
25834 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 62 79 20   the problem by 
25835 6c 6f 6f 6b 69 6e 67 20 61 74 20 70 50 61 72 73  looking at pPars
25836 65 2d 3e 6e 45 72 72 0a 2a 2a 20 61 6e 64 2f 6f  e->nErr.** and/o
25837 72 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61  r pParse->db->ma
25838 6c 6c 6f 63 46 61 69 6c 65 64 2e 0a 2a 2f 0a 73  llocFailed..*/.s
25839 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74  tatic void sqlit
2583a 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 50  e3SelectExpand(P
2583b 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 65  arse *pParse, Se
2583c 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a  lect *pSelect){.
2583d 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e    Walker w;.  w.
2583e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20  xSelectCallback 
2583f 3d 20 73 65 6c 65 63 74 45 78 70 61 6e 64 65 72  = selectExpander
25840 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62  ;.  w.xExprCallb
25841 61 63 6b 20 3d 20 65 78 70 72 57 61 6c 6b 4e 6f  ack = exprWalkNo
25842 6f 70 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 3d  op;.  w.pParse =
25843 20 70 50 61 72 73 65 3b 0a 20 20 73 71 6c 69 74   pParse;.  sqlit
25844 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c  e3WalkSelect(&w,
25845 20 70 53 65 6c 65 63 74 29 3b 0a 7d 0a 0a 0a 23   pSelect);.}...#
25846 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
25847 49 54 5f 53 55 42 51 55 45 52 59 0a 2f 2a 0a 2a  IT_SUBQUERY./*.*
25848 2a 20 54 68 69 73 20 69 73 20 61 20 57 61 6c 6b  * This is a Walk
25849 65 72 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61  er.xSelectCallba
2584a 63 6b 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20  ck callback for 
2584b 74 68 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63  the sqlite3Selec
2584c 74 54 79 70 65 49 6e 66 6f 28 29 0a 2a 2a 20 69  tTypeInfo().** i
2584d 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20  nterface..**.** 
2584e 46 6f 72 20 65 61 63 68 20 46 52 4f 4d 2d 63 6c  For each FROM-cl
2584f 61 75 73 65 20 73 75 62 71 75 65 72 79 2c 20 61  ause subquery, a
25850 64 64 20 43 6f 6c 75 6d 6e 2e 7a 54 79 70 65 20  dd Column.zType 
25851 61 6e 64 20 43 6f 6c 75 6d 6e 2e 7a 43 6f 6c 6c  and Column.zColl
25852 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  .** information 
25853 74 6f 20 74 68 65 20 54 61 62 6c 65 20 73 74 72  to the Table str
25854 75 63 74 75 72 65 20 74 68 61 74 20 72 65 70 72  ucture that repr
25855 65 73 65 6e 74 73 20 74 68 65 20 72 65 73 75 6c  esents the resul
25856 74 20 73 65 74 0a 2a 2a 20 6f 66 20 74 68 61 74  t set.** of that
25857 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a   subquery..**.**
25858 20 54 68 65 20 54 61 62 6c 65 20 73 74 72 75 63   The Table struc
25859 74 75 72 65 20 74 68 61 74 20 72 65 70 72 65 73  ture that repres
2585a 65 6e 74 73 20 74 68 65 20 72 65 73 75 6c 74 20  ents the result 
2585b 73 65 74 20 77 61 73 20 63 6f 6e 73 74 72 75 63  set was construc
2585c 74 65 64 0a 2a 2a 20 62 79 20 73 65 6c 65 63 74  ted.** by select
2585d 45 78 70 61 6e 64 65 72 28 29 20 62 75 74 20 74  Expander() but t
2585e 68 65 20 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c  he type and coll
2585f 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f  ation informatio
25860 6e 20 77 61 73 20 6f 6d 69 74 74 65 64 0a 2a 2a  n was omitted.**
25861 20 61 74 20 74 68 61 74 20 70 6f 69 6e 74 20 62   at that point b
25862 65 63 61 75 73 65 20 69 64 65 6e 74 69 66 69 65  ecause identifie
25863 72 73 20 68 61 64 20 6e 6f 74 20 79 65 74 20 62  rs had not yet b
25864 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 20 20 54  een resolved.  T
25865 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69  his.** routine i
25866 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 69  s called after i
25867 64 65 6e 74 69 66 69 65 72 20 72 65 73 6f 6c 75  dentifier resolu
25868 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
25869 69 6e 74 20 73 65 6c 65 63 74 41 64 64 53 75 62  int selectAddSub
2586a 71 75 65 72 79 54 79 70 65 49 6e 66 6f 28 57 61  queryTypeInfo(Wa
2586b 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53  lker *pWalker, S
2586c 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 50 61 72  elect *p){.  Par
2586d 73 65 20 2a 70 50 61 72 73 65 3b 0a 20 20 69 6e  se *pParse;.  in
2586e 74 20 69 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a  t i;.  SrcList *
2586f 70 54 61 62 4c 69 73 74 3b 0a 20 20 73 74 72 75  pTabList;.  stru
25870 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
25871 2a 70 46 72 6f 6d 3b 0a 0a 20 20 61 73 73 65 72  *pFrom;..  asser
25872 74 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26  t( p->selFlags &
25873 20 53 46 5f 52 65 73 6f 6c 76 65 64 20 29 3b 0a   SF_Resolved );.
25874 20 20 69 66 28 20 28 70 2d 3e 73 65 6c 46 6c 61    if( (p->selFla
25875 67 73 20 26 20 53 46 5f 48 61 73 54 79 70 65 49  gs & SF_HasTypeI
25876 6e 66 6f 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70  nfo)==0 ){.    p
25877 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46  ->selFlags |= SF
25878 5f 48 61 73 54 79 70 65 49 6e 66 6f 3b 0a 20 20  _HasTypeInfo;.  
25879 20 20 70 50 61 72 73 65 20 3d 20 70 57 61 6c 6b    pParse = pWalk
2587a 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 20  er->pParse;.    
2587b 70 54 61 62 4c 69 73 74 20 3d 20 70 2d 3e 70 53  pTabList = p->pS
2587c 72 63 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c  rc;.    for(i=0,
2587d 20 70 46 72 6f 6d 3d 70 54 61 62 4c 69 73 74 2d   pFrom=pTabList-
2587e 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e  >a; i<pTabList->
2587f 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 46 72 6f 6d  nSrc; i++, pFrom
25880 2b 2b 29 7b 0a 20 20 20 20 20 20 54 61 62 6c 65  ++){.      Table
25881 20 2a 70 54 61 62 20 3d 20 70 46 72 6f 6d 2d 3e   *pTab = pFrom->
25882 70 54 61 62 3b 0a 20 20 20 20 20 20 69 66 28 20  pTab;.      if( 
25883 70 54 61 62 20 26 26 20 28 70 54 61 62 2d 3e 74  pTab && (pTab->t
25884 61 62 46 6c 61 67 73 20 26 20 54 46 5f 45 70 68  abFlags & TF_Eph
25885 65 6d 65 72 61 6c 29 21 3d 30 20 29 7b 0a 20 20  emeral)!=0 ){.  
25886 20 20 20 20 20 20 2f 2a 20 41 20 73 75 62 2d 71        /* A sub-q
25887 75 65 72 79 20 69 6e 20 74 68 65 20 46 52 4f 4d  uery in the FROM
25888 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c   clause of a SEL
25889 45 43 54 20 2a 2f 0a 20 20 20 20 20 20 20 20 53  ECT */.        S
2588a 65 6c 65 63 74 20 2a 70 53 65 6c 20 3d 20 70 46  elect *pSel = pF
2588b 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a 20 20  rom->pSelect;.  
2588c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53        assert( pS
2588d 65 6c 20 29 3b 0a 20 20 20 20 20 20 20 20 77 68  el );.        wh
2588e 69 6c 65 28 20 70 53 65 6c 2d 3e 70 50 72 69 6f  ile( pSel->pPrio
2588f 72 20 29 20 70 53 65 6c 20 3d 20 70 53 65 6c 2d  r ) pSel = pSel-
25890 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 20  >pPrior;.       
25891 20 73 65 6c 65 63 74 41 64 64 43 6f 6c 75 6d 6e   selectAddColumn
25892 54 79 70 65 41 6e 64 43 6f 6c 6c 61 74 69 6f 6e  TypeAndCollation
25893 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 6e  (pParse, pTab->n
25894 43 6f 6c 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 2c  Col, pTab->aCol,
25895 20 70 53 65 6c 29 3b 0a 20 20 20 20 20 20 7d 0a   pSel);.      }.
25896 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
25897 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b  rn WRC_Continue;
25898 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  .}.#endif.../*.*
25899 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
2589a 64 64 73 20 64 61 74 61 74 79 70 65 20 61 6e 64  dds datatype and
2589b 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
2589c 6e 63 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  nce information 
2589d 74 6f 0a 2a 2a 20 74 68 65 20 54 61 62 6c 65 20  to.** the Table 
2589e 73 74 72 75 63 74 75 72 65 73 20 6f 66 20 61 6c  structures of al
2589f 6c 20 46 52 4f 4d 2d 63 6c 61 75 73 65 20 73 75  l FROM-clause su
258a0 62 71 75 65 72 69 65 73 20 69 6e 20 61 0a 2a 2a  bqueries in a.**
258a1 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
258a2 74 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 69  t..**.** Use thi
258a3 73 20 72 6f 75 74 69 6e 65 20 61 66 74 65 72 20  s routine after 
258a4 6e 61 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e 2e  name resolution.
258a5 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
258a6 73 71 6c 69 74 65 33 53 65 6c 65 63 74 41 64 64  sqlite3SelectAdd
258a7 54 79 70 65 49 6e 66 6f 28 50 61 72 73 65 20 2a  TypeInfo(Parse *
258a8 70 50 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a  pParse, Select *
258a9 70 53 65 6c 65 63 74 29 7b 0a 23 69 66 6e 64 65  pSelect){.#ifnde
258aa 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  f SQLITE_OMIT_SU
258ab 42 51 55 45 52 59 0a 20 20 57 61 6c 6b 65 72 20  BQUERY.  Walker 
258ac 77 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61  w;.  w.xSelectCa
258ad 6c 6c 62 61 63 6b 20 3d 20 73 65 6c 65 63 74 41  llback = selectA
258ae 64 64 53 75 62 71 75 65 72 79 54 79 70 65 49 6e  ddSubqueryTypeIn
258af 66 6f 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c  fo;.  w.xExprCal
258b0 6c 62 61 63 6b 20 3d 20 65 78 70 72 57 61 6c 6b  lback = exprWalk
258b1 4e 6f 6f 70 3b 0a 20 20 77 2e 70 50 61 72 73 65  Noop;.  w.pParse
258b2 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 71 6c   = pParse;.  sql
258b3 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 26  ite3WalkSelect(&
258b4 77 2c 20 70 53 65 6c 65 63 74 29 3b 0a 23 65 6e  w, pSelect);.#en
258b5 64 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  dif.}.../*.** Th
258b6 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20  is routine sets 
258b7 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  of a SELECT stat
258b8 65 6d 65 6e 74 20 66 6f 72 20 70 72 6f 63 65 73  ement for proces
258b9 73 69 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 66 6f  sing.  The.** fo
258ba 6c 6c 6f 77 69 6e 67 20 69 73 20 61 63 63 6f 6d  llowing is accom
258bb 70 6c 69 73 68 65 64 3a 0a 2a 2a 0a 2a 2a 20 20  plished:.**.**  
258bc 20 20 20 2a 20 20 56 44 42 45 20 43 75 72 73 6f     *  VDBE Curso
258bd 72 20 6e 75 6d 62 65 72 73 20 61 72 65 20 61 73  r numbers are as
258be 73 69 67 6e 65 64 20 74 6f 20 61 6c 6c 20 46 52  signed to all FR
258bf 4f 4d 2d 63 6c 61 75 73 65 20 74 65 72 6d 73 2e  OM-clause terms.
258c0 0a 2a 2a 20 20 20 20 20 2a 20 20 45 70 68 65 6d  .**     *  Ephem
258c1 65 72 61 6c 20 54 61 62 6c 65 20 6f 62 6a 65 63  eral Table objec
258c2 74 73 20 61 72 65 20 63 72 65 61 74 65 64 20 66  ts are created f
258c3 6f 72 20 61 6c 6c 20 46 52 4f 4d 2d 63 6c 61 75  or all FROM-clau
258c4 73 65 20 73 75 62 71 75 65 72 69 65 73 2e 0a 2a  se subqueries..*
258c5 2a 20 20 20 20 20 2a 20 20 4f 4e 20 61 6e 64 20  *     *  ON and 
258c6 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 61 72  USING clauses ar
258c7 65 20 73 68 69 66 74 65 64 20 69 6e 74 6f 20 57  e shifted into W
258c8 48 45 52 45 20 73 74 61 74 65 6d 65 6e 74 73 0a  HERE statements.
258c9 2a 2a 20 20 20 20 20 2a 20 20 57 69 6c 64 63 61  **     *  Wildca
258ca 72 64 73 20 22 2a 22 20 61 6e 64 20 22 54 41 42  rds "*" and "TAB
258cb 4c 45 2e 2a 22 20 69 6e 20 72 65 73 75 6c 74 20  LE.*" in result 
258cc 73 65 74 73 20 61 72 65 20 65 78 70 61 6e 64 65  sets are expande
258cd 64 2e 0a 2a 2a 20 20 20 20 20 2a 20 20 49 64 65  d..**     *  Ide
258ce 6e 74 69 66 69 65 72 73 20 69 6e 20 65 78 70 72  ntifiers in expr
258cf 65 73 73 69 6f 6e 20 61 72 65 20 6d 61 74 63 68  ession are match
258d0 65 64 20 74 6f 20 74 61 62 6c 65 73 2e 0a 2a 2a  ed to tables..**
258d1 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
258d2 20 61 63 74 73 20 72 65 63 75 72 73 69 76 65 6c   acts recursivel
258d3 79 20 6f 6e 20 61 6c 6c 20 73 75 62 71 75 65 72  y on all subquer
258d4 69 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 53  ies within the S
258d5 45 4c 45 43 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ELECT..*/.SQLITE
258d6 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
258d7 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28  lite3SelectPrep(
258d8 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
258d9 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
258da 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   parser context 
258db 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20  */.  Select *p, 
258dc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
258dd 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
258de 65 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 64 2e  ent being coded.
258df 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78   */.  NameContex
258e0 74 20 2a 70 4f 75 74 65 72 4e 43 20 20 2f 2a 20  t *pOuterNC  /* 
258e1 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72  Name context for
258e2 20 63 6f 6e 74 61 69 6e 65 72 20 2a 2f 0a 29 7b   container */.){
258e3 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a  .  sqlite3 *db;.
258e4 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74    if( p==0 ) ret
258e5 75 72 6e 3b 0a 20 20 64 62 20 3d 20 70 50 61 72  urn;.  db = pPar
258e6 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 2d  se->db;.  if( p-
258e7 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 48  >selFlags & SF_H
258e8 61 73 54 79 70 65 49 6e 66 6f 20 29 20 72 65 74  asTypeInfo ) ret
258e9 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72 73  urn;.  if( pPars
258ea 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d  e->nErr || db->m
258eb 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65  allocFailed ) re
258ec 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 53  turn;.  sqlite3S
258ed 65 6c 65 63 74 45 78 70 61 6e 64 28 70 50 61 72  electExpand(pPar
258ee 73 65 2c 20 70 29 3b 0a 20 20 69 66 28 20 70 50  se, p);.  if( pP
258ef 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62  arse->nErr || db
258f0 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
258f1 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74   return;.  sqlit
258f2 65 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e  e3ResolveSelectN
258f3 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 2c 20  ames(pParse, p, 
258f4 70 4f 75 74 65 72 4e 43 29 3b 0a 20 20 69 66 28  pOuterNC);.  if(
258f5 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c   pParse->nErr ||
258f6 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
258f7 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71  d ) return;.  sq
258f8 6c 69 74 65 33 53 65 6c 65 63 74 41 64 64 54 79  lite3SelectAddTy
258f9 70 65 49 6e 66 6f 28 70 50 61 72 73 65 2c 20 70  peInfo(pParse, p
258fa 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65  );.}../*.** Rese
258fb 74 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20  t the aggregate 
258fc 61 63 63 75 6d 75 6c 61 74 6f 72 2e 0a 2a 2a 0a  accumulator..**.
258fd 2a 2a 20 54 68 65 20 61 67 67 72 65 67 61 74 65  ** The aggregate
258fe 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 69 73 20   accumulator is 
258ff 61 20 73 65 74 20 6f 66 20 6d 65 6d 6f 72 79 20  a set of memory 
25900 63 65 6c 6c 73 20 74 68 61 74 20 68 6f 6c 64 0a  cells that hold.
25901 2a 2a 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  ** intermediate 
25902 72 65 73 75 6c 74 73 20 77 68 69 6c 65 20 63 61  results while ca
25903 6c 63 75 6c 61 74 69 6e 67 20 61 6e 20 61 67 67  lculating an agg
25904 72 65 67 61 74 65 2e 20 20 54 68 69 73 0a 2a 2a  regate.  This.**
25905 20 72 6f 75 74 69 6e 65 20 73 69 6d 70 6c 79 20   routine simply 
25906 73 74 6f 72 65 73 20 4e 55 4c 4c 73 20 69 6e 20  stores NULLs in 
25907 61 6c 6c 20 6f 66 20 74 68 6f 73 65 20 6d 65 6d  all of those mem
25908 6f 72 79 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 73 74  ory cells..*/.st
25909 61 74 69 63 20 76 6f 69 64 20 72 65 73 65 74 41  atic void resetA
2590a 63 63 75 6d 75 6c 61 74 6f 72 28 50 61 72 73 65  ccumulator(Parse
2590b 20 2a 70 50 61 72 73 65 2c 20 41 67 67 49 6e 66   *pParse, AggInf
2590c 6f 20 2a 70 41 67 67 49 6e 66 6f 29 7b 0a 20 20  o *pAggInfo){.  
2590d 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
2590e 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69  ->pVdbe;.  int i
2590f 3b 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e  ;.  struct AggIn
25910 66 6f 5f 66 75 6e 63 20 2a 70 46 75 6e 63 3b 0a  fo_func *pFunc;.
25911 20 20 69 66 28 20 70 41 67 67 49 6e 66 6f 2d 3e    if( pAggInfo->
25912 6e 46 75 6e 63 2b 70 41 67 67 49 6e 66 6f 2d 3e  nFunc+pAggInfo->
25913 6e 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20  nColumn==0 ){.  
25914 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
25915 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 67 67 49  for(i=0; i<pAggI
25916 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  nfo->nColumn; i+
25917 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  +){.    sqlite3V
25918 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
25919 4e 75 6c 6c 2c 20 30 2c 20 70 41 67 67 49 6e 66  Null, 0, pAggInf
2591a 6f 2d 3e 61 43 6f 6c 5b 69 5d 2e 69 4d 65 6d 29  o->aCol[i].iMem)
2591b 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 70 46 75 6e  ;.  }.  for(pFun
2591c 63 3d 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e  c=pAggInfo->aFun
2591d 63 2c 20 69 3d 30 3b 20 69 3c 70 41 67 67 49 6e  c, i=0; i<pAggIn
2591e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20  fo->nFunc; i++, 
2591f 70 46 75 6e 63 2b 2b 29 7b 0a 20 20 20 20 73 71  pFunc++){.    sq
25920 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
25921 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 70  v, OP_Null, 0, p
25922 46 75 6e 63 2d 3e 69 4d 65 6d 29 3b 0a 20 20 20  Func->iMem);.   
25923 20 69 66 28 20 70 46 75 6e 63 2d 3e 69 44 69 73   if( pFunc->iDis
25924 74 69 6e 63 74 3e 3d 30 20 29 7b 0a 20 20 20 20  tinct>=0 ){.    
25925 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70 46 75    Expr *pE = pFu
25926 6e 63 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20  nc->pExpr;.     
25927 20 69 66 28 20 70 45 2d 3e 70 4c 69 73 74 3d 3d   if( pE->pList==
25928 30 20 7c 7c 20 70 45 2d 3e 70 4c 69 73 74 2d 3e  0 || pE->pList->
25929 6e 45 78 70 72 21 3d 31 20 29 7b 0a 20 20 20 20  nExpr!=1 ){.    
2592a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2592b 4d 73 67 28 70 50 61 72 73 65 2c 20 22 44 49 53  Msg(pParse, "DIS
2592c 54 49 4e 43 54 20 69 6e 20 61 67 67 72 65 67 61  TINCT in aggrega
2592d 74 65 20 6d 75 73 74 20 62 65 20 66 6f 6c 6c 6f  te must be follo
2592e 77 65 64 20 22 0a 20 20 20 20 20 20 20 20 20 20  wed ".          
2592f 20 22 62 79 20 61 6e 20 65 78 70 72 65 73 73 69   "by an expressi
25930 6f 6e 22 29 3b 0a 20 20 20 20 20 20 20 20 70 46  on");.        pF
25931 75 6e 63 2d 3e 69 44 69 73 74 69 6e 63 74 20 3d  unc->iDistinct =
25932 20 2d 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65   -1;.      }else
25933 7b 0a 20 20 20 20 20 20 20 20 4b 65 79 49 6e 66  {.        KeyInf
25934 6f 20 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 6b 65  o *pKeyInfo = ke
25935 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73  yInfoFromExprLis
25936 74 28 70 50 61 72 73 65 2c 20 70 45 2d 3e 70 4c  t(pParse, pE->pL
25937 69 73 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ist);.        sq
25938 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
25939 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  v, OP_OpenEpheme
2593a 72 61 6c 2c 20 70 46 75 6e 63 2d 3e 69 44 69 73  ral, pFunc->iDis
2593b 74 69 6e 63 74 2c 20 30 2c 20 30 2c 0a 20 20 20  tinct, 0, 0,.   
2593c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2593d 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b         (char*)pK
2593e 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e  eyInfo, P4_KEYIN
2593f 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20  FO_HANDOFF);.   
25940 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d     }.    }.  }.}
25941 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74  ../*.** Invoke t
25942 68 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c 69 7a  he OP_AggFinaliz
25943 65 20 6f 70 63 6f 64 65 20 66 6f 72 20 65 76 65  e opcode for eve
25944 72 79 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  ry aggregate fun
25945 63 74 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 20  ction.** in the 
25946 41 67 67 49 6e 66 6f 20 73 74 72 75 63 74 75 72  AggInfo structur
25947 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
25948 64 20 66 69 6e 61 6c 69 7a 65 41 67 67 46 75 6e  d finalizeAggFun
25949 63 74 69 6f 6e 73 28 50 61 72 73 65 20 2a 70 50  ctions(Parse *pP
2594a 61 72 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70  arse, AggInfo *p
2594b 41 67 67 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65  AggInfo){.  Vdbe
2594c 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
2594d 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  dbe;.  int i;.  
2594e 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66  struct AggInfo_f
2594f 75 6e 63 20 2a 70 46 3b 0a 20 20 66 6f 72 28 69  unc *pF;.  for(i
25950 3d 30 2c 20 70 46 3d 70 41 67 67 49 6e 66 6f 2d  =0, pF=pAggInfo-
25951 3e 61 46 75 6e 63 3b 20 69 3c 70 41 67 67 49 6e  >aFunc; i<pAggIn
25952 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20  fo->nFunc; i++, 
25953 70 46 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 4c  pF++){.    ExprL
25954 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 46 2d  ist *pList = pF-
25955 3e 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b 0a 20  >pExpr->pList;. 
25956 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
25957 64 4f 70 34 28 76 2c 20 4f 50 5f 41 67 67 46 69  dOp4(v, OP_AggFi
25958 6e 61 6c 2c 20 70 46 2d 3e 69 4d 65 6d 2c 20 70  nal, pF->iMem, p
25959 4c 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45  List ? pList->nE
2595a 78 70 72 20 3a 20 30 2c 20 30 2c 0a 20 20 20 20  xpr : 0, 0,.    
2595b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2595c 20 20 28 76 6f 69 64 2a 29 70 46 2d 3e 70 46 75    (void*)pF->pFu
2595d 6e 63 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b  nc, P4_FUNCDEF);
2595e 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 70  .  }.}../*.** Up
2595f 64 61 74 65 20 74 68 65 20 61 63 63 75 6d 75 6c  date the accumul
25960 61 74 6f 72 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  ator memory cell
25961 73 20 66 6f 72 20 61 6e 20 61 67 67 72 65 67 61  s for an aggrega
25962 74 65 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 74  te based on.** t
25963 68 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f  he current curso
25964 72 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 73  r position..*/.s
25965 74 61 74 69 63 20 76 6f 69 64 20 75 70 64 61 74  tatic void updat
25966 65 41 63 63 75 6d 75 6c 61 74 6f 72 28 50 61 72  eAccumulator(Par
25967 73 65 20 2a 70 50 61 72 73 65 2c 20 41 67 67 49  se *pParse, AggI
25968 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 29 7b 0a  nfo *pAggInfo){.
25969 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
2596a 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74  se->pVdbe;.  int
2596b 20 69 3b 0a 20 20 73 74 72 75 63 74 20 41 67 67   i;.  struct Agg
2596c 49 6e 66 6f 5f 66 75 6e 63 20 2a 70 46 3b 0a 20  Info_func *pF;. 
2596d 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f   struct AggInfo_
2596e 63 6f 6c 20 2a 70 43 3b 0a 0a 20 20 70 41 67 67  col *pC;..  pAgg
2596f 49 6e 66 6f 2d 3e 64 69 72 65 63 74 4d 6f 64 65  Info->directMode
25970 20 3d 20 31 3b 0a 20 20 66 6f 72 28 69 3d 30 2c   = 1;.  for(i=0,
25971 20 70 46 3d 70 41 67 67 49 6e 66 6f 2d 3e 61 46   pF=pAggInfo->aF
25972 75 6e 63 3b 20 69 3c 70 41 67 67 49 6e 66 6f 2d  unc; i<pAggInfo-
25973 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20 70 46 2b  >nFunc; i++, pF+
25974 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 72 67  +){.    int nArg
25975 3b 0a 20 20 20 20 69 6e 74 20 61 64 64 72 4e 65  ;.    int addrNe
25976 78 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20  xt = 0;.    int 
25977 72 65 67 41 67 67 3b 0a 20 20 20 20 45 78 70 72  regAgg;.    Expr
25978 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 46  List *pList = pF
25979 2d 3e 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b 0a  ->pExpr->pList;.
2597a 20 20 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b      if( pList ){
2597b 0a 20 20 20 20 20 20 6e 41 72 67 20 3d 20 70 4c  .      nArg = pL
2597c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20  ist->nExpr;.    
2597d 20 20 72 65 67 41 67 67 20 3d 20 73 71 6c 69 74    regAgg = sqlit
2597e 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70  e3GetTempRange(p
2597f 50 61 72 73 65 2c 20 6e 41 72 67 29 3b 0a 20 20  Parse, nArg);.  
25980 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
25981 6f 64 65 45 78 70 72 4c 69 73 74 28 70 50 61 72  odeExprList(pPar
25982 73 65 2c 20 70 4c 69 73 74 2c 20 72 65 67 41 67  se, pList, regAg
25983 67 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65  g, 0);.    }else
25984 7b 0a 20 20 20 20 20 20 6e 41 72 67 20 3d 20 30  {.      nArg = 0
25985 3b 0a 20 20 20 20 20 20 72 65 67 41 67 67 20 3d  ;.      regAgg =
25986 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   0;.    }.    if
25987 28 20 70 46 2d 3e 69 44 69 73 74 69 6e 63 74 3e  ( pF->iDistinct>
25988 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 64 64 72  =0 ){.      addr
25989 4e 65 78 74 20 3d 20 73 71 6c 69 74 65 33 56 64  Next = sqlite3Vd
2598a 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
2598b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 41        assert( nA
2598c 72 67 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 63  rg==1 );.      c
2598d 6f 64 65 44 69 73 74 69 6e 63 74 28 70 50 61 72  odeDistinct(pPar
2598e 73 65 2c 20 70 46 2d 3e 69 44 69 73 74 69 6e 63  se, pF->iDistinc
2598f 74 2c 20 61 64 64 72 4e 65 78 74 2c 20 31 2c 20  t, addrNext, 1, 
25990 72 65 67 41 67 67 29 3b 0a 20 20 20 20 7d 0a 20  regAgg);.    }. 
25991 20 20 20 69 66 28 20 70 46 2d 3e 70 46 75 6e 63     if( pF->pFunc
25992 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
25993 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29  _FUNC_NEEDCOLL )
25994 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20  {.      CollSeq 
25995 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20  *pColl = 0;.    
25996 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
25997 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20  t_item *pItem;. 
25998 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20       int j;.    
25999 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 21    assert( pList!
2599a 3d 30 20 29 3b 20 20 2f 2a 20 70 4c 69 73 74 21  =0 );  /* pList!
2599b 3d 30 20 69 66 20 70 46 2d 3e 70 46 75 6e 63 20  =0 if pF->pFunc 
2599c 68 61 73 20 4e 45 45 44 43 4f 4c 4c 20 2a 2f 0a  has NEEDCOLL */.
2599d 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70        for(j=0, p
2599e 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 21  Item=pList->a; !
2599f 70 43 6f 6c 6c 20 26 26 20 6a 3c 6e 41 72 67 3b  pColl && j<nArg;
259a0 20 6a 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   j++, pItem++){.
259a1 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20          pColl = 
259a2 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53  sqlite3ExprCollS
259a3 65 71 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d  eq(pParse, pItem
259a4 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20  ->pExpr);.      
259a5 7d 0a 20 20 20 20 20 20 69 66 28 20 21 70 43 6f  }.      if( !pCo
259a6 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43  ll ){.        pC
259a7 6f 6c 6c 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  oll = pParse->db
259a8 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 20  ->pDfltColl;.   
259a9 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
259aa 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
259ab 4f 50 5f 43 6f 6c 6c 53 65 71 2c 20 30 2c 20 30  OP_CollSeq, 0, 0
259ac 2c 20 30 2c 20 28 63 68 61 72 20 2a 29 70 43 6f  , 0, (char *)pCo
259ad 6c 6c 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29 3b  ll, P4_COLLSEQ);
259ae 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
259af 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
259b0 4f 50 5f 41 67 67 53 74 65 70 2c 20 30 2c 20 72  OP_AggStep, 0, r
259b1 65 67 41 67 67 2c 20 70 46 2d 3e 69 4d 65 6d 2c  egAgg, pF->iMem,
259b2 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
259b3 20 20 20 20 20 20 20 28 76 6f 69 64 2a 29 70 46         (void*)pF
259b4 2d 3e 70 46 75 6e 63 2c 20 50 34 5f 46 55 4e 43  ->pFunc, P4_FUNC
259b5 44 45 46 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  DEF);.    sqlite
259b6 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
259b7 20 6e 41 72 67 29 3b 0a 20 20 20 20 73 71 6c 69   nArg);.    sqli
259b8 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61  te3ReleaseTempRa
259b9 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 41  nge(pParse, regA
259ba 67 67 2c 20 6e 41 72 67 29 3b 0a 20 20 20 20 73  gg, nArg);.    s
259bb 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
259bc 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50  ffinityChange(pP
259bd 61 72 73 65 2c 20 72 65 67 41 67 67 2c 20 6e 41  arse, regAgg, nA
259be 72 67 29 3b 0a 20 20 20 20 69 66 28 20 61 64 64  rg);.    if( add
259bf 72 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 73  rNext ){.      s
259c0 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
259c1 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 4e 65  eLabel(v, addrNe
259c2 78 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  xt);.    }.  }. 
259c3 20 66 6f 72 28 69 3d 30 2c 20 70 43 3d 70 41 67   for(i=0, pC=pAg
259c4 67 49 6e 66 6f 2d 3e 61 43 6f 6c 3b 20 69 3c 70  gInfo->aCol; i<p
259c5 41 67 67 49 6e 66 6f 2d 3e 6e 41 63 63 75 6d 75  AggInfo->nAccumu
259c6 6c 61 74 6f 72 3b 20 69 2b 2b 2c 20 70 43 2b 2b  lator; i++, pC++
259c7 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  ){.    sqlite3Ex
259c8 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
259c9 43 2d 3e 70 45 78 70 72 2c 20 70 43 2d 3e 69 4d  C->pExpr, pC->iM
259ca 65 6d 29 3b 0a 20 20 7d 0a 20 20 70 41 67 67 49  em);.  }.  pAggI
259cb 6e 66 6f 2d 3e 64 69 72 65 63 74 4d 6f 64 65 20  nfo->directMode 
259cc 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  = 0;.}../*.** Ge
259cd 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
259ce 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  the SELECT state
259cf 6d 65 6e 74 20 67 69 76 65 6e 20 69 6e 20 74 68  ment given in th
259d0 65 20 70 20 61 72 67 75 6d 65 6e 74 2e 20 20 0a  e p argument.  .
259d1 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74  **.** The result
259d2 73 20 61 72 65 20 64 69 73 74 72 69 62 75 74 65  s are distribute
259d3 64 20 69 6e 20 76 61 72 69 6f 75 73 20 77 61 79  d in various way
259d4 73 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74  s depending on t
259d5 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f  he.** contents o
259d6 66 20 74 68 65 20 53 65 6c 65 63 74 44 65 73 74  f the SelectDest
259d7 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e 74   structure point
259d8 65 64 20 74 6f 20 62 79 20 61 72 67 75 6d 65 6e  ed to by argumen
259d9 74 20 70 44 65 73 74 0a 2a 2a 20 61 73 20 66 6f  t pDest.** as fo
259da 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  llows:.**.**    
259db 20 70 44 65 73 74 2d 3e 65 44 65 73 74 20 20 20   pDest->eDest   
259dc 20 52 65 73 75 6c 74 0a 2a 2a 20 20 20 20 20 2d   Result.**     -
259dd 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 2d  -----------    -
259de 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
259df 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
259e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20  ----------.**   
259e1 20 20 53 52 54 5f 4f 75 74 70 75 74 20 20 20 20    SRT_Output    
259e2 20 20 47 65 6e 65 72 61 74 65 20 61 20 72 6f 77    Generate a row
259e3 20 6f 66 20 6f 75 74 70 75 74 20 28 75 73 69 6e   of output (usin
259e4 67 20 74 68 65 20 4f 50 5f 52 65 73 75 6c 74 52  g the OP_ResultR
259e5 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ow.**           
259e6 20 20 20 20 20 20 20 20 20 20 6f 70 63 6f 64 65            opcode
259e7 29 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 69  ) for each row i
259e8 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
259e9 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f  ..**.**     SRT_
259ea 4d 65 6d 20 20 20 20 20 20 20 20 20 4f 6e 6c 79  Mem         Only
259eb 20 76 61 6c 69 64 20 69 66 20 74 68 65 20 72 65   valid if the re
259ec 73 75 6c 74 20 69 73 20 61 20 73 69 6e 67 6c 65  sult is a single
259ed 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 20 20 20 20   column..**     
259ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
259ef 53 74 6f 72 65 20 74 68 65 20 66 69 72 73 74 20  Store the first 
259f0 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 66 69  column of the fi
259f1 72 73 74 20 72 65 73 75 6c 74 20 72 6f 77 0a 2a  rst result row.*
259f2 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
259f3 20 20 20 20 20 20 69 6e 20 72 65 67 69 73 74 65        in registe
259f4 72 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 20 74  r pDest->iParm t
259f5 68 65 6e 20 61 62 61 6e 64 6f 6e 20 74 68 65 20  hen abandon the 
259f6 72 65 73 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  rest.**         
259f7 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20 74              of t
259f8 68 65 20 71 75 65 72 79 2e 20 20 54 68 69 73 20  he query.  This 
259f9 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 6d 70 6c  destination impl
259fa 69 65 73 20 22 4c 49 4d 49 54 20 31 22 2e 0a 2a  ies "LIMIT 1"..*
259fb 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 53 65 74  *.**     SRT_Set
259fc 20 20 20 20 20 20 20 20 20 54 68 65 20 72 65 73           The res
259fd 75 6c 74 20 6d 75 73 74 20 62 65 20 61 20 73 69  ult must be a si
259fe 6e 67 6c 65 20 63 6f 6c 75 6d 6e 2e 20 20 53 74  ngle column.  St
259ff 6f 72 65 20 65 61 63 68 0a 2a 2a 20 20 20 20 20  ore each.**     
25a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25a01 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 20 61 73  row of result as
25a02 20 74 68 65 20 6b 65 79 20 69 6e 20 74 61 62 6c   the key in tabl
25a03 65 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2e 20  e pDest->iParm. 
25a04 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
25a05 20 20 20 20 20 20 20 20 41 70 70 6c 79 20 74 68          Apply th
25a06 65 20 61 66 66 69 6e 69 74 79 20 70 44 65 73 74  e affinity pDest
25a07 2d 3e 61 66 66 69 6e 69 74 79 20 62 65 66 6f 72  ->affinity befor
25a08 65 20 73 74 6f 72 69 6e 67 0a 2a 2a 20 20 20 20  e storing.**    
25a09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25a0a 20 72 65 73 75 6c 74 73 2e 20 20 55 73 65 64 20   results.  Used 
25a0b 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 22 49 4e  to implement "IN
25a0c 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 2e 0a   (SELECT ...)"..
25a0d 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 55 6e  **.**     SRT_Un
25a0e 69 6f 6e 20 20 20 20 20 20 20 53 74 6f 72 65 20  ion       Store 
25a0f 72 65 73 75 6c 74 73 20 61 73 20 61 20 6b 65 79  results as a key
25a10 20 69 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20   in a temporary 
25a11 74 61 62 6c 65 20 70 44 65 73 74 2d 3e 69 50 61  table pDest->iPa
25a12 72 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52  rm..**.**     SR
25a13 54 5f 45 78 63 65 70 74 20 20 20 20 20 20 52 65  T_Except      Re
25a14 6d 6f 76 65 20 72 65 73 75 6c 74 73 20 66 72 6f  move results fro
25a15 6d 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20  m the temporary 
25a16 74 61 62 6c 65 20 70 44 65 73 74 2d 3e 69 50 61  table pDest->iPa
25a17 72 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52  rm..**.**     SR
25a18 54 5f 54 61 62 6c 65 20 20 20 20 20 20 20 53 74  T_Table       St
25a19 6f 72 65 20 72 65 73 75 6c 74 73 20 69 6e 20 74  ore results in t
25a1a 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 70  emporary table p
25a1b 44 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a 20  Dest->iParm..** 
25a1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25a1d 20 20 20 20 54 68 69 73 20 69 73 20 6c 69 6b 65      This is like
25a1e 20 53 52 54 5f 45 70 68 65 6d 54 61 62 20 65 78   SRT_EphemTab ex
25a1f 63 65 70 74 20 74 68 61 74 20 74 68 65 20 74 61  cept that the ta
25a20 62 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ble.**          
25a21 20 20 20 20 20 20 20 20 20 20 20 69 73 20 61 73             is as
25a22 73 75 6d 65 64 20 74 6f 20 61 6c 72 65 61 64 79  sumed to already
25a23 20 62 65 20 6f 70 65 6e 2e 0a 2a 2a 0a 2a 2a 20   be open..**.** 
25a24 20 20 20 20 53 52 54 5f 45 70 68 65 6d 54 61 62      SRT_EphemTab
25a25 20 20 20 20 43 72 65 61 74 65 20 61 6e 20 74 65      Create an te
25a26 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 70 44  mporary table pD
25a27 65 73 74 2d 3e 69 50 61 72 6d 20 61 6e 64 20 73  est->iParm and s
25a28 74 6f 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  tore.**         
25a29 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20              the 
25a2a 72 65 73 75 6c 74 20 74 68 65 72 65 2e 20 54 68  result there. Th
25a2b 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74  e cursor is left
25a2c 20 6f 70 65 6e 20 61 66 74 65 72 0a 2a 2a 20 20   open after.**  
25a2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25a2e 20 20 20 72 65 74 75 72 6e 69 6e 67 2e 20 20 54     returning.  T
25a2f 68 69 73 20 69 73 20 6c 69 6b 65 20 53 52 54 5f  his is like SRT_
25a30 54 61 62 6c 65 20 65 78 63 65 70 74 20 74 68 61  Table except tha
25a31 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
25a32 20 20 20 20 20 20 20 20 20 74 68 69 73 20 64 65           this de
25a33 73 74 69 6e 61 74 69 6f 6e 20 75 73 65 73 20 4f  stination uses O
25a34 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20  P_OpenEphemeral 
25a35 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 20 20 20  to create.**    
25a36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25a37 20 74 68 65 20 74 61 62 6c 65 20 66 69 72 73 74   the table first
25a38 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f  ..**.**     SRT_
25a39 43 6f 72 6f 75 74 69 6e 65 20 20 20 47 65 6e 65  Coroutine   Gene
25a3a 72 61 74 65 20 61 20 63 6f 2d 72 6f 75 74 69 6e  rate a co-routin
25a3b 65 20 74 68 61 74 20 72 65 74 75 72 6e 73 20 61  e that returns a
25a3c 20 6e 65 77 20 72 6f 77 20 6f 66 0a 2a 2a 20 20   new row of.**  
25a3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25a3e 20 20 20 72 65 73 75 6c 74 73 20 65 61 63 68 20     results each 
25a3f 74 69 6d 65 20 69 74 20 69 73 20 69 6e 76 6f 6b  time it is invok
25a40 65 64 2e 20 20 54 68 65 20 65 6e 74 72 79 20 70  ed.  The entry p
25a41 6f 69 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  oint.**         
25a42 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20 74              of t
25a43 68 65 20 63 6f 2d 72 6f 75 74 69 6e 65 20 69 73  he co-routine is
25a44 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
25a45 74 65 72 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  ter pDest->iParm
25a46 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f  ..**.**     SRT_
25a47 45 78 69 73 74 73 20 20 20 20 20 20 53 74 6f 72  Exists      Stor
25a48 65 20 61 20 31 20 69 6e 20 6d 65 6d 6f 72 79 20  e a 1 in memory 
25a49 63 65 6c 6c 20 70 44 65 73 74 2d 3e 69 50 61 72  cell pDest->iPar
25a4a 6d 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 0a  m if the result.
25a4b 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
25a4c 20 20 20 20 20 20 20 73 65 74 20 69 73 20 6e 6f         set is no
25a4d 74 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 20  t empty..**.**  
25a4e 20 20 20 53 52 54 5f 44 69 73 63 61 72 64 20 20     SRT_Discard  
25a4f 20 20 20 54 68 72 6f 77 20 74 68 65 20 72 65 73     Throw the res
25a50 75 6c 74 73 20 61 77 61 79 2e 20 20 54 68 69 73  ults away.  This
25a51 20 69 73 20 75 73 65 64 20 62 79 20 53 45 4c 45   is used by SELE
25a52 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  CT.**           
25a53 20 20 20 20 20 20 20 20 20 20 73 74 61 74 65 6d            statem
25a54 65 6e 74 73 20 77 69 74 68 69 6e 20 74 72 69 67  ents within trig
25a55 67 65 72 73 20 77 68 6f 73 65 20 6f 6e 6c 79 20  gers whose only 
25a56 70 75 72 70 6f 73 65 20 69 73 0a 2a 2a 20 20 20  purpose is.**   
25a57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25a58 20 20 74 68 65 20 73 69 64 65 2d 65 66 66 65 63    the side-effec
25a59 74 73 20 6f 66 20 66 75 6e 63 74 69 6f 6e 73 2e  ts of functions.
25a5a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
25a5b 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ine returns the 
25a5c 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  number of errors
25a5d 2e 20 20 49 66 20 61 6e 79 20 65 72 72 6f 72 73  .  If any errors
25a5e 20 61 72 65 0a 2a 2a 20 65 6e 63 6f 75 6e 74 65   are.** encounte
25a5f 72 65 64 2c 20 74 68 65 6e 20 61 6e 20 61 70 70  red, then an app
25a60 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 6d  ropriate error m
25a61 65 73 73 61 67 65 20 69 73 20 6c 65 66 74 20 69  essage is left i
25a62 6e 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 7a 45 72  n.** pParse->zEr
25a63 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  rMsg..**.** This
25a64 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 4e 4f   routine does NO
25a65 54 20 66 72 65 65 20 74 68 65 20 53 65 6c 65 63  T free the Selec
25a66 74 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73  t structure pass
25a67 65 64 20 69 6e 2e 20 20 54 68 65 0a 2a 2a 20 63  ed in.  The.** c
25a68 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  alling function 
25a69 6e 65 65 64 73 20 74 6f 20 64 6f 20 74 68 61 74  needs to do that
25a6a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
25a6b 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53  ATE int sqlite3S
25a6c 65 6c 65 63 74 28 0a 20 20 50 61 72 73 65 20 2a  elect(.  Parse *
25a6d 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
25a6e 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f  /* The parser co
25a6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63  ntext */.  Selec
25a70 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  t *p,           
25a71 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20    /* The SELECT 
25a72 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20  statement being 
25a73 63 6f 64 65 64 2e 20 2a 2f 0a 20 20 53 65 6c 65  coded. */.  Sele
25a74 63 74 44 65 73 74 20 2a 70 44 65 73 74 20 20 20  ctDest *pDest   
25a75 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f     /* What to do
25a76 20 77 69 74 68 20 74 68 65 20 71 75 65 72 79 20   with the query 
25a77 72 65 73 75 6c 74 73 20 2a 2f 0a 29 7b 0a 20 20  results */.){.  
25a78 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20  int i, j;       
25a79 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
25a7a 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 57 68 65  ounters */.  Whe
25a7b 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20  reInfo *pWInfo; 
25a7c 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 66 72      /* Return fr
25a7d 6f 6d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42  om sqlite3WhereB
25a7e 65 67 69 6e 28 29 20 2a 2f 0a 20 20 56 64 62 65  egin() */.  Vdbe
25a7f 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20   *v;            
25a80 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
25a81 6c 20 6d 61 63 68 69 6e 65 20 75 6e 64 65 72 20  l machine under 
25a82 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a  construction */.
25a83 20 20 69 6e 74 20 69 73 41 67 67 3b 20 20 20 20    int isAgg;    
25a84 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
25a85 20 66 6f 72 20 73 65 6c 65 63 74 20 6c 69 73 74   for select list
25a86 73 20 6c 69 6b 65 20 22 63 6f 75 6e 74 28 2a 29  s like "count(*)
25a87 22 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  " */.  ExprList 
25a88 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20 2f 2a  *pEList;      /*
25a89 20 4c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73   List of columns
25a8a 20 74 6f 20 65 78 74 72 61 63 74 2e 20 2a 2f 0a   to extract. */.
25a8b 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c    SrcList *pTabL
25a8c 69 73 74 3b 20 20 20 20 20 2f 2a 20 4c 69 73 74  ist;     /* List
25a8d 20 6f 66 20 74 61 62 6c 65 73 20 74 6f 20 73 65   of tables to se
25a8e 6c 65 63 74 20 66 72 6f 6d 20 2a 2f 0a 20 20 45  lect from */.  E
25a8f 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20 20 20  xpr *pWhere;    
25a90 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45        /* The WHE
25a91 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20  RE clause.  May 
25a92 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78 70  be NULL */.  Exp
25a93 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 3b  rList *pOrderBy;
25a94 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52      /* The ORDER
25a95 20 42 59 20 63 6c 61 75 73 65 2e 20 20 4d 61 79   BY clause.  May
25a96 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78   be NULL */.  Ex
25a97 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79  prList *pGroupBy
25a98 3b 20 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55  ;    /* The GROU
25a99 50 20 42 59 20 63 6c 61 75 73 65 2e 20 20 4d 61  P BY clause.  Ma
25a9a 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45  y be NULL */.  E
25a9b 78 70 72 20 2a 70 48 61 76 69 6e 67 3b 20 20 20  xpr *pHaving;   
25a9c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 48 41 56        /* The HAV
25a9d 49 4e 47 20 63 6c 61 75 73 65 2e 20 20 4d 61 79  ING clause.  May
25a9e 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e   be NULL */.  in
25a9f 74 20 69 73 44 69 73 74 69 6e 63 74 3b 20 20 20  t isDistinct;   
25aa0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
25aa1 74 68 65 20 44 49 53 54 49 4e 43 54 20 6b 65 79  the DISTINCT key
25aa2 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74 20  word is present 
25aa3 2a 2f 0a 20 20 69 6e 74 20 64 69 73 74 69 6e 63  */.  int distinc
25aa4 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  t;          /* T
25aa5 61 62 6c 65 20 74 6f 20 75 73 65 20 66 6f 72 20  able to use for 
25aa6 74 68 65 20 64 69 73 74 69 6e 63 74 20 73 65 74  the distinct set
25aa7 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 31   */.  int rc = 1
25aa8 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
25aa9 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20  Value to return 
25aaa 66 72 6f 6d 20 74 68 69 73 20 66 75 6e 63 74 69  from this functi
25aab 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  on */.  int addr
25aac 53 6f 72 74 49 6e 64 65 78 3b 20 20 20 20 20 2f  SortIndex;     /
25aad 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 6e 20  * Address of an 
25aae 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
25aaf 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a   instruction */.
25ab0 20 20 41 67 67 49 6e 66 6f 20 73 41 67 67 49 6e    AggInfo sAggIn
25ab1 66 6f 3b 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f  fo;      /* Info
25ab2 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 62 79 20  rmation used by 
25ab3 61 67 67 72 65 67 61 74 65 20 71 75 65 72 69 65  aggregate querie
25ab4 73 20 2a 2f 0a 20 20 69 6e 74 20 69 45 6e 64 3b  s */.  int iEnd;
25ab5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
25ab6 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   Address of the 
25ab7 65 6e 64 20 6f 66 20 74 68 65 20 71 75 65 72 79  end of the query
25ab8 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
25ab9 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  b;           /* 
25aba 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
25abb 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 64 62  nection */..  db
25abc 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
25abd 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 64 62 2d   if( p==0 || db-
25abe 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c  >mallocFailed ||
25abf 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b   pParse->nErr ){
25ac0 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
25ac1 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
25ac2 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
25ac3 2c 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54 2c  , SQLITE_SELECT,
25ac4 20 30 2c 20 30 2c 20 30 29 20 29 20 72 65 74 75   0, 0, 0) ) retu
25ac5 72 6e 20 31 3b 0a 20 20 6d 65 6d 73 65 74 28 26  rn 1;.  memset(&
25ac6 73 41 67 67 49 6e 66 6f 2c 20 30 2c 20 73 69 7a  sAggInfo, 0, siz
25ac7 65 6f 66 28 73 41 67 67 49 6e 66 6f 29 29 3b 0a  eof(sAggInfo));.
25ac8 0a 20 20 70 4f 72 64 65 72 42 79 20 3d 20 70 2d  .  pOrderBy = p-
25ac9 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 69 66 28  >pOrderBy;.  if(
25aca 20 49 67 6e 6f 72 61 62 6c 65 4f 72 64 65 72 62   IgnorableOrderb
25acb 79 28 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20  y(pDest) ){.    
25acc 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 30 3b  p->pOrderBy = 0;
25acd 0a 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 65 73  ..    /* In thes
25ace 65 20 63 61 73 65 73 20 74 68 65 20 44 49 53 54  e cases the DIST
25acf 49 4e 43 54 20 6f 70 65 72 61 74 6f 72 20 6d 61  INCT operator ma
25ad0 6b 65 73 20 6e 6f 20 64 69 66 66 65 72 65 6e 63  kes no differenc
25ad1 65 20 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20  e to the.    ** 
25ad2 72 65 73 75 6c 74 73 2c 20 73 6f 20 72 65 6d 6f  results, so remo
25ad3 76 65 20 69 74 20 69 66 20 69 74 20 77 65 72 65  ve it if it were
25ad4 20 73 70 65 63 69 66 69 65 64 2e 0a 20 20 20 20   specified..    
25ad5 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 70 44  */.    assert(pD
25ad6 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f  est->eDest==SRT_
25ad7 45 78 69 73 74 73 20 7c 7c 20 70 44 65 73 74 2d  Exists || pDest-
25ad8 3e 65 44 65 73 74 3d 3d 53 52 54 5f 55 6e 69 6f  >eDest==SRT_Unio
25ad9 6e 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 20 20  n || .          
25ada 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53   pDest->eDest==S
25adb 52 54 5f 45 78 63 65 70 74 20 7c 7c 20 70 44 65  RT_Except || pDe
25adc 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 44  st->eDest==SRT_D
25add 69 73 63 61 72 64 29 3b 0a 20 20 20 20 70 2d 3e  iscard);.    p->
25ade 73 65 6c 46 6c 61 67 73 20 26 3d 20 7e 53 46 5f  selFlags &= ~SF_
25adf 44 69 73 74 69 6e 63 74 3b 0a 20 20 7d 0a 20 20  Distinct;.  }.  
25ae0 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65  sqlite3SelectPre
25ae1 70 28 70 50 61 72 73 65 2c 20 70 2c 20 30 29 3b  p(pParse, p, 0);
25ae2 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
25ae3 45 72 72 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  Err ){.    goto 
25ae4 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a  select_end;.  }.
25ae5 20 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20    p->pOrderBy = 
25ae6 70 4f 72 64 65 72 42 79 3b 0a 0a 0a 20 20 2f 2a  pOrderBy;...  /*
25ae7 20 4d 61 6b 65 20 6c 6f 63 61 6c 20 63 6f 70 69   Make local copi
25ae8 65 73 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65  es of the parame
25ae9 74 65 72 73 20 66 6f 72 20 74 68 69 73 20 71 75  ters for this qu
25aea 65 72 79 2e 0a 20 20 2a 2f 0a 20 20 70 54 61 62  ery..  */.  pTab
25aeb 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a  List = p->pSrc;.
25aec 20 20 69 73 41 67 67 20 3d 20 28 70 2d 3e 73 65    isAgg = (p->se
25aed 6c 46 6c 61 67 73 20 26 20 53 46 5f 41 67 67 72  lFlags & SF_Aggr
25aee 65 67 61 74 65 29 21 3d 30 3b 0a 20 20 70 45 4c  egate)!=0;.  pEL
25aef 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b  ist = p->pEList;
25af0 0a 20 20 69 66 28 20 70 45 4c 69 73 74 3d 3d 30  .  if( pEList==0
25af1 20 29 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65   ) goto select_e
25af2 6e 64 3b 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20  nd;..  /* .  ** 
25af3 44 6f 20 6e 6f 74 20 65 76 65 6e 20 61 74 74 65  Do not even atte
25af4 6d 70 74 20 74 6f 20 67 65 6e 65 72 61 74 65 20  mpt to generate 
25af5 61 6e 79 20 63 6f 64 65 20 69 66 20 77 65 20 68  any code if we h
25af6 61 76 65 20 61 6c 72 65 61 64 79 20 73 65 65 6e  ave already seen
25af7 0a 20 20 2a 2a 20 65 72 72 6f 72 73 20 62 65 66  .  ** errors bef
25af8 6f 72 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ore this routine
25af9 20 73 74 61 72 74 73 2e 0a 20 20 2a 2f 0a 20 20   starts..  */.  
25afa 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
25afb 3e 30 20 29 20 67 6f 74 6f 20 73 65 6c 65 63 74  >0 ) goto select
25afc 5f 65 6e 64 3b 0a 0a 20 20 2f 2a 20 4f 52 44 45  _end;..  /* ORDE
25afd 52 20 42 59 20 69 73 20 69 67 6e 6f 72 65 64 20  R BY is ignored 
25afe 66 6f 72 20 73 6f 6d 65 20 64 65 73 74 69 6e 61  for some destina
25aff 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  tions..  */.  if
25b00 28 20 49 67 6e 6f 72 61 62 6c 65 4f 72 64 65 72  ( IgnorableOrder
25b01 62 79 28 70 44 65 73 74 29 20 29 7b 0a 20 20 20  by(pDest) ){.   
25b02 20 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20   pOrderBy = 0;. 
25b03 20 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67   }..  /* Begin g
25b04 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 2e 0a  enerating code..
25b05 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74    */.  v = sqlit
25b06 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
25b07 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20  );.  if( v==0 ) 
25b08 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b  goto select_end;
25b09 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
25b0a 63 6f 64 65 20 66 6f 72 20 61 6c 6c 20 73 75 62  code for all sub
25b0b 2d 71 75 65 72 69 65 73 20 69 6e 20 74 68 65 20  -queries in the 
25b0c 46 52 4f 4d 20 63 6c 61 75 73 65 0a 20 20 2a 2f  FROM clause.  */
25b0d 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
25b0e 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
25b0f 52 59 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28  RY) || !defined(
25b10 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
25b11 29 0a 20 20 66 6f 72 28 69 3d 30 3b 20 21 70 2d  ).  for(i=0; !p-
25b12 3e 70 50 72 69 6f 72 20 26 26 20 69 3c 70 54 61  >pPrior && i<pTa
25b13 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  bList->nSrc; i++
25b14 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 53 72  ){.    struct Sr
25b15 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  cList_item *pIte
25b16 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61  m = &pTabList->a
25b17 5b 69 5d 3b 0a 20 20 20 20 53 65 6c 65 63 74 44  [i];.    SelectD
25b18 65 73 74 20 64 65 73 74 3b 0a 20 20 20 20 53 65  est dest;.    Se
25b19 6c 65 63 74 20 2a 70 53 75 62 20 3d 20 70 49 74  lect *pSub = pIt
25b1a 65 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a 20 20 20  em->pSelect;.   
25b1b 20 69 6e 74 20 69 73 41 67 67 53 75 62 3b 0a 0a   int isAggSub;..
25b1c 20 20 20 20 69 66 28 20 70 53 75 62 3d 3d 30 20      if( pSub==0 
25b1d 7c 7c 20 70 49 74 65 6d 2d 3e 69 73 50 6f 70 75  || pItem->isPopu
25b1e 6c 61 74 65 64 20 29 20 63 6f 6e 74 69 6e 75 65  lated ) continue
25b1f 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d  ;..    /* Increm
25b20 65 6e 74 20 50 61 72 73 65 2e 6e 48 65 69 67 68  ent Parse.nHeigh
25b21 74 20 62 79 20 74 68 65 20 68 65 69 67 68 74 20  t by the height 
25b22 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 65  of the largest e
25b23 78 70 72 65 73 73 69 6f 6e 0a 20 20 20 20 2a 2a  xpression.    **
25b24 20 74 72 65 65 20 72 65 66 65 72 65 64 20 74 6f   tree refered to
25b25 20 62 79 20 74 68 69 73 2c 20 74 68 65 20 70 61   by this, the pa
25b26 72 65 6e 74 20 73 65 6c 65 63 74 2e 20 54 68 65  rent select. The
25b27 20 63 68 69 6c 64 20 73 65 6c 65 63 74 0a 20 20   child select.  
25b28 20 20 2a 2a 20 6d 61 79 20 63 6f 6e 74 61 69 6e    ** may contain
25b29 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65   expression tree
25b2a 73 20 6f 66 20 61 74 20 6d 6f 73 74 0a 20 20 20  s of at most.   
25b2b 20 2a 2a 20 28 53 51 4c 49 54 45 5f 4d 41 58 5f   ** (SQLITE_MAX_
25b2c 45 58 50 52 5f 44 45 50 54 48 2d 50 61 72 73 65  EXPR_DEPTH-Parse
25b2d 2e 6e 48 65 69 67 68 74 29 20 68 65 69 67 68 74  .nHeight) height
25b2e 2e 20 54 68 69 73 20 69 73 20 61 20 62 69 74 0a  . This is a bit.
25b2f 20 20 20 20 2a 2a 20 6d 6f 72 65 20 63 6f 6e 73      ** more cons
25b30 65 72 76 61 74 69 76 65 20 74 68 61 6e 20 6e 65  ervative than ne
25b31 63 65 73 73 61 72 79 2c 20 62 75 74 20 6d 75 63  cessary, but muc
25b32 68 20 65 61 73 69 65 72 20 74 68 61 6e 20 65 6e  h easier than en
25b33 66 6f 72 63 69 6e 67 0a 20 20 20 20 2a 2a 20 61  forcing.    ** a
25b34 6e 20 65 78 61 63 74 20 6c 69 6d 69 74 2e 0a 20  n exact limit.. 
25b35 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 72 73 65     */.    pParse
25b36 2d 3e 6e 48 65 69 67 68 74 20 2b 3d 20 73 71 6c  ->nHeight += sql
25b37 69 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 65  ite3SelectExprHe
25b38 69 67 68 74 28 70 29 3b 0a 0a 20 20 20 20 2f 2a  ight(p);..    /*
25b39 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
25b3a 20 74 68 65 20 73 75 62 71 75 65 72 79 20 63 61   the subquery ca
25b3b 6e 20 62 65 20 61 62 73 6f 72 62 65 64 20 69 6e  n be absorbed in
25b3c 74 6f 20 74 68 65 20 70 61 72 65 6e 74 2e 20 2a  to the parent. *
25b3d 2f 0a 20 20 20 20 69 73 41 67 67 53 75 62 20 3d  /.    isAggSub =
25b3e 20 28 70 53 75 62 2d 3e 73 65 6c 46 6c 61 67 73   (pSub->selFlags
25b3f 20 26 20 53 46 5f 41 67 67 72 65 67 61 74 65 29   & SF_Aggregate)
25b40 21 3d 30 3b 0a 20 20 20 20 69 66 28 20 66 6c 61  !=0;.    if( fla
25b41 74 74 65 6e 53 75 62 71 75 65 72 79 28 70 50 61  ttenSubquery(pPa
25b42 72 73 65 2c 20 70 2c 20 69 2c 20 69 73 41 67 67  rse, p, i, isAgg
25b43 2c 20 69 73 41 67 67 53 75 62 29 20 29 7b 0a 20  , isAggSub) ){. 
25b44 20 20 20 20 20 69 66 28 20 69 73 41 67 67 53 75       if( isAggSu
25b45 62 20 29 7b 0a 20 20 20 20 20 20 20 20 69 73 41  b ){.        isA
25b46 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  gg = 1;.        
25b47 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53  p->selFlags |= S
25b48 46 5f 41 67 67 72 65 67 61 74 65 3b 0a 20 20 20  F_Aggregate;.   
25b49 20 20 20 7d 0a 20 20 20 20 20 20 69 20 3d 20 2d     }.      i = -
25b4a 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  1;.    }else{.  
25b4b 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
25b4c 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c  tDestInit(&dest,
25b4d 20 53 52 54 5f 45 70 68 65 6d 54 61 62 2c 20 70   SRT_EphemTab, p
25b4e 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 29 3b 0a  Item->iCursor);.
25b4f 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49        assert( pI
25b50 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64  tem->isPopulated
25b51 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==0 );.      sql
25b52 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73  ite3Select(pPars
25b53 65 2c 20 70 53 75 62 2c 20 26 64 65 73 74 29 3b  e, pSub, &dest);
25b54 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 73  .      pItem->is
25b55 50 6f 70 75 6c 61 74 65 64 20 3d 20 31 3b 0a 20  Populated = 1;. 
25b56 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61     }.    if( pPa
25b57 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d  rse->nErr || db-
25b58 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
25b59 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 65 6c 65  .      goto sele
25b5a 63 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a 20 20  ct_end;.    }.  
25b5b 20 20 70 50 61 72 73 65 2d 3e 6e 48 65 69 67 68    pParse->nHeigh
25b5c 74 20 2d 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  t -= sqlite3Sele
25b5d 63 74 45 78 70 72 48 65 69 67 68 74 28 70 29 3b  ctExprHeight(p);
25b5e 0a 20 20 20 20 70 54 61 62 4c 69 73 74 20 3d 20  .    pTabList = 
25b5f 70 2d 3e 70 53 72 63 3b 0a 20 20 20 20 69 66 28  p->pSrc;.    if(
25b60 20 21 49 67 6e 6f 72 61 62 6c 65 4f 72 64 65 72   !IgnorableOrder
25b61 62 79 28 70 44 65 73 74 29 20 29 7b 0a 20 20 20  by(pDest) ){.   
25b62 20 20 20 70 4f 72 64 65 72 42 79 20 3d 20 70 2d     pOrderBy = p-
25b63 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 20 20 7d  >pOrderBy;.    }
25b64 0a 20 20 7d 0a 20 20 70 45 4c 69 73 74 20 3d 20  .  }.  pEList = 
25b65 70 2d 3e 70 45 4c 69 73 74 3b 0a 23 65 6e 64 69  p->pEList;.#endi
25b66 66 0a 20 20 70 57 68 65 72 65 20 3d 20 70 2d 3e  f.  pWhere = p->
25b67 70 57 68 65 72 65 3b 0a 20 20 70 47 72 6f 75 70  pWhere;.  pGroup
25b68 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70 42 79  By = p->pGroupBy
25b69 3b 0a 20 20 70 48 61 76 69 6e 67 20 3d 20 70 2d  ;.  pHaving = p-
25b6a 3e 70 48 61 76 69 6e 67 3b 0a 20 20 69 73 44 69  >pHaving;.  isDi
25b6b 73 74 69 6e 63 74 20 3d 20 28 70 2d 3e 73 65 6c  stinct = (p->sel
25b6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73 74 69  Flags & SF_Disti
25b6d 6e 63 74 29 21 3d 30 3b 0a 0a 23 69 66 6e 64 65  nct)!=0;..#ifnde
25b6e 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f  f SQLITE_OMIT_CO
25b6f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 20 20  MPOUND_SELECT.  
25b70 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61  /* If there is a
25b71 72 65 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66  re a sequence of
25b72 20 71 75 65 72 69 65 73 2c 20 64 6f 20 74 68 65   queries, do the
25b73 20 65 61 72 6c 69 65 72 20 6f 6e 65 73 20 66 69   earlier ones fi
25b74 72 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  rst..  */.  if( 
25b75 70 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20  p->pPrior ){.   
25b76 20 69 66 28 20 70 2d 3e 70 52 69 67 68 74 6d 6f   if( p->pRightmo
25b77 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 53  st==0 ){.      S
25b78 65 6c 65 63 74 20 2a 70 4c 6f 6f 70 2c 20 2a 70  elect *pLoop, *p
25b79 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 20 20 20  Right = 0;.     
25b7a 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20   int cnt = 0;.  
25b7b 20 20 20 20 69 6e 74 20 6d 78 53 65 6c 65 63 74      int mxSelect
25b7c 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 4c 6f 6f  ;.      for(pLoo
25b7d 70 3d 70 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f  p=p; pLoop; pLoo
25b7e 70 3d 70 4c 6f 6f 70 2d 3e 70 50 72 69 6f 72 2c  p=pLoop->pPrior,
25b7f 20 63 6e 74 2b 2b 29 7b 0a 20 20 20 20 20 20 20   cnt++){.       
25b80 20 70 4c 6f 6f 70 2d 3e 70 52 69 67 68 74 6d 6f   pLoop->pRightmo
25b81 73 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20  st = p;.        
25b82 70 4c 6f 6f 70 2d 3e 70 4e 65 78 74 20 3d 20 70  pLoop->pNext = p
25b83 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20 70  Right;.        p
25b84 52 69 67 68 74 20 3d 20 70 4c 6f 6f 70 3b 0a 20  Right = pLoop;. 
25b85 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 78 53       }.      mxS
25b86 65 6c 65 63 74 20 3d 20 64 62 2d 3e 61 4c 69 6d  elect = db->aLim
25b87 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
25b88 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 5d  COMPOUND_SELECT]
25b89 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 78 53 65  ;.      if( mxSe
25b8a 6c 65 63 74 20 26 26 20 63 6e 74 3e 6d 78 53 65  lect && cnt>mxSe
25b8b 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20  lect ){.        
25b8c 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
25b8d 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e  pParse, "too man
25b8e 79 20 74 65 72 6d 73 20 69 6e 20 63 6f 6d 70 6f  y terms in compo
25b8f 75 6e 64 20 53 45 4c 45 43 54 22 29 3b 0a 20 20  und SELECT");.  
25b90 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
25b91 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
25b92 20 20 72 65 74 75 72 6e 20 6d 75 6c 74 69 53 65    return multiSe
25b93 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20  lect(pParse, p, 
25b94 70 44 65 73 74 29 3b 0a 20 20 7d 0a 23 65 6e 64  pDest);.  }.#end
25b95 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 72 69 74  if..  /* If writ
25b96 69 6e 67 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 72  ing to memory or
25b97 20 67 65 6e 65 72 61 74 69 6e 67 20 61 20 73 65   generating a se
25b98 74 0a 20 20 2a 2a 20 6f 6e 6c 79 20 61 20 73 69  t.  ** only a si
25b99 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6d 61 79 20  ngle column may 
25b9a 62 65 20 6f 75 74 70 75 74 2e 0a 20 20 2a 2f 0a  be output..  */.
25b9b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
25b9c 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 69  MIT_SUBQUERY.  i
25b9d 66 28 20 63 68 65 63 6b 46 6f 72 4d 75 6c 74 69  f( checkForMulti
25b9e 43 6f 6c 75 6d 6e 53 65 6c 65 63 74 45 72 72 6f  ColumnSelectErro
25b9f 72 28 70 50 61 72 73 65 2c 20 70 44 65 73 74 2c  r(pParse, pDest,
25ba0 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 20   pEList->nExpr) 
25ba1 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 65 6c 65  ){.    goto sele
25ba2 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 23 65 6e 64  ct_end;.  }.#end
25ba3 69 66 0a 0a 20 20 2f 2a 20 49 66 20 70 6f 73 73  if..  /* If poss
25ba4 69 62 6c 65 2c 20 72 65 77 72 69 74 65 20 74 68  ible, rewrite th
25ba5 65 20 71 75 65 72 79 20 74 6f 20 75 73 65 20 47  e query to use G
25ba6 52 4f 55 50 20 42 59 20 69 6e 73 74 65 61 64 20  ROUP BY instead 
25ba7 6f 66 20 44 49 53 54 49 4e 43 54 2e 0a 20 20 2a  of DISTINCT..  *
25ba8 2a 20 47 52 4f 55 50 20 42 59 20 6d 69 67 68 74  * GROUP BY might
25ba9 20 75 73 65 20 61 6e 20 69 6e 64 65 78 2c 20 44   use an index, D
25baa 49 53 54 49 4e 43 54 20 6e 65 76 65 72 20 64 6f  ISTINCT never do
25bab 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  es..  */.  if( (
25bac 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53  p->selFlags & (S
25bad 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67  F_Distinct|SF_Ag
25bae 67 72 65 67 61 74 65 29 29 3d 3d 53 46 5f 44 69  gregate))==SF_Di
25baf 73 74 69 6e 63 74 20 26 26 20 21 70 2d 3e 70 47  stinct && !p->pG
25bb0 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 70 2d  roupBy ){.    p-
25bb1 3e 70 47 72 6f 75 70 42 79 20 3d 20 73 71 6c 69  >pGroupBy = sqli
25bb2 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64  te3ExprListDup(d
25bb3 62 2c 20 70 2d 3e 70 45 4c 69 73 74 29 3b 0a 20  b, p->pEList);. 
25bb4 20 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70 2d     pGroupBy = p-
25bb5 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20 20 20 70  >pGroupBy;.    p
25bb6 2d 3e 73 65 6c 46 6c 61 67 73 20 26 3d 20 7e 53  ->selFlags &= ~S
25bb7 46 5f 44 69 73 74 69 6e 63 74 3b 0a 20 20 20 20  F_Distinct;.    
25bb8 69 73 44 69 73 74 69 6e 63 74 20 3d 20 30 3b 0a  isDistinct = 0;.
25bb9 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
25bba 72 65 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42  re is an ORDER B
25bbb 59 20 63 6c 61 75 73 65 2c 20 74 68 65 6e 20 74  Y clause, then t
25bbc 68 69 73 20 73 6f 72 74 69 6e 67 0a 20 20 2a 2a  his sorting.  **
25bbd 20 69 6e 64 65 78 20 6d 69 67 68 74 20 65 6e 64   index might end
25bbe 20 75 70 20 62 65 69 6e 67 20 75 6e 75 73 65 64   up being unused
25bbf 20 69 66 20 74 68 65 20 64 61 74 61 20 63 61 6e   if the data can
25bc0 20 62 65 20 0a 20 20 2a 2a 20 65 78 74 72 61 63   be .  ** extrac
25bc1 74 65 64 20 69 6e 20 70 72 65 2d 73 6f 72 74 65  ted in pre-sorte
25bc2 64 20 6f 72 64 65 72 2e 20 20 49 66 20 74 68 61  d order.  If tha
25bc3 74 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74  t is the case, t
25bc4 68 65 6e 20 74 68 65 0a 20 20 2a 2a 20 4f 50 5f  hen the.  ** OP_
25bc5 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e  OpenEphemeral in
25bc6 73 74 72 75 63 74 69 6f 6e 20 77 69 6c 6c 20 62  struction will b
25bc7 65 20 63 68 61 6e 67 65 64 20 74 6f 20 61 6e 20  e changed to an 
25bc8 4f 50 5f 4e 6f 6f 70 20 6f 6e 63 65 0a 20 20 2a  OP_Noop once.  *
25bc9 2a 20 77 65 20 66 69 67 75 72 65 20 6f 75 74 20  * we figure out 
25bca 74 68 61 74 20 74 68 65 20 73 6f 72 74 69 6e 67  that the sorting
25bcb 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20 6e 65   index is not ne
25bcc 65 64 65 64 2e 20 20 54 68 65 20 61 64 64 72 53  eded.  The addrS
25bcd 6f 72 74 49 6e 64 65 78 0a 20 20 2a 2a 20 76 61  ortIndex.  ** va
25bce 72 69 61 62 6c 65 20 69 73 20 75 73 65 64 20 74  riable is used t
25bcf 6f 20 66 61 63 69 6c 69 74 61 74 65 20 74 68 61  o facilitate tha
25bd0 74 20 63 68 61 6e 67 65 2e 0a 20 20 2a 2f 0a 20  t change..  */. 
25bd1 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b   if( pOrderBy ){
25bd2 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  .    KeyInfo *pK
25bd3 65 79 49 6e 66 6f 3b 0a 20 20 20 20 70 4b 65 79  eyInfo;.    pKey
25bd4 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66 6f 46 72  Info = keyInfoFr
25bd5 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61 72 73  omExprList(pPars
25bd6 65 2c 20 70 4f 72 64 65 72 42 79 29 3b 0a 20 20  e, pOrderBy);.  
25bd7 20 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75    pOrderBy->iECu
25bd8 72 73 6f 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e  rsor = pParse->n
25bd9 54 61 62 2b 2b 3b 0a 20 20 20 20 70 2d 3e 61 64  Tab++;.    p->ad
25bda 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d 20 3d 20  drOpenEphm[2] = 
25bdb 61 64 64 72 53 6f 72 74 49 6e 64 65 78 20 3d 0a  addrSortIndex =.
25bdc 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
25bdd 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70  eAddOp4(v, OP_Op
25bde 65 6e 45 70 68 65 6d 65 72 61 6c 2c 0a 20 20 20  enEphemeral,.   
25bdf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25be0 20 20 20 20 20 20 20 20 70 4f 72 64 65 72 42 79          pOrderBy
25be1 2d 3e 69 45 43 75 72 73 6f 72 2c 20 70 4f 72 64  ->iECursor, pOrd
25be2 65 72 42 79 2d 3e 6e 45 78 70 72 2b 32 2c 20 30  erBy->nExpr+2, 0
25be3 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
25be4 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68               (ch
25be5 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34  ar*)pKeyInfo, P4
25be6 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
25be7 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
25be8 61 64 64 72 53 6f 72 74 49 6e 64 65 78 20 3d 20  addrSortIndex = 
25be9 2d 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  -1;.  }..  /* If
25bea 20 74 68 65 20 6f 75 74 70 75 74 20 69 73 20 64   the output is d
25beb 65 73 74 69 6e 65 64 20 66 6f 72 20 61 20 74 65  estined for a te
25bec 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2c 20 6f  mporary table, o
25bed 70 65 6e 20 74 68 61 74 20 74 61 62 6c 65 2e 0a  pen that table..
25bee 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 65 73 74    */.  if( pDest
25bef 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 45 70 68  ->eDest==SRT_Eph
25bf0 65 6d 54 61 62 20 29 7b 0a 20 20 20 20 73 71 6c  emTab ){.    sql
25bf1 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
25bf2 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  , OP_OpenEphemer
25bf3 61 6c 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  al, pDest->iParm
25bf4 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29  , pEList->nExpr)
25bf5 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20  ;.  }..  /* Set 
25bf6 74 68 65 20 6c 69 6d 69 74 65 72 2e 0a 20 20 2a  the limiter..  *
25bf7 2f 0a 20 20 69 45 6e 64 20 3d 20 73 71 6c 69 74  /.  iEnd = sqlit
25bf8 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
25bf9 76 29 3b 0a 20 20 63 6f 6d 70 75 74 65 4c 69 6d  v);.  computeLim
25bfa 69 74 52 65 67 69 73 74 65 72 73 28 70 50 61 72  itRegisters(pPar
25bfb 73 65 2c 20 70 2c 20 69 45 6e 64 29 3b 0a 0a 20  se, p, iEnd);.. 
25bfc 20 2f 2a 20 4f 70 65 6e 20 61 20 76 69 72 74 75   /* Open a virtu
25bfd 61 6c 20 69 6e 64 65 78 20 74 6f 20 75 73 65 20  al index to use 
25bfe 66 6f 72 20 74 68 65 20 64 69 73 74 69 6e 63 74  for the distinct
25bff 20 73 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28   set..  */.  if(
25c00 20 69 73 44 69 73 74 69 6e 63 74 20 29 7b 0a 20   isDistinct ){. 
25c01 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79     KeyInfo *pKey
25c02 49 6e 66 6f 3b 0a 20 20 20 20 61 73 73 65 72 74  Info;.    assert
25c03 28 20 69 73 41 67 67 20 7c 7c 20 70 47 72 6f 75  ( isAgg || pGrou
25c04 70 42 79 20 29 3b 0a 20 20 20 20 64 69 73 74 69  pBy );.    disti
25c05 6e 63 74 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  nct = pParse->nT
25c06 61 62 2b 2b 3b 0a 20 20 20 20 70 4b 65 79 49 6e  ab++;.    pKeyIn
25c07 66 6f 20 3d 20 6b 65 79 49 6e 66 6f 46 72 6f 6d  fo = keyInfoFrom
25c08 45 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c  ExprList(pParse,
25c09 20 70 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20   p->pEList);.   
25c0a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
25c0b 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68  p4(v, OP_OpenEph
25c0c 65 6d 65 72 61 6c 2c 20 64 69 73 74 69 6e 63 74  emeral, distinct
25c0d 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  , 0, 0,.        
25c0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25c0f 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c  (char*)pKeyInfo,
25c10 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
25c11 4f 46 46 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  OFF);.  }else{. 
25c12 20 20 20 64 69 73 74 69 6e 63 74 20 3d 20 2d 31     distinct = -1
25c13 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 67 67 72  ;.  }..  /* Aggr
25c14 65 67 61 74 65 20 61 6e 64 20 6e 6f 6e 2d 61 67  egate and non-ag
25c15 67 72 65 67 61 74 65 20 71 75 65 72 69 65 73 20  gregate queries 
25c16 61 72 65 20 68 61 6e 64 6c 65 64 20 64 69 66 66  are handled diff
25c17 65 72 65 6e 74 6c 79 20 2a 2f 0a 20 20 69 66 28  erently */.  if(
25c18 20 21 69 73 41 67 67 20 26 26 20 70 47 72 6f 75   !isAgg && pGrou
25c19 70 42 79 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  pBy==0 ){.    /*
25c1a 20 54 68 69 73 20 63 61 73 65 20 69 73 20 66 6f   This case is fo
25c1b 72 20 6e 6f 6e 2d 61 67 67 72 65 67 61 74 65 20  r non-aggregate 
25c1c 71 75 65 72 69 65 73 0a 20 20 20 20 2a 2a 20 42  queries.    ** B
25c1d 65 67 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  egin the databas
25c1e 65 20 73 63 61 6e 0a 20 20 20 20 2a 2f 0a 20 20  e scan.    */.  
25c1f 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74    pWInfo = sqlit
25c20 65 33 57 68 65 72 65 42 65 67 69 6e 28 70 50 61  e3WhereBegin(pPa
25c21 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70  rse, pTabList, p
25c22 57 68 65 72 65 2c 20 26 70 4f 72 64 65 72 42 79  Where, &pOrderBy
25c23 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 57  , 0);.    if( pW
25c24 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 73  Info==0 ) goto s
25c25 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20 20 20 20  elect_end;..    
25c26 2f 2a 20 49 66 20 73 6f 72 74 69 6e 67 20 69 6e  /* If sorting in
25c27 64 65 78 20 74 68 61 74 20 77 61 73 20 63 72 65  dex that was cre
25c28 61 74 65 64 20 62 79 20 61 20 70 72 69 6f 72 20  ated by a prior 
25c29 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
25c2a 20 0a 20 20 20 20 2a 2a 20 69 6e 73 74 72 75 63   .    ** instruc
25c2b 74 69 6f 6e 20 65 6e 64 65 64 20 75 70 20 6e 6f  tion ended up no
25c2c 74 20 62 65 69 6e 67 20 6e 65 65 64 65 64 2c 20  t being needed, 
25c2d 74 68 65 6e 20 63 68 61 6e 67 65 20 74 68 65 20  then change the 
25c2e 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
25c2f 0a 20 20 20 20 2a 2a 20 69 6e 74 6f 20 61 6e 20  .    ** into an 
25c30 4f 50 5f 4e 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a  OP_Noop..    */.
25c31 20 20 20 20 69 66 28 20 61 64 64 72 53 6f 72 74      if( addrSort
25c32 49 6e 64 65 78 3e 3d 30 20 26 26 20 70 4f 72 64  Index>=0 && pOrd
25c33 65 72 42 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20  erBy==0 ){.     
25c34 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
25c35 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 61 64 64 72  geToNoop(v, addr
25c36 53 6f 72 74 49 6e 64 65 78 2c 20 31 29 3b 0a 20  SortIndex, 1);. 
25c37 20 20 20 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e       p->addrOpen
25c38 45 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a 20 20  Ephm[2] = -1;.  
25c39 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 55 73 65 20    }..    /* Use 
25c3a 74 68 65 20 73 74 61 6e 64 61 72 64 20 69 6e 6e  the standard inn
25c3b 65 72 20 6c 6f 6f 70 0a 20 20 20 20 2a 2f 0a 20  er loop.    */. 
25c3c 20 20 20 61 73 73 65 72 74 28 21 69 73 44 69 73     assert(!isDis
25c3d 74 69 6e 63 74 29 3b 0a 20 20 20 20 73 65 6c 65  tinct);.    sele
25c3e 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70 50 61 72  ctInnerLoop(pPar
25c3f 73 65 2c 20 70 2c 20 70 45 4c 69 73 74 2c 20 30  se, p, pEList, 0
25c40 2c 20 30 2c 20 70 4f 72 64 65 72 42 79 2c 20 2d  , 0, pOrderBy, -
25c41 31 2c 20 70 44 65 73 74 2c 0a 20 20 20 20 20 20  1, pDest,.      
25c42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 57                pW
25c43 49 6e 66 6f 2d 3e 69 43 6f 6e 74 69 6e 75 65 2c  Info->iContinue,
25c44 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 29   pWInfo->iBreak)
25c45 3b 0a 0a 20 20 20 20 2f 2a 20 45 6e 64 20 74 68  ;..    /* End th
25c46 65 20 64 61 74 61 62 61 73 65 20 73 63 61 6e 20  e database scan 
25c47 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  loop..    */.   
25c48 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64   sqlite3WhereEnd
25c49 28 70 57 49 6e 66 6f 29 3b 0a 20 20 7d 65 6c 73  (pWInfo);.  }els
25c4a 65 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69  e{.    /* This i
25c4b 73 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67  s the processing
25c4c 20 66 6f 72 20 61 67 67 72 65 67 61 74 65 20 71   for aggregate q
25c4d 75 65 72 69 65 73 20 2a 2f 0a 20 20 20 20 4e 61  ueries */.    Na
25c4e 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20  meContext sNC;  
25c4f 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78    /* Name contex
25c50 74 20 66 6f 72 20 70 72 6f 63 65 73 73 69 6e 67  t for processing
25c51 20 61 67 67 72 65 67 61 74 65 20 69 6e 66 6f 72   aggregate infor
25c52 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e  mation */.    in
25c53 74 20 69 41 4d 65 6d 3b 20 20 20 20 20 20 20 20  t iAMem;        
25c54 20 20 2f 2a 20 46 69 72 73 74 20 4d 65 6d 20 61    /* First Mem a
25c55 64 64 72 65 73 73 20 66 6f 72 20 73 74 6f 72 69  ddress for stori
25c56 6e 67 20 63 75 72 72 65 6e 74 20 47 52 4f 55 50  ng current GROUP
25c57 20 42 59 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69   BY */.    int i
25c58 42 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f  BMem;          /
25c59 2a 20 46 69 72 73 74 20 4d 65 6d 20 61 64 64 72  * First Mem addr
25c5a 65 73 73 20 66 6f 72 20 70 72 65 76 69 6f 75 73  ess for previous
25c5b 20 47 52 4f 55 50 20 42 59 20 2a 2f 0a 20 20 20   GROUP BY */.   
25c5c 20 69 6e 74 20 69 55 73 65 46 6c 61 67 3b 20 20   int iUseFlag;  
25c5d 20 20 20 20 20 2f 2a 20 4d 65 6d 20 61 64 64 72       /* Mem addr
25c5e 65 73 73 20 68 6f 6c 64 69 6e 67 20 66 6c 61 67  ess holding flag
25c5f 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74   indicating that
25c60 20 61 74 20 6c 65 61 73 74 0a 20 20 20 20 20 20   at least.      
25c61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25c62 20 20 2a 2a 20 6f 6e 65 20 72 6f 77 20 6f 66 20    ** one row of 
25c63 74 68 65 20 69 6e 70 75 74 20 74 6f 20 74 68 65  the input to the
25c64 20 61 67 67 72 65 67 61 74 6f 72 20 68 61 73 20   aggregator has 
25c65 62 65 65 6e 0a 20 20 20 20 20 20 20 20 20 20 20  been.           
25c66 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
25c67 70 72 6f 63 65 73 73 65 64 20 2a 2f 0a 20 20 20  processed */.   
25c68 20 69 6e 74 20 69 41 62 6f 72 74 46 6c 61 67 3b   int iAbortFlag;
25c69 20 20 20 20 20 2f 2a 20 4d 65 6d 20 61 64 64 72       /* Mem addr
25c6a 65 73 73 20 77 68 69 63 68 20 63 61 75 73 65 73  ess which causes
25c6b 20 71 75 65 72 79 20 61 62 6f 72 74 20 69 66 20   query abort if 
25c6c 70 6f 73 69 74 69 76 65 20 2a 2f 0a 20 20 20 20  positive */.    
25c6d 69 6e 74 20 67 72 6f 75 70 42 79 53 6f 72 74 3b  int groupBySort;
25c6e 20 20 20 20 2f 2a 20 52 6f 77 73 20 63 6f 6d 65      /* Rows come
25c6f 20 66 72 6f 6d 20 73 6f 75 72 63 65 20 69 6e 20   from source in 
25c70 47 52 4f 55 50 20 42 59 20 6f 72 64 65 72 20 2a  GROUP BY order *
25c71 2f 0a 20 20 20 20 69 6e 74 20 61 64 64 72 45 6e  /.    int addrEn
25c72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 64  d;        /* End
25c73 20 6f 66 20 70 72 6f 63 65 73 73 69 6e 67 20 66   of processing f
25c74 6f 72 20 74 68 69 73 20 53 45 4c 45 43 54 20 2a  or this SELECT *
25c75 2f 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65  /..    /* Remove
25c76 20 61 6e 79 20 61 6e 64 20 61 6c 6c 20 61 6c 69   any and all ali
25c77 61 73 65 73 20 62 65 74 77 65 65 6e 20 74 68 65  ases between the
25c78 20 72 65 73 75 6c 74 20 73 65 74 20 61 6e 64 20   result set and 
25c79 74 68 65 0a 20 20 20 20 2a 2a 20 47 52 4f 55 50  the.    ** GROUP
25c7a 20 42 59 20 63 6c 61 75 73 65 2e 0a 20 20 20 20   BY clause..    
25c7b 2a 2f 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75  */.    if( pGrou
25c7c 70 42 79 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  pBy ){.      int
25c7d 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
25c7e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
25c7f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
25c80 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
25c81 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
25c82 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67    /* For looping
25c83 20 6f 76 65 72 20 65 78 70 72 65 73 73 69 6f 6e   over expression
25c84 20 69 6e 20 61 20 6c 69 73 74 20 2a 2f 0a 0a 20   in a list */.. 
25c85 20 20 20 20 20 66 6f 72 28 69 3d 70 2d 3e 70 45       for(i=p->pE
25c86 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20 70 49 74  List->nExpr, pIt
25c87 65 6d 3d 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 3b  em=p->pEList->a;
25c88 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d   i>0; i--, pItem
25c89 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 49 74  ++){.        pIt
25c8a 65 6d 2d 3e 69 41 6c 69 61 73 20 3d 20 30 3b 0a  em->iAlias = 0;.
25c8b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f        }.      fo
25c8c 72 28 69 3d 70 47 72 6f 75 70 42 79 2d 3e 6e 45  r(i=pGroupBy->nE
25c8d 78 70 72 2c 20 70 49 74 65 6d 3d 70 47 72 6f 75  xpr, pItem=pGrou
25c8e 70 42 79 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d  pBy->a; i>0; i--
25c8f 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
25c90 20 20 20 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61      pItem->iAlia
25c91 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  s = 0;.      }. 
25c92 20 20 20 7d 0a 0a 20 0a 20 20 20 20 2f 2a 20 43     }.. .    /* C
25c93 72 65 61 74 65 20 61 20 6c 61 62 65 6c 20 74 6f  reate a label to
25c94 20 6a 75 6d 70 20 74 6f 20 77 68 65 6e 20 77 65   jump to when we
25c95 20 77 61 6e 74 20 74 6f 20 61 62 6f 72 74 20 74   want to abort t
25c96 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 20 20  he query */.    
25c97 61 64 64 72 45 6e 64 20 3d 20 73 71 6c 69 74 65  addrEnd = sqlite
25c98 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
25c99 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 6e 76 65  );..    /* Conve
25c9a 72 74 20 54 4b 5f 43 4f 4c 55 4d 4e 20 6e 6f 64  rt TK_COLUMN nod
25c9b 65 73 20 69 6e 74 6f 20 54 4b 5f 41 47 47 5f 43  es into TK_AGG_C
25c9c 4f 4c 55 4d 4e 20 61 6e 64 20 6d 61 6b 65 20 65  OLUMN and make e
25c9d 6e 74 72 69 65 73 20 69 6e 0a 20 20 20 20 2a 2a  ntries in.    **
25c9e 20 73 41 67 67 49 6e 66 6f 20 66 6f 72 20 61 6c   sAggInfo for al
25c9f 6c 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f  l TK_AGG_FUNCTIO
25ca0 4e 20 6e 6f 64 65 73 20 69 6e 20 65 78 70 72 65  N nodes in expre
25ca1 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 0a 20 20  ssions of the.  
25ca2 20 20 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74    ** SELECT stat
25ca3 65 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  ement..    */.  
25ca4 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30    memset(&sNC, 0
25ca5 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a  , sizeof(sNC));.
25ca6 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d      sNC.pParse =
25ca7 20 70 50 61 72 73 65 3b 0a 20 20 20 20 73 4e 43   pParse;.    sNC
25ca8 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 54 61 62  .pSrcList = pTab
25ca9 4c 69 73 74 3b 0a 20 20 20 20 73 4e 43 2e 70 41  List;.    sNC.pA
25caa 67 67 49 6e 66 6f 20 3d 20 26 73 41 67 67 49 6e  ggInfo = &sAggIn
25cab 66 6f 3b 0a 20 20 20 20 73 41 67 67 49 6e 66 6f  fo;.    sAggInfo
25cac 2e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 20  .nSortingColumn 
25cad 3d 20 70 47 72 6f 75 70 42 79 20 3f 20 70 47 72  = pGroupBy ? pGr
25cae 6f 75 70 42 79 2d 3e 6e 45 78 70 72 2b 31 20 3a  oupBy->nExpr+1 :
25caf 20 30 3b 0a 20 20 20 20 73 41 67 67 49 6e 66 6f   0;.    sAggInfo
25cb0 2e 70 47 72 6f 75 70 42 79 20 3d 20 70 47 72 6f  .pGroupBy = pGro
25cb1 75 70 42 79 3b 0a 20 20 20 20 73 71 6c 69 74 65  upBy;.    sqlite
25cb2 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c  3ExprAnalyzeAggL
25cb3 69 73 74 28 26 73 4e 43 2c 20 70 45 4c 69 73 74  ist(&sNC, pEList
25cb4 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
25cb5 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74  prAnalyzeAggList
25cb6 28 26 73 4e 43 2c 20 70 4f 72 64 65 72 42 79 29  (&sNC, pOrderBy)
25cb7 3b 0a 20 20 20 20 69 66 28 20 70 48 61 76 69 6e  ;.    if( pHavin
25cb8 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  g ){.      sqlit
25cb9 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67  e3ExprAnalyzeAgg
25cba 72 65 67 61 74 65 73 28 26 73 4e 43 2c 20 70 48  regates(&sNC, pH
25cbb 61 76 69 6e 67 29 3b 0a 20 20 20 20 7d 0a 20 20  aving);.    }.  
25cbc 20 20 73 41 67 67 49 6e 66 6f 2e 6e 41 63 63 75    sAggInfo.nAccu
25cbd 6d 75 6c 61 74 6f 72 20 3d 20 73 41 67 67 49 6e  mulator = sAggIn
25cbe 66 6f 2e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  fo.nColumn;.    
25cbf 66 6f 72 28 69 3d 30 3b 20 69 3c 73 41 67 67 49  for(i=0; i<sAggI
25cc0 6e 66 6f 2e 6e 46 75 6e 63 3b 20 69 2b 2b 29 7b  nfo.nFunc; i++){
25cc1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
25cc2 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74  prAnalyzeAggList
25cc3 28 26 73 4e 43 2c 20 73 41 67 67 49 6e 66 6f 2e  (&sNC, sAggInfo.
25cc4 61 46 75 6e 63 5b 69 5d 2e 70 45 78 70 72 2d 3e  aFunc[i].pExpr->
25cc5 70 4c 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20  pList);.    }.  
25cc6 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
25cc7 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 73 65  Failed ) goto se
25cc8 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20 20 20 20 2f  lect_end;..    /
25cc9 2a 20 50 72 6f 63 65 73 73 69 6e 67 20 66 6f 72  * Processing for
25cca 20 61 67 67 72 65 67 61 74 65 73 20 77 69 74 68   aggregates with
25ccb 20 47 52 4f 55 50 20 42 59 20 69 73 20 76 65 72   GROUP BY is ver
25ccc 79 20 64 69 66 66 65 72 65 6e 74 20 61 6e 64 0a  y different and.
25ccd 20 20 20 20 2a 2a 20 6d 75 63 68 20 6d 6f 72 65      ** much more
25cce 20 63 6f 6d 70 6c 65 78 20 74 68 61 6e 20 61 67   complex than ag
25ccf 67 72 65 67 61 74 65 73 20 77 69 74 68 6f 75 74  gregates without
25cd0 20 61 20 47 52 4f 55 50 20 42 59 2e 0a 20 20 20   a GROUP BY..   
25cd1 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 47 72 6f   */.    if( pGro
25cd2 75 70 42 79 20 29 7b 0a 20 20 20 20 20 20 4b 65  upBy ){.      Ke
25cd3 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b  yInfo *pKeyInfo;
25cd4 20 20 2f 2a 20 4b 65 79 69 6e 67 20 69 6e 66 6f    /* Keying info
25cd5 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20  rmation for the 
25cd6 67 72 6f 75 70 20 62 79 20 63 6c 61 75 73 65 20  group by clause 
25cd7 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a 31 3b  */.      int j1;
25cd8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
25cd9 41 2d 76 73 2d 42 20 63 6f 6d 70 61 72 69 73 69  A-vs-B comparisi
25cda 6f 6e 20 6a 75 6d 70 20 2a 2f 0a 20 20 20 20 20  on jump */.     
25cdb 20 69 6e 74 20 61 64 64 72 4f 75 74 70 75 74 52   int addrOutputR
25cdc 6f 77 3b 20 20 2f 2a 20 53 74 61 72 74 20 6f 66  ow;  /* Start of
25cdd 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74   subroutine that
25cde 20 6f 75 74 70 75 74 73 20 61 20 72 65 73 75 6c   outputs a resul
25cdf 74 20 72 6f 77 20 2a 2f 0a 20 20 20 20 20 20 69  t row */.      i
25ce0 6e 74 20 72 65 67 4f 75 74 70 75 74 52 6f 77 3b  nt regOutputRow;
25ce1 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 61 64 64     /* Return add
25ce2 72 65 73 73 20 72 65 67 69 73 74 65 72 20 66 6f  ress register fo
25ce3 72 20 6f 75 74 70 75 74 20 73 75 62 72 6f 75 74  r output subrout
25ce4 69 6e 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ine */.      int
25ce5 20 61 64 64 72 53 65 74 41 62 6f 72 74 3b 20 20   addrSetAbort;  
25ce6 20 2f 2a 20 53 65 74 20 74 68 65 20 61 62 6f 72   /* Set the abor
25ce7 74 20 66 6c 61 67 20 61 6e 64 20 72 65 74 75 72  t flag and retur
25ce8 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61  n */.      int a
25ce9 64 64 72 54 6f 70 4f 66 4c 6f 6f 70 3b 20 20 2f  ddrTopOfLoop;  /
25cea 2a 20 54 6f 70 20 6f 66 20 74 68 65 20 69 6e 70  * Top of the inp
25ceb 75 74 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20 20  ut loop */.     
25cec 20 69 6e 74 20 61 64 64 72 53 6f 72 74 69 6e 67   int addrSorting
25ced 49 64 78 3b 20 2f 2a 20 54 68 65 20 4f 50 5f 4f  Idx; /* The OP_O
25cee 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 66 6f 72  penEphemeral for
25cef 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64   the sorting ind
25cf0 65 78 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  ex */.      int 
25cf1 61 64 64 72 52 65 73 65 74 3b 20 20 20 20 20 20  addrReset;      
25cf2 2f 2a 20 53 75 62 72 6f 75 74 69 6e 65 20 66 6f  /* Subroutine fo
25cf3 72 20 72 65 73 65 74 74 69 6e 67 20 74 68 65 20  r resetting the 
25cf4 61 63 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20  accumulator */. 
25cf5 20 20 20 20 20 69 6e 74 20 72 65 67 52 65 73 65       int regRese
25cf6 74 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75  t;       /* Retu
25cf7 72 6e 20 61 64 64 72 65 73 73 20 72 65 67 69 73  rn address regis
25cf8 74 65 72 20 66 6f 72 20 72 65 73 65 74 20 73 75  ter for reset su
25cf9 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 0a 20 20 20  broutine */..   
25cfa 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69     /* If there i
25cfb 73 20 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61  s a GROUP BY cla
25cfc 75 73 65 20 77 65 20 6d 69 67 68 74 20 6e 65 65  use we might nee
25cfd 64 20 61 20 73 6f 72 74 69 6e 67 20 69 6e 64 65  d a sorting inde
25cfe 78 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 69 6d  x to.      ** im
25cff 70 6c 65 6d 65 6e 74 20 69 74 2e 20 20 41 6c 6c  plement it.  All
25d00 6f 63 61 74 65 20 74 68 61 74 20 73 6f 72 74 69  ocate that sorti
25d01 6e 67 20 69 6e 64 65 78 20 6e 6f 77 2e 20 20 49  ng index now.  I
25d02 66 20 69 74 20 74 75 72 6e 73 20 6f 75 74 0a 20  f it turns out. 
25d03 20 20 20 20 20 2a 2a 20 74 68 61 74 20 77 65 20       ** that we 
25d04 64 6f 20 6e 6f 74 20 6e 65 65 64 20 69 74 20 61  do not need it a
25d05 66 74 65 72 20 61 6c 6c 2c 20 74 68 65 20 4f 70  fter all, the Op
25d06 65 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74  enEphemeral inst
25d07 72 75 63 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a  ruction.      **
25d08 20 77 69 6c 6c 20 62 65 20 63 6f 6e 76 65 72 74   will be convert
25d09 65 64 20 69 6e 74 6f 20 61 20 4e 6f 6f 70 2e 20  ed into a Noop. 
25d0a 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20   .      */.     
25d0b 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e   sAggInfo.sortin
25d0c 67 49 64 78 20 3d 20 70 50 61 72 73 65 2d 3e 6e  gIdx = pParse->n
25d0d 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 70 4b 65  Tab++;.      pKe
25d0e 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66 6f 46  yInfo = keyInfoF
25d0f 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61 72  romExprList(pPar
25d10 73 65 2c 20 70 47 72 6f 75 70 42 79 29 3b 0a 20  se, pGroupBy);. 
25d11 20 20 20 20 20 61 64 64 72 53 6f 72 74 69 6e 67       addrSorting
25d12 49 64 78 20 3d 20 73 71 6c 69 74 65 33 56 64 62  Idx = sqlite3Vdb
25d13 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70  eAddOp4(v, OP_Op
25d14 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 0a 20 20  enEphemeral, .  
25d15 20 20 20 20 20 20 20 20 73 41 67 67 49 6e 66 6f          sAggInfo
25d16 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20 73 41 67  .sortingIdx, sAg
25d17 67 49 6e 66 6f 2e 6e 53 6f 72 74 69 6e 67 43 6f  gInfo.nSortingCo
25d18 6c 75 6d 6e 2c 20 0a 20 20 20 20 20 20 20 20 20  lumn, .         
25d19 20 30 2c 20 28 63 68 61 72 2a 29 70 4b 65 79 49   0, (char*)pKeyI
25d1a 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f  nfo, P4_KEYINFO_
25d1b 48 41 4e 44 4f 46 46 29 3b 0a 0a 20 20 20 20 20  HANDOFF);..     
25d1c 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 6d   /* Initialize m
25d1d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20  emory locations 
25d1e 75 73 65 64 20 62 79 20 47 52 4f 55 50 20 42 59  used by GROUP BY
25d1f 20 61 67 67 72 65 67 61 74 65 20 70 72 6f 63 65   aggregate proce
25d20 73 73 69 6e 67 0a 20 20 20 20 20 20 2a 2f 0a 20  ssing.      */. 
25d21 20 20 20 20 20 69 55 73 65 46 6c 61 67 20 3d 20       iUseFlag = 
25d22 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
25d23 20 20 20 20 20 20 69 41 62 6f 72 74 46 6c 61 67        iAbortFlag
25d24 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
25d25 6d 3b 0a 20 20 20 20 20 20 72 65 67 4f 75 74 70  m;.      regOutp
25d26 75 74 52 6f 77 20 3d 20 2b 2b 70 50 61 72 73 65  utRow = ++pParse
25d27 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 61 64  ->nMem;.      ad
25d28 64 72 4f 75 74 70 75 74 52 6f 77 20 3d 20 73 71  drOutputRow = sq
25d29 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
25d2a 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 72 65 67  el(v);.      reg
25d2b 52 65 73 65 74 20 3d 20 2b 2b 70 50 61 72 73 65  Reset = ++pParse
25d2c 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 61 64  ->nMem;.      ad
25d2d 64 72 52 65 73 65 74 20 3d 20 73 71 6c 69 74 65  drReset = sqlite
25d2e 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
25d2f 29 3b 0a 20 20 20 20 20 20 69 41 4d 65 6d 20 3d  );.      iAMem =
25d30 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20   pParse->nMem + 
25d31 31 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  1;.      pParse-
25d32 3e 6e 4d 65 6d 20 2b 3d 20 70 47 72 6f 75 70 42  >nMem += pGroupB
25d33 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20  y->nExpr;.      
25d34 69 42 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e  iBMem = pParse->
25d35 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20 20 20 20 20  nMem + 1;.      
25d36 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20  pParse->nMem += 
25d37 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 3b  pGroupBy->nExpr;
25d38 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
25d39 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
25d3a 6e 74 65 67 65 72 2c 20 30 2c 20 69 41 62 6f 72  nteger, 0, iAbor
25d3b 74 46 6c 61 67 29 3b 0a 20 20 20 20 20 20 56 64  tFlag);.      Vd
25d3c 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 63  beComment((v, "c
25d3d 6c 65 61 72 20 61 62 6f 72 74 20 66 6c 61 67 22  lear abort flag"
25d3e 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ));.      sqlite
25d3f 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
25d40 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 69 55  P_Integer, 0, iU
25d41 73 65 46 6c 61 67 29 3b 0a 20 20 20 20 20 20 56  seFlag);.      V
25d42 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
25d43 69 6e 64 69 63 61 74 65 20 61 63 63 75 6d 75 6c  indicate accumul
25d44 61 74 6f 72 20 65 6d 70 74 79 22 29 29 3b 0a 0a  ator empty"));..
25d45 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 61        /* Begin a
25d46 20 6c 6f 6f 70 20 74 68 61 74 20 77 69 6c 6c 20   loop that will 
25d47 65 78 74 72 61 63 74 20 61 6c 6c 20 73 6f 75 72  extract all sour
25d48 63 65 20 72 6f 77 73 20 69 6e 20 47 52 4f 55 50  ce rows in GROUP
25d49 20 42 59 20 6f 72 64 65 72 2e 0a 20 20 20 20 20   BY order..     
25d4a 20 2a 2a 20 54 68 69 73 20 6d 69 67 68 74 20 69   ** This might i
25d4b 6e 76 6f 6c 76 65 20 74 77 6f 20 73 65 70 61 72  nvolve two separ
25d4c 61 74 65 20 6c 6f 6f 70 73 20 77 69 74 68 20 61  ate loops with a
25d4d 6e 20 4f 50 5f 53 6f 72 74 20 69 6e 20 62 65 74  n OP_Sort in bet
25d4e 77 65 65 6e 2c 20 6f 72 0a 20 20 20 20 20 20 2a  ween, or.      *
25d4f 2a 20 69 74 20 6d 69 67 68 74 20 62 65 20 61 20  * it might be a 
25d50 73 69 6e 67 6c 65 20 6c 6f 6f 70 20 74 68 61 74  single loop that
25d51 20 75 73 65 73 20 61 6e 20 69 6e 64 65 78 20 74   uses an index t
25d52 6f 20 65 78 74 72 61 63 74 20 69 6e 66 6f 72 6d  o extract inform
25d53 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 69  ation.      ** i
25d54 6e 20 74 68 65 20 72 69 67 68 74 20 6f 72 64 65  n the right orde
25d55 72 20 74 6f 20 62 65 67 69 6e 20 77 69 74 68 2e  r to begin with.
25d56 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
25d57 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25d58 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72  2(v, OP_Gosub, r
25d59 65 67 52 65 73 65 74 2c 20 61 64 64 72 52 65 73  egReset, addrRes
25d5a 65 74 29 3b 0a 20 20 20 20 20 20 70 57 49 6e 66  et);.      pWInf
25d5b 6f 20 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65  o = sqlite3Where
25d5c 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54  Begin(pParse, pT
25d5d 61 62 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 20  abList, pWhere, 
25d5e 26 70 47 72 6f 75 70 42 79 2c 20 30 29 3b 0a 20  &pGroupBy, 0);. 
25d5f 20 20 20 20 20 69 66 28 20 70 57 49 6e 66 6f 3d       if( pWInfo=
25d60 3d 30 20 29 20 67 6f 74 6f 20 73 65 6c 65 63 74  =0 ) goto select
25d61 5f 65 6e 64 3b 0a 20 20 20 20 20 20 69 66 28 20  _end;.      if( 
25d62 70 47 72 6f 75 70 42 79 3d 3d 30 20 29 7b 0a 20  pGroupBy==0 ){. 
25d63 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 70         /* The op
25d64 74 69 6d 69 7a 65 72 20 69 73 20 61 62 6c 65 20  timizer is able 
25d65 74 6f 20 64 65 6c 69 76 65 72 20 72 6f 77 73 20  to deliver rows 
25d66 69 6e 20 67 72 6f 75 70 20 62 79 20 6f 72 64 65  in group by orde
25d67 72 20 73 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20  r so.        ** 
25d68 77 65 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74  we do not have t
25d69 6f 20 73 6f 72 74 2e 20 20 54 68 65 20 4f 50 5f  o sort.  The OP_
25d6a 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 74 61  OpenEphemeral ta
25d6b 62 6c 65 20 77 69 6c 6c 20 62 65 0a 20 20 20 20  ble will be.    
25d6c 20 20 20 20 2a 2a 20 63 61 6e 63 65 6c 6c 65 64      ** cancelled
25d6d 20 6c 61 74 65 72 20 62 65 63 61 75 73 65 20 77   later because w
25d6e 65 20 73 74 69 6c 6c 20 6e 65 65 64 20 74 6f 20  e still need to 
25d6f 75 73 65 20 74 68 65 20 70 4b 65 79 49 6e 66 6f  use the pKeyInfo
25d70 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
25d71 20 20 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70      pGroupBy = p
25d72 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20 20 20  ->pGroupBy;.    
25d73 20 20 20 20 67 72 6f 75 70 42 79 53 6f 72 74 20      groupBySort 
25d74 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = 0;.      }else
25d75 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77  {.        /* Row
25d76 73 20 61 72 65 20 63 6f 6d 69 6e 67 20 6f 75 74  s are coming out
25d77 20 69 6e 20 75 6e 64 65 74 65 72 6d 69 6e 65 64   in undetermined
25d78 20 6f 72 64 65 72 2e 20 20 57 65 20 68 61 76 65   order.  We have
25d79 20 74 6f 20 70 75 73 68 0a 20 20 20 20 20 20 20   to push.       
25d7a 20 2a 2a 20 65 61 63 68 20 72 6f 77 20 69 6e 74   ** each row int
25d7b 6f 20 61 20 73 6f 72 74 69 6e 67 20 69 6e 64 65  o a sorting inde
25d7c 78 2c 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65  x, terminate the
25d7d 20 66 69 72 73 74 20 6c 6f 6f 70 2c 0a 20 20 20   first loop,.   
25d7e 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 6c 6f 6f       ** then loo
25d7f 70 20 6f 76 65 72 20 74 68 65 20 73 6f 72 74 69  p over the sorti
25d80 6e 67 20 69 6e 64 65 78 20 69 6e 20 6f 72 64 65  ng index in orde
25d81 72 20 74 6f 20 67 65 74 20 74 68 65 20 6f 75 74  r to get the out
25d82 70 75 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  put.        ** i
25d83 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 0a 20  n sorted order. 
25d84 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
25d85 20 20 69 6e 74 20 72 65 67 42 61 73 65 3b 0a 20    int regBase;. 
25d86 20 20 20 20 20 20 20 69 6e 74 20 72 65 67 52 65         int regRe
25d87 63 6f 72 64 3b 0a 20 20 20 20 20 20 20 20 69 6e  cord;.        in
25d88 74 20 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20  t nCol;.        
25d89 69 6e 74 20 6e 47 72 6f 75 70 42 79 3b 0a 0a 20  int nGroupBy;.. 
25d8a 20 20 20 20 20 20 20 67 72 6f 75 70 42 79 53 6f         groupBySo
25d8b 72 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  rt = 1;.        
25d8c 6e 47 72 6f 75 70 42 79 20 3d 20 70 47 72 6f 75  nGroupBy = pGrou
25d8d 70 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20  pBy->nExpr;.    
25d8e 20 20 20 20 6e 43 6f 6c 20 3d 20 6e 47 72 6f 75      nCol = nGrou
25d8f 70 42 79 20 2b 20 31 3b 0a 20 20 20 20 20 20 20  pBy + 1;.       
25d90 20 6a 20 3d 20 6e 47 72 6f 75 70 42 79 2b 31 3b   j = nGroupBy+1;
25d91 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30  .        for(i=0
25d92 3b 20 69 3c 73 41 67 67 49 6e 66 6f 2e 6e 43 6f  ; i<sAggInfo.nCo
25d93 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  lumn; i++){.    
25d94 20 20 20 20 20 20 69 66 28 20 73 41 67 67 49 6e        if( sAggIn
25d95 66 6f 2e 61 43 6f 6c 5b 69 5d 2e 69 53 6f 72 74  fo.aCol[i].iSort
25d96 65 72 43 6f 6c 75 6d 6e 3e 3d 6a 20 29 7b 0a 20  erColumn>=j ){. 
25d97 20 20 20 20 20 20 20 20 20 20 20 6e 43 6f 6c 2b             nCol+
25d98 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a  +;.            j
25d99 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ++;.          }.
25d9a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
25d9b 20 20 72 65 67 42 61 73 65 20 3d 20 73 71 6c 69    regBase = sqli
25d9c 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28  te3GetTempRange(
25d9d 70 50 61 72 73 65 2c 20 6e 43 6f 6c 29 3b 0a 20  pParse, nCol);. 
25d9e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
25d9f 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28 70  prCodeExprList(p
25da0 50 61 72 73 65 2c 20 70 47 72 6f 75 70 42 79 2c  Parse, pGroupBy,
25da1 20 72 65 67 42 61 73 65 2c 20 30 29 3b 0a 20 20   regBase, 0);.  
25da2 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
25da3 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65  eAddOp2(v, OP_Se
25da4 71 75 65 6e 63 65 2c 20 73 41 67 67 49 6e 66 6f  quence, sAggInfo
25da5 2e 73 6f 72 74 69 6e 67 49 64 78 2c 72 65 67 42  .sortingIdx,regB
25da6 61 73 65 2b 6e 47 72 6f 75 70 42 79 29 3b 0a 20  ase+nGroupBy);. 
25da7 20 20 20 20 20 20 20 6a 20 3d 20 6e 47 72 6f 75         j = nGrou
25da8 70 42 79 2b 31 3b 0a 20 20 20 20 20 20 20 20 66  pBy+1;.        f
25da9 6f 72 28 69 3d 30 3b 20 69 3c 73 41 67 67 49 6e  or(i=0; i<sAggIn
25daa 66 6f 2e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29  fo.nColumn; i++)
25dab 7b 0a 20 20 20 20 20 20 20 20 20 20 73 74 72 75  {.          stru
25dac 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a  ct AggInfo_col *
25dad 70 43 6f 6c 20 3d 20 26 73 41 67 67 49 6e 66 6f  pCol = &sAggInfo
25dae 2e 61 43 6f 6c 5b 69 5d 3b 0a 20 20 20 20 20 20  .aCol[i];.      
25daf 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e 69 53      if( pCol->iS
25db0 6f 72 74 65 72 43 6f 6c 75 6d 6e 3e 3d 6a 20 29  orterColumn>=j )
25db1 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e  {.            in
25db2 74 20 72 31 20 3d 20 6a 20 2b 20 72 65 67 42 61  t r1 = j + regBa
25db3 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se;.            
25db4 69 6e 74 20 72 32 3b 0a 0a 20 20 20 20 20 20 20  int r2;..       
25db5 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65       r2 = sqlite
25db6 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c 75  3ExprCodeGetColu
25db7 6d 6e 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  mn(pParse, .    
25db8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25db9 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d             pCol-
25dba 3e 70 54 61 62 2c 20 70 43 6f 6c 2d 3e 69 43 6f  >pTab, pCol->iCo
25dbb 6c 75 6d 6e 2c 20 70 43 6f 6c 2d 3e 69 54 61 62  lumn, pCol->iTab
25dbc 6c 65 2c 20 72 31 2c 20 30 29 3b 0a 20 20 20 20  le, r1, 0);.    
25dbd 20 20 20 20 20 20 20 20 69 66 28 20 72 31 21 3d          if( r1!=
25dbe 72 32 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  r2 ){.          
25dbf 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
25dc0 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70  ddOp2(v, OP_SCop
25dc1 79 2c 20 72 32 2c 20 72 31 29 3b 0a 20 20 20 20  y, r2, r1);.    
25dc2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
25dc3 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20        j++;.     
25dc4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
25dc5 0a 20 20 20 20 20 20 20 20 72 65 67 52 65 63 6f  .        regReco
25dc6 72 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  rd = sqlite3GetT
25dc7 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
25dc8 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
25dc9 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
25dca 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 42  MakeRecord, regB
25dcb 61 73 65 2c 20 6e 43 6f 6c 2c 20 72 65 67 52 65  ase, nCol, regRe
25dcc 63 6f 72 64 29 3b 0a 20 20 20 20 20 20 20 20 73  cord);.        s
25dcd 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
25dce 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74  (v, OP_IdxInsert
25dcf 2c 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69  , sAggInfo.sorti
25dd0 6e 67 49 64 78 2c 20 72 65 67 52 65 63 6f 72 64  ngIdx, regRecord
25dd1 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
25dd2 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
25dd3 28 70 50 61 72 73 65 2c 20 72 65 67 52 65 63 6f  (pParse, regReco
25dd4 72 64 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  rd);.        sql
25dd5 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
25dd6 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67  ange(pParse, reg
25dd7 42 61 73 65 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20  Base, nCol);.   
25dd8 20 20 20 20 20 73 71 6c 69 74 65 33 57 68 65 72       sqlite3Wher
25dd9 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a 20 20  eEnd(pWInfo);.  
25dda 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
25ddb 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 6f  eAddOp2(v, OP_So
25ddc 72 74 2c 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72  rt, sAggInfo.sor
25ddd 74 69 6e 67 49 64 78 2c 20 61 64 64 72 45 6e 64  tingIdx, addrEnd
25dde 29 3b 0a 20 20 20 20 20 20 20 20 56 64 62 65 43  );.        VdbeC
25ddf 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 47 52 4f 55  omment((v, "GROU
25de0 50 20 42 59 20 73 6f 72 74 22 29 29 3b 0a 20 20  P BY sort"));.  
25de1 20 20 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 75        sAggInfo.u
25de2 73 65 53 6f 72 74 69 6e 67 49 64 78 20 3d 20 31  seSortingIdx = 1
25de3 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
25de4 20 2f 2a 20 45 76 61 6c 75 61 74 65 20 74 68 65   /* Evaluate the
25de5 20 63 75 72 72 65 6e 74 20 47 52 4f 55 50 20 42   current GROUP B
25de6 59 20 74 65 72 6d 73 20 61 6e 64 20 73 74 6f 72  Y terms and stor
25de7 65 20 69 6e 20 62 30 2c 20 62 31 2c 20 62 32 2e  e in b0, b1, b2.
25de8 2e 2e 0a 20 20 20 20 20 20 2a 2a 20 28 62 30 20  ...      ** (b0 
25de9 69 73 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69  is memory locati
25dea 6f 6e 20 69 42 4d 65 6d 2b 30 2c 20 62 31 20 69  on iBMem+0, b1 i
25deb 73 20 69 42 4d 65 6d 2b 31 2c 20 61 6e 64 20 73  s iBMem+1, and s
25dec 6f 20 66 6f 72 74 68 29 0a 20 20 20 20 20 20 2a  o forth).      *
25ded 2a 20 54 68 65 6e 20 63 6f 6d 70 61 72 65 20 74  * Then compare t
25dee 68 65 20 63 75 72 72 65 6e 74 20 47 52 4f 55 50  he current GROUP
25def 20 42 59 20 74 65 72 6d 73 20 61 67 61 69 6e 73   BY terms agains
25df0 74 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 74  t the GROUP BY t
25df1 65 72 6d 73 0a 20 20 20 20 20 20 2a 2a 20 66 72  erms.      ** fr
25df2 6f 6d 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  om the previous 
25df3 72 6f 77 20 63 75 72 72 65 6e 74 6c 79 20 73 74  row currently st
25df4 6f 72 65 64 20 69 6e 20 61 30 2c 20 61 31 2c 20  ored in a0, a1, 
25df5 61 32 2e 2e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  a2....      */. 
25df6 20 20 20 20 20 61 64 64 72 54 6f 70 4f 66 4c 6f       addrTopOfLo
25df7 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  op = sqlite3Vdbe
25df8 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
25df9 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
25dfa 3c 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72  <pGroupBy->nExpr
25dfb 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
25dfc 69 66 28 20 67 72 6f 75 70 42 79 53 6f 72 74 20  if( groupBySort 
25dfd 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
25dfe 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
25dff 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 41 67  , OP_Column, sAg
25e00 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78  gInfo.sortingIdx
25e01 2c 20 6a 2c 20 69 42 4d 65 6d 2b 6a 29 3b 0a 20  , j, iBMem+j);. 
25e02 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
25e03 20 20 20 20 20 20 20 20 73 41 67 67 49 6e 66 6f          sAggInfo
25e04 2e 64 69 72 65 63 74 4d 6f 64 65 20 3d 20 31 3b  .directMode = 1;
25e05 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
25e06 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
25e07 65 2c 20 70 47 72 6f 75 70 42 79 2d 3e 61 5b 6a  e, pGroupBy->a[j
25e08 5d 2e 70 45 78 70 72 2c 20 69 42 4d 65 6d 2b 6a  ].pExpr, iBMem+j
25e09 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
25e0a 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
25e0b 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
25e0c 4f 50 5f 43 6f 6d 70 61 72 65 2c 20 69 41 4d 65  OP_Compare, iAMe
25e0d 6d 2c 20 69 42 4d 65 6d 2c 20 70 47 72 6f 75 70  m, iBMem, pGroup
25e0e 42 79 2d 3e 6e 45 78 70 72 2c 0a 20 20 20 20 20  By->nExpr,.     
25e0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25e10 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79       (char*)pKey
25e11 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f  Info, P4_KEYINFO
25e12 29 3b 0a 20 20 20 20 20 20 6a 31 20 3d 20 73 71  );.      j1 = sq
25e13 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
25e14 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 73  Addr(v);.      s
25e15 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
25e16 28 76 2c 20 4f 50 5f 4a 75 6d 70 2c 20 6a 31 2b  (v, OP_Jump, j1+
25e17 31 2c 20 30 2c 20 6a 31 2b 31 29 3b 0a 0a 20 20  1, 0, j1+1);..  
25e18 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20      /* Generate 
25e19 63 6f 64 65 20 74 68 61 74 20 72 75 6e 73 20 77  code that runs w
25e1a 68 65 6e 65 76 65 72 20 74 68 65 20 47 52 4f 55  henever the GROU
25e1b 50 20 42 59 20 63 68 61 6e 67 65 73 2e 0a 20 20  P BY changes..  
25e1c 20 20 20 20 2a 2a 20 43 68 61 6e 67 65 73 20 69      ** Changes i
25e1d 6e 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 61  n the GROUP BY a
25e1e 72 65 20 64 65 74 65 63 74 65 64 20 62 79 20 74  re detected by t
25e1f 68 65 20 70 72 65 76 69 6f 75 73 20 63 6f 64 65  he previous code
25e20 0a 20 20 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 2e  .      ** block.
25e21 20 20 49 66 20 74 68 65 72 65 20 77 65 72 65 20    If there were 
25e22 6e 6f 20 63 68 61 6e 67 65 73 2c 20 74 68 69 73  no changes, this
25e23 20 62 6c 6f 63 6b 20 69 73 20 73 6b 69 70 70 65   block is skippe
25e24 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  d..      **.    
25e25 20 20 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 63    ** This code c
25e26 6f 70 69 65 73 20 63 75 72 72 65 6e 74 20 67 72  opies current gr
25e27 6f 75 70 20 62 79 20 74 65 72 6d 73 20 69 6e 20  oup by terms in 
25e28 62 30 2c 62 31 2c 62 32 2c 2e 2e 2e 0a 20 20 20  b0,b1,b2,....   
25e29 20 20 20 2a 2a 20 6f 76 65 72 20 74 6f 20 61 30     ** over to a0
25e2a 2c 61 31 2c 61 32 2e 20 20 49 74 20 74 68 65 6e  ,a1,a2.  It then
25e2b 20 63 61 6c 6c 73 20 74 68 65 20 6f 75 74 70 75   calls the outpu
25e2c 74 20 73 75 62 72 6f 75 74 69 6e 65 0a 20 20 20  t subroutine.   
25e2d 20 20 20 2a 2a 20 61 6e 64 20 72 65 73 65 74 73     ** and resets
25e2e 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 61   the aggregate a
25e2f 63 63 75 6d 75 6c 61 74 6f 72 20 72 65 67 69 73  ccumulator regis
25e30 74 65 72 73 20 69 6e 20 70 72 65 70 61 72 61 74  ters in preparat
25e31 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72  ion.      ** for
25e32 20 74 68 65 20 6e 65 78 74 20 47 52 4f 55 50 20   the next GROUP 
25e33 42 59 20 62 61 74 63 68 2e 0a 20 20 20 20 20 20  BY batch..      
25e34 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
25e35 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 70 50 61  ExprCodeMove(pPa
25e36 72 73 65 2c 20 69 42 4d 65 6d 2c 20 69 41 4d 65  rse, iBMem, iAMe
25e37 6d 2c 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78  m, pGroupBy->nEx
25e38 70 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  pr);.      sqlit
25e39 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
25e3a 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74  OP_Gosub, regOut
25e3b 70 75 74 52 6f 77 2c 20 61 64 64 72 4f 75 74 70  putRow, addrOutp
25e3c 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 56 64  utRow);.      Vd
25e3d 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 6f  beComment((v, "o
25e3e 75 74 70 75 74 20 6f 6e 65 20 72 6f 77 22 29 29  utput one row"))
25e3f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
25e40 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
25e41 49 66 50 6f 73 2c 20 69 41 62 6f 72 74 46 6c 61  IfPos, iAbortFla
25e42 67 2c 20 61 64 64 72 45 6e 64 29 3b 0a 20 20 20  g, addrEnd);.   
25e43 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
25e44 76 2c 20 22 63 68 65 63 6b 20 61 62 6f 72 74 20  v, "check abort 
25e45 66 6c 61 67 22 29 29 3b 0a 20 20 20 20 20 20 73  flag"));.      s
25e46 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
25e47 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65  (v, OP_Gosub, re
25e48 67 52 65 73 65 74 2c 20 61 64 64 72 52 65 73 65  gReset, addrRese
25e49 74 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f  t);.      VdbeCo
25e4a 6d 6d 65 6e 74 28 28 76 2c 20 22 72 65 73 65 74  mment((v, "reset
25e4b 20 61 63 63 75 6d 75 6c 61 74 6f 72 22 29 29 3b   accumulator"));
25e4c 0a 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 61 74  ..      /* Updat
25e4d 65 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20  e the aggregate 
25e4e 61 63 63 75 6d 75 6c 61 74 6f 72 73 20 62 61 73  accumulators bas
25e4f 65 64 20 6f 6e 20 74 68 65 20 63 6f 6e 74 65 6e  ed on the conten
25e50 74 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68  t of.      ** th
25e51 65 20 63 75 72 72 65 6e 74 20 72 6f 77 0a 20 20  e current row.  
25e52 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c      */.      sql
25e53 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
25e54 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 20 20 75  (v, j1);.      u
25e55 70 64 61 74 65 41 63 63 75 6d 75 6c 61 74 6f 72  pdateAccumulator
25e56 28 70 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e  (pParse, &sAggIn
25e57 66 6f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  fo);.      sqlit
25e58 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
25e59 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 69  OP_Integer, 1, i
25e5a 55 73 65 46 6c 61 67 29 3b 0a 20 20 20 20 20 20  UseFlag);.      
25e5b 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
25e5c 22 69 6e 64 69 63 61 74 65 20 64 61 74 61 20 69  "indicate data i
25e5d 6e 20 61 63 63 75 6d 75 6c 61 74 6f 72 22 29 29  n accumulator"))
25e5e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 45 6e 64 20  ;..      /* End 
25e5f 6f 66 20 74 68 65 20 6c 6f 6f 70 0a 20 20 20 20  of the loop.    
25e60 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 67    */.      if( g
25e61 72 6f 75 70 42 79 53 6f 72 74 20 29 7b 0a 20 20  roupBySort ){.  
25e62 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
25e63 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
25e64 78 74 2c 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72  xt, sAggInfo.sor
25e65 74 69 6e 67 49 64 78 2c 20 61 64 64 72 54 6f 70  tingIdx, addrTop
25e66 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 20 20 7d  OfLoop);.      }
25e67 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
25e68 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 70 57  lite3WhereEnd(pW
25e69 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 73  Info);.        s
25e6a 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
25e6b 54 6f 4e 6f 6f 70 28 76 2c 20 61 64 64 72 53 6f  ToNoop(v, addrSo
25e6c 72 74 69 6e 67 49 64 78 2c 20 31 29 3b 0a 20 20  rtingIdx, 1);.  
25e6d 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
25e6e 4f 75 74 70 75 74 20 74 68 65 20 66 69 6e 61 6c  Output the final
25e6f 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 0a 20   row of result. 
25e70 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
25e71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
25e72 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67  v, OP_Gosub, reg
25e73 4f 75 74 70 75 74 52 6f 77 2c 20 61 64 64 72 4f  OutputRow, addrO
25e74 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20 20  utputRow);.     
25e75 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
25e76 20 22 6f 75 74 70 75 74 20 66 69 6e 61 6c 20 72   "output final r
25e77 6f 77 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  ow"));..      /*
25e78 20 4a 75 6d 70 20 6f 76 65 72 20 74 68 65 20 73   Jump over the s
25e79 75 62 72 6f 75 74 69 6e 65 73 0a 20 20 20 20 20  ubroutines.     
25e7a 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
25e7b 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
25e7c 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 45  P_Goto, 0, addrE
25e7d 6e 64 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 47  nd);..      /* G
25e7e 65 6e 65 72 61 74 65 20 61 20 73 75 62 72 6f 75  enerate a subrou
25e7f 74 69 6e 65 20 74 68 61 74 20 6f 75 74 70 75 74  tine that output
25e80 73 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f  s a single row o
25e81 66 20 74 68 65 20 72 65 73 75 6c 74 0a 20 20 20  f the result.   
25e82 20 20 20 2a 2a 20 73 65 74 2e 20 20 54 68 69 73     ** set.  This
25e83 20 73 75 62 72 6f 75 74 69 6e 65 20 66 69 72 73   subroutine firs
25e84 74 20 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20 69  t looks at the i
25e85 55 73 65 46 6c 61 67 2e 20 20 49 66 20 69 55 73  UseFlag.  If iUs
25e86 65 46 6c 61 67 0a 20 20 20 20 20 20 2a 2a 20 69  eFlag.      ** i
25e87 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65  s less than or e
25e88 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2c 20 74 68  qual to zero, th
25e89 65 20 73 75 62 72 6f 75 74 69 6e 65 20 69 73 20  e subroutine is 
25e8a 61 20 6e 6f 2d 6f 70 2e 20 20 49 66 0a 20 20 20  a no-op.  If.   
25e8b 20 20 20 2a 2a 20 74 68 65 20 70 72 6f 63 65 73     ** the proces
25e8c 73 69 6e 67 20 63 61 6c 6c 73 20 66 6f 72 20 74  sing calls for t
25e8d 68 65 20 71 75 65 72 79 20 74 6f 20 61 62 6f 72  he query to abor
25e8e 74 2c 20 74 68 69 73 20 73 75 62 72 6f 75 74 69  t, this subrouti
25e8f 6e 65 0a 20 20 20 20 20 20 2a 2a 20 69 6e 63 72  ne.      ** incr
25e90 65 6d 65 6e 74 73 20 74 68 65 20 69 41 62 6f 72  ements the iAbor
25e91 74 46 6c 61 67 20 6d 65 6d 6f 72 79 20 6c 6f 63  tFlag memory loc
25e92 61 74 69 6f 6e 20 62 65 66 6f 72 65 20 72 65 74  ation before ret
25e93 75 72 6e 69 6e 67 20 69 6e 0a 20 20 20 20 20 20  urning in.      
25e94 2a 2a 20 6f 72 64 65 72 20 74 6f 20 73 69 67 6e  ** order to sign
25e95 61 6c 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f  al the caller to
25e96 20 61 62 6f 72 74 2e 0a 20 20 20 20 20 20 2a 2f   abort..      */
25e97 0a 20 20 20 20 20 20 61 64 64 72 53 65 74 41 62  .      addrSetAb
25e98 6f 72 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ort = sqlite3Vdb
25e99 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b  eCurrentAddr(v);
25e9a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
25e9b 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
25e9c 6e 74 65 67 65 72 2c 20 31 2c 20 69 41 62 6f 72  nteger, 1, iAbor
25e9d 74 46 6c 61 67 29 3b 0a 20 20 20 20 20 20 56 64  tFlag);.      Vd
25e9e 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 73  beComment((v, "s
25e9f 65 74 20 61 62 6f 72 74 20 66 6c 61 67 22 29 29  et abort flag"))
25ea0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
25ea1 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
25ea2 52 65 74 75 72 6e 2c 20 72 65 67 4f 75 74 70 75  Return, regOutpu
25ea3 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 73 71 6c  tRow);.      sql
25ea4 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
25ea5 61 62 65 6c 28 76 2c 20 61 64 64 72 4f 75 74 70  abel(v, addrOutp
25ea6 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 61 64  utRow);.      ad
25ea7 64 72 4f 75 74 70 75 74 52 6f 77 20 3d 20 73 71  drOutputRow = sq
25ea8 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
25ea9 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 73  Addr(v);.      s
25eaa 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
25eab 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 69 55  (v, OP_IfPos, iU
25eac 73 65 46 6c 61 67 2c 20 61 64 64 72 4f 75 74 70  seFlag, addrOutp
25ead 75 74 52 6f 77 2b 32 29 3b 0a 20 20 20 20 20 20  utRow+2);.      
25eae 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
25eaf 22 47 72 6f 75 70 62 79 20 72 65 73 75 6c 74 20  "Groupby result 
25eb0 67 65 6e 65 72 61 74 6f 72 20 65 6e 74 72 79 20  generator entry 
25eb1 70 6f 69 6e 74 22 29 29 3b 0a 20 20 20 20 20 20  point"));.      
25eb2 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25eb3 31 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c 20  1(v, OP_Return, 
25eb4 72 65 67 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20  regOutputRow);. 
25eb5 20 20 20 20 20 66 69 6e 61 6c 69 7a 65 41 67 67       finalizeAgg
25eb6 46 75 6e 63 74 69 6f 6e 73 28 70 50 61 72 73 65  Functions(pParse
25eb7 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20  , &sAggInfo);.  
25eb8 20 20 20 20 69 66 28 20 70 48 61 76 69 6e 67 20      if( pHaving 
25eb9 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
25eba 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50  e3ExprIfFalse(pP
25ebb 61 72 73 65 2c 20 70 48 61 76 69 6e 67 2c 20 61  arse, pHaving, a
25ebc 64 64 72 4f 75 74 70 75 74 52 6f 77 2b 31 2c 20  ddrOutputRow+1, 
25ebd 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c  SQLITE_JUMPIFNUL
25ebe 4c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  L);.      }.    
25ebf 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f    selectInnerLoo
25ec0 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e  p(pParse, p, p->
25ec1 70 45 4c 69 73 74 2c 20 30 2c 20 30 2c 20 70 4f  pEList, 0, 0, pO
25ec2 72 64 65 72 42 79 2c 0a 20 20 20 20 20 20 20 20  rderBy,.        
25ec3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 69                di
25ec4 73 74 69 6e 63 74 2c 20 70 44 65 73 74 2c 0a 20  stinct, pDest,. 
25ec5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25ec6 20 20 20 20 20 61 64 64 72 4f 75 74 70 75 74 52       addrOutputR
25ec7 6f 77 2b 31 2c 20 61 64 64 72 53 65 74 41 62 6f  ow+1, addrSetAbo
25ec8 72 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  rt);.      sqlit
25ec9 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
25eca 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65 67 4f 75  OP_Return, regOu
25ecb 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20  tputRow);.      
25ecc 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
25ecd 22 65 6e 64 20 67 72 6f 75 70 62 79 20 72 65 73  "end groupby res
25ece 75 6c 74 20 67 65 6e 65 72 61 74 6f 72 22 29 29  ult generator"))
25ecf 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 47 65 6e 65  ;..      /* Gene
25ed0 72 61 74 65 20 61 20 73 75 62 72 6f 75 74 69 6e  rate a subroutin
25ed1 65 20 74 68 61 74 20 77 69 6c 6c 20 72 65 73 65  e that will rese
25ed2 74 20 74 68 65 20 67 72 6f 75 70 2d 62 79 20 61  t the group-by a
25ed3 63 63 75 6d 75 6c 61 74 6f 72 0a 20 20 20 20 20  ccumulator.     
25ed4 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
25ed5 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
25ed6 6c 28 76 2c 20 61 64 64 72 52 65 73 65 74 29 3b  l(v, addrReset);
25ed7 0a 20 20 20 20 20 20 72 65 73 65 74 41 63 63 75  .      resetAccu
25ed8 6d 75 6c 61 74 6f 72 28 70 50 61 72 73 65 2c 20  mulator(pParse, 
25ed9 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20  &sAggInfo);.    
25eda 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
25edb 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e  Op1(v, OP_Return
25edc 2c 20 72 65 67 52 65 73 65 74 29 3b 0a 20 20 20  , regReset);.   
25edd 20 20 0a 20 20 20 20 7d 20 2f 2a 20 65 6e 64 69    .    } /* endi
25ede 66 20 70 47 72 6f 75 70 42 79 20 2a 2f 0a 20 20  f pGroupBy */.  
25edf 20 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 45    else {.      E
25ee0 78 70 72 4c 69 73 74 20 2a 70 4d 69 6e 4d 61 78  xprList *pMinMax
25ee1 20 3d 20 30 3b 0a 20 20 20 20 20 20 45 78 70 72   = 0;.      Expr
25ee2 4c 69 73 74 20 2a 70 44 65 6c 20 3d 20 30 3b 0a  List *pDel = 0;.
25ee3 20 20 20 20 20 20 75 38 20 66 6c 61 67 3b 0a 0a        u8 flag;..
25ee4 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 69        /* Check i
25ee5 66 20 74 68 65 20 71 75 65 72 79 20 69 73 20 6f  f the query is o
25ee6 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c  f one of the fol
25ee7 6c 6f 77 69 6e 67 20 66 6f 72 6d 73 3a 0a 20 20  lowing forms:.  
25ee8 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
25ee9 20 20 53 45 4c 45 43 54 20 6d 69 6e 28 78 29 20    SELECT min(x) 
25eea 46 52 4f 4d 20 2e 2e 2e 0a 20 20 20 20 20 20 2a  FROM ....      *
25eeb 2a 20 20 20 53 45 4c 45 43 54 20 6d 61 78 28 78  *   SELECT max(x
25eec 29 20 46 52 4f 4d 20 2e 2e 2e 0a 20 20 20 20 20  ) FROM ....     
25eed 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 66 20   **.      ** If 
25eee 69 74 20 69 73 2c 20 74 68 65 6e 20 61 73 6b 20  it is, then ask 
25eef 74 68 65 20 63 6f 64 65 20 69 6e 20 77 68 65 72  the code in wher
25ef0 65 2e 63 20 74 6f 20 61 74 74 65 6d 70 74 20 74  e.c to attempt t
25ef1 6f 20 73 6f 72 74 20 72 65 73 75 6c 74 73 0a 20  o sort results. 
25ef2 20 20 20 20 20 2a 2a 20 61 73 20 69 66 20 74 68       ** as if th
25ef3 65 72 65 20 77 61 73 20 61 6e 20 22 4f 52 44 45  ere was an "ORDE
25ef4 52 20 4f 4e 20 78 22 20 6f 72 20 22 4f 52 44 45  R ON x" or "ORDE
25ef5 52 20 4f 4e 20 78 20 44 45 53 43 22 20 63 6c 61  R ON x DESC" cla
25ef6 75 73 65 2e 20 0a 20 20 20 20 20 20 2a 2a 20 49  use. .      ** I
25ef7 66 20 77 68 65 72 65 2e 63 20 69 73 20 61 62 6c  f where.c is abl
25ef8 65 20 74 6f 20 70 72 6f 64 75 63 65 20 72 65 73  e to produce res
25ef9 75 6c 74 73 20 73 6f 72 74 65 64 20 69 6e 20 74  ults sorted in t
25efa 68 69 73 20 6f 72 64 65 72 2c 20 74 68 65 6e 0a  his order, then.
25efb 20 20 20 20 20 20 2a 2a 20 61 64 64 20 76 64 62        ** add vdb
25efc 65 20 63 6f 64 65 20 74 6f 20 62 72 65 61 6b 20  e code to break 
25efd 6f 75 74 20 6f 66 20 74 68 65 20 70 72 6f 63 65  out of the proce
25efe 73 73 69 6e 67 20 6c 6f 6f 70 20 61 66 74 65 72  ssing loop after
25eff 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 66   the .      ** f
25f00 69 72 73 74 20 69 74 65 72 61 74 69 6f 6e 20 28  irst iteration (
25f01 73 69 6e 63 65 20 74 68 65 20 66 69 72 73 74 20  since the first 
25f02 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  iteration of the
25f03 20 6c 6f 6f 70 20 69 73 20 0a 20 20 20 20 20 20   loop is .      
25f04 2a 2a 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  ** guaranteed to
25f05 20 6f 70 65 72 61 74 65 20 6f 6e 20 74 68 65 20   operate on the 
25f06 72 6f 77 20 77 69 74 68 20 74 68 65 20 6d 69 6e  row with the min
25f07 69 6d 75 6d 20 6f 72 20 6d 61 78 69 6d 75 6d 20  imum or maximum 
25f08 0a 20 20 20 20 20 20 2a 2a 20 76 61 6c 75 65 20  .      ** value 
25f09 6f 66 20 78 2c 20 74 68 65 20 6f 6e 6c 79 20 72  of x, the only r
25f0a 6f 77 20 72 65 71 75 69 72 65 64 29 2e 0a 20 20  ow required)..  
25f0b 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
25f0c 41 20 73 70 65 63 69 61 6c 20 66 6c 61 67 20 6d  A special flag m
25f0d 75 73 74 20 62 65 20 70 61 73 73 65 64 20 74 6f  ust be passed to
25f0e 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
25f0f 69 6e 28 29 20 74 6f 20 73 6c 69 67 68 74 6c 79  in() to slightly
25f10 0a 20 20 20 20 20 20 2a 2a 20 6d 6f 64 69 66 79  .      ** modify
25f11 20 62 65 68 61 76 69 6f 75 72 20 61 73 20 66 6f   behaviour as fo
25f12 6c 6c 6f 77 73 3a 0a 20 20 20 20 20 20 2a 2a 0a  llows:.      **.
25f13 20 20 20 20 20 20 2a 2a 20 20 20 2b 20 49 66 20        **   + If 
25f14 74 68 65 20 71 75 65 72 79 20 69 73 20 61 20 22  the query is a "
25f15 53 45 4c 45 43 54 20 6d 69 6e 28 78 29 22 2c 20  SELECT min(x)", 
25f16 74 68 65 6e 20 74 68 65 20 6c 6f 6f 70 20 63 6f  then the loop co
25f17 64 65 64 20 62 79 0a 20 20 20 20 20 20 2a 2a 20  ded by.      ** 
25f18 20 20 20 20 77 68 65 72 65 2e 63 20 73 68 6f 75      where.c shou
25f19 6c 64 20 6e 6f 74 20 69 74 65 72 61 74 65 20 6f  ld not iterate o
25f1a 76 65 72 20 61 6e 79 20 76 61 6c 75 65 73 20 77  ver any values w
25f1b 69 74 68 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65  ith a NULL value
25f1c 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 66 6f  .      **     fo
25f1d 72 20 78 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20  r x..      **.  
25f1e 20 20 20 20 2a 2a 20 20 20 2b 20 54 68 65 20 6f      **   + The o
25f1f 70 74 69 6d 69 7a 65 72 20 63 6f 64 65 20 69 6e  ptimizer code in
25f20 20 77 68 65 72 65 2e 63 20 28 74 68 65 20 74 68   where.c (the th
25f21 69 6e 67 20 74 68 61 74 20 64 65 63 69 64 65 73  ing that decides
25f22 20 77 68 69 63 68 0a 20 20 20 20 20 20 2a 2a 20   which.      ** 
25f23 20 20 20 20 69 6e 64 65 78 20 6f 72 20 69 6e 64      index or ind
25f24 69 63 65 73 20 74 6f 20 75 73 65 29 20 73 68 6f  ices to use) sho
25f25 75 6c 64 20 70 6c 61 63 65 20 61 20 64 69 66 66  uld place a diff
25f26 65 72 65 6e 74 20 70 72 69 6f 72 69 74 79 20 6f  erent priority o
25f27 6e 20 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20  n .      **     
25f28 73 61 74 69 73 66 79 69 6e 67 20 74 68 65 20 27  satisfying the '
25f29 4f 52 44 45 52 20 42 59 27 20 63 6c 61 75 73 65  ORDER BY' clause
25f2a 20 74 68 61 6e 20 69 74 20 64 6f 65 73 20 69 6e   than it does in
25f2b 20 6f 74 68 65 72 20 63 61 73 65 73 2e 0a 20 20   other cases..  
25f2c 20 20 20 20 2a 2a 20 20 20 20 20 52 65 66 65 72      **     Refer
25f2d 20 74 6f 20 63 6f 64 65 20 61 6e 64 20 63 6f 6d   to code and com
25f2e 6d 65 6e 74 73 20 69 6e 20 77 68 65 72 65 2e 63  ments in where.c
25f2f 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 20 20   for details..  
25f30 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 66 6c 61      */.      fla
25f31 67 20 3d 20 6d 69 6e 4d 61 78 51 75 65 72 79 28  g = minMaxQuery(
25f32 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 20 20  pParse, p);.    
25f33 20 20 69 66 28 20 66 6c 61 67 20 29 7b 0a 20 20    if( flag ){.  
25f34 20 20 20 20 20 20 70 44 65 6c 20 3d 20 70 4d 69        pDel = pMi
25f35 6e 4d 61 78 20 3d 20 73 71 6c 69 74 65 33 45 78  nMax = sqlite3Ex
25f36 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d  prListDup(db, p-
25f37 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  >pEList->a[0].pE
25f38 78 70 72 2d 3e 70 4c 69 73 74 29 3b 0a 20 20 20  xpr->pList);.   
25f39 20 20 20 20 20 69 66 28 20 70 4d 69 6e 4d 61 78       if( pMinMax
25f3a 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46   && !db->mallocF
25f3b 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20  ailed ){.       
25f3c 20 20 20 70 4d 69 6e 4d 61 78 2d 3e 61 5b 30 5d     pMinMax->a[0]
25f3d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 66 6c 61  .sortOrder = fla
25f3e 67 21 3d 57 48 45 52 45 5f 4f 52 44 45 52 42 59  g!=WHERE_ORDERBY
25f3f 5f 4d 49 4e 3b 0a 20 20 20 20 20 20 20 20 20 20  _MIN;.          
25f40 70 4d 69 6e 4d 61 78 2d 3e 61 5b 30 5d 2e 70 45  pMinMax->a[0].pE
25f41 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4c  xpr->op = TK_COL
25f42 55 4d 4e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  UMN;.        }. 
25f43 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
25f44 20 54 68 69 73 20 63 61 73 65 20 72 75 6e 73 20   This case runs 
25f45 69 66 20 74 68 65 20 61 67 67 72 65 67 61 74 65  if the aggregate
25f46 20 68 61 73 20 6e 6f 20 47 52 4f 55 50 20 42 59   has no GROUP BY
25f47 20 63 6c 61 75 73 65 2e 20 20 54 68 65 0a 20 20   clause.  The.  
25f48 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 69 6e      ** processin
25f49 67 20 69 73 20 6d 75 63 68 20 73 69 6d 70 6c 65  g is much simple
25f4a 72 20 73 69 6e 63 65 20 74 68 65 72 65 20 69 73  r since there is
25f4b 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 72   only a single r
25f4c 6f 77 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 6f  ow.      ** of o
25f4d 75 74 70 75 74 2e 0a 20 20 20 20 20 20 2a 2f 0a  utput..      */.
25f4e 20 20 20 20 20 20 72 65 73 65 74 41 63 63 75 6d        resetAccum
25f4f 75 6c 61 74 6f 72 28 70 50 61 72 73 65 2c 20 26  ulator(pParse, &
25f50 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20  sAggInfo);.     
25f51 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65   pWInfo = sqlite
25f52 33 57 68 65 72 65 42 65 67 69 6e 28 70 50 61 72  3WhereBegin(pPar
25f53 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 57  se, pTabList, pW
25f54 68 65 72 65 2c 20 26 70 4d 69 6e 4d 61 78 2c 20  here, &pMinMax, 
25f55 66 6c 61 67 29 3b 0a 20 20 20 20 20 20 69 66 28  flag);.      if(
25f56 20 70 57 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20   pWInfo==0 ){.  
25f57 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
25f58 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
25f59 70 44 65 6c 29 3b 0a 20 20 20 20 20 20 20 20 67  pDel);.        g
25f5a 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  oto select_end;.
25f5b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 75 70        }.      up
25f5c 64 61 74 65 41 63 63 75 6d 75 6c 61 74 6f 72 28  dateAccumulator(
25f5d 70 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66  pParse, &sAggInf
25f5e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70  o);.      if( !p
25f5f 4d 69 6e 4d 61 78 20 26 26 20 66 6c 61 67 20 29  MinMax && flag )
25f60 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
25f61 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
25f62 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 57 49 6e 66  P_Goto, 0, pWInf
25f63 6f 2d 3e 69 42 72 65 61 6b 29 3b 0a 20 20 20 20  o->iBreak);.    
25f64 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
25f65 28 76 2c 20 22 25 73 28 29 20 62 79 20 69 6e 64  (v, "%s() by ind
25f66 65 78 22 2c 28 66 6c 61 67 3d 3d 57 48 45 52 45  ex",(flag==WHERE
25f67 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 3f 22 6d 69  _ORDERBY_MIN?"mi
25f68 6e 22 3a 22 6d 61 78 22 29 29 29 3b 0a 20 20 20  n":"max")));.   
25f69 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
25f6a 65 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66  e3WhereEnd(pWInf
25f6b 6f 29 3b 0a 20 20 20 20 20 20 66 69 6e 61 6c 69  o);.      finali
25f6c 7a 65 41 67 67 46 75 6e 63 74 69 6f 6e 73 28 70  zeAggFunctions(p
25f6d 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66 6f  Parse, &sAggInfo
25f6e 29 3b 0a 20 20 20 20 20 20 70 4f 72 64 65 72 42  );.      pOrderB
25f6f 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28  y = 0;.      if(
25f70 20 70 48 61 76 69 6e 67 20 29 7b 0a 20 20 20 20   pHaving ){.    
25f71 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
25f72 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70  fFalse(pParse, p
25f73 48 61 76 69 6e 67 2c 20 61 64 64 72 45 6e 64 2c  Having, addrEnd,
25f74 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55   SQLITE_JUMPIFNU
25f75 4c 4c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  LL);.      }.   
25f76 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f     selectInnerLo
25f77 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d  op(pParse, p, p-
25f78 3e 70 45 4c 69 73 74 2c 20 30 2c 20 30 2c 20 30  >pEList, 0, 0, 0
25f79 2c 20 2d 31 2c 20 0a 20 20 20 20 20 20 20 20 20  , -1, .         
25f7a 20 20 20 20 20 20 20 20 20 20 20 20 20 70 44 65               pDe
25f7b 73 74 2c 20 61 64 64 72 45 6e 64 2c 20 61 64 64  st, addrEnd, add
25f7c 72 45 6e 64 29 3b 0a 0a 20 20 20 20 20 20 73 71  rEnd);..      sq
25f7d 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
25f7e 65 74 65 28 64 62 2c 20 70 44 65 6c 29 3b 0a 20  ete(db, pDel);. 
25f7f 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
25f80 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
25f81 28 76 2c 20 61 64 64 72 45 6e 64 29 3b 0a 20 20  (v, addrEnd);.  
25f82 20 20 0a 20 20 7d 20 2f 2a 20 65 6e 64 69 66 20    .  } /* endif 
25f83 61 67 67 72 65 67 61 74 65 20 71 75 65 72 79 20  aggregate query 
25f84 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72  */..  /* If ther
25f85 65 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59  e is an ORDER BY
25f86 20 63 6c 61 75 73 65 2c 20 74 68 65 6e 20 77 65   clause, then we
25f87 20 6e 65 65 64 20 74 6f 20 73 6f 72 74 20 74 68   need to sort th
25f88 65 20 72 65 73 75 6c 74 73 0a 20 20 2a 2a 20 61  e results.  ** a
25f89 6e 64 20 73 65 6e 64 20 74 68 65 6d 20 74 6f 20  nd send them to 
25f8a 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 65  the callback one
25f8b 20 62 79 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20   by one..  */.  
25f8c 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a  if( pOrderBy ){.
25f8d 20 20 20 20 67 65 6e 65 72 61 74 65 53 6f 72 74      generateSort
25f8e 54 61 69 6c 28 70 50 61 72 73 65 2c 20 70 2c 20  Tail(pParse, p, 
25f8f 76 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  v, pEList->nExpr
25f90 2c 20 70 44 65 73 74 29 3b 0a 20 20 7d 0a 0a 20  , pDest);.  }.. 
25f91 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f   /* Jump here to
25f92 20 73 6b 69 70 20 74 68 69 73 20 71 75 65 72 79   skip this query
25f93 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  .  */.  sqlite3V
25f94 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
25f95 76 2c 20 69 45 6e 64 29 3b 0a 0a 20 20 2f 2a 20  v, iEnd);..  /* 
25f96 54 68 65 20 53 45 4c 45 43 54 20 77 61 73 20 73  The SELECT was s
25f97 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 64 65  uccessfully code
25f98 64 2e 20 20 20 53 65 74 20 74 68 65 20 72 65 74  d.   Set the ret
25f99 75 72 6e 20 63 6f 64 65 20 74 6f 20 30 0a 20 20  urn code to 0.  
25f9a 2a 2a 20 74 6f 20 69 6e 64 69 63 61 74 65 20 6e  ** to indicate n
25f9b 6f 20 65 72 72 6f 72 73 2e 0a 20 20 2a 2f 0a 20  o errors..  */. 
25f9c 20 72 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 43   rc = 0;..  /* C
25f9d 6f 6e 74 72 6f 6c 20 6a 75 6d 70 73 20 74 6f 20  ontrol jumps to 
25f9e 68 65 72 65 20 69 66 20 61 6e 20 65 72 72 6f 72  here if an error
25f9f 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20   is encountered 
25fa0 61 62 6f 76 65 2c 20 6f 72 20 75 70 6f 6e 0a 20  above, or upon. 
25fa1 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 63   ** successful c
25fa2 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 53 45 4c  oding of the SEL
25fa3 45 43 54 2e 0a 20 20 2a 2f 0a 73 65 6c 65 63 74  ECT..  */.select
25fa4 5f 65 6e 64 3a 0a 0a 20 20 2f 2a 20 49 64 65 6e  _end:..  /* Iden
25fa5 74 69 66 79 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  tify column name
25fa6 73 20 69 66 20 72 65 73 75 6c 74 73 20 6f 66 20  s if results of 
25fa7 74 68 65 20 53 45 4c 45 43 54 20 61 72 65 20 74  the SELECT are t
25fa8 6f 20 62 65 20 6f 75 74 70 75 74 2e 0a 20 20 2a  o be output..  *
25fa9 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
25faa 54 45 5f 4f 4b 20 26 26 20 70 44 65 73 74 2d 3e  TE_OK && pDest->
25fab 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75  eDest==SRT_Outpu
25fac 74 20 29 7b 0a 20 20 20 20 67 65 6e 65 72 61 74  t ){.    generat
25fad 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61  eColumnNames(pPa
25fae 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70  rse, pTabList, p
25faf 45 4c 69 73 74 29 3b 0a 20 20 7d 0a 0a 20 20 73  EList);.  }..  s
25fb0 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
25fb1 20 73 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 29 3b   sAggInfo.aCol);
25fb2 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
25fb3 28 64 62 2c 20 73 41 67 67 49 6e 66 6f 2e 61 46  (db, sAggInfo.aF
25fb4 75 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  unc);.  return r
25fb5 63 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65  c;.}..#if define
25fb6 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a  d(SQLITE_DEBUG).
25fb7 2f 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /*.*************
25fb8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25fb9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25fba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25fbb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25fbc 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  **.** The follow
25fbd 69 6e 67 20 63 6f 64 65 20 69 73 20 75 73 65 64  ing code is used
25fbe 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64   for testing and
25fbf 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e   debugging only.
25fc0 20 20 54 68 65 20 63 6f 64 65 0a 2a 2a 20 74 68    The code.** th
25fc1 61 74 20 66 6f 6c 6c 6f 77 73 20 64 6f 65 73 20  at follows does 
25fc2 6e 6f 74 20 61 70 70 65 61 72 20 69 6e 20 6e 6f  not appear in no
25fc3 72 6d 61 6c 20 62 75 69 6c 64 73 2e 0a 2a 2a 0a  rmal builds..**.
25fc4 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
25fc5 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 70 72  s are used to pr
25fc6 69 6e 74 20 6f 75 74 20 74 68 65 20 63 6f 6e 74  int out the cont
25fc7 65 6e 74 20 6f 66 20 61 6c 6c 20 6f 72 20 70 61  ent of all or pa
25fc8 72 74 20 6f 66 20 61 20 0a 2a 2a 20 70 61 72 73  rt of a .** pars
25fc9 65 20 73 74 72 75 63 74 75 72 65 73 20 73 75 63  e structures suc
25fca 68 20 61 73 20 53 65 6c 65 63 74 20 6f 72 20 45  h as Select or E
25fcb 78 70 72 2e 20 20 53 75 63 68 20 70 72 69 6e 74  xpr.  Such print
25fcc 6f 75 74 73 20 61 72 65 20 75 73 65 66 75 6c 0a  outs are useful.
25fcd 2a 2a 20 66 6f 72 20 68 65 6c 70 69 6e 67 20 74  ** for helping t
25fce 6f 20 75 6e 64 65 72 73 74 61 6e 64 20 77 68 61  o understand wha
25fcf 74 20 69 73 20 68 61 70 70 65 6e 69 6e 67 20 69  t is happening i
25fd0 6e 73 69 64 65 20 74 68 65 20 63 6f 64 65 20 67  nside the code g
25fd1 65 6e 65 72 61 74 6f 72 0a 2a 2a 20 64 75 72 69  enerator.** duri
25fd2 6e 67 20 74 68 65 20 65 78 65 63 75 74 69 6f 6e  ng the execution
25fd3 20 6f 66 20 63 6f 6d 70 6c 65 78 20 53 45 4c 45   of complex SELE
25fd4 43 54 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  CT statements..*
25fd5 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
25fd6 6e 65 20 61 72 65 20 6e 6f 74 20 63 61 6c 6c 65  ne are not calle
25fd7 64 20 61 6e 79 77 68 65 72 65 20 66 72 6f 6d 20  d anywhere from 
25fd8 77 69 74 68 69 6e 20 74 68 65 20 6e 6f 72 6d 61  within the norma
25fd9 6c 0a 2a 2a 20 63 6f 64 65 20 62 61 73 65 2e 20  l.** code base. 
25fda 20 54 68 65 6e 20 61 72 65 20 69 6e 74 65 6e 64   Then are intend
25fdb 65 64 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 20  ed to be called 
25fdc 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20  from within the 
25fdd 64 65 62 75 67 67 65 72 0a 2a 2a 20 6f 72 20 66  debugger.** or f
25fde 72 6f 6d 20 74 65 6d 70 6f 72 61 72 79 20 22 70  rom temporary "p
25fdf 72 69 6e 74 66 22 20 73 74 61 74 65 6d 65 6e 74  rintf" statement
25fe0 73 20 69 6e 73 65 72 74 65 64 20 66 6f 72 20 64  s inserted for d
25fe1 65 62 75 67 67 69 6e 67 2e 0a 2a 2f 0a 53 51 4c  ebugging..*/.SQL
25fe2 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
25fe3 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70   sqlite3PrintExp
25fe4 72 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 66  r(Expr *p){.  if
25fe5 28 20 70 2d 3e 74 6f 6b 65 6e 2e 7a 20 26 26 20  ( p->token.z && 
25fe6 70 2d 3e 74 6f 6b 65 6e 2e 6e 3e 30 20 29 7b 0a  p->token.n>0 ){.
25fe7 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
25fe8 50 72 69 6e 74 66 28 22 28 25 2e 2a 73 22 2c 20  Printf("(%.*s", 
25fe9 70 2d 3e 74 6f 6b 65 6e 2e 6e 2c 20 70 2d 3e 74  p->token.n, p->t
25fea 6f 6b 65 6e 2e 7a 29 3b 0a 20 20 7d 65 6c 73 65  oken.z);.  }else
25feb 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62  {.    sqlite3Deb
25fec 75 67 50 72 69 6e 74 66 28 22 28 25 64 22 2c 20  ugPrintf("(%d", 
25fed 70 2d 3e 6f 70 29 3b 0a 20 20 7d 0a 20 20 69 66  p->op);.  }.  if
25fee 28 20 70 2d 3e 70 4c 65 66 74 20 29 7b 0a 20 20  ( p->pLeft ){.  
25fef 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
25ff0 69 6e 74 66 28 22 20 22 29 3b 0a 20 20 20 20 73  intf(" ");.    s
25ff1 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72 28  qlite3PrintExpr(
25ff2 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 7d 0a 20  p->pLeft);.  }. 
25ff3 20 69 66 28 20 70 2d 3e 70 52 69 67 68 74 20 29   if( p->pRight )
25ff4 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62  {.    sqlite3Deb
25ff5 75 67 50 72 69 6e 74 66 28 22 20 22 29 3b 0a 20  ugPrintf(" ");. 
25ff6 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45     sqlite3PrintE
25ff7 78 70 72 28 70 2d 3e 70 52 69 67 68 74 29 3b 0a  xpr(p->pRight);.
25ff8 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 65 62    }.  sqlite3Deb
25ff9 75 67 50 72 69 6e 74 66 28 22 29 22 29 3b 0a 7d  ugPrintf(")");.}
25ffa 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
25ffb 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 69 6e  void sqlite3Prin
25ffc 74 45 78 70 72 4c 69 73 74 28 45 78 70 72 4c 69  tExprList(ExprLi
25ffd 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e  st *pList){.  in
25ffe 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  t i;.  for(i=0; 
25fff 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  i<pList->nExpr; 
26000 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  i++){.    sqlite
26001 33 50 72 69 6e 74 45 78 70 72 28 70 4c 69 73 74  3PrintExpr(pList
26002 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 3b 0a 20  ->a[i].pExpr);. 
26003 20 20 20 69 66 28 20 69 3c 70 4c 69 73 74 2d 3e     if( i<pList->
26004 6e 45 78 70 72 2d 31 20 29 7b 0a 20 20 20 20 20  nExpr-1 ){.     
26005 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
26006 6e 74 66 28 22 2c 20 22 29 3b 0a 20 20 20 20 7d  ntf(", ");.    }
26007 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  .  }.}.SQLITE_PR
26008 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
26009 65 33 50 72 69 6e 74 53 65 6c 65 63 74 28 53 65  e3PrintSelect(Se
2600a 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 69 6e 64  lect *p, int ind
2600b 65 6e 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 44  ent){.  sqlite3D
2600c 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73 53  ebugPrintf("%*sS
2600d 45 4c 45 43 54 28 25 70 29 20 22 2c 20 69 6e 64  ELECT(%p) ", ind
2600e 65 6e 74 2c 20 22 22 2c 20 70 29 3b 0a 20 20 73  ent, "", p);.  s
2600f 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72 4c  qlite3PrintExprL
26010 69 73 74 28 70 2d 3e 70 45 4c 69 73 74 29 3b 0a  ist(p->pEList);.
26011 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
26012 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20 69 66  intf("\n");.  if
26013 28 20 70 2d 3e 70 53 72 63 20 29 7b 0a 20 20 20  ( p->pSrc ){.   
26014 20 63 68 61 72 20 2a 7a 50 72 65 66 69 78 3b 0a   char *zPrefix;.
26015 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 7a      int i;.    z
26016 50 72 65 66 69 78 20 3d 20 22 46 52 4f 4d 22 3b  Prefix = "FROM";
26017 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
26018 70 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 3b 20 69  p->pSrc->nSrc; i
26019 2b 2b 29 7b 0a 20 20 20 20 20 20 73 74 72 75 63  ++){.      struc
2601a 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
2601b 70 49 74 65 6d 20 3d 20 26 70 2d 3e 70 53 72 63  pItem = &p->pSrc
2601c 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 20 73 71  ->a[i];.      sq
2601d 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
2601e 28 22 25 2a 73 20 22 2c 20 69 6e 64 65 6e 74 2b  ("%*s ", indent+
2601f 36 2c 20 7a 50 72 65 66 69 78 29 3b 0a 20 20 20  6, zPrefix);.   
26020 20 20 20 7a 50 72 65 66 69 78 20 3d 20 22 22 3b     zPrefix = "";
26021 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d  .      if( pItem
26022 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ->pSelect ){.   
26023 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75       sqlite3Debu
26024 67 50 72 69 6e 74 66 28 22 28 5c 6e 22 29 3b 0a  gPrintf("(\n");.
26025 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
26026 72 69 6e 74 53 65 6c 65 63 74 28 70 49 74 65 6d  rintSelect(pItem
26027 2d 3e 70 53 65 6c 65 63 74 2c 20 69 6e 64 65 6e  ->pSelect, inden
26028 74 2b 31 30 29 3b 0a 20 20 20 20 20 20 20 20 73  t+10);.        s
26029 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2602a 66 28 22 25 2a 73 29 22 2c 20 69 6e 64 65 6e 74  f("%*s)", indent
2602b 2b 38 2c 20 22 22 29 3b 0a 20 20 20 20 20 20 7d  +8, "");.      }
2602c 65 6c 73 65 20 69 66 28 20 70 49 74 65 6d 2d 3e  else if( pItem->
2602d 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20  zName ){.       
2602e 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
2602f 6e 74 66 28 22 25 73 22 2c 20 70 49 74 65 6d 2d  ntf("%s", pItem-
26030 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d  >zName);.      }
26031 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d  .      if( pItem
26032 2d 3e 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20  ->pTab ){.      
26033 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
26034 69 6e 74 66 28 22 28 74 61 62 6c 65 3a 20 25 73  intf("(table: %s
26035 29 22 2c 20 70 49 74 65 6d 2d 3e 70 54 61 62 2d  )", pItem->pTab-
26036 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d  >zName);.      }
26037 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d  .      if( pItem
26038 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a 20 20 20 20  ->zAlias ){.    
26039 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
2603a 50 72 69 6e 74 66 28 22 20 41 53 20 25 73 22 2c  Printf(" AS %s",
2603b 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b   pItem->zAlias);
2603c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
2603d 66 28 20 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53  f( i<p->pSrc->nS
2603e 72 63 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  rc-1 ){.        
2603f 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
26040 74 66 28 22 2c 22 29 3b 0a 20 20 20 20 20 20 7d  tf(",");.      }
26041 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65  .      sqlite3De
26042 62 75 67 50 72 69 6e 74 66 28 22 5c 6e 22 29 3b  bugPrintf("\n");
26043 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
26044 20 70 2d 3e 70 57 68 65 72 65 20 29 7b 0a 20 20   p->pWhere ){.  
26045 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
26046 69 6e 74 66 28 22 25 2a 73 20 57 48 45 52 45 20  intf("%*s WHERE 
26047 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22 29 3b 0a  ", indent, "");.
26048 20 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74      sqlite3Print
26049 45 78 70 72 28 70 2d 3e 70 57 68 65 72 65 29 3b  Expr(p->pWhere);
2604a 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75  .    sqlite3Debu
2604b 67 50 72 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20  gPrintf("\n");. 
2604c 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 47 72 6f   }.  if( p->pGro
2604d 75 70 42 79 20 29 7b 0a 20 20 20 20 73 71 6c 69  upBy ){.    sqli
2604e 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
2604f 25 2a 73 20 47 52 4f 55 50 20 42 59 20 22 2c 20  %*s GROUP BY ", 
26050 69 6e 64 65 6e 74 2c 20 22 22 29 3b 0a 20 20 20  indent, "");.   
26051 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70   sqlite3PrintExp
26052 72 4c 69 73 74 28 70 2d 3e 70 47 72 6f 75 70 42  rList(p->pGroupB
26053 79 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  y);.    sqlite3D
26054 65 62 75 67 50 72 69 6e 74 66 28 22 5c 6e 22 29  ebugPrintf("\n")
26055 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70  ;.  }.  if( p->p
26056 48 61 76 69 6e 67 20 29 7b 0a 20 20 20 20 73 71  Having ){.    sq
26057 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
26058 28 22 25 2a 73 20 48 41 56 49 4e 47 20 22 2c 20  ("%*s HAVING ", 
26059 69 6e 64 65 6e 74 2c 20 22 22 29 3b 0a 20 20 20  indent, "");.   
2605a 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70   sqlite3PrintExp
2605b 72 28 70 2d 3e 70 48 61 76 69 6e 67 29 3b 0a 20  r(p->pHaving);. 
2605c 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
2605d 72 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20 7d  rintf("\n");.  }
2605e 0a 20 20 69 66 28 20 70 2d 3e 70 4f 72 64 65 72  .  if( p->pOrder
2605f 42 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  By ){.    sqlite
26060 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a  3DebugPrintf("%*
26061 73 20 4f 52 44 45 52 20 42 59 20 22 2c 20 69 6e  s ORDER BY ", in
26062 64 65 6e 74 2c 20 22 22 29 3b 0a 20 20 20 20 73  dent, "");.    s
26063 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72 4c  qlite3PrintExprL
26064 69 73 74 28 70 2d 3e 70 4f 72 64 65 72 42 79 29  ist(p->pOrderBy)
26065 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62  ;.    sqlite3Deb
26066 75 67 50 72 69 6e 74 66 28 22 5c 6e 22 29 3b 0a  ugPrintf("\n");.
26067 20 20 7d 0a 7d 0a 2f 2a 20 45 6e 64 20 6f 66 20    }.}./* End of 
26068 74 68 65 20 73 74 72 75 63 74 75 72 65 20 64 65  the structure de
26069 62 75 67 20 70 72 69 6e 74 69 6e 67 20 63 6f 64  bug printing cod
2606a 65 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.**************
2606b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2606c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2606d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2606e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2606f 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e  .#endif /* defin
26070 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20  ed(SQLITE_TEST) 
26071 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
26072 45 5f 44 45 42 55 47 29 20 2a 2f 0a 0a 2f 2a 2a  E_DEBUG) */../**
26073 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
26074 20 6f 66 20 73 65 6c 65 63 74 2e 63 20 2a 2a 2a   of select.c ***
26075 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26076 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26077 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
26078 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
26079 69 6e 20 66 69 6c 65 20 74 61 62 6c 65 2e 63 20  in file table.c 
2607a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2607b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2607c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
2607d 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
2607e 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
2607f 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
26080 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
26081 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
26082 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
26083 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
26084 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
26085 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
26086 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
26087 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
26088 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
26089 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
2608a 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
2608b 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
2608c 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
2608d 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
2608e 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
2608f 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
26090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26091 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26092 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26093 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26094 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
26095 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73 71   contains the sq
26096 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28  lite3_get_table(
26097 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 66 72  ) and sqlite3_fr
26098 65 65 5f 74 61 62 6c 65 28 29 0a 2a 2a 20 69 6e  ee_table().** in
26099 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e 65 73  terface routines
2609a 2e 20 20 54 68 65 73 65 20 61 72 65 20 6a 75 73  .  These are jus
2609b 74 20 77 72 61 70 70 65 72 73 20 61 72 6f 75 6e  t wrappers aroun
2609c 64 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 69 6e  d the main.** in
2609d 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e 65 20  terface routine 
2609e 6f 66 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  of sqlite3_exec(
2609f 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  )..**.** These r
260a0 6f 75 74 69 6e 65 73 20 61 72 65 20 69 6e 20 61  outines are in a
260a1 20 73 65 70 61 72 61 74 65 20 66 69 6c 65 73 20   separate files 
260a2 73 6f 20 74 68 61 74 20 74 68 65 79 20 77 69 6c  so that they wil
260a3 6c 20 6e 6f 74 20 62 65 20 6c 69 6e 6b 65 64 0a  l not be linked.
260a4 2a 2a 20 69 66 20 74 68 65 79 20 61 72 65 20 6e  ** if they are n
260a5 6f 74 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 24  ot used..**.** $
260a6 49 64 3a 20 74 61 62 6c 65 2e 63 2c 76 20 31 2e  Id: table.c,v 1.
260a7 33 36 20 32 30 30 38 2f 30 37 2f 30 38 20 32 32  36 2008/07/08 22
260a8 3a 32 38 3a 34 39 20 73 68 61 6e 65 20 45 78 70  :28:49 shane Exp
260a9 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53   $.*/..#ifndef S
260aa 51 4c 49 54 45 5f 4f 4d 49 54 5f 47 45 54 5f 54  QLITE_OMIT_GET_T
260ab 41 42 4c 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ABLE../*.** This
260ac 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73   structure is us
260ad 65 64 20 74 6f 20 70 61 73 73 20 64 61 74 61 20  ed to pass data 
260ae 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 67 65 74  from sqlite3_get
260af 5f 74 61 62 6c 65 28 29 20 74 68 72 6f 75 67 68  _table() through
260b0 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62  .** to the callb
260b1 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  ack function is 
260b2 75 73 65 73 20 74 6f 20 62 75 69 6c 64 20 74 68  uses to build th
260b3 65 20 72 65 73 75 6c 74 2e 0a 2a 2f 0a 74 79 70  e result..*/.typ
260b4 65 64 65 66 20 73 74 72 75 63 74 20 54 61 62 52  edef struct TabR
260b5 65 73 75 6c 74 20 7b 0a 20 20 63 68 61 72 20 2a  esult {.  char *
260b6 2a 61 7a 52 65 73 75 6c 74 3b 0a 20 20 63 68 61  *azResult;.  cha
260b7 72 20 2a 7a 45 72 72 4d 73 67 3b 0a 20 20 69 6e  r *zErrMsg;.  in
260b8 74 20 6e 52 65 73 75 6c 74 3b 0a 20 20 69 6e 74  t nResult;.  int
260b9 20 6e 41 6c 6c 6f 63 3b 0a 20 20 69 6e 74 20 6e   nAlloc;.  int n
260ba 52 6f 77 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 75  Row;.  int nColu
260bb 6d 6e 3b 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b  mn;.  int nData;
260bc 0a 20 20 69 6e 74 20 72 63 3b 0a 7d 20 54 61 62  .  int rc;.} Tab
260bd 52 65 73 75 6c 74 3b 0a 0a 2f 2a 0a 2a 2a 20 54  Result;../*.** T
260be 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
260bf 61 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 20 65  alled once for e
260c0 61 63 68 20 72 6f 77 20 69 6e 20 74 68 65 20 72  ach row in the r
260c1 65 73 75 6c 74 20 74 61 62 6c 65 2e 20 20 49 74  esult table.  It
260c2 73 20 6a 6f 62 0a 2a 2a 20 69 73 20 74 6f 20 66  s job.** is to f
260c3 69 6c 6c 20 69 6e 20 74 68 65 20 54 61 62 52 65  ill in the TabRe
260c4 73 75 6c 74 20 73 74 72 75 63 74 75 72 65 20 61  sult structure a
260c5 70 70 72 6f 70 72 69 61 74 65 6c 79 2c 20 61 6c  ppropriately, al
260c6 6c 6f 63 61 74 69 6e 67 20 6e 65 77 0a 2a 2a 20  locating new.** 
260c7 6d 65 6d 6f 72 79 20 61 73 20 6e 65 63 65 73 73  memory as necess
260c8 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ary..*/.static i
260c9 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  nt sqlite3_get_t
260ca 61 62 6c 65 5f 63 62 28 76 6f 69 64 20 2a 70 41  able_cb(void *pA
260cb 72 67 2c 20 69 6e 74 20 6e 43 6f 6c 2c 20 63 68  rg, int nCol, ch
260cc 61 72 20 2a 2a 61 72 67 76 2c 20 63 68 61 72 20  ar **argv, char 
260cd 2a 2a 63 6f 6c 76 29 7b 0a 20 20 54 61 62 52 65  **colv){.  TabRe
260ce 73 75 6c 74 20 2a 70 20 3d 20 28 54 61 62 52 65  sult *p = (TabRe
260cf 73 75 6c 74 2a 29 70 41 72 67 3b 0a 20 20 69 6e  sult*)pArg;.  in
260d0 74 20 6e 65 65 64 3b 0a 20 20 69 6e 74 20 69 3b  t need;.  int i;
260d1 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 2f  .  char *z;..  /
260d2 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 72  * Make sure ther
260d3 65 20 69 73 20 65 6e 6f 75 67 68 20 73 70 61 63  e is enough spac
260d4 65 20 69 6e 20 70 2d 3e 61 7a 52 65 73 75 6c 74  e in p->azResult
260d5 20 74 6f 20 68 6f 6c 64 20 65 76 65 72 79 74 68   to hold everyth
260d6 69 6e 67 0a 20 20 2a 2a 20 77 65 20 6e 65 65 64  ing.  ** we need
260d7 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 66 72 6f   to remember fro
260d8 6d 20 74 68 69 73 20 69 6e 76 6f 63 61 74 69 6f  m this invocatio
260d9 6e 20 6f 66 20 74 68 65 20 63 61 6c 6c 62 61 63  n of the callbac
260da 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d  k..  */.  if( p-
260db 3e 6e 52 6f 77 3d 3d 30 20 26 26 20 61 72 67 76  >nRow==0 && argv
260dc 21 3d 30 20 29 7b 0a 20 20 20 20 6e 65 65 64 20  !=0 ){.    need 
260dd 3d 20 6e 43 6f 6c 2a 32 3b 0a 20 20 7d 65 6c 73  = nCol*2;.  }els
260de 65 7b 0a 20 20 20 20 6e 65 65 64 20 3d 20 6e 43  e{.    need = nC
260df 6f 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d  ol;.  }.  if( p-
260e0 3e 6e 44 61 74 61 20 2b 20 6e 65 65 64 20 3e 3d  >nData + need >=
260e1 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20   p->nAlloc ){.  
260e2 20 20 63 68 61 72 20 2a 2a 61 7a 4e 65 77 3b 0a    char **azNew;.
260e3 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20      p->nAlloc = 
260e4 70 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b 20 6e 65  p->nAlloc*2 + ne
260e5 65 64 20 2b 20 31 3b 0a 20 20 20 20 61 7a 4e 65  ed + 1;.    azNe
260e6 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  w = sqlite3_real
260e7 6c 6f 63 28 20 70 2d 3e 61 7a 52 65 73 75 6c 74  loc( p->azResult
260e8 2c 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a  , sizeof(char*)*
260e9 70 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 20  p->nAlloc );.   
260ea 20 69 66 28 20 61 7a 4e 65 77 3d 3d 30 20 29 20   if( azNew==0 ) 
260eb 67 6f 74 6f 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c  goto malloc_fail
260ec 65 64 3b 0a 20 20 20 20 70 2d 3e 61 7a 52 65 73  ed;.    p->azRes
260ed 75 6c 74 20 3d 20 61 7a 4e 65 77 3b 0a 20 20 7d  ult = azNew;.  }
260ee 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69  ..  /* If this i
260ef 73 20 74 68 65 20 66 69 72 73 74 20 72 6f 77 2c  s the first row,
260f0 20 74 68 65 6e 20 67 65 6e 65 72 61 74 65 20 61   then generate a
260f1 6e 20 65 78 74 72 61 20 72 6f 77 20 63 6f 6e 74  n extra row cont
260f2 61 69 6e 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20  aining.  ** the 
260f3 6e 61 6d 65 73 20 6f 66 20 61 6c 6c 20 63 6f 6c  names of all col
260f4 75 6d 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  umns..  */.  if(
260f5 20 70 2d 3e 6e 52 6f 77 3d 3d 30 20 29 7b 0a 20   p->nRow==0 ){. 
260f6 20 20 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20     p->nColumn = 
260f7 6e 43 6f 6c 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nCol;.    for(i=
260f8 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  0; i<nCol; i++){
260f9 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74  .      z = sqlit
260fa 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c  e3_mprintf("%s",
260fb 20 63 6f 6c 76 5b 69 5d 29 3b 0a 20 20 20 20 20   colv[i]);.     
260fc 20 69 66 28 20 7a 3d 3d 30 20 29 20 67 6f 74 6f   if( z==0 ) goto
260fd 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a   malloc_failed;.
260fe 20 20 20 20 20 20 70 2d 3e 61 7a 52 65 73 75 6c        p->azResul
260ff 74 5b 70 2d 3e 6e 44 61 74 61 2b 2b 5d 20 3d 20  t[p->nData++] = 
26100 7a 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  z;.    }.  }else
26101 20 69 66 28 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 21   if( p->nColumn!
26102 3d 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 73 71 6c  =nCol ){.    sql
26103 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 45 72  ite3_free(p->zEr
26104 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45  rMsg);.    p->zE
26105 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 5f  rrMsg = sqlite3_
26106 6d 70 72 69 6e 74 66 28 0a 20 20 20 20 20 20 20  mprintf(.       
26107 22 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62  "sqlite3_get_tab
26108 6c 65 28 29 20 63 61 6c 6c 65 64 20 77 69 74 68  le() called with
26109 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 69 6e 63   two or more inc
2610a 6f 6d 70 61 74 69 62 6c 65 20 71 75 65 72 69 65  ompatible querie
2610b 73 22 0a 20 20 20 20 29 3b 0a 20 20 20 20 70 2d  s".    );.    p-
2610c 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52  >rc = SQLITE_ERR
2610d 4f 52 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  OR;.    return 1
2610e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79  ;.  }..  /* Copy
2610f 20 6f 76 65 72 20 74 68 65 20 72 6f 77 20 64 61   over the row da
26110 74 61 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61 72  ta.  */.  if( ar
26111 67 76 21 3d 30 20 29 7b 0a 20 20 20 20 66 6f 72  gv!=0 ){.    for
26112 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b  (i=0; i<nCol; i+
26113 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 72  +){.      if( ar
26114 67 76 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20  gv[i]==0 ){.    
26115 20 20 20 20 7a 20 3d 20 30 3b 0a 20 20 20 20 20      z = 0;.     
26116 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
26117 69 6e 74 20 6e 20 3d 20 73 74 72 6c 65 6e 28 61  int n = strlen(a
26118 72 67 76 5b 69 5d 29 2b 31 3b 0a 20 20 20 20 20  rgv[i])+1;.     
26119 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 6d     z = sqlite3_m
2611a 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 20 20  alloc( n );.    
2611b 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 67      if( z==0 ) g
2611c 6f 74 6f 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65  oto malloc_faile
2611d 64 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  d;.        memcp
2611e 79 28 7a 2c 20 61 72 67 76 5b 69 5d 2c 20 6e 29  y(z, argv[i], n)
2611f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
26120 70 2d 3e 61 7a 52 65 73 75 6c 74 5b 70 2d 3e 6e  p->azResult[p->n
26121 44 61 74 61 2b 2b 5d 20 3d 20 7a 3b 0a 20 20 20  Data++] = z;.   
26122 20 7d 0a 20 20 20 20 70 2d 3e 6e 52 6f 77 2b 2b   }.    p->nRow++
26123 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
26124 3b 0a 0a 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64  ;..malloc_failed
26125 3a 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49  :.  p->rc = SQLI
26126 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 72 65 74 75  TE_NOMEM;.  retu
26127 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51  rn 1;.}../*.** Q
26128 75 65 72 79 20 74 68 65 20 64 61 74 61 62 61 73  uery the databas
26129 65 2e 20 20 42 75 74 20 69 6e 73 74 65 61 64 20  e.  But instead 
2612a 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 61 20 63 61  of invoking a ca
2612b 6c 6c 62 61 63 6b 20 66 6f 72 20 65 61 63 68 20  llback for each 
2612c 72 6f 77 2c 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29  row,.** malloc()
2612d 20 66 6f 72 20 73 70 61 63 65 20 74 6f 20 68 6f   for space to ho
2612e 6c 64 20 74 68 65 20 72 65 73 75 6c 74 20 61 6e  ld the result an
2612f 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 6e 74  d return the ent
26130 69 72 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 61  ire results.** a
26131 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e  t the conclusion
26132 20 6f 66 20 74 68 65 20 63 61 6c 6c 2e 0a 2a 2a   of the call..**
26133 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 74  .** The result t
26134 68 61 74 20 69 73 20 77 72 69 74 74 65 6e 20 74  hat is written t
26135 6f 20 2a 2a 2a 70 61 7a 52 65 73 75 6c 74 20 69  o ***pazResult i
26136 73 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79  s held in memory
26137 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f   obtained.** fro
26138 6d 20 6d 61 6c 6c 6f 63 28 29 2e 20 20 42 75 74  m malloc().  But
26139 20 74 68 65 20 63 61 6c 6c 65 72 20 63 61 6e 6e   the caller cann
2613a 6f 74 20 66 72 65 65 20 74 68 69 73 20 6d 65 6d  ot free this mem
2613b 6f 72 79 20 64 69 72 65 63 74 6c 79 2e 20 20 0a  ory directly.  .
2613c 2a 2a 20 49 6e 73 74 65 61 64 2c 20 74 68 65 20  ** Instead, the 
2613d 65 6e 74 69 72 65 20 74 61 62 6c 65 20 73 68 6f  entire table sho
2613e 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 74 6f  uld be passed to
2613f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61   sqlite3_free_ta
26140 62 6c 65 28 29 20 77 68 65 6e 0a 2a 2a 20 74 68  ble() when.** th
26141 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 64  e calling proced
26142 75 72 65 20 69 73 20 66 69 6e 69 73 68 65 64 20  ure is finished 
26143 75 73 69 6e 67 20 69 74 2e 0a 2a 2f 0a 53 51 4c  using it..*/.SQL
26144 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
26145 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 0a 20  te3_get_table(. 
26146 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
26147 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26148 54 68 65 20 64 61 74 61 62 61 73 65 20 6f 6e 20  The database on 
26149 77 68 69 63 68 20 74 68 65 20 53 51 4c 20 65 78  which the SQL ex
2614a 65 63 75 74 65 73 20 2a 2f 0a 20 20 63 6f 6e 73  ecutes */.  cons
2614b 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20  t char *zSql,   
2614c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53          /* The S
2614d 51 4c 20 74 6f 20 62 65 20 65 78 65 63 75 74 65  QL to be execute
2614e 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 2a 70  d */.  char ***p
2614f 61 7a 52 65 73 75 6c 74 2c 20 20 20 20 20 20 20  azResult,       
26150 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
26151 72 65 73 75 6c 74 20 74 61 62 6c 65 20 68 65 72  result table her
26152 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52 6f  e */.  int *pnRo
26153 77 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  w,              
26154 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
26155 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69  number of rows i
26156 6e 20 74 68 65 20 72 65 73 75 6c 74 20 68 65 72  n the result her
26157 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f  e */.  int *pnCo
26158 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20 20  lumn,           
26159 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
2615a 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
2615b 73 20 6f 66 20 72 65 73 75 6c 74 20 68 65 72 65  s of result here
2615c 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45   */.  char **pzE
2615d 72 72 4d 73 67 20 20 20 20 20 20 20 20 20 20 20  rrMsg           
2615e 20 20 2f 2a 20 57 72 69 74 65 20 65 72 72 6f 72    /* Write error
2615f 20 6d 65 73 73 61 67 65 73 20 68 65 72 65 20 2a   messages here *
26160 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  /.){.  int rc;. 
26161 20 54 61 62 52 65 73 75 6c 74 20 72 65 73 3b 0a   TabResult res;.
26162 0a 20 20 2a 70 61 7a 52 65 73 75 6c 74 20 3d 20  .  *pazResult = 
26163 30 3b 0a 20 20 69 66 28 20 70 6e 43 6f 6c 75 6d  0;.  if( pnColum
26164 6e 20 29 20 2a 70 6e 43 6f 6c 75 6d 6e 20 3d 20  n ) *pnColumn = 
26165 30 3b 0a 20 20 69 66 28 20 70 6e 52 6f 77 20 29  0;.  if( pnRow )
26166 20 2a 70 6e 52 6f 77 20 3d 20 30 3b 0a 20 20 72   *pnRow = 0;.  r
26167 65 73 2e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a  es.zErrMsg = 0;.
26168 20 20 72 65 73 2e 6e 52 65 73 75 6c 74 20 3d 20    res.nResult = 
26169 30 3b 0a 20 20 72 65 73 2e 6e 52 6f 77 20 3d 20  0;.  res.nRow = 
2616a 30 3b 0a 20 20 72 65 73 2e 6e 43 6f 6c 75 6d 6e  0;.  res.nColumn
2616b 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e 44 61 74   = 0;.  res.nDat
2616c 61 20 3d 20 31 3b 0a 20 20 72 65 73 2e 6e 41 6c  a = 1;.  res.nAl
2616d 6c 6f 63 20 3d 20 32 30 3b 0a 20 20 72 65 73 2e  loc = 20;.  res.
2616e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
2616f 20 20 72 65 73 2e 61 7a 52 65 73 75 6c 74 20 3d    res.azResult =
26170 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
26171 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a 72 65  sizeof(char*)*re
26172 73 2e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 69 66  s.nAlloc );.  if
26173 28 20 72 65 73 2e 61 7a 52 65 73 75 6c 74 3d 3d  ( res.azResult==
26174 30 20 29 7b 0a 20 20 20 20 20 64 62 2d 3e 65 72  0 ){.     db->er
26175 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4e  rCode = SQLITE_N
26176 4f 4d 45 4d 3b 0a 20 20 20 20 20 72 65 74 75 72  OMEM;.     retur
26177 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
26178 20 20 7d 0a 20 20 72 65 73 2e 61 7a 52 65 73 75    }.  res.azResu
26179 6c 74 5b 30 5d 20 3d 20 30 3b 0a 20 20 72 63 20  lt[0] = 0;.  rc 
2617a 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64  = sqlite3_exec(d
2617b 62 2c 20 7a 53 71 6c 2c 20 73 71 6c 69 74 65 33  b, zSql, sqlite3
2617c 5f 67 65 74 5f 74 61 62 6c 65 5f 63 62 2c 20 26  _get_table_cb, &
2617d 72 65 73 2c 20 70 7a 45 72 72 4d 73 67 29 3b 0a  res, pzErrMsg);.
2617e 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
2617f 28 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 30 5d  (res.azResult[0]
26180 29 3e 3d 20 73 69 7a 65 6f 66 28 72 65 73 2e 6e  )>= sizeof(res.n
26181 44 61 74 61 29 20 29 3b 0a 20 20 72 65 73 2e 61  Data) );.  res.a
26182 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20 53 51 4c  zResult[0] = SQL
26183 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 72  ITE_INT_TO_PTR(r
26184 65 73 2e 6e 44 61 74 61 29 3b 0a 20 20 69 66 28  es.nData);.  if(
26185 20 28 72 63 26 30 78 66 66 29 3d 3d 53 51 4c 49   (rc&0xff)==SQLI
26186 54 45 5f 41 42 4f 52 54 20 29 7b 0a 20 20 20 20  TE_ABORT ){.    
26187 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62  sqlite3_free_tab
26188 6c 65 28 26 72 65 73 2e 61 7a 52 65 73 75 6c 74  le(&res.azResult
26189 5b 31 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 65  [1]);.    if( re
2618a 73 2e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20  s.zErrMsg ){.   
2618b 20 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20     if( pzErrMsg 
2618c 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2618d 65 33 5f 66 72 65 65 28 2a 70 7a 45 72 72 4d 73  e3_free(*pzErrMs
2618e 67 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 7a 45  g);.        *pzE
2618f 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 5f  rrMsg = sqlite3_
26190 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 72 65 73  mprintf("%s",res
26191 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20  .zErrMsg);.     
26192 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
26193 5f 66 72 65 65 28 72 65 73 2e 7a 45 72 72 4d 73  _free(res.zErrMs
26194 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62  g);.    }.    db
26195 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 65 73 2e  ->errCode = res.
26196 72 63 3b 20 20 2f 2a 20 41 73 73 75 6d 65 20 33  rc;  /* Assume 3
26197 32 2d 62 69 74 20 61 73 73 69 67 6e 6d 65 6e 74  2-bit assignment
26198 20 69 73 20 61 74 6f 6d 69 63 20 2a 2f 0a 20 20   is atomic */.  
26199 20 20 72 65 74 75 72 6e 20 72 65 73 2e 72 63 3b    return res.rc;
2619a 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66  .  }.  sqlite3_f
2619b 72 65 65 28 72 65 73 2e 7a 45 72 72 4d 73 67 29  ree(res.zErrMsg)
2619c 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
2619d 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c  TE_OK ){.    sql
2619e 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28  ite3_free_table(
2619f 26 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31 5d  &res.azResult[1]
261a0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  );.    return rc
261a1 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 65 73 2e  ;.  }.  if( res.
261a2 6e 41 6c 6c 6f 63 3e 72 65 73 2e 6e 44 61 74 61  nAlloc>res.nData
261a3 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a 61   ){.    char **a
261a4 7a 4e 65 77 3b 0a 20 20 20 20 61 7a 4e 65 77 20  zNew;.    azNew 
261a5 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  = sqlite3_reallo
261a6 63 28 20 72 65 73 2e 61 7a 52 65 73 75 6c 74 2c  c( res.azResult,
261a7 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a 28   sizeof(char*)*(
261a8 72 65 73 2e 6e 44 61 74 61 2b 31 29 20 29 3b 0a  res.nData+1) );.
261a9 20 20 20 20 69 66 28 20 61 7a 4e 65 77 3d 3d 30      if( azNew==0
261aa 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
261ab 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 26 72 65  3_free_table(&re
261ac 73 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 29 3b 0a  s.azResult[1]);.
261ad 20 20 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64        db->errCod
261ae 65 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  e = SQLITE_NOMEM
261af 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
261b0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
261b1 20 7d 0a 20 20 20 20 72 65 73 2e 6e 41 6c 6c 6f   }.    res.nAllo
261b2 63 20 3d 20 72 65 73 2e 6e 44 61 74 61 2b 31 3b  c = res.nData+1;
261b3 0a 20 20 20 20 72 65 73 2e 61 7a 52 65 73 75 6c  .    res.azResul
261b4 74 20 3d 20 61 7a 4e 65 77 3b 0a 20 20 7d 0a 20  t = azNew;.  }. 
261b5 20 2a 70 61 7a 52 65 73 75 6c 74 20 3d 20 26 72   *pazResult = &r
261b6 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 3b 0a  es.azResult[1];.
261b7 20 20 69 66 28 20 70 6e 43 6f 6c 75 6d 6e 20 29    if( pnColumn )
261b8 20 2a 70 6e 43 6f 6c 75 6d 6e 20 3d 20 72 65 73   *pnColumn = res
261b9 2e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69 66 28 20  .nColumn;.  if( 
261ba 70 6e 52 6f 77 20 29 20 2a 70 6e 52 6f 77 20 3d  pnRow ) *pnRow =
261bb 20 72 65 73 2e 6e 52 6f 77 3b 0a 20 20 72 65 74   res.nRow;.  ret
261bc 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
261bd 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72   This routine fr
261be 65 65 73 20 74 68 65 20 73 70 61 63 65 20 74 68  ees the space th
261bf 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61  e sqlite3_get_ta
261c0 62 6c 65 28 29 20 6d 61 6c 6c 6f 63 65 64 2e 0a  ble() malloced..
261c1 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
261c2 69 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f  id sqlite3_free_
261c3 74 61 62 6c 65 28 0a 20 20 63 68 61 72 20 2a 2a  table(.  char **
261c4 61 7a 52 65 73 75 6c 74 20 20 20 20 20 20 20 20  azResult        
261c5 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 72 65      /* Result re
261c6 74 75 72 6e 65 64 20 66 72 6f 6d 20 66 72 6f 6d  turned from from
261c7 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62   sqlite3_get_tab
261c8 6c 65 28 29 20 2a 2f 0a 29 7b 0a 20 20 69 66 28  le() */.){.  if(
261c9 20 61 7a 52 65 73 75 6c 74 20 29 7b 0a 20 20 20   azResult ){.   
261ca 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 20 20 61   int i, n;.    a
261cb 7a 52 65 73 75 6c 74 2d 2d 3b 0a 20 20 20 20 61  zResult--;.    a
261cc 73 73 65 72 74 28 20 61 7a 52 65 73 75 6c 74 21  ssert( azResult!
261cd 3d 30 20 29 3b 0a 20 20 20 20 6e 20 3d 20 53 51  =0 );.    n = SQ
261ce 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28  LITE_PTR_TO_INT(
261cf 61 7a 52 65 73 75 6c 74 5b 30 5d 29 3b 0a 20 20  azResult[0]);.  
261d0 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 6e 3b 20    for(i=1; i<n; 
261d1 69 2b 2b 29 7b 20 69 66 28 20 61 7a 52 65 73 75  i++){ if( azResu
261d2 6c 74 5b 69 5d 20 29 20 73 71 6c 69 74 65 33 5f  lt[i] ) sqlite3_
261d3 66 72 65 65 28 61 7a 52 65 73 75 6c 74 5b 69 5d  free(azResult[i]
261d4 29 3b 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33  ); }.    sqlite3
261d5 5f 66 72 65 65 28 61 7a 52 65 73 75 6c 74 29 3b  _free(azResult);
261d6 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  .  }.}..#endif /
261d7 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47 45  * SQLITE_OMIT_GE
261d8 54 5f 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a 2a  T_TABLE */../***
261d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
261da 6f 66 20 74 61 62 6c 65 2e 63 20 2a 2a 2a 2a 2a  of table.c *****
261db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
261dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
261dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
261de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
261df 6e 20 66 69 6c 65 20 74 72 69 67 67 65 72 2e 63  n file trigger.c
261e0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
261e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
261e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
261e3 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
261e4 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
261e5 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
261e6 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
261e7 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
261e8 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
261e9 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
261ea 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
261eb 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
261ec 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
261ed 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
261ee 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
261ef 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
261f0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
261f1 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
261f2 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
261f3 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
261f4 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
261f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
261f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
261f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
261f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
261f9 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 74 72 69 67  .**.** $Id: trig
261fa 67 65 72 2e 63 2c 76 20 31 2e 31 32 39 20 32 30  ger.c,v 1.129 20
261fb 30 38 2f 30 38 2f 32 30 20 31 36 3a 33 35 3a 31  08/08/20 16:35:1
261fc 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  0 drh Exp $.*/..
261fd 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
261fe 4d 49 54 5f 54 52 49 47 47 45 52 0a 2f 2a 0a 2a  MIT_TRIGGER./*.*
261ff 2a 20 44 65 6c 65 74 65 20 61 20 6c 69 6e 6b 65  * Delete a linke
26200 64 20 6c 69 73 74 20 6f 66 20 54 72 69 67 67 65  d list of Trigge
26201 72 53 74 65 70 20 73 74 72 75 63 74 75 72 65 73  rStep structures
26202 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
26203 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
26204 44 65 6c 65 74 65 54 72 69 67 67 65 72 53 74 65  DeleteTriggerSte
26205 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54  p(sqlite3 *db, T
26206 72 69 67 67 65 72 53 74 65 70 20 2a 70 54 72 69  riggerStep *pTri
26207 67 67 65 72 53 74 65 70 29 7b 0a 20 20 77 68 69  ggerStep){.  whi
26208 6c 65 28 20 70 54 72 69 67 67 65 72 53 74 65 70  le( pTriggerStep
26209 20 29 7b 0a 20 20 20 20 54 72 69 67 67 65 72 53   ){.    TriggerS
2620a 74 65 70 20 2a 20 70 54 6d 70 20 3d 20 70 54 72  tep * pTmp = pTr
2620b 69 67 67 65 72 53 74 65 70 3b 0a 20 20 20 20 70  iggerStep;.    p
2620c 54 72 69 67 67 65 72 53 74 65 70 20 3d 20 70 54  TriggerStep = pT
2620d 72 69 67 67 65 72 53 74 65 70 2d 3e 70 4e 65 78  riggerStep->pNex
2620e 74 3b 0a 0a 20 20 20 20 69 66 28 20 70 54 6d 70  t;..    if( pTmp
2620f 2d 3e 74 61 72 67 65 74 2e 64 79 6e 20 29 20 73  ->target.dyn ) s
26210 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
26211 20 28 63 68 61 72 2a 29 70 54 6d 70 2d 3e 74 61   (char*)pTmp->ta
26212 72 67 65 74 2e 7a 29 3b 0a 20 20 20 20 73 71 6c  rget.z);.    sql
26213 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
26214 62 2c 20 70 54 6d 70 2d 3e 70 57 68 65 72 65 29  b, pTmp->pWhere)
26215 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
26216 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
26217 70 54 6d 70 2d 3e 70 45 78 70 72 4c 69 73 74 29  pTmp->pExprList)
26218 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c  ;.    sqlite3Sel
26219 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54  ectDelete(db, pT
2621a 6d 70 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20  mp->pSelect);.  
2621b 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44    sqlite3IdListD
2621c 65 6c 65 74 65 28 64 62 2c 20 70 54 6d 70 2d 3e  elete(db, pTmp->
2621d 70 49 64 4c 69 73 74 29 3b 0a 0a 20 20 20 20 73  pIdList);..    s
2621e 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2621f 20 70 54 6d 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f   pTmp);.  }.}../
26220 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 63 61 6c  *.** This is cal
26221 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65  led by the parse
26222 72 20 77 68 65 6e 20 69 74 20 73 65 65 73 20 61  r when it sees a
26223 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20   CREATE TRIGGER 
26224 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 75 70 20  statement.** up 
26225 74 6f 20 74 68 65 20 70 6f 69 6e 74 20 6f 66 20  to the point of 
26226 74 68 65 20 42 45 47 49 4e 20 62 65 66 6f 72 65  the BEGIN before
26227 20 74 68 65 20 74 72 69 67 67 65 72 20 61 63 74   the trigger act
26228 69 6f 6e 73 2e 20 20 41 20 54 72 69 67 67 65 72  ions.  A Trigger
26229 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69 73  .** structure is
2622a 20 67 65 6e 65 72 61 74 65 64 20 62 61 73 65 64   generated based
2622b 20 6f 6e 20 74 68 65 20 69 6e 66 6f 72 6d 61 74   on the informat
2622c 69 6f 6e 20 61 76 61 69 6c 61 62 6c 65 20 61 6e  ion available an
2622d 64 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 70  d stored.** in p
2622e 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67  Parse->pNewTrigg
2622f 65 72 2e 20 20 41 66 74 65 72 20 74 68 65 20 74  er.  After the t
26230 72 69 67 67 65 72 20 61 63 74 69 6f 6e 73 20 68  rigger actions h
26231 61 76 65 20 62 65 65 6e 20 70 61 72 73 65 64 2c  ave been parsed,
26232 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 46   the.** sqlite3F
26233 69 6e 69 73 68 54 72 69 67 67 65 72 28 29 20 66  inishTrigger() f
26234 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
26235 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68  d to complete th
26236 65 20 74 72 69 67 67 65 72 0a 2a 2a 20 63 6f 6e  e trigger.** con
26237 73 74 72 75 63 74 69 6f 6e 20 70 72 6f 63 65 73  struction proces
26238 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
26239 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2623a 33 42 65 67 69 6e 54 72 69 67 67 65 72 28 0a 20  3BeginTrigger(. 
2623b 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2623c 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
2623d 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 68 65  e context of the
2623e 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20   CREATE TRIGGER 
2623f 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 54  statement */.  T
26240 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20  oken *pName1,   
26241 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f     /* The name o
26242 66 20 74 68 65 20 74 72 69 67 67 65 72 20 2a 2f  f the trigger */
26243 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32  .  Token *pName2
26244 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 61  ,      /* The na
26245 6d 65 20 6f 66 20 74 68 65 20 74 72 69 67 67 65  me of the trigge
26246 72 20 2a 2f 0a 20 20 69 6e 74 20 74 72 5f 74 6d  r */.  int tr_tm
26247 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e  ,          /* On
26248 65 20 6f 66 20 54 4b 5f 42 45 46 4f 52 45 2c 20  e of TK_BEFORE, 
26249 54 4b 5f 41 46 54 45 52 2c 20 54 4b 5f 49 4e 53  TK_AFTER, TK_INS
2624a 54 45 41 44 20 2a 2f 0a 20 20 69 6e 74 20 6f 70  TEAD */.  int op
2624b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
2624c 20 4f 6e 65 20 6f 66 20 54 4b 5f 49 4e 53 45 52   One of TK_INSER
2624d 54 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b  T, TK_UPDATE, TK
2624e 5f 44 45 4c 45 54 45 20 2a 2f 0a 20 20 49 64 4c  _DELETE */.  IdL
2624f 69 73 74 20 2a 70 43 6f 6c 75 6d 6e 73 2c 20 20  ist *pColumns,  
26250 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 20   /* column list 
26251 69 66 20 74 68 69 73 20 69 73 20 61 6e 20 55 50  if this is an UP
26252 44 41 54 45 20 4f 46 20 74 72 69 67 67 65 72 20  DATE OF trigger 
26253 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54  */.  SrcList *pT
26254 61 62 6c 65 4e 61 6d 65 2c 2f 2a 20 54 68 65 20  ableName,/* The 
26255 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  name of the tabl
26256 65 2f 76 69 65 77 20 74 68 65 20 74 72 69 67 67  e/view the trigg
26257 65 72 20 61 70 70 6c 69 65 73 20 74 6f 20 2a 2f  er applies to */
26258 0a 20 20 45 78 70 72 20 2a 70 57 68 65 6e 2c 20  .  Expr *pWhen, 
26259 20 20 20 20 20 20 20 2f 2a 20 57 48 45 4e 20 63         /* WHEN c
2625a 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69  lause */.  int i
2625b 73 54 65 6d 70 2c 20 20 20 20 20 20 20 20 20 2f  sTemp,         /
2625c 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 54 45  * True if the TE
2625d 4d 50 4f 52 41 52 59 20 6b 65 79 77 6f 72 64 20  MPORARY keyword 
2625e 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20  is present */.  
2625f 69 6e 74 20 6e 6f 45 72 72 20 20 20 20 20 20 20  int noErr       
26260 20 20 20 20 2f 2a 20 53 75 70 70 72 65 73 73 20      /* Suppress 
26261 65 72 72 6f 72 73 20 69 66 20 74 68 65 20 74 72  errors if the tr
26262 69 67 67 65 72 20 61 6c 72 65 61 64 79 20 65 78  igger already ex
26263 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 20 54 72 69  ists */.){.  Tri
26264 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 20 3d  gger *pTrigger =
26265 20 30 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   0;.  Table *pTa
26266 62 3b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65  b;.  char *zName
26267 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
26268 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 72 69 67  Name of the trig
26269 67 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ger */.  sqlite3
2626a 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2626b 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  b;.  int iDb;   
2626c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2626d 54 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  The database to 
2626e 73 74 6f 72 65 20 74 68 65 20 74 72 69 67 67 65  store the trigge
2626f 72 20 69 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  r in */.  Token 
26270 2a 70 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  *pName;         
26271 20 20 2f 2a 20 54 68 65 20 75 6e 71 75 61 6c 69    /* The unquali
26272 66 69 65 64 20 64 62 20 6e 61 6d 65 20 2a 2f 0a  fied db name */.
26273 20 20 44 62 46 69 78 65 72 20 73 46 69 78 3b 0a    DbFixer sFix;.
26274 20 20 69 6e 74 20 69 54 61 62 44 62 3b 0a 0a 20    int iTabDb;.. 
26275 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 31 21   assert( pName1!
26276 3d 30 20 29 3b 20 20 20 2f 2a 20 70 4e 61 6d 65  =0 );   /* pName
26277 31 2d 3e 7a 20 6d 69 67 68 74 20 62 65 20 4e 55  1->z might be NU
26278 4c 4c 2c 20 62 75 74 20 6e 6f 74 20 70 4e 61 6d  LL, but not pNam
26279 65 31 20 69 74 73 65 6c 66 20 2a 2f 0a 20 20 61  e1 itself */.  a
2627a 73 73 65 72 74 28 20 70 4e 61 6d 65 32 21 3d 30  ssert( pName2!=0
2627b 20 29 3b 0a 20 20 69 66 28 20 69 73 54 65 6d 70   );.  if( isTemp
2627c 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 54 45   ){.    /* If TE
2627d 4d 50 20 77 61 73 20 73 70 65 63 69 66 69 65 64  MP was specified
2627e 2c 20 74 68 65 6e 20 74 68 65 20 74 72 69 67 67  , then the trigg
2627f 65 72 20 6e 61 6d 65 20 6d 61 79 20 6e 6f 74 20  er name may not 
26280 62 65 20 71 75 61 6c 69 66 69 65 64 2e 20 2a 2f  be qualified. */
26281 0a 20 20 20 20 69 66 28 20 70 4e 61 6d 65 32 2d  .    if( pName2-
26282 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71  >n>0 ){.      sq
26283 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
26284 61 72 73 65 2c 20 22 74 65 6d 70 6f 72 61 72 79  arse, "temporary
26285 20 74 72 69 67 67 65 72 20 6d 61 79 20 6e 6f 74   trigger may not
26286 20 68 61 76 65 20 71 75 61 6c 69 66 69 65 64 20   have qualified 
26287 6e 61 6d 65 22 29 3b 0a 20 20 20 20 20 20 67 6f  name");.      go
26288 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  to trigger_clean
26289 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 44  up;.    }.    iD
2628a 62 20 3d 20 31 3b 0a 20 20 20 20 70 4e 61 6d 65  b = 1;.    pName
2628b 20 3d 20 70 4e 61 6d 65 31 3b 0a 20 20 7d 65 6c   = pName1;.  }el
2628c 73 65 7b 0a 20 20 20 20 2f 2a 20 46 69 67 75 72  se{.    /* Figur
2628d 65 20 6f 75 74 20 74 68 65 20 64 62 20 74 68 61  e out the db tha
2628e 74 20 74 68 65 20 74 68 65 20 74 72 69 67 67 65  t the the trigge
2628f 72 20 77 69 6c 6c 20 62 65 20 63 72 65 61 74 65  r will be create
26290 64 20 69 6e 20 2a 2f 0a 20 20 20 20 69 44 62 20  d in */.    iDb 
26291 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74  = sqlite3TwoPart
26292 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61  Name(pParse, pNa
26293 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e  me1, pName2, &pN
26294 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 69 44  ame);.    if( iD
26295 62 3c 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  b<0 ){.      got
26296 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  o trigger_cleanu
26297 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  p;.    }.  }..  
26298 2f 2a 20 49 66 20 74 68 65 20 74 72 69 67 67 65  /* If the trigge
26299 72 20 6e 61 6d 65 20 77 61 73 20 75 6e 71 75 61  r name was unqua
2629a 6c 69 66 69 65 64 2c 20 61 6e 64 20 74 68 65 20  lified, and the 
2629b 74 61 62 6c 65 20 69 73 20 61 20 74 65 6d 70 20  table is a temp 
2629c 74 61 62 6c 65 2c 0a 20 20 2a 2a 20 74 68 65 6e  table,.  ** then
2629d 20 73 65 74 20 69 44 62 20 74 6f 20 31 20 74 6f   set iDb to 1 to
2629e 20 63 72 65 61 74 65 20 74 68 65 20 74 72 69 67   create the trig
2629f 67 65 72 20 69 6e 20 74 68 65 20 74 65 6d 70 6f  ger in the tempo
262a0 72 61 72 79 20 64 61 74 61 62 61 73 65 2e 0a 20  rary database.. 
262a1 20 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 53 72   ** If sqlite3Sr
262a2 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 29 20 72 65  cListLookup() re
262a3 74 75 72 6e 73 20 30 2c 20 69 6e 64 69 63 61 74  turns 0, indicat
262a4 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 64 6f  ing the table do
262a5 65 73 20 6e 6f 74 0a 20 20 2a 2a 20 65 78 69 73  es not.  ** exis
262a6 74 2c 20 74 68 65 20 65 72 72 6f 72 20 69 73 20  t, the error is 
262a7 63 61 75 67 68 74 20 62 79 20 74 68 65 20 62 6c  caught by the bl
262a8 6f 63 6b 20 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a  ock below..  */.
262a9 20 20 69 66 28 20 21 70 54 61 62 6c 65 4e 61 6d    if( !pTableNam
262aa 65 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  e || db->mallocF
262ab 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74  ailed ){.    got
262ac 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  o trigger_cleanu
262ad 70 3b 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d 20  p;.  }.  pTab = 
262ae 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f  sqlite3SrcListLo
262af 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54 61  okup(pParse, pTa
262b0 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20  bleName);.  if( 
262b1 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 20 26 26 20  pName2->n==0 && 
262b2 70 54 61 62 20 26 26 20 70 54 61 62 2d 3e 70 53  pTab && pTab->pS
262b3 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b 31  chema==db->aDb[1
262b4 5d 2e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20  ].pSchema ){.   
262b5 20 69 44 62 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20   iDb = 1;.  }.. 
262b6 20 2f 2a 20 45 6e 73 75 72 65 20 74 68 65 20 74   /* Ensure the t
262b7 61 62 6c 65 20 6e 61 6d 65 20 6d 61 74 63 68 65  able name matche
262b8 73 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  s database name 
262b9 61 6e 64 20 74 68 61 74 20 74 68 65 20 74 61 62  and that the tab
262ba 6c 65 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 69  le exists */.  i
262bb 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
262bc 6c 65 64 20 29 20 67 6f 74 6f 20 74 72 69 67 67  led ) goto trigg
262bd 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 61 73  er_cleanup;.  as
262be 73 65 72 74 28 20 70 54 61 62 6c 65 4e 61 6d 65  sert( pTableName
262bf 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 69  ->nSrc==1 );.  i
262c0 66 28 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69  f( sqlite3FixIni
262c1 74 28 26 73 46 69 78 2c 20 70 50 61 72 73 65 2c  t(&sFix, pParse,
262c2 20 69 44 62 2c 20 22 74 72 69 67 67 65 72 22 2c   iDb, "trigger",
262c3 20 70 4e 61 6d 65 29 20 26 26 20 0a 20 20 20 20   pName) && .    
262c4 20 20 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c    sqlite3FixSrcL
262c5 69 73 74 28 26 73 46 69 78 2c 20 70 54 61 62 6c  ist(&sFix, pTabl
262c6 65 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f  eName) ){.    go
262c7 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  to trigger_clean
262c8 75 70 3b 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d  up;.  }.  pTab =
262c9 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c   sqlite3SrcListL
262ca 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54  ookup(pParse, pT
262cb 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 69 66 28  ableName);.  if(
262cc 20 21 70 54 61 62 20 29 7b 0a 20 20 20 20 2f 2a   !pTab ){.    /*
262cd 20 54 68 65 20 74 61 62 6c 65 20 64 6f 65 73 20   The table does 
262ce 6e 6f 74 20 65 78 69 73 74 2e 20 2a 2f 0a 20 20  not exist. */.  
262cf 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63    goto trigger_c
262d0 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66  leanup;.  }.  if
262d1 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  ( IsVirtual(pTab
262d2 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
262d3 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
262d4 20 22 63 61 6e 6e 6f 74 20 63 72 65 61 74 65 20   "cannot create 
262d5 74 72 69 67 67 65 72 73 20 6f 6e 20 76 69 72 74  triggers on virt
262d6 75 61 6c 20 74 61 62 6c 65 73 22 29 3b 0a 20 20  ual tables");.  
262d7 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63    goto trigger_c
262d8 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f  leanup;.  }..  /
262d9 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65  * Check that the
262da 20 74 72 69 67 67 65 72 20 6e 61 6d 65 20 69 73   trigger name is
262db 20 6e 6f 74 20 72 65 73 65 72 76 65 64 20 61 6e   not reserved an
262dc 64 20 74 68 61 74 20 6e 6f 20 74 72 69 67 67 65  d that no trigge
262dd 72 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 73 70  r of the.  ** sp
262de 65 63 69 66 69 65 64 20 6e 61 6d 65 20 65 78 69  ecified name exi
262df 73 74 73 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20 3d  sts */.  zName =
262e0 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
262e1 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29  Token(db, pName)
262e2 3b 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20 7c  ;.  if( !zName |
262e3 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c  | SQLITE_OK!=sql
262e4 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e  ite3CheckObjectN
262e5 61 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d  ame(pParse, zNam
262e6 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74  e) ){.    goto t
262e7 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a  rigger_cleanup;.
262e8 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65    }.  if( sqlite
262e9 33 48 61 73 68 46 69 6e 64 28 26 28 64 62 2d 3e  3HashFind(&(db->
262ea 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61  aDb[iDb].pSchema
262eb 2d 3e 74 72 69 67 48 61 73 68 29 2c 20 7a 4e 61  ->trigHash), zNa
262ec 6d 65 2c 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29  me,strlen(zName)
262ed 29 20 29 7b 0a 20 20 20 20 69 66 28 20 21 6e 6f  ) ){.    if( !no
262ee 45 72 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Err ){.      sql
262ef 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
262f0 72 73 65 2c 20 22 74 72 69 67 67 65 72 20 25 54  rse, "trigger %T
262f1 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 22   already exists"
262f2 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a  , pName);.    }.
262f3 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72      goto trigger
262f4 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20  _cleanup;.  }.. 
262f5 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 72 65 61 74   /* Do not creat
262f6 65 20 61 20 74 72 69 67 67 65 72 20 6f 6e 20 61  e a trigger on a
262f7 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20 2a 2f   system table */
262f8 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
262f9 72 4e 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61  rNICmp(pTab->zNa
262fa 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37  me, "sqlite_", 7
262fb 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  )==0 ){.    sqli
262fc 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
262fd 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63 72 65 61  se, "cannot crea
262fe 74 65 20 74 72 69 67 67 65 72 20 6f 6e 20 73 79  te trigger on sy
262ff 73 74 65 6d 20 74 61 62 6c 65 22 29 3b 0a 20 20  stem table");.  
26300 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b    pParse->nErr++
26301 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67  ;.    goto trigg
26302 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  er_cleanup;.  }.
26303 0a 20 20 2f 2a 20 49 4e 53 54 45 41 44 20 6f 66  .  /* INSTEAD of
26304 20 74 72 69 67 67 65 72 73 20 61 72 65 20 6f 6e   triggers are on
26305 6c 79 20 66 6f 72 20 76 69 65 77 73 20 61 6e 64  ly for views and
26306 20 76 69 65 77 73 20 6f 6e 6c 79 20 73 75 70 70   views only supp
26307 6f 72 74 20 49 4e 53 54 45 41 44 0a 20 20 2a 2a  ort INSTEAD.  **
26308 20 6f 66 20 74 72 69 67 67 65 72 73 2e 0a 20 20   of triggers..  
26309 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70  */.  if( pTab->p
2630a 53 65 6c 65 63 74 20 26 26 20 74 72 5f 74 6d 21  Select && tr_tm!
2630b 3d 54 4b 5f 49 4e 53 54 45 41 44 20 29 7b 0a 20  =TK_INSTEAD ){. 
2630c 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2630d 73 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e  sg(pParse, "cann
2630e 6f 74 20 63 72 65 61 74 65 20 25 73 20 74 72 69  ot create %s tri
2630f 67 67 65 72 20 6f 6e 20 76 69 65 77 3a 20 25 53  gger on view: %S
26310 22 2c 20 0a 20 20 20 20 20 20 20 20 28 74 72 5f  ", .        (tr_
26311 74 6d 20 3d 3d 20 54 4b 5f 42 45 46 4f 52 45 29  tm == TK_BEFORE)
26312 3f 22 42 45 46 4f 52 45 22 3a 22 41 46 54 45 52  ?"BEFORE":"AFTER
26313 22 2c 20 70 54 61 62 6c 65 4e 61 6d 65 2c 20 30  ", pTableName, 0
26314 29 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67  );.    goto trig
26315 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ger_cleanup;.  }
26316 0a 20 20 69 66 28 20 21 70 54 61 62 2d 3e 70 53  .  if( !pTab->pS
26317 65 6c 65 63 74 20 26 26 20 74 72 5f 74 6d 3d 3d  elect && tr_tm==
26318 54 4b 5f 49 4e 53 54 45 41 44 20 29 7b 0a 20 20  TK_INSTEAD ){.  
26319 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2631a 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f  g(pParse, "canno
2631b 74 20 63 72 65 61 74 65 20 49 4e 53 54 45 41 44  t create INSTEAD
2631c 20 4f 46 22 0a 20 20 20 20 20 20 20 20 22 20 74   OF".        " t
2631d 72 69 67 67 65 72 20 6f 6e 20 74 61 62 6c 65 3a  rigger on table:
2631e 20 25 53 22 2c 20 70 54 61 62 6c 65 4e 61 6d 65   %S", pTableName
2631f 2c 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20 74  , 0);.    goto t
26320 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a  rigger_cleanup;.
26321 20 20 7d 0a 20 20 69 54 61 62 44 62 20 3d 20 73    }.  iTabDb = s
26322 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
26323 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53  dex(db, pTab->pS
26324 63 68 65 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66  chema);..#ifndef
26325 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
26326 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20  HORIZATION.  {. 
26327 20 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 53 51     int code = SQ
26328 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 52 49 47  LITE_CREATE_TRIG
26329 47 45 52 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  GER;.    const c
2632a 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61  har *zDb = db->a
2632b 44 62 5b 69 54 61 62 44 62 5d 2e 7a 4e 61 6d 65  Db[iTabDb].zName
2632c 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
2632d 20 2a 7a 44 62 54 72 69 67 20 3d 20 69 73 54 65   *zDbTrig = isTe
2632e 6d 70 20 3f 20 64 62 2d 3e 61 44 62 5b 31 5d 2e  mp ? db->aDb[1].
2632f 7a 4e 61 6d 65 20 3a 20 7a 44 62 3b 0a 20 20 20  zName : zDb;.   
26330 20 69 66 28 20 69 54 61 62 44 62 3d 3d 31 20 7c   if( iTabDb==1 |
26331 7c 20 69 73 54 65 6d 70 20 29 20 63 6f 64 65 20  | isTemp ) code 
26332 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f  = SQLITE_CREATE_
26333 54 45 4d 50 5f 54 52 49 47 47 45 52 3b 0a 20 20  TEMP_TRIGGER;.  
26334 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
26335 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 63  hCheck(pParse, c
26336 6f 64 65 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62  ode, zName, pTab
26337 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 54 72 69 67  ->zName, zDbTrig
26338 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ) ){.      goto 
26339 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
2633a 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
2633b 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
2633c 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49  pParse, SQLITE_I
2633d 4e 53 45 52 54 2c 20 53 43 48 45 4d 41 5f 54 41  NSERT, SCHEMA_TA
2633e 42 4c 45 28 69 54 61 62 44 62 29 2c 30 2c 7a 44  BLE(iTabDb),0,zD
2633f 62 29 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  b)){.      goto 
26340 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
26341 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
26342 66 0a 0a 20 20 2f 2a 20 49 4e 53 54 45 41 44 20  f..  /* INSTEAD 
26343 4f 46 20 74 72 69 67 67 65 72 73 20 63 61 6e 20  OF triggers can 
26344 6f 6e 6c 79 20 61 70 70 65 61 72 20 6f 6e 20 76  only appear on v
26345 69 65 77 73 20 61 6e 64 20 42 45 46 4f 52 45 20  iews and BEFORE 
26346 74 72 69 67 67 65 72 73 0a 20 20 2a 2a 20 63 61  triggers.  ** ca
26347 6e 6e 6f 74 20 61 70 70 65 61 72 20 6f 6e 20 76  nnot appear on v
26348 69 65 77 73 2e 20 20 53 6f 20 77 65 20 6d 69 67  iews.  So we mig
26349 68 74 20 61 73 20 77 65 6c 6c 20 74 72 61 6e 73  ht as well trans
2634a 6c 61 74 65 20 65 76 65 72 79 0a 20 20 2a 2a 20  late every.  ** 
2634b 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67  INSTEAD OF trigg
2634c 65 72 20 69 6e 74 6f 20 61 20 42 45 46 4f 52 45  er into a BEFORE
2634d 20 74 72 69 67 67 65 72 2e 20 20 49 74 20 73 69   trigger.  It si
2634e 6d 70 6c 69 66 69 65 73 20 63 6f 64 65 0a 20 20  mplifies code.  
2634f 2a 2a 20 65 6c 73 65 77 68 65 72 65 2e 0a 20 20  ** elsewhere..  
26350 2a 2f 0a 20 20 69 66 20 28 74 72 5f 74 6d 20 3d  */.  if (tr_tm =
26351 3d 20 54 4b 5f 49 4e 53 54 45 41 44 29 7b 0a 20  = TK_INSTEAD){. 
26352 20 20 20 74 72 5f 74 6d 20 3d 20 54 4b 5f 42 45     tr_tm = TK_BE
26353 46 4f 52 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  FORE;.  }..  /* 
26354 42 75 69 6c 64 20 74 68 65 20 54 72 69 67 67 65  Build the Trigge
26355 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 70 54  r object */.  pT
26356 72 69 67 67 65 72 20 3d 20 28 54 72 69 67 67 65  rigger = (Trigge
26357 72 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c  r*)sqlite3DbMall
26358 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
26359 66 28 54 72 69 67 67 65 72 29 29 3b 0a 20 20 69  f(Trigger));.  i
2635a 66 28 20 70 54 72 69 67 67 65 72 3d 3d 30 20 29  f( pTrigger==0 )
2635b 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c   goto trigger_cl
2635c 65 61 6e 75 70 3b 0a 20 20 70 54 72 69 67 67 65  eanup;.  pTrigge
2635d 72 2d 3e 6e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b  r->name = zName;
2635e 0a 20 20 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20  .  zName = 0;.  
2635f 70 54 72 69 67 67 65 72 2d 3e 74 61 62 6c 65 20  pTrigger->table 
26360 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
26361 70 28 64 62 2c 20 70 54 61 62 6c 65 4e 61 6d 65  p(db, pTableName
26362 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  ->a[0].zName);. 
26363 20 70 54 72 69 67 67 65 72 2d 3e 70 53 63 68 65   pTrigger->pSche
26364 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62  ma = db->aDb[iDb
26365 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70 54 72  ].pSchema;.  pTr
26366 69 67 67 65 72 2d 3e 70 54 61 62 53 63 68 65 6d  igger->pTabSchem
26367 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  a = pTab->pSchem
26368 61 3b 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 6f  a;.  pTrigger->o
26369 70 20 3d 20 6f 70 3b 0a 20 20 70 54 72 69 67 67  p = op;.  pTrigg
2636a 65 72 2d 3e 74 72 5f 74 6d 20 3d 20 74 72 5f 74  er->tr_tm = tr_t
2636b 6d 3d 3d 54 4b 5f 42 45 46 4f 52 45 20 3f 20 54  m==TK_BEFORE ? T
2636c 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 3a 20  RIGGER_BEFORE : 
2636d 54 52 49 47 47 45 52 5f 41 46 54 45 52 3b 0a 20  TRIGGER_AFTER;. 
2636e 20 70 54 72 69 67 67 65 72 2d 3e 70 57 68 65 6e   pTrigger->pWhen
2636f 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
26370 70 28 64 62 2c 20 70 57 68 65 6e 29 3b 0a 20 20  p(db, pWhen);.  
26371 70 54 72 69 67 67 65 72 2d 3e 70 43 6f 6c 75 6d  pTrigger->pColum
26372 6e 73 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69  ns = sqlite3IdLi
26373 73 74 44 75 70 28 64 62 2c 20 70 43 6f 6c 75 6d  stDup(db, pColum
26374 6e 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 54 6f  ns);.  sqlite3To
26375 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26 70 54 72  kenCopy(db, &pTr
26376 69 67 67 65 72 2d 3e 6e 61 6d 65 54 6f 6b 65 6e  igger->nameToken
26377 2c 70 4e 61 6d 65 29 3b 0a 20 20 61 73 73 65 72  ,pName);.  asser
26378 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  t( pParse->pNewT
26379 72 69 67 67 65 72 3d 3d 30 20 29 3b 0a 20 20 70  rigger==0 );.  p
2637a 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67  Parse->pNewTrigg
2637b 65 72 20 3d 20 70 54 72 69 67 67 65 72 3b 0a 0a  er = pTrigger;..
2637c 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3a  trigger_cleanup:
2637d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
2637e 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 73  (db, zName);.  s
2637f 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c  qlite3SrcListDel
26380 65 74 65 28 64 62 2c 20 70 54 61 62 6c 65 4e 61  ete(db, pTableNa
26381 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 49 64  me);.  sqlite3Id
26382 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
26383 43 6f 6c 75 6d 6e 73 29 3b 0a 20 20 73 71 6c 69  Columns);.  sqli
26384 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
26385 2c 20 70 57 68 65 6e 29 3b 0a 20 20 69 66 28 20  , pWhen);.  if( 
26386 21 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69  !pParse->pNewTri
26387 67 67 65 72 20 29 7b 0a 20 20 20 20 73 71 6c 69  gger ){.    sqli
26388 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72  te3DeleteTrigger
26389 28 64 62 2c 20 70 54 72 69 67 67 65 72 29 3b 0a  (db, pTrigger);.
2638a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
2638b 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65  ert( pParse->pNe
2638c 77 54 72 69 67 67 65 72 3d 3d 70 54 72 69 67 67  wTrigger==pTrigg
2638d 65 72 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  er );.  }.}../*.
2638e 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2638f 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20  is called after 
26390 61 6c 6c 20 6f 66 20 74 68 65 20 74 72 69 67 67  all of the trigg
26391 65 72 20 61 63 74 69 6f 6e 73 20 68 61 76 65 20  er actions have 
26392 62 65 65 6e 20 70 61 72 73 65 64 0a 2a 2a 20 69  been parsed.** i
26393 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 6d 70 6c  n order to compl
26394 65 74 65 20 74 68 65 20 70 72 6f 63 65 73 73 20  ete the process 
26395 6f 66 20 62 75 69 6c 64 69 6e 67 20 74 68 65 20  of building the 
26396 74 72 69 67 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49  trigger..*/.SQLI
26397 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
26398 73 71 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69  sqlite3FinishTri
26399 67 67 65 72 28 0a 20 20 50 61 72 73 65 20 2a 70  gger(.  Parse *p
2639a 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
2639b 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78  /* Parser contex
2639c 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74  t */.  TriggerSt
2639d 65 70 20 2a 70 53 74 65 70 4c 69 73 74 2c 20 2f  ep *pStepList, /
2639e 2a 20 54 68 65 20 74 72 69 67 67 65 72 65 64 20  * The triggered 
2639f 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 54 6f 6b  program */.  Tok
263a0 65 6e 20 2a 70 41 6c 6c 20 20 20 20 20 20 20 20  en *pAll        
263a1 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 74 68       /* Token th
263a2 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65  at describes the
263a3 20 63 6f 6d 70 6c 65 74 65 20 43 52 45 41 54 45   complete CREATE
263a4 20 54 52 49 47 47 45 52 20 2a 2f 0a 29 7b 0a 20   TRIGGER */.){. 
263a5 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 20   Trigger *pTrig 
263a6 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 68 65 20  = 0;     /* The 
263a7 74 72 69 67 67 65 72 20 77 68 6f 73 65 20 63 6f  trigger whose co
263a8 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 66 69  nstruction is fi
263a9 6e 69 73 68 69 6e 67 20 75 70 20 2a 2f 0a 20 20  nishing up */.  
263aa 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
263ab 61 72 73 65 2d 3e 64 62 3b 20 20 2f 2a 20 54 68  arse->db;  /* Th
263ac 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  e database */.  
263ad 44 62 46 69 78 65 72 20 73 46 69 78 3b 0a 20 20  DbFixer sFix;.  
263ae 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
263af 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
263b0 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
263b1 67 20 74 68 65 20 74 72 69 67 67 65 72 20 2a 2f  g the trigger */
263b2 0a 0a 20 20 70 54 72 69 67 20 3d 20 70 50 61 72  ..  pTrig = pPar
263b3 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 3b  se->pNewTrigger;
263b4 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  .  pParse->pNewT
263b5 72 69 67 67 65 72 20 3d 20 30 3b 0a 20 20 69 66  rigger = 0;.  if
263b6 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c  ( pParse->nErr |
263b7 7c 20 21 70 54 72 69 67 20 29 20 67 6f 74 6f 20  | !pTrig ) goto 
263b8 74 72 69 67 67 65 72 66 69 6e 69 73 68 5f 63 6c  triggerfinish_cl
263b9 65 61 6e 75 70 3b 0a 20 20 69 44 62 20 3d 20 73  eanup;.  iDb = s
263ba 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
263bb 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  dex(pParse->db, 
263bc 70 54 72 69 67 2d 3e 70 53 63 68 65 6d 61 29 3b  pTrig->pSchema);
263bd 0a 20 20 70 54 72 69 67 2d 3e 73 74 65 70 5f 6c  .  pTrig->step_l
263be 69 73 74 20 3d 20 70 53 74 65 70 4c 69 73 74 3b  ist = pStepList;
263bf 0a 20 20 77 68 69 6c 65 28 20 70 53 74 65 70 4c  .  while( pStepL
263c0 69 73 74 20 29 7b 0a 20 20 20 20 70 53 74 65 70  ist ){.    pStep
263c1 4c 69 73 74 2d 3e 70 54 72 69 67 20 3d 20 70 54  List->pTrig = pT
263c2 72 69 67 3b 0a 20 20 20 20 70 53 74 65 70 4c 69  rig;.    pStepLi
263c3 73 74 20 3d 20 70 53 74 65 70 4c 69 73 74 2d 3e  st = pStepList->
263c4 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28  pNext;.  }.  if(
263c5 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28   sqlite3FixInit(
263c6 26 73 46 69 78 2c 20 70 50 61 72 73 65 2c 20 69  &sFix, pParse, i
263c7 44 62 2c 20 22 74 72 69 67 67 65 72 22 2c 20 26  Db, "trigger", &
263c8 70 54 72 69 67 2d 3e 6e 61 6d 65 54 6f 6b 65 6e  pTrig->nameToken
263c9 29 20 0a 20 20 20 20 20 20 20 20 20 20 26 26 20  ) .          && 
263ca 73 71 6c 69 74 65 33 46 69 78 54 72 69 67 67 65  sqlite3FixTrigge
263cb 72 53 74 65 70 28 26 73 46 69 78 2c 20 70 54 72  rStep(&sFix, pTr
263cc 69 67 2d 3e 73 74 65 70 5f 6c 69 73 74 29 20 29  ig->step_list) )
263cd 7b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67  {.    goto trigg
263ce 65 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70  erfinish_cleanup
263cf 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69 66 20 77  ;.  }..  /* if w
263d0 65 20 61 72 65 20 6e 6f 74 20 69 6e 69 74 69 61  e are not initia
263d1 6c 69 7a 69 6e 67 2c 20 61 6e 64 20 74 68 69 73  lizing, and this
263d2 20 74 72 69 67 67 65 72 20 69 73 20 6e 6f 74 20   trigger is not 
263d3 6f 6e 20 61 20 54 45 4d 50 20 74 61 62 6c 65 2c  on a TEMP table,
263d4 20 0a 20 20 2a 2a 20 62 75 69 6c 64 20 74 68 65   .  ** build the
263d5 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 65   sqlite_master e
263d6 6e 74 72 79 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ntry.  */.  if( 
263d7 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29  !db->init.busy )
263d8 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 3b 0a 20  {.    Vdbe *v;. 
263d9 20 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 20     char *z;..   
263da 20 2f 2a 20 4d 61 6b 65 20 61 6e 20 65 6e 74 72   /* Make an entr
263db 79 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f  y in the sqlite_
263dc 6d 61 73 74 65 72 20 74 61 62 6c 65 20 2a 2f 0a  master table */.
263dd 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47      v = sqlite3G
263de 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
263df 20 20 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67      if( v==0 ) g
263e0 6f 74 6f 20 74 72 69 67 67 65 72 66 69 6e 69 73  oto triggerfinis
263e1 68 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 73  h_cleanup;.    s
263e2 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
263e3 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
263e4 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 7a  , 0, iDb);.    z
263e5 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e   = sqlite3DbStrN
263e6 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70  Dup(db, (char*)p
263e7 41 6c 6c 2d 3e 7a 2c 20 70 41 6c 6c 2d 3e 6e 29  All->z, pAll->n)
263e8 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73  ;.    sqlite3Nes
263e9 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c  tedParse(pParse,
263ea 0a 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20  .       "INSERT 
263eb 49 4e 54 4f 20 25 51 2e 25 73 20 56 41 4c 55 45  INTO %Q.%s VALUE
263ec 53 28 27 74 72 69 67 67 65 72 27 2c 25 51 2c 25  S('trigger',%Q,%
263ed 51 2c 30 2c 27 43 52 45 41 54 45 20 54 52 49 47  Q,0,'CREATE TRIG
263ee 47 45 52 20 25 71 27 29 22 2c 0a 20 20 20 20 20  GER %q')",.     
263ef 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a    db->aDb[iDb].z
263f0 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41 42  Name, SCHEMA_TAB
263f1 4c 45 28 69 44 62 29 2c 20 70 54 72 69 67 2d 3e  LE(iDb), pTrig->
263f2 6e 61 6d 65 2c 0a 20 20 20 20 20 20 20 70 54 72  name,.       pTr
263f3 69 67 2d 3e 74 61 62 6c 65 2c 20 7a 29 3b 0a 20  ig->table, z);. 
263f4 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
263f5 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 73 71 6c  (db, z);.    sql
263f6 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65  ite3ChangeCookie
263f7 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20  (pParse, iDb);. 
263f8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
263f9 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 73 65  dOp4(v, OP_Parse
263fa 53 63 68 65 6d 61 2c 20 69 44 62 2c 20 30 2c 20  Schema, iDb, 0, 
263fb 30 2c 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74  0, sqlite3MPrint
263fc 66 28 0a 20 20 20 20 20 20 20 20 64 62 2c 20 22  f(.        db, "
263fd 74 79 70 65 3d 27 74 72 69 67 67 65 72 27 20 41  type='trigger' A
263fe 4e 44 20 6e 61 6d 65 3d 27 25 71 27 22 2c 20 70  ND name='%q'", p
263ff 54 72 69 67 2d 3e 6e 61 6d 65 29 2c 20 50 34 5f  Trig->name), P4_
26400 44 59 4e 41 4d 49 43 0a 20 20 20 20 29 3b 0a 20  DYNAMIC.    );. 
26401 20 7d 0a 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e   }..  if( db->in
26402 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 69  it.busy ){.    i
26403 6e 74 20 6e 3b 0a 20 20 20 20 54 61 62 6c 65 20  nt n;.    Table 
26404 2a 70 54 61 62 3b 0a 20 20 20 20 54 72 69 67 67  *pTab;.    Trigg
26405 65 72 20 2a 70 44 65 6c 3b 0a 20 20 20 20 70 44  er *pDel;.    pD
26406 65 6c 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68  el = sqlite3Hash
26407 49 6e 73 65 72 74 28 26 64 62 2d 3e 61 44 62 5b  Insert(&db->aDb[
26408 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72  iDb].pSchema->tr
26409 69 67 48 61 73 68 2c 20 0a 20 20 20 20 20 20 20  igHash, .       
2640a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54                pT
2640b 72 69 67 2d 3e 6e 61 6d 65 2c 20 73 74 72 6c 65  rig->name, strle
2640c 6e 28 70 54 72 69 67 2d 3e 6e 61 6d 65 29 2c 20  n(pTrig->name), 
2640d 70 54 72 69 67 29 3b 0a 20 20 20 20 69 66 28 20  pTrig);.    if( 
2640e 70 44 65 6c 20 29 7b 0a 20 20 20 20 20 20 61 73  pDel ){.      as
2640f 73 65 72 74 28 20 70 44 65 6c 3d 3d 70 54 72 69  sert( pDel==pTri
26410 67 20 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6d  g );.      db->m
26411 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
26412 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 72 69 67  .      goto trig
26413 67 65 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e 75  gerfinish_cleanu
26414 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 20 3d  p;.    }.    n =
26415 20 73 74 72 6c 65 6e 28 70 54 72 69 67 2d 3e 74   strlen(pTrig->t
26416 61 62 6c 65 29 20 2b 20 31 3b 0a 20 20 20 20 70  able) + 1;.    p
26417 54 61 62 20 3d 20 73 71 6c 69 74 65 33 48 61 73  Tab = sqlite3Has
26418 68 46 69 6e 64 28 26 70 54 72 69 67 2d 3e 70 54  hFind(&pTrig->pT
26419 61 62 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73  abSchema->tblHas
2641a 68 2c 20 70 54 72 69 67 2d 3e 74 61 62 6c 65 2c  h, pTrig->table,
2641b 20 6e 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28   n);.    assert(
2641c 20 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 20 20   pTab!=0 );.    
2641d 70 54 72 69 67 2d 3e 70 4e 65 78 74 20 3d 20 70  pTrig->pNext = p
2641e 54 61 62 2d 3e 70 54 72 69 67 67 65 72 3b 0a 20  Tab->pTrigger;. 
2641f 20 20 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65     pTab->pTrigge
26420 72 20 3d 20 70 54 72 69 67 3b 0a 20 20 20 20 70  r = pTrig;.    p
26421 54 72 69 67 20 3d 20 30 3b 0a 20 20 7d 0a 0a 74  Trig = 0;.  }..t
26422 72 69 67 67 65 72 66 69 6e 69 73 68 5f 63 6c 65  riggerfinish_cle
26423 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 44  anup:.  sqlite3D
26424 65 6c 65 74 65 54 72 69 67 67 65 72 28 64 62 2c  eleteTrigger(db,
26425 20 70 54 72 69 67 29 3b 0a 20 20 61 73 73 65 72   pTrig);.  asser
26426 74 28 20 21 70 50 61 72 73 65 2d 3e 70 4e 65 77  t( !pParse->pNew
26427 54 72 69 67 67 65 72 20 29 3b 0a 20 20 73 71 6c  Trigger );.  sql
26428 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65  ite3DeleteTrigge
26429 72 53 74 65 70 28 64 62 2c 20 70 53 74 65 70 4c  rStep(db, pStepL
2642a 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  ist);.}../*.** M
2642b 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 61 6c  ake a copy of al
2642c 6c 20 63 6f 6d 70 6f 6e 65 6e 74 73 20 6f 66 20  l components of 
2642d 74 68 65 20 67 69 76 65 6e 20 74 72 69 67 67 65  the given trigge
2642e 72 20 73 74 65 70 2e 20 20 54 68 69 73 20 68 61  r step.  This ha
2642f 73 0a 2a 2a 20 74 68 65 20 65 66 66 65 63 74 20  s.** the effect 
26430 6f 66 20 63 6f 70 79 69 6e 67 20 61 6c 6c 20 45  of copying all E
26431 78 70 72 2e 74 6f 6b 65 6e 2e 7a 20 76 61 6c 75  xpr.token.z valu
26432 65 73 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f  es into memory o
26433 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20  btained.** from 
26434 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
26435 2e 20 20 41 73 20 69 6e 69 74 69 61 6c 6c 79 20  .  As initially 
26436 63 72 65 61 74 65 64 2c 20 74 68 65 20 45 78 70  created, the Exp
26437 72 2e 74 6f 6b 65 6e 2e 7a 20 76 61 6c 75 65 73  r.token.z values
26438 0a 2a 2a 20 61 6c 6c 20 70 6f 69 6e 74 20 74 6f  .** all point to
26439 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e   the input strin
2643a 67 20 74 68 61 74 20 77 61 73 20 66 65 64 20 74  g that was fed t
2643b 6f 20 74 68 65 20 70 61 72 73 65 72 2e 20 20 42  o the parser.  B
2643c 75 74 20 74 68 61 74 0a 2a 2a 20 73 74 72 69 6e  ut that.** strin
2643d 67 20 69 73 20 65 70 68 65 6d 65 72 61 6c 20 2d  g is ephemeral -
2643e 20 69 74 20 77 69 6c 6c 20 67 6f 20 61 77 61 79   it will go away
2643f 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20   as soon as the 
26440 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 0a 2a  sqlite3_exec().*
26441 2a 20 63 61 6c 6c 20 74 68 61 74 20 73 74 61 72  * call that star
26442 74 65 64 20 74 68 65 20 70 61 72 73 65 72 20 65  ted the parser e
26443 78 69 74 73 2e 20 20 54 68 69 73 20 72 6f 75 74  xits.  This rout
26444 69 6e 65 20 6d 61 6b 65 73 20 61 20 70 65 72 73  ine makes a pers
26445 69 73 74 65 6e 74 0a 2a 2a 20 63 6f 70 79 20 6f  istent.** copy o
26446 66 20 61 6c 6c 20 74 68 65 20 45 78 70 72 2e 74  f all the Expr.t
26447 6f 6b 65 6e 2e 7a 20 73 74 72 69 6e 67 73 20 73  oken.z strings s
26448 6f 20 74 68 61 74 20 74 68 65 20 54 72 69 67 67  o that the Trigg
26449 65 72 53 74 65 70 20 73 74 72 75 63 74 75 72 65  erStep structure
2644a 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 76 61 6c 69  .** will be vali
2644b 64 20 65 76 65 6e 20 61 66 74 65 72 20 74 68 65  d even after the
2644c 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20   sqlite3_exec() 
2644d 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e 0a 2a 2f  call returns..*/
2644e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c  .static void sql
2644f 69 74 65 50 65 72 73 69 73 74 54 72 69 67 67 65  itePersistTrigge
26450 72 53 74 65 70 28 73 71 6c 69 74 65 33 20 2a 64  rStep(sqlite3 *d
26451 62 2c 20 54 72 69 67 67 65 72 53 74 65 70 20 2a  b, TriggerStep *
26452 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 74 61 72  p){.  if( p->tar
26453 67 65 74 2e 7a 20 29 7b 0a 20 20 20 20 70 2d 3e  get.z ){.    p->
26454 74 61 72 67 65 74 2e 7a 20 3d 20 28 75 38 2a 29  target.z = (u8*)
26455 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70  sqlite3DbStrNDup
26456 28 64 62 2c 20 28 63 68 61 72 2a 29 70 2d 3e 74  (db, (char*)p->t
26457 61 72 67 65 74 2e 7a 2c 20 70 2d 3e 74 61 72 67  arget.z, p->targ
26458 65 74 2e 6e 29 3b 0a 20 20 20 20 70 2d 3e 74 61  et.n);.    p->ta
26459 72 67 65 74 2e 64 79 6e 20 3d 20 31 3b 0a 20 20  rget.dyn = 1;.  
2645a 7d 0a 20 20 69 66 28 20 70 2d 3e 70 53 65 6c 65  }.  if( p->pSele
2645b 63 74 20 29 7b 0a 20 20 20 20 53 65 6c 65 63 74  ct ){.    Select
2645c 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33   *pNew = sqlite3
2645d 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 2d  SelectDup(db, p-
2645e 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 73  >pSelect);.    s
2645f 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
26460 74 65 28 64 62 2c 20 70 2d 3e 70 53 65 6c 65 63  te(db, p->pSelec
26461 74 29 3b 0a 20 20 20 20 70 2d 3e 70 53 65 6c 65  t);.    p->pSele
26462 63 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20  ct = pNew;.  }. 
26463 20 69 66 28 20 70 2d 3e 70 57 68 65 72 65 20 29   if( p->pWhere )
26464 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77  {.    Expr *pNew
26465 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
26466 70 28 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 29  p(db, p->pWhere)
26467 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
26468 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70  rDelete(db, p->p
26469 57 68 65 72 65 29 3b 0a 20 20 20 20 70 2d 3e 70  Where);.    p->p
2646a 57 68 65 72 65 20 3d 20 70 4e 65 77 3b 0a 20 20  Where = pNew;.  
2646b 7d 0a 20 20 69 66 28 20 70 2d 3e 70 45 78 70 72  }.  if( p->pExpr
2646c 4c 69 73 74 20 29 7b 0a 20 20 20 20 45 78 70 72  List ){.    Expr
2646d 4c 69 73 74 20 2a 70 4e 65 77 20 3d 20 73 71 6c  List *pNew = sql
2646e 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
2646f 64 62 2c 20 70 2d 3e 70 45 78 70 72 4c 69 73 74  db, p->pExprList
26470 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
26471 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
26472 20 70 2d 3e 70 45 78 70 72 4c 69 73 74 29 3b 0a   p->pExprList);.
26473 20 20 20 20 70 2d 3e 70 45 78 70 72 4c 69 73 74      p->pExprList
26474 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 69   = pNew;.  }.  i
26475 66 28 20 70 2d 3e 70 49 64 4c 69 73 74 20 29 7b  f( p->pIdList ){
26476 0a 20 20 20 20 49 64 4c 69 73 74 20 2a 70 4e 65  .    IdList *pNe
26477 77 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73  w = sqlite3IdLis
26478 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 49 64 4c  tDup(db, p->pIdL
26479 69 73 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ist);.    sqlite
2647a 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62  3IdListDelete(db
2647b 2c 20 70 2d 3e 70 49 64 4c 69 73 74 29 3b 0a 20  , p->pIdList);. 
2647c 20 20 20 70 2d 3e 70 49 64 4c 69 73 74 20 3d 20     p->pIdList = 
2647d 70 4e 65 77 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  pNew;.  }.}../*.
2647e 2a 2a 20 54 75 72 6e 20 61 20 53 45 4c 45 43 54  ** Turn a SELECT
2647f 20 73 74 61 74 65 6d 65 6e 74 20 28 74 68 61 74   statement (that
26480 20 74 68 65 20 70 53 65 6c 65 63 74 20 70 61 72   the pSelect par
26481 61 6d 65 74 65 72 20 70 6f 69 6e 74 73 20 74 6f  ameter points to
26482 29 20 69 6e 74 6f 0a 2a 2a 20 61 20 74 72 69 67  ) into.** a trig
26483 67 65 72 20 73 74 65 70 2e 20 20 52 65 74 75 72  ger step.  Retur
26484 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  n a pointer to a
26485 20 54 72 69 67 67 65 72 53 74 65 70 20 73 74 72   TriggerStep str
26486 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  ucture..**.** Th
26487 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74  e parser calls t
26488 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e  his routine when
26489 20 69 74 20 66 69 6e 64 73 20 61 20 53 45 4c 45   it finds a SELE
2648a 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 0a  CT statement in.
2648b 2a 2a 20 62 6f 64 79 20 6f 66 20 61 20 54 52 49  ** body of a TRI
2648c 47 47 45 52 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54  GGER.  .*/.SQLIT
2648d 45 5f 50 52 49 56 41 54 45 20 54 72 69 67 67 65  E_PRIVATE Trigge
2648e 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72  rStep *sqlite3Tr
2648f 69 67 67 65 72 53 65 6c 65 63 74 53 74 65 70 28  iggerSelectStep(
26490 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c  sqlite3 *db, Sel
26491 65 63 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a 20  ect *pSelect){. 
26492 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 54   TriggerStep *pT
26493 72 69 67 67 65 72 53 74 65 70 20 3d 20 73 71 6c  riggerStep = sql
26494 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
26495 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 72 69 67  (db, sizeof(Trig
26496 67 65 72 53 74 65 70 29 29 3b 0a 20 20 69 66 28  gerStep));.  if(
26497 20 70 54 72 69 67 67 65 72 53 74 65 70 3d 3d 30   pTriggerStep==0
26498 20 29 20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33   ) {.    sqlite3
26499 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
2649a 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 72   pSelect);.    r
2649b 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20  eturn 0;.  }..  
2649c 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 70  pTriggerStep->op
2649d 20 3d 20 54 4b 5f 53 45 4c 45 43 54 3b 0a 20 20   = TK_SELECT;.  
2649e 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 53  pTriggerStep->pS
2649f 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74 3b  elect = pSelect;
264a0 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d  .  pTriggerStep-
264a1 3e 6f 72 63 6f 6e 66 20 3d 20 4f 45 5f 44 65 66  >orconf = OE_Def
264a2 61 75 6c 74 3b 0a 20 20 73 71 6c 69 74 65 50 65  ault;.  sqlitePe
264a3 72 73 69 73 74 54 72 69 67 67 65 72 53 74 65 70  rsistTriggerStep
264a4 28 64 62 2c 20 70 54 72 69 67 67 65 72 53 74 65  (db, pTriggerSte
264a5 70 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 70 54  p);..  return pT
264a6 72 69 67 67 65 72 53 74 65 70 3b 0a 7d 0a 0a 2f  riggerStep;.}../
264a7 2a 0a 2a 2a 20 42 75 69 6c 64 20 61 20 74 72 69  *.** Build a tri
264a8 67 67 65 72 20 73 74 65 70 20 6f 75 74 20 6f 66  gger step out of
264a9 20 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74 65   an INSERT state
264aa 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e 20 61 20  ment.  Return a 
264ab 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68  pointer.** to th
264ac 65 20 6e 65 77 20 74 72 69 67 67 65 72 20 73 74  e new trigger st
264ad 65 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61  ep..**.** The pa
264ae 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20  rser calls this 
264af 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 69 74 20  routine when it 
264b0 73 65 65 73 20 61 6e 20 49 4e 53 45 52 54 20 69  sees an INSERT i
264b1 6e 73 69 64 65 20 74 68 65 0a 2a 2a 20 62 6f 64  nside the.** bod
264b2 79 20 6f 66 20 61 20 74 72 69 67 67 65 72 2e 0a  y of a trigger..
264b3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
264b4 45 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73  E TriggerStep *s
264b5 71 6c 69 74 65 33 54 72 69 67 67 65 72 49 6e 73  qlite3TriggerIns
264b6 65 72 74 53 74 65 70 28 0a 20 20 73 71 6c 69 74  ertStep(.  sqlit
264b7 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 2f  e3 *db,        /
264b8 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
264b9 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54  onnection */.  T
264ba 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65  oken *pTableName
264bb 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68  ,  /* Name of th
264bc 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69  e table into whi
264bd 63 68 20 77 65 20 69 6e 73 65 72 74 20 2a 2f 0a  ch we insert */.
264be 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c 75 6d    IdList *pColum
264bf 6e 2c 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66  n,    /* List of
264c0 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 54 61 62   columns in pTab
264c1 6c 65 4e 61 6d 65 20 74 6f 20 69 6e 73 65 72 74  leName to insert
264c2 20 69 6e 74 6f 20 2a 2f 0a 20 20 45 78 70 72 4c   into */.  ExprL
264c3 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 20 2f  ist *pEList,   /
264c4 2a 20 54 68 65 20 56 41 4c 55 45 20 63 6c 61 75  * The VALUE clau
264c5 73 65 3a 20 61 20 6c 69 73 74 20 6f 66 20 76 61  se: a list of va
264c6 6c 75 65 73 20 74 6f 20 62 65 20 69 6e 73 65 72  lues to be inser
264c7 74 65 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ted */.  Select 
264c8 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 2f 2a 20  *pSelect,    /* 
264c9 41 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  A SELECT stateme
264ca 6e 74 20 74 68 61 74 20 73 75 70 70 6c 69 65 73  nt that supplies
264cb 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 69 6e 74   values */.  int
264cc 20 6f 72 63 6f 6e 66 20 20 20 20 20 20 20 20 20   orconf         
264cd 20 2f 2a 20 54 68 65 20 63 6f 6e 66 6c 69 63 74   /* The conflict
264ce 20 61 6c 67 6f 72 69 74 68 6d 20 28 4f 45 5f 41   algorithm (OE_A
264cf 62 6f 72 74 2c 20 4f 45 5f 52 65 70 6c 61 63 65  bort, OE_Replace
264d0 2c 20 65 74 63 2e 29 20 2a 2f 0a 29 7b 0a 20 20  , etc.) */.){.  
264d1 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 54 72  TriggerStep *pTr
264d2 69 67 67 65 72 53 74 65 70 3b 0a 0a 20 20 61 73  iggerStep;..  as
264d3 73 65 72 74 28 70 45 4c 69 73 74 20 3d 3d 20 30  sert(pEList == 0
264d4 20 7c 7c 20 70 53 65 6c 65 63 74 20 3d 3d 20 30   || pSelect == 0
264d5 29 3b 0a 20 20 61 73 73 65 72 74 28 70 45 4c 69  );.  assert(pELi
264d6 73 74 20 21 3d 20 30 20 7c 7c 20 70 53 65 6c 65  st != 0 || pSele
264d7 63 74 20 21 3d 20 30 20 7c 7c 20 64 62 2d 3e 6d  ct != 0 || db->m
264d8 61 6c 6c 6f 63 46 61 69 6c 65 64 29 3b 0a 0a 20  allocFailed);.. 
264d9 20 70 54 72 69 67 67 65 72 53 74 65 70 20 3d 20   pTriggerStep = 
264da 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
264db 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54  ero(db, sizeof(T
264dc 72 69 67 67 65 72 53 74 65 70 29 29 3b 0a 20 20  riggerStep));.  
264dd 69 66 28 20 70 54 72 69 67 67 65 72 53 74 65 70  if( pTriggerStep
264de 20 29 7b 0a 20 20 20 20 70 54 72 69 67 67 65 72   ){.    pTrigger
264df 53 74 65 70 2d 3e 6f 70 20 3d 20 54 4b 5f 49 4e  Step->op = TK_IN
264e0 53 45 52 54 3b 0a 20 20 20 20 70 54 72 69 67 67  SERT;.    pTrigg
264e1 65 72 53 74 65 70 2d 3e 70 53 65 6c 65 63 74 20  erStep->pSelect 
264e2 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 70  = pSelect;.    p
264e3 54 72 69 67 67 65 72 53 74 65 70 2d 3e 74 61 72  TriggerStep->tar
264e4 67 65 74 20 20 3d 20 2a 70 54 61 62 6c 65 4e 61  get  = *pTableNa
264e5 6d 65 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72  me;.    pTrigger
264e6 53 74 65 70 2d 3e 70 49 64 4c 69 73 74 20 3d 20  Step->pIdList = 
264e7 70 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 70 54 72  pColumn;.    pTr
264e8 69 67 67 65 72 53 74 65 70 2d 3e 70 45 78 70 72  iggerStep->pExpr
264e9 4c 69 73 74 20 3d 20 70 45 4c 69 73 74 3b 0a 20  List = pEList;. 
264ea 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d     pTriggerStep-
264eb 3e 6f 72 63 6f 6e 66 20 3d 20 6f 72 63 6f 6e 66  >orconf = orconf
264ec 3b 0a 20 20 20 20 73 71 6c 69 74 65 50 65 72 73  ;.    sqlitePers
264ed 69 73 74 54 72 69 67 67 65 72 53 74 65 70 28 64  istTriggerStep(d
264ee 62 2c 20 70 54 72 69 67 67 65 72 53 74 65 70 29  b, pTriggerStep)
264ef 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
264f0 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65  qlite3IdListDele
264f1 74 65 28 64 62 2c 20 70 43 6f 6c 75 6d 6e 29 3b  te(db, pColumn);
264f2 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
264f3 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
264f4 45 4c 69 73 74 29 3b 0a 20 20 20 20 73 71 6c 69  EList);.    sqli
264f5 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
264f6 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20  db, pSelect);.  
264f7 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 54 72 69  }..  return pTri
264f8 67 67 65 72 53 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a  ggerStep;.}../*.
264f9 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 20 74  ** Construct a t
264fa 72 69 67 67 65 72 20 73 74 65 70 20 74 68 61 74  rigger step that
264fb 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 55   implements an U
264fc 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20  PDATE statement 
264fd 61 6e 64 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20  and return.** a 
264fe 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74 20  pointer to that 
264ff 74 72 69 67 67 65 72 20 73 74 65 70 2e 20 20 54  trigger step.  T
26500 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73 20  he parser calls 
26501 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65  this routine whe
26502 6e 20 69 74 0a 2a 2a 20 73 65 65 73 20 61 6e 20  n it.** sees an 
26503 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74  UPDATE statement
26504 20 69 6e 73 69 64 65 20 74 68 65 20 62 6f 64 79   inside the body
26505 20 6f 66 20 61 20 43 52 45 41 54 45 20 54 52 49   of a CREATE TRI
26506 47 47 45 52 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  GGER..*/.SQLITE_
26507 50 52 49 56 41 54 45 20 54 72 69 67 67 65 72 53  PRIVATE TriggerS
26508 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67  tep *sqlite3Trig
26509 67 65 72 55 70 64 61 74 65 53 74 65 70 28 0a 20  gerUpdateStep(. 
2650a 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
2650b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
2650c 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2650d 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61   */.  Token *pTa
2650e 62 6c 65 4e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61  bleName,   /* Na
2650f 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
26510 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 2a 2f  to be updated */
26511 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
26512 69 73 74 2c 20 20 20 20 2f 2a 20 54 68 65 20 53  ist,    /* The S
26513 45 54 20 63 6c 61 75 73 65 3a 20 6c 69 73 74 20  ET clause: list 
26514 6f 66 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 6e 65  of column and ne
26515 77 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 45 78  w values */.  Ex
26516 70 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20 20  pr *pWhere,     
26517 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20     /* The WHERE 
26518 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20  clause */.  int 
26519 6f 72 63 6f 6e 66 20 20 20 20 20 20 20 20 20 20  orconf          
2651a 20 2f 2a 20 54 68 65 20 63 6f 6e 66 6c 69 63 74   /* The conflict
2651b 20 61 6c 67 6f 72 69 74 68 6d 2e 20 28 4f 45 5f   algorithm. (OE_
2651c 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65  Abort, OE_Ignore
2651d 2c 20 65 74 63 29 20 2a 2f 0a 29 7b 0a 20 20 54  , etc) */.){.  T
2651e 72 69 67 67 65 72 53 74 65 70 20 2a 70 54 72 69  riggerStep *pTri
2651f 67 67 65 72 53 74 65 70 20 3d 20 73 71 6c 69 74  ggerStep = sqlit
26520 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
26521 62 2c 20 73 69 7a 65 6f 66 28 54 72 69 67 67 65  b, sizeof(Trigge
26522 72 53 74 65 70 29 29 3b 0a 20 20 69 66 28 20 70  rStep));.  if( p
26523 54 72 69 67 67 65 72 53 74 65 70 3d 3d 30 20 29  TriggerStep==0 )
26524 7b 0a 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  {.     sqlite3Ex
26525 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
26526 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 20 73   pEList);.     s
26527 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
26528 28 64 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20  (db, pWhere);.  
26529 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
2652a 0a 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70  ..  pTriggerStep
2652b 2d 3e 6f 70 20 3d 20 54 4b 5f 55 50 44 41 54 45  ->op = TK_UPDATE
2652c 3b 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70  ;.  pTriggerStep
2652d 2d 3e 74 61 72 67 65 74 20 20 3d 20 2a 70 54 61  ->target  = *pTa
2652e 62 6c 65 4e 61 6d 65 3b 0a 20 20 70 54 72 69 67  bleName;.  pTrig
2652f 67 65 72 53 74 65 70 2d 3e 70 45 78 70 72 4c 69  gerStep->pExprLi
26530 73 74 20 3d 20 70 45 4c 69 73 74 3b 0a 20 20 70  st = pEList;.  p
26531 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 57 68  TriggerStep->pWh
26532 65 72 65 20 3d 20 70 57 68 65 72 65 3b 0a 20 20  ere = pWhere;.  
26533 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 72  pTriggerStep->or
26534 63 6f 6e 66 20 3d 20 6f 72 63 6f 6e 66 3b 0a 20  conf = orconf;. 
26535 20 73 71 6c 69 74 65 50 65 72 73 69 73 74 54 72   sqlitePersistTr
26536 69 67 67 65 72 53 74 65 70 28 64 62 2c 20 70 54  iggerStep(db, pT
26537 72 69 67 67 65 72 53 74 65 70 29 3b 0a 0a 20 20  riggerStep);..  
26538 72 65 74 75 72 6e 20 70 54 72 69 67 67 65 72 53  return pTriggerS
26539 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  tep;.}../*.** Co
2653a 6e 73 74 72 75 63 74 20 61 20 74 72 69 67 67 65  nstruct a trigge
2653b 72 20 73 74 65 70 20 74 68 61 74 20 69 6d 70 6c  r step that impl
2653c 65 6d 65 6e 74 73 20 61 20 44 45 4c 45 54 45 20  ements a DELETE 
2653d 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65  statement and re
2653e 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65  turn.** a pointe
2653f 72 20 74 6f 20 74 68 61 74 20 74 72 69 67 67 65  r to that trigge
26540 72 20 73 74 65 70 2e 20 20 54 68 65 20 70 61 72  r step.  The par
26541 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72  ser calls this r
26542 6f 75 74 69 6e 65 20 77 68 65 6e 20 69 74 0a 2a  outine when it.*
26543 2a 20 73 65 65 73 20 61 20 44 45 4c 45 54 45 20  * sees a DELETE 
26544 73 74 61 74 65 6d 65 6e 74 20 69 6e 73 69 64 65  statement inside
26545 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 43   the body of a C
26546 52 45 41 54 45 20 54 52 49 47 47 45 52 2e 0a 2a  REATE TRIGGER..*
26547 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
26548 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71   TriggerStep *sq
26549 6c 69 74 65 33 54 72 69 67 67 65 72 44 65 6c 65  lite3TriggerDele
2654a 74 65 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65  teStep(.  sqlite
2654b 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
2654c 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
2654d 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f  nnection */.  To
2654e 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c  ken *pTableName,
2654f 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62        /* The tab
26550 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 72 6f  le from which ro
26551 77 73 20 61 72 65 20 64 65 6c 65 74 65 64 20 2a  ws are deleted *
26552 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65  /.  Expr *pWhere
26553 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
26554 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
26555 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 53  */.){.  TriggerS
26556 74 65 70 20 2a 70 54 72 69 67 67 65 72 53 74 65  tep *pTriggerSte
26557 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  p = sqlite3DbMal
26558 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
26559 6f 66 28 54 72 69 67 67 65 72 53 74 65 70 29 29  of(TriggerStep))
2655a 3b 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72  ;.  if( pTrigger
2655b 53 74 65 70 3d 3d 30 20 29 7b 0a 20 20 20 20 73  Step==0 ){.    s
2655c 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
2655d 28 64 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20  (db, pWhere);.  
2655e 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
2655f 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d  .  pTriggerStep-
26560 3e 6f 70 20 3d 20 54 4b 5f 44 45 4c 45 54 45 3b  >op = TK_DELETE;
26561 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d  .  pTriggerStep-
26562 3e 74 61 72 67 65 74 20 20 3d 20 2a 70 54 61 62  >target  = *pTab
26563 6c 65 4e 61 6d 65 3b 0a 20 20 70 54 72 69 67 67  leName;.  pTrigg
26564 65 72 53 74 65 70 2d 3e 70 57 68 65 72 65 20 3d  erStep->pWhere =
26565 20 70 57 68 65 72 65 3b 0a 20 20 70 54 72 69 67   pWhere;.  pTrig
26566 67 65 72 53 74 65 70 2d 3e 6f 72 63 6f 6e 66 20  gerStep->orconf 
26567 3d 20 4f 45 5f 44 65 66 61 75 6c 74 3b 0a 20 20  = OE_Default;.  
26568 73 71 6c 69 74 65 50 65 72 73 69 73 74 54 72 69  sqlitePersistTri
26569 67 67 65 72 53 74 65 70 28 64 62 2c 20 70 54 72  ggerStep(db, pTr
2656a 69 67 67 65 72 53 74 65 70 29 3b 0a 0a 20 20 72  iggerStep);..  r
2656b 65 74 75 72 6e 20 70 54 72 69 67 67 65 72 53 74  eturn pTriggerSt
2656c 65 70 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65  ep;.}../* .** Re
2656d 63 75 72 73 69 76 65 6c 79 20 64 65 6c 65 74 65  cursively delete
2656e 20 61 20 54 72 69 67 67 65 72 20 73 74 72 75 63   a Trigger struc
2656f 74 75 72 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ture.*/.SQLITE_P
26570 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
26571 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72  te3DeleteTrigger
26572 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 72  (sqlite3 *db, Tr
26573 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 29  igger *pTrigger)
26574 7b 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72  {.  if( pTrigger
26575 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
26576 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
26577 67 67 65 72 53 74 65 70 28 64 62 2c 20 70 54 72  ggerStep(db, pTr
26578 69 67 67 65 72 2d 3e 73 74 65 70 5f 6c 69 73 74  igger->step_list
26579 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
2657a 65 65 28 64 62 2c 20 70 54 72 69 67 67 65 72 2d  ee(db, pTrigger-
2657b 3e 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65  >name);.  sqlite
2657c 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 72 69  3DbFree(db, pTri
2657d 67 67 65 72 2d 3e 74 61 62 6c 65 29 3b 0a 20 20  gger->table);.  
2657e 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
2657f 65 28 64 62 2c 20 70 54 72 69 67 67 65 72 2d 3e  e(db, pTrigger->
26580 70 57 68 65 6e 29 3b 0a 20 20 73 71 6c 69 74 65  pWhen);.  sqlite
26581 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62  3IdListDelete(db
26582 2c 20 70 54 72 69 67 67 65 72 2d 3e 70 43 6f 6c  , pTrigger->pCol
26583 75 6d 6e 73 29 3b 0a 20 20 69 66 28 20 70 54 72  umns);.  if( pTr
26584 69 67 67 65 72 2d 3e 6e 61 6d 65 54 6f 6b 65 6e  igger->nameToken
26585 2e 64 79 6e 20 29 20 73 71 6c 69 74 65 33 44 62  .dyn ) sqlite3Db
26586 46 72 65 65 28 64 62 2c 20 28 63 68 61 72 2a 29  Free(db, (char*)
26587 70 54 72 69 67 67 65 72 2d 3e 6e 61 6d 65 54 6f  pTrigger->nameTo
26588 6b 65 6e 2e 7a 29 3b 0a 20 20 73 71 6c 69 74 65  ken.z);.  sqlite
26589 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 72 69  3DbFree(db, pTri
2658a 67 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  gger);.}../*.** 
2658b 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
2658c 20 63 61 6c 6c 65 64 20 74 6f 20 64 72 6f 70 20   called to drop 
2658d 61 20 74 72 69 67 67 65 72 20 66 72 6f 6d 20 74  a trigger from t
2658e 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
2658f 6d 61 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ma. .**.** This 
26590 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 64 69  may be called di
26591 72 65 63 74 6c 79 20 66 72 6f 6d 20 74 68 65 20  rectly from the 
26592 70 61 72 73 65 72 20 61 6e 64 20 74 68 65 72 65  parser and there
26593 66 6f 72 65 20 69 64 65 6e 74 69 66 69 65 73 0a  fore identifies.
26594 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72 20 62  ** the trigger b
26595 79 20 6e 61 6d 65 2e 20 20 54 68 65 20 73 71 6c  y name.  The sql
26596 69 74 65 33 44 72 6f 70 54 72 69 67 67 65 72 50  ite3DropTriggerP
26597 74 72 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65  tr() routine doe
26598 73 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 6a 6f  s the.** same jo
26599 62 20 61 73 20 74 68 69 73 20 72 6f 75 74 69 6e  b as this routin
2659a 65 20 65 78 63 65 70 74 20 69 74 20 74 61 6b 65  e except it take
2659b 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
2659c 68 65 20 74 72 69 67 67 65 72 0a 2a 2a 20 69 6e  he trigger.** in
2659d 73 74 65 61 64 20 6f 66 20 74 68 65 20 74 72 69  stead of the tri
2659e 67 67 65 72 20 6e 61 6d 65 2e 0a 2a 2a 2f 0a 53  gger name..**/.S
2659f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
265a0 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72  id sqlite3DropTr
265a1 69 67 67 65 72 28 50 61 72 73 65 20 2a 70 50 61  igger(Parse *pPa
265a2 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e  rse, SrcList *pN
265a3 61 6d 65 2c 20 69 6e 74 20 6e 6f 45 72 72 29 7b  ame, int noErr){
265a4 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
265a5 67 67 65 72 20 3d 20 30 3b 0a 20 20 69 6e 74 20  gger = 0;.  int 
265a6 69 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  i;.  const char 
265a7 2a 7a 44 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68  *zDb;.  const ch
265a8 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69 6e 74  ar *zName;.  int
265a9 20 6e 4e 61 6d 65 3b 0a 20 20 73 71 6c 69 74 65   nName;.  sqlite
265aa 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
265ab 64 62 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e 6d  db;..  if( db->m
265ac 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f  allocFailed ) go
265ad 74 6f 20 64 72 6f 70 5f 74 72 69 67 67 65 72 5f  to drop_trigger_
265ae 63 6c 65 61 6e 75 70 3b 0a 20 20 69 66 28 20 53  cleanup;.  if( S
265af 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65  QLITE_OK!=sqlite
265b0 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
265b1 73 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  se) ){.    goto 
265b2 64 72 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c 65  drop_trigger_cle
265b3 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  anup;.  }..  ass
265b4 65 72 74 28 20 70 4e 61 6d 65 2d 3e 6e 53 72 63  ert( pName->nSrc
265b5 3d 3d 31 20 29 3b 0a 20 20 7a 44 62 20 3d 20 70  ==1 );.  zDb = p
265b6 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61  Name->a[0].zData
265b7 62 61 73 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20  base;.  zName = 
265b8 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d  pName->a[0].zNam
265b9 65 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 74 72  e;.  nName = str
265ba 6c 65 6e 28 7a 4e 61 6d 65 29 3b 0a 20 20 66 6f  len(zName);.  fo
265bb 72 28 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b  r(i=OMIT_TEMPDB;
265bc 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
265bd 7b 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 28 69  {.    int j = (i
265be 3c 32 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20  <2) ? i^1 : i;  
265bf 2f 2a 20 53 65 61 72 63 68 20 54 45 4d 50 20 62  /* Search TEMP b
265c0 65 66 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 20  efore MAIN */.  
265c1 20 20 69 66 28 20 7a 44 62 20 26 26 20 73 71 6c    if( zDb && sql
265c2 69 74 65 33 53 74 72 49 43 6d 70 28 64 62 2d 3e  ite3StrICmp(db->
265c3 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 7a 44  aDb[j].zName, zD
265c4 62 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  b) ) continue;. 
265c5 20 20 20 70 54 72 69 67 67 65 72 20 3d 20 73 71     pTrigger = sq
265c6 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 28  lite3HashFind(&(
265c7 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53 63 68 65  db->aDb[j].pSche
265c8 6d 61 2d 3e 74 72 69 67 48 61 73 68 29 2c 20 7a  ma->trigHash), z
265c9 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20  Name, nName);.  
265ca 20 20 69 66 28 20 70 54 72 69 67 67 65 72 20 29    if( pTrigger )
265cb 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66   break;.  }.  if
265cc 28 20 21 70 54 72 69 67 67 65 72 20 29 7b 0a 20  ( !pTrigger ){. 
265cd 20 20 20 69 66 28 20 21 6e 6f 45 72 72 20 29 7b     if( !noErr ){
265ce 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
265cf 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
265d0 6e 6f 20 73 75 63 68 20 74 72 69 67 67 65 72 3a  no such trigger:
265d1 20 25 53 22 2c 20 70 4e 61 6d 65 2c 20 30 29 3b   %S", pName, 0);
265d2 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20  .    }.    goto 
265d3 64 72 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c 65  drop_trigger_cle
265d4 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  anup;.  }.  sqli
265d5 74 65 33 44 72 6f 70 54 72 69 67 67 65 72 50 74  te3DropTriggerPt
265d6 72 28 70 50 61 72 73 65 2c 20 70 54 72 69 67 67  r(pParse, pTrigg
265d7 65 72 29 3b 0a 0a 64 72 6f 70 5f 74 72 69 67 67  er);..drop_trigg
265d8 65 72 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71  er_cleanup:.  sq
265d9 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65  lite3SrcListDele
265da 74 65 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 7d  te(db, pName);.}
265db 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
265dc 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
265dd 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20  Table structure 
265de 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 74 68  for the table th
265df 61 74 20 61 20 74 72 69 67 67 65 72 0a 2a 2a 20  at a trigger.** 
265e0 69 73 20 73 65 74 20 6f 6e 2e 0a 2a 2f 0a 73 74  is set on..*/.st
265e1 61 74 69 63 20 54 61 62 6c 65 20 2a 74 61 62 6c  atic Table *tabl
265e2 65 4f 66 54 72 69 67 67 65 72 28 54 72 69 67 67  eOfTrigger(Trigg
265e3 65 72 20 2a 70 54 72 69 67 67 65 72 29 7b 0a 20  er *pTrigger){. 
265e4 20 69 6e 74 20 6e 20 3d 20 73 74 72 6c 65 6e 28   int n = strlen(
265e5 70 54 72 69 67 67 65 72 2d 3e 74 61 62 6c 65 29  pTrigger->table)
265e6 20 2b 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 73   + 1;.  return s
265e7 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26  qlite3HashFind(&
265e8 70 54 72 69 67 67 65 72 2d 3e 70 54 61 62 53 63  pTrigger->pTabSc
265e9 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70  hema->tblHash, p
265ea 54 72 69 67 67 65 72 2d 3e 74 61 62 6c 65 2c 20  Trigger->table, 
265eb 6e 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 72  n);.}.../*.** Dr
265ec 6f 70 20 61 20 74 72 69 67 67 65 72 20 67 69 76  op a trigger giv
265ed 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  en a pointer to 
265ee 74 68 61 74 20 74 72 69 67 67 65 72 2e 20 0a 2a  that trigger. .*
265ef 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
265f0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f   void sqlite3Dro
265f1 70 54 72 69 67 67 65 72 50 74 72 28 50 61 72 73  pTriggerPtr(Pars
265f2 65 20 2a 70 50 61 72 73 65 2c 20 54 72 69 67 67  e *pParse, Trigg
265f3 65 72 20 2a 70 54 72 69 67 67 65 72 29 7b 0a 20  er *pTrigger){. 
265f4 20 54 61 62 6c 65 20 20 20 2a 70 54 61 62 6c 65   Table   *pTable
265f5 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73  ;.  Vdbe *v;.  s
265f6 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
265f7 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69  rse->db;.  int i
265f8 44 62 3b 0a 0a 20 20 69 44 62 20 3d 20 73 71 6c  Db;..  iDb = sql
265f9 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
265fa 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  x(pParse->db, pT
265fb 72 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 29  rigger->pSchema)
265fc 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e  ;.  assert( iDb>
265fd 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44  =0 && iDb<db->nD
265fe 62 20 29 3b 0a 20 20 70 54 61 62 6c 65 20 3d 20  b );.  pTable = 
265ff 74 61 62 6c 65 4f 66 54 72 69 67 67 65 72 28 70  tableOfTrigger(p
26600 54 72 69 67 67 65 72 29 3b 0a 20 20 61 73 73 65  Trigger);.  asse
26601 72 74 28 20 70 54 61 62 6c 65 20 29 3b 0a 20 20  rt( pTable );.  
26602 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e  assert( pTable->
26603 70 53 63 68 65 6d 61 3d 3d 70 54 72 69 67 67 65  pSchema==pTrigge
26604 72 2d 3e 70 53 63 68 65 6d 61 20 7c 7c 20 69 44  r->pSchema || iD
26605 62 3d 3d 31 20 29 3b 0a 23 69 66 6e 64 65 66 20  b==1 );.#ifndef 
26606 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
26607 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20  ORIZATION.  {.  
26608 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c    int code = SQL
26609 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 47 45 52  ITE_DROP_TRIGGER
2660a 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
2660b 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b   *zDb = db->aDb[
2660c 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  iDb].zName;.    
2660d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62  const char *zTab
2660e 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28   = SCHEMA_TABLE(
2660f 69 44 62 29 3b 0a 20 20 20 20 69 66 28 20 69 44  iDb);.    if( iD
26610 62 3d 3d 31 20 29 20 63 6f 64 65 20 3d 20 53 51  b==1 ) code = SQ
26611 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54  LITE_DROP_TEMP_T
26612 52 49 47 47 45 52 3b 0a 20 20 20 20 69 66 28 20  RIGGER;.    if( 
26613 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
26614 28 70 50 61 72 73 65 2c 20 63 6f 64 65 2c 20 70  (pParse, code, p
26615 54 72 69 67 67 65 72 2d 3e 6e 61 6d 65 2c 20 70  Trigger->name, p
26616 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 44  Table->zName, zD
26617 62 29 20 7c 7c 0a 20 20 20 20 20 20 73 71 6c 69  b) ||.      sqli
26618 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
26619 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45  rse, SQLITE_DELE
2661a 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20 7a 44 62  TE, zTab, 0, zDb
2661b 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
2661c 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  n;.    }.  }.#en
2661d 64 69 66 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61  dif..  /* Genera
2661e 74 65 20 63 6f 64 65 20 74 6f 20 64 65 73 74 72  te code to destr
2661f 6f 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20  oy the database 
26620 72 65 63 6f 72 64 20 6f 66 20 74 68 65 20 74 72  record of the tr
26621 69 67 67 65 72 2e 0a 20 20 2a 2f 0a 20 20 61 73  igger..  */.  as
26622 73 65 72 74 28 20 70 54 61 62 6c 65 21 3d 30 20  sert( pTable!=0 
26623 29 3b 0a 20 20 69 66 28 20 28 76 20 3d 20 73 71  );.  if( (v = sq
26624 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
26625 72 73 65 29 29 21 3d 30 20 29 7b 0a 20 20 20 20  rse))!=0 ){.    
26626 69 6e 74 20 62 61 73 65 3b 0a 20 20 20 20 73 74  int base;.    st
26627 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f  atic const VdbeO
26628 70 4c 69 73 74 20 64 72 6f 70 54 72 69 67 67 65  pList dropTrigge
26629 72 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b 20  r[] = {.      { 
2662a 4f 50 5f 52 65 77 69 6e 64 2c 20 20 20 20 20 30  OP_Rewind,     0
2662b 2c 20 41 44 44 52 28 39 29 2c 20 20 30 7d 2c 0a  , ADDR(9),  0},.
2662c 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e        { OP_Strin
2662d 67 38 2c 20 20 20 20 30 2c 20 31 2c 20 20 20 20  g8,    0, 1,    
2662e 20 20 20 20 30 7d 2c 20 2f 2a 20 31 20 2a 2f 0a      0}, /* 1 */.
2662f 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6c 75 6d        { OP_Colum
26630 6e 2c 20 20 20 20 20 30 2c 20 31 2c 20 20 20 20  n,     0, 1,    
26631 20 20 20 20 32 7d 2c 0a 20 20 20 20 20 20 7b 20      2},.      { 
26632 4f 50 5f 4e 65 2c 20 20 20 20 20 20 20 20 20 32  OP_Ne,         2
26633 2c 20 41 44 44 52 28 38 29 2c 20 20 31 7d 2c 0a  , ADDR(8),  1},.
26634 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e        { OP_Strin
26635 67 38 2c 20 20 20 20 30 2c 20 31 2c 20 20 20 20  g8,    0, 1,    
26636 20 20 20 20 30 7d 2c 20 2f 2a 20 34 3a 20 22 74      0}, /* 4: "t
26637 72 69 67 67 65 72 22 20 2a 2f 0a 20 20 20 20 20  rigger" */.     
26638 20 7b 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 20 20   { OP_Column,   
26639 20 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 32    0, 0,        2
2663a 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65  },.      { OP_Ne
2663b 2c 20 20 20 20 20 20 20 20 20 32 2c 20 41 44 44  ,         2, ADD
2663c 52 28 38 29 2c 20 20 31 7d 2c 0a 20 20 20 20 20  R(8),  1},.     
2663d 20 7b 20 4f 50 5f 44 65 6c 65 74 65 2c 20 20 20   { OP_Delete,   
2663e 20 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 30    0, 0,        0
2663f 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65  },.      { OP_Ne
26640 78 74 2c 20 20 20 20 20 20 20 30 2c 20 41 44 44  xt,       0, ADD
26641 52 28 31 29 2c 20 20 30 7d 2c 20 2f 2a 20 38 20  R(1),  0}, /* 8 
26642 2a 2f 0a 20 20 20 20 7d 3b 0a 0a 20 20 20 20 73  */.    };..    s
26643 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
26644 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
26645 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 73  , 0, iDb);.    s
26646 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72  qlite3OpenMaster
26647 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44  Table(pParse, iD
26648 62 29 3b 0a 20 20 20 20 62 61 73 65 20 3d 20 73  b);.    base = s
26649 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c  qlite3VdbeAddOpL
2664a 69 73 74 28 76 2c 20 20 41 72 72 61 79 53 69 7a  ist(v,  ArraySiz
2664b 65 28 64 72 6f 70 54 72 69 67 67 65 72 29 2c 20  e(dropTrigger), 
2664c 64 72 6f 70 54 72 69 67 67 65 72 29 3b 0a 20 20  dropTrigger);.  
2664d 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
2664e 6e 67 65 50 34 28 76 2c 20 62 61 73 65 2b 31 2c  ngeP4(v, base+1,
2664f 20 70 54 72 69 67 67 65 72 2d 3e 6e 61 6d 65 2c   pTrigger->name,
26650 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
26651 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
26652 62 61 73 65 2b 34 2c 20 22 74 72 69 67 67 65 72  base+4, "trigger
26653 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  ", P4_STATIC);. 
26654 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65     sqlite3Change
26655 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69  Cookie(pParse, i
26656 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Db);.    sqlite3
26657 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
26658 5f 43 6c 6f 73 65 2c 20 30 2c 20 30 29 3b 0a 20  _Close, 0, 0);. 
26659 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2665a 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54  dOp4(v, OP_DropT
2665b 72 69 67 67 65 72 2c 20 69 44 62 2c 20 30 2c 20  rigger, iDb, 0, 
2665c 30 2c 20 70 54 72 69 67 67 65 72 2d 3e 6e 61 6d  0, pTrigger->nam
2665d 65 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  e, 0);.  }.}../*
2665e 0a 2a 2a 20 52 65 6d 6f 76 65 20 61 20 74 72 69  .** Remove a tri
2665f 67 67 65 72 20 66 72 6f 6d 20 74 68 65 20 68 61  gger from the ha
26660 73 68 20 74 61 62 6c 65 73 20 6f 66 20 74 68 65  sh tables of the
26661 20 73 71 6c 69 74 65 2a 20 70 6f 69 6e 74 65 72   sqlite* pointer
26662 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
26663 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
26664 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54  UnlinkAndDeleteT
26665 72 69 67 67 65 72 28 73 71 6c 69 74 65 33 20 2a  rigger(sqlite3 *
26666 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e  db, int iDb, con
26667 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b  st char *zName){
26668 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
26669 67 67 65 72 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d  gger;.  int nNam
2666a 65 20 3d 20 73 74 72 6c 65 6e 28 7a 4e 61 6d 65  e = strlen(zName
2666b 29 3b 0a 20 20 70 54 72 69 67 67 65 72 20 3d 20  );.  pTrigger = 
2666c 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72  sqlite3HashInser
2666d 74 28 26 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d  t(&(db->aDb[iDb]
2666e 2e 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61  .pSchema->trigHa
2666f 73 68 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  sh),.           
26670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26671 20 20 20 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65      zName, nName
26672 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 54 72 69  , 0);.  if( pTri
26673 67 67 65 72 20 29 7b 0a 20 20 20 20 54 61 62 6c  gger ){.    Tabl
26674 65 20 2a 70 54 61 62 6c 65 20 3d 20 74 61 62 6c  e *pTable = tabl
26675 65 4f 66 54 72 69 67 67 65 72 28 70 54 72 69 67  eOfTrigger(pTrig
26676 67 65 72 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ger);.    assert
26677 28 20 70 54 61 62 6c 65 21 3d 30 20 29 3b 0a 20  ( pTable!=0 );. 
26678 20 20 20 69 66 28 20 70 54 61 62 6c 65 2d 3e 70     if( pTable->p
26679 54 72 69 67 67 65 72 20 3d 3d 20 70 54 72 69 67  Trigger == pTrig
2667a 67 65 72 20 29 7b 0a 20 20 20 20 20 20 70 54 61  ger ){.      pTa
2667b 62 6c 65 2d 3e 70 54 72 69 67 67 65 72 20 3d 20  ble->pTrigger = 
2667c 70 54 72 69 67 67 65 72 2d 3e 70 4e 65 78 74 3b  pTrigger->pNext;
2667d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2667e 20 20 54 72 69 67 67 65 72 20 2a 63 63 20 3d 20    Trigger *cc = 
2667f 70 54 61 62 6c 65 2d 3e 70 54 72 69 67 67 65 72  pTable->pTrigger
26680 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 63  ;.      while( c
26681 63 20 29 7b 20 0a 20 20 20 20 20 20 20 20 69 66  c ){ .        if
26682 28 20 63 63 2d 3e 70 4e 65 78 74 20 3d 3d 20 70  ( cc->pNext == p
26683 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 20  Trigger ){.     
26684 20 20 20 20 20 63 63 2d 3e 70 4e 65 78 74 20 3d       cc->pNext =
26685 20 63 63 2d 3e 70 4e 65 78 74 2d 3e 70 4e 65 78   cc->pNext->pNex
26686 74 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  t;.          bre
26687 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
26688 20 20 20 20 20 20 63 63 20 3d 20 63 63 2d 3e 70        cc = cc->p
26689 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Next;.      }.  
2668a 20 20 20 20 61 73 73 65 72 74 28 63 63 29 3b 0a      assert(cc);.
2668b 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2668c 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 64  3DeleteTrigger(d
2668d 62 2c 20 70 54 72 69 67 67 65 72 29 3b 0a 20 20  b, pTrigger);.  
2668e 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53    db->flags |= S
2668f 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e  QLITE_InternChan
26690 67 65 73 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ges;.  }.}../*.*
26691 2a 20 70 45 4c 69 73 74 20 69 73 20 74 68 65 20  * pEList is the 
26692 53 45 54 20 63 6c 61 75 73 65 20 6f 66 20 61 6e  SET clause of an
26693 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e   UPDATE statemen
26694 74 2e 20 20 45 61 63 68 20 65 6e 74 72 79 0a 2a  t.  Each entry.*
26695 2a 20 69 6e 20 70 45 4c 69 73 74 20 69 73 20 6f  * in pEList is o
26696 66 20 74 68 65 20 66 6f 72 6d 61 74 20 3c 69 64  f the format <id
26697 3e 3d 3c 65 78 70 72 3e 2e 20 20 49 66 20 61 6e  >=<expr>.  If an
26698 79 20 6f 66 20 74 68 65 20 65 6e 74 72 69 65 73  y of the entries
26699 0a 2a 2a 20 69 6e 20 70 45 4c 69 73 74 20 68 61  .** in pEList ha
2669a 76 65 20 61 6e 20 3c 69 64 3e 20 77 68 69 63 68  ve an <id> which
2669b 20 6d 61 74 63 68 65 73 20 61 6e 20 69 64 65 6e   matches an iden
2669c 74 69 66 69 65 72 20 69 6e 20 70 49 64 4c 69 73  tifier in pIdLis
2669d 74 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 74 75 72  t,.** then retur
2669e 6e 20 54 52 55 45 2e 20 20 49 66 20 70 49 64 4c  n TRUE.  If pIdL
2669f 69 73 74 3d 3d 4e 55 4c 4c 2c 20 74 68 65 6e 20  ist==NULL, then 
266a0 69 74 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  it is considered
266a1 20 61 0a 2a 2a 20 77 69 6c 64 63 61 72 64 20 74   a.** wildcard t
266a2 68 61 74 20 6d 61 74 63 68 65 73 20 61 6e 79 74  hat matches anyt
266a3 68 69 6e 67 2e 20 20 4c 69 6b 65 77 69 73 65 20  hing.  Likewise 
266a4 69 66 20 70 45 4c 69 73 74 3d 3d 4e 55 4c 4c 20  if pEList==NULL 
266a5 74 68 65 6e 0a 2a 2a 20 69 74 20 6d 61 74 63 68  then.** it match
266a6 65 73 20 61 6e 79 74 68 69 6e 67 20 73 6f 20 61  es anything so a
266a7 6c 77 61 79 73 20 72 65 74 75 72 6e 20 74 72 75  lways return tru
266a8 65 2e 20 20 52 65 74 75 72 6e 20 66 61 6c 73 65  e.  Return false
266a9 20 6f 6e 6c 79 0a 2a 2a 20 69 66 20 74 68 65 72   only.** if ther
266aa 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a  e is no match..*
266ab 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65  /.static int che
266ac 63 6b 43 6f 6c 75 6d 6e 4f 76 65 72 4c 61 70 28  ckColumnOverLap(
266ad 49 64 4c 69 73 74 20 2a 70 49 64 4c 69 73 74 2c  IdList *pIdList,
266ae 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
266af 74 29 7b 0a 20 20 69 6e 74 20 65 3b 0a 20 20 69  t){.  int e;.  i
266b0 66 28 20 21 70 49 64 4c 69 73 74 20 7c 7c 20 21  f( !pIdList || !
266b1 70 45 4c 69 73 74 20 29 20 72 65 74 75 72 6e 20  pEList ) return 
266b2 31 3b 0a 20 20 66 6f 72 28 65 3d 30 3b 20 65 3c  1;.  for(e=0; e<
266b3 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 65  pEList->nExpr; e
266b4 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  ++){.    if( sql
266b5 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28  ite3IdListIndex(
266b6 70 49 64 4c 69 73 74 2c 20 70 45 4c 69 73 74 2d  pIdList, pEList-
266b7 3e 61 5b 65 5d 2e 7a 4e 61 6d 65 29 3e 3d 30 20  >a[e].zName)>=0 
266b8 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a  ) return 1;.  }.
266b9 20 20 72 65 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a    return 0; .}..
266ba 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 62  /*.** Return a b
266bb 69 74 20 76 65 63 74 6f 72 20 74 6f 20 69 6e 64  it vector to ind
266bc 69 63 61 74 65 20 77 68 61 74 20 6b 69 6e 64 20  icate what kind 
266bd 6f 66 20 74 72 69 67 67 65 72 73 20 65 78 69 73  of triggers exis
266be 74 20 66 6f 72 20 6f 70 65 72 61 74 69 6f 6e 0a  t for operation.
266bf 2a 2a 20 22 6f 70 22 20 6f 6e 20 74 61 62 6c 65  ** "op" on table
266c0 20 70 54 61 62 2e 20 20 49 66 20 70 43 68 61 6e   pTab.  If pChan
266c1 67 65 73 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20  ges is not NULL 
266c2 74 68 65 6e 20 69 74 20 69 73 20 61 20 6c 69 73  then it is a lis
266c3 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20  t of columns.** 
266c4 74 68 61 74 20 61 72 65 20 62 65 69 6e 67 20 75  that are being u
266c5 70 64 61 74 65 64 2e 20 20 54 72 69 67 67 65 72  pdated.  Trigger
266c6 73 20 6f 6e 6c 79 20 6d 61 74 63 68 20 69 66 20  s only match if 
266c7 74 68 65 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66  the ON clause of
266c8 20 74 68 65 0a 2a 2a 20 74 72 69 67 67 65 72 20   the.** trigger 
266c9 64 65 66 69 6e 69 74 69 6f 6e 20 6f 76 65 72 6c  definition overl
266ca 61 70 73 20 74 68 65 20 73 65 74 20 6f 66 20 63  aps the set of c
266cb 6f 6c 75 6d 6e 73 20 62 65 69 6e 67 20 75 70 64  olumns being upd
266cc 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ated..**.** The 
266cd 72 65 74 75 72 6e 65 64 20 62 69 74 20 76 65 63  returned bit vec
266ce 74 6f 72 20 69 73 20 73 6f 6d 65 20 63 6f 6d 62  tor is some comb
266cf 69 6e 61 74 69 6f 6e 20 6f 66 20 54 52 49 47 47  ination of TRIGG
266d0 45 52 5f 42 45 46 4f 52 45 20 61 6e 64 0a 2a 2a  ER_BEFORE and.**
266d1 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 2e 0a   TRIGGER_AFTER..
266d2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
266d3 45 20 69 6e 74 20 73 71 6c 69 74 65 33 54 72 69  E int sqlite3Tri
266d4 67 67 65 72 73 45 78 69 73 74 28 0a 20 20 50 61  ggersExist(.  Pa
266d5 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
266d6 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f        /* Used to
266d7 20 63 68 65 63 6b 20 66 6f 72 20 72 65 63 75 72   check for recur
266d8 73 69 76 65 20 74 72 69 67 67 65 72 73 20 2a 2f  sive triggers */
266d9 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20  .  Table *pTab, 
266da 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
266db 65 20 74 61 62 6c 65 20 74 68 65 20 63 6f 6e 74  e table the cont
266dc 61 69 6e 73 20 74 68 65 20 74 72 69 67 67 65 72  ains the trigger
266dd 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20  s */.  int op,  
266de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
266df 2a 20 6f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45  * one of TK_DELE
266e0 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54  TE, TK_INSERT, T
266e1 4b 5f 55 50 44 41 54 45 20 2a 2f 0a 20 20 45 78  K_UPDATE */.  Ex
266e2 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65 73  prList *pChanges
266e3 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73        /* Columns
266e4 20 74 68 61 74 20 63 68 61 6e 67 65 20 69 6e 20   that change in 
266e5 61 6e 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  an UPDATE statem
266e6 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67  ent */.){.  Trig
266e7 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 0a 20  ger *pTrigger;. 
266e8 20 69 6e 74 20 6d 61 73 6b 20 3d 20 30 3b 0a 0a   int mask = 0;..
266e9 20 20 70 54 72 69 67 67 65 72 20 3d 20 49 73 56    pTrigger = IsV
266ea 69 72 74 75 61 6c 28 70 54 61 62 29 20 3f 20 30  irtual(pTab) ? 0
266eb 20 3a 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65   : pTab->pTrigge
266ec 72 3b 0a 20 20 77 68 69 6c 65 28 20 70 54 72 69  r;.  while( pTri
266ed 67 67 65 72 20 29 7b 0a 20 20 20 20 69 66 28 20  gger ){.    if( 
266ee 70 54 72 69 67 67 65 72 2d 3e 6f 70 3d 3d 6f 70  pTrigger->op==op
266ef 20 26 26 20 63 68 65 63 6b 43 6f 6c 75 6d 6e 4f   && checkColumnO
266f0 76 65 72 4c 61 70 28 70 54 72 69 67 67 65 72 2d  verLap(pTrigger-
266f1 3e 70 43 6f 6c 75 6d 6e 73 2c 20 70 43 68 61 6e  >pColumns, pChan
266f2 67 65 73 29 20 29 7b 0a 20 20 20 20 20 20 6d 61  ges) ){.      ma
266f3 73 6b 20 7c 3d 20 70 54 72 69 67 67 65 72 2d 3e  sk |= pTrigger->
266f4 74 72 5f 74 6d 3b 0a 20 20 20 20 7d 0a 20 20 20  tr_tm;.    }.   
266f5 20 70 54 72 69 67 67 65 72 20 3d 20 70 54 72 69   pTrigger = pTri
266f6 67 67 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d  gger->pNext;.  }
266f7 0a 20 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a  .  return mask;.
266f8 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  }../*.** Convert
266f9 20 74 68 65 20 70 53 74 65 70 2d 3e 74 61 72 67   the pStep->targ
266fa 65 74 20 74 6f 6b 65 6e 20 69 6e 74 6f 20 61 20  et token into a 
266fb 53 72 63 4c 69 73 74 20 61 6e 64 20 72 65 74 75  SrcList and retu
266fc 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20  rn a pointer.** 
266fd 74 6f 20 74 68 61 74 20 53 72 63 4c 69 73 74 2e  to that SrcList.
266fe 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
266ff 69 6e 65 20 61 64 64 73 20 61 20 73 70 65 63 69  ine adds a speci
26700 66 69 63 20 64 61 74 61 62 61 73 65 20 6e 61 6d  fic database nam
26701 65 2c 20 69 66 20 6e 65 65 64 65 64 2c 20 74 6f  e, if needed, to
26702 20 74 68 65 20 74 61 72 67 65 74 20 77 68 65 6e   the target when
26703 0a 2a 2a 20 66 6f 72 6d 69 6e 67 20 74 68 65 20  .** forming the 
26704 53 72 63 4c 69 73 74 2e 20 20 54 68 69 73 20 70  SrcList.  This p
26705 72 65 76 65 6e 74 73 20 61 20 74 72 69 67 67 65  revents a trigge
26706 72 20 69 6e 20 6f 6e 65 20 64 61 74 61 62 61 73  r in one databas
26707 65 20 66 72 6f 6d 0a 2a 2a 20 72 65 66 65 72 72  e from.** referr
26708 69 6e 67 20 74 6f 20 61 20 74 61 72 67 65 74 20  ing to a target 
26709 69 6e 20 61 6e 6f 74 68 65 72 20 64 61 74 61 62  in another datab
2670a 61 73 65 2e 20 20 41 6e 20 65 78 63 65 70 74 69  ase.  An excepti
2670b 6f 6e 20 69 73 20 77 68 65 6e 20 74 68 65 0a 2a  on is when the.*
2670c 2a 20 74 72 69 67 67 65 72 20 69 73 20 69 6e 20  * trigger is in 
2670d 54 45 4d 50 20 69 6e 20 77 68 69 63 68 20 63 61  TEMP in which ca
2670e 73 65 20 69 74 20 63 61 6e 20 72 65 66 65 72 20  se it can refer 
2670f 74 6f 20 61 6e 79 20 6f 74 68 65 72 20 64 61 74  to any other dat
26710 61 62 61 73 65 20 69 74 0a 2a 2a 20 77 61 6e 74  abase it.** want
26711 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 72 63  s..*/.static Src
26712 4c 69 73 74 20 2a 74 61 72 67 65 74 53 72 63 4c  List *targetSrcL
26713 69 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ist(.  Parse *pP
26714 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 54  arse,       /* T
26715 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65  he parsing conte
26716 78 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53  xt */.  TriggerS
26717 74 65 70 20 2a 70 53 74 65 70 20 20 20 2f 2a 20  tep *pStep   /* 
26718 54 68 65 20 74 72 69 67 67 65 72 20 63 6f 6e 74  The trigger cont
26719 61 69 6e 69 6e 67 20 74 68 65 20 74 61 72 67 65  aining the targe
2671a 74 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20  t token */.){.  
2671b 54 6f 6b 65 6e 20 73 44 62 3b 20 20 20 20 20 20  Token sDb;      
2671c 20 20 20 20 20 2f 2a 20 44 75 6d 6d 79 20 64 61       /* Dummy da
2671d 74 61 62 61 73 65 20 6e 61 6d 65 20 74 6f 6b 65  tabase name toke
2671e 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  n */.  int iDb; 
2671f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
26720 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
26721 62 61 73 65 20 74 6f 20 75 73 65 20 2a 2f 0a 20  base to use */. 
26722 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 20   SrcList *pSrc; 
26723 20 20 20 20 20 20 2f 2a 20 53 72 63 4c 69 73 74        /* SrcList
26724 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20   to be returned 
26725 2a 2f 0a 0a 20 20 69 44 62 20 3d 20 73 71 6c 69  */..  iDb = sqli
26726 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
26727 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 74  (pParse->db, pSt
26728 65 70 2d 3e 70 54 72 69 67 2d 3e 70 53 63 68 65  ep->pTrig->pSche
26729 6d 61 29 3b 0a 20 20 69 66 28 20 69 44 62 3d 3d  ma);.  if( iDb==
2672a 30 20 7c 7c 20 69 44 62 3e 3d 32 20 29 7b 0a 20  0 || iDb>=2 ){. 
2672b 20 20 20 61 73 73 65 72 74 28 20 69 44 62 3c 70     assert( iDb<p
2672c 50 61 72 73 65 2d 3e 64 62 2d 3e 6e 44 62 20 29  Parse->db->nDb )
2672d 3b 0a 20 20 20 20 73 44 62 2e 7a 20 3d 20 28 75  ;.    sDb.z = (u
2672e 38 2a 29 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61  8*)pParse->db->a
2672f 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20  Db[iDb].zName;. 
26730 20 20 20 73 44 62 2e 6e 20 3d 20 73 74 72 6c 65     sDb.n = strle
26731 6e 28 28 63 68 61 72 2a 29 73 44 62 2e 7a 29 3b  n((char*)sDb.z);
26732 0a 20 20 20 20 70 53 72 63 20 3d 20 73 71 6c 69  .    pSrc = sqli
26733 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
26734 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 30 2c 20  (pParse->db, 0, 
26735 26 73 44 62 2c 20 26 70 53 74 65 70 2d 3e 74 61  &sDb, &pStep->ta
26736 72 67 65 74 29 3b 0a 20 20 7d 20 65 6c 73 65 20  rget);.  } else 
26737 7b 0a 20 20 20 20 70 53 72 63 20 3d 20 73 71 6c  {.    pSrc = sql
26738 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
26739 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 30 2c  d(pParse->db, 0,
2673a 20 26 70 53 74 65 70 2d 3e 74 61 72 67 65 74 2c   &pStep->target,
2673b 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   0);.  }.  retur
2673c 6e 20 70 53 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pSrc;.}../*.**
2673d 20 47 65 6e 65 72 61 74 65 20 56 44 42 45 20 63   Generate VDBE c
2673e 6f 64 65 20 66 6f 72 20 7a 65 72 6f 20 6f 72 20  ode for zero or 
2673f 6d 6f 72 65 20 73 74 61 74 65 6d 65 6e 74 73 20  more statements 
26740 69 6e 73 69 64 65 20 74 68 65 20 62 6f 64 79 20  inside the body 
26741 6f 66 20 61 0a 2a 2a 20 74 72 69 67 67 65 72 2e  of a.** trigger.
26742 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74    .*/.static int
26743 20 63 6f 64 65 54 72 69 67 67 65 72 50 72 6f 67   codeTriggerProg
26744 72 61 6d 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ram(.  Parse *pP
26745 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  arse,           
26746 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
26747 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72 69 67  ontext */.  Trig
26748 67 65 72 53 74 65 70 20 2a 70 53 74 65 70 4c 69  gerStep *pStepLi
26749 73 74 2c 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66  st,   /* List of
2674a 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 73 69   statements insi
2674b 64 65 20 74 68 65 20 74 72 69 67 67 65 72 20 62  de the trigger b
2674c 6f 64 79 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63  ody */.  int orc
2674d 6f 6e 66 69 6e 20 20 20 20 20 20 20 20 20 20 20  onfin           
2674e 20 20 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 20 61     /* Conflict a
2674f 6c 67 6f 72 69 74 68 6d 2e 20 28 4f 45 5f 41 62  lgorithm. (OE_Ab
26750 6f 72 74 2c 20 65 74 63 29 20 2a 2f 20 20 0a 29  ort, etc) */  .)
26751 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20  {.  TriggerStep 
26752 2a 20 70 54 72 69 67 67 65 72 53 74 65 70 20 3d  * pTriggerStep =
26753 20 70 53 74 65 70 4c 69 73 74 3b 0a 20 20 69 6e   pStepList;.  in
26754 74 20 6f 72 63 6f 6e 66 3b 0a 20 20 56 64 62 65  t orconf;.  Vdbe
26755 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
26756 64 62 65 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  dbe;.  sqlite3 *
26757 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
26758 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54 72 69  ..  assert( pTri
26759 67 67 65 72 53 74 65 70 21 3d 30 20 29 3b 0a 20  ggerStep!=0 );. 
2675a 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b   assert( v!=0 );
2675b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2675c 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 6e 74 65  dOp2(v, OP_Conte
2675d 78 74 50 75 73 68 2c 20 30 2c 20 30 29 3b 0a 20  xtPush, 0, 0);. 
2675e 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
2675f 20 22 62 65 67 69 6e 20 74 72 69 67 67 65 72 20   "begin trigger 
26760 25 73 22 2c 20 70 53 74 65 70 4c 69 73 74 2d 3e  %s", pStepList->
26761 70 54 72 69 67 2d 3e 6e 61 6d 65 29 29 3b 0a 20  pTrig->name));. 
26762 20 77 68 69 6c 65 28 20 70 54 72 69 67 67 65 72   while( pTrigger
26763 53 74 65 70 20 29 7b 0a 20 20 20 20 6f 72 63 6f  Step ){.    orco
26764 6e 66 20 3d 20 28 6f 72 63 6f 6e 66 69 6e 20 3d  nf = (orconfin =
26765 3d 20 4f 45 5f 44 65 66 61 75 6c 74 29 3f 70 54  = OE_Default)?pT
26766 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 72 63 6f  riggerStep->orco
26767 6e 66 3a 6f 72 63 6f 6e 66 69 6e 3b 0a 20 20 20  nf:orconfin;.   
26768 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61   pParse->trigSta
26769 63 6b 2d 3e 6f 72 63 6f 6e 66 20 3d 20 6f 72 63  ck->orconf = orc
2676a 6f 6e 66 3b 0a 20 20 20 20 73 77 69 74 63 68 28  onf;.    switch(
2676b 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f   pTriggerStep->o
2676c 70 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20  p ){.      case 
2676d 54 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a 20 20 20  TK_SELECT: {.   
2676e 20 20 20 20 20 53 65 6c 65 63 74 20 2a 73 73 20       Select *ss 
2676f 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  = sqlite3SelectD
26770 75 70 28 64 62 2c 20 70 54 72 69 67 67 65 72 53  up(db, pTriggerS
26771 74 65 70 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20  tep->pSelect);. 
26772 20 20 20 20 20 20 20 69 66 28 20 73 73 20 29 7b         if( ss ){
26773 0a 20 20 20 20 20 20 20 20 20 20 53 65 6c 65 63  .          Selec
26774 74 44 65 73 74 20 64 65 73 74 3b 0a 0a 20 20 20  tDest dest;..   
26775 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65         sqlite3Se
26776 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65  lectDestInit(&de
26777 73 74 2c 20 53 52 54 5f 44 69 73 63 61 72 64 2c  st, SRT_Discard,
26778 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73   0);.          s
26779 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61  qlite3Select(pPa
2677a 72 73 65 2c 20 73 73 2c 20 26 64 65 73 74 29 3b  rse, ss, &dest);
2677b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2677c 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64  e3SelectDelete(d
2677d 62 2c 20 73 73 29 3b 0a 20 20 20 20 20 20 20 20  b, ss);.        
2677e 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2677f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
26780 61 73 65 20 54 4b 5f 55 50 44 41 54 45 3a 20 7b  ase TK_UPDATE: {
26781 0a 20 20 20 20 20 20 20 20 53 72 63 4c 69 73 74  .        SrcList
26782 20 2a 70 53 72 63 3b 0a 20 20 20 20 20 20 20 20   *pSrc;.        
26783 70 53 72 63 20 3d 20 74 61 72 67 65 74 53 72 63  pSrc = targetSrc
26784 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 72  List(pParse, pTr
26785 69 67 67 65 72 53 74 65 70 29 3b 0a 20 20 20 20  iggerStep);.    
26786 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26787 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 65  ddOp2(v, OP_Rese
26788 74 43 6f 75 6e 74 2c 20 30 2c 20 30 29 3b 0a 20  tCount, 0, 0);. 
26789 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 55 70         sqlite3Up
2678a 64 61 74 65 28 70 50 61 72 73 65 2c 20 70 53 72  date(pParse, pSr
2678b 63 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  c,.             
2678c 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
2678d 73 74 44 75 70 28 64 62 2c 20 70 54 72 69 67 67  stDup(db, pTrigg
2678e 65 72 53 74 65 70 2d 3e 70 45 78 70 72 4c 69 73  erStep->pExprLis
2678f 74 29 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  t), .           
26790 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
26791 44 75 70 28 64 62 2c 20 70 54 72 69 67 67 65 72  Dup(db, pTrigger
26792 53 74 65 70 2d 3e 70 57 68 65 72 65 29 2c 20 6f  Step->pWhere), o
26793 72 63 6f 6e 66 29 3b 0a 20 20 20 20 20 20 20 20  rconf);.        
26794 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26795 32 28 76 2c 20 4f 50 5f 52 65 73 65 74 43 6f 75  2(v, OP_ResetCou
26796 6e 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20  nt, 1, 0);.     
26797 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
26798 7d 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f  }.      case TK_
26799 49 4e 53 45 52 54 3a 20 7b 0a 20 20 20 20 20 20  INSERT: {.      
2679a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b    SrcList *pSrc;
2679b 0a 20 20 20 20 20 20 20 20 70 53 72 63 20 3d 20  .        pSrc = 
2679c 74 61 72 67 65 74 53 72 63 4c 69 73 74 28 70 50  targetSrcList(pP
2679d 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 53 74  arse, pTriggerSt
2679e 65 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ep);.        sql
2679f 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
267a0 2c 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 2c  , OP_ResetCount,
267a1 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   0, 0);.        
267a2 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 70 50  sqlite3Insert(pP
267a3 61 72 73 65 2c 20 70 53 72 63 2c 0a 20 20 20 20  arse, pSrc,.    
267a4 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
267a5 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 54 72  rListDup(db, pTr
267a6 69 67 67 65 72 53 74 65 70 2d 3e 70 45 78 70 72  iggerStep->pExpr
267a7 4c 69 73 74 29 2c 20 0a 20 20 20 20 20 20 20 20  List), .        
267a8 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
267a9 75 70 28 64 62 2c 20 70 54 72 69 67 67 65 72 53  up(db, pTriggerS
267aa 74 65 70 2d 3e 70 53 65 6c 65 63 74 29 2c 20 0a  tep->pSelect), .
267ab 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
267ac 33 49 64 4c 69 73 74 44 75 70 28 64 62 2c 20 70  3IdListDup(db, p
267ad 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 49 64  TriggerStep->pId
267ae 4c 69 73 74 29 2c 20 6f 72 63 6f 6e 66 29 3b 0a  List), orconf);.
267af 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
267b0 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
267b1 52 65 73 65 74 43 6f 75 6e 74 2c 20 31 2c 20 30  ResetCount, 1, 0
267b2 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
267b3 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
267b4 63 61 73 65 20 54 4b 5f 44 45 4c 45 54 45 3a 20  case TK_DELETE: 
267b5 7b 0a 20 20 20 20 20 20 20 20 53 72 63 4c 69 73  {.        SrcLis
267b6 74 20 2a 70 53 72 63 3b 0a 20 20 20 20 20 20 20  t *pSrc;.       
267b7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
267b8 70 32 28 76 2c 20 4f 50 5f 52 65 73 65 74 43 6f  p2(v, OP_ResetCo
267b9 75 6e 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  unt, 0, 0);.    
267ba 20 20 20 20 70 53 72 63 20 3d 20 74 61 72 67 65      pSrc = targe
267bb 74 53 72 63 4c 69 73 74 28 70 50 61 72 73 65 2c  tSrcList(pParse,
267bc 20 70 54 72 69 67 67 65 72 53 74 65 70 29 3b 0a   pTriggerStep);.
267bd 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
267be 65 6c 65 74 65 46 72 6f 6d 28 70 50 61 72 73 65  eleteFrom(pParse
267bf 2c 20 70 53 72 63 2c 20 0a 20 20 20 20 20 20 20  , pSrc, .       
267c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
267c1 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 75     sqlite3ExprDu
267c2 70 28 64 62 2c 20 70 54 72 69 67 67 65 72 53 74  p(db, pTriggerSt
267c3 65 70 2d 3e 70 57 68 65 72 65 29 29 3b 0a 20 20  ep->pWhere));.  
267c4 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
267c5 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
267c6 73 65 74 43 6f 75 6e 74 2c 20 31 2c 20 30 29 3b  setCount, 1, 0);
267c7 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
267c8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65        }.      de
267c9 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 61  fault:.        a
267ca 73 73 65 72 74 28 30 29 3b 0a 20 20 20 20 7d 20  ssert(0);.    } 
267cb 0a 20 20 20 20 70 54 72 69 67 67 65 72 53 74 65  .    pTriggerSte
267cc 70 20 3d 20 70 54 72 69 67 67 65 72 53 74 65 70  p = pTriggerStep
267cd 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 73  ->pNext;.  }.  s
267ce 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
267cf 28 76 2c 20 4f 50 5f 43 6f 6e 74 65 78 74 50 6f  (v, OP_ContextPo
267d0 70 2c 20 30 2c 20 30 29 3b 0a 20 20 56 64 62 65  p, 0, 0);.  Vdbe
267d1 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 65 6e 64  Comment((v, "end
267d2 20 74 72 69 67 67 65 72 20 25 73 22 2c 20 70 53   trigger %s", pS
267d3 74 65 70 4c 69 73 74 2d 3e 70 54 72 69 67 2d 3e  tepList->pTrig->
267d4 6e 61 6d 65 29 29 3b 0a 0a 20 20 72 65 74 75 72  name));..  retur
267d5 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  n 0;.}../*.** Th
267d6 69 73 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  is is called to 
267d7 63 6f 64 65 20 46 4f 52 20 45 41 43 48 20 52 4f  code FOR EACH RO
267d8 57 20 74 72 69 67 67 65 72 73 2e 0a 2a 2a 0a 2a  W triggers..**.*
267d9 2a 20 57 68 65 6e 20 74 68 65 20 63 6f 64 65 20  * When the code 
267da 74 68 61 74 20 74 68 69 73 20 66 75 6e 63 74 69  that this functi
267db 6f 6e 20 67 65 6e 65 72 61 74 65 73 20 69 73 20  on generates is 
267dc 65 78 65 63 75 74 65 64 2c 20 74 68 65 20 66 6f  executed, the fo
267dd 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 6d 75 73 74  llowing .** must
267de 20 62 65 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20   be true:.**.** 
267df 31 2e 20 4e 6f 20 63 75 72 73 6f 72 73 20 6d 61  1. No cursors ma
267e0 79 20 62 65 20 6f 70 65 6e 20 69 6e 20 74 68 65  y be open in the
267e1 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20   main database. 
267e2 20 28 42 75 74 20 6e 65 77 49 64 78 20 61 6e 64   (But newIdx and
267e3 20 6f 6c 64 49 64 78 0a 2a 2a 20 20 20 20 63 61   oldIdx.**    ca
267e4 6e 20 62 65 20 69 6e 64 69 63 65 73 20 6f 66 20  n be indices of 
267e5 63 75 72 73 6f 72 73 20 69 6e 20 74 65 6d 70 6f  cursors in tempo
267e6 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20 53 65  rary tables.  Se
267e7 65 20 62 65 6c 6f 77 2e 29 0a 2a 2a 0a 2a 2a 20  e below.).**.** 
267e8 32 2e 20 49 66 20 74 68 65 20 74 72 69 67 67 65  2. If the trigge
267e9 72 73 20 62 65 69 6e 67 20 63 6f 64 65 64 20 61  rs being coded a
267ea 72 65 20 4f 4e 20 49 4e 53 45 52 54 20 6f 72 20  re ON INSERT or 
267eb 4f 4e 20 55 50 44 41 54 45 20 74 72 69 67 67 65  ON UPDATE trigge
267ec 72 73 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 61  rs, then.**    a
267ed 20 74 65 6d 70 6f 72 61 72 79 20 76 64 62 65 20   temporary vdbe 
267ee 63 75 72 73 6f 72 20 28 69 6e 64 65 78 20 6e 65  cursor (index ne
267ef 77 49 64 78 29 20 6d 75 73 74 20 62 65 20 6f 70  wIdx) must be op
267f0 65 6e 20 61 6e 64 20 70 6f 69 6e 74 69 6e 67 20  en and pointing 
267f1 61 74 0a 2a 2a 20 20 20 20 61 20 72 6f 77 20 63  at.**    a row c
267f2 6f 6e 74 61 69 6e 69 6e 67 20 76 61 6c 75 65 73  ontaining values
267f3 20 74 6f 20 62 65 20 73 75 62 73 74 69 74 75 74   to be substitut
267f4 65 64 20 66 6f 72 20 6e 65 77 2e 2a 20 65 78 70  ed for new.* exp
267f5 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 0a  ressions in the.
267f6 2a 2a 20 20 20 20 74 72 69 67 67 65 72 20 70 72  **    trigger pr
267f7 6f 67 72 61 6d 28 73 29 2e 0a 2a 2a 0a 2a 2a 20  ogram(s)..**.** 
267f8 33 2e 20 49 66 20 74 68 65 20 74 72 69 67 67 65  3. If the trigge
267f9 72 73 20 62 65 69 6e 67 20 63 6f 64 65 64 20 61  rs being coded a
267fa 72 65 20 4f 4e 20 44 45 4c 45 54 45 20 6f 72 20  re ON DELETE or 
267fb 4f 4e 20 55 50 44 41 54 45 20 74 72 69 67 67 65  ON UPDATE trigge
267fc 72 73 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 61  rs, then.**    a
267fd 20 74 65 6d 70 6f 72 61 72 79 20 76 64 62 65 20   temporary vdbe 
267fe 63 75 72 73 6f 72 20 28 69 6e 64 65 78 20 6f 6c  cursor (index ol
267ff 64 49 64 78 29 20 6d 75 73 74 20 62 65 20 6f 70  dIdx) must be op
26800 65 6e 20 61 6e 64 20 70 6f 69 6e 74 69 6e 67 20  en and pointing 
26801 61 74 0a 2a 2a 20 20 20 20 61 20 72 6f 77 20 63  at.**    a row c
26802 6f 6e 74 61 69 6e 69 6e 67 20 76 61 6c 75 65 73  ontaining values
26803 20 74 6f 20 62 65 20 73 75 62 73 74 69 74 75 74   to be substitut
26804 65 64 20 66 6f 72 20 6f 6c 64 2e 2a 20 65 78 70  ed for old.* exp
26805 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 0a  ressions in the.
26806 2a 2a 20 20 20 20 74 72 69 67 67 65 72 20 70 72  **    trigger pr
26807 6f 67 72 61 6d 28 73 29 2e 0a 2a 2a 0a 2a 2a 20  ogram(s)..**.** 
26808 49 66 20 74 68 65 79 20 61 72 65 20 6e 6f 74 20  If they are not 
26809 4e 55 4c 4c 2c 20 74 68 65 20 70 69 4f 6c 64 43  NULL, the piOldC
2680a 6f 6c 4d 61 73 6b 20 61 6e 64 20 70 69 4e 65 77  olMask and piNew
2680b 43 6f 6c 4d 61 73 6b 20 6f 75 74 70 75 74 20 76  ColMask output v
2680c 61 72 69 61 62 6c 65 73 0a 2a 2a 20 61 72 65 20  ariables.** are 
2680d 73 65 74 20 74 6f 20 76 61 6c 75 65 73 20 74 68  set to values th
2680e 61 74 20 64 65 73 63 72 69 62 65 20 74 68 65 20  at describe the 
2680f 63 6f 6c 75 6d 6e 73 20 75 73 65 64 20 62 79 20  columns used by 
26810 74 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67  the trigger prog
26811 72 61 6d 0a 2a 2a 20 69 6e 20 74 68 65 20 4f 4c  ram.** in the OL
26812 44 2e 2a 20 61 6e 64 20 4e 45 57 2e 2a 20 74 61  D.* and NEW.* ta
26813 62 6c 65 73 20 72 65 73 70 65 63 74 69 76 65 6c  bles respectivel
26814 79 2e 20 49 66 20 63 6f 6c 75 6d 6e 20 4e 20 6f  y. If column N o
26815 66 20 74 68 65 20 0a 2a 2a 20 70 73 65 75 64 6f  f the .** pseudo
26816 2d 74 61 62 6c 65 20 69 73 20 72 65 61 64 20 61  -table is read a
26817 74 20 6c 65 61 73 74 20 6f 6e 63 65 2c 20 74 68  t least once, th
26818 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
26819 62 69 74 20 6f 66 20 74 68 65 20 6f 75 74 70 75  bit of the outpu
2681a 74 0a 2a 2a 20 6d 61 73 6b 20 69 73 20 73 65 74  t.** mask is set
2681b 2e 20 49 66 20 61 20 63 6f 6c 75 6d 6e 20 77 69  . If a column wi
2681c 74 68 20 61 6e 20 69 6e 64 65 78 20 67 72 65 61  th an index grea
2681d 74 65 72 20 74 68 61 6e 20 33 32 20 69 73 20 72  ter than 32 is r
2681e 65 61 64 2c 20 74 68 65 0a 2a 2a 20 6f 75 74 70  ead, the.** outp
2681f 75 74 20 6d 61 73 6b 20 69 73 20 73 65 74 20 74  ut mask is set t
26820 6f 20 74 68 65 20 73 70 65 63 69 61 6c 20 76 61  o the special va
26821 6c 75 65 20 30 78 66 66 66 66 66 66 66 66 2e 0a  lue 0xffffffff..
26822 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  **.*/.SQLITE_PRI
26823 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
26824 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 0a  CodeRowTrigger(.
26825 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
26826 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20         /* Parse 
26827 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74  context */.  int
26828 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   op,            
26829 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 55    /* One of TK_U
2682a 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54  PDATE, TK_INSERT
2682b 2c 20 54 4b 5f 44 45 4c 45 54 45 20 2a 2f 0a 20  , TK_DELETE */. 
2682c 20 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e   ExprList *pChan
2682d 67 65 73 2c 20 20 2f 2a 20 43 68 61 6e 67 65 73  ges,  /* Changes
2682e 20 6c 69 73 74 20 66 6f 72 20 61 6e 79 20 55 50   list for any UP
2682f 44 41 54 45 20 4f 46 20 74 72 69 67 67 65 72 73  DATE OF triggers
26830 20 2a 2f 0a 20 20 69 6e 74 20 74 72 5f 74 6d 2c   */.  int tr_tm,
26831 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
26832 65 20 6f 66 20 54 52 49 47 47 45 52 5f 42 45 46  e of TRIGGER_BEF
26833 4f 52 45 2c 20 54 52 49 47 47 45 52 5f 41 46 54  ORE, TRIGGER_AFT
26834 45 52 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  ER */.  Table *p
26835 54 61 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Tab,         /* 
26836 54 68 65 20 74 61 62 6c 65 20 74 6f 20 63 6f 64  The table to cod
26837 65 20 74 72 69 67 67 65 72 73 20 66 72 6f 6d 20  e triggers from 
26838 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 49 64 78 2c  */.  int newIdx,
26839 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2683a 20 69 6e 64 69 63 65 20 6f 66 20 74 68 65 20 22   indice of the "
2683b 6e 65 77 22 20 72 6f 77 20 74 6f 20 61 63 63 65  new" row to acce
2683c 73 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 6c 64 49  ss */.  int oldI
2683d 64 78 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  dx,          /* 
2683e 54 68 65 20 69 6e 64 69 63 65 20 6f 66 20 74 68  The indice of th
2683f 65 20 22 6f 6c 64 22 20 72 6f 77 20 74 6f 20 61  e "old" row to a
26840 63 63 65 73 73 20 2a 2f 0a 20 20 69 6e 74 20 6f  ccess */.  int o
26841 72 63 6f 6e 66 2c 20 20 20 20 20 20 20 20 20 20  rconf,          
26842 2f 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70  /* ON CONFLICT p
26843 6f 6c 69 63 79 20 2a 2f 0a 20 20 69 6e 74 20 69  olicy */.  int i
26844 67 6e 6f 72 65 4a 75 6d 70 2c 20 20 20 20 20 20  gnoreJump,      
26845 2f 2a 20 49 6e 73 74 72 75 63 74 69 6f 6e 20 74  /* Instruction t
26846 6f 20 6a 75 6d 70 20 74 6f 20 66 6f 72 20 52 41  o jump to for RA
26847 49 53 45 28 49 47 4e 4f 52 45 29 20 2a 2f 0a 20  ISE(IGNORE) */. 
26848 20 75 33 32 20 2a 70 69 4f 6c 64 43 6f 6c 4d 61   u32 *piOldColMa
26849 73 6b 2c 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 61  sk,   /* OUT: Ma
2684a 73 6b 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 75 73  sk of columns us
2684b 65 64 20 66 72 6f 6d 20 74 68 65 20 4f 4c 44 2e  ed from the OLD.
2684c 2a 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 33 32  * table */.  u32
2684d 20 2a 70 69 4e 65 77 43 6f 6c 4d 61 73 6b 20 20   *piNewColMask  
2684e 20 20 2f 2a 20 4f 55 54 3a 20 4d 61 73 6b 20 6f    /* OUT: Mask o
2684f 66 20 63 6f 6c 75 6d 6e 73 20 75 73 65 64 20 66  f columns used f
26850 72 6f 6d 20 74 68 65 20 4e 45 57 2e 2a 20 74 61  rom the NEW.* ta
26851 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67  ble */.){.  Trig
26852 67 65 72 20 2a 70 3b 0a 20 20 73 71 6c 69 74 65  ger *p;.  sqlite
26853 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
26854 64 62 3b 0a 20 20 54 72 69 67 67 65 72 53 74 61  db;.  TriggerSta
26855 63 6b 20 74 72 69 67 53 74 61 63 6b 45 6e 74 72  ck trigStackEntr
26856 79 3b 0a 0a 20 20 74 72 69 67 53 74 61 63 6b 45  y;..  trigStackE
26857 6e 74 72 79 2e 6f 6c 64 43 6f 6c 4d 61 73 6b 20  ntry.oldColMask 
26858 3d 20 30 3b 0a 20 20 74 72 69 67 53 74 61 63 6b  = 0;.  trigStack
26859 45 6e 74 72 79 2e 6e 65 77 43 6f 6c 4d 61 73 6b  Entry.newColMask
2685a 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28   = 0;..  assert(
2685b 6f 70 20 3d 3d 20 54 4b 5f 55 50 44 41 54 45 20  op == TK_UPDATE 
2685c 7c 7c 20 6f 70 20 3d 3d 20 54 4b 5f 49 4e 53 45  || op == TK_INSE
2685d 52 54 20 7c 7c 20 6f 70 20 3d 3d 20 54 4b 5f 44  RT || op == TK_D
2685e 45 4c 45 54 45 29 3b 0a 20 20 61 73 73 65 72 74  ELETE);.  assert
2685f 28 74 72 5f 74 6d 20 3d 3d 20 54 52 49 47 47 45  (tr_tm == TRIGGE
26860 52 5f 42 45 46 4f 52 45 20 7c 7c 20 74 72 5f 74  R_BEFORE || tr_t
26861 6d 20 3d 3d 20 54 52 49 47 47 45 52 5f 41 46 54  m == TRIGGER_AFT
26862 45 52 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28  ER );..  assert(
26863 6e 65 77 49 64 78 20 21 3d 20 2d 31 20 7c 7c 20  newIdx != -1 || 
26864 6f 6c 64 49 64 78 20 21 3d 20 2d 31 29 3b 0a 0a  oldIdx != -1);..
26865 20 20 66 6f 72 28 70 3d 70 54 61 62 2d 3e 70 54    for(p=pTab->pT
26866 72 69 67 67 65 72 3b 20 70 3b 20 70 3d 70 2d 3e  rigger; p; p=p->
26867 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 74 20  pNext){.    int 
26868 66 69 72 65 5f 74 68 69 73 20 3d 20 30 3b 0a 0a  fire_this = 0;..
26869 20 20 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65      /* Determine
2686a 20 77 68 65 74 68 65 72 20 77 65 20 73 68 6f 75   whether we shou
2686b 6c 64 20 63 6f 64 65 20 74 68 69 73 20 74 72 69  ld code this tri
2686c 67 67 65 72 20 2a 2f 0a 20 20 20 20 69 66 28 20  gger */.    if( 
2686d 0a 20 20 20 20 20 20 70 2d 3e 6f 70 3d 3d 6f 70  .      p->op==op
2686e 20 26 26 20 0a 20 20 20 20 20 20 70 2d 3e 74 72   && .      p->tr
2686f 5f 74 6d 3d 3d 74 72 5f 74 6d 20 26 26 20 0a 20  _tm==tr_tm && . 
26870 20 20 20 20 20 28 70 2d 3e 70 53 63 68 65 6d 61       (p->pSchema
26871 3d 3d 70 2d 3e 70 54 61 62 53 63 68 65 6d 61 20  ==p->pTabSchema 
26872 7c 7c 20 70 2d 3e 70 53 63 68 65 6d 61 3d 3d 64  || p->pSchema==d
26873 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d  b->aDb[1].pSchem
26874 61 29 20 26 26 0a 20 20 20 20 20 20 28 6f 70 21  a) &&.      (op!
26875 3d 54 4b 5f 55 50 44 41 54 45 7c 7c 21 70 2d 3e  =TK_UPDATE||!p->
26876 70 43 6f 6c 75 6d 6e 73 7c 7c 63 68 65 63 6b 43  pColumns||checkC
26877 6f 6c 75 6d 6e 4f 76 65 72 4c 61 70 28 70 2d 3e  olumnOverLap(p->
26878 70 43 6f 6c 75 6d 6e 73 2c 70 43 68 61 6e 67 65  pColumns,pChange
26879 73 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  s)).    ){.     
2687a 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 2a 70   TriggerStack *p
2687b 53 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74  S;      /* Point
2687c 65 72 20 74 6f 20 74 72 69 67 67 65 72 2d 73 74  er to trigger-st
2687d 61 63 6b 20 65 6e 74 72 79 20 2a 2f 0a 20 20 20  ack entry */.   
2687e 20 20 20 66 6f 72 28 70 53 3d 70 50 61 72 73 65     for(pS=pParse
2687f 2d 3e 74 72 69 67 53 74 61 63 6b 3b 20 70 53 20  ->trigStack; pS 
26880 26 26 20 70 21 3d 70 53 2d 3e 70 54 72 69 67 67  && p!=pS->pTrigg
26881 65 72 3b 20 70 53 3d 70 53 2d 3e 70 4e 65 78 74  er; pS=pS->pNext
26882 29 7b 7d 0a 20 20 20 20 20 20 69 66 28 20 21 70  ){}.      if( !p
26883 53 20 29 7b 0a 20 20 20 20 20 20 20 20 66 69 72  S ){.        fir
26884 65 5f 74 68 69 73 20 3d 20 31 3b 0a 20 20 20 20  e_this = 1;.    
26885 20 20 7d 0a 23 69 66 20 30 20 20 20 20 2f 2a 20    }.#if 0    /* 
26886 47 69 76 65 20 6e 6f 20 77 61 72 6e 69 6e 67 20  Give no warning 
26887 66 6f 72 20 72 65 63 75 72 73 69 76 65 20 74 72  for recursive tr
26888 69 67 67 65 72 73 2e 20 20 4a 75 73 74 20 64 6f  iggers.  Just do
26889 20 6e 6f 74 20 64 6f 20 74 68 65 6d 20 2a 2f 0a   not do them */.
2688a 20 20 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20        else{.    
2688b 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2688c 4d 73 67 28 70 50 61 72 73 65 2c 20 22 72 65 63  Msg(pParse, "rec
2688d 75 72 73 69 76 65 20 74 72 69 67 67 65 72 73 20  ursive triggers 
2688e 6e 6f 74 20 73 75 70 70 6f 72 74 65 64 20 28 25  not supported (%
2688f 73 29 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  s)",.           
26890 20 70 2d 3e 6e 61 6d 65 29 3b 0a 20 20 20 20 20   p->name);.     
26891 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
26892 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d 0a  _ERROR;.      }.
26893 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 0a 20  #endif.    }. . 
26894 20 20 20 69 66 28 20 66 69 72 65 5f 74 68 69 73     if( fire_this
26895 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 6e   ){.      int en
26896 64 54 72 69 67 67 65 72 3b 0a 20 20 20 20 20 20  dTrigger;.      
26897 45 78 70 72 20 2a 20 77 68 65 6e 45 78 70 72 3b  Expr * whenExpr;
26898 0a 20 20 20 20 20 20 41 75 74 68 43 6f 6e 74 65  .      AuthConte
26899 78 74 20 73 43 6f 6e 74 65 78 74 3b 0a 20 20 20  xt sContext;.   
2689a 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73     NameContext s
2689b 4e 43 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  NC;..#ifndef SQL
2689c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20  ITE_OMIT_TRACE. 
2689d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2689e 41 64 64 4f 70 34 28 70 50 61 72 73 65 2d 3e 70  AddOp4(pParse->p
2689f 56 64 62 65 2c 20 4f 50 5f 54 72 61 63 65 2c 20  Vdbe, OP_Trace, 
268a0 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20  0, 0, 0,.       
268a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
268a2 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
268a3 64 62 2c 20 22 2d 2d 20 54 52 49 47 47 45 52 20  db, "-- TRIGGER 
268a4 25 73 22 2c 20 70 2d 3e 6e 61 6d 65 29 2c 0a 20  %s", p->name),. 
268a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
268a6 20 20 20 20 20 20 20 50 34 5f 44 59 4e 41 4d 49         P4_DYNAMI
268a7 43 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20  C);.#endif.     
268a8 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c   memset(&sNC, 0,
268a9 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20   sizeof(sNC));. 
268aa 20 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20       sNC.pParse 
268ab 3d 20 70 50 61 72 73 65 3b 0a 0a 20 20 20 20 20  = pParse;..     
268ac 20 2f 2a 20 50 75 73 68 20 61 6e 20 65 6e 74 72   /* Push an entr
268ad 79 20 6f 6e 20 74 6f 20 74 68 65 20 74 72 69 67  y on to the trig
268ae 67 65 72 20 73 74 61 63 6b 20 2a 2f 0a 20 20 20  ger stack */.   
268af 20 20 20 74 72 69 67 53 74 61 63 6b 45 6e 74 72     trigStackEntr
268b0 79 2e 70 54 72 69 67 67 65 72 20 3d 20 70 3b 0a  y.pTrigger = p;.
268b1 20 20 20 20 20 20 74 72 69 67 53 74 61 63 6b 45        trigStackE
268b2 6e 74 72 79 2e 6e 65 77 49 64 78 20 3d 20 6e 65  ntry.newIdx = ne
268b3 77 49 64 78 3b 0a 20 20 20 20 20 20 74 72 69 67  wIdx;.      trig
268b4 53 74 61 63 6b 45 6e 74 72 79 2e 6f 6c 64 49 64  StackEntry.oldId
268b5 78 20 3d 20 6f 6c 64 49 64 78 3b 0a 20 20 20 20  x = oldIdx;.    
268b6 20 20 74 72 69 67 53 74 61 63 6b 45 6e 74 72 79    trigStackEntry
268b7 2e 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20  .pTab = pTab;.  
268b8 20 20 20 20 74 72 69 67 53 74 61 63 6b 45 6e 74      trigStackEnt
268b9 72 79 2e 70 4e 65 78 74 20 3d 20 70 50 61 72 73  ry.pNext = pPars
268ba 65 2d 3e 74 72 69 67 53 74 61 63 6b 3b 0a 20 20  e->trigStack;.  
268bb 20 20 20 20 74 72 69 67 53 74 61 63 6b 45 6e 74      trigStackEnt
268bc 72 79 2e 69 67 6e 6f 72 65 4a 75 6d 70 20 3d 20  ry.ignoreJump = 
268bd 69 67 6e 6f 72 65 4a 75 6d 70 3b 0a 20 20 20 20  ignoreJump;.    
268be 20 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74    pParse->trigSt
268bf 61 63 6b 20 3d 20 26 74 72 69 67 53 74 61 63 6b  ack = &trigStack
268c0 45 6e 74 72 79 3b 0a 20 20 20 20 20 20 73 71 6c  Entry;.      sql
268c1 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50  ite3AuthContextP
268c2 75 73 68 28 70 50 61 72 73 65 2c 20 26 73 43 6f  ush(pParse, &sCo
268c3 6e 74 65 78 74 2c 20 70 2d 3e 6e 61 6d 65 29 3b  ntext, p->name);
268c4 0a 0a 20 20 20 20 20 20 2f 2a 20 63 6f 64 65 20  ..      /* code 
268c5 74 68 65 20 57 48 45 4e 20 63 6c 61 75 73 65 20  the WHEN clause 
268c6 2a 2f 0a 20 20 20 20 20 20 65 6e 64 54 72 69 67  */.      endTrig
268c7 67 65 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ger = sqlite3Vdb
268c8 65 4d 61 6b 65 4c 61 62 65 6c 28 70 50 61 72 73  eMakeLabel(pPars
268c9 65 2d 3e 70 56 64 62 65 29 3b 0a 20 20 20 20 20  e->pVdbe);.     
268ca 20 77 68 65 6e 45 78 70 72 20 3d 20 73 71 6c 69   whenExpr = sqli
268cb 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
268cc 2d 3e 70 57 68 65 6e 29 3b 0a 20 20 20 20 20 20  ->pWhen);.      
268cd 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
268ce 69 6c 65 64 20 7c 7c 20 73 71 6c 69 74 65 33 52  iled || sqlite3R
268cf 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
268d0 26 73 4e 43 2c 20 77 68 65 6e 45 78 70 72 29 20  &sNC, whenExpr) 
268d1 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73  ){.        pPars
268d2 65 2d 3e 74 72 69 67 53 74 61 63 6b 20 3d 20 74  e->trigStack = t
268d3 72 69 67 53 74 61 63 6b 45 6e 74 72 79 2e 70 4e  rigStackEntry.pN
268d4 65 78 74 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ext;.        sql
268d5 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
268d6 62 2c 20 77 68 65 6e 45 78 70 72 29 3b 0a 20 20  b, whenExpr);.  
268d7 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
268d8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
268d9 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65  lite3ExprIfFalse
268da 28 70 50 61 72 73 65 2c 20 77 68 65 6e 45 78 70  (pParse, whenExp
268db 72 2c 20 65 6e 64 54 72 69 67 67 65 72 2c 20 53  r, endTrigger, S
268dc 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
268dd 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
268de 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 77  ExprDelete(db, w
268df 68 65 6e 45 78 70 72 29 3b 0a 0a 20 20 20 20 20  henExpr);..     
268e0 20 63 6f 64 65 54 72 69 67 67 65 72 50 72 6f 67   codeTriggerProg
268e1 72 61 6d 28 70 50 61 72 73 65 2c 20 70 2d 3e 73  ram(pParse, p->s
268e2 74 65 70 5f 6c 69 73 74 2c 20 6f 72 63 6f 6e 66  tep_list, orconf
268e3 29 3b 20 0a 0a 20 20 20 20 20 20 2f 2a 20 50 6f  ); ..      /* Po
268e4 70 20 74 68 65 20 65 6e 74 72 79 20 6f 66 66 20  p the entry off 
268e5 74 68 65 20 74 72 69 67 67 65 72 20 73 74 61 63  the trigger stac
268e6 6b 20 2a 2f 0a 20 20 20 20 20 20 70 50 61 72 73  k */.      pPars
268e7 65 2d 3e 74 72 69 67 53 74 61 63 6b 20 3d 20 74  e->trigStack = t
268e8 72 69 67 53 74 61 63 6b 45 6e 74 72 79 2e 70 4e  rigStackEntry.pN
268e9 65 78 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ext;.      sqlit
268ea 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70  e3AuthContextPop
268eb 28 26 73 43 6f 6e 74 65 78 74 29 3b 0a 0a 20 20  (&sContext);..  
268ec 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
268ed 65 73 6f 6c 76 65 4c 61 62 65 6c 28 70 50 61 72  esolveLabel(pPar
268ee 73 65 2d 3e 70 56 64 62 65 2c 20 65 6e 64 54 72  se->pVdbe, endTr
268ef 69 67 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20  igger);.    }.  
268f0 7d 0a 20 20 69 66 28 20 70 69 4f 6c 64 43 6f 6c  }.  if( piOldCol
268f1 4d 61 73 6b 20 29 20 2a 70 69 4f 6c 64 43 6f 6c  Mask ) *piOldCol
268f2 4d 61 73 6b 20 7c 3d 20 74 72 69 67 53 74 61 63  Mask |= trigStac
268f3 6b 45 6e 74 72 79 2e 6f 6c 64 43 6f 6c 4d 61 73  kEntry.oldColMas
268f4 6b 3b 0a 20 20 69 66 28 20 70 69 4e 65 77 43 6f  k;.  if( piNewCo
268f5 6c 4d 61 73 6b 20 29 20 2a 70 69 4e 65 77 43 6f  lMask ) *piNewCo
268f6 6c 4d 61 73 6b 20 7c 3d 20 74 72 69 67 53 74 61  lMask |= trigSta
268f7 63 6b 45 6e 74 72 79 2e 6e 65 77 43 6f 6c 4d 61  ckEntry.newColMa
268f8 73 6b 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  sk;.  return 0;.
268f9 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  }.#endif /* !def
268fa 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
268fb 5f 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a 2f 2a  _TRIGGER) */../*
268fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
268fd 64 20 6f 66 20 74 72 69 67 67 65 72 2e 63 20 2a  d of trigger.c *
268fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
268ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
26901 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
26902 67 69 6e 20 66 69 6c 65 20 75 70 64 61 74 65 2e  gin file update.
26903 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
26904 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26905 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
26906 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
26907 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
26908 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
26909 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
2690a 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
2690b 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
2690c 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
2690d 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
2690e 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
2690f 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
26910 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
26911 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
26912 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
26913 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
26914 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
26915 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
26916 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
26917 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
26918 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
26919 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2691a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2691b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2691c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2691d 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
2691e 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64  e contains C cod
2691f 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20  e routines that 
26920 61 72 65 20 63 61 6c 6c 65 64 20 62 79 20 74 68  are called by th
26921 65 20 70 61 72 73 65 72 0a 2a 2a 20 74 6f 20 68  e parser.** to h
26922 61 6e 64 6c 65 20 55 50 44 41 54 45 20 73 74 61  andle UPDATE sta
26923 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 24  tements..**.** $
26924 49 64 3a 20 75 70 64 61 74 65 2e 63 2c 76 20 31  Id: update.c,v 1
26925 2e 31 38 36 20 32 30 30 38 2f 31 30 2f 33 31 20  .186 2008/10/31 
26926 31 30 3a 35 33 3a 32 33 20 64 61 6e 69 65 6c 6b  10:53:23 danielk
26927 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 23  1977 Exp $.*/..#
26928 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
26929 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
2692a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61  /* Forward decla
2692b 72 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63  ration */.static
2692c 20 76 6f 69 64 20 75 70 64 61 74 65 56 69 72 74   void updateVirt
2692d 75 61 6c 54 61 62 6c 65 28 0a 20 20 50 61 72 73  ualTable(.  Pars
2692e 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2692f 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20   /* The parsing 
26930 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63  context */.  Src
26931 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20  List *pSrc,     
26932 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c    /* The virtual
26933 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6d 6f 64   table to be mod
26934 69 66 69 65 64 20 2a 2f 0a 20 20 54 61 62 6c 65  ified */.  Table
26935 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 20   *pTab,         
26936 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 74  /* The virtual t
26937 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69  able */.  ExprLi
26938 73 74 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 2f  st *pChanges,  /
26939 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 74 6f  * The columns to
2693a 20 63 68 61 6e 67 65 20 69 6e 20 74 68 65 20 55   change in the U
2693b 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20  PDATE statement 
2693c 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 6f 77 69  */.  Expr *pRowi
2693d 64 45 78 70 72 2c 20 20 20 20 2f 2a 20 45 78 70  dExpr,    /* Exp
2693e 72 65 73 73 69 6f 6e 20 75 73 65 64 20 74 6f 20  ression used to 
2693f 72 65 63 6f 6d 70 75 74 65 20 74 68 65 20 72 6f  recompute the ro
26940 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 58  wid */.  int *aX
26941 52 65 66 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Ref,          /*
26942 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 63 6f   Mapping from co
26943 6c 75 6d 6e 73 20 6f 66 20 70 54 61 62 20 74 6f  lumns of pTab to
26944 20 65 6e 74 72 69 65 73 20 69 6e 20 70 43 68 61   entries in pCha
26945 6e 67 65 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a  nges */.  Expr *
26946 70 57 68 65 72 65 20 20 20 20 20 20 20 20 20 2f  pWhere         /
26947 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f  * WHERE clause o
26948 66 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61  f the UPDATE sta
26949 74 65 6d 65 6e 74 20 2a 2f 0a 29 3b 0a 23 65 6e  tement */.);.#en
2694a 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2694b 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20  IT_VIRTUALTABLE 
2694c 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 6f  */../*.** The mo
2694d 73 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 64 65  st recently code
2694e 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 61  d instruction wa
2694f 73 20 61 6e 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74  s an OP_Column t
26950 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 0a 2a  o retrieve the.*
26951 2a 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  * i-th column of
26952 20 74 61 62 6c 65 20 70 54 61 62 2e 20 54 68 69   table pTab. Thi
26953 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74  s routine sets t
26954 68 65 20 50 34 20 70 61 72 61 6d 65 74 65 72 20  he P4 parameter 
26955 6f 66 20 74 68 65 20 0a 2a 2a 20 4f 50 5f 43 6f  of the .** OP_Co
26956 6c 75 6d 6e 20 74 6f 20 74 68 65 20 64 65 66 61  lumn to the defa
26957 75 6c 74 20 76 61 6c 75 65 2c 20 69 66 20 61 6e  ult value, if an
26958 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66  y..**.** The def
26959 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 61 20  ault value of a 
2695a 63 6f 6c 75 6d 6e 20 69 73 20 73 70 65 63 69 66  column is specif
2695b 69 65 64 20 62 79 20 61 20 44 45 46 41 55 4c 54  ied by a DEFAULT
2695c 20 63 6c 61 75 73 65 20 69 6e 20 74 68 65 20 0a   clause in the .
2695d 2a 2a 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69  ** column defini
2695e 74 69 6f 6e 2e 20 54 68 69 73 20 77 61 73 20 65  tion. This was e
2695f 69 74 68 65 72 20 73 75 70 70 6c 69 65 64 20 62  ither supplied b
26960 79 20 74 68 65 20 75 73 65 72 20 77 68 65 6e 20  y the user when 
26961 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 77 61 73  the table.** was
26962 20 63 72 65 61 74 65 64 2c 20 6f 72 20 61 64 64   created, or add
26963 65 64 20 6c 61 74 65 72 20 74 6f 20 74 68 65 20  ed later to the 
26964 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e  table definition
26965 20 62 79 20 61 6e 20 41 4c 54 45 52 20 54 41 42   by an ALTER TAB
26966 4c 45 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2e 20 49  LE.** command. I
26967 66 20 74 68 65 20 6c 61 74 74 65 72 2c 20 74 68  f the latter, th
26968 65 6e 20 74 68 65 20 72 6f 77 2d 72 65 63 6f 72  en the row-recor
26969 64 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20  ds in the table 
2696a 62 74 72 65 65 20 6f 6e 20 64 69 73 6b 0a 2a 2a  btree on disk.**
2696b 20 6d 61 79 20 6e 6f 74 20 63 6f 6e 74 61 69 6e   may not contain
2696c 20 61 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65   a value for the
2696d 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 74 68 65 20   column and the 
2696e 64 65 66 61 75 6c 74 20 76 61 6c 75 65 2c 20 74  default value, t
2696f 61 6b 65 6e 0a 2a 2a 20 66 72 6f 6d 20 74 68 65  aken.** from the
26970 20 50 34 20 70 61 72 61 6d 65 74 65 72 20 6f 66   P4 parameter of
26971 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 69   the OP_Column i
26972 6e 73 74 72 75 63 74 69 6f 6e 2c 20 69 73 20 72  nstruction, is r
26973 65 74 75 72 6e 65 64 20 69 6e 73 74 65 61 64 2e  eturned instead.
26974 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 72 6d 65  .** If the forme
26975 72 2c 20 74 68 65 6e 20 61 6c 6c 20 72 6f 77 2d  r, then all row-
26976 72 65 63 6f 72 64 73 20 61 72 65 20 67 75 61 72  records are guar
26977 61 6e 74 65 65 64 20 74 6f 20 69 6e 63 6c 75 64  anteed to includ
26978 65 20 61 20 76 61 6c 75 65 0a 2a 2a 20 66 6f 72  e a value.** for
26979 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 6e 64 20   the column and 
2697a 74 68 65 20 50 34 20 76 61 6c 75 65 20 69 73 20  the P4 value is 
2697b 6e 6f 74 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a  not required..**
2697c 0a 2a 2a 20 43 6f 6c 75 6d 6e 20 64 65 66 69 6e  .** Column defin
2697d 69 74 69 6f 6e 73 20 63 72 65 61 74 65 64 20 62  itions created b
2697e 79 20 61 6e 20 41 4c 54 45 52 20 54 41 42 4c 45  y an ALTER TABLE
2697f 20 63 6f 6d 6d 61 6e 64 20 6d 61 79 20 6f 6e 6c   command may onl
26980 79 20 68 61 76 65 20 0a 2a 2a 20 6c 69 74 65 72  y have .** liter
26981 61 6c 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  al default value
26982 73 20 73 70 65 63 69 66 69 65 64 3a 20 61 20 6e  s specified: a n
26983 75 6d 62 65 72 2c 20 6e 75 6c 6c 20 6f 72 20 61  umber, null or a
26984 20 73 74 72 69 6e 67 2e 20 28 49 66 20 61 20 6d   string. (If a m
26985 6f 72 65 0a 2a 2a 20 63 6f 6d 70 6c 69 63 61 74  ore.** complicat
26986 65 64 20 64 65 66 61 75 6c 74 20 65 78 70 72 65  ed default expre
26987 73 73 69 6f 6e 20 76 61 6c 75 65 20 77 61 73 20  ssion value was 
26988 70 72 6f 76 69 64 65 64 2c 20 69 74 20 69 73 20  provided, it is 
26989 65 76 61 6c 75 61 74 65 64 20 0a 2a 2a 20 77 68  evaluated .** wh
2698a 65 6e 20 74 68 65 20 41 4c 54 45 52 20 54 41 42  en the ALTER TAB
2698b 4c 45 20 69 73 20 65 78 65 63 75 74 65 64 20 61  LE is executed a
2698c 6e 64 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 69  nd one of the li
2698d 74 65 72 61 6c 20 76 61 6c 75 65 73 20 77 72 69  teral values wri
2698e 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 74 68 65  tten.** into the
2698f 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
26990 61 62 6c 65 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65  able.).**.** The
26991 72 65 66 6f 72 65 2c 20 74 68 65 20 50 34 20 70  refore, the P4 p
26992 61 72 61 6d 65 74 65 72 20 69 73 20 6f 6e 6c 79  arameter is only
26993 20 72 65 71 75 69 72 65 64 20 69 66 20 74 68 65   required if the
26994 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66   default value f
26995 6f 72 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e  or.** the column
26996 20 69 73 20 61 20 6c 69 74 65 72 61 6c 20 6e 75   is a literal nu
26997 6d 62 65 72 2c 20 73 74 72 69 6e 67 20 6f 72 20  mber, string or 
26998 6e 75 6c 6c 2e 20 54 68 65 20 73 71 6c 69 74 65  null. The sqlite
26999 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 29  3ValueFromExpr()
2699a 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  .** function is 
2699b 63 61 70 61 62 6c 65 20 6f 66 20 74 72 61 6e 73  capable of trans
2699c 66 6f 72 6d 69 6e 67 20 74 68 65 73 65 20 74 79  forming these ty
2699d 70 65 73 20 6f 66 20 65 78 70 72 65 73 73 69 6f  pes of expressio
2699e 6e 73 20 69 6e 74 6f 0a 2a 2a 20 73 71 6c 69 74  ns into.** sqlit
2699f 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73  e3_value objects
269a0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
269a1 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
269a2 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 56 64  ColumnDefault(Vd
269a3 62 65 20 2a 76 2c 20 54 61 62 6c 65 20 2a 70 54  be *v, Table *pT
269a4 61 62 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 66  ab, int i){.  if
269a5 28 20 70 54 61 62 20 26 26 20 21 70 54 61 62 2d  ( pTab && !pTab-
269a6 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20  >pSelect ){.    
269a7 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
269a8 56 61 6c 75 65 3b 0a 20 20 20 20 75 38 20 65 6e  Value;.    u8 en
269a9 63 20 3d 20 45 4e 43 28 73 71 6c 69 74 65 33 56  c = ENC(sqlite3V
269aa 64 62 65 44 62 28 76 29 29 3b 0a 20 20 20 20 43  dbeDb(v));.    C
269ab 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70  olumn *pCol = &p
269ac 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a 20 20  Tab->aCol[i];.  
269ad 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
269ae 2c 20 22 25 73 2e 25 73 22 2c 20 70 54 61 62 2d  , "%s.%s", pTab-
269af 3e 7a 4e 61 6d 65 2c 20 70 43 6f 6c 2d 3e 7a 4e  >zName, pCol->zN
269b0 61 6d 65 29 29 3b 0a 20 20 20 20 61 73 73 65 72  ame));.    asser
269b1 74 28 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20  t( i<pTab->nCol 
269b2 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61  );.    sqlite3Va
269b3 6c 75 65 46 72 6f 6d 45 78 70 72 28 73 71 6c 69  lueFromExpr(sqli
269b4 74 65 33 56 64 62 65 44 62 28 76 29 2c 20 70 43  te3VdbeDb(v), pC
269b5 6f 6c 2d 3e 70 44 66 6c 74 2c 20 65 6e 63 2c 20  ol->pDflt, enc, 
269b6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
269b7 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e            pCol->
269b8 61 66 66 69 6e 69 74 79 2c 20 26 70 56 61 6c 75  affinity, &pValu
269b9 65 29 3b 0a 20 20 20 20 69 66 28 20 70 56 61 6c  e);.    if( pVal
269ba 75 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ue ){.      sqli
269bb 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
269bc 76 2c 20 2d 31 2c 20 28 63 6f 6e 73 74 20 63 68  v, -1, (const ch
269bd 61 72 20 2a 29 70 56 61 6c 75 65 2c 20 50 34 5f  ar *)pValue, P4_
269be 4d 45 4d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  MEM);.    }.  }.
269bf 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73  }../*.** Process
269c0 20 61 6e 20 55 50 44 41 54 45 20 73 74 61 74 65   an UPDATE state
269c1 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 55 50  ment..**.**   UP
269c2 44 41 54 45 20 4f 52 20 49 47 4e 4f 52 45 20 74  DATE OR IGNORE t
269c3 61 62 6c 65 5f 77 78 79 7a 20 53 45 54 20 61 3d  able_wxyz SET a=
269c4 62 2c 20 63 3d 64 20 57 48 45 52 45 20 65 3c 35  b, c=d WHERE e<5
269c5 20 41 4e 44 20 66 20 4e 4f 54 20 4e 55 4c 4c 3b   AND f NOT NULL;
269c6 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5c 5f 5f  .**          \__
269c7 5f 5f 5f 5f 5f 2f 20 5c 5f 5f 5f 5f 5f 5f 5f 5f  _____/ \________
269c8 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 2f 20 20  /     \______/  
269c9 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f       \__________
269ca 5f 5f 5f 5f 5f 5f 2f 0a 2a 20 20 20 20 20 20 20  ______/.*       
269cb 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 20 20 70       onError   p
269cc 54 61 62 4c 69 73 74 20 20 20 20 20 20 70 43 68  TabList      pCh
269cd 61 6e 67 65 73 20 20 20 20 20 20 20 20 20 20 20  anges           
269ce 20 20 70 57 68 65 72 65 0a 2a 2f 0a 53 51 4c 49    pWhere.*/.SQLI
269cf 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
269d0 73 71 6c 69 74 65 33 55 70 64 61 74 65 28 0a 20  sqlite3Update(. 
269d1 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
269d2 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
269d3 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
269d4 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62  .  SrcList *pTab
269d5 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20 54 68 65  List,     /* The
269d6 20 74 61 62 6c 65 20 69 6e 20 77 68 69 63 68 20   table in which 
269d7 77 65 20 73 68 6f 75 6c 64 20 63 68 61 6e 67 65  we should change
269d8 20 74 68 69 6e 67 73 20 2a 2f 0a 20 20 45 78 70   things */.  Exp
269d9 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65 73 2c  rList *pChanges,
269da 20 20 20 20 2f 2a 20 54 68 69 6e 67 73 20 74 6f      /* Things to
269db 20 62 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20   be changed */. 
269dc 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20   Expr *pWhere,  
269dd 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57          /* The W
269de 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 61  HERE clause.  Ma
269df 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 69  y be null */.  i
269e0 6e 74 20 6f 6e 45 72 72 6f 72 20 20 20 20 20 20  nt onError      
269e1 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20        /* How to 
269e2 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61 69 6e  handle constrain
269e3 74 20 65 72 72 6f 72 73 20 2a 2f 0a 29 7b 0a 20  t errors */.){. 
269e4 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20   int i, j;      
269e5 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
269e6 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 54 61  counters */.  Ta
269e7 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20  ble *pTab;      
269e8 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
269e9 65 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20  e to be updated 
269ea 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 20 3d 20  */.  int addr = 
269eb 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56  0;          /* V
269ec 44 42 45 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  DBE instruction 
269ed 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 73  address of the s
269ee 74 61 72 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70  tart of the loop
269ef 20 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f 20   */.  WhereInfo 
269f0 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20  *pWInfo;     /* 
269f1 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  Information abou
269f2 74 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  t the WHERE clau
269f3 73 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b  se */.  Vdbe *v;
269f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
269f5 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 64 61  * The virtual da
269f6 74 61 62 61 73 65 20 65 6e 67 69 6e 65 20 2a 2f  tabase engine */
269f7 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20  .  Index *pIdx; 
269f8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
269f9 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69 6e   looping over in
269fa 64 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  dices */.  int n
269fb 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  Idx;            
269fc 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69    /* Number of i
269fd 6e 64 69 63 65 73 20 74 68 61 74 20 6e 65 65 64  ndices that need
269fe 20 75 70 64 61 74 69 6e 67 20 2a 2f 0a 20 20 69   updating */.  i
269ff 6e 74 20 69 43 75 72 3b 20 20 20 20 20 20 20 20  nt iCur;        
26a00 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75        /* VDBE Cu
26a01 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 70  rsor number of p
26a02 54 61 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  Tab */.  sqlite3
26a03 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20   *db;           
26a04 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
26a05 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69  structure */.  i
26a06 6e 74 20 2a 61 52 65 67 49 64 78 20 3d 20 30 3b  nt *aRegIdx = 0;
26a07 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 72 65 67        /* One reg
26a08 69 73 74 65 72 20 61 73 73 69 67 6e 65 64 20 74  ister assigned t
26a09 6f 20 65 61 63 68 20 69 6e 64 65 78 20 74 6f 20  o each index to 
26a0a 62 65 20 75 70 64 61 74 65 64 20 2a 2f 0a 20 20  be updated */.  
26a0b 69 6e 74 20 2a 61 58 52 65 66 20 3d 20 30 3b 20  int *aXRef = 0; 
26a0c 20 20 20 20 20 20 20 2f 2a 20 61 58 52 65 66 5b         /* aXRef[
26a0d 69 5d 20 69 73 20 74 68 65 20 69 6e 64 65 78 20  i] is the index 
26a0e 69 6e 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 5d  in pChanges->a[]
26a0f 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 20   of the.        
26a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26a11 20 2a 2a 20 61 6e 20 65 78 70 72 65 73 73 69 6f   ** an expressio
26a12 6e 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 63  n for the i-th c
26a13 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 62  olumn of the tab
26a14 6c 65 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20  le..            
26a15 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
26a16 61 58 52 65 66 5b 69 5d 3d 3d 2d 31 20 69 66 20  aXRef[i]==-1 if 
26a17 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20  the i-th column 
26a18 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 2e 20  is not changed. 
26a19 2a 2f 0a 20 20 69 6e 74 20 63 68 6e 67 52 6f 77  */.  int chngRow
26a1a 69 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  id;         /* T
26a1b 72 75 65 20 69 66 20 74 68 65 20 72 65 63 6f 72  rue if the recor
26a1c 64 20 6e 75 6d 62 65 72 20 69 73 20 62 65 69 6e  d number is bein
26a1d 67 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 45  g changed */.  E
26a1e 78 70 72 20 2a 70 52 6f 77 69 64 45 78 70 72 20  xpr *pRowidExpr 
26a1f 3d 20 30 3b 20 20 2f 2a 20 45 78 70 72 65 73 73  = 0;  /* Express
26a20 69 6f 6e 20 64 65 66 69 6e 69 6e 67 20 74 68 65  ion defining the
26a21 20 6e 65 77 20 72 65 63 6f 72 64 20 6e 75 6d 62   new record numb
26a22 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e  er */.  int open
26a23 41 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 2f  All = 0;       /
26a24 2a 20 54 72 75 65 20 69 66 20 61 6c 6c 20 69 6e  * True if all in
26a25 64 69 63 65 73 20 6e 65 65 64 20 74 6f 20 62 65  dices need to be
26a26 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 41 75 74   opened */.  Aut
26a27 68 43 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78  hContext sContex
26a28 74 3b 20 20 2f 2a 20 54 68 65 20 61 75 74 68 6f  t;  /* The autho
26a29 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74  rization context
26a2a 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78   */.  NameContex
26a2b 74 20 73 4e 43 3b 20 20 20 20 20 20 20 2f 2a 20  t sNC;       /* 
26a2c 54 68 65 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74  The name-context
26a2d 20 74 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 72   to resolve expr
26a2e 65 73 73 69 6f 6e 73 20 69 6e 20 2a 2f 0a 20 20  essions in */.  
26a2f 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
26a30 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
26a31 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  se containing th
26a32 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 75 70  e table being up
26a33 64 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6a  dated */.  int j
26a34 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
26a35 20 20 2f 2a 20 41 64 64 72 65 73 73 65 73 20 6f    /* Addresses o
26a36 66 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69  f jump instructi
26a37 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 6b 4f  ons */.  int okO
26a38 6e 65 50 61 73 73 3b 20 20 20 20 20 20 20 20 20  nePass;         
26a39 2f 2a 20 54 72 75 65 20 66 6f 72 20 6f 6e 65 2d  /* True for one-
26a3a 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 20 77  pass algorithm w
26a3b 69 74 68 6f 75 74 20 74 68 65 20 46 49 46 4f 20  ithout the FIFO 
26a3c 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
26a3d 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
26a3e 20 20 69 6e 74 20 69 73 56 69 65 77 3b 20 20 20    int isView;   
26a3f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26a40 2a 20 54 72 79 69 6e 67 20 74 6f 20 75 70 64 61  * Trying to upda
26a41 74 65 20 61 20 76 69 65 77 20 2a 2f 0a 20 20 69  te a view */.  i
26a42 6e 74 20 74 72 69 67 67 65 72 73 5f 65 78 69 73  nt triggers_exis
26a43 74 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 54  t = 0;      /* T
26a44 72 75 65 20 69 66 20 61 6e 79 20 72 6f 77 20 74  rue if any row t
26a45 72 69 67 67 65 72 73 20 65 78 69 73 74 20 2a 2f  riggers exist */
26a46 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 69 42  .#endif.  int iB
26a47 65 67 69 6e 41 66 74 65 72 54 72 69 67 67 65 72  eginAfterTrigger
26a48 3b 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73  ;      /* Addres
26a49 73 20 6f 66 20 61 66 74 65 72 20 74 72 69 67 67  s of after trigg
26a4a 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20  er program */.  
26a4b 69 6e 74 20 69 45 6e 64 41 66 74 65 72 54 72 69  int iEndAfterTri
26a4c 67 67 65 72 3b 20 20 20 20 20 20 20 20 2f 2a 20  gger;        /* 
26a4d 45 78 69 74 20 6f 66 20 61 66 74 65 72 20 74 72  Exit of after tr
26a4e 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f  igger program */
26a4f 0a 20 20 69 6e 74 20 69 42 65 67 69 6e 42 65 66  .  int iBeginBef
26a50 6f 72 65 54 72 69 67 67 65 72 3b 20 20 20 20 20  oreTrigger;     
26a51 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 62 65  /* Address of be
26a52 66 6f 72 65 20 74 72 69 67 67 65 72 20 70 72 6f  fore trigger pro
26a53 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 45  gram */.  int iE
26a54 6e 64 42 65 66 6f 72 65 54 72 69 67 67 65 72 3b  ndBeforeTrigger;
26a55 20 20 20 20 20 20 20 2f 2a 20 45 78 69 74 20 6f         /* Exit o
26a56 66 20 62 65 66 6f 72 65 20 74 72 69 67 67 65 72  f before trigger
26a57 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 75 33   program */.  u3
26a58 32 20 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 20 3d  2 old_col_mask =
26a59 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 61   0;        /* Ma
26a5a 73 6b 20 6f 66 20 4f 4c 44 2e 2a 20 63 6f 6c 75  sk of OLD.* colu
26a5b 6d 6e 73 20 69 6e 20 75 73 65 20 2a 2f 0a 20 20  mns in use */.  
26a5c 75 33 32 20 6e 65 77 5f 63 6f 6c 5f 6d 61 73 6b  u32 new_col_mask
26a5d 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
26a5e 4d 61 73 6b 20 6f 66 20 4e 45 57 2e 2a 20 63 6f  Mask of NEW.* co
26a5f 6c 75 6d 6e 73 20 69 6e 20 75 73 65 20 2a 2f 0a  lumns in use */.
26a60 0a 20 20 69 6e 74 20 6e 65 77 49 64 78 20 20 20  .  int newIdx   
26a61 20 20 20 3d 20 2d 31 3b 20 20 2f 2a 20 69 6e 64     = -1;  /* ind
26a62 65 78 20 6f 66 20 74 72 69 67 67 65 72 20 22 6e  ex of trigger "n
26a63 65 77 22 20 74 65 6d 70 20 74 61 62 6c 65 20 20  ew" temp table  
26a64 20 20 20 20 20 2a 2f 0a 20 20 69 6e 74 20 6f 6c       */.  int ol
26a65 64 49 64 78 20 20 20 20 20 20 3d 20 2d 31 3b 20  dIdx      = -1; 
26a66 20 2f 2a 20 69 6e 64 65 78 20 6f 66 20 74 72 69   /* index of tri
26a67 67 67 65 72 20 22 6f 6c 64 22 20 74 65 6d 70 20  gger "old" temp 
26a68 74 61 62 6c 65 20 20 20 20 20 20 20 2a 2f 0a 0a  table       */..
26a69 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 41 6c    /* Register Al
26a6a 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69  locations */.  i
26a6b 6e 74 20 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d  nt regRowCount =
26a6c 20 30 3b 20 20 20 2f 2a 20 41 20 63 6f 75 6e 74   0;   /* A count
26a6d 20 6f 66 20 72 6f 77 73 20 63 68 61 6e 67 65 64   of rows changed
26a6e 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4f 6c 64   */.  int regOld
26a6f 52 6f 77 69 64 3b 20 20 20 20 20 20 20 2f 2a 20  Rowid;       /* 
26a70 54 68 65 20 6f 6c 64 20 72 6f 77 69 64 20 2a 2f  The old rowid */
26a71 0a 20 20 69 6e 74 20 72 65 67 4e 65 77 52 6f 77  .  int regNewRow
26a72 69 64 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  id;       /* The
26a73 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20   new rowid */.  
26a74 69 6e 74 20 72 65 67 44 61 74 61 3b 20 20 20 20  int regData;    
26a75 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 64 61         /* New da
26a76 74 61 20 66 6f 72 20 74 68 65 20 72 6f 77 20 2a  ta for the row *
26a77 2f 0a 0a 20 20 73 43 6f 6e 74 65 78 74 2e 70 50  /..  sContext.pP
26a78 61 72 73 65 20 3d 20 30 3b 0a 20 20 64 62 20 3d  arse = 0;.  db =
26a79 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
26a7a 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20  f( pParse->nErr 
26a7b 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
26a7c 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  led ){.    goto 
26a7d 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
26a7e 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54    }.  assert( pT
26a7f 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20  abList->nSrc==1 
26a80 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20  );..  /* Locate 
26a81 74 68 65 20 74 61 62 6c 65 20 77 68 69 63 68 20  the table which 
26a82 77 65 20 77 61 6e 74 20 74 6f 20 75 70 64 61 74  we want to updat
26a83 65 2e 20 0a 20 20 2a 2f 0a 20 20 70 54 61 62 20  e. .  */.  pTab 
26a84 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  = sqlite3SrcList
26a85 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70  Lookup(pParse, p
26a86 54 61 62 4c 69 73 74 29 3b 0a 20 20 69 66 28 20  TabList);.  if( 
26a87 70 54 61 62 3d 3d 30 20 29 20 67 6f 74 6f 20 75  pTab==0 ) goto u
26a88 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  pdate_cleanup;. 
26a89 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
26a8a 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72  hemaToIndex(pPar
26a8b 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53  se->db, pTab->pS
26a8c 63 68 65 6d 61 29 3b 0a 0a 20 20 2f 2a 20 46 69  chema);..  /* Fi
26a8d 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20 68  gure out if we h
26a8e 61 76 65 20 61 6e 79 20 74 72 69 67 67 65 72 73  ave any triggers
26a8f 20 61 6e 64 20 69 66 20 74 68 65 20 74 61 62 6c   and if the tabl
26a90 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 75 70 64  e being.  ** upd
26a91 61 74 65 64 20 69 73 20 61 20 76 69 65 77 0a 20  ated is a view. 
26a92 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
26a93 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
26a94 20 20 74 72 69 67 67 65 72 73 5f 65 78 69 73 74    triggers_exist
26a95 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65   = sqlite3Trigge
26a96 72 73 45 78 69 73 74 28 70 50 61 72 73 65 2c 20  rsExist(pParse, 
26a97 70 54 61 62 2c 20 54 4b 5f 55 50 44 41 54 45 2c  pTab, TK_UPDATE,
26a98 20 70 43 68 61 6e 67 65 73 29 3b 0a 20 20 69 73   pChanges);.  is
26a99 56 69 65 77 20 3d 20 70 54 61 62 2d 3e 70 53 65  View = pTab->pSe
26a9a 6c 65 63 74 21 3d 30 3b 0a 23 65 6c 73 65 0a 23  lect!=0;.#else.#
26a9b 20 64 65 66 69 6e 65 20 74 72 69 67 67 65 72 73   define triggers
26a9c 5f 65 78 69 73 74 20 30 0a 23 20 64 65 66 69 6e  _exist 0.# defin
26a9d 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69  e isView 0.#endi
26a9e 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
26a9f 4f 4d 49 54 5f 56 49 45 57 0a 23 20 75 6e 64 65  OMIT_VIEW.# unde
26aa0 66 20 69 73 56 69 65 77 0a 23 20 64 65 66 69 6e  f isView.# defin
26aa1 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69  e isView 0.#endi
26aa2 66 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  f..  if( sqlite3
26aa3 49 73 52 65 61 64 4f 6e 6c 79 28 70 50 61 72 73  IsReadOnly(pPars
26aa4 65 2c 20 70 54 61 62 2c 20 74 72 69 67 67 65 72  e, pTab, trigger
26aa5 73 5f 65 78 69 73 74 29 20 29 7b 0a 20 20 20 20  s_exist) ){.    
26aa6 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61  goto update_clea
26aa7 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  nup;.  }.  if( s
26aa8 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c  qlite3ViewGetCol
26aa9 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c  umnNames(pParse,
26aaa 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f   pTab) ){.    go
26aab 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75  to update_cleanu
26aac 70 3b 0a 20 20 7d 0a 20 20 61 58 52 65 66 20 3d  p;.  }.  aXRef =
26aad 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
26aae 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69  Raw(db, sizeof(i
26aaf 6e 74 29 20 2a 20 70 54 61 62 2d 3e 6e 43 6f 6c  nt) * pTab->nCol
26ab0 20 29 3b 0a 20 20 69 66 28 20 61 58 52 65 66 3d   );.  if( aXRef=
26ab1 3d 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65  =0 ) goto update
26ab2 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 66 6f 72 28  _cleanup;.  for(
26ab3 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f  i=0; i<pTab->nCo
26ab4 6c 3b 20 69 2b 2b 29 20 61 58 52 65 66 5b 69 5d  l; i++) aXRef[i]
26ab5 20 3d 20 2d 31 3b 0a 0a 20 20 2f 2a 20 49 66 20   = -1;..  /* If 
26ab6 74 68 65 72 65 20 61 72 65 20 46 4f 52 20 45 41  there are FOR EA
26ab7 43 48 20 52 4f 57 20 74 72 69 67 67 65 72 73 2c  CH ROW triggers,
26ab8 20 61 6c 6c 6f 63 61 74 65 20 63 75 72 73 6f 72   allocate cursor
26ab9 73 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 73  s for the.  ** s
26aba 70 65 63 69 61 6c 20 4f 4c 44 20 61 6e 64 20 4e  pecial OLD and N
26abb 45 57 20 74 61 62 6c 65 73 0a 20 20 2a 2f 0a 20  EW tables.  */. 
26abc 20 69 66 28 20 74 72 69 67 67 65 72 73 5f 65 78   if( triggers_ex
26abd 69 73 74 20 29 7b 0a 20 20 20 20 6e 65 77 49 64  ist ){.    newId
26abe 78 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  x = pParse->nTab
26abf 2b 2b 3b 0a 20 20 20 20 6f 6c 64 49 64 78 20 3d  ++;.    oldIdx =
26ac0 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
26ac1 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  .  }..  /* Alloc
26ac2 61 74 65 20 61 20 63 75 72 73 6f 72 73 20 66 6f  ate a cursors fo
26ac3 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  r the main datab
26ac4 61 73 65 20 74 61 62 6c 65 20 61 6e 64 20 66 6f  ase table and fo
26ac5 72 20 61 6c 6c 20 69 6e 64 69 63 65 73 2e 0a 20  r all indices.. 
26ac6 20 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 63 75   ** The index cu
26ac7 72 73 6f 72 73 20 6d 69 67 68 74 20 6e 6f 74 20  rsors might not 
26ac8 62 65 20 75 73 65 64 2c 20 62 75 74 20 69 66 20  be used, but if 
26ac9 74 68 65 79 20 61 72 65 20 75 73 65 64 20 74 68  they are used th
26aca 65 79 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20  ey.  ** need to 
26acb 6f 63 63 75 72 20 72 69 67 68 74 20 61 66 74 65  occur right afte
26acc 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  r the database c
26acd 75 72 73 6f 72 2e 20 20 53 6f 20 67 6f 20 61 68  ursor.  So go ah
26ace 65 61 64 20 61 6e 64 0a 20 20 2a 2a 20 61 6c 6c  ead and.  ** all
26acf 6f 63 61 74 65 20 65 6e 6f 75 67 68 20 73 70 61  ocate enough spa
26ad0 63 65 2c 20 6a 75 73 74 20 69 6e 20 63 61 73 65  ce, just in case
26ad1 2e 0a 20 20 2a 2f 0a 20 20 70 54 61 62 4c 69 73  ..  */.  pTabLis
26ad2 74 2d 3e 61 5b 30 5d 2e 69 43 75 72 73 6f 72 20  t->a[0].iCursor 
26ad3 3d 20 69 43 75 72 20 3d 20 70 50 61 72 73 65 2d  = iCur = pParse-
26ad4 3e 6e 54 61 62 2b 2b 3b 0a 20 20 66 6f 72 28 70  >nTab++;.  for(p
26ad5 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78  Idx=pTab->pIndex
26ad6 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64  ; pIdx; pIdx=pId
26ad7 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 70  x->pNext){.    p
26ad8 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
26ad9 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c   }..  /* Initial
26ada 69 7a 65 20 74 68 65 20 6e 61 6d 65 2d 63 6f 6e  ize the name-con
26adb 74 65 78 74 20 2a 2f 0a 20 20 6d 65 6d 73 65 74  text */.  memset
26adc 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66  (&sNC, 0, sizeof
26add 28 73 4e 43 29 29 3b 0a 20 20 73 4e 43 2e 70 50  (sNC));.  sNC.pP
26ade 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20  arse = pParse;. 
26adf 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20   sNC.pSrcList = 
26ae0 70 54 61 62 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20  pTabList;..  /* 
26ae1 52 65 73 6f 6c 76 65 20 74 68 65 20 63 6f 6c 75  Resolve the colu
26ae2 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20  mn names in all 
26ae3 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  the expressions 
26ae4 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6f 66 20 74  of the.  ** of t
26ae5 68 65 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  he UPDATE statem
26ae6 65 6e 74 2e 20 20 41 6c 73 6f 20 66 69 6e 64 20  ent.  Also find 
26ae7 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78  the column index
26ae8 0a 20 20 2a 2a 20 66 6f 72 20 65 61 63 68 20 63  .  ** for each c
26ae9 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 75 70 64 61  olumn to be upda
26aea 74 65 64 20 69 6e 20 74 68 65 20 70 43 68 61 6e  ted in the pChan
26aeb 67 65 73 20 61 72 72 61 79 2e 20 20 46 6f 72 20  ges array.  For 
26aec 65 61 63 68 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e  each.  ** column
26aed 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 2c 20   to be updated, 
26aee 6d 61 6b 65 20 73 75 72 65 20 77 65 20 68 61 76  make sure we hav
26aef 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  e authorization 
26af0 74 6f 20 63 68 61 6e 67 65 0a 20 20 2a 2a 20 74  to change.  ** t
26af1 68 61 74 20 63 6f 6c 75 6d 6e 2e 0a 20 20 2a 2f  hat column..  */
26af2 0a 20 20 63 68 6e 67 52 6f 77 69 64 20 3d 20 30  .  chngRowid = 0
26af3 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
26af4 43 68 61 6e 67 65 73 2d 3e 6e 45 78 70 72 3b 20  Changes->nExpr; 
26af5 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71  i++){.    if( sq
26af6 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
26af7 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 43 68 61  Names(&sNC, pCha
26af8 6e 67 65 73 2d 3e 61 5b 69 5d 2e 70 45 78 70 72  nges->a[i].pExpr
26af9 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ) ){.      goto 
26afa 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
26afb 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 6a 3d      }.    for(j=
26afc 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  0; j<pTab->nCol;
26afd 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   j++){.      if(
26afe 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
26aff 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e  pTab->aCol[j].zN
26b00 61 6d 65 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61  ame, pChanges->a
26b01 5b 69 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b  [i].zName)==0 ){
26b02 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a 3d 3d  .        if( j==
26b03 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20  pTab->iPKey ){. 
26b04 20 20 20 20 20 20 20 20 20 63 68 6e 67 52 6f 77           chngRow
26b05 69 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  id = 1;.        
26b06 20 20 70 52 6f 77 69 64 45 78 70 72 20 3d 20 70    pRowidExpr = p
26b07 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 70 45  Changes->a[i].pE
26b08 78 70 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  xpr;.        }. 
26b09 20 20 20 20 20 20 20 61 58 52 65 66 5b 6a 5d 20         aXRef[j] 
26b0a 3d 20 69 3b 0a 20 20 20 20 20 20 20 20 62 72 65  = i;.        bre
26b0b 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
26b0c 7d 0a 20 20 20 20 69 66 28 20 6a 3e 3d 70 54 61  }.    if( j>=pTa
26b0d 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20  b->nCol ){.     
26b0e 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 52 6f   if( sqlite3IsRo
26b0f 77 69 64 28 70 43 68 61 6e 67 65 73 2d 3e 61 5b  wid(pChanges->a[
26b10 69 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20  i].zName) ){.   
26b11 20 20 20 20 20 63 68 6e 67 52 6f 77 69 64 20 3d       chngRowid =
26b12 20 31 3b 0a 20 20 20 20 20 20 20 20 70 52 6f 77   1;.        pRow
26b13 69 64 45 78 70 72 20 3d 20 70 43 68 61 6e 67 65  idExpr = pChange
26b14 73 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20  s->a[i].pExpr;. 
26b15 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
26b16 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
26b17 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20  Msg(pParse, "no 
26b18 73 75 63 68 20 63 6f 6c 75 6d 6e 3a 20 25 73 22  such column: %s"
26b19 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d  , pChanges->a[i]
26b1a 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20  .zName);.       
26b1b 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65   goto update_cle
26b1c 61 6e 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20  anup;.      }.  
26b1d 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
26b1e 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
26b1f 41 54 49 4f 4e 0a 20 20 20 20 7b 0a 20 20 20 20  ATION.    {.    
26b20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 20    int rc;.      
26b21 72 63 20 3d 20 73 71 6c 69 74 65 33 41 75 74 68  rc = sqlite3Auth
26b22 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51  Check(pParse, SQ
26b23 4c 49 54 45 5f 55 50 44 41 54 45 2c 20 70 54 61  LITE_UPDATE, pTa
26b24 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20  b->zName,.      
26b25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26b26 20 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b       pTab->aCol[
26b27 6a 5d 2e 7a 4e 61 6d 65 2c 20 64 62 2d 3e 61 44  j].zName, db->aD
26b28 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  b[iDb].zName);. 
26b29 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
26b2a 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20  ITE_DENY ){.    
26b2b 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f      goto update_
26b2c 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 7d  cleanup;.      }
26b2d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c  else if( rc==SQL
26b2e 49 54 45 5f 49 47 4e 4f 52 45 20 29 7b 0a 20 20  ITE_IGNORE ){.  
26b2f 20 20 20 20 20 20 61 58 52 65 66 5b 6a 5d 20 3d        aXRef[j] =
26b30 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   -1;.      }.   
26b31 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20   }.#endif.  }.. 
26b32 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d   /* Allocate mem
26b33 6f 72 79 20 66 6f 72 20 74 68 65 20 61 72 72 61  ory for the arra
26b34 79 20 61 52 65 67 49 64 78 5b 5d 2e 20 20 54 68  y aRegIdx[].  Th
26b35 65 72 65 20 69 73 20 6f 6e 65 20 65 6e 74 72 79  ere is one entry
26b36 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 61 72 72   in the.  ** arr
26b37 61 79 20 66 6f 72 20 65 61 63 68 20 69 6e 64 65  ay for each inde
26b38 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  x associated wit
26b39 68 20 74 61 62 6c 65 20 62 65 69 6e 67 20 75 70  h table being up
26b3a 64 61 74 65 64 2e 20 20 46 69 6c 6c 20 69 6e 0a  dated.  Fill in.
26b3b 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 77    ** the value w
26b3c 69 74 68 20 61 20 72 65 67 69 73 74 65 72 20 6e  ith a register n
26b3d 75 6d 62 65 72 20 66 6f 72 20 69 6e 64 69 63 65  umber for indice
26b3e 73 20 74 68 61 74 20 61 72 65 20 74 6f 20 62 65  s that are to be
26b3f 20 75 73 65 64 0a 20 20 2a 2a 20 61 6e 64 20 77   used.  ** and w
26b40 69 74 68 20 7a 65 72 6f 20 66 6f 72 20 75 6e 75  ith zero for unu
26b41 73 65 64 20 69 6e 64 69 63 65 73 2e 0a 20 20 2a  sed indices..  *
26b42 2f 0a 20 20 66 6f 72 28 6e 49 64 78 3d 30 2c 20  /.  for(nIdx=0, 
26b43 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65  pIdx=pTab->pInde
26b44 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49  x; pIdx; pIdx=pI
26b45 64 78 2d 3e 70 4e 65 78 74 2c 20 6e 49 64 78 2b  dx->pNext, nIdx+
26b46 2b 29 7b 7d 0a 20 20 69 66 28 20 6e 49 64 78 3e  +){}.  if( nIdx>
26b47 30 20 29 7b 0a 20 20 20 20 61 52 65 67 49 64 78  0 ){.    aRegIdx
26b48 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
26b49 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66  ocRaw(db, sizeof
26b4a 28 49 6e 64 65 78 2a 29 20 2a 20 6e 49 64 78 20  (Index*) * nIdx 
26b4b 29 3b 0a 20 20 20 20 69 66 28 20 61 52 65 67 49  );.    if( aRegI
26b4c 64 78 3d 3d 30 20 29 20 67 6f 74 6f 20 75 70 64  dx==0 ) goto upd
26b4d 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ate_cleanup;.  }
26b4e 0a 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 64 78  .  for(j=0, pIdx
26b4f 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
26b50 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
26b51 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a 20 20 20  pNext, j++){.   
26b52 20 69 6e 74 20 72 65 67 3b 0a 20 20 20 20 69 66   int reg;.    if
26b53 28 20 63 68 6e 67 52 6f 77 69 64 20 29 7b 0a 20  ( chngRowid ){. 
26b54 20 20 20 20 20 72 65 67 20 3d 20 2b 2b 70 50 61       reg = ++pPa
26b55 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 7d  rse->nMem;.    }
26b56 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 67 20  else{.      reg 
26b57 3d 20 30 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  = 0;.      for(i
26b58 3d 30 3b 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c  =0; i<pIdx->nCol
26b59 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  umn; i++){.     
26b5a 20 20 20 69 66 28 20 61 58 52 65 66 5b 70 49 64     if( aXRef[pId
26b5b 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 5d 3e  x->aiColumn[i]]>
26b5c 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
26b5d 72 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  reg = ++pParse->
26b5e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20  nMem;.          
26b5f 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
26b60 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
26b61 20 20 20 61 52 65 67 49 64 78 5b 6a 5d 20 3d 20     aRegIdx[j] = 
26b62 72 65 67 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41  reg;.  }..  /* A
26b63 6c 6c 6f 63 61 74 65 20 61 20 62 6c 6f 63 6b 20  llocate a block 
26b64 6f 66 20 72 65 67 69 73 74 65 72 20 75 73 65 64  of register used
26b65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63 68   to store the ch
26b66 61 6e 67 65 20 72 65 63 6f 72 64 0a 20 20 2a 2a  ange record.  **
26b67 20 73 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33   sent to sqlite3
26b68 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69  GenerateConstrai
26b69 6e 74 43 68 65 63 6b 73 28 29 2e 20 20 54 68 65  ntChecks().  The
26b6a 72 65 20 61 72 65 20 65 69 74 68 65 72 0a 20 20  re are either.  
26b6b 2a 2a 20 6f 6e 65 20 6f 72 20 74 77 6f 20 72 65  ** one or two re
26b6c 67 69 73 74 65 72 73 20 66 6f 72 20 68 6f 6c 64  gisters for hold
26b6d 69 6e 67 20 74 68 65 20 72 6f 77 69 64 2e 20 20  ing the rowid.  
26b6e 4f 6e 65 20 72 6f 77 69 64 20 72 65 67 69 73 74  One rowid regist
26b6f 65 72 0a 20 20 2a 2a 20 69 73 20 75 73 65 64 20  er.  ** is used 
26b70 69 66 20 63 68 6e 67 52 6f 77 69 64 20 69 73 20  if chngRowid is 
26b71 66 61 6c 73 65 20 61 6e 64 20 74 77 6f 20 61 72  false and two ar
26b72 65 20 75 73 65 64 20 69 66 20 63 68 6e 67 52 6f  e used if chngRo
26b73 77 69 64 20 69 73 0a 20 20 2a 2a 20 74 72 75 65  wid is.  ** true
26b74 2e 20 20 46 6f 6c 6c 6f 77 69 6e 67 20 74 68 65  .  Following the
26b75 73 65 20 61 72 65 20 70 54 61 62 2d 3e 6e 43 6f  se are pTab->nCo
26b76 6c 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64 69  l register holdi
26b77 6e 67 20 63 6f 6c 75 6d 6e 0a 20 20 2a 2a 20 64  ng column.  ** d
26b78 61 74 61 2e 0a 20 20 2a 2f 0a 20 20 72 65 67 4f  ata..  */.  regO
26b79 6c 64 52 6f 77 69 64 20 3d 20 72 65 67 4e 65 77  ldRowid = regNew
26b7a 52 6f 77 69 64 20 3d 20 70 50 61 72 73 65 2d 3e  Rowid = pParse->
26b7b 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20 70 50 61 72  nMem + 1;.  pPar
26b7c 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 54 61 62  se->nMem += pTab
26b7d 2d 3e 6e 43 6f 6c 20 2b 20 31 3b 0a 20 20 69 66  ->nCol + 1;.  if
26b7e 28 20 63 68 6e 67 52 6f 77 69 64 20 29 7b 0a 20  ( chngRowid ){. 
26b7f 20 20 20 72 65 67 4e 65 77 52 6f 77 69 64 2b 2b     regNewRowid++
26b80 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d  ;.    pParse->nM
26b81 65 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 67 44  em++;.  }.  regD
26b82 61 74 61 20 3d 20 72 65 67 4e 65 77 52 6f 77 69  ata = regNewRowi
26b83 64 2b 31 3b 0a 20 0a 0a 20 20 2f 2a 20 42 65 67  d+1;. ..  /* Beg
26b84 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  in generating co
26b85 64 65 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73  de..  */.  v = s
26b86 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
26b87 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d  arse);.  if( v==
26b88 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f  0 ) goto update_
26b89 63 6c 65 61 6e 75 70 3b 0a 20 20 69 66 28 20 70  cleanup;.  if( p
26b8a 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30  Parse->nested==0
26b8b 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f   ) sqlite3VdbeCo
26b8c 75 6e 74 43 68 61 6e 67 65 73 28 76 29 3b 0a 20  untChanges(v);. 
26b8d 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69   sqlite3BeginWri
26b8e 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72  teOperation(pPar
26b8f 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a 0a 23 69  se, 1, iDb);..#i
26b90 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
26b91 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
26b92 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c   /* Virtual tabl
26b93 65 73 20 6d 75 73 74 20 62 65 20 68 61 6e 64 6c  es must be handl
26b94 65 64 20 73 65 70 61 72 61 74 65 6c 79 20 2a 2f  ed separately */
26b95 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c  .  if( IsVirtual
26b96 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 75 70  (pTab) ){.    up
26b97 64 61 74 65 56 69 72 74 75 61 6c 54 61 62 6c 65  dateVirtualTable
26b98 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73  (pParse, pTabLis
26b99 74 2c 20 70 54 61 62 2c 20 70 43 68 61 6e 67 65  t, pTab, pChange
26b9a 73 2c 20 70 52 6f 77 69 64 45 78 70 72 2c 20 61  s, pRowidExpr, a
26b9b 58 52 65 66 2c 0a 20 20 20 20 20 20 20 20 20 20  XRef,.          
26b9c 20 20 20 20 20 20 20 20 20 20 20 20 20 70 57 68               pWh
26b9d 65 72 65 29 3b 0a 20 20 20 20 70 57 68 65 72 65  ere);.    pWhere
26b9e 20 3d 20 30 3b 0a 20 20 20 20 70 54 61 62 4c 69   = 0;.    pTabLi
26b9f 73 74 20 3d 20 30 3b 0a 20 20 20 20 67 6f 74 6f  st = 0;.    goto
26ba0 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b   update_cleanup;
26ba1 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
26ba2 2a 20 53 74 61 72 74 20 74 68 65 20 76 69 65 77  * Start the view
26ba3 20 63 6f 6e 74 65 78 74 0a 20 20 2a 2f 0a 20 20   context.  */.  
26ba4 69 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20  if( isView ){.  
26ba5 20 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e    sqlite3AuthCon
26ba6 74 65 78 74 50 75 73 68 28 70 50 61 72 73 65 2c  textPush(pParse,
26ba7 20 26 73 43 6f 6e 74 65 78 74 2c 20 70 54 61 62   &sContext, pTab
26ba8 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 0a 20  ->zName);.  }.. 
26ba9 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65   /* Generate the
26baa 20 63 6f 64 65 20 66 6f 72 20 74 72 69 67 67 65   code for trigge
26bab 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 74  rs..  */.  if( t
26bac 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 29 7b  riggers_exist ){
26bad 0a 20 20 20 20 69 6e 74 20 69 47 6f 74 6f 3b 0a  .    int iGoto;.
26bae 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 70  .    /* Create p
26baf 73 65 75 64 6f 2d 74 61 62 6c 65 73 20 66 6f 72  seudo-tables for
26bb0 20 4e 45 57 20 61 6e 64 20 4f 4c 44 0a 20 20 20   NEW and OLD.   
26bb1 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
26bb2 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
26bb3 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30  SetNumColumns, 0
26bb4 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20  , pTab->nCol);. 
26bb5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26bb6 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 50  dOp2(v, OP_OpenP
26bb7 73 65 75 64 6f 2c 20 6f 6c 64 49 64 78 2c 20 30  seudo, oldIdx, 0
26bb8 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
26bb9 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
26bba 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c  etNumColumns, 0,
26bbb 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20   pTab->nCol);.  
26bbc 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26bbd 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 50 73  Op2(v, OP_OpenPs
26bbe 65 75 64 6f 2c 20 6e 65 77 49 64 78 2c 20 30 29  eudo, newIdx, 0)
26bbf 3b 0a 0a 20 20 20 20 69 47 6f 74 6f 20 3d 20 73  ;..    iGoto = s
26bc0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
26bc1 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
26bc2 30 29 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 73  0);.    addr = s
26bc3 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
26bc4 62 65 6c 28 76 29 3b 0a 20 20 20 20 69 42 65 67  bel(v);.    iBeg
26bc5 69 6e 42 65 66 6f 72 65 54 72 69 67 67 65 72 20  inBeforeTrigger 
26bc6 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
26bc7 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20  rentAddr(v);.   
26bc8 20 69 66 28 20 73 71 6c 69 74 65 33 43 6f 64 65   if( sqlite3Code
26bc9 52 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73  RowTrigger(pPars
26bca 65 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 70 43  e, TK_UPDATE, pC
26bcb 68 61 6e 67 65 73 2c 20 54 52 49 47 47 45 52 5f  hanges, TRIGGER_
26bcc 42 45 46 4f 52 45 2c 20 70 54 61 62 2c 0a 20 20  BEFORE, pTab,.  
26bcd 20 20 20 20 20 20 20 20 6e 65 77 49 64 78 2c 20          newIdx, 
26bce 6f 6c 64 49 64 78 2c 20 6f 6e 45 72 72 6f 72 2c  oldIdx, onError,
26bcf 20 61 64 64 72 2c 20 26 6f 6c 64 5f 63 6f 6c 5f   addr, &old_col_
26bd0 6d 61 73 6b 2c 20 26 6e 65 77 5f 63 6f 6c 5f 6d  mask, &new_col_m
26bd1 61 73 6b 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  ask) ){.      go
26bd2 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75  to update_cleanu
26bd3 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 45 6e  p;.    }.    iEn
26bd4 64 42 65 66 6f 72 65 54 72 69 67 67 65 72 20 3d  dBeforeTrigger =
26bd5 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26bd6 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
26bd7 2c 20 30 29 3b 0a 20 20 20 20 69 42 65 67 69 6e  , 0);.    iBegin
26bd8 41 66 74 65 72 54 72 69 67 67 65 72 20 3d 20 73  AfterTrigger = s
26bd9 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
26bda 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 69 66  tAddr(v);.    if
26bdb 28 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77  ( sqlite3CodeRow
26bdc 54 72 69 67 67 65 72 28 70 50 61 72 73 65 2c 20  Trigger(pParse, 
26bdd 54 4b 5f 55 50 44 41 54 45 2c 20 70 43 68 61 6e  TK_UPDATE, pChan
26bde 67 65 73 2c 20 54 52 49 47 47 45 52 5f 41 46 54  ges, TRIGGER_AFT
26bdf 45 52 2c 20 70 54 61 62 2c 20 0a 20 20 20 20 20  ER, pTab, .     
26be0 20 20 20 20 20 6e 65 77 49 64 78 2c 20 6f 6c 64       newIdx, old
26be1 49 64 78 2c 20 6f 6e 45 72 72 6f 72 2c 20 61 64  Idx, onError, ad
26be2 64 72 2c 20 26 6f 6c 64 5f 63 6f 6c 5f 6d 61 73  dr, &old_col_mas
26be3 6b 2c 20 26 6e 65 77 5f 63 6f 6c 5f 6d 61 73 6b  k, &new_col_mask
26be4 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ) ){.      goto 
26be5 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
26be6 20 20 20 20 7d 0a 20 20 20 20 69 45 6e 64 41 66      }.    iEndAf
26be7 74 65 72 54 72 69 67 67 65 72 20 3d 20 73 71 6c  terTrigger = sql
26be8 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
26be9 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30 29  , OP_Goto, 0, 0)
26bea 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
26beb 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 47 6f  eJumpHere(v, iGo
26bec 74 6f 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  to);.  }..  /* I
26bed 66 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 20  f we are trying 
26bee 74 6f 20 75 70 64 61 74 65 20 61 20 76 69 65 77  to update a view
26bef 2c 20 72 65 61 6c 69 7a 65 20 74 68 61 74 20 76  , realize that v
26bf0 69 65 77 20 69 6e 74 6f 0a 20 20 2a 2a 20 61 20  iew into.  ** a 
26bf1 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e  ephemeral table.
26bf2 0a 20 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  .  */.#if !defin
26bf3 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
26bf4 49 45 57 29 20 26 26 20 21 64 65 66 69 6e 65 64  IEW) && !defined
26bf5 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49  (SQLITE_OMIT_TRI
26bf6 47 47 45 52 29 0a 20 20 69 66 28 20 69 73 56 69  GGER).  if( isVi
26bf7 65 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ew ){.    sqlite
26bf8 33 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65 77  3MaterializeView
26bf9 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70  (pParse, pTab, p
26bfa 57 68 65 72 65 2c 20 69 43 75 72 29 3b 0a 20 20  Where, iCur);.  
26bfb 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52  }.#endif..  /* R
26bfc 65 73 6f 6c 76 65 20 74 68 65 20 63 6f 6c 75 6d  esolve the colum
26bfd 6e 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 74  n names in all t
26bfe 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69  he expressions i
26bff 6e 20 74 68 65 0a 20 20 2a 2a 20 57 48 45 52 45  n the.  ** WHERE
26c00 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20   clause..  */.  
26c01 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c  if( sqlite3Resol
26c02 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43  veExprNames(&sNC
26c03 2c 20 70 57 68 65 72 65 29 20 29 7b 0a 20 20 20  , pWhere) ){.   
26c04 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65   goto update_cle
26c05 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  anup;.  }..  /* 
26c06 42 65 67 69 6e 20 74 68 65 20 64 61 74 61 62 61  Begin the databa
26c07 73 65 20 73 63 61 6e 0a 20 20 2a 2f 0a 20 20 73  se scan.  */.  s
26c08 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
26c09 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
26c0a 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20  regOldRowid);.  
26c0b 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33  pWInfo = sqlite3
26c0c 57 68 65 72 65 42 65 67 69 6e 28 70 50 61 72 73  WhereBegin(pPars
26c0d 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 57 68  e, pTabList, pWh
26c0e 65 72 65 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  ere, 0,.        
26c0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26c10 20 20 20 20 20 57 48 45 52 45 5f 4f 4e 45 50 41       WHERE_ONEPA
26c11 53 53 5f 44 45 53 49 52 45 44 29 3b 0a 20 20 69  SS_DESIRED);.  i
26c12 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20 29 20 67  f( pWInfo==0 ) g
26c13 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e  oto update_clean
26c14 75 70 3b 0a 20 20 6f 6b 4f 6e 65 50 61 73 73 20  up;.  okOnePass 
26c15 3d 20 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50  = pWInfo->okOneP
26c16 61 73 73 3b 0a 0a 20 20 2f 2a 20 52 65 6d 65 6d  ass;..  /* Remem
26c17 62 65 72 20 74 68 65 20 72 6f 77 69 64 20 6f 66  ber the rowid of
26c18 20 65 76 65 72 79 20 69 74 65 6d 20 74 6f 20 62   every item to b
26c19 65 20 75 70 64 61 74 65 64 2e 0a 20 20 2a 2f 0a  e updated..  */.
26c1a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26c1b 4f 70 32 28 76 2c 20 49 73 56 69 72 74 75 61 6c  Op2(v, IsVirtual
26c1c 28 70 54 61 62 29 3f 4f 50 5f 56 52 6f 77 69 64  (pTab)?OP_VRowid
26c1d 3a 4f 50 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c  :OP_Rowid, iCur,
26c1e 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20   regOldRowid);. 
26c1f 20 69 66 28 20 21 6f 6b 4f 6e 65 50 61 73 73 20   if( !okOnePass 
26c20 29 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  ) sqlite3VdbeAdd
26c21 4f 70 32 28 76 2c 20 4f 50 5f 46 69 66 6f 57 72  Op2(v, OP_FifoWr
26c22 69 74 65 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64  ite, regOldRowid
26c23 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 45 6e 64 20  , 0);..  /* End 
26c24 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 61  the database sca
26c25 6e 20 6c 6f 6f 70 2e 0a 20 20 2a 2f 0a 20 20 73  n loop..  */.  s
26c26 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 70  qlite3WhereEnd(p
26c27 57 49 6e 66 6f 29 3b 0a 0a 20 20 2f 2a 20 49 6e  WInfo);..  /* In
26c28 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 75  itialize the cou
26c29 6e 74 20 6f 66 20 75 70 64 61 74 65 64 20 72 6f  nt of updated ro
26c2a 77 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62  ws.  */.  if( db
26c2b 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
26c2c 5f 43 6f 75 6e 74 52 6f 77 73 20 26 26 20 21 70  _CountRows && !p
26c2d 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b  Parse->trigStack
26c2e 20 29 7b 0a 20 20 20 20 72 65 67 52 6f 77 43 6f   ){.    regRowCo
26c2f 75 6e 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  unt = ++pParse->
26c30 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65  nMem;.    sqlite
26c31 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
26c32 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65  P_Integer, 0, re
26c33 67 52 6f 77 43 6f 75 6e 74 29 3b 0a 20 20 7d 0a  gRowCount);.  }.
26c34 0a 20 20 69 66 28 20 21 69 73 56 69 65 77 20 26  .  if( !isView &
26c35 26 20 21 49 73 56 69 72 74 75 61 6c 28 70 54 61  & !IsVirtual(pTa
26c36 62 29 20 29 7b 0a 20 20 20 20 2f 2a 20 0a 20 20  b) ){.    /* .  
26c37 20 20 2a 2a 20 4f 70 65 6e 20 65 76 65 72 79 20    ** Open every 
26c38 69 6e 64 65 78 20 74 68 61 74 20 6e 65 65 64 73  index that needs
26c39 20 75 70 64 61 74 69 6e 67 2e 20 20 4e 6f 74 65   updating.  Note
26c3a 20 74 68 61 74 20 69 66 20 61 6e 79 0a 20 20 20   that if any.   
26c3b 20 2a 2a 20 69 6e 64 65 78 20 63 6f 75 6c 64 20   ** index could 
26c3c 70 6f 74 65 6e 74 69 61 6c 6c 79 20 69 6e 76 6f  potentially invo
26c3d 6b 65 20 61 20 52 45 50 4c 41 43 45 20 63 6f 6e  ke a REPLACE con
26c3e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e  flict resolution
26c3f 20 0a 20 20 20 20 2a 2a 20 61 63 74 69 6f 6e 2c   .    ** action,
26c40 20 74 68 65 6e 20 77 65 20 6e 65 65 64 20 74 6f   then we need to
26c41 20 6f 70 65 6e 20 61 6c 6c 20 69 6e 64 69 63 65   open all indice
26c42 73 20 62 65 63 61 75 73 65 20 77 65 20 6d 69 67  s because we mig
26c43 68 74 20 6e 65 65 64 0a 20 20 20 20 2a 2a 20 74  ht need.    ** t
26c44 6f 20 62 65 20 64 65 6c 65 74 69 6e 67 20 73 6f  o be deleting so
26c45 6d 65 20 72 65 63 6f 72 64 73 2e 0a 20 20 20 20  me records..    
26c46 2a 2f 0a 20 20 20 20 69 66 28 20 21 6f 6b 4f 6e  */.    if( !okOn
26c47 65 50 61 73 73 20 29 20 73 71 6c 69 74 65 33 4f  ePass ) sqlite3O
26c48 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c  penTable(pParse,
26c49 20 69 43 75 72 2c 20 69 44 62 2c 20 70 54 61 62   iCur, iDb, pTab
26c4a 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b  , OP_OpenWrite);
26c4b 20 0a 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f   .    if( onErro
26c4c 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 7b  r==OE_Replace ){
26c4d 0a 20 20 20 20 20 20 6f 70 65 6e 41 6c 6c 20 3d  .      openAll =
26c4e 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   1;.    }else{. 
26c4f 20 20 20 20 20 6f 70 65 6e 41 6c 6c 20 3d 20 30       openAll = 0
26c50 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 49 64 78  ;.      for(pIdx
26c51 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
26c52 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
26c53 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20  pNext){.        
26c54 69 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f  if( pIdx->onErro
26c55 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 7b  r==OE_Replace ){
26c56 0a 20 20 20 20 20 20 20 20 20 20 6f 70 65 6e 41  .          openA
26c57 6c 6c 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ll = 1;.        
26c58 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
26c59 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
26c5a 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49  .    for(i=0, pI
26c5b 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
26c5c 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
26c5d 2d 3e 70 4e 65 78 74 2c 20 69 2b 2b 29 7b 0a 20  ->pNext, i++){. 
26c5e 20 20 20 20 20 69 66 28 20 6f 70 65 6e 41 6c 6c       if( openAll
26c5f 20 7c 7c 20 61 52 65 67 49 64 78 5b 69 5d 3e 30   || aRegIdx[i]>0
26c60 20 29 7b 0a 20 20 20 20 20 20 20 20 4b 65 79 49   ){.        KeyI
26c61 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69  nfo *pKey = sqli
26c62 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28  te3IndexKeyinfo(
26c63 70 50 61 72 73 65 2c 20 70 49 64 78 29 3b 0a 20  pParse, pIdx);. 
26c64 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
26c65 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f  beAddOp4(v, OP_O
26c66 70 65 6e 57 72 69 74 65 2c 20 69 43 75 72 2b 69  penWrite, iCur+i
26c67 2b 31 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20  +1, pIdx->tnum, 
26c68 69 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20  iDb,.           
26c69 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61              (cha
26c6a 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49  r*)pKey, P4_KEYI
26c6b 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20  NFO_HANDOFF);.  
26c6c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
26c6d 61 72 73 65 2d 3e 6e 54 61 62 3e 69 43 75 72 2b  arse->nTab>iCur+
26c6e 69 2b 31 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20  i+1 );.      }. 
26c6f 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a     }.  }.  .  /*
26c70 20 4a 75 6d 70 20 62 61 63 6b 20 74 6f 20 74 68   Jump back to th
26c71 69 73 20 70 6f 69 6e 74 20 69 66 20 61 20 74 72  is point if a tr
26c72 69 67 67 65 72 20 65 6e 63 6f 75 6e 74 65 72 73  igger encounters
26c73 20 61 6e 20 49 47 4e 4f 52 45 20 63 6f 6e 73 74   an IGNORE const
26c74 72 61 69 6e 74 2e 20 2a 2f 0a 20 20 69 66 28 20  raint. */.  if( 
26c75 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 29  triggers_exist )
26c76 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
26c77 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
26c78 20 61 64 64 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f   addr);.  }..  /
26c79 2a 20 54 6f 70 20 6f 66 20 74 68 65 20 75 70 64  * Top of the upd
26c7a 61 74 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 66  ate loop */.  if
26c7b 28 20 6f 6b 4f 6e 65 50 61 73 73 20 29 7b 0a 20  ( okOnePass ){. 
26c7c 20 20 20 69 6e 74 20 61 31 20 3d 20 73 71 6c 69     int a1 = sqli
26c7d 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
26c7e 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67   OP_NotNull, reg
26c7f 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20 20 20 61  OldRowid);.    a
26c80 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
26c81 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f  eAddOp0(v, OP_Go
26c82 74 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  to);.    sqlite3
26c83 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
26c84 61 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  a1);.  }else{.  
26c85 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
26c86 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
26c87 5f 46 69 66 6f 52 65 61 64 2c 20 72 65 67 4f 6c  _FifoRead, regOl
26c88 64 52 6f 77 69 64 2c 20 30 29 3b 0a 20 20 7d 0a  dRowid, 0);.  }.
26c89 0a 20 20 69 66 28 20 74 72 69 67 67 65 72 73 5f  .  if( triggers_
26c8a 65 78 69 73 74 20 29 7b 0a 20 20 20 20 69 6e 74  exist ){.    int
26c8b 20 72 65 67 52 6f 77 69 64 3b 0a 20 20 20 20 69   regRowid;.    i
26c8c 6e 74 20 72 65 67 52 6f 77 3b 0a 20 20 20 20 69  nt regRow;.    i
26c8d 6e 74 20 72 65 67 43 6f 6c 73 3b 0a 0a 20 20 20  nt regCols;..   
26c8e 20 2f 2a 20 4d 61 6b 65 20 63 75 72 73 6f 72 20   /* Make cursor 
26c8f 69 43 75 72 20 70 6f 69 6e 74 20 74 6f 20 74 68  iCur point to th
26c90 65 20 72 65 63 6f 72 64 20 74 68 61 74 20 69 73  e record that is
26c91 20 62 65 69 6e 67 20 75 70 64 61 74 65 64 2e 0a   being updated..
26c92 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
26c93 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
26c94 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69 43  OP_NotExists, iC
26c95 75 72 2c 20 61 64 64 72 2c 20 72 65 67 4f 6c 64  ur, addr, regOld
26c96 52 6f 77 69 64 29 3b 0a 0a 20 20 20 20 2f 2a 20  Rowid);..    /* 
26c97 47 65 6e 65 72 61 74 65 20 74 68 65 20 4f 4c 44  Generate the OLD
26c98 20 74 61 62 6c 65 0a 20 20 20 20 2a 2f 0a 20 20   table.    */.  
26c99 20 20 72 65 67 52 6f 77 69 64 20 3d 20 73 71 6c    regRowid = sql
26c9a 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
26c9b 50 61 72 73 65 29 3b 0a 20 20 20 20 72 65 67 52  Parse);.    regR
26c9c 6f 77 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  ow = sqlite3GetT
26c9d 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
26c9e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26c9f 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69  ddOp2(v, OP_Rowi
26ca0 64 2c 20 69 43 75 72 2c 20 72 65 67 52 6f 77 69  d, iCur, regRowi
26ca1 64 29 3b 0a 20 20 20 20 69 66 28 20 21 6f 6c 64  d);.    if( !old
26ca2 5f 63 6f 6c 5f 6d 61 73 6b 20 29 7b 0a 20 20 20  _col_mask ){.   
26ca3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26ca4 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
26ca5 20 30 2c 20 72 65 67 52 6f 77 29 3b 0a 20 20 20   0, regRow);.   
26ca6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
26ca7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
26ca8 76 2c 20 4f 50 5f 52 6f 77 44 61 74 61 2c 20 69  v, OP_RowData, i
26ca9 43 75 72 2c 20 72 65 67 52 6f 77 29 3b 0a 20 20  Cur, regRow);.  
26caa 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
26cab 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
26cac 49 6e 73 65 72 74 2c 20 6f 6c 64 49 64 78 2c 20  Insert, oldIdx, 
26cad 72 65 67 52 6f 77 2c 20 72 65 67 52 6f 77 69 64  regRow, regRowid
26cae 29 3b 0a 0a 20 20 20 20 2f 2a 20 47 65 6e 65 72  );..    /* Gener
26caf 61 74 65 20 74 68 65 20 4e 45 57 20 74 61 62 6c  ate the NEW tabl
26cb0 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  e.    */.    if(
26cb1 20 63 68 6e 67 52 6f 77 69 64 20 29 7b 0a 20 20   chngRowid ){.  
26cb2 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
26cb3 6f 64 65 41 6e 64 43 61 63 68 65 28 70 50 61 72  odeAndCache(pPar
26cb4 73 65 2c 20 70 52 6f 77 69 64 45 78 70 72 2c 20  se, pRowidExpr, 
26cb5 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  regRowid);.     
26cb6 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26cb7 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49  p1(v, OP_MustBeI
26cb8 6e 74 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  nt, regRowid);. 
26cb9 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
26cba 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26cbb 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69  2(v, OP_Rowid, i
26cbc 43 75 72 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  Cur, regRowid);.
26cbd 20 20 20 20 7d 0a 20 20 20 20 72 65 67 43 6f 6c      }.    regCol
26cbe 73 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  s = sqlite3GetTe
26cbf 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
26cc0 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20  pTab->nCol);.   
26cc1 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62   for(i=0; i<pTab
26cc2 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  ->nCol; i++){.  
26cc3 20 20 20 20 69 66 28 20 69 3d 3d 70 54 61 62 2d      if( i==pTab-
26cc4 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20  >iPKey ){.      
26cc5 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26cc6 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
26cc7 30 2c 20 72 65 67 43 6f 6c 73 2b 69 29 3b 0a 20  0, regCols+i);. 
26cc8 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
26cc9 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6a  .      }.      j
26cca 20 3d 20 61 58 52 65 66 5b 69 5d 3b 0a 20 20 20   = aXRef[i];.   
26ccb 20 20 20 69 66 28 20 6e 65 77 5f 63 6f 6c 5f 6d     if( new_col_m
26ccc 61 73 6b 26 28 28 75 33 32 29 31 3c 3c 69 29 20  ask&((u32)1<<i) 
26ccd 7c 7c 20 6e 65 77 5f 63 6f 6c 5f 6d 61 73 6b 3d  || new_col_mask=
26cce 3d 30 78 66 66 66 66 66 66 66 66 20 29 7b 0a 20  =0xffffffff ){. 
26ccf 20 20 20 20 20 20 20 69 66 28 20 6a 3c 30 20 29         if( j<0 )
26cd0 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
26cd1 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
26cd2 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72   OP_Column, iCur
26cd3 2c 20 69 2c 20 72 65 67 43 6f 6c 73 2b 69 29 3b  , i, regCols+i);
26cd4 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
26cd5 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28  e3ColumnDefault(
26cd6 76 2c 20 70 54 61 62 2c 20 69 29 3b 0a 20 20 20  v, pTab, i);.   
26cd7 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
26cd8 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
26cd9 72 43 6f 64 65 41 6e 64 43 61 63 68 65 28 70 50  rCodeAndCache(pP
26cda 61 72 73 65 2c 20 70 43 68 61 6e 67 65 73 2d 3e  arse, pChanges->
26cdb 61 5b 6a 5d 2e 70 45 78 70 72 2c 20 72 65 67 43  a[j].pExpr, regC
26cdc 6f 6c 73 2b 69 29 3b 0a 20 20 20 20 20 20 20 20  ols+i);.        
26cdd 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
26cde 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
26cdf 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
26ce0 75 6c 6c 2c 20 30 2c 20 72 65 67 43 6f 6c 73 2b  ull, 0, regCols+
26ce1 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  i);.      }.    
26ce2 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  }.    sqlite3Vdb
26ce3 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61  eAddOp3(v, OP_Ma
26ce4 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 43 6f 6c  keRecord, regCol
26ce5 73 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 72  s, pTab->nCol, r
26ce6 65 67 52 6f 77 29 3b 0a 20 20 20 20 69 66 28 20  egRow);.    if( 
26ce7 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20  !isView ){.     
26ce8 20 73 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66   sqlite3TableAff
26ce9 69 6e 69 74 79 53 74 72 28 76 2c 20 70 54 61 62  inityStr(v, pTab
26cea 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
26ceb 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
26cec 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20  yChange(pParse, 
26ced 72 65 67 43 6f 6c 73 2c 20 70 54 61 62 2d 3e 6e  regCols, pTab->n
26cee 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Col);.    }.    
26cef 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
26cf0 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
26cf1 72 65 67 43 6f 6c 73 2c 20 70 54 61 62 2d 3e 6e  regCols, pTab->n
26cf2 43 6f 6c 29 3b 0a 20 20 20 20 2f 2a 20 69 66 28  Col);.    /* if(
26cf3 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 20   pParse->nErr ) 
26cf4 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61  goto update_clea
26cf5 6e 75 70 3b 20 2a 2f 0a 20 20 20 20 73 71 6c 69  nup; */.    sqli
26cf6 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
26cf7 20 4f 50 5f 49 6e 73 65 72 74 2c 20 6e 65 77 49   OP_Insert, newI
26cf8 64 78 2c 20 72 65 67 52 6f 77 2c 20 72 65 67 52  dx, regRow, regR
26cf9 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  owid);.    sqlit
26cfa 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
26cfb 28 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77 69  (pParse, regRowi
26cfc 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52  d);.    sqlite3R
26cfd 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
26cfe 61 72 73 65 2c 20 72 65 67 52 6f 77 29 3b 0a 0a  arse, regRow);..
26cff 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26d00 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
26d01 2c 20 30 2c 20 69 42 65 67 69 6e 42 65 66 6f 72  , 0, iBeginBefor
26d02 65 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 73  eTrigger);.    s
26d03 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
26d04 72 65 28 76 2c 20 69 45 6e 64 42 65 66 6f 72 65  re(v, iEndBefore
26d05 54 72 69 67 67 65 72 29 3b 0a 20 20 7d 0a 0a 20  Trigger);.  }.. 
26d06 20 69 66 28 20 21 69 73 56 69 65 77 20 26 26 20   if( !isView && 
26d07 21 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29  !IsVirtual(pTab)
26d08 20 29 7b 0a 20 20 20 20 2f 2a 20 4c 6f 6f 70 20   ){.    /* Loop 
26d09 6f 76 65 72 20 65 76 65 72 79 20 72 65 63 6f 72  over every recor
26d0a 64 20 74 68 61 74 20 6e 65 65 64 73 20 75 70 64  d that needs upd
26d0b 61 74 69 6e 67 2e 20 20 57 65 20 68 61 76 65 20  ating.  We have 
26d0c 74 6f 20 6c 6f 61 64 0a 20 20 20 20 2a 2a 20 74  to load.    ** t
26d0d 68 65 20 6f 6c 64 20 64 61 74 61 20 66 6f 72 20  he old data for 
26d0e 65 61 63 68 20 72 65 63 6f 72 64 20 74 6f 20 62  each record to b
26d0f 65 20 75 70 64 61 74 65 64 20 62 65 63 61 75 73  e updated becaus
26d10 65 20 73 6f 6d 65 20 63 6f 6c 75 6d 6e 73 0a 20  e some columns. 
26d11 20 20 20 2a 2a 20 6d 69 67 68 74 20 6e 6f 74 20     ** might not 
26d12 63 68 61 6e 67 65 20 61 6e 64 20 77 65 20 77 69  change and we wi
26d13 6c 6c 20 6e 65 65 64 20 74 6f 20 63 6f 70 79 20  ll need to copy 
26d14 74 68 65 20 6f 6c 64 20 76 61 6c 75 65 2e 0a 20  the old value.. 
26d15 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 74 68 65 20     ** Also, the 
26d16 6f 6c 64 20 64 61 74 61 20 69 73 20 6e 65 65 64  old data is need
26d17 65 64 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65  ed to delete the
26d18 20 6f 6c 64 20 69 6e 64 65 78 20 65 6e 74 72 69   old index entri
26d19 65 73 2e 0a 20 20 20 20 2a 2a 20 53 6f 20 6d 61  es..    ** So ma
26d1a 6b 65 20 74 68 65 20 63 75 72 73 6f 72 20 70 6f  ke the cursor po
26d1b 69 6e 74 20 61 74 20 74 68 65 20 6f 6c 64 20 72  int at the old r
26d1c 65 63 6f 72 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  ecord..    */.  
26d1d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26d1e 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69  Op3(v, OP_NotExi
26d1f 73 74 73 2c 20 69 43 75 72 2c 20 61 64 64 72 2c  sts, iCur, addr,
26d20 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 0a   regOldRowid);..
26d21 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 72 65      /* If the re
26d22 63 6f 72 64 20 6e 75 6d 62 65 72 20 77 69 6c 6c  cord number will
26d23 20 63 68 61 6e 67 65 2c 20 70 75 73 68 20 74 68   change, push th
26d24 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  e record number 
26d25 61 73 20 69 74 0a 20 20 20 20 2a 2a 20 77 69 6c  as it.    ** wil
26d26 6c 20 62 65 20 61 66 74 65 72 20 74 68 65 20 75  l be after the u
26d27 70 64 61 74 65 2e 20 28 54 68 65 20 6f 6c 64 20  pdate. (The old 
26d28 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73  record number is
26d29 20 63 75 72 72 65 6e 74 6c 79 0a 20 20 20 20 2a   currently.    *
26d2a 2a 20 6f 6e 20 74 6f 70 20 6f 66 20 74 68 65 20  * on top of the 
26d2b 73 74 61 63 6b 2e 29 0a 20 20 20 20 2a 2f 0a 20  stack.).    */. 
26d2c 20 20 20 69 66 28 20 63 68 6e 67 52 6f 77 69 64     if( chngRowid
26d2d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
26d2e 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
26d2f 2c 20 70 52 6f 77 69 64 45 78 70 72 2c 20 72 65  , pRowidExpr, re
26d30 67 4e 65 77 52 6f 77 69 64 29 3b 0a 20 20 20 20  gNewRowid);.    
26d31 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26d32 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65  Op1(v, OP_MustBe
26d33 49 6e 74 2c 20 72 65 67 4e 65 77 52 6f 77 69 64  Int, regNewRowid
26d34 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
26d35 20 43 6f 6d 70 75 74 65 20 6e 65 77 20 64 61 74   Compute new dat
26d36 61 20 66 6f 72 20 74 68 69 73 20 72 65 63 6f 72  a for this recor
26d37 64 2e 20 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20  d.  .    */.    
26d38 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d  for(i=0; i<pTab-
26d39 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nCol; i++){.   
26d3a 20 20 20 69 66 28 20 69 3d 3d 70 54 61 62 2d 3e     if( i==pTab->
26d3b 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20  iPKey ){.       
26d3c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26d3d 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30  p2(v, OP_Null, 0
26d3e 2c 20 72 65 67 44 61 74 61 2b 69 29 3b 0a 20 20  , regData+i);.  
26d3f 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
26d40 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6a 20        }.      j 
26d41 3d 20 61 58 52 65 66 5b 69 5d 3b 0a 20 20 20 20  = aXRef[i];.    
26d42 20 20 69 66 28 20 6a 3c 30 20 29 7b 0a 20 20 20    if( j<0 ){.   
26d43 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26d44 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
26d45 75 6d 6e 2c 20 69 43 75 72 2c 20 69 2c 20 72 65  umn, iCur, i, re
26d46 67 44 61 74 61 2b 69 29 3b 0a 20 20 20 20 20 20  gData+i);.      
26d47 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44    sqlite3ColumnD
26d48 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62 2c 20  efault(v, pTab, 
26d49 69 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  i);.      }else{
26d4a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
26d4b 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
26d4c 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 6a 5d 2e   pChanges->a[j].
26d4d 70 45 78 70 72 2c 20 72 65 67 44 61 74 61 2b 69  pExpr, regData+i
26d4e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
26d4f 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f 6e 73  ..    /* Do cons
26d50 74 72 61 69 6e 74 20 63 68 65 63 6b 73 0a 20 20  traint checks.  
26d51 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33    */.    sqlite3
26d52 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69  GenerateConstrai
26d53 6e 74 43 68 65 63 6b 73 28 70 50 61 72 73 65 2c  ntChecks(pParse,
26d54 20 70 54 61 62 2c 20 69 43 75 72 2c 20 72 65 67   pTab, iCur, reg
26d55 4e 65 77 52 6f 77 69 64 2c 0a 20 20 20 20 20 20  NewRowid,.      
26d56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26d57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 52                aR
26d58 65 67 49 64 78 2c 20 63 68 6e 67 52 6f 77 69 64  egIdx, chngRowid
26d59 2c 20 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 1,.           
26d5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26d5b 20 20 20 20 20 20 20 20 20 6f 6e 45 72 72 6f 72           onError
26d5c 2c 20 61 64 64 72 29 3b 0a 0a 20 20 20 20 2f 2a  , addr);..    /*
26d5d 20 44 65 6c 65 74 65 20 74 68 65 20 6f 6c 64 20   Delete the old 
26d5e 69 6e 64 69 63 65 73 20 66 6f 72 20 74 68 65 20  indices for the 
26d5f 63 75 72 72 65 6e 74 20 72 65 63 6f 72 64 2e 0a  current record..
26d60 20 20 20 20 2a 2f 0a 20 20 20 20 6a 31 20 3d 20      */.    j1 = 
26d61 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26d62 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74  3(v, OP_NotExist
26d63 73 2c 20 69 43 75 72 2c 20 30 2c 20 72 65 67 4f  s, iCur, 0, regO
26d64 6c 64 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71  ldRowid);.    sq
26d65 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77  lite3GenerateRow
26d66 49 6e 64 65 78 44 65 6c 65 74 65 28 70 50 61 72  IndexDelete(pPar
26d67 73 65 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20  se, pTab, iCur, 
26d68 61 52 65 67 49 64 78 29 3b 0a 0a 20 20 20 20 2f  aRegIdx);..    /
26d69 2a 20 49 66 20 63 68 61 6e 67 69 6e 67 20 74 68  * If changing th
26d6a 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 2c  e record number,
26d6b 20 64 65 6c 65 74 65 20 74 68 65 20 6f 6c 64 20   delete the old 
26d6c 72 65 63 6f 72 64 2e 0a 20 20 20 20 2a 2f 0a 20  record..    */. 
26d6d 20 20 20 69 66 28 20 63 68 6e 67 52 6f 77 69 64     if( chngRowid
26d6e 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
26d6f 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
26d70 50 5f 44 65 6c 65 74 65 2c 20 69 43 75 72 2c 20  P_Delete, iCur, 
26d71 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  0);.    }.    sq
26d72 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
26d73 65 28 76 2c 20 6a 31 29 3b 0a 0a 20 20 20 20 2f  e(v, j1);..    /
26d74 2a 20 43 72 65 61 74 65 20 74 68 65 20 6e 65 77  * Create the new
26d75 20 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20 61   index entries a
26d76 6e 64 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72  nd the new recor
26d77 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  d..    */.    sq
26d78 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73  lite3CompleteIns
26d79 65 72 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70  ertion(pParse, p
26d7a 54 61 62 2c 20 69 43 75 72 2c 20 72 65 67 4e 65  Tab, iCur, regNe
26d7b 77 52 6f 77 69 64 2c 20 0a 20 20 20 20 20 20 20  wRowid, .       
26d7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26d7d 20 20 20 20 20 20 61 52 65 67 49 64 78 2c 20 63        aRegIdx, c
26d7e 68 6e 67 52 6f 77 69 64 2c 20 31 2c 20 2d 31 2c  hngRowid, 1, -1,
26d7f 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49   0);.  }..  /* I
26d80 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 6f 77  ncrement the row
26d81 20 63 6f 75 6e 74 65 72 20 0a 20 20 2a 2f 0a 20   counter .  */. 
26d82 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26   if( db->flags &
26d83 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77   SQLITE_CountRow
26d84 73 20 26 26 20 21 70 50 61 72 73 65 2d 3e 74 72  s && !pParse->tr
26d85 69 67 53 74 61 63 6b 29 7b 0a 20 20 20 20 73 71  igStack){.    sq
26d86 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
26d87 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72 65  v, OP_AddImm, re
26d88 67 52 6f 77 43 6f 75 6e 74 2c 20 31 29 3b 0a 20  gRowCount, 1);. 
26d89 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72   }..  /* If ther
26d8a 65 20 61 72 65 20 74 72 69 67 67 65 72 73 2c 20  e are triggers, 
26d8b 63 6c 6f 73 65 20 61 6c 6c 20 74 68 65 20 63 75  close all the cu
26d8c 72 73 6f 72 73 20 61 66 74 65 72 20 65 61 63 68  rsors after each
26d8d 20 69 74 65 72 61 74 69 6f 6e 0a 20 20 2a 2a 20   iteration.  ** 
26d8e 74 68 72 6f 75 67 68 20 74 68 65 20 6c 6f 6f 70  through the loop
26d8f 2e 20 20 54 68 65 20 66 69 72 65 20 74 68 65 20  .  The fire the 
26d90 61 66 74 65 72 20 74 72 69 67 67 65 72 73 2e 0a  after triggers..
26d91 20 20 2a 2f 0a 20 20 69 66 28 20 74 72 69 67 67    */.  if( trigg
26d92 65 72 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20  ers_exist ){.   
26d93 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26d94 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
26d95 2c 20 69 42 65 67 69 6e 41 66 74 65 72 54 72 69  , iBeginAfterTri
26d96 67 67 65 72 29 3b 0a 20 20 20 20 73 71 6c 69 74  gger);.    sqlit
26d97 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
26d98 2c 20 69 45 6e 64 41 66 74 65 72 54 72 69 67 67  , iEndAfterTrigg
26d99 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52  er);.  }..  /* R
26d9a 65 70 65 61 74 20 74 68 65 20 61 62 6f 76 65 20  epeat the above 
26d9b 77 69 74 68 20 74 68 65 20 6e 65 78 74 20 72 65  with the next re
26d9c 63 6f 72 64 20 74 6f 20 62 65 20 75 70 64 61 74  cord to be updat
26d9d 65 64 2c 20 75 6e 74 69 6c 0a 20 20 2a 2a 20 61  ed, until.  ** a
26d9e 6c 6c 20 72 65 63 6f 72 64 20 73 65 6c 65 63 74  ll record select
26d9f 65 64 20 62 79 20 74 68 65 20 57 48 45 52 45 20  ed by the WHERE 
26da0 63 6c 61 75 73 65 20 68 61 76 65 20 62 65 65 6e  clause have been
26da1 20 75 70 64 61 74 65 64 2e 0a 20 20 2a 2f 0a 20   updated..  */. 
26da2 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26da3 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
26da4 2c 20 61 64 64 72 29 3b 0a 20 20 73 71 6c 69 74  , addr);.  sqlit
26da5 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
26da6 2c 20 61 64 64 72 29 3b 0a 0a 20 20 2f 2a 20 43  , addr);..  /* C
26da7 6c 6f 73 65 20 61 6c 6c 20 74 61 62 6c 65 73 20  lose all tables 
26da8 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49  */.  for(i=0, pI
26da9 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
26daa 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
26dab 2d 3e 70 4e 65 78 74 2c 20 69 2b 2b 29 7b 0a 20  ->pNext, i++){. 
26dac 20 20 20 69 66 28 20 6f 70 65 6e 41 6c 6c 20 7c     if( openAll |
26dad 7c 20 61 52 65 67 49 64 78 5b 69 5d 3e 30 20 29  | aRegIdx[i]>0 )
26dae 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
26daf 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
26db0 43 6c 6f 73 65 2c 20 69 43 75 72 2b 69 2b 31 2c  Close, iCur+i+1,
26db1 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20   0);.    }.  }. 
26db2 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26db3 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p2(v, OP_Close, 
26db4 69 43 75 72 2c 20 30 29 3b 0a 20 20 69 66 28 20  iCur, 0);.  if( 
26db5 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 29  triggers_exist )
26db6 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
26db7 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c  eAddOp2(v, OP_Cl
26db8 6f 73 65 2c 20 6e 65 77 49 64 78 2c 20 30 29 3b  ose, newIdx, 0);
26db9 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
26dba 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp2(v, OP_Clo
26dbb 73 65 2c 20 6f 6c 64 49 64 78 2c 20 30 29 3b 0a  se, oldIdx, 0);.
26dbc 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52    }..  /*.  ** R
26dbd 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
26dbe 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20 77 65   of rows that we
26dbf 72 65 20 63 68 61 6e 67 65 64 2e 20 49 66 20 74  re changed. If t
26dc0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 0a  his routine is .
26dc1 20 20 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67 20    ** generating 
26dc2 63 6f 64 65 20 62 65 63 61 75 73 65 20 6f 66 20  code because of 
26dc3 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  a call to sqlite
26dc4 33 4e 65 73 74 65 64 50 61 72 73 65 28 29 2c 20  3NestedParse(), 
26dc5 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 69 6e 76 6f  do not.  ** invo
26dc6 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  ke the callback 
26dc7 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20  function..  */. 
26dc8 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26   if( db->flags &
26dc9 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77   SQLITE_CountRow
26dca 73 20 26 26 20 21 70 50 61 72 73 65 2d 3e 74 72  s && !pParse->tr
26dcb 69 67 53 74 61 63 6b 20 26 26 20 70 50 61 72 73  igStack && pPars
26dcc 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 7b 0a  e->nested==0 ){.
26dcd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26dce 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
26dcf 6c 74 52 6f 77 2c 20 72 65 67 52 6f 77 43 6f 75  ltRow, regRowCou
26dd0 6e 74 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  nt, 1);.    sqli
26dd1 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
26dd2 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c  s(v, 1);.    sql
26dd3 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
26dd4 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45  me(v, 0, COLNAME
26dd5 5f 4e 41 4d 45 2c 20 22 72 6f 77 73 20 75 70 64  _NAME, "rows upd
26dd6 61 74 65 64 22 2c 20 53 51 4c 49 54 45 5f 53 54  ated", SQLITE_ST
26dd7 41 54 49 43 29 3b 0a 20 20 7d 0a 0a 75 70 64 61  ATIC);.  }..upda
26dd8 74 65 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71  te_cleanup:.  sq
26dd9 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74  lite3AuthContext
26dda 50 6f 70 28 26 73 43 6f 6e 74 65 78 74 29 3b 0a  Pop(&sContext);.
26ddb 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
26ddc 64 62 2c 20 61 52 65 67 49 64 78 29 3b 0a 20 20  db, aRegIdx);.  
26ddd 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
26dde 2c 20 61 58 52 65 66 29 3b 0a 20 20 73 71 6c 69  , aXRef);.  sqli
26ddf 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65  te3SrcListDelete
26de0 28 64 62 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a  (db, pTabList);.
26de1 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
26de2 74 44 65 6c 65 74 65 28 64 62 2c 20 70 43 68 61  tDelete(db, pCha
26de3 6e 67 65 73 29 3b 0a 20 20 73 71 6c 69 74 65 33  nges);.  sqlite3
26de4 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
26de5 57 68 65 72 65 29 3b 0a 20 20 72 65 74 75 72 6e  Where);.  return
26de6 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
26de7 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
26de8 54 41 42 4c 45 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  TABLE./*.** Gene
26de9 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61 6e  rate code for an
26dea 20 55 50 44 41 54 45 20 6f 66 20 61 20 76 69 72   UPDATE of a vir
26deb 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a  tual table..**.*
26dec 2a 20 54 68 65 20 73 74 72 61 74 65 67 79 20 69  * The strategy i
26ded 73 20 74 68 61 74 20 77 65 20 63 72 65 61 74 65  s that we create
26dee 20 61 6e 20 65 70 68 65 6d 65 72 69 61 6c 20 74   an ephemerial t
26def 61 62 6c 65 20 74 68 61 74 20 63 6f 6e 74 61 69  able that contai
26df0 6e 73 0a 2a 2a 20 66 6f 72 20 65 61 63 68 20 72  ns.** for each r
26df1 6f 77 20 74 6f 20 62 65 20 63 68 61 6e 67 65 64  ow to be changed
26df2 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 41 29 20 20 54  :.**.**   (A)  T
26df3 68 65 20 6f 72 69 67 69 6e 61 6c 20 72 6f 77 69  he original rowi
26df4 64 20 6f 66 20 74 68 61 74 20 72 6f 77 2e 0a 2a  d of that row..*
26df5 2a 20 20 20 28 42 29 20 20 54 68 65 20 72 65 76  *   (B)  The rev
26df6 69 73 65 64 20 72 6f 77 69 64 20 66 6f 72 20 74  ised rowid for t
26df7 68 65 20 72 6f 77 2e 20 28 6e 6f 74 65 31 29 0a  he row. (note1).
26df8 2a 2a 20 20 20 28 43 29 20 20 54 68 65 20 63 6f  **   (C)  The co
26df9 6e 74 65 6e 74 20 6f 66 20 65 76 65 72 79 20 63  ntent of every c
26dfa 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 6f 77  olumn in the row
26dfb 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 77 65 20  ..**.** Then we 
26dfc 6c 6f 6f 70 20 6f 76 65 72 20 74 68 69 73 20 65  loop over this e
26dfd 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 61  phemeral table a
26dfe 6e 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20  nd for each row 
26dff 69 6e 0a 2a 2a 20 74 68 65 20 65 70 68 65 72 6d  in.** the epherm
26e00 65 72 61 6c 20 74 61 62 6c 65 20 63 61 6c 6c 20  eral table call 
26e01 56 55 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 57  VUpdate..**.** W
26e02 68 65 6e 20 66 69 6e 69 73 68 65 64 2c 20 64 72  hen finished, dr
26e03 6f 70 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c  op the ephemeral
26e04 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 28 6e   table..**.** (n
26e05 6f 74 65 31 29 20 41 63 74 75 61 6c 6c 79 2c 20  ote1) Actually, 
26e06 69 66 20 77 65 20 6b 6e 6f 77 20 69 6e 20 61 64  if we know in ad
26e07 76 61 6e 63 65 20 74 68 61 74 20 28 41 29 20 69  vance that (A) i
26e08 73 20 61 6c 77 61 79 73 20 74 68 65 20 73 61 6d  s always the sam
26e09 65 0a 2a 2a 20 61 73 20 28 42 29 20 77 65 20 6f  e.** as (B) we o
26e0a 6e 6c 79 20 73 74 6f 72 65 20 28 41 29 2c 20 74  nly store (A), t
26e0b 68 65 6e 20 64 75 70 6c 69 63 61 74 65 20 28 41  hen duplicate (A
26e0c 29 20 77 68 65 6e 20 70 75 6c 6c 69 6e 67 0a 2a  ) when pulling.*
26e0d 2a 20 69 74 20 6f 75 74 20 6f 66 20 74 68 65 20  * it out of the 
26e0e 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20  ephemeral table 
26e0f 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 56  before calling V
26e10 55 70 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69  Update..*/.stati
26e11 63 20 76 6f 69 64 20 75 70 64 61 74 65 56 69 72  c void updateVir
26e12 74 75 61 6c 54 61 62 6c 65 28 0a 20 20 50 61 72  tualTable(.  Par
26e13 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
26e14 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67    /* The parsing
26e15 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72   context */.  Sr
26e16 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20  cList *pSrc,    
26e17 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
26e18 6c 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6d 6f  l table to be mo
26e19 64 69 66 69 65 64 20 2a 2f 0a 20 20 54 61 62 6c  dified */.  Tabl
26e1a 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20  e *pTab,        
26e1b 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20   /* The virtual 
26e1c 74 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c  table */.  ExprL
26e1d 69 73 74 20 2a 70 43 68 61 6e 67 65 73 2c 20 20  ist *pChanges,  
26e1e 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 74  /* The columns t
26e1f 6f 20 63 68 61 6e 67 65 20 69 6e 20 74 68 65 20  o change in the 
26e20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74  UPDATE statement
26e21 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 6f 77   */.  Expr *pRow
26e22 69 64 2c 20 20 20 20 20 20 20 20 2f 2a 20 45 78  id,        /* Ex
26e23 70 72 65 73 73 69 6f 6e 20 75 73 65 64 20 74 6f  pression used to
26e24 20 72 65 63 6f 6d 70 75 74 65 20 74 68 65 20 72   recompute the r
26e25 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61  owid */.  int *a
26e26 58 52 65 66 2c 20 20 20 20 20 20 20 20 20 20 2f  XRef,          /
26e27 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 63  * Mapping from c
26e28 6f 6c 75 6d 6e 73 20 6f 66 20 70 54 61 62 20 74  olumns of pTab t
26e29 6f 20 65 6e 74 72 69 65 73 20 69 6e 20 70 43 68  o entries in pCh
26e2a 61 6e 67 65 73 20 2a 2f 0a 20 20 45 78 70 72 20  anges */.  Expr 
26e2b 2a 70 57 68 65 72 65 20 20 20 20 20 20 20 20 20  *pWhere         
26e2c 2f 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  /* WHERE clause 
26e2d 6f 66 20 74 68 65 20 55 50 44 41 54 45 20 73 74  of the UPDATE st
26e2e 61 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20  atement */.){.  
26e2f 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
26e30 2d 3e 70 56 64 62 65 3b 20 20 2f 2a 20 56 69 72  ->pVdbe;  /* Vir
26e31 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 75 6e 64  tual machine und
26e32 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20  er construction 
26e33 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
26e34 45 4c 69 73 74 20 3d 20 30 3b 20 20 20 20 20 2f  EList = 0;     /
26e35 2a 20 54 68 65 20 72 65 73 75 6c 74 20 73 65 74  * The result set
26e36 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73   of the SELECT s
26e37 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65  tatement */.  Se
26e38 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 3d 20  lect *pSelect = 
26e39 30 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53  0;      /* The S
26e3a 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
26e3b 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  */.  Expr *pExpr
26e3c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
26e3d 2a 20 54 65 6d 70 6f 72 61 72 79 20 65 78 70 72  * Temporary expr
26e3e 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ession */.  int 
26e3f 65 70 68 65 6d 54 61 62 3b 20 20 20 20 20 20 20  ephemTab;       
26e40 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68        /* Table h
26e41 6f 6c 64 69 6e 67 20 74 68 65 20 72 65 73 75 6c  olding the resul
26e42 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20  t of the SELECT 
26e43 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  */.  int i;     
26e44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26e45 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
26e46 2f 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20  /.  int addr;   
26e47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
26e48 20 41 64 64 72 65 73 73 20 6f 66 20 74 6f 70 20   Address of top 
26e49 6f 66 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74  of loop */.  int
26e4a 20 69 52 65 67 3b 20 20 20 20 20 20 20 20 20 20   iReg;          
26e4b 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
26e4c 72 65 67 69 73 74 65 72 20 69 6e 20 73 65 74 20  register in set 
26e4d 70 61 73 73 65 64 20 74 6f 20 4f 50 5f 56 55 70  passed to OP_VUp
26e4e 64 61 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  date */.  sqlite
26e4f 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
26e50 64 62 3b 20 2f 2a 20 44 61 74 61 62 61 73 65 20  db; /* Database 
26e51 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
26e52 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 56 74 61  const char *pVta
26e53 62 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a  b = (const char*
26e54 29 70 54 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20  )pTab->pVtab;.  
26e55 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b  SelectDest dest;
26e56 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74  ..  /* Construct
26e57 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
26e58 65 6d 65 6e 74 20 74 68 61 74 20 77 69 6c 6c 20  ement that will 
26e59 66 69 6e 64 20 74 68 65 20 6e 65 77 20 76 61 6c  find the new val
26e5a 75 65 73 20 66 6f 72 0a 20 20 2a 2a 20 61 6c 6c  ues for.  ** all
26e5b 20 75 70 64 61 74 65 64 20 72 6f 77 73 2e 20 0a   updated rows. .
26e5c 20 20 2a 2f 0a 20 20 70 45 4c 69 73 74 20 3d 20    */.  pEList = 
26e5d 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
26e5e 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c  ppend(pParse, 0,
26e5f 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
26e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26e61 20 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65     sqlite3Create
26e62 49 64 45 78 70 72 28 70 50 61 72 73 65 2c 20 22  IdExpr(pParse, "
26e63 5f 72 6f 77 69 64 5f 22 29 2c 20 30 29 3b 0a 20  _rowid_"), 0);. 
26e64 20 69 66 28 20 70 52 6f 77 69 64 20 29 7b 0a 20   if( pRowid ){. 
26e65 20 20 20 70 45 4c 69 73 74 20 3d 20 73 71 6c 69     pEList = sqli
26e66 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
26e67 64 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74  d(pParse, pEList
26e68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
26e69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26e6a 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
26e6b 44 75 70 28 64 62 2c 20 70 52 6f 77 69 64 29 2c  Dup(db, pRowid),
26e6c 20 30 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72   0);.  }.  asser
26e6d 74 28 20 70 54 61 62 2d 3e 69 50 4b 65 79 3c 30  t( pTab->iPKey<0
26e6e 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   );.  for(i=0; i
26e6f 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  <pTab->nCol; i++
26e70 29 7b 0a 20 20 20 20 69 66 28 20 61 58 52 65 66  ){.    if( aXRef
26e71 5b 69 5d 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  [i]>=0 ){.      
26e72 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45  pExpr = sqlite3E
26e73 78 70 72 44 75 70 28 64 62 2c 20 70 43 68 61 6e  xprDup(db, pChan
26e74 67 65 73 2d 3e 61 5b 61 58 52 65 66 5b 69 5d 5d  ges->a[aXRef[i]]
26e75 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 65 6c  .pExpr);.    }el
26e76 73 65 7b 0a 20 20 20 20 20 20 70 45 78 70 72 20  se{.      pExpr 
26e77 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 49  = sqlite3CreateI
26e78 64 45 78 70 72 28 70 50 61 72 73 65 2c 20 70 54  dExpr(pParse, pT
26e79 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d  ab->aCol[i].zNam
26e7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 45  e);.    }.    pE
26e7b 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78  List = sqlite3Ex
26e7c 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
26e7d 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 70 45 78  rse, pEList, pEx
26e7e 70 72 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70 53  pr, 0);.  }.  pS
26e7f 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53  elect = sqlite3S
26e80 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c  electNew(pParse,
26e81 20 70 45 4c 69 73 74 2c 20 70 53 72 63 2c 20 70   pEList, pSrc, p
26e82 57 68 65 72 65 2c 20 30 2c 20 30 2c 20 30 2c 20  Where, 0, 0, 0, 
26e83 30 2c 20 30 2c 20 30 29 3b 0a 20 20 0a 20 20 2f  0, 0, 0);.  .  /
26e84 2a 20 43 72 65 61 74 65 20 74 68 65 20 65 70 68  * Create the eph
26e85 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 69 6e 74  emeral table int
26e86 6f 20 77 68 69 63 68 20 74 68 65 20 75 70 64 61  o which the upda
26e87 74 65 20 72 65 73 75 6c 74 73 20 77 69 6c 6c 0a  te results will.
26e88 20 20 2a 2a 20 62 65 20 73 74 6f 72 65 64 2e 0a    ** be stored..
26e89 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 76    */.  assert( v
26e8a 20 29 3b 0a 20 20 65 70 68 65 6d 54 61 62 20 3d   );.  ephemTab =
26e8b 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
26e8c 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
26e8d 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45  dOp2(v, OP_OpenE
26e8e 70 68 65 6d 65 72 61 6c 2c 20 65 70 68 65 6d 54  phemeral, ephemT
26e8f 61 62 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31  ab, pTab->nCol+1
26e90 2b 28 70 52 6f 77 69 64 21 3d 30 29 29 3b 0a 0a  +(pRowid!=0));..
26e91 20 20 2f 2a 20 66 69 6c 6c 20 74 68 65 20 65 70    /* fill the ep
26e92 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 0a 20  hemeral table . 
26e93 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 53 65 6c   */.  sqlite3Sel
26e94 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73  ectDestInit(&des
26e95 74 2c 20 53 52 54 5f 54 61 62 6c 65 2c 20 65 70  t, SRT_Table, ep
26e96 68 65 6d 54 61 62 29 3b 0a 20 20 73 71 6c 69 74  hemTab);.  sqlit
26e97 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
26e98 20 70 53 65 6c 65 63 74 2c 20 26 64 65 73 74 29   pSelect, &dest)
26e99 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  ;..  /* Generate
26e9a 20 63 6f 64 65 20 74 6f 20 73 63 61 6e 20 74 68   code to scan th
26e9b 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c  e ephemeral tabl
26e9c 65 20 61 6e 64 20 63 61 6c 6c 20 56 55 70 64 61  e and call VUpda
26e9d 74 65 2e 20 2a 2f 0a 20 20 69 52 65 67 20 3d 20  te. */.  iReg = 
26e9e 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
26e9f 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b    pParse->nMem +
26ea0 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 3b 0a  = pTab->nCol+1;.
26ea1 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26ea2 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64  Op2(v, OP_Rewind
26ea3 2c 20 65 70 68 65 6d 54 61 62 2c 20 30 29 3b 0a  , ephemTab, 0);.
26ea4 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
26ea5 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
26ea6 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  v);.  sqlite3Vdb
26ea7 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
26ea8 6c 75 6d 6e 2c 20 20 65 70 68 65 6d 54 61 62 2c  lumn,  ephemTab,
26ea9 20 30 2c 20 69 52 65 67 29 3b 0a 20 20 73 71 6c   0, iReg);.  sql
26eaa 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
26eab 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 65 70 68  , OP_Column, eph
26eac 65 6d 54 61 62 2c 20 28 70 52 6f 77 69 64 3f 31  emTab, (pRowid?1
26ead 3a 30 29 2c 20 69 52 65 67 2b 31 29 3b 0a 20 20  :0), iReg+1);.  
26eae 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d  for(i=0; i<pTab-
26eaf 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nCol; i++){.   
26eb0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26eb1 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
26eb2 20 65 70 68 65 6d 54 61 62 2c 20 69 2b 31 2b 28   ephemTab, i+1+(
26eb3 70 52 6f 77 69 64 21 3d 30 29 2c 20 69 52 65 67  pRowid!=0), iReg
26eb4 2b 32 2b 69 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  +2+i);.  }.  sql
26eb5 69 74 65 33 56 74 61 62 4d 61 6b 65 57 72 69 74  ite3VtabMakeWrit
26eb6 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61  able(pParse, pTa
26eb7 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  b);.  sqlite3Vdb
26eb8 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 55  eAddOp4(v, OP_VU
26eb9 70 64 61 74 65 2c 20 30 2c 20 70 54 61 62 2d 3e  pdate, 0, pTab->
26eba 6e 43 6f 6c 2b 32 2c 20 69 52 65 67 2c 20 70 56  nCol+2, iReg, pV
26ebb 74 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20  tab, P4_VTAB);. 
26ebc 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26ebd 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 65  p2(v, OP_Next, e
26ebe 70 68 65 6d 54 61 62 2c 20 61 64 64 72 29 3b 0a  phemTab, addr);.
26ebf 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
26ec0 70 48 65 72 65 28 76 2c 20 61 64 64 72 2d 31 29  pHere(v, addr-1)
26ec1 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
26ec2 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp2(v, OP_Clos
26ec3 65 2c 20 65 70 68 65 6d 54 61 62 2c 20 30 29 3b  e, ephemTab, 0);
26ec4 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 75 70 20 2a  ..  /* Cleanup *
26ec5 2f 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  /.  sqlite3Selec
26ec6 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c  tDelete(db, pSel
26ec7 65 63 74 29 3b 20 20 0a 7d 0a 23 65 6e 64 69 66  ect);  .}.#endif
26ec8 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
26ec9 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a  VIRTUALTABLE */.
26eca 0a 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 22 69  ./* Make sure "i
26ecb 73 56 69 65 77 22 20 67 65 74 73 20 75 6e 64 65  sView" gets unde
26ecc 66 69 6e 65 64 20 69 6e 20 63 61 73 65 20 74 68  fined in case th
26ecd 69 73 20 66 69 6c 65 20 62 65 63 6f 6d 65 73 20  is file becomes 
26ece 70 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61  part of.** the a
26ecf 6d 61 6c 67 61 6d 61 74 69 6f 6e 20 2d 20 73 6f  malgamation - so
26ed0 20 74 68 61 74 20 73 75 62 73 65 71 75 65 6e 74   that subsequent
26ed1 20 66 69 6c 65 73 20 64 6f 20 6e 6f 74 20 73 65   files do not se
26ed2 65 20 69 73 56 69 65 77 20 61 73 20 61 0a 2a 2a  e isView as a.**
26ed3 20 6d 61 63 72 6f 2e 20 2a 2f 0a 23 75 6e 64 65   macro. */.#unde
26ed4 66 20 69 73 56 69 65 77 0a 0a 2f 2a 2a 2a 2a 2a  f isView../*****
26ed5 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
26ed6 20 75 70 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a   update.c ******
26ed7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26ed8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26ed9 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
26eda 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
26edb 66 69 6c 65 20 76 61 63 75 75 6d 2e 63 20 2a 2a  file vacuum.c **
26edc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26edd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26ede 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
26edf 32 30 30 33 20 41 70 72 69 6c 20 36 0a 2a 2a 0a  2003 April 6.**.
26ee0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
26ee1 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
26ee2 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
26ee3 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
26ee4 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
26ee5 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
26ee6 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
26ee7 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
26ee8 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
26ee9 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
26eea 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
26eeb 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
26eec 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
26eed 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
26eee 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
26eef 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
26ef0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
26ef1 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
26ef2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26ef3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26ef4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26ef5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
26ef6 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
26ef7 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69  s code used to i
26ef8 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 56 41 43  mplement the VAC
26ef9 55 55 4d 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a  UUM command..**.
26efa 2a 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 63  ** Most of the c
26efb 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ode in this file
26efc 20 6d 61 79 20 62 65 20 6f 6d 69 74 74 65 64 20   may be omitted 
26efd 62 79 20 64 65 66 69 6e 69 6e 67 20 74 68 65 0a  by defining the.
26efe 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ** SQLITE_OMIT_V
26eff 41 43 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2a 0a  ACUUM macro..**.
26f00 2a 2a 20 24 49 64 3a 20 76 61 63 75 75 6d 2e 63  ** $Id: vacuum.c
26f01 2c 76 20 31 2e 38 33 20 32 30 30 38 2f 30 38 2f  ,v 1.83 2008/08/
26f02 32 36 20 32 31 3a 30 37 3a 32 37 20 64 72 68 20  26 21:07:27 drh 
26f03 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 20 21 64  Exp $.*/..#if !d
26f04 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
26f05 49 54 5f 56 41 43 55 55 4d 29 20 26 26 20 21 64  IT_VACUUM) && !d
26f06 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
26f07 49 54 5f 41 54 54 41 43 48 29 0a 2f 2a 0a 2a 2a  IT_ATTACH)./*.**
26f08 20 45 78 65 63 75 74 65 20 7a 53 71 6c 20 6f 6e   Execute zSql on
26f09 20 64 61 74 61 62 61 73 65 20 64 62 2e 20 52 65   database db. Re
26f0a 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f  turn an error co
26f0b 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  de..*/.static in
26f0c 74 20 65 78 65 63 53 71 6c 28 73 71 6c 69 74 65  t execSql(sqlite
26f0d 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
26f0e 72 20 2a 7a 53 71 6c 29 7b 0a 20 20 73 71 6c 69  r *zSql){.  sqli
26f0f 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b  te3_stmt *pStmt;
26f10 0a 20 20 69 66 28 20 21 7a 53 71 6c 20 29 7b 0a  .  if( !zSql ){.
26f11 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
26f12 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69  E_NOMEM;.  }.  i
26f13 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71  f( SQLITE_OK!=sq
26f14 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 64 62  lite3_prepare(db
26f15 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74  , zSql, -1, &pSt
26f16 6d 74 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65  mt, 0) ){.    re
26f17 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 65 72 72  turn sqlite3_err
26f18 63 6f 64 65 28 64 62 29 3b 0a 20 20 7d 0a 20 20  code(db);.  }.  
26f19 77 68 69 6c 65 28 20 53 51 4c 49 54 45 5f 52 4f  while( SQLITE_RO
26f1a 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28  W==sqlite3_step(
26f1b 70 53 74 6d 74 29 20 29 7b 7d 0a 20 20 72 65 74  pStmt) ){}.  ret
26f1c 75 72 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  urn sqlite3_fina
26f1d 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a  lize(pStmt);.}..
26f1e 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 7a 53  /*.** Execute zS
26f1f 71 6c 20 6f 6e 20 64 61 74 61 62 61 73 65 20 64  ql on database d
26f20 62 2e 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74  b. The statement
26f21 20 72 65 74 75 72 6e 73 20 65 78 61 63 74 6c 79   returns exactly
26f22 0a 2a 2a 20 6f 6e 65 20 63 6f 6c 75 6d 6e 2e 20  .** one column. 
26f23 45 78 65 63 75 74 65 20 74 68 69 73 20 61 73 20  Execute this as 
26f24 53 51 4c 20 6f 6e 20 74 68 65 20 73 61 6d 65 20  SQL on the same 
26f25 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61  database..*/.sta
26f26 74 69 63 20 69 6e 74 20 65 78 65 63 45 78 65 63  tic int execExec
26f27 53 71 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  Sql(sqlite3 *db,
26f28 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71   const char *zSq
26f29 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  l){.  sqlite3_st
26f2a 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 69 6e 74  mt *pStmt;.  int
26f2b 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
26f2c 69 74 65 33 5f 70 72 65 70 61 72 65 28 64 62 2c  ite3_prepare(db,
26f2d 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d   zSql, -1, &pStm
26f2e 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21  t, 0);.  if( rc!
26f2f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
26f30 75 72 6e 20 72 63 3b 0a 0a 20 20 77 68 69 6c 65  urn rc;..  while
26f31 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71  ( SQLITE_ROW==sq
26f32 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74  lite3_step(pStmt
26f33 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 65 78  ) ){.    rc = ex
26f34 65 63 53 71 6c 28 64 62 2c 20 28 63 68 61 72 2a  ecSql(db, (char*
26f35 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  )sqlite3_column_
26f36 74 65 78 74 28 70 53 74 6d 74 2c 20 30 29 29 3b  text(pStmt, 0));
26f37 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
26f38 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
26f39 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
26f3a 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 20 20 72  (pStmt);.      r
26f3b 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
26f3c 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 73 71    }..  return sq
26f3d 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70  lite3_finalize(p
26f3e 53 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Stmt);.}../*.** 
26f3f 54 68 65 20 6e 6f 6e 2d 73 74 61 6e 64 61 72 64  The non-standard
26f40 20 56 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 20   VACUUM command 
26f41 69 73 20 75 73 65 64 20 74 6f 20 63 6c 65 61 6e  is used to clean
26f42 20 75 70 20 74 68 65 20 64 61 74 61 62 61 73 65   up the database
26f43 2c 0a 2a 2a 20 63 6f 6c 6c 61 70 73 65 20 66 72  ,.** collapse fr
26f44 65 65 20 73 70 61 63 65 2c 20 65 74 63 2e 20 20  ee space, etc.  
26f45 49 74 20 69 73 20 6d 6f 64 65 6c 6c 65 64 20 61  It is modelled a
26f46 66 74 65 72 20 74 68 65 20 56 41 43 55 55 4d 20  fter the VACUUM 
26f47 63 6f 6d 6d 61 6e 64 0a 2a 2a 20 69 6e 20 50 6f  command.** in Po
26f48 73 74 67 72 65 53 51 4c 2e 0a 2a 2a 0a 2a 2a 20  stgreSQL..**.** 
26f49 49 6e 20 76 65 72 73 69 6f 6e 20 31 2e 30 2e 78  In version 1.0.x
26f4a 20 6f 66 20 53 51 4c 69 74 65 2c 20 74 68 65 20   of SQLite, the 
26f4b 56 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 20 77  VACUUM command w
26f4c 6f 75 6c 64 20 63 61 6c 6c 0a 2a 2a 20 67 64 62  ould call.** gdb
26f4d 6d 5f 72 65 6f 72 67 61 6e 69 7a 65 28 29 20 6f  m_reorganize() o
26f4e 6e 20 61 6c 6c 20 74 68 65 20 64 61 74 61 62 61  n all the databa
26f4f 73 65 20 74 61 62 6c 65 73 2e 20 20 42 75 74 20  se tables.  But 
26f50 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69 74  beginning.** wit
26f51 68 20 32 2e 30 2e 30 2c 20 53 51 4c 69 74 65 20  h 2.0.0, SQLite 
26f52 6e 6f 20 6c 6f 6e 67 65 72 20 75 73 65 73 20 47  no longer uses G
26f53 44 42 4d 20 73 6f 20 74 68 69 73 20 63 6f 6d 6d  DBM so this comm
26f54 61 6e 64 20 68 61 73 0a 2a 2a 20 62 65 63 6f 6d  and has.** becom
26f55 65 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51  e a no-op..*/.SQ
26f56 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
26f57 64 20 73 71 6c 69 74 65 33 56 61 63 75 75 6d 28  d sqlite3Vacuum(
26f58 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a  Parse *pParse){.
26f59 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69    Vdbe *v = sqli
26f5a 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
26f5b 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20  e);.  if( v ){. 
26f5c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26f5d 64 4f 70 32 28 76 2c 20 4f 50 5f 56 61 63 75 75  dOp2(v, OP_Vacuu
26f5e 6d 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  m, 0, 0);.  }.  
26f5f 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  return;.}../*.**
26f60 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d   This routine im
26f61 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 4f 50 5f  plements the OP_
26f62 56 61 63 75 75 6d 20 6f 70 63 6f 64 65 20 6f 66  Vacuum opcode of
26f63 20 74 68 65 20 56 44 42 45 2e 0a 2a 2f 0a 53 51   the VDBE..*/.SQ
26f64 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
26f65 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75 75   sqlite3RunVacuu
26f66 6d 28 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73  m(char **pzErrMs
26f67 67 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  g, sqlite3 *db){
26f68 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
26f69 54 45 5f 4f 4b 3b 20 20 20 20 20 2f 2a 20 52 65  TE_OK;     /* Re
26f6a 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73  turn code from s
26f6b 65 72 76 69 63 65 20 72 6f 75 74 69 6e 65 73 20  ervice routines 
26f6c 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 4d 61 69  */.  Btree *pMai
26f6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  n;           /* 
26f6e 54 68 65 20 64 61 74 61 62 61 73 65 20 62 65 69  The database bei
26f6f 6e 67 20 76 61 63 75 75 6d 65 64 20 2a 2f 0a 20  ng vacuumed */. 
26f70 20 50 61 67 65 72 20 2a 70 4d 61 69 6e 50 61 67   Pager *pMainPag
26f71 65 72 3b 20 20 20 20 20 20 2f 2a 20 50 61 67 65  er;      /* Page
26f72 72 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 62  r for database b
26f73 65 69 6e 67 20 76 61 63 75 75 6d 65 64 20 2a 2f  eing vacuumed */
26f74 0a 20 20 42 74 72 65 65 20 2a 70 54 65 6d 70 3b  .  Btree *pTemp;
26f75 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
26f76 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61  e temporary data
26f77 62 61 73 65 20 77 65 20 76 61 63 75 75 6d 20 69  base we vacuum i
26f78 6e 74 6f 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  nto */.  char *z
26f79 53 71 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Sql = 0;        
26f7a 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   /* SQL statemen
26f7b 74 73 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65  ts */.  int save
26f7c 64 5f 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20  d_flags;        
26f7d 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65 20 6f  /* Saved value o
26f7e 66 20 74 68 65 20 64 62 2d 3e 66 6c 61 67 73 20  f the db->flags 
26f7f 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 64 5f 6e  */.  int saved_n
26f80 43 68 61 6e 67 65 3b 20 20 20 20 20 20 2f 2a 20  Change;      /* 
26f81 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66 20 64  Saved value of d
26f82 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f 0a 20 20  b->nChange */.  
26f83 69 6e 74 20 73 61 76 65 64 5f 6e 54 6f 74 61 6c  int saved_nTotal
26f84 43 68 61 6e 67 65 3b 20 2f 2a 20 53 61 76 65 64  Change; /* Saved
26f85 20 76 61 6c 75 65 20 6f 66 20 64 62 2d 3e 6e 54   value of db->nT
26f86 6f 74 61 6c 43 68 61 6e 67 65 20 2a 2f 0a 20 20  otalChange */.  
26f87 44 62 20 2a 70 44 62 20 3d 20 30 3b 20 20 20 20  Db *pDb = 0;    
26f88 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
26f89 61 73 65 20 74 6f 20 64 65 74 61 63 68 20 61 74  ase to detach at
26f8a 20 65 6e 64 20 6f 66 20 76 61 63 75 75 6d 20 2a   end of vacuum *
26f8b 2f 0a 20 20 69 6e 74 20 69 73 4d 65 6d 44 62 3b  /.  int isMemDb;
26f8c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
26f8d 72 75 65 20 69 73 20 76 61 63 75 75 6d 69 6e 67  rue is vacuuming
26f8e 20 61 20 3a 6d 65 6d 6f 72 79 3a 20 64 61 74 61   a :memory: data
26f8f 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  base */.  int nR
26f90 65 73 3b 0a 0a 20 20 2f 2a 20 53 61 76 65 20 74  es;..  /* Save t
26f91 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65  he current value
26f92 20 6f 66 20 74 68 65 20 77 72 69 74 65 2d 73 63   of the write-sc
26f93 68 65 6d 61 20 66 6c 61 67 20 62 65 66 6f 72 65  hema flag before
26f94 20 73 65 74 74 69 6e 67 20 69 74 2e 20 2a 2f 0a   setting it. */.
26f95 20 20 73 61 76 65 64 5f 66 6c 61 67 73 20 3d 20    saved_flags = 
26f96 64 62 2d 3e 66 6c 61 67 73 3b 0a 20 20 73 61 76  db->flags;.  sav
26f97 65 64 5f 6e 43 68 61 6e 67 65 20 3d 20 64 62 2d  ed_nChange = db-
26f98 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 73 61 76 65  >nChange;.  save
26f99 64 5f 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20 3d  d_nTotalChange =
26f9a 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67   db->nTotalChang
26f9b 65 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c  e;.  db->flags |
26f9c 3d 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63  = SQLITE_WriteSc
26f9d 68 65 6d 61 20 7c 20 53 51 4c 49 54 45 5f 49 67  hema | SQLITE_Ig
26f9e 6e 6f 72 65 43 68 65 63 6b 73 3b 0a 0a 20 20 69  noreChecks;..  i
26f9f 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d  f( !db->autoComm
26fa0 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  it ){.    sqlite
26fa1 33 53 65 74 53 74 72 69 6e 67 28 70 7a 45 72 72  3SetString(pzErr
26fa2 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74  Msg, db, "cannot
26fa3 20 56 41 43 55 55 4d 20 66 72 6f 6d 20 77 69 74   VACUUM from wit
26fa4 68 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f  hin a transactio
26fa5 6e 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  n");.    rc = SQ
26fa6 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
26fa7 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75  goto end_of_vacu
26fa8 75 6d 3b 0a 20 20 7d 0a 20 20 70 4d 61 69 6e 20  um;.  }.  pMain 
26fa9 3d 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74  = db->aDb[0].pBt
26faa 3b 0a 20 20 70 4d 61 69 6e 50 61 67 65 72 20 3d  ;.  pMainPager =
26fab 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67   sqlite3BtreePag
26fac 65 72 28 70 4d 61 69 6e 29 3b 0a 20 20 69 73 4d  er(pMain);.  isM
26fad 65 6d 44 62 20 3d 20 73 71 6c 69 74 65 33 50 61  emDb = sqlite3Pa
26fae 67 65 72 46 69 6c 65 28 70 4d 61 69 6e 50 61 67  gerFile(pMainPag
26faf 65 72 29 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 30  er)->pMethods==0
26fb0 3b 0a 0a 20 20 2f 2a 20 41 74 74 61 63 68 20 74  ;..  /* Attach t
26fb1 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74  he temporary dat
26fb2 61 62 61 73 65 20 61 73 20 27 76 61 63 75 75 6d  abase as 'vacuum
26fb3 5f 64 62 27 2e 20 54 68 65 20 73 79 6e 63 68 72  _db'. The synchr
26fb4 6f 6e 6f 75 73 20 70 72 61 67 6d 61 0a 20 20 2a  onous pragma.  *
26fb5 2a 20 63 61 6e 20 62 65 20 73 65 74 20 74 6f 20  * can be set to 
26fb6 27 6f 66 66 27 20 66 6f 72 20 74 68 69 73 20 66  'off' for this f
26fb7 69 6c 65 2c 20 61 73 20 69 74 20 69 73 20 6e 6f  ile, as it is no
26fb8 74 20 72 65 63 6f 76 65 72 65 64 20 69 66 20 61  t recovered if a
26fb9 20 63 72 61 73 68 0a 20 20 2a 2a 20 6f 63 63 75   crash.  ** occu
26fba 72 73 20 61 6e 79 77 61 79 2e 20 54 68 65 20 69  rs anyway. The i
26fbb 6e 74 65 67 72 69 74 79 20 6f 66 20 74 68 65 20  ntegrity of the 
26fbc 64 61 74 61 62 61 73 65 20 69 73 20 6d 61 69 6e  database is main
26fbd 74 61 69 6e 65 64 20 62 79 20 61 0a 20 20 2a 2a  tained by a.  **
26fbe 20 28 70 6f 73 73 69 62 6c 79 20 73 79 6e 63 68   (possibly synch
26fbf 72 6f 6e 6f 75 73 29 20 74 72 61 6e 73 61 63 74  ronous) transact
26fc0 69 6f 6e 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68  ion opened on th
26fc1 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
26fc2 62 65 66 6f 72 65 0a 20 20 2a 2a 20 73 71 6c 69  before.  ** sqli
26fc3 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c 65  te3BtreeCopyFile
26fc4 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20  () is called..  
26fc5 2a 2a 0a 20 20 2a 2a 20 41 6e 20 6f 70 74 69 6d  **.  ** An optim
26fc6 69 73 61 74 69 6f 6e 20 77 6f 75 6c 64 20 62 65  isation would be
26fc7 20 74 6f 20 75 73 65 20 61 20 6e 6f 6e 2d 6a 6f   to use a non-jo
26fc8 75 72 6e 61 6c 65 64 20 70 61 67 65 72 2e 0a 20  urnaled pager.. 
26fc9 20 2a 2a 20 28 4c 61 74 65 72 3a 29 20 49 20 74   ** (Later:) I t
26fca 72 69 65 64 20 73 65 74 74 69 6e 67 20 22 50 52  ried setting "PR
26fcb 41 47 4d 41 20 76 61 63 75 75 6d 5f 64 62 2e 6a  AGMA vacuum_db.j
26fcc 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4f 46 46 22  ournal_mode=OFF"
26fcd 20 62 75 74 0a 20 20 2a 2a 20 74 68 61 74 20 61   but.  ** that a
26fce 63 74 75 61 6c 6c 79 20 6d 61 64 65 20 74 68 65  ctually made the
26fcf 20 56 41 43 55 55 4d 20 72 75 6e 20 73 6c 6f 77   VACUUM run slow
26fd0 65 72 2e 20 20 56 65 72 79 20 6c 69 74 74 6c 65  er.  Very little
26fd1 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 0a 20 20 2a   journalling.  *
26fd2 2a 20 61 63 74 75 61 6c 6c 79 20 6f 63 63 75 72  * actually occur
26fd3 73 20 77 68 65 6e 20 64 6f 69 6e 67 20 61 20 76  s when doing a v
26fd4 61 63 75 75 6d 20 73 69 6e 63 65 20 74 68 65 20  acuum since the 
26fd5 76 61 63 75 75 6d 5f 64 62 20 69 73 20 69 6e 69  vacuum_db is ini
26fd6 74 69 61 6c 6c 79 0a 20 20 2a 2a 20 65 6d 70 74  tially.  ** empt
26fd7 79 2e 20 20 4f 6e 6c 79 20 74 68 65 20 6a 6f 75  y.  Only the jou
26fd8 72 6e 61 6c 20 68 65 61 64 65 72 20 69 73 20 77  rnal header is w
26fd9 72 69 74 74 65 6e 2e 20 20 41 70 70 61 72 65 6e  ritten.  Apparen
26fda 74 6c 79 20 69 74 20 74 61 6b 65 73 20 6d 6f 72  tly it takes mor
26fdb 65 0a 20 20 2a 2a 20 74 69 6d 65 20 74 6f 20 70  e.  ** time to p
26fdc 61 72 73 65 20 61 6e 64 20 72 75 6e 20 74 68 65  arse and run the
26fdd 20 50 52 41 47 4d 41 20 74 6f 20 74 75 72 6e 20   PRAGMA to turn 
26fde 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 6f 66 66 20  journalling off 
26fdf 74 68 61 6e 20 69 74 20 64 6f 65 73 0a 20 20 2a  than it does.  *
26fe0 2a 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 6a  * to write the j
26fe1 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 69  ournal header fi
26fe2 6c 65 2e 0a 20 20 2a 2f 0a 20 20 7a 53 71 6c 20  le..  */.  zSql 
26fe3 3d 20 22 41 54 54 41 43 48 20 27 27 20 41 53 20  = "ATTACH '' AS 
26fe4 76 61 63 75 75 6d 5f 64 62 3b 22 3b 0a 20 20 72  vacuum_db;";.  r
26fe5 63 20 3d 20 65 78 65 63 53 71 6c 28 64 62 2c 20  c = execSql(db, 
26fe6 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20 72 63 21  zSql);.  if( rc!
26fe7 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
26fe8 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b  o end_of_vacuum;
26fe9 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44  .  pDb = &db->aD
26fea 62 5b 64 62 2d 3e 6e 44 62 2d 31 5d 3b 0a 20 20  b[db->nDb-1];.  
26feb 61 73 73 65 72 74 28 20 73 74 72 63 6d 70 28 64  assert( strcmp(d
26fec 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44 62 2d 31  b->aDb[db->nDb-1
26fed 5d 2e 7a 4e 61 6d 65 2c 22 76 61 63 75 75 6d 5f  ].zName,"vacuum_
26fee 64 62 22 29 3d 3d 30 20 29 3b 0a 20 20 70 54 65  db")==0 );.  pTe
26fef 6d 70 20 3d 20 64 62 2d 3e 61 44 62 5b 64 62 2d  mp = db->aDb[db-
26ff0 3e 6e 44 62 2d 31 5d 2e 70 42 74 3b 0a 0a 20 20  >nDb-1].pBt;..  
26ff1 6e 52 65 73 20 3d 20 73 71 6c 69 74 65 33 42 74  nRes = sqlite3Bt
26ff2 72 65 65 47 65 74 52 65 73 65 72 76 65 28 70 4d  reeGetReserve(pM
26ff3 61 69 6e 29 3b 0a 0a 20 20 2f 2a 20 41 20 56 41  ain);..  /* A VA
26ff4 43 55 55 4d 20 63 61 6e 6e 6f 74 20 63 68 61 6e  CUUM cannot chan
26ff5 67 65 20 74 68 65 20 70 61 67 65 73 69 7a 65 20  ge the pagesize 
26ff6 6f 66 20 61 6e 20 65 6e 63 72 79 70 74 65 64 20  of an encrypted 
26ff7 64 61 74 61 62 61 73 65 2e 20 2a 2f 0a 23 69 66  database. */.#if
26ff8 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43  def SQLITE_HAS_C
26ff9 4f 44 45 43 0a 20 20 69 66 28 20 64 62 2d 3e 6e  ODEC.  if( db->n
26ffa 65 78 74 50 61 67 65 73 69 7a 65 20 29 7b 0a 20  extPagesize ){. 
26ffb 20 20 20 65 78 74 65 72 6e 20 76 6f 69 64 20 73     extern void s
26ffc 71 6c 69 74 65 33 43 6f 64 65 63 47 65 74 4b 65  qlite3CodecGetKe
26ffd 79 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c  y(sqlite3*, int,
26ffe 20 76 6f 69 64 2a 2a 2c 20 69 6e 74 2a 29 3b 0a   void**, int*);.
26fff 20 20 20 20 69 6e 74 20 6e 4b 65 79 3b 0a 20 20      int nKey;.  
27000 20 20 63 68 61 72 20 2a 7a 4b 65 79 3b 0a 20 20    char *zKey;.  
27001 20 20 73 71 6c 69 74 65 33 43 6f 64 65 63 47 65    sqlite3CodecGe
27002 74 4b 65 79 28 64 62 2c 20 30 2c 20 28 76 6f 69  tKey(db, 0, (voi
27003 64 2a 2a 29 26 7a 4b 65 79 2c 20 26 6e 4b 65 79  d**)&zKey, &nKey
27004 29 3b 0a 20 20 20 20 69 66 28 20 6e 4b 65 79 20  );.    if( nKey 
27005 29 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73 69  ) db->nextPagesi
27006 7a 65 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e 64  ze = 0;.  }.#end
27007 69 66 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65  if..  if( sqlite
27008 33 42 74 72 65 65 53 65 74 50 61 67 65 53 69 7a  3BtreeSetPageSiz
27009 65 28 70 54 65 6d 70 2c 20 73 71 6c 69 74 65 33  e(pTemp, sqlite3
2700a 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65  BtreeGetPageSize
2700b 28 70 4d 61 69 6e 29 2c 20 6e 52 65 73 29 0a 20  (pMain), nRes). 
2700c 20 20 7c 7c 20 28 21 69 73 4d 65 6d 44 62 20 26    || (!isMemDb &
2700d 26 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  & sqlite3BtreeSe
2700e 74 50 61 67 65 53 69 7a 65 28 70 54 65 6d 70 2c  tPageSize(pTemp,
2700f 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a   db->nextPagesiz
27010 65 2c 20 6e 52 65 73 29 29 0a 20 20 20 7c 7c 20  e, nRes)).   || 
27011 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
27012 20 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20   .  ){.    rc = 
27013 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
27014 20 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61    goto end_of_va
27015 63 75 75 6d 3b 0a 20 20 7d 0a 20 20 72 63 20 3d  cuum;.  }.  rc =
27016 20 65 78 65 63 53 71 6c 28 64 62 2c 20 22 50 52   execSql(db, "PR
27017 41 47 4d 41 20 76 61 63 75 75 6d 5f 64 62 2e 73  AGMA vacuum_db.s
27018 79 6e 63 68 72 6f 6e 6f 75 73 3d 4f 46 46 22 29  ynchronous=OFF")
27019 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
2701a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74  TE_OK ){.    got
2701b 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b  o end_of_vacuum;
2701c 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  .  }..#ifndef SQ
2701d 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
2701e 43 55 55 4d 0a 20 20 73 71 6c 69 74 65 33 42 74  CUUM.  sqlite3Bt
2701f 72 65 65 53 65 74 41 75 74 6f 56 61 63 75 75 6d  reeSetAutoVacuum
27020 28 70 54 65 6d 70 2c 20 64 62 2d 3e 6e 65 78 74  (pTemp, db->next
27021 41 75 74 6f 76 61 63 3e 3d 30 20 3f 20 64 62 2d  Autovac>=0 ? db-
27022 3e 6e 65 78 74 41 75 74 6f 76 61 63 20 3a 0a 20  >nextAutovac :. 
27023 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27024 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27025 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
27026 33 42 74 72 65 65 47 65 74 41 75 74 6f 56 61 63  3BtreeGetAutoVac
27027 75 75 6d 28 70 4d 61 69 6e 29 29 3b 0a 23 65 6e  uum(pMain));.#en
27028 64 69 66 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20  dif..  /* Begin 
27029 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f  a transaction */
2702a 0a 20 20 72 63 20 3d 20 65 78 65 63 53 71 6c 28  .  rc = execSql(
2702b 64 62 2c 20 22 42 45 47 49 4e 20 45 58 43 4c 55  db, "BEGIN EXCLU
2702c 53 49 56 45 3b 22 29 3b 0a 20 20 69 66 28 20 72  SIVE;");.  if( r
2702d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
2702e 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75  oto end_of_vacuu
2702f 6d 3b 0a 0a 20 20 2f 2a 20 51 75 65 72 79 20 74  m;..  /* Query t
27030 68 65 20 73 63 68 65 6d 61 20 6f 66 20 74 68 65  he schema of the
27031 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20   main database. 
27032 43 72 65 61 74 65 20 61 20 6d 69 72 72 6f 72 20  Create a mirror 
27033 73 63 68 65 6d 61 0a 20 20 2a 2a 20 69 6e 20 74  schema.  ** in t
27034 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74  he temporary dat
27035 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 72 63  abase..  */.  rc
27036 20 3d 20 65 78 65 63 45 78 65 63 53 71 6c 28 64   = execExecSql(d
27037 62 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43  b, .      "SELEC
27038 54 20 27 43 52 45 41 54 45 20 54 41 42 4c 45 20  T 'CREATE TABLE 
27039 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 73  vacuum_db.' || s
2703a 75 62 73 74 72 28 73 71 6c 2c 31 34 29 20 22 0a  ubstr(sql,14) ".
2703b 20 20 20 20 20 20 22 20 20 46 52 4f 4d 20 73 71        "  FROM sq
2703c 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45 52  lite_master WHER
2703d 45 20 74 79 70 65 3d 27 74 61 62 6c 65 27 20 41  E type='table' A
2703e 4e 44 20 6e 61 6d 65 21 3d 27 73 71 6c 69 74 65  ND name!='sqlite
2703f 5f 73 65 71 75 65 6e 63 65 27 22 0a 20 20 20 20  _sequence'".    
27040 20 20 22 20 20 20 41 4e 44 20 72 6f 6f 74 70 61    "   AND rootpa
27041 67 65 3e 30 22 0a 20 20 29 3b 0a 20 20 69 66 28  ge>0".  );.  if(
27042 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
27043 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63   goto end_of_vac
27044 75 75 6d 3b 0a 20 20 72 63 20 3d 20 65 78 65 63  uum;.  rc = exec
27045 45 78 65 63 53 71 6c 28 64 62 2c 20 0a 20 20 20  ExecSql(db, .   
27046 20 20 20 22 53 45 4c 45 43 54 20 27 43 52 45 41     "SELECT 'CREA
27047 54 45 20 49 4e 44 45 58 20 76 61 63 75 75 6d 5f  TE INDEX vacuum_
27048 64 62 2e 27 20 7c 7c 20 73 75 62 73 74 72 28 73  db.' || substr(s
27049 71 6c 2c 31 34 29 22 0a 20 20 20 20 20 20 22 20  ql,14)".      " 
2704a 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73   FROM sqlite_mas
2704b 74 65 72 20 57 48 45 52 45 20 73 71 6c 20 4c 49  ter WHERE sql LI
2704c 4b 45 20 27 43 52 45 41 54 45 20 49 4e 44 45 58  KE 'CREATE INDEX
2704d 20 25 27 20 22 29 3b 0a 20 20 69 66 28 20 72 63   %' ");.  if( rc
2704e 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
2704f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
27050 3b 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78 65  ;.  rc = execExe
27051 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20 20  cSql(db, .      
27052 22 53 45 4c 45 43 54 20 27 43 52 45 41 54 45 20  "SELECT 'CREATE 
27053 55 4e 49 51 55 45 20 49 4e 44 45 58 20 76 61 63  UNIQUE INDEX vac
27054 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 73 75 62 73  uum_db.' || subs
27055 74 72 28 73 71 6c 2c 32 31 29 20 22 0a 20 20 20  tr(sql,21) ".   
27056 20 20 20 22 20 20 46 52 4f 4d 20 73 71 6c 69 74     "  FROM sqlit
27057 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20 73  e_master WHERE s
27058 71 6c 20 4c 49 4b 45 20 27 43 52 45 41 54 45 20  ql LIKE 'CREATE 
27059 55 4e 49 51 55 45 20 49 4e 44 45 58 20 25 27 22  UNIQUE INDEX %'"
2705a 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
2705b 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e  ITE_OK ) goto en
2705c 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 20 20  d_of_vacuum;..  
2705d 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20  /* Loop through 
2705e 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20 74 68  the tables in th
2705f 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e  e main database.
27060 20 46 6f 72 20 65 61 63 68 2c 20 64 6f 0a 20 20   For each, do.  
27061 2a 2a 20 61 6e 20 22 49 4e 53 45 52 54 20 49 4e  ** an "INSERT IN
27062 54 4f 20 76 61 63 75 75 6d 5f 64 62 2e 78 78 78  TO vacuum_db.xxx
27063 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 78   SELECT * FROM x
27064 78 78 3b 22 20 74 6f 20 63 6f 70 79 0a 20 20 2a  xx;" to copy.  *
27065 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 74  * the contents t
27066 6f 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20  o the temporary 
27067 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20  database..  */. 
27068 20 72 63 20 3d 20 65 78 65 63 45 78 65 63 53 71   rc = execExecSq
27069 6c 28 64 62 2c 20 0a 20 20 20 20 20 20 22 53 45  l(db, .      "SE
2706a 4c 45 43 54 20 27 49 4e 53 45 52 54 20 49 4e 54  LECT 'INSERT INT
2706b 4f 20 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c  O vacuum_db.' ||
2706c 20 71 75 6f 74 65 28 6e 61 6d 65 29 20 22 0a 20   quote(name) ". 
2706d 20 20 20 20 20 22 7c 7c 20 27 20 53 45 4c 45 43       "|| ' SELEC
2706e 54 20 2a 20 46 52 4f 4d 20 27 20 7c 7c 20 71 75  T * FROM ' || qu
2706f 6f 74 65 28 6e 61 6d 65 29 20 7c 7c 20 27 3b 27  ote(name) || ';'
27070 22 0a 20 20 20 20 20 20 22 46 52 4f 4d 20 73 71  ".      "FROM sq
27071 6c 69 74 65 5f 6d 61 73 74 65 72 20 22 0a 20 20  lite_master ".  
27072 20 20 20 20 22 57 48 45 52 45 20 74 79 70 65 20      "WHERE type 
27073 3d 20 27 74 61 62 6c 65 27 20 41 4e 44 20 6e 61  = 'table' AND na
27074 6d 65 21 3d 27 73 71 6c 69 74 65 5f 73 65 71 75  me!='sqlite_sequ
27075 65 6e 63 65 27 20 22 0a 20 20 20 20 20 20 22 20  ence' ".      " 
27076 20 41 4e 44 20 72 6f 6f 74 70 61 67 65 3e 30 22   AND rootpage>0"
27077 0a 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63 21  ..  );.  if( rc!
27078 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
27079 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b  o end_of_vacuum;
2707a 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 6f 76 65 72  ..  /* Copy over
2707b 20 74 68 65 20 73 65 71 75 65 6e 63 65 20 74 61   the sequence ta
2707c 62 6c 65 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20  ble.  */.  rc = 
2707d 65 78 65 63 45 78 65 63 53 71 6c 28 64 62 2c 20  execExecSql(db, 
2707e 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 27  .      "SELECT '
2707f 44 45 4c 45 54 45 20 46 52 4f 4d 20 76 61 63 75  DELETE FROM vacu
27080 75 6d 5f 64 62 2e 27 20 7c 7c 20 71 75 6f 74 65  um_db.' || quote
27081 28 6e 61 6d 65 29 20 7c 7c 20 27 3b 27 20 22 0a  (name) || ';' ".
27082 20 20 20 20 20 20 22 46 52 4f 4d 20 76 61 63 75        "FROM vacu
27083 75 6d 5f 64 62 2e 73 71 6c 69 74 65 5f 6d 61 73  um_db.sqlite_mas
27084 74 65 72 20 57 48 45 52 45 20 6e 61 6d 65 3d 27  ter WHERE name='
27085 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 27  sqlite_sequence'
27086 20 22 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63   ".  );.  if( rc
27087 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
27088 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
27089 3b 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78 65  ;.  rc = execExe
2708a 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20 20  cSql(db, .      
2708b 22 53 45 4c 45 43 54 20 27 49 4e 53 45 52 54 20  "SELECT 'INSERT 
2708c 49 4e 54 4f 20 76 61 63 75 75 6d 5f 64 62 2e 27  INTO vacuum_db.'
2708d 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29 20   || quote(name) 
2708e 22 0a 20 20 20 20 20 20 22 7c 7c 20 27 20 53 45  ".      "|| ' SE
2708f 4c 45 43 54 20 2a 20 46 52 4f 4d 20 27 20 7c 7c  LECT * FROM ' ||
27090 20 71 75 6f 74 65 28 6e 61 6d 65 29 20 7c 7c 20   quote(name) || 
27091 27 3b 27 20 22 0a 20 20 20 20 20 20 22 46 52 4f  ';' ".      "FRO
27092 4d 20 76 61 63 75 75 6d 5f 64 62 2e 73 71 6c 69  M vacuum_db.sqli
27093 74 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20  te_master WHERE 
27094 6e 61 6d 65 3d 3d 27 73 71 6c 69 74 65 5f 73 65  name=='sqlite_se
27095 71 75 65 6e 63 65 27 3b 22 0a 20 20 29 3b 0a 20  quence';".  );. 
27096 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
27097 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
27098 5f 76 61 63 75 75 6d 3b 0a 0a 0a 20 20 2f 2a 20  _vacuum;...  /* 
27099 43 6f 70 79 20 74 68 65 20 74 72 69 67 67 65 72  Copy the trigger
2709a 73 2c 20 76 69 65 77 73 2c 20 61 6e 64 20 76 69  s, views, and vi
2709b 72 74 75 61 6c 20 74 61 62 6c 65 73 20 66 72 6f  rtual tables fro
2709c 6d 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  m the main datab
2709d 61 73 65 0a 20 20 2a 2a 20 6f 76 65 72 20 74 6f  ase.  ** over to
2709e 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64   the temporary d
2709f 61 74 61 62 61 73 65 2e 20 20 4e 6f 6e 65 20 6f  atabase.  None o
270a0 66 20 74 68 65 73 65 20 6f 62 6a 65 63 74 73 20  f these objects 
270a1 68 61 73 20 61 6e 79 0a 20 20 2a 2a 20 61 73 73  has any.  ** ass
270a2 6f 63 69 61 74 65 64 20 73 74 6f 72 61 67 65 2c  ociated storage,
270a3 20 73 6f 20 61 6c 6c 20 77 65 20 68 61 76 65 20   so all we have 
270a4 74 6f 20 64 6f 20 69 73 20 63 6f 70 79 20 74 68  to do is copy th
270a5 65 69 72 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a  eir entries.  **
270a6 20 66 72 6f 6d 20 74 68 65 20 53 51 4c 49 54 45   from the SQLITE
270a7 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e 0a 20  _MASTER table.. 
270a8 20 2a 2f 0a 20 20 72 63 20 3d 20 65 78 65 63 53   */.  rc = execS
270a9 71 6c 28 64 62 2c 0a 20 20 20 20 20 20 22 49 4e  ql(db,.      "IN
270aa 53 45 52 54 20 49 4e 54 4f 20 76 61 63 75 75 6d  SERT INTO vacuum
270ab 5f 64 62 2e 73 71 6c 69 74 65 5f 6d 61 73 74 65  _db.sqlite_maste
270ac 72 20 22 0a 20 20 20 20 20 20 22 20 20 53 45 4c  r ".      "  SEL
270ad 45 43 54 20 74 79 70 65 2c 20 6e 61 6d 65 2c 20  ECT type, name, 
270ae 74 62 6c 5f 6e 61 6d 65 2c 20 72 6f 6f 74 70 61  tbl_name, rootpa
270af 67 65 2c 20 73 71 6c 22 0a 20 20 20 20 20 20 22  ge, sql".      "
270b0 20 20 20 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f      FROM sqlite_
270b1 6d 61 73 74 65 72 22 0a 20 20 20 20 20 20 22 20  master".      " 
270b2 20 20 57 48 45 52 45 20 74 79 70 65 3d 27 76 69    WHERE type='vi
270b3 65 77 27 20 4f 52 20 74 79 70 65 3d 27 74 72 69  ew' OR type='tri
270b4 67 67 65 72 27 22 0a 20 20 20 20 20 20 22 20 20  gger'".      "  
270b5 20 20 20 20 4f 52 20 28 74 79 70 65 3d 27 74 61      OR (type='ta
270b6 62 6c 65 27 20 41 4e 44 20 72 6f 6f 74 70 61 67  ble' AND rootpag
270b7 65 3d 30 29 22 0a 20 20 29 3b 0a 20 20 69 66 28  e=0)".  );.  if(
270b8 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f   rc ) goto end_o
270b9 66 5f 76 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20  f_vacuum;..  /* 
270ba 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 75  At this point, u
270bb 6e 6c 65 73 73 20 74 68 65 20 6d 61 69 6e 20 64  nless the main d
270bc 62 20 77 61 73 20 63 6f 6d 70 6c 65 74 65 6c 79  b was completely
270bd 20 65 6d 70 74 79 2c 20 74 68 65 72 65 20 69 73   empty, there is
270be 20 6e 6f 77 20 61 0a 20 20 2a 2a 20 74 72 61 6e   now a.  ** tran
270bf 73 61 63 74 69 6f 6e 20 6f 70 65 6e 20 6f 6e 20  saction open on 
270c0 74 68 65 20 76 61 63 75 75 6d 20 64 61 74 61 62  the vacuum datab
270c1 61 73 65 2c 20 62 75 74 20 6e 6f 74 20 6f 6e 20  ase, but not on 
270c2 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
270c3 65 2e 0a 20 20 2a 2a 20 4f 70 65 6e 20 61 20 62  e..  ** Open a b
270c4 74 72 65 65 20 6c 65 76 65 6c 20 74 72 61 6e 73  tree level trans
270c5 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 6d 61  action on the ma
270c6 69 6e 20 64 61 74 61 62 61 73 65 2e 20 54 68 69  in database. Thi
270c7 73 20 61 6c 6c 6f 77 73 20 61 0a 20 20 2a 2a 20  s allows a.  ** 
270c8 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42  call to sqlite3B
270c9 74 72 65 65 43 6f 70 79 46 69 6c 65 28 29 2e 20  treeCopyFile(). 
270ca 54 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  The main databas
270cb 65 20 62 74 72 65 65 20 6c 65 76 65 6c 0a 20 20  e btree level.  
270cc 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  ** transaction i
270cd 73 20 74 68 65 6e 20 63 6f 6d 6d 69 74 74 65 64  s then committed
270ce 2c 20 73 6f 20 74 68 65 20 53 51 4c 20 6c 65 76  , so the SQL lev
270cf 65 6c 20 6e 65 76 65 72 20 6b 6e 6f 77 73 20 69  el never knows i
270d0 74 20 77 61 73 0a 20 20 2a 2a 20 6f 70 65 6e 65  t was.  ** opene
270d1 64 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 54  d for writing. T
270d2 68 69 73 20 77 61 79 2c 20 74 68 65 20 53 51 4c  his way, the SQL
270d3 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 75 73 65   transaction use
270d4 64 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 0a  d to create the.
270d5 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 64    ** temporary d
270d6 61 74 61 62 61 73 65 20 6e 65 76 65 72 20 6e 65  atabase never ne
270d7 65 64 73 20 74 6f 20 62 65 20 63 6f 6d 6d 69 74  eds to be commit
270d8 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ted..  */.  if( 
270d9 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
270da 0a 20 20 20 20 75 33 32 20 6d 65 74 61 3b 0a 20  .    u32 meta;. 
270db 20 20 20 69 6e 74 20 69 3b 0a 0a 20 20 20 20 2f     int i;..    /
270dc 2a 20 54 68 69 73 20 61 72 72 61 79 20 64 65 74  * This array det
270dd 65 72 6d 69 6e 65 73 20 77 68 69 63 68 20 6d 65  ermines which me
270de 74 61 20 6d 65 74 61 20 76 61 6c 75 65 73 20 61  ta meta values a
270df 72 65 20 70 72 65 73 65 72 76 65 64 20 69 6e 20  re preserved in 
270e0 74 68 65 0a 20 20 20 20 2a 2a 20 76 61 63 75 75  the.    ** vacuu
270e1 6d 2e 20 20 45 76 65 6e 20 65 6e 74 72 69 65 73  m.  Even entries
270e2 20 61 72 65 20 74 68 65 20 6d 65 74 61 20 76 61   are the meta va
270e3 6c 75 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 6f  lue number and o
270e4 64 64 20 65 6e 74 72 69 65 73 0a 20 20 20 20 2a  dd entries.    *
270e5 2a 20 61 72 65 20 61 6e 20 69 6e 63 72 65 6d 65  * are an increme
270e6 6e 74 20 74 6f 20 61 70 70 6c 79 20 74 6f 20 74  nt to apply to t
270e7 68 65 20 6d 65 74 61 20 76 61 6c 75 65 20 61 66  he meta value af
270e8 74 65 72 20 74 68 65 20 76 61 63 75 75 6d 2e 0a  ter the vacuum..
270e9 20 20 20 20 2a 2a 20 54 68 65 20 69 6e 63 72 65      ** The incre
270ea 6d 65 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20  ment is used to 
270eb 69 6e 63 72 65 61 73 65 20 74 68 65 20 73 63 68  increase the sch
270ec 65 6d 61 20 63 6f 6f 6b 69 65 20 73 6f 20 74 68  ema cookie so th
270ed 61 74 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20  at other.    ** 
270ee 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 6f 20 74  connections to t
270ef 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
270f0 20 77 69 6c 6c 20 6b 6e 6f 77 20 74 6f 20 72 65   will know to re
270f1 72 65 61 64 20 74 68 65 20 73 63 68 65 6d 61 2e  read the schema.
270f2 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 74 61 74  .    */.    stat
270f3 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  ic const unsigne
270f4 64 20 63 68 61 72 20 61 43 6f 70 79 5b 5d 20 3d  d char aCopy[] =
270f5 20 7b 0a 20 20 20 20 20 20 20 31 2c 20 31 2c 20   {.       1, 1, 
270f6 20 20 20 2f 2a 20 41 64 64 20 6f 6e 65 20 74 6f     /* Add one to
270f7 20 74 68 65 20 6f 6c 64 20 73 63 68 65 6d 61 20   the old schema 
270f8 63 6f 6f 6b 69 65 20 2a 2f 0a 20 20 20 20 20 20  cookie */.      
270f9 20 33 2c 20 30 2c 20 20 20 20 2f 2a 20 50 72 65   3, 0,    /* Pre
270fa 73 65 72 76 65 20 74 68 65 20 64 65 66 61 75 6c  serve the defaul
270fb 74 20 70 61 67 65 20 63 61 63 68 65 20 73 69 7a  t page cache siz
270fc 65 20 2a 2f 0a 20 20 20 20 20 20 20 35 2c 20 30  e */.       5, 0
270fd 2c 20 20 20 20 2f 2a 20 50 72 65 73 65 72 76 65  ,    /* Preserve
270fe 20 74 68 65 20 64 65 66 61 75 6c 74 20 74 65 78   the default tex
270ff 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20  t encoding */.  
27100 20 20 20 20 20 36 2c 20 30 2c 20 20 20 20 2f 2a       6, 0,    /*
27101 20 50 72 65 73 65 72 76 65 20 74 68 65 20 75 73   Preserve the us
27102 65 72 20 76 65 72 73 69 6f 6e 20 2a 2f 0a 20 20  er version */.  
27103 20 20 7d 3b 0a 0a 20 20 20 20 61 73 73 65 72 74    };..    assert
27104 28 20 31 3d 3d 73 71 6c 69 74 65 33 42 74 72 65  ( 1==sqlite3Btre
27105 65 49 73 49 6e 54 72 61 6e 73 28 70 54 65 6d 70  eIsInTrans(pTemp
27106 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ) );.    assert(
27107 20 31 3d 3d 73 71 6c 69 74 65 33 42 74 72 65 65   1==sqlite3Btree
27108 49 73 49 6e 54 72 61 6e 73 28 70 4d 61 69 6e 29  IsInTrans(pMain)
27109 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 70 79   );..    /* Copy
2710a 20 42 74 72 65 65 20 6d 65 74 61 20 76 61 6c 75   Btree meta valu
2710b 65 73 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d  es */.    for(i=
2710c 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 43 6f 70  0; i<sizeof(aCop
2710d 79 29 2f 73 69 7a 65 6f 66 28 61 43 6f 70 79 5b  y)/sizeof(aCopy[
2710e 30 5d 29 3b 20 69 2b 3d 32 29 7b 0a 20 20 20 20  0]); i+=2){.    
2710f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
27110 72 65 65 47 65 74 4d 65 74 61 28 70 4d 61 69 6e  reeGetMeta(pMain
27111 2c 20 61 43 6f 70 79 5b 69 5d 2c 20 26 6d 65 74  , aCopy[i], &met
27112 61 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  a);.      if( rc
27113 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
27114 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
27115 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
27116 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d  ite3BtreeUpdateM
27117 65 74 61 28 70 54 65 6d 70 2c 20 61 43 6f 70 79  eta(pTemp, aCopy
27118 5b 69 5d 2c 20 6d 65 74 61 2b 61 43 6f 70 79 5b  [i], meta+aCopy[
27119 69 2b 31 5d 29 3b 0a 20 20 20 20 20 20 69 66 28  i+1]);.      if(
2711a 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2711b 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63   goto end_of_vac
2711c 75 75 6d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  uum;.    }..    
2711d 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
2711e 65 43 6f 70 79 46 69 6c 65 28 70 4d 61 69 6e 2c  eCopyFile(pMain,
2711f 20 70 54 65 6d 70 29 3b 0a 20 20 20 20 69 66 28   pTemp);.    if(
27120 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
27121 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63   goto end_of_vac
27122 75 75 6d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  uum;.    rc = sq
27123 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74  lite3BtreeCommit
27124 28 70 54 65 6d 70 29 3b 0a 20 20 20 20 69 66 28  (pTemp);.    if(
27125 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
27126 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63   goto end_of_vac
27127 75 75 6d 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  uum;.#ifndef SQL
27128 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
27129 55 55 4d 0a 20 20 20 20 73 71 6c 69 74 65 33 42  UUM.    sqlite3B
2712a 74 72 65 65 53 65 74 41 75 74 6f 56 61 63 75 75  treeSetAutoVacuu
2712b 6d 28 70 4d 61 69 6e 2c 20 73 71 6c 69 74 65 33  m(pMain, sqlite3
2712c 42 74 72 65 65 47 65 74 41 75 74 6f 56 61 63 75  BtreeGetAutoVacu
2712d 75 6d 28 70 54 65 6d 70 29 29 3b 0a 23 65 6e 64  um(pTemp));.#end
2712e 69 66 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  if.    rc = sqli
2712f 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 70  te3BtreeCommit(p
27130 4d 61 69 6e 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  Main);.  }..  if
27131 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
27132 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
27133 74 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53  te3BtreeSetPageS
27134 69 7a 65 28 70 4d 61 69 6e 2c 20 73 71 6c 69 74  ize(pMain, sqlit
27135 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69  e3BtreeGetPageSi
27136 7a 65 28 70 54 65 6d 70 29 2c 20 6e 52 65 73 29  ze(pTemp), nRes)
27137 3b 0a 20 20 7d 0a 0a 65 6e 64 5f 6f 66 5f 76 61  ;.  }..end_of_va
27138 63 75 75 6d 3a 0a 20 20 2f 2a 20 52 65 73 74 6f  cuum:.  /* Resto
27139 72 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  re the original 
2713a 76 61 6c 75 65 20 6f 66 20 64 62 2d 3e 66 6c 61  value of db->fla
2713b 67 73 20 2a 2f 0a 20 20 64 62 2d 3e 66 6c 61 67  gs */.  db->flag
2713c 73 20 3d 20 73 61 76 65 64 5f 66 6c 61 67 73 3b  s = saved_flags;
2713d 0a 20 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 3d  .  db->nChange =
2713e 20 73 61 76 65 64 5f 6e 43 68 61 6e 67 65 3b 0a   saved_nChange;.
2713f 20 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e    db->nTotalChan
27140 67 65 20 3d 20 73 61 76 65 64 5f 6e 54 6f 74 61  ge = saved_nTota
27141 6c 43 68 61 6e 67 65 3b 0a 0a 20 20 2f 2a 20 43  lChange;..  /* C
27142 75 72 72 65 6e 74 6c 79 20 74 68 65 72 65 20 69  urrently there i
27143 73 20 61 6e 20 53 51 4c 20 6c 65 76 65 6c 20 74  s an SQL level t
27144 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 20  ransaction open 
27145 6f 6e 20 74 68 65 20 76 61 63 75 75 6d 0a 20 20  on the vacuum.  
27146 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 4e 6f 20  ** database. No 
27147 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 20 6f  locks are held o
27148 6e 20 61 6e 79 20 6f 74 68 65 72 20 66 69 6c 65  n any other file
27149 73 20 28 73 69 6e 63 65 20 74 68 65 20 6d 61 69  s (since the mai
2714a 6e 20 66 69 6c 65 0a 20 20 2a 2a 20 77 61 73 20  n file.  ** was 
2714b 63 6f 6d 6d 69 74 74 65 64 20 61 74 20 74 68 65  committed at the
2714c 20 62 74 72 65 65 20 6c 65 76 65 6c 29 2e 20 53   btree level). S
2714d 6f 20 69 74 20 73 61 66 65 20 74 6f 20 65 6e 64  o it safe to end
2714e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
2714f 0a 20 20 2a 2a 20 62 79 20 6d 61 6e 75 61 6c 6c  .  ** by manuall
27150 79 20 73 65 74 74 69 6e 67 20 74 68 65 20 61 75  y setting the au
27151 74 6f 43 6f 6d 6d 69 74 20 66 6c 61 67 20 74 6f  toCommit flag to
27152 20 74 72 75 65 20 61 6e 64 20 64 65 74 61 63 68   true and detach
27153 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 76 61 63  ing the.  ** vac
27154 75 75 6d 20 64 61 74 61 62 61 73 65 2e 20 54 68  uum database. Th
27155 65 20 76 61 63 75 75 6d 5f 64 62 20 6a 6f 75 72  e vacuum_db jour
27156 6e 61 6c 20 66 69 6c 65 20 69 73 20 64 65 6c 65  nal file is dele
27157 74 65 64 20 77 68 65 6e 20 74 68 65 20 70 61 67  ted when the pag
27158 65 72 0a 20 20 2a 2a 20 69 73 20 63 6c 6f 73 65  er.  ** is close
27159 64 20 62 79 20 74 68 65 20 44 45 54 41 43 48 2e  d by the DETACH.
2715a 0a 20 20 2a 2f 0a 20 20 64 62 2d 3e 61 75 74 6f  .  */.  db->auto
2715b 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 0a 20 20 69  Commit = 1;..  i
2715c 66 28 20 70 44 62 20 29 7b 0a 20 20 20 20 73 71  f( pDb ){.    sq
2715d 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28  lite3BtreeClose(
2715e 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 70  pDb->pBt);.    p
2715f 44 62 2d 3e 70 42 74 20 3d 20 30 3b 0a 20 20 20  Db->pBt = 0;.   
27160 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 20 3d 20   pDb->pSchema = 
27161 30 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65  0;.  }..  sqlite
27162 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63  3ResetInternalSc
27163 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 0a 20 20  hema(db, 0);..  
27164 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e  return rc;.}.#en
27165 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  dif  /* SQLITE_O
27166 4d 49 54 5f 56 41 43 55 55 4d 20 26 26 20 53 51  MIT_VACUUM && SQ
27167 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48  LITE_OMIT_ATTACH
27168 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
27169 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 61 63 75  **** End of vacu
2716a 75 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  um.c ***********
2716b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2716c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2716d 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
2716e 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
2716f 76 74 61 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  vtab.c *********
27170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27171 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27172 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20  ***/./*.** 2006 
27173 4a 75 6e 65 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68  June 10.**.** Th
27174 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
27175 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
27176 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
27177 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
27178 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
27179 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
2717a 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
2717b 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
2717c 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
2717d 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
2717e 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
2717f 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
27180 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
27181 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
27182 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
27183 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
27184 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
27185 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27186 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27187 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27188 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27189 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
2718a 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64  ile contains cod
2718b 65 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 69  e used to help i
2718c 6d 70 6c 65 6d 65 6e 74 20 76 69 72 74 75 61 6c  mplement virtual
2718d 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 24   tables..**.** $
2718e 49 64 3a 20 76 74 61 62 2e 63 2c 76 20 31 2e 37  Id: vtab.c,v 1.7
2718f 36 20 32 30 30 38 2f 30 38 2f 32 30 20 31 36 3a  6 2008/08/20 16:
27190 33 35 3a 31 30 20 64 72 68 20 45 78 70 20 24 0a  35:10 drh Exp $.
27191 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
27192 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
27193 42 4c 45 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  BLE..static int 
27194 63 72 65 61 74 65 4d 6f 64 75 6c 65 28 0a 20 20  createModule(.  
27195 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
27196 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27197 2f 2a 20 44 61 74 61 62 61 73 65 20 69 6e 20 77  /* Database in w
27198 68 69 63 68 20 6d 6f 64 75 6c 65 20 69 73 20 72  hich module is r
27199 65 67 69 73 74 65 72 65 64 20 2a 2f 0a 20 20 63  egistered */.  c
2719a 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
2719b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
2719c 2a 20 4e 61 6d 65 20 61 73 73 69 67 6e 65 64 20  * Name assigned 
2719d 74 6f 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 2a  to this module *
2719e 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  /.  const sqlite
2719f 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c  3_module *pModul
271a0 65 2c 20 20 2f 2a 20 54 68 65 20 64 65 66 69 6e  e,  /* The defin
271a1 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 64  ition of the mod
271a2 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ule */.  void *p
271a3 41 75 78 2c 20 20 20 20 20 20 20 20 20 20 20 20  Aux,            
271a4 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74           /* Cont
271a5 65 78 74 20 70 6f 69 6e 74 65 72 20 66 6f 72 20  ext pointer for 
271a6 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74  xCreate/xConnect
271a7 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65   */.  void (*xDe
271a8 73 74 72 6f 79 29 28 76 6f 69 64 20 2a 29 20 20  stroy)(void *)  
271a9 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20        /* Module 
271aa 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74  destructor funct
271ab 69 6f 6e 20 2a 2f 0a 29 20 7b 0a 20 20 69 6e 74  ion */.) {.  int
271ac 20 72 63 2c 20 6e 4e 61 6d 65 3b 0a 20 20 4d 6f   rc, nName;.  Mo
271ad 64 75 6c 65 20 2a 70 4d 6f 64 3b 0a 0a 20 20 73  dule *pMod;..  s
271ae 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
271af 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
271b0 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28   nName = strlen(
271b1 7a 4e 61 6d 65 29 3b 0a 20 20 70 4d 6f 64 20 3d  zName);.  pMod =
271b2 20 28 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74   (Module *)sqlit
271b3 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
271b4 2c 20 73 69 7a 65 6f 66 28 4d 6f 64 75 6c 65 29  , sizeof(Module)
271b5 20 2b 20 6e 4e 61 6d 65 20 2b 20 31 29 3b 0a 20   + nName + 1);. 
271b6 20 69 66 28 20 70 4d 6f 64 20 29 7b 0a 20 20 20   if( pMod ){.   
271b7 20 4d 6f 64 75 6c 65 20 2a 70 44 65 6c 3b 0a 20   Module *pDel;. 
271b8 20 20 20 63 68 61 72 20 2a 7a 43 6f 70 79 20 3d     char *zCopy =
271b9 20 28 63 68 61 72 20 2a 29 28 26 70 4d 6f 64 5b   (char *)(&pMod[
271ba 31 5d 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  1]);.    memcpy(
271bb 7a 43 6f 70 79 2c 20 7a 4e 61 6d 65 2c 20 6e 4e  zCopy, zName, nN
271bc 61 6d 65 2b 31 29 3b 0a 20 20 20 20 70 4d 6f 64  ame+1);.    pMod
271bd 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 43 6f 70 79 3b  ->zName = zCopy;
271be 0a 20 20 20 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75  .    pMod->pModu
271bf 6c 65 20 3d 20 70 4d 6f 64 75 6c 65 3b 0a 20 20  le = pModule;.  
271c0 20 20 70 4d 6f 64 2d 3e 70 41 75 78 20 3d 20 70    pMod->pAux = p
271c1 41 75 78 3b 0a 20 20 20 20 70 4d 6f 64 2d 3e 78  Aux;.    pMod->x
271c2 44 65 73 74 72 6f 79 20 3d 20 78 44 65 73 74 72  Destroy = xDestr
271c3 6f 79 3b 0a 20 20 20 20 70 44 65 6c 20 3d 20 28  oy;.    pDel = (
271c4 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65 33  Module *)sqlite3
271c5 48 61 73 68 49 6e 73 65 72 74 28 26 64 62 2d 3e  HashInsert(&db->
271c6 61 4d 6f 64 75 6c 65 2c 20 7a 43 6f 70 79 2c 20  aModule, zCopy, 
271c7 6e 4e 61 6d 65 2c 20 28 76 6f 69 64 2a 29 70 4d  nName, (void*)pM
271c8 6f 64 29 3b 0a 20 20 20 20 69 66 28 20 70 44 65  od);.    if( pDe
271c9 6c 20 26 26 20 70 44 65 6c 2d 3e 78 44 65 73 74  l && pDel->xDest
271ca 72 6f 79 20 29 7b 0a 20 20 20 20 20 20 70 44 65  roy ){.      pDe
271cb 6c 2d 3e 78 44 65 73 74 72 6f 79 28 70 44 65 6c  l->xDestroy(pDel
271cc 2d 3e 70 41 75 78 29 3b 0a 20 20 20 20 7d 0a 20  ->pAux);.    }. 
271cd 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
271ce 28 64 62 2c 20 70 44 65 6c 29 3b 0a 20 20 20 20  (db, pDel);.    
271cf 69 66 28 20 70 44 65 6c 3d 3d 70 4d 6f 64 20 29  if( pDel==pMod )
271d0 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c  {.      db->mall
271d1 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
271d2 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 52    }.    sqlite3R
271d3 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65  esetInternalSche
271d4 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 20  ma(db, 0);.  }. 
271d5 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
271d6 45 78 69 74 28 64 62 2c 20 53 51 4c 49 54 45 5f  Exit(db, SQLITE_
271d7 4f 4b 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  OK);.  sqlite3_m
271d8 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
271d9 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
271da 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 78  rc;.}.../*.** Ex
271db 74 65 72 6e 61 6c 20 41 50 49 20 66 75 6e 63 74  ternal API funct
271dc 69 6f 6e 20 75 73 65 64 20 74 6f 20 63 72 65 61  ion used to crea
271dd 74 65 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c  te a new virtual
271de 2d 74 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 0a 2a  -table module..*
271df 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
271e0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
271e1 6d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65  module(.  sqlite
271e2 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
271e3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
271e4 61 62 61 73 65 20 69 6e 20 77 68 69 63 68 20 6d  abase in which m
271e5 6f 64 75 6c 65 20 69 73 20 72 65 67 69 73 74 65  odule is registe
271e6 72 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  red */.  const c
271e7 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20  har *zName,     
271e8 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
271e9 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68 69   assigned to thi
271ea 73 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f  s module */.  co
271eb 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
271ec 6c 65 20 2a 70 4d 6f 64 75 6c 65 2c 20 20 2f 2a  le *pModule,  /*
271ed 20 54 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 20   The definition 
271ee 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f  of the module */
271ef 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 20 20 20  .  void *pAux   
271f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
271f1 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 70 6f     /* Context po
271f2 69 6e 74 65 72 20 66 6f 72 20 78 43 72 65 61 74  inter for xCreat
271f3 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 29 7b  e/xConnect */.){
271f4 0a 20 20 72 65 74 75 72 6e 20 63 72 65 61 74 65  .  return create
271f5 4d 6f 64 75 6c 65 28 64 62 2c 20 7a 4e 61 6d 65  Module(db, zName
271f6 2c 20 70 4d 6f 64 75 6c 65 2c 20 70 41 75 78 2c  , pModule, pAux,
271f7 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78   0);.}../*.** Ex
271f8 74 65 72 6e 61 6c 20 41 50 49 20 66 75 6e 63 74  ternal API funct
271f9 69 6f 6e 20 75 73 65 64 20 74 6f 20 63 72 65 61  ion used to crea
271fa 74 65 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c  te a new virtual
271fb 2d 74 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 0a 2a  -table module..*
271fc 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
271fd 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
271fe 6d 6f 64 75 6c 65 5f 76 32 28 0a 20 20 73 71 6c  module_v2(.  sql
271ff 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
27200 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
27201 44 61 74 61 62 61 73 65 20 69 6e 20 77 68 69 63  Database in whic
27202 68 20 6d 6f 64 75 6c 65 20 69 73 20 72 65 67 69  h module is regi
27203 73 74 65 72 65 64 20 2a 2f 0a 20 20 63 6f 6e 73  stered */.  cons
27204 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20  t char *zName,  
27205 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
27206 61 6d 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20  ame assigned to 
27207 74 68 69 73 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20  this module */. 
27208 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
27209 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 2c 20  odule *pModule, 
2720a 20 2f 2a 20 54 68 65 20 64 65 66 69 6e 69 74 69   /* The definiti
2720b 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65  on of the module
2720c 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78   */.  void *pAux
2720d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2720e 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74        /* Context
2720f 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 78 43 72   pointer for xCr
27210 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f  eate/xConnect */
27211 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72  .  void (*xDestr
27212 6f 79 29 28 76 6f 69 64 20 2a 29 20 20 20 20 20  oy)(void *)     
27213 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73     /* Module des
27214 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e  tructor function
27215 20 2a 2f 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20   */.){.  return 
27216 63 72 65 61 74 65 4d 6f 64 75 6c 65 28 64 62 2c  createModule(db,
27217 20 7a 4e 61 6d 65 2c 20 70 4d 6f 64 75 6c 65 2c   zName, pModule,
27218 20 70 41 75 78 2c 20 78 44 65 73 74 72 6f 79 29   pAux, xDestroy)
27219 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20  ;.}../*.** Lock 
2721a 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
2721b 65 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e  e so that it can
2721c 6e 6f 74 20 62 65 20 64 69 73 63 6f 6e 6e 65 63  not be disconnec
2721d 74 65 64 2e 0a 2a 2a 20 4c 6f 63 6b 73 20 6e 65  ted..** Locks ne
2721e 73 74 2e 20 20 45 76 65 72 79 20 6c 6f 63 6b 20  st.  Every lock 
2721f 73 68 6f 75 6c 64 20 68 61 76 65 20 61 20 63 6f  should have a co
27220 72 72 65 73 70 6f 6e 64 69 6e 67 20 75 6e 6c 6f  rresponding unlo
27221 63 6b 2e 0a 2a 2a 20 49 66 20 61 6e 20 75 6e 6c  ck..** If an unl
27222 6f 63 6b 20 69 73 20 6f 6d 69 74 74 65 64 2c 20  ock is omitted, 
27223 72 65 73 6f 75 72 63 65 73 20 6c 65 61 6b 73 20  resources leaks 
27224 77 69 6c 6c 20 6f 63 63 75 72 2e 20 20 0a 2a 2a  will occur.  .**
27225 0a 2a 2a 20 49 66 20 61 20 64 69 73 63 6f 6e 6e  .** If a disconn
27226 65 63 74 20 69 73 20 61 74 74 65 6d 70 74 65 64  ect is attempted
27227 20 77 68 69 6c 65 20 61 20 76 69 72 74 75 61 6c   while a virtual
27228 20 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64   table is locked
27229 2c 0a 2a 2a 20 74 68 65 20 64 69 73 63 6f 6e 6e  ,.** the disconn
2722a 65 63 74 20 69 73 20 64 65 66 65 72 72 65 64 20  ect is deferred 
2722b 75 6e 74 69 6c 20 61 6c 6c 20 6c 6f 63 6b 73 20  until all locks 
2722c 68 61 76 65 20 62 65 65 6e 20 72 65 6d 6f 76 65  have been remove
2722d 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
2722e 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2722f 33 56 74 61 62 4c 6f 63 6b 28 73 71 6c 69 74 65  3VtabLock(sqlite
27230 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a  3_vtab *pVtab){.
27231 20 20 70 56 74 61 62 2d 3e 6e 52 65 66 2b 2b 3b    pVtab->nRef++;
27232 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b  .}../*.** Unlock
27233 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
27234 2e 20 20 57 68 65 6e 20 74 68 65 20 6c 61 73 74  .  When the last
27235 20 6c 6f 63 6b 20 69 73 20 72 65 6d 6f 76 65 64   lock is removed
27236 2c 0a 2a 2a 20 64 69 73 63 6f 6e 6e 65 63 74 20  ,.** disconnect 
27237 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
27238 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
27239 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2723a 33 56 74 61 62 55 6e 6c 6f 63 6b 28 73 71 6c 69  3VtabUnlock(sqli
2723b 74 65 33 20 2a 64 62 2c 20 73 71 6c 69 74 65 33  te3 *db, sqlite3
2723c 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20  _vtab *pVtab){. 
2723d 20 70 56 74 61 62 2d 3e 6e 52 65 66 2d 2d 3b 0a   pVtab->nRef--;.
2723e 20 20 61 73 73 65 72 74 28 64 62 29 3b 0a 20 20    assert(db);.  
2723f 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53  assert( sqlite3S
27240 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 64 62 29  afetyCheckOk(db)
27241 20 29 3b 0a 20 20 69 66 28 20 70 56 74 61 62 2d   );.  if( pVtab-
27242 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20  >nRef==0 ){.    
27243 69 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53  if( db->magic==S
27244 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59  QLITE_MAGIC_BUSY
27245 20 29 7b 0a 20 20 20 20 20 20 28 76 6f 69 64 29   ){.      (void)
27246 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
27247 28 64 62 29 3b 0a 20 20 20 20 20 20 70 56 74 61  (db);.      pVta
27248 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44 69 73  b->pModule->xDis
27249 63 6f 6e 6e 65 63 74 28 70 56 74 61 62 29 3b 0a  connect(pVtab);.
2724a 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69        (void)sqli
2724b 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b  te3SafetyOn(db);
2724c 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20  .    } else {.  
2724d 20 20 20 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75      pVtab->pModu
2724e 6c 65 2d 3e 78 44 69 73 63 6f 6e 6e 65 63 74 28  le->xDisconnect(
2724f 70 56 74 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20  pVtab);.    }.  
27250 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72  }.}../*.** Clear
27251 20 61 6e 79 20 61 6e 64 20 61 6c 6c 20 76 69 72   any and all vir
27252 74 75 61 6c 2d 74 61 62 6c 65 20 69 6e 66 6f 72  tual-table infor
27253 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20  mation from the 
27254 54 61 62 6c 65 20 72 65 63 6f 72 64 2e 0a 2a 2a  Table record..**
27255 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
27256 20 63 61 6c 6c 65 64 2c 20 66 6f 72 20 65 78 61   called, for exa
27257 6d 70 6c 65 2c 20 6a 75 73 74 20 62 65 66 6f 72  mple, just befor
27258 65 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 54  e deleting the T
27259 61 62 6c 65 0a 2a 2a 20 72 65 63 6f 72 64 2e 0a  able.** record..
2725a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2725b 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
2725c 61 62 43 6c 65 61 72 28 54 61 62 6c 65 20 2a 70  abClear(Table *p
2725d 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  ){.  sqlite3_vta
2725e 62 20 2a 70 56 74 61 62 20 3d 20 70 2d 3e 70 56  b *pVtab = p->pV
2725f 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  tab;.  sqlite3 *
27260 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66  db = p->db;.  if
27261 28 20 70 56 74 61 62 20 29 7b 0a 20 20 20 20 61  ( pVtab ){.    a
27262 73 73 65 72 74 28 20 70 2d 3e 70 4d 6f 64 20 26  ssert( p->pMod &
27263 26 20 70 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f 64 75  & p->pMod->pModu
27264 6c 65 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  le );.    sqlite
27265 33 56 74 61 62 55 6e 6c 6f 63 6b 28 64 62 2c 20  3VtabUnlock(db, 
27266 70 56 74 61 62 29 3b 0a 20 20 20 20 70 2d 3e 70  pVtab);.    p->p
27267 56 74 61 62 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  Vtab = 0;.  }.  
27268 69 66 28 20 70 2d 3e 61 7a 4d 6f 64 75 6c 65 41  if( p->azModuleA
27269 72 67 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  rg ){.    int i;
2726a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
2726b 70 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 3b 20 69  p->nModuleArg; i
2726c 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
2726d 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
2726e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 69 5d 29 3b  azModuleArg[i]);
2726f 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
27270 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
27271 61 7a 4d 6f 64 75 6c 65 41 72 67 29 3b 0a 20 20  azModuleArg);.  
27272 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61  }.}../*.** Add a
27273 20 6e 65 77 20 6d 6f 64 75 6c 65 20 61 72 67 75   new module argu
27274 6d 65 6e 74 20 74 6f 20 70 54 61 62 6c 65 2d 3e  ment to pTable->
27275 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 5d 2e 0a 2a  azModuleArg[]..*
27276 2a 20 54 68 65 20 73 74 72 69 6e 67 20 69 73 20  * The string is 
27277 6e 6f 74 20 63 6f 70 69 65 64 20 2d 20 74 68 65  not copied - the
27278 20 70 6f 69 6e 74 65 72 20 69 73 20 73 74 6f 72   pointer is stor
27279 65 64 2e 20 20 54 68 65 0a 2a 2a 20 73 74 72 69  ed.  The.** stri
2727a 6e 67 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64  ng will be freed
2727b 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 77   automatically w
2727c 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 69 73  hen the table is
2727d 0a 2a 2a 20 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a  .** deleted..*/.
2727e 73 74 61 74 69 63 20 76 6f 69 64 20 61 64 64 4d  static void addM
2727f 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74 28 73 71  oduleArgument(sq
27280 6c 69 74 65 33 20 2a 64 62 2c 20 54 61 62 6c 65  lite3 *db, Table
27281 20 2a 70 54 61 62 6c 65 2c 20 63 68 61 72 20 2a   *pTable, char *
27282 7a 41 72 67 29 7b 0a 20 20 69 6e 74 20 69 20 3d  zArg){.  int i =
27283 20 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c 65   pTable->nModule
27284 41 72 67 2b 2b 3b 0a 20 20 69 6e 74 20 6e 42 79  Arg++;.  int nBy
27285 74 65 73 20 3d 20 73 69 7a 65 6f 66 28 63 68 61  tes = sizeof(cha
27286 72 20 2a 29 2a 28 31 2b 70 54 61 62 6c 65 2d 3e  r *)*(1+pTable->
27287 6e 4d 6f 64 75 6c 65 41 72 67 29 3b 0a 20 20 63  nModuleArg);.  c
27288 68 61 72 20 2a 2a 61 7a 4d 6f 64 75 6c 65 41 72  har **azModuleAr
27289 67 3b 0a 20 20 61 7a 4d 6f 64 75 6c 65 41 72 67  g;.  azModuleArg
2728a 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c   = sqlite3DbReal
2728b 6c 6f 63 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e  loc(db, pTable->
2728c 61 7a 4d 6f 64 75 6c 65 41 72 67 2c 20 6e 42 79  azModuleArg, nBy
2728d 74 65 73 29 3b 0a 20 20 69 66 28 20 61 7a 4d 6f  tes);.  if( azMo
2728e 64 75 6c 65 41 72 67 3d 3d 30 20 29 7b 0a 20 20  duleArg==0 ){.  
2728f 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 66 6f 72    int j;.    for
27290 28 6a 3d 30 3b 20 6a 3c 69 3b 20 6a 2b 2b 29 7b  (j=0; j<i; j++){
27291 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
27292 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 2d  Free(db, pTable-
27293 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 6a 5d 29  >azModuleArg[j])
27294 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
27295 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 41  te3DbFree(db, zA
27296 72 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  rg);.    sqlite3
27297 44 62 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c  DbFree(db, pTabl
27298 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 29 3b  e->azModuleArg);
27299 0a 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e 4d 6f  .    pTable->nMo
2729a 64 75 6c 65 41 72 67 20 3d 20 30 3b 0a 20 20 7d  duleArg = 0;.  }
2729b 65 6c 73 65 7b 0a 20 20 20 20 61 7a 4d 6f 64 75  else{.    azModu
2729c 6c 65 41 72 67 5b 69 5d 20 3d 20 7a 41 72 67 3b  leArg[i] = zArg;
2729d 0a 20 20 20 20 61 7a 4d 6f 64 75 6c 65 41 72 67  .    azModuleArg
2729e 5b 69 2b 31 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20  [i+1] = 0;.  }. 
2729f 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c   pTable->azModul
272a0 65 41 72 67 20 3d 20 61 7a 4d 6f 64 75 6c 65 41  eArg = azModuleA
272a1 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  rg;.}../*.** The
272a2 20 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68   parser calls th
272a3 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20  is routine when 
272a4 69 74 20 66 69 72 73 74 20 73 65 65 73 20 61 20  it first sees a 
272a5 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54  CREATE VIRTUAL T
272a6 41 42 4c 45 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  ABLE.** statemen
272a7 74 2e 20 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e  t.  The module n
272a8 61 6d 65 20 68 61 73 20 62 65 65 6e 20 70 61 72  ame has been par
272a9 73 65 64 2c 20 62 75 74 20 74 68 65 20 6f 70 74  sed, but the opt
272aa 69 6f 6e 61 6c 20 6c 69 73 74 0a 2a 2a 20 6f 66  ional list.** of
272ab 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 74   parameters that
272ac 20 66 6f 6c 6c 6f 77 20 74 68 65 20 6d 6f 64 75   follow the modu
272ad 6c 65 20 6e 61 6d 65 20 61 72 65 20 73 74 69 6c  le name are stil
272ae 6c 20 70 65 6e 64 69 6e 67 2e 0a 2a 2f 0a 53 51  l pending..*/.SQ
272af 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
272b0 64 20 73 71 6c 69 74 65 33 56 74 61 62 42 65 67  d sqlite3VtabBeg
272b1 69 6e 50 61 72 73 65 28 0a 20 20 50 61 72 73 65  inParse(.  Parse
272b2 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
272b3 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
272b4 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ext */.  Token *
272b5 70 4e 61 6d 65 31 2c 20 20 20 20 20 20 20 20 2f  pName1,        /
272b6 2a 20 4e 61 6d 65 20 6f 66 20 6e 65 77 20 74 61  * Name of new ta
272b7 62 6c 65 2c 20 6f 72 20 64 61 74 61 62 61 73 65  ble, or database
272b8 20 6e 61 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e   name */.  Token
272b9 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 20 20   *pName2,       
272ba 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 6e 65 77 20   /* Name of new 
272bb 74 61 62 6c 65 20 6f 72 20 4e 55 4c 4c 20 2a 2f  table or NULL */
272bc 0a 20 20 54 6f 6b 65 6e 20 2a 70 4d 6f 64 75 6c  .  Token *pModul
272bd 65 4e 61 6d 65 20 20 20 20 2f 2a 20 4e 61 6d 65  eName    /* Name
272be 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 66   of the module f
272bf 6f 72 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  or the virtual t
272c0 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  able */.){.  int
272c1 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20   iDb;           
272c2 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
272c3 73 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  se the table is 
272c4 62 65 69 6e 67 20 63 72 65 61 74 65 64 20 69 6e  being created in
272c5 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
272c6 62 6c 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  ble;        /* T
272c7 68 65 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74  he new virtual t
272c8 61 62 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  able */.  sqlite
272c9 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20  3 *db;          
272ca 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e  /* Database conn
272cb 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28  ection */..  if(
272cc 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61   pParse->db->fla
272cd 67 73 20 26 20 53 51 4c 49 54 45 5f 53 68 61 72  gs & SQLITE_Shar
272ce 65 64 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73  edCache ){.    s
272cf 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
272d0 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 75  Parse, "Cannot u
272d1 73 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  se virtual table
272d2 73 20 69 6e 20 73 68 61 72 65 64 2d 63 61 63 68  s in shared-cach
272d3 65 20 6d 6f 64 65 22 29 3b 0a 20 20 20 20 72 65  e mode");.    re
272d4 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c  turn;.  }..  sql
272d5 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28 70  ite3StartTable(p
272d6 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70  Parse, pName1, p
272d7 4e 61 6d 65 32 2c 20 30 2c 20 30 2c 20 31 2c 20  Name2, 0, 0, 1, 
272d8 30 29 3b 0a 20 20 70 54 61 62 6c 65 20 3d 20 70  0);.  pTable = p
272d9 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
272da 3b 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d  ;.  if( pTable==
272db 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72  0 || pParse->nEr
272dc 72 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73  r ) return;.  as
272dd 73 65 72 74 28 20 30 3d 3d 70 54 61 62 6c 65 2d  sert( 0==pTable-
272de 3e 70 49 6e 64 65 78 20 29 3b 0a 0a 20 20 64 62  >pIndex );..  db
272df 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
272e0 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
272e1 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
272e2 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 29  pTable->pSchema)
272e3 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e  ;.  assert( iDb>
272e4 3d 30 20 29 3b 0a 0a 20 20 70 54 61 62 6c 65 2d  =0 );..  pTable-
272e5 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 54 46 5f  >tabFlags |= TF_
272e6 56 69 72 74 75 61 6c 3b 0a 20 20 70 54 61 62 6c  Virtual;.  pTabl
272e7 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 20 3d 20  e->nModuleArg = 
272e8 30 3b 0a 20 20 61 64 64 4d 6f 64 75 6c 65 41 72  0;.  addModuleAr
272e9 67 75 6d 65 6e 74 28 64 62 2c 20 70 54 61 62 6c  gument(db, pTabl
272ea 65 2c 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72  e, sqlite3NameFr
272eb 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4d 6f 64  omToken(db, pMod
272ec 75 6c 65 4e 61 6d 65 29 29 3b 0a 20 20 61 64 64  uleName));.  add
272ed 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74 28 64  ModuleArgument(d
272ee 62 2c 20 70 54 61 62 6c 65 2c 20 73 71 6c 69 74  b, pTable, sqlit
272ef 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 64  e3DbStrDup(db, d
272f0 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
272f1 65 29 29 3b 0a 20 20 61 64 64 4d 6f 64 75 6c 65  e));.  addModule
272f2 41 72 67 75 6d 65 6e 74 28 64 62 2c 20 70 54 61  Argument(db, pTa
272f3 62 6c 65 2c 20 73 71 6c 69 74 65 33 44 62 53 74  ble, sqlite3DbSt
272f4 72 44 75 70 28 64 62 2c 20 70 54 61 62 6c 65 2d  rDup(db, pTable-
272f5 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 70 50 61 72  >zName));.  pPar
272f6 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 6e  se->sNameToken.n
272f7 20 3d 20 70 4d 6f 64 75 6c 65 4e 61 6d 65 2d 3e   = pModuleName->
272f8 7a 20 2b 20 70 4d 6f 64 75 6c 65 4e 61 6d 65 2d  z + pModuleName-
272f9 3e 6e 20 2d 20 70 4e 61 6d 65 31 2d 3e 7a 3b 0a  >n - pName1->z;.
272fa 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
272fb 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
272fc 4f 4e 0a 20 20 2f 2a 20 43 72 65 61 74 69 6e 67  ON.  /* Creating
272fd 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
272fe 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 61 75 74   invokes the aut
272ff 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62  horization callb
27300 61 63 6b 20 74 77 69 63 65 2e 0a 20 20 2a 2a 20  ack twice..  ** 
27301 54 68 65 20 66 69 72 73 74 20 69 6e 76 6f 63 61  The first invoca
27302 74 69 6f 6e 2c 20 74 6f 20 6f 62 74 61 69 6e 20  tion, to obtain 
27303 70 65 72 6d 69 73 73 69 6f 6e 20 74 6f 20 49 4e  permission to IN
27304 53 45 52 54 20 61 20 72 6f 77 20 69 6e 74 6f 20  SERT a row into 
27305 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f  the.  ** sqlite_
27306 6d 61 73 74 65 72 20 74 61 62 6c 65 2c 20 68 61  master table, ha
27307 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6d  s already been m
27308 61 64 65 20 62 79 20 73 71 6c 69 74 65 33 53 74  ade by sqlite3St
27309 61 72 74 54 61 62 6c 65 28 29 2e 0a 20 20 2a 2a  artTable()..  **
2730a 20 54 68 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c   The second call
2730b 2c 20 74 6f 20 6f 62 74 61 69 6e 20 70 65 72 6d  , to obtain perm
2730c 69 73 73 69 6f 6e 20 74 6f 20 63 72 65 61 74 65  ission to create
2730d 20 74 68 65 20 74 61 62 6c 65 2c 20 69 73 20 6d   the table, is m
2730e 61 64 65 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20  ade now..  */.  
2730f 69 66 28 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f  if( pTable->azMo
27310 64 75 6c 65 41 72 67 20 29 7b 0a 20 20 20 20 73  duleArg ){.    s
27311 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
27312 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 43  pParse, SQLITE_C
27313 52 45 41 54 45 5f 56 54 41 42 4c 45 2c 20 70 54  REATE_VTABLE, pT
27314 61 62 6c 65 2d 3e 7a 4e 61 6d 65 2c 20 0a 20 20  able->zName, .  
27315 20 20 20 20 20 20 20 20 20 20 70 54 61 62 6c 65            pTable
27316 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d  ->azModuleArg[0]
27317 2c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44  , pParse->db->aD
27318 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  b[iDb].zName);. 
27319 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a   }.#endif.}../*.
2731a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2731b 74 61 6b 65 73 20 74 68 65 20 6d 6f 64 75 6c 65  takes the module
2731c 20 61 72 67 75 6d 65 6e 74 20 74 68 61 74 20 68   argument that h
2731d 61 73 20 62 65 65 6e 20 61 63 63 75 6d 75 6c 61  as been accumula
2731e 74 69 6e 67 0a 2a 2a 20 69 6e 20 70 50 61 72 73  ting.** in pPars
2731f 65 2d 3e 7a 41 72 67 5b 5d 20 61 6e 64 20 61 70  e->zArg[] and ap
27320 70 65 6e 64 73 20 69 74 20 74 6f 20 74 68 65 20  pends it to the 
27321 6c 69 73 74 20 6f 66 20 61 72 67 75 6d 65 6e 74  list of argument
27322 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 76 69 72 74  s on the.** virt
27323 75 61 6c 20 74 61 62 6c 65 20 63 75 72 72 65 6e  ual table curren
27324 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  tly under constr
27325 75 63 74 69 6f 6e 20 69 6e 20 70 50 61 72 73 65  uction in pParse
27326 2d 3e 70 54 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  ->pTable..*/.sta
27327 74 69 63 20 76 6f 69 64 20 61 64 64 41 72 67 75  tic void addArgu
27328 6d 65 6e 74 54 6f 56 74 61 62 28 50 61 72 73 65  mentToVtab(Parse
27329 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 66 28   *pParse){.  if(
2732a 20 70 50 61 72 73 65 2d 3e 73 41 72 67 2e 7a 20   pParse->sArg.z 
2732b 26 26 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  && pParse->pNewT
2732c 61 62 6c 65 20 29 7b 0a 20 20 20 20 63 6f 6e 73  able ){.    cons
2732d 74 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e  t char *z = (con
2732e 73 74 20 63 68 61 72 2a 29 70 50 61 72 73 65 2d  st char*)pParse-
2732f 3e 73 41 72 67 2e 7a 3b 0a 20 20 20 20 69 6e 74  >sArg.z;.    int
27330 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 73 41 72   n = pParse->sAr
27331 67 2e 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  g.n;.    sqlite3
27332 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
27333 62 3b 0a 20 20 20 20 61 64 64 4d 6f 64 75 6c 65  b;.    addModule
27334 41 72 67 75 6d 65 6e 74 28 64 62 2c 20 70 50 61  Argument(db, pPa
27335 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2c 20  rse->pNewTable, 
27336 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70  sqlite3DbStrNDup
27337 28 64 62 2c 20 7a 2c 20 6e 29 29 3b 0a 20 20 7d  (db, z, n));.  }
27338 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61  .}../*.** The pa
27339 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20  rser calls this 
2733a 72 6f 75 74 69 6e 65 20 61 66 74 65 72 20 74 68  routine after th
2733b 65 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c  e CREATE VIRTUAL
2733c 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
2733d 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 6f 6d  .** has been com
2733e 70 6c 65 74 65 6c 79 20 70 61 72 73 65 64 2e 0a  pletely parsed..
2733f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
27340 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
27341 61 62 46 69 6e 69 73 68 50 61 72 73 65 28 50 61  abFinishParse(Pa
27342 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b  rse *pParse, Tok
27343 65 6e 20 2a 70 45 6e 64 29 7b 0a 20 20 54 61 62  en *pEnd){.  Tab
27344 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20  le *pTab;       
27345 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65   /* The table be
27346 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64 20  ing constructed 
27347 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
27348 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
27349 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2734a 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ion */.  char *z
2734b 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20 2f 2a 20  Module;      /* 
2734c 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20  The module name 
2734d 6f 66 20 74 68 65 20 74 61 62 6c 65 3a 20 55 53  of the table: US
2734e 49 4e 47 20 6d 6f 64 75 6c 65 6e 61 6d 65 20 2a  ING modulename *
2734f 2f 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64  /.  Module *pMod
27350 20 3d 20 30 3b 0a 0a 20 20 61 64 64 41 72 67 75   = 0;..  addArgu
27351 6d 65 6e 74 54 6f 56 74 61 62 28 70 50 61 72 73  mentToVtab(pPars
27352 65 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73 41  e);.  pParse->sA
27353 72 67 2e 7a 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20  rg.z = 0;..  /* 
27354 4c 6f 6f 6b 75 70 20 74 68 65 20 6d 6f 64 75 6c  Lookup the modul
27355 65 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 70 54 61  e name. */.  pTa
27356 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  b = pParse->pNew
27357 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 70 54 61  Table;.  if( pTa
27358 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  b==0 ) return;. 
27359 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62   db = pParse->db
2735a 3b 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 6e 4d  ;.  if( pTab->nM
2735b 6f 64 75 6c 65 41 72 67 3c 31 20 29 20 72 65 74  oduleArg<1 ) ret
2735c 75 72 6e 3b 0a 20 20 7a 4d 6f 64 75 6c 65 20 3d  urn;.  zModule =
2735d 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41   pTab->azModuleA
2735e 72 67 5b 30 5d 3b 0a 20 20 70 4d 6f 64 20 3d 20  rg[0];.  pMod = 
2735f 28 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65  (Module *)sqlite
27360 33 48 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61  3HashFind(&db->a
27361 4d 6f 64 75 6c 65 2c 20 7a 4d 6f 64 75 6c 65 2c  Module, zModule,
27362 20 73 74 72 6c 65 6e 28 7a 4d 6f 64 75 6c 65 29   strlen(zModule)
27363 29 3b 0a 20 20 70 54 61 62 2d 3e 70 4d 6f 64 20  );.  pTab->pMod 
27364 3d 20 70 4d 6f 64 3b 0a 20 20 0a 20 20 2f 2a 20  = pMod;.  .  /* 
27365 49 66 20 74 68 65 20 43 52 45 41 54 45 20 56 49  If the CREATE VI
27366 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74  RTUAL TABLE stat
27367 65 6d 65 6e 74 20 69 73 20 62 65 69 6e 67 20 65  ement is being e
27368 6e 74 65 72 65 64 20 66 6f 72 20 74 68 65 0a 20  ntered for the. 
27369 20 2a 2a 20 66 69 72 73 74 20 74 69 6d 65 20 28   ** first time (
2736a 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 20 69  in other words i
2736b 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  f the virtual ta
2736c 62 6c 65 20 69 73 20 61 63 74 75 61 6c 6c 79 20  ble is actually 
2736d 62 65 69 6e 67 0a 20 20 2a 2a 20 63 72 65 61 74  being.  ** creat
2736e 65 64 20 6e 6f 77 20 69 6e 73 74 65 61 64 20 6f  ed now instead o
2736f 66 20 6a 75 73 74 20 62 65 69 6e 67 20 72 65 61  f just being rea
27370 64 20 6f 75 74 20 6f 66 20 73 71 6c 69 74 65 5f  d out of sqlite_
27371 6d 61 73 74 65 72 29 20 74 68 65 6e 0a 20 20 2a  master) then.  *
27372 2a 20 64 6f 20 61 64 64 69 74 69 6f 6e 61 6c 20  * do additional 
27373 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 77  initialization w
27374 6f 72 6b 20 61 6e 64 20 73 74 6f 72 65 20 74 68  ork and store th
27375 65 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78 74  e statement text
27376 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c  .  ** in the sql
27377 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
27378 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62  ..  */.  if( !db
27379 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20  ->init.busy ){. 
2737a 20 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 0a     char *zStmt;.
2737b 20 20 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65      char *zWhere
2737c 3b 0a 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20  ;.    int iDb;. 
2737d 20 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 20     Vdbe *v;..   
2737e 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20   /* Compute the 
2737f 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66  complete text of
27380 20 74 68 65 20 43 52 45 41 54 45 20 56 49 52 54   the CREATE VIRT
27381 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74 65 6d  UAL TABLE statem
27382 65 6e 74 20 2a 2f 0a 20 20 20 20 69 66 28 20 70  ent */.    if( p
27383 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 70 50 61  End ){.      pPa
27384 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e  rse->sNameToken.
27385 6e 20 3d 20 70 45 6e 64 2d 3e 7a 20 2d 20 70 50  n = pEnd->z - pP
27386 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e  arse->sNameToken
27387 2e 7a 20 2b 20 70 45 6e 64 2d 3e 6e 3b 0a 20 20  .z + pEnd->n;.  
27388 20 20 7d 0a 20 20 20 20 7a 53 74 6d 74 20 3d 20    }.    zStmt = 
27389 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
2738a 62 2c 20 22 43 52 45 41 54 45 20 56 49 52 54 55  b, "CREATE VIRTU
2738b 41 4c 20 54 41 42 4c 45 20 25 54 22 2c 20 26 70  AL TABLE %T", &p
2738c 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65  Parse->sNameToke
2738d 6e 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 20 73 6c  n);..    /* A sl
2738e 6f 74 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72  ot for the recor
2738f 64 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  d has already be
27390 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20  en allocated in 
27391 74 68 65 20 0a 20 20 20 20 2a 2a 20 53 51 4c 49  the .    ** SQLI
27392 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e  TE_MASTER table.
27393 20 20 57 65 20 6a 75 73 74 20 6e 65 65 64 20 74    We just need t
27394 6f 20 75 70 64 61 74 65 20 74 68 61 74 20 73 6c  o update that sl
27395 6f 74 20 77 69 74 68 20 61 6c 6c 0a 20 20 20 20  ot with all.    
27396 2a 2a 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69  ** the informati
27397 6f 6e 20 77 65 27 76 65 20 63 6f 6c 6c 65 63 74  on we've collect
27398 65 64 2e 20 20 0a 20 20 20 20 2a 2a 0a 20 20 20  ed.  .    **.   
27399 20 2a 2a 20 54 68 65 20 56 4d 20 72 65 67 69 73   ** The VM regis
2739a 74 65 72 20 6e 75 6d 62 65 72 20 70 50 61 72 73  ter number pPars
2739b 65 2d 3e 72 65 67 52 6f 77 69 64 20 68 6f 6c 64  e->regRowid hold
2739c 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 61  s the rowid of a
2739d 6e 0a 20 20 20 20 2a 2a 20 65 6e 74 72 79 20 69  n.    ** entry i
2739e 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  n the sqlite_mas
2739f 74 65 72 20 74 61 62 6c 65 20 74 68 74 20 77 61  ter table tht wa
273a0 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 74 68  s created for th
273a1 69 73 20 76 74 61 62 0a 20 20 20 20 2a 2a 20 62  is vtab.    ** b
273a2 79 20 73 71 6c 69 74 65 33 53 74 61 72 74 54 61  y sqlite3StartTa
273a3 62 6c 65 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  ble()..    */.  
273a4 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
273a5 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
273a6 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
273a7 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74  .    sqlite3Nest
273a8 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a  edParse(pParse,.
273a9 20 20 20 20 20 20 22 55 50 44 41 54 45 20 25 51        "UPDATE %Q
273aa 2e 25 73 20 22 0a 20 20 20 20 20 20 20 20 20 22  .%s ".         "
273ab 53 45 54 20 74 79 70 65 3d 27 74 61 62 6c 65 27  SET type='table'
273ac 2c 20 6e 61 6d 65 3d 25 51 2c 20 74 62 6c 5f 6e  , name=%Q, tbl_n
273ad 61 6d 65 3d 25 51 2c 20 72 6f 6f 74 70 61 67 65  ame=%Q, rootpage
273ae 3d 30 2c 20 73 71 6c 3d 25 51 20 22 0a 20 20 20  =0, sql=%Q ".   
273af 20 20 20 20 22 57 48 45 52 45 20 72 6f 77 69 64      "WHERE rowid
273b0 3d 23 25 64 22 2c 0a 20 20 20 20 20 20 64 62 2d  =#%d",.      db-
273b1 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c  >aDb[iDb].zName,
273b2 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44   SCHEMA_TABLE(iD
273b3 62 29 2c 0a 20 20 20 20 20 20 70 54 61 62 2d 3e  b),.      pTab->
273b4 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 70 54 61  zName,.      pTa
273b5 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20  b->zName,.      
273b6 7a 53 74 6d 74 2c 0a 20 20 20 20 20 20 70 50 61  zStmt,.      pPa
273b7 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 0a 20 20  rse->regRowid.  
273b8 20 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33    );.    sqlite3
273b9 44 62 46 72 65 65 28 64 62 2c 20 7a 53 74 6d 74  DbFree(db, zStmt
273ba 29 3b 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74  );.    v = sqlit
273bb 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
273bc 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68  );.    sqlite3Ch
273bd 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73  angeCookie(pPars
273be 65 2c 20 69 44 62 29 3b 0a 0a 20 20 20 20 73 71  e, iDb);..    sq
273bf 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
273c0 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20 30 2c  v, OP_Expire, 0,
273c1 20 30 29 3b 0a 20 20 20 20 7a 57 68 65 72 65 20   0);.    zWhere 
273c2 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
273c3 28 64 62 2c 20 22 6e 61 6d 65 3d 27 25 71 27 22  (db, "name='%q'"
273c4 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
273c5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
273c6 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 73  ddOp4(v, OP_Pars
273c7 65 53 63 68 65 6d 61 2c 20 69 44 62 2c 20 31 2c  eSchema, iDb, 1,
273c8 20 30 2c 20 7a 57 68 65 72 65 2c 20 50 34 5f 44   0, zWhere, P4_D
273c9 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 73 71 6c  YNAMIC);.    sql
273ca 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
273cb 2c 20 4f 50 5f 56 43 72 65 61 74 65 2c 20 69 44  , OP_VCreate, iD
273cc 62 2c 20 30 2c 20 30 2c 20 0a 20 20 20 20 20 20  b, 0, 0, .      
273cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
273ce 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20     pTab->zName, 
273cf 73 74 72 6c 65 6e 28 70 54 61 62 2d 3e 7a 4e 61  strlen(pTab->zNa
273d0 6d 65 29 20 2b 20 31 29 3b 0a 20 20 7d 0a 0a 20  me) + 1);.  }.. 
273d1 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 72 65   /* If we are re
273d2 72 65 61 64 69 6e 67 20 74 68 65 20 73 71 6c 69  reading the sqli
273d3 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20  te_master table 
273d4 63 72 65 61 74 65 20 74 68 65 20 69 6e 2d 6d 65  create the in-me
273d5 6d 6f 72 79 0a 20 20 2a 2a 20 72 65 63 6f 72 64  mory.  ** record
273d6 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 49   of the table. I
273d7 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 68 61 73  f the module has
273d8 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 72 65   already been re
273d9 67 69 73 74 65 72 65 64 2c 0a 20 20 2a 2a 20 61  gistered,.  ** a
273da 6c 73 6f 20 63 61 6c 6c 20 74 68 65 20 78 43 6f  lso call the xCo
273db 6e 6e 65 63 74 20 6d 65 74 68 6f 64 20 68 65 72  nnect method her
273dc 65 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 20 7b  e..  */.  else {
273dd 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 4f 6c 64  .    Table *pOld
273de 3b 0a 20 20 20 20 53 63 68 65 6d 61 20 2a 70 53  ;.    Schema *pS
273df 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53  chema = pTab->pS
273e0 63 68 65 6d 61 3b 0a 20 20 20 20 63 6f 6e 73 74  chema;.    const
273e1 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70   char *zName = p
273e2 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20  Tab->zName;.    
273e3 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c  int nName = strl
273e4 65 6e 28 7a 4e 61 6d 65 29 20 2b 20 31 3b 0a 20  en(zName) + 1;. 
273e5 20 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65     pOld = sqlite
273e6 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 53 63  3HashInsert(&pSc
273e7 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 7a  hema->tblHash, z
273e8 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 70 54 61  Name, nName, pTa
273e9 62 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c 64  b);.    if( pOld
273ea 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61   ){.      db->ma
273eb 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
273ec 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54        assert( pT
273ed 61 62 3d 3d 70 4f 6c 64 20 29 3b 20 20 2f 2a 20  ab==pOld );  /* 
273ee 4d 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61 76 65  Malloc must have
273ef 20 66 61 69 6c 65 64 20 69 6e 73 69 64 65 20 48   failed inside H
273f0 61 73 68 49 6e 73 65 72 74 28 29 20 2a 2f 0a 20  ashInsert() */. 
273f1 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
273f2 20 7d 0a 20 20 20 20 70 53 63 68 65 6d 61 2d 3e   }.    pSchema->
273f3 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
273f4 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70 4e 65  .    pParse->pNe
273f5 77 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20 7d 0a  wTable = 0;.  }.
273f6 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72  }../*.** The par
273f7 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72  ser calls this r
273f8 6f 75 74 69 6e 65 20 77 68 65 6e 20 69 74 20 73  outine when it s
273f9 65 65 73 20 74 68 65 20 66 69 72 73 74 20 74 6f  ees the first to
273fa 6b 65 6e 0a 2a 2a 20 6f 66 20 61 6e 20 61 72 67  ken.** of an arg
273fb 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 6d 6f 64  ument to the mod
273fc 75 6c 65 20 6e 61 6d 65 20 69 6e 20 61 20 43 52  ule name in a CR
273fd 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42  EATE VIRTUAL TAB
273fe 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f  LE statement..*/
273ff 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
27400 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
27401 41 72 67 49 6e 69 74 28 50 61 72 73 65 20 2a 70  ArgInit(Parse *p
27402 50 61 72 73 65 29 7b 0a 20 20 61 64 64 41 72 67  Parse){.  addArg
27403 75 6d 65 6e 74 54 6f 56 74 61 62 28 70 50 61 72  umentToVtab(pPar
27404 73 65 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73  se);.  pParse->s
27405 41 72 67 2e 7a 20 3d 20 30 3b 0a 20 20 70 50 61  Arg.z = 0;.  pPa
27406 72 73 65 2d 3e 73 41 72 67 2e 6e 20 3d 20 30 3b  rse->sArg.n = 0;
27407 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61  .}../*.** The pa
27408 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20  rser calls this 
27409 72 6f 75 74 69 6e 65 20 66 6f 72 20 65 61 63 68  routine for each
2740a 20 74 6f 6b 65 6e 20 61 66 74 65 72 20 74 68 65   token after the
2740b 20 66 69 72 73 74 20 74 6f 6b 65 6e 0a 2a 2a 20   first token.** 
2740c 69 6e 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74  in an argument t
2740d 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d  o the module nam
2740e 65 20 69 6e 20 61 20 43 52 45 41 54 45 20 56 49  e in a CREATE VI
2740f 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74  RTUAL TABLE stat
27410 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ement..*/.SQLITE
27411 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
27412 6c 69 74 65 33 56 74 61 62 41 72 67 45 78 74 65  lite3VtabArgExte
27413 6e 64 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  nd(Parse *pParse
27414 2c 20 54 6f 6b 65 6e 20 2a 70 29 7b 0a 20 20 54  , Token *p){.  T
27415 6f 6b 65 6e 20 2a 70 41 72 67 20 3d 20 26 70 50  oken *pArg = &pP
27416 61 72 73 65 2d 3e 73 41 72 67 3b 0a 20 20 69 66  arse->sArg;.  if
27417 28 20 70 41 72 67 2d 3e 7a 3d 3d 30 20 29 7b 0a  ( pArg->z==0 ){.
27418 20 20 20 20 70 41 72 67 2d 3e 7a 20 3d 20 70 2d      pArg->z = p-
27419 3e 7a 3b 0a 20 20 20 20 70 41 72 67 2d 3e 6e 20  >z;.    pArg->n 
2741a 3d 20 70 2d 3e 6e 3b 0a 20 20 7d 65 6c 73 65 7b  = p->n;.  }else{
2741b 0a 20 20 20 20 61 73 73 65 72 74 28 70 41 72 67  .    assert(pArg
2741c 2d 3e 7a 20 3c 20 70 2d 3e 7a 29 3b 0a 20 20 20  ->z < p->z);.   
2741d 20 70 41 72 67 2d 3e 6e 20 3d 20 28 70 2d 3e 7a   pArg->n = (p->z
2741e 20 2b 20 70 2d 3e 6e 20 2d 20 70 41 72 67 2d 3e   + p->n - pArg->
2741f 7a 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  z);.  }.}../*.**
27420 20 49 6e 76 6f 6b 65 20 61 20 76 69 72 74 75 61   Invoke a virtua
27421 6c 20 74 61 62 6c 65 20 63 6f 6e 73 74 72 75 63  l table construc
27422 74 6f 72 20 28 65 69 74 68 65 72 20 78 43 72 65  tor (either xCre
27423 61 74 65 20 6f 72 20 78 43 6f 6e 6e 65 63 74 29  ate or xConnect)
27424 2e 20 54 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 72  . The.** pointer
27425 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   to the function
27426 20 74 6f 20 69 6e 76 6f 6b 65 20 69 73 20 70 61   to invoke is pa
27427 73 73 65 64 20 61 73 20 74 68 65 20 66 6f 75 72  ssed as the four
27428 74 68 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20  th parameter.** 
27429 74 6f 20 74 68 69 73 20 70 72 6f 63 65 64 75 72  to this procedur
2742a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
2742b 20 76 74 61 62 43 61 6c 6c 43 6f 6e 73 74 72 75   vtabCallConstru
2742c 63 74 6f 72 28 0a 20 20 73 71 6c 69 74 65 33 20  ctor(.  sqlite3 
2742d 2a 64 62 2c 20 0a 20 20 54 61 62 6c 65 20 2a 70  *db, .  Table *p
2742e 54 61 62 2c 0a 20 20 4d 6f 64 75 6c 65 20 2a 70  Tab,.  Module *p
2742f 4d 6f 64 2c 0a 20 20 69 6e 74 20 28 2a 78 43 6f  Mod,.  int (*xCo
27430 6e 73 74 72 75 63 74 29 28 73 71 6c 69 74 65 33  nstruct)(sqlite3
27431 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  *,void*,int,cons
27432 74 20 63 68 61 72 2a 63 6f 6e 73 74 2a 2c 73 71  t char*const*,sq
27433 6c 69 74 65 33 5f 76 74 61 62 2a 2a 2c 63 68 61  lite3_vtab**,cha
27434 72 2a 2a 29 2c 0a 20 20 63 68 61 72 20 2a 2a 70  r**),.  char **p
27435 7a 45 72 72 0a 29 7b 0a 20 20 69 6e 74 20 72 63  zErr.){.  int rc
27436 3b 0a 20 20 69 6e 74 20 72 63 32 3b 0a 20 20 73  ;.  int rc2;.  s
27437 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
27438 61 62 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20  ab = 0;.  const 
27439 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 7a 41 72  char *const*azAr
2743a 67 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  g = (const char 
2743b 2a 63 6f 6e 73 74 2a 29 70 54 61 62 2d 3e 61 7a  *const*)pTab->az
2743c 4d 6f 64 75 6c 65 41 72 67 3b 0a 20 20 69 6e 74  ModuleArg;.  int
2743d 20 6e 41 72 67 20 3d 20 70 54 61 62 2d 3e 6e 4d   nArg = pTab->nM
2743e 6f 64 75 6c 65 41 72 67 3b 0a 20 20 63 68 61 72  oduleArg;.  char
2743f 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 20 20 63 68   *zErr = 0;.  ch
27440 61 72 20 2a 7a 4d 6f 64 75 6c 65 4e 61 6d 65 20  ar *zModuleName 
27441 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
27442 28 64 62 2c 20 22 25 73 22 2c 20 70 54 61 62 2d  (db, "%s", pTab-
27443 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 69 66 28 20  >zName);..  if( 
27444 21 7a 4d 6f 64 75 6c 65 4e 61 6d 65 20 29 7b 0a  !zModuleName ){.
27445 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
27446 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20  E_NOMEM;.  }..  
27447 61 73 73 65 72 74 28 20 21 64 62 2d 3e 70 56 54  assert( !db->pVT
27448 61 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ab );.  assert( 
27449 78 43 6f 6e 73 74 72 75 63 74 20 29 3b 0a 0a 20  xConstruct );.. 
2744a 20 64 62 2d 3e 70 56 54 61 62 20 3d 20 70 54 61   db->pVTab = pTa
2744b 62 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  b;.  rc = sqlite
2744c 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
2744d 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
2744e 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 63 20  LITE_OK );.  rc 
2744f 3d 20 78 43 6f 6e 73 74 72 75 63 74 28 64 62 2c  = xConstruct(db,
27450 20 70 4d 6f 64 2d 3e 70 41 75 78 2c 20 6e 41 72   pMod->pAux, nAr
27451 67 2c 20 61 7a 41 72 67 2c 20 26 70 56 74 61 62  g, azArg, &pVtab
27452 2c 20 26 7a 45 72 72 29 3b 0a 20 20 72 63 32 20  , &zErr);.  rc2 
27453 3d 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  = sqlite3SafetyO
27454 6e 28 64 62 29 3b 0a 20 20 69 66 28 20 72 63 3d  n(db);.  if( rc=
27455 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 56  =SQLITE_OK && pV
27456 74 61 62 20 29 7b 0a 20 20 20 20 70 56 74 61 62  tab ){.    pVtab
27457 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 4d 6f 64  ->pModule = pMod
27458 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 70  ->pModule;.    p
27459 56 74 61 62 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a  Vtab->nRef = 1;.
2745a 20 20 20 20 70 54 61 62 2d 3e 70 56 74 61 62 20      pTab->pVtab 
2745b 3d 20 70 56 74 61 62 3b 0a 20 20 7d 0a 0a 20 20  = pVtab;.  }..  
2745c 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72  if( SQLITE_OK!=r
2745d 63 20 29 7b 0a 20 20 20 20 69 66 28 20 7a 45 72  c ){.    if( zEr
2745e 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a 70  r==0 ){.      *p
2745f 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50  zErr = sqlite3MP
27460 72 69 6e 74 66 28 64 62 2c 20 22 76 74 61 62 6c  rintf(db, "vtabl
27461 65 20 63 6f 6e 73 74 72 75 63 74 6f 72 20 66 61  e constructor fa
27462 69 6c 65 64 3a 20 25 73 22 2c 20 7a 4d 6f 64 75  iled: %s", zModu
27463 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c  leName);.    }el
27464 73 65 20 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72  se {.      *pzEr
27465 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  r = sqlite3MPrin
27466 74 66 28 64 62 2c 20 22 25 73 22 2c 20 7a 45 72  tf(db, "%s", zEr
27467 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  r);.      sqlite
27468 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72  3DbFree(db, zErr
27469 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
2746a 20 69 66 28 20 64 62 2d 3e 70 56 54 61 62 20 29   if( db->pVTab )
2746b 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
2746c 20 2a 7a 46 6f 72 6d 61 74 20 3d 20 22 76 74 61   *zFormat = "vta
2746d 62 6c 65 20 63 6f 6e 73 74 72 75 63 74 6f 72 20  ble constructor 
2746e 64 69 64 20 6e 6f 74 20 64 65 63 6c 61 72 65 20  did not declare 
2746f 73 63 68 65 6d 61 3a 20 25 73 22 3b 0a 20 20 20  schema: %s";.   
27470 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74 65   *pzErr = sqlite
27471 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f  3MPrintf(db, zFo
27472 72 6d 61 74 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  rmat, pTab->zNam
27473 65 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  e);.    rc = SQL
27474 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 20 0a  ITE_ERROR;.  } .
27475 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
27476 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  _OK ){.    rc = 
27477 72 63 32 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 70  rc2;.  }.  db->p
27478 56 54 61 62 20 3d 20 30 3b 0a 20 20 73 71 6c 69  VTab = 0;.  sqli
27479 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d  te3DbFree(db, zM
2747a 6f 64 75 6c 65 4e 61 6d 65 29 3b 0a 0a 20 20 2f  oduleName);..  /
2747b 2a 20 49 66 20 65 76 65 72 79 74 68 69 6e 67 20  * If everything 
2747c 77 65 6e 74 20 61 63 63 6f 72 64 69 6e 67 20 74  went according t
2747d 6f 20 70 6c 61 6e 2c 20 6c 6f 6f 70 20 74 68 72  o plan, loop thr
2747e 6f 75 67 68 20 74 68 65 20 63 6f 6c 75 6d 6e 73  ough the columns
2747f 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 61 62  .  ** of the tab
27480 6c 65 20 74 6f 20 73 65 65 20 69 66 20 61 6e 79  le to see if any
27481 20 6f 66 20 74 68 65 6d 20 63 6f 6e 74 61 69 6e   of them contain
27482 20 74 68 65 20 74 6f 6b 65 6e 20 22 68 69 64 64   the token "hidd
27483 65 6e 22 2e 0a 20 20 2a 2a 20 49 66 20 73 6f 2c  en"..  ** If so,
27484 20 73 65 74 20 74 68 65 20 43 6f 6c 75 6d 6e 2e   set the Column.
27485 69 73 48 69 64 64 65 6e 20 66 6c 61 67 20 61 6e  isHidden flag an
27486 64 20 72 65 6d 6f 76 65 20 74 68 65 20 74 6f 6b  d remove the tok
27487 65 6e 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65  en from.  ** the
27488 20 74 79 70 65 20 73 74 72 69 6e 67 2e 0a 20 20   type string..  
27489 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  */.  if( rc==SQL
2748a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e  ITE_OK ){.    in
2748b 74 20 69 43 6f 6c 3b 0a 20 20 20 20 66 6f 72 28  t iCol;.    for(
2748c 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61  iCol=0; iCol<pTa
2748d 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29  b->nCol; iCol++)
2748e 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54  {.      char *zT
2748f 79 70 65 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c  ype = pTab->aCol
27490 5b 69 43 6f 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20  [iCol].zType;.  
27491 20 20 20 20 69 6e 74 20 6e 54 79 70 65 3b 0a 20      int nType;. 
27492 20 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a       int i = 0;.
27493 20 20 20 20 20 20 69 66 28 20 21 7a 54 79 70 65        if( !zType
27494 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
27495 20 20 20 6e 54 79 70 65 20 3d 20 73 74 72 6c 65     nType = strle
27496 6e 28 7a 54 79 70 65 29 3b 0a 20 20 20 20 20 20  n(zType);.      
27497 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49  if( sqlite3StrNI
27498 43 6d 70 28 22 68 69 64 64 65 6e 22 2c 20 7a 54  Cmp("hidden", zT
27499 79 70 65 2c 20 36 29 20 7c 7c 20 28 7a 54 79 70  ype, 6) || (zTyp
2749a 65 5b 36 5d 20 26 26 20 7a 54 79 70 65 5b 36 5d  e[6] && zType[6]
2749b 21 3d 27 20 27 29 20 29 7b 0a 20 20 20 20 20 20  !=' ') ){.      
2749c 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 54 79    for(i=0; i<nTy
2749d 70 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  pe; i++){.      
2749e 20 20 20 20 69 66 28 20 28 30 3d 3d 73 71 6c 69      if( (0==sqli
2749f 74 65 33 53 74 72 4e 49 43 6d 70 28 22 20 68 69  te3StrNICmp(" hi
274a0 64 64 65 6e 22 2c 20 26 7a 54 79 70 65 5b 69 5d  dden", &zType[i]
274a1 2c 20 37 29 29 0a 20 20 20 20 20 20 20 20 20 20  , 7)).          
274a2 20 26 26 20 28 7a 54 79 70 65 5b 69 2b 37 5d 3d   && (zType[i+7]=
274a3 3d 27 5c 30 27 20 7c 7c 20 7a 54 79 70 65 5b 69  ='\0' || zType[i
274a4 2b 37 5d 3d 3d 27 20 27 29 0a 20 20 20 20 20 20  +7]==' ').      
274a5 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20      ){.         
274a6 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20     i++;.        
274a7 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
274a8 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
274a9 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
274aa 66 28 20 69 3c 6e 54 79 70 65 20 29 7b 0a 20 20  f( i<nType ){.  
274ab 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20        int j;.   
274ac 20 20 20 20 20 69 6e 74 20 6e 44 65 6c 20 3d 20       int nDel = 
274ad 36 20 2b 20 28 7a 54 79 70 65 5b 69 2b 36 5d 20  6 + (zType[i+6] 
274ae 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 20 20  ? 1 : 0);.      
274af 20 20 66 6f 72 28 6a 3d 69 3b 20 28 6a 2b 6e 44    for(j=i; (j+nD
274b0 65 6c 29 3c 3d 6e 54 79 70 65 3b 20 6a 2b 2b 29  el)<=nType; j++)
274b1 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 54 79 70  {.          zTyp
274b2 65 5b 6a 5d 20 3d 20 7a 54 79 70 65 5b 6a 2b 6e  e[j] = zType[j+n
274b3 44 65 6c 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Del];.        }.
274b4 20 20 20 20 20 20 20 20 69 66 28 20 7a 54 79 70          if( zTyp
274b5 65 5b 69 5d 3d 3d 27 5c 30 27 20 26 26 20 69 3e  e[i]=='\0' && i>
274b6 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  0 ){.          a
274b7 73 73 65 72 74 28 7a 54 79 70 65 5b 69 2d 31 5d  ssert(zType[i-1]
274b8 3d 3d 27 20 27 29 3b 0a 20 20 20 20 20 20 20 20  ==' ');.        
274b9 20 20 7a 54 79 70 65 5b 69 2d 31 5d 20 3d 20 27    zType[i-1] = '
274ba 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  \0';.        }. 
274bb 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61 43 6f         pTab->aCo
274bc 6c 5b 69 43 6f 6c 5d 2e 69 73 48 69 64 64 65 6e  l[iCol].isHidden
274bd 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
274be 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
274bf 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
274c0 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69  is function is i
274c1 6e 76 6f 6b 65 64 20 62 79 20 74 68 65 20 70 61  nvoked by the pa
274c2 72 73 65 72 20 74 6f 20 63 61 6c 6c 20 74 68 65  rser to call the
274c3 20 78 43 6f 6e 6e 65 63 74 28 29 20 6d 65 74 68   xConnect() meth
274c4 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20 76 69 72  od.** of the vir
274c5 74 75 61 6c 20 74 61 62 6c 65 20 70 54 61 62 2e  tual table pTab.
274c6 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
274c7 75 72 73 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f  urs, an error co
274c8 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 0a  de is returned .
274c9 2a 2a 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20  ** and an error 
274ca 6c 65 66 74 20 69 6e 20 70 50 61 72 73 65 2e 0a  left in pParse..
274cb 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20  **.** This call 
274cc 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 61  is a no-op if ta
274cd 62 6c 65 20 70 54 61 62 20 69 73 20 6e 6f 74 20  ble pTab is not 
274ce 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  a virtual table.
274cf 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
274d0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74  TE int sqlite3Vt
274d1 61 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 50 61  abCallConnect(Pa
274d2 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62  rse *pParse, Tab
274d3 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 4d 6f 64  le *pTab){.  Mod
274d4 75 6c 65 20 2a 70 4d 6f 64 3b 0a 20 20 69 6e 74  ule *pMod;.  int
274d5 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
274d6 0a 0a 20 20 69 66 28 20 21 70 54 61 62 20 7c 7c  ..  if( !pTab ||
274d7 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73   (pTab->tabFlags
274d8 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29 3d 3d   & TF_Virtual)==
274d9 30 20 7c 7c 20 70 54 61 62 2d 3e 70 56 74 61 62  0 || pTab->pVtab
274da 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
274db 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20  QLITE_OK;.  }.. 
274dc 20 70 4d 6f 64 20 3d 20 70 54 61 62 2d 3e 70 4d   pMod = pTab->pM
274dd 6f 64 3b 0a 20 20 69 66 28 20 21 70 4d 6f 64 20  od;.  if( !pMod 
274de 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ){.    const cha
274df 72 20 2a 7a 4d 6f 64 75 6c 65 20 3d 20 70 54 61  r *zModule = pTa
274e0 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30  b->azModuleArg[0
274e1 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ];.    sqlite3Er
274e2 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
274e3 6e 6f 20 73 75 63 68 20 6d 6f 64 75 6c 65 3a 20  no such module: 
274e4 25 73 22 2c 20 7a 4d 6f 64 75 6c 65 29 3b 0a 20  %s", zModule);. 
274e5 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
274e6 52 52 4f 52 3b 0a 20 20 7d 20 65 6c 73 65 20 7b  RROR;.  } else {
274e7 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72 20  .    char *zErr 
274e8 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 0;.    sqlite3
274e9 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
274ea 62 3b 0a 20 20 20 20 72 63 20 3d 20 76 74 61 62  b;.    rc = vtab
274eb 43 61 6c 6c 43 6f 6e 73 74 72 75 63 74 6f 72 28  CallConstructor(
274ec 64 62 2c 20 70 54 61 62 2c 20 70 4d 6f 64 2c 20  db, pTab, pMod, 
274ed 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78  pMod->pModule->x
274ee 43 6f 6e 6e 65 63 74 2c 20 26 7a 45 72 72 29 3b  Connect, &zErr);
274ef 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
274f0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
274f1 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
274f2 70 50 61 72 73 65 2c 20 22 25 73 22 2c 20 7a 45  pParse, "%s", zE
274f3 72 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  rr);.    }.    s
274f4 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
274f5 20 7a 45 72 72 29 3b 0a 20 20 7d 0a 0a 20 20 72   zErr);.  }..  r
274f6 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
274f7 2a 2a 20 41 64 64 20 74 68 65 20 76 69 72 74 75  ** Add the virtu
274f8 61 6c 20 74 61 62 6c 65 20 70 56 74 61 62 20 74  al table pVtab t
274f9 6f 20 74 68 65 20 61 72 72 61 79 20 73 71 6c 69  o the array sqli
274fa 74 65 33 2e 61 56 54 72 61 6e 73 5b 5d 2e 0a 2a  te3.aVTrans[]..*
274fb 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 64 64  /.static int add
274fc 54 6f 56 54 72 61 6e 73 28 73 71 6c 69 74 65 33  ToVTrans(sqlite3
274fd 20 2a 64 62 2c 20 73 71 6c 69 74 65 33 5f 76 74   *db, sqlite3_vt
274fe 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 63 6f  ab *pVtab){.  co
274ff 6e 73 74 20 69 6e 74 20 41 52 52 41 59 5f 49 4e  nst int ARRAY_IN
27500 43 52 20 3d 20 35 3b 0a 0a 20 20 2f 2a 20 47 72  CR = 5;..  /* Gr
27501 6f 77 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61  ow the sqlite3.a
27502 56 54 72 61 6e 73 20 61 72 72 61 79 20 69 66 20  VTrans array if 
27503 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 66  required */.  if
27504 28 20 28 64 62 2d 3e 6e 56 54 72 61 6e 73 25 41  ( (db->nVTrans%A
27505 52 52 41 59 5f 49 4e 43 52 29 3d 3d 30 20 29 7b  RRAY_INCR)==0 ){
27506 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61  .    sqlite3_vta
27507 62 20 2a 2a 61 56 54 72 61 6e 73 3b 0a 20 20 20  b **aVTrans;.   
27508 20 69 6e 74 20 6e 42 79 74 65 73 20 3d 20 73 69   int nBytes = si
27509 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 76 74 61  zeof(sqlite3_vta
2750a 62 20 2a 29 20 2a 20 28 64 62 2d 3e 6e 56 54 72  b *) * (db->nVTr
2750b 61 6e 73 20 2b 20 41 52 52 41 59 5f 49 4e 43 52  ans + ARRAY_INCR
2750c 29 3b 0a 20 20 20 20 61 56 54 72 61 6e 73 20 3d  );.    aVTrans =
2750d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f   sqlite3DbReallo
2750e 63 28 64 62 2c 20 28 76 6f 69 64 20 2a 29 64 62  c(db, (void *)db
2750f 2d 3e 61 56 54 72 61 6e 73 2c 20 6e 42 79 74 65  ->aVTrans, nByte
27510 73 29 3b 0a 20 20 20 20 69 66 28 20 21 61 56 54  s);.    if( !aVT
27511 72 61 6e 73 20 29 7b 0a 20 20 20 20 20 20 72 65  rans ){.      re
27512 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
27513 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d  M;.    }.    mem
27514 73 65 74 28 26 61 56 54 72 61 6e 73 5b 64 62 2d  set(&aVTrans[db-
27515 3e 6e 56 54 72 61 6e 73 5d 2c 20 30 2c 20 73 69  >nVTrans], 0, si
27516 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 76 74 61  zeof(sqlite3_vta
27517 62 20 2a 29 2a 41 52 52 41 59 5f 49 4e 43 52 29  b *)*ARRAY_INCR)
27518 3b 0a 20 20 20 20 64 62 2d 3e 61 56 54 72 61 6e  ;.    db->aVTran
27519 73 20 3d 20 61 56 54 72 61 6e 73 3b 0a 20 20 7d  s = aVTrans;.  }
2751a 0a 0a 20 20 2f 2a 20 41 64 64 20 70 56 74 61 62  ..  /* Add pVtab
2751b 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 73   to the end of s
2751c 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 20 2a  qlite3.aVTrans *
2751d 2f 0a 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b  /.  db->aVTrans[
2751e 64 62 2d 3e 6e 56 54 72 61 6e 73 2b 2b 5d 20 3d  db->nVTrans++] =
2751f 20 70 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 65   pVtab;.  sqlite
27520 33 56 74 61 62 4c 6f 63 6b 28 70 56 74 61 62 29  3VtabLock(pVtab)
27521 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
27522 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  E_OK;.}../*.** T
27523 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
27524 69 6e 76 6f 6b 65 64 20 62 79 20 74 68 65 20 76  invoked by the v
27525 64 62 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20  dbe to call the 
27526 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64 0a 2a  xCreate method.*
27527 2a 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c  * of the virtual
27528 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 61   table named zTa
27529 62 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 44  b in database iD
2752a 62 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  b. .**.** If an 
2752b 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 2a 70  error occurs, *p
2752c 7a 45 72 72 20 69 73 20 73 65 74 20 74 6f 20 70  zErr is set to p
2752d 6f 69 6e 74 20 61 6e 20 61 6e 20 45 6e 67 6c 69  oint an an Engli
2752e 73 68 20 6c 61 6e 67 75 61 67 65 0a 2a 2a 20 64  sh language.** d
2752f 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68  escription of th
27530 65 20 65 72 72 6f 72 20 61 6e 64 20 61 6e 20 53  e error and an S
27531 51 4c 49 54 45 5f 58 58 58 20 65 72 72 6f 72 20  QLITE_XXX error 
27532 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
27533 2e 0a 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73  ..** In this cas
27534 65 20 74 68 65 20 63 61 6c 6c 65 72 20 6d 75 73  e the caller mus
27535 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 62  t call sqlite3Db
27536 46 72 65 65 28 64 62 2c 20 29 20 6f 6e 20 2a 70  Free(db, ) on *p
27537 7a 45 72 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  zErr..*/.SQLITE_
27538 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
27539 74 65 33 56 74 61 62 43 61 6c 6c 43 72 65 61 74  te3VtabCallCreat
2753a 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  e(sqlite3 *db, i
2753b 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68  nt iDb, const ch
2753c 61 72 20 2a 7a 54 61 62 2c 20 63 68 61 72 20 2a  ar *zTab, char *
2753d 2a 70 7a 45 72 72 29 7b 0a 20 20 69 6e 74 20 72  *pzErr){.  int r
2753e 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
2753f 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
27540 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 0a 20 20  Module *pMod;.  
27541 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64  const char *zMod
27542 75 6c 65 3b 0a 0a 20 20 70 54 61 62 20 3d 20 73  ule;..  pTab = s
27543 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
27544 64 62 2c 20 7a 54 61 62 2c 20 64 62 2d 3e 61 44  db, zTab, db->aD
27545 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  b[iDb].zName);. 
27546 20 61 73 73 65 72 74 28 70 54 61 62 20 26 26 20   assert(pTab && 
27547 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20  (pTab->tabFlags 
27548 26 20 54 46 5f 56 69 72 74 75 61 6c 29 21 3d 30  & TF_Virtual)!=0
27549 20 26 26 20 21 70 54 61 62 2d 3e 70 56 74 61 62   && !pTab->pVtab
2754a 29 3b 0a 20 20 70 4d 6f 64 20 3d 20 70 54 61 62  );.  pMod = pTab
2754b 2d 3e 70 4d 6f 64 3b 0a 20 20 7a 4d 6f 64 75 6c  ->pMod;.  zModul
2754c 65 20 3d 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75  e = pTab->azModu
2754d 6c 65 41 72 67 5b 30 5d 3b 0a 0a 20 20 2f 2a 20  leArg[0];..  /* 
2754e 49 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 68 61  If the module ha
2754f 73 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65  s been registere
27550 64 20 61 6e 64 20 69 6e 63 6c 75 64 65 73 20 61  d and includes a
27551 20 43 72 65 61 74 65 20 6d 65 74 68 6f 64 2c 20   Create method, 
27552 0a 20 20 2a 2a 20 69 6e 76 6f 6b 65 20 69 74 20  .  ** invoke it 
27553 6e 6f 77 2e 20 49 66 20 74 68 65 20 6d 6f 64 75  now. If the modu
27554 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20  le has not been 
27555 72 65 67 69 73 74 65 72 65 64 2c 20 72 65 74 75  registered, retu
27556 72 6e 20 61 6e 20 0a 20 20 2a 2a 20 65 72 72 6f  rn an .  ** erro
27557 72 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 64 6f  r. Otherwise, do
27558 20 6e 6f 74 68 69 6e 67 2e 0a 20 20 2a 2f 0a 20   nothing..  */. 
27559 20 69 66 28 20 21 70 4d 6f 64 20 29 7b 0a 20 20   if( !pMod ){.  
2755a 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74    *pzErr = sqlit
2755b 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e  e3MPrintf(db, "n
2755c 6f 20 73 75 63 68 20 6d 6f 64 75 6c 65 3a 20 25  o such module: %
2755d 73 22 2c 20 7a 4d 6f 64 75 6c 65 29 3b 0a 20 20  s", zModule);.  
2755e 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
2755f 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ROR;.  }else{.  
27560 20 20 72 63 20 3d 20 76 74 61 62 43 61 6c 6c 43    rc = vtabCallC
27561 6f 6e 73 74 72 75 63 74 6f 72 28 64 62 2c 20 70  onstructor(db, p
27562 54 61 62 2c 20 70 4d 6f 64 2c 20 70 4d 6f 64 2d  Tab, pMod, pMod-
27563 3e 70 4d 6f 64 75 6c 65 2d 3e 78 43 72 65 61 74  >pModule->xCreat
27564 65 2c 20 70 7a 45 72 72 29 3b 0a 20 20 7d 0a 0a  e, pzErr);.  }..
27565 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
27566 5f 4f 4b 20 26 26 20 70 54 61 62 2d 3e 70 56 74  _OK && pTab->pVt
27567 61 62 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  ab ){.      rc =
27568 20 61 64 64 54 6f 56 54 72 61 6e 73 28 64 62 2c   addToVTrans(db,
27569 20 70 54 61 62 2d 3e 70 56 74 61 62 29 3b 0a 20   pTab->pVtab);. 
2756a 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
2756b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
2756c 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
2756d 74 6f 20 73 65 74 20 74 68 65 20 73 63 68 65 6d  to set the schem
2756e 61 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 74  a of a virtual t
2756f 61 62 6c 65 2e 20 20 49 74 20 69 73 20 6f 6e 6c  able.  It is onl
27570 79 0a 2a 2a 20 76 61 6c 69 64 20 74 6f 20 63 61  y.** valid to ca
27571 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ll this function
27572 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65   from within the
27573 20 78 43 72 65 61 74 65 28 29 20 6f 72 20 78 43   xCreate() or xC
27574 6f 6e 6e 65 63 74 28 29 20 6f 66 20 61 0a 2a 2a  onnect() of a.**
27575 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
27576 6f 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  odule..*/.SQLITE
27577 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
27578 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28 73 71  _declare_vtab(sq
27579 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
2757a 20 63 68 61 72 20 2a 7a 43 72 65 61 74 65 54 61   char *zCreateTa
2757b 62 6c 65 29 7b 0a 20 20 50 61 72 73 65 20 73 50  ble){.  Parse sP
2757c 61 72 73 65 3b 0a 0a 20 20 69 6e 74 20 72 63 20  arse;..  int rc 
2757d 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 54  = SQLITE_OK;.  T
2757e 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 63 68  able *pTab;.  ch
2757f 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 0a 20  ar *zErr = 0;.. 
27580 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
27581 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
27582 0a 20 20 70 54 61 62 20 3d 20 64 62 2d 3e 70 56  .  pTab = db->pV
27583 54 61 62 3b 0a 20 20 69 66 28 20 21 70 54 61 62  Tab;.  if( !pTab
27584 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
27585 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
27586 4d 49 53 55 53 45 2c 20 30 29 3b 0a 20 20 20 20  MISUSE, 0);.    
27587 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
27588 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
27589 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2758a 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
2758b 61 73 73 65 72 74 28 28 70 54 61 62 2d 3e 74 61  assert((pTab->ta
2758c 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74  bFlags & TF_Virt
2758d 75 61 6c 29 21 3d 30 20 26 26 20 70 54 61 62 2d  ual)!=0 && pTab-
2758e 3e 6e 43 6f 6c 3d 3d 30 20 26 26 20 70 54 61 62  >nCol==0 && pTab
2758f 2d 3e 61 43 6f 6c 3d 3d 30 29 3b 0a 0a 20 20 6d  ->aCol==0);..  m
27590 65 6d 73 65 74 28 26 73 50 61 72 73 65 2c 20 30  emset(&sParse, 0
27591 2c 20 73 69 7a 65 6f 66 28 50 61 72 73 65 29 29  , sizeof(Parse))
27592 3b 0a 20 20 73 50 61 72 73 65 2e 64 65 63 6c 61  ;.  sParse.decla
27593 72 65 56 74 61 62 20 3d 20 31 3b 0a 20 20 73 50  reVtab = 1;.  sP
27594 61 72 73 65 2e 64 62 20 3d 20 64 62 3b 0a 0a 20  arse.db = db;.. 
27595 20 69 66 28 20 0a 20 20 20 20 20 20 53 51 4c 49   if( .      SQLI
27596 54 45 5f 4f 4b 20 3d 3d 20 73 71 6c 69 74 65 33  TE_OK == sqlite3
27597 52 75 6e 50 61 72 73 65 72 28 26 73 50 61 72 73  RunParser(&sPars
27598 65 2c 20 7a 43 72 65 61 74 65 54 61 62 6c 65 2c  e, zCreateTable,
27599 20 26 7a 45 72 72 29 20 26 26 20 0a 20 20 20 20   &zErr) && .    
2759a 20 20 73 50 61 72 73 65 2e 70 4e 65 77 54 61 62    sParse.pNewTab
2759b 6c 65 20 26 26 20 0a 20 20 20 20 20 20 21 73 50  le && .      !sP
2759c 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 2d 3e  arse.pNewTable->
2759d 70 53 65 6c 65 63 74 20 26 26 20 0a 20 20 20 20  pSelect && .    
2759e 20 20 28 73 50 61 72 73 65 2e 70 4e 65 77 54 61    (sParse.pNewTa
2759f 62 6c 65 2d 3e 74 61 62 46 6c 61 67 73 20 26 20  ble->tabFlags & 
275a0 54 46 5f 56 69 72 74 75 61 6c 29 3d 3d 30 0a 20  TF_Virtual)==0. 
275a1 20 29 7b 0a 20 20 20 20 70 54 61 62 2d 3e 61 43   ){.    pTab->aC
275a2 6f 6c 20 3d 20 73 50 61 72 73 65 2e 70 4e 65 77  ol = sParse.pNew
275a3 54 61 62 6c 65 2d 3e 61 43 6f 6c 3b 0a 20 20 20  Table->aCol;.   
275a4 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 73 50   pTab->nCol = sP
275a5 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 2d 3e  arse.pNewTable->
275a6 6e 43 6f 6c 3b 0a 20 20 20 20 73 50 61 72 73 65  nCol;.    sParse
275a7 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 6e 43 6f 6c  .pNewTable->nCol
275a8 20 3d 20 30 3b 0a 20 20 20 20 73 50 61 72 73 65   = 0;.    sParse
275a9 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 61 43 6f 6c  .pNewTable->aCol
275aa 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 70 56   = 0;.    db->pV
275ab 54 61 62 20 3d 20 30 3b 0a 20 20 7d 20 65 6c 73  Tab = 0;.  } els
275ac 65 20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  e {.    sqlite3E
275ad 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
275ae 45 52 52 4f 52 2c 20 7a 45 72 72 29 3b 0a 20 20  ERROR, zErr);.  
275af 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
275b0 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 72  db, zErr);.    r
275b1 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
275b2 3b 0a 20 20 7d 0a 20 20 73 50 61 72 73 65 2e 64  ;.  }.  sParse.d
275b3 65 63 6c 61 72 65 56 74 61 62 20 3d 20 30 3b 0a  eclareVtab = 0;.
275b4 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  .  sqlite3_final
275b5 69 7a 65 28 28 73 71 6c 69 74 65 33 5f 73 74 6d  ize((sqlite3_stm
275b6 74 2a 29 73 50 61 72 73 65 2e 70 56 64 62 65 29  t*)sParse.pVdbe)
275b7 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74  ;.  sqlite3Delet
275b8 65 54 61 62 6c 65 28 73 50 61 72 73 65 2e 70 4e  eTable(sParse.pN
275b9 65 77 54 61 62 6c 65 29 3b 0a 20 20 73 50 61 72  ewTable);.  sPar
275ba 73 65 2e 70 4e 65 77 54 61 62 6c 65 20 3d 20 30  se.pNewTable = 0
275bb 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 28 72 63  ;..  assert( (rc
275bc 26 30 78 66 66 29 3d 3d 72 63 20 29 3b 0a 20 20  &0xff)==rc );.  
275bd 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45  rc = sqlite3ApiE
275be 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73  xit(db, rc);.  s
275bf 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
275c0 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
275c1 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
275c2 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
275c3 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 20 62 79  on is invoked by
275c4 20 74 68 65 20 76 64 62 65 20 74 6f 20 63 61 6c   the vdbe to cal
275c5 6c 20 74 68 65 20 78 44 65 73 74 72 6f 79 20 6d  l the xDestroy m
275c6 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20  ethod.** of the 
275c7 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6e 61  virtual table na
275c8 6d 65 64 20 7a 54 61 62 20 69 6e 20 64 61 74 61  med zTab in data
275c9 62 61 73 65 20 69 44 62 2e 20 54 68 69 73 20 6f  base iDb. This o
275ca 63 63 75 72 73 0a 2a 2a 20 77 68 65 6e 20 61 20  ccurs.** when a 
275cb 44 52 4f 50 20 54 41 42 4c 45 20 69 73 20 6d 65  DROP TABLE is me
275cc 6e 74 69 6f 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  ntioned..**.** T
275cd 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f  his call is a no
275ce 2d 6f 70 20 69 66 20 7a 54 61 62 20 69 73 20 6e  -op if zTab is n
275cf 6f 74 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  ot a virtual tab
275d0 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
275d1 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
275d2 33 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f 79  3VtabCallDestroy
275d3 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
275d4 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61  t iDb, const cha
275d5 72 20 2a 7a 54 61 62 29 0a 7b 0a 20 20 69 6e 74  r *zTab).{.  int
275d6 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
275d7 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a  .  Table *pTab;.
275d8 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
275d9 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
275da 54 61 62 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62  Tab, db->aDb[iDb
275db 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 61 73 73 65  ].zName);.  asse
275dc 72 74 28 70 54 61 62 29 3b 0a 20 20 69 66 28 20  rt(pTab);.  if( 
275dd 70 54 61 62 2d 3e 70 56 74 61 62 20 29 7b 0a 20  pTab->pVtab ){. 
275de 20 20 20 69 6e 74 20 28 2a 78 44 65 73 74 72 6f     int (*xDestro
275df 79 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  y)(sqlite3_vtab 
275e0 2a 70 56 54 61 62 29 20 3d 20 70 54 61 62 2d 3e  *pVTab) = pTab->
275e1 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78  pMod->pModule->x
275e2 44 65 73 74 72 6f 79 3b 0a 20 20 20 20 72 63 20  Destroy;.    rc 
275e3 3d 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  = sqlite3SafetyO
275e4 66 66 28 64 62 29 3b 0a 20 20 20 20 61 73 73 65  ff(db);.    asse
275e5 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
275e6 4b 20 29 3b 0a 20 20 20 20 69 66 28 20 78 44 65  K );.    if( xDe
275e7 73 74 72 6f 79 20 29 7b 0a 20 20 20 20 20 20 72  stroy ){.      r
275e8 63 20 3d 20 78 44 65 73 74 72 6f 79 28 70 54 61  c = xDestroy(pTa
275e9 62 2d 3e 70 56 74 61 62 29 3b 0a 20 20 20 20 7d  b->pVtab);.    }
275ea 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74  .    (void)sqlit
275eb 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a  e3SafetyOn(db);.
275ec 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
275ed 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69  TE_OK ){.      i
275ee 6e 74 20 69 3b 0a 20 20 20 20 20 20 66 6f 72 28  nt i;.      for(
275ef 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 56 54 72 61  i=0; i<db->nVTra
275f0 6e 73 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ns; i++){.      
275f1 20 20 69 66 28 20 64 62 2d 3e 61 56 54 72 61 6e    if( db->aVTran
275f2 73 5b 69 5d 3d 3d 70 54 61 62 2d 3e 70 56 74 61  s[i]==pTab->pVta
275f3 62 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64  b ){.          d
275f4 62 2d 3e 61 56 54 72 61 6e 73 5b 69 5d 20 3d 20  b->aVTrans[i] = 
275f5 64 62 2d 3e 61 56 54 72 61 6e 73 5b 2d 2d 64 62  db->aVTrans[--db
275f6 2d 3e 6e 56 54 72 61 6e 73 5d 3b 0a 20 20 20 20  ->nVTrans];.    
275f7 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
275f8 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
275f9 20 20 20 20 20 70 54 61 62 2d 3e 70 56 74 61 62       pTab->pVtab
275fa 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
275fb 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
275fc 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
275fd 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 20 65 69 74  tion invokes eit
275fe 68 65 72 20 74 68 65 20 78 52 6f 6c 6c 62 61 63  her the xRollbac
275ff 6b 20 6f 72 20 78 43 6f 6d 6d 69 74 20 6d 65 74  k or xCommit met
27600 68 6f 64 0a 2a 2a 20 6f 66 20 65 61 63 68 20 6f  hod.** of each o
27601 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  f the virtual ta
27602 62 6c 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69  bles in the sqli
27603 74 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61  te3.aVTrans arra
27604 79 2e 20 54 68 65 20 6d 65 74 68 6f 64 0a 2a 2a  y. The method.**
27605 20 63 61 6c 6c 65 64 20 69 73 20 69 64 65 6e 74   called is ident
27606 69 66 69 65 64 20 62 79 20 74 68 65 20 73 65 63  ified by the sec
27607 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 22 6f  ond argument, "o
27608 66 66 73 65 74 22 2c 20 77 68 69 63 68 20 69 73  ffset", which is
27609 0a 2a 2a 20 74 68 65 20 6f 66 66 73 65 74 20 6f  .** the offset o
2760a 66 20 74 68 65 20 6d 65 74 68 6f 64 20 74 6f 20  f the method to 
2760b 63 61 6c 6c 20 69 6e 20 74 68 65 20 73 71 6c 69  call in the sqli
2760c 74 65 33 5f 6d 6f 64 75 6c 65 20 73 74 72 75 63  te3_module struc
2760d 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ture..**.** The 
2760e 61 72 72 61 79 20 69 73 20 63 6c 65 61 72 65 64  array is cleared
2760f 20 61 66 74 65 72 20 69 6e 76 6f 6b 69 6e 67 20   after invoking 
27610 74 68 65 20 63 61 6c 6c 62 61 63 6b 73 2e 20 0a  the callbacks. .
27611 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
27612 61 6c 6c 46 69 6e 61 6c 69 73 65 72 28 73 71 6c  allFinaliser(sql
27613 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f 66  ite3 *db, int of
27614 66 73 65 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  fset){.  int i;.
27615 20 20 69 66 28 20 64 62 2d 3e 61 56 54 72 61 6e    if( db->aVTran
27616 73 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  s ){.    for(i=0
27617 3b 20 69 3c 64 62 2d 3e 6e 56 54 72 61 6e 73 20  ; i<db->nVTrans 
27618 26 26 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 69  && db->aVTrans[i
27619 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  ]; i++){.      s
2761a 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
2761b 61 62 20 3d 20 64 62 2d 3e 61 56 54 72 61 6e 73  ab = db->aVTrans
2761c 5b 69 5d 3b 0a 20 20 20 20 20 20 69 6e 74 20 28  [i];.      int (
2761d 2a 78 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  *x)(sqlite3_vtab
2761e 20 2a 29 3b 0a 20 20 20 20 20 20 78 20 3d 20 2a   *);.      x = *
2761f 28 69 6e 74 20 28 2a 2a 29 28 73 71 6c 69 74 65  (int (**)(sqlite
27620 33 5f 76 74 61 62 20 2a 29 29 28 28 63 68 61 72  3_vtab *))((char
27621 20 2a 29 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c   *)pVtab->pModul
27622 65 20 2b 20 6f 66 66 73 65 74 29 3b 0a 20 20 20  e + offset);.   
27623 20 20 20 69 66 28 20 78 20 29 20 78 28 70 56 74     if( x ) x(pVt
27624 61 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ab);.      sqlit
27625 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 64 62 2c  e3VtabUnlock(db,
27626 20 70 56 74 61 62 29 3b 0a 20 20 20 20 7d 0a 20   pVtab);.    }. 
27627 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
27628 28 64 62 2c 20 64 62 2d 3e 61 56 54 72 61 6e 73  (db, db->aVTrans
27629 29 3b 0a 20 20 20 20 64 62 2d 3e 6e 56 54 72 61  );.    db->nVTra
2762a 6e 73 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e  ns = 0;.    db->
2762b 61 56 54 72 61 6e 73 20 3d 20 30 3b 0a 20 20 7d  aVTrans = 0;.  }
2762c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65  .}../*.** Invoke
2762d 20 74 68 65 20 78 53 79 6e 63 20 6d 65 74 68 6f   the xSync metho
2762e 64 20 6f 66 20 61 6c 6c 20 76 69 72 74 75 61 6c  d of all virtual
2762f 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 73   tables in the s
27630 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 0a 2a  qlite3.aVTrans.*
27631 2a 20 61 72 72 61 79 2e 20 52 65 74 75 72 6e 20  * array. Return 
27632 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 66  the error code f
27633 6f 72 20 74 68 65 20 66 69 72 73 74 20 65 72 72  or the first err
27634 6f 72 20 74 68 61 74 20 6f 63 63 75 72 73 2c 20  or that occurs, 
27635 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20  or.** SQLITE_OK 
27636 69 66 20 61 6c 6c 20 78 53 79 6e 63 20 6f 70 65  if all xSync ope
27637 72 61 74 69 6f 6e 73 20 61 72 65 20 73 75 63 63  rations are succ
27638 65 73 73 66 75 6c 2e 0a 2a 2a 0a 2a 2a 20 53 65  essful..**.** Se
27639 74 20 2a 70 7a 45 72 72 6d 73 67 20 74 6f 20 70  t *pzErrmsg to p
2763a 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72  oint to a buffer
2763b 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20   that should be 
2763c 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67 20 0a  released using .
2763d 2a 2a 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  ** sqlite3DbFree
2763e 28 29 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6e  () containing an
2763f 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2c 20   error message, 
27640 69 66 20 6f 6e 65 20 69 73 20 61 76 61 69 6c 61  if one is availa
27641 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ble..*/.SQLITE_P
27642 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
27643 65 33 56 74 61 62 53 79 6e 63 28 73 71 6c 69 74  e3VtabSync(sqlit
27644 65 33 20 2a 64 62 2c 20 63 68 61 72 20 2a 2a 70  e3 *db, char **p
27645 7a 45 72 72 6d 73 67 29 7b 0a 20 20 69 6e 74 20  zErrmsg){.  int 
27646 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  i;.  int rc = SQ
27647 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72  LITE_OK;.  int r
27648 63 73 61 66 65 74 79 3b 0a 20 20 73 71 6c 69 74  csafety;.  sqlit
27649 65 33 5f 76 74 61 62 20 2a 2a 61 56 54 72 61 6e  e3_vtab **aVTran
2764a 73 20 3d 20 64 62 2d 3e 61 56 54 72 61 6e 73 3b  s = db->aVTrans;
2764b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
2764c 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20  SafetyOff(db);. 
2764d 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 3d 20 30   db->aVTrans = 0
2764e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d  ;.  for(i=0; rc=
2764f 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c  =SQLITE_OK && i<
27650 64 62 2d 3e 6e 56 54 72 61 6e 73 20 26 26 20 61  db->nVTrans && a
27651 56 54 72 61 6e 73 5b 69 5d 3b 20 69 2b 2b 29 7b  VTrans[i]; i++){
27652 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61  .    sqlite3_vta
27653 62 20 2a 70 56 74 61 62 20 3d 20 61 56 54 72 61  b *pVtab = aVTra
27654 6e 73 5b 69 5d 3b 0a 20 20 20 20 69 6e 74 20 28  ns[i];.    int (
27655 2a 78 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  *x)(sqlite3_vtab
27656 20 2a 29 3b 0a 20 20 20 20 78 20 3d 20 70 56 74   *);.    x = pVt
27657 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 53 79  ab->pModule->xSy
27658 6e 63 3b 0a 20 20 20 20 69 66 28 20 78 20 29 7b  nc;.    if( x ){
27659 0a 20 20 20 20 20 20 72 63 20 3d 20 78 28 70 56  .      rc = x(pV
2765a 74 61 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  tab);.      sqli
2765b 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 2a 70  te3DbFree(db, *p
2765c 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 20  zErrmsg);.      
2765d 2a 70 7a 45 72 72 6d 73 67 20 3d 20 70 56 74 61  *pzErrmsg = pVta
2765e 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20  b->zErrMsg;.    
2765f 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67    pVtab->zErrMsg
27660 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
27661 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 3d 20    db->aVTrans = 
27662 61 56 54 72 61 6e 73 3b 0a 20 20 72 63 73 61 66  aVTrans;.  rcsaf
27663 65 74 79 20 3d 20 73 71 6c 69 74 65 33 53 61 66  ety = sqlite3Saf
27664 65 74 79 4f 6e 28 64 62 29 3b 0a 0a 20 20 69 66  etyOn(db);..  if
27665 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
27666 29 7b 0a 20 20 20 20 72 63 20 3d 20 72 63 73 61  ){.    rc = rcsa
27667 66 65 74 79 3b 0a 20 20 7d 0a 20 20 72 65 74 75  fety;.  }.  retu
27668 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
27669 49 6e 76 6f 6b 65 20 74 68 65 20 78 52 6f 6c 6c  Invoke the xRoll
2766a 62 61 63 6b 20 6d 65 74 68 6f 64 20 6f 66 20 61  back method of a
2766b 6c 6c 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ll virtual table
2766c 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c  s in the .** sql
2766d 69 74 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72  ite3.aVTrans arr
2766e 61 79 2e 20 54 68 65 6e 20 63 6c 65 61 72 20 74  ay. Then clear t
2766f 68 65 20 61 72 72 61 79 20 69 74 73 65 6c 66 2e  he array itself.
27670 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
27671 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74  TE int sqlite3Vt
27672 61 62 52 6f 6c 6c 62 61 63 6b 28 73 71 6c 69 74  abRollback(sqlit
27673 65 33 20 2a 64 62 29 7b 0a 20 20 63 61 6c 6c 46  e3 *db){.  callF
27674 69 6e 61 6c 69 73 65 72 28 64 62 2c 20 6f 66 66  inaliser(db, off
27675 73 65 74 6f 66 28 73 71 6c 69 74 65 33 5f 6d 6f  setof(sqlite3_mo
27676 64 75 6c 65 2c 78 52 6f 6c 6c 62 61 63 6b 29 29  dule,xRollback))
27677 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
27678 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  E_OK;.}../*.** I
27679 6e 76 6f 6b 65 20 74 68 65 20 78 43 6f 6d 6d 69  nvoke the xCommi
2767a 74 20 6d 65 74 68 6f 64 20 6f 66 20 61 6c 6c 20  t method of all 
2767b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69  virtual tables i
2767c 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65  n the .** sqlite
2767d 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61 79 2e  3.aVTrans array.
2767e 20 54 68 65 6e 20 63 6c 65 61 72 20 74 68 65 20   Then clear the 
2767f 61 72 72 61 79 20 69 74 73 65 6c 66 2e 0a 2a 2f  array itself..*/
27680 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
27681 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43  int sqlite3VtabC
27682 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64  ommit(sqlite3 *d
27683 62 29 7b 0a 20 20 63 61 6c 6c 46 69 6e 61 6c 69  b){.  callFinali
27684 73 65 72 28 64 62 2c 20 6f 66 66 73 65 74 6f 66  ser(db, offsetof
27685 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2c  (sqlite3_module,
27686 78 43 6f 6d 6d 69 74 29 29 3b 0a 20 20 72 65 74  xCommit));.  ret
27687 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
27688 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76  ../*.** If the v
27689 69 72 74 75 61 6c 20 74 61 62 6c 65 20 70 56 74  irtual table pVt
2768a 61 62 20 73 75 70 70 6f 72 74 73 20 74 68 65 20  ab supports the 
2768b 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 74 65  transaction inte
2768c 72 66 61 63 65 0a 2a 2a 20 28 78 42 65 67 69 6e  rface.** (xBegin
2768d 2f 78 52 6f 6c 6c 62 61 63 6b 2f 78 43 6f 6d 6d  /xRollback/xComm
2768e 69 74 20 61 6e 64 20 6f 70 74 69 6f 6e 61 6c 6c  it and optionall
2768f 79 20 78 53 79 6e 63 29 20 61 6e 64 20 61 20 74  y xSync) and a t
27690 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a  ransaction is.**
27691 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 6f   not currently o
27692 70 65 6e 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20  pen, invoke the 
27693 78 42 65 67 69 6e 20 6d 65 74 68 6f 64 20 6e 6f  xBegin method no
27694 77 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  w..**.** If the 
27695 78 42 65 67 69 6e 20 63 61 6c 6c 20 69 73 20 73  xBegin call is s
27696 75 63 63 65 73 73 66 75 6c 2c 20 70 6c 61 63 65  uccessful, place
27697 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61   the sqlite3_vta
27698 62 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 69 6e 20  b pointer.** in 
27699 74 68 65 20 73 71 6c 69 74 65 33 2e 61 56 54 72  the sqlite3.aVTr
2769a 61 6e 73 20 61 72 72 61 79 2e 0a 2a 2f 0a 53 51  ans array..*/.SQ
2769b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2769c 20 73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69   sqlite3VtabBegi
2769d 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 73  n(sqlite3 *db, s
2769e 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
2769f 61 62 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ab){.  int rc = 
276a0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 6f 6e  SQLITE_OK;.  con
276a1 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
276a2 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 2f  e *pModule;..  /
276a3 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20  * Special case: 
276a4 49 66 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 69  If db->aVTrans i
276a5 73 20 4e 55 4c 4c 20 61 6e 64 20 64 62 2d 3e 6e  s NULL and db->n
276a6 56 54 72 61 6e 73 20 69 73 20 67 72 65 61 74 65  VTrans is greate
276a7 72 0a 20 20 2a 2a 20 74 68 61 6e 20 7a 65 72 6f  r.  ** than zero
276a8 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63  , then this func
276a9 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 63 61  tion is being ca
276aa 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e  lled from within
276ab 20 61 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20   a.  ** virtual 
276ac 6d 6f 64 75 6c 65 20 78 53 79 6e 63 28 29 20 63  module xSync() c
276ad 61 6c 6c 62 61 63 6b 2e 20 49 74 20 69 73 20 69  allback. It is i
276ae 6c 6c 65 67 61 6c 20 74 6f 20 77 72 69 74 65 20  llegal to write 
276af 74 6f 20 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c  to .  ** virtual
276b0 20 6d 6f 64 75 6c 65 20 74 61 62 6c 65 73 20 69   module tables i
276b1 6e 20 74 68 69 73 20 63 61 73 65 2c 20 73 6f 20  n this case, so 
276b2 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f  return SQLITE_LO
276b3 43 4b 45 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  CKED..  */.  if(
276b4 20 30 3d 3d 64 62 2d 3e 61 56 54 72 61 6e 73 20   0==db->aVTrans 
276b5 26 26 20 64 62 2d 3e 6e 56 54 72 61 6e 73 3e 30  && db->nVTrans>0
276b6 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
276b7 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20  QLITE_LOCKED;.  
276b8 7d 0a 20 20 69 66 28 20 21 70 56 74 61 62 20 29  }.  if( !pVtab )
276b9 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
276ba 49 54 45 5f 4f 4b 3b 0a 20 20 7d 20 0a 20 20 70  ITE_OK;.  } .  p
276bb 4d 6f 64 75 6c 65 20 3d 20 70 56 74 61 62 2d 3e  Module = pVtab->
276bc 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 69 66 28 20  pModule;..  if( 
276bd 70 4d 6f 64 75 6c 65 2d 3e 78 42 65 67 69 6e 20  pModule->xBegin 
276be 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 0a 0a  ){.    int i;...
276bf 20 20 20 20 2f 2a 20 49 66 20 70 56 74 61 62 20      /* If pVtab 
276c0 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68  is already in th
276c1 65 20 61 56 54 72 61 6e 73 20 61 72 72 61 79 2c  e aVTrans array,
276c2 20 72 65 74 75 72 6e 20 65 61 72 6c 79 20 2a 2f   return early */
276c3 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 28 69  .    for(i=0; (i
276c4 3c 64 62 2d 3e 6e 56 54 72 61 6e 73 29 20 26 26  <db->nVTrans) &&
276c5 20 30 21 3d 64 62 2d 3e 61 56 54 72 61 6e 73 5b   0!=db->aVTrans[
276c6 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  i]; i++){.      
276c7 69 66 28 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b  if( db->aVTrans[
276c8 69 5d 3d 3d 70 56 74 61 62 20 29 7b 0a 20 20 20  i]==pVtab ){.   
276c9 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
276ca 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20  TE_OK;.      }. 
276cb 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 6e 76     }..    /* Inv
276cc 6f 6b 65 20 74 68 65 20 78 42 65 67 69 6e 20 6d  oke the xBegin m
276cd 65 74 68 6f 64 20 2a 2f 0a 20 20 20 20 72 63 20  ethod */.    rc 
276ce 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 42 65 67 69  = pModule->xBegi
276cf 6e 28 70 56 74 61 62 29 3b 0a 20 20 20 20 69 66  n(pVtab);.    if
276d0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
276d1 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 64  ){.      rc = ad
276d2 64 54 6f 56 54 72 61 6e 73 28 64 62 2c 20 70 56  dToVTrans(db, pV
276d3 74 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  tab);.    }.  }.
276d4 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
276d5 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  /*.** The first 
276d6 70 61 72 61 6d 65 74 65 72 20 28 70 44 65 66 29  parameter (pDef)
276d7 20 69 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 69   is a function i
276d8 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20  mplementation.  
276d9 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61  The.** second pa
276da 72 61 6d 65 74 65 72 20 28 70 45 78 70 72 29 20  rameter (pExpr) 
276db 69 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67  is the first arg
276dc 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75  ument to this fu
276dd 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 49 66 20 70 45  nction..** If pE
276de 78 70 72 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20  xpr is a column 
276df 69 6e 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  in a virtual tab
276e0 6c 65 2c 20 74 68 65 6e 20 6c 65 74 20 74 68 65  le, then let the
276e1 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c   virtual.** tabl
276e2 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
276e3 20 68 61 76 65 20 61 6e 20 6f 70 70 6f 72 74 75   have an opportu
276e4 6e 69 74 79 20 74 6f 20 6f 76 65 72 6c 6f 61 64  nity to overload
276e5 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a   the function..*
276e6 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
276e7 65 20 69 73 20 75 73 65 64 20 74 6f 20 61 6c 6c  e is used to all
276e8 6f 77 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ow virtual table
276e9 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
276ea 20 74 6f 0a 2a 2a 20 6f 76 65 72 6c 6f 61 64 20   to.** overload 
276eb 4d 41 54 43 48 2c 20 4c 49 4b 45 2c 20 47 4c 4f  MATCH, LIKE, GLO
276ec 42 2c 20 61 6e 64 20 52 45 47 45 58 50 20 6f 70  B, and REGEXP op
276ed 65 72 61 74 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 52  erators..**.** R
276ee 65 74 75 72 6e 20 65 69 74 68 65 72 20 74 68 65  eturn either the
276ef 20 70 44 65 66 20 61 72 67 75 6d 65 6e 74 20 28   pDef argument (
276f0 69 6e 64 69 63 61 74 69 6e 67 20 6e 6f 20 63 68  indicating no ch
276f1 61 6e 67 65 29 20 6f 72 20 61 20 0a 2a 2a 20 6e  ange) or a .** n
276f2 65 77 20 46 75 6e 63 44 65 66 20 73 74 72 75 63  ew FuncDef struc
276f3 74 75 72 65 20 74 68 61 74 20 69 73 20 6d 61 72  ture that is mar
276f4 6b 65 64 20 61 73 20 65 70 68 65 6d 65 72 61 6c  ked as ephemeral
276f5 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 53 51   using the.** SQ
276f6 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d 20  LITE_FUNC_EPHEM 
276f7 66 6c 61 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  flag..*/.SQLITE_
276f8 50 52 49 56 41 54 45 20 46 75 6e 63 44 65 66 20  PRIVATE FuncDef 
276f9 2a 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72  *sqlite3VtabOver
276fa 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 0a 20 20  loadFunction(.  
276fb 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
276fc 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e  /* Database conn
276fd 65 63 74 69 6f 6e 20 66 6f 72 20 72 65 70 6f 72  ection for repor
276fe 74 69 6e 67 20 6d 61 6c 6c 6f 63 20 70 72 6f 62  ting malloc prob
276ff 6c 65 6d 73 20 2a 2f 0a 20 20 46 75 6e 63 44 65  lems */.  FuncDe
27700 66 20 2a 70 44 65 66 2c 20 20 2f 2a 20 46 75 6e  f *pDef,  /* Fun
27701 63 74 69 6f 6e 20 74 6f 20 70 6f 73 73 69 62 6c  ction to possibl
27702 79 20 6f 76 65 72 6c 6f 61 64 20 2a 2f 0a 20 20  y overload */.  
27703 69 6e 74 20 6e 41 72 67 2c 20 20 20 20 20 20 20  int nArg,       
27704 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67  /* Number of arg
27705 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75  uments to the fu
27706 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72  nction */.  Expr
27707 20 2a 70 45 78 70 72 20 20 20 20 20 2f 2a 20 46   *pExpr     /* F
27708 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
27709 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f   the function */
2770a 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  .){.  Table *pTa
2770b 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  b;.  sqlite3_vta
2770c 62 20 2a 70 56 74 61 62 3b 0a 20 20 73 71 6c 69  b *pVtab;.  sqli
2770d 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
2770e 3b 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63  ;.  void (*xFunc
2770f 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
27710 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
27711 61 6c 75 65 2a 2a 29 3b 0a 20 20 76 6f 69 64 20  alue**);.  void 
27712 2a 70 41 72 67 3b 0a 20 20 46 75 6e 63 44 65 66  *pArg;.  FuncDef
27713 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 72 63   *pNew;.  int rc
27714 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 4c   = 0;.  char *zL
27715 6f 77 65 72 4e 61 6d 65 3b 0a 20 20 75 6e 73 69  owerName;.  unsi
27716 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 0a 0a  gned char *z;...
27717 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65    /* Check to se
27718 65 20 74 68 65 20 6c 65 66 74 20 6f 70 65 72 61  e the left opera
27719 6e 64 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 69  nd is a column i
2771a 6e 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  n a virtual tabl
2771b 65 20 2a 2f 0a 20 20 69 66 28 20 70 45 78 70 72  e */.  if( pExpr
2771c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 70 44 65  ==0 ) return pDe
2771d 66 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  f;.  if( pExpr->
2771e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20  op!=TK_COLUMN ) 
2771f 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 20 70  return pDef;.  p
27720 54 61 62 20 3d 20 70 45 78 70 72 2d 3e 70 54 61  Tab = pExpr->pTa
27721 62 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30  b;.  if( pTab==0
27722 20 29 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a   ) return pDef;.
27723 20 20 69 66 28 20 28 70 54 61 62 2d 3e 74 61 62    if( (pTab->tab
27724 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74 75  Flags & TF_Virtu
27725 61 6c 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  al)==0 ) return 
27726 70 44 65 66 3b 0a 20 20 70 56 74 61 62 20 3d 20  pDef;.  pVtab = 
27727 70 54 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 61  pTab->pVtab;.  a
27728 73 73 65 72 74 28 20 70 56 74 61 62 21 3d 30 20  ssert( pVtab!=0 
27729 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 56 74  );.  assert( pVt
2772a 61 62 2d 3e 70 4d 6f 64 75 6c 65 21 3d 30 20 29  ab->pModule!=0 )
2772b 3b 0a 20 20 70 4d 6f 64 20 3d 20 28 73 71 6c 69  ;.  pMod = (sqli
2772c 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 70 56 74  te3_module *)pVt
2772d 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 69  ab->pModule;.  i
2772e 66 28 20 70 4d 6f 64 2d 3e 78 46 69 6e 64 46 75  f( pMod->xFindFu
2772f 6e 63 74 69 6f 6e 3d 3d 30 20 29 20 72 65 74 75  nction==0 ) retu
27730 72 6e 20 70 44 65 66 3b 0a 20 0a 20 20 2f 2a 20  rn pDef;. .  /* 
27731 43 61 6c 6c 20 74 68 65 20 78 46 69 6e 64 46 75  Call the xFindFu
27732 6e 63 74 69 6f 6e 20 6d 65 74 68 6f 64 20 6f 6e  nction method on
27733 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
27734 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  le implementatio
27735 6e 0a 20 20 2a 2a 20 74 6f 20 73 65 65 20 69 66  n.  ** to see if
27736 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
27737 69 6f 6e 20 77 61 6e 74 73 20 74 6f 20 6f 76 65  ion wants to ove
27738 72 6c 6f 61 64 20 74 68 69 73 20 66 75 6e 63 74  rload this funct
27739 69 6f 6e 20 0a 20 20 2a 2f 0a 20 20 7a 4c 6f 77  ion .  */.  zLow
2773a 65 72 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  erName = sqlite3
2773b 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 44 65  DbStrDup(db, pDe
2773c 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28  f->zName);.  if(
2773d 20 7a 4c 6f 77 65 72 4e 61 6d 65 20 29 7b 0a 20   zLowerName ){. 
2773e 20 20 20 66 6f 72 28 7a 3d 28 75 6e 73 69 67 6e     for(z=(unsign
2773f 65 64 20 63 68 61 72 2a 29 7a 4c 6f 77 65 72 4e  ed char*)zLowerN
27740 61 6d 65 3b 20 2a 7a 3b 20 7a 2b 2b 29 7b 0a 20  ame; *z; z++){. 
27741 20 20 20 20 20 2a 7a 20 3d 20 73 71 6c 69 74 65       *z = sqlite
27742 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 7a  3UpperToLower[*z
27743 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20  ];.    }.    rc 
27744 3d 20 70 4d 6f 64 2d 3e 78 46 69 6e 64 46 75 6e  = pMod->xFindFun
27745 63 74 69 6f 6e 28 70 56 74 61 62 2c 20 6e 41 72  ction(pVtab, nAr
27746 67 2c 20 7a 4c 6f 77 65 72 4e 61 6d 65 2c 20 26  g, zLowerName, &
27747 78 46 75 6e 63 2c 20 26 70 41 72 67 29 3b 0a 20  xFunc, &pArg);. 
27748 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
27749 28 64 62 2c 20 7a 4c 6f 77 65 72 4e 61 6d 65 29  (db, zLowerName)
2774a 3b 0a 20 20 20 20 69 66 28 20 70 56 74 61 62 2d  ;.    if( pVtab-
2774b 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20  >zErrMsg ){.    
2774c 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
2774d 62 2c 20 72 63 2c 20 22 25 73 22 2c 20 70 56 74  b, rc, "%s", pVt
2774e 61 62 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20  ab->zErrMsg);.  
2774f 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
27750 65 28 64 62 2c 20 70 56 74 61 62 2d 3e 7a 45 72  e(db, pVtab->zEr
27751 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 70 56 74  rMsg);.      pVt
27752 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b  ab->zErrMsg = 0;
27753 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
27754 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65   rc==0 ){.    re
27755 74 75 72 6e 20 70 44 65 66 3b 0a 20 20 7d 0a 0a  turn pDef;.  }..
27756 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 6e 65    /* Create a ne
27757 77 20 65 70 68 65 6d 65 72 61 6c 20 66 75 6e 63  w ephemeral func
27758 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20  tion definition 
27759 66 6f 72 20 74 68 65 20 6f 76 65 72 6c 6f 61 64  for the overload
2775a 65 64 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e  ed.  ** function
2775b 20 2a 2f 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c   */.  pNew = sql
2775c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
2775d 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65  (db, sizeof(*pNe
2775e 77 29 20 2b 20 73 74 72 6c 65 6e 28 70 44 65 66  w) + strlen(pDef
2775f 2d 3e 7a 4e 61 6d 65 29 20 29 3b 0a 20 20 69 66  ->zName) );.  if
27760 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20  ( pNew==0 ){.   
27761 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 20   return pDef;.  
27762 7d 0a 20 20 2a 70 4e 65 77 20 3d 20 2a 70 44 65  }.  *pNew = *pDe
27763 66 3b 0a 20 20 70 4e 65 77 2d 3e 7a 4e 61 6d 65  f;.  pNew->zName
27764 20 3d 20 28 63 68 61 72 20 2a 29 26 70 4e 65 77   = (char *)&pNew
27765 5b 31 5d 3b 0a 20 20 6d 65 6d 63 70 79 28 70 4e  [1];.  memcpy(pN
27766 65 77 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66 2d  ew->zName, pDef-
27767 3e 7a 4e 61 6d 65 2c 20 73 74 72 6c 65 6e 28 70  >zName, strlen(p
27768 44 65 66 2d 3e 7a 4e 61 6d 65 29 2b 31 29 3b 0a  Def->zName)+1);.
27769 20 20 70 4e 65 77 2d 3e 78 46 75 6e 63 20 3d 20    pNew->xFunc = 
2776a 78 46 75 6e 63 3b 0a 20 20 70 4e 65 77 2d 3e 70  xFunc;.  pNew->p
2776b 55 73 65 72 44 61 74 61 20 3d 20 70 41 72 67 3b  UserData = pArg;
2776c 0a 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c  .  pNew->flags |
2776d 3d 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50  = SQLITE_FUNC_EP
2776e 48 45 4d 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e  HEM;.  return pN
2776f 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  ew;.}../*.** Mak
27770 65 20 73 75 72 65 20 76 69 72 74 75 61 6c 20 74  e sure virtual t
27771 61 62 6c 65 20 70 54 61 62 20 69 73 20 63 6f 6e  able pTab is con
27772 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 70 50  tained in the pP
27773 61 72 73 65 2d 3e 61 70 56 69 72 74 75 61 6c 4c  arse->apVirtualL
27774 6f 63 6b 5b 5d 0a 2a 2a 20 61 72 72 61 79 20 73  ock[].** array s
27775 6f 20 74 68 61 74 20 61 6e 20 4f 50 5f 56 42 65  o that an OP_VBe
27776 67 69 6e 20 77 69 6c 6c 20 67 65 74 20 67 65 6e  gin will get gen
27777 65 72 61 74 65 64 20 66 6f 72 20 69 74 2e 20 20  erated for it.  
27778 41 64 64 20 70 54 61 62 20 74 6f 20 74 68 65 0a  Add pTab to the.
27779 2a 2a 20 61 72 72 61 79 20 69 66 20 69 74 20 69  ** array if it i
2777a 73 20 6d 69 73 73 69 6e 67 2e 20 20 49 66 20 70  s missing.  If p
2777b 54 61 62 20 69 73 20 61 6c 72 65 61 64 79 20 69  Tab is already i
2777c 6e 20 74 68 65 20 61 72 72 61 79 2c 20 74 68 69  n the array, thi
2777d 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20  s routine.** is 
2777e 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49  a no-op..*/.SQLI
2777f 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
27780 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b 65 57  sqlite3VtabMakeW
27781 72 69 74 61 62 6c 65 28 50 61 72 73 65 20 2a 70  ritable(Parse *p
27782 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54  Parse, Table *pT
27783 61 62 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b  ab){.  int i, n;
27784 0a 20 20 61 73 73 65 72 74 28 20 49 73 56 69 72  .  assert( IsVir
27785 74 75 61 6c 28 70 54 61 62 29 20 29 3b 0a 20 20  tual(pTab) );.  
27786 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73  for(i=0; i<pPars
27787 65 2d 3e 6e 56 74 61 62 4c 6f 63 6b 3b 20 69 2b  e->nVtabLock; i+
27788 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 54 61 62  +){.    if( pTab
27789 3d 3d 70 50 61 72 73 65 2d 3e 61 70 56 74 61 62  ==pParse->apVtab
2778a 4c 6f 63 6b 5b 69 5d 20 29 20 72 65 74 75 72 6e  Lock[i] ) return
2778b 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20 28 70 50 61  ;.  }.  n = (pPa
2778c 72 73 65 2d 3e 6e 56 74 61 62 4c 6f 63 6b 2b 31  rse->nVtabLock+1
2778d 29 2a 73 69 7a 65 6f 66 28 70 50 61 72 73 65 2d  )*sizeof(pParse-
2778e 3e 61 70 56 74 61 62 4c 6f 63 6b 5b 30 5d 29 3b  >apVtabLock[0]);
2778f 0a 20 20 70 50 61 72 73 65 2d 3e 61 70 56 74 61  .  pParse->apVta
27790 62 4c 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f  bLock = sqlite3_
27791 72 65 61 6c 6c 6f 63 28 70 50 61 72 73 65 2d 3e  realloc(pParse->
27792 61 70 56 74 61 62 4c 6f 63 6b 2c 20 6e 29 3b 0a  apVtabLock, n);.
27793 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 61 70    if( pParse->ap
27794 56 74 61 62 4c 6f 63 6b 20 29 7b 0a 20 20 20 20  VtabLock ){.    
27795 70 50 61 72 73 65 2d 3e 61 70 56 74 61 62 4c 6f  pParse->apVtabLo
27796 63 6b 5b 70 50 61 72 73 65 2d 3e 6e 56 74 61 62  ck[pParse->nVtab
27797 4c 6f 63 6b 2b 2b 5d 20 3d 20 70 54 61 62 3b 0a  Lock++] = pTab;.
27798 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61    }else{.    pPa
27799 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rse->db->mallocF
2779a 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d  ailed = 1;.  }.}
2779b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
2779c 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
2779d 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  ABLE */../******
2779e 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
2779f 76 74 61 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  vtab.c *********
277a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
277a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
277a2 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
277a3 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
277a4 69 6c 65 20 77 68 65 72 65 2e 63 20 2a 2a 2a 2a  ile where.c ****
277a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
277a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
277a7 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
277a8 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
277a9 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
277aa 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
277ab 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
277ac 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
277ad 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
277ae 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
277af 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
277b0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
277b1 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
277b2 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
277b3 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
277b4 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
277b5 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
277b6 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
277b7 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
277b8 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
277b9 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
277ba 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
277bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
277bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
277bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
277be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
277bf 2a 2a 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 63  ** This module c
277c0 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 74  ontains C code t
277c1 68 61 74 20 67 65 6e 65 72 61 74 65 73 20 56 44  hat generates VD
277c2 42 45 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20  BE code used to 
277c3 70 72 6f 63 65 73 73 0a 2a 2a 20 74 68 65 20 57  process.** the W
277c4 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 53  HERE clause of S
277c5 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20  QL statements.  
277c6 54 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20 72  This module is r
277c7 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 0a 2a  esponsible for.*
277c8 2a 20 67 65 6e 65 72 61 74 69 6e 67 20 74 68 65  * generating the
277c9 20 63 6f 64 65 20 74 68 61 74 20 6c 6f 6f 70 73   code that loops
277ca 20 74 68 72 6f 75 67 68 20 61 20 74 61 62 6c 65   through a table
277cb 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 70 70   looking for app
277cc 6c 69 63 61 62 6c 65 0a 2a 2a 20 72 6f 77 73 2e  licable.** rows.
277cd 20 20 49 6e 64 69 63 65 73 20 61 72 65 20 73 65    Indices are se
277ce 6c 65 63 74 65 64 20 61 6e 64 20 75 73 65 64 20  lected and used 
277cf 74 6f 20 73 70 65 65 64 20 74 68 65 20 73 65 61  to speed the sea
277d0 72 63 68 20 77 68 65 6e 20 64 6f 69 6e 67 0a 2a  rch when doing.*
277d1 2a 20 73 6f 20 69 73 20 61 70 70 6c 69 63 61 62  * so is applicab
277d2 6c 65 2e 20 20 42 65 63 61 75 73 65 20 74 68 69  le.  Because thi
277d3 73 20 6d 6f 64 75 6c 65 20 69 73 20 72 65 73 70  s module is resp
277d4 6f 6e 73 69 62 6c 65 20 66 6f 72 20 73 65 6c 65  onsible for sele
277d5 63 74 69 6e 67 0a 2a 2a 20 69 6e 64 69 63 65 73  cting.** indices
277d6 2c 20 79 6f 75 20 6d 69 67 68 74 20 61 6c 73 6f  , you might also
277d7 20 74 68 69 6e 6b 20 6f 66 20 74 68 69 73 20 6d   think of this m
277d8 6f 64 75 6c 65 20 61 73 20 74 68 65 20 22 71 75  odule as the "qu
277d9 65 72 79 20 6f 70 74 69 6d 69 7a 65 72 22 2e 0a  ery optimizer"..
277da 2a 2a 0a 2a 2a 20 24 49 64 3a 20 77 68 65 72 65  **.** $Id: where
277db 2e 63 2c 76 20 31 2e 33 32 38 20 32 30 30 38 2f  .c,v 1.328 2008/
277dc 31 31 2f 30 33 20 30 39 3a 30 36 3a 30 36 20 64  11/03 09:06:06 d
277dd 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
277de 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e  .*/../*.** The n
277df 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69 6e  umber of bits in
277e0 20 61 20 42 69 74 6d 61 73 6b 2e 20 20 22 42 4d   a Bitmask.  "BM
277e1 53 22 20 6d 65 61 6e 73 20 22 42 69 74 4d 61 73  S" means "BitMas
277e2 6b 20 53 69 7a 65 22 2e 0a 2a 2f 0a 23 64 65 66  k Size"..*/.#def
277e3 69 6e 65 20 42 4d 53 20 20 28 73 69 7a 65 6f 66  ine BMS  (sizeof
277e4 28 42 69 74 6d 61 73 6b 29 2a 38 29 0a 0a 2f 2a  (Bitmask)*8)../*
277e5 0a 2a 2a 20 54 72 61 63 65 20 6f 75 74 70 75 74  .** Trace output
277e6 20 6d 61 63 72 6f 73 0a 2a 2f 0a 23 69 66 20 64   macros.*/.#if d
277e7 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45  efined(SQLITE_TE
277e8 53 54 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  ST) || defined(S
277e9 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 53 51 4c  QLITE_DEBUG).SQL
277ea 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
277eb 73 71 6c 69 74 65 33 57 68 65 72 65 54 72 61 63  sqlite3WhereTrac
277ec 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 23 69  e = 0;.#endif.#i
277ed 66 20 30 0a 23 20 64 65 66 69 6e 65 20 57 48 45  f 0.# define WHE
277ee 52 45 54 52 41 43 45 28 58 29 20 20 69 66 28 73  RETRACE(X)  if(s
277ef 71 6c 69 74 65 33 57 68 65 72 65 54 72 61 63 65  qlite3WhereTrace
277f0 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
277f1 69 6e 74 66 20 58 0a 23 65 6c 73 65 0a 23 20 64  intf X.#else.# d
277f2 65 66 69 6e 65 20 57 48 45 52 45 54 52 41 43 45  efine WHERETRACE
277f3 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 46  (X).#endif../* F
277f4 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e 63 65  orward reference
277f5 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
277f6 63 74 20 57 68 65 72 65 43 6c 61 75 73 65 20 57  ct WhereClause W
277f7 68 65 72 65 43 6c 61 75 73 65 3b 0a 74 79 70 65  hereClause;.type
277f8 64 65 66 20 73 74 72 75 63 74 20 45 78 70 72 4d  def struct ExprM
277f9 61 73 6b 53 65 74 20 45 78 70 72 4d 61 73 6b 53  askSet ExprMaskS
277fa 65 74 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 71  et;../*.** The q
277fb 75 65 72 79 20 67 65 6e 65 72 61 74 6f 72 20 75  uery generator u
277fc 73 65 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20  ses an array of 
277fd 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 74 68 69  instances of thi
277fe 73 20 73 74 72 75 63 74 75 72 65 20 74 6f 0a 2a  s structure to.*
277ff 2a 20 68 65 6c 70 20 69 74 20 61 6e 61 6c 79 7a  * help it analyz
27800 65 20 74 68 65 20 73 75 62 65 78 70 72 65 73 73  e the subexpress
27801 69 6f 6e 73 20 6f 66 20 74 68 65 20 57 48 45 52  ions of the WHER
27802 45 20 63 6c 61 75 73 65 2e 20 20 45 61 63 68 20  E clause.  Each 
27803 57 48 45 52 45 0a 2a 2a 20 63 6c 61 75 73 65 20  WHERE.** clause 
27804 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 69 73  subexpression is
27805 20 73 65 70 61 72 61 74 65 64 20 66 72 6f 6d 20   separated from 
27806 74 68 65 20 6f 74 68 65 72 73 20 62 79 20 61 6e  the others by an
27807 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 0a 2a   AND operator..*
27808 2a 0a 2a 2a 20 41 6c 6c 20 57 68 65 72 65 54 65  *.** All WhereTe
27809 72 6d 73 20 61 72 65 20 63 6f 6c 6c 65 63 74 65  rms are collecte
2780a 64 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20  d into a single 
2780b 57 68 65 72 65 43 6c 61 75 73 65 20 73 74 72 75  WhereClause stru
2780c 63 74 75 72 65 2e 20 20 0a 2a 2a 20 54 68 65 20  cture.  .** The 
2780d 66 6f 6c 6c 6f 77 69 6e 67 20 69 64 65 6e 74 69  following identi
2780e 74 79 20 68 6f 6c 64 73 3a 0a 2a 2a 0a 2a 2a 20  ty holds:.**.** 
2780f 20 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d         WhereTerm
27810 2e 70 57 43 2d 3e 61 5b 57 68 65 72 65 54 65 72  .pWC->a[WhereTer
27811 6d 2e 69 64 78 5d 20 3d 3d 20 57 68 65 72 65 54  m.idx] == WhereT
27812 65 72 6d 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61  erm.**.** When a
27813 20 74 65 72 6d 20 69 73 20 6f 66 20 74 68 65 20   term is of the 
27814 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  form:.**.**     
27815 20 20 20 20 20 20 20 20 20 58 20 3c 6f 70 3e 20           X <op> 
27816 3c 65 78 70 72 3e 0a 2a 2a 0a 2a 2a 20 77 68 65  <expr>.**.** whe
27817 72 65 20 58 20 69 73 20 61 20 63 6f 6c 75 6d 6e  re X is a column
27818 20 6e 61 6d 65 20 61 6e 64 20 3c 6f 70 3e 20 69   name and <op> i
27819 73 20 6f 6e 65 20 6f 66 20 63 65 72 74 61 69 6e  s one of certain
2781a 20 6f 70 65 72 61 74 6f 72 73 2c 0a 2a 2a 20 74   operators,.** t
2781b 68 65 6e 20 57 68 65 72 65 54 65 72 6d 2e 6c 65  hen WhereTerm.le
2781c 66 74 43 75 72 73 6f 72 20 61 6e 64 20 57 68 65  ftCursor and Whe
2781d 72 65 54 65 72 6d 2e 6c 65 66 74 43 6f 6c 75 6d  reTerm.leftColum
2781e 6e 20 72 65 63 6f 72 64 20 74 68 65 0a 2a 2a 20  n record the.** 
2781f 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 61 6e  cursor number an
27820 64 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20  d column number 
27821 66 6f 72 20 58 2e 20 20 57 68 65 72 65 54 65 72  for X.  WhereTer
27822 6d 2e 6f 70 65 72 61 74 6f 72 20 72 65 63 6f 72  m.operator recor
27823 64 73 0a 2a 2a 20 74 68 65 20 3c 6f 70 3e 20 75  ds.** the <op> u
27824 73 69 6e 67 20 61 20 62 69 74 6d 61 73 6b 20 65  sing a bitmask e
27825 6e 63 6f 64 69 6e 67 20 64 65 66 69 6e 65 64 20  ncoding defined 
27826 62 79 20 57 4f 5f 78 78 78 20 62 65 6c 6f 77 2e  by WO_xxx below.
27827 20 20 54 68 65 0a 2a 2a 20 75 73 65 20 6f 66 20    The.** use of 
27828 61 20 62 69 74 6d 61 73 6b 20 65 6e 63 6f 64 69  a bitmask encodi
27829 6e 67 20 66 6f 72 20 74 68 65 20 6f 70 65 72 61  ng for the opera
2782a 74 6f 72 20 61 6c 6c 6f 77 73 20 75 73 20 74 6f  tor allows us to
2782b 20 73 65 61 72 63 68 0a 2a 2a 20 71 75 69 63 6b   search.** quick
2782c 6c 79 20 66 6f 72 20 74 65 72 6d 73 20 74 68 61  ly for terms tha
2782d 74 20 6d 61 74 63 68 20 61 6e 79 20 6f 66 20 73  t match any of s
2782e 65 76 65 72 61 6c 20 64 69 66 66 65 72 65 6e 74  everal different
2782f 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a 2a 0a 2a   operators..**.*
27830 2a 20 70 72 65 72 65 71 52 69 67 68 74 20 61 6e  * prereqRight an
27831 64 20 70 72 65 72 65 71 41 6c 6c 20 72 65 63 6f  d prereqAll reco
27832 72 64 20 73 65 74 73 20 6f 66 20 63 75 72 73 6f  rd sets of curso
27833 72 20 6e 75 6d 62 65 72 73 2c 0a 2a 2a 20 62 75  r numbers,.** bu
27834 74 20 74 68 65 79 20 64 6f 20 73 6f 20 69 6e 64  t they do so ind
27835 69 72 65 63 74 6c 79 2e 20 20 41 20 73 69 6e 67  irectly.  A sing
27836 6c 65 20 45 78 70 72 4d 61 73 6b 53 65 74 20 73  le ExprMaskSet s
27837 74 72 75 63 74 75 72 65 20 74 72 61 6e 73 6c 61  tructure transla
27838 74 65 73 0a 2a 2a 20 63 75 72 73 6f 72 20 6e 75  tes.** cursor nu
27839 6d 62 65 72 20 69 6e 74 6f 20 62 69 74 73 20 61  mber into bits a
2783a 6e 64 20 74 68 65 20 74 72 61 6e 73 6c 61 74 65  nd the translate
2783b 64 20 62 69 74 20 69 73 20 73 74 6f 72 65 64 20  d bit is stored 
2783c 69 6e 20 74 68 65 20 70 72 65 72 65 71 0a 2a 2a  in the prereq.**
2783d 20 66 69 65 6c 64 73 2e 20 20 54 68 65 20 74 72   fields.  The tr
2783e 61 6e 73 6c 61 74 69 6f 6e 20 69 73 20 75 73 65  anslation is use
2783f 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61  d in order to ma
27840 78 69 6d 69 7a 65 20 74 68 65 20 6e 75 6d 62 65  ximize the numbe
27841 72 20 6f 66 0a 2a 2a 20 62 69 74 73 20 74 68 61  r of.** bits tha
27842 74 20 77 69 6c 6c 20 66 69 74 20 69 6e 20 61 20  t will fit in a 
27843 42 69 74 6d 61 73 6b 2e 20 20 54 68 65 20 56 44  Bitmask.  The VD
27844 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  BE cursor number
27845 73 20 6d 69 67 68 74 20 62 65 0a 2a 2a 20 73 70  s might be.** sp
27846 72 65 61 64 20 6f 75 74 20 6f 76 65 72 20 74 68  read out over th
27847 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 69  e non-negative i
27848 6e 74 65 67 65 72 73 2e 20 20 46 6f 72 20 65 78  ntegers.  For ex
27849 61 6d 70 6c 65 2c 20 74 68 65 20 63 75 72 73 6f  ample, the curso
2784a 72 0a 2a 2a 20 6e 75 6d 62 65 72 73 20 6d 69 67  r.** numbers mig
2784b 68 74 20 62 65 20 33 2c 20 38 2c 20 39 2c 20 31  ht be 3, 8, 9, 1
2784c 30 2c 20 32 30 2c 20 32 33 2c 20 34 31 2c 20 61  0, 20, 23, 41, a
2784d 6e 64 20 34 35 2e 20 20 54 68 65 20 45 78 70 72  nd 45.  The Expr
2784e 4d 61 73 6b 53 65 74 0a 2a 2a 20 74 72 61 6e 73  MaskSet.** trans
2784f 6c 61 74 65 73 20 74 68 65 73 65 20 73 70 61 72  lates these spar
27850 73 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  se cursor number
27851 73 20 69 6e 74 6f 20 63 6f 6e 73 65 63 75 74 69  s into consecuti
27852 76 65 20 69 6e 74 65 67 65 72 73 0a 2a 2a 20 62  ve integers.** b
27853 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 30 20  eginning with 0 
27854 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61 6b 65  in order to make
27855 20 74 68 65 20 62 65 73 74 20 70 6f 73 73 69 62   the best possib
27856 6c 65 20 75 73 65 20 6f 66 20 74 68 65 20 61 76  le use of the av
27857 61 69 6c 61 62 6c 65 0a 2a 2a 20 62 69 74 73 20  ailable.** bits 
27858 69 6e 20 74 68 65 20 42 69 74 6d 61 73 6b 2e 20  in the Bitmask. 
27859 20 53 6f 2c 20 69 6e 20 74 68 65 20 65 78 61 6d   So, in the exam
2785a 70 6c 65 20 61 62 6f 76 65 2c 20 74 68 65 20 63  ple above, the c
2785b 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 0a 2a 2a  ursor numbers.**
2785c 20 77 6f 75 6c 64 20 62 65 20 6d 61 70 70 65 64   would be mapped
2785d 20 69 6e 74 6f 20 69 6e 74 65 67 65 72 73 20 30   into integers 0
2785e 20 74 68 72 6f 75 67 68 20 37 2e 0a 2a 2f 0a 74   through 7..*/.t
2785f 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57 68  ypedef struct Wh
27860 65 72 65 54 65 72 6d 20 57 68 65 72 65 54 65 72  ereTerm WhereTer
27861 6d 3b 0a 73 74 72 75 63 74 20 57 68 65 72 65 54  m;.struct WhereT
27862 65 72 6d 20 7b 0a 20 20 45 78 70 72 20 2a 70 45  erm {.  Expr *pE
27863 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  xpr;            
27864 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68  /* Pointer to th
27865 65 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20  e subexpression 
27866 2a 2f 0a 20 20 69 31 36 20 69 50 61 72 65 6e 74  */.  i16 iParent
27867 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
27868 44 69 73 61 62 6c 65 20 70 57 43 2d 3e 61 5b 69  Disable pWC->a[i
27869 50 61 72 65 6e 74 5d 20 77 68 65 6e 20 74 68 69  Parent] when thi
2786a 73 20 74 65 72 6d 20 64 69 73 61 62 6c 65 64 20  s term disabled 
2786b 2a 2f 0a 20 20 69 31 36 20 6c 65 66 74 43 75 72  */.  i16 leftCur
2786c 73 6f 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  sor;         /* 
2786d 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66  Cursor number of
2786e 20 58 20 69 6e 20 22 58 20 3c 6f 70 3e 20 3c 65   X in "X <op> <e
2786f 78 70 72 3e 22 20 2a 2f 0a 20 20 69 31 36 20 6c  xpr>" */.  i16 l
27870 65 66 74 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20  eftColumn;      
27871 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d     /* Column num
27872 62 65 72 20 6f 66 20 58 20 69 6e 20 22 58 20 3c  ber of X in "X <
27873 6f 70 3e 20 3c 65 78 70 72 3e 22 20 2a 2f 0a 20  op> <expr>" */. 
27874 20 75 31 36 20 65 4f 70 65 72 61 74 6f 72 3b 20   u16 eOperator; 
27875 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 57 4f           /* A WO
27876 5f 78 78 20 76 61 6c 75 65 20 64 65 73 63 72 69  _xx value descri
27877 62 69 6e 67 20 3c 6f 70 3e 20 2a 2f 0a 20 20 75  bing <op> */.  u
27878 38 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20  8 flags;        
27879 20 20 20 20 20 20 20 2f 2a 20 42 69 74 20 66 6c         /* Bit fl
2787a 61 67 73 2e 20 20 53 65 65 20 62 65 6c 6f 77 20  ags.  See below 
2787b 2a 2f 0a 20 20 75 38 20 6e 43 68 69 6c 64 3b 20  */.  u8 nChild; 
2787c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2787d 4e 75 6d 62 65 72 20 6f 66 20 63 68 69 6c 64 72  Number of childr
2787e 65 6e 20 74 68 61 74 20 6d 75 73 74 20 64 69 73  en that must dis
2787f 61 62 6c 65 20 75 73 20 2a 2f 0a 20 20 57 68 65  able us */.  Whe
27880 72 65 43 6c 61 75 73 65 20 2a 70 57 43 3b 20 20  reClause *pWC;  
27881 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6c 61 75       /* The clau
27882 73 65 20 74 68 69 73 20 74 65 72 6d 20 69 73 20  se this term is 
27883 70 61 72 74 20 6f 66 20 2a 2f 0a 20 20 42 69 74  part of */.  Bit
27884 6d 61 73 6b 20 70 72 65 72 65 71 52 69 67 68 74  mask prereqRight
27885 3b 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20  ;    /* Bitmask 
27886 6f 66 20 74 61 62 6c 65 73 20 75 73 65 64 20 62  of tables used b
27887 79 20 70 52 69 67 68 74 20 2a 2f 0a 20 20 42 69  y pRight */.  Bi
27888 74 6d 61 73 6b 20 70 72 65 72 65 71 41 6c 6c 3b  tmask prereqAll;
27889 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b        /* Bitmask
2788a 20 6f 66 20 74 61 62 6c 65 73 20 72 65 66 65 72   of tables refer
2788b 65 6e 63 65 64 20 62 79 20 70 20 2a 2f 0a 7d 3b  enced by p */.};
2788c 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20  ../*.** Allowed 
2788d 76 61 6c 75 65 73 20 6f 66 20 57 68 65 72 65 54  values of WhereT
2788e 65 72 6d 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65  erm.flags.*/.#de
2788f 66 69 6e 65 20 54 45 52 4d 5f 44 59 4e 41 4d 49  fine TERM_DYNAMI
27890 43 20 20 20 20 30 78 30 31 20 20 20 2f 2a 20 4e  C    0x01   /* N
27891 65 65 64 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69  eed to call sqli
27892 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
27893 2c 20 70 45 78 70 72 29 20 2a 2f 0a 23 64 65 66  , pExpr) */.#def
27894 69 6e 65 20 54 45 52 4d 5f 56 49 52 54 55 41 4c  ine TERM_VIRTUAL
27895 20 20 20 20 30 78 30 32 20 20 20 2f 2a 20 41 64      0x02   /* Ad
27896 64 65 64 20 62 79 20 74 68 65 20 6f 70 74 69 6d  ded by the optim
27897 69 7a 65 72 2e 20 20 44 6f 20 6e 6f 74 20 63 6f  izer.  Do not co
27898 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45  de */.#define TE
27899 52 4d 5f 43 4f 44 45 44 20 20 20 20 20 20 30 78  RM_CODED      0x
2789a 30 34 20 20 20 2f 2a 20 54 68 69 73 20 74 65 72  04   /* This ter
2789b 6d 20 69 73 20 61 6c 72 65 61 64 79 20 63 6f 64  m is already cod
2789c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45  ed */.#define TE
2789d 52 4d 5f 43 4f 50 49 45 44 20 20 20 20 20 30 78  RM_COPIED     0x
2789e 30 38 20 20 20 2f 2a 20 48 61 73 20 61 20 63 68  08   /* Has a ch
2789f 69 6c 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54  ild */.#define T
278a0 45 52 4d 5f 4f 52 5f 4f 4b 20 20 20 20 20 20 30  ERM_OR_OK      0
278a1 78 31 30 20 20 20 2f 2a 20 55 73 65 64 20 64 75  x10   /* Used du
278a2 72 69 6e 67 20 4f 52 2d 63 6c 61 75 73 65 20 70  ring OR-clause p
278a3 72 6f 63 65 73 73 69 6e 67 20 2a 2f 0a 0a 2f 2a  rocessing */../*
278a4 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
278a5 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
278a6 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73   structure holds
278a7 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   all information
278a8 20 61 62 6f 75 74 20 61 0a 2a 2a 20 57 48 45 52   about a.** WHER
278a9 45 20 63 6c 61 75 73 65 2e 20 20 4d 6f 73 74 6c  E clause.  Mostl
278aa 79 20 74 68 69 73 20 69 73 20 61 20 63 6f 6e 74  y this is a cont
278ab 61 69 6e 65 72 20 66 6f 72 20 6f 6e 65 20 6f 72  ainer for one or
278ac 20 6d 6f 72 65 20 57 68 65 72 65 54 65 72 6d 73   more WhereTerms
278ad 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72  ..*/.struct Wher
278ae 65 43 6c 61 75 73 65 20 7b 0a 20 20 50 61 72 73  eClause {.  Pars
278af 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20  e *pParse;      
278b0 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
278b1 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
278b2 45 78 70 72 4d 61 73 6b 53 65 74 20 2a 70 4d 61  ExprMaskSet *pMa
278b3 73 6b 53 65 74 3b 20 20 20 2f 2a 20 4d 61 70 70  skSet;   /* Mapp
278b4 69 6e 67 20 6f 66 20 74 61 62 6c 65 20 69 6e 64  ing of table ind
278b5 69 63 65 73 20 74 6f 20 62 69 74 6d 61 73 6b 73  ices to bitmasks
278b6 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b   */.  int nTerm;
278b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
278b8 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d  * Number of term
278b9 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 6c 6f 74  s */.  int nSlot
278ba 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
278bb 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
278bc 72 69 65 73 20 69 6e 20 61 5b 5d 20 2a 2f 0a 20  ries in a[] */. 
278bd 20 57 68 65 72 65 54 65 72 6d 20 2a 61 3b 20 20   WhereTerm *a;  
278be 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 61 63            /* Eac
278bf 68 20 61 5b 5d 20 64 65 73 63 72 69 62 65 73 20  h a[] describes 
278c0 61 20 74 65 72 6d 20 6f 66 20 74 68 65 20 57 48  a term of the WH
278c1 45 52 45 20 63 6c 75 61 73 65 20 2a 2f 0a 20 20  ERE cluase */.  
278c2 57 68 65 72 65 54 65 72 6d 20 61 53 74 61 74 69  WhereTerm aStati
278c3 63 5b 31 30 5d 3b 20 20 20 2f 2a 20 49 6e 69 74  c[10];   /* Init
278c4 69 61 6c 20 73 74 61 74 69 63 20 73 70 61 63 65  ial static space
278c5 20 66 6f 72 20 61 5b 5d 20 2a 2f 0a 7d 3b 0a 0a   for a[] */.};..
278c6 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
278c7 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
278c8 6e 67 20 73 74 72 75 63 74 75 72 65 20 6b 65 65  ng structure kee
278c9 70 73 20 74 72 61 63 6b 20 6f 66 20 61 20 6d 61  ps track of a ma
278ca 70 70 69 6e 67 0a 2a 2a 20 62 65 74 77 65 65 6e  pping.** between
278cb 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d   VDBE cursor num
278cc 62 65 72 73 20 61 6e 64 20 62 69 74 73 20 6f 66  bers and bits of
278cd 20 74 68 65 20 62 69 74 6d 61 73 6b 73 20 69 6e   the bitmasks in
278ce 20 57 68 65 72 65 54 65 72 6d 2e 0a 2a 2a 0a 2a   WhereTerm..**.*
278cf 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f  * The VDBE curso
278d0 72 20 6e 75 6d 62 65 72 73 20 61 72 65 20 73 6d  r numbers are sm
278d1 61 6c 6c 20 69 6e 74 65 67 65 72 73 20 63 6f 6e  all integers con
278d2 74 61 69 6e 65 64 20 69 6e 20 0a 2a 2a 20 53 72  tained in .** Sr
278d3 63 4c 69 73 74 5f 69 74 65 6d 2e 69 43 75 72 73  cList_item.iCurs
278d4 6f 72 20 61 6e 64 20 45 78 70 72 2e 69 54 61 62  or and Expr.iTab
278d5 6c 65 20 66 69 65 6c 64 73 2e 20 20 46 6f 72 20  le fields.  For 
278d6 61 6e 79 20 67 69 76 65 6e 20 57 48 45 52 45 20  any given WHERE 
278d7 0a 2a 2a 20 63 6c 61 75 73 65 2c 20 74 68 65 20  .** clause, the 
278d8 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 6d  cursor numbers m
278d9 69 67 68 74 20 6e 6f 74 20 62 65 67 69 6e 20 77  ight not begin w
278da 69 74 68 20 30 20 61 6e 64 20 74 68 65 79 20 6d  ith 0 and they m
278db 69 67 68 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20  ight.** contain 
278dc 67 61 70 73 20 69 6e 20 74 68 65 20 6e 75 6d 62  gaps in the numb
278dd 65 72 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 20  ering sequence. 
278de 20 42 75 74 20 77 65 20 77 61 6e 74 20 74 6f 20   But we want to 
278df 6d 61 6b 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20  make maximum.** 
278e0 75 73 65 20 6f 66 20 74 68 65 20 62 69 74 73 20  use of the bits 
278e1 69 6e 20 6f 75 72 20 62 69 74 6d 61 73 6b 73 2e  in our bitmasks.
278e2 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65    This structure
278e3 20 70 72 6f 76 69 64 65 73 20 61 20 6d 61 70 70   provides a mapp
278e4 69 6e 67 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20  ing.** from the 
278e5 73 70 61 72 73 65 20 63 75 72 73 6f 72 20 6e 75  sparse cursor nu
278e6 6d 62 65 72 73 20 69 6e 74 6f 20 63 6f 6e 73 65  mbers into conse
278e7 63 75 74 69 76 65 20 69 6e 74 65 67 65 72 73 20  cutive integers 
278e8 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69 74  beginning.** wit
278e9 68 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 45 78  h 0..**.** If Ex
278ea 70 72 4d 61 73 6b 53 65 74 2e 69 78 5b 41 5d 3d  prMaskSet.ix[A]=
278eb 3d 42 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  =B it means that
278ec 20 54 68 65 20 41 2d 74 68 20 62 69 74 20 6f 66   The A-th bit of
278ed 20 61 20 42 69 74 6d 61 73 6b 0a 2a 2a 20 63 6f   a Bitmask.** co
278ee 72 72 65 73 70 6f 6e 64 73 20 56 44 42 45 20 63  rresponds VDBE c
278ef 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 42 2e 20  ursor number B. 
278f0 20 54 68 65 20 41 2d 74 68 20 62 69 74 20 6f 66   The A-th bit of
278f1 20 61 20 62 69 74 6d 61 73 6b 20 69 73 20 31 3c   a bitmask is 1<
278f2 3c 41 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78  <A..**.** For ex
278f3 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 57 48  ample, if the WH
278f4 45 52 45 20 63 6c 61 75 73 65 20 65 78 70 72 65  ERE clause expre
278f5 73 73 69 6f 6e 20 75 73 65 64 20 74 68 65 73 65  ssion used these
278f6 20 56 44 42 45 0a 2a 2a 20 63 75 72 73 6f 72 73   VDBE.** cursors
278f7 3a 20 20 34 2c 20 35 2c 20 38 2c 20 32 39 2c 20  :  4, 5, 8, 29, 
278f8 35 37 2c 20 37 33 2e 20 20 54 68 65 6e 20 74 68  57, 73.  Then th
278f9 65 20 20 45 78 70 72 4d 61 73 6b 53 65 74 20 73  e  ExprMaskSet s
278fa 74 72 75 63 74 75 72 65 0a 2a 2a 20 77 6f 75 6c  tructure.** woul
278fb 64 20 6d 61 70 20 74 68 6f 73 65 20 63 75 72 73  d map those curs
278fc 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e 74 6f 20  or numbers into 
278fd 62 69 74 73 20 30 20 74 68 72 6f 75 67 68 20 35  bits 0 through 5
278fe 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
278ff 74 20 74 68 65 20 6d 61 70 70 69 6e 67 20 69 73  t the mapping is
27900 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79   not necessarily
27901 20 6f 72 64 65 72 65 64 2e 20 20 49 6e 20 74 68   ordered.  In th
27902 65 20 65 78 61 6d 70 6c 65 0a 2a 2a 20 61 62 6f  e example.** abo
27903 76 65 2c 20 74 68 65 20 6d 61 70 70 69 6e 67 20  ve, the mapping 
27904 6d 69 67 68 74 20 67 6f 20 6c 69 6b 65 20 74 68  might go like th
27905 69 73 3a 20 20 34 2d 3e 33 2c 20 35 2d 3e 31 2c  is:  4->3, 5->1,
27906 20 38 2d 3e 32 2c 20 32 39 2d 3e 30 2c 0a 2a 2a   8->2, 29->0,.**
27907 20 35 37 2d 3e 35 2c 20 37 33 2d 3e 34 2e 20 20   57->5, 73->4.  
27908 4f 72 20 6f 6e 65 20 6f 66 20 37 31 39 20 6f 74  Or one of 719 ot
27909 68 65 72 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73  her combinations
2790a 20 6d 69 67 68 74 20 62 65 20 75 73 65 64 2e 20   might be used. 
2790b 49 74 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 72  It.** does not r
2790c 65 61 6c 6c 79 20 6d 61 74 74 65 72 2e 20 20 57  eally matter.  W
2790d 68 61 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74  hat is important
2790e 20 69 73 20 74 68 61 74 20 73 70 61 72 73 65 20   is that sparse 
2790f 63 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d 62 65 72  cursor.** number
27910 73 20 61 6c 6c 20 67 65 74 20 6d 61 70 70 65 64  s all get mapped
27911 20 69 6e 74 6f 20 62 69 74 20 6e 75 6d 62 65 72   into bit number
27912 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 69 74  s that begin wit
27913 68 20 30 20 61 6e 64 20 63 6f 6e 74 61 69 6e 0a  h 0 and contain.
27914 2a 2a 20 6e 6f 20 67 61 70 73 2e 0a 2a 2f 0a 73  ** no gaps..*/.s
27915 74 72 75 63 74 20 45 78 70 72 4d 61 73 6b 53 65  truct ExprMaskSe
27916 74 20 7b 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20  t {.  int n;    
27917 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27918 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
27919 20 61 73 73 69 67 6e 65 64 20 63 75 72 73 6f 72   assigned cursor
2791a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 69 6e 74   values */.  int
2791b 20 69 78 5b 73 69 7a 65 6f 66 28 42 69 74 6d 61   ix[sizeof(Bitma
2791c 73 6b 29 2a 38 5d 3b 20 20 20 20 2f 2a 20 43 75  sk)*8];    /* Cu
2791d 72 73 6f 72 20 61 73 73 69 67 6e 65 64 20 74 6f  rsor assigned to
2791e 20 65 61 63 68 20 62 69 74 20 2a 2f 0a 7d 3b 0a   each bit */.};.
2791f 0a 0a 2f 2a 0a 2a 2a 20 42 69 74 6d 61 73 6b 73  ../*.** Bitmasks
27920 20 66 6f 72 20 74 68 65 20 6f 70 65 72 61 74 6f   for the operato
27921 72 73 20 74 68 61 74 20 69 6e 64 69 63 65 73 20  rs that indices 
27922 61 72 65 20 61 62 6c 65 20 74 6f 20 65 78 70 6c  are able to expl
27923 6f 69 74 2e 20 20 41 6e 0a 2a 2a 20 4f 52 2d 65  oit.  An.** OR-e
27924 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66  d combination of
27925 20 74 68 65 73 65 20 76 61 6c 75 65 73 20 63 61   these values ca
27926 6e 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 73  n be used when s
27927 65 61 72 63 68 69 6e 67 20 66 6f 72 0a 2a 2a 20  earching for.** 
27928 74 65 72 6d 73 20 69 6e 20 74 68 65 20 77 68 65  terms in the whe
27929 72 65 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 23 64  re clause..*/.#d
2792a 65 66 69 6e 65 20 57 4f 5f 49 4e 20 20 20 20 20  efine WO_IN     
2792b 31 0a 23 64 65 66 69 6e 65 20 57 4f 5f 45 51 20  1.#define WO_EQ 
2792c 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 57 4f      2.#define WO
2792d 5f 4c 54 20 20 20 20 20 28 57 4f 5f 45 51 3c 3c  _LT     (WO_EQ<<
2792e 28 54 4b 5f 4c 54 2d 54 4b 5f 45 51 29 29 0a 23  (TK_LT-TK_EQ)).#
2792f 64 65 66 69 6e 65 20 57 4f 5f 4c 45 20 20 20 20  define WO_LE    
27930 20 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f 4c 45 2d   (WO_EQ<<(TK_LE-
27931 54 4b 5f 45 51 29 29 0a 23 64 65 66 69 6e 65 20  TK_EQ)).#define 
27932 57 4f 5f 47 54 20 20 20 20 20 28 57 4f 5f 45 51  WO_GT     (WO_EQ
27933 3c 3c 28 54 4b 5f 47 54 2d 54 4b 5f 45 51 29 29  <<(TK_GT-TK_EQ))
27934 0a 23 64 65 66 69 6e 65 20 57 4f 5f 47 45 20 20  .#define WO_GE  
27935 20 20 20 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f 47     (WO_EQ<<(TK_G
27936 45 2d 54 4b 5f 45 51 29 29 0a 23 64 65 66 69 6e  E-TK_EQ)).#defin
27937 65 20 57 4f 5f 4d 41 54 43 48 20 20 36 34 0a 23  e WO_MATCH  64.#
27938 64 65 66 69 6e 65 20 57 4f 5f 49 53 4e 55 4c 4c  define WO_ISNULL
27939 20 31 32 38 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 75   128../*.** Valu
2793a 65 20 66 6f 72 20 66 6c 61 67 73 20 72 65 74 75  e for flags retu
2793b 72 6e 65 64 20 62 79 20 62 65 73 74 49 6e 64 65  rned by bestInde
2793c 78 28 29 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65  x().  .**.** The
2793d 20 6c 65 61 73 74 20 73 69 67 6e 69 66 69 63 61   least significa
2793e 6e 74 20 62 79 74 65 20 69 73 20 72 65 73 65 72  nt byte is reser
2793f 76 65 64 20 61 73 20 61 20 6d 61 73 6b 20 66 6f  ved as a mask fo
27940 72 20 57 4f 5f 20 76 61 6c 75 65 73 20 61 62 6f  r WO_ values abo
27941 76 65 2e 0a 2a 2a 20 54 68 65 20 57 68 65 72 65  ve..** The Where
27942 4c 65 76 65 6c 2e 66 6c 61 67 73 20 66 69 65 6c  Level.flags fiel
27943 64 20 69 73 20 75 73 75 61 6c 6c 79 20 73 65 74  d is usually set
27944 20 74 6f 20 57 4f 5f 49 4e 7c 57 4f 5f 45 51 7c   to WO_IN|WO_EQ|
27945 57 4f 5f 49 53 4e 55 4c 4c 2e 0a 2a 2a 20 42 75  WO_ISNULL..** Bu
27946 74 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69  t if the table i
27947 73 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c  s the right tabl
27948 65 20 6f 66 20 61 20 6c 65 66 74 20 6a 6f 69 6e  e of a left join
27949 2c 20 57 68 65 72 65 4c 65 76 65 6c 2e 66 6c 61  , WhereLevel.fla
2794a 67 73 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20  gs.** is set to 
2794b 57 4f 5f 49 4e 7c 57 4f 5f 45 51 2e 20 20 54 68  WO_IN|WO_EQ.  Th
2794c 65 20 57 68 65 72 65 4c 65 76 65 6c 2e 66 6c 61  e WhereLevel.fla
2794d 67 73 20 66 69 65 6c 64 20 63 61 6e 20 74 68 65  gs field can the
2794e 6e 20 62 65 20 75 73 65 64 20 61 73 0a 2a 2a 20  n be used as.** 
2794f 74 68 65 20 22 6f 70 22 20 70 61 72 61 6d 65 74  the "op" paramet
27950 65 72 20 74 6f 20 66 69 6e 64 54 65 72 6d 20 77  er to findTerm w
27951 68 65 6e 20 77 65 20 61 72 65 20 72 65 73 6f 6c  hen we are resol
27952 76 69 6e 67 20 65 71 75 61 6c 69 74 79 20 63 6f  ving equality co
27953 6e 73 74 72 61 69 6e 74 73 2e 0a 2a 2a 20 49 53  nstraints..** IS
27954 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 73  NULL constraints
27955 20 77 69 6c 6c 20 74 68 65 6e 20 6e 6f 74 20 62   will then not b
27956 65 20 75 73 65 64 20 6f 6e 20 74 68 65 20 72 69  e used on the ri
27957 67 68 74 20 74 61 62 6c 65 20 6f 66 20 61 20 6c  ght table of a l
27958 65 66 74 0a 2a 2a 20 6a 6f 69 6e 2e 20 20 54 69  eft.** join.  Ti
27959 63 6b 65 74 73 20 23 32 31 37 37 20 61 6e 64 20  ckets #2177 and 
2795a 23 32 31 38 39 2e 0a 2a 2f 0a 23 64 65 66 69 6e  #2189..*/.#defin
2795b 65 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51  e WHERE_ROWID_EQ
2795c 20 20 20 20 20 30 78 30 30 30 31 30 30 20 20 20       0x000100   
2795d 2f 2a 20 72 6f 77 69 64 3d 45 58 50 52 20 6f 72  /* rowid=EXPR or
2795e 20 72 6f 77 69 64 20 49 4e 20 28 2e 2e 2e 29 20   rowid IN (...) 
2795f 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45  */.#define WHERE
27960 5f 52 4f 57 49 44 5f 52 41 4e 47 45 20 20 30 78  _ROWID_RANGE  0x
27961 30 30 30 32 30 30 20 20 20 2f 2a 20 72 6f 77 69  000200   /* rowi
27962 64 3c 45 58 50 52 20 61 6e 64 2f 6f 72 20 72 6f  d<EXPR and/or ro
27963 77 69 64 3e 45 58 50 52 20 2a 2f 0a 23 64 65 66  wid>EXPR */.#def
27964 69 6e 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e  ine WHERE_COLUMN
27965 5f 45 51 20 20 20 20 30 78 30 30 31 30 30 30 20  _EQ    0x001000 
27966 20 20 2f 2a 20 78 3d 45 58 50 52 20 6f 72 20 78    /* x=EXPR or x
27967 20 49 4e 20 28 2e 2e 2e 29 20 2a 2f 0a 23 64 65   IN (...) */.#de
27968 66 69 6e 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d  fine WHERE_COLUM
27969 4e 5f 52 41 4e 47 45 20 30 78 30 30 32 30 30 30  N_RANGE 0x002000
2796a 20 20 20 2f 2a 20 78 3c 45 58 50 52 20 61 6e 64     /* x<EXPR and
2796b 2f 6f 72 20 78 3e 45 58 50 52 20 2a 2f 0a 23 64  /or x>EXPR */.#d
2796c 65 66 69 6e 65 20 57 48 45 52 45 5f 43 4f 4c 55  efine WHERE_COLU
2796d 4d 4e 5f 49 4e 20 20 20 20 30 78 30 30 34 30 30  MN_IN    0x00400
2796e 30 20 20 20 2f 2a 20 78 20 49 4e 20 28 2e 2e 2e  0   /* x IN (...
2796f 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  ) */.#define WHE
27970 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 20 20 20 20  RE_TOP_LIMIT    
27971 30 78 30 31 30 30 30 30 20 20 20 2f 2a 20 78 3c  0x010000   /* x<
27972 45 58 50 52 20 6f 72 20 78 3c 3d 45 58 50 52 20  EXPR or x<=EXPR 
27973 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 23 64  constraint */.#d
27974 65 66 69 6e 65 20 57 48 45 52 45 5f 42 54 4d 5f  efine WHERE_BTM_
27975 4c 49 4d 49 54 20 20 20 20 30 78 30 32 30 30 30  LIMIT    0x02000
27976 30 20 20 20 2f 2a 20 78 3e 45 58 50 52 20 6f 72  0   /* x>EXPR or
27977 20 78 3e 3d 45 58 50 52 20 63 6f 6e 73 74 72 61   x>=EXPR constra
27978 69 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57  int */.#define W
27979 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 20 20 20  HERE_IDX_ONLY   
2797a 20 20 30 78 30 38 30 30 30 30 20 20 20 2f 2a 20    0x080000   /* 
2797b 55 73 65 20 69 6e 64 65 78 20 6f 6e 6c 79 20 2d  Use index only -
2797c 20 6f 6d 69 74 20 74 61 62 6c 65 20 2a 2f 0a 23   omit table */.#
2797d 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44  define WHERE_ORD
2797e 45 52 42 59 20 20 20 20 20 20 30 78 31 30 30 30  ERBY      0x1000
2797f 30 30 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 77  00   /* Output w
27980 69 6c 6c 20 61 70 70 65 61 72 20 69 6e 20 63 6f  ill appear in co
27981 72 72 65 63 74 20 6f 72 64 65 72 20 2a 2f 0a 23  rrect order */.#
27982 64 65 66 69 6e 65 20 57 48 45 52 45 5f 52 45 56  define WHERE_REV
27983 45 52 53 45 20 20 20 20 20 20 30 78 32 30 30 30  ERSE      0x2000
27984 30 30 20 20 20 2f 2a 20 53 63 61 6e 20 69 6e 20  00   /* Scan in 
27985 72 65 76 65 72 73 65 20 6f 72 64 65 72 20 2a 2f  reverse order */
27986 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 55  .#define WHERE_U
27987 4e 49 51 55 45 20 20 20 20 20 20 20 30 78 34 30  NIQUE       0x40
27988 30 30 30 30 20 20 20 2f 2a 20 53 65 6c 65 63 74  0000   /* Select
27989 73 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f  s no more than o
2798a 6e 65 20 72 6f 77 20 2a 2f 0a 23 64 65 66 69 6e  ne row */.#defin
2798b 65 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54  e WHERE_VIRTUALT
2798c 41 42 4c 45 20 30 78 38 30 30 30 30 30 20 20 20  ABLE 0x800000   
2798d 2f 2a 20 55 73 65 20 76 69 72 74 75 61 6c 2d 74  /* Use virtual-t
2798e 61 62 6c 65 20 70 72 6f 63 65 73 73 69 6e 67 20  able processing 
2798f 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  */../*.** Initia
27990 6c 69 7a 65 20 61 20 70 72 65 61 6c 6c 6f 63 61  lize a prealloca
27991 74 65 64 20 57 68 65 72 65 43 6c 61 75 73 65 20  ted WhereClause 
27992 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74  structure..*/.st
27993 61 74 69 63 20 76 6f 69 64 20 77 68 65 72 65 43  atic void whereC
27994 6c 61 75 73 65 49 6e 69 74 28 0a 20 20 57 68 65  lauseInit(.  Whe
27995 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20  reClause *pWC,  
27996 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 68 65        /* The Whe
27997 72 65 43 6c 61 75 73 65 20 74 6f 20 62 65 20 69  reClause to be i
27998 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20  nitialized */.  
27999 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2799a 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2799b 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  parsing context 
2799c 2a 2f 0a 20 20 45 78 70 72 4d 61 73 6b 53 65 74  */.  ExprMaskSet
2799d 20 2a 70 4d 61 73 6b 53 65 74 20 20 20 20 2f 2a   *pMaskSet    /*
2799e 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 74 61   Mapping from ta
2799f 62 6c 65 20 69 6e 64 69 63 65 73 20 74 6f 20 62  ble indices to b
279a0 69 74 6d 61 73 6b 73 20 2a 2f 0a 29 7b 0a 20 20  itmasks */.){.  
279a1 70 57 43 2d 3e 70 50 61 72 73 65 20 3d 20 70 50  pWC->pParse = pP
279a2 61 72 73 65 3b 0a 20 20 70 57 43 2d 3e 70 4d 61  arse;.  pWC->pMa
279a3 73 6b 53 65 74 20 3d 20 70 4d 61 73 6b 53 65 74  skSet = pMaskSet
279a4 3b 0a 20 20 70 57 43 2d 3e 6e 54 65 72 6d 20 3d  ;.  pWC->nTerm =
279a5 20 30 3b 0a 20 20 70 57 43 2d 3e 6e 53 6c 6f 74   0;.  pWC->nSlot
279a6 20 3d 20 41 72 72 61 79 53 69 7a 65 28 70 57 43   = ArraySize(pWC
279a7 2d 3e 61 53 74 61 74 69 63 29 3b 0a 20 20 70 57  ->aStatic);.  pW
279a8 43 2d 3e 61 20 3d 20 70 57 43 2d 3e 61 53 74 61  C->a = pWC->aSta
279a9 74 69 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  tic;.}../*.** De
279aa 61 6c 6c 6f 63 61 74 65 20 61 20 57 68 65 72 65  allocate a Where
279ab 43 6c 61 75 73 65 20 73 74 72 75 63 74 75 72 65  Clause structure
279ac 2e 20 20 54 68 65 20 57 68 65 72 65 43 6c 61 75  .  The WhereClau
279ad 73 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  se structure.** 
279ae 69 74 73 65 6c 66 20 69 73 20 6e 6f 74 20 66 72  itself is not fr
279af 65 65 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69  eed.  This routi
279b0 6e 65 20 69 73 20 74 68 65 20 69 6e 76 65 72 73  ne is the invers
279b1 65 20 6f 66 20 77 68 65 72 65 43 6c 61 75 73 65  e of whereClause
279b2 49 6e 69 74 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  Init()..*/.stati
279b3 63 20 76 6f 69 64 20 77 68 65 72 65 43 6c 61 75  c void whereClau
279b4 73 65 43 6c 65 61 72 28 57 68 65 72 65 43 6c 61  seClear(WhereCla
279b5 75 73 65 20 2a 70 57 43 29 7b 0a 20 20 69 6e 74  use *pWC){.  int
279b6 20 69 3b 0a 20 20 57 68 65 72 65 54 65 72 6d 20   i;.  WhereTerm 
279b7 2a 61 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  *a;.  sqlite3 *d
279b8 62 20 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 2d  b = pWC->pParse-
279b9 3e 64 62 3b 0a 20 20 66 6f 72 28 69 3d 70 57 43  >db;.  for(i=pWC
279ba 2d 3e 6e 54 65 72 6d 2d 31 2c 20 61 3d 70 57 43  ->nTerm-1, a=pWC
279bb 2d 3e 61 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20  ->a; i>=0; i--, 
279bc 61 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 2d  a++){.    if( a-
279bd 3e 66 6c 61 67 73 20 26 20 54 45 52 4d 5f 44 59  >flags & TERM_DY
279be 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 20 20 73  NAMIC ){.      s
279bf 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
279c0 28 64 62 2c 20 61 2d 3e 70 45 78 70 72 29 3b 0a  (db, a->pExpr);.
279c1 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
279c2 70 57 43 2d 3e 61 21 3d 70 57 43 2d 3e 61 53 74  pWC->a!=pWC->aSt
279c3 61 74 69 63 20 29 7b 0a 20 20 20 20 73 71 6c 69  atic ){.    sqli
279c4 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 57  te3DbFree(db, pW
279c5 43 2d 3e 61 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  C->a);.  }.}../*
279c6 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6e  .** Add a new en
279c7 74 72 69 65 73 20 74 6f 20 74 68 65 20 57 68 65  tries to the Whe
279c8 72 65 43 6c 61 75 73 65 20 73 74 72 75 63 74 75  reClause structu
279c9 72 65 2e 20 20 49 6e 63 72 65 61 73 65 20 74 68  re.  Increase th
279ca 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 73  e allocated.** s
279cb 70 61 63 65 20 61 73 20 6e 65 63 65 73 73 61 72  pace as necessar
279cc 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  y..**.** If the 
279cd 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 69  flags argument i
279ce 6e 63 6c 75 64 65 73 20 54 45 52 4d 5f 44 59 4e  ncludes TERM_DYN
279cf 41 4d 49 43 2c 20 74 68 65 6e 20 72 65 73 70 6f  AMIC, then respo
279d0 6e 73 69 62 69 6c 69 74 79 0a 2a 2a 20 66 6f 72  nsibility.** for
279d1 20 66 72 65 65 69 6e 67 20 74 68 65 20 65 78 70   freeing the exp
279d2 72 65 73 73 69 6f 6e 20 70 20 69 73 20 61 73 73  ression p is ass
279d3 75 6d 65 64 20 62 79 20 74 68 65 20 57 68 65 72  umed by the Wher
279d4 65 43 6c 61 75 73 65 20 6f 62 6a 65 63 74 2e 0a  eClause object..
279d5 2a 2a 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a 20 20  **.** WARNING:  
279d6 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67  This routine mig
279d7 68 74 20 72 65 61 6c 6c 6f 63 61 74 65 20 74 68  ht reallocate th
279d8 65 20 73 70 61 63 65 20 75 73 65 64 20 74 6f 20  e space used to 
279d9 73 74 6f 72 65 0a 2a 2a 20 57 68 65 72 65 54 65  store.** WhereTe
279da 72 6d 73 2e 20 20 41 6c 6c 20 70 6f 69 6e 74 65  rms.  All pointe
279db 72 73 20 74 6f 20 57 68 65 72 65 54 65 72 6d 73  rs to WhereTerms
279dc 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 76 61 6c   should be inval
279dd 69 64 61 74 65 64 20 61 66 74 65 72 0a 2a 2a 20  idated after.** 
279de 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75  calling this rou
279df 74 69 6e 65 2e 20 20 53 75 63 68 20 70 6f 69 6e  tine.  Such poin
279e0 74 65 72 73 20 6d 61 79 20 62 65 20 72 65 69 6e  ters may be rein
279e1 69 74 69 61 6c 69 7a 65 64 20 62 79 20 72 65 66  itialized by ref
279e2 65 72 65 6e 63 69 6e 67 0a 2a 2a 20 74 68 65 20  erencing.** the 
279e3 70 57 43 2d 3e 61 5b 5d 20 61 72 72 61 79 2e 0a  pWC->a[] array..
279e4 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 68  */.static int wh
279e5 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28  ereClauseInsert(
279e6 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43  WhereClause *pWC
279e7 2c 20 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66  , Expr *p, int f
279e8 6c 61 67 73 29 7b 0a 20 20 57 68 65 72 65 54 65  lags){.  WhereTe
279e9 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e 74  rm *pTerm;.  int
279ea 20 69 64 78 3b 0a 20 20 69 66 28 20 70 57 43 2d   idx;.  if( pWC-
279eb 3e 6e 54 65 72 6d 3e 3d 70 57 43 2d 3e 6e 53 6c  >nTerm>=pWC->nSl
279ec 6f 74 20 29 7b 0a 20 20 20 20 57 68 65 72 65 54  ot ){.    WhereT
279ed 65 72 6d 20 2a 70 4f 6c 64 20 3d 20 70 57 43 2d  erm *pOld = pWC-
279ee 3e 61 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20  >a;.    sqlite3 
279ef 2a 64 62 20 3d 20 70 57 43 2d 3e 70 50 61 72 73  *db = pWC->pPars
279f0 65 2d 3e 64 62 3b 0a 20 20 20 20 70 57 43 2d 3e  e->db;.    pWC->
279f1 61 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  a = sqlite3DbMal
279f2 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f  locRaw(db, sizeo
279f3 66 28 70 57 43 2d 3e 61 5b 30 5d 29 2a 70 57 43  f(pWC->a[0])*pWC
279f4 2d 3e 6e 53 6c 6f 74 2a 32 20 29 3b 0a 20 20 20  ->nSlot*2 );.   
279f5 20 69 66 28 20 70 57 43 2d 3e 61 3d 3d 30 20 29   if( pWC->a==0 )
279f6 7b 0a 20 20 20 20 20 20 69 66 28 20 66 6c 61 67  {.      if( flag
279f7 73 20 26 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43  s & TERM_DYNAMIC
279f8 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
279f9 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
279fa 2c 20 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , p);.      }.  
279fb 20 20 20 20 70 57 43 2d 3e 61 20 3d 20 70 4f 6c      pWC->a = pOl
279fc 64 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  d;.      return 
279fd 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d  0;.    }.    mem
279fe 63 70 79 28 70 57 43 2d 3e 61 2c 20 70 4f 6c 64  cpy(pWC->a, pOld
279ff 2c 20 73 69 7a 65 6f 66 28 70 57 43 2d 3e 61 5b  , sizeof(pWC->a[
27a00 30 5d 29 2a 70 57 43 2d 3e 6e 54 65 72 6d 29 3b  0])*pWC->nTerm);
27a01 0a 20 20 20 20 69 66 28 20 70 4f 6c 64 21 3d 70  .    if( pOld!=p
27a02 57 43 2d 3e 61 53 74 61 74 69 63 20 29 7b 0a 20  WC->aStatic ){. 
27a03 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
27a04 65 65 28 64 62 2c 20 70 4f 6c 64 29 3b 0a 20 20  ee(db, pOld);.  
27a05 20 20 7d 0a 20 20 20 20 70 57 43 2d 3e 6e 53 6c    }.    pWC->nSl
27a06 6f 74 20 2a 3d 20 32 3b 0a 20 20 7d 0a 20 20 70  ot *= 2;.  }.  p
27a07 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69  Term = &pWC->a[i
27a08 64 78 20 3d 20 70 57 43 2d 3e 6e 54 65 72 6d 5d  dx = pWC->nTerm]
27a09 3b 0a 20 20 70 57 43 2d 3e 6e 54 65 72 6d 2b 2b  ;.  pWC->nTerm++
27a0a 3b 0a 20 20 70 54 65 72 6d 2d 3e 70 45 78 70 72  ;.  pTerm->pExpr
27a0b 20 3d 20 70 3b 0a 20 20 70 54 65 72 6d 2d 3e 66   = p;.  pTerm->f
27a0c 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20  lags = flags;.  
27a0d 70 54 65 72 6d 2d 3e 70 57 43 20 3d 20 70 57 43  pTerm->pWC = pWC
27a0e 3b 0a 20 20 70 54 65 72 6d 2d 3e 69 50 61 72 65  ;.  pTerm->iPare
27a0f 6e 74 20 3d 20 2d 31 3b 0a 20 20 72 65 74 75 72  nt = -1;.  retur
27a10 6e 20 69 64 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n idx;.}../*.** 
27a11 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 64 65  This routine ide
27a12 6e 74 69 66 69 65 73 20 73 75 62 65 78 70 72 65  ntifies subexpre
27a13 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 57 48  ssions in the WH
27a14 45 52 45 20 63 6c 61 75 73 65 20 77 68 65 72 65  ERE clause where
27a15 0a 2a 2a 20 65 61 63 68 20 73 75 62 65 78 70 72  .** each subexpr
27a16 65 73 73 69 6f 6e 20 69 73 20 73 65 70 61 72 61  ession is separa
27a17 74 65 64 20 62 79 20 74 68 65 20 41 4e 44 20 6f  ted by the AND o
27a18 70 65 72 61 74 6f 72 20 6f 72 20 73 6f 6d 65 20  perator or some 
27a19 6f 74 68 65 72 0a 2a 2a 20 6f 70 65 72 61 74 6f  other.** operato
27a1a 72 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74  r specified in t
27a1b 68 65 20 6f 70 20 70 61 72 61 6d 65 74 65 72 2e  he op parameter.
27a1c 20 20 54 68 65 20 57 68 65 72 65 43 6c 61 75 73    The WhereClaus
27a1d 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69  e structure.** i
27a1e 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 70 6f  s filled with po
27a1f 69 6e 74 65 72 73 20 74 6f 20 73 75 62 65 78 70  inters to subexp
27a20 72 65 73 73 69 6f 6e 73 2e 20 20 46 6f 72 20 65  ressions.  For e
27a21 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20  xample:.**.**   
27a22 20 57 48 45 52 45 20 20 61 3d 3d 27 68 65 6c 6c   WHERE  a=='hell
27a23 6f 27 20 41 4e 44 20 63 6f 61 6c 65 73 63 65 28  o' AND coalesce(
27a24 62 2c 31 31 29 3c 31 30 20 41 4e 44 20 28 63 2b  b,11)<10 AND (c+
27a25 31 32 21 3d 64 20 4f 52 20 63 3d 3d 32 32 29 0a  12!=d OR c==22).
27a26 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5c 5f 5f  **           \__
27a27 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f  ______/     \___
27a28 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20  ____________/   
27a29 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f    \_____________
27a2a 5f 5f 5f 2f 0a 2a 2a 20 20 20 20 20 20 20 20 20  ___/.**         
27a2b 20 20 20 73 6c 6f 74 5b 30 5d 20 20 20 20 20 20     slot[0]      
27a2c 20 20 20 20 20 20 73 6c 6f 74 5b 31 5d 20 20 20        slot[1]   
27a2d 20 20 20 20 20 20 20 20 20 20 20 20 73 6c 6f 74              slot
27a2e 5b 32 5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 72  [2].**.** The or
27a2f 69 67 69 6e 61 6c 20 57 48 45 52 45 20 63 6c 61  iginal WHERE cla
27a30 75 73 65 20 69 6e 20 70 45 78 70 72 20 69 73 20  use in pExpr is 
27a31 75 6e 61 6c 74 65 72 65 64 2e 20 20 41 6c 6c 20  unaltered.  All 
27a32 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
27a33 64 6f 65 73 20 69 73 20 6d 61 6b 65 20 73 6c 6f  does is make slo
27a34 74 5b 5d 20 65 6e 74 72 69 65 73 20 70 6f 69 6e  t[] entries poin
27a35 74 20 74 6f 20 73 75 62 73 74 72 75 63 74 75 72  t to substructur
27a36 65 20 77 69 74 68 69 6e 20 70 45 78 70 72 2e 0a  e within pExpr..
27a37 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 70 72 65  **.** In the pre
27a38 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65 20 61  vious sentence a
27a39 6e 64 20 69 6e 20 74 68 65 20 64 69 61 67 72 61  nd in the diagra
27a3a 6d 2c 20 22 73 6c 6f 74 5b 5d 22 20 72 65 66 65  m, "slot[]" refe
27a3b 72 73 20 74 6f 0a 2a 2a 20 74 68 65 20 57 68 65  rs to.** the Whe
27a3c 72 65 43 6c 61 75 73 65 2e 61 5b 5d 20 61 72 72  reClause.a[] arr
27a3d 61 79 2e 20 20 54 68 69 73 20 61 72 72 61 79 20  ay.  This array 
27a3e 67 72 6f 77 73 20 61 73 20 6e 65 65 64 65 64 20  grows as needed 
27a3f 74 6f 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 61 6c  to contain.** al
27a40 6c 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 57  l terms of the W
27a41 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a  HERE clause..*/.
27a42 73 74 61 74 69 63 20 76 6f 69 64 20 77 68 65 72  static void wher
27a43 65 53 70 6c 69 74 28 57 68 65 72 65 43 6c 61 75  eSplit(WhereClau
27a44 73 65 20 2a 70 57 43 2c 20 45 78 70 72 20 2a 70  se *pWC, Expr *p
27a45 45 78 70 72 2c 20 69 6e 74 20 6f 70 29 7b 0a 20  Expr, int op){. 
27a46 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20   if( pExpr==0 ) 
27a47 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 45  return;.  if( pE
27a48 78 70 72 2d 3e 6f 70 21 3d 6f 70 20 29 7b 0a 20  xpr->op!=op ){. 
27a49 20 20 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e     whereClauseIn
27a4a 73 65 72 74 28 70 57 43 2c 20 70 45 78 70 72 2c  sert(pWC, pExpr,
27a4b 20 30 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20   0);.  }else{.  
27a4c 20 20 77 68 65 72 65 53 70 6c 69 74 28 70 57 43    whereSplit(pWC
27a4d 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
27a4e 6f 70 29 3b 0a 20 20 20 20 77 68 65 72 65 53 70  op);.    whereSp
27a4f 6c 69 74 28 70 57 43 2c 20 70 45 78 70 72 2d 3e  lit(pWC, pExpr->
27a50 70 52 69 67 68 74 2c 20 6f 70 29 3b 0a 20 20 7d  pRight, op);.  }
27a51 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  .}../*.** Initia
27a52 6c 69 7a 65 20 61 6e 20 65 78 70 72 65 73 73 69  lize an expressi
27a53 6f 6e 20 6d 61 73 6b 20 73 65 74 0a 2a 2f 0a 23  on mask set.*/.#
27a54 64 65 66 69 6e 65 20 69 6e 69 74 4d 61 73 6b 53  define initMaskS
27a55 65 74 28 50 29 20 20 6d 65 6d 73 65 74 28 50 2c  et(P)  memset(P,
27a56 20 30 2c 20 73 69 7a 65 6f 66 28 2a 50 29 29 0a   0, sizeof(*P)).
27a57 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
27a58 65 20 62 69 74 6d 61 73 6b 20 66 6f 72 20 74 68  e bitmask for th
27a59 65 20 67 69 76 65 6e 20 63 75 72 73 6f 72 20 6e  e given cursor n
27a5a 75 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 20 30  umber.  Return 0
27a5b 20 69 66 0a 2a 2a 20 69 43 75 72 73 6f 72 20 69   if.** iCursor i
27a5c 73 20 6e 6f 74 20 69 6e 20 74 68 65 20 73 65 74  s not in the set
27a5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 42 69 74 6d  ..*/.static Bitm
27a5e 61 73 6b 20 67 65 74 4d 61 73 6b 28 45 78 70 72  ask getMask(Expr
27a5f 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65  MaskSet *pMaskSe
27a60 74 2c 20 69 6e 74 20 69 43 75 72 73 6f 72 29 7b  t, int iCursor){
27a61 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28  .  int i;.  for(
27a62 69 3d 30 3b 20 69 3c 70 4d 61 73 6b 53 65 74 2d  i=0; i<pMaskSet-
27a63 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  >n; i++){.    if
27a64 28 20 70 4d 61 73 6b 53 65 74 2d 3e 69 78 5b 69  ( pMaskSet->ix[i
27a65 5d 3d 3d 69 43 75 72 73 6f 72 20 29 7b 0a 20 20  ]==iCursor ){.  
27a66 20 20 20 20 72 65 74 75 72 6e 20 28 28 42 69 74      return ((Bit
27a67 6d 61 73 6b 29 31 29 3c 3c 69 3b 0a 20 20 20 20  mask)1)<<i;.    
27a68 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  }.  }.  return 0
27a69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  ;.}../*.** Creat
27a6a 65 20 61 20 6e 65 77 20 6d 61 73 6b 20 66 6f 72  e a new mask for
27a6b 20 63 75 72 73 6f 72 20 69 43 75 72 73 6f 72 2e   cursor iCursor.
27a6c 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20  .**.** There is 
27a6d 6f 6e 65 20 63 75 72 73 6f 72 20 70 65 72 20 74  one cursor per t
27a6e 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d  able in the FROM
27a6f 20 63 6c 61 75 73 65 2e 20 20 54 68 65 20 6e 75   clause.  The nu
27a70 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65  mber of.** table
27a71 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  s in the FROM cl
27a72 61 75 73 65 20 69 73 20 6c 69 6d 69 74 65 64 20  ause is limited 
27a73 62 79 20 61 20 74 65 73 74 20 65 61 72 6c 79 20  by a test early 
27a74 69 6e 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65  in the.** sqlite
27a75 33 57 68 65 72 65 42 65 67 69 6e 28 29 20 72 6f  3WhereBegin() ro
27a76 75 74 69 6e 65 2e 20 20 53 6f 20 77 65 20 6b 6e  utine.  So we kn
27a77 6f 77 20 74 68 61 74 20 74 68 65 20 70 4d 61 73  ow that the pMas
27a78 6b 53 65 74 2d 3e 69 78 5b 5d 0a 2a 2a 20 61 72  kSet->ix[].** ar
27a79 72 61 79 20 77 69 6c 6c 20 6e 65 76 65 72 20 6f  ray will never o
27a7a 76 65 72 66 6c 6f 77 2e 0a 2a 2f 0a 73 74 61 74  verflow..*/.stat
27a7b 69 63 20 76 6f 69 64 20 63 72 65 61 74 65 4d 61  ic void createMa
27a7c 73 6b 28 45 78 70 72 4d 61 73 6b 53 65 74 20 2a  sk(ExprMaskSet *
27a7d 70 4d 61 73 6b 53 65 74 2c 20 69 6e 74 20 69 43  pMaskSet, int iC
27a7e 75 72 73 6f 72 29 7b 0a 20 20 61 73 73 65 72 74  ursor){.  assert
27a7f 28 20 70 4d 61 73 6b 53 65 74 2d 3e 6e 20 3c 20  ( pMaskSet->n < 
27a80 41 72 72 61 79 53 69 7a 65 28 70 4d 61 73 6b 53  ArraySize(pMaskS
27a81 65 74 2d 3e 69 78 29 20 29 3b 0a 20 20 70 4d 61  et->ix) );.  pMa
27a82 73 6b 53 65 74 2d 3e 69 78 5b 70 4d 61 73 6b 53  skSet->ix[pMaskS
27a83 65 74 2d 3e 6e 2b 2b 5d 20 3d 20 69 43 75 72 73  et->n++] = iCurs
27a84 6f 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  or;.}../*.** Thi
27a85 73 20 72 6f 75 74 69 6e 65 20 77 61 6c 6b 73 20  s routine walks 
27a86 28 72 65 63 75 72 73 69 76 65 6c 79 29 20 61 6e  (recursively) an
27a87 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65   expression tree
27a88 20 61 6e 64 20 67 65 6e 65 72 61 74 65 73 0a 2a   and generates.*
27a89 2a 20 61 20 62 69 74 6d 61 73 6b 20 69 6e 64 69  * a bitmask indi
27a8a 63 61 74 69 6e 67 20 77 68 69 63 68 20 74 61 62  cating which tab
27a8b 6c 65 73 20 61 72 65 20 75 73 65 64 20 69 6e 20  les are used in 
27a8c 74 68 61 74 20 65 78 70 72 65 73 73 69 6f 6e 0a  that expression.
27a8d 2a 2a 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49  ** tree..**.** I
27a8e 6e 20 6f 72 64 65 72 20 66 6f 72 20 74 68 69 73  n order for this
27a8f 20 72 6f 75 74 69 6e 65 20 74 6f 20 77 6f 72 6b   routine to work
27a90 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75  , the calling fu
27a91 6e 63 74 69 6f 6e 20 6d 75 73 74 20 68 61 76 65  nction must have
27a92 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 69  .** previously i
27a93 6e 76 6f 6b 65 64 20 73 71 6c 69 74 65 33 52 65  nvoked sqlite3Re
27a94 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29  solveExprNames()
27a95 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69   on the expressi
27a96 6f 6e 2e 20 20 53 65 65 0a 2a 2a 20 74 68 65 20  on.  See.** the 
27a97 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 6f  header comment o
27a98 6e 20 74 68 61 74 20 72 6f 75 74 69 6e 65 20 66  n that routine f
27a99 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
27a9a 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68  formation..** Th
27a9b 65 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  e sqlite3Resolve
27a9c 45 78 70 72 4e 61 6d 65 73 28 29 20 72 6f 75 74  ExprNames() rout
27a9d 69 6e 65 73 20 6c 6f 6f 6b 73 20 66 6f 72 20 63  ines looks for c
27a9e 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 6e 64 0a  olumn names and.
27a9f 2a 2a 20 73 65 74 73 20 74 68 65 69 72 20 6f 70  ** sets their op
27aa0 63 6f 64 65 73 20 74 6f 20 54 4b 5f 43 4f 4c 55  codes to TK_COLU
27aa1 4d 4e 20 61 6e 64 20 74 68 65 69 72 20 45 78 70  MN and their Exp
27aa2 72 2e 69 54 61 62 6c 65 20 66 69 65 6c 64 73 20  r.iTable fields 
27aa3 74 6f 0a 2a 2a 20 74 68 65 20 56 44 42 45 20 63  to.** the VDBE c
27aa4 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20  ursor number of 
27aa5 74 68 65 20 74 61 62 6c 65 2e 20 20 54 68 69 73  the table.  This
27aa6 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 20 68 61   routine just ha
27aa7 73 20 74 6f 0a 2a 2a 20 74 72 61 6e 73 6c 61 74  s to.** translat
27aa8 65 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d  e the cursor num
27aa9 62 65 72 73 20 69 6e 74 6f 20 62 69 74 6d 61 73  bers into bitmas
27aaa 6b 20 76 61 6c 75 65 73 20 61 6e 64 20 4f 52 20  k values and OR 
27aab 61 6c 6c 0a 2a 2a 20 74 68 65 20 62 69 74 6d 61  all.** the bitma
27aac 73 6b 73 20 74 6f 67 65 74 68 65 72 2e 0a 2a 2f  sks together..*/
27aad 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20  .static Bitmask 
27aae 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61  exprListTableUsa
27aaf 67 65 28 45 78 70 72 4d 61 73 6b 53 65 74 2a 2c  ge(ExprMaskSet*,
27ab0 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 73 74 61   ExprList*);.sta
27ab1 74 69 63 20 42 69 74 6d 61 73 6b 20 65 78 70 72  tic Bitmask expr
27ab2 53 65 6c 65 63 74 54 61 62 6c 65 55 73 61 67 65  SelectTableUsage
27ab3 28 45 78 70 72 4d 61 73 6b 53 65 74 2a 2c 20 53  (ExprMaskSet*, S
27ab4 65 6c 65 63 74 2a 29 3b 0a 73 74 61 74 69 63 20  elect*);.static 
27ab5 42 69 74 6d 61 73 6b 20 65 78 70 72 54 61 62 6c  Bitmask exprTabl
27ab6 65 55 73 61 67 65 28 45 78 70 72 4d 61 73 6b 53  eUsage(ExprMaskS
27ab7 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 45 78  et *pMaskSet, Ex
27ab8 70 72 20 2a 70 29 7b 0a 20 20 42 69 74 6d 61 73  pr *p){.  Bitmas
27ab9 6b 20 6d 61 73 6b 20 3d 20 30 3b 0a 20 20 69 66  k mask = 0;.  if
27aba 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( p==0 ) return 
27abb 30 3b 0a 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d  0;.  if( p->op==
27abc 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20  TK_COLUMN ){.   
27abd 20 6d 61 73 6b 20 3d 20 67 65 74 4d 61 73 6b 28   mask = getMask(
27abe 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 69 54 61  pMaskSet, p->iTa
27abf 62 6c 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ble);.    return
27ac0 20 6d 61 73 6b 3b 0a 20 20 7d 0a 20 20 6d 61 73   mask;.  }.  mas
27ac1 6b 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61  k = exprTableUsa
27ac2 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e  ge(pMaskSet, p->
27ac3 70 52 69 67 68 74 29 3b 0a 20 20 6d 61 73 6b 20  pRight);.  mask 
27ac4 7c 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67  |= exprTableUsag
27ac5 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 70  e(pMaskSet, p->p
27ac6 4c 65 66 74 29 3b 0a 20 20 6d 61 73 6b 20 7c 3d  Left);.  mask |=
27ac7 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73   exprListTableUs
27ac8 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d  age(pMaskSet, p-
27ac9 3e 70 4c 69 73 74 29 3b 0a 20 20 6d 61 73 6b 20  >pList);.  mask 
27aca 7c 3d 20 65 78 70 72 53 65 6c 65 63 74 54 61 62  |= exprSelectTab
27acb 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
27acc 2c 20 70 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20  , p->pSelect);. 
27acd 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a   return mask;.}.
27ace 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20 65  static Bitmask e
27acf 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67  xprListTableUsag
27ad0 65 28 45 78 70 72 4d 61 73 6b 53 65 74 20 2a 70  e(ExprMaskSet *p
27ad1 4d 61 73 6b 53 65 74 2c 20 45 78 70 72 4c 69 73  MaskSet, ExprLis
27ad2 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74  t *pList){.  int
27ad3 20 69 3b 0a 20 20 42 69 74 6d 61 73 6b 20 6d 61   i;.  Bitmask ma
27ad4 73 6b 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 4c  sk = 0;.  if( pL
27ad5 69 73 74 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  ist ){.    for(i
27ad6 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78  =0; i<pList->nEx
27ad7 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  pr; i++){.      
27ad8 6d 61 73 6b 20 7c 3d 20 65 78 70 72 54 61 62 6c  mask |= exprTabl
27ad9 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c  eUsage(pMaskSet,
27ada 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78   pList->a[i].pEx
27adb 70 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  pr);.    }.  }. 
27adc 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a   return mask;.}.
27add 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20 65  static Bitmask e
27ade 78 70 72 53 65 6c 65 63 74 54 61 62 6c 65 55 73  xprSelectTableUs
27adf 61 67 65 28 45 78 70 72 4d 61 73 6b 53 65 74 20  age(ExprMaskSet 
27ae0 2a 70 4d 61 73 6b 53 65 74 2c 20 53 65 6c 65 63  *pMaskSet, Selec
27ae1 74 20 2a 70 53 29 7b 0a 20 20 42 69 74 6d 61 73  t *pS){.  Bitmas
27ae2 6b 20 6d 61 73 6b 20 3d 20 30 3b 0a 20 20 77 68  k mask = 0;.  wh
27ae3 69 6c 65 28 20 70 53 20 29 7b 0a 20 20 20 20 6d  ile( pS ){.    m
27ae4 61 73 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74 54  ask |= exprListT
27ae5 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
27ae6 65 74 2c 20 70 53 2d 3e 70 45 4c 69 73 74 29 3b  et, pS->pEList);
27ae7 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70  .    mask |= exp
27ae8 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28  rListTableUsage(
27ae9 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70 47  pMaskSet, pS->pG
27aea 72 6f 75 70 42 79 29 3b 0a 20 20 20 20 6d 61 73  roupBy);.    mas
27aeb 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74 54 61 62  k |= exprListTab
27aec 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
27aed 2c 20 70 53 2d 3e 70 4f 72 64 65 72 42 79 29 3b  , pS->pOrderBy);
27aee 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70  .    mask |= exp
27aef 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73  rTableUsage(pMas
27af0 6b 53 65 74 2c 20 70 53 2d 3e 70 57 68 65 72 65  kSet, pS->pWhere
27af1 29 3b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65  );.    mask |= e
27af2 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d  xprTableUsage(pM
27af3 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70 48 61 76  askSet, pS->pHav
27af4 69 6e 67 29 3b 0a 20 20 20 20 70 53 20 3d 20 70  ing);.    pS = p
27af5 53 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20  S->pPrior;.  }. 
27af6 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a   return mask;.}.
27af7 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
27af8 55 45 20 69 66 20 74 68 65 20 67 69 76 65 6e 20  UE if the given 
27af9 6f 70 65 72 61 74 6f 72 20 69 73 20 6f 6e 65 20  operator is one 
27afa 6f 66 20 74 68 65 20 6f 70 65 72 61 74 6f 72 73  of the operators
27afb 20 74 68 61 74 20 69 73 0a 2a 2a 20 61 6c 6c 6f   that is.** allo
27afc 77 65 64 20 66 6f 72 20 61 6e 20 69 6e 64 65 78  wed for an index
27afd 61 62 6c 65 20 57 48 45 52 45 20 63 6c 61 75 73  able WHERE claus
27afe 65 20 74 65 72 6d 2e 20 20 54 68 65 20 61 6c 6c  e term.  The all
27aff 6f 77 65 64 20 6f 70 65 72 61 74 6f 72 73 20 61  owed operators a
27b00 72 65 0a 2a 2a 20 22 3d 22 2c 20 22 3c 22 2c 20  re.** "=", "<", 
27b01 22 3e 22 2c 20 22 3c 3d 22 2c 20 22 3e 3d 22 2c  ">", "<=", ">=",
27b02 20 61 6e 64 20 22 49 4e 22 2e 0a 2a 2f 0a 73 74   and "IN"..*/.st
27b03 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 77 65 64  atic int allowed
27b04 4f 70 28 69 6e 74 20 6f 70 29 7b 0a 20 20 61 73  Op(int op){.  as
27b05 73 65 72 74 28 20 54 4b 5f 47 54 3e 54 4b 5f 45  sert( TK_GT>TK_E
27b06 51 20 26 26 20 54 4b 5f 47 54 3c 54 4b 5f 47 45  Q && TK_GT<TK_GE
27b07 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 54 4b   );.  assert( TK
27b08 5f 4c 54 3e 54 4b 5f 45 51 20 26 26 20 54 4b 5f  _LT>TK_EQ && TK_
27b09 4c 54 3c 54 4b 5f 47 45 20 29 3b 0a 20 20 61 73  LT<TK_GE );.  as
27b0a 73 65 72 74 28 20 54 4b 5f 4c 45 3e 54 4b 5f 45  sert( TK_LE>TK_E
27b0b 51 20 26 26 20 54 4b 5f 4c 45 3c 54 4b 5f 47 45  Q && TK_LE<TK_GE
27b0c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 54 4b   );.  assert( TK
27b0d 5f 47 45 3d 3d 54 4b 5f 45 51 2b 34 20 29 3b 0a  _GE==TK_EQ+4 );.
27b0e 20 20 72 65 74 75 72 6e 20 6f 70 3d 3d 54 4b 5f    return op==TK_
27b0f 49 4e 20 7c 7c 20 28 6f 70 3e 3d 54 4b 5f 45 51  IN || (op>=TK_EQ
27b10 20 26 26 20 6f 70 3c 3d 54 4b 5f 47 45 29 20 7c   && op<=TK_GE) |
27b11 7c 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 3b  | op==TK_ISNULL;
27b12 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 77 61 70 20 74  .}../*.** Swap t
27b13 77 6f 20 6f 62 6a 65 63 74 73 20 6f 66 20 74 79  wo objects of ty
27b14 70 65 20 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  pe T..*/.#define
27b15 20 53 57 41 50 28 54 59 50 45 2c 41 2c 42 29 20   SWAP(TYPE,A,B) 
27b16 7b 54 59 50 45 20 74 3d 41 3b 20 41 3d 42 3b 20  {TYPE t=A; A=B; 
27b17 42 3d 74 3b 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d  B=t;}../*.** Com
27b18 6d 75 74 65 20 61 20 63 6f 6d 70 61 72 69 73 6f  mute a compariso
27b19 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20 45 78 70  n operator.  Exp
27b1a 72 65 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 20  ressions of the 
27b1b 66 6f 72 6d 20 22 58 20 6f 70 20 59 22 0a 2a 2a  form "X op Y".**
27b1c 20 61 72 65 20 63 6f 6e 76 65 72 74 65 64 20 69   are converted i
27b1d 6e 74 6f 20 22 59 20 6f 70 20 58 22 2e 0a 2a 2a  nto "Y op X"..**
27b1e 0a 2a 2a 20 49 66 20 61 20 63 6f 6c 6c 61 74 69  .** If a collati
27b1f 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 61  on sequence is a
27b20 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 65  ssociated with e
27b21 69 74 68 65 72 20 74 68 65 20 6c 65 66 74 20 6f  ither the left o
27b22 72 20 72 69 67 68 74 0a 2a 2a 20 73 69 64 65 20  r right.** side 
27b23 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  of the compariso
27b24 6e 2c 20 69 74 20 72 65 6d 61 69 6e 73 20 61 73  n, it remains as
27b25 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
27b26 65 20 73 61 6d 65 20 73 69 64 65 20 61 66 74 65  e same side afte
27b27 72 0a 2a 2a 20 74 68 65 20 63 6f 6d 6d 75 74 61  r.** the commuta
27b28 74 69 6f 6e 2e 20 53 6f 20 22 59 20 63 6f 6c 6c  tion. So "Y coll
27b29 61 74 65 20 4e 4f 43 41 53 45 20 6f 70 20 58 22  ate NOCASE op X"
27b2a 20 62 65 63 6f 6d 65 73 20 0a 2a 2a 20 22 58 20   becomes .** "X 
27b2b 63 6f 6c 6c 61 74 65 20 4e 4f 43 41 53 45 20 6f  collate NOCASE o
27b2c 70 20 59 22 2e 20 54 68 69 73 20 69 73 20 62 65  p Y". This is be
27b2d 63 61 75 73 65 20 61 6e 79 20 63 6f 6c 6c 61 74  cause any collat
27b2e 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6f 6e 0a  ion sequence on.
27b2f 2a 2a 20 74 68 65 20 6c 65 66 74 20 68 61 6e 64  ** the left hand
27b30 20 73 69 64 65 20 6f 66 20 61 20 63 6f 6d 70 61   side of a compa
27b31 72 69 73 6f 6e 20 6f 76 65 72 72 69 64 65 73 20  rison overrides 
27b32 61 6e 79 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  any collation se
27b33 71 75 65 6e 63 65 20 0a 2a 2a 20 61 74 74 61 63  quence .** attac
27b34 68 65 64 20 74 6f 20 74 68 65 20 72 69 67 68 74  hed to the right
27b35 2e 20 46 6f 72 20 74 68 65 20 73 61 6d 65 20 72  . For the same r
27b36 65 61 73 6f 6e 20 74 68 65 20 45 50 5f 45 78 70  eason the EP_Exp
27b37 43 6f 6c 6c 61 74 65 20 66 6c 61 67 0a 2a 2a 20  Collate flag.** 
27b38 69 73 20 6e 6f 74 20 63 6f 6d 6d 75 74 65 64 2e  is not commuted.
27b39 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
27b3a 65 78 70 72 43 6f 6d 6d 75 74 65 28 50 61 72 73  exprCommute(Pars
27b3b 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
27b3c 2a 70 45 78 70 72 29 7b 0a 20 20 75 31 36 20 65  *pExpr){.  u16 e
27b3d 78 70 52 69 67 68 74 20 3d 20 28 70 45 78 70 72  xpRight = (pExpr
27b3e 2d 3e 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20  ->pRight->flags 
27b3f 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 29  & EP_ExpCollate)
27b40 3b 0a 20 20 75 31 36 20 65 78 70 4c 65 66 74 20  ;.  u16 expLeft 
27b41 3d 20 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d  = (pExpr->pLeft-
27b42 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43  >flags & EP_ExpC
27b43 6f 6c 6c 61 74 65 29 3b 0a 20 20 61 73 73 65 72  ollate);.  asser
27b44 74 28 20 61 6c 6c 6f 77 65 64 4f 70 28 70 45 78  t( allowedOp(pEx
27b45 70 72 2d 3e 6f 70 29 20 26 26 20 70 45 78 70 72  pr->op) && pExpr
27b46 2d 3e 6f 70 21 3d 54 4b 5f 49 4e 20 29 3b 0a 20  ->op!=TK_IN );. 
27b47 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e   pExpr->pRight->
27b48 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45  pColl = sqlite3E
27b49 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  xprCollSeq(pPars
27b4a 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  e, pExpr->pRight
27b4b 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 4c 65 66  );.  pExpr->pLef
27b4c 74 2d 3e 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74  t->pColl = sqlit
27b4d 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50  e3ExprCollSeq(pP
27b4e 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
27b4f 66 74 29 3b 0a 20 20 53 57 41 50 28 43 6f 6c 6c  ft);.  SWAP(Coll
27b50 53 65 71 2a 2c 70 45 78 70 72 2d 3e 70 52 69 67  Seq*,pExpr->pRig
27b51 68 74 2d 3e 70 43 6f 6c 6c 2c 70 45 78 70 72 2d  ht->pColl,pExpr-
27b52 3e 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 29 3b 0a  >pLeft->pColl);.
27b53 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d    pExpr->pRight-
27b54 3e 66 6c 61 67 73 20 3d 20 28 70 45 78 70 72 2d  >flags = (pExpr-
27b55 3e 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26  >pRight->flags &
27b56 20 7e 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 29   ~EP_ExpCollate)
27b57 20 7c 20 65 78 70 4c 65 66 74 3b 0a 20 20 70 45   | expLeft;.  pE
27b58 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 66 6c 61 67  xpr->pLeft->flag
27b59 73 20 3d 20 28 70 45 78 70 72 2d 3e 70 4c 65 66  s = (pExpr->pLef
27b5a 74 2d 3e 66 6c 61 67 73 20 26 20 7e 45 50 5f 45  t->flags & ~EP_E
27b5b 78 70 43 6f 6c 6c 61 74 65 29 20 7c 20 65 78 70  xpCollate) | exp
27b5c 52 69 67 68 74 3b 0a 20 20 53 57 41 50 28 45 78  Right;.  SWAP(Ex
27b5d 70 72 2a 2c 70 45 78 70 72 2d 3e 70 52 69 67 68  pr*,pExpr->pRigh
27b5e 74 2c 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b  t,pExpr->pLeft);
27b5f 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70  .  if( pExpr->op
27b60 3e 3d 54 4b 5f 47 54 20 29 7b 0a 20 20 20 20 61  >=TK_GT ){.    a
27b61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 54 4b  ssert( TK_LT==TK
27b62 5f 47 54 2b 32 20 29 3b 0a 20 20 20 20 61 73 73  _GT+2 );.    ass
27b63 65 72 74 28 20 54 4b 5f 47 45 3d 3d 54 4b 5f 4c  ert( TK_GE==TK_L
27b64 45 2b 32 20 29 3b 0a 20 20 20 20 61 73 73 65 72  E+2 );.    asser
27b65 74 28 20 54 4b 5f 47 54 3e 54 4b 5f 45 51 20 29  t( TK_GT>TK_EQ )
27b66 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 54 4b  ;.    assert( TK
27b67 5f 47 54 3c 54 4b 5f 4c 45 20 29 3b 0a 20 20 20  _GT<TK_LE );.   
27b68 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
27b69 6f 70 3e 3d 54 4b 5f 47 54 20 26 26 20 70 45 78  op>=TK_GT && pEx
27b6a 70 72 2d 3e 6f 70 3c 3d 54 4b 5f 47 45 20 29 3b  pr->op<=TK_GE );
27b6b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d  .    pExpr->op =
27b6c 20 28 28 70 45 78 70 72 2d 3e 6f 70 2d 54 4b 5f   ((pExpr->op-TK_
27b6d 47 54 29 5e 32 29 2b 54 4b 5f 47 54 3b 0a 20 20  GT)^2)+TK_GT;.  
27b6e 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73  }.}../*.** Trans
27b6f 6c 61 74 65 20 66 72 6f 6d 20 54 4b 5f 78 78 20  late from TK_xx 
27b70 6f 70 65 72 61 74 6f 72 20 74 6f 20 57 4f 5f 78  operator to WO_x
27b71 78 20 62 69 74 6d 61 73 6b 2e 0a 2a 2f 0a 73 74  x bitmask..*/.st
27b72 61 74 69 63 20 69 6e 74 20 6f 70 65 72 61 74 6f  atic int operato
27b73 72 4d 61 73 6b 28 69 6e 74 20 6f 70 29 7b 0a 20  rMask(int op){. 
27b74 20 69 6e 74 20 63 3b 0a 20 20 61 73 73 65 72 74   int c;.  assert
27b75 28 20 61 6c 6c 6f 77 65 64 4f 70 28 6f 70 29 20  ( allowedOp(op) 
27b76 29 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f  );.  if( op==TK_
27b77 49 4e 20 29 7b 0a 20 20 20 20 63 20 3d 20 57 4f  IN ){.    c = WO
27b78 5f 49 4e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  _IN;.  }else if(
27b79 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29   op==TK_ISNULL )
27b7a 7b 0a 20 20 20 20 63 20 3d 20 57 4f 5f 49 53 4e  {.    c = WO_ISN
27b7b 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ULL;.  }else{.  
27b7c 20 20 63 20 3d 20 57 4f 5f 45 51 3c 3c 28 6f 70    c = WO_EQ<<(op
27b7d 2d 54 4b 5f 45 51 29 3b 0a 20 20 7d 0a 20 20 61  -TK_EQ);.  }.  a
27b7e 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 49 53  ssert( op!=TK_IS
27b7f 4e 55 4c 4c 20 7c 7c 20 63 3d 3d 57 4f 5f 49 53  NULL || c==WO_IS
27b80 4e 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65 72 74  NULL );.  assert
27b81 28 20 6f 70 21 3d 54 4b 5f 49 4e 20 7c 7c 20 63  ( op!=TK_IN || c
27b82 3d 3d 57 4f 5f 49 4e 20 29 3b 0a 20 20 61 73 73  ==WO_IN );.  ass
27b83 65 72 74 28 20 6f 70 21 3d 54 4b 5f 45 51 20 7c  ert( op!=TK_EQ |
27b84 7c 20 63 3d 3d 57 4f 5f 45 51 20 29 3b 0a 20 20  | c==WO_EQ );.  
27b85 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 4c  assert( op!=TK_L
27b86 54 20 7c 7c 20 63 3d 3d 57 4f 5f 4c 54 20 29 3b  T || c==WO_LT );
27b87 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54  .  assert( op!=T
27b88 4b 5f 4c 45 20 7c 7c 20 63 3d 3d 57 4f 5f 4c 45  K_LE || c==WO_LE
27b89 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70   );.  assert( op
27b8a 21 3d 54 4b 5f 47 54 20 7c 7c 20 63 3d 3d 57 4f  !=TK_GT || c==WO
27b8b 5f 47 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28  _GT );.  assert(
27b8c 20 6f 70 21 3d 54 4b 5f 47 45 20 7c 7c 20 63 3d   op!=TK_GE || c=
27b8d 3d 57 4f 5f 47 45 20 29 3b 0a 20 20 72 65 74 75  =WO_GE );.  retu
27b8e 72 6e 20 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  rn c;.}../*.** S
27b8f 65 61 72 63 68 20 66 6f 72 20 61 20 74 65 72 6d  earch for a term
27b90 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c   in the WHERE cl
27b91 61 75 73 65 20 74 68 61 74 20 69 73 20 6f 66 20  ause that is of 
27b92 74 68 65 20 66 6f 72 6d 20 22 58 20 3c 6f 70 3e  the form "X <op>
27b93 20 3c 65 78 70 72 3e 22 0a 2a 2a 20 77 68 65 72   <expr>".** wher
27b94 65 20 58 20 69 73 20 61 20 72 65 66 65 72 65 6e  e X is a referen
27b95 63 65 20 74 6f 20 74 68 65 20 69 43 6f 6c 75 6d  ce to the iColum
27b96 6e 20 6f 66 20 74 61 62 6c 65 20 69 43 75 72 20  n of table iCur 
27b97 61 6e 64 20 3c 6f 70 3e 20 69 73 20 6f 6e 65 20  and <op> is one 
27b98 6f 66 0a 2a 2a 20 74 68 65 20 57 4f 5f 78 78 20  of.** the WO_xx 
27b99 6f 70 65 72 61 74 6f 72 20 63 6f 64 65 73 20 73  operator codes s
27b9a 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20  pecified by the 
27b9b 6f 70 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a  op parameter..**
27b9c 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
27b9d 72 20 74 6f 20 74 68 65 20 74 65 72 6d 2e 20 20  r to the term.  
27b9e 52 65 74 75 72 6e 20 30 20 69 66 20 6e 6f 74 20  Return 0 if not 
27b9f 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  found..*/.static
27ba0 20 57 68 65 72 65 54 65 72 6d 20 2a 66 69 6e 64   WhereTerm *find
27ba1 54 65 72 6d 28 0a 20 20 57 68 65 72 65 43 6c 61  Term(.  WhereCla
27ba2 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20 2f 2a  use *pWC,     /*
27ba3 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
27ba4 65 20 74 6f 20 62 65 20 73 65 61 72 63 68 65 64  e to be searched
27ba5 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20   */.  int iCur, 
27ba6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
27ba7 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20  ursor number of 
27ba8 4c 48 53 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f  LHS */.  int iCo
27ba9 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20 2f  lumn,          /
27baa 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20  * Column number 
27bab 6f 66 20 4c 48 53 20 2a 2f 0a 20 20 42 69 74 6d  of LHS */.  Bitm
27bac 61 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20 20  ask notReady,   
27bad 20 20 2f 2a 20 52 48 53 20 6d 75 73 74 20 6e 6f    /* RHS must no
27bae 74 20 6f 76 65 72 6c 61 70 20 77 69 74 68 20 74  t overlap with t
27baf 68 69 73 20 6d 61 73 6b 20 2a 2f 0a 20 20 75 31  his mask */.  u1
27bb0 36 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20  6 op,           
27bb1 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 57      /* Mask of W
27bb2 4f 5f 78 78 20 76 61 6c 75 65 73 20 64 65 73 63  O_xx values desc
27bb3 72 69 62 69 6e 67 20 6f 70 65 72 61 74 6f 72 20  ribing operator 
27bb4 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  */.  Index *pIdx
27bb5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75             /* Mu
27bb6 73 74 20 62 65 20 63 6f 6d 70 61 74 69 62 6c 65  st be compatible
27bb7 20 77 69 74 68 20 74 68 69 73 20 69 6e 64 65 78   with this index
27bb8 2c 20 69 66 20 6e 6f 74 20 4e 55 4c 4c 20 2a 2f  , if not NULL */
27bb9 0a 29 7b 0a 20 20 57 68 65 72 65 54 65 72 6d 20  .){.  WhereTerm 
27bba 2a 70 54 65 72 6d 3b 0a 20 20 69 6e 74 20 6b 3b  *pTerm;.  int k;
27bbb 0a 20 20 61 73 73 65 72 74 28 20 69 43 75 72 3e  .  assert( iCur>
27bbc 3d 30 20 29 3b 0a 20 20 66 6f 72 28 70 54 65 72  =0 );.  for(pTer
27bbd 6d 3d 70 57 43 2d 3e 61 2c 20 6b 3d 70 57 43 2d  m=pWC->a, k=pWC-
27bbe 3e 6e 54 65 72 6d 3b 20 6b 3b 20 6b 2d 2d 2c 20  >nTerm; k; k--, 
27bbf 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 69 66  pTerm++){.    if
27bc0 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72  ( pTerm->leftCur
27bc1 73 6f 72 3d 3d 69 43 75 72 0a 20 20 20 20 20 20  sor==iCur.      
27bc2 20 26 26 20 28 70 54 65 72 6d 2d 3e 70 72 65 72   && (pTerm->prer
27bc3 65 71 52 69 67 68 74 20 26 20 6e 6f 74 52 65 61  eqRight & notRea
27bc4 64 79 29 3d 3d 30 0a 20 20 20 20 20 20 20 26 26  dy)==0.       &&
27bc5 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 6f 6c 75   pTerm->leftColu
27bc6 6d 6e 3d 3d 69 43 6f 6c 75 6d 6e 0a 20 20 20 20  mn==iColumn.    
27bc7 20 20 20 26 26 20 28 70 54 65 72 6d 2d 3e 65 4f     && (pTerm->eO
27bc8 70 65 72 61 74 6f 72 20 26 20 6f 70 29 21 3d 30  perator & op)!=0
27bc9 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66  .    ){.      if
27bca 28 20 70 49 64 78 20 26 26 20 70 54 65 72 6d 2d  ( pIdx && pTerm-
27bcb 3e 65 4f 70 65 72 61 74 6f 72 21 3d 57 4f 5f 49  >eOperator!=WO_I
27bcc 53 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20  SNULL ){.       
27bcd 20 45 78 70 72 20 2a 70 58 20 3d 20 70 54 65 72   Expr *pX = pTer
27bce 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  m->pExpr;.      
27bcf 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
27bd0 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 69  ;.        char i
27bd1 64 78 61 66 66 3b 0a 20 20 20 20 20 20 20 20 69  dxaff;.        i
27bd2 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 50 61  nt j;.        Pa
27bd3 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70 57  rse *pParse = pW
27bd4 43 2d 3e 70 50 61 72 73 65 3b 0a 0a 20 20 20 20  C->pParse;..    
27bd5 20 20 20 20 69 64 78 61 66 66 20 3d 20 70 49 64      idxaff = pId
27bd6 78 2d 3e 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 5b  x->pTable->aCol[
27bd7 69 43 6f 6c 75 6d 6e 5d 2e 61 66 66 69 6e 69 74  iColumn].affinit
27bd8 79 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  y;.        if( !
27bd9 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69  sqlite3IndexAffi
27bda 6e 69 74 79 4f 6b 28 70 58 2c 20 69 64 78 61 66  nityOk(pX, idxaf
27bdb 66 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a  f) ) continue;..
27bdc 20 20 20 20 20 20 20 20 2f 2a 20 46 69 67 75 72          /* Figur
27bdd 65 20 6f 75 74 20 74 68 65 20 63 6f 6c 6c 61 74  e out the collat
27bde 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 72 65 71  ion sequence req
27bdf 75 69 72 65 64 20 66 72 6f 6d 20 61 6e 20 69 6e  uired from an in
27be0 64 65 78 20 66 6f 72 0a 20 20 20 20 20 20 20 20  dex for.        
27be1 2a 2a 20 69 74 20 74 6f 20 62 65 20 75 73 65 66  ** it to be usef
27be2 75 6c 20 66 6f 72 20 6f 70 74 69 6d 69 73 69 6e  ul for optimisin
27be3 67 20 65 78 70 72 65 73 73 69 6f 6e 20 70 58 2e  g expression pX.
27be4 20 53 74 6f 72 65 20 74 68 69 73 0a 20 20 20 20   Store this.    
27be5 20 20 20 20 2a 2a 20 76 61 6c 75 65 20 69 6e 20      ** value in 
27be6 76 61 72 69 61 62 6c 65 20 70 43 6f 6c 6c 2e 0a  variable pColl..
27be7 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
27be8 20 20 20 61 73 73 65 72 74 28 70 58 2d 3e 70 4c     assert(pX->pL
27be9 65 66 74 29 3b 0a 20 20 20 20 20 20 20 20 70 43  eft);.        pC
27bea 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 42 69 6e  oll = sqlite3Bin
27beb 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65  aryCompareCollSe
27bec 71 28 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 4c  q(pParse, pX->pL
27bed 65 66 74 2c 20 70 58 2d 3e 70 52 69 67 68 74 29  eft, pX->pRight)
27bee 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70  ;.        if( !p
27bef 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  Coll ){.        
27bf0 20 20 70 43 6f 6c 6c 20 3d 20 70 50 61 72 73 65    pColl = pParse
27bf1 2d 3e 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b  ->db->pDfltColl;
27bf2 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  .        }..    
27bf3 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 70 49 64      for(j=0; pId
27bf4 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 21 3d  x->aiColumn[j]!=
27bf5 69 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20  iColumn; j++){. 
27bf6 20 20 20 20 20 20 20 20 20 69 66 28 20 4e 45 56           if( NEV
27bf7 45 52 28 6a 3e 3d 70 49 64 78 2d 3e 6e 43 6f 6c  ER(j>=pIdx->nCol
27bf8 75 6d 6e 29 20 29 20 72 65 74 75 72 6e 20 30 3b  umn) ) return 0;
27bf9 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
27bfa 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
27bfb 72 49 43 6d 70 28 70 43 6f 6c 6c 2d 3e 7a 4e 61  rICmp(pColl->zNa
27bfc 6d 65 2c 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c  me, pIdx->azColl
27bfd 5b 6a 5d 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b  [j]) ) continue;
27bfe 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
27bff 65 74 75 72 6e 20 70 54 65 72 6d 3b 0a 20 20 20  eturn pTerm;.   
27c00 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
27c01 30 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  0;.}../* Forward
27c02 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73 74   reference */.st
27c03 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 41 6e  atic void exprAn
27c04 61 6c 79 7a 65 28 53 72 63 4c 69 73 74 2a 2c 20  alyze(SrcList*, 
27c05 57 68 65 72 65 43 6c 61 75 73 65 2a 2c 20 69 6e  WhereClause*, in
27c06 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20  t);../*.** Call 
27c07 65 78 70 72 41 6e 61 6c 79 7a 65 20 6f 6e 20 61  exprAnalyze on a
27c08 6c 6c 20 74 65 72 6d 73 20 69 6e 20 61 20 57 48  ll terms in a WH
27c09 45 52 45 20 63 6c 61 75 73 65 2e 20 20 0a 2a 2a  ERE clause.  .**
27c0a 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  .**.*/.static vo
27c0b 69 64 20 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c  id exprAnalyzeAl
27c0c 6c 28 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54  l(.  SrcList *pT
27c0d 61 62 4c 69 73 74 2c 20 20 20 20 20 20 20 2f 2a  abList,       /*
27c0e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
27c0f 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73   */.  WhereClaus
27c10 65 20 2a 70 57 43 20 20 20 20 20 20 20 20 20 2f  e *pWC         /
27c11 2a 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  * the WHERE clau
27c12 73 65 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65  se to be analyze
27c13 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  d */.){.  int i;
27c14 0a 20 20 66 6f 72 28 69 3d 70 57 43 2d 3e 6e 54  .  for(i=pWC->nT
27c15 65 72 6d 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d  erm-1; i>=0; i--
27c16 29 7b 0a 20 20 20 20 65 78 70 72 41 6e 61 6c 79  ){.    exprAnaly
27c17 7a 65 28 70 54 61 62 4c 69 73 74 2c 20 70 57 43  ze(pTabList, pWC
27c18 2c 20 69 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66  , i);.  }.}..#if
27c19 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
27c1a 5f 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49  _LIKE_OPTIMIZATI
27c1b 4f 4e 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74  ON./*.** Check t
27c1c 6f 20 73 65 65 20 69 66 20 74 68 65 20 67 69 76  o see if the giv
27c1d 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  en expression is
27c1e 20 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20   a LIKE or GLOB 
27c1f 6f 70 65 72 61 74 6f 72 20 74 68 61 74 0a 2a 2a  operator that.**
27c20 20 63 61 6e 20 62 65 20 6f 70 74 69 6d 69 7a 65   can be optimize
27c21 64 20 75 73 69 6e 67 20 69 6e 65 71 75 61 6c 69  d using inequali
27c22 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20  ty constraints. 
27c23 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
27c24 69 74 20 69 73 0a 2a 2a 20 73 6f 20 61 6e 64 20  it is.** so and 
27c25 66 61 6c 73 65 20 69 66 20 6e 6f 74 2e 0a 2a 2a  false if not..**
27c26 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 66 6f 72  .** In order for
27c27 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74 6f   the operator to
27c28 20 62 65 20 6f 70 74 69 6d 69 7a 69 62 6c 65 2c   be optimizible,
27c29 20 74 68 65 20 52 48 53 20 6d 75 73 74 20 62 65   the RHS must be
27c2a 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 6c 69 74   a string.** lit
27c2b 65 72 61 6c 20 74 68 61 74 20 64 6f 65 73 20 6e  eral that does n
27c2c 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20 61 20  ot begin with a 
27c2d 77 69 6c 64 63 61 72 64 2e 20 20 0a 2a 2f 0a 73  wildcard.  .*/.s
27c2e 74 61 74 69 63 20 69 6e 74 20 69 73 4c 69 6b 65  tatic int isLike
27c2f 4f 72 47 6c 6f 62 28 0a 20 20 50 61 72 73 65 20  OrGlob(.  Parse 
27c30 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 50  *pParse,    /* P
27c31 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20  arsing and code 
27c32 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65  generating conte
27c33 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45  xt */.  Expr *pE
27c34 78 70 72 2c 20 20 20 20 20 20 2f 2a 20 54 65 73  xpr,      /* Tes
27c35 74 20 74 68 69 73 20 65 78 70 72 65 73 73 69 6f  t this expressio
27c36 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 50 61  n */.  int *pnPa
27c37 74 74 65 72 6e 2c 20 20 20 2f 2a 20 4e 75 6d 62  ttern,   /* Numb
27c38 65 72 20 6f 66 20 6e 6f 6e 2d 77 69 6c 64 63 61  er of non-wildca
27c39 72 64 20 70 72 65 66 69 78 20 63 68 61 72 61 63  rd prefix charac
27c3a 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  ters */.  int *p
27c3b 69 73 43 6f 6d 70 6c 65 74 65 2c 20 2f 2a 20 54  isComplete, /* T
27c3c 72 75 65 20 69 66 20 74 68 65 20 6f 6e 6c 79 20  rue if the only 
27c3d 77 69 6c 64 63 61 72 64 20 69 73 20 25 20 69 6e  wildcard is % in
27c3e 20 74 68 65 20 6c 61 73 74 20 63 68 61 72 61 63   the last charac
27c3f 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  ter */.  int *pn
27c40 6f 43 61 73 65 20 20 20 20 20 20 2f 2a 20 54 72  oCase      /* Tr
27c41 75 65 20 69 66 20 75 70 70 65 72 63 61 73 65 20  ue if uppercase 
27c42 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f  is equivalent to
27c43 20 6c 6f 77 65 72 63 61 73 65 20 2a 2f 0a 29 7b   lowercase */.){
27c44 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
27c45 3b 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74  ;.  Expr *pRight
27c46 2c 20 2a 70 4c 65 66 74 3b 0a 20 20 45 78 70 72  , *pLeft;.  Expr
27c47 4c 69 73 74 20 2a 70 4c 69 73 74 3b 0a 20 20 69  List *pList;.  i
27c48 6e 74 20 63 2c 20 63 6e 74 3b 0a 20 20 63 68 61  nt c, cnt;.  cha
27c49 72 20 77 63 5b 33 5d 3b 0a 20 20 43 6f 6c 6c 53  r wc[3];.  CollS
27c4a 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 73 71 6c  eq *pColl;.  sql
27c4b 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
27c4c 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 21 73  e->db;..  if( !s
27c4d 71 6c 69 74 65 33 49 73 4c 69 6b 65 46 75 6e 63  qlite3IsLikeFunc
27c4e 74 69 6f 6e 28 64 62 2c 20 70 45 78 70 72 2c 20  tion(db, pExpr, 
27c4f 70 6e 6f 43 61 73 65 2c 20 77 63 29 20 29 7b 0a  pnoCase, wc) ){.
27c50 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
27c51 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  }.#ifdef SQLITE_
27c52 45 42 43 44 49 43 0a 20 20 69 66 28 20 2a 70 6e  EBCDIC.  if( *pn
27c53 6f 43 61 73 65 20 29 20 72 65 74 75 72 6e 20 30  oCase ) return 0
27c54 3b 0a 23 65 6e 64 69 66 0a 20 20 70 4c 69 73 74  ;.#endif.  pList
27c55 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b   = pExpr->pList;
27c56 0a 20 20 70 52 69 67 68 74 20 3d 20 70 4c 69 73  .  pRight = pLis
27c57 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20  t->a[0].pExpr;. 
27c58 20 69 66 28 20 70 52 69 67 68 74 2d 3e 6f 70 21   if( pRight->op!
27c59 3d 54 4b 5f 53 54 52 49 4e 47 0a 20 20 20 26 26  =TK_STRING.   &&
27c5a 20 28 70 52 69 67 68 74 2d 3e 6f 70 21 3d 54 4b   (pRight->op!=TK
27c5b 5f 52 45 47 49 53 54 45 52 20 7c 7c 20 70 52 69  _REGISTER || pRi
27c5c 67 68 74 2d 3e 69 43 6f 6c 75 6d 6e 21 3d 54 4b  ght->iColumn!=TK
27c5d 5f 53 54 52 49 4e 47 29 20 29 7b 0a 20 20 20 20  _STRING) ){.    
27c5e 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
27c5f 70 4c 65 66 74 20 3d 20 70 4c 69 73 74 2d 3e 61  pLeft = pList->a
27c60 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 69 66 28  [1].pExpr;.  if(
27c61 20 70 4c 65 66 74 2d 3e 6f 70 21 3d 54 4b 5f 43   pLeft->op!=TK_C
27c62 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 72 65 74  OLUMN ){.    ret
27c63 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 43 6f  urn 0;.  }.  pCo
27c64 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ll = sqlite3Expr
27c65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
27c66 70 4c 65 66 74 29 3b 0a 20 20 61 73 73 65 72 74  pLeft);.  assert
27c67 28 20 70 43 6f 6c 6c 21 3d 30 20 7c 7c 20 70 4c  ( pColl!=0 || pL
27c68 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 2d 31  eft->iColumn==-1
27c69 20 29 3b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 3d   );.  if( pColl=
27c6a 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 20  =0 ){.    /* No 
27c6b 63 6f 6c 6c 61 74 69 6f 6e 20 69 73 20 64 65 66  collation is def
27c6c 69 6e 65 64 20 66 6f 72 20 74 68 65 20 52 4f 57  ined for the ROW
27c6d 49 44 2e 20 20 55 73 65 20 74 68 65 20 64 65 66  ID.  Use the def
27c6e 61 75 6c 74 2e 20 2a 2f 0a 20 20 20 20 70 43 6f  ault. */.    pCo
27c6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f  ll = db->pDfltCo
27c70 6c 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 70  ll;.  }.  if( (p
27c71 43 6f 6c 6c 2d 3e 74 79 70 65 21 3d 53 51 4c 49  Coll->type!=SQLI
27c72 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59 20 7c  TE_COLL_BINARY |
27c73 7c 20 2a 70 6e 6f 43 61 73 65 29 20 26 26 0a 20  | *pnoCase) &&. 
27c74 20 20 20 20 20 28 70 43 6f 6c 6c 2d 3e 74 79 70       (pColl->typ
27c75 65 21 3d 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e  e!=SQLITE_COLL_N
27c76 4f 43 41 53 45 20 7c 7c 20 21 2a 70 6e 6f 43 61  OCASE || !*pnoCa
27c77 73 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  se) ){.    retur
27c78 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  n 0;.  }.  sqlit
27c79 65 33 44 65 71 75 6f 74 65 45 78 70 72 28 64 62  e3DequoteExpr(db
27c7a 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 7a 20 3d  , pRight);.  z =
27c7b 20 28 63 68 61 72 20 2a 29 70 52 69 67 68 74 2d   (char *)pRight-
27c7c 3e 74 6f 6b 65 6e 2e 7a 3b 0a 20 20 63 6e 74 20  >token.z;.  cnt 
27c7d 3d 20 30 3b 0a 20 20 69 66 28 20 7a 20 29 7b 0a  = 0;.  if( z ){.
27c7e 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d 7a 5b      while( (c=z[
27c7f 63 6e 74 5d 29 21 3d 30 20 26 26 20 63 21 3d 77  cnt])!=0 && c!=w
27c80 63 5b 30 5d 20 26 26 20 63 21 3d 77 63 5b 31 5d  c[0] && c!=wc[1]
27c81 20 26 26 20 63 21 3d 77 63 5b 32 5d 20 29 7b 20   && c!=wc[2] ){ 
27c82 63 6e 74 2b 2b 3b 20 7d 0a 20 20 7d 0a 20 20 69  cnt++; }.  }.  i
27c83 66 28 20 63 6e 74 3d 3d 30 20 7c 7c 20 32 35 35  f( cnt==0 || 255
27c84 3d 3d 28 75 38 29 7a 5b 63 6e 74 5d 20 29 7b 0a  ==(u8)z[cnt] ){.
27c85 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
27c86 7d 0a 20 20 2a 70 69 73 43 6f 6d 70 6c 65 74 65  }.  *pisComplete
27c87 20 3d 20 7a 5b 63 6e 74 5d 3d 3d 77 63 5b 30 5d   = z[cnt]==wc[0]
27c88 20 26 26 20 7a 5b 63 6e 74 2b 31 5d 3d 3d 30 3b   && z[cnt+1]==0;
27c89 0a 20 20 2a 70 6e 50 61 74 74 65 72 6e 20 3d 20  .  *pnPattern = 
27c8a 63 6e 74 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  cnt;.  return 1;
27c8b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
27c8c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50  ITE_OMIT_LIKE_OP
27c8d 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 0a  TIMIZATION */...
27c8e 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
27c8f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
27c90 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
27c91 73 65 65 20 69 66 20 74 68 65 20 67 69 76 65 6e  see if the given
27c92 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6f   expression is o
27c93 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a  f the form.**.**
27c94 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 20           column 
27c95 4d 41 54 43 48 20 65 78 70 72 0a 2a 2a 0a 2a 2a  MATCH expr.**.**
27c96 20 49 66 20 69 74 20 69 73 20 74 68 65 6e 20 72   If it is then r
27c97 65 74 75 72 6e 20 54 52 55 45 2e 20 20 49 66 20  eturn TRUE.  If 
27c98 6e 6f 74 2c 20 72 65 74 75 72 6e 20 46 41 4c 53  not, return FALS
27c99 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  E..*/.static int
27c9a 20 69 73 4d 61 74 63 68 4f 66 43 6f 6c 75 6d 6e   isMatchOfColumn
27c9b 28 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20  (.  Expr *pExpr 
27c9c 20 20 20 20 20 2f 2a 20 54 65 73 74 20 74 68 69       /* Test thi
27c9d 73 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  s expression */.
27c9e 29 7b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  ){.  ExprList *p
27c9f 4c 69 73 74 3b 0a 0a 20 20 69 66 28 20 70 45 78  List;..  if( pEx
27ca0 70 72 2d 3e 6f 70 21 3d 54 4b 5f 46 55 4e 43 54  pr->op!=TK_FUNCT
27ca1 49 4f 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ION ){.    retur
27ca2 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  n 0;.  }.  if( p
27ca3 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 21 3d 35  Expr->token.n!=5
27ca4 20 7c 7c 0a 20 20 20 20 20 20 20 73 71 6c 69 74   ||.       sqlit
27ca5 65 33 53 74 72 4e 49 43 6d 70 28 28 63 6f 6e 73  e3StrNICmp((cons
27ca6 74 20 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74  t char*)pExpr->t
27ca7 6f 6b 65 6e 2e 7a 2c 22 6d 61 74 63 68 22 2c 35  oken.z,"match",5
27ca8 29 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  )!=0 ){.    retu
27ca9 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4c 69 73  rn 0;.  }.  pLis
27caa 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74  t = pExpr->pList
27cab 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 6e  ;.  if( pList->n
27cac 45 78 70 72 21 3d 32 20 29 7b 0a 20 20 20 20 72  Expr!=2 ){.    r
27cad 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69  eturn 0;.  }.  i
27cae 66 28 20 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70  f( pList->a[1].p
27caf 45 78 70 72 2d 3e 6f 70 20 21 3d 20 54 4b 5f 43  Expr->op != TK_C
27cb0 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 72 65 74  OLUMN ){.    ret
27cb1 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  urn 0;.  }.  ret
27cb2 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 20  urn 1;.}.#endif 
27cb3 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
27cb4 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a  IRTUALTABLE */..
27cb5 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 42 61  /*.** If the pBa
27cb6 73 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72  se expression or
27cb7 69 67 69 6e 61 74 65 64 20 69 6e 20 74 68 65 20  iginated in the 
27cb8 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75  ON or USING clau
27cb9 73 65 20 6f 66 0a 2a 2a 20 61 20 6a 6f 69 6e 2c  se of.** a join,
27cba 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20 74   then transfer t
27cbb 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 6d  he appropriate m
27cbc 61 72 6b 69 6e 67 73 20 6f 76 65 72 20 74 6f 20  arkings over to 
27cbd 64 65 72 69 76 65 64 2e 0a 2a 2f 0a 73 74 61 74  derived..*/.stat
27cbe 69 63 20 76 6f 69 64 20 74 72 61 6e 73 66 65 72  ic void transfer
27cbf 4a 6f 69 6e 4d 61 72 6b 69 6e 67 73 28 45 78 70  JoinMarkings(Exp
27cc0 72 20 2a 70 44 65 72 69 76 65 64 2c 20 45 78 70  r *pDerived, Exp
27cc1 72 20 2a 70 42 61 73 65 29 7b 0a 20 20 70 44 65  r *pBase){.  pDe
27cc2 72 69 76 65 64 2d 3e 66 6c 61 67 73 20 7c 3d 20  rived->flags |= 
27cc3 70 42 61 73 65 2d 3e 66 6c 61 67 73 20 26 20 45  pBase->flags & E
27cc4 50 5f 46 72 6f 6d 4a 6f 69 6e 3b 0a 20 20 70 44  P_FromJoin;.  pD
27cc5 65 72 69 76 65 64 2d 3e 69 52 69 67 68 74 4a 6f  erived->iRightJo
27cc6 69 6e 54 61 62 6c 65 20 3d 20 70 42 61 73 65 2d  inTable = pBase-
27cc7 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65  >iRightJoinTable
27cc8 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65  ;.}..#if !define
27cc9 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52  d(SQLITE_OMIT_OR
27cca 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 29 20 26  _OPTIMIZATION) &
27ccb 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
27ccc 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29  E_OMIT_SUBQUERY)
27ccd 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
27cce 55 45 20 69 66 20 74 68 65 20 67 69 76 65 6e 20  UE if the given 
27ccf 74 65 72 6d 20 6f 66 20 61 6e 20 4f 52 20 63 6c  term of an OR cl
27cd0 61 75 73 65 20 63 61 6e 20 62 65 20 63 6f 6e 76  ause can be conv
27cd1 65 72 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 6e  erted.** into an
27cd2 20 49 4e 20 63 6c 61 75 73 65 2e 20 20 54 68 65   IN clause.  The
27cd3 20 69 43 75 72 73 6f 72 20 61 6e 64 20 69 43 6f   iCursor and iCo
27cd4 6c 75 6d 6e 20 64 65 66 69 6e 65 20 74 68 65 20  lumn define the 
27cd5 6c 65 66 74 2d 68 61 6e 64 0a 2a 2a 20 73 69 64  left-hand.** sid
27cd6 65 20 6f 66 20 74 68 65 20 49 4e 20 63 6c 61 75  e of the IN clau
27cd7 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  se..**.** The co
27cd8 6e 74 65 78 74 20 69 73 20 74 68 61 74 20 77 65  ntext is that we
27cd9 20 68 61 76 65 20 6d 75 6c 74 69 70 6c 65 20 4f   have multiple O
27cda 52 2d 63 6f 6e 6e 65 63 74 65 64 20 65 71 75 61  R-connected equa
27cdb 6c 69 74 79 20 74 65 72 6d 73 0a 2a 2a 20 6c 69  lity terms.** li
27cdc 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  ke this:.**.**  
27cdd 20 20 20 20 20 20 20 20 20 61 3d 3c 65 78 70 72           a=<expr
27cde 31 3e 20 4f 52 20 20 61 3d 3c 65 78 70 72 32 3e  1> OR  a=<expr2>
27cdf 20 4f 52 20 62 3d 3c 65 78 70 72 33 3e 20 20 4f   OR b=<expr3>  O
27ce0 52 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  R ....**.** The 
27ce1 70 4f 72 54 65 72 6d 20 69 6e 70 75 74 20 74 6f  pOrTerm input to
27ce2 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f   this routine co
27ce3 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 61 20 73  rresponds to a s
27ce4 69 6e 67 6c 65 20 74 65 72 6d 20 6f 66 0a 2a 2a  ingle term of.**
27ce5 20 74 68 69 73 20 4f 52 20 63 6c 61 75 73 65 2e   this OR clause.
27ce6 20 20 49 6e 20 6f 72 64 65 72 20 66 6f 72 20 74    In order for t
27ce7 68 65 20 74 65 72 6d 20 74 6f 20 62 65 20 61 20  he term to be a 
27ce8 63 61 6e 64 69 64 61 74 65 20 66 6f 72 0a 2a 2a  candidate for.**
27ce9 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 6f 20 61   conversion to a
27cea 6e 20 49 4e 20 6f 70 65 72 61 74 6f 72 2c 20 74  n IN operator, t
27ceb 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 75 73  he following mus
27cec 74 20 62 65 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a  t be true:.**.**
27ced 20 20 20 20 20 2a 20 20 54 68 65 20 6c 65 66 74       *  The left
27cee 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68  -hand side of th
27cef 65 20 74 65 72 6d 20 6d 75 73 74 20 62 65 20 74  e term must be t
27cf0 68 65 20 63 6f 6c 75 6d 6e 20 77 68 69 63 68 0a  he column which.
27cf1 2a 2a 20 20 20 20 20 20 20 20 69 73 20 69 64 65  **        is ide
27cf2 6e 74 69 66 69 65 64 20 62 79 20 69 43 75 72 73  ntified by iCurs
27cf3 6f 72 20 61 6e 64 20 69 43 6f 6c 75 6d 6e 2e 0a  or and iColumn..
27cf4 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 20 49 66 20  **.**     *  If 
27cf5 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73  the right-hand s
27cf6 69 64 65 20 69 73 20 61 6c 73 6f 20 61 20 63 6f  ide is also a co
27cf7 6c 75 6d 6e 2c 20 74 68 65 6e 20 74 68 65 20 61  lumn, then the a
27cf8 66 66 69 6e 69 74 69 65 73 0a 2a 2a 20 20 20 20  ffinities.**    
27cf9 20 20 20 20 6f 66 20 62 6f 74 68 20 72 69 67 68      of both righ
27cfa 74 20 61 6e 64 20 6c 65 66 74 20 73 69 64 65 73  t and left sides
27cfb 20 6d 75 73 74 20 62 65 20 73 75 63 68 20 74 68   must be such th
27cfc 61 74 20 6e 6f 20 74 79 70 65 0a 2a 2a 20 20 20  at no type.**   
27cfd 20 20 20 20 20 63 6f 6e 76 65 72 73 69 6f 6e 73       conversions
27cfe 20 61 72 65 20 72 65 71 75 69 72 65 64 20 6f 6e   are required on
27cff 20 74 68 65 20 72 69 67 68 74 2e 20 20 28 54 69   the right.  (Ti
27d00 63 6b 65 74 20 23 32 32 34 39 29 0a 2a 2a 0a 2a  cket #2249).**.*
27d01 2a 20 49 66 20 62 6f 74 68 20 6f 66 20 74 68 65  * If both of the
27d02 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 72  se conditions ar
27d03 65 20 74 72 75 65 2c 20 74 68 65 6e 20 72 65 74  e true, then ret
27d04 75 72 6e 20 74 72 75 65 2e 20 20 4f 74 68 65 72  urn true.  Other
27d05 77 69 73 65 0a 2a 2a 20 72 65 74 75 72 6e 20 66  wise.** return f
27d06 61 6c 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  alse..*/.static 
27d07 69 6e 74 20 6f 72 54 65 72 6d 49 73 4f 70 74 43  int orTermIsOptC
27d08 61 6e 64 69 64 61 74 65 28 57 68 65 72 65 54 65  andidate(WhereTe
27d09 72 6d 20 2a 70 4f 72 54 65 72 6d 2c 20 69 6e 74  rm *pOrTerm, int
27d0a 20 69 43 75 72 73 6f 72 2c 20 69 6e 74 20 69 43   iCursor, int iC
27d0b 6f 6c 75 6d 6e 29 7b 0a 20 20 69 6e 74 20 61 66  olumn){.  int af
27d0c 66 4c 65 66 74 2c 20 61 66 66 52 69 67 68 74 3b  fLeft, affRight;
27d0d 0a 20 20 61 73 73 65 72 74 28 20 70 4f 72 54 65  .  assert( pOrTe
27d0e 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57  rm->eOperator==W
27d0f 4f 5f 45 51 20 29 3b 0a 20 20 69 66 28 20 70 4f  O_EQ );.  if( pO
27d10 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  rTerm->leftCurso
27d11 72 21 3d 69 43 75 72 73 6f 72 20 29 7b 0a 20 20  r!=iCursor ){.  
27d12 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
27d13 20 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 6c    if( pOrTerm->l
27d14 65 66 74 43 6f 6c 75 6d 6e 21 3d 69 43 6f 6c 75  eftColumn!=iColu
27d15 6d 6e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  mn ){.    return
27d16 20 30 3b 0a 20 20 7d 0a 20 20 61 66 66 52 69 67   0;.  }.  affRig
27d17 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ht = sqlite3Expr
27d18 41 66 66 69 6e 69 74 79 28 70 4f 72 54 65 72 6d  Affinity(pOrTerm
27d19 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29  ->pExpr->pRight)
27d1a 3b 0a 20 20 69 66 28 20 61 66 66 52 69 67 68 74  ;.  if( affRight
27d1b 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
27d1c 6e 20 31 3b 0a 20 20 7d 0a 20 20 61 66 66 4c 65  n 1;.  }.  affLe
27d1d 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ft = sqlite3Expr
27d1e 41 66 66 69 6e 69 74 79 28 70 4f 72 54 65 72 6d  Affinity(pOrTerm
27d1f 2d 3e 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b  ->pExpr->pLeft);
27d20 0a 20 20 69 66 28 20 61 66 66 52 69 67 68 74 21  .  if( affRight!
27d21 3d 61 66 66 4c 65 66 74 20 29 7b 0a 20 20 20 20  =affLeft ){.    
27d22 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
27d23 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a  return 1;.}../*.
27d24 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  ** Return true i
27d25 66 20 74 68 65 20 67 69 76 65 6e 20 74 65 72 6d  f the given term
27d26 20 6f 66 20 61 6e 20 4f 52 20 63 6c 61 75 73 65   of an OR clause
27d27 20 63 61 6e 20 62 65 20 69 67 6e 6f 72 65 64 20   can be ignored 
27d28 64 75 72 69 6e 67 0a 2a 2a 20 61 20 63 68 65 63  during.** a chec
27d29 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 61  k to make sure a
27d2a 6c 6c 20 4f 52 20 74 65 72 6d 73 20 61 72 65 20  ll OR terms are 
27d2b 63 61 6e 64 69 64 61 74 65 73 20 66 6f 72 20 6f  candidates for o
27d2c 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 0a 2a 2a 20  ptimization..** 
27d2d 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
27d2e 72 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 61  return true if a
27d2f 20 63 61 6c 6c 20 74 6f 20 74 68 65 20 6f 72 54   call to the orT
27d30 65 72 6d 49 73 4f 70 74 43 61 6e 64 69 64 61 74  ermIsOptCandidat
27d31 65 28 29 0a 2a 2a 20 61 62 6f 76 65 20 72 65 74  e().** above ret
27d32 75 72 6e 65 64 20 66 61 6c 73 65 20 62 75 74 20  urned false but 
27d33 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73  it is not necess
27d34 61 72 79 20 74 6f 20 64 69 73 71 75 61 6c 69 66  ary to disqualif
27d35 79 20 74 68 65 0a 2a 2a 20 6f 70 74 69 6d 69 7a  y the.** optimiz
27d36 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 75 70  ation..**.** Sup
27d37 70 6f 73 65 20 74 68 65 20 6f 72 69 67 69 6e 61  pose the origina
27d38 6c 20 4f 52 20 70 68 72 61 73 65 20 77 61 73 20  l OR phrase was 
27d39 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  this:.**.**     
27d3a 20 20 20 20 20 20 61 3d 34 20 20 4f 52 20 20 61        a=4  OR  a
27d3b 3d 31 31 20 20 4f 52 20 20 61 3d 62 0a 2a 2a 0a  =11  OR  a=b.**.
27d3c 2a 2a 20 44 75 72 69 6e 67 20 61 6e 61 6c 79 73  ** During analys
27d3d 69 73 2c 20 74 68 65 20 74 68 69 72 64 20 74 65  is, the third te
27d3e 72 6d 20 67 65 74 73 20 66 6c 69 70 70 65 64 20  rm gets flipped 
27d3f 61 72 6f 75 6e 64 20 61 6e 64 20 64 75 70 6c 69  around and dupli
27d40 63 61 74 65 0a 2a 2a 20 73 6f 20 74 68 61 74 20  cate.** so that 
27d41 77 65 20 61 72 65 20 6c 65 66 74 20 77 69 74 68  we are left with
27d42 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   this:.**.**    
27d43 20 20 20 20 20 20 20 61 3d 34 20 20 4f 52 20 20         a=4  OR  
27d44 61 3d 31 31 20 20 4f 52 20 20 61 3d 62 20 20 4f  a=11  OR  a=b  O
27d45 52 20 20 62 3d 61 0a 2a 2a 0a 2a 2a 20 53 69 6e  R  b=a.**.** Sin
27d46 63 65 20 74 68 65 20 6c 61 73 74 20 74 77 6f 20  ce the last two 
27d47 74 65 72 6d 73 20 61 72 65 20 64 75 70 6c 69 63  terms are duplic
27d48 61 74 65 73 2c 20 6f 6e 6c 79 20 6f 6e 65 20 6f  ates, only one o
27d49 66 20 74 68 65 6d 0a 2a 2a 20 68 61 73 20 74 6f  f them.** has to
27d4a 20 71 75 61 6c 69 66 79 20 69 6e 20 6f 72 64 65   qualify in orde
27d4b 72 20 66 6f 72 20 74 68 65 20 77 68 6f 6c 65 20  r for the whole 
27d4c 70 68 72 61 73 65 20 74 6f 20 71 75 61 6c 69 66  phrase to qualif
27d4d 79 2e 20 20 57 68 65 6e 0a 2a 2a 20 74 68 69 73  y.  When.** this
27d4e 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
27d4f 65 64 2c 20 77 65 20 6b 6e 6f 77 20 74 68 61 74  ed, we know that
27d50 20 70 4f 72 54 65 72 6d 20 64 69 64 20 6e 6f 74   pOrTerm did not
27d51 20 71 75 61 6c 69 66 79 2e 0a 2a 2a 20 54 68 69   qualify..** Thi
27d52 73 20 72 6f 75 74 69 6e 65 20 6d 65 72 65 6c 79  s routine merely
27d53 20 63 68 65 63 6b 73 20 74 6f 20 73 65 65 20 69   checks to see i
27d54 66 20 70 4f 72 54 65 72 6d 20 68 61 73 20 61 20  f pOrTerm has a 
27d55 64 75 70 6c 69 63 61 74 65 20 74 68 61 74 0a 2a  duplicate that.*
27d56 2a 20 6d 69 67 68 74 20 71 75 61 6c 69 66 79 2e  * might qualify.
27d57 20 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20    If there is a 
27d58 64 75 70 6c 69 63 61 74 65 20 74 68 61 74 20 68  duplicate that h
27d59 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 0a  as not yet been.
27d5a 2a 2a 20 64 69 73 71 75 61 6c 69 66 69 65 64 2c  ** disqualified,
27d5b 20 74 68 65 6e 20 72 65 74 75 72 6e 20 74 72 75   then return tru
27d5c 65 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 65  e.  If there are
27d5d 20 6e 6f 20 64 75 70 6c 69 63 61 74 65 73 2c 20   no duplicates, 
27d5e 6f 72 0a 2a 2a 20 74 68 65 20 64 75 70 6c 69 63  or.** the duplic
27d5f 61 74 65 20 68 61 73 20 61 6c 73 6f 20 62 65 65  ate has also bee
27d60 6e 20 64 69 73 71 75 61 6c 69 66 69 65 64 2c 20  n disqualified, 
27d61 72 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2f  return false..*/
27d62 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 72 54 65  .static int orTe
27d63 72 6d 48 61 73 4f 6b 44 75 70 6c 69 63 61 74 65  rmHasOkDuplicate
27d64 28 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 4f  (WhereClause *pO
27d65 72 2c 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4f  r, WhereTerm *pO
27d66 72 54 65 72 6d 29 7b 0a 20 20 69 66 28 20 70 4f  rTerm){.  if( pO
27d67 72 54 65 72 6d 2d 3e 66 6c 61 67 73 20 26 20 54  rTerm->flags & T
27d68 45 52 4d 5f 43 4f 50 49 45 44 20 29 7b 0a 20 20  ERM_COPIED ){.  
27d69 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65    /* This is the
27d6a 20 6f 72 69 67 69 6e 61 6c 20 74 65 72 6d 2e 20   original term. 
27d6b 20 54 68 65 20 64 75 70 6c 69 63 61 74 65 20 69   The duplicate i
27d6c 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 68 61  s to the left ha
27d6d 64 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e 6f 74  d.    ** has not
27d6e 20 79 65 74 20 62 65 65 6e 20 61 6e 61 6c 79 7a   yet been analyz
27d6f 65 64 20 61 6e 64 20 74 68 75 73 20 68 61 73 20  ed and thus has 
27d70 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 64 69 73  not yet been dis
27d71 71 75 61 6c 69 66 69 65 64 2e 20 2a 2f 0a 20 20  qualified. */.  
27d72 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
27d73 20 20 69 66 28 20 28 70 4f 72 54 65 72 6d 2d 3e    if( (pOrTerm->
27d74 66 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 49 52  flags & TERM_VIR
27d75 54 55 41 4c 29 21 3d 30 0a 20 20 20 20 20 26 26  TUAL)!=0.     &&
27d76 20 28 70 4f 72 2d 3e 61 5b 70 4f 72 54 65 72 6d   (pOr->a[pOrTerm
27d77 2d 3e 69 50 61 72 65 6e 74 5d 2e 66 6c 61 67 73  ->iParent].flags
27d78 20 26 20 54 45 52 4d 5f 4f 52 5f 4f 4b 29 21 3d   & TERM_OR_OK)!=
27d79 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73  0 ){.    /* This
27d7a 20 69 73 20 61 20 64 75 70 6c 69 63 61 74 65 20   is a duplicate 
27d7b 74 65 72 6d 2e 20 20 54 68 65 20 6f 72 69 67 69  term.  The origi
27d7c 6e 61 6c 20 71 75 61 6c 69 66 69 65 64 20 73 6f  nal qualified so
27d7d 20 74 68 69 73 20 6f 6e 65 0a 20 20 20 20 2a 2a   this one.    **
27d7e 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74   does not have t
27d7f 6f 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  o. */.    return
27d80 20 31 3b 0a 20 20 7d 0a 20 20 2f 2a 20 54 68 69   1;.  }.  /* Thi
27d81 73 20 69 73 20 65 69 74 68 65 72 20 61 20 73 69  s is either a si
27d82 6e 67 6c 65 74 6f 6e 20 74 65 72 6d 20 6f 72 20  ngleton term or 
27d83 65 6c 73 65 20 69 74 20 69 73 20 61 20 64 75 70  else it is a dup
27d84 6c 69 63 61 74 65 20 66 6f 72 0a 20 20 2a 2a 20  licate for.  ** 
27d85 77 68 69 63 68 20 74 68 65 20 6f 72 69 67 69 6e  which the origin
27d86 61 6c 20 64 69 64 20 6e 6f 74 20 71 75 61 6c 69  al did not quali
27d87 66 79 2e 20 20 45 69 74 68 65 72 20 77 61 79 20  fy.  Either way 
27d88 77 65 20 61 72 65 20 64 6f 6e 65 20 66 6f 72 2e  we are done for.
27d89 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a   */.  return 0;.
27d8a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c  }.#endif /* !SQL
27d8b 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49  ITE_OMIT_OR_OPTI
27d8c 4d 49 5a 41 54 49 4f 4e 20 26 26 20 21 53 51 4c  MIZATION && !SQL
27d8d 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
27d8e 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  Y */../*.** The 
27d8f 69 6e 70 75 74 20 74 6f 20 74 68 69 73 20 72 6f  input to this ro
27d90 75 74 69 6e 65 20 69 73 20 61 6e 20 57 68 65 72  utine is an Wher
27d91 65 54 65 72 6d 20 73 74 72 75 63 74 75 72 65 20  eTerm structure 
27d92 77 69 74 68 20 6f 6e 6c 79 20 74 68 65 0a 2a 2a  with only the.**
27d93 20 22 70 45 78 70 72 22 20 66 69 65 6c 64 20 66   "pExpr" field f
27d94 69 6c 6c 65 64 20 69 6e 2e 20 20 54 68 65 20 6a  illed in.  The j
27d95 6f 62 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69  ob of this routi
27d96 6e 65 20 69 73 20 74 6f 20 61 6e 61 6c 79 7a 65  ne is to analyze
27d97 20 74 68 65 0a 2a 2a 20 73 75 62 65 78 70 72 65   the.** subexpre
27d98 73 73 69 6f 6e 20 61 6e 64 20 70 6f 70 75 6c 61  ssion and popula
27d99 74 65 20 61 6c 6c 20 74 68 65 20 6f 74 68 65 72  te all the other
27d9a 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 57   fields of the W
27d9b 68 65 72 65 54 65 72 6d 0a 2a 2a 20 73 74 72 75  hereTerm.** stru
27d9c 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  cture..**.** If 
27d9d 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
27d9e 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3c  s of the form "<
27d9f 65 78 70 72 3e 20 3c 6f 70 3e 20 58 22 20 69 74  expr> <op> X" it
27da0 20 67 65 74 73 20 63 6f 6d 6d 75 74 65 64 0a 2a   gets commuted.*
27da1 2a 20 74 6f 20 74 68 65 20 73 74 61 6e 64 61 72  * to the standar
27da2 64 20 66 6f 72 6d 20 6f 66 20 22 58 20 3c 6f 70  d form of "X <op
27da3 3e 20 3c 65 78 70 72 3e 22 2e 20 20 49 66 20 74  > <expr>".  If t
27da4 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
27da5 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 72 6d 20   of.** the form 
27da6 22 58 20 3c 6f 70 3e 20 59 22 20 77 68 65 72 65  "X <op> Y" where
27da7 20 62 6f 74 68 20 58 20 61 6e 64 20 59 20 61 72   both X and Y ar
27da8 65 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20  e columns, then 
27da9 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20  the original.** 
27daa 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 75 6e  expression is un
27dab 63 68 61 6e 67 65 64 20 61 6e 64 20 61 20 6e 65  changed and a ne
27dac 77 20 76 69 72 74 75 61 6c 20 65 78 70 72 65 73  w virtual expres
27dad 73 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d  sion of the form
27dae 0a 2a 2a 20 22 59 20 3c 6f 70 3e 20 58 22 20 69  .** "Y <op> X" i
27daf 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 57  s added to the W
27db0 48 45 52 45 20 63 6c 61 75 73 65 20 61 6e 64 20  HERE clause and 
27db1 61 6e 61 6c 79 7a 65 64 20 73 65 70 61 72 61 74  analyzed separat
27db2 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ely..*/.static v
27db3 6f 69 64 20 65 78 70 72 41 6e 61 6c 79 7a 65 28  oid exprAnalyze(
27db4 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63  .  SrcList *pSrc
27db5 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
27db6 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
27db7 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65  */.  WhereClause
27db8 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20 20 2f   *pWC,         /
27db9 2a 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  * the WHERE clau
27dba 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 54  se */.  int idxT
27dbb 65 72 6d 20 20 20 20 20 20 20 20 20 20 20 20 20  erm             
27dbc 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68    /* Index of th
27dbd 65 20 74 65 72 6d 20 74 6f 20 62 65 20 61 6e 61  e term to be ana
27dbe 6c 79 7a 65 64 20 2a 2f 0a 29 7b 0a 20 20 57 68  lyzed */.){.  Wh
27dbf 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0a  ereTerm *pTerm;.
27dc0 20 20 45 78 70 72 4d 61 73 6b 53 65 74 20 2a 70    ExprMaskSet *p
27dc1 4d 61 73 6b 53 65 74 3b 0a 20 20 45 78 70 72 20  MaskSet;.  Expr 
27dc2 2a 70 45 78 70 72 3b 0a 20 20 42 69 74 6d 61 73  *pExpr;.  Bitmas
27dc3 6b 20 70 72 65 72 65 71 4c 65 66 74 3b 0a 20 20  k prereqLeft;.  
27dc4 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71 41 6c  Bitmask prereqAl
27dc5 6c 3b 0a 20 20 42 69 74 6d 61 73 6b 20 65 78 74  l;.  Bitmask ext
27dc6 72 61 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 69  raRight = 0;.  i
27dc7 6e 74 20 6e 50 61 74 74 65 72 6e 3b 0a 20 20 69  nt nPattern;.  i
27dc8 6e 74 20 69 73 43 6f 6d 70 6c 65 74 65 3b 0a 20  nt isComplete;. 
27dc9 20 69 6e 74 20 6e 6f 43 61 73 65 3b 0a 20 20 69   int noCase;.  i
27dca 6e 74 20 6f 70 3b 0a 20 20 50 61 72 73 65 20 2a  nt op;.  Parse *
27dcb 70 50 61 72 73 65 20 3d 20 70 57 43 2d 3e 70 50  pParse = pWC->pP
27dcc 61 72 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 20  arse;.  sqlite3 
27dcd 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
27dce 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  ;..  if( db->mal
27dcf 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
27dd0 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70   return;.  }.  p
27dd1 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69  Term = &pWC->a[i
27dd2 64 78 54 65 72 6d 5d 3b 0a 20 20 70 4d 61 73 6b  dxTerm];.  pMask
27dd3 53 65 74 20 3d 20 70 57 43 2d 3e 70 4d 61 73 6b  Set = pWC->pMask
27dd4 53 65 74 3b 0a 20 20 70 45 78 70 72 20 3d 20 70  Set;.  pExpr = p
27dd5 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 70  Term->pExpr;.  p
27dd6 72 65 72 65 71 4c 65 66 74 20 3d 20 65 78 70 72  rereqLeft = expr
27dd7 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b  TableUsage(pMask
27dd8 53 65 74 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  Set, pExpr->pLef
27dd9 74 29 3b 0a 20 20 6f 70 20 3d 20 70 45 78 70 72  t);.  op = pExpr
27dda 2d 3e 6f 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d  ->op;.  if( op==
27ddb 54 4b 5f 49 4e 20 29 7b 0a 20 20 20 20 61 73 73  TK_IN ){.    ass
27ddc 65 72 74 28 20 70 45 78 70 72 2d 3e 70 52 69 67  ert( pExpr->pRig
27ddd 68 74 3d 3d 30 20 29 3b 0a 20 20 20 20 70 54 65  ht==0 );.    pTe
27dde 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20  rm->prereqRight 
27ddf 3d 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55  = exprListTableU
27de0 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70  sage(pMaskSet, p
27de1 45 78 70 72 2d 3e 70 4c 69 73 74 29 0a 20 20 20  Expr->pList).   
27de2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27de3 20 20 20 20 20 20 20 7c 20 65 78 70 72 53 65 6c         | exprSel
27de4 65 63 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d  ectTableUsage(pM
27de5 61 73 6b 53 65 74 2c 20 70 45 78 70 72 2d 3e 70  askSet, pExpr->p
27de6 53 65 6c 65 63 74 29 3b 0a 20 20 7d 65 6c 73 65  Select);.  }else
27de7 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55   if( op==TK_ISNU
27de8 4c 4c 20 29 7b 0a 20 20 20 20 70 54 65 72 6d 2d  LL ){.    pTerm-
27de9 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d 20 30  >prereqRight = 0
27dea 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
27deb 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68  Term->prereqRigh
27dec 74 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61  t = exprTableUsa
27ded 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78  ge(pMaskSet, pEx
27dee 70 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 7d  pr->pRight);.  }
27def 0a 20 20 70 72 65 72 65 71 41 6c 6c 20 3d 20 65  .  prereqAll = e
27df0 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d  xprTableUsage(pM
27df1 61 73 6b 53 65 74 2c 20 70 45 78 70 72 29 3b 0a  askSet, pExpr);.
27df2 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f    if( ExprHasPro
27df3 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
27df4 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 20  FromJoin) ){.   
27df5 20 42 69 74 6d 61 73 6b 20 78 20 3d 20 67 65 74   Bitmask x = get
27df6 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70  Mask(pMaskSet, p
27df7 45 78 70 72 2d 3e 69 52 69 67 68 74 4a 6f 69 6e  Expr->iRightJoin
27df8 54 61 62 6c 65 29 3b 0a 20 20 20 20 70 72 65 72  Table);.    prer
27df9 65 71 41 6c 6c 20 7c 3d 20 78 3b 0a 20 20 20 20  eqAll |= x;.    
27dfa 65 78 74 72 61 52 69 67 68 74 20 3d 20 78 2d 31  extraRight = x-1
27dfb 3b 20 20 2f 2a 20 4f 4e 20 63 6c 61 75 73 65 20  ;  /* ON clause 
27dfc 74 65 72 6d 73 20 6d 61 79 20 6e 6f 74 20 62 65  terms may not be
27dfd 20 75 73 65 64 20 77 69 74 68 20 61 6e 20 69 6e   used with an in
27dfe 64 65 78 0a 20 20 20 20 20 20 20 20 20 20 20 20  dex.            
27dff 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e             ** on
27e00 20 6c 65 66 74 20 74 61 62 6c 65 20 6f 66 20 61   left table of a
27e01 20 4c 45 46 54 20 4a 4f 49 4e 2e 20 20 54 69 63   LEFT JOIN.  Tic
27e02 6b 65 74 20 23 33 30 31 35 20 2a 2f 0a 20 20 7d  ket #3015 */.  }
27e03 0a 20 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71  .  pTerm->prereq
27e04 41 6c 6c 20 3d 20 70 72 65 72 65 71 41 6c 6c 3b  All = prereqAll;
27e05 0a 20 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75  .  pTerm->leftCu
27e06 72 73 6f 72 20 3d 20 2d 31 3b 0a 20 20 70 54 65  rsor = -1;.  pTe
27e07 72 6d 2d 3e 69 50 61 72 65 6e 74 20 3d 20 2d 31  rm->iParent = -1
27e08 3b 0a 20 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72  ;.  pTerm->eOper
27e09 61 74 6f 72 20 3d 20 30 3b 0a 20 20 69 66 28 20  ator = 0;.  if( 
27e0a 61 6c 6c 6f 77 65 64 4f 70 28 6f 70 29 20 26 26  allowedOp(op) &&
27e0b 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52   (pTerm->prereqR
27e0c 69 67 68 74 20 26 20 70 72 65 72 65 71 4c 65 66  ight & prereqLef
27e0d 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 45 78 70  t)==0 ){.    Exp
27e0e 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72  r *pLeft = pExpr
27e0f 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 45 78 70  ->pLeft;.    Exp
27e10 72 20 2a 70 52 69 67 68 74 20 3d 20 70 45 78 70  r *pRight = pExp
27e11 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 69  r->pRight;.    i
27e12 66 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b  f( pLeft->op==TK
27e13 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20  _COLUMN ){.     
27e14 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73   pTerm->leftCurs
27e15 6f 72 20 3d 20 70 4c 65 66 74 2d 3e 69 54 61 62  or = pLeft->iTab
27e16 6c 65 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d  le;.      pTerm-
27e17 3e 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c  >leftColumn = pL
27e18 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20  eft->iColumn;.  
27e19 20 20 20 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72      pTerm->eOper
27e1a 61 74 6f 72 20 3d 20 6f 70 65 72 61 74 6f 72 4d  ator = operatorM
27e1b 61 73 6b 28 6f 70 29 3b 0a 20 20 20 20 7d 0a 20  ask(op);.    }. 
27e1c 20 20 20 69 66 28 20 70 52 69 67 68 74 20 26 26     if( pRight &&
27e1d 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f   pRight->op==TK_
27e1e 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20  COLUMN ){.      
27e1f 57 68 65 72 65 54 65 72 6d 20 2a 70 4e 65 77 3b  WhereTerm *pNew;
27e20 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 44 75  .      Expr *pDu
27e21 70 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 65  p;.      if( pTe
27e22 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3e 3d  rm->leftCursor>=
27e23 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  0 ){.        int
27e24 20 69 64 78 4e 65 77 3b 0a 20 20 20 20 20 20 20   idxNew;.       
27e25 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45   pDup = sqlite3E
27e26 78 70 72 44 75 70 28 64 62 2c 20 70 45 78 70 72  xprDup(db, pExpr
27e27 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 64  );.        if( d
27e28 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
27e29 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
27e2a 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
27e2b 62 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 20 20  b, pDup);.      
27e2c 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
27e2d 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 64      }.        id
27e2e 78 4e 65 77 20 3d 20 77 68 65 72 65 43 6c 61 75  xNew = whereClau
27e2f 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70 44  seInsert(pWC, pD
27e30 75 70 2c 20 54 45 52 4d 5f 56 49 52 54 55 41 4c  up, TERM_VIRTUAL
27e31 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a  |TERM_DYNAMIC);.
27e32 20 20 20 20 20 20 20 20 69 66 28 20 69 64 78 4e          if( idxN
27e33 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  ew==0 ) return;.
27e34 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20 26          pNew = &
27e35 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 5d 3b 0a  pWC->a[idxNew];.
27e36 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 50          pNew->iP
27e37 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b  arent = idxTerm;
27e38 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d 20 3d  .        pTerm =
27e39 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d   &pWC->a[idxTerm
27e3a 5d 3b 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d  ];.        pTerm
27e3b 2d 3e 6e 43 68 69 6c 64 20 3d 20 31 3b 0a 20 20  ->nChild = 1;.  
27e3c 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 66 6c 61        pTerm->fla
27e3d 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f 50 49 45  gs |= TERM_COPIE
27e3e 44 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  D;.      }else{.
27e3f 20 20 20 20 20 20 20 20 70 44 75 70 20 3d 20 70          pDup = p
27e40 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 70 4e  Expr;.        pN
27e41 65 77 20 3d 20 70 54 65 72 6d 3b 0a 20 20 20 20  ew = pTerm;.    
27e42 20 20 7d 0a 20 20 20 20 20 20 65 78 70 72 43 6f    }.      exprCo
27e43 6d 6d 75 74 65 28 70 50 61 72 73 65 2c 20 70 44  mmute(pParse, pD
27e44 75 70 29 3b 0a 20 20 20 20 20 20 70 4c 65 66 74  up);.      pLeft
27e45 20 3d 20 70 44 75 70 2d 3e 70 4c 65 66 74 3b 0a   = pDup->pLeft;.
27e46 20 20 20 20 20 20 70 4e 65 77 2d 3e 6c 65 66 74        pNew->left
27e47 43 75 72 73 6f 72 20 3d 20 70 4c 65 66 74 2d 3e  Cursor = pLeft->
27e48 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 70 4e  iTable;.      pN
27e49 65 77 2d 3e 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d  ew->leftColumn =
27e4a 20 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b   pLeft->iColumn;
27e4b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 72 65  .      pNew->pre
27e4c 72 65 71 52 69 67 68 74 20 3d 20 70 72 65 72 65  reqRight = prere
27e4d 71 4c 65 66 74 3b 0a 20 20 20 20 20 20 70 4e 65  qLeft;.      pNe
27e4e 77 2d 3e 70 72 65 72 65 71 41 6c 6c 20 3d 20 70  w->prereqAll = p
27e4f 72 65 72 65 71 41 6c 6c 3b 0a 20 20 20 20 20 20  rereqAll;.      
27e50 70 4e 65 77 2d 3e 65 4f 70 65 72 61 74 6f 72 20  pNew->eOperator 
27e51 3d 20 6f 70 65 72 61 74 6f 72 4d 61 73 6b 28 70  = operatorMask(p
27e52 44 75 70 2d 3e 6f 70 29 3b 0a 20 20 20 20 7d 0a  Dup->op);.    }.
27e53 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
27e54 49 54 45 5f 4f 4d 49 54 5f 42 45 54 57 45 45 4e  ITE_OMIT_BETWEEN
27e55 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 20 20  _OPTIMIZATION.  
27e56 2f 2a 20 49 66 20 61 20 74 65 72 6d 20 69 73 20  /* If a term is 
27e57 74 68 65 20 42 45 54 57 45 45 4e 20 6f 70 65 72  the BETWEEN oper
27e58 61 74 6f 72 2c 20 63 72 65 61 74 65 20 74 77 6f  ator, create two
27e59 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74 65 72   new virtual ter
27e5a 6d 73 0a 20 20 2a 2a 20 74 68 61 74 20 64 65 66  ms.  ** that def
27e5b 69 6e 65 20 74 68 65 20 72 61 6e 67 65 20 74 68  ine the range th
27e5c 61 74 20 74 68 65 20 42 45 54 57 45 45 4e 20 69  at the BETWEEN i
27e5d 6d 70 6c 65 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a  mplements..  */.
27e5e 20 20 65 6c 73 65 20 69 66 28 20 70 45 78 70 72    else if( pExpr
27e5f 2d 3e 6f 70 3d 3d 54 4b 5f 42 45 54 57 45 45 4e  ->op==TK_BETWEEN
27e60 20 29 7b 0a 20 20 20 20 45 78 70 72 4c 69 73 74   ){.    ExprList
27e61 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 70 72 2d   *pList = pExpr-
27e62 3e 70 4c 69 73 74 3b 0a 20 20 20 20 69 6e 74 20  >pList;.    int 
27e63 69 3b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f  i;.    static co
27e64 6e 73 74 20 75 38 20 6f 70 73 5b 5d 20 3d 20 7b  nst u8 ops[] = {
27e65 54 4b 5f 47 45 2c 20 54 4b 5f 4c 45 7d 3b 0a 20  TK_GE, TK_LE};. 
27e66 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74     assert( pList
27e67 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  !=0 );.    asser
27e68 74 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3d  t( pList->nExpr=
27e69 3d 32 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  =2 );.    for(i=
27e6a 30 3b 20 69 3c 32 3b 20 69 2b 2b 29 7b 0a 20 20  0; i<2; i++){.  
27e6b 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77 45 78      Expr *pNewEx
27e6c 70 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 64  pr;.      int id
27e6d 78 4e 65 77 3b 0a 20 20 20 20 20 20 70 4e 65 77  xNew;.      pNew
27e6e 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78  Expr = sqlite3Ex
27e6f 70 72 28 64 62 2c 20 6f 70 73 5b 69 5d 2c 20 73  pr(db, ops[i], s
27e70 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
27e71 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 2c  , pExpr->pLeft),
27e72 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
27e73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
27e74 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
27e75 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78   pList->a[i].pEx
27e76 70 72 29 2c 20 30 29 3b 0a 20 20 20 20 20 20 69  pr), 0);.      i
27e77 64 78 4e 65 77 20 3d 20 77 68 65 72 65 43 6c 61  dxNew = whereCla
27e78 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70  useInsert(pWC, p
27e79 4e 65 77 45 78 70 72 2c 20 54 45 52 4d 5f 56 49  NewExpr, TERM_VI
27e7a 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d  RTUAL|TERM_DYNAM
27e7b 49 43 29 3b 0a 20 20 20 20 20 20 65 78 70 72 41  IC);.      exprA
27e7c 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20 70 57 43  nalyze(pSrc, pWC
27e7d 2c 20 69 64 78 4e 65 77 29 3b 0a 20 20 20 20 20  , idxNew);.     
27e7e 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61   pTerm = &pWC->a
27e7f 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20 20  [idxTerm];.     
27e80 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 5d 2e   pWC->a[idxNew].
27e81 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72  iParent = idxTer
27e82 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 65  m;.    }.    pTe
27e83 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 32 3b 0a  rm->nChild = 2;.
27e84 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51    }.#endif /* SQ
27e85 4c 49 54 45 5f 4f 4d 49 54 5f 42 45 54 57 45 45  LITE_OMIT_BETWEE
27e86 4e 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a  N_OPTIMIZATION *
27e87 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  /..#if !defined(
27e88 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f  SQLITE_OMIT_OR_O
27e89 50 54 49 4d 49 5a 41 54 49 4f 4e 29 20 26 26 20  PTIMIZATION) && 
27e8a 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
27e8b 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a 20  OMIT_SUBQUERY). 
27e8c 20 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 63   /* Attempt to c
27e8d 6f 6e 76 65 72 74 20 4f 52 2d 63 6f 6e 6e 65 63  onvert OR-connec
27e8e 74 65 64 20 74 65 72 6d 73 20 69 6e 74 6f 20 61  ted terms into a
27e8f 6e 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 73 6f  n IN operator so
27e90 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 65 79 20   that.  ** they 
27e91 63 61 6e 20 6d 61 6b 65 20 75 73 65 20 6f 66 20  can make use of 
27e92 69 6e 64 69 63 65 73 2e 20 20 45 78 61 6d 70 6c  indices.  Exampl
27e93 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  e:.  **.  **    
27e94 20 20 78 20 3d 20 65 78 70 72 31 20 20 4f 52 20    x = expr1  OR 
27e95 20 65 78 70 72 32 20 3d 20 78 20 20 4f 52 20 20   expr2 = x  OR  
27e96 78 20 3d 20 65 78 70 72 33 0a 20 20 2a 2a 0a 20  x = expr3.  **. 
27e97 20 2a 2a 20 69 73 20 63 6f 6e 76 65 72 74 65 64   ** is converted
27e98 20 69 6e 74 6f 0a 20 20 2a 2a 0a 20 20 2a 2a 20   into.  **.  ** 
27e99 20 20 20 20 20 78 20 49 4e 20 28 65 78 70 72 31       x IN (expr1
27e9a 2c 65 78 70 72 32 2c 65 78 70 72 33 29 0a 20 20  ,expr2,expr3).  
27e9b 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 6f 70 74  **.  ** This opt
27e9c 69 6d 69 7a 61 74 69 6f 6e 20 6d 75 73 74 20 62  imization must b
27e9d 65 20 6f 6d 69 74 74 65 64 20 69 66 20 4f 4d 49  e omitted if OMI
27e9e 54 5f 53 55 42 51 55 45 52 59 20 69 73 20 64 65  T_SUBQUERY is de
27e9f 66 69 6e 65 64 20 62 65 63 61 75 73 65 0a 20 20  fined because.  
27ea0 2a 2a 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20  ** the compiler 
27ea1 66 6f 72 20 74 68 65 20 74 68 65 20 49 4e 20 6f  for the the IN o
27ea2 70 65 72 61 74 6f 72 20 69 73 20 70 61 72 74 20  perator is part 
27ea3 6f 66 20 73 75 62 2d 71 75 65 72 69 65 73 2e 0a  of sub-queries..
27ea4 20 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 28 20    */.  else if( 
27ea5 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4f 52  pExpr->op==TK_OR
27ea6 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f 6b 3b 0a   ){.    int ok;.
27ea7 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20      int i, j;.  
27ea8 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 69    int iColumn, i
27ea9 43 75 72 73 6f 72 3b 0a 20 20 20 20 57 68 65 72  Cursor;.    Wher
27eaa 65 43 6c 61 75 73 65 20 73 4f 72 3b 0a 20 20 20  eClause sOr;.   
27eab 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4f 72 54   WhereTerm *pOrT
27eac 65 72 6d 3b 0a 0a 20 20 20 20 61 73 73 65 72 74  erm;..    assert
27ead 28 20 28 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20  ( (pTerm->flags 
27eae 26 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3d  & TERM_DYNAMIC)=
27eaf 3d 30 20 29 3b 0a 20 20 20 20 77 68 65 72 65 43  =0 );.    whereC
27eb0 6c 61 75 73 65 49 6e 69 74 28 26 73 4f 72 2c 20  lauseInit(&sOr, 
27eb1 70 57 43 2d 3e 70 50 61 72 73 65 2c 20 70 4d 61  pWC->pParse, pMa
27eb2 73 6b 53 65 74 29 3b 0a 20 20 20 20 77 68 65 72  skSet);.    wher
27eb3 65 53 70 6c 69 74 28 26 73 4f 72 2c 20 70 45 78  eSplit(&sOr, pEx
27eb4 70 72 2c 20 54 4b 5f 4f 52 29 3b 0a 20 20 20 20  pr, TK_OR);.    
27eb5 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c 6c 28 70  exprAnalyzeAll(p
27eb6 53 72 63 2c 20 26 73 4f 72 29 3b 0a 20 20 20 20  Src, &sOr);.    
27eb7 61 73 73 65 72 74 28 20 73 4f 72 2e 6e 54 65 72  assert( sOr.nTer
27eb8 6d 3e 3d 32 20 29 3b 0a 20 20 20 20 6a 20 3d 20  m>=2 );.    j = 
27eb9 30 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d  0;.    if( db->m
27eba 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f  allocFailed ) go
27ebb 74 6f 20 6f 72 5f 6e 6f 74 5f 70 6f 73 73 69 62  to or_not_possib
27ebc 6c 65 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20  le;.    do{.    
27ebd 20 20 61 73 73 65 72 74 28 20 6a 3c 73 4f 72 2e    assert( j<sOr.
27ebe 6e 54 65 72 6d 20 29 3b 0a 20 20 20 20 20 20 69  nTerm );.      i
27ebf 43 6f 6c 75 6d 6e 20 3d 20 73 4f 72 2e 61 5b 6a  Column = sOr.a[j
27ec0 5d 2e 6c 65 66 74 43 6f 6c 75 6d 6e 3b 0a 20 20  ].leftColumn;.  
27ec1 20 20 20 20 69 43 75 72 73 6f 72 20 3d 20 73 4f      iCursor = sO
27ec2 72 2e 61 5b 6a 5d 2e 6c 65 66 74 43 75 72 73 6f  r.a[j].leftCurso
27ec3 72 3b 0a 20 20 20 20 20 20 6f 6b 20 3d 20 69 43  r;.      ok = iC
27ec4 75 72 73 6f 72 3e 3d 30 3b 0a 20 20 20 20 20 20  ursor>=0;.      
27ec5 66 6f 72 28 69 3d 73 4f 72 2e 6e 54 65 72 6d 2d  for(i=sOr.nTerm-
27ec6 31 2c 20 70 4f 72 54 65 72 6d 3d 73 4f 72 2e 61  1, pOrTerm=sOr.a
27ec7 3b 20 69 3e 3d 30 20 26 26 20 6f 6b 3b 20 69 2d  ; i>=0 && ok; i-
27ec8 2d 2c 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20  -, pOrTerm++){. 
27ec9 20 20 20 20 20 20 20 69 66 28 20 70 4f 72 54 65         if( pOrTe
27eca 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 21 3d 57  rm->eOperator!=W
27ecb 4f 5f 45 51 20 29 7b 0a 20 20 20 20 20 20 20 20  O_EQ ){.        
27ecc 20 20 67 6f 74 6f 20 6f 72 5f 6e 6f 74 5f 70 6f    goto or_not_po
27ecd 73 73 69 62 6c 65 3b 0a 20 20 20 20 20 20 20 20  ssible;.        
27ece 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6f 72  }.        if( or
27ecf 54 65 72 6d 49 73 4f 70 74 43 61 6e 64 69 64 61  TermIsOptCandida
27ed0 74 65 28 70 4f 72 54 65 72 6d 2c 20 69 43 75 72  te(pOrTerm, iCur
27ed1 73 6f 72 2c 20 69 43 6f 6c 75 6d 6e 29 20 29 7b  sor, iColumn) ){
27ed2 0a 20 20 20 20 20 20 20 20 20 20 70 4f 72 54 65  .          pOrTe
27ed3 72 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 54 45 52  rm->flags |= TER
27ed4 4d 5f 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 20  M_OR_OK;.       
27ed5 20 7d 65 6c 73 65 20 69 66 28 20 6f 72 54 65 72   }else if( orTer
27ed6 6d 48 61 73 4f 6b 44 75 70 6c 69 63 61 74 65 28  mHasOkDuplicate(
27ed7 26 73 4f 72 2c 20 70 4f 72 54 65 72 6d 29 20 29  &sOr, pOrTerm) )
27ed8 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 72 54  {.          pOrT
27ed9 65 72 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 54  erm->flags &= ~T
27eda 45 52 4d 5f 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20  ERM_OR_OK;.     
27edb 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
27edc 20 20 20 20 6f 6b 20 3d 20 30 3b 0a 20 20 20 20      ok = 0;.    
27edd 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
27ede 20 20 7d 77 68 69 6c 65 28 20 21 6f 6b 20 26 26    }while( !ok &&
27edf 20 28 73 4f 72 2e 61 5b 6a 2b 2b 5d 2e 66 6c 61   (sOr.a[j++].fla
27ee0 67 73 20 26 20 54 45 52 4d 5f 43 4f 50 49 45 44  gs & TERM_COPIED
27ee1 29 21 3d 30 20 26 26 20 6a 3c 32 20 29 3b 0a 20  )!=0 && j<2 );. 
27ee2 20 20 20 69 66 28 20 6f 6b 20 29 7b 0a 20 20 20     if( ok ){.   
27ee3 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69     ExprList *pLi
27ee4 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 45 78  st = 0;.      Ex
27ee5 70 72 20 2a 70 4e 65 77 2c 20 2a 70 44 75 70 3b  pr *pNew, *pDup;
27ee6 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65  .      Expr *pLe
27ee7 66 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 66 6f  ft = 0;.      fo
27ee8 72 28 69 3d 73 4f 72 2e 6e 54 65 72 6d 2d 31 2c  r(i=sOr.nTerm-1,
27ee9 20 70 4f 72 54 65 72 6d 3d 73 4f 72 2e 61 3b 20   pOrTerm=sOr.a; 
27eea 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f 72 54 65  i>=0; i--, pOrTe
27eeb 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69  rm++){.        i
27eec 66 28 20 28 70 4f 72 54 65 72 6d 2d 3e 66 6c 61  f( (pOrTerm->fla
27eed 67 73 20 26 20 54 45 52 4d 5f 4f 52 5f 4f 4b 29  gs & TERM_OR_OK)
27eee 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==0 ) continue;.
27eef 20 20 20 20 20 20 20 20 70 44 75 70 20 3d 20 73          pDup = s
27ef0 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
27ef1 2c 20 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72  , pOrTerm->pExpr
27ef2 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20 20  ->pRight);.     
27ef3 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74     pList = sqlit
27ef4 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
27ef5 28 70 57 43 2d 3e 70 50 61 72 73 65 2c 20 70 4c  (pWC->pParse, pL
27ef6 69 73 74 2c 20 70 44 75 70 2c 20 30 29 3b 0a 20  ist, pDup, 0);. 
27ef7 20 20 20 20 20 20 20 70 4c 65 66 74 20 3d 20 70         pLeft = p
27ef8 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 70  OrTerm->pExpr->p
27ef9 4c 65 66 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Left;.      }.  
27efa 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 65 66      assert( pLef
27efb 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 44  t!=0 );.      pD
27efc 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  up = sqlite3Expr
27efd 44 75 70 28 64 62 2c 20 70 4c 65 66 74 29 3b 0a  Dup(db, pLeft);.
27efe 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c        pNew = sql
27eff 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f  ite3Expr(db, TK_
27f00 49 4e 2c 20 70 44 75 70 2c 20 30 2c 20 30 29 3b  IN, pDup, 0, 0);
27f01 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20  .      if( pNew 
27f02 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  ){.        int i
27f03 64 78 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 74  dxNew;.        t
27f04 72 61 6e 73 66 65 72 4a 6f 69 6e 4d 61 72 6b 69  ransferJoinMarki
27f05 6e 67 73 28 70 4e 65 77 2c 20 70 45 78 70 72 29  ngs(pNew, pExpr)
27f06 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e  ;.        pNew->
27f07 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a 20  pList = pList;. 
27f08 20 20 20 20 20 20 20 69 64 78 4e 65 77 20 3d 20         idxNew = 
27f09 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72  whereClauseInser
27f0a 74 28 70 57 43 2c 20 70 4e 65 77 2c 20 54 45 52  t(pWC, pNew, TER
27f0b 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44  M_VIRTUAL|TERM_D
27f0c 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 20  YNAMIC);.       
27f0d 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 70 53 72   exprAnalyze(pSr
27f0e 63 2c 20 70 57 43 2c 20 69 64 78 4e 65 77 29 3b  c, pWC, idxNew);
27f0f 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d 20 3d  .        pTerm =
27f10 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d   &pWC->a[idxTerm
27f11 5d 3b 0a 20 20 20 20 20 20 20 20 70 57 43 2d 3e  ];.        pWC->
27f12 61 5b 69 64 78 4e 65 77 5d 2e 69 50 61 72 65 6e  a[idxNew].iParen
27f13 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20  t = idxTerm;.   
27f14 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68 69       pTerm->nChi
27f15 6c 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65  ld = 1;.      }e
27f16 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
27f17 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
27f18 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20  te(db, pList);. 
27f19 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 6f 72 5f       }.    }.or_
27f1a 6e 6f 74 5f 70 6f 73 73 69 62 6c 65 3a 0a 20 20  not_possible:.  
27f1b 20 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65    whereClauseCle
27f1c 61 72 28 26 73 4f 72 29 3b 0a 20 20 7d 0a 23 65  ar(&sOr);.  }.#e
27f1d 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
27f1e 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54  MIT_OR_OPTIMIZAT
27f1f 49 4f 4e 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  ION */..#ifndef 
27f20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45  SQLITE_OMIT_LIKE
27f21 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 20 20  _OPTIMIZATION.  
27f22 2f 2a 20 41 64 64 20 63 6f 6e 73 74 72 61 69 6e  /* Add constrain
27f23 74 73 20 74 6f 20 72 65 64 75 63 65 20 74 68 65  ts to reduce the
27f24 20 73 65 61 72 63 68 20 73 70 61 63 65 20 6f 6e   search space on
27f25 20 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 0a   a LIKE or GLOB.
27f26 20 20 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 0a 20    ** operator.. 
27f27 20 2a 2a 0a 20 20 2a 2a 20 41 20 6c 69 6b 65 20   **.  ** A like 
27f28 70 61 74 74 65 72 6e 20 6f 66 20 74 68 65 20 66  pattern of the f
27f29 6f 72 6d 20 22 78 20 4c 49 4b 45 20 27 61 62 63  orm "x LIKE 'abc
27f2a 25 27 22 20 69 73 20 63 68 61 6e 67 65 64 20 69  %'" is changed i
27f2b 6e 74 6f 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a  nto constraints.
27f2c 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20    **.  **       
27f2d 20 20 20 78 3e 3d 27 61 62 63 27 20 41 4e 44 20     x>='abc' AND 
27f2e 78 3c 27 61 62 64 27 20 41 4e 44 20 78 20 4c 49  x<'abd' AND x LI
27f2f 4b 45 20 27 61 62 63 25 27 0a 20 20 2a 2a 0a 20  KE 'abc%'.  **. 
27f30 20 2a 2a 20 54 68 65 20 6c 61 73 74 20 63 68 61   ** The last cha
27f31 72 61 63 74 65 72 20 6f 66 20 74 68 65 20 70 72  racter of the pr
27f32 65 66 69 78 20 22 61 62 63 22 20 69 73 20 69 6e  efix "abc" is in
27f33 63 72 65 6d 65 6e 74 65 64 20 74 6f 20 66 6f 72  cremented to for
27f34 6d 20 74 68 65 0a 20 20 2a 2a 20 74 65 72 6d 69  m the.  ** termi
27f35 6e 61 74 69 6f 6e 20 63 6f 6e 64 69 74 69 6f 6e  nation condition
27f36 20 22 61 62 64 22 2e 0a 20 20 2a 2f 0a 20 20 69   "abd"..  */.  i
27f37 66 28 20 69 73 4c 69 6b 65 4f 72 47 6c 6f 62 28  f( isLikeOrGlob(
27f38 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26  pParse, pExpr, &
27f39 6e 50 61 74 74 65 72 6e 2c 20 26 69 73 43 6f 6d  nPattern, &isCom
27f3a 70 6c 65 74 65 2c 20 26 6e 6f 43 61 73 65 29 20  plete, &noCase) 
27f3b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 4c 65  ){.    Expr *pLe
27f3c 66 74 2c 20 2a 70 52 69 67 68 74 3b 0a 20 20 20  ft, *pRight;.   
27f3d 20 45 78 70 72 20 2a 70 53 74 72 31 2c 20 2a 70   Expr *pStr1, *p
27f3e 53 74 72 32 3b 0a 20 20 20 20 45 78 70 72 20 2a  Str2;.    Expr *
27f3f 70 4e 65 77 45 78 70 72 31 2c 20 2a 70 4e 65 77  pNewExpr1, *pNew
27f40 45 78 70 72 32 3b 0a 20 20 20 20 69 6e 74 20 69  Expr2;.    int i
27f41 64 78 4e 65 77 31 2c 20 69 64 78 4e 65 77 32 3b  dxNew1, idxNew2;
27f42 0a 0a 20 20 20 20 70 4c 65 66 74 20 3d 20 70 45  ..    pLeft = pE
27f43 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d  xpr->pList->a[1]
27f44 2e 70 45 78 70 72 3b 0a 20 20 20 20 70 52 69 67  .pExpr;.    pRig
27f45 68 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73  ht = pExpr->pLis
27f46 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20  t->a[0].pExpr;. 
27f47 20 20 20 70 53 74 72 31 20 3d 20 73 71 6c 69 74     pStr1 = sqlit
27f48 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
27f49 54 4b 5f 53 54 52 49 4e 47 2c 20 30 2c 20 30 2c  TK_STRING, 0, 0,
27f4a 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 53 74   0);.    if( pSt
27f4b 72 31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  r1 ){.      sqli
27f4c 74 65 33 54 6f 6b 65 6e 43 6f 70 79 28 64 62 2c  te3TokenCopy(db,
27f4d 20 26 70 53 74 72 31 2d 3e 74 6f 6b 65 6e 2c 20   &pStr1->token, 
27f4e 26 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 29 3b  &pRight->token);
27f4f 0a 20 20 20 20 20 20 70 53 74 72 31 2d 3e 74 6f  .      pStr1->to
27f50 6b 65 6e 2e 6e 20 3d 20 6e 50 61 74 74 65 72 6e  ken.n = nPattern
27f51 3b 0a 20 20 20 20 20 20 70 53 74 72 31 2d 3e 66  ;.      pStr1->f
27f52 6c 61 67 73 20 3d 20 45 50 5f 44 65 71 75 6f 74  lags = EP_Dequot
27f53 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53  ed;.    }.    pS
27f54 74 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70  tr2 = sqlite3Exp
27f55 72 44 75 70 28 64 62 2c 20 70 53 74 72 31 29 3b  rDup(db, pStr1);
27f56 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61  .    if( !db->ma
27f57 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
27f58 20 20 20 20 75 38 20 63 2c 20 2a 70 43 3b 0a 20      u8 c, *pC;. 
27f59 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53 74       assert( pSt
27f5a 72 32 2d 3e 74 6f 6b 65 6e 2e 64 79 6e 20 29 3b  r2->token.dyn );
27f5b 0a 20 20 20 20 20 20 70 43 20 3d 20 28 75 38 2a  .      pC = (u8*
27f5c 29 26 70 53 74 72 32 2d 3e 74 6f 6b 65 6e 2e 7a  )&pStr2->token.z
27f5d 5b 6e 50 61 74 74 65 72 6e 2d 31 5d 3b 0a 20 20  [nPattern-1];.  
27f5e 20 20 20 20 63 20 3d 20 2a 70 43 3b 0a 20 20 20      c = *pC;.   
27f5f 20 20 20 69 66 28 20 6e 6f 43 61 73 65 20 29 7b     if( noCase ){
27f60 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d  .        if( c==
27f61 27 40 27 20 29 20 69 73 43 6f 6d 70 6c 65 74 65  '@' ) isComplete
27f62 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 63 20   = 0;.        c 
27f63 3d 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f  = sqlite3UpperTo
27f64 4c 6f 77 65 72 5b 63 5d 3b 0a 20 20 20 20 20 20  Lower[c];.      
27f65 7d 0a 20 20 20 20 20 20 2a 70 43 20 3d 20 63 20  }.      *pC = c 
27f66 2b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  + 1;.    }.    p
27f67 4e 65 77 45 78 70 72 31 20 3d 20 73 71 6c 69 74  NewExpr1 = sqlit
27f68 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
27f69 54 4b 5f 47 45 2c 20 73 71 6c 69 74 65 33 45 78  TK_GE, sqlite3Ex
27f6a 70 72 44 75 70 28 64 62 2c 70 4c 65 66 74 29 2c  prDup(db,pLeft),
27f6b 20 70 53 74 72 31 2c 20 30 29 3b 0a 20 20 20 20   pStr1, 0);.    
27f6c 69 64 78 4e 65 77 31 20 3d 20 77 68 65 72 65 43  idxNew1 = whereC
27f6d 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c  lauseInsert(pWC,
27f6e 20 70 4e 65 77 45 78 70 72 31 2c 20 54 45 52 4d   pNewExpr1, TERM
27f6f 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59  _VIRTUAL|TERM_DY
27f70 4e 41 4d 49 43 29 3b 0a 20 20 20 20 65 78 70 72  NAMIC);.    expr
27f71 41 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20 70 57  Analyze(pSrc, pW
27f72 43 2c 20 69 64 78 4e 65 77 31 29 3b 0a 20 20 20  C, idxNew1);.   
27f73 20 70 4e 65 77 45 78 70 72 32 20 3d 20 73 71 6c   pNewExpr2 = sql
27f74 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
27f75 2c 20 54 4b 5f 4c 54 2c 20 73 71 6c 69 74 65 33  , TK_LT, sqlite3
27f76 45 78 70 72 44 75 70 28 64 62 2c 70 4c 65 66 74  ExprDup(db,pLeft
27f77 29 2c 20 70 53 74 72 32 2c 20 30 29 3b 0a 20 20  ), pStr2, 0);.  
27f78 20 20 69 64 78 4e 65 77 32 20 3d 20 77 68 65 72    idxNew2 = wher
27f79 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57  eClauseInsert(pW
27f7a 43 2c 20 70 4e 65 77 45 78 70 72 32 2c 20 54 45  C, pNewExpr2, TE
27f7b 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f  RM_VIRTUAL|TERM_
27f7c 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 65 78  DYNAMIC);.    ex
27f7d 70 72 41 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20  prAnalyze(pSrc, 
27f7e 70 57 43 2c 20 69 64 78 4e 65 77 32 29 3b 0a 20  pWC, idxNew2);. 
27f7f 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d     pTerm = &pWC-
27f80 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20  >a[idxTerm];.   
27f81 20 69 66 28 20 69 73 43 6f 6d 70 6c 65 74 65 20   if( isComplete 
27f82 29 7b 0a 20 20 20 20 20 20 70 57 43 2d 3e 61 5b  ){.      pWC->a[
27f83 69 64 78 4e 65 77 31 5d 2e 69 50 61 72 65 6e 74  idxNew1].iParent
27f84 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20   = idxTerm;.    
27f85 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 32    pWC->a[idxNew2
27f86 5d 2e 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54  ].iParent = idxT
27f87 65 72 6d 3b 0a 20 20 20 20 20 20 70 54 65 72 6d  erm;.      pTerm
27f88 2d 3e 6e 43 68 69 6c 64 20 3d 20 32 3b 0a 20 20  ->nChild = 2;.  
27f89 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f    }.  }.#endif /
27f8a 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49  * SQLITE_OMIT_LI
27f8b 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20  KE_OPTIMIZATION 
27f8c 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
27f8d 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
27f8e 41 42 4c 45 0a 20 20 2f 2a 20 41 64 64 20 61 20  ABLE.  /* Add a 
27f8f 57 4f 5f 4d 41 54 43 48 20 61 75 78 69 6c 69 61  WO_MATCH auxilia
27f90 72 79 20 74 65 72 6d 20 74 6f 20 74 68 65 20 63  ry term to the c
27f91 6f 6e 73 74 72 61 69 6e 74 20 73 65 74 20 69 66  onstraint set if
27f92 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 72 65 6e   the.  ** curren
27f93 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  t expression is 
27f94 6f 66 20 74 68 65 20 66 6f 72 6d 3a 20 20 63 6f  of the form:  co
27f95 6c 75 6d 6e 20 4d 41 54 43 48 20 65 78 70 72 2e  lumn MATCH expr.
27f96 0a 20 20 2a 2a 20 54 68 69 73 20 69 6e 66 6f 72  .  ** This infor
27f97 6d 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62  mation is used b
27f98 79 20 74 68 65 20 78 42 65 73 74 49 6e 64 65 78  y the xBestIndex
27f99 20 6d 65 74 68 6f 64 73 20 6f 66 0a 20 20 2a 2a   methods of.  **
27f9a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2e   virtual tables.
27f9b 20 20 54 68 65 20 6e 61 74 69 76 65 20 71 75 65    The native que
27f9c 72 79 20 6f 70 74 69 6d 69 7a 65 72 20 64 6f 65  ry optimizer doe
27f9d 73 20 6e 6f 74 20 61 74 74 65 6d 70 74 0a 20 20  s not attempt.  
27f9e 2a 2a 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e  ** to do anythin
27f9f 67 20 77 69 74 68 20 4d 41 54 43 48 20 66 75 6e  g with MATCH fun
27fa0 63 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69  ctions..  */.  i
27fa1 66 28 20 69 73 4d 61 74 63 68 4f 66 43 6f 6c 75  f( isMatchOfColu
27fa2 6d 6e 28 70 45 78 70 72 29 20 29 7b 0a 20 20 20  mn(pExpr) ){.   
27fa3 20 69 6e 74 20 69 64 78 4e 65 77 3b 0a 20 20 20   int idxNew;.   
27fa4 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20 2a   Expr *pRight, *
27fa5 70 4c 65 66 74 3b 0a 20 20 20 20 57 68 65 72 65  pLeft;.    Where
27fa6 54 65 72 6d 20 2a 70 4e 65 77 54 65 72 6d 3b 0a  Term *pNewTerm;.
27fa7 20 20 20 20 42 69 74 6d 61 73 6b 20 70 72 65 72      Bitmask prer
27fa8 65 71 43 6f 6c 75 6d 6e 2c 20 70 72 65 72 65 71  eqColumn, prereq
27fa9 45 78 70 72 3b 0a 0a 20 20 20 20 70 52 69 67 68  Expr;..    pRigh
27faa 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74  t = pExpr->pList
27fab 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[0].pExpr;.  
27fac 20 20 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d    pLeft = pExpr-
27fad 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78  >pList->a[1].pEx
27fae 70 72 3b 0a 20 20 20 20 70 72 65 72 65 71 45 78  pr;.    prereqEx
27faf 70 72 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73  pr = exprTableUs
27fb0 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 52  age(pMaskSet, pR
27fb1 69 67 68 74 29 3b 0a 20 20 20 20 70 72 65 72 65  ight);.    prere
27fb2 71 43 6f 6c 75 6d 6e 20 3d 20 65 78 70 72 54 61  qColumn = exprTa
27fb3 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65  bleUsage(pMaskSe
27fb4 74 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 69  t, pLeft);.    i
27fb5 66 28 20 28 70 72 65 72 65 71 45 78 70 72 20 26  f( (prereqExpr &
27fb6 20 70 72 65 72 65 71 43 6f 6c 75 6d 6e 29 3d 3d   prereqColumn)==
27fb7 30 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20  0 ){.      Expr 
27fb8 2a 70 4e 65 77 45 78 70 72 3b 0a 20 20 20 20 20  *pNewExpr;.     
27fb9 20 70 4e 65 77 45 78 70 72 20 3d 20 73 71 6c 69   pNewExpr = sqli
27fba 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 4d  te3Expr(db, TK_M
27fbb 41 54 43 48 2c 20 30 2c 20 73 71 6c 69 74 65 33  ATCH, 0, sqlite3
27fbc 45 78 70 72 44 75 70 28 64 62 2c 20 70 52 69 67  ExprDup(db, pRig
27fbd 68 74 29 2c 20 30 29 3b 0a 20 20 20 20 20 20 69  ht), 0);.      i
27fbe 64 78 4e 65 77 20 3d 20 77 68 65 72 65 43 6c 61  dxNew = whereCla
27fbf 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70  useInsert(pWC, p
27fc0 4e 65 77 45 78 70 72 2c 20 54 45 52 4d 5f 56 49  NewExpr, TERM_VI
27fc1 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d  RTUAL|TERM_DYNAM
27fc2 49 43 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 54  IC);.      pNewT
27fc3 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64  erm = &pWC->a[id
27fc4 78 4e 65 77 5d 3b 0a 20 20 20 20 20 20 70 4e 65  xNew];.      pNe
27fc5 77 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67  wTerm->prereqRig
27fc6 68 74 20 3d 20 70 72 65 72 65 71 45 78 70 72 3b  ht = prereqExpr;
27fc7 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d  .      pNewTerm-
27fc8 3e 6c 65 66 74 43 75 72 73 6f 72 20 3d 20 70 4c  >leftCursor = pL
27fc9 65 66 74 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20  eft->iTable;.   
27fca 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 6c 65 66     pNewTerm->lef
27fcb 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65 66 74 2d  tColumn = pLeft-
27fcc 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20  >iColumn;.      
27fcd 70 4e 65 77 54 65 72 6d 2d 3e 65 4f 70 65 72 61  pNewTerm->eOpera
27fce 74 6f 72 20 3d 20 57 4f 5f 4d 41 54 43 48 3b 0a  tor = WO_MATCH;.
27fcf 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e        pNewTerm->
27fd0 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72  iParent = idxTer
27fd1 6d 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 20 3d  m;.      pTerm =
27fd2 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d   &pWC->a[idxTerm
27fd3 5d 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e  ];.      pTerm->
27fd4 6e 43 68 69 6c 64 20 3d 20 31 3b 0a 20 20 20 20  nChild = 1;.    
27fd5 20 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20 7c    pTerm->flags |
27fd6 3d 20 54 45 52 4d 5f 43 4f 50 49 45 44 3b 0a 20  = TERM_COPIED;. 
27fd7 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 70       pNewTerm->p
27fd8 72 65 72 65 71 41 6c 6c 20 3d 20 70 54 65 72 6d  rereqAll = pTerm
27fd9 2d 3e 70 72 65 72 65 71 41 6c 6c 3b 0a 20 20 20  ->prereqAll;.   
27fda 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a   }.  }.#endif /*
27fdb 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
27fdc 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 20 20  TUALTABLE */..  
27fdd 2f 2a 20 50 72 65 76 65 6e 74 20 4f 4e 20 63 6c  /* Prevent ON cl
27fde 61 75 73 65 20 74 65 72 6d 73 20 6f 66 20 61 20  ause terms of a 
27fdf 4c 45 46 54 20 4a 4f 49 4e 20 66 72 6f 6d 20 62  LEFT JOIN from b
27fe0 65 69 6e 67 20 75 73 65 64 20 74 6f 20 64 72 69  eing used to dri
27fe1 76 65 0a 20 20 2a 2a 20 61 6e 20 69 6e 64 65 78  ve.  ** an index
27fe2 20 66 6f 72 20 74 61 62 6c 65 73 20 74 6f 20 74   for tables to t
27fe3 68 65 20 6c 65 66 74 20 6f 66 20 74 68 65 20 6a  he left of the j
27fe4 6f 69 6e 2e 0a 20 20 2a 2f 0a 20 20 70 54 65 72  oin..  */.  pTer
27fe5 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 7c  m->prereqRight |
27fe6 3d 20 65 78 74 72 61 52 69 67 68 74 3b 0a 7d 0a  = extraRight;.}.
27fe7 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
27fe8 55 45 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65  UE if any of the
27fe9 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20   expressions in 
27fea 70 4c 69 73 74 2d 3e 61 5b 69 46 69 72 73 74 2e  pList->a[iFirst.
27feb 2e 2e 5d 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 61  ..] contain.** a
27fec 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 6e   reference to an
27fed 79 20 74 61 62 6c 65 20 6f 74 68 65 72 20 74 68  y table other th
27fee 61 6e 20 74 68 65 20 69 42 61 73 65 20 74 61 62  an the iBase tab
27fef 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
27ff0 74 20 72 65 66 65 72 65 6e 63 65 73 4f 74 68 65  t referencesOthe
27ff1 72 54 61 62 6c 65 73 28 0a 20 20 45 78 70 72 4c  rTables(.  ExprL
27ff2 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20  ist *pList,     
27ff3 20 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20 65       /* Search e
27ff4 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68  xpressions in th
27ff5 73 20 6c 69 73 74 20 2a 2f 0a 20 20 45 78 70 72  s list */.  Expr
27ff6 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65  MaskSet *pMaskSe
27ff7 74 2c 20 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67  t,    /* Mapping
27ff8 20 66 72 6f 6d 20 74 61 62 6c 65 73 20 74 6f 20   from tables to 
27ff9 62 69 74 6d 61 70 73 20 2a 2f 0a 20 20 69 6e 74  bitmaps */.  int
27ffa 20 69 46 69 72 73 74 2c 20 20 20 20 20 20 20 20   iFirst,        
27ffb 20 20 20 20 20 20 20 2f 2a 20 42 65 20 73 65 61         /* Be sea
27ffc 72 63 68 69 6e 67 20 77 69 74 68 20 74 68 65 20  rching with the 
27ffd 69 46 69 72 73 74 2d 74 68 20 65 78 70 72 65 73  iFirst-th expres
27ffe 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 42  sion */.  int iB
27fff 61 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ase             
28000 20 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 72 65      /* Ignore re
28001 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 69 73  ferences to this
28002 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 42   table */.){.  B
28003 69 74 6d 61 73 6b 20 61 6c 6c 6f 77 65 64 20 3d  itmask allowed =
28004 20 7e 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53   ~getMask(pMaskS
28005 65 74 2c 20 69 42 61 73 65 29 3b 0a 20 20 77 68  et, iBase);.  wh
28006 69 6c 65 28 20 69 46 69 72 73 74 3c 70 4c 69 73  ile( iFirst<pLis
28007 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20  t->nExpr ){.    
28008 69 66 28 20 28 65 78 70 72 54 61 62 6c 65 55 73  if( (exprTableUs
28009 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 4c  age(pMaskSet, pL
2800a 69 73 74 2d 3e 61 5b 69 46 69 72 73 74 2b 2b 5d  ist->a[iFirst++]
2800b 2e 70 45 78 70 72 29 26 61 6c 6c 6f 77 65 64 29  .pExpr)&allowed)
2800c 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74  !=0 ){.      ret
2800d 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 1;.    }.  }
2800e 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
2800f 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
28010 69 6e 65 20 64 65 63 69 64 65 73 20 69 66 20 70  ine decides if p
28011 49 64 78 20 63 61 6e 20 62 65 20 75 73 65 64 20  Idx can be used 
28012 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 4f  to satisfy the O
28013 52 44 45 52 20 42 59 0a 2a 2a 20 63 6c 61 75 73  RDER BY.** claus
28014 65 2e 20 20 49 66 20 69 74 20 63 61 6e 2c 20 69  e.  If it can, i
28015 74 20 72 65 74 75 72 6e 73 20 31 2e 20 20 49 66  t returns 1.  If
28016 20 70 49 64 78 20 63 61 6e 6e 6f 74 20 73 61 74   pIdx cannot sat
28017 69 73 66 79 20 74 68 65 0a 2a 2a 20 4f 52 44 45  isfy the.** ORDE
28018 52 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68 69  R BY clause, thi
28019 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
2801a 73 20 30 2e 0a 2a 2a 0a 2a 2a 20 70 4f 72 64 65  s 0..**.** pOrde
2801b 72 42 79 20 69 73 20 61 6e 20 4f 52 44 45 52 20  rBy is an ORDER 
2801c 42 59 20 63 6c 61 75 73 65 20 66 72 6f 6d 20 61  BY clause from a
2801d 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2801e 74 2e 20 20 70 54 61 62 20 69 73 20 74 68 65 0a  t.  pTab is the.
2801f 2a 2a 20 6c 65 66 74 2d 6d 6f 73 74 20 74 61 62  ** left-most tab
28020 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  le in the FROM c
28021 6c 61 75 73 65 20 6f 66 20 74 68 61 74 20 73 61  lause of that sa
28022 6d 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  me SELECT statem
28023 65 6e 74 20 61 6e 64 0a 2a 2a 20 74 68 65 20 74  ent and.** the t
28024 61 62 6c 65 20 68 61 73 20 61 20 63 75 72 73 6f  able has a curso
28025 72 20 6e 75 6d 62 65 72 20 6f 66 20 22 62 61 73  r number of "bas
28026 65 22 2e 20 20 70 49 64 78 20 69 73 20 61 6e 20  e".  pIdx is an 
28027 69 6e 64 65 78 20 6f 6e 20 70 54 61 62 2e 0a 2a  index on pTab..*
28028 2a 0a 2a 2a 20 6e 45 71 43 6f 6c 20 69 73 20 74  *.** nEqCol is t
28029 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
2802a 75 6d 6e 73 20 6f 66 20 70 49 64 78 20 74 68 61  umns of pIdx tha
2802b 74 20 61 72 65 20 75 73 65 64 20 61 73 20 65 71  t are used as eq
2802c 75 61 6c 69 74 79 0a 2a 2a 20 63 6f 6e 73 74 72  uality.** constr
2802d 61 69 6e 74 73 2e 20 20 41 6e 79 20 6f 66 20 74  aints.  Any of t
2802e 68 65 73 65 20 63 6f 6c 75 6d 6e 73 20 6d 61 79  hese columns may
2802f 20 62 65 20 6d 69 73 73 69 6e 67 20 66 72 6f 6d   be missing from
28030 20 74 68 65 20 4f 52 44 45 52 20 42 59 0a 2a 2a   the ORDER BY.**
28031 20 63 6c 61 75 73 65 20 61 6e 64 20 74 68 65 20   clause and the 
28032 6d 61 74 63 68 20 63 61 6e 20 73 74 69 6c 6c 20  match can still 
28033 62 65 20 61 20 73 75 63 63 65 73 73 2e 0a 2a 2a  be a success..**
28034 0a 2a 2a 20 41 6c 6c 20 74 65 72 6d 73 20 6f 66  .** All terms of
28035 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 74 68   the ORDER BY th
28036 61 74 20 6d 61 74 63 68 20 61 67 61 69 6e 73 74  at match against
28037 20 74 68 65 20 69 6e 64 65 78 20 6d 75 73 74 20   the index must 
28038 62 65 20 65 69 74 68 65 72 0a 2a 2a 20 41 53 43  be either.** ASC
28039 20 6f 72 20 44 45 53 43 2e 20 20 28 54 65 72 6d   or DESC.  (Term
2803a 73 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42  s of the ORDER B
2803b 59 20 63 6c 61 75 73 65 20 70 61 73 74 20 74 68  Y clause past th
2803c 65 20 65 6e 64 20 6f 66 20 61 20 55 4e 49 51 55  e end of a UNIQU
2803d 45 0a 2a 2a 20 69 6e 64 65 78 20 64 6f 20 6e 6f  E.** index do no
2803e 74 20 6e 65 65 64 20 74 6f 20 73 61 74 69 73 66  t need to satisf
2803f 79 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e  y this constrain
28040 74 2e 29 20 20 54 68 65 20 2a 70 62 52 65 76 20  t.)  The *pbRev 
28041 76 61 6c 75 65 20 69 73 0a 2a 2a 20 73 65 74 20  value is.** set 
28042 74 6f 20 31 20 69 66 20 74 68 65 20 4f 52 44 45  to 1 if the ORDE
28043 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 61  R BY clause is a
28044 6c 6c 20 44 45 53 43 20 61 6e 64 20 69 74 20 69  ll DESC and it i
28045 73 20 73 65 74 20 74 6f 20 30 20 69 66 0a 2a 2a  s set to 0 if.**
28046 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
28047 61 75 73 65 20 69 73 20 61 6c 6c 20 41 53 43 2e  ause is all ASC.
28048 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
28049 73 53 6f 72 74 69 6e 67 49 6e 64 65 78 28 0a 20  sSortingIndex(. 
2804a 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2804b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
2804c 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
2804d 20 45 78 70 72 4d 61 73 6b 53 65 74 20 2a 70 4d   ExprMaskSet *pM
2804e 61 73 6b 53 65 74 2c 20 20 2f 2a 20 4d 61 70 70  askSet,  /* Mapp
2804f 69 6e 67 20 66 72 6f 6d 20 74 61 62 6c 65 20 69  ing from table i
28050 6e 64 69 63 65 73 20 74 6f 20 62 69 74 6d 61 70  ndices to bitmap
28051 73 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49  s */.  Index *pI
28052 64 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  dx,            /
28053 2a 20 54 68 65 20 69 6e 64 65 78 20 77 65 20 61  * The index we a
28054 72 65 20 74 65 73 74 69 6e 67 20 2a 2f 0a 20 20  re testing */.  
28055 69 6e 74 20 62 61 73 65 2c 20 20 20 20 20 20 20  int base,       
28056 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f          /* Curso
28057 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  r number for the
28058 20 74 61 62 6c 65 20 74 6f 20 62 65 20 73 6f 72   table to be sor
28059 74 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ted */.  ExprLis
2805a 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20  t *pOrderBy,    
2805b 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59   /* The ORDER BY
2805c 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74   clause */.  int
2805d 20 6e 45 71 43 6f 6c 2c 20 20 20 20 20 20 20 20   nEqCol,        
2805e 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2805f 66 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 73 20  f index columns 
28060 77 69 74 68 20 3d 3d 20 63 6f 6e 73 74 72 61 69  with == constrai
28061 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 62  nts */.  int *pb
28062 52 65 76 20 20 20 20 20 20 20 20 20 20 20 20 20  Rev             
28063 20 2f 2a 20 53 65 74 20 74 6f 20 31 20 69 66 20   /* Set to 1 if 
28064 4f 52 44 45 52 20 42 59 20 69 73 20 44 45 53 43  ORDER BY is DESC
28065 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20   */.){.  int i, 
28066 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  j;              
28067 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
28068 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69   counters */.  i
28069 6e 74 20 73 6f 72 74 4f 72 64 65 72 20 3d 20 30  nt sortOrder = 0
2806a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2806b 2a 20 58 4f 52 20 6f 66 20 69 6e 64 65 78 20 61  * XOR of index a
2806c 6e 64 20 4f 52 44 45 52 20 42 59 20 73 6f 72 74  nd ORDER BY sort
2806d 20 64 69 72 65 63 74 69 6f 6e 20 2a 2f 0a 20 20   direction */.  
2806e 69 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20 20 20  int nTerm;      
2806f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28070 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 4f 52 44  /* Number of ORD
28071 45 52 20 42 59 20 74 65 72 6d 73 20 2a 2f 0a 20  ER BY terms */. 
28072 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
28073 5f 69 74 65 6d 20 2a 70 54 65 72 6d 3b 20 20 20  _item *pTerm;   
28074 20 2f 2a 20 41 20 74 65 72 6d 20 6f 66 20 74 68   /* A term of th
28075 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
28076 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  e */.  sqlite3 *
28077 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
28078 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 72 64  ..  assert( pOrd
28079 65 72 42 79 21 3d 30 20 29 3b 0a 20 20 6e 54 65  erBy!=0 );.  nTe
2807a 72 6d 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 6e  rm = pOrderBy->n
2807b 45 78 70 72 3b 0a 20 20 61 73 73 65 72 74 28 20  Expr;.  assert( 
2807c 6e 54 65 72 6d 3e 30 20 29 3b 0a 0a 20 20 2f 2a  nTerm>0 );..  /*
2807d 20 4d 61 74 63 68 20 74 65 72 6d 73 20 6f 66 20   Match terms of 
2807e 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
2807f 75 73 65 20 61 67 61 69 6e 73 74 20 63 6f 6c 75  use against colu
28080 6d 6e 73 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20  mns of.  ** the 
28081 69 6e 64 65 78 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  index..  **.  **
28082 20 4e 6f 74 65 20 74 68 61 74 20 69 6e 64 69 63   Note that indic
28083 65 73 20 68 61 76 65 20 70 49 64 78 2d 3e 6e 43  es have pIdx->nC
28084 6f 6c 75 6d 6e 20 72 65 67 75 6c 61 72 20 63 6f  olumn regular co
28085 6c 75 6d 6e 73 20 70 6c 75 73 0a 20 20 2a 2a 20  lumns plus.  ** 
28086 6f 6e 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 63  one additional c
28087 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 69 6e 67  olumn containing
28088 20 74 68 65 20 72 6f 77 69 64 2e 20 20 54 68 65   the rowid.  The
28089 20 72 6f 77 69 64 20 63 6f 6c 75 6d 6e 0a 20 20   rowid column.  
2808a 2a 2a 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  ** of the index 
2808b 69 73 20 61 6c 73 6f 20 61 6c 6c 6f 77 65 64 20  is also allowed 
2808c 74 6f 20 6d 61 74 63 68 20 61 67 61 69 6e 73 74  to match against
2808d 20 74 68 65 20 4f 52 44 45 52 20 42 59 0a 20 20   the ORDER BY.  
2808e 2a 2a 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a  ** clause..  */.
2808f 20 20 66 6f 72 28 69 3d 6a 3d 30 2c 20 70 54 65    for(i=j=0, pTe
28090 72 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20  rm=pOrderBy->a; 
28091 6a 3c 6e 54 65 72 6d 20 26 26 20 69 3c 3d 70 49  j<nTerm && i<=pI
28092 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b  dx->nColumn; i++
28093 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78  ){.    Expr *pEx
28094 70 72 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  pr;       /* The
28095 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 74   expression of t
28096 68 65 20 4f 52 44 45 52 20 42 59 20 70 54 65 72  he ORDER BY pTer
28097 6d 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53 65 71  m */.    CollSeq
28098 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 54   *pColl;    /* T
28099 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  he collating seq
2809a 75 65 6e 63 65 20 6f 66 20 70 45 78 70 72 20 2a  uence of pExpr *
2809b 2f 0a 20 20 20 20 69 6e 74 20 74 65 72 6d 53 6f  /.    int termSo
2809c 72 74 4f 72 64 65 72 3b 20 2f 2a 20 53 6f 72 74  rtOrder; /* Sort
2809d 20 6f 72 64 65 72 20 66 6f 72 20 74 68 69 73 20   order for this 
2809e 74 65 72 6d 20 2a 2f 0a 20 20 20 20 69 6e 74 20  term */.    int 
2809f 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 2f  iColumn;       /
280a0 2a 20 54 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d  * The i-th colum
280a1 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e 20  n of the index. 
280a2 20 2d 31 20 66 6f 72 20 72 6f 77 69 64 20 2a 2f   -1 for rowid */
280a3 0a 20 20 20 20 69 6e 74 20 69 53 6f 72 74 4f 72  .    int iSortOr
280a4 64 65 72 3b 20 20 20 20 2f 2a 20 31 20 66 6f 72  der;    /* 1 for
280a5 20 44 45 53 43 2c 20 30 20 66 6f 72 20 41 53 43   DESC, 0 for ASC
280a6 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 69 6e 64   on the i-th ind
280a7 65 78 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 63  ex term */.    c
280a8 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c  onst char *zColl
280a9 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65  ; /* Name of the
280aa 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
280ab 6e 63 65 20 66 6f 72 20 69 2d 74 68 20 69 6e 64  nce for i-th ind
280ac 65 78 20 74 65 72 6d 20 2a 2f 0a 0a 20 20 20 20  ex term */..    
280ad 70 45 78 70 72 20 3d 20 70 54 65 72 6d 2d 3e 70  pExpr = pTerm->p
280ae 45 78 70 72 3b 0a 20 20 20 20 69 66 28 20 70 45  Expr;.    if( pE
280af 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55  xpr->op!=TK_COLU
280b0 4d 4e 20 7c 7c 20 70 45 78 70 72 2d 3e 69 54 61  MN || pExpr->iTa
280b1 62 6c 65 21 3d 62 61 73 65 20 29 7b 0a 20 20 20  ble!=base ){.   
280b2 20 20 20 2f 2a 20 43 61 6e 20 6e 6f 74 20 75 73     /* Can not us
280b3 65 20 61 6e 20 69 6e 64 65 78 20 73 6f 72 74 20  e an index sort 
280b4 6f 6e 20 61 6e 79 74 68 69 6e 67 20 74 68 61 74  on anything that
280b5 20 69 73 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e   is not a column
280b6 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20 2a 2a   in the.      **
280b7 20 6c 65 66 74 2d 6d 6f 73 74 20 74 61 62 6c 65   left-most table
280b8 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   of the FROM cla
280b9 75 73 65 20 2a 2f 0a 20 20 20 20 20 20 62 72 65  use */.      bre
280ba 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43  ak;.    }.    pC
280bb 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70  oll = sqlite3Exp
280bc 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  rCollSeq(pParse,
280bd 20 70 45 78 70 72 29 3b 0a 20 20 20 20 69 66 28   pExpr);.    if(
280be 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20   !pColl ){.     
280bf 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66   pColl = db->pDf
280c0 6c 74 43 6f 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20  ltColl;.    }.  
280c1 20 20 69 66 28 20 69 3c 70 49 64 78 2d 3e 6e 43    if( i<pIdx->nC
280c2 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 69  olumn ){.      i
280c3 43 6f 6c 75 6d 6e 20 3d 20 70 49 64 78 2d 3e 61  Column = pIdx->a
280c4 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20  iColumn[i];.    
280c5 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3d 3d 70    if( iColumn==p
280c6 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 69 50 4b  Idx->pTable->iPK
280c7 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 69 43  ey ){.        iC
280c8 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20  olumn = -1;.    
280c9 20 20 7d 0a 20 20 20 20 20 20 69 53 6f 72 74 4f    }.      iSortO
280ca 72 64 65 72 20 3d 20 70 49 64 78 2d 3e 61 53 6f  rder = pIdx->aSo
280cb 72 74 4f 72 64 65 72 5b 69 5d 3b 0a 20 20 20 20  rtOrder[i];.    
280cc 20 20 7a 43 6f 6c 6c 20 3d 20 70 49 64 78 2d 3e    zColl = pIdx->
280cd 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 7d  azColl[i];.    }
280ce 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 43 6f 6c  else{.      iCol
280cf 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20  umn = -1;.      
280d0 69 53 6f 72 74 4f 72 64 65 72 20 3d 20 30 3b 0a  iSortOrder = 0;.
280d1 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 43        zColl = pC
280d2 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20  oll->zName;.    
280d3 7d 0a 20 20 20 20 69 66 28 20 70 45 78 70 72 2d  }.    if( pExpr-
280d4 3e 69 43 6f 6c 75 6d 6e 21 3d 69 43 6f 6c 75 6d  >iColumn!=iColum
280d5 6e 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72 49  n || sqlite3StrI
280d6 43 6d 70 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65  Cmp(pColl->zName
280d7 2c 20 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20  , zColl) ){.    
280d8 20 20 2f 2a 20 54 65 72 6d 20 6a 20 6f 66 20 74    /* Term j of t
280d9 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
280da 73 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63  se does not matc
280db 68 20 63 6f 6c 75 6d 6e 20 69 20 6f 66 20 74 68  h column i of th
280dc 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 20  e index */.     
280dd 20 69 66 28 20 69 3c 6e 45 71 43 6f 6c 20 29 7b   if( i<nEqCol ){
280de 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61  .        /* If a
280df 6e 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 20 74  n index column t
280e0 68 61 74 20 69 73 20 63 6f 6e 73 74 72 61 69 6e  hat is constrain
280e1 65 64 20 62 79 20 3d 3d 20 66 61 69 6c 73 20 74  ed by == fails t
280e2 6f 20 6d 61 74 63 68 20 61 6e 0a 20 20 20 20 20  o match an.     
280e3 20 20 20 2a 2a 20 4f 52 44 45 52 20 42 59 20 74     ** ORDER BY t
280e4 65 72 6d 2c 20 74 68 61 74 20 69 73 20 4f 4b 2e  erm, that is OK.
280e5 20 20 4a 75 73 74 20 69 67 6e 6f 72 65 20 74 68    Just ignore th
280e6 61 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  at column of the
280e7 20 69 6e 64 65 78 0a 20 20 20 20 20 20 20 20 2a   index.        *
280e8 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e  /.        contin
280e9 75 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  ue;.      }else 
280ea 69 66 28 20 69 3d 3d 70 49 64 78 2d 3e 6e 43 6f  if( i==pIdx->nCo
280eb 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20 20  lumn ){.        
280ec 2f 2a 20 49 6e 64 65 78 20 63 6f 6c 75 6d 6e 20  /* Index column 
280ed 69 20 69 73 20 74 68 65 20 72 6f 77 69 64 2e 20  i is the rowid. 
280ee 20 41 6c 6c 20 6f 74 68 65 72 20 74 65 72 6d 73   All other terms
280ef 20 6d 61 74 63 68 2e 20 2a 2f 0a 20 20 20 20 20   match. */.     
280f0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
280f1 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f  }else{.        /
280f2 2a 20 49 66 20 61 6e 20 69 6e 64 65 78 20 63 6f  * If an index co
280f3 6c 75 6d 6e 20 66 61 69 6c 73 20 74 6f 20 6d 61  lumn fails to ma
280f4 74 63 68 20 61 6e 64 20 69 73 20 6e 6f 74 20 63  tch and is not c
280f5 6f 6e 73 74 72 61 69 6e 65 64 20 62 79 20 3d 3d  onstrained by ==
280f6 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e  .        ** then
280f7 20 74 68 65 20 69 6e 64 65 78 20 63 61 6e 6e 6f   the index canno
280f8 74 20 73 61 74 69 73 66 79 20 74 68 65 20 4f 52  t satisfy the OR
280f9 44 45 52 20 42 59 20 63 6f 6e 73 74 72 61 69 6e  DER BY constrain
280fa 74 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  t..        */.  
280fb 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
280fc 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
280fd 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e    assert( pIdx->
280fe 61 53 6f 72 74 4f 72 64 65 72 21 3d 30 20 29 3b  aSortOrder!=0 );
280ff 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65  .    assert( pTe
28100 72 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3d 3d 30  rm->sortOrder==0
28101 20 7c 7c 20 70 54 65 72 6d 2d 3e 73 6f 72 74 4f   || pTerm->sortO
28102 72 64 65 72 3d 3d 31 20 29 3b 0a 20 20 20 20 61  rder==1 );.    a
28103 73 73 65 72 74 28 20 69 53 6f 72 74 4f 72 64 65  ssert( iSortOrde
28104 72 3d 3d 30 20 7c 7c 20 69 53 6f 72 74 4f 72 64  r==0 || iSortOrd
28105 65 72 3d 3d 31 20 29 3b 0a 20 20 20 20 74 65 72  er==1 );.    ter
28106 6d 53 6f 72 74 4f 72 64 65 72 20 3d 20 69 53 6f  mSortOrder = iSo
28107 72 74 4f 72 64 65 72 20 5e 20 70 54 65 72 6d 2d  rtOrder ^ pTerm-
28108 3e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20  >sortOrder;.    
28109 69 66 28 20 69 3e 6e 45 71 43 6f 6c 20 29 7b 0a  if( i>nEqCol ){.
2810a 20 20 20 20 20 20 69 66 28 20 74 65 72 6d 53 6f        if( termSo
2810b 72 74 4f 72 64 65 72 21 3d 73 6f 72 74 4f 72 64  rtOrder!=sortOrd
2810c 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  er ){.        /*
2810d 20 49 6e 64 69 63 65 73 20 63 61 6e 20 6f 6e 6c   Indices can onl
2810e 79 20 62 65 20 75 73 65 64 20 69 66 20 61 6c 6c  y be used if all
2810f 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20   ORDER BY terms 
28110 70 61 73 74 20 74 68 65 0a 20 20 20 20 20 20 20  past the.       
28111 20 2a 2a 20 65 71 75 61 6c 69 74 79 20 63 6f 6e   ** equality con
28112 73 74 72 61 69 6e 74 73 20 61 72 65 20 61 6c 6c  straints are all
28113 20 65 69 74 68 65 72 20 44 45 53 43 20 6f 72 20   either DESC or 
28114 41 53 43 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ASC. */.        
28115 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
28116 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
28117 20 20 20 73 6f 72 74 4f 72 64 65 72 20 3d 20 74     sortOrder = t
28118 65 72 6d 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20  ermSortOrder;.  
28119 20 20 7d 0a 20 20 20 20 6a 2b 2b 3b 0a 20 20 20    }.    j++;.   
2811a 20 70 54 65 72 6d 2b 2b 3b 0a 20 20 20 20 69 66   pTerm++;.    if
2811b 28 20 69 43 6f 6c 75 6d 6e 3c 30 20 26 26 20 21  ( iColumn<0 && !
2811c 72 65 66 65 72 65 6e 63 65 73 4f 74 68 65 72 54  referencesOtherT
2811d 61 62 6c 65 73 28 70 4f 72 64 65 72 42 79 2c 20  ables(pOrderBy, 
2811e 70 4d 61 73 6b 53 65 74 2c 20 6a 2c 20 62 61 73  pMaskSet, j, bas
2811f 65 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49  e) ){.      /* I
28120 66 20 74 68 65 20 69 6e 64 65 78 65 64 20 63 6f  f the indexed co
28121 6c 75 6d 6e 20 69 73 20 74 68 65 20 70 72 69 6d  lumn is the prim
28122 61 72 79 20 6b 65 79 20 61 6e 64 20 65 76 65 72  ary key and ever
28123 79 74 68 69 6e 67 20 6d 61 74 63 68 65 73 0a 20  ything matches. 
28124 20 20 20 20 20 2a 2a 20 73 6f 20 66 61 72 20 61       ** so far a
28125 6e 64 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 4f  nd none of the O
28126 52 44 45 52 20 42 59 20 74 65 72 6d 73 20 74 6f  RDER BY terms to
28127 20 74 68 65 20 72 69 67 68 74 20 72 65 66 65 72   the right refer
28128 65 6e 63 65 20 6f 74 68 65 72 0a 20 20 20 20 20  ence other.     
28129 20 2a 2a 20 74 61 62 6c 65 73 20 69 6e 20 74 68   ** tables in th
2812a 65 20 6a 6f 69 6e 2c 20 74 68 65 6e 20 77 65 20  e join, then we 
2812b 61 72 65 20 61 73 73 75 72 65 64 20 74 68 61 74  are assured that
2812c 20 74 68 65 20 69 6e 64 65 78 20 63 61 6e 20 62   the index can b
2812d 65 20 75 73 65 64 20 0a 20 20 20 20 20 20 2a 2a  e used .      **
2812e 20 74 6f 20 73 6f 72 74 20 62 65 63 61 75 73 65   to sort because
2812f 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79   the primary key
28130 20 69 73 20 75 6e 69 71 75 65 20 61 6e 64 20 73   is unique and s
28131 6f 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 6f 74  o none of the ot
28132 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6c  her.      ** col
28133 75 6d 6e 73 20 77 69 6c 6c 20 6d 61 6b 65 20 61  umns will make a
28134 6e 79 20 64 69 66 66 65 72 65 6e 63 65 0a 20 20  ny difference.  
28135 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6a 20 3d      */.      j =
28136 20 6e 54 65 72 6d 3b 0a 20 20 20 20 7d 0a 20 20   nTerm;.    }.  
28137 7d 0a 0a 20 20 2a 70 62 52 65 76 20 3d 20 73 6f  }..  *pbRev = so
28138 72 74 4f 72 64 65 72 21 3d 30 3b 0a 20 20 69 66  rtOrder!=0;.  if
28139 28 20 6a 3e 3d 6e 54 65 72 6d 20 29 7b 0a 20 20  ( j>=nTerm ){.  
2813a 20 20 2f 2a 20 41 6c 6c 20 74 65 72 6d 73 20 6f    /* All terms o
2813b 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  f the ORDER BY c
2813c 6c 61 75 73 65 20 61 72 65 20 63 6f 76 65 72 65  lause are covere
2813d 64 20 62 79 20 74 68 69 73 20 69 6e 64 65 78 20  d by this index 
2813e 73 6f 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 69  so.    ** this i
2813f 6e 64 65 78 20 63 61 6e 20 62 65 20 75 73 65 64  ndex can be used
28140 20 66 6f 72 20 73 6f 72 74 69 6e 67 2e 20 2a 2f   for sorting. */
28141 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
28142 20 7d 0a 20 20 69 66 28 20 70 49 64 78 2d 3e 6f   }.  if( pIdx->o
28143 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20  nError!=OE_None 
28144 26 26 20 69 3d 3d 70 49 64 78 2d 3e 6e 43 6f 6c  && i==pIdx->nCol
28145 75 6d 6e 0a 20 20 20 20 20 20 26 26 20 21 72 65  umn.      && !re
28146 66 65 72 65 6e 63 65 73 4f 74 68 65 72 54 61 62  ferencesOtherTab
28147 6c 65 73 28 70 4f 72 64 65 72 42 79 2c 20 70 4d  les(pOrderBy, pM
28148 61 73 6b 53 65 74 2c 20 6a 2c 20 62 61 73 65 29  askSet, j, base)
28149 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6c 6c 20 74   ){.    /* All t
2814a 65 72 6d 73 20 6f 66 20 74 68 69 73 20 69 6e 64  erms of this ind
2814b 65 78 20 6d 61 74 63 68 20 73 6f 6d 65 20 70 72  ex match some pr
2814c 65 66 69 78 20 6f 66 20 74 68 65 20 4f 52 44 45  efix of the ORDE
2814d 52 20 42 59 20 63 6c 61 75 73 65 0a 20 20 20 20  R BY clause.    
2814e 2a 2a 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78  ** and the index
2814f 20 69 73 20 55 4e 49 51 55 45 20 61 6e 64 20 6e   is UNIQUE and n
28150 6f 20 74 65 72 6d 73 20 6f 6e 20 74 68 65 20 74  o terms on the t
28151 61 69 6c 20 6f 66 20 74 68 65 20 4f 52 44 45 52  ail of the ORDER
28152 20 42 59 0a 20 20 20 20 2a 2a 20 63 6c 61 75 73   BY.    ** claus
28153 65 20 72 65 66 65 72 65 6e 63 65 20 6f 74 68 65  e reference othe
28154 72 20 74 61 62 6c 65 73 20 69 6e 20 61 20 6a 6f  r tables in a jo
28155 69 6e 2e 20 20 49 66 20 74 68 69 73 20 69 73 20  in.  If this is 
28156 61 6c 6c 20 74 72 75 65 20 74 68 65 6e 0a 20 20  all true then.  
28157 20 20 2a 2a 20 74 68 65 20 6f 72 64 65 72 20 62    ** the order b
28158 79 20 63 6c 61 75 73 65 20 69 73 20 73 75 70 65  y clause is supe
28159 72 66 6c 75 6f 75 73 2e 20 2a 2f 0a 20 20 20 20  rfluous. */.    
2815a 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20  return 1;.  }.  
2815b 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
2815c 2a 2a 20 43 68 65 63 6b 20 74 61 62 6c 65 20 74  ** Check table t
2815d 6f 20 73 65 65 20 69 66 20 74 68 65 20 4f 52 44  o see if the ORD
2815e 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 6e 20  ER BY clause in 
2815f 70 4f 72 64 65 72 42 79 20 63 61 6e 20 62 65 20  pOrderBy can be 
28160 73 61 74 69 73 66 69 65 64 0a 2a 2a 20 62 79 20  satisfied.** by 
28161 73 6f 72 74 69 6e 67 20 69 6e 20 6f 72 64 65 72  sorting in order
28162 20 6f 66 20 52 4f 57 49 44 2e 20 20 52 65 74 75   of ROWID.  Retu
28163 72 6e 20 74 72 75 65 20 69 66 20 73 6f 20 61 6e  rn true if so an
28164 64 20 73 65 74 20 2a 70 62 52 65 76 20 74 6f 20  d set *pbRev to 
28165 62 65 0a 2a 2a 20 74 72 75 65 20 66 6f 72 20 72  be.** true for r
28166 65 76 65 72 73 65 20 52 4f 57 49 44 20 61 6e 64  everse ROWID and
28167 20 66 61 6c 73 65 20 66 6f 72 20 66 6f 72 77 61   false for forwa
28168 72 64 20 52 4f 57 49 44 20 6f 72 64 65 72 2e 0a  rd ROWID order..
28169 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 6f  */.static int so
2816a 72 74 61 62 6c 65 42 79 52 6f 77 69 64 28 0a 20  rtableByRowid(. 
2816b 20 69 6e 74 20 62 61 73 65 2c 20 20 20 20 20 20   int base,      
2816c 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73           /* Curs
2816d 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 61  or number for ta
2816e 62 6c 65 20 74 6f 20 62 65 20 73 6f 72 74 65 64  ble to be sorted
2816f 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
28170 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 2f 2a  pOrderBy,     /*
28171 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   The ORDER BY cl
28172 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4d 61  ause */.  ExprMa
28173 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c  skSet *pMaskSet,
28174 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f    /* Mapping fro
28175 6d 20 74 61 62 6c 65 73 20 74 6f 20 62 69 74 6d  m tables to bitm
28176 61 70 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 62  aps */.  int *pb
28177 52 65 76 20 20 20 20 20 20 20 20 20 20 20 20 20  Rev             
28178 20 2f 2a 20 53 65 74 20 74 6f 20 31 20 69 66 20   /* Set to 1 if 
28179 4f 52 44 45 52 20 42 59 20 69 73 20 44 45 53 43  ORDER BY is DESC
2817a 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70   */.){.  Expr *p
2817b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 72  ;..  assert( pOr
2817c 64 65 72 42 79 21 3d 30 20 29 3b 0a 20 20 61 73  derBy!=0 );.  as
2817d 73 65 72 74 28 20 70 4f 72 64 65 72 42 79 2d 3e  sert( pOrderBy->
2817e 6e 45 78 70 72 3e 30 20 29 3b 0a 20 20 70 20 3d  nExpr>0 );.  p =
2817f 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 30 5d 2e   pOrderBy->a[0].
28180 70 45 78 70 72 3b 0a 20 20 69 66 28 20 70 2d 3e  pExpr;.  if( p->
28181 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26  op==TK_COLUMN &&
28182 20 70 2d 3e 69 54 61 62 6c 65 3d 3d 62 61 73 65   p->iTable==base
28183 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d   && p->iColumn==
28184 2d 31 0a 20 20 20 20 26 26 20 21 72 65 66 65 72  -1.    && !refer
28185 65 6e 63 65 73 4f 74 68 65 72 54 61 62 6c 65 73  encesOtherTables
28186 28 70 4f 72 64 65 72 42 79 2c 20 70 4d 61 73 6b  (pOrderBy, pMask
28187 53 65 74 2c 20 31 2c 20 62 61 73 65 29 20 29 7b  Set, 1, base) ){
28188 0a 20 20 20 20 2a 70 62 52 65 76 20 3d 20 70 4f  .    *pbRev = pO
28189 72 64 65 72 42 79 2d 3e 61 5b 30 5d 2e 73 6f 72  rderBy->a[0].sor
2818a 74 4f 72 64 65 72 3b 0a 20 20 20 20 72 65 74 75  tOrder;.    retu
2818b 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rn 1;.  }.  retu
2818c 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  rn 0;.}../*.** P
2818d 72 65 70 61 72 65 20 61 20 63 72 75 64 65 20 65  repare a crude e
2818e 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20 6c  stimate of the l
2818f 6f 67 61 72 69 74 68 6d 20 6f 66 20 74 68 65 20  ogarithm of the 
28190 69 6e 70 75 74 20 76 61 6c 75 65 2e 0a 2a 2a 20  input value..** 
28191 54 68 65 20 72 65 73 75 6c 74 73 20 6e 65 65 64  The results need
28192 20 6e 6f 74 20 62 65 20 65 78 61 63 74 2e 20 20   not be exact.  
28193 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 75 73 65  This is only use
28194 64 20 66 6f 72 20 65 73 74 69 6d 61 74 69 6e 67  d for estimating
28195 0a 2a 2a 20 74 68 65 20 74 6f 74 61 6c 20 63 6f  .** the total co
28196 73 74 20 6f 66 20 70 65 72 66 6f 72 6d 69 6e 67  st of performing
28197 20 6f 70 65 72 61 74 69 6f 6e 73 20 77 69 74 68   operations with
28198 20 4f 28 6c 6f 67 4e 29 20 6f 72 20 4f 28 4e 6c   O(logN) or O(Nl
28199 6f 67 4e 29 0a 2a 2a 20 63 6f 6d 70 6c 65 78 69  ogN).** complexi
2819a 74 79 2e 20 20 42 65 63 61 75 73 65 20 4e 20 69  ty.  Because N i
2819b 73 20 6a 75 73 74 20 61 20 67 75 65 73 73 2c 20  s just a guess, 
2819c 69 74 20 69 73 20 6e 6f 20 67 72 65 61 74 20 74  it is no great t
2819d 72 61 67 65 64 79 20 69 66 0a 2a 2a 20 6c 6f 67  ragedy if.** log
2819e 4e 20 69 73 20 61 20 6c 69 74 74 6c 65 20 6f 66  N is a little of
2819f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 64 6f 75  f..*/.static dou
281a0 62 6c 65 20 65 73 74 4c 6f 67 28 64 6f 75 62 6c  ble estLog(doubl
281a1 65 20 4e 29 7b 0a 20 20 64 6f 75 62 6c 65 20 6c  e N){.  double l
281a2 6f 67 4e 20 3d 20 31 3b 0a 20 20 64 6f 75 62 6c  ogN = 1;.  doubl
281a3 65 20 78 20 3d 20 31 30 3b 0a 20 20 77 68 69 6c  e x = 10;.  whil
281a4 65 28 20 4e 3e 78 20 29 7b 0a 20 20 20 20 6c 6f  e( N>x ){.    lo
281a5 67 4e 20 2b 3d 20 31 3b 0a 20 20 20 20 78 20 2a  gN += 1;.    x *
281a6 3d 20 31 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75  = 10;.  }.  retu
281a7 72 6e 20 6c 6f 67 4e 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn logN;.}../*.*
281a8 2a 20 54 77 6f 20 72 6f 75 74 69 6e 65 73 20 66  * Two routines f
281a9 6f 72 20 70 72 69 6e 74 69 6e 67 20 74 68 65 20  or printing the 
281aa 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20 73 71  content of an sq
281ab 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
281ac 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20  .** structure.  
281ad 55 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  Used for testing
281ae 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f   and debugging o
281af 6e 6c 79 2e 20 20 49 66 20 6e 65 69 74 68 65 72  nly.  If neither
281b0 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20  .** SQLITE_TEST 
281b1 6f 72 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  or SQLITE_DEBUG 
281b2 61 72 65 20 64 65 66 69 6e 65 64 2c 20 74 68 65  are defined, the
281b3 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  n these routines
281b4 0a 2a 2a 20 61 72 65 20 6e 6f 2d 6f 70 73 2e 0a  .** are no-ops..
281b5 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  */.#if !defined(
281b6 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
281b7 55 41 4c 54 41 42 4c 45 29 20 26 26 20 64 65 66  UALTABLE) && def
281b8 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
281b9 47 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20 54  G).static void T
281ba 52 41 43 45 5f 49 44 58 5f 49 4e 50 55 54 53 28  RACE_IDX_INPUTS(
281bb 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
281bc 66 6f 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b  fo *p){.  int i;
281bd 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 57  .  if( !sqlite3W
281be 68 65 72 65 54 72 61 63 65 20 29 20 72 65 74 75  hereTrace ) retu
281bf 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  rn;.  for(i=0; i
281c0 3c 70 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  <p->nConstraint;
281c1 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74   i++){.    sqlit
281c2 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 20  e3DebugPrintf(" 
281c3 20 63 6f 6e 73 74 72 61 69 6e 74 5b 25 64 5d 3a   constraint[%d]:
281c4 20 63 6f 6c 3d 25 64 20 74 65 72 6d 69 64 3d 25   col=%d termid=%
281c5 64 20 6f 70 3d 25 64 20 75 73 61 62 6c 65 64 3d  d op=%d usabled=
281c6 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 69 2c  %d\n",.       i,
281c7 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73  .       p->aCons
281c8 74 72 61 69 6e 74 5b 69 5d 2e 69 43 6f 6c 75 6d  traint[i].iColum
281c9 6e 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f  n,.       p->aCo
281ca 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 69 54 65 72  nstraint[i].iTer
281cb 6d 4f 66 66 73 65 74 2c 0a 20 20 20 20 20 20 20  mOffset,.       
281cc 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69  p->aConstraint[i
281cd 5d 2e 6f 70 2c 0a 20 20 20 20 20 20 20 70 2d 3e  ].op,.       p->
281ce 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 75  aConstraint[i].u
281cf 73 61 62 6c 65 29 3b 0a 20 20 7d 0a 20 20 66 6f  sable);.  }.  fo
281d0 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 72 64  r(i=0; i<p->nOrd
281d1 65 72 42 79 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  erBy; i++){.    
281d2 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
281d3 74 66 28 22 20 20 6f 72 64 65 72 62 79 5b 25 64  tf("  orderby[%d
281d4 5d 3a 20 63 6f 6c 3d 25 64 20 64 65 73 63 3d 25  ]: col=%d desc=%
281d5 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 69 2c 0a  d\n",.       i,.
281d6 20 20 20 20 20 20 20 70 2d 3e 61 4f 72 64 65 72         p->aOrder
281d7 42 79 5b 69 5d 2e 69 43 6f 6c 75 6d 6e 2c 0a 20  By[i].iColumn,. 
281d8 20 20 20 20 20 20 70 2d 3e 61 4f 72 64 65 72 42        p->aOrderB
281d9 79 5b 69 5d 2e 64 65 73 63 29 3b 0a 20 20 7d 0a  y[i].desc);.  }.
281da 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 54 52  }.static void TR
281db 41 43 45 5f 49 44 58 5f 4f 55 54 50 55 54 53 28  ACE_IDX_OUTPUTS(
281dc 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
281dd 66 6f 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b  fo *p){.  int i;
281de 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 57  .  if( !sqlite3W
281df 68 65 72 65 54 72 61 63 65 20 29 20 72 65 74 75  hereTrace ) retu
281e0 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  rn;.  for(i=0; i
281e1 3c 70 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  <p->nConstraint;
281e2 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74   i++){.    sqlit
281e3 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 20  e3DebugPrintf(" 
281e4 20 75 73 61 67 65 5b 25 64 5d 3a 20 61 72 67 76   usage[%d]: argv
281e5 49 64 78 3d 25 64 20 6f 6d 69 74 3d 25 64 5c 6e  Idx=%d omit=%d\n
281e6 22 2c 0a 20 20 20 20 20 20 20 69 2c 0a 20 20 20  ",.       i,.   
281e7 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69      p->aConstrai
281e8 6e 74 55 73 61 67 65 5b 69 5d 2e 61 72 67 76 49  ntUsage[i].argvI
281e9 6e 64 65 78 2c 0a 20 20 20 20 20 20 20 70 2d 3e  ndex,.       p->
281ea 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
281eb 5b 69 5d 2e 6f 6d 69 74 29 3b 0a 20 20 7d 0a 20  [i].omit);.  }. 
281ec 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
281ed 6e 74 66 28 22 20 20 69 64 78 4e 75 6d 3d 25 64  ntf("  idxNum=%d
281ee 5c 6e 22 2c 20 70 2d 3e 69 64 78 4e 75 6d 29 3b  \n", p->idxNum);
281ef 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50  .  sqlite3DebugP
281f0 72 69 6e 74 66 28 22 20 20 69 64 78 53 74 72 3d  rintf("  idxStr=
281f1 25 73 5c 6e 22 2c 20 70 2d 3e 69 64 78 53 74 72  %s\n", p->idxStr
281f2 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75  );.  sqlite3Debu
281f3 67 50 72 69 6e 74 66 28 22 20 20 6f 72 64 65 72  gPrintf("  order
281f4 42 79 43 6f 6e 73 75 6d 65 64 3d 25 64 5c 6e 22  ByConsumed=%d\n"
281f5 2c 20 70 2d 3e 6f 72 64 65 72 42 79 43 6f 6e 73  , p->orderByCons
281f6 75 6d 65 64 29 3b 0a 20 20 73 71 6c 69 74 65 33  umed);.  sqlite3
281f7 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 20 65  DebugPrintf("  e
281f8 73 74 69 6d 61 74 65 64 43 6f 73 74 3d 25 67 5c  stimatedCost=%g\
281f9 6e 22 2c 20 70 2d 3e 65 73 74 69 6d 61 74 65 64  n", p->estimated
281fa 43 6f 73 74 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23  Cost);.}.#else.#
281fb 64 65 66 69 6e 65 20 54 52 41 43 45 5f 49 44 58  define TRACE_IDX
281fc 5f 49 4e 50 55 54 53 28 41 29 0a 23 64 65 66 69  _INPUTS(A).#defi
281fd 6e 65 20 54 52 41 43 45 5f 49 44 58 5f 4f 55 54  ne TRACE_IDX_OUT
281fe 50 55 54 53 28 41 29 0a 23 65 6e 64 69 66 0a 0a  PUTS(A).#endif..
281ff 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
28200 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
28201 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74  ./*.** Compute t
28202 68 65 20 62 65 73 74 20 69 6e 64 65 78 20 66 6f  he best index fo
28203 72 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  r a virtual tabl
28204 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 65 73  e..**.** The bes
28205 74 20 69 6e 64 65 78 20 69 73 20 63 6f 6d 70 75  t index is compu
28206 74 65 64 20 62 79 20 74 68 65 20 78 42 65 73 74  ted by the xBest
28207 49 6e 64 65 78 20 6d 65 74 68 6f 64 20 6f 66 20  Index method of 
28208 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74  the virtual.** t
28209 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68  able module.  Th
2820a 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 65  is routine is re
2820b 61 6c 6c 79 20 6a 75 73 74 20 61 20 77 72 61 70  ally just a wrap
2820c 70 65 72 20 74 68 61 74 20 73 65 74 73 20 75 70  per that sets up
2820d 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f  .** the sqlite3_
2820e 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63  index_info struc
2820f 74 75 72 65 20 74 68 61 74 20 69 73 20 75 73 65  ture that is use
28210 64 20 74 6f 20 63 6f 6d 6d 75 6e 69 63 61 74 65  d to communicate
28211 20 77 69 74 68 0a 2a 2a 20 78 42 65 73 74 49 6e   with.** xBestIn
28212 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 20  dex..**.** In a 
28213 6a 6f 69 6e 2c 20 74 68 69 73 20 72 6f 75 74 69  join, this routi
28214 6e 65 20 6d 69 67 68 74 20 62 65 20 63 61 6c 6c  ne might be call
28215 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65  ed multiple time
28216 73 20 66 6f 72 20 74 68 65 0a 2a 2a 20 73 61 6d  s for the.** sam
28217 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
28218 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e    The sqlite3_in
28219 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75  dex_info structu
2821a 72 65 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a  re is created.**
2821b 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 64   and initialized
2821c 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 69 6e   on the first in
2821d 76 6f 63 61 74 69 6f 6e 20 61 6e 64 20 72 65 75  vocation and reu
2821e 73 65 64 20 6f 6e 20 61 6c 6c 20 73 75 62 73 65  sed on all subse
2821f 71 75 65 6e 74 0a 2a 2a 20 69 6e 76 6f 63 61 74  quent.** invocat
28220 69 6f 6e 73 2e 20 20 54 68 65 20 73 71 6c 69 74  ions.  The sqlit
28221 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74  e3_index_info st
28222 72 75 63 74 75 72 65 20 69 73 20 61 6c 73 6f 20  ructure is also 
28223 75 73 65 64 20 77 68 65 6e 0a 2a 2a 20 63 6f 64  used when.** cod
28224 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 74  e is generated t
28225 6f 20 61 63 63 65 73 73 20 74 68 65 20 76 69 72  o access the vir
28226 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65  tual table.  The
28227 20 77 68 65 72 65 49 6e 66 6f 44 65 6c 65 74 65   whereInfoDelete
28228 28 29 20 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74  () .** routine t
28229 61 6b 65 73 20 63 61 72 65 20 6f 66 20 66 72 65  akes care of fre
2822a 65 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33  eing the sqlite3
2822b 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75  _index_info stru
2822c 63 74 75 72 65 20 61 66 74 65 72 0a 2a 2a 20 65  cture after.** e
2822d 76 65 72 79 62 6f 64 79 20 68 61 73 20 66 69 6e  verybody has fin
2822e 69 73 68 65 64 20 77 69 74 68 20 69 74 2e 0a 2a  ished with it..*
2822f 2f 0a 73 74 61 74 69 63 20 64 6f 75 62 6c 65 20  /.static double 
28230 62 65 73 74 56 69 72 74 75 61 6c 49 6e 64 65 78  bestVirtualIndex
28231 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
28232 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
28233 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e     /* The parsin
28234 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57  g context */.  W
28235 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c  hereClause *pWC,
28236 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
28237 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
28238 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72  e */.  struct Sr
28239 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 72 63  cList_item *pSrc
2823a 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 46 52 4f  ,     /* The FRO
2823b 4d 20 63 6c 61 75 73 65 20 74 65 72 6d 20 74 6f  M clause term to
2823c 20 73 65 61 72 63 68 20 2a 2f 0a 20 20 42 69 74   search */.  Bit
2823d 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20  mask notReady,  
2823e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
2823f 61 73 6b 20 6f 66 20 63 75 72 73 6f 72 73 20 74  ask of cursors t
28240 68 61 74 20 61 72 65 20 6e 6f 74 20 61 76 61 69  hat are not avai
28241 6c 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c  lable */.  ExprL
28242 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20  ist *pOrderBy,  
28243 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
28244 20 6f 72 64 65 72 20 62 79 20 63 6c 61 75 73 65   order by clause
28245 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 64 65 72 42   */.  int orderB
28246 79 55 73 61 62 6c 65 2c 20 20 20 20 20 20 20 20  yUsable,        
28247 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
28248 77 65 20 63 61 6e 20 70 6f 74 65 6e 74 69 61 6c  we can potential
28249 20 73 6f 72 74 20 2a 2f 0a 20 20 73 71 6c 69 74   sort */.  sqlit
2824a 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 2a  e3_index_info **
2824b 70 70 49 64 78 49 6e 66 6f 20 2f 2a 20 49 6e 64  ppIdxInfo /* Ind
2824c 65 78 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 70  ex information p
2824d 61 73 73 65 64 20 74 6f 20 78 42 65 73 74 49 6e  assed to xBestIn
2824e 64 65 78 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c  dex */.){.  Tabl
2824f 65 20 2a 70 54 61 62 20 3d 20 70 53 72 63 2d 3e  e *pTab = pSrc->
28250 70 54 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f  pTab;.  sqlite3_
28251 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20 70 54  vtab *pVtab = pT
28252 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 73 71 6c  ab->pVtab;.  sql
28253 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
28254 2a 70 49 64 78 49 6e 66 6f 3b 0a 20 20 73 74 72  *pIdxInfo;.  str
28255 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
28256 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 2a 70 49  x_constraint *pI
28257 64 78 43 6f 6e 73 3b 0a 20 20 73 74 72 75 63 74  dxCons;.  struct
28258 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f   sqlite3_index_o
28259 72 64 65 72 62 79 20 2a 70 49 64 78 4f 72 64 65  rderby *pIdxOrde
2825a 72 42 79 3b 0a 20 20 73 74 72 75 63 74 20 73 71  rBy;.  struct sq
2825b 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73  lite3_index_cons
2825c 74 72 61 69 6e 74 5f 75 73 61 67 65 20 2a 70 55  traint_usage *pU
2825d 73 61 67 65 3b 0a 20 20 57 68 65 72 65 54 65 72  sage;.  WhereTer
2825e 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e 74 20  m *pTerm;.  int 
2825f 69 2c 20 6a 3b 0a 20 20 69 6e 74 20 6e 4f 72 64  i, j;.  int nOrd
28260 65 72 42 79 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  erBy;.  int rc;.
28261 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73 71 6c  .  /* If the sql
28262 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
28263 73 74 72 75 63 74 75 72 65 20 68 61 73 20 6e 6f  structure has no
28264 74 20 62 65 65 6e 20 70 72 65 76 69 6f 75 73 6c  t been previousl
28265 79 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64  y.  ** allocated
28266 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 64   and initialized
28267 20 66 6f 72 20 74 68 69 73 20 76 69 72 74 75 61   for this virtua
28268 6c 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 61 6c  l table, then al
28269 6c 6f 63 61 74 65 0a 20 20 2a 2a 20 61 6e 64 20  locate.  ** and 
2826a 69 6e 69 74 69 61 6c 69 7a 65 20 69 74 20 6e 6f  initialize it no
2826b 77 0a 20 20 2a 2f 0a 20 20 70 49 64 78 49 6e 66  w.  */.  pIdxInf
2826c 6f 20 3d 20 2a 70 70 49 64 78 49 6e 66 6f 3b 0a  o = *ppIdxInfo;.
2826d 20 20 69 66 28 20 70 49 64 78 49 6e 66 6f 3d 3d    if( pIdxInfo==
2826e 30 20 29 7b 0a 20 20 20 20 57 68 65 72 65 54 65  0 ){.    WhereTe
2826f 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 20 20 69  rm *pTerm;.    i
28270 6e 74 20 6e 54 65 72 6d 3b 0a 20 20 20 20 57 48  nt nTerm;.    WH
28271 45 52 45 54 52 41 43 45 28 28 22 52 65 63 6f 6d  ERETRACE(("Recom
28272 70 75 74 69 6e 67 20 69 6e 64 65 78 20 69 6e 66  puting index inf
28273 6f 20 66 6f 72 20 25 73 2e 2e 2e 5c 6e 22 2c 20  o for %s...\n", 
28274 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 0a  pTab->zName));..
28275 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 74 68 65      /* Count the
28276 20 6e 75 6d 62 65 72 20 6f 66 20 70 6f 73 73 69   number of possi
28277 62 6c 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  ble WHERE clause
28278 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 72 65 66   constraints ref
28279 65 72 72 69 6e 67 0a 20 20 20 20 2a 2a 20 74 6f  erring.    ** to
2827a 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 74 61   this virtual ta
2827b 62 6c 65 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69  ble */.    for(i
2827c 3d 6e 54 65 72 6d 3d 30 2c 20 70 54 65 72 6d 3d  =nTerm=0, pTerm=
2827d 70 57 43 2d 3e 61 3b 20 69 3c 70 57 43 2d 3e 6e  pWC->a; i<pWC->n
2827e 54 65 72 6d 3b 20 69 2b 2b 2c 20 70 54 65 72 6d  Term; i++, pTerm
2827f 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  ++){.      if( p
28280 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72  Term->leftCursor
28281 20 21 3d 20 70 53 72 63 2d 3e 69 43 75 72 73 6f   != pSrc->iCurso
28282 72 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  r ) continue;.  
28283 20 20 20 20 61 73 73 65 72 74 28 20 28 70 54 65      assert( (pTe
28284 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 26 28 70  rm->eOperator&(p
28285 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 2d  Term->eOperator-
28286 31 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  1))==0 );.      
28287 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d  testcase( pTerm-
28288 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49  >eOperator==WO_I
28289 4e 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  N );.      testc
2828a 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65  ase( pTerm->eOpe
2828b 72 61 74 6f 72 3d 3d 57 4f 5f 49 53 4e 55 4c 4c  rator==WO_ISNULL
2828c 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54   );.      if( pT
2828d 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26  erm->eOperator &
2828e 20 28 57 4f 5f 49 4e 7c 57 4f 5f 49 53 4e 55 4c   (WO_IN|WO_ISNUL
2828f 4c 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  L) ) continue;. 
28290 20 20 20 20 20 6e 54 65 72 6d 2b 2b 3b 0a 20 20       nTerm++;.  
28291 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74    }..    /* If t
28292 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
28293 73 65 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 6c 79  se contains only
28294 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
28295 63 75 72 72 65 6e 74 20 0a 20 20 20 20 2a 2a 20  current .    ** 
28296 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 74 68  virtual table th
28297 65 6e 20 61 6c 6c 6f 63 61 74 65 20 73 70 61 63  en allocate spac
28298 65 20 66 6f 72 20 74 68 65 20 61 4f 72 64 65 72  e for the aOrder
28299 42 79 20 70 61 72 74 20 6f 66 0a 20 20 20 20 2a  By part of.    *
2829a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e  * the sqlite3_in
2829b 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75  dex_info structu
2829c 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e  re..    */.    n
2829d 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 20  OrderBy = 0;.   
2829e 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b   if( pOrderBy ){
2829f 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
282a0 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  i<pOrderBy->nExp
282a1 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  r; i++){.       
282a2 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70   Expr *pExpr = p
282a3 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 70 45  OrderBy->a[i].pE
282a4 78 70 72 3b 0a 20 20 20 20 20 20 20 20 69 66 28  xpr;.        if(
282a5 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43   pExpr->op!=TK_C
282a6 4f 4c 55 4d 4e 20 7c 7c 20 70 45 78 70 72 2d 3e  OLUMN || pExpr->
282a7 69 54 61 62 6c 65 21 3d 70 53 72 63 2d 3e 69 43  iTable!=pSrc->iC
282a8 75 72 73 6f 72 20 29 20 62 72 65 61 6b 3b 0a 20  ursor ) break;. 
282a9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
282aa 20 69 3d 3d 70 4f 72 64 65 72 42 79 2d 3e 6e 45   i==pOrderBy->nE
282ab 78 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20 6e  xpr ){.        n
282ac 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64 65 72  OrderBy = pOrder
282ad 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20  By->nExpr;.     
282ae 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a   }.    }..    /*
282af 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 73 71   Allocate the sq
282b0 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
282b1 20 73 74 72 75 63 74 75 72 65 0a 20 20 20 20 2a   structure.    *
282b2 2f 0a 20 20 20 20 70 49 64 78 49 6e 66 6f 20 3d  /.    pIdxInfo =
282b3 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
282b4 5a 65 72 6f 28 70 50 61 72 73 65 2d 3e 64 62 2c  Zero(pParse->db,
282b5 20 73 69 7a 65 6f 66 28 2a 70 49 64 78 49 6e 66   sizeof(*pIdxInf
282b6 6f 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  o).             
282b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
282b8 2b 20 28 73 69 7a 65 6f 66 28 2a 70 49 64 78 43  + (sizeof(*pIdxC
282b9 6f 6e 73 29 20 2b 20 73 69 7a 65 6f 66 28 2a 70  ons) + sizeof(*p
282ba 55 73 61 67 65 29 29 2a 6e 54 65 72 6d 0a 20 20  Usage))*nTerm.  
282bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
282bc 20 20 20 20 20 20 20 20 20 20 20 2b 20 73 69 7a             + siz
282bd 65 6f 66 28 2a 70 49 64 78 4f 72 64 65 72 42 79  eof(*pIdxOrderBy
282be 29 2a 6e 4f 72 64 65 72 42 79 20 29 3b 0a 20 20  )*nOrderBy );.  
282bf 20 20 69 66 28 20 70 49 64 78 49 6e 66 6f 3d 3d    if( pIdxInfo==
282c0 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
282c1 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
282c2 65 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72  e, "out of memor
282c3 79 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  y");.      retur
282c4 6e 20 30 2e 30 3b 0a 20 20 20 20 7d 0a 20 20 20  n 0.0;.    }.   
282c5 20 2a 70 70 49 64 78 49 6e 66 6f 20 3d 20 70 49   *ppIdxInfo = pI
282c6 64 78 49 6e 66 6f 3b 0a 0a 20 20 20 20 2f 2a 20  dxInfo;..    /* 
282c7 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 73  Initialize the s
282c8 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 73  tructure.  The s
282c9 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
282ca 6f 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74  o structure cont
282cb 61 69 6e 73 0a 20 20 20 20 2a 2a 20 6d 61 6e 79  ains.    ** many
282cc 20 66 69 65 6c 64 73 20 74 68 61 74 20 61 72 65   fields that are
282cd 20 64 65 63 6c 61 72 65 64 20 22 63 6f 6e 73 74   declared "const
282ce 22 20 74 6f 20 70 72 65 76 65 6e 74 20 78 42 65  " to prevent xBe
282cf 73 74 49 6e 64 65 78 20 66 72 6f 6d 0a 20 20 20  stIndex from.   
282d0 20 2a 2a 20 63 68 61 6e 67 69 6e 67 20 74 68 65   ** changing the
282d1 6d 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 64  m.  We have to d
282d2 6f 20 73 6f 6d 65 20 66 75 6e 6b 79 20 63 61 73  o some funky cas
282d3 74 69 6e 67 20 69 6e 20 6f 72 64 65 72 20 74 6f  ting in order to
282d4 0a 20 20 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69  .    ** initiali
282d5 7a 65 20 74 68 6f 73 65 20 66 69 65 6c 64 73 2e  ze those fields.
282d6 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 49 64 78  .    */.    pIdx
282d7 43 6f 6e 73 20 3d 20 28 73 74 72 75 63 74 20 73  Cons = (struct s
282d8 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e  qlite3_index_con
282d9 73 74 72 61 69 6e 74 2a 29 26 70 49 64 78 49 6e  straint*)&pIdxIn
282da 66 6f 5b 31 5d 3b 0a 20 20 20 20 70 49 64 78 4f  fo[1];.    pIdxO
282db 72 64 65 72 42 79 20 3d 20 28 73 74 72 75 63 74  rderBy = (struct
282dc 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f   sqlite3_index_o
282dd 72 64 65 72 62 79 2a 29 26 70 49 64 78 43 6f 6e  rderby*)&pIdxCon
282de 73 5b 6e 54 65 72 6d 5d 3b 0a 20 20 20 20 70 55  s[nTerm];.    pU
282df 73 61 67 65 20 3d 20 28 73 74 72 75 63 74 20 73  sage = (struct s
282e0 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e  qlite3_index_con
282e1 73 74 72 61 69 6e 74 5f 75 73 61 67 65 2a 29 26  straint_usage*)&
282e2 70 49 64 78 4f 72 64 65 72 42 79 5b 6e 4f 72 64  pIdxOrderBy[nOrd
282e3 65 72 42 79 5d 3b 0a 20 20 20 20 2a 28 69 6e 74  erBy];.    *(int
282e4 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f  *)&pIdxInfo->nCo
282e5 6e 73 74 72 61 69 6e 74 20 3d 20 6e 54 65 72 6d  nstraint = nTerm
282e6 3b 0a 20 20 20 20 2a 28 69 6e 74 2a 29 26 70 49  ;.    *(int*)&pI
282e7 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79  dxInfo->nOrderBy
282e8 20 3d 20 6e 4f 72 64 65 72 42 79 3b 0a 20 20 20   = nOrderBy;.   
282e9 20 2a 28 73 74 72 75 63 74 20 73 71 6c 69 74 65   *(struct sqlite
282ea 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69  3_index_constrai
282eb 6e 74 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e  nt**)&pIdxInfo->
282ec 61 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 70 49  aConstraint = pI
282ed 64 78 43 6f 6e 73 3b 0a 20 20 20 20 2a 28 73 74  dxCons;.    *(st
282ee 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
282ef 65 78 5f 6f 72 64 65 72 62 79 2a 2a 29 26 70 49  ex_orderby**)&pI
282f0 64 78 49 6e 66 6f 2d 3e 61 4f 72 64 65 72 42 79  dxInfo->aOrderBy
282f1 20 3d 20 70 49 64 78 4f 72 64 65 72 42 79 3b 0a   = pIdxOrderBy;.
282f2 20 20 20 20 2a 28 73 74 72 75 63 74 20 73 71 6c      *(struct sql
282f3 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74  ite3_index_const
282f4 72 61 69 6e 74 5f 75 73 61 67 65 2a 2a 29 26 70  raint_usage**)&p
282f5 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72  IdxInfo->aConstr
282f6 61 69 6e 74 55 73 61 67 65 20 3d 0a 20 20 20 20  aintUsage =.    
282f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
282f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
282f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
282fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
282fb 20 70 55 73 61 67 65 3b 0a 0a 20 20 20 20 66 6f   pUsage;..    fo
282fc 72 28 69 3d 6a 3d 30 2c 20 70 54 65 72 6d 3d 70  r(i=j=0, pTerm=p
282fd 57 43 2d 3e 61 3b 20 69 3c 70 57 43 2d 3e 6e 54  WC->a; i<pWC->nT
282fe 65 72 6d 3b 20 69 2b 2b 2c 20 70 54 65 72 6d 2b  erm; i++, pTerm+
282ff 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 54  +){.      if( pT
28300 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20  erm->leftCursor 
28301 21 3d 20 70 53 72 63 2d 3e 69 43 75 72 73 6f 72  != pSrc->iCursor
28302 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
28303 20 20 20 61 73 73 65 72 74 28 20 28 70 54 65 72     assert( (pTer
28304 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 26 28 70 54  m->eOperator&(pT
28305 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 2d 31  erm->eOperator-1
28306 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74  ))==0 );.      t
28307 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e  estcase( pTerm->
28308 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49 4e  eOperator==WO_IN
28309 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
2830a 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72  se( pTerm->eOper
2830b 61 74 6f 72 3d 3d 57 4f 5f 49 53 4e 55 4c 4c 20  ator==WO_ISNULL 
2830c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 65  );.      if( pTe
2830d 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20  rm->eOperator & 
2830e 28 57 4f 5f 49 4e 7c 57 4f 5f 49 53 4e 55 4c 4c  (WO_IN|WO_ISNULL
2830f 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  ) ) continue;.  
28310 20 20 20 20 70 49 64 78 43 6f 6e 73 5b 6a 5d 2e      pIdxCons[j].
28311 69 43 6f 6c 75 6d 6e 20 3d 20 70 54 65 72 6d 2d  iColumn = pTerm-
28312 3e 6c 65 66 74 43 6f 6c 75 6d 6e 3b 0a 20 20 20  >leftColumn;.   
28313 20 20 20 70 49 64 78 43 6f 6e 73 5b 6a 5d 2e 69     pIdxCons[j].i
28314 54 65 72 6d 4f 66 66 73 65 74 20 3d 20 69 3b 0a  TermOffset = i;.
28315 20 20 20 20 20 20 70 49 64 78 43 6f 6e 73 5b 6a        pIdxCons[j
28316 5d 2e 6f 70 20 3d 20 70 54 65 72 6d 2d 3e 65 4f  ].op = pTerm->eO
28317 70 65 72 61 74 6f 72 3b 0a 20 20 20 20 20 20 2f  perator;.      /
28318 2a 20 54 68 65 20 64 69 72 65 63 74 20 61 73 73  * The direct ass
28319 69 67 6e 6d 65 6e 74 20 69 6e 20 74 68 65 20 70  ignment in the p
2831a 72 65 76 69 6f 75 73 20 6c 69 6e 65 20 69 73 20  revious line is 
2831b 70 6f 73 73 69 62 6c 65 20 6f 6e 6c 79 20 62 65  possible only be
2831c 63 61 75 73 65 0a 20 20 20 20 20 20 2a 2a 20 74  cause.      ** t
2831d 68 65 20 57 4f 5f 20 61 6e 64 20 53 51 4c 49 54  he WO_ and SQLIT
2831e 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
2831f 4e 54 5f 20 63 6f 64 65 73 20 61 72 65 20 69 64  NT_ codes are id
28320 65 6e 74 69 63 61 6c 2e 20 20 54 68 65 0a 20 20  entical.  The.  
28321 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67      ** following
28322 20 61 73 73 65 72 74 73 20 76 65 72 69 66 79 20   asserts verify 
28323 74 68 69 73 20 66 61 63 74 2e 20 2a 2f 0a 20 20  this fact. */.  
28324 20 20 20 20 61 73 73 65 72 74 28 20 57 4f 5f 45      assert( WO_E
28325 51 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  Q==SQLITE_INDEX_
28326 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20 29 3b  CONSTRAINT_EQ );
28327 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 57  .      assert( W
28328 4f 5f 4c 54 3d 3d 53 51 4c 49 54 45 5f 49 4e 44  O_LT==SQLITE_IND
28329 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54  EX_CONSTRAINT_LT
2832a 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
2832b 28 20 57 4f 5f 4c 45 3d 3d 53 51 4c 49 54 45 5f  ( WO_LE==SQLITE_
2832c 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54  INDEX_CONSTRAINT
2832d 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20 61 73 73  _LE );.      ass
2832e 65 72 74 28 20 57 4f 5f 47 54 3d 3d 53 51 4c 49  ert( WO_GT==SQLI
2832f 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
28330 49 4e 54 5f 47 54 20 29 3b 0a 20 20 20 20 20 20  INT_GT );.      
28331 61 73 73 65 72 74 28 20 57 4f 5f 47 45 3d 3d 53  assert( WO_GE==S
28332 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
28333 54 52 41 49 4e 54 5f 47 45 20 29 3b 0a 20 20 20  TRAINT_GE );.   
28334 20 20 20 61 73 73 65 72 74 28 20 57 4f 5f 4d 41     assert( WO_MA
28335 54 43 48 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45  TCH==SQLITE_INDE
28336 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4d 41 54  X_CONSTRAINT_MAT
28337 43 48 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  CH );.      asse
28338 72 74 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72  rt( pTerm->eOper
28339 61 74 6f 72 20 26 20 28 57 4f 5f 45 51 7c 57 4f  ator & (WO_EQ|WO
2833a 5f 4c 54 7c 57 4f 5f 4c 45 7c 57 4f 5f 47 54 7c  _LT|WO_LE|WO_GT|
2833b 57 4f 5f 47 45 7c 57 4f 5f 4d 41 54 43 48 29 20  WO_GE|WO_MATCH) 
2833c 29 3b 0a 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20  );.      j++;.  
2833d 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b    }.    for(i=0;
2833e 20 69 3c 6e 4f 72 64 65 72 42 79 3b 20 69 2b 2b   i<nOrderBy; i++
2833f 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  ){.      Expr *p
28340 45 78 70 72 20 3d 20 70 4f 72 64 65 72 42 79 2d  Expr = pOrderBy-
28341 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[i].pExpr;.   
28342 20 20 20 70 49 64 78 4f 72 64 65 72 42 79 5b 69     pIdxOrderBy[i
28343 5d 2e 69 43 6f 6c 75 6d 6e 20 3d 20 70 45 78 70  ].iColumn = pExp
28344 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  r->iColumn;.    
28345 20 20 70 49 64 78 4f 72 64 65 72 42 79 5b 69 5d    pIdxOrderBy[i]
28346 2e 64 65 73 63 20 3d 20 70 4f 72 64 65 72 42 79  .desc = pOrderBy
28347 2d 3e 61 5b 69 5d 2e 73 6f 72 74 4f 72 64 65 72  ->a[i].sortOrder
28348 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
28349 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c  * At this point,
2834a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64   the sqlite3_ind
2834b 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72  ex_info structur
2834c 65 20 74 68 61 74 20 70 49 64 78 49 6e 66 6f 20  e that pIdxInfo 
2834d 70 6f 69 6e 74 73 0a 20 20 2a 2a 20 74 6f 20 77  points.  ** to w
2834e 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 69 6e  ill have been in
2834f 69 74 69 61 6c 69 7a 65 64 2c 20 65 69 74 68 65  itialized, eithe
28350 72 20 64 75 72 69 6e 67 20 74 68 65 20 63 75 72  r during the cur
28351 72 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e 20  rent invocation 
28352 6f 72 0a 20 20 2a 2a 20 64 75 72 69 6e 67 20 73  or.  ** during s
28353 6f 6d 65 20 70 72 69 6f 72 20 69 6e 76 6f 63 61  ome prior invoca
28354 74 69 6f 6e 2e 20 20 4e 6f 77 20 77 65 20 6a 75  tion.  Now we ju
28355 73 74 20 68 61 76 65 20 74 6f 20 63 75 73 74 6f  st have to custo
28356 6d 69 7a 65 20 74 68 65 0a 20 20 2a 2a 20 64 65  mize the.  ** de
28357 74 61 69 6c 73 20 6f 66 20 70 49 64 78 49 6e 66  tails of pIdxInf
28358 6f 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e  o for the curren
28359 74 20 69 6e 76 6f 63 61 74 69 6f 6e 20 61 6e 64  t invocation and
2835a 20 70 61 73 73 20 69 74 20 74 6f 0a 20 20 2a 2a   pass it to.  **
2835b 20 78 42 65 73 74 49 6e 64 65 78 2e 0a 20 20 2a   xBestIndex..  *
2835c 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75  /..  /* The modu
2835d 6c 65 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20  le name must be 
2835e 64 65 66 69 6e 65 64 2e 20 41 6c 73 6f 2c 20 62  defined. Also, b
2835f 79 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65  y this point the
28360 72 65 20 6d 75 73 74 0a 20 20 2a 2a 20 62 65 20  re must.  ** be 
28361 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
28362 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72  sqlite3_vtab str
28363 75 63 74 75 72 65 2e 20 4f 74 68 65 72 77 69 73  ucture. Otherwis
28364 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 69  e.  ** sqlite3Vi
28365 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73  ewGetColumnNames
28366 28 29 20 77 6f 75 6c 64 20 68 61 76 65 20 70 69  () would have pi
28367 63 6b 65 64 20 75 70 20 74 68 65 20 65 72 72 6f  cked up the erro
28368 72 2e 20 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  r. .  */.  asser
28369 74 28 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c  t( pTab->azModul
2836a 65 41 72 67 20 26 26 20 70 54 61 62 2d 3e 61 7a  eArg && pTab->az
2836b 4d 6f 64 75 6c 65 41 72 67 5b 30 5d 20 29 3b 0a  ModuleArg[0] );.
2836c 20 20 61 73 73 65 72 74 28 20 70 56 74 61 62 20    assert( pVtab 
2836d 29 3b 0a 23 69 66 20 30 0a 20 20 69 66 28 20 70  );.#if 0.  if( p
2836e 54 61 62 2d 3e 70 56 74 61 62 3d 3d 30 20 29 7b  Tab->pVtab==0 ){
2836f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
28370 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75 6e  rMsg(pParse, "un
28371 64 65 66 69 6e 65 64 20 6d 6f 64 75 6c 65 20 25  defined module %
28372 73 20 66 6f 72 20 74 61 62 6c 65 20 25 73 22 2c  s for table %s",
28373 0a 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61  .        pTab->a
28374 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d 2c 20 70  zModuleArg[0], p
28375 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Tab->zName);.   
28376 20 72 65 74 75 72 6e 20 30 2e 30 3b 0a 20 20 7d   return 0.0;.  }
28377 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 53 65  .#endif..  /* Se
28378 74 20 74 68 65 20 61 43 6f 6e 73 74 72 61 69 6e  t the aConstrain
28379 74 5b 5d 2e 75 73 61 62 6c 65 20 66 69 65 6c 64  t[].usable field
2837a 73 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65  s and initialize
2837b 20 61 6c 6c 20 0a 20 20 2a 2a 20 6f 75 74 70 75   all .  ** outpu
2837c 74 20 76 61 72 69 61 62 6c 65 73 20 74 6f 20 7a  t variables to z
2837d 65 72 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61  ero..  **.  ** a
2837e 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 75 73 61  Constraint[].usa
2837f 62 6c 65 20 69 73 20 74 72 75 65 20 66 6f 72 20  ble is true for 
28380 63 6f 6e 73 74 72 61 69 6e 74 73 20 77 68 65 72  constraints wher
28381 65 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64  e the right-hand
28382 0a 20 20 2a 2a 20 73 69 64 65 20 63 6f 6e 74 61  .  ** side conta
28383 69 6e 73 20 6f 6e 6c 79 20 72 65 66 65 72 65 6e  ins only referen
28384 63 65 73 20 74 6f 20 74 61 62 6c 65 73 20 74 6f  ces to tables to
28385 20 74 68 65 20 6c 65 66 74 20 6f 66 20 74 68 65   the left of the
28386 20 63 75 72 72 65 6e 74 0a 20 20 2a 2a 20 74 61   current.  ** ta
28387 62 6c 65 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  ble.  In other w
28388 6f 72 64 73 2c 20 69 66 20 74 68 65 20 63 6f 6e  ords, if the con
28389 73 74 72 61 69 6e 74 20 69 73 20 6f 66 20 74 68  straint is of th
2838a 65 20 66 6f 72 6d 3a 0a 20 20 2a 2a 0a 20 20 2a  e form:.  **.  *
2838b 2a 20 20 20 20 20 20 20 20 20 20 20 63 6f 6c 75  *           colu
2838c 6d 6e 20 3d 20 65 78 70 72 0a 20 20 2a 2a 0a 20  mn = expr.  **. 
2838d 20 2a 2a 20 61 6e 64 20 77 65 20 61 72 65 20 65   ** and we are e
2838e 76 61 6c 75 61 74 69 6e 67 20 61 20 6a 6f 69 6e  valuating a join
2838f 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 73 74  , then the const
28390 72 61 69 6e 74 20 6f 6e 20 63 6f 6c 75 6d 6e 20  raint on column 
28391 69 73 20 0a 20 20 2a 2a 20 6f 6e 6c 79 20 76 61  is .  ** only va
28392 6c 69 64 20 69 66 20 61 6c 6c 20 74 61 62 6c 65  lid if all table
28393 73 20 72 65 66 65 72 65 6e 63 65 64 20 69 6e 20  s referenced in 
28394 65 78 70 72 20 6f 63 63 75 72 20 74 6f 20 74 68  expr occur to th
28395 65 20 6c 65 66 74 0a 20 20 2a 2a 20 6f 66 20 74  e left.  ** of t
28396 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e  he table contain
28397 69 6e 67 20 63 6f 6c 75 6d 6e 2e 0a 20 20 2a 2a  ing column..  **
28398 0a 20 20 2a 2a 20 54 68 65 20 61 43 6f 6e 73 74  .  ** The aConst
28399 72 61 69 6e 74 73 5b 5d 20 61 72 72 61 79 20 63  raints[] array c
2839a 6f 6e 74 61 69 6e 73 20 65 6e 74 72 69 65 73 20  ontains entries 
2839b 66 6f 72 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69  for all constrai
2839c 6e 74 73 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20  nts.  ** on the 
2839d 63 75 72 72 65 6e 74 20 74 61 62 6c 65 2e 20 20  current table.  
2839e 54 68 61 74 20 77 61 79 20 77 65 20 6f 6e 6c 79  That way we only
2839f 20 68 61 76 65 20 74 6f 20 63 6f 6d 70 75 74 65   have to compute
283a0 20 69 74 20 6f 6e 63 65 0a 20 20 2a 2a 20 65 76   it once.  ** ev
283a1 65 6e 20 74 68 6f 75 67 68 20 77 65 20 6d 69 67  en though we mig
283a2 68 74 20 74 72 79 20 74 6f 20 70 69 63 6b 20 74  ht try to pick t
283a3 68 65 20 62 65 73 74 20 69 6e 64 65 78 20 6d 75  he best index mu
283a4 6c 74 69 70 6c 65 20 74 69 6d 65 73 2e 0a 20 20  ltiple times..  
283a5 2a 2a 20 46 6f 72 20 65 61 63 68 20 61 74 74 65  ** For each atte
283a6 6d 70 74 20 61 74 20 70 69 63 6b 69 6e 67 20 61  mpt at picking a
283a7 6e 20 69 6e 64 65 78 2c 20 74 68 65 20 6f 72 64  n index, the ord
283a8 65 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20  er of tables in 
283a9 74 68 65 0a 20 20 2a 2a 20 6a 6f 69 6e 20 6d 69  the.  ** join mi
283aa 67 68 74 20 62 65 20 64 69 66 66 65 72 65 6e 74  ght be different
283ab 20 73 6f 20 77 65 20 68 61 76 65 20 74 6f 20 72   so we have to r
283ac 65 63 6f 6d 70 75 74 65 20 74 68 65 20 75 73 61  ecompute the usa
283ad 62 6c 65 20 66 6c 61 67 0a 20 20 2a 2a 20 65 61  ble flag.  ** ea
283ae 63 68 20 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20 20  ch time..  */.  
283af 70 49 64 78 43 6f 6e 73 20 3d 20 2a 28 73 74 72  pIdxCons = *(str
283b0 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
283b1 78 5f 63 6f 6e 73 74 72 61 69 6e 74 2a 2a 29 26  x_constraint**)&
283b2 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74  pIdxInfo->aConst
283b3 72 61 69 6e 74 3b 0a 20 20 70 55 73 61 67 65 20  raint;.  pUsage 
283b4 3d 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e  = pIdxInfo->aCon
283b5 73 74 72 61 69 6e 74 55 73 61 67 65 3b 0a 20 20  straintUsage;.  
283b6 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64 78 49  for(i=0; i<pIdxI
283b7 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74  nfo->nConstraint
283b8 3b 20 69 2b 2b 2c 20 70 49 64 78 43 6f 6e 73 2b  ; i++, pIdxCons+
283b9 2b 29 7b 0a 20 20 20 20 6a 20 3d 20 70 49 64 78  +){.    j = pIdx
283ba 43 6f 6e 73 2d 3e 69 54 65 72 6d 4f 66 66 73 65  Cons->iTermOffse
283bb 74 3b 0a 20 20 20 20 70 54 65 72 6d 20 3d 20 26  t;.    pTerm = &
283bc 70 57 43 2d 3e 61 5b 6a 5d 3b 0a 20 20 20 20 70  pWC->a[j];.    p
283bd 49 64 78 43 6f 6e 73 2d 3e 75 73 61 62 6c 65 20  IdxCons->usable 
283be 3d 20 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 65  =  (pTerm->prere
283bf 71 52 69 67 68 74 20 26 20 6e 6f 74 52 65 61 64  qRight & notRead
283c0 79 29 3d 3d 30 3b 0a 20 20 7d 0a 20 20 6d 65 6d  y)==0;.  }.  mem
283c1 73 65 74 28 70 55 73 61 67 65 2c 20 30 2c 20 73  set(pUsage, 0, s
283c2 69 7a 65 6f 66 28 70 55 73 61 67 65 5b 30 5d 29  izeof(pUsage[0])
283c3 2a 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73  *pIdxInfo->nCons
283c4 74 72 61 69 6e 74 29 3b 0a 20 20 69 66 28 20 70  traint);.  if( p
283c5 49 64 78 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46  IdxInfo->needToF
283c6 72 65 65 49 64 78 53 74 72 20 29 7b 0a 20 20 20  reeIdxStr ){.   
283c7 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 49   sqlite3_free(pI
283c8 64 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72 29 3b  dxInfo->idxStr);
283c9 0a 20 20 7d 0a 20 20 70 49 64 78 49 6e 66 6f 2d  .  }.  pIdxInfo-
283ca 3e 69 64 78 53 74 72 20 3d 20 30 3b 0a 20 20 70  >idxStr = 0;.  p
283cb 49 64 78 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20  IdxInfo->idxNum 
283cc 3d 20 30 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d  = 0;.  pIdxInfo-
283cd 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74  >needToFreeIdxSt
283ce 72 20 3d 20 30 3b 0a 20 20 70 49 64 78 49 6e 66  r = 0;.  pIdxInf
283cf 6f 2d 3e 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d  o->orderByConsum
283d0 65 64 20 3d 20 30 3b 0a 20 20 70 49 64 78 49 6e  ed = 0;.  pIdxIn
283d1 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73  fo->estimatedCos
283d2 74 20 3d 20 53 51 4c 49 54 45 5f 42 49 47 5f 44  t = SQLITE_BIG_D
283d3 42 4c 20 2f 20 32 2e 30 3b 0a 20 20 6e 4f 72 64  BL / 2.0;.  nOrd
283d4 65 72 42 79 20 3d 20 70 49 64 78 49 6e 66 6f 2d  erBy = pIdxInfo-
283d5 3e 6e 4f 72 64 65 72 42 79 3b 0a 20 20 69 66 28  >nOrderBy;.  if(
283d6 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65   pIdxInfo->nOrde
283d7 72 42 79 20 26 26 20 21 6f 72 64 65 72 42 79 55  rBy && !orderByU
283d8 73 61 62 6c 65 20 29 7b 0a 20 20 20 20 2a 28 69  sable ){.    *(i
283d9 6e 74 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 6e  nt*)&pIdxInfo->n
283da 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 7d  OrderBy = 0;.  }
283db 0a 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65  ..  (void)sqlite
283dc 33 53 61 66 65 74 79 4f 66 66 28 70 50 61 72 73  3SafetyOff(pPars
283dd 65 2d 3e 64 62 29 3b 0a 20 20 57 48 45 52 45 54  e->db);.  WHERET
283de 52 41 43 45 28 28 22 78 42 65 73 74 49 6e 64 65  RACE(("xBestInde
283df 78 20 66 6f 72 20 25 73 5c 6e 22 2c 20 70 54 61  x for %s\n", pTa
283e0 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 54 52  b->zName));.  TR
283e1 41 43 45 5f 49 44 58 5f 49 4e 50 55 54 53 28 70  ACE_IDX_INPUTS(p
283e2 49 64 78 49 6e 66 6f 29 3b 0a 20 20 72 63 20 3d  IdxInfo);.  rc =
283e3 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d   pVtab->pModule-
283e4 3e 78 42 65 73 74 49 6e 64 65 78 28 70 56 74 61  >xBestIndex(pVta
283e5 62 2c 20 70 49 64 78 49 6e 66 6f 29 3b 0a 20 20  b, pIdxInfo);.  
283e6 54 52 41 43 45 5f 49 44 58 5f 4f 55 54 50 55 54  TRACE_IDX_OUTPUT
283e7 53 28 70 49 64 78 49 6e 66 6f 29 3b 0a 20 20 28  S(pIdxInfo);.  (
283e8 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
283e9 74 79 4f 6e 28 70 50 61 72 73 65 2d 3e 64 62 29  tyOn(pParse->db)
283ea 3b 0a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  ;..  if( rc!=SQL
283eb 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66  ITE_OK ){.    if
283ec 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
283ed 45 4d 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72  EM ){.      pPar
283ee 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  se->db->mallocFa
283ef 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 65  iled = 1;.    }e
283f0 6c 73 65 20 69 66 28 20 21 70 56 74 61 62 2d 3e  lse if( !pVtab->
283f1 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20  zErrMsg ){.     
283f2 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
283f3 28 70 50 61 72 73 65 2c 20 22 25 73 22 2c 20 73  (pParse, "%s", s
283f4 71 6c 69 74 65 33 45 72 72 53 74 72 28 72 63 29  qlite3ErrStr(rc)
283f5 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
283f6 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
283f7 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 22  Msg(pParse, "%s"
283f8 2c 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67  , pVtab->zErrMsg
283f9 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  );.    }.  }.  s
283fa 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 50 61  qlite3DbFree(pPa
283fb 72 73 65 2d 3e 64 62 2c 20 70 56 74 61 62 2d 3e  rse->db, pVtab->
283fc 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 56 74 61  zErrMsg);.  pVta
283fd 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a  b->zErrMsg = 0;.
283fe 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49  .  for(i=0; i<pI
283ff 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61  dxInfo->nConstra
28400 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  int; i++){.    i
28401 66 28 20 21 70 49 64 78 49 6e 66 6f 2d 3e 61 43  f( !pIdxInfo->aC
28402 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 75 73 61  onstraint[i].usa
28403 62 6c 65 20 26 26 20 70 55 73 61 67 65 5b 69 5d  ble && pUsage[i]
28404 2e 61 72 67 76 49 6e 64 65 78 3e 30 20 29 7b 0a  .argvIndex>0 ){.
28405 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
28406 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20  orMsg(pParse, . 
28407 20 20 20 20 20 20 20 20 20 22 74 61 62 6c 65 20           "table 
28408 25 73 3a 20 78 42 65 73 74 49 6e 64 65 78 20 72  %s: xBestIndex r
28409 65 74 75 72 6e 65 64 20 61 6e 20 69 6e 76 61 6c  eturned an inval
2840a 69 64 20 70 6c 61 6e 22 2c 20 70 54 61 62 2d 3e  id plan", pTab->
2840b 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65  zName);.      re
2840c 74 75 72 6e 20 30 2e 30 3b 0a 20 20 20 20 7d 0a  turn 0.0;.    }.
2840d 20 20 7d 0a 0a 20 20 2a 28 69 6e 74 2a 29 26 70    }..  *(int*)&p
2840e 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42  IdxInfo->nOrderB
2840f 79 20 3d 20 6e 4f 72 64 65 72 42 79 3b 0a 20 20  y = nOrderBy;.  
28410 72 65 74 75 72 6e 20 70 49 64 78 49 6e 66 6f 2d  return pIdxInfo-
28411 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74 3b 0a  >estimatedCost;.
28412 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
28413 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
28414 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46  ABLE */../*.** F
28415 69 6e 64 20 74 68 65 20 62 65 73 74 20 69 6e 64  ind the best ind
28416 65 78 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67  ex for accessing
28417 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 61   a particular ta
28418 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 61 20 70  ble.  Return a p
28419 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65  ointer.** to the
2841a 20 69 6e 64 65 78 2c 20 66 6c 61 67 73 20 74 68   index, flags th
2841b 61 74 20 64 65 73 63 72 69 62 65 20 68 6f 77 20  at describe how 
2841c 74 68 65 20 69 6e 64 65 78 20 73 68 6f 75 6c 64  the index should
2841d 20 62 65 20 75 73 65 64 2c 20 74 68 65 0a 2a 2a   be used, the.**
2841e 20 6e 75 6d 62 65 72 20 6f 66 20 65 71 75 61 6c   number of equal
2841f 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c  ity constraints,
28420 20 61 6e 64 20 74 68 65 20 22 63 6f 73 74 22 20   and the "cost" 
28421 66 6f 72 20 74 68 69 73 20 69 6e 64 65 78 2e 0a  for this index..
28422 2a 2a 0a 2a 2a 20 54 68 65 20 6c 6f 77 65 73 74  **.** The lowest
28423 20 63 6f 73 74 20 69 6e 64 65 78 20 77 69 6e 73   cost index wins
28424 2e 20 20 54 68 65 20 63 6f 73 74 20 69 73 20 61  .  The cost is a
28425 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68  n estimate of th
28426 65 20 61 6d 6f 75 6e 74 20 6f 66 0a 2a 2a 20 43  e amount of.** C
28427 50 55 20 61 6e 64 20 64 69 73 6b 20 49 2f 4f 20  PU and disk I/O 
28428 6e 65 65 64 20 74 6f 20 70 72 6f 63 65 73 73 20  need to process 
28429 74 68 65 20 72 65 71 75 65 73 74 20 75 73 69 6e  the request usin
2842a 67 20 74 68 65 20 73 65 6c 65 63 74 65 64 20 69  g the selected i
2842b 6e 64 65 78 2e 0a 2a 2a 20 46 61 63 74 6f 72 73  ndex..** Factors
2842c 20 74 68 61 74 20 69 6e 66 6c 75 65 6e 63 65 20   that influence 
2842d 63 6f 73 74 20 69 6e 63 6c 75 64 65 3a 0a 2a 2a  cost include:.**
2842e 0a 2a 2a 20 20 20 20 2a 20 20 54 68 65 20 65 73  .**    *  The es
2842f 74 69 6d 61 74 65 64 20 6e 75 6d 62 65 72 20 6f  timated number o
28430 66 20 72 6f 77 73 20 74 68 61 74 20 77 69 6c 6c  f rows that will
28431 20 62 65 20 72 65 74 72 69 65 76 65 64 2e 20 20   be retrieved.  
28432 28 54 68 65 0a 2a 2a 20 20 20 20 20 20 20 66 65  (The.**       fe
28433 77 65 72 20 74 68 65 20 62 65 74 74 65 72 2e 29  wer the better.)
28434 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 57 68 65  .**.**    *  Whe
28435 74 68 65 72 20 6f 72 20 6e 6f 74 20 73 6f 72 74  ther or not sort
28436 69 6e 67 20 6d 75 73 74 20 6f 63 63 75 72 2e 0a  ing must occur..
28437 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 57 68 65 74  **.**    *  Whet
28438 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 72 65  her or not there
28439 20 6d 75 73 74 20 62 65 20 73 65 70 61 72 61 74   must be separat
2843a 65 20 6c 6f 6f 6b 75 70 73 20 69 6e 20 74 68 65  e lookups in the
2843b 0a 2a 2a 20 20 20 20 20 20 20 69 6e 64 65 78 20  .**       index 
2843c 61 6e 64 20 69 6e 20 74 68 65 20 6d 61 69 6e 20  and in the main 
2843d 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  table..**.** If 
2843e 74 68 65 72 65 20 77 61 73 20 61 6e 20 49 4e 44  there was an IND
2843f 45 58 45 44 20 42 59 20 63 6c 61 75 73 65 20 61  EXED BY clause a
28440 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 74  ttached to the t
28441 61 62 6c 65 20 69 6e 20 74 68 65 20 53 45 4c 45  able in the SELE
28442 43 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2c  CT.** statement,
28443 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   then this funct
28444 69 6f 6e 20 6f 6e 6c 79 20 63 6f 6e 73 69 64 65  ion only conside
28445 72 73 20 73 74 72 61 74 65 67 69 65 73 20 75 73  rs strategies us
28446 69 6e 67 20 74 68 65 20 0a 2a 2a 20 6e 61 6d 65  ing the .** name
28447 64 20 69 6e 64 65 78 2e 20 49 66 20 6f 6e 65 20  d index. If one 
28448 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2c  cannot be found,
28449 20 74 68 65 6e 20 74 68 65 20 72 65 74 75 72 6e   then the return
2844a 65 64 20 63 6f 73 74 20 69 73 0a 2a 2a 20 53 51  ed cost is.** SQ
2844b 4c 49 54 45 5f 42 49 47 5f 44 42 4c 2e 20 49 66  LITE_BIG_DBL. If
2844c 20 61 20 73 74 72 61 74 65 67 79 20 63 61 6e 20   a strategy can 
2844d 62 65 20 66 6f 75 6e 64 20 74 68 61 74 20 75 73  be found that us
2844e 65 73 20 74 68 65 20 6e 61 6d 65 64 20 69 6e 64  es the named ind
2844f 65 78 2c 20 0a 2a 2a 20 74 68 65 6e 20 74 68 65  ex, .** then the
28450 20 63 6f 73 74 20 69 73 20 63 61 6c 63 75 6c 61   cost is calcula
28451 74 65 64 20 69 6e 20 74 68 65 20 75 73 75 61 6c  ted in the usual
28452 20 77 61 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61   way..**.** If a
28453 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c 61   NOT INDEXED cla
28454 75 73 65 20 77 61 73 20 61 74 74 61 63 68 65 64  use was attached
28455 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 69 6e   to the table in
28456 20 74 68 65 20 53 45 4c 45 43 54 20 0a 2a 2a 20   the SELECT .** 
28457 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20  statement, then 
28458 6e 6f 20 69 6e 64 65 78 65 73 20 61 72 65 20 63  no indexes are c
28459 6f 6e 73 69 64 65 72 65 64 2e 20 48 6f 77 65 76  onsidered. Howev
2845a 65 72 2c 20 74 68 65 20 73 65 6c 65 63 74 65 64  er, the selected
2845b 20 0a 2a 2a 20 73 74 61 74 65 67 79 20 6d 61 79   .** stategy may
2845c 20 73 74 69 6c 6c 20 74 61 6b 65 20 61 64 76 61   still take adva
2845d 6e 74 61 67 65 20 6f 66 20 74 68 65 20 74 61 62  ntage of the tab
2845e 6c 65 73 20 62 75 69 6c 74 2d 69 6e 20 72 6f 77  les built-in row
2845f 69 64 0a 2a 2a 20 69 6e 64 65 78 2e 0a 2a 2f 0a  id.** index..*/.
28460 73 74 61 74 69 63 20 64 6f 75 62 6c 65 20 62 65  static double be
28461 73 74 49 6e 64 65 78 28 0a 20 20 50 61 72 73 65  stIndex(.  Parse
28462 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
28463 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
28464 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
28465 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a  .  WhereClause *
28466 70 57 43 2c 20 20 20 20 20 20 20 20 20 20 20 2f  pWC,           /
28467 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75  * The WHERE clau
28468 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53  se */.  struct S
28469 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 72  rcList_item *pSr
2846a 63 2c 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20  c,  /* The FROM 
2846b 63 6c 61 75 73 65 20 74 65 72 6d 20 74 6f 20 73  clause term to s
2846c 65 61 72 63 68 20 2a 2f 0a 20 20 42 69 74 6d 61  earch */.  Bitma
2846d 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20  sk notReady,    
2846e 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f         /* Mask o
2846f 66 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 61  f cursors that a
28470 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65  re not available
28471 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
28472 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 20 20  pOrderBy,       
28473 20 20 2f 2a 20 54 68 65 20 6f 72 64 65 72 20 62    /* The order b
28474 79 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 49 6e  y clause */.  In
28475 64 65 78 20 2a 2a 70 70 49 6e 64 65 78 2c 20 20  dex **ppIndex,  
28476 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b            /* Mak
28477 65 20 2a 70 70 49 6e 64 65 78 20 70 6f 69 6e 74  e *ppIndex point
28478 20 74 6f 20 74 68 65 20 62 65 73 74 20 69 6e 64   to the best ind
28479 65 78 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 46 6c  ex */.  int *pFl
2847a 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  ags,            
2847b 20 20 20 20 2f 2a 20 50 75 74 20 66 6c 61 67 73      /* Put flags
2847c 20 64 65 73 63 72 69 62 69 6e 67 20 74 68 69 73   describing this
2847d 20 63 68 6f 69 63 65 20 69 6e 20 2a 70 46 6c 61   choice in *pFla
2847e 67 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 45  gs */.  int *pnE
2847f 71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  q               
28480 20 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20 6e      /* Put the n
28481 75 6d 62 65 72 20 6f 66 20 3d 3d 20 6f 72 20 49  umber of == or I
28482 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 68 65  N constraints he
28483 72 65 20 2a 2f 0a 29 7b 0a 20 20 57 68 65 72 65  re */.){.  Where
28484 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 49  Term *pTerm;.  I
28485 6e 64 65 78 20 2a 62 65 73 74 49 64 78 20 3d 20  ndex *bestIdx = 
28486 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  0;         /* In
28487 64 65 78 20 74 68 61 74 20 67 69 76 65 73 20 74  dex that gives t
28488 68 65 20 6c 6f 77 65 73 74 20 63 6f 73 74 20 2a  he lowest cost *
28489 2f 0a 20 20 64 6f 75 62 6c 65 20 6c 6f 77 65 73  /.  double lowes
2848a 74 43 6f 73 74 3b 20 20 20 20 20 20 20 20 20 20  tCost;          
2848b 2f 2a 20 54 68 65 20 63 6f 73 74 20 6f 66 20 75  /* The cost of u
2848c 73 69 6e 67 20 62 65 73 74 49 64 78 20 2a 2f 0a  sing bestIdx */.
2848d 20 20 69 6e 74 20 62 65 73 74 46 6c 61 67 73 20    int bestFlags 
2848e 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  = 0;          /*
2848f 20 46 6c 61 67 73 20 61 73 73 6f 63 69 61 74 65   Flags associate
28490 64 20 77 69 74 68 20 62 65 73 74 49 64 78 20 2a  d with bestIdx *
28491 2f 0a 20 20 69 6e 74 20 62 65 73 74 4e 45 71 20  /.  int bestNEq 
28492 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
28493 2f 2a 20 42 65 73 74 20 76 61 6c 75 65 20 66 6f  /* Best value fo
28494 72 20 6e 45 71 20 2a 2f 0a 20 20 69 6e 74 20 69  r nEq */.  int i
28495 43 75 72 20 3d 20 70 53 72 63 2d 3e 69 43 75 72  Cur = pSrc->iCur
28496 73 6f 72 3b 20 20 20 2f 2a 20 54 68 65 20 63 75  sor;   /* The cu
28497 72 73 6f 72 20 6f 66 20 74 68 65 20 74 61 62 6c  rsor of the tabl
28498 65 20 74 6f 20 62 65 20 61 63 63 65 73 73 65 64  e to be accessed
28499 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 50 72   */.  Index *pPr
2849a 6f 62 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  obe;            
2849b 20 20 2f 2a 20 41 6e 20 69 6e 64 65 78 20 77 65    /* An index we
2849c 20 61 72 65 20 65 76 61 6c 75 61 74 69 6e 67 20   are evaluating 
2849d 2a 2f 0a 20 20 69 6e 74 20 72 65 76 3b 20 20 20  */.  int rev;   
2849e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2849f 20 2f 2a 20 54 72 75 65 20 74 6f 20 73 63 61 6e   /* True to scan
284a0 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65   in reverse orde
284a1 72 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  r */.  int flags
284a2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
284a3 20 20 20 2f 2a 20 46 6c 61 67 73 20 61 73 73 6f     /* Flags asso
284a4 63 69 61 74 65 64 20 77 69 74 68 20 70 50 72 6f  ciated with pPro
284a5 62 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 71 3b  be */.  int nEq;
284a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
284a7 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
284a8 20 3d 3d 20 6f 72 20 49 4e 20 63 6f 6e 73 74 72   == or IN constr
284a9 61 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 65  aints */.  int e
284aa 71 54 65 72 6d 4d 61 73 6b 3b 20 20 20 20 20 20  qTermMask;      
284ab 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f         /* Mask o
284ac 66 20 76 61 6c 69 64 20 65 71 75 61 6c 69 74 79  f valid equality
284ad 20 6f 70 65 72 61 74 6f 72 73 20 2a 2f 0a 20 20   operators */.  
284ae 64 6f 75 62 6c 65 20 63 6f 73 74 3b 20 20 20 20  double cost;    
284af 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
284b0 6f 73 74 20 6f 66 20 75 73 69 6e 67 20 70 50 72  ost of using pPr
284b1 6f 62 65 20 2a 2f 0a 0a 20 20 57 48 45 52 45 54  obe */..  WHERET
284b2 52 41 43 45 28 28 22 62 65 73 74 49 6e 64 65 78  RACE(("bestIndex
284b3 3a 20 74 62 6c 3d 25 73 20 6e 6f 74 52 65 61 64  : tbl=%s notRead
284b4 79 3d 25 6c 6c 78 5c 6e 22 2c 20 70 53 72 63 2d  y=%llx\n", pSrc-
284b5 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 6e 6f  >pTab->zName, no
284b6 74 52 65 61 64 79 29 29 3b 0a 20 20 6c 6f 77 65  tReady));.  lowe
284b7 73 74 43 6f 73 74 20 3d 20 53 51 4c 49 54 45 5f  stCost = SQLITE_
284b8 42 49 47 5f 44 42 4c 3b 0a 20 20 70 50 72 6f 62  BIG_DBL;.  pProb
284b9 65 20 3d 20 70 53 72 63 2d 3e 70 54 61 62 2d 3e  e = pSrc->pTab->
284ba 70 49 6e 64 65 78 3b 0a 20 20 69 66 28 20 70 53  pIndex;.  if( pS
284bb 72 63 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 20 29  rc->notIndexed )
284bc 7b 0a 20 20 20 20 70 50 72 6f 62 65 20 3d 20 30  {.    pProbe = 0
284bd 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
284be 68 65 20 74 61 62 6c 65 20 68 61 73 20 6e 6f 20  he table has no 
284bf 69 6e 64 69 63 65 73 20 61 6e 64 20 74 68 65 72  indices and ther
284c0 65 20 61 72 65 20 6e 6f 20 74 65 72 6d 73 20 69  e are no terms i
284c1 6e 20 74 68 65 20 77 68 65 72 65 0a 20 20 2a 2a  n the where.  **
284c2 20 63 6c 61 75 73 65 20 74 68 61 74 20 72 65 66   clause that ref
284c3 65 72 20 74 6f 20 74 68 65 20 52 4f 57 49 44 2c  er to the ROWID,
284c4 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 6e 65   then we will ne
284c5 76 65 72 20 62 65 20 61 62 6c 65 20 74 6f 20 64  ver be able to d
284c6 6f 0a 20 20 2a 2a 20 61 6e 79 74 68 69 6e 67 20  o.  ** anything 
284c7 6f 74 68 65 72 20 74 68 61 6e 20 61 20 66 75 6c  other than a ful
284c8 6c 20 74 61 62 6c 65 20 73 63 61 6e 20 6f 6e 20  l table scan on 
284c9 74 68 69 73 20 74 61 62 6c 65 2e 20 20 57 65 20  this table.  We 
284ca 6d 69 67 68 74 20 61 73 0a 20 20 2a 2a 20 77 65  might as.  ** we
284cb 6c 6c 20 70 75 74 20 69 74 20 66 69 72 73 74 20  ll put it first 
284cc 69 6e 20 74 68 65 20 6a 6f 69 6e 20 6f 72 64 65  in the join orde
284cd 72 2e 20 20 54 68 61 74 20 77 61 79 2c 20 70 65  r.  That way, pe
284ce 72 68 61 70 73 20 69 74 20 63 61 6e 20 62 65 0a  rhaps it can be.
284cf 20 20 2a 2a 20 72 65 66 65 72 65 6e 63 65 64 20    ** referenced 
284d0 62 79 20 6f 74 68 65 72 20 74 61 62 6c 65 73 20  by other tables 
284d1 69 6e 20 74 68 65 20 6a 6f 69 6e 2e 0a 20 20 2a  in the join..  *
284d2 2f 0a 20 20 69 66 28 20 70 50 72 6f 62 65 3d 3d  /.  if( pProbe==
284d3 30 20 26 26 0a 20 20 20 20 20 66 69 6e 64 54 65  0 &&.     findTe
284d4 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 2d 31  rm(pWC, iCur, -1
284d5 2c 20 30 2c 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e  , 0, WO_EQ|WO_IN
284d6 7c 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 7c 57 4f 5f  |WO_LT|WO_LE|WO_
284d7 47 54 7c 57 4f 5f 47 45 2c 30 29 3d 3d 30 20 26  GT|WO_GE,0)==0 &
284d8 26 0a 20 20 20 20 20 28 70 4f 72 64 65 72 42 79  &.     (pOrderBy
284d9 3d 3d 30 20 7c 7c 20 21 73 6f 72 74 61 62 6c 65  ==0 || !sortable
284da 42 79 52 6f 77 69 64 28 69 43 75 72 2c 20 70 4f  ByRowid(iCur, pO
284db 72 64 65 72 42 79 2c 20 70 57 43 2d 3e 70 4d 61  rderBy, pWC->pMa
284dc 73 6b 53 65 74 2c 20 26 72 65 76 29 29 20 29 7b  skSet, &rev)) ){
284dd 0a 20 20 20 20 2a 70 46 6c 61 67 73 20 3d 20 30  .    *pFlags = 0
284de 3b 0a 20 20 20 20 2a 70 70 49 6e 64 65 78 20 3d  ;.    *ppIndex =
284df 20 30 3b 0a 20 20 20 20 2a 70 6e 45 71 20 3d 20   0;.    *pnEq = 
284e0 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 2e  0;.    return 0.
284e1 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65  0;.  }..  /* Che
284e2 63 6b 20 66 6f 72 20 61 20 72 6f 77 69 64 3d 45  ck for a rowid=E
284e3 58 50 52 20 6f 72 20 72 6f 77 69 64 20 49 4e 20  XPR or rowid IN 
284e4 28 2e 2e 2e 29 20 63 6f 6e 73 74 72 61 69 6e 74  (...) constraint
284e5 73 2e 20 49 66 20 74 68 65 72 65 20 77 61 73 0a  s. If there was.
284e6 20 20 2a 2a 20 61 6e 20 49 4e 44 45 58 45 44 20    ** an INDEXED 
284e7 42 59 20 63 6c 61 75 73 65 20 61 74 74 61 63 68  BY clause attach
284e8 65 64 20 74 6f 20 74 68 69 73 20 74 61 62 6c 65  ed to this table
284e9 2c 20 73 6b 69 70 20 74 68 69 73 20 73 74 65 70  , skip this step
284ea 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 53  ..  */.  if( !pS
284eb 72 63 2d 3e 70 49 6e 64 65 78 20 29 7b 0a 20 20  rc->pIndex ){.  
284ec 20 20 70 54 65 72 6d 20 3d 20 66 69 6e 64 54 65    pTerm = findTe
284ed 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 2d 31  rm(pWC, iCur, -1
284ee 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 45  , notReady, WO_E
284ef 51 7c 57 4f 5f 49 4e 2c 20 30 29 3b 0a 20 20 20  Q|WO_IN, 0);.   
284f0 20 69 66 28 20 70 54 65 72 6d 20 29 7b 0a 20 20   if( pTerm ){.  
284f1 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b      Expr *pExpr;
284f2 0a 20 20 20 20 20 20 2a 70 70 49 6e 64 65 78 20  .      *ppIndex 
284f3 3d 20 30 3b 0a 20 20 20 20 20 20 62 65 73 74 46  = 0;.      bestF
284f4 6c 61 67 73 20 3d 20 57 48 45 52 45 5f 52 4f 57  lags = WHERE_ROW
284f5 49 44 5f 45 51 3b 0a 20 20 20 20 20 20 69 66 28  ID_EQ;.      if(
284f6 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
284f7 72 20 26 20 57 4f 5f 45 51 20 29 7b 0a 20 20 20  r & WO_EQ ){.   
284f8 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 3d 3d 20       /* Rowid== 
284f9 69 73 20 61 6c 77 61 79 73 20 74 68 65 20 62 65  is always the be
284fa 73 74 20 70 69 63 6b 2e 20 20 4c 6f 6f 6b 20 6e  st pick.  Look n
284fb 6f 20 66 75 72 74 68 65 72 2e 20 20 42 65 63 61  o further.  Beca
284fc 75 73 65 20 6f 6e 6c 79 0a 20 20 20 20 20 20 20  use only.       
284fd 20 2a 2a 20 61 20 73 69 6e 67 6c 65 20 72 6f 77   ** a single row
284fe 20 69 73 20 67 65 6e 65 72 61 74 65 64 2c 20 6f   is generated, o
284ff 75 74 70 75 74 20 69 73 20 61 6c 77 61 79 73 20  utput is always 
28500 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 20  in sorted order 
28501 2a 2f 0a 20 20 20 20 20 20 20 20 2a 70 46 6c 61  */.        *pFla
28502 67 73 20 3d 20 57 48 45 52 45 5f 52 4f 57 49 44  gs = WHERE_ROWID
28503 5f 45 51 20 7c 20 57 48 45 52 45 5f 55 4e 49 51  _EQ | WHERE_UNIQ
28504 55 45 3b 0a 20 20 20 20 20 20 20 20 2a 70 6e 45  UE;.        *pnE
28505 71 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 57  q = 1;.        W
28506 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 20  HERETRACE(("... 
28507 62 65 73 74 20 69 73 20 72 6f 77 69 64 5c 6e 22  best is rowid\n"
28508 29 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  ));.        retu
28509 72 6e 20 30 2e 30 3b 0a 20 20 20 20 20 20 7d 65  rn 0.0;.      }e
2850a 6c 73 65 20 69 66 28 20 28 70 45 78 70 72 20 3d  lse if( (pExpr =
2850b 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 29 2d 3e   pTerm->pExpr)->
2850c 70 4c 69 73 74 21 3d 30 20 29 7b 0a 20 20 20 20  pList!=0 ){.    
2850d 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 49 4e 20      /* Rowid IN 
2850e 28 4c 49 53 54 29 3a 20 63 6f 73 74 20 69 73 20  (LIST): cost is 
2850f 4e 6c 6f 67 4e 20 77 68 65 72 65 20 4e 20 69 73  NlogN where N is
28510 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c   the number of l
28511 69 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 65  ist.        ** e
28512 6c 65 6d 65 6e 74 73 2e 20 20 2a 2f 0a 20 20 20  lements.  */.   
28513 20 20 20 20 20 6c 6f 77 65 73 74 43 6f 73 74 20       lowestCost 
28514 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e  = pExpr->pList->
28515 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 6c  nExpr;.        l
28516 6f 77 65 73 74 43 6f 73 74 20 2a 3d 20 65 73 74  owestCost *= est
28517 4c 6f 67 28 6c 6f 77 65 73 74 43 6f 73 74 29 3b  Log(lowestCost);
28518 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
28519 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 49        /* Rowid I
2851a 4e 20 28 53 45 4c 45 43 54 29 3a 20 63 6f 73 74  N (SELECT): cost
2851b 20 69 73 20 4e 6c 6f 67 4e 20 77 68 65 72 65 20   is NlogN where 
2851c 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  N is the number 
2851d 6f 66 20 72 6f 77 73 0a 20 20 20 20 20 20 20 20  of rows.        
2851e 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ** in the result
2851f 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 20 73 65   of the inner se
28520 6c 65 63 74 2e 20 20 57 65 20 68 61 76 65 20 6e  lect.  We have n
28521 6f 20 77 61 79 20 74 6f 20 65 73 74 69 6d 61 74  o way to estimat
28522 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61  e.        ** tha
28523 74 20 76 61 6c 75 65 20 73 6f 20 6d 61 6b 65 20  t value so make 
28524 61 20 77 69 6c 64 20 67 75 65 73 73 2e 20 2a 2f  a wild guess. */
28525 0a 20 20 20 20 20 20 20 20 6c 6f 77 65 73 74 43  .        lowestC
28526 6f 73 74 20 3d 20 32 30 30 3b 0a 20 20 20 20 20  ost = 200;.     
28527 20 7d 0a 20 20 20 20 20 20 57 48 45 52 45 54 52   }.      WHERETR
28528 41 43 45 28 28 22 2e 2e 2e 20 72 6f 77 69 64 20  ACE(("... rowid 
28529 49 4e 20 63 6f 73 74 3a 20 25 2e 39 67 5c 6e 22  IN cost: %.9g\n"
2852a 2c 20 6c 6f 77 65 73 74 43 6f 73 74 29 29 3b 0a  , lowestCost));.
2852b 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20      }.  .    /* 
2852c 45 73 74 69 6d 61 74 65 20 74 68 65 20 63 6f 73  Estimate the cos
2852d 74 20 6f 66 20 61 20 74 61 62 6c 65 20 73 63 61  t of a table sca
2852e 6e 2e 20 20 49 66 20 77 65 20 64 6f 20 6e 6f 74  n.  If we do not
2852f 20 6b 6e 6f 77 20 68 6f 77 20 6d 61 6e 79 0a 20   know how many. 
28530 20 20 20 2a 2a 20 65 6e 74 72 69 65 73 20 61 72     ** entries ar
28531 65 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20  e in the table, 
28532 75 73 65 20 31 20 6d 69 6c 6c 69 6f 6e 20 61 73  use 1 million as
28533 20 61 20 67 75 65 73 73 2e 0a 20 20 20 20 2a 2f   a guess..    */
28534 0a 20 20 20 20 63 6f 73 74 20 3d 20 70 50 72 6f  .    cost = pPro
28535 62 65 20 3f 20 70 50 72 6f 62 65 2d 3e 61 69 52  be ? pProbe->aiR
28536 6f 77 45 73 74 5b 30 5d 20 3a 20 31 30 30 30 30  owEst[0] : 10000
28537 30 30 3b 0a 20 20 20 20 57 48 45 52 45 54 52 41  00;.    WHERETRA
28538 43 45 28 28 22 2e 2e 2e 20 74 61 62 6c 65 20 73  CE(("... table s
28539 63 61 6e 20 62 61 73 65 20 63 6f 73 74 3a 20 25  can base cost: %
2853a 2e 39 67 5c 6e 22 2c 20 63 6f 73 74 29 29 3b 0a  .9g\n", cost));.
2853b 20 20 20 20 66 6c 61 67 73 20 3d 20 57 48 45 52      flags = WHER
2853c 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 3b 0a 20  E_ROWID_RANGE;. 
2853d 20 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 66   .    /* Check f
2853e 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f  or constraints o
2853f 6e 20 61 20 72 61 6e 67 65 20 6f 66 20 72 6f 77  n a range of row
28540 69 64 73 20 69 6e 20 61 20 74 61 62 6c 65 20 73  ids in a table s
28541 63 61 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  can..    */.    
28542 70 54 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d  pTerm = findTerm
28543 28 70 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20  (pWC, iCur, -1, 
28544 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 4c 54 7c  notReady, WO_LT|
28545 57 4f 5f 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47  WO_LE|WO_GT|WO_G
28546 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70  E, 0);.    if( p
28547 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 69 66  Term ){.      if
28548 28 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20  ( findTerm(pWC, 
28549 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61  iCur, -1, notRea
2854a 64 79 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 2c  dy, WO_LT|WO_LE,
2854b 20 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 66   0) ){.        f
2854c 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 54 4f  lags |= WHERE_TO
2854d 50 5f 4c 49 4d 49 54 3b 0a 20 20 20 20 20 20 20  P_LIMIT;.       
2854e 20 63 6f 73 74 20 2f 3d 20 33 3b 20 20 2f 2a 20   cost /= 3;  /* 
2854f 47 75 65 73 73 20 74 68 61 74 20 72 6f 77 69 64  Guess that rowid
28550 3c 45 58 50 52 20 65 6c 69 6d 69 6e 61 74 65 73  <EXPR eliminates
28551 20 74 77 6f 2d 74 68 69 72 64 73 20 6f 72 20 72   two-thirds or r
28552 6f 77 73 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20  ows */.      }. 
28553 20 20 20 20 20 69 66 28 20 66 69 6e 64 54 65 72       if( findTer
28554 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c  m(pWC, iCur, -1,
28555 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 47 54   notReady, WO_GT
28556 7c 57 4f 5f 47 45 2c 20 30 29 20 29 7b 0a 20 20  |WO_GE, 0) ){.  
28557 20 20 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 57        flags |= W
28558 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 3b 0a  HERE_BTM_LIMIT;.
28559 20 20 20 20 20 20 20 20 63 6f 73 74 20 2f 3d 20          cost /= 
2855a 33 3b 20 20 2f 2a 20 47 75 65 73 73 20 74 68 61  3;  /* Guess tha
2855b 74 20 72 6f 77 69 64 3e 45 58 50 52 20 65 6c 69  t rowid>EXPR eli
2855c 6d 69 6e 61 74 65 73 20 74 77 6f 2d 74 68 69 72  minates two-thir
2855d 64 73 20 6f 66 20 72 6f 77 73 20 2a 2f 0a 20 20  ds of rows */.  
2855e 20 20 20 20 7d 0a 20 20 20 20 20 20 57 48 45 52      }.      WHER
2855f 45 54 52 41 43 45 28 28 22 2e 2e 2e 20 72 6f 77  ETRACE(("... row
28560 69 64 20 72 61 6e 67 65 20 72 65 64 75 63 65 73  id range reduces
28561 20 63 6f 73 74 20 74 6f 20 25 2e 39 67 5c 6e 22   cost to %.9g\n"
28562 2c 20 63 6f 73 74 29 29 3b 0a 20 20 20 20 7d 65  , cost));.    }e
28563 6c 73 65 7b 0a 20 20 20 20 20 20 66 6c 61 67 73  lse{.      flags
28564 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20   = 0;.    }.  . 
28565 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74 61 62     /* If the tab
28566 6c 65 20 73 63 61 6e 20 64 6f 65 73 20 6e 6f 74  le scan does not
28567 20 73 61 74 69 73 66 79 20 74 68 65 20 4f 52 44   satisfy the ORD
28568 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20 69 6e  ER BY clause, in
28569 63 72 65 61 73 65 0a 20 20 20 20 2a 2a 20 74 68  crease.    ** th
2856a 65 20 63 6f 73 74 20 62 79 20 4e 6c 6f 67 4e 20  e cost by NlogN 
2856b 74 6f 20 63 6f 76 65 72 20 74 68 65 20 65 78 70  to cover the exp
2856c 65 6e 73 65 20 6f 66 20 73 6f 72 74 69 6e 67 2e  ense of sorting.
2856d 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f 72 64   */.    if( pOrd
2856e 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20 69 66  erBy ){.      if
2856f 28 20 73 6f 72 74 61 62 6c 65 42 79 52 6f 77 69  ( sortableByRowi
28570 64 28 69 43 75 72 2c 20 70 4f 72 64 65 72 42 79  d(iCur, pOrderBy
28571 2c 20 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 2c  , pWC->pMaskSet,
28572 20 26 72 65 76 29 20 29 7b 0a 20 20 20 20 20 20   &rev) ){.      
28573 20 20 66 6c 61 67 73 20 7c 3d 20 57 48 45 52 45    flags |= WHERE
28574 5f 4f 52 44 45 52 42 59 7c 57 48 45 52 45 5f 52  _ORDERBY|WHERE_R
28575 4f 57 49 44 5f 52 41 4e 47 45 3b 0a 20 20 20 20  OWID_RANGE;.    
28576 20 20 20 20 69 66 28 20 72 65 76 20 29 7b 0a 20      if( rev ){. 
28577 20 20 20 20 20 20 20 20 20 66 6c 61 67 73 20 7c           flags |
28578 3d 20 57 48 45 52 45 5f 52 45 56 45 52 53 45 3b  = WHERE_REVERSE;
28579 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2857a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2857b 63 6f 73 74 20 2b 3d 20 63 6f 73 74 2a 65 73 74  cost += cost*est
2857c 4c 6f 67 28 63 6f 73 74 29 3b 0a 20 20 20 20 20  Log(cost);.     
2857d 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22     WHERETRACE(("
2857e 2e 2e 2e 20 73 6f 72 74 69 6e 67 20 69 6e 63 72  ... sorting incr
2857f 65 61 73 65 73 20 63 6f 73 74 20 74 6f 20 25 2e  eases cost to %.
28580 39 67 5c 6e 22 2c 20 63 6f 73 74 29 29 3b 0a 20  9g\n", cost));. 
28581 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
28582 20 69 66 28 20 63 6f 73 74 3c 6c 6f 77 65 73 74   if( cost<lowest
28583 43 6f 73 74 20 29 7b 0a 20 20 20 20 20 20 6c 6f  Cost ){.      lo
28584 77 65 73 74 43 6f 73 74 20 3d 20 63 6f 73 74 3b  westCost = cost;
28585 0a 20 20 20 20 20 20 62 65 73 74 46 6c 61 67 73  .      bestFlags
28586 20 3d 20 66 6c 61 67 73 3b 0a 20 20 20 20 7d 0a   = flags;.    }.
28587 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
28588 20 70 53 72 63 20 74 61 62 6c 65 20 69 73 20 74   pSrc table is t
28589 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f  he right table o
2858a 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 20 74 68  f a LEFT JOIN th
2858b 65 6e 20 77 65 20 6d 61 79 20 6e 6f 74 0a 20 20  en we may not.  
2858c 2a 2a 20 75 73 65 20 61 6e 20 69 6e 64 65 78 20  ** use an index 
2858d 74 6f 20 73 61 74 69 73 66 79 20 49 53 20 4e 55  to satisfy IS NU
2858e 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f  LL constraints o
2858f 6e 20 74 68 61 74 20 74 61 62 6c 65 2e 20 20 54  n that table.  T
28590 68 69 73 20 69 73 0a 20 20 2a 2a 20 62 65 63 61  his is.  ** beca
28591 75 73 65 20 63 6f 6c 75 6d 6e 73 20 6d 69 67 68  use columns migh
28592 74 20 65 6e 64 20 75 70 20 62 65 69 6e 67 20 4e  t end up being N
28593 55 4c 4c 20 69 66 20 74 68 65 20 74 61 62 6c 65  ULL if the table
28594 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20   does not match 
28595 2d 0a 20 20 2a 2a 20 61 20 63 69 72 63 75 6d 73  -.  ** a circums
28596 74 61 6e 63 65 20 77 68 69 63 68 20 74 68 65 20  tance which the 
28597 69 6e 64 65 78 20 63 61 6e 6e 6f 74 20 68 65 6c  index cannot hel
28598 70 20 75 73 20 64 69 73 63 6f 76 65 72 2e 20 20  p us discover.  
28599 54 69 63 6b 65 74 20 23 32 31 37 37 2e 0a 20 20  Ticket #2177..  
2859a 2a 2f 0a 20 20 69 66 28 20 28 70 53 72 63 2d 3e  */.  if( (pSrc->
2859b 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4c 45  jointype & JT_LE
2859c 46 54 29 21 3d 30 20 29 7b 0a 20 20 20 20 65 71  FT)!=0 ){.    eq
2859d 54 65 72 6d 4d 61 73 6b 20 3d 20 57 4f 5f 45 51  TermMask = WO_EQ
2859e 7c 57 4f 5f 49 4e 3b 0a 20 20 7d 65 6c 73 65 7b  |WO_IN;.  }else{
2859f 0a 20 20 20 20 65 71 54 65 72 6d 4d 61 73 6b 20  .    eqTermMask 
285a0 3d 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 7c 57 4f  = WO_EQ|WO_IN|WO
285a1 5f 49 53 4e 55 4c 4c 3b 0a 20 20 7d 0a 0a 20 20  _ISNULL;.  }..  
285a2 2f 2a 20 4c 6f 6f 6b 20 61 74 20 65 61 63 68 20  /* Look at each 
285a3 69 6e 64 65 78 2e 0a 20 20 2a 2f 0a 20 20 69 66  index..  */.  if
285a4 28 20 70 53 72 63 2d 3e 70 49 6e 64 65 78 20 29  ( pSrc->pIndex )
285a5 7b 0a 20 20 20 20 70 50 72 6f 62 65 20 3d 20 70  {.    pProbe = p
285a6 53 72 63 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 7d  Src->pIndex;.  }
285a7 0a 20 20 66 6f 72 28 3b 20 70 50 72 6f 62 65 3b  .  for(; pProbe;
285a8 20 70 50 72 6f 62 65 3d 28 70 53 72 63 2d 3e 70   pProbe=(pSrc->p
285a9 49 6e 64 65 78 20 3f 20 30 20 3a 20 70 50 72 6f  Index ? 0 : pPro
285aa 62 65 2d 3e 70 4e 65 78 74 29 29 7b 0a 20 20 20  be->pNext)){.   
285ab 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
285ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
285ad 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
285ae 0a 20 20 20 20 64 6f 75 62 6c 65 20 69 6e 4d 75  .    double inMu
285af 6c 74 69 70 6c 69 65 72 20 3d 20 31 3b 0a 0a 20  ltiplier = 1;.. 
285b0 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22     WHERETRACE(("
285b1 2e 2e 2e 20 69 6e 64 65 78 20 25 73 3a 5c 6e 22  ... index %s:\n"
285b2 2c 20 70 50 72 6f 62 65 2d 3e 7a 4e 61 6d 65 29  , pProbe->zName)
285b3 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 75 6e 74  );..    /* Count
285b4 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
285b5 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 69 6e  olumns in the in
285b6 64 65 78 20 74 68 61 74 20 61 72 65 20 73 61 74  dex that are sat
285b7 69 73 66 69 65 64 0a 20 20 20 20 2a 2a 20 62 79  isfied.    ** by
285b8 20 78 3d 45 58 50 52 20 63 6f 6e 73 74 72 61 69   x=EXPR constrai
285b9 6e 74 73 20 6f 72 20 78 20 49 4e 20 28 2e 2e 2e  nts or x IN (...
285ba 29 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a 20  ) constraints.. 
285bb 20 20 20 2a 2f 0a 20 20 20 20 66 6c 61 67 73 20     */.    flags 
285bc 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  = 0;.    for(i=0
285bd 3b 20 69 3c 70 50 72 6f 62 65 2d 3e 6e 43 6f 6c  ; i<pProbe->nCol
285be 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  umn; i++){.     
285bf 20 69 6e 74 20 6a 20 3d 20 70 50 72 6f 62 65 2d   int j = pProbe-
285c0 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20  >aiColumn[i];.  
285c1 20 20 20 20 70 54 65 72 6d 20 3d 20 66 69 6e 64      pTerm = find
285c2 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20  Term(pWC, iCur, 
285c3 6a 2c 20 6e 6f 74 52 65 61 64 79 2c 20 65 71 54  j, notReady, eqT
285c4 65 72 6d 4d 61 73 6b 2c 20 70 50 72 6f 62 65 29  ermMask, pProbe)
285c5 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 65 72  ;.      if( pTer
285c6 6d 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20  m==0 ) break;.  
285c7 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 57 48 45      flags |= WHE
285c8 52 45 5f 43 4f 4c 55 4d 4e 5f 45 51 3b 0a 20 20  RE_COLUMN_EQ;.  
285c9 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 65      if( pTerm->e
285ca 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 49 4e  Operator & WO_IN
285cb 20 29 7b 0a 20 20 20 20 20 20 20 20 45 78 70 72   ){.        Expr
285cc 20 2a 70 45 78 70 72 20 3d 20 70 54 65 72 6d 2d   *pExpr = pTerm-
285cd 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20  >pExpr;.        
285ce 66 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 43  flags |= WHERE_C
285cf 4f 4c 55 4d 4e 5f 49 4e 3b 0a 20 20 20 20 20 20  OLUMN_IN;.      
285d0 20 20 69 66 28 20 70 45 78 70 72 2d 3e 70 53 65    if( pExpr->pSe
285d1 6c 65 63 74 21 3d 30 20 29 7b 0a 20 20 20 20 20  lect!=0 ){.     
285d2 20 20 20 20 20 69 6e 4d 75 6c 74 69 70 6c 69 65       inMultiplie
285d3 72 20 2a 3d 20 32 35 3b 0a 20 20 20 20 20 20 20  r *= 25;.       
285d4 20 7d 65 6c 73 65 20 69 66 28 20 41 4c 57 41 59   }else if( ALWAY
285d5 53 28 70 45 78 70 72 2d 3e 70 4c 69 73 74 29 20  S(pExpr->pList) 
285d6 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 4d  ){.          inM
285d7 75 6c 74 69 70 6c 69 65 72 20 2a 3d 20 70 45 78  ultiplier *= pEx
285d8 70 72 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72  pr->pList->nExpr
285d9 20 2b 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a   + 1;.        }.
285da 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
285db 20 20 63 6f 73 74 20 3d 20 70 50 72 6f 62 65 2d    cost = pProbe-
285dc 3e 61 69 52 6f 77 45 73 74 5b 69 5d 20 2a 20 69  >aiRowEst[i] * i
285dd 6e 4d 75 6c 74 69 70 6c 69 65 72 20 2a 20 65 73  nMultiplier * es
285de 74 4c 6f 67 28 69 6e 4d 75 6c 74 69 70 6c 69 65  tLog(inMultiplie
285df 72 29 3b 0a 20 20 20 20 6e 45 71 20 3d 20 69 3b  r);.    nEq = i;
285e0 0a 20 20 20 20 69 66 28 20 70 50 72 6f 62 65 2d  .    if( pProbe-
285e1 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e  >onError!=OE_Non
285e2 65 20 26 26 20 28 66 6c 61 67 73 20 26 20 57 48  e && (flags & WH
285e3 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 29 3d 3d  ERE_COLUMN_IN)==
285e4 30 0a 20 20 20 20 20 20 20 20 20 26 26 20 6e 45  0.         && nE
285e5 71 3d 3d 70 50 72 6f 62 65 2d 3e 6e 43 6f 6c 75  q==pProbe->nColu
285e6 6d 6e 20 29 7b 0a 20 20 20 20 20 20 66 6c 61 67  mn ){.      flag
285e7 73 20 7c 3d 20 57 48 45 52 45 5f 55 4e 49 51 55  s |= WHERE_UNIQU
285e8 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 57 48 45  E;.    }.    WHE
285e9 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 2e 2e 2e  RETRACE(("......
285ea 20 6e 45 71 3d 25 64 20 69 6e 4d 75 6c 74 3d 25   nEq=%d inMult=%
285eb 2e 39 67 20 63 6f 73 74 3d 25 2e 39 67 5c 6e 22  .9g cost=%.9g\n"
285ec 2c 6e 45 71 2c 69 6e 4d 75 6c 74 69 70 6c 69 65  ,nEq,inMultiplie
285ed 72 2c 63 6f 73 74 29 29 3b 0a 0a 20 20 20 20 2f  r,cost));..    /
285ee 2a 20 4c 6f 6f 6b 20 66 6f 72 20 72 61 6e 67 65  * Look for range
285ef 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 20   constraints.   
285f0 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 45 71 3c   */.    if( nEq<
285f1 70 50 72 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 20  pProbe->nColumn 
285f2 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 20 3d  ){.      int j =
285f3 20 70 50 72 6f 62 65 2d 3e 61 69 43 6f 6c 75 6d   pProbe->aiColum
285f4 6e 5b 6e 45 71 5d 3b 0a 20 20 20 20 20 20 70 54  n[nEq];.      pT
285f5 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 70  erm = findTerm(p
285f6 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20 6e 6f 74  WC, iCur, j, not
285f7 52 65 61 64 79 2c 20 57 4f 5f 4c 54 7c 57 4f 5f  Ready, WO_LT|WO_
285f8 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47 45 2c 20  LE|WO_GT|WO_GE, 
285f9 70 50 72 6f 62 65 29 3b 0a 20 20 20 20 20 20 69  pProbe);.      i
285fa 66 28 20 70 54 65 72 6d 20 29 7b 0a 20 20 20 20  f( pTerm ){.    
285fb 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 57 48 45      flags |= WHE
285fc 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45 3b  RE_COLUMN_RANGE;
285fd 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 69 6e  .        if( fin
285fe 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c  dTerm(pWC, iCur,
285ff 20 6a 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f   j, notReady, WO
28600 5f 4c 54 7c 57 4f 5f 4c 45 2c 20 70 50 72 6f 62  _LT|WO_LE, pProb
28601 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  e) ){.          
28602 66 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 54  flags |= WHERE_T
28603 4f 50 5f 4c 49 4d 49 54 3b 0a 20 20 20 20 20 20  OP_LIMIT;.      
28604 20 20 20 20 63 6f 73 74 20 2f 3d 20 33 3b 0a 20      cost /= 3;. 
28605 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
28606 20 69 66 28 20 66 69 6e 64 54 65 72 6d 28 70 57   if( findTerm(pW
28607 43 2c 20 69 43 75 72 2c 20 6a 2c 20 6e 6f 74 52  C, iCur, j, notR
28608 65 61 64 79 2c 20 57 4f 5f 47 54 7c 57 4f 5f 47  eady, WO_GT|WO_G
28609 45 2c 20 70 50 72 6f 62 65 29 20 29 7b 0a 20 20  E, pProbe) ){.  
2860a 20 20 20 20 20 20 20 20 66 6c 61 67 73 20 7c 3d          flags |=
2860b 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54   WHERE_BTM_LIMIT
2860c 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 73 74  ;.          cost
2860d 20 2f 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 7d   /= 3;.        }
2860e 0a 20 20 20 20 20 20 20 20 57 48 45 52 45 54 52  .        WHERETR
2860f 41 43 45 28 28 22 2e 2e 2e 2e 2e 2e 20 72 61 6e  ACE(("...... ran
28610 67 65 20 72 65 64 75 63 65 73 20 63 6f 73 74 20  ge reduces cost 
28611 74 6f 20 25 2e 39 67 5c 6e 22 2c 20 63 6f 73 74  to %.9g\n", cost
28612 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ));.      }.    
28613 7d 0a 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68  }..    /* Add th
28614 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f 73  e additional cos
28615 74 20 6f 66 20 73 6f 72 74 69 6e 67 20 69 66 20  t of sorting if 
28616 74 68 61 74 20 69 73 20 61 20 66 61 63 74 6f 72  that is a factor
28617 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
28618 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20   pOrderBy ){.   
28619 20 20 20 69 66 28 20 28 66 6c 61 67 73 20 26 20     if( (flags & 
2861a 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 29  WHERE_COLUMN_IN)
2861b 3d 3d 30 20 26 26 0a 20 20 20 20 20 20 20 20 20  ==0 &&.         
2861c 20 20 69 73 53 6f 72 74 69 6e 67 49 6e 64 65 78    isSortingIndex
2861d 28 70 50 61 72 73 65 2c 70 57 43 2d 3e 70 4d 61  (pParse,pWC->pMa
2861e 73 6b 53 65 74 2c 70 50 72 6f 62 65 2c 69 43 75  skSet,pProbe,iCu
2861f 72 2c 70 4f 72 64 65 72 42 79 2c 6e 45 71 2c 26  r,pOrderBy,nEq,&
28620 72 65 76 29 20 29 7b 0a 20 20 20 20 20 20 20 20  rev) ){.        
28621 69 66 28 20 66 6c 61 67 73 3d 3d 30 20 29 7b 0a  if( flags==0 ){.
28622 20 20 20 20 20 20 20 20 20 20 66 6c 61 67 73 20            flags 
28623 3d 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52  = WHERE_COLUMN_R
28624 41 4e 47 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ANGE;.        }.
28625 20 20 20 20 20 20 20 20 66 6c 61 67 73 20 7c 3d          flags |=
28626 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 3b 0a   WHERE_ORDERBY;.
28627 20 20 20 20 20 20 20 20 69 66 28 20 72 65 76 20          if( rev 
28628 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c 61  ){.          fla
28629 67 73 20 7c 3d 20 57 48 45 52 45 5f 52 45 56 45  gs |= WHERE_REVE
2862a 52 53 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  RSE;.        }. 
2862b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2862c 20 20 20 20 63 6f 73 74 20 2b 3d 20 63 6f 73 74      cost += cost
2862d 2a 65 73 74 4c 6f 67 28 63 6f 73 74 29 3b 0a 20  *estLog(cost);. 
2862e 20 20 20 20 20 20 20 57 48 45 52 45 54 52 41 43         WHERETRAC
2862f 45 28 28 22 2e 2e 2e 2e 2e 2e 20 6f 72 64 65 72  E(("...... order
28630 62 79 20 69 6e 63 72 65 61 73 65 73 20 63 6f 73  by increases cos
28631 74 20 74 6f 20 25 2e 39 67 5c 6e 22 2c 20 63 6f  t to %.9g\n", co
28632 73 74 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  st));.      }.  
28633 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63    }..    /* Chec
28634 6b 20 74 6f 20 73 65 65 20 69 66 20 77 65 20 63  k to see if we c
28635 61 6e 20 67 65 74 20 61 77 61 79 20 77 69 74 68  an get away with
28636 20 75 73 69 6e 67 20 6a 75 73 74 20 74 68 65 20   using just the 
28637 69 6e 64 65 78 20 77 69 74 68 6f 75 74 0a 20 20  index without.  
28638 20 20 2a 2a 20 65 76 65 72 20 72 65 61 64 69 6e    ** ever readin
28639 67 20 74 68 65 20 74 61 62 6c 65 2e 20 20 49 66  g the table.  If
2863a 20 74 68 61 74 20 69 73 20 74 68 65 20 63 61 73   that is the cas
2863b 65 2c 20 74 68 65 6e 20 68 61 6c 76 65 20 74 68  e, then halve th
2863c 65 0a 20 20 20 20 2a 2a 20 63 6f 73 74 20 6f 66  e.    ** cost of
2863d 20 74 68 69 73 20 69 6e 64 65 78 2e 0a 20 20 20   this index..   
2863e 20 2a 2f 0a 20 20 20 20 69 66 28 20 66 6c 61 67   */.    if( flag
2863f 73 20 26 26 20 70 53 72 63 2d 3e 63 6f 6c 55 73  s && pSrc->colUs
28640 65 64 20 3c 20 28 28 28 42 69 74 6d 61 73 6b 29  ed < (((Bitmask)
28641 31 29 3c 3c 28 42 4d 53 2d 31 29 29 20 29 7b 0a  1)<<(BMS-1)) ){.
28642 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20 6d 20        Bitmask m 
28643 3d 20 70 53 72 63 2d 3e 63 6f 6c 55 73 65 64 3b  = pSrc->colUsed;
28644 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20  .      int j;.  
28645 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70      for(j=0; j<p
28646 50 72 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20  Probe->nColumn; 
28647 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  j++){.        in
28648 74 20 78 20 3d 20 70 50 72 6f 62 65 2d 3e 61 69  t x = pProbe->ai
28649 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20 20 20  Column[j];.     
2864a 20 20 20 69 66 28 20 78 3c 42 4d 53 2d 31 20 29     if( x<BMS-1 )
2864b 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 20 26 3d  {.          m &=
2864c 20 7e 28 28 28 42 69 74 6d 61 73 6b 29 31 29 3c   ~(((Bitmask)1)<
2864d 3c 78 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  <x);.        }. 
2864e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
2864f 20 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20   m==0 ){.       
28650 20 66 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f   flags |= WHERE_
28651 49 44 58 5f 4f 4e 4c 59 3b 0a 20 20 20 20 20 20  IDX_ONLY;.      
28652 20 20 63 6f 73 74 20 2f 3d 20 32 3b 0a 20 20 20    cost /= 2;.   
28653 20 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28       WHERETRACE(
28654 28 22 2e 2e 2e 2e 2e 2e 20 69 64 78 2d 6f 6e 6c  ("...... idx-onl
28655 79 20 72 65 64 75 63 65 73 20 63 6f 73 74 20 74  y reduces cost t
28656 6f 20 25 2e 39 67 5c 6e 22 2c 20 63 6f 73 74 29  o %.9g\n", cost)
28657 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
28658 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73  ..    /* If this
28659 20 69 6e 64 65 78 20 68 61 73 20 61 63 68 69 65   index has achie
2865a 76 65 64 20 74 68 65 20 6c 6f 77 65 73 74 20 63  ved the lowest c
2865b 6f 73 74 20 73 6f 20 66 61 72 2c 20 74 68 65 6e  ost so far, then
2865c 20 75 73 65 20 69 74 2e 0a 20 20 20 20 2a 2f 0a   use it..    */.
2865d 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 26      if( flags &&
2865e 20 63 6f 73 74 20 3c 20 6c 6f 77 65 73 74 43 6f   cost < lowestCo
2865f 73 74 20 29 7b 0a 20 20 20 20 20 20 62 65 73 74  st ){.      best
28660 49 64 78 20 3d 20 70 50 72 6f 62 65 3b 0a 20 20  Idx = pProbe;.  
28661 20 20 20 20 6c 6f 77 65 73 74 43 6f 73 74 20 3d      lowestCost =
28662 20 63 6f 73 74 3b 0a 20 20 20 20 20 20 62 65 73   cost;.      bes
28663 74 46 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a  tFlags = flags;.
28664 20 20 20 20 20 20 62 65 73 74 4e 45 71 20 3d 20        bestNEq = 
28665 6e 45 71 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  nEq;.    }.  }..
28666 20 20 2f 2a 20 52 65 70 6f 72 74 20 74 68 65 20    /* Report the 
28667 62 65 73 74 20 72 65 73 75 6c 74 0a 20 20 2a 2f  best result.  */
28668 0a 20 20 2a 70 70 49 6e 64 65 78 20 3d 20 62 65  .  *ppIndex = be
28669 73 74 49 64 78 3b 0a 20 20 57 48 45 52 45 54 52  stIdx;.  WHERETR
2866a 41 43 45 28 28 22 62 65 73 74 20 69 6e 64 65 78  ACE(("best index
2866b 20 69 73 20 25 73 2c 20 63 6f 73 74 3d 25 2e 39   is %s, cost=%.9
2866c 67 2c 20 66 6c 61 67 73 3d 25 78 2c 20 6e 45 71  g, flags=%x, nEq
2866d 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20  =%d\n",.        
2866e 62 65 73 74 49 64 78 20 3f 20 62 65 73 74 49 64  bestIdx ? bestId
2866f 78 2d 3e 7a 4e 61 6d 65 20 3a 20 22 28 6e 6f 6e  x->zName : "(non
28670 65 29 22 2c 20 6c 6f 77 65 73 74 43 6f 73 74 2c  e)", lowestCost,
28671 20 62 65 73 74 46 6c 61 67 73 2c 20 62 65 73 74   bestFlags, best
28672 4e 45 71 29 29 3b 0a 20 20 2a 70 46 6c 61 67 73  NEq));.  *pFlags
28673 20 3d 20 62 65 73 74 46 6c 61 67 73 20 7c 20 65   = bestFlags | e
28674 71 54 65 72 6d 4d 61 73 6b 3b 0a 20 20 2a 70 6e  qTermMask;.  *pn
28675 45 71 20 3d 20 62 65 73 74 4e 45 71 3b 0a 20 20  Eq = bestNEq;.  
28676 72 65 74 75 72 6e 20 6c 6f 77 65 73 74 43 6f 73  return lowestCos
28677 74 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 69 73  t;.}.../*.** Dis
28678 61 62 6c 65 20 61 20 74 65 72 6d 20 69 6e 20 74  able a term in t
28679 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e  he WHERE clause.
2867a 20 20 45 78 63 65 70 74 2c 20 64 6f 20 6e 6f 74    Except, do not
2867b 20 64 69 73 61 62 6c 65 20 74 68 65 20 74 65 72   disable the ter
2867c 6d 0a 2a 2a 20 69 66 20 69 74 20 63 6f 6e 74 72  m.** if it contr
2867d 6f 6c 73 20 61 20 4c 45 46 54 20 4f 55 54 45 52  ols a LEFT OUTER
2867e 20 4a 4f 49 4e 20 61 6e 64 20 69 74 20 64 69 64   JOIN and it did
2867f 20 6e 6f 74 20 6f 72 69 67 69 6e 61 74 65 20 69   not originate i
28680 6e 20 74 68 65 20 4f 4e 0a 2a 2a 20 6f 72 20 55  n the ON.** or U
28681 53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 20 74  SING clause of t
28682 68 61 74 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20  hat join..**.** 
28683 43 6f 6e 73 69 64 65 72 20 74 68 65 20 74 65 72  Consider the ter
28684 6d 20 74 32 2e 7a 3d 27 6f 6b 27 20 69 6e 20 74  m t2.z='ok' in t
28685 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 71 75 65  he following que
28686 72 69 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 31  ries:.**.**   (1
28687 29 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d  )  SELECT * FROM
28688 20 74 31 20 4c 45 46 54 20 4a 4f 49 4e 20 74 32   t1 LEFT JOIN t2
28689 20 4f 4e 20 74 31 2e 61 3d 74 32 2e 78 20 57 48   ON t1.a=t2.x WH
2868a 45 52 45 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a  ERE t2.z='ok'.**
2868b 20 20 20 28 32 29 20 20 53 45 4c 45 43 54 20 2a     (2)  SELECT *
2868c 20 46 52 4f 4d 20 74 31 20 4c 45 46 54 20 4a 4f   FROM t1 LEFT JO
2868d 49 4e 20 74 32 20 4f 4e 20 74 31 2e 61 3d 74 32  IN t2 ON t1.a=t2
2868e 2e 78 20 41 4e 44 20 74 32 2e 7a 3d 27 6f 6b 27  .x AND t2.z='ok'
2868f 0a 2a 2a 20 20 20 28 33 29 20 20 53 45 4c 45 43  .**   (3)  SELEC
28690 54 20 2a 20 46 52 4f 4d 20 74 31 2c 20 74 32 20  T * FROM t1, t2 
28691 57 48 45 52 45 20 74 31 2e 61 3d 74 32 2e 78 20  WHERE t1.a=t2.x 
28692 41 4e 44 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a  AND t2.z='ok'.**
28693 0a 2a 2a 20 54 68 65 20 74 32 2e 7a 3d 27 6f 6b  .** The t2.z='ok
28694 27 20 69 73 20 64 69 73 61 62 6c 65 64 20 69 6e  ' is disabled in
28695 20 74 68 65 20 69 6e 20 28 32 29 20 62 65 63 61   the in (2) beca
28696 75 73 65 20 69 74 20 6f 72 69 67 69 6e 61 74 65  use it originate
28697 73 0a 2a 2a 20 69 6e 20 74 68 65 20 4f 4e 20 63  s.** in the ON c
28698 6c 61 75 73 65 2e 20 20 54 68 65 20 74 65 72 6d  lause.  The term
28699 20 69 73 20 64 69 73 61 62 6c 65 64 20 69 6e 20   is disabled in 
2869a 28 33 29 20 62 65 63 61 75 73 65 20 69 74 20 69  (3) because it i
2869b 73 20 6e 6f 74 20 70 61 72 74 0a 2a 2a 20 6f 66  s not part.** of
2869c 20 61 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f   a LEFT OUTER JO
2869d 49 4e 2e 20 20 49 6e 20 28 31 29 2c 20 74 68 65  IN.  In (1), the
2869e 20 74 65 72 6d 20 69 73 20 6e 6f 74 20 64 69 73   term is not dis
2869f 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 44 69 73  abled..**.** Dis
286a0 61 62 6c 69 6e 67 20 61 20 74 65 72 6d 20 63 61  abling a term ca
286a1 75 73 65 73 20 74 68 61 74 20 74 65 72 6d 20 74  uses that term t
286a2 6f 20 6e 6f 74 20 62 65 20 74 65 73 74 65 64 20  o not be tested 
286a3 69 6e 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f 6f  in the inner loo
286a4 70 0a 2a 2a 20 6f 66 20 74 68 65 20 6a 6f 69 6e  p.** of the join
286a5 2e 20 20 44 69 73 61 62 6c 69 6e 67 20 69 73 20  .  Disabling is 
286a6 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e  an optimization.
286a7 20 20 57 68 65 6e 20 74 65 72 6d 73 20 61 72 65    When terms are
286a8 20 73 61 74 69 73 66 69 65 64 0a 2a 2a 20 62 79   satisfied.** by
286a9 20 69 6e 64 69 63 65 73 2c 20 77 65 20 64 69 73   indices, we dis
286aa 61 62 6c 65 20 74 68 65 6d 20 74 6f 20 70 72 65  able them to pre
286ab 76 65 6e 74 20 72 65 64 75 6e 64 61 6e 74 20 74  vent redundant t
286ac 65 73 74 73 20 69 6e 20 74 68 65 20 69 6e 6e 65  ests in the inne
286ad 72 0a 2a 2a 20 6c 6f 6f 70 2e 20 20 57 65 20 77  r.** loop.  We w
286ae 6f 75 6c 64 20 67 65 74 20 74 68 65 20 63 6f 72  ould get the cor
286af 72 65 63 74 20 72 65 73 75 6c 74 73 20 69 66 20  rect results if 
286b0 6e 6f 74 68 69 6e 67 20 77 65 72 65 20 65 76 65  nothing were eve
286b1 72 20 64 69 73 61 62 6c 65 64 2c 0a 2a 2a 20 62  r disabled,.** b
286b2 75 74 20 6a 6f 69 6e 73 20 6d 69 67 68 74 20 72  ut joins might r
286b3 75 6e 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77  un a little slow
286b4 65 72 2e 20 20 54 68 65 20 74 72 69 63 6b 20 69  er.  The trick i
286b5 73 20 74 6f 20 64 69 73 61 62 6c 65 20 61 73 20  s to disable as 
286b6 6d 75 63 68 0a 2a 2a 20 61 73 20 77 65 20 63 61  much.** as we ca
286b7 6e 20 77 69 74 68 6f 75 74 20 64 69 73 61 62 6c  n without disabl
286b8 69 6e 67 20 74 6f 6f 20 6d 75 63 68 2e 20 20 49  ing too much.  I
286b9 66 20 77 65 20 64 69 73 61 62 6c 65 64 20 69 6e  f we disabled in
286ba 20 28 31 29 2c 20 77 65 27 64 20 67 65 74 0a 2a   (1), we'd get.*
286bb 2a 20 74 68 65 20 77 72 6f 6e 67 20 61 6e 73 77  * the wrong answ
286bc 65 72 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20  er.  See ticket 
286bd 23 38 31 33 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  #813..*/.static 
286be 76 6f 69 64 20 64 69 73 61 62 6c 65 54 65 72 6d  void disableTerm
286bf 28 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65  (WhereLevel *pLe
286c0 76 65 6c 2c 20 57 68 65 72 65 54 65 72 6d 20 2a  vel, WhereTerm *
286c1 70 54 65 72 6d 29 7b 0a 20 20 69 66 28 20 70 54  pTerm){.  if( pT
286c2 65 72 6d 0a 20 20 20 20 20 20 26 26 20 41 4c 57  erm.      && ALW
286c3 41 59 53 28 28 70 54 65 72 6d 2d 3e 66 6c 61 67  AYS((pTerm->flag
286c4 73 20 26 20 54 45 52 4d 5f 43 4f 44 45 44 29 3d  s & TERM_CODED)=
286c5 3d 30 29 0a 20 20 20 20 20 20 26 26 20 28 70 4c  =0).      && (pL
286c6 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 3d  evel->iLeftJoin=
286c7 3d 30 20 7c 7c 20 45 78 70 72 48 61 73 50 72 6f  =0 || ExprHasPro
286c8 70 65 72 74 79 28 70 54 65 72 6d 2d 3e 70 45 78  perty(pTerm->pEx
286c9 70 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29  pr, EP_FromJoin)
286ca 29 0a 20 20 29 7b 0a 20 20 20 20 70 54 65 72 6d  ).  ){.    pTerm
286cb 2d 3e 66 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f  ->flags |= TERM_
286cc 43 4f 44 45 44 3b 0a 20 20 20 20 69 66 28 20 70  CODED;.    if( p
286cd 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 3e 3d 30  Term->iParent>=0
286ce 20 29 7b 0a 20 20 20 20 20 20 57 68 65 72 65 54   ){.      WhereT
286cf 65 72 6d 20 2a 70 4f 74 68 65 72 20 3d 20 26 70  erm *pOther = &p
286d0 54 65 72 6d 2d 3e 70 57 43 2d 3e 61 5b 70 54 65  Term->pWC->a[pTe
286d1 72 6d 2d 3e 69 50 61 72 65 6e 74 5d 3b 0a 20 20  rm->iParent];.  
286d2 20 20 20 20 69 66 28 20 28 2d 2d 70 4f 74 68 65      if( (--pOthe
286d3 72 2d 3e 6e 43 68 69 6c 64 29 3d 3d 30 20 29 7b  r->nChild)==0 ){
286d4 0a 20 20 20 20 20 20 20 20 64 69 73 61 62 6c 65  .        disable
286d5 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 4f 74  Term(pLevel, pOt
286d6 68 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  her);.      }.  
286d7 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
286d8 20 41 70 70 6c 79 20 74 68 65 20 61 66 66 69 6e   Apply the affin
286d9 69 74 69 65 73 20 61 73 73 6f 63 69 61 74 65 64  ities associated
286da 20 77 69 74 68 20 74 68 65 20 66 69 72 73 74 20   with the first 
286db 6e 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 69 6e 64  n columns of ind
286dc 65 78 0a 2a 2a 20 70 49 64 78 20 74 6f 20 74 68  ex.** pIdx to th
286dd 65 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20  e values in the 
286de 6e 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72  n registers star
286df 74 69 6e 67 20 61 74 20 62 61 73 65 2e 0a 2a 2f  ting at base..*/
286e0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64  .static void cod
286e1 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 50  eApplyAffinity(P
286e2 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
286e3 74 20 62 61 73 65 2c 20 69 6e 74 20 6e 2c 20 49  t base, int n, I
286e4 6e 64 65 78 20 2a 70 49 64 78 29 7b 0a 20 20 69  ndex *pIdx){.  i
286e5 66 28 20 6e 3e 30 20 29 7b 0a 20 20 20 20 56 64  f( n>0 ){.    Vd
286e6 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
286e7 70 56 64 62 65 3b 0a 20 20 20 20 61 73 73 65 72  pVdbe;.    asser
286e8 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 20 20 73  t( v!=0 );.    s
286e9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
286ea 28 76 2c 20 4f 50 5f 41 66 66 69 6e 69 74 79 2c  (v, OP_Affinity,
286eb 20 62 61 73 65 2c 20 6e 29 3b 0a 20 20 20 20 73   base, n);.    s
286ec 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e  qlite3IndexAffin
286ed 69 74 79 53 74 72 28 76 2c 20 70 49 64 78 29 3b  ityStr(v, pIdx);
286ee 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
286ef 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
286f0 6e 67 65 28 70 50 61 72 73 65 2c 20 62 61 73 65  nge(pParse, base
286f1 2c 20 6e 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a  , n);.  }.}.../*
286f2 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
286f3 65 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 65  e for a single e
286f4 71 75 61 6c 69 74 79 20 74 65 72 6d 20 6f 66 20  quality term of 
286f5 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
286f6 2e 20 20 41 6e 20 65 71 75 61 6c 69 74 79 0a 2a  .  An equality.*
286f7 2a 20 74 65 72 6d 20 63 61 6e 20 62 65 20 65 69  * term can be ei
286f8 74 68 65 72 20 58 3d 65 78 70 72 20 6f 72 20 58  ther X=expr or X
286f9 20 49 4e 20 28 2e 2e 2e 29 2e 20 20 20 70 54 65   IN (...).   pTe
286fa 72 6d 20 69 73 20 74 68 65 20 74 65 72 6d 20 74  rm is the term t
286fb 6f 20 62 65 20 0a 2a 2a 20 63 6f 64 65 64 2e 0a  o be .** coded..
286fc 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e  **.** The curren
286fd 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  t value for the 
286fe 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 6c 65  constraint is le
286ff 66 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 69  ft in register i
28700 52 65 67 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61  Reg..**.** For a
28701 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 66 20 74   constraint of t
28702 68 65 20 66 6f 72 6d 20 58 3d 65 78 70 72 2c 20  he form X=expr, 
28703 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
28704 73 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20  s evaluated and 
28705 69 74 73 0a 2a 2a 20 72 65 73 75 6c 74 20 69 73  its.** result is
28706 20 6c 65 66 74 20 6f 6e 20 74 68 65 20 73 74 61   left on the sta
28707 63 6b 2e 20 20 46 6f 72 20 63 6f 6e 73 74 72 61  ck.  For constra
28708 69 6e 74 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  ints of the form
28709 20 58 20 49 4e 20 28 2e 2e 2e 29 0a 2a 2a 20 74   X IN (...).** t
2870a 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73  his routine sets
2870b 20 75 70 20 61 20 6c 6f 6f 70 20 74 68 61 74 20   up a loop that 
2870c 77 69 6c 6c 20 69 74 65 72 61 74 65 20 6f 76 65  will iterate ove
2870d 72 20 61 6c 6c 20 76 61 6c 75 65 73 20 6f 66 20  r all values of 
2870e 58 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  X..*/.static int
2870f 20 63 6f 64 65 45 71 75 61 6c 69 74 79 54 65 72   codeEqualityTer
28710 6d 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  m(.  Parse *pPar
28711 73 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  se,      /* The 
28712 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  parsing context 
28713 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a  */.  WhereTerm *
28714 70 54 65 72 6d 2c 20 20 20 2f 2a 20 54 68 65 20  pTerm,   /* The 
28715 74 65 72 6d 20 6f 66 20 74 68 65 20 57 48 45 52  term of the WHER
28716 45 20 63 6c 61 75 73 65 20 74 6f 20 62 65 20 63  E clause to be c
28717 6f 64 65 64 20 2a 2f 0a 20 20 57 68 65 72 65 4c  oded */.  WhereL
28718 65 76 65 6c 20 2a 70 4c 65 76 65 6c 2c 20 2f 2a  evel *pLevel, /*
28719 20 57 68 65 6e 20 6c 65 76 65 6c 20 6f 66 20 74   When level of t
2871a 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 77  he FROM clause w
2871b 65 20 61 72 65 20 77 6f 72 6b 69 6e 67 20 6f 6e  e are working on
2871c 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 72 67 65   */.  int iTarge
2871d 74 20 20 20 20 20 20 20 20 20 2f 2a 20 41 74 74  t         /* Att
2871e 65 6d 70 74 20 74 6f 20 6c 65 61 76 65 20 72 65  empt to leave re
2871f 73 75 6c 74 73 20 69 6e 20 74 68 69 73 20 72 65  sults in this re
28720 67 69 73 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 45  gister */.){.  E
28721 78 70 72 20 2a 70 58 20 3d 20 70 54 65 72 6d 2d  xpr *pX = pTerm-
28722 3e 70 45 78 70 72 3b 0a 20 20 56 64 62 65 20 2a  >pExpr;.  Vdbe *
28723 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
28724 65 3b 0a 20 20 69 6e 74 20 69 52 65 67 3b 20 20  e;.  int iReg;  
28725 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28726 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64  /* Register hold
28727 69 6e 67 20 72 65 73 75 6c 74 73 20 2a 2f 0a 0a  ing results */..
28728 20 20 61 73 73 65 72 74 28 20 69 54 61 72 67 65    assert( iTarge
28729 74 3e 30 20 29 3b 0a 20 20 69 66 28 20 70 58 2d  t>0 );.  if( pX-
2872a 3e 6f 70 3d 3d 54 4b 5f 45 51 20 29 7b 0a 20 20  >op==TK_EQ ){.  
2872b 20 20 69 52 65 67 20 3d 20 73 71 6c 69 74 65 33    iReg = sqlite3
2872c 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70  ExprCodeTarget(p
2872d 50 61 72 73 65 2c 20 70 58 2d 3e 70 52 69 67 68  Parse, pX->pRigh
2872e 74 2c 20 69 54 61 72 67 65 74 29 3b 0a 20 20 7d  t, iTarget);.  }
2872f 65 6c 73 65 20 69 66 28 20 70 58 2d 3e 6f 70 3d  else if( pX->op=
28730 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20  =TK_ISNULL ){.  
28731 20 20 69 52 65 67 20 3d 20 69 54 61 72 67 65 74    iReg = iTarget
28732 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
28733 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
28734 6c 6c 2c 20 30 2c 20 69 52 65 67 29 3b 0a 23 69  ll, 0, iReg);.#i
28735 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
28736 54 5f 53 55 42 51 55 45 52 59 0a 20 20 7d 65 6c  T_SUBQUERY.  }el
28737 73 65 7b 0a 20 20 20 20 69 6e 74 20 65 54 79 70  se{.    int eTyp
28738 65 3b 0a 20 20 20 20 69 6e 74 20 69 54 61 62 3b  e;.    int iTab;
28739 0a 20 20 20 20 73 74 72 75 63 74 20 49 6e 4c 6f  .    struct InLo
2873a 6f 70 20 2a 70 49 6e 3b 0a 0a 20 20 20 20 61 73  op *pIn;..    as
2873b 73 65 72 74 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b  sert( pX->op==TK
2873c 5f 49 4e 20 29 3b 0a 20 20 20 20 69 52 65 67 20  _IN );.    iReg 
2873d 3d 20 69 54 61 72 67 65 74 3b 0a 20 20 20 20 65  = iTarget;.    e
2873e 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 46 69  Type = sqlite3Fi
2873f 6e 64 49 6e 49 6e 64 65 78 28 70 50 61 72 73 65  ndInIndex(pParse
28740 2c 20 70 58 2c 20 30 29 3b 0a 20 20 20 20 69 54  , pX, 0);.    iT
28741 61 62 20 3d 20 70 58 2d 3e 69 54 61 62 6c 65 3b  ab = pX->iTable;
28742 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
28743 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77  AddOp2(v, OP_Rew
28744 69 6e 64 2c 20 69 54 61 62 2c 20 30 29 3b 0a 20  ind, iTab, 0);. 
28745 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
28746 76 2c 20 22 25 2e 2a 73 22 2c 20 70 58 2d 3e 73  v, "%.*s", pX->s
28747 70 61 6e 2e 6e 2c 20 70 58 2d 3e 73 70 61 6e 2e  pan.n, pX->span.
28748 7a 29 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65  z));.    if( pLe
28749 76 65 6c 2d 3e 6e 49 6e 3d 3d 30 20 29 7b 0a 20  vel->nIn==0 ){. 
2874a 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6e 78 74       pLevel->nxt
2874b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
2874c 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20  keLabel(v);.    
2874d 7d 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6e 49  }.    pLevel->nI
2874e 6e 2b 2b 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  n++;.    pLevel-
2874f 3e 61 49 6e 4c 6f 6f 70 20 3d 20 73 71 6c 69 74  >aInLoop = sqlit
28750 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65  e3DbReallocOrFre
28751 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4c  e(pParse->db, pL
28752 65 76 65 6c 2d 3e 61 49 6e 4c 6f 6f 70 2c 0a 20  evel->aInLoop,. 
28753 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28754 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28755 20 20 20 73 69 7a 65 6f 66 28 70 4c 65 76 65 6c     sizeof(pLevel
28756 2d 3e 61 49 6e 4c 6f 6f 70 5b 30 5d 29 2a 70 4c  ->aInLoop[0])*pL
28757 65 76 65 6c 2d 3e 6e 49 6e 29 3b 0a 20 20 20 20  evel->nIn);.    
28758 70 49 6e 20 3d 20 70 4c 65 76 65 6c 2d 3e 61 49  pIn = pLevel->aI
28759 6e 4c 6f 6f 70 3b 0a 20 20 20 20 69 66 28 20 70  nLoop;.    if( p
2875a 49 6e 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 20  In ){.      pIn 
2875b 2b 3d 20 70 4c 65 76 65 6c 2d 3e 6e 49 6e 20 2d  += pLevel->nIn -
2875c 20 31 3b 0a 20 20 20 20 20 20 70 49 6e 2d 3e 69   1;.      pIn->i
2875d 43 75 72 20 3d 20 69 54 61 62 3b 0a 20 20 20 20  Cur = iTab;.    
2875e 20 20 69 66 28 20 65 54 79 70 65 3d 3d 49 4e 5f    if( eType==IN_
2875f 49 4e 44 45 58 5f 52 4f 57 49 44 20 29 7b 0a 20  INDEX_ROWID ){. 
28760 20 20 20 20 20 20 20 70 49 6e 2d 3e 74 6f 70 41         pIn->topA
28761 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
28762 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f  eAddOp2(v, OP_Ro
28763 77 69 64 2c 20 69 54 61 62 2c 20 69 52 65 67 29  wid, iTab, iReg)
28764 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
28765 20 20 20 20 20 20 20 70 49 6e 2d 3e 74 6f 70 41         pIn->topA
28766 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
28767 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
28768 6c 75 6d 6e 2c 20 69 54 61 62 2c 20 30 2c 20 69  lumn, iTab, 0, i
28769 52 65 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Reg);.      }.  
2876a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2876b 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 73 4e 75  ddOp1(v, OP_IsNu
2876c 6c 6c 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 7d  ll, iReg);.    }
2876d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4c 65 76  else{.      pLev
2876e 65 6c 2d 3e 6e 49 6e 20 3d 20 30 3b 0a 20 20 20  el->nIn = 0;.   
2876f 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20   }.#endif.  }.  
28770 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76  disableTerm(pLev
28771 65 6c 2c 20 70 54 65 72 6d 29 3b 0a 20 20 72 65  el, pTerm);.  re
28772 74 75 72 6e 20 69 52 65 67 3b 0a 7d 0a 0a 2f 2a  turn iReg;.}../*
28773 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
28774 65 20 74 68 61 74 20 77 69 6c 6c 20 65 76 61 6c  e that will eval
28775 75 61 74 65 20 61 6c 6c 20 3d 3d 20 61 6e 64 20  uate all == and 
28776 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 66  IN constraints f
28777 6f 72 20 61 6e 0a 2a 2a 20 69 6e 64 65 78 2e 20  or an.** index. 
28778 20 54 68 65 20 76 61 6c 75 65 73 20 66 6f 72 20   The values for 
28779 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  all constraints 
2877a 61 72 65 20 6c 65 66 74 20 6f 6e 20 74 68 65 20  are left on the 
2877b 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  stack..**.** For
2877c 20 65 78 61 6d 70 6c 65 2c 20 63 6f 6e 73 69 64   example, consid
2877d 65 72 20 74 61 62 6c 65 20 74 31 28 61 2c 62 2c  er table t1(a,b,
2877e 63 2c 64 2c 65 2c 66 29 20 77 69 74 68 20 69 6e  c,d,e,f) with in
2877f 64 65 78 20 69 31 28 61 2c 62 2c 63 29 2e 0a 2a  dex i1(a,b,c)..*
28780 2a 20 53 75 70 70 6f 73 65 20 74 68 65 20 57 48  * Suppose the WH
28781 45 52 45 20 63 6c 61 75 73 65 20 69 73 20 74 68  ERE clause is th
28782 69 73 3a 20 20 61 3d 3d 35 20 41 4e 44 20 62 20  is:  a==5 AND b 
28783 49 4e 20 28 31 2c 32 2c 33 29 20 41 4e 44 20 63  IN (1,2,3) AND c
28784 3e 35 20 41 4e 44 20 63 3c 31 30 0a 2a 2a 20 54  >5 AND c<10.** T
28785 68 65 20 69 6e 64 65 78 20 68 61 73 20 61 73 20  he index has as 
28786 6d 61 6e 79 20 61 73 20 74 68 72 65 65 20 65 71  many as three eq
28787 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e  uality constrain
28788 74 73 2c 20 62 75 74 20 69 6e 20 74 68 69 73 0a  ts, but in this.
28789 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20  ** example, the 
2878a 74 68 69 72 64 20 22 63 22 20 76 61 6c 75 65 20  third "c" value 
2878b 69 73 20 61 6e 20 69 6e 65 71 75 61 6c 69 74 79  is an inequality
2878c 2e 20 20 53 6f 20 6f 6e 6c 79 20 74 77 6f 20 0a  .  So only two .
2878d 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61  ** constraints a
2878e 72 65 20 63 6f 64 65 64 2e 20 20 54 68 69 73 20  re coded.  This 
2878f 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 67 65 6e  routine will gen
28790 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 65 76  erate code to ev
28791 61 6c 75 61 74 65 0a 2a 2a 20 61 3d 3d 35 20 61  aluate.** a==5 a
28792 6e 64 20 62 20 49 4e 20 28 31 2c 32 2c 33 29 2e  nd b IN (1,2,3).
28793 20 20 54 68 65 20 63 75 72 72 65 6e 74 20 76 61    The current va
28794 6c 75 65 73 20 66 6f 72 20 61 20 61 6e 64 20 62  lues for a and b
28795 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 0a 2a 2a   will be left.**
28796 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 2d 20   on the stack - 
28797 61 20 69 73 20 74 68 65 20 64 65 65 70 65 73 74  a is the deepest
28798 20 61 6e 64 20 62 20 74 68 65 20 73 68 61 6c 6c   and b the shall
28799 6f 77 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  owest..**.** In 
2879a 74 68 65 20 65 78 61 6d 70 6c 65 20 61 62 6f 76  the example abov
2879b 65 20 6e 45 71 3d 3d 32 2e 20 20 42 75 74 20 74  e nEq==2.  But t
2879c 68 69 73 20 73 75 62 72 6f 75 74 69 6e 65 20 77  his subroutine w
2879d 6f 72 6b 73 20 66 6f 72 20 61 6e 79 20 76 61 6c  orks for any val
2879e 75 65 0a 2a 2a 20 6f 66 20 6e 45 71 20 69 6e 63  ue.** of nEq inc
2879f 6c 75 64 69 6e 67 20 30 2e 20 20 49 66 20 6e 45  luding 0.  If nE
287a0 71 3d 3d 30 2c 20 74 68 69 73 20 72 6f 75 74 69  q==0, this routi
287a1 6e 65 20 69 73 20 6e 65 61 72 6c 79 20 61 20 6e  ne is nearly a n
287a2 6f 2d 6f 70 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c  o-op..** The onl
287a3 79 20 74 68 69 6e 67 20 69 74 20 64 6f 65 73 20  y thing it does 
287a4 69 73 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20  is allocate the 
287a5 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d 20 6d 65 6d  pLevel->iMem mem
287a6 6f 72 79 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20  ory cell..**.** 
287a7 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 77  This routine alw
287a8 61 79 73 20 61 6c 6c 6f 63 61 74 65 73 20 61 74  ays allocates at
287a9 20 6c 65 61 73 74 20 6f 6e 65 20 6d 65 6d 6f 72   least one memor
287aa 79 20 63 65 6c 6c 20 61 6e 64 20 70 75 74 73 0a  y cell and puts.
287ab 2a 2a 20 74 68 65 20 61 64 64 72 65 73 73 20 6f  ** the address o
287ac 66 20 74 68 61 74 20 6d 65 6d 6f 72 79 20 63 65  f that memory ce
287ad 6c 6c 20 69 6e 20 70 4c 65 76 65 6c 2d 3e 69 4d  ll in pLevel->iM
287ae 65 6d 2e 20 20 54 68 65 20 63 6f 64 65 20 74 68  em.  The code th
287af 61 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 68 69 73  at.** calls this
287b0 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 75 73   routine will us
287b1 65 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d 20 74  e pLevel->iMem t
287b2 6f 20 73 74 6f 72 65 20 74 68 65 20 74 65 72 6d  o store the term
287b3 69 6e 61 74 69 6f 6e 0a 2a 2a 20 6b 65 79 20 76  ination.** key v
287b4 61 6c 75 65 20 6f 66 20 74 68 65 20 6c 6f 6f 70  alue of the loop
287b5 2e 20 20 49 66 20 6f 6e 65 20 6f 72 20 6d 6f 72  .  If one or mor
287b6 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 20 61  e IN operators a
287b7 70 70 65 61 72 2c 20 74 68 65 6e 0a 2a 2a 20 74  ppear, then.** t
287b8 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f  his routine allo
287b9 63 61 74 65 73 20 61 6e 20 61 64 64 69 74 69 6f  cates an additio
287ba 6e 61 6c 20 6e 45 71 20 6d 65 6d 6f 72 79 20 63  nal nEq memory c
287bb 65 6c 6c 73 20 66 6f 72 20 69 6e 74 65 72 6e 61  ells for interna
287bc 6c 0a 2a 2a 20 75 73 65 2e 0a 2a 2f 0a 73 74 61  l.** use..*/.sta
287bd 74 69 63 20 69 6e 74 20 63 6f 64 65 41 6c 6c 45  tic int codeAllE
287be 71 75 61 6c 69 74 79 54 65 72 6d 73 28 0a 20 20  qualityTerms(.  
287bf 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
287c0 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
287c1 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68   context */.  Wh
287c2 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c  ereLevel *pLevel
287c3 2c 20 20 20 2f 2a 20 57 68 69 63 68 20 6e 65 73  ,   /* Which nes
287c4 74 65 64 20 6c 6f 6f 70 20 6f 66 20 74 68 65 20  ted loop of the 
287c5 46 52 4f 4d 20 77 65 20 61 72 65 20 63 6f 64 69  FROM we are codi
287c6 6e 67 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61  ng */.  WhereCla
287c7 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20 2f 2a  use *pWC,     /*
287c8 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
287c9 65 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e  e */.  Bitmask n
287ca 6f 74 52 65 61 64 79 2c 20 20 20 20 20 2f 2a 20  otReady,     /* 
287cb 57 68 69 63 68 20 70 61 72 74 73 20 6f 66 20 46  Which parts of F
287cc 52 4f 4d 20 68 61 76 65 20 6e 6f 74 20 79 65 74  ROM have not yet
287cd 20 62 65 65 6e 20 63 6f 64 65 64 20 2a 2f 0a 20   been coded */. 
287ce 20 69 6e 74 20 6e 45 78 74 72 61 52 65 67 20 20   int nExtraReg  
287cf 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
287d0 20 6f 66 20 65 78 74 72 61 20 72 65 67 69 73 74   of extra regist
287d1 65 72 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ers to allocate 
287d2 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 45 71 20  */.){.  int nEq 
287d3 3d 20 70 4c 65 76 65 6c 2d 3e 6e 45 71 3b 20 20  = pLevel->nEq;  
287d4 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 75 6d        /* The num
287d5 62 65 72 20 6f 66 20 3d 3d 20 6f 72 20 49 4e 20  ber of == or IN 
287d6 63 6f 6e 73 74 72 61 69 6e 74 73 20 74 6f 20 63  constraints to c
287d7 6f 64 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76  ode */.  Vdbe *v
287d8 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
287d9 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69  ;      /* The vi
287da 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 75 6e  rtual machine un
287db 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
287dc 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64   */.  Index *pId
287dd 78 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 49 64 78  x = pLevel->pIdx
287de 3b 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78  ;   /* The index
287df 20 62 65 69 6e 67 20 75 73 65 64 20 66 6f 72 20   being used for 
287e0 74 68 69 73 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69  this loop */.  i
287e1 6e 74 20 69 43 75 72 20 3d 20 70 4c 65 76 65 6c  nt iCur = pLevel
287e2 2d 3e 69 54 61 62 43 75 72 3b 20 20 20 2f 2a 20  ->iTabCur;   /* 
287e3 54 68 65 20 63 75 72 73 6f 72 20 6f 66 20 74 68  The cursor of th
287e4 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 57 68 65  e table */.  Whe
287e5 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20  reTerm *pTerm;  
287e6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20             /* A 
287e7 73 69 6e 67 6c 65 20 63 6f 6e 73 74 72 61 69 6e  single constrain
287e8 74 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20  t term */.  int 
287e9 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  j;              
287ea 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
287eb 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69  p counter */.  i
287ec 6e 74 20 72 65 67 42 61 73 65 3b 20 20 20 20 20  nt regBase;     
287ed 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
287ee 42 61 73 65 20 72 65 67 69 73 74 65 72 20 2a 2f  Base register */
287ef 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75  ..  /* Figure ou
287f0 74 20 68 6f 77 20 6d 61 6e 79 20 6d 65 6d 6f 72  t how many memor
287f1 79 20 63 65 6c 6c 73 20 77 65 20 77 69 6c 6c 20  y cells we will 
287f2 6e 65 65 64 20 74 68 65 6e 20 61 6c 6c 6f 63 61  need then alloca
287f3 74 65 20 74 68 65 6d 2e 0a 20 20 2a 2a 20 57 65  te them..  ** We
287f4 20 61 6c 77 61 79 73 20 6e 65 65 64 20 61 74 20   always need at 
287f5 6c 65 61 73 74 20 6f 6e 65 20 75 73 65 64 20 74  least one used t
287f6 6f 20 73 74 6f 72 65 20 74 68 65 20 6c 6f 6f 70  o store the loop
287f7 20 74 65 72 6d 69 6e 61 74 6f 72 0a 20 20 2a 2a   terminator.  **
287f8 20 76 61 6c 75 65 2e 20 20 49 66 20 74 68 65 72   value.  If ther
287f9 65 20 61 72 65 20 49 4e 20 6f 70 65 72 61 74 6f  e are IN operato
287fa 72 73 20 77 65 27 6c 6c 20 6e 65 65 64 20 6f 6e  rs we'll need on
287fb 65 20 66 6f 72 20 65 61 63 68 20 3d 3d 20 6f 72  e for each == or
287fc 0a 20 20 2a 2a 20 49 4e 20 63 6f 6e 73 74 72 61  .  ** IN constra
287fd 69 6e 74 2e 0a 20 20 2a 2f 0a 20 20 70 4c 65 76  int..  */.  pLev
287fe 65 6c 2d 3e 69 4d 65 6d 20 3d 20 70 50 61 72 73  el->iMem = pPars
287ff 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20 72  e->nMem + 1;.  r
28800 65 67 42 61 73 65 20 3d 20 70 50 61 72 73 65 2d  egBase = pParse-
28801 3e 6e 4d 65 6d 20 2b 20 32 3b 0a 20 20 70 50 61  >nMem + 2;.  pPa
28802 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 4c 65  rse->nMem += pLe
28803 76 65 6c 2d 3e 6e 45 71 20 2b 20 32 20 2b 20 6e  vel->nEq + 2 + n
28804 45 78 74 72 61 52 65 67 3b 0a 0a 20 20 2f 2a 20  ExtraReg;..  /* 
28805 45 76 61 6c 75 61 74 65 20 74 68 65 20 65 71 75  Evaluate the equ
28806 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
28807 73 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  s.  */.  assert(
28808 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 3d   pIdx->nColumn>=
28809 6e 45 71 20 29 3b 0a 20 20 66 6f 72 28 6a 3d 30  nEq );.  for(j=0
2880a 3b 20 6a 3c 6e 45 71 3b 20 6a 2b 2b 29 7b 0a 20  ; j<nEq; j++){. 
2880b 20 20 20 69 6e 74 20 72 31 3b 0a 20 20 20 20 69     int r1;.    i
2880c 6e 74 20 6b 20 3d 20 70 49 64 78 2d 3e 61 69 43  nt k = pIdx->aiC
2880d 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20 20 70 54  olumn[j];.    pT
2880e 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 70  erm = findTerm(p
2880f 57 43 2c 20 69 43 75 72 2c 20 6b 2c 20 6e 6f 74  WC, iCur, k, not
28810 52 65 61 64 79 2c 20 70 4c 65 76 65 6c 2d 3e 66  Ready, pLevel->f
28811 6c 61 67 73 2c 20 70 49 64 78 29 3b 0a 20 20 20  lags, pIdx);.   
28812 20 69 66 28 20 4e 45 56 45 52 28 70 54 65 72 6d   if( NEVER(pTerm
28813 3d 3d 30 29 20 29 20 62 72 65 61 6b 3b 0a 20 20  ==0) ) break;.  
28814 20 20 61 73 73 65 72 74 28 20 28 70 54 65 72 6d    assert( (pTerm
28815 2d 3e 66 6c 61 67 73 20 26 20 54 45 52 4d 5f 43  ->flags & TERM_C
28816 4f 44 45 44 29 3d 3d 30 20 29 3b 0a 20 20 20 20  ODED)==0 );.    
28817 72 31 20 3d 20 63 6f 64 65 45 71 75 61 6c 69 74  r1 = codeEqualit
28818 79 54 65 72 6d 28 70 50 61 72 73 65 2c 20 70 54  yTerm(pParse, pT
28819 65 72 6d 2c 20 70 4c 65 76 65 6c 2c 20 72 65 67  erm, pLevel, reg
2881a 42 61 73 65 2b 6a 29 3b 0a 20 20 20 20 69 66 28  Base+j);.    if(
2881b 20 72 31 21 3d 72 65 67 42 61 73 65 2b 6a 20 29   r1!=regBase+j )
2881c 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
2881d 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2881e 53 43 6f 70 79 2c 20 72 31 2c 20 72 65 67 42 61  SCopy, r1, regBa
2881f 73 65 2b 6a 29 3b 0a 20 20 20 20 7d 0a 20 20 20  se+j);.    }.   
28820 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d   testcase( pTerm
28821 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f  ->eOperator & WO
28822 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 74  _ISNULL );.    t
28823 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e  estcase( pTerm->
28824 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 49  eOperator & WO_I
28825 4e 20 29 3b 0a 20 20 20 20 69 66 28 20 28 70 54  N );.    if( (pT
28826 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26  erm->eOperator &
28827 20 28 57 4f 5f 49 53 4e 55 4c 4c 7c 57 4f 5f 49   (WO_ISNULL|WO_I
28828 4e 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  N))==0 ){.      
28829 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2882a 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20  2(v, OP_IsNull, 
2882b 72 65 67 42 61 73 65 2b 6a 2c 20 70 4c 65 76 65  regBase+j, pLeve
2882c 6c 2d 3e 62 72 6b 29 3b 0a 20 20 20 20 7d 0a 20  l->brk);.    }. 
2882d 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 67 42   }.  return regB
2882e 61 73 65 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 69  ase;.}..#if defi
2882f 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29  ned(SQLITE_TEST)
28830 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
28831 77 69 6e 67 20 76 61 72 69 61 62 6c 65 20 68 6f  wing variable ho
28832 6c 64 73 20 61 20 74 65 78 74 20 64 65 73 63 72  lds a text descr
28833 69 70 74 69 6f 6e 20 6f 66 20 71 75 65 72 79 20  iption of query 
28834 70 6c 61 6e 20 67 65 6e 65 72 61 74 65 64 0a 2a  plan generated.*
28835 2a 20 62 79 20 74 68 65 20 6d 6f 73 74 20 72 65  * by the most re
28836 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c  cent call to sql
28837 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
28838 2e 20 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20  .  Each call to 
28839 57 68 65 72 65 42 65 67 69 6e 0a 2a 2a 20 6f 76  WhereBegin.** ov
2883a 65 72 77 72 69 74 65 73 20 74 68 65 20 70 72 65  erwrites the pre
2883b 76 69 6f 75 73 2e 20 20 54 68 69 73 20 69 6e 66  vious.  This inf
2883c 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75 73 65 64  ormation is used
2883d 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64   for testing and
2883e 0a 2a 2a 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c  .** analysis onl
2883f 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  y..*/.SQLITE_API
28840 20 63 68 61 72 20 73 71 6c 69 74 65 33 5f 71 75   char sqlite3_qu
28841 65 72 79 5f 70 6c 61 6e 5b 42 4d 53 2a 32 2a 34  ery_plan[BMS*2*4
28842 30 5d 3b 20 20 2f 2a 20 54 65 78 74 20 6f 66 20  0];  /* Text of 
28843 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 73 74 61 74  the join */.stat
28844 69 63 20 69 6e 74 20 6e 51 50 6c 61 6e 20 3d 20  ic int nQPlan = 
28845 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
28846 2f 2a 20 4e 65 78 74 20 66 72 65 65 20 73 6c 6f  /* Next free slo
28847 77 20 69 6e 20 5f 71 75 65 72 79 5f 70 6c 61 6e  w in _query_plan
28848 5b 5d 20 2a 2f 0a 0a 23 65 6e 64 69 66 20 2f 2a  [] */..#endif /*
28849 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f 0a   SQLITE_TEST */.
2884a 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 20 57  ../*.** Free a W
2884b 68 65 72 65 49 6e 66 6f 20 73 74 72 75 63 74 75  hereInfo structu
2884c 72 65 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  re.*/.static voi
2884d 64 20 77 68 65 72 65 49 6e 66 6f 46 72 65 65 28  d whereInfoFree(
2884e 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 57 68 65  sqlite3 *db, Whe
2884f 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 29 7b  reInfo *pWInfo){
28850 0a 20 20 69 66 28 20 70 57 49 6e 66 6f 20 29 7b  .  if( pWInfo ){
28851 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
28852 66 6f 72 28 69 3d 30 3b 20 69 3c 70 57 49 6e 66  for(i=0; i<pWInf
28853 6f 2d 3e 6e 4c 65 76 65 6c 3b 20 69 2b 2b 29 7b  o->nLevel; i++){
28854 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69  .      sqlite3_i
28855 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49 6e 66 6f  ndex_info *pInfo
28856 20 3d 20 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d 2e   = pWInfo->a[i].
28857 70 49 64 78 49 6e 66 6f 3b 0a 20 20 20 20 20 20  pIdxInfo;.      
28858 69 66 28 20 70 49 6e 66 6f 20 29 7b 0a 20 20 20  if( pInfo ){.   
28859 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e       assert( pIn
2885a 66 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64  fo->needToFreeId
2885b 78 53 74 72 3d 3d 30 20 29 3b 0a 20 20 20 20 20  xStr==0 );.     
2885c 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2885d 28 64 62 2c 20 70 49 6e 66 6f 29 3b 0a 20 20 20  (db, pInfo);.   
2885e 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
2885f 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
28860 20 70 57 49 6e 66 6f 29 3b 0a 20 20 7d 0a 7d 0a   pWInfo);.  }.}.
28861 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
28862 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
28863 66 20 74 68 65 20 6c 6f 6f 70 20 75 73 65 64 20  f the loop used 
28864 66 6f 72 20 57 48 45 52 45 20 63 6c 61 75 73 65  for WHERE clause
28865 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a 20   processing..** 
28866 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
28867 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
28868 20 61 6e 20 6f 70 61 71 75 65 20 73 74 72 75 63   an opaque struc
28869 74 75 72 65 20 74 68 61 74 20 63 6f 6e 74 61 69  ture that contai
2886a 6e 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f  ns.** informatio
2886b 6e 20 6e 65 65 64 65 64 20 74 6f 20 74 65 72 6d  n needed to term
2886c 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 2e 20  inate the loop. 
2886d 20 4c 61 74 65 72 2c 20 74 68 65 20 63 61 6c 6c   Later, the call
2886e 69 6e 67 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 73  ing routine.** s
2886f 68 6f 75 6c 64 20 69 6e 76 6f 6b 65 20 73 71 6c  hould invoke sql
28870 69 74 65 33 57 68 65 72 65 45 6e 64 28 29 20 77  ite3WhereEnd() w
28871 69 74 68 20 74 68 65 20 72 65 74 75 72 6e 20 76  ith the return v
28872 61 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e  alue of this fun
28873 63 74 69 6f 6e 0a 2a 2a 20 69 6e 20 6f 72 64 65  ction.** in orde
28874 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68  r to complete th
28875 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 70  e WHERE clause p
28876 72 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a 0a 2a 2a  rocessing..**.**
28877 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
28878 75 72 73 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  urs, this routin
28879 65 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a  e returns NULL..
2887a 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 73 69 63 20  **.** The basic 
2887b 69 64 65 61 20 69 73 20 74 6f 20 64 6f 20 61 20  idea is to do a 
2887c 6e 65 73 74 65 64 20 6c 6f 6f 70 2c 20 6f 6e 65  nested loop, one
2887d 20 6c 6f 6f 70 20 66 6f 72 20 65 61 63 68 20 74   loop for each t
2887e 61 62 6c 65 20 69 6e 0a 2a 2a 20 74 68 65 20 46  able in.** the F
2887f 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 20  ROM clause of a 
28880 73 65 6c 65 63 74 2e 20 20 28 49 4e 53 45 52 54  select.  (INSERT
28881 20 61 6e 64 20 55 50 44 41 54 45 20 73 74 61 74   and UPDATE stat
28882 65 6d 65 6e 74 73 20 61 72 65 20 74 68 65 0a 2a  ements are the.*
28883 2a 20 73 61 6d 65 20 61 73 20 61 20 53 45 4c 45  * same as a SELE
28884 43 54 20 77 69 74 68 20 6f 6e 6c 79 20 61 20 73  CT with only a s
28885 69 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 74  ingle table in t
28886 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 29  he FROM clause.)
28887 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65    For.** example
28888 2c 20 69 66 20 74 68 65 20 53 51 4c 20 69 73 20  , if the SQL is 
28889 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  this:.**.**     
2888a 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
2888b 74 31 2c 20 74 32 2c 20 74 33 20 57 48 45 52 45  t1, t2, t3 WHERE
2888c 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e   ...;.**.** Then
2888d 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61   the code genera
2888e 74 65 64 20 69 73 20 63 6f 6e 63 65 70 74 75 61  ted is conceptua
2888f 6c 6c 79 20 6c 69 6b 65 20 74 68 65 20 66 6f 6c  lly like the fol
28890 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  lowing:.**.**   
28891 20 20 20 66 6f 72 65 61 63 68 20 72 6f 77 31 20     foreach row1 
28892 69 6e 20 74 31 20 64 6f 20 20 20 20 20 20 20 5c  in t1 do       \
28893 20 20 20 20 43 6f 64 65 20 67 65 6e 65 72 61 74      Code generat
28894 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 66 6f 72  ed.**        for
28895 65 61 63 68 20 72 6f 77 32 20 69 6e 20 74 32 20  each row2 in t2 
28896 64 6f 20 20 20 20 20 20 7c 2d 2d 20 62 79 20 73  do      |-- by s
28897 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
28898 28 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66  ().**          f
28899 6f 72 65 61 63 68 20 72 6f 77 33 20 69 6e 20 74  oreach row3 in t
2889a 33 20 64 6f 20 20 20 2f 0a 2a 2a 20 20 20 20 20  3 do   /.**     
2889b 20 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20         ....**   
2889c 20 20 20 20 20 20 20 65 6e 64 20 20 20 20 20 20         end      
2889d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
2889e 20 20 20 20 43 6f 64 65 20 67 65 6e 65 72 61 74      Code generat
2889f 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 65 6e 64  ed.**        end
288a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
288a1 20 20 20 20 20 20 20 20 7c 2d 2d 20 62 79 20 73          |-- by s
288a2 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 29  qlite3WhereEnd()
288a3 0a 2a 2a 20 20 20 20 20 20 65 6e 64 20 20 20 20  .**      end    
288a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
288a5 20 20 20 20 20 2f 0a 2a 2a 0a 2a 2a 20 4e 6f 74       /.**.** Not
288a6 65 20 74 68 61 74 20 74 68 65 20 6c 6f 6f 70 73  e that the loops
288a7 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 6e 65   might not be ne
288a8 73 74 65 64 20 69 6e 20 74 68 65 20 6f 72 64 65  sted in the orde
288a9 72 20 69 6e 20 77 68 69 63 68 20 74 68 65 79 0a  r in which they.
288aa 2a 2a 20 61 70 70 65 61 72 20 69 6e 20 74 68 65  ** appear in the
288ab 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 66 20   FROM clause if 
288ac 61 20 64 69 66 66 65 72 65 6e 74 20 6f 72 64 65  a different orde
288ad 72 20 69 73 20 62 65 74 74 65 72 20 61 62 6c 65  r is better able
288ae 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 75 73 65 20   to make.** use 
288af 6f 66 20 69 6e 64 69 63 65 73 2e 20 20 4e 6f 74  of indices.  Not
288b0 65 20 61 6c 73 6f 20 74 68 61 74 20 77 68 65 6e  e also that when
288b1 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72   the IN operator
288b2 20 61 70 70 65 61 72 73 20 69 6e 0a 2a 2a 20 74   appears in.** t
288b3 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2c  he WHERE clause,
288b4 20 69 74 20 6d 69 67 68 74 20 72 65 73 75 6c 74   it might result
288b5 20 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 6e   in additional n
288b6 65 73 74 65 64 20 6c 6f 6f 70 73 20 66 6f 72 0a  ested loops for.
288b7 2a 2a 20 73 63 61 6e 6e 69 6e 67 20 74 68 72 6f  ** scanning thro
288b8 75 67 68 20 61 6c 6c 20 76 61 6c 75 65 73 20 6f  ugh all values o
288b9 6e 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64  n the right-hand
288ba 20 73 69 64 65 20 6f 66 20 74 68 65 20 49 4e 2e   side of the IN.
288bb 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65  .**.** There are
288bc 20 42 74 72 65 65 20 63 75 72 73 6f 72 73 20 61   Btree cursors a
288bd 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 65  ssociated with e
288be 61 63 68 20 74 61 62 6c 65 2e 20 20 74 31 20 75  ach table.  t1 u
288bf 73 65 73 20 63 75 72 73 6f 72 0a 2a 2a 20 6e 75  ses cursor.** nu
288c0 6d 62 65 72 20 70 54 61 62 4c 69 73 74 2d 3e 61  mber pTabList->a
288c1 5b 30 5d 2e 69 43 75 72 73 6f 72 2e 20 20 74 32  [0].iCursor.  t2
288c2 20 75 73 65 73 20 74 68 65 20 63 75 72 73 6f 72   uses the cursor
288c3 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 31 5d 2e   pTabList->a[1].
288c4 69 43 75 72 73 6f 72 2e 0a 2a 2a 20 41 6e 64 20  iCursor..** And 
288c5 73 6f 20 66 6f 72 74 68 2e 20 20 54 68 69 73 20  so forth.  This 
288c6 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65  routine generate
288c7 73 20 63 6f 64 65 20 74 6f 20 6f 70 65 6e 20 74  s code to open t
288c8 68 6f 73 65 20 56 44 42 45 20 63 75 72 73 6f 72  hose VDBE cursor
288c9 73 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33  s.** and sqlite3
288ca 57 68 65 72 65 45 6e 64 28 29 20 67 65 6e 65 72  WhereEnd() gener
288cb 61 74 65 73 20 74 68 65 20 63 6f 64 65 20 74 6f  ates the code to
288cc 20 63 6c 6f 73 65 20 74 68 65 6d 2e 0a 2a 2a 0a   close them..**.
288cd 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 68 61 74  ** The code that
288ce 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
288cf 69 6e 28 29 20 67 65 6e 65 72 61 74 65 73 20 6c  in() generates l
288d0 65 61 76 65 73 20 74 68 65 20 63 75 72 73 6f 72  eaves the cursor
288d1 73 20 6e 61 6d 65 64 0a 2a 2a 20 69 6e 20 70 54  s named.** in pT
288d2 61 62 4c 69 73 74 20 70 6f 69 6e 74 69 6e 67 20  abList pointing 
288d3 61 74 20 74 68 65 69 72 20 61 70 70 72 6f 70 72  at their appropr
288d4 69 61 74 65 20 65 6e 74 72 69 65 73 2e 20 20 54  iate entries.  T
288d5 68 65 20 5b 2e 2e 2e 5d 20 63 6f 64 65 0a 2a 2a  he [...] code.**
288d6 20 63 61 6e 20 75 73 65 20 4f 50 5f 43 6f 6c 75   can use OP_Colu
288d7 6d 6e 20 61 6e 64 20 4f 50 5f 52 6f 77 69 64 20  mn and OP_Rowid 
288d8 6f 70 63 6f 64 65 73 20 6f 6e 20 74 68 65 73 65  opcodes on these
288d9 20 63 75 72 73 6f 72 73 20 74 6f 20 65 78 74 72   cursors to extr
288da 61 63 74 0a 2a 2a 20 64 61 74 61 20 66 72 6f 6d  act.** data from
288db 20 74 68 65 20 76 61 72 69 6f 75 73 20 74 61 62   the various tab
288dc 6c 65 73 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e  les of the loop.
288dd 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 57 48  .**.** If the WH
288de 45 52 45 20 63 6c 61 75 73 65 20 69 73 20 65 6d  ERE clause is em
288df 70 74 79 2c 20 74 68 65 20 66 6f 72 65 61 63 68  pty, the foreach
288e0 20 6c 6f 6f 70 73 20 6d 75 73 74 20 65 61 63 68   loops must each
288e1 20 73 63 61 6e 20 74 68 65 69 72 0a 2a 2a 20 65   scan their.** e
288e2 6e 74 69 72 65 20 74 61 62 6c 65 73 2e 20 20 54  ntire tables.  T
288e3 68 75 73 20 61 20 74 68 72 65 65 2d 77 61 79 20  hus a three-way 
288e4 6a 6f 69 6e 20 69 73 20 61 6e 20 4f 28 4e 5e 33  join is an O(N^3
288e5 29 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 75  ) operation.  Bu
288e6 74 20 69 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c  t if.** the tabl
288e7 65 73 20 68 61 76 65 20 69 6e 64 69 63 65 73 20  es have indices 
288e8 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 74 65  and there are te
288e9 72 6d 73 20 69 6e 20 74 68 65 20 57 48 45 52 45  rms in the WHERE
288ea 20 63 6c 61 75 73 65 20 74 68 61 74 0a 2a 2a 20   clause that.** 
288eb 72 65 66 65 72 20 74 6f 20 74 68 6f 73 65 20 69  refer to those i
288ec 6e 64 69 63 65 73 2c 20 61 20 63 6f 6d 70 6c 65  ndices, a comple
288ed 74 65 20 74 61 62 6c 65 20 73 63 61 6e 20 63 61  te table scan ca
288ee 6e 20 62 65 20 61 76 6f 69 64 65 64 20 61 6e 64  n be avoided and
288ef 20 74 68 65 0a 2a 2a 20 63 6f 64 65 20 77 69 6c   the.** code wil
288f0 6c 20 72 75 6e 20 6d 75 63 68 20 66 61 73 74 65  l run much faste
288f1 72 2e 20 20 4d 6f 73 74 20 6f 66 20 74 68 65 20  r.  Most of the 
288f2 77 6f 72 6b 20 6f 66 20 74 68 69 73 20 72 6f 75  work of this rou
288f3 74 69 6e 65 20 69 73 20 63 68 65 63 6b 69 6e 67  tine is checking
288f4 0a 2a 2a 20 74 6f 20 73 65 65 20 69 66 20 74 68  .** to see if th
288f5 65 72 65 20 61 72 65 20 69 6e 64 69 63 65 73 20  ere are indices 
288f6 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65 64  that can be used
288f7 20 74 6f 20 73 70 65 65 64 20 75 70 20 74 68 65   to speed up the
288f8 20 6c 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 65 72   loop..**.** Ter
288f9 6d 73 20 6f 66 20 74 68 65 20 57 48 45 52 45 20  ms of the WHERE 
288fa 63 6c 61 75 73 65 20 61 72 65 20 61 6c 73 6f 20  clause are also 
288fb 75 73 65 64 20 74 6f 20 6c 69 6d 69 74 20 77 68  used to limit wh
288fc 69 63 68 20 72 6f 77 73 20 61 63 74 75 61 6c 6c  ich rows actuall
288fd 79 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 74 6f 20  y.** make it to 
288fe 74 68 65 20 22 2e 2e 2e 22 20 69 6e 20 74 68 65  the "..." in the
288ff 20 6d 69 64 64 6c 65 20 6f 66 20 74 68 65 20 6c   middle of the l
28900 6f 6f 70 2e 20 20 41 66 74 65 72 20 65 61 63 68  oop.  After each
28901 20 22 66 6f 72 65 61 63 68 22 2c 0a 2a 2a 20 74   "foreach",.** t
28902 65 72 6d 73 20 6f 66 20 74 68 65 20 57 48 45 52  erms of the WHER
28903 45 20 63 6c 61 75 73 65 20 74 68 61 74 20 75 73  E clause that us
28904 65 20 6f 6e 6c 79 20 74 65 72 6d 73 20 69 6e 20  e only terms in 
28905 74 68 61 74 20 6c 6f 6f 70 20 61 6e 64 20 6f 75  that loop and ou
28906 74 65 72 0a 2a 2a 20 6c 6f 6f 70 73 20 61 72 65  ter.** loops are
28907 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 69   evaluated and i
28908 66 20 66 61 6c 73 65 20 61 20 6a 75 6d 70 20 69  f false a jump i
28909 73 20 6d 61 64 65 20 61 72 6f 75 6e 64 20 61 6c  s made around al
2890a 6c 20 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20  l subsequent.** 
2890b 69 6e 6e 65 72 20 6c 6f 6f 70 73 20 28 6f 72 20  inner loops (or 
2890c 61 72 6f 75 6e 64 20 74 68 65 20 22 2e 2e 2e 22  around the "..."
2890d 20 69 66 20 74 68 65 20 74 65 73 74 20 6f 63 63   if the test occ
2890e 75 72 73 20 77 69 74 68 69 6e 20 74 68 65 20 69  urs within the i
2890f 6e 6e 65 72 2d 0a 2a 2a 20 6d 6f 73 74 20 6c 6f  nner-.** most lo
28910 6f 70 29 0a 2a 2a 0a 2a 2a 20 4f 55 54 45 52 20  op).**.** OUTER 
28911 4a 4f 49 4e 53 0a 2a 2a 0a 2a 2a 20 41 6e 20 6f  JOINS.**.** An o
28912 75 74 65 72 20 6a 6f 69 6e 20 6f 66 20 74 61 62  uter join of tab
28913 6c 65 73 20 74 31 20 61 6e 64 20 74 32 20 69 73  les t1 and t2 is
28914 20 63 6f 6e 63 65 70 74 61 6c 6c 79 20 63 6f 64   conceptally cod
28915 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  ed as follows:.*
28916 2a 0a 2a 2a 20 20 20 20 66 6f 72 65 61 63 68 20  *.**    foreach 
28917 72 6f 77 31 20 69 6e 20 74 31 20 64 6f 0a 2a 2a  row1 in t1 do.**
28918 20 20 20 20 20 20 66 6c 61 67 20 3d 20 30 0a 2a        flag = 0.*
28919 2a 20 20 20 20 20 20 66 6f 72 65 61 63 68 20 72  *      foreach r
2891a 6f 77 32 20 69 6e 20 74 32 20 64 6f 0a 2a 2a 20  ow2 in t2 do.** 
2891b 20 20 20 20 20 20 20 73 74 61 72 74 3a 0a 2a 2a         start:.**
2891c 20 20 20 20 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a            ....**
2891d 20 20 20 20 20 20 20 20 20 20 66 6c 61 67 20 3d            flag =
2891e 20 31 0a 2a 2a 20 20 20 20 20 20 65 6e 64 0a 2a   1.**      end.*
2891f 2a 20 20 20 20 20 20 69 66 20 66 6c 61 67 3d 3d  *      if flag==
28920 30 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20  0 then.**       
28921 20 6d 6f 76 65 20 74 68 65 20 72 6f 77 32 20 63   move the row2 c
28922 75 72 73 6f 72 20 74 6f 20 61 20 6e 75 6c 6c 20  ursor to a null 
28923 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 67 6f  row.**        go
28924 74 6f 20 73 74 61 72 74 0a 2a 2a 20 20 20 20 20  to start.**     
28925 20 66 69 0a 2a 2a 20 20 20 20 65 6e 64 0a 2a 2a   fi.**    end.**
28926 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 43 4c 41  .** ORDER BY CLA
28927 55 53 45 20 50 52 4f 43 45 53 53 49 4e 47 0a 2a  USE PROCESSING.*
28928 2a 0a 2a 2a 20 2a 70 70 4f 72 64 65 72 42 79 20  *.** *ppOrderBy 
28929 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
2892a 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
2892b 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54 20  use of a SELECT 
2892c 73 74 61 74 65 6d 65 6e 74 2c 0a 2a 2a 20 69 66  statement,.** if
2892d 20 74 68 65 72 65 20 69 73 20 6f 6e 65 2e 20 20   there is one.  
2892e 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 4f  If there is no O
2892f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 6f  RDER BY clause o
28930 72 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e  r if this routin
28931 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 66  e.** is called f
28932 72 6f 6d 20 61 6e 20 55 50 44 41 54 45 20 6f 72  rom an UPDATE or
28933 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e   DELETE statemen
28934 74 2c 20 74 68 65 6e 20 70 70 4f 72 64 65 72 42  t, then ppOrderB
28935 79 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  y is NULL..**.**
28936 20 49 66 20 61 6e 20 69 6e 64 65 78 20 63 61 6e   If an index can
28937 20 62 65 20 75 73 65 64 20 73 6f 20 74 68 61 74   be used so that
28938 20 74 68 65 20 6e 61 74 75 72 61 6c 20 6f 75 74   the natural out
28939 70 75 74 20 6f 72 64 65 72 20 6f 66 20 74 68 65  put order of the
2893a 20 74 61 62 6c 65 0a 2a 2a 20 73 63 61 6e 20 69   table.** scan i
2893b 73 20 63 6f 72 72 65 63 74 20 66 6f 72 20 74 68  s correct for th
2893c 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
2893d 65 2c 20 74 68 65 6e 20 74 68 61 74 20 69 6e 64  e, then that ind
2893e 65 78 20 69 73 20 75 73 65 64 20 61 6e 64 0a 2a  ex is used and.*
2893f 2a 20 2a 70 70 4f 72 64 65 72 42 79 20 69 73 20  * *ppOrderBy is 
28940 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 20 54 68  set to NULL.  Th
28941 69 73 20 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a  is is an optimiz
28942 61 74 69 6f 6e 20 74 68 61 74 20 70 72 65 76 65  ation that preve
28943 6e 74 73 20 61 6e 0a 2a 2a 20 75 6e 6e 65 63 65  nts an.** unnece
28944 73 73 61 72 79 20 73 6f 72 74 20 6f 66 20 74 68  ssary sort of th
28945 65 20 72 65 73 75 6c 74 20 73 65 74 20 69 66 20  e result set if 
28946 61 6e 20 69 6e 64 65 78 20 61 70 70 72 6f 70 72  an index appropr
28947 69 61 74 65 20 66 6f 72 20 74 68 65 0a 2a 2a 20  iate for the.** 
28948 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
28949 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e 0a  already exists..
2894a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 77 68 65  **.** If the whe
2894b 72 65 20 63 6c 61 75 73 65 20 6c 6f 6f 70 73 20  re clause loops 
2894c 63 61 6e 6e 6f 74 20 62 65 20 61 72 72 61 6e 67  cannot be arrang
2894d 65 64 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68  ed to provide th
2894e 65 20 63 6f 72 72 65 63 74 0a 2a 2a 20 6f 75 74  e correct.** out
2894f 70 75 74 20 6f 72 64 65 72 2c 20 74 68 65 6e 20  put order, then 
28950 74 68 65 20 2a 70 70 4f 72 64 65 72 42 79 20 69  the *ppOrderBy i
28951 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a  s unchanged..*/.
28952 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 57  SQLITE_PRIVATE W
28953 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69 74 65  hereInfo *sqlite
28954 33 57 68 65 72 65 42 65 67 69 6e 28 0a 20 20 50  3WhereBegin(.  P
28955 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
28956 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
28957 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
28958 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
28959 74 2c 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20  t,    /* A list 
2895a 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 74 6f  of all tables to
2895b 20 62 65 20 73 63 61 6e 6e 65 64 20 2a 2f 0a 20   be scanned */. 
2895c 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20   Expr *pWhere,  
2895d 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48         /* The WH
2895e 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ERE clause */.  
2895f 45 78 70 72 4c 69 73 74 20 2a 2a 70 70 4f 72 64  ExprList **ppOrd
28960 65 72 42 79 2c 20 2f 2a 20 41 6e 20 4f 52 44 45  erBy, /* An ORDE
28961 52 20 42 59 20 63 6c 61 75 73 65 2c 20 6f 72 20  R BY clause, or 
28962 4e 55 4c 4c 20 2a 2f 0a 20 20 75 38 20 77 66 6c  NULL */.  u8 wfl
28963 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 20  ags             
28964 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 57 48  /* One of the WH
28965 45 52 45 5f 2a 20 66 6c 61 67 73 20 64 65 66 69  ERE_* flags defi
28966 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74  ned in sqliteInt
28967 2e 68 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  .h */.){.  int i
28968 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
28969 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
2896a 75 6e 74 65 72 20 2a 2f 0a 20 20 57 68 65 72 65  unter */.  Where
2896b 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20 20  Info *pWInfo;   
2896c 20 20 20 20 20 20 2f 2a 20 57 69 6c 6c 20 62 65        /* Will be
2896d 63 6f 6d 65 20 74 68 65 20 72 65 74 75 72 6e 20  come the return 
2896e 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75  value of this fu
2896f 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 56 64 62 65  nction */.  Vdbe
28970 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
28971 64 62 65 3b 20 20 20 2f 2a 20 54 68 65 20 76 69  dbe;   /* The vi
28972 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65  rtual database e
28973 6e 67 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 62  ngine */.  int b
28974 72 6b 2c 20 63 6f 6e 74 20 3d 20 30 3b 20 20 20  rk, cont = 0;   
28975 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
28976 65 73 20 75 73 65 64 20 64 75 72 69 6e 67 20 63  es used during c
28977 6f 64 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 2a  ode generation *
28978 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52  /.  Bitmask notR
28979 65 61 64 79 3b 20 20 20 20 20 20 20 20 20 20 2f  eady;          /
2897a 2a 20 43 75 72 73 6f 72 73 20 74 68 61 74 20 61  * Cursors that a
2897b 72 65 20 6e 6f 74 20 79 65 74 20 70 6f 73 69 74  re not yet posit
2897c 69 6f 6e 65 64 20 2a 2f 0a 20 20 57 68 65 72 65  ioned */.  Where
2897d 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20  Term *pTerm;    
2897e 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c        /* A singl
2897f 65 20 74 65 72 6d 20 69 6e 20 74 68 65 20 57 48  e term in the WH
28980 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ERE clause */.  
28981 45 78 70 72 4d 61 73 6b 53 65 74 20 6d 61 73 6b  ExprMaskSet mask
28982 53 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 54 68  Set;       /* Th
28983 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 73  e expression mas
28984 6b 20 73 65 74 20 2a 2f 0a 20 20 57 68 65 72 65  k set */.  Where
28985 43 6c 61 75 73 65 20 77 63 3b 20 20 20 20 20 20  Clause wc;      
28986 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45        /* The WHE
28987 52 45 20 63 6c 61 75 73 65 20 69 73 20 64 69 76  RE clause is div
28988 69 64 65 64 20 69 6e 74 6f 20 74 68 65 73 65 20  ided into these 
28989 74 65 72 6d 73 20 2a 2f 0a 20 20 73 74 72 75 63  terms */.  struc
2898a 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
2898b 70 54 61 62 49 74 65 6d 3b 20 20 2f 2a 20 41 20  pTabItem;  /* A 
2898c 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 66 72 6f  single entry fro
2898d 6d 20 70 54 61 62 4c 69 73 74 20 2a 2f 0a 20 20  m pTabList */.  
2898e 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76  WhereLevel *pLev
2898f 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  el;             
28990 2f 2a 20 41 20 73 69 6e 67 6c 65 20 6c 65 76 65  /* A single leve
28991 6c 20 69 6e 20 74 68 65 20 70 57 49 6e 66 6f 20  l in the pWInfo 
28992 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 46  list */.  int iF
28993 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  rom;            
28994 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
28995 73 74 20 75 6e 75 73 65 64 20 46 52 4f 4d 20 63  st unused FROM c
28996 6c 61 75 73 65 20 65 6c 65 6d 65 6e 74 20 2a 2f  lause element */
28997 0a 20 20 69 6e 74 20 61 6e 64 46 6c 61 67 73 3b  .  int andFlags;
28998 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
28999 20 41 4e 44 2d 65 64 20 63 6f 6d 62 69 6e 61 74   AND-ed combinat
2899a 69 6f 6e 20 6f 66 20 61 6c 6c 20 77 63 2e 61 5b  ion of all wc.a[
2899b 5d 2e 66 6c 61 67 73 20 2a 2f 0a 20 20 73 71 6c  ].flags */.  sql
2899c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
2899d 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
2899e 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
2899f 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f  /.  ExprList *pO
289a0 72 64 65 72 42 79 20 3d 20 30 3b 0a 0a 20 20 2f  rderBy = 0;..  /
289a1 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * The number of 
289a2 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 46 52  tables in the FR
289a3 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 6c 69 6d  OM clause is lim
289a4 69 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d 62  ited by the numb
289a5 65 72 20 6f 66 0a 20 20 2a 2a 20 62 69 74 73 20  er of.  ** bits 
289a6 69 6e 20 61 20 42 69 74 6d 61 73 6b 20 0a 20 20  in a Bitmask .  
289a7 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 4c 69 73  */.  if( pTabLis
289a8 74 2d 3e 6e 53 72 63 3e 42 4d 53 20 29 7b 0a 20  t->nSrc>BMS ){. 
289a9 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
289aa 73 67 28 70 50 61 72 73 65 2c 20 22 61 74 20 6d  sg(pParse, "at m
289ab 6f 73 74 20 25 64 20 74 61 62 6c 65 73 20 69 6e  ost %d tables in
289ac 20 61 20 6a 6f 69 6e 22 2c 20 42 4d 53 29 3b 0a   a join", BMS);.
289ad 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
289ae 7d 0a 0a 20 20 69 66 28 20 70 70 4f 72 64 65 72  }..  if( ppOrder
289af 42 79 20 29 7b 0a 20 20 20 20 70 4f 72 64 65 72  By ){.    pOrder
289b0 42 79 20 3d 20 2a 70 70 4f 72 64 65 72 42 79 3b  By = *ppOrderBy;
289b1 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 70 6c 69 74  .  }..  /* Split
289b2 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
289b3 65 20 69 6e 74 6f 20 73 65 70 61 72 61 74 65 20  e into separate 
289b4 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20 77  subexpressions w
289b5 68 65 72 65 20 65 61 63 68 0a 20 20 2a 2a 20 73  here each.  ** s
289b6 75 62 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  ubexpression is 
289b7 73 65 70 61 72 61 74 65 64 20 62 79 20 61 6e 20  separated by an 
289b8 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20  AND operator..  
289b9 2a 2f 0a 20 20 69 6e 69 74 4d 61 73 6b 53 65 74  */.  initMaskSet
289ba 28 26 6d 61 73 6b 53 65 74 29 3b 0a 20 20 77 68  (&maskSet);.  wh
289bb 65 72 65 43 6c 61 75 73 65 49 6e 69 74 28 26 77  ereClauseInit(&w
289bc 63 2c 20 70 50 61 72 73 65 2c 20 26 6d 61 73 6b  c, pParse, &mask
289bd 53 65 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  Set);.  sqlite3E
289be 78 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73  xprCodeConstants
289bf 28 70 50 61 72 73 65 2c 20 70 57 68 65 72 65 29  (pParse, pWhere)
289c0 3b 0a 20 20 77 68 65 72 65 53 70 6c 69 74 28 26  ;.  whereSplit(&
289c1 77 63 2c 20 70 57 68 65 72 65 2c 20 54 4b 5f 41  wc, pWhere, TK_A
289c2 4e 44 29 3b 0a 20 20 20 20 0a 20 20 2f 2a 20 41  ND);.    .  /* A
289c3 6c 6c 6f 63 61 74 65 20 61 6e 64 20 69 6e 69 74  llocate and init
289c4 69 61 6c 69 7a 65 20 74 68 65 20 57 68 65 72 65  ialize the Where
289c5 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74  Info structure t
289c6 68 61 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20  hat will become 
289c7 74 68 65 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20  the.  ** return 
289c8 76 61 6c 75 65 2e 0a 20 20 2a 2f 0a 20 20 64 62  value..  */.  db
289c9 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
289ca 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65   pWInfo = sqlite
289cb 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
289cc 2c 20 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  ,  .            
289cd 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66            sizeof
289ce 28 57 68 65 72 65 49 6e 66 6f 29 20 2b 20 70 54  (WhereInfo) + pT
289cf 61 62 4c 69 73 74 2d 3e 6e 53 72 63 2a 73 69 7a  abList->nSrc*siz
289d0 65 6f 66 28 57 68 65 72 65 4c 65 76 65 6c 29 29  eof(WhereLevel))
289d1 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  ;.  if( db->mall
289d2 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
289d3 67 6f 74 6f 20 77 68 65 72 65 42 65 67 69 6e 45  goto whereBeginE
289d4 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 70 57 49 6e  rror;.  }.  pWIn
289d5 66 6f 2d 3e 6e 4c 65 76 65 6c 20 3d 20 70 54 61  fo->nLevel = pTa
289d6 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 0a 20 20 70  bList->nSrc;.  p
289d7 57 49 6e 66 6f 2d 3e 70 50 61 72 73 65 20 3d 20  WInfo->pParse = 
289d8 70 50 61 72 73 65 3b 0a 20 20 70 57 49 6e 66 6f  pParse;.  pWInfo
289d9 2d 3e 70 54 61 62 4c 69 73 74 20 3d 20 70 54 61  ->pTabList = pTa
289da 62 4c 69 73 74 3b 0a 20 20 70 57 49 6e 66 6f 2d  bList;.  pWInfo-
289db 3e 69 42 72 65 61 6b 20 3d 20 73 71 6c 69 74 65  >iBreak = sqlite
289dc 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
289dd 29 3b 0a 0a 20 20 2f 2a 20 53 70 65 63 69 61 6c  );..  /* Special
289de 20 63 61 73 65 3a 20 61 20 57 48 45 52 45 20 63   case: a WHERE c
289df 6c 61 75 73 65 20 74 68 61 74 20 69 73 20 63 6f  lause that is co
289e0 6e 73 74 61 6e 74 2e 20 20 45 76 61 6c 75 61 74  nstant.  Evaluat
289e1 65 20 74 68 65 0a 20 20 2a 2a 20 65 78 70 72 65  e the.  ** expre
289e2 73 73 69 6f 6e 20 61 6e 64 20 65 69 74 68 65 72  ssion and either
289e3 20 6a 75 6d 70 20 6f 76 65 72 20 61 6c 6c 20 6f   jump over all o
289e4 66 20 74 68 65 20 63 6f 64 65 20 6f 72 20 66 61  f the code or fa
289e5 6c 6c 20 74 68 72 75 2e 0a 20 20 2a 2f 0a 20 20  ll thru..  */.  
289e6 69 66 28 20 70 57 68 65 72 65 20 26 26 20 28 70  if( pWhere && (p
289e7 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 30  TabList->nSrc==0
289e8 20 7c 7c 20 73 71 6c 69 74 65 33 45 78 70 72 49   || sqlite3ExprI
289e9 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e  sConstantNotJoin
289ea 28 70 57 68 65 72 65 29 29 20 29 7b 0a 20 20 20  (pWhere)) ){.   
289eb 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61   sqlite3ExprIfFa
289ec 6c 73 65 28 70 50 61 72 73 65 2c 20 70 57 68 65  lse(pParse, pWhe
289ed 72 65 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65  re, pWInfo->iBre
289ee 61 6b 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  ak, SQLITE_JUMPI
289ef 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 70 57 68 65  FNULL);.    pWhe
289f0 72 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  re = 0;.  }..  /
289f1 2a 20 41 73 73 69 67 6e 20 61 20 62 69 74 20 66  * Assign a bit f
289f2 72 6f 6d 20 74 68 65 20 62 69 74 6d 61 73 6b 20  rom the bitmask 
289f3 74 6f 20 65 76 65 72 79 20 74 65 72 6d 20 69 6e  to every term in
289f4 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
289f5 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e  ..  **.  ** When
289f6 20 61 73 73 69 67 6e 69 6e 67 20 62 69 74 6d 61   assigning bitma
289f7 73 6b 20 76 61 6c 75 65 73 20 74 6f 20 46 52 4f  sk values to FRO
289f8 4d 20 63 6c 61 75 73 65 20 63 75 72 73 6f 72 73  M clause cursors
289f9 2c 20 69 74 20 6d 75 73 74 20 62 65 0a 20 20 2a  , it must be.  *
289fa 2a 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20  * the case that 
289fb 69 66 20 58 20 69 73 20 74 68 65 20 62 69 74 6d  if X is the bitm
289fc 61 73 6b 20 66 6f 72 20 74 68 65 20 4e 2d 74 68  ask for the N-th
289fd 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72   FROM clause ter
289fe 6d 20 74 68 65 6e 0a 20 20 2a 2a 20 74 68 65 20  m then.  ** the 
289ff 62 69 74 6d 61 73 6b 20 66 6f 72 20 61 6c 6c 20  bitmask for all 
28a00 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d  FROM clause term
28a01 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66  s to the left of
28a02 20 74 68 65 20 4e 2d 74 68 20 74 65 72 6d 0a 20   the N-th term. 
28a03 20 2a 2a 20 69 73 20 28 58 2d 31 29 2e 20 20 20   ** is (X-1).   
28a04 41 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 66 72  An expression fr
28a05 6f 6d 20 74 68 65 20 4f 4e 20 63 6c 61 75 73 65  om the ON clause
28a06 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 20   of a LEFT JOIN 
28a07 63 61 6e 20 75 73 65 0a 20 20 2a 2a 20 69 74 73  can use.  ** its
28a08 20 45 78 70 72 2e 69 52 69 67 68 74 4a 6f 69 6e   Expr.iRightJoin
28a09 54 61 62 6c 65 20 76 61 6c 75 65 20 74 6f 20 66  Table value to f
28a0a 69 6e 64 20 74 68 65 20 62 69 74 6d 61 73 6b 20  ind the bitmask 
28a0b 6f 66 20 74 68 65 20 72 69 67 68 74 20 74 61 62  of the right tab
28a0c 6c 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 6a  le.  ** of the j
28a0d 6f 69 6e 2e 20 20 53 75 62 74 72 61 63 74 69 6e  oin.  Subtractin
28a0e 67 20 6f 6e 65 20 66 72 6f 6d 20 74 68 65 20 72  g one from the r
28a0f 69 67 68 74 20 74 61 62 6c 65 20 62 69 74 6d 61  ight table bitma
28a10 73 6b 20 67 69 76 65 73 20 61 0a 20 20 2a 2a 20  sk gives a.  ** 
28a11 62 69 74 6d 61 73 6b 20 66 6f 72 20 61 6c 6c 20  bitmask for all 
28a12 74 61 62 6c 65 73 20 74 6f 20 74 68 65 20 6c 65  tables to the le
28a13 66 74 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 20  ft of the join. 
28a14 20 4b 6e 6f 77 69 6e 67 20 74 68 65 20 62 69 74   Knowing the bit
28a15 6d 61 73 6b 0a 20 20 2a 2a 20 66 6f 72 20 61 6c  mask.  ** for al
28a16 6c 20 74 61 62 6c 65 73 20 74 6f 20 74 68 65 20  l tables to the 
28a17 6c 65 66 74 20 6f 66 20 61 20 6c 65 66 74 20 6a  left of a left j
28a18 6f 69 6e 20 69 73 20 69 6d 70 6f 72 74 61 6e 74  oin is important
28a19 2e 20 20 54 69 63 6b 65 74 20 23 33 30 31 35 2e  .  Ticket #3015.
28a1a 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b  .  */.  for(i=0;
28a1b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72   i<pTabList->nSr
28a1c 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 72 65  c; i++){.    cre
28a1d 61 74 65 4d 61 73 6b 28 26 6d 61 73 6b 53 65 74  ateMask(&maskSet
28a1e 2c 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d  , pTabList->a[i]
28a1f 2e 69 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 23  .iCursor);.  }.#
28a20 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
28a21 7b 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 74 6f  {.    Bitmask to
28a22 54 68 65 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20  TheLeft = 0;.   
28a23 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62   for(i=0; i<pTab
28a24 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29  List->nSrc; i++)
28a25 7b 0a 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20  {.      Bitmask 
28a26 6d 20 3d 20 67 65 74 4d 61 73 6b 28 26 6d 61 73  m = getMask(&mas
28a27 6b 53 65 74 2c 20 70 54 61 62 4c 69 73 74 2d 3e  kSet, pTabList->
28a28 61 5b 69 5d 2e 69 43 75 72 73 6f 72 29 3b 0a 20  a[i].iCursor);. 
28a29 20 20 20 20 20 61 73 73 65 72 74 28 20 28 6d 2d       assert( (m-
28a2a 31 29 3d 3d 74 6f 54 68 65 4c 65 66 74 20 29 3b  1)==toTheLeft );
28a2b 0a 20 20 20 20 20 20 74 6f 54 68 65 4c 65 66 74  .      toTheLeft
28a2c 20 7c 3d 20 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d   |= m;.    }.  }
28a2d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 6e  .#endif..  /* An
28a2e 61 6c 79 7a 65 20 61 6c 6c 20 6f 66 20 74 68 65  alyze all of the
28a2f 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 2e   subexpressions.
28a30 20 20 4e 6f 74 65 20 74 68 61 74 20 65 78 70 72    Note that expr
28a31 41 6e 61 6c 79 7a 65 28 29 20 6d 69 67 68 74 0a  Analyze() might.
28a32 20 20 2a 2a 20 61 64 64 20 6e 65 77 20 76 69 72    ** add new vir
28a33 74 75 61 6c 20 74 65 72 6d 73 20 6f 6e 74 6f 20  tual terms onto 
28a34 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 57  the end of the W
28a35 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 57 65  HERE clause.  We
28a36 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 77 61 6e   do not.  ** wan
28a37 74 20 74 6f 20 61 6e 61 6c 79 7a 65 20 74 68 65  t to analyze the
28a38 73 65 20 76 69 72 74 75 61 6c 20 74 65 72 6d 73  se virtual terms
28a39 2c 20 73 6f 20 73 74 61 72 74 20 61 6e 61 6c 79  , so start analy
28a3a 7a 69 6e 67 20 61 74 20 74 68 65 20 65 6e 64 0a  zing at the end.
28a3b 20 20 2a 2a 20 61 6e 64 20 77 6f 72 6b 20 66 6f    ** and work fo
28a3c 72 77 61 72 64 20 73 6f 20 74 68 61 74 20 74 68  rward so that th
28a3d 65 20 61 64 64 65 64 20 76 69 72 74 75 61 6c 20  e added virtual 
28a3e 74 65 72 6d 73 20 61 72 65 20 6e 65 76 65 72 20  terms are never 
28a3f 70 72 6f 63 65 73 73 65 64 2e 0a 20 20 2a 2f 0a  processed..  */.
28a40 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c 6c    exprAnalyzeAll
28a41 28 70 54 61 62 4c 69 73 74 2c 20 26 77 63 29 3b  (pTabList, &wc);
28a42 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
28a43 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67  cFailed ){.    g
28a44 6f 74 6f 20 77 68 65 72 65 42 65 67 69 6e 45 72  oto whereBeginEr
28a45 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  ror;.  }..  /* C
28a46 68 6f 73 65 20 74 68 65 20 62 65 73 74 20 69 6e  hose the best in
28a47 64 65 78 20 74 6f 20 75 73 65 20 66 6f 72 20 65  dex to use for e
28a48 61 63 68 20 74 61 62 6c 65 20 69 6e 20 74 68 65  ach table in the
28a49 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 20 20   FROM clause..  
28a4a 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 6c 6f 6f  **.  ** This loo
28a4b 70 20 66 69 6c 6c 73 20 69 6e 20 74 68 65 20 66  p fills in the f
28a4c 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 73 3a  ollowing fields:
28a4d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 70 57 49  .  **.  **   pWI
28a4e 6e 66 6f 2d 3e 61 5b 5d 2e 70 49 64 78 20 20 20  nfo->a[].pIdx   
28a4f 20 20 20 54 68 65 20 69 6e 64 65 78 20 74 6f 20     The index to 
28a50 75 73 65 20 66 6f 72 20 74 68 69 73 20 6c 65 76  use for this lev
28a51 65 6c 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 0a  el of the loop..
28a52 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61    **   pWInfo->a
28a53 5b 5d 2e 66 6c 61 67 73 20 20 20 20 20 57 48 45  [].flags     WHE
28a54 52 45 5f 78 78 78 20 66 6c 61 67 73 20 61 73 73  RE_xxx flags ass
28a55 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 49 64  ociated with pId
28a56 78 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d  x.  **   pWInfo-
28a57 3e 61 5b 5d 2e 6e 45 71 20 20 20 20 20 20 20 54  >a[].nEq       T
28a58 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 3d 3d 20  he number of == 
28a59 61 6e 64 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e  and IN constrain
28a5a 74 73 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f  ts.  **   pWInfo
28a5b 2d 3e 61 5b 5d 2e 69 46 72 6f 6d 20 20 20 20 20  ->a[].iFrom     
28a5c 57 68 69 63 68 20 74 65 72 6d 20 6f 66 20 74 68  Which term of th
28a5d 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73  e FROM clause is
28a5e 20 62 65 69 6e 67 20 63 6f 64 65 64 0a 20 20 2a   being coded.  *
28a5f 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e  *   pWInfo->a[].
28a60 69 54 61 62 43 75 72 20 20 20 54 68 65 20 56 44  iTabCur   The VD
28a61 42 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68  BE cursor for th
28a62 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  e database table
28a63 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e  .  **   pWInfo->
28a64 61 5b 5d 2e 69 49 64 78 43 75 72 20 20 20 54 68  a[].iIdxCur   Th
28a65 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 66 6f  e VDBE cursor fo
28a66 72 20 74 68 65 20 69 6e 64 65 78 0a 20 20 2a 2a  r the index.  **
28a67 0a 20 20 2a 2a 20 54 68 69 73 20 6c 6f 6f 70 20  .  ** This loop 
28a68 61 6c 73 6f 20 66 69 67 75 72 65 73 20 6f 75 74  also figures out
28a69 20 74 68 65 20 6e 65 73 74 69 6e 67 20 6f 72 64   the nesting ord
28a6a 65 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20  er of tables in 
28a6b 74 68 65 20 46 52 4f 4d 0a 20 20 2a 2a 20 63 6c  the FROM.  ** cl
28a6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 6e 6f 74  ause..  */.  not
28a6d 52 65 61 64 79 20 3d 20 7e 28 42 69 74 6d 61 73  Ready = ~(Bitmas
28a6e 6b 29 30 3b 0a 20 20 70 54 61 62 49 74 65 6d 20  k)0;.  pTabItem 
28a6f 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 3b 0a 20  = pTabList->a;. 
28a70 20 70 4c 65 76 65 6c 20 3d 20 70 57 49 6e 66 6f   pLevel = pWInfo
28a71 2d 3e 61 3b 0a 20 20 61 6e 64 46 6c 61 67 73 20  ->a;.  andFlags 
28a72 3d 20 7e 30 3b 0a 20 20 57 48 45 52 45 54 52 41  = ~0;.  WHERETRA
28a73 43 45 28 28 22 2a 2a 2a 20 4f 70 74 69 6d 69 7a  CE(("*** Optimiz
28a74 65 72 20 53 74 61 72 74 20 2a 2a 2a 5c 6e 22 29  er Start ***\n")
28a75 29 3b 0a 20 20 66 6f 72 28 69 3d 69 46 72 6f 6d  );.  for(i=iFrom
28a76 3d 30 2c 20 70 4c 65 76 65 6c 3d 70 57 49 6e 66  =0, pLevel=pWInf
28a77 6f 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73 74  o->a; i<pTabList
28a78 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 4c 65  ->nSrc; i++, pLe
28a79 76 65 6c 2b 2b 29 7b 0a 20 20 20 20 49 6e 64 65  vel++){.    Inde
28a7a 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20  x *pIdx;        
28a7b 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
28a7c 20 66 6f 72 20 46 52 4f 4d 20 74 61 62 6c 65 20   for FROM table 
28a7d 61 74 20 70 54 61 62 49 74 65 6d 20 2a 2f 0a 20  at pTabItem */. 
28a7e 20 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20     int flags;   
28a7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
28a80 2a 20 46 6c 61 67 73 20 61 73 73 73 6f 63 69 61  * Flags asssocia
28a81 74 65 64 20 77 69 74 68 20 70 49 64 78 20 2a 2f  ted with pIdx */
28a82 0a 20 20 20 20 69 6e 74 20 6e 45 71 3b 20 20 20  .    int nEq;   
28a83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28a84 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 3d 3d   /* Number of ==
28a85 20 6f 72 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e   or IN constrain
28a86 74 73 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65  ts */.    double
28a87 20 63 6f 73 74 3b 20 20 20 20 20 20 20 20 20 20   cost;          
28a88 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 73        /* The cos
28a89 74 20 66 6f 72 20 70 49 64 78 20 2a 2f 0a 20 20  t for pIdx */.  
28a8a 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 20    int j;        
28a8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
28a8c 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65   For looping ove
28a8d 72 20 46 52 4f 4d 20 74 61 62 6c 65 73 20 2a 2f  r FROM tables */
28a8e 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 42 65 73  .    Index *pBes
28a8f 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  t = 0;          
28a90 20 2f 2a 20 54 68 65 20 62 65 73 74 20 69 6e 64   /* The best ind
28a91 65 78 20 73 65 65 6e 20 73 6f 20 66 61 72 20 2a  ex seen so far *
28a92 2f 0a 20 20 20 20 69 6e 74 20 62 65 73 74 46 6c  /.    int bestFl
28a93 61 67 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ags = 0;        
28a94 20 20 2f 2a 20 46 6c 61 67 73 20 61 73 73 6f 63    /* Flags assoc
28a95 69 61 74 65 64 20 77 69 74 68 20 70 42 65 73 74  iated with pBest
28a96 20 2a 2f 0a 20 20 20 20 69 6e 74 20 62 65 73 74   */.    int best
28a97 4e 45 71 20 3d 20 30 3b 20 20 20 20 20 20 20 20  NEq = 0;        
28a98 20 20 20 20 2f 2a 20 6e 45 71 20 61 73 73 6f 63      /* nEq assoc
28a99 69 61 74 65 64 20 77 69 74 68 20 70 42 65 73 74  iated with pBest
28a9a 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 6c   */.    double l
28a9b 6f 77 65 73 74 43 6f 73 74 3b 20 20 20 20 20 20  owestCost;      
28a9c 20 20 20 20 2f 2a 20 43 6f 73 74 20 6f 66 20 74      /* Cost of t
28a9d 68 65 20 70 42 65 73 74 20 2a 2f 0a 20 20 20 20  he pBest */.    
28a9e 69 6e 74 20 62 65 73 74 4a 20 3d 20 30 3b 20 20  int bestJ = 0;  
28a9f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
28aa0 68 65 20 76 61 6c 75 65 20 6f 66 20 6a 20 2a 2f  he value of j */
28aa1 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 6d 3b 20  .    Bitmask m; 
28aa2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28aa3 20 2f 2a 20 42 69 74 6d 61 73 6b 20 76 61 6c 75   /* Bitmask valu
28aa4 65 20 66 6f 72 20 6a 20 6f 72 20 62 65 73 74 4a  e for j or bestJ
28aa5 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6f 6e 63 65   */.    int once
28aa6 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
28aa7 20 20 20 20 2f 2a 20 54 72 75 65 20 77 68 65 6e      /* True when
28aa8 20 66 69 72 73 74 20 74 61 62 6c 65 20 69 73 20   first table is 
28aa9 73 65 65 6e 20 2a 2f 0a 20 20 20 20 73 71 6c 69  seen */.    sqli
28aaa 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
28aab 70 49 6e 64 65 78 3b 20 2f 2a 20 43 75 72 72 65  pIndex; /* Curre
28aac 6e 74 20 76 69 72 74 75 61 6c 20 69 6e 64 65 78  nt virtual index
28aad 20 2a 2f 0a 0a 20 20 20 20 6c 6f 77 65 73 74 43   */..    lowestC
28aae 6f 73 74 20 3d 20 53 51 4c 49 54 45 5f 42 49 47  ost = SQLITE_BIG
28aaf 5f 44 42 4c 3b 0a 20 20 20 20 66 6f 72 28 6a 3d  _DBL;.    for(j=
28ab0 69 46 72 6f 6d 2c 20 70 54 61 62 49 74 65 6d 3d  iFrom, pTabItem=
28ab1 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d 3b  &pTabList->a[j];
28ab2 20 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72   j<pTabList->nSr
28ab3 63 3b 20 6a 2b 2b 2c 20 70 54 61 62 49 74 65 6d  c; j++, pTabItem
28ab4 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 64  ++){.      int d
28ab5 6f 4e 6f 74 52 65 6f 72 64 65 72 3b 20 20 2f 2a  oNotReorder;  /*
28ab6 20 54 72 75 65 20 69 66 20 74 68 69 73 20 74 61   True if this ta
28ab7 62 6c 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  ble should not b
28ab8 65 20 72 65 6f 72 64 65 72 65 64 20 2a 2f 0a 0a  e reordered */..
28ab9 20 20 20 20 20 20 64 6f 4e 6f 74 52 65 6f 72 64        doNotReord
28aba 65 72 20 3d 20 20 28 70 54 61 62 49 74 65 6d 2d  er =  (pTabItem-
28abb 3e 6a 6f 69 6e 74 79 70 65 20 26 20 28 4a 54 5f  >jointype & (JT_
28abc 4c 45 46 54 7c 4a 54 5f 43 52 4f 53 53 29 29 21  LEFT|JT_CROSS))!
28abd 3d 30 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 6e  =0;.      if( on
28abe 63 65 20 26 26 20 64 6f 4e 6f 74 52 65 6f 72 64  ce && doNotReord
28abf 65 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  er ) break;.    
28ac0 20 20 6d 20 3d 20 67 65 74 4d 61 73 6b 28 26 6d    m = getMask(&m
28ac1 61 73 6b 53 65 74 2c 20 70 54 61 62 49 74 65 6d  askSet, pTabItem
28ac2 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  ->iCursor);.    
28ac3 20 20 69 66 28 20 28 6d 20 26 20 6e 6f 74 52 65    if( (m & notRe
28ac4 61 64 79 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ady)==0 ){.     
28ac5 20 20 20 69 66 28 20 6a 3d 3d 69 46 72 6f 6d 20     if( j==iFrom 
28ac6 29 20 69 46 72 6f 6d 2b 2b 3b 0a 20 20 20 20 20  ) iFrom++;.     
28ac7 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
28ac8 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72     }.      asser
28ac9 74 28 20 70 54 61 62 49 74 65 6d 2d 3e 70 54 61  t( pTabItem->pTa
28aca 62 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  b );.#ifndef SQL
28acb 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
28acc 54 41 42 4c 45 0a 20 20 20 20 20 20 69 66 28 20  TABLE.      if( 
28acd 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 49 74  IsVirtual(pTabIt
28ace 65 6d 2d 3e 70 54 61 62 29 20 29 7b 0a 20 20 20  em->pTab) ){.   
28acf 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64       sqlite3_ind
28ad0 65 78 5f 69 6e 66 6f 20 2a 2a 70 70 49 64 78 49  ex_info **ppIdxI
28ad1 6e 66 6f 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 61  nfo = &pWInfo->a
28ad2 5b 6a 5d 2e 70 49 64 78 49 6e 66 6f 3b 0a 20 20  [j].pIdxInfo;.  
28ad3 20 20 20 20 20 20 63 6f 73 74 20 3d 20 62 65 73        cost = bes
28ad4 74 56 69 72 74 75 61 6c 49 6e 64 65 78 28 70 50  tVirtualIndex(pP
28ad5 61 72 73 65 2c 20 26 77 63 2c 20 70 54 61 62 49  arse, &wc, pTabI
28ad6 74 65 6d 2c 20 6e 6f 74 52 65 61 64 79 2c 0a 20  tem, notReady,. 
28ad7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28ad8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
28ad9 70 4f 72 64 65 72 42 79 20 3f 20 2a 70 70 4f 72  pOrderBy ? *ppOr
28ada 64 65 72 42 79 20 3a 20 30 2c 20 69 3d 3d 30 2c  derBy : 0, i==0,
28adb 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
28adc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28add 20 70 70 49 64 78 49 6e 66 6f 29 3b 0a 20 20 20   ppIdxInfo);.   
28ade 20 20 20 20 20 66 6c 61 67 73 20 3d 20 57 48 45       flags = WHE
28adf 52 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 3b  RE_VIRTUALTABLE;
28ae0 0a 20 20 20 20 20 20 20 20 70 49 6e 64 65 78 20  .        pIndex 
28ae1 3d 20 2a 70 70 49 64 78 49 6e 66 6f 3b 0a 20 20  = *ppIdxInfo;.  
28ae2 20 20 20 20 20 20 69 66 28 20 70 49 6e 64 65 78        if( pIndex
28ae3 20 26 26 20 70 49 6e 64 65 78 2d 3e 6f 72 64 65   && pIndex->orde
28ae4 72 42 79 43 6f 6e 73 75 6d 65 64 20 29 7b 0a 20  rByConsumed ){. 
28ae5 20 20 20 20 20 20 20 20 20 66 6c 61 67 73 20 3d           flags =
28ae6 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54 41   WHERE_VIRTUALTA
28ae7 42 4c 45 20 7c 20 57 48 45 52 45 5f 4f 52 44 45  BLE | WHERE_ORDE
28ae8 52 42 59 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  RBY;.        }. 
28ae9 20 20 20 20 20 20 20 70 49 64 78 20 3d 20 30 3b         pIdx = 0;
28aea 0a 20 20 20 20 20 20 20 20 6e 45 71 20 3d 20 30  .        nEq = 0
28aeb 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 53  ;.        if( (S
28aec 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 2f 32 2e  QLITE_BIG_DBL/2.
28aed 30 29 3c 63 6f 73 74 20 29 7b 0a 20 20 20 20 20  0)<cost ){.     
28aee 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 73 74       /* The cost
28aef 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20   is not allowed 
28af0 74 6f 20 62 65 20 6c 61 72 67 65 72 20 74 68 61  to be larger tha
28af1 6e 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c  n SQLITE_BIG_DBL
28af2 20 28 74 68 65 0a 20 20 20 20 20 20 20 20 20 20   (the.          
28af3 2a 2a 20 69 6e 69 74 61 6c 20 76 61 6c 75 65 20  ** inital value 
28af4 6f 66 20 6c 6f 77 65 73 74 43 6f 73 74 20 69 6e  of lowestCost in
28af5 20 74 68 69 73 20 6c 6f 6f 70 2e 20 49 66 20 69   this loop. If i
28af6 74 20 69 73 2c 20 74 68 65 6e 0a 20 20 20 20 20  t is, then.     
28af7 20 20 20 20 20 2a 2a 20 74 68 65 20 28 63 6f 73       ** the (cos
28af8 74 3c 6c 6f 77 65 73 74 43 6f 73 74 29 20 74 65  t<lowestCost) te
28af9 73 74 20 62 65 6c 6f 77 20 77 69 6c 6c 20 6e 65  st below will ne
28afa 76 65 72 20 62 65 20 74 72 75 65 20 61 6e 64 0a  ver be true and.
28afb 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 4c 65            ** pLe
28afc 76 65 6c 2d 3e 70 42 65 73 74 49 64 78 20 6e 65  vel->pBestIdx ne
28afd 76 65 72 20 73 65 74 2e 0a 20 20 20 20 20 20 20  ver set..       
28afe 20 20 20 2a 2f 20 0a 20 20 20 20 20 20 20 20 20     */ .         
28aff 20 63 6f 73 74 20 3d 20 28 53 51 4c 49 54 45 5f   cost = (SQLITE_
28b00 42 49 47 5f 44 42 4c 2f 32 2e 30 29 3b 0a 20 20  BIG_DBL/2.0);.  
28b01 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
28b02 6c 73 65 20 0a 23 65 6e 64 69 66 0a 20 20 20 20  lse .#endif.    
28b03 20 20 7b 0a 20 20 20 20 20 20 20 20 63 6f 73 74    {.        cost
28b04 20 3d 20 62 65 73 74 49 6e 64 65 78 28 70 50 61   = bestIndex(pPa
28b05 72 73 65 2c 20 26 77 63 2c 20 70 54 61 62 49 74  rse, &wc, pTabIt
28b06 65 6d 2c 20 6e 6f 74 52 65 61 64 79 2c 0a 20 20  em, notReady,.  
28b07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28b08 20 20 20 20 20 20 20 28 69 3d 3d 30 20 26 26 20         (i==0 && 
28b09 70 70 4f 72 64 65 72 42 79 29 20 3f 20 2a 70 70  ppOrderBy) ? *pp
28b0a 4f 72 64 65 72 42 79 20 3a 20 30 2c 0a 20 20 20  OrderBy : 0,.   
28b0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28b0c 20 20 20 20 20 20 26 70 49 64 78 2c 20 26 66 6c        &pIdx, &fl
28b0d 61 67 73 2c 20 26 6e 45 71 29 3b 0a 20 20 20 20  ags, &nEq);.    
28b0e 20 20 20 20 70 49 6e 64 65 78 20 3d 20 30 3b 0a      pIndex = 0;.
28b0f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
28b10 28 20 63 6f 73 74 3c 6c 6f 77 65 73 74 43 6f 73  ( cost<lowestCos
28b11 74 20 29 7b 0a 20 20 20 20 20 20 20 20 6f 6e 63  t ){.        onc
28b12 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 6c  e = 1;.        l
28b13 6f 77 65 73 74 43 6f 73 74 20 3d 20 63 6f 73 74  owestCost = cost
28b14 3b 0a 20 20 20 20 20 20 20 20 70 42 65 73 74 20  ;.        pBest 
28b15 3d 20 70 49 64 78 3b 0a 20 20 20 20 20 20 20 20  = pIdx;.        
28b16 62 65 73 74 46 6c 61 67 73 20 3d 20 66 6c 61 67  bestFlags = flag
28b17 73 3b 0a 20 20 20 20 20 20 20 20 62 65 73 74 4e  s;.        bestN
28b18 45 71 20 3d 20 6e 45 71 3b 0a 20 20 20 20 20 20  Eq = nEq;.      
28b19 20 20 62 65 73 74 4a 20 3d 20 6a 3b 0a 20 20 20    bestJ = j;.   
28b1a 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 42 65       pLevel->pBe
28b1b 73 74 49 64 78 20 3d 20 70 49 6e 64 65 78 3b 0a  stIdx = pIndex;.
28b1c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
28b1d 28 20 64 6f 4e 6f 74 52 65 6f 72 64 65 72 20 29  ( doNotReorder )
28b1e 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
28b1f 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22 2a    WHERETRACE(("*
28b20 2a 2a 20 4f 70 74 69 6d 69 7a 65 72 20 73 65 6c  ** Optimizer sel
28b21 65 63 74 73 20 74 61 62 6c 65 20 25 64 20 66 6f  ects table %d fo
28b22 72 20 6c 6f 6f 70 20 25 64 5c 6e 22 2c 20 62 65  r loop %d\n", be
28b23 73 74 4a 2c 0a 20 20 20 20 20 20 20 20 20 20 20  stJ,.           
28b24 70 4c 65 76 65 6c 2d 70 57 49 6e 66 6f 2d 3e 61  pLevel-pWInfo->a
28b25 29 29 3b 0a 20 20 20 20 69 66 28 20 28 62 65 73  ));.    if( (bes
28b26 74 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 4f  tFlags & WHERE_O
28b27 52 44 45 52 42 59 29 21 3d 30 20 29 7b 0a 20 20  RDERBY)!=0 ){.  
28b28 20 20 20 20 2a 70 70 4f 72 64 65 72 42 79 20 3d      *ppOrderBy =
28b29 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 6e   0;.    }.    an
28b2a 64 46 6c 61 67 73 20 26 3d 20 62 65 73 74 46 6c  dFlags &= bestFl
28b2b 61 67 73 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  ags;.    pLevel-
28b2c 3e 66 6c 61 67 73 20 3d 20 62 65 73 74 46 6c 61  >flags = bestFla
28b2d 67 73 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e  gs;.    pLevel->
28b2e 70 49 64 78 20 3d 20 70 42 65 73 74 3b 0a 20 20  pIdx = pBest;.  
28b2f 20 20 70 4c 65 76 65 6c 2d 3e 6e 45 71 20 3d 20    pLevel->nEq = 
28b30 62 65 73 74 4e 45 71 3b 0a 20 20 20 20 70 4c 65  bestNEq;.    pLe
28b31 76 65 6c 2d 3e 61 49 6e 4c 6f 6f 70 20 3d 20 30  vel->aInLoop = 0
28b32 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6e 49  ;.    pLevel->nI
28b33 6e 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70  n = 0;.    if( p
28b34 42 65 73 74 20 29 7b 0a 20 20 20 20 20 20 70 4c  Best ){.      pL
28b35 65 76 65 6c 2d 3e 69 49 64 78 43 75 72 20 3d 20  evel->iIdxCur = 
28b36 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
28b37 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
28b38 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72   pLevel->iIdxCur
28b39 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20   = -1;.    }.   
28b3a 20 6e 6f 74 52 65 61 64 79 20 26 3d 20 7e 67 65   notReady &= ~ge
28b3b 74 4d 61 73 6b 28 26 6d 61 73 6b 53 65 74 2c 20  tMask(&maskSet, 
28b3c 70 54 61 62 4c 69 73 74 2d 3e 61 5b 62 65 73 74  pTabList->a[best
28b3d 4a 5d 2e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20  J].iCursor);.   
28b3e 20 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 20 3d   pLevel->iFrom =
28b3f 20 62 65 73 74 4a 3b 0a 0a 20 20 20 20 2f 2a 20   bestJ;..    /* 
28b40 43 68 65 63 6b 20 74 68 61 74 20 69 66 20 74 68  Check that if th
28b41 65 20 74 61 62 6c 65 20 73 63 61 6e 6e 65 64 20  e table scanned 
28b42 62 79 20 74 68 69 73 20 6c 6f 6f 70 20 69 74 65  by this loop ite
28b43 72 61 74 69 6f 6e 20 68 61 64 20 61 6e 0a 20 20  ration had an.  
28b44 20 20 2a 2a 20 49 4e 44 45 58 45 44 20 42 59 20    ** INDEXED BY 
28b45 63 6c 61 75 73 65 20 61 74 74 61 63 68 65 64 20  clause attached 
28b46 74 6f 20 69 74 2c 20 74 68 61 74 20 74 68 65 20  to it, that the 
28b47 6e 61 6d 65 64 20 69 6e 64 65 78 20 69 73 20 62  named index is b
28b48 65 69 6e 67 0a 20 20 20 20 2a 2a 20 75 73 65 64  eing.    ** used
28b49 20 66 6f 72 20 74 68 65 20 73 63 61 6e 2e 20 49   for the scan. I
28b4a 66 20 6e 6f 74 2c 20 74 68 65 6e 20 71 75 65 72  f not, then quer
28b4b 79 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 68 61  y compilation ha
28b4c 73 20 66 61 69 6c 65 64 2e 0a 20 20 20 20 2a 2a  s failed..    **
28b4d 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   Return an error
28b4e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 49 64  ..    */.    pId
28b4f 78 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b  x = pTabList->a[
28b50 62 65 73 74 4a 5d 2e 70 49 6e 64 65 78 3b 0a 20  bestJ].pIndex;. 
28b51 20 20 20 61 73 73 65 72 74 28 20 21 70 49 64 78     assert( !pIdx
28b52 20 7c 7c 20 21 70 42 65 73 74 20 7c 7c 20 70 49   || !pBest || pI
28b53 64 78 3d 3d 70 42 65 73 74 20 29 3b 0a 20 20 20  dx==pBest );.   
28b54 20 69 66 28 20 70 49 64 78 20 26 26 20 70 42 65   if( pIdx && pBe
28b55 73 74 21 3d 70 49 64 78 20 29 7b 0a 20 20 20 20  st!=pIdx ){.    
28b56 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
28b57 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f  g(pParse, "canno
28b58 74 20 75 73 65 20 69 6e 64 65 78 3a 20 25 73 22  t use index: %s"
28b59 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pIdx->zName);.
28b5a 20 20 20 20 20 20 67 6f 74 6f 20 77 68 65 72 65        goto where
28b5b 42 65 67 69 6e 45 72 72 6f 72 3b 0a 20 20 20 20  BeginError;.    
28b5c 7d 0a 20 20 7d 0a 20 20 57 48 45 52 45 54 52 41  }.  }.  WHERETRA
28b5d 43 45 28 28 22 2a 2a 2a 20 4f 70 74 69 6d 69 7a  CE(("*** Optimiz
28b5e 65 72 20 46 69 6e 69 73 68 65 64 20 2a 2a 2a 5c  er Finished ***\
28b5f 6e 22 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  n"));..  /* If t
28b60 68 65 20 74 6f 74 61 6c 20 71 75 65 72 79 20 6f  he total query o
28b61 6e 6c 79 20 73 65 6c 65 63 74 73 20 61 20 73 69  nly selects a si
28b62 6e 67 6c 65 20 72 6f 77 2c 20 74 68 65 6e 20 74  ngle row, then t
28b63 68 65 20 4f 52 44 45 52 20 42 59 0a 20 20 2a 2a  he ORDER BY.  **
28b64 20 63 6c 61 75 73 65 20 69 73 20 69 72 72 65 6c   clause is irrel
28b65 65 76 61 6e 74 2e 0a 20 20 2a 2f 0a 20 20 69 66  evant..  */.  if
28b66 28 20 28 61 6e 64 46 6c 61 67 73 20 26 20 57 48  ( (andFlags & WH
28b67 45 52 45 5f 55 4e 49 51 55 45 29 21 3d 30 20 26  ERE_UNIQUE)!=0 &
28b68 26 20 70 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  & ppOrderBy ){. 
28b69 20 20 20 2a 70 70 4f 72 64 65 72 42 79 20 3d 20     *ppOrderBy = 
28b6a 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  0;.  }..  /* If 
28b6b 74 68 65 20 63 61 6c 6c 65 72 20 69 73 20 61 6e  the caller is an
28b6c 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54   UPDATE or DELET
28b6d 45 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  E statement that
28b6e 20 69 73 20 72 65 71 75 65 73 74 69 6e 67 0a 20   is requesting. 
28b6f 20 2a 2a 20 74 6f 20 75 73 65 20 61 20 6f 6e 65   ** to use a one
28b70 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 2c  -pass algorithm,
28b71 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68   determine if th
28b72 69 73 20 69 73 20 61 70 70 72 6f 70 72 69 61 74  is is appropriat
28b73 65 2e 0a 20 20 2a 2a 20 54 68 65 20 6f 6e 65 2d  e..  ** The one-
28b74 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 20 6f  pass algorithm o
28b75 6e 6c 79 20 77 6f 72 6b 73 20 69 66 20 74 68 65  nly works if the
28b76 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f   WHERE clause co
28b77 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a 2a 20 74  nstraints.  ** t
28b78 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20  he statement to 
28b79 75 70 64 61 74 65 20 61 20 73 69 6e 67 6c 65 20  update a single 
28b7a 72 6f 77 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  row..  */.  asse
28b7b 72 74 28 20 28 77 66 6c 61 67 73 20 26 20 57 48  rt( (wflags & WH
28b7c 45 52 45 5f 4f 4e 45 50 41 53 53 5f 44 45 53 49  ERE_ONEPASS_DESI
28b7d 52 45 44 29 3d 3d 30 20 7c 7c 20 70 57 49 6e 66  RED)==0 || pWInf
28b7e 6f 2d 3e 6e 4c 65 76 65 6c 3d 3d 31 20 29 3b 0a  o->nLevel==1 );.
28b7f 20 20 69 66 28 20 28 77 66 6c 61 67 73 20 26 20    if( (wflags & 
28b80 57 48 45 52 45 5f 4f 4e 45 50 41 53 53 5f 44 45  WHERE_ONEPASS_DE
28b81 53 49 52 45 44 29 21 3d 30 20 26 26 20 28 61 6e  SIRED)!=0 && (an
28b82 64 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 55  dFlags & WHERE_U
28b83 4e 49 51 55 45 29 21 3d 30 20 29 7b 0a 20 20 20  NIQUE)!=0 ){.   
28b84 20 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61   pWInfo->okOnePa
28b85 73 73 20 3d 20 31 3b 0a 20 20 20 20 70 57 49 6e  ss = 1;.    pWIn
28b86 66 6f 2d 3e 61 5b 30 5d 2e 66 6c 61 67 73 20 26  fo->a[0].flags &
28b87 3d 20 7e 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c  = ~WHERE_IDX_ONL
28b88 59 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65  Y;.  }..  /* Ope
28b89 6e 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20  n all tables in 
28b8a 74 68 65 20 70 54 61 62 4c 69 73 74 20 61 6e 64  the pTabList and
28b8b 20 61 6e 79 20 69 6e 64 69 63 65 73 20 73 65 6c   any indices sel
28b8c 65 63 74 65 64 20 66 6f 72 0a 20 20 2a 2a 20 73  ected for.  ** s
28b8d 65 61 72 63 68 69 6e 67 20 74 68 6f 73 65 20 74  earching those t
28b8e 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 73 71  ables..  */.  sq
28b8f 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53  lite3CodeVerifyS
28b90 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 2d 31  chema(pParse, -1
28b91 29 3b 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 65  ); /* Insert the
28b92 20 63 6f 6f 6b 69 65 20 76 65 72 69 66 69 65 72   cookie verifier
28b93 20 47 6f 74 6f 20 2a 2f 0a 20 20 66 6f 72 28 69   Goto */.  for(i
28b94 3d 30 2c 20 70 4c 65 76 65 6c 3d 70 57 49 6e 66  =0, pLevel=pWInf
28b95 6f 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73 74  o->a; i<pTabList
28b96 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 4c 65  ->nSrc; i++, pLe
28b97 76 65 6c 2b 2b 29 7b 0a 20 20 20 20 54 61 62 6c  vel++){.    Tabl
28b98 65 20 2a 70 54 61 62 3b 20 20 20 20 20 2f 2a 20  e *pTab;     /* 
28b99 54 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f  Table to open */
28b9a 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 78 3b  .    Index *pIx;
28b9b 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 75        /* Index u
28b9c 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 70 54  sed to access pT
28b9d 61 62 20 28 69 66 20 61 6e 79 29 20 2a 2f 0a 20  ab (if any) */. 
28b9e 20 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20     int iDb;     
28b9f 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
28ba0 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
28ba1 69 6e 67 20 74 61 62 6c 65 2f 69 6e 64 65 78 20  ing table/index 
28ba2 2a 2f 0a 20 20 20 20 69 6e 74 20 69 49 64 78 43  */.    int iIdxC
28ba3 75 72 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 49 64  ur = pLevel->iId
28ba4 78 43 75 72 3b 0a 0a 23 69 66 6e 64 65 66 20 53  xCur;..#ifndef S
28ba5 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41  QLITE_OMIT_EXPLA
28ba6 49 4e 0a 20 20 20 20 69 66 28 20 70 50 61 72 73  IN.    if( pPars
28ba7 65 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 29 7b  e->explain==2 ){
28ba8 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4d 73  .      char *zMs
28ba9 67 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20  g;.      struct 
28baa 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  SrcList_item *pI
28bab 74 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d  tem = &pTabList-
28bac 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d  >a[pLevel->iFrom
28bad 5d 3b 0a 20 20 20 20 20 20 7a 4d 73 67 20 3d 20  ];.      zMsg = 
28bae 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
28baf 62 2c 20 22 54 41 42 4c 45 20 25 73 22 2c 20 70  b, "TABLE %s", p
28bb0 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  Item->zName);.  
28bb1 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a      if( pItem->z
28bb2 41 6c 69 61 73 20 29 7b 0a 20 20 20 20 20 20 20  Alias ){.       
28bb3 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d   zMsg = sqlite3M
28bb4 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67  Appendf(db, zMsg
28bb5 2c 20 22 25 73 20 41 53 20 25 73 22 2c 20 7a 4d  , "%s AS %s", zM
28bb6 73 67 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61  sg, pItem->zAlia
28bb7 73 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  s);.      }.    
28bb8 20 20 69 66 28 20 28 70 49 78 20 3d 20 70 4c 65    if( (pIx = pLe
28bb9 76 65 6c 2d 3e 70 49 64 78 29 21 3d 30 20 29 7b  vel->pIdx)!=0 ){
28bba 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20  .        zMsg = 
28bbb 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28  sqlite3MAppendf(
28bbc 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 57 49  db, zMsg, "%s WI
28bbd 54 48 20 49 4e 44 45 58 20 25 73 22 2c 20 7a 4d  TH INDEX %s", zM
28bbe 73 67 2c 20 70 49 78 2d 3e 7a 4e 61 6d 65 29 3b  sg, pIx->zName);
28bbf 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
28bc0 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26   pLevel->flags &
28bc1 20 28 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51   (WHERE_ROWID_EQ
28bc2 7c 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e  |WHERE_ROWID_RAN
28bc3 47 45 29 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  GE) ){.        z
28bc4 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 41 70  Msg = sqlite3MAp
28bc5 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20  pendf(db, zMsg, 
28bc6 22 25 73 20 55 53 49 4e 47 20 50 52 49 4d 41 52  "%s USING PRIMAR
28bc7 59 20 4b 45 59 22 2c 20 7a 4d 73 67 29 3b 0a 20  Y KEY", zMsg);. 
28bc8 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53       }.#ifndef S
28bc9 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
28bca 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 65 6c  ALTABLE.      el
28bcb 73 65 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70  se if( pLevel->p
28bcc 42 65 73 74 49 64 78 20 29 7b 0a 20 20 20 20 20  BestIdx ){.     
28bcd 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78     sqlite3_index
28bce 5f 69 6e 66 6f 20 2a 70 42 65 73 74 49 64 78 20  _info *pBestIdx 
28bcf 3d 20 70 4c 65 76 65 6c 2d 3e 70 42 65 73 74 49  = pLevel->pBestI
28bd0 64 78 3b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67  dx;.        zMsg
28bd1 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e   = sqlite3MAppen
28bd2 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73  df(db, zMsg, "%s
28bd3 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 49   VIRTUAL TABLE I
28bd4 4e 44 45 58 20 25 64 3a 25 73 22 2c 20 7a 4d 73  NDEX %d:%s", zMs
28bd5 67 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  g,.             
28bd6 20 20 20 20 20 20 20 70 42 65 73 74 49 64 78 2d         pBestIdx-
28bd7 3e 69 64 78 4e 75 6d 2c 20 70 42 65 73 74 49 64  >idxNum, pBestId
28bd8 78 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20 20 20  x->idxStr);.    
28bd9 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20    }.#endif.     
28bda 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 66 6c 61   if( pLevel->fla
28bdb 67 73 20 26 20 57 48 45 52 45 5f 4f 52 44 45 52  gs & WHERE_ORDER
28bdc 42 59 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 4d  BY ){.        zM
28bdd 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70  sg = sqlite3MApp
28bde 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22  endf(db, zMsg, "
28bdf 25 73 20 4f 52 44 45 52 20 42 59 22 2c 20 7a 4d  %s ORDER BY", zM
28be0 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  sg);.      }.   
28be1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
28be2 64 4f 70 34 28 76 2c 20 4f 50 5f 45 78 70 6c 61  dOp4(v, OP_Expla
28be3 69 6e 2c 20 69 2c 20 70 4c 65 76 65 6c 2d 3e 69  in, i, pLevel->i
28be4 46 72 6f 6d 2c 20 30 2c 20 7a 4d 73 67 2c 20 50  From, 0, zMsg, P
28be5 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20  4_DYNAMIC);.    
28be6 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
28be7 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 20  TE_OMIT_EXPLAIN 
28be8 2a 2f 0a 20 20 20 20 70 54 61 62 49 74 65 6d 20  */.    pTabItem 
28be9 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70  = &pTabList->a[p
28bea 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20  Level->iFrom];. 
28beb 20 20 20 70 54 61 62 20 3d 20 70 54 61 62 49 74     pTab = pTabIt
28bec 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69 44  em->pTab;.    iD
28bed 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
28bee 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d  aToIndex(pParse-
28bef 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  >db, pTab->pSche
28bf0 6d 61 29 3b 0a 20 20 20 20 69 66 28 20 28 70 54  ma);.    if( (pT
28bf1 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54  ab->tabFlags & T
28bf2 46 5f 45 70 68 65 6d 65 72 61 6c 29 21 3d 30 20  F_Ephemeral)!=0 
28bf3 7c 7c 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  || pTab->pSelect
28bf4 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 23 69 66   ) continue;.#if
28bf5 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
28bf6 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
28bf7 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 42    if( pLevel->pB
28bf8 65 73 74 49 64 78 20 29 7b 0a 20 20 20 20 20 20  estIdx ){.      
28bf9 69 6e 74 20 69 43 75 72 20 3d 20 70 54 61 62 49  int iCur = pTabI
28bfa 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20  tem->iCursor;.  
28bfb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
28bfc 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 4f 70 65  ddOp4(v, OP_VOpe
28bfd 6e 2c 20 69 43 75 72 2c 20 30 2c 20 30 2c 0a 20  n, iCur, 0, 0,. 
28bfe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28bff 20 20 20 20 20 20 20 28 63 6f 6e 73 74 20 63 68         (const ch
28c00 61 72 2a 29 70 54 61 62 2d 3e 70 56 74 61 62 2c  ar*)pTab->pVtab,
28c01 20 50 34 5f 56 54 41 42 29 3b 0a 20 20 20 20 7d   P4_VTAB);.    }
28c02 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20  else.#endif.    
28c03 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 66 6c 61  if( (pLevel->fla
28c04 67 73 20 26 20 57 48 45 52 45 5f 49 44 58 5f 4f  gs & WHERE_IDX_O
28c05 4e 4c 59 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  NLY)==0 ){.     
28c06 20 69 6e 74 20 6f 70 20 3d 20 70 57 49 6e 66 6f   int op = pWInfo
28c07 2d 3e 6f 6b 4f 6e 65 50 61 73 73 20 3f 20 4f 50  ->okOnePass ? OP
28c08 5f 4f 70 65 6e 57 72 69 74 65 20 3a 20 4f 50 5f  _OpenWrite : OP_
28c09 4f 70 65 6e 52 65 61 64 3b 0a 20 20 20 20 20 20  OpenRead;.      
28c0a 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65  sqlite3OpenTable
28c0b 28 70 50 61 72 73 65 2c 20 70 54 61 62 49 74 65  (pParse, pTabIte
28c0c 6d 2d 3e 69 43 75 72 73 6f 72 2c 20 69 44 62 2c  m->iCursor, iDb,
28c0d 20 70 54 61 62 2c 20 6f 70 29 3b 0a 20 20 20 20   pTab, op);.    
28c0e 20 20 69 66 28 20 21 70 57 49 6e 66 6f 2d 3e 6f    if( !pWInfo->o
28c0f 6b 4f 6e 65 50 61 73 73 20 26 26 20 70 54 61 62  kOnePass && pTab
28c10 2d 3e 6e 43 6f 6c 3c 28 73 69 7a 65 6f 66 28 42  ->nCol<(sizeof(B
28c11 69 74 6d 61 73 6b 29 2a 38 29 20 29 7b 0a 20 20  itmask)*8) ){.  
28c12 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20 62 20        Bitmask b 
28c13 3d 20 70 54 61 62 49 74 65 6d 2d 3e 63 6f 6c 55  = pTabItem->colU
28c14 73 65 64 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  sed;.        int
28c15 20 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20   n = 0;.        
28c16 66 6f 72 28 3b 20 62 3b 20 62 3d 62 3e 3e 31 2c  for(; b; b=b>>1,
28c17 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 20 20   n++){}.        
28c18 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
28c19 65 50 32 28 76 2c 20 73 71 6c 69 74 65 33 56 64  eP2(v, sqlite3Vd
28c1a 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
28c1b 2d 32 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20 20  -2, n);.        
28c1c 61 73 73 65 72 74 28 20 6e 3c 3d 70 54 61 62 2d  assert( n<=pTab-
28c1d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 20 20 7d  >nCol );.      }
28c1e 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
28c1f 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f    sqlite3TableLo
28c20 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20  ck(pParse, iDb, 
28c21 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70  pTab->tnum, 0, p
28c22 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Tab->zName);.   
28c23 20 7d 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69   }.    pLevel->i
28c24 54 61 62 43 75 72 20 3d 20 70 54 61 62 49 74 65  TabCur = pTabIte
28c25 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20  m->iCursor;.    
28c26 69 66 28 20 28 70 49 78 20 3d 20 70 4c 65 76 65  if( (pIx = pLeve
28c27 6c 2d 3e 70 49 64 78 29 21 3d 30 20 29 7b 0a 20  l->pIdx)!=0 ){. 
28c28 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b       KeyInfo *pK
28c29 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65  ey = sqlite3Inde
28c2a 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c  xKeyinfo(pParse,
28c2b 20 70 49 78 29 3b 0a 20 20 20 20 20 20 61 73 73   pIx);.      ass
28c2c 65 72 74 28 20 70 49 78 2d 3e 70 53 63 68 65 6d  ert( pIx->pSchem
28c2d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  a==pTab->pSchema
28c2e 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
28c2f 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
28c30 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c  P_SetNumColumns,
28c31 20 30 2c 20 70 49 78 2d 3e 6e 43 6f 6c 75 6d 6e   0, pIx->nColumn
28c32 2b 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  +1);.      sqlit
28c33 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
28c34 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 49 64  OP_OpenRead, iId
28c35 78 43 75 72 2c 20 70 49 78 2d 3e 74 6e 75 6d 2c  xCur, pIx->tnum,
28c36 20 69 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20   iDb,.          
28c37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63                (c
28c38 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45  har*)pKey, P4_KE
28c39 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a  YINFO_HANDOFF);.
28c3a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
28c3b 74 28 28 76 2c 20 22 25 73 22 2c 20 70 49 78 2d  t((v, "%s", pIx-
28c3c 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 7d 0a  >zName));.    }.
28c3d 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56      sqlite3CodeV
28c3e 65 72 69 66 79 53 63 68 65 6d 61 28 70 50 61 72  erifySchema(pPar
28c3f 73 65 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20  se, iDb);.  }.  
28c40 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 20 3d 20 73  pWInfo->iTop = s
28c41 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
28c42 74 41 64 64 72 28 76 29 3b 0a 0a 20 20 2f 2a 20  tAddr(v);..  /* 
28c43 47 65 6e 65 72 61 74 65 20 74 68 65 20 63 6f 64  Generate the cod
28c44 65 20 74 6f 20 64 6f 20 74 68 65 20 73 65 61 72  e to do the sear
28c45 63 68 2e 20 20 45 61 63 68 20 69 74 65 72 61 74  ch.  Each iterat
28c46 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 0a 20  ion of the for. 
28c47 20 2a 2a 20 6c 6f 6f 70 20 62 65 6c 6f 77 20 67   ** loop below g
28c48 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 66 6f  enerates code fo
28c49 72 20 61 20 73 69 6e 67 6c 65 20 6e 65 73 74 65  r a single neste
28c4a 64 20 6c 6f 6f 70 20 6f 66 20 74 68 65 20 56 4d  d loop of the VM
28c4b 0a 20 20 2a 2a 20 70 72 6f 67 72 61 6d 2e 0a 20  .  ** program.. 
28c4c 20 2a 2f 0a 20 20 6e 6f 74 52 65 61 64 79 20 3d   */.  notReady =
28c4d 20 7e 28 42 69 74 6d 61 73 6b 29 30 3b 0a 20 20   ~(Bitmask)0;.  
28c4e 66 6f 72 28 69 3d 30 2c 20 70 4c 65 76 65 6c 3d  for(i=0, pLevel=
28c4f 70 57 49 6e 66 6f 2d 3e 61 3b 20 69 3c 70 54 61  pWInfo->a; i<pTa
28c50 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  bList->nSrc; i++
28c51 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20 20 20  , pLevel++){.   
28c52 20 69 6e 74 20 6a 2c 20 6b 3b 0a 20 20 20 20 69   int j, k;.    i
28c53 6e 74 20 69 43 75 72 20 3d 20 70 54 61 62 49 74  nt iCur = pTabIt
28c54 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 20 20 2f 2a  em->iCursor;  /*
28c55 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72   The VDBE cursor
28c56 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 2a   for the table *
28c57 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64  /.    Index *pId
28c58 78 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  x;       /* The 
28c59 69 6e 64 65 78 20 77 65 20 77 69 6c 6c 20 62 65  index we will be
28c5a 20 75 73 69 6e 67 20 2a 2f 0a 20 20 20 20 69 6e   using */.    in
28c5b 74 20 6e 78 74 3b 20 20 20 20 20 20 20 20 20 20  t nxt;          
28c5c 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 6a 75 6d   /* Where to jum
28c5d 70 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69  p to continue wi
28c5e 74 68 20 74 68 65 20 6e 65 78 74 20 49 4e 20 63  th the next IN c
28c5f 61 73 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ase */.    int i
28c60 49 64 78 43 75 72 3b 20 20 20 20 20 20 20 2f 2a  IdxCur;       /*
28c61 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72   The VDBE cursor
28c62 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 20 2a   for the index *
28c63 2f 0a 20 20 20 20 69 6e 74 20 6f 6d 69 74 54 61  /.    int omitTa
28c64 62 6c 65 3b 20 20 20 20 20 2f 2a 20 54 72 75 65  ble;     /* True
28c65 20 69 66 20 77 65 20 75 73 65 20 74 68 65 20 69   if we use the i
28c66 6e 64 65 78 20 6f 6e 6c 79 20 2a 2f 0a 20 20 20  ndex only */.   
28c67 20 69 6e 74 20 62 52 65 76 3b 20 20 20 20 20 20   int bRev;      
28c68 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77      /* True if w
28c69 65 20 6e 65 65 64 20 74 6f 20 73 63 61 6e 20 69  e need to scan i
28c6a 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 20  n reverse order 
28c6b 2a 2f 0a 0a 20 20 20 20 70 54 61 62 49 74 65 6d  */..    pTabItem
28c6c 20 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b   = &pTabList->a[
28c6d 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a  pLevel->iFrom];.
28c6e 20 20 20 20 69 43 75 72 20 3d 20 70 54 61 62 49      iCur = pTabI
28c6f 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20  tem->iCursor;.  
28c70 20 20 70 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d    pIdx = pLevel-
28c71 3e 70 49 64 78 3b 0a 20 20 20 20 69 49 64 78 43  >pIdx;.    iIdxC
28c72 75 72 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 49 64  ur = pLevel->iId
28c73 78 43 75 72 3b 0a 20 20 20 20 62 52 65 76 20 3d  xCur;.    bRev =
28c74 20 28 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20   (pLevel->flags 
28c75 26 20 57 48 45 52 45 5f 52 45 56 45 52 53 45 29  & WHERE_REVERSE)
28c76 21 3d 30 3b 0a 20 20 20 20 6f 6d 69 74 54 61 62  !=0;.    omitTab
28c77 6c 65 20 3d 20 28 70 4c 65 76 65 6c 2d 3e 66 6c  le = (pLevel->fl
28c78 61 67 73 20 26 20 57 48 45 52 45 5f 49 44 58 5f  ags & WHERE_IDX_
28c79 4f 4e 4c 59 29 21 3d 30 3b 0a 0a 20 20 20 20 2f  ONLY)!=0;..    /
28c7a 2a 20 43 72 65 61 74 65 20 6c 61 62 65 6c 73 20  * Create labels 
28c7b 66 6f 72 20 74 68 65 20 22 62 72 65 61 6b 22 20  for the "break" 
28c7c 61 6e 64 20 22 63 6f 6e 74 69 6e 75 65 22 20 69  and "continue" i
28c7d 6e 73 74 72 75 63 74 69 6f 6e 73 0a 20 20 20 20  nstructions.    
28c7e 2a 2a 20 66 6f 72 20 74 68 65 20 63 75 72 72 65  ** for the curre
28c7f 6e 74 20 6c 6f 6f 70 2e 20 20 4a 75 6d 70 20 74  nt loop.  Jump t
28c80 6f 20 62 72 6b 20 74 6f 20 62 72 65 61 6b 20 6f  o brk to break o
28c81 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2e 0a 20 20  ut of a loop..  
28c82 20 20 2a 2a 20 4a 75 6d 70 20 74 6f 20 63 6f 6e    ** Jump to con
28c83 74 20 74 6f 20 67 6f 20 69 6d 6d 65 64 69 61 74  t to go immediat
28c84 65 6c 79 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ely to the next 
28c85 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  iteration of the
28c86 0a 20 20 20 20 2a 2a 20 6c 6f 6f 70 2e 0a 20 20  .    ** loop..  
28c87 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 68 65 6e    **.    ** When
28c88 20 74 68 65 72 65 20 69 73 20 61 6e 20 49 4e 20   there is an IN 
28c89 6f 70 65 72 61 74 6f 72 2c 20 77 65 20 61 6c 73  operator, we als
28c8a 6f 20 68 61 76 65 20 61 20 22 6e 78 74 22 20 6c  o have a "nxt" l
28c8b 61 62 65 6c 20 74 68 61 74 0a 20 20 20 20 2a 2a  abel that.    **
28c8c 20 6d 65 61 6e 73 20 74 6f 20 63 6f 6e 74 69 6e   means to contin
28c8d 75 65 20 77 69 74 68 20 74 68 65 20 6e 65 78 74  ue with the next
28c8e 20 49 4e 20 76 61 6c 75 65 20 63 6f 6d 62 69 6e   IN value combin
28c8f 61 74 69 6f 6e 2e 20 20 57 68 65 6e 0a 20 20 20  ation.  When.   
28c90 20 2a 2a 20 74 68 65 72 65 20 61 72 65 20 6e 6f   ** there are no
28c91 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 20 69 6e   IN operators in
28c92 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 73   the constraints
28c93 2c 20 74 68 65 20 22 6e 78 74 22 20 6c 61 62 65  , the "nxt" labe
28c94 6c 0a 20 20 20 20 2a 2a 20 69 73 20 74 68 65 20  l.    ** is the 
28c95 73 61 6d 65 20 61 73 20 22 62 72 6b 22 2e 0a 20  same as "brk".. 
28c96 20 20 20 2a 2f 0a 20 20 20 20 62 72 6b 20 3d 20     */.    brk = 
28c97 70 4c 65 76 65 6c 2d 3e 62 72 6b 20 3d 20 70 4c  pLevel->brk = pL
28c98 65 76 65 6c 2d 3e 6e 78 74 20 3d 20 73 71 6c 69  evel->nxt = sqli
28c99 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
28c9a 28 76 29 3b 0a 20 20 20 20 63 6f 6e 74 20 3d 20  (v);.    cont = 
28c9b 70 4c 65 76 65 6c 2d 3e 63 6f 6e 74 20 3d 20 73  pLevel->cont = s
28c9c 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
28c9d 62 65 6c 28 76 29 3b 0a 0a 20 20 20 20 2f 2a 20  bel(v);..    /* 
28c9e 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 72  If this is the r
28c9f 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20 61 20  ight table of a 
28ca0 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 2c  LEFT OUTER JOIN,
28ca1 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 0a 20 20   allocate and.  
28ca2 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20    ** initialize 
28ca3 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68  a memory cell th
28ca4 61 74 20 72 65 63 6f 72 64 73 20 69 66 20 74 68  at records if th
28ca5 69 73 20 74 61 62 6c 65 20 6d 61 74 63 68 65 73  is table matches
28ca6 20 61 6e 79 0a 20 20 20 20 2a 2a 20 72 6f 77 20   any.    ** row 
28ca7 6f 66 20 74 68 65 20 6c 65 66 74 20 74 61 62 6c  of the left tabl
28ca8 65 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 0a 20  e of the join.. 
28ca9 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4c     */.    if( pL
28caa 65 76 65 6c 2d 3e 69 46 72 6f 6d 3e 30 20 26 26  evel->iFrom>0 &&
28cab 20 28 70 54 61 62 49 74 65 6d 5b 30 5d 2e 6a 6f   (pTabItem[0].jo
28cac 69 6e 74 79 70 65 20 26 20 4a 54 5f 4c 45 46 54  intype & JT_LEFT
28cad 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4c  )!=0 ){.      pL
28cae 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20  evel->iLeftJoin 
28caf 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
28cb0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
28cb1 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
28cb2 49 6e 74 65 67 65 72 2c 20 30 2c 20 70 4c 65 76  Integer, 0, pLev
28cb3 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 29 3b 0a  el->iLeftJoin);.
28cb4 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
28cb5 74 28 28 76 2c 20 22 69 6e 69 74 20 4c 45 46 54  t((v, "init LEFT
28cb6 20 4a 4f 49 4e 20 6e 6f 2d 6d 61 74 63 68 20 66   JOIN no-match f
28cb7 6c 61 67 22 29 29 3b 0a 20 20 20 20 7d 0a 0a 23  lag"));.    }..#
28cb8 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
28cb9 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
28cba 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e      if( pLevel->
28cbb 70 42 65 73 74 49 64 78 20 29 7b 0a 20 20 20 20  pBestIdx ){.    
28cbc 20 20 2f 2a 20 43 61 73 65 20 30 3a 20 20 54 68    /* Case 0:  Th
28cbd 65 20 74 61 62 6c 65 20 69 73 20 61 20 76 69 72  e table is a vir
28cbe 74 75 61 6c 2d 74 61 62 6c 65 2e 20 20 55 73 65  tual-table.  Use
28cbf 20 74 68 65 20 56 46 69 6c 74 65 72 20 61 6e 64   the VFilter and
28cc0 20 56 4e 65 78 74 0a 20 20 20 20 20 20 2a 2a 20   VNext.      ** 
28cc1 20 20 20 20 20 20 20 20 20 74 6f 20 61 63 63 65           to acce
28cc2 73 73 20 74 68 65 20 64 61 74 61 2e 0a 20 20 20  ss the data..   
28cc3 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20     */.      int 
28cc4 6a 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 52 65  j;.      int iRe
28cc5 67 3b 20 20 20 2f 2a 20 50 33 20 56 61 6c 75 65  g;   /* P3 Value
28cc6 20 66 6f 72 20 4f 50 5f 56 46 69 6c 74 65 72 20   for OP_VFilter 
28cc7 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
28cc8 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 42 65  _index_info *pBe
28cc9 73 74 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d 3e  stIdx = pLevel->
28cca 70 42 65 73 74 49 64 78 3b 0a 20 20 20 20 20 20  pBestIdx;.      
28ccb 69 6e 74 20 6e 43 6f 6e 73 74 72 61 69 6e 74 20  int nConstraint 
28ccc 3d 20 70 42 65 73 74 49 64 78 2d 3e 6e 43 6f 6e  = pBestIdx->nCon
28ccd 73 74 72 61 69 6e 74 3b 0a 20 20 20 20 20 20 73  straint;.      s
28cce 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
28ccf 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75  dex_constraint_u
28cd0 73 61 67 65 20 2a 61 55 73 61 67 65 20 3d 0a 20  sage *aUsage =. 
28cd1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28cd2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28cd3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28cd4 20 70 42 65 73 74 49 64 78 2d 3e 61 43 6f 6e 73   pBestIdx->aCons
28cd5 74 72 61 69 6e 74 55 73 61 67 65 3b 0a 20 20 20  traintUsage;.   
28cd6 20 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20     const struct 
28cd7 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f  sqlite3_index_co
28cd8 6e 73 74 72 61 69 6e 74 20 2a 61 43 6f 6e 73 74  nstraint *aConst
28cd9 72 61 69 6e 74 20 3d 0a 20 20 20 20 20 20 20 20  raint =.        
28cda 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28cdb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28cdc 20 20 20 20 20 20 20 20 20 20 70 42 65 73 74 49            pBestI
28cdd 64 78 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 3b  dx->aConstraint;
28cde 0a 0a 20 20 20 20 20 20 69 52 65 67 20 3d 20 73  ..      iReg = s
28cdf 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e  qlite3GetTempRan
28ce0 67 65 28 70 50 61 72 73 65 2c 20 6e 43 6f 6e 73  ge(pParse, nCons
28ce1 74 72 61 69 6e 74 2b 32 29 3b 0a 20 20 20 20 20  traint+2);.     
28ce2 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65   pParse->disable
28ce3 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 20 20 20  ColCache++;.    
28ce4 20 20 66 6f 72 28 6a 3d 31 3b 20 6a 3c 3d 6e 43    for(j=1; j<=nC
28ce5 6f 6e 73 74 72 61 69 6e 74 3b 20 6a 2b 2b 29 7b  onstraint; j++){
28ce6 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a  .        int k;.
28ce7 20 20 20 20 20 20 20 20 66 6f 72 28 6b 3d 30 3b          for(k=0;
28ce8 20 6b 3c 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20   k<nConstraint; 
28ce9 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  k++){.          
28cea 69 66 28 20 61 55 73 61 67 65 5b 6b 5d 2e 61 72  if( aUsage[k].ar
28ceb 67 76 49 6e 64 65 78 3d 3d 6a 20 29 7b 0a 20 20  gvIndex==j ){.  
28cec 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 54            int iT
28ced 65 72 6d 20 3d 20 61 43 6f 6e 73 74 72 61 69 6e  erm = aConstrain
28cee 74 5b 6b 5d 2e 69 54 65 72 6d 4f 66 66 73 65 74  t[k].iTermOffset
28cef 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73  ;.            as
28cf0 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 64 69  sert( pParse->di
28cf1 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 20 29 3b  sableColCache );
28cf2 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
28cf3 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
28cf4 72 73 65 2c 20 77 63 2e 61 5b 69 54 65 72 6d 5d  rse, wc.a[iTerm]
28cf5 2e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20  .pExpr->pRight, 
28cf6 69 52 65 67 2b 6a 2b 31 29 3b 0a 20 20 20 20 20  iReg+j+1);.     
28cf7 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
28cf8 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
28cf9 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
28cfa 6b 3d 3d 6e 43 6f 6e 73 74 72 61 69 6e 74 20 29  k==nConstraint )
28cfb 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
28cfc 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
28cfd 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c  arse->disableCol
28cfe 43 61 63 68 65 20 29 3b 0a 20 20 20 20 20 20 70  Cache );.      p
28cff 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f  Parse->disableCo
28d00 6c 43 61 63 68 65 2d 2d 3b 0a 20 20 20 20 20 20  lCache--;.      
28d01 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28d02 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
28d03 20 70 42 65 73 74 49 64 78 2d 3e 69 64 78 4e 75   pBestIdx->idxNu
28d04 6d 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 20 20  m, iReg);.      
28d05 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28d06 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
28d07 20 6a 2d 31 2c 20 69 52 65 67 2b 31 29 3b 0a 20   j-1, iReg+1);. 
28d08 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
28d09 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 46 69  AddOp4(v, OP_VFi
28d0a 6c 74 65 72 2c 20 69 43 75 72 2c 20 62 72 6b 2c  lter, iCur, brk,
28d0b 20 69 52 65 67 2c 20 70 42 65 73 74 49 64 78 2d   iReg, pBestIdx-
28d0c 3e 69 64 78 53 74 72 2c 0a 20 20 20 20 20 20 20  >idxStr,.       
28d0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28d0e 20 70 42 65 73 74 49 64 78 2d 3e 6e 65 65 64 54   pBestIdx->needT
28d0f 6f 46 72 65 65 49 64 78 53 74 72 20 3f 20 50 34  oFreeIdxStr ? P4
28d10 5f 4d 50 52 49 4e 54 46 20 3a 20 50 34 5f 53 54  _MPRINTF : P4_ST
28d11 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c  ATIC);.      sql
28d12 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
28d13 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69 52 65  ange(pParse, iRe
28d14 67 2c 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 32  g, nConstraint+2
28d15 29 3b 0a 20 20 20 20 20 20 70 42 65 73 74 49 64  );.      pBestId
28d16 78 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78  x->needToFreeIdx
28d17 53 74 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 66  Str = 0;.      f
28d18 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 43 6f 6e 73 74  or(j=0; j<nConst
28d19 72 61 69 6e 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20  raint; j++){.   
28d1a 20 20 20 20 20 69 66 28 20 61 55 73 61 67 65 5b       if( aUsage[
28d1b 6a 5d 2e 6f 6d 69 74 20 29 7b 0a 20 20 20 20 20  j].omit ){.     
28d1c 20 20 20 20 20 69 6e 74 20 69 54 65 72 6d 20 3d       int iTerm =
28d1d 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 6a 5d 2e   aConstraint[j].
28d1e 69 54 65 72 6d 4f 66 66 73 65 74 3b 0a 20 20 20  iTermOffset;.   
28d1f 20 20 20 20 20 20 20 64 69 73 61 62 6c 65 54 65         disableTe
28d20 72 6d 28 70 4c 65 76 65 6c 2c 20 26 77 63 2e 61  rm(pLevel, &wc.a
28d21 5b 69 54 65 72 6d 5d 29 3b 0a 20 20 20 20 20 20  [iTerm]);.      
28d22 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
28d23 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f    pLevel->op = O
28d24 50 5f 56 4e 65 78 74 3b 0a 20 20 20 20 20 20 70  P_VNext;.      p
28d25 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 69 43 75 72  Level->p1 = iCur
28d26 3b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e  ;.      pLevel->
28d27 70 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  p2 = sqlite3Vdbe
28d28 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
28d29 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66      }else.#endif
28d2a 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
28d2b 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a  VIRTUALTABLE */.
28d2c 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d  .    if( pLevel-
28d2d 3e 66 6c 61 67 73 20 26 20 57 48 45 52 45 5f 52  >flags & WHERE_R
28d2e 4f 57 49 44 5f 45 51 20 29 7b 0a 20 20 20 20 20  OWID_EQ ){.     
28d2f 20 2f 2a 20 43 61 73 65 20 31 3a 20 20 57 65 20   /* Case 1:  We 
28d30 63 61 6e 20 64 69 72 65 63 74 6c 79 20 72 65 66  can directly ref
28d31 65 72 65 6e 63 65 20 61 20 73 69 6e 67 6c 65 20  erence a single 
28d32 72 6f 77 20 75 73 69 6e 67 20 61 6e 0a 20 20 20  row using an.   
28d33 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 65     **          e
28d34 71 75 61 6c 69 74 79 20 63 6f 6d 70 61 72 69 73  quality comparis
28d35 6f 6e 20 61 67 61 69 6e 73 74 20 74 68 65 20 52  on against the R
28d36 4f 57 49 44 20 66 69 65 6c 64 2e 20 20 4f 72 0a  OWID field.  Or.
28d37 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20        **        
28d38 20 20 77 65 20 72 65 66 65 72 65 6e 63 65 20 6d    we reference m
28d39 75 6c 74 69 70 6c 65 20 72 6f 77 73 20 75 73 69  ultiple rows usi
28d3a 6e 67 20 61 20 22 72 6f 77 69 64 20 49 4e 20 28  ng a "rowid IN (
28d3b 2e 2e 2e 29 22 0a 20 20 20 20 20 20 2a 2a 20 20  ...)".      **  
28d3c 20 20 20 20 20 20 20 20 63 6f 6e 73 74 72 75 63          construc
28d3d 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  t..      */.    
28d3e 20 20 69 6e 74 20 72 31 3b 0a 20 20 20 20 20 20    int r1;.      
28d3f 69 6e 74 20 72 74 6d 70 20 3d 20 73 71 6c 69 74  int rtmp = sqlit
28d40 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
28d41 72 73 65 29 3b 0a 20 20 20 20 20 20 70 54 65 72  rse);.      pTer
28d42 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 26 77 63  m = findTerm(&wc
28d43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52  , iCur, -1, notR
28d44 65 61 64 79 2c 20 57 4f 5f 45 51 7c 57 4f 5f 49  eady, WO_EQ|WO_I
28d45 4e 2c 20 30 29 3b 0a 20 20 20 20 20 20 61 73 73  N, 0);.      ass
28d46 65 72 74 28 20 70 54 65 72 6d 21 3d 30 20 29 3b  ert( pTerm!=0 );
28d47 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
28d48 54 65 72 6d 2d 3e 70 45 78 70 72 21 3d 30 20 29  Term->pExpr!=0 )
28d49 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
28d4a 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  pTerm->leftCurso
28d4b 72 3d 3d 69 43 75 72 20 29 3b 0a 20 20 20 20 20  r==iCur );.     
28d4c 20 61 73 73 65 72 74 28 20 6f 6d 69 74 54 61 62   assert( omitTab
28d4d 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 72  le==0 );.      r
28d4e 31 20 3d 20 63 6f 64 65 45 71 75 61 6c 69 74 79  1 = codeEquality
28d4f 54 65 72 6d 28 70 50 61 72 73 65 2c 20 70 54 65  Term(pParse, pTe
28d50 72 6d 2c 20 70 4c 65 76 65 6c 2c 20 72 74 6d 70  rm, pLevel, rtmp
28d51 29 3b 0a 20 20 20 20 20 20 6e 78 74 20 3d 20 70  );.      nxt = p
28d52 4c 65 76 65 6c 2d 3e 6e 78 74 3b 0a 20 20 20 20  Level->nxt;.    
28d53 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
28d54 4f 70 32 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65  Op2(v, OP_MustBe
28d55 49 6e 74 2c 20 72 31 2c 20 6e 78 74 29 3b 0a 20  Int, r1, nxt);. 
28d56 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
28d57 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74  AddOp3(v, OP_Not
28d58 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20 6e 78  Exists, iCur, nx
28d59 74 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71  t, r1);.      sq
28d5a 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
28d5b 52 65 67 28 70 50 61 72 73 65 2c 20 72 74 6d 70  Reg(pParse, rtmp
28d5c 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
28d5d 6d 65 6e 74 28 28 76 2c 20 22 70 6b 22 29 29 3b  ment((v, "pk"));
28d5e 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f  .      pLevel->o
28d5f 70 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20  p = OP_Noop;.   
28d60 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 65 76 65   }else if( pLeve
28d61 6c 2d 3e 66 6c 61 67 73 20 26 20 57 48 45 52 45  l->flags & WHERE
28d62 5f 52 4f 57 49 44 5f 52 41 4e 47 45 20 29 7b 0a  _ROWID_RANGE ){.
28d63 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20 32 3a        /* Case 2:
28d64 20 20 57 65 20 68 61 76 65 20 61 6e 20 69 6e 65    We have an ine
28d65 71 75 61 6c 69 74 79 20 63 6f 6d 70 61 72 69 73  quality comparis
28d66 6f 6e 20 61 67 61 69 6e 73 74 20 74 68 65 20 52  on against the R
28d67 4f 57 49 44 20 66 69 65 6c 64 2e 0a 20 20 20 20  OWID field..    
28d68 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 74    */.      int t
28d69 65 73 74 4f 70 20 3d 20 4f 50 5f 4e 6f 6f 70 3b  estOp = OP_Noop;
28d6a 0a 20 20 20 20 20 20 69 6e 74 20 73 74 61 72 74  .      int start
28d6b 3b 0a 20 20 20 20 20 20 57 68 65 72 65 54 65 72  ;.      WhereTer
28d6c 6d 20 2a 70 53 74 61 72 74 2c 20 2a 70 45 6e 64  m *pStart, *pEnd
28d6d 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
28d6e 20 6f 6d 69 74 54 61 62 6c 65 3d 3d 30 20 29 3b   omitTable==0 );
28d6f 0a 20 20 20 20 20 20 70 53 74 61 72 74 20 3d 20  .      pStart = 
28d70 66 69 6e 64 54 65 72 6d 28 26 77 63 2c 20 69 43  findTerm(&wc, iC
28d71 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64 79  ur, -1, notReady
28d72 2c 20 57 4f 5f 47 54 7c 57 4f 5f 47 45 2c 20 30  , WO_GT|WO_GE, 0
28d73 29 3b 0a 20 20 20 20 20 20 70 45 6e 64 20 3d 20  );.      pEnd = 
28d74 66 69 6e 64 54 65 72 6d 28 26 77 63 2c 20 69 43  findTerm(&wc, iC
28d75 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64 79  ur, -1, notReady
28d76 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 2c 20 30  , WO_LT|WO_LE, 0
28d77 29 3b 0a 20 20 20 20 20 20 69 66 28 20 62 52 65  );.      if( bRe
28d78 76 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 65  v ){.        pTe
28d79 72 6d 20 3d 20 70 53 74 61 72 74 3b 0a 20 20 20  rm = pStart;.   
28d7a 20 20 20 20 20 70 53 74 61 72 74 20 3d 20 70 45       pStart = pE
28d7b 6e 64 3b 0a 20 20 20 20 20 20 20 20 70 45 6e 64  nd;.        pEnd
28d7c 20 3d 20 70 54 65 72 6d 3b 0a 20 20 20 20 20 20   = pTerm;.      
28d7d 7d 0a 20 20 20 20 20 20 69 66 28 20 70 53 74 61  }.      if( pSta
28d7e 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 45 78  rt ){.        Ex
28d7f 70 72 20 2a 70 58 3b 0a 20 20 20 20 20 20 20 20  pr *pX;.        
28d80 69 6e 74 20 72 31 3b 0a 20 20 20 20 20 20 20 20  int r1;.        
28d81 70 58 20 3d 20 70 53 74 61 72 74 2d 3e 70 45 78  pX = pStart->pEx
28d82 70 72 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  pr;.        asse
28d83 72 74 28 20 70 58 21 3d 30 20 29 3b 0a 20 20 20  rt( pX!=0 );.   
28d84 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53 74       assert( pSt
28d85 61 72 74 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d  art->leftCursor=
28d86 3d 69 43 75 72 20 29 3b 0a 0a 20 20 20 20 20 20  =iCur );..      
28d87 20 20 2f 2a 20 54 68 65 20 46 6f 72 63 65 49 6e    /* The ForceIn
28d88 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6d 61  t instruction ma
28d89 79 20 6d 6f 64 69 66 79 20 74 68 65 20 72 65 67  y modify the reg
28d8a 69 73 74 65 72 20 74 68 61 74 20 69 74 20 6f 70  ister that it op
28d8b 65 72 61 74 65 73 0a 20 20 20 20 20 20 20 20 2a  erates.        *
28d8c 2a 20 6f 6e 2e 20 46 6f 72 20 65 78 61 6d 70 6c  * on. For exampl
28d8d 65 20 69 74 20 6d 61 79 20 72 65 70 6c 61 63 65  e it may replace
28d8e 20 61 20 72 65 61 6c 20 76 61 6c 75 65 20 77 69   a real value wi
28d8f 74 68 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 6e  th an integer on
28d90 65 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 72  e,.        ** or
28d91 20 69 66 20 70 33 20 69 73 20 74 72 75 65 20 69   if p3 is true i
28d92 74 20 6d 61 79 20 69 6e 63 72 65 6d 65 6e 74 20  t may increment 
28d93 74 68 65 20 72 65 67 69 73 74 65 72 20 76 61 6c  the register val
28d94 75 65 2e 20 46 6f 72 20 74 68 69 73 0a 20 20 20  ue. For this.   
28d95 20 20 20 20 20 2a 2a 20 72 65 61 73 6f 6e 20 77       ** reason w
28d96 65 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 73  e need to make s
28d97 75 72 65 20 74 68 61 74 20 72 65 67 69 73 74 65  ure that registe
28d98 72 20 72 31 20 69 73 20 72 65 61 6c 6c 79 20 61  r r1 is really a
28d99 20 6e 65 77 6c 79 0a 20 20 20 20 20 20 20 20 2a   newly.        *
28d9a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 74 65 6d 70  * allocated temp
28d9b 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 2c 20  orary register, 
28d9c 61 6e 64 20 6e 6f 74 20 70 61 72 74 20 6f 66 20  and not part of 
28d9d 74 68 65 20 63 6f 6c 75 6d 6e 2d 63 61 63 68 65  the column-cache
28d9e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 46 6f 72  ..        ** For
28d9f 20 74 68 69 73 20 72 65 61 73 6f 6e 20 77 65 20   this reason we 
28da0 63 61 6e 6e 6f 74 20 75 73 65 20 73 71 6c 69 74  cannot use sqlit
28da1 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 29  e3ExprCodeTemp()
28da2 20 68 65 72 65 2e 0a 20 20 20 20 20 20 20 20 2a   here..        *
28da3 2f 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20 73  /.        r1 = s
28da4 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
28da5 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
28da6 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
28da7 65 28 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 52  e(pParse, pX->pR
28da8 69 67 68 74 2c 20 72 31 29 3b 0a 0a 20 20 20 20  ight, r1);..    
28da9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
28daa 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 72 63  ddOp3(v, OP_Forc
28dab 65 49 6e 74 2c 20 72 31 2c 20 62 72 6b 2c 20 0a  eInt, r1, brk, .
28dac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28dad 20 20 20 20 20 20 20 20 20 20 20 20 20 70 58 2d               pX-
28dae 3e 6f 70 3d 3d 54 4b 5f 4c 45 20 7c 7c 20 70 58  >op==TK_LE || pX
28daf 2d 3e 6f 70 3d 3d 54 4b 5f 47 54 29 3b 0a 20 20  ->op==TK_GT);.  
28db0 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
28db1 65 41 64 64 4f 70 33 28 76 2c 20 62 52 65 76 20  eAddOp3(v, bRev 
28db2 3f 20 4f 50 5f 4d 6f 76 65 4c 74 20 3a 20 4f 50  ? OP_MoveLt : OP
28db3 5f 4d 6f 76 65 47 65 2c 20 69 43 75 72 2c 20 62  _MoveGe, iCur, b
28db4 72 6b 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20  rk, r1);.       
28db5 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
28db6 20 22 70 6b 22 29 29 3b 0a 20 20 20 20 20 20 20   "pk"));.       
28db7 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
28db8 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
28db9 31 29 3b 0a 20 20 20 20 20 20 20 20 64 69 73 61  1);.        disa
28dba 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20  bleTerm(pLevel, 
28dbb 70 53 74 61 72 74 29 3b 0a 20 20 20 20 20 20 7d  pStart);.      }
28dbc 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
28dbd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
28dbe 76 2c 20 62 52 65 76 20 3f 20 4f 50 5f 4c 61 73  v, bRev ? OP_Las
28dbf 74 20 3a 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69  t : OP_Rewind, i
28dc0 43 75 72 2c 20 62 72 6b 29 3b 0a 20 20 20 20 20  Cur, brk);.     
28dc1 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 45 6e   }.      if( pEn
28dc2 64 20 29 7b 0a 20 20 20 20 20 20 20 20 45 78 70  d ){.        Exp
28dc3 72 20 2a 70 58 3b 0a 20 20 20 20 20 20 20 20 70  r *pX;.        p
28dc4 58 20 3d 20 70 45 6e 64 2d 3e 70 45 78 70 72 3b  X = pEnd->pExpr;
28dc5 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
28dc6 20 70 58 21 3d 30 20 29 3b 0a 20 20 20 20 20 20   pX!=0 );.      
28dc7 20 20 61 73 73 65 72 74 28 20 70 45 6e 64 2d 3e    assert( pEnd->
28dc8 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72  leftCursor==iCur
28dc9 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4c 65 76   );.        pLev
28dca 65 6c 2d 3e 69 4d 65 6d 20 3d 20 2b 2b 70 50 61  el->iMem = ++pPa
28dcb 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20  rse->nMem;.     
28dcc 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
28dcd 64 65 28 70 50 61 72 73 65 2c 20 70 58 2d 3e 70  de(pParse, pX->p
28dce 52 69 67 68 74 2c 20 70 4c 65 76 65 6c 2d 3e 69  Right, pLevel->i
28dcf 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 69 66  Mem);.        if
28dd0 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 4c 54 20  ( pX->op==TK_LT 
28dd1 7c 7c 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 47 54  || pX->op==TK_GT
28dd2 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 74 65   ){.          te
28dd3 73 74 4f 70 20 3d 20 62 52 65 76 20 3f 20 4f 50  stOp = bRev ? OP
28dd4 5f 4c 65 20 3a 20 4f 50 5f 47 65 3b 0a 20 20 20  _Le : OP_Ge;.   
28dd5 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
28dd6 20 20 20 20 20 20 74 65 73 74 4f 70 20 3d 20 62        testOp = b
28dd7 52 65 76 20 3f 20 4f 50 5f 4c 74 20 3a 20 4f 50  Rev ? OP_Lt : OP
28dd8 5f 47 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  _Gt;.        }. 
28dd9 20 20 20 20 20 20 20 64 69 73 61 62 6c 65 54 65         disableTe
28dda 72 6d 28 70 4c 65 76 65 6c 2c 20 70 45 6e 64 29  rm(pLevel, pEnd)
28ddb 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
28ddc 73 74 61 72 74 20 3d 20 73 71 6c 69 74 65 33 56  start = sqlite3V
28ddd 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
28dde 29 3b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d  );.      pLevel-
28ddf 3e 6f 70 20 3d 20 62 52 65 76 20 3f 20 4f 50 5f  >op = bRev ? OP_
28de0 50 72 65 76 20 3a 20 4f 50 5f 4e 65 78 74 3b 0a  Prev : OP_Next;.
28de1 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 31        pLevel->p1
28de2 20 3d 20 69 43 75 72 3b 0a 20 20 20 20 20 20 70   = iCur;.      p
28de3 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 73 74 61 72  Level->p2 = star
28de4 74 3b 0a 20 20 20 20 20 20 69 66 28 20 74 65 73  t;.      if( tes
28de5 74 4f 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a  tOp!=OP_Noop ){.
28de6 20 20 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d          int r1 =
28de7 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
28de8 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
28de9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
28dea 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69  ddOp2(v, OP_Rowi
28deb 64 2c 20 69 43 75 72 2c 20 72 31 29 3b 0a 20 20  d, iCur, r1);.  
28dec 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33        /* sqlite3
28ded 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
28dee 5f 53 43 6f 70 79 2c 20 70 4c 65 76 65 6c 2d 3e  _SCopy, pLevel->
28def 69 4d 65 6d 2c 20 30 29 3b 20 2a 2f 0a 20 20 20  iMem, 0); */.   
28df0 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
28df1 41 64 64 4f 70 33 28 76 2c 20 74 65 73 74 4f 70  AddOp3(v, testOp
28df2 2c 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d 2c 20  , pLevel->iMem, 
28df3 62 72 6b 2c 20 72 31 29 3b 0a 20 20 20 20 20 20  brk, r1);.      
28df4 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
28df5 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54 45 5f  ngeP5(v, SQLITE_
28df6 41 46 46 5f 4e 55 4d 45 52 49 43 20 7c 20 53 51  AFF_NUMERIC | SQ
28df7 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29  LITE_JUMPIFNULL)
28df8 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
28df9 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
28dfa 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20  pParse, r1);.   
28dfb 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69     }.    }else i
28dfc 66 28 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73  f( pLevel->flags
28dfd 20 26 20 28 57 48 45 52 45 5f 43 4f 4c 55 4d 4e   & (WHERE_COLUMN
28dfe 5f 52 41 4e 47 45 7c 57 48 45 52 45 5f 43 4f 4c  _RANGE|WHERE_COL
28dff 55 4d 4e 5f 45 51 29 20 29 7b 0a 20 20 20 20 20  UMN_EQ) ){.     
28e00 20 2f 2a 20 43 61 73 65 20 33 3a 20 41 20 73 63   /* Case 3: A sc
28e01 61 6e 20 75 73 69 6e 67 20 61 6e 20 69 6e 64 65  an using an inde
28e02 78 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  x..      **.    
28e03 20 20 2a 2a 20 20 20 20 20 20 20 20 20 54 68 65    **         The
28e04 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 6d 61   WHERE clause ma
28e05 79 20 63 6f 6e 74 61 69 6e 20 7a 65 72 6f 20 6f  y contain zero o
28e06 72 20 6d 6f 72 65 20 65 71 75 61 6c 69 74 79 20  r more equality 
28e07 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20  .      **       
28e08 20 20 74 65 72 6d 73 20 28 22 3d 3d 22 20 6f 72    terms ("==" or
28e09 20 22 49 4e 22 20 6f 70 65 72 61 74 6f 72 73 29   "IN" operators)
28e0a 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 74   that refer to t
28e0b 68 65 20 4e 0a 20 20 20 20 20 20 2a 2a 20 20 20  he N.      **   
28e0c 20 20 20 20 20 20 6c 65 66 74 2d 6d 6f 73 74 20        left-most 
28e0d 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 69  columns of the i
28e0e 6e 64 65 78 2e 20 49 74 20 6d 61 79 20 61 6c 73  ndex. It may als
28e0f 6f 20 63 6f 6e 74 61 69 6e 0a 20 20 20 20 20 20  o contain.      
28e10 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 65 71 75  **         inequ
28e11 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
28e12 73 20 28 3e 2c 20 3c 2c 20 3e 3d 20 6f 72 20 3c  s (>, <, >= or <
28e13 3d 29 20 6f 6e 20 74 68 65 20 69 6e 64 65 78 65  =) on the indexe
28e14 64 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20  d.      **      
28e15 20 20 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69     column that i
28e16 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f  mmediately follo
28e17 77 73 20 74 68 65 20 4e 20 65 71 75 61 6c 69 74  ws the N equalit
28e18 69 65 73 2e 20 4f 6e 6c 79 20 0a 20 20 20 20 20  ies. Only .     
28e19 20 2a 2a 20 20 20 20 20 20 20 20 20 74 68 65 20   **         the 
28e1a 72 69 67 68 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d  right-most colum
28e1b 6e 20 63 61 6e 20 62 65 20 61 6e 20 69 6e 65 71  n can be an ineq
28e1c 75 61 6c 69 74 79 20 2d 20 74 68 65 20 72 65 73  uality - the res
28e1d 74 20 6d 75 73 74 0a 20 20 20 20 20 20 2a 2a 20  t must.      ** 
28e1e 20 20 20 20 20 20 20 20 75 73 65 20 74 68 65 20          use the 
28e1f 22 3d 3d 22 20 61 6e 64 20 22 49 4e 22 20 6f 70  "==" and "IN" op
28e20 65 72 61 74 6f 72 73 2e 20 46 6f 72 20 65 78 61  erators. For exa
28e21 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 0a 20 20  mple, if the .  
28e22 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 69      **         i
28e23 6e 64 65 78 20 69 73 20 6f 6e 20 28 78 2c 79 2c  ndex is on (x,y,
28e24 7a 29 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 6c  z), then the fol
28e25 6c 6f 77 69 6e 67 20 63 6c 61 75 73 65 73 20 61  lowing clauses a
28e26 72 65 20 61 6c 6c 20 0a 20 20 20 20 20 20 2a 2a  re all .      **
28e27 20 20 20 20 20 20 20 20 20 6f 70 74 69 6d 69 7a           optimiz
28e28 65 64 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  ed:.      **.   
28e29 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20     **           
28e2a 20 78 3d 35 0a 20 20 20 20 20 20 2a 2a 20 20 20   x=5.      **   
28e2b 20 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44           x=5 AND
28e2c 20 79 3d 31 30 0a 20 20 20 20 20 20 2a 2a 20 20   y=10.      **  
28e2d 20 20 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e            x=5 AN
28e2e 44 20 79 3c 31 30 0a 20 20 20 20 20 20 2a 2a 20  D y<10.      ** 
28e2f 20 20 20 20 20 20 20 20 20 20 20 78 3d 35 20 41             x=5 A
28e30 4e 44 20 79 3e 35 20 41 4e 44 20 79 3c 31 30 0a  ND y>5 AND y<10.
28e31 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20        **        
28e32 20 20 20 20 78 3d 35 20 41 4e 44 20 79 3d 35 20      x=5 AND y=5 
28e33 41 4e 44 20 7a 3c 3d 31 30 0a 20 20 20 20 20 20  AND z<=10.      
28e34 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20  **.      **     
28e35 20 20 20 20 54 68 65 20 7a 3c 31 30 20 74 65 72      The z<10 ter
28e36 6d 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  m of the followi
28e37 6e 67 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65  ng cannot be use
28e38 64 2c 20 6f 6e 6c 79 0a 20 20 20 20 20 20 2a 2a  d, only.      **
28e39 20 20 20 20 20 20 20 20 20 74 68 65 20 78 3d 35           the x=5
28e3a 20 74 65 72 6d 3a 0a 20 20 20 20 20 20 2a 2a 0a   term:.      **.
28e3b 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20        **        
28e3c 20 20 20 20 78 3d 35 20 41 4e 44 20 7a 3c 31 30      x=5 AND z<10
28e3d 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
28e3e 2a 2a 20 20 20 20 20 20 20 20 20 4e 20 6d 61 79  **         N may
28e3f 20 62 65 20 7a 65 72 6f 20 69 66 20 74 68 65 72   be zero if ther
28e40 65 20 61 72 65 20 69 6e 65 71 75 61 6c 69 74 79  e are inequality
28e41 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a 20 20   constraints..  
28e42 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 49      **         I
28e43 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 69  f there are no i
28e44 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72  nequality constr
28e45 61 69 6e 74 73 2c 20 74 68 65 6e 20 4e 20 69 73  aints, then N is
28e46 20 61 74 0a 20 20 20 20 20 20 2a 2a 20 20 20 20   at.      **    
28e47 20 20 20 20 20 6c 65 61 73 74 20 6f 6e 65 2e 0a       least one..
28e48 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
28e49 2a 20 20 20 20 20 20 20 20 20 54 68 69 73 20 63  *         This c
28e4a 61 73 65 20 69 73 20 61 6c 73 6f 20 75 73 65 64  ase is also used
28e4b 20 77 68 65 6e 20 74 68 65 72 65 20 61 72 65 20   when there are 
28e4c 6e 6f 20 57 48 45 52 45 20 63 6c 61 75 73 65 0a  no WHERE clause.
28e4d 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20        **        
28e4e 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 62 75 74   constraints but
28e4f 20 61 6e 20 69 6e 64 65 78 20 69 73 20 73 65 6c   an index is sel
28e50 65 63 74 65 64 20 61 6e 79 77 61 79 2c 20 69 6e  ected anyway, in
28e51 20 6f 72 64 65 72 0a 20 20 20 20 20 20 2a 2a 20   order.      ** 
28e52 20 20 20 20 20 20 20 20 74 6f 20 66 6f 72 63 65          to force
28e53 20 74 68 65 20 6f 75 74 70 75 74 20 6f 72 64 65   the output orde
28e54 72 20 74 6f 20 63 6f 6e 66 6f 72 6d 20 74 6f 20  r to conform to 
28e55 61 6e 20 4f 52 44 45 52 20 42 59 2e 0a 20 20 20  an ORDER BY..   
28e56 20 20 20 2a 2f 20 20 0a 20 20 20 20 20 20 69 6e     */  .      in
28e57 74 20 61 53 74 61 72 74 4f 70 5b 5d 20 3d 20 7b  t aStartOp[] = {
28e58 0a 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20 20  .        0,.    
28e59 20 20 20 20 30 2c 0a 20 20 20 20 20 20 20 20 4f      0,.        O
28e5a 50 5f 52 65 77 69 6e 64 2c 20 20 20 20 20 20 20  P_Rewind,       
28e5b 20 20 20 20 2f 2a 20 32 3a 20 28 21 73 74 61 72      /* 2: (!star
28e5c 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 26 26  t_constraints &&
28e5d 20 73 74 61 72 74 45 71 20 26 26 20 20 21 62 52   startEq &&  !bR
28e5e 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 4f  ev) */.        O
28e5f 50 5f 4c 61 73 74 2c 20 20 20 20 20 20 20 20 20  P_Last,         
28e60 20 20 20 20 2f 2a 20 33 3a 20 28 21 73 74 61 72      /* 3: (!star
28e61 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 26 26  t_constraints &&
28e62 20 73 74 61 72 74 45 71 20 26 26 20 20 20 62 52   startEq &&   bR
28e63 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 4f  ev) */.        O
28e64 50 5f 4d 6f 76 65 47 74 2c 20 20 20 20 20 20 20  P_MoveGt,       
28e65 20 20 20 20 2f 2a 20 34 3a 20 28 73 74 61 72 74      /* 4: (start
28e66 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 20 26 26  _constraints  &&
28e67 20 21 73 74 61 72 74 45 71 20 26 26 20 21 62 52   !startEq && !bR
28e68 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 4f  ev) */.        O
28e69 50 5f 4d 6f 76 65 4c 74 2c 20 20 20 20 20 20 20  P_MoveLt,       
28e6a 20 20 20 20 2f 2a 20 35 3a 20 28 73 74 61 72 74      /* 5: (start
28e6b 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 20 26 26  _constraints  &&
28e6c 20 21 73 74 61 72 74 45 71 20 26 26 20 20 62 52   !startEq &&  bR
28e6d 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 4f  ev) */.        O
28e6e 50 5f 4d 6f 76 65 47 65 2c 20 20 20 20 20 20 20  P_MoveGe,       
28e6f 20 20 20 20 2f 2a 20 36 3a 20 28 73 74 61 72 74      /* 6: (start
28e70 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 20 26 26  _constraints  &&
28e71 20 20 73 74 61 72 74 45 71 20 26 26 20 21 62 52    startEq && !bR
28e72 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 4f  ev) */.        O
28e73 50 5f 4d 6f 76 65 4c 65 20 20 20 20 20 20 20 20  P_MoveLe        
28e74 20 20 20 20 2f 2a 20 37 3a 20 28 73 74 61 72 74      /* 7: (start
28e75 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 20 26 26  _constraints  &&
28e76 20 20 73 74 61 72 74 45 71 20 26 26 20 20 62 52    startEq &&  bR
28e77 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 7d 3b 0a  ev) */.      };.
28e78 20 20 20 20 20 20 69 6e 74 20 61 45 6e 64 4f 70        int aEndOp
28e79 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 4f  [] = {.        O
28e7a 50 5f 4e 6f 6f 70 2c 20 20 20 20 20 20 20 20 20  P_Noop,         
28e7b 20 20 20 20 2f 2a 20 30 3a 20 28 21 65 6e 64 5f      /* 0: (!end_
28e7c 63 6f 6e 73 74 72 61 69 6e 74 73 29 20 2a 2f 0a  constraints) */.
28e7d 20 20 20 20 20 20 20 20 4f 50 5f 49 64 78 47 45          OP_IdxGE
28e7e 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
28e7f 31 3a 20 28 65 6e 64 5f 63 6f 6e 73 74 72 61 69  1: (end_constrai
28e80 6e 74 73 20 26 26 20 21 62 52 65 76 29 20 2a 2f  nts && !bRev) */
28e81 0a 20 20 20 20 20 20 20 20 4f 50 5f 49 64 78 4c  .        OP_IdxL
28e82 54 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  T             /*
28e83 20 32 3a 20 28 65 6e 64 5f 63 6f 6e 73 74 72 61   2: (end_constra
28e84 69 6e 74 73 20 26 26 20 62 52 65 76 29 20 2a 2f  ints && bRev) */
28e85 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20  .      };.      
28e86 69 6e 74 20 6e 45 71 20 3d 20 70 4c 65 76 65 6c  int nEq = pLevel
28e87 2d 3e 6e 45 71 3b 0a 20 20 20 20 20 20 69 6e 74  ->nEq;.      int
28e88 20 69 73 4d 69 6e 51 75 65 72 79 20 3d 20 30 3b   isMinQuery = 0;
28e89 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20            /* If 
28e8a 74 68 69 73 20 69 73 20 61 6e 20 6f 70 74 69 6d  this is an optim
28e8b 69 7a 65 64 20 53 45 4c 45 43 54 20 6d 69 6e 28  ized SELECT min(
28e8c 78 29 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 69 6e  x).. */.      in
28e8d 74 20 72 65 67 42 61 73 65 3b 20 20 20 20 20 20  t regBase;      
28e8e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61             /* Ba
28e8f 73 65 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64  se register hold
28e90 69 6e 67 20 63 6f 6e 73 74 72 61 69 6e 74 20 76  ing constraint v
28e91 61 6c 75 65 73 20 2a 2f 0a 20 20 20 20 20 20 69  alues */.      i
28e92 6e 74 20 72 31 3b 20 20 20 20 20 20 20 20 20 20  nt r1;          
28e93 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
28e94 65 6d 70 20 72 65 67 69 73 74 65 72 20 2a 2f 0a  emp register */.
28e95 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20        WhereTerm 
28e96 2a 70 52 61 6e 67 65 53 74 61 72 74 20 3d 20 30  *pRangeStart = 0
28e97 3b 20 20 2f 2a 20 49 6e 65 71 75 61 6c 69 74 79  ;  /* Inequality
28e98 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 74 20 72   constraint at r
28e99 61 6e 67 65 20 73 74 61 72 74 20 2a 2f 0a 20 20  ange start */.  
28e9a 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70      WhereTerm *p
28e9b 52 61 6e 67 65 45 6e 64 20 3d 20 30 3b 20 20 20  RangeEnd = 0;   
28e9c 20 2f 2a 20 49 6e 65 71 75 61 6c 69 74 79 20 63   /* Inequality c
28e9d 6f 6e 73 74 72 61 69 6e 74 20 61 74 20 72 61 6e  onstraint at ran
28e9e 67 65 20 65 6e 64 20 2a 2f 0a 20 20 20 20 20 20  ge end */.      
28e9f 69 6e 74 20 73 74 61 72 74 45 71 3b 20 20 20 20  int startEq;    
28ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
28ea1 54 72 75 65 20 69 66 20 72 61 6e 67 65 20 73 74  True if range st
28ea2 61 72 74 20 75 73 65 73 20 3d 3d 2c 20 3e 3d 20  art uses ==, >= 
28ea3 6f 72 20 3c 3d 20 2a 2f 0a 20 20 20 20 20 20 69  or <= */.      i
28ea4 6e 74 20 65 6e 64 45 71 3b 20 20 20 20 20 20 20  nt endEq;       
28ea5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
28ea6 72 75 65 20 69 66 20 72 61 6e 67 65 20 65 6e 64  rue if range end
28ea7 20 75 73 65 73 20 3d 3d 2c 20 3e 3d 20 6f 72 20   uses ==, >= or 
28ea8 3c 3d 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  <= */.      int 
28ea9 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74  start_constraint
28eaa 73 3b 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72  s;       /* Star
28eab 74 20 6f 66 20 72 61 6e 67 65 20 69 73 20 63 6f  t of range is co
28eac 6e 73 74 72 61 69 6e 65 64 20 2a 2f 0a 20 20 20  nstrained */.   
28ead 20 20 20 69 6e 74 20 6b 20 3d 20 70 49 64 78 2d     int k = pIdx-
28eae 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 45 71 5d 3b 20  >aiColumn[nEq]; 
28eaf 2f 2a 20 43 6f 6c 75 6d 6e 20 66 6f 72 20 69 6e  /* Column for in
28eb0 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
28eb1 69 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e  ints */.      in
28eb2 74 20 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20  t nConstraint;  
28eb3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
28eb4 6d 62 65 72 20 6f 66 20 63 6f 6e 73 74 72 61 69  mber of constrai
28eb5 6e 74 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20  nt terms */.    
28eb6 20 20 69 6e 74 20 6f 70 3b 0a 0a 20 20 20 20 20    int op;..     
28eb7 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
28eb8 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 61 6c  e to evaluate al
28eb9 6c 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 65 72  l constraint ter
28eba 6d 73 20 75 73 69 6e 67 20 3d 3d 20 6f 72 20 49  ms using == or I
28ebb 4e 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 73  N.      ** and s
28ebc 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 73 20  tore the values 
28ebd 6f 66 20 74 68 6f 73 65 20 74 65 72 6d 73 20 69  of those terms i
28ebe 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 72 65  n an array of re
28ebf 67 69 73 74 65 72 73 0a 20 20 20 20 20 20 2a 2a  gisters.      **
28ec0 20 73 74 61 72 74 69 6e 67 20 61 74 20 72 65 67   starting at reg
28ec1 42 61 73 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  Base..      */. 
28ec2 20 20 20 20 20 72 65 67 42 61 73 65 20 3d 20 63       regBase = c
28ec3 6f 64 65 41 6c 6c 45 71 75 61 6c 69 74 79 54 65  odeAllEqualityTe
28ec4 72 6d 73 28 70 50 61 72 73 65 2c 20 70 4c 65 76  rms(pParse, pLev
28ec5 65 6c 2c 20 26 77 63 2c 20 6e 6f 74 52 65 61 64  el, &wc, notRead
28ec6 79 2c 20 32 29 3b 0a 20 20 20 20 20 20 6e 78 74  y, 2);.      nxt
28ec7 20 3d 20 70 4c 65 76 65 6c 2d 3e 6e 78 74 3b 0a   = pLevel->nxt;.
28ec8 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69  .      /* If thi
28ec9 73 20 6c 6f 6f 70 20 73 61 74 69 73 66 69 65 73  s loop satisfies
28eca 20 61 20 73 6f 72 74 20 6f 72 64 65 72 20 28 70   a sort order (p
28ecb 4f 72 64 65 72 42 79 29 20 72 65 71 75 65 73 74  OrderBy) request
28ecc 20 74 68 61 74 20 0a 20 20 20 20 20 20 2a 2a 20   that .      ** 
28ecd 77 61 73 20 70 61 73 73 65 64 20 74 6f 20 74 68  was passed to th
28ece 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69  is function to i
28ecf 6d 70 6c 65 6d 65 6e 74 20 61 20 22 53 45 4c 45  mplement a "SELE
28ed0 43 54 20 6d 69 6e 28 78 29 20 2e 2e 2e 22 20 0a  CT min(x) ..." .
28ed1 20 20 20 20 20 20 2a 2a 20 71 75 65 72 79 2c 20        ** query, 
28ed2 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 20  then the caller 
28ed3 77 69 6c 6c 20 6f 6e 6c 79 20 61 6c 6c 6f 77 20  will only allow 
28ed4 74 68 65 20 6c 6f 6f 70 20 74 6f 20 72 75 6e 20  the loop to run 
28ed5 66 6f 72 0a 20 20 20 20 20 20 2a 2a 20 61 20 73  for.      ** a s
28ed6 69 6e 67 6c 65 20 69 74 65 72 61 74 69 6f 6e 2e  ingle iteration.
28ed7 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74   This means that
28ed8 20 74 68 65 20 66 69 72 73 74 20 72 6f 77 20 72   the first row r
28ed9 65 74 75 72 6e 65 64 0a 20 20 20 20 20 20 2a 2a  eturned.      **
28eda 20 73 68 6f 75 6c 64 20 6e 6f 74 20 68 61 76 65   should not have
28edb 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20 73 74   a NULL value st
28edc 6f 72 65 64 20 69 6e 20 27 78 27 2e 20 49 66 20  ored in 'x'. If 
28edd 63 6f 6c 75 6d 6e 20 27 78 27 20 69 73 0a 20 20  column 'x' is.  
28ede 20 20 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74      ** the first
28edf 20 6f 6e 65 20 61 66 74 65 72 20 74 68 65 20 6e   one after the n
28ee0 45 71 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73  Eq equality cons
28ee1 74 72 61 69 6e 74 73 20 69 6e 20 74 68 65 20 69  traints in the i
28ee2 6e 64 65 78 2c 0a 20 20 20 20 20 20 2a 2a 20 74  ndex,.      ** t
28ee3 68 69 73 20 72 65 71 75 69 72 65 73 20 73 6f 6d  his requires som
28ee4 65 20 73 70 65 63 69 61 6c 20 68 61 6e 64 6c 69  e special handli
28ee5 6e 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ng..      */.   
28ee6 20 20 20 69 66 28 20 28 77 66 6c 61 67 73 26 57     if( (wflags&W
28ee7 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e  HERE_ORDERBY_MIN
28ee8 29 21 3d 30 0a 20 20 20 20 20 20 20 26 26 20 28  )!=0.       && (
28ee9 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 26 57 48  pLevel->flags&WH
28eea 45 52 45 5f 4f 52 44 45 52 42 59 29 0a 20 20 20  ERE_ORDERBY).   
28eeb 20 20 20 20 26 26 20 28 70 49 64 78 2d 3e 6e 43      && (pIdx->nC
28eec 6f 6c 75 6d 6e 3e 6e 45 71 29 0a 20 20 20 20 20  olumn>nEq).     
28eed 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
28eee 72 74 28 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45  rt( pOrderBy->nE
28eef 78 70 72 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20  xpr==1 );.      
28ef0 20 20 61 73 73 65 72 74 28 20 70 4f 72 64 65 72    assert( pOrder
28ef1 42 79 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e  By->a[0].pExpr->
28ef2 69 43 6f 6c 75 6d 6e 3d 3d 70 49 64 78 2d 3e 61  iColumn==pIdx->a
28ef3 69 43 6f 6c 75 6d 6e 5b 6e 45 71 5d 20 29 3b 0a  iColumn[nEq] );.
28ef4 20 20 20 20 20 20 20 20 69 73 4d 69 6e 51 75 65          isMinQue
28ef5 72 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  ry = 1;.      }.
28ef6 0a 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 20 61  .      /* Find a
28ef7 6e 79 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f  ny inequality co
28ef8 6e 73 74 72 61 69 6e 74 20 74 65 72 6d 73 20 66  nstraint terms f
28ef9 6f 72 20 74 68 65 20 73 74 61 72 74 20 61 6e 64  or the start and
28efa 20 65 6e 64 20 0a 20 20 20 20 20 20 2a 2a 20 6f   end .      ** o
28efb 66 20 74 68 65 20 72 61 6e 67 65 2e 20 0a 20 20  f the range. .  
28efc 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
28efd 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26   pLevel->flags &
28efe 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54   WHERE_TOP_LIMIT
28eff 20 29 7b 0a 20 20 20 20 20 20 20 20 70 52 61 6e   ){.        pRan
28f00 67 65 45 6e 64 20 3d 20 66 69 6e 64 54 65 72 6d  geEnd = findTerm
28f01 28 26 77 63 2c 20 69 43 75 72 2c 20 6b 2c 20 6e  (&wc, iCur, k, n
28f02 6f 74 52 65 61 64 79 2c 20 28 57 4f 5f 4c 54 7c  otReady, (WO_LT|
28f03 57 4f 5f 4c 45 29 2c 20 70 49 64 78 29 3b 0a 20  WO_LE), pIdx);. 
28f04 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
28f05 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26   pLevel->flags &
28f06 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54   WHERE_BTM_LIMIT
28f07 20 29 7b 0a 20 20 20 20 20 20 20 20 70 52 61 6e   ){.        pRan
28f08 67 65 53 74 61 72 74 20 3d 20 66 69 6e 64 54 65  geStart = findTe
28f09 72 6d 28 26 77 63 2c 20 69 43 75 72 2c 20 6b 2c  rm(&wc, iCur, k,
28f0a 20 6e 6f 74 52 65 61 64 79 2c 20 28 57 4f 5f 47   notReady, (WO_G
28f0b 54 7c 57 4f 5f 47 45 29 2c 20 70 49 64 78 29 3b  T|WO_GE), pIdx);
28f0c 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
28f0d 2f 2a 20 49 66 20 77 65 20 61 72 65 20 64 6f 69  /* If we are doi
28f0e 6e 67 20 61 20 72 65 76 65 72 73 65 20 6f 72 64  ng a reverse ord
28f0f 65 72 20 73 63 61 6e 20 6f 6e 20 61 6e 20 61 73  er scan on an as
28f10 63 65 6e 64 69 6e 67 20 69 6e 64 65 78 2c 20 6f  cending index, o
28f11 72 0a 20 20 20 20 20 20 2a 2a 20 61 20 66 6f 72  r.      ** a for
28f12 77 61 72 64 20 6f 72 64 65 72 20 73 63 61 6e 20  ward order scan 
28f13 6f 6e 20 61 20 64 65 73 63 65 6e 64 69 6e 67 20  on a descending 
28f14 69 6e 64 65 78 2c 20 69 6e 74 65 72 63 68 61 6e  index, interchan
28f15 67 65 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a  ge the .      **
28f16 20 73 74 61 72 74 20 61 6e 64 20 65 6e 64 20 74   start and end t
28f17 65 72 6d 73 20 28 70 52 61 6e 67 65 53 74 61 72  erms (pRangeStar
28f18 74 20 61 6e 64 20 70 52 61 6e 67 65 45 6e 64 29  t and pRangeEnd)
28f19 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
28f1a 20 69 66 28 20 62 52 65 76 3d 3d 28 70 49 64 78   if( bRev==(pIdx
28f1b 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6e 45 71  ->aSortOrder[nEq
28f1c 5d 3d 3d 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43  ]==SQLITE_SO_ASC
28f1d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 53 57 41  ) ){.        SWA
28f1e 50 28 57 68 65 72 65 54 65 72 6d 20 2a 2c 20 70  P(WhereTerm *, p
28f1f 52 61 6e 67 65 45 6e 64 2c 20 70 52 61 6e 67 65  RangeEnd, pRange
28f20 53 74 61 72 74 29 3b 0a 20 20 20 20 20 20 7d 0a  Start);.      }.
28f21 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
28f22 20 70 52 61 6e 67 65 53 74 61 72 74 20 26 26 20   pRangeStart && 
28f23 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 65 4f 70  pRangeStart->eOp
28f24 65 72 61 74 6f 72 20 26 20 57 4f 5f 4c 45 20 29  erator & WO_LE )
28f25 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
28f26 28 20 70 52 61 6e 67 65 53 74 61 72 74 20 26 26  ( pRangeStart &&
28f27 20 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 65 4f   pRangeStart->eO
28f28 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 47 45 20  perator & WO_GE 
28f29 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
28f2a 65 28 20 70 52 61 6e 67 65 45 6e 64 20 26 26 20  e( pRangeEnd && 
28f2b 70 52 61 6e 67 65 45 6e 64 2d 3e 65 4f 70 65 72  pRangeEnd->eOper
28f2c 61 74 6f 72 20 26 20 57 4f 5f 4c 45 20 29 3b 0a  ator & WO_LE );.
28f2d 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
28f2e 70 52 61 6e 67 65 45 6e 64 20 26 26 20 70 52 61  pRangeEnd && pRa
28f2f 6e 67 65 45 6e 64 2d 3e 65 4f 70 65 72 61 74 6f  ngeEnd->eOperato
28f30 72 20 26 20 57 4f 5f 47 45 20 29 3b 0a 20 20 20  r & WO_GE );.   
28f31 20 20 20 73 74 61 72 74 45 71 20 3d 20 21 70 52     startEq = !pR
28f32 61 6e 67 65 53 74 61 72 74 20 7c 7c 20 70 52 61  angeStart || pRa
28f33 6e 67 65 53 74 61 72 74 2d 3e 65 4f 70 65 72 61  ngeStart->eOpera
28f34 74 6f 72 20 26 20 28 57 4f 5f 4c 45 7c 57 4f 5f  tor & (WO_LE|WO_
28f35 47 45 29 3b 0a 20 20 20 20 20 20 65 6e 64 45 71  GE);.      endEq
28f36 20 3d 20 20 20 21 70 52 61 6e 67 65 45 6e 64 20   =   !pRangeEnd 
28f37 7c 7c 20 70 52 61 6e 67 65 45 6e 64 2d 3e 65 4f  || pRangeEnd->eO
28f38 70 65 72 61 74 6f 72 20 26 20 28 57 4f 5f 4c 45  perator & (WO_LE
28f39 7c 57 4f 5f 47 45 29 3b 0a 20 20 20 20 20 20 73  |WO_GE);.      s
28f3a 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73  tart_constraints
28f3b 20 3d 20 70 52 61 6e 67 65 53 74 61 72 74 20 7c   = pRangeStart |
28f3c 7c 20 6e 45 71 3e 30 3b 0a 0a 20 20 20 20 20 20  | nEq>0;..      
28f3d 2f 2a 20 53 65 65 6b 20 74 68 65 20 69 6e 64 65  /* Seek the inde
28f3e 78 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20  x cursor to the 
28f3f 73 74 61 72 74 20 6f 66 20 74 68 65 20 72 61 6e  start of the ran
28f40 67 65 2e 20 2a 2f 0a 20 20 20 20 20 20 6e 43 6f  ge. */.      nCo
28f41 6e 73 74 72 61 69 6e 74 20 3d 20 6e 45 71 3b 0a  nstraint = nEq;.
28f42 20 20 20 20 20 20 69 66 28 20 70 52 61 6e 67 65        if( pRange
28f43 53 74 61 72 74 20 29 7b 0a 20 20 20 20 20 20 20  Start ){.       
28f44 20 69 6e 74 20 64 63 63 20 3d 20 70 50 61 72 73   int dcc = pPars
28f45 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63  e->disableColCac
28f46 68 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  he;.        if( 
28f47 70 52 61 6e 67 65 45 6e 64 20 29 7b 0a 20 20 20  pRangeEnd ){.   
28f48 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64         pParse->d
28f49 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 2b 2b  isableColCache++
28f4a 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
28f4b 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
28f4c 6f 64 65 28 70 50 61 72 73 65 2c 20 70 52 61 6e  ode(pParse, pRan
28f4d 67 65 53 74 61 72 74 2d 3e 70 45 78 70 72 2d 3e  geStart->pExpr->
28f4e 70 52 69 67 68 74 2c 20 72 65 67 42 61 73 65 2b  pRight, regBase+
28f4f 6e 45 71 29 3b 0a 20 20 20 20 20 20 20 20 70 50  nEq);.        pP
28f50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c  arse->disableCol
28f51 43 61 63 68 65 20 3d 20 64 63 63 3b 0a 20 20 20  Cache = dcc;.   
28f52 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
28f53 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 73 4e  AddOp2(v, OP_IsN
28f54 75 6c 6c 2c 20 72 65 67 42 61 73 65 2b 6e 45 71  ull, regBase+nEq
28f55 2c 20 6e 78 74 29 3b 0a 20 20 20 20 20 20 20 20  , nxt);.        
28f56 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b 0a 20  nConstraint++;. 
28f57 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69       }else if( i
28f58 73 4d 69 6e 51 75 65 72 79 20 29 7b 0a 20 20 20  sMinQuery ){.   
28f59 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
28f5a 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
28f5b 6c 2c 20 30 2c 20 72 65 67 42 61 73 65 2b 6e 45  l, 0, regBase+nE
28f5c 71 29 3b 0a 20 20 20 20 20 20 20 20 6e 43 6f 6e  q);.        nCon
28f5d 73 74 72 61 69 6e 74 2b 2b 3b 0a 20 20 20 20 20  straint++;.     
28f5e 20 20 20 73 74 61 72 74 45 71 20 3d 20 30 3b 0a     startEq = 0;.
28f5f 20 20 20 20 20 20 20 20 73 74 61 72 74 5f 63 6f          start_co
28f60 6e 73 74 72 61 69 6e 74 73 20 3d 20 31 3b 0a 20  nstraints = 1;. 
28f61 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 6f 64       }.      cod
28f62 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70  eApplyAffinity(p
28f63 50 61 72 73 65 2c 20 72 65 67 42 61 73 65 2c 20  Parse, regBase, 
28f64 6e 43 6f 6e 73 74 72 61 69 6e 74 2c 20 70 49 64  nConstraint, pId
28f65 78 29 3b 0a 20 20 20 20 20 20 6f 70 20 3d 20 61  x);.      op = a
28f66 53 74 61 72 74 4f 70 5b 28 73 74 61 72 74 5f 63  StartOp[(start_c
28f67 6f 6e 73 74 72 61 69 6e 74 73 3c 3c 32 29 20 2b  onstraints<<2) +
28f68 20 28 73 74 61 72 74 45 71 3c 3c 31 29 20 2b 20   (startEq<<1) + 
28f69 62 52 65 76 5d 3b 0a 20 20 20 20 20 20 61 73 73  bRev];.      ass
28f6a 65 72 74 28 20 6f 70 21 3d 30 20 29 3b 0a 20 20  ert( op!=0 );.  
28f6b 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
28f6c 3d 3d 4f 50 5f 52 65 77 69 6e 64 20 29 3b 0a 20  ==OP_Rewind );. 
28f6d 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
28f6e 70 3d 3d 4f 50 5f 4c 61 73 74 20 29 3b 0a 20 20  p==OP_Last );.  
28f6f 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
28f70 3d 3d 4f 50 5f 4d 6f 76 65 47 74 20 29 3b 0a 20  ==OP_MoveGt );. 
28f71 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
28f72 70 3d 3d 4f 50 5f 4d 6f 76 65 47 65 20 29 3b 0a  p==OP_MoveGe );.
28f73 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
28f74 6f 70 3d 3d 4f 50 5f 4d 6f 76 65 4c 65 20 29 3b  op==OP_MoveLe );
28f75 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
28f76 20 6f 70 3d 3d 4f 50 5f 4d 6f 76 65 4c 74 20 29   op==OP_MoveLt )
28f77 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
28f78 64 62 65 41 64 64 4f 70 34 28 76 2c 20 6f 70 2c  dbeAddOp4(v, op,
28f79 20 69 49 64 78 43 75 72 2c 20 6e 78 74 2c 20 72   iIdxCur, nxt, r
28f7a 65 67 42 61 73 65 2c 20 0a 20 20 20 20 20 20 20  egBase, .       
28f7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28f7c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50   SQLITE_INT_TO_P
28f7d 54 52 28 6e 43 6f 6e 73 74 72 61 69 6e 74 29 2c  TR(nConstraint),
28f7e 20 50 34 5f 49 4e 54 33 32 29 3b 0a 0a 20 20 20   P4_INT32);..   
28f7f 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 76     /* Load the v
28f80 61 6c 75 65 20 66 6f 72 20 74 68 65 20 69 6e 65  alue for the ine
28f81 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69  quality constrai
28f82 6e 74 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  nt at the end of
28f83 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 72 61   the.      ** ra
28f84 6e 67 65 20 28 69 66 20 61 6e 79 29 2e 0a 20 20  nge (if any)..  
28f85 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6e 43 6f      */.      nCo
28f86 6e 73 74 72 61 69 6e 74 20 3d 20 6e 45 71 3b 0a  nstraint = nEq;.
28f87 20 20 20 20 20 20 69 66 28 20 70 52 61 6e 67 65        if( pRange
28f88 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 20 20 73  End ){.        s
28f89 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
28f8a 50 61 72 73 65 2c 20 70 52 61 6e 67 65 45 6e 64  Parse, pRangeEnd
28f8b 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  ->pExpr->pRight,
28f8c 20 72 65 67 42 61 73 65 2b 6e 45 71 29 3b 0a 20   regBase+nEq);. 
28f8d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
28f8e 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
28f8f 73 4e 75 6c 6c 2c 20 72 65 67 42 61 73 65 2b 6e  sNull, regBase+n
28f90 45 71 2c 20 6e 78 74 29 3b 0a 20 20 20 20 20 20  Eq, nxt);.      
28f91 20 20 63 6f 64 65 41 70 70 6c 79 41 66 66 69 6e    codeApplyAffin
28f92 69 74 79 28 70 50 61 72 73 65 2c 20 72 65 67 42  ity(pParse, regB
28f93 61 73 65 2c 20 6e 45 71 2b 31 2c 20 70 49 64 78  ase, nEq+1, pIdx
28f94 29 3b 0a 20 20 20 20 20 20 20 20 6e 43 6f 6e 73  );.        nCons
28f95 74 72 61 69 6e 74 2b 2b 3b 0a 20 20 20 20 20 20  traint++;.      
28f96 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 54 6f 70 20  }..      /* Top 
28f97 6f 66 20 74 68 65 20 6c 6f 6f 70 20 62 6f 64 79  of the loop body
28f98 20 2a 2f 0a 20 20 20 20 20 20 70 4c 65 76 65 6c   */.      pLevel
28f99 2d 3e 70 32 20 3d 20 73 71 6c 69 74 65 33 56 64  ->p2 = sqlite3Vd
28f9a 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
28f9b 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63  ;..      /* Chec
28f9c 6b 20 69 66 20 74 68 65 20 69 6e 64 65 78 20 63  k if the index c
28f9d 75 72 73 6f 72 20 69 73 20 70 61 73 74 20 74 68  ursor is past th
28f9e 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72 61 6e  e end of the ran
28f9f 67 65 2e 20 2a 2f 0a 20 20 20 20 20 20 6f 70 20  ge. */.      op 
28fa0 3d 20 61 45 6e 64 4f 70 5b 28 70 52 61 6e 67 65  = aEndOp[(pRange
28fa1 45 6e 64 20 7c 7c 20 6e 45 71 29 20 2a 20 28 31  End || nEq) * (1
28fa2 20 2b 20 62 52 65 76 29 5d 3b 0a 20 20 20 20 20   + bRev)];.     
28fa3 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f   testcase( op==O
28fa4 50 5f 4e 6f 6f 70 20 29 3b 0a 20 20 20 20 20 20  P_Noop );.      
28fa5 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50  testcase( op==OP
28fa6 5f 49 64 78 47 45 20 29 3b 0a 20 20 20 20 20 20  _IdxGE );.      
28fa7 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50  testcase( op==OP
28fa8 5f 49 64 78 4c 54 20 29 3b 0a 20 20 20 20 20 20  _IdxLT );.      
28fa9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28faa 34 28 76 2c 20 6f 70 2c 20 69 49 64 78 43 75 72  4(v, op, iIdxCur
28fab 2c 20 6e 78 74 2c 20 72 65 67 42 61 73 65 2c 0a  , nxt, regBase,.
28fac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28fad 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49          SQLITE_I
28fae 4e 54 5f 54 4f 5f 50 54 52 28 6e 43 6f 6e 73 74  NT_TO_PTR(nConst
28faf 72 61 69 6e 74 29 2c 20 50 34 5f 49 4e 54 33 32  raint), P4_INT32
28fb0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
28fb1 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
28fb2 65 6e 64 45 71 21 3d 62 52 65 76 29 3b 0a 0a 20  endEq!=bRev);.. 
28fb3 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65       /* If there
28fb4 20 61 72 65 20 69 6e 65 71 75 61 6c 69 74 79 20   are inequality 
28fb5 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 63 68 65  constraints, che
28fb6 63 6b 20 74 68 61 74 20 74 68 65 20 76 61 6c 75  ck that the valu
28fb7 65 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68  e.      ** of th
28fb8 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74  e table column t
28fb9 68 61 74 20 74 68 65 20 69 6e 65 71 75 61 6c 69  hat the inequali
28fba 74 79 20 63 6f 6e 74 72 61 69 6e 73 20 69 73 20  ty contrains is 
28fbb 6e 6f 74 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 20  not NULL..      
28fbc 2a 2a 20 49 66 20 69 74 20 69 73 2c 20 6a 75 6d  ** If it is, jum
28fbd 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 74  p to the next it
28fbe 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c  eration of the l
28fbf 6f 6f 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  oop..      */.  
28fc0 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
28fc1 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
28fc2 65 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  e);.      testca
28fc3 73 65 28 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67  se( pLevel->flag
28fc4 73 20 26 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49  s & WHERE_BTM_LI
28fc5 4d 49 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73  MIT );.      tes
28fc6 74 63 61 73 65 28 20 70 4c 65 76 65 6c 2d 3e 66  tcase( pLevel->f
28fc7 6c 61 67 73 20 26 20 57 48 45 52 45 5f 54 4f 50  lags & WHERE_TOP
28fc8 5f 4c 49 4d 49 54 20 29 3b 0a 20 20 20 20 20 20  _LIMIT );.      
28fc9 69 66 28 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67  if( pLevel->flag
28fca 73 20 26 20 28 57 48 45 52 45 5f 42 54 4d 5f 4c  s & (WHERE_BTM_L
28fcb 49 4d 49 54 7c 57 48 45 52 45 5f 54 4f 50 5f 4c  IMIT|WHERE_TOP_L
28fcc 49 4d 49 54 29 20 29 7b 0a 20 20 20 20 20 20 20  IMIT) ){.       
28fcd 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
28fce 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
28fcf 20 69 49 64 78 43 75 72 2c 20 6e 45 71 2c 20 72   iIdxCur, nEq, r
28fd0 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  1);.        sqli
28fd1 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
28fd2 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 31 2c 20   OP_IsNull, r1, 
28fd3 63 6f 6e 74 29 3b 0a 20 20 20 20 20 20 7d 0a 0a  cont);.      }..
28fd4 20 20 20 20 20 20 2f 2a 20 53 65 65 6b 20 74 68        /* Seek th
28fd5 65 20 74 61 62 6c 65 20 63 75 72 73 6f 72 2c 20  e table cursor, 
28fd6 69 66 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20  if required */. 
28fd7 20 20 20 20 20 69 66 28 20 21 6f 6d 69 74 54 61       if( !omitTa
28fd8 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ble ){.        s
28fd9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
28fda 28 76 2c 20 4f 50 5f 49 64 78 52 6f 77 69 64 2c  (v, OP_IdxRowid,
28fdb 20 69 49 64 78 43 75 72 2c 20 72 31 29 3b 0a 20   iIdxCur, r1);. 
28fdc 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
28fdd 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
28fde 6f 76 65 47 65 2c 20 69 43 75 72 2c 20 30 2c 20  oveGe, iCur, 0, 
28fdf 72 31 29 3b 20 20 2f 2a 20 44 65 66 65 72 72 65  r1);  /* Deferre
28fe0 64 20 73 65 65 6b 20 2a 2f 0a 20 20 20 20 20 20  d seek */.      
28fe1 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  }.      sqlite3R
28fe2 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
28fe3 61 72 73 65 2c 20 72 31 29 3b 0a 0a 20 20 20 20  arse, r1);..    
28fe4 20 20 2f 2a 20 52 65 63 6f 72 64 20 74 68 65 20    /* Record the 
28fe5 69 6e 73 74 72 75 63 74 69 6f 6e 20 75 73 65 64  instruction used
28fe6 20 74 6f 20 74 65 72 6d 69 6e 61 74 65 20 74 68   to terminate th
28fe7 65 20 6c 6f 6f 70 2e 20 44 69 73 61 62 6c 65 20  e loop. Disable 
28fe8 0a 20 20 20 20 20 20 2a 2a 20 57 48 45 52 45 20  .      ** WHERE 
28fe9 63 6c 61 75 73 65 20 74 65 72 6d 73 20 6d 61 64  clause terms mad
28fea 65 20 72 65 64 75 6e 64 61 6e 74 20 62 79 20 74  e redundant by t
28feb 68 65 20 69 6e 64 65 78 20 72 61 6e 67 65 20 73  he index range s
28fec 63 61 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  can..      */.  
28fed 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d      pLevel->op =
28fee 20 62 52 65 76 20 3f 20 4f 50 5f 50 72 65 76 20   bRev ? OP_Prev 
28fef 3a 20 4f 50 5f 4e 65 78 74 3b 0a 20 20 20 20 20  : OP_Next;.     
28ff0 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 69 49   pLevel->p1 = iI
28ff1 64 78 43 75 72 3b 0a 20 20 20 20 20 20 64 69 73  dxCur;.      dis
28ff2 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c  ableTerm(pLevel,
28ff3 20 70 52 61 6e 67 65 53 74 61 72 74 29 3b 0a 20   pRangeStart);. 
28ff4 20 20 20 20 20 64 69 73 61 62 6c 65 54 65 72 6d       disableTerm
28ff5 28 70 4c 65 76 65 6c 2c 20 70 52 61 6e 67 65 45  (pLevel, pRangeE
28ff6 6e 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  nd);.    }else{.
28ff7 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20 34 3a        /* Case 4:
28ff8 20 20 54 68 65 72 65 20 69 73 20 6e 6f 20 75 73    There is no us
28ff9 61 62 6c 65 20 69 6e 64 65 78 2e 20 20 57 65 20  able index.  We 
28ffa 6d 75 73 74 20 64 6f 20 61 20 63 6f 6d 70 6c 65  must do a comple
28ffb 74 65 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20  te.      **     
28ffc 20 20 20 20 20 73 63 61 6e 20 6f 66 20 74 68 65       scan of the
28ffd 20 65 6e 74 69 72 65 20 74 61 62 6c 65 2e 0a 20   entire table.. 
28ffe 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73       */.      as
28fff 73 65 72 74 28 20 6f 6d 69 74 54 61 62 6c 65 3d  sert( omitTable=
29000 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  =0 );.      asse
29001 72 74 28 20 62 52 65 76 3d 3d 30 20 29 3b 0a 20  rt( bRev==0 );. 
29002 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20       pLevel->op 
29003 3d 20 4f 50 5f 4e 65 78 74 3b 0a 20 20 20 20 20  = OP_Next;.     
29004 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 69 43   pLevel->p1 = iC
29005 75 72 3b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c  ur;.      pLevel
29006 2d 3e 70 32 20 3d 20 31 20 2b 20 73 71 6c 69 74  ->p2 = 1 + sqlit
29007 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
29008 4f 50 5f 52 65 77 69 6e 64 2c 20 69 43 75 72 2c  OP_Rewind, iCur,
29009 20 62 72 6b 29 3b 0a 20 20 20 20 20 20 70 4c 65   brk);.      pLe
2900a 76 65 6c 2d 3e 70 35 20 3d 20 53 51 4c 49 54 45  vel->p5 = SQLITE
2900b 5f 53 54 4d 54 53 54 41 54 55 53 5f 46 55 4c 4c  _STMTSTATUS_FULL
2900c 53 43 41 4e 5f 53 54 45 50 3b 0a 20 20 20 20 7d  SCAN_STEP;.    }
2900d 0a 20 20 20 20 6e 6f 74 52 65 61 64 79 20 26 3d  .    notReady &=
2900e 20 7e 67 65 74 4d 61 73 6b 28 26 6d 61 73 6b 53   ~getMask(&maskS
2900f 65 74 2c 20 69 43 75 72 29 3b 0a 0a 20 20 20 20  et, iCur);..    
29010 2f 2a 20 49 6e 73 65 72 74 20 63 6f 64 65 20 74  /* Insert code t
29011 6f 20 74 65 73 74 20 65 76 65 72 79 20 73 75 62  o test every sub
29012 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20  expression that 
29013 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c  can be completel
29014 79 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 75 74 65  y.    ** compute
29015 64 20 75 73 69 6e 67 20 74 68 65 20 63 75 72 72  d using the curr
29016 65 6e 74 20 73 65 74 20 6f 66 20 74 61 62 6c 65  ent set of table
29017 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6b 20  s..    */.    k 
29018 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 70 54 65  = 0;.    for(pTe
29019 72 6d 3d 77 63 2e 61 2c 20 6a 3d 77 63 2e 6e 54  rm=wc.a, j=wc.nT
2901a 65 72 6d 3b 20 6a 3e 30 3b 20 6a 2d 2d 2c 20 70  erm; j>0; j--, p
2901b 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 45  Term++){.      E
2901c 78 70 72 20 2a 70 45 3b 0a 20 20 20 20 20 20 74  xpr *pE;.      t
2901d 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e  estcase( pTerm->
2901e 66 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 49 52  flags & TERM_VIR
2901f 54 55 41 4c 20 29 3b 0a 20 20 20 20 20 20 74 65  TUAL );.      te
29020 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 66  stcase( pTerm->f
29021 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 44 45  lags & TERM_CODE
29022 44 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  D );.      if( p
29023 54 65 72 6d 2d 3e 66 6c 61 67 73 20 26 20 28 54  Term->flags & (T
29024 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d  ERM_VIRTUAL|TERM
29025 5f 43 4f 44 45 44 29 20 29 20 63 6f 6e 74 69 6e  _CODED) ) contin
29026 75 65 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70  ue;.      if( (p
29027 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 20  Term->prereqAll 
29028 26 20 6e 6f 74 52 65 61 64 79 29 21 3d 30 20 29  & notReady)!=0 )
29029 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
2902a 20 70 45 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78   pE = pTerm->pEx
2902b 70 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  pr;.      assert
2902c 28 20 70 45 21 3d 30 20 29 3b 0a 20 20 20 20 20  ( pE!=0 );.     
2902d 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 69 4c 65   if( pLevel->iLe
2902e 66 74 4a 6f 69 6e 20 26 26 20 21 45 78 70 72 48  ftJoin && !ExprH
2902f 61 73 50 72 6f 70 65 72 74 79 28 70 45 2c 20 45  asProperty(pE, E
29030 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20  P_FromJoin) ){. 
29031 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
29032 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
29033 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f  Parse->disableCo
29034 6c 43 61 63 68 65 20 2b 3d 20 6b 3b 0a 20 20 20  lCache += k;.   
29035 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66     sqlite3ExprIf
29036 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45  False(pParse, pE
29037 2c 20 63 6f 6e 74 2c 20 53 51 4c 49 54 45 5f 4a  , cont, SQLITE_J
29038 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20  UMPIFNULL);.    
29039 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c    pParse->disabl
2903a 65 43 6f 6c 43 61 63 68 65 20 2d 3d 20 6b 3b 0a  eColCache -= k;.
2903b 20 20 20 20 20 20 6b 20 3d 20 31 3b 0a 20 20 20        k = 1;.   
2903c 20 20 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20     pTerm->flags 
2903d 7c 3d 20 54 45 52 4d 5f 43 4f 44 45 44 3b 0a 20  |= TERM_CODED;. 
2903e 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 6f 72     }..    /* For
2903f 20 61 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f   a LEFT OUTER JO
29040 49 4e 2c 20 67 65 6e 65 72 61 74 65 20 63 6f 64  IN, generate cod
29041 65 20 74 68 61 74 20 77 69 6c 6c 20 72 65 63 6f  e that will reco
29042 72 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74  rd the fact that
29043 0a 20 20 20 20 2a 2a 20 61 74 20 6c 65 61 73 74  .    ** at least
29044 20 6f 6e 65 20 72 6f 77 20 6f 66 20 74 68 65 20   one row of the 
29045 72 69 67 68 74 20 74 61 62 6c 65 20 68 61 73 20  right table has 
29046 6d 61 74 63 68 65 64 20 74 68 65 20 6c 65 66 74  matched the left
29047 20 74 61 62 6c 65 2e 20 20 0a 20 20 20 20 2a 2f   table.  .    */
29048 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d  .    if( pLevel-
29049 3e 69 4c 65 66 74 4a 6f 69 6e 20 29 7b 0a 20 20  >iLeftJoin ){.  
2904a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 74 6f 70 20      pLevel->top 
2904b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
2904c 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20  rentAddr(v);.   
2904d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2904e 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
2904f 65 72 2c 20 31 2c 20 70 4c 65 76 65 6c 2d 3e 69  er, 1, pLevel->i
29050 4c 65 66 74 4a 6f 69 6e 29 3b 0a 20 20 20 20 20  LeftJoin);.     
29051 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
29052 20 22 72 65 63 6f 72 64 20 4c 45 46 54 20 4a 4f   "record LEFT JO
29053 49 4e 20 68 69 74 22 29 29 3b 0a 20 20 20 20 20  IN hit"));.     
29054 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61   sqlite3ExprClea
29055 72 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50 61  rColumnCache(pPa
29056 72 73 65 2c 20 70 4c 65 76 65 6c 2d 3e 69 54 61  rse, pLevel->iTa
29057 62 43 75 72 29 3b 0a 20 20 20 20 20 20 73 71 6c  bCur);.      sql
29058 69 74 65 33 45 78 70 72 43 6c 65 61 72 43 6f 6c  ite3ExprClearCol
29059 75 6d 6e 43 61 63 68 65 28 70 50 61 72 73 65 2c  umnCache(pParse,
2905a 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72   pLevel->iIdxCur
2905b 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 54 65  );.      for(pTe
2905c 72 6d 3d 77 63 2e 61 2c 20 6a 3d 30 3b 20 6a 3c  rm=wc.a, j=0; j<
2905d 77 63 2e 6e 54 65 72 6d 3b 20 6a 2b 2b 2c 20 70  wc.nTerm; j++, p
2905e 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Term++){.       
2905f 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d   testcase( pTerm
29060 2d 3e 66 6c 61 67 73 20 26 20 54 45 52 4d 5f 56  ->flags & TERM_V
29061 49 52 54 55 41 4c 20 29 3b 0a 20 20 20 20 20 20  IRTUAL );.      
29062 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72    testcase( pTer
29063 6d 2d 3e 66 6c 61 67 73 20 26 20 54 45 52 4d 5f  m->flags & TERM_
29064 43 4f 44 45 44 20 29 3b 0a 20 20 20 20 20 20 20  CODED );.       
29065 20 69 66 28 20 70 54 65 72 6d 2d 3e 66 6c 61 67   if( pTerm->flag
29066 73 20 26 20 28 54 45 52 4d 5f 56 49 52 54 55 41  s & (TERM_VIRTUA
29067 4c 7c 54 45 52 4d 5f 43 4f 44 45 44 29 20 29 20  L|TERM_CODED) ) 
29068 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
29069 20 20 69 66 28 20 28 70 54 65 72 6d 2d 3e 70 72    if( (pTerm->pr
2906a 65 72 65 71 41 6c 6c 20 26 20 6e 6f 74 52 65 61  ereqAll & notRea
2906b 64 79 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e 75  dy)!=0 ) continu
2906c 65 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  e;.        asser
2906d 74 28 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 20  t( pTerm->pExpr 
2906e 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2906f 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50  e3ExprIfFalse(pP
29070 61 72 73 65 2c 20 70 54 65 72 6d 2d 3e 70 45 78  arse, pTerm->pEx
29071 70 72 2c 20 63 6f 6e 74 2c 20 53 51 4c 49 54 45  pr, cont, SQLITE
29072 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20  _JUMPIFNULL);.  
29073 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 66 6c 61        pTerm->fla
29074 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f 44 45 44  gs |= TERM_CODED
29075 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
29076 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49    }..#ifdef SQLI
29077 54 45 5f 54 45 53 54 20 20 2f 2a 20 46 6f 72 20  TE_TEST  /* For 
29078 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75  testing and debu
29079 67 67 69 6e 67 20 75 73 65 20 6f 6e 6c 79 20 2a  gging use only *
2907a 2f 0a 20 20 2f 2a 20 52 65 63 6f 72 64 20 69 6e  /.  /* Record in
2907b 20 74 68 65 20 71 75 65 72 79 20 70 6c 61 6e 20   the query plan 
2907c 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
2907d 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 61  t the current ta
2907e 62 6c 65 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65  ble.  ** and the
2907f 20 69 6e 64 65 78 20 75 73 65 64 20 74 6f 20 61   index used to a
29080 63 63 65 73 73 20 69 74 20 28 69 66 20 61 6e 79  ccess it (if any
29081 29 2e 20 20 49 66 20 74 68 65 20 74 61 62 6c 65  ).  If the table
29082 20 69 74 73 65 6c 66 0a 20 20 2a 2a 20 69 73 20   itself.  ** is 
29083 6e 6f 74 20 75 73 65 64 2c 20 69 74 73 20 6e 61  not used, its na
29084 6d 65 20 69 73 20 6a 75 73 74 20 27 7b 7d 27 2e  me is just '{}'.
29085 20 20 49 66 20 6e 6f 20 69 6e 64 65 78 20 69 73    If no index is
29086 20 75 73 65 64 0a 20 20 2a 2a 20 74 68 65 20 69   used.  ** the i
29087 6e 64 65 78 20 69 73 20 6c 69 73 74 65 64 20 61  ndex is listed a
29088 73 20 22 7b 7d 22 2e 20 20 49 66 20 74 68 65 20  s "{}".  If the 
29089 70 72 69 6d 61 72 79 20 6b 65 79 20 69 73 20 75  primary key is u
2908a 73 65 64 20 74 68 65 0a 20 20 2a 2a 20 69 6e 64  sed the.  ** ind
2908b 65 78 20 6e 61 6d 65 20 69 73 20 27 2a 27 2e 0a  ex name is '*'..
2908c 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20    */.  for(i=0; 
2908d 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  i<pTabList->nSrc
2908e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72  ; i++){.    char
2908f 20 2a 7a 3b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a   *z;.    int n;.
29090 20 20 20 20 70 4c 65 76 65 6c 20 3d 20 26 70 57      pLevel = &pW
29091 49 6e 66 6f 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20  Info->a[i];.    
29092 70 54 61 62 49 74 65 6d 20 3d 20 26 70 54 61 62  pTabItem = &pTab
29093 4c 69 73 74 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e  List->a[pLevel->
29094 69 46 72 6f 6d 5d 3b 0a 20 20 20 20 7a 20 3d 20  iFrom];.    z = 
29095 70 54 61 62 49 74 65 6d 2d 3e 7a 41 6c 69 61 73  pTabItem->zAlias
29096 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29  ;.    if( z==0 )
29097 20 7a 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 70   z = pTabItem->p
29098 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20  Tab->zName;.    
29099 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20  n = strlen(z);. 
2909a 20 20 20 69 66 28 20 6e 2b 6e 51 50 6c 61 6e 20     if( n+nQPlan 
2909b 3c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33  < sizeof(sqlite3
2909c 5f 71 75 65 72 79 5f 70 6c 61 6e 29 2d 31 30 20  _query_plan)-10 
2909d 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65  ){.      if( pLe
2909e 76 65 6c 2d 3e 66 6c 61 67 73 20 26 20 57 48 45  vel->flags & WHE
2909f 52 45 5f 49 44 58 5f 4f 4e 4c 59 20 29 7b 0a 20  RE_IDX_ONLY ){. 
290a0 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 73         memcpy(&s
290a1 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61  qlite3_query_pla
290a2 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 22 7b 7d 22 2c  n[nQPlan], "{}",
290a3 20 32 29 3b 0a 20 20 20 20 20 20 20 20 6e 51 50   2);.        nQP
290a4 6c 61 6e 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20  lan += 2;.      
290a5 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6d  }else{.        m
290a6 65 6d 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71  emcpy(&sqlite3_q
290a7 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e  uery_plan[nQPlan
290a8 5d 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20  ], z, n);.      
290a9 20 20 6e 51 50 6c 61 6e 20 2b 3d 20 6e 3b 0a 20    nQPlan += n;. 
290aa 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
290ab 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b  ite3_query_plan[
290ac 6e 51 50 6c 61 6e 2b 2b 5d 20 3d 20 27 20 27 3b  nQPlan++] = ' ';
290ad 0a 20 20 20 20 7d 0a 20 20 20 20 74 65 73 74 63  .    }.    testc
290ae 61 73 65 28 20 70 4c 65 76 65 6c 2d 3e 66 6c 61  ase( pLevel->fla
290af 67 73 20 26 20 57 48 45 52 45 5f 52 4f 57 49 44  gs & WHERE_ROWID
290b0 5f 45 51 20 29 3b 0a 20 20 20 20 74 65 73 74 63  _EQ );.    testc
290b1 61 73 65 28 20 70 4c 65 76 65 6c 2d 3e 66 6c 61  ase( pLevel->fla
290b2 67 73 20 26 20 57 48 45 52 45 5f 52 4f 57 49 44  gs & WHERE_ROWID
290b3 5f 52 41 4e 47 45 20 29 3b 0a 20 20 20 20 69 66  _RANGE );.    if
290b4 28 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20  ( pLevel->flags 
290b5 26 20 28 57 48 45 52 45 5f 52 4f 57 49 44 5f 45  & (WHERE_ROWID_E
290b6 51 7c 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41  Q|WHERE_ROWID_RA
290b7 4e 47 45 29 20 29 7b 0a 20 20 20 20 20 20 6d 65  NGE) ){.      me
290b8 6d 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71 75  mcpy(&sqlite3_qu
290b9 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d  ery_plan[nQPlan]
290ba 2c 20 22 2a 20 22 2c 20 32 29 3b 0a 20 20 20 20  , "* ", 2);.    
290bb 20 20 6e 51 50 6c 61 6e 20 2b 3d 20 32 3b 0a 20    nQPlan += 2;. 
290bc 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 65     }else if( pLe
290bd 76 65 6c 2d 3e 70 49 64 78 3d 3d 30 20 29 7b 0a  vel->pIdx==0 ){.
290be 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 73 71        memcpy(&sq
290bf 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e  lite3_query_plan
290c0 5b 6e 51 50 6c 61 6e 5d 2c 20 22 7b 7d 20 22 2c  [nQPlan], "{} ",
290c1 20 33 29 3b 0a 20 20 20 20 20 20 6e 51 50 6c 61   3);.      nQPla
290c2 6e 20 2b 3d 20 33 3b 0a 20 20 20 20 7d 65 6c 73  n += 3;.    }els
290c3 65 7b 0a 20 20 20 20 20 20 6e 20 3d 20 73 74 72  e{.      n = str
290c4 6c 65 6e 28 70 4c 65 76 65 6c 2d 3e 70 49 64 78  len(pLevel->pIdx
290c5 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ->zName);.      
290c6 69 66 28 20 6e 2b 6e 51 50 6c 61 6e 20 3c 20 73  if( n+nQPlan < s
290c7 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 71 75  izeof(sqlite3_qu
290c8 65 72 79 5f 70 6c 61 6e 29 2d 32 20 29 7b 0a 20  ery_plan)-2 ){. 
290c9 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 73         memcpy(&s
290ca 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61  qlite3_query_pla
290cb 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 70 4c 65 76 65  n[nQPlan], pLeve
290cc 6c 2d 3e 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20  l->pIdx->zName, 
290cd 6e 29 3b 0a 20 20 20 20 20 20 20 20 6e 51 50 6c  n);.        nQPl
290ce 61 6e 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 20  an += n;.       
290cf 20 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70   sqlite3_query_p
290d0 6c 61 6e 5b 6e 51 50 6c 61 6e 2b 2b 5d 20 3d 20  lan[nQPlan++] = 
290d1 27 20 27 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ' ';.      }.   
290d2 20 7d 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20   }.  }.  while( 
290d3 6e 51 50 6c 61 6e 3e 30 20 26 26 20 73 71 6c 69  nQPlan>0 && sqli
290d4 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e  te3_query_plan[n
290d5 51 50 6c 61 6e 2d 31 5d 3d 3d 27 20 27 20 29 7b  QPlan-1]==' ' ){
290d6 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 71 75 65  .    sqlite3_que
290d7 72 79 5f 70 6c 61 6e 5b 2d 2d 6e 51 50 6c 61 6e  ry_plan[--nQPlan
290d8 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c  ] = 0;.  }.  sql
290d9 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b  ite3_query_plan[
290da 6e 51 50 6c 61 6e 5d 20 3d 20 30 3b 0a 20 20 6e  nQPlan] = 0;.  n
290db 51 50 6c 61 6e 20 3d 20 30 3b 0a 23 65 6e 64 69  QPlan = 0;.#endi
290dc 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54  f /* SQLITE_TEST
290dd 20 2f 2f 20 54 65 73 74 69 6e 67 20 61 6e 64 20   // Testing and 
290de 64 65 62 75 67 67 69 6e 67 20 75 73 65 20 6f 6e  debugging use on
290df 6c 79 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 63 6f  ly */..  /* Reco
290e0 72 64 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 74  rd the continuat
290e1 69 6f 6e 20 61 64 64 72 65 73 73 20 69 6e 20 74  ion address in t
290e2 68 65 20 57 68 65 72 65 49 6e 66 6f 20 73 74 72  he WhereInfo str
290e3 75 63 74 75 72 65 2e 20 20 54 68 65 6e 0a 20 20  ucture.  Then.  
290e4 2a 2a 20 63 6c 65 61 6e 20 75 70 20 61 6e 64 20  ** clean up and 
290e5 72 65 74 75 72 6e 2e 0a 20 20 2a 2f 0a 20 20 70  return..  */.  p
290e6 57 49 6e 66 6f 2d 3e 69 43 6f 6e 74 69 6e 75 65  WInfo->iContinue
290e7 20 3d 20 63 6f 6e 74 3b 0a 20 20 77 68 65 72 65   = cont;.  where
290e8 43 6c 61 75 73 65 43 6c 65 61 72 28 26 77 63 29  ClauseClear(&wc)
290e9 3b 0a 20 20 72 65 74 75 72 6e 20 70 57 49 6e 66  ;.  return pWInf
290ea 6f 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65  o;..  /* Jump he
290eb 72 65 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69  re if malloc fai
290ec 6c 73 20 2a 2f 0a 77 68 65 72 65 42 65 67 69 6e  ls */.whereBegin
290ed 45 72 72 6f 72 3a 0a 20 20 77 68 65 72 65 43 6c  Error:.  whereCl
290ee 61 75 73 65 43 6c 65 61 72 28 26 77 63 29 3b 0a  auseClear(&wc);.
290ef 20 20 77 68 65 72 65 49 6e 66 6f 46 72 65 65 28    whereInfoFree(
290f0 64 62 2c 20 70 57 49 6e 66 6f 29 3b 0a 20 20 72  db, pWInfo);.  r
290f1 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
290f2 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 65  * Generate the e
290f3 6e 64 20 6f 66 20 74 68 65 20 57 48 45 52 45 20  nd of the WHERE 
290f4 6c 6f 6f 70 2e 20 20 53 65 65 20 63 6f 6d 6d 65  loop.  See comme
290f5 6e 74 73 20 6f 6e 20 0a 2a 2a 20 73 71 6c 69 74  nts on .** sqlit
290f6 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 20 66  e3WhereBegin() f
290f7 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
290f8 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51  formation..*/.SQ
290f9 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
290fa 64 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e  d sqlite3WhereEn
290fb 64 28 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49  d(WhereInfo *pWI
290fc 6e 66 6f 29 7b 0a 20 20 50 61 72 73 65 20 2a 70  nfo){.  Parse *p
290fd 50 61 72 73 65 20 3d 20 70 57 49 6e 66 6f 2d 3e  Parse = pWInfo->
290fe 70 50 61 72 73 65 3b 0a 20 20 56 64 62 65 20 2a  pParse;.  Vdbe *
290ff 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
29100 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 57 68  e;.  int i;.  Wh
29101 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c  ereLevel *pLevel
29102 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61  ;.  SrcList *pTa
29103 62 4c 69 73 74 20 3d 20 70 57 49 6e 66 6f 2d 3e  bList = pWInfo->
29104 70 54 61 62 4c 69 73 74 3b 0a 20 20 73 71 6c 69  pTabList;.  sqli
29105 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
29106 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65  ->db;..  /* Gene
29107 72 61 74 65 20 6c 6f 6f 70 20 74 65 72 6d 69 6e  rate loop termin
29108 61 74 69 6f 6e 20 63 6f 64 65 2e 0a 20 20 2a 2f  ation code..  */
29109 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c  .  sqlite3ExprCl
2910a 65 61 72 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70  earColumnCache(p
2910b 50 61 72 73 65 2c 20 2d 31 29 3b 0a 20 20 66 6f  Parse, -1);.  fo
2910c 72 28 69 3d 70 54 61 62 4c 69 73 74 2d 3e 6e 53  r(i=pTabList->nS
2910d 72 63 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29  rc-1; i>=0; i--)
2910e 7b 0a 20 20 20 20 70 4c 65 76 65 6c 20 3d 20 26  {.    pLevel = &
2910f 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d 3b 0a 20 20  pWInfo->a[i];.  
29110 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
29111 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 70 4c 65  olveLabel(v, pLe
29112 76 65 6c 2d 3e 63 6f 6e 74 29 3b 0a 20 20 20 20  vel->cont);.    
29113 69 66 28 20 70 4c 65 76 65 6c 2d 3e 6f 70 21 3d  if( pLevel->op!=
29114 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20 20 20 20 20  OP_Noop ){.     
29115 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29116 70 32 28 76 2c 20 70 4c 65 76 65 6c 2d 3e 6f 70  p2(v, pLevel->op
29117 2c 20 70 4c 65 76 65 6c 2d 3e 70 31 2c 20 70 4c  , pLevel->p1, pL
29118 65 76 65 6c 2d 3e 70 32 29 3b 0a 20 20 20 20 20  evel->p2);.     
29119 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
2911a 67 65 50 35 28 76 2c 20 70 4c 65 76 65 6c 2d 3e  geP5(v, pLevel->
2911b 70 35 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  p5);.    }.    i
2911c 66 28 20 70 4c 65 76 65 6c 2d 3e 6e 49 6e 20 29  f( pLevel->nIn )
2911d 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 49  {.      struct I
2911e 6e 4c 6f 6f 70 20 2a 70 49 6e 3b 0a 20 20 20 20  nLoop *pIn;.    
2911f 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 73    int j;.      s
29120 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
29121 65 4c 61 62 65 6c 28 76 2c 20 70 4c 65 76 65 6c  eLabel(v, pLevel
29122 2d 3e 6e 78 74 29 3b 0a 20 20 20 20 20 20 66 6f  ->nxt);.      fo
29123 72 28 6a 3d 70 4c 65 76 65 6c 2d 3e 6e 49 6e 2c  r(j=pLevel->nIn,
29124 20 70 49 6e 3d 26 70 4c 65 76 65 6c 2d 3e 61 49   pIn=&pLevel->aI
29125 6e 4c 6f 6f 70 5b 6a 2d 31 5d 3b 20 6a 3e 30 3b  nLoop[j-1]; j>0;
29126 20 6a 2d 2d 2c 20 70 49 6e 2d 2d 29 7b 0a 20 20   j--, pIn--){.  
29127 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29128 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 70 49 6e  eJumpHere(v, pIn
29129 2d 3e 74 6f 70 41 64 64 72 2b 31 29 3b 0a 20 20  ->topAddr+1);.  
2912a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2912b 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
2912c 78 74 2c 20 70 49 6e 2d 3e 69 43 75 72 2c 20 70  xt, pIn->iCur, p
2912d 49 6e 2d 3e 74 6f 70 41 64 64 72 29 3b 0a 20 20  In->topAddr);.  
2912e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2912f 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 70 49 6e  eJumpHere(v, pIn
29130 2d 3e 74 6f 70 41 64 64 72 2d 31 29 3b 0a 20 20  ->topAddr-1);.  
29131 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
29132 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c  te3DbFree(db, pL
29133 65 76 65 6c 2d 3e 61 49 6e 4c 6f 6f 70 29 3b 0a  evel->aInLoop);.
29134 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
29135 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
29136 6c 28 76 2c 20 70 4c 65 76 65 6c 2d 3e 62 72 6b  l(v, pLevel->brk
29137 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65  );.    if( pLeve
29138 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20 29 7b 0a  l->iLeftJoin ){.
29139 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a        int addr;.
2913a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
2913b 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2913c 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 70 4c 65 76  , OP_IfPos, pLev
2913d 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 29 3b 0a  el->iLeftJoin);.
2913e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2913f 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 75  eAddOp1(v, OP_Nu
29140 6c 6c 52 6f 77 2c 20 70 54 61 62 4c 69 73 74 2d  llRow, pTabList-
29141 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 29 3b 0a  >a[i].iCursor);.
29142 20 20 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c        if( pLevel
29143 2d 3e 69 49 64 78 43 75 72 3e 3d 30 20 29 7b 0a  ->iIdxCur>=0 ){.
29144 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29145 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
29146 4e 75 6c 6c 52 6f 77 2c 20 70 4c 65 76 65 6c 2d  NullRow, pLevel-
29147 3e 69 49 64 78 43 75 72 29 3b 0a 20 20 20 20 20  >iIdxCur);.     
29148 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
29149 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2914a 5f 47 6f 74 6f 2c 20 30 2c 20 70 4c 65 76 65 6c  _Goto, 0, pLevel
2914b 2d 3e 74 6f 70 29 3b 0a 20 20 20 20 20 20 73 71  ->top);.      sq
2914c 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
2914d 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 20  e(v, addr);.    
2914e 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20  }.  }..  /* The 
2914f 22 62 72 65 61 6b 22 20 70 6f 69 6e 74 20 69 73  "break" point is
29150 20 68 65 72 65 2c 20 6a 75 73 74 20 70 61 73 74   here, just past
29151 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
29152 6f 75 74 65 72 20 6c 6f 6f 70 2e 0a 20 20 2a 2a  outer loop..  **
29153 20 53 65 74 20 69 74 2e 0a 20 20 2a 2f 0a 20 20   Set it..  */.  
29154 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
29155 76 65 4c 61 62 65 6c 28 76 2c 20 70 57 49 6e 66  veLabel(v, pWInf
29156 6f 2d 3e 69 42 72 65 61 6b 29 3b 0a 0a 20 20 2f  o->iBreak);..  /
29157 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 6f 66 20 74  * Close all of t
29158 68 65 20 63 75 72 73 6f 72 73 20 74 68 61 74 20  he cursors that 
29159 77 65 72 65 20 6f 70 65 6e 65 64 20 62 79 20 73  were opened by s
2915a 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
2915b 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  ..  */.  for(i=0
2915c 2c 20 70 4c 65 76 65 6c 3d 70 57 49 6e 66 6f 2d  , pLevel=pWInfo-
2915d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e  >a; i<pTabList->
2915e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 4c 65 76 65  nSrc; i++, pLeve
2915f 6c 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74  l++){.    struct
29160 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
29161 54 61 62 49 74 65 6d 20 3d 20 26 70 54 61 62 4c  TabItem = &pTabL
29162 69 73 74 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69  ist->a[pLevel->i
29163 46 72 6f 6d 5d 3b 0a 20 20 20 20 54 61 62 6c 65  From];.    Table
29164 20 2a 70 54 61 62 20 3d 20 70 54 61 62 49 74 65   *pTab = pTabIte
29165 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 61 73 73  m->pTab;.    ass
29166 65 72 74 28 20 70 54 61 62 21 3d 30 20 29 3b 0a  ert( pTab!=0 );.
29167 20 20 20 20 69 66 28 20 28 70 54 61 62 2d 3e 74      if( (pTab->t
29168 61 62 46 6c 61 67 73 20 26 20 54 46 5f 45 70 68  abFlags & TF_Eph
29169 65 6d 65 72 61 6c 29 21 3d 30 20 7c 7c 20 70 54  emeral)!=0 || pT
2916a 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 20 63 6f  ab->pSelect ) co
2916b 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20  ntinue;.    if( 
2916c 21 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61  !pWInfo->okOnePa
2916d 73 73 20 26 26 20 28 70 4c 65 76 65 6c 2d 3e 66  ss && (pLevel->f
2916e 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 44 58  lags & WHERE_IDX
2916f 5f 4f 4e 4c 59 29 3d 3d 30 20 29 7b 0a 20 20 20  _ONLY)==0 ){.   
29170 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29171 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp1(v, OP_Close
29172 2c 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75 72  , pTabItem->iCur
29173 73 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  sor);.    }.    
29174 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 49 64 78  if( pLevel->pIdx
29175 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  !=0 ){.      sql
29176 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
29177 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 70 4c 65 76  , OP_Close, pLev
29178 65 6c 2d 3e 69 49 64 78 43 75 72 29 3b 0a 20 20  el->iIdxCur);.  
29179 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74    }..    /* If t
2917a 68 69 73 20 73 63 61 6e 20 75 73 65 73 20 61 6e  his scan uses an
2917b 20 69 6e 64 65 78 2c 20 6d 61 6b 65 20 63 6f 64   index, make cod
2917c 65 20 73 75 62 73 74 69 74 75 74 69 6f 6e 73 20  e substitutions 
2917d 74 6f 20 72 65 61 64 20 64 61 74 61 0a 20 20 20  to read data.   
2917e 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 69 6e 64   ** from the ind
2917f 65 78 20 69 6e 20 70 72 65 66 65 72 65 6e 63 65  ex in preference
29180 20 74 6f 20 74 68 65 20 74 61 62 6c 65 2e 20 53   to the table. S
29181 6f 6d 65 74 69 6d 65 73 2c 20 74 68 69 73 20 6d  ometimes, this m
29182 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68 65 20  eans.    ** the 
29183 74 61 62 6c 65 20 6e 65 65 64 20 6e 65 76 65 72  table need never
29184 20 62 65 20 72 65 61 64 20 66 72 6f 6d 2e 20 54   be read from. T
29185 68 69 73 20 69 73 20 61 20 70 65 72 66 6f 72 6d  his is a perform
29186 61 6e 63 65 20 62 6f 6f 73 74 2c 0a 20 20 20 20  ance boost,.    
29187 2a 2a 20 61 73 20 74 68 65 20 76 64 62 65 20 6c  ** as the vdbe l
29188 65 76 65 6c 20 77 61 69 74 73 20 75 6e 74 69 6c  evel waits until
29189 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 72 65   the table is re
2918a 61 64 20 62 65 66 6f 72 65 20 61 63 74 75 61 6c  ad before actual
2918b 6c 79 0a 20 20 20 20 2a 2a 20 73 65 65 6b 69 6e  ly.    ** seekin
2918c 67 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 73  g the table curs
2918d 6f 72 20 74 6f 20 74 68 65 20 72 65 63 6f 72 64  or to the record
2918e 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
2918f 6f 20 74 68 65 20 63 75 72 72 65 6e 74 0a 20 20  o the current.  
29190 20 20 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e    ** position in
29191 20 74 68 65 20 69 6e 64 65 78 2e 0a 20 20 20 20   the index..    
29192 2a 2a 20 0a 20 20 20 20 2a 2a 20 43 61 6c 6c 73  ** .    ** Calls
29193 20 74 6f 20 74 68 65 20 63 6f 64 65 20 67 65 6e   to the code gen
29194 65 72 61 74 6f 72 20 69 6e 20 62 65 74 77 65 65  erator in betwee
29195 6e 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  n sqlite3WhereBe
29196 67 69 6e 20 61 6e 64 0a 20 20 20 20 2a 2a 20 73  gin and.    ** s
29197 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 20 77  qlite3WhereEnd w
29198 69 6c 6c 20 68 61 76 65 20 63 72 65 61 74 65 64  ill have created
29199 20 63 6f 64 65 20 74 68 61 74 20 72 65 66 65 72   code that refer
2919a 65 6e 63 65 73 20 74 68 65 20 74 61 62 6c 65 0a  ences the table.
2919b 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79 2e      ** directly.
2919c 20 20 54 68 69 73 20 6c 6f 6f 70 20 73 63 61 6e    This loop scan
2919d 73 20 61 6c 6c 20 74 68 61 74 20 63 6f 64 65 20  s all that code 
2919e 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6f 70 63 6f  looking for opco
2919f 64 65 73 0a 20 20 20 20 2a 2a 20 74 68 61 74 20  des.    ** that 
291a0 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 74 61  reference the ta
291a1 62 6c 65 20 61 6e 64 20 63 6f 6e 76 65 72 74 73  ble and converts
291a2 20 74 68 65 6d 20 69 6e 74 6f 20 6f 70 63 6f 64   them into opcod
291a3 65 73 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 72  es that.    ** r
291a4 65 66 65 72 65 6e 63 65 20 74 68 65 20 69 6e 64  eference the ind
291a5 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ex..    */.    i
291a6 66 28 20 70 4c 65 76 65 6c 2d 3e 70 49 64 78 20  f( pLevel->pIdx 
291a7 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6b 2c 20  ){.      int k, 
291a8 6a 2c 20 6c 61 73 74 3b 0a 20 20 20 20 20 20 56  j, last;.      V
291a9 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20  dbeOp *pOp;.    
291aa 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20    Index *pIdx = 
291ab 70 4c 65 76 65 6c 2d 3e 70 49 64 78 3b 0a 20 20  pLevel->pIdx;.  
291ac 20 20 20 20 69 6e 74 20 75 73 65 49 6e 64 65 78      int useIndex
291ad 4f 6e 6c 79 20 3d 20 70 4c 65 76 65 6c 2d 3e 66  Only = pLevel->f
291ae 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 44 58  lags & WHERE_IDX
291af 5f 4f 4e 4c 59 3b 0a 0a 20 20 20 20 20 20 61 73  _ONLY;..      as
291b0 73 65 72 74 28 20 70 49 64 78 21 3d 30 20 29 3b  sert( pIdx!=0 );
291b1 0a 20 20 20 20 20 20 70 4f 70 20 3d 20 73 71 6c  .      pOp = sql
291b2 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c  ite3VdbeGetOp(v,
291b3 20 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 29 3b 0a   pWInfo->iTop);.
291b4 20 20 20 20 20 20 6c 61 73 74 20 3d 20 73 71 6c        last = sql
291b5 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
291b6 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 66 6f  ddr(v);.      fo
291b7 72 28 6b 3d 70 57 49 6e 66 6f 2d 3e 69 54 6f 70  r(k=pWInfo->iTop
291b8 3b 20 6b 3c 6c 61 73 74 3b 20 6b 2b 2b 2c 20 70  ; k<last; k++, p
291b9 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69  Op++){.        i
291ba 66 28 20 70 4f 70 2d 3e 70 31 21 3d 70 4c 65 76  f( pOp->p1!=pLev
291bb 65 6c 2d 3e 69 54 61 62 43 75 72 20 29 20 63 6f  el->iTabCur ) co
291bc 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20  ntinue;.        
291bd 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
291be 3d 4f 50 5f 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20  =OP_Column ){.  
291bf 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b          for(j=0;
291c0 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e   j<pIdx->nColumn
291c1 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
291c2 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d      if( pOp->p2=
291c3 3d 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  =pIdx->aiColumn[
291c4 6a 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  j] ){.          
291c5 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20 6a 3b      pOp->p2 = j;
291c6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
291c7 4f 70 2d 3e 70 31 20 3d 20 70 4c 65 76 65 6c 2d  Op->p1 = pLevel-
291c8 3e 69 49 64 78 43 75 72 3b 0a 20 20 20 20 20 20  >iIdxCur;.      
291c9 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
291ca 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
291cb 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
291cc 20 20 20 61 73 73 65 72 74 28 21 75 73 65 49 6e     assert(!useIn
291cd 64 65 78 4f 6e 6c 79 20 7c 7c 20 6a 3c 70 49 64  dexOnly || j<pId
291ce 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20  x->nColumn);.   
291cf 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
291d0 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52  Op->opcode==OP_R
291d1 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20  owid ){.        
291d2 20 20 70 4f 70 2d 3e 70 31 20 3d 20 70 4c 65 76    pOp->p1 = pLev
291d3 65 6c 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20 20  el->iIdxCur;.   
291d4 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f         pOp->opco
291d5 64 65 20 3d 20 4f 50 5f 49 64 78 52 6f 77 69 64  de = OP_IdxRowid
291d6 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20  ;.        }else 
291d7 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
291d8 3d 4f 50 5f 4e 75 6c 6c 52 6f 77 20 26 26 20 75  =OP_NullRow && u
291d9 73 65 49 6e 64 65 78 4f 6e 6c 79 20 29 7b 0a 20  seIndexOnly ){. 
291da 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70           pOp->op
291db 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a  code = OP_Noop;.
291dc 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
291dd 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  }.    }.  }..  /
291de 2a 20 46 69 6e 61 6c 20 63 6c 65 61 6e 75 70 0a  * Final cleanup.
291df 20 20 2a 2f 0a 20 20 77 68 65 72 65 49 6e 66 6f    */.  whereInfo
291e0 46 72 65 65 28 64 62 2c 20 70 57 49 6e 66 6f 29  Free(db, pWInfo)
291e1 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f  ;.  return;.}../
291e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
291e3 6e 64 20 6f 66 20 77 68 65 72 65 2e 63 20 2a 2a  nd of where.c **
291e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
291e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
291e8 65 67 69 6e 20 66 69 6c 65 20 70 61 72 73 65 2e  egin file parse.
291e9 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
291ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
291ec 2a 20 44 72 69 76 65 72 20 74 65 6d 70 6c 61 74  * Driver templat
291ed 65 20 66 6f 72 20 74 68 65 20 4c 45 4d 4f 4e 20  e for the LEMON 
291ee 70 61 72 73 65 72 20 67 65 6e 65 72 61 74 6f 72  parser generator
291ef 2e 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  ..** The author 
291f0 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
291f1 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
291f2 63 65 20 63 6f 64 65 2e 0a 2a 2f 0a 2f 2a 20 46  ce code..*/./* F
291f3 69 72 73 74 20 6f 66 66 2c 20 63 6f 64 65 20 69  irst off, code i
291f4 73 20 69 6e 63 6c 75 64 65 64 20 74 68 61 74 20  s included that 
291f5 66 6f 6c 6c 6f 77 73 20 74 68 65 20 22 69 6e 63  follows the "inc
291f6 6c 75 64 65 22 20 64 65 63 6c 61 72 61 74 69 6f  lude" declaratio
291f7 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 69 6e 70 75  n.** in the inpu
291f8 74 20 67 72 61 6d 6d 61 72 20 66 69 6c 65 2e 20  t grammar file. 
291f9 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  */.../*.** An in
291fa 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
291fb 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69  tructure holds i
291fc 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
291fd 20 74 68 65 0a 2a 2a 20 4c 49 4d 49 54 20 63 6c   the.** LIMIT cl
291fe 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54  ause of a SELECT
291ff 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73   statement..*/.s
29200 74 72 75 63 74 20 4c 69 6d 69 74 56 61 6c 20 7b  truct LimitVal {
29201 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 3b  .  Expr *pLimit;
29202 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54      /* The LIMIT
29203 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 4e 55   expression.  NU
29204 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73 20 6e  LL if there is n
29205 6f 20 6c 69 6d 69 74 20 2a 2f 0a 20 20 45 78 70  o limit */.  Exp
29206 72 20 2a 70 4f 66 66 73 65 74 3b 20 20 20 2f 2a  r *pOffset;   /*
29207 20 54 68 65 20 4f 46 46 53 45 54 20 65 78 70 72   The OFFSET expr
29208 65 73 73 69 6f 6e 2e 20 20 4e 55 4c 4c 20 69 66  ession.  NULL if
29209 20 74 68 65 72 65 20 69 73 20 6e 6f 6e 65 20 2a   there is none *
2920a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  /.};../*.** An i
2920b 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
2920c 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65  structure is use
2920d 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 4c  d to store the L
2920e 49 4b 45 2c 0a 2a 2a 20 47 4c 4f 42 2c 20 4e 4f  IKE,.** GLOB, NO
2920f 54 20 4c 49 4b 45 2c 20 61 6e 64 20 4e 4f 54 20  T LIKE, and NOT 
29210 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 73 2e 0a  GLOB operators..
29211 2a 2f 0a 73 74 72 75 63 74 20 4c 69 6b 65 4f 70  */.struct LikeOp
29212 20 7b 0a 20 20 54 6f 6b 65 6e 20 65 4f 70 65 72   {.  Token eOper
29213 61 74 6f 72 3b 20 20 2f 2a 20 22 6c 69 6b 65 22  ator;  /* "like"
29214 20 6f 72 20 22 67 6c 6f 62 22 20 6f 72 20 22 72   or "glob" or "r
29215 65 67 65 78 70 22 20 2a 2f 0a 20 20 69 6e 74 20  egexp" */.  int 
29216 6e 6f 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  not;         /* 
29217 54 72 75 65 20 69 66 20 74 68 65 20 4e 4f 54 20  True if the NOT 
29218 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73 65  keyword is prese
29219 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  nt */.};../*.** 
2921a 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
2921b 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
2921c 75 63 74 75 72 65 20 64 65 73 63 72 69 62 65 73  ucture describes
2921d 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 0a   the event of a.
2921e 2a 2a 20 54 52 49 47 47 45 52 2e 20 20 22 61 22  ** TRIGGER.  "a"
2921f 20 69 73 20 74 68 65 20 65 76 65 6e 74 20 74 79   is the event ty
29220 70 65 2c 20 6f 6e 65 20 6f 66 20 54 4b 5f 55 50  pe, one of TK_UP
29221 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c  DATE, TK_INSERT,
29222 0a 2a 2a 20 54 4b 5f 44 45 4c 45 54 45 2c 20 6f  .** TK_DELETE, o
29223 72 20 54 4b 5f 49 4e 53 54 45 41 44 2e 20 20 49  r TK_INSTEAD.  I
29224 66 20 74 68 65 20 65 76 65 6e 74 20 69 73 20 6f  f the event is o
29225 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a  f the form.**.**
29226 20 20 20 20 20 20 55 50 44 41 54 45 20 4f 4e 20        UPDATE ON 
29227 28 61 2c 62 2c 63 29 0a 2a 2a 0a 2a 2a 20 54 68  (a,b,c).**.** Th
29228 65 6e 20 74 68 65 20 22 62 22 20 49 64 4c 69 73  en the "b" IdLis
29229 74 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c 69  t records the li
2922a 73 74 20 22 61 2c 62 2c 63 22 2e 0a 2a 2f 0a 73  st "a,b,c"..*/.s
2922b 74 72 75 63 74 20 54 72 69 67 45 76 65 6e 74 20  truct TrigEvent 
2922c 7b 20 69 6e 74 20 61 3b 20 49 64 4c 69 73 74 20  { int a; IdList 
2922d 2a 20 62 3b 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  * b; };../*.** A
2922e 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
2922f 69 73 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c  is structure hol
29230 64 73 20 74 68 65 20 41 54 54 41 43 48 20 6b 65  ds the ATTACH ke
29231 79 20 61 6e 64 20 74 68 65 20 6b 65 79 20 74 79  y and the key ty
29232 70 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 41 74  pe..*/.struct At
29233 74 61 63 68 4b 65 79 20 7b 20 69 6e 74 20 74 79  tachKey { int ty
29234 70 65 3b 20 20 54 6f 6b 65 6e 20 6b 65 79 3b 20  pe;  Token key; 
29235 7d 3b 0a 0a 2f 2a 20 4e 65 78 74 20 69 73 20 61  };../* Next is a
29236 6c 6c 20 74 6f 6b 65 6e 20 76 61 6c 75 65 73 2c  ll token values,
29237 20 69 6e 20 61 20 66 6f 72 6d 20 73 75 69 74 61   in a form suita
29238 62 6c 65 20 66 6f 72 20 75 73 65 20 62 79 20 6d  ble for use by m
29239 61 6b 65 68 65 61 64 65 72 73 2e 0a 2a 2a 20 54  akeheaders..** T
2923a 68 69 73 20 73 65 63 74 69 6f 6e 20 77 69 6c 6c  his section will
2923b 20 62 65 20 6e 75 6c 6c 20 75 6e 6c 65 73 73 20   be null unless 
2923c 6c 65 6d 6f 6e 20 69 73 20 72 75 6e 20 77 69 74  lemon is run wit
2923d 68 20 74 68 65 20 2d 6d 20 73 77 69 74 63 68 2e  h the -m switch.
2923e 0a 2a 2f 0a 2f 2a 20 0a 2a 2a 20 54 68 65 73 65  .*/./* .** These
2923f 20 63 6f 6e 73 74 61 6e 74 73 20 28 61 6c 6c 20   constants (all 
29240 67 65 6e 65 72 61 74 65 64 20 61 75 74 6f 6d 61  generated automa
29241 74 69 63 61 6c 6c 79 20 62 79 20 74 68 65 20 70  tically by the p
29242 61 72 73 65 72 20 67 65 6e 65 72 61 74 6f 72 29  arser generator)
29243 0a 2a 2a 20 73 70 65 63 69 66 79 20 74 68 65 20  .** specify the 
29244 76 61 72 69 6f 75 73 20 6b 69 6e 64 73 20 6f 66  various kinds of
29245 20 74 6f 6b 65 6e 73 20 28 74 65 72 6d 69 6e 61   tokens (termina
29246 6c 73 29 20 74 68 61 74 20 74 68 65 20 70 61 72  ls) that the par
29247 73 65 72 0a 2a 2a 20 75 6e 64 65 72 73 74 61 6e  ser.** understan
29248 64 73 2e 20 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20  ds. .**.** Each 
29249 73 79 6d 62 6f 6c 20 68 65 72 65 20 69 73 20 61  symbol here is a
2924a 20 74 65 72 6d 69 6e 61 6c 20 73 79 6d 62 6f 6c   terminal symbol
2924b 20 69 6e 20 74 68 65 20 67 72 61 6d 6d 61 72 2e   in the grammar.
2924c 0a 2a 2f 0a 2f 2a 20 4d 61 6b 65 20 73 75 72 65  .*/./* Make sure
2924d 20 74 68 65 20 49 4e 54 45 52 46 41 43 45 20 6d   the INTERFACE m
2924e 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 2e  acro is defined.
2924f 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e 54 45  .*/.#ifndef INTE
29250 52 46 41 43 45 0a 23 20 64 65 66 69 6e 65 20 49  RFACE.# define I
29251 4e 54 45 52 46 41 43 45 20 31 0a 23 65 6e 64 69  NTERFACE 1.#endi
29252 66 0a 2f 2a 20 54 68 65 20 6e 65 78 74 20 74 68  f./* The next th
29253 69 6e 67 20 69 6e 63 6c 75 64 65 64 20 69 73 20  ing included is 
29254 73 65 72 69 65 73 20 6f 66 20 64 65 66 69 6e 65  series of define
29255 73 20 77 68 69 63 68 20 63 6f 6e 74 72 6f 6c 0a  s which control.
29256 2a 2a 20 76 61 72 69 6f 75 73 20 61 73 70 65 63  ** various aspec
29257 74 73 20 6f 66 20 74 68 65 20 67 65 6e 65 72 61  ts of the genera
29258 74 65 64 20 70 61 72 73 65 72 2e 0a 2a 2a 20 20  ted parser..**  
29259 20 20 59 59 43 4f 44 45 54 59 50 45 20 20 20 20    YYCODETYPE    
2925a 20 20 20 20 20 69 73 20 74 68 65 20 64 61 74 61       is the data
2925b 20 74 79 70 65 20 75 73 65 64 20 66 6f 72 20 73   type used for s
2925c 74 6f 72 69 6e 67 20 74 65 72 6d 69 6e 61 6c 0a  toring terminal.
2925d 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2925e 20 20 20 20 20 20 20 20 20 61 6e 64 20 6e 6f 6e           and non
2925f 74 65 72 6d 69 6e 61 6c 20 6e 75 6d 62 65 72 73  terminal numbers
29260 2e 20 20 22 75 6e 73 69 67 6e 65 64 20 63 68 61  .  "unsigned cha
29261 72 22 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20  r" is.**        
29262 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75                 u
29263 73 65 64 20 69 66 20 74 68 65 72 65 20 61 72 65  sed if there are
29264 20 66 65 77 65 72 20 74 68 61 6e 20 32 35 30 20   fewer than 250 
29265 74 65 72 6d 69 6e 61 6c 73 0a 2a 2a 20 20 20 20  terminals.**    
29266 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29267 20 20 20 61 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e     and nontermin
29268 61 6c 73 2e 20 20 22 69 6e 74 22 20 69 73 20 75  als.  "int" is u
29269 73 65 64 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a  sed otherwise..*
2926a 2a 20 20 20 20 59 59 4e 4f 43 4f 44 45 20 20 20  *    YYNOCODE   
2926b 20 20 20 20 20 20 20 20 69 73 20 61 20 6e 75 6d          is a num
2926c 62 65 72 20 6f 66 20 74 79 70 65 20 59 59 43 4f  ber of type YYCO
2926d 44 45 54 59 50 45 20 77 68 69 63 68 20 63 6f 72  DETYPE which cor
2926e 72 65 73 70 6f 6e 64 73 0a 2a 2a 20 20 20 20 20  responds.**     
2926f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29270 20 20 74 6f 20 6e 6f 20 6c 65 67 61 6c 20 74 65    to no legal te
29271 72 6d 69 6e 61 6c 20 6f 72 20 6e 6f 6e 74 65 72  rminal or nonter
29272 6d 69 6e 61 6c 20 6e 75 6d 62 65 72 2e 20 20 54  minal number.  T
29273 68 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  his.**          
29274 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 75 6d               num
29275 62 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 66  ber is used to f
29276 69 6c 6c 20 69 6e 20 65 6d 70 74 79 20 73 6c 6f  ill in empty slo
29277 74 73 20 6f 66 20 74 68 65 20 68 61 73 68 20 0a  ts of the hash .
29278 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
29279 20 20 20 20 20 20 20 20 20 74 61 62 6c 65 2e 0a           table..
2927a 2a 2a 20 20 20 20 59 59 46 41 4c 4c 42 41 43 4b  **    YYFALLBACK
2927b 20 20 20 20 20 20 20 20 20 49 66 20 64 65 66 69           If defi
2927c 6e 65 64 2c 20 74 68 69 73 20 69 6e 64 69 63 61  ned, this indica
2927d 74 65 73 20 74 68 61 74 20 6f 6e 65 20 6f 72 20  tes that one or 
2927e 6d 6f 72 65 20 74 6f 6b 65 6e 73 0a 2a 2a 20 20  more tokens.**  
2927f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29280 20 20 20 20 20 68 61 76 65 20 66 61 6c 6c 2d 62       have fall-b
29281 61 63 6b 20 76 61 6c 75 65 73 20 77 68 69 63 68  ack values which
29282 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20   should be used 
29283 69 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  if the.**       
29284 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29285 6f 72 69 67 69 6e 61 6c 20 76 61 6c 75 65 20 6f  original value o
29286 66 20 74 68 65 20 74 6f 6b 65 6e 20 77 69 6c 6c  f the token will
29287 20 6e 6f 74 20 70 61 72 73 65 2e 0a 2a 2a 20 20   not parse..**  
29288 20 20 59 59 41 43 54 49 4f 4e 54 59 50 45 20 20    YYACTIONTYPE  
29289 20 20 20 20 20 69 73 20 74 68 65 20 64 61 74 61       is the data
2928a 20 74 79 70 65 20 75 73 65 64 20 66 6f 72 20 73   type used for s
2928b 74 6f 72 69 6e 67 20 74 65 72 6d 69 6e 61 6c 0a  toring terminal.
2928c 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2928d 20 20 20 20 20 20 20 20 20 61 6e 64 20 6e 6f 6e           and non
2928e 74 65 72 6d 69 6e 61 6c 20 6e 75 6d 62 65 72 73  terminal numbers
2928f 2e 20 20 22 75 6e 73 69 67 6e 65 64 20 63 68 61  .  "unsigned cha
29290 72 22 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20  r" is.**        
29291 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75                 u
29292 73 65 64 20 69 66 20 74 68 65 72 65 20 61 72 65  sed if there are
29293 20 66 65 77 65 72 20 74 68 61 6e 20 32 35 30 20   fewer than 250 
29294 72 75 6c 65 73 20 61 6e 64 0a 2a 2a 20 20 20 20  rules and.**    
29295 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29296 20 20 20 73 74 61 74 65 73 20 63 6f 6d 62 69 6e     states combin
29297 65 64 2e 20 20 22 69 6e 74 22 20 69 73 20 75 73  ed.  "int" is us
29298 65 64 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a  ed otherwise..**
29299 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65      sqlite3Parse
2929a 72 54 4f 4b 45 4e 54 59 50 45 20 20 20 20 20 69  rTOKENTYPE     i
2929b 73 20 74 68 65 20 64 61 74 61 20 74 79 70 65 20  s the data type 
2929c 75 73 65 64 20 66 6f 72 20 6d 69 6e 6f 72 20 74  used for minor t
2929d 6f 6b 65 6e 73 20 67 69 76 65 6e 20 0a 2a 2a 20  okens given .** 
2929e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2929f 20 20 20 20 20 20 64 69 72 65 63 74 6c 79 20 74        directly t
292a0 6f 20 74 68 65 20 70 61 72 73 65 72 20 66 72 6f  o the parser fro
292a1 6d 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 2e  m the tokenizer.
292a2 0a 2a 2a 20 20 20 20 59 59 4d 49 4e 4f 52 54 59  .**    YYMINORTY
292a3 50 45 20 20 20 20 20 20 20 20 69 73 20 74 68 65  PE        is the
292a4 20 64 61 74 61 20 74 79 70 65 20 75 73 65 64 20   data type used 
292a5 66 6f 72 20 61 6c 6c 20 6d 69 6e 6f 72 20 74 6f  for all minor to
292a6 6b 65 6e 73 2e 0a 2a 2a 20 20 20 20 20 20 20 20  kens..**        
292a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54                 T
292a8 68 69 73 20 69 73 20 74 79 70 69 63 61 6c 6c 79  his is typically
292a9 20 61 20 75 6e 69 6f 6e 20 6f 66 20 6d 61 6e 79   a union of many
292aa 20 74 79 70 65 73 2c 20 6f 6e 65 20 6f 66 0a 2a   types, one of.*
292ab 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
292ac 20 20 20 20 20 20 20 20 77 68 69 63 68 20 69 73          which is
292ad 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f   sqlite3ParserTO
292ae 4b 45 4e 54 59 50 45 2e 20 20 54 68 65 20 65 6e  KENTYPE.  The en
292af 74 72 79 20 69 6e 20 74 68 65 20 75 6e 69 6f 6e  try in the union
292b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
292b1 20 20 20 20 20 20 20 20 20 20 66 6f 72 20 62 61            for ba
292b2 73 65 20 74 6f 6b 65 6e 73 20 69 73 20 63 61 6c  se tokens is cal
292b3 6c 65 64 20 22 79 79 30 22 2e 0a 2a 2a 20 20 20  led "yy0"..**   
292b4 20 59 59 53 54 41 43 4b 44 45 50 54 48 20 20 20   YYSTACKDEPTH   
292b5 20 20 20 20 69 73 20 74 68 65 20 6d 61 78 69 6d      is the maxim
292b6 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 65 20  um depth of the 
292b7 70 61 72 73 65 72 27 73 20 73 74 61 63 6b 2e 20  parser's stack. 
292b8 20 49 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   If.**          
292b9 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 65 72               zer
292ba 6f 20 74 68 65 20 73 74 61 63 6b 20 69 73 20 64  o the stack is d
292bb 79 6e 61 6d 69 63 61 6c 6c 79 20 73 69 7a 65 64  ynamically sized
292bc 20 75 73 69 6e 67 20 72 65 61 6c 6c 6f 63 28 29   using realloc()
292bd 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65 33 50 61  .**    sqlite3Pa
292be 72 73 65 72 41 52 47 5f 53 44 45 43 4c 20 20 20  rserARG_SDECL   
292bf 20 20 41 20 73 74 61 74 69 63 20 76 61 72 69 61    A static varia
292c0 62 6c 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  ble declaration 
292c1 66 6f 72 20 74 68 65 20 25 65 78 74 72 61 5f 61  for the %extra_a
292c2 72 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 73 71  rgument.**    sq
292c3 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 50  lite3ParserARG_P
292c4 44 45 43 4c 20 20 20 20 20 41 20 70 61 72 61 6d  DECL     A param
292c5 65 74 65 72 20 64 65 63 6c 61 72 61 74 69 6f 6e  eter declaration
292c6 20 66 6f 72 20 74 68 65 20 25 65 78 74 72 61 5f   for the %extra_
292c7 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 73  argument.**    s
292c8 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f  qlite3ParserARG_
292c9 53 54 4f 52 45 20 20 20 20 20 43 6f 64 65 20 74  STORE     Code t
292ca 6f 20 73 74 6f 72 65 20 25 65 78 74 72 61 5f 61  o store %extra_a
292cb 72 67 75 6d 65 6e 74 20 69 6e 74 6f 20 79 79 70  rgument into yyp
292cc 50 61 72 73 65 72 0a 2a 2a 20 20 20 20 73 71 6c  Parser.**    sql
292cd 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 46 45  ite3ParserARG_FE
292ce 54 43 48 20 20 20 20 20 43 6f 64 65 20 74 6f 20  TCH     Code to 
292cf 65 78 74 72 61 63 74 20 25 65 78 74 72 61 5f 61  extract %extra_a
292d0 72 67 75 6d 65 6e 74 20 66 72 6f 6d 20 79 79 70  rgument from yyp
292d1 50 61 72 73 65 72 0a 2a 2a 20 20 20 20 59 59 4e  Parser.**    YYN
292d2 53 54 41 54 45 20 20 20 20 20 20 20 20 20 20 20  STATE           
292d3 74 68 65 20 63 6f 6d 62 69 6e 65 64 20 6e 75 6d  the combined num
292d4 62 65 72 20 6f 66 20 73 74 61 74 65 73 2e 0a 2a  ber of states..*
292d5 2a 20 20 20 20 59 59 4e 52 55 4c 45 20 20 20 20  *    YYNRULE    
292d6 20 20 20 20 20 20 20 20 74 68 65 20 6e 75 6d 62          the numb
292d7 65 72 20 6f 66 20 72 75 6c 65 73 20 69 6e 20 74  er of rules in t
292d8 68 65 20 67 72 61 6d 6d 61 72 0a 2a 2a 20 20 20  he grammar.**   
292d9 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 20 20   YYERRORSYMBOL  
292da 20 20 20 20 69 73 20 74 68 65 20 63 6f 64 65 20      is the code 
292db 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 65 72  number of the er
292dc 72 6f 72 20 73 79 6d 62 6f 6c 2e 20 20 49 66 20  ror symbol.  If 
292dd 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  not.**          
292de 20 20 20 20 20 20 20 20 20 20 20 20 20 64 65 66               def
292df 69 6e 65 64 2c 20 74 68 65 6e 20 64 6f 20 6e 6f  ined, then do no
292e0 20 65 72 72 6f 72 20 70 72 6f 63 65 73 73 69 6e   error processin
292e1 67 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 59 59  g..*/.#define YY
292e2 43 4f 44 45 54 59 50 45 20 75 6e 73 69 67 6e 65  CODETYPE unsigne
292e3 64 20 63 68 61 72 0a 23 64 65 66 69 6e 65 20 59  d char.#define Y
292e4 59 4e 4f 43 4f 44 45 20 32 34 39 0a 23 64 65 66  YNOCODE 249.#def
292e5 69 6e 65 20 59 59 41 43 54 49 4f 4e 54 59 50 45  ine YYACTIONTYPE
292e6 20 75 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20   unsigned short 
292e7 69 6e 74 0a 23 64 65 66 69 6e 65 20 59 59 57 49  int.#define YYWI
292e8 4c 44 43 41 52 44 20 35 39 0a 23 64 65 66 69 6e  LDCARD 59.#defin
292e9 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54  e sqlite3ParserT
292ea 4f 4b 45 4e 54 59 50 45 20 54 6f 6b 65 6e 0a 74  OKENTYPE Token.t
292eb 79 70 65 64 65 66 20 75 6e 69 6f 6e 20 7b 0a 20  ypedef union {. 
292ec 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f   sqlite3ParserTO
292ed 4b 45 4e 54 59 50 45 20 79 79 30 3b 0a 20 20 53  KENTYPE yy0;.  S
292ee 65 6c 65 63 74 2a 20 79 79 34 33 3b 0a 20 20 54  elect* yy43;.  T
292ef 72 69 67 67 65 72 53 74 65 70 2a 20 79 79 37 35  riggerStep* yy75
292f0 3b 0a 20 20 73 74 72 75 63 74 20 4c 69 6d 69 74  ;.  struct Limit
292f1 56 61 6c 20 79 79 38 34 3b 0a 20 20 73 74 72 75  Val yy84;.  stru
292f2 63 74 20 4c 69 6b 65 4f 70 20 79 79 38 36 3b 0a  ct LikeOp yy86;.
292f3 20 20 73 74 72 75 63 74 20 7b 69 6e 74 20 76 61    struct {int va
292f4 6c 75 65 3b 20 69 6e 74 20 6d 61 73 6b 3b 7d 20  lue; int mask;} 
292f5 79 79 32 30 37 3b 0a 20 20 45 78 70 72 4c 69 73  yy207;.  ExprLis
292f6 74 2a 20 79 79 32 34 32 3b 0a 20 20 69 6e 74 20  t* yy242;.  int 
292f7 79 79 33 31 36 3b 0a 20 20 49 64 4c 69 73 74 2a  yy316;.  IdList*
292f8 20 79 79 33 35 32 3b 0a 20 20 73 74 72 75 63 74   yy352;.  struct
292f9 20 54 72 69 67 45 76 65 6e 74 20 79 79 33 35 34   TrigEvent yy354
292fa 3b 0a 20 20 53 72 63 4c 69 73 74 2a 20 79 79 34  ;.  SrcList* yy4
292fb 31 39 3b 0a 20 20 45 78 70 72 2a 20 79 79 34 35  19;.  Expr* yy45
292fc 30 3b 0a 7d 20 59 59 4d 49 4e 4f 52 54 59 50 45  0;.} YYMINORTYPE
292fd 3b 0a 23 69 66 6e 64 65 66 20 59 59 53 54 41 43  ;.#ifndef YYSTAC
292fe 4b 44 45 50 54 48 0a 23 64 65 66 69 6e 65 20 59  KDEPTH.#define Y
292ff 59 53 54 41 43 4b 44 45 50 54 48 20 31 30 30 0a  YSTACKDEPTH 100.
29300 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 73  #endif.#define s
29301 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f  qlite3ParserARG_
29302 53 44 45 43 4c 20 50 61 72 73 65 20 2a 70 50 61  SDECL Parse *pPa
29303 72 73 65 3b 0a 23 64 65 66 69 6e 65 20 73 71 6c  rse;.#define sql
29304 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 50 44  ite3ParserARG_PD
29305 45 43 4c 20 2c 50 61 72 73 65 20 2a 70 50 61 72  ECL ,Parse *pPar
29306 73 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  se.#define sqlit
29307 65 33 50 61 72 73 65 72 41 52 47 5f 46 45 54 43  e3ParserARG_FETC
29308 48 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 20  H Parse *pParse 
29309 3d 20 79 79 70 50 61 72 73 65 72 2d 3e 70 50 61  = yypParser->pPa
2930a 72 73 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  rse.#define sqli
2930b 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f  te3ParserARG_STO
2930c 52 45 20 79 79 70 50 61 72 73 65 72 2d 3e 70 50  RE yypParser->pP
2930d 61 72 73 65 20 3d 20 70 50 61 72 73 65 0a 23 64  arse = pParse.#d
2930e 65 66 69 6e 65 20 59 59 4e 53 54 41 54 45 20 35  efine YYNSTATE 5
2930f 39 38 0a 23 64 65 66 69 6e 65 20 59 59 4e 52 55  98.#define YYNRU
29310 4c 45 20 33 31 35 0a 23 64 65 66 69 6e 65 20 59  LE 315.#define Y
29311 59 46 41 4c 4c 42 41 43 4b 20 31 0a 23 64 65 66  YFALLBACK 1.#def
29312 69 6e 65 20 59 59 5f 4e 4f 5f 41 43 54 49 4f 4e  ine YY_NO_ACTION
29313 20 20 20 20 20 20 28 59 59 4e 53 54 41 54 45 2b        (YYNSTATE+
29314 59 59 4e 52 55 4c 45 2b 32 29 0a 23 64 65 66 69  YYNRULE+2).#defi
29315 6e 65 20 59 59 5f 41 43 43 45 50 54 5f 41 43 54  ne YY_ACCEPT_ACT
29316 49 4f 4e 20 20 28 59 59 4e 53 54 41 54 45 2b 59  ION  (YYNSTATE+Y
29317 59 4e 52 55 4c 45 2b 31 29 0a 23 64 65 66 69 6e  YNRULE+1).#defin
29318 65 20 59 59 5f 45 52 52 4f 52 5f 41 43 54 49 4f  e YY_ERROR_ACTIO
29319 4e 20 20 20 28 59 59 4e 53 54 41 54 45 2b 59 59  N   (YYNSTATE+YY
2931a 4e 52 55 4c 45 29 0a 0a 2f 2a 20 54 68 65 20 79  NRULE)../* The y
2931b 79 7a 65 72 6f 6d 69 6e 6f 72 20 63 6f 6e 73 74  yzerominor const
2931c 61 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20 69  ant is used to i
2931d 6e 69 74 69 61 6c 69 7a 65 20 69 6e 73 74 61 6e  nitialize instan
2931e 63 65 73 20 6f 66 0a 2a 2a 20 59 59 4d 49 4e 4f  ces of.** YYMINO
2931f 52 54 59 50 45 20 6f 62 6a 65 63 74 73 20 74 6f  RTYPE objects to
29320 20 7a 65 72 6f 2e 20 2a 2f 0a 23 69 66 20 30 0a   zero. */.#if 0.
29321 73 74 61 74 69 63 20 59 59 4d 49 4e 4f 52 54 59  static YYMINORTY
29322 50 45 20 79 79 7a 65 72 6f 6d 69 6e 6f 72 3b 0a  PE yyzerominor;.
29323 23 65 6c 73 65 0a 73 74 61 74 69 63 20 63 6f 6e  #else.static con
29324 73 74 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 79  st YYMINORTYPE y
29325 79 7a 65 72 6f 6d 69 6e 6f 72 3b 0a 23 65 6e 64  yzerominor;.#end
29326 69 66 0a 0a 2f 2a 20 4e 65 78 74 20 61 72 65 20  if../* Next are 
29327 74 68 65 20 74 61 62 6c 65 73 20 75 73 65 64 20  the tables used 
29328 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61  to determine wha
29329 74 20 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65  t action to take
2932a 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a   based on the.**
2932b 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 61   current state a
2932c 6e 64 20 6c 6f 6f 6b 61 68 65 61 64 20 74 6f 6b  nd lookahead tok
2932d 65 6e 2e 20 20 54 68 65 73 65 20 74 61 62 6c 65  en.  These table
2932e 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d  s are used to im
2932f 70 6c 65 6d 65 6e 74 0a 2a 2a 20 66 75 6e 63 74  plement.** funct
29330 69 6f 6e 73 20 74 68 61 74 20 74 61 6b 65 20 61  ions that take a
29331 20 73 74 61 74 65 20 6e 75 6d 62 65 72 20 61 6e   state number an
29332 64 20 6c 6f 6f 6b 61 68 65 61 64 20 76 61 6c 75  d lookahead valu
29333 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 0a  e and return an.
29334 2a 2a 20 61 63 74 69 6f 6e 20 69 6e 74 65 67 65  ** action intege
29335 72 2e 20 20 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f  r.  .**.** Suppo
29336 73 65 20 74 68 65 20 61 63 74 69 6f 6e 20 69 6e  se the action in
29337 74 65 67 65 72 20 69 73 20 4e 2e 20 20 54 68 65  teger is N.  The
29338 6e 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20  n the action is 
29339 64 65 74 65 72 6d 69 6e 65 64 20 61 73 0a 2a 2a  determined as.**
2933a 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 0a 2a 2a 20 20   follows.**.**  
2933b 20 30 20 3c 3d 20 4e 20 3c 20 59 59 4e 53 54 41   0 <= N < YYNSTA
2933c 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TE              
2933d 20 20 20 20 53 68 69 66 74 20 4e 2e 20 20 54 68      Shift N.  Th
2933e 61 74 20 69 73 2c 20 70 75 73 68 20 74 68 65 20  at is, push the 
2933f 6c 6f 6f 6b 61 68 65 61 64 0a 2a 2a 20 20 20 20  lookahead.**    
29340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29341 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29342 20 20 74 6f 6b 65 6e 20 6f 6e 74 6f 20 74 68 65    token onto the
29343 20 73 74 61 63 6b 20 61 6e 64 20 67 6f 74 6f 20   stack and goto 
29344 73 74 61 74 65 20 4e 2e 0a 2a 2a 0a 2a 2a 20 20  state N..**.**  
29345 20 59 59 4e 53 54 41 54 45 20 3c 3d 20 4e 20 3c   YYNSTATE <= N <
29346 20 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c   YYNSTATE+YYNRUL
29347 45 20 20 20 52 65 64 75 63 65 20 62 79 20 72 75  E   Reduce by ru
29348 6c 65 20 4e 2d 59 59 4e 53 54 41 54 45 2e 0a 2a  le N-YYNSTATE..*
29349 2a 0a 2a 2a 20 20 20 4e 20 3d 3d 20 59 59 4e 53  *.**   N == YYNS
2934a 54 41 54 45 2b 59 59 4e 52 55 4c 45 20 20 20 20  TATE+YYNRULE    
2934b 20 20 20 20 20 20 20 20 20 20 41 20 73 79 6e 74            A synt
2934c 61 78 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63  ax error has occ
2934d 75 72 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 4e  urred..**.**   N
2934e 20 3d 3d 20 59 59 4e 53 54 41 54 45 2b 59 59 4e   == YYNSTATE+YYN
2934f 52 55 4c 45 2b 31 20 20 20 20 20 20 20 20 20 20  RULE+1          
29350 20 20 54 68 65 20 70 61 72 73 65 72 20 61 63 63    The parser acc
29351 65 70 74 73 20 69 74 73 20 69 6e 70 75 74 2e 0a  epts its input..
29352 2a 2a 0a 2a 2a 20 20 20 4e 20 3d 3d 20 59 59 4e  **.**   N == YYN
29353 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b 32 20  STATE+YYNRULE+2 
29354 20 20 20 20 20 20 20 20 20 20 20 4e 6f 20 73 75             No su
29355 63 68 20 61 63 74 69 6f 6e 2e 20 20 44 65 6e 6f  ch action.  Deno
29356 74 65 73 20 75 6e 75 73 65 64 0a 2a 2a 20 20 20  tes unused.**   
29357 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29358 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29359 20 20 20 73 6c 6f 74 73 20 69 6e 20 74 68 65 20     slots in the 
2935a 79 79 5f 61 63 74 69 6f 6e 5b 5d 20 74 61 62 6c  yy_action[] tabl
2935b 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 74  e..**.** The act
2935c 69 6f 6e 20 74 61 62 6c 65 20 69 73 20 63 6f 6e  ion table is con
2935d 73 74 72 75 63 74 65 64 20 61 73 20 61 20 73 69  structed as a si
2935e 6e 67 6c 65 20 6c 61 72 67 65 20 74 61 62 6c 65  ngle large table
2935f 20 6e 61 6d 65 64 20 79 79 5f 61 63 74 69 6f 6e   named yy_action
29360 5b 5d 2e 0a 2a 2a 20 47 69 76 65 6e 20 73 74 61  []..** Given sta
29361 74 65 20 53 20 61 6e 64 20 6c 6f 6f 6b 61 68 65  te S and lookahe
29362 61 64 20 58 2c 20 74 68 65 20 61 63 74 69 6f 6e  ad X, the action
29363 20 69 73 20 63 6f 6d 70 75 74 65 64 20 61 73 0a   is computed as.
29364 2a 2a 0a 2a 2a 20 20 20 20 20 20 79 79 5f 61 63  **.**      yy_ac
29365 74 69 6f 6e 5b 20 79 79 5f 73 68 69 66 74 5f 6f  tion[ yy_shift_o
29366 66 73 74 5b 53 5d 20 2b 20 58 20 5d 0a 2a 2a 0a  fst[S] + X ].**.
29367 2a 2a 20 49 66 20 74 68 65 20 69 6e 64 65 78 20  ** If the index 
29368 76 61 6c 75 65 20 79 79 5f 73 68 69 66 74 5f 6f  value yy_shift_o
29369 66 73 74 5b 53 5d 2b 58 20 69 73 20 6f 75 74 20  fst[S]+X is out 
2936a 6f 66 20 72 61 6e 67 65 20 6f 72 20 69 66 20 74  of range or if t
2936b 68 65 20 76 61 6c 75 65 0a 2a 2a 20 79 79 5f 6c  he value.** yy_l
2936c 6f 6f 6b 61 68 65 61 64 5b 79 79 5f 73 68 69 66  ookahead[yy_shif
2936d 74 5f 6f 66 73 74 5b 53 5d 2b 58 5d 20 69 73 20  t_ofst[S]+X] is 
2936e 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 58 20 6f  not equal to X o
2936f 72 20 69 66 20 79 79 5f 73 68 69 66 74 5f 6f 66  r if yy_shift_of
29370 73 74 5b 53 5d 0a 2a 2a 20 69 73 20 65 71 75 61  st[S].** is equa
29371 6c 20 74 6f 20 59 59 5f 53 48 49 46 54 5f 55 53  l to YY_SHIFT_US
29372 45 5f 44 46 4c 54 2c 20 69 74 20 6d 65 61 6e 73  E_DFLT, it means
29373 20 74 68 61 74 20 74 68 65 20 61 63 74 69 6f 6e   that the action
29374 20 69 73 20 6e 6f 74 20 69 6e 20 74 68 65 20 74   is not in the t
29375 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 61 74  able.** and that
29376 20 79 79 5f 64 65 66 61 75 6c 74 5b 53 5d 20 73   yy_default[S] s
29377 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 69 6e  hould be used in
29378 73 74 65 61 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 54  stead.  .**.** T
29379 68 65 20 66 6f 72 6d 75 6c 61 20 61 62 6f 76 65  he formula above
2937a 20 69 73 20 66 6f 72 20 63 6f 6d 70 75 74 69 6e   is for computin
2937b 67 20 74 68 65 20 61 63 74 69 6f 6e 20 77 68 65  g the action whe
2937c 6e 20 74 68 65 20 6c 6f 6f 6b 61 68 65 61 64 20  n the lookahead 
2937d 69 73 0a 2a 2a 20 61 20 74 65 72 6d 69 6e 61 6c  is.** a terminal
2937e 20 73 79 6d 62 6f 6c 2e 20 20 49 66 20 74 68 65   symbol.  If the
2937f 20 6c 6f 6f 6b 61 68 65 61 64 20 69 73 20 61 20   lookahead is a 
29380 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c 20 28 61 73  non-terminal (as
29381 20 6f 63 63 75 72 73 20 61 66 74 65 72 0a 2a 2a   occurs after.**
29382 20 61 20 72 65 64 75 63 65 20 61 63 74 69 6f 6e   a reduce action
29383 29 20 74 68 65 6e 20 74 68 65 20 79 79 5f 72 65  ) then the yy_re
29384 64 75 63 65 5f 6f 66 73 74 5b 5d 20 61 72 72 61  duce_ofst[] arra
29385 79 20 69 73 20 75 73 65 64 20 69 6e 20 70 6c 61  y is used in pla
29386 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20 79 79 5f  ce of.** the yy_
29387 73 68 69 66 74 5f 6f 66 73 74 5b 5d 20 61 72 72  shift_ofst[] arr
29388 61 79 20 61 6e 64 20 59 59 5f 52 45 44 55 43 45  ay and YY_REDUCE
29389 5f 55 53 45 5f 44 46 4c 54 20 69 73 20 75 73 65  _USE_DFLT is use
2938a 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a  d in place of.**
2938b 20 59 59 5f 53 48 49 46 54 5f 55 53 45 5f 44 46   YY_SHIFT_USE_DF
2938c 4c 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  LT..**.** The fo
2938d 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 68 65 20  llowing are the 
2938e 74 61 62 6c 65 73 20 67 65 6e 65 72 61 74 65 64  tables generated
2938f 20 69 6e 20 74 68 69 73 20 73 65 63 74 69 6f 6e   in this section
29390 3a 0a 2a 2a 0a 2a 2a 20 20 79 79 5f 61 63 74 69  :.**.**  yy_acti
29391 6f 6e 5b 5d 20 20 20 20 20 20 20 20 41 20 73 69  on[]        A si
29392 6e 67 6c 65 20 74 61 62 6c 65 20 63 6f 6e 74 61  ngle table conta
29393 69 6e 69 6e 67 20 61 6c 6c 20 61 63 74 69 6f 6e  ining all action
29394 73 2e 0a 2a 2a 20 20 79 79 5f 6c 6f 6f 6b 61 68  s..**  yy_lookah
29395 65 61 64 5b 5d 20 20 20 20 20 41 20 74 61 62 6c  ead[]     A tabl
29396 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
29397 20 6c 6f 6f 6b 61 68 65 61 64 20 66 6f 72 20 65   lookahead for e
29398 61 63 68 20 65 6e 74 72 79 20 69 6e 0a 2a 2a 20  ach entry in.** 
29399 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2939a 20 20 20 20 79 79 5f 61 63 74 69 6f 6e 2e 20 20      yy_action.  
2939b 55 73 65 64 20 74 6f 20 64 65 74 65 63 74 20 68  Used to detect h
2939c 61 73 68 20 63 6f 6c 6c 69 73 69 6f 6e 73 2e 0a  ash collisions..
2939d 2a 2a 20 20 79 79 5f 73 68 69 66 74 5f 6f 66 73  **  yy_shift_ofs
2939e 74 5b 5d 20 20 20 20 46 6f 72 20 65 61 63 68 20  t[]    For each 
2939f 73 74 61 74 65 2c 20 74 68 65 20 6f 66 66 73 65  state, the offse
293a0 74 20 69 6e 74 6f 20 79 79 5f 61 63 74 69 6f 6e  t into yy_action
293a1 20 66 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20   for.**         
293a2 20 20 20 20 20 20 20 20 20 20 20 20 73 68 69 66              shif
293a3 74 69 6e 67 20 74 65 72 6d 69 6e 61 6c 73 2e 0a  ting terminals..
293a4 2a 2a 20 20 79 79 5f 72 65 64 75 63 65 5f 6f 66  **  yy_reduce_of
293a5 73 74 5b 5d 20 20 20 46 6f 72 20 65 61 63 68 20  st[]   For each 
293a6 73 74 61 74 65 2c 20 74 68 65 20 6f 66 66 73 65  state, the offse
293a7 74 20 69 6e 74 6f 20 79 79 5f 61 63 74 69 6f 6e  t into yy_action
293a8 20 66 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20   for.**         
293a9 20 20 20 20 20 20 20 20 20 20 20 20 73 68 69 66              shif
293aa 74 69 6e 67 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61  ting non-termina
293ab 6c 73 20 61 66 74 65 72 20 61 20 72 65 64 75 63  ls after a reduc
293ac 65 2e 0a 2a 2a 20 20 79 79 5f 64 65 66 61 75 6c  e..**  yy_defaul
293ad 74 5b 5d 20 20 20 20 20 20 20 44 65 66 61 75 6c  t[]       Defaul
293ae 74 20 61 63 74 69 6f 6e 20 66 6f 72 20 65 61 63  t action for eac
293af 68 20 73 74 61 74 65 2e 0a 2a 2f 0a 73 74 61 74  h state..*/.stat
293b0 69 63 20 63 6f 6e 73 74 20 59 59 41 43 54 49 4f  ic const YYACTIO
293b1 4e 54 59 50 45 20 79 79 5f 61 63 74 69 6f 6e 5b  NTYPE yy_action[
293b2 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20  ] = {. /*     0 
293b3 2a 2f 20 20 20 32 39 36 2c 20 20 39 31 34 2c 20  */   296,  914, 
293b4 20 31 32 30 2c 20 20 35 39 37 2c 20 20 20 20 32   120,  597,    2
293b5 2c 20 20 31 37 32 2c 20 20 34 32 35 2c 20 20 34  ,  172,  425,  4
293b6 32 35 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 0a  25,   62,   62,.
293b7 20 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20 20 20   /*    10 */    
293b8 36 32 2c 20 20 20 36 32 2c 20 20 32 31 30 2c 20  62,   62,  210, 
293b9 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36 34    64,   64,   64
293ba 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20  ,   64,   65,   
293bb 36 35 2c 20 20 20 36 36 2c 0a 20 2f 2a 20 20 20  65,   66,. /*   
293bc 20 32 30 20 2a 2f 20 20 20 20 36 36 2c 20 20 20   20 */    66,   
293bd 36 36 2c 20 20 20 36 37 2c 20 20 32 31 32 2c 20  66,   67,  212, 
293be 20 33 39 38 2c 20 20 33 39 35 2c 20 20 34 33 32   398,  395,  432
293bf 2c 20 20 34 33 38 2c 20 20 20 36 39 2c 20 20 20  ,  438,   69,   
293c0 36 34 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f  64,. /*    30 */
293c1 20 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20      64,   64,   
293c2 36 34 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 20  64,   65,   65, 
293c3 20 20 36 36 2c 20 20 20 36 36 2c 20 20 20 36 36    66,   66,   66
293c4 2c 20 20 20 36 37 2c 20 20 32 31 32 2c 0a 20 2f  ,   67,  212,. /
293c5 2a 20 20 20 20 34 30 20 2a 2f 20 20 20 34 35 38  *    40 */   458
293c6 2c 20 20 34 35 36 2c 20 20 33 32 37 2c 20 20 31  ,  456,  327,  1
293c7 36 38 2c 20 20 20 36 31 2c 20 20 20 36 30 2c 20  68,   61,   60, 
293c8 20 33 30 31 2c 20 20 34 34 32 2c 20 20 34 34 33   301,  442,  443
293c9 2c 20 20 34 33 39 2c 0a 20 2f 2a 20 20 20 20 35  ,  439,. /*    5
293ca 30 20 2a 2f 20 20 20 34 33 39 2c 20 20 20 36 33  0 */   439,   63
293cb 2c 20 20 20 36 33 2c 20 20 20 36 32 2c 20 20 20  ,   63,   62,   
293cc 36 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20  62,   62,   62, 
293cd 20 32 35 36 2c 20 20 20 36 34 2c 20 20 20 36 34   256,   64,   64
293ce 2c 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20  ,. /*    60 */  
293cf 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36 35    64,   64,   65
293d0 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 20  ,   65,   66,   
293d1 36 36 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20  66,   66,   67, 
293d2 20 32 31 32 2c 20 20 32 39 36 2c 0a 20 2f 2a 20   212,  296,. /* 
293d3 20 20 20 37 30 20 2a 2f 20 20 20 34 39 38 2c 20     70 */   498, 
293d4 20 34 32 35 2c 20 20 34 32 35 2c 20 20 32 31 32   425,  425,  212
293d5 2c 20 20 34 32 37 2c 20 20 20 38 33 2c 20 20 20  ,  427,   83,   
293d6 36 38 2c 20 20 34 36 39 2c 20 20 20 37 30 2c 20  68,  469,   70, 
293d7 20 31 35 34 2c 0a 20 2f 2a 20 20 20 20 38 30 20   154,. /*    80 
293d8 2a 2f 20 20 20 20 36 34 2c 20 20 20 36 34 2c 20  */    64,   64, 
293d9 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36 35    64,   64,   65
293da 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 20  ,   65,   66,   
293db 36 36 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 0a  66,   66,   67,.
293dc 20 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20 20 32   /*    90 */   2
293dd 31 32 2c 20 20 20 36 38 2c 20 20 33 30 37 2c 20  12,   68,  307, 
293de 20 20 37 30 2c 20 20 31 35 34 2c 20 20 34 33 32    70,  154,  432
293df 2c 20 20 34 33 38 2c 20 20 34 35 34 2c 20 20 32  ,  438,  454,  2
293e0 31 34 2c 20 20 20 35 39 2c 0a 20 2f 2a 20 20 20  14,   59,. /*   
293e1 31 30 30 20 2a 2f 20 20 20 20 36 35 2c 20 20 20  100 */    65,   
293e2 36 35 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20  65,   66,   66, 
293e3 20 20 36 36 2c 20 20 20 36 37 2c 20 20 32 31 32    66,   67,  212
293e4 2c 20 20 34 32 39 2c 20 20 34 32 39 2c 20 20 34  ,  429,  429,  4
293e5 32 39 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f  29,. /*   110 */
293e6 20 20 20 34 39 37 2c 20 20 35 38 33 2c 20 20 32     497,  583,  2
293e7 39 36 2c 20 20 20 36 31 2c 20 20 20 36 30 2c 20  96,   61,   60, 
293e8 20 33 30 31 2c 20 20 34 34 32 2c 20 20 34 34 33   301,  442,  443
293e9 2c 20 20 34 33 39 2c 20 20 34 33 39 2c 0a 20 2f  ,  439,  439,. /
293ea 2a 20 20 20 31 32 30 20 2a 2f 20 20 20 20 36 33  *   120 */    63
293eb 2c 20 20 20 36 33 2c 20 20 20 36 32 2c 20 20 20  ,   63,   62,   
293ec 36 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20  62,   62,   62, 
293ed 20 33 32 31 2c 20 20 20 36 34 2c 20 20 20 36 34   321,   64,   64
293ee 2c 20 20 20 36 34 2c 0a 20 2f 2a 20 20 20 31 33  ,   64,. /*   13
293ef 30 20 2a 2f 20 20 20 20 36 34 2c 20 20 20 36 35  0 */    64,   65
293f0 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 20  ,   65,   66,   
293f1 36 36 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20  66,   66,   67, 
293f2 20 32 31 32 2c 20 20 34 33 32 2c 20 20 34 33 38   212,  432,  438
293f3 2c 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20  ,. /*   140 */  
293f4 20 20 39 35 2c 20 20 20 36 36 2c 20 20 20 36 36    95,   66,   66
293f5 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20 32  ,   66,   67,  2
293f6 31 32 2c 20 20 34 30 33 2c 20 20 32 35 36 2c 20  12,  403,  256, 
293f7 20 34 32 31 2c 20 20 20 33 35 2c 0a 20 2f 2a 20   421,   35,. /* 
293f8 20 20 31 35 30 20 2a 2f 20 20 20 20 35 37 2c 20    150 */    57, 
293f9 20 20 36 37 2c 20 20 32 31 32 2c 20 20 31 37 35    67,  212,  175
293fa 2c 20 20 34 31 37 2c 20 20 34 39 39 2c 20 20 20  ,  417,  499,   
293fb 36 31 2c 20 20 20 36 30 2c 20 20 33 30 31 2c 20  61,   60,  301, 
293fc 20 34 34 32 2c 0a 20 2f 2a 20 20 20 31 36 30 20   442,. /*   160 
293fd 2a 2f 20 20 20 34 34 33 2c 20 20 34 33 39 2c 20  */   443,  439, 
293fe 20 34 33 39 2c 20 20 20 36 33 2c 20 20 20 36 33   439,   63,   63
293ff 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20 20  ,   62,   62,   
29400 36 32 2c 20 20 20 36 32 2c 20 20 20 31 39 2c 0a  62,   62,   19,.
29401 20 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20 20 20   /*   170 */    
29402 36 34 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20  64,   64,   64, 
29403 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20 36 35    64,   65,   65
29404 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20 20 20  ,   66,   66,   
29405 36 36 2c 20 20 20 36 37 2c 0a 20 2f 2a 20 20 20  66,   67,. /*   
29406 31 38 30 20 2a 2f 20 20 20 32 31 32 2c 20 20 32  180 */   212,  2
29407 39 36 2c 20 20 32 32 35 2c 20 20 35 33 32 2c 20  96,  225,  532, 
29408 20 32 39 39 2c 20 20 35 38 31 2c 20 20 31 30 39   299,  581,  109
29409 2c 20 20 34 32 32 2c 20 20 32 34 32 2c 20 20 34  ,  422,  242,  4
2940a 35 38 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f  58,. /*   190 */
2940b 20 20 20 34 31 36 2c 20 20 33 33 35 2c 20 20 34     416,  335,  4
2940c 31 34 2c 20 20 20 32 31 2c 20 20 35 30 32 2c 20  14,   21,  502, 
2940d 20 35 30 33 2c 20 20 33 34 36 2c 20 20 34 30 33   503,  346,  403
2940e 2c 20 20 35 32 37 2c 20 20 31 37 36 2c 0a 20 2f  ,  527,  176,. /
2940f 2a 20 20 20 32 30 30 20 2a 2f 20 20 20 31 36 30  *   200 */   160
29410 2c 20 20 34 35 34 2c 20 20 32 31 34 2c 20 20 35  ,  454,  214,  5
29411 38 30 2c 20 20 35 37 39 2c 20 20 33 34 34 2c 20  80,  579,  344, 
29412 20 35 30 30 2c 20 20 34 33 32 2c 20 20 34 33 38   500,  432,  438
29413 2c 20 20 31 34 39 2c 0a 20 2f 2a 20 20 20 32 31  ,  149,. /*   21
29414 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 34 30 34  0 */   150,  404
29415 2c 20 20 34 30 35 2c 20 20 35 33 39 2c 20 20 35  ,  405,  539,  5
29416 31 34 2c 20 20 34 31 38 2c 20 20 31 35 31 2c 20  14,  418,  151, 
29417 20 35 34 31 2c 20 20 20 20 38 2c 20 20 34 39 38   541,    8,  498
29418 2c 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20  ,. /*   220 */  
29419 20 35 33 38 2c 20 20 35 37 37 2c 20 20 35 37 38   538,  577,  578
2941a 2c 20 20 34 32 37 2c 20 20 32 39 36 2c 20 20 20  ,  427,  296,   
2941b 36 31 2c 20 20 20 36 30 2c 20 20 33 30 31 2c 20  61,   60,  301, 
2941c 20 34 34 32 2c 20 20 34 34 33 2c 0a 20 2f 2a 20   442,  443,. /* 
2941d 20 20 32 33 30 20 2a 2f 20 20 20 34 33 39 2c 20    230 */   439, 
2941e 20 34 33 39 2c 20 20 20 36 33 2c 20 20 20 36 33   439,   63,   63
2941f 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20 20  ,   62,   62,   
29420 36 32 2c 20 20 20 36 32 2c 20 20 31 39 36 2c 20  62,   62,  196, 
29421 20 20 36 34 2c 0a 20 2f 2a 20 20 20 32 34 30 20    64,. /*   240 
29422 2a 2f 20 20 20 20 36 34 2c 20 20 20 36 34 2c 20  */    64,   64, 
29423 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20 36 35    64,   65,   65
29424 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20 20 20  ,   66,   66,   
29425 36 36 2c 20 20 20 36 37 2c 20 20 32 31 32 2c 0a  66,   67,  212,.
29426 20 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20 20 34   /*   250 */   4
29427 33 32 2c 20 20 34 33 38 2c 20 20 34 35 34 2c 20  32,  438,  454, 
29428 20 35 39 38 2c 20 20 33 39 38 2c 20 20 33 39 35   598,  398,  395
29429 2c 20 20 34 32 39 2c 20 20 34 32 39 2c 20 20 34  ,  429,  429,  4
2942a 32 39 2c 20 20 33 36 39 2c 0a 20 2f 2a 20 20 20  29,  369,. /*   
2942b 32 36 30 20 2a 2f 20 20 20 35 35 38 2c 20 20 34  260 */   558,  4
2942c 38 31 2c 20 20 34 30 34 2c 20 20 34 30 35 2c 20  81,  404,  405, 
2942d 20 33 37 32 2c 20 20 35 37 36 2c 20 20 32 31 33   372,  576,  213
2942e 2c 20 20 32 39 36 2c 20 20 20 36 31 2c 20 20 20  ,  296,   61,   
2942f 36 30 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f  60,. /*   270 */
29430 20 20 20 33 30 31 2c 20 20 34 34 32 2c 20 20 34     301,  442,  4
29431 34 33 2c 20 20 34 33 39 2c 20 20 34 33 39 2c 20  43,  439,  439, 
29432 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36 32    63,   63,   62
29433 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 0a 20 2f  ,   62,   62,. /
29434 2a 20 20 20 32 38 30 20 2a 2f 20 20 20 20 36 32  *   280 */    62
29435 2c 20 20 33 32 31 2c 20 20 20 36 34 2c 20 20 20  ,  321,   64,   
29436 36 34 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20  64,   64,   64, 
29437 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 36    65,   65,   66
29438 2c 20 20 20 36 36 2c 0a 20 2f 2a 20 20 20 32 39  ,   66,. /*   29
29439 30 20 2a 2f 20 20 20 20 36 36 2c 20 20 20 36 37  0 */    66,   67
2943a 2c 20 20 32 31 32 2c 20 20 34 33 32 2c 20 20 34  ,  212,  432,  4
2943b 33 38 2c 20 20 35 35 35 2c 20 20 35 30 33 2c 20  38,  555,  503, 
2943c 20 33 30 34 2c 20 20 35 35 37 2c 20 20 35 33 32   304,  557,  532
2943d 2c 0a 20 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20  ,. /*   300 */  
2943e 20 32 31 38 2c 20 20 35 35 37 2c 20 20 35 35 32   218,  557,  552
2943f 2c 20 20 34 32 31 2c 20 20 20 33 36 2c 20 20 32  ,  421,   36,  2
29440 33 34 2c 20 20 33 39 37 2c 20 20 20 20 32 2c 20  34,  397,    2, 
29441 20 35 34 32 2c 20 20 20 32 31 2c 0a 20 2f 2a 20   542,   21,. /* 
29442 20 20 33 31 30 20 2a 2f 20 20 20 35 34 30 2c 20    310 */   540, 
29443 20 20 36 31 2c 20 20 20 36 30 2c 20 20 33 30 31    61,   60,  301
29444 2c 20 20 34 34 32 2c 20 20 34 34 33 2c 20 20 34  ,  442,  443,  4
29445 33 39 2c 20 20 34 33 39 2c 20 20 20 36 33 2c 20  39,  439,   63, 
29446 20 20 36 33 2c 0a 20 2f 2a 20 20 20 33 32 30 20    63,. /*   320 
29447 2a 2f 20 20 20 20 36 32 2c 20 20 20 36 32 2c 20  */    62,   62, 
29448 20 20 36 32 2c 20 20 20 36 32 2c 20 20 33 38 38    62,   62,  388
29449 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20  ,   64,   64,   
2944a 36 34 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 0a  64,   64,   65,.
2944b 20 2f 2a 20 20 20 33 33 30 20 2a 2f 20 20 20 20   /*   330 */    
2944c 36 35 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20  65,   66,   66, 
2944d 20 20 36 36 2c 20 20 20 36 37 2c 20 20 32 31 32    66,   67,  212
2944e 2c 20 20 34 31 35 2c 20 20 35 33 30 2c 20 20 20  ,  415,  530,   
2944f 38 35 2c 20 20 33 38 31 2c 0a 20 2f 2a 20 20 20  85,  381,. /*   
29450 33 34 30 20 2a 2f 20 20 20 20 37 38 2c 20 20 33  340 */    78,  3
29451 32 33 2c 20 20 32 39 36 2c 20 20 32 31 30 2c 20  23,  296,  210, 
29452 20 33 30 34 2c 20 20 35 32 37 2c 20 20 34 39 33   304,  527,  493
29453 2c 20 20 34 39 32 2c 20 20 33 37 39 2c 20 20 32  ,  492,  379,  2
29454 37 34 2c 0a 20 2f 2a 20 20 20 33 35 30 20 2a 2f  74,. /*   350 */
29455 20 20 20 32 37 33 2c 20 20 33 37 39 2c 20 20 32     273,  379,  2
29456 37 34 2c 20 20 32 37 33 2c 20 20 33 34 37 2c 20  74,  273,  347, 
29457 20 34 36 33 2c 20 20 32 34 31 2c 20 20 33 38 37   463,  241,  387
29458 2c 20 20 32 36 38 2c 20 20 32 31 30 2c 0a 20 2f  ,  268,  210,. /
29459 2a 20 20 20 33 36 30 20 2a 2f 20 20 20 35 33 33  *   360 */   533
2945a 2c 20 20 35 38 31 2c 20 20 32 31 30 2c 20 20 34  ,  581,  210,  4
2945b 30 33 2c 20 20 20 32 30 2c 20 20 32 32 34 2c 20  03,   20,  224, 
2945c 20 31 34 34 2c 20 20 34 36 34 2c 20 20 34 33 32   144,  464,  432
2945d 2c 20 20 34 33 38 2c 0a 20 2f 2a 20 20 20 33 37  ,  438,. /*   37
2945e 30 20 2a 2f 20 20 20 34 38 35 2c 20 20 31 36 34  0 */   485,  164
2945f 2c 20 20 31 31 34 2c 20 20 32 34 38 2c 20 20 33  ,  114,  248,  3
29460 34 39 2c 20 20 32 35 33 2c 20 20 33 35 30 2c 20  49,  253,  350, 
29461 20 31 37 37 2c 20 20 35 35 34 2c 20 20 35 38 30   177,  554,  580
29462 2c 0a 20 2f 2a 20 20 20 33 38 30 20 2a 2f 20 20  ,. /*   380 */  
29463 20 34 36 35 2c 20 20 34 32 30 2c 20 20 33 33 31   465,  420,  331
29464 2c 20 20 20 38 31 2c 20 20 32 35 37 2c 20 20 34  ,   81,  257,  4
29465 31 39 2c 20 20 20 36 31 2c 20 20 20 36 30 2c 20  19,   61,   60, 
29466 20 33 30 31 2c 20 20 34 34 32 2c 0a 20 2f 2a 20   301,  442,. /* 
29467 20 20 33 39 30 20 2a 2f 20 20 20 34 34 33 2c 20    390 */   443, 
29468 20 34 33 39 2c 20 20 34 33 39 2c 20 20 20 36 33   439,  439,   63
29469 2c 20 20 20 36 33 2c 20 20 20 36 32 2c 20 20 20  ,   63,   62,   
2946a 36 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20  62,   62,   62, 
2946b 20 33 39 31 2c 0a 20 2f 2a 20 20 20 34 30 30 20   391,. /*   400 
2946c 2a 2f 20 20 20 20 36 34 2c 20 20 20 36 34 2c 20  */    64,   64, 
2946d 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36 35    64,   64,   65
2946e 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 20  ,   65,   66,   
2946f 36 36 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 0a  66,   66,   67,.
29470 20 2f 2a 20 20 20 34 31 30 20 2a 2f 20 20 20 32   /*   410 */   2
29471 31 32 2c 20 20 32 39 36 2c 20 20 32 32 34 2c 20  12,  296,  224, 
29472 20 32 30 33 2c 20 20 32 34 39 2c 20 20 34 39 36   203,  249,  496
29473 2c 20 20 34 30 33 2c 20 20 34 34 30 2c 20 20 38  ,  403,  440,  8
29474 33 37 2c 20 20 31 31 34 2c 0a 20 2f 2a 20 20 20  37,  114,. /*   
29475 34 32 30 20 2a 2f 20 20 20 32 34 38 2c 20 20 33  420 */   248,  3
29476 34 39 2c 20 20 32 35 33 2c 20 20 33 35 30 2c 20  49,  253,  350, 
29477 20 31 37 37 2c 20 20 32 35 30 2c 20 20 33 32 31   177,  250,  321
29478 2c 20 20 31 35 32 2c 20 20 34 30 34 2c 20 20 34  ,  152,  404,  4
29479 30 35 2c 0a 20 2f 2a 20 20 20 34 33 30 20 2a 2f  05,. /*   430 */
2947a 20 20 20 33 32 31 2c 20 20 32 35 37 2c 20 20 33     321,  257,  3
2947b 30 33 2c 20 20 33 32 34 2c 20 20 31 35 35 2c 20  03,  324,  155, 
2947c 20 34 34 35 2c 20 20 34 34 35 2c 20 20 34 33 32   445,  445,  432
2947d 2c 20 20 34 33 38 2c 20 20 33 31 37 2c 0a 20 2f  ,  438,  317,. /
2947e 2a 20 20 20 34 34 30 20 2a 2f 20 20 20 34 30 30  *   440 */   400
2947f 2c 20 20 33 38 39 2c 20 20 32 31 33 2c 20 20 20  ,  389,  213,   
29480 36 38 2c 20 20 32 30 39 2c 20 20 20 37 30 2c 20  68,  209,   70, 
29481 20 31 35 34 2c 20 20 34 32 32 2c 20 20 34 32 31   154,  422,  421
29482 2c 20 20 20 33 35 2c 0a 20 2f 2a 20 20 20 34 35  ,   35,. /*   45
29483 30 20 2a 2f 20 20 20 33 39 33 2c 20 20 32 30 32  0 */   393,  202
29484 2c 20 20 34 32 31 2c 20 20 20 34 32 2c 20 20 34  ,  421,   42,  4
29485 38 31 2c 20 20 20 36 31 2c 20 20 20 36 30 2c 20  81,   61,   60, 
29486 20 33 30 31 2c 20 20 34 34 32 2c 20 20 34 34 33   301,  442,  443
29487 2c 0a 20 2f 2a 20 20 20 34 36 30 20 2a 2f 20 20  ,. /*   460 */  
29488 20 34 33 39 2c 20 20 34 33 39 2c 20 20 20 36 33   439,  439,   63
29489 2c 20 20 20 36 33 2c 20 20 20 36 32 2c 20 20 20  ,   63,   62,   
2948a 36 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20  62,   62,   62, 
2948b 20 34 32 32 2c 20 20 20 36 34 2c 0a 20 2f 2a 20   422,   64,. /* 
2948c 20 20 34 37 30 20 2a 2f 20 20 20 20 36 34 2c 20    470 */    64, 
2948d 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36 35    64,   64,   65
2948e 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 20  ,   65,   66,   
2948f 36 36 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20  66,   66,   67, 
29490 20 32 31 32 2c 0a 20 2f 2a 20 20 20 34 38 30 20   212,. /*   480 
29491 2a 2f 20 20 20 32 39 36 2c 20 20 34 30 34 2c 20  */   296,  404, 
29492 20 34 30 35 2c 20 20 31 38 33 2c 20 20 35 31 33   405,  183,  513
29493 2c 20 20 34 32 32 2c 20 20 33 35 31 2c 20 20 33  ,  422,  351,  3
29494 35 34 2c 20 20 33 35 35 2c 20 20 34 30 33 2c 0a  54,  355,  403,.
29495 20 2f 2a 20 20 20 34 39 30 20 2a 2f 20 20 20 20   /*   490 */    
29496 37 37 2c 20 20 33 33 35 2c 20 20 20 37 39 2c 20  77,  335,   79, 
29497 20 34 38 39 2c 20 20 32 31 36 2c 20 20 31 38 33   489,  216,  183
29498 2c 20 20 33 33 34 2c 20 20 33 35 36 2c 20 20 33  ,  334,  356,  3
29499 35 31 2c 20 20 33 35 34 2c 0a 20 2f 2a 20 20 20  51,  354,. /*   
2949a 35 30 30 20 2a 2f 20 20 20 33 35 35 2c 20 20 34  500 */   355,  4
2949b 33 33 2c 20 20 34 33 34 2c 20 20 34 30 36 2c 20  33,  434,  406, 
2949c 20 34 30 37 2c 20 20 34 30 38 2c 20 20 34 33 32   407,  408,  432
2949d 2c 20 20 34 33 38 2c 20 20 32 33 35 2c 20 20 33  ,  438,  235,  3
2949e 35 36 2c 0a 20 2f 2a 20 20 20 35 31 30 20 2a 2f  56,. /*   510 */
2949f 20 20 20 33 38 36 2c 20 20 20 36 38 2c 20 20 32     386,   68,  2
294a0 39 31 2c 20 20 20 37 30 2c 20 20 31 35 34 2c 20  91,   70,  154, 
294a1 20 34 35 36 2c 20 20 35 33 31 2c 20 20 31 36 38   456,  531,  168
294a2 2c 20 20 31 39 38 2c 20 20 33 30 32 2c 0a 20 2f  ,  198,  302,. /
294a3 2a 20 20 20 35 32 30 20 2a 2f 20 20 20 34 34 39  *   520 */   449
294a4 2c 20 20 34 35 30 2c 20 20 34 33 36 2c 20 20 34  ,  450,  436,  4
294a5 33 37 2c 20 20 20 36 31 2c 20 20 20 36 30 2c 20  37,   61,   60, 
294a6 20 33 30 31 2c 20 20 34 34 32 2c 20 20 34 34 33   301,  442,  443
294a7 2c 20 20 34 33 39 2c 0a 20 2f 2a 20 20 20 35 33  ,  439,. /*   53
294a8 30 20 2a 2f 20 20 20 34 33 39 2c 20 20 20 36 33  0 */   439,   63
294a9 2c 20 20 20 36 33 2c 20 20 20 36 32 2c 20 20 20  ,   63,   62,   
294aa 36 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20  62,   62,   62, 
294ab 20 33 39 34 2c 20 20 20 36 34 2c 20 20 20 36 34   394,   64,   64
294ac 2c 0a 20 2f 2a 20 20 20 35 34 30 20 2a 2f 20 20  ,. /*   540 */  
294ad 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36 35    64,   64,   65
294ae 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 20  ,   65,   66,   
294af 36 36 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20  66,   66,   67, 
294b0 20 32 31 32 2c 20 20 32 39 36 2c 0a 20 2f 2a 20   212,  296,. /* 
294b1 20 20 35 35 30 20 2a 2f 20 20 20 33 32 31 2c 20    550 */   321, 
294b2 20 34 33 35 2c 20 20 34 32 32 2c 20 20 32 36 30   435,  422,  260
294b3 2c 20 20 34 30 34 2c 20 20 34 30 35 2c 20 20 33  ,  404,  405,  3
294b4 32 31 2c 20 20 31 38 33 2c 20 20 31 35 33 2c 20  21,  183,  153, 
294b5 20 33 32 31 2c 0a 20 2f 2a 20 20 20 35 36 30 20   321,. /*   560 
294b6 2a 2f 20 20 20 33 35 31 2c 20 20 33 35 34 2c 20  */   351,  354, 
294b7 20 33 35 35 2c 20 20 34 34 36 2c 20 20 33 33 32   355,  446,  332
294b8 2c 20 20 33 32 31 2c 20 20 35 39 35 2c 20 20 39  ,  321,  595,  9
294b9 30 35 2c 20 20 33 32 31 2c 20 20 39 30 35 2c 0a  05,  321,  905,.
294ba 20 2f 2a 20 20 20 35 37 30 20 2a 2f 20 20 20 20   /*   570 */    
294bb 20 31 2c 20 20 33 35 36 2c 20 20 34 32 31 2c 20   1,  356,  421, 
294bc 20 20 32 38 2c 20 20 34 30 33 2c 20 20 34 33 32    28,  403,  432
294bd 2c 20 20 34 33 38 2c 20 20 33 37 36 2c 20 20 34  ,  438,  376,  4
294be 32 31 2c 20 20 20 34 32 2c 0a 20 2f 2a 20 20 20  21,   42,. /*   
294bf 35 38 30 20 2a 2f 20 20 20 34 37 37 2c 20 20 34  580 */   477,  4
294c0 32 31 2c 20 20 20 33 35 2c 20 20 32 31 33 2c 20  21,   35,  213, 
294c1 20 35 34 38 2c 20 20 33 36 36 2c 20 20 35 34 38   548,  366,  548
294c2 2c 20 20 34 32 31 2c 20 20 20 35 30 2c 20 20 31  ,  421,   50,  1
294c3 35 39 2c 0a 20 2f 2a 20 20 20 35 39 30 20 2a 2f  59,. /*   590 */
294c4 20 20 20 34 32 31 2c 20 20 20 35 30 2c 20 20 34     421,   50,  4
294c5 32 32 2c 20 20 20 36 31 2c 20 20 20 36 30 2c 20  22,   61,   60, 
294c6 20 33 30 31 2c 20 20 34 34 32 2c 20 20 34 34 33   301,  442,  443
294c7 2c 20 20 34 33 39 2c 20 20 34 33 39 2c 0a 20 2f  ,  439,  439,. /
294c8 2a 20 20 20 36 30 30 20 2a 2f 20 20 20 20 36 33  *   600 */    63
294c9 2c 20 20 20 36 33 2c 20 20 20 36 32 2c 20 20 20  ,   63,   62,   
294ca 36 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20  62,   62,   62, 
294cb 20 35 39 32 2c 20 20 20 36 34 2c 20 20 20 36 34   592,   64,   64
294cc 2c 20 20 20 36 34 2c 0a 20 2f 2a 20 20 20 36 31  ,   64,. /*   61
294cd 30 20 2a 2f 20 20 20 20 36 34 2c 20 20 20 36 35  0 */    64,   65
294ce 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 20  ,   65,   66,   
294cf 36 36 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20  66,   66,   67, 
294d0 20 32 31 32 2c 20 20 32 39 36 2c 20 20 33 33 37   212,  296,  337
294d1 2c 0a 20 2f 2a 20 20 20 36 32 30 20 2a 2f 20 20  ,. /*   620 */  
294d2 20 32 31 37 2c 20 20 34 36 33 2c 20 20 32 35 36   217,  463,  256
294d3 2c 20 20 20 39 34 2c 20 20 33 33 39 2c 20 20 33  ,   94,  339,  3
294d4 32 36 2c 20 20 34 34 39 2c 20 20 34 35 30 2c 20  26,  449,  450, 
294d5 20 31 37 32 2c 20 20 33 34 30 2c 0a 20 2f 2a 20   172,  340,. /* 
294d6 20 20 36 33 30 20 2a 2f 20 20 20 34 32 35 2c 20    630 */   425, 
294d7 20 33 34 35 2c 20 20 35 33 32 2c 20 20 34 36 34   345,  532,  464
294d8 2c 20 20 33 31 32 2c 20 20 35 39 35 2c 20 20 39  ,  312,  595,  9
294d9 30 34 2c 20 20 33 31 33 2c 20 20 39 30 34 2c 20  04,  313,  904, 
294da 20 34 30 34 2c 0a 20 2f 2a 20 20 20 36 34 30 20   404,. /*   640 
294db 2a 2f 20 20 20 34 30 35 2c 20 20 35 38 38 2c 20  */   405,  588, 
294dc 20 20 32 31 2c 20 20 32 32 36 2c 20 20 34 33 32    21,  226,  432
294dd 2c 20 20 34 33 38 2c 20 20 34 36 35 2c 20 20 32  ,  438,  465,  2
294de 34 33 2c 20 20 35 30 34 2c 20 20 33 32 34 2c 0a  43,  504,  324,.
294df 20 2f 2a 20 20 20 36 35 30 20 2a 2f 20 20 20 33   /*   650 */   3
294e0 32 32 2c 20 20 34 34 35 2c 20 20 34 34 35 2c 20  22,  445,  445, 
294e1 20 34 32 31 2c 20 20 20 20 33 2c 20 20 34 35 39   421,    3,  459
294e2 2c 20 20 32 33 30 2c 20 20 33 30 38 2c 20 20 35  ,  230,  308,  5
294e3 30 35 2c 20 20 31 39 34 2c 0a 20 2f 2a 20 20 20  05,  194,. /*   
294e4 36 36 30 20 2a 2f 20 20 20 32 37 38 2c 20 20 32  660 */   278,  2
294e5 39 36 2c 20 20 20 36 31 2c 20 20 20 36 30 2c 20  96,   61,   60, 
294e6 20 33 30 31 2c 20 20 34 34 32 2c 20 20 34 34 33   301,  442,  443
294e7 2c 20 20 34 33 39 2c 20 20 34 33 39 2c 20 20 20  ,  439,  439,   
294e8 36 33 2c 0a 20 2f 2a 20 20 20 36 37 30 20 2a 2f  63,. /*   670 */
294e9 20 20 20 20 36 33 2c 20 20 20 36 32 2c 20 20 20      63,   62,   
294ea 36 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20  62,   62,   62, 
294eb 20 35 39 32 2c 20 20 20 36 34 2c 20 20 20 36 34   592,   64,   64
294ec 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 0a 20 2f  ,   64,   64,. /
294ed 2a 20 20 20 36 38 30 20 2a 2f 20 20 20 20 36 35  *   680 */    65
294ee 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 20  ,   65,   66,   
294ef 36 36 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20  66,   66,   67, 
294f0 20 32 31 32 2c 20 20 34 33 32 2c 20 20 34 33 38   212,  432,  438
294f1 2c 20 20 32 31 33 2c 0a 20 2f 2a 20 20 20 36 39  ,  213,. /*   69
294f2 30 20 2a 2f 20 20 20 31 37 39 2c 20 20 31 38 30  0 */   179,  180
294f3 2c 20 20 31 38 31 2c 20 20 34 32 32 2c 20 20 33  ,  181,  422,  3
294f4 32 34 2c 20 20 34 32 35 2c 20 20 34 34 35 2c 20  24,  425,  445, 
294f5 20 34 34 35 2c 20 20 32 38 31 2c 20 20 32 36 32   445,  281,  262
294f6 2c 0a 20 2f 2a 20 20 20 37 30 30 20 2a 2f 20 20  ,. /*   700 */  
294f7 20 32 37 39 2c 20 20 34 30 32 2c 20 20 31 39 34   279,  402,  194
294f8 2c 20 20 34 38 31 2c 20 20 32 39 36 2c 20 20 20  ,  481,  296,   
294f9 36 31 2c 20 20 20 36 30 2c 20 20 33 30 31 2c 20  61,   60,  301, 
294fa 20 34 34 32 2c 20 20 34 34 33 2c 0a 20 2f 2a 20   442,  443,. /* 
294fb 20 20 37 31 30 20 2a 2f 20 20 20 34 33 39 2c 20    710 */   439, 
294fc 20 34 33 39 2c 20 20 20 36 33 2c 20 20 20 36 33   439,   63,   63
294fd 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20 20  ,   62,   62,   
294fe 36 32 2c 20 20 20 36 32 2c 20 20 33 37 37 2c 20  62,   62,  377, 
294ff 20 20 36 34 2c 0a 20 2f 2a 20 20 20 37 32 30 20    64,. /*   720 
29500 2a 2f 20 20 20 20 36 34 2c 20 20 20 36 34 2c 20  */    64,   64, 
29501 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20 36 35    64,   65,   65
29502 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20 20 20  ,   66,   66,   
29503 36 36 2c 20 20 20 36 37 2c 20 20 32 31 32 2c 0a  66,   67,  212,.
29504 20 2f 2a 20 20 20 37 33 30 20 2a 2f 20 20 20 34   /*   730 */   4
29505 33 32 2c 20 20 34 33 38 2c 20 20 35 39 31 2c 20  32,  438,  591, 
29506 20 32 39 35 2c 20 20 31 31 35 2c 20 20 32 36 38   295,  115,  268
29507 2c 20 20 34 32 32 2c 20 20 32 36 36 2c 20 20 32  ,  422,  266,  2
29508 31 31 2c 20 20 32 36 34 2c 0a 20 2f 2a 20 20 20  11,  264,. /*   
29509 37 34 30 20 2a 2f 20 20 20 33 37 33 2c 20 20 33  740 */   373,  3
2950a 32 34 2c 20 20 32 34 36 2c 20 20 34 34 35 2c 20  24,  246,  445, 
2950b 20 34 34 35 2c 20 20 20 35 36 2c 20 20 32 35 36   445,   56,  256
2950c 2c 20 20 32 39 36 2c 20 20 20 36 31 2c 20 20 20  ,  296,   61,   
2950d 37 31 2c 0a 20 2f 2a 20 20 20 37 35 30 20 2a 2f  71,. /*   750 */
2950e 20 20 20 33 30 31 2c 20 20 34 34 32 2c 20 20 34     301,  442,  4
2950f 34 33 2c 20 20 34 33 39 2c 20 20 34 33 39 2c 20  43,  439,  439, 
29510 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36 32    63,   63,   62
29511 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 0a 20 2f  ,   62,   62,. /
29512 2a 20 20 20 37 36 30 20 2a 2f 20 20 20 20 36 32  *   760 */    62
29513 2c 20 20 33 37 37 2c 20 20 20 36 34 2c 20 20 20  ,  377,   64,   
29514 36 34 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20  64,   64,   64, 
29515 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 36    65,   65,   66
29516 2c 20 20 20 36 36 2c 0a 20 2f 2a 20 20 20 37 37  ,   66,. /*   77
29517 30 20 2a 2f 20 20 20 20 36 36 2c 20 20 20 36 37  0 */    66,   67
29518 2c 20 20 32 31 32 2c 20 20 34 33 32 2c 20 20 34  ,  212,  432,  4
29519 33 38 2c 20 20 35 35 30 2c 20 20 32 36 39 2c 20  38,  550,  269, 
2951a 20 34 37 34 2c 20 20 20 31 38 2c 20 20 35 34 39   474,   18,  549
2951b 2c 0a 20 2f 2a 20 20 20 37 38 30 20 2a 2f 20 20  ,. /*   780 */  
2951c 20 32 38 30 2c 20 20 33 30 39 2c 20 20 33 34 33   280,  309,  343
2951d 2c 20 20 33 38 30 2c 20 20 31 37 31 2c 20 20 31  ,  380,  171,  1
2951e 36 30 2c 20 20 32 35 36 2c 20 20 32 36 38 2c 20  60,  256,  268, 
2951f 20 20 20 35 2c 20 20 32 36 38 2c 0a 20 2f 2a 20     5,  268,. /* 
29520 20 20 37 39 30 20 2a 2f 20 20 20 32 39 36 2c 20    790 */   296, 
29521 20 33 36 38 2c 20 20 20 36 30 2c 20 20 33 30 31   368,   60,  301
29522 2c 20 20 34 34 32 2c 20 20 34 34 33 2c 20 20 34  ,  442,  443,  4
29523 33 39 2c 20 20 34 33 39 2c 20 20 20 36 33 2c 20  39,  439,   63, 
29524 20 20 36 33 2c 0a 20 2f 2a 20 20 20 38 30 30 20    63,. /*   800 
29525 2a 2f 20 20 20 20 36 32 2c 20 20 20 36 32 2c 20  */    62,   62, 
29526 20 20 36 32 2c 20 20 20 36 32 2c 20 20 33 32 31    62,   62,  321
29527 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20  ,   64,   64,   
29528 36 34 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 0a  64,   64,   65,.
29529 20 2f 2a 20 20 20 38 31 30 20 2a 2f 20 20 20 20   /*   810 */    
2952a 36 35 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20  65,   66,   66, 
2952b 20 20 36 36 2c 20 20 20 36 37 2c 20 20 32 31 32    66,   67,  212
2952c 2c 20 20 34 33 32 2c 20 20 34 33 38 2c 20 20 34  ,  432,  438,  4
2952d 30 33 2c 20 20 20 31 30 2c 0a 20 2f 2a 20 20 20  03,   10,. /*   
2952e 38 32 30 20 2a 2f 20 20 20 34 30 33 2c 20 20 33  820 */   403,  3
2952f 31 30 2c 20 20 32 36 38 2c 20 20 34 30 33 2c 20  10,  268,  403, 
29530 20 32 36 38 2c 20 20 34 38 35 2c 20 20 34 32 31   268,  485,  421
29531 2c 20 20 20 32 39 2c 20 20 35 36 36 2c 20 20 20  ,   29,  566,   
29532 32 32 2c 0a 20 2f 2a 20 20 20 38 33 30 20 2a 2f  22,. /*   830 */
29533 20 20 20 35 36 38 2c 20 20 34 32 30 2c 20 20 34     568,  420,  4
29534 32 38 2c 20 20 34 32 35 2c 20 20 33 37 36 2c 20  28,  425,  376, 
29535 20 34 31 39 2c 20 20 33 30 31 2c 20 20 34 34 32   419,  301,  442
29536 2c 20 20 34 34 33 2c 20 20 34 33 39 2c 0a 20 2f  ,  443,  439,. /
29537 2a 20 20 20 38 34 30 20 2a 2f 20 20 20 34 33 39  *   840 */   439
29538 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20  ,   63,   63,   
29539 36 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20  62,   62,   62, 
2953a 20 20 36 32 2c 20 20 33 32 31 2c 20 20 20 36 34    62,  321,   64
2953b 2c 20 20 20 36 34 2c 0a 20 2f 2a 20 20 20 38 35  ,   64,. /*   85
2953c 30 20 2a 2f 20 20 20 20 36 34 2c 20 20 20 36 34  0 */    64,   64
2953d 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20  ,   65,   65,   
2953e 36 36 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20  66,   66,   66, 
2953f 20 20 36 37 2c 20 20 32 31 32 2c 20 20 20 37 33    67,  212,   73
29540 2c 0a 20 2f 2a 20 20 20 38 36 30 20 2a 2f 20 20  ,. /*   860 */  
29541 20 33 32 38 2c 20 20 34 38 35 2c 20 20 20 20 34   328,  485,    4
29542 2c 20 20 35 36 39 2c 20 20 32 36 38 2c 20 20 35  ,  569,  268,  5
29543 37 30 2c 20 20 33 30 30 2c 20 20 32 36 38 2c 20  70,  300,  268, 
29544 20 31 34 37 2c 20 20 34 32 31 2c 0a 20 2f 2a 20   147,  421,. /* 
29545 20 20 38 37 30 20 2a 2f 20 20 20 20 32 34 2c 20    870 */    24, 
29546 20 33 32 31 2c 20 20 33 35 39 2c 20 20 33 32 31   321,  359,  321
29547 2c 20 20 33 32 35 2c 20 20 20 37 33 2c 20 20 33  ,  325,   73,  3
29548 32 38 2c 20 20 34 39 31 2c 20 20 20 20 34 2c 20  28,  491,    4, 
29549 20 34 35 35 2c 0a 20 2f 2a 20 20 20 38 38 30 20   455,. /*   880 
2954a 2a 2f 20 20 20 33 32 31 2c 20 20 33 34 32 2c 20  */   321,  342, 
2954b 20 33 30 30 2c 20 20 34 30 34 2c 20 20 34 30 35   300,  404,  405
2954c 2c 20 20 34 30 34 2c 20 20 34 30 35 2c 20 20 33  ,  404,  405,  3
2954d 36 37 2c 20 20 34 30 34 2c 20 20 34 30 35 2c 0a  67,  404,  405,.
2954e 20 2f 2a 20 20 20 38 39 30 20 2a 2f 20 20 20 33   /*   890 */   3
2954f 32 35 2c 20 20 33 33 30 2c 20 20 33 32 31 2c 20  25,  330,  321, 
29550 20 34 32 31 2c 20 20 20 33 33 2c 20 20 34 32 31   421,   33,  421
29551 2c 20 20 20 35 34 2c 20 20 33 32 31 2c 20 20 34  ,   54,  321,  4
29552 32 35 2c 20 20 31 37 38 2c 0a 20 2f 2a 20 20 20  25,  178,. /*   
29553 39 30 30 20 2a 2f 20 20 20 32 32 39 2c 20 20 34  900 */   229,  4
29554 35 38 2c 20 20 34 32 31 2c 20 20 20 35 33 2c 20  58,  421,   53, 
29555 20 33 32 31 2c 20 20 32 32 37 2c 20 20 33 32 31   321,  227,  321
29556 2c 20 20 33 33 30 2c 20 20 32 32 38 2c 20 20 34  ,  330,  228,  4
29557 37 38 2c 0a 20 2f 2a 20 20 20 39 31 30 20 2a 2f  78,. /*   910 */
29558 20 20 20 31 36 35 2c 20 20 33 32 31 2c 20 20 33     165,  321,  3
29559 31 35 2c 20 20 31 31 39 2c 20 20 34 32 31 2c 20  15,  119,  421, 
2955a 20 20 39 39 2c 20 20 33 33 33 2c 20 20 34 35 38    99,  333,  458
2955b 2c 20 20 33 32 31 2c 20 20 34 32 31 2c 0a 20 2f  ,  321,  421,. /
2955c 2a 20 20 20 39 32 30 20 2a 2f 20 20 20 20 39 37  *   920 */    97
2955d 2c 20 20 20 37 36 2c 20 20 20 37 35 2c 20 20 33  ,   76,   75,  3
2955e 31 31 2c 20 20 32 36 38 2c 20 20 35 31 39 2c 20  11,  268,  519, 
2955f 20 34 32 31 2c 20 20 31 30 32 2c 20 20 34 32 31   421,  102,  421
29560 2c 20 20 31 30 33 2c 0a 20 2f 2a 20 20 20 39 33  ,  103,. /*   93
29561 30 20 2a 2f 20 20 20 20 37 34 2c 20 20 33 31 39  0 */    74,  319
29562 2c 20 20 33 32 30 2c 20 20 34 32 31 2c 20 20 31  ,  320,  421,  1
29563 30 38 2c 20 20 34 32 37 2c 20 20 34 36 37 2c 20  08,  427,  467, 
29564 20 20 37 36 2c 20 20 20 37 35 2c 20 20 34 39 30    76,   75,  490
29565 2c 0a 20 2f 2a 20 20 20 39 34 30 20 2a 2f 20 20  ,. /*   940 */  
29566 20 34 32 31 2c 20 20 31 31 30 2c 20 20 34 35 32   421,  110,  452
29567 2c 20 20 34 35 32 2c 20 20 33 32 31 2c 20 20 35  ,  452,  321,  5
29568 32 30 2c 20 20 20 37 34 2c 20 20 33 31 39 2c 20  20,   74,  319, 
29569 20 33 32 30 2c 20 20 20 37 33 2c 0a 20 2f 2a 20   320,   73,. /* 
2956a 20 20 39 35 30 20 2a 2f 20 20 20 33 32 38 2c 20    950 */   328, 
2956b 20 34 32 37 2c 20 20 20 20 34 2c 20 20 32 31 30   427,    4,  210
2956c 2c 20 20 32 39 38 2c 20 20 33 32 31 2c 20 20 33  ,  298,  321,  3
2956d 30 30 2c 20 20 33 32 31 2c 20 20 31 35 36 2c 20  00,  321,  156, 
2956e 20 32 35 37 2c 0a 20 2f 2a 20 20 20 39 36 30 20   257,. /*   960 
2956f 2a 2f 20 20 20 33 32 31 2c 20 20 32 31 30 2c 20  */   321,  210, 
29570 20 31 38 35 2c 20 20 31 38 32 2c 20 20 33 32 35   185,  182,  325
29571 2c 20 20 32 38 34 2c 20 20 34 32 31 2c 20 20 20  ,  284,  421,   
29572 31 37 2c 20 20 34 32 39 2c 20 20 34 32 39 2c 0a  17,  429,  429,.
29573 20 2f 2a 20 20 20 39 37 30 20 2a 2f 20 20 20 34   /*   970 */   4
29574 32 39 2c 20 20 34 33 30 2c 20 20 34 33 31 2c 20  29,  430,  431, 
29575 20 20 31 32 2c 20 20 35 39 33 2c 20 20 33 37 38    12,  593,  378
29576 2c 20 20 31 38 38 2c 20 20 34 32 31 2c 20 20 31  ,  188,  421,  1
29577 30 30 2c 20 20 34 32 31 2c 0a 20 2f 2a 20 20 20  00,  421,. /*   
29578 39 38 30 20 2a 2f 20 20 20 20 33 34 2c 20 20 33  980 */    34,  3
29579 33 30 2c 20 20 34 32 31 2c 20 20 20 39 38 2c 20  30,  421,   98, 
2957a 20 34 32 39 2c 20 20 34 32 39 2c 20 20 34 32 39   429,  429,  429
2957b 2c 20 20 34 33 30 2c 20 20 34 33 31 2c 20 20 20  ,  430,  431,   
2957c 31 32 2c 0a 20 2f 2a 20 20 20 39 39 30 20 2a 2f  12,. /*   990 */
2957d 20 20 20 34 37 35 2c 20 20 34 35 38 2c 20 20 34     475,  458,  4
2957e 32 32 2c 20 20 31 36 32 2c 20 20 34 38 30 2c 20  22,  162,  480, 
2957f 20 33 32 31 2c 20 20 34 32 32 2c 20 20 33 30 36   321,  422,  306
29580 2c 20 20 32 33 31 2c 20 20 32 33 32 2c 0a 20 2f  ,  231,  232,. /
29581 2a 20 20 31 30 30 30 20 2a 2f 20 20 20 32 33 33  *  1000 */   233
29582 2c 20 20 31 30 35 2c 20 20 34 38 34 2c 20 20 36  ,  105,  484,  6
29583 33 32 2c 20 20 34 37 36 2c 20 20 33 32 31 2c 20  32,  476,  321, 
29584 20 34 38 36 2c 20 20 34 34 37 2c 20 20 33 32 31   486,  447,  321
29585 2c 20 20 20 32 33 2c 0a 20 2f 2a 20 20 31 30 31  ,   23,. /*  101
29586 30 20 2a 2f 20 20 20 34 32 32 2c 20 20 20 37 36  0 */   422,   76
29587 2c 20 20 20 37 35 2c 20 20 35 39 34 2c 20 20 32  ,   75,  594,  2
29588 30 37 2c 20 20 31 37 38 2c 20 20 32 38 36 2c 20  07,  178,  286, 
29589 20 34 32 31 2c 20 20 20 32 35 2c 20 20 32 35 34   421,   25,  254
2958a 2c 0a 20 2f 2a 20 20 31 30 32 30 20 2a 2f 20 20  ,. /*  1020 */  
2958b 20 20 37 34 2c 20 20 33 31 39 2c 20 20 33 32 30    74,  319,  320
2958c 2c 20 20 32 38 37 2c 20 20 33 32 31 2c 20 20 34  ,  287,  321,  4
2958d 32 37 2c 20 20 33 32 31 2c 20 20 34 32 31 2c 20  27,  321,  421, 
2958e 20 20 35 35 2c 20 20 33 32 31 2c 0a 20 2f 2a 20    55,  321,. /* 
2958f 20 31 30 33 30 20 2a 2f 20 20 20 34 32 31 2c 20   1030 */   421, 
29590 20 31 31 31 2c 20 20 33 32 31 2c 20 20 34 37 31   111,  321,  471
29591 2c 20 20 33 32 31 2c 20 20 32 30 35 2c 20 20 35  ,  321,  205,  5
29592 31 35 2c 20 20 35 35 37 2c 20 20 35 31 31 2c 20  15,  557,  511, 
29593 20 33 36 33 2c 0a 20 2f 2a 20 20 31 30 34 30 20   363,. /*  1040 
29594 2a 2f 20 20 20 34 37 32 2c 20 20 32 30 34 2c 20  */   472,  204, 
29595 20 33 32 31 2c 20 20 35 31 36 2c 20 20 32 30 36   321,  516,  206
29596 2c 20 20 33 32 31 2c 20 20 34 32 31 2c 20 20 31  ,  321,  421,  1
29597 31 32 2c 20 20 34 32 31 2c 20 20 31 31 33 2c 0a  12,  421,  113,.
29598 20 2f 2a 20 20 31 30 35 30 20 2a 2f 20 20 20 33   /*  1050 */   3
29599 32 31 2c 20 20 34 32 31 2c 20 20 20 32 36 2c 20  21,  421,   26, 
2959a 20 33 32 31 2c 20 20 34 32 31 2c 20 20 20 33 37   321,  421,   37
2959b 2c 20 20 34 32 31 2c 20 20 20 33 38 2c 20 20 34  ,  421,   38,  4
2959c 32 39 2c 20 20 34 32 39 2c 0a 20 2f 2a 20 20 31  29,  429,. /*  1
2959d 30 36 30 20 2a 2f 20 20 20 34 32 39 2c 20 20 34  060 */   429,  4
2959e 33 30 2c 20 20 34 33 31 2c 20 20 20 31 32 2c 20  30,  431,   12, 
2959f 20 34 32 31 2c 20 20 20 32 37 2c 20 20 35 32 31   421,   27,  521
295a0 2c 20 20 34 32 31 2c 20 20 20 33 39 2c 20 20 33  ,  421,   39,  3
295a1 32 31 2c 0a 20 2f 2a 20 20 31 30 37 30 20 2a 2f  21,. /*  1070 */
295a2 20 20 20 32 39 38 2c 20 20 31 35 38 2c 20 20 34     298,  158,  4
295a3 32 31 2c 20 20 20 34 30 2c 20 20 32 35 35 2c 20  21,   40,  255, 
295a4 20 34 32 31 2c 20 20 20 34 31 2c 20 20 33 32 31   421,   41,  321
295a5 2c 20 20 34 38 33 2c 20 20 33 32 31 2c 0a 20 2f  ,  483,  321,. /
295a6 2a 20 20 31 30 38 30 20 2a 2f 20 20 20 31 37 33  *  1080 */   173
295a7 2c 20 20 35 32 33 2c 20 20 33 32 31 2c 20 20 31  ,  523,  321,  1
295a8 38 32 2c 20 20 33 32 31 2c 20 20 35 32 32 2c 20  82,  321,  522, 
295a9 20 33 32 31 2c 20 20 33 38 34 2c 20 20 32 38 33   321,  384,  283
295aa 2c 20 20 32 37 33 2c 0a 20 2f 2a 20 20 31 30 39  ,  273,. /*  109
295ab 30 20 2a 2f 20 20 20 33 32 31 2c 20 20 34 32 31  0 */   321,  421
295ac 2c 20 20 20 34 33 2c 20 20 32 39 37 2c 20 20 35  ,   43,  297,  5
295ad 33 34 2c 20 20 33 32 31 2c 20 20 34 37 36 2c 20  34,  321,  476, 
295ae 20 33 32 31 2c 20 20 32 31 30 2c 20 20 34 32 31   321,  210,  421
295af 2c 0a 20 2f 2a 20 20 31 31 30 30 20 2a 2f 20 20  ,. /*  1100 */  
295b0 20 20 34 34 2c 20 20 34 32 31 2c 20 20 20 34 35    44,  421,   45
295b1 2c 20 20 33 32 31 2c 20 20 34 32 31 2c 20 20 20  ,  321,  421,   
295b2 33 30 2c 20 20 34 32 31 2c 20 20 20 33 31 2c 20  30,  421,   31, 
295b3 20 34 32 31 2c 20 20 20 34 36 2c 0a 20 2f 2a 20   421,   46,. /* 
295b4 20 31 31 31 30 20 2a 2f 20 20 20 35 30 38 2c 20   1110 */   508, 
295b5 20 35 30 39 2c 20 20 34 32 31 2c 20 20 20 34 37   509,  421,   47
295b6 2c 20 20 32 35 39 2c 20 20 33 32 31 2c 20 20 31  ,  259,  321,  1
295b7 38 32 2c 20 20 34 32 31 2c 20 20 20 34 38 2c 20  82,  421,   48, 
295b8 20 34 32 31 2c 0a 20 2f 2a 20 20 31 31 32 30 20   421,. /*  1120 
295b9 2a 2f 20 20 20 20 34 39 2c 20 20 33 32 31 2c 20  */    49,  321, 
295ba 20 33 35 38 2c 20 20 33 39 30 2c 20 20 31 38 32   358,  390,  182
295bb 2c 20 20 34 32 31 2c 20 20 20 33 32 2c 20 20 33  ,  421,   32,  3
295bc 32 31 2c 20 20 32 36 31 2c 20 20 35 31 38 2c 0a  21,  261,  518,.
295bd 20 2f 2a 20 20 31 31 33 30 20 2a 2f 20 20 20 35   /*  1130 */   5
295be 31 37 2c 20 20 35 35 33 2c 20 20 35 36 31 2c 20  17,  553,  561, 
295bf 20 31 38 32 2c 20 20 31 37 33 2c 20 20 34 31 32   182,  173,  412
295c0 2c 20 20 31 39 31 2c 20 20 34 32 31 2c 20 20 20  ,  191,  421,   
295c1 31 31 2c 20 20 35 36 32 2c 0a 20 2f 2a 20 20 31  11,  562,. /*  1
295c2 31 34 30 20 2a 2f 20 20 20 35 37 33 2c 20 20 20  140 */   573,   
295c3 39 32 2c 20 20 20 39 32 2c 20 20 34 32 31 2c 20  92,   92,  421, 
295c4 20 20 35 31 2c 20 20 35 39 30 2c 20 20 32 36 33    51,  590,  263
295c5 2c 20 20 32 39 34 2c 20 20 32 36 35 2c 20 20 34  ,  294,  265,  4
295c6 32 31 2c 0a 20 2f 2a 20 20 31 31 35 30 20 2a 2f  21,. /*  1150 */
295c7 20 20 20 20 35 32 2c 20 20 32 36 37 2c 20 20 32      52,  267,  2
295c8 37 32 2c 20 20 33 37 31 2c 20 20 31 34 36 2c 20  72,  371,  146, 
295c9 20 33 37 34 2c 20 20 33 37 35 2c 20 20 32 37 35   374,  375,  275
295ca 2c 20 20 32 37 36 2c 20 20 32 37 37 2c 0a 20 2f  ,  276,  277,. /
295cb 2a 20 20 31 31 36 30 20 2a 2f 20 20 20 35 36 35  *  1160 */   565
295cc 2c 20 20 35 37 35 2c 20 20 32 38 35 2c 20 20 32  ,  575,  285,  2
295cd 38 38 2c 20 20 32 38 39 2c 20 20 35 38 37 2c 20  88,  289,  587, 
295ce 20 34 37 30 2c 20 20 34 35 31 2c 20 20 32 33 36   470,  451,  236
295cf 2c 20 20 34 35 33 2c 0a 20 2f 2a 20 20 31 31 37  ,  453,. /*  117
295d0 30 20 2a 2f 20 20 20 33 32 39 2c 20 20 32 34 34  0 */   329,  244
295d1 2c 20 20 34 37 33 2c 20 20 35 31 34 2c 20 20 32  ,  473,  514,  2
295d2 35 31 2c 20 20 35 32 34 2c 20 20 35 36 30 2c 20  51,  524,  560, 
295d3 20 31 36 33 2c 20 20 34 30 31 2c 20 20 35 37 32   163,  401,  572
295d4 2c 0a 20 2f 2a 20 20 31 31 38 30 20 2a 2f 20 20  ,. /*  1180 */  
295d5 20 34 32 36 2c 20 20 35 32 35 2c 20 20 32 38 32   426,  525,  282
295d6 2c 20 20 35 32 38 2c 20 20 34 30 39 2c 20 20 20  ,  528,  409,   
295d7 20 37 2c 20 20 34 31 30 2c 20 20 34 31 31 2c 20   7,  410,  411, 
295d8 20 33 38 35 2c 20 20 33 31 38 2c 0a 20 2f 2a 20   385,  318,. /* 
295d9 20 31 31 39 30 20 2a 2f 20 20 20 20 38 35 2c 20   1190 */    85, 
295da 20 32 33 37 2c 20 20 33 33 38 2c 20 20 35 32 36   237,  338,  526
295db 2c 20 20 20 38 34 2c 20 20 33 33 36 2c 20 20 33  ,   84,  336,  3
295dc 35 33 2c 20 20 20 35 38 2c 20 20 20 38 30 2c 20  53,   58,   80, 
295dd 20 32 31 35 2c 0a 20 2f 2a 20 20 31 32 30 30 20   215,. /*  1200 
295de 2a 2f 20 20 20 31 37 30 2c 20 20 34 36 38 2c 20  */   170,  468, 
295df 20 31 32 31 2c 20 20 20 38 36 2c 20 20 33 34 31   121,   86,  341
295e0 2c 20 20 33 34 38 2c 20 20 33 30 35 2c 20 20 35  ,  348,  305,  5
295e1 30 31 2c 20 20 35 30 36 2c 20 20 31 32 34 2c 0a  01,  506,  124,.
295e2 20 2f 2a 20 20 31 32 31 30 20 2a 2f 20 20 20 35   /*  1210 */   5
295e3 31 31 2c 20 20 32 32 32 2c 20 20 33 36 30 2c 20  11,  222,  360, 
295e4 20 34 32 33 2c 20 20 32 35 32 2c 20 20 31 38 36   423,  252,  186
295e5 2c 20 20 35 31 32 2c 20 20 35 31 30 2c 20 20 32  ,  512,  510,  2
295e6 32 31 2c 20 20 32 32 33 2c 0a 20 2f 2a 20 20 31  21,  223,. /*  1
295e7 32 32 30 20 2a 2f 20 20 20 32 33 38 2c 20 20 35  220 */   238,  5
295e8 30 37 2c 20 20 32 33 39 2c 20 20 35 33 35 2c 20  07,  239,  535, 
295e9 20 32 34 30 2c 20 20 32 39 32 2c 20 20 34 32 34   240,  292,  424
295ea 2c 20 20 35 32 39 2c 20 20 35 33 36 2c 20 20 35  ,  529,  536,  5
295eb 33 37 2c 0a 20 2f 2a 20 20 31 32 33 30 20 2a 2f  37,. /*  1230 */
295ec 20 20 20 32 39 33 2c 20 20 35 34 33 2c 20 20 31     293,  543,  1
295ed 38 37 2c 20 20 31 38 39 2c 20 20 32 34 35 2c 20  87,  189,  245, 
295ee 20 33 36 32 2c 20 20 34 38 32 2c 20 20 34 38 38   362,  482,  488
295ef 2c 20 20 32 34 37 2c 20 20 31 39 30 2c 0a 20 2f  ,  247,  190,. /
295f0 2a 20 20 31 32 34 30 20 2a 2f 20 20 20 33 36 34  *  1240 */   364
295f1 2c 20 20 20 38 39 2c 20 20 35 34 35 2c 20 20 31  ,   89,  545,  1
295f2 39 32 2c 20 20 31 31 37 2c 20 20 33 37 30 2c 20  92,  117,  370, 
295f3 20 31 33 32 2c 20 20 35 35 36 2c 20 20 35 36 33   132,  556,  563
295f4 2c 20 20 31 39 35 2c 0a 20 2f 2a 20 20 31 32 35  ,  195,. /*  125
295f5 30 20 2a 2f 20 20 20 33 38 32 2c 20 20 33 38 33  0 */   382,  383
295f6 2c 20 20 33 31 34 2c 20 20 31 33 33 2c 20 20 31  ,  314,  133,  1
295f7 33 34 2c 20 20 35 37 31 2c 20 20 31 33 38 2c 20  34,  571,  138, 
295f8 20 31 33 35 2c 20 20 31 33 36 2c 20 20 35 38 34   135,  136,  584
295f9 2c 0a 20 2f 2a 20 20 31 32 36 30 20 2a 2f 20 20  ,. /*  1260 */  
295fa 20 35 38 39 2c 20 20 35 38 35 2c 20 20 31 34 32   589,  585,  142
295fb 2c 20 20 33 39 39 2c 20 20 31 30 31 2c 20 20 34  ,  399,  101,  4
295fc 31 33 2c 20 20 32 32 30 2c 20 20 35 38 36 2c 20  13,  220,  586, 
295fd 20 32 37 30 2c 20 20 31 30 34 2c 0a 20 2f 2a 20   270,  104,. /* 
295fe 20 31 32 37 30 20 2a 2f 20 20 20 31 34 31 2c 20   1270 */   141, 
295ff 20 36 33 33 2c 20 20 36 33 34 2c 20 20 31 36 36   633,  634,  166
29600 2c 20 20 31 36 37 2c 20 20 34 34 31 2c 20 20 34  ,  167,  441,  4
29601 34 34 2c 20 20 20 37 32 2c 20 20 34 36 30 2c 20  44,   72,  460, 
29602 20 34 34 38 2c 0a 20 2f 2a 20 20 31 32 38 30 20   448,. /*  1280 
29603 2a 2f 20 20 20 34 35 37 2c 20 20 35 34 36 2c 20  */   457,  546, 
29604 20 31 34 33 2c 20 20 31 35 37 2c 20 20 20 20 36   143,  157,    6
29605 2c 20 20 34 36 31 2c 20 20 20 31 34 2c 20 20 34  ,  461,   14,  4
29606 37 39 2c 20 20 31 36 39 2c 20 20 34 36 32 2c 0a  79,  169,  462,.
29607 20 2f 2a 20 20 31 32 39 30 20 2a 2f 20 20 20 20   /*  1290 */    
29608 39 33 2c 20 20 34 36 36 2c 20 20 20 38 32 2c 20  93,  466,   82, 
29609 20 31 32 32 2c 20 20 20 31 33 2c 20 20 31 37 34   122,   13,  174
2960a 2c 20 20 34 38 37 2c 20 20 20 39 36 2c 20 20 31  ,  487,   96,  1
2960b 32 33 2c 20 20 31 36 31 2c 0a 20 2f 2a 20 20 31  23,  161,. /*  1
2960c 33 30 30 20 2a 2f 20 20 20 34 39 34 2c 20 20 34  300 */   494,  4
2960d 39 35 2c 20 20 20 38 37 2c 20 20 31 32 35 2c 20  95,   87,  125, 
2960e 20 31 32 36 2c 20 20 31 31 36 2c 20 20 32 35 38   126,  116,  258
2960f 2c 20 20 20 38 38 2c 20 20 31 32 37 2c 20 20 31  ,   88,  127,  1
29610 38 34 2c 0a 20 2f 2a 20 20 31 33 31 30 20 2a 2f  84,. /*  1310 */
29611 20 20 20 32 35 30 2c 20 20 33 36 31 2c 20 20 32     250,  361,  2
29612 31 39 2c 20 20 31 30 37 2c 20 20 35 34 34 2c 20  19,  107,  544, 
29613 20 31 34 35 2c 20 20 31 32 38 2c 20 20 31 39 33   145,  128,  193
29614 2c 20 20 33 36 35 2c 20 20 31 31 38 2c 0a 20 2f  ,  365,  118,. /
29615 2a 20 20 31 33 32 30 20 2a 2f 20 20 20 33 35 32  *  1320 */   352
29616 2c 20 20 33 35 37 2c 20 20 31 37 33 2c 20 20 32  ,  357,  173,  2
29617 37 31 2c 20 20 31 33 30 2c 20 20 20 20 39 2c 20  71,  130,    9, 
29618 20 33 31 36 2c 20 20 35 35 39 2c 20 20 31 39 37   316,  559,  197
29619 2c 20 20 20 39 30 2c 0a 20 2f 2a 20 20 31 33 33  ,   90,. /*  133
2961a 30 20 2a 2f 20 20 20 35 34 37 2c 20 20 31 33 31  0 */   547,  131
2961b 2c 20 20 31 32 39 2c 20 20 20 31 35 2c 20 20 31  ,  129,   15,  1
2961c 39 39 2c 20 20 35 35 31 2c 20 20 35 36 34 2c 20  99,  551,  564, 
2961d 20 32 30 30 2c 20 20 35 36 37 2c 20 20 32 30 31   200,  567,  201
2961e 2c 0a 20 2f 2a 20 20 31 33 34 30 20 2a 2f 20 20  ,. /*  1340 */  
2961f 20 31 33 39 2c 20 20 31 33 37 2c 20 20 35 38 32   139,  137,  582
29620 2c 20 20 20 39 31 2c 20 20 20 31 36 2c 20 20 31  ,   91,   16,  1
29621 30 36 2c 20 20 31 34 30 2c 20 20 32 30 38 2c 20  06,  140,  208, 
29622 20 35 37 34 2c 20 20 33 39 32 2c 0a 20 2f 2a 20   574,  392,. /* 
29623 20 31 33 35 30 20 2a 2f 20 20 20 33 39 36 2c 20   1350 */   396, 
29624 20 32 39 30 2c 20 20 31 34 38 2c 20 20 35 39 36   290,  148,  596
29625 2c 0a 7d 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73  ,.};.static cons
29626 74 20 59 59 43 4f 44 45 54 59 50 45 20 79 79 5f  t YYCODETYPE yy_
29627 6c 6f 6f 6b 61 68 65 61 64 5b 5d 20 3d 20 7b 0a  lookahead[] = {.
29628 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20 20 20 20   /*     0 */    
29629 31 36 2c 20 20 31 34 30 2c 20 20 31 34 31 2c 20  16,  140,  141, 
2962a 20 31 34 32 2c 20 20 31 34 33 2c 20 20 20 32 31   142,  143,   21
2962b 2c 20 20 20 32 33 2c 20 20 20 32 33 2c 20 20 20  ,   23,   23,   
2962c 36 39 2c 20 20 20 37 30 2c 0a 20 2f 2a 20 20 20  69,   70,. /*   
2962d 20 31 30 20 2a 2f 20 20 20 20 37 31 2c 20 20 20   10 */    71,   
2962e 37 32 2c 20 20 31 31 30 2c 20 20 20 37 34 2c 20  72,  110,   74, 
2962f 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37    75,   76,   77
29630 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20  ,   78,   79,   
29631 38 30 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f  80,. /*    20 */
29632 20 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20      81,   82,   
29633 38 33 2c 20 20 20 38 34 2c 20 20 20 20 31 2c 20  83,   84,    1, 
29634 20 20 20 32 2c 20 20 20 34 32 2c 20 20 20 34 33     2,   42,   43
29635 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 0a 20 2f  ,   73,   74,. /
29636 2a 20 20 20 20 33 30 20 2a 2f 20 20 20 20 37 35  *    30 */    75
29637 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20  ,   76,   77,   
29638 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20  78,   79,   80, 
29639 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33    81,   82,   83
2963a 2c 20 20 20 38 34 2c 0a 20 2f 2a 20 20 20 20 34  ,   84,. /*    4
2963b 30 20 2a 2f 20 20 20 20 35 38 2c 20 20 31 36 32  0 */    58,  162
2963c 2c 20 20 31 36 33 2c 20 20 31 36 34 2c 20 20 20  ,  163,  164,   
2963d 36 30 2c 20 20 20 36 31 2c 20 20 20 36 32 2c 20  60,   61,   62, 
2963e 20 20 36 33 2c 20 20 20 36 34 2c 20 20 20 36 35    63,   64,   65
2963f 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20  ,. /*    50 */  
29640 20 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38    66,   67,   68
29641 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20  ,   69,   70,   
29642 37 31 2c 20 20 20 37 32 2c 20 20 31 34 38 2c 20  71,   72,  148, 
29643 20 20 37 34 2c 20 20 20 37 35 2c 0a 20 2f 2a 20    74,   75,. /* 
29644 20 20 20 36 30 20 2a 2f 20 20 20 20 37 36 2c 20     60 */    76, 
29645 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39    77,   78,   79
29646 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20  ,   80,   81,   
29647 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20  82,   83,   84, 
29648 20 20 31 36 2c 0a 20 2f 2a 20 20 20 20 37 30 20    16,. /*    70 
29649 2a 2f 20 20 20 20 38 38 2c 20 20 20 38 38 2c 20  */    88,   88, 
2964a 20 20 38 38 2c 20 20 20 38 34 2c 20 20 20 39 32    88,   84,   92
2964b 2c 20 20 20 32 32 2c 20 20 32 31 39 2c 20 20 32  ,   22,  219,  2
2964c 32 30 2c 20 20 32 32 31 2c 20 20 32 32 32 2c 0a  20,  221,  222,.
2964d 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 20   /*    80 */    
2964e 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20  74,   75,   76, 
2964f 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39    77,   78,   79
29650 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20  ,   80,   81,   
29651 38 32 2c 20 20 20 38 33 2c 0a 20 2f 2a 20 20 20  82,   83,. /*   
29652 20 39 30 20 2a 2f 20 20 20 20 38 34 2c 20 20 32   90 */    84,  2
29653 31 39 2c 20 20 31 38 33 2c 20 20 32 32 31 2c 20  19,  183,  221, 
29654 20 32 32 32 2c 20 20 20 34 32 2c 20 20 20 34 33   222,   42,   43
29655 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20  ,   78,   79,   
29656 34 36 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f  46,. /*   100 */
29657 20 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20      78,   79,   
29658 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20  80,   81,   82, 
29659 20 20 38 33 2c 20 20 20 38 34 2c 20 20 31 32 35    83,   84,  125
2965a 2c 20 20 31 32 36 2c 20 20 31 32 37 2c 0a 20 2f  ,  126,  127,. /
2965b 2a 20 20 20 31 31 30 20 2a 2f 20 20 20 31 37 30  *   110 */   170
2965c 2c 20 20 32 33 39 2c 20 20 20 31 36 2c 20 20 20  ,  239,   16,   
2965d 36 30 2c 20 20 20 36 31 2c 20 20 20 36 32 2c 20  60,   61,   62, 
2965e 20 20 36 33 2c 20 20 20 36 34 2c 20 20 20 36 35    63,   64,   65
2965f 2c 20 20 20 36 36 2c 0a 20 2f 2a 20 20 20 31 32  ,   66,. /*   12
29660 30 20 2a 2f 20 20 20 20 36 37 2c 20 20 20 36 38  0 */    67,   68
29661 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20  ,   69,   70,   
29662 37 31 2c 20 20 20 37 32 2c 20 20 31 34 38 2c 20  71,   72,  148, 
29663 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36    74,   75,   76
29664 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20  ,. /*   130 */  
29665 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39    77,   78,   79
29666 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20  ,   80,   81,   
29667 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20  82,   83,   84, 
29668 20 20 34 32 2c 20 20 20 34 33 2c 0a 20 2f 2a 20    42,   43,. /* 
29669 20 20 31 34 30 20 2a 2f 20 20 20 20 34 34 2c 20    140 */    44, 
2966a 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32    80,   81,   82
2966b 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20  ,   83,   84,   
2966c 32 33 2c 20 20 31 34 38 2c 20 20 31 37 30 2c 20  23,  148,  170, 
2966d 20 31 37 31 2c 0a 20 2f 2a 20 20 20 31 35 30 20   171,. /*   150 
2966e 2a 2f 20 20 20 20 31 39 2c 20 20 20 38 33 2c 20  */    19,   83, 
2966f 20 20 38 34 2c 20 20 31 35 36 2c 20 20 20 32 33    84,  156,   23
29670 2c 20 20 31 37 30 2c 20 20 20 36 30 2c 20 20 20  ,  170,   60,   
29671 36 31 2c 20 20 20 36 32 2c 20 20 20 36 33 2c 0a  61,   62,   63,.
29672 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20 20 20 20   /*   160 */    
29673 36 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20  64,   65,   66, 
29674 20 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39    67,   68,   69
29675 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20  ,   70,   71,   
29676 37 32 2c 20 20 20 31 39 2c 0a 20 2f 2a 20 20 20  72,   19,. /*   
29677 31 37 30 20 2a 2f 20 20 20 20 37 34 2c 20 20 20  170 */    74,   
29678 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20  75,   76,   77, 
29679 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30    78,   79,   80
2967a 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20  ,   81,   82,   
2967b 38 33 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f  83,. /*   180 */
2967c 20 20 20 20 38 34 2c 20 20 20 31 36 2c 20 20 31      84,   16,  1
2967d 38 33 2c 20 20 31 34 38 2c 20 20 31 35 31 2c 20  83,  148,  151, 
2967e 20 31 34 38 2c 20 20 20 32 31 2c 20 20 31 39 30   148,   21,  190
2967f 2c 20 20 31 34 38 2c 20 20 20 35 38 2c 0a 20 2f  ,  148,   58,. /
29680 2a 20 20 20 31 39 30 20 2a 2f 20 20 20 31 36 39  *   190 */   169
29681 2c 20 20 32 31 33 2c 20 20 31 35 37 2c 20 20 31  ,  213,  157,  1
29682 35 38 2c 20 20 31 38 36 2c 20 20 31 38 37 2c 20  58,  186,  187, 
29683 20 32 31 38 2c 20 20 20 32 33 2c 20 20 31 37 37   218,   23,  177
29684 2c 20 20 32 30 32 2c 0a 20 2f 2a 20 20 20 32 30  ,  202,. /*   20
29685 30 20 2a 2f 20 20 20 32 30 33 2c 20 20 20 37 38  0 */   203,   78
29686 2c 20 20 20 37 39 2c 20 20 31 36 36 2c 20 20 31  ,   79,  166,  1
29687 36 37 2c 20 20 32 30 38 2c 20 20 31 36 31 2c 20  67,  208,  161, 
29688 20 20 34 32 2c 20 20 20 34 33 2c 20 20 20 37 38    42,   43,   78
29689 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20  ,. /*   210 */  
2968a 20 20 37 39 2c 20 20 20 38 38 2c 20 20 20 38 39    79,   88,   89
2968b 2c 20 20 31 37 37 2c 20 20 31 37 38 2c 20 20 31  ,  177,  178,  1
2968c 37 30 2c 20 20 31 38 31 2c 20 20 31 38 32 2c 20  70,  181,  182, 
2968d 20 20 36 38 2c 20 20 20 38 38 2c 0a 20 2f 2a 20    68,   88,. /* 
2968e 20 20 32 32 30 20 2a 2f 20 20 20 31 38 34 2c 20    220 */   184, 
2968f 20 20 39 38 2c 20 20 20 39 39 2c 20 20 20 39 32    98,   99,   92
29690 2c 20 20 20 31 36 2c 20 20 20 36 30 2c 20 20 20  ,   16,   60,   
29691 36 31 2c 20 20 20 36 32 2c 20 20 20 36 33 2c 20  61,   62,   63, 
29692 20 20 36 34 2c 0a 20 2f 2a 20 20 20 32 33 30 20    64,. /*   230 
29693 2a 2f 20 20 20 20 36 35 2c 20 20 20 36 36 2c 20  */    65,   66, 
29694 20 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39    67,   68,   69
29695 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20  ,   70,   71,   
29696 37 32 2c 20 20 20 32 32 2c 20 20 20 37 34 2c 0a  72,   22,   74,.
29697 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20 20 20 20   /*   240 */    
29698 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20  75,   76,   77, 
29699 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30    78,   79,   80
2969a 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20  ,   81,   82,   
2969b 38 33 2c 20 20 20 38 34 2c 0a 20 2f 2a 20 20 20  83,   84,. /*   
2969c 32 35 30 20 2a 2f 20 20 20 20 34 32 2c 20 20 20  250 */    42,   
2969d 34 33 2c 20 20 20 37 38 2c 20 20 20 20 30 2c 20  43,   78,    0, 
2969e 20 20 20 31 2c 20 20 20 20 32 2c 20 20 31 32 35     1,    2,  125
2969f 2c 20 20 31 32 36 2c 20 20 31 32 37 2c 20 20 32  ,  126,  127,  2
296a0 32 36 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f  26,. /*   260 */
296a1 20 20 20 20 31 31 2c 20 20 31 36 32 2c 20 20 20      11,  162,   
296a2 38 38 2c 20 20 20 38 39 2c 20 20 32 33 31 2c 20  88,   89,  231, 
296a3 20 32 32 38 2c 20 20 32 32 39 2c 20 20 20 31 36   228,  229,   16
296a4 2c 20 20 20 36 30 2c 20 20 20 36 31 2c 0a 20 2f  ,   60,   61,. /
296a5 2a 20 20 20 32 37 30 20 2a 2f 20 20 20 20 36 32  *   270 */    62
296a6 2c 20 20 20 36 33 2c 20 20 20 36 34 2c 20 20 20  ,   63,   64,   
296a7 36 35 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20  65,   66,   67, 
296a8 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30    68,   69,   70
296a9 2c 20 20 20 37 31 2c 0a 20 2f 2a 20 20 20 32 38  ,   71,. /*   28
296aa 30 20 2a 2f 20 20 20 20 37 32 2c 20 20 31 34 38  0 */    72,  148
296ab 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20  ,   74,   75,   
296ac 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20  76,   77,   78, 
296ad 20 20 37 39 2c 20 20 20 38 30 2c 20 20 20 38 31    79,   80,   81
296ae 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20  ,. /*   290 */  
296af 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34    82,   83,   84
296b0 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20 31  ,   42,   43,  1
296b1 38 36 2c 20 20 31 38 37 2c 20 20 20 31 36 2c 20  86,  187,   16, 
296b2 20 20 34 39 2c 20 20 31 34 38 2c 0a 20 2f 2a 20    49,  148,. /* 
296b3 20 20 33 30 30 20 2a 2f 20 20 20 32 30 31 2c 20    300 */   201, 
296b4 20 20 34 39 2c 20 20 20 31 38 2c 20 20 31 37 30    49,   18,  170
296b5 2c 20 20 31 37 31 2c 20 20 31 35 34 2c 20 20 31  ,  171,  154,  1
296b6 34 32 2c 20 20 31 34 33 2c 20 20 31 35 37 2c 20  42,  143,  157, 
296b7 20 31 35 38 2c 0a 20 2f 2a 20 20 20 33 31 30 20   158,. /*   310 
296b8 2a 2f 20 20 20 31 38 32 2c 20 20 20 36 30 2c 20  */   182,   60, 
296b9 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20 36 33    61,   62,   63
296ba 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20  ,   64,   65,   
296bb 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38 2c 0a  66,   67,   68,.
296bc 20 2f 2a 20 20 20 33 32 30 20 2a 2f 20 20 20 20   /*   320 */    
296bd 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20  69,   70,   71, 
296be 20 20 37 32 2c 20 20 20 39 31 2c 20 20 20 37 34    72,   91,   74
296bf 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20  ,   75,   76,   
296c0 37 37 2c 20 20 20 37 38 2c 0a 20 2f 2a 20 20 20  77,   78,. /*   
296c1 33 33 30 20 2a 2f 20 20 20 20 37 39 2c 20 20 20  330 */    79,   
296c2 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20  80,   81,   82, 
296c3 20 20 38 33 2c 20 20 20 38 34 2c 20 20 31 36 38    83,   84,  168
296c4 2c 20 20 31 36 39 2c 20 20 31 32 32 2c 20 20 20  ,  169,  122,   
296c5 35 35 2c 0a 20 2f 2a 20 20 20 33 34 30 20 2a 2f  55,. /*   340 */
296c6 20 20 20 31 33 32 2c 20 20 20 31 36 2c 20 20 20     132,   16,   
296c7 31 36 2c 20 20 31 31 30 2c 20 20 20 31 36 2c 20  16,  110,   16, 
296c8 20 31 37 37 2c 20 20 20 32 30 2c 20 20 20 32 30   177,   20,   20
296c9 2c 20 20 20 39 39 2c 20 20 31 30 30 2c 0a 20 2f  ,   99,  100,. /
296ca 2a 20 20 20 33 35 30 20 2a 2f 20 20 20 31 30 31  *   350 */   101
296cb 2c 20 20 20 39 39 2c 20 20 31 30 30 2c 20 20 31  ,   99,  100,  1
296cc 30 31 2c 20 20 20 38 30 2c 20 20 20 31 32 2c 20  01,   80,   12, 
296cd 20 32 32 33 2c 20 20 31 32 34 2c 20 20 31 34 38   223,  124,  148
296ce 2c 20 20 31 31 30 2c 0a 20 2f 2a 20 20 20 33 36  ,  110,. /*   36
296cf 30 20 2a 2f 20 20 20 31 38 32 2c 20 20 31 34 38  0 */   182,  148
296d0 2c 20 20 31 31 30 2c 20 20 20 32 33 2c 20 20 20  ,  110,   23,   
296d1 31 39 2c 20 20 20 38 34 2c 20 20 20 32 31 2c 20  19,   84,   21, 
296d2 20 20 32 34 2c 20 20 20 34 32 2c 20 20 20 34 33    24,   42,   43
296d3 2c 0a 20 2f 2a 20 20 20 33 37 30 20 2a 2f 20 20  ,. /*   370 */  
296d4 20 31 34 38 2c 20 20 20 39 30 2c 20 20 20 39 31   148,   90,   91
296d5 2c 20 20 20 39 32 2c 20 20 20 39 33 2c 20 20 20  ,   92,   93,   
296d6 39 34 2c 20 20 20 39 35 2c 20 20 20 39 36 2c 20  94,   95,   96, 
296d7 20 20 39 34 2c 20 20 31 36 36 2c 0a 20 2f 2a 20    94,  166,. /* 
296d8 20 20 33 38 30 20 2a 2f 20 20 20 20 33 37 2c 20    380 */    37, 
296d9 20 31 30 37 2c 20 20 20 33 39 2c 20 20 31 33 32   107,   39,  132
296da 2c 20 20 31 30 33 2c 20 20 31 31 31 2c 20 20 20  ,  103,  111,   
296db 36 30 2c 20 20 20 36 31 2c 20 20 20 36 32 2c 20  60,   61,   62, 
296dc 20 20 36 33 2c 0a 20 2f 2a 20 20 20 33 39 30 20    63,. /*   390 
296dd 2a 2f 20 20 20 20 36 34 2c 20 20 20 36 35 2c 20  */    64,   65, 
296de 20 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38    66,   67,   68
296df 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20  ,   69,   70,   
296e0 37 31 2c 20 20 20 37 32 2c 20 20 31 38 39 2c 0a  71,   72,  189,.
296e1 20 2f 2a 20 20 20 34 30 30 20 2a 2f 20 20 20 20   /*   400 */    
296e2 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20  74,   75,   76, 
296e3 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39    77,   78,   79
296e4 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20  ,   80,   81,   
296e5 38 32 2c 20 20 20 38 33 2c 0a 20 2f 2a 20 20 20  82,   83,. /*   
296e6 34 31 30 20 2a 2f 20 20 20 20 38 34 2c 20 20 20  410 */    84,   
296e7 31 36 2c 20 20 20 38 34 2c 20 20 31 35 36 2c 20  16,   84,  156, 
296e8 20 20 39 32 2c 20 20 20 32 30 2c 20 20 20 32 33    92,   20,   23
296e9 2c 20 20 20 39 32 2c 20 20 31 33 34 2c 20 20 20  ,   92,  134,   
296ea 39 31 2c 0a 20 2f 2a 20 20 20 34 32 30 20 2a 2f  91,. /*   420 */
296eb 20 20 20 20 39 32 2c 20 20 20 39 33 2c 20 20 20      92,   93,   
296ec 39 34 2c 20 20 20 39 35 2c 20 20 20 39 36 2c 20  94,   95,   96, 
296ed 20 31 30 33 2c 20 20 31 34 38 2c 20 20 20 32 32   103,  148,   22
296ee 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 0a 20 2f  ,   88,   89,. /
296ef 2a 20 20 20 34 33 30 20 2a 2f 20 20 20 31 34 38  *   430 */   148
296f0 2c 20 20 31 30 33 2c 20 20 32 31 30 2c 20 20 31  ,  103,  210,  1
296f1 30 36 2c 20 20 31 35 36 2c 20 20 31 30 38 2c 20  06,  156,  108, 
296f2 20 31 30 39 2c 20 20 20 34 32 2c 20 20 20 34 33   109,   42,   43
296f3 2c 20 20 31 34 34 2c 0a 20 2f 2a 20 20 20 34 34  ,  144,. /*   44
296f4 30 20 2a 2f 20 20 20 31 34 35 2c 20 20 32 32 38  0 */   145,  228
296f5 2c 20 20 32 32 39 2c 20 20 32 31 39 2c 20 20 31  ,  229,  219,  1
296f6 34 39 2c 20 20 32 32 31 2c 20 20 32 32 32 2c 20  49,  221,  222, 
296f7 20 31 39 30 2c 20 20 31 37 30 2c 20 20 31 37 31   190,  170,  171
296f8 2c 0a 20 2f 2a 20 20 20 34 35 30 20 2a 2f 20 20  ,. /*   450 */  
296f9 20 32 34 30 2c 20 20 31 35 36 2c 20 20 31 37 30   240,  156,  170
296fa 2c 20 20 31 37 31 2c 20 20 31 36 32 2c 20 20 20  ,  171,  162,   
296fb 36 30 2c 20 20 20 36 31 2c 20 20 20 36 32 2c 20  60,   61,   62, 
296fc 20 20 36 33 2c 20 20 20 36 34 2c 0a 20 2f 2a 20    63,   64,. /* 
296fd 20 20 34 36 30 20 2a 2f 20 20 20 20 36 35 2c 20    460 */    65, 
296fe 20 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38    66,   67,   68
296ff 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20  ,   69,   70,   
29700 37 31 2c 20 20 20 37 32 2c 20 20 31 39 30 2c 20  71,   72,  190, 
29701 20 20 37 34 2c 0a 20 2f 2a 20 20 20 34 37 30 20    74,. /*   470 
29702 2a 2f 20 20 20 20 37 35 2c 20 20 20 37 36 2c 20  */    75,   76, 
29703 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39    77,   78,   79
29704 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20  ,   80,   81,   
29705 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 0a  82,   83,   84,.
29706 20 2f 2a 20 20 20 34 38 30 20 2a 2f 20 20 20 20   /*   480 */    
29707 31 36 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20  16,   88,   89, 
29708 20 20 39 30 2c 20 20 20 32 30 2c 20 20 31 39 30    90,   20,  190
29709 2c 20 20 20 39 33 2c 20 20 20 39 34 2c 20 20 20  ,   93,   94,   
2970a 39 35 2c 20 20 20 32 33 2c 0a 20 2f 2a 20 20 20  95,   23,. /*   
2970b 34 39 30 20 2a 2f 20 20 20 31 33 31 2c 20 20 32  490 */   131,  2
2970c 31 33 2c 20 20 31 33 33 2c 20 20 32 30 31 2c 20  13,  133,  201, 
2970d 20 32 31 32 2c 20 20 20 39 30 2c 20 20 32 31 38   212,   90,  218
2970e 2c 20 20 31 30 34 2c 20 20 20 39 33 2c 20 20 20  ,  104,   93,   
2970f 39 34 2c 0a 20 2f 2a 20 20 20 35 30 30 20 2a 2f  94,. /*   500 */
29710 20 20 20 20 39 35 2c 20 20 20 34 32 2c 20 20 20      95,   42,   
29711 34 33 2c 20 20 20 20 37 2c 20 20 20 20 38 2c 20  43,    7,    8, 
29712 20 20 20 39 2c 20 20 20 34 32 2c 20 20 20 34 33     9,   42,   43
29713 2c 20 20 31 39 31 2c 20 20 31 30 34 2c 0a 20 2f  ,  191,  104,. /
29714 2a 20 20 20 35 31 30 20 2a 2f 20 20 20 32 31 35  *   510 */   215
29715 2c 20 20 32 31 39 2c 20 20 31 35 39 2c 20 20 32  ,  219,  159,  2
29716 32 31 2c 20 20 32 32 32 2c 20 20 31 36 32 2c 20  21,  222,  162, 
29717 20 31 36 33 2c 20 20 31 36 34 2c 20 20 31 35 36   163,  164,  156
29718 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 20 35 32  ,  165,. /*   52
29719 30 20 2a 2f 20 20 20 31 36 36 2c 20 20 31 36 37  0 */   166,  167
2971a 2c 20 20 20 36 33 2c 20 20 20 36 34 2c 20 20 20  ,   63,   64,   
2971b 36 30 2c 20 20 20 36 31 2c 20 20 20 36 32 2c 20  60,   61,   62, 
2971c 20 20 36 33 2c 20 20 20 36 34 2c 20 20 20 36 35    63,   64,   65
2971d 2c 0a 20 2f 2a 20 20 20 35 33 30 20 2a 2f 20 20  ,. /*   530 */  
2971e 20 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38    66,   67,   68
2971f 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20  ,   69,   70,   
29720 37 31 2c 20 20 20 37 32 2c 20 20 32 34 32 2c 20  71,   72,  242, 
29721 20 20 37 34 2c 20 20 20 37 35 2c 0a 20 2f 2a 20    74,   75,. /* 
29722 20 20 35 34 30 20 2a 2f 20 20 20 20 37 36 2c 20    540 */    76, 
29723 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39    77,   78,   79
29724 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20  ,   80,   81,   
29725 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20  82,   83,   84, 
29726 20 20 31 36 2c 0a 20 2f 2a 20 20 20 35 35 30 20    16,. /*   550 
29727 2a 2f 20 20 20 31 34 38 2c 20 20 20 39 32 2c 20  */   148,   92, 
29728 20 31 39 30 2c 20 20 20 32 30 2c 20 20 20 38 38   190,   20,   88
29729 2c 20 20 20 38 39 2c 20 20 31 34 38 2c 20 20 20  ,   89,  148,   
2972a 39 30 2c 20 20 31 35 36 2c 20 20 31 34 38 2c 0a  90,  156,  148,.
2972b 20 2f 2a 20 20 20 35 36 30 20 2a 2f 20 20 20 20   /*   560 */    
2972c 39 33 2c 20 20 20 39 34 2c 20 20 20 39 35 2c 20  93,   94,   95, 
2972d 20 20 32 30 2c 20 20 31 38 37 2c 20 20 31 34 38    20,  187,  148
2972e 2c 20 20 20 31 39 2c 20 20 20 32 30 2c 20 20 31  ,   19,   20,  1
2972f 34 38 2c 20 20 20 32 32 2c 0a 20 2f 2a 20 20 20  48,   22,. /*   
29730 35 37 30 20 2a 2f 20 20 20 20 31 39 2c 20 20 31  570 */    19,  1
29731 30 34 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20  04,  170,  171, 
29732 20 20 32 33 2c 20 20 20 34 32 2c 20 20 20 34 33    23,   42,   43
29733 2c 20 20 31 34 38 2c 20 20 31 37 30 2c 20 20 31  ,  148,  170,  1
29734 37 31 2c 0a 20 2f 2a 20 20 20 35 38 30 20 2a 2f  71,. /*   580 */
29735 20 20 20 31 31 34 2c 20 20 31 37 30 2c 20 20 31     114,  170,  1
29736 37 31 2c 20 20 32 32 39 2c 20 20 20 39 39 2c 20  71,  229,   99, 
29737 20 31 30 30 2c 20 20 31 30 31 2c 20 20 31 37 30   100,  101,  170
29738 2c 20 20 31 37 31 2c 20 20 31 34 38 2c 0a 20 2f  ,  171,  148,. /
29739 2a 20 20 20 35 39 30 20 2a 2f 20 20 20 31 37 30  *   590 */   170
2973a 2c 20 20 31 37 31 2c 20 20 31 39 30 2c 20 20 20  ,  171,  190,   
2973b 36 30 2c 20 20 20 36 31 2c 20 20 20 36 32 2c 20  60,   61,   62, 
2973c 20 20 36 33 2c 20 20 20 36 34 2c 20 20 20 36 35    63,   64,   65
2973d 2c 20 20 20 36 36 2c 0a 20 2f 2a 20 20 20 36 30  ,   66,. /*   60
2973e 30 20 2a 2f 20 20 20 20 36 37 2c 20 20 20 36 38  0 */    67,   68
2973f 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20  ,   69,   70,   
29740 37 31 2c 20 20 20 37 32 2c 20 20 20 35 39 2c 20  71,   72,   59, 
29741 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36    74,   75,   76
29742 2c 0a 20 2f 2a 20 20 20 36 31 30 20 2a 2f 20 20  ,. /*   610 */  
29743 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39    77,   78,   79
29744 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20  ,   80,   81,   
29745 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20  82,   83,   84, 
29746 20 20 31 36 2c 20 20 32 31 31 2c 0a 20 2f 2a 20    16,  211,. /* 
29747 20 20 36 32 30 20 2a 2f 20 20 20 32 31 32 2c 20    620 */   212, 
29748 20 20 31 32 2c 20 20 31 34 38 2c 20 20 20 32 31    12,  148,   21
29749 2c 20 20 32 31 33 2c 20 20 31 36 35 2c 20 20 31  ,  213,  165,  1
2974a 36 36 2c 20 20 31 36 37 2c 20 20 20 32 31 2c 20  66,  167,   21, 
2974b 20 31 34 38 2c 0a 20 2f 2a 20 20 20 36 33 30 20   148,. /*   630 
2974c 2a 2f 20 20 20 20 32 33 2c 20 20 31 34 38 2c 20  */    23,  148, 
2974d 20 31 34 38 2c 20 20 20 32 34 2c 20 20 32 31 37   148,   24,  217
2974e 2c 20 20 20 31 39 2c 20 20 20 32 30 2c 20 20 32  ,   19,   20,  2
2974f 31 37 2c 20 20 20 32 32 2c 20 20 20 38 38 2c 0a  17,   22,   88,.
29750 20 2f 2a 20 20 20 36 34 30 20 2a 2f 20 20 20 20   /*   640 */    
29751 38 39 2c 20 20 31 35 37 2c 20 20 31 35 38 2c 20  89,  157,  158, 
29752 20 32 31 34 2c 20 20 20 34 32 2c 20 20 20 34 33   214,   42,   43
29753 2c 20 20 20 33 37 2c 20 20 31 34 38 2c 20 20 20  ,   37,  148,   
29754 33 39 2c 20 20 31 30 36 2c 0a 20 2f 2a 20 20 20  39,  106,. /*   
29755 36 35 30 20 2a 2f 20 20 20 31 34 38 2c 20 20 31  650 */   148,  1
29756 30 38 2c 20 20 31 30 39 2c 20 20 31 37 30 2c 20  08,  109,  170, 
29757 20 31 37 31 2c 20 20 20 32 30 2c 20 20 31 34 36   171,   20,  146
29758 2c 20 20 31 38 33 2c 20 20 20 34 39 2c 20 20 31  ,  183,   49,  1
29759 35 36 2c 0a 20 2f 2a 20 20 20 36 36 30 20 2a 2f  56,. /*   660 */
2975a 20 20 20 20 31 34 2c 20 20 20 31 36 2c 20 20 20      14,   16,   
2975b 36 30 2c 20 20 20 36 31 2c 20 20 20 36 32 2c 20  60,   61,   62, 
2975c 20 20 36 33 2c 20 20 20 36 34 2c 20 20 20 36 35    63,   64,   65
2975d 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 0a 20 2f  ,   66,   67,. /
2975e 2a 20 20 20 36 37 30 20 2a 2f 20 20 20 20 36 38  *   670 */    68
2975f 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20  ,   69,   70,   
29760 37 31 2c 20 20 20 37 32 2c 20 20 20 35 39 2c 20  71,   72,   59, 
29761 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36    74,   75,   76
29762 2c 20 20 20 37 37 2c 0a 20 2f 2a 20 20 20 36 38  ,   77,. /*   68
29763 30 20 2a 2f 20 20 20 20 37 38 2c 20 20 20 37 39  0 */    78,   79
29764 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20  ,   80,   81,   
29765 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20  82,   83,   84, 
29766 20 20 34 32 2c 20 20 20 34 33 2c 20 20 32 32 39    42,   43,  229
29767 2c 0a 20 2f 2a 20 20 20 36 39 30 20 2a 2f 20 20  ,. /*   690 */  
29768 20 20 39 39 2c 20 20 31 30 30 2c 20 20 31 30 31    99,  100,  101
29769 2c 20 20 31 39 30 2c 20 20 31 30 36 2c 20 20 20  ,  190,  106,   
2976a 38 38 2c 20 20 31 30 38 2c 20 20 31 30 39 2c 20  88,  108,  109, 
2976b 20 20 35 32 2c 20 20 20 31 34 2c 0a 20 2f 2a 20    52,   14,. /* 
2976c 20 20 37 30 30 20 2a 2f 20 20 20 20 35 34 2c 20    700 */    54, 
2976d 20 31 34 38 2c 20 20 31 35 36 2c 20 20 31 36 32   148,  156,  162
2976e 2c 20 20 20 31 36 2c 20 20 20 36 30 2c 20 20 20  ,   16,   60,   
2976f 36 31 2c 20 20 20 36 32 2c 20 20 20 36 33 2c 20  61,   62,   63, 
29770 20 20 36 34 2c 0a 20 2f 2a 20 20 20 37 31 30 20    64,. /*   710 
29771 2a 2f 20 20 20 20 36 35 2c 20 20 20 36 36 2c 20  */    65,   66, 
29772 20 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39    67,   68,   69
29773 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20  ,   70,   71,   
29774 37 32 2c 20 20 32 31 35 2c 20 20 20 37 34 2c 0a  72,  215,   74,.
29775 20 2f 2a 20 20 20 37 32 30 20 2a 2f 20 20 20 20   /*   720 */    
29776 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20  75,   76,   77, 
29777 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30    78,   79,   80
29778 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20  ,   81,   82,   
29779 38 33 2c 20 20 20 38 34 2c 0a 20 2f 2a 20 20 20  83,   84,. /*   
2977a 37 33 30 20 2a 2f 20 20 20 20 34 32 2c 20 20 20  730 */    42,   
2977b 34 33 2c 20 20 32 34 35 2c 20 20 32 34 36 2c 20  43,  245,  246, 
2977c 20 31 34 38 2c 20 20 31 34 38 2c 20 20 31 39 30   148,  148,  190
2977d 2c 20 20 20 35 32 2c 20 20 31 39 33 2c 20 20 20  ,   52,  193,   
2977e 35 34 2c 0a 20 2f 2a 20 20 20 37 34 30 20 2a 2f  54,. /*   740 */
2977f 20 20 20 32 33 37 2c 20 20 31 30 36 2c 20 20 32     237,  106,  2
29780 30 31 2c 20 20 31 30 38 2c 20 20 31 30 39 2c 20  01,  108,  109, 
29781 20 32 30 30 2c 20 20 31 34 38 2c 20 20 20 31 36   200,  148,   16
29782 2c 20 20 20 36 30 2c 20 20 20 36 31 2c 0a 20 2f  ,   60,   61,. /
29783 2a 20 20 20 37 35 30 20 2a 2f 20 20 20 20 36 32  *   750 */    62
29784 2c 20 20 20 36 33 2c 20 20 20 36 34 2c 20 20 20  ,   63,   64,   
29785 36 35 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20  65,   66,   67, 
29786 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30    68,   69,   70
29787 2c 20 20 20 37 31 2c 0a 20 2f 2a 20 20 20 37 36  ,   71,. /*   76
29788 30 20 2a 2f 20 20 20 20 37 32 2c 20 20 32 31 35  0 */    72,  215
29789 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20  ,   74,   75,   
2978a 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20  76,   77,   78, 
2978b 20 20 37 39 2c 20 20 20 38 30 2c 20 20 20 38 31    79,   80,   81
2978c 2c 0a 20 2f 2a 20 20 20 37 37 30 20 2a 2f 20 20  ,. /*   770 */  
2978d 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34    82,   83,   84
2978e 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20 20  ,   42,   43,   
2978f 32 35 2c 20 20 31 38 39 2c 20 20 20 32 32 2c 20  25,  189,   22, 
29790 20 32 33 32 2c 20 20 20 32 39 2c 0a 20 2f 2a 20   232,   29,. /* 
29791 20 20 37 38 30 20 2a 2f 20 20 20 31 33 34 2c 20    780 */   134, 
29792 20 31 38 33 2c 20 20 20 31 36 2c 20 20 32 33 37   183,   16,  237
29793 2c 20 20 32 30 32 2c 20 20 32 30 33 2c 20 20 31  ,  202,  203,  1
29794 34 38 2c 20 20 31 34 38 2c 20 20 31 39 32 2c 20  48,  148,  192, 
29795 20 31 34 38 2c 0a 20 2f 2a 20 20 20 37 39 30 20   148,. /*   790 
29796 2a 2f 20 20 20 20 31 36 2c 20 20 20 34 31 2c 20  */    16,   41, 
29797 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20 36 33    61,   62,   63
29798 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20  ,   64,   65,   
29799 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38 2c 0a  66,   67,   68,.
2979a 20 2f 2a 20 20 20 38 30 30 20 2a 2f 20 20 20 20   /*   800 */    
2979b 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20  69,   70,   71, 
2979c 20 20 37 32 2c 20 20 31 34 38 2c 20 20 20 37 34    72,  148,   74
2979d 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20  ,   75,   76,   
2979e 37 37 2c 20 20 20 37 38 2c 0a 20 2f 2a 20 20 20  77,   78,. /*   
2979f 38 31 30 20 2a 2f 20 20 20 20 37 39 2c 20 20 20  810 */    79,   
297a0 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20  80,   81,   82, 
297a1 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 34 32    83,   84,   42
297a2 2c 20 20 20 34 33 2c 20 20 20 32 33 2c 20 20 20  ,   43,   23,   
297a3 31 39 2c 0a 20 2f 2a 20 20 20 38 32 30 20 2a 2f  19,. /*   820 */
297a4 20 20 20 20 32 33 2c 20 20 31 38 33 2c 20 20 31      23,  183,  1
297a5 34 38 2c 20 20 20 32 33 2c 20 20 31 34 38 2c 20  48,   23,  148, 
297a6 20 31 34 38 2c 20 20 31 37 30 2c 20 20 31 37 31   148,  170,  171
297a7 2c 20 20 31 38 39 2c 20 20 20 31 39 2c 0a 20 2f  ,  189,   19,. /
297a8 2a 20 20 20 38 33 30 20 2a 2f 20 20 20 31 38 39  *   830 */   189
297a9 2c 20 20 31 30 37 2c 20 20 31 34 38 2c 20 20 20  ,  107,  148,   
297aa 32 33 2c 20 20 31 34 38 2c 20 20 31 31 31 2c 20  23,  148,  111, 
297ab 20 20 36 32 2c 20 20 20 36 33 2c 20 20 20 36 34    62,   63,   64
297ac 2c 20 20 20 36 35 2c 0a 20 2f 2a 20 20 20 38 34  ,   65,. /*   84
297ad 30 20 2a 2f 20 20 20 20 36 36 2c 20 20 20 36 37  0 */    66,   67
297ae 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20  ,   68,   69,   
297af 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20  70,   71,   72, 
297b0 20 31 34 38 2c 20 20 20 37 34 2c 20 20 20 37 35   148,   74,   75
297b1 2c 0a 20 2f 2a 20 20 20 38 35 30 20 2a 2f 20 20  ,. /*   850 */  
297b2 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38    76,   77,   78
297b3 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 20  ,   79,   80,   
297b4 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20  81,   82,   83, 
297b5 20 20 38 34 2c 20 20 20 31 36 2c 0a 20 2f 2a 20    84,   16,. /* 
297b6 20 20 38 36 30 20 2a 2f 20 20 20 20 31 37 2c 20    860 */    17, 
297b7 20 31 34 38 2c 20 20 20 31 39 2c 20 20 31 38 39   148,   19,  189
297b8 2c 20 20 31 34 38 2c 20 20 31 38 39 2c 20 20 20  ,  148,  189,   
297b9 32 33 2c 20 20 31 34 38 2c 20 20 31 31 33 2c 20  23,  148,  113, 
297ba 20 31 37 30 2c 0a 20 2f 2a 20 20 20 38 37 30 20   170,. /*   870 
297bb 2a 2f 20 20 20 31 37 31 2c 20 20 31 34 38 2c 20  */   171,  148, 
297bc 20 20 31 36 2c 20 20 31 34 38 2c 20 20 20 33 31    16,  148,   31
297bd 2c 20 20 20 31 36 2c 20 20 20 31 37 2c 20 20 20  ,   16,   17,   
297be 38 30 2c 20 20 20 31 39 2c 20 20 31 36 32 2c 0a  80,   19,  162,.
297bf 20 2f 2a 20 20 20 38 38 30 20 2a 2f 20 20 20 31   /*   880 */   1
297c0 34 38 2c 20 20 31 31 35 2c 20 20 20 32 33 2c 20  48,  115,   23, 
297c1 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 38 38    88,   89,   88
297c2 2c 20 20 20 38 39 2c 20 20 32 31 30 2c 20 20 20  ,   89,  210,   
297c3 38 38 2c 20 20 20 38 39 2c 0a 20 2f 2a 20 20 20  88,   89,. /*   
297c4 38 39 30 20 2a 2f 20 20 20 20 33 31 2c 20 20 20  890 */    31,   
297c5 34 38 2c 20 20 31 34 38 2c 20 20 31 37 30 2c 20  48,  148,  170, 
297c6 20 31 37 31 2c 20 20 31 37 30 2c 20 20 31 37 31   171,  170,  171
297c7 2c 20 20 31 34 38 2c 20 20 20 38 38 2c 20 20 20  ,  148,   88,   
297c8 34 33 2c 0a 20 2f 2a 20 20 20 39 30 30 20 2a 2f  43,. /*   900 */
297c9 20 20 20 32 31 34 2c 20 20 20 35 38 2c 20 20 31     214,   58,  1
297ca 37 30 2c 20 20 31 37 31 2c 20 20 31 34 38 2c 20  70,  171,  148, 
297cb 20 31 38 39 2c 20 20 31 34 38 2c 20 20 20 34 38   189,  148,   48
297cc 2c 20 20 31 38 39 2c 20 20 31 31 34 2c 0a 20 2f  ,  189,  114,. /
297cd 2a 20 20 20 39 31 30 20 2a 2f 20 20 20 20 31 39  *   910 */    19
297ce 2c 20 20 31 34 38 2c 20 20 32 34 33 2c 20 20 32  ,  148,  243,  2
297cf 34 34 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20  44,  170,  171, 
297d0 20 31 34 38 2c 20 20 20 35 38 2c 20 20 31 34 38   148,   58,  148
297d1 2c 20 20 31 37 30 2c 0a 20 2f 2a 20 20 20 39 32  ,  170,. /*   92
297d2 30 20 2a 2f 20 20 20 31 37 31 2c 20 20 20 37 38  0 */   171,   78
297d3 2c 20 20 20 37 39 2c 20 20 32 31 30 2c 20 20 31  ,   79,  210,  1
297d4 34 38 2c 20 20 20 33 30 2c 20 20 31 37 30 2c 20  48,   30,  170, 
297d5 20 31 37 31 2c 20 20 31 37 30 2c 20 20 31 37 31   171,  170,  171
297d6 2c 0a 20 2f 2a 20 20 20 39 33 30 20 2a 2f 20 20  ,. /*   930 */  
297d7 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39    87,   88,   89
297d8 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20 20 20  ,  170,  171,   
297d9 39 32 2c 20 20 31 34 38 2c 20 20 20 37 38 2c 20  92,  148,   78, 
297da 20 20 37 39 2c 20 20 20 38 30 2c 0a 20 2f 2a 20    79,   80,. /* 
297db 20 20 39 34 30 20 2a 2f 20 20 20 31 37 30 2c 20    940 */   170, 
297dc 20 31 37 31 2c 20 20 31 32 35 2c 20 20 31 32 36   171,  125,  126
297dd 2c 20 20 31 34 38 2c 20 20 20 35 30 2c 20 20 20  ,  148,   50,   
297de 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20  87,   88,   89, 
297df 20 20 31 36 2c 0a 20 2f 2a 20 20 20 39 35 30 20    16,. /*   950 
297e0 2a 2f 20 20 20 20 31 37 2c 20 20 20 39 32 2c 20  */    17,   92, 
297e1 20 20 31 39 2c 20 20 31 31 30 2c 20 20 20 39 38    19,  110,   98
297e2 2c 20 20 31 34 38 2c 20 20 20 32 33 2c 20 20 31  ,  148,   23,  1
297e3 34 38 2c 20 20 31 35 36 2c 20 20 31 30 33 2c 0a  48,  156,  103,.
297e4 20 2f 2a 20 20 20 39 36 30 20 2a 2f 20 20 20 31   /*   960 */   1
297e5 34 38 2c 20 20 31 31 30 2c 20 20 31 35 36 2c 20  48,  110,  156, 
297e6 20 20 32 32 2c 20 20 20 33 31 2c 20 20 31 38 39    22,   31,  189
297e7 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20 20 31  ,  170,  171,  1
297e8 32 35 2c 20 20 31 32 36 2c 0a 20 2f 2a 20 20 20  25,  126,. /*   
297e9 39 37 30 20 2a 2f 20 20 20 31 32 37 2c 20 20 31  970 */   127,  1
297ea 32 38 2c 20 20 31 32 39 2c 20 20 31 33 30 2c 20  28,  129,  130, 
297eb 20 20 32 30 2c 20 20 31 32 34 2c 20 20 31 35 36    20,  124,  156
297ec 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20 20 31  ,  170,  171,  1
297ed 37 30 2c 0a 20 2f 2a 20 20 20 39 38 30 20 2a 2f  70,. /*   980 */
297ee 20 20 20 31 37 31 2c 20 20 20 34 38 2c 20 20 31     171,   48,  1
297ef 37 30 2c 20 20 31 37 31 2c 20 20 31 32 35 2c 20  70,  171,  125, 
297f0 20 31 32 36 2c 20 20 31 32 37 2c 20 20 31 32 38   126,  127,  128
297f1 2c 20 20 31 32 39 2c 20 20 31 33 30 2c 0a 20 2f  ,  129,  130,. /
297f2 2a 20 20 20 39 39 30 20 2a 2f 20 20 20 32 30 34  *   990 */   204
297f3 2c 20 20 20 35 38 2c 20 20 31 39 30 2c 20 20 20  ,   58,  190,   
297f4 20 35 2c 20 20 31 34 38 2c 20 20 31 34 38 2c 20   5,  148,  148, 
297f5 20 31 39 30 2c 20 20 31 30 32 2c 20 20 20 31 30   190,  102,   10
297f6 2c 20 20 20 31 31 2c 0a 20 2f 2a 20 20 31 30 30  ,   11,. /*  100
297f7 30 20 2a 2f 20 20 20 20 31 32 2c 20 20 20 31 33  0 */    12,   13
297f8 2c 20 20 31 34 38 2c 20 20 31 31 32 2c 20 20 20  ,  148,  112,   
297f9 32 32 2c 20 20 31 34 38 2c 20 20 31 34 38 2c 20  22,  148,  148, 
297fa 20 20 32 30 2c 20 20 31 34 38 2c 20 20 20 32 32    20,  148,   22
297fb 2c 0a 20 2f 2a 20 20 31 30 31 30 20 2a 2f 20 20  ,. /*  1010 */  
297fc 20 31 39 30 2c 20 20 20 37 38 2c 20 20 20 37 39   190,   78,   79
297fd 2c 20 20 20 35 39 2c 20 20 20 32 36 2c 20 20 20  ,   59,   26,   
297fe 34 33 2c 20 20 20 32 38 2c 20 20 31 37 30 2c 20  43,   28,  170, 
297ff 20 31 37 31 2c 20 20 31 34 38 2c 0a 20 2f 2a 20   171,  148,. /* 
29800 20 31 30 32 30 20 2a 2f 20 20 20 20 38 37 2c 20   1020 */    87, 
29801 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 33 35    88,   89,   35
29802 2c 20 20 31 34 38 2c 20 20 20 39 32 2c 20 20 31  ,  148,   92,  1
29803 34 38 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20  48,  170,  171, 
29804 20 31 34 38 2c 0a 20 2f 2a 20 20 31 30 33 30 20   148,. /*  1030 
29805 2a 2f 20 20 20 31 37 30 2c 20 20 31 37 31 2c 20  */   170,  171, 
29806 20 31 34 38 2c 20 20 20 32 37 2c 20 20 31 34 38   148,   27,  148
29807 2c 20 20 20 34 37 2c 20 20 31 34 38 2c 20 20 20  ,   47,  148,   
29808 34 39 2c 20 20 20 39 37 2c 20 20 32 33 34 2c 0a  49,   97,  234,.
29809 20 2f 2a 20 20 31 30 34 30 20 2a 2f 20 20 20 20   /*  1040 */    
2980a 33 34 2c 20 20 20 35 33 2c 20 20 31 34 38 2c 20  34,   53,  148, 
2980b 20 31 37 39 2c 20 20 20 35 36 2c 20 20 31 34 38   179,   56,  148
2980c 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20 20 31  ,  170,  171,  1
2980d 37 30 2c 20 20 31 37 31 2c 0a 20 2f 2a 20 20 31  70,  171,. /*  1
2980e 30 35 30 20 2a 2f 20 20 20 31 34 38 2c 20 20 31  050 */   148,  1
2980f 37 30 2c 20 20 31 37 31 2c 20 20 31 34 38 2c 20  70,  171,  148, 
29810 20 31 37 30 2c 20 20 31 37 31 2c 20 20 31 37 30   170,  171,  170
29811 2c 20 20 31 37 31 2c 20 20 31 32 35 2c 20 20 31  ,  171,  125,  1
29812 32 36 2c 0a 20 2f 2a 20 20 31 30 36 30 20 2a 2f  26,. /*  1060 */
29813 20 20 20 31 32 37 2c 20 20 31 32 38 2c 20 20 31     127,  128,  1
29814 32 39 2c 20 20 31 33 30 2c 20 20 31 37 30 2c 20  29,  130,  170, 
29815 20 31 37 31 2c 20 20 31 37 39 2c 20 20 31 37 30   171,  179,  170
29816 2c 20 20 31 37 31 2c 20 20 31 34 38 2c 0a 20 2f  ,  171,  148,. /
29817 2a 20 20 31 30 37 30 20 2a 2f 20 20 20 20 39 38  *  1070 */    98
29818 2c 20 20 20 38 39 2c 20 20 31 37 30 2c 20 20 31  ,   89,  170,  1
29819 37 31 2c 20 20 31 34 38 2c 20 20 31 37 30 2c 20  71,  148,  170, 
2981a 20 31 37 31 2c 20 20 31 34 38 2c 20 20 20 32 30   171,  148,   20
2981b 2c 20 20 31 34 38 2c 0a 20 2f 2a 20 20 31 30 38  ,  148,. /*  108
2981c 30 20 2a 2f 20 20 20 20 32 32 2c 20 20 20 32 30  0 */    22,   20
2981d 2c 20 20 31 34 38 2c 20 20 20 32 32 2c 20 20 31  ,  148,   22,  1
2981e 34 38 2c 20 20 31 37 39 2c 20 20 31 34 38 2c 20  48,  179,  148, 
2981f 20 20 39 39 2c 20 20 31 30 30 2c 20 20 31 30 31    99,  100,  101
29820 2c 0a 20 2f 2a 20 20 31 30 39 30 20 2a 2f 20 20  ,. /*  1090 */  
29821 20 31 34 38 2c 20 20 31 37 30 2c 20 20 31 37 31   148,  170,  171
29822 2c 20 20 31 30 35 2c 20 20 31 34 38 2c 20 20 31  ,  105,  148,  1
29823 34 38 2c 20 20 31 31 34 2c 20 20 31 34 38 2c 20  48,  114,  148, 
29824 20 31 31 30 2c 20 20 31 37 30 2c 0a 20 2f 2a 20   110,  170,. /* 
29825 20 31 31 30 30 20 2a 2f 20 20 20 31 37 31 2c 20   1100 */   171, 
29826 20 31 37 30 2c 20 20 31 37 31 2c 20 20 31 34 38   170,  171,  148
29827 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20 20 31  ,  170,  171,  1
29828 37 30 2c 20 20 31 37 31 2c 20 20 31 37 30 2c 20  70,  171,  170, 
29829 20 31 37 31 2c 0a 20 2f 2a 20 20 31 31 31 30 20   171,. /*  1110 
2982a 2a 2f 20 20 20 20 20 37 2c 20 20 20 20 38 2c 20  */     7,    8, 
2982b 20 31 37 30 2c 20 20 31 37 31 2c 20 20 20 32 30   170,  171,   20
2982c 2c 20 20 31 34 38 2c 20 20 20 32 32 2c 20 20 31  ,  148,   22,  1
2982d 37 30 2c 20 20 31 37 31 2c 20 20 31 37 30 2c 0a  70,  171,  170,.
2982e 20 2f 2a 20 20 31 31 32 30 20 2a 2f 20 20 20 31   /*  1120 */   1
2982f 37 31 2c 20 20 31 34 38 2c 20 20 20 32 30 2c 20  71,  148,   20, 
29830 20 31 33 35 2c 20 20 20 32 32 2c 20 20 31 37 30   135,   22,  170
29831 2c 20 20 31 37 31 2c 20 20 31 34 38 2c 20 20 31  ,  171,  148,  1
29832 34 38 2c 20 20 20 39 31 2c 0a 20 2f 2a 20 20 31  48,   91,. /*  1
29833 31 33 30 20 2a 2f 20 20 20 20 39 32 2c 20 20 20  130 */    92,   
29834 32 30 2c 20 20 20 32 30 2c 20 20 20 32 32 2c 20  20,   20,   22, 
29835 20 20 32 32 2c 20 20 31 35 30 2c 20 20 32 33 33    22,  150,  233
29836 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20 20 20  ,  170,  171,   
29837 32 30 2c 0a 20 2f 2a 20 20 31 31 34 30 20 2a 2f  20,. /*  1140 */
29838 20 20 20 20 32 30 2c 20 20 20 32 32 2c 20 20 20      20,   22,   
29839 32 32 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20  22,  170,  171, 
2983a 20 20 32 30 2c 20 20 31 34 38 2c 20 20 20 32 32    20,  148,   22
2983b 2c 20 20 31 34 38 2c 20 20 31 37 30 2c 0a 20 2f  ,  148,  170,. /
2983c 2a 20 20 31 31 35 30 20 2a 2f 20 20 20 31 37 31  *  1150 */   171
2983d 2c 20 20 31 34 38 2c 20 20 31 34 38 2c 20 20 31  ,  148,  148,  1
2983e 34 38 2c 20 20 31 39 32 2c 20 20 31 34 38 2c 20  48,  192,  148, 
2983f 20 31 34 38 2c 20 20 31 34 38 2c 20 20 31 34 38   148,  148,  148
29840 2c 20 20 31 34 38 2c 0a 20 2f 2a 20 20 31 31 36  ,  148,. /*  116
29841 30 20 2a 2f 20 20 20 31 34 38 2c 20 20 31 34 38  0 */   148,  148
29842 2c 20 20 31 34 38 2c 20 20 31 34 38 2c 20 20 31  ,  148,  148,  1
29843 34 38 2c 20 20 31 34 38 2c 20 20 31 37 33 2c 20  48,  148,  173, 
29844 20 32 33 30 2c 20 20 31 39 34 2c 20 20 32 33 30   230,  194,  230
29845 2c 0a 20 2f 2a 20 20 31 31 37 30 20 2a 2f 20 20  ,. /*  1170 */  
29846 20 32 32 35 2c 20 20 32 30 35 2c 20 20 31 37 33   225,  205,  173
29847 2c 20 20 31 37 38 2c 20 20 31 37 33 2c 20 20 31  ,  178,  173,  1
29848 37 33 2c 20 20 31 39 35 2c 20 20 20 20 36 2c 20  73,  195,    6, 
29849 20 31 34 37 2c 20 20 31 39 35 2c 0a 20 2f 2a 20   147,  195,. /* 
2984a 20 31 31 38 30 20 2a 2f 20 20 20 31 36 32 2c 20   1180 */   162, 
2984b 20 31 36 32 2c 20 20 32 30 35 2c 20 20 31 36 32   162,  205,  162
2984c 2c 20 20 31 34 37 2c 20 20 20 32 32 2c 20 20 31  ,  147,   22,  1
2984d 34 37 2c 20 20 31 34 37 2c 20 20 32 30 35 2c 20  47,  147,  205, 
2984e 20 31 35 35 2c 0a 20 2f 2a 20 20 31 31 39 30 20   155,. /*  1190 
2984f 2a 2f 20 20 20 31 32 32 2c 20 20 31 39 35 2c 20  */   122,  195, 
29850 20 31 31 39 2c 20 20 31 37 33 2c 20 20 31 32 30   119,  173,  120
29851 2c 20 20 31 31 38 2c 20 20 31 37 34 2c 20 20 31  ,  118,  174,  1
29852 32 31 2c 20 20 31 33 31 2c 20 20 32 32 34 2c 0a  21,  131,  224,.
29853 20 2f 2a 20 20 31 32 30 30 20 2a 2f 20 20 20 31   /*  1200 */   1
29854 31 32 2c 20 20 31 35 33 2c 20 20 31 35 33 2c 20  12,  153,  153, 
29855 20 20 39 38 2c 20 20 31 31 37 2c 20 20 20 39 38    98,  117,   98
29856 2c 20 20 20 34 30 2c 20 20 31 37 32 2c 20 20 31  ,   40,  172,  1
29857 37 32 2c 20 20 20 31 39 2c 0a 20 2f 2a 20 20 31  72,   19,. /*  1
29858 32 31 30 20 2a 2f 20 20 20 20 39 37 2c 20 20 20  210 */    97,   
29859 38 34 2c 20 20 20 31 35 2c 20 20 31 39 30 2c 20  84,   15,  190, 
2985a 20 31 37 32 2c 20 20 31 35 32 2c 20 20 31 37 32   172,  152,  172
2985b 2c 20 20 31 37 34 2c 20 20 32 32 37 2c 20 20 32  ,  174,  227,  2
2985c 32 37 2c 0a 20 2f 2a 20 20 31 32 32 30 20 2a 2f  27,. /*  1220 */
2985d 20 20 20 31 39 36 2c 20 20 31 38 30 2c 20 20 31     196,  180,  1
2985e 39 37 2c 20 20 31 37 32 2c 20 20 31 39 38 2c 20  97,  172,  198, 
2985f 20 31 37 35 2c 20 20 31 39 39 2c 20 20 31 38 30   175,  199,  180
29860 2c 20 20 31 37 32 2c 20 20 31 37 32 2c 0a 20 2f  ,  172,  172,. /
29861 2a 20 20 31 32 33 30 20 2a 2f 20 20 20 31 37 35  *  1230 */   175
29862 2c 20 20 31 35 33 2c 20 20 31 35 32 2c 20 20 31  ,  153,  152,  1
29863 35 32 2c 20 20 32 30 36 2c 20 20 31 35 33 2c 20  52,  206,  153, 
29864 20 32 30 37 2c 20 20 32 30 37 2c 20 20 32 30 36   207,  207,  206
29865 2c 20 20 31 35 33 2c 0a 20 2f 2a 20 20 31 32 34  ,  153,. /*  124
29866 30 20 2a 2f 20 20 20 20 33 38 2c 20 20 31 33 31  0 */    38,  131
29867 2c 20 20 31 35 33 2c 20 20 31 35 32 2c 20 20 20  ,  153,  152,   
29868 36 30 2c 20 20 31 35 33 2c 20 20 20 31 39 2c 20  60,  153,   19, 
29869 20 31 38 35 2c 20 20 31 39 35 2c 20 20 31 38 35   185,  195,  185
2986a 2c 0a 20 2f 2a 20 20 31 32 35 30 20 2a 2f 20 20  ,. /*  1250 */  
2986b 20 31 35 33 2c 20 20 20 31 35 2c 20 20 31 35 33   153,   15,  153
2986c 2c 20 20 31 38 38 2c 20 20 31 38 38 2c 20 20 31  ,  188,  188,  1
2986d 39 35 2c 20 20 31 38 35 2c 20 20 31 38 38 2c 20  95,  185,  188, 
2986e 20 31 38 38 2c 20 20 20 33 33 2c 0a 20 2f 2a 20   188,   33,. /* 
2986f 20 31 32 36 30 20 2a 2f 20 20 20 31 33 38 2c 20   1260 */   138, 
29870 20 31 35 33 2c 20 20 32 31 36 2c 20 20 20 20 31   153,  216,    1
29871 2c 20 20 31 36 30 2c 20 20 20 32 30 2c 20 20 31  ,  160,   20,  1
29872 37 36 2c 20 20 31 35 33 2c 20 20 32 33 35 2c 20  76,  153,  235, 
29873 20 31 37 36 2c 0a 20 2f 2a 20 20 31 32 37 30 20   176,. /*  1270 
29874 2a 2f 20 20 20 32 31 36 2c 20 20 31 31 32 2c 20  */   216,  112, 
29875 20 31 31 32 2c 20 20 31 31 32 2c 20 20 31 31 32   112,  112,  112
29876 2c 20 20 20 39 32 2c 20 20 31 30 37 2c 20 20 20  ,   92,  107,   
29877 31 39 2c 20 20 20 31 31 2c 20 20 20 32 30 2c 0a  19,   11,   20,.
29878 20 2f 2a 20 20 31 32 38 30 20 2a 2f 20 20 20 20   /*  1280 */    
29879 32 30 2c 20 20 32 33 36 2c 20 20 20 31 39 2c 20  20,  236,   19, 
2987a 20 20 31 39 2c 20 20 31 31 36 2c 20 20 20 32 30    19,  116,   20
2987b 2c 20 20 31 31 36 2c 20 20 31 31 34 2c 20 20 20  ,  116,  114,   
2987c 32 32 2c 20 20 20 32 30 2c 0a 20 2f 2a 20 20 31  22,   20,. /*  1
2987d 32 39 30 20 2a 2f 20 20 20 32 33 38 2c 20 20 20  290 */   238,   
2987e 32 30 2c 20 20 20 32 32 2c 20 20 20 31 39 2c 20  20,   22,   19, 
2987f 20 20 32 32 2c 20 20 31 31 36 2c 20 20 31 31 35    22,  116,  115
29880 2c 20 20 32 33 38 2c 20 20 20 32 30 2c 20 20 31  ,  238,   20,  1
29881 31 32 2c 0a 20 2f 2a 20 20 31 33 30 30 20 2a 2f  12,. /*  1300 */
29882 20 20 20 20 32 30 2c 20 20 20 32 30 2c 20 20 20      20,   20,   
29883 31 39 2c 20 20 20 31 39 2c 20 20 20 31 39 2c 20  19,   19,   19, 
29884 20 20 33 32 2c 20 20 20 32 30 2c 20 20 20 31 39    32,   20,   19
29885 2c 20 20 20 31 39 2c 20 20 20 39 36 2c 0a 20 2f  ,   19,   96,. /
29886 2a 20 20 31 33 31 30 20 2a 2f 20 20 20 31 30 33  *  1310 */   103
29887 2c 20 20 20 31 36 2c 20 20 20 34 34 2c 20 20 32  ,   16,   44,  2
29888 34 31 2c 20 20 20 31 37 2c 20 20 20 32 31 2c 20  41,   17,   21, 
29889 20 20 39 38 2c 20 20 20 39 38 2c 20 20 20 33 36    98,   98,   36
2988a 2c 20 20 32 34 34 2c 0a 20 2f 2a 20 20 31 33 32  ,  244,. /*  132
2988b 30 20 2a 2f 20 20 20 20 34 34 2c 20 20 20 34 34  0 */    44,   44
2988c 2c 20 20 20 32 32 2c 20 20 31 33 34 2c 20 20 20  ,   22,  134,   
2988d 31 39 2c 20 20 20 20 35 2c 20 20 32 34 37 2c 20  19,    5,  247, 
2988e 20 20 20 31 2c 20 20 31 32 33 2c 20 20 20 36 38     1,  123,   68
2988f 2c 0a 20 2f 2a 20 20 31 33 33 30 20 2a 2f 20 20  ,. /*  1330 */  
29890 20 20 35 31 2c 20 20 31 30 32 2c 20 20 20 34 35    51,  102,   45
29891 2c 20 20 20 31 39 2c 20 20 31 31 33 2c 20 20 20  ,   19,  113,   
29892 34 35 2c 20 20 20 20 31 2c 20 20 20 31 34 2c 20  45,    1,   14, 
29893 20 20 31 37 2c 20 20 31 31 37 2c 0a 20 2f 2a 20    17,  117,. /* 
29894 20 31 33 34 30 20 2a 2f 20 20 20 31 30 32 2c 20   1340 */   102, 
29895 20 31 31 33 2c 20 20 20 32 30 2c 20 20 20 36 38   113,   20,   68
29896 2c 20 20 20 31 39 2c 20 20 20 31 34 2c 20 20 31  ,   19,   14,  1
29897 32 33 2c 20 20 31 33 36 2c 20 20 31 32 34 2c 20  23,  136,  124, 
29898 20 20 35 37 2c 0a 20 2f 2a 20 20 31 33 35 30 20    57,. /*  1350 
29899 2a 2f 20 20 20 20 20 33 2c 20 20 31 33 37 2c 20  */     3,  137, 
2989a 20 20 31 39 2c 20 20 20 20 34 2c 0a 7d 3b 0a 23    19,    4,.};.#
2989b 64 65 66 69 6e 65 20 59 59 5f 53 48 49 46 54 5f  define YY_SHIFT_
2989c 55 53 45 5f 44 46 4c 54 20 28 2d 39 39 29 0a 23  USE_DFLT (-99).#
2989d 64 65 66 69 6e 65 20 59 59 5f 53 48 49 46 54 5f  define YY_SHIFT_
2989e 4d 41 58 20 33 39 36 0a 73 74 61 74 69 63 20 63  MAX 396.static c
2989f 6f 6e 73 74 20 73 68 6f 72 74 20 79 79 5f 73 68  onst short yy_sh
298a0 69 66 74 5f 6f 66 73 74 5b 5d 20 3d 20 7b 0a 20  ift_ofst[] = {. 
298a1 2f 2a 20 20 20 20 20 30 20 2a 2f 20 20 20 20 32  /*     0 */    2
298a2 33 2c 20 20 38 34 33 2c 20 20 39 38 38 2c 20 20  3,  843,  988,  
298a3 2d 31 36 2c 20 20 38 34 33 2c 20 20 39 33 33 2c  -16,  843,  933,
298a4 20 20 39 33 33 2c 20 20 33 39 33 2c 20 20 31 32    933,  393,  12
298a5 33 2c 20 20 32 35 32 2c 0a 20 2f 2a 20 20 20 20  3,  252,. /*    
298a6 31 30 20 2a 2f 20 20 20 2d 39 38 2c 20 20 20 39  10 */   -98,   9
298a7 36 2c 20 20 39 33 33 2c 20 20 39 33 33 2c 20 20  6,  933,  933,  
298a8 39 33 33 2c 20 20 39 33 33 2c 20 20 39 33 33 2c  933,  933,  933,
298a9 20 20 2d 34 35 2c 20 20 32 34 39 2c 20 20 31 37    -45,  249,  17
298aa 34 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20  4,. /*    20 */ 
298ab 20 20 33 34 30 2c 20 20 2d 31 37 2c 20 20 20 31    340,  -17,   1
298ac 39 2c 20 20 20 31 39 2c 20 20 20 35 33 2c 20 20  9,   19,   53,  
298ad 31 36 35 2c 20 20 32 30 38 2c 20 20 32 35 31 2c  165,  208,  251,
298ae 20 20 33 32 36 2c 20 20 33 39 35 2c 0a 20 2f 2a    326,  395,. /*
298af 20 20 20 20 33 30 20 2a 2f 20 20 20 34 36 34 2c      30 */   464,
298b0 20 20 35 33 33 2c 20 20 36 30 32 2c 20 20 36 34    533,  602,  64
298b1 35 2c 20 20 36 38 38 2c 20 20 36 34 35 2c 20 20  5,  688,  645,  
298b2 36 34 35 2c 20 20 36 34 35 2c 20 20 36 34 35 2c  645,  645,  645,
298b3 20 20 36 34 35 2c 0a 20 2f 2a 20 20 20 20 34 30    645,. /*    40
298b4 20 2a 2f 20 20 20 36 34 35 2c 20 20 36 34 35 2c   */   645,  645,
298b5 20 20 36 34 35 2c 20 20 36 34 35 2c 20 20 36 34    645,  645,  64
298b6 35 2c 20 20 36 34 35 2c 20 20 36 34 35 2c 20 20  5,  645,  645,  
298b7 36 34 35 2c 20 20 36 34 35 2c 20 20 36 34 35 2c  645,  645,  645,
298b8 0a 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20 20  . /*    50 */   
298b9 36 34 35 2c 20 20 36 34 35 2c 20 20 36 34 35 2c  645,  645,  645,
298ba 20 20 37 33 31 2c 20 20 37 37 34 2c 20 20 37 37    731,  774,  77
298bb 34 2c 20 20 38 35 39 2c 20 20 39 33 33 2c 20 20  4,  859,  933,  
298bc 39 33 33 2c 20 20 39 33 33 2c 0a 20 2f 2a 20 20  933,  933,. /*  
298bd 20 20 36 30 20 2a 2f 20 20 20 39 33 33 2c 20 20    60 */   933,  
298be 39 33 33 2c 20 20 39 33 33 2c 20 20 39 33 33 2c  933,  933,  933,
298bf 20 20 39 33 33 2c 20 20 39 33 33 2c 20 20 39 33    933,  933,  93
298c0 33 2c 20 20 39 33 33 2c 20 20 39 33 33 2c 20 20  3,  933,  933,  
298c1 39 33 33 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a  933,. /*    70 *
298c2 2f 20 20 20 39 33 33 2c 20 20 39 33 33 2c 20 20  /   933,  933,  
298c3 39 33 33 2c 20 20 39 33 33 2c 20 20 39 33 33 2c  933,  933,  933,
298c4 20 20 39 33 33 2c 20 20 39 33 33 2c 20 20 39 33    933,  933,  93
298c5 33 2c 20 20 39 33 33 2c 20 20 39 33 33 2c 0a 20  3,  933,  933,. 
298c6 2f 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 39 33  /*    80 */   93
298c7 33 2c 20 20 39 33 33 2c 20 20 39 33 33 2c 20 20  3,  933,  933,  
298c8 39 33 33 2c 20 20 39 33 33 2c 20 20 39 33 33 2c  933,  933,  933,
298c9 20 20 39 33 33 2c 20 20 39 33 33 2c 20 20 39 33    933,  933,  93
298ca 33 2c 20 20 39 33 33 2c 0a 20 2f 2a 20 20 20 20  3,  933,. /*    
298cb 39 30 20 2a 2f 20 20 20 39 33 33 2c 20 20 39 33  90 */   933,  93
298cc 33 2c 20 20 39 33 33 2c 20 20 39 33 33 2c 20 20  3,  933,  933,  
298cd 39 33 33 2c 20 20 39 33 33 2c 20 20 39 33 33 2c  933,  933,  933,
298ce 20 20 2d 36 31 2c 20 20 2d 36 31 2c 20 20 20 20    -61,  -61,    
298cf 36 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20  6,. /*   100 */ 
298d0 20 20 20 20 36 2c 20 20 32 38 31 2c 20 20 20 32      6,  281,   2
298d1 32 2c 20 20 20 36 31 2c 20 20 38 35 36 2c 20 20  2,   61,  856,  
298d2 32 38 34 2c 20 20 33 34 30 2c 20 20 33 34 30 2c  284,  340,  340,
298d3 20 20 20 36 38 2c 20 20 2d 31 37 2c 0a 20 2f 2a     68,  -17,. /*
298d4 20 20 20 31 31 30 20 2a 2f 20 20 20 2d 31 31 2c     110 */   -11,
298d5 20 20 2d 39 39 2c 20 20 2d 39 39 2c 20 20 2d 39    -99,  -99,  -9
298d6 39 2c 20 20 31 33 31 2c 20 20 33 32 38 2c 20 20  9,  131,  328,  
298d7 36 30 39 2c 20 20 36 30 39 2c 20 20 35 34 37 2c  609,  609,  547,
298d8 20 20 36 31 36 2c 0a 20 2f 2a 20 20 20 31 32 30    616,. /*   120
298d9 20 2a 2f 20 20 20 32 35 33 2c 20 20 36 30 37 2c   */   253,  607,
298da 20 20 33 34 30 2c 20 20 36 30 37 2c 20 20 33 34    340,  607,  34
298db 30 2c 20 20 33 34 30 2c 20 20 33 34 30 2c 20 20  0,  340,  340,  
298dc 33 34 30 2c 20 20 33 34 30 2c 20 20 33 34 30 2c  340,  340,  340,
298dd 0a 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20 20  . /*   130 */   
298de 33 34 30 2c 20 20 33 34 30 2c 20 20 33 34 30 2c  340,  340,  340,
298df 20 20 33 34 30 2c 20 20 33 34 30 2c 20 20 33 34    340,  340,  34
298e0 30 2c 20 20 33 34 30 2c 20 20 33 34 30 2c 20 20  0,  340,  340,  
298e1 33 34 30 2c 20 20 33 34 30 2c 0a 20 2f 2a 20 20  340,  340,. /*  
298e2 20 31 34 30 20 2a 2f 20 20 20 33 34 30 2c 20 20   140 */   340,  
298e3 32 33 33 2c 20 20 38 35 31 2c 20 20 2d 39 38 2c  233,  851,  -98,
298e4 20 20 2d 39 38 2c 20 20 2d 39 38 2c 20 20 2d 39    -98,  -98,  -9
298e5 39 2c 20 20 2d 39 39 2c 20 20 2d 39 39 2c 20 20  9,  -99,  -99,  
298e6 2d 31 38 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a  -18,. /*   150 *
298e7 2f 20 20 20 2d 31 38 2c 20 20 34 30 35 2c 20 20  /   -18,  405,  
298e8 34 36 37 2c 20 20 33 32 37 2c 20 20 35 35 31 2c  467,  327,  551,
298e9 20 20 35 34 33 2c 20 20 36 33 35 2c 20 20 33 34    543,  635,  34
298ea 33 2c 20 20 34 36 36 2c 20 20 37 39 35 2c 0a 20  3,  466,  795,. 
298eb 2f 2a 20 20 20 31 36 30 20 2a 2f 20 20 20 38 30  /*   160 */   80
298ec 30 2c 20 20 37 39 37 2c 20 20 34 39 36 2c 20 20  0,  797,  496,  
298ed 33 34 30 2c 20 20 33 34 30 2c 20 20 32 37 34 2c  340,  340,  274,
298ee 20 20 33 34 30 2c 20 20 33 34 30 2c 20 20 38 31    340,  340,  81
298ef 30 2c 20 20 33 34 30 2c 0a 20 2f 2a 20 20 20 31  0,  340,. /*   1
298f0 37 30 20 2a 2f 20 20 20 33 34 30 2c 20 20 39 38  70 */   340,  98
298f1 32 2c 20 20 33 34 30 2c 20 20 33 34 30 2c 20 20  2,  340,  340,  
298f2 33 34 30 2c 20 20 35 38 38 2c 20 20 39 38 32 2c  340,  588,  982,
298f3 20 20 33 34 30 2c 20 20 33 34 30 2c 20 20 38 39    340,  340,  89
298f4 35 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20  5,. /*   180 */ 
298f5 20 20 38 39 35 2c 20 20 38 39 35 2c 20 20 33 34    895,  895,  34
298f6 30 2c 20 20 33 34 30 2c 20 20 33 34 30 2c 20 20  0,  340,  340,  
298f7 35 38 38 2c 20 20 33 34 30 2c 20 20 33 34 30 2c  588,  340,  340,
298f8 20 20 35 38 38 2c 20 20 33 34 30 2c 0a 20 2f 2a    588,  340,. /*
298f9 20 20 20 31 39 30 20 2a 2f 20 20 20 37 35 30 2c     190 */   750,
298fa 20 20 34 38 35 2c 20 20 33 34 30 2c 20 20 33 34    485,  340,  34
298fb 30 2c 20 20 35 38 38 2c 20 20 33 34 30 2c 20 20  0,  588,  340,  
298fc 33 34 30 2c 20 20 33 34 30 2c 20 20 35 38 38 2c  340,  340,  588,
298fd 20 20 33 34 30 2c 0a 20 2f 2a 20 20 20 32 30 30    340,. /*   200
298fe 20 2a 2f 20 20 20 33 34 30 2c 20 20 33 34 30 2c   */   340,  340,
298ff 20 20 35 38 38 2c 20 20 35 38 38 2c 20 20 33 34    588,  588,  34
29900 30 2c 20 20 33 34 30 2c 20 20 33 34 30 2c 20 20  0,  340,  340,  
29901 33 34 30 2c 20 20 33 34 30 2c 20 20 33 34 35 2c  340,  340,  345,
29902 0a 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20 20  . /*   210 */   
29903 37 32 34 2c 20 20 37 35 35 2c 20 20 2d 31 37 2c  724,  755,  -17,
29904 20 20 38 31 37 2c 20 20 38 31 37 2c 20 20 33 35    817,  817,  35
29905 39 2c 20 31 30 30 36 2c 20 31 30 30 36 2c 20 20  9, 1006, 1006,  
29906 37 36 36 2c 20 31 30 30 36 2c 0a 20 2f 2a 20 20  766, 1006,. /*  
29907 20 32 32 30 20 2a 2f 20 20 20 39 37 32 2c 20 31   220 */   972, 1
29908 30 30 36 2c 20 20 2d 31 37 2c 20 31 30 30 36 2c  006,  -17, 1006,
29909 20 20 2d 31 37 2c 20 20 39 34 31 2c 20 20 32 31    -17,  941,  21
2990a 36 2c 20 20 37 36 36 2c 20 20 37 36 36 2c 20 20  6,  766,  766,  
2990b 32 31 36 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a  216,. /*   230 *
2990c 2f 20 20 31 31 37 31 2c 20 31 31 37 31 2c 20 31  /  1171, 1171, 1
2990d 31 37 31 2c 20 31 31 37 31 2c 20 31 31 36 33 2c  171, 1171, 1163,
2990e 20 20 2d 39 38 2c 20 31 30 36 38 2c 20 31 30 37    -98, 1068, 107
2990f 33 2c 20 31 30 37 34 2c 20 31 30 37 37 2c 0a 20  3, 1074, 1077,. 
29910 2f 2a 20 20 20 32 34 30 20 2a 2f 20 20 31 30 37  /*   240 */  107
29911 36 2c 20 31 30 36 37 2c 20 31 30 38 38 2c 20 31  6, 1067, 1088, 1
29912 30 38 38 2c 20 31 31 30 35 2c 20 31 30 38 37 2c  088, 1105, 1087,
29913 20 31 31 30 35 2c 20 31 30 38 37 2c 20 31 31 30   1105, 1087, 110
29914 37 2c 20 31 31 30 37 2c 0a 20 2f 2a 20 20 20 32  7, 1107,. /*   2
29915 35 30 20 2a 2f 20 20 31 31 36 36 2c 20 31 31 30  50 */  1166, 110
29916 37 2c 20 31 31 31 33 2c 20 31 31 30 37 2c 20 31  7, 1113, 1107, 1
29917 31 39 30 2c 20 31 31 32 37 2c 20 31 31 32 37 2c  190, 1127, 1127,
29918 20 31 31 36 36 2c 20 31 31 30 37 2c 20 31 31 30   1166, 1107, 110
29919 37 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20  7,. /*   260 */ 
2991a 20 31 31 30 37 2c 20 31 31 39 30 2c 20 31 31 39   1107, 1190, 119
2991b 37 2c 20 31 30 38 38 2c 20 31 31 39 37 2c 20 31  7, 1088, 1197, 1
2991c 30 38 38 2c 20 31 31 39 37 2c 20 31 30 38 38 2c  088, 1197, 1088,
2991d 20 31 30 38 38 2c 20 31 32 30 32 2c 0a 20 2f 2a   1088, 1202,. /*
2991e 20 20 20 32 37 30 20 2a 2f 20 20 31 31 31 30 2c     270 */  1110,
2991f 20 31 31 39 37 2c 20 31 30 38 38 2c 20 31 31 38   1197, 1088, 118
29920 34 2c 20 31 31 38 34 2c 20 31 32 32 37 2c 20 31  4, 1184, 1227, 1
29921 30 36 38 2c 20 31 30 38 38 2c 20 31 32 33 36 2c  068, 1088, 1236,
29922 20 31 32 33 36 2c 0a 20 2f 2a 20 20 20 32 38 30   1236,. /*   280
29923 20 2a 2f 20 20 31 32 33 36 2c 20 31 32 33 36 2c   */  1236, 1236,
29924 20 31 30 36 38 2c 20 31 31 38 34 2c 20 31 32 32   1068, 1184, 122
29925 37 2c 20 31 30 38 38 2c 20 31 32 32 36 2c 20 31  7, 1088, 1226, 1
29926 32 32 36 2c 20 31 30 38 38 2c 20 31 30 38 38 2c  226, 1088, 1088,
29927 0a 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20 31  . /*   290 */  1
29928 31 32 32 2c 20 20 2d 39 39 2c 20 20 2d 39 39 2c  122,  -99,  -99,
29929 20 20 2d 39 39 2c 20 20 2d 39 39 2c 20 20 2d 39    -99,  -99,  -9
2992a 39 2c 20 20 34 35 39 2c 20 20 36 34 36 2c 20 20  9,  459,  646,  
2992b 35 39 31 2c 20 20 36 38 35 2c 0a 20 2f 2a 20 20  591,  685,. /*  
2992c 20 33 30 30 20 2a 2f 20 20 20 38 39 31 2c 20 20   300 */   891,  
2992d 33 32 35 2c 20 20 39 38 37 2c 20 31 30 35 38 2c  325,  987, 1058,
2992e 20 20 33 32 32 2c 20 31 31 30 33 2c 20 31 30 33    322, 1103, 103
2992f 38 2c 20 31 30 36 31 2c 20 31 30 39 34 2c 20 31  8, 1061, 1094, 1
29930 31 30 32 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a  102,. /*   310 *
29931 2f 20 20 31 31 31 31 2c 20 31 31 31 32 2c 20 31  /  1111, 1112, 1
29932 31 31 39 2c 20 31 31 32 30 2c 20 20 31 35 30 2c  119, 1120,  150,
29933 20 31 31 32 35 2c 20 20 39 35 34 2c 20 31 32 36   1125,  954, 126
29934 32 2c 20 31 32 34 35 2c 20 31 31 35 39 2c 0a 20  2, 1245, 1159,. 
29935 2f 2a 20 20 20 33 32 30 20 2a 2f 20 20 31 31 36  /*   320 */  116
29936 30 2c 20 31 31 36 31 2c 20 31 31 36 32 2c 20 31  0, 1161, 1162, 1
29937 31 38 33 2c 20 31 31 36 39 2c 20 31 32 35 38 2c  183, 1169, 1258,
29938 20 31 32 35 39 2c 20 31 32 36 30 2c 20 31 32 36   1259, 1260, 126
29939 33 2c 20 31 32 36 37 2c 0a 20 2f 2a 20 20 20 33  3, 1267,. /*   3
2993a 33 30 20 2a 2f 20 20 31 32 36 34 2c 20 31 32 36  30 */  1264, 126
2993b 35 2c 20 31 32 36 36 2c 20 31 32 36 39 2c 20 31  5, 1266, 1269, 1
2993c 32 37 31 2c 20 31 32 37 30 2c 20 31 31 36 38 2c  271, 1270, 1168,
2993d 20 31 32 37 32 2c 20 31 31 37 30 2c 20 31 32 37   1272, 1170, 127
2993e 30 2c 0a 20 2f 2a 20 20 20 33 34 30 20 2a 2f 20  0,. /*   340 */ 
2993f 20 31 31 37 33 2c 20 31 32 37 34 2c 20 31 31 37   1173, 1274, 117
29940 39 2c 20 31 31 38 31 2c 20 31 32 37 38 2c 20 31  9, 1181, 1278, 1
29941 31 38 37 2c 20 31 32 38 30 2c 20 31 32 38 31 2c  187, 1280, 1281,
29942 20 31 32 37 33 2c 20 31 32 36 38 2c 0a 20 2f 2a   1273, 1268,. /*
29943 20 20 20 33 35 30 20 2a 2f 20 20 31 32 38 33 2c     350 */  1283,
29944 20 31 32 37 36 2c 20 31 32 38 34 2c 20 31 32 38   1276, 1284, 128
29945 36 2c 20 31 32 38 35 2c 20 31 32 38 38 2c 20 31  6, 1285, 1288, 1
29946 32 37 37 2c 20 31 32 38 39 2c 20 31 32 31 33 2c  277, 1289, 1213,
29947 20 31 32 30 37 2c 0a 20 2f 2a 20 20 20 33 36 30   1207,. /*   360
29948 20 2a 2f 20 20 31 32 39 35 2c 20 31 32 39 37 2c   */  1295, 1297,
29949 20 31 32 39 34 2c 20 31 32 31 38 2c 20 31 32 38   1294, 1218, 128
2994a 32 2c 20 31 32 37 39 2c 20 31 32 38 37 2c 20 31  2, 1279, 1287, 1
2994b 33 30 30 2c 20 31 32 39 30 2c 20 31 31 38 39 2c  300, 1290, 1189,
2994c 0a 20 2f 2a 20 20 20 33 37 30 20 2a 2f 20 20 31  . /*   370 */  1
2994d 32 31 39 2c 20 31 33 30 35 2c 20 31 33 32 30 2c  219, 1305, 1320,
2994e 20 31 33 32 36 2c 20 31 32 32 39 2c 20 31 32 36   1326, 1229, 126
2994f 31 2c 20 31 32 37 35 2c 20 31 32 30 35 2c 20 31  1, 1275, 1205, 1
29950 33 31 34 2c 20 31 32 32 31 2c 0a 20 2f 2a 20 20  314, 1221,. /*  
29951 20 33 38 30 20 2a 2f 20 20 31 33 33 35 2c 20 31   380 */  1335, 1
29952 33 32 33 2c 20 31 32 32 32 2c 20 31 33 32 31 2c  323, 1222, 1321,
29953 20 31 32 32 38 2c 20 31 32 33 38 2c 20 31 32 32   1228, 1238, 122
29954 33 2c 20 31 33 32 35 2c 20 31 32 32 34 2c 20 31  3, 1325, 1224, 1
29955 33 32 32 2c 0a 20 2f 2a 20 20 20 33 39 30 20 2a  322,. /*   390 *
29956 2f 20 20 31 33 33 31 2c 20 31 32 39 32 2c 20 31  /  1331, 1292, 1
29957 32 31 31 2c 20 31 32 31 34 2c 20 31 33 33 33 2c  211, 1214, 1333,
29958 20 31 33 34 37 2c 20 31 33 34 39 2c 0a 7d 3b 0a   1347, 1349,.};.
29959 23 64 65 66 69 6e 65 20 59 59 5f 52 45 44 55 43  #define YY_REDUC
2995a 45 5f 55 53 45 5f 44 46 4c 54 20 28 2d 31 34 34  E_USE_DFLT (-144
2995b 29 0a 23 64 65 66 69 6e 65 20 59 59 5f 52 45 44  ).#define YY_RED
2995c 55 43 45 5f 4d 41 58 20 32 39 35 0a 73 74 61 74  UCE_MAX 295.stat
2995d 69 63 20 63 6f 6e 73 74 20 73 68 6f 72 74 20 79  ic const short y
2995e 79 5f 72 65 64 75 63 65 5f 6f 66 73 74 5b 5d 20  y_reduce_ofst[] 
2995f 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a 2f  = {. /*     0 */
29960 20 20 2d 31 33 39 2c 20 20 32 37 38 2c 20 20 32    -139,  278,  2
29961 39 35 2c 20 20 32 39 32 2c 20 20 34 30 32 2c 20  95,  292,  402, 
29962 20 2d 32 32 2c 20 20 34 30 38 2c 20 20 20 33 35   -22,  408,   35
29963 2c 20 20 20 33 37 2c 20 20 35 34 36 2c 0a 20 2f  ,   37,  546,. /
29964 2a 20 20 20 20 31 30 20 2a 2f 20 20 20 20 2d 33  *    10 */    -3
29965 2c 20 2d 31 32 38 2c 20 20 31 33 33 2c 20 20 32  , -128,  133,  2
29966 38 32 2c 20 20 34 31 31 2c 20 20 34 31 37 2c 20  82,  411,  417, 
29967 20 34 32 30 2c 20 2d 31 34 33 2c 20 20 35 30 33   420, -143,  503
29968 2c 20 20 32 31 33 2c 0a 20 2f 2a 20 20 20 20 32  ,  213,. /*    2
29969 30 20 2a 2f 20 20 20 31 35 31 2c 20 20 33 35 33  0 */   151,  353
2996a 2c 20 20 33 35 34 2c 20 20 34 36 30 2c 20 20 32  ,  354,  460,  2
2996b 32 34 2c 20 20 32 32 34 2c 20 20 32 32 34 2c 20  24,  224,  224, 
2996c 20 32 32 34 2c 20 20 32 32 34 2c 20 20 32 32 34   224,  224,  224
2996d 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20 20  ,. /*    30 */  
2996e 20 32 32 34 2c 20 20 32 32 34 2c 20 20 32 32 34   224,  224,  224
2996f 2c 20 20 32 32 34 2c 20 20 32 32 34 2c 20 20 32  ,  224,  224,  2
29970 32 34 2c 20 20 32 32 34 2c 20 20 32 32 34 2c 20  24,  224,  224, 
29971 20 32 32 34 2c 20 20 32 32 34 2c 0a 20 2f 2a 20   224,  224,. /* 
29972 20 20 20 34 30 20 2a 2f 20 20 20 32 32 34 2c 20     40 */   224, 
29973 20 32 32 34 2c 20 20 32 32 34 2c 20 20 32 32 34   224,  224,  224
29974 2c 20 20 32 32 34 2c 20 20 32 32 34 2c 20 20 32  ,  224,  224,  2
29975 32 34 2c 20 20 32 32 34 2c 20 20 32 32 34 2c 20  24,  224,  224, 
29976 20 32 32 34 2c 0a 20 2f 2a 20 20 20 20 35 30 20   224,. /*    50 
29977 2a 2f 20 20 20 32 32 34 2c 20 20 32 32 34 2c 20  */   224,  224, 
29978 20 32 32 34 2c 20 20 32 32 34 2c 20 20 32 32 34   224,  224,  224
29979 2c 20 20 32 32 34 2c 20 20 34 38 33 2c 20 20 36  ,  224,  483,  6
2997a 35 36 2c 20 20 36 39 39 2c 20 20 37 32 33 2c 0a  56,  699,  723,.
2997b 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20 37   /*    60 */   7
2997c 32 35 2c 20 20 37 33 32 2c 20 20 37 34 34 2c 20  25,  732,  744, 
2997d 20 37 34 39 2c 20 20 37 35 36 2c 20 20 37 35 38   749,  756,  758
2997e 2c 20 20 37 36 33 2c 20 20 37 37 30 2c 20 20 37  ,  763,  770,  7
2997f 39 36 2c 20 20 38 30 37 2c 0a 20 2f 2a 20 20 20  96,  807,. /*   
29980 20 37 30 20 2a 2f 20 20 20 38 30 39 2c 20 20 38   70 */   809,  8
29981 31 32 2c 20 20 38 34 37 2c 20 20 38 35 37 2c 20  12,  847,  857, 
29982 20 38 36 30 2c 20 20 38 37 36 2c 20 20 38 37 38   860,  876,  878
29983 2c 20 20 38 38 31 2c 20 20 38 38 34 2c 20 20 38  ,  881,  884,  8
29984 38 36 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a 2f  86,. /*    80 */
29985 20 20 20 38 39 34 2c 20 20 38 39 37 2c 20 20 39     894,  897,  9
29986 30 32 2c 20 20 39 30 35 2c 20 20 39 32 31 2c 20  02,  905,  921, 
29987 20 39 32 39 2c 20 20 39 33 31 2c 20 20 39 33 34   929,  931,  934
29988 2c 20 20 39 33 36 2c 20 20 39 33 38 2c 0a 20 2f  ,  936,  938,. /
29989 2a 20 20 20 20 39 30 20 2a 2f 20 20 20 39 34 32  *    90 */   942
2998a 2c 20 20 39 34 37 2c 20 20 39 34 39 2c 20 20 39  ,  947,  949,  9
2998b 35 35 2c 20 20 39 36 37 2c 20 20 39 37 33 2c 20  55,  967,  973, 
2998c 20 39 37 39 2c 20 20 32 32 34 2c 20 20 32 32 34   979,  224,  224
2998d 2c 20 20 32 32 34 2c 0a 20 2f 2a 20 20 20 31 30  ,  224,. /*   10
2998e 30 20 2a 2f 20 20 20 32 32 34 2c 20 20 31 36 38  0 */   224,  168
2998f 2c 20 20 32 32 34 2c 20 20 32 32 34 2c 20 20 20  ,  224,  224,   
29990 33 36 2c 20 20 20 33 33 2c 20 20 32 31 30 2c 20  36,   33,  210, 
29991 20 34 38 34 2c 20 20 32 32 34 2c 20 2d 31 32 31   484,  224, -121
29992 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20  ,. /*   110 */  
29993 20 32 32 34 2c 20 20 32 32 34 2c 20 20 32 32 34   224,  224,  224
29994 2c 20 20 32 32 34 2c 20 20 20 34 35 2c 20 20 20  ,  224,   45,   
29995 32 31 2c 20 20 20 20 38 2c 20 20 31 30 39 2c 20  21,    8,  109, 
29996 20 34 38 37 2c 20 20 34 38 37 2c 0a 20 2f 2a 20   487,  487,. /* 
29997 20 20 31 32 30 20 2a 2f 20 20 20 31 36 34 2c 20    120 */   164, 
29998 20 20 39 39 2c 20 20 32 32 32 2c 20 20 35 34 31    99,  222,  541
29999 2c 20 20 2d 39 31 2c 20 20 20 2d 31 2c 20 20 34  ,  -91,   -1,  4
2999a 37 34 2c 20 20 35 39 38 2c 20 20 35 38 37 2c 20  74,  598,  587, 
2999b 20 36 37 37 2c 0a 20 2f 2a 20 20 20 31 33 30 20   677,. /*   130 
2999c 2a 2f 20 20 20 36 33 38 2c 20 20 34 32 39 2c 20  */   638,  429, 
2999d 20 37 31 33 2c 20 20 36 33 39 2c 20 20 36 34 31   713,  639,  641
2999e 2c 20 20 36 37 34 2c 20 20 36 37 36 2c 20 20 37  ,  674,  676,  7
2999f 31 36 2c 20 20 37 31 39 2c 20 20 36 38 36 2c 0a  16,  719,  686,.
299a0 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20 37   /*   140 */   7
299a1 37 36 2c 20 20 32 35 37 2c 20 20 33 36 32 2c 20  76,  257,  362, 
299a2 20 38 30 32 2c 20 20 38 30 36 2c 20 20 38 32 30   802,  806,  820
299a3 2c 20 20 35 34 35 2c 20 20 35 38 32 2c 20 20 36  ,  545,  582,  6
299a4 36 39 2c 20 20 2d 36 30 2c 0a 20 2f 2a 20 20 20  69,  -60,. /*   
299a5 31 35 30 20 2a 2f 20 20 20 2d 31 35 2c 20 20 31  150 */   -15,  1
299a6 32 38 2c 20 20 31 37 38 2c 20 20 33 31 37 2c 20  28,  178,  317, 
299a7 20 20 34 30 2c 20 20 33 31 37 2c 20 20 33 31 37    40,  317,  317
299a8 2c 20 20 33 37 37 2c 20 20 34 34 31 2c 20 20 34  ,  377,  441,  4
299a9 38 31 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a 2f  81,. /*   160 */
299aa 20 20 20 34 39 39 2c 20 20 35 30 32 2c 20 20 35     499,  502,  5
299ab 31 30 2c 20 20 35 35 33 2c 20 20 35 38 36 2c 20  10,  553,  586, 
299ac 20 35 39 36 2c 20 20 35 30 32 2c 20 20 36 38 34   596,  502,  684
299ad 2c 20 20 37 31 37 2c 20 20 37 36 38 2c 0a 20 2f  ,  717,  768,. /
299ae 2a 20 20 20 31 37 30 20 2a 2f 20 20 20 37 38 38  *   170 */   788
299af 2c 20 20 37 38 36 2c 20 20 38 34 36 2c 20 20 38  ,  786,  846,  8
299b0 35 34 2c 20 20 38 35 38 2c 20 20 33 31 37 2c 20  54,  858,  317, 
299b1 20 37 38 36 2c 20 20 38 37 31 2c 20 20 38 38 38   786,  871,  888
299b2 2c 20 20 38 36 34 2c 0a 20 2f 2a 20 20 20 31 38  ,  864,. /*   18
299b3 30 20 2a 2f 20 20 20 38 38 37 2c 20 20 39 30 36  0 */   887,  906
299b4 2c 20 20 39 32 36 2c 20 20 39 34 36 2c 20 20 39  ,  926,  946,  9
299b5 38 30 2c 20 20 33 31 37 2c 20 20 39 39 38 2c 20  80,  317,  998, 
299b6 31 30 30 30 2c 20 20 33 31 37 2c 20 31 30 30 33  1000,  317, 1003
299b7 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20 20  ,. /*   190 */  
299b8 20 39 30 33 2c 20 20 38 30 35 2c 20 31 30 30 34   903,  805, 1004
299b9 2c 20 31 30 30 35 2c 20 20 33 31 37 2c 20 31 30  , 1005,  317, 10
299ba 30 37 2c 20 31 30 30 38 2c 20 31 30 30 39 2c 20  07, 1008, 1009, 
299bb 20 33 31 37 2c 20 31 30 31 30 2c 0a 20 2f 2a 20   317, 1010,. /* 
299bc 20 20 32 30 30 20 2a 2f 20 20 31 30 31 31 2c 20    200 */  1011, 
299bd 31 30 31 32 2c 20 20 33 31 37 2c 20 20 33 31 37  1012,  317,  317
299be 2c 20 31 30 31 33 2c 20 31 30 31 34 2c 20 31 30  , 1013, 1014, 10
299bf 31 35 2c 20 31 30 31 36 2c 20 31 30 31 37 2c 20  15, 1016, 1017, 
299c0 20 39 38 35 2c 0a 20 2f 2a 20 20 20 32 31 30 20   985,. /*   210 
299c1 2a 2f 20 20 20 39 36 32 2c 20 20 39 37 34 2c 20  */   962,  974, 
299c2 31 30 31 38 2c 20 20 39 33 37 2c 20 20 39 33 39  1018,  937,  939
299c3 2c 20 20 39 34 35 2c 20 20 39 39 33 2c 20 20 39  ,  945,  993,  9
299c4 39 39 2c 20 20 39 36 36 2c 20 31 30 30 31 2c 0a  99,  966, 1001,.
299c5 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 20 39   /*   220 */   9
299c6 39 35 2c 20 31 30 30 32 2c 20 31 30 31 39 2c 20  95, 1002, 1019, 
299c7 31 30 32 30 2c 20 31 30 32 31 2c 20 31 30 32 32  1020, 1021, 1022
299c8 2c 20 20 39 38 31 2c 20 20 39 37 37 2c 20 20 39  ,  981,  977,  9
299c9 38 33 2c 20 20 39 38 34 2c 0a 20 2f 2a 20 20 20  83,  984,. /*   
299ca 32 33 30 20 2a 2f 20 20 31 30 33 31 2c 20 31 30  230 */  1031, 10
299cb 33 37 2c 20 31 30 33 39 2c 20 31 30 34 30 2c 20  37, 1039, 1040, 
299cc 31 30 33 34 2c 20 31 30 32 33 2c 20 20 39 39 36  1034, 1023,  996
299cd 2c 20 31 30 32 34 2c 20 31 30 32 35 2c 20 31 30  , 1024, 1025, 10
299ce 32 36 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a 2f  26,. /*   240 */
299cf 20 20 31 30 32 37 2c 20 20 39 37 35 2c 20 31 30    1027,  975, 10
299d0 34 38 2c 20 31 30 34 39 2c 20 31 30 32 38 2c 20  48, 1049, 1028, 
299d1 31 30 32 39 2c 20 31 30 33 32 2c 20 31 30 33 30  1029, 1032, 1030
299d2 2c 20 31 30 33 35 2c 20 31 30 33 36 2c 0a 20 2f  , 1035, 1036,. /
299d3 2a 20 20 20 32 35 30 20 2a 2f 20 20 31 30 34 31  *   250 */  1041
299d4 2c 20 31 30 34 32 2c 20 31 30 34 33 2c 20 31 30  , 1042, 1043, 10
299d5 34 34 2c 20 31 30 35 30 2c 20 20 39 39 31 2c 20  44, 1050,  991, 
299d6 20 39 39 32 2c 20 31 30 34 37 2c 20 31 30 35 31   992, 1047, 1051
299d7 2c 20 31 30 35 36 2c 0a 20 2f 2a 20 20 20 32 36  , 1056,. /*   26
299d8 30 20 2a 2f 20 20 31 30 35 37 2c 20 31 30 35 35  0 */  1057, 1055
299d9 2c 20 31 30 36 33 2c 20 31 30 37 38 2c 20 31 30  , 1063, 1078, 10
299da 38 30 2c 20 31 30 38 32 2c 20 31 30 38 31 2c 20  80, 1082, 1081, 
299db 31 30 38 36 2c 20 31 30 38 39 2c 20 31 30 33 33  1086, 1089, 1033
299dc 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20  ,. /*   270 */  
299dd 31 30 34 35 2c 20 31 30 39 31 2c 20 31 30 39 32  1045, 1091, 1092
299de 2c 20 31 30 36 32 2c 20 31 30 36 34 2c 20 31 30  , 1062, 1064, 10
299df 34 36 2c 20 31 30 35 33 2c 20 31 30 39 37 2c 20  46, 1053, 1097, 
299e0 31 30 36 35 2c 20 31 30 36 36 2c 0a 20 2f 2a 20  1065, 1066,. /* 
299e1 20 20 32 38 30 20 2a 2f 20 20 31 30 36 39 2c 20    280 */  1069, 
299e2 31 30 37 30 2c 20 31 30 36 30 2c 20 31 30 37 31  1070, 1060, 1071
299e3 2c 20 31 30 35 34 2c 20 31 30 39 39 2c 20 31 30  , 1054, 1099, 10
299e4 35 32 2c 20 31 30 35 39 2c 20 31 31 30 38 2c 20  52, 1059, 1108, 
299e5 31 31 31 34 2c 0a 20 2f 2a 20 20 20 32 39 30 20  1114,. /*   290 
299e6 2a 2f 20 20 31 30 37 32 2c 20 31 31 30 34 2c 20  */  1072, 1104, 
299e7 31 30 39 30 2c 20 31 30 39 33 2c 20 31 30 37 35  1090, 1093, 1075
299e8 2c 20 31 30 37 39 2c 0a 7d 3b 0a 73 74 61 74 69  , 1079,.};.stati
299e9 63 20 63 6f 6e 73 74 20 59 59 41 43 54 49 4f 4e  c const YYACTION
299ea 54 59 50 45 20 79 79 5f 64 65 66 61 75 6c 74 5b  TYPE yy_default[
299eb 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20  ] = {. /*     0 
299ec 2a 2f 20 20 20 36 30 33 2c 20 20 38 33 32 2c 20  */   603,  832, 
299ed 20 39 31 33 2c 20 20 37 31 39 2c 20 20 39 31 33   913,  719,  913
299ee 2c 20 20 38 33 32 2c 20 20 39 31 33 2c 20 20 39  ,  832,  913,  9
299ef 31 33 2c 20 20 38 35 39 2c 20 20 39 31 33 2c 0a  13,  859,  913,.
299f0 20 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20 20 37   /*    10 */   7
299f1 32 33 2c 20 20 38 38 38 2c 20 20 38 33 30 2c 20  23,  888,  830, 
299f2 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
299f3 2c 20 20 39 31 33 2c 20 20 38 30 34 2c 20 20 39  ,  913,  804,  9
299f4 31 33 2c 20 20 38 35 39 2c 0a 20 2f 2a 20 20 20  13,  859,. /*   
299f5 20 32 30 20 2a 2f 20 20 20 39 31 33 2c 20 20 36   20 */   913,  6
299f6 33 35 2c 20 20 38 35 39 2c 20 20 38 35 39 2c 20  35,  859,  859, 
299f7 20 37 35 35 2c 20 20 39 31 33 2c 20 20 39 31 33   755,  913,  913
299f8 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
299f9 31 33 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f  13,. /*    30 */
299fa 20 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39     913,  913,  9
299fb 31 33 2c 20 20 37 35 36 2c 20 20 39 31 33 2c 20  13,  756,  913, 
299fc 20 38 33 34 2c 20 20 38 32 39 2c 20 20 38 32 35   834,  829,  825
299fd 2c 20 20 38 32 37 2c 20 20 38 32 36 2c 0a 20 2f  ,  827,  826,. /
299fe 2a 20 20 20 20 34 30 20 2a 2f 20 20 20 38 33 33  *    40 */   833
299ff 2c 20 20 37 35 37 2c 20 20 37 34 36 2c 20 20 37  ,  757,  746,  7
29a00 35 33 2c 20 20 37 36 30 2c 20 20 37 33 35 2c 20  53,  760,  735, 
29a01 20 38 37 32 2c 20 20 37 36 32 2c 20 20 37 36 33   872,  762,  763
29a02 2c 20 20 37 36 39 2c 0a 20 2f 2a 20 20 20 20 35  ,  769,. /*    5
29a03 30 20 2a 2f 20 20 20 37 37 30 2c 20 20 38 38 39  0 */   770,  889
29a04 2c 20 20 38 38 37 2c 20 20 37 39 32 2c 20 20 37  ,  887,  792,  7
29a05 39 31 2c 20 20 38 31 30 2c 20 20 39 31 33 2c 20  91,  810,  913, 
29a06 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
29a07 2c 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20  ,. /*    60 */  
29a08 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
29a09 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
29a0a 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
29a0b 20 39 31 33 2c 20 20 39 31 33 2c 0a 20 2f 2a 20   913,  913,. /* 
29a0c 20 20 20 37 30 20 2a 2f 20 20 20 39 31 33 2c 20     70 */   913, 
29a0d 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
29a0e 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
29a0f 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
29a10 20 39 31 33 2c 0a 20 2f 2a 20 20 20 20 38 30 20   913,. /*    80 
29a11 2a 2f 20 20 20 39 31 33 2c 20 20 39 31 33 2c 20  */   913,  913, 
29a12 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
29a13 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
29a14 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 0a  13,  913,  913,.
29a15 20 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20 20 39   /*    90 */   9
29a16 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
29a17 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
29a18 2c 20 20 39 31 33 2c 20 20 37 39 34 2c 20 20 38  ,  913,  794,  8
29a19 31 36 2c 20 20 37 39 33 2c 0a 20 2f 2a 20 20 20  16,  793,. /*   
29a1a 31 30 30 20 2a 2f 20 20 20 38 30 33 2c 20 20 36  100 */   803,  6
29a1b 32 38 2c 20 20 37 39 35 2c 20 20 37 39 36 2c 20  28,  795,  796, 
29a1c 20 36 38 38 2c 20 20 36 32 33 2c 20 20 39 31 33   688,  623,  913
29a1d 2c 20 20 39 31 33 2c 20 20 37 39 37 2c 20 20 39  ,  913,  797,  9
29a1e 31 33 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f  13,. /*   110 */
29a1f 20 20 20 37 39 38 2c 20 20 38 31 31 2c 20 20 38     798,  811,  8
29a20 31 32 2c 20 20 38 31 33 2c 20 20 39 31 33 2c 20  12,  813,  913, 
29a21 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
29a22 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 0a 20 2f  ,  913,  913,. /
29a23 2a 20 20 20 31 32 30 20 2a 2f 20 20 20 36 30 33  *   120 */   603
29a24 2c 20 20 37 31 39 2c 20 20 39 31 33 2c 20 20 37  ,  719,  913,  7
29a25 31 39 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  19,  913,  913, 
29a26 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
29a27 2c 20 20 39 31 33 2c 0a 20 2f 2a 20 20 20 31 33  ,  913,. /*   13
29a28 30 20 2a 2f 20 20 20 39 31 33 2c 20 20 39 31 33  0 */   913,  913
29a29 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
29a2a 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
29a2b 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
29a2c 2c 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20  ,. /*   140 */  
29a2d 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
29a2e 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
29a2f 31 33 2c 20 20 37 31 33 2c 20 20 37 32 33 2c 20  13,  713,  723, 
29a30 20 39 30 36 2c 20 20 39 31 33 2c 0a 20 2f 2a 20   906,  913,. /* 
29a31 20 20 31 35 30 20 2a 2f 20 20 20 39 31 33 2c 20    150 */   913, 
29a32 20 36 37 39 2c 20 20 39 31 33 2c 20 20 39 31 33   679,  913,  913
29a33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
29a34 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
29a35 20 39 31 33 2c 0a 20 2f 2a 20 20 20 31 36 30 20   913,. /*   160 
29a36 2a 2f 20 20 20 39 31 33 2c 20 20 39 31 33 2c 20  */   913,  913, 
29a37 20 36 31 31 2c 20 20 36 30 39 2c 20 20 39 31 33   611,  609,  913
29a38 2c 20 20 37 31 31 2c 20 20 39 31 33 2c 20 20 39  ,  711,  913,  9
29a39 31 33 2c 20 20 36 33 37 2c 20 20 39 31 33 2c 0a  13,  637,  913,.
29a3a 20 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20 20 39   /*   170 */   9
29a3b 31 33 2c 20 20 37 32 31 2c 20 20 39 31 33 2c 20  13,  721,  913, 
29a3c 20 39 31 33 2c 20 20 39 31 33 2c 20 20 37 32 36   913,  913,  726
29a3d 2c 20 20 37 32 37 2c 20 20 39 31 33 2c 20 20 39  ,  727,  913,  9
29a3e 31 33 2c 20 20 39 31 33 2c 0a 20 2f 2a 20 20 20  13,  913,. /*   
29a3f 31 38 30 20 2a 2f 20 20 20 39 31 33 2c 20 20 39  180 */   913,  9
29a40 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
29a41 20 39 31 33 2c 20 20 36 32 35 2c 20 20 39 31 33   913,  625,  913
29a42 2c 20 20 39 31 33 2c 20 20 37 30 30 2c 20 20 39  ,  913,  700,  9
29a43 31 33 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f  13,. /*   190 */
29a44 20 20 20 38 36 35 2c 20 20 39 31 33 2c 20 20 39     865,  913,  9
29a45 31 33 2c 20 20 39 31 33 2c 20 20 38 37 39 2c 20  13,  913,  879, 
29a46 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
29a47 2c 20 20 38 37 37 2c 20 20 39 31 33 2c 0a 20 2f  ,  877,  913,. /
29a48 2a 20 20 20 32 30 30 20 2a 2f 20 20 20 39 31 33  *   200 */   913
29a49 2c 20 20 39 31 33 2c 20 20 37 30 32 2c 20 20 37  ,  913,  702,  7
29a4a 36 35 2c 20 20 38 34 35 2c 20 20 39 31 33 2c 20  65,  845,  913, 
29a4b 20 38 39 32 2c 20 20 38 39 34 2c 20 20 39 31 33   892,  894,  913
29a4c 2c 20 20 39 31 33 2c 0a 20 2f 2a 20 20 20 32 31  ,  913,. /*   21
29a4d 30 20 2a 2f 20 20 20 37 31 31 2c 20 20 37 32 30  0 */   711,  720
29a4e 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
29a4f 31 33 2c 20 20 38 32 38 2c 20 20 37 34 39 2c 20  13,  828,  749, 
29a50 20 37 34 39 2c 20 20 37 33 37 2c 20 20 37 34 39   749,  737,  749
29a51 2c 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20  ,. /*   220 */  
29a52 20 36 35 38 2c 20 20 37 34 39 2c 20 20 39 31 33   658,  749,  913
29a53 2c 20 20 37 34 39 2c 20 20 39 31 33 2c 20 20 36  ,  749,  913,  6
29a54 36 31 2c 20 20 37 35 39 2c 20 20 37 33 37 2c 20  61,  759,  737, 
29a55 20 37 33 37 2c 20 20 37 35 39 2c 0a 20 2f 2a 20   737,  759,. /* 
29a56 20 20 32 33 30 20 2a 2f 20 20 20 36 30 38 2c 20    230 */   608, 
29a57 20 36 30 38 2c 20 20 36 30 38 2c 20 20 36 30 38   608,  608,  608
29a58 2c 20 20 36 37 38 2c 20 20 39 31 33 2c 20 20 37  ,  678,  913,  7
29a59 35 39 2c 20 20 37 35 30 2c 20 20 37 35 32 2c 20  59,  750,  752, 
29a5a 20 37 34 32 2c 0a 20 2f 2a 20 20 20 32 34 30 20   742,. /*   240 
29a5b 2a 2f 20 20 20 37 35 34 2c 20 20 39 31 33 2c 20  */   754,  913, 
29a5c 20 37 32 38 2c 20 20 37 32 38 2c 20 20 37 33 36   728,  728,  736
29a5d 2c 20 20 37 34 31 2c 20 20 37 33 36 2c 20 20 37  ,  741,  736,  7
29a5e 34 31 2c 20 20 36 39 30 2c 20 20 36 39 30 2c 0a  41,  690,  690,.
29a5f 20 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20 20 36   /*   250 */   6
29a60 37 35 2c 20 20 36 39 30 2c 20 20 36 36 31 2c 20  75,  690,  661, 
29a61 20 36 39 30 2c 20 20 38 33 38 2c 20 20 38 34 32   690,  838,  842
29a62 2c 20 20 38 34 32 2c 20 20 36 37 35 2c 20 20 36  ,  842,  675,  6
29a63 39 30 2c 20 20 36 39 30 2c 0a 20 2f 2a 20 20 20  90,  690,. /*   
29a64 32 36 30 20 2a 2f 20 20 20 36 39 30 2c 20 20 38  260 */   690,  8
29a65 33 38 2c 20 20 36 32 30 2c 20 20 37 32 38 2c 20  38,  620,  728, 
29a66 20 36 32 30 2c 20 20 37 32 38 2c 20 20 36 32 30   620,  728,  620
29a67 2c 20 20 37 32 38 2c 20 20 37 32 38 2c 20 20 38  ,  728,  728,  8
29a68 36 39 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f  69,. /*   270 */
29a69 20 20 20 38 37 31 2c 20 20 36 32 30 2c 20 20 37     871,  620,  7
29a6a 32 38 2c 20 20 36 39 32 2c 20 20 36 39 32 2c 20  28,  692,  692, 
29a6b 20 37 37 31 2c 20 20 37 35 39 2c 20 20 37 32 38   771,  759,  728
29a6c 2c 20 20 36 39 39 2c 20 20 36 39 39 2c 0a 20 2f  ,  699,  699,. /
29a6d 2a 20 20 20 32 38 30 20 2a 2f 20 20 20 36 39 39  *   280 */   699
29a6e 2c 20 20 36 39 39 2c 20 20 37 35 39 2c 20 20 36  ,  699,  759,  6
29a6f 39 32 2c 20 20 37 37 31 2c 20 20 37 32 38 2c 20  92,  771,  728, 
29a70 20 38 39 31 2c 20 20 38 39 31 2c 20 20 37 32 38   891,  891,  728
29a71 2c 20 20 37 32 38 2c 0a 20 2f 2a 20 20 20 32 39  ,  728,. /*   29
29a72 30 20 2a 2f 20 20 20 38 39 39 2c 20 20 36 34 35  0 */   899,  645
29a73 2c 20 20 36 36 33 2c 20 20 36 36 33 2c 20 20 39  ,  663,  663,  9
29a74 30 36 2c 20 20 39 31 31 2c 20 20 39 31 33 2c 20  06,  911,  913, 
29a75 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
29a76 2c 0a 20 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20  ,. /*   300 */  
29a77 20 37 37 38 2c 20 20 39 31 33 2c 20 20 39 31 33   778,  913,  913
29a78 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
29a79 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
29a7a 20 39 31 33 2c 20 20 39 31 33 2c 0a 20 2f 2a 20   913,  913,. /* 
29a7b 20 20 33 31 30 20 2a 2f 20 20 20 39 31 33 2c 20    310 */   913, 
29a7c 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
29a7d 2c 20 20 38 35 32 2c 20 20 39 31 33 2c 20 20 39  ,  852,  913,  9
29a7e 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
29a7f 20 37 38 33 2c 0a 20 2f 2a 20 20 20 33 32 30 20   783,. /*   320 
29a80 2a 2f 20 20 20 37 37 39 2c 20 20 39 31 33 2c 20  */   779,  913, 
29a81 20 37 38 30 2c 20 20 39 31 33 2c 20 20 37 30 35   780,  913,  705
29a82 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
29a83 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 0a  13,  913,  913,.
29a84 20 2f 2a 20 20 20 33 33 30 20 2a 2f 20 20 20 39   /*   330 */   9
29a85 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
29a86 20 39 31 33 2c 20 20 39 31 33 2c 20 20 38 33 31   913,  913,  831
29a87 2c 20 20 39 31 33 2c 20 20 37 34 33 2c 20 20 39  ,  913,  743,  9
29a88 31 33 2c 20 20 37 35 31 2c 0a 20 2f 2a 20 20 20  13,  751,. /*   
29a89 33 34 30 20 2a 2f 20 20 20 39 31 33 2c 20 20 39  340 */   913,  9
29a8a 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
29a8b 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
29a8c 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
29a8d 31 33 2c 0a 20 2f 2a 20 20 20 33 35 30 20 2a 2f  13,. /*   350 */
29a8e 20 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39     913,  913,  9
29a8f 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
29a90 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
29a91 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 0a 20 2f  ,  913,  913,. /
29a92 2a 20 20 20 33 36 30 20 2a 2f 20 20 20 39 31 33  *   360 */   913
29a93 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
29a94 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
29a95 20 38 36 37 2c 20 20 38 36 38 2c 20 20 39 31 33   867,  868,  913
29a96 2c 20 20 39 31 33 2c 0a 20 2f 2a 20 20 20 33 37  ,  913,. /*   37
29a97 30 20 2a 2f 20 20 20 39 31 33 2c 20 20 39 31 33  0 */   913,  913
29a98 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
29a99 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
29a9a 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
29a9b 2c 0a 20 2f 2a 20 20 20 33 38 30 20 2a 2f 20 20  ,. /*   380 */  
29a9c 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
29a9d 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
29a9e 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
29a9f 20 39 31 33 2c 20 20 39 31 33 2c 0a 20 2f 2a 20   913,  913,. /* 
29aa0 20 20 33 39 30 20 2a 2f 20 20 20 39 31 33 2c 20    390 */   913, 
29aa1 20 38 39 38 2c 20 20 39 31 33 2c 20 20 39 31 33   898,  913,  913
29aa2 2c 20 20 39 30 31 2c 20 20 36 30 34 2c 20 20 39  ,  901,  604,  9
29aa3 31 33 2c 20 20 35 39 39 2c 20 20 36 30 31 2c 20  13,  599,  601, 
29aa4 20 36 30 32 2c 0a 20 2f 2a 20 20 20 34 30 30 20   602,. /*   400 
29aa5 2a 2f 20 20 20 36 30 36 2c 20 20 36 30 37 2c 20  */   606,  607, 
29aa6 20 36 31 30 2c 20 20 36 33 32 2c 20 20 36 33 33   610,  632,  633
29aa7 2c 20 20 36 33 34 2c 20 20 36 31 32 2c 20 20 36  ,  634,  612,  6
29aa8 31 33 2c 20 20 36 31 34 2c 20 20 36 31 35 2c 0a  13,  614,  615,.
29aa9 20 2f 2a 20 20 20 34 31 30 20 2a 2f 20 20 20 36   /*   410 */   6
29aaa 31 36 2c 20 20 36 31 37 2c 20 20 36 31 38 2c 20  16,  617,  618, 
29aab 20 36 32 34 2c 20 20 36 32 36 2c 20 20 36 34 34   624,  626,  644
29aac 2c 20 20 36 34 36 2c 20 20 36 33 30 2c 20 20 36  ,  646,  630,  6
29aad 34 38 2c 20 20 37 30 39 2c 0a 20 2f 2a 20 20 20  48,  709,. /*   
29aae 34 32 30 20 2a 2f 20 20 20 37 31 30 2c 20 20 37  420 */   710,  7
29aaf 37 35 2c 20 20 37 30 33 2c 20 20 37 30 34 2c 20  75,  703,  704, 
29ab0 20 37 30 38 2c 20 20 36 33 31 2c 20 20 37 38 36   708,  631,  786
29ab1 2c 20 20 37 37 37 2c 20 20 37 38 31 2c 20 20 37  ,  777,  781,  7
29ab2 38 32 2c 0a 20 2f 2a 20 20 20 34 33 30 20 2a 2f  82,. /*   430 */
29ab3 20 20 20 37 38 34 2c 20 20 37 38 35 2c 20 20 37     784,  785,  7
29ab4 39 39 2c 20 20 38 30 30 2c 20 20 38 30 32 2c 20  99,  800,  802, 
29ab5 20 38 30 38 2c 20 20 38 31 35 2c 20 20 38 31 38   808,  815,  818
29ab6 2c 20 20 38 30 31 2c 20 20 38 30 36 2c 0a 20 2f  ,  801,  806,. /
29ab7 2a 20 20 20 34 34 30 20 2a 2f 20 20 20 38 30 37  *   440 */   807
29ab8 2c 20 20 38 30 39 2c 20 20 38 31 34 2c 20 20 38  ,  809,  814,  8
29ab9 31 37 2c 20 20 37 30 36 2c 20 20 37 30 37 2c 20  17,  706,  707, 
29aba 20 38 32 31 2c 20 20 36 33 38 2c 20 20 36 33 39   821,  638,  639
29abb 2c 20 20 36 34 32 2c 0a 20 2f 2a 20 20 20 34 35  ,  642,. /*   45
29abc 30 20 2a 2f 20 20 20 36 34 33 2c 20 20 38 35 35  0 */   643,  855
29abd 2c 20 20 38 35 37 2c 20 20 38 35 36 2c 20 20 38  ,  857,  856,  8
29abe 35 38 2c 20 20 36 34 31 2c 20 20 36 34 30 2c 20  58,  641,  640, 
29abf 20 37 38 37 2c 20 20 37 39 30 2c 20 20 38 32 33   787,  790,  823
29ac0 2c 0a 20 2f 2a 20 20 20 34 36 30 20 2a 2f 20 20  ,. /*   460 */  
29ac1 20 38 32 34 2c 20 20 38 38 30 2c 20 20 38 38 31   824,  880,  881
29ac2 2c 20 20 38 38 32 2c 20 20 38 38 33 2c 20 20 38  ,  882,  883,  8
29ac3 38 34 2c 20 20 38 31 39 2c 20 20 37 32 39 2c 20  84,  819,  729, 
29ac4 20 38 32 32 2c 20 20 38 30 35 2c 0a 20 2f 2a 20   822,  805,. /* 
29ac5 20 20 34 37 30 20 2a 2f 20 20 20 37 34 34 2c 20    470 */   744, 
29ac6 20 37 34 37 2c 20 20 37 34 38 2c 20 20 37 34 35   747,  748,  745
29ac7 2c 20 20 37 31 32 2c 20 20 37 32 32 2c 20 20 37  ,  712,  722,  7
29ac8 33 31 2c 20 20 37 33 32 2c 20 20 37 33 33 2c 20  31,  732,  733, 
29ac9 20 37 33 34 2c 0a 20 2f 2a 20 20 20 34 38 30 20   734,. /*   480 
29aca 2a 2f 20 20 20 37 31 37 2c 20 20 37 31 38 2c 20  */   717,  718, 
29acb 20 37 32 34 2c 20 20 37 34 30 2c 20 20 37 37 33   724,  740,  773
29acc 2c 20 20 37 37 34 2c 20 20 37 33 38 2c 20 20 37  ,  774,  738,  7
29acd 33 39 2c 20 20 37 32 35 2c 20 20 37 31 34 2c 0a  39,  725,  714,.
29ace 20 2f 2a 20 20 20 34 39 30 20 2a 2f 20 20 20 37   /*   490 */   7
29acf 31 35 2c 20 20 37 31 36 2c 20 20 38 32 30 2c 20  15,  716,  820, 
29ad0 20 37 37 36 2c 20 20 37 38 38 2c 20 20 37 38 39   776,  788,  789
29ad1 2c 20 20 36 34 39 2c 20 20 36 35 30 2c 20 20 37  ,  649,  650,  7
29ad2 38 33 2c 20 20 36 35 31 2c 0a 20 2f 2a 20 20 20  83,  651,. /*   
29ad3 35 30 30 20 2a 2f 20 20 20 36 35 32 2c 20 20 36  500 */   652,  6
29ad4 35 33 2c 20 20 36 39 31 2c 20 20 36 39 34 2c 20  53,  691,  694, 
29ad5 20 36 39 35 2c 20 20 36 39 36 2c 20 20 36 35 34   695,  696,  654
29ad6 2c 20 20 36 37 33 2c 20 20 36 37 36 2c 20 20 36  ,  673,  676,  6
29ad7 37 37 2c 0a 20 2f 2a 20 20 20 35 31 30 20 2a 2f  77,. /*   510 */
29ad8 20 20 20 36 35 35 2c 20 20 36 36 32 2c 20 20 36     655,  662,  6
29ad9 35 36 2c 20 20 36 35 37 2c 20 20 36 36 34 2c 20  56,  657,  664, 
29ada 20 36 36 35 2c 20 20 36 36 36 2c 20 20 36 36 39   665,  666,  669
29adb 2c 20 20 36 37 30 2c 20 20 36 37 31 2c 0a 20 2f  ,  670,  671,. /
29adc 2a 20 20 20 35 32 30 20 2a 2f 20 20 20 36 37 32  *   520 */   672
29add 2c 20 20 36 36 37 2c 20 20 36 36 38 2c 20 20 38  ,  667,  668,  8
29ade 33 39 2c 20 20 38 34 30 2c 20 20 38 34 33 2c 20  39,  840,  843, 
29adf 20 38 34 31 2c 20 20 36 35 39 2c 20 20 36 36 30   841,  659,  660
29ae0 2c 20 20 36 37 34 2c 0a 20 2f 2a 20 20 20 35 33  ,  674,. /*   53
29ae1 30 20 2a 2f 20 20 20 36 34 37 2c 20 20 36 33 36  0 */   647,  636
29ae2 2c 20 20 36 32 39 2c 20 20 36 38 30 2c 20 20 36  ,  629,  680,  6
29ae3 38 33 2c 20 20 36 38 34 2c 20 20 36 38 35 2c 20  83,  684,  685, 
29ae4 20 36 38 36 2c 20 20 36 38 37 2c 20 20 36 38 39   686,  687,  689
29ae5 2c 0a 20 2f 2a 20 20 20 35 34 30 20 2a 2f 20 20  ,. /*   540 */  
29ae6 20 36 38 31 2c 20 20 36 38 32 2c 20 20 36 32 37   681,  682,  627
29ae7 2c 20 20 36 31 39 2c 20 20 36 32 31 2c 20 20 37  ,  619,  621,  7
29ae8 33 30 2c 20 20 38 36 31 2c 20 20 38 37 30 2c 20  30,  861,  870, 
29ae9 20 38 36 36 2c 20 20 38 36 32 2c 0a 20 2f 2a 20   866,  862,. /* 
29aea 20 20 35 35 30 20 2a 2f 20 20 20 38 36 33 2c 20    550 */   863, 
29aeb 20 38 36 34 2c 20 20 36 32 32 2c 20 20 38 33 35   864,  622,  835
29aec 2c 20 20 38 33 36 2c 20 20 36 39 33 2c 20 20 37  ,  836,  693,  7
29aed 36 37 2c 20 20 37 36 38 2c 20 20 38 36 30 2c 20  67,  768,  860, 
29aee 20 38 37 33 2c 0a 20 2f 2a 20 20 20 35 36 30 20   873,. /*   560 
29aef 2a 2f 20 20 20 38 37 35 2c 20 20 37 37 32 2c 20  */   875,  772, 
29af0 20 38 37 36 2c 20 20 38 37 38 2c 20 20 38 37 34   876,  878,  874
29af1 2c 20 20 39 30 33 2c 20 20 36 39 37 2c 20 20 36  ,  903,  697,  6
29af2 39 38 2c 20 20 37 30 31 2c 20 20 38 34 34 2c 0a  98,  701,  844,.
29af3 20 2f 2a 20 20 20 35 37 30 20 2a 2f 20 20 20 38   /*   570 */   8
29af4 38 35 2c 20 20 37 35 38 2c 20 20 37 36 31 2c 20  85,  758,  761, 
29af5 20 37 36 34 2c 20 20 37 36 36 2c 20 20 38 34 36   764,  766,  846
29af6 2c 20 20 38 34 37 2c 20 20 38 34 38 2c 20 20 38  ,  847,  848,  8
29af7 34 39 2c 20 20 38 35 30 2c 0a 20 2f 2a 20 20 20  49,  850,. /*   
29af8 35 38 30 20 2a 2f 20 20 20 38 35 33 2c 20 20 38  580 */   853,  8
29af9 35 34 2c 20 20 38 35 31 2c 20 20 38 38 36 2c 20  54,  851,  886, 
29afa 20 38 39 30 2c 20 20 38 39 33 2c 20 20 38 39 35   890,  893,  895
29afb 2c 20 20 38 39 36 2c 20 20 38 39 37 2c 20 20 39  ,  896,  897,  9
29afc 30 30 2c 0a 20 2f 2a 20 20 20 35 39 30 20 2a 2f  00,. /*   590 */
29afd 20 20 20 39 30 32 2c 20 20 39 30 37 2c 20 20 39     902,  907,  9
29afe 30 38 2c 20 20 39 30 39 2c 20 20 39 31 32 2c 20  08,  909,  912, 
29aff 20 39 31 30 2c 20 20 36 30 35 2c 20 20 36 30 30   910,  605,  600
29b00 2c 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 59 59 5f  ,.};.#define YY_
29b01 53 5a 5f 41 43 54 54 41 42 20 28 69 6e 74 29 28  SZ_ACTTAB (int)(
29b02 73 69 7a 65 6f 66 28 79 79 5f 61 63 74 69 6f 6e  sizeof(yy_action
29b03 29 2f 73 69 7a 65 6f 66 28 79 79 5f 61 63 74 69  )/sizeof(yy_acti
29b04 6f 6e 5b 30 5d 29 29 0a 0a 2f 2a 20 54 68 65 20  on[0]))../* The 
29b05 6e 65 78 74 20 74 61 62 6c 65 20 6d 61 70 73 20  next table maps 
29b06 74 6f 6b 65 6e 73 20 69 6e 74 6f 20 66 61 6c 6c  tokens into fall
29b07 62 61 63 6b 20 74 6f 6b 65 6e 73 2e 20 20 49 66  back tokens.  If
29b08 20 61 20 63 6f 6e 73 74 72 75 63 74 0a 2a 2a 20   a construct.** 
29b09 6c 69 6b 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69  like the followi
29b0a 6e 67 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 20 20 20  ng:.** .**      
29b0b 25 66 61 6c 6c 62 61 63 6b 20 49 44 20 58 20 59  %fallback ID X Y
29b0c 20 5a 2e 0a 2a 2a 0a 2a 2a 20 61 70 70 65 61 72   Z..**.** appear
29b0d 73 20 69 6e 20 74 68 65 20 67 72 61 6d 6d 61 72  s in the grammar
29b0e 2c 20 74 68 65 6e 20 49 44 20 62 65 63 6f 6d 65  , then ID become
29b0f 73 20 61 20 66 61 6c 6c 62 61 63 6b 20 74 6f 6b  s a fallback tok
29b10 65 6e 20 66 6f 72 20 58 2c 20 59 2c 0a 2a 2a 20  en for X, Y,.** 
29b11 61 6e 64 20 5a 2e 20 20 57 68 65 6e 65 76 65 72  and Z.  Whenever
29b12 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 6f 6b 65   one of the toke
29b13 6e 73 20 58 2c 20 59 2c 20 6f 72 20 5a 20 69 73  ns X, Y, or Z is
29b14 20 69 6e 70 75 74 20 74 6f 20 74 68 65 20 70 61   input to the pa
29b15 72 73 65 72 0a 2a 2a 20 62 75 74 20 69 74 20 64  rser.** but it d
29b16 6f 65 73 20 6e 6f 74 20 70 61 72 73 65 2c 20 74  oes not parse, t
29b17 68 65 20 74 79 70 65 20 6f 66 20 74 68 65 20 74  he type of the t
29b18 6f 6b 65 6e 20 69 73 20 63 68 61 6e 67 65 64 20  oken is changed 
29b19 74 6f 20 49 44 20 61 6e 64 0a 2a 2a 20 74 68 65  to ID and.** the
29b1a 20 70 61 72 73 65 20 69 73 20 72 65 74 72 69 65   parse is retrie
29b1b 64 20 62 65 66 6f 72 65 20 61 6e 20 65 72 72 6f  d before an erro
29b1c 72 20 69 73 20 74 68 72 6f 77 6e 2e 0a 2a 2f 0a  r is thrown..*/.
29b1d 23 69 66 64 65 66 20 59 59 46 41 4c 4c 42 41 43  #ifdef YYFALLBAC
29b1e 4b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 59  K.static const Y
29b1f 59 43 4f 44 45 54 59 50 45 20 79 79 46 61 6c 6c  YCODETYPE yyFall
29b20 62 61 63 6b 5b 5d 20 3d 20 7b 0a 20 20 20 20 30  back[] = {.    0
29b21 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 20 24  ,  /*          $
29b22 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
29b23 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
29b24 53 45 4d 49 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  SEMI => nothing 
29b25 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20  */.   23,  /*   
29b26 20 45 58 50 4c 41 49 4e 20 3d 3e 20 49 44 20 2a   EXPLAIN => ID *
29b27 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20  /.   23,  /*    
29b28 20 20 51 55 45 52 59 20 3d 3e 20 49 44 20 2a 2f    QUERY => ID */
29b29 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20  .   23,  /*     
29b2a 20 20 50 4c 41 4e 20 3d 3e 20 49 44 20 2a 2f 0a    PLAN => ID */.
29b2b 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20     23,  /*      
29b2c 42 45 47 49 4e 20 3d 3e 20 49 44 20 2a 2f 0a 20  BEGIN => ID */. 
29b2d 20 20 20 30 2c 20 20 2f 2a 20 54 52 41 4e 53 41     0,  /* TRANSA
29b2e 43 54 49 4f 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67  CTION => nothing
29b2f 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20   */.   23,  /*  
29b30 20 44 45 46 45 52 52 45 44 20 3d 3e 20 49 44 20   DEFERRED => ID 
29b31 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 49  */.   23,  /*  I
29b32 4d 4d 45 44 49 41 54 45 20 3d 3e 20 49 44 20 2a  MMEDIATE => ID *
29b33 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 45 58  /.   23,  /*  EX
29b34 43 4c 55 53 49 56 45 20 3d 3e 20 49 44 20 2a 2f  CLUSIVE => ID */
29b35 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20  .    0,  /*     
29b36 43 4f 4d 4d 49 54 20 3d 3e 20 6e 6f 74 68 69 6e  COMMIT => nothin
29b37 67 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20  g */.   23,  /* 
29b38 20 20 20 20 20 20 20 45 4e 44 20 3d 3e 20 49 44         END => ID
29b39 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
29b3a 20 52 4f 4c 4c 42 41 43 4b 20 3d 3e 20 6e 6f 74   ROLLBACK => not
29b3b 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
29b3c 2f 2a 20 20 20 20 20 43 52 45 41 54 45 20 3d 3e  /*     CREATE =>
29b3d 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
29b3e 30 2c 20 20 2f 2a 20 20 20 20 20 20 54 41 42 4c  0,  /*      TABL
29b3f 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  E => nothing */.
29b40 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20     23,  /*      
29b41 20 20 20 49 46 20 3d 3e 20 49 44 20 2a 2f 0a 20     IF => ID */. 
29b42 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
29b43 20 4e 4f 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20   NOT => nothing 
29b44 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
29b45 20 20 45 58 49 53 54 53 20 3d 3e 20 6e 6f 74 68    EXISTS => noth
29b46 69 6e 67 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f  ing */.   23,  /
29b47 2a 20 20 20 20 20 20 20 54 45 4d 50 20 3d 3e 20  *       TEMP => 
29b48 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ID */.    0,  /*
29b49 20 20 20 20 20 20 20 20 20 4c 50 20 3d 3e 20 6e           LP => n
29b4a 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
29b4b 20 20 2f 2a 20 20 20 20 20 20 20 20 20 52 50 20    /*         RP 
29b4c 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
29b4d 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20    0,  /*        
29b4e 20 41 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a   AS => nothing *
29b4f 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
29b50 20 20 43 4f 4d 4d 41 20 3d 3e 20 6e 6f 74 68 69    COMMA => nothi
29b51 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
29b52 20 20 20 20 20 20 20 20 20 49 44 20 3d 3e 20 6e           ID => n
29b53 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32 33 2c  othing */.   23,
29b54 20 20 2f 2a 20 20 20 20 20 20 41 42 4f 52 54 20    /*      ABORT 
29b55 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20  => ID */.   23, 
29b56 20 2f 2a 20 20 20 20 20 20 41 46 54 45 52 20 3d   /*      AFTER =
29b57 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20  > ID */.   23,  
29b58 2f 2a 20 20 20 20 41 4e 41 4c 59 5a 45 20 3d 3e  /*    ANALYZE =>
29b59 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f   ID */.   23,  /
29b5a 2a 20 20 20 20 20 20 20 20 41 53 43 20 3d 3e 20  *        ASC => 
29b5b 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a  ID */.   23,  /*
29b5c 20 20 20 20 20 41 54 54 41 43 48 20 3d 3e 20 49       ATTACH => I
29b5d 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20  D */.   23,  /* 
29b5e 20 20 20 20 42 45 46 4f 52 45 20 3d 3e 20 49 44      BEFORE => ID
29b5f 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20   */.   23,  /*  
29b60 20 20 43 41 53 43 41 44 45 20 3d 3e 20 49 44 20    CASCADE => ID 
29b61 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20  */.   23,  /*   
29b62 20 20 20 20 43 41 53 54 20 3d 3e 20 49 44 20 2a      CAST => ID *
29b63 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 43  /.   23,  /*   C
29b64 4f 4e 46 4c 49 43 54 20 3d 3e 20 49 44 20 2a 2f  ONFLICT => ID */
29b65 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 44 41  .   23,  /*   DA
29b66 54 41 42 41 53 45 20 3d 3e 20 49 44 20 2a 2f 0a  TABASE => ID */.
29b67 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20     23,  /*      
29b68 20 44 45 53 43 20 3d 3e 20 49 44 20 2a 2f 0a 20   DESC => ID */. 
29b69 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 44 45    23,  /*     DE
29b6a 54 41 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  TACH => ID */.  
29b6b 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20 20 45   23,  /*       E
29b6c 41 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  ACH => ID */.   
29b6d 32 33 2c 20 20 2f 2a 20 20 20 20 20 20 20 46 41  23,  /*       FA
29b6e 49 4c 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  IL => ID */.   2
29b6f 33 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 46 4f  3,  /*        FO
29b70 52 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33  R => ID */.   23
29b71 2c 20 20 2f 2a 20 20 20 20 20 49 47 4e 4f 52 45  ,  /*     IGNORE
29b72 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c   => ID */.   23,
29b73 20 20 2f 2a 20 20 49 4e 49 54 49 41 4c 4c 59 20    /*  INITIALLY 
29b74 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20  => ID */.   23, 
29b75 20 2f 2a 20 20 20 20 49 4e 53 54 45 41 44 20 3d   /*    INSTEAD =
29b76 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20  > ID */.   23,  
29b77 2f 2a 20 20 20 20 4c 49 4b 45 5f 4b 57 20 3d 3e  /*    LIKE_KW =>
29b78 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f   ID */.   23,  /
29b79 2a 20 20 20 20 20 20 4d 41 54 43 48 20 3d 3e 20  *      MATCH => 
29b7a 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a  ID */.   23,  /*
29b7b 20 20 20 20 20 20 20 20 4b 45 59 20 3d 3e 20 49          KEY => I
29b7c 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20  D */.   23,  /* 
29b7d 20 20 20 20 20 20 20 20 4f 46 20 3d 3e 20 49 44          OF => ID
29b7e 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20   */.   23,  /*  
29b7f 20 20 20 4f 46 46 53 45 54 20 3d 3e 20 49 44 20     OFFSET => ID 
29b80 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20  */.   23,  /*   
29b81 20 20 50 52 41 47 4d 41 20 3d 3e 20 49 44 20 2a    PRAGMA => ID *
29b82 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20  /.   23,  /*    
29b83 20 20 52 41 49 53 45 20 3d 3e 20 49 44 20 2a 2f    RAISE => ID */
29b84 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 52  .   23,  /*    R
29b85 45 50 4c 41 43 45 20 3d 3e 20 49 44 20 2a 2f 0a  EPLACE => ID */.
29b86 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 52 45 53     23,  /*   RES
29b87 54 52 49 43 54 20 3d 3e 20 49 44 20 2a 2f 0a 20  TRICT => ID */. 
29b88 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20 20    23,  /*       
29b89 20 52 4f 57 20 3d 3e 20 49 44 20 2a 2f 0a 20 20   ROW => ID */.  
29b8a 20 32 33 2c 20 20 2f 2a 20 20 20 20 54 52 49 47   23,  /*    TRIG
29b8b 47 45 52 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  GER => ID */.   
29b8c 32 33 2c 20 20 2f 2a 20 20 20 20 20 56 41 43 55  23,  /*     VACU
29b8d 55 4d 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  UM => ID */.   2
29b8e 33 2c 20 20 2f 2a 20 20 20 20 20 20 20 56 49 45  3,  /*       VIE
29b8f 57 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33  W => ID */.   23
29b90 2c 20 20 2f 2a 20 20 20 20 56 49 52 54 55 41 4c  ,  /*    VIRTUAL
29b91 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c   => ID */.   23,
29b92 20 20 2f 2a 20 20 20 20 52 45 49 4e 44 45 58 20    /*    REINDEX 
29b93 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20  => ID */.   23, 
29b94 20 2f 2a 20 20 20 20 20 52 45 4e 41 4d 45 20 3d   /*     RENAME =
29b95 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20  > ID */.   23,  
29b96 2f 2a 20 20 20 43 54 49 4d 45 5f 4b 57 20 3d 3e  /*   CTIME_KW =>
29b97 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f   ID */.    0,  /
29b98 2a 20 20 20 20 20 20 20 20 41 4e 59 20 3d 3e 20  *        ANY => 
29b99 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
29b9a 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 4f 52  ,  /*         OR
29b9b 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
29b9c 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
29b9d 20 41 4e 44 20 3d 3e 20 6e 6f 74 68 69 6e 67 20   AND => nothing 
29b9e 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
29b9f 20 20 20 20 20 20 49 53 20 3d 3e 20 6e 6f 74 68        IS => noth
29ba0 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
29ba1 2a 20 20 20 20 42 45 54 57 45 45 4e 20 3d 3e 20  *    BETWEEN => 
29ba2 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
29ba3 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 49 4e  ,  /*         IN
29ba4 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
29ba5 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 49 53     0,  /*     IS
29ba6 4e 55 4c 4c 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  NULL => nothing 
29ba7 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
29ba8 20 4e 4f 54 4e 55 4c 4c 20 3d 3e 20 6e 6f 74 68   NOTNULL => noth
29ba9 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
29baa 2a 20 20 20 20 20 20 20 20 20 4e 45 20 3d 3e 20  *         NE => 
29bab 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
29bac 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 45 51  ,  /*         EQ
29bad 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
29bae 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
29baf 20 20 47 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    GT => nothing 
29bb0 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
29bb1 20 20 20 20 20 20 4c 45 20 3d 3e 20 6e 6f 74 68        LE => noth
29bb2 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
29bb3 2a 20 20 20 20 20 20 20 20 20 4c 54 20 3d 3e 20  *         LT => 
29bb4 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
29bb5 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 47 45  ,  /*         GE
29bb6 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
29bb7 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 45 53     0,  /*     ES
29bb8 43 41 50 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  CAPE => nothing 
29bb9 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
29bba 20 20 42 49 54 41 4e 44 20 3d 3e 20 6e 6f 74 68    BITAND => noth
29bbb 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
29bbc 2a 20 20 20 20 20 20 42 49 54 4f 52 20 3d 3e 20  *      BITOR => 
29bbd 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
29bbe 2c 20 20 2f 2a 20 20 20 20 20 4c 53 48 49 46 54  ,  /*     LSHIFT
29bbf 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
29bc0 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 52 53     0,  /*     RS
29bc1 48 49 46 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  HIFT => nothing 
29bc2 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
29bc3 20 20 20 20 50 4c 55 53 20 3d 3e 20 6e 6f 74 68      PLUS => noth
29bc4 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
29bc5 2a 20 20 20 20 20 20 4d 49 4e 55 53 20 3d 3e 20  *      MINUS => 
29bc6 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
29bc7 2c 20 20 2f 2a 20 20 20 20 20 20 20 53 54 41 52  ,  /*       STAR
29bc8 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
29bc9 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 53     0,  /*      S
29bca 4c 41 53 48 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  LASH => nothing 
29bcb 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
29bcc 20 20 20 20 20 52 45 4d 20 3d 3e 20 6e 6f 74 68       REM => noth
29bcd 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
29bce 2a 20 20 20 20 20 43 4f 4e 43 41 54 20 3d 3e 20  *     CONCAT => 
29bcf 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
29bd0 2c 20 20 2f 2a 20 20 20 20 43 4f 4c 4c 41 54 45  ,  /*    COLLATE
29bd1 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
29bd2 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 55 4d     0,  /*     UM
29bd3 49 4e 55 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  INUS => nothing 
29bd4 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
29bd5 20 20 20 55 50 4c 55 53 20 3d 3e 20 6e 6f 74 68     UPLUS => noth
29bd6 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
29bd7 2a 20 20 20 20 20 42 49 54 4e 4f 54 20 3d 3e 20  *     BITNOT => 
29bd8 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
29bd9 2c 20 20 2f 2a 20 20 20 20 20 53 54 52 49 4e 47  ,  /*     STRING
29bda 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
29bdb 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 4a 4f 49     0,  /*    JOI
29bdc 4e 5f 4b 57 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  N_KW => nothing 
29bdd 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 43 4f  */.    0,  /* CO
29bde 4e 53 54 52 41 49 4e 54 20 3d 3e 20 6e 6f 74 68  NSTRAINT => noth
29bdf 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
29be0 2a 20 20 20 20 44 45 46 41 55 4c 54 20 3d 3e 20  *    DEFAULT => 
29be1 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
29be2 2c 20 20 2f 2a 20 20 20 20 20 20 20 4e 55 4c 4c  ,  /*       NULL
29be3 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
29be4 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 50 52 49     0,  /*    PRI
29be5 4d 41 52 59 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  MARY => nothing 
29be6 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
29be7 20 20 55 4e 49 51 55 45 20 3d 3e 20 6e 6f 74 68    UNIQUE => noth
29be8 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
29be9 2a 20 20 20 20 20 20 43 48 45 43 4b 20 3d 3e 20  *      CHECK => 
29bea 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
29beb 2c 20 20 2f 2a 20 52 45 46 45 52 45 4e 43 45 53  ,  /* REFERENCES
29bec 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
29bed 20 20 20 30 2c 20 20 2f 2a 20 20 20 41 55 54 4f     0,  /*   AUTO
29bee 49 4e 43 52 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  INCR => nothing 
29bef 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
29bf0 20 20 20 20 20 20 4f 4e 20 3d 3e 20 6e 6f 74 68        ON => noth
29bf1 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
29bf2 2a 20 20 20 20 20 44 45 4c 45 54 45 20 3d 3e 20  *     DELETE => 
29bf3 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
29bf4 2c 20 20 2f 2a 20 20 20 20 20 55 50 44 41 54 45  ,  /*     UPDATE
29bf5 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
29bf6 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 49 4e     0,  /*     IN
29bf7 53 45 52 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  SERT => nothing 
29bf8 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
29bf9 20 20 20 20 20 53 45 54 20 3d 3e 20 6e 6f 74 68       SET => noth
29bfa 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
29bfb 2a 20 44 45 46 45 52 52 41 42 4c 45 20 3d 3e 20  * DEFERRABLE => 
29bfc 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
29bfd 2c 20 20 2f 2a 20 20 20 20 46 4f 52 45 49 47 4e  ,  /*    FOREIGN
29bfe 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
29bff 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
29c00 44 52 4f 50 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  DROP => nothing 
29c01 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
29c02 20 20 20 55 4e 49 4f 4e 20 3d 3e 20 6e 6f 74 68     UNION => noth
29c03 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
29c04 2a 20 20 20 20 20 20 20 20 41 4c 4c 20 3d 3e 20  *        ALL => 
29c05 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
29c06 2c 20 20 2f 2a 20 20 20 20 20 45 58 43 45 50 54  ,  /*     EXCEPT
29c07 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
29c08 20 20 20 30 2c 20 20 2f 2a 20 20 49 4e 54 45 52     0,  /*  INTER
29c09 53 45 43 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  SECT => nothing 
29c0a 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
29c0b 20 20 53 45 4c 45 43 54 20 3d 3e 20 6e 6f 74 68    SELECT => noth
29c0c 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
29c0d 2a 20 20 20 44 49 53 54 49 4e 43 54 20 3d 3e 20  *   DISTINCT => 
29c0e 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
29c0f 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 44 4f 54  ,  /*        DOT
29c10 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
29c11 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
29c12 46 52 4f 4d 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  FROM => nothing 
29c13 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
29c14 20 20 20 20 4a 4f 49 4e 20 3d 3e 20 6e 6f 74 68      JOIN => noth
29c15 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
29c16 2a 20 20 20 20 49 4e 44 45 58 45 44 20 3d 3e 20  *    INDEXED => 
29c17 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
29c18 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 42 59  ,  /*         BY
29c19 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
29c1a 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 55     0,  /*      U
29c1b 53 49 4e 47 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  SING => nothing 
29c1c 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
29c1d 20 20 20 4f 52 44 45 52 20 3d 3e 20 6e 6f 74 68     ORDER => noth
29c1e 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
29c1f 2a 20 20 20 20 20 20 47 52 4f 55 50 20 3d 3e 20  *      GROUP => 
29c20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
29c21 2c 20 20 2f 2a 20 20 20 20 20 48 41 56 49 4e 47  ,  /*     HAVING
29c22 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
29c23 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 4c     0,  /*      L
29c24 49 4d 49 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  IMIT => nothing 
29c25 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
29c26 20 20 20 57 48 45 52 45 20 3d 3e 20 6e 6f 74 68     WHERE => noth
29c27 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
29c28 2a 20 20 20 20 20 20 20 49 4e 54 4f 20 3d 3e 20  *       INTO => 
29c29 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
29c2a 2c 20 20 2f 2a 20 20 20 20 20 56 41 4c 55 45 53  ,  /*     VALUES
29c2b 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
29c2c 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 49 4e 54     0,  /*    INT
29c2d 45 47 45 52 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  EGER => nothing 
29c2e 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
29c2f 20 20 20 46 4c 4f 41 54 20 3d 3e 20 6e 6f 74 68     FLOAT => noth
29c30 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
29c31 2a 20 20 20 20 20 20 20 42 4c 4f 42 20 3d 3e 20  *       BLOB => 
29c32 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
29c33 2c 20 20 2f 2a 20 20 20 52 45 47 49 53 54 45 52  ,  /*   REGISTER
29c34 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
29c35 20 20 20 30 2c 20 20 2f 2a 20 20 20 56 41 52 49     0,  /*   VARI
29c36 41 42 4c 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  ABLE => nothing 
29c37 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
29c38 20 20 20 20 43 41 53 45 20 3d 3e 20 6e 6f 74 68      CASE => noth
29c39 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
29c3a 2a 20 20 20 20 20 20 20 57 48 45 4e 20 3d 3e 20  *       WHEN => 
29c3b 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
29c3c 2c 20 20 2f 2a 20 20 20 20 20 20 20 54 48 45 4e  ,  /*       THEN
29c3d 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
29c3e 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
29c3f 45 4c 53 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  ELSE => nothing 
29c40 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
29c41 20 20 20 49 4e 44 45 58 20 3d 3e 20 6e 6f 74 68     INDEX => noth
29c42 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
29c43 2a 20 20 20 20 20 20 41 4c 54 45 52 20 3d 3e 20  *      ALTER => 
29c44 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
29c45 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 54 4f  ,  /*         TO
29c46 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
29c47 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
29c48 20 41 44 44 20 3d 3e 20 6e 6f 74 68 69 6e 67 20   ADD => nothing 
29c49 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
29c4a 43 4f 4c 55 4d 4e 4b 57 20 3d 3e 20 6e 6f 74 68  COLUMNKW => noth
29c4b 69 6e 67 20 2a 2f 0a 7d 3b 0a 23 65 6e 64 69 66  ing */.};.#endif
29c4c 20 2f 2a 20 59 59 46 41 4c 4c 42 41 43 4b 20 2a   /* YYFALLBACK *
29c4d 2f 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /../* The follow
29c4e 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 72 65  ing structure re
29c4f 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c  presents a singl
29c50 65 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65  e element of the
29c51 0a 2a 2a 20 70 61 72 73 65 72 27 73 20 73 74 61  .** parser's sta
29c52 63 6b 2e 20 20 49 6e 66 6f 72 6d 61 74 69 6f 6e  ck.  Information
29c53 20 73 74 6f 72 65 64 20 69 6e 63 6c 75 64 65 73   stored includes
29c54 3a 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 20 54 68 65  :.**.**   +  The
29c55 20 73 74 61 74 65 20 6e 75 6d 62 65 72 20 66 6f   state number fo
29c56 72 20 74 68 65 20 70 61 72 73 65 72 20 61 74 20  r the parser at 
29c57 74 68 69 73 20 6c 65 76 65 6c 20 6f 66 20 74 68  this level of th
29c58 65 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20  e stack..**.**  
29c59 20 2b 20 20 54 68 65 20 76 61 6c 75 65 20 6f 66   +  The value of
29c5a 20 74 68 65 20 74 6f 6b 65 6e 20 73 74 6f 72 65   the token store
29c5b 64 20 61 74 20 74 68 69 73 20 6c 65 76 65 6c 20  d at this level 
29c5c 6f 66 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a  of the stack..**
29c5d 20 20 20 20 20 20 28 49 6e 20 6f 74 68 65 72 20        (In other 
29c5e 77 6f 72 64 73 2c 20 74 68 65 20 22 6d 61 6a 6f  words, the "majo
29c5f 72 22 20 74 6f 6b 65 6e 2e 29 0a 2a 2a 0a 2a 2a  r" token.).**.**
29c60 20 20 20 2b 20 20 54 68 65 20 73 65 6d 61 6e 74     +  The semant
29c61 69 63 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20  ic value stored 
29c62 61 74 20 74 68 69 73 20 6c 65 76 65 6c 20 6f 66  at this level of
29c63 20 74 68 65 20 73 74 61 63 6b 2e 20 20 54 68 69   the stack.  Thi
29c64 73 20 69 73 0a 2a 2a 20 20 20 20 20 20 74 68 65  s is.**      the
29c65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65   information use
29c66 64 20 62 79 20 74 68 65 20 61 63 74 69 6f 6e 20  d by the action 
29c67 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 65 20  routines in the 
29c68 67 72 61 6d 6d 61 72 2e 0a 2a 2a 20 20 20 20 20  grammar..**     
29c69 20 49 74 20 69 73 20 73 6f 6d 65 74 69 6d 65 73   It is sometimes
29c6a 20 63 61 6c 6c 65 64 20 74 68 65 20 22 6d 69 6e   called the "min
29c6b 6f 72 22 20 74 6f 6b 65 6e 2e 0a 2a 2f 0a 73 74  or" token..*/.st
29c6c 72 75 63 74 20 79 79 53 74 61 63 6b 45 6e 74 72  ruct yyStackEntr
29c6d 79 20 7b 0a 20 20 59 59 41 43 54 49 4f 4e 54 59  y {.  YYACTIONTY
29c6e 50 45 20 73 74 61 74 65 6e 6f 3b 20 20 2f 2a 20  PE stateno;  /* 
29c6f 54 68 65 20 73 74 61 74 65 2d 6e 75 6d 62 65 72  The state-number
29c70 20 2a 2f 0a 20 20 59 59 43 4f 44 45 54 59 50 45   */.  YYCODETYPE
29c71 20 6d 61 6a 6f 72 3b 20 20 20 20 20 20 2f 2a 20   major;      /* 
29c72 54 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20  The major token 
29c73 76 61 6c 75 65 2e 20 20 54 68 69 73 20 69 73 20  value.  This is 
29c74 74 68 65 20 63 6f 64 65 0a 20 20 20 20 20 20 20  the code.       
29c75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29c76 20 20 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20    ** number for 
29c77 74 68 65 20 74 6f 6b 65 6e 20 61 74 20 74 68 69  the token at thi
29c78 73 20 73 74 61 63 6b 20 6c 65 76 65 6c 20 2a 2f  s stack level */
29c79 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 6d  .  YYMINORTYPE m
29c7a 69 6e 6f 72 3b 20 20 20 20 20 2f 2a 20 54 68 65  inor;     /* The
29c7b 20 75 73 65 72 2d 73 75 70 70 6c 69 65 64 20 6d   user-supplied m
29c7c 69 6e 6f 72 20 74 6f 6b 65 6e 20 76 61 6c 75 65  inor token value
29c7d 2e 20 20 54 68 69 73 0a 20 20 20 20 20 20 20 20  .  This.        
29c7e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29c7f 20 2a 2a 20 69 73 20 74 68 65 20 76 61 6c 75 65   ** is the value
29c80 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 20 2a   of the token  *
29c81 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72  /.};.typedef str
29c82 75 63 74 20 79 79 53 74 61 63 6b 45 6e 74 72 79  uct yyStackEntry
29c83 20 79 79 53 74 61 63 6b 45 6e 74 72 79 3b 0a 0a   yyStackEntry;..
29c84 2f 2a 20 54 68 65 20 73 74 61 74 65 20 6f 66 20  /* The state of 
29c85 74 68 65 20 70 61 72 73 65 72 20 69 73 20 63 6f  the parser is co
29c86 6d 70 6c 65 74 65 6c 79 20 63 6f 6e 74 61 69 6e  mpletely contain
29c87 65 64 20 69 6e 20 61 6e 20 69 6e 73 74 61 6e 63  ed in an instanc
29c88 65 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c  e of.** the foll
29c89 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
29c8a 2a 2f 0a 73 74 72 75 63 74 20 79 79 50 61 72 73  */.struct yyPars
29c8b 65 72 20 7b 0a 20 20 69 6e 74 20 79 79 69 64 78  er {.  int yyidx
29c8c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
29c8d 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
29c8e 20 74 6f 70 20 65 6c 65 6d 65 6e 74 20 69 6e 20   top element in 
29c8f 73 74 61 63 6b 20 2a 2f 0a 23 69 66 64 65 66 20  stack */.#ifdef 
29c90 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44  YYTRACKMAXSTACKD
29c91 45 50 54 48 0a 20 20 69 6e 74 20 79 79 69 64 78  EPTH.  int yyidx
29c92 4d 61 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  Max;            
29c93 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20       /* Maximum 
29c94 76 61 6c 75 65 20 6f 66 20 79 79 69 64 78 20 2a  value of yyidx *
29c95 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 79  /.#endif.  int y
29c96 79 65 72 72 63 6e 74 3b 20 20 20 20 20 20 20 20  yerrcnt;        
29c97 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 69 66           /* Shif
29c98 74 73 20 6c 65 66 74 20 62 65 66 6f 72 65 20 6f  ts left before o
29c99 75 74 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20  ut of the error 
29c9a 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73  */.  sqlite3Pars
29c9b 65 72 41 52 47 5f 53 44 45 43 4c 20 20 20 20 20  erARG_SDECL     
29c9c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20             /* A 
29c9d 70 6c 61 63 65 20 74 6f 20 68 6f 6c 64 20 25 65  place to hold %e
29c9e 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 2a 2f  xtra_argument */
29c9f 0a 23 69 66 20 59 59 53 54 41 43 4b 44 45 50 54  .#if YYSTACKDEPT
29ca0 48 3c 3d 30 0a 20 20 69 6e 74 20 79 79 73 74 6b  H<=0.  int yystk
29ca1 73 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  sz;             
29ca2 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
29ca3 73 69 64 65 20 6f 66 20 74 68 65 20 73 74 61 63  side of the stac
29ca4 6b 20 2a 2f 0a 20 20 79 79 53 74 61 63 6b 45 6e  k */.  yyStackEn
29ca5 74 72 79 20 2a 79 79 73 74 61 63 6b 3b 20 20 20  try *yystack;   
29ca6 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
29ca7 65 72 27 73 20 73 74 61 63 6b 20 2a 2f 0a 23 65  er's stack */.#e
29ca8 6c 73 65 0a 20 20 79 79 53 74 61 63 6b 45 6e 74  lse.  yyStackEnt
29ca9 72 79 20 79 79 73 74 61 63 6b 5b 59 59 53 54 41  ry yystack[YYSTA
29caa 43 4b 44 45 50 54 48 5d 3b 20 20 2f 2a 20 54 68  CKDEPTH];  /* Th
29cab 65 20 70 61 72 73 65 72 27 73 20 73 74 61 63 6b  e parser's stack
29cac 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 74 79   */.#endif.};.ty
29cad 70 65 64 65 66 20 73 74 72 75 63 74 20 79 79 50  pedef struct yyP
29cae 61 72 73 65 72 20 79 79 50 61 72 73 65 72 3b 0a  arser yyParser;.
29caf 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
29cb0 73 74 61 74 69 63 20 46 49 4c 45 20 2a 79 79 54  static FILE *yyT
29cb1 72 61 63 65 46 49 4c 45 20 3d 20 30 3b 0a 73 74  raceFILE = 0;.st
29cb2 61 74 69 63 20 63 68 61 72 20 2a 79 79 54 72 61  atic char *yyTra
29cb3 63 65 50 72 6f 6d 70 74 20 3d 20 30 3b 0a 23 65  cePrompt = 0;.#e
29cb4 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a  ndif /* NDEBUG *
29cb5 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  /..#ifndef NDEBU
29cb6 47 0a 2f 2a 20 0a 2a 2a 20 54 75 72 6e 20 70 61  G./* .** Turn pa
29cb7 72 73 65 72 20 74 72 61 63 69 6e 67 20 6f 6e 20  rser tracing on 
29cb8 62 79 20 67 69 76 69 6e 67 20 61 20 73 74 72 65  by giving a stre
29cb9 61 6d 20 74 6f 20 77 68 69 63 68 20 74 6f 20 77  am to which to w
29cba 72 69 74 65 20 74 68 65 20 74 72 61 63 65 0a 2a  rite the trace.*
29cbb 2a 20 61 6e 64 20 61 20 70 72 6f 6d 70 74 20 74  * and a prompt t
29cbc 6f 20 70 72 65 66 61 63 65 20 65 61 63 68 20 74  o preface each t
29cbd 72 61 63 65 20 6d 65 73 73 61 67 65 2e 20 20 54  race message.  T
29cbe 72 61 63 69 6e 67 20 69 73 20 74 75 72 6e 65 64  racing is turned
29cbf 20 6f 66 66 0a 2a 2a 20 62 79 20 6d 61 6b 69 6e   off.** by makin
29cc0 67 20 65 69 74 68 65 72 20 61 72 67 75 6d 65 6e  g either argumen
29cc1 74 20 4e 55 4c 4c 20 0a 2a 2a 0a 2a 2a 20 49 6e  t NULL .**.** In
29cc2 70 75 74 73 3a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a  puts:.** <ul>.**
29cc3 20 3c 6c 69 3e 20 41 20 46 49 4c 45 2a 20 74 6f   <li> A FILE* to
29cc4 20 77 68 69 63 68 20 74 72 61 63 65 20 6f 75 74   which trace out
29cc5 70 75 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72  put should be wr
29cc6 69 74 74 65 6e 2e 0a 2a 2a 20 20 20 20 20 20 49  itten..**      I
29cc7 66 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 72 61  f NULL, then tra
29cc8 63 69 6e 67 20 69 73 20 74 75 72 6e 65 64 20 6f  cing is turned o
29cc9 66 66 2e 0a 2a 2a 20 3c 6c 69 3e 20 41 20 70 72  ff..** <li> A pr
29cca 65 66 69 78 20 73 74 72 69 6e 67 20 77 72 69 74  efix string writ
29ccb 74 65 6e 20 61 74 20 74 68 65 20 62 65 67 69 6e  ten at the begin
29ccc 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 0a 2a 2a  ning of every.**
29ccd 20 20 20 20 20 20 6c 69 6e 65 20 6f 66 20 74 72        line of tr
29cce 61 63 65 20 6f 75 74 70 75 74 2e 20 20 49 66 20  ace output.  If 
29ccf 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 72 61 63 69  NULL, then traci
29cd0 6e 67 20 69 73 0a 2a 2a 20 20 20 20 20 20 74 75  ng is.**      tu
29cd1 72 6e 65 64 20 6f 66 66 2e 0a 2a 2a 20 3c 2f 75  rned off..** </u
29cd2 6c 3e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75 74 73  l>.**.** Outputs
29cd3 3a 0a 2a 2a 20 4e 6f 6e 65 2e 0a 2a 2f 0a 53 51  :.** None..*/.SQ
29cd4 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
29cd5 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54  d sqlite3ParserT
29cd6 72 61 63 65 28 46 49 4c 45 20 2a 54 72 61 63 65  race(FILE *Trace
29cd7 46 49 4c 45 2c 20 63 68 61 72 20 2a 7a 54 72 61  FILE, char *zTra
29cd8 63 65 50 72 6f 6d 70 74 29 7b 0a 20 20 79 79 54  cePrompt){.  yyT
29cd9 72 61 63 65 46 49 4c 45 20 3d 20 54 72 61 63 65  raceFILE = Trace
29cda 46 49 4c 45 3b 0a 20 20 79 79 54 72 61 63 65 50  FILE;.  yyTraceP
29cdb 72 6f 6d 70 74 20 3d 20 7a 54 72 61 63 65 50 72  rompt = zTracePr
29cdc 6f 6d 70 74 3b 0a 20 20 69 66 28 20 79 79 54 72  ompt;.  if( yyTr
29cdd 61 63 65 46 49 4c 45 3d 3d 30 20 29 20 79 79 54  aceFILE==0 ) yyT
29cde 72 61 63 65 50 72 6f 6d 70 74 20 3d 20 30 3b 0a  racePrompt = 0;.
29cdf 20 20 65 6c 73 65 20 69 66 28 20 79 79 54 72 61    else if( yyTra
29ce0 63 65 50 72 6f 6d 70 74 3d 3d 30 20 29 20 79 79  cePrompt==0 ) yy
29ce1 54 72 61 63 65 46 49 4c 45 20 3d 20 30 3b 0a 7d  TraceFILE = 0;.}
29ce2 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55  .#endif /* NDEBU
29ce3 47 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44  G */..#ifndef ND
29ce4 45 42 55 47 0a 2f 2a 20 46 6f 72 20 74 72 61 63  EBUG./* For trac
29ce5 69 6e 67 20 73 68 69 66 74 73 2c 20 74 68 65 20  ing shifts, the 
29ce6 6e 61 6d 65 73 20 6f 66 20 61 6c 6c 20 74 65 72  names of all ter
29ce7 6d 69 6e 61 6c 73 20 61 6e 64 20 6e 6f 6e 74 65  minals and nonte
29ce8 72 6d 69 6e 61 6c 73 0a 2a 2a 20 61 72 65 20 72  rminals.** are r
29ce9 65 71 75 69 72 65 64 2e 20 20 54 68 65 20 66 6f  equired.  The fo
29cea 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 73 75  llowing table su
29ceb 70 70 6c 69 65 73 20 74 68 65 73 65 20 6e 61 6d  pplies these nam
29cec 65 73 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  es */.static con
29ced 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20 79  st char *const y
29cee 79 54 6f 6b 65 6e 4e 61 6d 65 5b 5d 20 3d 20 7b  yTokenName[] = {
29cef 20 0a 20 20 22 24 22 2c 20 20 20 20 20 20 20 20   .  "$",        
29cf0 20 20 20 20 20 22 53 45 4d 49 22 2c 20 20 20 20       "SEMI",    
29cf1 20 20 20 20 20 20 22 45 58 50 4c 41 49 4e 22 2c        "EXPLAIN",
29cf2 20 20 20 20 20 20 20 22 51 55 45 52 59 22 2c 20         "QUERY", 
29cf3 20 20 20 20 20 20 0a 20 20 22 50 4c 41 4e 22 2c        .  "PLAN",
29cf4 20 20 20 20 20 20 20 20 20 20 22 42 45 47 49 4e            "BEGIN
29cf5 22 2c 20 20 20 20 20 20 20 20 20 22 54 52 41 4e  ",         "TRAN
29cf6 53 41 43 54 49 4f 4e 22 2c 20 20 20 22 44 45 46  SACTION",   "DEF
29cf7 45 52 52 45 44 22 2c 20 20 20 20 0a 20 20 22 49  ERRED",    .  "I
29cf8 4d 4d 45 44 49 41 54 45 22 2c 20 20 20 20 20 22  MMEDIATE",     "
29cf9 45 58 43 4c 55 53 49 56 45 22 2c 20 20 20 20 20  EXCLUSIVE",     
29cfa 22 43 4f 4d 4d 49 54 22 2c 20 20 20 20 20 20 20  "COMMIT",       
29cfb 20 22 45 4e 44 22 2c 20 20 20 20 20 20 20 20 20   "END",         
29cfc 0a 20 20 22 52 4f 4c 4c 42 41 43 4b 22 2c 20 20  .  "ROLLBACK",  
29cfd 20 20 20 20 22 43 52 45 41 54 45 22 2c 20 20 20      "CREATE",   
29cfe 20 20 20 20 20 22 54 41 42 4c 45 22 2c 20 20 20       "TABLE",   
29cff 20 20 20 20 20 20 22 49 46 22 2c 20 20 20 20 20        "IF",     
29d00 20 20 20 20 20 0a 20 20 22 4e 4f 54 22 2c 20 20       .  "NOT",  
29d01 20 20 20 20 20 20 20 20 20 22 45 58 49 53 54 53           "EXISTS
29d02 22 2c 20 20 20 20 20 20 20 20 22 54 45 4d 50 22  ",        "TEMP"
29d03 2c 20 20 20 20 20 20 20 20 20 20 22 4c 50 22 2c  ,          "LP",
29d04 20 20 20 20 20 20 20 20 20 20 0a 20 20 22 52 50            .  "RP
29d05 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 41  ",            "A
29d06 53 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  S",            "
29d07 43 4f 4d 4d 41 22 2c 20 20 20 20 20 20 20 20 20  COMMA",         
29d08 22 49 44 22 2c 20 20 20 20 20 20 20 20 20 20 0a  "ID",          .
29d09 20 20 22 41 42 4f 52 54 22 2c 20 20 20 20 20 20    "ABORT",      
29d0a 20 20 20 22 41 46 54 45 52 22 2c 20 20 20 20 20     "AFTER",     
29d0b 20 20 20 20 22 41 4e 41 4c 59 5a 45 22 2c 20 20      "ANALYZE",  
29d0c 20 20 20 20 20 22 41 53 43 22 2c 20 20 20 20 20       "ASC",     
29d0d 20 20 20 20 0a 20 20 22 41 54 54 41 43 48 22 2c      .  "ATTACH",
29d0e 20 20 20 20 20 20 20 20 22 42 45 46 4f 52 45 22          "BEFORE"
29d0f 2c 20 20 20 20 20 20 20 20 22 43 41 53 43 41 44  ,        "CASCAD
29d10 45 22 2c 20 20 20 20 20 20 20 22 43 41 53 54 22  E",       "CAST"
29d11 2c 20 20 20 20 20 20 20 20 0a 20 20 22 43 4f 4e  ,        .  "CON
29d12 46 4c 49 43 54 22 2c 20 20 20 20 20 20 22 44 41  FLICT",      "DA
29d13 54 41 42 41 53 45 22 2c 20 20 20 20 20 20 22 44  TABASE",      "D
29d14 45 53 43 22 2c 20 20 20 20 20 20 20 20 20 20 22  ESC",          "
29d15 44 45 54 41 43 48 22 2c 20 20 20 20 20 20 0a 20  DETACH",      . 
29d16 20 22 45 41 43 48 22 2c 20 20 20 20 20 20 20 20   "EACH",        
29d17 20 20 22 46 41 49 4c 22 2c 20 20 20 20 20 20 20    "FAIL",       
29d18 20 20 20 22 46 4f 52 22 2c 20 20 20 20 20 20 20     "FOR",       
29d19 20 20 20 20 22 49 47 4e 4f 52 45 22 2c 20 20 20      "IGNORE",   
29d1a 20 20 20 0a 20 20 22 49 4e 49 54 49 41 4c 4c 59     .  "INITIALLY
29d1b 22 2c 20 20 20 20 20 22 49 4e 53 54 45 41 44 22  ",     "INSTEAD"
29d1c 2c 20 20 20 20 20 20 20 22 4c 49 4b 45 5f 4b 57  ,       "LIKE_KW
29d1d 22 2c 20 20 20 20 20 20 20 22 4d 41 54 43 48 22  ",       "MATCH"
29d1e 2c 20 20 20 20 20 20 20 0a 20 20 22 4b 45 59 22  ,       .  "KEY"
29d1f 2c 20 20 20 20 20 20 20 20 20 20 20 22 4f 46 22  ,           "OF"
29d20 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 4f 46  ,            "OF
29d21 46 53 45 54 22 2c 20 20 20 20 20 20 20 20 22 50  FSET",        "P
29d22 52 41 47 4d 41 22 2c 20 20 20 20 20 20 0a 20 20  RAGMA",      .  
29d23 22 52 41 49 53 45 22 2c 20 20 20 20 20 20 20 20  "RAISE",        
29d24 20 22 52 45 50 4c 41 43 45 22 2c 20 20 20 20 20   "REPLACE",     
29d25 20 20 22 52 45 53 54 52 49 43 54 22 2c 20 20 20    "RESTRICT",   
29d26 20 20 20 22 52 4f 57 22 2c 20 20 20 20 20 20 20     "ROW",       
29d27 20 20 0a 20 20 22 54 52 49 47 47 45 52 22 2c 20    .  "TRIGGER", 
29d28 20 20 20 20 20 20 22 56 41 43 55 55 4d 22 2c 20        "VACUUM", 
29d29 20 20 20 20 20 20 20 22 56 49 45 57 22 2c 20 20         "VIEW",  
29d2a 20 20 20 20 20 20 20 20 22 56 49 52 54 55 41 4c          "VIRTUAL
29d2b 22 2c 20 20 20 20 20 0a 20 20 22 52 45 49 4e 44  ",     .  "REIND
29d2c 45 58 22 2c 20 20 20 20 20 20 20 22 52 45 4e 41  EX",       "RENA
29d2d 4d 45 22 2c 20 20 20 20 20 20 20 20 22 43 54 49  ME",        "CTI
29d2e 4d 45 5f 4b 57 22 2c 20 20 20 20 20 20 22 41 4e  ME_KW",      "AN
29d2f 59 22 2c 20 20 20 20 20 20 20 20 20 0a 20 20 22  Y",         .  "
29d30 4f 52 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  OR",            
29d31 22 41 4e 44 22 2c 20 20 20 20 20 20 20 20 20 20  "AND",          
29d32 20 22 49 53 22 2c 20 20 20 20 20 20 20 20 20 20   "IS",          
29d33 20 20 22 42 45 54 57 45 45 4e 22 2c 20 20 20 20    "BETWEEN",    
29d34 20 0a 20 20 22 49 4e 22 2c 20 20 20 20 20 20 20   .  "IN",       
29d35 20 20 20 20 20 22 49 53 4e 55 4c 4c 22 2c 20 20       "ISNULL",  
29d36 20 20 20 20 20 20 22 4e 4f 54 4e 55 4c 4c 22 2c        "NOTNULL",
29d37 20 20 20 20 20 20 20 22 4e 45 22 2c 20 20 20 20         "NE",    
29d38 20 20 20 20 20 20 0a 20 20 22 45 51 22 2c 20 20        .  "EQ",  
29d39 20 20 20 20 20 20 20 20 20 20 22 47 54 22 2c 20            "GT", 
29d3a 20 20 20 20 20 20 20 20 20 20 20 22 4c 45 22 2c             "LE",
29d3b 20 20 20 20 20 20 20 20 20 20 20 20 22 4c 54 22              "LT"
29d3c 2c 20 20 20 20 20 20 20 20 20 20 0a 20 20 22 47  ,          .  "G
29d3d 45 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  E",            "
29d3e 45 53 43 41 50 45 22 2c 20 20 20 20 20 20 20 20  ESCAPE",        
29d3f 22 42 49 54 41 4e 44 22 2c 20 20 20 20 20 20 20  "BITAND",       
29d40 20 22 42 49 54 4f 52 22 2c 20 20 20 20 20 20 20   "BITOR",       
29d41 0a 20 20 22 4c 53 48 49 46 54 22 2c 20 20 20 20  .  "LSHIFT",    
29d42 20 20 20 20 22 52 53 48 49 46 54 22 2c 20 20 20      "RSHIFT",   
29d43 20 20 20 20 20 22 50 4c 55 53 22 2c 20 20 20 20       "PLUS",    
29d44 20 20 20 20 20 20 22 4d 49 4e 55 53 22 2c 20 20        "MINUS",  
29d45 20 20 20 20 20 0a 20 20 22 53 54 41 52 22 2c 20       .  "STAR", 
29d46 20 20 20 20 20 20 20 20 20 22 53 4c 41 53 48 22           "SLASH"
29d47 2c 20 20 20 20 20 20 20 20 20 22 52 45 4d 22 2c  ,         "REM",
29d48 20 20 20 20 20 20 20 20 20 20 20 22 43 4f 4e 43             "CONC
29d49 41 54 22 2c 20 20 20 20 20 20 0a 20 20 22 43 4f  AT",      .  "CO
29d4a 4c 4c 41 54 45 22 2c 20 20 20 20 20 20 20 22 55  LLATE",       "U
29d4b 4d 49 4e 55 53 22 2c 20 20 20 20 20 20 20 20 22  MINUS",        "
29d4c 55 50 4c 55 53 22 2c 20 20 20 20 20 20 20 20 20  UPLUS",         
29d4d 22 42 49 54 4e 4f 54 22 2c 20 20 20 20 20 20 0a  "BITNOT",      .
29d4e 20 20 22 53 54 52 49 4e 47 22 2c 20 20 20 20 20    "STRING",     
29d4f 20 20 20 22 4a 4f 49 4e 5f 4b 57 22 2c 20 20 20     "JOIN_KW",   
29d50 20 20 20 20 22 43 4f 4e 53 54 52 41 49 4e 54 22      "CONSTRAINT"
29d51 2c 20 20 20 20 22 44 45 46 41 55 4c 54 22 2c 20  ,    "DEFAULT", 
29d52 20 20 20 20 0a 20 20 22 4e 55 4c 4c 22 2c 20 20      .  "NULL",  
29d53 20 20 20 20 20 20 20 20 22 50 52 49 4d 41 52 59          "PRIMARY
29d54 22 2c 20 20 20 20 20 20 20 22 55 4e 49 51 55 45  ",       "UNIQUE
29d55 22 2c 20 20 20 20 20 20 20 20 22 43 48 45 43 4b  ",        "CHECK
29d56 22 2c 20 20 20 20 20 20 20 0a 20 20 22 52 45 46  ",       .  "REF
29d57 45 52 45 4e 43 45 53 22 2c 20 20 20 20 22 41 55  ERENCES",    "AU
29d58 54 4f 49 4e 43 52 22 2c 20 20 20 20 20 20 22 4f  TOINCR",      "O
29d59 4e 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  N",            "
29d5a 44 45 4c 45 54 45 22 2c 20 20 20 20 20 20 0a 20  DELETE",      . 
29d5b 20 22 55 50 44 41 54 45 22 2c 20 20 20 20 20 20   "UPDATE",      
29d5c 20 20 22 49 4e 53 45 52 54 22 2c 20 20 20 20 20    "INSERT",     
29d5d 20 20 20 22 53 45 54 22 2c 20 20 20 20 20 20 20     "SET",       
29d5e 20 20 20 20 22 44 45 46 45 52 52 41 42 4c 45 22      "DEFERRABLE"
29d5f 2c 20 20 0a 20 20 22 46 4f 52 45 49 47 4e 22 2c  ,  .  "FOREIGN",
29d60 20 20 20 20 20 20 20 22 44 52 4f 50 22 2c 20 20         "DROP",  
29d61 20 20 20 20 20 20 20 20 22 55 4e 49 4f 4e 22 2c          "UNION",
29d62 20 20 20 20 20 20 20 20 20 22 41 4c 4c 22 2c 20           "ALL", 
29d63 20 20 20 20 20 20 20 20 0a 20 20 22 45 58 43 45          .  "EXCE
29d64 50 54 22 2c 20 20 20 20 20 20 20 20 22 49 4e 54  PT",        "INT
29d65 45 52 53 45 43 54 22 2c 20 20 20 20 20 22 53 45  ERSECT",     "SE
29d66 4c 45 43 54 22 2c 20 20 20 20 20 20 20 20 22 44  LECT",        "D
29d67 49 53 54 49 4e 43 54 22 2c 20 20 20 20 0a 20 20  ISTINCT",    .  
29d68 22 44 4f 54 22 2c 20 20 20 20 20 20 20 20 20 20  "DOT",          
29d69 20 22 46 52 4f 4d 22 2c 20 20 20 20 20 20 20 20   "FROM",        
29d6a 20 20 22 4a 4f 49 4e 22 2c 20 20 20 20 20 20 20    "JOIN",       
29d6b 20 20 20 22 49 4e 44 45 58 45 44 22 2c 20 20 20     "INDEXED",   
29d6c 20 20 0a 20 20 22 42 59 22 2c 20 20 20 20 20 20    .  "BY",      
29d6d 20 20 20 20 20 20 22 55 53 49 4e 47 22 2c 20 20        "USING",  
29d6e 20 20 20 20 20 20 20 22 4f 52 44 45 52 22 2c 20         "ORDER", 
29d6f 20 20 20 20 20 20 20 20 22 47 52 4f 55 50 22 2c          "GROUP",
29d70 20 20 20 20 20 20 20 0a 20 20 22 48 41 56 49 4e         .  "HAVIN
29d71 47 22 2c 20 20 20 20 20 20 20 20 22 4c 49 4d 49  G",        "LIMI
29d72 54 22 2c 20 20 20 20 20 20 20 20 20 22 57 48 45  T",         "WHE
29d73 52 45 22 2c 20 20 20 20 20 20 20 20 20 22 49 4e  RE",         "IN
29d74 54 4f 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22  TO",        .  "
29d75 56 41 4c 55 45 53 22 2c 20 20 20 20 20 20 20 20  VALUES",        
29d76 22 49 4e 54 45 47 45 52 22 2c 20 20 20 20 20 20  "INTEGER",      
29d77 20 22 46 4c 4f 41 54 22 2c 20 20 20 20 20 20 20   "FLOAT",       
29d78 20 20 22 42 4c 4f 42 22 2c 20 20 20 20 20 20 20    "BLOB",       
29d79 20 0a 20 20 22 52 45 47 49 53 54 45 52 22 2c 20   .  "REGISTER", 
29d7a 20 20 20 20 20 22 56 41 52 49 41 42 4c 45 22 2c       "VARIABLE",
29d7b 20 20 20 20 20 20 22 43 41 53 45 22 2c 20 20 20        "CASE",   
29d7c 20 20 20 20 20 20 20 22 57 48 45 4e 22 2c 20 20         "WHEN",  
29d7d 20 20 20 20 20 20 0a 20 20 22 54 48 45 4e 22 2c        .  "THEN",
29d7e 20 20 20 20 20 20 20 20 20 20 22 45 4c 53 45 22            "ELSE"
29d7f 2c 20 20 20 20 20 20 20 20 20 20 22 49 4e 44 45  ,          "INDE
29d80 58 22 2c 20 20 20 20 20 20 20 20 20 22 41 4c 54  X",         "ALT
29d81 45 52 22 2c 20 20 20 20 20 20 20 0a 20 20 22 54  ER",       .  "T
29d82 4f 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  O",            "
29d83 41 44 44 22 2c 20 20 20 20 20 20 20 20 20 20 20  ADD",           
29d84 22 43 4f 4c 55 4d 4e 4b 57 22 2c 20 20 20 20 20  "COLUMNKW",     
29d85 20 22 65 72 72 6f 72 22 2c 20 20 20 20 20 20 20   "error",       
29d86 0a 20 20 22 69 6e 70 75 74 22 2c 20 20 20 20 20  .  "input",     
29d87 20 20 20 20 22 63 6d 64 6c 69 73 74 22 2c 20 20      "cmdlist",  
29d88 20 20 20 20 20 22 65 63 6d 64 22 2c 20 20 20 20       "ecmd",    
29d89 20 20 20 20 20 20 22 65 78 70 6c 61 69 6e 22 2c        "explain",
29d8a 20 20 20 20 20 0a 20 20 22 63 6d 64 78 22 2c 20       .  "cmdx", 
29d8b 20 20 20 20 20 20 20 20 20 22 63 6d 64 22 2c 20           "cmd", 
29d8c 20 20 20 20 20 20 20 20 20 20 22 74 72 61 6e 73            "trans
29d8d 74 79 70 65 22 2c 20 20 20 20 20 22 74 72 61 6e  type",     "tran
29d8e 73 5f 6f 70 74 22 2c 20 20 20 0a 20 20 22 6e 6d  s_opt",   .  "nm
29d8f 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 63  ",            "c
29d90 72 65 61 74 65 5f 74 61 62 6c 65 22 2c 20 20 22  reate_table",  "
29d91 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61 72 67  create_table_arg
29d92 73 22 2c 20 20 22 74 65 6d 70 22 2c 20 20 20 20  s",  "temp",    
29d93 20 20 20 20 0a 20 20 22 69 66 6e 6f 74 65 78 69      .  "ifnotexi
29d94 73 74 73 22 2c 20 20 20 22 64 62 6e 6d 22 2c 20  sts",   "dbnm", 
29d95 20 20 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e           "column
29d96 6c 69 73 74 22 2c 20 20 20 20 22 63 6f 6e 73 6c  list",    "consl
29d97 69 73 74 5f 6f 70 74 22 2c 0a 20 20 22 73 65 6c  ist_opt",.  "sel
29d98 65 63 74 22 2c 20 20 20 20 20 20 20 20 22 63 6f  ect",        "co
29d99 6c 75 6d 6e 22 2c 20 20 20 20 20 20 20 20 22 63  lumn",        "c
29d9a 6f 6c 75 6d 6e 69 64 22 2c 20 20 20 20 20 20 22  olumnid",      "
29d9b 74 79 70 65 22 2c 20 20 20 20 20 20 20 20 0a 20  type",        . 
29d9c 20 22 63 61 72 67 6c 69 73 74 22 2c 20 20 20 20   "carglist",    
29d9d 20 20 22 69 64 22 2c 20 20 20 20 20 20 20 20 20    "id",         
29d9e 20 20 20 22 69 64 73 22 2c 20 20 20 20 20 20 20     "ids",       
29d9f 20 20 20 20 22 74 79 70 65 74 6f 6b 65 6e 22 2c      "typetoken",
29da0 20 20 20 0a 20 20 22 74 79 70 65 6e 61 6d 65 22     .  "typename"
29da1 2c 20 20 20 20 20 20 22 73 69 67 6e 65 64 22 2c  ,      "signed",
29da2 20 20 20 20 20 20 20 20 22 70 6c 75 73 5f 6e 75          "plus_nu
29da3 6d 22 2c 20 20 20 20 20 20 22 6d 69 6e 75 73 5f  m",      "minus_
29da4 6e 75 6d 22 2c 20 20 20 0a 20 20 22 63 61 72 67  num",   .  "carg
29da5 22 2c 20 20 20 20 20 20 20 20 20 20 22 63 63 6f  ",          "cco
29da6 6e 73 22 2c 20 20 20 20 20 20 20 20 20 22 74 65  ns",         "te
29da7 72 6d 22 2c 20 20 20 20 20 20 20 20 20 20 22 65  rm",          "e
29da8 78 70 72 22 2c 20 20 20 20 20 20 20 20 0a 20 20  xpr",        .  
29da9 22 6f 6e 63 6f 6e 66 22 2c 20 20 20 20 20 20 20  "onconf",       
29daa 20 22 73 6f 72 74 6f 72 64 65 72 22 2c 20 20 20   "sortorder",   
29dab 20 20 22 61 75 74 6f 69 6e 63 22 2c 20 20 20 20    "autoinc",    
29dac 20 20 20 22 69 64 78 6c 69 73 74 5f 6f 70 74 22     "idxlist_opt"
29dad 2c 20 0a 20 20 22 72 65 66 61 72 67 73 22 2c 20  , .  "refargs", 
29dae 20 20 20 20 20 20 22 64 65 66 65 72 5f 73 75 62        "defer_sub
29daf 63 6c 61 75 73 65 22 2c 20 20 22 72 65 66 61 72  clause",  "refar
29db0 67 22 2c 20 20 20 20 20 20 20 20 22 72 65 66 61  g",        "refa
29db1 63 74 22 2c 20 20 20 20 20 20 0a 20 20 22 69 6e  ct",      .  "in
29db2 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64  it_deferred_pred
29db3 5f 6f 70 74 22 2c 20 20 22 63 6f 6e 73 6c 69 73  _opt",  "conslis
29db4 74 22 2c 20 20 20 20 20 20 22 74 63 6f 6e 73 22  t",      "tcons"
29db5 2c 20 20 20 20 20 20 20 20 20 22 69 64 78 6c 69  ,         "idxli
29db6 73 74 22 2c 20 20 20 20 20 0a 20 20 22 64 65 66  st",     .  "def
29db7 65 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74  er_subclause_opt
29db8 22 2c 20 20 22 6f 72 63 6f 6e 66 22 2c 20 20 20  ",  "orconf",   
29db9 20 20 20 20 20 22 72 65 73 6f 6c 76 65 74 79 70       "resolvetyp
29dba 65 22 2c 20 20 20 22 72 61 69 73 65 74 79 70 65  e",   "raisetype
29dbb 22 2c 20 20 20 0a 20 20 22 69 66 65 78 69 73 74  ",   .  "ifexist
29dbc 73 22 2c 20 20 20 20 20 20 22 66 75 6c 6c 6e 61  s",      "fullna
29dbd 6d 65 22 2c 20 20 20 20 20 20 22 6f 6e 65 73 65  me",      "onese
29dbe 6c 65 63 74 22 2c 20 20 20 20 20 22 6d 75 6c 74  lect",     "mult
29dbf 69 73 65 6c 65 63 74 5f 6f 70 22 2c 0a 20 20 22  iselect_op",.  "
29dc0 64 69 73 74 69 6e 63 74 22 2c 20 20 20 20 20 20  distinct",      
29dc1 22 73 65 6c 63 6f 6c 6c 69 73 74 22 2c 20 20 20  "selcollist",   
29dc2 20 22 66 72 6f 6d 22 2c 20 20 20 20 20 20 20 20   "from",        
29dc3 20 20 22 77 68 65 72 65 5f 6f 70 74 22 2c 20 20    "where_opt",  
29dc4 20 0a 20 20 22 67 72 6f 75 70 62 79 5f 6f 70 74   .  "groupby_opt
29dc5 22 2c 20 20 20 22 68 61 76 69 6e 67 5f 6f 70 74  ",   "having_opt
29dc6 22 2c 20 20 20 20 22 6f 72 64 65 72 62 79 5f 6f  ",    "orderby_o
29dc7 70 74 22 2c 20 20 20 22 6c 69 6d 69 74 5f 6f 70  pt",   "limit_op
29dc8 74 22 2c 20 20 20 0a 20 20 22 73 63 6c 70 22 2c  t",   .  "sclp",
29dc9 20 20 20 20 20 20 20 20 20 20 22 61 73 22 2c 20            "as", 
29dca 20 20 20 20 20 20 20 20 20 20 20 22 73 65 6c 74             "selt
29dcb 61 62 6c 69 73 74 22 2c 20 20 20 20 22 73 74 6c  ablist",    "stl
29dcc 5f 70 72 65 66 69 78 22 2c 20 20 0a 20 20 22 6a  _prefix",  .  "j
29dcd 6f 69 6e 6f 70 22 2c 20 20 20 20 20 20 20 20 22  oinop",        "
29dce 69 6e 64 65 78 65 64 5f 6f 70 74 22 2c 20 20 20  indexed_opt",   
29dcf 22 6f 6e 5f 6f 70 74 22 2c 20 20 20 20 20 20 20  "on_opt",       
29dd0 20 22 75 73 69 6e 67 5f 6f 70 74 22 2c 20 20 20   "using_opt",   
29dd1 0a 20 20 22 73 65 6c 74 61 62 6c 69 73 74 5f 70  .  "seltablist_p
29dd2 61 72 65 6e 22 2c 20 20 22 6a 6f 69 6e 6f 70 32  aren",  "joinop2
29dd3 22 2c 20 20 20 20 20 20 20 22 69 6e 73 63 6f 6c  ",       "inscol
29dd4 6c 69 73 74 22 2c 20 20 20 20 22 73 6f 72 74 6c  list",    "sortl
29dd5 69 73 74 22 2c 20 20 20 20 0a 20 20 22 73 6f 72  ist",    .  "sor
29dd6 74 69 74 65 6d 22 2c 20 20 20 20 20 20 22 6e 65  titem",      "ne
29dd7 78 70 72 6c 69 73 74 22 2c 20 20 20 20 20 22 73  xprlist",     "s
29dd8 65 74 6c 69 73 74 22 2c 20 20 20 20 20 20 20 22  etlist",       "
29dd9 69 6e 73 65 72 74 5f 63 6d 64 22 2c 20 20 0a 20  insert_cmd",  . 
29dda 20 22 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74   "inscollist_opt
29ddb 22 2c 20 20 22 69 74 65 6d 6c 69 73 74 22 2c 20  ",  "itemlist", 
29ddc 20 20 20 20 20 22 65 78 70 72 6c 69 73 74 22 2c       "exprlist",
29ddd 20 20 20 20 20 20 22 6c 69 6b 65 6f 70 22 2c 20        "likeop", 
29dde 20 20 20 20 20 0a 20 20 22 65 73 63 61 70 65 22       .  "escape"
29ddf 2c 20 20 20 20 20 20 20 20 22 62 65 74 77 65 65  ,        "betwee
29de0 6e 5f 6f 70 22 2c 20 20 20 20 22 69 6e 5f 6f 70  n_op",    "in_op
29de1 22 2c 20 20 20 20 20 20 20 20 20 22 63 61 73 65  ",         "case
29de2 5f 6f 70 65 72 61 6e 64 22 2c 0a 20 20 22 63 61  _operand",.  "ca
29de3 73 65 5f 65 78 70 72 6c 69 73 74 22 2c 20 20 22  se_exprlist",  "
29de4 63 61 73 65 5f 65 6c 73 65 22 2c 20 20 20 20 20  case_else",     
29de5 22 75 6e 69 71 75 65 66 6c 61 67 22 2c 20 20 20  "uniqueflag",   
29de6 20 22 63 6f 6c 6c 61 74 65 22 2c 20 20 20 20 20   "collate",     
29de7 0a 20 20 22 6e 6d 6e 75 6d 22 2c 20 20 20 20 20  .  "nmnum",     
29de8 20 20 20 20 22 70 6c 75 73 5f 6f 70 74 22 2c 20      "plus_opt", 
29de9 20 20 20 20 20 22 6e 75 6d 62 65 72 22 2c 20 20       "number",  
29dea 20 20 20 20 20 20 22 74 72 69 67 67 65 72 5f 64        "trigger_d
29deb 65 63 6c 22 2c 0a 20 20 22 74 72 69 67 67 65 72  ecl",.  "trigger
29dec 5f 63 6d 64 5f 6c 69 73 74 22 2c 20 20 22 74 72  _cmd_list",  "tr
29ded 69 67 67 65 72 5f 74 69 6d 65 22 2c 20 20 22 74  igger_time",  "t
29dee 72 69 67 67 65 72 5f 65 76 65 6e 74 22 2c 20 20  rigger_event",  
29def 22 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 22  "foreach_clause"
29df0 2c 0a 20 20 22 77 68 65 6e 5f 63 6c 61 75 73 65  ,.  "when_clause
29df1 22 2c 20 20 20 22 74 72 69 67 67 65 72 5f 63 6d  ",   "trigger_cm
29df2 64 22 2c 20 20 20 22 64 61 74 61 62 61 73 65 5f  d",   "database_
29df3 6b 77 5f 6f 70 74 22 2c 20 20 22 6b 65 79 5f 6f  kw_opt",  "key_o
29df4 70 74 22 2c 20 20 20 20 20 0a 20 20 22 61 64 64  pt",     .  "add
29df5 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65  _column_fullname
29df6 22 2c 20 20 22 6b 77 63 6f 6c 75 6d 6e 5f 6f 70  ",  "kwcolumn_op
29df7 74 22 2c 20 20 22 63 72 65 61 74 65 5f 76 74 61  t",  "create_vta
29df8 62 22 2c 20 20 20 22 76 74 61 62 61 72 67 6c 69  b",   "vtabargli
29df9 73 74 22 2c 20 0a 20 20 22 76 74 61 62 61 72 67  st", .  "vtabarg
29dfa 22 2c 20 20 20 20 20 20 20 22 76 74 61 62 61 72  ",       "vtabar
29dfb 67 74 6f 6b 65 6e 22 2c 20 20 22 6c 70 22 2c 20  gtoken",  "lp", 
29dfc 20 20 20 20 20 20 20 20 20 20 20 22 61 6e 79 6c             "anyl
29dfd 69 73 74 22 2c 20 20 20 20 20 0a 7d 3b 0a 23 65  ist",     .};.#e
29dfe 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a  ndif /* NDEBUG *
29dff 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  /..#ifndef NDEBU
29e00 47 0a 2f 2a 20 46 6f 72 20 74 72 61 63 69 6e 67  G./* For tracing
29e01 20 72 65 64 75 63 65 20 61 63 74 69 6f 6e 73 2c   reduce actions,
29e02 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 61 6c   the names of al
29e03 6c 20 72 75 6c 65 73 20 61 72 65 20 72 65 71 75  l rules are requ
29e04 69 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ired..*/.static 
29e05 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
29e06 74 20 79 79 52 75 6c 65 4e 61 6d 65 5b 5d 20 3d  t yyRuleName[] =
29e07 20 7b 0a 20 2f 2a 20 20 20 30 20 2a 2f 20 22 69   {. /*   0 */ "i
29e08 6e 70 75 74 20 3a 3a 3d 20 63 6d 64 6c 69 73 74  nput ::= cmdlist
29e09 22 2c 0a 20 2f 2a 20 20 20 31 20 2a 2f 20 22 63  ",. /*   1 */ "c
29e0a 6d 64 6c 69 73 74 20 3a 3a 3d 20 63 6d 64 6c 69  mdlist ::= cmdli
29e0b 73 74 20 65 63 6d 64 22 2c 0a 20 2f 2a 20 20 20  st ecmd",. /*   
29e0c 32 20 2a 2f 20 22 63 6d 64 6c 69 73 74 20 3a 3a  2 */ "cmdlist ::
29e0d 3d 20 65 63 6d 64 22 2c 0a 20 2f 2a 20 20 20 33  = ecmd",. /*   3
29e0e 20 2a 2f 20 22 65 63 6d 64 20 3a 3a 3d 20 53 45   */ "ecmd ::= SE
29e0f 4d 49 22 2c 0a 20 2f 2a 20 20 20 34 20 2a 2f 20  MI",. /*   4 */ 
29e10 22 65 63 6d 64 20 3a 3a 3d 20 65 78 70 6c 61 69  "ecmd ::= explai
29e11 6e 20 63 6d 64 78 20 53 45 4d 49 22 2c 0a 20 2f  n cmdx SEMI",. /
29e12 2a 20 20 20 35 20 2a 2f 20 22 65 78 70 6c 61 69  *   5 */ "explai
29e13 6e 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 20 36 20  n ::=",. /*   6 
29e14 2a 2f 20 22 65 78 70 6c 61 69 6e 20 3a 3a 3d 20  */ "explain ::= 
29e15 45 58 50 4c 41 49 4e 22 2c 0a 20 2f 2a 20 20 20  EXPLAIN",. /*   
29e16 37 20 2a 2f 20 22 65 78 70 6c 61 69 6e 20 3a 3a  7 */ "explain ::
29e17 3d 20 45 58 50 4c 41 49 4e 20 51 55 45 52 59 20  = EXPLAIN QUERY 
29e18 50 4c 41 4e 22 2c 0a 20 2f 2a 20 20 20 38 20 2a  PLAN",. /*   8 *
29e19 2f 20 22 63 6d 64 78 20 3a 3a 3d 20 63 6d 64 22  / "cmdx ::= cmd"
29e1a 2c 0a 20 2f 2a 20 20 20 39 20 2a 2f 20 22 63 6d  ,. /*   9 */ "cm
29e1b 64 20 3a 3a 3d 20 42 45 47 49 4e 20 74 72 61 6e  d ::= BEGIN tran
29e1c 73 74 79 70 65 20 74 72 61 6e 73 5f 6f 70 74 22  stype trans_opt"
29e1d 2c 0a 20 2f 2a 20 20 31 30 20 2a 2f 20 22 74 72  ,. /*  10 */ "tr
29e1e 61 6e 73 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f  ans_opt ::=",. /
29e1f 2a 20 20 31 31 20 2a 2f 20 22 74 72 61 6e 73 5f  *  11 */ "trans_
29e20 6f 70 74 20 3a 3a 3d 20 54 52 41 4e 53 41 43 54  opt ::= TRANSACT
29e21 49 4f 4e 22 2c 0a 20 2f 2a 20 20 31 32 20 2a 2f  ION",. /*  12 */
29e22 20 22 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20   "trans_opt ::= 
29e23 54 52 41 4e 53 41 43 54 49 4f 4e 20 6e 6d 22 2c  TRANSACTION nm",
29e24 0a 20 2f 2a 20 20 31 33 20 2a 2f 20 22 74 72 61  . /*  13 */ "tra
29e25 6e 73 74 79 70 65 20 3a 3a 3d 22 2c 0a 20 2f 2a  nstype ::=",. /*
29e26 20 20 31 34 20 2a 2f 20 22 74 72 61 6e 73 74 79    14 */ "transty
29e27 70 65 20 3a 3a 3d 20 44 45 46 45 52 52 45 44 22  pe ::= DEFERRED"
29e28 2c 0a 20 2f 2a 20 20 31 35 20 2a 2f 20 22 74 72  ,. /*  15 */ "tr
29e29 61 6e 73 74 79 70 65 20 3a 3a 3d 20 49 4d 4d 45  anstype ::= IMME
29e2a 44 49 41 54 45 22 2c 0a 20 2f 2a 20 20 31 36 20  DIATE",. /*  16 
29e2b 2a 2f 20 22 74 72 61 6e 73 74 79 70 65 20 3a 3a  */ "transtype ::
29e2c 3d 20 45 58 43 4c 55 53 49 56 45 22 2c 0a 20 2f  = EXCLUSIVE",. /
29e2d 2a 20 20 31 37 20 2a 2f 20 22 63 6d 64 20 3a 3a  *  17 */ "cmd ::
29e2e 3d 20 43 4f 4d 4d 49 54 20 74 72 61 6e 73 5f 6f  = COMMIT trans_o
29e2f 70 74 22 2c 0a 20 2f 2a 20 20 31 38 20 2a 2f 20  pt",. /*  18 */ 
29e30 22 63 6d 64 20 3a 3a 3d 20 45 4e 44 20 74 72 61  "cmd ::= END tra
29e31 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 31 39  ns_opt",. /*  19
29e32 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 4f 4c   */ "cmd ::= ROL
29e33 4c 42 41 43 4b 20 74 72 61 6e 73 5f 6f 70 74 22  LBACK trans_opt"
29e34 2c 0a 20 2f 2a 20 20 32 30 20 2a 2f 20 22 63 6d  ,. /*  20 */ "cm
29e35 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 74 61 62  d ::= create_tab
29e36 6c 65 20 63 72 65 61 74 65 5f 74 61 62 6c 65 5f  le create_table_
29e37 61 72 67 73 22 2c 0a 20 2f 2a 20 20 32 31 20 2a  args",. /*  21 *
29e38 2f 20 22 63 72 65 61 74 65 5f 74 61 62 6c 65 20  / "create_table 
29e39 3a 3a 3d 20 43 52 45 41 54 45 20 74 65 6d 70 20  ::= CREATE temp 
29e3a 54 41 42 4c 45 20 69 66 6e 6f 74 65 78 69 73 74  TABLE ifnotexist
29e3b 73 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20  s nm dbnm",. /* 
29e3c 20 32 32 20 2a 2f 20 22 69 66 6e 6f 74 65 78 69   22 */ "ifnotexi
29e3d 73 74 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 32  sts ::=",. /*  2
29e3e 33 20 2a 2f 20 22 69 66 6e 6f 74 65 78 69 73 74  3 */ "ifnotexist
29e3f 73 20 3a 3a 3d 20 49 46 20 4e 4f 54 20 45 58 49  s ::= IF NOT EXI
29e40 53 54 53 22 2c 0a 20 2f 2a 20 20 32 34 20 2a 2f  STS",. /*  24 */
29e41 20 22 74 65 6d 70 20 3a 3a 3d 20 54 45 4d 50 22   "temp ::= TEMP"
29e42 2c 0a 20 2f 2a 20 20 32 35 20 2a 2f 20 22 74 65  ,. /*  25 */ "te
29e43 6d 70 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 32 36  mp ::=",. /*  26
29e44 20 2a 2f 20 22 63 72 65 61 74 65 5f 74 61 62 6c   */ "create_tabl
29e45 65 5f 61 72 67 73 20 3a 3a 3d 20 4c 50 20 63 6f  e_args ::= LP co
29e46 6c 75 6d 6e 6c 69 73 74 20 63 6f 6e 73 6c 69 73  lumnlist conslis
29e47 74 5f 6f 70 74 20 52 50 22 2c 0a 20 2f 2a 20 20  t_opt RP",. /*  
29e48 32 37 20 2a 2f 20 22 63 72 65 61 74 65 5f 74 61  27 */ "create_ta
29e49 62 6c 65 5f 61 72 67 73 20 3a 3a 3d 20 41 53 20  ble_args ::= AS 
29e4a 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 20 32 38  select",. /*  28
29e4b 20 2a 2f 20 22 63 6f 6c 75 6d 6e 6c 69 73 74 20   */ "columnlist 
29e4c 3a 3a 3d 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 43  ::= columnlist C
29e4d 4f 4d 4d 41 20 63 6f 6c 75 6d 6e 22 2c 0a 20 2f  OMMA column",. /
29e4e 2a 20 20 32 39 20 2a 2f 20 22 63 6f 6c 75 6d 6e  *  29 */ "column
29e4f 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 22  list ::= column"
29e50 2c 0a 20 2f 2a 20 20 33 30 20 2a 2f 20 22 63 6f  ,. /*  30 */ "co
29e51 6c 75 6d 6e 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 69  lumn ::= columni
29e52 64 20 74 79 70 65 20 63 61 72 67 6c 69 73 74 22  d type carglist"
29e53 2c 0a 20 2f 2a 20 20 33 31 20 2a 2f 20 22 63 6f  ,. /*  31 */ "co
29e54 6c 75 6d 6e 69 64 20 3a 3a 3d 20 6e 6d 22 2c 0a  lumnid ::= nm",.
29e55 20 2f 2a 20 20 33 32 20 2a 2f 20 22 69 64 20 3a   /*  32 */ "id :
29e56 3a 3d 20 49 44 22 2c 0a 20 2f 2a 20 20 33 33 20  := ID",. /*  33 
29e57 2a 2f 20 22 69 64 73 20 3a 3a 3d 20 49 44 7c 53  */ "ids ::= ID|S
29e58 54 52 49 4e 47 22 2c 0a 20 2f 2a 20 20 33 34 20  TRING",. /*  34 
29e59 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 49 44 22 2c 0a  */ "nm ::= ID",.
29e5a 20 2f 2a 20 20 33 35 20 2a 2f 20 22 6e 6d 20 3a   /*  35 */ "nm :
29e5b 3a 3d 20 53 54 52 49 4e 47 22 2c 0a 20 2f 2a 20  := STRING",. /* 
29e5c 20 33 36 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 4a   36 */ "nm ::= J
29e5d 4f 49 4e 5f 4b 57 22 2c 0a 20 2f 2a 20 20 33 37  OIN_KW",. /*  37
29e5e 20 2a 2f 20 22 74 79 70 65 20 3a 3a 3d 22 2c 0a   */ "type ::=",.
29e5f 20 2f 2a 20 20 33 38 20 2a 2f 20 22 74 79 70 65   /*  38 */ "type
29e60 20 3a 3a 3d 20 74 79 70 65 74 6f 6b 65 6e 22 2c   ::= typetoken",
29e61 0a 20 2f 2a 20 20 33 39 20 2a 2f 20 22 74 79 70  . /*  39 */ "typ
29e62 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e  etoken ::= typen
29e63 61 6d 65 22 2c 0a 20 2f 2a 20 20 34 30 20 2a 2f  ame",. /*  40 */
29e64 20 22 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20   "typetoken ::= 
29e65 74 79 70 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e  typename LP sign
29e66 65 64 20 52 50 22 2c 0a 20 2f 2a 20 20 34 31 20  ed RP",. /*  41 
29e67 2a 2f 20 22 74 79 70 65 74 6f 6b 65 6e 20 3a 3a  */ "typetoken ::
29e68 3d 20 74 79 70 65 6e 61 6d 65 20 4c 50 20 73 69  = typename LP si
29e69 67 6e 65 64 20 43 4f 4d 4d 41 20 73 69 67 6e 65  gned COMMA signe
29e6a 64 20 52 50 22 2c 0a 20 2f 2a 20 20 34 32 20 2a  d RP",. /*  42 *
29e6b 2f 20 22 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20  / "typename ::= 
29e6c 69 64 73 22 2c 0a 20 2f 2a 20 20 34 33 20 2a 2f  ids",. /*  43 */
29e6d 20 22 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20 74   "typename ::= t
29e6e 79 70 65 6e 61 6d 65 20 69 64 73 22 2c 0a 20 2f  ypename ids",. /
29e6f 2a 20 20 34 34 20 2a 2f 20 22 73 69 67 6e 65 64  *  44 */ "signed
29e70 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d 22 2c 0a   ::= plus_num",.
29e71 20 2f 2a 20 20 34 35 20 2a 2f 20 22 73 69 67 6e   /*  45 */ "sign
29e72 65 64 20 3a 3a 3d 20 6d 69 6e 75 73 5f 6e 75 6d  ed ::= minus_num
29e73 22 2c 0a 20 2f 2a 20 20 34 36 20 2a 2f 20 22 63  ",. /*  46 */ "c
29e74 61 72 67 6c 69 73 74 20 3a 3a 3d 20 63 61 72 67  arglist ::= carg
29e75 6c 69 73 74 20 63 61 72 67 22 2c 0a 20 2f 2a 20  list carg",. /* 
29e76 20 34 37 20 2a 2f 20 22 63 61 72 67 6c 69 73 74   47 */ "carglist
29e77 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 34 38 20 2a   ::=",. /*  48 *
29e78 2f 20 22 63 61 72 67 20 3a 3a 3d 20 43 4f 4e 53  / "carg ::= CONS
29e79 54 52 41 49 4e 54 20 6e 6d 20 63 63 6f 6e 73 22  TRAINT nm ccons"
29e7a 2c 0a 20 2f 2a 20 20 34 39 20 2a 2f 20 22 63 61  ,. /*  49 */ "ca
29e7b 72 67 20 3a 3a 3d 20 63 63 6f 6e 73 22 2c 0a 20  rg ::= ccons",. 
29e7c 2f 2a 20 20 35 30 20 2a 2f 20 22 63 63 6f 6e 73  /*  50 */ "ccons
29e7d 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 74 65 72   ::= DEFAULT ter
29e7e 6d 22 2c 0a 20 2f 2a 20 20 35 31 20 2a 2f 20 22  m",. /*  51 */ "
29e7f 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c  ccons ::= DEFAUL
29e80 54 20 4c 50 20 65 78 70 72 20 52 50 22 2c 0a 20  T LP expr RP",. 
29e81 2f 2a 20 20 35 32 20 2a 2f 20 22 63 63 6f 6e 73  /*  52 */ "ccons
29e82 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 50 4c 55   ::= DEFAULT PLU
29e83 53 20 74 65 72 6d 22 2c 0a 20 2f 2a 20 20 35 33  S term",. /*  53
29e84 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 44   */ "ccons ::= D
29e85 45 46 41 55 4c 54 20 4d 49 4e 55 53 20 74 65 72  EFAULT MINUS ter
29e86 6d 22 2c 0a 20 2f 2a 20 20 35 34 20 2a 2f 20 22  m",. /*  54 */ "
29e87 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c  ccons ::= DEFAUL
29e88 54 20 69 64 22 2c 0a 20 2f 2a 20 20 35 35 20 2a  T id",. /*  55 *
29e89 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 4e 55 4c  / "ccons ::= NUL
29e8a 4c 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20  L onconf",. /*  
29e8b 35 36 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d  56 */ "ccons ::=
29e8c 20 4e 4f 54 20 4e 55 4c 4c 20 6f 6e 63 6f 6e 66   NOT NULL onconf
29e8d 22 2c 0a 20 2f 2a 20 20 35 37 20 2a 2f 20 22 63  ",. /*  57 */ "c
29e8e 63 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d 41 52 59  cons ::= PRIMARY
29e8f 20 4b 45 59 20 73 6f 72 74 6f 72 64 65 72 20 6f   KEY sortorder o
29e90 6e 63 6f 6e 66 20 61 75 74 6f 69 6e 63 22 2c 0a  nconf autoinc",.
29e91 20 2f 2a 20 20 35 38 20 2a 2f 20 22 63 63 6f 6e   /*  58 */ "ccon
29e92 73 20 3a 3a 3d 20 55 4e 49 51 55 45 20 6f 6e 63  s ::= UNIQUE onc
29e93 6f 6e 66 22 2c 0a 20 2f 2a 20 20 35 39 20 2a 2f  onf",. /*  59 */
29e94 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43   "ccons ::= CHEC
29e95 4b 20 4c 50 20 65 78 70 72 20 52 50 22 2c 0a 20  K LP expr RP",. 
29e96 2f 2a 20 20 36 30 20 2a 2f 20 22 63 63 6f 6e 73  /*  60 */ "ccons
29e97 20 3a 3a 3d 20 52 45 46 45 52 45 4e 43 45 53 20   ::= REFERENCES 
29e98 6e 6d 20 69 64 78 6c 69 73 74 5f 6f 70 74 20 72  nm idxlist_opt r
29e99 65 66 61 72 67 73 22 2c 0a 20 2f 2a 20 20 36 31  efargs",. /*  61
29e9a 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 64   */ "ccons ::= d
29e9b 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 22 2c  efer_subclause",
29e9c 0a 20 2f 2a 20 20 36 32 20 2a 2f 20 22 63 63 6f  . /*  62 */ "cco
29e9d 6e 73 20 3a 3a 3d 20 43 4f 4c 4c 41 54 45 20 69  ns ::= COLLATE i
29e9e 64 73 22 2c 0a 20 2f 2a 20 20 36 33 20 2a 2f 20  ds",. /*  63 */ 
29e9f 22 61 75 74 6f 69 6e 63 20 3a 3a 3d 22 2c 0a 20  "autoinc ::=",. 
29ea0 2f 2a 20 20 36 34 20 2a 2f 20 22 61 75 74 6f 69  /*  64 */ "autoi
29ea1 6e 63 20 3a 3a 3d 20 41 55 54 4f 49 4e 43 52 22  nc ::= AUTOINCR"
29ea2 2c 0a 20 2f 2a 20 20 36 35 20 2a 2f 20 22 72 65  ,. /*  65 */ "re
29ea3 66 61 72 67 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  fargs ::=",. /* 
29ea4 20 36 36 20 2a 2f 20 22 72 65 66 61 72 67 73 20   66 */ "refargs 
29ea5 3a 3a 3d 20 72 65 66 61 72 67 73 20 72 65 66 61  ::= refargs refa
29ea6 72 67 22 2c 0a 20 2f 2a 20 20 36 37 20 2a 2f 20  rg",. /*  67 */ 
29ea7 22 72 65 66 61 72 67 20 3a 3a 3d 20 4d 41 54 43  "refarg ::= MATC
29ea8 48 20 6e 6d 22 2c 0a 20 2f 2a 20 20 36 38 20 2a  H nm",. /*  68 *
29ea9 2f 20 22 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e  / "refarg ::= ON
29eaa 20 44 45 4c 45 54 45 20 72 65 66 61 63 74 22 2c   DELETE refact",
29eab 0a 20 2f 2a 20 20 36 39 20 2a 2f 20 22 72 65 66  . /*  69 */ "ref
29eac 61 72 67 20 3a 3a 3d 20 4f 4e 20 55 50 44 41 54  arg ::= ON UPDAT
29ead 45 20 72 65 66 61 63 74 22 2c 0a 20 2f 2a 20 20  E refact",. /*  
29eae 37 30 20 2a 2f 20 22 72 65 66 61 72 67 20 3a 3a  70 */ "refarg ::
29eaf 3d 20 4f 4e 20 49 4e 53 45 52 54 20 72 65 66 61  = ON INSERT refa
29eb0 63 74 22 2c 0a 20 2f 2a 20 20 37 31 20 2a 2f 20  ct",. /*  71 */ 
29eb1 22 72 65 66 61 63 74 20 3a 3a 3d 20 53 45 54 20  "refact ::= SET 
29eb2 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 20 37 32 20 2a  NULL",. /*  72 *
29eb3 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d 20 53 45  / "refact ::= SE
29eb4 54 20 44 45 46 41 55 4c 54 22 2c 0a 20 2f 2a 20  T DEFAULT",. /* 
29eb5 20 37 33 20 2a 2f 20 22 72 65 66 61 63 74 20 3a   73 */ "refact :
29eb6 3a 3d 20 43 41 53 43 41 44 45 22 2c 0a 20 2f 2a  := CASCADE",. /*
29eb7 20 20 37 34 20 2a 2f 20 22 72 65 66 61 63 74 20    74 */ "refact 
29eb8 3a 3a 3d 20 52 45 53 54 52 49 43 54 22 2c 0a 20  ::= RESTRICT",. 
29eb9 2f 2a 20 20 37 35 20 2a 2f 20 22 64 65 66 65 72  /*  75 */ "defer
29eba 5f 73 75 62 63 6c 61 75 73 65 20 3a 3a 3d 20 4e  _subclause ::= N
29ebb 4f 54 20 44 45 46 45 52 52 41 42 4c 45 20 69 6e  OT DEFERRABLE in
29ebc 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64  it_deferred_pred
29ebd 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 37 36 20 2a  _opt",. /*  76 *
29ebe 2f 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75  / "defer_subclau
29ebf 73 65 20 3a 3a 3d 20 44 45 46 45 52 52 41 42 4c  se ::= DEFERRABL
29ec0 45 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f  E init_deferred_
29ec1 70 72 65 64 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20  pred_opt",. /*  
29ec2 37 37 20 2a 2f 20 22 69 6e 69 74 5f 64 65 66 65  77 */ "init_defe
29ec3 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a  rred_pred_opt ::
29ec4 3d 22 2c 0a 20 2f 2a 20 20 37 38 20 2a 2f 20 22  =",. /*  78 */ "
29ec5 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72  init_deferred_pr
29ec6 65 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 49 54 49  ed_opt ::= INITI
29ec7 41 4c 4c 59 20 44 45 46 45 52 52 45 44 22 2c 0a  ALLY DEFERRED",.
29ec8 20 2f 2a 20 20 37 39 20 2a 2f 20 22 69 6e 69 74   /*  79 */ "init
29ec9 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f  _deferred_pred_o
29eca 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59  pt ::= INITIALLY
29ecb 20 49 4d 4d 45 44 49 41 54 45 22 2c 0a 20 2f 2a   IMMEDIATE",. /*
29ecc 20 20 38 30 20 2a 2f 20 22 63 6f 6e 73 6c 69 73    80 */ "conslis
29ecd 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  t_opt ::=",. /* 
29ece 20 38 31 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74   81 */ "conslist
29ecf 5f 6f 70 74 20 3a 3a 3d 20 43 4f 4d 4d 41 20 63  _opt ::= COMMA c
29ed0 6f 6e 73 6c 69 73 74 22 2c 0a 20 2f 2a 20 20 38  onslist",. /*  8
29ed1 32 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 20 3a  2 */ "conslist :
29ed2 3a 3d 20 63 6f 6e 73 6c 69 73 74 20 43 4f 4d 4d  := conslist COMM
29ed3 41 20 74 63 6f 6e 73 22 2c 0a 20 2f 2a 20 20 38  A tcons",. /*  8
29ed4 33 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 20 3a  3 */ "conslist :
29ed5 3a 3d 20 63 6f 6e 73 6c 69 73 74 20 74 63 6f 6e  := conslist tcon
29ed6 73 22 2c 0a 20 2f 2a 20 20 38 34 20 2a 2f 20 22  s",. /*  84 */ "
29ed7 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 74 63 6f  conslist ::= tco
29ed8 6e 73 22 2c 0a 20 2f 2a 20 20 38 35 20 2a 2f 20  ns",. /*  85 */ 
29ed9 22 74 63 6f 6e 73 20 3a 3a 3d 20 43 4f 4e 53 54  "tcons ::= CONST
29eda 52 41 49 4e 54 20 6e 6d 22 2c 0a 20 2f 2a 20 20  RAINT nm",. /*  
29edb 38 36 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d  86 */ "tcons ::=
29edc 20 50 52 49 4d 41 52 59 20 4b 45 59 20 4c 50 20   PRIMARY KEY LP 
29edd 69 64 78 6c 69 73 74 20 61 75 74 6f 69 6e 63 20  idxlist autoinc 
29ede 52 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20  RP onconf",. /* 
29edf 20 38 37 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a   87 */ "tcons ::
29ee0 3d 20 55 4e 49 51 55 45 20 4c 50 20 69 64 78 6c  = UNIQUE LP idxl
29ee1 69 73 74 20 52 50 20 6f 6e 63 6f 6e 66 22 2c 0a  ist RP onconf",.
29ee2 20 2f 2a 20 20 38 38 20 2a 2f 20 22 74 63 6f 6e   /*  88 */ "tcon
29ee3 73 20 3a 3a 3d 20 43 48 45 43 4b 20 4c 50 20 65  s ::= CHECK LP e
29ee4 78 70 72 20 52 50 20 6f 6e 63 6f 6e 66 22 2c 0a  xpr RP onconf",.
29ee5 20 2f 2a 20 20 38 39 20 2a 2f 20 22 74 63 6f 6e   /*  89 */ "tcon
29ee6 73 20 3a 3a 3d 20 46 4f 52 45 49 47 4e 20 4b 45  s ::= FOREIGN KE
29ee7 59 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20  Y LP idxlist RP 
29ee8 52 45 46 45 52 45 4e 43 45 53 20 6e 6d 20 69 64  REFERENCES nm id
29ee9 78 6c 69 73 74 5f 6f 70 74 20 72 65 66 61 72 67  xlist_opt refarg
29eea 73 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73  s defer_subclaus
29eeb 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 39 30 20  e_opt",. /*  90 
29eec 2a 2f 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61  */ "defer_subcla
29eed 75 73 65 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f  use_opt ::=",. /
29eee 2a 20 20 39 31 20 2a 2f 20 22 64 65 66 65 72 5f  *  91 */ "defer_
29eef 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 20 3a 3a  subclause_opt ::
29ef0 3d 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73  = defer_subclaus
29ef1 65 22 2c 0a 20 2f 2a 20 20 39 32 20 2a 2f 20 22  e",. /*  92 */ "
29ef2 6f 6e 63 6f 6e 66 20 3a 3a 3d 22 2c 0a 20 2f 2a  onconf ::=",. /*
29ef3 20 20 39 33 20 2a 2f 20 22 6f 6e 63 6f 6e 66 20    93 */ "onconf 
29ef4 3a 3a 3d 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20  ::= ON CONFLICT 
29ef5 72 65 73 6f 6c 76 65 74 79 70 65 22 2c 0a 20 2f  resolvetype",. /
29ef6 2a 20 20 39 34 20 2a 2f 20 22 6f 72 63 6f 6e 66  *  94 */ "orconf
29ef7 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 39 35 20 2a   ::=",. /*  95 *
29ef8 2f 20 22 6f 72 63 6f 6e 66 20 3a 3a 3d 20 4f 52  / "orconf ::= OR
29ef9 20 72 65 73 6f 6c 76 65 74 79 70 65 22 2c 0a 20   resolvetype",. 
29efa 2f 2a 20 20 39 36 20 2a 2f 20 22 72 65 73 6f 6c  /*  96 */ "resol
29efb 76 65 74 79 70 65 20 3a 3a 3d 20 72 61 69 73 65  vetype ::= raise
29efc 74 79 70 65 22 2c 0a 20 2f 2a 20 20 39 37 20 2a  type",. /*  97 *
29efd 2f 20 22 72 65 73 6f 6c 76 65 74 79 70 65 20 3a  / "resolvetype :
29efe 3a 3d 20 49 47 4e 4f 52 45 22 2c 0a 20 2f 2a 20  := IGNORE",. /* 
29eff 20 39 38 20 2a 2f 20 22 72 65 73 6f 6c 76 65 74   98 */ "resolvet
29f00 79 70 65 20 3a 3a 3d 20 52 45 50 4c 41 43 45 22  ype ::= REPLACE"
29f01 2c 0a 20 2f 2a 20 20 39 39 20 2a 2f 20 22 63 6d  ,. /*  99 */ "cm
29f02 64 20 3a 3a 3d 20 44 52 4f 50 20 54 41 42 4c 45  d ::= DROP TABLE
29f03 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61   ifexists fullna
29f04 6d 65 22 2c 0a 20 2f 2a 20 31 30 30 20 2a 2f 20  me",. /* 100 */ 
29f05 22 69 66 65 78 69 73 74 73 20 3a 3a 3d 20 49 46  "ifexists ::= IF
29f06 20 45 58 49 53 54 53 22 2c 0a 20 2f 2a 20 31 30   EXISTS",. /* 10
29f07 31 20 2a 2f 20 22 69 66 65 78 69 73 74 73 20 3a  1 */ "ifexists :
29f08 3a 3d 22 2c 0a 20 2f 2a 20 31 30 32 20 2a 2f 20  :=",. /* 102 */ 
29f09 22 63 6d 64 20 3a 3a 3d 20 43 52 45 41 54 45 20  "cmd ::= CREATE 
29f0a 74 65 6d 70 20 56 49 45 57 20 69 66 6e 6f 74 65  temp VIEW ifnote
29f0b 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 41 53  xists nm dbnm AS
29f0c 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 30   select",. /* 10
29f0d 33 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 52  3 */ "cmd ::= DR
29f0e 4f 50 20 56 49 45 57 20 69 66 65 78 69 73 74 73  OP VIEW ifexists
29f0f 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20   fullname",. /* 
29f10 31 30 34 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  104 */ "cmd ::= 
29f11 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 30 35  select",. /* 105
29f12 20 2a 2f 20 22 73 65 6c 65 63 74 20 3a 3a 3d 20   */ "select ::= 
29f13 6f 6e 65 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20  oneselect",. /* 
29f14 31 30 36 20 2a 2f 20 22 73 65 6c 65 63 74 20 3a  106 */ "select :
29f15 3a 3d 20 73 65 6c 65 63 74 20 6d 75 6c 74 69 73  := select multis
29f16 65 6c 65 63 74 5f 6f 70 20 6f 6e 65 73 65 6c 65  elect_op onesele
29f17 63 74 22 2c 0a 20 2f 2a 20 31 30 37 20 2a 2f 20  ct",. /* 107 */ 
29f18 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20  "multiselect_op 
29f19 3a 3a 3d 20 55 4e 49 4f 4e 22 2c 0a 20 2f 2a 20  ::= UNION",. /* 
29f1a 31 30 38 20 2a 2f 20 22 6d 75 6c 74 69 73 65 6c  108 */ "multisel
29f1b 65 63 74 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f 4e  ect_op ::= UNION
29f1c 20 41 4c 4c 22 2c 0a 20 2f 2a 20 31 30 39 20 2a   ALL",. /* 109 *
29f1d 2f 20 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f  / "multiselect_o
29f1e 70 20 3a 3a 3d 20 45 58 43 45 50 54 7c 49 4e 54  p ::= EXCEPT|INT
29f1f 45 52 53 45 43 54 22 2c 0a 20 2f 2a 20 31 31 30  ERSECT",. /* 110
29f20 20 2a 2f 20 22 6f 6e 65 73 65 6c 65 63 74 20 3a   */ "oneselect :
29f21 3a 3d 20 53 45 4c 45 43 54 20 64 69 73 74 69 6e  := SELECT distin
29f22 63 74 20 73 65 6c 63 6f 6c 6c 69 73 74 20 66 72  ct selcollist fr
29f23 6f 6d 20 77 68 65 72 65 5f 6f 70 74 20 67 72 6f  om where_opt gro
29f24 75 70 62 79 5f 6f 70 74 20 68 61 76 69 6e 67 5f  upby_opt having_
29f25 6f 70 74 20 6f 72 64 65 72 62 79 5f 6f 70 74 20  opt orderby_opt 
29f26 6c 69 6d 69 74 5f 6f 70 74 22 2c 0a 20 2f 2a 20  limit_opt",. /* 
29f27 31 31 31 20 2a 2f 20 22 64 69 73 74 69 6e 63 74  111 */ "distinct
29f28 20 3a 3a 3d 20 44 49 53 54 49 4e 43 54 22 2c 0a   ::= DISTINCT",.
29f29 20 2f 2a 20 31 31 32 20 2a 2f 20 22 64 69 73 74   /* 112 */ "dist
29f2a 69 6e 63 74 20 3a 3a 3d 20 41 4c 4c 22 2c 0a 20  inct ::= ALL",. 
29f2b 2f 2a 20 31 31 33 20 2a 2f 20 22 64 69 73 74 69  /* 113 */ "disti
29f2c 6e 63 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 31  nct ::=",. /* 11
29f2d 34 20 2a 2f 20 22 73 63 6c 70 20 3a 3a 3d 20 73  4 */ "sclp ::= s
29f2e 65 6c 63 6f 6c 6c 69 73 74 20 43 4f 4d 4d 41 22  elcollist COMMA"
29f2f 2c 0a 20 2f 2a 20 31 31 35 20 2a 2f 20 22 73 63  ,. /* 115 */ "sc
29f30 6c 70 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 31 36  lp ::=",. /* 116
29f31 20 2a 2f 20 22 73 65 6c 63 6f 6c 6c 69 73 74 20   */ "selcollist 
29f32 3a 3a 3d 20 73 63 6c 70 20 65 78 70 72 20 61 73  ::= sclp expr as
29f33 22 2c 0a 20 2f 2a 20 31 31 37 20 2a 2f 20 22 73  ",. /* 117 */ "s
29f34 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63  elcollist ::= sc
29f35 6c 70 20 53 54 41 52 22 2c 0a 20 2f 2a 20 31 31  lp STAR",. /* 11
29f36 38 20 2a 2f 20 22 73 65 6c 63 6f 6c 6c 69 73 74  8 */ "selcollist
29f37 20 3a 3a 3d 20 73 63 6c 70 20 6e 6d 20 44 4f 54   ::= sclp nm DOT
29f38 20 53 54 41 52 22 2c 0a 20 2f 2a 20 31 31 39 20   STAR",. /* 119 
29f39 2a 2f 20 22 61 73 20 3a 3a 3d 20 41 53 20 6e 6d  */ "as ::= AS nm
29f3a 22 2c 0a 20 2f 2a 20 31 32 30 20 2a 2f 20 22 61  ",. /* 120 */ "a
29f3b 73 20 3a 3a 3d 20 69 64 73 22 2c 0a 20 2f 2a 20  s ::= ids",. /* 
29f3c 31 32 31 20 2a 2f 20 22 61 73 20 3a 3a 3d 22 2c  121 */ "as ::=",
29f3d 0a 20 2f 2a 20 31 32 32 20 2a 2f 20 22 66 72 6f  . /* 122 */ "fro
29f3e 6d 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 32 33 20  m ::=",. /* 123 
29f3f 2a 2f 20 22 66 72 6f 6d 20 3a 3a 3d 20 46 52 4f  */ "from ::= FRO
29f40 4d 20 73 65 6c 74 61 62 6c 69 73 74 22 2c 0a 20  M seltablist",. 
29f41 2f 2a 20 31 32 34 20 2a 2f 20 22 73 74 6c 5f 70  /* 124 */ "stl_p
29f42 72 65 66 69 78 20 3a 3a 3d 20 73 65 6c 74 61 62  refix ::= seltab
29f43 6c 69 73 74 20 6a 6f 69 6e 6f 70 22 2c 0a 20 2f  list joinop",. /
29f44 2a 20 31 32 35 20 2a 2f 20 22 73 74 6c 5f 70 72  * 125 */ "stl_pr
29f45 65 66 69 78 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31  efix ::=",. /* 1
29f46 32 36 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69 73  26 */ "seltablis
29f47 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78  t ::= stl_prefix
29f48 20 6e 6d 20 64 62 6e 6d 20 61 73 20 69 6e 64 65   nm dbnm as inde
29f49 78 65 64 5f 6f 70 74 20 6f 6e 5f 6f 70 74 20 75  xed_opt on_opt u
29f4a 73 69 6e 67 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31  sing_opt",. /* 1
29f4b 32 37 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69 73  27 */ "seltablis
29f4c 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78  t ::= stl_prefix
29f4d 20 4c 50 20 73 65 6c 74 61 62 6c 69 73 74 5f 70   LP seltablist_p
29f4e 61 72 65 6e 20 52 50 20 61 73 20 6f 6e 5f 6f 70  aren RP as on_op
29f4f 74 20 75 73 69 6e 67 5f 6f 70 74 22 2c 0a 20 2f  t using_opt",. /
29f50 2a 20 31 32 38 20 2a 2f 20 22 73 65 6c 74 61 62  * 128 */ "seltab
29f51 6c 69 73 74 5f 70 61 72 65 6e 20 3a 3a 3d 20 73  list_paren ::= s
29f52 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 32 39 20  elect",. /* 129 
29f53 2a 2f 20 22 73 65 6c 74 61 62 6c 69 73 74 5f 70  */ "seltablist_p
29f54 61 72 65 6e 20 3a 3a 3d 20 73 65 6c 74 61 62 6c  aren ::= seltabl
29f55 69 73 74 22 2c 0a 20 2f 2a 20 31 33 30 20 2a 2f  ist",. /* 130 */
29f56 20 22 64 62 6e 6d 20 3a 3a 3d 22 2c 0a 20 2f 2a   "dbnm ::=",. /*
29f57 20 31 33 31 20 2a 2f 20 22 64 62 6e 6d 20 3a 3a   131 */ "dbnm ::
29f58 3d 20 44 4f 54 20 6e 6d 22 2c 0a 20 2f 2a 20 31  = DOT nm",. /* 1
29f59 33 32 20 2a 2f 20 22 66 75 6c 6c 6e 61 6d 65 20  32 */ "fullname 
29f5a 3a 3a 3d 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f  ::= nm dbnm",. /
29f5b 2a 20 31 33 33 20 2a 2f 20 22 6a 6f 69 6e 6f 70  * 133 */ "joinop
29f5c 20 3a 3a 3d 20 43 4f 4d 4d 41 7c 4a 4f 49 4e 22   ::= COMMA|JOIN"
29f5d 2c 0a 20 2f 2a 20 31 33 34 20 2a 2f 20 22 6a 6f  ,. /* 134 */ "jo
29f5e 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57  inop ::= JOIN_KW
29f5f 20 4a 4f 49 4e 22 2c 0a 20 2f 2a 20 31 33 35 20   JOIN",. /* 135 
29f60 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a  */ "joinop ::= J
29f61 4f 49 4e 5f 4b 57 20 6e 6d 20 4a 4f 49 4e 22 2c  OIN_KW nm JOIN",
29f62 0a 20 2f 2a 20 31 33 36 20 2a 2f 20 22 6a 6f 69  . /* 136 */ "joi
29f63 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20  nop ::= JOIN_KW 
29f64 6e 6d 20 6e 6d 20 4a 4f 49 4e 22 2c 0a 20 2f 2a  nm nm JOIN",. /*
29f65 20 31 33 37 20 2a 2f 20 22 6f 6e 5f 6f 70 74 20   137 */ "on_opt 
29f66 3a 3a 3d 20 4f 4e 20 65 78 70 72 22 2c 0a 20 2f  ::= ON expr",. /
29f67 2a 20 31 33 38 20 2a 2f 20 22 6f 6e 5f 6f 70 74  * 138 */ "on_opt
29f68 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 33 39 20 2a   ::=",. /* 139 *
29f69 2f 20 22 69 6e 64 65 78 65 64 5f 6f 70 74 20 3a  / "indexed_opt :
29f6a 3a 3d 22 2c 0a 20 2f 2a 20 31 34 30 20 2a 2f 20  :=",. /* 140 */ 
29f6b 22 69 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d  "indexed_opt ::=
29f6c 20 49 4e 44 45 58 45 44 20 42 59 20 6e 6d 22 2c   INDEXED BY nm",
29f6d 0a 20 2f 2a 20 31 34 31 20 2a 2f 20 22 69 6e 64  . /* 141 */ "ind
29f6e 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20 4e 4f 54  exed_opt ::= NOT
29f6f 20 49 4e 44 45 58 45 44 22 2c 0a 20 2f 2a 20 31   INDEXED",. /* 1
29f70 34 32 20 2a 2f 20 22 75 73 69 6e 67 5f 6f 70 74  42 */ "using_opt
29f71 20 3a 3a 3d 20 55 53 49 4e 47 20 4c 50 20 69 6e   ::= USING LP in
29f72 73 63 6f 6c 6c 69 73 74 20 52 50 22 2c 0a 20 2f  scollist RP",. /
29f73 2a 20 31 34 33 20 2a 2f 20 22 75 73 69 6e 67 5f  * 143 */ "using_
29f74 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 34  opt ::=",. /* 14
29f75 34 20 2a 2f 20 22 6f 72 64 65 72 62 79 5f 6f 70  4 */ "orderby_op
29f76 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 34 35 20  t ::=",. /* 145 
29f77 2a 2f 20 22 6f 72 64 65 72 62 79 5f 6f 70 74 20  */ "orderby_opt 
29f78 3a 3a 3d 20 4f 52 44 45 52 20 42 59 20 73 6f 72  ::= ORDER BY sor
29f79 74 6c 69 73 74 22 2c 0a 20 2f 2a 20 31 34 36 20  tlist",. /* 146 
29f7a 2a 2f 20 22 73 6f 72 74 6c 69 73 74 20 3a 3a 3d  */ "sortlist ::=
29f7b 20 73 6f 72 74 6c 69 73 74 20 43 4f 4d 4d 41 20   sortlist COMMA 
29f7c 73 6f 72 74 69 74 65 6d 20 73 6f 72 74 6f 72 64  sortitem sortord
29f7d 65 72 22 2c 0a 20 2f 2a 20 31 34 37 20 2a 2f 20  er",. /* 147 */ 
29f7e 22 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20 73 6f  "sortlist ::= so
29f7f 72 74 69 74 65 6d 20 73 6f 72 74 6f 72 64 65 72  rtitem sortorder
29f80 22 2c 0a 20 2f 2a 20 31 34 38 20 2a 2f 20 22 73  ",. /* 148 */ "s
29f81 6f 72 74 69 74 65 6d 20 3a 3a 3d 20 65 78 70 72  ortitem ::= expr
29f82 22 2c 0a 20 2f 2a 20 31 34 39 20 2a 2f 20 22 73  ",. /* 149 */ "s
29f83 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 20 41 53 43  ortorder ::= ASC
29f84 22 2c 0a 20 2f 2a 20 31 35 30 20 2a 2f 20 22 73  ",. /* 150 */ "s
29f85 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 20 44 45 53  ortorder ::= DES
29f86 43 22 2c 0a 20 2f 2a 20 31 35 31 20 2a 2f 20 22  C",. /* 151 */ "
29f87 73 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 22 2c 0a  sortorder ::=",.
29f88 20 2f 2a 20 31 35 32 20 2a 2f 20 22 67 72 6f 75   /* 152 */ "grou
29f89 70 62 79 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f  pby_opt ::=",. /
29f8a 2a 20 31 35 33 20 2a 2f 20 22 67 72 6f 75 70 62  * 153 */ "groupb
29f8b 79 5f 6f 70 74 20 3a 3a 3d 20 47 52 4f 55 50 20  y_opt ::= GROUP 
29f8c 42 59 20 6e 65 78 70 72 6c 69 73 74 22 2c 0a 20  BY nexprlist",. 
29f8d 2f 2a 20 31 35 34 20 2a 2f 20 22 68 61 76 69 6e  /* 154 */ "havin
29f8e 67 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  g_opt ::=",. /* 
29f8f 31 35 35 20 2a 2f 20 22 68 61 76 69 6e 67 5f 6f  155 */ "having_o
29f90 70 74 20 3a 3a 3d 20 48 41 56 49 4e 47 20 65 78  pt ::= HAVING ex
29f91 70 72 22 2c 0a 20 2f 2a 20 31 35 36 20 2a 2f 20  pr",. /* 156 */ 
29f92 22 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 22 2c  "limit_opt ::=",
29f93 0a 20 2f 2a 20 31 35 37 20 2a 2f 20 22 6c 69 6d  . /* 157 */ "lim
29f94 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54  it_opt ::= LIMIT
29f95 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 35 38 20   expr",. /* 158 
29f96 2a 2f 20 22 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a  */ "limit_opt ::
29f97 3d 20 4c 49 4d 49 54 20 65 78 70 72 20 4f 46 46  = LIMIT expr OFF
29f98 53 45 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31  SET expr",. /* 1
29f99 35 39 20 2a 2f 20 22 6c 69 6d 69 74 5f 6f 70 74  59 */ "limit_opt
29f9a 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72 20   ::= LIMIT expr 
29f9b 43 4f 4d 4d 41 20 65 78 70 72 22 2c 0a 20 2f 2a  COMMA expr",. /*
29f9c 20 31 36 30 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   160 */ "cmd ::=
29f9d 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 66 75 6c   DELETE FROM ful
29f9e 6c 6e 61 6d 65 20 69 6e 64 65 78 65 64 5f 6f 70  lname indexed_op
29f9f 74 20 77 68 65 72 65 5f 6f 70 74 22 2c 0a 20 2f  t where_opt",. /
29fa0 2a 20 31 36 31 20 2a 2f 20 22 77 68 65 72 65 5f  * 161 */ "where_
29fa1 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 36  opt ::=",. /* 16
29fa2 32 20 2a 2f 20 22 77 68 65 72 65 5f 6f 70 74 20  2 */ "where_opt 
29fa3 3a 3a 3d 20 57 48 45 52 45 20 65 78 70 72 22 2c  ::= WHERE expr",
29fa4 0a 20 2f 2a 20 31 36 33 20 2a 2f 20 22 63 6d 64  . /* 163 */ "cmd
29fa5 20 3a 3a 3d 20 55 50 44 41 54 45 20 6f 72 63 6f   ::= UPDATE orco
29fa6 6e 66 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 64 65  nf fullname inde
29fa7 78 65 64 5f 6f 70 74 20 53 45 54 20 73 65 74 6c  xed_opt SET setl
29fa8 69 73 74 20 77 68 65 72 65 5f 6f 70 74 22 2c 0a  ist where_opt",.
29fa9 20 2f 2a 20 31 36 34 20 2a 2f 20 22 73 65 74 6c   /* 164 */ "setl
29faa 69 73 74 20 3a 3a 3d 20 73 65 74 6c 69 73 74 20  ist ::= setlist 
29fab 43 4f 4d 4d 41 20 6e 6d 20 45 51 20 65 78 70 72  COMMA nm EQ expr
29fac 22 2c 0a 20 2f 2a 20 31 36 35 20 2a 2f 20 22 73  ",. /* 165 */ "s
29fad 65 74 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20 45 51  etlist ::= nm EQ
29fae 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 36 36 20   expr",. /* 166 
29faf 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 69 6e 73 65  */ "cmd ::= inse
29fb0 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c  rt_cmd INTO full
29fb1 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f  name inscollist_
29fb2 6f 70 74 20 56 41 4c 55 45 53 20 4c 50 20 69 74  opt VALUES LP it
29fb3 65 6d 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20  emlist RP",. /* 
29fb4 31 36 37 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  167 */ "cmd ::= 
29fb5 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20  insert_cmd INTO 
29fb6 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c  fullname inscoll
29fb7 69 73 74 5f 6f 70 74 20 73 65 6c 65 63 74 22 2c  ist_opt select",
29fb8 0a 20 2f 2a 20 31 36 38 20 2a 2f 20 22 63 6d 64  . /* 168 */ "cmd
29fb9 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20   ::= insert_cmd 
29fba 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e  INTO fullname in
29fbb 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 44 45 46  scollist_opt DEF
29fbc 41 55 4c 54 20 56 41 4c 55 45 53 22 2c 0a 20 2f  AULT VALUES",. /
29fbd 2a 20 31 36 39 20 2a 2f 20 22 69 6e 73 65 72 74  * 169 */ "insert
29fbe 5f 63 6d 64 20 3a 3a 3d 20 49 4e 53 45 52 54 20  _cmd ::= INSERT 
29fbf 6f 72 63 6f 6e 66 22 2c 0a 20 2f 2a 20 31 37 30  orconf",. /* 170
29fc0 20 2a 2f 20 22 69 6e 73 65 72 74 5f 63 6d 64 20   */ "insert_cmd 
29fc1 3a 3a 3d 20 52 45 50 4c 41 43 45 22 2c 0a 20 2f  ::= REPLACE",. /
29fc2 2a 20 31 37 31 20 2a 2f 20 22 69 74 65 6d 6c 69  * 171 */ "itemli
29fc3 73 74 20 3a 3a 3d 20 69 74 65 6d 6c 69 73 74 20  st ::= itemlist 
29fc4 43 4f 4d 4d 41 20 65 78 70 72 22 2c 0a 20 2f 2a  COMMA expr",. /*
29fc5 20 31 37 32 20 2a 2f 20 22 69 74 65 6d 6c 69 73   172 */ "itemlis
29fc6 74 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20 2f 2a  t ::= expr",. /*
29fc7 20 31 37 33 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c   173 */ "inscoll
29fc8 69 73 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f  ist_opt ::=",. /
29fc9 2a 20 31 37 34 20 2a 2f 20 22 69 6e 73 63 6f 6c  * 174 */ "inscol
29fca 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 4c 50 20  list_opt ::= LP 
29fcb 69 6e 73 63 6f 6c 6c 69 73 74 20 52 50 22 2c 0a  inscollist RP",.
29fcc 20 2f 2a 20 31 37 35 20 2a 2f 20 22 69 6e 73 63   /* 175 */ "insc
29fcd 6f 6c 6c 69 73 74 20 3a 3a 3d 20 69 6e 73 63 6f  ollist ::= insco
29fce 6c 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 22 2c  llist COMMA nm",
29fcf 0a 20 2f 2a 20 31 37 36 20 2a 2f 20 22 69 6e 73  . /* 176 */ "ins
29fd0 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 6e 6d 22 2c  collist ::= nm",
29fd1 0a 20 2f 2a 20 31 37 37 20 2a 2f 20 22 65 78 70  . /* 177 */ "exp
29fd2 72 20 3a 3a 3d 20 74 65 72 6d 22 2c 0a 20 2f 2a  r ::= term",. /*
29fd3 20 31 37 38 20 2a 2f 20 22 65 78 70 72 20 3a 3a   178 */ "expr ::
29fd4 3d 20 4c 50 20 65 78 70 72 20 52 50 22 2c 0a 20  = LP expr RP",. 
29fd5 2f 2a 20 31 37 39 20 2a 2f 20 22 74 65 72 6d 20  /* 179 */ "term 
29fd6 3a 3a 3d 20 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 31  ::= NULL",. /* 1
29fd7 38 30 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  80 */ "expr ::= 
29fd8 49 44 22 2c 0a 20 2f 2a 20 31 38 31 20 2a 2f 20  ID",. /* 181 */ 
29fd9 22 65 78 70 72 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b  "expr ::= JOIN_K
29fda 57 22 2c 0a 20 2f 2a 20 31 38 32 20 2a 2f 20 22  W",. /* 182 */ "
29fdb 65 78 70 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20  expr ::= nm DOT 
29fdc 6e 6d 22 2c 0a 20 2f 2a 20 31 38 33 20 2a 2f 20  nm",. /* 183 */ 
29fdd 22 65 78 70 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54  "expr ::= nm DOT
29fde 20 6e 6d 20 44 4f 54 20 6e 6d 22 2c 0a 20 2f 2a   nm DOT nm",. /*
29fdf 20 31 38 34 20 2a 2f 20 22 74 65 72 6d 20 3a 3a   184 */ "term ::
29fe0 3d 20 49 4e 54 45 47 45 52 7c 46 4c 4f 41 54 7c  = INTEGER|FLOAT|
29fe1 42 4c 4f 42 22 2c 0a 20 2f 2a 20 31 38 35 20 2a  BLOB",. /* 185 *
29fe2 2f 20 22 74 65 72 6d 20 3a 3a 3d 20 53 54 52 49  / "term ::= STRI
29fe3 4e 47 22 2c 0a 20 2f 2a 20 31 38 36 20 2a 2f 20  NG",. /* 186 */ 
29fe4 22 65 78 70 72 20 3a 3a 3d 20 52 45 47 49 53 54  "expr ::= REGIST
29fe5 45 52 22 2c 0a 20 2f 2a 20 31 38 37 20 2a 2f 20  ER",. /* 187 */ 
29fe6 22 65 78 70 72 20 3a 3a 3d 20 56 41 52 49 41 42  "expr ::= VARIAB
29fe7 4c 45 22 2c 0a 20 2f 2a 20 31 38 38 20 2a 2f 20  LE",. /* 188 */ 
29fe8 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 43  "expr ::= expr C
29fe9 4f 4c 4c 41 54 45 20 69 64 73 22 2c 0a 20 2f 2a  OLLATE ids",. /*
29fea 20 31 38 39 20 2a 2f 20 22 65 78 70 72 20 3a 3a   189 */ "expr ::
29feb 3d 20 43 41 53 54 20 4c 50 20 65 78 70 72 20 41  = CAST LP expr A
29fec 53 20 74 79 70 65 74 6f 6b 65 6e 20 52 50 22 2c  S typetoken RP",
29fed 0a 20 2f 2a 20 31 39 30 20 2a 2f 20 22 65 78 70  . /* 190 */ "exp
29fee 72 20 3a 3a 3d 20 49 44 20 4c 50 20 64 69 73 74  r ::= ID LP dist
29fef 69 6e 63 74 20 65 78 70 72 6c 69 73 74 20 52 50  inct exprlist RP
29ff0 22 2c 0a 20 2f 2a 20 31 39 31 20 2a 2f 20 22 65  ",. /* 191 */ "e
29ff1 78 70 72 20 3a 3a 3d 20 49 44 20 4c 50 20 53 54  xpr ::= ID LP ST
29ff2 41 52 20 52 50 22 2c 0a 20 2f 2a 20 31 39 32 20  AR RP",. /* 192 
29ff3 2a 2f 20 22 74 65 72 6d 20 3a 3a 3d 20 43 54 49  */ "term ::= CTI
29ff4 4d 45 5f 4b 57 22 2c 0a 20 2f 2a 20 31 39 33 20  ME_KW",. /* 193 
29ff5 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70  */ "expr ::= exp
29ff6 72 20 41 4e 44 20 65 78 70 72 22 2c 0a 20 2f 2a  r AND expr",. /*
29ff7 20 31 39 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a   194 */ "expr ::
29ff8 3d 20 65 78 70 72 20 4f 52 20 65 78 70 72 22 2c  = expr OR expr",
29ff9 0a 20 2f 2a 20 31 39 35 20 2a 2f 20 22 65 78 70  . /* 195 */ "exp
29ffa 72 20 3a 3a 3d 20 65 78 70 72 20 4c 54 7c 47 54  r ::= expr LT|GT
29ffb 7c 47 45 7c 4c 45 20 65 78 70 72 22 2c 0a 20 2f  |GE|LE expr",. /
29ffc 2a 20 31 39 36 20 2a 2f 20 22 65 78 70 72 20 3a  * 196 */ "expr :
29ffd 3a 3d 20 65 78 70 72 20 45 51 7c 4e 45 20 65 78  := expr EQ|NE ex
29ffe 70 72 22 2c 0a 20 2f 2a 20 31 39 37 20 2a 2f 20  pr",. /* 197 */ 
29fff 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 42  "expr ::= expr B
2a000 49 54 41 4e 44 7c 42 49 54 4f 52 7c 4c 53 48 49  ITAND|BITOR|LSHI
2a001 46 54 7c 52 53 48 49 46 54 20 65 78 70 72 22 2c  FT|RSHIFT expr",
2a002 0a 20 2f 2a 20 31 39 38 20 2a 2f 20 22 65 78 70  . /* 198 */ "exp
2a003 72 20 3a 3a 3d 20 65 78 70 72 20 50 4c 55 53 7c  r ::= expr PLUS|
2a004 4d 49 4e 55 53 20 65 78 70 72 22 2c 0a 20 2f 2a  MINUS expr",. /*
2a005 20 31 39 39 20 2a 2f 20 22 65 78 70 72 20 3a 3a   199 */ "expr ::
2a006 3d 20 65 78 70 72 20 53 54 41 52 7c 53 4c 41 53  = expr STAR|SLAS
2a007 48 7c 52 45 4d 20 65 78 70 72 22 2c 0a 20 2f 2a  H|REM expr",. /*
2a008 20 32 30 30 20 2a 2f 20 22 65 78 70 72 20 3a 3a   200 */ "expr ::
2a009 3d 20 65 78 70 72 20 43 4f 4e 43 41 54 20 65 78  = expr CONCAT ex
2a00a 70 72 22 2c 0a 20 2f 2a 20 32 30 31 20 2a 2f 20  pr",. /* 201 */ 
2a00b 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4c 49 4b 45  "likeop ::= LIKE
2a00c 5f 4b 57 22 2c 0a 20 2f 2a 20 32 30 32 20 2a 2f  _KW",. /* 202 */
2a00d 20 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54   "likeop ::= NOT
2a00e 20 4c 49 4b 45 5f 4b 57 22 2c 0a 20 2f 2a 20 32   LIKE_KW",. /* 2
2a00f 30 33 20 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a 3a  03 */ "likeop ::
2a010 3d 20 4d 41 54 43 48 22 2c 0a 20 2f 2a 20 32 30  = MATCH",. /* 20
2a011 34 20 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a 3a 3d  4 */ "likeop ::=
2a012 20 4e 4f 54 20 4d 41 54 43 48 22 2c 0a 20 2f 2a   NOT MATCH",. /*
2a013 20 32 30 35 20 2a 2f 20 22 65 73 63 61 70 65 20   205 */ "escape 
2a014 3a 3a 3d 20 45 53 43 41 50 45 20 65 78 70 72 22  ::= ESCAPE expr"
2a015 2c 0a 20 2f 2a 20 32 30 36 20 2a 2f 20 22 65 73  ,. /* 206 */ "es
2a016 63 61 70 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32  cape ::=",. /* 2
2a017 30 37 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  07 */ "expr ::= 
2a018 65 78 70 72 20 6c 69 6b 65 6f 70 20 65 78 70 72  expr likeop expr
2a019 20 65 73 63 61 70 65 22 2c 0a 20 2f 2a 20 32 30   escape",. /* 20
2a01a 38 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65  8 */ "expr ::= e
2a01b 78 70 72 20 49 53 4e 55 4c 4c 7c 4e 4f 54 4e 55  xpr ISNULL|NOTNU
2a01c 4c 4c 22 2c 0a 20 2f 2a 20 32 30 39 20 2a 2f 20  LL",. /* 209 */ 
2a01d 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49  "expr ::= expr I
2a01e 53 20 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 32 31 30  S NULL",. /* 210
2a01f 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
2a020 70 72 20 4e 4f 54 20 4e 55 4c 4c 22 2c 0a 20 2f  pr NOT NULL",. /
2a021 2a 20 32 31 31 20 2a 2f 20 22 65 78 70 72 20 3a  * 211 */ "expr :
2a022 3a 3d 20 65 78 70 72 20 49 53 20 4e 4f 54 20 4e  := expr IS NOT N
2a023 55 4c 4c 22 2c 0a 20 2f 2a 20 32 31 32 20 2a 2f  ULL",. /* 212 */
2a024 20 22 65 78 70 72 20 3a 3a 3d 20 4e 4f 54 20 65   "expr ::= NOT e
2a025 78 70 72 22 2c 0a 20 2f 2a 20 32 31 33 20 2a 2f  xpr",. /* 213 */
2a026 20 22 65 78 70 72 20 3a 3a 3d 20 42 49 54 4e 4f   "expr ::= BITNO
2a027 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 31 34  T expr",. /* 214
2a028 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 4d 49   */ "expr ::= MI
2a029 4e 55 53 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32  NUS expr",. /* 2
2a02a 31 35 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  15 */ "expr ::= 
2a02b 50 4c 55 53 20 65 78 70 72 22 2c 0a 20 2f 2a 20  PLUS expr",. /* 
2a02c 32 31 36 20 2a 2f 20 22 62 65 74 77 65 65 6e 5f  216 */ "between_
2a02d 6f 70 20 3a 3a 3d 20 42 45 54 57 45 45 4e 22 2c  op ::= BETWEEN",
2a02e 0a 20 2f 2a 20 32 31 37 20 2a 2f 20 22 62 65 74  . /* 217 */ "bet
2a02f 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20  ween_op ::= NOT 
2a030 42 45 54 57 45 45 4e 22 2c 0a 20 2f 2a 20 32 31  BETWEEN",. /* 21
2a031 38 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65  8 */ "expr ::= e
2a032 78 70 72 20 62 65 74 77 65 65 6e 5f 6f 70 20 65  xpr between_op e
2a033 78 70 72 20 41 4e 44 20 65 78 70 72 22 2c 0a 20  xpr AND expr",. 
2a034 2f 2a 20 32 31 39 20 2a 2f 20 22 69 6e 5f 6f 70  /* 219 */ "in_op
2a035 20 3a 3a 3d 20 49 4e 22 2c 0a 20 2f 2a 20 32 32   ::= IN",. /* 22
2a036 30 20 2a 2f 20 22 69 6e 5f 6f 70 20 3a 3a 3d 20  0 */ "in_op ::= 
2a037 4e 4f 54 20 49 4e 22 2c 0a 20 2f 2a 20 32 32 31  NOT IN",. /* 221
2a038 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
2a039 70 72 20 69 6e 5f 6f 70 20 4c 50 20 65 78 70 72  pr in_op LP expr
2a03a 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 32 32  list RP",. /* 22
2a03b 32 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 4c  2 */ "expr ::= L
2a03c 50 20 73 65 6c 65 63 74 20 52 50 22 2c 0a 20 2f  P select RP",. /
2a03d 2a 20 32 32 33 20 2a 2f 20 22 65 78 70 72 20 3a  * 223 */ "expr :
2a03e 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20 4c 50  := expr in_op LP
2a03f 20 73 65 6c 65 63 74 20 52 50 22 2c 0a 20 2f 2a   select RP",. /*
2a040 20 32 32 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a   224 */ "expr ::
2a041 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20 6e 6d 20  = expr in_op nm 
2a042 64 62 6e 6d 22 2c 0a 20 2f 2a 20 32 32 35 20 2a  dbnm",. /* 225 *
2a043 2f 20 22 65 78 70 72 20 3a 3a 3d 20 45 58 49 53  / "expr ::= EXIS
2a044 54 53 20 4c 50 20 73 65 6c 65 63 74 20 52 50 22  TS LP select RP"
2a045 2c 0a 20 2f 2a 20 32 32 36 20 2a 2f 20 22 65 78  ,. /* 226 */ "ex
2a046 70 72 20 3a 3a 3d 20 43 41 53 45 20 63 61 73 65  pr ::= CASE case
2a047 5f 6f 70 65 72 61 6e 64 20 63 61 73 65 5f 65 78  _operand case_ex
2a048 70 72 6c 69 73 74 20 63 61 73 65 5f 65 6c 73 65  prlist case_else
2a049 20 45 4e 44 22 2c 0a 20 2f 2a 20 32 32 37 20 2a   END",. /* 227 *
2a04a 2f 20 22 63 61 73 65 5f 65 78 70 72 6c 69 73 74  / "case_exprlist
2a04b 20 3a 3a 3d 20 63 61 73 65 5f 65 78 70 72 6c 69   ::= case_exprli
2a04c 73 74 20 57 48 45 4e 20 65 78 70 72 20 54 48 45  st WHEN expr THE
2a04d 4e 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 32 38  N expr",. /* 228
2a04e 20 2a 2f 20 22 63 61 73 65 5f 65 78 70 72 6c 69   */ "case_exprli
2a04f 73 74 20 3a 3a 3d 20 57 48 45 4e 20 65 78 70 72  st ::= WHEN expr
2a050 20 54 48 45 4e 20 65 78 70 72 22 2c 0a 20 2f 2a   THEN expr",. /*
2a051 20 32 32 39 20 2a 2f 20 22 63 61 73 65 5f 65 6c   229 */ "case_el
2a052 73 65 20 3a 3a 3d 20 45 4c 53 45 20 65 78 70 72  se ::= ELSE expr
2a053 22 2c 0a 20 2f 2a 20 32 33 30 20 2a 2f 20 22 63  ",. /* 230 */ "c
2a054 61 73 65 5f 65 6c 73 65 20 3a 3a 3d 22 2c 0a 20  ase_else ::=",. 
2a055 2f 2a 20 32 33 31 20 2a 2f 20 22 63 61 73 65 5f  /* 231 */ "case_
2a056 6f 70 65 72 61 6e 64 20 3a 3a 3d 20 65 78 70 72  operand ::= expr
2a057 22 2c 0a 20 2f 2a 20 32 33 32 20 2a 2f 20 22 63  ",. /* 232 */ "c
2a058 61 73 65 5f 6f 70 65 72 61 6e 64 20 3a 3a 3d 22  ase_operand ::="
2a059 2c 0a 20 2f 2a 20 32 33 33 20 2a 2f 20 22 65 78  ,. /* 233 */ "ex
2a05a 70 72 6c 69 73 74 20 3a 3a 3d 20 6e 65 78 70 72  prlist ::= nexpr
2a05b 6c 69 73 74 22 2c 0a 20 2f 2a 20 32 33 34 20 2a  list",. /* 234 *
2a05c 2f 20 22 65 78 70 72 6c 69 73 74 20 3a 3a 3d 22  / "exprlist ::="
2a05d 2c 0a 20 2f 2a 20 32 33 35 20 2a 2f 20 22 6e 65  ,. /* 235 */ "ne
2a05e 78 70 72 6c 69 73 74 20 3a 3a 3d 20 6e 65 78 70  xprlist ::= nexp
2a05f 72 6c 69 73 74 20 43 4f 4d 4d 41 20 65 78 70 72  rlist COMMA expr
2a060 22 2c 0a 20 2f 2a 20 32 33 36 20 2a 2f 20 22 6e  ",. /* 236 */ "n
2a061 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 65 78 70  exprlist ::= exp
2a062 72 22 2c 0a 20 2f 2a 20 32 33 37 20 2a 2f 20 22  r",. /* 237 */ "
2a063 63 6d 64 20 3a 3a 3d 20 43 52 45 41 54 45 20 75  cmd ::= CREATE u
2a064 6e 69 71 75 65 66 6c 61 67 20 49 4e 44 45 58 20  niqueflag INDEX 
2a065 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20 64  ifnotexists nm d
2a066 62 6e 6d 20 4f 4e 20 6e 6d 20 4c 50 20 69 64 78  bnm ON nm LP idx
2a067 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 32 33  list RP",. /* 23
2a068 38 20 2a 2f 20 22 75 6e 69 71 75 65 66 6c 61 67  8 */ "uniqueflag
2a069 20 3a 3a 3d 20 55 4e 49 51 55 45 22 2c 0a 20 2f   ::= UNIQUE",. /
2a06a 2a 20 32 33 39 20 2a 2f 20 22 75 6e 69 71 75 65  * 239 */ "unique
2a06b 66 6c 61 67 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32  flag ::=",. /* 2
2a06c 34 30 20 2a 2f 20 22 69 64 78 6c 69 73 74 5f 6f  40 */ "idxlist_o
2a06d 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 34 31  pt ::=",. /* 241
2a06e 20 2a 2f 20 22 69 64 78 6c 69 73 74 5f 6f 70 74   */ "idxlist_opt
2a06f 20 3a 3a 3d 20 4c 50 20 69 64 78 6c 69 73 74 20   ::= LP idxlist 
2a070 52 50 22 2c 0a 20 2f 2a 20 32 34 32 20 2a 2f 20  RP",. /* 242 */ 
2a071 22 69 64 78 6c 69 73 74 20 3a 3a 3d 20 69 64 78  "idxlist ::= idx
2a072 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 20 63 6f  list COMMA nm co
2a073 6c 6c 61 74 65 20 73 6f 72 74 6f 72 64 65 72 22  llate sortorder"
2a074 2c 0a 20 2f 2a 20 32 34 33 20 2a 2f 20 22 69 64  ,. /* 243 */ "id
2a075 78 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20 63 6f 6c  xlist ::= nm col
2a076 6c 61 74 65 20 73 6f 72 74 6f 72 64 65 72 22 2c  late sortorder",
2a077 0a 20 2f 2a 20 32 34 34 20 2a 2f 20 22 63 6f 6c  . /* 244 */ "col
2a078 6c 61 74 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32  late ::=",. /* 2
2a079 34 35 20 2a 2f 20 22 63 6f 6c 6c 61 74 65 20 3a  45 */ "collate :
2a07a 3a 3d 20 43 4f 4c 4c 41 54 45 20 69 64 73 22 2c  := COLLATE ids",
2a07b 0a 20 2f 2a 20 32 34 36 20 2a 2f 20 22 63 6d 64  . /* 246 */ "cmd
2a07c 20 3a 3a 3d 20 44 52 4f 50 20 49 4e 44 45 58 20   ::= DROP INDEX 
2a07d 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d  ifexists fullnam
2a07e 65 22 2c 0a 20 2f 2a 20 32 34 37 20 2a 2f 20 22  e",. /* 247 */ "
2a07f 63 6d 64 20 3a 3a 3d 20 56 41 43 55 55 4d 22 2c  cmd ::= VACUUM",
2a080 0a 20 2f 2a 20 32 34 38 20 2a 2f 20 22 63 6d 64  . /* 248 */ "cmd
2a081 20 3a 3a 3d 20 56 41 43 55 55 4d 20 6e 6d 22 2c   ::= VACUUM nm",
2a082 0a 20 2f 2a 20 32 34 39 20 2a 2f 20 22 63 6d 64  . /* 249 */ "cmd
2a083 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64   ::= PRAGMA nm d
2a084 62 6e 6d 20 45 51 20 6e 6d 6e 75 6d 22 2c 0a 20  bnm EQ nmnum",. 
2a085 2f 2a 20 32 35 30 20 2a 2f 20 22 63 6d 64 20 3a  /* 250 */ "cmd :
2a086 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e  := PRAGMA nm dbn
2a087 6d 20 45 51 20 4f 4e 22 2c 0a 20 2f 2a 20 32 35  m EQ ON",. /* 25
2a088 31 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 50 52  1 */ "cmd ::= PR
2a089 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51 20  AGMA nm dbnm EQ 
2a08a 44 45 4c 45 54 45 22 2c 0a 20 2f 2a 20 32 35 32  DELETE",. /* 252
2a08b 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 50 52 41   */ "cmd ::= PRA
2a08c 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51 20 6d  GMA nm dbnm EQ m
2a08d 69 6e 75 73 5f 6e 75 6d 22 2c 0a 20 2f 2a 20 32  inus_num",. /* 2
2a08e 35 33 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 50  53 */ "cmd ::= P
2a08f 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 4c 50  RAGMA nm dbnm LP
2a090 20 6e 6d 6e 75 6d 20 52 50 22 2c 0a 20 2f 2a 20   nmnum RP",. /* 
2a091 32 35 34 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  254 */ "cmd ::= 
2a092 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 22 2c  PRAGMA nm dbnm",
2a093 0a 20 2f 2a 20 32 35 35 20 2a 2f 20 22 6e 6d 6e  . /* 255 */ "nmn
2a094 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d 22  um ::= plus_num"
2a095 2c 0a 20 2f 2a 20 32 35 36 20 2a 2f 20 22 6e 6d  ,. /* 256 */ "nm
2a096 6e 75 6d 20 3a 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a  num ::= nm",. /*
2a097 20 32 35 37 20 2a 2f 20 22 70 6c 75 73 5f 6e 75   257 */ "plus_nu
2a098 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6f 70 74 20 6e  m ::= plus_opt n
2a099 75 6d 62 65 72 22 2c 0a 20 2f 2a 20 32 35 38 20  umber",. /* 258 
2a09a 2a 2f 20 22 6d 69 6e 75 73 5f 6e 75 6d 20 3a 3a  */ "minus_num ::
2a09b 3d 20 4d 49 4e 55 53 20 6e 75 6d 62 65 72 22 2c  = MINUS number",
2a09c 0a 20 2f 2a 20 32 35 39 20 2a 2f 20 22 6e 75 6d  . /* 259 */ "num
2a09d 62 65 72 20 3a 3a 3d 20 49 4e 54 45 47 45 52 7c  ber ::= INTEGER|
2a09e 46 4c 4f 41 54 22 2c 0a 20 2f 2a 20 32 36 30 20  FLOAT",. /* 260 
2a09f 2a 2f 20 22 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d  */ "plus_opt ::=
2a0a0 20 50 4c 55 53 22 2c 0a 20 2f 2a 20 32 36 31 20   PLUS",. /* 261 
2a0a1 2a 2f 20 22 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d  */ "plus_opt ::=
2a0a2 22 2c 0a 20 2f 2a 20 32 36 32 20 2a 2f 20 22 63  ",. /* 262 */ "c
2a0a3 6d 64 20 3a 3a 3d 20 43 52 45 41 54 45 20 74 72  md ::= CREATE tr
2a0a4 69 67 67 65 72 5f 64 65 63 6c 20 42 45 47 49 4e  igger_decl BEGIN
2a0a5 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73   trigger_cmd_lis
2a0a6 74 20 45 4e 44 22 2c 0a 20 2f 2a 20 32 36 33 20  t END",. /* 263 
2a0a7 2a 2f 20 22 74 72 69 67 67 65 72 5f 64 65 63 6c  */ "trigger_decl
2a0a8 20 3a 3a 3d 20 74 65 6d 70 20 54 52 49 47 47 45   ::= temp TRIGGE
2a0a9 52 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d  R ifnotexists nm
2a0aa 20 64 62 6e 6d 20 74 72 69 67 67 65 72 5f 74 69   dbnm trigger_ti
2a0ab 6d 65 20 74 72 69 67 67 65 72 5f 65 76 65 6e 74  me trigger_event
2a0ac 20 4f 4e 20 66 75 6c 6c 6e 61 6d 65 20 66 6f 72   ON fullname for
2a0ad 65 61 63 68 5f 63 6c 61 75 73 65 20 77 68 65 6e  each_clause when
2a0ae 5f 63 6c 61 75 73 65 22 2c 0a 20 2f 2a 20 32 36  _clause",. /* 26
2a0af 34 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 74 69  4 */ "trigger_ti
2a0b0 6d 65 20 3a 3a 3d 20 42 45 46 4f 52 45 22 2c 0a  me ::= BEFORE",.
2a0b1 20 2f 2a 20 32 36 35 20 2a 2f 20 22 74 72 69 67   /* 265 */ "trig
2a0b2 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 41 46 54  ger_time ::= AFT
2a0b3 45 52 22 2c 0a 20 2f 2a 20 32 36 36 20 2a 2f 20  ER",. /* 266 */ 
2a0b4 22 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a  "trigger_time ::
2a0b5 3d 20 49 4e 53 54 45 41 44 20 4f 46 22 2c 0a 20  = INSTEAD OF",. 
2a0b6 2f 2a 20 32 36 37 20 2a 2f 20 22 74 72 69 67 67  /* 267 */ "trigg
2a0b7 65 72 5f 74 69 6d 65 20 3a 3a 3d 22 2c 0a 20 2f  er_time ::=",. /
2a0b8 2a 20 32 36 38 20 2a 2f 20 22 74 72 69 67 67 65  * 268 */ "trigge
2a0b9 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 44 45 4c 45  r_event ::= DELE
2a0ba 54 45 7c 49 4e 53 45 52 54 22 2c 0a 20 2f 2a 20  TE|INSERT",. /* 
2a0bb 32 36 39 20 2a 2f 20 22 74 72 69 67 67 65 72 5f  269 */ "trigger_
2a0bc 65 76 65 6e 74 20 3a 3a 3d 20 55 50 44 41 54 45  event ::= UPDATE
2a0bd 22 2c 0a 20 2f 2a 20 32 37 30 20 2a 2f 20 22 74  ",. /* 270 */ "t
2a0be 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d  rigger_event ::=
2a0bf 20 55 50 44 41 54 45 20 4f 46 20 69 6e 73 63 6f   UPDATE OF insco
2a0c0 6c 6c 69 73 74 22 2c 0a 20 2f 2a 20 32 37 31 20  llist",. /* 271 
2a0c1 2a 2f 20 22 66 6f 72 65 61 63 68 5f 63 6c 61 75  */ "foreach_clau
2a0c2 73 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 37 32  se ::=",. /* 272
2a0c3 20 2a 2f 20 22 66 6f 72 65 61 63 68 5f 63 6c 61   */ "foreach_cla
2a0c4 75 73 65 20 3a 3a 3d 20 46 4f 52 20 45 41 43 48  use ::= FOR EACH
2a0c5 20 52 4f 57 22 2c 0a 20 2f 2a 20 32 37 33 20 2a   ROW",. /* 273 *
2a0c6 2f 20 22 77 68 65 6e 5f 63 6c 61 75 73 65 20 3a  / "when_clause :
2a0c7 3a 3d 22 2c 0a 20 2f 2a 20 32 37 34 20 2a 2f 20  :=",. /* 274 */ 
2a0c8 22 77 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d  "when_clause ::=
2a0c9 20 57 48 45 4e 20 65 78 70 72 22 2c 0a 20 2f 2a   WHEN expr",. /*
2a0ca 20 32 37 35 20 2a 2f 20 22 74 72 69 67 67 65 72   275 */ "trigger
2a0cb 5f 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20 74 72  _cmd_list ::= tr
2a0cc 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 74  igger_cmd_list t
2a0cd 72 69 67 67 65 72 5f 63 6d 64 20 53 45 4d 49 22  rigger_cmd SEMI"
2a0ce 2c 0a 20 2f 2a 20 32 37 36 20 2a 2f 20 22 74 72  ,. /* 276 */ "tr
2a0cf 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 3a  igger_cmd_list :
2a0d0 3a 3d 20 74 72 69 67 67 65 72 5f 63 6d 64 20 53  := trigger_cmd S
2a0d1 45 4d 49 22 2c 0a 20 2f 2a 20 32 37 37 20 2a 2f  EMI",. /* 277 */
2a0d2 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a   "trigger_cmd ::
2a0d3 3d 20 55 50 44 41 54 45 20 6f 72 63 6f 6e 66 20  = UPDATE orconf 
2a0d4 6e 6d 20 53 45 54 20 73 65 74 6c 69 73 74 20 77  nm SET setlist w
2a0d5 68 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 32  here_opt",. /* 2
2a0d6 37 38 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63  78 */ "trigger_c
2a0d7 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d  md ::= insert_cm
2a0d8 64 20 49 4e 54 4f 20 6e 6d 20 69 6e 73 63 6f 6c  d INTO nm inscol
2a0d9 6c 69 73 74 5f 6f 70 74 20 56 41 4c 55 45 53 20  list_opt VALUES 
2a0da 4c 50 20 69 74 65 6d 6c 69 73 74 20 52 50 22 2c  LP itemlist RP",
2a0db 0a 20 2f 2a 20 32 37 39 20 2a 2f 20 22 74 72 69  . /* 279 */ "tri
2a0dc 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73  gger_cmd ::= ins
2a0dd 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 6e 6d 20  ert_cmd INTO nm 
2a0de 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 73  inscollist_opt s
2a0df 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 32 38 30 20  elect",. /* 280 
2a0e0 2a 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20  */ "trigger_cmd 
2a0e1 3a 3a 3d 20 44 45 4c 45 54 45 20 46 52 4f 4d 20  ::= DELETE FROM 
2a0e2 6e 6d 20 77 68 65 72 65 5f 6f 70 74 22 2c 0a 20  nm where_opt",. 
2a0e3 2f 2a 20 32 38 31 20 2a 2f 20 22 74 72 69 67 67  /* 281 */ "trigg
2a0e4 65 72 5f 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63  er_cmd ::= selec
2a0e5 74 22 2c 0a 20 2f 2a 20 32 38 32 20 2a 2f 20 22  t",. /* 282 */ "
2a0e6 65 78 70 72 20 3a 3a 3d 20 52 41 49 53 45 20 4c  expr ::= RAISE L
2a0e7 50 20 49 47 4e 4f 52 45 20 52 50 22 2c 0a 20 2f  P IGNORE RP",. /
2a0e8 2a 20 32 38 33 20 2a 2f 20 22 65 78 70 72 20 3a  * 283 */ "expr :
2a0e9 3a 3d 20 52 41 49 53 45 20 4c 50 20 72 61 69 73  := RAISE LP rais
2a0ea 65 74 79 70 65 20 43 4f 4d 4d 41 20 6e 6d 20 52  etype COMMA nm R
2a0eb 50 22 2c 0a 20 2f 2a 20 32 38 34 20 2a 2f 20 22  P",. /* 284 */ "
2a0ec 72 61 69 73 65 74 79 70 65 20 3a 3a 3d 20 52 4f  raisetype ::= RO
2a0ed 4c 4c 42 41 43 4b 22 2c 0a 20 2f 2a 20 32 38 35  LLBACK",. /* 285
2a0ee 20 2a 2f 20 22 72 61 69 73 65 74 79 70 65 20 3a   */ "raisetype :
2a0ef 3a 3d 20 41 42 4f 52 54 22 2c 0a 20 2f 2a 20 32  := ABORT",. /* 2
2a0f0 38 36 20 2a 2f 20 22 72 61 69 73 65 74 79 70 65  86 */ "raisetype
2a0f1 20 3a 3a 3d 20 46 41 49 4c 22 2c 0a 20 2f 2a 20   ::= FAIL",. /* 
2a0f2 32 38 37 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  287 */ "cmd ::= 
2a0f3 44 52 4f 50 20 54 52 49 47 47 45 52 20 69 66 65  DROP TRIGGER ife
2a0f4 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 22 2c  xists fullname",
2a0f5 0a 20 2f 2a 20 32 38 38 20 2a 2f 20 22 63 6d 64  . /* 288 */ "cmd
2a0f6 20 3a 3a 3d 20 41 54 54 41 43 48 20 64 61 74 61   ::= ATTACH data
2a0f7 62 61 73 65 5f 6b 77 5f 6f 70 74 20 65 78 70 72  base_kw_opt expr
2a0f8 20 41 53 20 65 78 70 72 20 6b 65 79 5f 6f 70 74   AS expr key_opt
2a0f9 22 2c 0a 20 2f 2a 20 32 38 39 20 2a 2f 20 22 63  ",. /* 289 */ "c
2a0fa 6d 64 20 3a 3a 3d 20 44 45 54 41 43 48 20 64 61  md ::= DETACH da
2a0fb 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20 65 78  tabase_kw_opt ex
2a0fc 70 72 22 2c 0a 20 2f 2a 20 32 39 30 20 2a 2f 20  pr",. /* 290 */ 
2a0fd 22 6b 65 79 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  "key_opt ::=",. 
2a0fe 2f 2a 20 32 39 31 20 2a 2f 20 22 6b 65 79 5f 6f  /* 291 */ "key_o
2a0ff 70 74 20 3a 3a 3d 20 4b 45 59 20 65 78 70 72 22  pt ::= KEY expr"
2a100 2c 0a 20 2f 2a 20 32 39 32 20 2a 2f 20 22 64 61  ,. /* 292 */ "da
2a101 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a  tabase_kw_opt ::
2a102 3d 20 44 41 54 41 42 41 53 45 22 2c 0a 20 2f 2a  = DATABASE",. /*
2a103 20 32 39 33 20 2a 2f 20 22 64 61 74 61 62 61 73   293 */ "databas
2a104 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  e_kw_opt ::=",. 
2a105 2f 2a 20 32 39 34 20 2a 2f 20 22 63 6d 64 20 3a  /* 294 */ "cmd :
2a106 3a 3d 20 52 45 49 4e 44 45 58 22 2c 0a 20 2f 2a  := REINDEX",. /*
2a107 20 32 39 35 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   295 */ "cmd ::=
2a108 20 52 45 49 4e 44 45 58 20 6e 6d 20 64 62 6e 6d   REINDEX nm dbnm
2a109 22 2c 0a 20 2f 2a 20 32 39 36 20 2a 2f 20 22 63  ",. /* 296 */ "c
2a10a 6d 64 20 3a 3a 3d 20 41 4e 41 4c 59 5a 45 22 2c  md ::= ANALYZE",
2a10b 0a 20 2f 2a 20 32 39 37 20 2a 2f 20 22 63 6d 64  . /* 297 */ "cmd
2a10c 20 3a 3a 3d 20 41 4e 41 4c 59 5a 45 20 6e 6d 20   ::= ANALYZE nm 
2a10d 64 62 6e 6d 22 2c 0a 20 2f 2a 20 32 39 38 20 2a  dbnm",. /* 298 *
2a10e 2f 20 22 63 6d 64 20 3a 3a 3d 20 41 4c 54 45 52  / "cmd ::= ALTER
2a10f 20 54 41 42 4c 45 20 66 75 6c 6c 6e 61 6d 65 20   TABLE fullname 
2a110 52 45 4e 41 4d 45 20 54 4f 20 6e 6d 22 2c 0a 20  RENAME TO nm",. 
2a111 2f 2a 20 32 39 39 20 2a 2f 20 22 63 6d 64 20 3a  /* 299 */ "cmd :
2a112 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c 45 20 61  := ALTER TABLE a
2a113 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61  dd_column_fullna
2a114 6d 65 20 41 44 44 20 6b 77 63 6f 6c 75 6d 6e 5f  me ADD kwcolumn_
2a115 6f 70 74 20 63 6f 6c 75 6d 6e 22 2c 0a 20 2f 2a  opt column",. /*
2a116 20 33 30 30 20 2a 2f 20 22 61 64 64 5f 63 6f 6c   300 */ "add_col
2a117 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d  umn_fullname ::=
2a118 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20   fullname",. /* 
2a119 33 30 31 20 2a 2f 20 22 6b 77 63 6f 6c 75 6d 6e  301 */ "kwcolumn
2a11a 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33  _opt ::=",. /* 3
2a11b 30 32 20 2a 2f 20 22 6b 77 63 6f 6c 75 6d 6e 5f  02 */ "kwcolumn_
2a11c 6f 70 74 20 3a 3a 3d 20 43 4f 4c 55 4d 4e 4b 57  opt ::= COLUMNKW
2a11d 22 2c 0a 20 2f 2a 20 33 30 33 20 2a 2f 20 22 63  ",. /* 303 */ "c
2a11e 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 76 74  md ::= create_vt
2a11f 61 62 22 2c 0a 20 2f 2a 20 33 30 34 20 2a 2f 20  ab",. /* 304 */ 
2a120 22 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f  "cmd ::= create_
2a121 76 74 61 62 20 4c 50 20 76 74 61 62 61 72 67 6c  vtab LP vtabargl
2a122 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 33 30 35  ist RP",. /* 305
2a123 20 2a 2f 20 22 63 72 65 61 74 65 5f 76 74 61 62   */ "create_vtab
2a124 20 3a 3a 3d 20 43 52 45 41 54 45 20 56 49 52 54   ::= CREATE VIRT
2a125 55 41 4c 20 54 41 42 4c 45 20 6e 6d 20 64 62 6e  UAL TABLE nm dbn
2a126 6d 20 55 53 49 4e 47 20 6e 6d 22 2c 0a 20 2f 2a  m USING nm",. /*
2a127 20 33 30 36 20 2a 2f 20 22 76 74 61 62 61 72 67   306 */ "vtabarg
2a128 6c 69 73 74 20 3a 3a 3d 20 76 74 61 62 61 72 67  list ::= vtabarg
2a129 22 2c 0a 20 2f 2a 20 33 30 37 20 2a 2f 20 22 76  ",. /* 307 */ "v
2a12a 74 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20 76  tabarglist ::= v
2a12b 74 61 62 61 72 67 6c 69 73 74 20 43 4f 4d 4d 41  tabarglist COMMA
2a12c 20 76 74 61 62 61 72 67 22 2c 0a 20 2f 2a 20 33   vtabarg",. /* 3
2a12d 30 38 20 2a 2f 20 22 76 74 61 62 61 72 67 20 3a  08 */ "vtabarg :
2a12e 3a 3d 22 2c 0a 20 2f 2a 20 33 30 39 20 2a 2f 20  :=",. /* 309 */ 
2a12f 22 76 74 61 62 61 72 67 20 3a 3a 3d 20 76 74 61  "vtabarg ::= vta
2a130 62 61 72 67 20 76 74 61 62 61 72 67 74 6f 6b 65  barg vtabargtoke
2a131 6e 22 2c 0a 20 2f 2a 20 33 31 30 20 2a 2f 20 22  n",. /* 310 */ "
2a132 76 74 61 62 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d  vtabargtoken ::=
2a133 20 41 4e 59 22 2c 0a 20 2f 2a 20 33 31 31 20 2a   ANY",. /* 311 *
2a134 2f 20 22 76 74 61 62 61 72 67 74 6f 6b 65 6e 20  / "vtabargtoken 
2a135 3a 3a 3d 20 6c 70 20 61 6e 79 6c 69 73 74 20 52  ::= lp anylist R
2a136 50 22 2c 0a 20 2f 2a 20 33 31 32 20 2a 2f 20 22  P",. /* 312 */ "
2a137 6c 70 20 3a 3a 3d 20 4c 50 22 2c 0a 20 2f 2a 20  lp ::= LP",. /* 
2a138 33 31 33 20 2a 2f 20 22 61 6e 79 6c 69 73 74 20  313 */ "anylist 
2a139 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 31 34 20 2a 2f  ::=",. /* 314 */
2a13a 20 22 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61 6e   "anylist ::= an
2a13b 79 6c 69 73 74 20 41 4e 59 22 2c 0a 7d 3b 0a 23  ylist ANY",.};.#
2a13c 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20  endif /* NDEBUG 
2a13d 2a 2f 0a 0a 0a 23 69 66 20 59 59 53 54 41 43 4b  */...#if YYSTACK
2a13e 44 45 50 54 48 3c 3d 30 0a 2f 2a 0a 2a 2a 20 54  DEPTH<=0./*.** T
2a13f 72 79 20 74 6f 20 69 6e 63 72 65 61 73 65 20 74  ry to increase t
2a140 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70  he size of the p
2a141 61 72 73 65 72 20 73 74 61 63 6b 2e 0a 2a 2f 0a  arser stack..*/.
2a142 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 47 72  static void yyGr
2a143 6f 77 53 74 61 63 6b 28 79 79 50 61 72 73 65 72  owStack(yyParser
2a144 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 65 77 53   *p){.  int newS
2a145 69 7a 65 3b 0a 20 20 79 79 53 74 61 63 6b 45 6e  ize;.  yyStackEn
2a146 74 72 79 20 2a 70 4e 65 77 3b 0a 0a 20 20 6e 65  try *pNew;..  ne
2a147 77 53 69 7a 65 20 3d 20 70 2d 3e 79 79 73 74 6b  wSize = p->yystk
2a148 73 7a 2a 32 20 2b 20 31 30 30 3b 0a 20 20 70 4e  sz*2 + 100;.  pN
2a149 65 77 20 3d 20 72 65 61 6c 6c 6f 63 28 70 2d 3e  ew = realloc(p->
2a14a 79 79 73 74 61 63 6b 2c 20 6e 65 77 53 69 7a 65  yystack, newSize
2a14b 2a 73 69 7a 65 6f 66 28 70 4e 65 77 5b 30 5d 29  *sizeof(pNew[0])
2a14c 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b  );.  if( pNew ){
2a14d 0a 20 20 20 20 70 2d 3e 79 79 73 74 61 63 6b 20  .    p->yystack 
2a14e 3d 20 70 4e 65 77 3b 0a 20 20 20 20 70 2d 3e 79  = pNew;.    p->y
2a14f 79 73 74 6b 73 7a 20 3d 20 6e 65 77 53 69 7a 65  ystksz = newSize
2a150 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ;.#ifndef NDEBUG
2a151 0a 20 20 20 20 69 66 28 20 79 79 54 72 61 63 65  .    if( yyTrace
2a152 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 20 66 70  FILE ){.      fp
2a153 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
2a154 45 2c 22 25 73 53 74 61 63 6b 20 67 72 6f 77 73  E,"%sStack grows
2a155 20 74 6f 20 25 64 20 65 6e 74 72 69 65 73 21 5c   to %d entries!\
2a156 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
2a157 20 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c    yyTracePrompt,
2a158 20 70 2d 3e 79 79 73 74 6b 73 7a 29 3b 0a 20 20   p->yystksz);.  
2a159 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d    }.#endif.  }.}
2a15a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 0a 2a 2a 20  .#endif../* .** 
2a15b 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c  This function al
2a15c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 20 70 61  locates a new pa
2a15d 72 73 65 72 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c  rser..** The onl
2a15e 79 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20  y argument is a 
2a15f 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e  pointer to a fun
2a160 63 74 69 6f 6e 20 77 68 69 63 68 20 77 6f 72 6b  ction which work
2a161 73 20 6c 69 6b 65 0a 2a 2a 20 6d 61 6c 6c 6f 63  s like.** malloc
2a162 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a  ..**.** Inputs:.
2a163 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** A pointer to 
2a164 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 75 73 65  the function use
2a165 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65  d to allocate me
2a166 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70  mory..**.** Outp
2a167 75 74 73 3a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65  uts:.** A pointe
2a168 72 20 74 6f 20 61 20 70 61 72 73 65 72 2e 20 20  r to a parser.  
2a169 54 68 69 73 20 70 6f 69 6e 74 65 72 20 69 73 20  This pointer is 
2a16a 75 73 65 64 20 69 6e 20 73 75 62 73 65 71 75 65  used in subseque
2a16b 6e 74 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 73  nt calls.** to s
2a16c 71 6c 69 74 65 33 50 61 72 73 65 72 20 61 6e 64  qlite3Parser and
2a16d 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 46 72   sqlite3ParserFr
2a16e 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ee..*/.SQLITE_PR
2a16f 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
2a170 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 28 76  te3ParserAlloc(v
2a171 6f 69 64 20 2a 28 2a 6d 61 6c 6c 6f 63 50 72 6f  oid *(*mallocPro
2a172 63 29 28 73 69 7a 65 5f 74 29 29 7b 0a 20 20 79  c)(size_t)){.  y
2a173 79 50 61 72 73 65 72 20 2a 70 50 61 72 73 65 72  yParser *pParser
2a174 3b 0a 20 20 70 50 61 72 73 65 72 20 3d 20 28 79  ;.  pParser = (y
2a175 79 50 61 72 73 65 72 2a 29 28 2a 6d 61 6c 6c 6f  yParser*)(*mallo
2a176 63 50 72 6f 63 29 28 20 28 73 69 7a 65 5f 74 29  cProc)( (size_t)
2a177 73 69 7a 65 6f 66 28 79 79 50 61 72 73 65 72 29  sizeof(yyParser)
2a178 20 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65   );.  if( pParse
2a179 72 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 72  r ){.    pParser
2a17a 2d 3e 79 79 69 64 78 20 3d 20 2d 31 3b 0a 23 69  ->yyidx = -1;.#i
2a17b 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53  fdef YYTRACKMAXS
2a17c 54 41 43 4b 44 45 50 54 48 0a 20 20 20 20 70 50  TACKDEPTH.    pP
2a17d 61 72 73 65 72 2d 3e 79 79 69 64 78 4d 61 78 20  arser->yyidxMax 
2a17e 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20  = 0;.#endif.#if 
2a17f 59 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a  YYSTACKDEPTH<=0.
2a180 20 20 20 20 79 79 47 72 6f 77 53 74 61 63 6b 28      yyGrowStack(
2a181 70 50 61 72 73 65 72 29 3b 0a 23 65 6e 64 69 66  pParser);.#endif
2a182 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 50  .  }.  return pP
2a183 61 72 73 65 72 3b 0a 7d 0a 0a 2f 2a 20 54 68 65  arser;.}../* The
2a184 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74   following funct
2a185 69 6f 6e 20 64 65 6c 65 74 65 73 20 74 68 65 20  ion deletes the 
2a186 76 61 6c 75 65 20 61 73 73 6f 63 69 61 74 65 64  value associated
2a187 20 77 69 74 68 20 61 0a 2a 2a 20 73 79 6d 62 6f   with a.** symbo
2a188 6c 2e 20 20 54 68 65 20 73 79 6d 62 6f 6c 20 63  l.  The symbol c
2a189 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 20 74  an be either a t
2a18a 65 72 6d 69 6e 61 6c 20 6f 72 20 6e 6f 6e 74 65  erminal or nonte
2a18b 72 6d 69 6e 61 6c 2e 0a 2a 2a 20 22 79 79 6d 61  rminal..** "yyma
2a18c 6a 6f 72 22 20 69 73 20 74 68 65 20 73 79 6d 62  jor" is the symb
2a18d 6f 6c 20 63 6f 64 65 2c 20 61 6e 64 20 22 79 79  ol code, and "yy
2a18e 70 6d 69 6e 6f 72 22 20 69 73 20 61 20 70 6f 69  pminor" is a poi
2a18f 6e 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 76  nter to.** the v
2a190 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  alue..*/.static 
2a191 76 6f 69 64 20 79 79 5f 64 65 73 74 72 75 63 74  void yy_destruct
2a192 6f 72 28 0a 20 20 79 79 50 61 72 73 65 72 20 2a  or(.  yyParser *
2a193 79 79 70 50 61 72 73 65 72 2c 20 20 20 20 2f 2a  yypParser,    /*
2a194 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20   The parser */. 
2a195 20 59 59 43 4f 44 45 54 59 50 45 20 79 79 6d 61   YYCODETYPE yyma
2a196 6a 6f 72 2c 20 20 20 20 20 2f 2a 20 54 79 70 65  jor,     /* Type
2a197 20 63 6f 64 65 20 66 6f 72 20 6f 62 6a 65 63 74   code for object
2a198 20 74 6f 20 64 65 73 74 72 6f 79 20 2a 2f 0a 20   to destroy */. 
2a199 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 2a 79 79   YYMINORTYPE *yy
2a19a 70 6d 69 6e 6f 72 20 20 20 2f 2a 20 54 68 65 20  pminor   /* The 
2a19b 6f 62 6a 65 63 74 20 74 6f 20 62 65 20 64 65 73  object to be des
2a19c 74 72 6f 79 65 64 20 2a 2f 0a 29 7b 0a 20 20 73  troyed */.){.  s
2a19d 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f  qlite3ParserARG_
2a19e 46 45 54 43 48 3b 0a 20 20 73 77 69 74 63 68 28  FETCH;.  switch(
2a19f 20 79 79 6d 61 6a 6f 72 20 29 7b 0a 20 20 20 20   yymajor ){.    
2a1a0 2f 2a 20 48 65 72 65 20 69 73 20 69 6e 73 65 72  /* Here is inser
2a1a1 74 65 64 20 74 68 65 20 61 63 74 69 6f 6e 73 20  ted the actions 
2a1a2 77 68 69 63 68 20 74 61 6b 65 20 70 6c 61 63 65  which take place
2a1a3 20 77 68 65 6e 20 61 0a 20 20 20 20 2a 2a 20 74   when a.    ** t
2a1a4 65 72 6d 69 6e 61 6c 20 6f 72 20 6e 6f 6e 2d 74  erminal or non-t
2a1a5 65 72 6d 69 6e 61 6c 20 69 73 20 64 65 73 74 72  erminal is destr
2a1a6 6f 79 65 64 2e 20 20 54 68 69 73 20 63 61 6e 20  oyed.  This can 
2a1a7 68 61 70 70 65 6e 0a 20 20 20 20 2a 2a 20 77 68  happen.    ** wh
2a1a8 65 6e 20 74 68 65 20 73 79 6d 62 6f 6c 20 69 73  en the symbol is
2a1a9 20 70 6f 70 70 65 64 20 66 72 6f 6d 20 74 68 65   popped from the
2a1aa 20 73 74 61 63 6b 20 64 75 72 69 6e 67 20 61 0a   stack during a.
2a1ab 20 20 20 20 2a 2a 20 72 65 64 75 63 65 20 6f 72      ** reduce or
2a1ac 20 64 75 72 69 6e 67 20 65 72 72 6f 72 20 70 72   during error pr
2a1ad 6f 63 65 73 73 69 6e 67 20 6f 72 20 77 68 65 6e  ocessing or when
2a1ae 20 61 20 70 61 72 73 65 72 20 69 73 20 0a 20 20   a parser is .  
2a1af 20 20 2a 2a 20 62 65 69 6e 67 20 64 65 73 74 72    ** being destr
2a1b0 6f 79 65 64 20 62 65 66 6f 72 65 20 69 74 20 69  oyed before it i
2a1b1 73 20 66 69 6e 69 73 68 65 64 20 70 61 72 73 69  s finished parsi
2a1b2 6e 67 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ng..    **.    *
2a1b3 2a 20 4e 6f 74 65 3a 20 64 75 72 69 6e 67 20 61  * Note: during a
2a1b4 20 72 65 64 75 63 65 2c 20 74 68 65 20 6f 6e 6c   reduce, the onl
2a1b5 79 20 73 79 6d 62 6f 6c 73 20 64 65 73 74 72 6f  y symbols destro
2a1b6 79 65 64 20 61 72 65 20 74 68 6f 73 65 0a 20 20  yed are those.  
2a1b7 20 20 2a 2a 20 77 68 69 63 68 20 61 70 70 65 61    ** which appea
2a1b8 72 20 6f 6e 20 74 68 65 20 52 48 53 20 6f 66 20  r on the RHS of 
2a1b9 74 68 65 20 72 75 6c 65 2c 20 62 75 74 20 77 68  the rule, but wh
2a1ba 69 63 68 20 61 72 65 20 6e 6f 74 20 75 73 65 64  ich are not used
2a1bb 0a 20 20 20 20 2a 2a 20 69 6e 73 69 64 65 20 74  .    ** inside t
2a1bc 68 65 20 43 20 63 6f 64 65 2e 0a 20 20 20 20 2a  he C code..    *
2a1bd 2f 0a 20 20 20 20 63 61 73 65 20 31 35 36 3a 20  /.    case 156: 
2a1be 2f 2a 20 73 65 6c 65 63 74 20 2a 2f 0a 20 20 20  /* select */.   
2a1bf 20 63 61 73 65 20 31 39 30 3a 20 2f 2a 20 6f 6e   case 190: /* on
2a1c0 65 73 65 6c 65 63 74 20 2a 2f 0a 20 20 20 20 63  eselect */.    c
2a1c1 61 73 65 20 32 30 38 3a 20 2f 2a 20 73 65 6c 74  ase 208: /* selt
2a1c2 61 62 6c 69 73 74 5f 70 61 72 65 6e 20 2a 2f 0a  ablist_paren */.
2a1c3 7b 0a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  {.sqlite3SelectD
2a1c4 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
2a1c5 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 34  , (yypminor->yy4
2a1c6 33 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65  3));.}.      bre
2a1c7 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 31 37 30  ak;.    case 170
2a1c8 3a 20 2f 2a 20 74 65 72 6d 20 2a 2f 0a 20 20 20  : /* term */.   
2a1c9 20 63 61 73 65 20 31 37 31 3a 20 2f 2a 20 65 78   case 171: /* ex
2a1ca 70 72 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31  pr */.    case 1
2a1cb 39 35 3a 20 2f 2a 20 77 68 65 72 65 5f 6f 70 74  95: /* where_opt
2a1cc 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 39 37   */.    case 197
2a1cd 3a 20 2f 2a 20 68 61 76 69 6e 67 5f 6f 70 74 20  : /* having_opt 
2a1ce 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 30 36 3a  */.    case 206:
2a1cf 20 2f 2a 20 6f 6e 5f 6f 70 74 20 2a 2f 0a 20 20   /* on_opt */.  
2a1d0 20 20 63 61 73 65 20 32 31 32 3a 20 2f 2a 20 73    case 212: /* s
2a1d1 6f 72 74 69 74 65 6d 20 2a 2f 0a 20 20 20 20 63  ortitem */.    c
2a1d2 61 73 65 20 32 32 30 3a 20 2f 2a 20 65 73 63 61  ase 220: /* esca
2a1d3 70 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32  pe */.    case 2
2a1d4 32 33 3a 20 2f 2a 20 63 61 73 65 5f 6f 70 65 72  23: /* case_oper
2a1d5 61 6e 64 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  and */.    case 
2a1d6 32 32 35 3a 20 2f 2a 20 63 61 73 65 5f 65 6c 73  225: /* case_els
2a1d7 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 33  e */.    case 23
2a1d8 36 3a 20 2f 2a 20 77 68 65 6e 5f 63 6c 61 75 73  6: /* when_claus
2a1d9 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 33  e */.    case 23
2a1da 39 3a 20 2f 2a 20 6b 65 79 5f 6f 70 74 20 2a 2f  9: /* key_opt */
2a1db 0a 7b 0a 73 71 6c 69 74 65 33 45 78 70 72 44 65  .{.sqlite3ExprDe
2a1dc 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
2a1dd 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 34 35   (yypminor->yy45
2a1de 30 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65  0));.}.      bre
2a1df 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 31 37 35  ak;.    case 175
2a1e0 3a 20 2f 2a 20 69 64 78 6c 69 73 74 5f 6f 70 74  : /* idxlist_opt
2a1e1 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 38 33   */.    case 183
2a1e2 3a 20 2f 2a 20 69 64 78 6c 69 73 74 20 2a 2f 0a  : /* idxlist */.
2a1e3 20 20 20 20 63 61 73 65 20 31 39 33 3a 20 2f 2a      case 193: /*
2a1e4 20 73 65 6c 63 6f 6c 6c 69 73 74 20 2a 2f 0a 20   selcollist */. 
2a1e5 20 20 20 63 61 73 65 20 31 39 36 3a 20 2f 2a 20     case 196: /* 
2a1e6 67 72 6f 75 70 62 79 5f 6f 70 74 20 2a 2f 0a 20  groupby_opt */. 
2a1e7 20 20 20 63 61 73 65 20 31 39 38 3a 20 2f 2a 20     case 198: /* 
2a1e8 6f 72 64 65 72 62 79 5f 6f 70 74 20 2a 2f 0a 20  orderby_opt */. 
2a1e9 20 20 20 63 61 73 65 20 32 30 30 3a 20 2f 2a 20     case 200: /* 
2a1ea 73 63 6c 70 20 2a 2f 0a 20 20 20 20 63 61 73 65  sclp */.    case
2a1eb 20 32 31 31 3a 20 2f 2a 20 73 6f 72 74 6c 69 73   211: /* sortlis
2a1ec 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 31  t */.    case 21
2a1ed 33 3a 20 2f 2a 20 6e 65 78 70 72 6c 69 73 74 20  3: /* nexprlist 
2a1ee 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 31 34 3a  */.    case 214:
2a1ef 20 2f 2a 20 73 65 74 6c 69 73 74 20 2a 2f 0a 20   /* setlist */. 
2a1f0 20 20 20 63 61 73 65 20 32 31 37 3a 20 2f 2a 20     case 217: /* 
2a1f1 69 74 65 6d 6c 69 73 74 20 2a 2f 0a 20 20 20 20  itemlist */.    
2a1f2 63 61 73 65 20 32 31 38 3a 20 2f 2a 20 65 78 70  case 218: /* exp
2a1f3 72 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73  rlist */.    cas
2a1f4 65 20 32 32 34 3a 20 2f 2a 20 63 61 73 65 5f 65  e 224: /* case_e
2a1f5 78 70 72 6c 69 73 74 20 2a 2f 0a 7b 0a 73 71 6c  xprlist */.{.sql
2a1f6 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
2a1f7 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28  te(pParse->db, (
2a1f8 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 32 34 32 29  yypminor->yy242)
2a1f9 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b  );.}.      break
2a1fa 3b 0a 20 20 20 20 63 61 73 65 20 31 38 39 3a 20  ;.    case 189: 
2a1fb 2f 2a 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 20  /* fullname */. 
2a1fc 20 20 20 63 61 73 65 20 31 39 34 3a 20 2f 2a 20     case 194: /* 
2a1fd 66 72 6f 6d 20 2a 2f 0a 20 20 20 20 63 61 73 65  from */.    case
2a1fe 20 32 30 32 3a 20 2f 2a 20 73 65 6c 74 61 62 6c   202: /* seltabl
2a1ff 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  ist */.    case 
2a200 32 30 33 3a 20 2f 2a 20 73 74 6c 5f 70 72 65 66  203: /* stl_pref
2a201 69 78 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 53  ix */.{.sqlite3S
2a202 72 63 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61  rcListDelete(pPa
2a203 72 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e  rse->db, (yypmin
2a204 6f 72 2d 3e 79 79 34 31 39 29 29 3b 0a 7d 0a 20  or->yy419));.}. 
2a205 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2a206 63 61 73 65 20 32 30 37 3a 20 2f 2a 20 75 73 69  case 207: /* usi
2a207 6e 67 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61  ng_opt */.    ca
2a208 73 65 20 32 31 30 3a 20 2f 2a 20 69 6e 73 63 6f  se 210: /* insco
2a209 6c 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73  llist */.    cas
2a20a 65 20 32 31 36 3a 20 2f 2a 20 69 6e 73 63 6f 6c  e 216: /* inscol
2a20b 6c 69 73 74 5f 6f 70 74 20 2a 2f 0a 7b 0a 73 71  list_opt */.{.sq
2a20c 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74  lite3IdListDelet
2a20d 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28 79  e(pParse->db, (y
2a20e 79 70 6d 69 6e 6f 72 2d 3e 79 79 33 35 32 29 29  ypminor->yy352))
2a20f 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.}.      break;
2a210 0a 20 20 20 20 63 61 73 65 20 32 33 32 3a 20 2f  .    case 232: /
2a211 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69  * trigger_cmd_li
2a212 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32  st */.    case 2
2a213 33 37 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63  37: /* trigger_c
2a214 6d 64 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 44  md */.{.sqlite3D
2a215 65 6c 65 74 65 54 72 69 67 67 65 72 53 74 65 70  eleteTriggerStep
2a216 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28 79 79  (pParse->db, (yy
2a217 70 6d 69 6e 6f 72 2d 3e 79 79 37 35 29 29 3b 0a  pminor->yy75));.
2a218 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
2a219 20 20 20 63 61 73 65 20 32 33 34 3a 20 2f 2a 20     case 234: /* 
2a21a 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20 2a 2f  trigger_event */
2a21b 0a 7b 0a 73 71 6c 69 74 65 33 49 64 4c 69 73 74  .{.sqlite3IdList
2a21c 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64  Delete(pParse->d
2a21d 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79  b, (yypminor->yy
2a21e 33 35 34 29 2e 62 29 3b 0a 7d 0a 20 20 20 20 20  354).b);.}.     
2a21f 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61   break;.    defa
2a220 75 6c 74 3a 20 20 62 72 65 61 6b 3b 20 20 20 2f  ult:  break;   /
2a221 2a 20 49 66 20 6e 6f 20 64 65 73 74 72 75 63 74  * If no destruct
2a222 6f 72 20 61 63 74 69 6f 6e 20 73 70 65 63 69 66  or action specif
2a223 69 65 64 3a 20 64 6f 20 6e 6f 74 68 69 6e 67 20  ied: do nothing 
2a224 2a 2f 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  */.  }.}../*.** 
2a225 50 6f 70 20 74 68 65 20 70 61 72 73 65 72 27 73  Pop the parser's
2a226 20 73 74 61 63 6b 20 6f 6e 63 65 2e 0a 2a 2a 0a   stack once..**.
2a227 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61  ** If there is a
2a228 20 64 65 73 74 72 75 63 74 6f 72 20 72 6f 75 74   destructor rout
2a229 69 6e 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  ine associated w
2a22a 69 74 68 20 74 68 65 20 74 6f 6b 65 6e 20 77 68  ith the token wh
2a22b 69 63 68 0a 2a 2a 20 69 73 20 70 6f 70 70 65 64  ich.** is popped
2a22c 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63 6b 2c   from the stack,
2a22d 20 74 68 65 6e 20 63 61 6c 6c 20 69 74 2e 0a 2a   then call it..*
2a22e 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
2a22f 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62  major token numb
2a230 65 72 20 66 6f 72 20 74 68 65 20 73 79 6d 62 6f  er for the symbo
2a231 6c 20 70 6f 70 70 65 64 2e 0a 2a 2f 0a 73 74 61  l popped..*/.sta
2a232 74 69 63 20 69 6e 74 20 79 79 5f 70 6f 70 5f 70  tic int yy_pop_p
2a233 61 72 73 65 72 5f 73 74 61 63 6b 28 79 79 50 61  arser_stack(yyPa
2a234 72 73 65 72 20 2a 70 50 61 72 73 65 72 29 7b 0a  rser *pParser){.
2a235 20 20 59 59 43 4f 44 45 54 59 50 45 20 79 79 6d    YYCODETYPE yym
2a236 61 6a 6f 72 3b 0a 20 20 79 79 53 74 61 63 6b 45  ajor;.  yyStackE
2a237 6e 74 72 79 20 2a 79 79 74 6f 73 20 3d 20 26 70  ntry *yytos = &p
2a238 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b  Parser->yystack[
2a239 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 3b  pParser->yyidx];
2a23a 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 72 2d  ..  if( pParser-
2a23b 3e 79 79 69 64 78 3c 30 20 29 20 72 65 74 75 72  >yyidx<0 ) retur
2a23c 6e 20 30 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45  n 0;.#ifndef NDE
2a23d 42 55 47 0a 20 20 69 66 28 20 79 79 54 72 61 63  BUG.  if( yyTrac
2a23e 65 46 49 4c 45 20 26 26 20 70 50 61 72 73 65 72  eFILE && pParser
2a23f 2d 3e 79 79 69 64 78 3e 3d 30 20 29 7b 0a 20 20  ->yyidx>=0 ){.  
2a240 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63    fprintf(yyTrac
2a241 65 46 49 4c 45 2c 22 25 73 50 6f 70 70 69 6e 67  eFILE,"%sPopping
2a242 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 79 79   %s\n",.      yy
2a243 54 72 61 63 65 50 72 6f 6d 70 74 2c 0a 20 20 20  TracePrompt,.   
2a244 20 20 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79     yyTokenName[y
2a245 79 74 6f 73 2d 3e 6d 61 6a 6f 72 5d 29 3b 0a 20  ytos->major]);. 
2a246 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79 79 6d 61   }.#endif.  yyma
2a247 6a 6f 72 20 3d 20 79 79 74 6f 73 2d 3e 6d 61 6a  jor = yytos->maj
2a248 6f 72 3b 0a 20 20 79 79 5f 64 65 73 74 72 75 63  or;.  yy_destruc
2a249 74 6f 72 28 70 50 61 72 73 65 72 2c 20 79 79 6d  tor(pParser, yym
2a24a 61 6a 6f 72 2c 20 26 79 79 74 6f 73 2d 3e 6d 69  ajor, &yytos->mi
2a24b 6e 6f 72 29 3b 0a 20 20 70 50 61 72 73 65 72 2d  nor);.  pParser-
2a24c 3e 79 79 69 64 78 2d 2d 3b 0a 20 20 72 65 74 75  >yyidx--;.  retu
2a24d 72 6e 20 79 79 6d 61 6a 6f 72 3b 0a 7d 0a 0a 2f  rn yymajor;.}../
2a24e 2a 20 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65  * .** Deallocate
2a24f 20 61 6e 64 20 64 65 73 74 72 6f 79 20 61 20 70   and destroy a p
2a250 61 72 73 65 72 2e 20 20 44 65 73 74 72 75 63 74  arser.  Destruct
2a251 6f 72 73 20 61 72 65 20 61 6c 6c 20 63 61 6c 6c  ors are all call
2a252 65 64 20 66 6f 72 0a 2a 2a 20 61 6c 6c 20 73 74  ed for.** all st
2a253 61 63 6b 20 65 6c 65 6d 65 6e 74 73 20 62 65 66  ack elements bef
2a254 6f 72 65 20 73 68 75 74 74 69 6e 67 20 74 68 65  ore shutting the
2a255 20 70 61 72 73 65 72 20 64 6f 77 6e 2e 0a 2a 2a   parser down..**
2a256 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a 2a 20 3c  .** Inputs:.** <
2a257 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 41 20 70  ul>.** <li>  A p
2a258 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61  ointer to the pa
2a259 72 73 65 72 2e 20 20 54 68 69 73 20 73 68 6f 75  rser.  This shou
2a25a 6c 64 20 62 65 20 61 20 70 6f 69 6e 74 65 72 0a  ld be a pointer.
2a25b 2a 2a 20 20 20 20 20 20 20 6f 62 74 61 69 6e 65  **       obtaine
2a25c 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 50 61  d from sqlite3Pa
2a25d 72 73 65 72 41 6c 6c 6f 63 2e 0a 2a 2a 20 3c 6c  rserAlloc..** <l
2a25e 69 3e 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  i>  A pointer to
2a25f 20 61 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64   a function used
2a260 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f   to reclaim memo
2a261 72 79 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 20  ry obtained.**  
2a262 20 20 20 20 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63       from malloc
2a263 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2f 0a 53 51  ..** </ul>.*/.SQ
2a264 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2a265 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 46  d sqlite3ParserF
2a266 72 65 65 28 0a 20 20 76 6f 69 64 20 2a 70 2c 20  ree(.  void *p, 
2a267 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a268 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
2a269 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 2a   to be deleted *
2a26a 2f 0a 20 20 76 6f 69 64 20 28 2a 66 72 65 65 50  /.  void (*freeP
2a26b 72 6f 63 29 28 76 6f 69 64 2a 29 20 20 20 20 20  roc)(void*)     
2a26c 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 75 73 65 64  /* Function used
2a26d 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f   to reclaim memo
2a26e 72 79 20 2a 2f 0a 29 7b 0a 20 20 79 79 50 61 72  ry */.){.  yyPar
2a26f 73 65 72 20 2a 70 50 61 72 73 65 72 20 3d 20 28  ser *pParser = (
2a270 79 79 50 61 72 73 65 72 2a 29 70 3b 0a 20 20 69  yyParser*)p;.  i
2a271 66 28 20 70 50 61 72 73 65 72 3d 3d 30 20 29 20  f( pParser==0 ) 
2a272 72 65 74 75 72 6e 3b 0a 20 20 77 68 69 6c 65 28  return;.  while(
2a273 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e   pParser->yyidx>
2a274 3d 30 20 29 20 79 79 5f 70 6f 70 5f 70 61 72 73  =0 ) yy_pop_pars
2a275 65 72 5f 73 74 61 63 6b 28 70 50 61 72 73 65 72  er_stack(pParser
2a276 29 3b 0a 23 69 66 20 59 59 53 54 41 43 4b 44 45  );.#if YYSTACKDE
2a277 50 54 48 3c 3d 30 0a 20 20 66 72 65 65 28 70 50  PTH<=0.  free(pP
2a278 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 29 3b  arser->yystack);
2a279 0a 23 65 6e 64 69 66 0a 20 20 28 2a 66 72 65 65  .#endif.  (*free
2a27a 50 72 6f 63 29 28 28 76 6f 69 64 2a 29 70 50 61  Proc)((void*)pPa
2a27b 72 73 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rser);.}../*.** 
2a27c 52 65 74 75 72 6e 20 74 68 65 20 70 65 61 6b 20  Return the peak 
2a27d 64 65 70 74 68 20 6f 66 20 74 68 65 20 73 74 61  depth of the sta
2a27e 63 6b 20 66 6f 72 20 61 20 70 61 72 73 65 72 2e  ck for a parser.
2a27f 0a 2a 2f 0a 23 69 66 64 65 66 20 59 59 54 52 41  .*/.#ifdef YYTRA
2a280 43 4b 4d 41 58 53 54 41 43 4b 44 45 50 54 48 0a  CKMAXSTACKDEPTH.
2a281 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2a282 6e 74 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  nt sqlite3Parser
2a283 53 74 61 63 6b 50 65 61 6b 28 76 6f 69 64 20 2a  StackPeak(void *
2a284 70 29 7b 0a 20 20 79 79 50 61 72 73 65 72 20 2a  p){.  yyParser *
2a285 70 50 61 72 73 65 72 20 3d 20 28 79 79 50 61 72  pParser = (yyPar
2a286 73 65 72 2a 29 70 3b 0a 20 20 72 65 74 75 72 6e  ser*)p;.  return
2a287 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 4d   pParser->yyidxM
2a288 61 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ax;.}.#endif../*
2a289 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 61 70 70  .** Find the app
2a28a 72 6f 70 72 69 61 74 65 20 61 63 74 69 6f 6e 20  ropriate action 
2a28b 66 6f 72 20 61 20 70 61 72 73 65 72 20 67 69 76  for a parser giv
2a28c 65 6e 20 74 68 65 20 74 65 72 6d 69 6e 61 6c 0a  en the terminal.
2a28d 2a 2a 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f  ** look-ahead to
2a28e 6b 65 6e 20 69 4c 6f 6f 6b 41 68 65 61 64 2e 0a  ken iLookAhead..
2a28f 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 6f  **.** If the loo
2a290 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 69 73  k-ahead token is
2a291 20 59 59 4e 4f 43 4f 44 45 2c 20 74 68 65 6e 20   YYNOCODE, then 
2a292 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  check to see if 
2a293 74 68 65 20 61 63 74 69 6f 6e 20 69 73 0a 2a 2a  the action is.**
2a294 20 69 6e 64 65 70 65 6e 64 65 6e 74 20 6f 66 20   independent of 
2a295 74 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 2e 20  the look-ahead. 
2a296 20 49 66 20 69 74 20 69 73 2c 20 72 65 74 75 72   If it is, retur
2a297 6e 20 74 68 65 20 61 63 74 69 6f 6e 2c 20 6f 74  n the action, ot
2a298 68 65 72 77 69 73 65 0a 2a 2a 20 72 65 74 75 72  herwise.** retur
2a299 6e 20 59 59 5f 4e 4f 5f 41 43 54 49 4f 4e 2e 0a  n YY_NO_ACTION..
2a29a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 79 79  */.static int yy
2a29b 5f 66 69 6e 64 5f 73 68 69 66 74 5f 61 63 74 69  _find_shift_acti
2a29c 6f 6e 28 0a 20 20 79 79 50 61 72 73 65 72 20 2a  on(.  yyParser *
2a29d 70 50 61 72 73 65 72 2c 20 20 20 20 20 20 20 20  pParser,        
2a29e 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f  /* The parser */
2a29f 0a 20 20 59 59 43 4f 44 45 54 59 50 45 20 69 4c  .  YYCODETYPE iL
2a2a0 6f 6f 6b 41 68 65 61 64 20 20 20 20 20 2f 2a 20  ookAhead     /* 
2a2a1 54 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74  The look-ahead t
2a2a2 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  oken */.){.  int
2a2a3 20 69 3b 0a 20 20 69 6e 74 20 73 74 61 74 65 6e   i;.  int staten
2a2a4 6f 20 3d 20 70 50 61 72 73 65 72 2d 3e 79 79 73  o = pParser->yys
2a2a5 74 61 63 6b 5b 70 50 61 72 73 65 72 2d 3e 79 79  tack[pParser->yy
2a2a6 69 64 78 5d 2e 73 74 61 74 65 6e 6f 3b 0a 20 0a  idx].stateno;. .
2a2a7 20 20 69 66 28 20 73 74 61 74 65 6e 6f 3e 59 59    if( stateno>YY
2a2a8 5f 53 48 49 46 54 5f 4d 41 58 20 7c 7c 20 28 69  _SHIFT_MAX || (i
2a2a9 20 3d 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74   = yy_shift_ofst
2a2aa 5b 73 74 61 74 65 6e 6f 5d 29 3d 3d 59 59 5f 53  [stateno])==YY_S
2a2ab 48 49 46 54 5f 55 53 45 5f 44 46 4c 54 20 29 7b  HIFT_USE_DFLT ){
2a2ac 0a 20 20 20 20 72 65 74 75 72 6e 20 79 79 5f 64  .    return yy_d
2a2ad 65 66 61 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b  efault[stateno];
2a2ae 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69  .  }.  assert( i
2a2af 4c 6f 6f 6b 41 68 65 61 64 21 3d 59 59 4e 4f 43  LookAhead!=YYNOC
2a2b0 4f 44 45 20 29 3b 0a 20 20 69 20 2b 3d 20 69 4c  ODE );.  i += iL
2a2b1 6f 6f 6b 41 68 65 61 64 3b 0a 20 20 69 66 28 20  ookAhead;.  if( 
2a2b2 69 3c 30 20 7c 7c 20 69 3e 3d 59 59 5f 53 5a 5f  i<0 || i>=YY_SZ_
2a2b3 41 43 54 54 41 42 20 7c 7c 20 79 79 5f 6c 6f 6f  ACTTAB || yy_loo
2a2b4 6b 61 68 65 61 64 5b 69 5d 21 3d 69 4c 6f 6f 6b  kahead[i]!=iLook
2a2b5 41 68 65 61 64 20 29 7b 0a 20 20 20 20 69 66 28  Ahead ){.    if(
2a2b6 20 69 4c 6f 6f 6b 41 68 65 61 64 3e 30 20 29 7b   iLookAhead>0 ){
2a2b7 0a 23 69 66 64 65 66 20 59 59 46 41 4c 4c 42 41  .#ifdef YYFALLBA
2a2b8 43 4b 0a 20 20 20 20 20 20 69 6e 74 20 69 46 61  CK.      int iFa
2a2b9 6c 6c 62 61 63 6b 3b 20 20 20 20 20 20 20 20 20  llback;         
2a2ba 20 20 20 2f 2a 20 46 61 6c 6c 62 61 63 6b 20 74     /* Fallback t
2a2bb 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20 69 66  oken */.      if
2a2bc 28 20 69 4c 6f 6f 6b 41 68 65 61 64 3c 73 69 7a  ( iLookAhead<siz
2a2bd 65 6f 66 28 79 79 46 61 6c 6c 62 61 63 6b 29 2f  eof(yyFallback)/
2a2be 73 69 7a 65 6f 66 28 79 79 46 61 6c 6c 62 61 63  sizeof(yyFallbac
2a2bf 6b 5b 30 5d 29 0a 20 20 20 20 20 20 20 20 20 20  k[0]).          
2a2c0 20 20 20 26 26 20 28 69 46 61 6c 6c 62 61 63 6b     && (iFallback
2a2c1 20 3d 20 79 79 46 61 6c 6c 62 61 63 6b 5b 69 4c   = yyFallback[iL
2a2c2 6f 6f 6b 41 68 65 61 64 5d 29 21 3d 30 20 29 7b  ookAhead])!=0 ){
2a2c3 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
2a2c4 20 20 20 20 20 20 20 20 69 66 28 20 79 79 54 72          if( yyTr
2a2c5 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 20  aceFILE ){.     
2a2c6 20 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54       fprintf(yyT
2a2c7 72 61 63 65 46 49 4c 45 2c 20 22 25 73 46 41 4c  raceFILE, "%sFAL
2a2c8 4c 42 41 43 4b 20 25 73 20 3d 3e 20 25 73 5c 6e  LBACK %s => %s\n
2a2c9 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
2a2ca 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 20 79  yyTracePrompt, y
2a2cb 79 54 6f 6b 65 6e 4e 61 6d 65 5b 69 4c 6f 6f 6b  yTokenName[iLook
2a2cc 41 68 65 61 64 5d 2c 20 79 79 54 6f 6b 65 6e 4e  Ahead], yyTokenN
2a2cd 61 6d 65 5b 69 46 61 6c 6c 62 61 63 6b 5d 29 3b  ame[iFallback]);
2a2ce 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  .        }.#endi
2a2cf 66 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  f.        return
2a2d0 20 79 79 5f 66 69 6e 64 5f 73 68 69 66 74 5f 61   yy_find_shift_a
2a2d1 63 74 69 6f 6e 28 70 50 61 72 73 65 72 2c 20 69  ction(pParser, i
2a2d2 46 61 6c 6c 62 61 63 6b 29 3b 0a 20 20 20 20 20  Fallback);.     
2a2d3 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66   }.#endif.#ifdef
2a2d4 20 59 59 57 49 4c 44 43 41 52 44 0a 20 20 20 20   YYWILDCARD.    
2a2d5 20 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20    {.        int 
2a2d6 6a 20 3d 20 69 20 2d 20 69 4c 6f 6f 6b 41 68 65  j = i - iLookAhe
2a2d7 61 64 20 2b 20 59 59 57 49 4c 44 43 41 52 44 3b  ad + YYWILDCARD;
2a2d8 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a 3e 3d  .        if( j>=
2a2d9 30 20 26 26 20 6a 3c 59 59 5f 53 5a 5f 41 43 54  0 && j<YY_SZ_ACT
2a2da 54 41 42 20 26 26 20 79 79 5f 6c 6f 6f 6b 61 68  TAB && yy_lookah
2a2db 65 61 64 5b 6a 5d 3d 3d 59 59 57 49 4c 44 43 41  ead[j]==YYWILDCA
2a2dc 52 44 20 29 7b 0a 23 69 66 6e 64 65 66 20 4e 44  RD ){.#ifndef ND
2a2dd 45 42 55 47 0a 20 20 20 20 20 20 20 20 20 20 69  EBUG.          i
2a2de 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 29  f( yyTraceFILE )
2a2df 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 70  {.            fp
2a2e0 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
2a2e1 45 2c 20 22 25 73 57 49 4c 44 43 41 52 44 20 25  E, "%sWILDCARD %
2a2e2 73 20 3d 3e 20 25 73 5c 6e 22 2c 0a 20 20 20 20  s => %s\n",.    
2a2e3 20 20 20 20 20 20 20 20 20 20 20 79 79 54 72 61             yyTra
2a2e4 63 65 50 72 6f 6d 70 74 2c 20 79 79 54 6f 6b 65  cePrompt, yyToke
2a2e5 6e 4e 61 6d 65 5b 69 4c 6f 6f 6b 41 68 65 61 64  nName[iLookAhead
2a2e6 5d 2c 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 59  ], yyTokenName[Y
2a2e7 59 57 49 4c 44 43 41 52 44 5d 29 3b 0a 20 20 20  YWILDCARD]);.   
2a2e8 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 20         }.#endif 
2a2e9 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 20 20 20  /* NDEBUG */.   
2a2ea 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 79 79         return yy
2a2eb 5f 61 63 74 69 6f 6e 5b 6a 5d 3b 0a 20 20 20 20  _action[j];.    
2a2ec 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65      }.      }.#e
2a2ed 6e 64 69 66 20 2f 2a 20 59 59 57 49 4c 44 43 41  ndif /* YYWILDCA
2a2ee 52 44 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20  RD */.    }.    
2a2ef 72 65 74 75 72 6e 20 79 79 5f 64 65 66 61 75 6c  return yy_defaul
2a2f0 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20 7d 65  t[stateno];.  }e
2a2f1 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
2a2f2 79 79 5f 61 63 74 69 6f 6e 5b 69 5d 3b 0a 20 20  yy_action[i];.  
2a2f3 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20  }.}../*.** Find 
2a2f4 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
2a2f5 61 63 74 69 6f 6e 20 66 6f 72 20 61 20 70 61 72  action for a par
2a2f6 73 65 72 20 67 69 76 65 6e 20 74 68 65 20 6e 6f  ser given the no
2a2f7 6e 2d 74 65 72 6d 69 6e 61 6c 0a 2a 2a 20 6c 6f  n-terminal.** lo
2a2f8 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 69  ok-ahead token i
2a2f9 4c 6f 6f 6b 41 68 65 61 64 2e 0a 2a 2a 0a 2a 2a  LookAhead..**.**
2a2fa 20 49 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68 65   If the look-ahe
2a2fb 61 64 20 74 6f 6b 65 6e 20 69 73 20 59 59 4e 4f  ad token is YYNO
2a2fc 43 4f 44 45 2c 20 74 68 65 6e 20 63 68 65 63 6b  CODE, then check
2a2fd 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 61   to see if the a
2a2fe 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 69 6e 64 65  ction is.** inde
2a2ff 70 65 6e 64 65 6e 74 20 6f 66 20 74 68 65 20 6c  pendent of the l
2a300 6f 6f 6b 2d 61 68 65 61 64 2e 20 20 49 66 20 69  ook-ahead.  If i
2a301 74 20 69 73 2c 20 72 65 74 75 72 6e 20 74 68 65  t is, return the
2a302 20 61 63 74 69 6f 6e 2c 20 6f 74 68 65 72 77 69   action, otherwi
2a303 73 65 0a 2a 2a 20 72 65 74 75 72 6e 20 59 59 5f  se.** return YY_
2a304 4e 4f 5f 41 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74  NO_ACTION..*/.st
2a305 61 74 69 63 20 69 6e 74 20 79 79 5f 66 69 6e 64  atic int yy_find
2a306 5f 72 65 64 75 63 65 5f 61 63 74 69 6f 6e 28 0a  _reduce_action(.
2a307 20 20 69 6e 74 20 73 74 61 74 65 6e 6f 2c 20 20    int stateno,  
2a308 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
2a309 75 72 72 65 6e 74 20 73 74 61 74 65 20 6e 75 6d  urrent state num
2a30a 62 65 72 20 2a 2f 0a 20 20 59 59 43 4f 44 45 54  ber */.  YYCODET
2a30b 59 50 45 20 69 4c 6f 6f 6b 41 68 65 61 64 20 20  YPE iLookAhead  
2a30c 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b 2d 61     /* The look-a
2a30d 68 65 61 64 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b  head token */.){
2a30e 0a 20 20 69 6e 74 20 69 3b 0a 23 69 66 64 65 66  .  int i;.#ifdef
2a30f 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20   YYERRORSYMBOL. 
2a310 20 69 66 28 20 73 74 61 74 65 6e 6f 3e 59 59 5f   if( stateno>YY_
2a311 52 45 44 55 43 45 5f 4d 41 58 20 29 7b 0a 20 20  REDUCE_MAX ){.  
2a312 20 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66 61    return yy_defa
2a313 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20  ult[stateno];.  
2a314 7d 0a 23 65 6c 73 65 0a 20 20 61 73 73 65 72 74  }.#else.  assert
2a315 28 20 73 74 61 74 65 6e 6f 3c 3d 59 59 5f 52 45  ( stateno<=YY_RE
2a316 44 55 43 45 5f 4d 41 58 20 29 3b 0a 23 65 6e 64  DUCE_MAX );.#end
2a317 69 66 0a 20 20 69 20 3d 20 79 79 5f 72 65 64 75  if.  i = yy_redu
2a318 63 65 5f 6f 66 73 74 5b 73 74 61 74 65 6e 6f 5d  ce_ofst[stateno]
2a319 3b 0a 20 20 61 73 73 65 72 74 28 20 69 21 3d 59  ;.  assert( i!=Y
2a31a 59 5f 52 45 44 55 43 45 5f 55 53 45 5f 44 46 4c  Y_REDUCE_USE_DFL
2a31b 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  T );.  assert( i
2a31c 4c 6f 6f 6b 41 68 65 61 64 21 3d 59 59 4e 4f 43  LookAhead!=YYNOC
2a31d 4f 44 45 20 29 3b 0a 20 20 69 20 2b 3d 20 69 4c  ODE );.  i += iL
2a31e 6f 6f 6b 41 68 65 61 64 3b 0a 23 69 66 64 65 66  ookAhead;.#ifdef
2a31f 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20   YYERRORSYMBOL. 
2a320 20 69 66 28 20 69 3c 30 20 7c 7c 20 69 3e 3d 59   if( i<0 || i>=Y
2a321 59 5f 53 5a 5f 41 43 54 54 41 42 20 7c 7c 20 79  Y_SZ_ACTTAB || y
2a322 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 69 5d 21 3d  y_lookahead[i]!=
2a323 69 4c 6f 6f 6b 41 68 65 61 64 20 29 7b 0a 20 20  iLookAhead ){.  
2a324 20 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66 61    return yy_defa
2a325 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20  ult[stateno];.  
2a326 7d 0a 23 65 6c 73 65 0a 20 20 61 73 73 65 72 74  }.#else.  assert
2a327 28 20 69 3e 3d 30 20 26 26 20 69 3c 59 59 5f 53  ( i>=0 && i<YY_S
2a328 5a 5f 41 43 54 54 41 42 20 29 3b 0a 20 20 61 73  Z_ACTTAB );.  as
2a329 73 65 72 74 28 20 79 79 5f 6c 6f 6f 6b 61 68 65  sert( yy_lookahe
2a32a 61 64 5b 69 5d 3d 3d 69 4c 6f 6f 6b 41 68 65 61  ad[i]==iLookAhea
2a32b 64 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65  d );.#endif.  re
2a32c 74 75 72 6e 20 79 79 5f 61 63 74 69 6f 6e 5b 69  turn yy_action[i
2a32d 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ];.}../*.** The 
2a32e 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e  following routin
2a32f 65 20 69 73 20 63 61 6c 6c 65 64 20 69 66 20 74  e is called if t
2a330 68 65 20 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f  he stack overflo
2a331 77 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ws..*/.static vo
2a332 69 64 20 79 79 53 74 61 63 6b 4f 76 65 72 66 6c  id yyStackOverfl
2a333 6f 77 28 79 79 50 61 72 73 65 72 20 2a 79 79 70  ow(yyParser *yyp
2a334 50 61 72 73 65 72 2c 20 59 59 4d 49 4e 4f 52 54  Parser, YYMINORT
2a335 59 50 45 20 2a 79 79 70 4d 69 6e 6f 72 29 7b 0a  YPE *yypMinor){.
2a336 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72     sqlite3Parser
2a337 41 52 47 5f 46 45 54 43 48 3b 0a 20 20 20 79 79  ARG_FETCH;.   yy
2a338 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 2d 2d  pParser->yyidx--
2a339 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ;.#ifndef NDEBUG
2a33a 0a 20 20 20 69 66 28 20 79 79 54 72 61 63 65 46  .   if( yyTraceF
2a33b 49 4c 45 20 29 7b 0a 20 20 20 20 20 66 70 72 69  ILE ){.     fpri
2a33c 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
2a33d 22 25 73 53 74 61 63 6b 20 4f 76 65 72 66 6c 6f  "%sStack Overflo
2a33e 77 21 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f  w!\n",yyTracePro
2a33f 6d 70 74 29 3b 0a 20 20 20 7d 0a 23 65 6e 64 69  mpt);.   }.#endi
2a340 66 0a 20 20 20 77 68 69 6c 65 28 20 79 79 70 50  f.   while( yypP
2a341 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20  arser->yyidx>=0 
2a342 29 20 79 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f  ) yy_pop_parser_
2a343 73 74 61 63 6b 28 79 79 70 50 61 72 73 65 72 29  stack(yypParser)
2a344 3b 0a 20 20 20 2f 2a 20 48 65 72 65 20 63 6f 64  ;.   /* Here cod
2a345 65 20 69 73 20 69 6e 73 65 72 74 65 64 20 77 68  e is inserted wh
2a346 69 63 68 20 77 69 6c 6c 20 65 78 65 63 75 74 65  ich will execute
2a347 20 69 66 20 74 68 65 20 70 61 72 73 65 72 0a 20   if the parser. 
2a348 20 20 2a 2a 20 73 74 61 63 6b 20 65 76 65 72 79    ** stack every
2a349 20 6f 76 65 72 66 6c 6f 77 73 20 2a 2f 0a 0a 20   overflows */.. 
2a34a 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2a34b 28 70 50 61 72 73 65 2c 20 22 70 61 72 73 65 72  (pParse, "parser
2a34c 20 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77 22   stack overflow"
2a34d 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 61 72  );.  pParse->par
2a34e 73 65 45 72 72 6f 72 20 3d 20 31 3b 0a 20 20 20  seError = 1;.   
2a34f 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
2a350 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70 72  _STORE; /* Suppr
2a351 65 73 73 20 77 61 72 6e 69 6e 67 20 61 62 6f 75  ess warning abou
2a352 74 20 75 6e 75 73 65 64 20 25 65 78 74 72 61 5f  t unused %extra_
2a353 61 72 67 75 6d 65 6e 74 20 76 61 72 20 2a 2f 0a  argument var */.
2a354 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d  }../*.** Perform
2a355 20 61 20 73 68 69 66 74 20 61 63 74 69 6f 6e 2e   a shift action.
2a356 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2a357 79 79 5f 73 68 69 66 74 28 0a 20 20 79 79 50 61  yy_shift(.  yyPa
2a358 72 73 65 72 20 2a 79 79 70 50 61 72 73 65 72 2c  rser *yypParser,
2a359 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2a35a 20 70 61 72 73 65 72 20 74 6f 20 62 65 20 73 68   parser to be sh
2a35b 69 66 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 79  ifted */.  int y
2a35c 79 4e 65 77 53 74 61 74 65 2c 20 20 20 20 20 20  yNewState,      
2a35d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2a35e 6e 65 77 20 73 74 61 74 65 20 74 6f 20 73 68 69  new state to shi
2a35f 66 74 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 79  ft in */.  int y
2a360 79 4d 61 6a 6f 72 2c 20 20 20 20 20 20 20 20 20  yMajor,         
2a361 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2a362 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 74 6f 20 73  major token to s
2a363 68 69 66 74 20 69 6e 20 2a 2f 0a 20 20 59 59 4d  hift in */.  YYM
2a364 49 4e 4f 52 54 59 50 45 20 2a 79 79 70 4d 69 6e  INORTYPE *yypMin
2a365 6f 72 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f  or         /* Po
2a366 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 69 6e  inter to the min
2a367 6f 72 20 74 6f 6b 65 6e 20 74 6f 20 73 68 69 66  or token to shif
2a368 74 20 69 6e 20 2a 2f 0a 29 7b 0a 20 20 79 79 53  t in */.){.  yyS
2a369 74 61 63 6b 45 6e 74 72 79 20 2a 79 79 74 6f 73  tackEntry *yytos
2a36a 3b 0a 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79  ;.  yypParser->y
2a36b 79 69 64 78 2b 2b 3b 0a 23 69 66 64 65 66 20 59  yidx++;.#ifdef Y
2a36c 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45  YTRACKMAXSTACKDE
2a36d 50 54 48 0a 20 20 69 66 28 20 79 79 70 50 61 72  PTH.  if( yypPar
2a36e 73 65 72 2d 3e 79 79 69 64 78 3e 79 79 70 50 61  ser->yyidx>yypPa
2a36f 72 73 65 72 2d 3e 79 79 69 64 78 4d 61 78 20 29  rser->yyidxMax )
2a370 7b 0a 20 20 20 20 79 79 70 50 61 72 73 65 72 2d  {.    yypParser-
2a371 3e 79 79 69 64 78 4d 61 78 20 3d 20 79 79 70 50  >yyidxMax = yypP
2a372 61 72 73 65 72 2d 3e 79 79 69 64 78 3b 0a 20 20  arser->yyidx;.  
2a373 7d 0a 23 65 6e 64 69 66 0a 23 69 66 20 59 59 53  }.#endif.#if YYS
2a374 54 41 43 4b 44 45 50 54 48 3e 30 20 0a 20 20 69  TACKDEPTH>0 .  i
2a375 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  f( yypParser->yy
2a376 69 64 78 3e 3d 59 59 53 54 41 43 4b 44 45 50 54  idx>=YYSTACKDEPT
2a377 48 20 29 7b 0a 20 20 20 20 79 79 53 74 61 63 6b  H ){.    yyStack
2a378 4f 76 65 72 66 6c 6f 77 28 79 79 70 50 61 72 73  Overflow(yypPars
2a379 65 72 2c 20 79 79 70 4d 69 6e 6f 72 29 3b 0a 20  er, yypMinor);. 
2a37a 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23     return;.  }.#
2a37b 65 6c 73 65 0a 20 20 69 66 28 20 79 79 70 50 61  else.  if( yypPa
2a37c 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 79 79 70  rser->yyidx>=yyp
2a37d 50 61 72 73 65 72 2d 3e 79 79 73 74 6b 73 7a 20  Parser->yystksz 
2a37e 29 7b 0a 20 20 20 20 79 79 47 72 6f 77 53 74 61  ){.    yyGrowSta
2a37f 63 6b 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20  ck(yypParser);. 
2a380 20 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72     if( yypParser
2a381 2d 3e 79 79 69 64 78 3e 3d 79 79 70 50 61 72 73  ->yyidx>=yypPars
2a382 65 72 2d 3e 79 79 73 74 6b 73 7a 20 29 7b 0a 20  er->yystksz ){. 
2a383 20 20 20 20 20 79 79 53 74 61 63 6b 4f 76 65 72       yyStackOver
2a384 66 6c 6f 77 28 79 79 70 50 61 72 73 65 72 2c 20  flow(yypParser, 
2a385 79 79 70 4d 69 6e 6f 72 29 3b 0a 20 20 20 20 20  yypMinor);.     
2a386 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
2a387 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79 79 74 6f   }.#endif.  yyto
2a388 73 20 3d 20 26 79 79 70 50 61 72 73 65 72 2d 3e  s = &yypParser->
2a389 79 79 73 74 61 63 6b 5b 79 79 70 50 61 72 73 65  yystack[yypParse
2a38a 72 2d 3e 79 79 69 64 78 5d 3b 0a 20 20 79 79 74  r->yyidx];.  yyt
2a38b 6f 73 2d 3e 73 74 61 74 65 6e 6f 20 3d 20 79 79  os->stateno = yy
2a38c 4e 65 77 53 74 61 74 65 3b 0a 20 20 79 79 74 6f  NewState;.  yyto
2a38d 73 2d 3e 6d 61 6a 6f 72 20 3d 20 79 79 4d 61 6a  s->major = yyMaj
2a38e 6f 72 3b 0a 20 20 79 79 74 6f 73 2d 3e 6d 69 6e  or;.  yytos->min
2a38f 6f 72 20 3d 20 2a 79 79 70 4d 69 6e 6f 72 3b 0a  or = *yypMinor;.
2a390 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
2a391 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45   if( yyTraceFILE
2a392 20 26 26 20 79 79 70 50 61 72 73 65 72 2d 3e 79   && yypParser->y
2a393 79 69 64 78 3e 30 20 29 7b 0a 20 20 20 20 69 6e  yidx>0 ){.    in
2a394 74 20 69 3b 0a 20 20 20 20 66 70 72 69 6e 74 66  t i;.    fprintf
2a395 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73  (yyTraceFILE,"%s
2a396 53 68 69 66 74 20 25 64 5c 6e 22 2c 79 79 54 72  Shift %d\n",yyTr
2a397 61 63 65 50 72 6f 6d 70 74 2c 79 79 4e 65 77 53  acePrompt,yyNewS
2a398 74 61 74 65 29 3b 0a 20 20 20 20 66 70 72 69 6e  tate);.    fprin
2a399 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22  tf(yyTraceFILE,"
2a39a 25 73 53 74 61 63 6b 3a 22 2c 79 79 54 72 61 63  %sStack:",yyTrac
2a39b 65 50 72 6f 6d 70 74 29 3b 0a 20 20 20 20 66 6f  ePrompt);.    fo
2a39c 72 28 69 3d 31 3b 20 69 3c 3d 79 79 70 50 61 72  r(i=1; i<=yypPar
2a39d 73 65 72 2d 3e 79 79 69 64 78 3b 20 69 2b 2b 29  ser->yyidx; i++)
2a39e 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 79  .      fprintf(y
2a39f 79 54 72 61 63 65 46 49 4c 45 2c 22 20 25 73 22  yTraceFILE," %s"
2a3a0 2c 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79 70  ,yyTokenName[yyp
2a3a1 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b  Parser->yystack[
2a3a2 69 5d 2e 6d 61 6a 6f 72 5d 29 3b 0a 20 20 20 20  i].major]);.    
2a3a3 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46  fprintf(yyTraceF
2a3a4 49 4c 45 2c 22 5c 6e 22 29 3b 0a 20 20 7d 0a 23  ILE,"\n");.  }.#
2a3a5 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 20 54 68 65 20  endif.}../* The 
2a3a6 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20  following table 
2a3a7 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61  contains informa
2a3a8 74 69 6f 6e 20 61 62 6f 75 74 20 65 76 65 72 79  tion about every
2a3a9 20 72 75 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73   rule that.** is
2a3aa 20 75 73 65 64 20 64 75 72 69 6e 67 20 74 68 65   used during the
2a3ab 20 72 65 64 75 63 65 2e 0a 2a 2f 0a 73 74 61 74   reduce..*/.stat
2a3ac 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20  ic const struct 
2a3ad 7b 0a 20 20 59 59 43 4f 44 45 54 59 50 45 20 6c  {.  YYCODETYPE l
2a3ae 68 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53  hs;         /* S
2a3af 79 6d 62 6f 6c 20 6f 6e 20 74 68 65 20 6c 65 66  ymbol on the lef
2a3b0 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74  t-hand side of t
2a3b1 68 65 20 72 75 6c 65 20 2a 2f 0a 20 20 75 6e 73  he rule */.  uns
2a3b2 69 67 6e 65 64 20 63 68 61 72 20 6e 72 68 73 3b  igned char nrhs;
2a3b3 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2a3b4 66 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64  f right-hand sid
2a3b5 65 20 73 79 6d 62 6f 6c 73 20 69 6e 20 74 68 65  e symbols in the
2a3b6 20 72 75 6c 65 20 2a 2f 0a 7d 20 79 79 52 75 6c   rule */.} yyRul
2a3b7 65 49 6e 66 6f 5b 5d 20 3d 20 7b 0a 20 20 7b 20  eInfo[] = {.  { 
2a3b8 31 34 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  140, 1 },.  { 14
2a3b9 31 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 31 2c  1, 2 },.  { 141,
2a3ba 20 31 20 7d 2c 0a 20 20 7b 20 31 34 32 2c 20 31   1 },.  { 142, 1
2a3bb 20 7d 2c 0a 20 20 7b 20 31 34 32 2c 20 33 20 7d   },.  { 142, 3 }
2a3bc 2c 0a 20 20 7b 20 31 34 33 2c 20 30 20 7d 2c 0a  ,.  { 143, 0 },.
2a3bd 20 20 7b 20 31 34 33 2c 20 31 20 7d 2c 0a 20 20    { 143, 1 },.  
2a3be 7b 20 31 34 33 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 143, 3 },.  { 
2a3bf 31 34 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  144, 1 },.  { 14
2a3c0 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  5, 3 },.  { 147,
2a3c1 20 30 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31   0 },.  { 147, 1
2a3c2 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32 20 7d   },.  { 147, 2 }
2a3c3 2c 0a 20 20 7b 20 31 34 36 2c 20 30 20 7d 2c 0a  ,.  { 146, 0 },.
2a3c4 20 20 7b 20 31 34 36 2c 20 31 20 7d 2c 0a 20 20    { 146, 1 },.  
2a3c5 7b 20 31 34 36 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 146, 1 },.  { 
2a3c6 31 34 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  146, 1 },.  { 14
2a3c7 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 35 2c  5, 2 },.  { 145,
2a3c8 20 32 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 32   2 },.  { 145, 2
2a3c9 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 32 20 7d   },.  { 145, 2 }
2a3ca 2c 0a 20 20 7b 20 31 34 39 2c 20 36 20 7d 2c 0a  ,.  { 149, 6 },.
2a3cb 20 20 7b 20 31 35 32 2c 20 30 20 7d 2c 0a 20 20    { 152, 0 },.  
2a3cc 7b 20 31 35 32 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 152, 3 },.  { 
2a3cd 31 35 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35  151, 1 },.  { 15
2a3ce 31 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35 30 2c  1, 0 },.  { 150,
2a3cf 20 34 20 7d 2c 0a 20 20 7b 20 31 35 30 2c 20 32   4 },.  { 150, 2
2a3d0 20 7d 2c 0a 20 20 7b 20 31 35 34 2c 20 33 20 7d   },.  { 154, 3 }
2a3d1 2c 0a 20 20 7b 20 31 35 34 2c 20 31 20 7d 2c 0a  ,.  { 154, 1 },.
2a3d2 20 20 7b 20 31 35 37 2c 20 33 20 7d 2c 0a 20 20    { 157, 3 },.  
2a3d3 7b 20 31 35 38 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 158, 1 },.  { 
2a3d4 31 36 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36  161, 1 },.  { 16
2a3d5 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 38 2c  2, 1 },.  { 148,
2a3d6 20 31 20 7d 2c 0a 20 20 7b 20 31 34 38 2c 20 31   1 },.  { 148, 1
2a3d7 20 7d 2c 0a 20 20 7b 20 31 34 38 2c 20 31 20 7d   },.  { 148, 1 }
2a3d8 2c 0a 20 20 7b 20 31 35 39 2c 20 30 20 7d 2c 0a  ,.  { 159, 0 },.
2a3d9 20 20 7b 20 31 35 39 2c 20 31 20 7d 2c 0a 20 20    { 159, 1 },.  
2a3da 7b 20 31 36 33 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 163, 1 },.  { 
2a3db 31 36 33 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 36  163, 4 },.  { 16
2a3dc 33 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 36 34 2c  3, 6 },.  { 164,
2a3dd 20 31 20 7d 2c 0a 20 20 7b 20 31 36 34 2c 20 32   1 },.  { 164, 2
2a3de 20 7d 2c 0a 20 20 7b 20 31 36 35 2c 20 31 20 7d   },.  { 165, 1 }
2a3df 2c 0a 20 20 7b 20 31 36 35 2c 20 31 20 7d 2c 0a  ,.  { 165, 1 },.
2a3e0 20 20 7b 20 31 36 30 2c 20 32 20 7d 2c 0a 20 20    { 160, 2 },.  
2a3e1 7b 20 31 36 30 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 160, 0 },.  { 
2a3e2 31 36 38 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 36  168, 3 },.  { 16
2a3e3 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 39 2c  8, 1 },.  { 169,
2a3e4 20 32 20 7d 2c 0a 20 20 7b 20 31 36 39 2c 20 34   2 },.  { 169, 4
2a3e5 20 7d 2c 0a 20 20 7b 20 31 36 39 2c 20 33 20 7d   },.  { 169, 3 }
2a3e6 2c 0a 20 20 7b 20 31 36 39 2c 20 33 20 7d 2c 0a  ,.  { 169, 3 },.
2a3e7 20 20 7b 20 31 36 39 2c 20 32 20 7d 2c 0a 20 20    { 169, 2 },.  
2a3e8 7b 20 31 36 39 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 169, 2 },.  { 
2a3e9 31 36 39 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 36  169, 3 },.  { 16
2a3ea 39 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 36 39 2c  9, 5 },.  { 169,
2a3eb 20 32 20 7d 2c 0a 20 20 7b 20 31 36 39 2c 20 34   2 },.  { 169, 4
2a3ec 20 7d 2c 0a 20 20 7b 20 31 36 39 2c 20 34 20 7d   },.  { 169, 4 }
2a3ed 2c 0a 20 20 7b 20 31 36 39 2c 20 31 20 7d 2c 0a  ,.  { 169, 1 },.
2a3ee 20 20 7b 20 31 36 39 2c 20 32 20 7d 2c 0a 20 20    { 169, 2 },.  
2a3ef 7b 20 31 37 34 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 174, 0 },.  { 
2a3f0 31 37 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37  174, 1 },.  { 17
2a3f1 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 37 36 2c  6, 0 },.  { 176,
2a3f2 20 32 20 7d 2c 0a 20 20 7b 20 31 37 38 2c 20 32   2 },.  { 178, 2
2a3f3 20 7d 2c 0a 20 20 7b 20 31 37 38 2c 20 33 20 7d   },.  { 178, 3 }
2a3f4 2c 0a 20 20 7b 20 31 37 38 2c 20 33 20 7d 2c 0a  ,.  { 178, 3 },.
2a3f5 20 20 7b 20 31 37 38 2c 20 33 20 7d 2c 0a 20 20    { 178, 3 },.  
2a3f6 7b 20 31 37 39 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 179, 2 },.  { 
2a3f7 31 37 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37  179, 2 },.  { 17
2a3f8 39 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 39 2c  9, 1 },.  { 179,
2a3f9 20 31 20 7d 2c 0a 20 20 7b 20 31 37 37 2c 20 33   1 },.  { 177, 3
2a3fa 20 7d 2c 0a 20 20 7b 20 31 37 37 2c 20 32 20 7d   },.  { 177, 2 }
2a3fb 2c 0a 20 20 7b 20 31 38 30 2c 20 30 20 7d 2c 0a  ,.  { 180, 0 },.
2a3fc 20 20 7b 20 31 38 30 2c 20 32 20 7d 2c 0a 20 20    { 180, 2 },.  
2a3fd 7b 20 31 38 30 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 180, 2 },.  { 
2a3fe 31 35 35 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35  155, 0 },.  { 15
2a3ff 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 31 2c  5, 2 },.  { 181,
2a400 20 33 20 7d 2c 0a 20 20 7b 20 31 38 31 2c 20 32   3 },.  { 181, 2
2a401 20 7d 2c 0a 20 20 7b 20 31 38 31 2c 20 31 20 7d   },.  { 181, 1 }
2a402 2c 0a 20 20 7b 20 31 38 32 2c 20 32 20 7d 2c 0a  ,.  { 182, 2 },.
2a403 20 20 7b 20 31 38 32 2c 20 37 20 7d 2c 0a 20 20    { 182, 7 },.  
2a404 7b 20 31 38 32 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 182, 5 },.  { 
2a405 31 38 32 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 38  182, 5 },.  { 18
2a406 32 2c 20 31 30 20 7d 2c 0a 20 20 7b 20 31 38 34  2, 10 },.  { 184
2a407 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 38 34 2c 20  , 0 },.  { 184, 
2a408 31 20 7d 2c 0a 20 20 7b 20 31 37 32 2c 20 30 20  1 },.  { 172, 0 
2a409 7d 2c 0a 20 20 7b 20 31 37 32 2c 20 33 20 7d 2c  },.  { 172, 3 },
2a40a 0a 20 20 7b 20 31 38 35 2c 20 30 20 7d 2c 0a 20  .  { 185, 0 },. 
2a40b 20 7b 20 31 38 35 2c 20 32 20 7d 2c 0a 20 20 7b   { 185, 2 },.  {
2a40c 20 31 38 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   186, 1 },.  { 1
2a40d 38 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 38 36  86, 1 },.  { 186
2a40e 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20  , 1 },.  { 145, 
2a40f 34 20 7d 2c 0a 20 20 7b 20 31 38 38 2c 20 32 20  4 },.  { 188, 2 
2a410 7d 2c 0a 20 20 7b 20 31 38 38 2c 20 30 20 7d 2c  },.  { 188, 0 },
2a411 0a 20 20 7b 20 31 34 35 2c 20 38 20 7d 2c 0a 20  .  { 145, 8 },. 
2a412 20 7b 20 31 34 35 2c 20 34 20 7d 2c 0a 20 20 7b   { 145, 4 },.  {
2a413 20 31 34 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   145, 1 },.  { 1
2a414 35 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35 36  56, 1 },.  { 156
2a415 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 39 31 2c 20  , 3 },.  { 191, 
2a416 31 20 7d 2c 0a 20 20 7b 20 31 39 31 2c 20 32 20  1 },.  { 191, 2 
2a417 7d 2c 0a 20 20 7b 20 31 39 31 2c 20 31 20 7d 2c  },.  { 191, 1 },
2a418 0a 20 20 7b 20 31 39 30 2c 20 39 20 7d 2c 0a 20  .  { 190, 9 },. 
2a419 20 7b 20 31 39 32 2c 20 31 20 7d 2c 0a 20 20 7b   { 192, 1 },.  {
2a41a 20 31 39 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   192, 1 },.  { 1
2a41b 39 32 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 30  92, 0 },.  { 200
2a41c 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 30 2c 20  , 2 },.  { 200, 
2a41d 30 20 7d 2c 0a 20 20 7b 20 31 39 33 2c 20 33 20  0 },.  { 193, 3 
2a41e 7d 2c 0a 20 20 7b 20 31 39 33 2c 20 32 20 7d 2c  },.  { 193, 2 },
2a41f 0a 20 20 7b 20 31 39 33 2c 20 34 20 7d 2c 0a 20  .  { 193, 4 },. 
2a420 20 7b 20 32 30 31 2c 20 32 20 7d 2c 0a 20 20 7b   { 201, 2 },.  {
2a421 20 32 30 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   201, 1 },.  { 2
2a422 30 31 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 34  01, 0 },.  { 194
2a423 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 34 2c 20  , 0 },.  { 194, 
2a424 32 20 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 32 20  2 },.  { 203, 2 
2a425 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 30 20 7d 2c  },.  { 203, 0 },
2a426 0a 20 20 7b 20 32 30 32 2c 20 37 20 7d 2c 0a 20  .  { 202, 7 },. 
2a427 20 7b 20 32 30 32 2c 20 37 20 7d 2c 0a 20 20 7b   { 202, 7 },.  {
2a428 20 32 30 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   208, 1 },.  { 2
2a429 30 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35 33  08, 1 },.  { 153
2a42a 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35 33 2c 20  , 0 },.  { 153, 
2a42b 32 20 7d 2c 0a 20 20 7b 20 31 38 39 2c 20 32 20  2 },.  { 189, 2 
2a42c 7d 2c 0a 20 20 7b 20 32 30 34 2c 20 31 20 7d 2c  },.  { 204, 1 },
2a42d 0a 20 20 7b 20 32 30 34 2c 20 32 20 7d 2c 0a 20  .  { 204, 2 },. 
2a42e 20 7b 20 32 30 34 2c 20 33 20 7d 2c 0a 20 20 7b   { 204, 3 },.  {
2a42f 20 32 30 34 2c 20 34 20 7d 2c 0a 20 20 7b 20 32   204, 4 },.  { 2
2a430 30 36 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 36  06, 2 },.  { 206
2a431 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 35 2c 20  , 0 },.  { 205, 
2a432 30 20 7d 2c 0a 20 20 7b 20 32 30 35 2c 20 33 20  0 },.  { 205, 3 
2a433 7d 2c 0a 20 20 7b 20 32 30 35 2c 20 32 20 7d 2c  },.  { 205, 2 },
2a434 0a 20 20 7b 20 32 30 37 2c 20 34 20 7d 2c 0a 20  .  { 207, 4 },. 
2a435 20 7b 20 32 30 37 2c 20 30 20 7d 2c 0a 20 20 7b   { 207, 0 },.  {
2a436 20 31 39 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   198, 0 },.  { 1
2a437 39 38 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 31 31  98, 3 },.  { 211
2a438 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 31 31 2c 20  , 4 },.  { 211, 
2a439 32 20 7d 2c 0a 20 20 7b 20 32 31 32 2c 20 31 20  2 },.  { 212, 1 
2a43a 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 31 20 7d 2c  },.  { 173, 1 },
2a43b 0a 20 20 7b 20 31 37 33 2c 20 31 20 7d 2c 0a 20  .  { 173, 1 },. 
2a43c 20 7b 20 31 37 33 2c 20 30 20 7d 2c 0a 20 20 7b   { 173, 0 },.  {
2a43d 20 31 39 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   196, 0 },.  { 1
2a43e 39 36 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 39 37  96, 3 },.  { 197
2a43f 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 37 2c 20  , 0 },.  { 197, 
2a440 32 20 7d 2c 0a 20 20 7b 20 31 39 39 2c 20 30 20  2 },.  { 199, 0 
2a441 7d 2c 0a 20 20 7b 20 31 39 39 2c 20 32 20 7d 2c  },.  { 199, 2 },
2a442 0a 20 20 7b 20 31 39 39 2c 20 34 20 7d 2c 0a 20  .  { 199, 4 },. 
2a443 20 7b 20 31 39 39 2c 20 34 20 7d 2c 0a 20 20 7b   { 199, 4 },.  {
2a444 20 31 34 35 2c 20 35 20 7d 2c 0a 20 20 7b 20 31   145, 5 },.  { 1
2a445 39 35 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 35  95, 0 },.  { 195
2a446 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20  , 2 },.  { 145, 
2a447 37 20 7d 2c 0a 20 20 7b 20 32 31 34 2c 20 35 20  7 },.  { 214, 5 
2a448 7d 2c 0a 20 20 7b 20 32 31 34 2c 20 33 20 7d 2c  },.  { 214, 3 },
2a449 0a 20 20 7b 20 31 34 35 2c 20 38 20 7d 2c 0a 20  .  { 145, 8 },. 
2a44a 20 7b 20 31 34 35 2c 20 35 20 7d 2c 0a 20 20 7b   { 145, 5 },.  {
2a44b 20 31 34 35 2c 20 36 20 7d 2c 0a 20 20 7b 20 32   145, 6 },.  { 2
2a44c 31 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 31 35  15, 2 },.  { 215
2a44d 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 31 37 2c 20  , 1 },.  { 217, 
2a44e 33 20 7d 2c 0a 20 20 7b 20 32 31 37 2c 20 31 20  3 },.  { 217, 1 
2a44f 7d 2c 0a 20 20 7b 20 32 31 36 2c 20 30 20 7d 2c  },.  { 216, 0 },
2a450 0a 20 20 7b 20 32 31 36 2c 20 33 20 7d 2c 0a 20  .  { 216, 3 },. 
2a451 20 7b 20 32 31 30 2c 20 33 20 7d 2c 0a 20 20 7b   { 210, 3 },.  {
2a452 20 32 31 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   210, 1 },.  { 1
2a453 37 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 31  71, 1 },.  { 171
2a454 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20  , 3 },.  { 170, 
2a455 31 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 31 20  1 },.  { 171, 1 
2a456 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 31 20 7d 2c  },.  { 171, 1 },
2a457 0a 20 20 7b 20 31 37 31 2c 20 33 20 7d 2c 0a 20  .  { 171, 3 },. 
2a458 20 7b 20 31 37 31 2c 20 35 20 7d 2c 0a 20 20 7b   { 171, 5 },.  {
2a459 20 31 37 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   170, 1 },.  { 1
2a45a 37 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 31  70, 1 },.  { 171
2a45b 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20  , 1 },.  { 171, 
2a45c 31 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 33 20  1 },.  { 171, 3 
2a45d 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 36 20 7d 2c  },.  { 171, 6 },
2a45e 0a 20 20 7b 20 31 37 31 2c 20 35 20 7d 2c 0a 20  .  { 171, 5 },. 
2a45f 20 7b 20 31 37 31 2c 20 34 20 7d 2c 0a 20 20 7b   { 171, 4 },.  {
2a460 20 31 37 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   170, 1 },.  { 1
2a461 37 31 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 31  71, 3 },.  { 171
2a462 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20  , 3 },.  { 171, 
2a463 33 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 33 20  3 },.  { 171, 3 
2a464 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 33 20 7d 2c  },.  { 171, 3 },
2a465 0a 20 20 7b 20 31 37 31 2c 20 33 20 7d 2c 0a 20  .  { 171, 3 },. 
2a466 20 7b 20 31 37 31 2c 20 33 20 7d 2c 0a 20 20 7b   { 171, 3 },.  {
2a467 20 31 37 31 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   171, 3 },.  { 2
2a468 31 39 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 31 39  19, 1 },.  { 219
2a469 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 31 39 2c 20  , 2 },.  { 219, 
2a46a 31 20 7d 2c 0a 20 20 7b 20 32 31 39 2c 20 32 20  1 },.  { 219, 2 
2a46b 7d 2c 0a 20 20 7b 20 32 32 30 2c 20 32 20 7d 2c  },.  { 220, 2 },
2a46c 0a 20 20 7b 20 32 32 30 2c 20 30 20 7d 2c 0a 20  .  { 220, 0 },. 
2a46d 20 7b 20 31 37 31 2c 20 34 20 7d 2c 0a 20 20 7b   { 171, 4 },.  {
2a46e 20 31 37 31 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   171, 2 },.  { 1
2a46f 37 31 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 31  71, 3 },.  { 171
2a470 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20  , 3 },.  { 171, 
2a471 34 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 32 20  4 },.  { 171, 2 
2a472 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 32 20 7d 2c  },.  { 171, 2 },
2a473 0a 20 20 7b 20 31 37 31 2c 20 32 20 7d 2c 0a 20  .  { 171, 2 },. 
2a474 20 7b 20 31 37 31 2c 20 32 20 7d 2c 0a 20 20 7b   { 171, 2 },.  {
2a475 20 32 32 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   221, 1 },.  { 2
2a476 32 31 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 31  21, 2 },.  { 171
2a477 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 32 32 2c 20  , 5 },.  { 222, 
2a478 31 20 7d 2c 0a 20 20 7b 20 32 32 32 2c 20 32 20  1 },.  { 222, 2 
2a479 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 35 20 7d 2c  },.  { 171, 5 },
2a47a 0a 20 20 7b 20 31 37 31 2c 20 33 20 7d 2c 0a 20  .  { 171, 3 },. 
2a47b 20 7b 20 31 37 31 2c 20 35 20 7d 2c 0a 20 20 7b   { 171, 5 },.  {
2a47c 20 31 37 31 2c 20 34 20 7d 2c 0a 20 20 7b 20 31   171, 4 },.  { 1
2a47d 37 31 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 31  71, 4 },.  { 171
2a47e 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 32 34 2c 20  , 5 },.  { 224, 
2a47f 35 20 7d 2c 0a 20 20 7b 20 32 32 34 2c 20 34 20  5 },.  { 224, 4 
2a480 7d 2c 0a 20 20 7b 20 32 32 35 2c 20 32 20 7d 2c  },.  { 225, 2 },
2a481 0a 20 20 7b 20 32 32 35 2c 20 30 20 7d 2c 0a 20  .  { 225, 0 },. 
2a482 20 7b 20 32 32 33 2c 20 31 20 7d 2c 0a 20 20 7b   { 223, 1 },.  {
2a483 20 32 32 33 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   223, 0 },.  { 2
2a484 31 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 31 38  18, 1 },.  { 218
2a485 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 31 33 2c 20  , 0 },.  { 213, 
2a486 33 20 7d 2c 0a 20 20 7b 20 32 31 33 2c 20 31 20  3 },.  { 213, 1 
2a487 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 31 31 20 7d  },.  { 145, 11 }
2a488 2c 0a 20 20 7b 20 32 32 36 2c 20 31 20 7d 2c 0a  ,.  { 226, 1 },.
2a489 20 20 7b 20 32 32 36 2c 20 30 20 7d 2c 0a 20 20    { 226, 0 },.  
2a48a 7b 20 31 37 35 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 175, 0 },.  { 
2a48b 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 38  175, 3 },.  { 18
2a48c 33 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 38 33 2c  3, 5 },.  { 183,
2a48d 20 33 20 7d 2c 0a 20 20 7b 20 32 32 37 2c 20 30   3 },.  { 227, 0
2a48e 20 7d 2c 0a 20 20 7b 20 32 32 37 2c 20 32 20 7d   },.  { 227, 2 }
2a48f 2c 0a 20 20 7b 20 31 34 35 2c 20 34 20 7d 2c 0a  ,.  { 145, 4 },.
2a490 20 20 7b 20 31 34 35 2c 20 31 20 7d 2c 0a 20 20    { 145, 1 },.  
2a491 7b 20 31 34 35 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 145, 2 },.  { 
2a492 31 34 35 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 34  145, 5 },.  { 14
2a493 35 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 34 35 2c  5, 5 },.  { 145,
2a494 20 35 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 35   5 },.  { 145, 5
2a495 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 36 20 7d   },.  { 145, 6 }
2a496 2c 0a 20 20 7b 20 31 34 35 2c 20 33 20 7d 2c 0a  ,.  { 145, 3 },.
2a497 20 20 7b 20 32 32 38 2c 20 31 20 7d 2c 0a 20 20    { 228, 1 },.  
2a498 7b 20 32 32 38 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 228, 1 },.  { 
2a499 31 36 36 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 36  166, 2 },.  { 16
2a49a 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33 30 2c  7, 2 },.  { 230,
2a49b 20 31 20 7d 2c 0a 20 20 7b 20 32 32 39 2c 20 31   1 },.  { 229, 1
2a49c 20 7d 2c 0a 20 20 7b 20 32 32 39 2c 20 30 20 7d   },.  { 229, 0 }
2a49d 2c 0a 20 20 7b 20 31 34 35 2c 20 35 20 7d 2c 0a  ,.  { 145, 5 },.
2a49e 20 20 7b 20 32 33 31 2c 20 31 31 20 7d 2c 0a 20    { 231, 11 },. 
2a49f 20 7b 20 32 33 33 2c 20 31 20 7d 2c 0a 20 20 7b   { 233, 1 },.  {
2a4a0 20 32 33 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   233, 1 },.  { 2
2a4a1 33 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33 33  33, 2 },.  { 233
2a4a2 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 33 34 2c 20  , 0 },.  { 234, 
2a4a3 31 20 7d 2c 0a 20 20 7b 20 32 33 34 2c 20 31 20  1 },.  { 234, 1 
2a4a4 7d 2c 0a 20 20 7b 20 32 33 34 2c 20 33 20 7d 2c  },.  { 234, 3 },
2a4a5 0a 20 20 7b 20 32 33 35 2c 20 30 20 7d 2c 0a 20  .  { 235, 0 },. 
2a4a6 20 7b 20 32 33 35 2c 20 33 20 7d 2c 0a 20 20 7b   { 235, 3 },.  {
2a4a7 20 32 33 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   236, 0 },.  { 2
2a4a8 33 36 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33 32  36, 2 },.  { 232
2a4a9 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 33 32 2c 20  , 3 },.  { 232, 
2a4aa 32 20 7d 2c 0a 20 20 7b 20 32 33 37 2c 20 36 20  2 },.  { 237, 6 
2a4ab 7d 2c 0a 20 20 7b 20 32 33 37 2c 20 38 20 7d 2c  },.  { 237, 8 },
2a4ac 0a 20 20 7b 20 32 33 37 2c 20 35 20 7d 2c 0a 20  .  { 237, 5 },. 
2a4ad 20 7b 20 32 33 37 2c 20 34 20 7d 2c 0a 20 20 7b   { 237, 4 },.  {
2a4ae 20 32 33 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   237, 1 },.  { 1
2a4af 37 31 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 31  71, 4 },.  { 171
2a4b0 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 38 37 2c 20  , 6 },.  { 187, 
2a4b1 31 20 7d 2c 0a 20 20 7b 20 31 38 37 2c 20 31 20  1 },.  { 187, 1 
2a4b2 7d 2c 0a 20 20 7b 20 31 38 37 2c 20 31 20 7d 2c  },.  { 187, 1 },
2a4b3 0a 20 20 7b 20 31 34 35 2c 20 34 20 7d 2c 0a 20  .  { 145, 4 },. 
2a4b4 20 7b 20 31 34 35 2c 20 36 20 7d 2c 0a 20 20 7b   { 145, 6 },.  {
2a4b5 20 31 34 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   145, 3 },.  { 2
2a4b6 33 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 33 39  39, 0 },.  { 239
2a4b7 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33 38 2c 20  , 2 },.  { 238, 
2a4b8 31 20 7d 2c 0a 20 20 7b 20 32 33 38 2c 20 30 20  1 },.  { 238, 0 
2a4b9 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 31 20 7d 2c  },.  { 145, 1 },
2a4ba 0a 20 20 7b 20 31 34 35 2c 20 33 20 7d 2c 0a 20  .  { 145, 3 },. 
2a4bb 20 7b 20 31 34 35 2c 20 31 20 7d 2c 0a 20 20 7b   { 145, 1 },.  {
2a4bc 20 31 34 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   145, 3 },.  { 1
2a4bd 34 35 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 34 35  45, 6 },.  { 145
2a4be 2c 20 36 20 7d 2c 0a 20 20 7b 20 32 34 30 2c 20  , 6 },.  { 240, 
2a4bf 31 20 7d 2c 0a 20 20 7b 20 32 34 31 2c 20 30 20  1 },.  { 241, 0 
2a4c0 7d 2c 0a 20 20 7b 20 32 34 31 2c 20 31 20 7d 2c  },.  { 241, 1 },
2a4c1 0a 20 20 7b 20 31 34 35 2c 20 31 20 7d 2c 0a 20  .  { 145, 1 },. 
2a4c2 20 7b 20 31 34 35 2c 20 34 20 7d 2c 0a 20 20 7b   { 145, 4 },.  {
2a4c3 20 32 34 32 2c 20 37 20 7d 2c 0a 20 20 7b 20 32   242, 7 },.  { 2
2a4c4 34 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 34 33  43, 1 },.  { 243
2a4c5 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 34 34 2c 20  , 3 },.  { 244, 
2a4c6 30 20 7d 2c 0a 20 20 7b 20 32 34 34 2c 20 32 20  0 },.  { 244, 2 
2a4c7 7d 2c 0a 20 20 7b 20 32 34 35 2c 20 31 20 7d 2c  },.  { 245, 1 },
2a4c8 0a 20 20 7b 20 32 34 35 2c 20 33 20 7d 2c 0a 20  .  { 245, 3 },. 
2a4c9 20 7b 20 32 34 36 2c 20 31 20 7d 2c 0a 20 20 7b   { 246, 1 },.  {
2a4ca 20 32 34 37 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   247, 0 },.  { 2
2a4cb 34 37 2c 20 32 20 7d 2c 0a 7d 3b 0a 0a 73 74 61  47, 2 },.};..sta
2a4cc 74 69 63 20 76 6f 69 64 20 79 79 5f 61 63 63 65  tic void yy_acce
2a4cd 70 74 28 79 79 50 61 72 73 65 72 2a 29 3b 20 20  pt(yyParser*);  
2a4ce 2f 2a 20 46 6f 72 77 61 72 64 20 44 65 63 6c 61  /* Forward Decla
2a4cf 72 61 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  ration */../*.**
2a4d0 20 50 65 72 66 6f 72 6d 20 61 20 72 65 64 75 63   Perform a reduc
2a4d1 65 20 61 63 74 69 6f 6e 20 61 6e 64 20 74 68 65  e action and the
2a4d2 20 73 68 69 66 74 20 74 68 61 74 20 6d 75 73 74   shift that must
2a4d3 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20   immediately.** 
2a4d4 66 6f 6c 6c 6f 77 20 74 68 65 20 72 65 64 75 63  follow the reduc
2a4d5 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
2a4d6 64 20 79 79 5f 72 65 64 75 63 65 28 0a 20 20 79  d yy_reduce(.  y
2a4d7 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73  yParser *yypPars
2a4d8 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  er,         /* T
2a4d9 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 69  he parser */.  i
2a4da 6e 74 20 79 79 72 75 6c 65 6e 6f 20 20 20 20 20  nt yyruleno     
2a4db 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2a4dc 75 6d 62 65 72 20 6f 66 20 74 68 65 20 72 75 6c  umber of the rul
2a4dd 65 20 62 79 20 77 68 69 63 68 20 74 6f 20 72 65  e by which to re
2a4de 64 75 63 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  duce */.){.  int
2a4df 20 79 79 67 6f 74 6f 3b 20 20 20 20 20 20 20 20   yygoto;        
2a4e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a4e1 54 68 65 20 6e 65 78 74 20 73 74 61 74 65 20 2a  The next state *
2a4e2 2f 0a 20 20 69 6e 74 20 79 79 61 63 74 3b 20 20  /.  int yyact;  
2a4e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a4e4 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20      /* The next 
2a4e5 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 59 59 4d 49  action */.  YYMI
2a4e6 4e 4f 52 54 59 50 45 20 79 79 67 6f 74 6f 6d 69  NORTYPE yygotomi
2a4e7 6e 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  nor;        /* T
2a4e8 68 65 20 4c 48 53 20 6f 66 20 74 68 65 20 72 75  he LHS of the ru
2a4e9 6c 65 20 72 65 64 75 63 65 64 20 2a 2f 0a 20 20  le reduced */.  
2a4ea 79 79 53 74 61 63 6b 45 6e 74 72 79 20 2a 79 79  yyStackEntry *yy
2a4eb 6d 73 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  msp;            
2a4ec 2f 2a 20 54 68 65 20 74 6f 70 20 6f 66 20 74 68  /* The top of th
2a4ed 65 20 70 61 72 73 65 72 27 73 20 73 74 61 63 6b  e parser's stack
2a4ee 20 2a 2f 0a 20 20 69 6e 74 20 79 79 73 69 7a 65   */.  int yysize
2a4ef 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2a4f0 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20        /* Amount 
2a4f1 74 6f 20 70 6f 70 20 74 68 65 20 73 74 61 63 6b  to pop the stack
2a4f2 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72   */.  sqlite3Par
2a4f3 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a 20 20  serARG_FETCH;.  
2a4f4 79 79 6d 73 70 20 3d 20 26 79 79 70 50 61 72 73  yymsp = &yypPars
2a4f5 65 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79 70 50  er->yystack[yypP
2a4f6 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 3b 0a 23  arser->yyidx];.#
2a4f7 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
2a4f8 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20  if( yyTraceFILE 
2a4f9 26 26 20 79 79 72 75 6c 65 6e 6f 3e 3d 30 20 0a  && yyruleno>=0 .
2a4fa 20 20 20 20 20 20 20 20 26 26 20 79 79 72 75 6c          && yyrul
2a4fb 65 6e 6f 3c 28 69 6e 74 29 28 73 69 7a 65 6f 66  eno<(int)(sizeof
2a4fc 28 79 79 52 75 6c 65 4e 61 6d 65 29 2f 73 69 7a  (yyRuleName)/siz
2a4fd 65 6f 66 28 79 79 52 75 6c 65 4e 61 6d 65 5b 30  eof(yyRuleName[0
2a4fe 5d 29 29 20 29 7b 0a 20 20 20 20 66 70 72 69 6e  ])) ){.    fprin
2a4ff 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 20  tf(yyTraceFILE, 
2a500 22 25 73 52 65 64 75 63 65 20 5b 25 73 5d 2e 5c  "%sReduce [%s].\
2a501 6e 22 2c 20 79 79 54 72 61 63 65 50 72 6f 6d 70  n", yyTracePromp
2a502 74 2c 0a 20 20 20 20 20 20 79 79 52 75 6c 65 4e  t,.      yyRuleN
2a503 61 6d 65 5b 79 79 72 75 6c 65 6e 6f 5d 29 3b 0a  ame[yyruleno]);.
2a504 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44    }.#endif /* ND
2a505 45 42 55 47 20 2a 2f 0a 0a 20 20 2f 2a 20 53 69  EBUG */..  /* Si
2a506 6c 65 6e 63 65 20 63 6f 6d 70 6c 61 69 6e 74 73  lence complaints
2a507 20 66 72 6f 6d 20 70 75 72 69 66 79 20 61 62 6f   from purify abo
2a508 75 74 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 20 62  ut yygotominor b
2a509 65 69 6e 67 20 75 6e 69 6e 69 74 69 61 6c 69 7a  eing uninitializ
2a50a 65 64 0a 20 20 2a 2a 20 69 6e 20 73 6f 6d 65 20  ed.  ** in some 
2a50b 63 61 73 65 73 20 77 68 65 6e 20 69 74 20 69 73  cases when it is
2a50c 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65   copied into the
2a50d 20 73 74 61 63 6b 20 61 66 74 65 72 20 74 68 65   stack after the
2a50e 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20   following.  ** 
2a50f 73 77 69 74 63 68 2e 20 20 79 79 67 6f 74 6f 6d  switch.  yygotom
2a510 69 6e 6f 72 20 69 73 20 75 6e 69 6e 69 74 69 61  inor is uninitia
2a511 6c 69 7a 65 64 20 77 68 65 6e 20 61 20 72 75 6c  lized when a rul
2a512 65 20 72 65 64 75 63 65 73 20 74 68 61 74 20 64  e reduces that d
2a513 6f 65 73 0a 20 20 2a 2a 20 6e 6f 74 20 73 65 74  oes.  ** not set
2a514 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 69 74   the value of it
2a515 73 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65  s left-hand side
2a516 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 2e 20 20 4c   nonterminal.  L
2a517 65 61 76 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20  eaving the.  ** 
2a518 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6e 6f 6e  value of the non
2a519 74 65 72 6d 69 6e 61 6c 20 75 6e 69 6e 69 74 69  terminal uniniti
2a51a 61 6c 69 7a 65 64 20 69 73 20 75 74 74 65 72 6c  alized is utterl
2a51b 79 20 68 61 72 6d 6c 65 73 73 20 61 73 20 6c 6f  y harmless as lo
2a51c 6e 67 0a 20 20 2a 2a 20 61 73 20 74 68 65 20 76  ng.  ** as the v
2a51d 61 6c 75 65 20 69 73 20 6e 65 76 65 72 20 75 73  alue is never us
2a51e 65 64 2e 20 20 53 6f 20 72 65 61 6c 6c 79 20 74  ed.  So really t
2a51f 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 74 68  he only thing th
2a520 69 73 20 63 6f 64 65 0a 20 20 2a 2a 20 61 63 63  is code.  ** acc
2a521 6f 6d 70 6c 69 73 68 65 73 20 69 73 20 74 6f 20  omplishes is to 
2a522 71 75 69 65 74 65 6e 20 70 75 72 69 66 79 2e 20  quieten purify. 
2a523 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 32 30 30 37   .  **.  ** 2007
2a524 2d 30 31 2d 31 36 3a 20 20 54 68 65 20 77 69 72  -01-16:  The wir
2a525 65 73 68 61 72 6b 20 70 72 6f 6a 65 63 74 20 28  eshark project (
2a526 77 77 77 2e 77 69 72 65 73 68 61 72 6b 2e 6f 72  www.wireshark.or
2a527 67 29 20 72 65 70 6f 72 74 73 20 74 68 61 74 0a  g) reports that.
2a528 20 20 2a 2a 20 77 69 74 68 6f 75 74 20 74 68 69    ** without thi
2a529 73 20 63 6f 64 65 2c 20 74 68 65 69 72 20 70 61  s code, their pa
2a52a 72 73 65 72 20 73 65 67 66 61 75 6c 74 73 2e 20  rser segfaults. 
2a52b 20 49 27 6d 20 6e 6f 74 20 73 75 72 65 20 77 68   I'm not sure wh
2a52c 61 74 20 74 68 65 72 65 0a 20 20 2a 2a 20 70 61  at there.  ** pa
2a52d 72 73 65 72 20 69 73 20 64 6f 69 6e 67 20 74 6f  rser is doing to
2a52e 20 6d 61 6b 65 20 74 68 69 73 20 68 61 70 70 65   make this happe
2a52f 6e 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20  n.  This is the 
2a530 73 65 63 6f 6e 64 20 62 75 67 20 72 65 70 6f 72  second bug repor
2a531 74 0a 20 20 2a 2a 20 66 72 6f 6d 20 77 69 72 65  t.  ** from wire
2a532 73 68 61 72 6b 20 74 68 69 73 20 77 65 65 6b 2e  shark this week.
2a533 20 20 43 6c 65 61 72 6c 79 20 74 68 65 79 20 61    Clearly they a
2a534 72 65 20 73 74 72 65 73 73 69 6e 67 20 4c 65 6d  re stressing Lem
2a535 6f 6e 20 69 6e 20 77 61 79 73 0a 20 20 2a 2a 20  on in ways.  ** 
2a536 74 68 61 74 20 69 74 20 68 61 73 20 6e 6f 74 20  that it has not 
2a537 62 65 65 6e 20 70 72 65 76 69 6f 75 73 6c 79 20  been previously 
2a538 73 74 72 65 73 73 65 64 2e 2e 2e 20 20 28 53 51  stressed...  (SQ
2a539 4c 69 74 65 20 74 69 63 6b 65 74 20 23 32 31 37  Lite ticket #217
2a53a 32 29 0a 20 20 2a 2f 0a 20 20 2f 2a 6d 65 6d 73  2).  */.  /*mems
2a53b 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2c  et(&yygotominor,
2a53c 20 30 2c 20 73 69 7a 65 6f 66 28 79 79 67 6f 74   0, sizeof(yygot
2a53d 6f 6d 69 6e 6f 72 29 29 3b 2a 2f 0a 20 20 79 79  ominor));*/.  yy
2a53e 67 6f 74 6f 6d 69 6e 6f 72 20 3d 20 79 79 7a 65  gotominor = yyze
2a53f 72 6f 6d 69 6e 6f 72 3b 0a 0a 0a 20 20 73 77 69  rominor;...  swi
2a540 74 63 68 28 20 79 79 72 75 6c 65 6e 6f 20 29 7b  tch( yyruleno ){
2a541 0a 20 20 2f 2a 20 42 65 67 69 6e 6e 69 6e 67 20  .  /* Beginning 
2a542 68 65 72 65 20 61 72 65 20 74 68 65 20 72 65 64  here are the red
2a543 75 63 74 69 6f 6e 20 63 61 73 65 73 2e 20 20 41  uction cases.  A
2a544 20 74 79 70 69 63 61 6c 20 65 78 61 6d 70 6c 65   typical example
2a545 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 73 3a 0a 20  .  ** follows:. 
2a546 20 2a 2a 20 20 20 63 61 73 65 20 30 3a 0a 20 20   **   case 0:.  
2a547 2a 2a 20 20 23 6c 69 6e 65 20 3c 6c 69 6e 65 6e  **  #line <linen
2a548 6f 3e 20 3c 67 72 61 6d 6d 61 72 66 69 6c 65 3e  o> <grammarfile>
2a549 0a 20 20 2a 2a 20 20 20 20 20 7b 20 2e 2e 2e 20  .  **     { ... 
2a54a 7d 20 20 20 20 20 20 20 20 20 20 20 2f 2f 20 55  }           // U
2a54b 73 65 72 20 73 75 70 70 6c 69 65 64 20 63 6f 64  ser supplied cod
2a54c 65 0a 20 20 2a 2a 20 20 23 6c 69 6e 65 20 3c 6c  e.  **  #line <l
2a54d 69 6e 65 6e 6f 3e 20 3c 74 68 69 73 66 69 6c 65  ineno> <thisfile
2a54e 3e 0a 20 20 2a 2a 20 20 20 20 20 62 72 65 61 6b  >.  **     break
2a54f 3b 0a 20 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ;.  */.      cas
2a550 65 20 30 3a 20 2f 2a 20 69 6e 70 75 74 20 3a 3a  e 0: /* input ::
2a551 3d 20 63 6d 64 6c 69 73 74 20 2a 2f 0a 20 20 20  = cmdlist */.   
2a552 20 20 20 63 61 73 65 20 31 3a 20 2f 2a 20 63 6d     case 1: /* cm
2a553 64 6c 69 73 74 20 3a 3a 3d 20 63 6d 64 6c 69 73  dlist ::= cmdlis
2a554 74 20 65 63 6d 64 20 2a 2f 0a 20 20 20 20 20 20  t ecmd */.      
2a555 63 61 73 65 20 32 3a 20 2f 2a 20 63 6d 64 6c 69  case 2: /* cmdli
2a556 73 74 20 3a 3a 3d 20 65 63 6d 64 20 2a 2f 0a 20  st ::= ecmd */. 
2a557 20 20 20 20 20 63 61 73 65 20 33 3a 20 2f 2a 20       case 3: /* 
2a558 65 63 6d 64 20 3a 3a 3d 20 53 45 4d 49 20 2a 2f  ecmd ::= SEMI */
2a559 0a 20 20 20 20 20 20 63 61 73 65 20 34 3a 20 2f  .      case 4: /
2a55a 2a 20 65 63 6d 64 20 3a 3a 3d 20 65 78 70 6c 61  * ecmd ::= expla
2a55b 69 6e 20 63 6d 64 78 20 53 45 4d 49 20 2a 2f 0a  in cmdx SEMI */.
2a55c 20 20 20 20 20 20 63 61 73 65 20 31 30 3a 20 2f        case 10: /
2a55d 2a 20 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20  * trans_opt ::= 
2a55e 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 31  */.      case 11
2a55f 3a 20 2f 2a 20 74 72 61 6e 73 5f 6f 70 74 20 3a  : /* trans_opt :
2a560 3a 3d 20 54 52 41 4e 53 41 43 54 49 4f 4e 20 2a  := TRANSACTION *
2a561 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 3a  /.      case 12:
2a562 20 2f 2a 20 74 72 61 6e 73 5f 6f 70 74 20 3a 3a   /* trans_opt ::
2a563 3d 20 54 52 41 4e 53 41 43 54 49 4f 4e 20 6e 6d  = TRANSACTION nm
2a564 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
2a565 30 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 63 72  0: /* cmd ::= cr
2a566 65 61 74 65 5f 74 61 62 6c 65 20 63 72 65 61 74  eate_table creat
2a567 65 5f 74 61 62 6c 65 5f 61 72 67 73 20 2a 2f 0a  e_table_args */.
2a568 20 20 20 20 20 20 63 61 73 65 20 32 38 3a 20 2f        case 28: /
2a569 2a 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 3a 3a 3d  * columnlist ::=
2a56a 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 43 4f 4d 4d   columnlist COMM
2a56b 41 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20  A column */.    
2a56c 20 20 63 61 73 65 20 32 39 3a 20 2f 2a 20 63 6f    case 29: /* co
2a56d 6c 75 6d 6e 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c  lumnlist ::= col
2a56e 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  umn */.      cas
2a56f 65 20 33 37 3a 20 2f 2a 20 74 79 70 65 20 3a 3a  e 37: /* type ::
2a570 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  = */.      case 
2a571 34 34 3a 20 2f 2a 20 73 69 67 6e 65 64 20 3a 3a  44: /* signed ::
2a572 3d 20 70 6c 75 73 5f 6e 75 6d 20 2a 2f 0a 20 20  = plus_num */.  
2a573 20 20 20 20 63 61 73 65 20 34 35 3a 20 2f 2a 20      case 45: /* 
2a574 73 69 67 6e 65 64 20 3a 3a 3d 20 6d 69 6e 75 73  signed ::= minus
2a575 5f 6e 75 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61  _num */.      ca
2a576 73 65 20 34 36 3a 20 2f 2a 20 63 61 72 67 6c 69  se 46: /* cargli
2a577 73 74 20 3a 3a 3d 20 63 61 72 67 6c 69 73 74 20  st ::= carglist 
2a578 63 61 72 67 20 2a 2f 0a 20 20 20 20 20 20 63 61  carg */.      ca
2a579 73 65 20 34 37 3a 20 2f 2a 20 63 61 72 67 6c 69  se 47: /* cargli
2a57a 73 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20  st ::= */.      
2a57b 63 61 73 65 20 34 38 3a 20 2f 2a 20 63 61 72 67  case 48: /* carg
2a57c 20 3a 3a 3d 20 43 4f 4e 53 54 52 41 49 4e 54 20   ::= CONSTRAINT 
2a57d 6e 6d 20 63 63 6f 6e 73 20 2a 2f 0a 20 20 20 20  nm ccons */.    
2a57e 20 20 63 61 73 65 20 34 39 3a 20 2f 2a 20 63 61    case 49: /* ca
2a57f 72 67 20 3a 3a 3d 20 63 63 6f 6e 73 20 2a 2f 0a  rg ::= ccons */.
2a580 20 20 20 20 20 20 63 61 73 65 20 35 35 3a 20 2f        case 55: /
2a581 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 4e 55 4c 4c  * ccons ::= NULL
2a582 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 20 20 20 20 20   onconf */.     
2a583 20 63 61 73 65 20 38 32 3a 20 2f 2a 20 63 6f 6e   case 82: /* con
2a584 73 6c 69 73 74 20 3a 3a 3d 20 63 6f 6e 73 6c 69  slist ::= consli
2a585 73 74 20 43 4f 4d 4d 41 20 74 63 6f 6e 73 20 2a  st COMMA tcons *
2a586 2f 0a 20 20 20 20 20 20 63 61 73 65 20 38 33 3a  /.      case 83:
2a587 20 2f 2a 20 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d   /* conslist ::=
2a588 20 63 6f 6e 73 6c 69 73 74 20 74 63 6f 6e 73 20   conslist tcons 
2a589 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 38 34  */.      case 84
2a58a 3a 20 2f 2a 20 63 6f 6e 73 6c 69 73 74 20 3a 3a  : /* conslist ::
2a58b 3d 20 74 63 6f 6e 73 20 2a 2f 0a 20 20 20 20 20  = tcons */.     
2a58c 20 63 61 73 65 20 38 35 3a 20 2f 2a 20 74 63 6f   case 85: /* tco
2a58d 6e 73 20 3a 3a 3d 20 43 4f 4e 53 54 52 41 49 4e  ns ::= CONSTRAIN
2a58e 54 20 6e 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61  T nm */.      ca
2a58f 73 65 20 32 36 30 3a 20 2f 2a 20 70 6c 75 73 5f  se 260: /* plus_
2a590 6f 70 74 20 3a 3a 3d 20 50 4c 55 53 20 2a 2f 0a  opt ::= PLUS */.
2a591 20 20 20 20 20 20 63 61 73 65 20 32 36 31 3a 20        case 261: 
2a592 2f 2a 20 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d 20  /* plus_opt ::= 
2a593 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 37  */.      case 27
2a594 31 3a 20 2f 2a 20 66 6f 72 65 61 63 68 5f 63 6c  1: /* foreach_cl
2a595 61 75 73 65 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20  ause ::= */.    
2a596 20 20 63 61 73 65 20 32 37 32 3a 20 2f 2a 20 66    case 272: /* f
2a597 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 20 3a 3a  oreach_clause ::
2a598 3d 20 46 4f 52 20 45 41 43 48 20 52 4f 57 20 2a  = FOR EACH ROW *
2a599 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 32  /.      case 292
2a59a 3a 20 2f 2a 20 64 61 74 61 62 61 73 65 5f 6b 77  : /* database_kw
2a59b 5f 6f 70 74 20 3a 3a 3d 20 44 41 54 41 42 41 53  _opt ::= DATABAS
2a59c 45 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  E */.      case 
2a59d 32 39 33 3a 20 2f 2a 20 64 61 74 61 62 61 73 65  293: /* database
2a59e 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20  _kw_opt ::= */. 
2a59f 20 20 20 20 20 63 61 73 65 20 33 30 31 3a 20 2f       case 301: /
2a5a0 2a 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 3a  * kwcolumn_opt :
2a5a1 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  := */.      case
2a5a2 20 33 30 32 3a 20 2f 2a 20 6b 77 63 6f 6c 75 6d   302: /* kwcolum
2a5a3 6e 5f 6f 70 74 20 3a 3a 3d 20 43 4f 4c 55 4d 4e  n_opt ::= COLUMN
2a5a4 4b 57 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  KW */.      case
2a5a5 20 33 30 36 3a 20 2f 2a 20 76 74 61 62 61 72 67   306: /* vtabarg
2a5a6 6c 69 73 74 20 3a 3a 3d 20 76 74 61 62 61 72 67  list ::= vtabarg
2a5a7 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33   */.      case 3
2a5a8 30 37 3a 20 2f 2a 20 76 74 61 62 61 72 67 6c 69  07: /* vtabargli
2a5a9 73 74 20 3a 3a 3d 20 76 74 61 62 61 72 67 6c 69  st ::= vtabargli
2a5aa 73 74 20 43 4f 4d 4d 41 20 76 74 61 62 61 72 67  st COMMA vtabarg
2a5ab 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33   */.      case 3
2a5ac 30 39 3a 20 2f 2a 20 76 74 61 62 61 72 67 20 3a  09: /* vtabarg :
2a5ad 3a 3d 20 76 74 61 62 61 72 67 20 76 74 61 62 61  := vtabarg vtaba
2a5ae 72 67 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 20  rgtoken */.     
2a5af 20 63 61 73 65 20 33 31 33 3a 20 2f 2a 20 61 6e   case 313: /* an
2a5b0 79 6c 69 73 74 20 3a 3a 3d 20 2a 2f 0a 7b 0a 7d  ylist ::= */.{.}
2a5b1 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2a5b2 20 20 20 20 20 20 63 61 73 65 20 35 3a 20 2f 2a        case 5: /*
2a5b3 20 65 78 70 6c 61 69 6e 20 3a 3a 3d 20 2a 2f 0a   explain ::= */.
2a5b4 7b 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61  { sqlite3BeginPa
2a5b5 72 73 65 28 70 50 61 72 73 65 2c 20 30 29 3b 20  rse(pParse, 0); 
2a5b6 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2a5b7 0a 20 20 20 20 20 20 63 61 73 65 20 36 3a 20 2f  .      case 6: /
2a5b8 2a 20 65 78 70 6c 61 69 6e 20 3a 3a 3d 20 45 58  * explain ::= EX
2a5b9 50 4c 41 49 4e 20 2a 2f 0a 7b 20 73 71 6c 69 74  PLAIN */.{ sqlit
2a5ba 65 33 42 65 67 69 6e 50 61 72 73 65 28 70 50 61  e3BeginParse(pPa
2a5bb 72 73 65 2c 20 31 29 3b 20 7d 0a 20 20 20 20 20  rse, 1); }.     
2a5bc 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2a5bd 63 61 73 65 20 37 3a 20 2f 2a 20 65 78 70 6c 61  case 7: /* expla
2a5be 69 6e 20 3a 3a 3d 20 45 58 50 4c 41 49 4e 20 51  in ::= EXPLAIN Q
2a5bf 55 45 52 59 20 50 4c 41 4e 20 2a 2f 0a 7b 20 73  UERY PLAN */.{ s
2a5c0 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72 73 65  qlite3BeginParse
2a5c1 28 70 50 61 72 73 65 2c 20 32 29 3b 20 7d 0a 20  (pParse, 2); }. 
2a5c2 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2a5c3 20 20 20 20 63 61 73 65 20 38 3a 20 2f 2a 20 63      case 8: /* c
2a5c4 6d 64 78 20 3a 3a 3d 20 63 6d 64 20 2a 2f 0a 7b  mdx ::= cmd */.{
2a5c5 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f   sqlite3FinishCo
2a5c6 64 69 6e 67 28 70 50 61 72 73 65 29 3b 20 7d 0a  ding(pParse); }.
2a5c7 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2a5c8 20 20 20 20 20 63 61 73 65 20 39 3a 20 2f 2a 20       case 9: /* 
2a5c9 63 6d 64 20 3a 3a 3d 20 42 45 47 49 4e 20 74 72  cmd ::= BEGIN tr
2a5ca 61 6e 73 74 79 70 65 20 74 72 61 6e 73 5f 6f 70  anstype trans_op
2a5cb 74 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 42 65 67  t */.{sqlite3Beg
2a5cc 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 50  inTransaction(pP
2a5cd 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e  arse, yymsp[-1].
2a5ce 6d 69 6e 6f 72 2e 79 79 33 31 36 29 3b 7d 0a 20  minor.yy316);}. 
2a5cf 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2a5d0 20 20 20 20 63 61 73 65 20 31 33 3a 20 2f 2a 20      case 13: /* 
2a5d1 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 2a 2f  transtype ::= */
2a5d2 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
2a5d3 33 31 36 20 3d 20 54 4b 5f 44 45 46 45 52 52 45  316 = TK_DEFERRE
2a5d4 44 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  D;}.        brea
2a5d5 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 34  k;.      case 14
2a5d6 3a 20 2f 2a 20 74 72 61 6e 73 74 79 70 65 20 3a  : /* transtype :
2a5d7 3a 3d 20 44 45 46 45 52 52 45 44 20 2a 2f 0a 20  := DEFERRED */. 
2a5d8 20 20 20 20 20 63 61 73 65 20 31 35 3a 20 2f 2a       case 15: /*
2a5d9 20 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 49   transtype ::= I
2a5da 4d 4d 45 44 49 41 54 45 20 2a 2f 0a 20 20 20 20  MMEDIATE */.    
2a5db 20 20 63 61 73 65 20 31 36 3a 20 2f 2a 20 74 72    case 16: /* tr
2a5dc 61 6e 73 74 79 70 65 20 3a 3a 3d 20 45 58 43 4c  anstype ::= EXCL
2a5dd 55 53 49 56 45 20 2a 2f 0a 20 20 20 20 20 20 63  USIVE */.      c
2a5de 61 73 65 20 31 30 37 3a 20 2f 2a 20 6d 75 6c 74  ase 107: /* mult
2a5df 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 55  iselect_op ::= U
2a5e0 4e 49 4f 4e 20 2a 2f 0a 20 20 20 20 20 20 63 61  NION */.      ca
2a5e1 73 65 20 31 30 39 3a 20 2f 2a 20 6d 75 6c 74 69  se 109: /* multi
2a5e2 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 45 58  select_op ::= EX
2a5e3 43 45 50 54 7c 49 4e 54 45 52 53 45 43 54 20 2a  CEPT|INTERSECT *
2a5e4 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
2a5e5 79 33 31 36 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  y316 = yymsp[0].
2a5e6 6d 61 6a 6f 72 3b 7d 0a 20 20 20 20 20 20 20 20  major;}.        
2a5e7 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2a5e8 65 20 31 37 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d  e 17: /* cmd ::=
2a5e9 20 43 4f 4d 4d 49 54 20 74 72 61 6e 73 5f 6f 70   COMMIT trans_op
2a5ea 74 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  t */.      case 
2a5eb 31 38 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 45  18: /* cmd ::= E
2a5ec 4e 44 20 74 72 61 6e 73 5f 6f 70 74 20 2a 2f 0a  ND trans_opt */.
2a5ed 7b 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 54 72  {sqlite3CommitTr
2a5ee 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 72 73 65  ansaction(pParse
2a5ef 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
2a5f0 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 39  k;.      case 19
2a5f1 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 4f 4c  : /* cmd ::= ROL
2a5f2 4c 42 41 43 4b 20 74 72 61 6e 73 5f 6f 70 74 20  LBACK trans_opt 
2a5f3 2a 2f 0a 7b 73 71 6c 69 74 65 33 52 6f 6c 6c 62  */.{sqlite3Rollb
2a5f4 61 63 6b 54 72 61 6e 73 61 63 74 69 6f 6e 28 70  ackTransaction(p
2a5f5 50 61 72 73 65 29 3b 7d 0a 20 20 20 20 20 20 20  Parse);}.       
2a5f6 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2a5f7 73 65 20 32 31 3a 20 2f 2a 20 63 72 65 61 74 65  se 21: /* create
2a5f8 5f 74 61 62 6c 65 20 3a 3a 3d 20 43 52 45 41 54  _table ::= CREAT
2a5f9 45 20 74 65 6d 70 20 54 41 42 4c 45 20 69 66 6e  E temp TABLE ifn
2a5fa 6f 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d  otexists nm dbnm
2a5fb 20 2a 2f 0a 7b 0a 20 20 20 73 71 6c 69 74 65 33   */.{.   sqlite3
2a5fc 53 74 61 72 74 54 61 62 6c 65 28 70 50 61 72 73  StartTable(pPars
2a5fd 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  e,&yymsp[-1].min
2a5fe 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d  or.yy0,&yymsp[0]
2a5ff 2e 6d 69 6e 6f 72 2e 79 79 30 2c 79 79 6d 73 70  .minor.yy0,yymsp
2a600 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36  [-4].minor.yy316
2a601 2c 30 2c 30 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d  ,0,0,yymsp[-2].m
2a602 69 6e 6f 72 2e 79 79 33 31 36 29 3b 0a 7d 0a 20  inor.yy316);.}. 
2a603 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2a604 20 20 20 20 63 61 73 65 20 32 32 3a 20 2f 2a 20      case 22: /* 
2a605 69 66 6e 6f 74 65 78 69 73 74 73 20 3a 3a 3d 20  ifnotexists ::= 
2a606 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 35  */.      case 25
2a607 3a 20 2f 2a 20 74 65 6d 70 20 3a 3a 3d 20 2a 2f  : /* temp ::= */
2a608 0a 20 20 20 20 20 20 63 61 73 65 20 36 33 3a 20  .      case 63: 
2a609 2f 2a 20 61 75 74 6f 69 6e 63 20 3a 3a 3d 20 2a  /* autoinc ::= *
2a60a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 37 37 3a  /.      case 77:
2a60b 20 2f 2a 20 69 6e 69 74 5f 64 65 66 65 72 72 65   /* init_deferre
2a60c 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d 20 2a  d_pred_opt ::= *
2a60d 2f 0a 20 20 20 20 20 20 63 61 73 65 20 37 39 3a  /.      case 79:
2a60e 20 2f 2a 20 69 6e 69 74 5f 64 65 66 65 72 72 65   /* init_deferre
2a60f 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d 20 49  d_pred_opt ::= I
2a610 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49 41  NITIALLY IMMEDIA
2a611 54 45 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  TE */.      case
2a612 20 39 30 3a 20 2f 2a 20 64 65 66 65 72 5f 73 75   90: /* defer_su
2a613 62 63 6c 61 75 73 65 5f 6f 70 74 20 3a 3a 3d 20  bclause_opt ::= 
2a614 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 30  */.      case 10
2a615 31 3a 20 2f 2a 20 69 66 65 78 69 73 74 73 20 3a  1: /* ifexists :
2a616 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  := */.      case
2a617 20 31 31 32 3a 20 2f 2a 20 64 69 73 74 69 6e 63   112: /* distinc
2a618 74 20 3a 3a 3d 20 41 4c 4c 20 2a 2f 0a 20 20 20  t ::= ALL */.   
2a619 20 20 20 63 61 73 65 20 31 31 33 3a 20 2f 2a 20     case 113: /* 
2a61a 64 69 73 74 69 6e 63 74 20 3a 3a 3d 20 2a 2f 0a  distinct ::= */.
2a61b 20 20 20 20 20 20 63 61 73 65 20 32 31 36 3a 20        case 216: 
2a61c 2f 2a 20 62 65 74 77 65 65 6e 5f 6f 70 20 3a 3a  /* between_op ::
2a61d 3d 20 42 45 54 57 45 45 4e 20 2a 2f 0a 20 20 20  = BETWEEN */.   
2a61e 20 20 20 63 61 73 65 20 32 31 39 3a 20 2f 2a 20     case 219: /* 
2a61f 69 6e 5f 6f 70 20 3a 3a 3d 20 49 4e 20 2a 2f 0a  in_op ::= IN */.
2a620 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  {yygotominor.yy3
2a621 31 36 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20  16 = 0;}.       
2a622 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2a623 73 65 20 32 33 3a 20 2f 2a 20 69 66 6e 6f 74 65  se 23: /* ifnote
2a624 78 69 73 74 73 20 3a 3a 3d 20 49 46 20 4e 4f 54  xists ::= IF NOT
2a625 20 45 58 49 53 54 53 20 2a 2f 0a 20 20 20 20 20   EXISTS */.     
2a626 20 63 61 73 65 20 32 34 3a 20 2f 2a 20 74 65 6d   case 24: /* tem
2a627 70 20 3a 3a 3d 20 54 45 4d 50 20 2a 2f 0a 20 20  p ::= TEMP */.  
2a628 20 20 20 20 63 61 73 65 20 36 34 3a 20 2f 2a 20      case 64: /* 
2a629 61 75 74 6f 69 6e 63 20 3a 3a 3d 20 41 55 54 4f  autoinc ::= AUTO
2a62a 49 4e 43 52 20 2a 2f 0a 20 20 20 20 20 20 63 61  INCR */.      ca
2a62b 73 65 20 37 38 3a 20 2f 2a 20 69 6e 69 74 5f 64  se 78: /* init_d
2a62c 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74  eferred_pred_opt
2a62d 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59 20 44   ::= INITIALLY D
2a62e 45 46 45 52 52 45 44 20 2a 2f 0a 20 20 20 20 20  EFERRED */.     
2a62f 20 63 61 73 65 20 31 30 30 3a 20 2f 2a 20 69 66   case 100: /* if
2a630 65 78 69 73 74 73 20 3a 3a 3d 20 49 46 20 45 58  exists ::= IF EX
2a631 49 53 54 53 20 2a 2f 0a 20 20 20 20 20 20 63 61  ISTS */.      ca
2a632 73 65 20 31 31 31 3a 20 2f 2a 20 64 69 73 74 69  se 111: /* disti
2a633 6e 63 74 20 3a 3a 3d 20 44 49 53 54 49 4e 43 54  nct ::= DISTINCT
2a634 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
2a635 31 37 3a 20 2f 2a 20 62 65 74 77 65 65 6e 5f 6f  17: /* between_o
2a636 70 20 3a 3a 3d 20 4e 4f 54 20 42 45 54 57 45 45  p ::= NOT BETWEE
2a637 4e 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  N */.      case 
2a638 32 32 30 3a 20 2f 2a 20 69 6e 5f 6f 70 20 3a 3a  220: /* in_op ::
2a639 3d 20 4e 4f 54 20 49 4e 20 2a 2f 0a 7b 79 79 67  = NOT IN */.{yyg
2a63a 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 31 36 20 3d  otominor.yy316 =
2a63b 20 31 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65   1;}.        bre
2a63c 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2a63d 36 3a 20 2f 2a 20 63 72 65 61 74 65 5f 74 61 62  6: /* create_tab
2a63e 6c 65 5f 61 72 67 73 20 3a 3a 3d 20 4c 50 20 63  le_args ::= LP c
2a63f 6f 6c 75 6d 6e 6c 69 73 74 20 63 6f 6e 73 6c 69  olumnlist consli
2a640 73 74 5f 6f 70 74 20 52 50 20 2a 2f 0a 7b 0a 20  st_opt RP */.{. 
2a641 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65   sqlite3EndTable
2a642 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d  (pParse,&yymsp[-
2a643 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  1].minor.yy0,&yy
2a644 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
2a645 2c 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  ,0);.}.        b
2a646 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2a647 20 32 37 3a 20 2f 2a 20 63 72 65 61 74 65 5f 74   27: /* create_t
2a648 61 62 6c 65 5f 61 72 67 73 20 3a 3a 3d 20 41 53  able_args ::= AS
2a649 20 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 73   select */.{.  s
2a64a 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28 70  qlite3EndTable(p
2a64b 50 61 72 73 65 2c 30 2c 30 2c 79 79 6d 73 70 5b  Parse,0,0,yymsp[
2a64c 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 33 29 3b 0a  0].minor.yy43);.
2a64d 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
2a64e 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
2a64f 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
2a650 2e 79 79 34 33 29 3b 0a 7d 0a 20 20 20 20 20 20  .yy43);.}.      
2a651 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2a652 61 73 65 20 33 30 3a 20 2f 2a 20 63 6f 6c 75 6d  ase 30: /* colum
2a653 6e 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 69 64 20 74  n ::= columnid t
2a654 79 70 65 20 63 61 72 67 6c 69 73 74 20 2a 2f 0a  ype carglist */.
2a655 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  {.  yygotominor.
2a656 79 79 30 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 32  yy0.z = yymsp[-2
2a657 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20  ].minor.yy0.z;. 
2a658 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30   yygotominor.yy0
2a659 2e 6e 20 3d 20 28 70 50 61 72 73 65 2d 3e 73 4c  .n = (pParse->sL
2a65a 61 73 74 54 6f 6b 65 6e 2e 7a 2d 79 79 6d 73 70  astToken.z-yymsp
2a65b 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  [-2].minor.yy0.z
2a65c 29 20 2b 20 70 50 61 72 73 65 2d 3e 73 4c 61 73  ) + pParse->sLas
2a65d 74 54 6f 6b 65 6e 2e 6e 3b 0a 7d 0a 20 20 20 20  tToken.n;.}.    
2a65e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2a65f 20 63 61 73 65 20 33 31 3a 20 2f 2a 20 63 6f 6c   case 31: /* col
2a660 75 6d 6e 69 64 20 3a 3a 3d 20 6e 6d 20 2a 2f 0a  umnid ::= nm */.
2a661 7b 0a 20 20 73 71 6c 69 74 65 33 41 64 64 43 6f  {.  sqlite3AddCo
2a662 6c 75 6d 6e 28 70 50 61 72 73 65 2c 26 79 79 6d  lumn(pParse,&yym
2a663 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
2a664 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.  yygotominor.
2a665 79 79 30 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  yy0 = yymsp[0].m
2a666 69 6e 6f 72 2e 79 79 30 3b 0a 7d 0a 20 20 20 20  inor.yy0;.}.    
2a667 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2a668 20 63 61 73 65 20 33 32 3a 20 2f 2a 20 69 64 20   case 32: /* id 
2a669 3a 3a 3d 20 49 44 20 2a 2f 0a 20 20 20 20 20 20  ::= ID */.      
2a66a 63 61 73 65 20 33 33 3a 20 2f 2a 20 69 64 73 20  case 33: /* ids 
2a66b 3a 3a 3d 20 49 44 7c 53 54 52 49 4e 47 20 2a 2f  ::= ID|STRING */
2a66c 0a 20 20 20 20 20 20 63 61 73 65 20 33 34 3a 20  .      case 34: 
2a66d 2f 2a 20 6e 6d 20 3a 3a 3d 20 49 44 20 2a 2f 0a  /* nm ::= ID */.
2a66e 20 20 20 20 20 20 63 61 73 65 20 33 35 3a 20 2f        case 35: /
2a66f 2a 20 6e 6d 20 3a 3a 3d 20 53 54 52 49 4e 47 20  * nm ::= STRING 
2a670 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33 36  */.      case 36
2a671 3a 20 2f 2a 20 6e 6d 20 3a 3a 3d 20 4a 4f 49 4e  : /* nm ::= JOIN
2a672 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  _KW */.      cas
2a673 65 20 33 39 3a 20 2f 2a 20 74 79 70 65 74 6f 6b  e 39: /* typetok
2a674 65 6e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 20  en ::= typename 
2a675 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 34 32  */.      case 42
2a676 3a 20 2f 2a 20 74 79 70 65 6e 61 6d 65 20 3a 3a  : /* typename ::
2a677 3d 20 69 64 73 20 2a 2f 0a 20 20 20 20 20 20 63  = ids */.      c
2a678 61 73 65 20 31 31 39 3a 20 2f 2a 20 61 73 20 3a  ase 119: /* as :
2a679 3a 3d 20 41 53 20 6e 6d 20 2a 2f 0a 20 20 20 20  := AS nm */.    
2a67a 20 20 63 61 73 65 20 31 32 30 3a 20 2f 2a 20 61    case 120: /* a
2a67b 73 20 3a 3a 3d 20 69 64 73 20 2a 2f 0a 20 20 20  s ::= ids */.   
2a67c 20 20 20 63 61 73 65 20 31 33 31 3a 20 2f 2a 20     case 131: /* 
2a67d 64 62 6e 6d 20 3a 3a 3d 20 44 4f 54 20 6e 6d 20  dbnm ::= DOT nm 
2a67e 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 34  */.      case 14
2a67f 30 3a 20 2f 2a 20 69 6e 64 65 78 65 64 5f 6f 70  0: /* indexed_op
2a680 74 20 3a 3a 3d 20 49 4e 44 45 58 45 44 20 42 59  t ::= INDEXED BY
2a681 20 6e 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73   nm */.      cas
2a682 65 20 32 34 35 3a 20 2f 2a 20 63 6f 6c 6c 61 74  e 245: /* collat
2a683 65 20 3a 3a 3d 20 43 4f 4c 4c 41 54 45 20 69 64  e ::= COLLATE id
2a684 73 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  s */.      case 
2a685 32 35 35 3a 20 2f 2a 20 6e 6d 6e 75 6d 20 3a 3a  255: /* nmnum ::
2a686 3d 20 70 6c 75 73 5f 6e 75 6d 20 2a 2f 0a 20 20  = plus_num */.  
2a687 20 20 20 20 63 61 73 65 20 32 35 36 3a 20 2f 2a      case 256: /*
2a688 20 6e 6d 6e 75 6d 20 3a 3a 3d 20 6e 6d 20 2a 2f   nmnum ::= nm */
2a689 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 37 3a  .      case 257:
2a68a 20 2f 2a 20 70 6c 75 73 5f 6e 75 6d 20 3a 3a 3d   /* plus_num ::=
2a68b 20 70 6c 75 73 5f 6f 70 74 20 6e 75 6d 62 65 72   plus_opt number
2a68c 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
2a68d 35 38 3a 20 2f 2a 20 6d 69 6e 75 73 5f 6e 75 6d  58: /* minus_num
2a68e 20 3a 3a 3d 20 4d 49 4e 55 53 20 6e 75 6d 62 65   ::= MINUS numbe
2a68f 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  r */.      case 
2a690 32 35 39 3a 20 2f 2a 20 6e 75 6d 62 65 72 20 3a  259: /* number :
2a691 3a 3d 20 49 4e 54 45 47 45 52 7c 46 4c 4f 41 54  := INTEGER|FLOAT
2a692 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2a693 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  .yy0 = yymsp[0].
2a694 6d 69 6e 6f 72 2e 79 79 30 3b 7d 0a 20 20 20 20  minor.yy0;}.    
2a695 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2a696 20 63 61 73 65 20 33 38 3a 20 2f 2a 20 74 79 70   case 38: /* typ
2a697 65 20 3a 3a 3d 20 74 79 70 65 74 6f 6b 65 6e 20  e ::= typetoken 
2a698 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 43 6f  */.{sqlite3AddCo
2a699 6c 75 6d 6e 54 79 70 65 28 70 50 61 72 73 65 2c  lumnType(pParse,
2a69a 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2a69b 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  yy0);}.        b
2a69c 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2a69d 20 34 30 3a 20 2f 2a 20 74 79 70 65 74 6f 6b 65   40: /* typetoke
2a69e 6e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 20 4c  n ::= typename L
2a69f 50 20 73 69 67 6e 65 64 20 52 50 20 2a 2f 0a 7b  P signed RP */.{
2a6a0 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
2a6a1 79 30 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 33 5d  y0.z = yymsp[-3]
2a6a2 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20  .minor.yy0.z;.  
2a6a3 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e  yygotominor.yy0.
2a6a4 6e 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  n = &yymsp[0].mi
2a6a5 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b  nor.yy0.z[yymsp[
2a6a6 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 20  0].minor.yy0.n] 
2a6a7 2d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  - yymsp[-3].mino
2a6a8 72 2e 79 79 30 2e 7a 3b 0a 7d 0a 20 20 20 20 20  r.yy0.z;.}.     
2a6a9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2a6aa 63 61 73 65 20 34 31 3a 20 2f 2a 20 74 79 70 65  case 41: /* type
2a6ab 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e 61  token ::= typena
2a6ac 6d 65 20 4c 50 20 73 69 67 6e 65 64 20 43 4f 4d  me LP signed COM
2a6ad 4d 41 20 73 69 67 6e 65 64 20 52 50 20 2a 2f 0a  MA signed RP */.
2a6ae 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  {.  yygotominor.
2a6af 79 79 30 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 35  yy0.z = yymsp[-5
2a6b0 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20  ].minor.yy0.z;. 
2a6b1 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30   yygotominor.yy0
2a6b2 2e 6e 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  .n = &yymsp[0].m
2a6b3 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70  inor.yy0.z[yymsp
2a6b4 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d  [0].minor.yy0.n]
2a6b5 20 2d 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e   - yymsp[-5].min
2a6b6 6f 72 2e 79 79 30 2e 7a 3b 0a 7d 0a 20 20 20 20  or.yy0.z;.}.    
2a6b7 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2a6b8 20 63 61 73 65 20 34 33 3a 20 2f 2a 20 74 79 70   case 43: /* typ
2a6b9 65 6e 61 6d 65 20 3a 3a 3d 20 74 79 70 65 6e 61  ename ::= typena
2a6ba 6d 65 20 69 64 73 20 2a 2f 0a 7b 79 79 67 6f 74  me ids */.{yygot
2a6bb 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3d 79 79 6d  ominor.yy0.z=yym
2a6bc 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
2a6bd 2e 7a 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  .z; yygotominor.
2a6be 79 79 30 2e 6e 3d 79 79 6d 73 70 5b 30 5d 2e 6d  yy0.n=yymsp[0].m
2a6bf 69 6e 6f 72 2e 79 79 30 2e 6e 2b 28 79 79 6d 73  inor.yy0.n+(yyms
2a6c0 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  p[0].minor.yy0.z
2a6c1 2d 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  -yymsp[-1].minor
2a6c2 2e 79 79 30 2e 7a 29 3b 7d 0a 20 20 20 20 20 20  .yy0.z);}.      
2a6c3 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2a6c4 61 73 65 20 35 30 3a 20 2f 2a 20 63 63 6f 6e 73  ase 50: /* ccons
2a6c5 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 74 65 72   ::= DEFAULT ter
2a6c6 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  m */.      case 
2a6c7 35 32 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d  52: /* ccons ::=
2a6c8 20 44 45 46 41 55 4c 54 20 50 4c 55 53 20 74 65   DEFAULT PLUS te
2a6c9 72 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64  rm */.{sqlite3Ad
2a6ca 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28 70 50  dDefaultValue(pP
2a6cb 61 72 73 65 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  arse,yymsp[0].mi
2a6cc 6e 6f 72 2e 79 79 34 35 30 29 3b 7d 0a 20 20 20  nor.yy450);}.   
2a6cd 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2a6ce 20 20 63 61 73 65 20 35 31 3a 20 2f 2a 20 63 63    case 51: /* cc
2a6cf 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20  ons ::= DEFAULT 
2a6d0 4c 50 20 65 78 70 72 20 52 50 20 2a 2f 0a 7b 73  LP expr RP */.{s
2a6d1 71 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c 74  qlite3AddDefault
2a6d2 56 61 6c 75 65 28 70 50 61 72 73 65 2c 79 79 6d  Value(pParse,yym
2a6d3 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-1].minor.yy4
2a6d4 35 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  50);}.        br
2a6d5 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2a6d6 35 33 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d  53: /* ccons ::=
2a6d7 20 44 45 46 41 55 4c 54 20 4d 49 4e 55 53 20 74   DEFAULT MINUS t
2a6d8 65 72 6d 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20  erm */.{.  Expr 
2a6d9 2a 70 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  *p = sqlite3PExp
2a6da 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 55 4d 49  r(pParse, TK_UMI
2a6db 4e 55 53 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  NUS, yymsp[0].mi
2a6dc 6e 6f 72 2e 79 79 34 35 30 2c 20 30 2c 20 30 29  nor.yy450, 0, 0)
2a6dd 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 53  ;.  sqlite3ExprS
2a6de 70 61 6e 28 70 2c 26 79 79 6d 73 70 5b 2d 31 5d  pan(p,&yymsp[-1]
2a6df 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
2a6e0 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30  p[0].minor.yy450
2a6e1 2d 3e 73 70 61 6e 29 3b 0a 20 20 73 71 6c 69 74  ->span);.  sqlit
2a6e2 65 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75  e3AddDefaultValu
2a6e3 65 28 70 50 61 72 73 65 2c 70 29 3b 0a 7d 0a 20  e(pParse,p);.}. 
2a6e4 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2a6e5 20 20 20 20 63 61 73 65 20 35 34 3a 20 2f 2a 20      case 54: /* 
2a6e6 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c  ccons ::= DEFAUL
2a6e7 54 20 69 64 20 2a 2f 0a 7b 0a 20 20 45 78 70 72  T id */.{.  Expr
2a6e8 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 50 45 78   *p = sqlite3PEx
2a6e9 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 53 54  pr(pParse, TK_ST
2a6ea 52 49 4e 47 2c 20 30 2c 20 30 2c 20 26 79 79 6d  RING, 0, 0, &yym
2a6eb 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
2a6ec 3b 0a 20 20 73 71 6c 69 74 65 33 41 64 64 44 65  ;.  sqlite3AddDe
2a6ed 66 61 75 6c 74 56 61 6c 75 65 28 70 50 61 72 73  faultValue(pPars
2a6ee 65 2c 70 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  e,p);.}.        
2a6ef 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2a6f0 65 20 35 36 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a  e 56: /* ccons :
2a6f1 3a 3d 20 4e 4f 54 20 4e 55 4c 4c 20 6f 6e 63 6f  := NOT NULL onco
2a6f2 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64  nf */.{sqlite3Ad
2a6f3 64 4e 6f 74 4e 75 6c 6c 28 70 50 61 72 73 65 2c  dNotNull(pParse,
2a6f4 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2a6f5 79 79 33 31 36 29 3b 7d 0a 20 20 20 20 20 20 20  yy316);}.       
2a6f6 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2a6f7 73 65 20 35 37 3a 20 2f 2a 20 63 63 6f 6e 73 20  se 57: /* ccons 
2a6f8 3a 3a 3d 20 50 52 49 4d 41 52 59 20 4b 45 59 20  ::= PRIMARY KEY 
2a6f9 73 6f 72 74 6f 72 64 65 72 20 6f 6e 63 6f 6e 66  sortorder onconf
2a6fa 20 61 75 74 6f 69 6e 63 20 2a 2f 0a 7b 73 71 6c   autoinc */.{sql
2a6fb 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65  ite3AddPrimaryKe
2a6fc 79 28 70 50 61 72 73 65 2c 30 2c 79 79 6d 73 70  y(pParse,0,yymsp
2a6fd 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36  [-1].minor.yy316
2a6fe 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  ,yymsp[0].minor.
2a6ff 79 79 33 31 36 2c 79 79 6d 73 70 5b 2d 32 5d 2e  yy316,yymsp[-2].
2a700 6d 69 6e 6f 72 2e 79 79 33 31 36 29 3b 7d 0a 20  minor.yy316);}. 
2a701 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2a702 20 20 20 20 63 61 73 65 20 35 38 3a 20 2f 2a 20      case 58: /* 
2a703 63 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55 45  ccons ::= UNIQUE
2a704 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69   onconf */.{sqli
2a705 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 28 70  te3CreateIndex(p
2a706 50 61 72 73 65 2c 30 2c 30 2c 30 2c 30 2c 79 79  Parse,0,0,0,0,yy
2a707 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
2a708 31 36 2c 30 2c 30 2c 30 2c 30 29 3b 7d 0a 20 20  16,0,0,0,0);}.  
2a709 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2a70a 20 20 20 63 61 73 65 20 35 39 3a 20 2f 2a 20 63     case 59: /* c
2a70b 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43 4b 20 4c  cons ::= CHECK L
2a70c 50 20 65 78 70 72 20 52 50 20 2a 2f 0a 7b 73 71  P expr RP */.{sq
2a70d 6c 69 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e  lite3AddCheckCon
2a70e 73 74 72 61 69 6e 74 28 70 50 61 72 73 65 2c 79  straint(pParse,y
2a70f 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2a710 79 34 35 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  y450);}.        
2a711 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2a712 65 20 36 30 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a  e 60: /* ccons :
2a713 3a 3d 20 52 45 46 45 52 45 4e 43 45 53 20 6e 6d  := REFERENCES nm
2a714 20 69 64 78 6c 69 73 74 5f 6f 70 74 20 72 65 66   idxlist_opt ref
2a715 61 72 67 73 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  args */.{sqlite3
2a716 43 72 65 61 74 65 46 6f 72 65 69 67 6e 4b 65 79  CreateForeignKey
2a717 28 70 50 61 72 73 65 2c 30 2c 26 79 79 6d 73 70  (pParse,0,&yymsp
2a718 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 79  [-2].minor.yy0,y
2a719 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2a71a 79 32 34 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  y242,yymsp[0].mi
2a71b 6e 6f 72 2e 79 79 33 31 36 29 3b 7d 0a 20 20 20  nor.yy316);}.   
2a71c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2a71d 20 20 63 61 73 65 20 36 31 3a 20 2f 2a 20 63 63    case 61: /* cc
2a71e 6f 6e 73 20 3a 3a 3d 20 64 65 66 65 72 5f 73 75  ons ::= defer_su
2a71f 62 63 6c 61 75 73 65 20 2a 2f 0a 7b 73 71 6c 69  bclause */.{sqli
2a720 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e 4b  te3DeferForeignK
2a721 65 79 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  ey(pParse,yymsp[
2a722 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 29 3b  0].minor.yy316);
2a723 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2a724 0a 20 20 20 20 20 20 63 61 73 65 20 36 32 3a 20  .      case 62: 
2a725 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 43 4f 4c  /* ccons ::= COL
2a726 4c 41 54 45 20 69 64 73 20 2a 2f 0a 7b 73 71 6c  LATE ids */.{sql
2a727 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79  ite3AddCollateTy
2a728 70 65 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73  pe(pParse, &yyms
2a729 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
2a72a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2a72b 0a 20 20 20 20 20 20 63 61 73 65 20 36 35 3a 20  .      case 65: 
2a72c 2f 2a 20 72 65 66 61 72 67 73 20 3a 3a 3d 20 2a  /* refargs ::= *
2a72d 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  /.{ yygotominor.
2a72e 79 79 33 31 36 20 3d 20 4f 45 5f 52 65 73 74 72  yy316 = OE_Restr
2a72f 69 63 74 20 2a 20 30 78 30 31 30 31 30 31 3b 20  ict * 0x010101; 
2a730 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2a731 0a 20 20 20 20 20 20 63 61 73 65 20 36 36 3a 20  .      case 66: 
2a732 2f 2a 20 72 65 66 61 72 67 73 20 3a 3a 3d 20 72  /* refargs ::= r
2a733 65 66 61 72 67 73 20 72 65 66 61 72 67 20 2a 2f  efargs refarg */
2a734 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
2a735 79 33 31 36 20 3d 20 28 79 79 6d 73 70 5b 2d 31  y316 = (yymsp[-1
2a736 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 20 26 20  ].minor.yy316 & 
2a737 7e 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  ~yymsp[0].minor.
2a738 79 79 32 30 37 2e 6d 61 73 6b 29 20 7c 20 79 79  yy207.mask) | yy
2a739 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32  msp[0].minor.yy2
2a73a 30 37 2e 76 61 6c 75 65 3b 20 7d 0a 20 20 20 20  07.value; }.    
2a73b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2a73c 20 63 61 73 65 20 36 37 3a 20 2f 2a 20 72 65 66   case 67: /* ref
2a73d 61 72 67 20 3a 3a 3d 20 4d 41 54 43 48 20 6e 6d  arg ::= MATCH nm
2a73e 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f   */.{ yygotomino
2a73f 72 2e 79 79 32 30 37 2e 76 61 6c 75 65 20 3d 20  r.yy207.value = 
2a740 30 3b 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  0;     yygotomin
2a741 6f 72 2e 79 79 32 30 37 2e 6d 61 73 6b 20 3d 20  or.yy207.mask = 
2a742 30 78 30 30 30 30 30 30 3b 20 7d 0a 20 20 20 20  0x000000; }.    
2a743 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2a744 20 63 61 73 65 20 36 38 3a 20 2f 2a 20 72 65 66   case 68: /* ref
2a745 61 72 67 20 3a 3a 3d 20 4f 4e 20 44 45 4c 45 54  arg ::= ON DELET
2a746 45 20 72 65 66 61 63 74 20 2a 2f 0a 7b 20 79 79  E refact */.{ yy
2a747 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 30 37 2e  gotominor.yy207.
2a748 76 61 6c 75 65 20 3d 20 79 79 6d 73 70 5b 30 5d  value = yymsp[0]
2a749 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 3b 20 20 20  .minor.yy316;   
2a74a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
2a74b 32 30 37 2e 6d 61 73 6b 20 3d 20 30 78 30 30 30  207.mask = 0x000
2a74c 30 66 66 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  0ff; }.        b
2a74d 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2a74e 20 36 39 3a 20 2f 2a 20 72 65 66 61 72 67 20 3a   69: /* refarg :
2a74f 3a 3d 20 4f 4e 20 55 50 44 41 54 45 20 72 65 66  := ON UPDATE ref
2a750 61 63 74 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d  act */.{ yygotom
2a751 69 6e 6f 72 2e 79 79 32 30 37 2e 76 61 6c 75 65  inor.yy207.value
2a752 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
2a753 72 2e 79 79 33 31 36 3c 3c 38 3b 20 20 79 79 67  r.yy316<<8;  yyg
2a754 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 30 37 2e 6d  otominor.yy207.m
2a755 61 73 6b 20 3d 20 30 78 30 30 66 66 30 30 3b 20  ask = 0x00ff00; 
2a756 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2a757 0a 20 20 20 20 20 20 63 61 73 65 20 37 30 3a 20  .      case 70: 
2a758 2f 2a 20 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e  /* refarg ::= ON
2a759 20 49 4e 53 45 52 54 20 72 65 66 61 63 74 20 2a   INSERT refact *
2a75a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  /.{ yygotominor.
2a75b 79 79 32 30 37 2e 76 61 6c 75 65 20 3d 20 79 79  yy207.value = yy
2a75c 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
2a75d 31 36 3c 3c 31 36 3b 20 79 79 67 6f 74 6f 6d 69  16<<16; yygotomi
2a75e 6e 6f 72 2e 79 79 32 30 37 2e 6d 61 73 6b 20 3d  nor.yy207.mask =
2a75f 20 30 78 66 66 30 30 30 30 3b 20 7d 0a 20 20 20   0xff0000; }.   
2a760 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2a761 20 20 63 61 73 65 20 37 31 3a 20 2f 2a 20 72 65    case 71: /* re
2a762 66 61 63 74 20 3a 3a 3d 20 53 45 54 20 4e 55 4c  fact ::= SET NUL
2a763 4c 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  L */.{ yygotomin
2a764 6f 72 2e 79 79 33 31 36 20 3d 20 4f 45 5f 53 65  or.yy316 = OE_Se
2a765 74 4e 75 6c 6c 3b 20 7d 0a 20 20 20 20 20 20 20  tNull; }.       
2a766 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2a767 73 65 20 37 32 3a 20 2f 2a 20 72 65 66 61 63 74  se 72: /* refact
2a768 20 3a 3a 3d 20 53 45 54 20 44 45 46 41 55 4c 54   ::= SET DEFAULT
2a769 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f   */.{ yygotomino
2a76a 72 2e 79 79 33 31 36 20 3d 20 4f 45 5f 53 65 74  r.yy316 = OE_Set
2a76b 44 66 6c 74 3b 20 7d 0a 20 20 20 20 20 20 20 20  Dflt; }.        
2a76c 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2a76d 65 20 37 33 3a 20 2f 2a 20 72 65 66 61 63 74 20  e 73: /* refact 
2a76e 3a 3a 3d 20 43 41 53 43 41 44 45 20 2a 2f 0a 7b  ::= CASCADE */.{
2a76f 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
2a770 31 36 20 3d 20 4f 45 5f 43 61 73 63 61 64 65 3b  16 = OE_Cascade;
2a771 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
2a772 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37 34 3a  ;.      case 74:
2a773 20 2f 2a 20 72 65 66 61 63 74 20 3a 3a 3d 20 52   /* refact ::= R
2a774 45 53 54 52 49 43 54 20 2a 2f 0a 7b 20 79 79 67  ESTRICT */.{ yyg
2a775 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 31 36 20 3d  otominor.yy316 =
2a776 20 4f 45 5f 52 65 73 74 72 69 63 74 3b 20 7d 0a   OE_Restrict; }.
2a777 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2a778 20 20 20 20 20 63 61 73 65 20 37 35 3a 20 2f 2a       case 75: /*
2a779 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65   defer_subclause
2a77a 20 3a 3a 3d 20 4e 4f 54 20 44 45 46 45 52 52 41   ::= NOT DEFERRA
2a77b 42 4c 45 20 69 6e 69 74 5f 64 65 66 65 72 72 65  BLE init_deferre
2a77c 64 5f 70 72 65 64 5f 6f 70 74 20 2a 2f 0a 20 20  d_pred_opt */.  
2a77d 20 20 20 20 63 61 73 65 20 37 36 3a 20 2f 2a 20      case 76: /* 
2a77e 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20  defer_subclause 
2a77f 3a 3a 3d 20 44 45 46 45 52 52 41 42 4c 45 20 69  ::= DEFERRABLE i
2a780 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65  nit_deferred_pre
2a781 64 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 20 20 63  d_opt */.      c
2a782 61 73 65 20 39 31 3a 20 2f 2a 20 64 65 66 65 72  ase 91: /* defer
2a783 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 20 3a  _subclause_opt :
2a784 3a 3d 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75  := defer_subclau
2a785 73 65 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  se */.      case
2a786 20 39 33 3a 20 2f 2a 20 6f 6e 63 6f 6e 66 20 3a   93: /* onconf :
2a787 3a 3d 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 72  := ON CONFLICT r
2a788 65 73 6f 6c 76 65 74 79 70 65 20 2a 2f 0a 20 20  esolvetype */.  
2a789 20 20 20 20 63 61 73 65 20 39 35 3a 20 2f 2a 20      case 95: /* 
2a78a 6f 72 63 6f 6e 66 20 3a 3a 3d 20 4f 52 20 72 65  orconf ::= OR re
2a78b 73 6f 6c 76 65 74 79 70 65 20 2a 2f 0a 20 20 20  solvetype */.   
2a78c 20 20 20 63 61 73 65 20 39 36 3a 20 2f 2a 20 72     case 96: /* r
2a78d 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d 20 72  esolvetype ::= r
2a78e 61 69 73 65 74 79 70 65 20 2a 2f 0a 20 20 20 20  aisetype */.    
2a78f 20 20 63 61 73 65 20 31 36 39 3a 20 2f 2a 20 69    case 169: /* i
2a790 6e 73 65 72 74 5f 63 6d 64 20 3a 3a 3d 20 49 4e  nsert_cmd ::= IN
2a791 53 45 52 54 20 6f 72 63 6f 6e 66 20 2a 2f 0a 7b  SERT orconf */.{
2a792 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 31  yygotominor.yy31
2a793 36 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  6 = yymsp[0].min
2a794 6f 72 2e 79 79 33 31 36 3b 7d 0a 20 20 20 20 20  or.yy316;}.     
2a795 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2a796 63 61 73 65 20 38 30 3a 20 2f 2a 20 63 6f 6e 73  case 80: /* cons
2a797 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a  list_opt ::= */.
2a798 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30  {yygotominor.yy0
2a799 2e 6e 20 3d 20 30 3b 20 79 79 67 6f 74 6f 6d 69  .n = 0; yygotomi
2a79a 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20 30 3b 7d 0a  nor.yy0.z = 0;}.
2a79b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2a79c 20 20 20 20 20 63 61 73 65 20 38 31 3a 20 2f 2a       case 81: /*
2a79d 20 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 20 3a 3a   conslist_opt ::
2a79e 3d 20 43 4f 4d 4d 41 20 63 6f 6e 73 6c 69 73 74  = COMMA conslist
2a79f 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2a7a0 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b 2d 31 5d  .yy0 = yymsp[-1]
2a7a1 2e 6d 69 6e 6f 72 2e 79 79 30 3b 7d 0a 20 20 20  .minor.yy0;}.   
2a7a2 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2a7a3 20 20 63 61 73 65 20 38 36 3a 20 2f 2a 20 74 63    case 86: /* tc
2a7a4 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d 41 52 59 20  ons ::= PRIMARY 
2a7a5 4b 45 59 20 4c 50 20 69 64 78 6c 69 73 74 20 61  KEY LP idxlist a
2a7a6 75 74 6f 69 6e 63 20 52 50 20 6f 6e 63 6f 6e 66  utoinc RP onconf
2a7a7 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 50   */.{sqlite3AddP
2a7a8 72 69 6d 61 72 79 4b 65 79 28 70 50 61 72 73 65  rimaryKey(pParse
2a7a9 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  ,yymsp[-3].minor
2a7aa 2e 79 79 32 34 32 2c 79 79 6d 73 70 5b 30 5d 2e  .yy242,yymsp[0].
2a7ab 6d 69 6e 6f 72 2e 79 79 33 31 36 2c 79 79 6d 73  minor.yy316,yyms
2a7ac 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31  p[-2].minor.yy31
2a7ad 36 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  6,0);}.        b
2a7ae 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2a7af 20 38 37 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a   87: /* tcons ::
2a7b0 3d 20 55 4e 49 51 55 45 20 4c 50 20 69 64 78 6c  = UNIQUE LP idxl
2a7b1 69 73 74 20 52 50 20 6f 6e 63 6f 6e 66 20 2a 2f  ist RP onconf */
2a7b2 0a 7b 73 71 6c 69 74 65 33 43 72 65 61 74 65 49  .{sqlite3CreateI
2a7b3 6e 64 65 78 28 70 50 61 72 73 65 2c 30 2c 30 2c  ndex(pParse,0,0,
2a7b4 30 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  0,yymsp[-2].mino
2a7b5 72 2e 79 79 32 34 32 2c 79 79 6d 73 70 5b 30 5d  r.yy242,yymsp[0]
2a7b6 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 2c 30 2c 30  .minor.yy316,0,0
2a7b7 2c 30 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  ,0,0);}.        
2a7b8 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2a7b9 65 20 38 38 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a  e 88: /* tcons :
2a7ba 3a 3d 20 43 48 45 43 4b 20 4c 50 20 65 78 70 72  := CHECK LP expr
2a7bb 20 52 50 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73   RP onconf */.{s
2a7bc 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b 43 6f  qlite3AddCheckCo
2a7bd 6e 73 74 72 61 69 6e 74 28 70 50 61 72 73 65 2c  nstraint(pParse,
2a7be 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2a7bf 79 79 34 35 30 29 3b 7d 0a 20 20 20 20 20 20 20  yy450);}.       
2a7c0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2a7c1 73 65 20 38 39 3a 20 2f 2a 20 74 63 6f 6e 73 20  se 89: /* tcons 
2a7c2 3a 3a 3d 20 46 4f 52 45 49 47 4e 20 4b 45 59 20  ::= FOREIGN KEY 
2a7c3 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20 52 45  LP idxlist RP RE
2a7c4 46 45 52 45 4e 43 45 53 20 6e 6d 20 69 64 78 6c  FERENCES nm idxl
2a7c5 69 73 74 5f 6f 70 74 20 72 65 66 61 72 67 73 20  ist_opt refargs 
2a7c6 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f  defer_subclause_
2a7c7 6f 70 74 20 2a 2f 0a 7b 0a 20 20 20 20 73 71 6c  opt */.{.    sql
2a7c8 69 74 65 33 43 72 65 61 74 65 46 6f 72 65 69 67  ite3CreateForeig
2a7c9 6e 4b 65 79 28 70 50 61 72 73 65 2c 20 79 79 6d  nKey(pParse, yym
2a7ca 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 32  sp[-6].minor.yy2
2a7cb 34 32 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d  42, &yymsp[-3].m
2a7cc 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b  inor.yy0, yymsp[
2a7cd 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 32 2c  -2].minor.yy242,
2a7ce 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
2a7cf 2e 79 79 33 31 36 29 3b 0a 20 20 20 20 73 71 6c  .yy316);.    sql
2a7d0 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e  ite3DeferForeign
2a7d1 4b 65 79 28 70 50 61 72 73 65 2c 20 79 79 6d 73  Key(pParse, yyms
2a7d2 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36  p[0].minor.yy316
2a7d3 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2a7d4 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 39  ak;.      case 9
2a7d5 32 3a 20 2f 2a 20 6f 6e 63 6f 6e 66 20 3a 3a 3d  2: /* onconf ::=
2a7d6 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 39   */.      case 9
2a7d7 34 3a 20 2f 2a 20 6f 72 63 6f 6e 66 20 3a 3a 3d  4: /* orconf ::=
2a7d8 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2a7d9 2e 79 79 33 31 36 20 3d 20 4f 45 5f 44 65 66 61  .yy316 = OE_Defa
2a7da 75 6c 74 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  ult;}.        br
2a7db 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2a7dc 39 37 3a 20 2f 2a 20 72 65 73 6f 6c 76 65 74 79  97: /* resolvety
2a7dd 70 65 20 3a 3a 3d 20 49 47 4e 4f 52 45 20 2a 2f  pe ::= IGNORE */
2a7de 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
2a7df 33 31 36 20 3d 20 4f 45 5f 49 67 6e 6f 72 65 3b  316 = OE_Ignore;
2a7e0 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2a7e1 0a 20 20 20 20 20 20 63 61 73 65 20 39 38 3a 20  .      case 98: 
2a7e2 2f 2a 20 72 65 73 6f 6c 76 65 74 79 70 65 20 3a  /* resolvetype :
2a7e3 3a 3d 20 52 45 50 4c 41 43 45 20 2a 2f 0a 20 20  := REPLACE */.  
2a7e4 20 20 20 20 63 61 73 65 20 31 37 30 3a 20 2f 2a      case 170: /*
2a7e5 20 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a 3d 20   insert_cmd ::= 
2a7e6 52 45 50 4c 41 43 45 20 2a 2f 0a 7b 79 79 67 6f  REPLACE */.{yygo
2a7e7 74 6f 6d 69 6e 6f 72 2e 79 79 33 31 36 20 3d 20  tominor.yy316 = 
2a7e8 4f 45 5f 52 65 70 6c 61 63 65 3b 7d 0a 20 20 20  OE_Replace;}.   
2a7e9 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2a7ea 20 20 63 61 73 65 20 39 39 3a 20 2f 2a 20 63 6d    case 99: /* cm
2a7eb 64 20 3a 3a 3d 20 44 52 4f 50 20 54 41 42 4c 45  d ::= DROP TABLE
2a7ec 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61   ifexists fullna
2a7ed 6d 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65  me */.{.  sqlite
2a7ee 33 44 72 6f 70 54 61 62 6c 65 28 70 50 61 72 73  3DropTable(pPars
2a7ef 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  e, yymsp[0].mino
2a7f0 72 2e 79 79 34 31 39 2c 20 30 2c 20 79 79 6d 73  r.yy419, 0, yyms
2a7f1 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31  p[-1].minor.yy31
2a7f2 36 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  6);.}.        br
2a7f3 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2a7f4 31 30 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  102: /* cmd ::= 
2a7f5 43 52 45 41 54 45 20 74 65 6d 70 20 56 49 45 57  CREATE temp VIEW
2a7f6 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20   ifnotexists nm 
2a7f7 64 62 6e 6d 20 41 53 20 73 65 6c 65 63 74 20 2a  dbnm AS select *
2a7f8 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 43 72 65  /.{.  sqlite3Cre
2a7f9 61 74 65 56 69 65 77 28 70 50 61 72 73 65 2c 20  ateView(pParse, 
2a7fa 26 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72  &yymsp[-7].minor
2a7fb 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 2d 33 5d  .yy0, &yymsp[-3]
2a7fc 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d  .minor.yy0, &yym
2a7fd 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
2a7fe 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
2a7ff 2e 79 79 34 33 2c 20 79 79 6d 73 70 5b 2d 36 5d  .yy43, yymsp[-6]
2a800 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 2c 20 79 79  .minor.yy316, yy
2a801 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2a802 33 31 36 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  316);.}.        
2a803 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2a804 65 20 31 30 33 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 103: /* cmd ::
2a805 3d 20 44 52 4f 50 20 56 49 45 57 20 69 66 65 78  = DROP VIEW ifex
2a806 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f  ists fullname */
2a807 0a 7b 0a 20 20 73 71 6c 69 74 65 33 44 72 6f 70  .{.  sqlite3Drop
2a808 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 79 79  Table(pParse, yy
2a809 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34  msp[0].minor.yy4
2a80a 31 39 2c 20 31 2c 20 79 79 6d 73 70 5b 2d 31 5d  19, 1, yymsp[-1]
2a80b 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 29 3b 0a 7d  .minor.yy316);.}
2a80c 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2a80d 20 20 20 20 20 20 63 61 73 65 20 31 30 34 3a 20        case 104: 
2a80e 2f 2a 20 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63  /* cmd ::= selec
2a80f 74 20 2a 2f 0a 7b 0a 20 20 53 65 6c 65 63 74 44  t */.{.  SelectD
2a810 65 73 74 20 64 65 73 74 20 3d 20 7b 53 52 54 5f  est dest = {SRT_
2a811 4f 75 74 70 75 74 2c 20 30 2c 20 30 2c 20 30 2c  Output, 0, 0, 0,
2a812 20 30 7d 3b 0a 20 20 73 71 6c 69 74 65 33 53 65   0};.  sqlite3Se
2a813 6c 65 63 74 28 70 50 61 72 73 65 2c 20 79 79 6d  lect(pParse, yym
2a814 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 33  sp[0].minor.yy43
2a815 2c 20 26 64 65 73 74 29 3b 0a 20 20 73 71 6c 69  , &dest);.  sqli
2a816 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
2a817 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73  pParse->db, yyms
2a818 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 33 29  p[0].minor.yy43)
2a819 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
2a81a 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 30  k;.      case 10
2a81b 35 3a 20 2f 2a 20 73 65 6c 65 63 74 20 3a 3a 3d  5: /* select ::=
2a81c 20 6f 6e 65 73 65 6c 65 63 74 20 2a 2f 0a 20 20   oneselect */.  
2a81d 20 20 20 20 63 61 73 65 20 31 32 38 3a 20 2f 2a      case 128: /*
2a81e 20 73 65 6c 74 61 62 6c 69 73 74 5f 70 61 72 65   seltablist_pare
2a81f 6e 20 3a 3a 3d 20 73 65 6c 65 63 74 20 2a 2f 0a  n ::= select */.
2a820 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34  {yygotominor.yy4
2a821 33 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  3 = yymsp[0].min
2a822 6f 72 2e 79 79 34 33 3b 7d 0a 20 20 20 20 20 20  or.yy43;}.      
2a823 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2a824 61 73 65 20 31 30 36 3a 20 2f 2a 20 73 65 6c 65  ase 106: /* sele
2a825 63 74 20 3a 3a 3d 20 73 65 6c 65 63 74 20 6d 75  ct ::= select mu
2a826 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 6f 6e 65  ltiselect_op one
2a827 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 69 66  select */.{.  if
2a828 28 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ( yymsp[0].minor
2a829 2e 79 79 34 33 20 29 7b 0a 20 20 20 20 79 79 6d  .yy43 ){.    yym
2a82a 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 33  sp[0].minor.yy43
2a82b 2d 3e 6f 70 20 3d 20 79 79 6d 73 70 5b 2d 31 5d  ->op = yymsp[-1]
2a82c 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 3b 0a 20 20  .minor.yy316;.  
2a82d 20 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72    yymsp[0].minor
2a82e 2e 79 79 34 33 2d 3e 70 50 72 69 6f 72 20 3d 20  .yy43->pPrior = 
2a82f 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2a830 79 79 34 33 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  yy43;.  }else{. 
2a831 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
2a832 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64  Delete(pParse->d
2a833 62 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  b, yymsp[-2].min
2a834 6f 72 2e 79 79 34 33 29 3b 0a 20 20 7d 0a 20 20  or.yy43);.  }.  
2a835 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 33  yygotominor.yy43
2a836 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
2a837 72 2e 79 79 34 33 3b 0a 7d 0a 20 20 20 20 20 20  r.yy43;.}.      
2a838 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2a839 61 73 65 20 31 30 38 3a 20 2f 2a 20 6d 75 6c 74  ase 108: /* mult
2a83a 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 55  iselect_op ::= U
2a83b 4e 49 4f 4e 20 41 4c 4c 20 2a 2f 0a 7b 79 79 67  NION ALL */.{yyg
2a83c 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 31 36 20 3d  otominor.yy316 =
2a83d 20 54 4b 5f 41 4c 4c 3b 7d 0a 20 20 20 20 20 20   TK_ALL;}.      
2a83e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2a83f 61 73 65 20 31 31 30 3a 20 2f 2a 20 6f 6e 65 73  ase 110: /* ones
2a840 65 6c 65 63 74 20 3a 3a 3d 20 53 45 4c 45 43 54  elect ::= SELECT
2a841 20 64 69 73 74 69 6e 63 74 20 73 65 6c 63 6f 6c   distinct selcol
2a842 6c 69 73 74 20 66 72 6f 6d 20 77 68 65 72 65 5f  list from where_
2a843 6f 70 74 20 67 72 6f 75 70 62 79 5f 6f 70 74 20  opt groupby_opt 
2a844 68 61 76 69 6e 67 5f 6f 70 74 20 6f 72 64 65 72  having_opt order
2a845 62 79 5f 6f 70 74 20 6c 69 6d 69 74 5f 6f 70 74  by_opt limit_opt
2a846 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69   */.{.  yygotomi
2a847 6e 6f 72 2e 79 79 34 33 20 3d 20 73 71 6c 69 74  nor.yy43 = sqlit
2a848 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72  e3SelectNew(pPar
2a849 73 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e  se,yymsp[-6].min
2a84a 6f 72 2e 79 79 32 34 32 2c 79 79 6d 73 70 5b 2d  or.yy242,yymsp[-
2a84b 35 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 39 2c 79  5].minor.yy419,y
2a84c 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
2a84d 79 34 35 30 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d  y450,yymsp[-3].m
2a84e 69 6e 6f 72 2e 79 79 32 34 32 2c 79 79 6d 73 70  inor.yy242,yymsp
2a84f 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30  [-2].minor.yy450
2a850 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  ,yymsp[-1].minor
2a851 2e 79 79 32 34 32 2c 79 79 6d 73 70 5b 2d 37 5d  .yy242,yymsp[-7]
2a852 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 2c 79 79 6d  .minor.yy316,yym
2a853 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 38 34  sp[0].minor.yy84
2a854 2e 70 4c 69 6d 69 74 2c 79 79 6d 73 70 5b 30 5d  .pLimit,yymsp[0]
2a855 2e 6d 69 6e 6f 72 2e 79 79 38 34 2e 70 4f 66 66  .minor.yy84.pOff
2a856 73 65 74 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  set);.}.        
2a857 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2a858 65 20 31 31 34 3a 20 2f 2a 20 73 63 6c 70 20 3a  e 114: /* sclp :
2a859 3a 3d 20 73 65 6c 63 6f 6c 6c 69 73 74 20 43 4f  := selcollist CO
2a85a 4d 4d 41 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  MMA */.      cas
2a85b 65 20 32 34 31 3a 20 2f 2a 20 69 64 78 6c 69 73  e 241: /* idxlis
2a85c 74 5f 6f 70 74 20 3a 3a 3d 20 4c 50 20 69 64 78  t_opt ::= LP idx
2a85d 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 79 79 67 6f  list RP */.{yygo
2a85e 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 32 20 3d 20  tominor.yy242 = 
2a85f 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2a860 79 79 32 34 32 3b 7d 0a 20 20 20 20 20 20 20 20  yy242;}.        
2a861 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2a862 65 20 31 31 35 3a 20 2f 2a 20 73 63 6c 70 20 3a  e 115: /* sclp :
2a863 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  := */.      case
2a864 20 31 34 34 3a 20 2f 2a 20 6f 72 64 65 72 62 79   144: /* orderby
2a865 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20  _opt ::= */.    
2a866 20 20 63 61 73 65 20 31 35 32 3a 20 2f 2a 20 67    case 152: /* g
2a867 72 6f 75 70 62 79 5f 6f 70 74 20 3a 3a 3d 20 2a  roupby_opt ::= *
2a868 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 34  /.      case 234
2a869 3a 20 2f 2a 20 65 78 70 72 6c 69 73 74 20 3a 3a  : /* exprlist ::
2a86a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  = */.      case 
2a86b 32 34 30 3a 20 2f 2a 20 69 64 78 6c 69 73 74 5f  240: /* idxlist_
2a86c 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f  opt ::= */.{yygo
2a86d 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 32 20 3d 20  tominor.yy242 = 
2a86e 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
2a86f 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 31  k;.      case 11
2a870 36 3a 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74  6: /* selcollist
2a871 20 3a 3a 3d 20 73 63 6c 70 20 65 78 70 72 20 61   ::= sclp expr a
2a872 73 20 2a 2f 0a 7b 0a 20 20 20 79 79 67 6f 74 6f  s */.{.   yygoto
2a873 6d 69 6e 6f 72 2e 79 79 32 34 32 20 3d 20 73 71  minor.yy242 = sq
2a874 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
2a875 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70  end(pParse,yymsp
2a876 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 32  [-2].minor.yy242
2a877 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  ,yymsp[-1].minor
2a878 2e 79 79 34 35 30 2c 79 79 6d 73 70 5b 30 5d 2e  .yy450,yymsp[0].
2a879 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3f 26 79 79 6d  minor.yy0.n?&yym
2a87a 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3a  sp[0].minor.yy0:
2a87b 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  0);.}.        br
2a87c 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2a87d 31 31 37 3a 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69  117: /* selcolli
2a87e 73 74 20 3a 3a 3d 20 73 63 6c 70 20 53 54 41 52  st ::= sclp STAR
2a87f 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 70 20   */.{.  Expr *p 
2a880 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2a881 50 61 72 73 65 2c 20 54 4b 5f 41 4c 4c 2c 20 30  Parse, TK_ALL, 0
2a882 2c 20 30 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74  , 0, 0);.  yygot
2a883 6f 6d 69 6e 6f 72 2e 79 79 32 34 32 20 3d 20 73  ominor.yy242 = s
2a884 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
2a885 70 65 6e 64 28 70 50 61 72 73 65 2c 20 79 79 6d  pend(pParse, yym
2a886 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32  sp[-1].minor.yy2
2a887 34 32 2c 20 70 2c 20 30 29 3b 0a 7d 0a 20 20 20  42, p, 0);.}.   
2a888 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2a889 20 20 63 61 73 65 20 31 31 38 3a 20 2f 2a 20 73    case 118: /* s
2a88a 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63  elcollist ::= sc
2a88b 6c 70 20 6e 6d 20 44 4f 54 20 53 54 41 52 20 2a  lp nm DOT STAR *
2a88c 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 70 52 69 67  /.{.  Expr *pRig
2a88d 68 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  ht = sqlite3PExp
2a88e 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 41 4c 4c  r(pParse, TK_ALL
2a88f 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 30  , 0, 0, &yymsp[0
2a890 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20  ].minor.yy0);.  
2a891 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 73 71  Expr *pLeft = sq
2a892 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2a893 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20  e, TK_ID, 0, 0, 
2a894 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
2a895 2e 79 79 30 29 3b 0a 20 20 45 78 70 72 20 2a 70  .yy0);.  Expr *p
2a896 44 6f 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78  Dot = sqlite3PEx
2a897 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f  pr(pParse, TK_DO
2a898 54 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74  T, pLeft, pRight
2a899 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  , 0);.  yygotomi
2a89a 6e 6f 72 2e 79 79 32 34 32 20 3d 20 73 71 6c 69  nor.yy242 = sqli
2a89b 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
2a89c 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  d(pParse,yymsp[-
2a89d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 32 2c 20  3].minor.yy242, 
2a89e 70 44 6f 74 2c 20 30 29 3b 0a 7d 0a 20 20 20 20  pDot, 0);.}.    
2a89f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2a8a0 20 63 61 73 65 20 31 32 31 3a 20 2f 2a 20 61 73   case 121: /* as
2a8a1 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d   ::= */.{yygotom
2a8a2 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 30 3b 7d  inor.yy0.n = 0;}
2a8a3 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2a8a4 20 20 20 20 20 20 63 61 73 65 20 31 32 32 3a 20        case 122: 
2a8a5 2f 2a 20 66 72 6f 6d 20 3a 3a 3d 20 2a 2f 0a 7b  /* from ::= */.{
2a8a6 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31  yygotominor.yy41
2a8a7 39 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  9 = sqlite3DbMal
2a8a8 6c 6f 63 5a 65 72 6f 28 70 50 61 72 73 65 2d 3e  locZero(pParse->
2a8a9 64 62 2c 20 73 69 7a 65 6f 66 28 2a 79 79 67 6f  db, sizeof(*yygo
2a8aa 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 39 29 29 3b  tominor.yy419));
2a8ab 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2a8ac 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 33 3a  .      case 123:
2a8ad 20 2f 2a 20 66 72 6f 6d 20 3a 3a 3d 20 46 52 4f   /* from ::= FRO
2a8ae 4d 20 73 65 6c 74 61 62 6c 69 73 74 20 2a 2f 0a  M seltablist */.
2a8af 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  {.  yygotominor.
2a8b0 79 79 34 31 39 20 3d 20 79 79 6d 73 70 5b 30 5d  yy419 = yymsp[0]
2a8b1 2e 6d 69 6e 6f 72 2e 79 79 34 31 39 3b 0a 20 20  .minor.yy419;.  
2a8b2 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68  sqlite3SrcListSh
2a8b3 69 66 74 4a 6f 69 6e 54 79 70 65 28 79 79 67 6f  iftJoinType(yygo
2a8b4 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 39 29 3b 0a  tominor.yy419);.
2a8b5 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2a8b6 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 34 3a  .      case 124:
2a8b7 20 2f 2a 20 73 74 6c 5f 70 72 65 66 69 78 20 3a   /* stl_prefix :
2a8b8 3a 3d 20 73 65 6c 74 61 62 6c 69 73 74 20 6a 6f  := seltablist jo
2a8b9 69 6e 6f 70 20 2a 2f 0a 7b 0a 20 20 20 79 79 67  inop */.{.   yyg
2a8ba 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 39 20 3d  otominor.yy419 =
2a8bb 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
2a8bc 2e 79 79 34 31 39 3b 0a 20 20 20 69 66 28 20 79  .yy419;.   if( y
2a8bd 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 39  ygotominor.yy419
2a8be 20 26 26 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e   && yygotominor.
2a8bf 79 79 34 31 39 2d 3e 6e 53 72 63 3e 30 20 29 20  yy419->nSrc>0 ) 
2a8c0 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31  yygotominor.yy41
2a8c1 39 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72  9->a[yygotominor
2a8c2 2e 79 79 34 31 39 2d 3e 6e 53 72 63 2d 31 5d 2e  .yy419->nSrc-1].
2a8c3 6a 6f 69 6e 74 79 70 65 20 3d 20 79 79 6d 73 70  jointype = yymsp
2a8c4 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 3b  [0].minor.yy316;
2a8c5 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2a8c6 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 35  ;.      case 125
2a8c7 3a 20 2f 2a 20 73 74 6c 5f 70 72 65 66 69 78 20  : /* stl_prefix 
2a8c8 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ::= */.{yygotomi
2a8c9 6e 6f 72 2e 79 79 34 31 39 20 3d 20 30 3b 7d 0a  nor.yy419 = 0;}.
2a8ca 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2a8cb 20 20 20 20 20 63 61 73 65 20 31 32 36 3a 20 2f       case 126: /
2a8cc 2a 20 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d  * seltablist ::=
2a8cd 20 73 74 6c 5f 70 72 65 66 69 78 20 6e 6d 20 64   stl_prefix nm d
2a8ce 62 6e 6d 20 61 73 20 69 6e 64 65 78 65 64 5f 6f  bnm as indexed_o
2a8cf 70 74 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f  pt on_opt using_
2a8d0 6f 70 74 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74  opt */.{.  yygot
2a8d1 6f 6d 69 6e 6f 72 2e 79 79 34 31 39 20 3d 20 73  ominor.yy419 = s
2a8d2 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
2a8d3 65 6e 64 46 72 6f 6d 54 65 72 6d 28 70 50 61 72  endFromTerm(pPar
2a8d4 73 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e  se,yymsp[-6].min
2a8d5 6f 72 2e 79 79 34 31 39 2c 26 79 79 6d 73 70 5b  or.yy419,&yymsp[
2a8d6 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -5].minor.yy0,&y
2a8d7 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
2a8d8 79 30 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  y0,&yymsp[-3].mi
2a8d9 6e 6f 72 2e 79 79 30 2c 30 2c 79 79 6d 73 70 5b  nor.yy0,0,yymsp[
2a8da 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c  -1].minor.yy450,
2a8db 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2a8dc 79 33 35 32 29 3b 0a 20 20 73 71 6c 69 74 65 33  y352);.  sqlite3
2a8dd 53 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79  SrcListIndexedBy
2a8de 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d  (pParse, yygotom
2a8df 69 6e 6f 72 2e 79 79 34 31 39 2c 20 26 79 79 6d  inor.yy419, &yym
2a8e0 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
2a8e1 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2a8e2 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2a8e3 32 37 3a 20 2f 2a 20 73 65 6c 74 61 62 6c 69 73  27: /* seltablis
2a8e4 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78  t ::= stl_prefix
2a8e5 20 4c 50 20 73 65 6c 74 61 62 6c 69 73 74 5f 70   LP seltablist_p
2a8e6 61 72 65 6e 20 52 50 20 61 73 20 6f 6e 5f 6f 70  aren RP as on_op
2a8e7 74 20 75 73 69 6e 67 5f 6f 70 74 20 2a 2f 0a 7b  t using_opt */.{
2a8e8 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
2a8e9 2e 79 79 34 31 39 20 3d 20 73 71 6c 69 74 65 33  .yy419 = sqlite3
2a8ea 53 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72 6f  SrcListAppendFro
2a8eb 6d 54 65 72 6d 28 70 50 61 72 73 65 2c 79 79 6d  mTerm(pParse,yym
2a8ec 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-6].minor.yy4
2a8ed 31 39 2c 30 2c 30 2c 26 79 79 6d 73 70 5b 2d 32  19,0,0,&yymsp[-2
2a8ee 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 79 79 6d 73  ].minor.yy0,yyms
2a8ef 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 33  p[-4].minor.yy43
2a8f0 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  ,yymsp[-1].minor
2a8f1 2e 79 79 34 35 30 2c 79 79 6d 73 70 5b 30 5d 2e  .yy450,yymsp[0].
2a8f2 6d 69 6e 6f 72 2e 79 79 33 35 32 29 3b 0a 20 20  minor.yy352);.  
2a8f3 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2a8f4 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 39 3a  .      case 129:
2a8f5 20 2f 2a 20 73 65 6c 74 61 62 6c 69 73 74 5f 70   /* seltablist_p
2a8f6 61 72 65 6e 20 3a 3a 3d 20 73 65 6c 74 61 62 6c  aren ::= seltabl
2a8f7 69 73 74 20 2a 2f 0a 7b 0a 20 20 20 20 20 73 71  ist */.{.     sq
2a8f8 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68 69 66  lite3SrcListShif
2a8f9 74 4a 6f 69 6e 54 79 70 65 28 79 79 6d 73 70 5b  tJoinType(yymsp[
2a8fa 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 39 29 3b  0].minor.yy419);
2a8fb 0a 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  .     yygotomino
2a8fc 72 2e 79 79 34 33 20 3d 20 73 71 6c 69 74 65 33  r.yy43 = sqlite3
2a8fd 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65  SelectNew(pParse
2a8fe 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  ,0,yymsp[0].mino
2a8ff 72 2e 79 79 34 31 39 2c 30 2c 30 2c 30 2c 30 2c  r.yy419,0,0,0,0,
2a900 30 2c 30 2c 30 29 3b 0a 20 20 7d 0a 20 20 20 20  0,0,0);.  }.    
2a901 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2a902 20 63 61 73 65 20 31 33 30 3a 20 2f 2a 20 64 62   case 130: /* db
2a903 6e 6d 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20  nm ::= */.      
2a904 63 61 73 65 20 31 33 39 3a 20 2f 2a 20 69 6e 64  case 139: /* ind
2a905 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a  exed_opt ::= */.
2a906 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30  {yygotominor.yy0
2a907 2e 7a 3d 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f  .z=0; yygotomino
2a908 72 2e 79 79 30 2e 6e 3d 30 3b 7d 0a 20 20 20 20  r.yy0.n=0;}.    
2a909 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2a90a 20 63 61 73 65 20 31 33 32 3a 20 2f 2a 20 66 75   case 132: /* fu
2a90b 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 6e 6d 20 64 62  llname ::= nm db
2a90c 6e 6d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  nm */.{yygotomin
2a90d 6f 72 2e 79 79 34 31 39 20 3d 20 73 71 6c 69 74  or.yy419 = sqlit
2a90e 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28  e3SrcListAppend(
2a90f 70 50 61 72 73 65 2d 3e 64 62 2c 30 2c 26 79 79  pParse->db,0,&yy
2a910 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2a911 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
2a912 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20  r.yy0);}.       
2a913 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2a914 73 65 20 31 33 33 3a 20 2f 2a 20 6a 6f 69 6e 6f  se 133: /* joino
2a915 70 20 3a 3a 3d 20 43 4f 4d 4d 41 7c 4a 4f 49 4e  p ::= COMMA|JOIN
2a916 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f   */.{ yygotomino
2a917 72 2e 79 79 33 31 36 20 3d 20 4a 54 5f 49 4e 4e  r.yy316 = JT_INN
2a918 45 52 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  ER; }.        br
2a919 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2a91a 31 33 34 3a 20 2f 2a 20 6a 6f 69 6e 6f 70 20 3a  134: /* joinop :
2a91b 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 4a 4f 49 4e 20  := JOIN_KW JOIN 
2a91c 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  */.{ yygotominor
2a91d 2e 79 79 33 31 36 20 3d 20 73 71 6c 69 74 65 33  .yy316 = sqlite3
2a91e 4a 6f 69 6e 54 79 70 65 28 70 50 61 72 73 65 2c  JoinType(pParse,
2a91f 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
2a920 2e 79 79 30 2c 30 2c 30 29 3b 20 7d 0a 20 20 20  .yy0,0,0); }.   
2a921 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2a922 20 20 63 61 73 65 20 31 33 35 3a 20 2f 2a 20 6a    case 135: /* j
2a923 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b  oinop ::= JOIN_K
2a924 57 20 6e 6d 20 4a 4f 49 4e 20 2a 2f 0a 7b 20 79  W nm JOIN */.{ y
2a925 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 31 36  ygotominor.yy316
2a926 20 3d 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79   = sqlite3JoinTy
2a927 70 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70  pe(pParse,&yymsp
2a928 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26  [-2].minor.yy0,&
2a929 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2a92a 79 79 30 2c 30 29 3b 20 7d 0a 20 20 20 20 20 20  yy0,0); }.      
2a92b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2a92c 61 73 65 20 31 33 36 3a 20 2f 2a 20 6a 6f 69 6e  ase 136: /* join
2a92d 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 6e  op ::= JOIN_KW n
2a92e 6d 20 6e 6d 20 4a 4f 49 4e 20 2a 2f 0a 7b 20 79  m nm JOIN */.{ y
2a92f 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 31 36  ygotominor.yy316
2a930 20 3d 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79   = sqlite3JoinTy
2a931 70 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70  pe(pParse,&yymsp
2a932 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26  [-3].minor.yy0,&
2a933 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2a934 79 79 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  yy0,&yymsp[-1].m
2a935 69 6e 6f 72 2e 79 79 30 29 3b 20 7d 0a 20 20 20  inor.yy0); }.   
2a936 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2a937 20 20 63 61 73 65 20 31 33 37 3a 20 2f 2a 20 6f    case 137: /* o
2a938 6e 5f 6f 70 74 20 3a 3a 3d 20 4f 4e 20 65 78 70  n_opt ::= ON exp
2a939 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  r */.      case 
2a93a 31 34 38 3a 20 2f 2a 20 73 6f 72 74 69 74 65 6d  148: /* sortitem
2a93b 20 3a 3a 3d 20 65 78 70 72 20 2a 2f 0a 20 20 20   ::= expr */.   
2a93c 20 20 20 63 61 73 65 20 31 35 35 3a 20 2f 2a 20     case 155: /* 
2a93d 68 61 76 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 48  having_opt ::= H
2a93e 41 56 49 4e 47 20 65 78 70 72 20 2a 2f 0a 20 20  AVING expr */.  
2a93f 20 20 20 20 63 61 73 65 20 31 36 32 3a 20 2f 2a      case 162: /*
2a940 20 77 68 65 72 65 5f 6f 70 74 20 3a 3a 3d 20 57   where_opt ::= W
2a941 48 45 52 45 20 65 78 70 72 20 2a 2f 0a 20 20 20  HERE expr */.   
2a942 20 20 20 63 61 73 65 20 31 37 37 3a 20 2f 2a 20     case 177: /* 
2a943 65 78 70 72 20 3a 3a 3d 20 74 65 72 6d 20 2a 2f  expr ::= term */
2a944 0a 20 20 20 20 20 20 63 61 73 65 20 32 30 35 3a  .      case 205:
2a945 20 2f 2a 20 65 73 63 61 70 65 20 3a 3a 3d 20 45   /* escape ::= E
2a946 53 43 41 50 45 20 65 78 70 72 20 2a 2f 0a 20 20  SCAPE expr */.  
2a947 20 20 20 20 63 61 73 65 20 32 32 39 3a 20 2f 2a      case 229: /*
2a948 20 63 61 73 65 5f 65 6c 73 65 20 3a 3a 3d 20 45   case_else ::= E
2a949 4c 53 45 20 65 78 70 72 20 2a 2f 0a 20 20 20 20  LSE expr */.    
2a94a 20 20 63 61 73 65 20 32 33 31 3a 20 2f 2a 20 63    case 231: /* c
2a94b 61 73 65 5f 6f 70 65 72 61 6e 64 20 3a 3a 3d 20  ase_operand ::= 
2a94c 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  expr */.{yygotom
2a94d 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20 79 79 6d  inor.yy450 = yym
2a94e 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35  sp[0].minor.yy45
2a94f 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
2a950 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33  k;.      case 13
2a951 38 3a 20 2f 2a 20 6f 6e 5f 6f 70 74 20 3a 3a 3d  8: /* on_opt ::=
2a952 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
2a953 35 34 3a 20 2f 2a 20 68 61 76 69 6e 67 5f 6f 70  54: /* having_op
2a954 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63  t ::= */.      c
2a955 61 73 65 20 31 36 31 3a 20 2f 2a 20 77 68 65 72  ase 161: /* wher
2a956 65 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20  e_opt ::= */.   
2a957 20 20 20 63 61 73 65 20 32 30 36 3a 20 2f 2a 20     case 206: /* 
2a958 65 73 63 61 70 65 20 3a 3a 3d 20 2a 2f 0a 20 20  escape ::= */.  
2a959 20 20 20 20 63 61 73 65 20 32 33 30 3a 20 2f 2a      case 230: /*
2a95a 20 63 61 73 65 5f 65 6c 73 65 20 3a 3a 3d 20 2a   case_else ::= *
2a95b 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 32  /.      case 232
2a95c 3a 20 2f 2a 20 63 61 73 65 5f 6f 70 65 72 61 6e  : /* case_operan
2a95d 64 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f  d ::= */.{yygoto
2a95e 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20 30 3b  minor.yy450 = 0;
2a95f 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2a960 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 31 3a  .      case 141:
2a961 20 2f 2a 20 69 6e 64 65 78 65 64 5f 6f 70 74 20   /* indexed_opt 
2a962 3a 3a 3d 20 4e 4f 54 20 49 4e 44 45 58 45 44 20  ::= NOT INDEXED 
2a963 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2a964 79 79 30 2e 7a 3d 30 3b 20 79 79 67 6f 74 6f 6d  yy0.z=0; yygotom
2a965 69 6e 6f 72 2e 79 79 30 2e 6e 3d 31 3b 7d 0a 20  inor.yy0.n=1;}. 
2a966 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2a967 20 20 20 20 63 61 73 65 20 31 34 32 3a 20 2f 2a      case 142: /*
2a968 20 75 73 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 55   using_opt ::= U
2a969 53 49 4e 47 20 4c 50 20 69 6e 73 63 6f 6c 6c 69  SING LP inscolli
2a96a 73 74 20 52 50 20 2a 2f 0a 20 20 20 20 20 20 63  st RP */.      c
2a96b 61 73 65 20 31 37 34 3a 20 2f 2a 20 69 6e 73 63  ase 174: /* insc
2a96c 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 4c  ollist_opt ::= L
2a96d 50 20 69 6e 73 63 6f 6c 6c 69 73 74 20 52 50 20  P inscollist RP 
2a96e 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2a96f 79 79 33 35 32 20 3d 20 79 79 6d 73 70 5b 2d 31  yy352 = yymsp[-1
2a970 5d 2e 6d 69 6e 6f 72 2e 79 79 33 35 32 3b 7d 0a  ].minor.yy352;}.
2a971 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2a972 20 20 20 20 20 63 61 73 65 20 31 34 33 3a 20 2f       case 143: /
2a973 2a 20 75 73 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20  * using_opt ::= 
2a974 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 37  */.      case 17
2a975 33 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74  3: /* inscollist
2a976 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67  _opt ::= */.{yyg
2a977 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 35 32 20 3d  otominor.yy352 =
2a978 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65   0;}.        bre
2a979 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2a97a 34 35 3a 20 2f 2a 20 6f 72 64 65 72 62 79 5f 6f  45: /* orderby_o
2a97b 70 74 20 3a 3a 3d 20 4f 52 44 45 52 20 42 59 20  pt ::= ORDER BY 
2a97c 73 6f 72 74 6c 69 73 74 20 2a 2f 0a 20 20 20 20  sortlist */.    
2a97d 20 20 63 61 73 65 20 31 35 33 3a 20 2f 2a 20 67    case 153: /* g
2a97e 72 6f 75 70 62 79 5f 6f 70 74 20 3a 3a 3d 20 47  roupby_opt ::= G
2a97f 52 4f 55 50 20 42 59 20 6e 65 78 70 72 6c 69 73  ROUP BY nexprlis
2a980 74 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  t */.      case 
2a981 32 33 33 3a 20 2f 2a 20 65 78 70 72 6c 69 73 74  233: /* exprlist
2a982 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74 20 2a   ::= nexprlist *
2a983 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
2a984 79 32 34 32 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  y242 = yymsp[0].
2a985 6d 69 6e 6f 72 2e 79 79 32 34 32 3b 7d 0a 20 20  minor.yy242;}.  
2a986 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2a987 20 20 20 63 61 73 65 20 31 34 36 3a 20 2f 2a 20     case 146: /* 
2a988 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20 73 6f 72  sortlist ::= sor
2a989 74 6c 69 73 74 20 43 4f 4d 4d 41 20 73 6f 72 74  tlist COMMA sort
2a98a 69 74 65 6d 20 73 6f 72 74 6f 72 64 65 72 20 2a  item sortorder *
2a98b 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
2a98c 72 2e 79 79 32 34 32 20 3d 20 73 71 6c 69 74 65  r.yy242 = sqlite
2a98d 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
2a98e 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 33 5d  pParse,yymsp[-3]
2a98f 2e 6d 69 6e 6f 72 2e 79 79 32 34 32 2c 79 79 6d  .minor.yy242,yym
2a990 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-1].minor.yy4
2a991 35 30 2c 30 29 3b 0a 20 20 69 66 28 20 79 79 67  50,0);.  if( yyg
2a992 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 32 20 29  otominor.yy242 )
2a993 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32   yygotominor.yy2
2a994 34 32 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f  42->a[yygotomino
2a995 72 2e 79 79 32 34 32 2d 3e 6e 45 78 70 72 2d 31  r.yy242->nExpr-1
2a996 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 79 79  ].sortOrder = yy
2a997 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
2a998 31 36 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  16;.}.        br
2a999 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2a99a 31 34 37 3a 20 2f 2a 20 73 6f 72 74 6c 69 73 74  147: /* sortlist
2a99b 20 3a 3a 3d 20 73 6f 72 74 69 74 65 6d 20 73 6f   ::= sortitem so
2a99c 72 74 6f 72 64 65 72 20 2a 2f 0a 7b 0a 20 20 79  rtorder */.{.  y
2a99d 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 32  ygotominor.yy242
2a99e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
2a99f 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
2a9a0 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  0,yymsp[-1].mino
2a9a1 72 2e 79 79 34 35 30 2c 30 29 3b 0a 20 20 69 66  r.yy450,0);.  if
2a9a2 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
2a9a3 32 34 32 20 26 26 20 79 79 67 6f 74 6f 6d 69 6e  242 && yygotomin
2a9a4 6f 72 2e 79 79 32 34 32 2d 3e 61 20 29 20 79 79  or.yy242->a ) yy
2a9a5 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 32 2d  gotominor.yy242-
2a9a6 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 64 65 72 20  >a[0].sortOrder 
2a9a7 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
2a9a8 2e 79 79 33 31 36 3b 0a 7d 0a 20 20 20 20 20 20  .yy316;.}.      
2a9a9 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2a9aa 61 73 65 20 31 34 39 3a 20 2f 2a 20 73 6f 72 74  ase 149: /* sort
2a9ab 6f 72 64 65 72 20 3a 3a 3d 20 41 53 43 20 2a 2f  order ::= ASC */
2a9ac 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 31 3a  .      case 151:
2a9ad 20 2f 2a 20 73 6f 72 74 6f 72 64 65 72 20 3a 3a   /* sortorder ::
2a9ae 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  = */.{yygotomino
2a9af 72 2e 79 79 33 31 36 20 3d 20 53 51 4c 49 54 45  r.yy316 = SQLITE
2a9b0 5f 53 4f 5f 41 53 43 3b 7d 0a 20 20 20 20 20 20  _SO_ASC;}.      
2a9b1 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2a9b2 61 73 65 20 31 35 30 3a 20 2f 2a 20 73 6f 72 74  ase 150: /* sort
2a9b3 6f 72 64 65 72 20 3a 3a 3d 20 44 45 53 43 20 2a  order ::= DESC *
2a9b4 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
2a9b5 79 33 31 36 20 3d 20 53 51 4c 49 54 45 5f 53 4f  y316 = SQLITE_SO
2a9b6 5f 44 45 53 43 3b 7d 0a 20 20 20 20 20 20 20 20  _DESC;}.        
2a9b7 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2a9b8 65 20 31 35 36 3a 20 2f 2a 20 6c 69 6d 69 74 5f  e 156: /* limit_
2a9b9 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f  opt ::= */.{yygo
2a9ba 74 6f 6d 69 6e 6f 72 2e 79 79 38 34 2e 70 4c 69  tominor.yy84.pLi
2a9bb 6d 69 74 20 3d 20 30 3b 20 79 79 67 6f 74 6f 6d  mit = 0; yygotom
2a9bc 69 6e 6f 72 2e 79 79 38 34 2e 70 4f 66 66 73 65  inor.yy84.pOffse
2a9bd 74 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20  t = 0;}.        
2a9be 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2a9bf 65 20 31 35 37 3a 20 2f 2a 20 6c 69 6d 69 74 5f  e 157: /* limit_
2a9c0 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78  opt ::= LIMIT ex
2a9c1 70 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  pr */.{yygotomin
2a9c2 6f 72 2e 79 79 38 34 2e 70 4c 69 6d 69 74 20 3d  or.yy84.pLimit =
2a9c3 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2a9c4 79 79 34 35 30 3b 20 79 79 67 6f 74 6f 6d 69 6e  yy450; yygotomin
2a9c5 6f 72 2e 79 79 38 34 2e 70 4f 66 66 73 65 74 20  or.yy84.pOffset 
2a9c6 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  = 0;}.        br
2a9c7 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2a9c8 31 35 38 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f 70  158: /* limit_op
2a9c9 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72  t ::= LIMIT expr
2a9ca 20 4f 46 46 53 45 54 20 65 78 70 72 20 2a 2f 0a   OFFSET expr */.
2a9cb 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 38  {yygotominor.yy8
2a9cc 34 2e 70 4c 69 6d 69 74 20 3d 20 79 79 6d 73 70  4.pLimit = yymsp
2a9cd 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30  [-2].minor.yy450
2a9ce 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ; yygotominor.yy
2a9cf 38 34 2e 70 4f 66 66 73 65 74 20 3d 20 79 79 6d  84.pOffset = yym
2a9d0 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35  sp[0].minor.yy45
2a9d1 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
2a9d2 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 35  k;.      case 15
2a9d3 39 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f 70 74 20  9: /* limit_opt 
2a9d4 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72 20 43  ::= LIMIT expr C
2a9d5 4f 4d 4d 41 20 65 78 70 72 20 2a 2f 0a 7b 79 79  OMMA expr */.{yy
2a9d6 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 38 34 2e 70  gotominor.yy84.p
2a9d7 4f 66 66 73 65 74 20 3d 20 79 79 6d 73 70 5b 2d  Offset = yymsp[-
2a9d8 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 3b 20  2].minor.yy450; 
2a9d9 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 38 34  yygotominor.yy84
2a9da 2e 70 4c 69 6d 69 74 20 3d 20 79 79 6d 73 70 5b  .pLimit = yymsp[
2a9db 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 3b 7d  0].minor.yy450;}
2a9dc 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2a9dd 20 20 20 20 20 20 63 61 73 65 20 31 36 30 3a 20        case 160: 
2a9de 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 45 4c 45 54  /* cmd ::= DELET
2a9df 45 20 46 52 4f 4d 20 66 75 6c 6c 6e 61 6d 65 20  E FROM fullname 
2a9e0 69 6e 64 65 78 65 64 5f 6f 70 74 20 77 68 65 72  indexed_opt wher
2a9e1 65 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c  e_opt */.{.  sql
2a9e2 69 74 65 33 53 72 63 4c 69 73 74 49 6e 64 65 78  ite3SrcListIndex
2a9e3 65 64 42 79 28 70 50 61 72 73 65 2c 20 79 79 6d  edBy(pParse, yym
2a9e4 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-2].minor.yy4
2a9e5 31 39 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  19, &yymsp[-1].m
2a9e6 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71 6c  inor.yy0);.  sql
2a9e7 69 74 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 70  ite3DeleteFrom(p
2a9e8 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 32 5d 2e  Parse,yymsp[-2].
2a9e9 6d 69 6e 6f 72 2e 79 79 34 31 39 2c 79 79 6d 73  minor.yy419,yyms
2a9ea 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30  p[0].minor.yy450
2a9eb 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2a9ec 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2a9ed 36 33 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 55  63: /* cmd ::= U
2a9ee 50 44 41 54 45 20 6f 72 63 6f 6e 66 20 66 75 6c  PDATE orconf ful
2a9ef 6c 6e 61 6d 65 20 69 6e 64 65 78 65 64 5f 6f 70  lname indexed_op
2a9f0 74 20 53 45 54 20 73 65 74 6c 69 73 74 20 77 68  t SET setlist wh
2a9f1 65 72 65 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20 73  ere_opt */.{.  s
2a9f2 71 6c 69 74 65 33 53 72 63 4c 69 73 74 49 6e 64  qlite3SrcListInd
2a9f3 65 78 65 64 42 79 28 70 50 61 72 73 65 2c 20 79  exedBy(pParse, y
2a9f4 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
2a9f5 79 34 31 39 2c 20 26 79 79 6d 73 70 5b 2d 33 5d  y419, &yymsp[-3]
2a9f6 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73  .minor.yy0);.  s
2a9f7 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43 68  qlite3ExprListCh
2a9f8 65 63 6b 4c 65 6e 67 74 68 28 70 50 61 72 73 65  eckLength(pParse
2a9f9 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  ,yymsp[-1].minor
2a9fa 2e 79 79 32 34 32 2c 22 73 65 74 20 6c 69 73 74  .yy242,"set list
2a9fb 22 29 3b 20 0a 20 20 73 71 6c 69 74 65 33 55 70  "); .  sqlite3Up
2a9fc 64 61 74 65 28 70 50 61 72 73 65 2c 79 79 6d 73  date(pParse,yyms
2a9fd 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31  p[-4].minor.yy41
2a9fe 39 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  9,yymsp[-1].mino
2a9ff 72 2e 79 79 32 34 32 2c 79 79 6d 73 70 5b 30 5d  r.yy242,yymsp[0]
2aa00 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 79 79 6d  .minor.yy450,yym
2aa01 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-5].minor.yy3
2aa02 31 36 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  16);.}.        b
2aa03 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2aa04 20 31 36 34 3a 20 2f 2a 20 73 65 74 6c 69 73 74   164: /* setlist
2aa05 20 3a 3a 3d 20 73 65 74 6c 69 73 74 20 43 4f 4d   ::= setlist COM
2aa06 4d 41 20 6e 6d 20 45 51 20 65 78 70 72 20 2a 2f  MA nm EQ expr */
2aa07 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
2aa08 32 34 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70  242 = sqlite3Exp
2aa09 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
2aa0a 73 65 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  se,yymsp[-4].min
2aa0b 6f 72 2e 79 79 32 34 32 2c 79 79 6d 73 70 5b 30  or.yy242,yymsp[0
2aa0c 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 26 79  ].minor.yy450,&y
2aa0d 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2aa0e 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  y0);}.        br
2aa0f 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2aa10 31 36 35 3a 20 2f 2a 20 73 65 74 6c 69 73 74 20  165: /* setlist 
2aa11 3a 3a 3d 20 6e 6d 20 45 51 20 65 78 70 72 20 2a  ::= nm EQ expr *
2aa12 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
2aa13 79 32 34 32 20 3d 20 73 71 6c 69 74 65 33 45 78  y242 = sqlite3Ex
2aa14 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
2aa15 72 73 65 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d  rse,0,yymsp[0].m
2aa16 69 6e 6f 72 2e 79 79 34 35 30 2c 26 79 79 6d 73  inor.yy450,&yyms
2aa17 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-2].minor.yy0)
2aa18 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2aa19 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 36  ;.      case 166
2aa1a 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 69 6e 73  : /* cmd ::= ins
2aa1b 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c  ert_cmd INTO ful
2aa1c 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74  lname inscollist
2aa1d 5f 6f 70 74 20 56 41 4c 55 45 53 20 4c 50 20 69  _opt VALUES LP i
2aa1e 74 65 6d 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 73  temlist RP */.{s
2aa1f 71 6c 69 74 65 33 49 6e 73 65 72 74 28 70 50 61  qlite3Insert(pPa
2aa20 72 73 65 2c 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d  rse, yymsp[-5].m
2aa21 69 6e 6f 72 2e 79 79 34 31 39 2c 20 79 79 6d 73  inor.yy419, yyms
2aa22 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34  p[-1].minor.yy24
2aa23 32 2c 20 30 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e  2, 0, yymsp[-4].
2aa24 6d 69 6e 6f 72 2e 79 79 33 35 32 2c 20 79 79 6d  minor.yy352, yym
2aa25 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-7].minor.yy3
2aa26 31 36 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  16);}.        br
2aa27 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2aa28 31 36 37 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  167: /* cmd ::= 
2aa29 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20  insert_cmd INTO 
2aa2a 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c  fullname inscoll
2aa2b 69 73 74 5f 6f 70 74 20 73 65 6c 65 63 74 20 2a  ist_opt select *
2aa2c 2f 0a 7b 73 71 6c 69 74 65 33 49 6e 73 65 72 74  /.{sqlite3Insert
2aa2d 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
2aa2e 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 39 2c 20  2].minor.yy419, 
2aa2f 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0, yymsp[0].mino
2aa30 72 2e 79 79 34 33 2c 20 79 79 6d 73 70 5b 2d 31  r.yy43, yymsp[-1
2aa31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 35 32 2c 20 79  ].minor.yy352, y
2aa32 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
2aa33 79 33 31 36 29 3b 7d 0a 20 20 20 20 20 20 20 20  y316);}.        
2aa34 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2aa35 65 20 31 36 38 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 168: /* cmd ::
2aa36 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54  = insert_cmd INT
2aa37 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f  O fullname insco
2aa38 6c 6c 69 73 74 5f 6f 70 74 20 44 45 46 41 55 4c  llist_opt DEFAUL
2aa39 54 20 56 41 4c 55 45 53 20 2a 2f 0a 7b 73 71 6c  T VALUES */.{sql
2aa3a 69 74 65 33 49 6e 73 65 72 74 28 70 50 61 72 73  ite3Insert(pPars
2aa3b 65 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  e, yymsp[-3].min
2aa3c 6f 72 2e 79 79 34 31 39 2c 20 30 2c 20 30 2c 20  or.yy419, 0, 0, 
2aa3d 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2aa3e 79 79 33 35 32 2c 20 79 79 6d 73 70 5b 2d 35 5d  yy352, yymsp[-5]
2aa3f 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 29 3b 7d 0a  .minor.yy316);}.
2aa40 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2aa41 20 20 20 20 20 63 61 73 65 20 31 37 31 3a 20 2f       case 171: /
2aa42 2a 20 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 69  * itemlist ::= i
2aa43 74 65 6d 6c 69 73 74 20 43 4f 4d 4d 41 20 65 78  temlist COMMA ex
2aa44 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  pr */.      case
2aa45 20 32 33 35 3a 20 2f 2a 20 6e 65 78 70 72 6c 69   235: /* nexprli
2aa46 73 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74  st ::= nexprlist
2aa47 20 43 4f 4d 4d 41 20 65 78 70 72 20 2a 2f 0a 7b   COMMA expr */.{
2aa48 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34  yygotominor.yy24
2aa49 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  2 = sqlite3ExprL
2aa4a 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
2aa4b 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  ,yymsp[-2].minor
2aa4c 2e 79 79 32 34 32 2c 79 79 6d 73 70 5b 30 5d 2e  .yy242,yymsp[0].
2aa4d 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 30 29 3b 7d  minor.yy450,0);}
2aa4e 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2aa4f 20 20 20 20 20 20 63 61 73 65 20 31 37 32 3a 20        case 172: 
2aa50 2f 2a 20 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20  /* itemlist ::= 
2aa51 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61  expr */.      ca
2aa52 73 65 20 32 33 36 3a 20 2f 2a 20 6e 65 78 70 72  se 236: /* nexpr
2aa53 6c 69 73 74 20 3a 3a 3d 20 65 78 70 72 20 2a 2f  list ::= expr */
2aa54 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
2aa55 32 34 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70  242 = sqlite3Exp
2aa56 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
2aa57 73 65 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  se,0,yymsp[0].mi
2aa58 6e 6f 72 2e 79 79 34 35 30 2c 30 29 3b 7d 0a 20  nor.yy450,0);}. 
2aa59 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2aa5a 20 20 20 20 63 61 73 65 20 31 37 35 3a 20 2f 2a      case 175: /*
2aa5b 20 69 6e 73 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20   inscollist ::= 
2aa5c 69 6e 73 63 6f 6c 6c 69 73 74 20 43 4f 4d 4d 41  inscollist COMMA
2aa5d 20 6e 6d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69   nm */.{yygotomi
2aa5e 6e 6f 72 2e 79 79 33 35 32 20 3d 20 73 71 6c 69  nor.yy352 = sqli
2aa5f 74 65 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28  te3IdListAppend(
2aa60 70 50 61 72 73 65 2d 3e 64 62 2c 79 79 6d 73 70  pParse->db,yymsp
2aa61 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 35 32  [-2].minor.yy352
2aa62 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
2aa63 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  .yy0);}.        
2aa64 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2aa65 65 20 31 37 36 3a 20 2f 2a 20 69 6e 73 63 6f 6c  e 176: /* inscol
2aa66 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20 2a 2f 0a 7b  list ::= nm */.{
2aa67 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 35  yygotominor.yy35
2aa68 32 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73  2 = sqlite3IdLis
2aa69 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e  tAppend(pParse->
2aa6a 64 62 2c 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  db,0,&yymsp[0].m
2aa6b 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20  inor.yy0);}.    
2aa6c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2aa6d 20 63 61 73 65 20 31 37 38 3a 20 2f 2a 20 65 78   case 178: /* ex
2aa6e 70 72 20 3a 3a 3d 20 4c 50 20 65 78 70 72 20 52  pr ::= LP expr R
2aa6f 50 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  P */.{yygotomino
2aa70 72 2e 79 79 34 35 30 20 3d 20 79 79 6d 73 70 5b  r.yy450 = yymsp[
2aa71 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 3b  -1].minor.yy450;
2aa72 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e   sqlite3ExprSpan
2aa73 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34  (yygotominor.yy4
2aa74 35 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  50,&yymsp[-2].mi
2aa75 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30  nor.yy0,&yymsp[0
2aa76 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 20 7d 0a  ].minor.yy0); }.
2aa77 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2aa78 20 20 20 20 20 63 61 73 65 20 31 37 39 3a 20 2f       case 179: /
2aa79 2a 20 74 65 72 6d 20 3a 3a 3d 20 4e 55 4c 4c 20  * term ::= NULL 
2aa7a 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 38  */.      case 18
2aa7b 34 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20 49  4: /* term ::= I
2aa7c 4e 54 45 47 45 52 7c 46 4c 4f 41 54 7c 42 4c 4f  NTEGER|FLOAT|BLO
2aa7d 42 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  B */.      case 
2aa7e 31 38 35 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d  185: /* term ::=
2aa7f 20 53 54 52 49 4e 47 20 2a 2f 0a 7b 79 79 67 6f   STRING */.{yygo
2aa80 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20  tominor.yy450 = 
2aa81 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2aa82 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 61  rse, yymsp[0].ma
2aa83 6a 6f 72 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73  jor, 0, 0, &yyms
2aa84 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
2aa85 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2aa86 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 30 3a  .      case 180:
2aa87 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 49 44 20   /* expr ::= ID 
2aa88 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 38  */.      case 18
2aa89 31 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 4a  1: /* expr ::= J
2aa8a 4f 49 4e 5f 4b 57 20 2a 2f 0a 7b 79 79 67 6f 74  OIN_KW */.{yygot
2aa8b 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20 73  ominor.yy450 = s
2aa8c 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2aa8d 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c  se, TK_ID, 0, 0,
2aa8e 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
2aa8f 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  .yy0);}.        
2aa90 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2aa91 65 20 31 38 32 3a 20 2f 2a 20 65 78 70 72 20 3a  e 182: /* expr :
2aa92 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20 2a 2f 0a  := nm DOT nm */.
2aa93 7b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70 31 20  {.  Expr *temp1 
2aa94 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2aa95 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c  Parse, TK_ID, 0,
2aa96 20 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d   0, &yymsp[-2].m
2aa97 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45 78 70  inor.yy0);.  Exp
2aa98 72 20 2a 74 65 6d 70 32 20 3d 20 73 71 6c 69 74  r *temp2 = sqlit
2aa99 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
2aa9a 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79  TK_ID, 0, 0, &yy
2aa9b 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
2aa9c 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  );.  yygotominor
2aa9d 2e 79 79 34 35 30 20 3d 20 73 71 6c 69 74 65 33  .yy450 = sqlite3
2aa9e 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2aa9f 5f 44 4f 54 2c 20 74 65 6d 70 31 2c 20 74 65 6d  _DOT, temp1, tem
2aaa0 70 32 2c 20 30 29 3b 0a 7d 0a 20 20 20 20 20 20  p2, 0);.}.      
2aaa1 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2aaa2 61 73 65 20 31 38 33 3a 20 2f 2a 20 65 78 70 72  ase 183: /* expr
2aaa3 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20 44   ::= nm DOT nm D
2aaa4 4f 54 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 45 78 70  OT nm */.{.  Exp
2aaa5 72 20 2a 74 65 6d 70 31 20 3d 20 73 71 6c 69 74  r *temp1 = sqlit
2aaa6 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
2aaa7 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79  TK_ID, 0, 0, &yy
2aaa8 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2aaa9 30 29 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70  0);.  Expr *temp
2aaaa 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  2 = sqlite3PExpr
2aaab 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20  (pParse, TK_ID, 
2aaac 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d  0, 0, &yymsp[-2]
2aaad 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45  .minor.yy0);.  E
2aaae 78 70 72 20 2a 74 65 6d 70 33 20 3d 20 73 71 6c  xpr *temp3 = sql
2aaaf 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2aab0 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26  , TK_ID, 0, 0, &
2aab1 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2aab2 79 30 29 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d  y0);.  Expr *tem
2aab3 70 34 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  p4 = sqlite3PExp
2aab4 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54  r(pParse, TK_DOT
2aab5 2c 20 74 65 6d 70 32 2c 20 74 65 6d 70 33 2c 20  , temp2, temp3, 
2aab6 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  0);.  yygotomino
2aab7 72 2e 79 79 34 35 30 20 3d 20 73 71 6c 69 74 65  r.yy450 = sqlite
2aab8 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
2aab9 4b 5f 44 4f 54 2c 20 74 65 6d 70 31 2c 20 74 65  K_DOT, temp1, te
2aaba 6d 70 34 2c 20 30 29 3b 0a 7d 0a 20 20 20 20 20  mp4, 0);.}.     
2aabb 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2aabc 63 61 73 65 20 31 38 36 3a 20 2f 2a 20 65 78 70  case 186: /* exp
2aabd 72 20 3a 3a 3d 20 52 45 47 49 53 54 45 52 20 2a  r ::= REGISTER *
2aabe 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
2aabf 79 34 35 30 20 3d 20 73 71 6c 69 74 65 33 52 65  y450 = sqlite3Re
2aac0 67 69 73 74 65 72 45 78 70 72 28 70 50 61 72 73  gisterExpr(pPars
2aac1 65 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  e, &yymsp[0].min
2aac2 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
2aac3 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2aac4 61 73 65 20 31 38 37 3a 20 2f 2a 20 65 78 70 72  ase 187: /* expr
2aac5 20 3a 3a 3d 20 56 41 52 49 41 42 4c 45 20 2a 2f   ::= VARIABLE */
2aac6 0a 7b 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b  .{.  Token *pTok
2aac7 65 6e 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  en = &yymsp[0].m
2aac8 69 6e 6f 72 2e 79 79 30 3b 0a 20 20 45 78 70 72  inor.yy0;.  Expr
2aac9 20 2a 70 45 78 70 72 20 3d 20 79 79 67 6f 74 6f   *pExpr = yygoto
2aaca 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20 73 71  minor.yy450 = sq
2aacb 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2aacc 65 2c 20 54 4b 5f 56 41 52 49 41 42 4c 45 2c 20  e, TK_VARIABLE, 
2aacd 30 2c 20 30 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20  0, 0, pToken);. 
2aace 20 73 71 6c 69 74 65 33 45 78 70 72 41 73 73 69   sqlite3ExprAssi
2aacf 67 6e 56 61 72 4e 75 6d 62 65 72 28 70 50 61 72  gnVarNumber(pPar
2aad0 73 65 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 20 20  se, pExpr);.}.  
2aad1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2aad2 20 20 20 63 61 73 65 20 31 38 38 3a 20 2f 2a 20     case 188: /* 
2aad3 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 43 4f  expr ::= expr CO
2aad4 4c 4c 41 54 45 20 69 64 73 20 2a 2f 0a 7b 0a 20  LLATE ids */.{. 
2aad5 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
2aad6 35 30 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  50 = sqlite3Expr
2aad7 53 65 74 43 6f 6c 6c 28 70 50 61 72 73 65 2c 20  SetColl(pParse, 
2aad8 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2aad9 79 79 34 35 30 2c 20 26 79 79 6d 73 70 5b 30 5d  yy450, &yymsp[0]
2aada 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20  .minor.yy0);.}. 
2aadb 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2aadc 20 20 20 20 63 61 73 65 20 31 38 39 3a 20 2f 2a      case 189: /*
2aadd 20 65 78 70 72 20 3a 3a 3d 20 43 41 53 54 20 4c   expr ::= CAST L
2aade 50 20 65 78 70 72 20 41 53 20 74 79 70 65 74 6f  P expr AS typeto
2aadf 6b 65 6e 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79  ken RP */.{.  yy
2aae0 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20  gotominor.yy450 
2aae1 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2aae2 50 61 72 73 65 2c 20 54 4b 5f 43 41 53 54 2c 20  Parse, TK_CAST, 
2aae3 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
2aae4 79 79 34 35 30 2c 20 30 2c 20 26 79 79 6d 73 70  yy450, 0, &yymsp
2aae5 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-1].minor.yy0);
2aae6 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70  .  sqlite3ExprSp
2aae7 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  an(yygotominor.y
2aae8 79 34 35 30 2c 26 79 79 6d 73 70 5b 2d 35 5d 2e  y450,&yymsp[-5].
2aae9 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
2aaea 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
2aaeb 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2aaec 0a 20 20 20 20 20 20 63 61 73 65 20 31 39 30 3a  .      case 190:
2aaed 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 49 44 20   /* expr ::= ID 
2aaee 4c 50 20 64 69 73 74 69 6e 63 74 20 65 78 70 72  LP distinct expr
2aaef 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 69  list RP */.{.  i
2aaf0 66 28 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  f( yymsp[-1].min
2aaf1 6f 72 2e 79 79 32 34 32 20 26 26 20 79 79 6d 73  or.yy242 && yyms
2aaf2 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34  p[-1].minor.yy24
2aaf3 32 2d 3e 6e 45 78 70 72 3e 53 51 4c 49 54 45 5f  2->nExpr>SQLITE_
2aaf4 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47  MAX_FUNCTION_ARG
2aaf5 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
2aaf6 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2aaf7 22 74 6f 6f 20 6d 61 6e 79 20 61 72 67 75 6d 65  "too many argume
2aaf8 6e 74 73 20 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  nts on function 
2aaf9 25 54 22 2c 20 26 79 79 6d 73 70 5b 2d 34 5d 2e  %T", &yymsp[-4].
2aafa 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d 0a  minor.yy0);.  }.
2aafb 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
2aafc 34 35 30 20 3d 20 73 71 6c 69 74 65 33 45 78 70  450 = sqlite3Exp
2aafd 72 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65  rFunction(pParse
2aafe 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
2aaff 72 2e 79 79 32 34 32 2c 20 26 79 79 6d 73 70 5b  r.yy242, &yymsp[
2ab00 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  -4].minor.yy0);.
2ab01 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61    sqlite3ExprSpa
2ab02 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  n(yygotominor.yy
2ab03 34 35 30 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d  450,&yymsp[-4].m
2ab04 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
2ab05 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  0].minor.yy0);. 
2ab06 20 69 66 28 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d   if( yymsp[-2].m
2ab07 69 6e 6f 72 2e 79 79 33 31 36 20 26 26 20 79 79  inor.yy316 && yy
2ab08 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20  gotominor.yy450 
2ab09 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  ){.    yygotomin
2ab0a 6f 72 2e 79 79 34 35 30 2d 3e 66 6c 61 67 73 20  or.yy450->flags 
2ab0b 7c 3d 20 45 50 5f 44 69 73 74 69 6e 63 74 3b 0a  |= EP_Distinct;.
2ab0c 20 20 7d 0a 7d 0a 20 20 20 20 20 20 20 20 62 72    }.}.        br
2ab0d 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2ab0e 31 39 31 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  191: /* expr ::=
2ab0f 20 49 44 20 4c 50 20 53 54 41 52 20 52 50 20 2a   ID LP STAR RP *
2ab10 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
2ab11 72 2e 79 79 34 35 30 20 3d 20 73 71 6c 69 74 65  r.yy450 = sqlite
2ab12 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 70 50  3ExprFunction(pP
2ab13 61 72 73 65 2c 20 30 2c 20 26 79 79 6d 73 70 5b  arse, 0, &yymsp[
2ab14 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  -3].minor.yy0);.
2ab15 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61    sqlite3ExprSpa
2ab16 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  n(yygotominor.yy
2ab17 34 35 30 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d  450,&yymsp[-3].m
2ab18 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
2ab19 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d  0].minor.yy0);.}
2ab1a 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2ab1b 20 20 20 20 20 20 63 61 73 65 20 31 39 32 3a 20        case 192: 
2ab1c 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20 43 54 49 4d  /* term ::= CTIM
2ab1d 45 5f 4b 57 20 2a 2f 0a 7b 0a 20 20 2f 2a 20 54  E_KW */.{.  /* T
2ab1e 68 65 20 43 55 52 52 45 4e 54 5f 54 49 4d 45 2c  he CURRENT_TIME,
2ab1f 20 43 55 52 52 45 4e 54 5f 44 41 54 45 2c 20 61   CURRENT_DATE, a
2ab20 6e 64 20 43 55 52 52 45 4e 54 5f 54 49 4d 45 53  nd CURRENT_TIMES
2ab21 54 41 4d 50 20 76 61 6c 75 65 73 20 61 72 65 0a  TAMP values are.
2ab22 20 20 2a 2a 20 74 72 65 61 74 65 64 20 61 73 20    ** treated as 
2ab23 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 72  functions that r
2ab24 65 74 75 72 6e 20 63 6f 6e 73 74 61 6e 74 73 20  eturn constants 
2ab25 2a 2f 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  */.  yygotominor
2ab26 2e 79 79 34 35 30 20 3d 20 73 71 6c 69 74 65 33  .yy450 = sqlite3
2ab27 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 70 50 61  ExprFunction(pPa
2ab28 72 73 65 2c 20 30 2c 26 79 79 6d 73 70 5b 30 5d  rse, 0,&yymsp[0]
2ab29 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 69  .minor.yy0);.  i
2ab2a 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  f( yygotominor.y
2ab2b 79 34 35 30 20 29 7b 0a 20 20 20 20 79 79 67 6f  y450 ){.    yygo
2ab2c 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 2d 3e 6f  tominor.yy450->o
2ab2d 70 20 3d 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e  p = TK_CONST_FUN
2ab2e 43 3b 20 20 0a 20 20 20 20 79 79 67 6f 74 6f 6d  C;  .    yygotom
2ab2f 69 6e 6f 72 2e 79 79 34 35 30 2d 3e 73 70 61 6e  inor.yy450->span
2ab30 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
2ab31 72 2e 79 79 30 3b 0a 20 20 7d 0a 7d 0a 20 20 20  r.yy0;.  }.}.   
2ab32 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2ab33 20 20 63 61 73 65 20 31 39 33 3a 20 2f 2a 20 65    case 193: /* e
2ab34 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 41 4e 44  xpr ::= expr AND
2ab35 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63   expr */.      c
2ab36 61 73 65 20 31 39 34 3a 20 2f 2a 20 65 78 70 72  ase 194: /* expr
2ab37 20 3a 3a 3d 20 65 78 70 72 20 4f 52 20 65 78 70   ::= expr OR exp
2ab38 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  r */.      case 
2ab39 31 39 35 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  195: /* expr ::=
2ab3a 20 65 78 70 72 20 4c 54 7c 47 54 7c 47 45 7c 4c   expr LT|GT|GE|L
2ab3b 45 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20  E expr */.      
2ab3c 63 61 73 65 20 31 39 36 3a 20 2f 2a 20 65 78 70  case 196: /* exp
2ab3d 72 20 3a 3a 3d 20 65 78 70 72 20 45 51 7c 4e 45  r ::= expr EQ|NE
2ab3e 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63   expr */.      c
2ab3f 61 73 65 20 31 39 37 3a 20 2f 2a 20 65 78 70 72  ase 197: /* expr
2ab40 20 3a 3a 3d 20 65 78 70 72 20 42 49 54 41 4e 44   ::= expr BITAND
2ab41 7c 42 49 54 4f 52 7c 4c 53 48 49 46 54 7c 52 53  |BITOR|LSHIFT|RS
2ab42 48 49 46 54 20 65 78 70 72 20 2a 2f 0a 20 20 20  HIFT expr */.   
2ab43 20 20 20 63 61 73 65 20 31 39 38 3a 20 2f 2a 20     case 198: /* 
2ab44 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 50 4c  expr ::= expr PL
2ab45 55 53 7c 4d 49 4e 55 53 20 65 78 70 72 20 2a 2f  US|MINUS expr */
2ab46 0a 20 20 20 20 20 20 63 61 73 65 20 31 39 39 3a  .      case 199:
2ab47 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70   /* expr ::= exp
2ab48 72 20 53 54 41 52 7c 53 4c 41 53 48 7c 52 45 4d  r STAR|SLASH|REM
2ab49 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63   expr */.      c
2ab4a 61 73 65 20 32 30 30 3a 20 2f 2a 20 65 78 70 72  ase 200: /* expr
2ab4b 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4e 43 41 54   ::= expr CONCAT
2ab4c 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f   expr */.{yygoto
2ab4d 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20 73 71  minor.yy450 = sq
2ab4e 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2ab4f 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 61 6a 6f  e,yymsp[-1].majo
2ab50 72 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  r,yymsp[-2].mino
2ab51 72 2e 79 79 34 35 30 2c 79 79 6d 73 70 5b 30 5d  r.yy450,yymsp[0]
2ab52 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 30 29 3b  .minor.yy450,0);
2ab53 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2ab54 0a 20 20 20 20 20 20 63 61 73 65 20 32 30 31 3a  .      case 201:
2ab55 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4c   /* likeop ::= L
2ab56 49 4b 45 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20  IKE_KW */.      
2ab57 63 61 73 65 20 32 30 33 3a 20 2f 2a 20 6c 69 6b  case 203: /* lik
2ab58 65 6f 70 20 3a 3a 3d 20 4d 41 54 43 48 20 2a 2f  eop ::= MATCH */
2ab59 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
2ab5a 38 36 2e 65 4f 70 65 72 61 74 6f 72 20 3d 20 79  86.eOperator = y
2ab5b 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2ab5c 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  0; yygotominor.y
2ab5d 79 38 36 2e 6e 6f 74 20 3d 20 30 3b 7d 0a 20 20  y86.not = 0;}.  
2ab5e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2ab5f 20 20 20 63 61 73 65 20 32 30 32 3a 20 2f 2a 20     case 202: /* 
2ab60 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4c  likeop ::= NOT L
2ab61 49 4b 45 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20  IKE_KW */.      
2ab62 63 61 73 65 20 32 30 34 3a 20 2f 2a 20 6c 69 6b  case 204: /* lik
2ab63 65 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4d 41 54 43  eop ::= NOT MATC
2ab64 48 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  H */.{yygotomino
2ab65 72 2e 79 79 38 36 2e 65 4f 70 65 72 61 74 6f 72  r.yy86.eOperator
2ab66 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
2ab67 72 2e 79 79 30 3b 20 79 79 67 6f 74 6f 6d 69 6e  r.yy0; yygotomin
2ab68 6f 72 2e 79 79 38 36 2e 6e 6f 74 20 3d 20 31 3b  or.yy86.not = 1;
2ab69 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2ab6a 0a 20 20 20 20 20 20 63 61 73 65 20 32 30 37 3a  .      case 207:
2ab6b 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70   /* expr ::= exp
2ab6c 72 20 6c 69 6b 65 6f 70 20 65 78 70 72 20 65 73  r likeop expr es
2ab6d 63 61 70 65 20 2a 2f 0a 7b 0a 20 20 45 78 70 72  cape */.{.  Expr
2ab6e 4c 69 73 74 20 2a 70 4c 69 73 74 3b 0a 20 20 70  List *pList;.  p
2ab6f 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78  List = sqlite3Ex
2ab70 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
2ab71 72 73 65 2c 30 2c 20 79 79 6d 73 70 5b 2d 31 5d  rse,0, yymsp[-1]
2ab72 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 20 30 29  .minor.yy450, 0)
2ab73 3b 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69  ;.  pList = sqli
2ab74 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
2ab75 64 28 70 50 61 72 73 65 2c 70 4c 69 73 74 2c 20  d(pParse,pList, 
2ab76 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
2ab77 79 79 34 35 30 2c 20 30 29 3b 0a 20 20 69 66 28  yy450, 0);.  if(
2ab78 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2ab79 79 79 34 35 30 20 29 7b 0a 20 20 20 20 70 4c 69  yy450 ){.    pLi
2ab7a 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
2ab7b 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
2ab7c 65 2c 70 4c 69 73 74 2c 20 79 79 6d 73 70 5b 30  e,pList, yymsp[0
2ab7d 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 20 30  ].minor.yy450, 0
2ab7e 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d  );.  }.  yygotom
2ab7f 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20 73 71 6c  inor.yy450 = sql
2ab80 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e  ite3ExprFunction
2ab81 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c 20  (pParse, pList, 
2ab82 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
2ab83 2e 79 79 38 36 2e 65 4f 70 65 72 61 74 6f 72 29  .yy86.eOperator)
2ab84 3b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 32  ;.  if( yymsp[-2
2ab85 5d 2e 6d 69 6e 6f 72 2e 79 79 38 36 2e 6e 6f 74  ].minor.yy86.not
2ab86 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79   ) yygotominor.y
2ab87 79 34 35 30 20 3d 20 73 71 6c 69 74 65 33 50 45  y450 = sqlite3PE
2ab88 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e  xpr(pParse, TK_N
2ab89 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  OT, yygotominor.
2ab8a 79 79 34 35 30 2c 20 30 2c 20 30 29 3b 0a 20 20  yy450, 0, 0);.  
2ab8b 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28  sqlite3ExprSpan(
2ab8c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35  yygotominor.yy45
2ab8d 30 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  0, &yymsp[-3].mi
2ab8e 6e 6f 72 2e 79 79 34 35 30 2d 3e 73 70 61 6e 2c  nor.yy450->span,
2ab8f 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f   &yymsp[-1].mino
2ab90 72 2e 79 79 34 35 30 2d 3e 73 70 61 6e 29 3b 0a  r.yy450->span);.
2ab91 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
2ab92 72 2e 79 79 34 35 30 20 29 20 79 79 67 6f 74 6f  r.yy450 ) yygoto
2ab93 6d 69 6e 6f 72 2e 79 79 34 35 30 2d 3e 66 6c 61  minor.yy450->fla
2ab94 67 73 20 7c 3d 20 45 50 5f 49 6e 66 69 78 46 75  gs |= EP_InfixFu
2ab95 6e 63 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  nc;.}.        br
2ab96 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2ab97 32 30 38 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  208: /* expr ::=
2ab98 20 65 78 70 72 20 49 53 4e 55 4c 4c 7c 4e 4f 54   expr ISNULL|NOT
2ab99 4e 55 4c 4c 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f  NULL */.{.  yygo
2ab9a 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20  tominor.yy450 = 
2ab9b 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2ab9c 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 61  rse, yymsp[0].ma
2ab9d 6a 6f 72 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  jor, yymsp[-1].m
2ab9e 69 6e 6f 72 2e 79 79 34 35 30 2c 20 30 2c 20 30  inor.yy450, 0, 0
2ab9f 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
2aba0 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72  Span(yygotominor
2aba1 2e 79 79 34 35 30 2c 26 79 79 6d 73 70 5b 2d 31  .yy450,&yymsp[-1
2aba2 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2d 3e 73  ].minor.yy450->s
2aba3 70 61 6e 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  pan,&yymsp[0].mi
2aba4 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20  nor.yy0);.}.    
2aba5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2aba6 20 63 61 73 65 20 32 30 39 3a 20 2f 2a 20 65 78   case 209: /* ex
2aba7 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 20 4e  pr ::= expr IS N
2aba8 55 4c 4c 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74  ULL */.{.  yygot
2aba9 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20 73  ominor.yy450 = s
2abaa 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2abab 73 65 2c 20 54 4b 5f 49 53 4e 55 4c 4c 2c 20 79  se, TK_ISNULL, y
2abac 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2abad 79 34 35 30 2c 20 30 2c 20 30 29 3b 0a 20 20 73  y450, 0, 0);.  s
2abae 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 79  qlite3ExprSpan(y
2abaf 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30  ygotominor.yy450
2abb0 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  ,&yymsp[-2].mino
2abb1 72 2e 79 79 34 35 30 2d 3e 73 70 61 6e 2c 26 79  r.yy450->span,&y
2abb2 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2abb3 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  0);.}.        br
2abb4 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2abb5 32 31 30 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  210: /* expr ::=
2abb6 20 65 78 70 72 20 4e 4f 54 20 4e 55 4c 4c 20 2a   expr NOT NULL *
2abb7 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
2abb8 72 2e 79 79 34 35 30 20 3d 20 73 71 6c 69 74 65  r.yy450 = sqlite
2abb9 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
2abba 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 79 79 6d 73 70  K_NOTNULL, yymsp
2abbb 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30  [-2].minor.yy450
2abbc 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  , 0, 0);.  sqlit
2abbd 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74  e3ExprSpan(yygot
2abbe 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 26 79 79  ominor.yy450,&yy
2abbf 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2abc0 34 35 30 2d 3e 73 70 61 6e 2c 26 79 79 6d 73 70  450->span,&yymsp
2abc1 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
2abc2 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2abc3 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 31 3a  .      case 211:
2abc4 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70   /* expr ::= exp
2abc5 72 20 49 53 20 4e 4f 54 20 4e 55 4c 4c 20 2a 2f  r IS NOT NULL */
2abc6 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .{.  yygotominor
2abc7 2e 79 79 34 35 30 20 3d 20 73 71 6c 69 74 65 33  .yy450 = sqlite3
2abc8 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2abc9 5f 4e 4f 54 4e 55 4c 4c 2c 20 79 79 6d 73 70 5b  _NOTNULL, yymsp[
2abca 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c  -3].minor.yy450,
2abcb 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65   0, 0);.  sqlite
2abcc 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f  3ExprSpan(yygoto
2abcd 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 26 79 79 6d  minor.yy450,&yym
2abce 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-3].minor.yy4
2abcf 35 30 2d 3e 73 70 61 6e 2c 26 79 79 6d 73 70 5b  50->span,&yymsp[
2abd0 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d  0].minor.yy0);.}
2abd1 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2abd2 20 20 20 20 20 20 63 61 73 65 20 32 31 32 3a 20        case 212: 
2abd3 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 4e 4f 54 20  /* expr ::= NOT 
2abd4 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61  expr */.      ca
2abd5 73 65 20 32 31 33 3a 20 2f 2a 20 65 78 70 72 20  se 213: /* expr 
2abd6 3a 3a 3d 20 42 49 54 4e 4f 54 20 65 78 70 72 20  ::= BITNOT expr 
2abd7 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  */.{.  yygotomin
2abd8 6f 72 2e 79 79 34 35 30 20 3d 20 73 71 6c 69 74  or.yy450 = sqlit
2abd9 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
2abda 79 79 6d 73 70 5b 2d 31 5d 2e 6d 61 6a 6f 72 2c  yymsp[-1].major,
2abdb 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2abdc 79 79 34 35 30 2c 20 30 2c 20 30 29 3b 0a 20 20  yy450, 0, 0);.  
2abdd 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28  sqlite3ExprSpan(
2abde 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35  yygotominor.yy45
2abdf 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  0,&yymsp[-1].min
2abe0 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d  or.yy0,&yymsp[0]
2abe1 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2d 3e 73 70  .minor.yy450->sp
2abe2 61 6e 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  an);.}.        b
2abe3 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2abe4 20 32 31 34 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   214: /* expr ::
2abe5 3d 20 4d 49 4e 55 53 20 65 78 70 72 20 2a 2f 0a  = MINUS expr */.
2abe6 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  {.  yygotominor.
2abe7 79 79 34 35 30 20 3d 20 73 71 6c 69 74 65 33 50  yy450 = sqlite3P
2abe8 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
2abe9 55 4d 49 4e 55 53 2c 20 79 79 6d 73 70 5b 30 5d  UMINUS, yymsp[0]
2abea 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 20 30 2c  .minor.yy450, 0,
2abeb 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78   0);.  sqlite3Ex
2abec 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e  prSpan(yygotomin
2abed 6f 72 2e 79 79 34 35 30 2c 26 79 79 6d 73 70 5b  or.yy450,&yymsp[
2abee 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -1].minor.yy0,&y
2abef 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2abf0 34 35 30 2d 3e 73 70 61 6e 29 3b 0a 7d 0a 20 20  450->span);.}.  
2abf1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2abf2 20 20 20 63 61 73 65 20 32 31 35 3a 20 2f 2a 20     case 215: /* 
2abf3 65 78 70 72 20 3a 3a 3d 20 50 4c 55 53 20 65 78  expr ::= PLUS ex
2abf4 70 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  pr */.{.  yygoto
2abf5 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20 73 71  minor.yy450 = sq
2abf6 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2abf7 65 2c 20 54 4b 5f 55 50 4c 55 53 2c 20 79 79 6d  e, TK_UPLUS, yym
2abf8 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35  sp[0].minor.yy45
2abf9 30 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69  0, 0, 0);.  sqli
2abfa 74 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f  te3ExprSpan(yygo
2abfb 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 26 79  tominor.yy450,&y
2abfc 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2abfd 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0,&yymsp[0].min
2abfe 6f 72 2e 79 79 34 35 30 2d 3e 73 70 61 6e 29 3b  or.yy450->span);
2abff 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2ac00 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 38  ;.      case 218
2ac01 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78  : /* expr ::= ex
2ac02 70 72 20 62 65 74 77 65 65 6e 5f 6f 70 20 65 78  pr between_op ex
2ac03 70 72 20 41 4e 44 20 65 78 70 72 20 2a 2f 0a 7b  pr AND expr */.{
2ac04 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  .  ExprList *pLi
2ac05 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
2ac06 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
2ac07 65 2c 30 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d  e,0, yymsp[-2].m
2ac08 69 6e 6f 72 2e 79 79 34 35 30 2c 20 30 29 3b 0a  inor.yy450, 0);.
2ac09 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65    pList = sqlite
2ac0a 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
2ac0b 70 50 61 72 73 65 2c 70 4c 69 73 74 2c 20 79 79  pParse,pList, yy
2ac0c 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34  msp[0].minor.yy4
2ac0d 35 30 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f  50, 0);.  yygoto
2ac0e 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20 73 71  minor.yy450 = sq
2ac0f 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2ac10 65 2c 20 54 4b 5f 42 45 54 57 45 45 4e 2c 20 79  e, TK_BETWEEN, y
2ac11 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
2ac12 79 34 35 30 2c 20 30 2c 20 30 29 3b 0a 20 20 69  y450, 0, 0);.  i
2ac13 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  f( yygotominor.y
2ac14 79 34 35 30 20 29 7b 0a 20 20 20 20 79 79 67 6f  y450 ){.    yygo
2ac15 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 2d 3e 70  tominor.yy450->p
2ac16 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a 20 20  List = pList;.  
2ac17 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
2ac18 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
2ac19 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4c 69  (pParse->db, pLi
2ac1a 73 74 29 3b 0a 20 20 7d 20 0a 20 20 69 66 28 20  st);.  } .  if( 
2ac1b 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
2ac1c 79 79 33 31 36 20 29 20 79 79 67 6f 74 6f 6d 69  yy316 ) yygotomi
2ac1d 6e 6f 72 2e 79 79 34 35 30 20 3d 20 73 71 6c 69  nor.yy450 = sqli
2ac1e 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2ac1f 20 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d   TK_NOT, yygotom
2ac20 69 6e 6f 72 2e 79 79 34 35 30 2c 20 30 2c 20 30  inor.yy450, 0, 0
2ac21 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
2ac22 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72  Span(yygotominor
2ac23 2e 79 79 34 35 30 2c 26 79 79 6d 73 70 5b 2d 34  .yy450,&yymsp[-4
2ac24 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2d 3e 73  ].minor.yy450->s
2ac25 70 61 6e 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  pan,&yymsp[0].mi
2ac26 6e 6f 72 2e 79 79 34 35 30 2d 3e 73 70 61 6e 29  nor.yy450->span)
2ac27 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
2ac28 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 32  k;.      case 22
2ac29 31 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65  1: /* expr ::= e
2ac2a 78 70 72 20 69 6e 5f 6f 70 20 4c 50 20 65 78 70  xpr in_op LP exp
2ac2b 72 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 0a 20 20  rlist RP */.{.  
2ac2c 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
2ac2d 34 35 30 20 3d 20 73 71 6c 69 74 65 33 50 45 78  450 = sqlite3PEx
2ac2e 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e  pr(pParse, TK_IN
2ac2f 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  , yymsp[-4].mino
2ac30 72 2e 79 79 34 35 30 2c 20 30 2c 20 30 29 3b 0a  r.yy450, 0, 0);.
2ac31 20 20 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69      if( yygotomi
2ac32 6e 6f 72 2e 79 79 34 35 30 20 29 7b 0a 20 20 20  nor.yy450 ){.   
2ac33 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
2ac34 79 34 35 30 2d 3e 70 4c 69 73 74 20 3d 20 79 79  y450->pList = yy
2ac35 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2ac36 32 34 32 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  242;.      sqlit
2ac37 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28  e3ExprSetHeight(
2ac38 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69  pParse, yygotomi
2ac39 6e 6f 72 2e 79 79 34 35 30 29 3b 0a 20 20 20 20  nor.yy450);.    
2ac3a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
2ac3b 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
2ac3c 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79  te(pParse->db, y
2ac3d 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2ac3e 79 32 34 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20  y242);.    }.   
2ac3f 20 69 66 28 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d   if( yymsp[-3].m
2ac40 69 6e 6f 72 2e 79 79 33 31 36 20 29 20 79 79 67  inor.yy316 ) yyg
2ac41 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d  otominor.yy450 =
2ac42 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2ac43 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79  arse, TK_NOT, yy
2ac44 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 2c  gotominor.yy450,
2ac45 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
2ac46 74 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f  te3ExprSpan(yygo
2ac47 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 26 79  tominor.yy450,&y
2ac48 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
2ac49 79 34 35 30 2d 3e 73 70 61 6e 2c 26 79 79 6d 73  y450->span,&yyms
2ac4a 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
2ac4b 0a 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  .  }.        bre
2ac4c 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2ac4d 32 32 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  22: /* expr ::= 
2ac4e 4c 50 20 73 65 6c 65 63 74 20 52 50 20 2a 2f 0a  LP select RP */.
2ac4f 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  {.    yygotomino
2ac50 72 2e 79 79 34 35 30 20 3d 20 73 71 6c 69 74 65  r.yy450 = sqlite
2ac51 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
2ac52 4b 5f 53 45 4c 45 43 54 2c 20 30 2c 20 30 2c 20  K_SELECT, 0, 0, 
2ac53 30 29 3b 0a 20 20 20 20 69 66 28 20 79 79 67 6f  0);.    if( yygo
2ac54 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20 29 7b  tominor.yy450 ){
2ac55 0a 20 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  .      yygotomin
2ac56 6f 72 2e 79 79 34 35 30 2d 3e 70 53 65 6c 65 63  or.yy450->pSelec
2ac57 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  t = yymsp[-1].mi
2ac58 6e 6f 72 2e 79 79 34 33 3b 0a 20 20 20 20 20 20  nor.yy43;.      
2ac59 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65  sqlite3ExprSetHe
2ac5a 69 67 68 74 28 70 50 61 72 73 65 2c 20 79 79 67  ight(pParse, yyg
2ac5b 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 29 3b  otominor.yy450);
2ac5c 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2ac5d 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
2ac5e 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
2ac5f 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
2ac60 72 2e 79 79 34 33 29 3b 0a 20 20 20 20 7d 0a 20  r.yy43);.    }. 
2ac61 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70     sqlite3ExprSp
2ac62 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  an(yygotominor.y
2ac63 79 34 35 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e  y450,&yymsp[-2].
2ac64 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
2ac65 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
2ac66 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61    }.        brea
2ac67 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 32  k;.      case 22
2ac68 33 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65  3: /* expr ::= e
2ac69 78 70 72 20 69 6e 5f 6f 70 20 4c 50 20 73 65 6c  xpr in_op LP sel
2ac6a 65 63 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 20 20  ect RP */.{.    
2ac6b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35  yygotominor.yy45
2ac6c 30 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  0 = sqlite3PExpr
2ac6d 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20  (pParse, TK_IN, 
2ac6e 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
2ac6f 79 79 34 35 30 2c 20 30 2c 20 30 29 3b 0a 20 20  yy450, 0, 0);.  
2ac70 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
2ac71 72 2e 79 79 34 35 30 20 29 7b 0a 20 20 20 20 20  r.yy450 ){.     
2ac72 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
2ac73 35 30 2d 3e 70 53 65 6c 65 63 74 20 3d 20 79 79  50->pSelect = yy
2ac74 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2ac75 34 33 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  43;.      sqlite
2ac76 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70  3ExprSetHeight(p
2ac77 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e  Parse, yygotomin
2ac78 6f 72 2e 79 79 34 35 30 29 3b 0a 20 20 20 20 7d  or.yy450);.    }
2ac79 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
2ac7a 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
2ac7b 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73  pParse->db, yyms
2ac7c 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 33  p[-1].minor.yy43
2ac7d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
2ac7e 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72   yymsp[-3].minor
2ac7f 2e 79 79 33 31 36 20 29 20 79 79 67 6f 74 6f 6d  .yy316 ) yygotom
2ac80 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20 73 71 6c  inor.yy450 = sql
2ac81 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2ac82 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f  , TK_NOT, yygoto
2ac83 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 20 30 2c 20  minor.yy450, 0, 
2ac84 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  0);.    sqlite3E
2ac85 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69  xprSpan(yygotomi
2ac86 6e 6f 72 2e 79 79 34 35 30 2c 26 79 79 6d 73 70  nor.yy450,&yymsp
2ac87 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30  [-4].minor.yy450
2ac88 2d 3e 73 70 61 6e 2c 26 79 79 6d 73 70 5b 30 5d  ->span,&yymsp[0]
2ac89 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d  .minor.yy0);.  }
2ac8a 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2ac8b 20 20 20 20 20 20 63 61 73 65 20 32 32 34 3a 20        case 224: 
2ac8c 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  /* expr ::= expr
2ac8d 20 69 6e 5f 6f 70 20 6e 6d 20 64 62 6e 6d 20 2a   in_op nm dbnm *
2ac8e 2f 0a 7b 0a 20 20 20 20 53 72 63 4c 69 73 74 20  /.{.    SrcList 
2ac8f 2a 70 53 72 63 20 3d 20 73 71 6c 69 74 65 33 53  *pSrc = sqlite3S
2ac90 72 63 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  rcListAppend(pPa
2ac91 72 73 65 2d 3e 64 62 2c 20 30 2c 26 79 79 6d 73  rse->db, 0,&yyms
2ac92 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-1].minor.yy0,
2ac93 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2ac94 79 79 30 29 3b 0a 20 20 20 20 79 79 67 6f 74 6f  yy0);.    yygoto
2ac95 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20 73 71  minor.yy450 = sq
2ac96 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2ac97 65 2c 20 54 4b 5f 49 4e 2c 20 79 79 6d 73 70 5b  e, TK_IN, yymsp[
2ac98 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c  -3].minor.yy450,
2ac99 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20   0, 0);.    if( 
2ac9a 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35  yygotominor.yy45
2ac9b 30 20 29 7b 0a 20 20 20 20 20 20 79 79 67 6f 74  0 ){.      yygot
2ac9c 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 2d 3e 70 53  ominor.yy450->pS
2ac9d 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53  elect = sqlite3S
2ac9e 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c  electNew(pParse,
2ac9f 20 30 2c 70 53 72 63 2c 30 2c 30 2c 30 2c 30 2c   0,pSrc,0,0,0,0,
2aca0 30 2c 30 2c 30 29 3b 0a 20 20 20 20 20 20 73 71  0,0,0);.      sq
2aca1 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67  lite3ExprSetHeig
2aca2 68 74 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74  ht(pParse, yygot
2aca3 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 29 3b 0a 20  ominor.yy450);. 
2aca4 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2aca5 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65  sqlite3SrcListDe
2aca6 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
2aca7 20 70 53 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20   pSrc);.    }.  
2aca8 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 32 5d 2e    if( yymsp[-2].
2aca9 6d 69 6e 6f 72 2e 79 79 33 31 36 20 29 20 79 79  minor.yy316 ) yy
2acaa 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20  gotominor.yy450 
2acab 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2acac 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79  Parse, TK_NOT, y
2acad 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30  ygotominor.yy450
2acae 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
2acaf 69 74 65 33 45 78 70 72 53 70 61 6e 28 79 79 67  ite3ExprSpan(yyg
2acb0 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 26  otominor.yy450,&
2acb1 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
2acb2 79 79 34 35 30 2d 3e 73 70 61 6e 2c 79 79 6d 73  yy450->span,yyms
2acb3 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  p[0].minor.yy0.z
2acb4 3f 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ?&yymsp[0].minor
2acb5 2e 79 79 30 3a 26 79 79 6d 73 70 5b 2d 31 5d 2e  .yy0:&yymsp[-1].
2acb6 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d 0a  minor.yy0);.  }.
2acb7 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2acb8 20 20 20 20 20 63 61 73 65 20 32 32 35 3a 20 2f       case 225: /
2acb9 2a 20 65 78 70 72 20 3a 3a 3d 20 45 58 49 53 54  * expr ::= EXIST
2acba 53 20 4c 50 20 73 65 6c 65 63 74 20 52 50 20 2a  S LP select RP *
2acbb 2f 0a 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 20  /.{.    Expr *p 
2acbc 3d 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  = yygotominor.yy
2acbd 34 35 30 20 3d 20 73 71 6c 69 74 65 33 50 45 78  450 = sqlite3PEx
2acbe 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 45 58  pr(pParse, TK_EX
2acbf 49 53 54 53 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  ISTS, 0, 0, 0);.
2acc0 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20      if( p ){.   
2acc1 20 20 20 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20     p->pSelect = 
2acc2 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2acc3 79 79 34 33 3b 0a 20 20 20 20 20 20 73 71 6c 69  yy43;.      sqli
2acc4 74 65 33 45 78 70 72 53 70 61 6e 28 70 2c 26 79  te3ExprSpan(p,&y
2acc5 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
2acc6 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0,&yymsp[0].min
2acc7 6f 72 2e 79 79 30 29 3b 0a 20 20 20 20 20 20 73  or.yy0);.      s
2acc8 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69  qlite3ExprSetHei
2acc9 67 68 74 28 70 50 61 72 73 65 2c 20 79 79 67 6f  ght(pParse, yygo
2acca 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 29 3b 0a  tominor.yy450);.
2accb 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2accc 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
2accd 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
2acce 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
2accf 2e 79 79 34 33 29 3b 0a 20 20 20 20 7d 0a 20 20  .yy43);.    }.  
2acd0 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2acd1 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 36 3a  .      case 226:
2acd2 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 43 41 53   /* expr ::= CAS
2acd3 45 20 63 61 73 65 5f 6f 70 65 72 61 6e 64 20 63  E case_operand c
2acd4 61 73 65 5f 65 78 70 72 6c 69 73 74 20 63 61 73  ase_exprlist cas
2acd5 65 5f 65 6c 73 65 20 45 4e 44 20 2a 2f 0a 7b 0a  e_else END */.{.
2acd6 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
2acd7 34 35 30 20 3d 20 73 71 6c 69 74 65 33 50 45 78  450 = sqlite3PEx
2acd8 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 43 41  pr(pParse, TK_CA
2acd9 53 45 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  SE, yymsp[-3].mi
2acda 6e 6f 72 2e 79 79 34 35 30 2c 20 79 79 6d 73 70  nor.yy450, yymsp
2acdb 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30  [-1].minor.yy450
2acdc 2c 20 30 29 3b 0a 20 20 69 66 28 20 79 79 67 6f  , 0);.  if( yygo
2acdd 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20 29 7b  tominor.yy450 ){
2acde 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
2acdf 2e 79 79 34 35 30 2d 3e 70 4c 69 73 74 20 3d 20  .yy450->pList = 
2ace0 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2ace1 79 79 32 34 32 3b 0a 20 20 20 20 73 71 6c 69 74  yy242;.    sqlit
2ace2 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28  e3ExprSetHeight(
2ace3 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69  pParse, yygotomi
2ace4 6e 6f 72 2e 79 79 34 35 30 29 3b 0a 20 20 7d 65  nor.yy450);.  }e
2ace5 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
2ace6 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70  ExprListDelete(p
2ace7 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70  Parse->db, yymsp
2ace8 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 32  [-2].minor.yy242
2ace9 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
2acea 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d  ExprSpan(yygotom
2aceb 69 6e 6f 72 2e 79 79 34 35 30 2c 20 26 79 79 6d  inor.yy450, &yym
2acec 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-4].minor.yy0
2aced 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
2acee 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20  r.yy0);.}.      
2acef 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2acf0 61 73 65 20 32 32 37 3a 20 2f 2a 20 63 61 73 65  ase 227: /* case
2acf1 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 63 61  _exprlist ::= ca
2acf2 73 65 5f 65 78 70 72 6c 69 73 74 20 57 48 45 4e  se_exprlist WHEN
2acf3 20 65 78 70 72 20 54 48 45 4e 20 65 78 70 72 20   expr THEN expr 
2acf4 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  */.{.  yygotomin
2acf5 6f 72 2e 79 79 32 34 32 20 3d 20 73 71 6c 69 74  or.yy242 = sqlit
2acf6 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
2acf7 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 34  (pParse,yymsp[-4
2acf8 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 32 2c 20 79  ].minor.yy242, y
2acf9 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2acfa 79 34 35 30 2c 20 30 29 3b 0a 20 20 79 79 67 6f  y450, 0);.  yygo
2acfb 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 32 20 3d 20  tominor.yy242 = 
2acfc 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
2acfd 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 67  ppend(pParse,yyg
2acfe 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 32 2c 20  otominor.yy242, 
2acff 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2ad00 79 34 35 30 2c 20 30 29 3b 0a 7d 0a 20 20 20 20  y450, 0);.}.    
2ad01 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2ad02 20 63 61 73 65 20 32 32 38 3a 20 2f 2a 20 63 61   case 228: /* ca
2ad03 73 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20  se_exprlist ::= 
2ad04 57 48 45 4e 20 65 78 70 72 20 54 48 45 4e 20 65  WHEN expr THEN e
2ad05 78 70 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74  xpr */.{.  yygot
2ad06 6f 6d 69 6e 6f 72 2e 79 79 32 34 32 20 3d 20 73  ominor.yy242 = s
2ad07 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
2ad08 70 65 6e 64 28 70 50 61 72 73 65 2c 30 2c 20 79  pend(pParse,0, y
2ad09 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2ad0a 79 34 35 30 2c 20 30 29 3b 0a 20 20 79 79 67 6f  y450, 0);.  yygo
2ad0b 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 32 20 3d 20  tominor.yy242 = 
2ad0c 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
2ad0d 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 67  ppend(pParse,yyg
2ad0e 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 32 2c 20  otominor.yy242, 
2ad0f 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2ad10 79 34 35 30 2c 20 30 29 3b 0a 7d 0a 20 20 20 20  y450, 0);.}.    
2ad11 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2ad12 20 63 61 73 65 20 32 33 37 3a 20 2f 2a 20 63 6d   case 237: /* cm
2ad13 64 20 3a 3a 3d 20 43 52 45 41 54 45 20 75 6e 69  d ::= CREATE uni
2ad14 71 75 65 66 6c 61 67 20 49 4e 44 45 58 20 69 66  queflag INDEX if
2ad15 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e  notexists nm dbn
2ad16 6d 20 4f 4e 20 6e 6d 20 4c 50 20 69 64 78 6c 69  m ON nm LP idxli
2ad17 73 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 73 71 6c  st RP */.{.  sql
2ad18 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 28  ite3CreateIndex(
2ad19 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d  pParse, &yymsp[-
2ad1a 36 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79  6].minor.yy0, &y
2ad1b 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-5].minor.y
2ad1c 79 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  y0, .           
2ad1d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2ad1e 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 70  3SrcListAppend(p
2ad1f 50 61 72 73 65 2d 3e 64 62 2c 30 2c 26 79 79 6d  Parse->db,0,&yym
2ad20 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
2ad21 2c 30 29 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  ,0), yymsp[-1].m
2ad22 69 6e 6f 72 2e 79 79 32 34 32 2c 20 79 79 6d 73  inor.yy242, yyms
2ad23 70 5b 2d 39 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31  p[-9].minor.yy31
2ad24 36 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  6,.             
2ad25 20 20 20 20 20 20 20 20 20 26 79 79 6d 73 70 5b           &yymsp[
2ad26 2d 31 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  -10].minor.yy0, 
2ad27 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2ad28 79 79 30 2c 20 53 51 4c 49 54 45 5f 53 4f 5f 41  yy0, SQLITE_SO_A
2ad29 53 43 2c 20 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69  SC, yymsp[-7].mi
2ad2a 6e 6f 72 2e 79 79 33 31 36 29 3b 0a 7d 0a 20 20  nor.yy316);.}.  
2ad2b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2ad2c 20 20 20 63 61 73 65 20 32 33 38 3a 20 2f 2a 20     case 238: /* 
2ad2d 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d 20 55  uniqueflag ::= U
2ad2e 4e 49 51 55 45 20 2a 2f 0a 20 20 20 20 20 20 63  NIQUE */.      c
2ad2f 61 73 65 20 32 38 35 3a 20 2f 2a 20 72 61 69 73  ase 285: /* rais
2ad30 65 74 79 70 65 20 3a 3a 3d 20 41 42 4f 52 54 20  etype ::= ABORT 
2ad31 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2ad32 79 79 33 31 36 20 3d 20 4f 45 5f 41 62 6f 72 74  yy316 = OE_Abort
2ad33 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2ad34 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 39  ;.      case 239
2ad35 3a 20 2f 2a 20 75 6e 69 71 75 65 66 6c 61 67 20  : /* uniqueflag 
2ad36 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ::= */.{yygotomi
2ad37 6e 6f 72 2e 79 79 33 31 36 20 3d 20 4f 45 5f 4e  nor.yy316 = OE_N
2ad38 6f 6e 65 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  one;}.        br
2ad39 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2ad3a 32 34 32 3a 20 2f 2a 20 69 64 78 6c 69 73 74 20  242: /* idxlist 
2ad3b 3a 3a 3d 20 69 64 78 6c 69 73 74 20 43 4f 4d 4d  ::= idxlist COMM
2ad3c 41 20 6e 6d 20 63 6f 6c 6c 61 74 65 20 73 6f 72  A nm collate sor
2ad3d 74 6f 72 64 65 72 20 2a 2f 0a 7b 0a 20 20 45 78  torder */.{.  Ex
2ad3e 70 72 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28  pr *p = 0;.  if(
2ad3f 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
2ad40 2e 79 79 30 2e 6e 3e 30 20 29 7b 0a 20 20 20 20  .yy0.n>0 ){.    
2ad41 70 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  p = sqlite3PExpr
2ad42 28 70 50 61 72 73 65 2c 20 54 4b 5f 43 4f 4c 55  (pParse, TK_COLU
2ad43 4d 4e 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20  MN, 0, 0, 0);.  
2ad44 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74    sqlite3ExprSet
2ad45 43 6f 6c 6c 28 70 50 61 72 73 65 2c 20 70 2c 20  Coll(pParse, p, 
2ad46 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
2ad47 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20 79 79 67  .yy0);.  }.  yyg
2ad48 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 32 20 3d  otominor.yy242 =
2ad49 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2ad4a 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79  Append(pParse,yy
2ad4b 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2ad4c 32 34 32 2c 20 70 2c 20 26 79 79 6d 73 70 5b 2d  242, p, &yymsp[-
2ad4d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  2].minor.yy0);. 
2ad4e 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2ad4f 43 68 65 63 6b 4c 65 6e 67 74 68 28 70 50 61 72  CheckLength(pPar
2ad50 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  se, yygotominor.
2ad51 79 79 32 34 32 2c 20 22 69 6e 64 65 78 22 29 3b  yy242, "index");
2ad52 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e  .  if( yygotomin
2ad53 6f 72 2e 79 79 32 34 32 20 29 20 79 79 67 6f 74  or.yy242 ) yygot
2ad54 6f 6d 69 6e 6f 72 2e 79 79 32 34 32 2d 3e 61 5b  ominor.yy242->a[
2ad55 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34  yygotominor.yy24
2ad56 32 2d 3e 6e 45 78 70 72 2d 31 5d 2e 73 6f 72 74  2->nExpr-1].sort
2ad57 4f 72 64 65 72 20 3d 20 79 79 6d 73 70 5b 30 5d  Order = yymsp[0]
2ad58 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 3b 0a 7d 0a  .minor.yy316;.}.
2ad59 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2ad5a 20 20 20 20 20 63 61 73 65 20 32 34 33 3a 20 2f       case 243: /
2ad5b 2a 20 69 64 78 6c 69 73 74 20 3a 3a 3d 20 6e 6d  * idxlist ::= nm
2ad5c 20 63 6f 6c 6c 61 74 65 20 73 6f 72 74 6f 72 64   collate sortord
2ad5d 65 72 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a  er */.{.  Expr *
2ad5e 70 20 3d 20 30 3b 0a 20 20 69 66 28 20 79 79 6d  p = 0;.  if( yym
2ad5f 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
2ad60 2e 6e 3e 30 20 29 7b 0a 20 20 20 20 70 20 3d 20  .n>0 ){.    p = 
2ad61 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2ad62 72 73 65 2c 20 54 4b 5f 43 4f 4c 55 4d 4e 2c 20  rse, TK_COLUMN, 
2ad63 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  0, 0, 0);.    sq
2ad64 6c 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c  lite3ExprSetColl
2ad65 28 70 50 61 72 73 65 2c 20 70 2c 20 26 79 79 6d  (pParse, p, &yym
2ad66 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
2ad67 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d  );.  }.  yygotom
2ad68 69 6e 6f 72 2e 79 79 32 34 32 20 3d 20 73 71 6c  inor.yy242 = sql
2ad69 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
2ad6a 6e 64 28 70 50 61 72 73 65 2c 30 2c 20 70 2c 20  nd(pParse,0, p, 
2ad6b 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
2ad6c 2e 79 79 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  .yy0);.  sqlite3
2ad6d 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 6e  ExprListCheckLen
2ad6e 67 74 68 28 70 50 61 72 73 65 2c 20 79 79 67 6f  gth(pParse, yygo
2ad6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 32 2c 20 22  tominor.yy242, "
2ad70 69 6e 64 65 78 22 29 3b 0a 20 20 69 66 28 20 79  index");.  if( y
2ad71 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 32  ygotominor.yy242
2ad72 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79   ) yygotominor.y
2ad73 79 32 34 32 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69  y242->a[yygotomi
2ad74 6e 6f 72 2e 79 79 32 34 32 2d 3e 6e 45 78 70 72  nor.yy242->nExpr
2ad75 2d 31 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20  -1].sortOrder = 
2ad76 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2ad77 79 33 31 36 3b 0a 7d 0a 20 20 20 20 20 20 20 20  y316;.}.        
2ad78 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2ad79 65 20 32 34 34 3a 20 2f 2a 20 63 6f 6c 6c 61 74  e 244: /* collat
2ad7a 65 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f  e ::= */.{yygoto
2ad7b 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20 30 3b  minor.yy0.z = 0;
2ad7c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30   yygotominor.yy0
2ad7d 2e 6e 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20  .n = 0;}.       
2ad7e 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2ad7f 73 65 20 32 34 36 3a 20 2f 2a 20 63 6d 64 20 3a  se 246: /* cmd :
2ad80 3a 3d 20 44 52 4f 50 20 49 4e 44 45 58 20 69 66  := DROP INDEX if
2ad81 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 20  exists fullname 
2ad82 2a 2f 0a 7b 73 71 6c 69 74 65 33 44 72 6f 70 49  */.{sqlite3DropI
2ad83 6e 64 65 78 28 70 50 61 72 73 65 2c 20 79 79 6d  ndex(pParse, yym
2ad84 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31  sp[0].minor.yy41
2ad85 39 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  9, yymsp[-1].min
2ad86 6f 72 2e 79 79 33 31 36 29 3b 7d 0a 20 20 20 20  or.yy316);}.    
2ad87 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2ad88 20 63 61 73 65 20 32 34 37 3a 20 2f 2a 20 63 6d   case 247: /* cm
2ad89 64 20 3a 3a 3d 20 56 41 43 55 55 4d 20 2a 2f 0a  d ::= VACUUM */.
2ad8a 20 20 20 20 20 20 63 61 73 65 20 32 34 38 3a 20        case 248: 
2ad8b 2f 2a 20 63 6d 64 20 3a 3a 3d 20 56 41 43 55 55  /* cmd ::= VACUU
2ad8c 4d 20 6e 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  M nm */.{sqlite3
2ad8d 56 61 63 75 75 6d 28 70 50 61 72 73 65 29 3b 7d  Vacuum(pParse);}
2ad8e 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2ad8f 20 20 20 20 20 20 63 61 73 65 20 32 34 39 3a 20        case 249: 
2ad90 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d  /* cmd ::= PRAGM
2ad91 41 20 6e 6d 20 64 62 6e 6d 20 45 51 20 6e 6d 6e  A nm dbnm EQ nmn
2ad92 75 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  um */.      case
2ad93 20 32 35 30 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   250: /* cmd ::=
2ad94 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20   PRAGMA nm dbnm 
2ad95 45 51 20 4f 4e 20 2a 2f 0a 20 20 20 20 20 20 63  EQ ON */.      c
2ad96 61 73 65 20 32 35 31 3a 20 2f 2a 20 63 6d 64 20  ase 251: /* cmd 
2ad97 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62  ::= PRAGMA nm db
2ad98 6e 6d 20 45 51 20 44 45 4c 45 54 45 20 2a 2f 0a  nm EQ DELETE */.
2ad99 7b 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28 70  {sqlite3Pragma(p
2ad9a 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 33 5d  Parse,&yymsp[-3]
2ad9b 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
2ad9c 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-2].minor.yy0,
2ad9d 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2ad9e 79 79 30 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20  yy0,0);}.       
2ad9f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2ada0 73 65 20 32 35 32 3a 20 2f 2a 20 63 6d 64 20 3a  se 252: /* cmd :
2ada1 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e  := PRAGMA nm dbn
2ada2 6d 20 45 51 20 6d 69 6e 75 73 5f 6e 75 6d 20 2a  m EQ minus_num *
2ada3 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 50 72 61  /.{.  sqlite3Pra
2ada4 67 6d 61 28 70 50 61 72 73 65 2c 26 79 79 6d 73  gma(pParse,&yyms
2ada5 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-3].minor.yy0,
2ada6 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
2ada7 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  .yy0,&yymsp[0].m
2ada8 69 6e 6f 72 2e 79 79 30 2c 31 29 3b 0a 7d 0a 20  inor.yy0,1);.}. 
2ada9 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2adaa 20 20 20 20 63 61 73 65 20 32 35 33 3a 20 2f 2a      case 253: /*
2adab 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20   cmd ::= PRAGMA 
2adac 6e 6d 20 64 62 6e 6d 20 4c 50 20 6e 6d 6e 75 6d  nm dbnm LP nmnum
2adad 20 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 50   RP */.{sqlite3P
2adae 72 61 67 6d 61 28 70 50 61 72 73 65 2c 26 79 79  ragma(pParse,&yy
2adaf 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2adb0 30 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  0,&yymsp[-3].min
2adb1 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 31  or.yy0,&yymsp[-1
2adb2 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 29 3b 7d  ].minor.yy0,0);}
2adb3 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2adb4 20 20 20 20 20 20 63 61 73 65 20 32 35 34 3a 20        case 254: 
2adb5 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d  /* cmd ::= PRAGM
2adb6 41 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 73 71  A nm dbnm */.{sq
2adb7 6c 69 74 65 33 50 72 61 67 6d 61 28 70 50 61 72  lite3Pragma(pPar
2adb8 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  se,&yymsp[-1].mi
2adb9 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30  nor.yy0,&yymsp[0
2adba 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 2c 30 29  ].minor.yy0,0,0)
2adbb 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2adbc 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 32  ;.      case 262
2adbd 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 43 52 45  : /* cmd ::= CRE
2adbe 41 54 45 20 74 72 69 67 67 65 72 5f 64 65 63 6c  ATE trigger_decl
2adbf 20 42 45 47 49 4e 20 74 72 69 67 67 65 72 5f 63   BEGIN trigger_c
2adc0 6d 64 5f 6c 69 73 74 20 45 4e 44 20 2a 2f 0a 7b  md_list END */.{
2adc1 0a 20 20 54 6f 6b 65 6e 20 61 6c 6c 3b 0a 20 20  .  Token all;.  
2adc2 61 6c 6c 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 33  all.z = yymsp[-3
2adc3 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20  ].minor.yy0.z;. 
2adc4 20 61 6c 6c 2e 6e 20 3d 20 28 79 79 6d 73 70 5b   all.n = (yymsp[
2adc5 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 2d  0].minor.yy0.z -
2adc6 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72   yymsp[-3].minor
2adc7 2e 79 79 30 2e 7a 29 20 2b 20 79 79 6d 73 70 5b  .yy0.z) + yymsp[
2adc8 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3b 0a  0].minor.yy0.n;.
2adc9 20 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 54    sqlite3FinishT
2adca 72 69 67 67 65 72 28 70 50 61 72 73 65 2c 20 79  rigger(pParse, y
2adcb 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2adcc 79 37 35 2c 20 26 61 6c 6c 29 3b 0a 7d 0a 20 20  y75, &all);.}.  
2adcd 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2adce 20 20 20 63 61 73 65 20 32 36 33 3a 20 2f 2a 20     case 263: /* 
2adcf 74 72 69 67 67 65 72 5f 64 65 63 6c 20 3a 3a 3d  trigger_decl ::=
2add0 20 74 65 6d 70 20 54 52 49 47 47 45 52 20 69 66   temp TRIGGER if
2add1 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e  notexists nm dbn
2add2 6d 20 74 72 69 67 67 65 72 5f 74 69 6d 65 20 74  m trigger_time t
2add3 72 69 67 67 65 72 5f 65 76 65 6e 74 20 4f 4e 20  rigger_event ON 
2add4 66 75 6c 6c 6e 61 6d 65 20 66 6f 72 65 61 63 68  fullname foreach
2add5 5f 63 6c 61 75 73 65 20 77 68 65 6e 5f 63 6c 61  _clause when_cla
2add6 75 73 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74  use */.{.  sqlit
2add7 65 33 42 65 67 69 6e 54 72 69 67 67 65 72 28 70  e3BeginTrigger(p
2add8 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 37  Parse, &yymsp[-7
2add9 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79  ].minor.yy0, &yy
2adda 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-6].minor.yy
2addb 30 2c 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e  0, yymsp[-5].min
2addc 6f 72 2e 79 79 33 31 36 2c 20 79 79 6d 73 70 5b  or.yy316, yymsp[
2addd 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 35 34 2e  -4].minor.yy354.
2adde 61 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  a, yymsp[-4].min
2addf 6f 72 2e 79 79 33 35 34 2e 62 2c 20 79 79 6d 73  or.yy354.b, yyms
2ade0 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31  p[-2].minor.yy41
2ade1 39 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  9, yymsp[0].mino
2ade2 72 2e 79 79 34 35 30 2c 20 79 79 6d 73 70 5b 2d  r.yy450, yymsp[-
2ade3 31 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 2c  10].minor.yy316,
2ade4 20 79 79 6d 73 70 5b 2d 38 5d 2e 6d 69 6e 6f 72   yymsp[-8].minor
2ade5 2e 79 79 33 31 36 29 3b 0a 20 20 79 79 67 6f 74  .yy316);.  yygot
2ade6 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d 20 28 79 79  ominor.yy0 = (yy
2ade7 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-6].minor.yy
2ade8 30 2e 6e 3d 3d 30 3f 79 79 6d 73 70 5b 2d 37 5d  0.n==0?yymsp[-7]
2ade9 2e 6d 69 6e 6f 72 2e 79 79 30 3a 79 79 6d 73 70  .minor.yy0:yymsp
2adea 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-6].minor.yy0);
2adeb 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2adec 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 34  ;.      case 264
2aded 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 74 69 6d  : /* trigger_tim
2adee 65 20 3a 3a 3d 20 42 45 46 4f 52 45 20 2a 2f 0a  e ::= BEFORE */.
2adef 20 20 20 20 20 20 63 61 73 65 20 32 36 37 3a 20        case 267: 
2adf0 2f 2a 20 74 72 69 67 67 65 72 5f 74 69 6d 65 20  /* trigger_time 
2adf1 3a 3a 3d 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d  ::= */.{ yygotom
2adf2 69 6e 6f 72 2e 79 79 33 31 36 20 3d 20 54 4b 5f  inor.yy316 = TK_
2adf3 42 45 46 4f 52 45 3b 20 7d 0a 20 20 20 20 20 20  BEFORE; }.      
2adf4 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2adf5 61 73 65 20 32 36 35 3a 20 2f 2a 20 74 72 69 67  ase 265: /* trig
2adf6 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 41 46 54  ger_time ::= AFT
2adf7 45 52 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69  ER */.{ yygotomi
2adf8 6e 6f 72 2e 79 79 33 31 36 20 3d 20 54 4b 5f 41  nor.yy316 = TK_A
2adf9 46 54 45 52 3b 20 20 7d 0a 20 20 20 20 20 20 20  FTER;  }.       
2adfa 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2adfb 73 65 20 32 36 36 3a 20 2f 2a 20 74 72 69 67 67  se 266: /* trigg
2adfc 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 49 4e 53 54  er_time ::= INST
2adfd 45 41 44 20 4f 46 20 2a 2f 0a 7b 20 79 79 67 6f  EAD OF */.{ yygo
2adfe 74 6f 6d 69 6e 6f 72 2e 79 79 33 31 36 20 3d 20  tominor.yy316 = 
2adff 54 4b 5f 49 4e 53 54 45 41 44 3b 7d 0a 20 20 20  TK_INSTEAD;}.   
2ae00 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2ae01 20 20 63 61 73 65 20 32 36 38 3a 20 2f 2a 20 74    case 268: /* t
2ae02 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d  rigger_event ::=
2ae03 20 44 45 4c 45 54 45 7c 49 4e 53 45 52 54 20 2a   DELETE|INSERT *
2ae04 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 39  /.      case 269
2ae05 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 65 76 65  : /* trigger_eve
2ae06 6e 74 20 3a 3a 3d 20 55 50 44 41 54 45 20 2a 2f  nt ::= UPDATE */
2ae07 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
2ae08 33 35 34 2e 61 20 3d 20 79 79 6d 73 70 5b 30 5d  354.a = yymsp[0]
2ae09 2e 6d 61 6a 6f 72 3b 20 79 79 67 6f 74 6f 6d 69  .major; yygotomi
2ae0a 6e 6f 72 2e 79 79 33 35 34 2e 62 20 3d 20 30 3b  nor.yy354.b = 0;
2ae0b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2ae0c 0a 20 20 20 20 20 20 63 61 73 65 20 32 37 30 3a  .      case 270:
2ae0d 20 2f 2a 20 74 72 69 67 67 65 72 5f 65 76 65 6e   /* trigger_even
2ae0e 74 20 3a 3a 3d 20 55 50 44 41 54 45 20 4f 46 20  t ::= UPDATE OF 
2ae0f 69 6e 73 63 6f 6c 6c 69 73 74 20 2a 2f 0a 7b 79  inscollist */.{y
2ae10 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 35 34  ygotominor.yy354
2ae11 2e 61 20 3d 20 54 4b 5f 55 50 44 41 54 45 3b 20  .a = TK_UPDATE; 
2ae12 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 35  yygotominor.yy35
2ae13 34 2e 62 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  4.b = yymsp[0].m
2ae14 69 6e 6f 72 2e 79 79 33 35 32 3b 7d 0a 20 20 20  inor.yy352;}.   
2ae15 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2ae16 20 20 63 61 73 65 20 32 37 33 3a 20 2f 2a 20 77    case 273: /* w
2ae17 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 2a  hen_clause ::= *
2ae18 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 30  /.      case 290
2ae19 3a 20 2f 2a 20 6b 65 79 5f 6f 70 74 20 3a 3a 3d  : /* key_opt ::=
2ae1a 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f   */.{ yygotomino
2ae1b 72 2e 79 79 34 35 30 20 3d 20 30 3b 20 7d 0a 20  r.yy450 = 0; }. 
2ae1c 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2ae1d 20 20 20 20 63 61 73 65 20 32 37 34 3a 20 2f 2a      case 274: /*
2ae1e 20 77 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d   when_clause ::=
2ae1f 20 57 48 45 4e 20 65 78 70 72 20 2a 2f 0a 20 20   WHEN expr */.  
2ae20 20 20 20 20 63 61 73 65 20 32 39 31 3a 20 2f 2a      case 291: /*
2ae21 20 6b 65 79 5f 6f 70 74 20 3a 3a 3d 20 4b 45 59   key_opt ::= KEY
2ae22 20 65 78 70 72 20 2a 2f 0a 7b 20 79 79 67 6f 74   expr */.{ yygot
2ae23 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20 79  ominor.yy450 = y
2ae24 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2ae25 34 35 30 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  450; }.        b
2ae26 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2ae27 20 32 37 35 3a 20 2f 2a 20 74 72 69 67 67 65 72   275: /* trigger
2ae28 5f 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20 74 72  _cmd_list ::= tr
2ae29 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 74  igger_cmd_list t
2ae2a 72 69 67 67 65 72 5f 63 6d 64 20 53 45 4d 49 20  rigger_cmd SEMI 
2ae2b 2a 2f 0a 7b 0a 2f 2a 0a 20 20 69 66 28 20 79 79  */.{./*.  if( yy
2ae2c 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2ae2d 37 35 20 29 7b 0a 20 20 20 20 79 79 6d 73 70 5b  75 ){.    yymsp[
2ae2e 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 37 35 2d 3e  -2].minor.yy75->
2ae2f 70 4c 61 73 74 2d 3e 70 4e 65 78 74 20 3d 20 79  pLast->pNext = y
2ae30 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2ae31 79 37 35 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  y75;.  }else{.  
2ae32 20 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f    yymsp[-2].mino
2ae33 72 2e 79 79 37 35 20 3d 20 79 79 6d 73 70 5b 2d  r.yy75 = yymsp[-
2ae34 31 5d 2e 6d 69 6e 6f 72 2e 79 79 37 35 3b 0a 20  1].minor.yy75;. 
2ae35 20 7d 0a 2a 2f 0a 20 20 61 73 73 65 72 74 28 20   }.*/.  assert( 
2ae36 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2ae37 79 79 37 35 21 3d 30 20 29 3b 0a 20 20 79 79 6d  yy75!=0 );.  yym
2ae38 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 37  sp[-2].minor.yy7
2ae39 35 2d 3e 70 4c 61 73 74 2d 3e 70 4e 65 78 74 20  5->pLast->pNext 
2ae3a 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  = yymsp[-1].mino
2ae3b 72 2e 79 79 37 35 3b 0a 20 20 79 79 6d 73 70 5b  r.yy75;.  yymsp[
2ae3c 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 37 35 2d 3e  -2].minor.yy75->
2ae3d 70 4c 61 73 74 20 3d 20 79 79 6d 73 70 5b 2d 31  pLast = yymsp[-1
2ae3e 5d 2e 6d 69 6e 6f 72 2e 79 79 37 35 3b 0a 20 20  ].minor.yy75;.  
2ae3f 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 35  yygotominor.yy75
2ae40 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e   = yymsp[-2].min
2ae41 6f 72 2e 79 79 37 35 3b 0a 7d 0a 20 20 20 20 20  or.yy75;.}.     
2ae42 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2ae43 63 61 73 65 20 32 37 36 3a 20 2f 2a 20 74 72 69  case 276: /* tri
2ae44 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 3a 3a  gger_cmd_list ::
2ae45 3d 20 74 72 69 67 67 65 72 5f 63 6d 64 20 53 45  = trigger_cmd SE
2ae46 4d 49 20 2a 2f 0a 7b 20 0a 20 20 2f 2a 20 69 66  MI */.{ .  /* if
2ae47 28 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  ( yymsp[-1].mino
2ae48 72 2e 79 79 37 35 20 29 20 2a 2f 0a 20 20 61 73  r.yy75 ) */.  as
2ae49 73 65 72 74 28 20 79 79 6d 73 70 5b 2d 31 5d 2e  sert( yymsp[-1].
2ae4a 6d 69 6e 6f 72 2e 79 79 37 35 21 3d 30 20 29 3b  minor.yy75!=0 );
2ae4b 0a 20 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  .  yymsp[-1].min
2ae4c 6f 72 2e 79 79 37 35 2d 3e 70 4c 61 73 74 20 3d  or.yy75->pLast =
2ae4d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
2ae4e 2e 79 79 37 35 3b 0a 20 20 79 79 67 6f 74 6f 6d  .yy75;.  yygotom
2ae4f 69 6e 6f 72 2e 79 79 37 35 20 3d 20 79 79 6d 73  inor.yy75 = yyms
2ae50 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 37 35  p[-1].minor.yy75
2ae51 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
2ae52 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 37  k;.      case 27
2ae53 37 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d  7: /* trigger_cm
2ae54 64 20 3a 3a 3d 20 55 50 44 41 54 45 20 6f 72 63  d ::= UPDATE orc
2ae55 6f 6e 66 20 6e 6d 20 53 45 54 20 73 65 74 6c 69  onf nm SET setli
2ae56 73 74 20 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a  st where_opt */.
2ae57 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
2ae58 37 35 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67  75 = sqlite3Trig
2ae59 67 65 72 55 70 64 61 74 65 53 74 65 70 28 70 50  gerUpdateStep(pP
2ae5a 61 72 73 65 2d 3e 64 62 2c 20 26 79 79 6d 73 70  arse->db, &yymsp
2ae5b 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-3].minor.yy0, 
2ae5c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2ae5d 79 79 32 34 32 2c 20 79 79 6d 73 70 5b 30 5d 2e  yy242, yymsp[0].
2ae5e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 20 79 79 6d  minor.yy450, yym
2ae5f 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-4].minor.yy3
2ae60 31 36 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  16); }.        b
2ae61 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2ae62 20 32 37 38 3a 20 2f 2a 20 74 72 69 67 67 65 72   278: /* trigger
2ae63 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f  _cmd ::= insert_
2ae64 63 6d 64 20 49 4e 54 4f 20 6e 6d 20 69 6e 73 63  cmd INTO nm insc
2ae65 6f 6c 6c 69 73 74 5f 6f 70 74 20 56 41 4c 55 45  ollist_opt VALUE
2ae66 53 20 4c 50 20 69 74 65 6d 6c 69 73 74 20 52 50  S LP itemlist RP
2ae67 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2ae68 2e 79 79 37 35 20 3d 20 73 71 6c 69 74 65 33 54  .yy75 = sqlite3T
2ae69 72 69 67 67 65 72 49 6e 73 65 72 74 53 74 65 70  riggerInsertStep
2ae6a 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 26 79 79  (pParse->db, &yy
2ae6b 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-5].minor.yy
2ae6c 30 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  0, yymsp[-4].min
2ae6d 6f 72 2e 79 79 33 35 32 2c 20 79 79 6d 73 70 5b  or.yy352, yymsp[
2ae6e 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 32 2c  -1].minor.yy242,
2ae6f 20 30 2c 20 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69   0, yymsp[-7].mi
2ae70 6e 6f 72 2e 79 79 33 31 36 29 3b 7d 0a 20 20 20  nor.yy316);}.   
2ae71 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2ae72 20 20 63 61 73 65 20 32 37 39 3a 20 2f 2a 20 74    case 279: /* t
2ae73 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 69  rigger_cmd ::= i
2ae74 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 6e  nsert_cmd INTO n
2ae75 6d 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74  m inscollist_opt
2ae76 20 73 65 6c 65 63 74 20 2a 2f 0a 7b 79 79 67 6f   select */.{yygo
2ae77 74 6f 6d 69 6e 6f 72 2e 79 79 37 35 20 3d 20 73  tominor.yy75 = s
2ae78 71 6c 69 74 65 33 54 72 69 67 67 65 72 49 6e 73  qlite3TriggerIns
2ae79 65 72 74 53 74 65 70 28 70 50 61 72 73 65 2d 3e  ertStep(pParse->
2ae7a 64 62 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d  db, &yymsp[-2].m
2ae7b 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b  inor.yy0, yymsp[
2ae7c 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 35 32 2c  -1].minor.yy352,
2ae7d 20 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e   0, yymsp[0].min
2ae7e 6f 72 2e 79 79 34 33 2c 20 79 79 6d 73 70 5b 2d  or.yy43, yymsp[-
2ae7f 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 29 3b  4].minor.yy316);
2ae80 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2ae81 0a 20 20 20 20 20 20 63 61 73 65 20 32 38 30 3a  .      case 280:
2ae82 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 20   /* trigger_cmd 
2ae83 3a 3a 3d 20 44 45 4c 45 54 45 20 46 52 4f 4d 20  ::= DELETE FROM 
2ae84 6e 6d 20 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a  nm where_opt */.
2ae85 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37  {yygotominor.yy7
2ae86 35 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67  5 = sqlite3Trigg
2ae87 65 72 44 65 6c 65 74 65 53 74 65 70 28 70 50 61  erDeleteStep(pPa
2ae88 72 73 65 2d 3e 64 62 2c 20 26 79 79 6d 73 70 5b  rse->db, &yymsp[
2ae89 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79  -1].minor.yy0, y
2ae8a 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2ae8b 34 35 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  450);}.        b
2ae8c 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2ae8d 20 32 38 31 3a 20 2f 2a 20 74 72 69 67 67 65 72   281: /* trigger
2ae8e 5f 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63 74 20  _cmd ::= select 
2ae8f 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2ae90 79 79 37 35 20 3d 20 73 71 6c 69 74 65 33 54 72  yy75 = sqlite3Tr
2ae91 69 67 67 65 72 53 65 6c 65 63 74 53 74 65 70 28  iggerSelectStep(
2ae92 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73  pParse->db, yyms
2ae93 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 33 29  p[0].minor.yy43)
2ae94 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ; }.        brea
2ae95 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 38  k;.      case 28
2ae96 32 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 52  2: /* expr ::= R
2ae97 41 49 53 45 20 4c 50 20 49 47 4e 4f 52 45 20 52  AISE LP IGNORE R
2ae98 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d  P */.{.  yygotom
2ae99 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20 73 71 6c  inor.yy450 = sql
2ae9a 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2ae9b 2c 20 54 4b 5f 52 41 49 53 45 2c 20 30 2c 20 30  , TK_RAISE, 0, 0
2ae9c 2c 20 30 29 3b 20 0a 20 20 69 66 28 20 79 79 67  , 0); .  if( yyg
2ae9d 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20 29  otominor.yy450 )
2ae9e 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  {.    yygotomino
2ae9f 72 2e 79 79 34 35 30 2d 3e 69 43 6f 6c 75 6d 6e  r.yy450->iColumn
2aea0 20 3d 20 4f 45 5f 49 67 6e 6f 72 65 3b 0a 20 20   = OE_Ignore;.  
2aea1 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61    sqlite3ExprSpa
2aea2 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  n(yygotominor.yy
2aea3 34 35 30 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e  450, &yymsp[-3].
2aea4 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73  minor.yy0, &yyms
2aea5 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
2aea6 0a 20 20 7d 0a 7d 0a 20 20 20 20 20 20 20 20 62  .  }.}.        b
2aea7 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2aea8 20 32 38 33 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   283: /* expr ::
2aea9 3d 20 52 41 49 53 45 20 4c 50 20 72 61 69 73 65  = RAISE LP raise
2aeaa 74 79 70 65 20 43 4f 4d 4d 41 20 6e 6d 20 52 50  type COMMA nm RP
2aeab 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69   */.{.  yygotomi
2aeac 6e 6f 72 2e 79 79 34 35 30 20 3d 20 73 71 6c 69  nor.yy450 = sqli
2aead 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2aeae 20 54 4b 5f 52 41 49 53 45 2c 20 30 2c 20 30 2c   TK_RAISE, 0, 0,
2aeaf 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f   &yymsp[-1].mino
2aeb0 72 2e 79 79 30 29 3b 20 0a 20 20 69 66 28 20 79  r.yy0); .  if( y
2aeb1 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30  ygotominor.yy450
2aeb2 20 29 20 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d   ) {.    yygotom
2aeb3 69 6e 6f 72 2e 79 79 34 35 30 2d 3e 69 43 6f 6c  inor.yy450->iCol
2aeb4 75 6d 6e 20 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e  umn = yymsp[-3].
2aeb5 6d 69 6e 6f 72 2e 79 79 33 31 36 3b 0a 20 20 20  minor.yy316;.   
2aeb6 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e   sqlite3ExprSpan
2aeb7 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34  (yygotominor.yy4
2aeb8 35 30 2c 20 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d  50, &yymsp[-5].m
2aeb9 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70  inor.yy0, &yymsp
2aeba 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
2aebb 20 20 7d 0a 7d 0a 20 20 20 20 20 20 20 20 62 72    }.}.        br
2aebc 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2aebd 32 38 34 3a 20 2f 2a 20 72 61 69 73 65 74 79 70  284: /* raisetyp
2aebe 65 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20 2a  e ::= ROLLBACK *
2aebf 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
2aec0 79 33 31 36 20 3d 20 4f 45 5f 52 6f 6c 6c 62 61  y316 = OE_Rollba
2aec1 63 6b 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  ck;}.        bre
2aec2 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2aec3 38 36 3a 20 2f 2a 20 72 61 69 73 65 74 79 70 65  86: /* raisetype
2aec4 20 3a 3a 3d 20 46 41 49 4c 20 2a 2f 0a 7b 79 79   ::= FAIL */.{yy
2aec5 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 31 36 20  gotominor.yy316 
2aec6 3d 20 4f 45 5f 46 61 69 6c 3b 7d 0a 20 20 20 20  = OE_Fail;}.    
2aec7 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2aec8 20 63 61 73 65 20 32 38 37 3a 20 2f 2a 20 63 6d   case 287: /* cm
2aec9 64 20 3a 3a 3d 20 44 52 4f 50 20 54 52 49 47 47  d ::= DROP TRIGG
2aeca 45 52 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c  ER ifexists full
2aecb 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  name */.{.  sqli
2aecc 74 65 33 44 72 6f 70 54 72 69 67 67 65 72 28 70  te3DropTrigger(p
2aecd 50 61 72 73 65 2c 79 79 6d 73 70 5b 30 5d 2e 6d  Parse,yymsp[0].m
2aece 69 6e 6f 72 2e 79 79 34 31 39 2c 79 79 6d 73 70  inor.yy419,yymsp
2aecf 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36  [-1].minor.yy316
2aed0 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2aed1 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2aed2 38 38 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41  88: /* cmd ::= A
2aed3 54 54 41 43 48 20 64 61 74 61 62 61 73 65 5f 6b  TTACH database_k
2aed4 77 5f 6f 70 74 20 65 78 70 72 20 41 53 20 65 78  w_opt expr AS ex
2aed5 70 72 20 6b 65 79 5f 6f 70 74 20 2a 2f 0a 7b 0a  pr key_opt */.{.
2aed6 20 20 73 71 6c 69 74 65 33 41 74 74 61 63 68 28    sqlite3Attach(
2aed7 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 33  pParse, yymsp[-3
2aed8 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 20 79  ].minor.yy450, y
2aed9 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2aeda 79 34 35 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  y450, yymsp[0].m
2aedb 69 6e 6f 72 2e 79 79 34 35 30 29 3b 0a 7d 0a 20  inor.yy450);.}. 
2aedc 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2aedd 20 20 20 20 63 61 73 65 20 32 38 39 3a 20 2f 2a      case 289: /*
2aede 20 63 6d 64 20 3a 3a 3d 20 44 45 54 41 43 48 20   cmd ::= DETACH 
2aedf 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20  database_kw_opt 
2aee0 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  expr */.{.  sqli
2aee1 74 65 33 44 65 74 61 63 68 28 70 50 61 72 73 65  te3Detach(pParse
2aee2 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
2aee3 2e 79 79 34 35 30 29 3b 0a 7d 0a 20 20 20 20 20  .yy450);.}.     
2aee4 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2aee5 63 61 73 65 20 32 39 34 3a 20 2f 2a 20 63 6d 64  case 294: /* cmd
2aee6 20 3a 3a 3d 20 52 45 49 4e 44 45 58 20 2a 2f 0a   ::= REINDEX */.
2aee7 7b 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28  {sqlite3Reindex(
2aee8 70 50 61 72 73 65 2c 20 30 2c 20 30 29 3b 7d 0a  pParse, 0, 0);}.
2aee9 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2aeea 20 20 20 20 20 63 61 73 65 20 32 39 35 3a 20 2f       case 295: /
2aeeb 2a 20 63 6d 64 20 3a 3a 3d 20 52 45 49 4e 44 45  * cmd ::= REINDE
2aeec 58 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 73 71  X nm dbnm */.{sq
2aeed 6c 69 74 65 33 52 65 69 6e 64 65 78 28 70 50 61  lite3Reindex(pPa
2aeee 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e  rse, &yymsp[-1].
2aeef 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73  minor.yy0, &yyms
2aef0 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
2aef1 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2aef2 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 36 3a  .      case 296:
2aef3 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41 4e 41 4c   /* cmd ::= ANAL
2aef4 59 5a 45 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41  YZE */.{sqlite3A
2aef5 6e 61 6c 79 7a 65 28 70 50 61 72 73 65 2c 20 30  nalyze(pParse, 0
2aef6 2c 20 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  , 0);}.        b
2aef7 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2aef8 20 32 39 37 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   297: /* cmd ::=
2aef9 20 41 4e 41 4c 59 5a 45 20 6e 6d 20 64 62 6e 6d   ANALYZE nm dbnm
2aefa 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 6e 61 6c   */.{sqlite3Anal
2aefb 79 7a 65 28 70 50 61 72 73 65 2c 20 26 79 79 6d  yze(pParse, &yym
2aefc 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
2aefd 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
2aefe 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20  r.yy0);}.       
2aeff 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2af00 73 65 20 32 39 38 3a 20 2f 2a 20 63 6d 64 20 3a  se 298: /* cmd :
2af01 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c 45 20 66  := ALTER TABLE f
2af02 75 6c 6c 6e 61 6d 65 20 52 45 4e 41 4d 45 20 54  ullname RENAME T
2af03 4f 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  O nm */.{.  sqli
2af04 74 65 33 41 6c 74 65 72 52 65 6e 61 6d 65 54 61  te3AlterRenameTa
2af05 62 6c 65 28 70 50 61 72 73 65 2c 79 79 6d 73 70  ble(pParse,yymsp
2af06 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 39  [-3].minor.yy419
2af07 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
2af08 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20  .yy0);.}.       
2af09 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2af0a 73 65 20 32 39 39 3a 20 2f 2a 20 63 6d 64 20 3a  se 299: /* cmd :
2af0b 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c 45 20 61  := ALTER TABLE a
2af0c 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61  dd_column_fullna
2af0d 6d 65 20 41 44 44 20 6b 77 63 6f 6c 75 6d 6e 5f  me ADD kwcolumn_
2af0e 6f 70 74 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 7b 0a  opt column */.{.
2af0f 20 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 69    sqlite3AlterFi
2af10 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 70 50  nishAddColumn(pP
2af11 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  arse, &yymsp[0].
2af12 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20  minor.yy0);.}.  
2af13 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2af14 20 20 20 63 61 73 65 20 33 30 30 3a 20 2f 2a 20     case 300: /* 
2af15 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e  add_column_fulln
2af16 61 6d 65 20 3a 3a 3d 20 66 75 6c 6c 6e 61 6d 65  ame ::= fullname
2af17 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41   */.{.  sqlite3A
2af18 6c 74 65 72 42 65 67 69 6e 41 64 64 43 6f 6c 75  lterBeginAddColu
2af19 6d 6e 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  mn(pParse, yymsp
2af1a 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 39 29  [0].minor.yy419)
2af1b 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
2af1c 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 30  k;.      case 30
2af1d 33 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 63 72  3: /* cmd ::= cr
2af1e 65 61 74 65 5f 76 74 61 62 20 2a 2f 0a 7b 73 71  eate_vtab */.{sq
2af1f 6c 69 74 65 33 56 74 61 62 46 69 6e 69 73 68 50  lite3VtabFinishP
2af20 61 72 73 65 28 70 50 61 72 73 65 2c 30 29 3b 7d  arse(pParse,0);}
2af21 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2af22 20 20 20 20 20 20 63 61 73 65 20 33 30 34 3a 20        case 304: 
2af23 2f 2a 20 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74  /* cmd ::= creat
2af24 65 5f 76 74 61 62 20 4c 50 20 76 74 61 62 61 72  e_vtab LP vtabar
2af25 67 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 73 71 6c  glist RP */.{sql
2af26 69 74 65 33 56 74 61 62 46 69 6e 69 73 68 50 61  ite3VtabFinishPa
2af27 72 73 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73  rse(pParse,&yyms
2af28 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
2af29 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2af2a 0a 20 20 20 20 20 20 63 61 73 65 20 33 30 35 3a  .      case 305:
2af2b 20 2f 2a 20 63 72 65 61 74 65 5f 76 74 61 62 20   /* create_vtab 
2af2c 3a 3a 3d 20 43 52 45 41 54 45 20 56 49 52 54 55  ::= CREATE VIRTU
2af2d 41 4c 20 54 41 42 4c 45 20 6e 6d 20 64 62 6e 6d  AL TABLE nm dbnm
2af2e 20 55 53 49 4e 47 20 6e 6d 20 2a 2f 0a 7b 0a 20   USING nm */.{. 
2af2f 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 42 65     sqlite3VtabBe
2af30 67 69 6e 50 61 72 73 65 28 70 50 61 72 73 65 2c  ginParse(pParse,
2af31 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f   &yymsp[-3].mino
2af32 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 2d 32  r.yy0, &yymsp[-2
2af33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79  ].minor.yy0, &yy
2af34 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
2af35 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2af36 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33  ak;.      case 3
2af37 30 38 3a 20 2f 2a 20 76 74 61 62 61 72 67 20 3a  08: /* vtabarg :
2af38 3a 3d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 56 74  := */.{sqlite3Vt
2af39 61 62 41 72 67 49 6e 69 74 28 70 50 61 72 73 65  abArgInit(pParse
2af3a 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
2af3b 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 31  k;.      case 31
2af3c 30 3a 20 2f 2a 20 76 74 61 62 61 72 67 74 6f 6b  0: /* vtabargtok
2af3d 65 6e 20 3a 3a 3d 20 41 4e 59 20 2a 2f 0a 20 20  en ::= ANY */.  
2af3e 20 20 20 20 63 61 73 65 20 33 31 31 3a 20 2f 2a      case 311: /*
2af3f 20 76 74 61 62 61 72 67 74 6f 6b 65 6e 20 3a 3a   vtabargtoken ::
2af40 3d 20 6c 70 20 61 6e 79 6c 69 73 74 20 52 50 20  = lp anylist RP 
2af41 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33 31  */.      case 31
2af42 32 3a 20 2f 2a 20 6c 70 20 3a 3a 3d 20 4c 50 20  2: /* lp ::= LP 
2af43 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33 31  */.      case 31
2af44 34 3a 20 2f 2a 20 61 6e 79 6c 69 73 74 20 3a 3a  4: /* anylist ::
2af45 3d 20 61 6e 79 6c 69 73 74 20 41 4e 59 20 2a 2f  = anylist ANY */
2af46 0a 7b 73 71 6c 69 74 65 33 56 74 61 62 41 72 67  .{sqlite3VtabArg
2af47 45 78 74 65 6e 64 28 70 50 61 72 73 65 2c 26 79  Extend(pParse,&y
2af48 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2af49 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
2af4a 61 6b 3b 0a 20 20 7d 3b 0a 20 20 79 79 67 6f 74  ak;.  };.  yygot
2af4b 6f 20 3d 20 79 79 52 75 6c 65 49 6e 66 6f 5b 79  o = yyRuleInfo[y
2af4c 79 72 75 6c 65 6e 6f 5d 2e 6c 68 73 3b 0a 20 20  yruleno].lhs;.  
2af4d 79 79 73 69 7a 65 20 3d 20 79 79 52 75 6c 65 49  yysize = yyRuleI
2af4e 6e 66 6f 5b 79 79 72 75 6c 65 6e 6f 5d 2e 6e 72  nfo[yyruleno].nr
2af4f 68 73 3b 0a 20 20 79 79 70 50 61 72 73 65 72 2d  hs;.  yypParser-
2af50 3e 79 79 69 64 78 20 2d 3d 20 79 79 73 69 7a 65  >yyidx -= yysize
2af51 3b 0a 20 20 79 79 61 63 74 20 3d 20 79 79 5f 66  ;.  yyact = yy_f
2af52 69 6e 64 5f 72 65 64 75 63 65 5f 61 63 74 69 6f  ind_reduce_actio
2af53 6e 28 79 79 6d 73 70 5b 2d 79 79 73 69 7a 65 5d  n(yymsp[-yysize]
2af54 2e 73 74 61 74 65 6e 6f 2c 79 79 67 6f 74 6f 29  .stateno,yygoto)
2af55 3b 0a 20 20 69 66 28 20 79 79 61 63 74 20 3c 20  ;.  if( yyact < 
2af56 59 59 4e 53 54 41 54 45 20 29 7b 0a 23 69 66 64  YYNSTATE ){.#ifd
2af57 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f 2a  ef NDEBUG.    /*
2af58 20 49 66 20 77 65 20 61 72 65 20 6e 6f 74 20 64   If we are not d
2af59 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 68 65  ebugging and the
2af5a 20 72 65 64 75 63 65 20 61 63 74 69 6f 6e 20 70   reduce action p
2af5b 6f 70 70 65 64 20 61 74 20 6c 65 61 73 74 0a 20  opped at least. 
2af5c 20 20 20 2a 2a 20 6f 6e 65 20 65 6c 65 6d 65 6e     ** one elemen
2af5d 74 20 6f 66 66 20 74 68 65 20 73 74 61 63 6b 2c  t off the stack,
2af5e 20 74 68 65 6e 20 77 65 20 63 61 6e 20 70 75 73   then we can pus
2af5f 68 20 74 68 65 20 6e 65 77 20 65 6c 65 6d 65 6e  h the new elemen
2af60 74 20 62 61 63 6b 0a 20 20 20 20 2a 2a 20 6f 6e  t back.    ** on
2af61 74 6f 20 74 68 65 20 73 74 61 63 6b 20 68 65 72  to the stack her
2af62 65 2c 20 61 6e 64 20 73 6b 69 70 20 74 68 65 20  e, and skip the 
2af63 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77 20 74  stack overflow t
2af64 65 73 74 20 69 6e 20 79 79 5f 73 68 69 66 74 28  est in yy_shift(
2af65 29 2e 0a 20 20 20 20 2a 2a 20 54 68 61 74 20 67  )..    ** That g
2af66 69 76 65 73 20 61 20 73 69 67 6e 69 66 69 63 61  ives a significa
2af67 6e 74 20 73 70 65 65 64 20 69 6d 70 72 6f 76 65  nt speed improve
2af68 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 69 66 28  ment. */.    if(
2af69 20 79 79 73 69 7a 65 20 29 7b 0a 20 20 20 20 20   yysize ){.     
2af6a 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64   yypParser->yyid
2af6b 78 2b 2b 3b 0a 20 20 20 20 20 20 79 79 6d 73 70  x++;.      yymsp
2af6c 20 2d 3d 20 79 79 73 69 7a 65 2d 31 3b 0a 20 20   -= yysize-1;.  
2af6d 20 20 20 20 79 79 6d 73 70 2d 3e 73 74 61 74 65      yymsp->state
2af6e 6e 6f 20 3d 20 79 79 61 63 74 3b 0a 20 20 20 20  no = yyact;.    
2af6f 20 20 79 79 6d 73 70 2d 3e 6d 61 6a 6f 72 20 3d    yymsp->major =
2af70 20 79 79 67 6f 74 6f 3b 0a 20 20 20 20 20 20 79   yygoto;.      y
2af71 79 6d 73 70 2d 3e 6d 69 6e 6f 72 20 3d 20 79 79  ymsp->minor = yy
2af72 67 6f 74 6f 6d 69 6e 6f 72 3b 0a 20 20 20 20 7d  gotominor;.    }
2af73 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20  else.#endif.    
2af74 7b 0a 20 20 20 20 20 20 79 79 5f 73 68 69 66 74  {.      yy_shift
2af75 28 79 79 70 50 61 72 73 65 72 2c 79 79 61 63 74  (yypParser,yyact
2af76 2c 79 79 67 6f 74 6f 2c 26 79 79 67 6f 74 6f 6d  ,yygoto,&yygotom
2af77 69 6e 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  inor);.    }.  }
2af78 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
2af79 28 20 79 79 61 63 74 20 3d 3d 20 59 59 4e 53 54  ( yyact == YYNST
2af7a 41 54 45 20 2b 20 59 59 4e 52 55 4c 45 20 2b 20  ATE + YYNRULE + 
2af7b 31 20 29 3b 0a 20 20 20 20 79 79 5f 61 63 63 65  1 );.    yy_acce
2af7c 70 74 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20  pt(yypParser);. 
2af7d 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20   }.}../*.** The 
2af7e 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 65  following code e
2af7f 78 65 63 75 74 65 73 20 77 68 65 6e 20 74 68 65  xecutes when the
2af80 20 70 61 72 73 65 20 66 61 69 6c 73 0a 2a 2f 0a   parse fails.*/.
2af81 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f 70  static void yy_p
2af82 61 72 73 65 5f 66 61 69 6c 65 64 28 0a 20 20 79  arse_failed(.  y
2af83 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73  yParser *yypPars
2af84 65 72 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  er           /* 
2af85 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 29 7b  The parser */.){
2af86 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  .  sqlite3Parser
2af87 41 52 47 5f 46 45 54 43 48 3b 0a 23 69 66 6e 64  ARG_FETCH;.#ifnd
2af88 65 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20  ef NDEBUG.  if( 
2af89 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20  yyTraceFILE ){. 
2af8a 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61     fprintf(yyTra
2af8b 63 65 46 49 4c 45 2c 22 25 73 46 61 69 6c 21 5c  ceFILE,"%sFail!\
2af8c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74  n",yyTracePrompt
2af8d 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
2af8e 77 68 69 6c 65 28 20 79 79 70 50 61 72 73 65 72  while( yypParser
2af8f 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20 79 79 5f  ->yyidx>=0 ) yy_
2af90 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b  pop_parser_stack
2af91 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20 2f  (yypParser);.  /
2af92 2a 20 48 65 72 65 20 63 6f 64 65 20 69 73 20 69  * Here code is i
2af93 6e 73 65 72 74 65 64 20 77 68 69 63 68 20 77 69  nserted which wi
2af94 6c 6c 20 62 65 20 65 78 65 63 75 74 65 64 20 77  ll be executed w
2af95 68 65 6e 65 76 65 72 20 74 68 65 0a 20 20 2a 2a  henever the.  **
2af96 20 70 61 72 73 65 72 20 66 61 69 6c 73 20 2a 2f   parser fails */
2af97 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  .  sqlite3Parser
2af98 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75  ARG_STORE; /* Su
2af99 70 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61  ppress warning a
2af9a 62 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78 74  bout unused %ext
2af9b 72 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 69  ra_argument vari
2af9c 61 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a  able */.}../*.**
2af9d 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
2af9e 6f 64 65 20 65 78 65 63 75 74 65 73 20 77 68 65  ode executes whe
2af9f 6e 20 61 20 73 79 6e 74 61 78 20 65 72 72 6f 72  n a syntax error
2afa0 20 66 69 72 73 74 20 6f 63 63 75 72 73 2e 0a 2a   first occurs..*
2afa1 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79 79  /.static void yy
2afa2 5f 73 79 6e 74 61 78 5f 65 72 72 6f 72 28 0a 20  _syntax_error(. 
2afa3 20 79 79 50 61 72 73 65 72 20 2a 79 79 70 50 61   yyParser *yypPa
2afa4 72 73 65 72 2c 20 20 20 20 20 20 20 20 20 20 20  rser,           
2afa5 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f  /* The parser */
2afa6 0a 20 20 69 6e 74 20 79 79 6d 61 6a 6f 72 2c 20  .  int yymajor, 
2afa7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2afa8 20 20 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74    /* The major t
2afa9 79 70 65 20 6f 66 20 74 68 65 20 65 72 72 6f 72  ype of the error
2afaa 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 59 59 4d 49   token */.  YYMI
2afab 4e 4f 52 54 59 50 45 20 79 79 6d 69 6e 6f 72 20  NORTYPE yyminor 
2afac 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2afad 65 20 6d 69 6e 6f 72 20 74 79 70 65 20 6f 66 20  e minor type of 
2afae 74 68 65 20 65 72 72 6f 72 20 74 6f 6b 65 6e 20  the error token 
2afaf 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 50  */.){.  sqlite3P
2afb0 61 72 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a  arserARG_FETCH;.
2afb1 23 64 65 66 69 6e 65 20 54 4f 4b 45 4e 20 28 79  #define TOKEN (y
2afb2 79 6d 69 6e 6f 72 2e 79 79 30 29 0a 0a 20 20 61  yminor.yy0)..  a
2afb3 73 73 65 72 74 28 20 54 4f 4b 45 4e 2e 7a 5b 30  ssert( TOKEN.z[0
2afb4 5d 20 29 3b 20 20 2f 2a 20 54 68 65 20 74 6f 6b  ] );  /* The tok
2afb5 65 6e 69 7a 65 72 20 61 6c 77 61 79 73 20 67 69  enizer always gi
2afb6 76 65 73 20 75 73 20 61 20 74 6f 6b 65 6e 20 2a  ves us a token *
2afb7 2f 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72  /.  sqlite3Error
2afb8 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 65 61  Msg(pParse, "nea
2afb9 72 20 5c 22 25 54 5c 22 3a 20 73 79 6e 74 61 78  r \"%T\": syntax
2afba 20 65 72 72 6f 72 22 2c 20 26 54 4f 4b 45 4e 29   error", &TOKEN)
2afbb 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 61 72 73  ;.  pParse->pars
2afbc 65 45 72 72 6f 72 20 3d 20 31 3b 0a 20 20 73 71  eError = 1;.  sq
2afbd 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53  lite3ParserARG_S
2afbe 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70 72 65 73  TORE; /* Suppres
2afbf 73 20 77 61 72 6e 69 6e 67 20 61 62 6f 75 74 20  s warning about 
2afc0 75 6e 75 73 65 64 20 25 65 78 74 72 61 5f 61 72  unused %extra_ar
2afc1 67 75 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 20  gument variable 
2afc2 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  */.}../*.** The 
2afc3 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 65 78 65  following is exe
2afc4 63 75 74 65 64 20 77 68 65 6e 20 74 68 65 20 70  cuted when the p
2afc5 61 72 73 65 72 20 61 63 63 65 70 74 73 0a 2a 2f  arser accepts.*/
2afc6 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f  .static void yy_
2afc7 61 63 63 65 70 74 28 0a 20 20 79 79 50 61 72 73  accept(.  yyPars
2afc8 65 72 20 2a 79 79 70 50 61 72 73 65 72 20 20 20  er *yypParser   
2afc9 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
2afca 61 72 73 65 72 20 2a 2f 0a 29 7b 0a 20 20 73 71  arser */.){.  sq
2afcb 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 46  lite3ParserARG_F
2afcc 45 54 43 48 3b 0a 23 69 66 6e 64 65 66 20 4e 44  ETCH;.#ifndef ND
2afcd 45 42 55 47 0a 20 20 69 66 28 20 79 79 54 72 61  EBUG.  if( yyTra
2afce 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 66 70  ceFILE ){.    fp
2afcf 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
2afd0 45 2c 22 25 73 41 63 63 65 70 74 21 5c 6e 22 2c  E,"%sAccept!\n",
2afd1 79 79 54 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a  yyTracePrompt);.
2afd2 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 77 68 69    }.#endif.  whi
2afd3 6c 65 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79  le( yypParser->y
2afd4 79 69 64 78 3e 3d 30 20 29 20 79 79 5f 70 6f 70  yidx>=0 ) yy_pop
2afd5 5f 70 61 72 73 65 72 5f 73 74 61 63 6b 28 79 79  _parser_stack(yy
2afd6 70 50 61 72 73 65 72 29 3b 0a 20 20 2f 2a 20 48  pParser);.  /* H
2afd7 65 72 65 20 63 6f 64 65 20 69 73 20 69 6e 73 65  ere code is inse
2afd8 72 74 65 64 20 77 68 69 63 68 20 77 69 6c 6c 20  rted which will 
2afd9 62 65 20 65 78 65 63 75 74 65 64 20 77 68 65 6e  be executed when
2afda 65 76 65 72 20 74 68 65 0a 20 20 2a 2a 20 70 61  ever the.  ** pa
2afdb 72 73 65 72 20 61 63 63 65 70 74 73 20 2a 2f 0a  rser accepts */.
2afdc 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41    sqlite3ParserA
2afdd 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70  RG_STORE; /* Sup
2afde 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61 62  press warning ab
2afdf 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78 74 72  out unused %extr
2afe0 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 69 61  a_argument varia
2afe1 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a 20 54 68 65  ble */.}../* The
2afe2 20 6d 61 69 6e 20 70 61 72 73 65 72 20 70 72 6f   main parser pro
2afe3 67 72 61 6d 2e 0a 2a 2a 20 54 68 65 20 66 69 72  gram..** The fir
2afe4 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61  st argument is a
2afe5 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74   pointer to a st
2afe6 72 75 63 74 75 72 65 20 6f 62 74 61 69 6e 65 64  ructure obtained
2afe7 20 66 72 6f 6d 0a 2a 2a 20 22 73 71 6c 69 74 65   from.** "sqlite
2afe8 33 50 61 72 73 65 72 41 6c 6c 6f 63 22 20 77 68  3ParserAlloc" wh
2afe9 69 63 68 20 64 65 73 63 72 69 62 65 73 20 74 68  ich describes th
2afea 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20  e current state 
2afeb 6f 66 20 74 68 65 20 70 61 72 73 65 72 2e 0a 2a  of the parser..*
2afec 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67  * The second arg
2afed 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6d 61 6a  ument is the maj
2afee 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e  or token number.
2afef 20 20 54 68 65 20 74 68 69 72 64 20 69 73 0a 2a    The third is.*
2aff0 2a 20 74 68 65 20 6d 69 6e 6f 72 20 74 6f 6b 65  * the minor toke
2aff1 6e 2e 20 20 54 68 65 20 66 6f 75 72 74 68 20 6f  n.  The fourth o
2aff2 70 74 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74  ptional argument
2aff3 20 69 73 20 77 68 61 74 65 76 65 72 20 74 68 65   is whatever the
2aff4 0a 2a 2a 20 75 73 65 72 20 77 61 6e 74 73 20 28  .** user wants (
2aff5 61 6e 64 20 73 70 65 63 69 66 69 65 64 20 69 6e  and specified in
2aff6 20 74 68 65 20 67 72 61 6d 6d 61 72 29 20 61 6e   the grammar) an
2aff7 64 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 66  d is available f
2aff8 6f 72 0a 2a 2a 20 75 73 65 20 62 79 20 74 68 65  or.** use by the
2aff9 20 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73   action routines
2affa 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a  ..**.** Inputs:.
2affb 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20  ** <ul>.** <li> 
2affc 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
2affd 20 70 61 72 73 65 72 20 28 61 6e 20 6f 70 61 71   parser (an opaq
2affe 75 65 20 73 74 72 75 63 74 75 72 65 2e 29 0a 2a  ue structure.).*
2afff 2a 20 3c 6c 69 3e 20 54 68 65 20 6d 61 6a 6f 72  * <li> The major
2b000 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e 0a 2a   token number..*
2b001 2a 20 3c 6c 69 3e 20 54 68 65 20 6d 69 6e 6f 72  * <li> The minor
2b002 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e 0a 2a   token number..*
2b003 2a 20 3c 6c 69 3e 20 41 6e 20 6f 70 74 69 6f 6e  * <li> An option
2b004 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 61 20 67   argument of a g
2b005 72 61 6d 6d 61 72 2d 73 70 65 63 69 66 69 65 64  rammar-specified
2b006 20 74 79 70 65 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a   type..** </ul>.
2b007 2a 2a 0a 2a 2a 20 4f 75 74 70 75 74 73 3a 0a 2a  **.** Outputs:.*
2b008 2a 20 4e 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54  * None..*/.SQLIT
2b009 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2b00a 71 6c 69 74 65 33 50 61 72 73 65 72 28 0a 20 20  qlite3Parser(.  
2b00b 76 6f 69 64 20 2a 79 79 70 2c 20 20 20 20 20 20  void *yyp,      
2b00c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2b00d 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20  The parser */.  
2b00e 69 6e 74 20 79 79 6d 61 6a 6f 72 2c 20 20 20 20  int yymajor,    
2b00f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2b010 54 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20  The major token 
2b011 63 6f 64 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20  code number */. 
2b012 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f   sqlite3ParserTO
2b013 4b 45 4e 54 59 50 45 20 79 79 6d 69 6e 6f 72 20  KENTYPE yyminor 
2b014 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 61 6c        /* The val
2b015 75 65 20 66 6f 72 20 74 68 65 20 74 6f 6b 65 6e  ue for the token
2b016 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72   */.  sqlite3Par
2b017 73 65 72 41 52 47 5f 50 44 45 43 4c 20 20 20 20  serARG_PDECL    
2b018 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70             /* Op
2b019 74 69 6f 6e 61 6c 20 25 65 78 74 72 61 5f 61 72  tional %extra_ar
2b01a 67 75 6d 65 6e 74 20 70 61 72 61 6d 65 74 65 72  gument parameter
2b01b 20 2a 2f 0a 29 7b 0a 20 20 59 59 4d 49 4e 4f 52   */.){.  YYMINOR
2b01c 54 59 50 45 20 79 79 6d 69 6e 6f 72 75 6e 69 6f  TYPE yyminorunio
2b01d 6e 3b 0a 20 20 69 6e 74 20 79 79 61 63 74 3b 20  n;.  int yyact; 
2b01e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2b01f 65 20 70 61 72 73 65 72 20 61 63 74 69 6f 6e 2e  e parser action.
2b020 20 2a 2f 0a 20 20 69 6e 74 20 79 79 65 6e 64 6f   */.  int yyendo
2b021 66 69 6e 70 75 74 3b 20 20 20 20 20 2f 2a 20 54  finput;     /* T
2b022 72 75 65 20 69 66 20 77 65 20 61 72 65 20 61 74  rue if we are at
2b023 20 74 68 65 20 65 6e 64 20 6f 66 20 69 6e 70 75   the end of inpu
2b024 74 20 2a 2f 0a 23 69 66 64 65 66 20 59 59 45 52  t */.#ifdef YYER
2b025 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 69 6e 74 20  RORSYMBOL.  int 
2b026 79 79 65 72 72 6f 72 68 69 74 20 3d 20 30 3b 20  yyerrorhit = 0; 
2b027 20 20 2f 2a 20 54 72 75 65 20 69 66 20 79 79 6d    /* True if yym
2b028 61 6a 6f 72 20 68 61 73 20 69 6e 76 6f 6b 65 64  ajor has invoked
2b029 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 23 65 6e   an error */.#en
2b02a 64 69 66 0a 20 20 79 79 50 61 72 73 65 72 20 2a  dif.  yyParser *
2b02b 79 79 70 50 61 72 73 65 72 3b 20 20 2f 2a 20 54  yypParser;  /* T
2b02c 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 0a 20 20  he parser */..  
2b02d 2f 2a 20 28 72 65 29 69 6e 69 74 69 61 6c 69 7a  /* (re)initializ
2b02e 65 20 74 68 65 20 70 61 72 73 65 72 2c 20 69 66  e the parser, if
2b02f 20 6e 65 63 65 73 73 61 72 79 20 2a 2f 0a 20 20   necessary */.  
2b030 79 79 70 50 61 72 73 65 72 20 3d 20 28 79 79 50  yypParser = (yyP
2b031 61 72 73 65 72 2a 29 79 79 70 3b 0a 20 20 69 66  arser*)yyp;.  if
2b032 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69  ( yypParser->yyi
2b033 64 78 3c 30 20 29 7b 0a 23 69 66 20 59 59 53 54  dx<0 ){.#if YYST
2b034 41 43 4b 44 45 50 54 48 3c 3d 30 0a 20 20 20 20  ACKDEPTH<=0.    
2b035 69 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79  if( yypParser->y
2b036 79 73 74 6b 73 7a 20 3c 3d 30 20 29 7b 0a 20 20  ystksz <=0 ){.  
2b037 20 20 20 20 2f 2a 6d 65 6d 73 65 74 28 26 79 79      /*memset(&yy
2b038 6d 69 6e 6f 72 75 6e 69 6f 6e 2c 20 30 2c 20 73  minorunion, 0, s
2b039 69 7a 65 6f 66 28 79 79 6d 69 6e 6f 72 75 6e 69  izeof(yyminoruni
2b03a 6f 6e 29 29 3b 2a 2f 0a 20 20 20 20 20 20 79 79  on));*/.      yy
2b03b 6d 69 6e 6f 72 75 6e 69 6f 6e 20 3d 20 79 79 7a  minorunion = yyz
2b03c 65 72 6f 6d 69 6e 6f 72 3b 0a 20 20 20 20 20 20  erominor;.      
2b03d 79 79 53 74 61 63 6b 4f 76 65 72 66 6c 6f 77 28  yyStackOverflow(
2b03e 79 79 70 50 61 72 73 65 72 2c 20 26 79 79 6d 69  yypParser, &yymi
2b03f 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20  norunion);.     
2b040 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 23   return;.    }.#
2b041 65 6e 64 69 66 0a 20 20 20 20 79 79 70 50 61 72  endif.    yypPar
2b042 73 65 72 2d 3e 79 79 69 64 78 20 3d 20 30 3b 0a  ser->yyidx = 0;.
2b043 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79      yypParser->y
2b044 79 65 72 72 63 6e 74 20 3d 20 2d 31 3b 0a 20 20  yerrcnt = -1;.  
2b045 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73    yypParser->yys
2b046 74 61 63 6b 5b 30 5d 2e 73 74 61 74 65 6e 6f 20  tack[0].stateno 
2b047 3d 20 30 3b 0a 20 20 20 20 79 79 70 50 61 72 73  = 0;.    yypPars
2b048 65 72 2d 3e 79 79 73 74 61 63 6b 5b 30 5d 2e 6d  er->yystack[0].m
2b049 61 6a 6f 72 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  ajor = 0;.  }.  
2b04a 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 2e 79 79 30  yyminorunion.yy0
2b04b 20 3d 20 79 79 6d 69 6e 6f 72 3b 0a 20 20 79 79   = yyminor;.  yy
2b04c 65 6e 64 6f 66 69 6e 70 75 74 20 3d 20 28 79 79  endofinput = (yy
2b04d 6d 61 6a 6f 72 3d 3d 30 29 3b 0a 20 20 73 71 6c  major==0);.  sql
2b04e 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 54  ite3ParserARG_ST
2b04f 4f 52 45 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44  ORE;..#ifndef ND
2b050 45 42 55 47 0a 20 20 69 66 28 20 79 79 54 72 61  EBUG.  if( yyTra
2b051 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 66 70  ceFILE ){.    fp
2b052 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
2b053 45 2c 22 25 73 49 6e 70 75 74 20 25 73 5c 6e 22  E,"%sInput %s\n"
2b054 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 79  ,yyTracePrompt,y
2b055 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79 6d 61 6a  yTokenName[yymaj
2b056 6f 72 5d 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  or]);.  }.#endif
2b057 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 79 79 61 63  ..  do{.    yyac
2b058 74 20 3d 20 79 79 5f 66 69 6e 64 5f 73 68 69 66  t = yy_find_shif
2b059 74 5f 61 63 74 69 6f 6e 28 79 79 70 50 61 72 73  t_action(yypPars
2b05a 65 72 2c 79 79 6d 61 6a 6f 72 29 3b 0a 20 20 20  er,yymajor);.   
2b05b 20 69 66 28 20 79 79 61 63 74 3c 59 59 4e 53 54   if( yyact<YYNST
2b05c 41 54 45 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ATE ){.      ass
2b05d 65 72 74 28 20 21 79 79 65 6e 64 6f 66 69 6e 70  ert( !yyendofinp
2b05e 75 74 20 29 3b 20 20 2f 2a 20 49 6d 70 6f 73 73  ut );  /* Imposs
2b05f 69 62 6c 65 20 74 6f 20 73 68 69 66 74 20 74 68  ible to shift th
2b060 65 20 24 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20  e $ token */.   
2b061 20 20 20 79 79 5f 73 68 69 66 74 28 79 79 70 50     yy_shift(yypP
2b062 61 72 73 65 72 2c 79 79 61 63 74 2c 79 79 6d 61  arser,yyact,yyma
2b063 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f  jor,&yyminorunio
2b064 6e 29 3b 0a 20 20 20 20 20 20 79 79 70 50 61 72  n);.      yypPar
2b065 73 65 72 2d 3e 79 79 65 72 72 63 6e 74 2d 2d 3b  ser->yyerrcnt--;
2b066 0a 20 20 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d  .      yymajor =
2b067 20 59 59 4e 4f 43 4f 44 45 3b 0a 20 20 20 20 7d   YYNOCODE;.    }
2b068 65 6c 73 65 20 69 66 28 20 79 79 61 63 74 20 3c  else if( yyact <
2b069 20 59 59 4e 53 54 41 54 45 20 2b 20 59 59 4e 52   YYNSTATE + YYNR
2b06a 55 4c 45 20 29 7b 0a 20 20 20 20 20 20 79 79 5f  ULE ){.      yy_
2b06b 72 65 64 75 63 65 28 79 79 70 50 61 72 73 65 72  reduce(yypParser
2b06c 2c 79 79 61 63 74 2d 59 59 4e 53 54 41 54 45 29  ,yyact-YYNSTATE)
2b06d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2b06e 20 20 20 61 73 73 65 72 74 28 20 79 79 61 63 74     assert( yyact
2b06f 20 3d 3d 20 59 59 5f 45 52 52 4f 52 5f 41 43 54   == YY_ERROR_ACT
2b070 49 4f 4e 20 29 3b 0a 23 69 66 64 65 66 20 59 59  ION );.#ifdef YY
2b071 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 20 20  ERRORSYMBOL.    
2b072 20 20 69 6e 74 20 79 79 6d 78 3b 0a 23 65 6e 64    int yymx;.#end
2b073 69 66 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  if.#ifndef NDEBU
2b074 47 0a 20 20 20 20 20 20 69 66 28 20 79 79 54 72  G.      if( yyTr
2b075 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 20  aceFILE ){.     
2b076 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61     fprintf(yyTra
2b077 63 65 46 49 4c 45 2c 22 25 73 53 79 6e 74 61 78  ceFILE,"%sSyntax
2b078 20 45 72 72 6f 72 21 5c 6e 22 2c 79 79 54 72 61   Error!\n",yyTra
2b079 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20 20 20 20  cePrompt);.     
2b07a 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66   }.#endif.#ifdef
2b07b 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20   YYERRORSYMBOL. 
2b07c 20 20 20 20 20 2f 2a 20 41 20 73 79 6e 74 61 78       /* A syntax
2b07d 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72   error has occur
2b07e 72 65 64 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68  red..      ** Th
2b07f 65 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 61 6e  e response to an
2b080 20 65 72 72 6f 72 20 64 65 70 65 6e 64 73 20 75   error depends u
2b081 70 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e  pon whether or n
2b082 6f 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  ot the.      ** 
2b083 67 72 61 6d 6d 61 72 20 64 65 66 69 6e 65 73 20  grammar defines 
2b084 61 6e 20 65 72 72 6f 72 20 74 6f 6b 65 6e 20 22  an error token "
2b085 45 52 52 4f 52 22 2e 20 20 0a 20 20 20 20 20 20  ERROR".  .      
2b086 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73  **.      ** This
2b087 20 69 73 20 77 68 61 74 20 77 65 20 64 6f 20 69   is what we do i
2b088 66 20 74 68 65 20 67 72 61 6d 6d 61 72 20 64 6f  f the grammar do
2b089 65 73 20 64 65 66 69 6e 65 20 45 52 52 4f 52 3a  es define ERROR:
2b08a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
2b08b 2a 2a 20 20 2a 20 43 61 6c 6c 20 74 68 65 20 25  **  * Call the %
2b08c 73 79 6e 74 61 78 5f 65 72 72 6f 72 20 66 75 6e  syntax_error fun
2b08d 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a  ction..      **.
2b08e 20 20 20 20 20 20 2a 2a 20 20 2a 20 42 65 67 69        **  * Begi
2b08f 6e 20 70 6f 70 70 69 6e 67 20 74 68 65 20 73 74  n popping the st
2b090 61 63 6b 20 75 6e 74 69 6c 20 77 65 20 65 6e 74  ack until we ent
2b091 65 72 20 61 20 73 74 61 74 65 20 77 68 65 72 65  er a state where
2b092 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 69 74 20  .      **    it 
2b093 69 73 20 6c 65 67 61 6c 20 74 6f 20 73 68 69 66  is legal to shif
2b094 74 20 74 68 65 20 65 72 72 6f 72 20 73 79 6d 62  t the error symb
2b095 6f 6c 2c 20 74 68 65 6e 20 73 68 69 66 74 0a 20  ol, then shift. 
2b096 20 20 20 20 20 2a 2a 20 20 20 20 74 68 65 20 65       **    the e
2b097 72 72 6f 72 20 73 79 6d 62 6f 6c 2e 0a 20 20 20  rror symbol..   
2b098 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20     **.      **  
2b099 2a 20 53 65 74 20 74 68 65 20 65 72 72 6f 72 20  * Set the error 
2b09a 63 6f 75 6e 74 20 74 6f 20 74 68 72 65 65 2e 0a  count to three..
2b09b 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
2b09c 2a 20 20 2a 20 42 65 67 69 6e 20 61 63 63 65 70  *  * Begin accep
2b09d 74 69 6e 67 20 61 6e 64 20 73 68 69 66 74 69 6e  ting and shiftin
2b09e 67 20 6e 65 77 20 74 6f 6b 65 6e 73 2e 20 20 4e  g new tokens.  N
2b09f 6f 20 6e 65 77 20 65 72 72 6f 72 0a 20 20 20 20  o new error.    
2b0a0 20 20 2a 2a 20 20 20 20 70 72 6f 63 65 73 73 69    **    processi
2b0a1 6e 67 20 77 69 6c 6c 20 6f 63 63 75 72 20 75 6e  ng will occur un
2b0a2 74 69 6c 20 74 68 72 65 65 20 74 6f 6b 65 6e 73  til three tokens
2b0a3 20 68 61 76 65 20 62 65 65 6e 0a 20 20 20 20 20   have been.     
2b0a4 20 2a 2a 20 20 20 20 73 68 69 66 74 65 64 20 73   **    shifted s
2b0a5 75 63 63 65 73 73 66 75 6c 6c 79 2e 0a 20 20 20  uccessfully..   
2b0a6 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2f 0a 20     **.      */. 
2b0a7 20 20 20 20 20 69 66 28 20 79 79 70 50 61 72 73       if( yypPars
2b0a8 65 72 2d 3e 79 79 65 72 72 63 6e 74 3c 30 20 29  er->yyerrcnt<0 )
2b0a9 7b 0a 20 20 20 20 20 20 20 20 79 79 5f 73 79 6e  {.        yy_syn
2b0aa 74 61 78 5f 65 72 72 6f 72 28 79 79 70 50 61 72  tax_error(yypPar
2b0ab 73 65 72 2c 79 79 6d 61 6a 6f 72 2c 79 79 6d 69  ser,yymajor,yymi
2b0ac 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20  norunion);.     
2b0ad 20 7d 0a 20 20 20 20 20 20 79 79 6d 78 20 3d 20   }.      yymx = 
2b0ae 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61  yypParser->yysta
2b0af 63 6b 5b 79 79 70 50 61 72 73 65 72 2d 3e 79 79  ck[yypParser->yy
2b0b0 69 64 78 5d 2e 6d 61 6a 6f 72 3b 0a 20 20 20 20  idx].major;.    
2b0b1 20 20 69 66 28 20 79 79 6d 78 3d 3d 59 59 45 52    if( yymx==YYER
2b0b2 52 4f 52 53 59 4d 42 4f 4c 20 7c 7c 20 79 79 65  RORSYMBOL || yye
2b0b3 72 72 6f 72 68 69 74 20 29 7b 0a 23 69 66 6e 64  rrorhit ){.#ifnd
2b0b4 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 20 20  ef NDEBUG.      
2b0b5 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c    if( yyTraceFIL
2b0b6 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66  E ){.          f
2b0b7 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49  printf(yyTraceFI
2b0b8 4c 45 2c 22 25 73 44 69 73 63 61 72 64 20 69 6e  LE,"%sDiscard in
2b0b9 70 75 74 20 74 6f 6b 65 6e 20 25 73 5c 6e 22 2c  put token %s\n",
2b0ba 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 79 79  .             yy
2b0bb 54 72 61 63 65 50 72 6f 6d 70 74 2c 79 79 54 6f  TracePrompt,yyTo
2b0bc 6b 65 6e 4e 61 6d 65 5b 79 79 6d 61 6a 6f 72 5d  kenName[yymajor]
2b0bd 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e  );.        }.#en
2b0be 64 69 66 0a 20 20 20 20 20 20 20 20 79 79 5f 64  dif.        yy_d
2b0bf 65 73 74 72 75 63 74 6f 72 28 79 79 70 50 61 72  estructor(yypPar
2b0c0 73 65 72 2c 20 79 79 6d 61 6a 6f 72 2c 26 79 79  ser, yymajor,&yy
2b0c1 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20  minorunion);.   
2b0c2 20 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59       yymajor = Y
2b0c3 59 4e 4f 43 4f 44 45 3b 0a 20 20 20 20 20 20 7d  YNOCODE;.      }
2b0c4 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 77  else{.         w
2b0c5 68 69 6c 65 28 0a 20 20 20 20 20 20 20 20 20 20  hile(.          
2b0c6 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
2b0c7 20 3e 3d 20 30 20 26 26 0a 20 20 20 20 20 20 20   >= 0 &&.       
2b0c8 20 20 20 79 79 6d 78 20 21 3d 20 59 59 45 52 52     yymx != YYERR
2b0c9 4f 52 53 59 4d 42 4f 4c 20 26 26 0a 20 20 20 20  ORSYMBOL &&.    
2b0ca 20 20 20 20 20 20 28 79 79 61 63 74 20 3d 20 79        (yyact = y
2b0cb 79 5f 66 69 6e 64 5f 72 65 64 75 63 65 5f 61 63  y_find_reduce_ac
2b0cc 74 69 6f 6e 28 0a 20 20 20 20 20 20 20 20 20 20  tion(.          
2b0cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 79 79                yy
2b0ce 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b  pParser->yystack
2b0cf 5b 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64  [yypParser->yyid
2b0d0 78 5d 2e 73 74 61 74 65 6e 6f 2c 0a 20 20 20 20  x].stateno,.    
2b0d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b0d2 20 20 20 20 59 59 45 52 52 4f 52 53 59 4d 42 4f      YYERRORSYMBO
2b0d3 4c 29 29 20 3e 3d 20 59 59 4e 53 54 41 54 45 0a  L)) >= YYNSTATE.
2b0d4 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20          ){.     
2b0d5 20 20 20 20 20 79 79 5f 70 6f 70 5f 70 61 72 73       yy_pop_pars
2b0d6 65 72 5f 73 74 61 63 6b 28 79 79 70 50 61 72 73  er_stack(yypPars
2b0d7 65 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  er);.        }. 
2b0d8 20 20 20 20 20 20 20 69 66 28 20 79 79 70 50 61         if( yypPa
2b0d9 72 73 65 72 2d 3e 79 79 69 64 78 20 3c 20 30 20  rser->yyidx < 0 
2b0da 7c 7c 20 79 79 6d 61 6a 6f 72 3d 3d 30 20 29 7b  || yymajor==0 ){
2b0db 0a 20 20 20 20 20 20 20 20 20 20 79 79 5f 64 65  .          yy_de
2b0dc 73 74 72 75 63 74 6f 72 28 79 79 70 50 61 72 73  structor(yypPars
2b0dd 65 72 2c 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69  er,yymajor,&yymi
2b0de 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20  norunion);.     
2b0df 20 20 20 20 20 79 79 5f 70 61 72 73 65 5f 66 61       yy_parse_fa
2b0e0 69 6c 65 64 28 79 79 70 50 61 72 73 65 72 29 3b  iled(yypParser);
2b0e1 0a 20 20 20 20 20 20 20 20 20 20 79 79 6d 61 6a  .          yymaj
2b0e2 6f 72 20 3d 20 59 59 4e 4f 43 4f 44 45 3b 0a 20  or = YYNOCODE;. 
2b0e3 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
2b0e4 20 79 79 6d 78 21 3d 59 59 45 52 52 4f 52 53 59   yymx!=YYERRORSY
2b0e5 4d 42 4f 4c 20 29 7b 0a 20 20 20 20 20 20 20 20  MBOL ){.        
2b0e6 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 75 32    YYMINORTYPE u2
2b0e7 3b 0a 20 20 20 20 20 20 20 20 20 20 75 32 2e 59  ;.          u2.Y
2b0e8 59 45 52 52 53 59 4d 44 54 20 3d 20 30 3b 0a 20  YERRSYMDT = 0;. 
2b0e9 20 20 20 20 20 20 20 20 20 79 79 5f 73 68 69 66           yy_shif
2b0ea 74 28 79 79 70 50 61 72 73 65 72 2c 79 79 61 63  t(yypParser,yyac
2b0eb 74 2c 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 2c  t,YYERRORSYMBOL,
2b0ec 26 75 32 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  &u2);.        }.
2b0ed 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 79 79        }.      yy
2b0ee 70 50 61 72 73 65 72 2d 3e 79 79 65 72 72 63 6e  pParser->yyerrcn
2b0ef 74 20 3d 20 33 3b 0a 20 20 20 20 20 20 79 79 65  t = 3;.      yye
2b0f0 72 72 6f 72 68 69 74 20 3d 20 31 3b 0a 23 65 6c  rrorhit = 1;.#el
2b0f1 73 65 20 20 2f 2a 20 59 59 45 52 52 4f 52 53 59  se  /* YYERRORSY
2b0f2 4d 42 4f 4c 20 69 73 20 6e 6f 74 20 64 65 66 69  MBOL is not defi
2b0f3 6e 65 64 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20  ned */.      /* 
2b0f4 54 68 69 73 20 69 73 20 77 68 61 74 20 77 65 20  This is what we 
2b0f5 64 6f 20 69 66 20 74 68 65 20 67 72 61 6d 6d 61  do if the gramma
2b0f6 72 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e  r does not defin
2b0f7 65 20 45 52 52 4f 52 3a 0a 20 20 20 20 20 20 2a  e ERROR:.      *
2b0f8 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 52 65  *.      **  * Re
2b0f9 70 6f 72 74 20 61 6e 20 65 72 72 6f 72 20 6d 65  port an error me
2b0fa 73 73 61 67 65 2c 20 61 6e 64 20 74 68 72 6f 77  ssage, and throw
2b0fb 20 61 77 61 79 20 74 68 65 20 69 6e 70 75 74 20   away the input 
2b0fc 74 6f 6b 65 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a  token..      **.
2b0fd 20 20 20 20 20 20 2a 2a 20 20 2a 20 49 66 20 74        **  * If t
2b0fe 68 65 20 69 6e 70 75 74 20 74 6f 6b 65 6e 20 69  he input token i
2b0ff 73 20 24 2c 20 74 68 65 6e 20 66 61 69 6c 20 74  s $, then fail t
2b100 68 65 20 70 61 72 73 65 2e 0a 20 20 20 20 20 20  he parse..      
2b101 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41 73 20 62  **.      ** As b
2b102 65 66 6f 72 65 2c 20 73 75 62 73 65 71 75 65 6e  efore, subsequen
2b103 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73  t error messages
2b104 20 61 72 65 20 73 75 70 70 72 65 73 73 65 64 20   are suppressed 
2b105 75 6e 74 69 6c 0a 20 20 20 20 20 20 2a 2a 20 74  until.      ** t
2b106 68 72 65 65 20 69 6e 70 75 74 20 74 6f 6b 65 6e  hree input token
2b107 73 20 68 61 76 65 20 62 65 65 6e 20 73 75 63 63  s have been succ
2b108 65 73 73 66 75 6c 6c 79 20 73 68 69 66 74 65 64  essfully shifted
2b109 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
2b10a 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e   if( yypParser->
2b10b 79 79 65 72 72 63 6e 74 3c 3d 30 20 29 7b 0a 20  yyerrcnt<=0 ){. 
2b10c 20 20 20 20 20 20 20 79 79 5f 73 79 6e 74 61 78         yy_syntax
2b10d 5f 65 72 72 6f 72 28 79 79 70 50 61 72 73 65 72  _error(yypParser
2b10e 2c 79 79 6d 61 6a 6f 72 2c 79 79 6d 69 6e 6f 72  ,yymajor,yyminor
2b10f 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 7d 0a  union);.      }.
2b110 20 20 20 20 20 20 79 79 70 50 61 72 73 65 72 2d        yypParser-
2b111 3e 79 79 65 72 72 63 6e 74 20 3d 20 33 3b 0a 20  >yyerrcnt = 3;. 
2b112 20 20 20 20 20 79 79 5f 64 65 73 74 72 75 63 74       yy_destruct
2b113 6f 72 28 79 79 70 50 61 72 73 65 72 2c 79 79 6d  or(yypParser,yym
2b114 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69  ajor,&yyminoruni
2b115 6f 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 79  on);.      if( y
2b116 79 65 6e 64 6f 66 69 6e 70 75 74 20 29 7b 0a 20  yendofinput ){. 
2b117 20 20 20 20 20 20 20 79 79 5f 70 61 72 73 65 5f         yy_parse_
2b118 66 61 69 6c 65 64 28 79 79 70 50 61 72 73 65 72  failed(yypParser
2b119 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
2b11a 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43   yymajor = YYNOC
2b11b 4f 44 45 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  ODE;.#endif.    
2b11c 7d 0a 20 20 7d 77 68 69 6c 65 28 20 79 79 6d 61  }.  }while( yyma
2b11d 6a 6f 72 21 3d 59 59 4e 4f 43 4f 44 45 20 26 26  jor!=YYNOCODE &&
2b11e 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64   yypParser->yyid
2b11f 78 3e 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e  x>=0 );.  return
2b120 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
2b121 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 72 73  **** End of pars
2b122 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
2b123 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b124 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b125 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
2b126 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
2b127 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a  tokenize.c *****
2b128 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b129 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b12a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
2b12b 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a  September 15.**.
2b12c 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
2b12d 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
2b12e 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
2b12f 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
2b130 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
2b131 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
2b132 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
2b133 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
2b134 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
2b135 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2b136 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
2b137 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
2b138 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
2b139 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2b13a 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
2b13b 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
2b13c 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
2b13d 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
2b13e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b13f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b141 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 41  ***********.** A
2b142 6e 20 74 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20  n tokenizer for 
2b143 53 51 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  SQL.**.** This f
2b144 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63  ile contains C c
2b145 6f 64 65 20 74 68 61 74 20 73 70 6c 69 74 73 20  ode that splits 
2b146 61 6e 20 53 51 4c 20 69 6e 70 75 74 20 73 74 72  an SQL input str
2b147 69 6e 67 20 75 70 20 69 6e 74 6f 0a 2a 2a 20 69  ing up into.** i
2b148 6e 64 69 76 69 64 75 61 6c 20 74 6f 6b 65 6e 73  ndividual tokens
2b149 20 61 6e 64 20 73 65 6e 64 73 20 74 68 6f 73 65   and sends those
2b14a 20 74 6f 6b 65 6e 73 20 6f 6e 65 2d 62 79 2d 6f   tokens one-by-o
2b14b 6e 65 20 6f 76 65 72 20 74 6f 20 74 68 65 0a 2a  ne over to the.*
2b14c 2a 20 70 61 72 73 65 72 20 66 6f 72 20 61 6e 61  * parser for ana
2b14d 6c 79 73 69 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  lysis..**.** $Id
2b14e 3a 20 74 6f 6b 65 6e 69 7a 65 2e 63 2c 76 20 31  : tokenize.c,v 1
2b14f 2e 31 35 32 20 32 30 30 38 2f 30 39 2f 30 31 20  .152 2008/09/01 
2b150 31 35 3a 35 32 3a 31 31 20 64 72 68 20 45 78 70  15:52:11 drh Exp
2b151 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65   $.*/../*.** The
2b152 20 63 68 61 72 4d 61 70 28 29 20 6d 61 63 72 6f   charMap() macro
2b153 20 6d 61 70 73 20 61 6c 70 68 61 62 65 74 69 63   maps alphabetic
2b154 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 74 6f   characters into
2b155 20 74 68 65 69 72 0a 2a 2a 20 6c 6f 77 65 72 2d   their.** lower-
2b156 63 61 73 65 20 41 53 43 49 49 20 65 71 75 69 76  case ASCII equiv
2b157 61 6c 65 6e 74 2e 20 20 4f 6e 20 41 53 43 49 49  alent.  On ASCII
2b158 20 6d 61 63 68 69 6e 65 73 2c 20 74 68 69 73 20   machines, this 
2b159 69 73 20 6a 75 73 74 0a 2a 2a 20 61 6e 20 75 70  is just.** an up
2b15a 70 65 72 2d 74 6f 2d 6c 6f 77 65 72 20 63 61 73  per-to-lower cas
2b15b 65 20 6d 61 70 2e 20 20 4f 6e 20 45 42 43 44 49  e map.  On EBCDI
2b15c 43 20 6d 61 63 68 69 6e 65 73 20 77 65 20 61 6c  C machines we al
2b15d 73 6f 20 6e 65 65 64 0a 2a 2a 20 74 6f 20 61 64  so need.** to ad
2b15e 6a 75 73 74 20 74 68 65 20 65 6e 63 6f 64 69 6e  just the encodin
2b15f 67 2e 20 20 4f 6e 6c 79 20 61 6c 70 68 61 62 65  g.  Only alphabe
2b160 74 69 63 20 63 68 61 72 61 63 74 65 72 73 20 61  tic characters a
2b161 6e 64 20 75 6e 64 65 72 73 63 6f 72 65 73 0a 2a  nd underscores.*
2b162 2a 20 6e 65 65 64 20 74 6f 20 62 65 20 74 72 61  * need to be tra
2b163 6e 73 6c 61 74 65 64 2e 0a 2a 2f 0a 23 69 66 64  nslated..*/.#ifd
2b164 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a  ef SQLITE_ASCII.
2b165 23 20 64 65 66 69 6e 65 20 63 68 61 72 4d 61 70  # define charMap
2b166 28 58 29 20 73 71 6c 69 74 65 33 55 70 70 65 72  (X) sqlite3Upper
2b167 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65  ToLower[(unsigne
2b168 64 20 63 68 61 72 29 58 5d 0a 23 65 6e 64 69 66  d char)X].#endif
2b169 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
2b16a 42 43 44 49 43 0a 23 20 64 65 66 69 6e 65 20 63  BCDIC.# define c
2b16b 68 61 72 4d 61 70 28 58 29 20 65 62 63 64 69 63  harMap(X) ebcdic
2b16c 54 6f 41 73 63 69 69 5b 28 75 6e 73 69 67 6e 65  ToAscii[(unsigne
2b16d 64 20 63 68 61 72 29 58 5d 0a 63 6f 6e 73 74 20  d char)X].const 
2b16e 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 65 62  unsigned char eb
2b16f 63 64 69 63 54 6f 41 73 63 69 69 5b 5d 20 3d 20  cdicToAscii[] = 
2b170 7b 0a 2f 2a 20 30 20 20 20 31 20 20 20 32 20 20  {./* 0   1   2  
2b171 20 33 20 20 20 34 20 20 20 35 20 20 20 36 20 20   3   4   5   6  
2b172 20 37 20 20 20 38 20 20 20 39 20 20 20 41 20 20   7   8   9   A  
2b173 20 42 20 20 20 43 20 20 20 44 20 20 20 45 20 20   B   C   D   E  
2b174 20 46 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20   F */.   0,  0, 
2b175 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b176 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b177 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b178 20 30 2c 20 20 30 2c 20 20 2f 2a 20 30 78 20 2a   0,  0,  /* 0x *
2b179 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20  /.   0,  0,  0, 
2b17a 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b17b 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b17c 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b17d 20 30 2c 20 20 2f 2a 20 31 78 20 2a 2f 0a 20 20   0,  /* 1x */.  
2b17e 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b17f 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b180 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b181 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b182 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20 30 2c 20   /* 2x */.   0, 
2b183 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b184 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b185 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b186 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20   0,  0,  0,  /* 
2b187 33 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20  3x */.   0,  0, 
2b188 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b189 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b18a 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b18b 20 30 2c 20 20 30 2c 20 20 2f 2a 20 34 78 20 2a   0,  0,  /* 4x *
2b18c 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20  /.   0,  0,  0, 
2b18d 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b18e 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b18f 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b190 20 30 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a 20 20   0,  /* 5x */.  
2b191 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b192 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b193 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b194 20 30 2c 20 39 35 2c 20 20 30 2c 20 20 30 2c 20   0, 95,  0,  0, 
2b195 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 30 2c 20   /* 6x */.   0, 
2b196 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b197 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b198 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b199 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20   0,  0,  0,  /* 
2b19a 37 78 20 2a 2f 0a 20 20 20 30 2c 20 39 37 2c 20  7x */.   0, 97, 
2b19b 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c 31  98, 99,100,101,1
2b19c 30 32 2c 31 30 33 2c 31 30 34 2c 31 30 35 2c 20  02,103,104,105, 
2b19d 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b19e 20 30 2c 20 20 30 2c 20 20 2f 2a 20 38 78 20 2a   0,  0,  /* 8x *
2b19f 2f 0a 20 20 20 30 2c 31 30 36 2c 31 30 37 2c 31  /.   0,106,107,1
2b1a0 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31  08,109,110,111,1
2b1a1 31 32 2c 31 31 33 2c 31 31 34 2c 20 20 30 2c 20  12,113,114,  0, 
2b1a2 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b1a3 20 30 2c 20 20 2f 2a 20 39 78 20 2a 2f 0a 20 20   0,  /* 9x */.  
2b1a4 20 30 2c 20 20 30 2c 31 31 35 2c 31 31 36 2c 31   0,  0,115,116,1
2b1a5 31 37 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c 31  17,118,119,120,1
2b1a6 32 31 2c 31 32 32 2c 20 20 30 2c 20 20 30 2c 20  21,122,  0,  0, 
2b1a7 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b1a8 20 2f 2a 20 41 78 20 2a 2f 0a 20 20 20 30 2c 20   /* Ax */.   0, 
2b1a9 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b1aa 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b1ab 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b1ac 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20   0,  0,  0,  /* 
2b1ad 42 78 20 2a 2f 0a 20 20 20 30 2c 20 39 37 2c 20  Bx */.   0, 97, 
2b1ae 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c 31  98, 99,100,101,1
2b1af 30 32 2c 31 30 33 2c 31 30 34 2c 31 30 35 2c 20  02,103,104,105, 
2b1b0 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b1b1 20 30 2c 20 20 30 2c 20 20 2f 2a 20 43 78 20 2a   0,  0,  /* Cx *
2b1b2 2f 0a 20 20 20 30 2c 31 30 36 2c 31 30 37 2c 31  /.   0,106,107,1
2b1b3 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31  08,109,110,111,1
2b1b4 31 32 2c 31 31 33 2c 31 31 34 2c 20 20 30 2c 20  12,113,114,  0, 
2b1b5 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b1b6 20 30 2c 20 20 2f 2a 20 44 78 20 2a 2f 0a 20 20   0,  /* Dx */.  
2b1b7 20 30 2c 20 20 30 2c 31 31 35 2c 31 31 36 2c 31   0,  0,115,116,1
2b1b8 31 37 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c 31  17,118,119,120,1
2b1b9 32 31 2c 31 32 32 2c 20 20 30 2c 20 20 30 2c 20  21,122,  0,  0, 
2b1ba 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b1bb 20 2f 2a 20 45 78 20 2a 2f 0a 20 20 20 30 2c 20   /* Ex */.   0, 
2b1bc 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b1bd 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b1be 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
2b1bf 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20   0,  0,  0,  /* 
2b1c0 46 78 20 2a 2f 0a 7d 3b 0a 23 65 6e 64 69 66 0a  Fx */.};.#endif.
2b1c1 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
2b1c2 65 33 4b 65 79 77 6f 72 64 43 6f 64 65 20 66 75  e3KeywordCode fu
2b1c3 6e 63 74 69 6f 6e 20 6c 6f 6f 6b 73 20 75 70 20  nction looks up 
2b1c4 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20 74 6f  an identifier to
2b1c5 20 64 65 74 65 72 6d 69 6e 65 20 69 66 0a 2a 2a   determine if.**
2b1c6 20 69 74 20 69 73 20 61 20 6b 65 79 77 6f 72 64   it is a keyword
2b1c7 2e 20 20 49 66 20 69 74 20 69 73 20 61 20 6b 65  .  If it is a ke
2b1c8 79 77 6f 72 64 2c 20 74 68 65 20 74 6f 6b 65 6e  yword, the token
2b1c9 20 63 6f 64 65 20 6f 66 20 74 68 61 74 20 6b 65   code of that ke
2b1ca 79 77 6f 72 64 20 69 73 20 0a 2a 2a 20 72 65 74  yword is .** ret
2b1cb 75 72 6e 65 64 2e 20 20 49 66 20 74 68 65 20 69  urned.  If the i
2b1cc 6e 70 75 74 20 69 73 20 6e 6f 74 20 61 20 6b 65  nput is not a ke
2b1cd 79 77 6f 72 64 2c 20 54 4b 5f 49 44 20 69 73 20  yword, TK_ID is 
2b1ce 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
2b1cf 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  The implementati
2b1d0 6f 6e 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69  on of this routi
2b1d1 6e 65 20 77 61 73 20 67 65 6e 65 72 61 74 65 64  ne was generated
2b1d2 20 62 79 20 61 20 70 72 6f 67 72 61 6d 2c 0a 2a   by a program,.*
2b1d3 2a 20 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68 2e  * mkkeywordhash.
2b1d4 68 2c 20 6c 6f 63 61 74 65 64 20 69 6e 20 74 68  h, located in th
2b1d5 65 20 74 6f 6f 6c 20 73 75 62 64 69 72 65 63 74  e tool subdirect
2b1d6 6f 72 79 20 6f 66 20 74 68 65 20 64 69 73 74 72  ory of the distr
2b1d7 69 62 75 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20  ibution..** The 
2b1d8 6f 75 74 70 75 74 20 6f 66 20 74 68 65 20 6d 6b  output of the mk
2b1d9 6b 65 79 77 6f 72 64 68 61 73 68 2e 63 20 70 72  keywordhash.c pr
2b1da 6f 67 72 61 6d 20 69 73 20 77 72 69 74 74 65 6e  ogram is written
2b1db 20 69 6e 74 6f 20 61 20 66 69 6c 65 0a 2a 2a 20   into a file.** 
2b1dc 6e 61 6d 65 64 20 6b 65 79 77 6f 72 64 68 61 73  named keywordhas
2b1dd 68 2e 68 20 61 6e 64 20 74 68 65 6e 20 69 6e 63  h.h and then inc
2b1de 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 69 73 20  luded into this 
2b1df 73 6f 75 72 63 65 20 66 69 6c 65 20 62 79 0a 2a  source file by.*
2b1e0 2a 20 74 68 65 20 23 69 6e 63 6c 75 64 65 20 62  * the #include b
2b1e1 65 6c 6f 77 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  elow..*/./******
2b1e2 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
2b1e3 20 6b 65 79 77 6f 72 64 68 61 73 68 2e 68 20 69   keywordhash.h i
2b1e4 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
2b1e5 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a  tokenize.c *****
2b1e6 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
2b1e7 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
2b1e8 69 6c 65 20 6b 65 79 77 6f 72 64 68 61 73 68 2e  ile keywordhash.
2b1e9 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
2b1ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b1eb 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 20  *******/./***** 
2b1ec 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
2b1ed 6e 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ns automatically
2b1ee 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20   generated code 
2b1ef 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65  ******.**.** The
2b1f0 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69   code in this fi
2b1f1 6c 65 20 68 61 73 20 62 65 65 6e 20 61 75 74 6f  le has been auto
2b1f2 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 61  matically genera
2b1f3 74 65 64 20 62 79 0a 2a 2a 0a 2a 2a 20 20 20 20  ted by.**.**    
2b1f4 20 24 48 65 61 64 65 72 3a 20 2f 73 71 6c 69 74   $Header: /sqlit
2b1f5 65 2f 73 71 6c 69 74 65 2f 74 6f 6f 6c 2f 6d 6b  e/sqlite/tool/mk
2b1f6 6b 65 79 77 6f 72 64 68 61 73 68 2e 63 2c 76 20  keywordhash.c,v 
2b1f7 31 2e 33 32 20 32 30 30 38 2f 31 30 2f 30 36 20  1.32 2008/10/06 
2b1f8 30 35 3a 33 32 3a 31 39 20 64 61 6e 69 65 6c 6b  05:32:19 danielk
2b1f9 31 39 37 37 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a  1977 Exp $.**.**
2b1fa 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   The code in thi
2b1fb 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  s file implement
2b1fc 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 68 61  s a function tha
2b1fd 74 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65  t determines whe
2b1fe 74 68 65 72 0a 2a 2a 20 6f 72 20 6e 6f 74 20 61  ther.** or not a
2b1ff 20 67 69 76 65 6e 20 69 64 65 6e 74 69 66 69 65   given identifie
2b200 72 20 69 73 20 72 65 61 6c 6c 79 20 61 6e 20 53  r is really an S
2b201 51 4c 20 6b 65 79 77 6f 72 64 2e 20 20 54 68 65  QL keyword.  The
2b202 20 73 61 6d 65 20 74 68 69 6e 67 0a 2a 2a 20 6d   same thing.** m
2b203 69 67 68 74 20 62 65 20 69 6d 70 6c 65 6d 65 6e  ight be implemen
2b204 74 65 64 20 6d 6f 72 65 20 64 69 72 65 63 74 6c  ted more directl
2b205 79 20 75 73 69 6e 67 20 61 20 68 61 6e 64 2d 77  y using a hand-w
2b206 72 69 74 74 65 6e 20 68 61 73 68 20 74 61 62 6c  ritten hash tabl
2b207 65 2e 0a 2a 2a 20 42 75 74 20 62 79 20 75 73 69  e..** But by usi
2b208 6e 67 20 74 68 69 73 20 61 75 74 6f 6d 61 74 69  ng this automati
2b209 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20  cally generated 
2b20a 63 6f 64 65 2c 20 74 68 65 20 73 69 7a 65 20 6f  code, the size o
2b20b 66 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 69 73  f the code.** is
2b20c 20 73 75 62 73 74 61 6e 74 69 61 6c 6c 79 20 72   substantially r
2b20d 65 64 75 63 65 64 2e 20 20 54 68 69 73 20 69 73  educed.  This is
2b20e 20 69 6d 70 6f 72 74 61 6e 74 20 66 6f 72 20 65   important for e
2b20f 6d 62 65 64 64 65 64 20 61 70 70 6c 69 63 61 74  mbedded applicat
2b210 69 6f 6e 73 0a 2a 2a 20 6f 6e 20 70 6c 61 74 66  ions.** on platf
2b211 6f 72 6d 73 20 77 69 74 68 20 6c 69 6d 69 74 65  orms with limite
2b212 64 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 2f 2a 20  d memory..*/./* 
2b213 48 61 73 68 20 73 63 6f 72 65 3a 20 31 36 37 20  Hash score: 167 
2b214 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6b 65  */.static int ke
2b215 79 77 6f 72 64 43 6f 64 65 28 63 6f 6e 73 74 20  ywordCode(const 
2b216 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b  char *z, int n){
2b217 0a 20 20 2f 2a 20 7a 54 65 78 74 5b 5d 20 65 6e  .  /* zText[] en
2b218 63 6f 64 65 73 20 37 38 33 20 62 79 74 65 73 20  codes 783 bytes 
2b219 6f 66 20 6b 65 79 77 6f 72 64 73 20 69 6e 20 35  of keywords in 5
2b21a 32 38 20 62 79 74 65 73 20 2a 2f 0a 20 20 73 74  28 bytes */.  st
2b21b 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
2b21c 7a 54 65 78 74 5b 35 32 38 5d 20 3d 0a 20 20 20  zText[528] =.   
2b21d 20 22 52 45 49 4e 44 45 58 45 44 45 53 43 41 50   "REINDEXEDESCAP
2b21e 45 41 43 48 45 43 4b 45 59 42 45 46 4f 52 45 49  EACHECKEYBEFOREI
2b21f 47 4e 4f 52 45 47 45 58 50 4c 41 49 4e 53 54 45  GNOREGEXPLAINSTE
2b220 41 44 44 41 54 41 42 41 53 45 4c 45 43 54 41 42  ADDATABASELECTAB
2b221 4c 45 22 0a 20 20 20 20 22 46 54 48 45 4e 44 45  LE".    "FTHENDE
2b222 46 45 52 52 41 42 4c 45 4c 53 45 58 43 45 50 54  FERRABLELSEXCEPT
2b223 52 41 4e 53 41 43 54 49 4f 4e 41 54 55 52 41 4c  RANSACTIONATURAL
2b224 54 45 52 41 49 53 45 58 43 4c 55 53 49 56 45 58  TERAISEXCLUSIVEX
2b225 49 53 54 53 43 4f 4e 53 54 52 41 49 4e 54 22 0a  ISTSCONSTRAINT".
2b226 20 20 20 20 22 45 52 53 45 43 54 52 49 47 47 45      "ERSECTRIGGE
2b227 52 45 46 45 52 45 4e 43 45 53 55 4e 49 51 55 45  REFERENCESUNIQUE
2b228 52 59 41 54 54 41 43 48 41 56 49 4e 47 52 4f 55  RYATTACHAVINGROU
2b229 50 44 41 54 45 4d 50 4f 52 41 52 59 42 45 47 49  PDATEMPORARYBEGI
2b22a 4e 4e 45 52 22 0a 20 20 20 20 22 45 4e 41 4d 45  NNER".    "ENAME
2b22b 42 45 54 57 45 45 4e 4f 54 4e 55 4c 4c 49 4b 45  BETWEENOTNULLIKE
2b22c 43 41 53 43 41 44 45 4c 45 54 45 43 41 53 45 43  CASCADELETECASEC
2b22d 4f 4c 4c 41 54 45 43 52 45 41 54 45 43 55 52 52  OLLATECREATECURR
2b22e 45 4e 54 5f 44 41 54 45 22 0a 20 20 20 20 22 44  ENT_DATE".    "D
2b22f 45 54 41 43 48 49 4d 4d 45 44 49 41 54 45 4a 4f  ETACHIMMEDIATEJO
2b230 49 4e 53 45 52 54 4d 41 54 43 48 50 4c 41 4e 41  INSERTMATCHPLANA
2b231 4c 59 5a 45 50 52 41 47 4d 41 42 4f 52 54 56 41  LYZEPRAGMABORTVA
2b232 4c 55 45 53 56 49 52 54 55 41 4c 49 4d 49 54 22  LUESVIRTUALIMIT"
2b233 0a 20 20 20 20 22 57 48 45 4e 57 48 45 52 45 50  .    "WHENWHEREP
2b234 4c 41 43 45 41 46 54 45 52 45 53 54 52 49 43 54  LACEAFTERESTRICT
2b235 41 4e 44 45 46 41 55 4c 54 41 55 54 4f 49 4e 43  ANDEFAULTAUTOINC
2b236 52 45 4d 45 4e 54 43 41 53 54 43 4f 4c 55 4d 4e  REMENTCASTCOLUMN
2b237 43 4f 4d 4d 49 54 22 0a 20 20 20 20 22 43 4f 4e  COMMIT".    "CON
2b238 46 4c 49 43 54 43 52 4f 53 53 43 55 52 52 45 4e  FLICTCROSSCURREN
2b239 54 5f 54 49 4d 45 53 54 41 4d 50 52 49 4d 41 52  T_TIMESTAMPRIMAR
2b23a 59 44 45 46 45 52 52 45 44 49 53 54 49 4e 43 54  YDEFERREDISTINCT
2b23b 44 52 4f 50 46 41 49 4c 46 52 4f 4d 22 0a 20 20  DROPFAILFROM".  
2b23c 20 20 22 46 55 4c 4c 47 4c 4f 42 59 49 46 49 4e    "FULLGLOBYIFIN
2b23d 54 4f 46 46 53 45 54 49 53 4e 55 4c 4c 4f 52 44  TOFFSETISNULLORD
2b23e 45 52 49 47 48 54 4f 55 54 45 52 4f 4c 4c 42 41  ERIGHTOUTEROLLBA
2b23f 43 4b 52 4f 57 55 4e 49 4f 4e 55 53 49 4e 47 56  CKROWUNIONUSINGV
2b240 41 43 55 55 4d 22 0a 20 20 20 20 22 56 49 45 57  ACUUM".    "VIEW
2b241 49 4e 49 54 49 41 4c 4c 59 22 3b 0a 20 20 73 74  INITIALLY";.  st
2b242 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
2b243 6e 65 64 20 63 68 61 72 20 61 48 61 73 68 5b 31  ned char aHash[1
2b244 32 37 5d 20 3d 20 7b 0a 20 20 20 20 20 20 36 35  27] = {.      65
2b245 2c 20 20 39 34 2c 20 31 31 30 2c 20 20 36 33 2c  ,  94, 110,  63,
2b246 20 20 20 30 2c 20 20 34 34 2c 20 20 20 30 2c 20     0,  44,   0, 
2b247 20 20 30 2c 20 20 37 31 2c 20 20 20 30 2c 20 20    0,  71,   0,  
2b248 36 36 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20  66,   0,   0,.  
2b249 20 20 20 31 30 34 2c 20 20 31 32 2c 20 20 36 37     104,  12,  67
2b24a 2c 20 20 31 35 2c 20 20 20 30 2c 20 31 30 38 2c  ,  15,   0, 108,
2b24b 20 20 37 34 2c 20 31 30 35 2c 20 31 30 31 2c 20    74, 105, 101, 
2b24c 20 20 30 2c 20 20 31 39 2c 20 20 20 30 2c 20 20    0,  19,   0,  
2b24d 20 30 2c 0a 20 20 20 20 20 31 31 34 2c 20 20 20   0,.     114,   
2b24e 30 2c 20 31 31 32 2c 20 20 37 38 2c 20 20 20 30  0, 112,  78,   0
2b24f 2c 20 20 32 32 2c 20 20 38 32 2c 20 20 20 30 2c  ,  22,  82,   0,
2b250 20 20 20 39 2c 20 20 20 30 2c 20 20 20 30 2c 20     9,   0,   0, 
2b251 20 35 39 2c 20 20 36 30 2c 0a 20 20 20 20 20 20   59,  60,.      
2b252 20 30 2c 20 20 35 38 2c 20 20 20 36 2c 20 20 20   0,  58,   6,   
2b253 30 2c 20 20 33 39 2c 20 20 37 39 2c 20 20 39 31  0,  39,  79,  91
2b254 2c 20 20 20 30 2c 20 31 31 31 2c 20 20 39 30 2c  ,   0, 111,  90,
2b255 20 20 20 30 2c 20 20 20 30 2c 20 20 34 35 2c 0a     0,   0,  45,.
2b256 20 20 20 20 20 20 20 30 2c 20 20 39 32 2c 20 20         0,  92,  
2b257 32 34 2c 20 20 20 30 2c 20 20 31 37 2c 20 20 20  24,   0,  17,   
2b258 30 2c 20 31 31 35 2c 20 20 34 30 2c 20 20 32 33  0, 115,  40,  23
2b259 2c 20 20 20 30 2c 20 20 20 35 2c 20 20 39 39 2c  ,   0,   5,  99,
2b25a 20 20 32 35 2c 0a 20 20 20 20 20 20 38 35 2c 20    25,.      85, 
2b25b 20 20 30 2c 20 20 20 30 2c 20 31 31 37 2c 20 20    0,   0, 117,  
2b25c 39 35 2c 20 20 35 30 2c 20 31 31 36 2c 20 20 34  95,  50, 116,  4
2b25d 37 2c 20 20 20 37 2c 20 20 34 32 2c 20 20 20 30  7,   7,  42,   0
2b25e 2c 20 20 38 30 2c 20 20 20 30 2c 0a 20 20 20 20  ,  80,   0,.    
2b25f 20 20 38 39 2c 20 20 32 36 2c 20 20 20 30 2c 20    89,  26,   0, 
2b260 20 38 38 2c 20 20 20 30 2c 20 20 20 30 2c 20 20   88,   0,   0,  
2b261 20 30 2c 20 20 38 34 2c 20 20 38 31 2c 20 20 38   0,  84,  81,  8
2b262 36 2c 20 20 37 37 2c 20 20 39 38 2c 20 20 31 34  6,  77,  98,  14
2b263 2c 0a 20 20 20 20 20 20 33 34 2c 20 20 39 37 2c  ,.      34,  97,
2b264 20 20 20 30 2c 20 20 37 30 2c 20 20 20 30 2c 20     0,  70,   0, 
2b265 20 31 38 2c 20 20 37 36 2c 20 31 30 30 2c 20 20   18,  76, 100,  
2b266 33 31 2c 20 20 20 30 2c 20 31 31 33 2c 20 20 36  31,   0, 113,  6
2b267 39 2c 20 31 30 36 2c 0a 20 20 20 20 20 20 35 32  9, 106,.      52
2b268 2c 20 20 34 36 2c 20 20 37 33 2c 20 20 20 30 2c  ,  46,  73,   0,
2b269 20 20 20 30 2c 20 20 38 33 2c 20 31 30 32 2c 20     0,  83, 102, 
2b26a 20 20 30 2c 20 31 30 39 2c 20 20 20 30 2c 20 20    0, 109,   0,  
2b26b 33 35 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20  35,   0,   0,.  
2b26c 20 20 20 20 32 38 2c 20 20 20 30 2c 20 20 37 35      28,   0,  75
2b26d 2c 20 20 34 38 2c 20 20 35 33 2c 20 20 20 30 2c  ,  48,  53,   0,
2b26e 20 20 32 30 2c 20 20 35 31 2c 20 20 20 30 2c 20    20,  51,   0, 
2b26f 20 34 33 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74   43,.  };.  stat
2b270 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  ic const unsigne
2b271 64 20 63 68 61 72 20 61 4e 65 78 74 5b 31 31 37  d char aNext[117
2b272 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 30 2c 20  ] = {.       0, 
2b273 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
2b274 20 34 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   4,   0,   0,   
2b275 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
2b276 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20 20  ,   0,   0,.    
2b277 20 20 20 30 2c 20 20 20 32 2c 20 20 20 30 2c 20     0,   2,   0, 
2b278 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
2b279 20 30 2c 20 20 20 30 2c 20 20 31 33 2c 20 20 20   0,   0,  13,   
2b27a 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
2b27b 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 20 30 2c  ,.       0,   0,
2b27c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
2b27d 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
2b27e 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
2b27f 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 20 30  0,   0,.       0
2b280 2c 20 20 20 30 2c 20 20 20 33 2c 20 20 33 38 2c  ,   0,   3,  38,
2b281 20 20 20 30 2c 20 20 33 32 2c 20 20 32 31 2c 20     0,  32,  21, 
2b282 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
2b283 20 30 2c 20 20 32 39 2c 20 20 20 30 2c 0a 20 20   0,  29,   0,.  
2b284 20 20 20 20 20 30 2c 20 20 33 37 2c 20 20 20 30       0,  37,   0
2b285 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 31 2c  ,   0,   0,   1,
2b286 20 20 35 35 2c 20 20 20 30 2c 20 20 20 30 2c 20    55,   0,   0, 
2b287 20 35 36 2c 20 20 20 30 2c 20 20 20 30 2c 20 20   56,   0,   0,  
2b288 20 30 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 20   0,.       0,   
2b289 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
2b28a 2c 20 20 20 30 2c 20 20 35 34 2c 20 20 20 30 2c  ,   0,  54,   0,
2b28b 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
2b28c 20 33 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 20   30,   0,.      
2b28d 31 36 2c 20 20 33 33 2c 20 20 31 30 2c 20 20 20  16,  33,  10,   
2b28e 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
2b28f 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
2b290 20 20 31 31 2c 20 20 36 31 2c 20 20 36 38 2c 0a    11,  61,  68,.
2b291 20 20 20 20 20 20 20 30 2c 20 20 20 38 2c 20 20         0,   8,  
2b292 20 30 2c 20 20 39 33 2c 20 20 38 37 2c 20 20 20   0,  93,  87,   
2b293 30 2c 20 20 39 36 2c 20 20 20 30 2c 20 20 34 39  0,  96,   0,  49
2b294 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 36 34 2c  ,   0,   0,  64,
2b295 20 20 20 30 2c 0a 20 20 20 20 20 20 34 31 2c 20     0,.      41, 
2b296 31 30 33 2c 20 20 20 30 2c 20 20 32 37 2c 20 31  103,   0,  27, 1
2b297 30 37 2c 20 20 33 36 2c 20 20 36 32 2c 20 20 37  07,  36,  62,  7
2b298 32 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 35 37  2,   0,   0,  57
2b299 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 7d 3b  ,   0,   0,.  };
2b29a 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
2b29b 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 4c  unsigned char aL
2b29c 65 6e 5b 31 31 37 5d 20 3d 20 7b 0a 20 20 20 20  en[117] = {.    
2b29d 20 20 20 37 2c 20 20 20 37 2c 20 20 20 35 2c 20     7,   7,   5, 
2b29e 20 20 34 2c 20 20 20 36 2c 20 20 20 34 2c 20 20    4,   6,   4,  
2b29f 20 35 2c 20 20 20 33 2c 20 20 20 36 2c 20 20 20   5,   3,   6,   
2b2a0 37 2c 20 20 20 33 2c 20 20 20 36 2c 20 20 20 36  7,   3,   6,   6
2b2a1 2c 0a 20 20 20 20 20 20 20 37 2c 20 20 20 37 2c  ,.       7,   7,
2b2a2 20 20 20 33 2c 20 20 20 38 2c 20 20 20 32 2c 20     3,   8,   2, 
2b2a3 20 20 36 2c 20 20 20 35 2c 20 20 20 34 2c 20 20    6,   5,   4,  
2b2a4 20 34 2c 20 20 20 33 2c 20 20 31 30 2c 20 20 20   4,   3,  10,   
2b2a5 34 2c 20 20 20 36 2c 0a 20 20 20 20 20 20 31 31  4,   6,.      11
2b2a6 2c 20 20 20 32 2c 20 20 20 37 2c 20 20 20 35 2c  ,   2,   7,   5,
2b2a7 20 20 20 35 2c 20 20 20 39 2c 20 20 20 36 2c 20     5,   9,   6, 
2b2a8 20 31 30 2c 20 20 20 39 2c 20 20 20 37 2c 20 20   10,   9,   7,  
2b2a9 31 30 2c 20 20 20 36 2c 20 20 20 35 2c 0a 20 20  10,   6,   5,.  
2b2aa 20 20 20 20 20 36 2c 20 20 20 36 2c 20 20 20 35       6,   6,   5
2b2ab 2c 20 20 20 36 2c 20 20 20 34 2c 20 20 20 39 2c  ,   6,   4,   9,
2b2ac 20 20 20 32 2c 20 20 20 35 2c 20 20 20 35 2c 20     2,   5,   5, 
2b2ad 20 20 36 2c 20 20 20 37 2c 20 20 20 37 2c 20 20    6,   7,   7,  
2b2ae 20 33 2c 0a 20 20 20 20 20 20 20 34 2c 20 20 20   3,.       4,   
2b2af 34 2c 20 20 20 37 2c 20 20 20 33 2c 20 20 20 36  4,   7,   3,   6
2b2b0 2c 20 20 20 34 2c 20 20 20 37 2c 20 20 20 36 2c  ,   4,   7,   6,
2b2b1 20 20 31 32 2c 20 20 20 36 2c 20 20 20 39 2c 20    12,   6,   9, 
2b2b2 20 20 34 2c 20 20 20 36 2c 0a 20 20 20 20 20 20    4,   6,.      
2b2b3 20 35 2c 20 20 20 34 2c 20 20 20 37 2c 20 20 20   5,   4,   7,   
2b2b4 36 2c 20 20 20 35 2c 20 20 20 36 2c 20 20 20 37  6,   5,   6,   7
2b2b5 2c 20 20 20 35 2c 20 20 20 34 2c 20 20 20 35 2c  ,   5,   4,   5,
2b2b6 20 20 20 37 2c 20 20 20 35 2c 20 20 20 38 2c 0a     7,   5,   8,.
2b2b7 20 20 20 20 20 20 20 33 2c 20 20 20 37 2c 20 20         3,   7,  
2b2b8 31 33 2c 20 20 20 32 2c 20 20 20 32 2c 20 20 20  13,   2,   2,   
2b2b9 34 2c 20 20 20 36 2c 20 20 20 36 2c 20 20 20 38  4,   6,   6,   8
2b2ba 2c 20 20 20 35 2c 20 20 31 37 2c 20 20 31 32 2c  ,   5,  17,  12,
2b2bb 20 20 20 37 2c 0a 20 20 20 20 20 20 20 38 2c 20     7,.       8, 
2b2bc 20 20 38 2c 20 20 20 32 2c 20 20 20 34 2c 20 20    8,   2,   4,  
2b2bd 20 34 2c 20 20 20 34 2c 20 20 20 34 2c 20 20 20   4,   4,   4,   
2b2be 34 2c 20 20 20 32 2c 20 20 20 32 2c 20 20 20 34  4,   2,   2,   4
2b2bf 2c 20 20 20 36 2c 20 20 20 32 2c 0a 20 20 20 20  ,   6,   2,.    
2b2c0 20 20 20 33 2c 20 20 20 36 2c 20 20 20 35 2c 20     3,   6,   5, 
2b2c1 20 20 35 2c 20 20 20 35 2c 20 20 20 38 2c 20 20    5,   5,   8,  
2b2c2 20 33 2c 20 20 20 35 2c 20 20 20 35 2c 20 20 20   3,   5,   5,   
2b2c3 36 2c 20 20 20 34 2c 20 20 20 39 2c 20 20 20 33  6,   4,   9,   3
2b2c4 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20  ,.  };.  static 
2b2c5 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 73  const unsigned s
2b2c6 68 6f 72 74 20 69 6e 74 20 61 4f 66 66 73 65 74  hort int aOffset
2b2c7 5b 31 31 37 5d 20 3d 20 7b 0a 20 20 20 20 20 20  [117] = {.      
2b2c8 20 30 2c 20 20 20 32 2c 20 20 20 32 2c 20 20 20   0,   2,   2,   
2b2c9 38 2c 20 20 20 39 2c 20 20 31 34 2c 20 20 31 36  8,   9,  14,  16
2b2ca 2c 20 20 32 30 2c 20 20 32 33 2c 20 20 32 35 2c  ,  20,  23,  25,
2b2cb 20 20 32 35 2c 20 20 32 39 2c 20 20 33 33 2c 0a    25,  29,  33,.
2b2cc 20 20 20 20 20 20 33 36 2c 20 20 34 31 2c 20 20        36,  41,  
2b2cd 34 36 2c 20 20 34 38 2c 20 20 35 33 2c 20 20 35  46,  48,  53,  5
2b2ce 34 2c 20 20 35 39 2c 20 20 36 32 2c 20 20 36 35  4,  59,  62,  65
2b2cf 2c 20 20 36 37 2c 20 20 36 39 2c 20 20 37 38 2c  ,  67,  69,  78,
2b2d0 20 20 38 31 2c 0a 20 20 20 20 20 20 38 36 2c 20    81,.      86, 
2b2d1 20 39 35 2c 20 20 39 36 2c 20 31 30 31 2c 20 31   95,  96, 101, 1
2b2d2 30 35 2c 20 31 30 39 2c 20 31 31 37 2c 20 31 32  05, 109, 117, 12
2b2d3 33 2c 20 31 33 30 2c 20 31 33 38 2c 20 31 34 34  3, 130, 138, 144
2b2d4 2c 20 31 35 34 2c 20 31 35 37 2c 0a 20 20 20 20  , 154, 157,.    
2b2d5 20 31 36 32 2c 20 31 36 37 2c 20 31 37 32 2c 20   162, 167, 172, 
2b2d6 31 37 35 2c 20 31 37 39 2c 20 31 37 39 2c 20 31  175, 179, 179, 1
2b2d7 38 33 2c 20 31 38 38 2c 20 31 39 31 2c 20 31 39  83, 188, 191, 19
2b2d8 35 2c 20 32 30 31 2c 20 32 30 37 2c 20 32 30 37  5, 201, 207, 207
2b2d9 2c 0a 20 20 20 20 20 32 31 30 2c 20 32 31 33 2c  ,.     210, 213,
2b2da 20 32 31 37 2c 20 32 31 38 2c 20 32 32 32 2c 20   217, 218, 222, 
2b2db 32 32 38 2c 20 32 33 32 2c 20 32 33 39 2c 20 32  228, 232, 239, 2
2b2dc 34 35 2c 20 32 35 37 2c 20 32 36 33 2c 20 32 37  45, 257, 263, 27
2b2dd 32 2c 20 32 37 34 2c 0a 20 20 20 20 20 32 38 30  2, 274,.     280
2b2de 2c 20 32 38 35 2c 20 32 38 37 2c 20 32 39 34 2c  , 285, 287, 294,
2b2df 20 32 39 39 2c 20 33 30 34 2c 20 33 31 30 2c 20   299, 304, 310, 
2b2e0 33 31 36 2c 20 33 32 31 2c 20 33 32 35 2c 20 33  316, 321, 325, 3
2b2e1 32 38 2c 20 33 33 35 2c 20 33 33 39 2c 0a 20 20  28, 335, 339,.  
2b2e2 20 20 20 33 34 37 2c 20 33 34 39 2c 20 33 35 36     347, 349, 356
2b2e3 2c 20 33 35 38 2c 20 33 36 30 2c 20 33 36 39 2c  , 358, 360, 369,
2b2e4 20 33 37 33 2c 20 33 37 39 2c 20 33 38 35 2c 20   373, 379, 385, 
2b2e5 33 39 33 2c 20 33 39 38 2c 20 33 39 38 2c 20 34  393, 398, 398, 4
2b2e6 31 34 2c 0a 20 20 20 20 20 34 32 31 2c 20 34 32  14,.     421, 42
2b2e7 38 2c 20 34 32 39 2c 20 34 33 36 2c 20 34 34 30  8, 429, 436, 440
2b2e8 2c 20 34 34 34 2c 20 34 34 38 2c 20 34 35 32 2c  , 444, 448, 452,
2b2e9 20 34 35 35 2c 20 34 35 37 2c 20 34 35 39 2c 20   455, 457, 459, 
2b2ea 34 36 32 2c 20 34 36 32 2c 0a 20 20 20 20 20 34  462, 462,.     4
2b2eb 36 35 2c 20 34 36 38 2c 20 34 37 34 2c 20 34 37  65, 468, 474, 47
2b2ec 38 2c 20 34 38 33 2c 20 34 38 37 2c 20 34 39 35  8, 483, 487, 495
2b2ed 2c 20 34 39 38 2c 20 35 30 33 2c 20 35 30 38 2c  , 498, 503, 508,
2b2ee 20 35 31 34 2c 20 35 31 38 2c 20 35 32 33 2c 0a   514, 518, 523,.
2b2ef 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f    };.  static co
2b2f0 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
2b2f1 72 20 61 43 6f 64 65 5b 31 31 37 5d 20 3d 20 7b  r aCode[117] = {
2b2f2 0a 20 20 20 20 54 4b 5f 52 45 49 4e 44 45 58 2c  .    TK_REINDEX,
2b2f3 20 20 20 20 54 4b 5f 49 4e 44 45 58 45 44 2c 20      TK_INDEXED, 
2b2f4 20 20 20 54 4b 5f 49 4e 44 45 58 2c 20 20 20 20     TK_INDEX,    
2b2f5 20 20 54 4b 5f 44 45 53 43 2c 20 20 20 20 20 20    TK_DESC,      
2b2f6 20 54 4b 5f 45 53 43 41 50 45 2c 20 20 20 20 20   TK_ESCAPE,     
2b2f7 0a 20 20 20 20 54 4b 5f 45 41 43 48 2c 20 20 20  .    TK_EACH,   
2b2f8 20 20 20 20 54 4b 5f 43 48 45 43 4b 2c 20 20 20      TK_CHECK,   
2b2f9 20 20 20 54 4b 5f 4b 45 59 2c 20 20 20 20 20 20     TK_KEY,      
2b2fa 20 20 54 4b 5f 42 45 46 4f 52 45 2c 20 20 20 20    TK_BEFORE,    
2b2fb 20 54 4b 5f 46 4f 52 45 49 47 4e 2c 20 20 20 20   TK_FOREIGN,    
2b2fc 0a 20 20 20 20 54 4b 5f 46 4f 52 2c 20 20 20 20  .    TK_FOR,    
2b2fd 20 20 20 20 54 4b 5f 49 47 4e 4f 52 45 2c 20 20      TK_IGNORE,  
2b2fe 20 20 20 54 4b 5f 4c 49 4b 45 5f 4b 57 2c 20 20     TK_LIKE_KW,  
2b2ff 20 20 54 4b 5f 45 58 50 4c 41 49 4e 2c 20 20 20    TK_EXPLAIN,   
2b300 20 54 4b 5f 49 4e 53 54 45 41 44 2c 20 20 20 20   TK_INSTEAD,    
2b301 0a 20 20 20 20 54 4b 5f 41 44 44 2c 20 20 20 20  .    TK_ADD,    
2b302 20 20 20 20 54 4b 5f 44 41 54 41 42 41 53 45 2c      TK_DATABASE,
2b303 20 20 20 54 4b 5f 41 53 2c 20 20 20 20 20 20 20     TK_AS,       
2b304 20 20 54 4b 5f 53 45 4c 45 43 54 2c 20 20 20 20    TK_SELECT,    
2b305 20 54 4b 5f 54 41 42 4c 45 2c 20 20 20 20 20 20   TK_TABLE,      
2b306 0a 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c  .    TK_JOIN_KW,
2b307 20 20 20 20 54 4b 5f 54 48 45 4e 2c 20 20 20 20      TK_THEN,    
2b308 20 20 20 54 4b 5f 45 4e 44 2c 20 20 20 20 20 20     TK_END,      
2b309 20 20 54 4b 5f 44 45 46 45 52 52 41 42 4c 45 2c    TK_DEFERRABLE,
2b30a 20 54 4b 5f 45 4c 53 45 2c 20 20 20 20 20 20 20   TK_ELSE,       
2b30b 0a 20 20 20 20 54 4b 5f 45 58 43 45 50 54 2c 20  .    TK_EXCEPT, 
2b30c 20 20 20 20 54 4b 5f 54 52 41 4e 53 41 43 54 49      TK_TRANSACTI
2b30d 4f 4e 2c 54 4b 5f 4f 4e 2c 20 20 20 20 20 20 20  ON,TK_ON,       
2b30e 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20    TK_JOIN_KW,   
2b30f 20 54 4b 5f 41 4c 54 45 52 2c 20 20 20 20 20 20   TK_ALTER,      
2b310 0a 20 20 20 20 54 4b 5f 52 41 49 53 45 2c 20 20  .    TK_RAISE,  
2b311 20 20 20 20 54 4b 5f 45 58 43 4c 55 53 49 56 45      TK_EXCLUSIVE
2b312 2c 20 20 54 4b 5f 45 58 49 53 54 53 2c 20 20 20  ,  TK_EXISTS,   
2b313 20 20 54 4b 5f 43 4f 4e 53 54 52 41 49 4e 54 2c    TK_CONSTRAINT,
2b314 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 2c 20 20   TK_INTERSECT,  
2b315 0a 20 20 20 20 54 4b 5f 54 52 49 47 47 45 52 2c  .    TK_TRIGGER,
2b316 20 20 20 20 54 4b 5f 52 45 46 45 52 45 4e 43 45      TK_REFERENCE
2b317 53 2c 20 54 4b 5f 55 4e 49 51 55 45 2c 20 20 20  S, TK_UNIQUE,   
2b318 20 20 54 4b 5f 51 55 45 52 59 2c 20 20 20 20 20    TK_QUERY,     
2b319 20 54 4b 5f 41 54 54 41 43 48 2c 20 20 20 20 20   TK_ATTACH,     
2b31a 0a 20 20 20 20 54 4b 5f 48 41 56 49 4e 47 2c 20  .    TK_HAVING, 
2b31b 20 20 20 20 54 4b 5f 47 52 4f 55 50 2c 20 20 20      TK_GROUP,   
2b31c 20 20 20 54 4b 5f 55 50 44 41 54 45 2c 20 20 20     TK_UPDATE,   
2b31d 20 20 54 4b 5f 54 45 4d 50 2c 20 20 20 20 20 20    TK_TEMP,      
2b31e 20 54 4b 5f 54 45 4d 50 2c 20 20 20 20 20 20 20   TK_TEMP,       
2b31f 0a 20 20 20 20 54 4b 5f 4f 52 2c 20 20 20 20 20  .    TK_OR,     
2b320 20 20 20 20 54 4b 5f 42 45 47 49 4e 2c 20 20 20      TK_BEGIN,   
2b321 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20     TK_JOIN_KW,  
2b322 20 20 54 4b 5f 52 45 4e 41 4d 45 2c 20 20 20 20    TK_RENAME,    
2b323 20 54 4b 5f 42 45 54 57 45 45 4e 2c 20 20 20 20   TK_BETWEEN,    
2b324 0a 20 20 20 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c  .    TK_NOTNULL,
2b325 20 20 20 20 54 4b 5f 4e 4f 54 2c 20 20 20 20 20      TK_NOT,     
2b326 20 20 20 54 4b 5f 4e 55 4c 4c 2c 20 20 20 20 20     TK_NULL,     
2b327 20 20 54 4b 5f 4c 49 4b 45 5f 4b 57 2c 20 20 20    TK_LIKE_KW,   
2b328 20 54 4b 5f 43 41 53 43 41 44 45 2c 20 20 20 20   TK_CASCADE,    
2b329 0a 20 20 20 20 54 4b 5f 41 53 43 2c 20 20 20 20  .    TK_ASC,    
2b32a 20 20 20 20 54 4b 5f 44 45 4c 45 54 45 2c 20 20      TK_DELETE,  
2b32b 20 20 20 54 4b 5f 43 41 53 45 2c 20 20 20 20 20     TK_CASE,     
2b32c 20 20 54 4b 5f 43 4f 4c 4c 41 54 45 2c 20 20 20    TK_COLLATE,   
2b32d 20 54 4b 5f 43 52 45 41 54 45 2c 20 20 20 20 20   TK_CREATE,     
2b32e 0a 20 20 20 20 54 4b 5f 43 54 49 4d 45 5f 4b 57  .    TK_CTIME_KW
2b32f 2c 20 20 20 54 4b 5f 44 45 54 41 43 48 2c 20 20  ,   TK_DETACH,  
2b330 20 20 20 54 4b 5f 49 4d 4d 45 44 49 41 54 45 2c     TK_IMMEDIATE,
2b331 20 20 54 4b 5f 4a 4f 49 4e 2c 20 20 20 20 20 20    TK_JOIN,      
2b332 20 54 4b 5f 49 4e 53 45 52 54 2c 20 20 20 20 20   TK_INSERT,     
2b333 0a 20 20 20 20 54 4b 5f 4d 41 54 43 48 2c 20 20  .    TK_MATCH,  
2b334 20 20 20 20 54 4b 5f 50 4c 41 4e 2c 20 20 20 20      TK_PLAN,    
2b335 20 20 20 54 4b 5f 41 4e 41 4c 59 5a 45 2c 20 20     TK_ANALYZE,  
2b336 20 20 54 4b 5f 50 52 41 47 4d 41 2c 20 20 20 20    TK_PRAGMA,    
2b337 20 54 4b 5f 41 42 4f 52 54 2c 20 20 20 20 20 20   TK_ABORT,      
2b338 0a 20 20 20 20 54 4b 5f 56 41 4c 55 45 53 2c 20  .    TK_VALUES, 
2b339 20 20 20 20 54 4b 5f 56 49 52 54 55 41 4c 2c 20      TK_VIRTUAL, 
2b33a 20 20 20 54 4b 5f 4c 49 4d 49 54 2c 20 20 20 20     TK_LIMIT,    
2b33b 20 20 54 4b 5f 57 48 45 4e 2c 20 20 20 20 20 20    TK_WHEN,      
2b33c 20 54 4b 5f 57 48 45 52 45 2c 20 20 20 20 20 20   TK_WHERE,      
2b33d 0a 20 20 20 20 54 4b 5f 52 45 50 4c 41 43 45 2c  .    TK_REPLACE,
2b33e 20 20 20 20 54 4b 5f 41 46 54 45 52 2c 20 20 20      TK_AFTER,   
2b33f 20 20 20 54 4b 5f 52 45 53 54 52 49 43 54 2c 20     TK_RESTRICT, 
2b340 20 20 54 4b 5f 41 4e 44 2c 20 20 20 20 20 20 20    TK_AND,       
2b341 20 54 4b 5f 44 45 46 41 55 4c 54 2c 20 20 20 20   TK_DEFAULT,    
2b342 0a 20 20 20 20 54 4b 5f 41 55 54 4f 49 4e 43 52  .    TK_AUTOINCR
2b343 2c 20 20 20 54 4b 5f 54 4f 2c 20 20 20 20 20 20  ,   TK_TO,      
2b344 20 20 20 54 4b 5f 49 4e 2c 20 20 20 20 20 20 20     TK_IN,       
2b345 20 20 54 4b 5f 43 41 53 54 2c 20 20 20 20 20 20    TK_CAST,      
2b346 20 54 4b 5f 43 4f 4c 55 4d 4e 4b 57 2c 20 20 20   TK_COLUMNKW,   
2b347 0a 20 20 20 20 54 4b 5f 43 4f 4d 4d 49 54 2c 20  .    TK_COMMIT, 
2b348 20 20 20 20 54 4b 5f 43 4f 4e 46 4c 49 43 54 2c      TK_CONFLICT,
2b349 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20     TK_JOIN_KW,  
2b34a 20 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 2c 20 20    TK_CTIME_KW,  
2b34b 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 2c 20 20 20   TK_CTIME_KW,   
2b34c 0a 20 20 20 20 54 4b 5f 50 52 49 4d 41 52 59 2c  .    TK_PRIMARY,
2b34d 20 20 20 20 54 4b 5f 44 45 46 45 52 52 45 44 2c      TK_DEFERRED,
2b34e 20 20 20 54 4b 5f 44 49 53 54 49 4e 43 54 2c 20     TK_DISTINCT, 
2b34f 20 20 54 4b 5f 49 53 2c 20 20 20 20 20 20 20 20    TK_IS,        
2b350 20 54 4b 5f 44 52 4f 50 2c 20 20 20 20 20 20 20   TK_DROP,       
2b351 0a 20 20 20 20 54 4b 5f 46 41 49 4c 2c 20 20 20  .    TK_FAIL,   
2b352 20 20 20 20 54 4b 5f 46 52 4f 4d 2c 20 20 20 20      TK_FROM,    
2b353 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20     TK_JOIN_KW,  
2b354 20 20 54 4b 5f 4c 49 4b 45 5f 4b 57 2c 20 20 20    TK_LIKE_KW,   
2b355 20 54 4b 5f 42 59 2c 20 20 20 20 20 20 20 20 20   TK_BY,         
2b356 0a 20 20 20 20 54 4b 5f 49 46 2c 20 20 20 20 20  .    TK_IF,     
2b357 20 20 20 20 54 4b 5f 49 4e 54 4f 2c 20 20 20 20      TK_INTO,    
2b358 20 20 20 54 4b 5f 4f 46 46 53 45 54 2c 20 20 20     TK_OFFSET,   
2b359 20 20 54 4b 5f 4f 46 2c 20 20 20 20 20 20 20 20    TK_OF,        
2b35a 20 54 4b 5f 53 45 54 2c 20 20 20 20 20 20 20 20   TK_SET,        
2b35b 0a 20 20 20 20 54 4b 5f 49 53 4e 55 4c 4c 2c 20  .    TK_ISNULL, 
2b35c 20 20 20 20 54 4b 5f 4f 52 44 45 52 2c 20 20 20      TK_ORDER,   
2b35d 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20     TK_JOIN_KW,  
2b35e 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20    TK_JOIN_KW,   
2b35f 20 54 4b 5f 52 4f 4c 4c 42 41 43 4b 2c 20 20 20   TK_ROLLBACK,   
2b360 0a 20 20 20 20 54 4b 5f 52 4f 57 2c 20 20 20 20  .    TK_ROW,    
2b361 20 20 20 20 54 4b 5f 55 4e 49 4f 4e 2c 20 20 20      TK_UNION,   
2b362 20 20 20 54 4b 5f 55 53 49 4e 47 2c 20 20 20 20     TK_USING,    
2b363 20 20 54 4b 5f 56 41 43 55 55 4d 2c 20 20 20 20    TK_VACUUM,    
2b364 20 54 4b 5f 56 49 45 57 2c 20 20 20 20 20 20 20   TK_VIEW,       
2b365 0a 20 20 20 20 54 4b 5f 49 4e 49 54 49 41 4c 4c  .    TK_INITIALL
2b366 59 2c 20 20 54 4b 5f 41 4c 4c 2c 20 20 20 20 20  Y,  TK_ALL,     
2b367 20 20 20 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 68     .  };.  int h
2b368 2c 20 69 3b 0a 20 20 69 66 28 20 6e 3c 32 20 29  , i;.  if( n<2 )
2b369 20 72 65 74 75 72 6e 20 54 4b 5f 49 44 3b 0a 20   return TK_ID;. 
2b36a 20 68 20 3d 20 28 28 63 68 61 72 4d 61 70 28 7a   h = ((charMap(z
2b36b 5b 30 5d 29 2a 34 29 20 5e 0a 20 20 20 20 20 20  [0])*4) ^.      
2b36c 28 63 68 61 72 4d 61 70 28 7a 5b 6e 2d 31 5d 29  (charMap(z[n-1])
2b36d 2a 33 29 20 5e 0a 20 20 20 20 20 20 6e 29 20 25  *3) ^.      n) %
2b36e 20 31 32 37 3b 0a 20 20 66 6f 72 28 69 3d 28 28   127;.  for(i=((
2b36f 69 6e 74 29 61 48 61 73 68 5b 68 5d 29 2d 31 3b  int)aHash[h])-1;
2b370 20 69 3e 3d 30 3b 20 69 3d 28 28 69 6e 74 29 61   i>=0; i=((int)a
2b371 4e 65 78 74 5b 69 5d 29 2d 31 29 7b 0a 20 20 20  Next[i])-1){.   
2b372 20 69 66 28 20 61 4c 65 6e 5b 69 5d 3d 3d 6e 20   if( aLen[i]==n 
2b373 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  && sqlite3StrNIC
2b374 6d 70 28 26 7a 54 65 78 74 5b 61 4f 66 66 73 65  mp(&zText[aOffse
2b375 74 5b 69 5d 5d 2c 7a 2c 6e 29 3d 3d 30 20 29 7b  t[i]],z,n)==0 ){
2b376 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 61 43  .      return aC
2b377 6f 64 65 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20  ode[i];.    }.  
2b378 7d 0a 20 20 72 65 74 75 72 6e 20 54 4b 5f 49 44  }.  return TK_ID
2b379 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
2b37a 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4b 65  TE int sqlite3Ke
2b37b 79 77 6f 72 64 43 6f 64 65 28 63 6f 6e 73 74 20  ywordCode(const 
2b37c 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
2b37d 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75  , int n){.  retu
2b37e 72 6e 20 6b 65 79 77 6f 72 64 43 6f 64 65 28 28  rn keywordCode((
2b37f 63 68 61 72 2a 29 7a 2c 20 6e 29 3b 0a 7d 0a 0a  char*)z, n);.}..
2b380 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2b381 45 6e 64 20 6f 66 20 6b 65 79 77 6f 72 64 68 61  End of keywordha
2b382 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  sh.h ***********
2b383 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b384 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2b385 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2b386 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
2b387 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
2b388 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a  tokenize.c *****
2b389 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2b38a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 58 20 69 73 20  ../*.** If X is 
2b38b 61 20 63 68 61 72 61 63 74 65 72 20 74 68 61 74  a character that
2b38c 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20   can be used in 
2b38d 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20 74 68  an identifier th
2b38e 65 6e 0a 2a 2a 20 49 64 43 68 61 72 28 58 29 20  en.** IdChar(X) 
2b38f 77 69 6c 6c 20 62 65 20 74 72 75 65 2e 20 20 4f  will be true.  O
2b390 74 68 65 72 77 69 73 65 20 69 74 20 69 73 20 66  therwise it is f
2b391 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  alse..**.** For 
2b392 41 53 43 49 49 2c 20 61 6e 79 20 63 68 61 72 61  ASCII, any chara
2b393 63 74 65 72 20 77 69 74 68 20 74 68 65 20 68 69  cter with the hi
2b394 67 68 2d 6f 72 64 65 72 20 62 69 74 20 73 65 74  gh-order bit set
2b395 20 69 73 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 69   is.** allowed i
2b396 6e 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 2e  n an identifier.
2b397 20 20 46 6f 72 20 37 2d 62 69 74 20 63 68 61 72    For 7-bit char
2b398 61 63 74 65 72 73 2c 20 0a 2a 2a 20 73 71 6c 69  acters, .** sqli
2b399 74 65 33 49 73 49 64 43 68 61 72 5b 58 5d 20 6d  te3IsIdChar[X] m
2b39a 75 73 74 20 62 65 20 31 2e 0a 2a 2a 0a 2a 2a 20  ust be 1..**.** 
2b39b 46 6f 72 20 45 42 43 44 49 43 2c 20 74 68 65 20  For EBCDIC, the 
2b39c 72 75 6c 65 73 20 61 72 65 20 6d 6f 72 65 20 63  rules are more c
2b39d 6f 6d 70 6c 65 78 20 62 75 74 20 68 61 76 65 20  omplex but have 
2b39e 74 68 65 20 73 61 6d 65 0a 2a 2a 20 65 6e 64 20  the same.** end 
2b39f 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54 69  result..**.** Ti
2b3a0 63 6b 65 74 20 23 31 30 36 36 2e 20 20 74 68 65  cket #1066.  the
2b3a1 20 53 51 4c 20 73 74 61 6e 64 61 72 64 20 64 6f   SQL standard do
2b3a2 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 27 24 27  es not allow '$'
2b3a3 20 69 6e 20 74 68 65 0a 2a 2a 20 6d 69 64 64 6c   in the.** middl
2b3a4 65 20 6f 66 20 69 64 65 6e 74 66 69 65 72 73 2e  e of identfiers.
2b3a5 20 20 42 75 74 20 6d 61 6e 79 20 53 51 4c 20 69    But many SQL i
2b3a6 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 64  mplementations d
2b3a7 6f 2e 20 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69  o. .** SQLite wi
2b3a8 6c 6c 20 61 6c 6c 6f 77 20 27 24 27 20 69 6e 20  ll allow '$' in 
2b3a9 69 64 65 6e 74 69 66 69 65 72 73 20 66 6f 72 20  identifiers for 
2b3aa 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a  compatibility..*
2b3ab 2a 20 42 75 74 20 74 68 65 20 66 65 61 74 75 72  * But the featur
2b3ac 65 20 69 73 20 75 6e 64 6f 63 75 6d 65 6e 74 65  e is undocumente
2b3ad 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  d..*/.#ifdef SQL
2b3ae 49 54 45 5f 41 53 43 49 49 0a 53 51 4c 49 54 45  ITE_ASCII.SQLITE
2b3af 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
2b3b0 68 61 72 20 73 71 6c 69 74 65 33 49 73 41 73 63  har sqlite3IsAsc
2b3b1 69 69 49 64 43 68 61 72 5b 5d 20 3d 20 7b 0a 2f  iiIdChar[] = {./
2b3b2 2a 20 78 30 20 78 31 20 78 32 20 78 33 20 78 34  * x0 x1 x2 x3 x4
2b3b3 20 78 35 20 78 36 20 78 37 20 78 38 20 78 39 20   x5 x6 x7 x8 x9 
2b3b4 78 41 20 78 42 20 78 43 20 78 44 20 78 45 20 78  xA xB xC xD xE x
2b3b5 46 20 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20 30  F */.    0, 0, 0
2b3b6 2c 20 30 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 1, 0, 0, 0,
2b3b7 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2b3b8 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 32 78 20  0, 0, 0,  /* 2x 
2b3b9 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20  */.    1, 1, 1, 
2b3ba 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2b3bb 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 0, 0, 0, 0,
2b3bc 20 30 2c 20 30 2c 20 20 2f 2a 20 33 78 20 2a 2f   0, 0,  /* 3x */
2b3bd 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c  .    0, 1, 1, 1,
2b3be 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2b3bf 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2b3c0 2c 20 31 2c 20 20 2f 2a 20 34 78 20 2a 2f 0a 20  , 1,  /* 4x */. 
2b3c1 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31     1, 1, 1, 1, 1
2b3c2 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2b3c3 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 0, 0, 0, 0, 
2b3c4 31 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20  1,  /* 5x */.   
2b3c5 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 1, 1, 1, 1, 
2b3c6 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2b3c7 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2b3c8 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 20 31    /* 6x */.    1
2b3c9 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2b3ca 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2b3cb 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20  0, 0, 0, 0, 0,  
2b3cc 2f 2a 20 37 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66  /* 7x */.};.#def
2b3cd 69 6e 65 20 49 64 43 68 61 72 28 43 29 20 20 28  ine IdChar(C)  (
2b3ce 28 28 63 3d 43 29 26 30 78 38 30 29 21 3d 30 20  ((c=C)&0x80)!=0 
2b3cf 7c 7c 20 28 63 3e 30 78 31 66 20 26 26 20 73 71  || (c>0x1f && sq
2b3d0 6c 69 74 65 33 49 73 41 73 63 69 69 49 64 43 68  lite3IsAsciiIdCh
2b3d1 61 72 5b 63 2d 30 78 32 30 5d 29 29 0a 23 65 6e  ar[c-0x20])).#en
2b3d2 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
2b3d3 45 5f 45 42 43 44 49 43 0a 53 51 4c 49 54 45 5f  E_EBCDIC.SQLITE_
2b3d4 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
2b3d5 61 72 20 73 71 6c 69 74 65 33 49 73 45 62 63 64  ar sqlite3IsEbcd
2b3d6 69 63 49 64 43 68 61 72 5b 5d 20 3d 20 7b 0a 2f  icIdChar[] = {./
2b3d7 2a 20 78 30 20 78 31 20 78 32 20 78 33 20 78 34  * x0 x1 x2 x3 x4
2b3d8 20 78 35 20 78 36 20 78 37 20 78 38 20 78 39 20   x5 x6 x7 x8 x9 
2b3d9 78 41 20 78 42 20 78 43 20 78 44 20 78 45 20 78  xA xB xC xD xE x
2b3da 46 20 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20 31  F */.    0, 0, 1
2b3db 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2b3dc 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 1, 0, 0, 0, 
2b3dd 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 34 78 20  0, 0, 0,  /* 4x 
2b3de 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20  */.    0, 1, 1, 
2b3df 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2b3e0 2c 20 31 2c 20 30 2c 20 31 2c 20 30 2c 20 30 2c  , 1, 0, 1, 0, 0,
2b3e1 20 30 2c 20 30 2c 20 20 2f 2a 20 35 78 20 2a 2f   0, 0,  /* 5x */
2b3e2 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 31 2c  .    0, 0, 1, 1,
2b3e3 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2b3e4 31 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 30  1, 0, 0, 0, 1, 0
2b3e5 2c 20 30 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20  , 0,  /* 6x */. 
2b3e6 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31     0, 1, 1, 1, 1
2b3e7 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
2b3e8 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2b3e9 30 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a 20 20 20  0,  /* 7x */.   
2b3ea 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 1, 1, 1, 1, 
2b3eb 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 1, 1, 1, 1, 0
2b3ec 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 0, 1, 1, 1, 0,
2b3ed 20 20 2f 2a 20 38 78 20 2a 2f 0a 20 20 20 20 30    /* 8x */.    0
2b3ee 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2b3ef 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
2b3f0 30 2c 20 31 2c 20 30 2c 20 31 2c 20 30 2c 20 20  0, 1, 0, 1, 0,  
2b3f1 2f 2a 20 39 78 20 2a 2f 0a 20 20 20 20 31 2c 20  /* 9x */.    1, 
2b3f2 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
2b3f3 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
2b3f4 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 20 2f 2a   1, 1, 1, 0,  /*
2b3f5 20 41 78 20 2a 2f 0a 20 20 20 20 30 2c 20 30 2c   Ax */.    0, 0,
2b3f6 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2b3f7 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2b3f8 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 42  , 0, 0, 0,  /* B
2b3f9 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31  x */.    0, 1, 1
2b3fa 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2b3fb 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20   1, 1, 0, 1, 1, 
2b3fc 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 43 78 20  1, 1, 1,  /* Cx 
2b3fd 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20  */.    0, 1, 1, 
2b3fe 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2b3ff 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 0, 1, 1, 1,
2b400 20 31 2c 20 31 2c 20 20 2f 2a 20 44 78 20 2a 2f   1, 1,  /* Dx */
2b401 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 31 2c  .    0, 0, 1, 1,
2b402 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2b403 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 0, 1, 1, 1, 1
2b404 2c 20 31 2c 20 20 2f 2a 20 45 78 20 2a 2f 0a 20  , 1,  /* Ex */. 
2b405 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31     1, 1, 1, 1, 1
2b406 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2b407 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 1, 1, 1, 1, 
2b408 30 2c 20 20 2f 2a 20 46 78 20 2a 2f 0a 7d 3b 0a  0,  /* Fx */.};.
2b409 23 64 65 66 69 6e 65 20 49 64 43 68 61 72 28 43  #define IdChar(C
2b40a 29 20 20 28 28 28 63 3d 43 29 3e 3d 30 78 34 32  )  (((c=C)>=0x42
2b40b 20 26 26 20 73 71 6c 69 74 65 33 49 73 45 62 63   && sqlite3IsEbc
2b40c 64 69 63 49 64 43 68 61 72 5b 63 2d 30 78 34 30  dicIdChar[c-0x40
2b40d 5d 29 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  ])).#endif.../*.
2b40e 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6c 65  ** Return the le
2b40f 6e 67 74 68 20 6f 66 20 74 68 65 20 74 6f 6b 65  ngth of the toke
2b410 6e 20 74 68 61 74 20 62 65 67 69 6e 73 20 61 74  n that begins at
2b411 20 7a 5b 30 5d 2e 20 0a 2a 2a 20 53 74 6f 72 65   z[0]. .** Store
2b412 20 74 68 65 20 74 6f 6b 65 6e 20 74 79 70 65 20   the token type 
2b413 69 6e 20 2a 74 6f 6b 65 6e 54 79 70 65 20 62 65  in *tokenType be
2b414 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a  fore returning..
2b415 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2b416 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74  E int sqlite3Get
2b417 54 6f 6b 65 6e 28 63 6f 6e 73 74 20 75 6e 73 69  Token(const unsi
2b418 67 6e 65 64 20 63 68 61 72 20 2a 7a 2c 20 69 6e  gned char *z, in
2b419 74 20 2a 74 6f 6b 65 6e 54 79 70 65 29 7b 0a 20  t *tokenType){. 
2b41a 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 73 77 69   int i, c;.  swi
2b41b 74 63 68 28 20 2a 7a 20 29 7b 0a 20 20 20 20 63  tch( *z ){.    c
2b41c 61 73 65 20 27 20 27 3a 20 63 61 73 65 20 27 5c  ase ' ': case '\
2b41d 74 27 3a 20 63 61 73 65 20 27 5c 6e 27 3a 20 63  t': case '\n': c
2b41e 61 73 65 20 27 5c 66 27 3a 20 63 61 73 65 20 27  ase '\f': case '
2b41f 5c 72 27 3a 20 7b 0a 20 20 20 20 20 20 66 6f 72  \r': {.      for
2b420 28 69 3d 31 3b 20 69 73 73 70 61 63 65 28 7a 5b  (i=1; isspace(z[
2b421 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20  i]); i++){}.    
2b422 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
2b423 4b 5f 53 50 41 43 45 3b 0a 20 20 20 20 20 20 72  K_SPACE;.      r
2b424 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20  eturn i;.    }. 
2b425 20 20 20 63 61 73 65 20 27 2d 27 3a 20 7b 0a 20     case '-': {. 
2b426 20 20 20 20 20 69 66 28 20 7a 5b 31 5d 3d 3d 27       if( z[1]=='
2b427 2d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f  -' ){.        fo
2b428 72 28 69 3d 32 3b 20 28 63 3d 7a 5b 69 5d 29 21  r(i=2; (c=z[i])!
2b429 3d 30 20 26 26 20 63 21 3d 27 5c 6e 27 3b 20 69  =0 && c!='\n'; i
2b42a 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 20 20 2a 74  ++){}.        *t
2b42b 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53 50  okenType = TK_SP
2b42c 41 43 45 3b 0a 20 20 20 20 20 20 20 20 72 65 74  ACE;.        ret
2b42d 75 72 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20  urn i;.      }. 
2b42e 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
2b42f 3d 20 54 4b 5f 4d 49 4e 55 53 3b 0a 20 20 20 20  = TK_MINUS;.    
2b430 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
2b431 7d 0a 20 20 20 20 63 61 73 65 20 27 28 27 3a 20  }.    case '(': 
2b432 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  {.      *tokenTy
2b433 70 65 20 3d 20 54 4b 5f 4c 50 3b 0a 20 20 20 20  pe = TK_LP;.    
2b434 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
2b435 7d 0a 20 20 20 20 63 61 73 65 20 27 29 27 3a 20  }.    case ')': 
2b436 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  {.      *tokenTy
2b437 70 65 20 3d 20 54 4b 5f 52 50 3b 0a 20 20 20 20  pe = TK_RP;.    
2b438 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
2b439 7d 0a 20 20 20 20 63 61 73 65 20 27 3b 27 3a 20  }.    case ';': 
2b43a 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  {.      *tokenTy
2b43b 70 65 20 3d 20 54 4b 5f 53 45 4d 49 3b 0a 20 20  pe = TK_SEMI;.  
2b43c 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2b43d 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2b 27    }.    case '+'
2b43e 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e  : {.      *token
2b43f 54 79 70 65 20 3d 20 54 4b 5f 50 4c 55 53 3b 0a  Type = TK_PLUS;.
2b440 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
2b441 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
2b442 2a 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b  *': {.      *tok
2b443 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53 54 41 52  enType = TK_STAR
2b444 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  ;.      return 1
2b445 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
2b446 20 27 2f 27 3a 20 7b 0a 20 20 20 20 20 20 69 66   '/': {.      if
2b447 28 20 7a 5b 31 5d 21 3d 27 2a 27 20 7c 7c 20 7a  ( z[1]!='*' || z
2b448 5b 32 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  [2]==0 ){.      
2b449 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
2b44a 4b 5f 53 4c 41 53 48 3b 0a 20 20 20 20 20 20 20  K_SLASH;.       
2b44b 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
2b44c 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 33   }.      for(i=3
2b44d 2c 20 63 3d 7a 5b 32 5d 3b 20 28 63 21 3d 27 2a  , c=z[2]; (c!='*
2b44e 27 20 7c 7c 20 7a 5b 69 5d 21 3d 27 2f 27 29 20  ' || z[i]!='/') 
2b44f 26 26 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20  && (c=z[i])!=0; 
2b450 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 69 66 28  i++){}.      if(
2b451 20 63 20 29 20 69 2b 2b 3b 0a 20 20 20 20 20 20   c ) i++;.      
2b452 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
2b453 53 50 41 43 45 3b 0a 20 20 20 20 20 20 72 65 74  SPACE;.      ret
2b454 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20  urn i;.    }.   
2b455 20 63 61 73 65 20 27 25 27 3a 20 7b 0a 20 20 20   case '%': {.   
2b456 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
2b457 54 4b 5f 52 45 4d 3b 0a 20 20 20 20 20 20 72 65  TK_REM;.      re
2b458 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
2b459 20 20 63 61 73 65 20 27 3d 27 3a 20 7b 0a 20 20    case '=': {.  
2b45a 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
2b45b 20 54 4b 5f 45 51 3b 0a 20 20 20 20 20 20 72 65   TK_EQ;.      re
2b45c 74 75 72 6e 20 31 20 2b 20 28 7a 5b 31 5d 3d 3d  turn 1 + (z[1]==
2b45d 27 3d 27 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  '=');.    }.    
2b45e 63 61 73 65 20 27 3c 27 3a 20 7b 0a 20 20 20 20  case '<': {.    
2b45f 20 20 69 66 28 20 28 63 3d 7a 5b 31 5d 29 3d 3d    if( (c=z[1])==
2b460 27 3d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2a  '=' ){.        *
2b461 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4c  tokenType = TK_L
2b462 45 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  E;.        retur
2b463 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  n 2;.      }else
2b464 20 69 66 28 20 63 3d 3d 27 3e 27 20 29 7b 0a 20   if( c=='>' ){. 
2b465 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70         *tokenTyp
2b466 65 20 3d 20 54 4b 5f 4e 45 3b 0a 20 20 20 20 20  e = TK_NE;.     
2b467 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20     return 2;.   
2b468 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d     }else if( c==
2b469 27 3c 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2a  '<' ){.        *
2b46a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4c  tokenType = TK_L
2b46b 53 48 49 46 54 3b 0a 20 20 20 20 20 20 20 20 72  SHIFT;.        r
2b46c 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d  eturn 2;.      }
2b46d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2a 74  else{.        *t
2b46e 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4c 54  okenType = TK_LT
2b46f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
2b470 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
2b471 7d 0a 20 20 20 20 63 61 73 65 20 27 3e 27 3a 20  }.    case '>': 
2b472 7b 0a 20 20 20 20 20 20 69 66 28 20 28 63 3d 7a  {.      if( (c=z
2b473 5b 31 5d 29 3d 3d 27 3d 27 20 29 7b 0a 20 20 20  [1])=='=' ){.   
2b474 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
2b475 3d 20 54 4b 5f 47 45 3b 0a 20 20 20 20 20 20 20  = TK_GE;.       
2b476 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20   return 2;.     
2b477 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 3e   }else if( c=='>
2b478 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f  ' ){.        *to
2b479 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 52 53 48  kenType = TK_RSH
2b47a 49 46 54 3b 0a 20 20 20 20 20 20 20 20 72 65 74  IFT;.        ret
2b47b 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c  urn 2;.      }el
2b47c 73 65 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b  se{.        *tok
2b47d 65 6e 54 79 70 65 20 3d 20 54 4b 5f 47 54 3b 0a  enType = TK_GT;.
2b47e 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31          return 1
2b47f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2b480 20 20 20 20 63 61 73 65 20 27 21 27 3a 20 7b 0a      case '!': {.
2b481 20 20 20 20 20 20 69 66 28 20 7a 5b 31 5d 21 3d        if( z[1]!=
2b482 27 3d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2a  '=' ){.        *
2b483 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49  tokenType = TK_I
2b484 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 20 20  LLEGAL;.        
2b485 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20  return 2;.      
2b486 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2a  }else{.        *
2b487 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4e  tokenType = TK_N
2b488 45 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  E;.        retur
2b489 6e 20 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 2;.      }.   
2b48a 20 7d 0a 20 20 20 20 63 61 73 65 20 27 7c 27 3a   }.    case '|':
2b48b 20 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 31   {.      if( z[1
2b48c 5d 21 3d 27 7c 27 20 29 7b 0a 20 20 20 20 20 20  ]!='|' ){.      
2b48d 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
2b48e 4b 5f 42 49 54 4f 52 3b 0a 20 20 20 20 20 20 20  K_BITOR;.       
2b48f 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
2b490 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2b491 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
2b492 43 4f 4e 43 41 54 3b 0a 20 20 20 20 20 20 20 20  CONCAT;.        
2b493 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20  return 2;.      
2b494 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  }.    }.    case
2b495 20 27 2c 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74   ',': {.      *t
2b496 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 43 4f  okenType = TK_CO
2b497 4d 4d 41 3b 0a 20 20 20 20 20 20 72 65 74 75 72  MMA;.      retur
2b498 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 1;.    }.    c
2b499 61 73 65 20 27 26 27 3a 20 7b 0a 20 20 20 20 20  ase '&': {.     
2b49a 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
2b49b 5f 42 49 54 41 4e 44 3b 0a 20 20 20 20 20 20 72  _BITAND;.      r
2b49c 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
2b49d 20 20 20 63 61 73 65 20 27 7e 27 3a 20 7b 0a 20     case '~': {. 
2b49e 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
2b49f 3d 20 54 4b 5f 42 49 54 4e 4f 54 3b 0a 20 20 20  = TK_BITNOT;.   
2b4a0 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
2b4a1 20 7d 0a 20 20 20 20 63 61 73 65 20 27 60 27 3a   }.    case '`':
2b4a2 0a 20 20 20 20 63 61 73 65 20 27 5c 27 27 3a 0a  .    case '\'':.
2b4a3 20 20 20 20 63 61 73 65 20 27 22 27 3a 20 7b 0a      case '"': {.
2b4a4 20 20 20 20 20 20 69 6e 74 20 64 65 6c 69 6d 20        int delim 
2b4a5 3d 20 7a 5b 30 5d 3b 0a 20 20 20 20 20 20 66 6f  = z[0];.      fo
2b4a6 72 28 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d 29 21  r(i=1; (c=z[i])!
2b4a7 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  =0; i++){.      
2b4a8 20 20 69 66 28 20 63 3d 3d 64 65 6c 69 6d 20 29    if( c==delim )
2b4a9 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
2b4aa 7a 5b 69 2b 31 5d 3d 3d 64 65 6c 69 6d 20 29 7b  z[i+1]==delim ){
2b4ab 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b  .            i++
2b4ac 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
2b4ad 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62  e{.            b
2b4ae 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
2b4af 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
2b4b0 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 3d    }.      if( c=
2b4b1 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20 20 20 20  ='\'' ){.       
2b4b2 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
2b4b3 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 20 20 20  _STRING;.       
2b4b4 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20   return i+1;.   
2b4b5 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 21 3d     }else if( c!=
2b4b6 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f  0 ){.        *to
2b4b7 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 44 3b  kenType = TK_ID;
2b4b8 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
2b4b9 69 2b 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  i+1;.      }else
2b4ba 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
2b4bb 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41  Type = TK_ILLEGA
2b4bc 4c 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  L;.        retur
2b4bd 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n i;.      }.   
2b4be 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2e 27 3a   }.    case '.':
2b4bf 20 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   {.#ifndef SQLIT
2b4c0 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
2b4c1 50 4f 49 4e 54 0a 20 20 20 20 20 20 69 66 28 20  POINT.      if( 
2b4c2 21 69 73 64 69 67 69 74 28 7a 5b 31 5d 29 20 29  !isdigit(z[1]) )
2b4c3 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a  .#endif.      {.
2b4c4 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
2b4c5 70 65 20 3d 20 54 4b 5f 44 4f 54 3b 0a 20 20 20  pe = TK_DOT;.   
2b4c6 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
2b4c7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20       }.      /* 
2b4c8 49 66 20 74 68 65 20 6e 65 78 74 20 63 68 61 72  If the next char
2b4c9 61 63 74 65 72 20 69 73 20 61 20 64 69 67 69 74  acter is a digit
2b4ca 2c 20 74 68 69 73 20 69 73 20 61 20 66 6c 6f 61  , this is a floa
2b4cb 74 69 6e 67 20 70 6f 69 6e 74 0a 20 20 20 20 20  ting point.     
2b4cc 20 2a 2a 20 6e 75 6d 62 65 72 20 74 68 61 74 20   ** number that 
2b4cd 62 65 67 69 6e 73 20 77 69 74 68 20 22 2e 22 2e  begins with ".".
2b4ce 20 20 46 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f    Fall thru into
2b4cf 20 74 68 65 20 6e 65 78 74 20 63 61 73 65 20 2a   the next case *
2b4d0 2f 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  /.    }.    case
2b4d1 20 27 30 27 3a 20 63 61 73 65 20 27 31 27 3a 20   '0': case '1': 
2b4d2 63 61 73 65 20 27 32 27 3a 20 63 61 73 65 20 27  case '2': case '
2b4d3 33 27 3a 20 63 61 73 65 20 27 34 27 3a 0a 20 20  3': case '4':.  
2b4d4 20 20 63 61 73 65 20 27 35 27 3a 20 63 61 73 65    case '5': case
2b4d5 20 27 36 27 3a 20 63 61 73 65 20 27 37 27 3a 20   '6': case '7': 
2b4d6 63 61 73 65 20 27 38 27 3a 20 63 61 73 65 20 27  case '8': case '
2b4d7 39 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b  9': {.      *tok
2b4d8 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4e 54 45  enType = TK_INTE
2b4d9 47 45 52 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  GER;.      for(i
2b4da 3d 30 3b 20 69 73 64 69 67 69 74 28 7a 5b 69 5d  =0; isdigit(z[i]
2b4db 29 3b 20 69 2b 2b 29 7b 7d 0a 23 69 66 6e 64 65  ); i++){}.#ifnde
2b4dc 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
2b4dd 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20  OATING_POINT.   
2b4de 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 27 2e 27     if( z[i]=='.'
2b4df 20 29 7b 0a 20 20 20 20 20 20 20 20 69 2b 2b 3b   ){.        i++;
2b4e0 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
2b4e1 69 73 64 69 67 69 74 28 7a 5b 69 5d 29 20 29 7b  isdigit(z[i]) ){
2b4e2 20 69 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20   i++; }.        
2b4e3 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
2b4e4 46 4c 4f 41 54 3b 0a 20 20 20 20 20 20 7d 0a 20  FLOAT;.      }. 
2b4e5 20 20 20 20 20 69 66 28 20 28 7a 5b 69 5d 3d 3d       if( (z[i]==
2b4e6 27 65 27 20 7c 7c 20 7a 5b 69 5d 3d 3d 27 45 27  'e' || z[i]=='E'
2b4e7 29 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20  ) &&.           
2b4e8 28 20 69 73 64 69 67 69 74 28 7a 5b 69 2b 31 5d  ( isdigit(z[i+1]
2b4e9 29 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c  ) .            |
2b4ea 7c 20 28 28 7a 5b 69 2b 31 5d 3d 3d 27 2b 27 20  | ((z[i+1]=='+' 
2b4eb 7c 7c 20 7a 5b 69 2b 31 5d 3d 3d 27 2d 27 29 20  || z[i+1]=='-') 
2b4ec 26 26 20 69 73 64 69 67 69 74 28 7a 5b 69 2b 32  && isdigit(z[i+2
2b4ed 5d 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 29  ])).           )
2b4ee 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20  .      ){.      
2b4ef 20 20 69 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20    i += 2;.      
2b4f0 20 20 77 68 69 6c 65 28 20 69 73 64 69 67 69 74    while( isdigit
2b4f1 28 7a 5b 69 5d 29 20 29 7b 20 69 2b 2b 3b 20 7d  (z[i]) ){ i++; }
2b4f2 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
2b4f3 79 70 65 20 3d 20 54 4b 5f 46 4c 4f 41 54 3b 0a  ype = TK_FLOAT;.
2b4f4 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
2b4f5 20 20 20 20 20 77 68 69 6c 65 28 20 49 64 43 68       while( IdCh
2b4f6 61 72 28 7a 5b 69 5d 29 20 29 7b 0a 20 20 20 20  ar(z[i]) ){.    
2b4f7 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
2b4f8 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20   TK_ILLEGAL;.   
2b4f9 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20       i++;.      
2b4fa 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  }.      return i
2b4fb 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
2b4fc 20 27 5b 27 3a 20 7b 0a 20 20 20 20 20 20 66 6f   '[': {.      fo
2b4fd 72 28 69 3d 31 2c 20 63 3d 7a 5b 30 5d 3b 20 63  r(i=1, c=z[0]; c
2b4fe 21 3d 27 5d 27 20 26 26 20 28 63 3d 7a 5b 69 5d  !=']' && (c=z[i]
2b4ff 29 21 3d 30 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20  )!=0; i++){}.   
2b500 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
2b501 63 3d 3d 27 5d 27 20 3f 20 54 4b 5f 49 44 20 3a  c==']' ? TK_ID :
2b502 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20   TK_ILLEGAL;.   
2b503 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20     return i;.   
2b504 20 7d 0a 20 20 20 20 63 61 73 65 20 27 3f 27 3a   }.    case '?':
2b505 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54   {.      *tokenT
2b506 79 70 65 20 3d 20 54 4b 5f 56 41 52 49 41 42 4c  ype = TK_VARIABL
2b507 45 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31  E;.      for(i=1
2b508 3b 20 69 73 64 69 67 69 74 28 7a 5b 69 5d 29 3b  ; isdigit(z[i]);
2b509 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 72 65   i++){}.      re
2b50a 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20  turn i;.    }.  
2b50b 20 20 63 61 73 65 20 27 23 27 3a 20 7b 0a 20 20    case '#': {.  
2b50c 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 73 64      for(i=1; isd
2b50d 69 67 69 74 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29  igit(z[i]); i++)
2b50e 7b 7d 0a 20 20 20 20 20 20 69 66 28 20 69 3e 31  {}.      if( i>1
2b50f 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 50   ){.        /* P
2b510 61 72 61 6d 65 74 65 72 73 20 6f 66 20 74 68 65  arameters of the
2b511 20 66 6f 72 6d 20 23 4e 4e 4e 20 28 77 68 65 72   form #NNN (wher
2b512 65 20 4e 4e 4e 20 69 73 20 61 20 6e 75 6d 62 65  e NNN is a numbe
2b513 72 29 20 61 72 65 20 75 73 65 64 0a 20 20 20 20  r) are used.    
2b514 20 20 20 20 2a 2a 20 69 6e 74 65 72 6e 61 6c 6c      ** internall
2b515 79 20 62 79 20 73 71 6c 69 74 65 33 4e 65 73 74  y by sqlite3Nest
2b516 65 64 50 61 72 73 65 2e 20 20 2a 2f 0a 20 20 20  edParse.  */.   
2b517 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
2b518 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20  = TK_REGISTER;. 
2b519 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b         return i;
2b51a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f  .      }.      /
2b51b 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 69  * Fall through i
2b51c 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73  nto the next cas
2b51d 65 20 69 66 20 74 68 65 20 27 23 27 20 69 73 20  e if the '#' is 
2b51e 6e 6f 74 20 66 6f 6c 6c 6f 77 65 64 20 62 79 0a  not followed by.
2b51f 20 20 20 20 20 20 2a 2a 20 61 20 64 69 67 69 74        ** a digit
2b520 2e 20 54 72 79 20 74 6f 20 6d 61 74 63 68 20 23  . Try to match #
2b521 41 41 41 41 20 77 68 65 72 65 20 41 41 41 41 20  AAAA where AAAA 
2b522 69 73 20 61 20 70 61 72 61 6d 65 74 65 72 20 6e  is a parameter n
2b523 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 7d 0a 23 69  ame. */.    }.#i
2b524 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2b525 54 5f 54 43 4c 5f 56 41 52 49 41 42 4c 45 0a 20  T_TCL_VARIABLE. 
2b526 20 20 20 63 61 73 65 20 27 24 27 3a 0a 23 65 6e     case '$':.#en
2b527 64 69 66 0a 20 20 20 20 63 61 73 65 20 27 40 27  dif.    case '@'
2b528 3a 20 20 2f 2a 20 46 6f 72 20 63 6f 6d 70 61 74  :  /* For compat
2b529 69 62 69 6c 69 74 79 20 77 69 74 68 20 4d 53 20  ibility with MS 
2b52a 53 51 4c 20 53 65 72 76 65 72 20 2a 2f 0a 20 20  SQL Server */.  
2b52b 20 20 63 61 73 65 20 27 3a 27 3a 20 7b 0a 20 20    case ':': {.  
2b52c 20 20 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20      int n = 0;. 
2b52d 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
2b52e 3d 20 54 4b 5f 56 41 52 49 41 42 4c 45 3b 0a 20  = TK_VARIABLE;. 
2b52f 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 28 63       for(i=1; (c
2b530 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b  =z[i])!=0; i++){
2b531 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 64 43  .        if( IdC
2b532 68 61 72 28 63 29 20 29 7b 0a 20 20 20 20 20 20  har(c) ){.      
2b533 20 20 20 20 6e 2b 2b 3b 0a 23 69 66 6e 64 65 66      n++;.#ifndef
2b534 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 43 4c   SQLITE_OMIT_TCL
2b535 5f 56 41 52 49 41 42 4c 45 0a 20 20 20 20 20 20  _VARIABLE.      
2b536 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27    }else if( c=='
2b537 28 27 20 26 26 20 6e 3e 30 20 29 7b 0a 20 20 20  (' && n>0 ){.   
2b538 20 20 20 20 20 20 20 64 6f 7b 0a 20 20 20 20 20         do{.     
2b539 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20         i++;.    
2b53a 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 28 63        }while( (c
2b53b 3d 7a 5b 69 5d 29 21 3d 30 20 26 26 20 21 69 73  =z[i])!=0 && !is
2b53c 73 70 61 63 65 28 63 29 20 26 26 20 63 21 3d 27  space(c) && c!='
2b53d 29 27 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  )' );.          
2b53e 69 66 28 20 63 3d 3d 27 29 27 20 29 7b 0a 20 20  if( c==')' ){.  
2b53f 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20            i++;. 
2b540 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
2b541 20 20 20 20 20 20 20 20 20 20 20 20 2a 74 6f 6b              *tok
2b542 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45  enType = TK_ILLE
2b543 47 41 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  GAL;.          }
2b544 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
2b545 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20  ;.        }else 
2b546 69 66 28 20 63 3d 3d 27 3a 27 20 26 26 20 7a 5b  if( c==':' && z[
2b547 69 2b 31 5d 3d 3d 27 3a 27 20 29 7b 0a 20 20 20  i+1]==':' ){.   
2b548 20 20 20 20 20 20 20 69 2b 2b 3b 0a 23 65 6e 64         i++;.#end
2b549 69 66 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  if.        }else
2b54a 7b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  {.          brea
2b54b 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
2b54c 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6e     }.      if( n
2b54d 3d 3d 30 20 29 20 2a 74 6f 6b 65 6e 54 79 70 65  ==0 ) *tokenType
2b54e 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20   = TK_ILLEGAL;. 
2b54f 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20       return i;. 
2b550 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
2b551 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49  ITE_OMIT_BLOB_LI
2b552 54 45 52 41 4c 0a 20 20 20 20 63 61 73 65 20 27  TERAL.    case '
2b553 78 27 3a 20 63 61 73 65 20 27 58 27 3a 20 7b 0a  x': case 'X': {.
2b554 20 20 20 20 20 20 69 66 28 20 7a 5b 31 5d 3d 3d        if( z[1]==
2b555 27 5c 27 27 20 29 7b 0a 20 20 20 20 20 20 20 20  '\'' ){.        
2b556 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
2b557 42 4c 4f 42 3b 0a 20 20 20 20 20 20 20 20 66 6f  BLOB;.        fo
2b558 72 28 69 3d 32 3b 20 28 63 3d 7a 5b 69 5d 29 21  r(i=2; (c=z[i])!
2b559 3d 30 20 26 26 20 63 21 3d 27 5c 27 27 3b 20 69  =0 && c!='\''; i
2b55a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
2b55b 66 28 20 21 69 73 78 64 69 67 69 74 28 63 29 20  f( !isxdigit(c) 
2b55c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  ){.            *
2b55d 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49  tokenType = TK_I
2b55e 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 20 20  LLEGAL;.        
2b55f 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
2b560 20 20 20 20 20 20 69 66 28 20 69 25 32 20 7c 7c        if( i%2 ||
2b561 20 21 63 20 29 20 2a 74 6f 6b 65 6e 54 79 70 65   !c ) *tokenType
2b562 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20   = TK_ILLEGAL;. 
2b563 20 20 20 20 20 20 20 69 66 28 20 63 20 29 20 69         if( c ) i
2b564 2b 2b 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  ++;.        retu
2b565 72 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn i;.      }.  
2b566 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65      /* Otherwise
2b567 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f   fall through to
2b568 20 74 68 65 20 6e 65 78 74 20 63 61 73 65 20 2a   the next case *
2b569 2f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  /.    }.#endif. 
2b56a 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
2b56b 20 20 20 20 69 66 28 20 21 49 64 43 68 61 72 28      if( !IdChar(
2b56c 2a 7a 29 20 29 7b 0a 20 20 20 20 20 20 20 20 62  *z) ){.        b
2b56d 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
2b56e 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 49 64 43      for(i=1; IdC
2b56f 68 61 72 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b  har(z[i]); i++){
2b570 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  }.      *tokenTy
2b571 70 65 20 3d 20 6b 65 79 77 6f 72 64 43 6f 64 65  pe = keywordCode
2b572 28 28 63 68 61 72 2a 29 7a 2c 20 69 29 3b 0a 20  ((char*)z, i);. 
2b573 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20       return i;. 
2b574 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 74 6f 6b 65     }.  }.  *toke
2b575 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47  nType = TK_ILLEG
2b576 41 4c 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a  AL;.  return 1;.
2b577 7d 0a 0a 2f 2a 0a 2a 2a 20 52 75 6e 20 74 68 65  }../*.** Run the
2b578 20 70 61 72 73 65 72 20 6f 6e 20 74 68 65 20 67   parser on the g
2b579 69 76 65 6e 20 53 51 4c 20 73 74 72 69 6e 67 2e  iven SQL string.
2b57a 20 20 54 68 65 20 70 61 72 73 65 72 20 73 74 72    The parser str
2b57b 75 63 74 75 72 65 20 69 73 0a 2a 2a 20 70 61 73  ucture is.** pas
2b57c 73 65 64 20 69 6e 2e 20 20 41 6e 20 53 51 4c 49  sed in.  An SQLI
2b57d 54 45 5f 20 73 74 61 74 75 73 20 63 6f 64 65 20  TE_ status code 
2b57e 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66  is returned.  If
2b57f 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
2b580 0a 2a 2a 20 74 68 65 6e 20 61 6e 20 61 6e 64 20  .** then an and 
2b581 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20  attempt is made 
2b582 74 6f 20 77 72 69 74 65 20 61 6e 20 65 72 72 6f  to write an erro
2b583 72 20 6d 65 73 73 61 67 65 20 69 6e 74 6f 20 0a  r message into .
2b584 2a 2a 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  ** memory obtain
2b585 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  ed from sqlite3_
2b586 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 74 6f 20  malloc() and to 
2b587 6d 61 6b 65 20 2a 70 7a 45 72 72 4d 73 67 20 70  make *pzErrMsg p
2b588 6f 69 6e 74 20 74 6f 20 74 68 61 74 0a 2a 2a 20  oint to that.** 
2b589 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a  error message..*
2b58a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2b58b 20 69 6e 74 20 73 71 6c 69 74 65 33 52 75 6e 50   int sqlite3RunP
2b58c 61 72 73 65 72 28 50 61 72 73 65 20 2a 70 50 61  arser(Parse *pPa
2b58d 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  rse, const char 
2b58e 2a 7a 53 71 6c 2c 20 63 68 61 72 20 2a 2a 70 7a  *zSql, char **pz
2b58f 45 72 72 4d 73 67 29 7b 0a 20 20 69 6e 74 20 6e  ErrMsg){.  int n
2b590 45 72 72 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69  Err = 0;.  int i
2b591 3b 0a 20 20 76 6f 69 64 20 2a 70 45 6e 67 69 6e  ;.  void *pEngin
2b592 65 3b 0a 20 20 69 6e 74 20 74 6f 6b 65 6e 54 79  e;.  int tokenTy
2b593 70 65 3b 0a 20 20 69 6e 74 20 6c 61 73 74 54 6f  pe;.  int lastTo
2b594 6b 65 6e 50 61 72 73 65 64 20 3d 20 2d 31 3b 0a  kenParsed = -1;.
2b595 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2b596 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e  pParse->db;.  in
2b597 74 20 6d 78 53 71 6c 4c 65 6e 20 3d 20 64 62 2d  t mxSqlLen = db-
2b598 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
2b599 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54 48 5d  IMIT_SQL_LENGTH]
2b59a 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e 61 63 74  ;..  if( db->act
2b59b 69 76 65 56 64 62 65 43 6e 74 3d 3d 30 20 29 7b  iveVdbeCnt==0 ){
2b59c 0a 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e  .    db->u1.isIn
2b59d 74 65 72 72 75 70 74 65 64 20 3d 20 30 3b 0a 20  terrupted = 0;. 
2b59e 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e 72 63 20   }.  pParse->rc 
2b59f 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70  = SQLITE_OK;.  p
2b5a0 50 61 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 70  Parse->zTail = p
2b5a1 50 61 72 73 65 2d 3e 7a 53 71 6c 20 3d 20 7a 53  Parse->zSql = zS
2b5a2 71 6c 3b 0a 20 20 69 20 3d 20 30 3b 0a 20 20 61  ql;.  i = 0;.  a
2b5a3 73 73 65 72 74 28 20 70 7a 45 72 72 4d 73 67 21  ssert( pzErrMsg!
2b5a4 3d 30 20 29 3b 0a 20 20 70 45 6e 67 69 6e 65 20  =0 );.  pEngine 
2b5a5 3d 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41  = sqlite3ParserA
2b5a6 6c 6c 6f 63 28 28 76 6f 69 64 2a 28 2a 29 28 73  lloc((void*(*)(s
2b5a7 69 7a 65 5f 74 29 29 73 71 6c 69 74 65 33 4d 61  ize_t))sqlite3Ma
2b5a8 6c 6c 6f 63 29 3b 0a 20 20 69 66 28 20 70 45 6e  lloc);.  if( pEn
2b5a9 67 69 6e 65 3d 3d 30 20 29 7b 0a 20 20 20 20 64  gine==0 ){.    d
2b5aa 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2b5ab 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 1;.    return 
2b5ac 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
2b5ad 7d 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  }.  assert( pPar
2b5ae 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 64  se->sLastToken.d
2b5af 79 6e 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  yn==0 );.  asser
2b5b0 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  t( pParse->pNewT
2b5b1 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 61 73 73  able==0 );.  ass
2b5b2 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65  ert( pParse->pNe
2b5b3 77 54 72 69 67 67 65 72 3d 3d 30 20 29 3b 0a 20  wTrigger==0 );. 
2b5b4 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
2b5b5 3e 6e 56 61 72 3d 3d 30 20 29 3b 0a 20 20 61 73  >nVar==0 );.  as
2b5b6 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 56  sert( pParse->nV
2b5b7 61 72 45 78 70 72 3d 3d 30 20 29 3b 0a 20 20 61  arExpr==0 );.  a
2b5b8 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e  ssert( pParse->n
2b5b9 56 61 72 45 78 70 72 41 6c 6c 6f 63 3d 3d 30 20  VarExprAlloc==0 
2b5ba 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
2b5bb 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 3d 3d  rse->apVarExpr==
2b5bc 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 21 64  0 );.  while( !d
2b5bd 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2b5be 26 26 20 7a 53 71 6c 5b 69 5d 21 3d 30 20 29 7b  && zSql[i]!=0 ){
2b5bf 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d  .    assert( i>=
2b5c0 30 20 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  0 );.    pParse-
2b5c1 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 7a 20 3d 20  >sLastToken.z = 
2b5c2 28 75 38 2a 29 26 7a 53 71 6c 5b 69 5d 3b 0a 20  (u8*)&zSql[i];. 
2b5c3 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73     assert( pPars
2b5c4 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 64 79  e->sLastToken.dy
2b5c5 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 70 50 61 72  n==0 );.    pPar
2b5c6 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 6e  se->sLastToken.n
2b5c7 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b   = sqlite3GetTok
2b5c8 65 6e 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61  en((unsigned cha
2b5c9 72 2a 29 26 7a 53 71 6c 5b 69 5d 2c 26 74 6f 6b  r*)&zSql[i],&tok
2b5ca 65 6e 54 79 70 65 29 3b 0a 20 20 20 20 69 20 2b  enType);.    i +
2b5cb 3d 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54  = pParse->sLastT
2b5cc 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 69 66 28 20  oken.n;.    if( 
2b5cd 69 3e 6d 78 53 71 6c 4c 65 6e 20 29 7b 0a 20 20  i>mxSqlLen ){.  
2b5ce 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d      pParse->rc =
2b5cf 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a   SQLITE_TOOBIG;.
2b5d0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2b5d1 20 7d 0a 20 20 20 20 73 77 69 74 63 68 28 20 74   }.    switch( t
2b5d2 6f 6b 65 6e 54 79 70 65 20 29 7b 0a 20 20 20 20  okenType ){.    
2b5d3 20 20 63 61 73 65 20 54 4b 5f 53 50 41 43 45 3a    case TK_SPACE:
2b5d4 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 64   {.        if( d
2b5d5 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70  b->u1.isInterrup
2b5d6 74 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ted ){.         
2b5d7 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51   pParse->rc = SQ
2b5d8 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b 0a  LITE_INTERRUPT;.
2b5d9 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2b5da 33 53 65 74 53 74 72 69 6e 67 28 70 7a 45 72 72  3SetString(pzErr
2b5db 4d 73 67 2c 20 64 62 2c 20 22 69 6e 74 65 72 72  Msg, db, "interr
2b5dc 75 70 74 22 29 3b 0a 20 20 20 20 20 20 20 20 20  upt");.         
2b5dd 20 67 6f 74 6f 20 61 62 6f 72 74 5f 70 61 72 73   goto abort_pars
2b5de 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  e;.        }.   
2b5df 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2b5e0 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 54    }.      case T
2b5e1 4b 5f 49 4c 4c 45 47 41 4c 3a 20 7b 0a 20 20 20  K_ILLEGAL: {.   
2b5e2 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
2b5e3 65 65 28 64 62 2c 20 2a 70 7a 45 72 72 4d 73 67  ee(db, *pzErrMsg
2b5e4 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 7a 45 72  );.        *pzEr
2b5e5 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 50  rMsg = sqlite3MP
2b5e6 72 69 6e 74 66 28 64 62 2c 20 22 75 6e 72 65 63  rintf(db, "unrec
2b5e7 6f 67 6e 69 7a 65 64 20 74 6f 6b 65 6e 3a 20 5c  ognized token: \
2b5e8 22 25 54 5c 22 22 2c 0a 20 20 20 20 20 20 20 20  "%T\"",.        
2b5e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b5ea 26 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f  &pParse->sLastTo
2b5eb 6b 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 6e 45  ken);.        nE
2b5ec 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 67 6f  rr++;.        go
2b5ed 74 6f 20 61 62 6f 72 74 5f 70 61 72 73 65 3b 0a  to abort_parse;.
2b5ee 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
2b5ef 73 65 20 54 4b 5f 53 45 4d 49 3a 20 7b 0a 20 20  se TK_SEMI: {.  
2b5f0 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 54        pParse->zT
2b5f1 61 69 6c 20 3d 20 26 7a 53 71 6c 5b 69 5d 3b 0a  ail = &zSql[i];.
2b5f2 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20          /* Fall 
2b5f3 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20 64 65  thru into the de
2b5f4 66 61 75 6c 74 20 63 61 73 65 20 2a 2f 0a 20 20  fault case */.  
2b5f5 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61      }.      defa
2b5f6 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 73  ult: {.        s
2b5f7 71 6c 69 74 65 33 50 61 72 73 65 72 28 70 45 6e  qlite3Parser(pEn
2b5f8 67 69 6e 65 2c 20 74 6f 6b 65 6e 54 79 70 65 2c  gine, tokenType,
2b5f9 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f   pParse->sLastTo
2b5fa 6b 65 6e 2c 20 70 50 61 72 73 65 29 3b 0a 20 20  ken, pParse);.  
2b5fb 20 20 20 20 20 20 6c 61 73 74 54 6f 6b 65 6e 50        lastTokenP
2b5fc 61 72 73 65 64 20 3d 20 74 6f 6b 65 6e 54 79 70  arsed = tokenTyp
2b5fd 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  e;.        if( p
2b5fe 50 61 72 73 65 2d 3e 72 63 21 3d 53 51 4c 49 54  Parse->rc!=SQLIT
2b5ff 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
2b600 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 70 61 72    goto abort_par
2b601 73 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  se;.        }.  
2b602 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2b603 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 61     }.    }.  }.a
2b604 62 6f 72 74 5f 70 61 72 73 65 3a 0a 20 20 69 66  bort_parse:.  if
2b605 28 20 7a 53 71 6c 5b 69 5d 3d 3d 30 20 26 26 20  ( zSql[i]==0 && 
2b606 6e 45 72 72 3d 3d 30 20 26 26 20 70 50 61 72 73  nErr==0 && pPars
2b607 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  e->rc==SQLITE_OK
2b608 20 29 7b 0a 20 20 20 20 69 66 28 20 6c 61 73 74   ){.    if( last
2b609 54 6f 6b 65 6e 50 61 72 73 65 64 21 3d 54 4b 5f  TokenParsed!=TK_
2b60a 53 45 4d 49 20 29 7b 0a 20 20 20 20 20 20 73 71  SEMI ){.      sq
2b60b 6c 69 74 65 33 50 61 72 73 65 72 28 70 45 6e 67  lite3Parser(pEng
2b60c 69 6e 65 2c 20 54 4b 5f 53 45 4d 49 2c 20 70 50  ine, TK_SEMI, pP
2b60d 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e  arse->sLastToken
2b60e 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  , pParse);.     
2b60f 20 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 20 3d   pParse->zTail =
2b610 20 26 7a 53 71 6c 5b 69 5d 3b 0a 20 20 20 20 7d   &zSql[i];.    }
2b611 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73  .    sqlite3Pars
2b612 65 72 28 70 45 6e 67 69 6e 65 2c 20 30 2c 20 70  er(pEngine, 0, p
2b613 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65  Parse->sLastToke
2b614 6e 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a  n, pParse);.  }.
2b615 23 69 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41  #ifdef YYTRACKMA
2b616 58 53 54 41 43 4b 44 45 50 54 48 0a 20 20 73 71  XSTACKDEPTH.  sq
2b617 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53  lite3StatusSet(S
2b618 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 52  QLITE_STATUS_PAR
2b619 53 45 52 5f 53 54 41 43 4b 2c 0a 20 20 20 20 20  SER_STACK,.     
2b61a 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 53 74   sqlite3ParserSt
2b61b 61 63 6b 50 65 61 6b 28 70 45 6e 67 69 6e 65 29  ackPeak(pEngine)
2b61c 0a 20 20 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  .  );.#endif /* 
2b61d 59 59 44 45 42 55 47 20 2a 2f 0a 20 20 73 71 6c  YYDEBUG */.  sql
2b61e 69 74 65 33 50 61 72 73 65 72 46 72 65 65 28 70  ite3ParserFree(p
2b61f 45 6e 67 69 6e 65 2c 20 73 71 6c 69 74 65 33 5f  Engine, sqlite3_
2b620 66 72 65 65 29 3b 0a 20 20 69 66 28 20 64 62 2d  free);.  if( db-
2b621 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
2b622 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20  .    pParse->rc 
2b623 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
2b624 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65    }.  if( pParse
2b625 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ->rc!=SQLITE_OK 
2b626 26 26 20 70 50 61 72 73 65 2d 3e 72 63 21 3d 53  && pParse->rc!=S
2b627 51 4c 49 54 45 5f 44 4f 4e 45 20 26 26 20 70 50  QLITE_DONE && pP
2b628 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30  arse->zErrMsg==0
2b629 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
2b62a 65 74 53 74 72 69 6e 67 28 26 70 50 61 72 73 65  etString(&pParse
2b62b 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22  ->zErrMsg, db, "
2b62c 25 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53  %s", sqlite3ErrS
2b62d 74 72 28 70 50 61 72 73 65 2d 3e 72 63 29 29 3b  tr(pParse->rc));
2b62e 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73  .  }.  if( pPars
2b62f 65 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20  e->zErrMsg ){.  
2b630 20 20 69 66 28 20 2a 70 7a 45 72 72 4d 73 67 3d    if( *pzErrMsg=
2b631 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45  =0 ){.      *pzE
2b632 72 72 4d 73 67 20 3d 20 70 50 61 72 73 65 2d 3e  rrMsg = pParse->
2b633 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 7d 65 6c  zErrMsg;.    }el
2b634 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
2b635 33 44 62 46 72 65 65 28 64 62 2c 20 70 50 61 72  3DbFree(db, pPar
2b636 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20  se->zErrMsg);.  
2b637 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e    }.    pParse->
2b638 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20  zErrMsg = 0;.   
2b639 20 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a 20 20 69   nErr++;.  }.  i
2b63a 66 28 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65  f( pParse->pVdbe
2b63b 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 45 72 72   && pParse->nErr
2b63c 3e 30 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 65  >0 && pParse->ne
2b63d 73 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73  sted==0 ){.    s
2b63e 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65  qlite3VdbeDelete
2b63f 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 29 3b  (pParse->pVdbe);
2b640 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70 56 64  .    pParse->pVd
2b641 62 65 20 3d 20 30 3b 0a 20 20 7d 0a 23 69 66 6e  be = 0;.  }.#ifn
2b642 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2b643 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69  SHARED_CACHE.  i
2b644 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  f( pParse->neste
2b645 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  d==0 ){.    sqli
2b646 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 50  te3DbFree(db, pP
2b647 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b  arse->aTableLock
2b648 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 61  );.    pParse->a
2b649 54 61 62 6c 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20  TableLock = 0;. 
2b64a 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c     pParse->nTabl
2b64b 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 7d 0a 23  eLock = 0;.  }.#
2b64c 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51  endif.#ifndef SQ
2b64d 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2b64e 4c 54 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33  LTABLE.  sqlite3
2b64f 44 62 46 72 65 65 28 64 62 2c 20 70 50 61 72 73  DbFree(db, pPars
2b650 65 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 29 3b 0a  e->apVtabLock);.
2b651 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 21 49  #endif..  if( !I
2b652 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29  N_DECLARE_VTAB )
2b653 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  {.    /* If the 
2b654 70 50 61 72 73 65 2d 3e 64 65 63 6c 61 72 65 56  pParse->declareV
2b655 74 61 62 20 66 6c 61 67 20 69 73 20 73 65 74 2c  tab flag is set,
2b656 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 61   do not delete a
2b657 6e 79 20 74 61 62 6c 65 20 0a 20 20 20 20 2a 2a  ny table .    **
2b658 20 73 74 72 75 63 74 75 72 65 20 62 75 69 6c 74   structure built
2b659 20 75 70 20 69 6e 20 70 50 61 72 73 65 2d 3e 70   up in pParse->p
2b65a 4e 65 77 54 61 62 6c 65 2e 20 54 68 65 20 63 61  NewTable. The ca
2b65b 6c 6c 69 6e 67 20 63 6f 64 65 20 28 73 65 65 20  lling code (see 
2b65c 76 74 61 62 2e 63 29 0a 20 20 20 20 2a 2a 20 77  vtab.c).    ** w
2b65d 69 6c 6c 20 74 61 6b 65 20 72 65 73 70 6f 6e 73  ill take respons
2b65e 69 62 69 6c 69 74 79 20 66 6f 72 20 66 72 65 65  ibility for free
2b65f 69 6e 67 20 74 68 65 20 54 61 62 6c 65 20 73 74  ing the Table st
2b660 72 75 63 74 75 72 65 2e 0a 20 20 20 20 2a 2f 0a  ructure..    */.
2b661 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74      sqlite3Delet
2b662 65 54 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 70  eTable(pParse->p
2b663 4e 65 77 54 61 62 6c 65 29 3b 0a 20 20 7d 0a 0a  NewTable);.  }..
2b664 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54    sqlite3DeleteT
2b665 72 69 67 67 65 72 28 64 62 2c 20 70 50 61 72 73  rigger(db, pPars
2b666 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 29 3b  e->pNewTrigger);
2b667 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
2b668 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 70 56  (db, pParse->apV
2b669 61 72 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74  arExpr);.  sqlit
2b66a 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 50 61  e3DbFree(db, pPa
2b66b 72 73 65 2d 3e 61 41 6c 69 61 73 29 3b 0a 20 20  rse->aAlias);.  
2b66c 77 68 69 6c 65 28 20 70 50 61 72 73 65 2d 3e 70  while( pParse->p
2b66d 5a 6f 6d 62 69 65 54 61 62 20 29 7b 0a 20 20 20  ZombieTab ){.   
2b66e 20 54 61 62 6c 65 20 2a 70 20 3d 20 70 50 61 72   Table *p = pPar
2b66f 73 65 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 3b 0a  se->pZombieTab;.
2b670 20 20 20 20 70 50 61 72 73 65 2d 3e 70 5a 6f 6d      pParse->pZom
2b671 62 69 65 54 61 62 20 3d 20 70 2d 3e 70 4e 65 78  bieTab = p->pNex
2b672 74 5a 6f 6d 62 69 65 3b 0a 20 20 20 20 73 71 6c  tZombie;.    sql
2b673 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28  ite3DeleteTable(
2b674 70 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 45  p);.  }.  if( nE
2b675 72 72 3e 30 20 26 26 20 28 70 50 61 72 73 65 2d  rr>0 && (pParse-
2b676 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c  >rc==SQLITE_OK |
2b677 7c 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51  | pParse->rc==SQ
2b678 4c 49 54 45 5f 44 4f 4e 45 29 20 29 7b 0a 20 20  LITE_DONE) ){.  
2b679 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
2b67a 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
2b67b 0a 20 20 72 65 74 75 72 6e 20 6e 45 72 72 3b 0a  .  return nErr;.
2b67c 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
2b67d 2a 2a 20 45 6e 64 20 6f 66 20 74 6f 6b 65 6e 69  ** End of tokeni
2b67e 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ze.c ***********
2b67f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b681 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
2b682 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 63 6f  ** Begin file co
2b683 6d 70 6c 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  mplete.c *******
2b684 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b685 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b686 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
2b687 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
2b688 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
2b689 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
2b68a 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
2b68b 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
2b68c 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
2b68d 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
2b68e 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
2b68f 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
2b690 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
2b691 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
2b692 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
2b693 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
2b694 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
2b695 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
2b696 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
2b697 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
2b698 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
2b699 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b69a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b69b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b69c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b69d 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 41 6e 20  *********.** An 
2b69e 74 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 53 51  tokenizer for SQ
2b69f 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  L.**.** This fil
2b6a0 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64  e contains C cod
2b6a1 65 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  e that implement
2b6a2 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f  s the sqlite3_co
2b6a3 6d 70 6c 65 74 65 28 29 20 41 50 49 2e 0a 2a 2a  mplete() API..**
2b6a4 20 54 68 69 73 20 63 6f 64 65 20 75 73 65 64 20   This code used 
2b6a5 74 6f 20 62 65 20 70 61 72 74 20 6f 66 20 74 68  to be part of th
2b6a6 65 20 74 6f 6b 65 6e 69 7a 65 72 2e 63 20 73 6f  e tokenizer.c so
2b6a7 75 72 63 65 20 66 69 6c 65 2e 20 20 42 75 74 20  urce file.  But 
2b6a8 62 79 0a 2a 2a 20 73 65 70 61 72 61 74 69 6e 67  by.** separating
2b6a9 20 69 74 20 6f 75 74 2c 20 74 68 65 20 63 6f 64   it out, the cod
2b6aa 65 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61  e will be automa
2b6ab 74 69 63 61 6c 6c 79 20 6f 6d 69 74 74 65 64 20  tically omitted 
2b6ac 66 72 6f 6d 0a 2a 2a 20 73 74 61 74 69 63 20 6c  from.** static l
2b6ad 69 6e 6b 73 20 74 68 61 74 20 64 6f 20 6e 6f 74  inks that do not
2b6ae 20 75 73 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 24   use it..**.** $
2b6af 49 64 3a 20 63 6f 6d 70 6c 65 74 65 2e 63 2c 76  Id: complete.c,v
2b6b0 20 31 2e 37 20 32 30 30 38 2f 30 36 2f 31 33 20   1.7 2008/06/13 
2b6b1 31 38 3a 32 34 3a 32 37 20 64 72 68 20 45 78 70  18:24:27 drh Exp
2b6b2 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51   $.*/.#ifndef SQ
2b6b3 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4c 45  LITE_OMIT_COMPLE
2b6b4 54 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69  TE../*.** This i
2b6b5 73 20 64 65 66 69 6e 65 64 20 69 6e 20 74 6f 6b  s defined in tok
2b6b6 65 6e 69 7a 65 2e 63 2e 20 20 57 65 20 6a 75 73  enize.c.  We jus
2b6b7 74 20 68 61 76 65 20 74 6f 20 69 6d 70 6f 72 74  t have to import
2b6b8 20 74 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 2e   the definition.
2b6b9 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
2b6ba 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a  TE_AMALGAMATION.
2b6bb 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53  #ifdef SQLITE_AS
2b6bc 43 49 49 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  CII.SQLITE_PRIVA
2b6bd 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71  TE const char sq
2b6be 6c 69 74 65 33 49 73 41 73 63 69 69 49 64 43 68  lite3IsAsciiIdCh
2b6bf 61 72 5b 5d 3b 0a 23 64 65 66 69 6e 65 20 49 64  ar[];.#define Id
2b6c0 43 68 61 72 28 43 29 20 20 28 28 28 63 3d 43 29  Char(C)  (((c=C)
2b6c1 26 30 78 38 30 29 21 3d 30 20 7c 7c 20 28 63 3e  &0x80)!=0 || (c>
2b6c2 30 78 31 66 20 26 26 20 73 71 6c 69 74 65 33 49  0x1f && sqlite3I
2b6c3 73 41 73 63 69 69 49 64 43 68 61 72 5b 63 2d 30  sAsciiIdChar[c-0
2b6c4 78 32 30 5d 29 29 0a 23 65 6e 64 69 66 0a 23 69  x20])).#endif.#i
2b6c5 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44  fdef SQLITE_EBCD
2b6c6 49 43 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  IC.SQLITE_PRIVAT
2b6c7 45 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c  E const char sql
2b6c8 69 74 65 33 49 73 45 62 63 64 69 63 49 64 43 68  ite3IsEbcdicIdCh
2b6c9 61 72 5b 5d 3b 0a 23 64 65 66 69 6e 65 20 49 64  ar[];.#define Id
2b6ca 43 68 61 72 28 43 29 20 20 28 28 28 63 3d 43 29  Char(C)  (((c=C)
2b6cb 3e 3d 30 78 34 32 20 26 26 20 73 71 6c 69 74 65  >=0x42 && sqlite
2b6cc 33 49 73 45 62 63 64 69 63 49 64 43 68 61 72 5b  3IsEbcdicIdChar[
2b6cd 63 2d 30 78 34 30 5d 29 29 0a 23 65 6e 64 69 66  c-0x40])).#endif
2b6ce 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2b6cf 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 2a  E_AMALGAMATION *
2b6d0 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 6f 6b 65 6e 20  /.../*.** Token 
2b6d1 74 79 70 65 73 20 75 73 65 64 20 62 79 20 74 68  types used by th
2b6d2 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65  e sqlite3_comple
2b6d3 74 65 28 29 20 72 6f 75 74 69 6e 65 2e 20 20 53  te() routine.  S
2b6d4 65 65 20 74 68 65 20 68 65 61 64 65 72 0a 2a 2a  ee the header.**
2b6d5 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68 61   comments on tha
2b6d6 74 20 70 72 6f 63 65 64 75 72 65 20 66 6f 72 20  t procedure for 
2b6d7 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
2b6d8 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69  mation..*/.#defi
2b6d9 6e 65 20 74 6b 53 45 4d 49 20 20 20 20 30 0a 23  ne tkSEMI    0.#
2b6da 64 65 66 69 6e 65 20 74 6b 57 53 20 20 20 20 20  define tkWS     
2b6db 20 31 0a 23 64 65 66 69 6e 65 20 74 6b 4f 54 48   1.#define tkOTH
2b6dc 45 52 20 20 20 32 0a 23 64 65 66 69 6e 65 20 74  ER   2.#define t
2b6dd 6b 45 58 50 4c 41 49 4e 20 33 0a 23 64 65 66 69  kEXPLAIN 3.#defi
2b6de 6e 65 20 74 6b 43 52 45 41 54 45 20 20 34 0a 23  ne tkCREATE  4.#
2b6df 64 65 66 69 6e 65 20 74 6b 54 45 4d 50 20 20 20  define tkTEMP   
2b6e0 20 35 0a 23 64 65 66 69 6e 65 20 74 6b 54 52 49   5.#define tkTRI
2b6e1 47 47 45 52 20 36 0a 23 64 65 66 69 6e 65 20 74  GGER 6.#define t
2b6e2 6b 45 4e 44 20 20 20 20 20 37 0a 0a 2f 2a 0a 2a  kEND     7../*.*
2b6e3 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66  * Return TRUE if
2b6e4 20 74 68 65 20 67 69 76 65 6e 20 53 51 4c 20 73   the given SQL s
2b6e5 74 72 69 6e 67 20 65 6e 64 73 20 69 6e 20 61 20  tring ends in a 
2b6e6 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a 2a  semicolon..**.**
2b6e7 20 53 70 65 63 69 61 6c 20 68 61 6e 64 6c 69 6e   Special handlin
2b6e8 67 20 69 73 20 72 65 71 75 69 72 65 20 66 6f 72  g is require for
2b6e9 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20   CREATE TRIGGER 
2b6ea 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 57  statements..** W
2b6eb 68 65 6e 65 76 65 72 20 74 68 65 20 43 52 45 41  henever the CREA
2b6ec 54 45 20 54 52 49 47 47 45 52 20 6b 65 79 77 6f  TE TRIGGER keywo
2b6ed 72 64 73 20 61 72 65 20 73 65 65 6e 2c 20 74 68  rds are seen, th
2b6ee 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 6d  e statement.** m
2b6ef 75 73 74 20 65 6e 64 20 77 69 74 68 20 22 3b 45  ust end with ";E
2b6f0 4e 44 3b 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ND;"..**.** This
2b6f1 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
2b6f2 75 73 65 73 20 61 20 73 74 61 74 65 20 6d 61 63  uses a state mac
2b6f3 68 69 6e 65 20 77 69 74 68 20 37 20 73 74 61 74  hine with 7 stat
2b6f4 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 30 29 20  es:.**.**   (0) 
2b6f5 53 54 41 52 54 20 20 20 20 20 41 74 20 74 68 65  START     At the
2b6f6 20 62 65 67 69 6e 6e 69 6e 67 20 6f 72 20 65 6e   beginning or en
2b6f7 64 20 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74  d of an SQL stat
2b6f8 65 6d 65 6e 74 2e 20 20 54 68 69 73 20 72 6f 75  ement.  This rou
2b6f9 74 69 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  tine.**         
2b6fa 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 73 20          returns 
2b6fb 31 20 69 66 20 69 74 20 65 6e 64 73 20 69 6e 20  1 if it ends in 
2b6fc 74 68 65 20 53 54 41 52 54 20 73 74 61 74 65 20  the START state 
2b6fd 61 6e 64 20 30 20 69 66 20 69 74 20 65 6e 64 73  and 0 if it ends
2b6fe 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2b6ff 20 20 20 20 69 6e 20 61 6e 79 20 6f 74 68 65 72      in any other
2b700 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20   state..**.**   
2b701 28 31 29 20 4e 4f 52 4d 41 4c 20 20 20 20 57 65  (1) NORMAL    We
2b702 20 61 72 65 20 69 6e 20 74 68 65 20 6d 69 64 64   are in the midd
2b703 6c 65 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 20  le of statement 
2b704 77 68 69 63 68 20 65 6e 64 73 20 77 69 74 68 20  which ends with 
2b705 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 20 20 20 20  a single.**     
2b706 20 20 20 20 20 20 20 20 20 20 20 20 73 65 6d 69              semi
2b707 63 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28  colon..**.**   (
2b708 32 29 20 45 58 50 4c 41 49 4e 20 20 20 54 68 65  2) EXPLAIN   The
2b709 20 6b 65 79 77 6f 72 64 20 45 58 50 4c 41 49 4e   keyword EXPLAIN
2b70a 20 68 61 73 20 62 65 65 6e 20 73 65 65 6e 20 61   has been seen a
2b70b 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  t the beginning 
2b70c 6f 66 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  of .**          
2b70d 20 20 20 20 20 20 20 61 20 73 74 61 74 65 6d 65         a stateme
2b70e 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 33 29 20  nt..**.**   (3) 
2b70f 43 52 45 41 54 45 20 20 20 20 54 68 65 20 6b 65  CREATE    The ke
2b710 79 77 6f 72 64 20 43 52 45 41 54 45 20 68 61 73  yword CREATE has
2b711 20 62 65 65 6e 20 73 65 65 6e 20 61 74 20 74 68   been seen at th
2b712 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61  e beginning of a
2b713 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2b714 20 20 20 20 73 74 61 74 65 6d 65 6e 74 2c 20 70      statement, p
2b715 6f 73 73 69 62 6c 79 20 70 72 65 63 65 65 64 65  ossibly preceede
2b716 64 20 62 79 20 45 58 50 4c 41 49 4e 20 61 6e 64  d by EXPLAIN and
2b717 2f 6f 72 20 66 6f 6c 6c 6f 77 65 64 20 62 79 0a  /or followed by.
2b718 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2b719 20 20 20 54 45 4d 50 20 6f 72 20 54 45 4d 50 4f     TEMP or TEMPO
2b71a 52 41 52 59 0a 2a 2a 0a 2a 2a 20 20 20 28 34 29  RARY.**.**   (4)
2b71b 20 54 52 49 47 47 45 52 20 20 20 57 65 20 61 72   TRIGGER   We ar
2b71c 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  e in the middle 
2b71d 6f 66 20 61 20 74 72 69 67 67 65 72 20 64 65 66  of a trigger def
2b71e 69 6e 69 74 69 6f 6e 20 74 68 61 74 20 6d 75 73  inition that mus
2b71f 74 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  t be.**         
2b720 20 20 20 20 20 20 20 20 65 6e 64 65 64 20 62 79          ended by
2b721 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 2c 20 74 68   a semicolon, th
2b722 65 20 6b 65 79 77 6f 72 64 20 45 4e 44 2c 20 61  e keyword END, a
2b723 6e 64 20 61 6e 6f 74 68 65 72 20 73 65 6d 69 63  nd another semic
2b724 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 35  olon..**.**   (5
2b725 29 20 53 45 4d 49 20 20 20 20 20 20 57 65 27 76  ) SEMI      We'v
2b726 65 20 73 65 65 6e 20 74 68 65 20 66 69 72 73 74  e seen the first
2b727 20 73 65 6d 69 63 6f 6c 6f 6e 20 69 6e 20 74 68   semicolon in th
2b728 65 20 22 3b 45 4e 44 3b 22 20 74 68 61 74 20 6f  e ";END;" that o
2b729 63 63 75 72 73 20 61 74 0a 2a 2a 20 20 20 20 20  ccurs at.**     
2b72a 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20              the 
2b72b 65 6e 64 20 6f 66 20 61 20 74 72 69 67 67 65 72  end of a trigger
2b72c 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a   definition..**.
2b72d 2a 2a 20 20 20 28 36 29 20 45 4e 44 20 20 20 20  **   (6) END    
2b72e 20 20 20 57 65 27 76 65 20 73 65 65 6e 20 74 68     We've seen th
2b72f 65 20 22 3b 45 4e 44 22 20 6f 66 20 74 68 65 20  e ";END" of the 
2b730 22 3b 45 4e 44 3b 22 20 74 68 61 74 20 6f 63 63  ";END;" that occ
2b731 75 72 73 20 61 74 20 74 68 65 20 65 6e 64 0a 2a  urs at the end.*
2b732 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2b733 20 20 6f 66 20 61 20 74 72 69 67 67 65 72 20 64    of a trigger d
2b734 69 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  ifinition..**.**
2b735 20 54 72 61 6e 73 69 74 69 6f 6e 73 20 62 65 74   Transitions bet
2b736 77 65 65 6e 20 73 74 61 74 65 73 20 61 62 6f 76  ween states abov
2b737 65 20 61 72 65 20 64 65 74 65 72 6d 69 6e 65 64  e are determined
2b738 20 62 79 20 74 6f 6b 65 6e 73 20 65 78 74 72 61   by tokens extra
2b739 63 74 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65  cted.** from the
2b73a 20 69 6e 70 75 74 2e 20 20 54 68 65 20 66 6f 6c   input.  The fol
2b73b 6c 6f 77 69 6e 67 20 74 6f 6b 65 6e 73 20 61 72  lowing tokens ar
2b73c 65 20 73 69 67 6e 69 66 69 63 61 6e 74 3a 0a 2a  e significant:.*
2b73d 2a 0a 2a 2a 20 20 20 28 30 29 20 74 6b 53 45 4d  *.**   (0) tkSEM
2b73e 49 20 20 20 20 20 20 41 20 73 65 6d 69 63 6f 6c  I      A semicol
2b73f 6f 6e 2e 0a 2a 2a 20 20 20 28 31 29 20 74 6b 57  on..**   (1) tkW
2b740 53 20 20 20 20 20 20 20 20 57 68 69 74 65 73 70  S        Whitesp
2b741 61 63 65 0a 2a 2a 20 20 20 28 32 29 20 74 6b 4f  ace.**   (2) tkO
2b742 54 48 45 52 20 20 20 20 20 41 6e 79 20 6f 74 68  THER     Any oth
2b743 65 72 20 53 51 4c 20 74 6f 6b 65 6e 2e 0a 2a 2a  er SQL token..**
2b744 20 20 20 28 33 29 20 74 6b 45 58 50 4c 41 49 4e     (3) tkEXPLAIN
2b745 20 20 20 54 68 65 20 22 65 78 70 6c 61 69 6e 22     The "explain"
2b746 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a 20 20 20 28   keyword..**   (
2b747 34 29 20 74 6b 43 52 45 41 54 45 20 20 20 20 54  4) tkCREATE    T
2b748 68 65 20 22 63 72 65 61 74 65 22 20 6b 65 79 77  he "create" keyw
2b749 6f 72 64 2e 0a 2a 2a 20 20 20 28 35 29 20 74 6b  ord..**   (5) tk
2b74a 54 45 4d 50 20 20 20 20 20 20 54 68 65 20 22 74  TEMP      The "t
2b74b 65 6d 70 22 20 6f 72 20 22 74 65 6d 70 6f 72 61  emp" or "tempora
2b74c 72 79 22 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a 20  ry" keyword..** 
2b74d 20 20 28 36 29 20 74 6b 54 52 49 47 47 45 52 20    (6) tkTRIGGER 
2b74e 20 20 54 68 65 20 22 74 72 69 67 67 65 72 22 20    The "trigger" 
2b74f 6b 65 79 77 6f 72 64 2e 0a 2a 2a 20 20 20 28 37  keyword..**   (7
2b750 29 20 74 6b 45 4e 44 20 20 20 20 20 20 20 54 68  ) tkEND       Th
2b751 65 20 22 65 6e 64 22 20 6b 65 79 77 6f 72 64 2e  e "end" keyword.
2b752 0a 2a 2a 0a 2a 2a 20 57 68 69 74 65 73 70 61 63  .**.** Whitespac
2b753 65 20 6e 65 76 65 72 20 63 61 75 73 65 73 20 61  e never causes a
2b754 20 73 74 61 74 65 20 74 72 61 6e 73 69 74 69 6f   state transitio
2b755 6e 20 61 6e 64 20 69 73 20 61 6c 77 61 79 73 20  n and is always 
2b756 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  ignored..**.** I
2b757 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74  f we compile wit
2b758 68 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  h SQLITE_OMIT_TR
2b759 49 47 47 45 52 2c 20 61 6c 6c 20 6f 66 20 74 68  IGGER, all of th
2b75a 65 20 63 6f 6d 70 75 74 61 74 69 6f 6e 20 6e 65  e computation ne
2b75b 65 64 65 64 0a 2a 2a 20 74 6f 20 72 65 63 6f 67  eded.** to recog
2b75c 6e 69 7a 65 20 74 68 65 20 65 6e 64 20 6f 66 20  nize the end of 
2b75d 61 20 74 72 69 67 67 65 72 20 63 61 6e 20 62 65  a trigger can be
2b75e 20 6f 6d 69 74 74 65 64 2e 20 20 41 6c 6c 20 77   omitted.  All w
2b75f 65 20 68 61 76 65 20 74 6f 20 64 6f 0a 2a 2a 20  e have to do.** 
2b760 69 73 20 6c 6f 6f 6b 20 66 6f 72 20 61 20 73 65  is look for a se
2b761 6d 69 63 6f 6c 6f 6e 20 74 68 61 74 20 69 73 20  micolon that is 
2b762 6e 6f 74 20 70 61 72 74 20 6f 66 20 61 6e 20 73  not part of an s
2b763 74 72 69 6e 67 20 6f 72 20 63 6f 6d 6d 65 6e 74  tring or comment
2b764 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2b765 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  int sqlite3_comp
2b766 6c 65 74 65 28 63 6f 6e 73 74 20 63 68 61 72 20  lete(const char 
2b767 2a 7a 53 71 6c 29 7b 0a 20 20 75 38 20 73 74 61  *zSql){.  u8 sta
2b768 74 65 20 3d 20 30 3b 20 20 20 2f 2a 20 43 75 72  te = 0;   /* Cur
2b769 72 65 6e 74 20 73 74 61 74 65 2c 20 75 73 69 6e  rent state, usin
2b76a 67 20 6e 75 6d 62 65 72 73 20 64 65 66 69 6e 65  g numbers define
2b76b 64 20 69 6e 20 68 65 61 64 65 72 20 63 6f 6d 6d  d in header comm
2b76c 65 6e 74 20 2a 2f 0a 20 20 75 38 20 74 6f 6b 65  ent */.  u8 toke
2b76d 6e 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75  n;       /* Valu
2b76e 65 20 6f 66 20 74 68 65 20 6e 65 78 74 20 74 6f  e of the next to
2b76f 6b 65 6e 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  ken */..#ifndef 
2b770 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
2b771 47 45 52 0a 20 20 2f 2a 20 41 20 63 6f 6d 70 6c  GER.  /* A compl
2b772 65 78 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 63  ex statement mac
2b773 68 69 6e 65 20 75 73 65 64 20 74 6f 20 64 65 74  hine used to det
2b774 65 63 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61  ect the end of a
2b775 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 0a   CREATE TRIGGER.
2b776 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20    ** statement. 
2b777 20 54 68 69 73 20 69 73 20 74 68 65 20 6e 6f 72   This is the nor
2b778 6d 61 6c 20 63 61 73 65 2e 0a 20 20 2a 2f 0a 20  mal case..  */. 
2b779 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38   static const u8
2b77a 20 74 72 61 6e 73 5b 37 5d 5b 38 5d 20 3d 20 7b   trans[7][8] = {
2b77b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2b77c 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 3a 20        /* Token: 
2b77d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b77e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b77f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
2b780 2f 0a 20 20 20 20 20 2f 2a 20 53 74 61 74 65 3a  /.     /* State:
2b781 20 20 20 20 20 20 20 2a 2a 20 20 53 45 4d 49 20         **  SEMI 
2b782 20 57 53 20 20 4f 54 48 45 52 20 45 58 50 4c 41   WS  OTHER EXPLA
2b783 49 4e 20 20 43 52 45 41 54 45 20 20 54 45 4d 50  IN  CREATE  TEMP
2b784 20 20 54 52 49 47 47 45 52 20 20 45 4e 44 20 20    TRIGGER  END  
2b785 2a 2f 0a 20 20 20 20 20 2f 2a 20 30 20 20 20 53  */.     /* 0   S
2b786 54 41 52 54 3a 20 2a 2f 20 7b 20 20 20 20 30 2c  TART: */ {    0,
2b787 20 20 30 2c 20 20 20 20 20 31 2c 20 20 20 20 20    0,     1,     
2b788 20 32 2c 20 20 20 20 20 20 33 2c 20 20 20 20 31   2,      3,    1
2b789 2c 20 20 20 20 20 20 20 31 2c 20 20 20 31 2c 20  ,       1,   1, 
2b78a 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 31 20 20 4e   },.     /* 1  N
2b78b 4f 52 4d 41 4c 3a 20 2a 2f 20 7b 20 20 20 20 30  ORMAL: */ {    0
2b78c 2c 20 20 31 2c 20 20 20 20 20 31 2c 20 20 20 20  ,  1,     1,    
2b78d 20 20 31 2c 20 20 20 20 20 20 31 2c 20 20 20 20    1,      1,    
2b78e 31 2c 20 20 20 20 20 20 20 31 2c 20 20 20 31 2c  1,       1,   1,
2b78f 20 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 32 20 45    },.     /* 2 E
2b790 58 50 4c 41 49 4e 3a 20 2a 2f 20 7b 20 20 20 20  XPLAIN: */ {    
2b791 30 2c 20 20 32 2c 20 20 20 20 20 31 2c 20 20 20  0,  2,     1,   
2b792 20 20 20 31 2c 20 20 20 20 20 20 33 2c 20 20 20     1,      3,   
2b793 20 31 2c 20 20 20 20 20 20 20 31 2c 20 20 20 31   1,       1,   1
2b794 2c 20 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 33 20  ,  },.     /* 3 
2b795 20 43 52 45 41 54 45 3a 20 2a 2f 20 7b 20 20 20   CREATE: */ {   
2b796 20 30 2c 20 20 33 2c 20 20 20 20 20 31 2c 20 20   0,  3,     1,  
2b797 20 20 20 20 31 2c 20 20 20 20 20 20 31 2c 20 20      1,      1,  
2b798 20 20 33 2c 20 20 20 20 20 20 20 34 2c 20 20 20    3,       4,   
2b799 31 2c 20 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 34  1,  },.     /* 4
2b79a 20 54 52 49 47 47 45 52 3a 20 2a 2f 20 7b 20 20   TRIGGER: */ {  
2b79b 20 20 35 2c 20 20 34 2c 20 20 20 20 20 34 2c 20    5,  4,     4, 
2b79c 20 20 20 20 20 34 2c 20 20 20 20 20 20 34 2c 20       4,      4, 
2b79d 20 20 20 34 2c 20 20 20 20 20 20 20 34 2c 20 20     4,       4,  
2b79e 20 34 2c 20 20 7d 2c 0a 20 20 20 20 20 2f 2a 20   4,  },.     /* 
2b79f 35 20 20 20 20 53 45 4d 49 3a 20 2a 2f 20 7b 20  5    SEMI: */ { 
2b7a0 20 20 20 35 2c 20 20 35 2c 20 20 20 20 20 34 2c     5,  5,     4,
2b7a1 20 20 20 20 20 20 34 2c 20 20 20 20 20 20 34 2c        4,      4,
2b7a2 20 20 20 20 34 2c 20 20 20 20 20 20 20 34 2c 20      4,       4, 
2b7a3 20 20 36 2c 20 20 7d 2c 0a 20 20 20 20 20 2f 2a    6,  },.     /*
2b7a4 20 36 20 20 20 20 20 45 4e 44 3a 20 2a 2f 20 7b   6     END: */ {
2b7a5 20 20 20 20 30 2c 20 20 36 2c 20 20 20 20 20 34      0,  6,     4
2b7a6 2c 20 20 20 20 20 20 34 2c 20 20 20 20 20 20 34  ,      4,      4
2b7a7 2c 20 20 20 20 34 2c 20 20 20 20 20 20 20 34 2c  ,    4,       4,
2b7a8 20 20 20 34 2c 20 20 7d 2c 0a 20 20 7d 3b 0a 23     4,  },.  };.#
2b7a9 65 6c 73 65 0a 20 20 2f 2a 20 49 66 20 74 72 69  else.  /* If tri
2b7aa 67 67 65 72 73 20 61 72 65 20 6e 6f 74 20 73 75  ggers are not su
2b7ab 70 70 6f 72 65 64 20 62 79 20 74 68 69 73 20 63  ppored by this c
2b7ac 6f 6d 70 69 6c 65 20 74 68 65 6e 20 74 68 65 20  ompile then the 
2b7ad 73 74 61 74 65 6d 65 6e 74 20 6d 61 63 68 69 6e  statement machin
2b7ae 65 0a 20 20 2a 2a 20 75 73 65 64 20 74 6f 20 64  e.  ** used to d
2b7af 65 74 65 63 74 20 74 68 65 20 65 6e 64 20 6f 66  etect the end of
2b7b0 20 61 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20   a statement is 
2b7b1 6d 75 63 68 20 73 69 6d 70 6c 69 65 72 0a 20 20  much simplier.  
2b7b2 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  */.  static cons
2b7b3 74 20 75 38 20 74 72 61 6e 73 5b 32 5d 5b 33 5d  t u8 trans[2][3]
2b7b4 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20   = {.           
2b7b5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b            /* Tok
2b7b6 65 6e 3a 20 20 20 20 20 20 20 20 20 20 20 2a 2f  en:           */
2b7b7 0a 20 20 20 20 20 2f 2a 20 53 74 61 74 65 3a 20  .     /* State: 
2b7b8 20 20 20 20 20 20 2a 2a 20 20 53 45 4d 49 20 20        **  SEMI  
2b7b9 57 53 20 20 4f 54 48 45 52 20 2a 2f 0a 20 20 20  WS  OTHER */.   
2b7ba 20 20 2f 2a 20 30 20 20 20 53 54 41 52 54 3a 20    /* 0   START: 
2b7bb 2a 2f 20 7b 20 20 20 20 30 2c 20 20 30 2c 20 20  */ {    0,  0,  
2b7bc 20 20 20 31 2c 20 7d 2c 0a 20 20 20 20 20 2f 2a     1, },.     /*
2b7bd 20 31 20 20 4e 4f 52 4d 41 4c 3a 20 2a 2f 20 7b   1  NORMAL: */ {
2b7be 20 20 20 20 30 2c 20 20 31 2c 20 20 20 20 20 31      0,  1,     1
2b7bf 2c 20 7d 2c 0a 20 20 7d 3b 0a 23 65 6e 64 69 66  , },.  };.#endif
2b7c0 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2b7c1 54 52 49 47 47 45 52 20 2a 2f 0a 0a 20 20 77 68  TRIGGER */..  wh
2b7c2 69 6c 65 28 20 2a 7a 53 71 6c 20 29 7b 0a 20 20  ile( *zSql ){.  
2b7c3 20 20 73 77 69 74 63 68 28 20 2a 7a 53 71 6c 20    switch( *zSql 
2b7c4 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 27 3b  ){.      case ';
2b7c5 27 3a 20 7b 20 20 2f 2a 20 41 20 73 65 6d 69 63  ': {  /* A semic
2b7c6 6f 6c 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20  olon */.        
2b7c7 74 6f 6b 65 6e 20 3d 20 74 6b 53 45 4d 49 3b 0a  token = tkSEMI;.
2b7c8 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2b7c9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
2b7ca 65 20 27 20 27 3a 0a 20 20 20 20 20 20 63 61 73  e ' ':.      cas
2b7cb 65 20 27 5c 72 27 3a 0a 20 20 20 20 20 20 63 61  e '\r':.      ca
2b7cc 73 65 20 27 5c 74 27 3a 0a 20 20 20 20 20 20 63  se '\t':.      c
2b7cd 61 73 65 20 27 5c 6e 27 3a 0a 20 20 20 20 20 20  ase '\n':.      
2b7ce 63 61 73 65 20 27 5c 66 27 3a 20 7b 20 20 2f 2a  case '\f': {  /*
2b7cf 20 57 68 69 74 65 20 73 70 61 63 65 20 69 73 20   White space is 
2b7d0 69 67 6e 6f 72 65 64 20 2a 2f 0a 20 20 20 20 20  ignored */.     
2b7d1 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 57 53 3b     token = tkWS;
2b7d2 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2b7d3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
2b7d4 73 65 20 27 2f 27 3a 20 7b 20 20 20 2f 2a 20 43  se '/': {   /* C
2b7d5 2d 73 74 79 6c 65 20 63 6f 6d 6d 65 6e 74 73 20  -style comments 
2b7d6 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a  */.        if( z
2b7d7 53 71 6c 5b 31 5d 21 3d 27 2a 27 20 29 7b 0a 20  Sql[1]!='*' ){. 
2b7d8 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d           token =
2b7d9 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20   tkOTHER;.      
2b7da 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2b7db 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7a 53 71     }.        zSq
2b7dc 6c 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 20  l += 2;.        
2b7dd 77 68 69 6c 65 28 20 7a 53 71 6c 5b 30 5d 20 26  while( zSql[0] &
2b7de 26 20 28 7a 53 71 6c 5b 30 5d 21 3d 27 2a 27 20  & (zSql[0]!='*' 
2b7df 7c 7c 20 7a 53 71 6c 5b 31 5d 21 3d 27 2f 27 29  || zSql[1]!='/')
2b7e0 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20 20   ){ zSql++; }.  
2b7e1 20 20 20 20 20 20 69 66 28 20 7a 53 71 6c 5b 30        if( zSql[0
2b7e2 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  ]==0 ) return 0;
2b7e3 0a 20 20 20 20 20 20 20 20 7a 53 71 6c 2b 2b 3b  .        zSql++;
2b7e4 0a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d  .        token =
2b7e5 20 74 6b 57 53 3b 0a 20 20 20 20 20 20 20 20 62   tkWS;.        b
2b7e6 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
2b7e7 20 20 20 20 63 61 73 65 20 27 2d 27 3a 20 7b 20      case '-': { 
2b7e8 20 20 2f 2a 20 53 51 4c 2d 73 74 79 6c 65 20 63    /* SQL-style c
2b7e9 6f 6d 6d 65 6e 74 73 20 66 72 6f 6d 20 22 2d 2d  omments from "--
2b7ea 22 20 74 6f 20 65 6e 64 20 6f 66 20 6c 69 6e 65  " to end of line
2b7eb 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
2b7ec 7a 53 71 6c 5b 31 5d 21 3d 27 2d 27 20 29 7b 0a  zSql[1]!='-' ){.
2b7ed 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20            token 
2b7ee 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20  = tkOTHER;.     
2b7ef 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2b7f0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 77 68      }.        wh
2b7f1 69 6c 65 28 20 2a 7a 53 71 6c 20 26 26 20 2a 7a  ile( *zSql && *z
2b7f2 53 71 6c 21 3d 27 5c 6e 27 20 29 7b 20 7a 53 71  Sql!='\n' ){ zSq
2b7f3 6c 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20 69  l++; }.        i
2b7f4 66 28 20 2a 7a 53 71 6c 3d 3d 30 20 29 20 72 65  f( *zSql==0 ) re
2b7f5 74 75 72 6e 20 73 74 61 74 65 3d 3d 30 3b 0a 20  turn state==0;. 
2b7f6 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
2b7f7 6b 57 53 3b 0a 20 20 20 20 20 20 20 20 62 72 65  kWS;.        bre
2b7f8 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
2b7f9 20 20 63 61 73 65 20 27 5b 27 3a 20 7b 20 20 20    case '[': {   
2b7fa 2f 2a 20 4d 69 63 72 6f 73 6f 66 74 2d 73 74 79  /* Microsoft-sty
2b7fb 6c 65 20 69 64 65 6e 74 69 66 69 65 72 73 20 69  le identifiers i
2b7fc 6e 20 5b 2e 2e 2e 5d 20 2a 2f 0a 20 20 20 20 20  n [...] */.     
2b7fd 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20 20 20 20     zSql++;.     
2b7fe 20 20 20 77 68 69 6c 65 28 20 2a 7a 53 71 6c 20     while( *zSql 
2b7ff 26 26 20 2a 7a 53 71 6c 21 3d 27 5d 27 20 29 7b  && *zSql!=']' ){
2b800 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20 20 20   zSql++; }.     
2b801 20 20 20 69 66 28 20 2a 7a 53 71 6c 3d 3d 30 20     if( *zSql==0 
2b802 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
2b803 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54      token = tkOT
2b804 48 45 52 3b 0a 20 20 20 20 20 20 20 20 62 72 65  HER;.        bre
2b805 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
2b806 20 20 63 61 73 65 20 27 60 27 3a 20 20 20 20 20    case '`':     
2b807 2f 2a 20 47 72 61 76 65 2d 61 63 63 65 6e 74 20  /* Grave-accent 
2b808 71 75 6f 74 65 64 20 73 79 6d 62 6f 6c 73 20 75  quoted symbols u
2b809 73 65 64 20 62 79 20 4d 79 53 51 4c 20 2a 2f 0a  sed by MySQL */.
2b80a 20 20 20 20 20 20 63 61 73 65 20 27 22 27 3a 20        case '"': 
2b80b 20 20 20 20 2f 2a 20 73 69 6e 67 6c 65 2d 20 61      /* single- a
2b80c 6e 64 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64  nd double-quoted
2b80d 20 73 74 72 69 6e 67 73 20 2a 2f 0a 20 20 20 20   strings */.    
2b80e 20 20 63 61 73 65 20 27 5c 27 27 3a 20 7b 0a 20    case '\'': {. 
2b80f 20 20 20 20 20 20 20 69 6e 74 20 63 20 3d 20 2a         int c = *
2b810 7a 53 71 6c 3b 0a 20 20 20 20 20 20 20 20 7a 53  zSql;.        zS
2b811 71 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77 68  ql++;.        wh
2b812 69 6c 65 28 20 2a 7a 53 71 6c 20 26 26 20 2a 7a  ile( *zSql && *z
2b813 53 71 6c 21 3d 63 20 29 7b 20 7a 53 71 6c 2b 2b  Sql!=c ){ zSql++
2b814 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ; }.        if( 
2b815 2a 7a 53 71 6c 3d 3d 30 20 29 20 72 65 74 75 72  *zSql==0 ) retur
2b816 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 74 6f 6b  n 0;.        tok
2b817 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20  en = tkOTHER;.  
2b818 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2b819 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75     }.      defau
2b81a 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 6e  lt: {.        in
2b81b 74 20 63 3b 0a 20 20 20 20 20 20 20 20 69 66 28  t c;.        if(
2b81c 20 49 64 43 68 61 72 28 28 75 38 29 2a 7a 53 71   IdChar((u8)*zSq
2b81d 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  l) ){.          
2b81e 2f 2a 20 4b 65 79 77 6f 72 64 73 20 61 6e 64 20  /* Keywords and 
2b81f 75 6e 71 75 6f 74 65 64 20 69 64 65 6e 74 69 66  unquoted identif
2b820 69 65 72 73 20 2a 2f 0a 20 20 20 20 20 20 20 20  iers */.        
2b821 20 20 69 6e 74 20 6e 49 64 3b 0a 20 20 20 20 20    int nId;.     
2b822 20 20 20 20 20 66 6f 72 28 6e 49 64 3d 31 3b 20       for(nId=1; 
2b823 49 64 43 68 61 72 28 7a 53 71 6c 5b 6e 49 64 5d  IdChar(zSql[nId]
2b824 29 3b 20 6e 49 64 2b 2b 29 7b 7d 0a 23 69 66 64  ); nId++){}.#ifd
2b825 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
2b826 52 49 47 47 45 52 0a 20 20 20 20 20 20 20 20 20  RIGGER.         
2b827 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52   token = tkOTHER
2b828 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20  ;.#else.        
2b829 20 20 73 77 69 74 63 68 28 20 2a 7a 53 71 6c 20    switch( *zSql 
2b82a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63  ){.            c
2b82b 61 73 65 20 27 63 27 3a 20 63 61 73 65 20 27 43  ase 'c': case 'C
2b82c 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ': {.           
2b82d 20 20 20 69 66 28 20 6e 49 64 3d 3d 36 20 26 26     if( nId==6 &&
2b82e 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
2b82f 28 7a 53 71 6c 2c 20 22 63 72 65 61 74 65 22 2c  (zSql, "create",
2b830 20 36 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20   6)==0 ){.      
2b831 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20            token 
2b832 3d 20 74 6b 43 52 45 41 54 45 3b 0a 20 20 20 20  = tkCREATE;.    
2b833 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
2b834 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2b835 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52   token = tkOTHER
2b836 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
2b837 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }.              
2b838 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
2b839 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
2b83a 20 63 61 73 65 20 27 74 27 3a 20 63 61 73 65 20   case 't': case 
2b83b 27 54 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20  'T': {.         
2b83c 20 20 20 20 20 69 66 28 20 6e 49 64 3d 3d 37 20       if( nId==7 
2b83d 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  && sqlite3StrNIC
2b83e 6d 70 28 7a 53 71 6c 2c 20 22 74 72 69 67 67 65  mp(zSql, "trigge
2b83f 72 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 20 20  r", 7)==0 ){.   
2b840 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b               tok
2b841 65 6e 20 3d 20 74 6b 54 52 49 47 47 45 52 3b 0a  en = tkTRIGGER;.
2b842 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 65                }e
2b843 6c 73 65 20 69 66 28 20 6e 49 64 3d 3d 34 20 26  lse if( nId==4 &
2b844 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  & sqlite3StrNICm
2b845 70 28 7a 53 71 6c 2c 20 22 74 65 6d 70 22 2c 20  p(zSql, "temp", 
2b846 34 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  4)==0 ){.       
2b847 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d           token =
2b848 20 74 6b 54 45 4d 50 3b 0a 20 20 20 20 20 20 20   tkTEMP;.       
2b849 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
2b84a 20 6e 49 64 3d 3d 39 20 26 26 20 73 71 6c 69 74   nId==9 && sqlit
2b84b 65 33 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c  e3StrNICmp(zSql,
2b84c 20 22 74 65 6d 70 6f 72 61 72 79 22 2c 20 39 29   "temporary", 9)
2b84d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
2b84e 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
2b84f 6b 54 45 4d 50 3b 0a 20 20 20 20 20 20 20 20 20  kTEMP;.         
2b850 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2b851 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65              toke
2b852 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20  n = tkOTHER;.   
2b853 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
2b854 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
2b855 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
2b856 20 20 20 20 20 20 20 20 20 20 20 20 63 61 73 65              case
2b857 20 27 65 27 3a 20 20 63 61 73 65 20 27 45 27 3a   'e':  case 'E':
2b858 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   {.             
2b859 20 69 66 28 20 6e 49 64 3d 3d 33 20 26 26 20 73   if( nId==3 && s
2b85a 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a  qlite3StrNICmp(z
2b85b 53 71 6c 2c 20 22 65 6e 64 22 2c 20 33 29 3d 3d  Sql, "end", 3)==
2b85c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
2b85d 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 45       token = tkE
2b85e 4e 44 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ND;.            
2b85f 20 20 7d 65 6c 73 65 0a 23 69 66 6e 64 65 66 20    }else.#ifndef 
2b860 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c  SQLITE_OMIT_EXPL
2b861 41 49 4e 0a 20 20 20 20 20 20 20 20 20 20 20 20  AIN.            
2b862 20 20 69 66 28 20 6e 49 64 3d 3d 37 20 26 26 20    if( nId==7 && 
2b863 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
2b864 7a 53 71 6c 2c 20 22 65 78 70 6c 61 69 6e 22 2c  zSql, "explain",
2b865 20 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20   7)==0 ){.      
2b866 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20            token 
2b867 3d 20 74 6b 45 58 50 4c 41 49 4e 3b 0a 20 20 20  = tkEXPLAIN;.   
2b868 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
2b869 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20  .#endif.        
2b86a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20        {.        
2b86b 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
2b86c 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20  tkOTHER;.       
2b86d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2b86e 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2b86f 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2b870 20 20 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a          default:
2b871 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   {.             
2b872 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52   token = tkOTHER
2b873 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
2b874 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
2b875 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d     }.          }
2b876 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2b877 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a  E_OMIT_TRIGGER *
2b878 2f 0a 20 20 20 20 20 20 20 20 20 20 7a 53 71 6c  /.          zSql
2b879 20 2b 3d 20 6e 49 64 2d 31 3b 0a 20 20 20 20 20   += nId-1;.     
2b87a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2b87b 20 20 20 20 2f 2a 20 4f 70 65 72 61 74 6f 72 73      /* Operators
2b87c 20 61 6e 64 20 73 70 65 63 69 61 6c 20 73 79 6d   and special sym
2b87d 62 6f 6c 73 20 2a 2f 0a 20 20 20 20 20 20 20 20  bols */.        
2b87e 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45    token = tkOTHE
2b87f 52 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  R;.        }.   
2b880 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2b881 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 74    }.    }.    st
2b882 61 74 65 20 3d 20 74 72 61 6e 73 5b 73 74 61 74  ate = trans[stat
2b883 65 5d 5b 74 6f 6b 65 6e 5d 3b 0a 20 20 20 20 7a  e][token];.    z
2b884 53 71 6c 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74  Sql++;.  }.  ret
2b885 75 72 6e 20 73 74 61 74 65 3d 3d 30 3b 0a 7d 0a  urn state==0;.}.
2b886 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2b887 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a  OMIT_UTF16./*.**
2b888 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
2b889 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
2b88a 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
2b88b 65 28 29 20 72 6f 75 74 69 6e 65 20 64 65 73 63  e() routine desc
2b88c 72 69 62 65 64 0a 2a 2a 20 61 62 6f 76 65 2c 20  ribed.** above, 
2b88d 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20  except that the 
2b88e 70 61 72 61 6d 65 74 65 72 20 69 73 20 72 65 71  parameter is req
2b88f 75 69 72 65 64 20 74 6f 20 62 65 20 55 54 46 2d  uired to be UTF-
2b890 31 36 20 65 6e 63 6f 64 65 64 2c 20 6e 6f 74 0a  16 encoded, not.
2b891 2a 2a 20 55 54 46 2d 38 2e 0a 2a 2f 0a 53 51 4c  ** UTF-8..*/.SQL
2b892 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2b893 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 63  te3_complete16(c
2b894 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 29  onst void *zSql)
2b895 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  {.  sqlite3_valu
2b896 65 20 2a 70 56 61 6c 3b 0a 20 20 63 68 61 72 20  e *pVal;.  char 
2b897 63 6f 6e 73 74 20 2a 7a 53 71 6c 38 3b 0a 20 20  const *zSql8;.  
2b898 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
2b899 4e 4f 4d 45 4d 3b 0a 0a 23 69 66 6e 64 65 66 20  NOMEM;..#ifndef 
2b89a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
2b89b 49 4e 49 54 0a 20 20 72 63 20 3d 20 73 71 6c 69  INIT.  rc = sqli
2b89c 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
2b89d 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74  ;.  if( rc ) ret
2b89e 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20  urn rc;.#endif. 
2b89f 20 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56   pVal = sqlite3V
2b8a0 61 6c 75 65 4e 65 77 28 30 29 3b 0a 20 20 73 71  alueNew(0);.  sq
2b8a1 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72  lite3ValueSetStr
2b8a2 28 70 56 61 6c 2c 20 2d 31 2c 20 7a 53 71 6c 2c  (pVal, -1, zSql,
2b8a3 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
2b8a4 49 56 45 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  IVE, SQLITE_STAT
2b8a5 49 43 29 3b 0a 20 20 7a 53 71 6c 38 20 3d 20 73  IC);.  zSql8 = s
2b8a6 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28  qlite3ValueText(
2b8a7 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46  pVal, SQLITE_UTF
2b8a8 38 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 38 20  8);.  if( zSql8 
2b8a9 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
2b8aa 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 7a 53 71  te3_complete(zSq
2b8ab 6c 38 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  l8);.  }else{.  
2b8ac 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
2b8ad 4d 45 4d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  MEM;.  }.  sqlit
2b8ae 65 33 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c  e3ValueFree(pVal
2b8af 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  );.  return sqli
2b8b0 74 65 33 41 70 69 45 78 69 74 28 30 2c 20 72 63  te3ApiExit(0, rc
2b8b1 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  );.}.#endif /* S
2b8b2 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
2b8b3 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51   */.#endif /* SQ
2b8b4 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4c 45  LITE_OMIT_COMPLE
2b8b5 54 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  TE */../********
2b8b6 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 63 6f  ****** End of co
2b8b7 6d 70 6c 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  mplete.c *******
2b8b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b8b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b8ba 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
2b8bb 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
2b8bc 65 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a  e main.c *******
2b8bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b8be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b8bf 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
2b8c0 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a  1 September 15.*
2b8c1 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
2b8c2 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
2b8c3 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
2b8c4 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
2b8c5 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
2b8c6 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
2b8c7 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
2b8c8 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
2b8c9 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
2b8ca 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
2b8cb 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
2b8cc 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
2b8cd 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
2b8ce 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
2b8cf 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
2b8d0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
2b8d1 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
2b8d2 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
2b8d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b8d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b8d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b8d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
2b8d7 20 4d 61 69 6e 20 66 69 6c 65 20 66 6f 72 20 74   Main file for t
2b8d8 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72  he SQLite librar
2b8d9 79 2e 20 20 54 68 65 20 72 6f 75 74 69 6e 65 73  y.  The routines
2b8da 20 69 6e 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a   in this file.**
2b8db 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 70   implement the p
2b8dc 72 6f 67 72 61 6d 6d 65 72 20 69 6e 74 65 72 66  rogrammer interf
2b8dd 61 63 65 20 74 6f 20 74 68 65 20 6c 69 62 72 61  ace to the libra
2b8de 72 79 2e 20 20 52 6f 75 74 69 6e 65 73 20 69 6e  ry.  Routines in
2b8df 0a 2a 2a 20 6f 74 68 65 72 20 66 69 6c 65 73 20  .** other files 
2b8e0 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c  are for internal
2b8e1 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 20 61   use by SQLite a
2b8e2 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65  nd should not be
2b8e3 0a 2a 2a 20 61 63 63 65 73 73 65 64 20 62 79 20  .** accessed by 
2b8e4 75 73 65 72 73 20 6f 66 20 74 68 65 20 6c 69 62  users of the lib
2b8e5 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  rary..**.** $Id:
2b8e6 20 6d 61 69 6e 2e 63 2c 76 20 31 2e 35 31 30 20   main.c,v 1.510 
2b8e7 32 30 30 38 2f 31 31 2f 30 34 20 31 33 3a 34 36  2008/11/04 13:46
2b8e8 3a 32 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :28 drh Exp $.*/
2b8e9 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
2b8ea 45 4e 41 42 4c 45 5f 46 54 53 33 0a 2f 2a 2a 2a  ENABLE_FTS3./***
2b8eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
2b8ec 75 64 65 20 66 74 73 33 2e 68 20 69 6e 20 74 68  ude fts3.h in th
2b8ed 65 20 6d 69 64 64 6c 65 20 6f 66 20 6d 61 69 6e  e middle of main
2b8ee 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
2b8ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
2b8f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
2b8f1 6e 20 66 69 6c 65 20 66 74 73 33 2e 68 20 2a 2a  n file fts3.h **
2b8f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b8f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b8f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
2b8f5 2a 20 32 30 30 36 20 4f 63 74 20 31 30 0a 2a 2a  * 2006 Oct 10.**
2b8f6 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
2b8f7 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
2b8f8 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
2b8f9 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
2b8fa 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
2b8fb 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
2b8fc 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
2b8fd 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
2b8fe 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
2b8ff 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
2b900 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
2b901 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
2b902 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
2b903 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
2b904 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
2b905 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
2b906 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
2b907 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
2b908 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b909 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b90a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b90b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b90c 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61  *.**.** This hea
2b90d 64 65 72 20 66 69 6c 65 20 69 73 20 75 73 65 64  der file is used
2b90e 20 62 79 20 70 72 6f 67 72 61 6d 73 20 74 68 61   by programs tha
2b90f 74 20 77 61 6e 74 20 74 6f 20 6c 69 6e 6b 20 61  t want to link a
2b910 67 61 69 6e 73 74 20 74 68 65 0a 2a 2a 20 46 54  gainst the.** FT
2b911 53 33 20 6c 69 62 72 61 72 79 2e 20 20 41 6c 6c  S3 library.  All
2b912 20 69 74 20 64 6f 65 73 20 69 73 20 64 65 63 6c   it does is decl
2b913 61 72 65 20 74 68 65 20 73 71 6c 69 74 65 33 46  are the sqlite3F
2b914 74 73 33 49 6e 69 74 28 29 20 69 6e 74 65 72 66  ts3Init() interf
2b915 61 63 65 2e 0a 2a 2f 0a 0a 23 69 66 20 30 0a 65  ace..*/..#if 0.e
2b916 78 74 65 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64  xtern "C" {.#end
2b917 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c  if  /* __cpluspl
2b918 75 73 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52  us */..SQLITE_PR
2b919 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2b91a 33 46 74 73 33 49 6e 69 74 28 73 71 6c 69 74 65  3Fts3Init(sqlite
2b91b 33 20 2a 64 62 29 3b 0a 0a 23 69 66 20 30 0a 7d  3 *db);..#if 0.}
2b91c 20 20 2f 2a 20 65 78 74 65 72 6e 20 22 43 22 20    /* extern "C" 
2b91d 2a 2f 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f  */.#endif  /* __
2b91e 63 70 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a 2f 2a  cplusplus */../*
2b91f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
2b920 64 20 6f 66 20 66 74 73 33 2e 68 20 2a 2a 2a 2a  d of fts3.h ****
2b921 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b922 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b923 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2b924 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f  ************* Co
2b925 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77  ntinuing where w
2b926 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6d 61  e left off in ma
2b927 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  in.c ***********
2b928 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65  ************/.#e
2b929 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
2b92a 54 45 5f 45 4e 41 42 4c 45 5f 52 54 52 45 45 0a  TE_ENABLE_RTREE.
2b92b 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2b92c 49 6e 63 6c 75 64 65 20 72 74 72 65 65 2e 68 20  Include rtree.h 
2b92d 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
2b92e 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   main.c ********
2b92f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2b930 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2b931 42 65 67 69 6e 20 66 69 6c 65 20 72 74 72 65 65  Begin file rtree
2b932 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
2b933 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b934 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2b935 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32  /*.** 2008 May 2
2b936 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  6.**.** The auth
2b937 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
2b938 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
2b939 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
2b93a 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
2b93b 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
2b93c 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
2b93d 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
2b93e 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
2b93f 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
2b940 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
2b941 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
2b942 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
2b943 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
2b944 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
2b945 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
2b946 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
2b947 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
2b948 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b949 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b94a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b94b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b94c 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
2b94d 20 68 65 61 64 65 72 20 66 69 6c 65 20 69 73 20   header file is 
2b94e 75 73 65 64 20 62 79 20 70 72 6f 67 72 61 6d 73  used by programs
2b94f 20 74 68 61 74 20 77 61 6e 74 20 74 6f 20 6c 69   that want to li
2b950 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 65 0a 2a  nk against the.*
2b951 2a 20 52 54 52 45 45 20 6c 69 62 72 61 72 79 2e  * RTREE library.
2b952 20 20 41 6c 6c 20 69 74 20 64 6f 65 73 20 69 73    All it does is
2b953 20 64 65 63 6c 61 72 65 20 74 68 65 20 73 71 6c   declare the sql
2b954 69 74 65 33 52 74 72 65 65 49 6e 69 74 28 29 20  ite3RtreeInit() 
2b955 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 0a 23  interface..*/..#
2b956 69 66 20 30 0a 65 78 74 65 72 6e 20 22 43 22 20  if 0.extern "C" 
2b957 7b 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63  {.#endif  /* __c
2b958 70 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a 53 51 4c  plusplus */..SQL
2b959 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2b95a 73 71 6c 69 74 65 33 52 74 72 65 65 49 6e 69 74  sqlite3RtreeInit
2b95b 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 0a  (sqlite3 *db);..
2b95c 23 69 66 20 30 0a 7d 20 20 2f 2a 20 65 78 74 65  #if 0.}  /* exte
2b95d 72 6e 20 22 43 22 20 2a 2f 0a 23 65 6e 64 69 66  rn "C" */.#endif
2b95e 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73    /* __cplusplus
2b95f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
2b960 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 74 72 65  **** End of rtre
2b961 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
2b962 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b963 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b964 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
2b965 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
2b966 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
2b967 66 20 69 6e 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a  f in main.c ****
2b968 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b969 2a 2a 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64  ***/.#endif.#ifd
2b96a 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
2b96b 5f 49 43 55 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  _ICU./**********
2b96c 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c  **** Include sql
2b96d 69 74 65 69 63 75 2e 68 20 69 6e 20 74 68 65 20  iteicu.h in the 
2b96e 6d 69 64 64 6c 65 20 6f 66 20 6d 61 69 6e 2e 63  middle of main.c
2b96f 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2b970 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
2b971 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
2b972 73 71 6c 69 74 65 69 63 75 2e 68 20 2a 2a 2a 2a  sqliteicu.h ****
2b973 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b974 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b975 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20  ***/./*.** 2008 
2b976 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65  May 26.**.** The
2b977 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
2b978 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
2b979 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
2b97a 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
2b97b 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
2b97c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
2b97d 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
2b97e 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
2b97f 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
2b980 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
2b981 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
2b982 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
2b983 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
2b984 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
2b985 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
2b986 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
2b987 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
2b988 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b989 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b98a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b98b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b98c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
2b98d 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c   This header fil
2b98e 65 20 69 73 20 75 73 65 64 20 62 79 20 70 72 6f  e is used by pro
2b98f 67 72 61 6d 73 20 74 68 61 74 20 77 61 6e 74 20  grams that want 
2b990 74 6f 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20  to link against 
2b991 74 68 65 0a 2a 2a 20 49 43 55 20 65 78 74 65 6e  the.** ICU exten
2b992 73 69 6f 6e 2e 20 20 41 6c 6c 20 69 74 20 64 6f  sion.  All it do
2b993 65 73 20 69 73 20 64 65 63 6c 61 72 65 20 74 68  es is declare th
2b994 65 20 73 71 6c 69 74 65 33 49 63 75 49 6e 69 74  e sqlite3IcuInit
2b995 28 29 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f  () interface..*/
2b996 0a 0a 23 69 66 20 30 0a 65 78 74 65 72 6e 20 22  ..#if 0.extern "
2b997 43 22 20 7b 0a 23 65 6e 64 69 66 20 20 2f 2a 20  C" {.#endif  /* 
2b998 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a  __cplusplus */..
2b999 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2b99a 6e 74 20 73 71 6c 69 74 65 33 49 63 75 49 6e 69  nt sqlite3IcuIni
2b99b 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a  t(sqlite3 *db);.
2b99c 0a 23 69 66 20 30 0a 7d 20 20 2f 2a 20 65 78 74  .#if 0.}  /* ext
2b99d 65 72 6e 20 22 43 22 20 2a 2f 0a 23 65 6e 64 69  ern "C" */.#endi
2b99e 66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75  f  /* __cplusplu
2b99f 73 20 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  s */.../********
2b9a0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71  ****** End of sq
2b9a1 6c 69 74 65 69 63 75 2e 68 20 2a 2a 2a 2a 2a 2a  liteicu.h ******
2b9a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b9a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b9a4 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
2b9a5 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
2b9a6 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
2b9a7 6f 66 66 20 69 6e 20 6d 61 69 6e 2e 63 20 2a 2a  off in main.c **
2b9a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b9a9 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f  *****/.#endif../
2b9aa 2a 0a 2a 2a 20 54 68 65 20 76 65 72 73 69 6f 6e  *.** The version
2b9ab 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 0a   of the library.
2b9ac 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
2b9ad 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74 65 33  nst char sqlite3
2b9ae 5f 76 65 72 73 69 6f 6e 5b 5d 20 3d 20 53 51 4c  _version[] = SQL
2b9af 49 54 45 5f 56 45 52 53 49 4f 4e 3b 0a 53 51 4c  ITE_VERSION;.SQL
2b9b0 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
2b9b1 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c 69 62 76  ar *sqlite3_libv
2b9b2 65 72 73 69 6f 6e 28 76 6f 69 64 29 7b 20 72 65  ersion(void){ re
2b9b3 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 76 65 72  turn sqlite3_ver
2b9b4 73 69 6f 6e 3b 20 7d 0a 53 51 4c 49 54 45 5f 41  sion; }.SQLITE_A
2b9b5 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c  PI int sqlite3_l
2b9b6 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72  ibversion_number
2b9b7 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53  (void){ return S
2b9b8 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55  QLITE_VERSION_NU
2b9b9 4d 42 45 52 3b 20 7d 0a 53 51 4c 49 54 45 5f 41  MBER; }.SQLITE_A
2b9ba 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74  PI int sqlite3_t
2b9bb 68 72 65 61 64 73 61 66 65 28 76 6f 69 64 29 7b  hreadsafe(void){
2b9bc 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 54   return SQLITE_T
2b9bd 48 52 45 41 44 53 41 46 45 3b 20 7d 0a 0a 23 69  HREADSAFE; }..#i
2b9be 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
2b9bf 45 5f 4f 4d 49 54 5f 54 52 41 43 45 29 20 26 26  E_OMIT_TRACE) &&
2b9c0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
2b9c1 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 29 0a  ENABLE_IOTRACE).
2b9c2 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c  /*.** If the fol
2b9c3 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  lowing function 
2b9c4 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74 20 4e  pointer is not N
2b9c5 55 4c 4c 20 61 6e 64 20 69 66 0a 2a 2a 20 53 51  ULL and if.** SQ
2b9c6 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52  LITE_ENABLE_IOTR
2b9c7 41 43 45 20 69 73 20 65 6e 61 62 6c 65 64 2c 20  ACE is enabled, 
2b9c8 74 68 65 6e 20 6d 65 73 73 61 67 65 73 20 64 65  then messages de
2b9c9 73 63 72 69 62 69 6e 67 0a 2a 2a 20 49 2f 4f 20  scribing.** I/O 
2b9ca 61 63 74 69 76 65 20 61 72 65 20 77 72 69 74 74  active are writt
2b9cb 65 6e 20 75 73 69 6e 67 20 74 68 69 73 20 66 75  en using this fu
2b9cc 6e 63 74 69 6f 6e 2e 20 20 54 68 65 73 65 20 6d  nction.  These m
2b9cd 65 73 73 61 67 65 73 0a 2a 2a 20 61 72 65 20 69  essages.** are i
2b9ce 6e 74 65 6e 64 65 64 20 66 6f 72 20 64 65 62 75  ntended for debu
2b9cf 67 67 69 6e 67 20 61 63 74 69 76 69 74 79 20 6f  gging activity o
2b9d0 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  nly..*/.SQLITE_P
2b9d1 52 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73 71  RIVATE void (*sq
2b9d2 6c 69 74 65 33 49 6f 54 72 61 63 65 29 28 63 6f  lite3IoTrace)(co
2b9d3 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 20  nst char*, ...) 
2b9d4 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  = 0;.#endif../*.
2b9d5 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ** If the follow
2b9d6 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61  ing global varia
2b9d7 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  ble points to a 
2b9d8 73 74 72 69 6e 67 20 77 68 69 63 68 20 69 73 20  string which is 
2b9d9 74 68 65 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 61  the.** name of a
2b9da 20 64 69 72 65 63 74 6f 72 79 2c 20 74 68 65 6e   directory, then
2b9db 20 74 68 61 74 20 64 69 72 65 63 74 6f 72 79 20   that directory 
2b9dc 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74 6f 20  will be used to 
2b9dd 73 74 6f 72 65 0a 2a 2a 20 74 65 6d 70 6f 72 61  store.** tempora
2b9de 72 79 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  ry files..**.** 
2b9df 53 65 65 20 61 6c 73 6f 20 74 68 65 20 22 50 52  See also the "PR
2b9e0 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 5f  AGMA temp_store_
2b9e1 64 69 72 65 63 74 6f 72 79 22 20 53 51 4c 20 63  directory" SQL c
2b9e2 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54  ommand..*/.SQLIT
2b9e3 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69  E_API char *sqli
2b9e4 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f  te3_temp_directo
2b9e5 72 79 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 49  ry = 0;../*.** I
2b9e6 6e 69 74 69 61 6c 69 7a 65 20 53 51 4c 69 74 65  nitialize SQLite
2b9e7 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  .  .**.** This r
2b9e8 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63  outine must be c
2b9e9 61 6c 6c 65 64 20 74 6f 20 69 6e 69 74 69 61 6c  alled to initial
2b9ea 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61  ize the memory a
2b9eb 6c 6c 6f 63 61 74 69 6f 6e 2c 0a 2a 2a 20 56 46  llocation,.** VF
2b9ec 53 2c 20 61 6e 64 20 6d 75 74 65 78 20 73 75 62  S, and mutex sub
2b9ed 73 79 73 74 65 6d 73 20 70 72 69 6f 72 20 74 6f  systems prior to
2b9ee 20 64 6f 69 6e 67 20 61 6e 79 20 73 65 72 69 6f   doing any serio
2b9ef 75 73 20 77 6f 72 6b 20 77 69 74 68 0a 2a 2a 20  us work with.** 
2b9f0 53 51 4c 69 74 65 2e 20 20 42 75 74 20 61 73 20  SQLite.  But as 
2b9f1 6c 6f 6e 67 20 61 73 20 79 6f 75 20 64 6f 20 6e  long as you do n
2b9f2 6f 74 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20  ot compile with 
2b9f3 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
2b9f4 49 4e 49 54 0a 2a 2a 20 74 68 69 73 20 72 6f 75  INIT.** this rou
2b9f5 74 69 6e 65 20 77 69 6c 6c 20 62 65 20 63 61 6c  tine will be cal
2b9f6 6c 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  led automaticall
2b9f7 79 20 62 79 20 6b 65 79 20 72 6f 75 74 69 6e 65  y by key routine
2b9f8 73 20 73 75 63 68 20 61 73 0a 2a 2a 20 73 71 6c  s such as.** sql
2b9f9 69 74 65 33 5f 6f 70 65 6e 28 29 2e 20 20 0a 2a  ite3_open().  .*
2b9fa 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2b9fb 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 65 78 63  e is a no-op exc
2b9fc 65 70 74 20 6f 6e 20 69 74 73 20 76 65 72 79 20  ept on its very 
2b9fd 66 69 72 73 74 20 63 61 6c 6c 20 66 6f 72 20 74  first call for t
2b9fe 68 65 20 70 72 6f 63 65 73 73 2c 0a 2a 2a 20 6f  he process,.** o
2b9ff 72 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20  r for the first 
2ba00 63 61 6c 6c 20 61 66 74 65 72 20 61 20 63 61 6c  call after a cal
2ba01 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75  l to sqlite3_shu
2ba02 74 64 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tdown..**.** The
2ba03 20 66 69 72 73 74 20 74 68 72 65 61 64 20 74 6f   first thread to
2ba04 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69   call this routi
2ba05 6e 65 20 72 75 6e 73 20 74 68 65 20 69 6e 69 74  ne runs the init
2ba06 69 61 6c 69 7a 61 74 69 6f 6e 20 74 6f 0a 2a 2a  ialization to.**
2ba07 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e 20 20 49 66   completion.  If
2ba08 20 73 75 62 73 65 71 75 65 6e 74 20 74 68 72 65   subsequent thre
2ba09 61 64 73 20 63 61 6c 6c 20 74 68 69 73 20 72 6f  ads call this ro
2ba0a 75 74 69 6e 65 20 62 65 66 6f 72 65 20 74 68 65  utine before the
2ba0b 20 66 69 72 73 74 0a 2a 2a 20 74 68 72 65 61 64   first.** thread
2ba0c 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 74 68   has finished th
2ba0d 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  e initialization
2ba0e 20 70 72 6f 63 65 73 73 2c 20 74 68 65 6e 20 74   process, then t
2ba0f 68 65 20 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a  he subsequent.**
2ba10 20 74 68 72 65 61 64 73 20 6d 75 73 74 20 62 6c   threads must bl
2ba11 6f 63 6b 20 75 6e 74 69 6c 20 74 68 65 20 66 69  ock until the fi
2ba12 72 73 74 20 74 68 72 65 61 64 20 66 69 6e 69 73  rst thread finis
2ba13 68 65 73 20 77 69 74 68 20 74 68 65 20 69 6e 69  hes with the ini
2ba14 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 0a 2a 2a 0a  tialization..**.
2ba15 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 68 72  ** The first thr
2ba16 65 61 64 20 6d 69 67 68 74 20 63 61 6c 6c 20 74  ead might call t
2ba17 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 63 75  his routine recu
2ba18 72 73 69 76 65 6c 79 2e 20 20 52 65 63 75 72 73  rsively.  Recurs
2ba19 69 76 65 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20  ive.** calls to 
2ba1a 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f  this routine sho
2ba1b 75 6c 64 20 6e 6f 74 20 62 6c 6f 63 6b 2c 20 6f  uld not block, o
2ba1c 66 20 63 6f 75 72 73 65 2e 20 20 4f 74 68 65 72  f course.  Other
2ba1d 77 69 73 65 20 74 68 65 0a 2a 2a 20 69 6e 69 74  wise the.** init
2ba1e 69 61 6c 69 7a 61 74 69 6f 6e 20 70 72 6f 63 65  ialization proce
2ba1f 73 73 20 77 6f 75 6c 64 20 6e 65 76 65 72 20 63  ss would never c
2ba20 6f 6d 70 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 4c  omplete..**.** L
2ba21 65 74 20 58 20 62 65 20 74 68 65 20 66 69 72 73  et X be the firs
2ba22 74 20 74 68 72 65 61 64 20 74 6f 20 65 6e 74 65  t thread to ente
2ba23 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20  r this routine. 
2ba24 20 4c 65 74 20 59 20 62 65 20 73 6f 6d 65 20 6f   Let Y be some o
2ba25 74 68 65 72 0a 2a 2a 20 74 68 72 65 61 64 2e 20  ther.** thread. 
2ba26 20 54 68 65 6e 20 77 68 69 6c 65 20 74 68 65 20   Then while the 
2ba27 69 6e 69 74 69 61 6c 20 69 6e 76 6f 63 61 74 69  initial invocati
2ba28 6f 6e 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69  on of this routi
2ba29 6e 65 20 62 79 20 58 20 69 73 0a 2a 2a 20 69 6e  ne by X is.** in
2ba2a 63 6f 6d 70 6c 65 74 65 2c 20 69 74 20 69 73 20  complete, it is 
2ba2b 72 65 71 75 69 72 65 64 20 74 68 61 74 3a 0a 2a  required that:.*
2ba2c 2a 0a 2a 2a 20 20 20 20 2a 20 20 43 61 6c 6c 73  *.**    *  Calls
2ba2d 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65   to this routine
2ba2e 20 66 72 6f 6d 20 59 20 6d 75 73 74 20 62 6c 6f   from Y must blo
2ba2f 63 6b 20 75 6e 74 69 6c 20 74 68 65 20 6f 75 74  ck until the out
2ba30 65 72 2d 6d 6f 73 74 0a 2a 2a 20 20 20 20 20 20  er-most.**      
2ba31 20 63 61 6c 6c 20 62 79 20 58 20 63 6f 6d 70 6c   call by X compl
2ba32 65 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a  etes..**.**    *
2ba33 20 20 52 65 63 75 72 73 69 76 65 20 63 61 6c 6c    Recursive call
2ba34 73 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  s to this routin
2ba35 65 20 66 72 6f 6d 20 74 68 72 65 61 64 20 58 20  e from thread X 
2ba36 72 65 74 75 72 6e 20 69 6d 6d 65 64 69 61 74 65  return immediate
2ba37 6c 79 0a 2a 2a 20 20 20 20 20 20 20 77 69 74 68  ly.**       with
2ba38 6f 75 74 20 62 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f  out blocking..*/
2ba39 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2ba3a 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
2ba3b 7a 65 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69  ze(void){.  sqli
2ba3c 74 65 33 5f 6d 75 74 65 78 20 2a 70 4d 61 73 74  te3_mutex *pMast
2ba3d 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  er;             
2ba3e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2ba3f 6d 61 69 6e 20 73 74 61 74 69 63 20 6d 75 74 65  main static mute
2ba40 78 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  x */.  int rc;  
2ba41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ba42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ba43 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f      /* Result co
2ba44 64 65 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51  de */..#ifdef SQ
2ba45 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 20 20  LITE_OMIT_WSD.  
2ba46 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 77 73 64  rc = sqlite3_wsd
2ba47 5f 69 6e 69 74 28 34 30 39 36 2c 20 32 34 29 3b  _init(4096, 24);
2ba48 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
2ba49 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
2ba4a 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69  rn rc;.  }.#endi
2ba4b 66 0a 0a 20 20 2f 2a 20 49 66 20 53 51 4c 69 74  f..  /* If SQLit
2ba4c 65 20 69 73 20 61 6c 72 65 61 64 79 20 63 6f 6d  e is already com
2ba4d 70 6c 65 74 65 6c 79 20 69 6e 69 74 69 61 6c 69  pletely initiali
2ba4e 7a 65 64 2c 20 74 68 65 6e 20 74 68 69 73 20 63  zed, then this c
2ba4f 61 6c 6c 0a 20 20 2a 2a 20 74 6f 20 73 71 6c 69  all.  ** to sqli
2ba50 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
2ba51 20 73 68 6f 75 6c 64 20 62 65 20 61 20 6e 6f 2d   should be a no-
2ba52 6f 70 2e 20 20 42 75 74 20 74 68 65 20 69 6e 69  op.  But the ini
2ba53 74 69 61 6c 69 7a 61 74 69 6f 6e 0a 20 20 2a 2a  tialization.  **
2ba54 20 6d 75 73 74 20 62 65 20 63 6f 6d 70 6c 65 74   must be complet
2ba55 65 2e 20 20 53 6f 20 69 73 49 6e 69 74 20 6d 75  e.  So isInit mu
2ba56 73 74 20 6e 6f 74 20 62 65 20 73 65 74 20 75 6e  st not be set un
2ba57 74 69 6c 20 74 68 65 20 76 65 72 79 20 65 6e 64  til the very end
2ba58 0a 20 20 2a 2a 20 6f 66 20 74 68 69 73 20 72 6f  .  ** of this ro
2ba59 75 74 69 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  utine..  */.  if
2ba5a 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
2ba5b 6f 6e 66 69 67 2e 69 73 49 6e 69 74 20 29 20 72  onfig.isInit ) r
2ba5c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2ba5d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
2ba5e 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79   the mutex subsy
2ba5f 73 74 65 6d 20 69 73 20 69 6e 69 74 69 61 6c 69  stem is initiali
2ba60 7a 65 64 2e 20 20 49 66 20 75 6e 61 62 6c 65 20  zed.  If unable 
2ba61 74 6f 20 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c  to .  ** initial
2ba62 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73 75  ize the mutex su
2ba63 62 73 79 73 74 65 6d 2c 20 72 65 74 75 72 6e 20  bsystem, return 
2ba64 65 61 72 6c 79 20 77 69 74 68 20 74 68 65 20 65  early with the e
2ba65 72 72 6f 72 2e 0a 20 20 2a 2a 20 49 66 20 74 68  rror..  ** If th
2ba66 65 20 73 79 73 74 65 6d 20 69 73 20 73 6f 20 73  e system is so s
2ba67 69 63 6b 20 74 68 61 74 20 77 65 20 61 72 65 20  ick that we are 
2ba68 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61  unable to alloca
2ba69 74 65 20 61 20 6d 75 74 65 78 2c 0a 20 20 2a 2a  te a mutex,.  **
2ba6a 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 6d 75   there is not mu
2ba6b 63 68 20 53 51 4c 69 74 65 20 69 73 20 67 6f 69  ch SQLite is goi
2ba6c 6e 67 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f  ng to be able to
2ba6d 20 64 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54   do..  **.  ** T
2ba6e 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74  he mutex subsyst
2ba6f 65 6d 20 6d 75 73 74 20 74 61 6b 65 20 63 61 72  em must take car
2ba70 65 20 6f 66 20 73 65 72 69 61 6c 69 7a 69 6e 67  e of serializing
2ba71 20 69 74 73 20 6f 77 6e 0a 20 20 2a 2a 20 69 6e   its own.  ** in
2ba72 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 0a 20 20  itialization..  
2ba73 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
2ba74 33 4d 75 74 65 78 49 6e 69 74 28 29 3b 0a 20 20  3MutexInit();.  
2ba75 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
2ba76 72 63 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61  rc;..  /* Initia
2ba77 6c 69 7a 65 20 74 68 65 20 6d 61 6c 6c 6f 63 28  lize the malloc(
2ba78 29 20 73 79 73 74 65 6d 20 61 6e 64 20 74 68 65  ) system and the
2ba79 20 72 65 63 75 72 73 69 76 65 20 70 49 6e 69 74   recursive pInit
2ba7a 4d 75 74 65 78 20 6d 75 74 65 78 2e 0a 20 20 2a  Mutex mutex..  *
2ba7b 2a 20 54 68 69 73 20 6f 70 65 72 61 74 69 6f 6e  * This operation
2ba7c 20 69 73 20 70 72 6f 74 65 63 74 65 64 20 62 79   is protected by
2ba7d 20 74 68 65 20 53 54 41 54 49 43 5f 4d 41 53 54   the STATIC_MAST
2ba7e 45 52 20 6d 75 74 65 78 2e 20 20 4e 6f 74 65 20  ER mutex.  Note 
2ba7f 74 68 61 74 0a 20 20 2a 2a 20 4d 75 74 65 78 41  that.  ** MutexA
2ba80 6c 6c 6f 63 28 29 20 69 73 20 63 61 6c 6c 65 64  lloc() is called
2ba81 20 66 6f 72 20 61 20 73 74 61 74 69 63 20 6d 75   for a static mu
2ba82 74 65 78 20 70 72 69 6f 72 20 74 6f 20 69 6e 69  tex prior to ini
2ba83 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 0a 20 20  tializing the.  
2ba84 2a 2a 20 6d 61 6c 6c 6f 63 20 73 75 62 73 79 73  ** malloc subsys
2ba85 74 65 6d 20 2d 20 74 68 69 73 20 69 6d 70 6c 69  tem - this impli
2ba86 65 73 20 74 68 61 74 20 74 68 65 20 61 6c 6c 6f  es that the allo
2ba87 63 61 74 69 6f 6e 20 6f 66 20 61 20 73 74 61 74  cation of a stat
2ba88 69 63 0a 20 20 2a 2a 20 6d 75 74 65 78 20 6d 75  ic.  ** mutex mu
2ba89 73 74 20 6e 6f 74 20 72 65 71 75 69 72 65 20 73  st not require s
2ba8a 75 70 70 6f 72 74 20 66 72 6f 6d 20 74 68 65 20  upport from the 
2ba8b 6d 61 6c 6c 6f 63 20 73 75 62 73 79 73 74 65 6d  malloc subsystem
2ba8c 2e 0a 20 20 2a 2f 0a 20 20 70 4d 61 73 74 65 72  ..  */.  pMaster
2ba8d 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
2ba8e 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
2ba8f 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
2ba90 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
2ba91 78 5f 65 6e 74 65 72 28 70 4d 61 73 74 65 72 29  x_enter(pMaster)
2ba92 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33  ;.  if( !sqlite3
2ba93 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 4d  GlobalConfig.isM
2ba94 61 6c 6c 6f 63 49 6e 69 74 20 29 7b 0a 20 20 20  allocInit ){.   
2ba95 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c   rc = sqlite3Mal
2ba96 6c 6f 63 49 6e 69 74 28 29 3b 0a 20 20 7d 0a 20  locInit();.  }. 
2ba97 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
2ba98 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OK ){.    sqlite
2ba99 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73  3GlobalConfig.is
2ba9a 4d 61 6c 6c 6f 63 49 6e 69 74 20 3d 20 31 3b 0a  MallocInit = 1;.
2ba9b 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33      if( !sqlite3
2ba9c 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e  GlobalConfig.pIn
2ba9d 69 74 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 20  itMutex ){.     
2ba9e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
2ba9f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78 20  nfig.pInitMutex 
2baa0 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  = sqlite3MutexAl
2baa1 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
2baa2 5f 52 45 43 55 52 53 49 56 45 29 3b 0a 20 20 20  _RECURSIVE);.   
2baa3 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c     if( sqlite3Gl
2baa4 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65  obalConfig.bCore
2baa5 4d 75 74 65 78 20 26 26 20 21 73 71 6c 69 74 65  Mutex && !sqlite
2baa6 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49  3GlobalConfig.pI
2baa7 6e 69 74 4d 75 74 65 78 20 29 7b 0a 20 20 20 20  nitMutex ){.    
2baa8 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
2baa9 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20  NOMEM;.      }. 
2baaa 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 72     }.  }.  if( r
2baab 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
2baac 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
2baad 6c 43 6f 6e 66 69 67 2e 6e 52 65 66 49 6e 69 74  lConfig.nRefInit
2baae 4d 75 74 65 78 2b 2b 3b 0a 20 20 7d 0a 20 20 73  Mutex++;.  }.  s
2baaf 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
2bab0 76 65 28 70 4d 61 73 74 65 72 29 3b 0a 0a 20 20  ve(pMaster);..  
2bab1 2f 2a 20 49 66 20 75 6e 61 62 6c 65 20 74 6f 20  /* If unable to 
2bab2 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d  initialize the m
2bab3 61 6c 6c 6f 63 20 73 75 62 73 79 73 74 65 6d 2c  alloc subsystem,
2bab4 20 74 68 65 6e 20 72 65 74 75 72 6e 20 65 61 72   then return ear
2bab5 6c 79 2e 0a 20 20 2a 2a 20 54 68 65 72 65 20 69  ly..  ** There i
2bab6 73 20 6c 69 74 74 6c 65 20 68 6f 70 65 20 6f 66  s little hope of
2bab7 20 67 65 74 74 69 6e 67 20 53 51 4c 69 74 65 20   getting SQLite 
2bab8 74 6f 20 72 75 6e 20 69 66 20 74 68 65 20 6d 61  to run if the ma
2bab9 6c 6c 6f 63 0a 20 20 2a 2a 20 73 75 62 73 79 73  lloc.  ** subsys
2baba 74 65 6d 20 63 61 6e 6e 6f 74 20 62 65 20 69 6e  tem cannot be in
2babb 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a  itialized..  */.
2babc 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2babd 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  _OK ){.    retur
2babe 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  n rc;.  }..  /* 
2babf 44 6f 20 74 68 65 20 72 65 73 74 20 6f 66 20 74  Do the rest of t
2bac0 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  he initializatio
2bac1 6e 20 75 6e 64 65 72 20 74 68 65 20 72 65 63 75  n under the recu
2bac2 72 73 69 76 65 20 6d 75 74 65 78 20 73 6f 0a 20  rsive mutex so. 
2bac3 20 2a 2a 20 74 68 61 74 20 77 65 20 77 69 6c 6c   ** that we will
2bac4 20 62 65 20 61 62 6c 65 20 74 6f 20 68 61 6e 64   be able to hand
2bac5 6c 65 20 72 65 63 75 72 73 69 76 65 20 63 61 6c  le recursive cal
2bac6 6c 73 20 69 6e 74 6f 0a 20 20 2a 2a 20 73 71 6c  ls into.  ** sql
2bac7 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
2bac8 29 2e 20 20 54 68 65 20 72 65 63 75 72 73 69 76  ).  The recursiv
2bac9 65 20 63 61 6c 6c 73 20 6e 6f 72 6d 61 6c 6c 79  e calls normally
2baca 20 63 6f 6d 65 20 74 68 72 6f 75 67 68 0a 20 20   come through.  
2bacb 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  ** sqlite3_os_in
2bacc 69 74 28 29 20 77 68 65 6e 20 69 74 20 69 6e 76  it() when it inv
2bacd 6f 6b 65 73 20 73 71 6c 69 74 65 33 5f 76 66 73  okes sqlite3_vfs
2bace 5f 72 65 67 69 73 74 65 72 28 29 2c 20 62 75 74  _register(), but
2bacf 20 6f 74 68 65 72 0a 20 20 2a 2a 20 72 65 63 75   other.  ** recu
2bad0 72 73 69 76 65 20 63 61 6c 6c 73 20 6d 69 67 68  rsive calls migh
2bad1 74 20 61 6c 73 6f 20 62 65 20 70 6f 73 73 69 62  t also be possib
2bad2 6c 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  le..  */.  sqlit
2bad3 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73  e3_mutex_enter(s
2bad4 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2bad5 69 67 2e 70 49 6e 69 74 4d 75 74 65 78 29 3b 0a  ig.pInitMutex);.
2bad6 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
2bad7 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49 6e 69 74  balConfig.isInit
2bad8 3d 3d 30 20 26 26 20 73 71 6c 69 74 65 33 47 6c  ==0 && sqlite3Gl
2bad9 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 6e 50 72 6f  obalConfig.inPro
2bada 67 72 65 73 73 3d 3d 30 20 29 7b 0a 20 20 20 20  gress==0 ){.    
2badb 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61  FuncDefHash *pHa
2badc 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e  sh = &GLOBAL(Fun
2badd 63 44 65 66 48 61 73 68 2c 20 73 71 6c 69 74 65  cDefHash, sqlite
2bade 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73  3GlobalFunctions
2badf 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  );.    sqlite3Gl
2bae0 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 6e 50 72 6f  obalConfig.inPro
2bae1 67 72 65 73 73 20 3d 20 31 3b 0a 20 20 20 20 6d  gress = 1;.    m
2bae2 65 6d 73 65 74 28 70 48 61 73 68 2c 20 30 2c 20  emset(pHash, 0, 
2bae3 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 47 6c  sizeof(sqlite3Gl
2bae4 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 29 3b  obalFunctions));
2bae5 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 67 69  .    sqlite3Regi
2bae6 73 74 65 72 47 6c 6f 62 61 6c 46 75 6e 63 74 69  sterGlobalFuncti
2bae7 6f 6e 73 28 29 3b 0a 20 20 20 20 72 63 20 3d 20  ons();.    rc = 
2bae8 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28  sqlite3_os_init(
2bae9 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
2baea 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
2baeb 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 63    rc = sqlite3Pc
2baec 61 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 29  acheInitialize()
2baed 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50  ;.      sqlite3P
2baee 43 61 63 68 65 42 75 66 66 65 72 53 65 74 75 70  CacheBufferSetup
2baef 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
2baf0 6f 6e 66 69 67 2e 70 50 61 67 65 2c 20 0a 20 20  onfig.pPage, .  
2baf1 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47          sqlite3G
2baf2 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61  lobalConfig.szPa
2baf3 67 65 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  ge, sqlite3Globa
2baf4 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 29 3b 0a  lConfig.nPage);.
2baf5 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2baf6 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 6e  3GlobalConfig.in
2baf7 50 72 6f 67 72 65 73 73 20 3d 20 30 3b 0a 20 20  Progress = 0;.  
2baf8 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
2baf9 6f 6e 66 69 67 2e 69 73 49 6e 69 74 20 3d 20 28  onfig.isInit = (
2bafa 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20  rc==SQLITE_OK ? 
2bafb 31 20 3a 20 30 29 3b 0a 20 20 7d 0a 20 20 73 71  1 : 0);.  }.  sq
2bafc 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
2bafd 65 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  e(sqlite3GlobalC
2bafe 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78  onfig.pInitMutex
2baff 29 3b 0a 0a 20 20 2f 2a 20 47 6f 20 62 61 63 6b  );..  /* Go back
2bb00 20 75 6e 64 65 72 20 74 68 65 20 73 74 61 74 69   under the stati
2bb01 63 20 6d 75 74 65 78 20 61 6e 64 20 63 6c 65 61  c mutex and clea
2bb02 6e 20 75 70 20 74 68 65 20 72 65 63 75 72 73 69  n up the recursi
2bb03 76 65 0a 20 20 2a 2a 20 6d 75 74 65 78 20 74 6f  ve.  ** mutex to
2bb04 20 70 72 65 76 65 6e 74 20 61 20 72 65 73 6f 75   prevent a resou
2bb05 72 63 65 20 6c 65 61 6b 2e 0a 20 20 2a 2f 0a 20  rce leak..  */. 
2bb06 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
2bb07 6e 74 65 72 28 70 4d 61 73 74 65 72 29 3b 0a 20  nter(pMaster);. 
2bb08 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
2bb09 6e 66 69 67 2e 6e 52 65 66 49 6e 69 74 4d 75 74  nfig.nRefInitMut
2bb0a 65 78 2d 2d 3b 0a 20 20 69 66 28 20 73 71 6c 69  ex--;.  if( sqli
2bb0b 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2bb0c 6e 52 65 66 49 6e 69 74 4d 75 74 65 78 3c 3d 30  nRefInitMutex<=0
2bb0d 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
2bb0e 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
2bb0f 66 69 67 2e 6e 52 65 66 49 6e 69 74 4d 75 74 65  fig.nRefInitMute
2bb10 78 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69  x==0 );.    sqli
2bb11 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73  te3_mutex_free(s
2bb12 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2bb13 69 67 2e 70 49 6e 69 74 4d 75 74 65 78 29 3b 0a  ig.pInitMutex);.
2bb14 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
2bb15 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74  lConfig.pInitMut
2bb16 65 78 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71  ex = 0;.  }.  sq
2bb17 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
2bb18 65 28 70 4d 61 73 74 65 72 29 3b 0a 0a 20 20 2f  e(pMaster);..  /
2bb19 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
2bb1a 69 73 20 6a 75 73 74 20 61 20 73 61 6e 69 74 79  is just a sanity
2bb1b 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73   check to make s
2bb1c 75 72 65 20 53 51 4c 69 74 65 20 68 61 73 0a 20  ure SQLite has. 
2bb1d 20 2a 2a 20 62 65 65 6e 20 63 6f 6d 70 69 6c 65   ** been compile
2bb1e 64 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 49 74  d correctly.  It
2bb1f 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f   is important to
2bb20 20 72 75 6e 20 74 68 69 73 20 63 6f 64 65 2c 20   run this code, 
2bb21 62 75 74 0a 20 20 2a 2a 20 77 65 20 64 6f 6e 27  but.  ** we don'
2bb22 74 20 77 61 6e 74 20 74 6f 20 72 75 6e 20 69 74  t want to run it
2bb23 20 74 6f 6f 20 6f 66 74 65 6e 20 61 6e 64 20 73   too often and s
2bb24 6f 61 6b 20 75 70 20 43 50 55 20 63 79 63 6c 65  oak up CPU cycle
2bb25 73 20 66 6f 72 20 6e 6f 0a 20 20 2a 2a 20 72 65  s for no.  ** re
2bb26 61 73 6f 6e 2e 20 20 53 6f 20 77 65 20 72 75 6e  ason.  So we run
2bb27 20 69 74 20 6f 6e 63 65 20 64 75 72 69 6e 67 20   it once during 
2bb28 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 0a  initialization..
2bb29 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45    */.#ifndef NDE
2bb2a 42 55 47 0a 20 20 2f 2a 20 54 68 69 73 20 73 65  BUG.  /* This se
2bb2b 63 74 69 6f 6e 20 6f 66 20 63 6f 64 65 27 73 20  ction of code's 
2bb2c 6f 6e 6c 79 20 22 6f 75 74 70 75 74 22 20 69 73  only "output" is
2bb2d 20 76 69 61 20 61 73 73 65 72 74 28 29 20 73 74   via assert() st
2bb2e 61 74 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20 20 69  atements. */.  i
2bb2f 66 20 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  f ( rc==SQLITE_O
2bb30 4b 20 29 7b 0a 20 20 20 20 75 36 34 20 78 20 3d  K ){.    u64 x =
2bb31 20 28 28 28 75 36 34 29 31 29 3c 3c 36 33 29 2d   (((u64)1)<<63)-
2bb32 31 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 79 3b  1;.    double y;
2bb33 0a 20 20 20 20 61 73 73 65 72 74 28 73 69 7a 65  .    assert(size
2bb34 6f 66 28 78 29 3d 3d 38 29 3b 0a 20 20 20 20 61  of(x)==8);.    a
2bb35 73 73 65 72 74 28 73 69 7a 65 6f 66 28 78 29 3d  ssert(sizeof(x)=
2bb36 3d 73 69 7a 65 6f 66 28 79 29 29 3b 0a 20 20 20  =sizeof(y));.   
2bb37 20 6d 65 6d 63 70 79 28 26 79 2c 20 26 78 2c 20   memcpy(&y, &x, 
2bb38 38 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  8);.    assert( 
2bb39 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 79 29 20  sqlite3IsNaN(y) 
2bb3a 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  );.  }.#endif.. 
2bb3b 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
2bb3c 2a 0a 2a 2a 20 55 6e 64 6f 20 74 68 65 20 65 66  *.** Undo the ef
2bb3d 66 65 63 74 73 20 6f 66 20 73 71 6c 69 74 65 33  fects of sqlite3
2bb3e 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e 20 20  _initialize().  
2bb3f 4d 75 73 74 20 6e 6f 74 20 62 65 20 63 61 6c 6c  Must not be call
2bb40 65 64 20 77 68 69 6c 65 0a 2a 2a 20 74 68 65 72  ed while.** ther
2bb41 65 20 61 72 65 20 6f 75 74 73 74 61 6e 64 69 6e  e are outstandin
2bb42 67 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  g database conne
2bb43 63 74 69 6f 6e 73 20 6f 72 20 6d 65 6d 6f 72 79  ctions or memory
2bb44 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6f 72 0a   allocations or.
2bb45 2a 2a 20 77 68 69 6c 65 20 61 6e 79 20 70 61 72  ** while any par
2bb46 74 20 6f 66 20 53 51 4c 69 74 65 20 69 73 20 6f  t of SQLite is o
2bb47 74 68 65 72 77 69 73 65 20 69 6e 20 75 73 65 20  therwise in use 
2bb48 69 6e 20 61 6e 79 20 74 68 72 65 61 64 2e 20 20  in any thread.  
2bb49 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  This.** routine 
2bb4a 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66  is not threadsaf
2bb4b 65 2e 20 20 4e 6f 74 20 62 79 20 61 20 6c 6f 6e  e.  Not by a lon
2bb4c 67 20 73 68 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54  g shot..*/.SQLIT
2bb4d 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2bb4e 33 5f 73 68 75 74 64 6f 77 6e 28 76 6f 69 64 29  3_shutdown(void)
2bb4f 7b 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  {.  sqlite3Globa
2bb50 6c 43 6f 6e 66 69 67 2e 69 73 4d 61 6c 6c 6f 63  lConfig.isMalloc
2bb51 49 6e 69 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69  Init = 0;.  sqli
2bb52 74 65 33 50 63 61 63 68 65 53 68 75 74 64 6f 77  te3PcacheShutdow
2bb53 6e 28 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  n();.  if( sqlit
2bb54 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69  e3GlobalConfig.i
2bb55 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 73 71 6c  sInit ){.    sql
2bb56 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 3b 0a 20  ite3_os_end();. 
2bb57 20 7d 0a 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c   }.  sqlite3Mall
2bb58 6f 63 45 6e 64 28 29 3b 0a 20 20 73 71 6c 69 74  ocEnd();.  sqlit
2bb59 65 33 4d 75 74 65 78 45 6e 64 28 29 3b 0a 20 20  e3MutexEnd();.  
2bb5a 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
2bb5b 66 69 67 2e 69 73 49 6e 69 74 20 3d 20 30 3b 0a  fig.isInit = 0;.
2bb5c 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2bb5d 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  OK;.}../*.** Thi
2bb5e 73 20 41 50 49 20 61 6c 6c 6f 77 73 20 61 70 70  s API allows app
2bb5f 6c 69 63 61 74 69 6f 6e 73 20 74 6f 20 6d 6f 64  lications to mod
2bb60 69 66 79 20 74 68 65 20 67 6c 6f 62 61 6c 20 63  ify the global c
2bb61 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 66 0a  onfiguration of.
2bb62 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69  ** the SQLite li
2bb63 62 72 61 72 79 20 61 74 20 72 75 6e 2d 74 69 6d  brary at run-tim
2bb64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  e..**.** This ro
2bb65 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 6f 6e 6c  utine should onl
2bb66 79 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e  y be called when
2bb67 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 75   there are no ou
2bb68 74 73 74 61 6e 64 69 6e 67 0a 2a 2a 20 64 61 74  tstanding.** dat
2bb69 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2bb6a 73 20 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  s or memory allo
2bb6b 63 61 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 72  cations.  This r
2bb6c 6f 75 74 69 6e 65 20 69 73 20 6e 6f 74 0a 2a 2a  outine is not.**
2bb6d 20 74 68 72 65 61 64 73 61 66 65 2e 20 20 46 61   threadsafe.  Fa
2bb6e 69 6c 75 72 65 20 74 6f 20 68 65 65 64 20 74 68  ilure to heed th
2bb6f 65 73 65 20 77 61 72 6e 69 6e 67 73 20 63 61 6e  ese warnings can
2bb70 20 6c 65 61 64 20 74 6f 20 75 6e 70 72 65 64 69   lead to unpredi
2bb71 63 74 61 62 6c 65 0a 2a 2a 20 62 65 68 61 76 69  ctable.** behavi
2bb72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  or..*/.SQLITE_AP
2bb73 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f  I int sqlite3_co
2bb74 6e 66 69 67 28 69 6e 74 20 6f 70 2c 20 2e 2e 2e  nfig(int op, ...
2bb75 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b  ){.  va_list ap;
2bb76 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
2bb77 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 73 71 6c  TE_OK;..  /* sql
2bb78 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 73 68  ite3_config() sh
2bb79 61 6c 6c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  all return SQLIT
2bb7a 45 5f 4d 49 53 55 53 45 20 69 66 20 69 74 20 69  E_MISUSE if it i
2bb7b 73 20 69 6e 76 6f 6b 65 64 20 77 68 69 6c 65 0a  s invoked while.
2bb7c 20 20 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20    ** the SQLite 
2bb7d 6c 69 62 72 61 72 79 20 69 73 20 69 6e 20 75 73  library is in us
2bb7e 65 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  e. */.  if( sqli
2bb7f 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2bb80 69 73 49 6e 69 74 20 29 20 72 65 74 75 72 6e 20  isInit ) return 
2bb81 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 0a  SQLITE_MISUSE;..
2bb82 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 6f    va_start(ap, o
2bb83 70 29 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70  p);.  switch( op
2bb84 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 4d 75 74 65   ){..    /* Mute
2bb85 78 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  x configuration 
2bb86 6f 70 74 69 6f 6e 73 20 61 72 65 20 6f 6e 6c 79  options are only
2bb87 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 61 20   available in a 
2bb88 74 68 72 65 61 64 73 61 66 65 0a 20 20 20 20 2a  threadsafe.    *
2bb89 2a 20 63 6f 6d 70 69 6c 65 2e 20 0a 20 20 20 20  * compile. .    
2bb8a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  */.#if SQLITE_TH
2bb8b 52 45 41 44 53 41 46 45 0a 20 20 20 20 63 61 73  READSAFE.    cas
2bb8c 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
2bb8d 53 49 4e 47 4c 45 54 48 52 45 41 44 3a 20 7b 0a  SINGLETHREAD: {.
2bb8e 20 20 20 20 20 20 2f 2a 20 44 69 73 61 62 6c 65        /* Disable
2bb8f 20 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20 2a 2f   all mutexing */
2bb90 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
2bb91 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65  obalConfig.bCore
2bb92 4d 75 74 65 78 20 3d 20 30 3b 0a 20 20 20 20 20  Mutex = 0;.     
2bb93 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
2bb94 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65 78 20  nfig.bFullMutex 
2bb95 3d 20 30 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  = 0;.      break
2bb96 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
2bb97 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d   SQLITE_CONFIG_M
2bb98 55 4c 54 49 54 48 52 45 41 44 3a 20 7b 0a 20 20  ULTITHREAD: {.  
2bb99 20 20 20 20 2f 2a 20 44 69 73 61 62 6c 65 20 6d      /* Disable m
2bb9a 75 74 65 78 69 6e 67 20 6f 66 20 64 61 74 61 62  utexing of datab
2bb9b 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ase connections 
2bb9c 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 45 6e 61 62  */.      /* Enab
2bb9d 6c 65 20 6d 75 74 65 78 69 6e 67 20 6f 66 20 63  le mutexing of c
2bb9e 6f 72 65 20 64 61 74 61 20 73 74 72 75 63 74 75  ore data structu
2bb9f 72 65 73 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c  res */.      sql
2bba0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2bba1 2e 62 43 6f 72 65 4d 75 74 65 78 20 3d 20 31 3b  .bCoreMutex = 1;
2bba2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
2bba3 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 46 75 6c 6c  obalConfig.bFull
2bba4 4d 75 74 65 78 20 3d 20 30 3b 0a 20 20 20 20 20  Mutex = 0;.     
2bba5 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
2bba6 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f    case SQLITE_CO
2bba7 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44 3a  NFIG_SERIALIZED:
2bba8 20 7b 0a 20 20 20 20 20 20 2f 2a 20 45 6e 61 62   {.      /* Enab
2bba9 6c 65 20 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20  le all mutexing 
2bbaa 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
2bbab 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f  GlobalConfig.bCo
2bbac 72 65 4d 75 74 65 78 20 3d 20 31 3b 0a 20 20 20  reMutex = 1;.   
2bbad 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
2bbae 43 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65  Config.bFullMute
2bbaf 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 62 72 65  x = 1;.      bre
2bbb0 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
2bbb1 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  se SQLITE_CONFIG
2bbb2 5f 4d 55 54 45 58 3a 20 7b 0a 20 20 20 20 20 20  _MUTEX: {.      
2bbb3 2f 2a 20 53 70 65 63 69 66 79 20 61 6e 20 61 6c  /* Specify an al
2bbb4 74 65 72 6e 61 74 69 76 65 20 6d 75 74 65 78 20  ternative mutex 
2bbb5 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  implementation *
2bbb6 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  /.      sqlite3G
2bbb7 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65  lobalConfig.mute
2bbb8 78 20 3d 20 2a 76 61 5f 61 72 67 28 61 70 2c 20  x = *va_arg(ap, 
2bbb9 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
2bbba 74 68 6f 64 73 2a 29 3b 0a 20 20 20 20 20 20 62  thods*);.      b
2bbbb 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
2bbbc 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  case SQLITE_CONF
2bbbd 49 47 5f 47 45 54 4d 55 54 45 58 3a 20 7b 0a 20  IG_GETMUTEX: {. 
2bbbe 20 20 20 20 20 2f 2a 20 52 65 74 72 69 65 76 65       /* Retrieve
2bbbf 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 75 74   the current mut
2bbc0 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ex implementatio
2bbc1 6e 20 2a 2f 0a 20 20 20 20 20 20 2a 76 61 5f 61  n */.      *va_a
2bbc2 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f 6d  rg(ap, sqlite3_m
2bbc3 75 74 65 78 5f 6d 65 74 68 6f 64 73 2a 29 20 3d  utex_methods*) =
2bbc4 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
2bbc5 6e 66 69 67 2e 6d 75 74 65 78 3b 0a 20 20 20 20  nfig.mutex;.    
2bbc6 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
2bbc7 65 6e 64 69 66 0a 0a 0a 20 20 20 20 63 61 73 65  endif...    case
2bbc8 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d   SQLITE_CONFIG_M
2bbc9 41 4c 4c 4f 43 3a 20 7b 0a 20 20 20 20 20 20 2f  ALLOC: {.      /
2bbca 2a 20 53 70 65 63 69 66 79 20 61 6e 20 61 6c 74  * Specify an alt
2bbcb 65 72 6e 61 74 69 76 65 20 6d 61 6c 6c 6f 63 20  ernative malloc 
2bbcc 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  implementation *
2bbcd 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  /.      sqlite3G
2bbce 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 3d 20  lobalConfig.m = 
2bbcf 2a 76 61 5f 61 72 67 28 61 70 2c 20 73 71 6c 69  *va_arg(ap, sqli
2bbd0 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a  te3_mem_methods*
2bbd1 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
2bbd2 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53      }.    case S
2bbd3 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54  QLITE_CONFIG_GET
2bbd4 4d 41 4c 4c 4f 43 3a 20 7b 0a 20 20 20 20 20 20  MALLOC: {.      
2bbd5 2f 2a 20 52 65 74 72 69 65 76 65 20 74 68 65 20  /* Retrieve the 
2bbd6 63 75 72 72 65 6e 74 20 6d 61 6c 6c 6f 63 28 29  current malloc()
2bbd7 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
2bbd8 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  */.      if( sql
2bbd9 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2bbda 2e 6d 2e 78 4d 61 6c 6c 6f 63 3d 3d 30 20 29 20  .m.xMalloc==0 ) 
2bbdb 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66  sqlite3MemSetDef
2bbdc 61 75 6c 74 28 29 3b 0a 20 20 20 20 20 20 2a 76  ault();.      *v
2bbdd 61 5f 61 72 67 28 61 70 2c 20 73 71 6c 69 74 65  a_arg(ap, sqlite
2bbde 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 29 20  3_mem_methods*) 
2bbdf 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
2bbe0 6f 6e 66 69 67 2e 6d 3b 0a 20 20 20 20 20 20 62  onfig.m;.      b
2bbe1 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
2bbe2 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  case SQLITE_CONF
2bbe3 49 47 5f 4d 45 4d 53 54 41 54 55 53 3a 20 7b 0a  IG_MEMSTATUS: {.
2bbe4 20 20 20 20 20 20 2f 2a 20 45 6e 61 62 6c 65 20        /* Enable 
2bbe5 6f 72 20 64 69 73 61 62 6c 65 20 74 68 65 20 6d  or disable the m
2bbe6 61 6c 6c 6f 63 20 73 74 61 74 75 73 20 63 6f 6c  alloc status col
2bbe7 6c 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20  lection */.     
2bbe8 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
2bbe9 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 3d 20  nfig.bMemstat = 
2bbea 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b  va_arg(ap, int);
2bbeb 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2bbec 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c    }.    case SQL
2bbed 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54  ITE_CONFIG_SCRAT
2bbee 43 48 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 44  CH: {.      /* D
2bbef 65 73 69 67 6e 61 74 65 20 61 20 62 75 66 66 65  esignate a buffe
2bbf0 72 20 66 6f 72 20 73 63 72 61 74 63 68 20 6d 65  r for scratch me
2bbf1 6d 6f 72 79 20 73 70 61 63 65 20 2a 2f 0a 20 20  mory space */.  
2bbf2 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
2bbf3 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68  lConfig.pScratch
2bbf4 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 76 6f   = va_arg(ap, vo
2bbf5 69 64 2a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  id*);.      sqli
2bbf6 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2bbf7 73 7a 53 63 72 61 74 63 68 20 3d 20 76 61 5f 61  szScratch = va_a
2bbf8 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20  rg(ap, int);.   
2bbf9 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
2bbfa 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 20  Config.nScratch 
2bbfb 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74  = va_arg(ap, int
2bbfc 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
2bbfd 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53      }.    case S
2bbfe 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47  QLITE_CONFIG_PAG
2bbff 45 43 41 43 48 45 3a 20 7b 0a 20 20 20 20 20 20  ECACHE: {.      
2bc00 2f 2a 20 44 65 73 69 67 6e 61 74 65 20 61 20 62  /* Designate a b
2bc01 75 66 66 65 72 20 66 6f 72 20 73 63 72 61 74 63  uffer for scratc
2bc02 68 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 2a  h memory space *
2bc03 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  /.      sqlite3G
2bc04 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67  lobalConfig.pPag
2bc05 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 76  e = va_arg(ap, v
2bc06 6f 69 64 2a 29 3b 0a 20 20 20 20 20 20 73 71 6c  oid*);.      sql
2bc07 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2bc08 2e 73 7a 50 61 67 65 20 3d 20 76 61 5f 61 72 67  .szPage = va_arg
2bc09 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20  (ap, int);.     
2bc0a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
2bc0b 6e 66 69 67 2e 6e 50 61 67 65 20 3d 20 76 61 5f  nfig.nPage = va_
2bc0c 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20  arg(ap, int);.  
2bc0d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2bc0e 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  ..#if defined(SQ
2bc0f 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53  LITE_ENABLE_MEMS
2bc10 59 53 33 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  YS3) || defined(
2bc11 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
2bc12 4d 53 59 53 35 29 0a 20 20 20 20 63 61 73 65 20  MSYS5).    case 
2bc13 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48 45  SQLITE_CONFIG_HE
2bc14 41 50 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 44  AP: {.      /* D
2bc15 65 73 69 67 6e 61 74 65 20 61 20 62 75 66 66 65  esignate a buffe
2bc16 72 20 66 6f 72 20 68 65 61 70 20 6d 65 6d 6f 72  r for heap memor
2bc17 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20  y space */.     
2bc18 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
2bc19 6e 66 69 67 2e 70 48 65 61 70 20 3d 20 76 61 5f  nfig.pHeap = va_
2bc1a 61 72 67 28 61 70 2c 20 76 6f 69 64 2a 29 3b 0a  arg(ap, void*);.
2bc1b 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f        sqlite3Glo
2bc1c 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 61 70 20  balConfig.nHeap 
2bc1d 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74  = va_arg(ap, int
2bc1e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2bc1f 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 6e 52  GlobalConfig.mnR
2bc20 65 71 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  eq = va_arg(ap, 
2bc21 69 6e 74 29 3b 0a 0a 20 20 20 20 20 20 69 66 28  int);..      if(
2bc22 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
2bc23 6e 66 69 67 2e 70 48 65 61 70 3d 3d 30 20 29 7b  nfig.pHeap==0 ){
2bc24 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74  .        /* If t
2bc25 68 65 20 68 65 61 70 20 70 6f 69 6e 74 65 72 20  he heap pointer 
2bc26 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 72 65  is NULL, then re
2bc27 73 74 6f 72 65 20 74 68 65 20 6d 61 6c 6c 6f 63  store the malloc
2bc28 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a   implementation.
2bc29 20 20 20 20 20 20 20 20 2a 2a 20 62 61 63 6b 20          ** back 
2bc2a 74 6f 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73  to NULL pointers
2bc2b 20 74 6f 6f 2e 20 20 54 68 69 73 20 77 69 6c 6c   too.  This will
2bc2c 20 63 61 75 73 65 20 74 68 65 20 6d 61 6c 6c 6f   cause the mallo
2bc2d 63 20 74 6f 20 67 6f 0a 20 20 20 20 20 20 20 20  c to go.        
2bc2e 2a 2a 20 62 61 63 6b 20 74 6f 20 69 74 73 20 64  ** back to its d
2bc2f 65 66 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e 74  efault implement
2bc30 61 74 69 6f 6e 20 77 68 65 6e 20 73 71 6c 69 74  ation when sqlit
2bc31 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
2bc32 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 75  is.        ** ru
2bc33 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  n..        */.  
2bc34 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 73 71        memset(&sq
2bc35 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2bc36 67 2e 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  g.m, 0, sizeof(s
2bc37 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2bc38 69 67 2e 6d 29 29 3b 0a 20 20 20 20 20 20 7d 65  ig.m));.      }e
2bc39 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  lse{.        /* 
2bc3a 54 68 65 20 68 65 61 70 20 70 6f 69 6e 74 65 72  The heap pointer
2bc3b 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   is not NULL, th
2bc3c 65 6e 20 69 6e 73 74 61 6c 6c 20 6f 6e 65 20 6f  en install one o
2bc3d 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a  f the.        **
2bc3e 20 6d 65 6d 35 2e 63 2f 6d 65 6d 33 2e 63 20 6d   mem5.c/mem3.c m
2bc3f 65 74 68 6f 64 73 2e 20 49 66 20 6e 65 69 74 68  ethods. If neith
2bc40 65 72 20 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  er ENABLE_MEMSYS
2bc41 33 20 6e 6f 72 0a 20 20 20 20 20 20 20 20 2a 2a  3 nor.        **
2bc42 20 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20   ENABLE_MEMSYS5 
2bc43 69 73 20 64 65 66 69 6e 65 64 2c 20 72 65 74 75  is defined, retu
2bc44 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20  rn an error..   
2bc45 20 20 20 20 20 2a 2a 20 74 68 65 20 64 65 66 61       ** the defa
2bc46 75 6c 74 20 63 61 73 65 20 61 6e 64 20 72 65 74  ult case and ret
2bc47 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20  urn an error..  
2bc48 20 20 20 20 20 20 2a 2f 0a 23 69 66 64 65 66 20        */.#ifdef 
2bc49 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
2bc4a 4d 53 59 53 33 0a 20 20 20 20 20 20 20 20 73 71  MSYS3.        sq
2bc4b 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2bc4c 67 2e 6d 20 3d 20 2a 73 71 6c 69 74 65 33 4d 65  g.m = *sqlite3Me
2bc4d 6d 47 65 74 4d 65 6d 73 79 73 33 28 29 3b 0a 23  mGetMemsys3();.#
2bc4e 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
2bc4f 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59  ITE_ENABLE_MEMSY
2bc50 53 35 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  S5.        sqlit
2bc51 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
2bc52 20 3d 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65   = *sqlite3MemGe
2bc53 74 4d 65 6d 73 79 73 35 28 29 3b 0a 23 65 6e 64  tMemsys5();.#end
2bc54 69 66 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  if.      }.     
2bc55 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
2bc56 6e 64 69 66 0a 0a 23 69 66 20 64 65 66 69 6e 65  ndif..#if define
2bc57 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
2bc58 4d 45 4d 53 59 53 36 29 0a 20 20 20 20 63 61 73  MEMSYS6).    cas
2bc59 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
2bc5a 43 48 55 4e 4b 41 4c 4c 4f 43 3a 20 7b 0a 20 20  CHUNKALLOC: {.  
2bc5b 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
2bc5c 6c 43 6f 6e 66 69 67 2e 6e 53 6d 61 6c 6c 20 3d  lConfig.nSmall =
2bc5d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29   va_arg(ap, int)
2bc5e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  ;.      sqlite3G
2bc5f 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 3d 20  lobalConfig.m = 
2bc60 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65  *sqlite3MemGetMe
2bc61 6d 73 79 73 36 28 29 3b 0a 20 20 20 20 20 20 62  msys6();.      b
2bc62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  reak;.    }.#end
2bc63 69 66 0a 0a 20 20 20 20 63 61 73 65 20 53 51 4c  if..    case SQL
2bc64 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41  ITE_CONFIG_LOOKA
2bc65 53 49 44 45 3a 20 7b 0a 20 20 20 20 20 20 73 71  SIDE: {.      sq
2bc66 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2bc67 67 2e 73 7a 4c 6f 6f 6b 61 73 69 64 65 20 3d 20  g.szLookaside = 
2bc68 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b  va_arg(ap, int);
2bc69 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
2bc6a 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 4c 6f 6f 6b  obalConfig.nLook
2bc6b 61 73 69 64 65 20 3d 20 76 61 5f 61 72 67 28 61  aside = va_arg(a
2bc6c 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 62  p, int);.      b
2bc6d 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
2bc6e 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
2bc6f 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
2bc70 52 4f 52 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ROR;.      break
2bc71 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 76 61  ;.    }.  }.  va
2bc72 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 75  _end(ap);.  retu
2bc73 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
2bc74 53 65 74 20 75 70 20 74 68 65 20 6c 6f 6f 6b 61  Set up the looka
2bc75 73 69 64 65 20 62 75 66 66 65 72 73 20 66 6f 72  side buffers for
2bc76 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e   a database conn
2bc77 65 63 74 69 6f 6e 2e 0a 2a 2a 20 52 65 74 75 72  ection..** Retur
2bc78 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  n SQLITE_OK on s
2bc79 75 63 63 65 73 73 2e 20 20 0a 2a 2a 20 49 66 20  uccess.  .** If 
2bc7a 6c 6f 6f 6b 61 73 69 64 65 20 69 73 20 61 6c 72  lookaside is alr
2bc7b 65 61 64 79 20 61 63 74 69 76 65 2c 20 72 65 74  eady active, ret
2bc7c 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e  urn SQLITE_BUSY.
2bc7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 7a 20 70 61  .**.** The sz pa
2bc7e 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 6e  rameter is the n
2bc7f 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
2bc80 6e 20 65 61 63 68 20 6c 6f 6f 6b 61 73 69 64 65  n each lookaside
2bc81 20 73 6c 6f 74 2e 0a 2a 2a 20 54 68 65 20 63 6e   slot..** The cn
2bc82 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74  t parameter is t
2bc83 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 6c 6f  he number of slo
2bc84 74 73 2e 20 20 49 66 20 70 53 74 61 72 74 20 69  ts.  If pStart i
2bc85 73 20 4e 55 4c 4c 20 74 68 65 0a 2a 2a 20 73 70  s NULL the.** sp
2bc86 61 63 65 20 66 6f 72 20 74 68 65 20 6c 6f 6f 6b  ace for the look
2bc87 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 69 73 20  aside memory is 
2bc88 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
2bc89 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a  lite3_malloc()..
2bc8a 2a 2a 20 49 66 20 70 53 74 61 72 74 20 69 73 20  ** If pStart is 
2bc8b 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 69 74  not NULL then it
2bc8c 20 69 73 20 73 7a 2a 63 6e 74 20 62 79 74 65 73   is sz*cnt bytes
2bc8d 20 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20 75 73   of memory to us
2bc8e 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 6c 6f 6f  e for.** the loo
2bc8f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 2e 0a 2a  kaside memory..*
2bc90 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 74  /.static int set
2bc91 75 70 4c 6f 6f 6b 61 73 69 64 65 28 73 71 6c 69  upLookaside(sqli
2bc92 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70  te3 *db, void *p
2bc93 42 75 66 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74  Buf, int sz, int
2bc94 20 63 6e 74 29 7b 0a 20 20 76 6f 69 64 20 2a 70   cnt){.  void *p
2bc95 53 74 61 72 74 3b 0a 20 20 69 66 28 20 64 62 2d  Start;.  if( db-
2bc96 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 20  >lookaside.nOut 
2bc97 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
2bc98 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20  LITE_BUSY;.  }. 
2bc99 20 69 66 28 20 73 7a 3c 30 20 29 20 73 7a 20 3d   if( sz<0 ) sz =
2bc9a 20 30 3b 0a 20 20 69 66 28 20 63 6e 74 3c 30 20   0;.  if( cnt<0 
2bc9b 29 20 63 6e 74 20 3d 20 30 3b 0a 20 20 69 66 28  ) cnt = 0;.  if(
2bc9c 20 70 42 75 66 3d 3d 30 20 29 7b 0a 20 20 20 20   pBuf==0 ){.    
2bc9d 73 7a 20 3d 20 28 73 7a 20 2b 20 37 29 26 7e 37  sz = (sz + 7)&~7
2bc9e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67  ;.    sqlite3Beg
2bc9f 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  inBenignMalloc()
2bca0 3b 0a 20 20 20 20 70 53 74 61 72 74 20 3d 20 73  ;.    pStart = s
2bca1 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 73 7a  qlite3Malloc( sz
2bca2 2a 63 6e 74 20 29 3b 0a 20 20 20 20 73 71 6c 69  *cnt );.    sqli
2bca3 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
2bca4 6f 63 28 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  oc();.  }else{. 
2bca5 20 20 20 73 7a 20 3d 20 73 7a 26 7e 37 3b 0a 20     sz = sz&~7;. 
2bca6 20 20 20 70 53 74 61 72 74 20 3d 20 70 42 75 66     pStart = pBuf
2bca7 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e  ;.  }.  if( db->
2bca8 6c 6f 6f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c 6f  lookaside.bMallo
2bca9 63 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ced ){.    sqlit
2bcaa 65 33 5f 66 72 65 65 28 64 62 2d 3e 6c 6f 6f 6b  e3_free(db->look
2bcab 61 73 69 64 65 2e 70 53 74 61 72 74 29 3b 0a 20  aside.pStart);. 
2bcac 20 7d 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69   }.  db->lookasi
2bcad 64 65 2e 70 53 74 61 72 74 20 3d 20 70 53 74 61  de.pStart = pSta
2bcae 72 74 3b 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73  rt;.  db->lookas
2bcaf 69 64 65 2e 70 46 72 65 65 20 3d 20 30 3b 0a 20  ide.pFree = 0;. 
2bcb0 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73   db->lookaside.s
2bcb1 7a 20 3d 20 73 7a 3b 0a 20 20 64 62 2d 3e 6c 6f  z = sz;.  db->lo
2bcb2 6f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c 6f 63 65  okaside.bMalloce
2bcb3 64 20 3d 20 70 42 75 66 3d 3d 30 3b 0a 20 20 69  d = pBuf==0;.  i
2bcb4 66 28 20 70 53 74 61 72 74 20 29 7b 0a 20 20 20  f( pStart ){.   
2bcb5 20 69 6e 74 20 69 3b 0a 20 20 20 20 4c 6f 6f 6b   int i;.    Look
2bcb6 61 73 69 64 65 53 6c 6f 74 20 2a 70 3b 0a 20 20  asideSlot *p;.  
2bcb7 20 20 70 20 3d 20 28 4c 6f 6f 6b 61 73 69 64 65    p = (Lookaside
2bcb8 53 6c 6f 74 2a 29 70 53 74 61 72 74 3b 0a 20 20  Slot*)pStart;.  
2bcb9 20 20 66 6f 72 28 69 3d 63 6e 74 2d 31 3b 20 69    for(i=cnt-1; i
2bcba 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 20  >=0; i--){.     
2bcbb 20 70 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e   p->pNext = db->
2bcbc 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 3b  lookaside.pFree;
2bcbd 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61  .      db->looka
2bcbe 73 69 64 65 2e 70 46 72 65 65 20 3d 20 70 3b 0a  side.pFree = p;.
2bcbf 20 20 20 20 20 20 70 20 3d 20 28 4c 6f 6f 6b 61        p = (Looka
2bcc0 73 69 64 65 53 6c 6f 74 2a 29 26 28 28 75 38 2a  sideSlot*)&((u8*
2bcc1 29 70 29 5b 73 7a 5d 3b 0a 20 20 20 20 7d 0a 20  )p)[sz];.    }. 
2bcc2 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65     db->lookaside
2bcc3 2e 70 45 6e 64 20 3d 20 70 3b 0a 20 20 20 20 64  .pEnd = p;.    d
2bcc4 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e  b->lookaside.bEn
2bcc5 61 62 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c  abled = 1;.  }el
2bcc6 73 65 7b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b  se{.    db->look
2bcc7 61 73 69 64 65 2e 70 45 6e 64 20 3d 20 30 3b 0a  aside.pEnd = 0;.
2bcc8 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
2bcc9 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a  e.bEnabled = 0;.
2bcca 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
2bccb 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
2bccc 20 52 65 74 75 72 6e 20 74 68 65 20 6d 75 74 65   Return the mute
2bccd 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  x associated wit
2bcce 68 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e  h a database con
2bccf 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  nection..*/.SQLI
2bcd0 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 6d  TE_API sqlite3_m
2bcd1 75 74 65 78 20 2a 73 71 6c 69 74 65 33 5f 64 62  utex *sqlite3_db
2bcd2 5f 6d 75 74 65 78 28 73 71 6c 69 74 65 33 20 2a  _mutex(sqlite3 *
2bcd3 64 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62  db){.  return db
2bcd4 2d 3e 6d 75 74 65 78 3b 0a 7d 0a 0a 2f 2a 0a 2a  ->mutex;.}../*.*
2bcd5 2a 20 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  * Configuration 
2bcd6 73 65 74 74 69 6e 67 73 20 66 6f 72 20 61 6e 20  settings for an 
2bcd7 69 6e 64 69 76 69 64 75 61 6c 20 64 61 74 61 62  individual datab
2bcd8 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a  ase connection.*
2bcd9 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2bcda 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66   sqlite3_db_conf
2bcdb 69 67 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ig(sqlite3 *db, 
2bcdc 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 7b 0a 20 20  int op, ...){.  
2bcdd 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 6e  va_list ap;.  in
2bcde 74 20 72 63 3b 0a 20 20 76 61 5f 73 74 61 72 74  t rc;.  va_start
2bcdf 28 61 70 2c 20 6f 70 29 3b 0a 20 20 73 77 69 74  (ap, op);.  swit
2bce0 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61  ch( op ){.    ca
2bce1 73 65 20 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46  se SQLITE_DBCONF
2bce2 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 3a 20 7b 0a  IG_LOOKASIDE: {.
2bce3 20 20 20 20 20 20 76 6f 69 64 20 2a 70 42 75 66        void *pBuf
2bce4 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 76 6f   = va_arg(ap, vo
2bce5 69 64 2a 29 3b 0a 20 20 20 20 20 20 69 6e 74 20  id*);.      int 
2bce6 73 7a 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  sz = va_arg(ap, 
2bce7 69 6e 74 29 3b 0a 20 20 20 20 20 20 69 6e 74 20  int);.      int 
2bce8 63 6e 74 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  cnt = va_arg(ap,
2bce9 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 72 63 20   int);.      rc 
2bcea 3d 20 73 65 74 75 70 4c 6f 6f 6b 61 73 69 64 65  = setupLookaside
2bceb 28 64 62 2c 20 70 42 75 66 2c 20 73 7a 2c 20 63  (db, pBuf, sz, c
2bcec 6e 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  nt);.      break
2bced 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
2bcee 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 63 20  ult: {.      rc 
2bcef 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
2bcf0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2bcf1 20 7d 0a 20 20 7d 0a 20 20 76 61 5f 65 6e 64 28   }.  }.  va_end(
2bcf2 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ap);.  return rc
2bcf3 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69  ;.}../*.** Routi
2bcf4 6e 65 20 6e 65 65 64 65 64 20 74 6f 20 73 75 70  ne needed to sup
2bcf5 70 6f 72 74 20 74 68 65 20 74 65 73 74 63 61 73  port the testcas
2bcf6 65 28 29 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69  e() macro..*/.#i
2bcf7 66 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 56 45  fdef SQLITE_COVE
2bcf8 52 41 47 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  RAGE_TEST.SQLITE
2bcf9 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2bcfa 6c 69 74 65 33 43 6f 76 65 72 61 67 65 28 69 6e  lite3Coverage(in
2bcfb 74 20 78 29 7b 0a 20 20 73 74 61 74 69 63 20 69  t x){.  static i
2bcfc 6e 74 20 64 75 6d 6d 79 20 3d 20 30 3b 0a 20 20  nt dummy = 0;.  
2bcfd 64 75 6d 6d 79 20 2b 3d 20 78 3b 0a 7d 0a 23 65  dummy += x;.}.#e
2bcfe 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ndif.../*.** Ret
2bcff 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20  urn true if the 
2bd00 62 75 66 66 65 72 20 7a 5b 30 2e 2e 6e 2d 31 5d  buffer z[0..n-1]
2bd01 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 73 70   contains all sp
2bd02 61 63 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  aces..*/.static 
2bd03 69 6e 74 20 61 6c 6c 53 70 61 63 65 73 28 63 6f  int allSpaces(co
2bd04 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
2bd05 20 6e 29 7b 0a 20 20 77 68 69 6c 65 28 20 6e 3e   n){.  while( n>
2bd06 30 20 26 26 20 7a 5b 6e 2d 31 5d 3d 3d 27 20 27  0 && z[n-1]==' '
2bd07 20 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 72 65 74   ){ n--; }.  ret
2bd08 75 72 6e 20 6e 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a  urn n==0;.}../*.
2bd09 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 64  ** This is the d
2bd0a 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6e 67  efault collating
2bd0b 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20   function named 
2bd0c 22 42 49 4e 41 52 59 22 20 77 68 69 63 68 20 69  "BINARY" which i
2bd0d 73 20 61 6c 77 61 79 73 0a 2a 2a 20 61 76 61 69  s always.** avai
2bd0e 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  lable..**.** If 
2bd0f 74 68 65 20 70 61 64 46 6c 61 67 20 61 72 67 75  the padFlag argu
2bd10 6d 65 6e 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  ment is not NULL
2bd11 20 74 68 65 6e 20 73 70 61 63 65 20 70 61 64 64   then space padd
2bd12 69 6e 67 20 61 74 20 74 68 65 20 65 6e 64 0a 2a  ing at the end.*
2bd13 2a 20 6f 66 20 73 74 72 69 6e 67 73 20 69 73 20  * of strings is 
2bd14 69 67 6e 6f 72 65 64 2e 20 20 54 68 69 73 20 69  ignored.  This i
2bd15 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 52 54  mplements the RT
2bd16 52 49 4d 20 63 6f 6c 6c 61 74 69 6f 6e 2e 0a 2a  RIM collation..*
2bd17 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 69 6e  /.static int bin
2bd18 43 6f 6c 6c 46 75 6e 63 28 0a 20 20 76 6f 69 64  CollFunc(.  void
2bd19 20 2a 70 61 64 46 6c 61 67 2c 0a 20 20 69 6e 74   *padFlag,.  int
2bd1a 20 6e 4b 65 79 31 2c 20 63 6f 6e 73 74 20 76 6f   nKey1, const vo
2bd1b 69 64 20 2a 70 4b 65 79 31 2c 0a 20 20 69 6e 74  id *pKey1,.  int
2bd1c 20 6e 4b 65 79 32 2c 20 63 6f 6e 73 74 20 76 6f   nKey2, const vo
2bd1d 69 64 20 2a 70 4b 65 79 32 0a 29 7b 0a 20 20 69  id *pKey2.){.  i
2bd1e 6e 74 20 72 63 2c 20 6e 3b 0a 20 20 6e 20 3d 20  nt rc, n;.  n = 
2bd1f 6e 4b 65 79 31 3c 6e 4b 65 79 32 20 3f 20 6e 4b  nKey1<nKey2 ? nK
2bd20 65 79 31 20 3a 20 6e 4b 65 79 32 3b 0a 20 20 72  ey1 : nKey2;.  r
2bd21 63 20 3d 20 6d 65 6d 63 6d 70 28 70 4b 65 79 31  c = memcmp(pKey1
2bd22 2c 20 70 4b 65 79 32 2c 20 6e 29 3b 0a 20 20 69  , pKey2, n);.  i
2bd23 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20  f( rc==0 ){.    
2bd24 69 66 28 20 70 61 64 46 6c 61 67 0a 20 20 20 20  if( padFlag.    
2bd25 20 26 26 20 61 6c 6c 53 70 61 63 65 73 28 28 28   && allSpaces(((
2bd26 63 68 61 72 2a 29 70 4b 65 79 31 29 2b 6e 2c 20  char*)pKey1)+n, 
2bd27 6e 4b 65 79 31 2d 6e 29 0a 20 20 20 20 20 26 26  nKey1-n).     &&
2bd28 20 61 6c 6c 53 70 61 63 65 73 28 28 28 63 68 61   allSpaces(((cha
2bd29 72 2a 29 70 4b 65 79 32 29 2b 6e 2c 20 6e 4b 65  r*)pKey2)+n, nKe
2bd2a 79 32 2d 6e 29 0a 20 20 20 20 29 7b 0a 20 20 20  y2-n).    ){.   
2bd2b 20 20 20 2f 2a 20 4c 65 61 76 65 20 72 63 20 75     /* Leave rc u
2bd2c 6e 63 68 61 6e 67 65 64 20 61 74 20 30 20 2a 2f  nchanged at 0 */
2bd2d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2bd2e 20 20 72 63 20 3d 20 6e 4b 65 79 31 20 2d 20 6e    rc = nKey1 - n
2bd2f 4b 65 79 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Key2;.    }.  }.
2bd30 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
2bd31 2f 2a 0a 2a 2a 20 41 6e 6f 74 68 65 72 20 62 75  /*.** Another bu
2bd32 69 6c 74 2d 69 6e 20 63 6f 6c 6c 61 74 69 6e 67  ilt-in collating
2bd33 20 73 65 71 75 65 6e 63 65 3a 20 4e 4f 43 41 53   sequence: NOCAS
2bd34 45 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63  E. .**.** This c
2bd35 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
2bd36 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f  e is intended to
2bd37 20 62 65 20 75 73 65 64 20 66 6f 72 20 22 63 61   be used for "ca
2bd38 73 65 20 69 6e 64 65 70 65 6e 64 61 6e 74 0a 2a  se independant.*
2bd39 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 22 2e 20 53  * comparison". S
2bd3a 51 4c 69 74 65 27 73 20 6b 6e 6f 77 6c 65 64 67  QLite's knowledg
2bd3b 65 20 6f 66 20 75 70 70 65 72 20 61 6e 64 20 6c  e of upper and l
2bd3c 6f 77 65 72 20 63 61 73 65 20 65 71 75 69 76 61  ower case equiva
2bd3d 6c 65 6e 74 73 0a 2a 2a 20 65 78 74 65 6e 64 73  lents.** extends
2bd3e 20 6f 6e 6c 79 20 74 6f 20 74 68 65 20 32 36 20   only to the 26 
2bd3f 63 68 61 72 61 63 74 65 72 73 20 75 73 65 64 20  characters used 
2bd40 69 6e 20 74 68 65 20 45 6e 67 6c 69 73 68 20 6c  in the English l
2bd41 61 6e 67 75 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 41  anguage..**.** A
2bd42 74 20 74 68 65 20 6d 6f 6d 65 6e 74 20 74 68 65  t the moment the
2bd43 72 65 20 69 73 20 6f 6e 6c 79 20 61 20 55 54 46  re is only a UTF
2bd44 2d 38 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  -8 implementatio
2bd45 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  n..*/.static int
2bd46 20 6e 6f 63 61 73 65 43 6f 6c 6c 61 74 69 6e 67   nocaseCollating
2bd47 46 75 6e 63 28 0a 20 20 76 6f 69 64 20 2a 4e 6f  Func(.  void *No
2bd48 74 55 73 65 64 2c 0a 20 20 69 6e 74 20 6e 4b 65  tUsed,.  int nKe
2bd49 79 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  y1, const void *
2bd4a 70 4b 65 79 31 2c 0a 20 20 69 6e 74 20 6e 4b 65  pKey1,.  int nKe
2bd4b 79 32 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  y2, const void *
2bd4c 70 4b 65 79 32 0a 29 7b 0a 20 20 69 6e 74 20 72  pKey2.){.  int r
2bd4d 20 3d 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43   = sqlite3StrNIC
2bd4e 6d 70 28 0a 20 20 20 20 20 20 28 63 6f 6e 73 74  mp(.      (const
2bd4f 20 63 68 61 72 20 2a 29 70 4b 65 79 31 2c 20 28   char *)pKey1, (
2bd50 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 4b 65  const char *)pKe
2bd51 79 32 2c 20 28 6e 4b 65 79 31 3c 6e 4b 65 79 32  y2, (nKey1<nKey2
2bd52 29 3f 6e 4b 65 79 31 3a 6e 4b 65 79 32 29 3b 0a  )?nKey1:nKey2);.
2bd53 20 20 69 66 28 20 30 3d 3d 72 20 29 7b 0a 20 20    if( 0==r ){.  
2bd54 20 20 72 20 3d 20 6e 4b 65 79 31 2d 6e 4b 65 79    r = nKey1-nKey
2bd55 32 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  2;.  }.  return 
2bd56 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  r;.}../*.** Retu
2bd57 72 6e 20 74 68 65 20 52 4f 57 49 44 20 6f 66 20  rn the ROWID of 
2bd58 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
2bd59 69 6e 73 65 72 74 0a 2a 2f 0a 53 51 4c 49 54 45  insert.*/.SQLITE
2bd5a 5f 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 36  _API sqlite_int6
2bd5b 34 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69  4 sqlite3_last_i
2bd5c 6e 73 65 72 74 5f 72 6f 77 69 64 28 73 71 6c 69  nsert_rowid(sqli
2bd5d 74 65 33 20 2a 64 62 29 7b 0a 20 20 72 65 74 75  te3 *db){.  retu
2bd5e 72 6e 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64  rn db->lastRowid
2bd5f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
2bd60 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
2bd61 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 6d  changes in the m
2bd62 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20  ost recent call 
2bd63 74 6f 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  to sqlite3_exec(
2bd64 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  )..*/.SQLITE_API
2bd65 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 68 61   int sqlite3_cha
2bd66 6e 67 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62  nges(sqlite3 *db
2bd67 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e  ){.  return db->
2bd68 6e 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  nChange;.}../*.*
2bd69 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
2bd6a 62 65 72 20 6f 66 20 63 68 61 6e 67 65 73 20 73  ber of changes s
2bd6b 69 6e 63 65 20 74 68 65 20 64 61 74 61 62 61 73  ince the databas
2bd6c 65 20 68 61 6e 64 6c 65 20 77 61 73 20 6f 70 65  e handle was ope
2bd6d 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ned..*/.SQLITE_A
2bd6e 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74  PI int sqlite3_t
2bd6f 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 73 71 6c  otal_changes(sql
2bd70 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 72 65 74  ite3 *db){.  ret
2bd71 75 72 6e 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 68  urn db->nTotalCh
2bd72 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ange;.}../*.** C
2bd73 6c 6f 73 65 20 61 6e 20 65 78 69 73 74 69 6e 67  lose an existing
2bd74 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65   SQLite database
2bd75 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
2bd76 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65  nt sqlite3_close
2bd77 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
2bd78 20 48 61 73 68 45 6c 65 6d 20 2a 69 3b 0a 20 20   HashElem *i;.  
2bd79 69 6e 74 20 6a 3b 0a 0a 20 20 69 66 28 20 21 64  int j;..  if( !d
2bd7a 62 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  b ){.    return 
2bd7b 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
2bd7c 20 69 66 28 20 21 73 71 6c 69 74 65 33 53 61 66   if( !sqlite3Saf
2bd7d 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b  etyCheckSickOrOk
2bd7e 28 64 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75  (db) ){.    retu
2bd7f 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  rn SQLITE_MISUSE
2bd80 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
2bd81 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
2bd82 6d 75 74 65 78 29 3b 0a 0a 23 69 66 64 65 66 20  mutex);..#ifdef 
2bd83 53 51 4c 49 54 45 5f 53 53 45 0a 20 20 7b 0a 20  SQLITE_SSE.  {. 
2bd84 20 20 20 65 78 74 65 72 6e 20 76 6f 69 64 20 73     extern void s
2bd85 71 6c 69 74 65 33 53 73 65 43 6c 65 61 6e 75 70  qlite3SseCleanup
2bd86 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 20 20  (sqlite3*);.    
2bd87 73 71 6c 69 74 65 33 53 73 65 43 6c 65 61 6e 75  sqlite3SseCleanu
2bd88 70 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  p(db);.  }.#endi
2bd89 66 20 0a 0a 20 20 73 71 6c 69 74 65 33 52 65 73  f ..  sqlite3Res
2bd8a 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
2bd8b 28 64 62 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 49  (db, 0);..  /* I
2bd8c 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  f a transaction 
2bd8d 69 73 20 6f 70 65 6e 2c 20 74 68 65 20 52 65 73  is open, the Res
2bd8e 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
2bd8f 28 29 20 63 61 6c 6c 20 61 62 6f 76 65 0a 20 20  () call above.  
2bd90 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 68 61 76 65  ** will not have
2bd91 20 63 61 6c 6c 65 64 20 74 68 65 20 78 44 69 73   called the xDis
2bd92 63 6f 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f 64  connect() method
2bd93 20 6f 6e 20 61 6e 79 20 76 69 72 74 75 61 6c 0a   on any virtual.
2bd94 20 20 2a 2a 20 74 61 62 6c 65 73 20 69 6e 20 74    ** tables in t
2bd95 68 65 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 5d  he db->aVTrans[]
2bd96 20 61 72 72 61 79 2e 20 54 68 65 20 66 6f 6c 6c   array. The foll
2bd97 6f 77 69 6e 67 20 73 71 6c 69 74 65 33 56 74 61  owing sqlite3Vta
2bd98 62 52 6f 6c 6c 62 61 63 6b 28 29 0a 20 20 2a 2a  bRollback().  **
2bd99 20 63 61 6c 6c 20 77 69 6c 6c 20 64 6f 20 73 6f   call will do so
2bd9a 2e 20 57 65 20 6e 65 65 64 20 74 6f 20 64 6f 20  . We need to do 
2bd9b 74 68 69 73 20 62 65 66 6f 72 65 20 74 68 65 20  this before the 
2bd9c 63 68 65 63 6b 20 66 6f 72 20 61 63 74 69 76 65  check for active
2bd9d 0a 20 20 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d  .  ** SQL statem
2bd9e 65 6e 74 73 20 62 65 6c 6f 77 2c 20 61 73 20 74  ents below, as t
2bd9f 68 65 20 76 2d 74 61 62 6c 65 20 69 6d 70 6c 65  he v-table imple
2bda0 6d 65 6e 74 61 74 69 6f 6e 20 6d 61 79 20 62 65  mentation may be
2bda1 20 73 74 6f 72 69 6e 67 0a 20 20 2a 2a 20 73 6f   storing.  ** so
2bda2 6d 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74  me prepared stat
2bda3 65 6d 65 6e 74 73 20 69 6e 74 65 72 6e 61 6c 6c  ements internall
2bda4 79 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  y..  */.  sqlite
2bda5 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 64 62  3VtabRollback(db
2bda6 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72  );..  /* If ther
2bda7 65 20 61 72 65 20 61 6e 79 20 6f 75 74 73 74 61  e are any outsta
2bda8 6e 64 69 6e 67 20 56 4d 73 2c 20 72 65 74 75 72  nding VMs, retur
2bda9 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 2a  n SQLITE_BUSY. *
2bdaa 2f 0a 20 20 69 66 28 20 64 62 2d 3e 70 56 64 62  /.  if( db->pVdb
2bdab 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  e ){.    sqlite3
2bdac 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45  Error(db, SQLITE
2bdad 5f 42 55 53 59 2c 20 0a 20 20 20 20 20 20 20 20  _BUSY, .        
2bdae 22 55 6e 61 62 6c 65 20 74 6f 20 63 6c 6f 73 65  "Unable to close
2bdaf 20 64 75 65 20 74 6f 20 75 6e 66 69 6e 61 6c 69   due to unfinali
2bdb0 73 65 64 20 73 74 61 74 65 6d 65 6e 74 73 22 29  sed statements")
2bdb1 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
2bdb2 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
2bdb3 74 65 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  tex);.    return
2bdb4 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
2bdb5 7d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  }.  assert( sqli
2bdb6 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 53 69  te3SafetyCheckSi
2bdb7 63 6b 4f 72 4f 6b 28 64 62 29 20 29 3b 0a 0a 20  ckOrOk(db) );.. 
2bdb8 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 64 62 2d 3e   for(j=0; j<db->
2bdb9 6e 44 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 73  nDb; j++){.    s
2bdba 74 72 75 63 74 20 44 62 20 2a 70 44 62 20 3d 20  truct Db *pDb = 
2bdbb 26 64 62 2d 3e 61 44 62 5b 6a 5d 3b 0a 20 20 20  &db->aDb[j];.   
2bdbc 20 69 66 28 20 70 44 62 2d 3e 70 42 74 20 29 7b   if( pDb->pBt ){
2bdbd 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  .      sqlite3Bt
2bdbe 72 65 65 43 6c 6f 73 65 28 70 44 62 2d 3e 70 42  reeClose(pDb->pB
2bdbf 74 29 3b 0a 20 20 20 20 20 20 70 44 62 2d 3e 70  t);.      pDb->p
2bdc0 42 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66  Bt = 0;.      if
2bdc1 28 20 6a 21 3d 31 20 29 7b 0a 20 20 20 20 20 20  ( j!=1 ){.      
2bdc2 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 20 3d    pDb->pSchema =
2bdc3 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
2bdc4 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52  }.  }.  sqlite3R
2bdc5 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65  esetInternalSche
2bdc6 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 61 73 73  ma(db, 0);.  ass
2bdc7 65 72 74 28 20 64 62 2d 3e 6e 44 62 3c 3d 32 20  ert( db->nDb<=2 
2bdc8 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d  );.  assert( db-
2bdc9 3e 61 44 62 3d 3d 64 62 2d 3e 61 44 62 53 74 61  >aDb==db->aDbSta
2bdca 74 69 63 20 29 3b 0a 20 20 66 6f 72 28 6a 3d 30  tic );.  for(j=0
2bdcb 3b 20 6a 3c 41 72 72 61 79 53 69 7a 65 28 64 62  ; j<ArraySize(db
2bdcc 2d 3e 61 46 75 6e 63 2e 61 29 3b 20 6a 2b 2b 29  ->aFunc.a); j++)
2bdcd 7b 0a 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70  {.    FuncDef *p
2bdce 4e 65 78 74 2c 20 2a 70 48 61 73 68 2c 20 2a 70  Next, *pHash, *p
2bdcf 3b 0a 20 20 20 20 66 6f 72 28 70 3d 64 62 2d 3e  ;.    for(p=db->
2bdd0 61 46 75 6e 63 2e 61 5b 6a 5d 3b 20 70 3b 20 70  aFunc.a[j]; p; p
2bdd1 3d 70 48 61 73 68 29 7b 0a 20 20 20 20 20 20 70  =pHash){.      p
2bdd2 48 61 73 68 20 3d 20 70 2d 3e 70 48 61 73 68 3b  Hash = p->pHash;
2bdd3 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 20  .      while( p 
2bdd4 29 7b 0a 20 20 20 20 20 20 20 20 70 4e 65 78 74  ){.        pNext
2bdd5 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20   = p->pNext;.   
2bdd6 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
2bdd7 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20  ee(db, p);.     
2bdd8 20 20 20 70 20 3d 20 70 4e 65 78 74 3b 0a 20 20     p = pNext;.  
2bdd9 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
2bdda 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61    for(i=sqliteHa
2bddb 73 68 46 69 72 73 74 28 26 64 62 2d 3e 61 43 6f  shFirst(&db->aCo
2bddc 6c 6c 53 65 71 29 3b 20 69 3b 20 69 3d 73 71 6c  llSeq); i; i=sql
2bddd 69 74 65 48 61 73 68 4e 65 78 74 28 69 29 29 7b  iteHashNext(i)){
2bdde 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43  .    CollSeq *pC
2bddf 6f 6c 6c 20 3d 20 28 43 6f 6c 6c 53 65 71 20 2a  oll = (CollSeq *
2bde0 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28  )sqliteHashData(
2bde1 69 29 3b 0a 20 20 20 20 2f 2a 20 49 6e 76 6f 6b  i);.    /* Invok
2bde2 65 20 61 6e 79 20 64 65 73 74 72 75 63 74 6f 72  e any destructor
2bde3 73 20 72 65 67 69 73 74 65 72 65 64 20 66 6f 72  s registered for
2bde4 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
2bde5 6e 63 65 20 75 73 65 72 20 64 61 74 61 2e 20 2a  nce user data. *
2bde6 2f 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a  /.    for(j=0; j
2bde7 3c 33 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  <3; j++){.      
2bde8 69 66 28 20 70 43 6f 6c 6c 5b 6a 5d 2e 78 44 65  if( pColl[j].xDe
2bde9 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f  l ){.        pCo
2bdea 6c 6c 5b 6a 5d 2e 78 44 65 6c 28 70 43 6f 6c 6c  ll[j].xDel(pColl
2bdeb 5b 6a 5d 2e 70 55 73 65 72 29 3b 0a 20 20 20 20  [j].pUser);.    
2bdec 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71    }.    }.    sq
2bded 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2bdee 70 43 6f 6c 6c 29 3b 0a 20 20 7d 0a 20 20 73 71  pColl);.  }.  sq
2bdef 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 26  lite3HashClear(&
2bdf0 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 29 3b 0a 23  db->aCollSeq);.#
2bdf1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2bdf2 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
2bdf3 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61    for(i=sqliteHa
2bdf4 73 68 46 69 72 73 74 28 26 64 62 2d 3e 61 4d 6f  shFirst(&db->aMo
2bdf5 64 75 6c 65 29 3b 20 69 3b 20 69 3d 73 71 6c 69  dule); i; i=sqli
2bdf6 74 65 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a  teHashNext(i)){.
2bdf7 20 20 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64      Module *pMod
2bdf8 20 3d 20 28 4d 6f 64 75 6c 65 20 2a 29 73 71 6c   = (Module *)sql
2bdf9 69 74 65 48 61 73 68 44 61 74 61 28 69 29 3b 0a  iteHashData(i);.
2bdfa 20 20 20 20 69 66 28 20 70 4d 6f 64 2d 3e 78 44      if( pMod->xD
2bdfb 65 73 74 72 6f 79 20 29 7b 0a 20 20 20 20 20 20  estroy ){.      
2bdfc 70 4d 6f 64 2d 3e 78 44 65 73 74 72 6f 79 28 70  pMod->xDestroy(p
2bdfd 4d 6f 64 2d 3e 70 41 75 78 29 3b 0a 20 20 20 20  Mod->pAux);.    
2bdfe 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  }.    sqlite3DbF
2bdff 72 65 65 28 64 62 2c 20 70 4d 6f 64 29 3b 0a 20  ree(db, pMod);. 
2be00 20 7d 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68   }.  sqlite3Hash
2be01 43 6c 65 61 72 28 26 64 62 2d 3e 61 4d 6f 64 75  Clear(&db->aModu
2be02 6c 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 73  le);.#endif..  s
2be03 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
2be04 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 20 2f  SQLITE_OK, 0); /
2be05 2a 20 44 65 61 6c 6c 6f 63 61 74 65 73 20 61 6e  * Deallocates an
2be06 79 20 63 61 63 68 65 64 20 65 72 72 6f 72 20 73  y cached error s
2be07 74 72 69 6e 67 73 2e 20 2a 2f 0a 20 20 69 66 28  trings. */.  if(
2be08 20 64 62 2d 3e 70 45 72 72 20 29 7b 0a 20 20 20   db->pErr ){.   
2be09 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65   sqlite3ValueFre
2be0a 65 28 64 62 2d 3e 70 45 72 72 29 3b 0a 20 20 7d  e(db->pErr);.  }
2be0b 0a 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45  .  sqlite3CloseE
2be0c 78 74 65 6e 73 69 6f 6e 73 28 64 62 29 3b 0a 0a  xtensions(db);..
2be0d 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51    db->magic = SQ
2be0e 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52  LITE_MAGIC_ERROR
2be0f 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 74 65 6d 70  ;..  /* The temp
2be10 2d 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61  -database schema
2be11 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 64 69   is allocated di
2be12 66 66 65 72 65 6e 74 6c 79 20 66 72 6f 6d 20 74  fferently from t
2be13 68 65 20 6f 74 68 65 72 20 73 63 68 65 6d 61 0a  he other schema.
2be14 20 20 2a 2a 20 6f 62 6a 65 63 74 73 20 28 75 73    ** objects (us
2be15 69 6e 67 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63  ing sqliteMalloc
2be16 28 29 20 64 69 72 65 63 74 6c 79 2c 20 69 6e 73  () directly, ins
2be17 74 65 61 64 20 6f 66 20 73 71 6c 69 74 65 33 42  tead of sqlite3B
2be18 74 72 65 65 53 63 68 65 6d 61 28 29 29 2e 0a 20  treeSchema()).. 
2be19 20 2a 2a 20 53 6f 20 69 74 20 6e 65 65 64 73 20   ** So it needs 
2be1a 74 6f 20 62 65 20 66 72 65 65 64 20 68 65 72 65  to be freed here
2be1b 2e 20 54 6f 64 6f 3a 20 57 68 79 20 6e 6f 74 20  . Todo: Why not 
2be1c 72 6f 6c 6c 20 74 68 65 20 74 65 6d 70 20 73 63  roll the temp sc
2be1d 68 65 6d 61 20 69 6e 74 6f 0a 20 20 2a 2a 20 74  hema into.  ** t
2be1e 68 65 20 73 61 6d 65 20 73 71 6c 69 74 65 4d 61  he same sqliteMa
2be1f 6c 6c 6f 63 28 29 20 61 73 20 74 68 65 20 6f 6e  lloc() as the on
2be20 65 20 74 68 61 74 20 61 6c 6c 6f 63 61 74 65 73  e that allocates
2be21 20 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 20   the database . 
2be22 20 2a 2a 20 73 74 72 75 63 74 75 72 65 3f 0a 20   ** structure?. 
2be23 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 44 62 46   */.  sqlite3DbF
2be24 72 65 65 28 64 62 2c 20 64 62 2d 3e 61 44 62 5b  ree(db, db->aDb[
2be25 31 5d 2e 70 53 63 68 65 6d 61 29 3b 0a 20 20 73  1].pSchema);.  s
2be26 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
2be27 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
2be28 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c   db->magic = SQL
2be29 49 54 45 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 44  ITE_MAGIC_CLOSED
2be2a 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
2be2b 78 5f 66 72 65 65 28 64 62 2d 3e 6d 75 74 65 78  x_free(db->mutex
2be2c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d  );.  assert( db-
2be2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3d  >lookaside.nOut=
2be2e 3d 30 20 29 3b 20 20 2f 2a 20 46 61 69 6c 73 20  =0 );  /* Fails 
2be2f 6f 6e 20 61 20 6c 6f 6f 6b 61 73 69 64 65 20 6d  on a lookaside m
2be30 65 6d 6f 72 79 20 6c 65 61 6b 20 2a 2f 0a 20 20  emory leak */.  
2be31 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  if( db->lookasid
2be32 65 2e 62 4d 61 6c 6c 6f 63 65 64 20 29 7b 0a 20  e.bMalloced ){. 
2be33 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
2be34 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53  db->lookaside.pS
2be35 74 61 72 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  tart);.  }.  sql
2be36 69 74 65 33 5f 66 72 65 65 28 64 62 29 3b 0a 20  ite3_free(db);. 
2be37 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
2be38 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c  K;.}../*.** Roll
2be39 62 61 63 6b 20 61 6c 6c 20 64 61 74 61 62 61 73  back all databas
2be3a 65 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49  e files..*/.SQLI
2be3b 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2be3c 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41  sqlite3RollbackA
2be3d 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  ll(sqlite3 *db){
2be3e 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
2be3f 69 6e 54 72 61 6e 73 20 3d 20 30 3b 0a 20 20 61  inTrans = 0;.  a
2be40 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
2be41 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75  utex_held(db->mu
2be42 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65  tex) );.  sqlite
2be43 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c  3BeginBenignMall
2be44 6f 63 28 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  oc();.  for(i=0;
2be45 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
2be46 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44  {.    if( db->aD
2be47 62 5b 69 5d 2e 70 42 74 20 29 7b 0a 20 20 20 20  b[i].pBt ){.    
2be48 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74 72    if( sqlite3Btr
2be49 65 65 49 73 49 6e 54 72 61 6e 73 28 64 62 2d 3e  eeIsInTrans(db->
2be4a 61 44 62 5b 69 5d 2e 70 42 74 29 20 29 7b 0a 20  aDb[i].pBt) ){. 
2be4b 20 20 20 20 20 20 20 69 6e 54 72 61 6e 73 20 3d         inTrans =
2be4c 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
2be4d 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f    sqlite3BtreeRo
2be4e 6c 6c 62 61 63 6b 28 64 62 2d 3e 61 44 62 5b 69  llback(db->aDb[i
2be4f 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20 20 64 62  ].pBt);.      db
2be50 2d 3e 61 44 62 5b 69 5d 2e 69 6e 54 72 61 6e 73  ->aDb[i].inTrans
2be51 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
2be52 20 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c    sqlite3VtabRol
2be53 6c 62 61 63 6b 28 64 62 29 3b 0a 20 20 73 71 6c  lback(db);.  sql
2be54 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
2be55 6c 6f 63 28 29 3b 0a 0a 20 20 69 66 28 20 64 62  loc();..  if( db
2be56 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 49  ->flags&SQLITE_I
2be57 6e 74 65 72 6e 43 68 61 6e 67 65 73 20 29 7b 0a  nternChanges ){.
2be58 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72      sqlite3Expir
2be59 65 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65  ePreparedStateme
2be5a 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 73 71 6c  nts(db);.    sql
2be5b 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
2be5c 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a  lSchema(db, 0);.
2be5d 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65    }..  /* If one
2be5e 20 68 61 73 20 62 65 65 6e 20 63 6f 6e 66 69 67   has been config
2be5f 75 72 65 64 2c 20 69 6e 76 6f 6b 65 20 74 68 65  ured, invoke the
2be60 20 72 6f 6c 6c 62 61 63 6b 2d 68 6f 6f 6b 20 63   rollback-hook c
2be61 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 66 28  allback */.  if(
2be62 20 64 62 2d 3e 78 52 6f 6c 6c 62 61 63 6b 43 61   db->xRollbackCa
2be63 6c 6c 62 61 63 6b 20 26 26 20 28 69 6e 54 72 61  llback && (inTra
2be64 6e 73 20 7c 7c 20 21 64 62 2d 3e 61 75 74 6f 43  ns || !db->autoC
2be65 6f 6d 6d 69 74 29 20 29 7b 0a 20 20 20 20 64 62  ommit) ){.    db
2be66 2d 3e 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62  ->xRollbackCallb
2be67 61 63 6b 28 64 62 2d 3e 70 52 6f 6c 6c 62 61 63  ack(db->pRollbac
2be68 6b 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  kArg);.  }.}../*
2be69 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 73 74 61  .** Return a sta
2be6a 74 69 63 20 73 74 72 69 6e 67 20 74 68 61 74 20  tic string that 
2be6b 64 65 73 63 72 69 62 65 73 20 74 68 65 20 6b 69  describes the ki
2be6c 6e 64 20 6f 66 20 65 72 72 6f 72 20 73 70 65 63  nd of error spec
2be6d 69 66 69 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20  ified in the.** 
2be6e 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c  argument..*/.SQL
2be6f 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
2be70 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 45  t char *sqlite3E
2be71 72 72 53 74 72 28 69 6e 74 20 72 63 29 7b 0a 20  rrStr(int rc){. 
2be72 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a   const char *z;.
2be73 20 20 73 77 69 74 63 68 28 20 72 63 20 26 20 30    switch( rc & 0
2be74 78 66 66 20 29 7b 0a 20 20 20 20 63 61 73 65 20  xff ){.    case 
2be75 53 51 4c 49 54 45 5f 52 4f 57 3a 0a 20 20 20 20  SQLITE_ROW:.    
2be76 63 61 73 65 20 53 51 4c 49 54 45 5f 44 4f 4e 45  case SQLITE_DONE
2be77 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  :.    case SQLIT
2be78 45 5f 4f 4b 3a 20 20 20 20 20 20 20 20 20 7a 20  E_OK:         z 
2be79 3d 20 22 6e 6f 74 20 61 6e 20 65 72 72 6f 72 22  = "not an error"
2be7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2be7b 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
2be7c 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2be7d 45 5f 45 52 52 4f 52 3a 20 20 20 20 20 20 7a 20  E_ERROR:      z 
2be7e 3d 20 22 53 51 4c 20 6c 6f 67 69 63 20 65 72 72  = "SQL logic err
2be7f 6f 72 20 6f 72 20 6d 69 73 73 69 6e 67 20 64 61  or or missing da
2be80 74 61 62 61 73 65 22 3b 20 20 20 62 72 65 61 6b  tabase";   break
2be81 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2be82 45 5f 50 45 52 4d 3a 20 20 20 20 20 20 20 7a 20  E_PERM:       z 
2be83 3d 20 22 61 63 63 65 73 73 20 70 65 72 6d 69 73  = "access permis
2be84 73 69 6f 6e 20 64 65 6e 69 65 64 22 3b 20 20 20  sion denied";   
2be85 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
2be86 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2be87 45 5f 41 42 4f 52 54 3a 20 20 20 20 20 20 7a 20  E_ABORT:      z 
2be88 3d 20 22 63 61 6c 6c 62 61 63 6b 20 72 65 71 75  = "callback requ
2be89 65 73 74 65 64 20 71 75 65 72 79 20 61 62 6f 72  ested query abor
2be8a 74 22 3b 20 20 20 20 20 20 20 20 62 72 65 61 6b  t";        break
2be8b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2be8c 45 5f 42 55 53 59 3a 20 20 20 20 20 20 20 7a 20  E_BUSY:       z 
2be8d 3d 20 22 64 61 74 61 62 61 73 65 20 69 73 20 6c  = "database is l
2be8e 6f 63 6b 65 64 22 3b 20 20 20 20 20 20 20 20 20  ocked";         
2be8f 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
2be90 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2be91 45 5f 4c 4f 43 4b 45 44 3a 20 20 20 20 20 7a 20  E_LOCKED:     z 
2be92 3d 20 22 64 61 74 61 62 61 73 65 20 74 61 62 6c  = "database tabl
2be93 65 20 69 73 20 6c 6f 63 6b 65 64 22 3b 20 20 20  e is locked";   
2be94 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
2be95 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2be96 45 5f 4e 4f 4d 45 4d 3a 20 20 20 20 20 20 7a 20  E_NOMEM:      z 
2be97 3d 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79  = "out of memory
2be98 22 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ";              
2be99 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
2be9a 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2be9b 45 5f 52 45 41 44 4f 4e 4c 59 3a 20 20 20 7a 20  E_READONLY:   z 
2be9c 3d 20 22 61 74 74 65 6d 70 74 20 74 6f 20 77 72  = "attempt to wr
2be9d 69 74 65 20 61 20 72 65 61 64 6f 6e 6c 79 20 64  ite a readonly d
2be9e 61 74 61 62 61 73 65 22 3b 20 20 62 72 65 61 6b  atabase";  break
2be9f 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2bea0 45 5f 49 4e 54 45 52 52 55 50 54 3a 20 20 7a 20  E_INTERRUPT:  z 
2bea1 3d 20 22 69 6e 74 65 72 72 75 70 74 65 64 22 3b  = "interrupted";
2bea2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bea3 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
2bea4 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2bea5 45 5f 49 4f 45 52 52 3a 20 20 20 20 20 20 7a 20  E_IOERR:      z 
2bea6 3d 20 22 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f  = "disk I/O erro
2bea7 72 22 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  r";             
2bea8 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
2bea9 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2beaa 45 5f 43 4f 52 52 55 50 54 3a 20 20 20 20 7a 20  E_CORRUPT:    z 
2beab 3d 20 22 64 61 74 61 62 61 73 65 20 64 69 73 6b  = "database disk
2beac 20 69 6d 61 67 65 20 69 73 20 6d 61 6c 66 6f 72   image is malfor
2bead 6d 65 64 22 3b 20 20 20 20 20 20 62 72 65 61 6b  med";      break
2beae 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2beaf 45 5f 46 55 4c 4c 3a 20 20 20 20 20 20 20 7a 20  E_FULL:       z 
2beb0 3d 20 22 64 61 74 61 62 61 73 65 20 6f 72 20 64  = "database or d
2beb1 69 73 6b 20 69 73 20 66 75 6c 6c 22 3b 20 20 20  isk is full";   
2beb2 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
2beb3 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2beb4 45 5f 43 41 4e 54 4f 50 45 4e 3a 20 20 20 7a 20  E_CANTOPEN:   z 
2beb5 3d 20 22 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65  = "unable to ope
2beb6 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 22  n database file"
2beb7 3b 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;          break
2beb8 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2beb9 45 5f 45 4d 50 54 59 3a 20 20 20 20 20 20 7a 20  E_EMPTY:      z 
2beba 3d 20 22 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e  = "table contain
2bebb 73 20 6e 6f 20 64 61 74 61 22 3b 20 20 20 20 20  s no data";     
2bebc 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
2bebd 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2bebe 45 5f 53 43 48 45 4d 41 3a 20 20 20 20 20 7a 20  E_SCHEMA:     z 
2bebf 3d 20 22 64 61 74 61 62 61 73 65 20 73 63 68 65  = "database sche
2bec0 6d 61 20 68 61 73 20 63 68 61 6e 67 65 64 22 3b  ma has changed";
2bec1 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
2bec2 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2bec3 45 5f 54 4f 4f 42 49 47 3a 20 20 20 20 20 7a 20  E_TOOBIG:     z 
2bec4 3d 20 22 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f  = "String or BLO
2bec5 42 20 65 78 63 65 65 64 65 64 20 73 69 7a 65 20  B exceeded size 
2bec6 6c 69 6d 69 74 22 3b 20 20 20 20 62 72 65 61 6b  limit";    break
2bec7 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2bec8 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3a 20 7a 20  E_CONSTRAINT: z 
2bec9 3d 20 22 63 6f 6e 73 74 72 61 69 6e 74 20 66 61  = "constraint fa
2beca 69 6c 65 64 22 3b 20 20 20 20 20 20 20 20 20 20  iled";          
2becb 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
2becc 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2becd 45 5f 4d 49 53 4d 41 54 43 48 3a 20 20 20 7a 20  E_MISMATCH:   z 
2bece 3d 20 22 64 61 74 61 74 79 70 65 20 6d 69 73 6d  = "datatype mism
2becf 61 74 63 68 22 3b 20 20 20 20 20 20 20 20 20 20  atch";          
2bed0 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
2bed1 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2bed2 45 5f 4d 49 53 55 53 45 3a 20 20 20 20 20 7a 20  E_MISUSE:     z 
2bed3 3d 20 22 6c 69 62 72 61 72 79 20 72 6f 75 74 69  = "library routi
2bed4 6e 65 20 63 61 6c 6c 65 64 20 6f 75 74 20 6f 66  ne called out of
2bed5 20 73 65 71 75 65 6e 63 65 22 3b 62 72 65 61 6b   sequence";break
2bed6 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2bed7 45 5f 4e 4f 4c 46 53 3a 20 20 20 20 20 20 7a 20  E_NOLFS:      z 
2bed8 3d 20 22 6c 61 72 67 65 20 66 69 6c 65 20 73 75  = "large file su
2bed9 70 70 6f 72 74 20 69 73 20 64 69 73 61 62 6c 65  pport is disable
2beda 64 22 3b 20 20 20 20 20 20 20 20 62 72 65 61 6b  d";        break
2bedb 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2bedc 45 5f 41 55 54 48 3a 20 20 20 20 20 20 20 7a 20  E_AUTH:       z 
2bedd 3d 20 22 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  = "authorization
2bede 20 64 65 6e 69 65 64 22 3b 20 20 20 20 20 20 20   denied";       
2bedf 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
2bee0 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2bee1 45 5f 46 4f 52 4d 41 54 3a 20 20 20 20 20 7a 20  E_FORMAT:     z 
2bee2 3d 20 22 61 75 78 69 6c 69 61 72 79 20 64 61 74  = "auxiliary dat
2bee3 61 62 61 73 65 20 66 6f 72 6d 61 74 20 65 72 72  abase format err
2bee4 6f 72 22 3b 20 20 20 20 20 20 20 62 72 65 61 6b  or";       break
2bee5 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2bee6 45 5f 52 41 4e 47 45 3a 20 20 20 20 20 20 7a 20  E_RANGE:      z 
2bee7 3d 20 22 62 69 6e 64 20 6f 72 20 63 6f 6c 75 6d  = "bind or colum
2bee8 6e 20 69 6e 64 65 78 20 6f 75 74 20 6f 66 20 72  n index out of r
2bee9 61 6e 67 65 22 3b 20 20 20 20 20 62 72 65 61 6b  ange";     break
2beea 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
2beeb 45 5f 4e 4f 54 41 44 42 3a 20 20 20 20 20 7a 20  E_NOTADB:     z 
2beec 3d 20 22 66 69 6c 65 20 69 73 20 65 6e 63 72 79  = "file is encry
2beed 70 74 65 64 20 6f 72 20 69 73 20 6e 6f 74 20 61  pted or is not a
2beee 20 64 61 74 61 62 61 73 65 22 3b 62 72 65 61 6b   database";break
2beef 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20  ;.    default:  
2bef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 20                z 
2bef1 3d 20 22 75 6e 6b 6e 6f 77 6e 20 65 72 72 6f 72  = "unknown error
2bef2 22 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ";              
2bef3 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
2bef4 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a  ;.  }.  return z
2bef5 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
2bef6 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e  routine implemen
2bef7 74 73 20 61 20 62 75 73 79 20 63 61 6c 6c 62 61  ts a busy callba
2bef8 63 6b 20 74 68 61 74 20 73 6c 65 65 70 73 20 61  ck that sleeps a
2bef9 6e 64 20 74 72 69 65 73 0a 2a 2a 20 61 67 61 69  nd tries.** agai
2befa 6e 20 75 6e 74 69 6c 20 61 20 74 69 6d 65 6f 75  n until a timeou
2befb 74 20 76 61 6c 75 65 20 69 73 20 72 65 61 63 68  t value is reach
2befc 65 64 2e 20 20 54 68 65 20 74 69 6d 65 6f 75 74  ed.  The timeout
2befd 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61 6e 20   value is.** an 
2befe 69 6e 74 65 67 65 72 20 6e 75 6d 62 65 72 20 6f  integer number o
2beff 66 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 70  f milliseconds p
2bf00 61 73 73 65 64 20 69 6e 20 61 73 20 74 68 65 20  assed in as the 
2bf01 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e  first.** argumen
2bf02 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
2bf03 20 73 71 6c 69 74 65 44 65 66 61 75 6c 74 42 75   sqliteDefaultBu
2bf04 73 79 43 61 6c 6c 62 61 63 6b 28 0a 20 76 6f 69  syCallback(. voi
2bf05 64 20 2a 70 74 72 2c 20 20 20 20 20 20 20 20 20  d *ptr,         
2bf06 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
2bf07 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
2bf08 20 69 6e 74 20 63 6f 75 6e 74 20 20 20 20 20 20   int count      
2bf09 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
2bf0a 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 61 62  ber of times tab
2bf0b 6c 65 20 68 61 73 20 62 65 65 6e 20 62 75 73 79  le has been busy
2bf0c 20 2a 2f 0a 29 7b 0a 23 69 66 20 53 51 4c 49 54   */.){.#if SQLIT
2bf0d 45 5f 4f 53 5f 57 49 4e 20 7c 7c 20 28 64 65 66  E_OS_WIN || (def
2bf0e 69 6e 65 64 28 48 41 56 45 5f 55 53 4c 45 45 50  ined(HAVE_USLEEP
2bf0f 29 20 26 26 20 48 41 56 45 5f 55 53 4c 45 45 50  ) && HAVE_USLEEP
2bf10 29 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ).  static const
2bf11 20 75 38 20 64 65 6c 61 79 73 5b 5d 20 3d 0a 20   u8 delays[] =. 
2bf12 20 20 20 20 7b 20 31 2c 20 32 2c 20 35 2c 20 31      { 1, 2, 5, 1
2bf13 30 2c 20 31 35 2c 20 32 30 2c 20 32 35 2c 20 32  0, 15, 20, 25, 2
2bf14 35 2c 20 20 32 35 2c 20 20 35 30 2c 20 20 35 30  5,  25,  50,  50
2bf15 2c 20 31 30 30 20 7d 3b 0a 20 20 73 74 61 74 69  , 100 };.  stati
2bf16 63 20 63 6f 6e 73 74 20 75 38 20 74 6f 74 61 6c  c const u8 total
2bf17 73 5b 5d 20 3d 0a 20 20 20 20 20 7b 20 30 2c 20  s[] =.     { 0, 
2bf18 31 2c 20 33 2c 20 20 38 2c 20 31 38 2c 20 33 33  1, 3,  8, 18, 33
2bf19 2c 20 35 33 2c 20 37 38 2c 20 31 30 33 2c 20 31  , 53, 78, 103, 1
2bf1a 32 38 2c 20 31 37 38 2c 20 32 32 38 20 7d 3b 0a  28, 178, 228 };.
2bf1b 23 20 64 65 66 69 6e 65 20 4e 44 45 4c 41 59 20  # define NDELAY 
2bf1c 28 73 69 7a 65 6f 66 28 64 65 6c 61 79 73 29 2f  (sizeof(delays)/
2bf1d 73 69 7a 65 6f 66 28 64 65 6c 61 79 73 5b 30 5d  sizeof(delays[0]
2bf1e 29 29 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  )).  sqlite3 *db
2bf1f 20 3d 20 28 73 71 6c 69 74 65 33 20 2a 29 70 74   = (sqlite3 *)pt
2bf20 72 3b 0a 20 20 69 6e 74 20 74 69 6d 65 6f 75 74  r;.  int timeout
2bf21 20 3d 20 64 62 2d 3e 62 75 73 79 54 69 6d 65 6f   = db->busyTimeo
2bf22 75 74 3b 0a 20 20 69 6e 74 20 64 65 6c 61 79 2c  ut;.  int delay,
2bf23 20 70 72 69 6f 72 3b 0a 0a 20 20 61 73 73 65 72   prior;..  asser
2bf24 74 28 20 63 6f 75 6e 74 3e 3d 30 20 29 3b 0a 20  t( count>=0 );. 
2bf25 20 69 66 28 20 63 6f 75 6e 74 20 3c 20 4e 44 45   if( count < NDE
2bf26 4c 41 59 20 29 7b 0a 20 20 20 20 64 65 6c 61 79  LAY ){.    delay
2bf27 20 3d 20 64 65 6c 61 79 73 5b 63 6f 75 6e 74 5d   = delays[count]
2bf28 3b 0a 20 20 20 20 70 72 69 6f 72 20 3d 20 74 6f  ;.    prior = to
2bf29 74 61 6c 73 5b 63 6f 75 6e 74 5d 3b 0a 20 20 7d  tals[count];.  }
2bf2a 65 6c 73 65 7b 0a 20 20 20 20 64 65 6c 61 79 20  else{.    delay 
2bf2b 3d 20 64 65 6c 61 79 73 5b 4e 44 45 4c 41 59 2d  = delays[NDELAY-
2bf2c 31 5d 3b 0a 20 20 20 20 70 72 69 6f 72 20 3d 20  1];.    prior = 
2bf2d 74 6f 74 61 6c 73 5b 4e 44 45 4c 41 59 2d 31 5d  totals[NDELAY-1]
2bf2e 20 2b 20 64 65 6c 61 79 2a 28 63 6f 75 6e 74 2d   + delay*(count-
2bf2f 28 4e 44 45 4c 41 59 2d 31 29 29 3b 0a 20 20 7d  (NDELAY-1));.  }
2bf30 0a 20 20 69 66 28 20 70 72 69 6f 72 20 2b 20 64  .  if( prior + d
2bf31 65 6c 61 79 20 3e 20 74 69 6d 65 6f 75 74 20 29  elay > timeout )
2bf32 7b 0a 20 20 20 20 64 65 6c 61 79 20 3d 20 74 69  {.    delay = ti
2bf33 6d 65 6f 75 74 20 2d 20 70 72 69 6f 72 3b 0a 20  meout - prior;. 
2bf34 20 20 20 69 66 28 20 64 65 6c 61 79 3c 3d 30 20     if( delay<=0 
2bf35 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a  ) return 0;.  }.
2bf36 20 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70    sqlite3OsSleep
2bf37 28 64 62 2d 3e 70 56 66 73 2c 20 64 65 6c 61 79  (db->pVfs, delay
2bf38 2a 31 30 30 30 29 3b 0a 20 20 72 65 74 75 72 6e  *1000);.  return
2bf39 20 31 3b 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69   1;.#else.  sqli
2bf3a 74 65 33 20 2a 64 62 20 3d 20 28 73 71 6c 69 74  te3 *db = (sqlit
2bf3b 65 33 20 2a 29 70 74 72 3b 0a 20 20 69 6e 74 20  e3 *)ptr;.  int 
2bf3c 74 69 6d 65 6f 75 74 20 3d 20 28 28 73 71 6c 69  timeout = ((sqli
2bf3d 74 65 33 20 2a 29 70 74 72 29 2d 3e 62 75 73 79  te3 *)ptr)->busy
2bf3e 54 69 6d 65 6f 75 74 3b 0a 20 20 69 66 28 20 28  Timeout;.  if( (
2bf3f 63 6f 75 6e 74 2b 31 29 2a 31 30 30 30 20 3e 20  count+1)*1000 > 
2bf40 74 69 6d 65 6f 75 74 20 29 7b 0a 20 20 20 20 72  timeout ){.    r
2bf41 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73  eturn 0;.  }.  s
2bf42 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 64 62  qlite3OsSleep(db
2bf43 2d 3e 70 56 66 73 2c 20 31 30 30 30 30 30 30 29  ->pVfs, 1000000)
2bf44 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 23 65  ;.  return 1;.#e
2bf45 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  ndif.}../*.** In
2bf46 76 6f 6b 65 20 74 68 65 20 67 69 76 65 6e 20 62  voke the given b
2bf47 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 0a  usy handler..**.
2bf48 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2bf49 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61  is called when a
2bf4a 6e 20 6f 70 65 72 61 74 69 6f 6e 20 66 61 69 6c  n operation fail
2bf4b 65 64 20 77 69 74 68 20 61 20 6c 6f 63 6b 2e 0a  ed with a lock..
2bf4c 2a 2a 20 49 66 20 74 68 69 73 20 72 6f 75 74 69  ** If this routi
2bf4d 6e 65 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a  ne returns non-z
2bf4e 65 72 6f 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73  ero, the lock is
2bf4f 20 72 65 74 72 69 65 64 2e 20 20 49 66 20 69 74   retried.  If it
2bf50 0a 2a 2a 20 72 65 74 75 72 6e 73 20 30 2c 20 74  .** returns 0, t
2bf51 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 62 6f  he operation abo
2bf52 72 74 73 20 77 69 74 68 20 61 6e 20 53 51 4c 49  rts with an SQLI
2bf53 54 45 5f 42 55 53 59 20 65 72 72 6f 72 2e 0a 2a  TE_BUSY error..*
2bf54 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2bf55 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 76 6f   int sqlite3Invo
2bf56 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 42 75  keBusyHandler(Bu
2bf57 73 79 48 61 6e 64 6c 65 72 20 2a 70 29 7b 0a 20  syHandler *p){. 
2bf58 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 4e   int rc;.  if( N
2bf59 45 56 45 52 28 70 3d 3d 30 29 20 7c 7c 20 70 2d  EVER(p==0) || p-
2bf5a 3e 78 46 75 6e 63 3d 3d 30 20 7c 7c 20 70 2d 3e  >xFunc==0 || p->
2bf5b 6e 42 75 73 79 3c 30 20 29 20 72 65 74 75 72 6e  nBusy<0 ) return
2bf5c 20 30 3b 0a 20 20 72 63 20 3d 20 70 2d 3e 78 46   0;.  rc = p->xF
2bf5d 75 6e 63 28 70 2d 3e 70 41 72 67 2c 20 70 2d 3e  unc(p->pArg, p->
2bf5e 6e 42 75 73 79 29 3b 0a 20 20 69 66 28 20 72 63  nBusy);.  if( rc
2bf5f 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 42  ==0 ){.    p->nB
2bf60 75 73 79 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73  usy = -1;.  }els
2bf61 65 7b 0a 20 20 20 20 70 2d 3e 6e 42 75 73 79 2b  e{.    p->nBusy+
2bf62 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  +;.  }.  return 
2bf63 72 63 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  rc; .}../*.** Th
2bf64 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20  is routine sets 
2bf65 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63  the busy callbac
2bf66 6b 20 66 6f 72 20 61 6e 20 53 71 6c 69 74 65 20  k for an Sqlite 
2bf67 64 61 74 61 62 61 73 65 20 74 6f 20 74 68 65 0a  database to the.
2bf68 2a 2a 20 67 69 76 65 6e 20 63 61 6c 6c 62 61 63  ** given callbac
2bf69 6b 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20  k function with 
2bf6a 74 68 65 20 67 69 76 65 6e 20 61 72 67 75 6d 65  the given argume
2bf6b 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  nt..*/.SQLITE_AP
2bf6c 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75  I int sqlite3_bu
2bf6d 73 79 5f 68 61 6e 64 6c 65 72 28 0a 20 20 73 71  sy_handler(.  sq
2bf6e 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 69 6e 74  lite3 *db,.  int
2bf6f 20 28 2a 78 42 75 73 79 29 28 76 6f 69 64 2a 2c   (*xBusy)(void*,
2bf70 69 6e 74 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41  int),.  void *pA
2bf71 72 67 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  rg.){.  sqlite3_
2bf72 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
2bf73 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e 62 75  mutex);.  db->bu
2bf74 73 79 48 61 6e 64 6c 65 72 2e 78 46 75 6e 63 20  syHandler.xFunc 
2bf75 3d 20 78 42 75 73 79 3b 0a 20 20 64 62 2d 3e 62  = xBusy;.  db->b
2bf76 75 73 79 48 61 6e 64 6c 65 72 2e 70 41 72 67 20  usyHandler.pArg 
2bf77 3d 20 70 41 72 67 3b 0a 20 20 64 62 2d 3e 62 75  = pArg;.  db->bu
2bf78 73 79 48 61 6e 64 6c 65 72 2e 6e 42 75 73 79 20  syHandler.nBusy 
2bf79 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  = 0;.  sqlite3_m
2bf7a 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
2bf7b 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
2bf7c 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69  SQLITE_OK;.}..#i
2bf7d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2bf7e 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42  T_PROGRESS_CALLB
2bf7f 41 43 4b 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  ACK./*.** This r
2bf80 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20  outine sets the 
2bf81 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63  progress callbac
2bf82 6b 20 66 6f 72 20 61 6e 20 53 71 6c 69 74 65 20  k for an Sqlite 
2bf83 64 61 74 61 62 61 73 65 20 74 6f 20 74 68 65 0a  database to the.
2bf84 2a 2a 20 67 69 76 65 6e 20 63 61 6c 6c 62 61 63  ** given callbac
2bf85 6b 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20  k function with 
2bf86 74 68 65 20 67 69 76 65 6e 20 61 72 67 75 6d 65  the given argume
2bf87 6e 74 2e 20 54 68 65 20 70 72 6f 67 72 65 73 73  nt. The progress
2bf88 20 63 61 6c 6c 62 61 63 6b 20 77 69 6c 6c 0a 2a   callback will.*
2bf89 2a 20 62 65 20 69 6e 76 6f 6b 65 64 20 65 76 65  * be invoked eve
2bf8a 72 79 20 6e 4f 70 73 20 6f 70 63 6f 64 65 73 2e  ry nOps opcodes.
2bf8b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
2bf8c 6f 69 64 20 73 71 6c 69 74 65 33 5f 70 72 6f 67  oid sqlite3_prog
2bf8d 72 65 73 73 5f 68 61 6e 64 6c 65 72 28 0a 20 20  ress_handler(.  
2bf8e 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20  sqlite3 *db, .  
2bf8f 69 6e 74 20 6e 4f 70 73 2c 0a 20 20 69 6e 74 20  int nOps,.  int 
2bf90 28 2a 78 50 72 6f 67 72 65 73 73 29 28 76 6f 69  (*xProgress)(voi
2bf91 64 2a 29 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41  d*), .  void *pA
2bf92 72 67 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  rg.){.  sqlite3_
2bf93 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
2bf94 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6e 4f  mutex);.  if( nO
2bf95 70 73 3e 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e  ps>0 ){.    db->
2bf96 78 50 72 6f 67 72 65 73 73 20 3d 20 78 50 72 6f  xProgress = xPro
2bf97 67 72 65 73 73 3b 0a 20 20 20 20 64 62 2d 3e 6e  gress;.    db->n
2bf98 50 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 6e 4f  ProgressOps = nO
2bf99 70 73 3b 0a 20 20 20 20 64 62 2d 3e 70 50 72 6f  ps;.    db->pPro
2bf9a 67 72 65 73 73 41 72 67 20 3d 20 70 41 72 67 3b  gressArg = pArg;
2bf9b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62  .  }else{.    db
2bf9c 2d 3e 78 50 72 6f 67 72 65 73 73 20 3d 20 30 3b  ->xProgress = 0;
2bf9d 0a 20 20 20 20 64 62 2d 3e 6e 50 72 6f 67 72 65  .    db->nProgre
2bf9e 73 73 4f 70 73 20 3d 20 30 3b 0a 20 20 20 20 64  ssOps = 0;.    d
2bf9f 62 2d 3e 70 50 72 6f 67 72 65 73 73 41 72 67 20  b->pProgressArg 
2bfa0 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  = 0;.  }.  sqlit
2bfa1 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
2bfa2 62 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 23 65 6e  b->mutex);.}.#en
2bfa3 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  dif.../*.** This
2bfa4 20 72 6f 75 74 69 6e 65 20 69 6e 73 74 61 6c 6c   routine install
2bfa5 73 20 61 20 64 65 66 61 75 6c 74 20 62 75 73 79  s a default busy
2bfa6 20 68 61 6e 64 6c 65 72 20 74 68 61 74 20 77 61   handler that wa
2bfa7 69 74 73 20 66 6f 72 20 74 68 65 0a 2a 2a 20 73  its for the.** s
2bfa8 70 65 63 69 66 69 65 64 20 6e 75 6d 62 65 72 20  pecified number 
2bfa9 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20  of milliseconds 
2bfaa 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
2bfab 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50   0..*/.SQLITE_AP
2bfac 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75  I int sqlite3_bu
2bfad 73 79 5f 74 69 6d 65 6f 75 74 28 73 71 6c 69 74  sy_timeout(sqlit
2bfae 65 33 20 2a 64 62 2c 20 69 6e 74 20 6d 73 29 7b  e3 *db, int ms){
2bfaf 0a 20 20 69 66 28 20 6d 73 3e 30 20 29 7b 0a 20  .  if( ms>0 ){. 
2bfb0 20 20 20 64 62 2d 3e 62 75 73 79 54 69 6d 65 6f     db->busyTimeo
2bfb1 75 74 20 3d 20 6d 73 3b 0a 20 20 20 20 73 71 6c  ut = ms;.    sql
2bfb2 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65  ite3_busy_handle
2bfb3 72 28 64 62 2c 20 73 71 6c 69 74 65 44 65 66 61  r(db, sqliteDefa
2bfb4 75 6c 74 42 75 73 79 43 61 6c 6c 62 61 63 6b 2c  ultBusyCallback,
2bfb5 20 28 76 6f 69 64 2a 29 64 62 29 3b 0a 20 20 7d   (void*)db);.  }
2bfb6 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
2bfb7 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 64  3_busy_handler(d
2bfb8 62 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  b, 0, 0);.  }.  
2bfb9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
2bfba 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 75 73 65  ;.}../*.** Cause
2bfbb 20 61 6e 79 20 70 65 6e 64 69 6e 67 20 6f 70 65   any pending ope
2bfbc 72 61 74 69 6f 6e 20 74 6f 20 73 74 6f 70 20 61  ration to stop a
2bfbd 74 20 69 74 73 20 65 61 72 6c 69 65 73 74 20 6f  t its earliest o
2bfbe 70 70 6f 72 74 75 6e 69 74 79 2e 0a 2a 2f 0a 53  pportunity..*/.S
2bfbf 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
2bfc0 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
2bfc1 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
2bfc2 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72   db->u1.isInterr
2bfc3 75 70 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f  upted = 1;.}.../
2bfc4 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
2bfc5 6f 6e 20 69 73 20 65 78 61 63 74 6c 79 20 74 68  on is exactly th
2bfc6 65 20 73 61 6d 65 20 61 73 20 73 71 6c 69 74 65  e same as sqlite
2bfc7 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
2bfc8 6e 28 29 2c 20 65 78 63 65 70 74 0a 2a 2a 20 74  n(), except.** t
2bfc9 68 61 74 20 69 74 20 69 73 20 64 65 73 69 67 6e  hat it is design
2bfca 65 64 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 20  ed to be called 
2bfcb 62 79 20 69 6e 74 65 72 6e 61 6c 20 63 6f 64 65  by internal code
2bfcc 2e 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65  . The difference
2bfcd 20 69 73 0a 2a 2a 20 74 68 61 74 20 69 66 20 61   is.** that if a
2bfce 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20   malloc() fails 
2bfcf 69 6e 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  in sqlite3_creat
2bfd0 65 5f 66 75 6e 63 74 69 6f 6e 28 29 2c 20 61 6e  e_function(), an
2bfd1 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 69   error code.** i
2bfd2 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74  s returned and t
2bfd3 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  he mallocFailed 
2bfd4 66 6c 61 67 20 63 6c 65 61 72 65 64 2e 20 0a 2a  flag cleared. .*
2bfd5 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2bfd6 20 69 6e 74 20 73 71 6c 69 74 65 33 43 72 65 61   int sqlite3Crea
2bfd7 74 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  teFunc(.  sqlite
2bfd8 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63  3 *db,.  const c
2bfd9 68 61 72 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61  har *zFunctionNa
2bfda 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a  me,.  int nArg,.
2bfdb 20 20 69 6e 74 20 65 6e 63 2c 0a 20 20 76 6f 69    int enc,.  voi
2bfdc 64 20 2a 70 55 73 65 72 44 61 74 61 2c 0a 20 20  d *pUserData,.  
2bfdd 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71  void (*xFunc)(sq
2bfde 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
2bfdf 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
2bfe0 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78   **),.  void (*x
2bfe1 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f  Step)(sqlite3_co
2bfe2 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
2bfe3 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20  e3_value **),.  
2bfe4 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73  void (*xFinal)(s
2bfe5 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
2bfe6 0a 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70  .){.  FuncDef *p
2bfe7 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 0a  ;.  int nName;..
2bfe8 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
2bfe9 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d  3_mutex_held(db-
2bfea 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28  >mutex) );.  if(
2bfeb 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 3d 3d   zFunctionName==
2bfec 30 20 7c 7c 0a 20 20 20 20 20 20 28 78 46 75 6e  0 ||.      (xFun
2bfed 63 20 26 26 20 28 78 46 69 6e 61 6c 20 7c 7c 20  c && (xFinal || 
2bfee 78 53 74 65 70 29 29 20 7c 7c 20 0a 20 20 20 20  xStep)) || .    
2bfef 20 20 28 21 78 46 75 6e 63 20 26 26 20 28 78 46    (!xFunc && (xF
2bff0 69 6e 61 6c 20 26 26 20 21 78 53 74 65 70 29 29  inal && !xStep))
2bff1 20 7c 7c 0a 20 20 20 20 20 20 28 21 78 46 75 6e   ||.      (!xFun
2bff2 63 20 26 26 20 28 21 78 46 69 6e 61 6c 20 26 26  c && (!xFinal &&
2bff3 20 78 53 74 65 70 29 29 20 7c 7c 0a 20 20 20 20   xStep)) ||.    
2bff4 20 20 28 6e 41 72 67 3c 2d 31 20 7c 7c 20 6e 41    (nArg<-1 || nA
2bff5 72 67 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55  rg>SQLITE_MAX_FU
2bff6 4e 43 54 49 4f 4e 5f 41 52 47 29 20 7c 7c 0a 20  NCTION_ARG) ||. 
2bff7 20 20 20 20 20 28 32 35 35 3c 28 6e 4e 61 6d 65       (255<(nName
2bff8 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
2bff9 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61  (db, zFunctionNa
2bffa 6d 65 29 29 29 20 29 7b 0a 20 20 20 20 73 71 6c  me))) ){.    sql
2bffb 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
2bffc 4c 49 54 45 5f 45 52 52 4f 52 2c 20 22 62 61 64  LITE_ERROR, "bad
2bffd 20 70 61 72 61 6d 65 74 65 72 73 22 29 3b 0a 20   parameters");. 
2bffe 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2bfff 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 0a 23  _ERROR;.  }.  .#
2c000 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2c001 49 54 5f 55 54 46 31 36 0a 20 20 2f 2a 20 49 66  IT_UTF16.  /* If
2c002 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20 69 73   SQLITE_UTF16 is
2c003 20 73 70 65 63 69 66 69 65 64 20 61 73 20 74 68   specified as th
2c004 65 20 65 6e 63 6f 64 69 6e 67 20 74 79 70 65 2c  e encoding type,
2c005 20 74 72 61 6e 73 66 6f 72 6d 20 74 68 69 73 0a   transform this.
2c006 20 20 2a 2a 20 74 6f 20 6f 6e 65 20 6f 66 20 53    ** to one of S
2c007 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72  QLITE_UTF16LE or
2c008 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20   SQLITE_UTF16BE 
2c009 75 73 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 53  using the.  ** S
2c00a 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
2c00b 45 20 6d 61 63 72 6f 2e 20 53 51 4c 49 54 45 5f  E macro. SQLITE_
2c00c 55 54 46 31 36 20 69 73 20 6e 6f 74 20 75 73 65  UTF16 is not use
2c00d 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 20 20  d internally..  
2c00e 2a 2a 0a 20 20 2a 2a 20 49 66 20 53 51 4c 49 54  **.  ** If SQLIT
2c00f 45 5f 41 4e 59 20 69 73 20 73 70 65 63 69 66 69  E_ANY is specifi
2c010 65 64 2c 20 61 64 64 20 74 68 72 65 65 20 76 65  ed, add three ve
2c011 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 75  rsions of the fu
2c012 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 74 6f 20 74  nction.  ** to t
2c013 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 20  he hash table.. 
2c014 20 2a 2f 0a 20 20 69 66 28 20 65 6e 63 3d 3d 53   */.  if( enc==S
2c015 51 4c 49 54 45 5f 55 54 46 31 36 20 29 7b 0a 20  QLITE_UTF16 ){. 
2c016 20 20 20 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f     enc = SQLITE_
2c017 55 54 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 7d  UTF16NATIVE;.  }
2c018 65 6c 73 65 20 69 66 28 20 65 6e 63 3d 3d 53 51  else if( enc==SQ
2c019 4c 49 54 45 5f 41 4e 59 20 29 7b 0a 20 20 20 20  LITE_ANY ){.    
2c01a 69 6e 74 20 72 63 3b 0a 20 20 20 20 72 63 20 3d  int rc;.    rc =
2c01b 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75   sqlite3CreateFu
2c01c 6e 63 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e  nc(db, zFunction
2c01d 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 53 51 4c 49  Name, nArg, SQLI
2c01e 54 45 5f 55 54 46 38 2c 0a 20 20 20 20 20 20 20  TE_UTF8,.       
2c01f 20 20 70 55 73 65 72 44 61 74 61 2c 20 78 46 75    pUserData, xFu
2c020 6e 63 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61  nc, xStep, xFina
2c021 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  l);.    if( rc==
2c022 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
2c023 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 43     rc = sqlite3C
2c024 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 7a 46  reateFunc(db, zF
2c025 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e 41 72  unctionName, nAr
2c026 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  g, SQLITE_UTF16L
2c027 45 2c 0a 20 20 20 20 20 20 20 20 20 20 70 55 73  E,.          pUs
2c028 65 72 44 61 74 61 2c 20 78 46 75 6e 63 2c 20 78  erData, xFunc, x
2c029 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 3b 0a 20  Step, xFinal);. 
2c02a 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21     }.    if( rc!
2c02b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
2c02c 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
2c02d 20 20 20 7d 0a 20 20 20 20 65 6e 63 20 3d 20 53     }.    enc = S
2c02e 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3b 0a 20  QLITE_UTF16BE;. 
2c02f 20 7d 0a 23 65 6c 73 65 0a 20 20 65 6e 63 20 3d   }.#else.  enc =
2c030 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 23 65   SQLITE_UTF8;.#e
2c031 6e 64 69 66 0a 20 20 0a 20 20 2f 2a 20 43 68 65  ndif.  .  /* Che
2c032 63 6b 20 69 66 20 61 6e 20 65 78 69 73 74 69 6e  ck if an existin
2c033 67 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65  g function is be
2c034 69 6e 67 20 6f 76 65 72 72 69 64 64 65 6e 20 6f  ing overridden o
2c035 72 20 64 65 6c 65 74 65 64 2e 20 49 66 20 73 6f  r deleted. If so
2c036 2c 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 72 65  ,.  ** and there
2c037 20 61 72 65 20 61 63 74 69 76 65 20 56 4d 73 2c   are active VMs,
2c038 20 74 68 65 6e 20 72 65 74 75 72 6e 20 53 51 4c   then return SQL
2c039 49 54 45 5f 42 55 53 59 2e 20 49 66 20 61 20 66  ITE_BUSY. If a f
2c03a 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20  unction.  ** is 
2c03b 62 65 69 6e 67 20 6f 76 65 72 72 69 64 64 65 6e  being overridden
2c03c 2f 64 65 6c 65 74 65 64 20 62 75 74 20 74 68 65  /deleted but the
2c03d 72 65 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65  re are no active
2c03e 20 56 4d 73 2c 20 61 6c 6c 6f 77 20 74 68 65 0a   VMs, allow the.
2c03f 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 74    ** operation t
2c040 6f 20 63 6f 6e 74 69 6e 75 65 20 62 75 74 20 69  o continue but i
2c041 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 70 72  nvalidate all pr
2c042 65 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d  ecompiled statem
2c043 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 70 20 3d  ents..  */.  p =
2c044 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63   sqlite3FindFunc
2c045 74 69 6f 6e 28 64 62 2c 20 7a 46 75 6e 63 74 69  tion(db, zFuncti
2c046 6f 6e 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e  onName, nName, n
2c047 41 72 67 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20  Arg, enc, 0);.  
2c048 69 66 28 20 70 20 26 26 20 70 2d 3e 69 50 72 65  if( p && p->iPre
2c049 66 45 6e 63 3d 3d 65 6e 63 20 26 26 20 70 2d 3e  fEnc==enc && p->
2c04a 6e 41 72 67 3d 3d 6e 41 72 67 20 29 7b 0a 20 20  nArg==nArg ){.  
2c04b 20 20 69 66 28 20 64 62 2d 3e 61 63 74 69 76 65    if( db->active
2c04c 56 64 62 65 43 6e 74 20 29 7b 0a 20 20 20 20 20  VdbeCnt ){.     
2c04d 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
2c04e 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 2c 20 0a  , SQLITE_BUSY, .
2c04f 20 20 20 20 20 20 20 20 22 55 6e 61 62 6c 65 20          "Unable 
2c050 74 6f 20 64 65 6c 65 74 65 2f 6d 6f 64 69 66 79  to delete/modify
2c051 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64   user-function d
2c052 75 65 20 74 6f 20 61 63 74 69 76 65 20 73 74 61  ue to active sta
2c053 74 65 6d 65 6e 74 73 22 29 3b 0a 20 20 20 20 20  tements");.     
2c054 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61   assert( !db->ma
2c055 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
2c056 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2c057 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73  E_BUSY;.    }els
2c058 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
2c059 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74  ExpirePreparedSt
2c05a 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20  atements(db);.  
2c05b 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 20 3d 20 73    }.  }..  p = s
2c05c 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69  qlite3FindFuncti
2c05d 6f 6e 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e  on(db, zFunction
2c05e 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e 41 72  Name, nName, nAr
2c05f 67 2c 20 65 6e 63 2c 20 31 29 3b 0a 20 20 61 73  g, enc, 1);.  as
2c060 73 65 72 74 28 70 20 7c 7c 20 64 62 2d 3e 6d 61  sert(p || db->ma
2c061 6c 6c 6f 63 46 61 69 6c 65 64 29 3b 0a 20 20 69  llocFailed);.  i
2c062 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74  f( !p ){.    ret
2c063 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
2c064 3b 0a 20 20 7d 0a 20 20 70 2d 3e 66 6c 61 67 73  ;.  }.  p->flags
2c065 20 3d 20 30 3b 0a 20 20 70 2d 3e 78 46 75 6e 63   = 0;.  p->xFunc
2c066 20 3d 20 78 46 75 6e 63 3b 0a 20 20 70 2d 3e 78   = xFunc;.  p->x
2c067 53 74 65 70 20 3d 20 78 53 74 65 70 3b 0a 20 20  Step = xStep;.  
2c068 70 2d 3e 78 46 69 6e 61 6c 69 7a 65 20 3d 20 78  p->xFinalize = x
2c069 46 69 6e 61 6c 3b 0a 20 20 70 2d 3e 70 55 73 65  Final;.  p->pUse
2c06a 72 44 61 74 61 20 3d 20 70 55 73 65 72 44 61 74  rData = pUserDat
2c06b 61 3b 0a 20 20 70 2d 3e 6e 41 72 67 20 3d 20 6e  a;.  p->nArg = n
2c06c 41 72 67 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  Arg;.  return SQ
2c06d 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
2c06e 2a 20 43 72 65 61 74 65 20 6e 65 77 20 75 73 65  * Create new use
2c06f 72 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a  r functions..*/.
2c070 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2c071 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
2c072 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65  nction(.  sqlite
2c073 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63  3 *db,.  const c
2c074 68 61 72 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61  har *zFunctionNa
2c075 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a  me,.  int nArg,.
2c076 20 20 69 6e 74 20 65 6e 63 2c 0a 20 20 76 6f 69    int enc,.  voi
2c077 64 20 2a 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78  d *p,.  void (*x
2c078 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f  Func)(sqlite3_co
2c079 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
2c07a 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20  e3_value **),.  
2c07b 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71  void (*xStep)(sq
2c07c 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
2c07d 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
2c07e 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78   **),.  void (*x
2c07f 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63  Final)(sqlite3_c
2c080 6f 6e 74 65 78 74 2a 29 0a 29 7b 0a 20 20 69 6e  ontext*).){.  in
2c081 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f  t rc;.  sqlite3_
2c082 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
2c083 6d 75 74 65 78 29 3b 0a 20 20 72 63 20 3d 20 73  mutex);.  rc = s
2c084 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63  qlite3CreateFunc
2c085 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61  (db, zFunctionNa
2c086 6d 65 2c 20 6e 41 72 67 2c 20 65 6e 63 2c 20 70  me, nArg, enc, p
2c087 2c 20 78 46 75 6e 63 2c 20 78 53 74 65 70 2c 20  , xFunc, xStep, 
2c088 78 46 69 6e 61 6c 29 3b 0a 20 20 72 63 20 3d 20  xFinal);.  rc = 
2c089 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
2c08a 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65  b, rc);.  sqlite
2c08b 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
2c08c 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
2c08d 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65  rn rc;.}..#ifnde
2c08e 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
2c08f 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  F16.SQLITE_API i
2c090 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  nt sqlite3_creat
2c091 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 0a 20 20  e_function16(.  
2c092 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63  sqlite3 *db,.  c
2c093 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46 75 6e 63  onst void *zFunc
2c094 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20  tionName,.  int 
2c095 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78  nArg,.  int eTex
2c096 74 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 2c  tRep,.  void *p,
2c097 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29  .  void (*xFunc)
2c098 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2c099 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
2c09a 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28  lue**),.  void (
2c09b 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f  *xStep)(sqlite3_
2c09c 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
2c09d 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20  ite3_value**),. 
2c09e 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28   void (*xFinal)(
2c09f 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
2c0a0 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ).){.  int rc;. 
2c0a1 20 63 68 61 72 20 2a 7a 46 75 6e 63 38 3b 0a 20   char *zFunc8;. 
2c0a2 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
2c0a3 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
2c0a4 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e  .  assert( !db->
2c0a5 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
2c0a6 20 20 7a 46 75 6e 63 38 20 3d 20 73 71 6c 69 74    zFunc8 = sqlit
2c0a7 65 33 55 74 66 31 36 74 6f 38 28 64 62 2c 20 7a  e3Utf16to8(db, z
2c0a8 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 2d 31  FunctionName, -1
2c0a9 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
2c0aa 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20  3CreateFunc(db, 
2c0ab 7a 46 75 6e 63 38 2c 20 6e 41 72 67 2c 20 65 54  zFunc8, nArg, eT
2c0ac 65 78 74 52 65 70 2c 20 70 2c 20 78 46 75 6e 63  extRep, p, xFunc
2c0ad 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29  , xStep, xFinal)
2c0ae 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
2c0af 65 28 64 62 2c 20 7a 46 75 6e 63 38 29 3b 0a 20  e(db, zFunc8);. 
2c0b0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
2c0b1 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
2c0b2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
2c0b3 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
2c0b4 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23    return rc;.}.#
2c0b5 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65  endif.../*.** De
2c0b6 63 6c 61 72 65 20 74 68 61 74 20 61 20 66 75 6e  clare that a fun
2c0b7 63 74 69 6f 6e 20 68 61 73 20 62 65 65 6e 20 6f  ction has been o
2c0b8 76 65 72 6c 6f 61 64 65 64 20 62 79 20 61 20 76  verloaded by a v
2c0b9 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a  irtual table..**
2c0ba 0a 2a 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74  .** If the funct
2c0bb 69 6f 6e 20 61 6c 72 65 61 64 79 20 65 78 69 73  ion already exis
2c0bc 74 73 20 61 73 20 61 20 72 65 67 75 6c 61 72 20  ts as a regular 
2c0bd 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e 2c  global function,
2c0be 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f   then.** this ro
2c0bf 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70  utine is a no-op
2c0c0 2e 20 20 49 66 20 74 68 65 20 66 75 6e 63 74 69  .  If the functi
2c0c1 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  on does not exis
2c0c2 74 2c 20 74 68 65 6e 20 63 72 65 61 74 65 0a 2a  t, then create.*
2c0c3 2a 20 61 20 6e 65 77 20 6f 6e 65 20 74 68 61 74  * a new one that
2c0c4 20 61 6c 77 61 79 73 20 74 68 72 6f 77 73 20 61   always throws a
2c0c5 20 72 75 6e 2d 74 69 6d 65 20 65 72 72 6f 72 2e   run-time error.
2c0c6 20 20 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 76 69    .**.** When vi
2c0c7 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 74  rtual tables int
2c0c8 65 6e 64 20 74 6f 20 70 72 6f 76 69 64 65 20 61  end to provide a
2c0c9 6e 20 6f 76 65 72 6c 6f 61 64 65 64 20 66 75 6e  n overloaded fun
2c0ca 63 74 69 6f 6e 2c 20 74 68 65 79 0a 2a 2a 20 73  ction, they.** s
2c0cb 68 6f 75 6c 64 20 63 61 6c 6c 20 74 68 69 73 20  hould call this 
2c0cc 72 6f 75 74 69 6e 65 20 74 6f 20 6d 61 6b 65 20  routine to make 
2c0cd 73 75 72 65 20 74 68 65 20 67 6c 6f 62 61 6c 20  sure the global 
2c0ce 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 2e  function exists.
2c0cf 0a 2a 2a 20 41 20 67 6c 6f 62 61 6c 20 66 75 6e  .** A global fun
2c0d0 63 74 69 6f 6e 20 6d 75 73 74 20 65 78 69 73 74  ction must exist
2c0d1 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 6e 61   in order for na
2c0d2 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 74 6f  me resolution to
2c0d3 20 77 6f 72 6b 0a 2a 2a 20 70 72 6f 70 65 72 6c   work.** properl
2c0d4 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  y..*/.SQLITE_API
2c0d5 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 76 65   int sqlite3_ove
2c0d6 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 0a  rload_function(.
2c0d7 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20    sqlite3 *db,. 
2c0d8 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
2c0d9 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 0a 29  me,.  int nArg.)
2c0da 7b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20  {.  int nName = 
2c0db 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 28 64 62  sqlite3Strlen(db
2c0dc 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 6e 74 20  , zName);.  int 
2c0dd 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  rc;.  sqlite3_mu
2c0de 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
2c0df 74 65 78 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  tex);.  if( sqli
2c0e0 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28  te3FindFunction(
2c0e1 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  db, zName, nName
2c0e2 2c 20 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55  , nArg, SQLITE_U
2c0e3 54 46 38 2c 20 30 29 3d 3d 30 20 29 7b 0a 20 20  TF8, 0)==0 ){.  
2c0e4 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46    sqlite3CreateF
2c0e5 75 6e 63 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e  unc(db, zName, n
2c0e6 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  Arg, SQLITE_UTF8
2c0e7 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2c0e8 20 20 20 20 20 20 20 20 30 2c 20 73 71 6c 69 74          0, sqlit
2c0e9 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f  e3InvalidFunctio
2c0ea 6e 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  n, 0, 0);.  }.  
2c0eb 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45  rc = sqlite3ApiE
2c0ec 78 69 74 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f  xit(db, SQLITE_O
2c0ed 4b 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  K);.  sqlite3_mu
2c0ee 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
2c0ef 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  tex);.  return r
2c0f0 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  c;.}..#ifndef SQ
2c0f1 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a  LITE_OMIT_TRACE.
2c0f2 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61  /*.** Register a
2c0f3 20 74 72 61 63 65 20 66 75 6e 63 74 69 6f 6e 2e   trace function.
2c0f4 20 20 54 68 65 20 70 41 72 67 20 66 72 6f 6d 20    The pArg from 
2c0f5 74 68 65 20 70 72 65 76 69 6f 75 73 6c 79 20 72  the previously r
2c0f6 65 67 69 73 74 65 72 65 64 20 74 72 61 63 65 0a  egistered trace.
2c0f7 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  ** is returned. 
2c0f8 20 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 74   .**.** A NULL t
2c0f9 72 61 63 65 20 66 75 6e 63 74 69 6f 6e 20 6d 65  race function me
2c0fa 61 6e 73 20 74 68 61 74 20 6e 6f 20 74 72 61 63  ans that no trac
2c0fb 69 6e 67 20 69 73 20 65 78 65 63 75 74 65 73 2e  ing is executes.
2c0fc 20 20 41 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20    A non-NULL.** 
2c0fd 74 72 61 63 65 20 69 73 20 61 20 70 6f 69 6e 74  trace is a point
2c0fe 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e  er to a function
2c0ff 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64   that is invoked
2c100 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66   at the start of
2c101 20 65 61 63 68 0a 2a 2a 20 53 51 4c 20 73 74 61   each.** SQL sta
2c102 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  tement..*/.SQLIT
2c103 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
2c104 74 65 33 5f 74 72 61 63 65 28 73 71 6c 69 74 65  te3_trace(sqlite
2c105 33 20 2a 64 62 2c 20 76 6f 69 64 20 28 2a 78 54  3 *db, void (*xT
2c106 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73  race)(void*,cons
2c107 74 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 20 2a  t char*), void *
2c108 70 41 72 67 29 7b 0a 20 20 76 6f 69 64 20 2a 70  pArg){.  void *p
2c109 4f 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  Old;.  sqlite3_m
2c10a 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
2c10b 75 74 65 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20  utex);.  pOld = 
2c10c 64 62 2d 3e 70 54 72 61 63 65 41 72 67 3b 0a 20  db->pTraceArg;. 
2c10d 20 64 62 2d 3e 78 54 72 61 63 65 20 3d 20 78 54   db->xTrace = xT
2c10e 72 61 63 65 3b 0a 20 20 64 62 2d 3e 70 54 72 61  race;.  db->pTra
2c10f 63 65 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20  ceArg = pArg;.  
2c110 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
2c111 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
2c112 20 20 72 65 74 75 72 6e 20 70 4f 6c 64 3b 0a 7d    return pOld;.}
2c113 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
2c114 61 20 70 72 6f 66 69 6c 65 20 66 75 6e 63 74 69  a profile functi
2c115 6f 6e 2e 20 20 54 68 65 20 70 41 72 67 20 66 72  on.  The pArg fr
2c116 6f 6d 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c  om the previousl
2c117 79 20 72 65 67 69 73 74 65 72 65 64 20 0a 2a 2a  y registered .**
2c118 20 70 72 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f   profile functio
2c119 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  n is returned.  
2c11a 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 70 72  .**.** A NULL pr
2c11b 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e 20 6d  ofile function m
2c11c 65 61 6e 73 20 74 68 61 74 20 6e 6f 20 70 72 6f  eans that no pro
2c11d 66 69 6c 69 6e 67 20 69 73 20 65 78 65 63 75 74  filing is execut
2c11e 65 73 2e 20 20 41 20 6e 6f 6e 2d 4e 55 4c 4c 0a  es.  A non-NULL.
2c11f 2a 2a 20 70 72 6f 66 69 6c 65 20 69 73 20 61 20  ** profile is a 
2c120 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e  pointer to a fun
2c121 63 74 69 6f 6e 20 74 68 61 74 20 69 73 20 69 6e  ction that is in
2c122 76 6f 6b 65 64 20 61 74 20 74 68 65 20 63 6f 6e  voked at the con
2c123 63 6c 75 73 69 6f 6e 20 6f 66 0a 2a 2a 20 65 61  clusion of.** ea
2c124 63 68 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ch SQL statement
2c125 20 74 68 61 74 20 69 73 20 72 75 6e 2e 0a 2a 2f   that is run..*/
2c126 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
2c127 20 2a 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c   *sqlite3_profil
2c128 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e(.  sqlite3 *db
2c129 2c 0a 20 20 76 6f 69 64 20 28 2a 78 50 72 6f 66  ,.  void (*xProf
2c12a 69 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74  ile)(void*,const
2c12b 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 5f 75 69   char*,sqlite_ui
2c12c 6e 74 36 34 29 2c 0a 20 20 76 6f 69 64 20 2a 70  nt64),.  void *p
2c12d 41 72 67 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70  Arg.){.  void *p
2c12e 4f 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  Old;.  sqlite3_m
2c12f 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
2c130 75 74 65 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20  utex);.  pOld = 
2c131 64 62 2d 3e 70 50 72 6f 66 69 6c 65 41 72 67 3b  db->pProfileArg;
2c132 0a 20 20 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20  .  db->xProfile 
2c133 3d 20 78 50 72 6f 66 69 6c 65 3b 0a 20 20 64 62  = xProfile;.  db
2c134 2d 3e 70 50 72 6f 66 69 6c 65 41 72 67 20 3d 20  ->pProfileArg = 
2c135 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f  pArg;.  sqlite3_
2c136 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
2c137 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
2c138 20 70 4f 6c 64 3b 0a 7d 0a 23 65 6e 64 69 66 20   pOld;.}.#endif 
2c139 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  /* SQLITE_OMIT_T
2c13a 52 41 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a 20 45 58  RACE */../*** EX
2c13b 50 45 52 49 4d 45 4e 54 41 4c 20 2a 2a 2a 0a 2a  PERIMENTAL ***.*
2c13c 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20  *.** Register a 
2c13d 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 69  function to be i
2c13e 6e 76 6f 6b 65 64 20 77 68 65 6e 20 61 20 74 72  nvoked when a tr
2c13f 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6d 6d 65 6e  ansaction commen
2c140 74 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e  ts..** If the in
2c141 76 6f 6b 65 64 20 66 75 6e 63 74 69 6f 6e 20 72  voked function r
2c142 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  eturns non-zero,
2c143 20 74 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69 74   then the commit
2c144 20 62 65 63 6f 6d 65 73 20 61 0a 2a 2a 20 72 6f   becomes a.** ro
2c145 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54  llback..*/.SQLIT
2c146 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
2c147 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28  te3_commit_hook(
2c148 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
2c149 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c14a 41 74 74 61 63 68 20 74 68 65 20 68 6f 6f 6b 20  Attach the hook 
2c14b 74 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65  to this database
2c14c 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 61 6c   */.  int (*xCal
2c14d 6c 62 61 63 6b 29 28 76 6f 69 64 2a 29 2c 20 20  lback)(void*),  
2c14e 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 69  /* Function to i
2c14f 6e 76 6f 6b 65 20 6f 6e 20 65 61 63 68 20 63 6f  nvoke on each co
2c150 6d 6d 69 74 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  mmit */.  void *
2c151 70 41 72 67 20 20 20 20 20 20 20 20 20 20 20 20  pArg            
2c152 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20      /* Argument 
2c153 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  to the function 
2c154 2a 2f 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4f  */.){.  void *pO
2c155 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  ld;.  sqlite3_mu
2c156 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
2c157 74 65 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20 64  tex);.  pOld = d
2c158 62 2d 3e 70 43 6f 6d 6d 69 74 41 72 67 3b 0a 20  b->pCommitArg;. 
2c159 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c 6c   db->xCommitCall
2c15a 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b  back = xCallback
2c15b 3b 0a 20 20 64 62 2d 3e 70 43 6f 6d 6d 69 74 41  ;.  db->pCommitA
2c15c 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c  rg = pArg;.  sql
2c15d 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
2c15e 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
2c15f 65 74 75 72 6e 20 70 4f 6c 64 3b 0a 7d 0a 0a 2f  eturn pOld;.}../
2c160 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20  *.** Register a 
2c161 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20 69  callback to be i
2c162 6e 76 6f 6b 65 64 20 65 61 63 68 20 74 69 6d 65  nvoked each time
2c163 20 61 20 72 6f 77 20 69 73 20 75 70 64 61 74 65   a row is update
2c164 64 2c 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20 6f  d,.** inserted o
2c165 72 20 64 65 6c 65 74 65 64 20 75 73 69 6e 67 20  r deleted using 
2c166 74 68 69 73 20 64 61 74 61 62 61 73 65 20 63 6f  this database co
2c167 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  nnection..*/.SQL
2c168 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71  ITE_API void *sq
2c169 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f  lite3_update_hoo
2c16a 6b 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  k(.  sqlite3 *db
2c16b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
2c16c 2a 20 41 74 74 61 63 68 20 74 68 65 20 68 6f 6f  * Attach the hoo
2c16d 6b 20 74 6f 20 74 68 69 73 20 64 61 74 61 62 61  k to this databa
2c16e 73 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  se */.  void (*x
2c16f 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c  Callback)(void*,
2c170 69 6e 74 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a  int,char const *
2c171 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c 73 71  ,char const *,sq
2c172 6c 69 74 65 5f 69 6e 74 36 34 29 2c 0a 20 20 76  lite_int64),.  v
2c173 6f 69 64 20 2a 70 41 72 67 20 20 20 20 20 20 20  oid *pArg       
2c174 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75           /* Argu
2c175 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63  ment to the func
2c176 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 76 6f 69  tion */.){.  voi
2c177 64 20 2a 70 52 65 74 3b 0a 20 20 73 71 6c 69 74  d *pRet;.  sqlit
2c178 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
2c179 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 52 65  b->mutex);.  pRe
2c17a 74 20 3d 20 64 62 2d 3e 70 55 70 64 61 74 65 41  t = db->pUpdateA
2c17b 72 67 3b 0a 20 20 64 62 2d 3e 78 55 70 64 61 74  rg;.  db->xUpdat
2c17c 65 43 61 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c  eCallback = xCal
2c17d 6c 62 61 63 6b 3b 0a 20 20 64 62 2d 3e 70 55 70  lback;.  db->pUp
2c17e 64 61 74 65 41 72 67 20 3d 20 70 41 72 67 3b 0a  dateArg = pArg;.
2c17f 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2c180 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
2c181 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b  ;.  return pRet;
2c182 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74  .}../*.** Regist
2c183 65 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 6f  er a callback to
2c184 20 62 65 20 69 6e 76 6f 6b 65 64 20 65 61 63 68   be invoked each
2c185 20 74 69 6d 65 20 61 20 74 72 61 6e 73 61 63 74   time a transact
2c186 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 0a 2a 2a  ion is rolled.**
2c187 20 62 61 63 6b 20 62 79 20 74 68 69 73 20 64 61   back by this da
2c188 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2c189 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  n..*/.SQLITE_API
2c18a 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72   void *sqlite3_r
2c18b 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 0a 20 20  ollback_hook(.  
2c18c 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
2c18d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 74 74            /* Att
2c18e 61 63 68 20 74 68 65 20 68 6f 6f 6b 20 74 6f 20  ach the hook to 
2c18f 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f  this database */
2c190 0a 20 20 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62  .  void (*xCallb
2c191 61 63 6b 29 28 76 6f 69 64 2a 29 2c 20 2f 2a 20  ack)(void*), /* 
2c192 43 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f  Callback functio
2c193 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72  n */.  void *pAr
2c194 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g               
2c195 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20   /* Argument to 
2c196 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  the function */.
2c197 29 7b 0a 20 20 76 6f 69 64 20 2a 70 52 65 74 3b  ){.  void *pRet;
2c198 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2c199 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
2c19a 29 3b 0a 20 20 70 52 65 74 20 3d 20 64 62 2d 3e  );.  pRet = db->
2c19b 70 52 6f 6c 6c 62 61 63 6b 41 72 67 3b 0a 20 20  pRollbackArg;.  
2c19c 64 62 2d 3e 78 52 6f 6c 6c 62 61 63 6b 43 61 6c  db->xRollbackCal
2c19d 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63  lback = xCallbac
2c19e 6b 3b 0a 20 20 64 62 2d 3e 70 52 6f 6c 6c 62 61  k;.  db->pRollba
2c19f 63 6b 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20  ckArg = pArg;.  
2c1a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
2c1a1 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
2c1a2 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d    return pRet;.}
2c1a3 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
2c1a4 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74  tine is called t
2c1a5 6f 20 63 72 65 61 74 65 20 61 20 63 6f 6e 6e 65  o create a conne
2c1a6 63 74 69 6f 6e 20 74 6f 20 61 20 64 61 74 61 62  ction to a datab
2c1a7 61 73 65 20 42 54 72 65 65 0a 2a 2a 20 64 72 69  ase BTree.** dri
2c1a8 76 65 72 2e 20 20 49 66 20 7a 46 69 6c 65 6e 61  ver.  If zFilena
2c1a9 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  me is the name o
2c1aa 66 20 61 20 66 69 6c 65 2c 20 74 68 65 6e 20 74  f a file, then t
2c1ab 68 61 74 20 66 69 6c 65 20 69 73 0a 2a 2a 20 6f  hat file is.** o
2c1ac 70 65 6e 65 64 20 61 6e 64 20 75 73 65 64 2e 20  pened and used. 
2c1ad 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73   If zFilename is
2c1ae 20 74 68 65 20 6d 61 67 69 63 20 6e 61 6d 65 20   the magic name 
2c1af 22 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e 0a  ":memory:" then.
2c1b0 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
2c1b1 69 73 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d  is stored in mem
2c1b2 6f 72 79 20 28 61 6e 64 20 69 73 20 74 68 75 73  ory (and is thus
2c1b3 20 66 6f 72 67 6f 74 74 65 6e 20 61 73 20 73 6f   forgotten as so
2c1b4 6f 6e 20 61 73 0a 2a 2a 20 74 68 65 20 63 6f 6e  on as.** the con
2c1b5 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65  nection is close
2c1b6 64 2e 29 20 20 49 66 20 7a 46 69 6c 65 6e 61 6d  d.)  If zFilenam
2c1b7 65 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74  e is NULL then t
2c1b8 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 69  he database.** i
2c1b9 73 20 61 20 22 76 69 72 74 75 61 6c 22 20 64 61  s a "virtual" da
2c1ba 74 61 62 61 73 65 20 66 6f 72 20 74 72 61 6e 73  tabase for trans
2c1bb 69 65 6e 74 20 75 73 65 20 6f 6e 6c 79 20 61 6e  ient use only an
2c1bc 64 20 69 73 20 64 65 6c 65 74 65 64 20 61 73 0a  d is deleted as.
2c1bd 2a 2a 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 63  ** soon as the c
2c1be 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f  onnection is clo
2c1bf 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72  sed..**.** A vir
2c1c0 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 63 61  tual database ca
2c1c1 6e 20 62 65 20 65 69 74 68 65 72 20 61 20 64 69  n be either a di
2c1c2 73 6b 20 66 69 6c 65 20 28 74 68 61 74 20 69 73  sk file (that is
2c1c3 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a   automatically.*
2c1c4 2a 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74  * deleted when t
2c1c5 68 65 20 66 69 6c 65 20 69 73 20 63 6c 6f 73 65  he file is close
2c1c6 64 29 20 6f 72 20 69 74 20 61 6e 20 62 65 20 68  d) or it an be h
2c1c7 65 6c 64 20 65 6e 74 69 72 65 6c 79 20 69 6e 20  eld entirely in 
2c1c8 6d 65 6d 6f 72 79 2c 0a 2a 2a 20 64 65 70 65 6e  memory,.** depen
2c1c9 64 69 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c 75  ding on the valu
2c1ca 65 73 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45  es of the SQLITE
2c1cb 5f 54 45 4d 50 5f 53 54 4f 52 45 20 63 6f 6d 70  _TEMP_STORE comp
2c1cc 69 6c 65 2d 74 69 6d 65 20 6d 61 63 72 6f 20 61  ile-time macro a
2c1cd 6e 64 20 74 68 65 0a 2a 2a 20 64 62 2d 3e 74 65  nd the.** db->te
2c1ce 6d 70 5f 73 74 6f 72 65 20 76 61 72 69 61 62 6c  mp_store variabl
2c1cf 65 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20  e, according to 
2c1d0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68  the following ch
2c1d1 61 72 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c  art:.**.**   SQL
2c1d2 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 20 20  ITE_TEMP_STORE  
2c1d3 20 20 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72     db->temp_stor
2c1d4 65 20 20 20 20 20 4c 6f 63 61 74 69 6f 6e 20 6f  e     Location o
2c1d5 66 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61  f temporary data
2c1d6 62 61 73 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d  base.**   ------
2c1d7 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20  -----------     
2c1d8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20  --------------  
2c1d9 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
2c1da 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2c1db 2d 0a 2a 2a 20 20 20 30 20 20 20 20 20 20 20 20  -.**   0        
2c1dc 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 79               any
2c1dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c1de 66 69 6c 65 0a 2a 2a 20 20 20 31 20 20 20 20 20  file.**   1     
2c1df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c1e0 31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1               
2c1e1 20 20 20 66 69 6c 65 0a 2a 2a 20 20 20 31 20 20     file.**   1  
2c1e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c1e3 20 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20     2            
2c1e4 20 20 20 20 20 20 6d 65 6d 6f 72 79 0a 2a 2a 20        memory.** 
2c1e5 20 20 31 20 20 20 20 20 20 20 20 20 20 20 20 20    1             
2c1e6 20 20 20 20 20 20 20 20 30 20 20 20 20 20 20 20          0       
2c1e7 20 20 20 20 20 20 20 20 20 20 20 66 69 6c 65 0a             file.
2c1e8 2a 2a 20 20 20 32 20 20 20 20 20 20 20 20 20 20  **   2          
2c1e9 20 20 20 20 20 20 20 20 20 20 20 31 20 20 20 20             1    
2c1ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69                fi
2c1eb 6c 65 0a 2a 2a 20 20 20 32 20 20 20 20 20 20 20  le.**   2       
2c1ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 20                2 
2c1ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c1ee 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 32 20 20   memory.**   2  
2c1ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c1f0 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20     0            
2c1f1 20 20 20 20 20 20 6d 65 6d 6f 72 79 0a 2a 2a 20        memory.** 
2c1f2 20 20 33 20 20 20 20 20 20 20 20 20 20 20 20 20    3             
2c1f3 20 20 20 20 20 20 20 20 61 6e 79 20 20 20 20 20          any     
2c1f4 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 6f 72             memor
2c1f5 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  y.*/.SQLITE_PRIV
2c1f6 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
2c1f7 74 72 65 65 46 61 63 74 6f 72 79 28 0a 20 20 63  treeFactory(.  c
2c1f8 6f 6e 73 74 20 73 71 6c 69 74 65 33 20 2a 64 62  onst sqlite3 *db
2c1f9 2c 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 69 6e  ,        /* Main
2c1fa 20 64 61 74 61 62 61 73 65 20 77 68 65 6e 20 6f   database when o
2c1fb 70 65 6e 69 6e 67 20 61 75 78 20 6f 74 68 65 72  pening aux other
2c1fc 77 69 73 65 20 30 20 2a 2f 0a 20 20 63 6f 6e 73  wise 0 */.  cons
2c1fd 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
2c1fe 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  e,    /* Name of
2c1ff 20 74 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 69   the file contai
2c200 6e 69 6e 67 20 74 68 65 20 42 54 72 65 65 20 64  ning the BTree d
2c201 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74  atabase */.  int
2c202 20 6f 6d 69 74 4a 6f 75 72 6e 61 6c 2c 20 20 20   omitJournal,   
2c203 20 20 20 20 20 20 20 2f 2a 20 69 66 20 54 52 55         /* if TRU
2c204 45 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 6a 6f  E then do not jo
2c205 75 72 6e 61 6c 20 74 68 69 73 20 66 69 6c 65 20  urnal this file 
2c206 2a 2f 0a 20 20 69 6e 74 20 6e 43 61 63 68 65 2c  */.  int nCache,
2c207 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2c208 2a 20 48 6f 77 20 6d 61 6e 79 20 70 61 67 65 73  * How many pages
2c209 20 69 6e 20 74 68 65 20 70 61 67 65 20 63 61 63   in the page cac
2c20a 68 65 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46  he */.  int vfsF
2c20b 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20  lags,           
2c20c 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65    /* Flags passe
2c20d 64 20 74 68 72 6f 75 67 68 20 74 6f 20 76 66 73  d through to vfs
2c20e 4f 70 65 6e 20 2a 2f 0a 20 20 42 74 72 65 65 20  Open */.  Btree 
2c20f 2a 2a 70 70 42 74 72 65 65 20 20 20 20 20 20 20  **ppBtree       
2c210 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
2c211 6f 20 6e 65 77 20 42 74 72 65 65 20 6f 62 6a 65  o new Btree obje
2c212 63 74 20 77 72 69 74 74 65 6e 20 68 65 72 65 20  ct written here 
2c213 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 62 74 46 6c  */.){.  int btFl
2c214 61 67 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  ags = 0;.  int r
2c215 63 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20  c;.  .  assert( 
2c216 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
2c217 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
2c218 0a 20 20 61 73 73 65 72 74 28 20 70 70 42 74 72  .  assert( ppBtr
2c219 65 65 20 21 3d 20 30 29 3b 0a 20 20 69 66 28 20  ee != 0);.  if( 
2c21a 6f 6d 69 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20  omitJournal ){. 
2c21b 20 20 20 62 74 46 6c 61 67 73 20 7c 3d 20 42 54     btFlags |= BT
2c21c 52 45 45 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c  REE_OMIT_JOURNAL
2c21d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e  ;.  }.  if( db->
2c21e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4e  flags & SQLITE_N
2c21f 6f 52 65 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 20  oReadlock ){.   
2c220 20 62 74 46 6c 61 67 73 20 7c 3d 20 42 54 52 45   btFlags |= BTRE
2c221 45 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 3b 0a 20  E_NO_READLOCK;. 
2c222 20 7d 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61   }.  if( zFilena
2c223 6d 65 3d 3d 30 20 29 7b 0a 23 69 66 20 53 51 4c  me==0 ){.#if SQL
2c224 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d  ITE_TEMP_STORE==
2c225 30 0a 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68  0.    /* Do noth
2c226 69 6e 67 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69  ing */.#endif.#i
2c227 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2c228 54 5f 4d 45 4d 4f 52 59 44 42 0a 23 69 66 20 53  T_MEMORYDB.#if S
2c229 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45  QLITE_TEMP_STORE
2c22a 3d 3d 31 0a 20 20 20 20 69 66 28 20 64 62 2d 3e  ==1.    if( db->
2c22b 74 65 6d 70 5f 73 74 6f 72 65 3d 3d 32 20 29 20  temp_store==2 ) 
2c22c 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 22 3a 6d 65  zFilename = ":me
2c22d 6d 6f 72 79 3a 22 3b 0a 23 65 6e 64 69 66 0a 23  mory:";.#endif.#
2c22e 69 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53  if SQLITE_TEMP_S
2c22f 54 4f 52 45 3d 3d 32 0a 20 20 20 20 69 66 28 20  TORE==2.    if( 
2c230 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 21 3d  db->temp_store!=
2c231 31 20 29 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20  1 ) zFilename = 
2c232 22 3a 6d 65 6d 6f 72 79 3a 22 3b 0a 23 65 6e 64  ":memory:";.#end
2c233 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 45  if.#if SQLITE_TE
2c234 4d 50 5f 53 54 4f 52 45 3d 3d 33 0a 20 20 20 20  MP_STORE==3.    
2c235 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 22 3a 6d 65  zFilename = ":me
2c236 6d 6f 72 79 3a 22 3b 0a 23 65 6e 64 69 66 0a 23  mory:";.#endif.#
2c237 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2c238 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 2a 2f  OMIT_MEMORYDB */
2c239 0a 20 20 7d 0a 0a 20 20 69 66 28 20 28 76 66 73  .  }..  if( (vfs
2c23a 46 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  Flags & SQLITE_O
2c23b 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 21 3d 30 20  PEN_MAIN_DB)!=0 
2c23c 26 26 20 28 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30  && (zFilename==0
2c23d 20 7c 7c 20 2a 7a 46 69 6c 65 6e 61 6d 65 3d 3d   || *zFilename==
2c23e 30 29 20 29 7b 0a 20 20 20 20 76 66 73 46 6c 61  0) ){.    vfsFla
2c23f 67 73 20 3d 20 28 76 66 73 46 6c 61 67 73 20 26  gs = (vfsFlags &
2c240 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41   ~SQLITE_OPEN_MA
2c241 49 4e 5f 44 42 29 20 7c 20 53 51 4c 49 54 45 5f  IN_DB) | SQLITE_
2c242 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 3b 0a 20 20  OPEN_TEMP_DB;.  
2c243 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  }.  rc = sqlite3
2c244 42 74 72 65 65 4f 70 65 6e 28 7a 46 69 6c 65 6e  BtreeOpen(zFilen
2c245 61 6d 65 2c 20 28 73 71 6c 69 74 65 33 20 2a 29  ame, (sqlite3 *)
2c246 64 62 2c 20 70 70 42 74 72 65 65 2c 20 62 74 46  db, ppBtree, btF
2c247 6c 61 67 73 2c 20 76 66 73 46 6c 61 67 73 29 3b  lags, vfsFlags);
2c248 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 42 2d  ..  /* If the B-
2c249 54 72 65 65 20 77 61 73 20 73 75 63 63 65 73 73  Tree was success
2c24a 66 75 6c 6c 79 20 6f 70 65 6e 65 64 2c 20 73 65  fully opened, se
2c24b 74 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68  t the pager-cach
2c24c 65 20 73 69 7a 65 20 74 6f 20 74 68 65 0a 20 20  e size to the.  
2c24d 2a 2a 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  ** default value
2c24e 2e 20 45 78 63 65 70 74 2c 20 69 66 20 74 68 65  . Except, if the
2c24f 20 63 61 6c 6c 20 74 6f 20 42 74 72 65 65 4f 70   call to BtreeOp
2c250 65 6e 28 29 20 72 65 74 75 72 6e 65 64 20 61 20  en() returned a 
2c251 68 61 6e 64 6c 65 0a 20 20 2a 2a 20 6f 70 65 6e  handle.  ** open
2c252 20 6f 6e 20 61 6e 20 65 78 69 73 74 69 6e 67 20   on an existing 
2c253 73 68 61 72 65 64 20 70 61 67 65 72 2d 63 61 63  shared pager-cac
2c254 68 65 2c 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67  he, do not chang
2c255 65 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68  e the pager-cach
2c256 65 20 0a 20 20 2a 2a 20 73 69 7a 65 2e 0a 20 20  e .  ** size..  
2c257 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  */.  if( rc==SQL
2c258 49 54 45 5f 4f 4b 20 26 26 20 30 3d 3d 73 71 6c  ITE_OK && 0==sql
2c259 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 28  ite3BtreeSchema(
2c25a 2a 70 70 42 74 72 65 65 2c 20 30 2c 20 30 29 20  *ppBtree, 0, 0) 
2c25b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  ){.    sqlite3Bt
2c25c 72 65 65 53 65 74 43 61 63 68 65 53 69 7a 65 28  reeSetCacheSize(
2c25d 2a 70 70 42 74 72 65 65 2c 20 6e 43 61 63 68 65  *ppBtree, nCache
2c25e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
2c25f 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  rc;.}../*.** Ret
2c260 75 72 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 65  urn UTF-8 encode
2c261 64 20 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61  d English langua
2c262 67 65 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20 6f  ge explanation o
2c263 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  f the most recen
2c264 74 0a 2a 2a 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53  t.** error..*/.S
2c265 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
2c266 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 65 72  char *sqlite3_er
2c267 72 6d 73 67 28 73 71 6c 69 74 65 33 20 2a 64 62  rmsg(sqlite3 *db
2c268 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
2c269 2a 7a 3b 0a 20 20 69 66 28 20 21 64 62 20 29 7b  *z;.  if( !db ){
2c26a 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
2c26b 74 65 33 45 72 72 53 74 72 28 53 51 4c 49 54 45  te3ErrStr(SQLITE
2c26c 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 69  _NOMEM);.  }.  i
2c26d 66 28 20 21 73 71 6c 69 74 65 33 53 61 66 65 74  f( !sqlite3Safet
2c26e 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64  yCheckSickOrOk(d
2c26f 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  b) ){.    return
2c270 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 53   sqlite3ErrStr(S
2c271 51 4c 49 54 45 5f 4d 49 53 55 53 45 29 3b 0a 20  QLITE_MISUSE);. 
2c272 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   }.  sqlite3_mut
2c273 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
2c274 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  ex);.  assert( !
2c275 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2c276 20 29 3b 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a   );.  z = (char*
2c277 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
2c278 65 78 74 28 64 62 2d 3e 70 45 72 72 29 3b 0a 20  ext(db->pErr);. 
2c279 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61   assert( !db->ma
2c27a 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
2c27b 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20  if( z==0 ){.    
2c27c 7a 20 3d 20 73 71 6c 69 74 65 33 45 72 72 53 74  z = sqlite3ErrSt
2c27d 72 28 64 62 2d 3e 65 72 72 43 6f 64 65 29 3b 0a  r(db->errCode);.
2c27e 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
2c27f 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
2c280 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a  tex);.  return z
2c281 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
2c282 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f  ITE_OMIT_UTF16./
2c283 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 55 54 46 2d  *.** Return UTF-
2c284 31 36 20 65 6e 63 6f 64 65 64 20 45 6e 67 6c 69  16 encoded Engli
2c285 73 68 20 6c 61 6e 67 75 61 67 65 20 65 78 70 6c  sh language expl
2c286 61 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d  anation of the m
2c287 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 65 72  ost recent.** er
2c288 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ror..*/.SQLITE_A
2c289 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
2c28a 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28  qlite3_errmsg16(
2c28b 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
2c28c 2f 2a 20 42 65 63 61 75 73 65 20 61 6c 6c 20 74  /* Because all t
2c28d 68 65 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  he characters in
2c28e 20 74 68 65 20 73 74 72 69 6e 67 20 61 72 65 20   the string are 
2c28f 69 6e 20 74 68 65 20 75 6e 69 63 6f 64 65 0a 20  in the unicode. 
2c290 20 2a 2a 20 72 61 6e 67 65 20 30 78 30 30 2d 30   ** range 0x00-0
2c291 78 46 46 2c 20 69 66 20 77 65 20 70 61 64 20 74  xFF, if we pad t
2c292 68 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 73 74  he big-endian st
2c293 72 69 6e 67 20 77 69 74 68 20 61 20 0a 20 20 2a  ring with a .  *
2c294 2a 20 7a 65 72 6f 20 62 79 74 65 2c 20 77 65 20  * zero byte, we 
2c295 63 61 6e 20 6f 62 74 61 69 6e 20 74 68 65 20 6c  can obtain the l
2c296 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 73 74 72  ittle-endian str
2c297 69 6e 67 20 77 69 74 68 0a 20 20 2a 2a 20 26 62  ing with.  ** &b
2c298 69 67 5f 65 6e 64 69 61 6e 5b 31 5d 2e 0a 20 20  ig_endian[1]..  
2c299 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  */.  static cons
2c29a 74 20 63 68 61 72 20 6f 75 74 4f 66 4d 65 6d 42  t char outOfMemB
2c29b 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 30 2c 20 27  e[] = {.    0, '
2c29c 6f 27 2c 20 30 2c 20 27 75 27 2c 20 30 2c 20 27  o', 0, 'u', 0, '
2c29d 74 27 2c 20 30 2c 20 27 20 27 2c 20 0a 20 20 20  t', 0, ' ', .   
2c29e 20 30 2c 20 27 6f 27 2c 20 30 2c 20 27 66 27 2c   0, 'o', 0, 'f',
2c29f 20 30 2c 20 27 20 27 2c 20 0a 20 20 20 20 30 2c   0, ' ', .    0,
2c2a0 20 27 6d 27 2c 20 30 2c 20 27 65 27 2c 20 30 2c   'm', 0, 'e', 0,
2c2a1 20 27 6d 27 2c 20 30 2c 20 27 6f 27 2c 20 30 2c   'm', 0, 'o', 0,
2c2a2 20 27 72 27 2c 20 30 2c 20 27 79 27 2c 20 30 2c   'r', 0, 'y', 0,
2c2a3 20 30 2c 20 30 0a 20 20 7d 3b 0a 20 20 73 74 61   0, 0.  };.  sta
2c2a4 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 6d  tic const char m
2c2a5 69 73 75 73 65 42 65 20 5b 5d 20 3d 20 7b 0a 20  isuseBe [] = {. 
2c2a6 20 20 20 30 2c 20 27 6c 27 2c 20 30 2c 20 27 69     0, 'l', 0, 'i
2c2a7 27 2c 20 30 2c 20 27 62 27 2c 20 30 2c 20 27 72  ', 0, 'b', 0, 'r
2c2a8 27 2c 20 30 2c 20 27 61 27 2c 20 30 2c 20 27 72  ', 0, 'a', 0, 'r
2c2a9 27 2c 20 30 2c 20 27 79 27 2c 20 30 2c 20 27 20  ', 0, 'y', 0, ' 
2c2aa 27 2c 20 0a 20 20 20 20 30 2c 20 27 72 27 2c 20  ', .    0, 'r', 
2c2ab 30 2c 20 27 6f 27 2c 20 30 2c 20 27 75 27 2c 20  0, 'o', 0, 'u', 
2c2ac 30 2c 20 27 74 27 2c 20 30 2c 20 27 69 27 2c 20  0, 't', 0, 'i', 
2c2ad 30 2c 20 27 6e 27 2c 20 30 2c 20 27 65 27 2c 20  0, 'n', 0, 'e', 
2c2ae 30 2c 20 27 20 27 2c 20 0a 20 20 20 20 30 2c 20  0, ' ', .    0, 
2c2af 27 63 27 2c 20 30 2c 20 27 61 27 2c 20 30 2c 20  'c', 0, 'a', 0, 
2c2b0 27 6c 27 2c 20 30 2c 20 27 6c 27 2c 20 30 2c 20  'l', 0, 'l', 0, 
2c2b1 27 65 27 2c 20 30 2c 20 27 64 27 2c 20 30 2c 20  'e', 0, 'd', 0, 
2c2b2 27 20 27 2c 20 0a 20 20 20 20 30 2c 20 27 6f 27  ' ', .    0, 'o'
2c2b3 2c 20 30 2c 20 27 75 27 2c 20 30 2c 20 27 74 27  , 0, 'u', 0, 't'
2c2b4 2c 20 30 2c 20 27 20 27 2c 20 0a 20 20 20 20 30  , 0, ' ', .    0
2c2b5 2c 20 27 6f 27 2c 20 30 2c 20 27 66 27 2c 20 30  , 'o', 0, 'f', 0
2c2b6 2c 20 27 20 27 2c 20 0a 20 20 20 20 30 2c 20 27  , ' ', .    0, '
2c2b7 73 27 2c 20 30 2c 20 27 65 27 2c 20 30 2c 20 27  s', 0, 'e', 0, '
2c2b8 71 27 2c 20 30 2c 20 27 75 27 2c 20 30 2c 20 27  q', 0, 'u', 0, '
2c2b9 65 27 2c 20 30 2c 20 27 6e 27 2c 20 30 2c 20 27  e', 0, 'n', 0, '
2c2ba 63 27 2c 20 30 2c 20 27 65 27 2c 20 30 2c 20 30  c', 0, 'e', 0, 0
2c2bb 2c 20 30 0a 20 20 7d 3b 0a 0a 20 20 63 6f 6e 73  , 0.  };..  cons
2c2bc 74 20 76 6f 69 64 20 2a 7a 3b 0a 20 20 69 66 28  t void *z;.  if(
2c2bd 20 21 64 62 20 29 7b 0a 20 20 20 20 72 65 74 75   !db ){.    retu
2c2be 72 6e 20 28 76 6f 69 64 20 2a 29 28 26 6f 75 74  rn (void *)(&out
2c2bf 4f 66 4d 65 6d 42 65 5b 53 51 4c 49 54 45 5f 55  OfMemBe[SQLITE_U
2c2c0 54 46 31 36 4e 41 54 49 56 45 3d 3d 53 51 4c 49  TF16NATIVE==SQLI
2c2c1 54 45 5f 55 54 46 31 36 4c 45 3f 31 3a 30 5d 29  TE_UTF16LE?1:0])
2c2c2 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 73 71 6c  ;.  }.  if( !sql
2c2c3 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 53  ite3SafetyCheckS
2c2c4 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20  ickOrOk(db) ){. 
2c2c5 20 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 20     return (void 
2c2c6 2a 29 28 26 6d 69 73 75 73 65 42 65 5b 53 51 4c  *)(&misuseBe[SQL
2c2c7 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3d  ITE_UTF16NATIVE=
2c2c8 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 3f  =SQLITE_UTF16LE?
2c2c9 31 3a 30 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  1:0]);.  }.  sql
2c2ca 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
2c2cb 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61  (db->mutex);.  a
2c2cc 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c  ssert( !db->mall
2c2cd 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 7a 20  ocFailed );.  z 
2c2ce 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
2c2cf 74 65 78 74 31 36 28 64 62 2d 3e 70 45 72 72 29  text16(db->pErr)
2c2d0 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a  ;.  if( z==0 ){.
2c2d1 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65      sqlite3Value
2c2d2 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c  SetStr(db->pErr,
2c2d3 20 2d 31 2c 20 73 71 6c 69 74 65 33 45 72 72 53   -1, sqlite3ErrS
2c2d4 74 72 28 64 62 2d 3e 65 72 72 43 6f 64 65 29 2c  tr(db->errCode),
2c2d5 0a 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45  .         SQLITE
2c2d6 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54  _UTF8, SQLITE_ST
2c2d7 41 54 49 43 29 3b 0a 20 20 20 20 7a 20 3d 20 73  ATIC);.    z = s
2c2d8 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
2c2d9 74 31 36 28 64 62 2d 3e 70 45 72 72 29 3b 0a 20  t16(db->pErr);. 
2c2da 20 7d 0a 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63   }.  /* A malloc
2c2db 28 29 20 6d 61 79 20 68 61 76 65 20 66 61 69 6c  () may have fail
2c2dc 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 63 61  ed within the ca
2c2dd 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 61  ll to sqlite3_va
2c2de 6c 75 65 5f 74 65 78 74 31 36 28 29 0a 20 20 2a  lue_text16().  *
2c2df 2a 20 61 62 6f 76 65 2e 20 49 66 20 74 68 69 73  * above. If this
2c2e0 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68   is the case, th
2c2e1 65 6e 20 74 68 65 20 64 62 2d 3e 6d 61 6c 6c 6f  en the db->mallo
2c2e2 63 46 61 69 6c 65 64 20 66 6c 61 67 20 6e 65 65  cFailed flag nee
2c2e3 64 73 20 74 6f 0a 20 20 2a 2a 20 62 65 20 63 6c  ds to.  ** be cl
2c2e4 65 61 72 65 64 20 62 65 66 6f 72 65 20 72 65 74  eared before ret
2c2e5 75 72 6e 69 6e 67 2e 20 44 6f 20 74 68 69 73 20  urning. Do this 
2c2e6 64 69 72 65 63 74 6c 79 2c 20 69 6e 73 74 65 61  directly, instea
2c2e7 64 20 6f 66 20 76 69 61 0a 20 20 2a 2a 20 73 71  d of via.  ** sq
2c2e8 6c 69 74 65 33 41 70 69 45 78 69 74 28 29 2c 20  lite3ApiExit(), 
2c2e9 74 6f 20 61 76 6f 69 64 20 73 65 74 74 69 6e 67  to avoid setting
2c2ea 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
2c2eb 6e 64 6c 65 20 65 72 72 6f 72 20 6d 65 73 73 61  ndle error messa
2c2ec 67 65 2e 0a 20 20 2a 2f 0a 20 20 64 62 2d 3e 6d  ge..  */.  db->m
2c2ed 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b  allocFailed = 0;
2c2ee 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2c2ef 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
2c2f0 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d  );.  return z;.}
2c2f1 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2c2f2 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a  E_OMIT_UTF16 */.
2c2f3 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
2c2f4 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72  e most recent er
2c2f5 72 6f 72 20 63 6f 64 65 20 67 65 6e 65 72 61 74  ror code generat
2c2f6 65 64 20 62 79 20 61 6e 20 53 51 4c 69 74 65 20  ed by an SQLite 
2c2f7 72 6f 75 74 69 6e 65 2e 20 49 66 20 4e 55 4c 4c  routine. If NULL
2c2f8 20 69 73 0a 2a 2a 20 70 61 73 73 65 64 20 74 6f   is.** passed to
2c2f9 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20   this function, 
2c2fa 77 65 20 61 73 73 75 6d 65 20 61 20 6d 61 6c 6c  we assume a mall
2c2fb 6f 63 28 29 20 66 61 69 6c 65 64 20 64 75 72 69  oc() failed duri
2c2fc 6e 67 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  ng sqlite3_open(
2c2fd 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  )..*/.SQLITE_API
2c2fe 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 72 72   int sqlite3_err
2c2ff 63 6f 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62  code(sqlite3 *db
2c300 29 7b 0a 20 20 69 66 28 20 64 62 20 26 26 20 21  ){.  if( db && !
2c301 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65  sqlite3SafetyChe
2c302 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29  ckSickOrOk(db) )
2c303 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
2c304 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a  ITE_MISUSE;.  }.
2c305 20 20 69 66 28 20 21 64 62 20 7c 7c 20 64 62 2d    if( !db || db-
2c306 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
2c307 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
2c308 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
2c309 72 65 74 75 72 6e 20 64 62 2d 3e 65 72 72 43 6f  return db->errCo
2c30a 64 65 20 26 20 64 62 2d 3e 65 72 72 4d 61 73 6b  de & db->errMask
2c30b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ;.}.SQLITE_API i
2c30c 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e  nt sqlite3_exten
2c30d 64 65 64 5f 65 72 72 63 6f 64 65 28 73 71 6c 69  ded_errcode(sqli
2c30e 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 20  te3 *db){.  if( 
2c30f 64 62 20 26 26 20 21 73 71 6c 69 74 65 33 53 61  db && !sqlite3Sa
2c310 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f  fetyCheckSickOrO
2c311 6b 28 64 62 29 20 29 7b 0a 20 20 20 20 72 65 74  k(db) ){.    ret
2c312 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  urn SQLITE_MISUS
2c313 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 64 62  E;.  }.  if( !db
2c314 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
2c315 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75  iled ){.    retu
2c316 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
2c317 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 64 62  .  }.  return db
2c318 2d 3e 65 72 72 43 6f 64 65 3b 0a 7d 0a 0a 2f 2a  ->errCode;.}../*
2c319 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
2c31a 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74   collating funct
2c31b 69 6f 6e 20 66 6f 72 20 64 61 74 61 62 61 73 65  ion for database
2c31c 20 22 64 62 22 2e 20 20 54 68 65 20 6e 61 6d 65   "db".  The name
2c31d 20 69 73 20 7a 4e 61 6d 65 0a 2a 2a 20 61 6e 64   is zName.** and
2c31e 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 69 73   the encoding is
2c31f 20 65 6e 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   enc..*/.static 
2c320 69 6e 74 20 63 72 65 61 74 65 43 6f 6c 6c 61 74  int createCollat
2c321 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a 20  ion(.  sqlite3* 
2c322 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  db, .  const cha
2c323 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74  r *zName, .  int
2c324 20 65 6e 63 2c 20 0a 20 20 76 6f 69 64 2a 20 70   enc, .  void* p
2c325 43 74 78 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d  Ctx,.  int(*xCom
2c326 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  pare)(void*,int,
2c327 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
2c328 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 0a 20 20  const void*),.  
2c329 76 6f 69 64 28 2a 78 44 65 6c 29 28 76 6f 69 64  void(*xDel)(void
2c32a 2a 29 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20  *).){.  CollSeq 
2c32b 2a 70 43 6f 6c 6c 3b 0a 20 20 69 6e 74 20 65 6e  *pColl;.  int en
2c32c 63 32 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b  c2;.  int nName;
2c32d 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 73 71  .  .  assert( sq
2c32e 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
2c32f 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a  (db->mutex) );..
2c330 20 20 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f 55    /* If SQLITE_U
2c331 54 46 31 36 20 69 73 20 73 70 65 63 69 66 69 65  TF16 is specifie
2c332 64 20 61 73 20 74 68 65 20 65 6e 63 6f 64 69 6e  d as the encodin
2c333 67 20 74 79 70 65 2c 20 74 72 61 6e 73 66 6f 72  g type, transfor
2c334 6d 20 74 68 69 73 0a 20 20 2a 2a 20 74 6f 20 6f  m this.  ** to o
2c335 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 55 54 46  ne of SQLITE_UTF
2c336 31 36 4c 45 20 6f 72 20 53 51 4c 49 54 45 5f 55  16LE or SQLITE_U
2c337 54 46 31 36 42 45 20 75 73 69 6e 67 20 74 68 65  TF16BE using the
2c338 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46  .  ** SQLITE_UTF
2c339 31 36 4e 41 54 49 56 45 20 6d 61 63 72 6f 2e 20  16NATIVE macro. 
2c33a 53 51 4c 49 54 45 5f 55 54 46 31 36 20 69 73 20  SQLITE_UTF16 is 
2c33b 6e 6f 74 20 75 73 65 64 20 69 6e 74 65 72 6e 61  not used interna
2c33c 6c 6c 79 2e 0a 20 20 2a 2f 0a 20 20 65 6e 63 32  lly..  */.  enc2
2c33d 20 3d 20 65 6e 63 20 26 20 7e 53 51 4c 49 54 45   = enc & ~SQLITE
2c33e 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 3b 0a  _UTF16_ALIGNED;.
2c33f 20 20 69 66 28 20 65 6e 63 32 3d 3d 53 51 4c 49    if( enc2==SQLI
2c340 54 45 5f 55 54 46 31 36 20 29 7b 0a 20 20 20 20  TE_UTF16 ){.    
2c341 65 6e 63 32 20 3d 20 53 51 4c 49 54 45 5f 55 54  enc2 = SQLITE_UT
2c342 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 7d 0a 20  F16NATIVE;.  }. 
2c343 20 69 66 28 20 28 65 6e 63 32 26 7e 33 29 21 3d   if( (enc2&~3)!=
2c344 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
2c345 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
2c346 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69   }..  /* Check i
2c347 66 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 72  f this call is r
2c348 65 6d 6f 76 69 6e 67 20 6f 72 20 72 65 70 6c 61  emoving or repla
2c349 63 69 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67  cing an existing
2c34a 20 63 6f 6c 6c 61 74 69 6f 6e 20 0a 20 20 2a 2a   collation .  **
2c34b 20 73 65 71 75 65 6e 63 65 2e 20 49 66 20 73 6f   sequence. If so
2c34c 2c 20 61 6e 64 20 74 68 65 72 65 20 61 72 65 20  , and there are 
2c34d 61 63 74 69 76 65 20 56 4d 73 2c 20 72 65 74 75  active VMs, retu
2c34e 72 6e 20 62 75 73 79 2e 20 49 66 20 74 68 65 72  rn busy. If ther
2c34f 65 0a 20 20 2a 2a 20 61 72 65 20 6e 6f 20 61 63  e.  ** are no ac
2c350 74 69 76 65 20 56 4d 73 2c 20 69 6e 76 61 6c 69  tive VMs, invali
2c351 64 61 74 65 20 61 6e 79 20 70 72 65 2d 63 6f 6d  date any pre-com
2c352 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73  piled statements
2c353 2e 0a 20 20 2a 2f 0a 20 20 6e 4e 61 6d 65 20 3d  ..  */.  nName =
2c354 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 28 64   sqlite3Strlen(d
2c355 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 70 43 6f  b, zName);.  pCo
2c356 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ll = sqlite3Find
2c357 43 6f 6c 6c 53 65 71 28 64 62 2c 20 28 75 38 29  CollSeq(db, (u8)
2c358 65 6e 63 32 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  enc2, zName, nNa
2c359 6d 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 43  me, 0);.  if( pC
2c35a 6f 6c 6c 20 26 26 20 70 43 6f 6c 6c 2d 3e 78 43  oll && pColl->xC
2c35b 6d 70 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62  mp ){.    if( db
2c35c 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 20  ->activeVdbeCnt 
2c35d 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2c35e 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45  Error(db, SQLITE
2c35f 5f 42 55 53 59 2c 20 0a 20 20 20 20 20 20 20 20  _BUSY, .        
2c360 22 55 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65 74  "Unable to delet
2c361 65 2f 6d 6f 64 69 66 79 20 63 6f 6c 6c 61 74 69  e/modify collati
2c362 6f 6e 20 73 65 71 75 65 6e 63 65 20 64 75 65 20  on sequence due 
2c363 74 6f 20 61 63 74 69 76 65 20 73 74 61 74 65 6d  to active statem
2c364 65 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 72 65  ents");.      re
2c365 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
2c366 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
2c367 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65  te3ExpirePrepare
2c368 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b  dStatements(db);
2c369 0a 0a 20 20 20 20 2f 2a 20 49 66 20 63 6f 6c 6c  ..    /* If coll
2c36a 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 70  ation sequence p
2c36b 43 6f 6c 6c 20 77 61 73 20 63 72 65 61 74 65 64  Coll was created
2c36c 20 64 69 72 65 63 74 6c 79 20 62 79 20 61 20 63   directly by a c
2c36d 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71  all to.    ** sq
2c36e 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
2c36f 6c 61 74 69 6f 6e 2c 20 61 6e 64 20 6e 6f 74 20  lation, and not 
2c370 67 65 6e 65 72 61 74 65 64 20 62 79 20 73 79 6e  generated by syn
2c371 74 68 43 6f 6c 6c 53 65 71 28 29 2c 0a 20 20 20  thCollSeq(),.   
2c372 20 2a 2a 20 74 68 65 6e 20 61 6e 79 20 63 6f 70   ** then any cop
2c373 69 65 73 20 6d 61 64 65 20 62 79 20 73 79 6e 74  ies made by synt
2c374 68 43 6f 6c 6c 53 65 71 28 29 20 6e 65 65 64 20  hCollSeq() need 
2c375 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65  to be invalidate
2c376 64 2e 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20  d..    ** Also, 
2c377 63 6f 6c 6c 61 74 69 6f 6e 20 64 65 73 74 72 75  collation destru
2c378 63 74 6f 72 20 2d 20 43 6f 6c 6c 53 65 71 2e 78  ctor - CollSeq.x
2c379 44 65 6c 28 29 20 2d 20 66 75 6e 63 74 69 6f 6e  Del() - function
2c37a 20 6d 61 79 20 6e 65 65 64 0a 20 20 20 20 2a 2a   may need.    **
2c37b 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 2e 0a 20   to be called.. 
2c37c 20 20 20 2a 2f 20 0a 20 20 20 20 69 66 28 20 28     */ .    if( (
2c37d 70 43 6f 6c 6c 2d 3e 65 6e 63 20 26 20 7e 53 51  pColl->enc & ~SQ
2c37e 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e  LITE_UTF16_ALIGN
2c37f 45 44 29 3d 3d 65 6e 63 32 20 29 7b 0a 20 20 20  ED)==enc2 ){.   
2c380 20 20 20 43 6f 6c 6c 53 65 71 20 2a 61 43 6f 6c     CollSeq *aCol
2c381 6c 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46  l = sqlite3HashF
2c382 69 6e 64 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65  ind(&db->aCollSe
2c383 71 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  q, zName, nName)
2c384 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20  ;.      int j;. 
2c385 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
2c386 33 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  3; j++){.       
2c387 20 43 6f 6c 6c 53 65 71 20 2a 70 20 3d 20 26 61   CollSeq *p = &a
2c388 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 20  Coll[j];.       
2c389 20 69 66 28 20 70 2d 3e 65 6e 63 3d 3d 70 43 6f   if( p->enc==pCo
2c38a 6c 6c 2d 3e 65 6e 63 20 29 7b 0a 20 20 20 20 20  ll->enc ){.     
2c38b 20 20 20 20 20 69 66 28 20 70 2d 3e 78 44 65 6c       if( p->xDel
2c38c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2c38d 70 2d 3e 78 44 65 6c 28 70 2d 3e 70 55 73 65 72  p->xDel(p->pUser
2c38e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
2c38f 20 20 20 20 20 20 20 20 20 70 2d 3e 78 43 6d 70           p->xCmp
2c390 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 0;.        }.
2c391 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2c392 7d 0a 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c  }..  pColl = sql
2c393 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28  ite3FindCollSeq(
2c394 64 62 2c 20 28 75 38 29 65 6e 63 32 2c 20 7a 4e  db, (u8)enc2, zN
2c395 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 31 29 3b 0a  ame, nName, 1);.
2c396 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20    if( pColl ){. 
2c397 20 20 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 3d     pColl->xCmp =
2c398 20 78 43 6f 6d 70 61 72 65 3b 0a 20 20 20 20 70   xCompare;.    p
2c399 43 6f 6c 6c 2d 3e 70 55 73 65 72 20 3d 20 70 43  Coll->pUser = pC
2c39a 74 78 3b 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e 78  tx;.    pColl->x
2c39b 44 65 6c 20 3d 20 78 44 65 6c 3b 0a 20 20 20 20  Del = xDel;.    
2c39c 70 43 6f 6c 6c 2d 3e 65 6e 63 20 3d 20 65 6e 63  pColl->enc = enc
2c39d 32 20 7c 20 28 65 6e 63 20 26 20 53 51 4c 49 54  2 | (enc & SQLIT
2c39e 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29  E_UTF16_ALIGNED)
2c39f 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45  ;.  }.  sqlite3E
2c3a0 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
2c3a1 4f 4b 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e  OK, 0);.  return
2c3a2 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a   SQLITE_OK;.}...
2c3a3 2f 2a 0a 2a 2a 20 54 68 69 73 20 61 72 72 61 79  /*.** This array
2c3a4 20 64 65 66 69 6e 65 73 20 68 61 72 64 20 75 70   defines hard up
2c3a5 70 65 72 20 62 6f 75 6e 64 73 20 6f 6e 20 6c 69  per bounds on li
2c3a6 6d 69 74 20 76 61 6c 75 65 73 2e 20 20 54 68 65  mit values.  The
2c3a7 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 72 20  .** initializer 
2c3a8 6d 75 73 74 20 62 65 20 6b 65 70 74 20 69 6e 20  must be kept in 
2c3a9 73 79 6e 63 20 77 69 74 68 20 74 68 65 20 53 51  sync with the SQ
2c3aa 4c 49 54 45 5f 4c 49 4d 49 54 5f 2a 0a 2a 2a 20  LITE_LIMIT_*.** 
2c3ab 23 64 65 66 69 6e 65 73 20 69 6e 20 73 71 6c 69  #defines in sqli
2c3ac 74 65 33 2e 68 2e 0a 2a 2f 0a 73 74 61 74 69 63  te3.h..*/.static
2c3ad 20 63 6f 6e 73 74 20 69 6e 74 20 61 48 61 72 64   const int aHard
2c3ae 4c 69 6d 69 74 5b 5d 20 3d 20 7b 0a 20 20 53 51  Limit[] = {.  SQ
2c3af 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 2c  LITE_MAX_LENGTH,
2c3b0 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51  .  SQLITE_MAX_SQ
2c3b1 4c 5f 4c 45 4e 47 54 48 2c 0a 20 20 53 51 4c 49  L_LENGTH,.  SQLI
2c3b2 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 2c 0a 20  TE_MAX_COLUMN,. 
2c3b3 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52   SQLITE_MAX_EXPR
2c3b4 5f 44 45 50 54 48 2c 0a 20 20 53 51 4c 49 54 45  _DEPTH,.  SQLITE
2c3b5 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45  _MAX_COMPOUND_SE
2c3b6 4c 45 43 54 2c 0a 20 20 53 51 4c 49 54 45 5f 4d  LECT,.  SQLITE_M
2c3b7 41 58 5f 56 44 42 45 5f 4f 50 2c 0a 20 20 53 51  AX_VDBE_OP,.  SQ
2c3b8 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f  LITE_MAX_FUNCTIO
2c3b9 4e 5f 41 52 47 2c 0a 20 20 53 51 4c 49 54 45 5f  N_ARG,.  SQLITE_
2c3ba 4d 41 58 5f 41 54 54 41 43 48 45 44 2c 0a 20 20  MAX_ATTACHED,.  
2c3bb 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f  SQLITE_MAX_LIKE_
2c3bc 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 2c 0a  PATTERN_LENGTH,.
2c3bd 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52    SQLITE_MAX_VAR
2c3be 49 41 42 4c 45 5f 4e 55 4d 42 45 52 2c 0a 7d 3b  IABLE_NUMBER,.};
2c3bf 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72  ../*.** Make sur
2c3c0 65 20 74 68 65 20 68 61 72 64 20 6c 69 6d 69 74  e the hard limit
2c3c1 73 20 61 72 65 20 73 65 74 20 74 6f 20 72 65 61  s are set to rea
2c3c2 73 6f 6e 61 62 6c 65 20 76 61 6c 75 65 73 0a 2a  sonable values.*
2c3c3 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  /.#if SQLITE_MAX
2c3c4 5f 4c 45 4e 47 54 48 3c 31 30 30 0a 23 20 65 72  _LENGTH<100.# er
2c3c5 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c  ror SQLITE_MAX_L
2c3c6 45 4e 47 54 48 20 6d 75 73 74 20 62 65 20 61 74  ENGTH must be at
2c3c7 20 6c 65 61 73 74 20 31 30 30 0a 23 65 6e 64 69   least 100.#endi
2c3c8 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  f.#if SQLITE_MAX
2c3c9 5f 53 51 4c 5f 4c 45 4e 47 54 48 3c 31 30 30 0a  _SQL_LENGTH<100.
2c3ca 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d  # error SQLITE_M
2c3cb 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 20 6d 75  AX_SQL_LENGTH mu
2c3cc 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 31  st be at least 1
2c3cd 30 30 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51  00.#endif.#if SQ
2c3ce 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e  LITE_MAX_SQL_LEN
2c3cf 47 54 48 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 4c  GTH>SQLITE_MAX_L
2c3d0 45 4e 47 54 48 0a 23 20 65 72 72 6f 72 20 53 51  ENGTH.# error SQ
2c3d1 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e  LITE_MAX_SQL_LEN
2c3d2 47 54 48 20 6d 75 73 74 20 6e 6f 74 20 62 65 20  GTH must not be 
2c3d3 67 72 65 61 74 65 72 20 74 68 61 6e 20 53 51 4c  greater than SQL
2c3d4 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a 23  ITE_MAX_LENGTH.#
2c3d5 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45  endif.#if SQLITE
2c3d6 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45  _MAX_COMPOUND_SE
2c3d7 4c 45 43 54 3c 32 0a 23 20 65 72 72 6f 72 20 53  LECT<2.# error S
2c3d8 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55  QLITE_MAX_COMPOU
2c3d9 4e 44 5f 53 45 4c 45 43 54 20 6d 75 73 74 20 62  ND_SELECT must b
2c3da 65 20 61 74 20 6c 65 61 73 74 20 32 0a 23 65 6e  e at least 2.#en
2c3db 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d  dif.#if SQLITE_M
2c3dc 41 58 5f 56 44 42 45 5f 4f 50 3c 34 30 0a 23 20  AX_VDBE_OP<40.# 
2c3dd 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58  error SQLITE_MAX
2c3de 5f 56 44 42 45 5f 4f 50 20 6d 75 73 74 20 62 65  _VDBE_OP must be
2c3df 20 61 74 20 6c 65 61 73 74 20 34 30 0a 23 65 6e   at least 40.#en
2c3e0 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d  dif.#if SQLITE_M
2c3e1 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 3c  AX_FUNCTION_ARG<
2c3e2 30 20 7c 7c 20 53 51 4c 49 54 45 5f 4d 41 58 5f  0 || SQLITE_MAX_
2c3e3 46 55 4e 43 54 49 4f 4e 5f 41 52 47 3e 31 30 30  FUNCTION_ARG>100
2c3e4 30 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45  0.# error SQLITE
2c3e5 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52  _MAX_FUNCTION_AR
2c3e6 47 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 65  G must be betwee
2c3e7 6e 20 30 20 61 6e 64 20 31 30 30 30 0a 23 65 6e  n 0 and 1000.#en
2c3e8 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d  dif.#if SQLITE_M
2c3e9 41 58 5f 41 54 54 41 43 48 45 44 3c 30 20 7c 7c  AX_ATTACHED<0 ||
2c3ea 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41   SQLITE_MAX_ATTA
2c3eb 43 48 45 44 3e 33 30 0a 23 20 65 72 72 6f 72 20  CHED>30.# error 
2c3ec 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43  SQLITE_MAX_ATTAC
2c3ed 48 45 44 20 6d 75 73 74 20 62 65 20 62 65 74 77  HED must be betw
2c3ee 65 65 6e 20 30 20 61 6e 64 20 33 30 0a 23 65 6e  een 0 and 30.#en
2c3ef 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d  dif.#if SQLITE_M
2c3f0 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f  AX_LIKE_PATTERN_
2c3f1 4c 45 4e 47 54 48 3c 31 0a 23 20 65 72 72 6f 72  LENGTH<1.# error
2c3f2 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45   SQLITE_MAX_LIKE
2c3f3 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 20  _PATTERN_LENGTH 
2c3f4 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 73 74  must be at least
2c3f5 20 31 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51   1.#endif.#if SQ
2c3f6 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c  LITE_MAX_VARIABL
2c3f7 45 5f 4e 55 4d 42 45 52 3c 31 0a 23 20 65 72 72  E_NUMBER<1.# err
2c3f8 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41  or SQLITE_MAX_VA
2c3f9 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 20 6d 75  RIABLE_NUMBER mu
2c3fa 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 31  st be at least 1
2c3fb 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
2c3fc 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 3e 33 32  TE_MAX_COLUMN>32
2c3fd 37 36 37 0a 23 20 65 72 72 6f 72 20 53 51 4c 49  767.# error SQLI
2c3fe 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 20 6d 75  TE_MAX_COLUMN mu
2c3ff 73 74 20 6e 6f 74 20 65 78 63 65 65 64 20 33 32  st not exceed 32
2c400 37 36 37 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  767.#endif.../*.
2c401 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61  ** Change the va
2c402 6c 75 65 20 6f 66 20 61 20 6c 69 6d 69 74 2e 20  lue of a limit. 
2c403 20 52 65 70 6f 72 74 20 74 68 65 20 6f 6c 64 20   Report the old 
2c404 76 61 6c 75 65 2e 0a 2a 2a 20 49 66 20 61 6e 20  value..** If an 
2c405 69 6e 76 61 6c 69 64 20 6c 69 6d 69 74 20 69 6e  invalid limit in
2c406 64 65 78 20 69 73 20 73 75 70 70 6c 69 65 64 2c  dex is supplied,
2c407 20 72 65 70 6f 72 74 20 2d 31 2e 0a 2a 2a 20 4d   report -1..** M
2c408 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 20 62  ake no changes b
2c409 75 74 20 73 74 69 6c 6c 20 72 65 70 6f 72 74 20  ut still report 
2c40a 74 68 65 20 6f 6c 64 20 76 61 6c 75 65 20 69 66  the old value if
2c40b 20 74 68 65 0a 2a 2a 20 6e 65 77 20 6c 69 6d 69   the.** new limi
2c40c 74 20 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 2a  t is negative..*
2c40d 2a 0a 2a 2a 20 41 20 6e 65 77 20 6c 6f 77 65 72  *.** A new lower
2c40e 20 6c 69 6d 69 74 20 64 6f 65 73 20 6e 6f 74 20   limit does not 
2c40f 73 68 72 69 6e 6b 20 65 78 69 73 74 69 6e 67 20  shrink existing 
2c410 63 6f 6e 73 74 72 75 63 74 73 2e 0a 2a 2a 20 49  constructs..** I
2c411 74 20 6d 65 72 65 6c 79 20 70 72 65 76 65 6e 74  t merely prevent
2c412 73 20 6e 65 77 20 63 6f 6e 73 74 72 75 63 74 73  s new constructs
2c413 20 74 68 61 74 20 65 78 63 65 65 64 20 74 68 65   that exceed the
2c414 20 6c 69 6d 69 74 0a 2a 2a 20 66 72 6f 6d 20 66   limit.** from f
2c415 6f 72 6d 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54  orming..*/.SQLIT
2c416 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2c417 33 5f 6c 69 6d 69 74 28 73 71 6c 69 74 65 33 20  3_limit(sqlite3 
2c418 2a 64 62 2c 20 69 6e 74 20 6c 69 6d 69 74 49 64  *db, int limitId
2c419 2c 20 69 6e 74 20 6e 65 77 4c 69 6d 69 74 29 7b  , int newLimit){
2c41a 0a 20 20 69 6e 74 20 6f 6c 64 4c 69 6d 69 74 3b  .  int oldLimit;
2c41b 0a 20 20 69 66 28 20 6c 69 6d 69 74 49 64 3c 30  .  if( limitId<0
2c41c 20 7c 7c 20 6c 69 6d 69 74 49 64 3e 3d 53 51 4c   || limitId>=SQL
2c41d 49 54 45 5f 4e 5f 4c 49 4d 49 54 20 29 7b 0a 20  ITE_N_LIMIT ){. 
2c41e 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20     return -1;.  
2c41f 7d 0a 20 20 6f 6c 64 4c 69 6d 69 74 20 3d 20 64  }.  oldLimit = d
2c420 62 2d 3e 61 4c 69 6d 69 74 5b 6c 69 6d 69 74 49  b->aLimit[limitI
2c421 64 5d 3b 0a 20 20 69 66 28 20 6e 65 77 4c 69 6d  d];.  if( newLim
2c422 69 74 3e 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  it>=0 ){.    if(
2c423 20 6e 65 77 4c 69 6d 69 74 3e 61 48 61 72 64 4c   newLimit>aHardL
2c424 69 6d 69 74 5b 6c 69 6d 69 74 49 64 5d 20 29 7b  imit[limitId] ){
2c425 0a 20 20 20 20 20 20 6e 65 77 4c 69 6d 69 74 20  .      newLimit 
2c426 3d 20 61 48 61 72 64 4c 69 6d 69 74 5b 6c 69 6d  = aHardLimit[lim
2c427 69 74 49 64 5d 3b 0a 20 20 20 20 7d 0a 20 20 20  itId];.    }.   
2c428 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 6c 69 6d 69   db->aLimit[limi
2c429 74 49 64 5d 20 3d 20 6e 65 77 4c 69 6d 69 74 3b  tId] = newLimit;
2c42a 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6f 6c  .  }.  return ol
2c42b 64 4c 69 6d 69 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  dLimit;.}../*.**
2c42c 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f   This routine do
2c42d 65 73 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 6f  es the work of o
2c42e 70 65 6e 69 6e 67 20 61 20 64 61 74 61 62 61 73  pening a databas
2c42f 65 20 6f 6e 20 62 65 68 61 6c 66 20 6f 66 0a 2a  e on behalf of.*
2c430 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29  * sqlite3_open()
2c431 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65   and sqlite3_ope
2c432 6e 31 36 28 29 2e 20 54 68 65 20 64 61 74 61 62  n16(). The datab
2c433 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 22 7a 46  ase filename "zF
2c434 69 6c 65 6e 61 6d 65 22 20 20 0a 2a 2a 20 69 73  ilename"  .** is
2c435 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 2e 0a   UTF-8 encoded..
2c436 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70  */.static int op
2c437 65 6e 44 61 74 61 62 61 73 65 28 0a 20 20 63 6f  enDatabase(.  co
2c438 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
2c439 61 6d 65 2c 20 2f 2a 20 44 61 74 61 62 61 73 65  ame, /* Database
2c43a 20 66 69 6c 65 6e 61 6d 65 20 55 54 46 2d 38 20   filename UTF-8 
2c43b 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 73 71 6c  encoded */.  sql
2c43c 69 74 65 33 20 2a 2a 70 70 44 62 2c 20 20 20 20  ite3 **ppDb,    
2c43d 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75      /* OUT: Retu
2c43e 72 6e 65 64 20 64 61 74 61 62 61 73 65 20 68 61  rned database ha
2c43f 6e 64 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  ndle */.  unsign
2c440 65 64 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20  ed flags,       
2c441 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 61 6c 20   /* Operational 
2c442 66 6c 61 67 73 20 2a 2f 0a 20 20 63 6f 6e 73 74  flags */.  const
2c443 20 63 68 61 72 20 2a 7a 56 66 73 20 20 20 20 20   char *zVfs     
2c444 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
2c445 20 56 46 53 20 74 6f 20 75 73 65 20 2a 2f 0a 29   VFS to use */.)
2c446 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  {.  sqlite3 *db;
2c447 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 43 6f 6c  .  int rc;.  Col
2c448 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69  lSeq *pColl;.  i
2c449 6e 74 20 69 73 54 68 72 65 61 64 73 61 66 65 3b  nt isThreadsafe;
2c44a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2c44b 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20  _OMIT_AUTOINIT. 
2c44c 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e   rc = sqlite3_in
2c44d 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66  itialize();.  if
2c44e 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
2c44f 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20  ;.#endif..  if( 
2c450 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
2c451 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 3d 3d  fig.bCoreMutex==
2c452 30 20 29 7b 0a 20 20 20 20 69 73 54 68 72 65 61  0 ){.    isThrea
2c453 64 73 61 66 65 20 3d 20 30 3b 0a 20 20 7d 65 6c  dsafe = 0;.  }el
2c454 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 53  se if( flags & S
2c455 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54  QLITE_OPEN_NOMUT
2c456 45 58 20 29 7b 0a 20 20 20 20 69 73 54 68 72 65  EX ){.    isThre
2c457 61 64 73 61 66 65 20 3d 20 30 3b 0a 20 20 7d 65  adsafe = 0;.  }e
2c458 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20  lse if( flags & 
2c459 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c  SQLITE_OPEN_FULL
2c45a 4d 55 54 45 58 20 29 7b 0a 20 20 20 20 69 73 54  MUTEX ){.    isT
2c45b 68 72 65 61 64 73 61 66 65 20 3d 20 31 3b 0a 20  hreadsafe = 1;. 
2c45c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 73 54 68   }else{.    isTh
2c45d 72 65 61 64 73 61 66 65 20 3d 20 73 71 6c 69 74  readsafe = sqlit
2c45e 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62  e3GlobalConfig.b
2c45f 46 75 6c 6c 4d 75 74 65 78 3b 0a 20 20 7d 0a 0a  FullMutex;.  }..
2c460 20 20 2f 2a 20 52 65 6d 6f 76 65 20 68 61 72 6d    /* Remove harm
2c461 66 75 6c 20 62 69 74 73 20 66 72 6f 6d 20 74 68  ful bits from th
2c462 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65  e flags paramete
2c463 72 20 2a 2f 0a 20 20 66 6c 61 67 73 20 26 3d 20  r */.  flags &= 
2c464 20 7e 28 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f   ~( SQLITE_OPEN_
2c465 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 7c 0a  DELETEONCLOSE |.
2c466 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
2c467 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
2c468 44 42 20 7c 0a 20 20 20 20 20 20 20 20 20 20 20  DB |.           
2c469 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
2c46a 54 45 4d 50 5f 44 42 20 7c 20 0a 20 20 20 20 20  TEMP_DB | .     
2c46b 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
2c46c 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f  _OPEN_TRANSIENT_
2c46d 44 42 20 7c 20 0a 20 20 20 20 20 20 20 20 20 20  DB | .          
2c46e 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
2c46f 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 20  _MAIN_JOURNAL | 
2c470 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2c471 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50  SQLITE_OPEN_TEMP
2c472 5f 4a 4f 55 52 4e 41 4c 20 7c 20 0a 20 20 20 20  _JOURNAL | .    
2c473 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
2c474 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41  E_OPEN_SUBJOURNA
2c475 4c 20 7c 20 0a 20 20 20 20 20 20 20 20 20 20 20  L | .           
2c476 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
2c477 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 7c  MASTER_JOURNAL |
2c478 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2c479 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55  SQLITE_OPEN_NOMU
2c47a 54 45 58 20 7c 0a 20 20 20 20 20 20 20 20 20 20  TEX |.          
2c47b 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
2c47c 5f 46 55 4c 4c 4d 55 54 45 58 0a 20 20 20 20 20  _FULLMUTEX.     
2c47d 20 20 20 20 20 20 20 20 29 3b 0a 0a 20 20 2f 2a          );..  /*
2c47e 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 73 71   Allocate the sq
2c47f 6c 69 74 65 20 64 61 74 61 20 73 74 72 75 63 74  lite data struct
2c480 75 72 65 20 2a 2f 0a 20 20 64 62 20 3d 20 73 71  ure */.  db = sq
2c481 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28  lite3MallocZero(
2c482 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 29   sizeof(sqlite3)
2c483 20 29 3b 0a 20 20 69 66 28 20 64 62 3d 3d 30 20   );.  if( db==0 
2c484 29 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75  ) goto opendb_ou
2c485 74 3b 0a 20 20 69 66 28 20 69 73 54 68 72 65 61  t;.  if( isThrea
2c486 64 73 61 66 65 20 29 7b 0a 20 20 20 20 64 62 2d  dsafe ){.    db-
2c487 3e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33  >mutex = sqlite3
2c488 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
2c489 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
2c48a 45 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e  E);.    if( db->
2c48b 6d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20  mutex==0 ){.    
2c48c 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 64    sqlite3_free(d
2c48d 62 29 3b 0a 20 20 20 20 20 20 64 62 20 3d 20 30  b);.      db = 0
2c48e 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65  ;.      goto ope
2c48f 6e 64 62 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  ndb_out;.    }. 
2c490 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   }.  sqlite3_mut
2c491 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
2c492 65 78 29 3b 0a 20 20 64 62 2d 3e 65 72 72 4d 61  ex);.  db->errMa
2c493 73 6b 20 3d 20 30 78 66 66 3b 0a 20 20 64 62 2d  sk = 0xff;.  db-
2c494 3e 70 72 69 6f 72 4e 65 77 52 6f 77 69 64 20 3d  >priorNewRowid =
2c495 20 30 3b 0a 20 20 64 62 2d 3e 6e 44 62 20 3d 20   0;.  db->nDb = 
2c496 32 3b 0a 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d  2;.  db->magic =
2c497 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55   SQLITE_MAGIC_BU
2c498 53 59 3b 0a 20 20 64 62 2d 3e 61 44 62 20 3d 20  SY;.  db->aDb = 
2c499 64 62 2d 3e 61 44 62 53 74 61 74 69 63 3b 0a 0a  db->aDbStatic;..
2c49a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
2c49b 28 64 62 2d 3e 61 4c 69 6d 69 74 29 3d 3d 73 69  (db->aLimit)==si
2c49c 7a 65 6f 66 28 61 48 61 72 64 4c 69 6d 69 74 29  zeof(aHardLimit)
2c49d 20 29 3b 0a 20 20 6d 65 6d 63 70 79 28 64 62 2d   );.  memcpy(db-
2c49e 3e 61 4c 69 6d 69 74 2c 20 61 48 61 72 64 4c 69  >aLimit, aHardLi
2c49f 6d 69 74 2c 20 73 69 7a 65 6f 66 28 64 62 2d 3e  mit, sizeof(db->
2c4a0 61 4c 69 6d 69 74 29 29 3b 0a 20 20 64 62 2d 3e  aLimit));.  db->
2c4a1 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a  autoCommit = 1;.
2c4a2 20 20 64 62 2d 3e 6e 65 78 74 41 75 74 6f 76 61    db->nextAutova
2c4a3 63 20 3d 20 2d 31 3b 0a 20 20 64 62 2d 3e 6e 65  c = -1;.  db->ne
2c4a4 78 74 50 61 67 65 73 69 7a 65 20 3d 20 30 3b 0a  xtPagesize = 0;.
2c4a5 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53    db->flags |= S
2c4a6 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61  QLITE_ShortColNa
2c4a7 6d 65 73 0a 23 69 66 20 53 51 4c 49 54 45 5f 44  mes.#if SQLITE_D
2c4a8 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46 4f 52 4d  EFAULT_FILE_FORM
2c4a9 41 54 3c 34 0a 20 20 20 20 20 20 20 20 20 20 20  AT<4.           
2c4aa 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 4c        | SQLITE_L
2c4ab 65 67 61 63 79 46 69 6c 65 46 6d 74 0a 23 65 6e  egacyFileFmt.#en
2c4ac 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
2c4ad 45 5f 45 4e 41 42 4c 45 5f 4c 4f 41 44 5f 45 58  E_ENABLE_LOAD_EX
2c4ae 54 45 4e 53 49 4f 4e 0a 20 20 20 20 20 20 20 20  TENSION.        
2c4af 20 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54           | SQLIT
2c4b0 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 0a  E_LoadExtension.
2c4b1 23 65 6e 64 69 66 0a 20 20 20 20 20 20 3b 0a 20  #endif.      ;. 
2c4b2 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74   sqlite3HashInit
2c4b3 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 2c 20  (&db->aCollSeq, 
2c4b4 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  0);.#ifndef SQLI
2c4b5 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
2c4b6 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 48 61  ABLE.  sqlite3Ha
2c4b7 73 68 49 6e 69 74 28 26 64 62 2d 3e 61 4d 6f 64  shInit(&db->aMod
2c4b8 75 6c 65 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a  ule, 0);.#endif.
2c4b9 0a 20 20 64 62 2d 3e 70 56 66 73 20 3d 20 73 71  .  db->pVfs = sq
2c4ba 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 7a  lite3_vfs_find(z
2c4bb 56 66 73 29 3b 0a 20 20 69 66 28 20 21 64 62 2d  Vfs);.  if( !db-
2c4bc 3e 70 56 66 73 20 29 7b 0a 20 20 20 20 72 63 20  >pVfs ){.    rc 
2c4bd 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
2c4be 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2c4bf 28 64 62 2c 20 72 63 2c 20 22 6e 6f 20 73 75 63  (db, rc, "no suc
2c4c0 68 20 76 66 73 3a 20 25 73 22 2c 20 7a 56 66 73  h vfs: %s", zVfs
2c4c1 29 3b 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e  );.    goto open
2c4c2 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f  db_out;.  }..  /
2c4c3 2a 20 41 64 64 20 74 68 65 20 64 65 66 61 75 6c  * Add the defaul
2c4c4 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  t collation sequ
2c4c5 65 6e 63 65 20 42 49 4e 41 52 59 2e 20 42 49 4e  ence BINARY. BIN
2c4c6 41 52 59 20 77 6f 72 6b 73 20 66 6f 72 20 62 6f  ARY works for bo
2c4c7 74 68 20 55 54 46 2d 38 0a 20 20 2a 2a 20 61 6e  th UTF-8.  ** an
2c4c8 64 20 55 54 46 2d 31 36 2c 20 73 6f 20 61 64 64  d UTF-16, so add
2c4c9 20 61 20 76 65 72 73 69 6f 6e 20 66 6f 72 20 65   a version for e
2c4ca 61 63 68 20 74 6f 20 61 76 6f 69 64 20 61 6e 79  ach to avoid any
2c4cb 20 75 6e 6e 65 63 65 73 73 61 72 79 0a 20 20 2a   unnecessary.  *
2c4cc 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 2e 20 54  * conversions. T
2c4cd 68 65 20 6f 6e 6c 79 20 65 72 72 6f 72 20 74 68  he only error th
2c4ce 61 74 20 63 61 6e 20 6f 63 63 75 72 20 68 65 72  at can occur her
2c4cf 65 20 69 73 20 61 20 6d 61 6c 6c 6f 63 28 29 20  e is a malloc() 
2c4d0 66 61 69 6c 75 72 65 2e 0a 20 20 2a 2f 0a 20 20  failure..  */.  
2c4d1 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28  createCollation(
2c4d2 64 62 2c 20 22 42 49 4e 41 52 59 22 2c 20 53 51  db, "BINARY", SQ
2c4d3 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 62 69  LITE_UTF8, 0, bi
2c4d4 6e 43 6f 6c 6c 46 75 6e 63 2c 20 30 29 3b 0a 20  nCollFunc, 0);. 
2c4d5 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e   createCollation
2c4d6 28 64 62 2c 20 22 42 49 4e 41 52 59 22 2c 20 53  (db, "BINARY", S
2c4d7 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 30  QLITE_UTF16BE, 0
2c4d8 2c 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c 20 30  , binCollFunc, 0
2c4d9 29 3b 0a 20 20 63 72 65 61 74 65 43 6f 6c 6c 61  );.  createColla
2c4da 74 69 6f 6e 28 64 62 2c 20 22 42 49 4e 41 52 59  tion(db, "BINARY
2c4db 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  ", SQLITE_UTF16L
2c4dc 45 2c 20 30 2c 20 62 69 6e 43 6f 6c 6c 46 75 6e  E, 0, binCollFun
2c4dd 63 2c 20 30 29 3b 0a 20 20 63 72 65 61 74 65 43  c, 0);.  createC
2c4de 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 22 52 54  ollation(db, "RT
2c4df 52 49 4d 22 2c 20 53 51 4c 49 54 45 5f 55 54 46  RIM", SQLITE_UTF
2c4e0 38 2c 20 28 76 6f 69 64 2a 29 31 2c 20 62 69 6e  8, (void*)1, bin
2c4e1 43 6f 6c 6c 46 75 6e 63 2c 20 30 29 3b 0a 20 20  CollFunc, 0);.  
2c4e2 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
2c4e3 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f  iled ){.    goto
2c4e4 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d   opendb_out;.  }
2c4e5 0a 20 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c  .  db->pDfltColl
2c4e6 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f   = sqlite3FindCo
2c4e7 6c 6c 53 65 71 28 64 62 2c 20 53 51 4c 49 54 45  llSeq(db, SQLITE
2c4e8 5f 55 54 46 38 2c 20 22 42 49 4e 41 52 59 22 2c  _UTF8, "BINARY",
2c4e9 20 36 2c 20 30 29 3b 0a 20 20 61 73 73 65 72 74   6, 0);.  assert
2c4ea 28 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 21  ( db->pDfltColl!
2c4eb 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 73 6f  =0 );..  /* Also
2c4ec 20 61 64 64 20 61 20 55 54 46 2d 38 20 63 61 73   add a UTF-8 cas
2c4ed 65 2d 69 6e 73 65 6e 73 69 74 69 76 65 20 63 6f  e-insensitive co
2c4ee 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
2c4ef 2e 20 2a 2f 0a 20 20 63 72 65 61 74 65 43 6f 6c  . */.  createCol
2c4f0 6c 61 74 69 6f 6e 28 64 62 2c 20 22 4e 4f 43 41  lation(db, "NOCA
2c4f1 53 45 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  SE", SQLITE_UTF8
2c4f2 2c 20 30 2c 20 6e 6f 63 61 73 65 43 6f 6c 6c 61  , 0, nocaseColla
2c4f3 74 69 6e 67 46 75 6e 63 2c 20 30 29 3b 0a 0a 20  tingFunc, 0);.. 
2c4f4 20 2f 2a 20 53 65 74 20 66 6c 61 67 73 20 6f 6e   /* Set flags on
2c4f5 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 63 6f   the built-in co
2c4f6 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
2c4f7 73 20 2a 2f 0a 20 20 64 62 2d 3e 70 44 66 6c 74  s */.  db->pDflt
2c4f8 43 6f 6c 6c 2d 3e 74 79 70 65 20 3d 20 53 51 4c  Coll->type = SQL
2c4f9 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59 3b  ITE_COLL_BINARY;
2c4fa 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74  .  pColl = sqlit
2c4fb 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62  e3FindCollSeq(db
2c4fc 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 22  , SQLITE_UTF8, "
2c4fd 4e 4f 43 41 53 45 22 2c 20 36 2c 20 30 29 3b 0a  NOCASE", 6, 0);.
2c4fe 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20    if( pColl ){. 
2c4ff 20 20 20 70 43 6f 6c 6c 2d 3e 74 79 70 65 20 3d     pColl->type =
2c500 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e 4f 43   SQLITE_COLL_NOC
2c501 41 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f  ASE;.  }..  /* O
2c502 70 65 6e 20 74 68 65 20 62 61 63 6b 65 6e 64 20  pen the backend 
2c503 64 61 74 61 62 61 73 65 20 64 72 69 76 65 72 20  database driver 
2c504 2a 2f 0a 20 20 64 62 2d 3e 6f 70 65 6e 46 6c 61  */.  db->openFla
2c505 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 72 63  gs = flags;.  rc
2c506 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 46   = sqlite3BtreeF
2c507 61 63 74 6f 72 79 28 64 62 2c 20 7a 46 69 6c 65  actory(db, zFile
2c508 6e 61 6d 65 2c 20 30 2c 20 53 51 4c 49 54 45 5f  name, 0, SQLITE_
2c509 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49  DEFAULT_CACHE_SI
2c50a 5a 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  ZE, .           
2c50b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c50c 66 6c 61 67 73 20 7c 20 53 51 4c 49 54 45 5f 4f  flags | SQLITE_O
2c50d 50 45 4e 5f 4d 41 49 4e 5f 44 42 2c 0a 20 20 20  PEN_MAIN_DB,.   
2c50e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c50f 20 20 20 20 20 20 20 20 26 64 62 2d 3e 61 44 62          &db->aDb
2c510 5b 30 5d 2e 70 42 74 29 3b 0a 20 20 69 66 28 20  [0].pBt);.  if( 
2c511 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
2c512 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
2c513 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20  ITE_IOERR_NOMEM 
2c514 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
2c515 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
2c516 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  }.    sqlite3Err
2c517 6f 72 28 64 62 2c 20 72 63 2c 20 30 29 3b 0a 20  or(db, rc, 0);. 
2c518 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f     goto opendb_o
2c519 75 74 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 61 44  ut;.  }.  db->aD
2c51a 62 5b 30 5d 2e 70 53 63 68 65 6d 61 20 3d 20 73  b[0].pSchema = s
2c51b 71 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74 28  qlite3SchemaGet(
2c51c 64 62 2c 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70  db, db->aDb[0].p
2c51d 42 74 29 3b 0a 20 20 64 62 2d 3e 61 44 62 5b 31  Bt);.  db->aDb[1
2c51e 5d 2e 70 53 63 68 65 6d 61 20 3d 20 73 71 6c 69  ].pSchema = sqli
2c51f 74 65 33 53 63 68 65 6d 61 47 65 74 28 64 62 2c  te3SchemaGet(db,
2c520 20 30 29 3b 0a 0a 0a 20 20 2f 2a 20 54 68 65 20   0);...  /* The 
2c521 64 65 66 61 75 6c 74 20 73 61 66 65 74 79 5f 6c  default safety_l
2c522 65 76 65 6c 20 66 6f 72 20 74 68 65 20 6d 61 69  evel for the mai
2c523 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 27 66  n database is 'f
2c524 75 6c 6c 27 3b 20 66 6f 72 20 74 68 65 20 74 65  ull'; for the te
2c525 6d 70 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65  mp.  ** database
2c526 20 69 74 20 69 73 20 27 4e 4f 4e 45 27 2e 20 54   it is 'NONE'. T
2c527 68 69 73 20 6d 61 74 63 68 65 73 20 74 68 65 20  his matches the 
2c528 70 61 67 65 72 20 6c 61 79 65 72 20 64 65 66 61  pager layer defa
2c529 75 6c 74 73 2e 20 20 0a 20 20 2a 2f 0a 20 20 64  ults.  .  */.  d
2c52a 62 2d 3e 61 44 62 5b 30 5d 2e 7a 4e 61 6d 65 20  b->aDb[0].zName 
2c52b 3d 20 22 6d 61 69 6e 22 3b 0a 20 20 64 62 2d 3e  = "main";.  db->
2c52c 61 44 62 5b 30 5d 2e 73 61 66 65 74 79 5f 6c 65  aDb[0].safety_le
2c52d 76 65 6c 20 3d 20 33 3b 0a 23 69 66 6e 64 65 66  vel = 3;.#ifndef
2c52e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d   SQLITE_OMIT_TEM
2c52f 50 44 42 0a 20 20 64 62 2d 3e 61 44 62 5b 31 5d  PDB.  db->aDb[1]
2c530 2e 7a 4e 61 6d 65 20 3d 20 22 74 65 6d 70 22 3b  .zName = "temp";
2c531 0a 20 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 73 61  .  db->aDb[1].sa
2c532 66 65 74 79 5f 6c 65 76 65 6c 20 3d 20 31 3b 0a  fety_level = 1;.
2c533 23 65 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 6d 61  #endif..  db->ma
2c534 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47  gic = SQLITE_MAG
2c535 49 43 5f 4f 50 45 4e 3b 0a 20 20 69 66 28 20 64  IC_OPEN;.  if( d
2c536 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2c537 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e  ){.    goto open
2c538 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f  db_out;.  }..  /
2c539 2a 20 52 65 67 69 73 74 65 72 20 61 6c 6c 20 62  * Register all b
2c53a 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e  uilt-in function
2c53b 73 2c 20 62 75 74 20 64 6f 20 6e 6f 74 20 61 74  s, but do not at
2c53c 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 74 68  tempt to read th
2c53d 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20  e.  ** database 
2c53e 73 63 68 65 6d 61 20 79 65 74 2e 20 54 68 69 73  schema yet. This
2c53f 20 69 73 20 64 65 6c 61 79 65 64 20 75 6e 74 69   is delayed unti
2c540 6c 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65  l the first time
2c541 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20   the database.  
2c542 2a 2a 20 69 73 20 61 63 63 65 73 73 65 64 2e 0a  ** is accessed..
2c543 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 72    */.  sqlite3Er
2c544 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f  ror(db, SQLITE_O
2c545 4b 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  K, 0);.  sqlite3
2c546 52 65 67 69 73 74 65 72 42 75 69 6c 74 69 6e 46  RegisterBuiltinF
2c547 75 6e 63 74 69 6f 6e 73 28 64 62 29 3b 0a 0a 20  unctions(db);.. 
2c548 20 2f 2a 20 4c 6f 61 64 20 61 75 74 6f 6d 61 74   /* Load automat
2c549 69 63 20 65 78 74 65 6e 73 69 6f 6e 73 20 2d 20  ic extensions - 
2c54a 65 78 74 65 6e 73 69 6f 6e 73 20 74 68 61 74 20  extensions that 
2c54b 68 61 76 65 20 62 65 65 6e 20 72 65 67 69 73 74  have been regist
2c54c 65 72 65 64 0a 20 20 2a 2a 20 75 73 69 6e 67 20  ered.  ** using 
2c54d 74 68 65 20 73 71 6c 69 74 65 33 5f 61 75 74 6f  the sqlite3_auto
2c54e 6d 61 74 69 63 5f 65 78 74 65 6e 73 69 6f 6e 28  matic_extension(
2c54f 29 20 41 50 49 2e 0a 20 20 2a 2f 0a 20 20 28 76  ) API..  */.  (v
2c550 6f 69 64 29 73 71 6c 69 74 65 33 41 75 74 6f 4c  oid)sqlite3AutoL
2c551 6f 61 64 45 78 74 65 6e 73 69 6f 6e 73 28 64 62  oadExtensions(db
2c552 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
2c553 5f 65 72 72 63 6f 64 65 28 64 62 29 21 3d 53 51  _errcode(db)!=SQ
2c554 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67  LITE_OK ){.    g
2c555 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a  oto opendb_out;.
2c556 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49    }..#ifdef SQLI
2c557 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 31 0a 20  TE_ENABLE_FTS1. 
2c558 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63   if( !db->malloc
2c559 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 65 78  Failed ){.    ex
2c55a 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33  tern int sqlite3
2c55b 46 74 73 31 49 6e 69 74 28 73 71 6c 69 74 65 33  Fts1Init(sqlite3
2c55c 2a 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  *);.    rc = sql
2c55d 69 74 65 33 46 74 73 31 49 6e 69 74 28 64 62 29  ite3Fts1Init(db)
2c55e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  ;.  }.#endif..#i
2c55f 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
2c560 4c 45 5f 46 54 53 32 0a 20 20 69 66 28 20 21 64  LE_FTS2.  if( !d
2c561 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2c562 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
2c563 20 29 7b 0a 20 20 20 20 65 78 74 65 72 6e 20 69   ){.    extern i
2c564 6e 74 20 73 71 6c 69 74 65 33 46 74 73 32 49 6e  nt sqlite3Fts2In
2c565 69 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20  it(sqlite3*);.  
2c566 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74    rc = sqlite3Ft
2c567 73 32 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a  s2Init(db);.  }.
2c568 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
2c569 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
2c56a 33 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c  3.  if( !db->mal
2c56b 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 72 63 3d  locFailed && rc=
2c56c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
2c56d 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74    rc = sqlite3Ft
2c56e 73 33 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a  s3Init(db);.  }.
2c56f 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
2c570 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55  QLITE_ENABLE_ICU
2c571 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c  .  if( !db->mall
2c572 6f 63 46 61 69 6c 65 64 20 26 26 20 72 63 3d 3d  ocFailed && rc==
2c573 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
2c574 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49 63 75   rc = sqlite3Icu
2c575 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65  Init(db);.  }.#e
2c576 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
2c577 49 54 45 5f 45 4e 41 42 4c 45 5f 52 54 52 45 45  ITE_ENABLE_RTREE
2c578 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c  .  if( !db->mall
2c579 6f 63 46 61 69 6c 65 64 20 26 26 20 72 63 3d 3d  ocFailed && rc==
2c57a 53 51 4c 49 54 45 5f 4f 4b 29 7b 0a 20 20 20 20  SQLITE_OK){.    
2c57b 72 63 20 3d 20 73 71 6c 69 74 65 33 52 74 72 65  rc = sqlite3Rtre
2c57c 65 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23  eInit(db);.  }.#
2c57d 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33  endif..  sqlite3
2c57e 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 30 29  Error(db, rc, 0)
2c57f 3b 0a 0a 20 20 2f 2a 20 2d 44 53 51 4c 49 54 45  ;..  /* -DSQLITE
2c580 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47  _DEFAULT_LOCKING
2c581 5f 4d 4f 44 45 3d 31 20 6d 61 6b 65 73 20 45 58  _MODE=1 makes EX
2c582 43 4c 55 53 49 56 45 20 74 68 65 20 64 65 66 61  CLUSIVE the defa
2c583 75 6c 74 20 6c 6f 63 6b 69 6e 67 0a 20 20 2a 2a  ult locking.  **
2c584 20 6d 6f 64 65 2e 20 20 2d 44 53 51 4c 49 54 45   mode.  -DSQLITE
2c585 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47  _DEFAULT_LOCKING
2c586 5f 4d 4f 44 45 3d 30 20 6d 61 6b 65 20 4e 4f 52  _MODE=0 make NOR
2c587 4d 41 4c 20 74 68 65 20 64 65 66 61 75 6c 74 20  MAL the default 
2c588 6c 6f 63 6b 69 6e 67 0a 20 20 2a 2a 20 6d 6f 64  locking.  ** mod
2c589 65 2e 20 20 44 6f 69 6e 67 20 6e 6f 74 68 69 6e  e.  Doing nothin
2c58a 67 20 61 74 20 61 6c 6c 20 61 6c 73 6f 20 6d 61  g at all also ma
2c58b 6b 65 73 20 4e 4f 52 4d 41 4c 20 74 68 65 20 64  kes NORMAL the d
2c58c 65 66 61 75 6c 74 2e 0a 20 20 2a 2f 0a 23 69 66  efault..  */.#if
2c58d 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55  def SQLITE_DEFAU
2c58e 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45 0a  LT_LOCKING_MODE.
2c58f 20 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f    db->dfltLockMo
2c590 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41  de = SQLITE_DEFA
2c591 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45  ULT_LOCKING_MODE
2c592 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72  ;.  sqlite3Pager
2c593 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 73 71 6c 69  LockingMode(sqli
2c594 74 65 33 42 74 72 65 65 50 61 67 65 72 28 64 62  te3BtreePager(db
2c595 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29 2c 0a 20  ->aDb[0].pBt),. 
2c596 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c597 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
2c598 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f  DEFAULT_LOCKING_
2c599 4d 4f 44 45 29 3b 0a 23 65 6e 64 69 66 0a 0a 20  MODE);.#endif.. 
2c59a 20 2f 2a 20 45 6e 61 62 6c 65 20 74 68 65 20 6c   /* Enable the l
2c59b 6f 6f 6b 61 73 69 64 65 2d 6d 61 6c 6c 6f 63 20  ookaside-malloc 
2c59c 73 75 62 73 79 73 74 65 6d 20 2a 2f 0a 20 20 73  subsystem */.  s
2c59d 65 74 75 70 4c 6f 6f 6b 61 73 69 64 65 28 64 62  etupLookaside(db
2c59e 2c 20 30 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62  , 0, sqlite3Glob
2c59f 61 6c 43 6f 6e 66 69 67 2e 73 7a 4c 6f 6f 6b 61  alConfig.szLooka
2c5a0 73 69 64 65 2c 0a 20 20 20 20 20 20 20 20 20 20  side,.          
2c5a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
2c5a2 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2c5a3 67 2e 6e 4c 6f 6f 6b 61 73 69 64 65 29 3b 0a 0a  g.nLookaside);..
2c5a4 6f 70 65 6e 64 62 5f 6f 75 74 3a 0a 20 20 69 66  opendb_out:.  if
2c5a5 28 20 64 62 20 29 7b 0a 20 20 20 20 61 73 73 65  ( db ){.    asse
2c5a6 72 74 28 20 64 62 2d 3e 6d 75 74 65 78 21 3d 30  rt( db->mutex!=0
2c5a7 20 7c 7c 20 69 73 54 68 72 65 61 64 73 61 66 65   || isThreadsafe
2c5a8 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c  ==0 || sqlite3Gl
2c5a9 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 46 75 6c 6c  obalConfig.bFull
2c5aa 4d 75 74 65 78 3d 3d 30 20 29 3b 0a 20 20 20 20  Mutex==0 );.    
2c5ab 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
2c5ac 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
2c5ad 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74    }.  rc = sqlit
2c5ae 65 33 5f 65 72 72 63 6f 64 65 28 64 62 29 3b 0a  e3_errcode(db);.
2c5af 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
2c5b0 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71  _NOMEM ){.    sq
2c5b1 6c 69 74 65 33 5f 63 6c 6f 73 65 28 64 62 29 3b  lite3_close(db);
2c5b2 0a 20 20 20 20 64 62 20 3d 20 30 3b 0a 20 20 7d  .    db = 0;.  }
2c5b3 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c  else if( rc!=SQL
2c5b4 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 64 62  ITE_OK ){.    db
2c5b5 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45  ->magic = SQLITE
2c5b6 5f 4d 41 47 49 43 5f 53 49 43 4b 3b 0a 20 20 7d  _MAGIC_SICK;.  }
2c5b7 0a 20 20 2a 70 70 44 62 20 3d 20 64 62 3b 0a 20  .  *ppDb = db;. 
2c5b8 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41   return sqlite3A
2c5b9 70 69 45 78 69 74 28 30 2c 20 72 63 29 3b 0a 7d  piExit(0, rc);.}
2c5ba 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e  ../*.** Open a n
2c5bb 65 77 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  ew database hand
2c5bc 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  le..*/.SQLITE_AP
2c5bd 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70  I int sqlite3_op
2c5be 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  en(.  const char
2c5bf 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 0a 20 20   *zFilename, .  
2c5c0 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 20 0a  sqlite3 **ppDb .
2c5c1 29 7b 0a 20 20 72 65 74 75 72 6e 20 6f 70 65 6e  ){.  return open
2c5c2 44 61 74 61 62 61 73 65 28 7a 46 69 6c 65 6e 61  Database(zFilena
2c5c3 6d 65 2c 20 70 70 44 62 2c 0a 20 20 20 20 20 20  me, ppDb,.      
2c5c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c5c5 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
2c5c6 57 52 49 54 45 20 7c 20 53 51 4c 49 54 45 5f 4f  WRITE | SQLITE_O
2c5c7 50 45 4e 5f 43 52 45 41 54 45 2c 20 30 29 3b 0a  PEN_CREATE, 0);.
2c5c8 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
2c5c9 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32   sqlite3_open_v2
2c5ca 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  (.  const char *
2c5cb 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44  filename,   /* D
2c5cc 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65  atabase filename
2c5cd 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71   (UTF-8) */.  sq
2c5ce 6c 69 74 65 33 20 2a 2a 70 70 44 62 2c 20 20 20  lite3 **ppDb,   
2c5cf 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51        /* OUT: SQ
2c5d0 4c 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a  Lite db handle *
2c5d1 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  /.  int flags,  
2c5d2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
2c5d3 6c 61 67 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  lags */.  const 
2c5d4 63 68 61 72 20 2a 7a 56 66 73 20 20 20 20 20 20  char *zVfs      
2c5d5 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 56 46 53    /* Name of VFS
2c5d6 20 6d 6f 64 75 6c 65 20 74 6f 20 75 73 65 20 2a   module to use *
2c5d7 2f 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 6f 70  /.){.  return op
2c5d8 65 6e 44 61 74 61 62 61 73 65 28 66 69 6c 65 6e  enDatabase(filen
2c5d9 61 6d 65 2c 20 70 70 44 62 2c 20 66 6c 61 67 73  ame, ppDb, flags
2c5da 2c 20 7a 56 66 73 29 3b 0a 7d 0a 0a 23 69 66 6e  , zVfs);.}..#ifn
2c5db 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2c5dc 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e  UTF16./*.** Open
2c5dd 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20   a new database 
2c5de 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54  handle..*/.SQLIT
2c5df 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2c5e0 33 5f 6f 70 65 6e 31 36 28 0a 20 20 63 6f 6e 73  3_open16(.  cons
2c5e1 74 20 76 6f 69 64 20 2a 7a 46 69 6c 65 6e 61 6d  t void *zFilenam
2c5e2 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a  e, .  sqlite3 **
2c5e3 70 70 44 62 0a 29 7b 0a 20 20 63 68 61 72 20 63  ppDb.){.  char c
2c5e4 6f 6e 73 74 20 2a 7a 46 69 6c 65 6e 61 6d 65 38  onst *zFilename8
2c5e5 3b 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65  ;   /* zFilename
2c5e6 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d   encoded in UTF-
2c5e7 38 20 69 6e 73 74 65 61 64 20 6f 66 20 55 54 46  8 instead of UTF
2c5e8 2d 31 36 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  -16 */.  sqlite3
2c5e9 5f 76 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20 20  _value *pVal;.  
2c5ea 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72  int rc;..  asser
2c5eb 74 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a  t( zFilename );.
2c5ec 20 20 61 73 73 65 72 74 28 20 70 70 44 62 20 29    assert( ppDb )
2c5ed 3b 0a 20 20 2a 70 70 44 62 20 3d 20 30 3b 0a 23  ;.  *ppDb = 0;.#
2c5ee 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2c5ef 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 72 63  IT_AUTOINIT.  rc
2c5f0 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69   = sqlite3_initi
2c5f1 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 72  alize();.  if( r
2c5f2 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 23  c ) return rc;.#
2c5f3 65 6e 64 69 66 0a 20 20 70 56 61 6c 20 3d 20 73  endif.  pVal = s
2c5f4 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 30  qlite3ValueNew(0
2c5f5 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 61 6c 75  );.  sqlite3Valu
2c5f6 65 53 65 74 53 74 72 28 70 56 61 6c 2c 20 2d 31  eSetStr(pVal, -1
2c5f7 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 53 51 4c  , zFilename, SQL
2c5f8 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c  ITE_UTF16NATIVE,
2c5f9 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
2c5fa 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 38 20 3d 20  .  zFilename8 = 
2c5fb 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74  sqlite3ValueText
2c5fc 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54  (pVal, SQLITE_UT
2c5fd 46 38 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65  F8);.  if( zFile
2c5fe 6e 61 6d 65 38 20 29 7b 0a 20 20 20 20 72 63 20  name8 ){.    rc 
2c5ff 3d 20 6f 70 65 6e 44 61 74 61 62 61 73 65 28 7a  = openDatabase(z
2c600 46 69 6c 65 6e 61 6d 65 38 2c 20 70 70 44 62 2c  Filename8, ppDb,
2c601 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2c602 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
2c603 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 20 53  EN_READWRITE | S
2c604 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
2c605 45 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65 72  E, 0);.    asser
2c606 74 28 20 2a 70 70 44 62 20 7c 7c 20 72 63 3d 3d  t( *ppDb || rc==
2c607 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a  SQLITE_NOMEM );.
2c608 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
2c609 54 45 5f 4f 4b 20 26 26 20 21 44 62 48 61 73 50  TE_OK && !DbHasP
2c60a 72 6f 70 65 72 74 79 28 2a 70 70 44 62 2c 20 30  roperty(*ppDb, 0
2c60b 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65  , DB_SchemaLoade
2c60c 64 29 20 29 7b 0a 20 20 20 20 20 20 45 4e 43 28  d) ){.      ENC(
2c60d 2a 70 70 44 62 29 20 3d 20 53 51 4c 49 54 45 5f  *ppDb) = SQLITE_
2c60e 55 54 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 20  UTF16NATIVE;.   
2c60f 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
2c610 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
2c611 4d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  M;.  }.  sqlite3
2c612 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29 3b  ValueFree(pVal);
2c613 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ..  return sqlit
2c614 65 33 41 70 69 45 78 69 74 28 30 2c 20 72 63 29  e3ApiExit(0, rc)
2c615 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
2c616 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
2c617 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74  */../*.** Regist
2c618 65 72 20 61 20 6e 65 77 20 63 6f 6c 6c 61 74 69  er a new collati
2c619 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69 74 68  on sequence with
2c61a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
2c61b 6e 64 6c 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49  ndle db..*/.SQLI
2c61c 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2c61d 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
2c61e 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a 20  ion(.  sqlite3* 
2c61f 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  db, .  const cha
2c620 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74  r *zName, .  int
2c621 20 65 6e 63 2c 20 0a 20 20 76 6f 69 64 2a 20 70   enc, .  void* p
2c622 43 74 78 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d  Ctx,.  int(*xCom
2c623 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  pare)(void*,int,
2c624 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
2c625 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 7b 0a  const void*).){.
2c626 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69    int rc;.  sqli
2c627 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
2c628 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73  db->mutex);.  as
2c629 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f  sert( !db->mallo
2c62a 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 72 63 20  cFailed );.  rc 
2c62b 3d 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f  = createCollatio
2c62c 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 65 6e 63  n(db, zName, enc
2c62d 2c 20 70 43 74 78 2c 20 78 43 6f 6d 70 61 72 65  , pCtx, xCompare
2c62e 2c 20 30 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  , 0);.  rc = sql
2c62f 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20  ite3ApiExit(db, 
2c630 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  rc);.  sqlite3_m
2c631 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
2c632 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
2c633 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67  rc;.}../*.** Reg
2c634 69 73 74 65 72 20 61 20 6e 65 77 20 63 6f 6c 6c  ister a new coll
2c635 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77  ation sequence w
2c636 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73 65  ith the database
2c637 20 68 61 6e 64 6c 65 20 64 62 2e 0a 2a 2f 0a 53   handle db..*/.S
2c638 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2c639 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
2c63a 6c 61 74 69 6f 6e 5f 76 32 28 0a 20 20 73 71 6c  lation_v2(.  sql
2c63b 69 74 65 33 2a 20 64 62 2c 20 0a 20 20 63 6f 6e  ite3* db, .  con
2c63c 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
2c63d 0a 20 20 69 6e 74 20 65 6e 63 2c 20 0a 20 20 76  .  int enc, .  v
2c63e 6f 69 64 2a 20 70 43 74 78 2c 0a 20 20 69 6e 74  oid* pCtx,.  int
2c63f 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64  (*xCompare)(void
2c640 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
2c641 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
2c642 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78 44 65 6c  *),.  void(*xDel
2c643 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e  )(void*).){.  in
2c644 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f  t rc;.  sqlite3_
2c645 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
2c646 6d 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72 74  mutex);.  assert
2c647 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
2c648 6c 65 64 20 29 3b 0a 20 20 72 63 20 3d 20 63 72  led );.  rc = cr
2c649 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62  eateCollation(db
2c64a 2c 20 7a 4e 61 6d 65 2c 20 65 6e 63 2c 20 70 43  , zName, enc, pC
2c64b 74 78 2c 20 78 43 6f 6d 70 61 72 65 2c 20 78 44  tx, xCompare, xD
2c64c 65 6c 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  el);.  rc = sqli
2c64d 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72  te3ApiExit(db, r
2c64e 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  c);.  sqlite3_mu
2c64f 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
2c650 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  tex);.  return r
2c651 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  c;.}..#ifndef SQ
2c652 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
2c653 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61  /*.** Register a
2c654 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   new collation s
2c655 65 71 75 65 6e 63 65 20 77 69 74 68 20 74 68 65  equence with the
2c656 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
2c657 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41   db..*/.SQLITE_A
2c658 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
2c659 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31  reate_collation1
2c65a 36 28 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62  6(.  sqlite3* db
2c65b 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  , .  const void 
2c65c 2a 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 65 6e  *zName,.  int en
2c65d 63 2c 20 0a 20 20 76 6f 69 64 2a 20 70 43 74 78  c, .  void* pCtx
2c65e 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72  ,.  int(*xCompar
2c65f 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  e)(void*,int,con
2c660 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  st void*,int,con
2c661 73 74 20 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69  st void*).){.  i
2c662 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
2c663 4b 3b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65  K;.  char *zName
2c664 38 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  8;.  sqlite3_mut
2c665 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
2c666 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  ex);.  assert( !
2c667 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2c668 20 29 3b 0a 20 20 7a 4e 61 6d 65 38 20 3d 20 73   );.  zName8 = s
2c669 71 6c 69 74 65 33 55 74 66 31 36 74 6f 38 28 64  qlite3Utf16to8(d
2c66a 62 2c 20 7a 4e 61 6d 65 2c 20 2d 31 29 3b 0a 20  b, zName, -1);. 
2c66b 20 69 66 28 20 7a 4e 61 6d 65 38 20 29 7b 0a 20   if( zName8 ){. 
2c66c 20 20 20 72 63 20 3d 20 63 72 65 61 74 65 43 6f     rc = createCo
2c66d 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d  llation(db, zNam
2c66e 65 38 2c 20 65 6e 63 2c 20 70 43 74 78 2c 20 78  e8, enc, pCtx, x
2c66f 43 6f 6d 70 61 72 65 2c 20 30 29 3b 0a 20 20 20  Compare, 0);.   
2c670 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2c671 62 2c 20 7a 4e 61 6d 65 38 29 3b 0a 20 20 7d 0a  b, zName8);.  }.
2c672 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70    rc = sqlite3Ap
2c673 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20  iExit(db, rc);. 
2c674 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
2c675 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
2c676 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2c677 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2c678 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a  _OMIT_UTF16 */..
2c679 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61  /*.** Register a
2c67a 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
2c67b 6e 63 65 20 66 61 63 74 6f 72 79 20 63 61 6c 6c  nce factory call
2c67c 62 61 63 6b 20 77 69 74 68 20 74 68 65 20 64 61  back with the da
2c67d 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a  tabase handle.**
2c67e 20 64 62 2e 20 52 65 70 6c 61 63 65 20 61 6e 79   db. Replace any
2c67f 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 73 74   previously inst
2c680 61 6c 6c 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20  alled collation 
2c681 73 65 71 75 65 6e 63 65 20 66 61 63 74 6f 72 79  sequence factory
2c682 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2c683 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c  int sqlite3_coll
2c684 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 0a 20 20  ation_needed(.  
2c685 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20  sqlite3 *db, .  
2c686 76 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65 65 64 65  void *pCollNeede
2c687 64 41 72 67 2c 20 0a 20 20 76 6f 69 64 28 2a 78  dArg, .  void(*x
2c688 43 6f 6c 6c 4e 65 65 64 65 64 29 28 76 6f 69 64  CollNeeded)(void
2c689 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65  *,sqlite3*,int e
2c68a 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68  TextRep,const ch
2c68b 61 72 2a 29 0a 29 7b 0a 20 20 73 71 6c 69 74 65  ar*).){.  sqlite
2c68c 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
2c68d 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e  ->mutex);.  db->
2c68e 78 43 6f 6c 6c 4e 65 65 64 65 64 20 3d 20 78 43  xCollNeeded = xC
2c68f 6f 6c 6c 4e 65 65 64 65 64 3b 0a 20 20 64 62 2d  ollNeeded;.  db-
2c690 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 20 3d  >xCollNeeded16 =
2c691 20 30 3b 0a 20 20 64 62 2d 3e 70 43 6f 6c 6c 4e   0;.  db->pCollN
2c692 65 65 64 65 64 41 72 67 20 3d 20 70 43 6f 6c 6c  eededArg = pColl
2c693 4e 65 65 64 65 64 41 72 67 3b 0a 20 20 73 71 6c  NeededArg;.  sql
2c694 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
2c695 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
2c696 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2c697 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
2c698 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a  TE_OMIT_UTF16./*
2c699 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 63  .** Register a c
2c69a 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
2c69b 65 20 66 61 63 74 6f 72 79 20 63 61 6c 6c 62 61  e factory callba
2c69c 63 6b 20 77 69 74 68 20 74 68 65 20 64 61 74 61  ck with the data
2c69d 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 64  base handle.** d
2c69e 62 2e 20 52 65 70 6c 61 63 65 20 61 6e 79 20 70  b. Replace any p
2c69f 72 65 76 69 6f 75 73 6c 79 20 69 6e 73 74 61 6c  reviously instal
2c6a0 6c 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  led collation se
2c6a1 71 75 65 6e 63 65 20 66 61 63 74 6f 72 79 2e 0a  quence factory..
2c6a2 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
2c6a3 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74  t sqlite3_collat
2c6a4 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 0a 20 20  ion_needed16(.  
2c6a5 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20  sqlite3 *db, .  
2c6a6 76 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65 65 64 65  void *pCollNeede
2c6a7 64 41 72 67 2c 20 0a 20 20 76 6f 69 64 28 2a 78  dArg, .  void(*x
2c6a8 43 6f 6c 6c 4e 65 65 64 65 64 31 36 29 28 76 6f  CollNeeded16)(vo
2c6a9 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74  id*,sqlite3*,int
2c6aa 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20   eTextRep,const 
2c6ab 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 73 71 6c 69  void*).){.  sqli
2c6ac 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
2c6ad 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62  db->mutex);.  db
2c6ae 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20 3d 20  ->xCollNeeded = 
2c6af 30 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65  0;.  db->xCollNe
2c6b0 65 64 65 64 31 36 20 3d 20 78 43 6f 6c 6c 4e 65  eded16 = xCollNe
2c6b1 65 64 65 64 31 36 3b 0a 20 20 64 62 2d 3e 70 43  eded16;.  db->pC
2c6b2 6f 6c 6c 4e 65 65 64 65 64 41 72 67 20 3d 20 70  ollNeededArg = p
2c6b3 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 3b 0a 20  CollNeededArg;. 
2c6b4 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
2c6b5 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
2c6b6 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
2c6b7 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  _OK;.}.#endif /*
2c6b8 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
2c6b9 31 36 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  16 */..#ifndef S
2c6ba 51 4c 49 54 45 5f 4f 4d 49 54 5f 47 4c 4f 42 41  QLITE_OMIT_GLOBA
2c6bb 4c 52 45 43 4f 56 45 52 0a 23 69 66 6e 64 65 66  LRECOVER.#ifndef
2c6bc 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50   SQLITE_OMIT_DEP
2c6bd 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 54 68  RECATED./*.** Th
2c6be 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e  is function is n
2c6bf 6f 77 20 61 6e 20 61 6e 61 63 68 72 6f 6e 69 73  ow an anachronis
2c6c0 6d 2e 20 49 74 20 75 73 65 64 20 74 6f 20 62 65  m. It used to be
2c6c1 20 75 73 65 64 20 74 6f 20 72 65 63 6f 76 65 72   used to recover
2c6c2 20 66 72 6f 6d 20 61 0a 2a 2a 20 6d 61 6c 6c 6f   from a.** mallo
2c6c3 63 28 29 20 66 61 69 6c 75 72 65 2c 20 62 75 74  c() failure, but
2c6c4 20 53 51 4c 69 74 65 20 6e 6f 77 20 64 6f 65 73   SQLite now does
2c6c5 20 74 68 69 73 20 61 75 74 6f 6d 61 74 69 63 61   this automatica
2c6c6 6c 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  lly..*/.SQLITE_A
2c6c7 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67  PI int sqlite3_g
2c6c8 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28 76 6f  lobal_recover(vo
2c6c9 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 53 51  id){.  return SQ
2c6ca 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69  LITE_OK;.}.#endi
2c6cb 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  f.#endif../*.** 
2c6cc 54 65 73 74 20 74 6f 20 73 65 65 20 77 68 65 74  Test to see whet
2c6cd 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 64  her or not the d
2c6ce 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
2c6cf 6f 6e 20 69 73 20 69 6e 20 61 75 74 6f 63 6f 6d  on is in autocom
2c6d0 6d 69 74 0a 2a 2a 20 6d 6f 64 65 2e 20 20 52 65  mit.** mode.  Re
2c6d1 74 75 72 6e 20 54 52 55 45 20 69 66 20 69 74 20  turn TRUE if it 
2c6d2 69 73 20 61 6e 64 20 46 41 4c 53 45 20 69 66 20  is and FALSE if 
2c6d3 6e 6f 74 2e 20 20 41 75 74 6f 63 6f 6d 6d 69 74  not.  Autocommit
2c6d4 20 6d 6f 64 65 20 69 73 20 6f 6e 0a 2a 2a 20 62   mode is on.** b
2c6d5 79 20 64 65 66 61 75 6c 74 2e 20 20 41 75 74 6f  y default.  Auto
2c6d6 63 6f 6d 6d 69 74 20 69 73 20 64 69 73 61 62 6c  commit is disabl
2c6d7 65 64 20 62 79 20 61 20 42 45 47 49 4e 20 73 74  ed by a BEGIN st
2c6d8 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65 65 6e  atement and reen
2c6d9 61 62 6c 65 64 0a 2a 2a 20 62 79 20 74 68 65 20  abled.** by the 
2c6da 6e 65 78 74 20 43 4f 4d 4d 49 54 20 6f 72 20 52  next COMMIT or R
2c6db 4f 4c 4c 42 41 43 4b 2e 0a 2a 2a 0a 2a 2a 2a 2a  OLLBACK..**.****
2c6dc 2a 2a 2a 20 54 48 49 53 20 49 53 20 41 4e 20 45  *** THIS IS AN E
2c6dd 58 50 45 52 49 4d 45 4e 54 41 4c 20 41 50 49 20  XPERIMENTAL API 
2c6de 41 4e 44 20 49 53 20 53 55 42 4a 45 43 54 20 54  AND IS SUBJECT T
2c6df 4f 20 43 48 41 4e 47 45 20 2a 2a 2a 2a 2a 2a 0a  O CHANGE ******.
2c6e0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
2c6e1 74 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75  t sqlite3_get_au
2c6e2 74 6f 63 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33  tocommit(sqlite3
2c6e3 20 2a 64 62 29 7b 0a 20 20 72 65 74 75 72 6e 20   *db){.  return 
2c6e4 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3b 0a  db->autoCommit;.
2c6e5 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
2c6e6 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65  _DEBUG./*.** The
2c6e7 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69   following routi
2c6e8 6e 65 20 69 73 20 73 75 62 74 69 74 75 74 65 64  ne is subtituted
2c6e9 20 66 6f 72 20 63 6f 6e 73 74 61 6e 74 20 53 51   for constant SQ
2c6ea 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69 6e 0a  LITE_CORRUPT in.
2c6eb 2a 2a 20 64 65 62 75 67 67 69 6e 67 20 62 75 69  ** debugging bui
2c6ec 6c 64 73 2e 20 20 54 68 69 73 20 70 72 6f 76 69  lds.  This provi
2c6ed 64 65 73 20 61 20 77 61 79 20 74 6f 20 73 65 74  des a way to set
2c6ee 20 61 20 62 72 65 61 6b 70 6f 69 6e 74 20 66 6f   a breakpoint fo
2c6ef 72 20 77 68 65 6e 0a 2a 2a 20 63 6f 72 72 75 70  r when.** corrup
2c6f0 74 69 6f 6e 20 69 73 20 66 69 72 73 74 20 64 65  tion is first de
2c6f1 74 65 63 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  tected..*/.SQLIT
2c6f2 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2c6f3 6c 69 74 65 33 43 6f 72 72 75 70 74 28 76 6f 69  lite3Corrupt(voi
2c6f4 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  d){.  return SQL
2c6f5 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a 7d 0a 23  ITE_CORRUPT;.}.#
2c6f6 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53  endif..#ifndef S
2c6f7 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45  QLITE_OMIT_DEPRE
2c6f8 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 54 68 69 73  CATED./*.** This
2c6f9 20 69 73 20 61 20 63 6f 6e 76 65 6e 69 65 6e 63   is a convenienc
2c6fa 65 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20 6d  e routine that m
2c6fb 61 6b 65 73 20 73 75 72 65 20 74 68 61 74 20 61  akes sure that a
2c6fc 6c 6c 20 74 68 72 65 61 64 2d 73 70 65 63 69 66  ll thread-specif
2c6fd 69 63 0a 2a 2a 20 64 61 74 61 20 66 6f 72 20 74  ic.** data for t
2c6fe 68 69 73 20 74 68 72 65 61 64 20 68 61 73 20 62  his thread has b
2c6ff 65 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e  een deallocated.
2c700 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 6e 6f  .**.** SQLite no
2c701 20 6c 6f 6e 67 65 72 20 75 73 65 73 20 74 68 72   longer uses thr
2c702 65 61 64 2d 73 70 65 63 69 66 69 63 20 64 61 74  ead-specific dat
2c703 61 20 73 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  a so this routin
2c704 65 20 69 73 20 6e 6f 77 20 61 0a 2a 2a 20 6e 6f  e is now a.** no
2c705 2d 6f 70 2e 20 20 49 74 20 69 73 20 72 65 74 61  -op.  It is reta
2c706 69 6e 65 64 20 66 6f 72 20 68 69 73 74 6f 72 69  ined for histori
2c707 63 61 6c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74  cal compatibilit
2c708 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  y..*/.SQLITE_API
2c709 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 74 68   void sqlite3_th
2c70a 72 65 61 64 5f 63 6c 65 61 6e 75 70 28 76 6f 69  read_cleanup(voi
2c70b 64 29 7b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  d){.}.#endif../*
2c70c 0a 2a 2a 20 52 65 74 75 72 6e 20 6d 65 74 61 20  .** Return meta 
2c70d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
2c70e 74 20 61 20 73 70 65 63 69 66 69 63 20 63 6f 6c  t a specific col
2c70f 75 6d 6e 20 6f 66 20 61 20 64 61 74 61 62 61 73  umn of a databas
2c710 65 20 74 61 62 6c 65 2e 0a 2a 2a 20 53 65 65 20  e table..** See 
2c711 63 6f 6d 6d 65 6e 74 20 69 6e 20 73 71 6c 69 74  comment in sqlit
2c712 65 33 2e 68 20 28 73 71 6c 69 74 65 2e 68 2e 69  e3.h (sqlite.h.i
2c713 6e 29 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a  n) for details..
2c714 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
2c715 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d  _ENABLE_COLUMN_M
2c716 45 54 41 44 41 54 41 0a 53 51 4c 49 54 45 5f 41  ETADATA.SQLITE_A
2c717 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74  PI int sqlite3_t
2c718 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61  able_column_meta
2c719 64 61 74 61 28 0a 20 20 73 71 6c 69 74 65 33 20  data(.  sqlite3 
2c71a 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
2c71b 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f      /* Connectio
2c71c 6e 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f  n handle */.  co
2c71d 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d  nst char *zDbNam
2c71e 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74  e,        /* Dat
2c71f 61 62 61 73 65 20 6e 61 6d 65 20 6f 72 20 4e 55  abase name or NU
2c720 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  LL */.  const ch
2c721 61 72 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 2c 20  ar *zTableName, 
2c722 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d      /* Table nam
2c723 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
2c724 72 20 2a 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 2c 20  r *zColumnName, 
2c725 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d     /* Column nam
2c726 65 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73  e */.  char cons
2c727 74 20 2a 2a 70 7a 44 61 74 61 54 79 70 65 2c 20  t **pzDataType, 
2c728 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 44 65     /* OUTPUT: De
2c729 63 6c 61 72 65 64 20 64 61 74 61 20 74 79 70 65  clared data type
2c72a 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 74   */.  char const
2c72b 20 2a 2a 70 7a 43 6f 6c 6c 53 65 71 2c 20 20 20   **pzCollSeq,   
2c72c 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 43 6f 6c    /* OUTPUT: Col
2c72d 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
2c72e 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  name */.  int *p
2c72f 4e 6f 74 4e 75 6c 6c 2c 20 20 20 20 20 20 20 20  NotNull,        
2c730 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a        /* OUTPUT:
2c731 20 54 72 75 65 20 69 66 20 4e 4f 54 20 4e 55 4c   True if NOT NUL
2c732 4c 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 78 69  L constraint exi
2c733 73 74 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 50  sts */.  int *pP
2c734 72 69 6d 61 72 79 4b 65 79 2c 20 20 20 20 20 20  rimaryKey,      
2c735 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20       /* OUTPUT: 
2c736 54 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 70  True if column p
2c737 61 72 74 20 6f 66 20 50 4b 20 2a 2f 0a 20 20 69  art of PK */.  i
2c738 6e 74 20 2a 70 41 75 74 6f 69 6e 63 20 20 20 20  nt *pAutoinc    
2c739 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
2c73a 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 63 6f  TPUT: True if co
2c73b 6c 75 6d 6e 20 69 73 20 61 75 74 6f 2d 69 6e 63  lumn is auto-inc
2c73c 72 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 69  rement */.){.  i
2c73d 6e 74 20 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a  nt rc;.  char *z
2c73e 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 54 61  ErrMsg = 0;.  Ta
2c73f 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 3b 0a 20  ble *pTab = 0;. 
2c740 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20   Column *pCol = 
2c741 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 0a  0;.  int iCol;..
2c742 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 44    char const *zD
2c743 61 74 61 54 79 70 65 20 3d 20 30 3b 0a 20 20 63  ataType = 0;.  c
2c744 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43 6f 6c 6c  har const *zColl
2c745 53 65 71 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e  Seq = 0;.  int n
2c746 6f 74 6e 75 6c 6c 20 3d 20 30 3b 0a 20 20 69 6e  otnull = 0;.  in
2c747 74 20 70 72 69 6d 61 72 79 6b 65 79 20 3d 20 30  t primarykey = 0
2c748 3b 0a 20 20 69 6e 74 20 61 75 74 6f 69 6e 63 20  ;.  int autoinc 
2c749 3d 20 30 3b 0a 0a 20 20 2f 2a 20 45 6e 73 75 72  = 0;..  /* Ensur
2c74a 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  e the database s
2c74b 63 68 65 6d 61 20 68 61 73 20 62 65 65 6e 20 6c  chema has been l
2c74c 6f 61 64 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74  oaded */.  sqlit
2c74d 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
2c74e 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 28 76 6f  b->mutex);.  (vo
2c74f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
2c750 4f 6e 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65  On(db);.  sqlite
2c751 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64  3BtreeEnterAll(d
2c752 62 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  b);.  rc = sqlit
2c753 65 33 49 6e 69 74 28 64 62 2c 20 26 7a 45 72 72  e3Init(db, &zErr
2c754 4d 73 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  Msg);.  sqlite3B
2c755 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29  treeLeaveAll(db)
2c756 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ;.  if( SQLITE_O
2c757 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20 67 6f 74  K!=rc ){.    got
2c758 6f 20 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d  o error_out;.  }
2c759 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68  ..  /* Locate th
2c75a 65 20 74 61 62 6c 65 20 69 6e 20 71 75 65 73 74  e table in quest
2c75b 69 6f 6e 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20  ion */.  pTab = 
2c75c 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65  sqlite3FindTable
2c75d 28 64 62 2c 20 7a 54 61 62 6c 65 4e 61 6d 65 2c  (db, zTableName,
2c75e 20 7a 44 62 4e 61 6d 65 29 3b 0a 20 20 69 66 28   zDbName);.  if(
2c75f 20 21 70 54 61 62 20 7c 7c 20 70 54 61 62 2d 3e   !pTab || pTab->
2c760 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 70  pSelect ){.    p
2c761 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 67 6f 74  Tab = 0;.    got
2c762 6f 20 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d  o error_out;.  }
2c763 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20  ..  /* Find the 
2c764 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 68 69 63 68  column for which
2c765 20 69 6e 66 6f 20 69 73 20 72 65 71 75 65 73 74   info is request
2c766 65 64 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  ed */.  if( sqli
2c767 74 65 33 49 73 52 6f 77 69 64 28 7a 43 6f 6c 75  te3IsRowid(zColu
2c768 6d 6e 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 69  mnName) ){.    i
2c769 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65  Col = pTab->iPKe
2c76a 79 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3e  y;.    if( iCol>
2c76b 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c  =0 ){.      pCol
2c76c 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69   = &pTab->aCol[i
2c76d 43 6f 6c 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  Col];.    }.  }e
2c76e 6c 73 65 7b 0a 20 20 20 20 66 6f 72 28 69 43 6f  lse{.    for(iCo
2c76f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d 3e  l=0; iCol<pTab->
2c770 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20  nCol; iCol++){. 
2c771 20 20 20 20 20 70 43 6f 6c 20 3d 20 26 70 54 61       pCol = &pTa
2c772 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20  b->aCol[iCol];. 
2c773 20 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69       if( 0==sqli
2c774 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 2d  te3StrICmp(pCol-
2c775 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 75 6d 6e 4e  >zName, zColumnN
2c776 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ame) ){.        
2c777 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
2c778 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 43 6f     }.    if( iCo
2c779 6c 3d 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b  l==pTab->nCol ){
2c77a 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 30 3b  .      pTab = 0;
2c77b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 72 72 6f  .      goto erro
2c77c 72 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  r_out;.    }.  }
2c77d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f  ..  /* The follo
2c77e 77 69 6e 67 20 62 6c 6f 63 6b 20 73 74 6f 72 65  wing block store
2c77f 73 20 74 68 65 20 6d 65 74 61 20 69 6e 66 6f 72  s the meta infor
2c780 6d 61 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c  mation that will
2c781 20 62 65 20 72 65 74 75 72 6e 65 64 0a 20 20 2a   be returned.  *
2c782 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20  * to the caller 
2c783 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  in local variabl
2c784 65 73 20 7a 44 61 74 61 54 79 70 65 2c 20 7a 43  es zDataType, zC
2c785 6f 6c 6c 53 65 71 2c 20 6e 6f 74 6e 75 6c 6c 2c  ollSeq, notnull,
2c786 20 70 72 69 6d 61 72 79 6b 65 79 0a 20 20 2a 2a   primarykey.  **
2c787 20 61 6e 64 20 61 75 74 6f 69 6e 63 2e 20 41 74   and autoinc. At
2c788 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 72   this point ther
2c789 65 20 61 72 65 20 74 77 6f 20 70 6f 73 73 69 62  e are two possib
2c78a 69 6c 69 74 69 65 73 3a 0a 20 20 2a 2a 20 0a 20  ilities:.  ** . 
2c78b 20 2a 2a 20 20 20 20 20 31 2e 20 54 68 65 20 73   **     1. The s
2c78c 70 65 63 69 66 69 65 64 20 63 6f 6c 75 6d 6e 20  pecified column 
2c78d 6e 61 6d 65 20 77 61 73 20 72 6f 77 69 64 22 2c  name was rowid",
2c78e 20 22 6f 69 64 22 20 6f 72 20 22 5f 72 6f 77 69   "oid" or "_rowi
2c78f 64 5f 22 20 0a 20 20 2a 2a 20 20 20 20 20 20 20  d_" .  **       
2c790 20 61 6e 64 20 74 68 65 72 65 20 69 73 20 6e 6f   and there is no
2c791 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c   explicitly decl
2c792 61 72 65 64 20 49 50 4b 20 63 6f 6c 75 6d 6e 2e  ared IPK column.
2c793 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20   .  **.  **     
2c794 32 2e 20 54 68 65 20 74 61 62 6c 65 20 69 73 20  2. The table is 
2c795 6e 6f 74 20 61 20 76 69 65 77 20 61 6e 64 20 74  not a view and t
2c796 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 69  he column name i
2c797 64 65 6e 74 69 66 69 65 64 20 61 6e 20 0a 20 20  dentified an .  
2c798 2a 2a 20 20 20 20 20 20 20 20 65 78 70 6c 69 63  **        explic
2c799 69 74 6c 79 20 64 65 63 6c 61 72 65 64 20 63 6f  itly declared co
2c79a 6c 75 6d 6e 2e 20 43 6f 70 79 20 6d 65 74 61 20  lumn. Copy meta 
2c79b 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d  information from
2c79c 20 2a 70 43 6f 6c 2e 0a 20 20 2a 2f 20 0a 20 20   *pCol..  */ .  
2c79d 69 66 28 20 70 43 6f 6c 20 29 7b 0a 20 20 20 20  if( pCol ){.    
2c79e 7a 44 61 74 61 54 79 70 65 20 3d 20 70 43 6f 6c  zDataType = pCol
2c79f 2d 3e 7a 54 79 70 65 3b 0a 20 20 20 20 7a 43 6f  ->zType;.    zCo
2c7a0 6c 6c 53 65 71 20 3d 20 70 43 6f 6c 2d 3e 7a 43  llSeq = pCol->zC
2c7a1 6f 6c 6c 3b 0a 20 20 20 20 6e 6f 74 6e 75 6c 6c  oll;.    notnull
2c7a2 20 3d 20 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c   = pCol->notNull
2c7a3 21 3d 30 3b 0a 20 20 20 20 70 72 69 6d 61 72 79  !=0;.    primary
2c7a4 6b 65 79 20 20 3d 20 70 43 6f 6c 2d 3e 69 73 50  key  = pCol->isP
2c7a5 72 69 6d 4b 65 79 21 3d 30 3b 0a 20 20 20 20 61  rimKey!=0;.    a
2c7a6 75 74 6f 69 6e 63 20 3d 20 70 54 61 62 2d 3e 69  utoinc = pTab->i
2c7a7 50 4b 65 79 3d 3d 69 43 6f 6c 20 26 26 20 28 70  PKey==iCol && (p
2c7a8 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20  Tab->tabFlags & 
2c7a9 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74  TF_Autoincrement
2c7aa 29 21 3d 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  )!=0;.  }else{. 
2c7ab 20 20 20 7a 44 61 74 61 54 79 70 65 20 3d 20 22     zDataType = "
2c7ac 49 4e 54 45 47 45 52 22 3b 0a 20 20 20 20 70 72  INTEGER";.    pr
2c7ad 69 6d 61 72 79 6b 65 79 20 3d 20 31 3b 0a 20 20  imarykey = 1;.  
2c7ae 7d 0a 20 20 69 66 28 20 21 7a 43 6f 6c 6c 53 65  }.  if( !zCollSe
2c7af 71 20 29 7b 0a 20 20 20 20 7a 43 6f 6c 6c 53 65  q ){.    zCollSe
2c7b0 71 20 3d 20 22 42 49 4e 41 52 59 22 3b 0a 20 20  q = "BINARY";.  
2c7b1 7d 0a 0a 65 72 72 6f 72 5f 6f 75 74 3a 0a 20 20  }..error_out:.  
2c7b2 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
2c7b3 65 74 79 4f 66 66 28 64 62 29 3b 0a 0a 20 20 2f  etyOff(db);..  /
2c7b4 2a 20 57 68 65 74 68 65 72 20 74 68 65 20 66 75  * Whether the fu
2c7b5 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 73 75 63 63  nction call succ
2c7b6 65 65 64 65 64 20 6f 72 20 66 61 69 6c 65 64 2c  eeded or failed,
2c7b7 20 73 65 74 20 74 68 65 20 6f 75 74 70 75 74 20   set the output 
2c7b8 70 61 72 61 6d 65 74 65 72 73 0a 20 20 2a 2a 20  parameters.  ** 
2c7b9 74 6f 20 77 68 61 74 65 76 65 72 20 74 68 65 69  to whatever thei
2c7ba 72 20 6c 6f 63 61 6c 20 63 6f 75 6e 74 65 72 70  r local counterp
2c7bb 61 72 74 73 20 63 6f 6e 74 61 69 6e 2e 20 49 66  arts contain. If
2c7bc 20 61 6e 20 65 72 72 6f 72 20 64 69 64 20 6f 63   an error did oc
2c7bd 63 75 72 2c 0a 20 20 2a 2a 20 74 68 69 73 20 68  cur,.  ** this h
2c7be 61 73 20 74 68 65 20 65 66 66 65 63 74 20 6f 66  as the effect of
2c7bf 20 7a 65 72 6f 69 6e 67 20 61 6c 6c 20 6f 75 74   zeroing all out
2c7c0 70 75 74 20 70 61 72 61 6d 65 74 65 72 73 2e 0a  put parameters..
2c7c1 20 20 2a 2f 0a 20 20 69 66 28 20 70 7a 44 61 74    */.  if( pzDat
2c7c2 61 54 79 70 65 20 29 20 2a 70 7a 44 61 74 61 54  aType ) *pzDataT
2c7c3 79 70 65 20 3d 20 7a 44 61 74 61 54 79 70 65 3b  ype = zDataType;
2c7c4 0a 20 20 69 66 28 20 70 7a 43 6f 6c 6c 53 65 71  .  if( pzCollSeq
2c7c5 20 29 20 2a 70 7a 43 6f 6c 6c 53 65 71 20 3d 20   ) *pzCollSeq = 
2c7c6 7a 43 6f 6c 6c 53 65 71 3b 0a 20 20 69 66 28 20  zCollSeq;.  if( 
2c7c7 70 4e 6f 74 4e 75 6c 6c 20 29 20 2a 70 4e 6f 74  pNotNull ) *pNot
2c7c8 4e 75 6c 6c 20 3d 20 6e 6f 74 6e 75 6c 6c 3b 0a  Null = notnull;.
2c7c9 20 20 69 66 28 20 70 50 72 69 6d 61 72 79 4b 65    if( pPrimaryKe
2c7ca 79 20 29 20 2a 70 50 72 69 6d 61 72 79 4b 65 79  y ) *pPrimaryKey
2c7cb 20 3d 20 70 72 69 6d 61 72 79 6b 65 79 3b 0a 20   = primarykey;. 
2c7cc 20 69 66 28 20 70 41 75 74 6f 69 6e 63 20 29 20   if( pAutoinc ) 
2c7cd 2a 70 41 75 74 6f 69 6e 63 20 3d 20 61 75 74 6f  *pAutoinc = auto
2c7ce 69 6e 63 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49  inc;..  if( SQLI
2c7cf 54 45 5f 4f 4b 3d 3d 72 63 20 26 26 20 21 70 54  TE_OK==rc && !pT
2c7d0 61 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ab ){.    sqlite
2c7d1 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72  3DbFree(db, zErr
2c7d2 4d 73 67 29 3b 0a 20 20 20 20 7a 45 72 72 4d 73  Msg);.    zErrMs
2c7d3 67 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  g = sqlite3MPrin
2c7d4 74 66 28 64 62 2c 20 22 6e 6f 20 73 75 63 68 20  tf(db, "no such 
2c7d5 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 3a 20 25 73  table column: %s
2c7d6 2e 25 73 22 2c 20 7a 54 61 62 6c 65 4e 61 6d 65  .%s", zTableName
2c7d7 2c 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 75 6d  ,.        zColum
2c7d8 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 72 63 20 3d  nName);.    rc =
2c7d9 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
2c7da 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f   }.  sqlite3Erro
2c7db 72 28 64 62 2c 20 72 63 2c 20 28 7a 45 72 72 4d  r(db, rc, (zErrM
2c7dc 73 67 3f 22 25 73 22 3a 30 29 2c 20 7a 45 72 72  sg?"%s":0), zErr
2c7dd 4d 73 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  Msg);.  sqlite3D
2c7de 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 4d 73  bFree(db, zErrMs
2c7df 67 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  g);.  rc = sqlit
2c7e0 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63  e3ApiExit(db, rc
2c7e1 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
2c7e2 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
2c7e3 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
2c7e4 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
2c7e5 2a 20 53 6c 65 65 70 20 66 6f 72 20 61 20 6c 69  * Sleep for a li
2c7e6 74 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 65 74  ttle while.  Ret
2c7e7 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f  urn the amount o
2c7e8 66 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2f  f time slept..*/
2c7e9 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2c7ea 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 28 69 6e  sqlite3_sleep(in
2c7eb 74 20 6d 73 29 7b 0a 20 20 73 71 6c 69 74 65 33  t ms){.  sqlite3
2c7ec 5f 76 66 73 20 2a 70 56 66 73 3b 0a 20 20 69 6e  _vfs *pVfs;.  in
2c7ed 74 20 72 63 3b 0a 20 20 70 56 66 73 20 3d 20 73  t rc;.  pVfs = s
2c7ee 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28  qlite3_vfs_find(
2c7ef 30 29 3b 0a 20 20 69 66 28 20 70 56 66 73 3d 3d  0);.  if( pVfs==
2c7f0 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20  0 ) return 0;.. 
2c7f1 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f   /* This functio
2c7f2 6e 20 77 6f 72 6b 73 20 69 6e 20 6d 69 6c 6c 69  n works in milli
2c7f3 73 65 63 6f 6e 64 73 2c 20 62 75 74 20 74 68 65  seconds, but the
2c7f4 20 75 6e 64 65 72 6c 79 69 6e 67 20 4f 73 53 6c   underlying OsSl
2c7f5 65 65 70 28 29 20 0a 20 20 2a 2a 20 41 50 49 20  eep() .  ** API 
2c7f6 75 73 65 73 20 6d 69 63 72 6f 73 65 63 6f 6e 64  uses microsecond
2c7f7 73 2e 20 48 65 6e 63 65 20 74 68 65 20 31 30 30  s. Hence the 100
2c7f8 30 27 73 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d  0's..  */.  rc =
2c7f9 20 28 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70   (sqlite3OsSleep
2c7fa 28 70 56 66 73 2c 20 31 30 30 30 2a 6d 73 29 2f  (pVfs, 1000*ms)/
2c7fb 31 30 30 30 29 3b 0a 20 20 72 65 74 75 72 6e 20  1000);.  return 
2c7fc 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 61  rc;.}../*.** Ena
2c7fd 62 6c 65 20 6f 72 20 64 69 73 61 62 6c 65 20 74  ble or disable t
2c7fe 68 65 20 65 78 74 65 6e 64 65 64 20 72 65 73 75  he extended resu
2c7ff 6c 74 20 63 6f 64 65 73 2e 0a 2a 2f 0a 53 51 4c  lt codes..*/.SQL
2c800 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2c801 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73  te3_extended_res
2c802 75 6c 74 5f 63 6f 64 65 73 28 73 71 6c 69 74 65  ult_codes(sqlite
2c803 33 20 2a 64 62 2c 20 69 6e 74 20 6f 6e 6f 66 66  3 *db, int onoff
2c804 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ){.  sqlite3_mut
2c805 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
2c806 65 78 29 3b 0a 20 20 64 62 2d 3e 65 72 72 4d 61  ex);.  db->errMa
2c807 73 6b 20 3d 20 6f 6e 6f 66 66 20 3f 20 30 78 66  sk = onoff ? 0xf
2c808 66 66 66 66 66 66 66 20 3a 20 30 78 66 66 3b 0a  fffffff : 0xff;.
2c809 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2c80a 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
2c80b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
2c80c 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  E_OK;.}../*.** I
2c80d 6e 76 6f 6b 65 20 74 68 65 20 78 46 69 6c 65 43  nvoke the xFileC
2c80e 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 6f 6e  ontrol method on
2c80f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61   a particular da
2c810 74 61 62 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  tabase..*/.SQLIT
2c811 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2c812 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 73  3_file_control(s
2c813 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73  qlite3 *db, cons
2c814 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c  t char *zDbName,
2c815 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70   int op, void *p
2c816 41 72 67 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  Arg){.  int rc =
2c817 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
2c818 20 69 6e 74 20 69 44 62 3b 0a 20 20 73 71 6c 69   int iDb;.  sqli
2c819 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
2c81a 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66  db->mutex);.  if
2c81b 28 20 7a 44 62 4e 61 6d 65 3d 3d 30 20 29 7b 0a  ( zDbName==0 ){.
2c81c 20 20 20 20 69 44 62 20 3d 20 30 3b 0a 20 20 7d      iDb = 0;.  }
2c81d 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72 28 69 44  else{.    for(iD
2c81e 62 3d 30 3b 20 69 44 62 3c 64 62 2d 3e 6e 44 62  b=0; iDb<db->nDb
2c81f 3b 20 69 44 62 2b 2b 29 7b 0a 20 20 20 20 20 20  ; iDb++){.      
2c820 69 66 28 20 73 74 72 63 6d 70 28 64 62 2d 3e 61  if( strcmp(db->a
2c821 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a  Db[iDb].zName, z
2c822 44 62 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65  DbName)==0 ) bre
2c823 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ak;.    }.  }.  
2c824 69 66 28 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20  if( iDb<db->nDb 
2c825 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42  ){.    Btree *pB
2c826 74 72 65 65 20 3d 20 64 62 2d 3e 61 44 62 5b 69  tree = db->aDb[i
2c827 44 62 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28  Db].pBt;.    if(
2c828 20 70 42 74 72 65 65 20 29 7b 0a 20 20 20 20 20   pBtree ){.     
2c829 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a   Pager *pPager;.
2c82a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69        sqlite3_fi
2c82b 6c 65 20 2a 66 64 3b 0a 20 20 20 20 20 20 73 71  le *fd;.      sq
2c82c 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
2c82d 70 42 74 72 65 65 29 3b 0a 20 20 20 20 20 20 70  pBtree);.      p
2c82e 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42  Pager = sqlite3B
2c82f 74 72 65 65 50 61 67 65 72 28 70 42 74 72 65 65  treePager(pBtree
2c830 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
2c831 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20   pPager!=0 );.  
2c832 20 20 20 20 66 64 20 3d 20 73 71 6c 69 74 65 33      fd = sqlite3
2c833 50 61 67 65 72 46 69 6c 65 28 70 50 61 67 65 72  PagerFile(pPager
2c834 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
2c835 20 66 64 21 3d 30 20 29 3b 0a 20 20 20 20 20 20   fd!=0 );.      
2c836 69 66 28 20 66 64 2d 3e 70 4d 65 74 68 6f 64 73  if( fd->pMethods
2c837 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
2c838 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f   sqlite3OsFileCo
2c839 6e 74 72 6f 6c 28 66 64 2c 20 6f 70 2c 20 70 41  ntrol(fd, op, pA
2c83a 72 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rg);.      }.   
2c83b 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
2c83c 65 61 76 65 28 70 42 74 72 65 65 29 3b 0a 20 20  eave(pBtree);.  
2c83d 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65    }.  }.  sqlite
2c83e 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
2c83f 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
2c840 72 6e 20 72 63 3b 20 20 20 0a 7d 0a 0a 2f 2a 0a  rn rc;   .}../*.
2c841 2a 2a 20 49 6e 74 65 72 66 61 63 65 20 74 6f 20  ** Interface to 
2c842 74 68 65 20 74 65 73 74 69 6e 67 20 6c 6f 67 69  the testing logi
2c843 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  c..*/.SQLITE_API
2c844 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 65 73   int sqlite3_tes
2c845 74 5f 63 6f 6e 74 72 6f 6c 28 69 6e 74 20 6f 70  t_control(int op
2c846 2c 20 2e 2e 2e 29 7b 0a 20 20 69 6e 74 20 72 63  , ...){.  int rc
2c847 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51   = 0;.#ifndef SQ
2c848 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49  LITE_OMIT_BUILTI
2c849 4e 5f 54 45 53 54 0a 20 20 76 61 5f 6c 69 73 74  N_TEST.  va_list
2c84a 20 61 70 3b 0a 20 20 76 61 5f 73 74 61 72 74 28   ap;.  va_start(
2c84b 61 70 2c 20 6f 70 29 3b 0a 20 20 73 77 69 74 63  ap, op);.  switc
2c84c 68 28 20 6f 70 20 29 7b 0a 0a 20 20 20 20 2f 2a  h( op ){..    /*
2c84d 0a 20 20 20 20 2a 2a 20 53 61 76 65 20 74 68 65  .    ** Save the
2c84e 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f   current state o
2c84f 66 20 74 68 65 20 50 52 4e 47 2e 0a 20 20 20 20  f the PRNG..    
2c850 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  */.    case SQLI
2c851 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47  TE_TESTCTRL_PRNG
2c852 5f 53 41 56 45 3a 20 7b 0a 20 20 20 20 20 20 73  _SAVE: {.      s
2c853 71 6c 69 74 65 33 50 72 6e 67 53 61 76 65 53 74  qlite3PrngSaveSt
2c854 61 74 65 28 29 3b 0a 20 20 20 20 20 20 62 72 65  ate();.      bre
2c855 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  ak;.    }..    /
2c856 2a 0a 20 20 20 20 2a 2a 20 52 65 73 74 6f 72 65  *.    ** Restore
2c857 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68   the state of th
2c858 65 20 50 52 4e 47 20 74 6f 20 74 68 65 20 6c 61  e PRNG to the la
2c859 73 74 20 73 74 61 74 65 20 73 61 76 65 64 20 75  st state saved u
2c85a 73 69 6e 67 0a 20 20 20 20 2a 2a 20 50 52 4e 47  sing.    ** PRNG
2c85b 5f 53 41 56 45 2e 20 20 49 66 20 50 52 4e 47 5f  _SAVE.  If PRNG_
2c85c 53 41 56 45 20 68 61 73 20 6e 65 76 65 72 20 62  SAVE has never b
2c85d 65 66 6f 72 65 20 62 65 65 6e 20 63 61 6c 6c 65  efore been calle
2c85e 64 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 74  d, then.    ** t
2c85f 68 69 73 20 76 65 72 62 20 61 63 74 73 20 6c 69  his verb acts li
2c860 6b 65 20 50 52 4e 47 5f 52 45 53 45 54 2e 0a 20  ke PRNG_RESET.. 
2c861 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53     */.    case S
2c862 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50  QLITE_TESTCTRL_P
2c863 52 4e 47 5f 52 45 53 54 4f 52 45 3a 20 7b 0a 20  RNG_RESTORE: {. 
2c864 20 20 20 20 20 73 71 6c 69 74 65 33 50 72 6e 67       sqlite3Prng
2c865 52 65 73 74 6f 72 65 53 74 61 74 65 28 29 3b 0a  RestoreState();.
2c866 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2c867 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a   }..    /*.    *
2c868 2a 20 52 65 73 65 74 20 74 68 65 20 50 52 4e 47  * Reset the PRNG
2c869 20 62 61 63 6b 20 74 6f 20 69 74 73 20 75 6e 69   back to its uni
2c86a 6e 69 74 69 61 6c 69 7a 65 64 20 73 74 61 74 65  nitialized state
2c86b 2e 20 20 54 68 65 20 6e 65 78 74 20 63 61 6c 6c  .  The next call
2c86c 0a 20 20 20 20 2a 2a 20 74 6f 20 73 71 6c 69 74  .    ** to sqlit
2c86d 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 29 20  e3_randomness() 
2c86e 77 69 6c 6c 20 72 65 73 65 65 64 20 74 68 65 20  will reseed the 
2c86f 50 52 4e 47 20 75 73 69 6e 67 20 61 20 73 69 6e  PRNG using a sin
2c870 67 6c 65 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20  gle call.    ** 
2c871 74 6f 20 74 68 65 20 78 52 61 6e 64 6f 6d 6e 65  to the xRandomne
2c872 73 73 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65  ss method of the
2c873 20 64 65 66 61 75 6c 74 20 56 46 53 2e 0a 20 20   default VFS..  
2c874 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51    */.    case SQ
2c875 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52  LITE_TESTCTRL_PR
2c876 4e 47 5f 52 45 53 45 54 3a 20 7b 0a 20 20 20 20  NG_RESET: {.    
2c877 20 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73    sqlite3PrngRes
2c878 65 74 53 74 61 74 65 28 29 3b 0a 20 20 20 20 20  etState();.     
2c879 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
2c87a 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 20 73 71     /*.    **  sq
2c87b 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72  lite3_test_contr
2c87c 6f 6c 28 42 49 54 56 45 43 5f 54 45 53 54 2c 20  ol(BITVEC_TEST, 
2c87d 73 69 7a 65 2c 20 70 72 6f 67 72 61 6d 29 0a 20  size, program). 
2c87e 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 52 75 6e     **.    ** Run
2c87f 20 61 20 74 65 73 74 20 61 67 61 69 6e 73 74 20   a test against 
2c880 61 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 20  a Bitvec object 
2c881 6f 66 20 73 69 7a 65 2e 20 20 54 68 65 20 70 72  of size.  The pr
2c882 6f 67 72 61 6d 20 61 72 67 75 6d 65 6e 74 0a 20  ogram argument. 
2c883 20 20 20 2a 2a 20 69 73 20 61 6e 20 61 72 72 61     ** is an arra
2c884 79 20 6f 66 20 69 6e 74 65 67 65 72 73 20 74 68  y of integers th
2c885 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 74  at defines the t
2c886 65 73 74 2e 20 20 52 65 74 75 72 6e 20 2d 31 20  est.  Return -1 
2c887 6f 6e 20 61 0a 20 20 20 20 2a 2a 20 6d 65 6d 6f  on a.    ** memo
2c888 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72  ry allocation er
2c889 72 6f 72 2c 20 30 20 6f 6e 20 73 75 63 63 65 73  ror, 0 on succes
2c88a 73 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 20 66  s, or non-zero f
2c88b 6f 72 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20  or an error..   
2c88c 20 2a 2a 20 53 65 65 20 74 68 65 20 73 71 6c 69   ** See the sqli
2c88d 74 65 33 42 69 74 76 65 63 42 75 69 6c 74 69 6e  te3BitvecBuiltin
2c88e 54 65 73 74 28 29 20 66 6f 72 20 61 64 64 69 74  Test() for addit
2c88f 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
2c890 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  n..    */.    ca
2c891 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54  se SQLITE_TESTCT
2c892 52 4c 5f 42 49 54 56 45 43 5f 54 45 53 54 3a 20  RL_BITVEC_TEST: 
2c893 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 7a 20 3d  {.      int sz =
2c894 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29   va_arg(ap, int)
2c895 3b 0a 20 20 20 20 20 20 69 6e 74 20 2a 61 50 72  ;.      int *aPr
2c896 6f 67 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  og = va_arg(ap, 
2c897 69 6e 74 2a 29 3b 0a 20 20 20 20 20 20 72 63 20  int*);.      rc 
2c898 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 42  = sqlite3BitvecB
2c899 75 69 6c 74 69 6e 54 65 73 74 28 73 7a 2c 20 61  uiltinTest(sz, a
2c89a 50 72 6f 67 29 3b 0a 20 20 20 20 20 20 62 72 65  Prog);.      bre
2c89b 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  ak;.    }..    /
2c89c 2a 0a 20 20 20 20 2a 2a 20 20 73 71 6c 69 74 65  *.    **  sqlite
2c89d 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 42  3_test_control(B
2c89e 45 4e 49 47 4e 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f  ENIGN_MALLOC_HOO
2c89f 4b 53 2c 20 78 42 65 67 69 6e 2c 20 78 45 6e 64  KS, xBegin, xEnd
2c8a0 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ).    **.    ** 
2c8a1 52 65 67 69 73 74 65 72 20 68 6f 6f 6b 73 20 74  Register hooks t
2c8a2 6f 20 63 61 6c 6c 20 74 6f 20 69 6e 64 69 63 61  o call to indica
2c8a3 74 65 20 77 68 69 63 68 20 6d 61 6c 6c 6f 63 28  te which malloc(
2c8a4 29 20 66 61 69 6c 75 72 65 73 20 0a 20 20 20 20  ) failures .    
2c8a5 2a 2a 20 61 72 65 20 62 65 6e 69 67 6e 2e 0a 20  ** are benign.. 
2c8a6 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53     */.    case S
2c8a7 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 42  QLITE_TESTCTRL_B
2c8a8 45 4e 49 47 4e 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f  ENIGN_MALLOC_HOO
2c8a9 4b 53 3a 20 7b 0a 20 20 20 20 20 20 74 79 70 65  KS: {.      type
2c8aa 64 65 66 20 76 6f 69 64 20 28 2a 76 6f 69 64 5f  def void (*void_
2c8ab 66 75 6e 63 74 69 6f 6e 29 28 76 6f 69 64 29 3b  function)(void);
2c8ac 0a 20 20 20 20 20 20 76 6f 69 64 5f 66 75 6e 63  .      void_func
2c8ad 74 69 6f 6e 20 78 42 65 6e 69 67 6e 42 65 67 69  tion xBenignBegi
2c8ae 6e 3b 0a 20 20 20 20 20 20 76 6f 69 64 5f 66 75  n;.      void_fu
2c8af 6e 63 74 69 6f 6e 20 78 42 65 6e 69 67 6e 45 6e  nction xBenignEn
2c8b0 64 3b 0a 20 20 20 20 20 20 78 42 65 6e 69 67 6e  d;.      xBenign
2c8b1 42 65 67 69 6e 20 3d 20 76 61 5f 61 72 67 28 61  Begin = va_arg(a
2c8b2 70 2c 20 76 6f 69 64 5f 66 75 6e 63 74 69 6f 6e  p, void_function
2c8b3 29 3b 0a 20 20 20 20 20 20 78 42 65 6e 69 67 6e  );.      xBenign
2c8b4 45 6e 64 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  End = va_arg(ap,
2c8b5 20 76 6f 69 64 5f 66 75 6e 63 74 69 6f 6e 29 3b   void_function);
2c8b6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65  .      sqlite3Be
2c8b7 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 28  nignMallocHooks(
2c8b8 78 42 65 6e 69 67 6e 42 65 67 69 6e 2c 20 78 42  xBenignBegin, xB
2c8b9 65 6e 69 67 6e 45 6e 64 29 3b 0a 20 20 20 20 20  enignEnd);.     
2c8ba 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
2c8bb 7d 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a  }.  va_end(ap);.
2c8bc 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2c8bd 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45  _OMIT_BUILTIN_TE
2c8be 53 54 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72  ST */.  return r
2c8bf 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  c;.}../*********
2c8c0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 61 69  ***** End of mai
2c8c1 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
2c8c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c8c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c8c4 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
2c8c5 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
2c8c6 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   fts3.c ********
2c8c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c8c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c8c9 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36  ****/./*.** 2006
2c8ca 20 4f 63 74 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68   Oct 10.**.** Th
2c8cb 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
2c8cc 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
2c8cd 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
2c8ce 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
2c8cf 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
2c8d0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
2c8d1 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
2c8d2 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
2c8d3 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
2c8d4 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
2c8d5 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
2c8d6 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
2c8d7 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
2c8d8 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
2c8d9 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
2c8da 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
2c8db 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
2c8dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c8dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c8de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c8df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c8e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
2c8e1 2a 20 54 68 69 73 20 69 73 20 61 6e 20 53 51 4c  * This is an SQL
2c8e2 69 74 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65  ite module imple
2c8e3 6d 65 6e 74 69 6e 67 20 66 75 6c 6c 2d 74 65 78  menting full-tex
2c8e4 74 20 73 65 61 72 63 68 2e 0a 2a 2f 0a 0a 2f 2a  t search..*/../*
2c8e5 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20  .** The code in 
2c8e6 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c  this file is onl
2c8e7 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a  y compiled if:.*
2c8e8 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46  *.**     * The F
2c8e9 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65  TS3 module is be
2c8ea 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61 6e 20  ing built as an 
2c8eb 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20  extension.**    
2c8ec 20 20 20 28 69 6e 20 77 68 69 63 68 20 63 61 73     (in which cas
2c8ed 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 69 73  e SQLITE_CORE is
2c8ee 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 6f   not defined), o
2c8ef 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68  r.**.**     * Th
2c8f0 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73  e FTS3 module is
2c8f1 20 62 65 69 6e 67 20 62 75 69 6c 74 20 69 6e 74   being built int
2c8f2 6f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a  o the core of.**
2c8f3 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 28 69         SQLite (i
2c8f4 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 51 4c  n which case SQL
2c8f5 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20  ITE_ENABLE_FTS3 
2c8f6 69 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a  is defined)..*/.
2c8f7 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
2c8f8 43 6f 6e 73 69 64 65 72 20 65 78 70 6f 72 74 69  Consider exporti
2c8f9 6e 67 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 20  ng this comment 
2c8fa 74 6f 20 61 6e 20 48 54 4d 4c 20 66 69 6c 65 20  to an HTML file 
2c8fb 6f 72 20 74 68 65 0a 2a 2a 20 77 69 6b 69 2e 0a  or the.** wiki..
2c8fc 2a 2f 0a 2f 2a 20 54 68 65 20 66 75 6c 6c 2d 74  */./* The full-t
2c8fd 65 78 74 20 69 6e 64 65 78 20 69 73 20 73 74 6f  ext index is sto
2c8fe 72 65 64 20 69 6e 20 61 20 73 65 72 69 65 73 20  red in a series 
2c8ff 6f 66 20 62 2b 74 72 65 65 20 28 2d 6c 69 6b 65  of b+tree (-like
2c900 29 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20  ).** structures 
2c901 63 61 6c 6c 65 64 20 73 65 67 6d 65 6e 74 73 20  called segments 
2c902 77 68 69 63 68 20 6d 61 70 20 74 65 72 6d 73 20  which map terms 
2c903 74 6f 20 64 6f 63 6c 69 73 74 73 2e 20 20 54 68  to doclists.  Th
2c904 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20  e.** structures 
2c905 61 72 65 20 6c 69 6b 65 20 62 2b 74 72 65 65 73  are like b+trees
2c906 20 69 6e 20 6c 61 79 6f 75 74 2c 20 62 75 74 20   in layout, but 
2c907 61 72 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20  are constructed 
2c908 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 62 6f 74 74  from the.** bott
2c909 6f 6d 20 75 70 20 69 6e 20 6f 70 74 69 6d 61 6c  om up in optimal
2c90a 20 66 61 73 68 69 6f 6e 20 61 6e 64 20 61 72 65   fashion and are
2c90b 20 6e 6f 74 20 75 70 64 61 74 61 62 6c 65 2e 20   not updatable. 
2c90c 20 53 69 6e 63 65 20 74 72 65 65 73 0a 2a 2a 20   Since trees.** 
2c90d 61 72 65 20 62 75 69 6c 74 20 66 72 6f 6d 20 74  are built from t
2c90e 68 65 20 62 6f 74 74 6f 6d 20 75 70 2c 20 74 68  he bottom up, th
2c90f 69 6e 67 73 20 77 69 6c 6c 20 62 65 20 64 65 73  ings will be des
2c910 63 72 69 62 65 64 20 66 72 6f 6d 20 74 68 65 0a  cribed from the.
2c911 2a 2a 20 62 6f 74 74 6f 6d 20 75 70 2e 0a 2a 2a  ** bottom up..**
2c912 0a 2a 2a 0a 2a 2a 2a 2a 20 56 61 72 69 6e 74 73  .**.**** Varints
2c913 20 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 73   ****.** The bas
2c914 69 63 20 75 6e 69 74 20 6f 66 20 65 6e 63 6f 64  ic unit of encod
2c915 69 6e 67 20 69 73 20 61 20 76 61 72 69 61 62 6c  ing is a variabl
2c916 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e-length integer
2c917 20 63 61 6c 6c 65 64 20 61 0a 2a 2a 20 76 61 72   called a.** var
2c918 69 6e 74 2e 20 20 57 65 20 65 6e 63 6f 64 65 20  int.  We encode 
2c919 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20  variable-length 
2c91a 69 6e 74 65 67 65 72 73 20 69 6e 20 6c 69 74 74  integers in litt
2c91b 6c 65 2d 65 6e 64 69 61 6e 20 6f 72 64 65 72 0a  le-endian order.
2c91c 2a 2a 20 75 73 69 6e 67 20 73 65 76 65 6e 20 62  ** using seven b
2c91d 69 74 73 20 2a 20 70 65 72 20 62 79 74 65 20 61  its * per byte a
2c91e 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
2c91f 20 4b 45 59 3a 0a 2a 2a 20 20 20 20 20 20 20 20   KEY:.**        
2c920 20 41 20 3d 20 30 78 78 78 78 78 78 78 20 20 20   A = 0xxxxxxx   
2c921 20 37 20 62 69 74 73 20 6f 66 20 64 61 74 61 20   7 bits of data 
2c922 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74  and one flag bit
2c923 0a 2a 2a 20 20 20 20 20 20 20 20 20 42 20 3d 20  .**         B = 
2c924 31 78 78 78 78 78 78 78 20 20 20 20 37 20 62 69  1xxxxxxx    7 bi
2c925 74 73 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f  ts of data and o
2c926 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a 0a 2a  ne flag bit.**.*
2c927 2a 20 20 37 20 62 69 74 73 20 2d 20 41 0a 2a 2a  *  7 bits - A.**
2c928 20 31 34 20 62 69 74 73 20 2d 20 42 41 0a 2a 2a   14 bits - BA.**
2c929 20 32 31 20 62 69 74 73 20 2d 20 42 42 41 0a 2a   21 bits - BBA.*
2c92a 2a 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a  * and so on..**.
2c92b 2a 2a 20 54 68 69 73 20 69 73 20 69 64 65 6e 74  ** This is ident
2c92c 69 63 61 6c 20 74 6f 20 68 6f 77 20 73 71 6c 69  ical to how sqli
2c92d 74 65 20 65 6e 63 6f 64 65 73 20 76 61 72 69 6e  te encodes varin
2c92e 74 73 20 28 73 65 65 20 75 74 69 6c 2e 63 29 2e  ts (see util.c).
2c92f 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 44 6f 63 75  .**.**.**** Docu
2c930 6d 65 6e 74 20 6c 69 73 74 73 20 2a 2a 2a 2a 0a  ment lists ****.
2c931 2a 2a 20 41 20 64 6f 63 6c 69 73 74 20 28 64 6f  ** A doclist (do
2c932 63 75 6d 65 6e 74 20 6c 69 73 74 29 20 68 6f 6c  cument list) hol
2c933 64 73 20 61 20 64 6f 63 69 64 2d 73 6f 72 74 65  ds a docid-sorte
2c934 64 20 6c 69 73 74 20 6f 66 20 68 69 74 73 20 66  d list of hits f
2c935 6f 72 20 61 0a 2a 2a 20 67 69 76 65 6e 20 74 65  or a.** given te
2c936 72 6d 2e 20 20 44 6f 63 6c 69 73 74 73 20 68 6f  rm.  Doclists ho
2c937 6c 64 20 64 6f 63 69 64 73 2c 20 61 6e 64 20 63  ld docids, and c
2c938 61 6e 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 61 73  an optionally as
2c939 73 6f 63 69 61 74 65 0a 2a 2a 20 74 6f 6b 65 6e  sociate.** token
2c93a 20 70 6f 73 69 74 69 6f 6e 73 20 61 6e 64 20 6f   positions and o
2c93b 66 66 73 65 74 73 20 77 69 74 68 20 64 6f 63 69  ffsets with doci
2c93c 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 44 4c 5f 50  ds..**.** A DL_P
2c93d 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53  OSITIONS_OFFSETS
2c93e 20 64 6f 63 6c 69 73 74 20 69 73 20 73 74 6f 72   doclist is stor
2c93f 65 64 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  ed like this:.**
2c940 0a 2a 2a 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20  .** array {.**  
2c941 20 76 61 72 69 6e 74 20 64 6f 63 69 64 3b 0a 2a   varint docid;.*
2c942 2a 20 20 20 61 72 72 61 79 20 7b 20 20 20 20 20  *   array {     
2c943 20 20 20 20 20 20 20 20 20 20 20 28 70 6f 73 69             (posi
2c944 74 69 6f 6e 20 6c 69 73 74 20 66 6f 72 20 63 6f  tion list for co
2c945 6c 75 6d 6e 20 30 29 0a 2a 2a 20 20 20 20 20 76  lumn 0).**     v
2c946 61 72 69 6e 74 20 70 6f 73 69 74 69 6f 6e 3b 20  arint position; 
2c947 20 20 20 20 28 64 65 6c 74 61 20 66 72 6f 6d 20      (delta from 
2c948 70 72 65 76 69 6f 75 73 20 70 6f 73 69 74 69 6f  previous positio
2c949 6e 20 70 6c 75 73 20 50 4f 53 5f 42 41 53 45 29  n plus POS_BASE)
2c94a 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20 73  .**     varint s
2c94b 74 61 72 74 4f 66 66 73 65 74 3b 20 20 28 64 65  tartOffset;  (de
2c94c 6c 74 61 20 66 72 6f 6d 20 70 72 65 76 69 6f 75  lta from previou
2c94d 73 20 73 74 61 72 74 4f 66 66 73 65 74 29 0a 2a  s startOffset).*
2c94e 2a 20 20 20 20 20 76 61 72 69 6e 74 20 65 6e 64  *     varint end
2c94f 4f 66 66 73 65 74 3b 20 20 20 20 28 64 65 6c 74  Offset;    (delt
2c950 61 20 66 72 6f 6d 20 73 74 61 72 74 4f 66 66 73  a from startOffs
2c951 65 74 29 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 20 20  et).**   }.**   
2c952 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20 20 20 76  array {.**     v
2c953 61 72 69 6e 74 20 50 4f 53 5f 43 4f 4c 55 4d 4e  arint POS_COLUMN
2c954 3b 20 20 20 28 6d 61 72 6b 73 20 73 74 61 72 74  ;   (marks start
2c955 20 6f 66 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73   of position lis
2c956 74 20 66 6f 72 20 6e 65 77 20 63 6f 6c 75 6d 6e  t for new column
2c957 29 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20  ).**     varint 
2c958 63 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 28 69  column;       (i
2c959 6e 64 65 78 20 6f 66 20 6e 65 77 20 63 6f 6c 75  ndex of new colu
2c95a 6d 6e 29 0a 2a 2a 20 20 20 20 20 61 72 72 61 79  mn).**     array
2c95b 20 7b 0a 2a 2a 20 20 20 20 20 20 20 76 61 72 69   {.**       vari
2c95c 6e 74 20 70 6f 73 69 74 69 6f 6e 3b 20 20 20 28  nt position;   (
2c95d 64 65 6c 74 61 20 66 72 6f 6d 20 70 72 65 76 69  delta from previ
2c95e 6f 75 73 20 70 6f 73 69 74 69 6f 6e 20 70 6c 75  ous position plu
2c95f 73 20 50 4f 53 5f 42 41 53 45 29 0a 2a 2a 20 20  s POS_BASE).**  
2c960 20 20 20 20 20 76 61 72 69 6e 74 20 73 74 61 72       varint star
2c961 74 4f 66 66 73 65 74 3b 28 64 65 6c 74 61 20 66  tOffset;(delta f
2c962 72 6f 6d 20 70 72 65 76 69 6f 75 73 20 73 74 61  rom previous sta
2c963 72 74 4f 66 66 73 65 74 29 0a 2a 2a 20 20 20 20  rtOffset).**    
2c964 20 20 20 76 61 72 69 6e 74 20 65 6e 64 4f 66 66     varint endOff
2c965 73 65 74 3b 20 20 28 64 65 6c 74 61 20 66 72 6f  set;  (delta fro
2c966 6d 20 73 74 61 72 74 4f 66 66 73 65 74 29 0a 2a  m startOffset).*
2c967 2a 20 20 20 20 20 7d 0a 2a 2a 20 20 20 7d 0a 2a  *     }.**   }.*
2c968 2a 20 20 20 76 61 72 69 6e 74 20 50 4f 53 5f 45  *   varint POS_E
2c969 4e 44 3b 20 20 20 20 20 20 20 20 28 6d 61 72 6b  ND;        (mark
2c96a 73 20 65 6e 64 20 6f 66 20 70 6f 73 69 74 69 6f  s end of positio
2c96b 6e 73 20 66 6f 72 20 74 68 69 73 20 64 6f 63 75  ns for this docu
2c96c 6d 65 6e 74 2e 0a 2a 2a 20 7d 0a 2a 2a 0a 2a 2a  ment..** }.**.**
2c96d 20 48 65 72 65 2c 20 61 72 72 61 79 20 7b 20 58   Here, array { X
2c96e 20 7d 20 6d 65 61 6e 73 20 7a 65 72 6f 20 6f 72   } means zero or
2c96f 20 6d 6f 72 65 20 6f 63 63 75 72 72 65 6e 63 65   more occurrence
2c970 73 20 6f 66 20 58 2c 20 61 64 6a 61 63 65 6e 74  s of X, adjacent
2c971 20 69 6e 0a 2a 2a 20 6d 65 6d 6f 72 79 2e 20 20   in.** memory.  
2c972 41 20 22 70 6f 73 69 74 69 6f 6e 22 20 69 73 20  A "position" is 
2c973 61 6e 20 69 6e 64 65 78 20 6f 66 20 61 20 74 6f  an index of a to
2c974 6b 65 6e 20 69 6e 20 74 68 65 20 74 6f 6b 65 6e  ken in the token
2c975 20 73 74 72 65 61 6d 0a 2a 2a 20 67 65 6e 65 72   stream.** gener
2c976 61 74 65 64 20 62 79 20 74 68 65 20 74 6f 6b 65  ated by the toke
2c977 6e 69 7a 65 72 2c 20 77 68 69 6c 65 20 61 6e 20  nizer, while an 
2c978 22 6f 66 66 73 65 74 22 20 69 73 20 61 20 62 79  "offset" is a by
2c979 74 65 20 6f 66 66 73 65 74 2c 0a 2a 2a 20 62 6f  te offset,.** bo
2c97a 74 68 20 62 61 73 65 64 20 61 74 20 30 2e 20 20  th based at 0.  
2c97b 4e 6f 74 65 20 74 68 61 74 20 50 4f 53 5f 45 4e  Note that POS_EN
2c97c 44 20 61 6e 64 20 50 4f 53 5f 43 4f 4c 55 4d 4e  D and POS_COLUMN
2c97d 20 6f 63 63 75 72 20 69 6e 20 74 68 65 0a 2a 2a   occur in the.**
2c97e 20 73 61 6d 65 20 6c 6f 67 69 63 61 6c 20 70 6c   same logical pl
2c97f 61 63 65 20 61 73 20 74 68 65 20 70 6f 73 69 74  ace as the posit
2c980 69 6f 6e 20 65 6c 65 6d 65 6e 74 2c 20 61 6e 64  ion element, and
2c981 20 61 63 74 20 61 73 20 73 65 6e 74 69 6e 61 6c   act as sentinal
2c982 73 0a 2a 2a 20 65 6e 64 69 6e 67 20 61 20 70 6f  s.** ending a po
2c983 73 69 74 69 6f 6e 20 6c 69 73 74 20 61 72 72 61  sition list arra
2c984 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 44 4c 5f 50 4f  y..**.** A DL_PO
2c985 53 49 54 49 4f 4e 53 20 64 6f 63 6c 69 73 74 20  SITIONS doclist 
2c986 6f 6d 69 74 73 20 74 68 65 20 73 74 61 72 74 4f  omits the startO
2c987 66 66 73 65 74 20 61 6e 64 20 65 6e 64 4f 66 66  ffset and endOff
2c988 73 65 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  set.** informati
2c989 6f 6e 2e 20 20 41 20 44 4c 5f 44 4f 43 49 44 53  on.  A DL_DOCIDS
2c98a 20 64 6f 63 6c 69 73 74 20 6f 6d 69 74 73 20 62   doclist omits b
2c98b 6f 74 68 20 74 68 65 20 70 6f 73 69 74 69 6f 6e  oth the position
2c98c 20 61 6e 64 0a 2a 2a 20 6f 66 66 73 65 74 20 69   and.** offset i
2c98d 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 62 65 63 6f  nformation, beco
2c98e 6d 69 6e 67 20 61 6e 20 61 72 72 61 79 20 6f 66  ming an array of
2c98f 20 76 61 72 69 6e 74 2d 65 6e 63 6f 64 65 64 20   varint-encoded 
2c990 64 6f 63 69 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  docids..**.** On
2c991 2d 64 69 73 6b 20 64 61 74 61 20 69 73 20 73 74  -disk data is st
2c992 6f 72 65 64 20 61 73 20 74 79 70 65 20 44 4c 5f  ored as type DL_
2c993 44 45 46 41 55 4c 54 2c 20 73 6f 20 77 65 20 64  DEFAULT, so we d
2c994 6f 6e 27 74 20 73 65 72 69 61 6c 69 7a 65 0a 2a  on't serialize.*
2c995 2a 20 74 68 65 20 74 79 70 65 2e 20 20 44 75 65  * the type.  Due
2c996 20 74 6f 20 68 6f 77 20 64 65 6c 65 74 69 6f 6e   to how deletion
2c997 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20   is implemented 
2c998 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74 61 74  in the segmentat
2c999 69 6f 6e 0a 2a 2a 20 73 79 73 74 65 6d 2c 20 6f  ion.** system, o
2c99a 6e 2d 64 69 73 6b 20 64 6f 63 6c 69 73 74 73 20  n-disk doclists 
2c99b 4d 55 53 54 20 73 74 6f 72 65 20 61 74 20 6c 65  MUST store at le
2c99c 61 73 74 20 70 6f 73 69 74 69 6f 6e 73 2e 0a 2a  ast positions..*
2c99d 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e  *.**.**** Segmen
2c99e 74 20 6c 65 61 66 20 6e 6f 64 65 73 20 2a 2a 2a  t leaf nodes ***
2c99f 2a 0a 2a 2a 20 53 65 67 6d 65 6e 74 20 6c 65 61  *.** Segment lea
2c9a0 66 20 6e 6f 64 65 73 20 73 74 6f 72 65 20 74 65  f nodes store te
2c9a1 72 6d 73 20 61 6e 64 20 64 6f 63 6c 69 73 74 73  rms and doclists
2c9a2 2c 20 6f 72 64 65 72 65 64 20 62 79 20 74 65 72  , ordered by ter
2c9a3 6d 2e 20 20 4c 65 61 66 0a 2a 2a 20 6e 6f 64 65  m.  Leaf.** node
2c9a4 73 20 61 72 65 20 77 72 69 74 74 65 6e 20 75 73  s are written us
2c9a5 69 6e 67 20 4c 65 61 66 57 72 69 74 65 72 2c 20  ing LeafWriter, 
2c9a6 61 6e 64 20 72 65 61 64 20 75 73 69 6e 67 20 4c  and read using L
2c9a7 65 61 66 52 65 61 64 65 72 20 28 74 6f 0a 2a 2a  eafReader (to.**
2c9a8 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   iterate through
2c9a9 20 61 20 73 69 6e 67 6c 65 20 6c 65 61 66 20 6e   a single leaf n
2c9aa 6f 64 65 27 73 20 64 61 74 61 29 20 61 6e 64 20  ode's data) and 
2c9ab 4c 65 61 76 65 73 52 65 61 64 65 72 20 28 74 6f  LeavesReader (to
2c9ac 0a 2a 2a 20 69 74 65 72 61 74 65 20 74 68 72 6f  .** iterate thro
2c9ad 75 67 68 20 61 20 73 65 67 6d 65 6e 74 27 73 20  ugh a segment's 
2c9ae 65 6e 74 69 72 65 20 6c 65 61 66 20 6c 61 79 65  entire leaf laye
2c9af 72 29 2e 20 20 4c 65 61 66 20 6e 6f 64 65 73 20  r).  Leaf nodes 
2c9b0 68 61 76 65 0a 2a 2a 20 74 68 65 20 66 6f 72 6d  have.** the form
2c9b1 61 74 3a 0a 2a 2a 0a 2a 2a 20 76 61 72 69 6e 74  at:.**.** varint
2c9b2 20 69 48 65 69 67 68 74 3b 20 20 20 20 20 20 20   iHeight;       
2c9b3 20 20 20 20 20 20 28 68 65 69 67 68 74 20 66 72        (height fr
2c9b4 6f 6d 20 6c 65 61 66 20 6c 65 76 65 6c 2c 20 61  om leaf level, a
2c9b5 6c 77 61 79 73 20 30 29 0a 2a 2a 20 76 61 72 69  lways 0).** vari
2c9b6 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20 20 20 20  nt nTerm;       
2c9b7 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20          (length 
2c9b8 6f 66 20 66 69 72 73 74 20 74 65 72 6d 29 0a 2a  of first term).*
2c9b9 2a 20 63 68 61 72 20 70 54 65 72 6d 5b 6e 54 65  * char pTerm[nTe
2c9ba 72 6d 5d 3b 20 20 20 20 20 20 20 20 20 20 28 63  rm];          (c
2c9bb 6f 6e 74 65 6e 74 20 6f 66 20 66 69 72 73 74 20  ontent of first 
2c9bc 74 65 72 6d 29 0a 2a 2a 20 76 61 72 69 6e 74 20  term).** varint 
2c9bd 6e 44 6f 63 6c 69 73 74 3b 20 20 20 20 20 20 20  nDoclist;       
2c9be 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20       (length of 
2c9bf 74 65 72 6d 27 73 20 61 73 73 6f 63 69 61 74 65  term's associate
2c9c0 64 20 64 6f 63 6c 69 73 74 29 0a 2a 2a 20 63 68  d doclist).** ch
2c9c1 61 72 20 70 44 6f 63 6c 69 73 74 5b 6e 44 6f 63  ar pDoclist[nDoc
2c9c2 6c 69 73 74 5d 3b 20 20 20 20 28 63 6f 6e 74 65  list];    (conte
2c9c3 6e 74 20 6f 66 20 64 6f 63 6c 69 73 74 29 0a 2a  nt of doclist).*
2c9c4 2a 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20 20  * array {.**    
2c9c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c9c6 20 20 20 20 20 20 20 20 20 28 66 75 72 74 68 65           (furthe
2c9c7 72 20 74 65 72 6d 73 20 61 72 65 20 64 65 6c 74  r terms are delt
2c9c8 61 2d 65 6e 63 6f 64 65 64 29 0a 2a 2a 20 20 20  a-encoded).**   
2c9c9 76 61 72 69 6e 74 20 6e 50 72 65 66 69 78 3b 20  varint nPrefix; 
2c9ca 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74            (lengt
2c9cb 68 20 6f 66 20 70 72 65 66 69 78 20 73 68 61 72  h of prefix shar
2c9cc 65 64 20 77 69 74 68 20 70 72 65 76 69 6f 75 73  ed with previous
2c9cd 20 74 65 72 6d 29 0a 2a 2a 20 20 20 76 61 72 69   term).**   vari
2c9ce 6e 74 20 6e 53 75 66 66 69 78 3b 20 20 20 20 20  nt nSuffix;     
2c9cf 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66        (length of
2c9d0 20 75 6e 73 68 61 72 65 64 20 73 75 66 66 69 78   unshared suffix
2c9d1 29 0a 2a 2a 20 20 20 63 68 61 72 20 70 54 65 72  ).**   char pTer
2c9d2 6d 53 75 66 66 69 78 5b 6e 53 75 66 66 69 78 5d  mSuffix[nSuffix]
2c9d3 3b 28 75 6e 73 68 61 72 65 64 20 73 75 66 66 69  ;(unshared suffi
2c9d4 78 20 6f 66 20 6e 65 78 74 20 74 65 72 6d 29 0a  x of next term).
2c9d5 2a 2a 20 20 20 76 61 72 69 6e 74 20 6e 44 6f 63  **   varint nDoc
2c9d6 6c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 28  list;          (
2c9d7 6c 65 6e 67 74 68 20 6f 66 20 74 65 72 6d 27 73  length of term's
2c9d8 20 61 73 73 6f 63 69 61 74 65 64 20 64 6f 63 6c   associated docl
2c9d9 69 73 74 29 0a 2a 2a 20 20 20 63 68 61 72 20 70  ist).**   char p
2c9da 44 6f 63 6c 69 73 74 5b 6e 44 6f 63 6c 69 73 74  Doclist[nDoclist
2c9db 5d 3b 20 20 28 63 6f 6e 74 65 6e 74 20 6f 66 20  ];  (content of 
2c9dc 64 6f 63 6c 69 73 74 29 0a 2a 2a 20 7d 0a 2a 2a  doclist).** }.**
2c9dd 0a 2a 2a 20 48 65 72 65 2c 20 61 72 72 61 79 20  .** Here, array 
2c9de 7b 20 58 20 7d 20 6d 65 61 6e 73 20 7a 65 72 6f  { X } means zero
2c9df 20 6f 72 20 6d 6f 72 65 20 6f 63 63 75 72 72 65   or more occurre
2c9e0 6e 63 65 73 20 6f 66 20 58 2c 20 61 64 6a 61 63  nces of X, adjac
2c9e1 65 6e 74 20 69 6e 0a 2a 2a 20 6d 65 6d 6f 72 79  ent in.** memory
2c9e2 2e 0a 2a 2a 0a 2a 2a 20 4c 65 61 66 20 6e 6f 64  ..**.** Leaf nod
2c9e3 65 73 20 61 72 65 20 62 72 6f 6b 65 6e 20 69 6e  es are broken in
2c9e4 74 6f 20 62 6c 6f 63 6b 73 20 77 68 69 63 68 20  to blocks which 
2c9e5 61 72 65 20 73 74 6f 72 65 64 20 63 6f 6e 74 69  are stored conti
2c9e6 67 75 6f 75 73 6c 79 20 69 6e 0a 2a 2a 20 74 68  guously in.** th
2c9e7 65 20 25 5f 73 65 67 6d 65 6e 74 73 20 74 61 62  e %_segments tab
2c9e8 6c 65 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64  le in sorted ord
2c9e9 65 72 2e 20 20 54 68 69 73 20 6d 65 61 6e 73 20  er.  This means 
2c9ea 74 68 61 74 20 77 68 65 6e 20 74 68 65 20 65 6e  that when the en
2c9eb 64 0a 2a 2a 20 6f 66 20 61 20 6e 6f 64 65 20 69  d.** of a node i
2c9ec 73 20 72 65 61 63 68 65 64 2c 20 74 68 65 20 6e  s reached, the n
2c9ed 65 78 74 20 74 65 72 6d 20 69 73 20 69 6e 20 74  ext term is in t
2c9ee 68 65 20 6e 6f 64 65 20 77 69 74 68 20 74 68 65  he node with the
2c9ef 20 6e 65 78 74 0a 2a 2a 20 67 72 65 61 74 65 72   next.** greater
2c9f0 20 6e 6f 64 65 20 69 64 2e 0a 2a 2a 0a 2a 2a 20   node id..**.** 
2c9f1 4e 65 77 20 64 61 74 61 20 69 73 20 73 70 69 6c  New data is spil
2c9f2 6c 65 64 20 74 6f 20 61 20 6e 65 77 20 6c 65 61  led to a new lea
2c9f3 66 20 6e 6f 64 65 20 77 68 65 6e 20 74 68 65 20  f node when the 
2c9f4 63 75 72 72 65 6e 74 20 6e 6f 64 65 0a 2a 2a 20  current node.** 
2c9f5 65 78 63 65 65 64 73 20 4c 45 41 46 5f 4d 41 58  exceeds LEAF_MAX
2c9f6 20 62 79 74 65 73 20 28 64 65 66 61 75 6c 74 20   bytes (default 
2c9f7 32 30 34 38 29 2e 20 20 4e 65 77 20 64 61 74 61  2048).  New data
2c9f8 20 77 68 69 63 68 20 69 74 73 65 6c 66 20 69 73   which itself is
2c9f9 0a 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20  .** larger than 
2c9fa 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 28  STANDALONE_MIN (
2c9fb 64 65 66 61 75 6c 74 20 31 30 32 34 29 20 69 73  default 1024) is
2c9fc 20 70 6c 61 63 65 64 20 69 6e 20 61 20 73 74 61   placed in a sta
2c9fd 6e 64 61 6c 6f 6e 65 0a 2a 2a 20 6e 6f 64 65 20  ndalone.** node 
2c9fe 28 61 20 6c 65 61 66 20 6e 6f 64 65 20 77 69 74  (a leaf node wit
2c9ff 68 20 61 20 73 69 6e 67 6c 65 20 74 65 72 6d 20  h a single term 
2ca00 61 6e 64 20 64 6f 63 6c 69 73 74 29 2e 20 20 54  and doclist).  T
2ca01 68 65 20 67 6f 61 6c 20 6f 66 0a 2a 2a 20 74 68  he goal of.** th
2ca02 65 73 65 20 73 65 74 74 69 6e 67 73 20 69 73 20  ese settings is 
2ca03 74 6f 20 70 61 63 6b 20 74 6f 67 65 74 68 65 72  to pack together
2ca04 20 67 72 6f 75 70 73 20 6f 66 20 73 6d 61 6c 6c   groups of small
2ca05 20 64 6f 63 6c 69 73 74 73 20 77 68 69 6c 65 0a   doclists while.
2ca06 2a 2a 20 6d 61 6b 69 6e 67 20 69 74 20 65 66 66  ** making it eff
2ca07 69 63 69 65 6e 74 20 74 6f 20 64 69 72 65 63 74  icient to direct
2ca08 6c 79 20 61 63 63 65 73 73 20 6c 61 72 67 65 20  ly access large 
2ca09 64 6f 63 6c 69 73 74 73 2e 20 20 54 68 65 0a 2a  doclists.  The.*
2ca0a 2a 20 61 73 73 75 6d 70 74 69 6f 6e 20 69 73 20  * assumption is 
2ca0b 74 68 61 74 20 6c 61 72 67 65 20 64 6f 63 6c 69  that large docli
2ca0c 73 74 73 20 72 65 70 72 65 73 65 6e 74 20 74 65  sts represent te
2ca0d 72 6d 73 20 77 68 69 63 68 20 61 72 65 20 6d 6f  rms which are mo
2ca0e 72 65 0a 2a 2a 20 6c 69 6b 65 6c 79 20 74 6f 20  re.** likely to 
2ca0f 62 65 20 71 75 65 72 79 20 74 61 72 67 65 74 73  be query targets
2ca10 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73 68 65  ..**.** TODO(she
2ca11 73 73 29 20 49 74 20 6d 61 79 20 62 65 20 75 73  ss) It may be us
2ca12 65 66 75 6c 20 66 6f 72 20 62 6c 6f 63 6b 69 6e  eful for blockin
2ca13 67 20 64 65 63 69 73 69 6f 6e 73 20 74 6f 20 62  g decisions to b
2ca14 65 20 6d 6f 72 65 0a 2a 2a 20 64 79 6e 61 6d 69  e more.** dynami
2ca15 63 2e 20 20 46 6f 72 20 69 6e 73 74 61 6e 63 65  c.  For instance
2ca16 2c 20 69 74 20 6d 61 79 20 6d 61 6b 65 20 6d 6f  , it may make mo
2ca17 72 65 20 73 65 6e 73 65 20 74 6f 20 68 61 76 65  re sense to have
2ca18 20 61 20 32 2e 35 6b 20 6c 65 61 66 0a 2a 2a 20   a 2.5k leaf.** 
2ca19 6e 6f 64 65 20 72 61 74 68 65 72 20 74 68 61 6e  node rather than
2ca1a 20 73 70 6c 69 74 74 69 6e 67 20 69 6e 74 6f 20   splitting into 
2ca1b 32 6b 20 61 6e 64 20 2e 35 6b 20 6e 6f 64 65 73  2k and .5k nodes
2ca1c 2e 20 20 4d 79 20 69 6e 74 75 69 74 69 6f 6e 20  .  My intuition 
2ca1d 69 73 0a 2a 2a 20 74 68 61 74 20 74 68 69 73 20  is.** that this 
2ca1e 6d 69 67 68 74 20 65 78 74 65 6e 64 20 74 68 72  might extend thr
2ca1f 6f 75 67 68 20 32 78 20 6f 72 20 34 78 20 74 68  ough 2x or 4x th
2ca20 65 20 70 61 67 65 73 69 7a 65 2e 0a 2a 2a 0a 2a  e pagesize..**.*
2ca21 2a 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74 20 69  *.**** Segment i
2ca22 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 2a 2a  nterior nodes **
2ca23 2a 2a 0a 2a 2a 20 53 65 67 6d 65 6e 74 20 69 6e  **.** Segment in
2ca24 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 73 74 6f  terior nodes sto
2ca25 72 65 20 62 6c 6f 63 6b 69 64 73 20 66 6f 72 20  re blockids for 
2ca26 73 75 62 74 72 65 65 20 6e 6f 64 65 73 20 61 6e  subtree nodes an
2ca27 64 20 74 65 72 6d 73 0a 2a 2a 20 74 6f 20 64 65  d terms.** to de
2ca28 73 63 72 69 62 65 20 77 68 61 74 20 64 61 74 61  scribe what data
2ca29 20 69 73 20 73 74 6f 72 65 64 20 62 79 20 74 68   is stored by th
2ca2a 65 20 65 61 63 68 20 73 75 62 74 72 65 65 2e 20  e each subtree. 
2ca2b 20 49 6e 74 65 72 69 6f 72 0a 2a 2a 20 6e 6f 64   Interior.** nod
2ca2c 65 73 20 61 72 65 20 77 72 69 74 74 65 6e 20 75  es are written u
2ca2d 73 69 6e 67 20 49 6e 74 65 72 69 6f 72 57 72 69  sing InteriorWri
2ca2e 74 65 72 2c 20 61 6e 64 20 72 65 61 64 20 75 73  ter, and read us
2ca2f 69 6e 67 0a 2a 2a 20 49 6e 74 65 72 69 6f 72 52  ing.** InteriorR
2ca30 65 61 64 65 72 2e 20 20 49 6e 74 65 72 69 6f 72  eader.  Interior
2ca31 57 72 69 74 65 72 73 20 61 72 65 20 63 72 65 61  Writers are crea
2ca32 74 65 64 20 61 73 20 6e 65 65 64 65 64 20 77 68  ted as needed wh
2ca33 65 6e 0a 2a 2a 20 53 65 67 6d 65 6e 74 57 72 69  en.** SegmentWri
2ca34 74 65 72 20 63 72 65 61 74 65 73 20 6e 65 77 20  ter creates new 
2ca35 6c 65 61 66 20 6e 6f 64 65 73 2c 20 6f 72 20 77  leaf nodes, or w
2ca36 68 65 6e 20 61 6e 20 69 6e 74 65 72 69 6f 72 20  hen an interior 
2ca37 6e 6f 64 65 0a 2a 2a 20 69 74 73 65 6c 66 20 67  node.** itself g
2ca38 72 6f 77 73 20 74 6f 6f 20 62 69 67 20 61 6e 64  rows too big and
2ca39 20 6d 75 73 74 20 62 65 20 73 70 6c 69 74 2e 20   must be split. 
2ca3a 20 54 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 69   The format of i
2ca3b 6e 74 65 72 69 6f 72 0a 2a 2a 20 6e 6f 64 65 73  nterior.** nodes
2ca3c 3a 0a 2a 2a 0a 2a 2a 20 76 61 72 69 6e 74 20 69  :.**.** varint i
2ca3d 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 20  Height;         
2ca3e 20 20 28 68 65 69 67 68 74 20 66 72 6f 6d 20 6c    (height from l
2ca3f 65 61 66 20 6c 65 76 65 6c 2c 20 61 6c 77 61 79  eaf level, alway
2ca40 73 20 3e 30 29 0a 2a 2a 20 76 61 72 69 6e 74 20  s >0).** varint 
2ca41 69 42 6c 6f 63 6b 69 64 3b 20 20 20 20 20 20 20  iBlockid;       
2ca42 20 20 20 28 62 6c 6f 63 6b 20 69 64 20 6f 66 20     (block id of 
2ca43 6e 6f 64 65 27 73 20 6c 65 66 74 6d 6f 73 74 20  node's leftmost 
2ca44 73 75 62 74 72 65 65 29 0a 2a 2a 20 6f 70 74 69  subtree).** opti
2ca45 6f 6e 61 6c 20 7b 0a 2a 2a 20 20 20 76 61 72 69  onal {.**   vari
2ca46 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20 20 20 20  nt nTerm;       
2ca47 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 66      (length of f
2ca48 69 72 73 74 20 74 65 72 6d 29 0a 2a 2a 20 20 20  irst term).**   
2ca49 63 68 61 72 20 70 54 65 72 6d 5b 6e 54 65 72 6d  char pTerm[nTerm
2ca4a 5d 3b 20 20 20 20 20 20 28 63 6f 6e 74 65 6e 74  ];      (content
2ca4b 20 6f 66 20 66 69 72 73 74 20 74 65 72 6d 29 0a   of first term).
2ca4c 2a 2a 20 20 20 61 72 72 61 79 20 7b 0a 2a 2a 20  **   array {.** 
2ca4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ca4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
2ca4f 66 75 72 74 68 65 72 20 74 65 72 6d 73 20 61 72  further terms ar
2ca50 65 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64 29  e delta-encoded)
2ca51 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20 6e  .**     varint n
2ca52 50 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 20  Prefix;         
2ca53 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 73 68     (length of sh
2ca54 61 72 65 64 20 70 72 65 66 69 78 20 77 69 74 68  ared prefix with
2ca55 20 70 72 65 76 69 6f 75 73 20 74 65 72 6d 29 0a   previous term).
2ca56 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20 6e 53  **     varint nS
2ca57 75 66 66 69 78 3b 20 20 20 20 20 20 20 20 20 20  uffix;          
2ca58 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 75 6e 73    (length of uns
2ca59 68 61 72 65 64 20 73 75 66 66 69 78 29 0a 2a 2a  hared suffix).**
2ca5a 20 20 20 20 20 63 68 61 72 20 70 54 65 72 6d 53       char pTermS
2ca5b 75 66 66 69 78 5b 6e 53 75 66 66 69 78 5d 3b 20  uffix[nSuffix]; 
2ca5c 28 75 6e 73 68 61 72 65 64 20 73 75 66 66 69 78  (unshared suffix
2ca5d 20 6f 66 20 6e 65 78 74 20 74 65 72 6d 29 0a 2a   of next term).*
2ca5e 2a 20 20 20 7d 0a 2a 2a 20 7d 0a 2a 2a 0a 2a 2a  *   }.** }.**.**
2ca5f 20 48 65 72 65 2c 20 6f 70 74 69 6f 6e 61 6c 20   Here, optional 
2ca60 7b 20 58 20 7d 20 6d 65 61 6e 73 20 61 6e 20 6f  { X } means an o
2ca61 70 74 69 6f 6e 61 6c 20 65 6c 65 6d 65 6e 74 2c  ptional element,
2ca62 20 77 68 69 6c 65 20 61 72 72 61 79 20 7b 20 58   while array { X
2ca63 20 7d 0a 2a 2a 20 6d 65 61 6e 73 20 7a 65 72 6f   }.** means zero
2ca64 20 6f 72 20 6d 6f 72 65 20 6f 63 63 75 72 72 65   or more occurre
2ca65 6e 63 65 73 20 6f 66 20 58 2c 20 61 64 6a 61 63  nces of X, adjac
2ca66 65 6e 74 20 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a  ent in memory..*
2ca67 2a 0a 2a 2a 20 41 6e 20 69 6e 74 65 72 69 6f 72  *.** An interior
2ca68 20 6e 6f 64 65 20 65 6e 63 6f 64 65 73 20 6e 20   node encodes n 
2ca69 74 65 72 6d 73 20 73 65 70 61 72 61 74 69 6e 67  terms separating
2ca6a 20 6e 2b 31 20 73 75 62 74 72 65 65 73 2e 20 20   n+1 subtrees.  
2ca6b 54 68 65 0a 2a 2a 20 73 75 62 74 72 65 65 20 62  The.** subtree b
2ca6c 6c 6f 63 6b 73 20 61 72 65 20 63 6f 6e 74 69 67  locks are contig
2ca6d 75 6f 75 73 2c 20 73 6f 20 6f 6e 6c 79 20 74 68  uous, so only th
2ca6e 65 20 66 69 72 73 74 20 73 75 62 74 72 65 65 27  e first subtree'
2ca6f 73 20 62 6c 6f 63 6b 69 64 0a 2a 2a 20 69 73 20  s blockid.** is 
2ca70 65 6e 63 6f 64 65 64 2e 20 20 54 68 65 20 73 75  encoded.  The su
2ca71 62 74 72 65 65 20 61 74 20 69 42 6c 6f 63 6b 69  btree at iBlocki
2ca72 64 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 61  d will contain a
2ca73 6c 6c 20 74 65 72 6d 73 20 6c 65 73 73 0a 2a 2a  ll terms less.**
2ca74 20 74 68 61 6e 20 74 68 65 20 66 69 72 73 74 20   than the first 
2ca75 74 65 72 6d 20 65 6e 63 6f 64 65 64 20 28 6f 72  term encoded (or
2ca76 20 61 6c 6c 20 74 65 72 6d 73 20 69 66 20 6e 6f   all terms if no
2ca77 20 74 65 72 6d 20 69 73 20 65 6e 63 6f 64 65 64   term is encoded
2ca78 29 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  )..** Otherwise,
2ca79 20 66 6f 72 20 74 65 72 6d 73 20 67 72 65 61 74   for terms great
2ca7a 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  er than or equal
2ca7b 20 74 6f 20 70 54 65 72 6d 5b 69 5d 20 62 75 74   to pTerm[i] but
2ca7c 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 70 54   less.** than pT
2ca7d 65 72 6d 5b 69 2b 31 5d 2c 20 74 68 65 20 73 75  erm[i+1], the su
2ca7e 62 74 72 65 65 20 66 6f 72 20 74 68 61 74 20 74  btree for that t
2ca7f 65 72 6d 20 77 69 6c 6c 20 62 65 20 72 6f 6f 74  erm will be root
2ca80 65 64 20 61 74 0a 2a 2a 20 69 42 6c 6f 63 6b 69  ed at.** iBlocki
2ca81 64 2b 69 2e 20 20 49 6e 74 65 72 69 6f 72 20 6e  d+i.  Interior n
2ca82 6f 64 65 73 20 6f 6e 6c 79 20 73 74 6f 72 65 20  odes only store 
2ca83 65 6e 6f 75 67 68 20 74 65 72 6d 20 64 61 74 61  enough term data
2ca84 20 74 6f 0a 2a 2a 20 64 69 73 74 69 6e 67 75 69   to.** distingui
2ca85 73 68 20 61 64 6a 61 63 65 6e 74 20 63 68 69 6c  sh adjacent chil
2ca86 64 72 65 6e 20 28 69 66 20 74 68 65 20 72 69 67  dren (if the rig
2ca87 68 74 6d 6f 73 74 20 74 65 72 6d 20 6f 66 20 74  htmost term of t
2ca88 68 65 20 6c 65 66 74 0a 2a 2a 20 63 68 69 6c 64  he left.** child
2ca89 20 69 73 20 22 73 6f 6d 65 74 68 69 6e 67 22 2c   is "something",
2ca8a 20 61 6e 64 20 74 68 65 20 6c 65 66 74 6d 6f 73   and the leftmos
2ca8b 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20 72 69  t term of the ri
2ca8c 67 68 74 20 63 68 69 6c 64 20 69 73 0a 2a 2a 20  ght child is.** 
2ca8d 22 77 69 63 6b 65 64 22 2c 20 6f 6e 6c 79 20 22  "wicked", only "
2ca8e 77 22 20 69 73 20 73 74 6f 72 65 64 29 2e 0a 2a  w" is stored)..*
2ca8f 2a 0a 2a 2a 20 4e 65 77 20 64 61 74 61 20 69 73  *.** New data is
2ca90 20 73 70 69 6c 6c 65 64 20 74 6f 20 61 20 6e 65   spilled to a ne
2ca91 77 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20  w interior node 
2ca92 61 74 20 74 68 65 20 73 61 6d 65 20 68 65 69 67  at the same heig
2ca93 68 74 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 63  ht when.** the c
2ca94 75 72 72 65 6e 74 20 6e 6f 64 65 20 65 78 63 65  urrent node exce
2ca95 65 64 73 20 49 4e 54 45 52 49 4f 52 5f 4d 41 58  eds INTERIOR_MAX
2ca96 20 62 79 74 65 73 20 28 64 65 66 61 75 6c 74 20   bytes (default 
2ca97 32 30 34 38 29 2e 0a 2a 2a 20 49 4e 54 45 52 49  2048)..** INTERI
2ca98 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53 20 28 64 65  OR_MIN_TERMS (de
2ca99 66 61 75 6c 74 20 37 29 20 6b 65 65 70 73 20 6c  fault 7) keeps l
2ca9a 61 72 67 65 20 74 65 72 6d 73 20 66 72 6f 6d 20  arge terms from 
2ca9b 6d 6f 6e 6f 70 6f 6c 69 7a 69 6e 67 0a 2a 2a 20  monopolizing.** 
2ca9c 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 61  interior nodes a
2ca9d 6e 64 20 6d 61 6b 69 6e 67 20 74 68 65 20 74 72  nd making the tr
2ca9e 65 65 20 74 6f 6f 20 73 6b 69 6e 6e 79 2e 20 20  ee too skinny.  
2ca9f 54 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  The interior nod
2caa0 65 73 0a 2a 2a 20 61 74 20 61 20 67 69 76 65 6e  es.** at a given
2caa1 20 68 65 69 67 68 74 20 61 72 65 20 6e 61 74 75   height are natu
2caa2 72 61 6c 6c 79 20 74 72 61 63 6b 65 64 20 62 79  rally tracked by
2caa3 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20   interior nodes 
2caa4 61 74 0a 2a 2a 20 68 65 69 67 68 74 2b 31 2c 20  at.** height+1, 
2caa5 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a  and so on..**.**
2caa6 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74 20 64 69  .**** Segment di
2caa7 72 65 63 74 6f 72 79 20 2a 2a 2a 2a 0a 2a 2a 20  rectory ****.** 
2caa8 54 68 65 20 73 65 67 6d 65 6e 74 20 64 69 72 65  The segment dire
2caa9 63 74 6f 72 79 20 69 6e 20 74 61 62 6c 65 20 25  ctory in table %
2caaa 5f 73 65 67 64 69 72 20 73 74 6f 72 65 73 20 6d  _segdir stores m
2caab 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  eta-information 
2caac 66 6f 72 0a 2a 2a 20 6d 65 72 67 69 6e 67 20 61  for.** merging a
2caad 6e 64 20 64 65 6c 65 74 69 6e 67 20 73 65 67 6d  nd deleting segm
2caae 65 6e 74 73 2c 20 61 6e 64 20 61 6c 73 6f 20 74  ents, and also t
2caaf 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20  he root node of 
2cab0 74 68 65 0a 2a 2a 20 73 65 67 6d 65 6e 74 27 73  the.** segment's
2cab1 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   tree..**.** The
2cab2 20 72 6f 6f 74 20 6e 6f 64 65 20 69 73 20 74 68   root node is th
2cab3 65 20 74 6f 70 20 6e 6f 64 65 20 6f 66 20 74 68  e top node of th
2cab4 65 20 73 65 67 6d 65 6e 74 27 73 20 74 72 65 65  e segment's tree
2cab5 20 61 66 74 65 72 20 65 6e 63 6f 64 69 6e 67 0a   after encoding.
2cab6 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 20 73 65  ** the entire se
2cab7 67 6d 65 6e 74 2c 20 72 65 73 74 72 69 63 74 65  gment, restricte
2cab8 64 20 74 6f 20 52 4f 4f 54 5f 4d 41 58 20 62 79  d to ROOT_MAX by
2cab9 74 65 73 20 28 64 65 66 61 75 6c 74 20 31 30 32  tes (default 102
2caba 34 29 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 75 6c  4)..** This coul
2cabb 64 20 62 65 20 65 69 74 68 65 72 20 61 20 6c 65  d be either a le
2cabc 61 66 20 6e 6f 64 65 20 6f 72 20 61 6e 20 69 6e  af node or an in
2cabd 74 65 72 69 6f 72 20 6e 6f 64 65 2e 20 20 49 66  terior node.  If
2cabe 20 74 68 65 20 74 6f 70 0a 2a 2a 20 6e 6f 64 65   the top.** node
2cabf 20 72 65 71 75 69 72 65 73 20 6d 6f 72 65 20 74   requires more t
2cac0 68 61 6e 20 52 4f 4f 54 5f 4d 41 58 20 62 79 74  han ROOT_MAX byt
2cac1 65 73 2c 20 69 74 20 69 73 20 66 6c 75 73 68 65  es, it is flushe
2cac2 64 20 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 0a  d to %_segments.
2cac3 2a 2a 20 61 6e 64 20 61 20 6e 65 77 20 72 6f 6f  ** and a new roo
2cac4 74 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20  t interior node 
2cac5 69 73 20 67 65 6e 65 72 61 74 65 64 20 28 77 68  is generated (wh
2cac6 69 63 68 20 73 68 6f 75 6c 64 20 61 6c 77 61 79  ich should alway
2cac7 73 20 66 69 74 0a 2a 2a 20 77 69 74 68 69 6e 20  s fit.** within 
2cac8 52 4f 4f 54 5f 4d 41 58 20 62 65 63 61 75 73 65  ROOT_MAX because
2cac9 20 69 74 20 6f 6e 6c 79 20 6e 65 65 64 73 20 73   it only needs s
2caca 70 61 63 65 20 66 6f 72 20 32 20 76 61 72 69 6e  pace for 2 varin
2cacb 74 73 2c 20 74 68 65 0a 2a 2a 20 68 65 69 67 68  ts, the.** heigh
2cacc 74 20 61 6e 64 20 74 68 65 20 62 6c 6f 63 6b 69  t and the blocki
2cacd 64 20 6f 66 20 74 68 65 20 70 72 65 76 69 6f 75  d of the previou
2cace 73 20 72 6f 6f 74 29 2e 0a 2a 2a 0a 2a 2a 20 54  s root)..**.** T
2cacf 68 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74  he meta-informat
2cad0 69 6f 6e 20 69 6e 20 74 68 65 20 73 65 67 6d 65  ion in the segme
2cad1 6e 74 20 64 69 72 65 63 74 6f 72 79 20 69 73 3a  nt directory is:
2cad2 0a 2a 2a 20 20 20 6c 65 76 65 6c 20 20 20 20 20  .**   level     
2cad3 20 20 20 20 20 20 20 20 20 20 2d 20 73 65 67 6d            - segm
2cad4 65 6e 74 20 6c 65 76 65 6c 20 28 73 65 65 20 62  ent level (see b
2cad5 65 6c 6f 77 29 0a 2a 2a 20 20 20 69 64 78 20 20  elow).**   idx  
2cad6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d                 -
2cad7 20 69 6e 64 65 78 20 77 69 74 68 69 6e 20 6c 65   index within le
2cad8 76 65 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  vel.**          
2cad9 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 20 28               - (
2cada 6c 65 76 65 6c 2c 69 64 78 20 75 6e 69 71 75 65  level,idx unique
2cadb 6c 79 20 69 64 65 6e 74 69 66 79 20 61 20 73 65  ly identify a se
2cadc 67 6d 65 6e 74 29 0a 2a 2a 20 20 20 73 74 61 72  gment).**   star
2cadd 74 5f 62 6c 6f 63 6b 20 20 20 20 20 20 20 20 20  t_block         
2cade 2d 20 66 69 72 73 74 20 6c 65 61 66 20 6e 6f 64  - first leaf nod
2cadf 65 0a 2a 2a 20 20 20 6c 65 61 76 65 73 5f 65 6e  e.**   leaves_en
2cae0 64 5f 62 6c 6f 63 6b 20 20 20 20 2d 20 6c 61 73  d_block    - las
2cae1 74 20 6c 65 61 66 20 6e 6f 64 65 0a 2a 2a 20 20  t leaf node.**  
2cae2 20 65 6e 64 5f 62 6c 6f 63 6b 20 20 20 20 20 20   end_block      
2cae3 20 20 20 20 20 2d 20 6c 61 73 74 20 62 6c 6f 63       - last bloc
2cae4 6b 20 28 69 6e 63 6c 75 64 69 6e 67 20 69 6e 74  k (including int
2cae5 65 72 69 6f 72 20 6e 6f 64 65 73 29 0a 2a 2a 20  erior nodes).** 
2cae6 20 20 72 6f 6f 74 20 20 20 20 20 20 20 20 20 20    root          
2cae7 20 20 20 20 20 20 2d 20 63 6f 6e 74 65 6e 74 73        - contents
2cae8 20 6f 66 20 72 6f 6f 74 20 6e 6f 64 65 0a 2a 2a   of root node.**
2cae9 0a 2a 2a 20 49 66 20 74 68 65 20 72 6f 6f 74 20  .** If the root 
2caea 6e 6f 64 65 20 69 73 20 61 20 6c 65 61 66 20 6e  node is a leaf n
2caeb 6f 64 65 2c 20 74 68 65 6e 20 73 74 61 72 74 5f  ode, then start_
2caec 62 6c 6f 63 6b 2c 0a 2a 2a 20 6c 65 61 76 65 73  block,.** leaves
2caed 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 61 6e 64 20  _end_block, and 
2caee 65 6e 64 5f 62 6c 6f 63 6b 20 61 72 65 20 61 6c  end_block are al
2caef 6c 20 30 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20  l 0..**.**.**** 
2caf0 53 65 67 6d 65 6e 74 20 6d 65 72 67 69 6e 67 20  Segment merging 
2caf1 2a 2a 2a 2a 0a 2a 2a 20 54 6f 20 61 6d 6f 72 74  ****.** To amort
2caf2 69 7a 65 20 75 70 64 61 74 65 20 63 6f 73 74 73  ize update costs
2caf3 2c 20 73 65 67 6d 65 6e 74 73 20 61 72 65 20 67  , segments are g
2caf4 72 6f 75 70 73 20 69 6e 74 6f 20 6c 65 76 65 6c  roups into level
2caf5 73 20 61 6e 64 0a 2a 2a 20 6d 65 72 67 65 64 20  s and.** merged 
2caf6 69 6e 20 6d 61 74 63 68 65 73 2e 20 20 45 61 63  in matches.  Eac
2caf7 68 20 69 6e 63 72 65 61 73 65 20 69 6e 20 6c 65  h increase in le
2caf8 76 65 6c 20 72 65 70 72 65 73 65 6e 74 73 20 65  vel represents e
2caf9 78 70 6f 6e 65 6e 74 69 61 6c 6c 79 0a 2a 2a 20  xponentially.** 
2cafa 6d 6f 72 65 20 64 6f 63 75 6d 65 6e 74 73 2e 0a  more documents..
2cafb 2a 2a 0a 2a 2a 20 4e 65 77 20 64 6f 63 75 6d 65  **.** New docume
2cafc 6e 74 73 20 28 61 63 74 75 61 6c 6c 79 2c 20 64  nts (actually, d
2cafd 6f 63 75 6d 65 6e 74 20 75 70 64 61 74 65 73 29  ocument updates)
2cafe 20 61 72 65 20 74 6f 6b 65 6e 69 7a 65 64 20 61   are tokenized a
2caff 6e 64 0a 2a 2a 20 77 72 69 74 74 65 6e 20 69 6e  nd.** written in
2cb00 64 69 76 69 64 75 61 6c 6c 79 20 28 75 73 69 6e  dividually (usin
2cb01 67 20 4c 65 61 66 57 72 69 74 65 72 29 20 74 6f  g LeafWriter) to
2cb02 20 61 20 6c 65 76 65 6c 20 30 20 73 65 67 6d 65   a level 0 segme
2cb03 6e 74 2c 20 77 69 74 68 0a 2a 2a 20 69 6e 63 72  nt, with.** incr
2cb04 65 6d 65 6e 74 69 6e 67 20 69 64 78 2e 20 20 57  ementing idx.  W
2cb05 68 65 6e 20 69 64 78 20 72 65 61 63 68 65 73 20  hen idx reaches 
2cb06 4d 45 52 47 45 5f 43 4f 55 4e 54 20 28 64 65 66  MERGE_COUNT (def
2cb07 61 75 6c 74 20 31 36 29 2c 20 61 6c 6c 0a 2a 2a  ault 16), all.**
2cb08 20 6c 65 76 65 6c 20 30 20 73 65 67 6d 65 6e 74   level 0 segment
2cb09 73 20 61 72 65 20 6d 65 72 67 65 64 20 69 6e 74  s are merged int
2cb0a 6f 20 61 20 73 69 6e 67 6c 65 20 6c 65 76 65 6c  o a single level
2cb0b 20 31 20 73 65 67 6d 65 6e 74 2e 20 20 4c 65 76   1 segment.  Lev
2cb0c 65 6c 20 31 0a 2a 2a 20 69 73 20 70 6f 70 75 6c  el 1.** is popul
2cb0d 61 74 65 64 20 6c 69 6b 65 20 6c 65 76 65 6c 20  ated like level 
2cb0e 30 2c 20 61 6e 64 20 65 76 65 6e 74 75 61 6c 6c  0, and eventuall
2cb0f 79 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 6c 65  y MERGE_COUNT le
2cb10 76 65 6c 20 31 0a 2a 2a 20 73 65 67 6d 65 6e 74  vel 1.** segment
2cb11 73 20 61 72 65 20 6d 65 72 67 65 64 20 74 6f 20  s are merged to 
2cb12 61 20 73 69 6e 67 6c 65 20 6c 65 76 65 6c 20 32  a single level 2
2cb13 20 73 65 67 6d 65 6e 74 20 28 72 65 70 72 65 73   segment (repres
2cb14 65 6e 74 69 6e 67 0a 2a 2a 20 4d 45 52 47 45 5f  enting.** MERGE_
2cb15 43 4f 55 4e 54 5e 32 20 75 70 64 61 74 65 73 29  COUNT^2 updates)
2cb16 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a  , and so on..**.
2cb17 2a 2a 20 41 20 73 65 67 6d 65 6e 74 20 6d 65 72  ** A segment mer
2cb18 67 65 20 74 72 61 76 65 72 73 65 73 20 61 6c 6c  ge traverses all
2cb19 20 73 65 67 6d 65 6e 74 73 20 61 74 20 61 20 67   segments at a g
2cb1a 69 76 65 6e 20 6c 65 76 65 6c 20 69 6e 0a 2a 2a  iven level in.**
2cb1b 20 70 61 72 61 6c 6c 65 6c 2c 20 70 65 72 66 6f   parallel, perfo
2cb1c 72 6d 69 6e 67 20 61 20 73 74 72 61 69 67 68 74  rming a straight
2cb1d 66 6f 72 77 61 72 64 20 73 6f 72 74 65 64 20 6d  forward sorted m
2cb1e 65 72 67 65 2e 20 20 53 69 6e 63 65 20 73 65 67  erge.  Since seg
2cb1f 6d 65 6e 74 0a 2a 2a 20 6c 65 61 66 20 6e 6f 64  ment.** leaf nod
2cb20 65 73 20 61 72 65 20 77 72 69 74 74 65 6e 20 69  es are written i
2cb21 6e 20 74 6f 20 74 68 65 20 25 5f 73 65 67 6d 65  n to the %_segme
2cb22 6e 74 73 20 74 61 62 6c 65 20 69 6e 20 6f 72 64  nts table in ord
2cb23 65 72 2c 20 74 68 69 73 0a 2a 2a 20 6d 65 72 67  er, this.** merg
2cb24 65 20 74 72 61 76 65 72 73 65 73 20 74 68 65 20  e traverses the 
2cb25 75 6e 64 65 72 6c 79 69 6e 67 20 73 71 6c 69 74  underlying sqlit
2cb26 65 20 64 69 73 6b 20 73 74 72 75 63 74 75 72 65  e disk structure
2cb27 73 20 65 66 66 69 63 69 65 6e 74 6c 79 2e 0a 2a  s efficiently..*
2cb28 2a 20 41 66 74 65 72 20 74 68 65 20 6d 65 72 67  * After the merg
2cb29 65 2c 20 61 6c 6c 20 73 65 67 6d 65 6e 74 20 62  e, all segment b
2cb2a 6c 6f 63 6b 73 20 66 72 6f 6d 20 74 68 65 20 6d  locks from the m
2cb2b 65 72 67 65 64 20 6c 65 76 65 6c 20 61 72 65 0a  erged level are.
2cb2c 2a 2a 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a 2a  ** deleted..**.*
2cb2d 2a 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 63 6f  * MERGE_COUNT co
2cb2e 6e 74 72 6f 6c 73 20 68 6f 77 20 6f 66 74 65 6e  ntrols how often
2cb2f 20 77 65 20 6d 65 72 67 65 20 73 65 67 6d 65 6e   we merge segmen
2cb30 74 73 2e 20 20 31 36 20 73 65 65 6d 73 20 74 6f  ts.  16 seems to
2cb31 20 62 65 0a 2a 2a 20 73 6f 6d 65 77 68 61 74 20   be.** somewhat 
2cb32 6f 66 20 61 20 73 77 65 65 74 20 73 70 6f 74 20  of a sweet spot 
2cb33 66 6f 72 20 69 6e 73 65 72 74 69 6f 6e 20 70 65  for insertion pe
2cb34 72 66 6f 72 6d 61 6e 63 65 2e 20 20 33 32 20 61  rformance.  32 a
2cb35 6e 64 20 36 34 20 73 68 6f 77 0a 2a 2a 20 76 65  nd 64 show.** ve
2cb36 72 79 20 73 69 6d 69 6c 61 72 20 70 65 72 66 6f  ry similar perfo
2cb37 72 6d 61 6e 63 65 20 6e 75 6d 62 65 72 73 20 74  rmance numbers t
2cb38 6f 20 31 36 20 6f 6e 20 69 6e 73 65 72 74 69 6f  o 16 on insertio
2cb39 6e 2c 20 74 68 6f 75 67 68 20 74 68 65 79 27 72  n, though they'r
2cb3a 65 0a 2a 2a 20 61 20 74 69 6e 79 20 62 69 74 20  e.** a tiny bit 
2cb3b 73 6c 6f 77 65 72 20 28 70 65 72 68 61 70 73 20  slower (perhaps 
2cb3c 64 75 65 20 74 6f 20 6d 6f 72 65 20 6f 76 65 72  due to more over
2cb3d 68 65 61 64 20 69 6e 20 6d 65 72 67 65 2d 74 69  head in merge-ti
2cb3e 6d 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 29 2e 20  me.** sorting). 
2cb3f 20 38 20 69 73 20 61 62 6f 75 74 20 32 30 25 20   8 is about 20% 
2cb40 73 6c 6f 77 65 72 20 74 68 61 6e 20 31 36 2c 20  slower than 16, 
2cb41 34 20 61 62 6f 75 74 20 35 30 25 20 73 6c 6f 77  4 about 50% slow
2cb42 65 72 20 74 68 61 6e 0a 2a 2a 20 31 36 2c 20 32  er than.** 16, 2
2cb43 20 61 62 6f 75 74 20 36 36 25 20 73 6c 6f 77 65   about 66% slowe
2cb44 72 20 74 68 61 6e 20 31 36 2e 0a 2a 2a 0a 2a 2a  r than 16..**.**
2cb45 20 41 74 20 71 75 65 72 79 20 74 69 6d 65 2c 20   At query time, 
2cb46 68 69 67 68 20 4d 45 52 47 45 5f 43 4f 55 4e 54  high MERGE_COUNT
2cb47 20 69 6e 63 72 65 61 73 65 73 20 74 68 65 20 6e   increases the n
2cb48 75 6d 62 65 72 20 6f 66 20 73 65 67 6d 65 6e 74  umber of segment
2cb49 73 0a 2a 2a 20 77 68 69 63 68 20 6e 65 65 64 20  s.** which need 
2cb4a 74 6f 20 62 65 20 73 63 61 6e 6e 65 64 20 61 6e  to be scanned an
2cb4b 64 20 6d 65 72 67 65 64 2e 20 20 46 6f 72 20 69  d merged.  For i
2cb4c 6e 73 74 61 6e 63 65 2c 20 77 69 74 68 20 31 30  nstance, with 10
2cb4d 30 6b 20 64 6f 63 73 0a 2a 2a 20 69 6e 73 65 72  0k docs.** inser
2cb4e 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 45  ted:.**.**    ME
2cb4f 52 47 45 5f 43 4f 55 4e 54 20 20 20 73 65 67 6d  RGE_COUNT   segm
2cb50 65 6e 74 73 0a 2a 2a 20 20 20 20 20 20 20 31 36  ents.**       16
2cb51 20 20 20 20 20 20 20 20 20 20 20 32 35 0a 2a 2a             25.**
2cb52 20 20 20 20 20 20 20 20 38 20 20 20 20 20 20 20          8       
2cb53 20 20 20 20 31 32 0a 2a 2a 20 20 20 20 20 20 20      12.**       
2cb54 20 34 20 20 20 20 20 20 20 20 20 20 20 31 30 0a   4           10.
2cb55 2a 2a 20 20 20 20 20 20 20 20 32 20 20 20 20 20  **        2     
2cb56 20 20 20 20 20 20 20 36 0a 2a 2a 0a 2a 2a 20 54         6.**.** T
2cb57 68 69 73 20 61 70 70 65 61 72 73 20 74 6f 20 68  his appears to h
2cb58 61 76 65 20 6f 6e 6c 79 20 61 20 6d 6f 64 65 72  ave only a moder
2cb59 61 74 65 20 69 6d 70 61 63 74 20 6f 6e 20 71 75  ate impact on qu
2cb5a 65 72 69 65 73 20 66 6f 72 20 76 65 72 79 0a 2a  eries for very.*
2cb5b 2a 20 66 72 65 71 75 65 6e 74 20 74 65 72 6d 73  * frequent terms
2cb5c 20 28 77 68 69 63 68 20 61 72 65 20 73 6f 6d 65   (which are some
2cb5d 77 68 61 74 20 64 6f 6d 69 6e 61 74 65 64 20 62  what dominated b
2cb5e 79 20 73 65 67 6d 65 6e 74 20 6d 65 72 67 65 0a  y segment merge.
2cb5f 2a 2a 20 63 6f 73 74 73 29 2c 20 61 6e 64 20 69  ** costs), and i
2cb60 6e 66 72 65 71 75 65 6e 74 20 61 6e 64 20 6e 6f  nfrequent and no
2cb61 6e 2d 65 78 69 73 74 65 6e 74 20 74 65 72 6d 73  n-existent terms
2cb62 20 73 74 69 6c 6c 20 73 65 65 6d 20 74 6f 20 62   still seem to b
2cb63 65 20 66 61 73 74 0a 2a 2a 20 65 76 65 6e 20 77  e fast.** even w
2cb64 69 74 68 20 6d 61 6e 79 20 73 65 67 6d 65 6e 74  ith many segment
2cb65 73 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73 68  s..**.** TODO(sh
2cb66 65 73 73 29 20 54 68 61 74 20 73 61 69 64 2c 20  ess) That said, 
2cb67 69 74 20 77 6f 75 6c 64 20 62 65 20 6e 69 63 65  it would be nice
2cb68 20 74 6f 20 68 61 76 65 20 61 20 62 65 74 74 65   to have a bette
2cb69 72 20 71 75 65 72 79 2d 73 69 64 65 0a 2a 2a 20  r query-side.** 
2cb6a 61 72 67 75 6d 65 6e 74 20 66 6f 72 20 4d 45 52  argument for MER
2cb6b 47 45 5f 43 4f 55 4e 54 20 6f 66 20 31 36 2e 20  GE_COUNT of 16. 
2cb6c 20 41 6c 73 6f 2c 20 69 74 20 69 73 20 70 6f 73   Also, it is pos
2cb6d 73 69 62 6c 65 2f 6c 69 6b 65 6c 79 20 74 68 61  sible/likely tha
2cb6e 74 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f  t.** optimizatio
2cb6f 6e 73 20 74 6f 20 74 68 69 6e 67 73 20 6c 69 6b  ns to things lik
2cb70 65 20 64 6f 63 6c 69 73 74 20 6d 65 72 67 69 6e  e doclist mergin
2cb71 67 20 77 69 6c 6c 20 73 77 69 6e 67 20 74 68 65  g will swing the
2cb72 20 73 77 65 65 74 0a 2a 2a 20 73 70 6f 74 20 61   sweet.** spot a
2cb73 72 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 0a  round..**.**.**.
2cb74 2a 2a 2a 2a 20 48 61 6e 64 6c 69 6e 67 20 6f 66  **** Handling of
2cb75 20 64 65 6c 65 74 69 6f 6e 73 20 61 6e 64 20 75   deletions and u
2cb76 70 64 61 74 65 73 20 2a 2a 2a 2a 0a 2a 2a 20 53  pdates ****.** S
2cb77 69 6e 63 65 20 77 65 27 72 65 20 75 73 69 6e 67  ince we're using
2cb78 20 61 20 73 65 67 6d 65 6e 74 65 64 20 73 74 72   a segmented str
2cb79 75 63 74 75 72 65 2c 20 77 69 74 68 20 6e 6f 20  ucture, with no 
2cb7a 64 6f 63 69 64 2d 6f 72 69 65 6e 74 65 64 0a 2a  docid-oriented.*
2cb7b 2a 20 69 6e 64 65 78 20 69 6e 74 6f 20 74 68 65  * index into the
2cb7c 20 74 65 72 6d 20 69 6e 64 65 78 2c 20 77 65 20   term index, we 
2cb7d 63 6c 65 61 72 6c 79 20 63 61 6e 6e 6f 74 20 73  clearly cannot s
2cb7e 69 6d 70 6c 79 20 75 70 64 61 74 65 20 74 68 65  imply update the
2cb7f 20 74 65 72 6d 0a 2a 2a 20 69 6e 64 65 78 20 77   term.** index w
2cb80 68 65 6e 20 61 20 64 6f 63 75 6d 65 6e 74 20 69  hen a document i
2cb81 73 20 64 65 6c 65 74 65 64 20 6f 72 20 75 70 64  s deleted or upd
2cb82 61 74 65 64 2e 20 20 46 6f 72 20 64 65 6c 65 74  ated.  For delet
2cb83 69 6f 6e 73 2c 20 77 65 0a 2a 2a 20 77 72 69 74  ions, we.** writ
2cb84 65 20 61 6e 20 65 6d 70 74 79 20 64 6f 63 6c 69  e an empty docli
2cb85 73 74 20 28 76 61 72 69 6e 74 28 64 6f 63 69 64  st (varint(docid
2cb86 29 20 76 61 72 69 6e 74 28 50 4f 53 5f 45 4e 44  ) varint(POS_END
2cb87 29 29 2c 20 66 6f 72 20 75 70 64 61 74 65 73 0a  )), for updates.
2cb88 2a 2a 20 77 65 20 73 69 6d 70 6c 79 20 77 72 69  ** we simply wri
2cb89 74 65 20 74 68 65 20 6e 65 77 20 64 6f 63 6c 69  te the new docli
2cb8a 73 74 2e 20 20 53 65 67 6d 65 6e 74 20 6d 65 72  st.  Segment mer
2cb8b 67 65 73 20 6f 76 65 72 77 72 69 74 65 20 6f 6c  ges overwrite ol
2cb8c 64 65 72 0a 2a 2a 20 64 61 74 61 20 66 6f 72 20  der.** data for 
2cb8d 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 6f 63  a particular doc
2cb8e 69 64 20 77 69 74 68 20 6e 65 77 65 72 20 64 61  id with newer da
2cb8f 74 61 2c 20 73 6f 20 64 65 6c 65 74 65 73 20 6f  ta, so deletes o
2cb90 72 20 75 70 64 61 74 65 73 0a 2a 2a 20 77 69 6c  r updates.** wil
2cb91 6c 20 65 76 65 6e 74 75 61 6c 6c 79 20 6f 76 65  l eventually ove
2cb92 72 74 61 6b 65 20 74 68 65 20 65 61 72 6c 69 65  rtake the earlie
2cb93 72 20 64 61 74 61 20 61 6e 64 20 6b 6e 6f 63 6b  r data and knock
2cb94 20 69 74 20 6f 75 74 2e 20 20 54 68 65 0a 2a 2a   it out.  The.**
2cb95 20 71 75 65 72 79 20 6c 6f 67 69 63 20 6c 69 6b   query logic lik
2cb96 65 77 69 73 65 20 6d 65 72 67 65 73 20 64 6f 63  ewise merges doc
2cb97 6c 69 73 74 73 20 73 6f 20 74 68 61 74 20 6e 65  lists so that ne
2cb98 77 65 72 20 64 61 74 61 20 6b 6e 6f 63 6b 73 20  wer data knocks 
2cb99 6f 75 74 0a 2a 2a 20 6f 6c 64 65 72 20 64 61 74  out.** older dat
2cb9a 61 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73 68  a..**.** TODO(sh
2cb9b 65 73 73 29 20 50 72 6f 76 69 64 65 20 61 20 56  ess) Provide a V
2cb9c 41 43 55 55 4d 20 74 79 70 65 20 6f 70 65 72 61  ACUUM type opera
2cb9d 74 69 6f 6e 20 74 6f 20 63 6c 65 61 72 20 6f 75  tion to clear ou
2cb9e 74 20 61 6c 6c 0a 2a 2a 20 64 65 6c 65 74 69 6f  t all.** deletio
2cb9f 6e 73 20 61 6e 64 20 64 75 70 6c 69 63 61 74 69  ns and duplicati
2cba0 6f 6e 73 2e 20 20 54 68 69 73 20 77 6f 75 6c 64  ons.  This would
2cba1 20 62 61 73 69 63 61 6c 6c 79 20 62 65 20 61 20   basically be a 
2cba2 66 6f 72 63 65 64 20 6d 65 72 67 65 0a 2a 2a 20  forced merge.** 
2cba3 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 65  into a single se
2cba4 67 6d 65 6e 74 2e 0a 2a 2f 0a 0a 23 69 66 20 21  gment..*/..#if !
2cba5 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43  defined(SQLITE_C
2cba6 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  ORE) || defined(
2cba7 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
2cba8 53 33 29 0a 0a 23 69 66 20 64 65 66 69 6e 65 64  S3)..#if defined
2cba9 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46  (SQLITE_ENABLE_F
2cbaa 54 53 33 29 20 26 26 20 21 64 65 66 69 6e 65 64  TS3) && !defined
2cbab 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 0a 23 20  (SQLITE_CORE).# 
2cbac 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
2cbad 52 45 20 31 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  RE 1.#endif.../*
2cbae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
2cbaf 63 6c 75 64 65 20 66 74 73 33 5f 68 61 73 68 2e  clude fts3_hash.
2cbb0 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
2cbb1 6f 66 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a  of fts3.c ******
2cbb2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2cbb3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
2cbb4 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 68 61  gin file fts3_ha
2cbb5 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  sh.h ***********
2cbb6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cbb7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2cbb8 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
2cbb9 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 22.**.** The 
2cbba 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
2cbbb 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
2cbbc 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
2cbbd 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
2cbbe 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
2cbbf 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
2cbc0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
2cbc1 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
2cbc2 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
2cbc3 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
2cbc4 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
2cbc5 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
2cbc6 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
2cbc7 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
2cbc8 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
2cbc9 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
2cbca 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
2cbcb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cbcc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cbcd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cbce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cbcf 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ****.** This is 
2cbd0 74 68 65 20 68 65 61 64 65 72 20 66 69 6c 65 20  the header file 
2cbd1 66 6f 72 20 74 68 65 20 67 65 6e 65 72 69 63 20  for the generic 
2cbd2 68 61 73 68 2d 74 61 62 6c 65 20 69 6d 70 6c 65  hash-table imple
2cbd3 6d 65 6e 61 74 69 6f 6e 0a 2a 2a 20 75 73 65 64  menation.** used
2cbd4 20 69 6e 20 53 51 4c 69 74 65 2e 20 20 57 65 27   in SQLite.  We'
2cbd5 76 65 20 6d 6f 64 69 66 69 65 64 20 69 74 20 73  ve modified it s
2cbd6 6c 69 67 68 74 6c 79 20 74 6f 20 73 65 72 76 65  lightly to serve
2cbd7 20 61 73 20 61 20 73 74 61 6e 64 61 6c 6f 6e 65   as a standalone
2cbd8 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20 69  .** hash table i
2cbd9 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f  mplementation fo
2cbda 72 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 20  r the full-text 
2cbdb 69 6e 64 65 78 69 6e 67 20 6d 6f 64 75 6c 65 2e  indexing module.
2cbdc 0a 2a 2a 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f  .**.*/.#ifndef _
2cbdd 46 54 53 33 5f 48 41 53 48 5f 48 5f 0a 23 64 65  FTS3_HASH_H_.#de
2cbde 66 69 6e 65 20 5f 46 54 53 33 5f 48 41 53 48 5f  fine _FTS3_HASH_
2cbdf 48 5f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64  H_../* Forward d
2cbe0 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 73  eclarations of s
2cbe1 74 72 75 63 74 75 72 65 73 2e 20 2a 2f 0a 74 79  tructures. */.ty
2cbe2 70 65 64 65 66 20 73 74 72 75 63 74 20 66 74 73  pedef struct fts
2cbe3 33 48 61 73 68 20 66 74 73 33 48 61 73 68 3b 0a  3Hash fts3Hash;.
2cbe4 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 66  typedef struct f
2cbe5 74 73 33 48 61 73 68 45 6c 65 6d 20 66 74 73 33  ts3HashElem fts3
2cbe6 48 61 73 68 45 6c 65 6d 3b 0a 0a 2f 2a 20 41 20  HashElem;../* A 
2cbe7 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 74 61  complete hash ta
2cbe8 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e  ble is an instan
2cbe9 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
2cbea 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ing structure..*
2cbeb 2a 20 54 68 65 20 69 6e 74 65 72 6e 61 6c 73 20  * The internals 
2cbec 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
2cbed 65 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74  e are intended t
2cbee 6f 20 62 65 20 6f 70 61 71 75 65 20 2d 2d 20 63  o be opaque -- c
2cbef 6c 69 65 6e 74 0a 2a 2a 20 63 6f 64 65 20 73 68  lient.** code sh
2cbf0 6f 75 6c 64 20 6e 6f 74 20 61 74 74 65 6d 70 74  ould not attempt
2cbf1 20 74 6f 20 61 63 63 65 73 73 20 6f 72 20 6d 6f   to access or mo
2cbf2 64 69 66 79 20 74 68 65 20 66 69 65 6c 64 73 20  dify the fields 
2cbf3 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
2cbf4 65 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2e 20 20  e.** directly.  
2cbf5 43 68 61 6e 67 65 20 74 68 69 73 20 73 74 72 75  Change this stru
2cbf6 63 74 75 72 65 20 6f 6e 6c 79 20 62 79 20 75 73  cture only by us
2cbf7 69 6e 67 20 74 68 65 20 72 6f 75 74 69 6e 65 73  ing the routines
2cbf8 20 62 65 6c 6f 77 2e 0a 2a 2a 20 48 6f 77 65 76   below..** Howev
2cbf9 65 72 2c 20 6d 61 6e 79 20 6f 66 20 74 68 65 20  er, many of the 
2cbfa 22 70 72 6f 63 65 64 75 72 65 73 22 20 61 6e 64  "procedures" and
2cbfb 20 22 66 75 6e 63 74 69 6f 6e 73 22 20 66 6f 72   "functions" for
2cbfc 20 6d 6f 64 69 66 79 69 6e 67 20 61 6e 64 0a 2a   modifying and.*
2cbfd 2a 20 61 63 63 65 73 73 69 6e 67 20 74 68 69 73  * accessing this
2cbfe 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 72   structure are r
2cbff 65 61 6c 6c 79 20 6d 61 63 72 6f 73 2c 20 73 6f  eally macros, so
2cc00 20 77 65 20 63 61 6e 27 74 20 72 65 61 6c 6c 79   we can't really
2cc01 20 6d 61 6b 65 0a 2a 2a 20 74 68 69 73 20 73 74   make.** this st
2cc02 72 75 63 74 75 72 65 20 6f 70 61 71 75 65 2e 0a  ructure opaque..
2cc03 2a 2f 0a 73 74 72 75 63 74 20 66 74 73 33 48 61  */.struct fts3Ha
2cc04 73 68 20 7b 0a 20 20 63 68 61 72 20 6b 65 79 43  sh {.  char keyC
2cc05 6c 61 73 73 3b 20 20 20 20 20 20 20 20 20 20 2f  lass;          /
2cc06 2a 20 48 41 53 48 5f 49 4e 54 2c 20 5f 50 4f 49  * HASH_INT, _POI
2cc07 4e 54 45 52 2c 20 5f 53 54 52 49 4e 47 2c 20 5f  NTER, _STRING, _
2cc08 42 49 4e 41 52 59 20 2a 2f 0a 20 20 63 68 61 72  BINARY */.  char
2cc09 20 63 6f 70 79 4b 65 79 3b 20 20 20 20 20 20 20   copyKey;       
2cc0a 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63      /* True if c
2cc0b 6f 70 79 20 6f 66 20 6b 65 79 20 6d 61 64 65 20  opy of key made 
2cc0c 6f 6e 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20 69  on insert */.  i
2cc0d 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20  nt count;       
2cc0e 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
2cc0f 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74   of entries in t
2cc10 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 66  his table */.  f
2cc11 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 66 69 72  ts3HashElem *fir
2cc12 73 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 66 69  st;    /* The fi
2cc13 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74  rst element of t
2cc14 68 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e  he array */.  in
2cc15 74 20 68 74 73 69 7a 65 3b 20 20 20 20 20 20 20  t htsize;       
2cc16 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2cc17 6f 66 20 62 75 63 6b 65 74 73 20 69 6e 20 74 68  of buckets in th
2cc18 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a  e hash table */.
2cc19 20 20 73 74 72 75 63 74 20 5f 66 74 73 33 68 74    struct _fts3ht
2cc1a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65   {        /* the
2cc1b 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20   hash table */. 
2cc1c 20 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20     int count;   
2cc1d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2cc1e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
2cc1f 20 77 69 74 68 20 74 68 69 73 20 68 61 73 68 20   with this hash 
2cc20 2a 2f 0a 20 20 20 20 66 74 73 33 48 61 73 68 45  */.    fts3HashE
2cc21 6c 65 6d 20 2a 63 68 61 69 6e 3b 20 20 20 20 20  lem *chain;     
2cc22 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 69  /* Pointer to fi
2cc23 72 73 74 20 65 6e 74 72 79 20 77 69 74 68 20 74  rst entry with t
2cc24 68 69 73 20 68 61 73 68 20 2a 2f 0a 20 20 7d 20  his hash */.  } 
2cc25 2a 68 74 3b 0a 7d 3b 0a 0a 2f 2a 20 45 61 63 68  *ht;.};../* Each
2cc26 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 20   element in the 
2cc27 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 61 6e  hash table is an
2cc28 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
2cc29 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 73   following .** s
2cc2a 74 72 75 63 74 75 72 65 2e 20 20 41 6c 6c 20 65  tructure.  All e
2cc2b 6c 65 6d 65 6e 74 73 20 61 72 65 20 73 74 6f 72  lements are stor
2cc2c 65 64 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64  ed on a single d
2cc2d 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73  oubly-linked lis
2cc2e 74 2e 0a 2a 2a 0a 2a 2a 20 41 67 61 69 6e 2c 20  t..**.** Again, 
2cc2f 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69  this structure i
2cc30 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65  s intended to be
2cc31 20 6f 70 61 71 75 65 2c 20 62 75 74 20 69 74 20   opaque, but it 
2cc32 63 61 6e 27 74 20 72 65 61 6c 6c 79 0a 2a 2a 20  can't really.** 
2cc33 62 65 20 6f 70 61 71 75 65 20 62 65 63 61 75 73  be opaque becaus
2cc34 65 20 69 74 20 69 73 20 75 73 65 64 20 62 79 20  e it is used by 
2cc35 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 73 74 72 75 63  macros..*/.struc
2cc36 74 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 7b  t fts3HashElem {
2cc37 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20  .  fts3HashElem 
2cc38 2a 6e 65 78 74 2c 20 2a 70 72 65 76 3b 20 2f 2a  *next, *prev; /*
2cc39 20 4e 65 78 74 20 61 6e 64 20 70 72 65 76 69 6f   Next and previo
2cc3a 75 73 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74  us elements in t
2cc3b 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76 6f  he table */.  vo
2cc3c 69 64 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20  id *data;       
2cc3d 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
2cc3e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
2cc3f 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f   this element */
2cc40 0a 20 20 76 6f 69 64 20 2a 70 4b 65 79 3b 20 69  .  void *pKey; i
2cc41 6e 74 20 6e 4b 65 79 3b 20 20 20 20 20 20 2f 2a  nt nKey;      /*
2cc42 20 4b 65 79 20 61 73 73 6f 63 69 61 74 65 64 20   Key associated 
2cc43 77 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65 6e  with this elemen
2cc44 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  t */.};../*.** T
2cc45 68 65 72 65 20 61 72 65 20 32 20 64 69 66 66 65  here are 2 diffe
2cc46 72 65 6e 74 20 6d 6f 64 65 73 20 6f 66 20 6f 70  rent modes of op
2cc47 65 72 61 74 69 6f 6e 20 66 6f 72 20 61 20 68 61  eration for a ha
2cc48 73 68 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20  sh table:.**.** 
2cc49 20 20 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49    FTS3_HASH_STRI
2cc4a 4e 47 20 20 20 20 20 20 20 20 70 4b 65 79 20 70  NG        pKey p
2cc4b 6f 69 6e 74 73 20 74 6f 20 61 20 73 74 72 69 6e  oints to a strin
2cc4c 67 20 74 68 61 74 20 69 73 20 6e 4b 65 79 20 62  g that is nKey b
2cc4d 79 74 65 73 20 6c 6f 6e 67 0a 2a 2a 20 20 20 20  ytes long.**    
2cc4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cc4f 20 20 20 20 20 20 20 28 69 6e 63 6c 75 64 69 6e         (includin
2cc50 67 20 74 68 65 20 6e 75 6c 6c 2d 74 65 72 6d 69  g the null-termi
2cc51 6e 61 74 6f 72 2c 20 69 66 20 61 6e 79 29 2e 20  nator, if any). 
2cc52 20 43 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20   Case.**        
2cc53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cc54 20 20 20 69 73 20 72 65 73 70 65 63 74 65 64 20     is respected 
2cc55 69 6e 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a  in comparisons..
2cc56 2a 2a 0a 2a 2a 20 20 20 46 54 53 33 5f 48 41 53  **.**   FTS3_HAS
2cc57 48 5f 42 49 4e 41 52 59 20 20 20 20 20 20 20 20  H_BINARY        
2cc58 70 4b 65 79 20 70 6f 69 6e 74 73 20 74 6f 20 62  pKey points to b
2cc59 69 6e 61 72 79 20 64 61 74 61 20 6e 4b 65 79 20  inary data nKey 
2cc5a 62 79 74 65 73 20 6c 6f 6e 67 2e 20 0a 2a 2a 20  bytes long. .** 
2cc5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cc5c 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 6d 70            memcmp
2cc5d 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f  () is used to co
2cc5e 6d 70 61 72 65 20 6b 65 79 73 2e 0a 2a 2a 0a 2a  mpare keys..**.*
2cc5f 2a 20 41 20 63 6f 70 79 20 6f 66 20 74 68 65 20  * A copy of the 
2cc60 6b 65 79 20 69 73 20 6d 61 64 65 20 69 66 20 74  key is made if t
2cc61 68 65 20 63 6f 70 79 4b 65 79 20 70 61 72 61 6d  he copyKey param
2cc62 65 74 65 72 20 74 6f 20 66 74 73 33 48 61 73 68  eter to fts3Hash
2cc63 49 6e 69 74 20 69 73 20 31 2e 20 20 0a 2a 2f 0a  Init is 1.  .*/.
2cc64 23 64 65 66 69 6e 65 20 46 54 53 33 5f 48 41 53  #define FTS3_HAS
2cc65 48 5f 53 54 52 49 4e 47 20 20 20 20 31 0a 23 64  H_STRING    1.#d
2cc66 65 66 69 6e 65 20 46 54 53 33 5f 48 41 53 48 5f  efine FTS3_HASH_
2cc67 42 49 4e 41 52 59 20 20 20 20 32 0a 0a 2f 2a 0a  BINARY    2../*.
2cc68 2a 2a 20 41 63 63 65 73 73 20 72 6f 75 74 69 6e  ** Access routin
2cc69 65 73 2e 20 20 54 6f 20 64 65 6c 65 74 65 2c 20  es.  To delete, 
2cc6a 69 6e 73 65 72 74 20 61 20 4e 55 4c 4c 20 70 6f  insert a NULL po
2cc6b 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  inter..*/.SQLITE
2cc6c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2cc6d 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 69  lite3Fts3HashIni
2cc6e 74 28 66 74 73 33 48 61 73 68 2a 2c 20 69 6e 74  t(fts3Hash*, int
2cc6f 20 6b 65 79 74 79 70 65 2c 20 69 6e 74 20 63 6f   keytype, int co
2cc70 70 79 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f 50  pyKey);.SQLITE_P
2cc71 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
2cc72 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 73 65  ite3Fts3HashInse
2cc73 72 74 28 66 74 73 33 48 61 73 68 2a 2c 20 63 6f  rt(fts3Hash*, co
2cc74 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
2cc75 69 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 64 20 2a  int nKey, void *
2cc76 70 44 61 74 61 29 3b 0a 53 51 4c 49 54 45 5f 50  pData);.SQLITE_P
2cc77 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
2cc78 69 74 65 33 46 74 73 33 48 61 73 68 46 69 6e 64  ite3Fts3HashFind
2cc79 28 63 6f 6e 73 74 20 66 74 73 33 48 61 73 68 2a  (const fts3Hash*
2cc7a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b  , const void *pK
2cc7b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 3b 0a 53  ey, int nKey);.S
2cc7c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2cc7d 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 48 61  id sqlite3Fts3Ha
2cc7e 73 68 43 6c 65 61 72 28 66 74 73 33 48 61 73 68  shClear(fts3Hash
2cc7f 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 68 6f 72 74  *);../*.** Short
2cc80 68 61 6e 64 20 66 6f 72 20 74 68 65 20 66 75 6e  hand for the fun
2cc81 63 74 69 6f 6e 73 20 61 62 6f 76 65 0a 2a 2f 0a  ctions above.*/.
2cc82 23 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68  #define fts3Hash
2cc83 49 6e 69 74 20 20 20 73 71 6c 69 74 65 33 46 74  Init   sqlite3Ft
2cc84 73 33 48 61 73 68 49 6e 69 74 0a 23 64 65 66 69  s3HashInit.#defi
2cc85 6e 65 20 66 74 73 33 48 61 73 68 49 6e 73 65 72  ne fts3HashInser
2cc86 74 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73  t sqlite3Fts3Has
2cc87 68 49 6e 73 65 72 74 0a 23 64 65 66 69 6e 65 20  hInsert.#define 
2cc88 66 74 73 33 48 61 73 68 46 69 6e 64 20 20 20 73  fts3HashFind   s
2cc89 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 46 69  qlite3Fts3HashFi
2cc8a 6e 64 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48  nd.#define fts3H
2cc8b 61 73 68 43 6c 65 61 72 20 20 73 71 6c 69 74 65  ashClear  sqlite
2cc8c 33 46 74 73 33 48 61 73 68 43 6c 65 61 72 0a 0a  3Fts3HashClear..
2cc8d 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72  /*.** Macros for
2cc8e 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c   looping over al
2cc8f 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 61 20  l elements of a 
2cc90 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68 65  hash table.  The
2cc91 20 69 64 69 6f 6d 20 69 73 0a 2a 2a 20 6c 69 6b   idiom is.** lik
2cc92 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
2cc93 66 74 73 33 48 61 73 68 20 68 3b 0a 2a 2a 20 20  fts3Hash h;.**  
2cc94 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 70   fts3HashElem *p
2cc95 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20  ;.**   ....**   
2cc96 66 6f 72 28 70 3d 66 74 73 33 48 61 73 68 46 69  for(p=fts3HashFi
2cc97 72 73 74 28 26 68 29 3b 20 70 3b 20 70 3d 66 74  rst(&h); p; p=ft
2cc98 73 33 48 61 73 68 4e 65 78 74 28 70 29 29 7b 0a  s3HashNext(p)){.
2cc99 2a 2a 20 20 20 20 20 53 6f 6d 65 53 74 72 75 63  **     SomeStruc
2cc9a 74 75 72 65 20 2a 70 44 61 74 61 20 3d 20 66 74  ture *pData = ft
2cc9b 73 33 48 61 73 68 44 61 74 61 28 70 29 3b 0a 2a  s3HashData(p);.*
2cc9c 2a 20 20 20 20 20 2f 2f 20 64 6f 20 73 6f 6d 65  *     // do some
2cc9d 74 68 69 6e 67 20 77 69 74 68 20 70 44 61 74 61  thing with pData
2cc9e 0a 2a 2a 20 20 20 7d 0a 2a 2f 0a 23 64 65 66 69  .**   }.*/.#defi
2cc9f 6e 65 20 66 74 73 33 48 61 73 68 46 69 72 73 74  ne fts3HashFirst
2cca0 28 48 29 20 20 28 28 48 29 2d 3e 66 69 72 73 74  (H)  ((H)->first
2cca1 29 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48 61  ).#define fts3Ha
2cca2 73 68 4e 65 78 74 28 45 29 20 20 20 28 28 45 29  shNext(E)   ((E)
2cca3 2d 3e 6e 65 78 74 29 0a 23 64 65 66 69 6e 65 20  ->next).#define 
2cca4 66 74 73 33 48 61 73 68 44 61 74 61 28 45 29 20  fts3HashData(E) 
2cca5 20 20 28 28 45 29 2d 3e 64 61 74 61 29 0a 23 64    ((E)->data).#d
2cca6 65 66 69 6e 65 20 66 74 73 33 48 61 73 68 4b 65  efine fts3HashKe
2cca7 79 28 45 29 20 20 20 20 28 28 45 29 2d 3e 70 4b  y(E)    ((E)->pK
2cca8 65 79 29 0a 23 64 65 66 69 6e 65 20 66 74 73 33  ey).#define fts3
2cca9 48 61 73 68 4b 65 79 73 69 7a 65 28 45 29 20 28  HashKeysize(E) (
2ccaa 28 45 29 2d 3e 6e 4b 65 79 29 0a 0a 2f 2a 0a 2a  (E)->nKey)../*.*
2ccab 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
2ccac 69 65 73 20 69 6e 20 61 20 68 61 73 68 20 74 61  ies in a hash ta
2ccad 62 6c 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66  ble.*/.#define f
2ccae 74 73 33 48 61 73 68 43 6f 75 6e 74 28 48 29 20  ts3HashCount(H) 
2ccaf 20 28 28 48 29 2d 3e 63 6f 75 6e 74 29 0a 0a 23   ((H)->count)..#
2ccb0 65 6e 64 69 66 20 2f 2a 20 5f 46 54 53 33 5f 48  endif /* _FTS3_H
2ccb1 41 53 48 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  ASH_H_ */../****
2ccb2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
2ccb3 66 20 66 74 73 33 5f 68 61 73 68 2e 68 20 2a 2a  f fts3_hash.h **
2ccb4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ccb5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ccb6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
2ccb7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
2ccb8 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
2ccb9 65 66 74 20 6f 66 66 20 69 6e 20 66 74 73 33 2e  eft off in fts3.
2ccba 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2ccbb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
2ccbc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
2ccbd 64 65 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65  de fts3_tokenize
2ccbe 72 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  r.h in the middl
2ccbf 65 20 6f 66 20 66 74 73 33 2e 63 20 2a 2a 2a 2a  e of fts3.c ****
2ccc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
2ccc1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
2ccc2 20 66 69 6c 65 20 66 74 73 33 5f 74 6f 6b 65 6e   file fts3_token
2ccc3 69 7a 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  izer.h *********
2ccc4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ccc5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
2ccc6 20 32 30 30 36 20 4a 75 6c 79 20 31 30 0a 2a 2a   2006 July 10.**
2ccc7 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
2ccc8 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
2ccc9 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
2ccca 65 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  e code..**.*****
2cccb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cccc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cccd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ccce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cccf 2a 2a 2a 2a 0a 2a 2a 20 44 65 66 69 6e 65 73 20  ****.** Defines 
2ccd0 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f  the interface to
2ccd1 20 74 6f 6b 65 6e 69 7a 65 72 73 20 75 73 65 64   tokenizers used
2ccd2 20 62 79 20 66 75 6c 6c 74 65 78 74 2d 73 65 61   by fulltext-sea
2ccd3 72 63 68 2e 20 20 54 68 65 72 65 0a 2a 2a 20 61  rch.  There.** a
2ccd4 72 65 20 74 68 72 65 65 20 62 61 73 69 63 20 63  re three basic c
2ccd5 6f 6d 70 6f 6e 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a  omponents:.**.**
2ccd6 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
2ccd7 65 72 5f 6d 6f 64 75 6c 65 20 69 73 20 61 20 73  er_module is a s
2ccd8 69 6e 67 6c 65 74 6f 6e 20 64 65 66 69 6e 69 6e  ingleton definin
2ccd9 67 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 0a  g the tokenizer.
2ccda 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 66 75 6e  ** interface fun
2ccdb 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 69 73  ctions.  This is
2ccdc 20 65 73 73 65 6e 74 69 61 6c 6c 79 20 74 68 65   essentially the
2ccdd 20 63 6c 61 73 73 20 73 74 72 75 63 74 75 72 65   class structure
2ccde 20 66 6f 72 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 65   for.** tokenize
2ccdf 72 73 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65  rs..**.** sqlite
2cce0 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 69 73 20 75  3_tokenizer is u
2cce1 73 65 64 20 74 6f 20 64 65 66 69 6e 65 20 61 20  sed to define a 
2cce2 70 61 72 74 69 63 75 6c 61 72 20 74 6f 6b 65 6e  particular token
2cce3 69 7a 65 72 2c 20 70 65 72 68 61 70 73 0a 2a 2a  izer, perhaps.**
2cce4 20 69 6e 63 6c 75 64 69 6e 67 20 63 75 73 74 6f   including custo
2cce5 6d 69 7a 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61  mization informa
2cce6 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 61 74 20  tion defined at 
2cce7 63 72 65 61 74 69 6f 6e 20 74 69 6d 65 2e 0a 2a  creation time..*
2cce8 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b  *.** sqlite3_tok
2cce9 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 69 73  enizer_cursor is
2ccea 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 61 20   generated by a 
2cceb 74 6f 6b 65 6e 69 7a 65 72 20 74 6f 20 67 65 6e  tokenizer to gen
2ccec 65 72 61 74 65 0a 2a 2a 20 74 6f 6b 65 6e 73 20  erate.** tokens 
2cced 66 72 6f 6d 20 61 20 70 61 72 74 69 63 75 6c 61  from a particula
2ccee 72 20 69 6e 70 75 74 2e 0a 2a 2f 0a 23 69 66 6e  r input..*/.#ifn
2ccef 64 65 66 20 5f 46 54 53 33 5f 54 4f 4b 45 4e 49  def _FTS3_TOKENI
2ccf0 5a 45 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f  ZER_H_.#define _
2ccf1 46 54 53 33 5f 54 4f 4b 45 4e 49 5a 45 52 5f 48  FTS3_TOKENIZER_H
2ccf2 5f 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  _../* TODO(shess
2ccf3 29 20 4f 6e 6c 79 20 75 73 65 64 20 66 6f 72 20  ) Only used for 
2ccf4 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 53 51  SQLITE_OK and SQ
2ccf5 4c 49 54 45 5f 44 4f 4e 45 20 61 74 20 74 68 69  LITE_DONE at thi
2ccf6 73 20 74 69 6d 65 2e 0a 2a 2a 20 49 66 20 74 6f  s time..** If to
2ccf7 6b 65 6e 69 7a 65 72 73 20 61 72 65 20 74 6f 20  kenizers are to 
2ccf8 62 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 63 61  be allowed to ca
2ccf9 6c 6c 20 73 71 6c 69 74 65 33 5f 2a 28 29 20 66  ll sqlite3_*() f
2ccfa 75 6e 63 74 69 6f 6e 73 2c 20 74 68 65 6e 0a 2a  unctions, then.*
2ccfb 2a 20 77 65 20 77 69 6c 6c 20 6e 65 65 64 20 61  * we will need a
2ccfc 20 77 61 79 20 74 6f 20 72 65 67 69 73 74 65 72   way to register
2ccfd 20 74 68 65 20 41 50 49 20 63 6f 6e 73 69 73 74   the API consist
2ccfe 65 6e 74 6c 79 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ently..*/../*.**
2ccff 20 53 74 72 75 63 74 75 72 65 73 20 75 73 65 64   Structures used
2cd00 20 62 79 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65   by the tokenize
2cd01 72 20 69 6e 74 65 72 66 61 63 65 2e 20 57 68 65  r interface. Whe
2cd02 6e 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a 65  n a new tokenize
2cd03 72 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  r.** implementat
2cd04 69 6f 6e 20 69 73 20 72 65 67 69 73 74 65 72 65  ion is registere
2cd05 64 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 70 72  d, the caller pr
2cd06 6f 76 69 64 65 73 20 61 20 70 6f 69 6e 74 65 72  ovides a pointer
2cd07 20 74 6f 0a 2a 2a 20 61 6e 20 73 71 6c 69 74 65   to.** an sqlite
2cd08 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
2cd09 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 6f  le containing po
2cd0a 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 63 61  inters to the ca
2cd0b 6c 6c 62 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69  llback.** functi
2cd0c 6f 6e 73 20 74 68 61 74 20 6d 61 6b 65 20 75 70  ons that make up
2cd0d 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69   an implementati
2cd0e 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61  on..**.** When a
2cd0f 6e 20 66 74 73 33 20 74 61 62 6c 65 20 69 73 20  n fts3 table is 
2cd10 63 72 65 61 74 65 64 2c 20 69 74 20 70 61 73 73  created, it pass
2cd11 65 73 20 61 6e 79 20 61 72 67 75 6d 65 6e 74 73  es any arguments
2cd12 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20 74 68   passed to.** th
2cd13 65 20 74 6f 6b 65 6e 69 7a 65 72 20 63 6c 61 75  e tokenizer clau
2cd14 73 65 20 6f 66 20 74 68 65 20 43 52 45 41 54 45  se of the CREATE
2cd15 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73   VIRTUAL TABLE s
2cd16 74 61 74 65 6d 65 6e 74 20 74 6f 20 74 68 65 0a  tatement to the.
2cd17 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  ** sqlite3_token
2cd18 69 7a 65 72 5f 6d 6f 64 75 6c 65 2e 78 43 72 65  izer_module.xCre
2cd19 61 74 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 6f  ate() function o
2cd1a 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  f the requested 
2cd1b 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20 69 6d 70  tokenizer.** imp
2cd1c 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 54 68 65  lementation. The
2cd1d 20 78 43 72 65 61 74 65 28 29 20 66 75 6e 63 74   xCreate() funct
2cd1e 69 6f 6e 20 69 6e 20 74 75 72 6e 20 72 65 74 75  ion in turn retu
2cd1f 72 6e 73 20 61 6e 20 0a 2a 2a 20 73 71 6c 69 74  rns an .** sqlit
2cd20 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 73 74 72  e3_tokenizer str
2cd21 75 63 74 75 72 65 20 72 65 70 72 65 73 65 6e 74  ucture represent
2cd22 69 6e 67 20 74 68 65 20 73 70 65 63 69 66 69 63  ing the specific
2cd23 20 74 6f 6b 65 6e 69 7a 65 72 20 74 6f 0a 2a 2a   tokenizer to.**
2cd24 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65   be used for the
2cd25 20 66 74 73 33 20 74 61 62 6c 65 20 28 63 75 73   fts3 table (cus
2cd26 74 6f 6d 69 7a 65 64 20 62 79 20 74 68 65 20 74  tomized by the t
2cd27 6f 6b 65 6e 69 7a 65 72 20 63 6c 61 75 73 65 20  okenizer clause 
2cd28 61 72 67 75 6d 65 6e 74 73 29 2e 0a 2a 2a 0a 2a  arguments)..**.*
2cd29 2a 20 54 6f 20 74 6f 6b 65 6e 69 7a 65 20 61 6e  * To tokenize an
2cd2a 20 69 6e 70 75 74 20 62 75 66 66 65 72 2c 20 74   input buffer, t
2cd2b 68 65 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  he sqlite3_token
2cd2c 69 7a 65 72 5f 6d 6f 64 75 6c 65 2e 78 4f 70 65  izer_module.xOpe
2cd2d 6e 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 69 73  n().** method is
2cd2e 20 63 61 6c 6c 65 64 2e 20 49 74 20 72 65 74 75   called. It retu
2cd2f 72 6e 73 20 61 6e 20 73 71 6c 69 74 65 33 5f 74  rns an sqlite3_t
2cd30 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
2cd31 6f 62 6a 65 63 74 0a 2a 2a 20 74 68 61 74 20 6d  object.** that m
2cd32 61 79 20 62 65 20 75 73 65 64 20 74 6f 20 74 6f  ay be used to to
2cd33 6b 65 6e 69 7a 65 20 61 20 73 70 65 63 69 66 69  kenize a specifi
2cd34 63 20 69 6e 70 75 74 20 62 75 66 66 65 72 20 62  c input buffer b
2cd35 61 73 65 64 20 6f 6e 0a 2a 2a 20 74 68 65 20 74  ased on.** the t
2cd36 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 72 75 6c 65  okenization rule
2cd37 73 20 73 75 70 70 6c 69 65 64 20 62 79 20 61 20  s supplied by a 
2cd38 73 70 65 63 69 66 69 63 20 73 71 6c 69 74 65 33  specific sqlite3
2cd39 5f 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20 6f 62  _tokenizer.** ob
2cd3a 6a 65 63 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ject..*/.typedef
2cd3b 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
2cd3c 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
2cd3d 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
2cd3e 65 72 5f 6d 6f 64 75 6c 65 3b 0a 74 79 70 65 64  er_module;.typed
2cd3f 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
2cd40 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 73 71 6c 69  3_tokenizer sqli
2cd41 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 3b 0a 74  te3_tokenizer;.t
2cd42 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
2cd43 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
2cd44 63 75 72 73 6f 72 20 73 71 6c 69 74 65 33 5f 74  cursor sqlite3_t
2cd45 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 3b  okenizer_cursor;
2cd46 0a 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  ..struct sqlite3
2cd47 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
2cd48 65 20 7b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53  e {..  /*.  ** S
2cd49 74 72 75 63 74 75 72 65 20 76 65 72 73 69 6f 6e  tructure version
2cd4a 2e 20 53 68 6f 75 6c 64 20 61 6c 77 61 79 73 20  . Should always 
2cd4b 62 65 20 73 65 74 20 74 6f 20 30 2e 0a 20 20 2a  be set to 0..  *
2cd4c 2f 0a 20 20 69 6e 74 20 69 56 65 72 73 69 6f 6e  /.  int iVersion
2cd4d 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 72 65  ;..  /*.  ** Cre
2cd4e 61 74 65 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69  ate a new tokeni
2cd4f 7a 65 72 2e 20 54 68 65 20 76 61 6c 75 65 73 20  zer. The values 
2cd50 69 6e 20 74 68 65 20 61 72 67 76 5b 5d 20 61 72  in the argv[] ar
2cd51 72 61 79 20 61 72 65 20 74 68 65 0a 20 20 2a 2a  ray are the.  **
2cd52 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73 65   arguments passe
2cd53 64 20 74 6f 20 74 68 65 20 22 74 6f 6b 65 6e 69  d to the "tokeni
2cd54 7a 65 72 22 20 63 6c 61 75 73 65 20 6f 66 20 74  zer" clause of t
2cd55 68 65 20 43 52 45 41 54 45 20 56 49 52 54 55 41  he CREATE VIRTUA
2cd56 4c 0a 20 20 2a 2a 20 54 41 42 4c 45 20 73 74 61  L.  ** TABLE sta
2cd57 74 65 6d 65 6e 74 20 74 68 61 74 20 63 72 65 61  tement that crea
2cd58 74 65 64 20 74 68 65 20 66 74 73 33 20 74 61 62  ted the fts3 tab
2cd59 6c 65 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  le. For example,
2cd5a 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 66 6f 6c   if.  ** the fol
2cd5b 6c 6f 77 69 6e 67 20 53 51 4c 20 69 73 20 65 78  lowing SQL is ex
2cd5c 65 63 75 74 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a  ecuted:.  **.  *
2cd5d 2a 20 20 20 43 52 45 41 54 45 20 2e 2e 20 55 53  *   CREATE .. US
2cd5e 49 4e 47 20 66 74 73 33 28 20 2e 2e 2e 20 2c 20  ING fts3( ... , 
2cd5f 74 6f 6b 65 6e 69 7a 65 72 20 3c 74 6f 6b 65 6e  tokenizer <token
2cd60 69 7a 65 72 2d 6e 61 6d 65 3e 20 61 72 67 31 20  izer-name> arg1 
2cd61 61 72 67 32 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20  arg2).  **.  ** 
2cd62 74 68 65 6e 20 61 72 67 63 20 69 73 20 73 65 74  then argc is set
2cd63 20 74 6f 20 32 2c 20 61 6e 64 20 74 68 65 20 61   to 2, and the a
2cd64 72 67 76 5b 5d 20 61 72 72 61 79 20 63 6f 6e 74  rgv[] array cont
2cd65 61 69 6e 73 20 70 6f 69 6e 74 65 72 73 0a 20 20  ains pointers.  
2cd66 2a 2a 20 74 6f 20 74 68 65 20 73 74 72 69 6e 67  ** to the string
2cd67 73 20 22 61 72 67 31 22 20 61 6e 64 20 22 61 72  s "arg1" and "ar
2cd68 67 32 22 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  g2"..  **.  ** T
2cd69 68 69 73 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c  his method shoul
2cd6a 64 20 72 65 74 75 72 6e 20 65 69 74 68 65 72 20  d return either 
2cd6b 53 51 4c 49 54 45 5f 4f 4b 20 28 30 29 2c 20 6f  SQLITE_OK (0), o
2cd6c 72 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f  r an SQLite erro
2cd6d 72 20 0a 20 20 2a 2a 20 63 6f 64 65 2e 20 49 66  r .  ** code. If
2cd6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
2cd6f 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 2a 70 70  turned, then *pp
2cd70 54 6f 6b 65 6e 69 7a 65 72 20 73 68 6f 75 6c 64  Tokenizer should
2cd71 20 62 65 20 73 65 74 0a 20 20 2a 2a 20 74 6f 20   be set.  ** to 
2cd72 70 6f 69 6e 74 20 61 74 20 74 68 65 20 6e 65 77  point at the new
2cd73 6c 79 20 63 72 65 61 74 65 64 20 74 6f 6b 65 6e  ly created token
2cd74 69 7a 65 72 20 73 74 72 75 63 74 75 72 65 2e 20  izer structure. 
2cd75 54 68 65 20 67 65 6e 65 72 69 63 0a 20 20 2a 2a  The generic.  **
2cd76 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
2cd77 65 72 2e 70 4d 6f 64 75 6c 65 20 76 61 72 69 61  er.pModule varia
2cd78 62 6c 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  ble should not b
2cd79 65 20 69 6e 69 74 69 61 6c 69 73 65 64 20 62 79  e initialised by
2cd7a 0a 20 20 2a 2a 20 74 68 69 73 20 63 61 6c 6c 62  .  ** this callb
2cd7b 61 63 6b 2e 20 54 68 65 20 63 61 6c 6c 65 72 20  ack. The caller 
2cd7c 77 69 6c 6c 20 64 6f 20 73 6f 2e 0a 20 20 2a 2f  will do so..  */
2cd7d 0a 20 20 69 6e 74 20 28 2a 78 43 72 65 61 74 65  .  int (*xCreate
2cd7e 29 28 0a 20 20 20 20 69 6e 74 20 61 72 67 63 2c  )(.    int argc,
2cd7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cd80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
2cd81 7a 65 20 6f 66 20 61 72 67 76 20 61 72 72 61 79  ze of argv array
2cd82 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68   */.    const ch
2cd83 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 20  ar *const*argv, 
2cd84 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2cd85 6f 6b 65 6e 69 7a 65 72 20 61 72 67 75 6d 65 6e  okenizer argumen
2cd86 74 20 73 74 72 69 6e 67 73 20 2a 2f 0a 20 20 20  t strings */.   
2cd87 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
2cd88 65 72 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65 72  er **ppTokenizer
2cd89 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 43 72 65       /* OUT: Cre
2cd8a 61 74 65 64 20 74 6f 6b 65 6e 69 7a 65 72 20 2a  ated tokenizer *
2cd8b 2f 0a 20 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  /.  );..  /*.  *
2cd8c 2a 20 44 65 73 74 72 6f 79 20 61 6e 20 65 78 69  * Destroy an exi
2cd8d 73 74 69 6e 67 20 74 6f 6b 65 6e 69 7a 65 72 2e  sting tokenizer.
2cd8e 20 54 68 65 20 66 74 73 33 20 6d 6f 64 75 6c 65   The fts3 module
2cd8f 20 63 61 6c 6c 73 20 74 68 69 73 20 6d 65 74 68   calls this meth
2cd90 6f 64 0a 20 20 2a 2a 20 65 78 61 63 74 6c 79 20  od.  ** exactly 
2cd91 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 73 75  once for each su
2cd92 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f  ccessful call to
2cd93 20 78 43 72 65 61 74 65 28 29 2e 0a 20 20 2a 2f   xCreate()..  */
2cd94 0a 20 20 69 6e 74 20 28 2a 78 44 65 73 74 72 6f  .  int (*xDestro
2cd95 79 29 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  y)(sqlite3_token
2cd96 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72  izer *pTokenizer
2cd97 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 72  );..  /*.  ** Cr
2cd98 65 61 74 65 20 61 20 74 6f 6b 65 6e 69 7a 65 72  eate a tokenizer
2cd99 20 63 75 72 73 6f 72 20 74 6f 20 74 6f 6b 65 6e   cursor to token
2cd9a 69 7a 65 20 61 6e 20 69 6e 70 75 74 20 62 75 66  ize an input buf
2cd9b 66 65 72 2e 20 54 68 65 20 63 61 6c 6c 65 72 0a  fer. The caller.
2cd9c 20 20 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69    ** is responsi
2cd9d 62 6c 65 20 66 6f 72 20 65 6e 73 75 72 69 6e 67  ble for ensuring
2cd9e 20 74 68 61 74 20 74 68 65 20 69 6e 70 75 74 20   that the input 
2cd9f 62 75 66 66 65 72 20 72 65 6d 61 69 6e 73 20 76  buffer remains v
2cda0 61 6c 69 64 0a 20 20 2a 2a 20 75 6e 74 69 6c 20  alid.  ** until 
2cda1 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 63 6c  the cursor is cl
2cda2 6f 73 65 64 20 28 75 73 69 6e 67 20 74 68 65 20  osed (using the 
2cda3 78 43 6c 6f 73 65 28 29 20 6d 65 74 68 6f 64 29  xClose() method)
2cda4 2e 20 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  . .  */.  int (*
2cda5 78 4f 70 65 6e 29 28 0a 20 20 20 20 73 71 6c 69  xOpen)(.    sqli
2cda6 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70  te3_tokenizer *p
2cda7 54 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20 20  Tokenizer,      
2cda8 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 6f 62   /* Tokenizer ob
2cda9 6a 65 63 74 20 2a 2f 0a 20 20 20 20 63 6f 6e 73  ject */.    cons
2cdaa 74 20 63 68 61 72 20 2a 70 49 6e 70 75 74 2c 20  t char *pInput, 
2cdab 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20  int nBytes,     
2cdac 20 2f 2a 20 49 6e 70 75 74 20 62 75 66 66 65 72   /* Input buffer
2cdad 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   */.    sqlite3_
2cdae 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
2cdaf 20 2a 2a 70 70 43 75 72 73 6f 72 20 20 2f 2a 20   **ppCursor  /* 
2cdb0 4f 55 54 3a 20 43 72 65 61 74 65 64 20 74 6f 6b  OUT: Created tok
2cdb1 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 20 2a 2f  enizer cursor */
2cdb2 0a 20 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  .  );..  /*.  **
2cdb3 20 44 65 73 74 72 6f 79 20 61 6e 20 65 78 69 73   Destroy an exis
2cdb4 74 69 6e 67 20 74 6f 6b 65 6e 69 7a 65 72 20 63  ting tokenizer c
2cdb5 75 72 73 6f 72 2e 20 54 68 65 20 66 74 73 33 20  ursor. The fts3 
2cdb6 6d 6f 64 75 6c 65 20 63 61 6c 6c 73 20 74 68 69  module calls thi
2cdb7 73 20 0a 20 20 2a 2a 20 6d 65 74 68 6f 64 20 65  s .  ** method e
2cdb8 78 61 63 74 6c 79 20 6f 6e 63 65 20 66 6f 72 20  xactly once for 
2cdb9 65 61 63 68 20 73 75 63 63 65 73 73 66 75 6c 20  each successful 
2cdba 63 61 6c 6c 20 74 6f 20 78 4f 70 65 6e 28 29 2e  call to xOpen().
2cdbb 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43  .  */.  int (*xC
2cdbc 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 74 6f  lose)(sqlite3_to
2cdbd 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
2cdbe 70 43 75 72 73 6f 72 29 3b 0a 0a 20 20 2f 2a 0a  pCursor);..  /*.
2cdbf 20 20 2a 2a 20 52 65 74 72 69 65 76 65 20 74 68    ** Retrieve th
2cdc0 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f  e next token fro
2cdc1 6d 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20  m the tokenizer 
2cdc2 63 75 72 73 6f 72 20 70 43 75 72 73 6f 72 2e 20  cursor pCursor. 
2cdc3 54 68 69 73 0a 20 20 2a 2a 20 6d 65 74 68 6f 64  This.  ** method
2cdc4 20 73 68 6f 75 6c 64 20 65 69 74 68 65 72 20 72   should either r
2cdc5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
2cdc6 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75  and set the valu
2cdc7 65 73 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 22  es of the.  ** "
2cdc8 4f 55 54 22 20 76 61 72 69 61 62 6c 65 73 20 69  OUT" variables i
2cdc9 64 65 6e 74 69 66 69 65 64 20 62 65 6c 6f 77 2c  dentified below,
2cdca 20 6f 72 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20   or SQLITE_DONE 
2cdcb 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74  to indicate that
2cdcc 0a 20 20 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66  .  ** the end of
2cdcd 20 74 68 65 20 62 75 66 66 65 72 20 68 61 73 20   the buffer has 
2cdce 62 65 65 6e 20 72 65 61 63 68 65 64 2c 20 6f 72  been reached, or
2cdcf 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
2cdd0 20 63 6f 64 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a   code..  **.  **
2cdd1 20 2a 70 70 54 6f 6b 65 6e 20 73 68 6f 75 6c 64   *ppToken should
2cdd2 20 62 65 20 73 65 74 20 74 6f 20 70 6f 69 6e 74   be set to point
2cdd3 20 61 74 20 61 20 62 75 66 66 65 72 20 63 6f 6e   at a buffer con
2cdd4 74 61 69 6e 69 6e 67 20 74 68 65 20 0a 20 20 2a  taining the .  *
2cdd5 2a 20 6e 6f 72 6d 61 6c 69 7a 65 64 20 76 65 72  * normalized ver
2cdd6 73 69 6f 6e 20 6f 66 20 74 68 65 20 74 6f 6b 65  sion of the toke
2cdd7 6e 20 28 69 2e 65 2e 20 61 66 74 65 72 20 61 6e  n (i.e. after an
2cdd8 79 20 63 61 73 65 2d 66 6f 6c 64 69 6e 67 20 61  y case-folding a
2cdd9 6e 64 2f 6f 72 0a 20 20 2a 2a 20 73 74 65 6d 6d  nd/or.  ** stemm
2cdda 69 6e 67 20 68 61 73 20 62 65 65 6e 20 70 65 72  ing has been per
2cddb 66 6f 72 6d 65 64 29 2e 20 2a 70 6e 42 79 74 65  formed). *pnByte
2cddc 73 20 73 68 6f 75 6c 64 20 62 65 20 73 65 74 20  s should be set 
2cddd 74 6f 20 74 68 65 20 6c 65 6e 67 74 68 0a 20 20  to the length.  
2cdde 2a 2a 20 6f 66 20 74 68 69 73 20 62 75 66 66 65  ** of this buffe
2cddf 72 20 69 6e 20 62 79 74 65 73 2e 20 54 68 65 20  r in bytes. The 
2cde0 69 6e 70 75 74 20 74 65 78 74 20 74 68 61 74 20  input text that 
2cde1 67 65 6e 65 72 61 74 65 64 20 74 68 65 20 74 6f  generated the to
2cde2 6b 65 6e 20 69 73 0a 20 20 2a 2a 20 69 64 65 6e  ken is.  ** iden
2cde3 74 69 66 69 65 64 20 62 79 20 74 68 65 20 62 79  tified by the by
2cde4 74 65 20 6f 66 66 73 65 74 73 20 72 65 74 75 72  te offsets retur
2cde5 6e 65 64 20 69 6e 20 2a 70 69 53 74 61 72 74 4f  ned in *piStartO
2cde6 66 66 73 65 74 20 61 6e 64 0a 20 20 2a 2a 20 2a  ffset and.  ** *
2cde7 70 69 45 6e 64 4f 66 66 73 65 74 2e 0a 20 20 2a  piEndOffset..  *
2cde8 2a 0a 20 20 2a 2a 20 54 68 65 20 62 75 66 66 65  *.  ** The buffe
2cde9 72 20 2a 70 70 54 6f 6b 65 6e 20 69 73 20 73 65  r *ppToken is se
2cdea 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 69 73  t to point at is
2cdeb 20 6d 61 6e 61 67 65 64 20 62 79 20 74 68 65 20   managed by the 
2cdec 74 6f 6b 65 6e 69 7a 65 72 0a 20 20 2a 2a 20 69  tokenizer.  ** i
2cded 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 49  mplementation. I
2cdee 74 20 69 73 20 6f 6e 6c 79 20 72 65 71 75 69 72  t is only requir
2cdef 65 64 20 74 6f 20 62 65 20 76 61 6c 69 64 20 75  ed to be valid u
2cdf0 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 20 63 61  ntil the next ca
2cdf1 6c 6c 0a 20 20 2a 2a 20 74 6f 20 78 4e 65 78 74  ll.  ** to xNext
2cdf2 28 29 20 6f 72 20 78 43 6c 6f 73 65 28 29 2e 20  () or xClose(). 
2cdf3 0a 20 20 2a 2f 0a 20 20 2f 2a 20 54 4f 44 4f 28  .  */.  /* TODO(
2cdf4 73 68 65 73 73 29 20 63 75 72 72 65 6e 74 20 69  shess) current i
2cdf5 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 72 65  mplementation re
2cdf6 71 75 69 72 65 73 20 70 49 6e 70 75 74 20 74 6f  quires pInput to
2cdf7 20 62 65 0a 20 20 2a 2a 20 6e 75 6c 2d 74 65 72   be.  ** nul-ter
2cdf8 6d 69 6e 61 74 65 64 2e 20 20 54 68 69 73 20 73  minated.  This s
2cdf9 68 6f 75 6c 64 20 65 69 74 68 65 72 20 62 65 20  hould either be 
2cdfa 66 69 78 65 64 2c 20 6f 72 20 70 49 6e 70 75 74  fixed, or pInput
2cdfb 2f 6e 42 79 74 65 73 0a 20 20 2a 2a 20 73 68 6f  /nBytes.  ** sho
2cdfc 75 6c 64 20 62 65 20 63 6f 6e 76 65 72 74 65 64  uld be converted
2cdfd 20 74 6f 20 7a 49 6e 70 75 74 2e 0a 20 20 2a 2f   to zInput..  */
2cdfe 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78 74 29 28  .  int (*xNext)(
2cdff 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  .    sqlite3_tok
2ce00 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70  enizer_cursor *p
2ce01 43 75 72 73 6f 72 2c 20 20 20 2f 2a 20 54 6f 6b  Cursor,   /* Tok
2ce02 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 20 2a 2f  enizer cursor */
2ce03 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
2ce04 2a 2a 70 70 54 6f 6b 65 6e 2c 20 69 6e 74 20 2a  **ppToken, int *
2ce05 70 6e 42 79 74 65 73 2c 20 20 2f 2a 20 4f 55 54  pnBytes,  /* OUT
2ce06 3a 20 4e 6f 72 6d 61 6c 69 7a 65 64 20 74 65 78  : Normalized tex
2ce07 74 20 66 6f 72 20 74 6f 6b 65 6e 20 2a 2f 0a 20  t for token */. 
2ce08 20 20 20 69 6e 74 20 2a 70 69 53 74 61 72 74 4f     int *piStartO
2ce09 66 66 73 65 74 2c 20 20 2f 2a 20 4f 55 54 3a 20  ffset,  /* OUT: 
2ce0a 42 79 74 65 20 6f 66 66 73 65 74 20 6f 66 20 74  Byte offset of t
2ce0b 6f 6b 65 6e 20 69 6e 20 69 6e 70 75 74 20 62 75  oken in input bu
2ce0c 66 66 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ffer */.    int 
2ce0d 2a 70 69 45 6e 64 4f 66 66 73 65 74 2c 20 20 20  *piEndOffset,   
2ce0e 20 2f 2a 20 4f 55 54 3a 20 42 79 74 65 20 6f 66   /* OUT: Byte of
2ce0f 66 73 65 74 20 6f 66 20 65 6e 64 20 6f 66 20 74  fset of end of t
2ce10 6f 6b 65 6e 20 69 6e 20 69 6e 70 75 74 20 62 75  oken in input bu
2ce11 66 66 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ffer */.    int 
2ce12 2a 70 69 50 6f 73 69 74 69 6f 6e 20 20 20 20 20  *piPosition     
2ce13 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20   /* OUT: Number 
2ce14 6f 66 20 74 6f 6b 65 6e 73 20 72 65 74 75 72 6e  of tokens return
2ce15 65 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 6f  ed before this o
2ce16 6e 65 20 2a 2f 0a 20 20 29 3b 0a 7d 3b 0a 0a 73  ne */.  );.};..s
2ce17 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 74 6f  truct sqlite3_to
2ce18 6b 65 6e 69 7a 65 72 20 7b 0a 20 20 63 6f 6e 73  kenizer {.  cons
2ce19 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  t sqlite3_tokeni
2ce1a 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  zer_module *pMod
2ce1b 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20 6d 6f 64  ule;  /* The mod
2ce1c 75 6c 65 20 66 6f 72 20 74 68 69 73 20 74 6f 6b  ule for this tok
2ce1d 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 2f 2a 20 54  enizer */.  /* T
2ce1e 6f 6b 65 6e 69 7a 65 72 20 69 6d 70 6c 65 6d 65  okenizer impleme
2ce1f 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79  ntations will ty
2ce20 70 69 63 61 6c 6c 79 20 61 64 64 20 61 64 64 69  pically add addi
2ce21 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f  tional fields */
2ce22 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 73 71 6c 69  .};..struct sqli
2ce23 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
2ce24 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33  rsor {.  sqlite3
2ce25 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b  _tokenizer *pTok
2ce26 65 6e 69 7a 65 72 3b 20 20 20 20 20 20 20 2f 2a  enizer;       /*
2ce27 20 54 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 74   Tokenizer for t
2ce28 68 69 73 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20  his cursor. */. 
2ce29 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 69 6d   /* Tokenizer im
2ce2a 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69  plementations wi
2ce2b 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20 61 64 64  ll typically add
2ce2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c   additional fiel
2ce2d 64 73 20 2a 2f 0a 7d 3b 0a 0a 23 65 6e 64 69 66  ds */.};..#endif
2ce2e 20 2f 2a 20 5f 46 54 53 33 5f 54 4f 4b 45 4e 49   /* _FTS3_TOKENI
2ce2f 5a 45 52 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  ZER_H_ */../****
2ce30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
2ce31 66 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72  f fts3_tokenizer
2ce32 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
2ce33 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ce34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
2ce35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
2ce36 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
2ce37 65 66 74 20 6f 66 66 20 69 6e 20 66 74 73 33 2e  eft off in fts3.
2ce38 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2ce39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 66 6e 64  *********/.#ifnd
2ce3a 65 66 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 0a  ef SQLITE_CORE .
2ce3b 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49    SQLITE_EXTENSI
2ce3c 4f 4e 5f 49 4e 49 54 31 0a 23 65 6e 64 69 66 0a  ON_INIT1.#endif.
2ce3d 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  ../* TODO(shess)
2ce3e 20 4d 41 4e 2c 20 74 68 69 73 20 74 68 69 6e 67   MAN, this thing
2ce3f 20 6e 65 65 64 73 20 73 6f 6d 65 20 72 65 66 61   needs some refa
2ce40 63 74 6f 72 69 6e 67 2e 20 20 41 74 20 6d 69 6e  ctoring.  At min
2ce41 69 6d 75 6d 2c 20 69 74 0a 2a 2a 20 77 6f 75 6c  imum, it.** woul
2ce42 64 20 62 65 20 6e 69 63 65 20 74 6f 20 6f 72 64  d be nice to ord
2ce43 65 72 20 74 68 65 20 66 69 6c 65 20 62 65 74 74  er the file bett
2ce44 65 72 2c 20 70 65 72 68 61 70 73 20 73 6f 6d 65  er, perhaps some
2ce45 74 68 69 6e 67 20 61 6c 6f 6e 67 20 74 68 65 0a  thing along the.
2ce46 2a 2a 20 6c 69 6e 65 73 20 6f 66 3a 0a 2a 2a 0a  ** lines of:.**.
2ce47 2a 2a 20 20 2d 20 75 74 69 6c 69 74 79 20 66 75  **  - utility fu
2ce48 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 2d 20 74 61  nctions.**  - ta
2ce49 62 6c 65 20 73 65 74 75 70 20 66 75 6e 63 74 69  ble setup functi
2ce4a 6f 6e 73 0a 2a 2a 20 20 2d 20 74 61 62 6c 65 20  ons.**  - table 
2ce4b 75 70 64 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  update functions
2ce4c 0a 2a 2a 20 20 2d 20 74 61 62 6c 65 20 71 75 65  .**  - table que
2ce4d 72 79 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 0a  ry functions.**.
2ce4e 2a 2a 20 50 75 74 20 74 68 65 20 71 75 65 72 79  ** Put the query
2ce4f 20 66 75 6e 63 74 69 6f 6e 73 20 6c 61 73 74 20   functions last 
2ce50 62 65 63 61 75 73 65 20 74 68 65 79 27 72 65 20  because they're 
2ce51 6c 69 6b 65 6c 79 20 74 6f 20 72 65 66 65 72 65  likely to refere
2ce52 6e 63 65 0a 2a 2a 20 74 79 70 65 64 65 66 73 20  nce.** typedefs 
2ce53 6f 72 20 66 75 6e 63 74 69 6f 6e 73 20 66 72 6f  or functions fro
2ce54 6d 20 74 68 65 20 74 61 62 6c 65 20 75 70 64 61  m the table upda
2ce55 74 65 20 73 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 0a  te section..*/..
2ce56 23 69 66 20 30 0a 23 20 64 65 66 69 6e 65 20 46  #if 0.# define F
2ce57 54 53 54 52 41 43 45 28 41 29 20 20 70 72 69 6e  TSTRACE(A)  prin
2ce58 74 66 20 41 3b 20 66 66 6c 75 73 68 28 73 74 64  tf A; fflush(std
2ce59 6f 75 74 29 0a 23 65 6c 73 65 0a 23 20 64 65 66  out).#else.# def
2ce5a 69 6e 65 20 46 54 53 54 52 41 43 45 28 41 29 0a  ine FTSTRACE(A).
2ce5b 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65  #endif../*.** De
2ce5c 66 61 75 6c 74 20 73 70 61 6e 20 66 6f 72 20 4e  fault span for N
2ce5d 45 41 52 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a  EAR operators..*
2ce5e 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
2ce5f 5f 46 54 53 33 5f 44 45 46 41 55 4c 54 5f 4e 45  _FTS3_DEFAULT_NE
2ce60 41 52 5f 50 41 52 41 4d 20 31 30 0a 0a 2f 2a 20  AR_PARAM 10../* 
2ce61 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74  It is not safe t
2ce62 6f 20 63 61 6c 6c 20 69 73 73 70 61 63 65 28 29  o call isspace()
2ce63 2c 20 74 6f 6c 6f 77 65 72 28 29 2c 20 6f 72 20  , tolower(), or 
2ce64 69 73 61 6c 6e 75 6d 28 29 20 6f 6e 0a 2a 2a 20  isalnum() on.** 
2ce65 68 69 2d 62 69 74 2d 73 65 74 20 63 68 61 72 61  hi-bit-set chara
2ce66 63 74 65 72 73 2e 20 20 54 68 69 73 20 69 73 20  cters.  This is 
2ce67 74 68 65 20 73 61 6d 65 20 73 6f 6c 75 74 69 6f  the same solutio
2ce68 6e 20 75 73 65 64 20 69 6e 20 74 68 65 0a 2a 2a  n used in the.**
2ce69 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2f 0a 2f   tokenizer..*/./
2ce6a 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68  * TODO(shess) Th
2ce6b 65 20 73 6e 69 70 70 65 74 2d 67 65 6e 65 72 61  e snippet-genera
2ce6c 74 69 6f 6e 20 63 6f 64 65 20 73 68 6f 75 6c 64  tion code should
2ce6d 20 62 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a   be using the.**
2ce6e 20 74 6f 6b 65 6e 69 7a 65 72 2d 67 65 6e 65 72   tokenizer-gener
2ce6f 61 74 65 64 20 74 6f 6b 65 6e 73 20 72 61 74 68  ated tokens rath
2ce70 65 72 20 74 68 61 6e 20 64 6f 69 6e 67 20 69 74  er than doing it
2ce71 73 20 6f 77 6e 20 6c 6f 63 61 6c 0a 2a 2a 20 74  s own local.** t
2ce72 6f 6b 65 6e 69 7a 61 74 69 6f 6e 2e 0a 2a 2f 0a  okenization..*/.
2ce73 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 49  /* TODO(shess) I
2ce74 73 20 5f 5f 69 73 61 73 63 69 69 28 29 20 61 20  s __isascii() a 
2ce75 70 6f 72 74 61 62 6c 65 20 76 65 72 73 69 6f 6e  portable version
2ce76 20 6f 66 20 28 63 26 30 78 38 30 29 3d 3d 30 3f   of (c&0x80)==0?
2ce77 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73   */.static int s
2ce78 61 66 65 5f 69 73 73 70 61 63 65 28 63 68 61 72  afe_isspace(char
2ce79 20 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 63   c){.  return (c
2ce7a 26 30 78 38 30 29 3d 3d 30 20 3f 20 69 73 73 70  &0x80)==0 ? issp
2ce7b 61 63 65 28 63 29 20 3a 20 30 3b 0a 7d 0a 73 74  ace(c) : 0;.}.st
2ce7c 61 74 69 63 20 69 6e 74 20 73 61 66 65 5f 74 6f  atic int safe_to
2ce7d 6c 6f 77 65 72 28 63 68 61 72 20 63 29 7b 0a 20  lower(char c){. 
2ce7e 20 72 65 74 75 72 6e 20 28 63 26 30 78 38 30 29   return (c&0x80)
2ce7f 3d 3d 30 20 3f 20 74 6f 6c 6f 77 65 72 28 63 29  ==0 ? tolower(c)
2ce80 20 3a 20 63 3b 0a 7d 0a 73 74 61 74 69 63 20 69   : c;.}.static i
2ce81 6e 74 20 73 61 66 65 5f 69 73 61 6c 6e 75 6d 28  nt safe_isalnum(
2ce82 63 68 61 72 20 63 29 7b 0a 20 20 72 65 74 75 72  char c){.  retur
2ce83 6e 20 28 63 26 30 78 38 30 29 3d 3d 30 20 3f 20  n (c&0x80)==0 ? 
2ce84 69 73 61 6c 6e 75 6d 28 63 29 20 3a 20 30 3b 0a  isalnum(c) : 0;.
2ce85 7d 0a 0a 74 79 70 65 64 65 66 20 65 6e 75 6d 20  }..typedef enum 
2ce86 44 6f 63 4c 69 73 74 54 79 70 65 20 7b 0a 20 20  DocListType {.  
2ce87 44 4c 5f 44 4f 43 49 44 53 2c 20 20 20 20 20 20  DL_DOCIDS,      
2ce88 20 20 20 20 20 20 20 20 2f 2a 20 64 6f 63 69 64          /* docid
2ce89 73 20 6f 6e 6c 79 20 2a 2f 0a 20 20 44 4c 5f 50  s only */.  DL_P
2ce8a 4f 53 49 54 49 4f 4e 53 2c 20 20 20 20 20 20 20  OSITIONS,       
2ce8b 20 20 20 20 2f 2a 20 64 6f 63 69 64 73 20 2b 20      /* docids + 
2ce8c 70 6f 73 69 74 69 6f 6e 73 20 2a 2f 0a 20 20 44  positions */.  D
2ce8d 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53  L_POSITIONS_OFFS
2ce8e 45 54 53 20 20 20 20 2f 2a 20 64 6f 63 69 64 73  ETS    /* docids
2ce8f 20 2b 20 70 6f 73 69 74 69 6f 6e 73 20 2b 20 6f   + positions + o
2ce90 66 66 73 65 74 73 20 2a 2f 0a 7d 20 44 6f 63 4c  ffsets */.} DocL
2ce91 69 73 74 54 79 70 65 3b 0a 0a 2f 2a 0a 2a 2a 20  istType;../*.** 
2ce92 42 79 20 64 65 66 61 75 6c 74 2c 20 6f 6e 6c 79  By default, only
2ce93 20 70 6f 73 69 74 69 6f 6e 73 20 61 6e 64 20 6e   positions and n
2ce94 6f 74 20 6f 66 66 73 65 74 73 20 61 72 65 20 73  ot offsets are s
2ce95 74 6f 72 65 64 20 69 6e 20 74 68 65 20 64 6f 63  tored in the doc
2ce96 6c 69 73 74 73 2e 0a 2a 2a 20 54 6f 20 63 68 61  lists..** To cha
2ce97 6e 67 65 20 74 68 69 73 20 73 6f 20 74 68 61 74  nge this so that
2ce98 20 6f 66 66 73 65 74 73 20 61 72 65 20 73 74 6f   offsets are sto
2ce99 72 65 64 20 74 6f 6f 2c 20 63 6f 6d 70 69 6c 65  red too, compile
2ce9a 20 77 69 74 68 0a 2a 2a 0a 2a 2a 20 20 20 20 20   with.**.**     
2ce9b 20 20 20 20 20 2d 44 44 4c 5f 44 45 46 41 55 4c       -DDL_DEFAUL
2ce9c 54 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f  T=DL_POSITIONS_O
2ce9d 46 46 53 45 54 53 0a 2a 2a 0a 2a 2a 20 49 66 20  FFSETS.**.** If 
2ce9e 44 4c 5f 44 45 46 41 55 4c 54 20 69 73 20 73 65  DL_DEFAULT is se
2ce9f 74 20 74 6f 20 44 4c 5f 44 4f 43 49 44 53 2c 20  t to DL_DOCIDS, 
2cea0 79 6f 75 72 20 74 61 62 6c 65 20 63 61 6e 20 6f  your table can o
2cea1 6e 6c 79 20 62 65 20 69 6e 73 65 72 74 65 64 0a  nly be inserted.
2cea2 2a 2a 20 69 6e 74 6f 20 28 6e 6f 20 64 65 6c 65  ** into (no dele
2cea3 74 65 73 20 6f 72 20 75 70 64 61 74 65 73 29 2e  tes or updates).
2cea4 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 44 4c 5f 44  .*/.#ifndef DL_D
2cea5 45 46 41 55 4c 54 0a 23 20 64 65 66 69 6e 65 20  EFAULT.# define 
2cea6 44 4c 5f 44 45 46 41 55 4c 54 20 44 4c 5f 50 4f  DL_DEFAULT DL_PO
2cea7 53 49 54 49 4f 4e 53 0a 23 65 6e 64 69 66 0a 0a  SITIONS.#endif..
2cea8 65 6e 75 6d 20 7b 0a 20 20 50 4f 53 5f 45 4e 44  enum {.  POS_END
2cea9 20 3d 20 30 2c 20 20 20 20 20 20 20 20 2f 2a 20   = 0,        /* 
2ceaa 65 6e 64 20 6f 66 20 74 68 69 73 20 70 6f 73 69  end of this posi
2ceab 74 69 6f 6e 20 6c 69 73 74 20 2a 2f 0a 20 20 50  tion list */.  P
2ceac 4f 53 5f 43 4f 4c 55 4d 4e 2c 20 20 20 20 20 20  OS_COLUMN,      
2cead 20 20 20 2f 2a 20 66 6f 6c 6c 6f 77 65 64 20 62     /* followed b
2ceae 79 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 6e 75 6d  y new column num
2ceaf 62 65 72 20 2a 2f 0a 20 20 50 4f 53 5f 42 41 53  ber */.  POS_BAS
2ceb0 45 0a 7d 3b 0a 0a 2f 2a 20 4d 45 52 47 45 5f 43  E.};../* MERGE_C
2ceb1 4f 55 4e 54 20 63 6f 6e 74 72 6f 6c 73 20 68 6f  OUNT controls ho
2ceb2 77 20 6f 66 74 65 6e 20 77 65 20 6d 65 72 67 65  w often we merge
2ceb3 20 73 65 67 6d 65 6e 74 73 20 28 73 65 65 20 63   segments (see c
2ceb4 6f 6d 6d 65 6e 74 20 61 74 0a 2a 2a 20 74 6f 70  omment at.** top
2ceb5 20 6f 66 20 66 69 6c 65 29 2e 0a 2a 2f 0a 23 64   of file)..*/.#d
2ceb6 65 66 69 6e 65 20 4d 45 52 47 45 5f 43 4f 55 4e  efine MERGE_COUN
2ceb7 54 20 31 36 0a 0a 2f 2a 20 75 74 69 6c 69 74 79  T 16../* utility
2ceb8 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 0a 2f   functions */../
2ceb9 2a 20 43 4c 45 41 52 28 29 20 61 6e 64 20 53 43  * CLEAR() and SC
2ceba 52 41 4d 42 4c 45 28 29 20 61 62 73 74 72 61 63  RAMBLE() abstrac
2cebb 74 20 6d 65 6d 73 65 74 28 29 20 6f 6e 20 61 20  t memset() on a 
2cebc 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 69 6e  pointer to a sin
2cebd 67 6c 65 0a 2a 2a 20 72 65 63 6f 72 64 20 74 6f  gle.** record to
2cebe 20 70 72 65 76 65 6e 74 20 65 72 72 6f 72 73 20   prevent errors 
2cebf 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a  of the form:.**.
2cec0 2a 2a 20 6d 79 5f 66 75 6e 63 74 69 6f 6e 28 53  ** my_function(S
2cec1 6f 6d 65 54 79 70 65 20 2a 62 29 7b 0a 2a 2a 20  omeType *b){.** 
2cec2 20 20 6d 65 6d 73 65 74 28 62 2c 20 27 5c 30 27    memset(b, '\0'
2cec3 2c 20 73 69 7a 65 6f 66 28 62 29 29 3b 20 20 2f  , sizeof(b));  /
2cec4 2f 20 73 69 7a 65 6f 66 28 62 29 21 3d 73 69 7a  / sizeof(b)!=siz
2cec5 65 6f 66 28 2a 62 29 0a 2a 2a 20 7d 0a 2a 2f 0a  eof(*b).** }.*/.
2cec6 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 4f  /* TODO(shess) O
2cec7 62 76 69 6f 75 73 20 63 61 6e 64 69 64 61 74 65  bvious candidate
2cec8 73 20 66 6f 72 20 61 20 68 65 61 64 65 72 20 66  s for a header f
2cec9 69 6c 65 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ile. */.#define 
2ceca 43 4c 45 41 52 28 62 29 20 6d 65 6d 73 65 74 28  CLEAR(b) memset(
2cecb 62 2c 20 27 5c 30 27 2c 20 73 69 7a 65 6f 66 28  b, '\0', sizeof(
2cecc 2a 28 62 29 29 29 0a 0a 23 69 66 6e 64 65 66 20  *(b)))..#ifndef 
2cecd 4e 44 45 42 55 47 0a 23 20 20 64 65 66 69 6e 65  NDEBUG.#  define
2cece 20 53 43 52 41 4d 42 4c 45 28 62 29 20 6d 65 6d   SCRAMBLE(b) mem
2cecf 73 65 74 28 62 2c 20 30 78 35 35 2c 20 73 69 7a  set(b, 0x55, siz
2ced0 65 6f 66 28 2a 28 62 29 29 29 0a 23 65 6c 73 65  eof(*(b))).#else
2ced1 0a 23 20 20 64 65 66 69 6e 65 20 53 43 52 41 4d  .#  define SCRAM
2ced2 42 4c 45 28 62 29 0a 23 65 6e 64 69 66 0a 0a 2f  BLE(b).#endif../
2ced3 2a 20 57 65 20 6d 61 79 20 6e 65 65 64 20 75 70  * We may need up
2ced4 20 74 6f 20 56 41 52 49 4e 54 5f 4d 41 58 20 62   to VARINT_MAX b
2ced5 79 74 65 73 20 74 6f 20 73 74 6f 72 65 20 61 6e  ytes to store an
2ced6 20 65 6e 63 6f 64 65 64 20 36 34 2d 62 69 74 20   encoded 64-bit 
2ced7 69 6e 74 65 67 65 72 2e 20 2a 2f 0a 23 64 65 66  integer. */.#def
2ced8 69 6e 65 20 56 41 52 49 4e 54 5f 4d 41 58 20 31  ine VARINT_MAX 1
2ced9 30 0a 0a 2f 2a 20 57 72 69 74 65 20 61 20 36 34  0../* Write a 64
2ceda 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65  -bit variable-le
2cedb 6e 67 74 68 20 69 6e 74 65 67 65 72 20 74 6f 20  ngth integer to 
2cedc 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20  memory starting 
2cedd 61 74 20 70 5b 30 5d 2e 0a 20 2a 20 54 68 65 20  at p[0].. * The 
2cede 6c 65 6e 67 74 68 20 6f 66 20 64 61 74 61 20 77  length of data w
2cedf 72 69 74 74 65 6e 20 77 69 6c 6c 20 62 65 20 62  ritten will be b
2cee0 65 74 77 65 65 6e 20 31 20 61 6e 64 20 56 41 52  etween 1 and VAR
2cee1 49 4e 54 5f 4d 41 58 20 62 79 74 65 73 2e 0a 20  INT_MAX bytes.. 
2cee2 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * The number of 
2cee3 62 79 74 65 73 20 77 72 69 74 74 65 6e 20 69 73  bytes written is
2cee4 20 72 65 74 75 72 6e 65 64 2e 20 2a 2f 0a 73 74   returned. */.st
2cee5 61 74 69 63 20 69 6e 74 20 66 74 73 33 50 75 74  atic int fts3Put
2cee6 56 61 72 69 6e 74 28 63 68 61 72 20 2a 70 2c 20  Varint(char *p, 
2cee7 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 76 29 7b  sqlite_int64 v){
2cee8 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
2cee9 20 2a 71 20 3d 20 28 75 6e 73 69 67 6e 65 64 20   *q = (unsigned 
2ceea 63 68 61 72 20 2a 29 20 70 3b 0a 20 20 73 71 6c  char *) p;.  sql
2ceeb 69 74 65 5f 75 69 6e 74 36 34 20 76 75 20 3d 20  ite_uint64 vu = 
2ceec 76 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 2a 71 2b  v;.  do{.    *q+
2ceed 2b 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  + = (unsigned ch
2ceee 61 72 29 20 28 28 76 75 20 26 20 30 78 37 66 29  ar) ((vu & 0x7f)
2ceef 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 20 76 75   | 0x80);.    vu
2cef0 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65   >>= 7;.  }while
2cef1 28 20 76 75 21 3d 30 20 29 3b 0a 20 20 71 5b 2d  ( vu!=0 );.  q[-
2cef2 31 5d 20 26 3d 20 30 78 37 66 3b 20 20 2f 2a 20  1] &= 0x7f;  /* 
2cef3 74 75 72 6e 20 6f 66 66 20 68 69 67 68 20 62 69  turn off high bi
2cef4 74 20 69 6e 20 66 69 6e 61 6c 20 62 79 74 65 20  t in final byte 
2cef5 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 71 20 2d  */.  assert( q -
2cef6 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
2cef7 2a 29 70 20 3c 3d 20 56 41 52 49 4e 54 5f 4d 41  *)p <= VARINT_MA
2cef8 58 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 69  X );.  return (i
2cef9 6e 74 29 20 28 71 20 2d 20 28 75 6e 73 69 67 6e  nt) (q - (unsign
2cefa 65 64 20 63 68 61 72 20 2a 29 70 29 3b 0a 7d 0a  ed char *)p);.}.
2cefb 0a 2f 2a 20 52 65 61 64 20 61 20 36 34 2d 62 69  ./* Read a 64-bi
2cefc 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74  t variable-lengt
2cefd 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d  h integer from m
2cefe 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61  emory starting a
2ceff 74 20 70 5b 30 5d 2e 0a 20 2a 20 52 65 74 75 72  t p[0].. * Retur
2cf00 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
2cf01 62 79 74 65 73 20 72 65 61 64 2c 20 6f 72 20 30  bytes read, or 0
2cf02 20 6f 6e 20 65 72 72 6f 72 2e 0a 20 2a 20 54 68   on error.. * Th
2cf03 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65  e value is store
2cf04 64 20 69 6e 20 2a 76 2e 20 2a 2f 0a 73 74 61 74  d in *v. */.stat
2cf05 69 63 20 69 6e 74 20 66 74 73 33 47 65 74 56 61  ic int fts3GetVa
2cf06 72 69 6e 74 28 63 6f 6e 73 74 20 63 68 61 72 20  rint(const char 
2cf07 2a 70 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  *p, sqlite_int64
2cf08 20 2a 76 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e   *v){.  const un
2cf09 73 69 67 6e 65 64 20 63 68 61 72 20 2a 71 20 3d  signed char *q =
2cf0a 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64   (const unsigned
2cf0b 20 63 68 61 72 20 2a 29 20 70 3b 0a 20 20 73 71   char *) p;.  sq
2cf0c 6c 69 74 65 5f 75 69 6e 74 36 34 20 78 20 3d 20  lite_uint64 x = 
2cf0d 30 2c 20 79 20 3d 20 31 3b 0a 20 20 77 68 69 6c  0, y = 1;.  whil
2cf0e 65 28 20 28 2a 71 20 26 20 30 78 38 30 29 20 3d  e( (*q & 0x80) =
2cf0f 3d 20 30 78 38 30 20 29 7b 0a 20 20 20 20 78 20  = 0x80 ){.    x 
2cf10 2b 3d 20 79 20 2a 20 28 2a 71 2b 2b 20 26 20 30  += y * (*q++ & 0
2cf11 78 37 66 29 3b 0a 20 20 20 20 79 20 3c 3c 3d 20  x7f);.    y <<= 
2cf12 37 3b 0a 20 20 20 20 69 66 28 20 71 20 2d 20 28  7;.    if( q - (
2cf13 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
2cf14 70 20 3e 3d 20 56 41 52 49 4e 54 5f 4d 41 58 20  p >= VARINT_MAX 
2cf15 29 7b 20 20 2f 2a 20 62 61 64 20 64 61 74 61 20  ){  /* bad data 
2cf16 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
2cf17 20 30 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75   0 );.      retu
2cf18 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rn 0;.    }.  }.
2cf19 20 20 78 20 2b 3d 20 79 20 2a 20 28 2a 71 2b 2b    x += y * (*q++
2cf1a 29 3b 0a 20 20 2a 76 20 3d 20 28 73 71 6c 69 74  );.  *v = (sqlit
2cf1b 65 5f 69 6e 74 36 34 29 20 78 3b 0a 20 20 72 65  e_int64) x;.  re
2cf1c 74 75 72 6e 20 28 69 6e 74 29 20 28 71 20 2d 20  turn (int) (q - 
2cf1d 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
2cf1e 29 70 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69  )p);.}..static i
2cf1f 6e 74 20 66 74 73 33 47 65 74 56 61 72 69 6e 74  nt fts3GetVarint
2cf20 33 32 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  32(const char *p
2cf21 2c 20 69 6e 74 20 2a 70 69 29 7b 0a 20 73 71 6c  , int *pi){. sql
2cf22 69 74 65 5f 69 6e 74 36 34 20 69 3b 0a 20 69 6e  ite_int64 i;. in
2cf23 74 20 72 65 74 20 3d 20 66 74 73 33 47 65 74 56  t ret = fts3GetV
2cf24 61 72 69 6e 74 28 70 2c 20 26 69 29 3b 0a 20 2a  arint(p, &i);. *
2cf25 70 69 20 3d 20 28 69 6e 74 29 20 69 3b 0a 20 61  pi = (int) i;. a
2cf26 73 73 65 72 74 28 20 2a 70 69 3d 3d 69 20 29 3b  ssert( *pi==i );
2cf27 0a 20 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a  . return ret;.}.
2cf28 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2cf29 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cf2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cf2b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cf2c 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44 61 74 61 42 75  *****/./* DataBu
2cf2d 66 66 65 72 20 69 73 20 75 73 65 64 20 74 6f 20  ffer is used to 
2cf2e 63 6f 6c 6c 65 63 74 20 64 61 74 61 20 69 6e 74  collect data int
2cf2f 6f 20 61 20 62 75 66 66 65 72 20 69 6e 20 70 69  o a buffer in pi
2cf30 65 63 65 6d 65 61 6c 0a 2a 2a 20 66 61 73 68 69  ecemeal.** fashi
2cf31 6f 6e 2e 20 20 49 74 20 69 6d 70 6c 65 6d 65 6e  on.  It implemen
2cf32 74 73 20 74 68 65 20 75 73 75 61 6c 20 64 69 73  ts the usual dis
2cf33 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65 65 6e  tinction between
2cf34 20 61 6d 6f 75 6e 74 20 6f 66 0a 2a 2a 20 64 61   amount of.** da
2cf35 74 61 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f  ta currently sto
2cf36 72 65 64 20 28 6e 44 61 74 61 29 20 61 6e 64 20  red (nData) and 
2cf37 62 75 66 66 65 72 20 63 61 70 61 63 69 74 79 20  buffer capacity 
2cf38 28 6e 43 61 70 61 63 69 74 79 29 2e 0a 2a 2a 0a  (nCapacity)..**.
2cf39 2a 2a 20 64 61 74 61 42 75 66 66 65 72 49 6e 69  ** dataBufferIni
2cf3a 74 20 2d 20 63 72 65 61 74 65 20 61 20 62 75 66  t - create a buf
2cf3b 66 65 72 20 77 69 74 68 20 67 69 76 65 6e 20 69  fer with given i
2cf3c 6e 69 74 69 61 6c 20 63 61 70 61 63 69 74 79 2e  nitial capacity.
2cf3d 0a 2a 2a 20 64 61 74 61 42 75 66 66 65 72 52 65  .** dataBufferRe
2cf3e 73 65 74 20 2d 20 66 6f 72 67 65 74 20 62 75 66  set - forget buf
2cf3f 66 65 72 27 73 20 64 61 74 61 2c 20 72 65 74 61  fer's data, reta
2cf40 69 6e 69 6e 67 20 63 61 70 61 63 69 74 79 2e 0a  ining capacity..
2cf41 2a 2a 20 64 61 74 61 42 75 66 66 65 72 44 65 73  ** dataBufferDes
2cf42 74 72 6f 79 20 2d 20 66 72 65 65 20 62 75 66 66  troy - free buff
2cf43 65 72 27 73 20 64 61 74 61 2e 0a 2a 2a 20 64 61  er's data..** da
2cf44 74 61 42 75 66 66 65 72 53 77 61 70 20 2d 20 73  taBufferSwap - s
2cf45 77 61 70 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  wap contents of 
2cf46 74 77 6f 20 62 75 66 66 65 72 73 2e 0a 2a 2a 20  two buffers..** 
2cf47 64 61 74 61 42 75 66 66 65 72 45 78 70 61 6e 64  dataBufferExpand
2cf48 20 2d 20 65 78 70 61 6e 64 20 63 61 70 61 63 69   - expand capaci
2cf49 74 79 20 77 69 74 68 6f 75 74 20 61 64 64 69 6e  ty without addin
2cf4a 67 20 64 61 74 61 2e 0a 2a 2a 20 64 61 74 61 42  g data..** dataB
2cf4b 75 66 66 65 72 41 70 70 65 6e 64 20 2d 20 61 70  ufferAppend - ap
2cf4c 70 65 6e 64 20 64 61 74 61 2e 0a 2a 2a 20 64 61  pend data..** da
2cf4d 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32 20  taBufferAppend2 
2cf4e 2d 20 61 70 70 65 6e 64 20 74 77 6f 20 70 69 65  - append two pie
2cf4f 63 65 73 20 6f 66 20 64 61 74 61 20 61 74 20 6f  ces of data at o
2cf50 6e 63 65 2e 0a 2a 2a 20 64 61 74 61 42 75 66 66  nce..** dataBuff
2cf51 65 72 52 65 70 6c 61 63 65 20 2d 20 72 65 70 6c  erReplace - repl
2cf52 61 63 65 20 62 75 66 66 65 72 27 73 20 64 61 74  ace buffer's dat
2cf53 61 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  a..*/.typedef st
2cf54 72 75 63 74 20 44 61 74 61 42 75 66 66 65 72 20  ruct DataBuffer 
2cf55 7b 0a 20 20 63 68 61 72 20 2a 70 44 61 74 61 3b  {.  char *pData;
2cf56 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
2cf57 6e 74 65 72 20 74 6f 20 6d 61 6c 6c 6f 63 27 65  nter to malloc'e
2cf58 64 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20 69  d buffer. */.  i
2cf59 6e 74 20 6e 43 61 70 61 63 69 74 79 3b 20 20 20  nt nCapacity;   
2cf5a 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
2cf5b 70 44 61 74 61 20 62 75 66 66 65 72 2e 20 2a 2f  pData buffer. */
2cf5c 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 20 20 20  .  int nData;   
2cf5d 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 64 20           /* End 
2cf5e 6f 66 20 64 61 74 61 20 6c 6f 61 64 65 64 20 69  of data loaded i
2cf5f 6e 74 6f 20 70 44 61 74 61 2e 20 2a 2f 0a 7d 20  nto pData. */.} 
2cf60 44 61 74 61 42 75 66 66 65 72 3b 0a 0a 73 74 61  DataBuffer;..sta
2cf61 74 69 63 20 76 6f 69 64 20 64 61 74 61 42 75 66  tic void dataBuf
2cf62 66 65 72 49 6e 69 74 28 44 61 74 61 42 75 66 66  ferInit(DataBuff
2cf63 65 72 20 2a 70 42 75 66 66 65 72 2c 20 69 6e 74  er *pBuffer, int
2cf64 20 6e 43 61 70 61 63 69 74 79 29 7b 0a 20 20 61   nCapacity){.  a
2cf65 73 73 65 72 74 28 20 6e 43 61 70 61 63 69 74 79  ssert( nCapacity
2cf66 3e 3d 30 20 29 3b 0a 20 20 70 42 75 66 66 65 72  >=0 );.  pBuffer
2cf67 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20 70  ->nData = 0;.  p
2cf68 42 75 66 66 65 72 2d 3e 6e 43 61 70 61 63 69 74  Buffer->nCapacit
2cf69 79 20 3d 20 6e 43 61 70 61 63 69 74 79 3b 0a 20  y = nCapacity;. 
2cf6a 20 70 42 75 66 66 65 72 2d 3e 70 44 61 74 61 20   pBuffer->pData 
2cf6b 3d 20 6e 43 61 70 61 63 69 74 79 3d 3d 30 20 3f  = nCapacity==0 ?
2cf6c 20 4e 55 4c 4c 20 3a 20 73 71 6c 69 74 65 33 5f   NULL : sqlite3_
2cf6d 6d 61 6c 6c 6f 63 28 6e 43 61 70 61 63 69 74 79  malloc(nCapacity
2cf6e 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  );.}.static void
2cf6f 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74   dataBufferReset
2cf70 28 44 61 74 61 42 75 66 66 65 72 20 2a 70 42 75  (DataBuffer *pBu
2cf71 66 66 65 72 29 7b 0a 20 20 70 42 75 66 66 65 72  ffer){.  pBuffer
2cf72 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a 7d 0a 73  ->nData = 0;.}.s
2cf73 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 61 42  tatic void dataB
2cf74 75 66 66 65 72 44 65 73 74 72 6f 79 28 44 61 74  ufferDestroy(Dat
2cf75 61 42 75 66 66 65 72 20 2a 70 42 75 66 66 65 72  aBuffer *pBuffer
2cf76 29 7b 0a 20 20 69 66 28 20 70 42 75 66 66 65 72  ){.  if( pBuffer
2cf77 2d 3e 70 44 61 74 61 21 3d 4e 55 4c 4c 20 29 20  ->pData!=NULL ) 
2cf78 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 75  sqlite3_free(pBu
2cf79 66 66 65 72 2d 3e 70 44 61 74 61 29 3b 0a 20 20  ffer->pData);.  
2cf7a 53 43 52 41 4d 42 4c 45 28 70 42 75 66 66 65 72  SCRAMBLE(pBuffer
2cf7b 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  );.}.static void
2cf7c 20 64 61 74 61 42 75 66 66 65 72 53 77 61 70 28   dataBufferSwap(
2cf7d 44 61 74 61 42 75 66 66 65 72 20 2a 70 42 75 66  DataBuffer *pBuf
2cf7e 66 65 72 31 2c 20 44 61 74 61 42 75 66 66 65 72  fer1, DataBuffer
2cf7f 20 2a 70 42 75 66 66 65 72 32 29 7b 0a 20 20 44   *pBuffer2){.  D
2cf80 61 74 61 42 75 66 66 65 72 20 74 6d 70 20 3d 20  ataBuffer tmp = 
2cf81 2a 70 42 75 66 66 65 72 31 3b 0a 20 20 2a 70 42  *pBuffer1;.  *pB
2cf82 75 66 66 65 72 31 20 3d 20 2a 70 42 75 66 66 65  uffer1 = *pBuffe
2cf83 72 32 3b 0a 20 20 2a 70 42 75 66 66 65 72 32 20  r2;.  *pBuffer2 
2cf84 3d 20 74 6d 70 3b 0a 7d 0a 73 74 61 74 69 63 20  = tmp;.}.static 
2cf85 76 6f 69 64 20 64 61 74 61 42 75 66 66 65 72 45  void dataBufferE
2cf86 78 70 61 6e 64 28 44 61 74 61 42 75 66 66 65 72  xpand(DataBuffer
2cf87 20 2a 70 42 75 66 66 65 72 2c 20 69 6e 74 20 6e   *pBuffer, int n
2cf88 41 64 64 43 61 70 61 63 69 74 79 29 7b 0a 20 20  AddCapacity){.  
2cf89 61 73 73 65 72 74 28 20 6e 41 64 64 43 61 70 61  assert( nAddCapa
2cf8a 63 69 74 79 3e 30 20 29 3b 0a 20 20 2f 2a 20 54  city>0 );.  /* T
2cf8b 4f 44 4f 28 73 68 65 73 73 29 20 43 6f 6e 73 69  ODO(shess) Consi
2cf8c 64 65 72 20 65 78 70 61 6e 64 69 6e 67 20 6d 6f  der expanding mo
2cf8d 72 65 20 61 67 67 72 65 73 73 69 76 65 6c 79 2e  re aggressively.
2cf8e 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 0a    Note that the.
2cf8f 20 20 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20    ** underlying 
2cf90 6d 61 6c 6c 6f 63 20 69 6d 70 6c 65 6d 65 6e 74  malloc implement
2cf91 61 74 69 6f 6e 20 6d 61 79 20 74 61 6b 65 20 63  ation may take c
2cf92 61 72 65 20 6f 66 20 73 75 63 68 20 74 68 69 6e  are of such thin
2cf93 67 73 20 66 6f 72 0a 20 20 2a 2a 20 75 73 20 61  gs for.  ** us a
2cf94 6c 72 65 61 64 79 2e 0a 20 20 2a 2f 0a 20 20 69  lready..  */.  i
2cf95 66 28 20 70 42 75 66 66 65 72 2d 3e 6e 44 61 74  f( pBuffer->nDat
2cf96 61 2b 6e 41 64 64 43 61 70 61 63 69 74 79 3e 70  a+nAddCapacity>p
2cf97 42 75 66 66 65 72 2d 3e 6e 43 61 70 61 63 69 74  Buffer->nCapacit
2cf98 79 20 29 7b 0a 20 20 20 20 70 42 75 66 66 65 72  y ){.    pBuffer
2cf99 2d 3e 6e 43 61 70 61 63 69 74 79 20 3d 20 70 42  ->nCapacity = pB
2cf9a 75 66 66 65 72 2d 3e 6e 44 61 74 61 2b 6e 41 64  uffer->nData+nAd
2cf9b 64 43 61 70 61 63 69 74 79 3b 0a 20 20 20 20 70  dCapacity;.    p
2cf9c 42 75 66 66 65 72 2d 3e 70 44 61 74 61 20 3d 20  Buffer->pData = 
2cf9d 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
2cf9e 70 42 75 66 66 65 72 2d 3e 70 44 61 74 61 2c 20  pBuffer->pData, 
2cf9f 70 42 75 66 66 65 72 2d 3e 6e 43 61 70 61 63 69  pBuffer->nCapaci
2cfa0 74 79 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69  ty);.  }.}.stati
2cfa1 63 20 76 6f 69 64 20 64 61 74 61 42 75 66 66 65  c void dataBuffe
2cfa2 72 41 70 70 65 6e 64 28 44 61 74 61 42 75 66 66  rAppend(DataBuff
2cfa3 65 72 20 2a 70 42 75 66 66 65 72 2c 0a 20 20 20  er *pBuffer,.   
2cfa4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cfa5 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
2cfa6 63 68 61 72 20 2a 70 53 6f 75 72 63 65 2c 20 69  char *pSource, i
2cfa7 6e 74 20 6e 53 6f 75 72 63 65 29 7b 0a 20 20 61  nt nSource){.  a
2cfa8 73 73 65 72 74 28 20 6e 53 6f 75 72 63 65 3e 30  ssert( nSource>0
2cfa9 20 26 26 20 70 53 6f 75 72 63 65 21 3d 4e 55 4c   && pSource!=NUL
2cfaa 4c 20 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  L );.  dataBuffe
2cfab 72 45 78 70 61 6e 64 28 70 42 75 66 66 65 72 2c  rExpand(pBuffer,
2cfac 20 6e 53 6f 75 72 63 65 29 3b 0a 20 20 6d 65 6d   nSource);.  mem
2cfad 63 70 79 28 70 42 75 66 66 65 72 2d 3e 70 44 61  cpy(pBuffer->pDa
2cfae 74 61 2b 70 42 75 66 66 65 72 2d 3e 6e 44 61 74  ta+pBuffer->nDat
2cfaf 61 2c 20 70 53 6f 75 72 63 65 2c 20 6e 53 6f 75  a, pSource, nSou
2cfb0 72 63 65 29 3b 0a 20 20 70 42 75 66 66 65 72 2d  rce);.  pBuffer-
2cfb1 3e 6e 44 61 74 61 20 2b 3d 20 6e 53 6f 75 72 63  >nData += nSourc
2cfb2 65 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  e;.}.static void
2cfb3 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
2cfb4 64 32 28 44 61 74 61 42 75 66 66 65 72 20 2a 70  d2(DataBuffer *p
2cfb5 42 75 66 66 65 72 2c 0a 20 20 20 20 20 20 20 20  Buffer,.        
2cfb6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cfb7 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
2cfb8 20 2a 70 53 6f 75 72 63 65 31 2c 20 69 6e 74 20   *pSource1, int 
2cfb9 6e 53 6f 75 72 63 65 31 2c 0a 20 20 20 20 20 20  nSource1,.      
2cfba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cfbb 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
2cfbc 61 72 20 2a 70 53 6f 75 72 63 65 32 2c 20 69 6e  ar *pSource2, in
2cfbd 74 20 6e 53 6f 75 72 63 65 32 29 7b 0a 20 20 61  t nSource2){.  a
2cfbe 73 73 65 72 74 28 20 6e 53 6f 75 72 63 65 31 3e  ssert( nSource1>
2cfbf 30 20 26 26 20 70 53 6f 75 72 63 65 31 21 3d 4e  0 && pSource1!=N
2cfc0 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ULL );.  assert(
2cfc1 20 6e 53 6f 75 72 63 65 32 3e 30 20 26 26 20 70   nSource2>0 && p
2cfc2 53 6f 75 72 63 65 32 21 3d 4e 55 4c 4c 20 29 3b  Source2!=NULL );
2cfc3 0a 20 20 64 61 74 61 42 75 66 66 65 72 45 78 70  .  dataBufferExp
2cfc4 61 6e 64 28 70 42 75 66 66 65 72 2c 20 6e 53 6f  and(pBuffer, nSo
2cfc5 75 72 63 65 31 2b 6e 53 6f 75 72 63 65 32 29 3b  urce1+nSource2);
2cfc6 0a 20 20 6d 65 6d 63 70 79 28 70 42 75 66 66 65  .  memcpy(pBuffe
2cfc7 72 2d 3e 70 44 61 74 61 2b 70 42 75 66 66 65 72  r->pData+pBuffer
2cfc8 2d 3e 6e 44 61 74 61 2c 20 70 53 6f 75 72 63 65  ->nData, pSource
2cfc9 31 2c 20 6e 53 6f 75 72 63 65 31 29 3b 0a 20 20  1, nSource1);.  
2cfca 6d 65 6d 63 70 79 28 70 42 75 66 66 65 72 2d 3e  memcpy(pBuffer->
2cfcb 70 44 61 74 61 2b 70 42 75 66 66 65 72 2d 3e 6e  pData+pBuffer->n
2cfcc 44 61 74 61 2b 6e 53 6f 75 72 63 65 31 2c 20 70  Data+nSource1, p
2cfcd 53 6f 75 72 63 65 32 2c 20 6e 53 6f 75 72 63 65  Source2, nSource
2cfce 32 29 3b 0a 20 20 70 42 75 66 66 65 72 2d 3e 6e  2);.  pBuffer->n
2cfcf 44 61 74 61 20 2b 3d 20 6e 53 6f 75 72 63 65 31  Data += nSource1
2cfd0 2b 6e 53 6f 75 72 63 65 32 3b 0a 7d 0a 73 74 61  +nSource2;.}.sta
2cfd1 74 69 63 20 76 6f 69 64 20 64 61 74 61 42 75 66  tic void dataBuf
2cfd2 66 65 72 52 65 70 6c 61 63 65 28 44 61 74 61 42  ferReplace(DataB
2cfd3 75 66 66 65 72 20 2a 70 42 75 66 66 65 72 2c 0a  uffer *pBuffer,.
2cfd4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cfd5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
2cfd6 6e 73 74 20 63 68 61 72 20 2a 70 53 6f 75 72 63  nst char *pSourc
2cfd7 65 2c 20 69 6e 74 20 6e 53 6f 75 72 63 65 29 7b  e, int nSource){
2cfd8 0a 20 20 64 61 74 61 42 75 66 66 65 72 52 65 73  .  dataBufferRes
2cfd9 65 74 28 70 42 75 66 66 65 72 29 3b 0a 20 20 64  et(pBuffer);.  d
2cfda 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28  ataBufferAppend(
2cfdb 70 42 75 66 66 65 72 2c 20 70 53 6f 75 72 63 65  pBuffer, pSource
2cfdc 2c 20 6e 53 6f 75 72 63 65 29 3b 0a 7d 0a 0a 2f  , nSource);.}../
2cfdd 2a 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 69  * StringBuffer i
2cfde 73 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61  s a null-termina
2cfdf 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 44  ted version of D
2cfe0 61 74 61 42 75 66 66 65 72 2e 20 2a 2f 0a 74 79  ataBuffer. */.ty
2cfe1 70 65 64 65 66 20 73 74 72 75 63 74 20 53 74 72  pedef struct Str
2cfe2 69 6e 67 42 75 66 66 65 72 20 7b 0a 20 20 44 61  ingBuffer {.  Da
2cfe3 74 61 42 75 66 66 65 72 20 62 3b 20 20 20 20 20  taBuffer b;     
2cfe4 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 6c 75 64         /* Includ
2cfe5 65 73 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 74  es null terminat
2cfe6 6f 72 2e 20 2a 2f 0a 7d 20 53 74 72 69 6e 67 42  or. */.} StringB
2cfe7 75 66 66 65 72 3b 0a 0a 73 74 61 74 69 63 20 76  uffer;..static v
2cfe8 6f 69 64 20 69 6e 69 74 53 74 72 69 6e 67 42 75  oid initStringBu
2cfe9 66 66 65 72 28 53 74 72 69 6e 67 42 75 66 66 65  ffer(StringBuffe
2cfea 72 20 2a 73 62 29 7b 0a 20 20 64 61 74 61 42 75  r *sb){.  dataBu
2cfeb 66 66 65 72 49 6e 69 74 28 26 73 62 2d 3e 62 2c  fferInit(&sb->b,
2cfec 20 31 30 30 29 3b 0a 20 20 64 61 74 61 42 75 66   100);.  dataBuf
2cfed 66 65 72 52 65 70 6c 61 63 65 28 26 73 62 2d 3e  ferReplace(&sb->
2cfee 62 2c 20 22 22 2c 20 31 29 3b 0a 7d 0a 73 74 61  b, "", 1);.}.sta
2cfef 74 69 63 20 69 6e 74 20 73 74 72 69 6e 67 42 75  tic int stringBu
2cff0 66 66 65 72 4c 65 6e 67 74 68 28 53 74 72 69 6e  fferLength(Strin
2cff1 67 42 75 66 66 65 72 20 2a 73 62 29 7b 0a 20 20  gBuffer *sb){.  
2cff2 72 65 74 75 72 6e 20 73 62 2d 3e 62 2e 6e 44 61  return sb->b.nDa
2cff3 74 61 2d 31 3b 0a 7d 0a 73 74 61 74 69 63 20 63  ta-1;.}.static c
2cff4 68 61 72 20 2a 73 74 72 69 6e 67 42 75 66 66 65  har *stringBuffe
2cff5 72 44 61 74 61 28 53 74 72 69 6e 67 42 75 66 66  rData(StringBuff
2cff6 65 72 20 2a 73 62 29 7b 0a 20 20 72 65 74 75 72  er *sb){.  retur
2cff7 6e 20 73 62 2d 3e 62 2e 70 44 61 74 61 3b 0a 7d  n sb->b.pData;.}
2cff8 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 74 72  .static void str
2cff9 69 6e 67 42 75 66 66 65 72 44 65 73 74 72 6f 79  ingBufferDestroy
2cffa 28 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a 73  (StringBuffer *s
2cffb 62 29 7b 0a 20 20 64 61 74 61 42 75 66 66 65 72  b){.  dataBuffer
2cffc 44 65 73 74 72 6f 79 28 26 73 62 2d 3e 62 29 3b  Destroy(&sb->b);
2cffd 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .}..static void 
2cffe 6e 61 70 70 65 6e 64 28 53 74 72 69 6e 67 42 75  nappend(StringBu
2cfff 66 66 65 72 20 2a 73 62 2c 20 63 6f 6e 73 74 20  ffer *sb, const 
2d000 63 68 61 72 20 2a 7a 46 72 6f 6d 2c 20 69 6e 74  char *zFrom, int
2d001 20 6e 46 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72   nFrom){.  asser
2d002 74 28 20 73 62 2d 3e 62 2e 6e 44 61 74 61 3e 30  t( sb->b.nData>0
2d003 20 29 3b 0a 20 20 69 66 28 20 6e 46 72 6f 6d 3e   );.  if( nFrom>
2d004 30 20 29 7b 0a 20 20 20 20 73 62 2d 3e 62 2e 6e  0 ){.    sb->b.n
2d005 44 61 74 61 2d 2d 3b 0a 20 20 20 20 64 61 74 61  Data--;.    data
2d006 42 75 66 66 65 72 41 70 70 65 6e 64 32 28 26 73  BufferAppend2(&s
2d007 62 2d 3e 62 2c 20 7a 46 72 6f 6d 2c 20 6e 46 72  b->b, zFrom, nFr
2d008 6f 6d 2c 20 22 22 2c 20 31 29 3b 0a 20 20 7d 0a  om, "", 1);.  }.
2d009 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 70  }.static void ap
2d00a 70 65 6e 64 28 53 74 72 69 6e 67 42 75 66 66 65  pend(StringBuffe
2d00b 72 20 2a 73 62 2c 20 63 6f 6e 73 74 20 63 68 61  r *sb, const cha
2d00c 72 20 2a 7a 46 72 6f 6d 29 7b 0a 20 20 6e 61 70  r *zFrom){.  nap
2d00d 70 65 6e 64 28 73 62 2c 20 7a 46 72 6f 6d 2c 20  pend(sb, zFrom, 
2d00e 73 74 72 6c 65 6e 28 7a 46 72 6f 6d 29 29 3b 0a  strlen(zFrom));.
2d00f 7d 0a 0a 2f 2a 20 41 70 70 65 6e 64 20 61 20 6c  }../* Append a l
2d010 69 73 74 20 6f 66 20 73 74 72 69 6e 67 73 20 73  ist of strings s
2d011 65 70 61 72 61 74 65 64 20 62 79 20 63 6f 6d 6d  eparated by comm
2d012 61 73 2e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f  as. */.static vo
2d013 69 64 20 61 70 70 65 6e 64 4c 69 73 74 28 53 74  id appendList(St
2d014 72 69 6e 67 42 75 66 66 65 72 20 2a 73 62 2c 20  ringBuffer *sb, 
2d015 69 6e 74 20 6e 53 74 72 69 6e 67 2c 20 63 68 61  int nString, cha
2d016 72 20 2a 2a 61 7a 53 74 72 69 6e 67 29 7b 0a 20  r **azString){. 
2d017 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
2d018 30 3b 20 69 3c 6e 53 74 72 69 6e 67 3b 20 2b 2b  0; i<nString; ++
2d019 69 29 7b 0a 20 20 20 20 69 66 28 20 69 3e 30 20  i){.    if( i>0 
2d01a 29 20 61 70 70 65 6e 64 28 73 62 2c 20 22 2c 20  ) append(sb, ", 
2d01b 22 29 3b 0a 20 20 20 20 61 70 70 65 6e 64 28 73  ");.    append(s
2d01c 62 2c 20 61 7a 53 74 72 69 6e 67 5b 69 5d 29 3b  b, azString[i]);
2d01d 0a 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 69  .  }.}..static i
2d01e 6e 74 20 65 6e 64 73 49 6e 57 68 69 74 65 53 70  nt endsInWhiteSp
2d01f 61 63 65 28 53 74 72 69 6e 67 42 75 66 66 65 72  ace(StringBuffer
2d020 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73   *p){.  return s
2d021 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e 67 74  tringBufferLengt
2d022 68 28 70 29 3e 30 20 26 26 0a 20 20 20 20 73 61  h(p)>0 &&.    sa
2d023 66 65 5f 69 73 73 70 61 63 65 28 73 74 72 69 6e  fe_isspace(strin
2d024 67 42 75 66 66 65 72 44 61 74 61 28 70 29 5b 73  gBufferData(p)[s
2d025 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e 67 74  tringBufferLengt
2d026 68 28 70 29 2d 31 5d 29 3b 0a 7d 0a 0a 2f 2a 20  h(p)-1]);.}../* 
2d027 49 66 20 74 68 65 20 53 74 72 69 6e 67 42 75 66  If the StringBuf
2d028 66 65 72 20 65 6e 64 73 20 69 6e 20 73 6f 6d 65  fer ends in some
2d029 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e  thing other than
2d02a 20 77 68 69 74 65 20 73 70 61 63 65 2c 20 61 64   white space, ad
2d02b 64 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 73 70  d a.** single sp
2d02c 61 63 65 20 63 68 61 72 61 63 74 65 72 20 74 6f  ace character to
2d02d 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61   the end..*/.sta
2d02e 74 69 63 20 76 6f 69 64 20 61 70 70 65 6e 64 57  tic void appendW
2d02f 68 69 74 65 53 70 61 63 65 28 53 74 72 69 6e 67  hiteSpace(String
2d030 42 75 66 66 65 72 20 2a 70 29 7b 0a 20 20 69 66  Buffer *p){.  if
2d031 28 20 73 74 72 69 6e 67 42 75 66 66 65 72 4c 65  ( stringBufferLe
2d032 6e 67 74 68 28 70 29 3d 3d 30 20 29 20 72 65 74  ngth(p)==0 ) ret
2d033 75 72 6e 3b 0a 20 20 69 66 28 20 21 65 6e 64 73  urn;.  if( !ends
2d034 49 6e 57 68 69 74 65 53 70 61 63 65 28 70 29 20  InWhiteSpace(p) 
2d035 29 20 61 70 70 65 6e 64 28 70 2c 20 22 20 22 29  ) append(p, " ")
2d036 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 77  ;.}../* Remove w
2d037 68 69 74 65 20 73 70 61 63 65 20 66 72 6f 6d 20  hite space from 
2d038 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 53  the end of the S
2d039 74 72 69 6e 67 42 75 66 66 65 72 20 2a 2f 0a 73  tringBuffer */.s
2d03a 74 61 74 69 63 20 76 6f 69 64 20 74 72 69 6d 57  tatic void trimW
2d03b 68 69 74 65 53 70 61 63 65 28 53 74 72 69 6e 67  hiteSpace(String
2d03c 42 75 66 66 65 72 20 2a 70 29 7b 0a 20 20 77 68  Buffer *p){.  wh
2d03d 69 6c 65 28 20 65 6e 64 73 49 6e 57 68 69 74 65  ile( endsInWhite
2d03e 53 70 61 63 65 28 70 29 20 29 7b 0a 20 20 20 20  Space(p) ){.    
2d03f 70 2d 3e 62 2e 70 44 61 74 61 5b 2d 2d 70 2d 3e  p->b.pData[--p->
2d040 62 2e 6e 44 61 74 61 2d 31 5d 20 3d 20 27 5c 30  b.nData-1] = '\0
2d041 27 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  ';.  }.}../*****
2d042 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d043 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d044 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d045 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2d046 2f 2a 20 44 4c 52 65 61 64 65 72 20 69 73 20 75  /* DLReader is u
2d047 73 65 64 20 74 6f 20 72 65 61 64 20 64 6f 63 75  sed to read docu
2d048 6d 65 6e 74 20 65 6c 65 6d 65 6e 74 73 20 66 72  ment elements fr
2d049 6f 6d 20 61 20 64 6f 63 6c 69 73 74 2e 20 20 54  om a doclist.  T
2d04a 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 64 6f  he.** current do
2d04b 63 69 64 20 69 73 20 63 61 63 68 65 64 2c 20 73  cid is cached, s
2d04c 6f 20 64 6c 72 44 6f 63 69 64 28 29 20 69 73 20  o dlrDocid() is 
2d04d 66 61 73 74 2e 20 20 44 4c 52 65 61 64 65 72 20  fast.  DLReader 
2d04e 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6f 77 6e 20  does not.** own 
2d04f 74 68 65 20 64 6f 63 6c 69 73 74 20 62 75 66 66  the doclist buff
2d050 65 72 2e 0a 2a 2a 0a 2a 2a 20 64 6c 72 41 74 45  er..**.** dlrAtE
2d051 6e 64 20 2d 20 74 72 75 65 20 69 66 20 74 68 65  nd - true if the
2d052 72 65 27 73 20 6e 6f 20 6d 6f 72 65 20 64 61 74  re's no more dat
2d053 61 20 74 6f 20 72 65 61 64 2e 0a 2a 2a 20 64 6c  a to read..** dl
2d054 72 44 6f 63 69 64 20 2d 20 64 6f 63 69 64 20 6f  rDocid - docid o
2d055 66 20 63 75 72 72 65 6e 74 20 64 6f 63 75 6d 65  f current docume
2d056 6e 74 2e 0a 2a 2a 20 64 6c 72 44 6f 63 44 61 74  nt..** dlrDocDat
2d057 61 20 2d 20 64 6f 63 6c 69 73 74 20 64 61 74 61  a - doclist data
2d058 20 66 6f 72 20 63 75 72 72 65 6e 74 20 64 6f 63   for current doc
2d059 75 6d 65 6e 74 20 28 69 6e 63 6c 75 64 69 6e 67  ument (including
2d05a 20 64 6f 63 69 64 29 2e 0a 2a 2a 20 64 6c 72 44   docid)..** dlrD
2d05b 6f 63 44 61 74 61 42 79 74 65 73 20 2d 20 6c 65  ocDataBytes - le
2d05c 6e 67 74 68 20 6f 66 20 73 61 6d 65 2e 0a 2a 2a  ngth of same..**
2d05d 20 64 6c 72 41 6c 6c 44 61 74 61 42 79 74 65 73   dlrAllDataBytes
2d05e 20 2d 20 6c 65 6e 67 74 68 20 6f 66 20 61 6c 6c   - length of all
2d05f 20 72 65 6d 61 69 6e 69 6e 67 20 64 61 74 61 2e   remaining data.
2d060 0a 2a 2a 20 64 6c 72 50 6f 73 44 61 74 61 20 2d  .** dlrPosData -
2d061 20 70 6f 73 69 74 69 6f 6e 20 64 61 74 61 20 66   position data f
2d062 6f 72 20 63 75 72 72 65 6e 74 20 64 6f 63 75 6d  or current docum
2d063 65 6e 74 2e 0a 2a 2a 20 64 6c 72 50 6f 73 44 61  ent..** dlrPosDa
2d064 74 61 4c 65 6e 20 2d 20 6c 65 6e 67 74 68 20 6f  taLen - length o
2d065 66 20 70 6f 73 20 64 61 74 61 20 66 6f 72 20 63  f pos data for c
2d066 75 72 72 65 6e 74 20 64 6f 63 75 6d 65 6e 74 20  urrent document 
2d067 28 69 6e 63 6c 20 50 4f 53 5f 45 4e 44 29 2e 0a  (incl POS_END)..
2d068 2a 2a 20 64 6c 72 53 74 65 70 20 2d 20 73 74 65  ** dlrStep - ste
2d069 70 20 74 6f 20 63 75 72 72 65 6e 74 20 64 6f 63  p to current doc
2d06a 75 6d 65 6e 74 2e 0a 2a 2a 20 64 6c 72 49 6e 69  ument..** dlrIni
2d06b 74 20 2d 20 69 6e 69 74 69 61 6c 20 66 6f 72 20  t - initial for 
2d06c 64 6f 63 6c 69 73 74 20 6f 66 20 67 69 76 65 6e  doclist of given
2d06d 20 74 79 70 65 20 61 67 61 69 6e 73 74 20 67 69   type against gi
2d06e 76 65 6e 20 64 61 74 61 2e 0a 2a 2a 20 64 6c 72  ven data..** dlr
2d06f 44 65 73 74 72 6f 79 20 2d 20 63 6c 65 61 6e 20  Destroy - clean 
2d070 75 70 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 65 63 74  up..**.** Expect
2d071 65 64 20 75 73 61 67 65 20 69 73 20 73 6f 6d 65  ed usage is some
2d072 74 68 69 6e 67 20 6c 69 6b 65 3a 0a 2a 2a 0a 2a  thing like:.**.*
2d073 2a 20 20 20 44 4c 52 65 61 64 65 72 20 72 65 61  *   DLReader rea
2d074 64 65 72 3b 0a 2a 2a 20 20 20 64 6c 72 49 6e 69  der;.**   dlrIni
2d075 74 28 26 72 65 61 64 65 72 2c 20 70 44 61 74 61  t(&reader, pData
2d076 2c 20 6e 44 61 74 61 29 3b 0a 2a 2a 20 20 20 77  , nData);.**   w
2d077 68 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64 28  hile( !dlrAtEnd(
2d078 26 72 65 61 64 65 72 29 20 29 7b 0a 2a 2a 20 20  &reader) ){.**  
2d079 20 20 20 2f 2f 20 63 61 6c 6c 73 20 74 6f 20 64     // calls to d
2d07a 6c 72 44 6f 63 69 64 28 29 20 61 6e 64 20 6b 69  lrDocid() and ki
2d07b 6e 2e 0a 2a 2a 20 20 20 20 20 64 6c 72 53 74 65  n..**     dlrSte
2d07c 70 28 26 72 65 61 64 65 72 29 3b 0a 2a 2a 20 20  p(&reader);.**  
2d07d 20 7d 0a 2a 2a 20 20 20 64 6c 72 44 65 73 74 72   }.**   dlrDestr
2d07e 6f 79 28 26 72 65 61 64 65 72 29 3b 0a 2a 2f 0a  oy(&reader);.*/.
2d07f 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 44  typedef struct D
2d080 4c 52 65 61 64 65 72 20 7b 0a 20 20 44 6f 63 4c  LReader {.  DocL
2d081 69 73 74 54 79 70 65 20 69 54 79 70 65 3b 0a 20  istType iType;. 
2d082 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61   const char *pDa
2d083 74 61 3b 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b  ta;.  int nData;
2d084 0a 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  ..  sqlite_int64
2d085 20 69 44 6f 63 69 64 3b 0a 20 20 69 6e 74 20 6e   iDocid;.  int n
2d086 45 6c 65 6d 65 6e 74 3b 0a 7d 20 44 4c 52 65 61  Element;.} DLRea
2d087 64 65 72 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74  der;..static int
2d088 20 64 6c 72 41 74 45 6e 64 28 44 4c 52 65 61 64   dlrAtEnd(DLRead
2d089 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
2d08a 61 73 73 65 72 74 28 20 70 52 65 61 64 65 72 2d  assert( pReader-
2d08b 3e 6e 44 61 74 61 3e 3d 30 20 29 3b 0a 20 20 72  >nData>=0 );.  r
2d08c 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 6e  eturn pReader->n
2d08d 44 61 74 61 3d 3d 30 3b 0a 7d 0a 73 74 61 74 69  Data==0;.}.stati
2d08e 63 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 64  c sqlite_int64 d
2d08f 6c 72 44 6f 63 69 64 28 44 4c 52 65 61 64 65 72  lrDocid(DLReader
2d090 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73   *pReader){.  as
2d091 73 65 72 74 28 20 21 64 6c 72 41 74 45 6e 64 28  sert( !dlrAtEnd(
2d092 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65  pReader) );.  re
2d093 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 69 44  turn pReader->iD
2d094 6f 63 69 64 3b 0a 7d 0a 73 74 61 74 69 63 20 63  ocid;.}.static c
2d095 6f 6e 73 74 20 63 68 61 72 20 2a 64 6c 72 44 6f  onst char *dlrDo
2d096 63 44 61 74 61 28 44 4c 52 65 61 64 65 72 20 2a  cData(DLReader *
2d097 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65  pReader){.  asse
2d098 72 74 28 20 21 64 6c 72 41 74 45 6e 64 28 70 52  rt( !dlrAtEnd(pR
2d099 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75  eader) );.  retu
2d09a 72 6e 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74  rn pReader->pDat
2d09b 61 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  a;.}.static int 
2d09c 64 6c 72 44 6f 63 44 61 74 61 42 79 74 65 73 28  dlrDocDataBytes(
2d09d 44 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  DLReader *pReade
2d09e 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 64  r){.  assert( !d
2d09f 6c 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29  lrAtEnd(pReader)
2d0a0 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65   );.  return pRe
2d0a1 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 3b 0a  ader->nElement;.
2d0a2 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6c 72  }.static int dlr
2d0a3 41 6c 6c 44 61 74 61 42 79 74 65 73 28 44 4c 52  AllDataBytes(DLR
2d0a4 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
2d0a5 0a 20 20 61 73 73 65 72 74 28 20 21 64 6c 72 41  .  assert( !dlrA
2d0a6 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b  tEnd(pReader) );
2d0a7 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65  .  return pReade
2d0a8 72 2d 3e 6e 44 61 74 61 3b 0a 7d 0a 2f 2a 20 54  r->nData;.}./* T
2d0a9 4f 44 4f 28 73 68 65 73 73 29 20 43 6f 6e 73 69  ODO(shess) Consi
2d0aa 64 65 72 20 61 64 64 69 6e 67 20 61 20 66 69 65  der adding a fie
2d0ab 6c 64 20 74 6f 20 74 72 61 63 6b 20 69 44 6f 63  ld to track iDoc
2d0ac 69 64 20 76 61 72 69 6e 74 20 6c 65 6e 67 74 68  id varint length
2d0ad 0a 2a 2a 20 74 6f 20 6d 61 6b 65 20 74 68 65 73  .** to make thes
2d0ae 65 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20  e two functions 
2d0af 66 61 73 74 65 72 2e 20 20 54 68 69 73 20 6d 69  faster.  This mi
2d0b0 67 68 74 20 6d 61 74 74 65 72 20 28 61 20 74 69  ght matter (a ti
2d0b1 6e 79 20 62 69 74 29 0a 2a 2a 20 66 6f 72 20 71  ny bit).** for q
2d0b2 75 65 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  ueries..*/.stati
2d0b3 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 6c  c const char *dl
2d0b4 72 50 6f 73 44 61 74 61 28 44 4c 52 65 61 64 65  rPosData(DLReade
2d0b5 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 73  r *pReader){.  s
2d0b6 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 75 6d  qlite_int64 iDum
2d0b7 6d 79 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 66 74  my;.  int n = ft
2d0b8 73 33 47 65 74 56 61 72 69 6e 74 28 70 52 65 61  s3GetVarint(pRea
2d0b9 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 69 44 75  der->pData, &iDu
2d0ba 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20  mmy);.  assert( 
2d0bb 21 64 6c 72 41 74 45 6e 64 28 70 52 65 61 64 65  !dlrAtEnd(pReade
2d0bc 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  r) );.  return p
2d0bd 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 3b  Reader->pData+n;
2d0be 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6c  .}.static int dl
2d0bf 72 50 6f 73 44 61 74 61 4c 65 6e 28 44 4c 52 65  rPosDataLen(DLRe
2d0c0 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
2d0c1 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
2d0c2 44 75 6d 6d 79 3b 0a 20 20 69 6e 74 20 6e 20 3d  Dummy;.  int n =
2d0c3 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 28 70   fts3GetVarint(p
2d0c4 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26  Reader->pData, &
2d0c5 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72  iDummy);.  asser
2d0c6 74 28 20 21 64 6c 72 41 74 45 6e 64 28 70 52 65  t( !dlrAtEnd(pRe
2d0c7 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72  ader) );.  retur
2d0c8 6e 20 70 52 65 61 64 65 72 2d 3e 6e 45 6c 65 6d  n pReader->nElem
2d0c9 65 6e 74 2d 6e 3b 0a 7d 0a 73 74 61 74 69 63 20  ent-n;.}.static 
2d0ca 76 6f 69 64 20 64 6c 72 53 74 65 70 28 44 4c 52  void dlrStep(DLR
2d0cb 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
2d0cc 0a 20 20 61 73 73 65 72 74 28 20 21 64 6c 72 41  .  assert( !dlrA
2d0cd 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b  tEnd(pReader) );
2d0ce 0a 0a 20 20 2f 2a 20 53 6b 69 70 20 70 61 73 74  ..  /* Skip past
2d0cf 20 63 75 72 72 65 6e 74 20 64 6f 63 6c 69 73 74   current doclist
2d0d0 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 61   element. */.  a
2d0d1 73 73 65 72 74 28 20 70 52 65 61 64 65 72 2d 3e  ssert( pReader->
2d0d2 6e 45 6c 65 6d 65 6e 74 3c 3d 70 52 65 61 64 65  nElement<=pReade
2d0d3 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20 70 52  r->nData );.  pR
2d0d4 65 61 64 65 72 2d 3e 70 44 61 74 61 20 2b 3d 20  eader->pData += 
2d0d5 70 52 65 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e  pReader->nElemen
2d0d6 74 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 6e 44  t;.  pReader->nD
2d0d7 61 74 61 20 2d 3d 20 70 52 65 61 64 65 72 2d 3e  ata -= pReader->
2d0d8 6e 45 6c 65 6d 65 6e 74 3b 0a 0a 20 20 2f 2a 20  nElement;..  /* 
2d0d9 49 66 20 74 68 65 72 65 20 69 73 20 6d 6f 72 65  If there is more
2d0da 20 64 61 74 61 2c 20 72 65 61 64 20 74 68 65 20   data, read the 
2d0db 6e 65 78 74 20 64 6f 63 6c 69 73 74 20 65 6c 65  next doclist ele
2d0dc 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70  ment. */.  if( p
2d0dd 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 21 3d 30  Reader->nData!=0
2d0de 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 5f 69   ){.    sqlite_i
2d0df 6e 74 36 34 20 69 44 6f 63 69 64 44 65 6c 74 61  nt64 iDocidDelta
2d0e0 3b 0a 20 20 20 20 69 6e 74 20 69 44 75 6d 6d 79  ;.    int iDummy
2d0e1 2c 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72  , n = fts3GetVar
2d0e2 69 6e 74 28 70 52 65 61 64 65 72 2d 3e 70 44 61  int(pReader->pDa
2d0e3 74 61 2c 20 26 69 44 6f 63 69 64 44 65 6c 74 61  ta, &iDocidDelta
2d0e4 29 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  );.    pReader->
2d0e5 69 44 6f 63 69 64 20 2b 3d 20 69 44 6f 63 69 64  iDocid += iDocid
2d0e6 44 65 6c 74 61 3b 0a 20 20 20 20 69 66 28 20 70  Delta;.    if( p
2d0e7 52 65 61 64 65 72 2d 3e 69 54 79 70 65 3e 3d 44  Reader->iType>=D
2d0e8 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 29 7b 0a 20  L_POSITIONS ){. 
2d0e9 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c 70       assert( n<p
2d0ea 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b  Reader->nData );
2d0eb 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 31 20  .      while( 1 
2d0ec 29 7b 0a 20 20 20 20 20 20 20 20 6e 20 2b 3d 20  ){.        n += 
2d0ed 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
2d0ee 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e  pReader->pData+n
2d0ef 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20  , &iDummy);.    
2d0f0 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c 3d 70      assert( n<=p
2d0f1 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b  Reader->nData );
2d0f2 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 44 75  .        if( iDu
2d0f3 6d 6d 79 3d 3d 50 4f 53 5f 45 4e 44 20 29 20 62  mmy==POS_END ) b
2d0f4 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 69 66  reak;.        if
2d0f5 28 20 69 44 75 6d 6d 79 3d 3d 50 4f 53 5f 43 4f  ( iDummy==POS_CO
2d0f6 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20 20 20  LUMN ){.        
2d0f7 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61    n += fts3GetVa
2d0f8 72 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e  rint32(pReader->
2d0f9 70 44 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79  pData+n, &iDummy
2d0fa 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  );.          ass
2d0fb 65 72 74 28 20 6e 3c 70 52 65 61 64 65 72 2d 3e  ert( n<pReader->
2d0fc 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 20 20 20  nData );.       
2d0fd 20 7d 65 6c 73 65 20 69 66 28 20 70 52 65 61 64   }else if( pRead
2d0fe 65 72 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 50 4f  er->iType==DL_PO
2d0ff 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20  SITIONS_OFFSETS 
2d100 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b  ){.          n +
2d101 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33  = fts3GetVarint3
2d102 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61  2(pReader->pData
2d103 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20  +n, &iDummy);.  
2d104 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66 74 73          n += fts
2d105 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65  3GetVarint32(pRe
2d106 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26  ader->pData+n, &
2d107 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20  iDummy);.       
2d108 20 20 20 61 73 73 65 72 74 28 20 6e 3c 70 52 65     assert( n<pRe
2d109 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20  ader->nData );. 
2d10a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
2d10b 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 65 61 64  .    }.    pRead
2d10c 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 20 3d 20 6e  er->nElement = n
2d10d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 52  ;.    assert( pR
2d10e 65 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 3c  eader->nElement<
2d10f 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20  =pReader->nData 
2d110 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20  );.  }.}.static 
2d111 76 6f 69 64 20 64 6c 72 49 6e 69 74 28 44 4c 52  void dlrInit(DLR
2d112 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 2c 20  eader *pReader, 
2d113 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79 70  DocListType iTyp
2d114 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e,.             
2d115 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
2d116 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44  r *pData, int nD
2d117 61 74 61 29 7b 0a 20 20 61 73 73 65 72 74 28 20  ata){.  assert( 
2d118 70 44 61 74 61 21 3d 4e 55 4c 4c 20 26 26 20 6e  pData!=NULL && n
2d119 44 61 74 61 21 3d 30 20 29 3b 0a 20 20 70 52 65  Data!=0 );.  pRe
2d11a 61 64 65 72 2d 3e 69 54 79 70 65 20 3d 20 69 54  ader->iType = iT
2d11b 79 70 65 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e  ype;.  pReader->
2d11c 70 44 61 74 61 20 3d 20 70 44 61 74 61 3b 0a 20  pData = pData;. 
2d11d 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20   pReader->nData 
2d11e 3d 20 6e 44 61 74 61 3b 0a 20 20 70 52 65 61 64  = nData;.  pRead
2d11f 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 20 3d 20 30  er->nElement = 0
2d120 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 44 6f  ;.  pReader->iDo
2d121 63 69 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 4c  cid = 0;..  /* L
2d122 6f 61 64 20 74 68 65 20 66 69 72 73 74 20 65 6c  oad the first el
2d123 65 6d 65 6e 74 27 73 20 64 61 74 61 2e 20 20 54  ement's data.  T
2d124 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 20 66  here must be a f
2d125 69 72 73 74 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f  irst element. */
2d126 0a 20 20 64 6c 72 53 74 65 70 28 70 52 65 61 64  .  dlrStep(pRead
2d127 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  er);.}.static vo
2d128 69 64 20 64 6c 72 44 65 73 74 72 6f 79 28 44 4c  id dlrDestroy(DL
2d129 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
2d12a 7b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 52 65  {.  SCRAMBLE(pRe
2d12b 61 64 65 72 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65  ader);.}..#ifnde
2d12c 66 20 4e 44 45 42 55 47 0a 2f 2a 20 56 65 72 69  f NDEBUG./* Veri
2d12d 66 79 20 74 68 61 74 20 74 68 65 20 64 6f 63 6c  fy that the docl
2d12e 69 73 74 20 63 61 6e 20 62 65 20 76 61 6c 69 64  ist can be valid
2d12f 6c 79 20 64 65 63 6f 64 65 64 2e 20 20 41 6c 73  ly decoded.  Als
2d130 6f 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a  o returns the.**
2d131 20 6c 61 73 74 20 64 6f 63 69 64 20 66 6f 75 6e   last docid foun
2d132 64 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20  d because it is 
2d133 63 6f 6e 76 65 6e 69 65 6e 74 20 69 6e 20 6f 74  convenient in ot
2d134 68 65 72 20 61 73 73 65 72 74 69 6f 6e 73 20 66  her assertions f
2d135 6f 72 0a 2a 2a 20 44 4c 57 72 69 74 65 72 2e 0a  or.** DLWriter..
2d136 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  */.static void d
2d137 6f 63 4c 69 73 74 56 61 6c 69 64 61 74 65 28 44  ocListValidate(D
2d138 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79 70 65  ocListType iType
2d139 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44  , const char *pD
2d13a 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a  ata, int nData,.
2d13b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d13c 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2d13d 74 65 5f 69 6e 74 36 34 20 2a 70 4c 61 73 74 44  te_int64 *pLastD
2d13e 6f 63 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 5f  ocid){.  sqlite_
2d13f 69 6e 74 36 34 20 69 50 72 65 76 44 6f 63 69 64  int64 iPrevDocid
2d140 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
2d141 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 61 73 73  nData>0 );.  ass
2d142 65 72 74 28 20 70 44 61 74 61 21 3d 30 20 29 3b  ert( pData!=0 );
2d143 0a 20 20 61 73 73 65 72 74 28 20 70 44 61 74 61  .  assert( pData
2d144 2b 6e 44 61 74 61 3e 70 44 61 74 61 20 29 3b 0a  +nData>pData );.
2d145 20 20 77 68 69 6c 65 28 20 6e 44 61 74 61 21 3d    while( nData!=
2d146 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 5f  0 ){.    sqlite_
2d147 69 6e 74 36 34 20 69 44 6f 63 69 64 44 65 6c 74  int64 iDocidDelt
2d148 61 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 66  a;.    int n = f
2d149 74 73 33 47 65 74 56 61 72 69 6e 74 28 70 44 61  ts3GetVarint(pDa
2d14a 74 61 2c 20 26 69 44 6f 63 69 64 44 65 6c 74 61  ta, &iDocidDelta
2d14b 29 3b 0a 20 20 20 20 69 50 72 65 76 44 6f 63 69  );.    iPrevDoci
2d14c 64 20 2b 3d 20 69 44 6f 63 69 64 44 65 6c 74 61  d += iDocidDelta
2d14d 3b 0a 20 20 20 20 69 66 28 20 69 54 79 70 65 3e  ;.    if( iType>
2d14e 44 4c 5f 44 4f 43 49 44 53 20 29 7b 0a 20 20 20  DL_DOCIDS ){.   
2d14f 20 20 20 69 6e 74 20 69 44 75 6d 6d 79 3b 0a 20     int iDummy;. 
2d150 20 20 20 20 20 77 68 69 6c 65 28 20 31 20 29 7b       while( 1 ){
2d151 0a 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66 74  .        n += ft
2d152 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 44  s3GetVarint32(pD
2d153 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b  ata+n, &iDummy);
2d154 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 44 75  .        if( iDu
2d155 6d 6d 79 3d 3d 50 4f 53 5f 45 4e 44 20 29 20 62  mmy==POS_END ) b
2d156 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 69 66  reak;.        if
2d157 28 20 69 44 75 6d 6d 79 3d 3d 50 4f 53 5f 43 4f  ( iDummy==POS_CO
2d158 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20 20 20  LUMN ){.        
2d159 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61    n += fts3GetVa
2d15a 72 69 6e 74 33 32 28 70 44 61 74 61 2b 6e 2c 20  rint32(pData+n, 
2d15b 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20  &iDummy);.      
2d15c 20 20 7d 65 6c 73 65 20 69 66 28 20 69 54 79 70    }else if( iTyp
2d15d 65 3e 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 29  e>DL_POSITIONS )
2d15e 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d  {.          n +=
2d15f 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
2d160 28 70 44 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d  (pData+n, &iDumm
2d161 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 20  y);.          n 
2d162 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74  += fts3GetVarint
2d163 33 32 28 70 44 61 74 61 2b 6e 2c 20 26 69 44 75  32(pData+n, &iDu
2d164 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  mmy);.        }.
2d165 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2d166 6e 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20 20 20  n<=nData );.    
2d167 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73    }.    }.    as
2d168 73 65 72 74 28 20 6e 3c 3d 6e 44 61 74 61 20 29  sert( n<=nData )
2d169 3b 0a 20 20 20 20 70 44 61 74 61 20 2b 3d 20 6e  ;.    pData += n
2d16a 3b 0a 20 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e  ;.    nData -= n
2d16b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4c 61 73  ;.  }.  if( pLas
2d16c 74 44 6f 63 69 64 20 29 20 2a 70 4c 61 73 74 44  tDocid ) *pLastD
2d16d 6f 63 69 64 20 3d 20 69 50 72 65 76 44 6f 63 69  ocid = iPrevDoci
2d16e 64 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 41 53 53  d;.}.#define ASS
2d16f 45 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c 49 53  ERT_VALID_DOCLIS
2d170 54 28 69 2c 20 70 2c 20 6e 2c 20 6f 29 20 64 6f  T(i, p, n, o) do
2d171 63 4c 69 73 74 56 61 6c 69 64 61 74 65 28 69 2c  cListValidate(i,
2d172 20 70 2c 20 6e 2c 20 6f 29 0a 23 65 6c 73 65 0a   p, n, o).#else.
2d173 23 64 65 66 69 6e 65 20 41 53 53 45 52 54 5f 56  #define ASSERT_V
2d174 41 4c 49 44 5f 44 4f 43 4c 49 53 54 28 69 2c 20  ALID_DOCLIST(i, 
2d175 70 2c 20 6e 2c 20 6f 29 20 61 73 73 65 72 74 28  p, n, o) assert(
2d176 20 31 20 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a   1 ).#endif../**
2d177 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d178 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d179 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d17a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d17b 2a 2f 0a 2f 2a 20 44 4c 57 72 69 74 65 72 20 69  */./* DLWriter i
2d17c 73 20 75 73 65 64 20 74 6f 20 77 72 69 74 65 20  s used to write 
2d17d 64 6f 63 6c 69 73 74 20 64 61 74 61 20 74 6f 20  doclist data to 
2d17e 61 20 44 61 74 61 42 75 66 66 65 72 2e 20 20 44  a DataBuffer.  D
2d17f 4c 57 72 69 74 65 72 0a 2a 2a 20 61 6c 77 61 79  LWriter.** alway
2d180 73 20 61 70 70 65 6e 64 73 20 74 6f 20 74 68 65  s appends to the
2d181 20 62 75 66 66 65 72 20 61 6e 64 20 64 6f 65 73   buffer and does
2d182 20 6e 6f 74 20 6f 77 6e 20 69 74 2e 0a 2a 2a 0a   not own it..**.
2d183 2a 2a 20 64 6c 77 49 6e 69 74 20 2d 20 69 6e 69  ** dlwInit - ini
2d184 74 69 61 6c 69 7a 65 20 74 6f 20 77 72 69 74 65  tialize to write
2d185 20 61 20 67 69 76 65 6e 20 74 79 70 65 20 64 6f   a given type do
2d186 63 6c 69 73 74 74 6f 20 61 20 62 75 66 66 65 72  clistto a buffer
2d187 2e 0a 2a 2a 20 64 6c 77 44 65 73 74 72 6f 79 20  ..** dlwDestroy 
2d188 2d 20 63 6c 65 61 72 20 74 68 65 20 77 72 69 74  - clear the writ
2d189 65 72 27 73 20 6d 65 6d 6f 72 79 2e 20 20 44 6f  er's memory.  Do
2d18a 65 73 20 6e 6f 74 20 66 72 65 65 20 62 75 66 66  es not free buff
2d18b 65 72 2e 0a 2a 2a 20 64 6c 77 41 70 70 65 6e 64  er..** dlwAppend
2d18c 20 2d 20 61 70 70 65 6e 64 20 72 61 77 20 64 6f   - append raw do
2d18d 63 6c 69 73 74 20 64 61 74 61 20 74 6f 20 62 75  clist data to bu
2d18e 66 66 65 72 2e 0a 2a 2a 20 64 6c 77 43 6f 70 79  ffer..** dlwCopy
2d18f 20 2d 20 63 6f 70 79 20 6e 65 78 74 20 64 6f 63   - copy next doc
2d190 6c 69 73 74 20 66 72 6f 6d 20 72 65 61 64 65 72  list from reader
2d191 20 74 6f 20 77 72 69 74 65 72 2e 0a 2a 2a 20 64   to writer..** d
2d192 6c 77 41 64 64 20 2d 20 63 6f 6e 73 74 72 75 63  lwAdd - construc
2d193 74 20 64 6f 63 6c 69 73 74 20 65 6c 65 6d 65 6e  t doclist elemen
2d194 74 20 61 6e 64 20 61 70 70 65 6e 64 20 74 6f 20  t and append to 
2d195 62 75 66 66 65 72 2e 0a 2a 2a 20 20 20 20 4f 6e  buffer..**    On
2d196 6c 79 20 61 70 70 6c 79 20 64 6c 77 41 64 64 28  ly apply dlwAdd(
2d197 29 20 74 6f 20 44 4c 5f 44 4f 43 49 44 53 20 64  ) to DL_DOCIDS d
2d198 6f 63 6c 69 73 74 73 20 28 65 6c 73 65 20 75 73  oclists (else us
2d199 65 20 50 4c 57 72 69 74 65 72 29 2e 0a 2a 2f 0a  e PLWriter)..*/.
2d19a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 44  typedef struct D
2d19b 4c 57 72 69 74 65 72 20 7b 0a 20 20 44 6f 63 4c  LWriter {.  DocL
2d19c 69 73 74 54 79 70 65 20 69 54 79 70 65 3b 0a 20  istType iType;. 
2d19d 20 44 61 74 61 42 75 66 66 65 72 20 2a 62 3b 0a   DataBuffer *b;.
2d19e 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
2d19f 50 72 65 76 44 6f 63 69 64 3b 0a 23 69 66 6e 64  PrevDocid;.#ifnd
2d1a0 65 66 20 4e 44 45 42 55 47 0a 20 20 69 6e 74 20  ef NDEBUG.  int 
2d1a1 68 61 73 5f 69 50 72 65 76 44 6f 63 69 64 3b 0a  has_iPrevDocid;.
2d1a2 23 65 6e 64 69 66 0a 7d 20 44 4c 57 72 69 74 65  #endif.} DLWrite
2d1a3 72 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  r;..static void 
2d1a4 64 6c 77 49 6e 69 74 28 44 4c 57 72 69 74 65 72  dlwInit(DLWriter
2d1a5 20 2a 70 57 72 69 74 65 72 2c 20 44 6f 63 4c 69   *pWriter, DocLi
2d1a6 73 74 54 79 70 65 20 69 54 79 70 65 2c 20 44 61  stType iType, Da
2d1a7 74 61 42 75 66 66 65 72 20 2a 62 29 7b 0a 20 20  taBuffer *b){.  
2d1a8 70 57 72 69 74 65 72 2d 3e 62 20 3d 20 62 3b 0a  pWriter->b = b;.
2d1a9 20 20 70 57 72 69 74 65 72 2d 3e 69 54 79 70 65    pWriter->iType
2d1aa 20 3d 20 69 54 79 70 65 3b 0a 20 20 70 57 72 69   = iType;.  pWri
2d1ab 74 65 72 2d 3e 69 50 72 65 76 44 6f 63 69 64 20  ter->iPrevDocid 
2d1ac 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45  = 0;.#ifndef NDE
2d1ad 42 55 47 0a 20 20 70 57 72 69 74 65 72 2d 3e 68  BUG.  pWriter->h
2d1ae 61 73 5f 69 50 72 65 76 44 6f 63 69 64 20 3d 20  as_iPrevDocid = 
2d1af 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 73 74 61 74  0;.#endif.}.stat
2d1b0 69 63 20 76 6f 69 64 20 64 6c 77 44 65 73 74 72  ic void dlwDestr
2d1b1 6f 79 28 44 4c 57 72 69 74 65 72 20 2a 70 57 72  oy(DLWriter *pWr
2d1b2 69 74 65 72 29 7b 0a 20 20 53 43 52 41 4d 42 4c  iter){.  SCRAMBL
2d1b3 45 28 70 57 72 69 74 65 72 29 3b 0a 7d 0a 2f 2a  E(pWriter);.}./*
2d1b4 20 69 46 69 72 73 74 44 6f 63 69 64 20 69 73 20   iFirstDocid is 
2d1b5 74 68 65 20 66 69 72 73 74 20 64 6f 63 69 64 20  the first docid 
2d1b6 69 6e 20 74 68 65 20 64 6f 63 6c 69 73 74 20 69  in the doclist i
2d1b7 6e 20 70 44 61 74 61 2e 20 20 49 74 20 69 73 0a  n pData.  It is.
2d1b8 2a 2a 20 6e 65 65 64 65 64 20 62 65 63 61 75 73  ** needed becaus
2d1b9 65 20 70 44 61 74 61 20 6d 61 79 20 70 6f 69 6e  e pData may poin
2d1ba 74 20 77 69 74 68 69 6e 20 61 20 6c 61 72 67 65  t within a large
2d1bb 72 20 64 6f 63 6c 69 73 74 2c 20 69 6e 20 77 68  r doclist, in wh
2d1bc 69 63 68 0a 2a 2a 20 63 61 73 65 20 74 68 65 20  ich.** case the 
2d1bd 66 69 72 73 74 20 69 74 65 6d 20 77 6f 75 6c 64  first item would
2d1be 20 62 65 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65   be delta-encode
2d1bf 64 2e 0a 2a 2a 0a 2a 2a 20 69 4c 61 73 74 44 6f  d..**.** iLastDo
2d1c0 63 69 64 20 69 73 20 74 68 65 20 66 69 6e 61 6c  cid is the final
2d1c1 20 64 6f 63 69 64 20 69 6e 20 74 68 65 20 64 6f   docid in the do
2d1c2 63 6c 69 73 74 20 69 6e 20 70 44 61 74 61 2e 20  clist in pData. 
2d1c3 20 49 74 20 69 73 0a 2a 2a 20 6e 65 65 64 65 64   It is.** needed
2d1c4 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 6e   to create the n
2d1c5 65 77 20 69 50 72 65 76 44 6f 63 69 64 20 66 6f  ew iPrevDocid fo
2d1c6 72 20 66 75 74 75 72 65 20 64 65 6c 74 61 2d 65  r future delta-e
2d1c7 6e 63 6f 64 69 6e 67 2e 20 20 54 68 65 0a 2a 2a  ncoding.  The.**
2d1c8 20 63 6f 64 65 20 63 6f 75 6c 64 20 64 65 63 6f   code could deco
2d1c9 64 65 20 74 68 65 20 70 61 73 73 65 64 20 64 6f  de the passed do
2d1ca 63 6c 69 73 74 20 74 6f 20 72 65 63 72 65 61 74  clist to recreat
2d1cb 65 20 69 4c 61 73 74 44 6f 63 69 64 2c 20 62 75  e iLastDocid, bu
2d1cc 74 0a 2a 2a 20 74 68 65 20 6f 6e 6c 79 20 63 75  t.** the only cu
2d1cd 72 72 65 6e 74 20 75 73 65 72 20 28 64 6f 63 4c  rrent user (docL
2d1ce 69 73 74 4d 65 72 67 65 29 20 61 6c 72 65 61 64  istMerge) alread
2d1cf 79 20 68 61 73 20 64 65 63 6f 64 65 64 20 74 68  y has decoded th
2d1d0 69 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f  is.** informatio
2d1d1 6e 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68  n..*/./* TODO(sh
2d1d2 65 73 73 29 20 54 68 69 73 20 68 61 73 20 62 65  ess) This has be
2d1d3 63 6f 6d 65 20 6a 75 73 74 20 61 20 68 65 6c 70  come just a help
2d1d4 65 72 20 66 6f 72 20 64 6f 63 4c 69 73 74 4d 65  er for docListMe
2d1d5 72 67 65 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72  rge..** Consider
2d1d6 20 61 20 72 65 66 61 63 74 6f 72 20 74 6f 20 6d   a refactor to m
2d1d7 61 6b 65 20 74 68 69 73 20 63 6c 65 61 6e 65 72  ake this cleaner
2d1d8 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2d1d9 20 64 6c 77 41 70 70 65 6e 64 28 44 4c 57 72 69   dlwAppend(DLWri
2d1da 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20  ter *pWriter,.  
2d1db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d1dc 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2d1dd 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61  pData, int nData
2d1de 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2d1df 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69          sqlite_i
2d1e0 6e 74 36 34 20 69 46 69 72 73 74 44 6f 63 69 64  nt64 iFirstDocid
2d1e1 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  , sqlite_int64 i
2d1e2 4c 61 73 74 44 6f 63 69 64 29 7b 0a 20 20 73 71  LastDocid){.  sq
2d1e3 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69  lite_int64 iDoci
2d1e4 64 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 5b  d = 0;.  char c[
2d1e5 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69  VARINT_MAX];.  i
2d1e6 6e 74 20 6e 46 69 72 73 74 4f 6c 64 2c 20 6e 46  nt nFirstOld, nF
2d1e7 69 72 73 74 4e 65 77 3b 20 20 20 20 20 2f 2a 20  irstNew;     /* 
2d1e8 4f 6c 64 20 61 6e 64 20 6e 65 77 20 76 61 72 69  Old and new vari
2d1e9 6e 74 20 6c 65 6e 20 6f 66 20 66 69 72 73 74 20  nt len of first 
2d1ea 64 6f 63 69 64 2e 20 2a 2f 0a 23 69 66 6e 64 65  docid. */.#ifnde
2d1eb 66 20 4e 44 45 42 55 47 0a 20 20 73 71 6c 69 74  f NDEBUG.  sqlit
2d1ec 65 5f 69 6e 74 36 34 20 69 4c 61 73 74 44 6f 63  e_int64 iLastDoc
2d1ed 69 64 44 65 6c 74 61 3b 0a 23 65 6e 64 69 66 0a  idDelta;.#endif.
2d1ee 0a 20 20 2f 2a 20 52 65 63 6f 64 65 20 74 68 65  .  /* Recode the
2d1ef 20 69 6e 69 74 69 61 6c 20 64 6f 63 69 64 20 61   initial docid a
2d1f0 73 20 64 65 6c 74 61 20 66 72 6f 6d 20 69 50 72  s delta from iPr
2d1f1 65 76 44 6f 63 69 64 2e 20 2a 2f 0a 20 20 6e 46  evDocid. */.  nF
2d1f2 69 72 73 74 4f 6c 64 20 3d 20 66 74 73 33 47 65  irstOld = fts3Ge
2d1f3 74 56 61 72 69 6e 74 28 70 44 61 74 61 2c 20 26  tVarint(pData, &
2d1f4 69 44 6f 63 69 64 29 3b 0a 20 20 61 73 73 65 72  iDocid);.  asser
2d1f5 74 28 20 6e 46 69 72 73 74 4f 6c 64 3c 6e 44 61  t( nFirstOld<nDa
2d1f6 74 61 20 7c 7c 20 28 6e 46 69 72 73 74 4f 6c 64  ta || (nFirstOld
2d1f7 3d 3d 6e 44 61 74 61 20 26 26 20 70 57 72 69 74  ==nData && pWrit
2d1f8 65 72 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 44 4f  er->iType==DL_DO
2d1f9 43 49 44 53 29 20 29 3b 0a 20 20 6e 46 69 72 73  CIDS) );.  nFirs
2d1fa 74 4e 65 77 20 3d 20 66 74 73 33 50 75 74 56 61  tNew = fts3PutVa
2d1fb 72 69 6e 74 28 63 2c 20 69 46 69 72 73 74 44 6f  rint(c, iFirstDo
2d1fc 63 69 64 2d 70 57 72 69 74 65 72 2d 3e 69 50 72  cid-pWriter->iPr
2d1fd 65 76 44 6f 63 69 64 29 3b 0a 0a 20 20 2f 2a 20  evDocid);..  /* 
2d1fe 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20  Verify that the 
2d1ff 69 6e 63 6f 6d 69 6e 67 20 64 6f 63 6c 69 73 74  incoming doclist
2d200 20 69 73 20 76 61 6c 69 64 20 41 4e 44 20 74 68   is valid AND th
2d201 61 74 20 69 74 20 65 6e 64 73 20 77 69 74 68 0a  at it ends with.
2d202 20 20 2a 2a 20 74 68 65 20 65 78 70 65 63 74 65    ** the expecte
2d203 64 20 64 6f 63 69 64 2e 20 20 54 68 69 73 20 69  d docid.  This i
2d204 73 20 65 73 73 65 6e 74 69 61 6c 20 62 65 63 61  s essential beca
2d205 75 73 65 20 77 65 27 6c 6c 20 74 72 75 73 74 20  use we'll trust 
2d206 74 68 69 73 0a 20 20 2a 2a 20 64 6f 63 69 64 20  this.  ** docid 
2d207 69 6e 20 66 75 74 75 72 65 20 64 65 6c 74 61 2d  in future delta-
2d208 65 6e 63 6f 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20  encoding..  */. 
2d209 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f   ASSERT_VALID_DO
2d20a 43 4c 49 53 54 28 70 57 72 69 74 65 72 2d 3e 69  CLIST(pWriter->i
2d20b 54 79 70 65 2c 20 70 44 61 74 61 2c 20 6e 44 61  Type, pData, nDa
2d20c 74 61 2c 20 26 69 4c 61 73 74 44 6f 63 69 64 44  ta, &iLastDocidD
2d20d 65 6c 74 61 29 3b 0a 20 20 61 73 73 65 72 74 28  elta);.  assert(
2d20e 20 69 4c 61 73 74 44 6f 63 69 64 3d 3d 69 46 69   iLastDocid==iFi
2d20f 72 73 74 44 6f 63 69 64 2d 69 44 6f 63 69 64 2b  rstDocid-iDocid+
2d210 69 4c 61 73 74 44 6f 63 69 64 44 65 6c 74 61 20  iLastDocidDelta 
2d211 29 3b 0a 0a 20 20 2f 2a 20 41 70 70 65 6e 64 20  );..  /* Append 
2d212 72 65 63 6f 64 65 64 20 69 6e 69 74 69 61 6c 20  recoded initial 
2d213 64 6f 63 69 64 20 61 6e 64 20 65 76 65 72 79 74  docid and everyt
2d214 68 69 6e 67 20 65 6c 73 65 2e 20 20 52 65 73 74  hing else.  Rest
2d215 20 6f 66 20 64 6f 63 69 64 73 0a 20 20 2a 2a 20   of docids.  ** 
2d216 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e  should have been
2d217 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64 20 66   delta-encoded f
2d218 72 6f 6d 20 70 72 65 76 69 6f 75 73 20 69 6e 69  rom previous ini
2d219 74 69 61 6c 20 64 6f 63 69 64 2e 0a 20 20 2a 2f  tial docid..  */
2d21a 0a 20 20 69 66 28 20 6e 46 69 72 73 74 4f 6c 64  .  if( nFirstOld
2d21b 3c 6e 44 61 74 61 20 29 7b 0a 20 20 20 20 64 61  <nData ){.    da
2d21c 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32 28  taBufferAppend2(
2d21d 70 57 72 69 74 65 72 2d 3e 62 2c 20 63 2c 20 6e  pWriter->b, c, n
2d21e 46 69 72 73 74 4e 65 77 2c 0a 20 20 20 20 20 20  FirstNew,.      
2d21f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d220 70 44 61 74 61 2b 6e 46 69 72 73 74 4f 6c 64 2c  pData+nFirstOld,
2d221 20 6e 44 61 74 61 2d 6e 46 69 72 73 74 4f 6c 64   nData-nFirstOld
2d222 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
2d223 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
2d224 28 70 57 72 69 74 65 72 2d 3e 62 2c 20 63 2c 20  (pWriter->b, c, 
2d225 6e 46 69 72 73 74 4e 65 77 29 3b 0a 20 20 7d 0a  nFirstNew);.  }.
2d226 20 20 70 57 72 69 74 65 72 2d 3e 69 50 72 65 76    pWriter->iPrev
2d227 44 6f 63 69 64 20 3d 20 69 4c 61 73 74 44 6f 63  Docid = iLastDoc
2d228 69 64 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69  id;.}.static voi
2d229 64 20 64 6c 77 43 6f 70 79 28 44 4c 57 72 69 74  d dlwCopy(DLWrit
2d22a 65 72 20 2a 70 57 72 69 74 65 72 2c 20 44 4c 52  er *pWriter, DLR
2d22b 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
2d22c 0a 20 20 64 6c 77 41 70 70 65 6e 64 28 70 57 72  .  dlwAppend(pWr
2d22d 69 74 65 72 2c 20 64 6c 72 44 6f 63 44 61 74 61  iter, dlrDocData
2d22e 28 70 52 65 61 64 65 72 29 2c 20 64 6c 72 44 6f  (pReader), dlrDo
2d22f 63 44 61 74 61 42 79 74 65 73 28 70 52 65 61 64  cDataBytes(pRead
2d230 65 72 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  er),.           
2d231 20 64 6c 72 44 6f 63 69 64 28 70 52 65 61 64 65   dlrDocid(pReade
2d232 72 29 2c 20 64 6c 72 44 6f 63 69 64 28 70 52 65  r), dlrDocid(pRe
2d233 61 64 65 72 29 29 3b 0a 7d 0a 73 74 61 74 69 63  ader));.}.static
2d234 20 76 6f 69 64 20 64 6c 77 41 64 64 28 44 4c 57   void dlwAdd(DLW
2d235 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c 20  riter *pWriter, 
2d236 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f  sqlite_int64 iDo
2d237 63 69 64 29 7b 0a 20 20 63 68 61 72 20 63 5b 56  cid){.  char c[V
2d238 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e  ARINT_MAX];.  in
2d239 74 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72  t n = fts3PutVar
2d23a 69 6e 74 28 63 2c 20 69 44 6f 63 69 64 2d 70 57  int(c, iDocid-pW
2d23b 72 69 74 65 72 2d 3e 69 50 72 65 76 44 6f 63 69  riter->iPrevDoci
2d23c 64 29 3b 0a 0a 20 20 2f 2a 20 44 6f 63 69 64 73  d);..  /* Docids
2d23d 20 6d 75 73 74 20 61 73 63 65 6e 64 2e 20 2a 2f   must ascend. */
2d23e 0a 20 20 61 73 73 65 72 74 28 20 21 70 57 72 69  .  assert( !pWri
2d23f 74 65 72 2d 3e 68 61 73 5f 69 50 72 65 76 44 6f  ter->has_iPrevDo
2d240 63 69 64 20 7c 7c 20 69 44 6f 63 69 64 3e 70 57  cid || iDocid>pW
2d241 72 69 74 65 72 2d 3e 69 50 72 65 76 44 6f 63 69  riter->iPrevDoci
2d242 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  d );.  assert( p
2d243 57 72 69 74 65 72 2d 3e 69 54 79 70 65 3d 3d 44  Writer->iType==D
2d244 4c 5f 44 4f 43 49 44 53 20 29 3b 0a 0a 20 20 64  L_DOCIDS );..  d
2d245 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28  ataBufferAppend(
2d246 70 57 72 69 74 65 72 2d 3e 62 2c 20 63 2c 20 6e  pWriter->b, c, n
2d247 29 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 50  );.  pWriter->iP
2d248 72 65 76 44 6f 63 69 64 20 3d 20 69 44 6f 63 69  revDocid = iDoci
2d249 64 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  d;.#ifndef NDEBU
2d24a 47 0a 20 20 70 57 72 69 74 65 72 2d 3e 68 61 73  G.  pWriter->has
2d24b 5f 69 50 72 65 76 44 6f 63 69 64 20 3d 20 31 3b  _iPrevDocid = 1;
2d24c 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 2a 2a 2a  .#endif.}../****
2d24d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d24e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d24f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2d251 0a 2f 2a 20 50 4c 52 65 61 64 65 72 20 69 73 20  ./* PLReader is 
2d252 75 73 65 64 20 74 6f 20 72 65 61 64 20 64 61 74  used to read dat
2d253 61 20 66 72 6f 6d 20 61 20 64 6f 63 75 6d 65 6e  a from a documen
2d254 74 27 73 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73  t's position lis
2d255 74 2e 20 20 41 73 0a 2a 2a 20 74 68 65 20 63 61  t.  As.** the ca
2d256 6c 6c 65 72 20 73 74 65 70 73 20 74 68 72 6f 75  ller steps throu
2d257 67 68 20 74 68 65 20 6c 69 73 74 2c 20 64 61 74  gh the list, dat
2d258 61 20 69 73 20 63 61 63 68 65 64 20 73 6f 20 74  a is cached so t
2d259 68 61 74 20 76 61 72 69 6e 74 73 0a 2a 2a 20 6f  hat varints.** o
2d25a 6e 6c 79 20 6e 65 65 64 20 74 6f 20 62 65 20 64  nly need to be d
2d25b 65 63 6f 64 65 64 20 6f 6e 63 65 2e 0a 2a 2a 0a  ecoded once..**.
2d25c 2a 2a 20 70 6c 72 49 6e 69 74 2c 20 70 6c 72 44  ** plrInit, plrD
2d25d 65 73 74 72 6f 79 20 2d 20 63 72 65 61 74 65 2f  estroy - create/
2d25e 64 65 73 74 72 6f 79 20 61 20 72 65 61 64 65 72  destroy a reader
2d25f 2e 0a 2a 2a 20 70 6c 72 43 6f 6c 75 6d 6e 2c 20  ..** plrColumn, 
2d260 70 6c 72 50 6f 73 69 74 69 6f 6e 2c 20 70 6c 72  plrPosition, plr
2d261 53 74 61 72 74 4f 66 66 73 65 74 2c 20 70 6c 72  StartOffset, plr
2d262 45 6e 64 4f 66 66 73 65 74 20 2d 20 61 63 63 65  EndOffset - acce
2d263 73 73 6f 72 73 0a 2a 2a 20 70 6c 72 41 74 45 6e  ssors.** plrAtEn
2d264 64 20 2d 20 61 74 20 65 6e 64 20 6f 66 20 73 74  d - at end of st
2d265 72 65 61 6d 2c 20 6f 6e 6c 79 20 63 61 6c 6c 20  ream, only call 
2d266 70 6c 72 44 65 73 74 72 6f 79 20 6f 6e 63 65 20  plrDestroy once 
2d267 74 72 75 65 2e 0a 2a 2a 20 70 6c 72 53 74 65 70  true..** plrStep
2d268 20 2d 20 73 74 65 70 20 74 6f 20 74 68 65 20 6e   - step to the n
2d269 65 78 74 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a  ext element..*/.
2d26a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50  typedef struct P
2d26b 4c 52 65 61 64 65 72 20 7b 0a 20 20 2f 2a 20 54  LReader {.  /* T
2d26c 68 65 73 65 20 72 65 66 65 72 20 74 6f 20 74 68  hese refer to th
2d26d 65 20 6e 65 78 74 20 70 6f 73 69 74 69 6f 6e 27  e next position'
2d26e 73 20 64 61 74 61 2e 20 20 6e 44 61 74 61 20 77  s data.  nData w
2d26f 69 6c 6c 20 72 65 61 63 68 20 30 20 77 68 65 6e  ill reach 0 when
2d270 0a 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 74 68  .  ** reading th
2d271 65 20 6c 61 73 74 20 70 6f 73 69 74 69 6f 6e 2c  e last position,
2d272 20 73 6f 20 70 6c 72 53 74 65 70 28 29 20 73 69   so plrStep() si
2d273 67 6e 61 6c 73 20 45 4f 46 20 62 79 20 73 65 74  gnals EOF by set
2d274 74 69 6e 67 0a 20 20 2a 2a 20 70 44 61 74 61 20  ting.  ** pData 
2d275 74 6f 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20  to NULL..  */.  
2d276 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
2d277 61 3b 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a  a;.  int nData;.
2d278 0a 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69  .  DocListType i
2d279 54 79 70 65 3b 0a 20 20 69 6e 74 20 69 43 6f 6c  Type;.  int iCol
2d27a 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  umn;         /* 
2d27b 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20  the last column 
2d27c 72 65 61 64 20 2a 2f 0a 20 20 69 6e 74 20 69 50  read */.  int iP
2d27d 6f 73 69 74 69 6f 6e 3b 20 20 20 20 20 20 20 2f  osition;       /
2d27e 2a 20 74 68 65 20 6c 61 73 74 20 70 6f 73 69 74  * the last posit
2d27f 69 6f 6e 20 72 65 61 64 20 2a 2f 0a 20 20 69 6e  ion read */.  in
2d280 74 20 69 53 74 61 72 74 4f 66 66 73 65 74 3b 20  t iStartOffset; 
2d281 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74 20 73     /* the last s
2d282 74 61 72 74 20 6f 66 66 73 65 74 20 72 65 61 64  tart offset read
2d283 20 2a 2f 0a 20 20 69 6e 74 20 69 45 6e 64 4f 66   */.  int iEndOf
2d284 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 74 68  fset;      /* th
2d285 65 20 6c 61 73 74 20 65 6e 64 20 6f 66 66 73 65  e last end offse
2d286 74 20 72 65 61 64 20 2a 2f 0a 7d 20 50 4c 52 65  t read */.} PLRe
2d287 61 64 65 72 3b 0a 0a 73 74 61 74 69 63 20 69 6e  ader;..static in
2d288 74 20 70 6c 72 41 74 45 6e 64 28 50 4c 52 65 61  t plrAtEnd(PLRea
2d289 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
2d28a 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d   return pReader-
2d28b 3e 70 44 61 74 61 3d 3d 4e 55 4c 4c 3b 0a 7d 0a  >pData==NULL;.}.
2d28c 73 74 61 74 69 63 20 69 6e 74 20 70 6c 72 43 6f  static int plrCo
2d28d 6c 75 6d 6e 28 50 4c 52 65 61 64 65 72 20 2a 70  lumn(PLReader *p
2d28e 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72  Reader){.  asser
2d28f 74 28 20 21 70 6c 72 41 74 45 6e 64 28 70 52 65  t( !plrAtEnd(pRe
2d290 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72  ader) );.  retur
2d291 6e 20 70 52 65 61 64 65 72 2d 3e 69 43 6f 6c 75  n pReader->iColu
2d292 6d 6e 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  mn;.}.static int
2d293 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 50 4c 52   plrPosition(PLR
2d294 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
2d295 0a 20 20 61 73 73 65 72 74 28 20 21 70 6c 72 41  .  assert( !plrA
2d296 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b  tEnd(pReader) );
2d297 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65  .  return pReade
2d298 72 2d 3e 69 50 6f 73 69 74 69 6f 6e 3b 0a 7d 0a  r->iPosition;.}.
2d299 73 74 61 74 69 63 20 69 6e 74 20 70 6c 72 53 74  static int plrSt
2d29a 61 72 74 4f 66 66 73 65 74 28 50 4c 52 65 61 64  artOffset(PLRead
2d29b 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
2d29c 61 73 73 65 72 74 28 20 21 70 6c 72 41 74 45 6e  assert( !plrAtEn
2d29d 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20  d(pReader) );.  
2d29e 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
2d29f 69 53 74 61 72 74 4f 66 66 73 65 74 3b 0a 7d 0a  iStartOffset;.}.
2d2a0 73 74 61 74 69 63 20 69 6e 74 20 70 6c 72 45 6e  static int plrEn
2d2a1 64 4f 66 66 73 65 74 28 50 4c 52 65 61 64 65 72  dOffset(PLReader
2d2a2 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73   *pReader){.  as
2d2a3 73 65 72 74 28 20 21 70 6c 72 41 74 45 6e 64 28  sert( !plrAtEnd(
2d2a4 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65  pReader) );.  re
2d2a5 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 69 45  turn pReader->iE
2d2a6 6e 64 4f 66 66 73 65 74 3b 0a 7d 0a 73 74 61 74  ndOffset;.}.stat
2d2a7 69 63 20 76 6f 69 64 20 70 6c 72 53 74 65 70 28  ic void plrStep(
2d2a8 50 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  PLReader *pReade
2d2a9 72 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a  r){.  int i, n;.
2d2aa 0a 20 20 61 73 73 65 72 74 28 20 21 70 6c 72 41  .  assert( !plrA
2d2ab 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b  tEnd(pReader) );
2d2ac 0a 0a 20 20 69 66 28 20 70 52 65 61 64 65 72 2d  ..  if( pReader-
2d2ad 3e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20  >nData==0 ){.   
2d2ae 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 20   pReader->pData 
2d2af 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 72 65 74 75  = NULL;.    retu
2d2b0 72 6e 3b 0a 20 20 7d 0a 0a 20 20 6e 20 3d 20 66  rn;.  }..  n = f
2d2b1 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
2d2b2 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26  Reader->pData, &
2d2b3 69 29 3b 0a 20 20 69 66 28 20 69 3d 3d 50 4f 53  i);.  if( i==POS
2d2b4 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 6e  _COLUMN ){.    n
2d2b5 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e   += fts3GetVarin
2d2b6 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61  t32(pReader->pDa
2d2b7 74 61 2b 6e 2c 20 26 70 52 65 61 64 65 72 2d 3e  ta+n, &pReader->
2d2b8 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 70 52  iColumn);.    pR
2d2b9 65 61 64 65 72 2d 3e 69 50 6f 73 69 74 69 6f 6e  eader->iPosition
2d2ba 20 3d 20 30 3b 0a 20 20 20 20 70 52 65 61 64 65   = 0;.    pReade
2d2bb 72 2d 3e 69 53 74 61 72 74 4f 66 66 73 65 74 20  r->iStartOffset 
2d2bc 3d 20 30 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74  = 0;.    n += ft
2d2bd 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52  s3GetVarint32(pR
2d2be 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20  eader->pData+n, 
2d2bf 26 69 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 53 68  &i);.  }.  /* Sh
2d2c0 6f 75 6c 64 20 6e 65 76 65 72 20 73 65 65 20 61  ould never see a
2d2c1 64 6a 61 63 65 6e 74 20 63 6f 6c 75 6d 6e 20 63  djacent column c
2d2c2 68 61 6e 67 65 73 2e 20 2a 2f 0a 20 20 61 73 73  hanges. */.  ass
2d2c3 65 72 74 28 20 69 21 3d 50 4f 53 5f 43 4f 4c 55  ert( i!=POS_COLU
2d2c4 4d 4e 20 29 3b 0a 0a 20 20 69 66 28 20 69 3d 3d  MN );..  if( i==
2d2c5 50 4f 53 5f 45 4e 44 20 29 7b 0a 20 20 20 20 70  POS_END ){.    p
2d2c6 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 3d 20  Reader->nData = 
2d2c7 30 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  0;.    pReader->
2d2c8 70 44 61 74 61 20 3d 20 4e 55 4c 4c 3b 0a 20 20  pData = NULL;.  
2d2c9 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
2d2ca 20 70 52 65 61 64 65 72 2d 3e 69 50 6f 73 69 74   pReader->iPosit
2d2cb 69 6f 6e 20 2b 3d 20 69 2d 50 4f 53 5f 42 41 53  ion += i-POS_BAS
2d2cc 45 3b 0a 20 20 69 66 28 20 70 52 65 61 64 65 72  E;.  if( pReader
2d2cd 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49  ->iType==DL_POSI
2d2ce 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29 7b  TIONS_OFFSETS ){
2d2cf 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65  .    n += fts3Ge
2d2d0 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65  tVarint32(pReade
2d2d1 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69 29 3b  r->pData+n, &i);
2d2d2 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 69 53  .    pReader->iS
2d2d3 74 61 72 74 4f 66 66 73 65 74 20 2b 3d 20 69 3b  tartOffset += i;
2d2d4 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65  .    n += fts3Ge
2d2d5 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65  tVarint32(pReade
2d2d6 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69 29 3b  r->pData+n, &i);
2d2d7 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 69 45  .    pReader->iE
2d2d8 6e 64 4f 66 66 73 65 74 20 3d 20 70 52 65 61 64  ndOffset = pRead
2d2d9 65 72 2d 3e 69 53 74 61 72 74 4f 66 66 73 65 74  er->iStartOffset
2d2da 2b 69 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  +i;.  }.  assert
2d2db 28 20 6e 3c 3d 70 52 65 61 64 65 72 2d 3e 6e 44  ( n<=pReader->nD
2d2dc 61 74 61 20 29 3b 0a 20 20 70 52 65 61 64 65 72  ata );.  pReader
2d2dd 2d 3e 70 44 61 74 61 20 2b 3d 20 6e 3b 0a 20 20  ->pData += n;.  
2d2de 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 2d  pReader->nData -
2d2df 3d 20 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  = n;.}..static v
2d2e0 6f 69 64 20 70 6c 72 49 6e 69 74 28 50 4c 52 65  oid plrInit(PLRe
2d2e1 61 64 65 72 20 2a 70 52 65 61 64 65 72 2c 20 44  ader *pReader, D
2d2e2 4c 52 65 61 64 65 72 20 2a 70 44 4c 52 65 61 64  LReader *pDLRead
2d2e3 65 72 29 7b 0a 20 20 70 52 65 61 64 65 72 2d 3e  er){.  pReader->
2d2e4 70 44 61 74 61 20 3d 20 64 6c 72 50 6f 73 44 61  pData = dlrPosDa
2d2e5 74 61 28 70 44 4c 52 65 61 64 65 72 29 3b 0a 20  ta(pDLReader);. 
2d2e6 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20   pReader->nData 
2d2e7 3d 20 64 6c 72 50 6f 73 44 61 74 61 4c 65 6e 28  = dlrPosDataLen(
2d2e8 70 44 4c 52 65 61 64 65 72 29 3b 0a 20 20 70 52  pDLReader);.  pR
2d2e9 65 61 64 65 72 2d 3e 69 54 79 70 65 20 3d 20 70  eader->iType = p
2d2ea 44 4c 52 65 61 64 65 72 2d 3e 69 54 79 70 65 3b  DLReader->iType;
2d2eb 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 43 6f 6c  .  pReader->iCol
2d2ec 75 6d 6e 20 3d 20 30 3b 0a 20 20 70 52 65 61 64  umn = 0;.  pRead
2d2ed 65 72 2d 3e 69 50 6f 73 69 74 69 6f 6e 20 3d 20  er->iPosition = 
2d2ee 30 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 53  0;.  pReader->iS
2d2ef 74 61 72 74 4f 66 66 73 65 74 20 3d 20 30 3b 0a  tartOffset = 0;.
2d2f0 20 20 70 52 65 61 64 65 72 2d 3e 69 45 6e 64 4f    pReader->iEndO
2d2f1 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 70 6c 72  ffset = 0;.  plr
2d2f2 53 74 65 70 28 70 52 65 61 64 65 72 29 3b 0a 7d  Step(pReader);.}
2d2f3 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 6c 72  .static void plr
2d2f4 44 65 73 74 72 6f 79 28 50 4c 52 65 61 64 65 72  Destroy(PLReader
2d2f5 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 53 43   *pReader){.  SC
2d2f6 52 41 4d 42 4c 45 28 70 52 65 61 64 65 72 29 3b  RAMBLE(pReader);
2d2f7 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
2d2f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d2f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d2fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d2fb 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 50 4c 57  ********/./* PLW
2d2fc 72 69 74 65 72 20 69 73 20 75 73 65 64 20 69 6e  riter is used in
2d2fd 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 61 20   constructing a 
2d2fe 64 6f 63 75 6d 65 6e 74 27 73 20 70 6f 73 69 74  document's posit
2d2ff 69 6f 6e 20 6c 69 73 74 2e 20 20 41 73 20 61 0a  ion list.  As a.
2d300 2a 2a 20 63 6f 6e 76 65 6e 69 65 6e 63 65 2c 20  ** convenience, 
2d301 69 66 20 69 54 79 70 65 20 69 73 20 44 4c 5f 44  if iType is DL_D
2d302 4f 43 49 44 53 2c 20 50 4c 57 72 69 74 65 72 20  OCIDS, PLWriter 
2d303 62 65 63 6f 6d 65 73 20 61 20 6e 6f 2d 6f 70 2e  becomes a no-op.
2d304 0a 2a 2a 20 50 4c 57 72 69 74 65 72 20 77 72 69  .** PLWriter wri
2d305 74 65 73 20 74 6f 20 74 68 65 20 61 73 73 6f 63  tes to the assoc
2d306 69 61 74 65 64 20 44 4c 57 72 69 74 65 72 27 73  iated DLWriter's
2d307 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 70   buffer..**.** p
2d308 6c 77 49 6e 69 74 20 2d 20 69 6e 69 74 20 66 6f  lwInit - init fo
2d309 72 20 77 72 69 74 69 6e 67 20 61 20 64 6f 63 75  r writing a docu
2d30a 6d 65 6e 74 27 73 20 70 6f 73 6c 69 73 74 2e 0a  ment's poslist..
2d30b 2a 2a 20 70 6c 77 44 65 73 74 72 6f 79 20 2d 20  ** plwDestroy - 
2d30c 63 6c 65 61 72 20 61 20 77 72 69 74 65 72 2e 0a  clear a writer..
2d30d 2a 2a 20 70 6c 77 41 64 64 20 2d 20 61 70 70 65  ** plwAdd - appe
2d30e 6e 64 20 70 6f 73 69 74 69 6f 6e 20 61 6e 64 20  nd position and 
2d30f 6f 66 66 73 65 74 20 69 6e 66 6f 72 6d 61 74 69  offset informati
2d310 6f 6e 2e 0a 2a 2a 20 70 6c 77 43 6f 70 79 20 2d  on..** plwCopy -
2d311 20 63 6f 70 79 20 6e 65 78 74 20 70 6f 73 69 74   copy next posit
2d312 69 6f 6e 27 73 20 64 61 74 61 20 66 72 6f 6d 20  ion's data from 
2d313 72 65 61 64 65 72 20 74 6f 20 77 72 69 74 65 72  reader to writer
2d314 2e 0a 2a 2a 20 70 6c 77 54 65 72 6d 69 6e 61 74  ..** plwTerminat
2d315 65 20 2d 20 61 64 64 20 61 6e 79 20 6e 65 63 65  e - add any nece
2d316 73 73 61 72 79 20 64 6f 63 6c 69 73 74 20 74 65  ssary doclist te
2d317 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20  rminator..**.** 
2d318 43 61 6c 6c 69 6e 67 20 70 6c 77 41 64 64 28 29  Calling plwAdd()
2d319 20 61 66 74 65 72 20 70 6c 77 54 65 72 6d 69 6e   after plwTermin
2d31a 61 74 65 28 29 20 6d 61 79 20 72 65 73 75 6c 74  ate() may result
2d31b 20 69 6e 20 61 20 63 6f 72 72 75 70 74 0a 2a 2a   in a corrupt.**
2d31c 20 64 6f 63 6c 69 73 74 2e 0a 2a 2f 0a 2f 2a 20   doclist..*/./* 
2d31d 54 4f 44 4f 28 73 68 65 73 73 29 20 55 6e 74 69  TODO(shess) Unti
2d31e 6c 20 77 65 27 76 65 20 77 72 69 74 74 65 6e 20  l we've written 
2d31f 74 68 65 20 73 65 63 6f 6e 64 20 69 74 65 6d 2c  the second item,
2d320 20 77 65 20 63 61 6e 20 63 61 63 68 65 20 74 68   we can cache th
2d321 65 0a 2a 2a 20 66 69 72 73 74 20 69 74 65 6d 27  e.** first item'
2d322 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20  s information.  
2d323 54 68 65 6e 20 77 65 27 64 20 68 61 76 65 20 74  Then we'd have t
2d324 68 72 65 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a  hree states:.**.
2d325 2a 2a 20 2d 20 69 6e 69 74 69 61 6c 69 7a 65 64  ** - initialized
2d326 20 77 69 74 68 20 64 6f 63 69 64 2c 20 6e 6f 20   with docid, no 
2d327 70 6f 73 69 74 69 6f 6e 73 2e 0a 2a 2a 20 2d 20  positions..** - 
2d328 64 6f 63 69 64 20 61 6e 64 20 6f 6e 65 20 70 6f  docid and one po
2d329 73 69 74 69 6f 6e 2e 0a 2a 2a 20 2d 20 64 6f 63  sition..** - doc
2d32a 69 64 20 61 6e 64 20 6d 75 6c 74 69 70 6c 65 20  id and multiple 
2d32b 70 6f 73 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  positions..**.**
2d32c 20 4f 6e 6c 79 20 74 68 65 20 6c 61 73 74 20 73   Only the last s
2d32d 74 61 74 65 20 6e 65 65 64 73 20 74 6f 20 61 63  tate needs to ac
2d32e 74 75 61 6c 6c 79 20 77 72 69 74 65 20 74 6f 20  tually write to 
2d32f 64 6c 77 2d 3e 62 2c 20 77 68 69 63 68 20 77 6f  dlw->b, which wo
2d330 75 6c 64 0a 2a 2a 20 62 65 20 61 6e 20 69 6d 70  uld.** be an imp
2d331 72 6f 76 65 6d 65 6e 74 20 69 6e 20 74 68 65 20  rovement in the 
2d332 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 63 61 73 65  DLCollector case
2d333 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
2d334 75 63 74 20 50 4c 57 72 69 74 65 72 20 7b 0a 20  uct PLWriter {. 
2d335 20 44 4c 57 72 69 74 65 72 20 2a 64 6c 77 3b 0a   DLWriter *dlw;.
2d336 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20  .  int iColumn; 
2d337 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74 20 63     /* the last c
2d338 6f 6c 75 6d 6e 20 77 72 69 74 74 65 6e 20 2a 2f  olumn written */
2d339 0a 20 20 69 6e 74 20 69 50 6f 73 3b 20 20 20 20  .  int iPos;    
2d33a 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74 20 70     /* the last p
2d33b 6f 73 69 74 69 6f 6e 20 77 72 69 74 74 65 6e 20  osition written 
2d33c 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74  */.  int iOffset
2d33d 3b 20 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74  ;    /* the last
2d33e 20 73 74 61 72 74 20 6f 66 66 73 65 74 20 77 72   start offset wr
2d33f 69 74 74 65 6e 20 2a 2f 0a 7d 20 50 4c 57 72 69  itten */.} PLWri
2d340 74 65 72 3b 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68  ter;../* TODO(sh
2d341 65 73 73 29 20 49 6e 20 74 68 65 20 63 61 73 65  ess) In the case
2d342 20 77 68 65 72 65 20 74 68 65 20 70 61 72 65 6e   where the paren
2d343 74 20 69 73 20 72 65 61 64 69 6e 67 20 74 68 65  t is reading the
2d344 73 65 20 76 61 6c 75 65 73 0a 2a 2a 20 66 72 6f  se values.** fro
2d345 6d 20 61 20 50 4c 52 65 61 64 65 72 2c 20 77 65  m a PLReader, we
2d346 20 63 6f 75 6c 64 20 6f 70 74 69 6d 69 7a 65 20   could optimize 
2d347 74 6f 20 61 20 63 6f 70 79 20 69 66 20 74 68 61  to a copy if tha
2d348 74 20 50 4c 52 65 61 64 65 72 20 68 61 73 0a 2a  t PLReader has.*
2d349 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20  * the same type 
2d34a 61 73 20 70 57 72 69 74 65 72 2e 0a 2a 2f 0a 73  as pWriter..*/.s
2d34b 74 61 74 69 63 20 76 6f 69 64 20 70 6c 77 41 64  tatic void plwAd
2d34c 64 28 50 4c 57 72 69 74 65 72 20 2a 70 57 72 69  d(PLWriter *pWri
2d34d 74 65 72 2c 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  ter, int iColumn
2d34e 2c 20 69 6e 74 20 69 50 6f 73 2c 0a 20 20 20 20  , int iPos,.    
2d34f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
2d350 6e 74 20 69 53 74 61 72 74 4f 66 66 73 65 74 2c  nt iStartOffset,
2d351 20 69 6e 74 20 69 45 6e 64 4f 66 66 73 65 74 29   int iEndOffset)
2d352 7b 0a 20 20 2f 2a 20 57 6f 72 73 74 2d 63 61 73  {.  /* Worst-cas
2d353 65 20 73 70 61 63 65 20 66 6f 72 20 50 4f 53 5f  e space for POS_
2d354 43 4f 4c 55 4d 4e 2c 20 69 43 6f 6c 75 6d 6e 2c  COLUMN, iColumn,
2d355 20 69 50 6f 73 44 65 6c 74 61 2c 0a 20 20 2a 2a   iPosDelta,.  **
2d356 20 69 53 74 61 72 74 4f 66 66 73 65 74 44 65 6c   iStartOffsetDel
2d357 74 61 2c 20 61 6e 64 20 69 45 6e 64 4f 66 66 73  ta, and iEndOffs
2d358 65 74 44 65 6c 74 61 2e 0a 20 20 2a 2f 0a 20 20  etDelta..  */.  
2d359 63 68 61 72 20 63 5b 35 2a 56 41 52 49 4e 54 5f  char c[5*VARINT_
2d35a 4d 41 58 5d 3b 0a 20 20 69 6e 74 20 6e 20 3d 20  MAX];.  int n = 
2d35b 30 3b 0a 0a 20 20 2f 2a 20 42 61 6e 20 70 6c 77  0;..  /* Ban plw
2d35c 41 64 64 28 29 20 61 66 74 65 72 20 70 6c 77 54  Add() after plwT
2d35d 65 72 6d 69 6e 61 74 65 28 29 2e 20 2a 2f 0a 20  erminate(). */. 
2d35e 20 61 73 73 65 72 74 28 20 70 57 72 69 74 65 72   assert( pWriter
2d35f 2d 3e 69 50 6f 73 21 3d 2d 31 20 29 3b 0a 0a 20  ->iPos!=-1 );.. 
2d360 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e 64 6c   if( pWriter->dl
2d361 77 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 44 4f 43  w->iType==DL_DOC
2d362 49 44 53 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20  IDS ) return;.. 
2d363 20 69 66 28 20 69 43 6f 6c 75 6d 6e 21 3d 70 57   if( iColumn!=pW
2d364 72 69 74 65 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29  riter->iColumn )
2d365 7b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 50  {.    n += fts3P
2d366 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 50 4f  utVarint(c+n, PO
2d367 53 5f 43 4f 4c 55 4d 4e 29 3b 0a 20 20 20 20 6e  S_COLUMN);.    n
2d368 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e   += fts3PutVarin
2d369 74 28 63 2b 6e 2c 20 69 43 6f 6c 75 6d 6e 29 3b  t(c+n, iColumn);
2d36a 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e 69 43  .    pWriter->iC
2d36b 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 75 6d 6e 3b  olumn = iColumn;
2d36c 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e 69 50  .    pWriter->iP
2d36d 6f 73 20 3d 20 30 3b 0a 20 20 20 20 70 57 72 69  os = 0;.    pWri
2d36e 74 65 72 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30  ter->iOffset = 0
2d36f 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
2d370 69 50 6f 73 3e 3d 70 57 72 69 74 65 72 2d 3e 69  iPos>=pWriter->i
2d371 50 6f 73 20 29 3b 0a 20 20 6e 20 2b 3d 20 66 74  Pos );.  n += ft
2d372 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c  s3PutVarint(c+n,
2d373 20 50 4f 53 5f 42 41 53 45 2b 28 69 50 6f 73 2d   POS_BASE+(iPos-
2d374 70 57 72 69 74 65 72 2d 3e 69 50 6f 73 29 29 3b  pWriter->iPos));
2d375 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 50 6f 73  .  pWriter->iPos
2d376 20 3d 20 69 50 6f 73 3b 0a 20 20 69 66 28 20 70   = iPos;.  if( p
2d377 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 69 54 79  Writer->dlw->iTy
2d378 70 65 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53  pe==DL_POSITIONS
2d379 5f 4f 46 46 53 45 54 53 20 29 7b 0a 20 20 20 20  _OFFSETS ){.    
2d37a 61 73 73 65 72 74 28 20 69 53 74 61 72 74 4f 66  assert( iStartOf
2d37b 66 73 65 74 3e 3d 70 57 72 69 74 65 72 2d 3e 69  fset>=pWriter->i
2d37c 4f 66 66 73 65 74 20 29 3b 0a 20 20 20 20 6e 20  Offset );.    n 
2d37d 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74  += fts3PutVarint
2d37e 28 63 2b 6e 2c 20 69 53 74 61 72 74 4f 66 66 73  (c+n, iStartOffs
2d37f 65 74 2d 70 57 72 69 74 65 72 2d 3e 69 4f 66 66  et-pWriter->iOff
2d380 73 65 74 29 3b 0a 20 20 20 20 70 57 72 69 74 65  set);.    pWrite
2d381 72 2d 3e 69 4f 66 66 73 65 74 20 3d 20 69 53 74  r->iOffset = iSt
2d382 61 72 74 4f 66 66 73 65 74 3b 0a 20 20 20 20 61  artOffset;.    a
2d383 73 73 65 72 74 28 20 69 45 6e 64 4f 66 66 73 65  ssert( iEndOffse
2d384 74 3e 3d 69 53 74 61 72 74 4f 66 66 73 65 74 20  t>=iStartOffset 
2d385 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33  );.    n += fts3
2d386 50 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 69  PutVarint(c+n, i
2d387 45 6e 64 4f 66 66 73 65 74 2d 69 53 74 61 72 74  EndOffset-iStart
2d388 4f 66 66 73 65 74 29 3b 0a 20 20 7d 0a 20 20 64  Offset);.  }.  d
2d389 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28  ataBufferAppend(
2d38a 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 62 2c  pWriter->dlw->b,
2d38b 20 63 2c 20 6e 29 3b 0a 7d 0a 73 74 61 74 69 63   c, n);.}.static
2d38c 20 76 6f 69 64 20 70 6c 77 43 6f 70 79 28 50 4c   void plwCopy(PL
2d38d 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c  Writer *pWriter,
2d38e 20 50 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64   PLReader *pRead
2d38f 65 72 29 7b 0a 20 20 70 6c 77 41 64 64 28 70 57  er){.  plwAdd(pW
2d390 72 69 74 65 72 2c 20 70 6c 72 43 6f 6c 75 6d 6e  riter, plrColumn
2d391 28 70 52 65 61 64 65 72 29 2c 20 70 6c 72 50 6f  (pReader), plrPo
2d392 73 69 74 69 6f 6e 28 70 52 65 61 64 65 72 29 2c  sition(pReader),
2d393 0a 20 20 20 20 20 20 20 20 20 70 6c 72 53 74 61  .         plrSta
2d394 72 74 4f 66 66 73 65 74 28 70 52 65 61 64 65 72  rtOffset(pReader
2d395 29 2c 20 70 6c 72 45 6e 64 4f 66 66 73 65 74 28  ), plrEndOffset(
2d396 70 52 65 61 64 65 72 29 29 3b 0a 7d 0a 73 74 61  pReader));.}.sta
2d397 74 69 63 20 76 6f 69 64 20 70 6c 77 49 6e 69 74  tic void plwInit
2d398 28 50 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74  (PLWriter *pWrit
2d399 65 72 2c 20 44 4c 57 72 69 74 65 72 20 2a 64 6c  er, DLWriter *dl
2d39a 77 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  w, sqlite_int64 
2d39b 69 44 6f 63 69 64 29 7b 0a 20 20 63 68 61 72 20  iDocid){.  char 
2d39c 63 5b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20  c[VARINT_MAX];. 
2d39d 20 69 6e 74 20 6e 3b 0a 0a 20 20 70 57 72 69 74   int n;..  pWrit
2d39e 65 72 2d 3e 64 6c 77 20 3d 20 64 6c 77 3b 0a 0a  er->dlw = dlw;..
2d39f 20 20 2f 2a 20 44 6f 63 69 64 73 20 6d 75 73 74    /* Docids must
2d3a0 20 61 73 63 65 6e 64 2e 20 2a 2f 0a 20 20 61 73   ascend. */.  as
2d3a1 73 65 72 74 28 20 21 70 57 72 69 74 65 72 2d 3e  sert( !pWriter->
2d3a2 64 6c 77 2d 3e 68 61 73 5f 69 50 72 65 76 44 6f  dlw->has_iPrevDo
2d3a3 63 69 64 20 7c 7c 20 69 44 6f 63 69 64 3e 70 57  cid || iDocid>pW
2d3a4 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 69 50 72 65  riter->dlw->iPre
2d3a5 76 44 6f 63 69 64 20 29 3b 0a 20 20 6e 20 3d 20  vDocid );.  n = 
2d3a6 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c  fts3PutVarint(c,
2d3a7 20 69 44 6f 63 69 64 2d 70 57 72 69 74 65 72 2d   iDocid-pWriter-
2d3a8 3e 64 6c 77 2d 3e 69 50 72 65 76 44 6f 63 69 64  >dlw->iPrevDocid
2d3a9 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 41  );.  dataBufferA
2d3aa 70 70 65 6e 64 28 70 57 72 69 74 65 72 2d 3e 64  ppend(pWriter->d
2d3ab 6c 77 2d 3e 62 2c 20 63 2c 20 6e 29 3b 0a 20 20  lw->b, c, n);.  
2d3ac 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 69 50  pWriter->dlw->iP
2d3ad 72 65 76 44 6f 63 69 64 20 3d 20 69 44 6f 63 69  revDocid = iDoci
2d3ae 64 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  d;.#ifndef NDEBU
2d3af 47 0a 20 20 70 57 72 69 74 65 72 2d 3e 64 6c 77  G.  pWriter->dlw
2d3b0 2d 3e 68 61 73 5f 69 50 72 65 76 44 6f 63 69 64  ->has_iPrevDocid
2d3b1 20 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 20 20   = 1;.#endif..  
2d3b2 70 57 72 69 74 65 72 2d 3e 69 43 6f 6c 75 6d 6e  pWriter->iColumn
2d3b3 20 3d 20 30 3b 0a 20 20 70 57 72 69 74 65 72 2d   = 0;.  pWriter-
2d3b4 3e 69 50 6f 73 20 3d 20 30 3b 0a 20 20 70 57 72  >iPos = 0;.  pWr
2d3b5 69 74 65 72 2d 3e 69 4f 66 66 73 65 74 20 3d 20  iter->iOffset = 
2d3b6 30 3b 0a 7d 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  0;.}./* TODO(she
2d3b7 73 73 29 20 53 68 6f 75 6c 64 20 70 6c 77 44 65  ss) Should plwDe
2d3b8 73 74 72 6f 79 28 29 20 61 6c 73 6f 20 74 65 72  stroy() also ter
2d3b9 6d 69 6e 61 74 65 20 74 68 65 20 64 6f 63 6c 69  minate the docli
2d3ba 73 74 3f 20 20 42 75 74 0a 2a 2a 20 74 68 65 6e  st?  But.** then
2d3bb 20 70 6c 77 44 65 73 74 72 6f 79 28 29 20 77 6f   plwDestroy() wo
2d3bc 75 6c 64 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65  uld no longer be
2d3bd 20 6a 75 73 74 20 61 20 64 65 73 74 72 75 63 74   just a destruct
2d3be 6f 72 2c 20 69 74 20 77 6f 75 6c 64 0a 2a 2a 20  or, it would.** 
2d3bf 61 6c 73 6f 20 62 65 20 64 6f 69 6e 67 20 77 6f  also be doing wo
2d3c0 72 6b 2c 20 77 68 69 63 68 20 69 73 6e 27 74 20  rk, which isn't 
2d3c1 63 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20  consistent with 
2d3c2 74 68 65 20 6f 76 65 72 61 6c 6c 20 69 64 69 6f  the overall idio
2d3c3 6d 2e 0a 2a 2a 20 41 6e 6f 74 68 65 72 20 6f 70  m..** Another op
2d3c4 74 69 6f 6e 20 77 6f 75 6c 64 20 62 65 20 66 6f  tion would be fo
2d3c5 72 20 70 6c 77 41 64 64 28 29 20 74 6f 20 61 6c  r plwAdd() to al
2d3c6 77 61 79 73 20 61 70 70 65 6e 64 20 61 6e 79 20  ways append any 
2d3c7 6e 65 63 65 73 73 61 72 79 0a 2a 2a 20 74 65 72  necessary.** ter
2d3c8 6d 69 6e 61 74 6f 72 2c 20 73 6f 20 74 68 61 74  minator, so that
2d3c9 20 74 68 65 20 6f 75 74 70 75 74 20 69 73 20 61   the output is a
2d3ca 6c 77 61 79 73 20 63 6f 72 72 65 63 74 2e 20 20  lways correct.  
2d3cb 42 75 74 20 74 68 61 74 20 77 6f 75 6c 64 0a 2a  But that would.*
2d3cc 2a 20 61 64 64 20 69 6e 63 72 65 6d 65 6e 74 61  * add incrementa
2d3cd 6c 20 77 6f 72 6b 20 74 6f 20 74 68 65 20 63 6f  l work to the co
2d3ce 6d 6d 6f 6e 20 63 61 73 65 20 77 69 74 68 20 74  mmon case with t
2d3cf 68 65 20 6f 6e 6c 79 20 62 65 6e 65 66 69 74 20  he only benefit 
2d3d0 62 65 69 6e 67 0a 2a 2a 20 41 50 49 20 65 6c 65  being.** API ele
2d3d1 67 61 6e 63 65 2e 20 20 50 75 6e 74 20 66 6f 72  gance.  Punt for
2d3d2 20 6e 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   now..*/.static 
2d3d3 76 6f 69 64 20 70 6c 77 54 65 72 6d 69 6e 61 74  void plwTerminat
2d3d4 65 28 50 4c 57 72 69 74 65 72 20 2a 70 57 72 69  e(PLWriter *pWri
2d3d5 74 65 72 29 7b 0a 20 20 69 66 28 20 70 57 72 69  ter){.  if( pWri
2d3d6 74 65 72 2d 3e 64 6c 77 2d 3e 69 54 79 70 65 3e  ter->dlw->iType>
2d3d7 44 4c 5f 44 4f 43 49 44 53 20 29 7b 0a 20 20 20  DL_DOCIDS ){.   
2d3d8 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d   char c[VARINT_M
2d3d9 41 58 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d  AX];.    int n =
2d3da 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
2d3db 2c 20 50 4f 53 5f 45 4e 44 29 3b 0a 20 20 20 20  , POS_END);.    
2d3dc 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
2d3dd 28 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 62  (pWriter->dlw->b
2d3de 2c 20 63 2c 20 6e 29 3b 0a 20 20 7d 0a 23 69 66  , c, n);.  }.#if
2d3df 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a  ndef NDEBUG.  /*
2d3e0 20 4d 61 72 6b 20 61 73 20 74 65 72 6d 69 6e 61   Mark as termina
2d3e1 74 65 64 20 66 6f 72 20 61 73 73 65 72 74 20 69  ted for assert i
2d3e2 6e 20 70 6c 77 41 64 64 28 29 2e 20 2a 2f 0a 20  n plwAdd(). */. 
2d3e3 20 70 57 72 69 74 65 72 2d 3e 69 50 6f 73 20 3d   pWriter->iPos =
2d3e4 20 2d 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 73 74   -1;.#endif.}.st
2d3e5 61 74 69 63 20 76 6f 69 64 20 70 6c 77 44 65 73  atic void plwDes
2d3e6 74 72 6f 79 28 50 4c 57 72 69 74 65 72 20 2a 70  troy(PLWriter *p
2d3e7 57 72 69 74 65 72 29 7b 0a 20 20 53 43 52 41 4d  Writer){.  SCRAM
2d3e8 42 4c 45 28 70 57 72 69 74 65 72 29 3b 0a 7d 0a  BLE(pWriter);.}.
2d3e9 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2d3ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d3eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d3ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d3ed 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44 4c 43 6f 6c 6c  *****/./* DLColl
2d3ee 65 63 74 6f 72 20 77 72 61 70 73 20 50 4c 57 72  ector wraps PLWr
2d3ef 69 74 65 72 20 61 6e 64 20 44 4c 57 72 69 74 65  iter and DLWrite
2d3f0 72 20 74 6f 20 70 72 6f 76 69 64 65 20 61 0a 2a  r to provide a.*
2d3f1 2a 20 64 79 6e 61 6d 69 63 61 6c 6c 79 2d 61 6c  * dynamically-al
2d3f2 6c 6f 63 61 74 65 64 20 64 6f 63 6c 69 73 74 20  located doclist 
2d3f3 61 72 65 61 20 74 6f 20 75 73 65 20 64 75 72 69  area to use duri
2d3f4 6e 67 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 2e  ng tokenization.
2d3f5 0a 2a 2a 0a 2a 2a 20 64 6c 63 4e 65 77 20 2d 20  .**.** dlcNew - 
2d3f6 6d 61 6c 6c 6f 63 20 75 70 20 61 6e 64 20 69 6e  malloc up and in
2d3f7 69 74 69 61 6c 69 7a 65 20 61 20 63 6f 6c 6c 65  itialize a colle
2d3f8 63 74 6f 72 2e 0a 2a 2a 20 64 6c 63 44 65 6c 65  ctor..** dlcDele
2d3f9 74 65 20 2d 20 64 65 73 74 72 6f 79 20 61 20 63  te - destroy a c
2d3fa 6f 6c 6c 65 63 74 6f 72 20 61 6e 64 20 61 6c 6c  ollector and all
2d3fb 20 63 6f 6e 74 61 69 6e 65 64 20 69 74 65 6d 73   contained items
2d3fc 2e 0a 2a 2a 20 64 6c 63 41 64 64 50 6f 73 20 2d  ..** dlcAddPos -
2d3fd 20 61 70 70 65 6e 64 20 70 6f 73 69 74 69 6f 6e   append position
2d3fe 20 61 6e 64 20 6f 66 66 73 65 74 20 69 6e 66 6f   and offset info
2d3ff 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 64 6c 63 41  rmation..** dlcA
2d400 64 64 44 6f 63 6c 69 73 74 20 2d 20 61 64 64 20  ddDoclist - add 
2d401 74 68 65 20 63 6f 6c 6c 65 63 74 65 64 20 64 6f  the collected do
2d402 63 6c 69 73 74 20 74 6f 20 74 68 65 20 67 69 76  clist to the giv
2d403 65 6e 20 62 75 66 66 65 72 2e 0a 2a 2a 20 64 6c  en buffer..** dl
2d404 63 4e 65 78 74 20 2d 20 74 65 72 6d 69 6e 61 74  cNext - terminat
2d405 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 6f  e the current do
2d406 63 75 6d 65 6e 74 20 61 6e 64 20 6f 70 65 6e 20  cument and open 
2d407 61 6e 6f 74 68 65 72 2e 0a 2a 2f 0a 74 79 70 65  another..*/.type
2d408 64 65 66 20 73 74 72 75 63 74 20 44 4c 43 6f 6c  def struct DLCol
2d409 6c 65 63 74 6f 72 20 7b 0a 20 20 44 61 74 61 42  lector {.  DataB
2d40a 75 66 66 65 72 20 62 3b 0a 20 20 44 4c 57 72 69  uffer b;.  DLWri
2d40b 74 65 72 20 64 6c 77 3b 0a 20 20 50 4c 57 72 69  ter dlw;.  PLWri
2d40c 74 65 72 20 70 6c 77 3b 0a 7d 20 44 4c 43 6f 6c  ter plw;.} DLCol
2d40d 6c 65 63 74 6f 72 3b 0a 0a 2f 2a 20 54 4f 44 4f  lector;../* TODO
2d40e 28 73 68 65 73 73 29 20 54 68 69 73 20 63 6f 75  (shess) This cou
2d40f 6c 64 20 61 6c 73 6f 20 62 65 20 64 6f 6e 65 20  ld also be done 
2d410 62 79 20 63 61 6c 6c 69 6e 67 20 70 6c 77 54 65  by calling plwTe
2d411 72 6d 69 6e 61 74 65 28 29 20 61 6e 64 0a 2a 2a  rminate() and.**
2d412 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
2d413 64 28 29 2e 20 20 49 20 74 72 69 65 64 20 74 68  d().  I tried th
2d414 61 74 2c 20 65 78 70 65 63 74 69 6e 67 20 6e 6f  at, expecting no
2d415 6d 69 6e 61 6c 20 70 65 72 66 6f 72 6d 61 6e 63  minal performanc
2d416 65 0a 2a 2a 20 64 69 66 66 65 72 65 6e 63 65 73  e.** differences
2d417 2c 20 62 75 74 20 69 74 20 73 65 65 6d 65 64 20  , but it seemed 
2d418 74 6f 20 70 72 65 74 74 79 20 72 65 6c 69 61 62  to pretty reliab
2d419 6c 79 20 62 65 20 77 6f 72 74 68 20 31 25 20 74  ly be worth 1% t
2d41a 6f 20 63 6f 64 65 0a 2a 2a 20 69 74 20 74 68 69  o code.** it thi
2d41b 73 20 77 61 79 2e 20 20 49 20 73 75 73 70 65 63  s way.  I suspec
2d41c 74 20 69 74 20 69 73 20 74 68 65 20 69 6e 63 72  t it is the incr
2d41d 65 6d 65 6e 74 61 6c 20 6d 61 6c 6c 6f 63 20 6f  emental malloc o
2d41e 76 65 72 68 65 61 64 20 28 73 6f 6d 65 0a 2a 2a  verhead (some.**
2d41f 20 70 65 72 63 65 6e 74 61 67 65 20 6f 66 20 74   percentage of t
2d420 68 65 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 28  he plwTerminate(
2d421 29 20 63 61 6c 6c 73 20 77 69 6c 6c 20 63 61 75  ) calls will cau
2d422 73 65 20 61 20 72 65 61 6c 6c 6f 63 29 2c 20 73  se a realloc), s
2d423 6f 0a 2a 2a 20 74 68 69 73 20 6d 69 67 68 74 20  o.** this might 
2d424 62 65 20 77 6f 72 74 68 20 72 65 76 69 73 69 74  be worth revisit
2d425 69 6e 67 20 69 66 20 74 68 65 20 44 61 74 61 42  ing if the DataB
2d426 75 66 66 65 72 20 69 6d 70 6c 65 6d 65 6e 74 61  uffer implementa
2d427 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65 73 2e  tion.** changes.
2d428 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2d429 64 6c 63 41 64 64 44 6f 63 6c 69 73 74 28 44 4c  dlcAddDoclist(DL
2d42a 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c 6c  Collector *pColl
2d42b 65 63 74 6f 72 2c 20 44 61 74 61 42 75 66 66 65  ector, DataBuffe
2d42c 72 20 2a 62 29 7b 0a 20 20 69 66 28 20 70 43 6f  r *b){.  if( pCo
2d42d 6c 6c 65 63 74 6f 72 2d 3e 64 6c 77 2e 69 54 79  llector->dlw.iTy
2d42e 70 65 3e 44 4c 5f 44 4f 43 49 44 53 20 29 7b 0a  pe>DL_DOCIDS ){.
2d42f 20 20 20 20 63 68 61 72 20 63 5b 56 41 52 49 4e      char c[VARIN
2d430 54 5f 4d 41 58 5d 3b 0a 20 20 20 20 69 6e 74 20  T_MAX];.    int 
2d431 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e  n = fts3PutVarin
2d432 74 28 63 2c 20 50 4f 53 5f 45 4e 44 29 3b 0a 20  t(c, POS_END);. 
2d433 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70     dataBufferApp
2d434 65 6e 64 32 28 62 2c 20 70 43 6f 6c 6c 65 63 74  end2(b, pCollect
2d435 6f 72 2d 3e 62 2e 70 44 61 74 61 2c 20 70 43 6f  or->b.pData, pCo
2d436 6c 6c 65 63 74 6f 72 2d 3e 62 2e 6e 44 61 74 61  llector->b.nData
2d437 2c 20 63 2c 20 6e 29 3b 0a 20 20 7d 65 6c 73 65  , c, n);.  }else
2d438 7b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  {.    dataBuffer
2d439 41 70 70 65 6e 64 28 62 2c 20 70 43 6f 6c 6c 65  Append(b, pColle
2d43a 63 74 6f 72 2d 3e 62 2e 70 44 61 74 61 2c 20 70  ctor->b.pData, p
2d43b 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62 2e 6e 44 61  Collector->b.nDa
2d43c 74 61 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69  ta);.  }.}.stati
2d43d 63 20 76 6f 69 64 20 64 6c 63 4e 65 78 74 28 44  c void dlcNext(D
2d43e 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c  LCollector *pCol
2d43f 6c 65 63 74 6f 72 2c 20 73 71 6c 69 74 65 5f 69  lector, sqlite_i
2d440 6e 74 36 34 20 69 44 6f 63 69 64 29 7b 0a 20 20  nt64 iDocid){.  
2d441 70 6c 77 54 65 72 6d 69 6e 61 74 65 28 26 70 43  plwTerminate(&pC
2d442 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77 29 3b 0a  ollector->plw);.
2d443 20 20 70 6c 77 44 65 73 74 72 6f 79 28 26 70 43    plwDestroy(&pC
2d444 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77 29 3b 0a  ollector->plw);.
2d445 20 20 70 6c 77 49 6e 69 74 28 26 70 43 6f 6c 6c    plwInit(&pColl
2d446 65 63 74 6f 72 2d 3e 70 6c 77 2c 20 26 70 43 6f  ector->plw, &pCo
2d447 6c 6c 65 63 74 6f 72 2d 3e 64 6c 77 2c 20 69 44  llector->dlw, iD
2d448 6f 63 69 64 29 3b 0a 7d 0a 73 74 61 74 69 63 20  ocid);.}.static 
2d449 76 6f 69 64 20 64 6c 63 41 64 64 50 6f 73 28 44  void dlcAddPos(D
2d44a 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c  LCollector *pCol
2d44b 6c 65 63 74 6f 72 2c 20 69 6e 74 20 69 43 6f 6c  lector, int iCol
2d44c 75 6d 6e 2c 20 69 6e 74 20 69 50 6f 73 2c 0a 20  umn, int iPos,. 
2d44d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d44e 20 20 20 20 20 69 6e 74 20 69 53 74 61 72 74 4f       int iStartO
2d44f 66 66 73 65 74 2c 20 69 6e 74 20 69 45 6e 64 4f  ffset, int iEndO
2d450 66 66 73 65 74 29 7b 0a 20 20 70 6c 77 41 64 64  ffset){.  plwAdd
2d451 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c  (&pCollector->pl
2d452 77 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 50 6f 73  w, iColumn, iPos
2d453 2c 20 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20  , iStartOffset, 
2d454 69 45 6e 64 4f 66 66 73 65 74 29 3b 0a 7d 0a 0a  iEndOffset);.}..
2d455 73 74 61 74 69 63 20 44 4c 43 6f 6c 6c 65 63 74  static DLCollect
2d456 6f 72 20 2a 64 6c 63 4e 65 77 28 73 71 6c 69 74  or *dlcNew(sqlit
2d457 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 2c 20  e_int64 iDocid, 
2d458 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79 70  DocListType iTyp
2d459 65 29 7b 0a 20 20 44 4c 43 6f 6c 6c 65 63 74 6f  e){.  DLCollecto
2d45a 72 20 2a 70 43 6f 6c 6c 65 63 74 6f 72 20 3d 20  r *pCollector = 
2d45b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
2d45c 69 7a 65 6f 66 28 44 4c 43 6f 6c 6c 65 63 74 6f  izeof(DLCollecto
2d45d 72 29 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  r));.  dataBuffe
2d45e 72 49 6e 69 74 28 26 70 43 6f 6c 6c 65 63 74 6f  rInit(&pCollecto
2d45f 72 2d 3e 62 2c 20 30 29 3b 0a 20 20 64 6c 77 49  r->b, 0);.  dlwI
2d460 6e 69 74 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d  nit(&pCollector-
2d461 3e 64 6c 77 2c 20 69 54 79 70 65 2c 20 26 70 43  >dlw, iType, &pC
2d462 6f 6c 6c 65 63 74 6f 72 2d 3e 62 29 3b 0a 20 20  ollector->b);.  
2d463 70 6c 77 49 6e 69 74 28 26 70 43 6f 6c 6c 65 63  plwInit(&pCollec
2d464 74 6f 72 2d 3e 70 6c 77 2c 20 26 70 43 6f 6c 6c  tor->plw, &pColl
2d465 65 63 74 6f 72 2d 3e 64 6c 77 2c 20 69 44 6f 63  ector->dlw, iDoc
2d466 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 43  id);.  return pC
2d467 6f 6c 6c 65 63 74 6f 72 3b 0a 7d 0a 73 74 61 74  ollector;.}.stat
2d468 69 63 20 76 6f 69 64 20 64 6c 63 44 65 6c 65 74  ic void dlcDelet
2d469 65 28 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70  e(DLCollector *p
2d46a 43 6f 6c 6c 65 63 74 6f 72 29 7b 0a 20 20 70 6c  Collector){.  pl
2d46b 77 44 65 73 74 72 6f 79 28 26 70 43 6f 6c 6c 65  wDestroy(&pColle
2d46c 63 74 6f 72 2d 3e 70 6c 77 29 3b 0a 20 20 64 6c  ctor->plw);.  dl
2d46d 77 44 65 73 74 72 6f 79 28 26 70 43 6f 6c 6c 65  wDestroy(&pColle
2d46e 63 74 6f 72 2d 3e 64 6c 77 29 3b 0a 20 20 64 61  ctor->dlw);.  da
2d46f 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
2d470 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62 29 3b  &pCollector->b);
2d471 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 43 6f 6c  .  SCRAMBLE(pCol
2d472 6c 65 63 74 6f 72 29 3b 0a 20 20 73 71 6c 69 74  lector);.  sqlit
2d473 65 33 5f 66 72 65 65 28 70 43 6f 6c 6c 65 63 74  e3_free(pCollect
2d474 6f 72 29 3b 0a 7d 0a 0a 0a 2f 2a 20 43 6f 70 79  or);.}.../* Copy
2d475 20 74 68 65 20 64 6f 63 6c 69 73 74 20 64 61 74   the doclist dat
2d476 61 20 6f 66 20 69 54 79 70 65 20 69 6e 20 70 44  a of iType in pD
2d477 61 74 61 2f 6e 44 61 74 61 20 69 6e 74 6f 20 2a  ata/nData into *
2d478 6f 75 74 2c 20 74 72 69 6d 6d 69 6e 67 0a 2a 2a  out, trimming.**
2d479 20 75 6e 6e 65 63 65 73 73 61 72 79 20 64 61 74   unnecessary dat
2d47a 61 20 61 73 20 77 65 20 67 6f 2e 20 20 4f 6e 6c  a as we go.  Onl
2d47b 79 20 63 6f 6c 75 6d 6e 73 20 6d 61 74 63 68 69  y columns matchi
2d47c 6e 67 20 69 43 6f 6c 75 6d 6e 20 61 72 65 0a 2a  ng iColumn are.*
2d47d 2a 20 63 6f 70 69 65 64 2c 20 61 6c 6c 20 63 6f  * copied, all co
2d47e 6c 75 6d 6e 73 20 63 6f 70 69 65 64 20 69 66 20  lumns copied if 
2d47f 69 43 6f 6c 75 6d 6e 20 69 73 20 2d 31 2e 20 20  iColumn is -1.  
2d480 45 6c 65 6d 65 6e 74 73 20 77 69 74 68 20 6e 6f  Elements with no
2d481 0a 2a 2a 20 6d 61 74 63 68 69 6e 67 20 63 6f 6c  .** matching col
2d482 75 6d 6e 73 20 61 72 65 20 64 72 6f 70 70 65 64  umns are dropped
2d483 2e 20 20 54 68 65 20 6f 75 74 70 75 74 20 69 73  .  The output is
2d484 20 61 6e 20 69 4f 75 74 54 79 70 65 20 64 6f 63   an iOutType doc
2d485 6c 69 73 74 2e 0a 2a 2f 0a 2f 2a 20 4e 4f 54 45  list..*/./* NOTE
2d486 28 73 68 65 73 73 29 20 54 68 69 73 20 63 6f 64  (shess) This cod
2d487 65 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69 64 20  e is only valid 
2d488 61 66 74 65 72 20 61 6c 6c 20 64 6f 63 6c 69 73  after all doclis
2d489 74 73 20 61 72 65 20 6d 65 72 67 65 64 2e 0a 2a  ts are merged..*
2d48a 2a 20 49 66 20 74 68 69 73 20 69 73 20 72 75 6e  * If this is run
2d48b 20 62 65 66 6f 72 65 20 6d 65 72 67 65 73 2c 20   before merges, 
2d48c 74 68 65 6e 20 64 6f 63 6c 69 73 74 20 69 74 65  then doclist ite
2d48d 6d 73 20 77 68 69 63 68 20 72 65 70 72 65 73 65  ms which represe
2d48e 6e 74 0a 2a 2a 20 64 65 6c 65 74 69 6f 6e 20 77  nt.** deletion w
2d48f 69 6c 6c 20 62 65 20 74 72 69 6d 6d 65 64 2c 20  ill be trimmed, 
2d490 61 6e 64 20 77 69 6c 6c 20 74 68 75 73 20 6e 6f  and will thus no
2d491 74 20 65 66 66 65 63 74 20 61 20 64 65 6c 65 74  t effect a delet
2d492 69 6f 6e 0a 2a 2a 20 64 75 72 69 6e 67 20 74 68  ion.** during th
2d493 65 20 6d 65 72 67 65 2e 0a 2a 2f 0a 73 74 61 74  e merge..*/.stat
2d494 69 63 20 76 6f 69 64 20 64 6f 63 4c 69 73 74 54  ic void docListT
2d495 72 69 6d 28 44 6f 63 4c 69 73 74 54 79 70 65 20  rim(DocListType 
2d496 69 54 79 70 65 2c 20 63 6f 6e 73 74 20 63 68 61  iType, const cha
2d497 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44  r *pData, int nD
2d498 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
2d499 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74               int
2d49a 20 69 43 6f 6c 75 6d 6e 2c 20 44 6f 63 4c 69 73   iColumn, DocLis
2d49b 74 54 79 70 65 20 69 4f 75 74 54 79 70 65 2c 20  tType iOutType, 
2d49c 44 61 74 61 42 75 66 66 65 72 20 2a 6f 75 74 29  DataBuffer *out)
2d49d 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 64 6c 52  {.  DLReader dlR
2d49e 65 61 64 65 72 3b 0a 20 20 44 4c 57 72 69 74 65  eader;.  DLWrite
2d49f 72 20 64 6c 57 72 69 74 65 72 3b 0a 0a 20 20 61  r dlWriter;..  a
2d4a0 73 73 65 72 74 28 20 69 4f 75 74 54 79 70 65 3c  ssert( iOutType<
2d4a1 3d 69 54 79 70 65 20 29 3b 0a 0a 20 20 64 6c 72  =iType );..  dlr
2d4a2 49 6e 69 74 28 26 64 6c 52 65 61 64 65 72 2c 20  Init(&dlReader, 
2d4a3 69 54 79 70 65 2c 20 70 44 61 74 61 2c 20 6e 44  iType, pData, nD
2d4a4 61 74 61 29 3b 0a 20 20 64 6c 77 49 6e 69 74 28  ata);.  dlwInit(
2d4a5 26 64 6c 57 72 69 74 65 72 2c 20 69 4f 75 74 54  &dlWriter, iOutT
2d4a6 79 70 65 2c 20 6f 75 74 29 3b 0a 0a 20 20 77 68  ype, out);..  wh
2d4a7 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64 28 26  ile( !dlrAtEnd(&
2d4a8 64 6c 52 65 61 64 65 72 29 20 29 7b 0a 20 20 20  dlReader) ){.   
2d4a9 20 50 4c 52 65 61 64 65 72 20 70 6c 52 65 61 64   PLReader plRead
2d4aa 65 72 3b 0a 20 20 20 20 50 4c 57 72 69 74 65 72  er;.    PLWriter
2d4ab 20 70 6c 57 72 69 74 65 72 3b 0a 20 20 20 20 69   plWriter;.    i
2d4ac 6e 74 20 6d 61 74 63 68 20 3d 20 30 3b 0a 0a 20  nt match = 0;.. 
2d4ad 20 20 20 70 6c 72 49 6e 69 74 28 26 70 6c 52 65     plrInit(&plRe
2d4ae 61 64 65 72 2c 20 26 64 6c 52 65 61 64 65 72 29  ader, &dlReader)
2d4af 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 21 70  ;..    while( !p
2d4b0 6c 72 41 74 45 6e 64 28 26 70 6c 52 65 61 64 65  lrAtEnd(&plReade
2d4b1 72 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  r) ){.      if( 
2d4b2 69 43 6f 6c 75 6d 6e 3d 3d 2d 31 20 7c 7c 20 70  iColumn==-1 || p
2d4b3 6c 72 43 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64  lrColumn(&plRead
2d4b4 65 72 29 3d 3d 69 43 6f 6c 75 6d 6e 20 29 7b 0a  er)==iColumn ){.
2d4b5 20 20 20 20 20 20 20 20 69 66 28 20 21 6d 61 74          if( !mat
2d4b6 63 68 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ch ){.          
2d4b7 70 6c 77 49 6e 69 74 28 26 70 6c 57 72 69 74 65  plwInit(&plWrite
2d4b8 72 2c 20 26 64 6c 57 72 69 74 65 72 2c 20 64 6c  r, &dlWriter, dl
2d4b9 72 44 6f 63 69 64 28 26 64 6c 52 65 61 64 65 72  rDocid(&dlReader
2d4ba 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6d 61  ));.          ma
2d4bb 74 63 68 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  tch = 1;.       
2d4bc 20 7d 0a 20 20 20 20 20 20 20 20 70 6c 77 41 64   }.        plwAd
2d4bd 64 28 26 70 6c 57 72 69 74 65 72 2c 20 70 6c 72  d(&plWriter, plr
2d4be 43 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64 65 72  Column(&plReader
2d4bf 29 2c 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26  ), plrPosition(&
2d4c0 70 6c 52 65 61 64 65 72 29 2c 0a 20 20 20 20 20  plReader),.     
2d4c1 20 20 20 20 20 20 20 20 20 20 70 6c 72 53 74 61            plrSta
2d4c2 72 74 4f 66 66 73 65 74 28 26 70 6c 52 65 61 64  rtOffset(&plRead
2d4c3 65 72 29 2c 20 70 6c 72 45 6e 64 4f 66 66 73 65  er), plrEndOffse
2d4c4 74 28 26 70 6c 52 65 61 64 65 72 29 29 3b 0a 20  t(&plReader));. 
2d4c5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 6c 72       }.      plr
2d4c6 53 74 65 70 28 26 70 6c 52 65 61 64 65 72 29 3b  Step(&plReader);
2d4c7 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d  .    }.    if( m
2d4c8 61 74 63 68 20 29 7b 0a 20 20 20 20 20 20 70 6c  atch ){.      pl
2d4c9 77 54 65 72 6d 69 6e 61 74 65 28 26 70 6c 57 72  wTerminate(&plWr
2d4ca 69 74 65 72 29 3b 0a 20 20 20 20 20 20 70 6c 77  iter);.      plw
2d4cb 44 65 73 74 72 6f 79 28 26 70 6c 57 72 69 74 65  Destroy(&plWrite
2d4cc 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70  r);.    }..    p
2d4cd 6c 72 44 65 73 74 72 6f 79 28 26 70 6c 52 65 61  lrDestroy(&plRea
2d4ce 64 65 72 29 3b 0a 20 20 20 20 64 6c 72 53 74 65  der);.    dlrSte
2d4cf 70 28 26 64 6c 52 65 61 64 65 72 29 3b 0a 20 20  p(&dlReader);.  
2d4d0 7d 0a 20 20 64 6c 77 44 65 73 74 72 6f 79 28 26  }.  dlwDestroy(&
2d4d1 64 6c 57 72 69 74 65 72 29 3b 0a 20 20 64 6c 72  dlWriter);.  dlr
2d4d2 44 65 73 74 72 6f 79 28 26 64 6c 52 65 61 64 65  Destroy(&dlReade
2d4d3 72 29 3b 0a 7d 0a 0a 2f 2a 20 55 73 65 64 20 62  r);.}../* Used b
2d4d4 79 20 64 6f 63 4c 69 73 74 4d 65 72 67 65 28 29  y docListMerge()
2d4d5 20 74 6f 20 6b 65 65 70 20 64 6f 63 6c 69 73 74   to keep doclist
2d4d6 73 20 69 6e 20 74 68 65 20 61 73 63 65 6e 64 69  s in the ascendi
2d4d7 6e 67 20 6f 72 64 65 72 20 62 79 0a 2a 2a 20 64  ng order by.** d
2d4d8 6f 63 69 64 2c 20 74 68 65 6e 20 61 73 63 65 6e  ocid, then ascen
2d4d9 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 61 67  ding order by ag
2d4da 65 20 28 73 6f 20 74 68 65 20 6e 65 77 65 73 74  e (so the newest
2d4db 20 63 6f 6d 65 73 20 66 69 72 73 74 29 2e 0a 2a   comes first)..*
2d4dc 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
2d4dd 20 4f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72   OrderedDLReader
2d4de 20 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 2a 70   {.  DLReader *p
2d4df 52 65 61 64 65 72 3b 0a 0a 20 20 2f 2a 20 54 4f  Reader;..  /* TO
2d4e0 44 4f 28 73 68 65 73 73 29 20 49 66 20 77 65 20  DO(shess) If we 
2d4e1 61 73 73 75 6d 65 20 74 68 61 74 20 64 6f 63 4c  assume that docL
2d4e2 69 73 74 4d 65 72 67 65 20 70 52 65 61 64 65 72  istMerge pReader
2d4e3 73 20 69 73 20 6f 72 64 65 72 65 64 20 62 79 0a  s is ordered by.
2d4e4 20 20 2a 2a 20 61 67 65 20 28 77 68 69 63 68 20    ** age (which 
2d4e5 77 65 20 64 6f 29 2c 20 74 68 65 6e 20 77 65 20  we do), then we 
2d4e6 63 6f 75 6c 64 20 75 73 65 20 70 52 65 61 64 65  could use pReade
2d4e7 72 20 63 6f 6d 70 61 72 69 73 6f 6e 73 20 74 6f  r comparisons to
2d4e8 20 62 72 65 61 6b 0a 20 20 2a 2a 20 74 69 65 73   break.  ** ties
2d4e9 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78  ..  */.  int idx
2d4ea 3b 0a 7d 20 4f 72 64 65 72 65 64 44 4c 52 65 61  ;.} OrderedDLRea
2d4eb 64 65 72 3b 0a 0a 2f 2a 20 4f 72 64 65 72 20 65  der;../* Order e
2d4ec 6f 66 20 74 6f 20 65 6e 64 2c 20 74 68 65 6e 20  of to end, then 
2d4ed 62 79 20 64 6f 63 69 64 20 61 73 63 2c 20 69 64  by docid asc, id
2d4ee 78 20 64 65 73 63 2e 20 2a 2f 0a 73 74 61 74 69  x desc. */.stati
2d4ef 63 20 69 6e 74 20 6f 72 64 65 72 65 64 44 4c 52  c int orderedDLR
2d4f0 65 61 64 65 72 43 6d 70 28 4f 72 64 65 72 65 64  eaderCmp(Ordered
2d4f1 44 4c 52 65 61 64 65 72 20 2a 72 31 2c 20 4f 72  DLReader *r1, Or
2d4f2 64 65 72 65 64 44 4c 52 65 61 64 65 72 20 2a 72  deredDLReader *r
2d4f3 32 29 7b 0a 20 20 69 66 28 20 64 6c 72 41 74 45  2){.  if( dlrAtE
2d4f4 6e 64 28 72 31 2d 3e 70 52 65 61 64 65 72 29 20  nd(r1->pReader) 
2d4f5 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72 41 74  ){.    if( dlrAt
2d4f6 45 6e 64 28 72 32 2d 3e 70 52 65 61 64 65 72 29  End(r2->pReader)
2d4f7 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a   ) return 0;  /*
2d4f8 20 42 6f 74 68 20 61 74 45 6e 64 28 29 2e 20 2a   Both atEnd(). *
2d4f9 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 20  /.    return 1; 
2d4fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d4fb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d4fc 4f 6e 6c 79 20 72 31 20 61 74 45 6e 64 28 29 2e  Only r1 atEnd().
2d4fd 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 64 6c   */.  }.  if( dl
2d4fe 72 41 74 45 6e 64 28 72 32 2d 3e 70 52 65 61 64  rAtEnd(r2->pRead
2d4ff 65 72 29 20 29 20 72 65 74 75 72 6e 20 2d 31 3b  er) ) return -1;
2d500 20 20 20 2f 2a 20 4f 6e 6c 79 20 72 32 20 61 74     /* Only r2 at
2d501 45 6e 64 28 29 2e 20 2a 2f 0a 0a 20 20 69 66 28  End(). */..  if(
2d502 20 64 6c 72 44 6f 63 69 64 28 72 31 2d 3e 70 52   dlrDocid(r1->pR
2d503 65 61 64 65 72 29 3c 64 6c 72 44 6f 63 69 64 28  eader)<dlrDocid(
2d504 72 32 2d 3e 70 52 65 61 64 65 72 29 20 29 20 72  r2->pReader) ) r
2d505 65 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66 28 20  eturn -1;.  if( 
2d506 64 6c 72 44 6f 63 69 64 28 72 31 2d 3e 70 52 65  dlrDocid(r1->pRe
2d507 61 64 65 72 29 3e 64 6c 72 44 6f 63 69 64 28 72  ader)>dlrDocid(r
2d508 32 2d 3e 70 52 65 61 64 65 72 29 20 29 20 72 65  2->pReader) ) re
2d509 74 75 72 6e 20 31 3b 0a 0a 20 20 2f 2a 20 44 65  turn 1;..  /* De
2d50a 73 63 65 6e 64 69 6e 67 20 6f 6e 20 69 64 78 2e  scending on idx.
2d50b 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 32 2d   */.  return r2-
2d50c 3e 69 64 78 2d 72 31 2d 3e 69 64 78 3b 0a 7d 0a  >idx-r1->idx;.}.
2d50d 0a 2f 2a 20 42 75 62 62 6c 65 20 70 5b 30 5d 20  ./* Bubble p[0] 
2d50e 74 6f 20 61 70 70 72 6f 70 72 69 61 74 65 20 70  to appropriate p
2d50f 6c 61 63 65 20 69 6e 20 70 5b 31 2e 2e 6e 2d 31  lace in p[1..n-1
2d510 5d 2e 20 20 41 73 73 75 6d 65 73 20 74 68 61 74  ].  Assumes that
2d511 0a 2a 2a 20 70 5b 31 2e 2e 6e 2d 31 5d 20 69 73  .** p[1..n-1] is
2d512 20 61 6c 72 65 61 64 79 20 73 6f 72 74 65 64 2e   already sorted.
2d513 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  .*/./* TODO(shes
2d514 73 29 20 49 73 20 74 68 69 73 20 66 72 65 71 75  s) Is this frequ
2d515 65 6e 74 20 65 6e 6f 75 67 68 20 74 6f 20 77 61  ent enough to wa
2d516 72 72 61 6e 74 20 61 20 62 69 6e 61 72 79 20 73  rrant a binary s
2d517 65 61 72 63 68 3f 0a 2a 2a 20 42 65 66 6f 72 65  earch?.** Before
2d518 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68   implementing th
2d519 61 74 2c 20 69 6e 73 74 72 75 6d 65 6e 74 20 74  at, instrument t
2d51a 68 65 20 63 6f 64 65 20 74 6f 20 63 68 65 63 6b  he code to check
2d51b 2e 20 20 49 6e 20 6d 6f 73 74 0a 2a 2a 20 63 75  .  In most.** cu
2d51c 72 72 65 6e 74 20 75 73 61 67 65 2c 20 49 20 65  rrent usage, I e
2d51d 78 70 65 63 74 20 74 68 61 74 20 70 5b 30 5d 20  xpect that p[0] 
2d51e 77 69 6c 6c 20 62 65 20 6c 65 73 73 20 74 68 61  will be less tha
2d51f 6e 20 70 5b 31 5d 20 61 20 76 65 72 79 0a 2a 2a  n p[1] a very.**
2d520 20 68 69 67 68 20 70 72 6f 70 6f 72 74 69 6f 6e   high proportion
2d521 20 6f 66 20 74 68 65 20 74 69 6d 65 2e 0a 2a 2f   of the time..*/
2d522 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 72 64  .static void ord
2d523 65 72 65 64 44 4c 52 65 61 64 65 72 52 65 6f 72  eredDLReaderReor
2d524 64 65 72 28 4f 72 64 65 72 65 64 44 4c 52 65 61  der(OrderedDLRea
2d525 64 65 72 20 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a  der *p, int n){.
2d526 20 20 77 68 69 6c 65 28 20 6e 3e 31 20 26 26 20    while( n>1 && 
2d527 6f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72 43  orderedDLReaderC
2d528 6d 70 28 70 2c 20 70 2b 31 29 3e 30 20 29 7b 0a  mp(p, p+1)>0 ){.
2d529 20 20 20 20 4f 72 64 65 72 65 64 44 4c 52 65 61      OrderedDLRea
2d52a 64 65 72 20 74 6d 70 20 3d 20 70 5b 30 5d 3b 0a  der tmp = p[0];.
2d52b 20 20 20 20 70 5b 30 5d 20 3d 20 70 5b 31 5d 3b      p[0] = p[1];
2d52c 0a 20 20 20 20 70 5b 31 5d 20 3d 20 74 6d 70 3b  .    p[1] = tmp;
2d52d 0a 20 20 20 20 6e 2d 2d 3b 0a 20 20 20 20 70 2b  .    n--;.    p+
2d52e 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 47 69 76  +;.  }.}../* Giv
2d52f 65 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 64  en an array of d
2d530 6f 63 6c 69 73 74 20 72 65 61 64 65 72 73 2c 20  oclist readers, 
2d531 6d 65 72 67 65 20 74 68 65 69 72 20 64 6f 63 6c  merge their docl
2d532 69 73 74 20 65 6c 65 6d 65 6e 74 73 0a 2a 2a 20  ist elements.** 
2d533 69 6e 74 6f 20 6f 75 74 20 69 6e 20 73 6f 72 74  into out in sort
2d534 65 64 20 6f 72 64 65 72 20 28 62 79 20 64 6f 63  ed order (by doc
2d535 69 64 29 2c 20 64 72 6f 70 70 69 6e 67 20 65 6c  id), dropping el
2d536 65 6d 65 6e 74 73 20 66 72 6f 6d 20 6f 6c 64 65  ements from olde
2d537 72 0a 2a 2a 20 72 65 61 64 65 72 73 20 77 68 65  r.** readers whe
2d538 6e 20 74 68 65 72 65 20 69 73 20 61 20 64 75 70  n there is a dup
2d539 6c 69 63 61 74 65 20 64 6f 63 69 64 2e 20 20 70  licate docid.  p
2d53a 52 65 61 64 65 72 73 20 69 73 20 61 73 73 75 6d  Readers is assum
2d53b 65 64 20 74 6f 20 62 65 0a 2a 2a 20 6f 72 64 65  ed to be.** orde
2d53c 72 65 64 20 62 79 20 61 67 65 2c 20 6f 6c 64 65  red by age, olde
2d53d 73 74 20 66 69 72 73 74 2e 0a 2a 2f 0a 2f 2a 20  st first..*/./* 
2d53e 54 4f 44 4f 28 73 68 65 73 73 29 20 6e 52 65 61  TODO(shess) nRea
2d53f 64 65 72 73 20 6d 75 73 74 20 62 65 20 3c 3d 20  ders must be <= 
2d540 4d 45 52 47 45 5f 43 4f 55 4e 54 2e 20 20 54 68  MERGE_COUNT.  Th
2d541 69 73 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 62  is should probab
2d542 6c 79 0a 2a 2a 20 62 65 20 66 69 78 65 64 2e 0a  ly.** be fixed..
2d543 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  */.static void d
2d544 6f 63 4c 69 73 74 4d 65 72 67 65 28 44 61 74 61  ocListMerge(Data
2d545 42 75 66 66 65 72 20 2a 6f 75 74 2c 0a 20 20 20  Buffer *out,.   
2d546 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d547 20 20 20 20 20 20 44 4c 52 65 61 64 65 72 20 2a        DLReader *
2d548 70 52 65 61 64 65 72 73 2c 20 69 6e 74 20 6e 52  pReaders, int nR
2d549 65 61 64 65 72 73 29 7b 0a 20 20 4f 72 64 65 72  eaders){.  Order
2d54a 65 64 44 4c 52 65 61 64 65 72 20 72 65 61 64 65  edDLReader reade
2d54b 72 73 5b 4d 45 52 47 45 5f 43 4f 55 4e 54 5d 3b  rs[MERGE_COUNT];
2d54c 0a 20 20 44 4c 57 72 69 74 65 72 20 77 72 69 74  .  DLWriter writ
2d54d 65 72 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a  er;.  int i, n;.
2d54e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 53    const char *pS
2d54f 74 61 72 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20  tart = 0;.  int 
2d550 6e 53 74 61 72 74 20 3d 20 30 3b 0a 20 20 73 71  nStart = 0;.  sq
2d551 6c 69 74 65 5f 69 6e 74 36 34 20 69 46 69 72 73  lite_int64 iFirs
2d552 74 44 6f 63 69 64 20 3d 20 30 2c 20 69 4c 61 73  tDocid = 0, iLas
2d553 74 44 6f 63 69 64 20 3d 20 30 3b 0a 0a 20 20 61  tDocid = 0;..  a
2d554 73 73 65 72 74 28 20 6e 52 65 61 64 65 72 73 3e  ssert( nReaders>
2d555 30 20 29 3b 0a 20 20 69 66 28 20 6e 52 65 61 64  0 );.  if( nRead
2d556 65 72 73 3d 3d 31 20 29 7b 0a 20 20 20 20 64 61  ers==1 ){.    da
2d557 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 6f  taBufferAppend(o
2d558 75 74 2c 20 64 6c 72 44 6f 63 44 61 74 61 28 70  ut, dlrDocData(p
2d559 52 65 61 64 65 72 73 29 2c 20 64 6c 72 41 6c 6c  Readers), dlrAll
2d55a 44 61 74 61 42 79 74 65 73 28 70 52 65 61 64 65  DataBytes(pReade
2d55b 72 73 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  rs));.    return
2d55c 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  ;.  }..  assert(
2d55d 20 6e 52 65 61 64 65 72 73 3c 3d 4d 45 52 47 45   nReaders<=MERGE
2d55e 5f 43 4f 55 4e 54 20 29 3b 0a 20 20 6e 20 3d 20  _COUNT );.  n = 
2d55f 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  0;.  for(i=0; i<
2d560 6e 52 65 61 64 65 72 73 3b 20 69 2b 2b 29 7b 0a  nReaders; i++){.
2d561 20 20 20 20 61 73 73 65 72 74 28 20 70 52 65 61      assert( pRea
2d562 64 65 72 73 5b 69 5d 2e 69 54 79 70 65 3d 3d 70  ders[i].iType==p
2d563 52 65 61 64 65 72 73 5b 30 5d 2e 69 54 79 70 65  Readers[0].iType
2d564 20 29 3b 0a 20 20 20 20 72 65 61 64 65 72 73 5b   );.    readers[
2d565 69 5d 2e 70 52 65 61 64 65 72 20 3d 20 70 52 65  i].pReader = pRe
2d566 61 64 65 72 73 2b 69 3b 0a 20 20 20 20 72 65 61  aders+i;.    rea
2d567 64 65 72 73 5b 69 5d 2e 69 64 78 20 3d 20 69 3b  ders[i].idx = i;
2d568 0a 20 20 20 20 6e 20 2b 3d 20 64 6c 72 41 6c 6c  .    n += dlrAll
2d569 44 61 74 61 42 79 74 65 73 28 26 70 52 65 61 64  DataBytes(&pRead
2d56a 65 72 73 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 2f  ers[i]);.  }.  /
2d56b 2a 20 43 6f 6e 73 65 72 76 61 74 69 76 65 6c 79  * Conservatively
2d56c 20 73 69 7a 65 20 6f 75 74 70 75 74 20 74 6f 20   size output to 
2d56d 73 75 6d 20 6f 66 20 69 6e 70 75 74 73 2e 20 20  sum of inputs.  
2d56e 4f 75 74 70 75 74 20 73 68 6f 75 6c 64 20 65 6e  Output should en
2d56f 64 0a 20 20 2a 2a 20 75 70 20 73 74 72 69 63 74  d.  ** up strict
2d570 6c 79 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20  ly smaller than 
2d571 69 6e 70 75 74 2e 0a 20 20 2a 2f 0a 20 20 64 61  input..  */.  da
2d572 74 61 42 75 66 66 65 72 45 78 70 61 6e 64 28 6f  taBufferExpand(o
2d573 75 74 2c 20 6e 29 3b 0a 0a 20 20 2f 2a 20 47 65  ut, n);..  /* Ge
2d574 74 20 74 68 65 20 72 65 61 64 65 72 73 20 69 6e  t the readers in
2d575 74 6f 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e  to sorted order.
2d576 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69 2d 2d   */.  while( i--
2d577 3e 30 20 29 7b 0a 20 20 20 20 6f 72 64 65 72 65  >0 ){.    ordere
2d578 64 44 4c 52 65 61 64 65 72 52 65 6f 72 64 65 72  dDLReaderReorder
2d579 28 72 65 61 64 65 72 73 2b 69 2c 20 6e 52 65 61  (readers+i, nRea
2d57a 64 65 72 73 2d 69 29 3b 0a 20 20 7d 0a 0a 20 20  ders-i);.  }..  
2d57b 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72 2c  dlwInit(&writer,
2d57c 20 70 52 65 61 64 65 72 73 5b 30 5d 2e 69 54 79   pReaders[0].iTy
2d57d 70 65 2c 20 6f 75 74 29 3b 0a 20 20 77 68 69 6c  pe, out);.  whil
2d57e 65 28 20 21 64 6c 72 41 74 45 6e 64 28 72 65 61  e( !dlrAtEnd(rea
2d57f 64 65 72 73 5b 30 5d 2e 70 52 65 61 64 65 72 29  ders[0].pReader)
2d580 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 5f 69   ){.    sqlite_i
2d581 6e 74 36 34 20 69 44 6f 63 69 64 20 3d 20 64 6c  nt64 iDocid = dl
2d582 72 44 6f 63 69 64 28 72 65 61 64 65 72 73 5b 30  rDocid(readers[0
2d583 5d 2e 70 52 65 61 64 65 72 29 3b 0a 0a 20 20 20  ].pReader);..   
2d584 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
2d585 20 63 6f 6e 74 69 6e 75 61 74 69 6f 6e 20 6f 66   continuation of
2d586 20 74 68 65 20 63 75 72 72 65 6e 74 20 62 75 66   the current buf
2d587 66 65 72 20 74 6f 20 63 6f 70 79 2c 20 65 78 74  fer to copy, ext
2d588 65 6e 64 0a 20 20 20 20 2a 2a 20 74 68 61 74 20  end.    ** that 
2d589 62 75 66 66 65 72 2e 20 20 6d 65 6d 63 70 79 28  buffer.  memcpy(
2d58a 29 20 73 65 65 6d 73 20 74 6f 20 62 65 20 6d 6f  ) seems to be mo
2d58b 72 65 20 65 66 66 69 63 69 65 6e 74 20 69 66 20  re efficient if 
2d58c 69 74 20 68 61 73 20 61 0a 20 20 20 20 2a 2a 20  it has a.    ** 
2d58d 6c 6f 74 73 20 6f 66 20 64 61 74 61 20 74 6f 20  lots of data to 
2d58e 63 6f 70 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  copy..    */.   
2d58f 20 69 66 28 20 64 6c 72 44 6f 63 44 61 74 61 28   if( dlrDocData(
2d590 72 65 61 64 65 72 73 5b 30 5d 2e 70 52 65 61 64  readers[0].pRead
2d591 65 72 29 3d 3d 70 53 74 61 72 74 2b 6e 53 74 61  er)==pStart+nSta
2d592 72 74 20 29 7b 0a 20 20 20 20 20 20 6e 53 74 61  rt ){.      nSta
2d593 72 74 20 2b 3d 20 64 6c 72 44 6f 63 44 61 74 61  rt += dlrDocData
2d594 42 79 74 65 73 28 72 65 61 64 65 72 73 5b 30 5d  Bytes(readers[0]
2d595 2e 70 52 65 61 64 65 72 29 3b 0a 20 20 20 20 7d  .pReader);.    }
2d596 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20  else{.      if( 
2d597 70 53 74 61 72 74 21 3d 30 20 29 7b 0a 20 20 20  pStart!=0 ){.   
2d598 20 20 20 20 20 64 6c 77 41 70 70 65 6e 64 28 26       dlwAppend(&
2d599 77 72 69 74 65 72 2c 20 70 53 74 61 72 74 2c 20  writer, pStart, 
2d59a 6e 53 74 61 72 74 2c 20 69 46 69 72 73 74 44 6f  nStart, iFirstDo
2d59b 63 69 64 2c 20 69 4c 61 73 74 44 6f 63 69 64 29  cid, iLastDocid)
2d59c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2d59d 70 53 74 61 72 74 20 3d 20 64 6c 72 44 6f 63 44  pStart = dlrDocD
2d59e 61 74 61 28 72 65 61 64 65 72 73 5b 30 5d 2e 70  ata(readers[0].p
2d59f 52 65 61 64 65 72 29 3b 0a 20 20 20 20 20 20 6e  Reader);.      n
2d5a0 53 74 61 72 74 20 3d 20 64 6c 72 44 6f 63 44 61  Start = dlrDocDa
2d5a1 74 61 42 79 74 65 73 28 72 65 61 64 65 72 73 5b  taBytes(readers[
2d5a2 30 5d 2e 70 52 65 61 64 65 72 29 3b 0a 20 20 20  0].pReader);.   
2d5a3 20 20 20 69 46 69 72 73 74 44 6f 63 69 64 20 3d     iFirstDocid =
2d5a4 20 69 44 6f 63 69 64 3b 0a 20 20 20 20 7d 0a 20   iDocid;.    }. 
2d5a5 20 20 20 69 4c 61 73 74 44 6f 63 69 64 20 3d 20     iLastDocid = 
2d5a6 69 44 6f 63 69 64 3b 0a 20 20 20 20 64 6c 72 53  iDocid;.    dlrS
2d5a7 74 65 70 28 72 65 61 64 65 72 73 5b 30 5d 2e 70  tep(readers[0].p
2d5a8 52 65 61 64 65 72 29 3b 0a 0a 20 20 20 20 2f 2a  Reader);..    /*
2d5a9 20 44 72 6f 70 20 61 6c 6c 20 6f 66 20 74 68 65   Drop all of the
2d5aa 20 6f 6c 64 65 72 20 65 6c 65 6d 65 6e 74 73 20   older elements 
2d5ab 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 64 6f  with the same do
2d5ac 63 69 64 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28  cid. */.    for(
2d5ad 69 3d 31 3b 20 69 3c 6e 52 65 61 64 65 72 73 20  i=1; i<nReaders 
2d5ae 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  &&.             
2d5af 21 64 6c 72 41 74 45 6e 64 28 72 65 61 64 65 72  !dlrAtEnd(reader
2d5b0 73 5b 69 5d 2e 70 52 65 61 64 65 72 29 20 26 26  s[i].pReader) &&
2d5b1 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 64 6c  .             dl
2d5b2 72 44 6f 63 69 64 28 72 65 61 64 65 72 73 5b 69  rDocid(readers[i
2d5b3 5d 2e 70 52 65 61 64 65 72 29 3d 3d 69 44 6f 63  ].pReader)==iDoc
2d5b4 69 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  id; i++){.      
2d5b5 64 6c 72 53 74 65 70 28 72 65 61 64 65 72 73 5b  dlrStep(readers[
2d5b6 69 5d 2e 70 52 65 61 64 65 72 29 3b 0a 20 20 20  i].pReader);.   
2d5b7 20 7d 0a 0a 20 20 20 20 2f 2a 20 47 65 74 20 74   }..    /* Get t
2d5b8 68 65 20 72 65 61 64 65 72 73 20 62 61 63 6b 20  he readers back 
2d5b9 69 6e 74 6f 20 6f 72 64 65 72 2e 20 2a 2f 0a 20  into order. */. 
2d5ba 20 20 20 77 68 69 6c 65 28 20 69 2d 2d 3e 30 20     while( i-->0 
2d5bb 29 7b 0a 20 20 20 20 20 20 6f 72 64 65 72 65 64  ){.      ordered
2d5bc 44 4c 52 65 61 64 65 72 52 65 6f 72 64 65 72 28  DLReaderReorder(
2d5bd 72 65 61 64 65 72 73 2b 69 2c 20 6e 52 65 61 64  readers+i, nRead
2d5be 65 72 73 2d 69 29 3b 0a 20 20 20 20 7d 0a 20 20  ers-i);.    }.  
2d5bf 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 6f 76 65  }..  /* Copy ove
2d5c0 72 20 61 6e 79 20 72 65 6d 61 69 6e 69 6e 67 20  r any remaining 
2d5c1 65 6c 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20 20 69  elements. */.  i
2d5c2 66 28 20 6e 53 74 61 72 74 3e 30 20 29 20 64 6c  f( nStart>0 ) dl
2d5c3 77 41 70 70 65 6e 64 28 26 77 72 69 74 65 72 2c  wAppend(&writer,
2d5c4 20 70 53 74 61 72 74 2c 20 6e 53 74 61 72 74 2c   pStart, nStart,
2d5c5 20 69 46 69 72 73 74 44 6f 63 69 64 2c 20 69 4c   iFirstDocid, iL
2d5c6 61 73 74 44 6f 63 69 64 29 3b 0a 20 20 64 6c 77  astDocid);.  dlw
2d5c7 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29  Destroy(&writer)
2d5c8 3b 0a 7d 0a 0a 2f 2a 20 48 65 6c 70 65 72 20 66  ;.}../* Helper f
2d5c9 75 6e 63 74 69 6f 6e 20 66 6f 72 20 70 6f 73 4c  unction for posL
2d5ca 69 73 74 55 6e 69 6f 6e 28 29 2e 20 20 43 6f 6d  istUnion().  Com
2d5cb 70 61 72 65 73 20 74 68 65 20 63 75 72 72 65 6e  pares the curren
2d5cc 74 20 70 6f 73 69 74 69 6f 6e 0a 2a 2a 20 62 65  t position.** be
2d5cd 74 77 65 65 6e 20 6c 65 66 74 20 61 6e 64 20 72  tween left and r
2d5ce 69 67 68 74 2c 20 72 65 74 75 72 6e 69 6e 67 20  ight, returning 
2d5cf 61 73 20 73 74 61 6e 64 61 72 64 20 43 20 69 64  as standard C id
2d5d0 69 6f 6d 20 6f 66 20 3c 30 20 69 66 0a 2a 2a 20  iom of <0 if.** 
2d5d1 6c 65 66 74 3c 72 69 67 68 74 2c 20 3e 30 20 69  left<right, >0 i
2d5d2 66 20 6c 65 66 74 3e 72 69 67 68 74 2c 20 61 6e  f left>right, an
2d5d3 64 20 30 20 69 66 20 6c 65 66 74 3d 3d 72 69 67  d 0 if left==rig
2d5d4 68 74 2e 20 20 22 45 6e 64 22 20 61 6c 77 61 79  ht.  "End" alway
2d5d5 73 0a 2a 2a 20 63 6f 6d 70 61 72 65 73 20 67 72  s.** compares gr
2d5d6 65 61 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  eater..*/.static
2d5d7 20 69 6e 74 20 70 6f 73 4c 69 73 74 43 6d 70 28   int posListCmp(
2d5d8 50 4c 52 65 61 64 65 72 20 2a 70 4c 65 66 74 2c  PLReader *pLeft,
2d5d9 20 50 4c 52 65 61 64 65 72 20 2a 70 52 69 67 68   PLReader *pRigh
2d5da 74 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4c  t){.  assert( pL
2d5db 65 66 74 2d 3e 69 54 79 70 65 3d 3d 70 52 69 67  eft->iType==pRig
2d5dc 68 74 2d 3e 69 54 79 70 65 20 29 3b 0a 20 20 69  ht->iType );.  i
2d5dd 66 28 20 70 4c 65 66 74 2d 3e 69 54 79 70 65 3d  f( pLeft->iType=
2d5de 3d 44 4c 5f 44 4f 43 49 44 53 20 29 20 72 65 74  =DL_DOCIDS ) ret
2d5df 75 72 6e 20 30 3b 0a 0a 20 20 69 66 28 20 70 6c  urn 0;..  if( pl
2d5e0 72 41 74 45 6e 64 28 70 4c 65 66 74 29 20 29 20  rAtEnd(pLeft) ) 
2d5e1 72 65 74 75 72 6e 20 70 6c 72 41 74 45 6e 64 28  return plrAtEnd(
2d5e2 70 52 69 67 68 74 29 20 3f 20 30 20 3a 20 31 3b  pRight) ? 0 : 1;
2d5e3 0a 20 20 69 66 28 20 70 6c 72 41 74 45 6e 64 28  .  if( plrAtEnd(
2d5e4 70 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e  pRight) ) return
2d5e5 20 2d 31 3b 0a 0a 20 20 69 66 28 20 70 6c 72 43   -1;..  if( plrC
2d5e6 6f 6c 75 6d 6e 28 70 4c 65 66 74 29 3c 70 6c 72  olumn(pLeft)<plr
2d5e7 43 6f 6c 75 6d 6e 28 70 52 69 67 68 74 29 20 29  Column(pRight) )
2d5e8 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66   return -1;.  if
2d5e9 28 20 70 6c 72 43 6f 6c 75 6d 6e 28 70 4c 65 66  ( plrColumn(pLef
2d5ea 74 29 3e 70 6c 72 43 6f 6c 75 6d 6e 28 70 52 69  t)>plrColumn(pRi
2d5eb 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 31 3b  ght) ) return 1;
2d5ec 0a 0a 20 20 69 66 28 20 70 6c 72 50 6f 73 69 74  ..  if( plrPosit
2d5ed 69 6f 6e 28 70 4c 65 66 74 29 3c 70 6c 72 50 6f  ion(pLeft)<plrPo
2d5ee 73 69 74 69 6f 6e 28 70 52 69 67 68 74 29 20 29  sition(pRight) )
2d5ef 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66   return -1;.  if
2d5f0 28 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70 4c  ( plrPosition(pL
2d5f1 65 66 74 29 3e 70 6c 72 50 6f 73 69 74 69 6f 6e  eft)>plrPosition
2d5f2 28 70 52 69 67 68 74 29 20 29 20 72 65 74 75 72  (pRight) ) retur
2d5f3 6e 20 31 3b 0a 20 20 69 66 28 20 70 4c 65 66 74  n 1;.  if( pLeft
2d5f4 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49  ->iType==DL_POSI
2d5f5 54 49 4f 4e 53 20 29 20 72 65 74 75 72 6e 20 30  TIONS ) return 0
2d5f6 3b 0a 0a 20 20 69 66 28 20 70 6c 72 53 74 61 72  ;..  if( plrStar
2d5f7 74 4f 66 66 73 65 74 28 70 4c 65 66 74 29 3c 70  tOffset(pLeft)<p
2d5f8 6c 72 53 74 61 72 74 4f 66 66 73 65 74 28 70 52  lrStartOffset(pR
2d5f9 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 2d  ight) ) return -
2d5fa 31 3b 0a 20 20 69 66 28 20 70 6c 72 53 74 61 72  1;.  if( plrStar
2d5fb 74 4f 66 66 73 65 74 28 70 4c 65 66 74 29 3e 70  tOffset(pLeft)>p
2d5fc 6c 72 53 74 61 72 74 4f 66 66 73 65 74 28 70 52  lrStartOffset(pR
2d5fd 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 31  ight) ) return 1
2d5fe 3b 0a 0a 20 20 69 66 28 20 70 6c 72 45 6e 64 4f  ;..  if( plrEndO
2d5ff 66 66 73 65 74 28 70 4c 65 66 74 29 3c 70 6c 72  ffset(pLeft)<plr
2d600 45 6e 64 4f 66 66 73 65 74 28 70 52 69 67 68 74  EndOffset(pRight
2d601 29 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20  ) ) return -1;. 
2d602 20 69 66 28 20 70 6c 72 45 6e 64 4f 66 66 73 65   if( plrEndOffse
2d603 74 28 70 4c 65 66 74 29 3e 70 6c 72 45 6e 64 4f  t(pLeft)>plrEndO
2d604 66 66 73 65 74 28 70 52 69 67 68 74 29 20 29 20  ffset(pRight) ) 
2d605 72 65 74 75 72 6e 20 31 3b 0a 0a 20 20 72 65 74  return 1;..  ret
2d606 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 57 72 69  urn 0;.}../* Wri
2d607 74 65 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20  te the union of 
2d608 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 73 20 69  position lists i
2d609 6e 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67  n pLeft and pRig
2d60a 68 74 20 74 6f 20 70 4f 75 74 2e 0a 2a 2a 20 22  ht to pOut..** "
2d60b 55 6e 69 6f 6e 22 20 69 6e 20 74 68 69 73 20 63  Union" in this c
2d60c 61 73 65 20 6d 65 61 6e 69 6e 67 20 22 41 6c 6c  ase meaning "All
2d60d 20 75 6e 69 71 75 65 20 70 6f 73 69 74 69 6f 6e   unique position
2d60e 20 74 75 70 6c 65 73 22 2e 20 20 53 68 6f 75 6c   tuples".  Shoul
2d60f 64 0a 2a 2a 20 77 6f 72 6b 20 77 69 74 68 20 61  d.** work with a
2d610 6e 79 20 64 6f 63 6c 69 73 74 20 74 79 70 65 2c  ny doclist type,
2d611 20 74 68 6f 75 67 68 20 62 6f 74 68 20 69 6e 70   though both inp
2d612 75 74 73 20 61 6e 64 20 74 68 65 20 6f 75 74 70  uts and the outp
2d613 75 74 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20  ut.** should be 
2d614 74 68 65 20 73 61 6d 65 20 74 79 70 65 2e 0a 2a  the same type..*
2d615 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 6f  /.static void po
2d616 73 4c 69 73 74 55 6e 69 6f 6e 28 44 4c 52 65 61  sListUnion(DLRea
2d617 64 65 72 20 2a 70 4c 65 66 74 2c 20 44 4c 52 65  der *pLeft, DLRe
2d618 61 64 65 72 20 2a 70 52 69 67 68 74 2c 20 44 4c  ader *pRight, DL
2d619 57 72 69 74 65 72 20 2a 70 4f 75 74 29 7b 0a 20  Writer *pOut){. 
2d61a 20 50 4c 52 65 61 64 65 72 20 6c 65 66 74 2c 20   PLReader left, 
2d61b 72 69 67 68 74 3b 0a 20 20 50 4c 57 72 69 74 65  right;.  PLWrite
2d61c 72 20 77 72 69 74 65 72 3b 0a 0a 20 20 61 73 73  r writer;..  ass
2d61d 65 72 74 28 20 64 6c 72 44 6f 63 69 64 28 70 4c  ert( dlrDocid(pL
2d61e 65 66 74 29 3d 3d 64 6c 72 44 6f 63 69 64 28 70  eft)==dlrDocid(p
2d61f 52 69 67 68 74 29 20 29 3b 0a 20 20 61 73 73 65  Right) );.  asse
2d620 72 74 28 20 70 4c 65 66 74 2d 3e 69 54 79 70 65  rt( pLeft->iType
2d621 3d 3d 70 52 69 67 68 74 2d 3e 69 54 79 70 65 20  ==pRight->iType 
2d622 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65  );.  assert( pLe
2d623 66 74 2d 3e 69 54 79 70 65 3d 3d 70 4f 75 74 2d  ft->iType==pOut-
2d624 3e 69 54 79 70 65 20 29 3b 0a 0a 20 20 70 6c 72  >iType );..  plr
2d625 49 6e 69 74 28 26 6c 65 66 74 2c 20 70 4c 65 66  Init(&left, pLef
2d626 74 29 3b 0a 20 20 70 6c 72 49 6e 69 74 28 26 72  t);.  plrInit(&r
2d627 69 67 68 74 2c 20 70 52 69 67 68 74 29 3b 0a 20  ight, pRight);. 
2d628 20 70 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72   plwInit(&writer
2d629 2c 20 70 4f 75 74 2c 20 64 6c 72 44 6f 63 69 64  , pOut, dlrDocid
2d62a 28 70 4c 65 66 74 29 29 3b 0a 0a 20 20 77 68 69  (pLeft));..  whi
2d62b 6c 65 28 20 21 70 6c 72 41 74 45 6e 64 28 26 6c  le( !plrAtEnd(&l
2d62c 65 66 74 29 20 7c 7c 20 21 70 6c 72 41 74 45 6e  eft) || !plrAtEn
2d62d 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20  d(&right) ){.   
2d62e 20 69 6e 74 20 63 20 3d 20 70 6f 73 4c 69 73 74   int c = posList
2d62f 43 6d 70 28 26 6c 65 66 74 2c 20 26 72 69 67 68  Cmp(&left, &righ
2d630 74 29 3b 0a 20 20 20 20 69 66 28 20 63 3c 30 20  t);.    if( c<0 
2d631 29 7b 0a 20 20 20 20 20 20 70 6c 77 43 6f 70 79  ){.      plwCopy
2d632 28 26 77 72 69 74 65 72 2c 20 26 6c 65 66 74 29  (&writer, &left)
2d633 3b 0a 20 20 20 20 20 20 70 6c 72 53 74 65 70 28  ;.      plrStep(
2d634 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73  &left);.    }els
2d635 65 20 69 66 28 20 63 3e 30 20 29 7b 0a 20 20 20  e if( c>0 ){.   
2d636 20 20 20 70 6c 77 43 6f 70 79 28 26 77 72 69 74     plwCopy(&writ
2d637 65 72 2c 20 26 72 69 67 68 74 29 3b 0a 20 20 20  er, &right);.   
2d638 20 20 20 70 6c 72 53 74 65 70 28 26 72 69 67 68     plrStep(&righ
2d639 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  t);.    }else{. 
2d63a 20 20 20 20 20 70 6c 77 43 6f 70 79 28 26 77 72       plwCopy(&wr
2d63b 69 74 65 72 2c 20 26 6c 65 66 74 29 3b 0a 20 20  iter, &left);.  
2d63c 20 20 20 20 70 6c 72 53 74 65 70 28 26 6c 65 66      plrStep(&lef
2d63d 74 29 3b 0a 20 20 20 20 20 20 70 6c 72 53 74 65  t);.      plrSte
2d63e 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d  p(&right);.    }
2d63f 0a 20 20 7d 0a 0a 20 20 70 6c 77 54 65 72 6d 69  .  }..  plwTermi
2d640 6e 61 74 65 28 26 77 72 69 74 65 72 29 3b 0a 20  nate(&writer);. 
2d641 20 70 6c 77 44 65 73 74 72 6f 79 28 26 77 72 69   plwDestroy(&wri
2d642 74 65 72 29 3b 0a 20 20 70 6c 72 44 65 73 74 72  ter);.  plrDestr
2d643 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20 70 6c 72  oy(&left);.  plr
2d644 44 65 73 74 72 6f 79 28 26 72 69 67 68 74 29 3b  Destroy(&right);
2d645 0a 7d 0a 0a 2f 2a 20 57 72 69 74 65 20 74 68 65  .}../* Write the
2d646 20 75 6e 69 6f 6e 20 6f 66 20 64 6f 63 6c 69 73   union of doclis
2d647 74 73 20 69 6e 20 70 4c 65 66 74 20 61 6e 64 20  ts in pLeft and 
2d648 70 52 69 67 68 74 20 74 6f 20 70 4f 75 74 2e 20  pRight to pOut. 
2d649 20 46 6f 72 0a 2a 2a 20 64 6f 63 69 64 73 20 69   For.** docids i
2d64a 6e 20 63 6f 6d 6d 6f 6e 20 62 65 74 77 65 65 6e  n common between
2d64b 20 74 68 65 20 69 6e 70 75 74 73 2c 20 74 68 65   the inputs, the
2d64c 20 75 6e 69 6f 6e 20 6f 66 20 74 68 65 20 70 6f   union of the po
2d64d 73 69 74 69 6f 6e 0a 2a 2a 20 6c 69 73 74 73 20  sition.** lists 
2d64e 69 73 20 77 72 69 74 74 65 6e 2e 20 20 49 6e 70  is written.  Inp
2d64f 75 74 73 20 61 6e 64 20 6f 75 74 70 75 74 73 20  uts and outputs 
2d650 61 72 65 20 61 6c 77 61 79 73 20 74 79 70 65 20  are always type 
2d651 44 4c 5f 44 45 46 41 55 4c 54 2e 0a 2a 2f 0a 73  DL_DEFAULT..*/.s
2d652 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63 4c 69  tatic void docLi
2d653 73 74 55 6e 69 6f 6e 28 0a 20 20 63 6f 6e 73 74  stUnion(.  const
2d654 20 63 68 61 72 20 2a 70 4c 65 66 74 2c 20 69 6e   char *pLeft, in
2d655 74 20 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e 73 74  t nLeft,.  const
2d656 20 63 68 61 72 20 2a 70 52 69 67 68 74 2c 20 69   char *pRight, i
2d657 6e 74 20 6e 52 69 67 68 74 2c 0a 20 20 44 61 74  nt nRight,.  Dat
2d658 61 42 75 66 66 65 72 20 2a 70 4f 75 74 20 20 20  aBuffer *pOut   
2d659 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
2d65a 63 6f 6d 62 69 6e 65 64 20 64 6f 63 6c 69 73 74  combined doclist
2d65b 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 44 4c   here */.){.  DL
2d65c 52 65 61 64 65 72 20 6c 65 66 74 2c 20 72 69 67  Reader left, rig
2d65d 68 74 3b 0a 20 20 44 4c 57 72 69 74 65 72 20 77  ht;.  DLWriter w
2d65e 72 69 74 65 72 3b 0a 0a 20 20 69 66 28 20 6e 4c  riter;..  if( nL
2d65f 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  eft==0 ){.    if
2d660 28 20 6e 52 69 67 68 74 21 3d 30 29 20 64 61 74  ( nRight!=0) dat
2d661 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 4f  aBufferAppend(pO
2d662 75 74 2c 20 70 52 69 67 68 74 2c 20 6e 52 69 67  ut, pRight, nRig
2d663 68 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ht);.    return;
2d664 0a 20 20 7d 0a 20 20 69 66 28 20 6e 52 69 67 68  .  }.  if( nRigh
2d665 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64 61 74 61  t==0 ){.    data
2d666 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 4f 75  BufferAppend(pOu
2d667 74 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29  t, pLeft, nLeft)
2d668 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
2d669 7d 0a 0a 20 20 64 6c 72 49 6e 69 74 28 26 6c 65  }..  dlrInit(&le
2d66a 66 74 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20  ft, DL_DEFAULT, 
2d66b 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20  pLeft, nLeft);. 
2d66c 20 64 6c 72 49 6e 69 74 28 26 72 69 67 68 74 2c   dlrInit(&right,
2d66d 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70 52 69   DL_DEFAULT, pRi
2d66e 67 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20  ght, nRight);.  
2d66f 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72 2c  dlwInit(&writer,
2d670 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70 4f 75   DL_DEFAULT, pOu
2d671 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 21 64  t);..  while( !d
2d672 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29 20 7c  lrAtEnd(&left) |
2d673 7c 20 21 64 6c 72 41 74 45 6e 64 28 26 72 69 67  | !dlrAtEnd(&rig
2d674 68 74 29 20 29 7b 0a 20 20 20 20 69 66 28 20 64  ht) ){.    if( d
2d675 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29 20  lrAtEnd(&right) 
2d676 29 7b 0a 20 20 20 20 20 20 64 6c 77 43 6f 70 79  ){.      dlwCopy
2d677 28 26 77 72 69 74 65 72 2c 20 26 6c 65 66 74 29  (&writer, &left)
2d678 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28  ;.      dlrStep(
2d679 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73  &left);.    }els
2d67a 65 20 69 66 28 20 64 6c 72 41 74 45 6e 64 28 26  e if( dlrAtEnd(&
2d67b 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20 64  left) ){.      d
2d67c 6c 77 43 6f 70 79 28 26 77 72 69 74 65 72 2c 20  lwCopy(&writer, 
2d67d 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20 20 64  &right);.      d
2d67e 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a  lrStep(&right);.
2d67f 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 64 6c      }else if( dl
2d680 72 44 6f 63 69 64 28 26 6c 65 66 74 29 3c 64 6c  rDocid(&left)<dl
2d681 72 44 6f 63 69 64 28 26 72 69 67 68 74 29 20 29  rDocid(&right) )
2d682 7b 0a 20 20 20 20 20 20 64 6c 77 43 6f 70 79 28  {.      dlwCopy(
2d683 26 77 72 69 74 65 72 2c 20 26 6c 65 66 74 29 3b  &writer, &left);
2d684 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26  .      dlrStep(&
2d685 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  left);.    }else
2d686 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28 26 6c   if( dlrDocid(&l
2d687 65 66 74 29 3e 64 6c 72 44 6f 63 69 64 28 26 72  eft)>dlrDocid(&r
2d688 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 64  ight) ){.      d
2d689 6c 77 43 6f 70 79 28 26 77 72 69 74 65 72 2c 20  lwCopy(&writer, 
2d68a 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20 20 64  &right);.      d
2d68b 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a  lrStep(&right);.
2d68c 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2d68d 20 70 6f 73 4c 69 73 74 55 6e 69 6f 6e 28 26 6c   posListUnion(&l
2d68e 65 66 74 2c 20 26 72 69 67 68 74 2c 20 26 77 72  eft, &right, &wr
2d68f 69 74 65 72 29 3b 0a 20 20 20 20 20 20 64 6c 72  iter);.      dlr
2d690 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20  Step(&left);.   
2d691 20 20 20 64 6c 72 53 74 65 70 28 26 72 69 67 68     dlrStep(&righ
2d692 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  t);.    }.  }.. 
2d693 20 64 6c 72 44 65 73 74 72 6f 79 28 26 6c 65 66   dlrDestroy(&lef
2d694 74 29 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f 79  t);.  dlrDestroy
2d695 28 26 72 69 67 68 74 29 3b 0a 20 20 64 6c 77 44  (&right);.  dlwD
2d696 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b  estroy(&writer);
2d697 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20  .}../* .** This 
2d698 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
2d699 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20   as part of the 
2d69a 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
2d69b 66 20 70 68 72 61 73 65 20 61 6e 64 0a 2a 2a 20  f phrase and.** 
2d69c 4e 45 41 52 20 6d 61 74 63 68 69 6e 67 2e 0a 2a  NEAR matching..*
2d69d 2a 0a 2a 2a 20 70 4c 65 66 74 20 61 6e 64 20 70  *.** pLeft and p
2d69e 52 69 67 68 74 20 61 72 65 20 44 4c 52 65 61 64  Right are DLRead
2d69f 65 72 73 20 70 6f 73 69 74 69 6f 6e 65 64 20 74  ers positioned t
2d6a0 6f 20 74 68 65 20 73 61 6d 65 20 64 6f 63 69 64  o the same docid
2d6a1 20 69 6e 0a 2a 2a 20 6c 69 73 74 73 20 6f 66 20   in.** lists of 
2d6a2 74 79 70 65 20 44 4c 5f 50 4f 53 49 54 49 4f 4e  type DL_POSITION
2d6a3 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
2d6a4 77 72 69 74 65 73 20 61 6e 20 65 6e 74 72 79 20  writes an entry 
2d6a5 74 6f 20 74 68 65 0a 2a 2a 20 44 4c 57 72 69 74  to the.** DLWrit
2d6a6 65 72 20 70 4f 75 74 20 66 6f 72 20 65 61 63 68  er pOut for each
2d6a7 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 70 52 69   position in pRi
2d6a8 67 68 74 20 74 68 61 74 20 69 73 20 6c 65 73 73  ght that is less
2d6a9 20 74 68 61 6e 0a 2a 2a 20 28 6e 4e 65 61 72 2b   than.** (nNear+
2d6aa 31 29 20 67 72 65 61 74 65 72 20 28 62 75 74 20  1) greater (but 
2d6ab 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 6f 72 20  not equal to or 
2d6ac 73 6d 61 6c 6c 65 72 29 20 74 68 61 6e 20 61 20  smaller) than a 
2d6ad 70 6f 73 69 74 69 6f 6e 20 0a 2a 2a 20 69 6e 20  position .** in 
2d6ae 70 4c 65 66 74 2e 20 46 6f 72 20 65 78 61 6d 70  pLeft. For examp
2d6af 6c 65 2c 20 69 66 20 6e 4e 65 61 72 20 69 73 20  le, if nNear is 
2d6b0 30 2c 20 61 6e 64 20 74 68 65 20 70 6f 73 69 74  0, and the posit
2d6b1 69 6f 6e 73 20 63 6f 6e 74 61 69 6e 65 64 0a 2a  ions contained.*
2d6b2 2a 20 62 79 20 70 4c 65 66 74 20 61 6e 64 20 70  * by pLeft and p
2d6b3 52 69 67 68 74 20 61 72 65 3a 0a 2a 2a 0a 2a 2a  Right are:.**.**
2d6b4 20 20 20 20 70 4c 65 66 74 3a 20 20 35 20 31 30      pLeft:  5 10
2d6b5 20 31 35 20 32 30 0a 2a 2a 20 20 20 20 70 52 69   15 20.**    pRi
2d6b6 67 68 74 3a 20 36 20 20 39 20 31 37 20 32 31 0a  ght: 6  9 17 21.
2d6b7 2a 2a 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 64  **.** then the d
2d6b8 6f 63 69 64 20 69 73 20 61 64 64 65 64 20 74 6f  ocid is added to
2d6b9 20 70 4f 75 74 2e 20 49 66 20 70 4f 75 74 20 69   pOut. If pOut i
2d6ba 73 20 6f 66 20 74 79 70 65 20 44 4c 5f 50 4f 53  s of type DL_POS
2d6bb 49 54 49 4f 4e 53 2c 0a 2a 2a 20 74 68 65 6e 20  ITIONS,.** then 
2d6bc 61 20 70 6f 73 69 74 69 6f 6e 69 64 73 20 22 36  a positionids "6
2d6bd 22 20 61 6e 64 20 22 32 31 22 20 61 72 65 20 61  " and "21" are a
2d6be 6c 73 6f 20 61 64 64 65 64 20 74 6f 20 70 4f 75  lso added to pOu
2d6bf 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f 6f 6c  t..**.** If bool
2d6c0 65 61 6e 20 61 72 67 75 6d 65 6e 74 20 69 73 53  ean argument isS
2d6c1 61 76 65 4c 65 66 74 20 69 73 20 74 72 75 65 2c  aveLeft is true,
2d6c2 20 74 68 65 6e 20 70 6f 73 69 74 69 6f 6e 69 64   then positionid
2d6c3 73 20 61 72 65 20 63 6f 70 69 65 64 0a 2a 2a 20  s are copied.** 
2d6c4 66 72 6f 6d 20 70 4c 65 66 74 20 69 6e 73 74 65  from pLeft inste
2d6c5 61 64 20 6f 66 20 70 52 69 67 68 74 2e 20 49 6e  ad of pRight. In
2d6c6 20 74 68 65 20 65 78 61 6d 70 6c 65 20 61 62 6f   the example abo
2d6c7 76 65 2c 20 74 68 65 20 70 6f 73 69 74 69 6f 6e  ve, the position
2d6c8 73 20 22 35 22 0a 2a 2a 20 61 6e 64 20 22 32 30  s "5".** and "20
2d6c9 22 20 77 6f 75 6c 64 20 62 65 20 61 64 64 65 64  " would be added
2d6ca 20 69 6e 73 74 65 61 64 20 6f 66 20 22 36 22 20   instead of "6" 
2d6cb 61 6e 64 20 22 32 31 22 2e 0a 2a 2f 0a 73 74 61  and "21"..*/.sta
2d6cc 74 69 63 20 76 6f 69 64 20 70 6f 73 4c 69 73 74  tic void posList
2d6cd 50 68 72 61 73 65 4d 65 72 67 65 28 0a 20 20 44  PhraseMerge(.  D
2d6ce 4c 52 65 61 64 65 72 20 2a 70 4c 65 66 74 2c 20  LReader *pLeft, 
2d6cf 0a 20 20 44 4c 52 65 61 64 65 72 20 2a 70 52 69  .  DLReader *pRi
2d6d0 67 68 74 2c 0a 20 20 69 6e 74 20 6e 4e 65 61 72  ght,.  int nNear
2d6d1 2c 0a 20 20 69 6e 74 20 69 73 53 61 76 65 4c 65  ,.  int isSaveLe
2d6d2 66 74 2c 0a 20 20 44 4c 57 72 69 74 65 72 20 2a  ft,.  DLWriter *
2d6d3 70 4f 75 74 0a 29 7b 0a 20 20 50 4c 52 65 61 64  pOut.){.  PLRead
2d6d4 65 72 20 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a  er left, right;.
2d6d5 20 20 50 4c 57 72 69 74 65 72 20 77 72 69 74 65    PLWriter write
2d6d6 72 3b 0a 20 20 69 6e 74 20 6d 61 74 63 68 20 3d  r;.  int match =
2d6d7 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64   0;..  assert( d
2d6d8 6c 72 44 6f 63 69 64 28 70 4c 65 66 74 29 3d 3d  lrDocid(pLeft)==
2d6d9 64 6c 72 44 6f 63 69 64 28 70 52 69 67 68 74 29  dlrDocid(pRight)
2d6da 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
2d6db 75 74 2d 3e 69 54 79 70 65 21 3d 44 4c 5f 50 4f  ut->iType!=DL_PO
2d6dc 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20  SITIONS_OFFSETS 
2d6dd 29 3b 0a 0a 20 20 70 6c 72 49 6e 69 74 28 26 6c  );..  plrInit(&l
2d6de 65 66 74 2c 20 70 4c 65 66 74 29 3b 0a 20 20 70  eft, pLeft);.  p
2d6df 6c 72 49 6e 69 74 28 26 72 69 67 68 74 2c 20 70  lrInit(&right, p
2d6e0 52 69 67 68 74 29 3b 0a 0a 20 20 77 68 69 6c 65  Right);..  while
2d6e1 28 20 21 70 6c 72 41 74 45 6e 64 28 26 6c 65 66  ( !plrAtEnd(&lef
2d6e2 74 29 20 26 26 20 21 70 6c 72 41 74 45 6e 64 28  t) && !plrAtEnd(
2d6e3 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 69  &right) ){.    i
2d6e4 66 28 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 6c 65  f( plrColumn(&le
2d6e5 66 74 29 3c 70 6c 72 43 6f 6c 75 6d 6e 28 26 72  ft)<plrColumn(&r
2d6e6 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 70  ight) ){.      p
2d6e7 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20  lrStep(&left);. 
2d6e8 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 6c 72     }else if( plr
2d6e9 43 6f 6c 75 6d 6e 28 26 6c 65 66 74 29 3e 70 6c  Column(&left)>pl
2d6ea 72 43 6f 6c 75 6d 6e 28 26 72 69 67 68 74 29 20  rColumn(&right) 
2d6eb 29 7b 0a 20 20 20 20 20 20 70 6c 72 53 74 65 70  ){.      plrStep
2d6ec 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 65  (&right);.    }e
2d6ed 6c 73 65 20 69 66 28 20 70 6c 72 50 6f 73 69 74  lse if( plrPosit
2d6ee 69 6f 6e 28 26 6c 65 66 74 29 3e 3d 70 6c 72 50  ion(&left)>=plrP
2d6ef 6f 73 69 74 69 6f 6e 28 26 72 69 67 68 74 29 20  osition(&right) 
2d6f0 29 7b 0a 20 20 20 20 20 20 70 6c 72 53 74 65 70  ){.      plrStep
2d6f1 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 65  (&right);.    }e
2d6f2 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 28  lse{.      if( (
2d6f3 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26 72 69 67  plrPosition(&rig
2d6f4 68 74 29 2d 70 6c 72 50 6f 73 69 74 69 6f 6e 28  ht)-plrPosition(
2d6f5 26 6c 65 66 74 29 29 3c 3d 28 6e 4e 65 61 72 2b  &left))<=(nNear+
2d6f6 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  1) ){.        if
2d6f7 28 20 21 6d 61 74 63 68 20 29 7b 0a 20 20 20 20  ( !match ){.    
2d6f8 20 20 20 20 20 20 70 6c 77 49 6e 69 74 28 26 77        plwInit(&w
2d6f9 72 69 74 65 72 2c 20 70 4f 75 74 2c 20 64 6c 72  riter, pOut, dlr
2d6fa 44 6f 63 69 64 28 70 4c 65 66 74 29 29 3b 0a 20  Docid(pLeft));. 
2d6fb 20 20 20 20 20 20 20 20 20 6d 61 74 63 68 20 3d           match =
2d6fc 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   1;.        }.  
2d6fd 20 20 20 20 20 20 69 66 28 20 21 69 73 53 61 76        if( !isSav
2d6fe 65 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20  eLeft ){.       
2d6ff 20 20 20 70 6c 77 41 64 64 28 26 77 72 69 74 65     plwAdd(&write
2d700 72 2c 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 72 69  r, plrColumn(&ri
2d701 67 68 74 29 2c 20 70 6c 72 50 6f 73 69 74 69 6f  ght), plrPositio
2d702 6e 28 26 72 69 67 68 74 29 2c 20 30 2c 20 30 29  n(&right), 0, 0)
2d703 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
2d704 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77 41 64  .          plwAd
2d705 64 28 26 77 72 69 74 65 72 2c 20 70 6c 72 43 6f  d(&writer, plrCo
2d706 6c 75 6d 6e 28 26 6c 65 66 74 29 2c 20 70 6c 72  lumn(&left), plr
2d707 50 6f 73 69 74 69 6f 6e 28 26 6c 65 66 74 29 2c  Position(&left),
2d708 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   0, 0);.        
2d709 7d 0a 20 20 20 20 20 20 20 20 70 6c 72 53 74 65  }.        plrSte
2d70a 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20  p(&right);.     
2d70b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2d70c 70 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a  plrStep(&left);.
2d70d 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2d70e 7d 0a 0a 20 20 69 66 28 20 6d 61 74 63 68 20 29  }..  if( match )
2d70f 7b 0a 20 20 20 20 70 6c 77 54 65 72 6d 69 6e 61  {.    plwTermina
2d710 74 65 28 26 77 72 69 74 65 72 29 3b 0a 20 20 20  te(&writer);.   
2d711 20 70 6c 77 44 65 73 74 72 6f 79 28 26 77 72 69   plwDestroy(&wri
2d712 74 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 70 6c 72  ter);.  }..  plr
2d713 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a  Destroy(&left);.
2d714 20 20 70 6c 72 44 65 73 74 72 6f 79 28 26 72 69    plrDestroy(&ri
2d715 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ght);.}../*.** C
2d716 6f 6d 70 61 72 65 20 74 68 65 20 76 61 6c 75 65  ompare the value
2d717 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  s pointed to by 
2d718 74 68 65 20 50 4c 52 65 61 64 65 72 73 20 70 61  the PLReaders pa
2d719 73 73 65 64 20 61 73 20 61 72 67 75 6d 65 6e 74  ssed as argument
2d71a 73 2e 20 0a 2a 2a 20 52 65 74 75 72 6e 20 2d 31  s. .** Return -1
2d71b 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 70 6f   if the value po
2d71c 69 6e 74 65 64 20 74 6f 20 62 79 20 70 4c 65 66  inted to by pLef
2d71d 74 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  t is considered 
2d71e 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 74 68 65  less than.** the
2d71f 20 76 61 6c 75 65 20 70 6f 69 6e 74 65 64 20 74   value pointed t
2d720 6f 20 62 79 20 70 52 69 67 68 74 2c 20 2b 31 20  o by pRight, +1 
2d721 69 66 20 69 74 20 69 73 20 63 6f 6e 73 69 64 65  if it is conside
2d722 72 65 64 20 67 72 65 61 74 65 72 0a 2a 2a 20 74  red greater.** t
2d723 68 61 6e 20 69 74 2c 20 6f 72 20 30 20 69 66 20  han it, or 0 if 
2d724 69 74 20 69 73 20 65 71 75 61 6c 2e 20 69 2e 65  it is equal. i.e
2d725 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 2a 70 4c  ..**.**     (*pL
2d726 65 66 74 20 2d 20 2a 70 52 69 67 68 74 29 0a 2a  eft - *pRight).*
2d727 2a 0a 2a 2a 20 41 20 50 4c 52 65 61 64 65 72 20  *.** A PLReader 
2d728 74 68 61 74 20 69 73 20 69 6e 20 74 68 65 20 45  that is in the E
2d729 4f 46 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 20  OF condition is 
2d72a 63 6f 6e 73 69 64 65 72 65 64 20 67 72 65 61 74  considered great
2d72b 65 72 20 74 68 61 6e 0a 2a 2a 20 61 6e 79 20 6f  er than.** any o
2d72c 74 68 65 72 2e 20 49 66 20 6e 65 69 74 68 65 72  ther. If neither
2d72d 20 61 72 67 75 6d 65 6e 74 20 69 73 20 69 6e 20   argument is in 
2d72e 45 4f 46 20 73 74 61 74 65 2c 20 74 68 65 20 72  EOF state, the r
2d72f 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 0a 2a  eturn value of.*
2d730 2a 20 70 6c 72 43 6f 6c 75 6d 6e 28 29 20 69 73  * plrColumn() is
2d731 20 75 73 65 64 2e 20 49 66 20 74 68 65 20 70 6c   used. If the pl
2d732 72 43 6f 6c 75 6d 6e 28 29 20 76 61 6c 75 65 73  rColumn() values
2d733 20 61 72 65 20 65 71 75 61 6c 2c 20 74 68 65 0a   are equal, the.
2d734 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73  ** comparison is
2d735 20 6f 6e 20 74 68 65 20 62 61 73 69 73 20 6f 66   on the basis of
2d736 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 29 2e 0a   plrPosition()..
2d737 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6c  */.static int pl
2d738 72 43 6f 6d 70 61 72 65 28 50 4c 52 65 61 64 65  rCompare(PLReade
2d739 72 20 2a 70 4c 65 66 74 2c 20 50 4c 52 65 61 64  r *pLeft, PLRead
2d73a 65 72 20 2a 70 52 69 67 68 74 29 7b 0a 20 20 61  er *pRight){.  a
2d73b 73 73 65 72 74 28 21 70 6c 72 41 74 45 6e 64 28  ssert(!plrAtEnd(
2d73c 70 4c 65 66 74 29 20 7c 7c 20 21 70 6c 72 41 74  pLeft) || !plrAt
2d73d 45 6e 64 28 70 52 69 67 68 74 29 29 3b 0a 0a 20  End(pRight));.. 
2d73e 20 69 66 28 20 70 6c 72 41 74 45 6e 64 28 70 52   if( plrAtEnd(pR
2d73f 69 67 68 74 29 20 7c 7c 20 70 6c 72 41 74 45 6e  ight) || plrAtEn
2d740 64 28 70 4c 65 66 74 29 20 29 7b 0a 20 20 20 20  d(pLeft) ){.    
2d741 72 65 74 75 72 6e 20 28 70 6c 72 41 74 45 6e 64  return (plrAtEnd
2d742 28 70 52 69 67 68 74 29 20 3f 20 2d 31 20 3a 20  (pRight) ? -1 : 
2d743 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 6c  1);.  }.  if( pl
2d744 72 43 6f 6c 75 6d 6e 28 70 4c 65 66 74 29 21 3d  rColumn(pLeft)!=
2d745 70 6c 72 43 6f 6c 75 6d 6e 28 70 52 69 67 68 74  plrColumn(pRight
2d746 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
2d747 28 28 70 6c 72 43 6f 6c 75 6d 6e 28 70 4c 65 66  ((plrColumn(pLef
2d748 74 29 3c 70 6c 72 43 6f 6c 75 6d 6e 28 70 52 69  t)<plrColumn(pRi
2d749 67 68 74 29 29 20 3f 20 2d 31 20 3a 20 31 29 3b  ght)) ? -1 : 1);
2d74a 0a 20 20 7d 0a 20 20 69 66 28 20 70 6c 72 50 6f  .  }.  if( plrPo
2d74b 73 69 74 69 6f 6e 28 70 4c 65 66 74 29 21 3d 70  sition(pLeft)!=p
2d74c 6c 72 50 6f 73 69 74 69 6f 6e 28 70 52 69 67 68  lrPosition(pRigh
2d74d 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  t) ){.    return
2d74e 20 28 28 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70   ((plrPosition(p
2d74f 4c 65 66 74 29 3c 70 6c 72 50 6f 73 69 74 69 6f  Left)<plrPositio
2d750 6e 28 70 52 69 67 68 74 29 29 20 3f 20 2d 31 20  n(pRight)) ? -1 
2d751 3a 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  : 1);.  }.  retu
2d752 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 57 65 20 68  rn 0;.}../* We h
2d753 61 76 65 20 74 77 6f 20 64 6f 63 6c 69 73 74 73  ave two doclists
2d754 20 77 69 74 68 20 70 6f 73 69 74 69 6f 6e 73 3a   with positions:
2d755 20 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67    pLeft and pRig
2d756 68 74 2e 20 44 65 70 65 6e 64 69 6e 67 0a 2a 2a  ht. Depending.**
2d757 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66   on the value of
2d758 20 74 68 65 20 6e 4e 65 61 72 20 70 61 72 61 6d   the nNear param
2d759 65 74 65 72 2c 20 70 65 72 66 6f 72 6d 20 65 69  eter, perform ei
2d75a 74 68 65 72 20 61 20 70 68 72 61 73 65 0a 2a 2a  ther a phrase.**
2d75b 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 28 69   intersection (i
2d75c 66 20 6e 4e 65 61 72 3d 3d 30 29 20 6f 72 20 61  f nNear==0) or a
2d75d 20 4e 45 41 52 20 69 6e 74 65 72 73 65 63 74 69   NEAR intersecti
2d75e 6f 6e 20 28 69 66 20 6e 4e 65 61 72 3e 30 29 0a  on (if nNear>0).
2d75f 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 74 68 65  ** and write the
2d760 20 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 70 4f   results into pO
2d761 75 74 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 68 72 61  ut..**.** A phra
2d762 73 65 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20  se intersection 
2d763 6d 65 61 6e 73 20 74 68 61 74 20 74 77 6f 20 64  means that two d
2d764 6f 63 75 6d 65 6e 74 73 20 6f 6e 6c 79 20 6d 61  ocuments only ma
2d765 74 63 68 0a 2a 2a 20 69 66 20 70 4c 65 66 74 2e  tch.** if pLeft.
2d766 69 50 6f 73 2b 31 3d 3d 70 52 69 67 68 74 2e 69  iPos+1==pRight.i
2d767 50 6f 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 45 41  Pos..**.** A NEA
2d768 52 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 6d  R intersection m
2d769 65 61 6e 73 20 74 68 61 74 20 74 77 6f 20 64 6f  eans that two do
2d76a 63 75 6d 65 6e 74 73 20 6f 6e 6c 79 20 6d 61 74  cuments only mat
2d76b 63 68 20 69 66 20 0a 2a 2a 20 28 61 62 73 28 70  ch if .** (abs(p
2d76c 4c 65 66 74 2e 69 50 6f 73 2d 70 52 69 67 68 74  Left.iPos-pRight
2d76d 2e 69 50 6f 73 29 3c 6e 4e 65 61 72 29 2e 0a 2a  .iPos)<nNear)..*
2d76e 2a 0a 2a 2a 20 49 66 20 61 20 4e 45 41 52 20 69  *.** If a NEAR i
2d76f 6e 74 65 72 73 65 63 74 69 6f 6e 20 69 73 20 72  ntersection is r
2d770 65 71 75 65 73 74 65 64 2c 20 74 68 65 6e 20 74  equested, then t
2d771 68 65 20 6e 50 68 72 61 73 65 20 61 72 67 75 6d  he nPhrase argum
2d772 65 6e 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65  ent should.** be
2d773 20 70 61 73 73 65 64 20 74 68 65 20 6e 75 6d 62   passed the numb
2d774 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20  er of tokens in 
2d775 74 68 65 20 74 77 6f 20 6f 70 65 72 61 6e 64 73  the two operands
2d776 20 74 6f 20 74 68 65 20 4e 45 41 52 20 6f 70 65   to the NEAR ope
2d777 72 61 74 6f 72 0a 2a 2a 20 63 6f 6d 62 69 6e 65  rator.** combine
2d778 64 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a  d. For example:.
2d779 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 51 75 65 72  **.**       Quer
2d77a 79 20 73 79 6e 74 61 78 20 20 20 20 20 20 20 20  y syntax        
2d77b 20 20 20 20 20 20 20 6e 50 68 72 61 73 65 0a 2a         nPhrase.*
2d77c 2a 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  *      ---------
2d77d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2d77e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20  -----------.**  
2d77f 20 20 20 20 20 22 41 20 42 20 43 22 20 4e 45 41       "A B C" NEA
2d780 52 20 22 44 20 45 22 20 20 20 20 20 20 20 20 20  R "D E"         
2d781 35 0a 2a 2a 20 20 20 20 20 20 20 41 20 4e 45 41  5.**       A NEA
2d782 52 20 42 20 20 20 20 20 20 20 20 20 20 20 20 20  R B             
2d783 20 20 20 20 20 20 32 0a 2a 2a 0a 2a 2a 20 69 54        2.**.** iT
2d784 79 70 65 20 63 6f 6e 74 72 6f 6c 73 20 74 68 65  ype controls the
2d785 20 74 79 70 65 20 6f 66 20 64 61 74 61 20 77 72   type of data wr
2d786 69 74 74 65 6e 20 74 6f 20 70 4f 75 74 2e 20 20  itten to pOut.  
2d787 49 66 20 69 54 79 70 65 20 69 73 0a 2a 2a 20 44  If iType is.** D
2d788 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c 20 74 68 65  L_POSITIONS, the
2d789 20 70 6f 73 69 74 69 6f 6e 73 20 61 72 65 20 74   positions are t
2d78a 68 6f 73 65 20 66 72 6f 6d 20 70 52 69 67 68 74  hose from pRight
2d78b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2d78c 20 64 6f 63 4c 69 73 74 50 68 72 61 73 65 4d 65   docListPhraseMe
2d78d 72 67 65 28 0a 20 20 63 6f 6e 73 74 20 63 68 61  rge(.  const cha
2d78e 72 20 2a 70 4c 65 66 74 2c 20 69 6e 74 20 6e 4c  r *pLeft, int nL
2d78f 65 66 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  eft,.  const cha
2d790 72 20 2a 70 52 69 67 68 74 2c 20 69 6e 74 20 6e  r *pRight, int n
2d791 52 69 67 68 74 2c 0a 20 20 69 6e 74 20 6e 4e 65  Right,.  int nNe
2d792 61 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  ar,            /
2d793 2a 20 30 20 66 6f 72 20 61 20 70 68 72 61 73 65  * 0 for a phrase
2d794 20 6d 65 72 67 65 2c 20 6e 6f 6e 2d 7a 65 72 6f   merge, non-zero
2d795 20 66 6f 72 20 61 20 4e 45 41 52 20 6d 65 72 67   for a NEAR merg
2d796 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 68 72 61  e */.  int nPhra
2d797 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  se,          /* 
2d798 4e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73  Number of tokens
2d799 20 69 6e 20 6c 65 66 74 2b 72 69 67 68 74 20 6f   in left+right o
2d79a 70 65 72 61 6e 64 73 20 74 6f 20 4e 45 41 52 20  perands to NEAR 
2d79b 2a 2f 0a 20 20 44 6f 63 4c 69 73 74 54 79 70 65  */.  DocListType
2d79c 20 69 54 79 70 65 2c 20 20 20 20 2f 2a 20 54 79   iType,    /* Ty
2d79d 70 65 20 6f 66 20 64 6f 63 6c 69 73 74 20 74 6f  pe of doclist to
2d79e 20 77 72 69 74 65 20 74 6f 20 70 4f 75 74 20 2a   write to pOut *
2d79f 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 2a  /.  DataBuffer *
2d7a0 70 4f 75 74 20 20 20 20 20 20 2f 2a 20 57 72 69  pOut      /* Wri
2d7a1 74 65 20 74 68 65 20 63 6f 6d 62 69 6e 65 64 20  te the combined 
2d7a2 64 6f 63 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a  doclist here */.
2d7a3 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 6c 65  ){.  DLReader le
2d7a4 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 44 4c 57  ft, right;.  DLW
2d7a5 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a 0a 20  riter writer;.. 
2d7a6 20 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20 7c 7c   if( nLeft==0 ||
2d7a7 20 6e 52 69 67 68 74 3d 3d 30 20 29 20 72 65 74   nRight==0 ) ret
2d7a8 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  urn;..  assert( 
2d7a9 69 54 79 70 65 21 3d 44 4c 5f 50 4f 53 49 54 49  iType!=DL_POSITI
2d7aa 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29 3b 0a 0a  ONS_OFFSETS );..
2d7ab 20 20 64 6c 72 49 6e 69 74 28 26 6c 65 66 74 2c    dlrInit(&left,
2d7ac 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c 20 70   DL_POSITIONS, p
2d7ad 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20  Left, nLeft);.  
2d7ae 64 6c 72 49 6e 69 74 28 26 72 69 67 68 74 2c 20  dlrInit(&right, 
2d7af 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c 20 70 52  DL_POSITIONS, pR
2d7b0 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20  ight, nRight);. 
2d7b1 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72   dlwInit(&writer
2d7b2 2c 20 69 54 79 70 65 2c 20 70 4f 75 74 29 3b 0a  , iType, pOut);.
2d7b3 0a 20 20 77 68 69 6c 65 28 20 21 64 6c 72 41 74  .  while( !dlrAt
2d7b4 45 6e 64 28 26 6c 65 66 74 29 20 26 26 20 21 64  End(&left) && !d
2d7b5 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29 20  lrAtEnd(&right) 
2d7b6 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72 44 6f  ){.    if( dlrDo
2d7b7 63 69 64 28 26 6c 65 66 74 29 3c 64 6c 72 44 6f  cid(&left)<dlrDo
2d7b8 63 69 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20  cid(&right) ){. 
2d7b9 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65       dlrStep(&le
2d7ba 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ft);.    }else i
2d7bb 66 28 20 64 6c 72 44 6f 63 69 64 28 26 72 69 67  f( dlrDocid(&rig
2d7bc 68 74 29 3c 64 6c 72 44 6f 63 69 64 28 26 6c 65  ht)<dlrDocid(&le
2d7bd 66 74 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 72  ft) ){.      dlr
2d7be 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20  Step(&right);.  
2d7bf 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
2d7c0 66 28 20 6e 4e 65 61 72 3d 3d 30 20 29 7b 0a 20  f( nNear==0 ){. 
2d7c1 20 20 20 20 20 20 20 70 6f 73 4c 69 73 74 50 68         posListPh
2d7c2 72 61 73 65 4d 65 72 67 65 28 26 6c 65 66 74 2c  raseMerge(&left,
2d7c3 20 26 72 69 67 68 74 2c 20 30 2c 20 30 2c 20 26   &right, 0, 0, &
2d7c4 77 72 69 74 65 72 29 3b 0a 20 20 20 20 20 20 7d  writer);.      }
2d7c5 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a  else{.        /*
2d7c6 20 54 68 69 73 20 63 61 73 65 20 6f 63 63 75 72   This case occur
2d7c7 73 20 77 68 65 6e 20 74 77 6f 20 74 65 72 6d 73  s when two terms
2d7c8 20 28 73 69 6d 70 6c 65 20 74 65 72 6d 73 20 6f   (simple terms o
2d7c9 72 20 70 68 72 61 73 65 73 29 20 61 72 65 0a 20  r phrases) are. 
2d7ca 20 20 20 20 20 20 20 20 2a 20 63 6f 6e 6e 65 63          * connec
2d7cb 74 65 64 20 62 79 20 61 20 4e 45 41 52 20 6f 70  ted by a NEAR op
2d7cc 65 72 61 74 6f 72 2c 20 73 70 61 6e 20 28 6e 4e  erator, span (nN
2d7cd 65 61 72 2b 31 29 2e 20 69 2e 65 2e 0a 20 20 20  ear+1). i.e..   
2d7ce 20 20 20 20 20 20 2a 0a 20 20 20 20 20 20 20 20        *.        
2d7cf 20 2a 20 20 20 20 20 27 22 74 65 72 72 69 62 6c   *     '"terribl
2d7d0 65 20 63 6f 6d 70 61 6e 79 22 20 4e 45 41 52 20  e company" NEAR 
2d7d1 77 69 64 67 65 74 27 0a 20 20 20 20 20 20 20 20  widget'.        
2d7d2 20 2a 2f 0a 20 20 20 20 20 20 20 20 44 61 74 61   */.        Data
2d7d3 42 75 66 66 65 72 20 6f 6e 65 20 3d 20 7b 30 2c  Buffer one = {0,
2d7d4 20 30 2c 20 30 7d 3b 0a 20 20 20 20 20 20 20 20   0, 0};.        
2d7d5 44 61 74 61 42 75 66 66 65 72 20 74 77 6f 20 3d  DataBuffer two =
2d7d6 20 7b 30 2c 20 30 2c 20 30 7d 3b 0a 0a 20 20 20   {0, 0, 0};..   
2d7d7 20 20 20 20 20 44 4c 57 72 69 74 65 72 20 64 6c       DLWriter dl
2d7d8 77 72 69 74 65 72 32 3b 0a 20 20 20 20 20 20 20  writer2;.       
2d7d9 20 44 4c 52 65 61 64 65 72 20 64 72 31 20 3d 20   DLReader dr1 = 
2d7da 7b 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 7d 3b  {0, 0, 0, 0, 0};
2d7db 20 0a 20 20 20 20 20 20 20 20 44 4c 52 65 61 64   .        DLRead
2d7dc 65 72 20 64 72 32 20 3d 20 7b 30 2c 20 30 2c 20  er dr2 = {0, 0, 
2d7dd 30 2c 20 30 2c 20 30 7d 3b 0a 0a 20 20 20 20 20  0, 0, 0};..     
2d7de 20 20 20 64 6c 77 49 6e 69 74 28 26 64 6c 77 72     dlwInit(&dlwr
2d7df 69 74 65 72 32 2c 20 69 54 79 70 65 2c 20 26 6f  iter2, iType, &o
2d7e0 6e 65 29 3b 0a 20 20 20 20 20 20 20 20 70 6f 73  ne);.        pos
2d7e1 4c 69 73 74 50 68 72 61 73 65 4d 65 72 67 65 28  ListPhraseMerge(
2d7e2 26 72 69 67 68 74 2c 20 26 6c 65 66 74 2c 20 6e  &right, &left, n
2d7e3 4e 65 61 72 2d 33 2b 6e 50 68 72 61 73 65 2c 20  Near-3+nPhrase, 
2d7e4 31 2c 20 26 64 6c 77 72 69 74 65 72 32 29 3b 0a  1, &dlwriter2);.
2d7e5 20 20 20 20 20 20 20 20 64 6c 77 49 6e 69 74 28          dlwInit(
2d7e6 26 64 6c 77 72 69 74 65 72 32 2c 20 69 54 79 70  &dlwriter2, iTyp
2d7e7 65 2c 20 26 74 77 6f 29 3b 0a 20 20 20 20 20 20  e, &two);.      
2d7e8 20 20 70 6f 73 4c 69 73 74 50 68 72 61 73 65 4d    posListPhraseM
2d7e9 65 72 67 65 28 26 6c 65 66 74 2c 20 26 72 69 67  erge(&left, &rig
2d7ea 68 74 2c 20 6e 4e 65 61 72 2d 31 2c 20 30 2c 20  ht, nNear-1, 0, 
2d7eb 26 64 6c 77 72 69 74 65 72 32 29 3b 0a 0a 20 20  &dlwriter2);..  
2d7ec 20 20 20 20 20 20 69 66 28 20 6f 6e 65 2e 6e 44        if( one.nD
2d7ed 61 74 61 29 20 64 6c 72 49 6e 69 74 28 26 64 72  ata) dlrInit(&dr
2d7ee 31 2c 20 69 54 79 70 65 2c 20 6f 6e 65 2e 70 44  1, iType, one.pD
2d7ef 61 74 61 2c 20 6f 6e 65 2e 6e 44 61 74 61 29 3b  ata, one.nData);
2d7f0 0a 20 20 20 20 20 20 20 20 69 66 28 20 74 77 6f  .        if( two
2d7f1 2e 6e 44 61 74 61 29 20 64 6c 72 49 6e 69 74 28  .nData) dlrInit(
2d7f2 26 64 72 32 2c 20 69 54 79 70 65 2c 20 74 77 6f  &dr2, iType, two
2d7f3 2e 70 44 61 74 61 2c 20 74 77 6f 2e 6e 44 61 74  .pData, two.nDat
2d7f4 61 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28  a);..        if(
2d7f5 20 21 64 6c 72 41 74 45 6e 64 28 26 64 72 31 29   !dlrAtEnd(&dr1)
2d7f6 20 7c 7c 20 21 64 6c 72 41 74 45 6e 64 28 26 64   || !dlrAtEnd(&d
2d7f7 72 32 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  r2) ){.         
2d7f8 20 50 4c 52 65 61 64 65 72 20 70 72 31 20 3d 20   PLReader pr1 = 
2d7f9 7b 30 7d 3b 0a 20 20 20 20 20 20 20 20 20 20 50  {0};.          P
2d7fa 4c 52 65 61 64 65 72 20 70 72 32 20 3d 20 7b 30  LReader pr2 = {0
2d7fb 7d 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 50 4c  };..          PL
2d7fc 57 72 69 74 65 72 20 70 6c 77 72 69 74 65 72 3b  Writer plwriter;
2d7fd 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77 49 6e  .          plwIn
2d7fe 69 74 28 26 70 6c 77 72 69 74 65 72 2c 20 26 77  it(&plwriter, &w
2d7ff 72 69 74 65 72 2c 20 64 6c 72 44 6f 63 69 64 28  riter, dlrDocid(
2d800 64 6c 72 41 74 45 6e 64 28 26 64 72 31 29 3f 26  dlrAtEnd(&dr1)?&
2d801 64 72 32 3a 26 64 72 31 29 29 3b 0a 0a 20 20 20  dr2:&dr1));..   
2d802 20 20 20 20 20 20 20 69 66 28 20 6f 6e 65 2e 6e         if( one.n
2d803 44 61 74 61 20 29 20 70 6c 72 49 6e 69 74 28 26  Data ) plrInit(&
2d804 70 72 31 2c 20 26 64 72 31 29 3b 0a 20 20 20 20  pr1, &dr1);.    
2d805 20 20 20 20 20 20 69 66 28 20 74 77 6f 2e 6e 44        if( two.nD
2d806 61 74 61 20 29 20 70 6c 72 49 6e 69 74 28 26 70  ata ) plrInit(&p
2d807 72 32 2c 20 26 64 72 32 29 3b 0a 20 20 20 20 20  r2, &dr2);.     
2d808 20 20 20 20 20 77 68 69 6c 65 28 20 21 70 6c 72       while( !plr
2d809 41 74 45 6e 64 28 26 70 72 31 29 20 7c 7c 20 21  AtEnd(&pr1) || !
2d80a 70 6c 72 41 74 45 6e 64 28 26 70 72 32 29 20 29  plrAtEnd(&pr2) )
2d80b 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e  {.            in
2d80c 74 20 69 43 6f 6d 70 61 72 65 20 3d 20 70 6c 72  t iCompare = plr
2d80d 43 6f 6d 70 61 72 65 28 26 70 72 31 2c 20 26 70  Compare(&pr1, &p
2d80e 72 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  r2);.           
2d80f 20 73 77 69 74 63 68 28 20 69 43 6f 6d 70 61 72   switch( iCompar
2d810 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  e ){.           
2d811 20 20 20 63 61 73 65 20 2d 31 3a 0a 20 20 20 20     case -1:.    
2d812 20 20 20 20 20 20 20 20 20 20 20 20 70 6c 77 43              plwC
2d813 6f 70 79 28 26 70 6c 77 72 69 74 65 72 2c 20 26  opy(&plwriter, &
2d814 70 72 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20  pr1);.          
2d815 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 70        plrStep(&p
2d816 72 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  r1);.           
2d817 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2d818 20 20 20 20 20 20 20 20 20 20 63 61 73 65 20 31            case 1
2d819 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  :.              
2d81a 20 20 70 6c 77 43 6f 70 79 28 26 70 6c 77 72 69    plwCopy(&plwri
2d81b 74 65 72 2c 20 26 70 72 32 29 3b 0a 20 20 20 20  ter, &pr2);.    
2d81c 20 20 20 20 20 20 20 20 20 20 20 20 70 6c 72 53              plrS
2d81d 74 65 70 28 26 70 72 32 29 3b 0a 20 20 20 20 20  tep(&pr2);.     
2d81e 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
2d81f 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
2d820 63 61 73 65 20 30 3a 0a 20 20 20 20 20 20 20 20  case 0:.        
2d821 20 20 20 20 20 20 20 20 70 6c 77 43 6f 70 79 28          plwCopy(
2d822 26 70 6c 77 72 69 74 65 72 2c 20 26 70 72 31 29  &plwriter, &pr1)
2d823 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
2d824 20 20 70 6c 72 53 74 65 70 28 26 70 72 31 29 3b    plrStep(&pr1);
2d825 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2d826 20 70 6c 72 53 74 65 70 28 26 70 72 32 29 3b 0a   plrStep(&pr2);.
2d827 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d828 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
2d829 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d     }.          }
2d82a 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77 54 65  .          plwTe
2d82b 72 6d 69 6e 61 74 65 28 26 70 6c 77 72 69 74 65  rminate(&plwrite
2d82c 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  r);.        }.  
2d82d 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72        dataBuffer
2d82e 44 65 73 74 72 6f 79 28 26 6f 6e 65 29 3b 0a 20  Destroy(&one);. 
2d82f 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65         dataBuffe
2d830 72 44 65 73 74 72 6f 79 28 26 74 77 6f 29 3b 0a  rDestroy(&two);.
2d831 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 6c        }.      dl
2d832 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20  rStep(&left);.  
2d833 20 20 20 20 64 6c 72 53 74 65 70 28 26 72 69 67      dlrStep(&rig
2d834 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ht);.    }.  }..
2d835 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 6c 65    dlrDestroy(&le
2d836 66 74 29 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f  ft);.  dlrDestro
2d837 79 28 26 72 69 67 68 74 29 3b 0a 20 20 64 6c 77  y(&right);.  dlw
2d838 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29  Destroy(&writer)
2d839 3b 0a 7d 0a 0a 2f 2a 20 57 65 20 68 61 76 65 20  ;.}../* We have 
2d83a 74 77 6f 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f  two DL_DOCIDS do
2d83b 63 6c 69 73 74 73 3a 20 20 70 4c 65 66 74 20 61  clists:  pLeft a
2d83c 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2a 20 57 72  nd pRight..** Wr
2d83d 69 74 65 20 74 68 65 20 69 6e 74 65 72 73 65 63  ite the intersec
2d83e 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 74 77  tion of these tw
2d83f 6f 20 64 6f 63 6c 69 73 74 73 20 69 6e 74 6f 20  o doclists into 
2d840 70 4f 75 74 20 61 73 20 61 0a 2a 2a 20 44 4c 5f  pOut as a.** DL_
2d841 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74 2e 0a  DOCIDS doclist..
2d842 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  */.static void d
2d843 6f 63 4c 69 73 74 41 6e 64 4d 65 72 67 65 28 0a  ocListAndMerge(.
2d844 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4c    const char *pL
2d845 65 66 74 2c 20 69 6e 74 20 6e 4c 65 66 74 2c 0a  eft, int nLeft,.
2d846 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 52    const char *pR
2d847 69 67 68 74 2c 20 69 6e 74 20 6e 52 69 67 68 74  ight, int nRight
2d848 2c 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 2a  ,.  DataBuffer *
2d849 70 4f 75 74 20 20 20 20 20 20 2f 2a 20 57 72 69  pOut      /* Wri
2d84a 74 65 20 74 68 65 20 63 6f 6d 62 69 6e 65 64 20  te the combined 
2d84b 64 6f 63 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a  doclist here */.
2d84c 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 6c 65  ){.  DLReader le
2d84d 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 44 4c 57  ft, right;.  DLW
2d84e 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a 0a 20  riter writer;.. 
2d84f 20 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20 7c 7c   if( nLeft==0 ||
2d850 20 6e 52 69 67 68 74 3d 3d 30 20 29 20 72 65 74   nRight==0 ) ret
2d851 75 72 6e 3b 0a 0a 20 20 64 6c 72 49 6e 69 74 28  urn;..  dlrInit(
2d852 26 6c 65 66 74 2c 20 44 4c 5f 44 4f 43 49 44 53  &left, DL_DOCIDS
2d853 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b  , pLeft, nLeft);
2d854 0a 20 20 64 6c 72 49 6e 69 74 28 26 72 69 67 68  .  dlrInit(&righ
2d855 74 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 52  t, DL_DOCIDS, pR
2d856 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20  ight, nRight);. 
2d857 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72   dlwInit(&writer
2d858 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 4f 75  , DL_DOCIDS, pOu
2d859 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 21 64  t);..  while( !d
2d85a 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29 20 26  lrAtEnd(&left) &
2d85b 26 20 21 64 6c 72 41 74 45 6e 64 28 26 72 69 67  & !dlrAtEnd(&rig
2d85c 68 74 29 20 29 7b 0a 20 20 20 20 69 66 28 20 64  ht) ){.    if( d
2d85d 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29 3c 64  lrDocid(&left)<d
2d85e 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74 29 20  lrDocid(&right) 
2d85f 29 7b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70  ){.      dlrStep
2d860 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c  (&left);.    }el
2d861 73 65 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28  se if( dlrDocid(
2d862 26 72 69 67 68 74 29 3c 64 6c 72 44 6f 63 69 64  &right)<dlrDocid
2d863 28 26 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 20  (&left) ){.     
2d864 20 64 6c 72 53 74 65 70 28 26 72 69 67 68 74 29   dlrStep(&right)
2d865 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2d866 20 20 20 64 6c 77 41 64 64 28 26 77 72 69 74 65     dlwAdd(&write
2d867 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66  r, dlrDocid(&lef
2d868 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74  t));.      dlrSt
2d869 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 20  ep(&left);.     
2d86a 20 64 6c 72 53 74 65 70 28 26 72 69 67 68 74 29   dlrStep(&right)
2d86b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 64  ;.    }.  }..  d
2d86c 6c 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29  lrDestroy(&left)
2d86d 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26  ;.  dlrDestroy(&
2d86e 72 69 67 68 74 29 3b 0a 20 20 64 6c 77 44 65 73  right);.  dlwDes
2d86f 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 7d  troy(&writer);.}
2d870 0a 0a 2f 2a 20 57 65 20 68 61 76 65 20 74 77 6f  ../* We have two
2d871 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69   DL_DOCIDS docli
2d872 73 74 73 3a 20 20 70 4c 65 66 74 20 61 6e 64 20  sts:  pLeft and 
2d873 70 52 69 67 68 74 2e 0a 2a 2a 20 57 72 69 74 65  pRight..** Write
2d874 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20 74 68   the union of th
2d875 65 73 65 20 74 77 6f 20 64 6f 63 6c 69 73 74 73  ese two doclists
2d876 20 69 6e 74 6f 20 70 4f 75 74 20 61 73 20 61 0a   into pOut as a.
2d877 2a 2a 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63  ** DL_DOCIDS doc
2d878 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  list..*/.static 
2d879 76 6f 69 64 20 64 6f 63 4c 69 73 74 4f 72 4d 65  void docListOrMe
2d87a 72 67 65 28 0a 20 20 63 6f 6e 73 74 20 63 68 61  rge(.  const cha
2d87b 72 20 2a 70 4c 65 66 74 2c 20 69 6e 74 20 6e 4c  r *pLeft, int nL
2d87c 65 66 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  eft,.  const cha
2d87d 72 20 2a 70 52 69 67 68 74 2c 20 69 6e 74 20 6e  r *pRight, int n
2d87e 52 69 67 68 74 2c 0a 20 20 44 61 74 61 42 75 66  Right,.  DataBuf
2d87f 66 65 72 20 2a 70 4f 75 74 20 20 20 20 20 20 2f  fer *pOut      /
2d880 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6d 62  * Write the comb
2d881 69 6e 65 64 20 64 6f 63 6c 69 73 74 20 68 65 72  ined doclist her
2d882 65 20 2a 2f 0a 29 7b 0a 20 20 44 4c 52 65 61 64  e */.){.  DLRead
2d883 65 72 20 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a  er left, right;.
2d884 20 20 44 4c 57 72 69 74 65 72 20 77 72 69 74 65    DLWriter write
2d885 72 3b 0a 0a 20 20 69 66 28 20 6e 4c 65 66 74 3d  r;..  if( nLeft=
2d886 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 52  =0 ){.    if( nR
2d887 69 67 68 74 21 3d 30 20 29 20 64 61 74 61 42 75  ight!=0 ) dataBu
2d888 66 66 65 72 41 70 70 65 6e 64 28 70 4f 75 74 2c  fferAppend(pOut,
2d889 20 70 52 69 67 68 74 2c 20 6e 52 69 67 68 74 29   pRight, nRight)
2d88a 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
2d88b 7d 0a 20 20 69 66 28 20 6e 52 69 67 68 74 3d 3d  }.  if( nRight==
2d88c 30 20 29 7b 0a 20 20 20 20 64 61 74 61 42 75 66  0 ){.    dataBuf
2d88d 66 65 72 41 70 70 65 6e 64 28 70 4f 75 74 2c 20  ferAppend(pOut, 
2d88e 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20  pLeft, nLeft);. 
2d88f 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
2d890 20 20 64 6c 72 49 6e 69 74 28 26 6c 65 66 74 2c    dlrInit(&left,
2d891 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 4c 65 66   DL_DOCIDS, pLef
2d892 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20 64 6c 72  t, nLeft);.  dlr
2d893 49 6e 69 74 28 26 72 69 67 68 74 2c 20 44 4c 5f  Init(&right, DL_
2d894 44 4f 43 49 44 53 2c 20 70 52 69 67 68 74 2c 20  DOCIDS, pRight, 
2d895 6e 52 69 67 68 74 29 3b 0a 20 20 64 6c 77 49 6e  nRight);.  dlwIn
2d896 69 74 28 26 77 72 69 74 65 72 2c 20 44 4c 5f 44  it(&writer, DL_D
2d897 4f 43 49 44 53 2c 20 70 4f 75 74 29 3b 0a 0a 20  OCIDS, pOut);.. 
2d898 20 77 68 69 6c 65 28 20 21 64 6c 72 41 74 45 6e   while( !dlrAtEn
2d899 64 28 26 6c 65 66 74 29 20 7c 7c 20 21 64 6c 72  d(&left) || !dlr
2d89a 41 74 45 6e 64 28 26 72 69 67 68 74 29 20 29 7b  AtEnd(&right) ){
2d89b 0a 20 20 20 20 69 66 28 20 64 6c 72 41 74 45 6e  .    if( dlrAtEn
2d89c 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20  d(&right) ){.   
2d89d 20 20 20 64 6c 77 41 64 64 28 26 77 72 69 74 65     dlwAdd(&write
2d89e 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66  r, dlrDocid(&lef
2d89f 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74  t));.      dlrSt
2d8a0 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d  ep(&left);.    }
2d8a1 65 6c 73 65 20 69 66 28 20 64 6c 72 41 74 45 6e  else if( dlrAtEn
2d8a2 64 28 26 6c 65 66 74 29 20 29 7b 0a 20 20 20 20  d(&left) ){.    
2d8a3 20 20 64 6c 77 41 64 64 28 26 77 72 69 74 65 72    dlwAdd(&writer
2d8a4 2c 20 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68  , dlrDocid(&righ
2d8a5 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74  t));.      dlrSt
2d8a6 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20  ep(&right);.    
2d8a7 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 44 6f 63  }else if( dlrDoc
2d8a8 69 64 28 26 6c 65 66 74 29 3c 64 6c 72 44 6f 63  id(&left)<dlrDoc
2d8a9 69 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20  id(&right) ){.  
2d8aa 20 20 20 20 64 6c 77 41 64 64 28 26 77 72 69 74      dlwAdd(&writ
2d8ab 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 6c 65  er, dlrDocid(&le
2d8ac 66 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72 53  ft));.      dlrS
2d8ad 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20  tep(&left);.    
2d8ae 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 44 6f 63  }else if( dlrDoc
2d8af 69 64 28 26 72 69 67 68 74 29 3c 64 6c 72 44 6f  id(&right)<dlrDo
2d8b0 63 69 64 28 26 6c 65 66 74 29 20 29 7b 0a 20 20  cid(&left) ){.  
2d8b1 20 20 20 20 64 6c 77 41 64 64 28 26 77 72 69 74      dlwAdd(&writ
2d8b2 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 72 69  er, dlrDocid(&ri
2d8b3 67 68 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72  ght));.      dlr
2d8b4 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20  Step(&right);.  
2d8b5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64    }else{.      d
2d8b6 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c 20 64  lwAdd(&writer, d
2d8b7 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29 29 3b  lrDocid(&left));
2d8b8 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26  .      dlrStep(&
2d8b9 6c 65 66 74 29 3b 0a 20 20 20 20 20 20 64 6c 72  left);.      dlr
2d8ba 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20  Step(&right);.  
2d8bb 20 20 7d 0a 20 20 7d 0a 0a 20 20 64 6c 72 44 65    }.  }..  dlrDe
2d8bc 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20  stroy(&left);.  
2d8bd 64 6c 72 44 65 73 74 72 6f 79 28 26 72 69 67 68  dlrDestroy(&righ
2d8be 74 29 3b 0a 20 20 64 6c 77 44 65 73 74 72 6f 79  t);.  dlwDestroy
2d8bf 28 26 77 72 69 74 65 72 29 3b 0a 7d 0a 0a 2f 2a  (&writer);.}../*
2d8c0 20 57 65 20 68 61 76 65 20 74 77 6f 20 44 4c 5f   We have two DL_
2d8c1 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74 73 3a  DOCIDS doclists:
2d8c2 20 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67    pLeft and pRig
2d8c3 68 74 2e 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74  ht..** Write int
2d8c4 6f 20 70 4f 75 74 20 61 73 20 44 4c 5f 44 4f 43  o pOut as DL_DOC
2d8c5 49 44 53 20 64 6f 63 6c 69 73 74 20 63 6f 6e 74  IDS doclist cont
2d8c6 61 69 6e 69 6e 67 20 61 6c 6c 20 64 6f 63 75 6d  aining all docum
2d8c7 65 6e 74 73 20 74 68 61 74 0a 2a 2a 20 6f 63 63  ents that.** occ
2d8c8 75 72 20 69 6e 20 70 4c 65 66 74 20 62 75 74 20  ur in pLeft but 
2d8c9 6e 6f 74 20 69 6e 20 70 52 69 67 68 74 2e 0a 2a  not in pRight..*
2d8ca 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f  /.static void do
2d8cb 63 4c 69 73 74 45 78 63 65 70 74 4d 65 72 67 65  cListExceptMerge
2d8cc 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  (.  const char *
2d8cd 70 4c 65 66 74 2c 20 69 6e 74 20 6e 4c 65 66 74  pLeft, int nLeft
2d8ce 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
2d8cf 70 52 69 67 68 74 2c 20 69 6e 74 20 6e 52 69 67  pRight, int nRig
2d8d0 68 74 2c 0a 20 20 44 61 74 61 42 75 66 66 65 72  ht,.  DataBuffer
2d8d1 20 2a 70 4f 75 74 20 20 20 20 20 20 2f 2a 20 57   *pOut      /* W
2d8d2 72 69 74 65 20 74 68 65 20 63 6f 6d 62 69 6e 65  rite the combine
2d8d3 64 20 64 6f 63 6c 69 73 74 20 68 65 72 65 20 2a  d doclist here *
2d8d4 2f 0a 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20  /.){.  DLReader 
2d8d5 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 44  left, right;.  D
2d8d6 4c 57 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a  LWriter writer;.
2d8d7 0a 20 20 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20  .  if( nLeft==0 
2d8d8 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
2d8d9 6e 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20  nRight==0 ){.   
2d8da 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
2d8db 64 28 70 4f 75 74 2c 20 70 4c 65 66 74 2c 20 6e  d(pOut, pLeft, n
2d8dc 4c 65 66 74 29 3b 0a 20 20 20 20 72 65 74 75 72  Left);.    retur
2d8dd 6e 3b 0a 20 20 7d 0a 0a 20 20 64 6c 72 49 6e 69  n;.  }..  dlrIni
2d8de 74 28 26 6c 65 66 74 2c 20 44 4c 5f 44 4f 43 49  t(&left, DL_DOCI
2d8df 44 53 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74  DS, pLeft, nLeft
2d8e0 29 3b 0a 20 20 64 6c 72 49 6e 69 74 28 26 72 69  );.  dlrInit(&ri
2d8e1 67 68 74 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20  ght, DL_DOCIDS, 
2d8e2 70 52 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b  pRight, nRight);
2d8e3 0a 20 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74  .  dlwInit(&writ
2d8e4 65 72 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70  er, DL_DOCIDS, p
2d8e5 4f 75 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20  Out);..  while( 
2d8e6 21 64 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29  !dlrAtEnd(&left)
2d8e7 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 21   ){.    while( !
2d8e8 64 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29  dlrAtEnd(&right)
2d8e9 20 26 26 20 64 6c 72 44 6f 63 69 64 28 26 72 69   && dlrDocid(&ri
2d8ea 67 68 74 29 3c 64 6c 72 44 6f 63 69 64 28 26 6c  ght)<dlrDocid(&l
2d8eb 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20 64 6c  eft) ){.      dl
2d8ec 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20  rStep(&right);. 
2d8ed 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64 6c 72     }.    if( dlr
2d8ee 41 74 45 6e 64 28 26 72 69 67 68 74 29 20 7c 7c  AtEnd(&right) ||
2d8ef 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29   dlrDocid(&left)
2d8f0 3c 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74  <dlrDocid(&right
2d8f1 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 77 41 64  ) ){.      dlwAd
2d8f2 64 28 26 77 72 69 74 65 72 2c 20 64 6c 72 44 6f  d(&writer, dlrDo
2d8f3 63 69 64 28 26 6c 65 66 74 29 29 3b 0a 20 20 20  cid(&left));.   
2d8f4 20 7d 0a 20 20 20 20 64 6c 72 53 74 65 70 28 26   }.    dlrStep(&
2d8f5 6c 65 66 74 29 3b 0a 20 20 7d 0a 0a 20 20 64 6c  left);.  }..  dl
2d8f6 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29 3b  rDestroy(&left);
2d8f7 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72  .  dlrDestroy(&r
2d8f8 69 67 68 74 29 3b 0a 20 20 64 6c 77 44 65 73 74  ight);.  dlwDest
2d8f9 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 7d 0a  roy(&writer);.}.
2d8fa 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 73 74  .static char *st
2d8fb 72 69 6e 67 5f 64 75 70 5f 6e 28 63 6f 6e 73 74  ring_dup_n(const
2d8fc 20 63 68 61 72 20 2a 73 2c 20 69 6e 74 20 6e 29   char *s, int n)
2d8fd 7b 0a 20 20 63 68 61 72 20 2a 73 74 72 20 3d 20  {.  char *str = 
2d8fe 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
2d8ff 20 2b 20 31 29 3b 0a 20 20 6d 65 6d 63 70 79 28   + 1);.  memcpy(
2d900 73 74 72 2c 20 73 2c 20 6e 29 3b 0a 20 20 73 74  str, s, n);.  st
2d901 72 5b 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 72  r[n] = '\0';.  r
2d902 65 74 75 72 6e 20 73 74 72 3b 0a 7d 0a 0a 2f 2a  eturn str;.}../*
2d903 20 44 75 70 6c 69 63 61 74 65 20 61 20 73 74 72   Duplicate a str
2d904 69 6e 67 3b 20 74 68 65 20 63 61 6c 6c 65 72 20  ing; the caller 
2d905 6d 75 73 74 20 66 72 65 65 28 29 20 74 68 65 20  must free() the 
2d906 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 2e  returned string.
2d907 0a 20 2a 20 28 57 65 20 64 6f 6e 27 74 20 75 73  . * (We don't us
2d908 65 20 73 74 72 64 75 70 28 29 20 73 69 6e 63 65  e strdup() since
2d909 20 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20   it is not part 
2d90a 6f 66 20 74 68 65 20 73 74 61 6e 64 61 72 64 20  of the standard 
2d90b 43 20 6c 69 62 72 61 72 79 20 61 6e 64 0a 20 2a  C library and. *
2d90c 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 76 61 69   may not be avai
2d90d 6c 61 62 6c 65 20 65 76 65 72 79 77 68 65 72 65  lable everywhere
2d90e 2e 29 20 2a 2f 0a 73 74 61 74 69 63 20 63 68 61  .) */.static cha
2d90f 72 20 2a 73 74 72 69 6e 67 5f 64 75 70 28 63 6f  r *string_dup(co
2d910 6e 73 74 20 63 68 61 72 20 2a 73 29 7b 0a 20 20  nst char *s){.  
2d911 72 65 74 75 72 6e 20 73 74 72 69 6e 67 5f 64 75  return string_du
2d912 70 5f 6e 28 73 2c 20 73 74 72 6c 65 6e 28 73 29  p_n(s, strlen(s)
2d913 29 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 6d 61 74 20  );.}../* Format 
2d914 61 20 73 74 72 69 6e 67 2c 20 72 65 70 6c 61 63  a string, replac
2d915 69 6e 67 20 65 61 63 68 20 6f 63 63 75 72 72 65  ing each occurre
2d916 6e 63 65 20 6f 66 20 74 68 65 20 25 20 63 68 61  nce of the % cha
2d917 72 61 63 74 65 72 20 77 69 74 68 0a 20 2a 20 7a  racter with. * z
2d918 44 62 2e 7a 4e 61 6d 65 2e 20 20 54 68 69 73 20  Db.zName.  This 
2d919 6d 61 79 20 62 65 20 6d 6f 72 65 20 63 6f 6e 76  may be more conv
2d91a 65 6e 69 65 6e 74 20 74 68 61 6e 20 73 71 6c 69  enient than sqli
2d91b 74 65 5f 6d 70 72 69 6e 74 66 28 29 0a 20 2a 20  te_mprintf(). * 
2d91c 77 68 65 6e 20 6f 6e 65 20 73 74 72 69 6e 67 20  when one string 
2d91d 69 73 20 75 73 65 64 20 72 65 70 65 61 74 65 64  is used repeated
2d91e 6c 79 20 69 6e 20 61 20 66 6f 72 6d 61 74 20 73  ly in a format s
2d91f 74 72 69 6e 67 2e 0a 20 2a 20 54 68 65 20 63 61  tring.. * The ca
2d920 6c 6c 65 72 20 6d 75 73 74 20 66 72 65 65 28 29  ller must free()
2d921 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74   the returned st
2d922 72 69 6e 67 2e 20 2a 2f 0a 73 74 61 74 69 63 20  ring. */.static 
2d923 63 68 61 72 20 2a 73 74 72 69 6e 67 5f 66 6f 72  char *string_for
2d924 6d 61 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  mat(const char *
2d925 7a 46 6f 72 6d 61 74 2c 0a 20 20 20 20 20 20 20  zFormat,.       
2d926 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d927 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2d928 7a 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  zDb, const char 
2d929 2a 7a 4e 61 6d 65 29 7b 0a 20 20 63 6f 6e 73 74  *zName){.  const
2d92a 20 63 68 61 72 20 2a 70 3b 0a 20 20 73 69 7a 65   char *p;.  size
2d92b 5f 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 73 69  _t len = 0;.  si
2d92c 7a 65 5f 74 20 6e 44 62 20 3d 20 73 74 72 6c 65  ze_t nDb = strle
2d92d 6e 28 7a 44 62 29 3b 0a 20 20 73 69 7a 65 5f 74  n(zDb);.  size_t
2d92e 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28   nName = strlen(
2d92f 7a 4e 61 6d 65 29 3b 0a 20 20 73 69 7a 65 5f 74  zName);.  size_t
2d930 20 6e 46 75 6c 6c 54 61 62 6c 65 4e 61 6d 65 20   nFullTableName 
2d931 3d 20 6e 44 62 2b 31 2b 6e 4e 61 6d 65 3b 0a 20  = nDb+1+nName;. 
2d932 20 63 68 61 72 20 2a 72 65 73 75 6c 74 3b 0a 20   char *result;. 
2d933 20 63 68 61 72 20 2a 72 3b 0a 0a 20 20 2f 2a 20   char *r;..  /* 
2d934 66 69 72 73 74 20 63 6f 6d 70 75 74 65 20 6c 65  first compute le
2d935 6e 67 74 68 20 6e 65 65 64 65 64 20 2a 2f 0a 20  ngth needed */. 
2d936 20 66 6f 72 28 70 20 3d 20 7a 46 6f 72 6d 61 74   for(p = zFormat
2d937 20 3b 20 2a 70 20 3b 20 2b 2b 70 29 7b 0a 20 20   ; *p ; ++p){.  
2d938 20 20 6c 65 6e 20 2b 3d 20 28 2a 70 3d 3d 27 25    len += (*p=='%
2d939 27 20 3f 20 6e 46 75 6c 6c 54 61 62 6c 65 4e 61  ' ? nFullTableNa
2d93a 6d 65 20 3a 20 31 29 3b 0a 20 20 7d 0a 20 20 6c  me : 1);.  }.  l
2d93b 65 6e 20 2b 3d 20 31 3b 20 20 2f 2a 20 66 6f 72  en += 1;  /* for
2d93c 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 74 6f 72   null terminator
2d93d 20 2a 2f 0a 0a 20 20 72 20 3d 20 72 65 73 75 6c   */..  r = resul
2d93e 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  t = sqlite3_mall
2d93f 6f 63 28 6c 65 6e 29 3b 0a 20 20 66 6f 72 28 70  oc(len);.  for(p
2d940 20 3d 20 7a 46 6f 72 6d 61 74 3b 20 2a 70 3b 20   = zFormat; *p; 
2d941 2b 2b 70 29 7b 0a 20 20 20 20 69 66 28 20 2a 70  ++p){.    if( *p
2d942 3d 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20 6d  =='%' ){.      m
2d943 65 6d 63 70 79 28 72 2c 20 7a 44 62 2c 20 6e 44  emcpy(r, zDb, nD
2d944 62 29 3b 0a 20 20 20 20 20 20 72 20 2b 3d 20 6e  b);.      r += n
2d945 44 62 3b 0a 20 20 20 20 20 20 2a 72 2b 2b 20 3d  Db;.      *r++ =
2d946 20 27 2e 27 3b 0a 20 20 20 20 20 20 6d 65 6d 63   '.';.      memc
2d947 70 79 28 72 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  py(r, zName, nNa
2d948 6d 65 29 3b 0a 20 20 20 20 20 20 72 20 2b 3d 20  me);.      r += 
2d949 6e 4e 61 6d 65 3b 0a 20 20 20 20 7d 20 65 6c 73  nName;.    } els
2d94a 65 20 7b 0a 20 20 20 20 20 20 2a 72 2b 2b 20 3d  e {.      *r++ =
2d94b 20 2a 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20   *p;.    }.  }. 
2d94c 20 2a 72 2b 2b 20 3d 20 27 5c 30 27 3b 0a 20 20   *r++ = '\0';.  
2d94d 61 73 73 65 72 74 28 20 72 20 3d 3d 20 72 65 73  assert( r == res
2d94e 75 6c 74 20 2b 20 6c 65 6e 20 29 3b 0a 20 20 72  ult + len );.  r
2d94f 65 74 75 72 6e 20 72 65 73 75 6c 74 3b 0a 7d 0a  eturn result;.}.
2d950 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 5f  .static int sql_
2d951 65 78 65 63 28 73 71 6c 69 74 65 33 20 2a 64 62  exec(sqlite3 *db
2d952 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44  , const char *zD
2d953 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
2d954 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20  Name,.          
2d955 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
2d956 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 29 7b 0a  char *zFormat){.
2d957 20 20 63 68 61 72 20 2a 7a 43 6f 6d 6d 61 6e 64    char *zCommand
2d958 20 3d 20 73 74 72 69 6e 67 5f 66 6f 72 6d 61 74   = string_format
2d959 28 7a 46 6f 72 6d 61 74 2c 20 7a 44 62 2c 20 7a  (zFormat, zDb, z
2d95a 4e 61 6d 65 29 3b 0a 20 20 69 6e 74 20 72 63 3b  Name);.  int rc;
2d95b 0a 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54  .  FTSTRACE(("FT
2d95c 53 33 20 73 71 6c 3a 20 25 73 5c 6e 22 2c 20 7a  S3 sql: %s\n", z
2d95d 43 6f 6d 6d 61 6e 64 29 29 3b 0a 20 20 72 63 20  Command));.  rc 
2d95e 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64  = sqlite3_exec(d
2d95f 62 2c 20 7a 43 6f 6d 6d 61 6e 64 2c 20 4e 55 4c  b, zCommand, NUL
2d960 4c 2c 20 30 2c 20 4e 55 4c 4c 29 3b 0a 20 20 73  L, 0, NULL);.  s
2d961 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6d  qlite3_free(zCom
2d962 6d 61 6e 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  mand);.  return 
2d963 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  rc;.}..static in
2d964 74 20 73 71 6c 5f 70 72 65 70 61 72 65 28 73 71  t sql_prepare(sq
2d965 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
2d966 20 63 68 61 72 20 2a 7a 44 62 2c 20 63 6f 6e 73   char *zDb, cons
2d967 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20  t char *zName,. 
2d968 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d969 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74        sqlite3_st
2d96a 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 63 6f 6e  mt **ppStmt, con
2d96b 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74  st char *zFormat
2d96c 29 7b 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6d 6d  ){.  char *zComm
2d96d 61 6e 64 20 3d 20 73 74 72 69 6e 67 5f 66 6f 72  and = string_for
2d96e 6d 61 74 28 7a 46 6f 72 6d 61 74 2c 20 7a 44 62  mat(zFormat, zDb
2d96f 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 6e 74 20  , zName);.  int 
2d970 72 63 3b 0a 20 20 46 54 53 54 52 41 43 45 28 28  rc;.  FTSTRACE((
2d971 22 46 54 53 33 20 70 72 65 70 61 72 65 3a 20 25  "FTS3 prepare: %
2d972 73 5c 6e 22 2c 20 7a 43 6f 6d 6d 61 6e 64 29 29  s\n", zCommand))
2d973 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
2d974 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c 20  _prepare_v2(db, 
2d975 7a 43 6f 6d 6d 61 6e 64 2c 20 2d 31 2c 20 70 70  zCommand, -1, pp
2d976 53 74 6d 74 2c 20 4e 55 4c 4c 29 3b 0a 20 20 73  Stmt, NULL);.  s
2d977 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6d  qlite3_free(zCom
2d978 6d 61 6e 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  mand);.  return 
2d979 72 63 3b 0a 7d 0a 0a 2f 2a 20 65 6e 64 20 75 74  rc;.}../* end ut
2d97a 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e 73 20  ility functions 
2d97b 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72  */../* Forward r
2d97c 65 66 65 72 65 6e 63 65 20 2a 2f 0a 74 79 70 65  eference */.type
2d97d 64 65 66 20 73 74 72 75 63 74 20 66 75 6c 6c 74  def struct fullt
2d97e 65 78 74 5f 76 74 61 62 20 66 75 6c 6c 74 65 78  ext_vtab fulltex
2d97f 74 5f 76 74 61 62 3b 0a 0a 2f 2a 20 41 20 73 69  t_vtab;../* A si
2d980 6e 67 6c 65 20 74 65 72 6d 20 69 6e 20 61 20 71  ngle term in a q
2d981 75 65 72 79 20 69 73 20 72 65 70 72 65 73 65 6e  uery is represen
2d982 74 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e  ted by an instan
2d983 63 65 73 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f  ces of.** the fo
2d984 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
2d985 65 2e 20 45 61 63 68 20 77 6f 72 64 20 77 68 69  e. Each word whi
2d986 63 68 20 6d 61 79 20 6d 61 74 63 68 20 61 67 61  ch may match aga
2d987 69 6e 73 74 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74  inst.** document
2d988 20 63 6f 6e 74 65 6e 74 20 69 73 20 61 20 74 65   content is a te
2d989 72 6d 2e 20 4f 70 65 72 61 74 6f 72 73 2c 20 6c  rm. Operators, l
2d98a 69 6b 65 20 4e 45 41 52 20 6f 72 20 4f 52 2c 20  ike NEAR or OR, 
2d98b 61 72 65 0a 2a 2a 20 6e 6f 74 20 74 65 72 6d 73  are.** not terms
2d98c 2e 20 51 75 65 72 79 20 74 65 72 6d 73 20 61 72  . Query terms ar
2d98d 65 20 6f 72 67 61 6e 69 7a 65 64 20 61 73 20 61  e organized as a
2d98e 20 66 6c 61 74 20 6c 69 73 74 20 73 74 6f 72 65   flat list store
2d98f 64 0a 2a 2a 20 69 6e 20 74 68 65 20 51 75 65 72  d.** in the Quer
2d990 79 2e 70 54 65 72 6d 73 20 61 72 72 61 79 2e 0a  y.pTerms array..
2d991 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 51 75 65  **.** If the Que
2d992 72 79 54 65 72 6d 2e 6e 50 68 72 61 73 65 20 76  ryTerm.nPhrase v
2d993 61 72 69 61 62 6c 65 20 69 73 20 6e 6f 6e 2d 7a  ariable is non-z
2d994 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 51 75  ero, then the Qu
2d995 65 72 79 54 65 72 6d 0a 2a 2a 20 69 73 20 74 68  eryTerm.** is th
2d996 65 20 66 69 72 73 74 20 69 6e 20 61 20 63 6f 6e  e first in a con
2d997 74 69 67 75 6f 75 73 20 73 74 72 69 6e 67 20 6f  tiguous string o
2d998 66 20 74 65 72 6d 73 20 74 68 61 74 20 61 72 65  f terms that are
2d999 20 65 69 74 68 65 72 20 70 61 72 74 0a 2a 2a 20   either part.** 
2d99a 6f 66 20 74 68 65 20 73 61 6d 65 20 70 68 72 61  of the same phra
2d99b 73 65 2c 20 6f 72 20 63 6f 6e 6e 65 63 74 65 64  se, or connected
2d99c 20 62 79 20 74 68 65 20 4e 45 41 52 20 6f 70 65   by the NEAR ope
2d99d 72 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  rator..**.** If 
2d99e 74 68 65 20 51 75 65 72 79 54 65 72 6d 2e 6e 4e  the QueryTerm.nN
2d99f 65 61 72 20 76 61 72 69 61 62 6c 65 20 69 73 20  ear variable is 
2d9a0 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74  non-zero, then t
2d9a1 68 65 20 74 6f 6b 65 6e 20 69 73 20 66 6f 6c 6c  he token is foll
2d9a2 6f 77 65 64 20 0a 2a 2a 20 62 79 20 61 20 4e 45  owed .** by a NE
2d9a3 41 52 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68  AR operator with
2d9a4 20 73 70 61 6e 20 73 65 74 20 74 6f 20 28 6e 4e   span set to (nN
2d9a5 65 61 72 2d 31 29 2e 20 46 6f 72 20 65 78 61 6d  ear-1). For exam
2d9a6 70 6c 65 2c 20 74 68 65 20 0a 2a 2a 20 66 6f 6c  ple, the .** fol
2d9a7 6c 6f 77 69 6e 67 20 71 75 65 72 79 3a 0a 2a 2a  lowing query:.**
2d9a8 0a 2a 2a 20 54 68 65 20 51 75 65 72 79 54 65 72  .** The QueryTer
2d9a9 6d 2e 69 50 68 72 61 73 65 20 76 61 72 69 61 62  m.iPhrase variab
2d9aa 6c 65 20 73 74 6f 72 65 73 20 74 68 65 20 69 6e  le stores the in
2d9ab 64 65 78 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  dex of the token
2d9ac 20 77 69 74 68 69 6e 0a 2a 2a 20 69 74 73 20 70   within.** its p
2d9ad 68 72 61 73 65 2c 20 69 6e 64 65 78 65 64 20 73  hrase, indexed s
2d9ae 74 61 72 74 69 6e 67 20 61 74 20 31 2c 20 6f 72  tarting at 1, or
2d9af 20 31 20 69 66 20 74 68 65 20 74 6f 6b 65 6e 20   1 if the token 
2d9b0 69 73 20 6e 6f 74 20 70 61 72 74 20 0a 2a 2a 20  is not part .** 
2d9b1 6f 66 20 61 6e 79 20 70 68 72 61 73 65 2e 0a 2a  of any phrase..*
2d9b2 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65  *.** For example
2d9b3 2c 20 74 68 65 20 64 61 74 61 20 73 74 72 75 63  , the data struc
2d9b4 74 75 72 65 20 75 73 65 64 20 74 6f 20 72 65 70  ture used to rep
2d9b5 72 65 73 65 6e 74 20 74 68 65 20 66 6f 6c 6c 6f  resent the follo
2d9b6 77 69 6e 67 20 71 75 65 72 79 3a 0a 2a 2a 0a 2a  wing query:.**.*
2d9b7 2a 20 20 20 20 20 2e 2e 2e 20 4d 41 54 43 48 20  *     ... MATCH 
2d9b8 27 73 71 6c 69 74 65 20 4e 45 41 52 2f 35 20 67  'sqlite NEAR/5 g
2d9b9 6f 6f 67 6c 65 20 4e 45 41 52 2f 32 20 22 73 65  oogle NEAR/2 "se
2d9ba 61 72 63 68 20 65 6e 67 69 6e 65 22 27 0a 2a 2a  arch engine"'.**
2d9bb 0a 2a 2a 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  .** is:.**.**   
2d9bc 20 20 7b 6e 50 68 72 61 73 65 3d 34 2c 20 69 50    {nPhrase=4, iP
2d9bd 68 72 61 73 65 3d 31 2c 20 6e 4e 65 61 72 3d 36  hrase=1, nNear=6
2d9be 2c 20 70 54 65 72 6d 3d 22 73 71 6c 69 74 65 22  , pTerm="sqlite"
2d9bf 7d 2c 0a 2a 2a 20 20 20 20 20 7b 6e 50 68 72 61  },.**     {nPhra
2d9c0 73 65 3d 30 2c 20 69 50 68 72 61 73 65 3d 31 2c  se=0, iPhrase=1,
2d9c1 20 6e 4e 65 61 72 3d 33 2c 20 70 54 65 72 6d 3d   nNear=3, pTerm=
2d9c2 22 67 6f 6f 67 6c 65 22 7d 2c 0a 2a 2a 20 20 20  "google"},.**   
2d9c3 20 20 7b 6e 50 68 72 61 73 65 3d 30 2c 20 69 50    {nPhrase=0, iP
2d9c4 68 72 61 73 65 3d 31 2c 20 6e 4e 65 61 72 3d 30  hrase=1, nNear=0
2d9c5 2c 20 70 54 65 72 6d 3d 22 73 65 61 72 63 68 22  , pTerm="search"
2d9c6 7d 2c 0a 2a 2a 20 20 20 20 20 7b 6e 50 68 72 61  },.**     {nPhra
2d9c7 73 65 3d 30 2c 20 69 50 68 72 61 73 65 3d 32 2c  se=0, iPhrase=2,
2d9c8 20 6e 4e 65 61 72 3d 30 2c 20 70 54 65 72 6d 3d   nNear=0, pTerm=
2d9c9 22 65 6e 67 69 6e 65 22 7d 2c 0a 2a 2a 0a 2a 2a  "engine"},.**.**
2d9ca 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 46   compiling the F
2d9cb 54 53 33 20 73 79 6e 74 61 78 20 74 6f 20 51 75  TS3 syntax to Qu
2d9cc 65 72 79 20 73 74 72 75 63 74 75 72 65 73 20 69  ery structures i
2d9cd 73 20 64 6f 6e 65 20 62 79 20 74 68 65 20 70 61  s done by the pa
2d9ce 72 73 65 51 75 65 72 79 28 29 0a 2a 2a 20 66 75  rseQuery().** fu
2d9cf 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64  nction..*/.typed
2d9d0 65 66 20 73 74 72 75 63 74 20 51 75 65 72 79 54  ef struct QueryT
2d9d1 65 72 6d 20 7b 0a 20 20 73 68 6f 72 74 20 69 6e  erm {.  short in
2d9d2 74 20 6e 50 68 72 61 73 65 3b 20 2f 2a 20 48 6f  t nPhrase; /* Ho
2d9d3 77 20 6d 61 6e 79 20 66 6f 6c 6c 6f 77 69 6e 67  w many following
2d9d4 20 74 65 72 6d 73 20 61 72 65 20 70 61 72 74 20   terms are part 
2d9d5 6f 66 20 74 68 65 20 73 61 6d 65 20 70 68 72 61  of the same phra
2d9d6 73 65 20 2a 2f 0a 20 20 73 68 6f 72 74 20 69 6e  se */.  short in
2d9d7 74 20 69 50 68 72 61 73 65 3b 20 2f 2a 20 54 68  t iPhrase; /* Th
2d9d8 69 73 20 69 73 20 74 68 65 20 69 2d 74 68 20 74  is is the i-th t
2d9d9 65 72 6d 20 6f 66 20 61 20 70 68 72 61 73 65 2e  erm of a phrase.
2d9da 20 2a 2f 0a 20 20 73 68 6f 72 74 20 69 6e 74 20   */.  short int 
2d9db 69 43 6f 6c 75 6d 6e 3b 20 2f 2a 20 43 6f 6c 75  iColumn; /* Colu
2d9dc 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  mn of the index 
2d9dd 74 68 61 74 20 6d 75 73 74 20 6d 61 74 63 68 20  that must match 
2d9de 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a 20 20 73  this term */.  s
2d9df 68 6f 72 74 20 69 6e 74 20 6e 4e 65 61 72 3b 20  hort int nNear; 
2d9e0 20 20 2f 2a 20 74 65 72 6d 20 66 6f 6c 6c 6f 77    /* term follow
2d9e1 65 64 20 62 79 20 61 20 4e 45 41 52 20 6f 70 65  ed by a NEAR ope
2d9e2 72 61 74 6f 72 20 77 69 74 68 20 73 70 61 6e 3d  rator with span=
2d9e3 28 6e 4e 65 61 72 2d 31 29 20 2a 2f 0a 20 20 73  (nNear-1) */.  s
2d9e4 69 67 6e 65 64 20 63 68 61 72 20 69 73 4f 72 3b  igned char isOr;
2d9e5 20 20 2f 2a 20 74 68 69 73 20 74 65 72 6d 20 69    /* this term i
2d9e6 73 20 70 72 65 63 65 64 65 64 20 62 79 20 22 4f  s preceded by "O
2d9e7 52 22 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63  R" */.  signed c
2d9e8 68 61 72 20 69 73 4e 6f 74 3b 20 2f 2a 20 74 68  har isNot; /* th
2d9e9 69 73 20 74 65 72 6d 20 69 73 20 70 72 65 63 65  is term is prece
2d9ea 64 65 64 20 62 79 20 22 2d 22 20 2a 2f 0a 20 20  ded by "-" */.  
2d9eb 73 69 67 6e 65 64 20 63 68 61 72 20 69 73 50 72  signed char isPr
2d9ec 65 66 69 78 3b 20 2f 2a 20 74 68 69 73 20 74 65  efix; /* this te
2d9ed 72 6d 20 69 73 20 66 6f 6c 6c 6f 77 65 64 20 62  rm is followed b
2d9ee 79 20 22 2a 22 20 2a 2f 0a 20 20 63 68 61 72 20  y "*" */.  char 
2d9ef 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20 2f 2a  *pTerm;       /*
2d9f0 20 74 65 78 74 20 6f 66 20 74 68 65 20 74 65 72   text of the ter
2d9f1 6d 2e 20 20 27 5c 30 30 30 27 20 74 65 72 6d 69  m.  '\000' termi
2d9f2 6e 61 74 65 64 2e 20 20 6d 61 6c 6c 6f 63 65 64  nated.  malloced
2d9f3 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b   */.  int nTerm;
2d9f4 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
2d9f5 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 70  er of bytes in p
2d9f6 54 65 72 6d 5b 5d 20 2a 2f 0a 7d 20 51 75 65 72  Term[] */.} Quer
2d9f7 79 54 65 72 6d 3b 0a 0a 0a 2f 2a 20 41 20 71 75  yTerm;.../* A qu
2d9f8 65 72 79 20 73 74 72 69 6e 67 20 69 73 20 70 61  ery string is pa
2d9f9 72 73 65 64 20 69 6e 74 6f 20 61 20 51 75 65 72  rsed into a Quer
2d9fa 79 20 73 74 72 75 63 74 75 72 65 2e 0a 20 2a 0a  y structure.. *.
2d9fb 20 2a 20 57 65 20 63 6f 75 6c 64 2c 20 69 6e 20   * We could, in 
2d9fc 74 68 65 6f 72 79 2c 20 61 6c 6c 6f 77 20 71 75  theory, allow qu
2d9fd 65 72 79 20 73 74 72 69 6e 67 73 20 74 6f 20 62  ery strings to b
2d9fe 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 0a 20 2a  e complicated. *
2d9ff 20 6e 65 73 74 65 64 20 65 78 70 72 65 73 73 69   nested expressi
2da00 6f 6e 73 20 77 69 74 68 20 70 72 65 63 65 64 65  ons with precede
2da01 6e 63 65 20 64 65 74 65 72 6d 69 6e 65 64 20 62  nce determined b
2da02 79 20 70 61 72 65 6e 74 68 65 73 65 73 2e 0a 20  y parentheses.. 
2da03 2a 20 42 75 74 20 6e 6f 6e 65 20 6f 66 20 74 68  * But none of th
2da04 65 20 6d 61 6a 6f 72 20 73 65 61 72 63 68 20 65  e major search e
2da05 6e 67 69 6e 65 73 20 64 6f 20 74 68 69 73 2e 20  ngines do this. 
2da06 20 28 50 65 72 68 61 70 73 20 74 68 65 0a 20 2a   (Perhaps the. *
2da07 20 66 65 65 6c 69 6e 67 20 69 73 20 74 68 61 74   feeling is that
2da08 20 61 6e 20 70 61 72 65 6e 74 68 65 73 69 7a 65   an parenthesize
2da09 64 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  d expression is 
2da0a 74 77 6f 20 63 6f 6d 70 6c 65 78 20 6f 66 0a 20  two complex of. 
2da0b 2a 20 61 6e 20 69 64 65 61 20 66 6f 72 20 74 68  * an idea for th
2da0c 65 20 61 76 65 72 61 67 65 20 75 73 65 72 20 74  e average user t
2da0d 6f 20 67 72 61 73 70 2e 29 20 20 54 61 6b 69 6e  o grasp.)  Takin
2da0e 67 20 6f 75 72 20 6c 65 61 64 20 66 72 6f 6d 0a  g our lead from.
2da0f 20 2a 20 74 68 65 20 6d 61 6a 6f 72 20 73 65 61   * the major sea
2da10 72 63 68 20 65 6e 67 69 6e 65 73 2c 20 77 65 20  rch engines, we 
2da11 77 69 6c 6c 20 61 6c 6c 6f 77 20 71 75 65 72 69  will allow queri
2da12 65 73 20 74 6f 20 62 65 20 61 20 6c 69 73 74 0a  es to be a list.
2da13 20 2a 20 6f 66 20 74 65 72 6d 73 20 28 77 69 74   * of terms (wit
2da14 68 20 61 6e 20 69 6d 70 6c 69 65 64 20 41 4e 44  h an implied AND
2da15 20 6f 70 65 72 61 74 6f 72 29 20 6f 72 20 70 68   operator) or ph
2da16 72 61 73 65 73 20 69 6e 20 64 6f 75 62 6c 65 2d  rases in double-
2da17 71 75 6f 74 65 73 2c 0a 20 2a 20 77 69 74 68 20  quotes,. * with 
2da18 61 20 73 69 6e 67 6c 65 20 6f 70 74 69 6f 6e 61  a single optiona
2da19 6c 20 22 2d 22 20 62 65 66 6f 72 65 20 65 61 63  l "-" before eac
2da1a 68 20 6e 6f 6e 2d 70 68 72 61 73 65 20 74 65 72  h non-phrase ter
2da1b 6d 20 74 6f 20 64 65 73 69 67 6e 61 74 65 0a 20  m to designate. 
2da1c 2a 20 6e 65 67 61 74 69 6f 6e 20 61 6e 64 20 61  * negation and a
2da1d 6e 20 6f 70 74 69 6f 6e 61 6c 20 4f 52 20 63 6f  n optional OR co
2da1e 6e 6e 65 63 74 6f 72 2e 0a 20 2a 0a 20 2a 20 4f  nnector.. *. * O
2da1f 52 20 62 69 6e 64 73 20 6d 6f 72 65 20 74 69 67  R binds more tig
2da20 68 74 6c 79 20 74 68 61 6e 20 74 68 65 20 69 6d  htly than the im
2da21 70 6c 69 65 64 20 41 4e 44 2c 20 77 68 69 63 68  plied AND, which
2da22 20 69 73 20 77 68 61 74 20 74 68 65 0a 20 2a 20   is what the. * 
2da23 6d 61 6a 6f 72 20 73 65 61 72 63 68 20 65 6e 67  major search eng
2da24 69 6e 65 73 20 73 65 65 6d 20 74 6f 20 64 6f 2e  ines seem to do.
2da25 20 20 53 6f 2c 20 66 6f 72 20 65 78 61 6d 70 6c    So, for exampl
2da26 65 3a 0a 20 2a 20 0a 20 2a 20 20 20 20 5b 6f 6e  e:. * . *    [on
2da27 65 20 74 77 6f 20 4f 52 20 74 68 72 65 65 5d 20  e two OR three] 
2da28 20 20 20 20 3d 3d 3e 20 20 20 20 6f 6e 65 20 41      ==>    one A
2da29 4e 44 20 28 74 77 6f 20 4f 52 20 74 68 72 65 65  ND (two OR three
2da2a 29 0a 20 2a 20 20 20 20 5b 6f 6e 65 20 4f 52 20  ). *    [one OR 
2da2b 74 77 6f 20 74 68 72 65 65 5d 20 20 20 20 20 3d  two three]     =
2da2c 3d 3e 20 20 20 20 28 6f 6e 65 20 4f 52 20 74 77  =>    (one OR tw
2da2d 6f 29 20 41 4e 44 20 74 68 72 65 65 0a 20 2a 0a  o) AND three. *.
2da2e 20 2a 20 41 20 22 2d 22 20 62 65 66 6f 72 65 20   * A "-" before 
2da2f 61 20 74 65 72 6d 20 6d 61 74 63 68 65 73 20 61  a term matches a
2da30 6c 6c 20 65 6e 74 72 69 65 73 20 74 68 61 74 20  ll entries that 
2da31 6c 61 63 6b 20 74 68 61 74 20 74 65 72 6d 2e 0a  lack that term..
2da32 20 2a 20 54 68 65 20 22 2d 22 20 6d 75 73 74 20   * The "-" must 
2da33 6f 63 63 75 72 20 69 6d 6d 65 64 69 61 74 65 6c  occur immediatel
2da34 79 20 62 65 66 6f 72 65 20 74 68 65 20 74 65 72  y before the ter
2da35 6d 20 77 69 74 68 20 69 6e 20 69 6e 74 65 72 76  m with in interv
2da36 65 6e 69 6e 67 0a 20 2a 20 73 70 61 63 65 2e 20  ening. * space. 
2da37 20 54 68 69 73 20 69 73 20 68 6f 77 20 74 68 65   This is how the
2da38 20 73 65 61 72 63 68 20 65 6e 67 69 6e 65 73 20   search engines 
2da39 64 6f 20 69 74 2e 0a 20 2a 0a 20 2a 20 41 20 4e  do it.. *. * A N
2da3a 4f 54 20 74 65 72 6d 20 63 61 6e 6e 6f 74 20 62  OT term cannot b
2da3b 65 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64  e the right-hand
2da3c 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 6e 20 4f   operand of an O
2da3d 52 2e 20 20 49 66 20 74 68 69 73 0a 20 2a 20 6f  R.  If this. * o
2da3e 63 63 75 72 73 20 69 6e 20 74 68 65 20 71 75 65  ccurs in the que
2da3f 72 79 20 73 74 72 69 6e 67 2c 20 74 68 65 20 4e  ry string, the N
2da40 4f 54 20 69 73 20 69 67 6e 6f 72 65 64 3a 0a 20  OT is ignored:. 
2da41 2a 0a 20 2a 20 20 20 20 5b 6f 6e 65 20 4f 52 20  *. *    [one OR 
2da42 2d 74 77 6f 5d 20 20 20 20 20 20 20 20 20 20 3d  -two]          =
2da43 3d 3e 20 20 20 20 6f 6e 65 20 4f 52 20 74 77 6f  =>    one OR two
2da44 0a 20 2a 0a 20 2a 2f 0a 74 79 70 65 64 65 66 20  . *. */.typedef 
2da45 73 74 72 75 63 74 20 51 75 65 72 79 20 7b 0a 20  struct Query {. 
2da46 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a   fulltext_vtab *
2da47 70 46 74 73 3b 20 20 2f 2a 20 54 68 65 20 66 75  pFts;  /* The fu
2da48 6c 6c 20 74 65 78 74 20 69 6e 64 65 78 20 2a 2f  ll text index */
2da49 0a 20 20 69 6e 74 20 6e 54 65 72 6d 73 3b 20 20  .  int nTerms;  
2da4a 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
2da4b 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 74  er of terms in t
2da4c 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 51 75  he query */.  Qu
2da4d 65 72 79 54 65 72 6d 20 2a 70 54 65 72 6d 73 3b  eryTerm *pTerms;
2da4e 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20      /* Array of 
2da4f 74 65 72 6d 73 2e 20 20 53 70 61 63 65 20 6f 62  terms.  Space ob
2da50 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c  tained from mall
2da51 6f 63 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 65  oc() */.  int ne
2da52 78 74 49 73 4f 72 3b 20 20 20 20 20 20 20 20 20  xtIsOr;         
2da53 2f 2a 20 53 65 74 20 74 68 65 20 69 73 4f 72 20  /* Set the isOr 
2da54 66 6c 61 67 20 6f 6e 20 74 68 65 20 6e 65 78 74  flag on the next
2da55 20 69 6e 73 65 72 74 65 64 20 74 65 72 6d 20 2a   inserted term *
2da56 2f 0a 20 20 69 6e 74 20 6e 65 78 74 49 73 4e 65  /.  int nextIsNe
2da57 61 72 3b 20 20 20 20 20 20 20 2f 2a 20 53 65 74  ar;       /* Set
2da58 20 74 68 65 20 69 73 4f 72 20 66 6c 61 67 20 6f   the isOr flag o
2da59 6e 20 74 68 65 20 6e 65 78 74 20 69 6e 73 65 72  n the next inser
2da5a 74 65 64 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e  ted term */.  in
2da5b 74 20 6e 65 78 74 43 6f 6c 75 6d 6e 3b 20 20 20  t nextColumn;   
2da5c 20 20 20 20 2f 2a 20 4e 65 78 74 20 77 6f 72 64      /* Next word
2da5d 20 70 61 72 73 65 64 20 6d 75 73 74 20 62 65 20   parsed must be 
2da5e 69 6e 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a  in this column *
2da5f 2f 0a 20 20 69 6e 74 20 64 66 6c 74 43 6f 6c 75  /.  int dfltColu
2da60 6d 6e 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  mn;       /* The
2da61 20 64 65 66 61 75 6c 74 20 63 6f 6c 75 6d 6e 20   default column 
2da62 2a 2f 0a 7d 20 51 75 65 72 79 3b 0a 0a 0a 2f 2a  */.} Query;.../*
2da63 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
2da64 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
2da65 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 73   structure keeps
2da66 20 74 72 61 63 6b 20 6f 66 20 67 65 6e 65 72 61   track of genera
2da67 74 65 64 0a 2a 2a 20 6d 61 74 63 68 69 6e 67 2d  ted.** matching-
2da68 77 6f 72 64 20 6f 66 66 73 65 74 20 69 6e 66 6f  word offset info
2da69 72 6d 61 74 69 6f 6e 20 61 6e 64 20 73 6e 69 70  rmation and snip
2da6a 70 65 74 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  pets..*/.typedef
2da6b 20 73 74 72 75 63 74 20 53 6e 69 70 70 65 74 20   struct Snippet 
2da6c 7b 0a 20 20 69 6e 74 20 6e 4d 61 74 63 68 3b 20  {.  int nMatch; 
2da6d 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d      /* Total num
2da6e 62 65 72 20 6f 66 20 6d 61 74 63 68 65 73 20 2a  ber of matches *
2da6f 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20  /.  int nAlloc; 
2da70 20 20 20 20 2f 2a 20 53 70 61 63 65 20 61 6c 6c      /* Space all
2da71 6f 63 61 74 65 64 20 66 6f 72 20 61 4d 61 74 63  ocated for aMatc
2da72 68 5b 5d 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  h[] */.  struct 
2da73 73 6e 69 70 70 65 74 4d 61 74 63 68 20 7b 20 2f  snippetMatch { /
2da74 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20  * One entry for 
2da75 65 61 63 68 20 6d 61 74 63 68 69 6e 67 20 74 65  each matching te
2da76 72 6d 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 73  rm */.    char s
2da77 6e 53 74 61 74 75 73 3b 20 20 20 20 20 20 20 2f  nStatus;       /
2da78 2a 20 53 74 61 74 75 73 20 66 6c 61 67 20 66 6f  * Status flag fo
2da79 72 20 75 73 65 20 77 68 69 6c 65 20 63 6f 6e 73  r use while cons
2da7a 74 72 75 63 74 69 6e 67 20 73 6e 69 70 70 65 74  tructing snippet
2da7b 73 20 2a 2f 0a 20 20 20 20 73 68 6f 72 74 20 69  s */.    short i
2da7c 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 2f 2a  nt iCol;      /*
2da7d 20 54 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74   The column that
2da7e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6d 61   contains the ma
2da7f 74 63 68 20 2a 2f 0a 20 20 20 20 73 68 6f 72 74  tch */.    short
2da80 20 69 6e 74 20 69 54 65 72 6d 3b 20 20 20 20 20   int iTerm;     
2da81 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 69 6e 20  /* The index in 
2da82 51 75 65 72 79 2e 70 54 65 72 6d 73 5b 5d 20 6f  Query.pTerms[] o
2da83 66 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 74  f the matching t
2da84 65 72 6d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  erm */.    int i
2da85 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20  Token;          
2da86 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20  /* The index of 
2da87 74 68 65 20 6d 61 74 63 68 69 6e 67 20 64 6f 63  the matching doc
2da88 75 6d 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20  ument token */. 
2da89 20 20 20 73 68 6f 72 74 20 69 6e 74 20 6e 42 79     short int nBy
2da8a 74 65 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  te;     /* Numbe
2da8b 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
2da8c 65 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 69 6e  e term */.    in
2da8d 74 20 69 53 74 61 72 74 3b 20 20 20 20 20 20 20  t iStart;       
2da8e 20 20 20 2f 2a 20 54 68 65 20 6f 66 66 73 65 74     /* The offset
2da8f 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 63 68   to the first ch
2da90 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 20 74  aracter of the t
2da91 65 72 6d 20 2a 2f 0a 20 20 7d 20 2a 61 4d 61 74  erm */.  } *aMat
2da92 63 68 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  ch;      /* Poin
2da93 74 73 20 74 6f 20 73 70 61 63 65 20 6f 62 74 61  ts to space obta
2da94 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63  ined from malloc
2da95 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4f 66 66   */.  char *zOff
2da96 73 65 74 3b 20 20 2f 2a 20 54 65 78 74 20 72 65  set;  /* Text re
2da97 6e 64 65 72 69 6e 67 20 6f 66 20 61 4d 61 74 63  ndering of aMatc
2da98 68 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 66  h[] */.  int nOf
2da99 66 73 65 74 3b 20 20 20 20 2f 2a 20 73 74 72 6c  fset;    /* strl
2da9a 65 6e 28 7a 4f 66 66 73 65 74 29 20 2a 2f 0a 20  en(zOffset) */. 
2da9b 20 63 68 61 72 20 2a 7a 53 6e 69 70 70 65 74 3b   char *zSnippet;
2da9c 20 2f 2a 20 53 6e 69 70 70 65 74 20 74 65 78 74   /* Snippet text
2da9d 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 6e 69 70 70   */.  int nSnipp
2da9e 65 74 3b 20 20 20 2f 2a 20 73 74 72 6c 65 6e 28  et;   /* strlen(
2da9f 7a 53 6e 69 70 70 65 74 29 20 2a 2f 0a 7d 20 53  zSnippet) */.} S
2daa0 6e 69 70 70 65 74 3b 0a 0a 0a 74 79 70 65 64 65  nippet;...typede
2daa1 66 20 65 6e 75 6d 20 51 75 65 72 79 54 79 70 65  f enum QueryType
2daa2 20 7b 0a 20 20 51 55 45 52 59 5f 47 45 4e 45 52   {.  QUERY_GENER
2daa3 49 43 2c 20 20 20 2f 2a 20 74 61 62 6c 65 20 73  IC,   /* table s
2daa4 63 61 6e 20 2a 2f 0a 20 20 51 55 45 52 59 5f 44  can */.  QUERY_D
2daa5 4f 43 49 44 2c 20 20 20 20 20 2f 2a 20 6c 6f 6f  OCID,     /* loo
2daa6 6b 75 70 20 62 79 20 64 6f 63 69 64 20 2a 2f 0a  kup by docid */.
2daa7 20 20 51 55 45 52 59 5f 46 55 4c 4c 54 45 58 54    QUERY_FULLTEXT
2daa8 20 20 20 2f 2a 20 51 55 45 52 59 5f 46 55 4c 4c     /* QUERY_FULL
2daa9 54 45 58 54 20 2b 20 5b 69 5d 20 69 73 20 61 20  TEXT + [i] is a 
2daaa 66 75 6c 6c 2d 74 65 78 74 20 73 65 61 72 63 68  full-text search
2daab 20 66 6f 72 20 63 6f 6c 75 6d 6e 20 69 2a 2f 0a   for column i*/.
2daac 7d 20 51 75 65 72 79 54 79 70 65 3b 0a 0a 74 79  } QueryType;..ty
2daad 70 65 64 65 66 20 65 6e 75 6d 20 66 75 6c 6c 74  pedef enum fullt
2daae 65 78 74 5f 73 74 61 74 65 6d 65 6e 74 20 7b 0a  ext_statement {.
2daaf 20 20 43 4f 4e 54 45 4e 54 5f 49 4e 53 45 52 54    CONTENT_INSERT
2dab0 5f 53 54 4d 54 2c 0a 20 20 43 4f 4e 54 45 4e 54  _STMT,.  CONTENT
2dab1 5f 53 45 4c 45 43 54 5f 53 54 4d 54 2c 0a 20 20  _SELECT_STMT,.  
2dab2 43 4f 4e 54 45 4e 54 5f 55 50 44 41 54 45 5f 53  CONTENT_UPDATE_S
2dab3 54 4d 54 2c 0a 20 20 43 4f 4e 54 45 4e 54 5f 44  TMT,.  CONTENT_D
2dab4 45 4c 45 54 45 5f 53 54 4d 54 2c 0a 20 20 43 4f  ELETE_STMT,.  CO
2dab5 4e 54 45 4e 54 5f 45 58 49 53 54 53 5f 53 54 4d  NTENT_EXISTS_STM
2dab6 54 2c 0a 0a 20 20 42 4c 4f 43 4b 5f 49 4e 53 45  T,..  BLOCK_INSE
2dab7 52 54 5f 53 54 4d 54 2c 0a 20 20 42 4c 4f 43 4b  RT_STMT,.  BLOCK
2dab8 5f 53 45 4c 45 43 54 5f 53 54 4d 54 2c 0a 20 20  _SELECT_STMT,.  
2dab9 42 4c 4f 43 4b 5f 44 45 4c 45 54 45 5f 53 54 4d  BLOCK_DELETE_STM
2daba 54 2c 0a 20 20 42 4c 4f 43 4b 5f 44 45 4c 45 54  T,.  BLOCK_DELET
2dabb 45 5f 41 4c 4c 5f 53 54 4d 54 2c 0a 0a 20 20 53  E_ALL_STMT,..  S
2dabc 45 47 44 49 52 5f 4d 41 58 5f 49 4e 44 45 58 5f  EGDIR_MAX_INDEX_
2dabd 53 54 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f 53  STMT,.  SEGDIR_S
2dabe 45 54 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44 49  ET_STMT,.  SEGDI
2dabf 52 5f 53 45 4c 45 43 54 5f 4c 45 56 45 4c 5f 53  R_SELECT_LEVEL_S
2dac0 54 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f 53 50  TMT,.  SEGDIR_SP
2dac1 41 4e 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44 49  AN_STMT,.  SEGDI
2dac2 52 5f 44 45 4c 45 54 45 5f 53 54 4d 54 2c 0a 20  R_DELETE_STMT,. 
2dac3 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 53   SEGDIR_SELECT_S
2dac4 45 47 4d 45 4e 54 5f 53 54 4d 54 2c 0a 20 20 53  EGMENT_STMT,.  S
2dac5 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 41 4c 4c  EGDIR_SELECT_ALL
2dac6 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f  _STMT,.  SEGDIR_
2dac7 44 45 4c 45 54 45 5f 41 4c 4c 5f 53 54 4d 54 2c  DELETE_ALL_STMT,
2dac8 0a 20 20 53 45 47 44 49 52 5f 43 4f 55 4e 54 5f  .  SEGDIR_COUNT_
2dac9 53 54 4d 54 2c 0a 0a 20 20 4d 41 58 5f 53 54 4d  STMT,..  MAX_STM
2daca 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
2dacb 20 20 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 20        /* Always 
2dacc 61 74 20 65 6e 64 21 20 2a 2f 0a 7d 20 66 75 6c  at end! */.} ful
2dacd 6c 74 65 78 74 5f 73 74 61 74 65 6d 65 6e 74 3b  ltext_statement;
2dace 0a 0a 2f 2a 20 54 68 65 73 65 20 6d 75 73 74 20  ../* These must 
2dacf 65 78 61 63 74 6c 79 20 6d 61 74 63 68 20 74 68  exactly match th
2dad0 65 20 65 6e 75 6d 20 61 62 6f 76 65 2e 20 2a 2f  e enum above. */
2dad1 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 3a  ./* TODO(shess):
2dad2 20 49 73 20 74 68 65 72 65 20 73 6f 6d 65 20 72   Is there some r
2dad3 69 73 6b 20 74 68 61 74 20 61 20 73 74 61 74 65  isk that a state
2dad4 6d 65 6e 74 20 77 69 6c 6c 20 62 65 20 75 73 65  ment will be use
2dad5 64 20 69 6e 20 74 77 6f 0a 2a 2a 20 63 75 72 73  d in two.** curs
2dad6 6f 72 73 20 61 74 20 6f 6e 63 65 2c 20 65 2e 67  ors at once, e.g
2dad7 2e 20 20 69 66 20 61 20 71 75 65 72 79 20 6a 6f  .  if a query jo
2dad8 69 6e 73 20 61 20 76 69 72 74 75 61 6c 20 74 61  ins a virtual ta
2dad9 62 6c 65 20 74 6f 20 69 74 73 65 6c 66 3f 0a 2a  ble to itself?.*
2dada 2a 20 49 66 20 73 6f 20 70 65 72 68 61 70 73 20  * If so perhaps 
2dadb 77 65 20 73 68 6f 75 6c 64 20 6d 6f 76 65 20 73  we should move s
2dadc 6f 6d 65 20 6f 66 20 74 68 65 73 65 20 74 6f 20  ome of these to 
2dadd 74 68 65 20 63 75 72 73 6f 72 20 6f 62 6a 65 63  the cursor objec
2dade 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  t..*/.static con
2dadf 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20 66  st char *const f
2dae0 75 6c 6c 74 65 78 74 5f 7a 53 74 61 74 65 6d 65  ulltext_zStateme
2dae1 6e 74 5b 4d 41 58 5f 53 54 4d 54 5d 20 3d 20 7b  nt[MAX_STMT] = {
2dae2 0a 20 20 2f 2a 20 43 4f 4e 54 45 4e 54 5f 49 4e  .  /* CONTENT_IN
2dae3 53 45 52 54 20 2a 2f 20 4e 55 4c 4c 2c 20 20 2f  SERT */ NULL,  /
2dae4 2a 20 67 65 6e 65 72 61 74 65 64 20 69 6e 20 63  * generated in c
2dae5 6f 6e 74 65 6e 74 49 6e 73 65 72 74 53 74 61 74  ontentInsertStat
2dae6 65 6d 65 6e 74 28 29 20 2a 2f 0a 20 20 2f 2a 20  ement() */.  /* 
2dae7 43 4f 4e 54 45 4e 54 5f 53 45 4c 45 43 54 20 2a  CONTENT_SELECT *
2dae8 2f 20 4e 55 4c 4c 2c 20 20 2f 2a 20 67 65 6e 65  / NULL,  /* gene
2dae9 72 61 74 65 64 20 69 6e 20 63 6f 6e 74 65 6e 74  rated in content
2daea 53 65 6c 65 63 74 53 74 61 74 65 6d 65 6e 74 28  SelectStatement(
2daeb 29 20 2a 2f 0a 20 20 2f 2a 20 43 4f 4e 54 45 4e  ) */.  /* CONTEN
2daec 54 5f 55 50 44 41 54 45 20 2a 2f 20 4e 55 4c 4c  T_UPDATE */ NULL
2daed 2c 20 20 2f 2a 20 67 65 6e 65 72 61 74 65 64 20  ,  /* generated 
2daee 69 6e 20 63 6f 6e 74 65 6e 74 55 70 64 61 74 65  in contentUpdate
2daef 53 74 61 74 65 6d 65 6e 74 28 29 20 2a 2f 0a 20  Statement() */. 
2daf0 20 2f 2a 20 43 4f 4e 54 45 4e 54 5f 44 45 4c 45   /* CONTENT_DELE
2daf1 54 45 20 2a 2f 20 22 64 65 6c 65 74 65 20 66 72  TE */ "delete fr
2daf2 6f 6d 20 25 5f 63 6f 6e 74 65 6e 74 20 77 68 65  om %_content whe
2daf3 72 65 20 64 6f 63 69 64 20 3d 20 3f 22 2c 0a 20  re docid = ?",. 
2daf4 20 2f 2a 20 43 4f 4e 54 45 4e 54 5f 45 58 49 53   /* CONTENT_EXIS
2daf5 54 53 20 2a 2f 20 22 73 65 6c 65 63 74 20 64 6f  TS */ "select do
2daf6 63 69 64 20 66 72 6f 6d 20 25 5f 63 6f 6e 74 65  cid from %_conte
2daf7 6e 74 20 6c 69 6d 69 74 20 31 22 2c 0a 0a 20 20  nt limit 1",..  
2daf8 2f 2a 20 42 4c 4f 43 4b 5f 49 4e 53 45 52 54 20  /* BLOCK_INSERT 
2daf9 2a 2f 0a 20 20 22 69 6e 73 65 72 74 20 69 6e 74  */.  "insert int
2dafa 6f 20 25 5f 73 65 67 6d 65 6e 74 73 20 28 62 6c  o %_segments (bl
2dafb 6f 63 6b 69 64 2c 20 62 6c 6f 63 6b 29 20 76 61  ockid, block) va
2dafc 6c 75 65 73 20 28 6e 75 6c 6c 2c 20 3f 29 22 2c  lues (null, ?)",
2dafd 0a 20 20 2f 2a 20 42 4c 4f 43 4b 5f 53 45 4c 45  .  /* BLOCK_SELE
2dafe 43 54 20 2a 2f 20 22 73 65 6c 65 63 74 20 62 6c  CT */ "select bl
2daff 6f 63 6b 20 66 72 6f 6d 20 25 5f 73 65 67 6d 65  ock from %_segme
2db00 6e 74 73 20 77 68 65 72 65 20 62 6c 6f 63 6b 69  nts where blocki
2db01 64 20 3d 20 3f 22 2c 0a 20 20 2f 2a 20 42 4c 4f  d = ?",.  /* BLO
2db02 43 4b 5f 44 45 4c 45 54 45 20 2a 2f 20 22 64 65  CK_DELETE */ "de
2db03 6c 65 74 65 20 66 72 6f 6d 20 25 5f 73 65 67 6d  lete from %_segm
2db04 65 6e 74 73 20 77 68 65 72 65 20 62 6c 6f 63 6b  ents where block
2db05 69 64 20 62 65 74 77 65 65 6e 20 3f 20 61 6e 64  id between ? and
2db06 20 3f 22 2c 0a 20 20 2f 2a 20 42 4c 4f 43 4b 5f   ?",.  /* BLOCK_
2db07 44 45 4c 45 54 45 5f 41 4c 4c 20 2a 2f 20 22 64  DELETE_ALL */ "d
2db08 65 6c 65 74 65 20 66 72 6f 6d 20 25 5f 73 65 67  elete from %_seg
2db09 6d 65 6e 74 73 22 2c 0a 0a 20 20 2f 2a 20 53 45  ments",..  /* SE
2db0a 47 44 49 52 5f 4d 41 58 5f 49 4e 44 45 58 20 2a  GDIR_MAX_INDEX *
2db0b 2f 20 22 73 65 6c 65 63 74 20 6d 61 78 28 69 64  / "select max(id
2db0c 78 29 20 66 72 6f 6d 20 25 5f 73 65 67 64 69 72  x) from %_segdir
2db0d 20 77 68 65 72 65 20 6c 65 76 65 6c 20 3d 20 3f   where level = ?
2db0e 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f 53  ",.  /* SEGDIR_S
2db0f 45 54 20 2a 2f 20 22 69 6e 73 65 72 74 20 69 6e  ET */ "insert in
2db10 74 6f 20 25 5f 73 65 67 64 69 72 20 76 61 6c 75  to %_segdir valu
2db11 65 73 20 28 3f 2c 20 3f 2c 20 3f 2c 20 3f 2c 20  es (?, ?, ?, ?, 
2db12 3f 2c 20 3f 29 22 2c 0a 20 20 2f 2a 20 53 45 47  ?, ?)",.  /* SEG
2db13 44 49 52 5f 53 45 4c 45 43 54 5f 4c 45 56 45 4c  DIR_SELECT_LEVEL
2db14 20 2a 2f 0a 20 20 22 73 65 6c 65 63 74 20 73 74   */.  "select st
2db15 61 72 74 5f 62 6c 6f 63 6b 2c 20 6c 65 61 76 65  art_block, leave
2db16 73 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 72 6f 6f  s_end_block, roo
2db17 74 20 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 20  t from %_segdir 
2db18 22 0a 20 20 22 20 77 68 65 72 65 20 6c 65 76 65  ".  " where leve
2db19 6c 20 3d 20 3f 20 6f 72 64 65 72 20 62 79 20 69  l = ? order by i
2db1a 64 78 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52  dx",.  /* SEGDIR
2db1b 5f 53 50 41 4e 20 2a 2f 0a 20 20 22 73 65 6c 65  _SPAN */.  "sele
2db1c 63 74 20 6d 69 6e 28 73 74 61 72 74 5f 62 6c 6f  ct min(start_blo
2db1d 63 6b 29 2c 20 6d 61 78 28 65 6e 64 5f 62 6c 6f  ck), max(end_blo
2db1e 63 6b 29 20 66 72 6f 6d 20 25 5f 73 65 67 64 69  ck) from %_segdi
2db1f 72 20 22 0a 20 20 22 20 77 68 65 72 65 20 6c 65  r ".  " where le
2db20 76 65 6c 20 3d 20 3f 20 61 6e 64 20 73 74 61 72  vel = ? and star
2db21 74 5f 62 6c 6f 63 6b 20 3c 3e 20 30 22 2c 0a 20  t_block <> 0",. 
2db22 20 2f 2a 20 53 45 47 44 49 52 5f 44 45 4c 45 54   /* SEGDIR_DELET
2db23 45 20 2a 2f 20 22 64 65 6c 65 74 65 20 66 72 6f  E */ "delete fro
2db24 6d 20 25 5f 73 65 67 64 69 72 20 77 68 65 72 65  m %_segdir where
2db25 20 6c 65 76 65 6c 20 3d 20 3f 22 2c 0a 0a 20 20   level = ?",..  
2db26 2f 2a 20 4e 4f 54 45 28 73 68 65 73 73 29 3a 20  /* NOTE(shess): 
2db27 54 68 65 20 66 69 72 73 74 20 74 68 72 65 65 20  The first three 
2db28 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 66  results of the f
2db29 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 0a 20 20 2a  ollowing two.  *
2db2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 6d 75 73  * statements mus
2db2b 74 20 6d 61 74 63 68 2e 0a 20 20 2a 2f 0a 20 20  t match..  */.  
2db2c 2f 2a 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54  /* SEGDIR_SELECT
2db2d 5f 53 45 47 4d 45 4e 54 20 2a 2f 0a 20 20 22 73  _SEGMENT */.  "s
2db2e 65 6c 65 63 74 20 73 74 61 72 74 5f 62 6c 6f 63  elect start_bloc
2db2f 6b 2c 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c  k, leaves_end_bl
2db30 6f 63 6b 2c 20 72 6f 6f 74 20 66 72 6f 6d 20 25  ock, root from %
2db31 5f 73 65 67 64 69 72 20 22 0a 20 20 22 20 77 68  _segdir ".  " wh
2db32 65 72 65 20 6c 65 76 65 6c 20 3d 20 3f 20 61 6e  ere level = ? an
2db33 64 20 69 64 78 20 3d 20 3f 22 2c 0a 20 20 2f 2a  d idx = ?",.  /*
2db34 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 41   SEGDIR_SELECT_A
2db35 4c 4c 20 2a 2f 0a 20 20 22 73 65 6c 65 63 74 20  LL */.  "select 
2db36 73 74 61 72 74 5f 62 6c 6f 63 6b 2c 20 6c 65 61  start_block, lea
2db37 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 72  ves_end_block, r
2db38 6f 6f 74 20 66 72 6f 6d 20 25 5f 73 65 67 64 69  oot from %_segdi
2db39 72 20 22 0a 20 20 22 20 6f 72 64 65 72 20 62 79  r ".  " order by
2db3a 20 6c 65 76 65 6c 20 64 65 73 63 2c 20 69 64 78   level desc, idx
2db3b 20 61 73 63 22 2c 0a 20 20 2f 2a 20 53 45 47 44   asc",.  /* SEGD
2db3c 49 52 5f 44 45 4c 45 54 45 5f 41 4c 4c 20 2a 2f  IR_DELETE_ALL */
2db3d 20 22 64 65 6c 65 74 65 20 66 72 6f 6d 20 25 5f   "delete from %_
2db3e 73 65 67 64 69 72 22 2c 0a 20 20 2f 2a 20 53 45  segdir",.  /* SE
2db3f 47 44 49 52 5f 43 4f 55 4e 54 20 2a 2f 20 22 73  GDIR_COUNT */ "s
2db40 65 6c 65 63 74 20 63 6f 75 6e 74 28 2a 29 2c 20  elect count(*), 
2db41 69 66 6e 75 6c 6c 28 6d 61 78 28 6c 65 76 65 6c  ifnull(max(level
2db42 29 2c 30 29 20 66 72 6f 6d 20 25 5f 73 65 67 64  ),0) from %_segd
2db43 69 72 22 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  ir",.};../*.** A
2db44 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 61   connection to a
2db45 20 66 75 6c 6c 74 65 78 74 20 69 6e 64 65 78 20   fulltext index 
2db46 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
2db47 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  f the following.
2db48 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  ** structure.  T
2db49 68 65 20 78 43 72 65 61 74 65 20 61 6e 64 20 78  he xCreate and x
2db4a 43 6f 6e 6e 65 63 74 20 6d 65 74 68 6f 64 73 20  Connect methods 
2db4b 63 72 65 61 74 65 20 61 6e 20 69 6e 73 74 61 6e  create an instan
2db4c 63 65 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74  ce.** of this st
2db4d 72 75 63 74 75 72 65 20 61 6e 64 20 78 44 65 73  ructure and xDes
2db4e 74 72 6f 79 20 61 6e 64 20 78 44 69 73 63 6f 6e  troy and xDiscon
2db4f 6e 65 63 74 20 66 72 65 65 20 74 68 61 74 20 69  nect free that i
2db50 6e 73 74 61 6e 63 65 2e 0a 2a 2a 20 41 6c 6c 20  nstance..** All 
2db51 6f 74 68 65 72 20 6d 65 74 68 6f 64 73 20 72 65  other methods re
2db52 63 65 69 76 65 20 61 20 70 6f 69 6e 74 65 72 20  ceive a pointer 
2db53 74 6f 20 74 68 65 20 73 74 72 75 63 74 75 72 65  to the structure
2db54 20 61 73 20 6f 6e 65 20 6f 66 20 74 68 65 69 72   as one of their
2db55 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a  .** arguments..*
2db56 2f 0a 73 74 72 75 63 74 20 66 75 6c 6c 74 65 78  /.struct fulltex
2db57 74 5f 76 74 61 62 20 7b 0a 20 20 73 71 6c 69 74  t_vtab {.  sqlit
2db58 65 33 5f 76 74 61 62 20 62 61 73 65 3b 20 20 20  e3_vtab base;   
2db59 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
2db5a 61 73 65 20 63 6c 61 73 73 20 75 73 65 64 20 62  ase class used b
2db5b 79 20 53 51 4c 69 74 65 20 63 6f 72 65 20 2a 2f  y SQLite core */
2db5c 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
2db5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2db5e 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
2db5f 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
2db60 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
2db61 7a 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  zDb;            
2db62 20 20 20 20 20 2f 2a 20 6c 6f 67 69 63 61 6c 20       /* logical 
2db63 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f  database name */
2db64 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2db65 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  Name;           
2db66 20 20 20 20 2f 2a 20 76 69 72 74 75 61 6c 20 74      /* virtual t
2db67 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 69  able name */.  i
2db68 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20  nt nColumn;     
2db69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2db6a 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  /* number of col
2db6b 75 6d 6e 73 20 69 6e 20 76 69 72 74 75 61 6c 20  umns in virtual 
2db6c 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20  table */.  char 
2db6d 2a 2a 61 7a 43 6f 6c 75 6d 6e 3b 20 20 20 20 20  **azColumn;     
2db6e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63              /* c
2db6f 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 6d 61  olumn names.  ma
2db70 6c 6c 6f 63 65 64 20 2a 2f 0a 20 20 63 68 61 72  lloced */.  char
2db71 20 2a 2a 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75   **azContentColu
2db72 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  mn;          /* 
2db73 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 6e 20  column names in 
2db74 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 3b 20 6d  content table; m
2db75 61 6c 6c 6f 63 65 64 20 2a 2f 0a 20 20 73 71 6c  alloced */.  sql
2db76 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
2db77 70 54 6f 6b 65 6e 69 7a 65 72 3b 20 20 20 2f 2a  pTokenizer;   /*
2db78 20 74 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 69   tokenizer for i
2db79 6e 73 65 72 74 73 20 61 6e 64 20 71 75 65 72 69  nserts and queri
2db7a 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 50 72 65 63  es */..  /* Prec
2db7b 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e  ompiled statemen
2db7c 74 73 20 77 68 69 63 68 20 77 65 20 6b 65 65 70  ts which we keep
2db7d 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20   as long as the 
2db7e 74 61 62 6c 65 20 69 73 0a 20 20 2a 2a 20 6f 70  table is.  ** op
2db7f 65 6e 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  en..  */.  sqlit
2db80 65 33 5f 73 74 6d 74 20 2a 70 46 75 6c 6c 74 65  e3_stmt *pFullte
2db81 78 74 53 74 61 74 65 6d 65 6e 74 73 5b 4d 41 58  xtStatements[MAX
2db82 5f 53 54 4d 54 5d 3b 0a 0a 20 20 2f 2a 20 50 72  _STMT];..  /* Pr
2db83 65 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d  ecompiled statem
2db84 65 6e 74 73 20 75 73 65 64 20 66 6f 72 20 73 65  ents used for se
2db85 67 6d 65 6e 74 20 6d 65 72 67 65 73 2e 20 20 57  gment merges.  W
2db86 65 20 72 75 6e 20 61 0a 20 20 2a 2a 20 73 65 70  e run a.  ** sep
2db87 61 72 61 74 65 20 73 65 6c 65 63 74 20 61 63 72  arate select acr
2db88 6f 73 73 20 74 68 65 20 6c 65 61 66 20 6c 65 76  oss the leaf lev
2db89 65 6c 20 6f 66 20 65 61 63 68 20 74 72 65 65 20  el of each tree 
2db8a 62 65 69 6e 67 20 6d 65 72 67 65 64 2e 0a 20 20  being merged..  
2db8b 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
2db8c 74 20 2a 70 4c 65 61 66 53 65 6c 65 63 74 53 74  t *pLeafSelectSt
2db8d 6d 74 73 5b 4d 45 52 47 45 5f 43 4f 55 4e 54 5d  mts[MERGE_COUNT]
2db8e 3b 0a 20 20 2f 2a 20 54 68 65 20 73 74 61 74 65  ;.  /* The state
2db8f 6d 65 6e 74 20 75 73 65 64 20 74 6f 20 70 72 65  ment used to pre
2db90 70 61 72 65 20 70 4c 65 61 66 53 65 6c 65 63 74  pare pLeafSelect
2db91 53 74 6d 74 73 2e 20 2a 2f 0a 23 64 65 66 69 6e  Stmts. */.#defin
2db92 65 20 4c 45 41 46 5f 53 45 4c 45 43 54 20 5c 0a  e LEAF_SELECT \.
2db93 20 20 22 73 65 6c 65 63 74 20 62 6c 6f 63 6b 20    "select block 
2db94 66 72 6f 6d 20 25 5f 73 65 67 6d 65 6e 74 73 20  from %_segments 
2db95 77 68 65 72 65 20 62 6c 6f 63 6b 69 64 20 62 65  where blockid be
2db96 74 77 65 65 6e 20 3f 20 61 6e 64 20 3f 20 6f 72  tween ? and ? or
2db97 64 65 72 20 62 79 20 62 6c 6f 63 6b 69 64 22 0a  der by blockid".
2db98 0a 20 20 2f 2a 20 54 68 65 73 65 20 62 75 66 66  .  /* These buff
2db99 65 72 20 70 65 6e 64 69 6e 67 20 69 6e 64 65 78  er pending index
2db9a 20 75 70 64 61 74 65 73 20 64 75 72 69 6e 67 20   updates during 
2db9b 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 20 20  transactions..  
2db9c 2a 2a 20 6e 50 65 6e 64 69 6e 67 44 61 74 61 20  ** nPendingData 
2db9d 65 73 74 69 6d 61 74 65 73 20 74 68 65 20 6d 65  estimates the me
2db9e 6d 6f 72 79 20 73 69 7a 65 20 6f 66 20 74 68 65  mory size of the
2db9f 20 70 65 6e 64 69 6e 67 20 64 61 74 61 2e 20 20   pending data.  
2dba0 49 74 0a 20 20 2a 2a 20 64 6f 65 73 6e 27 74 20  It.  ** doesn't 
2dba1 69 6e 63 6c 75 64 65 20 74 68 65 20 68 61 73 68  include the hash
2dba2 2d 62 75 63 6b 65 74 20 6f 76 65 72 68 65 61 64  -bucket overhead
2dba3 2c 20 6e 6f 72 20 61 6e 79 20 6d 61 6c 6c 6f 63  , nor any malloc
2dba4 0a 20 20 2a 2a 20 6f 76 65 72 68 65 61 64 2e 20  .  ** overhead. 
2dba5 20 57 68 65 6e 20 6e 50 65 6e 64 69 6e 67 44 61   When nPendingDa
2dba6 74 61 20 65 78 63 65 65 64 73 20 6b 50 65 6e 64  ta exceeds kPend
2dba7 69 6e 67 54 68 72 65 73 68 6f 6c 64 2c 20 74 68  ingThreshold, th
2dba8 65 0a 20 20 2a 2a 20 62 75 66 66 65 72 20 69 73  e.  ** buffer is
2dba9 20 66 6c 75 73 68 65 64 20 65 76 65 6e 20 62 65   flushed even be
2dbaa 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73 61 63  fore the transac
2dbab 74 69 6f 6e 20 63 6c 6f 73 65 73 2e 0a 20 20 2a  tion closes..  *
2dbac 2a 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20 73  * pendingTerms s
2dbad 74 6f 72 65 73 20 74 68 65 20 64 61 74 61 2c 20  tores the data, 
2dbae 61 6e 64 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69  and is only vali
2dbaf 64 20 77 68 65 6e 20 6e 50 65 6e 64 69 6e 67 44  d when nPendingD
2dbb0 61 74 61 0a 20 20 2a 2a 20 69 73 20 3e 3d 30 20  ata.  ** is >=0 
2dbb1 28 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c 30 20  (nPendingData<0 
2dbb2 6d 65 61 6e 73 20 70 65 6e 64 69 6e 67 54 65 72  means pendingTer
2dbb3 6d 73 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 0a  ms has not been.
2dbb4 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64    ** initialized
2dbb5 29 2e 20 20 69 50 72 65 76 44 6f 63 69 64 20 69  ).  iPrevDocid i
2dbb6 73 20 74 68 65 20 6c 61 73 74 20 64 6f 63 69 64  s the last docid
2dbb7 20 77 72 69 74 74 65 6e 2c 20 75 73 65 64 20 74   written, used t
2dbb8 6f 20 6d 61 6b 65 0a 20 20 2a 2a 20 63 65 72 74  o make.  ** cert
2dbb9 61 69 6e 20 77 65 27 72 65 20 69 6e 73 65 72 74  ain we're insert
2dbba 69 6e 67 20 69 6e 20 73 6f 72 74 65 64 20 6f 72  ing in sorted or
2dbbb 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  der..  */.  int 
2dbbc 6e 50 65 6e 64 69 6e 67 44 61 74 61 3b 0a 23 64  nPendingData;.#d
2dbbd 65 66 69 6e 65 20 6b 50 65 6e 64 69 6e 67 54 68  efine kPendingTh
2dbbe 72 65 73 68 6f 6c 64 20 28 31 2a 31 30 32 34 2a  reshold (1*1024*
2dbbf 31 30 32 34 29 0a 20 20 73 71 6c 69 74 65 5f 69  1024).  sqlite_i
2dbc0 6e 74 36 34 20 69 50 72 65 76 44 6f 63 69 64 3b  nt64 iPrevDocid;
2dbc1 0a 20 20 66 74 73 33 48 61 73 68 20 70 65 6e 64  .  fts3Hash pend
2dbc2 69 6e 67 54 65 72 6d 73 3b 0a 7d 3b 0a 0a 2f 2a  ingTerms;.};../*
2dbc3 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 6f 72  .** When the cor
2dbc4 65 20 77 61 6e 74 73 20 74 6f 20 64 6f 20 61 20  e wants to do a 
2dbc5 71 75 65 72 79 2c 20 69 74 20 63 72 65 61 74 65  query, it create
2dbc6 20 61 20 63 75 72 73 6f 72 20 75 73 69 6e 67 20   a cursor using 
2dbc7 61 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 78 4f 70  a.** call to xOp
2dbc8 65 6e 2e 20 20 54 68 69 73 20 73 74 72 75 63 74  en.  This struct
2dbc9 75 72 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e  ure is an instan
2dbca 63 65 20 6f 66 20 61 20 63 75 72 73 6f 72 2e 20  ce of a cursor. 
2dbcb 20 49 74 0a 2a 2a 20 69 73 20 64 65 73 74 72 6f   It.** is destro
2dbcc 79 65 64 20 62 79 20 78 43 6c 6f 73 65 2e 0a 2a  yed by xClose..*
2dbcd 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
2dbce 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72   fulltext_cursor
2dbcf 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61   {.  sqlite3_vta
2dbd0 62 5f 63 75 72 73 6f 72 20 62 61 73 65 3b 20 20  b_cursor base;  
2dbd1 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 63 6c        /* Base cl
2dbd2 61 73 73 20 75 73 65 64 20 62 79 20 53 51 4c 69  ass used by SQLi
2dbd3 74 65 20 63 6f 72 65 20 2a 2f 0a 20 20 51 75 65  te core */.  Que
2dbd4 72 79 54 79 70 65 20 69 43 75 72 73 6f 72 54 79  ryType iCursorTy
2dbd5 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  pe;           /*
2dbd6 20 43 6f 70 79 20 6f 66 20 73 71 6c 69 74 65 33   Copy of sqlite3
2dbd7 5f 69 6e 64 65 78 5f 69 6e 66 6f 2e 69 64 78 4e  _index_info.idxN
2dbd8 75 6d 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  um */.  sqlite3_
2dbd9 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20 20  stmt *pStmt;    
2dbda 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 70           /* Prep
2dbdb 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 69  ared statement i
2dbdc 6e 20 75 73 65 20 62 79 20 74 68 65 20 63 75 72  n use by the cur
2dbdd 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 65 6f 66  sor */.  int eof
2dbde 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2dbdf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
2dbe0 65 20 69 66 20 61 74 20 45 6e 64 20 4f 66 20 52  e if at End Of R
2dbe1 65 73 75 6c 74 73 20 2a 2f 0a 20 20 51 75 65 72  esults */.  Quer
2dbe2 79 20 71 3b 20 20 20 20 20 20 20 20 20 20 20 20  y q;            
2dbe3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2dbe4 50 61 72 73 65 64 20 71 75 65 72 79 20 73 74 72  Parsed query str
2dbe5 69 6e 67 20 2a 2f 0a 20 20 53 6e 69 70 70 65 74  ing */.  Snippet
2dbe6 20 73 6e 69 70 70 65 74 3b 20 20 20 20 20 20 20   snippet;       
2dbe7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63            /* Cac
2dbe8 68 65 64 20 73 6e 69 70 70 65 74 20 66 6f 72 20  hed snippet for 
2dbe9 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20  the current row 
2dbea 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  */.  int iColumn
2dbeb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2dbec 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20        /* Column 
2dbed 62 65 69 6e 67 20 73 65 61 72 63 68 65 64 20 2a  being searched *
2dbee 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 72  /.  DataBuffer r
2dbef 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20 20 20  esult;          
2dbf0 20 20 20 20 20 2f 2a 20 44 6f 63 6c 69 73 74 20       /* Doclist 
2dbf1 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 66 75 6c  results from ful
2dbf2 6c 74 65 78 74 51 75 65 72 79 20 2a 2f 0a 20 20  ltextQuery */.  
2dbf3 44 4c 52 65 61 64 65 72 20 72 65 61 64 65 72 3b  DLReader reader;
2dbf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dbf5 20 2f 2a 20 52 65 73 75 6c 74 20 72 65 61 64 65   /* Result reade
2dbf6 72 20 69 66 20 72 65 73 75 6c 74 20 6e 6f 74 20  r if result not 
2dbf7 65 6d 70 74 79 20 2a 2f 0a 7d 20 66 75 6c 6c 74  empty */.} fullt
2dbf8 65 78 74 5f 63 75 72 73 6f 72 3b 0a 0a 73 74 61  ext_cursor;..sta
2dbf9 74 69 63 20 73 74 72 75 63 74 20 66 75 6c 6c 74  tic struct fullt
2dbfa 65 78 74 5f 76 74 61 62 20 2a 63 75 72 73 6f 72  ext_vtab *cursor
2dbfb 5f 76 74 61 62 28 66 75 6c 6c 74 65 78 74 5f 63  _vtab(fulltext_c
2dbfc 75 72 73 6f 72 20 2a 63 29 7b 0a 20 20 72 65 74  ursor *c){.  ret
2dbfd 75 72 6e 20 28 66 75 6c 6c 74 65 78 74 5f 76 74  urn (fulltext_vt
2dbfe 61 62 20 2a 29 20 63 2d 3e 62 61 73 65 2e 70 56  ab *) c->base.pV
2dbff 74 61 62 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 63  tab;.}..static c
2dc00 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
2dc01 75 6c 65 20 66 74 73 33 4d 6f 64 75 6c 65 3b 20  ule fts3Module; 
2dc02 20 20 2f 2a 20 66 6f 72 77 61 72 64 20 64 65 63    /* forward dec
2dc03 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 20  laration */../* 
2dc04 52 65 74 75 72 6e 20 61 20 64 79 6e 61 6d 69 63  Return a dynamic
2dc05 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20 73  ally generated s
2dc06 74 61 74 65 6d 65 6e 74 20 6f 66 20 74 68 65 20  tatement of the 
2dc07 66 6f 72 6d 0a 20 2a 20 20 20 69 6e 73 65 72 74  form. *   insert
2dc08 20 69 6e 74 6f 20 25 5f 63 6f 6e 74 65 6e 74 20   into %_content 
2dc09 28 64 6f 63 69 64 2c 20 2e 2e 2e 29 20 76 61 6c  (docid, ...) val
2dc0a 75 65 73 20 28 3f 2c 20 2e 2e 2e 29 0a 20 2a 2f  ues (?, ...). */
2dc0b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68  .static const ch
2dc0c 61 72 20 2a 63 6f 6e 74 65 6e 74 49 6e 73 65 72  ar *contentInser
2dc0d 74 53 74 61 74 65 6d 65 6e 74 28 66 75 6c 6c 74  tStatement(fullt
2dc0e 65 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20  ext_vtab *v){.  
2dc0f 53 74 72 69 6e 67 42 75 66 66 65 72 20 73 62 3b  StringBuffer sb;
2dc10 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 69 6e 69  .  int i;..  ini
2dc11 74 53 74 72 69 6e 67 42 75 66 66 65 72 28 26 73  tStringBuffer(&s
2dc12 62 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 62  b);.  append(&sb
2dc13 2c 20 22 69 6e 73 65 72 74 20 69 6e 74 6f 20 25  , "insert into %
2dc14 5f 63 6f 6e 74 65 6e 74 20 28 64 6f 63 69 64 2c  _content (docid,
2dc15 20 22 29 3b 0a 20 20 61 70 70 65 6e 64 4c 69 73   ");.  appendLis
2dc16 74 28 26 73 62 2c 20 76 2d 3e 6e 43 6f 6c 75 6d  t(&sb, v->nColum
2dc17 6e 2c 20 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43  n, v->azContentC
2dc18 6f 6c 75 6d 6e 29 3b 0a 20 20 61 70 70 65 6e 64  olumn);.  append
2dc19 28 26 73 62 2c 20 22 29 20 76 61 6c 75 65 73 20  (&sb, ") values 
2dc1a 28 3f 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  (?");.  for(i=0;
2dc1b 20 69 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b   i<v->nColumn; +
2dc1c 2b 69 29 0a 20 20 20 20 61 70 70 65 6e 64 28 26  +i).    append(&
2dc1d 73 62 2c 20 22 2c 20 3f 22 29 3b 0a 20 20 61 70  sb, ", ?");.  ap
2dc1e 70 65 6e 64 28 26 73 62 2c 20 22 29 22 29 3b 0a  pend(&sb, ")");.
2dc1f 20 20 72 65 74 75 72 6e 20 73 74 72 69 6e 67 42    return stringB
2dc20 75 66 66 65 72 44 61 74 61 28 26 73 62 29 3b 0a  ufferData(&sb);.
2dc21 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 61 20 64  }../* Return a d
2dc22 79 6e 61 6d 69 63 61 6c 6c 79 20 67 65 6e 65 72  ynamically gener
2dc23 61 74 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6f  ated statement o
2dc24 66 20 74 68 65 20 66 6f 72 6d 0a 20 2a 20 20 20  f the form. *   
2dc25 73 65 6c 65 63 74 20 3c 63 6f 6e 74 65 6e 74 20  select <content 
2dc26 63 6f 6c 75 6d 6e 73 3e 20 66 72 6f 6d 20 25 5f  columns> from %_
2dc27 63 6f 6e 74 65 6e 74 20 77 68 65 72 65 20 64 6f  content where do
2dc28 63 69 64 20 3d 20 3f 0a 20 2a 2f 0a 73 74 61 74  cid = ?. */.stat
2dc29 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  ic const char *c
2dc2a 6f 6e 74 65 6e 74 53 65 6c 65 63 74 53 74 61 74  ontentSelectStat
2dc2b 65 6d 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76  ement(fulltext_v
2dc2c 74 61 62 20 2a 76 29 7b 0a 20 20 53 74 72 69 6e  tab *v){.  Strin
2dc2d 67 42 75 66 66 65 72 20 73 62 3b 0a 20 20 69 6e  gBuffer sb;.  in
2dc2e 69 74 53 74 72 69 6e 67 42 75 66 66 65 72 28 26  itStringBuffer(&
2dc2f 73 62 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73  sb);.  append(&s
2dc30 62 2c 20 22 53 45 4c 45 43 54 20 22 29 3b 0a 20  b, "SELECT ");. 
2dc31 20 61 70 70 65 6e 64 4c 69 73 74 28 26 73 62 2c   appendList(&sb,
2dc32 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 76 2d 3e   v->nColumn, v->
2dc33 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29  azContentColumn)
2dc34 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20  ;.  append(&sb, 
2dc35 22 20 46 52 4f 4d 20 25 5f 63 6f 6e 74 65 6e 74  " FROM %_content
2dc36 20 57 48 45 52 45 20 64 6f 63 69 64 20 3d 20 3f   WHERE docid = ?
2dc37 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 74 72  ");.  return str
2dc38 69 6e 67 42 75 66 66 65 72 44 61 74 61 28 26 73  ingBufferData(&s
2dc39 62 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e  b);.}../* Return
2dc3a 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 67   a dynamically g
2dc3b 65 6e 65 72 61 74 65 64 20 73 74 61 74 65 6d 65  enerated stateme
2dc3c 6e 74 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 20  nt of the form. 
2dc3d 2a 20 20 20 75 70 64 61 74 65 20 25 5f 63 6f 6e  *   update %_con
2dc3e 74 65 6e 74 20 73 65 74 20 5b 63 6f 6c 5f 30 5d  tent set [col_0]
2dc3f 20 3d 20 3f 2c 20 5b 63 6f 6c 5f 31 5d 20 3d 20   = ?, [col_1] = 
2dc40 3f 2c 20 2e 2e 2e 0a 20 2a 20 20 20 20 20 20 20  ?, .... *       
2dc41 20 20 20 20 20 20 20 20 20 20 20 20 20 77 68 65               whe
2dc42 72 65 20 64 6f 63 69 64 20 3d 20 3f 0a 20 2a 2f  re docid = ?. */
2dc43 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68  .static const ch
2dc44 61 72 20 2a 63 6f 6e 74 65 6e 74 55 70 64 61 74  ar *contentUpdat
2dc45 65 53 74 61 74 65 6d 65 6e 74 28 66 75 6c 6c 74  eStatement(fullt
2dc46 65 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20  ext_vtab *v){.  
2dc47 53 74 72 69 6e 67 42 75 66 66 65 72 20 73 62 3b  StringBuffer sb;
2dc48 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 69 6e 69  .  int i;..  ini
2dc49 74 53 74 72 69 6e 67 42 75 66 66 65 72 28 26 73  tStringBuffer(&s
2dc4a 62 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 62  b);.  append(&sb
2dc4b 2c 20 22 75 70 64 61 74 65 20 25 5f 63 6f 6e 74  , "update %_cont
2dc4c 65 6e 74 20 73 65 74 20 22 29 3b 0a 20 20 66 6f  ent set ");.  fo
2dc4d 72 28 69 3d 30 3b 20 69 3c 76 2d 3e 6e 43 6f 6c  r(i=0; i<v->nCol
2dc4e 75 6d 6e 3b 20 2b 2b 69 29 20 7b 0a 20 20 20 20  umn; ++i) {.    
2dc4f 69 66 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 20  if( i>0 ){.     
2dc50 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 2c 20   append(&sb, ", 
2dc51 22 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 70  ");.    }.    ap
2dc52 70 65 6e 64 28 26 73 62 2c 20 76 2d 3e 61 7a 43  pend(&sb, v->azC
2dc53 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 5b 69 5d 29  ontentColumn[i])
2dc54 3b 0a 20 20 20 20 61 70 70 65 6e 64 28 26 73 62  ;.    append(&sb
2dc55 2c 20 22 20 3d 20 3f 22 29 3b 0a 20 20 7d 0a 20  , " = ?");.  }. 
2dc56 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 20 77   append(&sb, " w
2dc57 68 65 72 65 20 64 6f 63 69 64 20 3d 20 3f 22 29  here docid = ?")
2dc58 3b 0a 20 20 72 65 74 75 72 6e 20 73 74 72 69 6e  ;.  return strin
2dc59 67 42 75 66 66 65 72 44 61 74 61 28 26 73 62 29  gBufferData(&sb)
2dc5a 3b 0a 7d 0a 0a 2f 2a 20 50 75 74 73 20 61 20 66  ;.}../* Puts a f
2dc5b 72 65 73 68 6c 79 2d 70 72 65 70 61 72 65 64 20  reshly-prepared 
2dc5c 73 74 61 74 65 6d 65 6e 74 20 64 65 74 65 72 6d  statement determ
2dc5d 69 6e 65 64 20 62 79 20 69 53 74 6d 74 20 69 6e  ined by iStmt in
2dc5e 20 2a 70 70 53 74 6d 74 2e 0a 2a 2a 20 49 66 20   *ppStmt..** If 
2dc5f 74 68 65 20 69 6e 64 69 63 61 74 65 64 20 73 74  the indicated st
2dc60 61 74 65 6d 65 6e 74 20 68 61 73 20 6e 65 76 65  atement has neve
2dc61 72 20 62 65 65 6e 20 70 72 65 70 61 72 65 64 2c  r been prepared,
2dc62 20 69 74 20 69 73 20 70 72 65 70 61 72 65 64 0a   it is prepared.
2dc63 2a 2a 20 61 6e 64 20 63 61 63 68 65 64 2c 20 6f  ** and cached, o
2dc64 74 68 65 72 77 69 73 65 20 74 68 65 20 63 61 63  therwise the cac
2dc65 68 65 64 20 76 65 72 73 69 6f 6e 20 69 73 20 72  hed version is r
2dc66 65 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  eset..*/.static 
2dc67 69 6e 74 20 73 71 6c 5f 67 65 74 5f 73 74 61 74  int sql_get_stat
2dc68 65 6d 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76  ement(fulltext_v
2dc69 74 61 62 20 2a 76 2c 20 66 75 6c 6c 74 65 78 74  tab *v, fulltext
2dc6a 5f 73 74 61 74 65 6d 65 6e 74 20 69 53 74 6d 74  _statement iStmt
2dc6b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2dc6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
2dc6d 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70  qlite3_stmt **pp
2dc6e 53 74 6d 74 29 7b 0a 20 20 61 73 73 65 72 74 28  Stmt){.  assert(
2dc6f 20 69 53 74 6d 74 3c 4d 41 58 5f 53 54 4d 54 20   iStmt<MAX_STMT 
2dc70 29 3b 0a 20 20 69 66 28 20 76 2d 3e 70 46 75 6c  );.  if( v->pFul
2dc71 6c 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73 5b  ltextStatements[
2dc72 69 53 74 6d 74 5d 3d 3d 4e 55 4c 4c 20 29 7b 0a  iStmt]==NULL ){.
2dc73 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2dc74 7a 53 74 6d 74 3b 0a 20 20 20 20 69 6e 74 20 72  zStmt;.    int r
2dc75 63 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 69  c;.    switch( i
2dc76 53 74 6d 74 20 29 7b 0a 20 20 20 20 20 20 63 61  Stmt ){.      ca
2dc77 73 65 20 43 4f 4e 54 45 4e 54 5f 49 4e 53 45 52  se CONTENT_INSER
2dc78 54 5f 53 54 4d 54 3a 0a 20 20 20 20 20 20 20 20  T_STMT:.        
2dc79 7a 53 74 6d 74 20 3d 20 63 6f 6e 74 65 6e 74 49  zStmt = contentI
2dc7a 6e 73 65 72 74 53 74 61 74 65 6d 65 6e 74 28 76  nsertStatement(v
2dc7b 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  ); break;.      
2dc7c 63 61 73 65 20 43 4f 4e 54 45 4e 54 5f 53 45 4c  case CONTENT_SEL
2dc7d 45 43 54 5f 53 54 4d 54 3a 0a 20 20 20 20 20 20  ECT_STMT:.      
2dc7e 20 20 7a 53 74 6d 74 20 3d 20 63 6f 6e 74 65 6e    zStmt = conten
2dc7f 74 53 65 6c 65 63 74 53 74 61 74 65 6d 65 6e 74  tSelectStatement
2dc80 28 76 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20  (v); break;.    
2dc81 20 20 63 61 73 65 20 43 4f 4e 54 45 4e 54 5f 55    case CONTENT_U
2dc82 50 44 41 54 45 5f 53 54 4d 54 3a 0a 20 20 20 20  PDATE_STMT:.    
2dc83 20 20 20 20 7a 53 74 6d 74 20 3d 20 63 6f 6e 74      zStmt = cont
2dc84 65 6e 74 55 70 64 61 74 65 53 74 61 74 65 6d 65  entUpdateStateme
2dc85 6e 74 28 76 29 3b 20 62 72 65 61 6b 3b 0a 20 20  nt(v); break;.  
2dc86 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20      default:.   
2dc87 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 66 75 6c       zStmt = ful
2dc88 6c 74 65 78 74 5f 7a 53 74 61 74 65 6d 65 6e 74  ltext_zStatement
2dc89 5b 69 53 74 6d 74 5d 3b 0a 20 20 20 20 7d 0a 20  [iStmt];.    }. 
2dc8a 20 20 20 72 63 20 3d 20 73 71 6c 5f 70 72 65 70     rc = sql_prep
2dc8b 61 72 65 28 76 2d 3e 64 62 2c 20 76 2d 3e 7a 44  are(v->db, v->zD
2dc8c 62 2c 20 76 2d 3e 7a 4e 61 6d 65 2c 20 26 76 2d  b, v->zName, &v-
2dc8d 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d  >pFulltextStatem
2dc8e 65 6e 74 73 5b 69 53 74 6d 74 5d 2c 0a 20 20 20  ents[iStmt],.   
2dc8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dc90 20 20 20 20 20 20 7a 53 74 6d 74 29 3b 0a 20 20        zStmt);.  
2dc91 20 20 69 66 28 20 7a 53 74 6d 74 20 21 3d 20 66    if( zStmt != f
2dc92 75 6c 6c 74 65 78 74 5f 7a 53 74 61 74 65 6d 65  ulltext_zStateme
2dc93 6e 74 5b 69 53 74 6d 74 5d 29 20 73 71 6c 69 74  nt[iStmt]) sqlit
2dc94 65 33 5f 66 72 65 65 28 28 76 6f 69 64 20 2a 29  e3_free((void *)
2dc95 20 7a 53 74 6d 74 29 3b 0a 20 20 20 20 69 66 28   zStmt);.    if(
2dc96 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2dc97 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20   return rc;.  } 
2dc98 65 6c 73 65 20 7b 0a 20 20 20 20 69 6e 74 20 72  else {.    int r
2dc99 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65  c = sqlite3_rese
2dc9a 74 28 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74  t(v->pFulltextSt
2dc9b 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d 74 5d 29  atements[iStmt])
2dc9c 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
2dc9d 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
2dc9e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 53   rc;.  }..  *ppS
2dc9f 74 6d 74 20 3d 20 76 2d 3e 70 46 75 6c 6c 74 65  tmt = v->pFullte
2dca0 78 74 53 74 61 74 65 6d 65 6e 74 73 5b 69 53 74  xtStatements[iSt
2dca1 6d 74 5d 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  mt];.  return SQ
2dca2 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4c  LITE_OK;.}../* L
2dca3 69 6b 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70  ike sqlite3_step
2dca4 28 29 2c 20 62 75 74 20 63 6f 6e 76 65 72 74 20  (), but convert 
2dca5 53 51 4c 49 54 45 5f 44 4f 4e 45 20 74 6f 20 53  SQLITE_DONE to S
2dca6 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 0a 2a 2a 20  QLITE_OK and.** 
2dca7 53 51 4c 49 54 45 5f 52 4f 57 20 74 6f 20 53 51  SQLITE_ROW to SQ
2dca8 4c 49 54 45 5f 45 52 52 4f 52 2e 20 20 55 73 65  LITE_ERROR.  Use
2dca9 66 75 6c 20 66 6f 72 20 73 74 61 74 65 6d 65 6e  ful for statemen
2dcaa 74 73 20 6c 69 6b 65 20 55 50 44 41 54 45 2c 0a  ts like UPDATE,.
2dcab 2a 2a 20 77 68 65 72 65 20 77 65 20 65 78 70 65  ** where we expe
2dcac 63 74 20 6e 6f 20 72 65 73 75 6c 74 73 2e 0a 2a  ct no results..*
2dcad 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  /.static int sql
2dcae 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73 71 6c  _single_step(sql
2dcaf 69 74 65 33 5f 73 74 6d 74 20 2a 73 29 7b 0a 20  ite3_stmt *s){. 
2dcb0 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65   int rc = sqlite
2dcb1 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 72 65 74  3_step(s);.  ret
2dcb2 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f  urn (rc==SQLITE_
2dcb3 44 4f 4e 45 29 20 3f 20 53 51 4c 49 54 45 5f 4f  DONE) ? SQLITE_O
2dcb4 4b 20 3a 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4c 69  K : rc;.}../* Li
2dcb5 6b 65 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65  ke sql_get_state
2dcb6 6d 65 6e 74 28 29 2c 20 62 75 74 20 66 6f 72 20  ment(), but for 
2dcb7 73 70 65 63 69 61 6c 20 72 65 70 6c 69 63 61 74  special replicat
2dcb8 65 64 20 4c 45 41 46 5f 53 45 4c 45 43 54 0a 2a  ed LEAF_SELECT.*
2dcb9 2a 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 69  * statements.  i
2dcba 64 78 20 2d 31 20 69 73 20 61 20 73 70 65 63 69  dx -1 is a speci
2dcbb 61 6c 20 63 61 73 65 20 66 6f 72 20 61 6e 20 75  al case for an u
2dcbc 6e 63 61 63 68 65 64 20 76 65 72 73 69 6f 6e 20  ncached version 
2dcbd 6f 66 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 6d  of.** the statem
2dcbe 65 6e 74 20 28 75 73 65 64 20 69 6e 20 74 68 65  ent (used in the
2dcbf 20 6f 70 74 69 6d 69 7a 65 20 69 6d 70 6c 65 6d   optimize implem
2dcc0 65 6e 74 61 74 69 6f 6e 29 2e 0a 2a 2f 0a 2f 2a  entation)..*/./*
2dcc1 20 54 4f 44 4f 28 73 68 65 73 73 29 20 57 72 69   TODO(shess) Wri
2dcc2 74 65 20 76 65 72 73 69 6f 6e 20 66 6f 72 20 67  te version for g
2dcc3 65 6e 65 72 69 63 20 73 74 61 74 65 6d 65 6e 74  eneric statement
2dcc4 73 20 61 6e 64 20 74 68 65 6e 20 73 68 61 72 65  s and then share
2dcc5 0a 2a 2a 20 74 68 61 74 20 62 65 74 77 65 65 6e  .** that between
2dcc6 20 74 68 65 20 63 61 63 68 65 64 2d 73 74 61 74   the cached-stat
2dcc7 65 6d 65 6e 74 20 66 75 6e 63 74 69 6f 6e 73 2e  ement functions.
2dcc8 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
2dcc9 71 6c 5f 67 65 74 5f 6c 65 61 66 5f 73 74 61 74  ql_get_leaf_stat
2dcca 65 6d 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76  ement(fulltext_v
2dccb 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 64 78 2c  tab *v, int idx,
2dccc 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2dccd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dcce 20 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20     sqlite3_stmt 
2dccf 2a 2a 70 70 53 74 6d 74 29 7b 0a 20 20 61 73 73  **ppStmt){.  ass
2dcd0 65 72 74 28 20 69 64 78 3e 3d 2d 31 20 26 26 20  ert( idx>=-1 && 
2dcd1 69 64 78 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 20  idx<MERGE_COUNT 
2dcd2 29 3b 0a 20 20 69 66 28 20 69 64 78 3d 3d 2d 31  );.  if( idx==-1
2dcd3 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73   ){.    return s
2dcd4 71 6c 5f 70 72 65 70 61 72 65 28 76 2d 3e 64 62  ql_prepare(v->db
2dcd5 2c 20 76 2d 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61  , v->zDb, v->zNa
2dcd6 6d 65 2c 20 70 70 53 74 6d 74 2c 20 4c 45 41 46  me, ppStmt, LEAF
2dcd7 5f 53 45 4c 45 43 54 29 3b 0a 20 20 7d 65 6c 73  _SELECT);.  }els
2dcd8 65 20 69 66 28 20 76 2d 3e 70 4c 65 61 66 53 65  e if( v->pLeafSe
2dcd9 6c 65 63 74 53 74 6d 74 73 5b 69 64 78 5d 3d 3d  lectStmts[idx]==
2dcda 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 69 6e 74 20  NULL ){.    int 
2dcdb 72 63 20 3d 20 73 71 6c 5f 70 72 65 70 61 72 65  rc = sql_prepare
2dcdc 28 76 2d 3e 64 62 2c 20 76 2d 3e 7a 44 62 2c 20  (v->db, v->zDb, 
2dcdd 76 2d 3e 7a 4e 61 6d 65 2c 20 26 76 2d 3e 70 4c  v->zName, &v->pL
2dcde 65 61 66 53 65 6c 65 63 74 53 74 6d 74 73 5b 69  eafSelectStmts[i
2dcdf 64 78 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20  dx],.           
2dce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4c 45                LE
2dce1 41 46 5f 53 45 4c 45 43 54 29 3b 0a 20 20 20 20  AF_SELECT);.    
2dce2 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2dce3 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
2dce4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20   }else{.    int 
2dce5 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73  rc = sqlite3_res
2dce6 65 74 28 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63  et(v->pLeafSelec
2dce7 74 53 74 6d 74 73 5b 69 64 78 5d 29 3b 0a 20 20  tStmts[idx]);.  
2dce8 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2dce9 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
2dcea 0a 20 20 7d 0a 0a 20 20 2a 70 70 53 74 6d 74 20  .  }..  *ppStmt 
2dceb 3d 20 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74  = v->pLeafSelect
2dcec 53 74 6d 74 73 5b 69 64 78 5d 3b 0a 20 20 72 65  Stmts[idx];.  re
2dced 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
2dcee 7d 0a 0a 2f 2a 20 69 6e 73 65 72 74 20 69 6e 74  }../* insert int
2dcef 6f 20 25 5f 63 6f 6e 74 65 6e 74 20 28 64 6f 63  o %_content (doc
2dcf0 69 64 2c 20 2e 2e 2e 29 20 76 61 6c 75 65 73 20  id, ...) values 
2dcf1 28 5b 64 6f 63 69 64 5d 2c 20 5b 70 56 61 6c 75  ([docid], [pValu
2dcf2 65 73 5d 29 0a 2a 2a 20 49 66 20 74 68 65 20 64  es]).** If the d
2dcf3 6f 63 69 64 20 63 6f 6e 74 61 69 6e 73 20 53 51  ocid contains SQ
2dcf4 4c 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 20 75  L NULL, then a u
2dcf5 6e 69 71 75 65 20 64 6f 63 69 64 20 77 69 6c 6c  nique docid will
2dcf6 20 62 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 64   be.** generated
2dcf7 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2dcf8 63 6f 6e 74 65 6e 74 5f 69 6e 73 65 72 74 28 66  content_insert(f
2dcf9 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
2dcfa 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2dcfb 64 6f 63 69 64 2c 0a 20 20 20 20 20 20 20 20 20  docid,.         
2dcfc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dcfd 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2dcfe 2a 70 56 61 6c 75 65 73 29 7b 0a 20 20 73 71 6c  *pValues){.  sql
2dcff 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20  ite3_stmt *s;.  
2dd00 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20  int i;.  int rc 
2dd01 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d  = sql_get_statem
2dd02 65 6e 74 28 76 2c 20 43 4f 4e 54 45 4e 54 5f 49  ent(v, CONTENT_I
2dd03 4e 53 45 52 54 5f 53 54 4d 54 2c 20 26 73 29 3b  NSERT_STMT, &s);
2dd04 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
2dd05 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
2dd06 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
2dd07 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 2c 20  3_bind_value(s, 
2dd08 31 2c 20 64 6f 63 69 64 29 3b 0a 20 20 69 66 28  1, docid);.  if(
2dd09 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2dd0a 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 66   return rc;..  f
2dd0b 6f 72 28 69 3d 30 3b 20 69 3c 76 2d 3e 6e 43 6f  or(i=0; i<v->nCo
2dd0c 6c 75 6d 6e 3b 20 2b 2b 69 29 7b 0a 20 20 20 20  lumn; ++i){.    
2dd0d 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e  rc = sqlite3_bin
2dd0e 64 5f 76 61 6c 75 65 28 73 2c 20 32 2b 69 2c 20  d_value(s, 2+i, 
2dd0f 70 56 61 6c 75 65 73 5b 69 5d 29 3b 0a 20 20 20  pValues[i]);.   
2dd10 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2dd11 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
2dd12 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 73 71    }..  return sq
2dd13 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73 29  l_single_step(s)
2dd14 3b 0a 7d 0a 0a 2f 2a 20 75 70 64 61 74 65 20 25  ;.}../* update %
2dd15 5f 63 6f 6e 74 65 6e 74 20 73 65 74 20 63 6f 6c  _content set col
2dd16 30 20 3d 20 70 56 61 6c 75 65 73 5b 30 5d 2c 20  0 = pValues[0], 
2dd17 63 6f 6c 31 20 3d 20 70 56 61 6c 75 65 73 5b 31  col1 = pValues[1
2dd18 5d 2c 20 2e 2e 2e 0a 20 2a 20 20 20 20 20 20 20  ], .... *       
2dd19 20 20 20 20 20 20 20 20 20 20 20 77 68 65 72 65             where
2dd1a 20 64 6f 63 69 64 20 3d 20 5b 69 44 6f 63 69 64   docid = [iDocid
2dd1b 5d 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ] */.static int 
2dd1c 63 6f 6e 74 65 6e 74 5f 75 70 64 61 74 65 28 66  content_update(f
2dd1d 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
2dd1e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2dd1f 2a 70 56 61 6c 75 65 73 2c 0a 20 20 20 20 20 20  *pValues,.      
2dd20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dd21 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
2dd22 20 69 44 6f 63 69 64 29 7b 0a 20 20 73 71 6c 69   iDocid){.  sqli
2dd23 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69  te3_stmt *s;.  i
2dd24 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d  nt i;.  int rc =
2dd25 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65   sql_get_stateme
2dd26 6e 74 28 76 2c 20 43 4f 4e 54 45 4e 54 5f 55 50  nt(v, CONTENT_UP
2dd27 44 41 54 45 5f 53 54 4d 54 2c 20 26 73 29 3b 0a  DATE_STMT, &s);.
2dd28 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2dd29 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
2dd2a 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 76  ..  for(i=0; i<v
2dd2b 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 7b  ->nColumn; ++i){
2dd2c 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
2dd2d 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 2c 20  3_bind_value(s, 
2dd2e 31 2b 69 2c 20 70 56 61 6c 75 65 73 5b 69 5d 29  1+i, pValues[i])
2dd2f 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
2dd30 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
2dd31 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d   rc;.  }..  rc =
2dd32 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
2dd33 74 36 34 28 73 2c 20 31 2b 76 2d 3e 6e 43 6f 6c  t64(s, 1+v->nCol
2dd34 75 6d 6e 2c 20 69 44 6f 63 69 64 29 3b 0a 20 20  umn, iDocid);.  
2dd35 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2dd36 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
2dd37 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 73 69 6e    return sql_sin
2dd38 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d 0a 0a  gle_step(s);.}..
2dd39 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65  static void free
2dd3a 53 74 72 69 6e 67 41 72 72 61 79 28 69 6e 74 20  StringArray(int 
2dd3b 6e 53 74 72 69 6e 67 2c 20 63 6f 6e 73 74 20 63  nString, const c
2dd3c 68 61 72 20 2a 2a 70 53 74 72 69 6e 67 29 7b 0a  har **pString){.
2dd3d 20 20 69 6e 74 20 69 3b 0a 0a 20 20 66 6f 72 20    int i;..  for 
2dd3e 28 69 3d 30 20 3b 20 69 20 3c 20 6e 53 74 72 69  (i=0 ; i < nStri
2dd3f 6e 67 20 3b 20 2b 2b 69 29 20 7b 0a 20 20 20 20  ng ; ++i) {.    
2dd40 69 66 28 20 70 53 74 72 69 6e 67 5b 69 5d 21 3d  if( pString[i]!=
2dd41 4e 55 4c 4c 20 29 20 73 71 6c 69 74 65 33 5f 66  NULL ) sqlite3_f
2dd42 72 65 65 28 28 76 6f 69 64 20 2a 29 20 70 53 74  ree((void *) pSt
2dd43 72 69 6e 67 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20  ring[i]);.  }.  
2dd44 73 71 6c 69 74 65 33 5f 66 72 65 65 28 28 76 6f  sqlite3_free((vo
2dd45 69 64 20 2a 29 20 70 53 74 72 69 6e 67 29 3b 0a  id *) pString);.
2dd46 7d 0a 0a 2f 2a 20 73 65 6c 65 63 74 20 2a 20 66  }../* select * f
2dd47 72 6f 6d 20 25 5f 63 6f 6e 74 65 6e 74 20 77 68  rom %_content wh
2dd48 65 72 65 20 64 6f 63 69 64 20 3d 20 5b 69 44 6f  ere docid = [iDo
2dd49 63 69 64 5d 0a 20 2a 20 54 68 65 20 63 61 6c 6c  cid]. * The call
2dd4a 65 72 20 6d 75 73 74 20 64 65 6c 65 74 65 20 74  er must delete t
2dd4b 68 65 20 72 65 74 75 72 6e 65 64 20 61 72 72 61  he returned arra
2dd4c 79 20 61 6e 64 20 61 6c 6c 20 73 74 72 69 6e 67  y and all string
2dd4d 73 20 69 6e 20 69 74 2e 0a 20 2a 20 6e 75 6c 6c  s in it.. * null
2dd4e 20 66 69 65 6c 64 73 20 77 69 6c 6c 20 62 65 20   fields will be 
2dd4f 4e 55 4c 4c 20 69 6e 20 74 68 65 20 72 65 74 75  NULL in the retu
2dd50 72 6e 65 64 20 61 72 72 61 79 2e 0a 20 2a 0a 20  rned array.. *. 
2dd51 2a 20 54 4f 44 4f 3a 20 50 65 72 68 61 70 73 20  * TODO: Perhaps 
2dd52 77 65 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e  we should return
2dd53 20 70 6f 69 6e 74 65 72 2f 6c 65 6e 67 74 68 20   pointer/length 
2dd54 73 74 72 69 6e 67 73 20 68 65 72 65 20 66 6f 72  strings here for
2dd55 20 63 6f 6e 73 69 73 74 65 6e 63 79 0a 20 2a 20   consistency. * 
2dd56 77 69 74 68 20 6f 74 68 65 72 20 63 6f 64 65 20  with other code 
2dd57 77 68 69 63 68 20 75 73 65 73 20 70 6f 69 6e 74  which uses point
2dd58 65 72 2f 6c 65 6e 67 74 68 2e 20 2a 2f 0a 73 74  er/length. */.st
2dd59 61 74 69 63 20 69 6e 74 20 63 6f 6e 74 65 6e 74  atic int content
2dd5a 5f 73 65 6c 65 63 74 28 66 75 6c 6c 74 65 78 74  _select(fulltext
2dd5b 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65  _vtab *v, sqlite
2dd5c 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 2c 0a 20  _int64 iDocid,. 
2dd5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dd5e 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
2dd5f 68 61 72 20 2a 2a 2a 70 56 61 6c 75 65 73 29 7b  har ***pValues){
2dd60 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
2dd61 2a 73 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  *s;.  const char
2dd62 20 2a 2a 76 61 6c 75 65 73 3b 0a 20 20 69 6e 74   **values;.  int
2dd63 20 69 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20   i;.  int rc;.. 
2dd64 20 2a 70 56 61 6c 75 65 73 20 3d 20 4e 55 4c 4c   *pValues = NULL
2dd65 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 5f 67 65  ;..  rc = sql_ge
2dd66 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 43  t_statement(v, C
2dd67 4f 4e 54 45 4e 54 5f 53 45 4c 45 43 54 5f 53 54  ONTENT_SELECT_ST
2dd68 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72  MT, &s);.  if( r
2dd69 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
2dd6a 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
2dd6b 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  = sqlite3_bind_i
2dd6c 6e 74 36 34 28 73 2c 20 31 2c 20 69 44 6f 63 69  nt64(s, 1, iDoci
2dd6d 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  d);.  if( rc!=SQ
2dd6e 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
2dd6f 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
2dd70 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20  ite3_step(s);.  
2dd71 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52  if( rc!=SQLITE_R
2dd72 4f 57 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OW ) return rc;.
2dd73 0a 20 20 76 61 6c 75 65 73 20 3d 20 28 63 6f 6e  .  values = (con
2dd74 73 74 20 63 68 61 72 20 2a 2a 29 20 73 71 6c 69  st char **) sqli
2dd75 74 65 33 5f 6d 61 6c 6c 6f 63 28 76 2d 3e 6e 43  te3_malloc(v->nC
2dd76 6f 6c 75 6d 6e 20 2a 20 73 69 7a 65 6f 66 28 63  olumn * sizeof(c
2dd77 6f 6e 73 74 20 63 68 61 72 20 2a 29 29 3b 0a 20  onst char *));. 
2dd78 20 66 6f 72 28 69 3d 30 3b 20 69 3c 76 2d 3e 6e   for(i=0; i<v->n
2dd79 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 7b 0a 20 20  Column; ++i){.  
2dd7a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 6f    if( sqlite3_co
2dd7b 6c 75 6d 6e 5f 74 79 70 65 28 73 2c 20 69 29 3d  lumn_type(s, i)=
2dd7c 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a  =SQLITE_NULL ){.
2dd7d 20 20 20 20 20 20 76 61 6c 75 65 73 5b 69 5d 20        values[i] 
2dd7e 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 65 6c 73  = NULL;.    }els
2dd7f 65 7b 0a 20 20 20 20 20 20 76 61 6c 75 65 73 5b  e{.      values[
2dd80 69 5d 20 3d 20 73 74 72 69 6e 67 5f 64 75 70 28  i] = string_dup(
2dd81 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 63  (char*)sqlite3_c
2dd82 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 2c 20 69 29  olumn_text(s, i)
2dd83 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
2dd84 2f 2a 20 57 65 20 65 78 70 65 63 74 20 6f 6e 6c  /* We expect onl
2dd85 79 20 6f 6e 65 20 72 6f 77 2e 20 20 57 65 20 6d  y one row.  We m
2dd86 75 73 74 20 65 78 65 63 75 74 65 20 61 6e 6f 74  ust execute anot
2dd87 68 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70  her sqlite3_step
2dd88 28 29 0a 20 20 20 2a 20 74 6f 20 63 6f 6d 70 6c  ().   * to compl
2dd89 65 74 65 20 74 68 65 20 69 74 65 72 61 74 69 6f  ete the iteratio
2dd8a 6e 3b 20 6f 74 68 65 72 77 69 73 65 20 74 68 65  n; otherwise the
2dd8b 20 74 61 62 6c 65 20 77 69 6c 6c 20 72 65 6d 61   table will rema
2dd8c 69 6e 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20  in locked. */.  
2dd8d 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65  rc = sqlite3_ste
2dd8e 70 28 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  p(s);.  if( rc==
2dd8f 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20  SQLITE_DONE ){. 
2dd90 20 20 20 2a 70 56 61 6c 75 65 73 20 3d 20 76 61     *pValues = va
2dd91 6c 75 65 73 3b 0a 20 20 20 20 72 65 74 75 72 6e  lues;.    return
2dd92 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
2dd93 0a 20 20 66 72 65 65 53 74 72 69 6e 67 41 72 72  .  freeStringArr
2dd94 61 79 28 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 76  ay(v->nColumn, v
2dd95 61 6c 75 65 73 29 3b 0a 20 20 72 65 74 75 72 6e  alues);.  return
2dd96 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 64 65 6c 65 74   rc;.}../* delet
2dd97 65 20 66 72 6f 6d 20 25 5f 63 6f 6e 74 65 6e 74  e from %_content
2dd98 20 77 68 65 72 65 20 64 6f 63 69 64 20 3d 20 5b   where docid = [
2dd99 69 44 6f 63 69 64 20 5d 20 2a 2f 0a 73 74 61 74  iDocid ] */.stat
2dd9a 69 63 20 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 64  ic int content_d
2dd9b 65 6c 65 74 65 28 66 75 6c 6c 74 65 78 74 5f 76  elete(fulltext_v
2dd9c 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69  tab *v, sqlite_i
2dd9d 6e 74 36 34 20 69 44 6f 63 69 64 29 7b 0a 20 20  nt64 iDocid){.  
2dd9e 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b  sqlite3_stmt *s;
2dd9f 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f  .  int rc = sql_
2dda0 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c  get_statement(v,
2dda1 20 43 4f 4e 54 45 4e 54 5f 44 45 4c 45 54 45 5f   CONTENT_DELETE_
2dda2 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28  STMT, &s);.  if(
2dda3 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2dda4 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
2dda5 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  c = sqlite3_bind
2dda6 5f 69 6e 74 36 34 28 73 2c 20 31 2c 20 69 44 6f  _int64(s, 1, iDo
2dda7 63 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  cid);.  if( rc!=
2dda8 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
2dda9 72 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e  rn rc;..  return
2ddaa 20 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70   sql_single_step
2ddab 28 73 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72  (s);.}../* Retur
2ddac 6e 73 20 53 51 4c 49 54 45 5f 52 4f 57 20 69 66  ns SQLITE_ROW if
2ddad 20 61 6e 79 20 72 6f 77 73 20 65 78 69 73 74 20   any rows exist 
2ddae 69 6e 20 25 5f 63 6f 6e 74 65 6e 74 2c 20 53 51  in %_content, SQ
2ddaf 4c 49 54 45 5f 44 4f 4e 45 20 69 66 0a 2a 2a 20  LITE_DONE if.** 
2ddb0 6e 6f 20 72 6f 77 73 20 65 78 69 73 74 2c 20 61  no rows exist, a
2ddb1 6e 64 20 61 6e 79 20 65 72 72 6f 72 20 69 6e 20  nd any error in 
2ddb2 63 61 73 65 20 6f 66 20 66 61 69 6c 75 72 65 2e  case of failure.
2ddb3 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
2ddb4 6f 6e 74 65 6e 74 5f 65 78 69 73 74 73 28 66 75  ontent_exists(fu
2ddb5 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29 7b  lltext_vtab *v){
2ddb6 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
2ddb7 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73  *s;.  int rc = s
2ddb8 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
2ddb9 28 76 2c 20 43 4f 4e 54 45 4e 54 5f 45 58 49 53  (v, CONTENT_EXIS
2ddba 54 53 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20  TS_STMT, &s);.  
2ddbb 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2ddbc 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
2ddbd 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73    rc = sqlite3_s
2ddbe 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20 72 63  tep(s);.  if( rc
2ddbf 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72  !=SQLITE_ROW ) r
2ddc0 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20  eturn rc;..  /* 
2ddc1 57 65 20 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f  We expect only o
2ddc2 6e 65 20 72 6f 77 2e 20 20 57 65 20 6d 75 73 74  ne row.  We must
2ddc3 20 65 78 65 63 75 74 65 20 61 6e 6f 74 68 65 72   execute another
2ddc4 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0a   sqlite3_step().
2ddc5 20 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65     * to complete
2ddc6 20 74 68 65 20 69 74 65 72 61 74 69 6f 6e 3b 20   the iteration; 
2ddc7 6f 74 68 65 72 77 69 73 65 20 74 68 65 20 74 61  otherwise the ta
2ddc8 62 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20  ble will remain 
2ddc9 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63 20  locked. */.  rc 
2ddca 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73  = sqlite3_step(s
2ddcb 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
2ddcc 49 54 45 5f 44 4f 4e 45 20 29 20 72 65 74 75 72  ITE_DONE ) retur
2ddcd 6e 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 20 20  n SQLITE_ROW;.  
2ddce 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52  if( rc==SQLITE_R
2ddcf 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  OW ) return SQLI
2ddd0 54 45 5f 45 52 52 4f 52 3b 0a 20 20 72 65 74 75  TE_ERROR;.  retu
2ddd1 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 69 6e 73  rn rc;.}../* ins
2ddd2 65 72 74 20 69 6e 74 6f 20 25 5f 73 65 67 6d 65  ert into %_segme
2ddd3 6e 74 73 20 76 61 6c 75 65 73 20 28 5b 70 44 61  nts values ([pDa
2ddd4 74 61 5d 29 0a 2a 2a 20 20 20 72 65 74 75 72 6e  ta]).**   return
2ddd5 73 20 61 73 73 69 67 6e 65 64 20 62 6c 6f 63 6b  s assigned block
2ddd6 69 64 20 69 6e 20 2a 70 69 42 6c 6f 63 6b 69 64  id in *piBlockid
2ddd7 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62  .*/.static int b
2ddd8 6c 6f 63 6b 5f 69 6e 73 65 72 74 28 66 75 6c 6c  lock_insert(full
2ddd9 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 63 6f  text_vtab *v, co
2ddda 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c  nst char *pData,
2dddb 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20   int nData,.    
2dddc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dddd 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
2ddde 20 2a 70 69 42 6c 6f 63 6b 69 64 29 7b 0a 20 20   *piBlockid){.  
2dddf 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b  sqlite3_stmt *s;
2dde0 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f  .  int rc = sql_
2dde1 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c  get_statement(v,
2dde2 20 42 4c 4f 43 4b 5f 49 4e 53 45 52 54 5f 53 54   BLOCK_INSERT_ST
2dde3 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72  MT, &s);.  if( r
2dde4 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
2dde5 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
2dde6 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62  = sqlite3_bind_b
2dde7 6c 6f 62 28 73 2c 20 31 2c 20 70 44 61 74 61 2c  lob(s, 1, pData,
2dde8 20 6e 44 61 74 61 2c 20 53 51 4c 49 54 45 5f 53   nData, SQLITE_S
2dde9 54 41 54 49 43 29 3b 0a 20 20 69 66 28 20 72 63  TATIC);.  if( rc
2ddea 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
2ddeb 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
2ddec 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29   sqlite3_step(s)
2dded 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
2ddee 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20  TE_ROW ) return 
2ddef 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
2ddf0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44  if( rc!=SQLITE_D
2ddf1 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 72 63 3b  ONE ) return rc;
2ddf2 0a 0a 20 20 2f 2a 20 62 6c 6f 63 6b 69 64 20 63  ..  /* blockid c
2ddf3 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 61 6c 69 61  olumn is an alia
2ddf4 73 20 66 6f 72 20 72 6f 77 69 64 2e 20 2a 2f 0a  s for rowid. */.
2ddf5 20 20 2a 70 69 42 6c 6f 63 6b 69 64 20 3d 20 73    *piBlockid = s
2ddf6 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
2ddf7 72 74 5f 72 6f 77 69 64 28 76 2d 3e 64 62 29 3b  rt_rowid(v->db);
2ddf8 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
2ddf9 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 64 65 6c 65 74  _OK;.}../* delet
2ddfa 65 20 66 72 6f 6d 20 25 5f 73 65 67 6d 65 6e 74  e from %_segment
2ddfb 73 0a 2a 2a 20 20 20 77 68 65 72 65 20 62 6c 6f  s.**   where blo
2ddfc 63 6b 69 64 20 62 65 74 77 65 65 6e 20 5b 69 53  ckid between [iS
2ddfd 74 61 72 74 42 6c 6f 63 6b 69 64 5d 20 61 6e 64  tartBlockid] and
2ddfe 20 5b 69 45 6e 64 42 6c 6f 63 6b 69 64 5d 0a 2a   [iEndBlockid].*
2ddff 2a 0a 2a 2a 20 44 65 6c 65 74 65 73 20 74 68 65  *.** Deletes the
2de00 20 72 61 6e 67 65 20 6f 66 20 62 6c 6f 63 6b 73   range of blocks
2de01 2c 20 69 6e 63 6c 75 73 69 76 65 2c 20 75 73 65  , inclusive, use
2de02 64 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20  d to delete the 
2de03 62 6c 6f 63 6b 73 0a 2a 2a 20 77 68 69 63 68 20  blocks.** which 
2de04 66 6f 72 6d 20 61 20 73 65 67 6d 65 6e 74 2e 0a  form a segment..
2de05 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 6c  */.static int bl
2de06 6f 63 6b 5f 64 65 6c 65 74 65 28 66 75 6c 6c 74  ock_delete(fullt
2de07 65 78 74 5f 76 74 61 62 20 2a 76 2c 0a 20 20 20  ext_vtab *v,.   
2de08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2de09 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
2de0a 34 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c  4 iStartBlockid,
2de0b 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45   sqlite_int64 iE
2de0c 6e 64 42 6c 6f 63 6b 69 64 29 7b 0a 20 20 73 71  ndBlockid){.  sq
2de0d 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20  lite3_stmt *s;. 
2de0e 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65   int rc = sql_ge
2de0f 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 42  t_statement(v, B
2de10 4c 4f 43 4b 5f 44 45 4c 45 54 45 5f 53 54 4d 54  LOCK_DELETE_STMT
2de11 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21  , &s);.  if( rc!
2de12 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
2de13 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
2de14 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
2de15 36 34 28 73 2c 20 31 2c 20 69 53 74 61 72 74 42  64(s, 1, iStartB
2de16 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72  lockid);.  if( r
2de17 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
2de18 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
2de19 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  = sqlite3_bind_i
2de1a 6e 74 36 34 28 73 2c 20 32 2c 20 69 45 6e 64 42  nt64(s, 2, iEndB
2de1b 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72  lockid);.  if( r
2de1c 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
2de1d 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 65 74  eturn rc;..  ret
2de1e 75 72 6e 20 73 71 6c 5f 73 69 6e 67 6c 65 5f 73  urn sql_single_s
2de1f 74 65 70 28 73 29 3b 0a 7d 0a 0a 2f 2a 20 52 65  tep(s);.}../* Re
2de20 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 52 4f 57  turns SQLITE_ROW
2de21 20 77 69 74 68 20 2a 70 69 64 78 20 73 65 74 20   with *pidx set 
2de22 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 73  to the maximum s
2de23 65 67 6d 65 6e 74 20 69 64 78 20 66 6f 75 6e 64  egment idx found
2de24 0a 2a 2a 20 61 74 20 69 4c 65 76 65 6c 2e 20 20  .** at iLevel.  
2de25 52 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 44  Returns SQLITE_D
2de26 4f 4e 45 20 69 66 20 74 68 65 72 65 20 61 72 65  ONE if there are
2de27 20 6e 6f 20 73 65 67 6d 65 6e 74 73 20 61 74 0a   no segments at.
2de28 2a 2a 20 69 4c 65 76 65 6c 2e 20 20 4f 74 68 65  ** iLevel.  Othe
2de29 72 77 69 73 65 20 72 65 74 75 72 6e 73 20 61 6e  rwise returns an
2de2a 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69   error..*/.stati
2de2b 63 20 69 6e 74 20 73 65 67 64 69 72 5f 6d 61 78  c int segdir_max
2de2c 5f 69 6e 64 65 78 28 66 75 6c 6c 74 65 78 74 5f  _index(fulltext_
2de2d 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c 65  vtab *v, int iLe
2de2e 76 65 6c 2c 20 69 6e 74 20 2a 70 69 64 78 29 7b  vel, int *pidx){
2de2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
2de30 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73  *s;.  int rc = s
2de31 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
2de32 28 76 2c 20 53 45 47 44 49 52 5f 4d 41 58 5f 49  (v, SEGDIR_MAX_I
2de33 4e 44 45 58 5f 53 54 4d 54 2c 20 26 73 29 3b 0a  NDEX_STMT, &s);.
2de34 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2de35 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
2de36 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
2de37 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 31 2c 20  _bind_int(s, 1, 
2de38 69 4c 65 76 65 6c 29 3b 0a 20 20 69 66 28 20 72  iLevel);.  if( r
2de39 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
2de3a 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
2de3b 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73  = sqlite3_step(s
2de3c 29 3b 0a 20 20 2f 2a 20 53 68 6f 75 6c 64 20 61  );.  /* Should a
2de3d 6c 77 61 79 73 20 67 65 74 20 61 74 20 6c 65 61  lways get at lea
2de3e 73 74 20 6f 6e 65 20 72 6f 77 20 64 75 65 20 74  st one row due t
2de3f 6f 20 68 6f 77 20 6d 61 78 28 29 20 77 6f 72 6b  o how max() work
2de40 73 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d  s. */.  if( rc==
2de41 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72 65  SQLITE_DONE ) re
2de42 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45  turn SQLITE_DONE
2de43 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
2de44 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20  TE_ROW ) return 
2de45 72 63 3b 0a 0a 20 20 2f 2a 20 4e 55 4c 4c 20 6d  rc;..  /* NULL m
2de46 65 61 6e 73 20 74 68 61 74 20 74 68 65 72 65 20  eans that there 
2de47 77 65 72 65 20 6e 6f 20 69 6e 70 75 74 73 20 74  were no inputs t
2de48 6f 20 6d 61 78 28 29 2e 20 2a 2f 0a 20 20 69 66  o max(). */.  if
2de49 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73  ( SQLITE_NULL==s
2de4a 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
2de4b 70 65 28 73 2c 20 30 29 20 29 7b 0a 20 20 20 20  pe(s, 0) ){.    
2de4c 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65  rc = sqlite3_ste
2de4d 70 28 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63  p(s);.    if( rc
2de4e 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72  ==SQLITE_ROW ) r
2de4f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
2de50 4f 52 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72  OR;.    return r
2de51 63 3b 0a 20 20 7d 0a 0a 20 20 2a 70 69 64 78 20  c;.  }..  *pidx 
2de52 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
2de53 5f 69 6e 74 28 73 2c 20 30 29 3b 0a 0a 20 20 2f  _int(s, 0);..  /
2de54 2a 20 57 65 20 65 78 70 65 63 74 20 6f 6e 6c 79  * We expect only
2de55 20 6f 6e 65 20 72 6f 77 2e 20 20 57 65 20 6d 75   one row.  We mu
2de56 73 74 20 65 78 65 63 75 74 65 20 61 6e 6f 74 68  st execute anoth
2de57 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  er sqlite3_step(
2de58 29 0a 20 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65  ).   * to comple
2de59 74 65 20 74 68 65 20 69 74 65 72 61 74 69 6f 6e  te the iteration
2de5a 3b 20 6f 74 68 65 72 77 69 73 65 20 74 68 65 20  ; otherwise the 
2de5b 74 61 62 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69  table will remai
2de5c 6e 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72  n locked. */.  r
2de5d 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
2de5e 28 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  (s);.  if( rc==S
2de5f 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75  QLITE_ROW ) retu
2de60 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
2de61 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
2de62 45 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20  E_DONE ) return 
2de63 72 63 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  rc;.  return SQL
2de64 49 54 45 5f 52 4f 57 3b 0a 7d 0a 0a 2f 2a 20 69  ITE_ROW;.}../* i
2de65 6e 73 65 72 74 20 69 6e 74 6f 20 25 5f 73 65 67  nsert into %_seg
2de66 64 69 72 20 76 61 6c 75 65 73 20 28 0a 2a 2a 20  dir values (.** 
2de67 20 20 5b 69 4c 65 76 65 6c 5d 2c 20 5b 69 64 78    [iLevel], [idx
2de68 5d 2c 0a 2a 2a 20 20 20 5b 69 53 74 61 72 74 42  ],.**   [iStartB
2de69 6c 6f 63 6b 69 64 5d 2c 20 5b 69 4c 65 61 76 65  lockid], [iLeave
2de6a 73 45 6e 64 42 6c 6f 63 6b 69 64 5d 2c 20 5b 69  sEndBlockid], [i
2de6b 45 6e 64 42 6c 6f 63 6b 69 64 5d 2c 0a 2a 2a 20  EndBlockid],.** 
2de6c 20 20 5b 70 52 6f 6f 74 44 61 74 61 5d 0a 2a 2a    [pRootData].**
2de6d 20 29 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74   ).*/.static int
2de6e 20 73 65 67 64 69 72 5f 73 65 74 28 66 75 6c 6c   segdir_set(full
2de6f 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e  text_vtab *v, in
2de70 74 20 69 4c 65 76 65 6c 2c 20 69 6e 74 20 69 64  t iLevel, int id
2de71 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  x,.             
2de72 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f           sqlite_
2de73 69 6e 74 36 34 20 69 53 74 61 72 74 42 6c 6f 63  int64 iStartBloc
2de74 6b 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20  kid,.           
2de75 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2de76 65 5f 69 6e 74 36 34 20 69 4c 65 61 76 65 73 45  e_int64 iLeavesE
2de77 6e 64 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20  ndBlockid,.     
2de78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2de79 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45   sqlite_int64 iE
2de7a 6e 64 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20  ndBlockid,.     
2de7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2de7c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 52 6f   const char *pRo
2de7d 6f 74 44 61 74 61 2c 20 69 6e 74 20 6e 52 6f 6f  otData, int nRoo
2de7e 74 44 61 74 61 29 7b 0a 20 20 73 71 6c 69 74 65  tData){.  sqlite
2de7f 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74  3_stmt *s;.  int
2de80 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74   rc = sql_get_st
2de81 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49  atement(v, SEGDI
2de82 52 5f 53 45 54 5f 53 54 4d 54 2c 20 26 73 29 3b  R_SET_STMT, &s);
2de83 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
2de84 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
2de85 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
2de86 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 31 2c  3_bind_int(s, 1,
2de87 20 69 4c 65 76 65 6c 29 3b 0a 20 20 69 66 28 20   iLevel);.  if( 
2de88 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2de89 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
2de8a 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f   = sqlite3_bind_
2de8b 69 6e 74 28 73 2c 20 32 2c 20 69 64 78 29 3b 0a  int(s, 2, idx);.
2de8c 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2de8d 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
2de8e 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
2de8f 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 33  _bind_int64(s, 3
2de90 2c 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 29  , iStartBlockid)
2de91 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
2de92 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
2de93 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  c;..  rc = sqlit
2de94 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c  e3_bind_int64(s,
2de95 20 34 2c 20 69 4c 65 61 76 65 73 45 6e 64 42 6c   4, iLeavesEndBl
2de96 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72 63  ockid);.  if( rc
2de97 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
2de98 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
2de99 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
2de9a 74 36 34 28 73 2c 20 35 2c 20 69 45 6e 64 42 6c  t64(s, 5, iEndBl
2de9b 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72 63  ockid);.  if( rc
2de9c 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
2de9d 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
2de9e 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c   sqlite3_bind_bl
2de9f 6f 62 28 73 2c 20 36 2c 20 70 52 6f 6f 74 44 61  ob(s, 6, pRootDa
2dea0 74 61 2c 20 6e 52 6f 6f 74 44 61 74 61 2c 20 53  ta, nRootData, S
2dea1 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
2dea2 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2dea3 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
2dea4 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 73 69  .  return sql_si
2dea5 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d 0a  ngle_step(s);.}.
2dea6 0a 2f 2a 20 51 75 65 72 69 65 73 20 25 5f 73 65  ./* Queries %_se
2dea7 67 64 69 72 20 66 6f 72 20 74 68 65 20 62 6c 6f  gdir for the blo
2dea8 63 6b 20 73 70 61 6e 20 6f 66 20 74 68 65 20 73  ck span of the s
2dea9 65 67 6d 65 6e 74 73 20 69 6e 20 6c 65 76 65 6c  egments in level
2deaa 0a 2a 2a 20 69 4c 65 76 65 6c 2e 20 20 52 65 74  .** iLevel.  Ret
2deab 75 72 6e 73 20 53 51 4c 49 54 45 5f 44 4f 4e 45  urns SQLITE_DONE
2deac 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f   if there are no
2dead 20 62 6c 6f 63 6b 73 20 66 6f 72 20 69 4c 65 76   blocks for iLev
2deae 65 6c 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 52 4f  el,.** SQLITE_RO
2deaf 57 20 69 66 20 74 68 65 72 65 20 61 72 65 20 62  W if there are b
2deb0 6c 6f 63 6b 73 2c 20 65 6c 73 65 20 61 6e 20 65  locks, else an e
2deb1 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rror..*/.static 
2deb2 69 6e 74 20 73 65 67 64 69 72 5f 73 70 61 6e 28  int segdir_span(
2deb3 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
2deb4 2c 20 69 6e 74 20 69 4c 65 76 65 6c 2c 0a 20 20  , int iLevel,.  
2deb5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2deb6 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
2deb7 34 20 2a 70 69 53 74 61 72 74 42 6c 6f 63 6b 69  4 *piStartBlocki
2deb8 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
2deb9 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2deba 5f 69 6e 74 36 34 20 2a 70 69 45 6e 64 42 6c 6f  _int64 *piEndBlo
2debb 63 6b 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33  ckid){.  sqlite3
2debc 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20  _stmt *s;.  int 
2debd 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61  rc = sql_get_sta
2debe 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49 52  tement(v, SEGDIR
2debf 5f 53 50 41 4e 5f 53 54 4d 54 2c 20 26 73 29 3b  _SPAN_STMT, &s);
2dec0 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
2dec1 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
2dec2 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
2dec3 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 31 2c  3_bind_int(s, 1,
2dec4 20 69 4c 65 76 65 6c 29 3b 0a 20 20 69 66 28 20   iLevel);.  if( 
2dec5 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2dec6 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
2dec7 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
2dec8 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  s);.  if( rc==SQ
2dec9 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72 65 74 75  LITE_DONE ) retu
2deca 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 20  rn SQLITE_DONE; 
2decb 20 2f 2a 20 53 68 6f 75 6c 64 20 6e 65 76 65 72   /* Should never
2decc 20 68 61 70 70 65 6e 20 2a 2f 0a 20 20 69 66 28   happen */.  if(
2decd 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20   rc!=SQLITE_ROW 
2dece 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
2decf 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20  /* This happens 
2ded0 69 66 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73 20  if all segments 
2ded1 61 74 20 74 68 69 73 20 6c 65 76 65 6c 20 61 72  at this level ar
2ded2 65 20 65 6e 74 69 72 65 6c 79 20 69 6e 6c 69 6e  e entirely inlin
2ded3 65 2e 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49  e. */.  if( SQLI
2ded4 54 45 5f 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 33  TE_NULL==sqlite3
2ded5 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 2c 20  _column_type(s, 
2ded6 30 29 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20  0) ){.    /* We 
2ded7 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20  expect only one 
2ded8 72 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65 78  row.  We must ex
2ded9 65 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73 71  ecute another sq
2deda 6c 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20 20  lite3_step().   
2dedb 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20    * to complete 
2dedc 74 68 65 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f  the iteration; o
2dedd 74 68 65 72 77 69 73 65 20 74 68 65 20 74 61 62  therwise the tab
2dede 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c  le will remain l
2dedf 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 20 20 69 6e  ocked. */.    in
2dee0 74 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 5f  t rc2 = sqlite3_
2dee1 73 74 65 70 28 73 29 3b 0a 20 20 20 20 69 66 28  step(s);.    if(
2dee2 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 52 4f 57   rc2==SQLITE_ROW
2dee3 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
2dee4 5f 45 52 52 4f 52 3b 0a 20 20 20 20 72 65 74 75  _ERROR;.    retu
2dee5 72 6e 20 72 63 32 3b 0a 20 20 7d 0a 0a 20 20 2a  rn rc2;.  }..  *
2dee6 70 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 20 3d  piStartBlockid =
2dee7 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
2dee8 69 6e 74 36 34 28 73 2c 20 30 29 3b 0a 20 20 2a  int64(s, 0);.  *
2dee9 70 69 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 73  piEndBlockid = s
2deea 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
2deeb 74 36 34 28 73 2c 20 31 29 3b 0a 0a 20 20 2f 2a  t64(s, 1);..  /*
2deec 20 57 65 20 65 78 70 65 63 74 20 6f 6e 6c 79 20   We expect only 
2deed 6f 6e 65 20 72 6f 77 2e 20 20 57 65 20 6d 75 73  one row.  We mus
2deee 74 20 65 78 65 63 75 74 65 20 61 6e 6f 74 68 65  t execute anothe
2deef 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  r sqlite3_step()
2def0 0a 20 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74  .   * to complet
2def1 65 20 74 68 65 20 69 74 65 72 61 74 69 6f 6e 3b  e the iteration;
2def2 20 6f 74 68 65 72 77 69 73 65 20 74 68 65 20 74   otherwise the t
2def3 61 62 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e  able will remain
2def4 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63   locked. */.  rc
2def5 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
2def6 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  s);.  if( rc==SQ
2def7 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72  LITE_ROW ) retur
2def8 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
2def9 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2defa 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 72  _DONE ) return r
2defb 63 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  c;.  return SQLI
2defc 54 45 5f 52 4f 57 3b 0a 7d 0a 0a 2f 2a 20 44 65  TE_ROW;.}../* De
2defd 6c 65 74 65 20 74 68 65 20 73 65 67 6d 65 6e 74  lete the segment
2defe 20 62 6c 6f 63 6b 73 20 61 6e 64 20 73 65 67 6d   blocks and segm
2deff 65 6e 74 20 64 69 72 65 63 74 6f 72 79 20 72 65  ent directory re
2df00 63 6f 72 64 73 20 66 6f 72 20 61 6c 6c 0a 2a 2a  cords for all.**
2df01 20 73 65 67 6d 65 6e 74 73 20 61 74 20 69 4c 65   segments at iLe
2df02 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  vel..*/.static i
2df03 6e 74 20 73 65 67 64 69 72 5f 64 65 6c 65 74 65  nt segdir_delete
2df04 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
2df05 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 29 7b 0a  v, int iLevel){.
2df06 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
2df07 73 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  s;.  sqlite_int6
2df08 34 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c  4 iStartBlockid,
2df09 20 69 45 6e 64 42 6c 6f 63 6b 69 64 3b 0a 20 20   iEndBlockid;.  
2df0a 69 6e 74 20 72 63 20 3d 20 73 65 67 64 69 72 5f  int rc = segdir_
2df0b 73 70 61 6e 28 76 2c 20 69 4c 65 76 65 6c 2c 20  span(v, iLevel, 
2df0c 26 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c 20  &iStartBlockid, 
2df0d 26 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20  &iEndBlockid);. 
2df0e 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2df0f 52 4f 57 20 26 26 20 72 63 21 3d 53 51 4c 49 54  ROW && rc!=SQLIT
2df10 45 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20  E_DONE ) return 
2df11 72 63 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53  rc;..  if( rc==S
2df12 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
2df13 20 72 63 20 3d 20 62 6c 6f 63 6b 5f 64 65 6c 65   rc = block_dele
2df14 74 65 28 76 2c 20 69 53 74 61 72 74 42 6c 6f 63  te(v, iStartBloc
2df15 6b 69 64 2c 20 69 45 6e 64 42 6c 6f 63 6b 69 64  kid, iEndBlockid
2df16 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
2df17 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
2df18 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  n rc;.  }..  /* 
2df19 44 65 6c 65 74 65 20 74 68 65 20 73 65 67 6d 65  Delete the segme
2df1a 6e 74 20 64 69 72 65 63 74 6f 72 79 20 69 74 73  nt directory its
2df1b 65 6c 66 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73  elf. */.  rc = s
2df1c 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
2df1d 28 76 2c 20 53 45 47 44 49 52 5f 44 45 4c 45 54  (v, SEGDIR_DELET
2df1e 45 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69  E_STMT, &s);.  i
2df1f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2df20 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
2df21 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
2df22 6e 64 5f 69 6e 74 36 34 28 73 2c 20 31 2c 20 69  nd_int64(s, 1, i
2df23 4c 65 76 65 6c 29 3b 0a 20 20 69 66 28 20 72 63  Level);.  if( rc
2df24 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
2df25 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75  turn rc;..  retu
2df26 72 6e 20 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74  rn sql_single_st
2df27 65 70 28 73 29 3b 0a 7d 0a 0a 2f 2a 20 44 65 6c  ep(s);.}../* Del
2df28 65 74 65 20 65 6e 74 69 72 65 20 66 74 73 20 69  ete entire fts i
2df29 6e 64 65 78 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  ndex, SQLITE_OK 
2df2a 6f 6e 20 73 75 63 63 65 73 73 2c 20 72 65 6c 65  on success, rele
2df2b 76 61 6e 74 20 65 72 72 6f 72 20 6f 6e 0a 2a 2a  vant error on.**
2df2c 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61   failure..*/.sta
2df2d 74 69 63 20 69 6e 74 20 73 65 67 64 69 72 5f 64  tic int segdir_d
2df2e 65 6c 65 74 65 5f 61 6c 6c 28 66 75 6c 6c 74 65  elete_all(fullte
2df2f 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20 73  xt_vtab *v){.  s
2df30 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a  qlite3_stmt *s;.
2df31 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67    int rc = sql_g
2df32 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
2df33 53 45 47 44 49 52 5f 44 45 4c 45 54 45 5f 41 4c  SEGDIR_DELETE_AL
2df34 4c 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69  L_STMT, &s);.  i
2df35 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2df36 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
2df37 20 72 63 20 3d 20 73 71 6c 5f 73 69 6e 67 6c 65   rc = sql_single
2df38 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20  _step(s);.  if( 
2df39 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2df3a 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
2df3b 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65   = sql_get_state
2df3c 6d 65 6e 74 28 76 2c 20 42 4c 4f 43 4b 5f 44 45  ment(v, BLOCK_DE
2df3d 4c 45 54 45 5f 41 4c 4c 5f 53 54 4d 54 2c 20 26  LETE_ALL_STMT, &
2df3e 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  s);.  if( rc!=SQ
2df3f 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
2df40 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 73   rc;..  return s
2df41 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73  ql_single_step(s
2df42 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 73  );.}../* Returns
2df43 20 53 51 4c 49 54 45 5f 4f 4b 20 77 69 74 68 20   SQLITE_OK with 
2df44 2a 70 6e 53 65 67 6d 65 6e 74 73 20 73 65 74 20  *pnSegments set 
2df45 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  to the number of
2df46 20 65 6e 74 72 69 65 73 20 69 6e 0a 2a 2a 20 25   entries in.** %
2df47 5f 73 65 67 64 69 72 20 61 6e 64 20 2a 70 69 4d  _segdir and *piM
2df48 61 78 4c 65 76 65 6c 20 73 65 74 20 74 6f 20 74  axLevel set to t
2df49 68 65 20 68 69 67 68 65 73 74 20 6c 65 76 65 6c  he highest level
2df4a 20 77 68 69 63 68 20 68 61 73 20 61 0a 2a 2a 20   which has a.** 
2df4b 73 65 67 6d 65 6e 74 2e 20 20 4f 74 68 65 72 77  segment.  Otherw
2df4c 69 73 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ise returns the 
2df4d 53 51 4c 69 74 65 20 65 72 72 6f 72 20 77 68 69  SQLite error whi
2df4e 63 68 20 63 61 75 73 65 64 20 66 61 69 6c 75 72  ch caused failur
2df4f 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
2df50 20 73 65 67 64 69 72 5f 63 6f 75 6e 74 28 66 75   segdir_count(fu
2df51 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
2df52 69 6e 74 20 2a 70 6e 53 65 67 6d 65 6e 74 73 2c  int *pnSegments,
2df53 20 69 6e 74 20 2a 70 69 4d 61 78 4c 65 76 65 6c   int *piMaxLevel
2df54 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  ){.  sqlite3_stm
2df55 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d  t *s;.  int rc =
2df56 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65   sql_get_stateme
2df57 6e 74 28 76 2c 20 53 45 47 44 49 52 5f 43 4f 55  nt(v, SEGDIR_COU
2df58 4e 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20  NT_STMT, &s);.  
2df59 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2df5a 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
2df5b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73    rc = sqlite3_s
2df5c 74 65 70 28 73 29 3b 0a 20 20 2f 2a 20 54 4f 44  tep(s);.  /* TOD
2df5d 4f 28 73 68 65 73 73 29 3a 20 54 68 69 73 20 63  O(shess): This c
2df5e 61 73 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  ase should not b
2df5f 65 20 70 6f 73 73 69 62 6c 65 3f 20 20 53 68 6f  e possible?  Sho
2df60 75 6c 64 20 73 74 72 6f 6e 67 65 72 0a 20 20 2a  uld stronger.  *
2df61 2a 20 6d 65 61 73 75 72 65 73 20 62 65 20 74 61  * measures be ta
2df62 6b 65 6e 20 69 66 20 69 74 20 68 61 70 70 65 6e  ken if it happen
2df63 73 3f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63  s?.  */.  if( rc
2df64 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
2df65 0a 20 20 20 20 2a 70 6e 53 65 67 6d 65 6e 74 73  .    *pnSegments
2df66 20 3d 20 30 3b 0a 20 20 20 20 2a 70 69 4d 61 78   = 0;.    *piMax
2df67 4c 65 76 65 6c 20 3d 20 30 3b 0a 20 20 20 20 72  Level = 0;.    r
2df68 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2df69 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 3d 53  .  }.  if( rc!=S
2df6a 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75  QLITE_ROW ) retu
2df6b 72 6e 20 72 63 3b 0a 0a 20 20 2a 70 6e 53 65 67  rn rc;..  *pnSeg
2df6c 6d 65 6e 74 73 20 3d 20 73 71 6c 69 74 65 33 5f  ments = sqlite3_
2df6d 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 2c 20 30 29  column_int(s, 0)
2df6e 3b 0a 20 20 2a 70 69 4d 61 78 4c 65 76 65 6c 20  ;.  *piMaxLevel 
2df6f 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
2df70 5f 69 6e 74 28 73 2c 20 31 29 3b 0a 0a 20 20 2f  _int(s, 1);..  /
2df71 2a 20 57 65 20 65 78 70 65 63 74 20 6f 6e 6c 79  * We expect only
2df72 20 6f 6e 65 20 72 6f 77 2e 20 20 57 65 20 6d 75   one row.  We mu
2df73 73 74 20 65 78 65 63 75 74 65 20 61 6e 6f 74 68  st execute anoth
2df74 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  er sqlite3_step(
2df75 29 0a 20 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65  ).   * to comple
2df76 74 65 20 74 68 65 20 69 74 65 72 61 74 69 6f 6e  te the iteration
2df77 3b 20 6f 74 68 65 72 77 69 73 65 20 74 68 65 20  ; otherwise the 
2df78 74 61 62 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69  table will remai
2df79 6e 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72  n locked. */.  r
2df7a 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
2df7b 28 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  (s);.  if( rc==S
2df7c 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72 65 74  QLITE_DONE ) ret
2df7d 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
2df7e 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
2df7f 52 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ROW ) return SQL
2df80 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 72 65 74  ITE_ERROR;.  ret
2df81 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 4f  urn rc;.}../* TO
2df82 44 4f 28 73 68 65 73 73 29 20 63 6c 65 61 72 50  DO(shess) clearP
2df83 65 6e 64 69 6e 67 54 65 72 6d 73 28 29 20 69 73  endingTerms() is
2df84 20 66 61 72 20 64 6f 77 6e 20 74 68 65 20 66 69   far down the fi
2df85 6c 65 20 62 65 63 61 75 73 65 0a 2a 2a 20 77 72  le because.** wr
2df86 69 74 65 5a 65 72 6f 53 65 67 6d 65 6e 74 28 29  iteZeroSegment()
2df87 20 69 73 20 66 61 72 20 64 6f 77 6e 20 74 68 65   is far down the
2df88 20 66 69 6c 65 20 62 65 63 61 75 73 65 20 4c 65   file because Le
2df89 61 66 57 72 69 74 65 72 20 69 73 20 66 61 72 0a  afWriter is far.
2df8a 2a 2a 20 64 6f 77 6e 20 74 68 65 20 66 69 6c 65  ** down the file
2df8b 2e 20 20 43 6f 6e 73 69 64 65 72 20 72 65 66 61  .  Consider refa
2df8c 63 74 6f 72 69 6e 67 20 74 68 65 20 63 6f 64 65  ctoring the code
2df8d 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 6e 6f 6e   to move the non
2df8e 2d 76 74 61 62 0a 2a 2a 20 63 6f 64 65 20 61 62  -vtab.** code ab
2df8f 6f 76 65 20 74 68 65 20 76 74 61 62 20 63 6f 64  ove the vtab cod
2df90 65 20 73 6f 20 74 68 61 74 20 77 65 20 64 6f 6e  e so that we don
2df91 27 74 20 6e 65 65 64 20 74 68 69 73 20 66 6f 72  't need this for
2df92 77 61 72 64 0a 2a 2a 20 72 65 66 65 72 65 6e 63  ward.** referenc
2df93 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
2df94 20 63 6c 65 61 72 50 65 6e 64 69 6e 67 54 65 72   clearPendingTer
2df95 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  ms(fulltext_vtab
2df96 20 2a 76 29 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 65   *v);../*.** Fre
2df97 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 75 73 65  e the memory use
2df98 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 66  d to contain a f
2df99 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 73 74 72  ulltext_vtab str
2df9a 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69  ucture..*/.stati
2df9b 63 20 76 6f 69 64 20 66 75 6c 6c 74 65 78 74 5f  c void fulltext_
2df9c 76 74 61 62 5f 64 65 73 74 72 6f 79 28 66 75 6c  vtab_destroy(ful
2df9d 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a  ltext_vtab *v){.
2df9e 20 20 69 6e 74 20 69 53 74 6d 74 2c 20 69 3b 0a    int iStmt, i;.
2df9f 0a 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54  .  FTSTRACE(("FT
2dfa0 53 33 20 44 65 73 74 72 6f 79 20 25 70 5c 6e 22  S3 Destroy %p\n"
2dfa1 2c 20 76 29 29 3b 0a 20 20 66 6f 72 28 20 69 53  , v));.  for( iS
2dfa2 74 6d 74 3d 30 3b 20 69 53 74 6d 74 3c 4d 41 58  tmt=0; iStmt<MAX
2dfa3 5f 53 54 4d 54 3b 20 69 53 74 6d 74 2b 2b 20 29  _STMT; iStmt++ )
2dfa4 7b 0a 20 20 20 20 69 66 28 20 76 2d 3e 70 46 75  {.    if( v->pFu
2dfa5 6c 6c 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73  lltextStatements
2dfa6 5b 69 53 74 6d 74 5d 21 3d 4e 55 4c 4c 20 29 7b  [iStmt]!=NULL ){
2dfa7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
2dfa8 69 6e 61 6c 69 7a 65 28 76 2d 3e 70 46 75 6c 6c  inalize(v->pFull
2dfa9 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73 5b 69  textStatements[i
2dfaa 53 74 6d 74 5d 29 3b 0a 20 20 20 20 20 20 76 2d  Stmt]);.      v-
2dfab 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d  >pFulltextStatem
2dfac 65 6e 74 73 5b 69 53 74 6d 74 5d 20 3d 20 4e 55  ents[iStmt] = NU
2dfad 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  LL;.    }.  }.. 
2dfae 20 66 6f 72 28 20 69 3d 30 3b 20 69 3c 4d 45 52   for( i=0; i<MER
2dfaf 47 45 5f 43 4f 55 4e 54 3b 20 69 2b 2b 20 29 7b  GE_COUNT; i++ ){
2dfb0 0a 20 20 20 20 69 66 28 20 76 2d 3e 70 4c 65 61  .    if( v->pLea
2dfb1 66 53 65 6c 65 63 74 53 74 6d 74 73 5b 69 5d 21  fSelectStmts[i]!
2dfb2 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 73  =NULL ){.      s
2dfb3 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
2dfb4 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74 53 74  v->pLeafSelectSt
2dfb5 6d 74 73 5b 69 5d 29 3b 0a 20 20 20 20 20 20 76  mts[i]);.      v
2dfb6 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d  ->pLeafSelectStm
2dfb7 74 73 5b 69 5d 20 3d 20 4e 55 4c 4c 3b 0a 20 20  ts[i] = NULL;.  
2dfb8 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 76    }.  }..  if( v
2dfb9 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 21 3d 4e 55  ->pTokenizer!=NU
2dfba 4c 4c 20 29 7b 0a 20 20 20 20 76 2d 3e 70 54 6f  LL ){.    v->pTo
2dfbb 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65  kenizer->pModule
2dfbc 2d 3e 78 44 65 73 74 72 6f 79 28 76 2d 3e 70 54  ->xDestroy(v->pT
2dfbd 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20 20 20 20 76  okenizer);.    v
2dfbe 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 4e  ->pTokenizer = N
2dfbf 55 4c 4c 3b 0a 20 20 7d 0a 0a 20 20 63 6c 65 61  ULL;.  }..  clea
2dfc0 72 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 76 29  rPendingTerms(v)
2dfc1 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  ;..  sqlite3_fre
2dfc2 65 28 76 2d 3e 61 7a 43 6f 6c 75 6d 6e 29 3b 0a  e(v->azColumn);.
2dfc3 20 20 66 6f 72 28 69 20 3d 20 30 3b 20 69 20 3c    for(i = 0; i <
2dfc4 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69   v->nColumn; ++i
2dfc5 29 20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ) {.    sqlite3_
2dfc6 66 72 65 65 28 76 2d 3e 61 7a 43 6f 6e 74 65 6e  free(v->azConten
2dfc7 74 43 6f 6c 75 6d 6e 5b 69 5d 29 3b 0a 20 20 7d  tColumn[i]);.  }
2dfc8 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
2dfc9 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75  v->azContentColu
2dfca 6d 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  mn);.  sqlite3_f
2dfcb 72 65 65 28 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ree(v);.}../*.**
2dfcc 20 54 6f 6b 65 6e 20 74 79 70 65 73 20 66 6f 72   Token types for
2dfcd 20 70 61 72 73 69 6e 67 20 74 68 65 20 61 72 67   parsing the arg
2dfce 75 6d 65 6e 74 73 20 74 6f 20 78 43 6f 6e 6e 65  uments to xConne
2dfcf 63 74 20 6f 72 20 78 43 72 65 61 74 65 2e 0a 2a  ct or xCreate..*
2dfd0 2f 0a 23 64 65 66 69 6e 65 20 54 4f 4b 45 4e 5f  /.#define TOKEN_
2dfd1 45 4f 46 20 20 20 20 20 20 20 20 20 30 20 20 20  EOF         0   
2dfd2 20 2f 2a 20 45 6e 64 20 6f 66 20 66 69 6c 65 20   /* End of file 
2dfd3 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4f 4b 45 4e  */.#define TOKEN
2dfd4 5f 53 50 41 43 45 20 20 20 20 20 20 20 31 20 20  _SPACE       1  
2dfd5 20 20 2f 2a 20 41 6e 79 20 6b 69 6e 64 20 6f 66    /* Any kind of
2dfd6 20 77 68 69 74 65 73 70 61 63 65 20 2a 2f 0a 23   whitespace */.#
2dfd7 64 65 66 69 6e 65 20 54 4f 4b 45 4e 5f 49 44 20  define TOKEN_ID 
2dfd8 20 20 20 20 20 20 20 20 20 32 20 20 20 20 2f 2a           2    /*
2dfd9 20 41 6e 20 69 64 65 6e 74 69 66 69 65 72 20 2a   An identifier *
2dfda 2f 0a 23 64 65 66 69 6e 65 20 54 4f 4b 45 4e 5f  /.#define TOKEN_
2dfdb 53 54 52 49 4e 47 20 20 20 20 20 20 33 20 20 20  STRING      3   
2dfdc 20 2f 2a 20 41 20 73 74 72 69 6e 67 20 6c 69 74   /* A string lit
2dfdd 65 72 61 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20  eral */.#define 
2dfde 54 4f 4b 45 4e 5f 50 55 4e 43 54 20 20 20 20 20  TOKEN_PUNCT     
2dfdf 20 20 34 20 20 20 20 2f 2a 20 41 20 73 69 6e 67    4    /* A sing
2dfe0 6c 65 20 70 75 6e 63 74 75 61 74 69 6f 6e 20 63  le punctuation c
2dfe1 68 61 72 61 63 74 65 72 20 2a 2f 0a 0a 2f 2a 0a  haracter */../*.
2dfe2 2a 2a 20 49 66 20 58 20 69 73 20 61 20 63 68 61  ** If X is a cha
2dfe3 72 61 63 74 65 72 20 74 68 61 74 20 63 61 6e 20  racter that can 
2dfe4 62 65 20 75 73 65 64 20 69 6e 20 61 6e 20 69 64  be used in an id
2dfe5 65 6e 74 69 66 69 65 72 20 74 68 65 6e 0a 2a 2a  entifier then.**
2dfe6 20 66 74 73 49 64 43 68 61 72 28 58 29 20 77 69   ftsIdChar(X) wi
2dfe7 6c 6c 20 62 65 20 74 72 75 65 2e 20 20 4f 74 68  ll be true.  Oth
2dfe8 65 72 77 69 73 65 20 69 74 20 69 73 20 66 61 6c  erwise it is fal
2dfe9 73 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 41 53  se..**.** For AS
2dfea 43 49 49 2c 20 61 6e 79 20 63 68 61 72 61 63 74  CII, any charact
2dfeb 65 72 20 77 69 74 68 20 74 68 65 20 68 69 67 68  er with the high
2dfec 2d 6f 72 64 65 72 20 62 69 74 20 73 65 74 20 69  -order bit set i
2dfed 73 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 69 6e 20  s.** allowed in 
2dfee 61 6e 20 69 64 65 6e 74 69 66 69 65 72 2e 20 20  an identifier.  
2dfef 46 6f 72 20 37 2d 62 69 74 20 63 68 61 72 61 63  For 7-bit charac
2dff0 74 65 72 73 2c 20 0a 2a 2a 20 69 73 46 74 73 49  ters, .** isFtsI
2dff1 64 43 68 61 72 5b 58 5d 20 6d 75 73 74 20 62 65  dChar[X] must be
2dff2 20 31 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74   1..**.** Ticket
2dff3 20 23 31 30 36 36 2e 20 20 74 68 65 20 53 51 4c   #1066.  the SQL
2dff4 20 73 74 61 6e 64 61 72 64 20 64 6f 65 73 20 6e   standard does n
2dff5 6f 74 20 61 6c 6c 6f 77 20 27 24 27 20 69 6e 20  ot allow '$' in 
2dff6 74 68 65 0a 2a 2a 20 6d 69 64 64 6c 65 20 6f 66  the.** middle of
2dff7 20 69 64 65 6e 74 66 69 65 72 73 2e 20 20 42 75   identfiers.  Bu
2dff8 74 20 6d 61 6e 79 20 53 51 4c 20 69 6d 70 6c 65  t many SQL imple
2dff9 6d 65 6e 74 61 74 69 6f 6e 73 20 64 6f 2e 20 0a  mentations do. .
2dffa 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61  ** SQLite will a
2dffb 6c 6c 6f 77 20 27 24 27 20 69 6e 20 69 64 65 6e  llow '$' in iden
2dffc 74 69 66 69 65 72 73 20 66 6f 72 20 63 6f 6d 70  tifiers for comp
2dffd 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a 20 42 75  atibility..** Bu
2dffe 74 20 74 68 65 20 66 65 61 74 75 72 65 20 69 73  t the feature is
2dfff 20 75 6e 64 6f 63 75 6d 65 6e 74 65 64 2e 0a 2a   undocumented..*
2e000 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  /.static const c
2e001 68 61 72 20 69 73 46 74 73 49 64 43 68 61 72 5b  har isFtsIdChar[
2e002 5d 20 3d 20 7b 0a 2f 2a 20 78 30 20 78 31 20 78  ] = {./* x0 x1 x
2e003 32 20 78 33 20 78 34 20 78 35 20 78 36 20 78 37  2 x3 x4 x5 x6 x7
2e004 20 78 38 20 78 39 20 78 41 20 78 42 20 78 43 20   x8 x9 xA xB xC 
2e005 78 44 20 78 45 20 78 46 20 2a 2f 0a 20 20 20 20  xD xE xF */.    
2e006 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 30  0, 0, 0, 0, 1, 0
2e007 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
2e008 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2e009 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20 20 31 2c   /* 2x */.    1,
2e00a 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2e00b 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30  1, 1, 1, 1, 0, 0
2e00c 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f  , 0, 0, 0, 0,  /
2e00d 2a 20 33 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31  * 3x */.    0, 1
2e00e 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2e00f 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2e010 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20  1, 1, 1, 1,  /* 
2e011 34 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20  4x */.    1, 1, 
2e012 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2e013 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c  , 1, 1, 1, 0, 0,
2e014 20 30 2c 20 30 2c 20 31 2c 20 20 2f 2a 20 35 78   0, 0, 1,  /* 5x
2e015 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c   */.    0, 1, 1,
2e016 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2e017 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2e018 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 36 78 20 2a  , 1, 1,  /* 6x *
2e019 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31  /.    1, 1, 1, 1
2e01a 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2e01b 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 1, 0, 0, 0, 
2e01c 30 2c 20 30 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a  0, 0,  /* 7x */.
2e01d 7d 3b 0a 23 64 65 66 69 6e 65 20 66 74 73 49 64  };.#define ftsId
2e01e 43 68 61 72 28 43 29 20 20 28 28 28 63 3d 43 29  Char(C)  (((c=C)
2e01f 26 30 78 38 30 29 21 3d 30 20 7c 7c 20 28 63 3e  &0x80)!=0 || (c>
2e020 30 78 31 66 20 26 26 20 69 73 46 74 73 49 64 43  0x1f && isFtsIdC
2e021 68 61 72 5b 63 2d 30 78 32 30 5d 29 29 0a 0a 0a  har[c-0x20]))...
2e022 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
2e023 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 74   length of the t
2e024 6f 6b 65 6e 20 74 68 61 74 20 62 65 67 69 6e 73  oken that begins
2e025 20 61 74 20 7a 5b 30 5d 2e 20 0a 2a 2a 20 53 74   at z[0]. .** St
2e026 6f 72 65 20 74 68 65 20 74 6f 6b 65 6e 20 74 79  ore the token ty
2e027 70 65 20 69 6e 20 2a 74 6f 6b 65 6e 54 79 70 65  pe in *tokenType
2e028 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
2e029 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  g..*/.static int
2e02a 20 66 74 73 47 65 74 54 6f 6b 65 6e 28 63 6f 6e   ftsGetToken(con
2e02b 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20  st char *z, int 
2e02c 2a 74 6f 6b 65 6e 54 79 70 65 29 7b 0a 20 20 69  *tokenType){.  i
2e02d 6e 74 20 69 2c 20 63 3b 0a 20 20 73 77 69 74 63  nt i, c;.  switc
2e02e 68 28 20 2a 7a 20 29 7b 0a 20 20 20 20 63 61 73  h( *z ){.    cas
2e02f 65 20 30 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f  e 0: {.      *to
2e030 6b 65 6e 54 79 70 65 20 3d 20 54 4f 4b 45 4e 5f  kenType = TOKEN_
2e031 45 4f 46 3b 0a 20 20 20 20 20 20 72 65 74 75 72  EOF;.      retur
2e032 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 0;.    }.    c
2e033 61 73 65 20 27 20 27 3a 20 63 61 73 65 20 27 5c  ase ' ': case '\
2e034 74 27 3a 20 63 61 73 65 20 27 5c 6e 27 3a 20 63  t': case '\n': c
2e035 61 73 65 20 27 5c 66 27 3a 20 63 61 73 65 20 27  ase '\f': case '
2e036 5c 72 27 3a 20 7b 0a 20 20 20 20 20 20 66 6f 72  \r': {.      for
2e037 28 69 3d 31 3b 20 73 61 66 65 5f 69 73 73 70 61  (i=1; safe_isspa
2e038 63 65 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d  ce(z[i]); i++){}
2e039 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70  .      *tokenTyp
2e03a 65 20 3d 20 54 4f 4b 45 4e 5f 53 50 41 43 45 3b  e = TOKEN_SPACE;
2e03b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b  .      return i;
2e03c 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
2e03d 27 60 27 3a 0a 20 20 20 20 63 61 73 65 20 27 5c  '`':.    case '\
2e03e 27 27 3a 0a 20 20 20 20 63 61 73 65 20 27 22 27  '':.    case '"'
2e03f 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 65  : {.      int de
2e040 6c 69 6d 20 3d 20 7a 5b 30 5d 3b 0a 20 20 20 20  lim = z[0];.    
2e041 20 20 66 6f 72 28 69 3d 31 3b 20 28 63 3d 7a 5b    for(i=1; (c=z[
2e042 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20  i])!=0; i++){.  
2e043 20 20 20 20 20 20 69 66 28 20 63 3d 3d 64 65 6c        if( c==del
2e044 69 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  im ){.          
2e045 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 64 65 6c 69  if( z[i+1]==deli
2e046 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  m ){.           
2e047 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20   i++;.          
2e048 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
2e049 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e04a 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
2e04b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 74        }.      *t
2e04c 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4f 4b 45 4e  okenType = TOKEN
2e04d 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 20 20 72  _STRING;.      r
2e04e 65 74 75 72 6e 20 69 20 2b 20 28 63 21 3d 30 29  eturn i + (c!=0)
2e04f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
2e050 20 27 5b 27 3a 20 7b 0a 20 20 20 20 20 20 66 6f   '[': {.      fo
2e051 72 28 69 3d 31 2c 20 63 3d 7a 5b 30 5d 3b 20 63  r(i=1, c=z[0]; c
2e052 21 3d 27 5d 27 20 26 26 20 28 63 3d 7a 5b 69 5d  !=']' && (c=z[i]
2e053 29 21 3d 30 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20  )!=0; i++){}.   
2e054 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
2e055 54 4f 4b 45 4e 5f 49 44 3b 0a 20 20 20 20 20 20  TOKEN_ID;.      
2e056 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a  return i;.    }.
2e057 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
2e058 20 20 20 20 20 69 66 28 20 21 66 74 73 49 64 43       if( !ftsIdC
2e059 68 61 72 28 2a 7a 29 20 29 7b 0a 20 20 20 20 20  har(*z) ){.     
2e05a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e05b 7d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b  }.      for(i=1;
2e05c 20 66 74 73 49 64 43 68 61 72 28 7a 5b 69 5d 29   ftsIdChar(z[i])
2e05d 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 2a  ; i++){}.      *
2e05e 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4f 4b 45  tokenType = TOKE
2e05f 4e 5f 49 44 3b 0a 20 20 20 20 20 20 72 65 74 75  N_ID;.      retu
2e060 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rn i;.    }.  }.
2e061 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
2e062 4f 4b 45 4e 5f 50 55 4e 43 54 3b 0a 20 20 72 65  OKEN_PUNCT;.  re
2e063 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 1;.}../*.**
2e064 20 41 20 74 6f 6b 65 6e 20 65 78 74 72 61 63 74   A token extract
2e065 65 64 20 66 72 6f 6d 20 61 20 73 74 72 69 6e 67  ed from a string
2e066 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   is an instance 
2e067 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
2e068 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  .** structure..*
2e069 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
2e06a 20 46 74 73 54 6f 6b 65 6e 20 7b 0a 20 20 63 6f   FtsToken {.  co
2e06b 6e 73 74 20 63 68 61 72 20 2a 7a 3b 20 20 20 20  nst char *z;    
2e06c 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
2e06d 20 74 6f 6b 65 6e 20 74 65 78 74 2e 20 20 4e 6f   token text.  No
2e06e 74 20 27 5c 30 30 30 27 20 74 65 72 6d 69 6e 61  t '\000' termina
2e06f 74 65 64 20 2a 2f 0a 20 20 73 68 6f 72 74 20 69  ted */.  short i
2e070 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a  nt n;         /*
2e071 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 74   Length of the t
2e072 6f 6b 65 6e 20 74 65 78 74 20 69 6e 20 62 79 74  oken text in byt
2e073 65 73 2e 20 2a 2f 0a 7d 20 46 74 73 54 6f 6b 65  es. */.} FtsToke
2e074 6e 3b 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20  n;../*.** Given 
2e075 61 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 28  a input string (
2e076 77 68 69 63 68 20 69 73 20 72 65 61 6c 6c 79 20  which is really 
2e077 6f 6e 65 20 6f 66 20 74 68 65 20 61 72 67 76 5b  one of the argv[
2e078 5d 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20  ] parameters.** 
2e079 70 61 73 73 65 64 20 69 6e 74 6f 20 78 43 6f 6e  passed into xCon
2e07a 6e 65 63 74 20 6f 72 20 78 43 72 65 61 74 65 29  nect or xCreate)
2e07b 20 73 70 6c 69 74 20 74 68 65 20 73 74 72 69 6e   split the strin
2e07c 67 20 75 70 20 69 6e 74 6f 20 74 6f 6b 65 6e 73  g up into tokens
2e07d 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 61  ..** Return an a
2e07e 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 73  rray of pointers
2e07f 20 74 6f 20 27 5c 30 30 30 27 20 74 65 72 6d 69   to '\000' termi
2e080 6e 61 74 65 64 20 73 74 72 69 6e 67 73 2c 20 6f  nated strings, o
2e081 6e 65 20 73 74 72 69 6e 67 0a 2a 2a 20 66 6f 72  ne string.** for
2e082 20 65 61 63 68 20 6e 6f 6e 2d 77 68 69 74 65 73   each non-whites
2e083 70 61 63 65 20 74 6f 6b 65 6e 2e 0a 2a 2a 0a 2a  pace token..**.*
2e084 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 61  * The returned a
2e085 72 72 61 79 20 69 73 20 74 65 72 6d 69 6e 61 74  rray is terminat
2e086 65 64 20 62 79 20 61 20 73 69 6e 67 6c 65 20 4e  ed by a single N
2e087 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a  ULL pointer..**.
2e088 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64  ** Space to hold
2e089 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 61 72   the returned ar
2e08a 72 61 79 20 69 73 20 6f 62 74 61 69 6e 65 64 20  ray is obtained 
2e08b 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 0a 2a 2a  from a single.**
2e08c 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 73 68 6f 75   malloc and shou
2e08d 6c 64 20 62 65 20 66 72 65 65 64 20 62 79 20 70  ld be freed by p
2e08e 61 73 73 69 6e 67 20 74 68 65 20 72 65 74 75 72  assing the retur
2e08f 6e 20 76 61 6c 75 65 20 74 6f 20 66 72 65 65 28  n value to free(
2e090 29 2e 0a 2a 2a 20 54 68 65 20 69 6e 64 69 76 69  )..** The indivi
2e091 64 75 61 6c 20 73 74 72 69 6e 67 73 20 77 69 74  dual strings wit
2e092 68 69 6e 20 74 68 65 20 74 6f 6b 65 6e 20 6c 69  hin the token li
2e093 73 74 20 61 72 65 20 61 6c 6c 20 61 20 70 61 72  st are all a par
2e094 74 20 6f 66 0a 2a 2a 20 74 68 65 20 73 69 6e 67  t of.** the sing
2e095 6c 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  le memory alloca
2e096 74 69 6f 6e 20 61 6e 64 20 77 69 6c 6c 20 61 6c  tion and will al
2e097 6c 20 62 65 20 66 72 65 65 64 20 61 74 20 6f 6e  l be freed at on
2e098 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68  ce..*/.static ch
2e099 61 72 20 2a 2a 74 6f 6b 65 6e 69 7a 65 53 74 72  ar **tokenizeStr
2e09a 69 6e 67 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ing(const char *
2e09b 7a 2c 20 69 6e 74 20 2a 70 6e 54 6f 6b 65 6e 29  z, int *pnToken)
2e09c 7b 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 20 3d  {.  int nToken =
2e09d 20 30 3b 0a 20 20 46 74 73 54 6f 6b 65 6e 20 2a   0;.  FtsToken *
2e09e 61 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33  aToken = sqlite3
2e09f 5f 6d 61 6c 6c 6f 63 28 20 73 74 72 6c 65 6e 28  _malloc( strlen(
2e0a0 7a 29 20 2a 20 73 69 7a 65 6f 66 28 61 54 6f 6b  z) * sizeof(aTok
2e0a1 65 6e 5b 30 5d 29 20 29 3b 0a 20 20 69 6e 74 20  en[0]) );.  int 
2e0a2 6e 20 3d 20 31 3b 0a 20 20 69 6e 74 20 65 2c 20  n = 1;.  int e, 
2e0a3 69 3b 0a 20 20 69 6e 74 20 74 6f 74 61 6c 53 69  i;.  int totalSi
2e0a4 7a 65 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a  ze = 0;.  char *
2e0a5 2a 61 7a 54 6f 6b 65 6e 3b 0a 20 20 63 68 61 72  *azToken;.  char
2e0a6 20 2a 7a 43 6f 70 79 3b 0a 20 20 77 68 69 6c 65   *zCopy;.  while
2e0a7 28 20 6e 3e 30 20 29 7b 0a 20 20 20 20 6e 20 3d  ( n>0 ){.    n =
2e0a8 20 66 74 73 47 65 74 54 6f 6b 65 6e 28 7a 2c 20   ftsGetToken(z, 
2e0a9 26 65 29 3b 0a 20 20 20 20 69 66 28 20 65 21 3d  &e);.    if( e!=
2e0aa 54 4f 4b 45 4e 5f 53 50 41 43 45 20 29 7b 0a 20  TOKEN_SPACE ){. 
2e0ab 20 20 20 20 20 61 54 6f 6b 65 6e 5b 6e 54 6f 6b       aToken[nTok
2e0ac 65 6e 5d 2e 7a 20 3d 20 7a 3b 0a 20 20 20 20 20  en].z = z;.     
2e0ad 20 61 54 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d 2e   aToken[nToken].
2e0ae 6e 20 3d 20 6e 3b 0a 20 20 20 20 20 20 6e 54 6f  n = n;.      nTo
2e0af 6b 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 74 6f 74  ken++;.      tot
2e0b0 61 6c 53 69 7a 65 20 2b 3d 20 6e 2b 31 3b 0a 20  alSize += n+1;. 
2e0b1 20 20 20 7d 0a 20 20 20 20 7a 20 2b 3d 20 6e 3b     }.    z += n;
2e0b2 0a 20 20 7d 0a 20 20 61 7a 54 6f 6b 65 6e 20 3d  .  }.  azToken =
2e0b3 20 28 63 68 61 72 2a 2a 29 73 71 6c 69 74 65 33   (char**)sqlite3
2e0b4 5f 6d 61 6c 6c 6f 63 28 20 6e 54 6f 6b 65 6e 2a  _malloc( nToken*
2e0b5 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 20 2b 20  sizeof(char*) + 
2e0b6 74 6f 74 61 6c 53 69 7a 65 20 29 3b 0a 20 20 7a  totalSize );.  z
2e0b7 43 6f 70 79 20 3d 20 28 63 68 61 72 2a 29 26 61  Copy = (char*)&a
2e0b8 7a 54 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d 3b 0a  zToken[nToken];.
2e0b9 20 20 6e 54 6f 6b 65 6e 2d 2d 3b 0a 20 20 66 6f    nToken--;.  fo
2e0ba 72 28 69 3d 30 3b 20 69 3c 6e 54 6f 6b 65 6e 3b  r(i=0; i<nToken;
2e0bb 20 69 2b 2b 29 7b 0a 20 20 20 20 61 7a 54 6f 6b   i++){.    azTok
2e0bc 65 6e 5b 69 5d 20 3d 20 7a 43 6f 70 79 3b 0a 20  en[i] = zCopy;. 
2e0bd 20 20 20 6e 20 3d 20 61 54 6f 6b 65 6e 5b 69 5d     n = aToken[i]
2e0be 2e 6e 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a  .n;.    memcpy(z
2e0bf 43 6f 70 79 2c 20 61 54 6f 6b 65 6e 5b 69 5d 2e  Copy, aToken[i].
2e0c0 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a 43 6f 70 79  z, n);.    zCopy
2e0c1 5b 6e 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 43 6f  [n] = 0;.    zCo
2e0c2 70 79 20 2b 3d 20 6e 2b 31 3b 0a 20 20 7d 0a 20  py += n+1;.  }. 
2e0c3 20 61 7a 54 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d   azToken[nToken]
2e0c4 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f   = 0;.  sqlite3_
2e0c5 66 72 65 65 28 61 54 6f 6b 65 6e 29 3b 0a 20 20  free(aToken);.  
2e0c6 2a 70 6e 54 6f 6b 65 6e 20 3d 20 6e 54 6f 6b 65  *pnToken = nToke
2e0c7 6e 3b 0a 20 20 72 65 74 75 72 6e 20 61 7a 54 6f  n;.  return azTo
2e0c8 6b 65 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  ken;.}../*.** Co
2e0c9 6e 76 65 72 74 20 61 6e 20 53 51 4c 2d 73 74 79  nvert an SQL-sty
2e0ca 6c 65 20 71 75 6f 74 65 64 20 73 74 72 69 6e 67  le quoted string
2e0cb 20 69 6e 74 6f 20 61 20 6e 6f 72 6d 61 6c 20 73   into a normal s
2e0cc 74 72 69 6e 67 20 62 79 20 72 65 6d 6f 76 69 6e  tring by removin
2e0cd 67 0a 2a 2a 20 74 68 65 20 71 75 6f 74 65 20 63  g.** the quote c
2e0ce 68 61 72 61 63 74 65 72 73 2e 20 20 54 68 65 20  haracters.  The 
2e0cf 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 64 6f  conversion is do
2e0d0 6e 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20 49 66  ne in-place.  If
2e0d1 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 64 6f   the.** input do
2e0d2 65 73 20 6e 6f 74 20 62 65 67 69 6e 20 77 69 74  es not begin wit
2e0d3 68 20 61 20 71 75 6f 74 65 20 63 68 61 72 61 63  h a quote charac
2e0d4 74 65 72 2c 20 74 68 65 6e 20 74 68 69 73 20 72  ter, then this r
2e0d5 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e  outine.** is a n
2e0d6 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d  o-op..**.** Exam
2e0d7 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ples:.**.**     
2e0d8 22 61 62 63 22 20 20 20 62 65 63 6f 6d 65 73 20  "abc"   becomes 
2e0d9 20 20 61 62 63 0a 2a 2a 20 20 20 20 20 27 78 79    abc.**     'xy
2e0da 7a 27 20 20 20 62 65 63 6f 6d 65 73 20 20 20 78  z'   becomes   x
2e0db 79 7a 0a 2a 2a 20 20 20 20 20 5b 70 71 72 5d 20  yz.**     [pqr] 
2e0dc 20 20 62 65 63 6f 6d 65 73 20 20 20 70 71 72 0a    becomes   pqr.
2e0dd 2a 2a 20 20 20 20 20 60 6d 6e 6f 60 20 20 20 62  **     `mno`   b
2e0de 65 63 6f 6d 65 73 20 20 20 6d 6e 6f 0a 2a 2f 0a  ecomes   mno.*/.
2e0df 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 71 75  static void dequ
2e0e0 6f 74 65 53 74 72 69 6e 67 28 63 68 61 72 20 2a  oteString(char *
2e0e1 7a 29 7b 0a 20 20 69 6e 74 20 71 75 6f 74 65 3b  z){.  int quote;
2e0e2 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 69  .  int i, j;.  i
2e0e3 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( z==0 ) return
2e0e4 3b 0a 20 20 71 75 6f 74 65 20 3d 20 7a 5b 30 5d  ;.  quote = z[0]
2e0e5 3b 0a 20 20 73 77 69 74 63 68 28 20 71 75 6f 74  ;.  switch( quot
2e0e6 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 27 5c  e ){.    case '\
2e0e7 27 27 3a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20  '':  break;.    
2e0e8 63 61 73 65 20 27 22 27 3a 20 20 20 62 72 65 61  case '"':   brea
2e0e9 6b 3b 0a 20 20 20 20 63 61 73 65 20 27 60 27 3a  k;.    case '`':
2e0ea 20 20 20 62 72 65 61 6b 3b 20 20 20 20 20 20 20     break;       
2e0eb 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20           /* For 
2e0ec 4d 79 53 51 4c 20 63 6f 6d 70 61 74 69 62 69 6c  MySQL compatibil
2e0ed 69 74 79 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  ity */.    case 
2e0ee 27 5b 27 3a 20 20 20 71 75 6f 74 65 20 3d 20 27  '[':   quote = '
2e0ef 5d 27 3b 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20  ]';  break;  /* 
2e0f0 46 6f 72 20 4d 53 20 53 71 6c 53 65 72 76 65 72  For MS SqlServer
2e0f1 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2a   compatibility *
2e0f2 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20  /.    default:  
2e0f3 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
2e0f4 66 6f 72 28 69 3d 31 2c 20 6a 3d 30 3b 20 7a 5b  for(i=1, j=0; z[
2e0f5 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  i]; i++){.    if
2e0f6 28 20 7a 5b 69 5d 3d 3d 71 75 6f 74 65 20 29 7b  ( z[i]==quote ){
2e0f7 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31  .      if( z[i+1
2e0f8 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 20  ]==quote ){.    
2e0f9 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 71 75 6f      z[j++] = quo
2e0fa 74 65 3b 0a 20 20 20 20 20 20 20 20 69 2b 2b 3b  te;.        i++;
2e0fb 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2e0fc 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 30        z[j++] = 0
2e0fd 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
2e0fe 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
2e0ff 73 65 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d  se{.      z[j++]
2e100 20 3d 20 7a 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20   = z[i];.    }. 
2e101 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20   }.}../*.** The 
2e102 69 6e 70 75 74 20 61 7a 49 6e 20 69 73 20 61 20  input azIn is a 
2e103 4e 55 4c 4c 2d 74 65 72 6d 69 6e 61 74 65 64 20  NULL-terminated 
2e104 6c 69 73 74 20 6f 66 20 74 6f 6b 65 6e 73 2e 20  list of tokens. 
2e105 20 52 65 6d 6f 76 65 20 74 68 65 20 66 69 72 73   Remove the firs
2e106 74 0a 2a 2a 20 74 6f 6b 65 6e 20 61 6e 64 20 61  t.** token and a
2e107 6c 6c 20 70 75 6e 63 74 75 61 74 69 6f 6e 20 74  ll punctuation t
2e108 6f 6b 65 6e 73 2e 20 20 52 65 6d 6f 76 65 20 74  okens.  Remove t
2e109 68 65 20 71 75 6f 74 65 73 20 66 72 6f 6d 0a 2a  he quotes from.*
2e10a 2a 20 61 72 6f 75 6e 64 20 73 74 72 69 6e 67 20  * around string 
2e10b 6c 69 74 65 72 61 6c 20 74 6f 6b 65 6e 73 2e 0a  literal tokens..
2e10c 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 2a  **.** Example:.*
2e10d 2a 0a 2a 2a 20 20 20 20 20 69 6e 70 75 74 3a 20  *.**     input: 
2e10e 20 20 20 20 20 74 6f 6b 65 6e 69 7a 65 20 63 68       tokenize ch
2e10f 69 6e 65 73 65 20 28 20 27 73 69 6d 70 6c 69 66  inese ( 'simplif
2e110 65 64 27 20 2c 20 27 6d 69 78 65 64 27 20 29 0a  ed' , 'mixed' ).
2e111 2a 2a 20 20 20 20 20 6f 75 74 70 75 74 3a 20 20  **     output:  
2e112 20 20 20 63 68 69 6e 65 73 65 20 73 69 6d 70 6c     chinese simpl
2e113 69 66 65 64 20 6d 69 78 65 64 0a 2a 2a 0a 2a 2a  ifed mixed.**.**
2e114 20 41 6e 6f 74 68 65 72 20 65 78 61 6d 70 6c 65   Another example
2e115 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 69 6e 70 75  :.**.**     inpu
2e116 74 3a 20 20 20 20 20 20 64 65 6c 69 6d 69 74 65  t:      delimite
2e117 72 73 20 28 20 27 5b 27 20 2c 20 27 5d 27 20 2c  rs ( '[' , ']' ,
2e118 20 27 2e 2e 2e 27 20 29 0a 2a 2a 20 20 20 20 20   '...' ).**     
2e119 6f 75 74 70 75 74 3a 20 20 20 20 20 5b 20 5d 20  output:     [ ] 
2e11a 2e 2e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ....*/.static vo
2e11b 69 64 20 74 6f 6b 65 6e 4c 69 73 74 54 6f 49 64  id tokenListToId
2e11c 4c 69 73 74 28 63 68 61 72 20 2a 2a 61 7a 49 6e  List(char **azIn
2e11d 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20  ){.  int i, j;. 
2e11e 20 69 66 28 20 61 7a 49 6e 20 29 7b 0a 20 20 20   if( azIn ){.   
2e11f 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 2d 31 3b 20   for(i=0, j=-1; 
2e120 61 7a 49 6e 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20  azIn[i]; i++){. 
2e121 20 20 20 20 20 69 66 28 20 73 61 66 65 5f 69 73       if( safe_is
2e122 61 6c 6e 75 6d 28 61 7a 49 6e 5b 69 5d 5b 30 5d  alnum(azIn[i][0]
2e123 29 20 7c 7c 20 61 7a 49 6e 5b 69 5d 5b 31 5d 20  ) || azIn[i][1] 
2e124 29 7b 0a 20 20 20 20 20 20 20 20 64 65 71 75 6f  ){.        dequo
2e125 74 65 53 74 72 69 6e 67 28 61 7a 49 6e 5b 69 5d  teString(azIn[i]
2e126 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a  );.        if( j
2e127 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  >=0 ){.         
2e128 20 61 7a 49 6e 5b 6a 5d 20 3d 20 61 7a 49 6e 5b   azIn[j] = azIn[
2e129 69 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  i];.        }.  
2e12a 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20        j++;.     
2e12b 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 7a 49   }.    }.    azI
2e12c 6e 5b 6a 5d 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a  n[j] = 0;.  }.}.
2e12d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65  ../*.** Find the
2e12e 20 66 69 72 73 74 20 61 6c 70 68 61 6e 75 6d 65   first alphanume
2e12f 72 69 63 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65  ric token in the
2e130 20 73 74 72 69 6e 67 20 7a 49 6e 2e 20 20 4e 75   string zIn.  Nu
2e131 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20  ll-terminate.** 
2e132 74 68 69 73 20 74 6f 6b 65 6e 2e 20 20 52 65 6d  this token.  Rem
2e133 6f 76 65 20 61 6e 79 20 71 75 6f 74 61 74 69 6f  ove any quotatio
2e134 6e 20 6d 61 72 6b 73 2e 20 20 41 6e 64 20 72 65  n marks.  And re
2e135 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
2e136 6f 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 2e  o.** the result.
2e137 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20  .*/.static char 
2e138 2a 66 69 72 73 74 54 6f 6b 65 6e 28 63 68 61 72  *firstToken(char
2e139 20 2a 7a 49 6e 2c 20 63 68 61 72 20 2a 2a 70 7a   *zIn, char **pz
2e13a 54 61 69 6c 29 7b 0a 20 20 69 6e 74 20 6e 2c 20  Tail){.  int n, 
2e13b 74 74 79 70 65 3b 0a 20 20 77 68 69 6c 65 28 31  ttype;.  while(1
2e13c 29 7b 0a 20 20 20 20 6e 20 3d 20 66 74 73 47 65  ){.    n = ftsGe
2e13d 74 54 6f 6b 65 6e 28 7a 49 6e 2c 20 26 74 74 79  tToken(zIn, &tty
2e13e 70 65 29 3b 0a 20 20 20 20 69 66 28 20 74 74 79  pe);.    if( tty
2e13f 70 65 3d 3d 54 4f 4b 45 4e 5f 53 50 41 43 45 20  pe==TOKEN_SPACE 
2e140 29 7b 0a 20 20 20 20 20 20 7a 49 6e 20 2b 3d 20  ){.      zIn += 
2e141 6e 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  n;.    }else if(
2e142 20 74 74 79 70 65 3d 3d 54 4f 4b 45 4e 5f 45 4f   ttype==TOKEN_EO
2e143 46 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a 54 61  F ){.      *pzTa
2e144 69 6c 20 3d 20 7a 49 6e 3b 0a 20 20 20 20 20 20  il = zIn;.      
2e145 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 65  return 0;.    }e
2e146 6c 73 65 7b 0a 20 20 20 20 20 20 7a 49 6e 5b 6e  lse{.      zIn[n
2e147 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 2a 70 7a  ] = 0;.      *pz
2e148 54 61 69 6c 20 3d 20 26 7a 49 6e 5b 31 5d 3b 0a  Tail = &zIn[1];.
2e149 20 20 20 20 20 20 64 65 71 75 6f 74 65 53 74 72        dequoteStr
2e14a 69 6e 67 28 7a 49 6e 29 3b 0a 20 20 20 20 20 20  ing(zIn);.      
2e14b 72 65 74 75 72 6e 20 7a 49 6e 3b 0a 20 20 20 20  return zIn;.    
2e14c 7d 0a 20 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41  }.  }.  /*NOTREA
2e14d 43 48 45 44 2a 2f 0a 7d 0a 0a 2f 2a 20 52 65 74  CHED*/.}../* Ret
2e14e 75 72 6e 20 74 72 75 65 20 69 66 2e 2e 2e 0a 2a  urn true if....*
2e14f 2a 0a 2a 2a 20 20 20 2a 20 20 73 20 62 65 67 69  *.**   *  s begi
2e150 6e 73 20 77 69 74 68 20 74 68 65 20 73 74 72 69  ns with the stri
2e151 6e 67 20 74 2c 20 69 67 6e 6f 72 69 6e 67 20 63  ng t, ignoring c
2e152 61 73 65 0a 2a 2a 20 20 20 2a 20 20 73 20 69 73  ase.**   *  s is
2e153 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 74 0a 2a   longer than t.*
2e154 2a 20 20 20 2a 20 20 54 68 65 20 66 69 72 73 74  *   *  The first
2e155 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 73 20   character of s 
2e156 62 65 79 6f 6e 64 20 74 20 69 73 20 6e 6f 74 20  beyond t is not 
2e157 61 20 61 6c 70 68 61 6e 75 6d 65 72 69 63 0a 2a  a alphanumeric.*
2e158 2a 20 0a 2a 2a 20 49 67 6e 6f 72 65 20 6c 65 61  * .** Ignore lea
2e159 64 69 6e 67 20 73 70 61 63 65 20 69 6e 20 2a 73  ding space in *s
2e15a 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 70 75 74 20 69  ..**.** To put i
2e15b 74 20 61 6e 6f 74 68 65 72 20 77 61 79 2c 20 72  t another way, r
2e15c 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68  eturn true if th
2e15d 65 20 66 69 72 73 74 20 74 6f 6b 65 6e 20 6f 66  e first token of
2e15e 0a 2a 2a 20 73 5b 5d 20 69 73 20 74 5b 5d 2e 0a  .** s[] is t[]..
2e15f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 74  */.static int st
2e160 61 72 74 73 57 69 74 68 28 63 6f 6e 73 74 20 63  artsWith(const c
2e161 68 61 72 20 2a 73 2c 20 63 6f 6e 73 74 20 63 68  har *s, const ch
2e162 61 72 20 2a 74 29 7b 0a 20 20 77 68 69 6c 65 28  ar *t){.  while(
2e163 20 73 61 66 65 5f 69 73 73 70 61 63 65 28 2a 73   safe_isspace(*s
2e164 29 20 29 7b 20 73 2b 2b 3b 20 7d 0a 20 20 77 68  ) ){ s++; }.  wh
2e165 69 6c 65 28 20 2a 74 20 29 7b 0a 20 20 20 20 69  ile( *t ){.    i
2e166 66 28 20 73 61 66 65 5f 74 6f 6c 6f 77 65 72 28  f( safe_tolower(
2e167 2a 73 2b 2b 29 21 3d 73 61 66 65 5f 74 6f 6c 6f  *s++)!=safe_tolo
2e168 77 65 72 28 2a 74 2b 2b 29 20 29 20 72 65 74 75  wer(*t++) ) retu
2e169 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rn 0;.  }.  retu
2e16a 72 6e 20 2a 73 21 3d 27 5f 27 20 26 26 20 21 73  rn *s!='_' && !s
2e16b 61 66 65 5f 69 73 61 6c 6e 75 6d 28 2a 73 29 3b  afe_isalnum(*s);
2e16c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  .}../*.** An ins
2e16d 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74  tance of this st
2e16e 72 75 63 74 75 72 65 20 64 65 66 69 6e 65 73 20  ructure defines 
2e16f 74 68 65 20 22 73 70 65 63 22 20 6f 66 20 61 0a  the "spec" of a.
2e170 2a 2a 20 66 75 6c 6c 20 74 65 78 74 20 69 6e 64  ** full text ind
2e171 65 78 2e 20 20 54 68 69 73 20 73 74 72 75 63 74  ex.  This struct
2e172 75 72 65 20 69 73 20 70 6f 70 75 6c 61 74 65 64  ure is populated
2e173 20 62 79 20 70 61 72 73 65 53 70 65 63 0a 2a 2a   by parseSpec.**
2e174 20 61 6e 64 20 75 73 65 20 62 79 20 66 75 6c 6c   and use by full
2e175 74 65 78 74 43 6f 6e 6e 65 63 74 20 61 6e 64 20  textConnect and 
2e176 66 75 6c 6c 74 65 78 74 43 72 65 61 74 65 2e 0a  fulltextCreate..
2e177 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
2e178 74 20 54 61 62 6c 65 53 70 65 63 20 7b 0a 20 20  t TableSpec {.  
2e179 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b  const char *zDb;
2e17a 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 67 69           /* Logi
2e17b 63 61 6c 20 64 61 74 61 62 61 73 65 20 6e 61 6d  cal database nam
2e17c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
2e17d 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20  r *zName;       
2e17e 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66  /* Name of the f
2e17f 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78 20 2a  ull-text index *
2e180 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b  /.  int nColumn;
2e181 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e182 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  Number of column
2e183 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20  s to be indexed 
2e184 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f  */.  char **azCo
2e185 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a  lumn;         /*
2e186 20 4f 72 69 67 69 6e 61 6c 20 6e 61 6d 65 73 20   Original names 
2e187 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 62 65  of columns to be
2e188 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 63 68   indexed */.  ch
2e189 61 72 20 2a 2a 61 7a 43 6f 6e 74 65 6e 74 43 6f  ar **azContentCo
2e18a 6c 75 6d 6e 3b 20 20 2f 2a 20 43 6f 6c 75 6d 6e  lumn;  /* Column
2e18b 20 6e 61 6d 65 73 20 66 6f 72 20 25 5f 63 6f 6e   names for %_con
2e18c 74 65 6e 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a  tent */.  char *
2e18d 2a 61 7a 54 6f 6b 65 6e 69 7a 65 72 3b 20 20 20  *azTokenizer;   
2e18e 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 6f     /* Name of to
2e18f 6b 65 6e 69 7a 65 72 20 61 6e 64 20 69 74 73 20  kenizer and its 
2e190 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 7d 20 54  arguments */.} T
2e191 61 62 6c 65 53 70 65 63 3b 0a 0a 2f 2a 0a 2a 2a  ableSpec;../*.**
2e192 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6f 66 20   Reclaim all of 
2e193 74 68 65 20 6d 65 6d 6f 72 79 20 75 73 65 64 20  the memory used 
2e194 62 79 20 61 20 54 61 62 6c 65 53 70 65 63 0a 2a  by a TableSpec.*
2e195 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c  /.static void cl
2e196 65 61 72 54 61 62 6c 65 53 70 65 63 28 54 61 62  earTableSpec(Tab
2e197 6c 65 53 70 65 63 20 2a 70 29 20 7b 0a 20 20 73  leSpec *p) {.  s
2e198 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61  qlite3_free(p->a
2e199 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 20 73 71 6c 69  zColumn);.  sqli
2e19a 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 7a 43 6f  te3_free(p->azCo
2e19b 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20 20  ntentColumn);.  
2e19c 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e  sqlite3_free(p->
2e19d 61 7a 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 7d 0a  azTokenizer);.}.
2e19e 0a 2f 2a 20 50 61 72 73 65 20 61 20 43 52 45 41  ./* Parse a CREA
2e19f 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45  TE VIRTUAL TABLE
2e1a0 20 73 74 61 74 65 6d 65 6e 74 2c 20 77 68 69 63   statement, whic
2e1a1 68 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69  h looks like thi
2e1a2 73 3a 0a 20 2a 0a 20 2a 20 43 52 45 41 54 45 20  s:. *. * CREATE 
2e1a3 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 65 6d  VIRTUAL TABLE em
2e1a4 61 69 6c 0a 20 2a 20 20 20 20 20 20 20 20 55 53  ail. *        US
2e1a5 49 4e 47 20 66 74 73 33 28 73 75 62 6a 65 63 74  ING fts3(subject
2e1a6 2c 20 62 6f 64 79 2c 20 74 6f 6b 65 6e 69 7a 65  , body, tokenize
2e1a7 20 6d 79 74 6f 6b 65 6e 69 7a 65 72 28 6d 79 61   mytokenizer(mya
2e1a8 72 67 29 29 0a 20 2a 0a 20 2a 20 57 65 20 72 65  rg)). *. * We re
2e1a9 74 75 72 6e 20 70 61 72 73 65 64 20 69 6e 66 6f  turn parsed info
2e1aa 72 6d 61 74 69 6f 6e 20 69 6e 20 61 20 54 61 62  rmation in a Tab
2e1ab 6c 65 53 70 65 63 20 73 74 72 75 63 74 75 72 65  leSpec structure
2e1ac 2e 0a 20 2a 20 0a 20 2a 2f 0a 73 74 61 74 69 63  .. * . */.static
2e1ad 20 69 6e 74 20 70 61 72 73 65 53 70 65 63 28 54   int parseSpec(T
2e1ae 61 62 6c 65 53 70 65 63 20 2a 70 53 70 65 63 2c  ableSpec *pSpec,
2e1af 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74   int argc, const
2e1b0 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67   char *const*arg
2e1b1 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  v,.             
2e1b2 20 20 20 20 20 20 20 20 63 68 61 72 2a 2a 70 7a          char**pz
2e1b3 45 72 72 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6e  Err){.  int i, n
2e1b4 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20 2a 7a 44  ;.  char *z, *zD
2e1b5 75 6d 6d 79 3b 0a 20 20 63 68 61 72 20 2a 2a 61  ummy;.  char **a
2e1b6 7a 41 72 67 3b 0a 20 20 63 6f 6e 73 74 20 63 68  zArg;.  const ch
2e1b7 61 72 20 2a 7a 54 6f 6b 65 6e 69 7a 65 72 20 3d  ar *zTokenizer =
2e1b8 20 30 3b 20 20 20 20 2f 2a 20 61 72 67 76 5b 5d   0;    /* argv[]
2e1b9 20 65 6e 74 72 79 20 64 65 73 63 72 69 62 69 6e   entry describin
2e1ba 67 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20  g the tokenizer 
2e1bb 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72  */..  assert( ar
2e1bc 67 63 3e 3d 33 20 29 3b 0a 20 20 2f 2a 20 43 75  gc>=3 );.  /* Cu
2e1bd 72 72 65 6e 74 20 69 6e 74 65 72 66 61 63 65 3a  rrent interface:
2e1be 0a 20 20 2a 2a 20 61 72 67 76 5b 30 5d 20 2d 20  .  ** argv[0] - 
2e1bf 6d 6f 64 75 6c 65 20 6e 61 6d 65 0a 20 20 2a 2a  module name.  **
2e1c0 20 61 72 67 76 5b 31 5d 20 2d 20 64 61 74 61 62   argv[1] - datab
2e1c1 61 73 65 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 72  ase name.  ** ar
2e1c2 67 76 5b 32 5d 20 2d 20 74 61 62 6c 65 20 6e 61  gv[2] - table na
2e1c3 6d 65 0a 20 20 2a 2a 20 61 72 67 76 5b 33 2e 2e  me.  ** argv[3..
2e1c4 5d 20 2d 20 63 6f 6c 75 6d 6e 73 2c 20 6f 70 74  ] - columns, opt
2e1c5 69 6f 6e 61 6c 6c 79 20 66 6f 6c 6c 6f 77 65 64  ionally followed
2e1c6 20 62 79 20 74 6f 6b 65 6e 69 7a 65 72 20 73 70   by tokenizer sp
2e1c7 65 63 69 66 69 63 61 74 69 6f 6e 0a 20 20 2a 2a  ecification.  **
2e1c8 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 64               and
2e1c9 20 73 6e 69 70 70 65 74 20 64 65 6c 69 6d 69 74   snippet delimit
2e1ca 65 72 73 20 73 70 65 63 69 66 69 63 61 74 69 6f  ers specificatio
2e1cb 6e 2e 0a 20 20 2a 2f 0a 0a 20 20 2f 2a 20 4d 61  n..  */..  /* Ma
2e1cc 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  ke a copy of the
2e1cd 20 63 6f 6d 70 6c 65 74 65 20 61 72 67 76 5b 5d   complete argv[]
2e1ce 5b 5d 20 61 72 72 61 79 20 69 6e 20 61 20 73 69  [] array in a si
2e1cf 6e 67 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e  ngle allocation.
2e1d0 0a 20 20 2a 2a 20 54 68 65 20 61 72 67 76 5b 5d  .  ** The argv[]
2e1d1 5b 5d 20 61 72 72 61 79 20 69 73 20 72 65 61 64  [] array is read
2e1d2 2d 6f 6e 6c 79 20 61 6e 64 20 74 72 61 6e 73 69  -only and transi
2e1d3 65 6e 74 2e 20 20 57 65 20 63 61 6e 20 77 72 69  ent.  We can wri
2e1d4 74 65 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 63  te to the.  ** c
2e1d5 6f 70 79 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  opy in order to 
2e1d6 6d 6f 64 69 66 79 20 74 68 69 6e 67 73 20 61 6e  modify things an
2e1d7 64 20 74 68 65 20 63 6f 70 79 20 69 73 20 70 65  d the copy is pe
2e1d8 72 73 69 73 74 65 6e 74 2e 0a 20 20 2a 2f 0a 20  rsistent..  */. 
2e1d9 20 43 4c 45 41 52 28 70 53 70 65 63 29 3b 0a 20   CLEAR(pSpec);. 
2e1da 20 66 6f 72 28 69 3d 6e 3d 30 3b 20 69 3c 61 72   for(i=n=0; i<ar
2e1db 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 20  gc; i++){.    n 
2e1dc 2b 3d 20 73 74 72 6c 65 6e 28 61 72 67 76 5b 69  += strlen(argv[i
2e1dd 5d 29 20 2b 20 31 3b 0a 20 20 7d 0a 20 20 61 7a  ]) + 1;.  }.  az
2e1de 41 72 67 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  Arg = sqlite3_ma
2e1df 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 63 68 61  lloc( sizeof(cha
2e1e0 72 2a 29 2a 61 72 67 63 20 2b 20 6e 20 29 3b 0a  r*)*argc + n );.
2e1e1 20 20 69 66 28 20 61 7a 41 72 67 3d 3d 30 20 29    if( azArg==0 )
2e1e2 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
2e1e3 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
2e1e4 20 7a 20 3d 20 28 63 68 61 72 2a 29 26 61 7a 41   z = (char*)&azA
2e1e5 72 67 5b 61 72 67 63 5d 3b 0a 20 20 66 6f 72 28  rg[argc];.  for(
2e1e6 69 3d 30 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b  i=0; i<argc; i++
2e1e7 29 7b 0a 20 20 20 20 61 7a 41 72 67 5b 69 5d 20  ){.    azArg[i] 
2e1e8 3d 20 7a 3b 0a 20 20 20 20 73 74 72 63 70 79 28  = z;.    strcpy(
2e1e9 7a 2c 20 61 72 67 76 5b 69 5d 29 3b 0a 20 20 20  z, argv[i]);.   
2e1ea 20 7a 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 29 2b   z += strlen(z)+
2e1eb 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 64 65  1;.  }..  /* Ide
2e1ec 6e 74 69 66 79 20 74 68 65 20 63 6f 6c 75 6d 6e  ntify the column
2e1ed 20 6e 61 6d 65 73 20 61 6e 64 20 74 68 65 20 74   names and the t
2e1ee 6f 6b 65 6e 69 7a 65 72 20 61 6e 64 20 64 65 6c  okenizer and del
2e1ef 69 6d 69 74 65 72 20 61 72 67 75 6d 65 6e 74 73  imiter arguments
2e1f0 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 61 72 67  .  ** in the arg
2e1f1 76 5b 5d 5b 5d 20 61 72 72 61 79 2e 0a 20 20 2a  v[][] array..  *
2e1f2 2f 0a 20 20 70 53 70 65 63 2d 3e 7a 44 62 20 3d  /.  pSpec->zDb =
2e1f3 20 61 7a 41 72 67 5b 31 5d 3b 0a 20 20 70 53 70   azArg[1];.  pSp
2e1f4 65 63 2d 3e 7a 4e 61 6d 65 20 3d 20 61 7a 41 72  ec->zName = azAr
2e1f5 67 5b 32 5d 3b 0a 20 20 70 53 70 65 63 2d 3e 6e  g[2];.  pSpec->n
2e1f6 43 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20 70 53  Column = 0;.  pS
2e1f7 70 65 63 2d 3e 61 7a 43 6f 6c 75 6d 6e 20 3d 20  pec->azColumn = 
2e1f8 61 7a 41 72 67 3b 0a 20 20 7a 54 6f 6b 65 6e 69  azArg;.  zTokeni
2e1f9 7a 65 72 20 3d 20 22 74 6f 6b 65 6e 69 7a 65 20  zer = "tokenize 
2e1fa 73 69 6d 70 6c 65 22 3b 0a 20 20 66 6f 72 28 69  simple";.  for(i
2e1fb 3d 33 3b 20 69 3c 61 72 67 63 3b 20 2b 2b 69 29  =3; i<argc; ++i)
2e1fc 7b 0a 20 20 20 20 69 66 28 20 73 74 61 72 74 73  {.    if( starts
2e1fd 57 69 74 68 28 61 7a 41 72 67 5b 69 5d 2c 22 74  With(azArg[i],"t
2e1fe 6f 6b 65 6e 69 7a 65 22 29 20 29 7b 0a 20 20 20  okenize") ){.   
2e1ff 20 20 20 7a 54 6f 6b 65 6e 69 7a 65 72 20 3d 20     zTokenizer = 
2e200 61 7a 41 72 67 5b 69 5d 3b 0a 20 20 20 20 7d 65  azArg[i];.    }e
2e201 6c 73 65 7b 0a 20 20 20 20 20 20 7a 20 3d 20 61  lse{.      z = a
2e202 7a 41 72 67 5b 70 53 70 65 63 2d 3e 6e 43 6f 6c  zArg[pSpec->nCol
2e203 75 6d 6e 5d 20 3d 20 66 69 72 73 74 54 6f 6b 65  umn] = firstToke
2e204 6e 28 61 7a 41 72 67 5b 69 5d 2c 20 26 7a 44 75  n(azArg[i], &zDu
2e205 6d 6d 79 29 3b 0a 20 20 20 20 20 20 70 53 70 65  mmy);.      pSpe
2e206 63 2d 3e 6e 43 6f 6c 75 6d 6e 2b 2b 3b 0a 20 20  c->nColumn++;.  
2e207 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 53    }.  }.  if( pS
2e208 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 30 20  pec->nColumn==0 
2e209 29 7b 0a 20 20 20 20 61 7a 41 72 67 5b 30 5d 20  ){.    azArg[0] 
2e20a 3d 20 22 63 6f 6e 74 65 6e 74 22 3b 0a 20 20 20  = "content";.   
2e20b 20 70 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 20   pSpec->nColumn 
2e20c 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20  = 1;.  }..  /*. 
2e20d 20 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 74 68   ** Construct th
2e20e 65 20 6c 69 73 74 20 6f 66 20 63 6f 6e 74 65 6e  e list of conten
2e20f 74 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 0a  t column names..
2e210 20 20 2a 2a 0a 20 20 2a 2a 20 45 61 63 68 20 63    **.  ** Each c
2e211 6f 6e 74 65 6e 74 20 63 6f 6c 75 6d 6e 20 6e 61  ontent column na
2e212 6d 65 20 77 69 6c 6c 20 62 65 20 6f 66 20 74 68  me will be of th
2e213 65 20 66 6f 72 6d 20 63 4e 4e 41 41 41 41 0a 20  e form cNNAAAA. 
2e214 20 2a 2a 20 77 68 65 72 65 20 4e 4e 20 69 73 20   ** where NN is 
2e215 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65  the column numbe
2e216 72 20 61 6e 64 20 41 41 41 41 20 69 73 20 74 68  r and AAAA is th
2e217 65 20 73 61 6e 69 74 69 7a 65 64 0a 20 20 2a 2a  e sanitized.  **
2e218 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 20 20 22   column name.  "
2e219 73 61 6e 69 74 69 7a 65 64 22 20 6d 65 61 6e 73  sanitized" means
2e21a 20 74 68 61 74 20 73 70 65 63 69 61 6c 20 63 68   that special ch
2e21b 61 72 61 63 74 65 72 73 20 61 72 65 0a 20 20 2a  aracters are.  *
2e21c 2a 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 22  * converted to "
2e21d 5f 22 2e 20 20 54 68 65 20 63 4e 4e 20 70 72 65  _".  The cNN pre
2e21e 66 69 78 20 67 75 61 72 61 6e 74 65 65 73 20 74  fix guarantees t
2e21f 68 61 74 20 61 6c 6c 20 63 6f 6c 75 6d 6e 0a 20  hat all column. 
2e220 20 2a 2a 20 6e 61 6d 65 73 20 61 72 65 20 75 6e   ** names are un
2e221 69 71 75 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ique..  **.  ** 
2e222 54 68 65 20 41 41 41 41 20 73 75 66 66 69 78 20  The AAAA suffix 
2e223 69 73 20 6e 6f 74 20 73 74 72 69 63 74 6c 79 20  is not strictly 
2e224 6e 65 63 65 73 73 61 72 79 2e 20 20 49 74 20 69  necessary.  It i
2e225 73 20 69 6e 63 6c 75 64 65 64 0a 20 20 2a 2a 20  s included.  ** 
2e226 66 6f 72 20 74 68 65 20 63 6f 6e 76 65 6e 69 65  for the convenie
2e227 6e 63 65 20 6f 66 20 70 65 6f 70 6c 65 20 77 68  nce of people wh
2e228 6f 20 6d 69 67 68 74 20 65 78 61 6d 69 6e 65 20  o might examine 
2e229 74 68 65 20 67 65 6e 65 72 61 74 65 64 0a 20 20  the generated.  
2e22a 2a 2a 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62  ** %_content tab
2e22b 6c 65 20 61 6e 64 20 77 6f 6e 64 65 72 20 77 68  le and wonder wh
2e22c 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 61  at the columns a
2e22d 72 65 20 75 73 65 64 20 66 6f 72 2e 0a 20 20 2a  re used for..  *
2e22e 2f 0a 20 20 70 53 70 65 63 2d 3e 61 7a 43 6f 6e  /.  pSpec->azCon
2e22f 74 65 6e 74 43 6f 6c 75 6d 6e 20 3d 20 73 71 6c  tentColumn = sql
2e230 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 70 53 70  ite3_malloc( pSp
2e231 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 20 2a 20 73 69  ec->nColumn * si
2e232 7a 65 6f 66 28 63 68 61 72 20 2a 29 20 29 3b 0a  zeof(char *) );.
2e233 20 20 69 66 28 20 70 53 70 65 63 2d 3e 61 7a 43    if( pSpec->azC
2e234 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 3d 3d 30 20  ontentColumn==0 
2e235 29 7b 0a 20 20 20 20 63 6c 65 61 72 54 61 62 6c  ){.    clearTabl
2e236 65 53 70 65 63 28 70 53 70 65 63 29 3b 0a 20 20  eSpec(pSpec);.  
2e237 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2e238 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 66 6f 72  NOMEM;.  }.  for
2e239 28 69 3d 30 3b 20 69 3c 70 53 70 65 63 2d 3e 6e  (i=0; i<pSpec->n
2e23a 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
2e23b 20 20 63 68 61 72 20 2a 70 3b 0a 20 20 20 20 70    char *p;.    p
2e23c 53 70 65 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43  Spec->azContentC
2e23d 6f 6c 75 6d 6e 5b 69 5d 20 3d 20 73 71 6c 69 74  olumn[i] = sqlit
2e23e 65 33 5f 6d 70 72 69 6e 74 66 28 22 63 25 64 25  e3_mprintf("c%d%
2e23f 73 22 2c 20 69 2c 20 61 7a 41 72 67 5b 69 5d 29  s", i, azArg[i])
2e240 3b 0a 20 20 20 20 66 6f 72 20 28 70 20 3d 20 70  ;.    for (p = p
2e241 53 70 65 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43  Spec->azContentC
2e242 6f 6c 75 6d 6e 5b 69 5d 3b 20 2a 70 20 3b 20 2b  olumn[i]; *p ; +
2e243 2b 70 29 20 7b 0a 20 20 20 20 20 20 69 66 28 20  +p) {.      if( 
2e244 21 73 61 66 65 5f 69 73 61 6c 6e 75 6d 28 2a 70  !safe_isalnum(*p
2e245 29 20 29 20 2a 70 20 3d 20 27 5f 27 3b 0a 20 20  ) ) *p = '_';.  
2e246 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20    }.  }..  /*.  
2e247 2a 2a 20 50 61 72 73 65 20 74 68 65 20 74 6f 6b  ** Parse the tok
2e248 65 6e 69 7a 65 72 20 73 70 65 63 69 66 69 63 61  enizer specifica
2e249 74 69 6f 6e 20 73 74 72 69 6e 67 2e 0a 20 20 2a  tion string..  *
2e24a 2f 0a 20 20 70 53 70 65 63 2d 3e 61 7a 54 6f 6b  /.  pSpec->azTok
2e24b 65 6e 69 7a 65 72 20 3d 20 74 6f 6b 65 6e 69 7a  enizer = tokeniz
2e24c 65 53 74 72 69 6e 67 28 7a 54 6f 6b 65 6e 69 7a  eString(zTokeniz
2e24d 65 72 2c 20 26 6e 29 3b 0a 20 20 74 6f 6b 65 6e  er, &n);.  token
2e24e 4c 69 73 74 54 6f 49 64 4c 69 73 74 28 70 53 70  ListToIdList(pSp
2e24f 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72 29  ec->azTokenizer)
2e250 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ;..  return SQLI
2e251 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
2e252 47 65 6e 65 72 61 74 65 20 61 20 43 52 45 41 54  Generate a CREAT
2e253 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  E TABLE statemen
2e254 74 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73  t that describes
2e255 20 74 68 65 20 73 63 68 65 6d 61 20 6f 66 0a 2a   the schema of.*
2e256 2a 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  * the virtual ta
2e257 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 61 20 70  ble.  Return a p
2e258 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73  ointer to this s
2e259 63 68 65 6d 61 20 73 74 72 69 6e 67 2e 0a 2a 2a  chema string..**
2e25a 0a 2a 2a 20 53 70 61 63 65 20 69 73 20 6f 62 74  .** Space is obt
2e25b 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
2e25c 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 61 6e 64  e3_mprintf() and
2e25d 20 73 68 6f 75 6c 64 20 62 65 20 66 72 65 65 64   should be freed
2e25e 0a 2a 2a 20 75 73 69 6e 67 20 73 71 6c 69 74 65  .** using sqlite
2e25f 33 5f 66 72 65 65 28 29 2e 0a 2a 2f 0a 73 74 61  3_free()..*/.sta
2e260 74 69 63 20 63 68 61 72 20 2a 66 75 6c 6c 74 65  tic char *fullte
2e261 78 74 53 63 68 65 6d 61 28 0a 20 20 69 6e 74 20  xtSchema(.  int 
2e262 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20  nColumn,        
2e263 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
2e264 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a  ber of columns *
2e265 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
2e266 63 6f 6e 73 74 2a 20 61 7a 43 6f 6c 75 6d 6e 2c  const* azColumn,
2e267 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 63 6f 6c    /* List of col
2e268 75 6d 6e 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  umns */.  const 
2e269 63 68 61 72 20 2a 7a 54 61 62 6c 65 4e 61 6d 65  char *zTableName
2e26a 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
2e26b 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  of the table */.
2e26c 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 68  ){.  int i;.  ch
2e26d 61 72 20 2a 7a 53 63 68 65 6d 61 2c 20 2a 7a 4e  ar *zSchema, *zN
2e26e 65 78 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  ext;.  const cha
2e26f 72 20 2a 7a 53 65 70 20 3d 20 22 28 22 3b 0a 20  r *zSep = "(";. 
2e270 20 7a 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74   zSchema = sqlit
2e271 65 33 5f 6d 70 72 69 6e 74 66 28 22 43 52 45 41  e3_mprintf("CREA
2e272 54 45 20 54 41 42 4c 45 20 78 22 29 3b 0a 20 20  TE TABLE x");.  
2e273 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75  for(i=0; i<nColu
2e274 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 7a 4e  mn; i++){.    zN
2e275 65 78 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  ext = sqlite3_mp
2e276 72 69 6e 74 66 28 22 25 73 25 73 25 51 22 2c 20  rintf("%s%s%Q", 
2e277 7a 53 63 68 65 6d 61 2c 20 7a 53 65 70 2c 20 61  zSchema, zSep, a
2e278 7a 43 6f 6c 75 6d 6e 5b 69 5d 29 3b 0a 20 20 20  zColumn[i]);.   
2e279 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53   sqlite3_free(zS
2e27a 63 68 65 6d 61 29 3b 0a 20 20 20 20 7a 53 63 68  chema);.    zSch
2e27b 65 6d 61 20 3d 20 7a 4e 65 78 74 3b 0a 20 20 20  ema = zNext;.   
2e27c 20 7a 53 65 70 20 3d 20 22 2c 22 3b 0a 20 20 7d   zSep = ",";.  }
2e27d 0a 20 20 7a 4e 65 78 74 20 3d 20 73 71 6c 69 74  .  zNext = sqlit
2e27e 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 2c 25  e3_mprintf("%s,%
2e27f 51 20 48 49 44 44 45 4e 22 2c 20 7a 53 63 68 65  Q HIDDEN", zSche
2e280 6d 61 2c 20 7a 54 61 62 6c 65 4e 61 6d 65 29 3b  ma, zTableName);
2e281 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
2e282 7a 53 63 68 65 6d 61 29 3b 0a 20 20 7a 53 63 68  zSchema);.  zSch
2e283 65 6d 61 20 3d 20 7a 4e 65 78 74 3b 0a 20 20 7a  ema = zNext;.  z
2e284 4e 65 78 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d  Next = sqlite3_m
2e285 70 72 69 6e 74 66 28 22 25 73 2c 64 6f 63 69 64  printf("%s,docid
2e286 20 48 49 44 44 45 4e 29 22 2c 20 7a 53 63 68 65   HIDDEN)", zSche
2e287 6d 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ma);.  sqlite3_f
2e288 72 65 65 28 7a 53 63 68 65 6d 61 29 3b 0a 20 20  ree(zSchema);.  
2e289 72 65 74 75 72 6e 20 7a 4e 65 78 74 3b 0a 7d 0a  return zNext;.}.
2e28a 0a 2f 2a 0a 2a 2a 20 42 75 69 6c 64 20 61 20 6e  ./*.** Build a n
2e28b 65 77 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  ew sqlite3_vtab 
2e28c 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 77  structure that w
2e28d 69 6c 6c 20 64 65 73 63 72 69 62 65 20 74 68 65  ill describe the
2e28e 0a 2a 2a 20 66 75 6c 6c 74 65 78 74 20 69 6e 64  .** fulltext ind
2e28f 65 78 20 64 65 66 69 6e 65 64 20 62 79 20 73 70  ex defined by sp
2e290 65 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ec..*/.static in
2e291 74 20 63 6f 6e 73 74 72 75 63 74 56 74 61 62 28  t constructVtab(
2e292 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
2e293 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e294 54 68 65 20 53 51 4c 69 74 65 20 64 61 74 61 62  The SQLite datab
2e295 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
2e296 2f 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70 48  /.  fts3Hash *pH
2e297 61 73 68 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ash,          /*
2e298 20 48 61 73 68 20 74 61 62 6c 65 20 63 6f 6e 74   Hash table cont
2e299 61 69 6e 69 6e 67 20 74 6f 6b 65 6e 69 7a 65 72  aining tokenizer
2e29a 73 20 2a 2f 0a 20 20 54 61 62 6c 65 53 70 65 63  s */.  TableSpec
2e29b 20 2a 73 70 65 63 2c 20 20 20 20 20 20 20 20 20   *spec,         
2e29c 20 2f 2a 20 50 61 72 73 65 64 20 73 70 65 63 20   /* Parsed spec 
2e29d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d  information from
2e29e 20 70 61 72 73 65 53 70 65 63 28 29 20 2a 2f 0a   parseSpec() */.
2e29f 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
2e2a0 2a 70 70 56 54 61 62 2c 20 20 20 20 2f 2a 20 57  *ppVTab,    /* W
2e2a1 72 69 74 65 20 74 68 65 20 72 65 73 75 6c 74 69  rite the resulti
2e2a2 6e 67 20 76 74 61 62 20 73 74 72 75 63 74 75 72  ng vtab structur
2e2a3 65 20 68 65 72 65 20 2a 2f 0a 20 20 63 68 61 72  e here */.  char
2e2a4 20 2a 2a 70 7a 45 72 72 20 20 20 20 20 20 20 20   **pzErr        
2e2a5 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 61        /* Write a
2e2a6 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  ny error message
2e2a7 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e   here */.){.  in
2e2a8 74 20 72 63 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20  t rc;.  int n;. 
2e2a9 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a   fulltext_vtab *
2e2aa 76 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 73  v = 0;.  const s
2e2ab 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
2e2ac 5f 6d 6f 64 75 6c 65 20 2a 6d 20 3d 20 4e 55 4c  _module *m = NUL
2e2ad 4c 3b 0a 20 20 63 68 61 72 20 2a 73 63 68 65 6d  L;.  char *schem
2e2ae 61 3b 0a 0a 20 20 63 68 61 72 20 63 6f 6e 73 74  a;..  char const
2e2af 20 2a 7a 54 6f 6b 3b 20 20 20 20 20 20 20 20 20   *zTok;         
2e2b0 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 6f 6b 65 6e  /* Name of token
2e2b1 69 7a 65 72 20 74 6f 20 75 73 65 20 66 6f 72 20  izer to use for 
2e2b2 74 68 69 73 20 66 74 73 20 74 61 62 6c 65 20 2a  this fts table *
2e2b3 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b 3b 20 20 20  /.  int nTok;   
2e2b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2e2b5 20 4c 65 6e 67 74 68 20 6f 66 20 7a 54 6f 6b 2c   Length of zTok,
2e2b6 20 69 6e 63 6c 75 64 69 6e 67 20 6e 75 6c 20 74   including nul t
2e2b7 65 72 6d 69 6e 61 74 6f 72 20 2a 2f 0a 0a 20 20  erminator */..  
2e2b8 76 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 76 74  v = (fulltext_vt
2e2b9 61 62 20 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61  ab *) sqlite3_ma
2e2ba 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 66 75 6c 6c  lloc(sizeof(full
2e2bb 74 65 78 74 5f 76 74 61 62 29 29 3b 0a 20 20 69  text_vtab));.  i
2e2bc 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( v==0 ) return
2e2bd 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
2e2be 20 43 4c 45 41 52 28 76 29 3b 0a 20 20 2f 2a 20   CLEAR(v);.  /* 
2e2bf 73 71 6c 69 74 65 20 77 69 6c 6c 20 69 6e 69 74  sqlite will init
2e2c0 69 61 6c 69 7a 65 20 76 2d 3e 62 61 73 65 20 2a  ialize v->base *
2e2c1 2f 0a 20 20 76 2d 3e 64 62 20 3d 20 64 62 3b 0a  /.  v->db = db;.
2e2c2 20 20 76 2d 3e 7a 44 62 20 3d 20 73 70 65 63 2d    v->zDb = spec-
2e2c3 3e 7a 44 62 3b 20 20 20 20 20 20 20 2f 2a 20 46  >zDb;       /* F
2e2c4 72 65 65 64 20 77 68 65 6e 20 61 7a 43 6f 6c 75  reed when azColu
2e2c5 6d 6e 20 69 73 20 66 72 65 65 64 20 2a 2f 0a 20  mn is freed */. 
2e2c6 20 76 2d 3e 7a 4e 61 6d 65 20 3d 20 73 70 65 63   v->zName = spec
2e2c7 2d 3e 7a 4e 61 6d 65 3b 20 20 20 2f 2a 20 46 72  ->zName;   /* Fr
2e2c8 65 65 64 20 77 68 65 6e 20 61 7a 43 6f 6c 75 6d  eed when azColum
2e2c9 6e 20 69 73 20 66 72 65 65 64 20 2a 2f 0a 20 20  n is freed */.  
2e2ca 76 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 73 70 65  v->nColumn = spe
2e2cb 63 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 76 2d  c->nColumn;.  v-
2e2cc 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e  >azContentColumn
2e2cd 20 3d 20 73 70 65 63 2d 3e 61 7a 43 6f 6e 74 65   = spec->azConte
2e2ce 6e 74 43 6f 6c 75 6d 6e 3b 0a 20 20 73 70 65 63  ntColumn;.  spec
2e2cf 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d  ->azContentColum
2e2d0 6e 20 3d 20 30 3b 0a 20 20 76 2d 3e 61 7a 43 6f  n = 0;.  v->azCo
2e2d1 6c 75 6d 6e 20 3d 20 73 70 65 63 2d 3e 61 7a 43  lumn = spec->azC
2e2d2 6f 6c 75 6d 6e 3b 0a 20 20 73 70 65 63 2d 3e 61  olumn;.  spec->a
2e2d3 7a 43 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 0a 20 20  zColumn = 0;..  
2e2d4 69 66 28 20 73 70 65 63 2d 3e 61 7a 54 6f 6b 65  if( spec->azToke
2e2d5 6e 69 7a 65 72 3d 3d 30 20 29 7b 0a 20 20 20 20  nizer==0 ){.    
2e2d6 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
2e2d7 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 7a 54 6f 6b  MEM;.  }..  zTok
2e2d8 20 3d 20 73 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e   = spec->azToken
2e2d9 69 7a 65 72 5b 30 5d 3b 20 0a 20 20 69 66 28 20  izer[0]; .  if( 
2e2da 21 7a 54 6f 6b 20 29 7b 0a 20 20 20 20 7a 54 6f  !zTok ){.    zTo
2e2db 6b 20 3d 20 22 73 69 6d 70 6c 65 22 3b 0a 20 20  k = "simple";.  
2e2dc 7d 0a 20 20 6e 54 6f 6b 20 3d 20 73 74 72 6c 65  }.  nTok = strle
2e2dd 6e 28 7a 54 6f 6b 29 2b 31 3b 0a 0a 20 20 6d 20  n(zTok)+1;..  m 
2e2de 3d 20 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  = (sqlite3_token
2e2df 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 29 73 71  izer_module *)sq
2e2e0 6c 69 74 65 33 46 74 73 33 48 61 73 68 46 69 6e  lite3Fts3HashFin
2e2e1 64 28 70 48 61 73 68 2c 20 7a 54 6f 6b 2c 20 6e  d(pHash, zTok, n
2e2e2 54 6f 6b 29 3b 0a 20 20 69 66 28 20 21 6d 20 29  Tok);.  if( !m )
2e2e3 7b 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73  {.    *pzErr = s
2e2e4 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
2e2e5 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65  unknown tokenize
2e2e6 72 3a 20 25 73 22 2c 20 73 70 65 63 2d 3e 61 7a  r: %s", spec->az
2e2e7 54 6f 6b 65 6e 69 7a 65 72 5b 30 5d 29 3b 0a 20  Tokenizer[0]);. 
2e2e8 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
2e2e9 52 52 4f 52 3b 0a 20 20 20 20 67 6f 74 6f 20 65  RROR;.    goto e
2e2ea 72 72 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 6e  rr;.  }..  for(n
2e2eb 3d 30 3b 20 73 70 65 63 2d 3e 61 7a 54 6f 6b 65  =0; spec->azToke
2e2ec 6e 69 7a 65 72 5b 6e 5d 3b 20 6e 2b 2b 29 7b 7d  nizer[n]; n++){}
2e2ed 0a 20 20 69 66 28 20 6e 20 29 7b 0a 20 20 20 20  .  if( n ){.    
2e2ee 72 63 20 3d 20 6d 2d 3e 78 43 72 65 61 74 65 28  rc = m->xCreate(
2e2ef 6e 2d 31 2c 20 28 63 6f 6e 73 74 20 63 68 61 72  n-1, (const char
2e2f0 2a 63 6f 6e 73 74 2a 29 26 73 70 65 63 2d 3e 61  *const*)&spec->a
2e2f1 7a 54 6f 6b 65 6e 69 7a 65 72 5b 31 5d 2c 0a 20  zTokenizer[1],. 
2e2f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e2f3 20 20 20 26 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65     &v->pTokenize
2e2f4 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  r);.  }else{.   
2e2f5 20 72 63 20 3d 20 6d 2d 3e 78 43 72 65 61 74 65   rc = m->xCreate
2e2f6 28 30 2c 20 30 2c 20 26 76 2d 3e 70 54 6f 6b 65  (0, 0, &v->pToke
2e2f7 6e 69 7a 65 72 29 3b 0a 20 20 7d 0a 20 20 69 66  nizer);.  }.  if
2e2f8 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2e2f9 29 20 67 6f 74 6f 20 65 72 72 3b 0a 20 20 76 2d  ) goto err;.  v-
2e2fa 3e 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f  >pTokenizer->pMo
2e2fb 64 75 6c 65 20 3d 20 6d 3b 0a 0a 20 20 2f 2a 20  dule = m;..  /* 
2e2fc 54 4f 44 4f 3a 20 76 65 72 69 66 79 20 74 68 65  TODO: verify the
2e2fd 20 65 78 69 73 74 65 6e 63 65 20 6f 66 20 62 61   existence of ba
2e2fe 63 6b 69 6e 67 20 74 61 62 6c 65 73 20 66 6f 6f  cking tables foo
2e2ff 5f 63 6f 6e 74 65 6e 74 2c 20 66 6f 6f 5f 74 65  _content, foo_te
2e300 72 6d 20 2a 2f 0a 0a 20 20 73 63 68 65 6d 61 20  rm */..  schema 
2e301 3d 20 66 75 6c 6c 74 65 78 74 53 63 68 65 6d 61  = fulltextSchema
2e302 28 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 28 63 6f  (v->nColumn, (co
2e303 6e 73 74 20 63 68 61 72 2a 63 6f 6e 73 74 2a 29  nst char*const*)
2e304 76 2d 3e 61 7a 43 6f 6c 75 6d 6e 2c 0a 20 20 20  v->azColumn,.   
2e305 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e306 20 20 20 20 20 20 20 73 70 65 63 2d 3e 7a 4e 61         spec->zNa
2e307 6d 65 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  me);.  rc = sqli
2e308 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62  te3_declare_vtab
2e309 28 64 62 2c 20 73 63 68 65 6d 61 29 3b 0a 20 20  (db, schema);.  
2e30a 73 71 6c 69 74 65 33 5f 66 72 65 65 28 73 63 68  sqlite3_free(sch
2e30b 65 6d 61 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  ema);.  if( rc!=
2e30c 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
2e30d 20 65 72 72 3b 0a 0a 20 20 6d 65 6d 73 65 74 28   err;..  memset(
2e30e 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74  v->pFulltextStat
2e30f 65 6d 65 6e 74 73 2c 20 30 2c 20 73 69 7a 65 6f  ements, 0, sizeo
2e310 66 28 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74  f(v->pFulltextSt
2e311 61 74 65 6d 65 6e 74 73 29 29 3b 0a 0a 20 20 2f  atements));..  /
2e312 2a 20 49 6e 64 69 63 61 74 65 20 74 68 61 74 20  * Indicate that 
2e313 74 68 65 20 62 75 66 66 65 72 20 69 73 20 6e 6f  the buffer is no
2e314 74 20 6c 69 76 65 2e 20 2a 2f 0a 20 20 76 2d 3e  t live. */.  v->
2e315 6e 50 65 6e 64 69 6e 67 44 61 74 61 20 3d 20 2d  nPendingData = -
2e316 31 3b 0a 0a 20 20 2a 70 70 56 54 61 62 20 3d 20  1;..  *ppVTab = 
2e317 26 76 2d 3e 62 61 73 65 3b 0a 20 20 46 54 53 54  &v->base;.  FTST
2e318 52 41 43 45 28 28 22 46 54 53 33 20 43 6f 6e 6e  RACE(("FTS3 Conn
2e319 65 63 74 20 25 70 5c 6e 22 2c 20 76 29 29 3b 0a  ect %p\n", v));.
2e31a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 65  .  return rc;..e
2e31b 72 72 3a 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76  rr:.  fulltext_v
2e31c 74 61 62 5f 64 65 73 74 72 6f 79 28 76 29 3b 0a  tab_destroy(v);.
2e31d 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
2e31e 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74  static int fullt
2e31f 65 78 74 43 6f 6e 6e 65 63 74 28 0a 20 20 73 71  extConnect(.  sq
2e320 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 76 6f 69  lite3 *db,.  voi
2e321 64 20 2a 70 41 75 78 2c 0a 20 20 69 6e 74 20 61  d *pAux,.  int a
2e322 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  rgc, const char 
2e323 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 73  *const*argv,.  s
2e324 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70  qlite3_vtab **pp
2e325 56 54 61 62 2c 0a 20 20 63 68 61 72 20 2a 2a 70  VTab,.  char **p
2e326 7a 45 72 72 0a 29 7b 0a 20 20 54 61 62 6c 65 53  zErr.){.  TableS
2e327 70 65 63 20 73 70 65 63 3b 0a 20 20 69 6e 74 20  pec spec;.  int 
2e328 72 63 20 3d 20 70 61 72 73 65 53 70 65 63 28 26  rc = parseSpec(&
2e329 73 70 65 63 2c 20 61 72 67 63 2c 20 61 72 67 76  spec, argc, argv
2e32a 2c 20 70 7a 45 72 72 29 3b 0a 20 20 69 66 28 20  , pzErr);.  if( 
2e32b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2e32c 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
2e32d 20 3d 20 63 6f 6e 73 74 72 75 63 74 56 74 61 62   = constructVtab
2e32e 28 64 62 2c 20 28 66 74 73 33 48 61 73 68 20 2a  (db, (fts3Hash *
2e32f 29 70 41 75 78 2c 20 26 73 70 65 63 2c 20 70 70  )pAux, &spec, pp
2e330 56 54 61 62 2c 20 70 7a 45 72 72 29 3b 0a 20 20  VTab, pzErr);.  
2e331 63 6c 65 61 72 54 61 62 6c 65 53 70 65 63 28 26  clearTableSpec(&
2e332 73 70 65 63 29 3b 0a 20 20 72 65 74 75 72 6e 20  spec);.  return 
2e333 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 25 5f  rc;.}../* The %_
2e334 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 20 68 6f  content table ho
2e335 6c 64 73 20 74 68 65 20 74 65 78 74 20 6f 66 20  lds the text of 
2e336 65 61 63 68 20 64 6f 63 75 6d 65 6e 74 2c 20 77  each document, w
2e337 69 74 68 0a 2a 2a 20 74 68 65 20 64 6f 63 69 64  ith.** the docid
2e338 20 63 6f 6c 75 6d 6e 20 65 78 70 6f 73 65 64 20   column exposed 
2e339 61 73 20 74 68 65 20 53 51 4c 69 74 65 20 72 6f  as the SQLite ro
2e33a 77 69 64 20 66 6f 72 20 74 68 65 20 74 61 62 6c  wid for the tabl
2e33b 65 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68  e..*/./* TODO(sh
2e33c 65 73 73 29 20 54 68 69 73 20 63 6f 6d 6d 65 6e  ess) This commen
2e33d 74 20 6e 65 65 64 73 20 65 6c 61 62 6f 72 61 74  t needs elaborat
2e33e 69 6f 6e 20 74 6f 20 6d 61 74 63 68 20 74 68 65  ion to match the
2e33f 20 75 70 64 61 74 65 64 0a 2a 2a 20 63 6f 64 65   updated.** code
2e340 2e 20 20 57 6f 72 6b 20 69 74 20 69 6e 74 6f 20  .  Work it into 
2e341 74 68 65 20 74 6f 70 2d 6f 66 2d 66 69 6c 65 20  the top-of-file 
2e342 63 6f 6d 6d 65 6e 74 20 61 74 20 74 68 61 74 20  comment at that 
2e343 74 69 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  time..*/.static 
2e344 69 6e 74 20 66 75 6c 6c 74 65 78 74 43 72 65 61  int fulltextCrea
2e345 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  te(sqlite3 *db, 
2e346 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 20 20  void *pAux,.    
2e347 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e348 20 20 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20        int argc, 
2e349 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63 6f 6e  const char * con
2e34a 73 74 20 2a 61 72 67 76 2c 0a 20 20 20 20 20 20  st *argv,.      
2e34b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e34c 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62      sqlite3_vtab
2e34d 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61 72 20   **ppVTab, char 
2e34e 2a 2a 70 7a 45 72 72 29 7b 0a 20 20 69 6e 74 20  **pzErr){.  int 
2e34f 72 63 3b 0a 20 20 54 61 62 6c 65 53 70 65 63 20  rc;.  TableSpec 
2e350 73 70 65 63 3b 0a 20 20 53 74 72 69 6e 67 42 75  spec;.  StringBu
2e351 66 66 65 72 20 73 63 68 65 6d 61 3b 0a 20 20 46  ffer schema;.  F
2e352 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20 43  TSTRACE(("FTS3 C
2e353 72 65 61 74 65 5c 6e 22 29 29 3b 0a 0a 20 20 72  reate\n"));..  r
2e354 63 20 3d 20 70 61 72 73 65 53 70 65 63 28 26 73  c = parseSpec(&s
2e355 70 65 63 2c 20 61 72 67 63 2c 20 61 72 67 76 2c  pec, argc, argv,
2e356 20 70 7a 45 72 72 29 3b 0a 20 20 69 66 28 20 72   pzErr);.  if( r
2e357 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
2e358 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 69 6e 69  eturn rc;..  ini
2e359 74 53 74 72 69 6e 67 42 75 66 66 65 72 28 26 73  tStringBuffer(&s
2e35a 63 68 65 6d 61 29 3b 0a 20 20 61 70 70 65 6e 64  chema);.  append
2e35b 28 26 73 63 68 65 6d 61 2c 20 22 43 52 45 41 54  (&schema, "CREAT
2e35c 45 20 54 41 42 4c 45 20 25 5f 63 6f 6e 74 65 6e  E TABLE %_conten
2e35d 74 28 22 29 3b 0a 20 20 61 70 70 65 6e 64 28 26  t(");.  append(&
2e35e 73 63 68 65 6d 61 2c 20 22 20 20 64 6f 63 69 64  schema, "  docid
2e35f 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
2e360 20 4b 45 59 2c 22 29 3b 0a 20 20 61 70 70 65 6e   KEY,");.  appen
2e361 64 4c 69 73 74 28 26 73 63 68 65 6d 61 2c 20 73  dList(&schema, s
2e362 70 65 63 2e 6e 43 6f 6c 75 6d 6e 2c 20 73 70 65  pec.nColumn, spe
2e363 63 2e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d  c.azContentColum
2e364 6e 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 63  n);.  append(&sc
2e365 68 65 6d 61 2c 20 22 29 22 29 3b 0a 20 20 72 63  hema, ")");.  rc
2e366 20 3d 20 73 71 6c 5f 65 78 65 63 28 64 62 2c 20   = sql_exec(db, 
2e367 73 70 65 63 2e 7a 44 62 2c 20 73 70 65 63 2e 7a  spec.zDb, spec.z
2e368 4e 61 6d 65 2c 20 73 74 72 69 6e 67 42 75 66 66  Name, stringBuff
2e369 65 72 44 61 74 61 28 26 73 63 68 65 6d 61 29 29  erData(&schema))
2e36a 3b 0a 20 20 73 74 72 69 6e 67 42 75 66 66 65 72  ;.  stringBuffer
2e36b 44 65 73 74 72 6f 79 28 26 73 63 68 65 6d 61 29  Destroy(&schema)
2e36c 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
2e36d 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 6f 75 74  TE_OK ) goto out
2e36e 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 5f 65 78  ;..  rc = sql_ex
2e36f 65 63 28 64 62 2c 20 73 70 65 63 2e 7a 44 62 2c  ec(db, spec.zDb,
2e370 20 73 70 65 63 2e 7a 4e 61 6d 65 2c 0a 20 20 20   spec.zName,.   
2e371 20 20 20 20 20 20 20 20 20 20 20 20 20 22 63 72               "cr
2e372 65 61 74 65 20 74 61 62 6c 65 20 25 5f 73 65 67  eate table %_seg
2e373 6d 65 6e 74 73 28 22 0a 20 20 20 20 20 20 20 20  ments(".        
2e374 20 20 20 20 20 20 20 20 22 20 20 62 6c 6f 63 6b          "  block
2e375 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  id INTEGER PRIMA
2e376 52 59 20 4b 45 59 2c 22 0a 20 20 20 20 20 20 20  RY KEY,".       
2e377 20 20 20 20 20 20 20 20 20 22 20 20 62 6c 6f 63           "  bloc
2e378 6b 20 62 6c 6f 62 22 0a 20 20 20 20 20 20 20 20  k blob".        
2e379 20 20 20 20 20 20 20 20 22 29 3b 22 0a 20 20 20          ");".   
2e37a 20 20 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a               );.
2e37b 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2e37c 5f 4f 4b 20 29 20 67 6f 74 6f 20 6f 75 74 3b 0a  _OK ) goto out;.
2e37d 0a 20 20 72 63 20 3d 20 73 71 6c 5f 65 78 65 63  .  rc = sql_exec
2e37e 28 64 62 2c 20 73 70 65 63 2e 7a 44 62 2c 20 73  (db, spec.zDb, s
2e37f 70 65 63 2e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20  pec.zName,.     
2e380 20 20 20 20 20 20 20 20 20 20 20 22 63 72 65 61             "crea
2e381 74 65 20 74 61 62 6c 65 20 25 5f 73 65 67 64 69  te table %_segdi
2e382 72 28 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  r(".            
2e383 20 20 20 20 22 20 20 6c 65 76 65 6c 20 69 6e 74      "  level int
2e384 65 67 65 72 2c 22 0a 20 20 20 20 20 20 20 20 20  eger,".         
2e385 20 20 20 20 20 20 20 22 20 20 69 64 78 20 69 6e         "  idx in
2e386 74 65 67 65 72 2c 22 0a 20 20 20 20 20 20 20 20  teger,".        
2e387 20 20 20 20 20 20 20 20 22 20 20 73 74 61 72 74          "  start
2e388 5f 62 6c 6f 63 6b 20 69 6e 74 65 67 65 72 2c 22  _block integer,"
2e389 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2e38a 20 22 20 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62   "  leaves_end_b
2e38b 6c 6f 63 6b 20 69 6e 74 65 67 65 72 2c 22 0a 20  lock integer,". 
2e38c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
2e38d 20 20 65 6e 64 5f 62 6c 6f 63 6b 20 69 6e 74 65    end_block inte
2e38e 67 65 72 2c 22 0a 20 20 20 20 20 20 20 20 20 20  ger,".          
2e38f 20 20 20 20 20 20 22 20 20 72 6f 6f 74 20 62 6c        "  root bl
2e390 6f 62 2c 22 0a 20 20 20 20 20 20 20 20 20 20 20  ob,".           
2e391 20 20 20 20 20 22 20 20 70 72 69 6d 61 72 79 20       "  primary 
2e392 6b 65 79 28 6c 65 76 65 6c 2c 20 69 64 78 29 22  key(level, idx)"
2e393 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2e394 20 22 29 3b 22 29 3b 0a 20 20 69 66 28 20 72 63   ");");.  if( rc
2e395 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
2e396 74 6f 20 6f 75 74 3b 0a 0a 20 20 72 63 20 3d 20  to out;..  rc = 
2e397 63 6f 6e 73 74 72 75 63 74 56 74 61 62 28 64 62  constructVtab(db
2e398 2c 20 28 66 74 73 33 48 61 73 68 20 2a 29 70 41  , (fts3Hash *)pA
2e399 75 78 2c 20 26 73 70 65 63 2c 20 70 70 56 54 61  ux, &spec, ppVTa
2e39a 62 2c 20 70 7a 45 72 72 29 3b 0a 0a 6f 75 74 3a  b, pzErr);..out:
2e39b 0a 20 20 63 6c 65 61 72 54 61 62 6c 65 53 70 65  .  clearTableSpe
2e39c 63 28 26 73 70 65 63 29 3b 0a 20 20 72 65 74 75  c(&spec);.  retu
2e39d 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 44 65 63  rn rc;.}../* Dec
2e39e 69 64 65 20 68 6f 77 20 74 6f 20 68 61 6e 64 6c  ide how to handl
2e39f 65 20 61 6e 20 53 51 4c 20 71 75 65 72 79 2e 20  e an SQL query. 
2e3a0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75  */.static int fu
2e3a1 6c 6c 74 65 78 74 42 65 73 74 49 6e 64 65 78 28  lltextBestIndex(
2e3a2 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
2e3a3 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64  Tab, sqlite3_ind
2e3a4 65 78 5f 69 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b  ex_info *pInfo){
2e3a5 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  .  fulltext_vtab
2e3a6 20 2a 76 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f   *v = (fulltext_
2e3a7 76 74 61 62 20 2a 29 70 56 54 61 62 3b 0a 20 20  vtab *)pVTab;.  
2e3a8 69 6e 74 20 69 3b 0a 20 20 46 54 53 54 52 41 43  int i;.  FTSTRAC
2e3a9 45 28 28 22 46 54 53 33 20 42 65 73 74 49 6e 64  E(("FTS3 BestInd
2e3aa 65 78 5c 6e 22 29 29 3b 0a 0a 20 20 66 6f 72 28  ex\n"));..  for(
2e3ab 69 3d 30 3b 20 69 3c 70 49 6e 66 6f 2d 3e 6e 43  i=0; i<pInfo->nC
2e3ac 6f 6e 73 74 72 61 69 6e 74 3b 20 2b 2b 69 29 7b  onstraint; ++i){
2e3ad 0a 20 20 20 20 63 6f 6e 73 74 20 73 74 72 75 63  .    const struc
2e3ae 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
2e3af 63 6f 6e 73 74 72 61 69 6e 74 20 2a 70 43 6f 6e  constraint *pCon
2e3b0 73 74 72 61 69 6e 74 3b 0a 20 20 20 20 70 43 6f  straint;.    pCo
2e3b1 6e 73 74 72 61 69 6e 74 20 3d 20 26 70 49 6e 66  nstraint = &pInf
2e3b2 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69  o->aConstraint[i
2e3b3 5d 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6e 73  ];.    if( pCons
2e3b4 74 72 61 69 6e 74 2d 3e 75 73 61 62 6c 65 20 29  traint->usable )
2e3b5 20 7b 0a 20 20 20 20 20 20 69 66 28 20 28 70 43   {.      if( (pC
2e3b6 6f 6e 73 74 72 61 69 6e 74 2d 3e 69 43 6f 6c 75  onstraint->iColu
2e3b7 6d 6e 3d 3d 2d 31 20 7c 7c 20 70 43 6f 6e 73 74  mn==-1 || pConst
2e3b8 72 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d  raint->iColumn==
2e3b9 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 20 26 26  v->nColumn+1) &&
2e3ba 0a 20 20 20 20 20 20 20 20 20 20 70 43 6f 6e 73  .          pCons
2e3bb 74 72 61 69 6e 74 2d 3e 6f 70 3d 3d 53 51 4c 49  traint->op==SQLI
2e3bc 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
2e3bd 49 4e 54 5f 45 51 20 29 7b 0a 20 20 20 20 20 20  INT_EQ ){.      
2e3be 20 20 70 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20    pInfo->idxNum 
2e3bf 3d 20 51 55 45 52 59 5f 44 4f 43 49 44 3b 20 20  = QUERY_DOCID;  
2e3c0 20 20 20 20 2f 2a 20 6c 6f 6f 6b 75 70 20 62 79      /* lookup by
2e3c1 20 64 6f 63 69 64 20 2a 2f 0a 20 20 20 20 20 20   docid */.      
2e3c2 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53    FTSTRACE(("FTS
2e3c3 33 20 51 55 45 52 59 5f 44 4f 43 49 44 5c 6e 22  3 QUERY_DOCID\n"
2e3c4 29 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65  ));.      } else
2e3c5 20 69 66 28 20 70 43 6f 6e 73 74 72 61 69 6e 74   if( pConstraint
2e3c6 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20  ->iColumn>=0 && 
2e3c7 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 69 43 6f  pConstraint->iCo
2e3c8 6c 75 6d 6e 3c 3d 76 2d 3e 6e 43 6f 6c 75 6d 6e  lumn<=v->nColumn
2e3c9 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 20   &&.            
2e3ca 20 20 20 20 20 70 43 6f 6e 73 74 72 61 69 6e 74       pConstraint
2e3cb 2d 3e 6f 70 3d 3d 53 51 4c 49 54 45 5f 49 4e 44  ->op==SQLITE_IND
2e3cc 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4d 41  EX_CONSTRAINT_MA
2e3cd 54 43 48 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  TCH ){.        /
2e3ce 2a 20 66 75 6c 6c 2d 74 65 78 74 20 73 65 61 72  * full-text sear
2e3cf 63 68 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 49  ch */.        pI
2e3d0 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 51 55  nfo->idxNum = QU
2e3d1 45 52 59 5f 46 55 4c 4c 54 45 58 54 20 2b 20 70  ERY_FULLTEXT + p
2e3d2 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 69 43 6f 6c  Constraint->iCol
2e3d3 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20 46 54 53  umn;.        FTS
2e3d4 54 52 41 43 45 28 28 22 46 54 53 33 20 51 55 45  TRACE(("FTS3 QUE
2e3d5 52 59 5f 46 55 4c 4c 54 45 58 54 20 25 64 5c 6e  RY_FULLTEXT %d\n
2e3d6 22 2c 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e  ", pConstraint->
2e3d7 69 43 6f 6c 75 6d 6e 29 29 3b 0a 20 20 20 20 20  iColumn));.     
2e3d8 20 7d 20 65 6c 73 65 20 63 6f 6e 74 69 6e 75 65   } else continue
2e3d9 3b 0a 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e  ;..      pInfo->
2e3da 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
2e3db 5b 69 5d 2e 61 72 67 76 49 6e 64 65 78 20 3d 20  [i].argvIndex = 
2e3dc 31 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e  1;.      pInfo->
2e3dd 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
2e3de 5b 69 5d 2e 6f 6d 69 74 20 3d 20 31 3b 0a 0a 20  [i].omit = 1;.. 
2e3df 20 20 20 20 20 2f 2a 20 41 6e 20 61 72 62 69 74       /* An arbit
2e3e0 72 61 72 79 20 76 61 6c 75 65 20 66 6f 72 20 6e  rary value for n
2e3e1 6f 77 2e 0a 20 20 20 20 20 20 20 2a 20 54 4f 44  ow..       * TOD
2e3e2 4f 3a 20 50 65 72 68 61 70 73 20 64 6f 63 69 64  O: Perhaps docid
2e3e3 20 6d 61 74 63 68 65 73 20 73 68 6f 75 6c 64 20   matches should 
2e3e4 62 65 20 63 6f 6e 73 69 64 65 72 65 64 20 63 68  be considered ch
2e3e5 65 61 70 65 72 20 74 68 61 6e 0a 20 20 20 20 20  eaper than.     
2e3e6 20 20 2a 20 66 75 6c 6c 2d 74 65 78 74 20 73 65    * full-text se
2e3e7 61 72 63 68 65 73 2e 20 2a 2f 0a 20 20 20 20 20  arches. */.     
2e3e8 20 70 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65   pInfo->estimate
2e3e9 64 43 6f 73 74 20 3d 20 31 2e 30 3b 20 20 20 0a  dCost = 1.0;   .
2e3ea 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
2e3eb 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
2e3ec 20 7d 0a 20 20 70 49 6e 66 6f 2d 3e 69 64 78 4e   }.  pInfo->idxN
2e3ed 75 6d 20 3d 20 51 55 45 52 59 5f 47 45 4e 45 52  um = QUERY_GENER
2e3ee 49 43 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  IC;.  return SQL
2e3ef 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69  ITE_OK;.}..stati
2e3f0 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 44 69  c int fulltextDi
2e3f1 73 63 6f 6e 6e 65 63 74 28 73 71 6c 69 74 65 33  sconnect(sqlite3
2e3f2 5f 76 74 61 62 20 2a 70 56 54 61 62 29 7b 0a 20  _vtab *pVTab){. 
2e3f3 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33   FTSTRACE(("FTS3
2e3f4 20 44 69 73 63 6f 6e 6e 65 63 74 20 25 70 5c 6e   Disconnect %p\n
2e3f5 22 2c 20 70 56 54 61 62 29 29 3b 0a 20 20 66 75  ", pVTab));.  fu
2e3f6 6c 6c 74 65 78 74 5f 76 74 61 62 5f 64 65 73 74  lltext_vtab_dest
2e3f7 72 6f 79 28 28 66 75 6c 6c 74 65 78 74 5f 76 74  roy((fulltext_vt
2e3f8 61 62 20 2a 29 70 56 54 61 62 29 3b 0a 20 20 72  ab *)pVTab);.  r
2e3f9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2e3fa 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .}..static int f
2e3fb 75 6c 6c 74 65 78 74 44 65 73 74 72 6f 79 28 73  ulltextDestroy(s
2e3fc 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54  qlite3_vtab *pVT
2e3fd 61 62 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f  ab){.  fulltext_
2e3fe 76 74 61 62 20 2a 76 20 3d 20 28 66 75 6c 6c 74  vtab *v = (fullt
2e3ff 65 78 74 5f 76 74 61 62 20 2a 29 70 56 54 61 62  ext_vtab *)pVTab
2e400 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 46  ;.  int rc;..  F
2e401 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20 44  TSTRACE(("FTS3 D
2e402 65 73 74 72 6f 79 20 25 70 5c 6e 22 2c 20 70 56  estroy %p\n", pV
2e403 54 61 62 29 29 3b 0a 20 20 72 63 20 3d 20 73 71  Tab));.  rc = sq
2e404 6c 5f 65 78 65 63 28 76 2d 3e 64 62 2c 20 76 2d  l_exec(v->db, v-
2e405 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61 6d 65 2c 0a  >zDb, v->zName,.
2e406 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e407 22 64 72 6f 70 20 74 61 62 6c 65 20 69 66 20 65  "drop table if e
2e408 78 69 73 74 73 20 25 5f 63 6f 6e 74 65 6e 74 3b  xists %_content;
2e409 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
2e40a 20 20 22 64 72 6f 70 20 74 61 62 6c 65 20 69 66    "drop table if
2e40b 20 65 78 69 73 74 73 20 25 5f 73 65 67 6d 65 6e   exists %_segmen
2e40c 74 73 3b 22 0a 20 20 20 20 20 20 20 20 20 20 20  ts;".           
2e40d 20 20 20 20 20 22 64 72 6f 70 20 74 61 62 6c 65       "drop table
2e40e 20 69 66 20 65 78 69 73 74 73 20 25 5f 73 65 67   if exists %_seg
2e40f 64 69 72 3b 22 0a 20 20 20 20 20 20 20 20 20 20  dir;".          
2e410 20 20 20 20 20 20 29 3b 0a 20 20 69 66 28 20 72        );.  if( r
2e411 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
2e412 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 66 75 6c  eturn rc;..  ful
2e413 6c 74 65 78 74 5f 76 74 61 62 5f 64 65 73 74 72  ltext_vtab_destr
2e414 6f 79 28 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  oy((fulltext_vta
2e415 62 20 2a 29 70 56 54 61 62 29 3b 0a 20 20 72 65  b *)pVTab);.  re
2e416 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
2e417 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75  }..static int fu
2e418 6c 6c 74 65 78 74 4f 70 65 6e 28 73 71 6c 69 74  lltextOpen(sqlit
2e419 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c 20  e3_vtab *pVTab, 
2e41a 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
2e41b 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 29 7b  sor **ppCursor){
2e41c 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  .  fulltext_curs
2e41d 6f 72 20 2a 63 3b 0a 0a 20 20 63 20 3d 20 28 66  or *c;..  c = (f
2e41e 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a  ulltext_cursor *
2e41f 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  ) sqlite3_malloc
2e420 28 73 69 7a 65 6f 66 28 66 75 6c 6c 74 65 78 74  (sizeof(fulltext
2e421 5f 63 75 72 73 6f 72 29 29 3b 0a 20 20 69 66 28  _cursor));.  if(
2e422 20 63 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74   c ){.    memset
2e423 28 63 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 75  (c, 0, sizeof(fu
2e424 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 29 29 3b  lltext_cursor));
2e425 0a 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 20 77  .    /* sqlite w
2e426 69 6c 6c 20 69 6e 69 74 69 61 6c 69 7a 65 20 63  ill initialize c
2e427 2d 3e 62 61 73 65 20 2a 2f 0a 20 20 20 20 2a 70  ->base */.    *p
2e428 70 43 75 72 73 6f 72 20 3d 20 26 63 2d 3e 62 61  pCursor = &c->ba
2e429 73 65 3b 0a 20 20 20 20 46 54 53 54 52 41 43 45  se;.    FTSTRACE
2e42a 28 28 22 46 54 53 33 20 4f 70 65 6e 20 25 70 3a  (("FTS3 Open %p:
2e42b 20 25 70 5c 6e 22 2c 20 70 56 54 61 62 2c 20 63   %p\n", pVTab, c
2e42c 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  ));.    return S
2e42d 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73  QLITE_OK;.  }els
2e42e 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  e{.    return SQ
2e42f 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
2e430 7d 0a 0a 0a 2f 2a 20 46 72 65 65 20 61 6c 6c 20  }.../* Free all 
2e431 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 61 6c  of the dynamical
2e432 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d  ly allocated mem
2e433 6f 72 79 20 68 65 6c 64 20 62 79 20 2a 71 0a 2a  ory held by *q.*
2e434 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 71 75  /.static void qu
2e435 65 72 79 43 6c 65 61 72 28 51 75 65 72 79 20 2a  eryClear(Query *
2e436 71 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66  q){.  int i;.  f
2e437 6f 72 28 69 20 3d 20 30 3b 20 69 20 3c 20 71 2d  or(i = 0; i < q-
2e438 3e 6e 54 65 72 6d 73 3b 20 2b 2b 69 29 7b 0a 20  >nTerms; ++i){. 
2e439 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
2e43a 71 2d 3e 70 54 65 72 6d 73 5b 69 5d 2e 70 54 65  q->pTerms[i].pTe
2e43b 72 6d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  rm);.  }.  sqlit
2e43c 65 33 5f 66 72 65 65 28 71 2d 3e 70 54 65 72 6d  e3_free(q->pTerm
2e43d 73 29 3b 0a 20 20 43 4c 45 41 52 28 71 29 3b 0a  s);.  CLEAR(q);.
2e43e 7d 0a 0a 2f 2a 20 46 72 65 65 20 61 6c 6c 20 6f  }../* Free all o
2e43f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 61 6c 6c  f the dynamicall
2e440 79 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f  y allocated memo
2e441 72 79 20 68 65 6c 64 20 62 79 20 74 68 65 0a 2a  ry held by the.*
2e442 2a 20 53 6e 69 70 70 65 74 0a 2a 2f 0a 73 74 61  * Snippet.*/.sta
2e443 74 69 63 20 76 6f 69 64 20 73 6e 69 70 70 65 74  tic void snippet
2e444 43 6c 65 61 72 28 53 6e 69 70 70 65 74 20 2a 70  Clear(Snippet *p
2e445 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  ){.  sqlite3_fre
2e446 65 28 70 2d 3e 61 4d 61 74 63 68 29 3b 0a 20 20  e(p->aMatch);.  
2e447 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e  sqlite3_free(p->
2e448 7a 4f 66 66 73 65 74 29 3b 0a 20 20 73 71 6c 69  zOffset);.  sqli
2e449 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 53 6e 69  te3_free(p->zSni
2e44a 70 70 65 74 29 3b 0a 20 20 43 4c 45 41 52 28 70  ppet);.  CLEAR(p
2e44b 29 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e  );.}./*.** Appen
2e44c 64 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79  d a single entry
2e44d 20 74 6f 20 74 68 65 20 70 2d 3e 61 4d 61 74 63   to the p->aMatc
2e44e 68 5b 5d 20 6c 6f 67 2e 0a 2a 2f 0a 73 74 61 74  h[] log..*/.stat
2e44f 69 63 20 76 6f 69 64 20 73 6e 69 70 70 65 74 41  ic void snippetA
2e450 70 70 65 6e 64 4d 61 74 63 68 28 0a 20 20 53 6e  ppendMatch(.  Sn
2e451 69 70 70 65 74 20 2a 70 2c 20 20 20 20 20 20 20  ippet *p,       
2e452 20 20 20 20 20 20 20 20 2f 2a 20 41 70 70 65 6e          /* Appen
2e453 64 20 74 68 65 20 65 6e 74 72 79 20 74 6f 20 74  d the entry to t
2e454 68 69 73 20 73 6e 69 70 70 65 74 20 2a 2f 0a 20  his snippet */. 
2e455 20 69 6e 74 20 69 43 6f 6c 2c 20 69 6e 74 20 69   int iCol, int i
2e456 54 65 72 6d 2c 20 20 20 20 20 20 2f 2a 20 54 68  Term,      /* Th
2e457 65 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 71 75 65  e column and que
2e458 72 79 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74  ry term */.  int
2e459 20 69 54 6f 6b 65 6e 2c 20 20 20 20 20 20 20 20   iToken,        
2e45a 20 20 20 20 20 20 20 2f 2a 20 4d 61 74 63 68 69         /* Matchi
2e45b 6e 67 20 74 6f 6b 65 6e 20 69 6e 20 64 6f 63 75  ng token in docu
2e45c 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 69 53  ment */.  int iS
2e45d 74 61 72 74 2c 20 69 6e 74 20 6e 42 79 74 65 20  tart, int nByte 
2e45e 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 61 6e      /* Offset an
2e45f 64 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6d 61  d size of the ma
2e460 74 63 68 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  tch */.){.  int 
2e461 69 3b 0a 20 20 73 74 72 75 63 74 20 73 6e 69 70  i;.  struct snip
2e462 70 65 74 4d 61 74 63 68 20 2a 70 4d 61 74 63 68  petMatch *pMatch
2e463 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4d 61 74 63  ;.  if( p->nMatc
2e464 68 2b 31 3e 3d 70 2d 3e 6e 41 6c 6c 6f 63 20 29  h+1>=p->nAlloc )
2e465 7b 0a 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20  {.    p->nAlloc 
2e466 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b 20  = p->nAlloc*2 + 
2e467 31 30 3b 0a 20 20 20 20 70 2d 3e 61 4d 61 74 63  10;.    p->aMatc
2e468 68 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  h = sqlite3_real
2e469 6c 6f 63 28 70 2d 3e 61 4d 61 74 63 68 2c 20 70  loc(p->aMatch, p
2e46a 2d 3e 6e 41 6c 6c 6f 63 2a 73 69 7a 65 6f 66 28  ->nAlloc*sizeof(
2e46b 70 2d 3e 61 4d 61 74 63 68 5b 30 5d 29 20 29 3b  p->aMatch[0]) );
2e46c 0a 20 20 20 20 69 66 28 20 70 2d 3e 61 4d 61 74  .    if( p->aMat
2e46d 63 68 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  ch==0 ){.      p
2e46e 2d 3e 6e 4d 61 74 63 68 20 3d 20 30 3b 0a 20 20  ->nMatch = 0;.  
2e46f 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20      p->nAlloc = 
2e470 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  0;.      return;
2e471 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 20 3d  .    }.  }.  i =
2e472 20 70 2d 3e 6e 4d 61 74 63 68 2b 2b 3b 0a 20 20   p->nMatch++;.  
2e473 70 4d 61 74 63 68 20 3d 20 26 70 2d 3e 61 4d 61  pMatch = &p->aMa
2e474 74 63 68 5b 69 5d 3b 0a 20 20 70 4d 61 74 63 68  tch[i];.  pMatch
2e475 2d 3e 69 43 6f 6c 20 3d 20 69 43 6f 6c 3b 0a 20  ->iCol = iCol;. 
2e476 20 70 4d 61 74 63 68 2d 3e 69 54 65 72 6d 20 3d   pMatch->iTerm =
2e477 20 69 54 65 72 6d 3b 0a 20 20 70 4d 61 74 63 68   iTerm;.  pMatch
2e478 2d 3e 69 54 6f 6b 65 6e 20 3d 20 69 54 6f 6b 65  ->iToken = iToke
2e479 6e 3b 0a 20 20 70 4d 61 74 63 68 2d 3e 69 53 74  n;.  pMatch->iSt
2e47a 61 72 74 20 3d 20 69 53 74 61 72 74 3b 0a 20 20  art = iStart;.  
2e47b 70 4d 61 74 63 68 2d 3e 6e 42 79 74 65 20 3d 20  pMatch->nByte = 
2e47c 6e 42 79 74 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  nByte;.}../*.** 
2e47d 53 69 7a 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69  Sizing informati
2e47e 6f 6e 20 66 6f 72 20 74 68 65 20 63 69 72 63 75  on for the circu
2e47f 6c 61 72 20 62 75 66 66 65 72 20 75 73 65 64 20  lar buffer used 
2e480 69 6e 20 73 6e 69 70 70 65 74 4f 66 66 73 65 74  in snippetOffset
2e481 73 4f 66 43 6f 6c 75 6d 6e 28 29 0a 2a 2f 0a 23  sOfColumn().*/.#
2e482 64 65 66 69 6e 65 20 46 54 53 33 5f 52 4f 54 4f  define FTS3_ROTO
2e483 52 5f 53 5a 20 20 20 28 33 32 29 0a 23 64 65 66  R_SZ   (32).#def
2e484 69 6e 65 20 46 54 53 33 5f 52 4f 54 4f 52 5f 4d  ine FTS3_ROTOR_M
2e485 41 53 4b 20 28 46 54 53 33 5f 52 4f 54 4f 52 5f  ASK (FTS3_ROTOR_
2e486 53 5a 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  SZ-1)../*.** Add
2e487 20 65 6e 74 72 69 65 73 20 74 6f 20 70 53 6e 69   entries to pSni
2e488 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b 5d 20 66  ppet->aMatch[] f
2e489 6f 72 20 65 76 65 72 79 20 6d 61 74 63 68 20 74  or every match t
2e48a 68 61 74 20 6f 63 63 75 72 73 20 61 67 61 69 6e  hat occurs again
2e48b 73 74 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 20 7a  st.** document z
2e48c 44 6f 63 5b 30 2e 2e 6e 44 6f 63 2d 31 5d 20 77  Doc[0..nDoc-1] w
2e48d 68 69 63 68 20 69 73 20 73 74 6f 72 65 64 20 69  hich is stored i
2e48e 6e 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 75 6d 6e  n column iColumn
2e48f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2e490 20 73 6e 69 70 70 65 74 4f 66 66 73 65 74 73 4f   snippetOffsetsO
2e491 66 43 6f 6c 75 6d 6e 28 0a 20 20 51 75 65 72 79  fColumn(.  Query
2e492 20 2a 70 51 75 65 72 79 2c 0a 20 20 53 6e 69 70   *pQuery,.  Snip
2e493 70 65 74 20 2a 70 53 6e 69 70 70 65 74 2c 0a 20  pet *pSnippet,. 
2e494 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 0a 20 20   int iColumn,.  
2e495 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 6f 63  const char *zDoc
2e496 2c 0a 20 20 69 6e 74 20 6e 44 6f 63 0a 29 7b 0a  ,.  int nDoc.){.
2e497 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
2e498 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
2e499 20 2a 70 54 4d 6f 64 75 6c 65 3b 20 20 2f 2a 20   *pTModule;  /* 
2e49a 54 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 6d 6f  The tokenizer mo
2e49b 64 75 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  dule */.  sqlite
2e49c 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f  3_tokenizer *pTo
2e49d 6b 65 6e 69 7a 65 72 3b 20 20 20 20 20 20 20 20  kenizer;        
2e49e 20 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65 63       /* The spec
2e49f 69 66 69 63 20 74 6f 6b 65 6e 69 7a 65 72 20 2a  ific tokenizer *
2e4a0 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  /.  sqlite3_toke
2e4a1 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 54  nizer_cursor *pT
2e4a2 43 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 2f  Cursor;        /
2e4a3 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 63 75 72 73  * Tokenizer curs
2e4a4 6f 72 20 2a 2f 0a 20 20 66 75 6c 6c 74 65 78 74  or */.  fulltext
2e4a5 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 20 20 20  _vtab *pVtab;   
2e4a6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e4a7 54 68 65 20 66 75 6c 6c 20 74 65 78 74 20 69 6e  The full text in
2e4a8 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  dex */.  int nCo
2e4a9 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20  lumn;           
2e4aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2e4ab 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   Number of colum
2e4ac 6e 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78 20  ns in the index 
2e4ad 2a 2f 0a 20 20 63 6f 6e 73 74 20 51 75 65 72 79  */.  const Query
2e4ae 54 65 72 6d 20 2a 61 54 65 72 6d 3b 20 20 20 20  Term *aTerm;    
2e4af 20 20 20 20 20 20 20 20 20 20 2f 2a 20 51 75 65            /* Que
2e4b0 72 79 20 73 74 72 69 6e 67 20 74 65 72 6d 73 20  ry string terms 
2e4b1 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 20  */.  int nTerm; 
2e4b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e4b3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
2e4b4 62 65 72 20 6f 66 20 71 75 65 72 79 20 73 74 72  ber of query str
2e4b5 69 6e 67 20 74 65 72 6d 73 20 2a 2f 20 20 0a 20  ing terms */  . 
2e4b6 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20   int i, j;      
2e4b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e4b8 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
2e4b9 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20  unters */.  int 
2e4ba 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
2e4bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e4bc 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
2e4bd 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  */.  unsigned in
2e4be 74 20 6d 61 74 63 68 2c 20 70 72 65 76 4d 61 74  t match, prevMat
2e4bf 63 68 3b 20 20 20 20 20 20 20 2f 2a 20 50 68 72  ch;       /* Phr
2e4c0 61 73 65 20 73 65 61 72 63 68 20 62 69 74 6d 61  ase search bitma
2e4c1 73 6b 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  sks */.  const c
2e4c2 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 20 20 20 20  har *zToken;    
2e4c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2e4c4 20 4e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d   Next token from
2e4c5 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a   the tokenizer *
2e4c6 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 20  /.  int nToken; 
2e4c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e4c8 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
2e4c9 20 6f 66 20 7a 54 6f 6b 65 6e 20 2a 2f 0a 20 20   of zToken */.  
2e4ca 69 6e 74 20 69 42 65 67 69 6e 2c 20 69 45 6e 64  int iBegin, iEnd
2e4cb 2c 20 69 50 6f 73 3b 20 20 20 20 20 20 20 20 20  , iPos;         
2e4cc 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 73 20       /* Offsets 
2e4cd 6f 66 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64  of beginning and
2e4ce 20 65 6e 64 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68   end */..  /* Th
2e4cf 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69  e following vari
2e4d0 61 62 6c 65 73 20 6b 65 65 70 20 61 20 63 69 72  ables keep a cir
2e4d1 63 75 6c 61 72 20 62 75 66 66 65 72 20 6f 66 20  cular buffer of 
2e4d2 74 68 65 20 6c 61 73 74 0a 20 20 2a 2a 20 66 65  the last.  ** fe
2e4d3 77 20 74 6f 6b 65 6e 73 20 2a 2f 0a 20 20 75 6e  w tokens */.  un
2e4d4 73 69 67 6e 65 64 20 69 6e 74 20 69 52 6f 74 6f  signed int iRoto
2e4d5 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  r = 0;          
2e4d6 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63     /* Index of c
2e4d7 75 72 72 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a  urrent token */.
2e4d8 20 20 69 6e 74 20 69 52 6f 74 6f 72 42 65 67 69    int iRotorBegi
2e4d9 6e 5b 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 5d  n[FTS3_ROTOR_SZ]
2e4da 3b 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 6e  ;      /* Beginn
2e4db 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20 74 6f  ing offset of to
2e4dc 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 52 6f  ken */.  int iRo
2e4dd 74 6f 72 4c 65 6e 5b 46 54 53 33 5f 52 4f 54 4f  torLen[FTS3_ROTO
2e4de 52 5f 53 5a 5d 3b 20 20 20 20 20 20 20 20 2f 2a  R_SZ];        /*
2e4df 20 4c 65 6e 67 74 68 20 6f 66 20 74 6f 6b 65 6e   Length of token
2e4e0 20 2a 2f 0a 0a 20 20 70 56 74 61 62 20 3d 20 70   */..  pVtab = p
2e4e1 51 75 65 72 79 2d 3e 70 46 74 73 3b 0a 20 20 6e  Query->pFts;.  n
2e4e2 43 6f 6c 75 6d 6e 20 3d 20 70 56 74 61 62 2d 3e  Column = pVtab->
2e4e3 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 70 54 6f 6b 65  nColumn;.  pToke
2e4e4 6e 69 7a 65 72 20 3d 20 70 56 74 61 62 2d 3e 70  nizer = pVtab->p
2e4e5 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 70 54 4d  Tokenizer;.  pTM
2e4e6 6f 64 75 6c 65 20 3d 20 70 54 6f 6b 65 6e 69 7a  odule = pTokeniz
2e4e7 65 72 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 72  er->pModule;.  r
2e4e8 63 20 3d 20 70 54 4d 6f 64 75 6c 65 2d 3e 78 4f  c = pTModule->xO
2e4e9 70 65 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20  pen(pTokenizer, 
2e4ea 7a 44 6f 63 2c 20 6e 44 6f 63 2c 20 26 70 54 43  zDoc, nDoc, &pTC
2e4eb 75 72 73 6f 72 29 3b 0a 20 20 69 66 28 20 72 63  ursor);.  if( rc
2e4ec 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 54 43   ) return;.  pTC
2e4ed 75 72 73 6f 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65  ursor->pTokenize
2e4ee 72 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a  r = pTokenizer;.
2e4ef 20 20 61 54 65 72 6d 20 3d 20 70 51 75 65 72 79    aTerm = pQuery
2e4f0 2d 3e 70 54 65 72 6d 73 3b 0a 20 20 6e 54 65 72  ->pTerms;.  nTer
2e4f1 6d 20 3d 20 70 51 75 65 72 79 2d 3e 6e 54 65 72  m = pQuery->nTer
2e4f2 6d 73 3b 0a 20 20 69 66 28 20 6e 54 65 72 6d 3e  ms;.  if( nTerm>
2e4f3 3d 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 20 29  =FTS3_ROTOR_SZ )
2e4f4 7b 0a 20 20 20 20 6e 54 65 72 6d 20 3d 20 46 54  {.    nTerm = FT
2e4f5 53 33 5f 52 4f 54 4f 52 5f 53 5a 20 2d 20 31 3b  S3_ROTOR_SZ - 1;
2e4f6 0a 20 20 7d 0a 20 20 70 72 65 76 4d 61 74 63 68  .  }.  prevMatch
2e4f7 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 31 29   = 0;.  while(1)
2e4f8 7b 0a 20 20 20 20 72 63 20 3d 20 70 54 4d 6f 64  {.    rc = pTMod
2e4f9 75 6c 65 2d 3e 78 4e 65 78 74 28 70 54 43 75 72  ule->xNext(pTCur
2e4fa 73 6f 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26 6e  sor, &zToken, &n
2e4fb 54 6f 6b 65 6e 2c 20 26 69 42 65 67 69 6e 2c 20  Token, &iBegin, 
2e4fc 26 69 45 6e 64 2c 20 26 69 50 6f 73 29 3b 0a 20  &iEnd, &iPos);. 
2e4fd 20 20 20 69 66 28 20 72 63 20 29 20 62 72 65 61     if( rc ) brea
2e4fe 6b 3b 0a 20 20 20 20 69 52 6f 74 6f 72 42 65 67  k;.    iRotorBeg
2e4ff 69 6e 5b 69 52 6f 74 6f 72 26 46 54 53 33 5f 52  in[iRotor&FTS3_R
2e500 4f 54 4f 52 5f 4d 41 53 4b 5d 20 3d 20 69 42 65  OTOR_MASK] = iBe
2e501 67 69 6e 3b 0a 20 20 20 20 69 52 6f 74 6f 72 4c  gin;.    iRotorL
2e502 65 6e 5b 69 52 6f 74 6f 72 26 46 54 53 33 5f 52  en[iRotor&FTS3_R
2e503 4f 54 4f 52 5f 4d 41 53 4b 5d 20 3d 20 69 45 6e  OTOR_MASK] = iEn
2e504 64 2d 69 42 65 67 69 6e 3b 0a 20 20 20 20 6d 61  d-iBegin;.    ma
2e505 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72  tch = 0;.    for
2e506 28 69 3d 30 3b 20 69 3c 6e 54 65 72 6d 3b 20 69  (i=0; i<nTerm; i
2e507 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  ++){.      int i
2e508 43 6f 6c 3b 0a 20 20 20 20 20 20 69 43 6f 6c 20  Col;.      iCol 
2e509 3d 20 61 54 65 72 6d 5b 69 5d 2e 69 43 6f 6c 75  = aTerm[i].iColu
2e50a 6d 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 69 43  mn;.      if( iC
2e50b 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 6e 43  ol>=0 && iCol<nC
2e50c 6f 6c 75 6d 6e 20 26 26 20 69 43 6f 6c 21 3d 69  olumn && iCol!=i
2e50d 43 6f 6c 75 6d 6e 20 29 20 63 6f 6e 74 69 6e 75  Column ) continu
2e50e 65 3b 0a 20 20 20 20 20 20 69 66 28 20 61 54 65  e;.      if( aTe
2e50f 72 6d 5b 69 5d 2e 6e 54 65 72 6d 3e 6e 54 6f 6b  rm[i].nTerm>nTok
2e510 65 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  en ) continue;. 
2e511 20 20 20 20 20 69 66 28 20 21 61 54 65 72 6d 5b       if( !aTerm[
2e512 69 5d 2e 69 73 50 72 65 66 69 78 20 26 26 20 61  i].isPrefix && a
2e513 54 65 72 6d 5b 69 5d 2e 6e 54 65 72 6d 3c 6e 54  Term[i].nTerm<nT
2e514 6f 6b 65 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b  oken ) continue;
2e515 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 61  .      assert( a
2e516 54 65 72 6d 5b 69 5d 2e 6e 54 65 72 6d 3c 3d 6e  Term[i].nTerm<=n
2e517 54 6f 6b 65 6e 20 29 3b 0a 20 20 20 20 20 20 69  Token );.      i
2e518 66 28 20 6d 65 6d 63 6d 70 28 61 54 65 72 6d 5b  f( memcmp(aTerm[
2e519 69 5d 2e 70 54 65 72 6d 2c 20 7a 54 6f 6b 65 6e  i].pTerm, zToken
2e51a 2c 20 61 54 65 72 6d 5b 69 5d 2e 6e 54 65 72 6d  , aTerm[i].nTerm
2e51b 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  ) ) continue;.  
2e51c 20 20 20 20 69 66 28 20 61 54 65 72 6d 5b 69 5d      if( aTerm[i]
2e51d 2e 69 50 68 72 61 73 65 3e 31 20 26 26 20 28 70  .iPhrase>1 && (p
2e51e 72 65 76 4d 61 74 63 68 20 26 20 28 31 3c 3c 69  revMatch & (1<<i
2e51f 29 29 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  ))==0 ) continue
2e520 3b 0a 20 20 20 20 20 20 6d 61 74 63 68 20 7c 3d  ;.      match |=
2e521 20 31 3c 3c 69 3b 0a 20 20 20 20 20 20 69 66 28   1<<i;.      if(
2e522 20 69 3d 3d 6e 54 65 72 6d 2d 31 20 7c 7c 20 61   i==nTerm-1 || a
2e523 54 65 72 6d 5b 69 2b 31 5d 2e 69 50 68 72 61 73  Term[i+1].iPhras
2e524 65 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  e==1 ){.        
2e525 66 6f 72 28 6a 3d 61 54 65 72 6d 5b 69 5d 2e 69  for(j=aTerm[i].i
2e526 50 68 72 61 73 65 2d 31 3b 20 6a 3e 3d 30 3b 20  Phrase-1; j>=0; 
2e527 6a 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20  j--){.          
2e528 69 6e 74 20 6b 20 3d 20 28 69 52 6f 74 6f 72 2d  int k = (iRotor-
2e529 6a 29 20 26 20 46 54 53 33 5f 52 4f 54 4f 52 5f  j) & FTS3_ROTOR_
2e52a 4d 41 53 4b 3b 0a 20 20 20 20 20 20 20 20 20 20  MASK;.          
2e52b 73 6e 69 70 70 65 74 41 70 70 65 6e 64 4d 61 74  snippetAppendMat
2e52c 63 68 28 70 53 6e 69 70 70 65 74 2c 20 69 43 6f  ch(pSnippet, iCo
2e52d 6c 75 6d 6e 2c 20 69 2d 6a 2c 20 69 50 6f 73 2d  lumn, i-j, iPos-
2e52e 6a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  j,.             
2e52f 20 20 20 69 52 6f 74 6f 72 42 65 67 69 6e 5b 6b     iRotorBegin[k
2e530 5d 2c 20 69 52 6f 74 6f 72 4c 65 6e 5b 6b 5d 29  ], iRotorLen[k])
2e531 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2e532 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 72    }.    }.    pr
2e533 65 76 4d 61 74 63 68 20 3d 20 6d 61 74 63 68 3c  evMatch = match<
2e534 3c 31 3b 0a 20 20 20 20 69 52 6f 74 6f 72 2b 2b  <1;.    iRotor++
2e535 3b 0a 20 20 7d 0a 20 20 70 54 4d 6f 64 75 6c 65  ;.  }.  pTModule
2e536 2d 3e 78 43 6c 6f 73 65 28 70 54 43 75 72 73 6f  ->xClose(pTCurso
2e537 72 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  r);  .}../*.** R
2e538 65 6d 6f 76 65 20 65 6e 74 72 69 65 73 20 66 72  emove entries fr
2e539 6f 6d 20 74 68 65 20 70 53 6e 69 70 70 65 74 20  om the pSnippet 
2e53a 73 74 72 75 63 74 75 72 65 20 74 6f 20 61 63 63  structure to acc
2e53b 6f 75 6e 74 20 66 6f 72 20 74 68 65 20 4e 45 41  ount for the NEA
2e53c 52 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 20 57  R.** operator. W
2e53d 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c 6c  hen this is call
2e53e 65 64 2c 20 70 53 6e 69 70 70 65 74 20 63 6f 6e  ed, pSnippet con
2e53f 74 61 69 6e 73 20 74 68 65 20 6c 69 73 74 20 6f  tains the list o
2e540 66 20 74 6f 6b 65 6e 20 0a 2a 2a 20 6f 66 66 73  f token .** offs
2e541 65 74 73 20 70 72 6f 64 75 63 65 64 20 62 79 20  ets produced by 
2e542 74 72 65 61 74 69 6e 67 20 61 6c 6c 20 4e 45 41  treating all NEA
2e543 52 20 6f 70 65 72 61 74 6f 72 73 20 61 73 20 41  R operators as A
2e544 4e 44 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a 2a  ND operators..**
2e545 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   This function r
2e546 65 6d 6f 76 65 73 20 61 6e 79 20 65 6e 74 72 69  emoves any entri
2e547 65 73 20 74 68 61 74 20 73 68 6f 75 6c 64 20 6e  es that should n
2e548 6f 74 20 62 65 20 70 72 65 73 65 6e 74 20 61 66  ot be present af
2e549 74 65 72 0a 2a 2a 20 61 63 63 6f 75 6e 74 69 6e  ter.** accountin
2e54a 67 20 66 6f 72 20 74 68 65 20 4e 45 41 52 20 72  g for the NEAR r
2e54b 65 73 74 72 69 63 74 69 6f 6e 2e 20 46 6f 72 20  estriction. For 
2e54c 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20  example, if the 
2e54d 71 75 65 72 69 65 64 0a 2a 2a 20 64 6f 63 75 6d  queried.** docum
2e54e 65 6e 74 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ent is:.**.**   
2e54f 20 20 22 41 20 42 20 43 20 44 20 45 20 41 22 0a    "A B C D E A".
2e550 2a 2a 0a 2a 2a 20 61 6e 64 20 74 68 65 20 71 75  **.** and the qu
2e551 65 72 79 20 69 73 3a 0a 2a 2a 20 0a 2a 2a 20 20  ery is:.** .**  
2e552 20 20 20 41 20 4e 45 41 52 2f 30 20 45 0a 2a 2a     A NEAR/0 E.**
2e553 0a 2a 2a 20 74 68 65 6e 20 77 68 65 6e 20 74 68  .** then when th
2e554 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
2e555 61 6c 6c 65 64 20 74 68 65 20 53 6e 69 70 70 65  alled the Snippe
2e556 74 20 63 6f 6e 74 61 69 6e 73 20 74 6f 6b 65 6e  t contains token
2e557 20 6f 66 66 73 65 74 73 0a 2a 2a 20 30 2c 20 34   offsets.** 0, 4
2e558 20 61 6e 64 20 35 2e 20 54 68 69 73 20 66 75 6e   and 5. This fun
2e559 63 74 69 6f 6e 20 72 65 6d 6f 76 65 73 20 74 68  ction removes th
2e55a 65 20 22 30 22 20 65 6e 74 72 79 20 28 62 65 63  e "0" entry (bec
2e55b 61 75 73 65 20 74 68 65 20 66 69 72 73 74 20 41  ause the first A
2e55c 0a 2a 2a 20 69 73 20 6e 6f 74 20 6e 65 61 72 20  .** is not near 
2e55d 65 6e 6f 75 67 68 20 74 6f 20 61 6e 20 45 29 2e  enough to an E).
2e55e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2e55f 74 72 69 6d 53 6e 69 70 70 65 74 4f 66 66 73 65  trimSnippetOffse
2e560 74 73 46 6f 72 4e 65 61 72 28 51 75 65 72 79 20  tsForNear(Query 
2e561 2a 70 51 75 65 72 79 2c 20 53 6e 69 70 70 65 74  *pQuery, Snippet
2e562 20 2a 70 53 6e 69 70 70 65 74 29 7b 0a 20 20 69   *pSnippet){.  i
2e563 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 69 44 69  nt ii;.  int iDi
2e564 72 20 3d 20 31 3b 0a 0a 20 20 77 68 69 6c 65 28  r = 1;..  while(
2e565 69 44 69 72 3e 2d 32 29 20 7b 0a 20 20 20 20 61  iDir>-2) {.    a
2e566 73 73 65 72 74 28 20 69 44 69 72 3d 3d 31 20 7c  ssert( iDir==1 |
2e567 7c 20 69 44 69 72 3d 3d 2d 31 20 29 3b 0a 20 20  | iDir==-1 );.  
2e568 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70    for(ii=0; ii<p
2e569 53 6e 69 70 70 65 74 2d 3e 6e 4d 61 74 63 68 3b  Snippet->nMatch;
2e56a 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e   ii++){.      in
2e56b 74 20 6a 6a 3b 0a 20 20 20 20 20 20 69 6e 74 20  t jj;.      int 
2e56c 6e 4e 65 61 72 3b 0a 20 20 20 20 20 20 73 74 72  nNear;.      str
2e56d 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74 63 68  uct snippetMatch
2e56e 20 2a 70 4d 61 74 63 68 20 3d 20 26 70 53 6e 69   *pMatch = &pSni
2e56f 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b 69 69 5d  ppet->aMatch[ii]
2e570 3b 0a 20 20 20 20 20 20 51 75 65 72 79 54 65 72  ;.      QueryTer
2e571 6d 20 2a 70 51 75 65 72 79 54 65 72 6d 20 3d 20  m *pQueryTerm = 
2e572 26 70 51 75 65 72 79 2d 3e 70 54 65 72 6d 73 5b  &pQuery->pTerms[
2e573 70 4d 61 74 63 68 2d 3e 69 54 65 72 6d 5d 3b 0a  pMatch->iTerm];.
2e574 0a 20 20 20 20 20 20 69 66 28 20 28 70 4d 61 74  .      if( (pMat
2e575 63 68 2d 3e 69 54 65 72 6d 2b 69 44 69 72 29 3c  ch->iTerm+iDir)<
2e576 30 20 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 4d  0 .       || (pM
2e577 61 74 63 68 2d 3e 69 54 65 72 6d 2b 69 44 69 72  atch->iTerm+iDir
2e578 29 3e 3d 70 51 75 65 72 79 2d 3e 6e 54 65 72 6d  )>=pQuery->nTerm
2e579 73 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20  s.      ){.     
2e57a 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
2e57b 20 20 20 7d 0a 20 20 20 20 20 0a 20 20 20 20 20     }.     .     
2e57c 20 6e 4e 65 61 72 20 3d 20 70 51 75 65 72 79 54   nNear = pQueryT
2e57d 65 72 6d 2d 3e 6e 4e 65 61 72 3b 0a 20 20 20 20  erm->nNear;.    
2e57e 20 20 69 66 28 20 69 44 69 72 3c 30 20 29 7b 0a    if( iDir<0 ){.
2e57f 20 20 20 20 20 20 20 20 6e 4e 65 61 72 20 3d 20          nNear = 
2e580 70 51 75 65 72 79 54 65 72 6d 5b 2d 31 5d 2e 6e  pQueryTerm[-1].n
2e581 4e 65 61 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Near;.      }.  
2e582 0a 20 20 20 20 20 20 69 66 28 20 70 4d 61 74 63  .      if( pMatc
2e583 68 2d 3e 69 54 65 72 6d 3e 3d 30 20 26 26 20 6e  h->iTerm>=0 && n
2e584 4e 65 61 72 20 29 7b 0a 20 20 20 20 20 20 20 20  Near ){.        
2e585 69 6e 74 20 69 73 4f 6b 20 3d 20 30 3b 0a 20 20  int isOk = 0;.  
2e586 20 20 20 20 20 20 69 6e 74 20 69 4e 65 78 74 54        int iNextT
2e587 65 72 6d 20 3d 20 70 4d 61 74 63 68 2d 3e 69 54  erm = pMatch->iT
2e588 65 72 6d 2b 69 44 69 72 3b 0a 20 20 20 20 20 20  erm+iDir;.      
2e589 20 20 69 6e 74 20 69 50 72 65 76 54 65 72 6d 20    int iPrevTerm 
2e58a 3d 20 69 4e 65 78 74 54 65 72 6d 3b 0a 0a 20 20  = iNextTerm;..  
2e58b 20 20 20 20 20 20 69 6e 74 20 69 45 6e 64 54 6f        int iEndTo
2e58c 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  ken;.        int
2e58d 20 69 53 74 61 72 74 54 6f 6b 65 6e 3b 0a 0a 20   iStartToken;.. 
2e58e 20 20 20 20 20 20 20 69 66 28 20 69 44 69 72 3c         if( iDir<
2e58f 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  0 ){.          i
2e590 6e 74 20 6e 50 68 72 61 73 65 20 3d 20 31 3b 0a  nt nPhrase = 1;.
2e591 20 20 20 20 20 20 20 20 20 20 69 53 74 61 72 74            iStart
2e592 54 6f 6b 65 6e 20 3d 20 70 4d 61 74 63 68 2d 3e  Token = pMatch->
2e593 69 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20  iToken;.        
2e594 20 20 77 68 69 6c 65 28 20 28 70 4d 61 74 63 68    while( (pMatch
2e595 2d 3e 69 54 65 72 6d 2b 6e 50 68 72 61 73 65 29  ->iTerm+nPhrase)
2e596 3c 70 51 75 65 72 79 2d 3e 6e 54 65 72 6d 73 20  <pQuery->nTerms 
2e597 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26  .              &
2e598 26 20 70 51 75 65 72 79 2d 3e 70 54 65 72 6d 73  & pQuery->pTerms
2e599 5b 70 4d 61 74 63 68 2d 3e 69 54 65 72 6d 2b 6e  [pMatch->iTerm+n
2e59a 50 68 72 61 73 65 5d 2e 69 50 68 72 61 73 65 3e  Phrase].iPhrase>
2e59b 31 20 0a 20 20 20 20 20 20 20 20 20 20 29 7b 0a  1 .          ){.
2e59c 20 20 20 20 20 20 20 20 20 20 20 20 6e 50 68 72              nPhr
2e59d 61 73 65 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20  ase++;.         
2e59e 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 45 6e   }.          iEn
2e59f 64 54 6f 6b 65 6e 20 3d 20 69 53 74 61 72 74 54  dToken = iStartT
2e5a0 6f 6b 65 6e 20 2b 20 6e 50 68 72 61 73 65 20 2d  oken + nPhrase -
2e5a1 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73   1;.        }els
2e5a2 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69 45 6e  e{.          iEn
2e5a3 64 54 6f 6b 65 6e 20 20 20 3d 20 70 4d 61 74 63  dToken   = pMatc
2e5a4 68 2d 3e 69 54 6f 6b 65 6e 3b 0a 20 20 20 20 20  h->iToken;.     
2e5a5 20 20 20 20 20 69 53 74 61 72 74 54 6f 6b 65 6e       iStartToken
2e5a6 20 3d 20 70 4d 61 74 63 68 2d 3e 69 54 6f 6b 65   = pMatch->iToke
2e5a7 6e 2b 31 2d 70 51 75 65 72 79 54 65 72 6d 2d 3e  n+1-pQueryTerm->
2e5a8 69 50 68 72 61 73 65 3b 0a 20 20 20 20 20 20 20  iPhrase;.       
2e5a9 20 7d 0a 0a 20 20 20 20 20 20 20 20 77 68 69 6c   }..        whil
2e5aa 65 28 20 70 51 75 65 72 79 2d 3e 70 54 65 72 6d  e( pQuery->pTerm
2e5ab 73 5b 69 4e 65 78 74 54 65 72 6d 5d 2e 69 50 68  s[iNextTerm].iPh
2e5ac 72 61 73 65 3e 31 20 29 7b 0a 20 20 20 20 20 20  rase>1 ){.      
2e5ad 20 20 20 20 69 4e 65 78 74 54 65 72 6d 2d 2d 3b      iNextTerm--;
2e5ae 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2e5af 20 20 20 77 68 69 6c 65 28 20 28 69 50 72 65 76     while( (iPrev
2e5b0 54 65 72 6d 2b 31 29 3c 70 51 75 65 72 79 2d 3e  Term+1)<pQuery->
2e5b1 6e 54 65 72 6d 73 20 26 26 20 0a 20 20 20 20 20  nTerms && .     
2e5b2 20 20 20 20 20 20 20 20 20 20 70 51 75 65 72 79            pQuery
2e5b3 2d 3e 70 54 65 72 6d 73 5b 69 50 72 65 76 54 65  ->pTerms[iPrevTe
2e5b4 72 6d 2b 31 5d 2e 69 50 68 72 61 73 65 3e 31 20  rm+1].iPhrase>1 
2e5b5 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20  .        ){.    
2e5b6 20 20 20 20 20 20 69 50 72 65 76 54 65 72 6d 2b        iPrevTerm+
2e5b7 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 0a  +;.        }.  .
2e5b8 20 20 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d 30          for(jj=0
2e5b9 3b 20 69 73 4f 6b 3d 3d 30 20 26 26 20 6a 6a 3c  ; isOk==0 && jj<
2e5ba 70 53 6e 69 70 70 65 74 2d 3e 6e 4d 61 74 63 68  pSnippet->nMatch
2e5bb 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  ; jj++){.       
2e5bc 20 20 20 73 74 72 75 63 74 20 73 6e 69 70 70 65     struct snippe
2e5bd 74 4d 61 74 63 68 20 2a 70 20 3d 20 26 70 53 6e  tMatch *p = &pSn
2e5be 69 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b 6a 6a  ippet->aMatch[jj
2e5bf 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ];.          if(
2e5c0 20 70 2d 3e 69 43 6f 6c 3d 3d 70 4d 61 74 63 68   p->iCol==pMatch
2e5c1 2d 3e 69 43 6f 6c 20 26 26 20 28 28 0a 20 20 20  ->iCol && ((.   
2e5c2 20 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69              p->i
2e5c3 54 65 72 6d 3d 3d 69 4e 65 78 74 54 65 72 6d 20  Term==iNextTerm 
2e5c4 26 26 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  && .            
2e5c5 20 20 20 70 2d 3e 69 54 6f 6b 65 6e 3e 69 45 6e     p->iToken>iEn
2e5c6 64 54 6f 6b 65 6e 20 26 26 20 0a 20 20 20 20 20  dToken && .     
2e5c7 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 54 6f            p->iTo
2e5c8 6b 65 6e 3c 3d 69 45 6e 64 54 6f 6b 65 6e 2b 6e  ken<=iEndToken+n
2e5c9 4e 65 61 72 0a 20 20 20 20 20 20 20 20 20 20 29  Near.          )
2e5ca 20 7c 7c 20 28 0a 20 20 20 20 20 20 20 20 20 20   || (.          
2e5cb 20 20 20 20 20 70 2d 3e 69 54 65 72 6d 3d 3d 69       p->iTerm==i
2e5cc 50 72 65 76 54 65 72 6d 20 26 26 20 0a 20 20 20  PrevTerm && .   
2e5cd 20 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69              p->i
2e5ce 54 6f 6b 65 6e 3c 69 53 74 61 72 74 54 6f 6b 65  Token<iStartToke
2e5cf 6e 20 26 26 20 0a 20 20 20 20 20 20 20 20 20 20  n && .          
2e5d0 20 20 20 20 20 70 2d 3e 69 54 6f 6b 65 6e 3e 3d       p->iToken>=
2e5d1 69 53 74 61 72 74 54 6f 6b 65 6e 2d 6e 4e 65 61  iStartToken-nNea
2e5d2 72 0a 20 20 20 20 20 20 20 20 20 20 29 29 29 7b  r.          ))){
2e5d3 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 73 4f  .            isO
2e5d4 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  k = 1;.         
2e5d5 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
2e5d6 20 20 20 20 20 69 66 28 20 21 69 73 4f 6b 20 29       if( !isOk )
2e5d7 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28  {.          for(
2e5d8 6a 6a 3d 31 2d 70 51 75 65 72 79 54 65 72 6d 2d  jj=1-pQueryTerm-
2e5d9 3e 69 50 68 72 61 73 65 3b 20 6a 6a 3c 3d 30 3b  >iPhrase; jj<=0;
2e5da 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20   jj++){.        
2e5db 20 20 20 20 70 4d 61 74 63 68 5b 6a 6a 5d 2e 69      pMatch[jj].i
2e5dc 54 65 72 6d 20 3d 20 2d 31 3b 0a 20 20 20 20 20  Term = -1;.     
2e5dd 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
2e5de 20 69 69 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20   ii = -1;.      
2e5df 20 20 20 20 69 44 69 72 20 3d 20 31 3b 0a 20 20      iDir = 1;.  
2e5e0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2e5e1 20 20 20 20 7d 0a 20 20 20 20 69 44 69 72 20 2d      }.    iDir -
2e5e2 3d 20 32 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  = 2;.  }.}../*.*
2e5e3 2a 20 43 6f 6d 70 75 74 65 20 61 6c 6c 20 6f 66  * Compute all of
2e5e4 66 73 65 74 73 20 66 6f 72 20 74 68 65 20 63 75  fsets for the cu
2e5e5 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65  rrent row of the
2e5e6 20 71 75 65 72 79 2e 20 20 0a 2a 2a 20 49 66 20   query.  .** If 
2e5e7 74 68 65 20 6f 66 66 73 65 74 73 20 68 61 76 65  the offsets have
2e5e8 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f   already been co
2e5e9 6d 70 75 74 65 64 2c 20 74 68 69 73 20 72 6f 75  mputed, this rou
2e5ea 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tine is a no-op.
2e5eb 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2e5ec 73 6e 69 70 70 65 74 41 6c 6c 4f 66 66 73 65 74  snippetAllOffset
2e5ed 73 28 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f  s(fulltext_curso
2e5ee 72 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 43 6f  r *p){.  int nCo
2e5ef 6c 75 6d 6e 3b 0a 20 20 69 6e 74 20 69 43 6f 6c  lumn;.  int iCol
2e5f0 75 6d 6e 2c 20 69 3b 0a 20 20 69 6e 74 20 69 46  umn, i;.  int iF
2e5f1 69 72 73 74 2c 20 69 4c 61 73 74 3b 0a 20 20 66  irst, iLast;.  f
2e5f2 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 70 46  ulltext_vtab *pF
2e5f3 74 73 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 73 6e  ts;..  if( p->sn
2e5f4 69 70 70 65 74 2e 6e 4d 61 74 63 68 20 29 20 72  ippet.nMatch ) r
2e5f5 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e  eturn;.  if( p->
2e5f6 71 2e 6e 54 65 72 6d 73 3d 3d 30 20 29 20 72 65  q.nTerms==0 ) re
2e5f7 74 75 72 6e 3b 0a 20 20 70 46 74 73 20 3d 20 70  turn;.  pFts = p
2e5f8 2d 3e 71 2e 70 46 74 73 3b 0a 20 20 6e 43 6f 6c  ->q.pFts;.  nCol
2e5f9 75 6d 6e 20 3d 20 70 46 74 73 2d 3e 6e 43 6f 6c  umn = pFts->nCol
2e5fa 75 6d 6e 3b 0a 20 20 69 43 6f 6c 75 6d 6e 20 3d  umn;.  iColumn =
2e5fb 20 28 70 2d 3e 69 43 75 72 73 6f 72 54 79 70 65   (p->iCursorType
2e5fc 20 2d 20 51 55 45 52 59 5f 46 55 4c 4c 54 45 58   - QUERY_FULLTEX
2e5fd 54 29 3b 0a 20 20 69 66 28 20 69 43 6f 6c 75 6d  T);.  if( iColum
2e5fe 6e 3c 30 20 7c 7c 20 69 43 6f 6c 75 6d 6e 3e 3d  n<0 || iColumn>=
2e5ff 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 69  nColumn ){.    i
2e600 46 69 72 73 74 20 3d 20 30 3b 0a 20 20 20 20 69  First = 0;.    i
2e601 4c 61 73 74 20 3d 20 6e 43 6f 6c 75 6d 6e 2d 31  Last = nColumn-1
2e602 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
2e603 46 69 72 73 74 20 3d 20 69 43 6f 6c 75 6d 6e 3b  First = iColumn;
2e604 0a 20 20 20 20 69 4c 61 73 74 20 3d 20 69 43 6f  .    iLast = iCo
2e605 6c 75 6d 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  lumn;.  }.  for(
2e606 69 3d 69 46 69 72 73 74 3b 20 69 3c 3d 69 4c 61  i=iFirst; i<=iLa
2e607 73 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 6f  st; i++){.    co
2e608 6e 73 74 20 63 68 61 72 20 2a 7a 44 6f 63 3b 0a  nst char *zDoc;.
2e609 20 20 20 20 69 6e 74 20 6e 44 6f 63 3b 0a 20 20      int nDoc;.  
2e60a 20 20 7a 44 6f 63 20 3d 20 28 63 6f 6e 73 74 20    zDoc = (const 
2e60b 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 63 6f  char*)sqlite3_co
2e60c 6c 75 6d 6e 5f 74 65 78 74 28 70 2d 3e 70 53 74  lumn_text(p->pSt
2e60d 6d 74 2c 20 69 2b 31 29 3b 0a 20 20 20 20 6e 44  mt, i+1);.    nD
2e60e 6f 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  oc = sqlite3_col
2e60f 75 6d 6e 5f 62 79 74 65 73 28 70 2d 3e 70 53 74  umn_bytes(p->pSt
2e610 6d 74 2c 20 69 2b 31 29 3b 0a 20 20 20 20 73 6e  mt, i+1);.    sn
2e611 69 70 70 65 74 4f 66 66 73 65 74 73 4f 66 43 6f  ippetOffsetsOfCo
2e612 6c 75 6d 6e 28 26 70 2d 3e 71 2c 20 26 70 2d 3e  lumn(&p->q, &p->
2e613 73 6e 69 70 70 65 74 2c 20 69 2c 20 7a 44 6f 63  snippet, i, zDoc
2e614 2c 20 6e 44 6f 63 29 3b 0a 20 20 7d 0a 0a 20 20  , nDoc);.  }..  
2e615 74 72 69 6d 53 6e 69 70 70 65 74 4f 66 66 73 65  trimSnippetOffse
2e616 74 73 46 6f 72 4e 65 61 72 28 26 70 2d 3e 71 2c  tsForNear(&p->q,
2e617 20 26 70 2d 3e 73 6e 69 70 70 65 74 29 3b 0a 7d   &p->snippet);.}
2e618 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  ../*.** Convert 
2e619 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  the information 
2e61a 69 6e 20 74 68 65 20 61 4d 61 74 63 68 5b 5d 20  in the aMatch[] 
2e61b 61 72 72 61 79 20 6f 66 20 74 68 65 20 73 6e 69  array of the sni
2e61c 70 70 65 74 0a 2a 2a 20 69 6e 74 6f 20 74 68 65  ppet.** into the
2e61d 20 73 74 72 69 6e 67 20 7a 4f 66 66 73 65 74 5b   string zOffset[
2e61e 30 2e 2e 6e 4f 66 66 73 65 74 2d 31 5d 2e 0a 2a  0..nOffset-1]..*
2e61f 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 6e  /.static void sn
2e620 69 70 70 65 74 4f 66 66 73 65 74 54 65 78 74 28  ippetOffsetText(
2e621 53 6e 69 70 70 65 74 20 2a 70 29 7b 0a 20 20 69  Snippet *p){.  i
2e622 6e 74 20 69 3b 0a 20 20 69 6e 74 20 63 6e 74 20  nt i;.  int cnt 
2e623 3d 20 30 3b 0a 20 20 53 74 72 69 6e 67 42 75 66  = 0;.  StringBuf
2e624 66 65 72 20 73 62 3b 0a 20 20 63 68 61 72 20 7a  fer sb;.  char z
2e625 42 75 66 5b 32 30 30 5d 3b 0a 20 20 69 66 28 20  Buf[200];.  if( 
2e626 70 2d 3e 7a 4f 66 66 73 65 74 20 29 20 72 65 74  p->zOffset ) ret
2e627 75 72 6e 3b 0a 20 20 69 6e 69 74 53 74 72 69 6e  urn;.  initStrin
2e628 67 42 75 66 66 65 72 28 26 73 62 29 3b 0a 20 20  gBuffer(&sb);.  
2e629 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4d  for(i=0; i<p->nM
2e62a 61 74 63 68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  atch; i++){.    
2e62b 73 74 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61  struct snippetMa
2e62c 74 63 68 20 2a 70 4d 61 74 63 68 20 3d 20 26 70  tch *pMatch = &p
2e62d 2d 3e 61 4d 61 74 63 68 5b 69 5d 3b 0a 20 20 20  ->aMatch[i];.   
2e62e 20 69 66 28 20 70 4d 61 74 63 68 2d 3e 69 54 65   if( pMatch->iTe
2e62f 72 6d 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f  rm>=0 ){.      /
2e630 2a 20 49 66 20 73 6e 69 70 70 65 74 4d 61 74 63  * If snippetMatc
2e631 68 2e 69 54 65 72 6d 20 69 73 20 6c 65 73 73 20  h.iTerm is less 
2e632 74 68 61 6e 20 30 2c 20 74 68 65 6e 20 74 68 65  than 0, then the
2e633 20 6d 61 74 63 68 20 77 61 73 20 0a 20 20 20 20   match was .    
2e634 20 20 2a 2a 20 64 69 73 63 61 72 64 65 64 20 61    ** discarded a
2e635 73 20 70 61 72 74 20 6f 66 20 70 72 6f 63 65 73  s part of proces
2e636 73 69 6e 67 20 74 68 65 20 4e 45 41 52 20 6f 70  sing the NEAR op
2e637 65 72 61 74 6f 72 20 28 73 65 65 20 74 68 65 20  erator (see the 
2e638 0a 20 20 20 20 20 20 2a 2a 20 74 72 69 6d 53 6e  .      ** trimSn
2e639 69 70 70 65 74 4f 66 66 73 65 74 73 46 6f 72 4e  ippetOffsetsForN
2e63a 65 61 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 66  ear() function f
2e63b 6f 72 20 64 65 74 61 69 6c 73 29 2e 20 49 67 6e  or details). Ign
2e63c 6f 72 65 20 0a 20 20 20 20 20 20 2a 2a 20 69 74  ore .      ** it
2e63d 20 69 6e 20 74 68 69 73 20 63 61 73 65 0a 20 20   in this case.  
2e63e 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 7a 42 75      */.      zBu
2e63f 66 5b 30 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20  f[0] = ' ';.    
2e640 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
2e641 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2d  tf(sizeof(zBuf)-
2e642 31 2c 20 26 7a 42 75 66 5b 63 6e 74 3e 30 5d 2c  1, &zBuf[cnt>0],
2e643 20 22 25 64 20 25 64 20 25 64 20 25 64 22 2c 0a   "%d %d %d %d",.
2e644 20 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68            pMatch
2e645 2d 3e 69 43 6f 6c 2c 20 70 4d 61 74 63 68 2d 3e  ->iCol, pMatch->
2e646 69 54 65 72 6d 2c 20 70 4d 61 74 63 68 2d 3e 69  iTerm, pMatch->i
2e647 53 74 61 72 74 2c 20 70 4d 61 74 63 68 2d 3e 6e  Start, pMatch->n
2e648 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 70 70  Byte);.      app
2e649 65 6e 64 28 26 73 62 2c 20 7a 42 75 66 29 3b 0a  end(&sb, zBuf);.
2e64a 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20        cnt++;.   
2e64b 20 7d 0a 20 20 7d 0a 20 20 70 2d 3e 7a 4f 66 66   }.  }.  p->zOff
2e64c 73 65 74 20 3d 20 73 74 72 69 6e 67 42 75 66 66  set = stringBuff
2e64d 65 72 44 61 74 61 28 26 73 62 29 3b 0a 20 20 70  erData(&sb);.  p
2e64e 2d 3e 6e 4f 66 66 73 65 74 20 3d 20 73 74 72 69  ->nOffset = stri
2e64f 6e 67 42 75 66 66 65 72 4c 65 6e 67 74 68 28 26  ngBufferLength(&
2e650 73 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 7a 44  sb);.}../*.** zD
2e651 6f 63 5b 30 2e 2e 6e 44 6f 63 2d 31 5d 20 69 73  oc[0..nDoc-1] is
2e652 20 70 68 72 61 73 65 20 6f 66 20 74 65 78 74 2e   phrase of text.
2e653 20 20 61 4d 61 74 63 68 5b 30 2e 2e 6e 4d 61 74    aMatch[0..nMat
2e654 63 68 2d 31 5d 20 61 72 65 20 61 20 73 65 74 0a  ch-1] are a set.
2e655 2a 2a 20 6f 66 20 6d 61 74 63 68 69 6e 67 20 77  ** of matching w
2e656 6f 72 64 73 20 73 6f 6d 65 20 6f 66 20 77 68 69  ords some of whi
2e657 63 68 20 6d 69 67 68 74 20 62 65 20 69 6e 20 7a  ch might be in z
2e658 44 6f 63 2e 20 20 7a 44 6f 63 20 69 73 20 63 6f  Doc.  zDoc is co
2e659 6c 75 6d 6e 0a 2a 2a 20 6e 75 6d 62 65 72 20 69  lumn.** number i
2e65a 43 6f 6c 2e 0a 2a 2a 0a 2a 2a 20 69 42 72 65 61  Col..**.** iBrea
2e65b 6b 20 69 73 20 73 75 67 67 65 73 74 65 64 20 73  k is suggested s
2e65c 70 6f 74 20 69 6e 20 7a 44 6f 63 20 77 68 65 72  pot in zDoc wher
2e65d 65 20 77 65 20 63 6f 75 6c 64 20 62 65 67 69 6e  e we could begin
2e65e 20 6f 72 20 65 6e 64 20 61 6e 0a 2a 2a 20 65 78   or end an.** ex
2e65f 63 65 72 70 74 2e 20 20 52 65 74 75 72 6e 20 61  cerpt.  Return a
2e660 20 76 61 6c 75 65 20 73 69 6d 69 6c 61 72 20 74   value similar t
2e661 6f 20 69 42 72 65 61 6b 20 62 75 74 20 70 6f 73  o iBreak but pos
2e662 73 69 62 6c 79 20 61 64 6a 75 73 74 65 64 0a 2a  sibly adjusted.*
2e663 2a 20 74 6f 20 62 65 20 61 20 6c 69 74 74 6c 65  * to be a little
2e664 20 6c 65 66 74 20 6f 72 20 72 69 67 68 74 20 73   left or right s
2e665 6f 20 74 68 61 74 20 74 68 65 20 62 72 65 61 6b  o that the break
2e666 20 70 6f 69 6e 74 20 69 73 20 62 65 74 74 65 72   point is better
2e667 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2e668 77 6f 72 64 42 6f 75 6e 64 61 72 79 28 0a 20 20  wordBoundary(.  
2e669 69 6e 74 20 69 42 72 65 61 6b 2c 20 20 20 20 20  int iBreak,     
2e66a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2e66b 20 54 68 65 20 73 75 67 67 65 73 74 65 64 20 62   The suggested b
2e66c 72 65 61 6b 20 70 6f 69 6e 74 20 2a 2f 0a 20 20  reak point */.  
2e66d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 6f 63  const char *zDoc
2e66e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
2e66f 20 44 6f 63 75 6d 65 6e 74 20 74 65 78 74 20 2a   Document text *
2e670 2f 0a 20 20 69 6e 74 20 6e 44 6f 63 2c 20 20 20  /.  int nDoc,   
2e671 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e672 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
2e673 79 74 65 73 20 69 6e 20 7a 44 6f 63 5b 5d 20 2a  ytes in zDoc[] *
2e674 2f 0a 20 20 73 74 72 75 63 74 20 73 6e 69 70 70  /.  struct snipp
2e675 65 74 4d 61 74 63 68 20 2a 61 4d 61 74 63 68 2c  etMatch *aMatch,
2e676 20 20 2f 2a 20 4d 61 74 63 68 69 6e 67 20 77 6f    /* Matching wo
2e677 72 64 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61  rds */.  int nMa
2e678 74 63 68 2c 20 20 20 20 20 20 20 20 20 20 20 20  tch,            
2e679 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
2e67a 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
2e67b 4d 61 74 63 68 5b 5d 20 2a 2f 0a 20 20 69 6e 74  Match[] */.  int
2e67c 20 69 43 6f 6c 20 20 20 20 20 20 20 20 20 20 20   iCol           
2e67d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2e67e 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20  e column number 
2e67f 66 6f 72 20 7a 44 6f 63 5b 5d 20 2a 2f 0a 29 7b  for zDoc[] */.){
2e680 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
2e681 69 42 72 65 61 6b 3c 3d 31 30 20 29 7b 0a 20 20  iBreak<=10 ){.  
2e682 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
2e683 20 20 69 66 28 20 69 42 72 65 61 6b 3e 3d 6e 44    if( iBreak>=nD
2e684 6f 63 2d 31 30 20 29 7b 0a 20 20 20 20 72 65 74  oc-10 ){.    ret
2e685 75 72 6e 20 6e 44 6f 63 3b 0a 20 20 7d 0a 20 20  urn nDoc;.  }.  
2e686 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4d 61 74 63  for(i=0; i<nMatc
2e687 68 20 26 26 20 61 4d 61 74 63 68 5b 69 5d 2e 69  h && aMatch[i].i
2e688 43 6f 6c 3c 69 43 6f 6c 3b 20 69 2b 2b 29 7b 7d  Col<iCol; i++){}
2e689 0a 20 20 77 68 69 6c 65 28 20 69 3c 6e 4d 61 74  .  while( i<nMat
2e68a 63 68 20 26 26 20 61 4d 61 74 63 68 5b 69 5d 2e  ch && aMatch[i].
2e68b 69 53 74 61 72 74 2b 61 4d 61 74 63 68 5b 69 5d  iStart+aMatch[i]
2e68c 2e 6e 42 79 74 65 3c 69 42 72 65 61 6b 20 29 7b  .nByte<iBreak ){
2e68d 20 69 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 69 3c   i++; }.  if( i<
2e68e 6e 4d 61 74 63 68 20 29 7b 0a 20 20 20 20 69 66  nMatch ){.    if
2e68f 28 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61  ( aMatch[i].iSta
2e690 72 74 3c 69 42 72 65 61 6b 2b 31 30 20 29 7b 0a  rt<iBreak+10 ){.
2e691 20 20 20 20 20 20 72 65 74 75 72 6e 20 61 4d 61        return aMa
2e692 74 63 68 5b 69 5d 2e 69 53 74 61 72 74 3b 0a 20  tch[i].iStart;. 
2e693 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3e 30     }.    if( i>0
2e694 20 26 26 20 61 4d 61 74 63 68 5b 69 2d 31 5d 2e   && aMatch[i-1].
2e695 69 53 74 61 72 74 2b 61 4d 61 74 63 68 5b 69 2d  iStart+aMatch[i-
2e696 31 5d 2e 6e 42 79 74 65 3e 3d 69 42 72 65 61 6b  1].nByte>=iBreak
2e697 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
2e698 20 61 4d 61 74 63 68 5b 69 2d 31 5d 2e 69 53 74   aMatch[i-1].iSt
2e699 61 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  art;.    }.  }. 
2e69a 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 31 30 3b   for(i=1; i<=10;
2e69b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73   i++){.    if( s
2e69c 61 66 65 5f 69 73 73 70 61 63 65 28 7a 44 6f 63  afe_isspace(zDoc
2e69d 5b 69 42 72 65 61 6b 2d 69 5d 29 20 29 7b 0a 20  [iBreak-i]) ){. 
2e69e 20 20 20 20 20 72 65 74 75 72 6e 20 69 42 72 65       return iBre
2e69f 61 6b 20 2d 20 69 20 2b 20 31 3b 0a 20 20 20 20  ak - i + 1;.    
2e6a0 7d 0a 20 20 20 20 69 66 28 20 73 61 66 65 5f 69  }.    if( safe_i
2e6a1 73 73 70 61 63 65 28 7a 44 6f 63 5b 69 42 72 65  sspace(zDoc[iBre
2e6a2 61 6b 2b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20  ak+i]) ){.      
2e6a3 72 65 74 75 72 6e 20 69 42 72 65 61 6b 20 2b 20  return iBreak + 
2e6a4 69 20 2b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  i + 1;.    }.  }
2e6a5 0a 20 20 72 65 74 75 72 6e 20 69 42 72 65 61 6b  .  return iBreak
2e6a6 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  ;.}..../*.** All
2e6a7 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20  owed values for 
2e6a8 53 6e 69 70 70 65 74 2e 61 4d 61 74 63 68 5b 5d  Snippet.aMatch[]
2e6a9 2e 73 6e 53 74 61 74 75 73 0a 2a 2f 0a 23 64 65  .snStatus.*/.#de
2e6aa 66 69 6e 65 20 53 4e 49 50 50 45 54 5f 49 47 4e  fine SNIPPET_IGN
2e6ab 4f 52 45 20 20 30 20 20 20 2f 2a 20 49 74 20 69  ORE  0   /* It i
2e6ac 73 20 6f 6b 20 74 6f 20 6f 6d 69 74 20 74 68 69  s ok to omit thi
2e6ad 73 20 6d 61 74 63 68 20 66 72 6f 6d 20 74 68 65  s match from the
2e6ae 20 73 6e 69 70 70 65 74 20 2a 2f 0a 23 64 65 66   snippet */.#def
2e6af 69 6e 65 20 53 4e 49 50 50 45 54 5f 44 45 53 49  ine SNIPPET_DESI
2e6b0 52 45 44 20 31 20 20 20 2f 2a 20 57 65 20 77 61  RED 1   /* We wa
2e6b1 6e 74 20 74 6f 20 69 6e 63 6c 75 64 65 20 74 68  nt to include th
2e6b2 69 73 20 6d 61 74 63 68 20 69 6e 20 74 68 65 20  is match in the 
2e6b3 73 6e 69 70 70 65 74 20 2a 2f 0a 0a 2f 2a 0a 2a  snippet */../*.*
2e6b4 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 74  * Generate the t
2e6b5 65 78 74 20 6f 66 20 61 20 73 6e 69 70 70 65 74  ext of a snippet
2e6b6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2e6b7 20 73 6e 69 70 70 65 74 54 65 78 74 28 0a 20 20   snippetText(.  
2e6b8 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20  fulltext_cursor 
2e6b9 2a 70 43 75 72 73 6f 72 2c 20 20 20 2f 2a 20 54  *pCursor,   /* T
2e6ba 68 65 20 63 75 72 73 6f 72 20 77 65 20 6e 65 65  he cursor we nee
2e6bb 64 20 74 68 65 20 73 6e 69 70 70 65 74 20 66 6f  d the snippet fo
2e6bc 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  r */.  const cha
2e6bd 72 20 2a 7a 53 74 61 72 74 4d 61 72 6b 2c 20 20  r *zStartMark,  
2e6be 20 20 20 2f 2a 20 4d 61 72 6b 75 70 20 74 6f 20     /* Markup to 
2e6bf 61 70 70 65 61 72 20 62 65 66 6f 72 65 20 65 61  appear before ea
2e6c0 63 68 20 6d 61 74 63 68 20 2a 2f 0a 20 20 63 6f  ch match */.  co
2e6c1 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e 64 4d 61  nst char *zEndMa
2e6c2 72 6b 2c 20 20 20 20 20 20 20 2f 2a 20 4d 61 72  rk,       /* Mar
2e6c3 6b 75 70 20 74 6f 20 61 70 70 65 61 72 20 61 66  kup to appear af
2e6c4 74 65 72 20 65 61 63 68 20 6d 61 74 63 68 20 2a  ter each match *
2e6c5 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
2e6c6 7a 45 6c 6c 69 70 73 69 73 20 20 20 20 20 20 20  zEllipsis       
2e6c7 2f 2a 20 45 6c 6c 69 70 73 69 73 20 6d 61 72 6b  /* Ellipsis mark
2e6c8 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20   */.){.  int i, 
2e6c9 6a 3b 0a 20 20 73 74 72 75 63 74 20 73 6e 69 70  j;.  struct snip
2e6ca 70 65 74 4d 61 74 63 68 20 2a 61 4d 61 74 63 68  petMatch *aMatch
2e6cb 3b 0a 20 20 69 6e 74 20 6e 4d 61 74 63 68 3b 0a  ;.  int nMatch;.
2e6cc 20 20 69 6e 74 20 6e 44 65 73 69 72 65 64 3b 0a    int nDesired;.
2e6cd 20 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 73    StringBuffer s
2e6ce 62 3b 0a 20 20 69 6e 74 20 74 61 69 6c 43 6f 6c  b;.  int tailCol
2e6cf 3b 0a 20 20 69 6e 74 20 74 61 69 6c 4f 66 66 73  ;.  int tailOffs
2e6d0 65 74 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 0a  et;.  int iCol;.
2e6d1 20 20 69 6e 74 20 6e 44 6f 63 3b 0a 20 20 63 6f    int nDoc;.  co
2e6d2 6e 73 74 20 63 68 61 72 20 2a 7a 44 6f 63 3b 0a  nst char *zDoc;.
2e6d3 20 20 69 6e 74 20 69 53 74 61 72 74 2c 20 69 45    int iStart, iE
2e6d4 6e 64 3b 0a 20 20 69 6e 74 20 74 61 69 6c 45 6c  nd;.  int tailEl
2e6d5 6c 69 70 73 69 73 20 3d 20 30 3b 0a 20 20 69 6e  lipsis = 0;.  in
2e6d6 74 20 69 4d 61 74 63 68 3b 0a 20 20 0a 0a 20 20  t iMatch;.  ..  
2e6d7 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 75  sqlite3_free(pCu
2e6d8 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 7a 53  rsor->snippet.zS
2e6d9 6e 69 70 70 65 74 29 3b 0a 20 20 70 43 75 72 73  nippet);.  pCurs
2e6da 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 7a 53 6e 69  or->snippet.zSni
2e6db 70 70 65 74 20 3d 20 30 3b 0a 20 20 61 4d 61 74  ppet = 0;.  aMat
2e6dc 63 68 20 3d 20 70 43 75 72 73 6f 72 2d 3e 73 6e  ch = pCursor->sn
2e6dd 69 70 70 65 74 2e 61 4d 61 74 63 68 3b 0a 20 20  ippet.aMatch;.  
2e6de 6e 4d 61 74 63 68 20 3d 20 70 43 75 72 73 6f 72  nMatch = pCursor
2e6df 2d 3e 73 6e 69 70 70 65 74 2e 6e 4d 61 74 63 68  ->snippet.nMatch
2e6e0 3b 0a 20 20 69 6e 69 74 53 74 72 69 6e 67 42 75  ;.  initStringBu
2e6e1 66 66 65 72 28 26 73 62 29 3b 0a 0a 20 20 66 6f  ffer(&sb);..  fo
2e6e2 72 28 69 3d 30 3b 20 69 3c 6e 4d 61 74 63 68 3b  r(i=0; i<nMatch;
2e6e3 20 69 2b 2b 29 7b 0a 20 20 20 20 61 4d 61 74 63   i++){.    aMatc
2e6e4 68 5b 69 5d 2e 73 6e 53 74 61 74 75 73 20 3d 20  h[i].snStatus = 
2e6e5 53 4e 49 50 50 45 54 5f 49 47 4e 4f 52 45 3b 0a  SNIPPET_IGNORE;.
2e6e6 20 20 7d 0a 20 20 6e 44 65 73 69 72 65 64 20 3d    }.  nDesired =
2e6e7 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   0;.  for(i=0; i
2e6e8 3c 70 43 75 72 73 6f 72 2d 3e 71 2e 6e 54 65 72  <pCursor->q.nTer
2e6e9 6d 73 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 66 6f  ms; i++){.    fo
2e6ea 72 28 6a 3d 30 3b 20 6a 3c 6e 4d 61 74 63 68 3b  r(j=0; j<nMatch;
2e6eb 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   j++){.      if(
2e6ec 20 61 4d 61 74 63 68 5b 6a 5d 2e 69 54 65 72 6d   aMatch[j].iTerm
2e6ed 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20 20 20 61  ==i ){.        a
2e6ee 4d 61 74 63 68 5b 6a 5d 2e 73 6e 53 74 61 74 75  Match[j].snStatu
2e6ef 73 20 3d 20 53 4e 49 50 50 45 54 5f 44 45 53 49  s = SNIPPET_DESI
2e6f0 52 45 44 3b 0a 20 20 20 20 20 20 20 20 6e 44 65  RED;.        nDe
2e6f1 73 69 72 65 64 2b 2b 3b 0a 20 20 20 20 20 20 20  sired++;.       
2e6f2 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
2e6f3 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 4d 61      }.  }..  iMa
2e6f4 74 63 68 20 3d 20 30 3b 0a 20 20 74 61 69 6c 43  tch = 0;.  tailC
2e6f5 6f 6c 20 3d 20 2d 31 3b 0a 20 20 74 61 69 6c 4f  ol = -1;.  tailO
2e6f6 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 66 6f 72  ffset = 0;.  for
2e6f7 28 69 3d 30 3b 20 69 3c 6e 4d 61 74 63 68 20 26  (i=0; i<nMatch &
2e6f8 26 20 6e 44 65 73 69 72 65 64 3e 30 3b 20 69 2b  & nDesired>0; i+
2e6f9 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 4d 61 74  +){.    if( aMat
2e6fa 63 68 5b 69 5d 2e 73 6e 53 74 61 74 75 73 21 3d  ch[i].snStatus!=
2e6fb 53 4e 49 50 50 45 54 5f 44 45 53 49 52 45 44 20  SNIPPET_DESIRED 
2e6fc 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
2e6fd 6e 44 65 73 69 72 65 64 2d 2d 3b 0a 20 20 20 20  nDesired--;.    
2e6fe 69 43 6f 6c 20 3d 20 61 4d 61 74 63 68 5b 69 5d  iCol = aMatch[i]
2e6ff 2e 69 43 6f 6c 3b 0a 20 20 20 20 7a 44 6f 63 20  .iCol;.    zDoc 
2e700 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73  = (const char*)s
2e701 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
2e702 78 74 28 70 43 75 72 73 6f 72 2d 3e 70 53 74 6d  xt(pCursor->pStm
2e703 74 2c 20 69 43 6f 6c 2b 31 29 3b 0a 20 20 20 20  t, iCol+1);.    
2e704 6e 44 6f 63 20 3d 20 73 71 6c 69 74 65 33 5f 63  nDoc = sqlite3_c
2e705 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 43 75 72  olumn_bytes(pCur
2e706 73 6f 72 2d 3e 70 53 74 6d 74 2c 20 69 43 6f 6c  sor->pStmt, iCol
2e707 2b 31 29 3b 0a 20 20 20 20 69 53 74 61 72 74 20  +1);.    iStart 
2e708 3d 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61  = aMatch[i].iSta
2e709 72 74 20 2d 20 34 30 3b 0a 20 20 20 20 69 53 74  rt - 40;.    iSt
2e70a 61 72 74 20 3d 20 77 6f 72 64 42 6f 75 6e 64 61  art = wordBounda
2e70b 72 79 28 69 53 74 61 72 74 2c 20 7a 44 6f 63 2c  ry(iStart, zDoc,
2e70c 20 6e 44 6f 63 2c 20 61 4d 61 74 63 68 2c 20 6e   nDoc, aMatch, n
2e70d 4d 61 74 63 68 2c 20 69 43 6f 6c 29 3b 0a 20 20  Match, iCol);.  
2e70e 20 20 69 66 28 20 69 53 74 61 72 74 3c 3d 31 30    if( iStart<=10
2e70f 20 29 7b 0a 20 20 20 20 20 20 69 53 74 61 72 74   ){.      iStart
2e710 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
2e711 69 66 28 20 69 43 6f 6c 3d 3d 74 61 69 6c 43 6f  if( iCol==tailCo
2e712 6c 20 26 26 20 69 53 74 61 72 74 3c 3d 74 61 69  l && iStart<=tai
2e713 6c 4f 66 66 73 65 74 2b 32 30 20 29 7b 0a 20 20  lOffset+20 ){.  
2e714 20 20 20 20 69 53 74 61 72 74 20 3d 20 74 61 69      iStart = tai
2e715 6c 4f 66 66 73 65 74 3b 0a 20 20 20 20 7d 0a 20  lOffset;.    }. 
2e716 20 20 20 69 66 28 20 28 69 43 6f 6c 21 3d 74 61     if( (iCol!=ta
2e717 69 6c 43 6f 6c 20 26 26 20 74 61 69 6c 43 6f 6c  ilCol && tailCol
2e718 3e 3d 30 29 20 7c 7c 20 69 53 74 61 72 74 21 3d  >=0) || iStart!=
2e719 74 61 69 6c 4f 66 66 73 65 74 20 29 7b 0a 20 20  tailOffset ){.  
2e71a 20 20 20 20 74 72 69 6d 57 68 69 74 65 53 70 61      trimWhiteSpa
2e71b 63 65 28 26 73 62 29 3b 0a 20 20 20 20 20 20 61  ce(&sb);.      a
2e71c 70 70 65 6e 64 57 68 69 74 65 53 70 61 63 65 28  ppendWhiteSpace(
2e71d 26 73 62 29 3b 0a 20 20 20 20 20 20 61 70 70 65  &sb);.      appe
2e71e 6e 64 28 26 73 62 2c 20 7a 45 6c 6c 69 70 73 69  nd(&sb, zEllipsi
2e71f 73 29 3b 0a 20 20 20 20 20 20 61 70 70 65 6e 64  s);.      append
2e720 57 68 69 74 65 53 70 61 63 65 28 26 73 62 29 3b  WhiteSpace(&sb);
2e721 0a 20 20 20 20 7d 0a 20 20 20 20 69 45 6e 64 20  .    }.    iEnd 
2e722 3d 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61  = aMatch[i].iSta
2e723 72 74 20 2b 20 61 4d 61 74 63 68 5b 69 5d 2e 6e  rt + aMatch[i].n
2e724 42 79 74 65 20 2b 20 34 30 3b 0a 20 20 20 20 69  Byte + 40;.    i
2e725 45 6e 64 20 3d 20 77 6f 72 64 42 6f 75 6e 64 61  End = wordBounda
2e726 72 79 28 69 45 6e 64 2c 20 7a 44 6f 63 2c 20 6e  ry(iEnd, zDoc, n
2e727 44 6f 63 2c 20 61 4d 61 74 63 68 2c 20 6e 4d 61  Doc, aMatch, nMa
2e728 74 63 68 2c 20 69 43 6f 6c 29 3b 0a 20 20 20 20  tch, iCol);.    
2e729 69 66 28 20 69 45 6e 64 3e 3d 6e 44 6f 63 2d 31  if( iEnd>=nDoc-1
2e72a 30 20 29 7b 0a 20 20 20 20 20 20 69 45 6e 64 20  0 ){.      iEnd 
2e72b 3d 20 6e 44 6f 63 3b 0a 20 20 20 20 20 20 74 61  = nDoc;.      ta
2e72c 69 6c 45 6c 6c 69 70 73 69 73 20 3d 20 30 3b 0a  ilEllipsis = 0;.
2e72d 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2e72e 20 74 61 69 6c 45 6c 6c 69 70 73 69 73 20 3d 20   tailEllipsis = 
2e72f 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 68 69  1;.    }.    whi
2e730 6c 65 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74 63  le( iMatch<nMatc
2e731 68 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74  h && aMatch[iMat
2e732 63 68 5d 2e 69 43 6f 6c 3c 69 43 6f 6c 20 29 7b  ch].iCol<iCol ){
2e733 20 69 4d 61 74 63 68 2b 2b 3b 20 7d 0a 20 20 20   iMatch++; }.   
2e734 20 77 68 69 6c 65 28 20 69 53 74 61 72 74 3c 69   while( iStart<i
2e735 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 77 68 69  End ){.      whi
2e736 6c 65 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74 63  le( iMatch<nMatc
2e737 68 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74  h && aMatch[iMat
2e738 63 68 5d 2e 69 53 74 61 72 74 3c 69 53 74 61 72  ch].iStart<iStar
2e739 74 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 26  t.             &
2e73a 26 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d  & aMatch[iMatch]
2e73b 2e 69 43 6f 6c 3c 3d 69 43 6f 6c 20 29 7b 0a 20  .iCol<=iCol ){. 
2e73c 20 20 20 20 20 20 20 69 4d 61 74 63 68 2b 2b 3b         iMatch++;
2e73d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
2e73e 66 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74 63 68  f( iMatch<nMatch
2e73f 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74 63   && aMatch[iMatc
2e740 68 5d 2e 69 53 74 61 72 74 3c 69 45 6e 64 0a 20  h].iStart<iEnd. 
2e741 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 61              && a
2e742 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69 43  Match[iMatch].iC
2e743 6f 6c 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20  ol==iCol ){.    
2e744 20 20 20 20 6e 61 70 70 65 6e 64 28 26 73 62 2c      nappend(&sb,
2e745 20 26 7a 44 6f 63 5b 69 53 74 61 72 74 5d 2c 20   &zDoc[iStart], 
2e746 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69  aMatch[iMatch].i
2e747 53 74 61 72 74 20 2d 20 69 53 74 61 72 74 29 3b  Start - iStart);
2e748 0a 20 20 20 20 20 20 20 20 69 53 74 61 72 74 20  .        iStart 
2e749 3d 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d  = aMatch[iMatch]
2e74a 2e 69 53 74 61 72 74 3b 0a 20 20 20 20 20 20 20  .iStart;.       
2e74b 20 61 70 70 65 6e 64 28 26 73 62 2c 20 7a 53 74   append(&sb, zSt
2e74c 61 72 74 4d 61 72 6b 29 3b 0a 20 20 20 20 20 20  artMark);.      
2e74d 20 20 6e 61 70 70 65 6e 64 28 26 73 62 2c 20 26    nappend(&sb, &
2e74e 7a 44 6f 63 5b 69 53 74 61 72 74 5d 2c 20 61 4d  zDoc[iStart], aM
2e74f 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 6e 42 79  atch[iMatch].nBy
2e750 74 65 29 3b 0a 20 20 20 20 20 20 20 20 61 70 70  te);.        app
2e751 65 6e 64 28 26 73 62 2c 20 7a 45 6e 64 4d 61 72  end(&sb, zEndMar
2e752 6b 29 3b 0a 20 20 20 20 20 20 20 20 69 53 74 61  k);.        iSta
2e753 72 74 20 2b 3d 20 61 4d 61 74 63 68 5b 69 4d 61  rt += aMatch[iMa
2e754 74 63 68 5d 2e 6e 42 79 74 65 3b 0a 20 20 20 20  tch].nByte;.    
2e755 20 20 20 20 66 6f 72 28 6a 3d 69 4d 61 74 63 68      for(j=iMatch
2e756 2b 31 3b 20 6a 3c 6e 4d 61 74 63 68 3b 20 6a 2b  +1; j<nMatch; j+
2e757 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
2e758 28 20 61 4d 61 74 63 68 5b 6a 5d 2e 69 54 65 72  ( aMatch[j].iTer
2e759 6d 3d 3d 61 4d 61 74 63 68 5b 69 4d 61 74 63 68  m==aMatch[iMatch
2e75a 5d 2e 69 54 65 72 6d 0a 20 20 20 20 20 20 20 20  ].iTerm.        
2e75b 20 20 20 20 20 20 26 26 20 61 4d 61 74 63 68 5b        && aMatch[
2e75c 6a 5d 2e 73 6e 53 74 61 74 75 73 3d 3d 53 4e 49  j].snStatus==SNI
2e75d 50 50 45 54 5f 44 45 53 49 52 45 44 20 29 7b 0a  PPET_DESIRED ){.
2e75e 20 20 20 20 20 20 20 20 20 20 20 20 6e 44 65 73              nDes
2e75f 69 72 65 64 2d 2d 3b 0a 20 20 20 20 20 20 20 20  ired--;.        
2e760 20 20 20 20 61 4d 61 74 63 68 5b 6a 5d 2e 73 6e      aMatch[j].sn
2e761 53 74 61 74 75 73 20 3d 20 53 4e 49 50 50 45 54  Status = SNIPPET
2e762 5f 49 47 4e 4f 52 45 3b 0a 20 20 20 20 20 20 20  _IGNORE;.       
2e763 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
2e764 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2e765 20 20 20 20 6e 61 70 70 65 6e 64 28 26 73 62 2c      nappend(&sb,
2e766 20 26 7a 44 6f 63 5b 69 53 74 61 72 74 5d 2c 20   &zDoc[iStart], 
2e767 69 45 6e 64 20 2d 20 69 53 74 61 72 74 29 3b 0a  iEnd - iStart);.
2e768 20 20 20 20 20 20 20 20 69 53 74 61 72 74 20 3d          iStart =
2e769 20 69 45 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20   iEnd;.      }. 
2e76a 20 20 20 7d 0a 20 20 20 20 74 61 69 6c 43 6f 6c     }.    tailCol
2e76b 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 74 61 69   = iCol;.    tai
2e76c 6c 4f 66 66 73 65 74 20 3d 20 69 45 6e 64 3b 0a  lOffset = iEnd;.
2e76d 20 20 7d 0a 20 20 74 72 69 6d 57 68 69 74 65 53    }.  trimWhiteS
2e76e 70 61 63 65 28 26 73 62 29 3b 0a 20 20 69 66 28  pace(&sb);.  if(
2e76f 20 74 61 69 6c 45 6c 6c 69 70 73 69 73 20 29 7b   tailEllipsis ){
2e770 0a 20 20 20 20 61 70 70 65 6e 64 57 68 69 74 65  .    appendWhite
2e771 53 70 61 63 65 28 26 73 62 29 3b 0a 20 20 20 20  Space(&sb);.    
2e772 61 70 70 65 6e 64 28 26 73 62 2c 20 7a 45 6c 6c  append(&sb, zEll
2e773 69 70 73 69 73 29 3b 0a 20 20 7d 0a 20 20 70 43  ipsis);.  }.  pC
2e774 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 7a  ursor->snippet.z
2e775 53 6e 69 70 70 65 74 20 3d 20 73 74 72 69 6e 67  Snippet = string
2e776 42 75 66 66 65 72 44 61 74 61 28 26 73 62 29 3b  BufferData(&sb);
2e777 0a 20 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70  .  pCursor->snip
2e778 70 65 74 2e 6e 53 6e 69 70 70 65 74 20 3d 20 73  pet.nSnippet = s
2e779 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e 67 74  tringBufferLengt
2e77a 68 28 26 73 62 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  h(&sb);.}.../*.*
2e77b 2a 20 43 6c 6f 73 65 20 74 68 65 20 63 75 72 73  * Close the curs
2e77c 6f 72 2e 20 20 46 6f 72 20 61 64 64 69 74 69 6f  or.  For additio
2e77d 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  nal information 
2e77e 73 65 65 20 74 68 65 20 64 6f 63 75 6d 65 6e 74  see the document
2e77f 61 74 69 6f 6e 0a 2a 2a 20 6f 6e 20 74 68 65 20  ation.** on the 
2e780 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 6f 66  xClose method of
2e781 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
2e782 6c 65 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f  le interface..*/
2e783 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c  .static int full
2e784 74 65 78 74 43 6c 6f 73 65 28 73 71 6c 69 74 65  textClose(sqlite
2e785 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70  3_vtab_cursor *p
2e786 43 75 72 73 6f 72 29 7b 0a 20 20 66 75 6c 6c 74  Cursor){.  fullt
2e787 65 78 74 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20  ext_cursor *c = 
2e788 28 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72  (fulltext_cursor
2e789 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 46   *) pCursor;.  F
2e78a 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20 43  TSTRACE(("FTS3 C
2e78b 6c 6f 73 65 20 25 70 5c 6e 22 2c 20 63 29 29 3b  lose %p\n", c));
2e78c 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  .  sqlite3_final
2e78d 69 7a 65 28 63 2d 3e 70 53 74 6d 74 29 3b 0a 20  ize(c->pStmt);. 
2e78e 20 71 75 65 72 79 43 6c 65 61 72 28 26 63 2d 3e   queryClear(&c->
2e78f 71 29 3b 0a 20 20 73 6e 69 70 70 65 74 43 6c 65  q);.  snippetCle
2e790 61 72 28 26 63 2d 3e 73 6e 69 70 70 65 74 29 3b  ar(&c->snippet);
2e791 0a 20 20 69 66 28 20 63 2d 3e 72 65 73 75 6c 74  .  if( c->result
2e792 2e 6e 44 61 74 61 21 3d 30 20 29 20 64 6c 72 44  .nData!=0 ) dlrD
2e793 65 73 74 72 6f 79 28 26 63 2d 3e 72 65 61 64 65  estroy(&c->reade
2e794 72 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72  r);.  dataBuffer
2e795 44 65 73 74 72 6f 79 28 26 63 2d 3e 72 65 73 75  Destroy(&c->resu
2e796 6c 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  lt);.  sqlite3_f
2e797 72 65 65 28 63 29 3b 0a 20 20 72 65 74 75 72 6e  ree(c);.  return
2e798 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73   SQLITE_OK;.}..s
2e799 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65  tatic int fullte
2e79a 78 74 4e 65 78 74 28 73 71 6c 69 74 65 33 5f 76  xtNext(sqlite3_v
2e79b 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  tab_cursor *pCur
2e79c 73 6f 72 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74  sor){.  fulltext
2e79d 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 66 75  _cursor *c = (fu
2e79e 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 29  lltext_cursor *)
2e79f 20 70 43 75 72 73 6f 72 3b 0a 20 20 69 6e 74 20   pCursor;.  int 
2e7a0 72 63 3b 0a 0a 20 20 46 54 53 54 52 41 43 45 28  rc;..  FTSTRACE(
2e7a1 28 22 46 54 53 33 20 4e 65 78 74 20 25 70 5c 6e  ("FTS3 Next %p\n
2e7a2 22 2c 20 70 43 75 72 73 6f 72 29 29 3b 0a 20 20  ", pCursor));.  
2e7a3 73 6e 69 70 70 65 74 43 6c 65 61 72 28 26 63 2d  snippetClear(&c-
2e7a4 3e 73 6e 69 70 70 65 74 29 3b 0a 20 20 69 66 28  >snippet);.  if(
2e7a5 20 63 2d 3e 69 43 75 72 73 6f 72 54 79 70 65 20   c->iCursorType 
2e7a6 3c 20 51 55 45 52 59 5f 46 55 4c 4c 54 45 58 54  < QUERY_FULLTEXT
2e7a7 20 29 7b 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 28   ){.    /* TODO(
2e7a8 73 68 65 73 73 29 20 48 61 6e 64 6c 65 20 53 51  shess) Handle SQ
2e7a9 4c 49 54 45 5f 53 43 48 45 4d 41 20 41 4e 44 20  LITE_SCHEMA AND 
2e7aa 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 2a 2f 0a  SQLITE_BUSY. */.
2e7ab 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
2e7ac 5f 73 74 65 70 28 63 2d 3e 70 53 74 6d 74 29 3b  _step(c->pStmt);
2e7ad 0a 20 20 20 20 73 77 69 74 63 68 28 20 72 63 20  .    switch( rc 
2e7ae 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 53 51  ){.      case SQ
2e7af 4c 49 54 45 5f 52 4f 57 3a 0a 20 20 20 20 20 20  LITE_ROW:.      
2e7b0 20 20 63 2d 3e 65 6f 66 20 3d 20 30 3b 0a 20 20    c->eof = 0;.  
2e7b1 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
2e7b2 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 63 61  ITE_OK;.      ca
2e7b3 73 65 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3a 0a  se SQLITE_DONE:.
2e7b4 20 20 20 20 20 20 20 20 63 2d 3e 65 6f 66 20 3d          c->eof =
2e7b5 20 31 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75   1;.        retu
2e7b6 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
2e7b7 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20      default:.   
2e7b8 20 20 20 20 20 63 2d 3e 65 6f 66 20 3d 20 31 3b       c->eof = 1;
2e7b9 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
2e7ba 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c  rc;.    }.  } el
2e7bb 73 65 20 7b 20 20 2f 2a 20 66 75 6c 6c 2d 74 65  se {  /* full-te
2e7bc 78 74 20 71 75 65 72 79 20 2a 2f 0a 20 20 20 20  xt query */.    
2e7bd 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73  rc = sqlite3_res
2e7be 65 74 28 63 2d 3e 70 53 74 6d 74 29 3b 0a 20 20  et(c->pStmt);.  
2e7bf 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2e7c0 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
2e7c1 0a 0a 20 20 20 20 69 66 28 20 63 2d 3e 72 65 73  ..    if( c->res
2e7c2 75 6c 74 2e 6e 44 61 74 61 3d 3d 30 20 7c 7c 20  ult.nData==0 || 
2e7c3 64 6c 72 41 74 45 6e 64 28 26 63 2d 3e 72 65 61  dlrAtEnd(&c->rea
2e7c4 64 65 72 29 20 29 7b 0a 20 20 20 20 20 20 63 2d  der) ){.      c-
2e7c5 3e 65 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20  >eof = 1;.      
2e7c6 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
2e7c7 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
2e7c8 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
2e7c9 74 36 34 28 63 2d 3e 70 53 74 6d 74 2c 20 31 2c  t64(c->pStmt, 1,
2e7ca 20 64 6c 72 44 6f 63 69 64 28 26 63 2d 3e 72 65   dlrDocid(&c->re
2e7cb 61 64 65 72 29 29 3b 0a 20 20 20 20 64 6c 72 53  ader));.    dlrS
2e7cc 74 65 70 28 26 63 2d 3e 72 65 61 64 65 72 29 3b  tep(&c->reader);
2e7cd 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
2e7ce 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
2e7cf 72 63 3b 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 28  rc;.    /* TODO(
2e7d0 73 68 65 73 73 29 20 48 61 6e 64 6c 65 20 53 51  shess) Handle SQ
2e7d1 4c 49 54 45 5f 53 43 48 45 4d 41 20 41 4e 44 20  LITE_SCHEMA AND 
2e7d2 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 2a 2f 0a  SQLITE_BUSY. */.
2e7d3 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
2e7d4 5f 73 74 65 70 28 63 2d 3e 70 53 74 6d 74 29 3b  _step(c->pStmt);
2e7d5 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
2e7d6 49 54 45 5f 52 4f 57 20 29 7b 20 20 20 2f 2a 20  ITE_ROW ){   /* 
2e7d7 74 68 65 20 63 61 73 65 20 77 65 20 65 78 70 65  the case we expe
2e7d8 63 74 20 2a 2f 0a 20 20 20 20 20 20 63 2d 3e 65  ct */.      c->e
2e7d9 6f 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65  of = 0;.      re
2e7da 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
2e7db 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 61 6e 20      }.    /* an 
2e7dc 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 3b 20  error occurred; 
2e7dd 61 62 6f 72 74 20 2a 2f 0a 20 20 20 20 72 65 74  abort */.    ret
2e7de 75 72 6e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  urn rc==SQLITE_D
2e7df 4f 4e 45 20 3f 20 53 51 4c 49 54 45 5f 45 52 52  ONE ? SQLITE_ERR
2e7e0 4f 52 20 3a 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a  OR : rc;.  }.}..
2e7e1 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
2e7e2 49 66 20 77 65 20 70 75 73 68 65 64 20 4c 65 61  If we pushed Lea
2e7e3 66 52 65 61 64 65 72 20 74 6f 20 74 68 65 20 74  fReader to the t
2e7e4 6f 70 20 6f 66 20 74 68 65 20 66 69 6c 65 2c 20  op of the file, 
2e7e5 6f 72 20 74 6f 0a 2a 2a 20 61 6e 6f 74 68 65 72  or to.** another
2e7e6 20 66 69 6c 65 2c 20 74 65 72 6d 5f 73 65 6c 65   file, term_sele
2e7e7 63 74 28 29 20 63 6f 75 6c 64 20 62 65 20 70 75  ct() could be pu
2e7e8 73 68 65 64 20 61 62 6f 76 65 0a 2a 2a 20 64 6f  shed above.** do
2e7e9 63 4c 69 73 74 4f 66 54 65 72 6d 28 29 2e 0a 2a  cListOfTerm()..*
2e7ea 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 74 65 72  /.static int ter
2e7eb 6d 53 65 6c 65 63 74 28 66 75 6c 6c 74 65 78 74  mSelect(fulltext
2e7ec 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 43  _vtab *v, int iC
2e7ed 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20 20 20 20 20  olumn,.         
2e7ee 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
2e7ef 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20  st char *pTerm, 
2e7f0 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69  int nTerm, int i
2e7f1 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20  sPrefix,.       
2e7f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 44                 D
2e7f3 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79 70 65  ocListType iType
2e7f4 2c 20 44 61 74 61 42 75 66 66 65 72 20 2a 6f 75  , DataBuffer *ou
2e7f5 74 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 61  t);../* Return a
2e7f6 20 44 6f 63 4c 69 73 74 20 63 6f 72 72 65 73 70   DocList corresp
2e7f7 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 71 75  onding to the qu
2e7f8 65 72 79 20 74 65 72 6d 20 2a 70 54 65 72 6d 2e  ery term *pTerm.
2e7f9 20 20 49 66 20 2a 70 54 65 72 6d 0a 2a 2a 20 69    If *pTerm.** i
2e7fa 73 20 74 68 65 20 66 69 72 73 74 20 74 65 72 6d  s the first term
2e7fb 20 6f 66 20 61 20 70 68 72 61 73 65 20 71 75 65   of a phrase que
2e7fc 72 79 2c 20 67 6f 20 61 68 65 61 64 20 61 6e 64  ry, go ahead and
2e7fd 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 70 68   evaluate the ph
2e7fe 72 61 73 65 0a 2a 2a 20 71 75 65 72 79 20 61 6e  rase.** query an
2e7ff 64 20 72 65 74 75 72 6e 20 74 68 65 20 64 6f 63  d return the doc
2e800 6c 69 73 74 20 66 6f 72 20 74 68 65 20 65 6e 74  list for the ent
2e801 69 72 65 20 70 68 72 61 73 65 20 71 75 65 72 79  ire phrase query
2e802 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75  ..**.** The resu
2e803 6c 74 69 6e 67 20 44 4c 5f 44 4f 43 49 44 53 20  lting DL_DOCIDS 
2e804 64 6f 63 6c 69 73 74 20 69 73 20 73 74 6f 72 65  doclist is store
2e805 64 20 69 6e 20 70 52 65 73 75 6c 74 2c 20 77 68  d in pResult, wh
2e806 69 63 68 20 69 73 0a 2a 2a 20 6f 76 65 72 77 72  ich is.** overwr
2e807 69 74 74 65 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  itten..*/.static
2e808 20 69 6e 74 20 64 6f 63 4c 69 73 74 4f 66 54 65   int docListOfTe
2e809 72 6d 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76  rm(.  fulltext_v
2e80a 74 61 62 20 2a 76 2c 20 20 20 20 2f 2a 20 54 68  tab *v,    /* Th
2e80b 65 20 66 75 6c 6c 20 74 65 78 74 20 69 6e 64 65  e full text inde
2e80c 78 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75  x */.  int iColu
2e80d 6d 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 63  mn,         /* c
2e80e 6f 6c 75 6d 6e 20 74 6f 20 72 65 73 74 72 69 63  olumn to restric
2e80f 74 20 74 6f 2e 20 20 4e 6f 20 72 65 73 74 72 69  t to.  No restri
2e810 63 74 69 6f 6e 20 69 66 20 3e 3d 6e 43 6f 6c 75  ction if >=nColu
2e811 6d 6e 20 2a 2f 0a 20 20 51 75 65 72 79 54 65 72  mn */.  QueryTer
2e812 6d 20 2a 70 51 54 65 72 6d 2c 20 20 20 2f 2a 20  m *pQTerm,   /* 
2e813 54 65 72 6d 20 77 65 20 61 72 65 20 6c 6f 6f 6b  Term we are look
2e814 69 6e 67 20 66 6f 72 2c 20 6f 72 20 31 73 74 20  ing for, or 1st 
2e815 74 65 72 6d 20 6f 66 20 61 20 70 68 72 61 73 65  term of a phrase
2e816 20 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72   */.  DataBuffer
2e817 20 2a 70 52 65 73 75 6c 74 20 20 2f 2a 20 57 72   *pResult  /* Wr
2e818 69 74 65 20 74 68 65 20 72 65 73 75 6c 74 20 68  ite the result h
2e819 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 44 61 74 61  ere */.){.  Data
2e81a 42 75 66 66 65 72 20 6c 65 66 74 2c 20 72 69 67  Buffer left, rig
2e81b 68 74 2c 20 6e 65 77 3b 0a 20 20 69 6e 74 20 69  ht, new;.  int i
2e81c 2c 20 72 63 3b 0a 0a 20 20 2f 2a 20 4e 6f 20 70  , rc;..  /* No p
2e81d 68 72 61 73 65 20 73 65 61 72 63 68 20 69 66 20  hrase search if 
2e81e 6e 6f 20 70 6f 73 69 74 69 6f 6e 20 69 6e 66 6f  no position info
2e81f 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  . */.  assert( p
2e820 51 54 65 72 6d 2d 3e 6e 50 68 72 61 73 65 3d 3d  QTerm->nPhrase==
2e821 30 20 7c 7c 20 44 4c 5f 44 45 46 41 55 4c 54 21  0 || DL_DEFAULT!
2e822 3d 44 4c 5f 44 4f 43 49 44 53 20 29 3b 0a 0a 20  =DL_DOCIDS );.. 
2e823 20 2f 2a 20 54 68 69 73 20 63 6f 64 65 20 73 68   /* This code sh
2e824 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20 63 61  ould never be ca
2e825 6c 6c 65 64 20 77 69 74 68 20 62 75 66 66 65 72  lled with buffer
2e826 65 64 20 75 70 64 61 74 65 73 2e 20 2a 2f 0a 20  ed updates. */. 
2e827 20 61 73 73 65 72 74 28 20 76 2d 3e 6e 50 65 6e   assert( v->nPen
2e828 64 69 6e 67 44 61 74 61 3c 30 20 29 3b 0a 0a 20  dingData<0 );.. 
2e829 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28   dataBufferInit(
2e82a 26 6c 65 66 74 2c 20 30 29 3b 0a 20 20 72 63 20  &left, 0);.  rc 
2e82b 3d 20 74 65 72 6d 53 65 6c 65 63 74 28 76 2c 20  = termSelect(v, 
2e82c 69 43 6f 6c 75 6d 6e 2c 20 70 51 54 65 72 6d 2d  iColumn, pQTerm-
2e82d 3e 70 54 65 72 6d 2c 20 70 51 54 65 72 6d 2d 3e  >pTerm, pQTerm->
2e82e 6e 54 65 72 6d 2c 20 70 51 54 65 72 6d 2d 3e 69  nTerm, pQTerm->i
2e82f 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20  sPrefix,.       
2e830 20 20 20 20 20 20 20 20 20 20 20 28 30 3c 70 51             (0<pQ
2e831 54 65 72 6d 2d 3e 6e 50 68 72 61 73 65 20 3f 20  Term->nPhrase ? 
2e832 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 3a 20 44  DL_POSITIONS : D
2e833 4c 5f 44 4f 43 49 44 53 29 2c 20 26 6c 65 66 74  L_DOCIDS), &left
2e834 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  );.  if( rc ) re
2e835 74 75 72 6e 20 72 63 3b 0a 20 20 66 6f 72 28 69  turn rc;.  for(i
2e836 3d 31 3b 20 69 3c 3d 70 51 54 65 72 6d 2d 3e 6e  =1; i<=pQTerm->n
2e837 50 68 72 61 73 65 20 26 26 20 6c 65 66 74 2e 6e  Phrase && left.n
2e838 44 61 74 61 3e 30 3b 20 69 2b 2b 29 7b 0a 20 20  Data>0; i++){.  
2e839 20 20 2f 2a 20 49 66 20 74 68 69 73 20 74 6f 6b    /* If this tok
2e83a 65 6e 20 69 73 20 63 6f 6e 6e 65 63 74 65 64 20  en is connected 
2e83b 74 6f 20 74 68 65 20 6e 65 78 74 20 62 79 20 61  to the next by a
2e83c 20 4e 45 41 52 20 6f 70 65 72 61 74 6f 72 2c 20   NEAR operator, 
2e83d 61 6e 64 0a 20 20 20 20 2a 2a 20 74 68 65 20 6e  and.    ** the n
2e83e 65 78 74 20 74 6f 6b 65 6e 20 69 73 20 74 68 65  ext token is the
2e83f 20 73 74 61 72 74 20 6f 66 20 61 20 70 68 72 61   start of a phra
2e840 73 65 2c 20 74 68 65 6e 20 73 65 74 20 6e 50 68  se, then set nPh
2e841 72 61 73 65 52 69 67 68 74 0a 20 20 20 20 2a 2a  raseRight.    **
2e842 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   to the number o
2e843 66 20 74 6f 6b 65 6e 73 20 69 6e 20 74 68 65 20  f tokens in the 
2e844 70 68 72 61 73 65 2e 20 4f 74 68 65 72 77 69 73  phrase. Otherwis
2e845 65 20 6c 65 61 76 65 20 69 74 20 61 74 20 31 2e  e leave it at 1.
2e846 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20  .    */.    int 
2e847 6e 50 68 72 61 73 65 52 69 67 68 74 20 3d 20 31  nPhraseRight = 1
2e848 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 69 2b  ;.    while( (i+
2e849 6e 50 68 72 61 73 65 52 69 67 68 74 29 3c 3d 70  nPhraseRight)<=p
2e84a 51 54 65 72 6d 2d 3e 6e 50 68 72 61 73 65 20 0a  QTerm->nPhrase .
2e84b 20 20 20 20 20 20 20 20 26 26 20 70 51 54 65 72          && pQTer
2e84c 6d 5b 69 2b 6e 50 68 72 61 73 65 52 69 67 68 74  m[i+nPhraseRight
2e84d 5d 2e 6e 4e 65 61 72 3d 3d 30 20 0a 20 20 20 20  ].nNear==0 .    
2e84e 29 7b 0a 20 20 20 20 20 20 6e 50 68 72 61 73 65  ){.      nPhrase
2e84f 52 69 67 68 74 2b 2b 3b 0a 20 20 20 20 7d 0a 0a  Right++;.    }..
2e850 20 20 20 20 64 61 74 61 42 75 66 66 65 72 49 6e      dataBufferIn
2e851 69 74 28 26 72 69 67 68 74 2c 20 30 29 3b 0a 20  it(&right, 0);. 
2e852 20 20 20 72 63 20 3d 20 74 65 72 6d 53 65 6c 65     rc = termSele
2e853 63 74 28 76 2c 20 69 43 6f 6c 75 6d 6e 2c 20 70  ct(v, iColumn, p
2e854 51 54 65 72 6d 5b 69 5d 2e 70 54 65 72 6d 2c 20  QTerm[i].pTerm, 
2e855 70 51 54 65 72 6d 5b 69 5d 2e 6e 54 65 72 6d 2c  pQTerm[i].nTerm,
2e856 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2e857 20 20 20 20 20 70 51 54 65 72 6d 5b 69 5d 2e 69       pQTerm[i].i
2e858 73 50 72 65 66 69 78 2c 20 44 4c 5f 50 4f 53 49  sPrefix, DL_POSI
2e859 54 49 4f 4e 53 2c 20 26 72 69 67 68 74 29 3b 0a  TIONS, &right);.
2e85a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
2e85b 20 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65      dataBufferDe
2e85c 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20  stroy(&left);.  
2e85d 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
2e85e 20 20 20 7d 0a 20 20 20 20 64 61 74 61 42 75 66     }.    dataBuf
2e85f 66 65 72 49 6e 69 74 28 26 6e 65 77 2c 20 30 29  ferInit(&new, 0)
2e860 3b 0a 20 20 20 20 64 6f 63 4c 69 73 74 50 68 72  ;.    docListPhr
2e861 61 73 65 4d 65 72 67 65 28 6c 65 66 74 2e 70 44  aseMerge(left.pD
2e862 61 74 61 2c 20 6c 65 66 74 2e 6e 44 61 74 61 2c  ata, left.nData,
2e863 20 72 69 67 68 74 2e 70 44 61 74 61 2c 20 72 69   right.pData, ri
2e864 67 68 74 2e 6e 44 61 74 61 2c 0a 20 20 20 20 20  ght.nData,.     
2e865 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e866 20 20 70 51 54 65 72 6d 5b 69 2d 31 5d 2e 6e 4e    pQTerm[i-1].nN
2e867 65 61 72 2c 20 70 51 54 65 72 6d 5b 69 2d 31 5d  ear, pQTerm[i-1]
2e868 2e 69 50 68 72 61 73 65 20 2b 20 6e 50 68 72 61  .iPhrase + nPhra
2e869 73 65 52 69 67 68 74 2c 0a 20 20 20 20 20 20 20  seRight,.       
2e86a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e86b 28 28 69 3c 70 51 54 65 72 6d 2d 3e 6e 50 68 72  ((i<pQTerm->nPhr
2e86c 61 73 65 29 20 3f 20 44 4c 5f 50 4f 53 49 54 49  ase) ? DL_POSITI
2e86d 4f 4e 53 20 3a 20 44 4c 5f 44 4f 43 49 44 53 29  ONS : DL_DOCIDS)
2e86e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2e86f 20 20 20 20 20 20 20 20 20 26 6e 65 77 29 3b 0a           &new);.
2e870 20 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65      dataBufferDe
2e871 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20  stroy(&left);.  
2e872 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74    dataBufferDest
2e873 72 6f 79 28 26 72 69 67 68 74 29 3b 0a 20 20 20  roy(&right);.   
2e874 20 6c 65 66 74 20 3d 20 6e 65 77 3b 0a 20 20 7d   left = new;.  }
2e875 0a 20 20 2a 70 52 65 73 75 6c 74 20 3d 20 6c 65  .  *pResult = le
2e876 66 74 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  ft;.  return SQL
2e877 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 41 64  ITE_OK;.}../* Ad
2e878 64 20 61 20 6e 65 77 20 74 65 72 6d 20 70 54 65  d a new term pTe
2e879 72 6d 5b 30 2e 2e 6e 54 65 72 6d 2d 31 5d 20 74  rm[0..nTerm-1] t
2e87a 6f 20 74 68 65 20 71 75 65 72 79 20 2a 71 2e 0a  o the query *q..
2e87b 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 71  */.static void q
2e87c 75 65 72 79 41 64 64 28 51 75 65 72 79 20 2a 71  ueryAdd(Query *q
2e87d 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54  , const char *pT
2e87e 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 29 7b  erm, int nTerm){
2e87f 0a 20 20 51 75 65 72 79 54 65 72 6d 20 2a 74 3b  .  QueryTerm *t;
2e880 0a 20 20 2b 2b 71 2d 3e 6e 54 65 72 6d 73 3b 0a  .  ++q->nTerms;.
2e881 20 20 71 2d 3e 70 54 65 72 6d 73 20 3d 20 73 71    q->pTerms = sq
2e882 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 71 2d  lite3_realloc(q-
2e883 3e 70 54 65 72 6d 73 2c 20 71 2d 3e 6e 54 65 72  >pTerms, q->nTer
2e884 6d 73 20 2a 20 73 69 7a 65 6f 66 28 71 2d 3e 70  ms * sizeof(q->p
2e885 54 65 72 6d 73 5b 30 5d 29 29 3b 0a 20 20 69 66  Terms[0]));.  if
2e886 28 20 71 2d 3e 70 54 65 72 6d 73 3d 3d 30 20 29  ( q->pTerms==0 )
2e887 7b 0a 20 20 20 20 71 2d 3e 6e 54 65 72 6d 73 20  {.    q->nTerms 
2e888 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  = 0;.    return;
2e889 0a 20 20 7d 0a 20 20 74 20 3d 20 26 71 2d 3e 70  .  }.  t = &q->p
2e88a 54 65 72 6d 73 5b 71 2d 3e 6e 54 65 72 6d 73 20  Terms[q->nTerms 
2e88b 2d 20 31 5d 3b 0a 20 20 43 4c 45 41 52 28 74 29  - 1];.  CLEAR(t)
2e88c 3b 0a 20 20 74 2d 3e 70 54 65 72 6d 20 3d 20 73  ;.  t->pTerm = s
2e88d 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 54  qlite3_malloc(nT
2e88e 65 72 6d 2b 31 29 3b 0a 20 20 6d 65 6d 63 70 79  erm+1);.  memcpy
2e88f 28 74 2d 3e 70 54 65 72 6d 2c 20 70 54 65 72 6d  (t->pTerm, pTerm
2e890 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 74 2d 3e 70  , nTerm);.  t->p
2e891 54 65 72 6d 5b 6e 54 65 72 6d 5d 20 3d 20 30 3b  Term[nTerm] = 0;
2e892 0a 20 20 74 2d 3e 6e 54 65 72 6d 20 3d 20 6e 54  .  t->nTerm = nT
2e893 65 72 6d 3b 0a 20 20 74 2d 3e 69 73 4f 72 20 3d  erm;.  t->isOr =
2e894 20 71 2d 3e 6e 65 78 74 49 73 4f 72 3b 0a 20 20   q->nextIsOr;.  
2e895 74 2d 3e 69 73 50 72 65 66 69 78 20 3d 20 30 3b  t->isPrefix = 0;
2e896 0a 20 20 71 2d 3e 6e 65 78 74 49 73 4f 72 20 3d  .  q->nextIsOr =
2e897 20 30 3b 0a 20 20 74 2d 3e 69 43 6f 6c 75 6d 6e   0;.  t->iColumn
2e898 20 3d 20 71 2d 3e 6e 65 78 74 43 6f 6c 75 6d 6e   = q->nextColumn
2e899 3b 0a 20 20 71 2d 3e 6e 65 78 74 43 6f 6c 75 6d  ;.  q->nextColum
2e89a 6e 20 3d 20 71 2d 3e 64 66 6c 74 43 6f 6c 75 6d  n = q->dfltColum
2e89b 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  n;.}../*.** Chec
2e89c 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20  k to see if the 
2e89d 73 74 72 69 6e 67 20 7a 54 6f 6b 65 6e 5b 30 2e  string zToken[0.
2e89e 2e 2e 6e 54 6f 6b 65 6e 2d 31 5d 20 6d 61 74 63  ..nToken-1] matc
2e89f 68 65 73 20 61 6e 79 0a 2a 2a 20 63 6f 6c 75 6d  hes any.** colum
2e8a0 6e 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 76 69  n name in the vi
2e8a1 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 20 49  rtual table.   I
2e8a2 66 20 69 74 20 64 6f 65 73 2c 0a 2a 2a 20 72 65  f it does,.** re
2e8a3 74 75 72 6e 20 74 68 65 20 7a 65 72 6f 2d 69 6e  turn the zero-in
2e8a4 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20 6e 75 6d  dexed column num
2e8a5 62 65 72 2e 20 20 49 66 20 6e 6f 74 2c 20 72 65  ber.  If not, re
2e8a6 74 75 72 6e 20 2d 31 2e 0a 2a 2f 0a 73 74 61 74  turn -1..*/.stat
2e8a7 69 63 20 69 6e 74 20 63 68 65 63 6b 43 6f 6c 75  ic int checkColu
2e8a8 6d 6e 53 70 65 63 69 66 69 65 72 28 0a 20 20 66  mnSpecifier(.  f
2e8a9 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 70 56  ulltext_vtab *pV
2e8aa 74 61 62 2c 20 20 20 20 2f 2a 20 54 68 65 20 76  tab,    /* The v
2e8ab 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a  irtual table */.
2e8ac 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
2e8ad 6f 6b 65 6e 2c 20 20 20 20 20 20 2f 2a 20 54 65  oken,      /* Te
2e8ae 78 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20  xt of the token 
2e8af 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 20  */.  int nToken 
2e8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2e8b1 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61   Number of chara
2e8b2 63 74 65 72 73 20 69 6e 20 74 68 65 20 74 6f 6b  cters in the tok
2e8b3 65 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  en */.){.  int i
2e8b4 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
2e8b5 56 74 61 62 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69  Vtab->nColumn; i
2e8b6 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d  ++){.    if( mem
2e8b7 63 6d 70 28 70 56 74 61 62 2d 3e 61 7a 43 6f 6c  cmp(pVtab->azCol
2e8b8 75 6d 6e 5b 69 5d 2c 20 7a 54 6f 6b 65 6e 2c 20  umn[i], zToken, 
2e8b9 6e 54 6f 6b 65 6e 29 3d 3d 30 0a 20 20 20 20 20  nToken)==0.     
2e8ba 20 20 20 26 26 20 70 56 74 61 62 2d 3e 61 7a 43     && pVtab->azC
2e8bb 6f 6c 75 6d 6e 5b 69 5d 5b 6e 54 6f 6b 65 6e 5d  olumn[i][nToken]
2e8bc 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ==0 ){.      ret
2e8bd 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn i;.    }.  }
2e8be 0a 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 7d 0a  .  return -1;.}.
2e8bf 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 74 68 65  ./*.** Parse the
2e8c0 20 74 65 78 74 20 61 74 20 7a 53 65 67 6d 65 6e   text at zSegmen
2e8c1 74 5b 30 2e 2e 6e 53 65 67 6d 65 6e 74 2d 31 5d  t[0..nSegment-1]
2e8c2 2e 20 20 41 64 64 20 61 64 64 69 74 69 6f 6e 61  .  Add additiona
2e8c3 6c 20 74 65 72 6d 73 0a 2a 2a 20 74 6f 20 74 68  l terms.** to th
2e8c4 65 20 71 75 65 72 79 20 62 65 69 6e 67 20 61 73  e query being as
2e8c5 73 65 6d 62 6c 69 65 64 20 69 6e 20 70 51 75 65  semblied in pQue
2e8c6 72 79 2e 0a 2a 2a 0a 2a 2a 20 69 6e 50 68 72 61  ry..**.** inPhra
2e8c7 73 65 20 69 73 20 74 72 75 65 20 69 66 20 7a 53  se is true if zS
2e8c8 65 67 6d 65 6e 74 5b 30 2e 2e 6e 53 65 67 65 6d  egment[0..nSegem
2e8c9 65 6e 74 2d 31 5d 20 69 73 20 63 6f 6e 74 61 69  ent-1] is contai
2e8ca 6e 65 64 20 77 69 74 68 69 6e 0a 2a 2a 20 64 6f  ned within.** do
2e8cb 75 62 6c 65 2d 71 75 6f 74 65 73 2e 20 20 49 66  uble-quotes.  If
2e8cc 20 69 6e 50 68 72 61 73 65 20 69 73 20 74 72 75   inPhrase is tru
2e8cd 65 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72 73  e, then the firs
2e8ce 74 20 74 65 72 6d 0a 2a 2a 20 69 73 20 6d 61 72  t term.** is mar
2e8cf 6b 65 64 20 77 69 74 68 20 74 68 65 20 6e 75 6d  ked with the num
2e8d0 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20  ber of terms in 
2e8d1 74 68 65 20 70 68 72 61 73 65 20 6c 65 73 73 20  the phrase less 
2e8d2 6f 6e 65 20 61 6e 64 0a 2a 2a 20 4f 52 20 61 6e  one and.** OR an
2e8d3 64 20 22 2d 22 20 73 79 6e 74 61 78 20 69 73 20  d "-" syntax is 
2e8d4 69 67 6e 6f 72 65 64 2e 20 20 49 66 20 69 6e 50  ignored.  If inP
2e8d5 68 72 61 73 65 20 69 73 20 66 61 6c 73 65 2c 20  hrase is false, 
2e8d6 74 68 65 6e 20 65 76 65 72 79 0a 2a 2a 20 74 65  then every.** te
2e8d7 72 6d 20 66 6f 75 6e 64 20 69 73 20 6d 61 72 6b  rm found is mark
2e8d8 65 64 20 77 69 74 68 20 6e 50 68 72 61 73 65 3d  ed with nPhrase=
2e8d9 30 20 61 6e 64 20 4f 52 20 61 6e 64 20 22 2d 22  0 and OR and "-"
2e8da 20 73 79 6e 74 61 78 20 69 73 20 73 69 67 6e 69   syntax is signi
2e8db 66 69 63 61 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69  ficant..*/.stati
2e8dc 63 20 69 6e 74 20 74 6f 6b 65 6e 69 7a 65 53 65  c int tokenizeSe
2e8dd 67 6d 65 6e 74 28 0a 20 20 73 71 6c 69 74 65 33  gment(.  sqlite3
2e8de 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b  _tokenizer *pTok
2e8df 65 6e 69 7a 65 72 2c 20 20 20 20 20 20 20 20 20  enizer,         
2e8e0 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 69 7a 65   /* The tokenize
2e8e1 72 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 63 6f  r to use */.  co
2e8e2 6e 73 74 20 63 68 61 72 20 2a 7a 53 65 67 6d 65  nst char *zSegme
2e8e3 6e 74 2c 20 69 6e 74 20 6e 53 65 67 6d 65 6e 74  nt, int nSegment
2e8e4 2c 20 20 20 20 20 2f 2a 20 51 75 65 72 79 20 65  ,     /* Query e
2e8e5 78 70 72 65 73 73 69 6f 6e 20 62 65 69 6e 67 20  xpression being 
2e8e6 70 61 72 73 65 64 20 2a 2f 0a 20 20 69 6e 74 20  parsed */.  int 
2e8e7 69 6e 50 68 72 61 73 65 2c 20 20 20 20 20 20 20  inPhrase,       
2e8e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e8e9 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77      /* True if w
2e8ea 69 74 68 69 6e 20 22 2e 2e 2e 22 20 2a 2f 0a 20  ithin "..." */. 
2e8eb 20 51 75 65 72 79 20 2a 70 51 75 65 72 79 20 20   Query *pQuery  
2e8ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e8ed 20 20 20 20 20 20 20 20 20 2f 2a 20 41 70 70 65           /* Appe
2e8ee 6e 64 20 72 65 73 75 6c 74 73 20 68 65 72 65 20  nd results here 
2e8ef 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 73 71  */.){.  const sq
2e8f0 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
2e8f1 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 20  module *pModule 
2e8f2 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d  = pTokenizer->pM
2e8f3 6f 64 75 6c 65 3b 0a 20 20 73 71 6c 69 74 65 33  odule;.  sqlite3
2e8f4 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
2e8f5 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20 69 6e  r *pCursor;.  in
2e8f6 74 20 66 69 72 73 74 49 6e 64 65 78 20 3d 20 70  t firstIndex = p
2e8f7 51 75 65 72 79 2d 3e 6e 54 65 72 6d 73 3b 0a 20  Query->nTerms;. 
2e8f8 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 69 6e 74   int iCol;.  int
2e8f9 20 6e 54 65 72 6d 20 3d 20 31 3b 0a 20 20 0a 20   nTerm = 1;.  . 
2e8fa 20 69 6e 74 20 72 63 20 3d 20 70 4d 6f 64 75 6c   int rc = pModul
2e8fb 65 2d 3e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69  e->xOpen(pTokeni
2e8fc 7a 65 72 2c 20 7a 53 65 67 6d 65 6e 74 2c 20 6e  zer, zSegment, n
2e8fd 53 65 67 6d 65 6e 74 2c 20 26 70 43 75 72 73 6f  Segment, &pCurso
2e8fe 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  r);.  if( rc!=SQ
2e8ff 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
2e900 20 72 63 3b 0a 20 20 70 43 75 72 73 6f 72 2d 3e   rc;.  pCursor->
2e901 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f  pTokenizer = pTo
2e902 6b 65 6e 69 7a 65 72 3b 0a 0a 20 20 77 68 69 6c  kenizer;..  whil
2e903 65 28 20 31 20 29 7b 0a 20 20 20 20 63 6f 6e 73  e( 1 ){.    cons
2e904 74 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 0a  t char *zToken;.
2e905 20 20 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 2c 20      int nToken, 
2e906 69 42 65 67 69 6e 2c 20 69 45 6e 64 2c 20 69 50  iBegin, iEnd, iP
2e907 6f 73 3b 0a 0a 20 20 20 20 72 63 20 3d 20 70 4d  os;..    rc = pM
2e908 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43 75  odule->xNext(pCu
2e909 72 73 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20  rsor,.          
2e90a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 7a                &z
2e90b 54 6f 6b 65 6e 2c 20 26 6e 54 6f 6b 65 6e 2c 0a  Token, &nToken,.
2e90c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e90d 20 20 20 20 20 20 20 20 26 69 42 65 67 69 6e 2c          &iBegin,
2e90e 20 26 69 45 6e 64 2c 20 26 69 50 6f 73 29 3b 0a   &iEnd, &iPos);.
2e90f 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
2e910 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b 0a 20  TE_OK ) break;. 
2e911 20 20 20 69 66 28 20 21 69 6e 50 68 72 61 73 65     if( !inPhrase
2e912 20 26 26 0a 20 20 20 20 20 20 20 20 7a 53 65 67   &&.        zSeg
2e913 6d 65 6e 74 5b 69 45 6e 64 5d 3d 3d 27 3a 27 20  ment[iEnd]==':' 
2e914 26 26 0a 20 20 20 20 20 20 20 20 20 28 69 43 6f  &&.         (iCo
2e915 6c 20 3d 20 63 68 65 63 6b 43 6f 6c 75 6d 6e 53  l = checkColumnS
2e916 70 65 63 69 66 69 65 72 28 70 51 75 65 72 79 2d  pecifier(pQuery-
2e917 3e 70 46 74 73 2c 20 7a 54 6f 6b 65 6e 2c 20 6e  >pFts, zToken, n
2e918 54 6f 6b 65 6e 29 29 3e 3d 30 20 29 7b 0a 20 20  Token))>=0 ){.  
2e919 20 20 20 20 70 51 75 65 72 79 2d 3e 6e 65 78 74      pQuery->next
2e91a 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a 20  Column = iCol;. 
2e91b 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
2e91c 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 69 6e     }.    if( !in
2e91d 50 68 72 61 73 65 20 26 26 20 70 51 75 65 72 79  Phrase && pQuery
2e91e 2d 3e 6e 54 65 72 6d 73 3e 30 20 26 26 20 6e 54  ->nTerms>0 && nT
2e91f 6f 6b 65 6e 3d 3d 32 20 0a 20 20 20 20 20 26 26  oken==2 .     &&
2e920 20 7a 53 65 67 6d 65 6e 74 5b 69 42 65 67 69 6e   zSegment[iBegin
2e921 2b 30 5d 3d 3d 27 4f 27 0a 20 20 20 20 20 26 26  +0]=='O'.     &&
2e922 20 7a 53 65 67 6d 65 6e 74 5b 69 42 65 67 69 6e   zSegment[iBegin
2e923 2b 31 5d 3d 3d 27 52 27 20 0a 20 20 20 20 29 7b  +1]=='R' .    ){
2e924 0a 20 20 20 20 20 20 70 51 75 65 72 79 2d 3e 6e  .      pQuery->n
2e925 65 78 74 49 73 4f 72 20 3d 20 31 3b 0a 20 20 20  extIsOr = 1;.   
2e926 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
2e927 20 7d 0a 20 20 20 20 69 66 28 20 21 69 6e 50 68   }.    if( !inPh
2e928 72 61 73 65 20 26 26 20 70 51 75 65 72 79 2d 3e  rase && pQuery->
2e929 6e 54 65 72 6d 73 3e 30 20 26 26 20 21 70 51 75  nTerms>0 && !pQu
2e92a 65 72 79 2d 3e 6e 65 78 74 49 73 4f 72 20 26 26  ery->nextIsOr &&
2e92b 20 6e 54 6f 6b 65 6e 3d 3d 34 20 0a 20 20 20 20   nToken==4 .    
2e92c 20 20 26 26 20 6d 65 6d 63 6d 70 28 26 7a 53 65    && memcmp(&zSe
2e92d 67 6d 65 6e 74 5b 69 42 65 67 69 6e 5d 2c 20 22  gment[iBegin], "
2e92e 4e 45 41 52 22 2c 20 34 29 3d 3d 30 0a 20 20 20  NEAR", 4)==0.   
2e92f 20 29 7b 0a 20 20 20 20 20 20 51 75 65 72 79 54   ){.      QueryT
2e930 65 72 6d 20 2a 70 54 65 72 6d 20 3d 20 26 70 51  erm *pTerm = &pQ
2e931 75 65 72 79 2d 3e 70 54 65 72 6d 73 5b 70 51 75  uery->pTerms[pQu
2e932 65 72 79 2d 3e 6e 54 65 72 6d 73 2d 31 5d 3b 0a  ery->nTerms-1];.
2e933 20 20 20 20 20 20 69 66 28 20 28 69 42 65 67 69        if( (iBegi
2e934 6e 2b 36 29 3c 6e 53 65 67 6d 65 6e 74 20 0a 20  n+6)<nSegment . 
2e935 20 20 20 20 20 20 26 26 20 7a 53 65 67 6d 65 6e        && zSegmen
2e936 74 5b 69 42 65 67 69 6e 2b 34 5d 20 3d 3d 20 27  t[iBegin+4] == '
2e937 2f 27 0a 20 20 20 20 20 20 20 26 26 20 69 73 64  /'.       && isd
2e938 69 67 69 74 28 7a 53 65 67 6d 65 6e 74 5b 69 42  igit(zSegment[iB
2e939 65 67 69 6e 2b 35 5d 29 0a 20 20 20 20 20 20 29  egin+5]).      )
2e93a 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6b 3b  {.        int k;
2e93b 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d 2d 3e  .        pTerm->
2e93c 6e 4e 65 61 72 20 3d 20 30 3b 0a 20 20 20 20 20  nNear = 0;.     
2e93d 20 20 20 66 6f 72 28 6b 3d 35 3b 20 28 69 42 65     for(k=5; (iBe
2e93e 67 69 6e 2b 6b 29 3c 3d 6e 53 65 67 6d 65 6e 74  gin+k)<=nSegment
2e93f 20 26 26 20 69 73 64 69 67 69 74 28 7a 53 65 67   && isdigit(zSeg
2e940 6d 65 6e 74 5b 69 42 65 67 69 6e 2b 6b 5d 29 3b  ment[iBegin+k]);
2e941 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   k++){.         
2e942 20 70 54 65 72 6d 2d 3e 6e 4e 65 61 72 20 3d 20   pTerm->nNear = 
2e943 70 54 65 72 6d 2d 3e 6e 4e 65 61 72 2a 31 30 20  pTerm->nNear*10 
2e944 2b 20 28 7a 53 65 67 6d 65 6e 74 5b 69 42 65 67  + (zSegment[iBeg
2e945 69 6e 2b 6b 5d 20 2d 20 27 30 27 29 3b 0a 20 20  in+k] - '0');.  
2e946 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2e947 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70  pModule->xNext(p
2e948 43 75 72 73 6f 72 2c 20 26 7a 54 6f 6b 65 6e 2c  Cursor, &zToken,
2e949 20 26 6e 54 6f 6b 65 6e 2c 20 26 69 42 65 67 69   &nToken, &iBegi
2e94a 6e 2c 20 26 69 45 6e 64 2c 20 26 69 50 6f 73 29  n, &iEnd, &iPos)
2e94b 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b  ;.      } else {
2e94c 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d 2d 3e  .        pTerm->
2e94d 6e 4e 65 61 72 20 3d 20 53 51 4c 49 54 45 5f 46  nNear = SQLITE_F
2e94e 54 53 33 5f 44 45 46 41 55 4c 54 5f 4e 45 41 52  TS3_DEFAULT_NEAR
2e94f 5f 50 41 52 41 4d 3b 0a 20 20 20 20 20 20 7d 0a  _PARAM;.      }.
2e950 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e 4e 65        pTerm->nNe
2e951 61 72 2b 2b 3b 0a 20 20 20 20 20 20 63 6f 6e 74  ar++;.      cont
2e952 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  inue;.    }..   
2e953 20 71 75 65 72 79 41 64 64 28 70 51 75 65 72 79   queryAdd(pQuery
2e954 2c 20 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e  , zToken, nToken
2e955 29 3b 0a 20 20 20 20 69 66 28 20 21 69 6e 50 68  );.    if( !inPh
2e956 72 61 73 65 20 26 26 20 69 42 65 67 69 6e 3e 30  rase && iBegin>0
2e957 20 26 26 20 7a 53 65 67 6d 65 6e 74 5b 69 42 65   && zSegment[iBe
2e958 67 69 6e 2d 31 5d 3d 3d 27 2d 27 20 29 7b 0a 20  gin-1]=='-' ){. 
2e959 20 20 20 20 20 70 51 75 65 72 79 2d 3e 70 54 65       pQuery->pTe
2e95a 72 6d 73 5b 70 51 75 65 72 79 2d 3e 6e 54 65 72  rms[pQuery->nTer
2e95b 6d 73 2d 31 5d 2e 69 73 4e 6f 74 20 3d 20 31 3b  ms-1].isNot = 1;
2e95c 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69  .    }.    if( i
2e95d 45 6e 64 3c 6e 53 65 67 6d 65 6e 74 20 26 26 20  End<nSegment && 
2e95e 7a 53 65 67 6d 65 6e 74 5b 69 45 6e 64 5d 3d 3d  zSegment[iEnd]==
2e95f 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 70 51 75  '*' ){.      pQu
2e960 65 72 79 2d 3e 70 54 65 72 6d 73 5b 70 51 75 65  ery->pTerms[pQue
2e961 72 79 2d 3e 6e 54 65 72 6d 73 2d 31 5d 2e 69 73  ry->nTerms-1].is
2e962 50 72 65 66 69 78 20 3d 20 31 3b 0a 20 20 20 20  Prefix = 1;.    
2e963 7d 0a 20 20 20 20 70 51 75 65 72 79 2d 3e 70 54  }.    pQuery->pT
2e964 65 72 6d 73 5b 70 51 75 65 72 79 2d 3e 6e 54 65  erms[pQuery->nTe
2e965 72 6d 73 2d 31 5d 2e 69 50 68 72 61 73 65 20 3d  rms-1].iPhrase =
2e966 20 6e 54 65 72 6d 3b 0a 20 20 20 20 69 66 28 20   nTerm;.    if( 
2e967 69 6e 50 68 72 61 73 65 20 29 7b 0a 20 20 20 20  inPhrase ){.    
2e968 20 20 6e 54 65 72 6d 2b 2b 3b 0a 20 20 20 20 7d    nTerm++;.    }
2e969 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 6e 50 68  .  }..  if( inPh
2e96a 72 61 73 65 20 26 26 20 70 51 75 65 72 79 2d 3e  rase && pQuery->
2e96b 6e 54 65 72 6d 73 3e 66 69 72 73 74 49 6e 64 65  nTerms>firstInde
2e96c 78 20 29 7b 0a 20 20 20 20 70 51 75 65 72 79 2d  x ){.    pQuery-
2e96d 3e 70 54 65 72 6d 73 5b 66 69 72 73 74 49 6e 64  >pTerms[firstInd
2e96e 65 78 5d 2e 6e 50 68 72 61 73 65 20 3d 20 70 51  ex].nPhrase = pQ
2e96f 75 65 72 79 2d 3e 6e 54 65 72 6d 73 20 2d 20 66  uery->nTerms - f
2e970 69 72 73 74 49 6e 64 65 78 20 2d 20 31 3b 0a 20  irstIndex - 1;. 
2e971 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 4d 6f   }..  return pMo
2e972 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 43 75  dule->xClose(pCu
2e973 72 73 6f 72 29 3b 0a 7d 0a 0a 2f 2a 20 50 61 72  rsor);.}../* Par
2e974 73 65 20 61 20 71 75 65 72 79 20 73 74 72 69 6e  se a query strin
2e975 67 2c 20 79 69 65 6c 64 69 6e 67 20 61 20 51 75  g, yielding a Qu
2e976 65 72 79 20 6f 62 6a 65 63 74 20 70 51 75 65 72  ery object pQuer
2e977 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c  y..**.** The cal
2e978 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 77 69  ling function wi
2e979 6c 6c 20 6e 65 65 64 20 74 6f 20 71 75 65 72 79  ll need to query
2e97a 43 6c 65 61 72 28 29 20 74 6f 20 63 6c 65 61 6e  Clear() to clean
2e97b 20 75 70 0a 2a 2a 20 74 68 65 20 64 79 6e 61 6d   up.** the dynam
2e97c 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64  ically allocated
2e97d 20 6d 65 6d 6f 72 79 20 68 65 6c 64 20 62 79 20   memory held by 
2e97e 70 51 75 65 72 79 2e 0a 2a 2f 0a 73 74 61 74 69  pQuery..*/.stati
2e97f 63 20 69 6e 74 20 70 61 72 73 65 51 75 65 72 79  c int parseQuery
2e980 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61  (.  fulltext_vta
2e981 62 20 2a 76 2c 20 20 20 20 20 20 20 20 2f 2a 20  b *v,        /* 
2e982 54 68 65 20 66 75 6c 6c 74 65 78 74 20 69 6e 64  The fulltext ind
2e983 65 78 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ex */.  const ch
2e984 61 72 20 2a 7a 49 6e 70 75 74 2c 20 20 20 20 20  ar *zInput,     
2e985 20 2f 2a 20 49 6e 70 75 74 20 74 65 78 74 20 6f   /* Input text o
2e986 66 20 74 68 65 20 71 75 65 72 79 20 73 74 72 69  f the query stri
2e987 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 70  ng */.  int nInp
2e988 75 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ut,             
2e989 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20   /* Size of the 
2e98a 69 6e 70 75 74 20 74 65 78 74 20 2a 2f 0a 20 20  input text */.  
2e98b 69 6e 74 20 64 66 6c 74 43 6f 6c 75 6d 6e 2c 20  int dfltColumn, 
2e98c 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61           /* Defa
2e98d 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  ult column of th
2e98e 65 20 69 6e 64 65 78 20 74 6f 20 6d 61 74 63 68  e index to match
2e98f 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 51 75   against */.  Qu
2e990 65 72 79 20 2a 70 51 75 65 72 79 20 20 20 20 20  ery *pQuery     
2e991 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
2e992 74 68 65 20 70 61 72 73 65 20 72 65 73 75 6c 74  the parse result
2e993 73 20 68 65 72 65 2e 20 2a 2f 0a 29 7b 0a 20 20  s here. */.){.  
2e994 69 6e 74 20 69 49 6e 70 75 74 2c 20 69 6e 50 68  int iInput, inPh
2e995 72 61 73 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20  rase = 0;.  int 
2e996 69 69 3b 0a 20 20 51 75 65 72 79 54 65 72 6d 20  ii;.  QueryTerm 
2e997 2a 61 54 65 72 6d 3b 0a 0a 20 20 69 66 28 20 7a  *aTerm;..  if( z
2e998 49 6e 70 75 74 3d 3d 30 20 29 20 6e 49 6e 70 75  Input==0 ) nInpu
2e999 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 49 6e  t = 0;.  if( nIn
2e99a 70 75 74 3c 30 20 29 20 6e 49 6e 70 75 74 20 3d  put<0 ) nInput =
2e99b 20 73 74 72 6c 65 6e 28 7a 49 6e 70 75 74 29 3b   strlen(zInput);
2e99c 0a 20 20 70 51 75 65 72 79 2d 3e 6e 54 65 72 6d  .  pQuery->nTerm
2e99d 73 20 3d 20 30 3b 0a 20 20 70 51 75 65 72 79 2d  s = 0;.  pQuery-
2e99e 3e 70 54 65 72 6d 73 20 3d 20 4e 55 4c 4c 3b 0a  >pTerms = NULL;.
2e99f 20 20 70 51 75 65 72 79 2d 3e 6e 65 78 74 49 73    pQuery->nextIs
2e9a0 4f 72 20 3d 20 30 3b 0a 20 20 70 51 75 65 72 79  Or = 0;.  pQuery
2e9a1 2d 3e 6e 65 78 74 43 6f 6c 75 6d 6e 20 3d 20 64  ->nextColumn = d
2e9a2 66 6c 74 43 6f 6c 75 6d 6e 3b 0a 20 20 70 51 75  fltColumn;.  pQu
2e9a3 65 72 79 2d 3e 64 66 6c 74 43 6f 6c 75 6d 6e 20  ery->dfltColumn 
2e9a4 3d 20 64 66 6c 74 43 6f 6c 75 6d 6e 3b 0a 20 20  = dfltColumn;.  
2e9a5 70 51 75 65 72 79 2d 3e 70 46 74 73 20 3d 20 76  pQuery->pFts = v
2e9a6 3b 0a 0a 20 20 66 6f 72 28 69 49 6e 70 75 74 3d  ;..  for(iInput=
2e9a7 30 3b 20 69 49 6e 70 75 74 3c 6e 49 6e 70 75 74  0; iInput<nInput
2e9a8 3b 20 2b 2b 69 49 6e 70 75 74 29 7b 0a 20 20 20  ; ++iInput){.   
2e9a9 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28   int i;.    for(
2e9aa 69 3d 69 49 6e 70 75 74 3b 20 69 3c 6e 49 6e 70  i=iInput; i<nInp
2e9ab 75 74 20 26 26 20 7a 49 6e 70 75 74 5b 69 5d 21  ut && zInput[i]!
2e9ac 3d 27 22 27 3b 20 2b 2b 69 29 7b 7d 0a 20 20 20  ='"'; ++i){}.   
2e9ad 20 69 66 28 20 69 3e 69 49 6e 70 75 74 20 29 7b   if( i>iInput ){
2e9ae 0a 20 20 20 20 20 20 74 6f 6b 65 6e 69 7a 65 53  .      tokenizeS
2e9af 65 67 6d 65 6e 74 28 76 2d 3e 70 54 6f 6b 65 6e  egment(v->pToken
2e9b0 69 7a 65 72 2c 20 7a 49 6e 70 75 74 2b 69 49 6e  izer, zInput+iIn
2e9b1 70 75 74 2c 20 69 2d 69 49 6e 70 75 74 2c 20 69  put, i-iInput, i
2e9b2 6e 50 68 72 61 73 65 2c 0a 20 20 20 20 20 20 20  nPhrase,.       
2e9b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e9b4 70 51 75 65 72 79 29 3b 0a 20 20 20 20 7d 0a 20  pQuery);.    }. 
2e9b5 20 20 20 69 49 6e 70 75 74 20 3d 20 69 3b 0a 20     iInput = i;. 
2e9b6 20 20 20 69 66 28 20 69 3c 6e 49 6e 70 75 74 20     if( i<nInput 
2e9b7 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
2e9b8 20 7a 49 6e 70 75 74 5b 69 5d 3d 3d 27 22 27 20   zInput[i]=='"' 
2e9b9 29 3b 0a 20 20 20 20 20 20 69 6e 50 68 72 61 73  );.      inPhras
2e9ba 65 20 3d 20 21 69 6e 50 68 72 61 73 65 3b 0a 20  e = !inPhrase;. 
2e9bb 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
2e9bc 69 6e 50 68 72 61 73 65 20 29 7b 0a 20 20 20 20  inPhrase ){.    
2e9bd 2f 2a 20 75 6e 6d 61 74 63 68 65 64 20 71 75 6f  /* unmatched quo
2e9be 74 65 20 2a 2f 0a 20 20 20 20 71 75 65 72 79 43  te */.    queryC
2e9bf 6c 65 61 72 28 70 51 75 65 72 79 29 3b 0a 20 20  lear(pQuery);.  
2e9c0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2e9c1 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  ERROR;.  }..  /*
2e9c2 20 4d 6f 64 69 66 79 20 74 68 65 20 76 61 6c 75   Modify the valu
2e9c3 65 73 20 6f 66 20 74 68 65 20 51 75 65 72 79 54  es of the QueryT
2e9c4 65 72 6d 2e 6e 50 68 72 61 73 65 20 76 61 72 69  erm.nPhrase vari
2e9c5 61 62 6c 65 73 20 74 6f 20 61 63 63 6f 75 6e 74  ables to account
2e9c6 20 66 6f 72 0a 20 20 2a 2a 20 74 68 65 20 4e 45   for.  ** the NE
2e9c7 41 52 20 6f 70 65 72 61 74 6f 72 2e 20 46 6f 72  AR operator. For
2e9c8 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66   the purposes of
2e9c9 20 51 75 65 72 79 54 65 72 6d 2e 6e 50 68 72 61   QueryTerm.nPhra
2e9ca 73 65 2c 20 70 68 72 61 73 65 73 0a 20 20 2a 2a  se, phrases.  **
2e9cb 20 61 6e 64 20 74 6f 6b 65 6e 73 20 63 6f 6e 6e   and tokens conn
2e9cc 65 63 74 65 64 20 62 79 20 74 68 65 20 4e 45 41  ected by the NEA
2e9cd 52 20 6f 70 65 72 61 74 6f 72 20 61 72 65 20 68  R operator are h
2e9ce 61 6e 64 6c 65 64 20 61 73 20 61 20 73 69 6e 67  andled as a sing
2e9cf 6c 65 0a 20 20 2a 2a 20 70 68 72 61 73 65 2e 20  le.  ** phrase. 
2e9d0 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f  See comments abo
2e9d1 76 65 20 74 68 65 20 51 75 65 72 79 54 65 72 6d  ve the QueryTerm
2e9d2 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 64   structure for d
2e9d3 65 74 61 69 6c 73 2e 0a 20 20 2a 2f 0a 20 20 61  etails..  */.  a
2e9d4 54 65 72 6d 20 3d 20 70 51 75 65 72 79 2d 3e 70  Term = pQuery->p
2e9d5 54 65 72 6d 73 3b 0a 20 20 66 6f 72 28 69 69 3d  Terms;.  for(ii=
2e9d6 30 3b 20 69 69 3c 70 51 75 65 72 79 2d 3e 6e 54  0; ii<pQuery->nT
2e9d7 65 72 6d 73 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  erms; ii++){.   
2e9d8 20 69 66 28 20 61 54 65 72 6d 5b 69 69 5d 2e 6e   if( aTerm[ii].n
2e9d9 4e 65 61 72 20 7c 7c 20 61 54 65 72 6d 5b 69 69  Near || aTerm[ii
2e9da 5d 2e 6e 50 68 72 61 73 65 20 29 7b 0a 20 20 20  ].nPhrase ){.   
2e9db 20 20 20 77 68 69 6c 65 20 28 61 54 65 72 6d 5b     while (aTerm[
2e9dc 69 69 2b 61 54 65 72 6d 5b 69 69 5d 2e 6e 50 68  ii+aTerm[ii].nPh
2e9dd 72 61 73 65 5d 2e 6e 4e 65 61 72 29 20 7b 0a 20  rase].nNear) {. 
2e9de 20 20 20 20 20 20 20 61 54 65 72 6d 5b 69 69 5d         aTerm[ii]
2e9df 2e 6e 50 68 72 61 73 65 20 2b 3d 20 28 31 20 2b  .nPhrase += (1 +
2e9e0 20 61 54 65 72 6d 5b 69 69 2b 61 54 65 72 6d 5b   aTerm[ii+aTerm[
2e9e1 69 69 5d 2e 6e 50 68 72 61 73 65 2b 31 5d 2e 6e  ii].nPhrase+1].n
2e9e2 50 68 72 61 73 65 29 3b 0a 20 20 20 20 20 20 7d  Phrase);.      }
2e9e3 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65  .    }.  }..  re
2e9e4 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
2e9e5 7d 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  }../* TODO(shess
2e9e6 29 20 52 65 66 61 63 74 6f 72 20 74 68 65 20 63  ) Refactor the c
2e9e7 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65 20 74 68  ode to remove th
2e9e8 69 73 20 66 6f 72 77 61 72 64 20 64 65 63 6c 2e  is forward decl.
2e9e9 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66   */.static int f
2e9ea 6c 75 73 68 50 65 6e 64 69 6e 67 54 65 72 6d 73  lushPendingTerms
2e9eb 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
2e9ec 76 29 3b 0a 0a 2f 2a 20 50 65 72 66 6f 72 6d 20  v);../* Perform 
2e9ed 61 20 66 75 6c 6c 2d 74 65 78 74 20 71 75 65 72  a full-text quer
2e9ee 79 20 75 73 69 6e 67 20 74 68 65 20 73 65 61 72  y using the sear
2e9ef 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  ch expression in
2e9f0 0a 2a 2a 20 7a 49 6e 70 75 74 5b 30 2e 2e 6e 49  .** zInput[0..nI
2e9f1 6e 70 75 74 2d 31 5d 2e 20 20 52 65 74 75 72 6e  nput-1].  Return
2e9f2 20 61 20 6c 69 73 74 20 6f 66 20 6d 61 74 63 68   a list of match
2e9f3 69 6e 67 20 64 6f 63 75 6d 65 6e 74 73 0a 2a 2a  ing documents.**
2e9f4 20 69 6e 20 70 52 65 73 75 6c 74 2e 0a 2a 2a 0a   in pResult..**.
2e9f5 2a 2a 20 51 75 65 72 69 65 73 20 6d 75 73 74 20  ** Queries must 
2e9f6 6d 61 74 63 68 20 63 6f 6c 75 6d 6e 20 69 43 6f  match column iCo
2e9f7 6c 75 6d 6e 2e 20 20 4f 72 20 69 66 20 69 43 6f  lumn.  Or if iCo
2e9f8 6c 75 6d 6e 3e 3d 6e 43 6f 6c 75 6d 6e 0a 2a 2a  lumn>=nColumn.**
2e9f9 20 74 68 65 79 20 61 72 65 20 61 6c 6c 6f 77 65   they are allowe
2e9fa 64 20 74 6f 20 6d 61 74 63 68 20 61 67 61 69 6e  d to match again
2e9fb 73 74 20 61 6e 79 20 63 6f 6c 75 6d 6e 2e 0a 2a  st any column..*
2e9fc 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c  /.static int ful
2e9fd 6c 74 65 78 74 51 75 65 72 79 28 0a 20 20 66 75  ltextQuery(.  fu
2e9fe 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
2e9ff 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6c 6c       /* The full
2ea00 20 74 65 78 74 20 69 6e 64 65 78 20 2a 2f 0a 20   text index */. 
2ea01 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20   int iColumn,   
2ea02 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 74 63 68          /* Match
2ea03 20 61 67 61 69 6e 73 74 20 74 68 69 73 20 63 6f   against this co
2ea04 6c 75 6d 6e 20 62 79 20 64 65 66 61 75 6c 74 20  lumn by default 
2ea05 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
2ea06 2a 7a 49 6e 70 75 74 2c 20 20 20 20 2f 2a 20 54  *zInput,    /* T
2ea07 68 65 20 71 75 65 72 79 20 73 74 72 69 6e 67 20  he query string 
2ea08 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 70 75 74 2c  */.  int nInput,
2ea09 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2ea0a 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
2ea0b 6e 20 7a 49 6e 70 75 74 5b 5d 20 2a 2f 0a 20 20  n zInput[] */.  
2ea0c 44 61 74 61 42 75 66 66 65 72 20 2a 70 52 65 73  DataBuffer *pRes
2ea0d 75 6c 74 2c 20 20 20 2f 2a 20 57 72 69 74 65 20  ult,   /* Write 
2ea0e 74 68 65 20 72 65 73 75 6c 74 20 64 6f 63 6c 69  the result docli
2ea0f 73 74 20 68 65 72 65 20 2a 2f 0a 20 20 51 75 65  st here */.  Que
2ea10 72 79 20 2a 70 51 75 65 72 79 20 20 20 20 20 20  ry *pQuery      
2ea11 20 20 20 20 2f 2a 20 50 75 74 20 70 61 72 73 65      /* Put parse
2ea12 64 20 71 75 65 72 79 20 73 74 72 69 6e 67 20 68  d query string h
2ea13 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ere */.){.  int 
2ea14 69 2c 20 69 4e 65 78 74 2c 20 72 63 3b 0a 20 20  i, iNext, rc;.  
2ea15 44 61 74 61 42 75 66 66 65 72 20 6c 65 66 74 2c  DataBuffer left,
2ea16 20 72 69 67 68 74 2c 20 6f 72 2c 20 6e 65 77 3b   right, or, new;
2ea17 0a 20 20 69 6e 74 20 6e 4e 6f 74 20 3d 20 30 3b  .  int nNot = 0;
2ea18 0a 20 20 51 75 65 72 79 54 65 72 6d 20 2a 61 54  .  QueryTerm *aT
2ea19 65 72 6d 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28  erm;..  /* TODO(
2ea1a 73 68 65 73 73 29 20 49 6e 73 74 65 61 64 20 6f  shess) Instead o
2ea1b 66 20 66 6c 75 73 68 69 6e 67 20 70 65 6e 64 69  f flushing pendi
2ea1c 6e 67 54 65 72 6d 73 2c 20 77 65 20 63 6f 75 6c  ngTerms, we coul
2ea1d 64 20 71 75 65 72 79 20 66 6f 72 0a 20 20 2a 2a  d query for.  **
2ea1e 20 74 68 65 20 72 65 6c 65 76 61 6e 74 20 74 65   the relevant te
2ea1f 72 6d 20 61 6e 64 20 6d 65 72 67 65 20 74 68 65  rm and merge the
2ea20 20 64 6f 63 6c 69 73 74 20 69 6e 74 6f 20 77 68   doclist into wh
2ea21 61 74 20 77 65 20 72 65 63 65 69 76 65 20 66 72  at we receive fr
2ea22 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61  om.  ** the data
2ea23 62 61 73 65 2e 20 20 57 61 69 74 20 61 6e 64 20  base.  Wait and 
2ea24 73 65 65 20 69 66 20 74 68 69 73 20 69 73 20 61  see if this is a
2ea25 20 63 6f 6d 6d 6f 6e 20 69 73 73 75 65 2c 20 66   common issue, f
2ea26 69 72 73 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  irst..  **.  ** 
2ea27 41 20 67 6f 6f 64 20 72 65 61 73 6f 6e 20 6e 6f  A good reason no
2ea28 74 20 74 6f 20 66 6c 75 73 68 20 69 73 20 74 6f  t to flush is to
2ea29 20 6e 6f 74 20 67 65 6e 65 72 61 74 65 20 75 70   not generate up
2ea2a 64 61 74 65 2d 72 65 6c 61 74 65 64 0a 20 20 2a  date-related.  *
2ea2b 2a 20 65 72 72 6f 72 20 63 6f 64 65 73 20 66 72  * error codes fr
2ea2c 6f 6d 20 68 65 72 65 2e 0a 20 20 2a 2f 0a 0a 20  om here..  */.. 
2ea2d 20 2f 2a 20 46 6c 75 73 68 20 61 6e 79 20 62 75   /* Flush any bu
2ea2e 66 66 65 72 65 64 20 75 70 64 61 74 65 73 20 62  ffered updates b
2ea2f 65 66 6f 72 65 20 65 78 65 63 75 74 69 6e 67 20  efore executing 
2ea30 74 68 65 20 71 75 65 72 79 2e 20 2a 2f 0a 20 20  the query. */.  
2ea31 72 63 20 3d 20 66 6c 75 73 68 50 65 6e 64 69 6e  rc = flushPendin
2ea32 67 54 65 72 6d 73 28 76 29 3b 0a 20 20 69 66 28  gTerms(v);.  if(
2ea33 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2ea34 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f   return rc;..  /
2ea35 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 49 20  * TODO(shess) I 
2ea36 74 68 69 6e 6b 20 74 68 61 74 20 74 68 65 20 71  think that the q
2ea37 75 65 72 79 43 6c 65 61 72 28 29 20 63 61 6c 6c  ueryClear() call
2ea38 73 20 62 65 6c 6f 77 20 61 72 65 20 6e 6f 74 0a  s below are not.
2ea39 20 20 2a 2a 20 6e 65 63 65 73 73 61 72 79 2c 20    ** necessary, 
2ea3a 62 65 63 61 75 73 65 20 66 75 6c 6c 74 65 78 74  because fulltext
2ea3b 43 6c 6f 73 65 28 29 20 61 6c 72 65 61 64 79 20  Close() already 
2ea3c 63 6c 65 61 72 73 20 74 68 65 20 71 75 65 72 79  clears the query
2ea3d 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 70 61  ..  */.  rc = pa
2ea3e 72 73 65 51 75 65 72 79 28 76 2c 20 7a 49 6e 70  rseQuery(v, zInp
2ea3f 75 74 2c 20 6e 49 6e 70 75 74 2c 20 69 43 6f 6c  ut, nInput, iCol
2ea40 75 6d 6e 2c 20 70 51 75 65 72 79 29 3b 0a 20 20  umn, pQuery);.  
2ea41 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2ea42 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
2ea43 20 20 2f 2a 20 45 6d 70 74 79 20 6f 72 20 4e 55    /* Empty or NU
2ea44 4c 4c 20 71 75 65 72 69 65 73 20 72 65 74 75 72  LL queries retur
2ea45 6e 20 6e 6f 20 72 65 73 75 6c 74 73 2e 20 2a 2f  n no results. */
2ea46 0a 20 20 69 66 28 20 70 51 75 65 72 79 2d 3e 6e  .  if( pQuery->n
2ea47 54 65 72 6d 73 3d 3d 30 20 29 7b 0a 20 20 20 20  Terms==0 ){.    
2ea48 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 70  dataBufferInit(p
2ea49 52 65 73 75 6c 74 2c 20 30 29 3b 0a 20 20 20 20  Result, 0);.    
2ea4a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
2ea4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 65 72 67  ;.  }..  /* Merg
2ea4c 65 20 41 4e 44 20 74 65 72 6d 73 2e 20 2a 2f 0a  e AND terms. */.
2ea4d 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29    /* TODO(shess)
2ea4e 20 49 20 74 68 69 6e 6b 20 77 65 20 63 61 6e 20   I think we can 
2ea4f 65 61 72 6c 79 2d 65 78 69 74 20 69 66 28 20 69  early-exit if( i
2ea50 3e 6e 4e 6f 74 20 26 26 20 6c 65 66 74 2e 6e 44  >nNot && left.nD
2ea51 61 74 61 3d 3d 30 20 29 2e 20 2a 2f 0a 20 20 61  ata==0 ). */.  a
2ea52 54 65 72 6d 20 3d 20 70 51 75 65 72 79 2d 3e 70  Term = pQuery->p
2ea53 54 65 72 6d 73 3b 0a 20 20 66 6f 72 28 69 20 3d  Terms;.  for(i =
2ea54 20 30 3b 20 69 3c 70 51 75 65 72 79 2d 3e 6e 54   0; i<pQuery->nT
2ea55 65 72 6d 73 3b 20 69 3d 69 4e 65 78 74 29 7b 0a  erms; i=iNext){.
2ea56 20 20 20 20 69 66 28 20 61 54 65 72 6d 5b 69 5d      if( aTerm[i]
2ea57 2e 69 73 4e 6f 74 20 29 7b 0a 20 20 20 20 20 20  .isNot ){.      
2ea58 2f 2a 20 48 61 6e 64 6c 65 20 61 6c 6c 20 4e 4f  /* Handle all NO
2ea59 54 20 74 65 72 6d 73 20 69 6e 20 61 20 73 65 70  T terms in a sep
2ea5a 61 72 61 74 65 20 70 61 73 73 20 2a 2f 0a 20 20  arate pass */.  
2ea5b 20 20 20 20 6e 4e 6f 74 2b 2b 3b 0a 20 20 20 20      nNot++;.    
2ea5c 20 20 69 4e 65 78 74 20 3d 20 69 20 2b 20 61 54    iNext = i + aT
2ea5d 65 72 6d 5b 69 5d 2e 6e 50 68 72 61 73 65 2b 31  erm[i].nPhrase+1
2ea5e 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65  ;.      continue
2ea5f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 4e 65 78  ;.    }.    iNex
2ea60 74 20 3d 20 69 20 2b 20 61 54 65 72 6d 5b 69 5d  t = i + aTerm[i]
2ea61 2e 6e 50 68 72 61 73 65 20 2b 20 31 3b 0a 20 20  .nPhrase + 1;.  
2ea62 20 20 72 63 20 3d 20 64 6f 63 4c 69 73 74 4f 66    rc = docListOf
2ea63 54 65 72 6d 28 76 2c 20 61 54 65 72 6d 5b 69 5d  Term(v, aTerm[i]
2ea64 2e 69 43 6f 6c 75 6d 6e 2c 20 26 61 54 65 72 6d  .iColumn, &aTerm
2ea65 5b 69 5d 2c 20 26 72 69 67 68 74 29 3b 0a 20 20  [i], &right);.  
2ea66 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
2ea67 20 20 69 66 28 20 69 21 3d 6e 4e 6f 74 20 29 20    if( i!=nNot ) 
2ea68 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
2ea69 79 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 20 20  y(&left);.      
2ea6a 71 75 65 72 79 43 6c 65 61 72 28 70 51 75 65 72  queryClear(pQuer
2ea6b 79 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  y);.      return
2ea6c 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77   rc;.    }.    w
2ea6d 68 69 6c 65 28 20 69 4e 65 78 74 3c 70 51 75 65  hile( iNext<pQue
2ea6e 72 79 2d 3e 6e 54 65 72 6d 73 20 26 26 20 61 54  ry->nTerms && aT
2ea6f 65 72 6d 5b 69 4e 65 78 74 5d 2e 69 73 4f 72 20  erm[iNext].isOr 
2ea70 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 64 6f  ){.      rc = do
2ea71 63 4c 69 73 74 4f 66 54 65 72 6d 28 76 2c 20 61  cListOfTerm(v, a
2ea72 54 65 72 6d 5b 69 4e 65 78 74 5d 2e 69 43 6f 6c  Term[iNext].iCol
2ea73 75 6d 6e 2c 20 26 61 54 65 72 6d 5b 69 4e 65 78  umn, &aTerm[iNex
2ea74 74 5d 2c 20 26 6f 72 29 3b 0a 20 20 20 20 20 20  t], &or);.      
2ea75 69 4e 65 78 74 20 2b 3d 20 61 54 65 72 6d 5b 69  iNext += aTerm[i
2ea76 4e 65 78 74 5d 2e 6e 50 68 72 61 73 65 20 2b 20  Next].nPhrase + 
2ea77 31 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20  1;.      if( rc 
2ea78 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  ){.        if( i
2ea79 21 3d 6e 4e 6f 74 20 29 20 64 61 74 61 42 75 66  !=nNot ) dataBuf
2ea7a 66 65 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74  ferDestroy(&left
2ea7b 29 3b 0a 20 20 20 20 20 20 20 20 64 61 74 61 42  );.        dataB
2ea7c 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 72 69  ufferDestroy(&ri
2ea7d 67 68 74 29 3b 0a 20 20 20 20 20 20 20 20 71 75  ght);.        qu
2ea7e 65 72 79 43 6c 65 61 72 28 70 51 75 65 72 79 29  eryClear(pQuery)
2ea7f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
2ea80 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   rc;.      }.   
2ea81 20 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69     dataBufferIni
2ea82 74 28 26 6e 65 77 2c 20 30 29 3b 0a 20 20 20 20  t(&new, 0);.    
2ea83 20 20 64 6f 63 4c 69 73 74 4f 72 4d 65 72 67 65    docListOrMerge
2ea84 28 72 69 67 68 74 2e 70 44 61 74 61 2c 20 72 69  (right.pData, ri
2ea85 67 68 74 2e 6e 44 61 74 61 2c 20 6f 72 2e 70 44  ght.nData, or.pD
2ea86 61 74 61 2c 20 6f 72 2e 6e 44 61 74 61 2c 20 26  ata, or.nData, &
2ea87 6e 65 77 29 3b 0a 20 20 20 20 20 20 64 61 74 61  new);.      data
2ea88 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 72  BufferDestroy(&r
2ea89 69 67 68 74 29 3b 0a 20 20 20 20 20 20 64 61 74  ight);.      dat
2ea8a 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  aBufferDestroy(&
2ea8b 6f 72 29 3b 0a 20 20 20 20 20 20 72 69 67 68 74  or);.      right
2ea8c 20 3d 20 6e 65 77 3b 0a 20 20 20 20 7d 0a 20 20   = new;.    }.  
2ea8d 20 20 69 66 28 20 69 3d 3d 6e 4e 6f 74 20 29 7b    if( i==nNot ){
2ea8e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 69             /* fi
2ea8f 72 73 74 20 74 65 72 6d 20 70 72 6f 63 65 73 73  rst term process
2ea90 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20 6c 65 66  ed. */.      lef
2ea91 74 20 3d 20 72 69 67 68 74 3b 0a 20 20 20 20 7d  t = right;.    }
2ea92 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 61 74 61  else{.      data
2ea93 42 75 66 66 65 72 49 6e 69 74 28 26 6e 65 77 2c  BufferInit(&new,
2ea94 20 30 29 3b 0a 20 20 20 20 20 20 64 6f 63 4c 69   0);.      docLi
2ea95 73 74 41 6e 64 4d 65 72 67 65 28 6c 65 66 74 2e  stAndMerge(left.
2ea96 70 44 61 74 61 2c 20 6c 65 66 74 2e 6e 44 61 74  pData, left.nDat
2ea97 61 2c 20 72 69 67 68 74 2e 70 44 61 74 61 2c 20  a, right.pData, 
2ea98 72 69 67 68 74 2e 6e 44 61 74 61 2c 20 26 6e 65  right.nData, &ne
2ea99 77 29 3b 0a 20 20 20 20 20 20 64 61 74 61 42 75  w);.      dataBu
2ea9a 66 66 65 72 44 65 73 74 72 6f 79 28 26 72 69 67  fferDestroy(&rig
2ea9b 68 74 29 3b 0a 20 20 20 20 20 20 64 61 74 61 42  ht);.      dataB
2ea9c 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 6c 65  ufferDestroy(&le
2ea9d 66 74 29 3b 0a 20 20 20 20 20 20 6c 65 66 74 20  ft);.      left 
2ea9e 3d 20 6e 65 77 3b 0a 20 20 20 20 7d 0a 20 20 7d  = new;.    }.  }
2ea9f 0a 0a 20 20 69 66 28 20 6e 4e 6f 74 3d 3d 70 51  ..  if( nNot==pQ
2eaa0 75 65 72 79 2d 3e 6e 54 65 72 6d 73 20 29 7b 0a  uery->nTerms ){.
2eaa1 20 20 20 20 2f 2a 20 57 65 20 64 6f 20 6e 6f 74      /* We do not
2eaa2 20 79 65 74 20 6b 6e 6f 77 20 68 6f 77 20 74 6f   yet know how to
2eaa3 20 68 61 6e 64 6c 65 20 61 20 71 75 65 72 79 20   handle a query 
2eaa4 6f 66 20 6f 6e 6c 79 20 4e 4f 54 20 74 65 72 6d  of only NOT term
2eaa5 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  s */.    return 
2eaa6 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
2eaa7 7d 0a 0a 20 20 2f 2a 20 44 6f 20 74 68 65 20 45  }..  /* Do the E
2eaa8 58 43 45 50 54 20 74 65 72 6d 73 20 2a 2f 0a 20  XCEPT terms */. 
2eaa9 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 51 75 65   for(i=0; i<pQue
2eaaa 72 79 2d 3e 6e 54 65 72 6d 73 3b 20 20 69 20 2b  ry->nTerms;  i +
2eaab 3d 20 61 54 65 72 6d 5b 69 5d 2e 6e 50 68 72 61  = aTerm[i].nPhra
2eaac 73 65 20 2b 20 31 29 7b 0a 20 20 20 20 69 66 28  se + 1){.    if(
2eaad 20 21 61 54 65 72 6d 5b 69 5d 2e 69 73 4e 6f 74   !aTerm[i].isNot
2eaae 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
2eaaf 20 72 63 20 3d 20 64 6f 63 4c 69 73 74 4f 66 54   rc = docListOfT
2eab0 65 72 6d 28 76 2c 20 61 54 65 72 6d 5b 69 5d 2e  erm(v, aTerm[i].
2eab1 69 43 6f 6c 75 6d 6e 2c 20 26 61 54 65 72 6d 5b  iColumn, &aTerm[
2eab2 69 5d 2c 20 26 72 69 67 68 74 29 3b 0a 20 20 20  i], &right);.   
2eab3 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
2eab4 20 71 75 65 72 79 43 6c 65 61 72 28 70 51 75 65   queryClear(pQue
2eab5 72 79 29 3b 0a 20 20 20 20 20 20 64 61 74 61 42  ry);.      dataB
2eab6 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 6c 65  ufferDestroy(&le
2eab7 66 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ft);.      retur
2eab8 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
2eab9 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26  dataBufferInit(&
2eaba 6e 65 77 2c 20 30 29 3b 0a 20 20 20 20 64 6f 63  new, 0);.    doc
2eabb 4c 69 73 74 45 78 63 65 70 74 4d 65 72 67 65 28  ListExceptMerge(
2eabc 6c 65 66 74 2e 70 44 61 74 61 2c 20 6c 65 66 74  left.pData, left
2eabd 2e 6e 44 61 74 61 2c 20 72 69 67 68 74 2e 70 44  .nData, right.pD
2eabe 61 74 61 2c 20 72 69 67 68 74 2e 6e 44 61 74 61  ata, right.nData
2eabf 2c 20 26 6e 65 77 29 3b 0a 20 20 20 20 64 61 74  , &new);.    dat
2eac0 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  aBufferDestroy(&
2eac1 72 69 67 68 74 29 3b 0a 20 20 20 20 64 61 74 61  right);.    data
2eac2 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 6c  BufferDestroy(&l
2eac3 65 66 74 29 3b 0a 20 20 20 20 6c 65 66 74 20 3d  eft);.    left =
2eac4 20 6e 65 77 3b 0a 20 20 7d 0a 0a 20 20 2a 70 52   new;.  }..  *pR
2eac5 65 73 75 6c 74 20 3d 20 6c 65 66 74 3b 0a 20 20  esult = left;.  
2eac6 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
2eac7 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
2eac8 78 46 69 6c 74 65 72 20 69 6e 74 65 72 66 61 63  xFilter interfac
2eac9 65 20 66 6f 72 20 74 68 65 20 76 69 72 74 75 61  e for the virtua
2eaca 6c 20 74 61 62 6c 65 2e 20 20 53 65 65 0a 2a 2a  l table.  See.**
2eacb 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
2eacc 6c 65 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f  le xFilter metho
2eacd 64 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20  d documentation 
2eace 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a  for additional.*
2eacf 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  * information..*
2ead0 2a 0a 2a 2a 20 49 66 20 69 64 78 4e 75 6d 3d 3d  *.** If idxNum==
2ead1 51 55 45 52 59 5f 47 45 4e 45 52 49 43 20 74 68  QUERY_GENERIC th
2ead2 65 6e 20 64 6f 20 61 20 66 75 6c 6c 20 74 61 62  en do a full tab
2ead3 6c 65 20 73 63 61 6e 20 61 67 61 69 6e 73 74 0a  le scan against.
2ead4 2a 2a 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74  ** the %_content
2ead5 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66   table..**.** If
2ead6 20 69 64 78 4e 75 6d 3d 3d 51 55 45 52 59 5f 44   idxNum==QUERY_D
2ead7 4f 43 49 44 20 74 68 65 6e 20 64 6f 20 61 20 64  OCID then do a d
2ead8 6f 63 69 64 20 6c 6f 6f 6b 75 70 20 66 6f 72 20  ocid lookup for 
2ead9 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 0a 2a  a single entry.*
2eada 2a 20 69 6e 20 74 68 65 20 25 5f 63 6f 6e 74 65  * in the %_conte
2eadb 6e 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  nt table..**.** 
2eadc 49 66 20 69 64 78 4e 75 6d 3e 3d 51 55 45 52 59  If idxNum>=QUERY
2eadd 5f 46 55 4c 4c 54 45 58 54 20 74 68 65 6e 20 75  _FULLTEXT then u
2eade 73 65 20 74 68 65 20 66 75 6c 6c 20 74 65 78 74  se the full text
2eadf 20 69 6e 64 65 78 2e 20 20 54 68 65 0a 2a 2a 20   index.  The.** 
2eae0 63 6f 6c 75 6d 6e 20 6f 6e 20 74 68 65 20 6c 65  column on the le
2eae1 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20  ft-hand side of 
2eae2 74 68 65 20 4d 41 54 43 48 20 6f 70 65 72 61 74  the MATCH operat
2eae3 6f 72 20 69 73 20 63 6f 6c 75 6d 6e 0a 2a 2a 20  or is column.** 
2eae4 6e 75 6d 62 65 72 20 69 64 78 4e 75 6d 2d 51 55  number idxNum-QU
2eae5 45 52 59 5f 46 55 4c 4c 54 45 58 54 2c 20 30 20  ERY_FULLTEXT, 0 
2eae6 69 6e 64 65 78 65 64 2e 20 20 61 72 67 76 5b 30  indexed.  argv[0
2eae7 5d 20 69 73 20 74 68 65 20 72 69 67 68 74 2d 68  ] is the right-h
2eae8 61 6e 64 0a 2a 2a 20 73 69 64 65 20 6f 66 20 74  and.** side of t
2eae9 68 65 20 4d 41 54 43 48 20 6f 70 65 72 61 74 6f  he MATCH operato
2eaea 72 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68  r..*/./* TODO(sh
2eaeb 65 73 73 29 20 55 70 67 72 61 64 65 20 74 68 65  ess) Upgrade the
2eaec 20 63 75 72 73 6f 72 20 69 6e 69 74 69 61 6c 69   cursor initiali
2eaed 7a 61 74 69 6f 6e 20 61 6e 64 20 64 65 73 74 72  zation and destr
2eaee 75 63 74 69 6f 6e 20 74 6f 0a 2a 2a 20 61 63 63  uction to.** acc
2eaef 6f 75 6e 74 20 66 6f 72 20 66 75 6c 6c 74 65 78  ount for fulltex
2eaf0 74 46 69 6c 74 65 72 28 29 20 62 65 69 6e 67 20  tFilter() being 
2eaf1 63 61 6c 6c 65 64 20 6d 75 6c 74 69 70 6c 65 20  called multiple 
2eaf2 74 69 6d 65 73 20 6f 6e 20 74 68 65 0a 2a 2a 20  times on the.** 
2eaf3 73 61 6d 65 20 63 75 72 73 6f 72 2e 20 20 54 68  same cursor.  Th
2eaf4 65 20 63 75 72 72 65 6e 74 20 73 6f 6c 75 74 69  e current soluti
2eaf5 6f 6e 20 69 73 20 76 65 72 79 20 66 72 61 67 69  on is very fragi
2eaf6 6c 65 2e 20 20 41 70 70 6c 79 20 66 69 78 20 74  le.  Apply fix t
2eaf7 6f 0a 2a 2a 20 66 74 73 33 20 61 73 20 61 70 70  o.** fts3 as app
2eaf8 72 6f 70 72 69 61 74 65 2e 0a 2a 2f 0a 73 74 61  ropriate..*/.sta
2eaf9 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74  tic int fulltext
2eafa 46 69 6c 74 65 72 28 0a 20 20 73 71 6c 69 74 65  Filter(.  sqlite
2eafb 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70  3_vtab_cursor *p
2eafc 43 75 72 73 6f 72 2c 20 20 20 20 20 2f 2a 20 54  Cursor,     /* T
2eafd 68 65 20 63 75 72 73 6f 72 20 75 73 65 64 20 66  he cursor used f
2eafe 6f 72 20 74 68 69 73 20 71 75 65 72 79 20 2a 2f  or this query */
2eaff 0a 20 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63  .  int idxNum, c
2eb00 6f 6e 73 74 20 63 68 61 72 20 2a 69 64 78 53 74  onst char *idxSt
2eb01 72 2c 20 20 20 2f 2a 20 57 68 69 63 68 20 69 6e  r,   /* Which in
2eb02 64 65 78 69 6e 67 20 73 63 68 65 6d 65 20 74 6f  dexing scheme to
2eb03 20 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 61 72   use */.  int ar
2eb04 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
2eb05 65 20 2a 2a 61 72 67 76 20 20 20 20 2f 2a 20 41  e **argv    /* A
2eb06 72 67 75 6d 65 6e 74 73 20 66 6f 72 20 74 68 65  rguments for the
2eb07 20 69 6e 64 65 78 69 6e 67 20 73 63 68 65 6d 65   indexing scheme
2eb08 20 2a 2f 0a 29 7b 0a 20 20 66 75 6c 6c 74 65 78   */.){.  fulltex
2eb09 74 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 66  t_cursor *c = (f
2eb0a 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a  ulltext_cursor *
2eb0b 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 66 75 6c  ) pCursor;.  ful
2eb0c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d 20  ltext_vtab *v = 
2eb0d 63 75 72 73 6f 72 5f 76 74 61 62 28 63 29 3b 0a  cursor_vtab(c);.
2eb0e 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 46 54 53    int rc;..  FTS
2eb0f 54 52 41 43 45 28 28 22 46 54 53 33 20 46 69 6c  TRACE(("FTS3 Fil
2eb10 74 65 72 20 25 70 5c 6e 22 2c 70 43 75 72 73 6f  ter %p\n",pCurso
2eb11 72 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  r));..  /* If th
2eb12 65 20 63 75 72 73 6f 72 20 68 61 73 20 61 20 73  e cursor has a s
2eb13 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 77 61  tatement that wa
2eb14 73 20 6e 6f 74 20 70 72 65 70 61 72 65 64 20 61  s not prepared a
2eb15 63 63 6f 72 64 69 6e 67 20 74 6f 0a 20 20 2a 2a  ccording to.  **
2eb16 20 69 64 78 4e 75 6d 2c 20 63 6c 65 61 72 20 69   idxNum, clear i
2eb17 74 2e 20 20 49 20 62 65 6c 69 65 76 65 20 61 6c  t.  I believe al
2eb18 6c 20 63 61 6c 6c 73 20 74 6f 20 66 75 6c 6c 74  l calls to fullt
2eb19 65 78 74 46 69 6c 74 65 72 20 77 69 74 68 20 61  extFilter with a
2eb1a 0a 20 20 2a 2a 20 67 69 76 65 6e 20 63 75 72 73  .  ** given curs
2eb1b 6f 72 20 77 69 6c 6c 20 68 61 76 65 20 74 68 65  or will have the
2eb1c 20 73 61 6d 65 20 69 64 78 4e 75 6d 20 2c 20 62   same idxNum , b
2eb1d 75 74 20 69 6e 20 74 68 69 73 20 63 61 73 65 20  ut in this case 
2eb1e 69 74 27 73 0a 20 20 2a 2a 20 65 61 73 79 20 74  it's.  ** easy t
2eb1f 6f 20 62 65 20 73 61 66 65 2e 0a 20 20 2a 2f 0a  o be safe..  */.
2eb20 20 20 69 66 28 20 63 2d 3e 70 53 74 6d 74 20 26    if( c->pStmt &
2eb21 26 20 63 2d 3e 69 43 75 72 73 6f 72 54 79 70 65  & c->iCursorType
2eb22 21 3d 69 64 78 4e 75 6d 20 29 7b 0a 20 20 20 20  !=idxNum ){.    
2eb23 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
2eb24 28 63 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20  (c->pStmt);.    
2eb25 63 2d 3e 70 53 74 6d 74 20 3d 20 4e 55 4c 4c 3b  c->pStmt = NULL;
2eb26 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 61  .  }..  /* Get a
2eb27 20 66 72 65 73 68 20 73 74 61 74 65 6d 65 6e 74   fresh statement
2eb28 20 61 70 70 72 6f 70 72 69 61 74 65 20 74 6f 20   appropriate to 
2eb29 69 64 78 4e 75 6d 2e 20 2a 2f 0a 20 20 2f 2a 20  idxNum. */.  /* 
2eb2a 54 4f 44 4f 28 73 68 65 73 73 29 3a 20 41 64 64  TODO(shess): Add
2eb2b 20 61 20 70 72 65 70 61 72 65 64 2d 73 74 61 74   a prepared-stat
2eb2c 65 6d 65 6e 74 20 63 61 63 68 65 20 69 6e 20 74  ement cache in t
2eb2d 68 65 20 76 74 20 73 74 72 75 63 74 75 72 65 2e  he vt structure.
2eb2e 0a 20 20 2a 2a 20 54 68 65 20 63 61 63 68 65 20  .  ** The cache 
2eb2f 6d 75 73 74 20 68 61 6e 64 6c 65 20 6d 75 6c 74  must handle mult
2eb30 69 70 6c 65 20 6f 70 65 6e 20 63 75 72 73 6f 72  iple open cursor
2eb31 73 2e 20 20 45 61 73 69 65 72 20 74 6f 20 63 61  s.  Easier to ca
2eb32 63 68 65 20 74 68 65 0a 20 20 2a 2a 20 73 74 61  che the.  ** sta
2eb33 74 65 6d 65 6e 74 20 76 61 72 69 61 6e 74 73 20  tement variants 
2eb34 61 74 20 74 68 65 20 76 74 20 74 6f 20 72 65 64  at the vt to red
2eb35 75 63 65 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c  uce malloc/reall
2eb36 6f 63 2f 66 72 65 65 20 68 65 72 65 2e 0a 20 20  oc/free here..  
2eb37 2a 2a 20 4f 72 20 77 65 20 63 6f 75 6c 64 20 68  ** Or we could h
2eb38 61 76 65 20 61 20 53 74 72 69 6e 67 42 75 66 66  ave a StringBuff
2eb39 65 72 20 76 61 72 69 61 6e 74 20 77 68 69 63 68  er variant which
2eb3a 20 61 6c 6c 6f 77 65 64 20 73 74 61 63 6b 0a 20   allowed stack. 
2eb3b 20 2a 2a 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e   ** construction
2eb3c 20 66 6f 72 20 73 6d 61 6c 6c 20 76 61 6c 75 65   for small value
2eb3d 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 63  s..  */.  if( !c
2eb3e 2d 3e 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 53  ->pStmt ){.    S
2eb3f 74 72 69 6e 67 42 75 66 66 65 72 20 73 62 3b 0a  tringBuffer sb;.
2eb40 20 20 20 20 69 6e 69 74 53 74 72 69 6e 67 42 75      initStringBu
2eb41 66 66 65 72 28 26 73 62 29 3b 0a 20 20 20 20 61  ffer(&sb);.    a
2eb42 70 70 65 6e 64 28 26 73 62 2c 20 22 53 45 4c 45  ppend(&sb, "SELE
2eb43 43 54 20 64 6f 63 69 64 2c 20 22 29 3b 0a 20 20  CT docid, ");.  
2eb44 20 20 61 70 70 65 6e 64 4c 69 73 74 28 26 73 62    appendList(&sb
2eb45 2c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 76 2d  , v->nColumn, v-
2eb46 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e  >azContentColumn
2eb47 29 3b 0a 20 20 20 20 61 70 70 65 6e 64 28 26 73  );.    append(&s
2eb48 62 2c 20 22 20 46 52 4f 4d 20 25 5f 63 6f 6e 74  b, " FROM %_cont
2eb49 65 6e 74 22 29 3b 0a 20 20 20 20 69 66 28 20 69  ent");.    if( i
2eb4a 64 78 4e 75 6d 21 3d 51 55 45 52 59 5f 47 45 4e  dxNum!=QUERY_GEN
2eb4b 45 52 49 43 20 29 20 61 70 70 65 6e 64 28 26 73  ERIC ) append(&s
2eb4c 62 2c 20 22 20 57 48 45 52 45 20 64 6f 63 69 64  b, " WHERE docid
2eb4d 20 3d 20 3f 22 29 3b 0a 20 20 20 20 72 63 20 3d   = ?");.    rc =
2eb4e 20 73 71 6c 5f 70 72 65 70 61 72 65 28 76 2d 3e   sql_prepare(v->
2eb4f 64 62 2c 20 76 2d 3e 7a 44 62 2c 20 76 2d 3e 7a  db, v->zDb, v->z
2eb50 4e 61 6d 65 2c 20 26 63 2d 3e 70 53 74 6d 74 2c  Name, &c->pStmt,
2eb51 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2eb52 20 20 20 20 20 20 73 74 72 69 6e 67 42 75 66 66        stringBuff
2eb53 65 72 44 61 74 61 28 26 73 62 29 29 3b 0a 20 20  erData(&sb));.  
2eb54 20 20 73 74 72 69 6e 67 42 75 66 66 65 72 44 65    stringBufferDe
2eb55 73 74 72 6f 79 28 26 73 62 29 3b 0a 20 20 20 20  stroy(&sb);.    
2eb56 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2eb57 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
2eb58 20 20 20 63 2d 3e 69 43 75 72 73 6f 72 54 79 70     c->iCursorTyp
2eb59 65 20 3d 20 69 64 78 4e 75 6d 3b 0a 20 20 7d 65  e = idxNum;.  }e
2eb5a 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
2eb5b 5f 72 65 73 65 74 28 63 2d 3e 70 53 74 6d 74 29  _reset(c->pStmt)
2eb5c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 2d  ;.    assert( c-
2eb5d 3e 69 43 75 72 73 6f 72 54 79 70 65 3d 3d 69 64  >iCursorType==id
2eb5e 78 4e 75 6d 20 29 3b 0a 20 20 7d 0a 0a 20 20 73  xNum );.  }..  s
2eb5f 77 69 74 63 68 28 20 69 64 78 4e 75 6d 20 29 7b  witch( idxNum ){
2eb60 0a 20 20 20 20 63 61 73 65 20 51 55 45 52 59 5f  .    case QUERY_
2eb61 47 45 4e 45 52 49 43 3a 0a 20 20 20 20 20 20 62  GENERIC:.      b
2eb62 72 65 61 6b 3b 0a 0a 20 20 20 20 63 61 73 65 20  reak;..    case 
2eb63 51 55 45 52 59 5f 44 4f 43 49 44 3a 0a 20 20 20  QUERY_DOCID:.   
2eb64 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
2eb65 62 69 6e 64 5f 69 6e 74 36 34 28 63 2d 3e 70 53  bind_int64(c->pS
2eb66 74 6d 74 2c 20 31 2c 20 73 71 6c 69 74 65 33 5f  tmt, 1, sqlite3_
2eb67 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76  value_int64(argv
2eb68 5b 30 5d 29 29 3b 0a 20 20 20 20 20 20 69 66 28  [0]));.      if(
2eb69 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2eb6a 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
2eb6b 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 64 65    break;..    de
2eb6c 66 61 75 6c 74 3a 20 20 20 2f 2a 20 66 75 6c 6c  fault:   /* full
2eb6d 2d 74 65 78 74 20 73 65 61 72 63 68 20 2a 2f 0a  -text search */.
2eb6e 20 20 20 20 7b 0a 20 20 20 20 20 20 63 6f 6e 73      {.      cons
2eb6f 74 20 63 68 61 72 20 2a 7a 51 75 65 72 79 20 3d  t char *zQuery =
2eb70 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
2eb71 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
2eb72 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20  t(argv[0]);.    
2eb73 20 20 61 73 73 65 72 74 28 20 69 64 78 4e 75 6d    assert( idxNum
2eb74 3c 3d 51 55 45 52 59 5f 46 55 4c 4c 54 45 58 54  <=QUERY_FULLTEXT
2eb75 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20  +v->nColumn);.  
2eb76 20 20 20 20 61 73 73 65 72 74 28 20 61 72 67 63      assert( argc
2eb77 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 71 75 65  ==1 );.      que
2eb78 72 79 43 6c 65 61 72 28 26 63 2d 3e 71 29 3b 0a  ryClear(&c->q);.
2eb79 20 20 20 20 20 20 69 66 28 20 63 2d 3e 72 65 73        if( c->res
2eb7a 75 6c 74 2e 6e 44 61 74 61 21 3d 30 20 29 7b 0a  ult.nData!=0 ){.
2eb7b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
2eb7c 63 61 73 65 20 68 61 70 70 65 6e 73 20 69 66 20  case happens if 
2eb7d 74 68 65 20 73 61 6d 65 20 63 75 72 73 6f 72 20  the same cursor 
2eb7e 69 73 20 75 73 65 64 20 72 65 70 65 61 74 65 64  is used repeated
2eb7f 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 64  ly. */.        d
2eb80 6c 72 44 65 73 74 72 6f 79 28 26 63 2d 3e 72 65  lrDestroy(&c->re
2eb81 61 64 65 72 29 3b 0a 20 20 20 20 20 20 20 20 64  ader);.        d
2eb82 61 74 61 42 75 66 66 65 72 52 65 73 65 74 28 26  ataBufferReset(&
2eb83 63 2d 3e 72 65 73 75 6c 74 29 3b 0a 20 20 20 20  c->result);.    
2eb84 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2eb85 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28   dataBufferInit(
2eb86 26 63 2d 3e 72 65 73 75 6c 74 2c 20 30 29 3b 0a  &c->result, 0);.
2eb87 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63        }.      rc
2eb88 20 3d 20 66 75 6c 6c 74 65 78 74 51 75 65 72 79   = fulltextQuery
2eb89 28 76 2c 20 69 64 78 4e 75 6d 2d 51 55 45 52 59  (v, idxNum-QUERY
2eb8a 5f 46 55 4c 4c 54 45 58 54 2c 20 7a 51 75 65 72  _FULLTEXT, zQuer
2eb8b 79 2c 20 2d 31 2c 20 26 63 2d 3e 72 65 73 75 6c  y, -1, &c->resul
2eb8c 74 2c 20 26 63 2d 3e 71 29 3b 0a 20 20 20 20 20  t, &c->q);.     
2eb8d 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2eb8e 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
2eb8f 20 20 20 20 20 20 69 66 28 20 63 2d 3e 72 65 73        if( c->res
2eb90 75 6c 74 2e 6e 44 61 74 61 21 3d 30 20 29 7b 0a  ult.nData!=0 ){.
2eb91 20 20 20 20 20 20 20 20 64 6c 72 49 6e 69 74 28          dlrInit(
2eb92 26 63 2d 3e 72 65 61 64 65 72 2c 20 44 4c 5f 44  &c->reader, DL_D
2eb93 4f 43 49 44 53 2c 20 63 2d 3e 72 65 73 75 6c 74  OCIDS, c->result
2eb94 2e 70 44 61 74 61 2c 20 63 2d 3e 72 65 73 75 6c  .pData, c->resul
2eb95 74 2e 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 20  t.nData);.      
2eb96 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
2eb97 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
2eb98 72 6e 20 66 75 6c 6c 74 65 78 74 4e 65 78 74 28  rn fulltextNext(
2eb99 70 43 75 72 73 6f 72 29 3b 0a 7d 0a 0a 2f 2a 20  pCursor);.}../* 
2eb9a 54 68 69 73 20 69 73 20 74 68 65 20 78 45 6f 66  This is the xEof
2eb9b 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 76   method of the v
2eb9c 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54  irtual table.  T
2eb9d 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 0a 2a  he SQLite core.*
2eb9e 2a 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  * calls this rou
2eb9f 74 69 6e 65 20 74 6f 20 66 69 6e 64 20 6f 75 74  tine to find out
2eba0 20 69 66 20 69 74 20 68 61 73 20 72 65 61 63 68   if it has reach
2eba1 65 64 20 74 68 65 20 65 6e 64 20 6f 66 0a 2a 2a  ed the end of.**
2eba2 20 61 20 71 75 65 72 79 27 73 20 72 65 73 75 6c   a query's resul
2eba3 74 73 20 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69  ts set..*/.stati
2eba4 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 45 6f  c int fulltextEo
2eba5 66 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  f(sqlite3_vtab_c
2eba6 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b  ursor *pCursor){
2eba7 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  .  fulltext_curs
2eba8 6f 72 20 2a 63 20 3d 20 28 66 75 6c 6c 74 65 78  or *c = (fulltex
2eba9 74 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72  t_cursor *) pCur
2ebaa 73 6f 72 3b 0a 20 20 72 65 74 75 72 6e 20 63 2d  sor;.  return c-
2ebab 3e 65 6f 66 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73  >eof;.}../* This
2ebac 20 69 73 20 74 68 65 20 78 43 6f 6c 75 6d 6e 20   is the xColumn 
2ebad 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 76 69  method of the vi
2ebae 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68  rtual table.  Th
2ebaf 65 20 53 51 4c 69 74 65 0a 2a 2a 20 63 6f 72 65  e SQLite.** core
2ebb0 20 63 61 6c 6c 73 20 74 68 69 73 20 6d 65 74 68   calls this meth
2ebb1 6f 64 20 64 75 72 69 6e 67 20 61 20 71 75 65 72  od during a quer
2ebb2 79 20 77 68 65 6e 20 69 74 20 6e 65 65 64 73 20  y when it needs 
2ebb3 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20  the value.** of 
2ebb4 61 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 74 68  a column from th
2ebb5 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
2ebb6 20 20 54 68 69 73 20 6d 65 74 68 6f 64 20 6e 65    This method ne
2ebb7 65 64 73 20 74 6f 20 75 73 65 0a 2a 2a 20 6f 6e  eds to use.** on
2ebb8 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  e of the sqlite3
2ebb9 5f 72 65 73 75 6c 74 5f 2a 28 29 20 72 6f 75 74  _result_*() rout
2ebba 69 6e 65 73 20 74 6f 20 73 74 6f 72 65 20 74 68  ines to store th
2ebbb 65 20 72 65 71 75 65 73 74 65 64 0a 2a 2a 20 76  e requested.** v
2ebbc 61 6c 75 65 20 62 61 63 6b 20 69 6e 20 74 68 65  alue back in the
2ebbd 20 70 43 6f 6e 74 65 78 74 2e 0a 2a 2f 0a 73 74   pContext..*/.st
2ebbe 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78  atic int fulltex
2ebbf 74 43 6f 6c 75 6d 6e 28 73 71 6c 69 74 65 33 5f  tColumn(sqlite3_
2ebc0 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75  vtab_cursor *pCu
2ebc1 72 73 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20  rsor,.          
2ebc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ebc3 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
2ebc4 2a 70 43 6f 6e 74 65 78 74 2c 20 69 6e 74 20 69  *pContext, int i
2ebc5 64 78 43 6f 6c 29 7b 0a 20 20 66 75 6c 6c 74 65  dxCol){.  fullte
2ebc6 78 74 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28  xt_cursor *c = (
2ebc7 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20  fulltext_cursor 
2ebc8 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 66 75  *) pCursor;.  fu
2ebc9 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d  lltext_vtab *v =
2ebca 20 63 75 72 73 6f 72 5f 76 74 61 62 28 63 29 3b   cursor_vtab(c);
2ebcb 0a 0a 20 20 69 66 28 20 69 64 78 43 6f 6c 3c 76  ..  if( idxCol<v
2ebcc 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ->nColumn ){.   
2ebcd 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2ebce 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 63  pVal = sqlite3_c
2ebcf 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 63 2d 3e 70  olumn_value(c->p
2ebd0 53 74 6d 74 2c 20 69 64 78 43 6f 6c 2b 31 29 3b  Stmt, idxCol+1);
2ebd1 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
2ebd2 75 6c 74 5f 76 61 6c 75 65 28 70 43 6f 6e 74 65  ult_value(pConte
2ebd3 78 74 2c 20 70 56 61 6c 29 3b 0a 20 20 7d 65 6c  xt, pVal);.  }el
2ebd4 73 65 20 69 66 28 20 69 64 78 43 6f 6c 3d 3d 76  se if( idxCol==v
2ebd5 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ->nColumn ){.   
2ebd6 20 2f 2a 20 54 68 65 20 65 78 74 72 61 20 63 6f   /* The extra co
2ebd7 6c 75 6d 6e 20 77 68 6f 73 65 20 6e 61 6d 65 20  lumn whose name 
2ebd8 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  is the same as t
2ebd9 68 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a  he table..    **
2ebda 20 52 65 74 75 72 6e 20 61 20 62 6c 6f 62 20 77   Return a blob w
2ebdb 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65  hich is a pointe
2ebdc 72 20 74 6f 20 74 68 65 20 63 75 72 73 6f 72 0a  r to the cursor.
2ebdd 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
2ebde 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 70  e3_result_blob(p
2ebdf 43 6f 6e 74 65 78 74 2c 20 26 63 2c 20 73 69 7a  Context, &c, siz
2ebe0 65 6f 66 28 63 29 2c 20 53 51 4c 49 54 45 5f 54  eof(c), SQLITE_T
2ebe1 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 65 6c  RANSIENT);.  }el
2ebe2 73 65 20 69 66 28 20 69 64 78 43 6f 6c 3d 3d 76  se if( idxCol==v
2ebe3 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 20 29 7b 0a 20  ->nColumn+1 ){. 
2ebe4 20 20 20 2f 2a 20 54 68 65 20 64 6f 63 69 64 20     /* The docid 
2ebe5 63 6f 6c 75 6d 6e 2c 20 77 68 69 63 68 20 69 73  column, which is
2ebe6 20 61 6e 20 61 6c 69 61 73 20 66 6f 72 20 72 6f   an alias for ro
2ebe7 77 69 64 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69  wid. */.    sqli
2ebe8 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20  te3_value *pVal 
2ebe9 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
2ebea 5f 76 61 6c 75 65 28 63 2d 3e 70 53 74 6d 74 2c  _value(c->pStmt,
2ebeb 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
2ebec 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 70 43  _result_value(pC
2ebed 6f 6e 74 65 78 74 2c 20 70 56 61 6c 29 3b 0a 20  ontext, pVal);. 
2ebee 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
2ebef 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 68 69  TE_OK;.}../* Thi
2ebf0 73 20 69 73 20 74 68 65 20 78 52 6f 77 69 64 20  s is the xRowid 
2ebf1 6d 65 74 68 6f 64 2e 20 20 54 68 65 20 53 51 4c  method.  The SQL
2ebf2 69 74 65 20 63 6f 72 65 20 63 61 6c 6c 73 20 74  ite core calls t
2ebf3 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 0a 2a  his routine to.*
2ebf4 2a 20 72 65 74 72 69 65 76 65 20 74 68 65 20 72  * retrieve the r
2ebf5 6f 77 69 64 20 66 6f 72 20 74 68 65 20 63 75 72  owid for the cur
2ebf6 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20  rent row of the 
2ebf7 72 65 73 75 6c 74 20 73 65 74 2e 20 20 66 74 73  result set.  fts
2ebf8 33 0a 2a 2a 20 65 78 70 6f 73 65 73 20 25 5f 63  3.** exposes %_c
2ebf9 6f 6e 74 65 6e 74 2e 64 6f 63 69 64 20 61 73 20  ontent.docid as 
2ebfa 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68  the rowid for th
2ebfb 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
2ebfc 20 20 54 68 65 0a 2a 2a 20 72 6f 77 69 64 20 73    The.** rowid s
2ebfd 68 6f 75 6c 64 20 62 65 20 77 72 69 74 74 65 6e  hould be written
2ebfe 20 74 6f 20 2a 70 52 6f 77 69 64 2e 0a 2a 2f 0a   to *pRowid..*/.
2ebff 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74  static int fullt
2ec00 65 78 74 52 6f 77 69 64 28 73 71 6c 69 74 65 33  extRowid(sqlite3
2ec01 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43  _vtab_cursor *pC
2ec02 75 72 73 6f 72 2c 20 73 71 6c 69 74 65 5f 69 6e  ursor, sqlite_in
2ec03 74 36 34 20 2a 70 52 6f 77 69 64 29 7b 0a 20 20  t64 *pRowid){.  
2ec04 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20  fulltext_cursor 
2ec05 2a 63 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 63  *c = (fulltext_c
2ec06 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72  ursor *) pCursor
2ec07 3b 0a 0a 20 20 2a 70 52 6f 77 69 64 20 3d 20 73  ;..  *pRowid = s
2ec08 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
2ec09 74 36 34 28 63 2d 3e 70 53 74 6d 74 2c 20 30 29  t64(c->pStmt, 0)
2ec0a 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
2ec0b 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20  E_OK;.}../* Add 
2ec0c 61 6c 6c 20 74 65 72 6d 73 20 69 6e 20 5b 7a 54  all terms in [zT
2ec0d 65 78 74 5d 20 74 6f 20 70 65 6e 64 69 6e 67 54  ext] to pendingT
2ec0e 65 72 6d 73 20 74 61 62 6c 65 2e 20 20 49 66 20  erms table.  If 
2ec0f 5b 69 43 6f 6c 75 6d 6e 5d 20 3e 20 30 2c 0a 2a  [iColumn] > 0,.*
2ec10 2a 20 77 65 20 61 6c 73 6f 20 73 74 6f 72 65 20  * we also store 
2ec11 70 6f 73 69 74 69 6f 6e 73 20 61 6e 64 20 6f 66  positions and of
2ec12 66 73 65 74 73 20 69 6e 20 74 68 65 20 68 61 73  fsets in the has
2ec13 68 20 74 61 62 6c 65 20 75 73 69 6e 67 20 74 68  h table using th
2ec14 61 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d  at.** column num
2ec15 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ber..*/.static i
2ec16 6e 74 20 62 75 69 6c 64 54 65 72 6d 73 28 66 75  nt buildTerms(fu
2ec17 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
2ec18 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f  sqlite_int64 iDo
2ec19 63 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20  cid,.           
2ec1a 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
2ec1b 20 63 68 61 72 20 2a 7a 54 65 78 74 2c 20 69 6e   char *zText, in
2ec1c 74 20 69 43 6f 6c 75 6d 6e 29 7b 0a 20 20 73 71  t iColumn){.  sq
2ec1d 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
2ec1e 2a 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 76 2d  *pTokenizer = v-
2ec1f 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73  >pTokenizer;.  s
2ec20 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
2ec21 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
2ec22 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
2ec23 70 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74 20 6e 54  pToken;.  int nT
2ec24 6f 6b 65 6e 42 79 74 65 73 3b 0a 20 20 69 6e 74  okenBytes;.  int
2ec25 20 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 69   iStartOffset, i
2ec26 45 6e 64 4f 66 66 73 65 74 2c 20 69 50 6f 73 69  EndOffset, iPosi
2ec27 74 69 6f 6e 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  tion;.  int rc;.
2ec28 0a 20 20 72 63 20 3d 20 70 54 6f 6b 65 6e 69 7a  .  rc = pTokeniz
2ec29 65 72 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70  er->pModule->xOp
2ec2a 65 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a  en(pTokenizer, z
2ec2b 54 65 78 74 2c 20 2d 31 2c 20 26 70 43 75 72 73  Text, -1, &pCurs
2ec2c 6f 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  or);.  if( rc!=S
2ec2d 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
2ec2e 6e 20 72 63 3b 0a 0a 20 20 70 43 75 72 73 6f 72  n rc;..  pCursor
2ec2f 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70  ->pTokenizer = p
2ec30 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 77 68 69  Tokenizer;.  whi
2ec31 6c 65 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28  le( SQLITE_OK==(
2ec32 72 63 3d 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70  rc=pTokenizer->p
2ec33 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43  Module->xNext(pC
2ec34 75 72 73 6f 72 2c 0a 20 20 20 20 20 20 20 20 20  ursor,.         
2ec35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec37 20 20 20 20 20 20 20 20 20 20 26 70 54 6f 6b 65            &pToke
2ec38 6e 2c 20 26 6e 54 6f 6b 65 6e 42 79 74 65 73 2c  n, &nTokenBytes,
2ec39 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2ec3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec3c 20 20 20 20 26 69 53 74 61 72 74 4f 66 66 73 65      &iStartOffse
2ec3d 74 2c 20 26 69 45 6e 64 4f 66 66 73 65 74 2c 0a  t, &iEndOffset,.
2ec3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec3f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec41 20 20 20 26 69 50 6f 73 69 74 69 6f 6e 29 29 20     &iPosition)) 
2ec42 29 7b 0a 20 20 20 20 44 4c 43 6f 6c 6c 65 63 74  ){.    DLCollect
2ec43 6f 72 20 2a 70 3b 0a 20 20 20 20 69 6e 74 20 6e  or *p;.    int n
2ec44 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  Data;           
2ec45 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
2ec46 6f 66 20 64 6f 63 6c 69 73 74 20 62 65 66 6f 72  of doclist befor
2ec47 65 20 6f 75 72 20 75 70 64 61 74 65 2e 20 2a 2f  e our update. */
2ec48 0a 0a 20 20 20 20 2f 2a 20 50 6f 73 69 74 69 6f  ..    /* Positio
2ec49 6e 73 20 63 61 6e 27 74 20 62 65 20 6e 65 67 61  ns can't be nega
2ec4a 74 69 76 65 3b 20 77 65 20 75 73 65 20 2d 31 20  tive; we use -1 
2ec4b 61 73 20 61 20 74 65 72 6d 69 6e 61 74 6f 72 0a  as a terminator.
2ec4c 20 20 20 20 20 2a 20 69 6e 74 65 72 6e 61 6c 6c       * internall
2ec4d 79 2e 20 20 54 6f 6b 65 6e 20 63 61 6e 27 74 20  y.  Token can't 
2ec4e 62 65 20 4e 55 4c 4c 20 6f 72 20 65 6d 70 74 79  be NULL or empty
2ec4f 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 50 6f  . */.    if( iPo
2ec50 73 69 74 69 6f 6e 3c 30 20 7c 7c 20 70 54 6f 6b  sition<0 || pTok
2ec51 65 6e 20 3d 3d 20 4e 55 4c 4c 20 7c 7c 20 6e 54  en == NULL || nT
2ec52 6f 6b 65 6e 42 79 74 65 73 20 3d 3d 20 30 20 29  okenBytes == 0 )
2ec53 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
2ec54 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
2ec55 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
2ec56 20 20 20 70 20 3d 20 66 74 73 33 48 61 73 68 46     p = fts3HashF
2ec57 69 6e 64 28 26 76 2d 3e 70 65 6e 64 69 6e 67 54  ind(&v->pendingT
2ec58 65 72 6d 73 2c 20 70 54 6f 6b 65 6e 2c 20 6e 54  erms, pToken, nT
2ec59 6f 6b 65 6e 42 79 74 65 73 29 3b 0a 20 20 20 20  okenBytes);.    
2ec5a 69 66 28 20 70 3d 3d 4e 55 4c 4c 20 29 7b 0a 20  if( p==NULL ){. 
2ec5b 20 20 20 20 20 6e 44 61 74 61 20 3d 20 30 3b 0a       nData = 0;.
2ec5c 20 20 20 20 20 20 70 20 3d 20 64 6c 63 4e 65 77        p = dlcNew
2ec5d 28 69 44 6f 63 69 64 2c 20 44 4c 5f 44 45 46 41  (iDocid, DL_DEFA
2ec5e 55 4c 54 29 3b 0a 20 20 20 20 20 20 66 74 73 33  ULT);.      fts3
2ec5f 48 61 73 68 49 6e 73 65 72 74 28 26 76 2d 3e 70  HashInsert(&v->p
2ec60 65 6e 64 69 6e 67 54 65 72 6d 73 2c 20 70 54 6f  endingTerms, pTo
2ec61 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 42 79 74 65 73  ken, nTokenBytes
2ec62 2c 20 70 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  , p);..      /* 
2ec63 4f 76 65 72 68 65 61 64 20 66 6f 72 20 6f 75 72  Overhead for our
2ec64 20 68 61 73 68 20 74 61 62 6c 65 20 65 6e 74 72   hash table entr
2ec65 79 2c 20 74 68 65 20 6b 65 79 2c 20 61 6e 64 20  y, the key, and 
2ec66 74 68 65 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20  the value. */.  
2ec67 20 20 20 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44      v->nPendingD
2ec68 61 74 61 20 2b 3d 20 73 69 7a 65 6f 66 28 73 74  ata += sizeof(st
2ec69 72 75 63 74 20 66 74 73 33 48 61 73 68 45 6c 65  ruct fts3HashEle
2ec6a 6d 29 2b 73 69 7a 65 6f 66 28 2a 70 29 2b 6e 54  m)+sizeof(*p)+nT
2ec6b 6f 6b 65 6e 42 79 74 65 73 3b 0a 20 20 20 20 7d  okenBytes;.    }
2ec6c 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 44 61 74  else{.      nDat
2ec6d 61 20 3d 20 70 2d 3e 62 2e 6e 44 61 74 61 3b 0a  a = p->b.nData;.
2ec6e 20 20 20 20 20 20 69 66 28 20 70 2d 3e 64 6c 77        if( p->dlw
2ec6f 2e 69 50 72 65 76 44 6f 63 69 64 21 3d 69 44 6f  .iPrevDocid!=iDo
2ec70 63 69 64 20 29 20 64 6c 63 4e 65 78 74 28 70 2c  cid ) dlcNext(p,
2ec71 20 69 44 6f 63 69 64 29 3b 0a 20 20 20 20 7d 0a   iDocid);.    }.
2ec72 20 20 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3e      if( iColumn>
2ec73 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 6c 63 41  =0 ){.      dlcA
2ec74 64 64 50 6f 73 28 70 2c 20 69 43 6f 6c 75 6d 6e  ddPos(p, iColumn
2ec75 2c 20 69 50 6f 73 69 74 69 6f 6e 2c 20 69 53 74  , iPosition, iSt
2ec76 61 72 74 4f 66 66 73 65 74 2c 20 69 45 6e 64 4f  artOffset, iEndO
2ec77 66 66 73 65 74 29 3b 0a 20 20 20 20 7d 0a 0a 20  ffset);.    }.. 
2ec78 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65     /* Accumulate
2ec79 20 64 61 74 61 20 61 64 64 65 64 20 62 79 20 64   data added by d
2ec7a 6c 63 4e 65 77 20 6f 72 20 64 6c 63 4e 65 78 74  lcNew or dlcNext
2ec7b 2c 20 61 6e 64 20 64 6c 63 41 64 64 50 6f 73 2e  , and dlcAddPos.
2ec7c 20 2a 2f 0a 20 20 20 20 76 2d 3e 6e 50 65 6e 64   */.    v->nPend
2ec7d 69 6e 67 44 61 74 61 20 2b 3d 20 70 2d 3e 62 2e  ingData += p->b.
2ec7e 6e 44 61 74 61 2d 6e 44 61 74 61 3b 0a 20 20 7d  nData-nData;.  }
2ec7f 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73  ..  /* TODO(shes
2ec80 73 29 20 43 68 65 63 6b 20 72 65 74 75 72 6e 3f  s) Check return?
2ec81 20 20 53 68 6f 75 6c 64 20 74 68 69 73 20 62 65    Should this be
2ec82 20 61 62 6c 65 20 74 6f 20 63 61 75 73 65 20 65   able to cause e
2ec83 72 72 6f 72 73 20 61 74 0a 20 20 2a 2a 20 74 68  rrors at.  ** th
2ec84 69 73 20 70 6f 69 6e 74 3f 20 20 41 63 74 75 61  is point?  Actua
2ec85 6c 6c 79 2c 20 73 61 6d 65 20 71 75 65 73 74 69  lly, same questi
2ec86 6f 6e 20 61 62 6f 75 74 20 73 71 6c 69 74 65 33  on about sqlite3
2ec87 5f 66 69 6e 61 6c 69 7a 65 28 29 2c 0a 20 20 2a  _finalize(),.  *
2ec88 2a 20 74 68 6f 75 67 68 20 6f 6e 65 20 63 6f 75  * though one cou
2ec89 6c 64 20 61 72 67 75 65 20 74 68 61 74 20 66 61  ld argue that fa
2ec8a 69 6c 75 72 65 20 74 68 65 72 65 20 6d 65 61 6e  ilure there mean
2ec8b 73 20 74 68 61 74 20 74 68 65 20 64 61 74 61 20  s that the data 
2ec8c 69 73 0a 20 20 2a 2a 20 6e 6f 74 20 64 75 72 61  is.  ** not dura
2ec8d 62 6c 65 2e 20 20 2a 70 6f 6e 64 65 72 2a 0a 20  ble.  *ponder*. 
2ec8e 20 2a 2f 0a 20 20 70 54 6f 6b 65 6e 69 7a 65 72   */.  pTokenizer
2ec8f 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73  ->pModule->xClos
2ec90 65 28 70 43 75 72 73 6f 72 29 3b 0a 20 20 69 66  e(pCursor);.  if
2ec91 28 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3d 3d  ( SQLITE_DONE ==
2ec92 20 72 63 20 29 20 72 65 74 75 72 6e 20 53 51 4c   rc ) return SQL
2ec93 49 54 45 5f 4f 4b 3b 0a 20 20 72 65 74 75 72 6e  ITE_OK;.  return
2ec94 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20 64   rc;.}../* Add d
2ec95 6f 63 6c 69 73 74 73 20 66 6f 72 20 61 6c 6c 20  oclists for all 
2ec96 74 65 72 6d 73 20 69 6e 20 5b 70 56 61 6c 75 65  terms in [pValue
2ec97 73 5d 20 74 6f 20 70 65 6e 64 69 6e 67 54 65 72  s] to pendingTer
2ec98 6d 73 20 74 61 62 6c 65 2e 20 2a 2f 0a 73 74 61  ms table. */.sta
2ec99 74 69 63 20 69 6e 74 20 69 6e 73 65 72 74 54 65  tic int insertTe
2ec9a 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  rms(fulltext_vta
2ec9b 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74  b *v, sqlite_int
2ec9c 36 34 20 69 44 6f 63 69 64 2c 0a 20 20 20 20 20  64 iDocid,.     
2ec9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec9e 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
2ec9f 2a 2a 70 56 61 6c 75 65 73 29 7b 0a 20 20 69 6e  **pValues){.  in
2eca0 74 20 69 3b 0a 20 20 66 6f 72 28 69 20 3d 20 30  t i;.  for(i = 0
2eca1 3b 20 69 20 3c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e  ; i < v->nColumn
2eca2 20 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 63 68 61   ; ++i){.    cha
2eca3 72 20 2a 7a 54 65 78 74 20 3d 20 28 63 68 61 72  r *zText = (char
2eca4 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
2eca5 74 65 78 74 28 70 56 61 6c 75 65 73 5b 69 5d 29  text(pValues[i])
2eca6 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 62  ;.    int rc = b
2eca7 75 69 6c 64 54 65 72 6d 73 28 76 2c 20 69 44 6f  uildTerms(v, iDo
2eca8 63 69 64 2c 20 7a 54 65 78 74 2c 20 69 29 3b 0a  cid, zText, i);.
2eca9 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
2ecaa 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
2ecab 63 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  c;.  }.  return 
2ecac 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
2ecad 20 41 64 64 20 65 6d 70 74 79 20 64 6f 63 6c 69   Add empty docli
2ecae 73 74 73 20 66 6f 72 20 61 6c 6c 20 74 65 72 6d  sts for all term
2ecaf 73 20 69 6e 20 74 68 65 20 67 69 76 65 6e 20 72  s in the given r
2ecb0 6f 77 27 73 20 63 6f 6e 74 65 6e 74 20 74 6f 0a  ow's content to.
2ecb1 2a 2a 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 2e  ** pendingTerms.
2ecb2 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .*/.static int d
2ecb3 65 6c 65 74 65 54 65 72 6d 73 28 66 75 6c 6c 74  eleteTerms(fullt
2ecb4 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c  ext_vtab *v, sql
2ecb5 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64  ite_int64 iDocid
2ecb6 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
2ecb7 2a 2a 70 56 61 6c 75 65 73 3b 0a 20 20 69 6e 74  **pValues;.  int
2ecb8 20 69 2c 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 4f   i, rc;..  /* TO
2ecb9 44 4f 28 73 68 65 73 73 29 20 53 68 6f 75 6c 64  DO(shess) Should
2ecba 20 77 65 20 61 6c 6c 6f 77 20 73 75 63 68 20 74   we allow such t
2ecbb 61 62 6c 65 73 20 61 74 20 61 6c 6c 3f 20 2a 2f  ables at all? */
2ecbc 0a 20 20 69 66 28 20 44 4c 5f 44 45 46 41 55 4c  .  if( DL_DEFAUL
2ecbd 54 3d 3d 44 4c 5f 44 4f 43 49 44 53 20 29 20 72  T==DL_DOCIDS ) r
2ecbe 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
2ecbf 4f 52 3b 0a 0a 20 20 72 63 20 3d 20 63 6f 6e 74  OR;..  rc = cont
2ecc0 65 6e 74 5f 73 65 6c 65 63 74 28 76 2c 20 69 44  ent_select(v, iD
2ecc1 6f 63 69 64 2c 20 26 70 56 61 6c 75 65 73 29 3b  ocid, &pValues);
2ecc2 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
2ecc3 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
2ecc4 3b 0a 0a 20 20 66 6f 72 28 69 20 3d 20 30 20 3b  ;..  for(i = 0 ;
2ecc5 20 69 20 3c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b   i < v->nColumn;
2ecc6 20 2b 2b 69 29 20 7b 0a 20 20 20 20 72 63 20 3d   ++i) {.    rc =
2ecc7 20 62 75 69 6c 64 54 65 72 6d 73 28 76 2c 20 69   buildTerms(v, i
2ecc8 44 6f 63 69 64 2c 20 70 56 61 6c 75 65 73 5b 69  Docid, pValues[i
2ecc9 5d 2c 20 2d 31 29 3b 0a 20 20 20 20 69 66 28 20  ], -1);.    if( 
2ecca 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2eccb 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 66 72  break;.  }..  fr
2eccc 65 65 53 74 72 69 6e 67 41 72 72 61 79 28 76 2d  eeStringArray(v-
2eccd 3e 6e 43 6f 6c 75 6d 6e 2c 20 70 56 61 6c 75 65  >nColumn, pValue
2ecce 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  s);.  return SQL
2eccf 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 4f  ITE_OK;.}../* TO
2ecd0 44 4f 28 73 68 65 73 73 29 20 52 65 66 61 63 74  DO(shess) Refact
2ecd1 6f 72 20 74 68 65 20 63 6f 64 65 20 74 6f 20 72  or the code to r
2ecd2 65 6d 6f 76 65 20 74 68 69 73 20 66 6f 72 77 61  emove this forwa
2ecd3 72 64 20 64 65 63 6c 2e 20 2a 2f 0a 73 74 61 74  rd decl. */.stat
2ecd4 69 63 20 69 6e 74 20 69 6e 69 74 50 65 6e 64 69  ic int initPendi
2ecd5 6e 67 54 65 72 6d 73 28 66 75 6c 6c 74 65 78 74  ngTerms(fulltext
2ecd6 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65  _vtab *v, sqlite
2ecd7 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 29 3b 0a  _int64 iDocid);.
2ecd8 0a 2f 2a 20 49 6e 73 65 72 74 20 61 20 72 6f 77  ./* Insert a row
2ecd9 20 69 6e 74 6f 20 74 68 65 20 25 5f 63 6f 6e 74   into the %_cont
2ecda 65 6e 74 20 74 61 62 6c 65 3b 20 73 65 74 20 2a  ent table; set *
2ecdb 70 69 44 6f 63 69 64 20 74 6f 20 62 65 20 74 68  piDocid to be th
2ecdc 65 20 49 44 20 6f 66 20 74 68 65 0a 2a 2a 20 6e  e ID of the.** n
2ecdd 65 77 20 72 6f 77 2e 20 20 41 64 64 20 64 6f 63  ew row.  Add doc
2ecde 6c 69 73 74 73 20 66 6f 72 20 74 65 72 6d 73 20  lists for terms 
2ecdf 74 6f 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 2e  to pendingTerms.
2ece0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
2ece1 6e 64 65 78 5f 69 6e 73 65 72 74 28 66 75 6c 6c  ndex_insert(full
2ece2 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71  text_vtab *v, sq
2ece3 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 52 65  lite3_value *pRe
2ece4 71 75 65 73 74 44 6f 63 69 64 2c 0a 20 20 20 20  questDocid,.    
2ece5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ece6 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75      sqlite3_valu
2ece7 65 20 2a 2a 70 56 61 6c 75 65 73 2c 20 73 71 6c  e **pValues, sql
2ece8 69 74 65 5f 69 6e 74 36 34 20 2a 70 69 44 6f 63  ite_int64 *piDoc
2ece9 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  id){.  int rc;..
2ecea 20 20 72 63 20 3d 20 63 6f 6e 74 65 6e 74 5f 69    rc = content_i
2eceb 6e 73 65 72 74 28 76 2c 20 70 52 65 71 75 65 73  nsert(v, pReques
2ecec 74 44 6f 63 69 64 2c 20 70 56 61 6c 75 65 73 29  tDocid, pValues)
2eced 3b 20 20 2f 2a 20 65 78 65 63 75 74 65 20 61 6e  ;  /* execute an
2ecee 20 53 51 4c 20 49 4e 53 45 52 54 20 2a 2f 0a 20   SQL INSERT */. 
2ecef 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2ecf0 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
2ecf1 0a 20 20 2f 2a 20 64 6f 63 69 64 20 63 6f 6c 75  .  /* docid colu
2ecf2 6d 6e 20 69 73 20 61 6e 20 61 6c 69 61 73 20 66  mn is an alias f
2ecf3 6f 72 20 72 6f 77 69 64 2e 20 2a 2f 0a 20 20 2a  or rowid. */.  *
2ecf4 70 69 44 6f 63 69 64 20 3d 20 73 71 6c 69 74 65  piDocid = sqlite
2ecf5 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
2ecf6 77 69 64 28 76 2d 3e 64 62 29 3b 0a 20 20 72 63  wid(v->db);.  rc
2ecf7 20 3d 20 69 6e 69 74 50 65 6e 64 69 6e 67 54 65   = initPendingTe
2ecf8 72 6d 73 28 76 2c 20 2a 70 69 44 6f 63 69 64 29  rms(v, *piDocid)
2ecf9 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
2ecfa 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
2ecfb 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 69 6e 73  c;..  return ins
2ecfc 65 72 74 54 65 72 6d 73 28 76 2c 20 2a 70 69 44  ertTerms(v, *piD
2ecfd 6f 63 69 64 2c 20 70 56 61 6c 75 65 73 29 3b 0a  ocid, pValues);.
2ecfe 7d 0a 0a 2f 2a 20 44 65 6c 65 74 65 20 61 20 72  }../* Delete a r
2ecff 6f 77 20 66 72 6f 6d 20 74 68 65 20 25 5f 63 6f  ow from the %_co
2ed00 6e 74 65 6e 74 20 74 61 62 6c 65 3b 20 61 64 64  ntent table; add
2ed01 20 65 6d 70 74 79 20 64 6f 63 6c 69 73 74 73 20   empty doclists 
2ed02 66 6f 72 20 74 65 72 6d 73 0a 2a 2a 20 74 6f 20  for terms.** to 
2ed03 70 65 6e 64 69 6e 67 54 65 72 6d 73 2e 0a 2a 2f  pendingTerms..*/
2ed04 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 64 65  .static int inde
2ed05 78 5f 64 65 6c 65 74 65 28 66 75 6c 6c 74 65 78  x_delete(fulltex
2ed06 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74  t_vtab *v, sqlit
2ed07 65 5f 69 6e 74 36 34 20 69 52 6f 77 29 7b 0a 20  e_int64 iRow){. 
2ed08 20 69 6e 74 20 72 63 20 3d 20 69 6e 69 74 50 65   int rc = initPe
2ed09 6e 64 69 6e 67 54 65 72 6d 73 28 76 2c 20 69 52  ndingTerms(v, iR
2ed0a 6f 77 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  ow);.  if( rc!=S
2ed0b 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
2ed0c 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 64 65  n rc;..  rc = de
2ed0d 6c 65 74 65 54 65 72 6d 73 28 76 2c 20 69 52 6f  leteTerms(v, iRo
2ed0e 77 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  w);.  if( rc!=SQ
2ed0f 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
2ed10 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 63   rc;..  return c
2ed11 6f 6e 74 65 6e 74 5f 64 65 6c 65 74 65 28 76 2c  ontent_delete(v,
2ed12 20 69 52 6f 77 29 3b 20 20 2f 2a 20 65 78 65 63   iRow);  /* exec
2ed13 75 74 65 20 61 6e 20 53 51 4c 20 44 45 4c 45 54  ute an SQL DELET
2ed14 45 20 2a 2f 0a 7d 0a 0a 2f 2a 20 55 70 64 61 74  E */.}../* Updat
2ed15 65 20 61 20 72 6f 77 20 69 6e 20 74 68 65 20 25  e a row in the %
2ed16 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 3b 20  _content table; 
2ed17 61 64 64 20 64 65 6c 65 74 65 20 64 6f 63 6c 69  add delete docli
2ed18 73 74 73 20 74 6f 0a 2a 2a 20 70 65 6e 64 69 6e  sts to.** pendin
2ed19 67 54 65 72 6d 73 20 66 6f 72 20 6f 6c 64 20 74  gTerms for old t
2ed1a 65 72 6d 73 20 6e 6f 74 20 69 6e 20 74 68 65 20  erms not in the 
2ed1b 6e 65 77 20 64 61 74 61 2c 20 61 64 64 20 69 6e  new data, add in
2ed1c 73 65 72 74 20 64 6f 63 6c 69 73 74 73 0a 2a 2a  sert doclists.**
2ed1d 20 74 6f 20 70 65 6e 64 69 6e 67 54 65 72 6d 73   to pendingTerms
2ed1e 20 66 6f 72 20 74 65 72 6d 73 20 69 6e 20 74 68   for terms in th
2ed1f 65 20 6e 65 77 20 64 61 74 61 2e 0a 2a 2f 0a 73  e new data..*/.s
2ed20 74 61 74 69 63 20 69 6e 74 20 69 6e 64 65 78 5f  tatic int index_
2ed21 75 70 64 61 74 65 28 66 75 6c 6c 74 65 78 74 5f  update(fulltext_
2ed22 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f  vtab *v, sqlite_
2ed23 69 6e 74 36 34 20 69 52 6f 77 2c 0a 20 20 20 20  int64 iRow,.    
2ed24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ed25 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75      sqlite3_valu
2ed26 65 20 2a 2a 70 56 61 6c 75 65 73 29 7b 0a 20 20  e **pValues){.  
2ed27 69 6e 74 20 72 63 20 3d 20 69 6e 69 74 50 65 6e  int rc = initPen
2ed28 64 69 6e 67 54 65 72 6d 73 28 76 2c 20 69 52 6f  dingTerms(v, iRo
2ed29 77 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  w);.  if( rc!=SQ
2ed2a 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
2ed2b 20 72 63 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72   rc;..  /* Gener
2ed2c 61 74 65 20 61 6e 20 65 6d 70 74 79 20 64 6f 63  ate an empty doc
2ed2d 6c 69 73 74 20 66 6f 72 20 65 61 63 68 20 74 65  list for each te
2ed2e 72 6d 20 74 68 61 74 20 70 72 65 76 69 6f 75 73  rm that previous
2ed2f 6c 79 20 61 70 70 65 61 72 65 64 20 69 6e 20 74  ly appeared in t
2ed30 68 69 73 0a 20 20 20 2a 20 72 6f 77 2e 20 2a 2f  his.   * row. */
2ed31 0a 20 20 72 63 20 3d 20 64 65 6c 65 74 65 54 65  .  rc = deleteTe
2ed32 72 6d 73 28 76 2c 20 69 52 6f 77 29 3b 0a 20 20  rms(v, iRow);.  
2ed33 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2ed34 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
2ed35 20 20 72 63 20 3d 20 63 6f 6e 74 65 6e 74 5f 75    rc = content_u
2ed36 70 64 61 74 65 28 76 2c 20 70 56 61 6c 75 65 73  pdate(v, pValues
2ed37 2c 20 69 52 6f 77 29 3b 20 20 2f 2a 20 65 78 65  , iRow);  /* exe
2ed38 63 75 74 65 20 61 6e 20 53 51 4c 20 55 50 44 41  cute an SQL UPDA
2ed39 54 45 20 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d  TE */.  if( rc!=
2ed3a 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
2ed3b 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 4e 6f 77  rn rc;..  /* Now
2ed3c 20 61 64 64 20 70 6f 73 69 74 69 6f 6e 73 20 66   add positions f
2ed3d 6f 72 20 74 65 72 6d 73 20 77 68 69 63 68 20 61  or terms which a
2ed3e 70 70 65 61 72 20 69 6e 20 74 68 65 20 75 70 64  ppear in the upd
2ed3f 61 74 65 64 20 72 6f 77 2e 20 2a 2f 0a 20 20 72  ated row. */.  r
2ed40 65 74 75 72 6e 20 69 6e 73 65 72 74 54 65 72 6d  eturn insertTerm
2ed41 73 28 76 2c 20 69 52 6f 77 2c 20 70 56 61 6c 75  s(v, iRow, pValu
2ed42 65 73 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  es);.}../*******
2ed43 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ed44 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ed45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ed46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2ed47 20 49 6e 74 65 72 69 6f 72 57 72 69 74 65 72 20   InteriorWriter 
2ed48 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6c 6c 65  is used to colle
2ed49 63 74 20 74 65 72 6d 73 20 61 6e 64 20 62 6c 6f  ct terms and blo
2ed4a 63 6b 20 72 65 66 65 72 65 6e 63 65 73 20 69 6e  ck references in
2ed4b 74 6f 0a 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e  to.** interior n
2ed4c 6f 64 65 73 20 69 6e 20 25 5f 73 65 67 6d 65 6e  odes in %_segmen
2ed4d 74 73 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74  ts.  See comment
2ed4e 61 72 79 20 61 74 20 74 6f 70 20 6f 66 20 66 69  ary at top of fi
2ed4f 6c 65 20 66 6f 72 0a 2a 2a 20 66 6f 72 6d 61 74  le for.** format
2ed50 2e 0a 2a 2f 0a 0a 2f 2a 20 48 6f 77 20 6c 61 72  ..*/../* How lar
2ed51 67 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  ge interior node
2ed52 73 20 63 61 6e 20 67 72 6f 77 2e 20 2a 2f 0a 23  s can grow. */.#
2ed53 64 65 66 69 6e 65 20 49 4e 54 45 52 49 4f 52 5f  define INTERIOR_
2ed54 4d 41 58 20 32 30 34 38 0a 0a 2f 2a 20 4d 69 6e  MAX 2048../* Min
2ed55 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74  imum number of t
2ed56 65 72 6d 73 20 70 65 72 20 69 6e 74 65 72 69 6f  erms per interio
2ed57 72 20 6e 6f 64 65 20 28 65 78 63 65 70 74 20 74  r node (except t
2ed58 68 65 20 72 6f 6f 74 29 2e 20 54 68 69 73 0a 2a  he root). This.*
2ed59 2a 20 70 72 65 76 65 6e 74 73 20 6c 61 72 67 65  * prevents large
2ed5a 20 74 65 72 6d 73 20 66 72 6f 6d 20 6d 61 6b 69   terms from maki
2ed5b 6e 67 20 74 68 65 20 74 72 65 65 20 74 6f 6f 20  ng the tree too 
2ed5c 73 6b 69 6e 6e 79 20 2d 20 6d 75 73 74 20 62 65  skinny - must be
2ed5d 20 3e 30 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74   >0.** so that t
2ed5e 68 65 20 74 72 65 65 20 61 6c 77 61 79 73 20 6d  he tree always m
2ed5f 61 6b 65 73 20 70 72 6f 67 72 65 73 73 2e 20 20  akes progress.  
2ed60 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 6d 69  Note that the mi
2ed61 6e 20 74 72 65 65 0a 2a 2a 20 66 61 6e 6f 75 74  n tree.** fanout
2ed62 20 77 69 6c 6c 20 62 65 20 49 4e 54 45 52 49 4f   will be INTERIO
2ed63 52 5f 4d 49 4e 5f 54 45 52 4d 53 2b 31 2e 0a 2a  R_MIN_TERMS+1..*
2ed64 2f 0a 23 64 65 66 69 6e 65 20 49 4e 54 45 52 49  /.#define INTERI
2ed65 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53 20 37 0a 23  OR_MIN_TERMS 7.#
2ed66 69 66 20 49 4e 54 45 52 49 4f 52 5f 4d 49 4e 5f  if INTERIOR_MIN_
2ed67 54 45 52 4d 53 3c 31 0a 23 20 65 72 72 6f 72 20  TERMS<1.# error 
2ed68 49 4e 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52  INTERIOR_MIN_TER
2ed69 4d 53 20 6d 75 73 74 20 62 65 20 67 72 65 61 74  MS must be great
2ed6a 65 72 20 74 68 61 6e 20 30 2e 0a 23 65 6e 64 69  er than 0..#endi
2ed6b 66 0a 0a 2f 2a 20 52 4f 4f 54 5f 4d 41 58 20 63  f../* ROOT_MAX c
2ed6c 6f 6e 74 72 6f 6c 73 20 68 6f 77 20 6d 75 63 68  ontrols how much
2ed6d 20 64 61 74 61 20 69 73 20 73 74 6f 72 65 64 20   data is stored 
2ed6e 69 6e 6c 69 6e 65 20 69 6e 20 74 68 65 20 73 65  inline in the se
2ed6f 67 6d 65 6e 74 0a 2a 2a 20 64 69 72 65 63 74 6f  gment.** directo
2ed70 72 79 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73  ry..*/./* TODO(s
2ed71 68 65 73 73 29 20 50 75 73 68 20 52 4f 4f 54 5f  hess) Push ROOT_
2ed72 4d 41 58 20 64 6f 77 6e 20 74 6f 20 77 68 6f 65  MAX down to whoe
2ed73 76 65 72 20 69 73 20 77 72 69 74 69 6e 67 20 74  ver is writing t
2ed74 68 69 6e 67 73 2e 20 20 49 74 27 73 0a 2a 2a 20  hings.  It's.** 
2ed75 6f 6e 6c 79 20 68 65 72 65 20 73 6f 20 74 68 61  only here so tha
2ed76 74 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72  t interiorWriter
2ed77 52 6f 6f 74 49 6e 66 6f 28 29 20 61 6e 64 20 6c  RootInfo() and l
2ed78 65 61 66 57 72 69 74 65 72 52 6f 6f 74 49 6e 66  eafWriterRootInf
2ed79 6f 28 29 0a 2a 2a 20 63 61 6e 20 62 6f 74 68 20  o().** can both 
2ed7a 73 65 65 20 69 74 2c 20 62 75 74 20 69 66 20 74  see it, but if t
2ed7b 68 65 20 63 61 6c 6c 65 72 20 70 61 73 73 65 64  he caller passed
2ed7c 20 69 74 20 69 6e 2c 20 77 65 20 77 6f 75 6c 64   it in, we would
2ed7d 6e 27 74 20 65 76 65 6e 0a 2a 2a 20 6e 65 65 64  n't even.** need
2ed7e 20 61 20 64 65 66 69 6e 65 2e 0a 2a 2f 0a 23 64   a define..*/.#d
2ed7f 65 66 69 6e 65 20 52 4f 4f 54 5f 4d 41 58 20 31  efine ROOT_MAX 1
2ed80 30 32 34 0a 23 69 66 20 52 4f 4f 54 5f 4d 41 58  024.#if ROOT_MAX
2ed81 3c 56 41 52 49 4e 54 5f 4d 41 58 2a 32 0a 23 20  <VARINT_MAX*2.# 
2ed82 65 72 72 6f 72 20 52 4f 4f 54 5f 4d 41 58 20 6d  error ROOT_MAX m
2ed83 75 73 74 20 68 61 76 65 20 65 6e 6f 75 67 68 20  ust have enough 
2ed84 73 70 61 63 65 20 66 6f 72 20 61 20 68 65 61 64  space for a head
2ed85 65 72 2e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 49  er..#endif../* I
2ed86 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 73 74 6f  nteriorBlock sto
2ed87 72 65 73 20 61 20 6c 69 6e 6b 65 64 2d 6c 69 73  res a linked-lis
2ed88 74 20 6f 66 20 69 6e 74 65 72 69 6f 72 20 62 6c  t of interior bl
2ed89 6f 63 6b 73 20 77 68 69 6c 65 20 61 20 6c 6f 77  ocks while a low
2ed8a 65 72 0a 2a 2a 20 6c 61 79 65 72 20 69 73 20 62  er.** layer is b
2ed8b 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64  eing constructed
2ed8c 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
2ed8d 75 63 74 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63  uct InteriorBloc
2ed8e 6b 20 7b 0a 20 20 44 61 74 61 42 75 66 66 65 72  k {.  DataBuffer
2ed8f 20 74 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20   term;          
2ed90 20 2f 2a 20 4c 65 66 74 6d 6f 73 74 20 74 65 72   /* Leftmost ter
2ed91 6d 20 69 6e 20 62 6c 6f 63 6b 27 73 20 73 75 62  m in block's sub
2ed92 74 72 65 65 2e 20 2a 2f 0a 20 20 44 61 74 61 42  tree. */.  DataB
2ed93 75 66 66 65 72 20 64 61 74 61 3b 20 20 20 20 20  uffer data;     
2ed94 20 20 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c        /* Accumul
2ed95 61 74 65 64 20 64 61 74 61 20 66 6f 72 20 74 68  ated data for th
2ed96 65 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 73 74  e block. */.  st
2ed97 72 75 63 74 20 49 6e 74 65 72 69 6f 72 42 6c 6f  ruct InteriorBlo
2ed98 63 6b 20 2a 6e 65 78 74 3b 0a 7d 20 49 6e 74 65  ck *next;.} Inte
2ed99 72 69 6f 72 42 6c 6f 63 6b 3b 0a 0a 73 74 61 74  riorBlock;..stat
2ed9a 69 63 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b  ic InteriorBlock
2ed9b 20 2a 69 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 4e   *interiorBlockN
2ed9c 65 77 28 69 6e 74 20 69 48 65 69 67 68 74 2c 20  ew(int iHeight, 
2ed9d 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 43 68  sqlite_int64 iCh
2ed9e 69 6c 64 42 6c 6f 63 6b 2c 0a 20 20 20 20 20 20  ildBlock,.      
2ed9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eda1 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65   const char *pTe
2eda2 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 29 7b 0a  rm, int nTerm){.
2eda3 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20    InteriorBlock 
2eda4 2a 62 6c 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33  *block = sqlite3
2eda5 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 49  _malloc(sizeof(I
2eda6 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 29 29 3b 0a  nteriorBlock));.
2eda7 20 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f    char c[VARINT_
2eda8 4d 41 58 2b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b  MAX+VARINT_MAX];
2eda9 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 69 66 28  .  int n;..  if(
2edaa 20 62 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 6d 65   block ){.    me
2edab 6d 73 65 74 28 62 6c 6f 63 6b 2c 20 30 2c 20 73  mset(block, 0, s
2edac 69 7a 65 6f 66 28 2a 62 6c 6f 63 6b 29 29 3b 0a  izeof(*block));.
2edad 20 20 20 20 64 61 74 61 42 75 66 66 65 72 49 6e      dataBufferIn
2edae 69 74 28 26 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2c  it(&block->term,
2edaf 20 30 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66   0);.    dataBuf
2edb0 66 65 72 52 65 70 6c 61 63 65 28 26 62 6c 6f 63  ferReplace(&bloc
2edb1 6b 2d 3e 74 65 72 6d 2c 20 70 54 65 72 6d 2c 20  k->term, pTerm, 
2edb2 6e 54 65 72 6d 29 3b 0a 0a 20 20 20 20 6e 20 3d  nTerm);..    n =
2edb3 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
2edb4 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20 20 20  , iHeight);.    
2edb5 6e 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69  n += fts3PutVari
2edb6 6e 74 28 63 2b 6e 2c 20 69 43 68 69 6c 64 42 6c  nt(c+n, iChildBl
2edb7 6f 63 6b 29 3b 0a 20 20 20 20 64 61 74 61 42 75  ock);.    dataBu
2edb8 66 66 65 72 49 6e 69 74 28 26 62 6c 6f 63 6b 2d  fferInit(&block-
2edb9 3e 64 61 74 61 2c 20 49 4e 54 45 52 49 4f 52 5f  >data, INTERIOR_
2edba 4d 41 58 29 3b 0a 20 20 20 20 64 61 74 61 42 75  MAX);.    dataBu
2edbb 66 66 65 72 52 65 70 6c 61 63 65 28 26 62 6c 6f  fferReplace(&blo
2edbc 63 6b 2d 3e 64 61 74 61 2c 20 63 2c 20 6e 29 3b  ck->data, c, n);
2edbd 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 62 6c  .  }.  return bl
2edbe 6f 63 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  ock;.}..#ifndef 
2edbf 4e 44 45 42 55 47 0a 2f 2a 20 56 65 72 69 66 79  NDEBUG./* Verify
2edc0 20 74 68 61 74 20 74 68 65 20 64 61 74 61 20 69   that the data i
2edc1 73 20 72 65 61 64 61 62 6c 65 20 61 73 20 61 6e  s readable as an
2edc2 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2e 20   interior node. 
2edc3 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  */.static void i
2edc4 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 56 61 6c 69  nteriorBlockVali
2edc5 64 61 74 65 28 49 6e 74 65 72 69 6f 72 42 6c 6f  date(InteriorBlo
2edc6 63 6b 20 2a 70 42 6c 6f 63 6b 29 7b 0a 20 20 63  ck *pBlock){.  c
2edc7 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61  onst char *pData
2edc8 20 3d 20 70 42 6c 6f 63 6b 2d 3e 64 61 74 61 2e   = pBlock->data.
2edc9 70 44 61 74 61 3b 0a 20 20 69 6e 74 20 6e 44 61  pData;.  int nDa
2edca 74 61 20 3d 20 70 42 6c 6f 63 6b 2d 3e 64 61 74  ta = pBlock->dat
2edcb 61 2e 6e 44 61 74 61 3b 0a 20 20 69 6e 74 20 6e  a.nData;.  int n
2edcc 2c 20 69 44 75 6d 6d 79 3b 0a 20 20 73 71 6c 69  , iDummy;.  sqli
2edcd 74 65 5f 69 6e 74 36 34 20 69 42 6c 6f 63 6b 69  te_int64 iBlocki
2edce 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 44  d;..  assert( nD
2edcf 61 74 61 3e 30 20 29 3b 0a 20 20 61 73 73 65 72  ata>0 );.  asser
2edd0 74 28 20 70 44 61 74 61 21 3d 30 20 29 3b 0a 20  t( pData!=0 );. 
2edd1 20 61 73 73 65 72 74 28 20 70 44 61 74 61 2b 6e   assert( pData+n
2edd2 44 61 74 61 3e 70 44 61 74 61 20 29 3b 0a 0a 20  Data>pData );.. 
2edd3 20 2f 2a 20 4d 75 73 74 20 6c 65 61 64 20 77 69   /* Must lead wi
2edd4 74 68 20 68 65 69 67 68 74 20 6f 66 20 6e 6f 64  th height of nod
2edd5 65 20 61 73 20 61 20 76 61 72 69 6e 74 28 6e 29  e as a varint(n)
2edd6 2c 20 6e 3e 30 20 2a 2f 0a 20 20 6e 20 3d 20 66  , n>0 */.  n = f
2edd7 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
2edd8 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a  Data, &iDummy);.
2edd9 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b    assert( n>0 );
2edda 0a 20 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d  .  assert( iDumm
2eddb 79 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  y>0 );.  assert(
2eddc 20 6e 3c 6e 44 61 74 61 20 29 3b 0a 20 20 70 44   n<nData );.  pD
2eddd 61 74 61 20 2b 3d 20 6e 3b 0a 20 20 6e 44 61 74  ata += n;.  nDat
2edde 61 20 2d 3d 20 6e 3b 0a 0a 20 20 2f 2a 20 4d 75  a -= n;..  /* Mu
2eddf 73 74 20 63 6f 6e 74 61 69 6e 20 69 42 6c 6f 63  st contain iBloc
2ede0 6b 69 64 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66 74  kid. */.  n = ft
2ede1 73 33 47 65 74 56 61 72 69 6e 74 28 70 44 61 74  s3GetVarint(pDat
2ede2 61 2c 20 26 69 42 6c 6f 63 6b 69 64 29 3b 0a 20  a, &iBlockid);. 
2ede3 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a   assert( n>0 );.
2ede4 20 20 61 73 73 65 72 74 28 20 6e 3c 3d 6e 44 61    assert( n<=nDa
2ede5 74 61 20 29 3b 0a 20 20 70 44 61 74 61 20 2b 3d  ta );.  pData +=
2ede6 20 6e 3b 0a 20 20 6e 44 61 74 61 20 2d 3d 20 6e   n;.  nData -= n
2ede7 3b 0a 0a 20 20 2f 2a 20 5a 65 72 6f 20 6f 72 20  ;..  /* Zero or 
2ede8 6d 6f 72 65 20 74 65 72 6d 73 20 6f 66 20 70 6f  more terms of po
2ede9 73 69 74 69 76 65 20 6c 65 6e 67 74 68 20 2a 2f  sitive length */
2edea 0a 20 20 69 66 28 20 6e 44 61 74 61 21 3d 30 20  .  if( nData!=0 
2edeb 29 7b 0a 20 20 20 20 2f 2a 20 46 69 72 73 74 20  ){.    /* First 
2edec 74 65 72 6d 20 69 73 20 6e 6f 74 20 64 65 6c 74  term is not delt
2eded 61 2d 65 6e 63 6f 64 65 64 2e 20 2a 2f 0a 20 20  a-encoded. */.  
2edee 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72    n = fts3GetVar
2edef 69 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44  int32(pData, &iD
2edf0 75 6d 6d 79 29 3b 0a 20 20 20 20 61 73 73 65 72  ummy);.    asser
2edf1 74 28 20 6e 3e 30 20 29 3b 0a 20 20 20 20 61 73  t( n>0 );.    as
2edf2 73 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29  sert( iDummy>0 )
2edf3 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b  ;.    assert( n+
2edf4 69 44 75 6d 6d 79 3e 30 29 3b 0a 20 20 20 20 61  iDummy>0);.    a
2edf5 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c  ssert( n+iDummy<
2edf6 3d 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 70 44  =nData );.    pD
2edf7 61 74 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b  ata += n+iDummy;
2edf8 0a 20 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e 2b  .    nData -= n+
2edf9 69 44 75 6d 6d 79 3b 0a 0a 20 20 20 20 2f 2a 20  iDummy;..    /* 
2edfa 46 6f 6c 6c 6f 77 69 6e 67 20 74 65 72 6d 73 20  Following terms 
2edfb 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64 2e 20 2a  delta-encoded. *
2edfc 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 44 61  /.    while( nDa
2edfd 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f  ta!=0 ){.      /
2edfe 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 68 61 72  * Length of shar
2edff 65 64 20 70 72 65 66 69 78 2e 20 2a 2f 0a 20 20  ed prefix. */.  
2ee00 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56      n = fts3GetV
2ee01 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c 20 26  arint32(pData, &
2ee02 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 61  iDummy);.      a
2ee03 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20  ssert( n>0 );.  
2ee04 20 20 20 20 61 73 73 65 72 74 28 20 69 44 75 6d      assert( iDum
2ee05 6d 79 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 61  my>=0 );.      a
2ee06 73 73 65 72 74 28 20 6e 3c 6e 44 61 74 61 20 29  ssert( n<nData )
2ee07 3b 0a 20 20 20 20 20 20 70 44 61 74 61 20 2b 3d  ;.      pData +=
2ee08 20 6e 3b 0a 20 20 20 20 20 20 6e 44 61 74 61 20   n;.      nData 
2ee09 2d 3d 20 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  -= n;..      /* 
2ee0a 4c 65 6e 67 74 68 20 61 6e 64 20 64 61 74 61 20  Length and data 
2ee0b 6f 66 20 64 69 73 74 69 6e 63 74 20 73 75 66 66  of distinct suff
2ee0c 69 78 2e 20 2a 2f 0a 20 20 20 20 20 20 6e 20 3d  ix. */.      n =
2ee0d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
2ee0e 28 70 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29  (pData, &iDummy)
2ee0f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2ee10 6e 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  n>0 );.      ass
2ee11 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b  ert( iDummy>0 );
2ee12 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e  .      assert( n
2ee13 2b 69 44 75 6d 6d 79 3e 30 29 3b 0a 20 20 20 20  +iDummy>0);.    
2ee14 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d    assert( n+iDum
2ee15 6d 79 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20 20  my<=nData );.   
2ee16 20 20 20 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44     pData += n+iD
2ee17 75 6d 6d 79 3b 0a 20 20 20 20 20 20 6e 44 61 74  ummy;.      nDat
2ee18 61 20 2d 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20  a -= n+iDummy;. 
2ee19 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 64 65 66 69     }.  }.}.#defi
2ee1a 6e 65 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f  ne ASSERT_VALID_
2ee1b 49 4e 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 78  INTERIOR_BLOCK(x
2ee1c 29 20 69 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 56  ) interiorBlockV
2ee1d 61 6c 69 64 61 74 65 28 78 29 0a 23 65 6c 73 65  alidate(x).#else
2ee1e 0a 23 64 65 66 69 6e 65 20 41 53 53 45 52 54 5f  .#define ASSERT_
2ee1f 56 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42  VALID_INTERIOR_B
2ee20 4c 4f 43 4b 28 78 29 20 61 73 73 65 72 74 28 20  LOCK(x) assert( 
2ee21 31 20 29 0a 23 65 6e 64 69 66 0a 0a 74 79 70 65  1 ).#endif..type
2ee22 64 65 66 20 73 74 72 75 63 74 20 49 6e 74 65 72  def struct Inter
2ee23 69 6f 72 57 72 69 74 65 72 20 7b 0a 20 20 69 6e  iorWriter {.  in
2ee24 74 20 69 48 65 69 67 68 74 3b 20 20 20 20 20 20  t iHeight;      
2ee25 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2ee26 66 72 6f 6d 20 30 20 61 74 20 6c 65 61 76 65 73  from 0 at leaves
2ee27 2e 20 2a 2f 0a 20 20 49 6e 74 65 72 69 6f 72 42  . */.  InteriorB
2ee28 6c 6f 63 6b 20 2a 66 69 72 73 74 2c 20 2a 6c 61  lock *first, *la
2ee29 73 74 3b 0a 20 20 73 74 72 75 63 74 20 49 6e 74  st;.  struct Int
2ee2a 65 72 69 6f 72 57 72 69 74 65 72 20 2a 70 61 72  eriorWriter *par
2ee2b 65 6e 74 57 72 69 74 65 72 3b 0a 0a 20 20 44 61  entWriter;..  Da
2ee2c 74 61 42 75 66 66 65 72 20 74 65 72 6d 3b 20 20  taBuffer term;  
2ee2d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2ee2e 4c 61 73 74 20 74 65 72 6d 20 77 72 69 74 74 65  Last term writte
2ee2f 6e 20 74 6f 20 62 6c 6f 63 6b 20 22 6c 61 73 74  n to block "last
2ee30 22 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69  ". */.  sqlite_i
2ee31 6e 74 36 34 20 69 4f 70 65 6e 69 6e 67 43 68 69  nt64 iOpeningChi
2ee32 6c 64 42 6c 6f 63 6b 3b 20 2f 2a 20 46 69 72 73  ldBlock; /* Firs
2ee33 74 20 63 68 69 6c 64 20 62 6c 6f 63 6b 20 69 6e  t child block in
2ee34 20 62 6c 6f 63 6b 20 22 6c 61 73 74 22 2e 20 2a   block "last". *
2ee35 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  /.#ifndef NDEBUG
2ee36 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
2ee37 69 4c 61 73 74 43 68 69 6c 64 42 6c 6f 63 6b 3b  iLastChildBlock;
2ee38 20 20 2f 2a 20 66 6f 72 20 63 6f 6e 73 69 73 74    /* for consist
2ee39 65 6e 63 79 20 63 68 65 63 6b 73 2e 20 2a 2f 0a  ency checks. */.
2ee3a 23 65 6e 64 69 66 0a 7d 20 49 6e 74 65 72 69 6f  #endif.} Interio
2ee3b 72 57 72 69 74 65 72 3b 0a 0a 2f 2a 20 49 6e 69  rWriter;../* Ini
2ee3c 74 69 61 6c 69 7a 65 20 61 6e 20 69 6e 74 65 72  tialize an inter
2ee3d 69 6f 72 20 6e 6f 64 65 20 77 68 65 72 65 20 70  ior node where p
2ee3e 54 65 72 6d 5b 6e 54 65 72 6d 5d 20 6d 61 72 6b  Term[nTerm] mark
2ee3f 73 20 74 68 65 20 6c 65 66 74 6d 6f 73 74 0a 2a  s the leftmost.*
2ee40 2a 20 74 65 72 6d 20 69 6e 20 74 68 65 20 74 72  * term in the tr
2ee41 65 65 2e 20 20 69 43 68 69 6c 64 42 6c 6f 63 6b  ee.  iChildBlock
2ee42 20 69 73 20 74 68 65 20 6c 65 66 74 6d 6f 73 74   is the leftmost
2ee43 20 63 68 69 6c 64 20 62 6c 6f 63 6b 20 61 74 20   child block at 
2ee44 74 68 65 0a 2a 2a 20 6e 65 78 74 20 6c 65 76 65  the.** next leve
2ee45 6c 20 64 6f 77 6e 20 74 68 65 20 74 72 65 65 2e  l down the tree.
2ee46 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2ee47 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 49 6e  interiorWriterIn
2ee48 69 74 28 69 6e 74 20 69 48 65 69 67 68 74 2c 20  it(int iHeight, 
2ee49 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72  const char *pTer
2ee4a 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 0a 20 20  m, int nTerm,.  
2ee4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ee4c 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
2ee4d 69 74 65 5f 69 6e 74 36 34 20 69 43 68 69 6c 64  ite_int64 iChild
2ee4e 42 6c 6f 63 6b 2c 0a 20 20 20 20 20 20 20 20 20  Block,.         
2ee4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ee50 20 20 20 20 20 20 49 6e 74 65 72 69 6f 72 57 72        InteriorWr
2ee51 69 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a  iter *pWriter){.
2ee52 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20    InteriorBlock 
2ee53 2a 62 6c 6f 63 6b 3b 0a 20 20 61 73 73 65 72 74  *block;.  assert
2ee54 28 20 69 48 65 69 67 68 74 3e 30 20 29 3b 0a 20  ( iHeight>0 );. 
2ee55 20 43 4c 45 41 52 28 70 57 72 69 74 65 72 29 3b   CLEAR(pWriter);
2ee56 0a 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 48 65  ..  pWriter->iHe
2ee57 69 67 68 74 20 3d 20 69 48 65 69 67 68 74 3b 0a  ight = iHeight;.
2ee58 20 20 70 57 72 69 74 65 72 2d 3e 69 4f 70 65 6e    pWriter->iOpen
2ee59 69 6e 67 43 68 69 6c 64 42 6c 6f 63 6b 20 3d 20  ingChildBlock = 
2ee5a 69 43 68 69 6c 64 42 6c 6f 63 6b 3b 0a 23 69 66  iChildBlock;.#if
2ee5b 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 70 57  ndef NDEBUG.  pW
2ee5c 72 69 74 65 72 2d 3e 69 4c 61 73 74 43 68 69 6c  riter->iLastChil
2ee5d 64 42 6c 6f 63 6b 20 3d 20 69 43 68 69 6c 64 42  dBlock = iChildB
2ee5e 6c 6f 63 6b 3b 0a 23 65 6e 64 69 66 0a 20 20 62  lock;.#endif.  b
2ee5f 6c 6f 63 6b 20 3d 20 69 6e 74 65 72 69 6f 72 42  lock = interiorB
2ee60 6c 6f 63 6b 4e 65 77 28 69 48 65 69 67 68 74 2c  lockNew(iHeight,
2ee61 20 69 43 68 69 6c 64 42 6c 6f 63 6b 2c 20 70 54   iChildBlock, pT
2ee62 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 70  erm, nTerm);.  p
2ee63 57 72 69 74 65 72 2d 3e 6c 61 73 74 20 3d 20 70  Writer->last = p
2ee64 57 72 69 74 65 72 2d 3e 66 69 72 73 74 20 3d 20  Writer->first = 
2ee65 62 6c 6f 63 6b 3b 0a 20 20 41 53 53 45 52 54 5f  block;.  ASSERT_
2ee66 56 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42  VALID_INTERIOR_B
2ee67 4c 4f 43 4b 28 70 57 72 69 74 65 72 2d 3e 6c 61  LOCK(pWriter->la
2ee68 73 74 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  st);.  dataBuffe
2ee69 72 49 6e 69 74 28 26 70 57 72 69 74 65 72 2d 3e  rInit(&pWriter->
2ee6a 74 65 72 6d 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 20  term, 0);.}../* 
2ee6b 41 70 70 65 6e 64 20 74 68 65 20 63 68 69 6c 64  Append the child
2ee6c 20 6e 6f 64 65 20 72 6f 6f 74 65 64 20 61 74 20   node rooted at 
2ee6d 69 43 68 69 6c 64 42 6c 6f 63 6b 20 74 6f 20 74  iChildBlock to t
2ee6e 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  he interior node
2ee6f 2c 0a 2a 2a 20 77 69 74 68 20 70 54 65 72 6d 5b  ,.** with pTerm[
2ee70 6e 54 65 72 6d 5d 20 61 73 20 74 68 65 20 6c 65  nTerm] as the le
2ee71 66 74 6d 6f 73 74 20 74 65 72 6d 20 69 6e 20 69  ftmost term in i
2ee72 43 68 69 6c 64 42 6c 6f 63 6b 27 73 20 73 75 62  ChildBlock's sub
2ee73 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tree..*/.static 
2ee74 76 6f 69 64 20 69 6e 74 65 72 69 6f 72 57 72 69  void interiorWri
2ee75 74 65 72 41 70 70 65 6e 64 28 49 6e 74 65 72 69  terAppend(Interi
2ee76 6f 72 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  orWriter *pWrite
2ee77 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
2ee78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ee79 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2ee7a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d  pTerm, int nTerm
2ee7b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2ee7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ee7d 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
2ee7e 69 43 68 69 6c 64 42 6c 6f 63 6b 29 7b 0a 20 20  iChildBlock){.  
2ee7f 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41  char c[VARINT_MA
2ee80 58 2b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20  X+VARINT_MAX];. 
2ee81 20 69 6e 74 20 6e 2c 20 6e 50 72 65 66 69 78 20   int n, nPrefix 
2ee82 3d 20 30 3b 0a 0a 20 20 41 53 53 45 52 54 5f 56  = 0;..  ASSERT_V
2ee83 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42 4c  ALID_INTERIOR_BL
2ee84 4f 43 4b 28 70 57 72 69 74 65 72 2d 3e 6c 61 73  OCK(pWriter->las
2ee85 74 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 69  t);..  /* The fi
2ee86 72 73 74 20 74 65 72 6d 20 77 72 69 74 74 65 6e  rst term written
2ee87 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 72 69 6f   into an interio
2ee88 72 20 6e 6f 64 65 20 69 73 20 61 63 74 75 61 6c  r node is actual
2ee89 6c 79 0a 20 20 2a 2a 20 61 73 73 6f 63 69 61 74  ly.  ** associat
2ee8a 65 64 20 77 69 74 68 20 74 68 65 20 73 65 63 6f  ed with the seco
2ee8b 6e 64 20 63 68 69 6c 64 20 61 64 64 65 64 20 28  nd child added (
2ee8c 74 68 65 20 66 69 72 73 74 20 63 68 69 6c 64 20  the first child 
2ee8d 77 61 73 20 61 64 64 65 64 0a 20 20 2a 2a 20 69  was added.  ** i
2ee8e 6e 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72  n interiorWriter
2ee8f 49 6e 69 74 2c 20 6f 72 20 69 6e 20 74 68 65 20  Init, or in the 
2ee90 69 66 20 63 6c 61 75 73 65 20 61 74 20 74 68 65  if clause at the
2ee91 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 69 73 0a   bottom of this.
2ee92 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 29 2e 20    ** function). 
2ee93 20 54 68 61 74 20 74 65 72 6d 20 67 65 74 73 20   That term gets 
2ee94 65 6e 63 6f 64 65 64 20 73 74 72 61 69 67 68 74  encoded straight
2ee95 20 75 70 2c 20 77 69 74 68 20 6e 50 72 65 66 69   up, with nPrefi
2ee96 78 20 6c 65 66 74 0a 20 20 2a 2a 20 61 74 20 30  x left.  ** at 0
2ee97 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 57 72  ..  */.  if( pWr
2ee98 69 74 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61  iter->term.nData
2ee99 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 66  ==0 ){.    n = f
2ee9a 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20  ts3PutVarint(c, 
2ee9b 6e 54 65 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b  nTerm);.  }else{
2ee9c 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 50 72 65  .    while( nPre
2ee9d 66 69 78 3c 70 57 72 69 74 65 72 2d 3e 74 65 72  fix<pWriter->ter
2ee9e 6d 2e 6e 44 61 74 61 20 26 26 0a 20 20 20 20 20  m.nData &&.     
2ee9f 20 20 20 20 20 20 70 54 65 72 6d 5b 6e 50 72 65        pTerm[nPre
2eea0 66 69 78 5d 3d 3d 70 57 72 69 74 65 72 2d 3e 74  fix]==pWriter->t
2eea1 65 72 6d 2e 70 44 61 74 61 5b 6e 50 72 65 66 69  erm.pData[nPrefi
2eea2 78 5d 20 29 7b 0a 20 20 20 20 20 20 6e 50 72 65  x] ){.      nPre
2eea3 66 69 78 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20  fix++;.    }..  
2eea4 20 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72    n = fts3PutVar
2eea5 69 6e 74 28 63 2c 20 6e 50 72 65 66 69 78 29 3b  int(c, nPrefix);
2eea6 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 50 75  .    n += fts3Pu
2eea7 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 6e 54 65  tVarint(c+n, nTe
2eea8 72 6d 2d 6e 50 72 65 66 69 78 29 3b 0a 20 20 7d  rm-nPrefix);.  }
2eea9 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
2eeaa 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 4c 61 73  .  pWriter->iLas
2eeab 74 43 68 69 6c 64 42 6c 6f 63 6b 2b 2b 3b 0a 23  tChildBlock++;.#
2eeac 65 6e 64 69 66 0a 20 20 61 73 73 65 72 74 28 20  endif.  assert( 
2eead 70 57 72 69 74 65 72 2d 3e 69 4c 61 73 74 43 68  pWriter->iLastCh
2eeae 69 6c 64 42 6c 6f 63 6b 3d 3d 69 43 68 69 6c 64  ildBlock==iChild
2eeaf 42 6c 6f 63 6b 20 29 3b 0a 0a 20 20 2f 2a 20 4f  Block );..  /* O
2eeb0 76 65 72 66 6c 6f 77 20 74 6f 20 61 20 6e 65 77  verflow to a new
2eeb1 20 62 6c 6f 63 6b 20 69 66 20 74 68 65 20 6e 65   block if the ne
2eeb2 77 20 74 65 72 6d 20 6d 61 6b 65 73 20 74 68 65  w term makes the
2eeb3 20 63 75 72 72 65 6e 74 20 62 6c 6f 63 6b 0a 20   current block. 
2eeb4 20 2a 2a 20 74 6f 6f 20 62 69 67 2c 20 61 6e 64   ** too big, and
2eeb5 20 74 68 65 20 63 75 72 72 65 6e 74 20 62 6c 6f   the current blo
2eeb6 63 6b 20 61 6c 72 65 61 64 79 20 68 61 73 20 65  ck already has e
2eeb7 6e 6f 75 67 68 20 74 65 72 6d 73 2e 0a 20 20 2a  nough terms..  *
2eeb8 2f 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d  /.  if( pWriter-
2eeb9 3e 6c 61 73 74 2d 3e 64 61 74 61 2e 6e 44 61 74  >last->data.nDat
2eeba 61 2b 6e 2b 6e 54 65 72 6d 2d 6e 50 72 65 66 69  a+n+nTerm-nPrefi
2eebb 78 3e 49 4e 54 45 52 49 4f 52 5f 4d 41 58 20 26  x>INTERIOR_MAX &
2eebc 26 0a 20 20 20 20 20 20 69 43 68 69 6c 64 42 6c  &.      iChildBl
2eebd 6f 63 6b 2d 70 57 72 69 74 65 72 2d 3e 69 4f 70  ock-pWriter->iOp
2eebe 65 6e 69 6e 67 43 68 69 6c 64 42 6c 6f 63 6b 3e  eningChildBlock>
2eebf 49 4e 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52  INTERIOR_MIN_TER
2eec0 4d 53 20 29 7b 0a 20 20 20 20 70 57 72 69 74 65  MS ){.    pWrite
2eec1 72 2d 3e 6c 61 73 74 2d 3e 6e 65 78 74 20 3d 20  r->last->next = 
2eec2 69 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 4e 65 77  interiorBlockNew
2eec3 28 70 57 72 69 74 65 72 2d 3e 69 48 65 69 67 68  (pWriter->iHeigh
2eec4 74 2c 20 69 43 68 69 6c 64 42 6c 6f 63 6b 2c 0a  t, iChildBlock,.
2eec5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eec6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eec7 20 20 20 20 20 20 20 20 20 20 20 70 54 65 72 6d             pTerm
2eec8 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20 20 70 57  , nTerm);.    pW
2eec9 72 69 74 65 72 2d 3e 6c 61 73 74 20 3d 20 70 57  riter->last = pW
2eeca 72 69 74 65 72 2d 3e 6c 61 73 74 2d 3e 6e 65 78  riter->last->nex
2eecb 74 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  t;.    pWriter->
2eecc 69 4f 70 65 6e 69 6e 67 43 68 69 6c 64 42 6c 6f  iOpeningChildBlo
2eecd 63 6b 20 3d 20 69 43 68 69 6c 64 42 6c 6f 63 6b  ck = iChildBlock
2eece 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  ;.    dataBuffer
2eecf 52 65 73 65 74 28 26 70 57 72 69 74 65 72 2d 3e  Reset(&pWriter->
2eed0 74 65 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  term);.  }else{.
2eed1 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70      dataBufferAp
2eed2 70 65 6e 64 32 28 26 70 57 72 69 74 65 72 2d 3e  pend2(&pWriter->
2eed3 6c 61 73 74 2d 3e 64 61 74 61 2c 20 63 2c 20 6e  last->data, c, n
2eed4 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2eed5 20 20 20 20 20 20 20 20 70 54 65 72 6d 2b 6e 50          pTerm+nP
2eed6 72 65 66 69 78 2c 20 6e 54 65 72 6d 2d 6e 50 72  refix, nTerm-nPr
2eed7 65 66 69 78 29 3b 0a 20 20 20 20 64 61 74 61 42  efix);.    dataB
2eed8 75 66 66 65 72 52 65 70 6c 61 63 65 28 26 70 57  ufferReplace(&pW
2eed9 72 69 74 65 72 2d 3e 74 65 72 6d 2c 20 70 54 65  riter->term, pTe
2eeda 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 7d 0a  rm, nTerm);.  }.
2eedb 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 49    ASSERT_VALID_I
2eedc 4e 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 70 57  NTERIOR_BLOCK(pW
2eedd 72 69 74 65 72 2d 3e 6c 61 73 74 29 3b 0a 7d 0a  riter->last);.}.
2eede 0a 2f 2a 20 46 72 65 65 20 74 68 65 20 73 70 61  ./* Free the spa
2eedf 63 65 20 75 73 65 64 20 62 79 20 70 57 72 69 74  ce used by pWrit
2eee0 65 72 2c 20 69 6e 63 6c 75 64 69 6e 67 20 74 68  er, including th
2eee1 65 20 6c 69 6e 6b 65 64 2d 6c 69 73 74 20 6f 66  e linked-list of
2eee2 0a 2a 2a 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63  .** InteriorBloc
2eee3 6b 73 2c 20 61 6e 64 20 70 61 72 65 6e 74 57 72  ks, and parentWr
2eee4 69 74 65 72 2c 20 69 66 20 70 72 65 73 65 6e 74  iter, if present
2eee5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2eee6 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 44 65  interiorWriterDe
2eee7 73 74 72 6f 79 28 49 6e 74 65 72 69 6f 72 57 72  stroy(InteriorWr
2eee8 69 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a  iter *pWriter){.
2eee9 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20    InteriorBlock 
2eeea 2a 62 6c 6f 63 6b 20 3d 20 70 57 72 69 74 65 72  *block = pWriter
2eeeb 2d 3e 66 69 72 73 74 3b 0a 0a 20 20 77 68 69 6c  ->first;..  whil
2eeec 65 28 20 62 6c 6f 63 6b 21 3d 4e 55 4c 4c 20 29  e( block!=NULL )
2eeed 7b 0a 20 20 20 20 49 6e 74 65 72 69 6f 72 42 6c  {.    InteriorBl
2eeee 6f 63 6b 20 2a 62 20 3d 20 62 6c 6f 63 6b 3b 0a  ock *b = block;.
2eeef 20 20 20 20 62 6c 6f 63 6b 20 3d 20 62 6c 6f 63      block = bloc
2eef0 6b 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 64 61 74  k->next;.    dat
2eef1 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  aBufferDestroy(&
2eef2 62 2d 3e 74 65 72 6d 29 3b 0a 20 20 20 20 64 61  b->term);.    da
2eef3 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
2eef4 26 62 2d 3e 64 61 74 61 29 3b 0a 20 20 20 20 73  &b->data);.    s
2eef5 71 6c 69 74 65 33 5f 66 72 65 65 28 62 29 3b 0a  qlite3_free(b);.
2eef6 20 20 7d 0a 20 20 69 66 28 20 70 57 72 69 74 65    }.  if( pWrite
2eef7 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 21  r->parentWriter!
2eef8 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 69 6e 74  =NULL ){.    int
2eef9 65 72 69 6f 72 57 72 69 74 65 72 44 65 73 74 72  eriorWriterDestr
2eefa 6f 79 28 70 57 72 69 74 65 72 2d 3e 70 61 72 65  oy(pWriter->pare
2eefb 6e 74 57 72 69 74 65 72 29 3b 0a 20 20 20 20 73  ntWriter);.    s
2eefc 71 6c 69 74 65 33 5f 66 72 65 65 28 70 57 72 69  qlite3_free(pWri
2eefd 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65  ter->parentWrite
2eefe 72 29 3b 0a 20 20 7d 0a 20 20 64 61 74 61 42 75  r);.  }.  dataBu
2eeff 66 66 65 72 44 65 73 74 72 6f 79 28 26 70 57 72  fferDestroy(&pWr
2ef00 69 74 65 72 2d 3e 74 65 72 6d 29 3b 0a 20 20 53  iter->term);.  S
2ef01 43 52 41 4d 42 4c 45 28 70 57 72 69 74 65 72 29  CRAMBLE(pWriter)
2ef02 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
2ef03 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 49 66 20 70  E_OK;.}../* If p
2ef04 57 72 69 74 65 72 20 63 61 6e 20 66 69 74 20 65  Writer can fit e
2ef05 6e 74 69 72 65 6c 79 20 69 6e 20 52 4f 4f 54 5f  ntirely in ROOT_
2ef06 4d 41 58 2c 20 72 65 74 75 72 6e 20 69 74 20 61  MAX, return it a
2ef07 73 20 74 68 65 20 72 6f 6f 74 20 69 6e 66 6f 0a  s the root info.
2ef08 2a 2a 20 64 69 72 65 63 74 6c 79 2c 20 6c 65 61  ** directly, lea
2ef09 76 69 6e 67 20 2a 70 69 45 6e 64 42 6c 6f 63 6b  ving *piEndBlock
2ef0a 69 64 20 75 6e 63 68 61 6e 67 65 64 2e 20 20 4f  id unchanged.  O
2ef0b 74 68 65 72 77 69 73 65 2c 20 66 6c 75 73 68 0a  therwise, flush.
2ef0c 2a 2a 20 70 57 72 69 74 65 72 20 74 6f 20 25 5f  ** pWriter to %_
2ef0d 73 65 67 6d 65 6e 74 73 2c 20 62 75 69 6c 64 69  segments, buildi
2ef0e 6e 67 20 61 20 6e 65 77 20 6c 61 79 65 72 20 6f  ng a new layer o
2ef0f 66 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73  f interior nodes
2ef10 2c 20 61 6e 64 0a 2a 2a 20 72 65 63 75 72 73 69  , and.** recursi
2ef11 76 65 6c 79 20 61 73 6b 20 66 6f 72 20 74 68 65  vely ask for the
2ef12 69 72 20 72 6f 6f 74 20 69 6e 74 6f 2e 0a 2a 2f  ir root into..*/
2ef13 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 74 65  .static int inte
2ef14 72 69 6f 72 57 72 69 74 65 72 52 6f 6f 74 49 6e  riorWriterRootIn
2ef15 66 6f 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  fo(fulltext_vtab
2ef16 20 2a 76 2c 20 49 6e 74 65 72 69 6f 72 57 72 69   *v, InteriorWri
2ef17 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20  ter *pWriter,.  
2ef18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ef19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ef1a 63 68 61 72 20 2a 2a 70 70 52 6f 6f 74 49 6e 66  char **ppRootInf
2ef1b 6f 2c 20 69 6e 74 20 2a 70 6e 52 6f 6f 74 49 6e  o, int *pnRootIn
2ef1c 66 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  fo,.            
2ef1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ef1e 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74        sqlite_int
2ef1f 36 34 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64  64 *piEndBlockid
2ef20 29 7b 0a 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f  ){.  InteriorBlo
2ef21 63 6b 20 2a 62 6c 6f 63 6b 20 3d 20 70 57 72 69  ck *block = pWri
2ef22 74 65 72 2d 3e 66 69 72 73 74 3b 0a 20 20 73 71  ter->first;.  sq
2ef23 6c 69 74 65 5f 69 6e 74 36 34 20 69 42 6c 6f 63  lite_int64 iBloc
2ef24 6b 69 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  kid = 0;.  int r
2ef25 63 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63  c;..  /* If we c
2ef26 61 6e 20 66 69 74 20 74 68 65 20 73 65 67 6d 65  an fit the segme
2ef27 6e 74 20 69 6e 6c 69 6e 65 20 2a 2f 0a 20 20 69  nt inline */.  i
2ef28 66 28 20 62 6c 6f 63 6b 3d 3d 70 57 72 69 74 65  f( block==pWrite
2ef29 72 2d 3e 6c 61 73 74 20 26 26 20 62 6c 6f 63 6b  r->last && block
2ef2a 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3c 52 4f 4f  ->data.nData<ROO
2ef2b 54 5f 4d 41 58 20 29 7b 0a 20 20 20 20 2a 70 70  T_MAX ){.    *pp
2ef2c 52 6f 6f 74 49 6e 66 6f 20 3d 20 62 6c 6f 63 6b  RootInfo = block
2ef2d 2d 3e 64 61 74 61 2e 70 44 61 74 61 3b 0a 20 20  ->data.pData;.  
2ef2e 20 20 2a 70 6e 52 6f 6f 74 49 6e 66 6f 20 3d 20    *pnRootInfo = 
2ef2f 62 6c 6f 63 6b 2d 3e 64 61 74 61 2e 6e 44 61 74  block->data.nDat
2ef30 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  a;.    return SQ
2ef31 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
2ef32 2f 2a 20 46 6c 75 73 68 20 74 68 65 20 66 69 72  /* Flush the fir
2ef33 73 74 20 62 6c 6f 63 6b 20 74 6f 20 25 5f 73 65  st block to %_se
2ef34 67 6d 65 6e 74 73 2c 20 61 6e 64 20 63 72 65 61  gments, and crea
2ef35 74 65 20 61 20 6e 65 77 20 6c 65 76 65 6c 20 6f  te a new level o
2ef36 66 0a 20 20 2a 2a 20 69 6e 74 65 72 69 6f 72 20  f.  ** interior 
2ef37 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 41 53 53  node..  */.  ASS
2ef38 45 52 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52 49  ERT_VALID_INTERI
2ef39 4f 52 5f 42 4c 4f 43 4b 28 62 6c 6f 63 6b 29 3b  OR_BLOCK(block);
2ef3a 0a 20 20 72 63 20 3d 20 62 6c 6f 63 6b 5f 69 6e  .  rc = block_in
2ef3b 73 65 72 74 28 76 2c 20 62 6c 6f 63 6b 2d 3e 64  sert(v, block->d
2ef3c 61 74 61 2e 70 44 61 74 61 2c 20 62 6c 6f 63 6b  ata.pData, block
2ef3d 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2c 20 26 69  ->data.nData, &i
2ef3e 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20  Blockid);.  if( 
2ef3f 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2ef40 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 2a 70 69  return rc;.  *pi
2ef41 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 69 42 6c  EndBlockid = iBl
2ef42 6f 63 6b 69 64 3b 0a 0a 20 20 70 57 72 69 74 65  ockid;..  pWrite
2ef43 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 20  r->parentWriter 
2ef44 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
2ef45 28 73 69 7a 65 6f 66 28 2a 70 57 72 69 74 65 72  (sizeof(*pWriter
2ef46 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 29 29  ->parentWriter))
2ef47 3b 0a 20 20 69 6e 74 65 72 69 6f 72 57 72 69 74  ;.  interiorWrit
2ef48 65 72 49 6e 69 74 28 70 57 72 69 74 65 72 2d 3e  erInit(pWriter->
2ef49 69 48 65 69 67 68 74 2b 31 2c 0a 20 20 20 20 20  iHeight+1,.     
2ef4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ef4b 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2e 70 44 61 74  block->term.pDat
2ef4c 61 2c 20 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2e 6e  a, block->term.n
2ef4d 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20  Data,.          
2ef4e 20 20 20 20 20 20 20 20 20 20 20 69 42 6c 6f 63             iBloc
2ef4f 6b 69 64 2c 20 70 57 72 69 74 65 72 2d 3e 70 61  kid, pWriter->pa
2ef50 72 65 6e 74 57 72 69 74 65 72 29 3b 0a 0a 20 20  rentWriter);..  
2ef51 2f 2a 20 46 6c 75 73 68 20 61 64 64 69 74 69 6f  /* Flush additio
2ef52 6e 61 6c 20 62 6c 6f 63 6b 73 20 61 6e 64 20 61  nal blocks and a
2ef53 70 70 65 6e 64 20 74 6f 20 74 68 65 20 68 69 67  ppend to the hig
2ef54 68 65 72 20 69 6e 74 65 72 69 6f 72 0a 20 20 2a  her interior.  *
2ef55 2a 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 66  * node..  */.  f
2ef56 6f 72 28 62 6c 6f 63 6b 3d 62 6c 6f 63 6b 2d 3e  or(block=block->
2ef57 6e 65 78 74 3b 20 62 6c 6f 63 6b 21 3d 4e 55 4c  next; block!=NUL
2ef58 4c 3b 20 62 6c 6f 63 6b 3d 62 6c 6f 63 6b 2d 3e  L; block=block->
2ef59 6e 65 78 74 29 7b 0a 20 20 20 20 41 53 53 45 52  next){.    ASSER
2ef5a 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52  T_VALID_INTERIOR
2ef5b 5f 42 4c 4f 43 4b 28 62 6c 6f 63 6b 29 3b 0a 20  _BLOCK(block);. 
2ef5c 20 20 20 72 63 20 3d 20 62 6c 6f 63 6b 5f 69 6e     rc = block_in
2ef5d 73 65 72 74 28 76 2c 20 62 6c 6f 63 6b 2d 3e 64  sert(v, block->d
2ef5e 61 74 61 2e 70 44 61 74 61 2c 20 62 6c 6f 63 6b  ata.pData, block
2ef5f 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2c 20 26 69  ->data.nData, &i
2ef60 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 20 20 69 66  Blockid);.    if
2ef61 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2ef62 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
2ef63 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64 20 3d   *piEndBlockid =
2ef64 20 69 42 6c 6f 63 6b 69 64 3b 0a 0a 20 20 20 20   iBlockid;..    
2ef65 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 41 70  interiorWriterAp
2ef66 70 65 6e 64 28 70 57 72 69 74 65 72 2d 3e 70 61  pend(pWriter->pa
2ef67 72 65 6e 74 57 72 69 74 65 72 2c 0a 20 20 20 20  rentWriter,.    
2ef68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ef69 20 20 20 20 20 62 6c 6f 63 6b 2d 3e 74 65 72 6d       block->term
2ef6a 2e 70 44 61 74 61 2c 20 62 6c 6f 63 6b 2d 3e 74  .pData, block->t
2ef6b 65 72 6d 2e 6e 44 61 74 61 2c 20 69 42 6c 6f 63  erm.nData, iBloc
2ef6c 6b 69 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  kid);.  }..  /* 
2ef6d 50 61 72 65 6e 74 20 6e 6f 64 65 20 67 65 74 73  Parent node gets
2ef6e 20 74 68 65 20 63 68 61 6e 63 65 20 74 6f 20 62   the chance to b
2ef6f 65 20 74 68 65 20 72 6f 6f 74 2e 20 2a 2f 0a 20  e the root. */. 
2ef70 20 72 65 74 75 72 6e 20 69 6e 74 65 72 69 6f 72   return interior
2ef71 57 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 76  WriterRootInfo(v
2ef72 2c 20 70 57 72 69 74 65 72 2d 3e 70 61 72 65 6e  , pWriter->paren
2ef73 74 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20  tWriter,.       
2ef74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ef75 20 20 20 20 20 20 20 20 20 70 70 52 6f 6f 74 49           ppRootI
2ef76 6e 66 6f 2c 20 70 6e 52 6f 6f 74 49 6e 66 6f 2c  nfo, pnRootInfo,
2ef77 20 70 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a   piEndBlockid);.
2ef78 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
2ef79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ef7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ef7b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ef7c 2a 2a 2a 2a 2f 0a 2f 2a 20 49 6e 74 65 72 69 6f  ****/./* Interio
2ef7d 72 52 65 61 64 65 72 20 69 73 20 75 73 65 64 20  rReader is used 
2ef7e 74 6f 20 72 65 61 64 20 6f 66 66 20 74 68 65 20  to read off the 
2ef7f 64 61 74 61 20 66 72 6f 6d 20 61 6e 20 69 6e 74  data from an int
2ef80 65 72 69 6f 72 20 6e 6f 64 65 0a 2a 2a 20 28 73  erior node.** (s
2ef81 65 65 20 63 6f 6d 6d 65 6e 74 20 61 74 20 74 6f  ee comment at to
2ef82 70 20 6f 66 20 66 69 6c 65 20 66 6f 72 20 74 68  p of file for th
2ef83 65 20 66 6f 72 6d 61 74 29 2e 0a 2a 2f 0a 74 79  e format)..*/.ty
2ef84 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e 74  pedef struct Int
2ef85 65 72 69 6f 72 52 65 61 64 65 72 20 7b 0a 20 20  eriorReader {.  
2ef86 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
2ef87 61 3b 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a  a;.  int nData;.
2ef88 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 74 65  .  DataBuffer te
2ef89 72 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  rm;          /* 
2ef8a 70 72 65 76 69 6f 75 73 20 74 65 72 6d 2c 20 66  previous term, f
2ef8b 6f 72 20 64 65 63 6f 64 69 6e 67 20 74 65 72 6d  or decoding term
2ef8c 20 64 65 6c 74 61 2e 20 2a 2f 0a 0a 20 20 73 71   delta. */..  sq
2ef8d 6c 69 74 65 5f 69 6e 74 36 34 20 69 42 6c 6f 63  lite_int64 iBloc
2ef8e 6b 69 64 3b 0a 7d 20 49 6e 74 65 72 69 6f 72 52  kid;.} InteriorR
2ef8f 65 61 64 65 72 3b 0a 0a 73 74 61 74 69 63 20 76  eader;..static v
2ef90 6f 69 64 20 69 6e 74 65 72 69 6f 72 52 65 61 64  oid interiorRead
2ef91 65 72 44 65 73 74 72 6f 79 28 49 6e 74 65 72 69  erDestroy(Interi
2ef92 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  orReader *pReade
2ef93 72 29 7b 0a 20 20 64 61 74 61 42 75 66 66 65 72  r){.  dataBuffer
2ef94 44 65 73 74 72 6f 79 28 26 70 52 65 61 64 65 72  Destroy(&pReader
2ef95 2d 3e 74 65 72 6d 29 3b 0a 20 20 53 43 52 41 4d  ->term);.  SCRAM
2ef96 42 4c 45 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a  BLE(pReader);.}.
2ef97 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
2ef98 54 68 65 20 61 73 73 65 72 74 69 6f 6e 73 20 61  The assertions a
2ef99 72 65 20 67 72 65 61 74 2c 20 62 75 74 20 77 68  re great, but wh
2ef9a 61 74 20 69 66 20 77 65 27 72 65 20 69 6e 20 4e  at if we're in N
2ef9b 44 45 42 55 47 0a 2a 2a 20 61 6e 64 20 74 68 65  DEBUG.** and the
2ef9c 20 62 6c 6f 62 20 69 73 20 65 6d 70 74 79 20 6f   blob is empty o
2ef9d 72 20 6f 74 68 65 72 77 69 73 65 20 63 6f 6e 74  r otherwise cont
2ef9e 61 69 6e 73 20 73 75 73 70 65 63 74 20 64 61 74  ains suspect dat
2ef9f 61 3f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  a?.*/.static voi
2efa0 64 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72  d interiorReader
2efa1 49 6e 69 74 28 63 6f 6e 73 74 20 63 68 61 72 20  Init(const char 
2efa2 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74  *pData, int nDat
2efa3 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  a,.             
2efa4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2efa5 20 20 49 6e 74 65 72 69 6f 72 52 65 61 64 65 72    InteriorReader
2efa6 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 69 6e   *pReader){.  in
2efa7 74 20 6e 2c 20 6e 54 65 72 6d 3b 0a 0a 20 20 2f  t n, nTerm;..  /
2efa8 2a 20 52 65 71 75 69 72 65 20 61 74 20 6c 65 61  * Require at lea
2efa9 73 74 20 74 68 65 20 6c 65 61 64 69 6e 67 20 66  st the leading f
2efaa 6c 61 67 20 62 79 74 65 20 2a 2f 0a 20 20 61 73  lag byte */.  as
2efab 73 65 72 74 28 20 6e 44 61 74 61 3e 30 20 29 3b  sert( nData>0 );
2efac 0a 20 20 61 73 73 65 72 74 28 20 70 44 61 74 61  .  assert( pData
2efad 5b 30 5d 21 3d 27 5c 30 27 20 29 3b 0a 0a 20 20  [0]!='\0' );..  
2efae 43 4c 45 41 52 28 70 52 65 61 64 65 72 29 3b 0a  CLEAR(pReader);.
2efaf 0a 20 20 2f 2a 20 44 65 63 6f 64 65 20 74 68 65  .  /* Decode the
2efb0 20 62 61 73 65 20 62 6c 6f 63 6b 69 64 2c 20 61   base blockid, a
2efb1 6e 64 20 73 65 74 20 74 68 65 20 63 75 72 73 6f  nd set the curso
2efb2 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 74  r to the first t
2efb3 65 72 6d 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66 74  erm. */.  n = ft
2efb4 73 33 47 65 74 56 61 72 69 6e 74 28 70 44 61 74  s3GetVarint(pDat
2efb5 61 2b 31 2c 20 26 70 52 65 61 64 65 72 2d 3e 69  a+1, &pReader->i
2efb6 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 61 73 73 65  Blockid);.  asse
2efb7 72 74 28 20 31 2b 6e 3c 3d 6e 44 61 74 61 20 29  rt( 1+n<=nData )
2efb8 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61  ;.  pReader->pDa
2efb9 74 61 20 3d 20 70 44 61 74 61 2b 31 2b 6e 3b 0a  ta = pData+1+n;.
2efba 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61    pReader->nData
2efbb 20 3d 20 6e 44 61 74 61 2d 28 31 2b 6e 29 3b 0a   = nData-(1+n);.
2efbc 0a 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 2d 63  .  /* A single-c
2efbd 68 69 6c 64 20 69 6e 74 65 72 69 6f 72 20 6e 6f  hild interior no
2efbe 64 65 20 28 73 75 63 68 20 61 73 20 77 68 65 6e  de (such as when
2efbf 20 61 20 6c 65 61 66 20 6e 6f 64 65 20 77 61 73   a leaf node was
2efc0 20 74 6f 6f 0a 20 20 2a 2a 20 6c 61 72 67 65 20   too.  ** large 
2efc1 66 6f 72 20 74 68 65 20 73 65 67 6d 65 6e 74 20  for the segment 
2efc2 64 69 72 65 63 74 6f 72 79 29 20 77 6f 6e 27 74  directory) won't
2efc3 20 68 61 76 65 20 61 6e 79 20 74 65 72 6d 73 2e   have any terms.
2efc4 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  .  ** Otherwise,
2efc5 20 64 65 63 6f 64 65 20 74 68 65 20 66 69 72 73   decode the firs
2efc6 74 20 74 65 72 6d 2e 0a 20 20 2a 2f 0a 20 20 69  t term..  */.  i
2efc7 66 28 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  f( pReader->nDat
2efc8 61 3d 3d 30 20 29 7b 0a 20 20 20 20 64 61 74 61  a==0 ){.    data
2efc9 42 75 66 66 65 72 49 6e 69 74 28 26 70 52 65 61  BufferInit(&pRea
2efca 64 65 72 2d 3e 74 65 72 6d 2c 20 30 29 3b 0a 20  der->term, 0);. 
2efcb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 20 3d 20   }else{.    n = 
2efcc 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
2efcd 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20  pReader->pData, 
2efce 26 6e 54 65 72 6d 29 3b 0a 20 20 20 20 64 61 74  &nTerm);.    dat
2efcf 61 42 75 66 66 65 72 49 6e 69 74 28 26 70 52 65  aBufferInit(&pRe
2efd0 61 64 65 72 2d 3e 74 65 72 6d 2c 20 6e 54 65 72  ader->term, nTer
2efd1 6d 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66  m);.    dataBuff
2efd2 65 72 52 65 70 6c 61 63 65 28 26 70 52 65 61 64  erReplace(&pRead
2efd3 65 72 2d 3e 74 65 72 6d 2c 20 70 52 65 61 64 65  er->term, pReade
2efd4 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 6e 54 65 72  r->pData+n, nTer
2efd5 6d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  m);.    assert( 
2efd6 6e 2b 6e 54 65 72 6d 3c 3d 70 52 65 61 64 65 72  n+nTerm<=pReader
2efd7 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 70  ->nData );.    p
2efd8 52 65 61 64 65 72 2d 3e 70 44 61 74 61 20 2b 3d  Reader->pData +=
2efd9 20 6e 2b 6e 54 65 72 6d 3b 0a 20 20 20 20 70 52   n+nTerm;.    pR
2efda 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 2d 3d 20  eader->nData -= 
2efdb 6e 2b 6e 54 65 72 6d 3b 0a 20 20 7d 0a 7d 0a 0a  n+nTerm;.  }.}..
2efdc 73 74 61 74 69 63 20 69 6e 74 20 69 6e 74 65 72  static int inter
2efdd 69 6f 72 52 65 61 64 65 72 41 74 45 6e 64 28 49  iorReaderAtEnd(I
2efde 6e 74 65 72 69 6f 72 52 65 61 64 65 72 20 2a 70  nteriorReader *p
2efdf 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72  Reader){.  retur
2efe0 6e 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e  n pReader->term.
2efe1 6e 44 61 74 61 3d 3d 30 3b 0a 7d 0a 0a 73 74 61  nData==0;.}..sta
2efe2 74 69 63 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  tic sqlite_int64
2efe3 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 43   interiorReaderC
2efe4 75 72 72 65 6e 74 42 6c 6f 63 6b 69 64 28 49 6e  urrentBlockid(In
2efe5 74 65 72 69 6f 72 52 65 61 64 65 72 20 2a 70 52  teriorReader *pR
2efe6 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72 6e  eader){.  return
2efe7 20 70 52 65 61 64 65 72 2d 3e 69 42 6c 6f 63 6b   pReader->iBlock
2efe8 69 64 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  id;.}..static in
2efe9 74 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72  t interiorReader
2efea 54 65 72 6d 42 79 74 65 73 28 49 6e 74 65 72 69  TermBytes(Interi
2efeb 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  orReader *pReade
2efec 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 69  r){.  assert( !i
2efed 6e 74 65 72 69 6f 72 52 65 61 64 65 72 41 74 45  nteriorReaderAtE
2efee 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20  nd(pReader) );. 
2efef 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d   return pReader-
2eff0 3e 74 65 72 6d 2e 6e 44 61 74 61 3b 0a 7d 0a 73  >term.nData;.}.s
2eff1 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
2eff2 20 2a 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72   *interiorReader
2eff3 54 65 72 6d 28 49 6e 74 65 72 69 6f 72 52 65 61  Term(InteriorRea
2eff4 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
2eff5 20 61 73 73 65 72 74 28 20 21 69 6e 74 65 72 69   assert( !interi
2eff6 6f 72 52 65 61 64 65 72 41 74 45 6e 64 28 70 52  orReaderAtEnd(pR
2eff7 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75  eader) );.  retu
2eff8 72 6e 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d  rn pReader->term
2eff9 2e 70 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 20 53 74  .pData;.}../* St
2effa 65 70 20 66 6f 72 77 61 72 64 20 74 6f 20 74 68  ep forward to th
2effb 65 20 6e 65 78 74 20 74 65 72 6d 20 69 6e 20 74  e next term in t
2effc 68 65 20 6e 6f 64 65 2e 20 2a 2f 0a 73 74 61 74  he node. */.stat
2effd 69 63 20 76 6f 69 64 20 69 6e 74 65 72 69 6f 72  ic void interior
2effe 52 65 61 64 65 72 53 74 65 70 28 49 6e 74 65 72  ReaderStep(Inter
2efff 69 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61 64  iorReader *pRead
2f000 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21  er){.  assert( !
2f001 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 41 74  interiorReaderAt
2f002 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a  End(pReader) );.
2f003 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6c 61 73  .  /* If the las
2f004 74 20 74 65 72 6d 20 68 61 73 20 62 65 65 6e 20  t term has been 
2f005 72 65 61 64 2c 20 73 69 67 6e 61 6c 20 65 6f 66  read, signal eof
2f006 2c 20 65 6c 73 65 20 63 6f 6e 73 74 72 75 63 74  , else construct
2f007 20 74 68 65 0a 20 20 2a 2a 20 6e 65 78 74 20 74   the.  ** next t
2f008 65 72 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  erm..  */.  if( 
2f009 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 3d 3d  pReader->nData==
2f00a 30 20 29 7b 0a 20 20 20 20 64 61 74 61 42 75 66  0 ){.    dataBuf
2f00b 66 65 72 52 65 73 65 74 28 26 70 52 65 61 64 65  ferReset(&pReade
2f00c 72 2d 3e 74 65 72 6d 29 3b 0a 20 20 7d 65 6c 73  r->term);.  }els
2f00d 65 7b 0a 20 20 20 20 69 6e 74 20 6e 2c 20 6e 50  e{.    int n, nP
2f00e 72 65 66 69 78 2c 20 6e 53 75 66 66 69 78 3b 0a  refix, nSuffix;.
2f00f 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65 74  .    n = fts3Get
2f010 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65 72  Varint32(pReader
2f011 2d 3e 70 44 61 74 61 2c 20 26 6e 50 72 65 66 69  ->pData, &nPrefi
2f012 78 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73  x);.    n += fts
2f013 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65  3GetVarint32(pRe
2f014 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26  ader->pData+n, &
2f015 6e 53 75 66 66 69 78 29 3b 0a 0a 20 20 20 20 2f  nSuffix);..    /
2f016 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 63  * Truncate the c
2f017 75 72 72 65 6e 74 20 74 65 72 6d 20 61 6e 64 20  urrent term and 
2f018 61 70 70 65 6e 64 20 73 75 66 66 69 78 20 64 61  append suffix da
2f019 74 61 2e 20 2a 2f 0a 20 20 20 20 70 52 65 61 64  ta. */.    pRead
2f01a 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 20 3d  er->term.nData =
2f01b 20 6e 50 72 65 66 69 78 3b 0a 20 20 20 20 64 61   nPrefix;.    da
2f01c 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 26  taBufferAppend(&
2f01d 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2c 20 70  pReader->term, p
2f01e 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c  Reader->pData+n,
2f01f 20 6e 53 75 66 66 69 78 29 3b 0a 0a 20 20 20 20   nSuffix);..    
2f020 61 73 73 65 72 74 28 20 6e 2b 6e 53 75 66 66 69  assert( n+nSuffi
2f021 78 3c 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  x<=pReader->nDat
2f022 61 20 29 3b 0a 20 20 20 20 70 52 65 61 64 65 72  a );.    pReader
2f023 2d 3e 70 44 61 74 61 20 2b 3d 20 6e 2b 6e 53 75  ->pData += n+nSu
2f024 66 66 69 78 3b 0a 20 20 20 20 70 52 65 61 64 65  ffix;.    pReade
2f025 72 2d 3e 6e 44 61 74 61 20 2d 3d 20 6e 2b 6e 53  r->nData -= n+nS
2f026 75 66 66 69 78 3b 0a 20 20 7d 0a 20 20 70 52 65  uffix;.  }.  pRe
2f027 61 64 65 72 2d 3e 69 42 6c 6f 63 6b 69 64 2b 2b  ader->iBlockid++
2f028 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6d 70 61 72 65 20  ;.}../* Compare 
2f029 74 68 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d  the current term
2f02a 20 74 6f 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d   to pTerm[nTerm]
2f02b 2c 20 72 65 74 75 72 6e 69 6e 67 20 73 74 72 63  , returning strc
2f02c 6d 70 2d 73 74 79 6c 65 0a 2a 2a 20 72 65 73 75  mp-style.** resu
2f02d 6c 74 73 2e 20 20 49 66 20 69 73 50 72 65 66 69  lts.  If isPrefi
2f02e 78 2c 20 65 71 75 61 6c 69 74 79 20 6d 65 61 6e  x, equality mean
2f02f 73 20 65 71 75 61 6c 20 74 68 72 6f 75 67 68 20  s equal through 
2f030 6e 54 65 72 6d 20 62 79 74 65 73 2e 0a 2a 2f 0a  nTerm bytes..*/.
2f031 73 74 61 74 69 63 20 69 6e 74 20 69 6e 74 65 72  static int inter
2f032 69 6f 72 52 65 61 64 65 72 54 65 72 6d 43 6d 70  iorReaderTermCmp
2f033 28 49 6e 74 65 72 69 6f 72 52 65 61 64 65 72 20  (InteriorReader 
2f034 2a 70 52 65 61 64 65 72 2c 0a 20 20 20 20 20 20  *pReader,.      
2f035 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f036 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
2f037 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e   char *pTerm, in
2f038 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73 50  t nTerm, int isP
2f039 72 65 66 69 78 29 7b 0a 20 20 63 6f 6e 73 74 20  refix){.  const 
2f03a 63 68 61 72 20 2a 70 52 65 61 64 65 72 54 65 72  char *pReaderTer
2f03b 6d 20 3d 20 69 6e 74 65 72 69 6f 72 52 65 61 64  m = interiorRead
2f03c 65 72 54 65 72 6d 28 70 52 65 61 64 65 72 29 3b  erTerm(pReader);
2f03d 0a 20 20 69 6e 74 20 6e 52 65 61 64 65 72 54 65  .  int nReaderTe
2f03e 72 6d 20 3d 20 69 6e 74 65 72 69 6f 72 52 65 61  rm = interiorRea
2f03f 64 65 72 54 65 72 6d 42 79 74 65 73 28 70 52 65  derTermBytes(pRe
2f040 61 64 65 72 29 3b 0a 20 20 69 6e 74 20 63 2c 20  ader);.  int c, 
2f041 6e 20 3d 20 6e 52 65 61 64 65 72 54 65 72 6d 3c  n = nReaderTerm<
2f042 6e 54 65 72 6d 20 3f 20 6e 52 65 61 64 65 72 54  nTerm ? nReaderT
2f043 65 72 6d 20 3a 20 6e 54 65 72 6d 3b 0a 0a 20 20  erm : nTerm;..  
2f044 69 66 28 20 6e 3d 3d 30 20 29 7b 0a 20 20 20 20  if( n==0 ){.    
2f045 69 66 28 20 6e 52 65 61 64 65 72 54 65 72 6d 3e  if( nReaderTerm>
2f046 30 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20  0 ) return -1;. 
2f047 20 20 20 69 66 28 20 6e 54 65 72 6d 3e 30 20 29     if( nTerm>0 )
2f048 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 72   return 1;.    r
2f049 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20  eturn 0;.  }..  
2f04a 63 20 3d 20 6d 65 6d 63 6d 70 28 70 52 65 61 64  c = memcmp(pRead
2f04b 65 72 54 65 72 6d 2c 20 70 54 65 72 6d 2c 20 6e  erTerm, pTerm, n
2f04c 29 3b 0a 20 20 69 66 28 20 63 21 3d 30 20 29 20  );.  if( c!=0 ) 
2f04d 72 65 74 75 72 6e 20 63 3b 0a 20 20 69 66 28 20  return c;.  if( 
2f04e 69 73 50 72 65 66 69 78 20 26 26 20 6e 3d 3d 6e  isPrefix && n==n
2f04f 54 65 72 6d 20 29 20 72 65 74 75 72 6e 20 30 3b  Term ) return 0;
2f050 0a 20 20 72 65 74 75 72 6e 20 6e 52 65 61 64 65  .  return nReade
2f051 72 54 65 72 6d 20 2d 20 6e 54 65 72 6d 3b 0a 7d  rTerm - nTerm;.}
2f052 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
2f053 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f054 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f055 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f056 2a 2a 2a 2f 0a 2f 2a 20 4c 65 61 66 57 72 69 74  ***/./* LeafWrit
2f057 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f  er is used to co
2f058 6c 6c 65 63 74 20 74 65 72 6d 73 20 61 6e 64 20  llect terms and 
2f059 61 73 73 6f 63 69 61 74 65 64 20 64 6f 63 6c 69  associated docli
2f05a 73 74 20 64 61 74 61 0a 2a 2a 20 69 6e 74 6f 20  st data.** into 
2f05b 6c 65 61 66 20 62 6c 6f 63 6b 73 20 69 6e 20 25  leaf blocks in %
2f05c 5f 73 65 67 6d 65 6e 74 73 20 28 73 65 65 20 74  _segments (see t
2f05d 6f 70 20 6f 66 20 66 69 6c 65 20 66 6f 72 20 66  op of file for f
2f05e 6f 72 6d 61 74 20 69 6e 66 6f 29 2e 0a 2a 2a 20  ormat info)..** 
2f05f 45 78 70 65 63 74 65 64 20 75 73 61 67 65 20 69  Expected usage i
2f060 73 3a 0a 2a 2a 0a 2a 2a 20 4c 65 61 66 57 72 69  s:.**.** LeafWri
2f061 74 65 72 20 77 72 69 74 65 72 3b 0a 2a 2a 20 6c  ter writer;.** l
2f062 65 61 66 57 72 69 74 65 72 49 6e 69 74 28 30 2c  eafWriterInit(0,
2f063 20 30 2c 20 26 77 72 69 74 65 72 29 3b 0a 2a 2a   0, &writer);.**
2f064 20 77 68 69 6c 65 28 20 73 6f 72 74 65 64 5f 74   while( sorted_t
2f065 65 72 6d 73 5f 6c 65 66 74 5f 74 6f 5f 70 72 6f  erms_left_to_pro
2f066 63 65 73 73 20 29 7b 0a 2a 2a 20 20 20 2f 2f 20  cess ){.**   // 
2f067 64 61 74 61 20 69 73 20 64 6f 63 6c 69 73 74 20  data is doclist 
2f068 64 61 74 61 20 66 6f 72 20 74 68 61 74 20 74 65  data for that te
2f069 72 6d 2e 0a 2a 2a 20 20 20 72 63 20 3d 20 6c 65  rm..**   rc = le
2f06a 61 66 57 72 69 74 65 72 53 74 65 70 28 76 2c 20  afWriterStep(v, 
2f06b 26 77 72 69 74 65 72 2c 20 70 54 65 72 6d 2c 20  &writer, pTerm, 
2f06c 6e 54 65 72 6d 2c 20 70 44 61 74 61 2c 20 6e 44  nTerm, pData, nD
2f06d 61 74 61 29 3b 0a 2a 2a 20 20 20 69 66 28 20 72  ata);.**   if( r
2f06e 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
2f06f 6f 74 6f 20 65 72 72 3b 0a 2a 2a 20 7d 0a 2a 2a  oto err;.** }.**
2f070 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72   rc = leafWriter
2f071 46 69 6e 61 6c 69 7a 65 28 76 2c 20 26 77 72 69  Finalize(v, &wri
2f072 74 65 72 29 3b 0a 2a 2a 65 72 72 3a 0a 2a 2a 20  ter);.**err:.** 
2f073 6c 65 61 66 57 72 69 74 65 72 44 65 73 74 72 6f  leafWriterDestro
2f074 79 28 26 77 72 69 74 65 72 29 3b 0a 2a 2a 20 72  y(&writer);.** r
2f075 65 74 75 72 6e 20 72 63 3b 0a 2a 2a 0a 2a 2a 20  eturn rc;.**.** 
2f076 6c 65 61 66 57 72 69 74 65 72 53 74 65 70 28 29  leafWriterStep()
2f077 20 6d 61 79 20 77 72 69 74 65 20 61 20 63 6f 6c   may write a col
2f078 6c 65 63 74 65 64 20 6c 65 61 66 20 6f 75 74 20  lected leaf out 
2f079 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 2e 0a 2a  to %_segments..*
2f07a 2a 20 6c 65 61 66 57 72 69 74 65 72 46 69 6e 61  * leafWriterFina
2f07b 6c 69 7a 65 28 29 20 66 69 6e 69 73 68 65 73 20  lize() finishes 
2f07c 77 72 69 74 69 6e 67 20 61 6e 79 20 62 75 66 66  writing any buff
2f07d 65 72 65 64 20 64 61 74 61 20 61 6e 64 20 73 74  ered data and st
2f07e 6f 72 65 73 0a 2a 2a 20 61 20 72 6f 6f 74 20 6e  ores.** a root n
2f07f 6f 64 65 20 69 6e 20 25 5f 73 65 67 64 69 72 2e  ode in %_segdir.
2f080 20 20 6c 65 61 66 57 72 69 74 65 72 44 65 73 74    leafWriterDest
2f081 72 6f 79 28 29 20 66 72 65 65 73 20 61 6c 6c 20  roy() frees all 
2f082 62 75 66 66 65 72 73 20 61 6e 64 0a 2a 2a 20 49  buffers and.** I
2f083 6e 74 65 72 69 6f 72 57 72 69 74 65 72 73 20 61  nteriorWriters a
2f084 6c 6c 6f 63 61 74 65 64 20 61 73 20 70 61 72 74  llocated as part
2f085 20 6f 66 20 77 72 69 74 69 6e 67 20 74 68 69 73   of writing this
2f086 20 73 65 67 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20   segment..**.** 
2f087 54 4f 44 4f 28 73 68 65 73 73 29 20 44 6f 63 75  TODO(shess) Docu
2f088 6d 65 6e 74 20 6c 65 61 66 57 72 69 74 65 72 53  ment leafWriterS
2f089 74 65 70 4d 65 72 67 65 28 29 2e 0a 2a 2f 0a 0a  tepMerge()..*/..
2f08a 2f 2a 20 50 75 74 20 74 65 72 6d 73 20 77 69 74  /* Put terms wit
2f08b 68 20 64 61 74 61 20 74 68 69 73 20 62 69 67 20  h data this big 
2f08c 69 6e 20 74 68 65 69 72 20 6f 77 6e 20 62 6c 6f  in their own blo
2f08d 63 6b 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ck. */.#define S
2f08e 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 31 30  TANDALONE_MIN 10
2f08f 32 34 0a 0a 2f 2a 20 4b 65 65 70 20 6c 65 61 66  24../* Keep leaf
2f090 20 62 6c 6f 63 6b 73 20 62 65 6c 6f 77 20 74 68   blocks below th
2f091 69 73 20 73 69 7a 65 2e 20 2a 2f 0a 23 64 65 66  is size. */.#def
2f092 69 6e 65 20 4c 45 41 46 5f 4d 41 58 20 32 30 34  ine LEAF_MAX 204
2f093 38 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  8..typedef struc
2f094 74 20 4c 65 61 66 57 72 69 74 65 72 20 7b 0a 20  t LeafWriter {. 
2f095 20 69 6e 74 20 69 4c 65 76 65 6c 3b 0a 20 20 69   int iLevel;.  i
2f096 6e 74 20 69 64 78 3b 0a 20 20 73 71 6c 69 74 65  nt idx;.  sqlite
2f097 5f 69 6e 74 36 34 20 69 53 74 61 72 74 42 6c 6f  _int64 iStartBlo
2f098 63 6b 69 64 3b 20 20 20 20 20 2f 2a 20 6e 65 65  ckid;     /* nee
2f099 64 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 68  ded to create th
2f09a 65 20 72 6f 6f 74 20 69 6e 66 6f 20 2a 2f 0a 20  e root info */. 
2f09b 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45   sqlite_int64 iE
2f09c 6e 64 42 6c 6f 63 6b 69 64 3b 20 20 20 20 20 20  ndBlockid;      
2f09d 20 2f 2a 20 77 68 65 6e 20 77 65 27 72 65 20 64   /* when we're d
2f09e 6f 6e 65 20 77 72 69 74 69 6e 67 2e 20 2a 2f 0a  one writing. */.
2f09f 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 74 65  .  DataBuffer te
2f0a0 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rm;             
2f0a1 20 20 20 2f 2a 20 70 72 65 76 69 6f 75 73 20 65     /* previous e
2f0a2 6e 63 6f 64 65 64 20 74 65 72 6d 20 2a 2f 0a 20  ncoded term */. 
2f0a3 20 44 61 74 61 42 75 66 66 65 72 20 64 61 74 61   DataBuffer data
2f0a4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2f0a5 20 2f 2a 20 65 6e 63 6f 64 69 6e 67 20 62 75 66   /* encoding buf
2f0a6 66 65 72 20 2a 2f 0a 0a 20 20 2f 2a 20 62 79 74  fer */..  /* byt
2f0a7 65 73 20 6f 66 20 66 69 72 73 74 20 74 65 72 6d  es of first term
2f0a8 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20   in the current 
2f0a9 6e 6f 64 65 20 77 68 69 63 68 20 64 69 73 74 69  node which disti
2f0aa 6e 67 75 69 73 68 65 73 20 74 68 61 74 0a 20 20  nguishes that.  
2f0ab 2a 2a 20 74 65 72 6d 20 66 72 6f 6d 20 74 68 65  ** term from the
2f0ac 20 6c 61 73 74 20 74 65 72 6d 20 6f 66 20 74 68   last term of th
2f0ad 65 20 70 72 65 76 69 6f 75 73 20 6e 6f 64 65 2e  e previous node.
2f0ae 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72  .  */.  int nTer
2f0af 6d 44 69 73 74 69 6e 63 74 3b 0a 0a 20 20 49 6e  mDistinct;..  In
2f0b0 74 65 72 69 6f 72 57 72 69 74 65 72 20 70 61 72  teriorWriter par
2f0b1 65 6e 74 57 72 69 74 65 72 3b 20 20 20 20 2f 2a  entWriter;    /*
2f0b2 20 69 66 20 77 65 20 6f 76 65 72 66 6c 6f 77 20   if we overflow 
2f0b3 2a 2f 0a 20 20 69 6e 74 20 68 61 73 5f 70 61 72  */.  int has_par
2f0b4 65 6e 74 3b 0a 7d 20 4c 65 61 66 57 72 69 74 65  ent;.} LeafWrite
2f0b5 72 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  r;..static void 
2f0b6 6c 65 61 66 57 72 69 74 65 72 49 6e 69 74 28 69  leafWriterInit(i
2f0b7 6e 74 20 69 4c 65 76 65 6c 2c 20 69 6e 74 20 69  nt iLevel, int i
2f0b8 64 78 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a  dx, LeafWriter *
2f0b9 70 57 72 69 74 65 72 29 7b 0a 20 20 43 4c 45 41  pWriter){.  CLEA
2f0ba 52 28 70 57 72 69 74 65 72 29 3b 0a 20 20 70 57  R(pWriter);.  pW
2f0bb 72 69 74 65 72 2d 3e 69 4c 65 76 65 6c 20 3d 20  riter->iLevel = 
2f0bc 69 4c 65 76 65 6c 3b 0a 20 20 70 57 72 69 74 65  iLevel;.  pWrite
2f0bd 72 2d 3e 69 64 78 20 3d 20 69 64 78 3b 0a 0a 20  r->idx = idx;.. 
2f0be 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28   dataBufferInit(
2f0bf 26 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 2c 20  &pWriter->term, 
2f0c0 33 32 29 3b 0a 0a 20 20 2f 2a 20 53 74 61 72 74  32);..  /* Start
2f0c1 20 6f 75 74 20 77 69 74 68 20 61 20 72 65 61 73   out with a reas
2f0c2 6f 6e 61 62 6c 79 20 73 69 7a 65 64 20 62 6c 6f  onably sized blo
2f0c3 63 6b 2c 20 74 68 6f 75 67 68 20 69 74 20 63 61  ck, though it ca
2f0c4 6e 20 67 72 6f 77 2e 20 2a 2f 0a 20 20 64 61 74  n grow. */.  dat
2f0c5 61 42 75 66 66 65 72 49 6e 69 74 28 26 70 57 72  aBufferInit(&pWr
2f0c6 69 74 65 72 2d 3e 64 61 74 61 2c 20 4c 45 41 46  iter->data, LEAF
2f0c7 5f 4d 41 58 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65  _MAX);.}..#ifnde
2f0c8 66 20 4e 44 45 42 55 47 0a 2f 2a 20 56 65 72 69  f NDEBUG./* Veri
2f0c9 66 79 20 74 68 61 74 20 74 68 65 20 64 61 74 61  fy that the data
2f0ca 20 69 73 20 72 65 61 64 61 62 6c 65 20 61 73 20   is readable as 
2f0cb 61 20 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a  a leaf node. */.
2f0cc 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65 61 66  static void leaf
2f0cd 4e 6f 64 65 56 61 6c 69 64 61 74 65 28 63 6f 6e  NodeValidate(con
2f0ce 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
2f0cf 69 6e 74 20 6e 44 61 74 61 29 7b 0a 20 20 69 6e  int nData){.  in
2f0d0 74 20 6e 2c 20 69 44 75 6d 6d 79 3b 0a 0a 20 20  t n, iDummy;..  
2f0d1 69 66 28 20 6e 44 61 74 61 3d 3d 30 20 29 20 72  if( nData==0 ) r
2f0d2 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28  eturn;.  assert(
2f0d3 20 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 61 73   nData>0 );.  as
2f0d4 73 65 72 74 28 20 70 44 61 74 61 21 3d 30 20 29  sert( pData!=0 )
2f0d5 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 61 74  ;.  assert( pDat
2f0d6 61 2b 6e 44 61 74 61 3e 70 44 61 74 61 20 29 3b  a+nData>pData );
2f0d7 0a 0a 20 20 2f 2a 20 4d 75 73 74 20 6c 65 61 64  ..  /* Must lead
2f0d8 20 77 69 74 68 20 61 20 76 61 72 69 6e 74 28 30   with a varint(0
2f0d9 29 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47  ) */.  n = fts3G
2f0da 65 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61  etVarint32(pData
2f0db 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73  , &iDummy);.  as
2f0dc 73 65 72 74 28 20 69 44 75 6d 6d 79 3d 3d 30 20  sert( iDummy==0 
2f0dd 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 30  );.  assert( n>0
2f0de 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3c   );.  assert( n<
2f0df 6e 44 61 74 61 20 29 3b 0a 20 20 70 44 61 74 61  nData );.  pData
2f0e0 20 2b 3d 20 6e 3b 0a 20 20 6e 44 61 74 61 20 2d   += n;.  nData -
2f0e1 3d 20 6e 3b 0a 0a 20 20 2f 2a 20 4c 65 61 64 69  = n;..  /* Leadi
2f0e2 6e 67 20 74 65 72 6d 20 6c 65 6e 67 74 68 20 61  ng term length a
2f0e3 6e 64 20 64 61 74 61 20 6d 75 73 74 20 66 69 74  nd data must fit
2f0e4 20 69 6e 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20   in buffer. */. 
2f0e5 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
2f0e6 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44 75  nt32(pData, &iDu
2f0e7 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20  mmy);.  assert( 
2f0e8 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  n>0 );.  assert(
2f0e9 20 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 61   iDummy>0 );.  a
2f0ea 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3e  ssert( n+iDummy>
2f0eb 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  0 );.  assert( n
2f0ec 2b 69 44 75 6d 6d 79 3c 6e 44 61 74 61 20 29 3b  +iDummy<nData );
2f0ed 0a 20 20 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44  .  pData += n+iD
2f0ee 75 6d 6d 79 3b 0a 20 20 6e 44 61 74 61 20 2d 3d  ummy;.  nData -=
2f0ef 20 6e 2b 69 44 75 6d 6d 79 3b 0a 0a 20 20 2f 2a   n+iDummy;..  /*
2f0f0 20 4c 65 61 64 69 6e 67 20 74 65 72 6d 27 73 20   Leading term's 
2f0f1 64 6f 63 6c 69 73 74 20 6c 65 6e 67 74 68 20 61  doclist length a
2f0f2 6e 64 20 64 61 74 61 20 6d 75 73 74 20 66 69 74  nd data must fit
2f0f3 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47  . */.  n = fts3G
2f0f4 65 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61  etVarint32(pData
2f0f5 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73  , &iDummy);.  as
2f0f6 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 61  sert( n>0 );.  a
2f0f7 73 73 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20  ssert( iDummy>0 
2f0f8 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 2b 69  );.  assert( n+i
2f0f9 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 61 73 73  Dummy>0 );.  ass
2f0fa 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c 3d 6e  ert( n+iDummy<=n
2f0fb 44 61 74 61 20 29 3b 0a 20 20 41 53 53 45 52 54  Data );.  ASSERT
2f0fc 5f 56 41 4c 49 44 5f 44 4f 43 4c 49 53 54 28 44  _VALID_DOCLIST(D
2f0fd 4c 5f 44 45 46 41 55 4c 54 2c 20 70 44 61 74 61  L_DEFAULT, pData
2f0fe 2b 6e 2c 20 69 44 75 6d 6d 79 2c 20 4e 55 4c 4c  +n, iDummy, NULL
2f0ff 29 3b 0a 20 20 70 44 61 74 61 20 2b 3d 20 6e 2b  );.  pData += n+
2f100 69 44 75 6d 6d 79 3b 0a 20 20 6e 44 61 74 61 20  iDummy;.  nData 
2f101 2d 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 0a 20 20  -= n+iDummy;..  
2f102 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74  /* Verify that t
2f103 72 61 69 6c 69 6e 67 20 74 65 72 6d 73 20 61 6e  railing terms an
2f104 64 20 64 6f 63 6c 69 73 74 73 20 61 6c 73 6f 20  d doclists also 
2f105 61 72 65 20 72 65 61 64 61 62 6c 65 2e 20 2a 2f  are readable. */
2f106 0a 20 20 77 68 69 6c 65 28 20 6e 44 61 74 61 21  .  while( nData!
2f107 3d 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 66 74  =0 ){.    n = ft
2f108 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 44  s3GetVarint32(pD
2f109 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20  ata, &iDummy);. 
2f10a 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29     assert( n>0 )
2f10b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44  ;.    assert( iD
2f10c 75 6d 6d 79 3e 3d 30 20 29 3b 0a 20 20 20 20 61  ummy>=0 );.    a
2f10d 73 73 65 72 74 28 20 6e 3c 6e 44 61 74 61 20 29  ssert( n<nData )
2f10e 3b 0a 20 20 20 20 70 44 61 74 61 20 2b 3d 20 6e  ;.    pData += n
2f10f 3b 0a 20 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e  ;.    nData -= n
2f110 3b 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65  ;.    n = fts3Ge
2f111 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c  tVarint32(pData,
2f112 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 61   &iDummy);.    a
2f113 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20  ssert( n>0 );.  
2f114 20 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d 79    assert( iDummy
2f115 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  >0 );.    assert
2f116 28 20 6e 2b 69 44 75 6d 6d 79 3e 30 20 29 3b 0a  ( n+iDummy>0 );.
2f117 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44      assert( n+iD
2f118 75 6d 6d 79 3c 6e 44 61 74 61 20 29 3b 0a 20 20  ummy<nData );.  
2f119 20 20 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44 75    pData += n+iDu
2f11a 6d 6d 79 3b 0a 20 20 20 20 6e 44 61 74 61 20 2d  mmy;.    nData -
2f11b 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 0a 20 20 20  = n+iDummy;..   
2f11c 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
2f11d 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44 75  nt32(pData, &iDu
2f11e 6d 6d 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74  mmy);.    assert
2f11f 28 20 6e 3e 30 20 29 3b 0a 20 20 20 20 61 73 73  ( n>0 );.    ass
2f120 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b  ert( iDummy>0 );
2f121 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 69  .    assert( n+i
2f122 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 20 20 61  Dummy>0 );.    a
2f123 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c  ssert( n+iDummy<
2f124 3d 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 41 53  =nData );.    AS
2f125 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c 49  SERT_VALID_DOCLI
2f126 53 54 28 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70  ST(DL_DEFAULT, p
2f127 44 61 74 61 2b 6e 2c 20 69 44 75 6d 6d 79 2c 20  Data+n, iDummy, 
2f128 4e 55 4c 4c 29 3b 0a 20 20 20 20 70 44 61 74 61  NULL);.    pData
2f129 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20   += n+iDummy;.  
2f12a 20 20 6e 44 61 74 61 20 2d 3d 20 6e 2b 69 44 75    nData -= n+iDu
2f12b 6d 6d 79 3b 0a 20 20 7d 0a 7d 0a 23 64 65 66 69  mmy;.  }.}.#defi
2f12c 6e 65 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f  ne ASSERT_VALID_
2f12d 4c 45 41 46 5f 4e 4f 44 45 28 70 2c 20 6e 29 20  LEAF_NODE(p, n) 
2f12e 6c 65 61 66 4e 6f 64 65 56 61 6c 69 64 61 74 65  leafNodeValidate
2f12f 28 70 2c 20 6e 29 0a 23 65 6c 73 65 0a 23 64 65  (p, n).#else.#de
2f130 66 69 6e 65 20 41 53 53 45 52 54 5f 56 41 4c 49  fine ASSERT_VALI
2f131 44 5f 4c 45 41 46 5f 4e 4f 44 45 28 70 2c 20 6e  D_LEAF_NODE(p, n
2f132 29 20 61 73 73 65 72 74 28 20 31 20 29 0a 23 65  ) assert( 1 ).#e
2f133 6e 64 69 66 0a 0a 2f 2a 20 46 6c 75 73 68 20 74  ndif../* Flush t
2f134 68 65 20 63 75 72 72 65 6e 74 20 6c 65 61 66 20  he current leaf 
2f135 6e 6f 64 65 20 74 6f 20 25 5f 73 65 67 6d 65 6e  node to %_segmen
2f136 74 73 2c 20 61 6e 64 20 61 64 64 69 6e 67 20 74  ts, and adding t
2f137 68 65 20 72 65 73 75 6c 74 69 6e 67 0a 2a 2a 20  he resulting.** 
2f138 62 6c 6f 63 6b 69 64 20 61 6e 64 20 74 68 65 20  blockid and the 
2f139 73 74 61 72 74 69 6e 67 20 74 65 72 6d 20 74 6f  starting term to
2f13a 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f   the interior no
2f13b 64 65 20 77 68 69 63 68 20 77 69 6c 6c 0a 2a 2a  de which will.**
2f13c 20 63 6f 6e 74 61 69 6e 20 69 74 2e 0a 2a 2f 0a   contain it..*/.
2f13d 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57  static int leafW
2f13e 72 69 74 65 72 49 6e 74 65 72 6e 61 6c 46 6c 75  riterInternalFlu
2f13f 73 68 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  sh(fulltext_vtab
2f140 20 2a 76 2c 20 4c 65 61 66 57 72 69 74 65 72 20   *v, LeafWriter 
2f141 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20  *pWriter,.      
2f142 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f143 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74               int
2f144 20 69 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74   iData, int nDat
2f145 61 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  a){.  sqlite_int
2f146 36 34 20 69 42 6c 6f 63 6b 69 64 20 3d 20 30 3b  64 iBlockid = 0;
2f147 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  .  const char *p
2f148 53 74 61 72 74 69 6e 67 54 65 72 6d 3b 0a 20 20  StartingTerm;.  
2f149 69 6e 74 20 6e 53 74 61 72 74 69 6e 67 54 65 72  int nStartingTer
2f14a 6d 2c 20 72 63 2c 20 6e 3b 0a 0a 20 20 2f 2a 20  m, rc, n;..  /* 
2f14b 4d 75 73 74 20 68 61 76 65 20 74 68 65 20 6c 65  Must have the le
2f14c 61 64 69 6e 67 20 76 61 72 69 6e 74 28 30 29 20  ading varint(0) 
2f14d 66 6c 61 67 2c 20 70 6c 75 73 20 61 74 20 6c 65  flag, plus at le
2f14e 61 73 74 20 73 6f 6d 65 0a 20 20 2a 2a 20 76 61  ast some.  ** va
2f14f 6c 69 64 2d 6c 6f 6f 6b 69 6e 67 20 64 61 74 61  lid-looking data
2f150 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
2f151 20 6e 44 61 74 61 3e 32 20 29 3b 0a 20 20 61 73   nData>2 );.  as
2f152 73 65 72 74 28 20 69 44 61 74 61 3e 3d 30 20 29  sert( iData>=0 )
2f153 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 61 74  ;.  assert( iDat
2f154 61 2b 6e 44 61 74 61 3c 3d 70 57 72 69 74 65 72  a+nData<=pWriter
2f155 2d 3e 64 61 74 61 2e 6e 44 61 74 61 20 29 3b 0a  ->data.nData );.
2f156 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 4c    ASSERT_VALID_L
2f157 45 41 46 5f 4e 4f 44 45 28 70 57 72 69 74 65 72  EAF_NODE(pWriter
2f158 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 61  ->data.pData+iDa
2f159 74 61 2c 20 6e 44 61 74 61 29 3b 0a 0a 20 20 72  ta, nData);..  r
2f15a 63 20 3d 20 62 6c 6f 63 6b 5f 69 6e 73 65 72 74  c = block_insert
2f15b 28 76 2c 20 70 57 72 69 74 65 72 2d 3e 64 61 74  (v, pWriter->dat
2f15c 61 2e 70 44 61 74 61 2b 69 44 61 74 61 2c 20 6e  a.pData+iData, n
2f15d 44 61 74 61 2c 20 26 69 42 6c 6f 63 6b 69 64 29  Data, &iBlockid)
2f15e 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
2f15f 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
2f160 63 3b 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c  c;.  assert( iBl
2f161 6f 63 6b 69 64 21 3d 30 20 29 3b 0a 0a 20 20 2f  ockid!=0 );..  /
2f162 2a 20 52 65 63 6f 6e 73 74 72 75 63 74 20 74 68  * Reconstruct th
2f163 65 20 66 69 72 73 74 20 74 65 72 6d 20 69 6e 20  e first term in 
2f164 74 68 65 20 6c 65 61 66 20 66 6f 72 20 70 75 72  the leaf for pur
2f165 70 6f 73 65 73 20 6f 66 20 62 75 69 6c 64 69 6e  poses of buildin
2f166 67 0a 20 20 2a 2a 20 74 68 65 20 69 6e 74 65 72  g.  ** the inter
2f167 69 6f 72 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20  ior node..  */. 
2f168 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
2f169 6e 74 33 32 28 70 57 72 69 74 65 72 2d 3e 64 61  nt32(pWriter->da
2f16a 74 61 2e 70 44 61 74 61 2b 69 44 61 74 61 2b 31  ta.pData+iData+1
2f16b 2c 20 26 6e 53 74 61 72 74 69 6e 67 54 65 72 6d  , &nStartingTerm
2f16c 29 3b 0a 20 20 70 53 74 61 72 74 69 6e 67 54 65  );.  pStartingTe
2f16d 72 6d 20 3d 20 70 57 72 69 74 65 72 2d 3e 64 61  rm = pWriter->da
2f16e 74 61 2e 70 44 61 74 61 2b 69 44 61 74 61 2b 31  ta.pData+iData+1
2f16f 2b 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 57  +n;.  assert( pW
2f170 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
2f171 61 3e 69 44 61 74 61 2b 31 2b 6e 2b 6e 53 74 61  a>iData+1+n+nSta
2f172 72 74 69 6e 67 54 65 72 6d 20 29 3b 0a 20 20 61  rtingTerm );.  a
2f173 73 73 65 72 74 28 20 70 57 72 69 74 65 72 2d 3e  ssert( pWriter->
2f174 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3e 30 20  nTermDistinct>0 
2f175 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 57 72  );.  assert( pWr
2f176 69 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74 69  iter->nTermDisti
2f177 6e 63 74 3c 3d 6e 53 74 61 72 74 69 6e 67 54 65  nct<=nStartingTe
2f178 72 6d 20 29 3b 0a 20 20 6e 53 74 61 72 74 69 6e  rm );.  nStartin
2f179 67 54 65 72 6d 20 3d 20 70 57 72 69 74 65 72 2d  gTerm = pWriter-
2f17a 3e 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3b 0a  >nTermDistinct;.
2f17b 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e  .  if( pWriter->
2f17c 68 61 73 5f 70 61 72 65 6e 74 20 29 7b 0a 20 20  has_parent ){.  
2f17d 20 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72    interiorWriter
2f17e 41 70 70 65 6e 64 28 26 70 57 72 69 74 65 72 2d  Append(&pWriter-
2f17f 3e 70 61 72 65 6e 74 57 72 69 74 65 72 2c 0a 20  >parentWriter,. 
2f180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f181 20 20 20 20 20 20 20 20 70 53 74 61 72 74 69 6e          pStartin
2f182 67 54 65 72 6d 2c 20 6e 53 74 61 72 74 69 6e 67  gTerm, nStarting
2f183 54 65 72 6d 2c 20 69 42 6c 6f 63 6b 69 64 29 3b  Term, iBlockid);
2f184 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
2f185 74 65 72 69 6f 72 57 72 69 74 65 72 49 6e 69 74  teriorWriterInit
2f186 28 31 2c 20 70 53 74 61 72 74 69 6e 67 54 65 72  (1, pStartingTer
2f187 6d 2c 20 6e 53 74 61 72 74 69 6e 67 54 65 72 6d  m, nStartingTerm
2f188 2c 20 69 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20  , iBlockid,.    
2f189 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f18a 20 20 20 26 70 57 72 69 74 65 72 2d 3e 70 61 72     &pWriter->par
2f18b 65 6e 74 57 72 69 74 65 72 29 3b 0a 20 20 20 20  entWriter);.    
2f18c 70 57 72 69 74 65 72 2d 3e 68 61 73 5f 70 61 72  pWriter->has_par
2f18d 65 6e 74 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20  ent = 1;.  }..  
2f18e 2f 2a 20 54 72 61 63 6b 20 74 68 65 20 73 70 61  /* Track the spa
2f18f 6e 20 6f 66 20 74 68 69 73 20 73 65 67 6d 65 6e  n of this segmen
2f190 74 27 73 20 6c 65 61 66 20 6e 6f 64 65 73 2e 20  t's leaf nodes. 
2f191 2a 2f 0a 20 20 69 66 28 20 70 57 72 69 74 65 72  */.  if( pWriter
2f192 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69 64 3d 3d 30  ->iEndBlockid==0
2f193 20 29 7b 0a 20 20 20 20 70 57 72 69 74 65 72 2d   ){.    pWriter-
2f194 3e 69 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 70  >iEndBlockid = p
2f195 57 72 69 74 65 72 2d 3e 69 53 74 61 72 74 42 6c  Writer->iStartBl
2f196 6f 63 6b 69 64 20 3d 20 69 42 6c 6f 63 6b 69 64  ockid = iBlockid
2f197 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
2f198 57 72 69 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63  Writer->iEndBloc
2f199 6b 69 64 2b 2b 3b 0a 20 20 20 20 61 73 73 65 72  kid++;.    asser
2f19a 74 28 20 69 42 6c 6f 63 6b 69 64 3d 3d 70 57 72  t( iBlockid==pWr
2f19b 69 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69  iter->iEndBlocki
2f19c 64 20 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  d );.  }..  retu
2f19d 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
2f19e 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57  static int leafW
2f19f 72 69 74 65 72 46 6c 75 73 68 28 66 75 6c 6c 74  riterFlush(fullt
2f1a0 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61  ext_vtab *v, Lea
2f1a1 66 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  fWriter *pWriter
2f1a2 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 6c 65  ){.  int rc = le
2f1a3 61 66 57 72 69 74 65 72 49 6e 74 65 72 6e 61 6c  afWriterInternal
2f1a4 46 6c 75 73 68 28 76 2c 20 70 57 72 69 74 65 72  Flush(v, pWriter
2f1a5 2c 20 30 2c 20 70 57 72 69 74 65 72 2d 3e 64 61  , 0, pWriter->da
2f1a6 74 61 2e 6e 44 61 74 61 29 3b 0a 20 20 69 66 28  ta.nData);.  if(
2f1a7 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2f1a8 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f   return rc;..  /
2f1a9 2a 20 52 65 2d 69 6e 69 74 69 61 6c 69 7a 65 20  * Re-initialize 
2f1aa 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65  the output buffe
2f1ab 72 2e 20 2a 2f 0a 20 20 64 61 74 61 42 75 66 66  r. */.  dataBuff
2f1ac 65 72 52 65 73 65 74 28 26 70 57 72 69 74 65 72  erReset(&pWriter
2f1ad 2d 3e 64 61 74 61 29 3b 0a 0a 20 20 72 65 74 75  ->data);..  retu
2f1ae 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
2f1af 0a 2f 2a 20 46 65 74 63 68 20 74 68 65 20 72 6f  ./* Fetch the ro
2f1b0 6f 74 20 69 6e 66 6f 20 66 6f 72 20 74 68 65 20  ot info for the 
2f1b1 73 65 67 6d 65 6e 74 2e 20 20 49 66 20 74 68 65  segment.  If the
2f1b2 20 65 6e 74 69 72 65 20 6c 65 61 66 20 66 69 74   entire leaf fit
2f1b3 73 0a 2a 2a 20 77 69 74 68 69 6e 20 52 4f 4f 54  s.** within ROOT
2f1b4 5f 4d 41 58 2c 20 74 68 65 6e 20 69 74 20 77 69  _MAX, then it wi
2f1b5 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 64  ll be returned d
2f1b6 69 72 65 63 74 6c 79 2c 20 6f 74 68 65 72 77 69  irectly, otherwi
2f1b7 73 65 20 69 74 0a 2a 2a 20 77 69 6c 6c 20 62 65  se it.** will be
2f1b8 20 66 6c 75 73 68 65 64 20 61 6e 64 20 74 68 65   flushed and the
2f1b9 20 72 6f 6f 74 20 69 6e 66 6f 20 77 69 6c 6c 20   root info will 
2f1ba 62 65 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d  be returned from
2f1bb 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72 69 6f 72   the.** interior
2f1bc 20 6e 6f 64 65 2e 20 20 2a 70 69 45 6e 64 42 6c   node.  *piEndBl
2f1bd 6f 63 6b 69 64 20 69 73 20 73 65 74 20 74 6f 20  ockid is set to 
2f1be 74 68 65 20 62 6c 6f 63 6b 69 64 20 6f 66 20 74  the blockid of t
2f1bf 68 65 20 6c 61 73 74 0a 2a 2a 20 69 6e 74 65 72  he last.** inter
2f1c0 69 6f 72 20 6f 72 20 6c 65 61 66 20 6e 6f 64 65  ior or leaf node
2f1c1 20 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b   written to disk
2f1c2 20 28 30 20 69 66 20 6e 6f 6e 65 20 61 72 65 20   (0 if none are 
2f1c3 77 72 69 74 74 65 6e 20 61 74 0a 2a 2a 20 61 6c  written at.** al
2f1c4 6c 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  l)..*/.static in
2f1c5 74 20 6c 65 61 66 57 72 69 74 65 72 52 6f 6f 74  t leafWriterRoot
2f1c6 49 6e 66 6f 28 66 75 6c 6c 74 65 78 74 5f 76 74  Info(fulltext_vt
2f1c7 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69 74 65  ab *v, LeafWrite
2f1c8 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20  r *pWriter,.    
2f1c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f1ca 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
2f1cb 2a 70 70 52 6f 6f 74 49 6e 66 6f 2c 20 69 6e 74  *ppRootInfo, int
2f1cc 20 2a 70 6e 52 6f 6f 74 49 6e 66 6f 2c 0a 20 20   *pnRootInfo,.  
2f1cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f1ce 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2f1cf 74 65 5f 69 6e 74 36 34 20 2a 70 69 45 6e 64 42  te_int64 *piEndB
2f1d0 6c 6f 63 6b 69 64 29 7b 0a 20 20 2f 2a 20 77 65  lockid){.  /* we
2f1d1 20 63 61 6e 20 66 69 74 20 74 68 65 20 73 65 67   can fit the seg
2f1d2 6d 65 6e 74 20 65 6e 74 69 72 65 6c 79 20 69 6e  ment entirely in
2f1d3 6c 69 6e 65 20 2a 2f 0a 20 20 69 66 28 20 21 70  line */.  if( !p
2f1d4 57 72 69 74 65 72 2d 3e 68 61 73 5f 70 61 72 65  Writer->has_pare
2f1d5 6e 74 20 26 26 20 70 57 72 69 74 65 72 2d 3e 64  nt && pWriter->d
2f1d6 61 74 61 2e 6e 44 61 74 61 3c 52 4f 4f 54 5f 4d  ata.nData<ROOT_M
2f1d7 41 58 20 29 7b 0a 20 20 20 20 2a 70 70 52 6f 6f  AX ){.    *ppRoo
2f1d8 74 49 6e 66 6f 20 3d 20 70 57 72 69 74 65 72 2d  tInfo = pWriter-
2f1d9 3e 64 61 74 61 2e 70 44 61 74 61 3b 0a 20 20 20  >data.pData;.   
2f1da 20 2a 70 6e 52 6f 6f 74 49 6e 66 6f 20 3d 20 70   *pnRootInfo = p
2f1db 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61  Writer->data.nDa
2f1dc 74 61 3b 0a 20 20 20 20 2a 70 69 45 6e 64 42 6c  ta;.    *piEndBl
2f1dd 6f 63 6b 69 64 20 3d 20 30 3b 0a 20 20 20 20 72  ockid = 0;.    r
2f1de 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2f1df 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6c 75 73 68  .  }..  /* Flush
2f1e0 20 72 65 6d 61 69 6e 69 6e 67 20 6c 65 61 66 20   remaining leaf 
2f1e1 64 61 74 61 2e 20 2a 2f 0a 20 20 69 66 28 20 70  data. */.  if( p
2f1e2 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61  Writer->data.nDa
2f1e3 74 61 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  ta>0 ){.    int 
2f1e4 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72 46  rc = leafWriterF
2f1e5 6c 75 73 68 28 76 2c 20 70 57 72 69 74 65 72 29  lush(v, pWriter)
2f1e6 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
2f1e7 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
2f1e8 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57   rc;.  }..  /* W
2f1e9 65 20 6d 75 73 74 20 68 61 76 65 20 66 6c 75 73  e must have flus
2f1ea 68 65 64 20 61 20 6c 65 61 66 20 61 74 20 73 6f  hed a leaf at so
2f1eb 6d 65 20 70 6f 69 6e 74 2e 20 2a 2f 0a 20 20 61  me point. */.  a
2f1ec 73 73 65 72 74 28 20 70 57 72 69 74 65 72 2d 3e  ssert( pWriter->
2f1ed 68 61 73 5f 70 61 72 65 6e 74 20 29 3b 0a 0a 20  has_parent );.. 
2f1ee 20 2f 2a 20 54 65 6e 61 74 69 76 65 6c 79 20 73   /* Tenatively s
2f1ef 65 74 20 74 68 65 20 65 6e 64 20 6c 65 61 66 20  et the end leaf 
2f1f0 62 6c 6f 63 6b 69 64 20 61 73 20 74 68 65 20 65  blockid as the e
2f1f1 6e 64 20 62 6c 6f 63 6b 69 64 2e 20 20 49 66 20  nd blockid.  If 
2f1f2 74 68 65 0a 20 20 2a 2a 20 69 6e 74 65 72 69 6f  the.  ** interio
2f1f3 72 20 6e 6f 64 65 20 63 61 6e 20 62 65 20 72 65  r node can be re
2f1f4 74 75 72 6e 65 64 20 69 6e 6c 69 6e 65 2c 20 74  turned inline, t
2f1f5 68 69 73 20 77 69 6c 6c 20 62 65 20 74 68 65 20  his will be the 
2f1f6 66 69 6e 61 6c 0a 20 20 2a 2a 20 62 6c 6f 63 6b  final.  ** block
2f1f7 69 64 2c 20 6f 74 68 65 72 77 69 73 65 20 69 74  id, otherwise it
2f1f8 20 77 69 6c 6c 20 62 65 20 6f 76 65 72 77 72 69   will be overwri
2f1f9 74 74 65 6e 20 62 79 0a 20 20 2a 2a 20 69 6e 74  tten by.  ** int
2f1fa 65 72 69 6f 72 57 72 69 74 65 72 52 6f 6f 74 49  eriorWriterRootI
2f1fb 6e 66 6f 28 29 2e 0a 20 20 2a 2f 0a 20 20 2a 70  nfo()..  */.  *p
2f1fc 69 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 70 57  iEndBlockid = pW
2f1fd 72 69 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b  riter->iEndBlock
2f1fe 69 64 3b 0a 0a 20 20 72 65 74 75 72 6e 20 69 6e  id;..  return in
2f1ff 74 65 72 69 6f 72 57 72 69 74 65 72 52 6f 6f 74  teriorWriterRoot
2f200 49 6e 66 6f 28 76 2c 20 26 70 57 72 69 74 65 72  Info(v, &pWriter
2f201 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 2c 0a  ->parentWriter,.
2f202 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f203 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f204 70 70 52 6f 6f 74 49 6e 66 6f 2c 20 70 6e 52 6f  ppRootInfo, pnRo
2f205 6f 74 49 6e 66 6f 2c 20 70 69 45 6e 64 42 6c 6f  otInfo, piEndBlo
2f206 63 6b 69 64 29 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6c  ckid);.}../* Col
2f207 6c 65 63 74 20 74 68 65 20 72 6f 6f 74 49 6e 66  lect the rootInf
2f208 6f 20 64 61 74 61 20 61 6e 64 20 73 74 6f 72 65  o data and store
2f209 20 69 74 20 69 6e 74 6f 20 74 68 65 20 73 65 67   it into the seg
2f20a 6d 65 6e 74 20 64 69 72 65 63 74 6f 72 79 2e 0a  ment directory..
2f20b 2a 2a 20 54 68 69 73 20 68 61 73 20 74 68 65 20  ** This has the 
2f20c 65 66 66 65 63 74 20 6f 66 20 66 6c 75 73 68 69  effect of flushi
2f20d 6e 67 20 74 68 65 20 73 65 67 6d 65 6e 74 27 73  ng the segment's
2f20e 20 6c 65 61 66 20 64 61 74 61 20 74 6f 0a 2a 2a   leaf data to.**
2f20f 20 25 5f 73 65 67 6d 65 6e 74 73 2c 20 61 6e 64   %_segments, and
2f210 20 61 6c 73 6f 20 66 6c 75 73 68 69 6e 67 20 61   also flushing a
2f211 6e 79 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  ny interior node
2f212 73 20 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 2e  s to %_segments.
2f213 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
2f214 65 61 66 57 72 69 74 65 72 46 69 6e 61 6c 69 7a  eafWriterFinaliz
2f215 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  e(fulltext_vtab 
2f216 2a 76 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a  *v, LeafWriter *
2f217 70 57 72 69 74 65 72 29 7b 0a 20 20 73 71 6c 69  pWriter){.  sqli
2f218 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c 6f  te_int64 iEndBlo
2f219 63 6b 69 64 3b 0a 20 20 63 68 61 72 20 2a 70 52  ckid;.  char *pR
2f21a 6f 6f 74 49 6e 66 6f 3b 0a 20 20 69 6e 74 20 72  ootInfo;.  int r
2f21b 63 2c 20 6e 52 6f 6f 74 49 6e 66 6f 3b 0a 0a 20  c, nRootInfo;.. 
2f21c 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72   rc = leafWriter
2f21d 52 6f 6f 74 49 6e 66 6f 28 76 2c 20 70 57 72 69  RootInfo(v, pWri
2f21e 74 65 72 2c 20 26 70 52 6f 6f 74 49 6e 66 6f 2c  ter, &pRootInfo,
2f21f 20 26 6e 52 6f 6f 74 49 6e 66 6f 2c 20 26 69 45   &nRootInfo, &iE
2f220 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66  ndBlockid);.  if
2f221 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2f222 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
2f223 2f 2a 20 44 6f 6e 27 74 20 62 6f 74 68 65 72 20  /* Don't bother 
2f224 73 74 6f 72 69 6e 67 20 61 6e 20 65 6e 74 69 72  storing an entir
2f225 65 6c 79 20 65 6d 70 74 79 20 73 65 67 6d 65 6e  ely empty segmen
2f226 74 2e 20 2a 2f 0a 20 20 69 66 28 20 69 45 6e 64  t. */.  if( iEnd
2f227 42 6c 6f 63 6b 69 64 3d 3d 30 20 26 26 20 6e 52  Blockid==0 && nR
2f228 6f 6f 74 49 6e 66 6f 3d 3d 30 20 29 20 72 65 74  ootInfo==0 ) ret
2f229 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  urn SQLITE_OK;..
2f22a 20 20 72 65 74 75 72 6e 20 73 65 67 64 69 72 5f    return segdir_
2f22b 73 65 74 28 76 2c 20 70 57 72 69 74 65 72 2d 3e  set(v, pWriter->
2f22c 69 4c 65 76 65 6c 2c 20 70 57 72 69 74 65 72 2d  iLevel, pWriter-
2f22d 3e 69 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20  >idx,.          
2f22e 20 20 20 20 20 20 20 20 20 20 70 57 72 69 74 65            pWrite
2f22f 72 2d 3e 69 53 74 61 72 74 42 6c 6f 63 6b 69 64  r->iStartBlockid
2f230 2c 20 70 57 72 69 74 65 72 2d 3e 69 45 6e 64 42  , pWriter->iEndB
2f231 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20 20  lockid,.        
2f232 20 20 20 20 20 20 20 20 20 20 20 20 69 45 6e 64              iEnd
2f233 42 6c 6f 63 6b 69 64 2c 20 70 52 6f 6f 74 49 6e  Blockid, pRootIn
2f234 66 6f 2c 20 6e 52 6f 6f 74 49 6e 66 6f 29 3b 0a  fo, nRootInfo);.
2f235 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c  }..static void l
2f236 65 61 66 57 72 69 74 65 72 44 65 73 74 72 6f 79  eafWriterDestroy
2f237 28 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57 72  (LeafWriter *pWr
2f238 69 74 65 72 29 7b 0a 20 20 69 66 28 20 70 57 72  iter){.  if( pWr
2f239 69 74 65 72 2d 3e 68 61 73 5f 70 61 72 65 6e 74  iter->has_parent
2f23a 20 29 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65   ) interiorWrite
2f23b 72 44 65 73 74 72 6f 79 28 26 70 57 72 69 74 65  rDestroy(&pWrite
2f23c 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 29  r->parentWriter)
2f23d 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65  ;.  dataBufferDe
2f23e 73 74 72 6f 79 28 26 70 57 72 69 74 65 72 2d 3e  stroy(&pWriter->
2f23f 74 65 72 6d 29 3b 0a 20 20 64 61 74 61 42 75 66  term);.  dataBuf
2f240 66 65 72 44 65 73 74 72 6f 79 28 26 70 57 72 69  ferDestroy(&pWri
2f241 74 65 72 2d 3e 64 61 74 61 29 3b 0a 7d 0a 0a 2f  ter->data);.}../
2f242 2a 20 45 6e 63 6f 64 65 20 61 20 74 65 72 6d 20  * Encode a term 
2f243 69 6e 74 6f 20 74 68 65 20 6c 65 61 66 57 72 69  into the leafWri
2f244 74 65 72 2c 20 64 65 6c 74 61 2d 65 6e 63 6f 64  ter, delta-encod
2f245 69 6e 67 20 61 73 20 61 70 70 72 6f 70 72 69 61  ing as appropria
2f246 74 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 73 20 74  te..** Returns t
2f247 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65  he length of the
2f248 20 6e 65 77 20 74 65 72 6d 20 77 68 69 63 68 20   new term which 
2f249 64 69 73 74 69 6e 67 75 69 73 68 65 73 20 69 74  distinguishes it
2f24a 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 70 72 65   from the.** pre
2f24b 76 69 6f 75 73 20 74 65 72 6d 2c 20 77 68 69 63  vious term, whic
2f24c 68 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  h can be used to
2f24d 20 73 65 74 20 6e 54 65 72 6d 44 69 73 74 69 6e   set nTermDistin
2f24e 63 74 20 77 68 65 6e 20 61 20 6e 6f 64 65 0a 2a  ct when a node.*
2f24f 2a 20 62 6f 75 6e 64 61 72 79 20 69 73 20 63 72  * boundary is cr
2f250 6f 73 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ossed..*/.static
2f251 20 69 6e 74 20 6c 65 61 66 57 72 69 74 65 72 45   int leafWriterE
2f252 6e 63 6f 64 65 54 65 72 6d 28 4c 65 61 66 57 72  ncodeTerm(LeafWr
2f253 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20  iter *pWriter,. 
2f254 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f255 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
2f256 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d  onst char *pTerm
2f257 2c 20 69 6e 74 20 6e 54 65 72 6d 29 7b 0a 20 20  , int nTerm){.  
2f258 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41  char c[VARINT_MA
2f259 58 2b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20  X+VARINT_MAX];. 
2f25a 20 69 6e 74 20 6e 2c 20 6e 50 72 65 66 69 78 20   int n, nPrefix 
2f25b 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  = 0;..  assert( 
2f25c 6e 54 65 72 6d 3e 30 20 29 3b 0a 20 20 77 68 69  nTerm>0 );.  whi
2f25d 6c 65 28 20 6e 50 72 65 66 69 78 3c 70 57 72 69  le( nPrefix<pWri
2f25e 74 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 20  ter->term.nData 
2f25f 26 26 0a 20 20 20 20 20 20 20 20 20 70 54 65 72  &&.         pTer
2f260 6d 5b 6e 50 72 65 66 69 78 5d 3d 3d 70 57 72 69  m[nPrefix]==pWri
2f261 74 65 72 2d 3e 74 65 72 6d 2e 70 44 61 74 61 5b  ter->term.pData[
2f262 6e 50 72 65 66 69 78 5d 20 29 7b 0a 20 20 20 20  nPrefix] ){.    
2f263 6e 50 72 65 66 69 78 2b 2b 3b 0a 20 20 20 20 2f  nPrefix++;.    /
2f264 2a 20 46 61 69 6c 69 6e 67 20 74 68 69 73 20 69  * Failing this i
2f265 6d 70 6c 69 65 73 20 74 68 61 74 20 74 68 65 20  mplies that the 
2f266 74 65 72 6d 73 20 77 65 72 65 6e 27 74 20 69 6e  terms weren't in
2f267 20 6f 72 64 65 72 2e 20 2a 2f 0a 20 20 20 20 61   order. */.    a
2f268 73 73 65 72 74 28 20 6e 50 72 65 66 69 78 3c 6e  ssert( nPrefix<n
2f269 54 65 72 6d 20 29 3b 0a 20 20 7d 0a 0a 20 20 69  Term );.  }..  i
2f26a 66 28 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61  f( pWriter->data
2f26b 2e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20  .nData==0 ){.   
2f26c 20 2f 2a 20 45 6e 63 6f 64 65 20 74 68 65 20 6e   /* Encode the n
2f26d 6f 64 65 20 68 65 61 64 65 72 20 61 6e 64 20 6c  ode header and l
2f26e 65 61 64 69 6e 67 20 74 65 72 6d 20 61 73 3a 0a  eading term as:.
2f26f 20 20 20 20 2a 2a 20 20 76 61 72 69 6e 74 28 30      **  varint(0
2f270 29 0a 20 20 20 20 2a 2a 20 20 76 61 72 69 6e 74  ).    **  varint
2f271 28 6e 54 65 72 6d 29 0a 20 20 20 20 2a 2a 20 20  (nTerm).    **  
2f272 63 68 61 72 20 70 54 65 72 6d 5b 6e 54 65 72 6d  char pTerm[nTerm
2f273 5d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e 20 3d  ].    */.    n =
2f274 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
2f275 2c 20 27 5c 30 27 29 3b 0a 20 20 20 20 6e 20 2b  , '\0');.    n +
2f276 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28  = fts3PutVarint(
2f277 63 2b 6e 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20  c+n, nTerm);.   
2f278 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
2f279 64 32 28 26 70 57 72 69 74 65 72 2d 3e 64 61 74  d2(&pWriter->dat
2f27a 61 2c 20 63 2c 20 6e 2c 20 70 54 65 72 6d 2c 20  a, c, n, pTerm, 
2f27b 6e 54 65 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b  nTerm);.  }else{
2f27c 0a 20 20 20 20 2f 2a 20 44 65 6c 74 61 2d 65 6e  .    /* Delta-en
2f27d 63 6f 64 65 20 74 68 65 20 74 65 72 6d 20 61 73  code the term as
2f27e 3a 0a 20 20 20 20 2a 2a 20 20 76 61 72 69 6e 74  :.    **  varint
2f27f 28 6e 50 72 65 66 69 78 29 0a 20 20 20 20 2a 2a  (nPrefix).    **
2f280 20 20 76 61 72 69 6e 74 28 6e 53 75 66 66 69 78    varint(nSuffix
2f281 29 0a 20 20 20 20 2a 2a 20 20 63 68 61 72 20 70  ).    **  char p
2f282 54 65 72 6d 53 75 66 66 69 78 5b 6e 53 75 66 66  TermSuffix[nSuff
2f283 69 78 5d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e  ix].    */.    n
2f284 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74   = fts3PutVarint
2f285 28 63 2c 20 6e 50 72 65 66 69 78 29 3b 0a 20 20  (c, nPrefix);.  
2f286 20 20 6e 20 2b 3d 20 66 74 73 33 50 75 74 56 61    n += fts3PutVa
2f287 72 69 6e 74 28 63 2b 6e 2c 20 6e 54 65 72 6d 2d  rint(c+n, nTerm-
2f288 6e 50 72 65 66 69 78 29 3b 0a 20 20 20 20 64 61  nPrefix);.    da
2f289 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32 28  taBufferAppend2(
2f28a 26 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2c 20  &pWriter->data, 
2f28b 63 2c 20 6e 2c 20 70 54 65 72 6d 2b 6e 50 72 65  c, n, pTerm+nPre
2f28c 66 69 78 2c 20 6e 54 65 72 6d 2d 6e 50 72 65 66  fix, nTerm-nPref
2f28d 69 78 29 3b 0a 20 20 7d 0a 20 20 64 61 74 61 42  ix);.  }.  dataB
2f28e 75 66 66 65 72 52 65 70 6c 61 63 65 28 26 70 57  ufferReplace(&pW
2f28f 72 69 74 65 72 2d 3e 74 65 72 6d 2c 20 70 54 65  riter->term, pTe
2f290 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20 72  rm, nTerm);..  r
2f291 65 74 75 72 6e 20 6e 50 72 65 66 69 78 2b 31 3b  eturn nPrefix+1;
2f292 0a 7d 0a 0a 2f 2a 20 55 73 65 64 20 74 6f 20 61  .}../* Used to a
2f293 76 6f 69 64 20 61 20 6d 65 6d 6d 6f 76 65 20 77  void a memmove w
2f294 68 65 6e 20 61 20 6c 61 72 67 65 20 61 6d 6f 75  hen a large amou
2f295 6e 74 20 6f 66 20 64 6f 63 6c 69 73 74 20 64 61  nt of doclist da
2f296 74 61 20 69 73 20 69 6e 0a 2a 2a 20 74 68 65 20  ta is in.** the 
2f297 62 75 66 66 65 72 2e 20 20 54 68 69 73 20 63 6f  buffer.  This co
2f298 6e 73 74 72 75 63 74 73 20 61 20 6e 6f 64 65 20  nstructs a node 
2f299 61 6e 64 20 74 65 72 6d 20 68 65 61 64 65 72 20  and term header 
2f29a 62 65 66 6f 72 65 0a 2a 2a 20 69 44 6f 63 6c 69  before.** iDocli
2f29b 73 74 44 61 74 61 20 61 6e 64 20 66 6c 75 73 68  stData and flush
2f29c 65 73 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67  es the resulting
2f29d 20 63 6f 6d 70 6c 65 74 65 20 6e 6f 64 65 20 75   complete node u
2f29e 73 69 6e 67 0a 2a 2a 20 6c 65 61 66 57 72 69 74  sing.** leafWrit
2f29f 65 72 49 6e 74 65 72 6e 61 6c 46 6c 75 73 68 28  erInternalFlush(
2f2a0 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
2f2a1 20 6c 65 61 66 57 72 69 74 65 72 49 6e 6c 69 6e   leafWriterInlin
2f2a2 65 46 6c 75 73 68 28 66 75 6c 6c 74 65 78 74 5f  eFlush(fulltext_
2f2a3 76 74 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69  vtab *v, LeafWri
2f2a4 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20  ter *pWriter,.  
2f2a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f2a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
2f2a7 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d  onst char *pTerm
2f2a8 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 0a 20 20 20  , int nTerm,.   
2f2a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f2aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
2f2ab 74 20 69 44 6f 63 6c 69 73 74 44 61 74 61 29 7b  t iDoclistData){
2f2ac 0a 20 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54  .  char c[VARINT
2f2ad 5f 4d 41 58 2b 56 41 52 49 4e 54 5f 4d 41 58 5d  _MAX+VARINT_MAX]
2f2ae 3b 0a 20 20 69 6e 74 20 69 44 61 74 61 2c 20 6e  ;.  int iData, n
2f2af 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74   = fts3PutVarint
2f2b0 28 63 2c 20 30 29 3b 0a 20 20 6e 20 2b 3d 20 66  (c, 0);.  n += f
2f2b1 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b 6e  ts3PutVarint(c+n
2f2b2 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20 2f 2a 20  , nTerm);..  /* 
2f2b3 54 68 65 72 65 20 73 68 6f 75 6c 64 20 61 6c 77  There should alw
2f2b4 61 79 73 20 62 65 20 72 6f 6f 6d 20 66 6f 72 20  ays be room for 
2f2b5 74 68 65 20 68 65 61 64 65 72 2e 20 20 45 76 65  the header.  Eve
2f2b6 6e 20 69 66 20 70 54 65 72 6d 20 73 68 61 72 65  n if pTerm share
2f2b7 64 0a 20 20 2a 2a 20 61 20 73 75 62 73 74 61 6e  d.  ** a substan
2f2b8 74 69 61 6c 20 70 72 65 66 69 78 20 77 69 74 68  tial prefix with
2f2b9 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 74 65   the previous te
2f2ba 72 6d 2c 20 74 68 65 20 65 6e 74 69 72 65 20 70  rm, the entire p
2f2bb 72 65 66 69 78 0a 20 20 2a 2a 20 63 6f 75 6c 64  refix.  ** could
2f2bc 20 62 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20   be constructed 
2f2bd 66 72 6f 6d 20 65 61 72 6c 69 65 72 20 64 61 74  from earlier dat
2f2be 61 20 69 6e 20 74 68 65 20 64 6f 63 6c 69 73 74  a in the doclist
2f2bf 2c 20 73 6f 20 74 68 65 72 65 0a 20 20 2a 2a 20  , so there.  ** 
2f2c0 73 68 6f 75 6c 64 20 62 65 20 72 6f 6f 6d 2e 0a  should be room..
2f2c1 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69    */.  assert( i
2f2c2 44 6f 63 6c 69 73 74 44 61 74 61 3e 3d 6e 2b 6e  DoclistData>=n+n
2f2c3 54 65 72 6d 20 29 3b 0a 0a 20 20 69 44 61 74 61  Term );..  iData
2f2c4 20 3d 20 69 44 6f 63 6c 69 73 74 44 61 74 61 2d   = iDoclistData-
2f2c5 28 6e 2b 6e 54 65 72 6d 29 3b 0a 20 20 6d 65 6d  (n+nTerm);.  mem
2f2c6 63 70 79 28 70 57 72 69 74 65 72 2d 3e 64 61 74  cpy(pWriter->dat
2f2c7 61 2e 70 44 61 74 61 2b 69 44 61 74 61 2c 20 63  a.pData+iData, c
2f2c8 2c 20 6e 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70  , n);.  memcpy(p
2f2c9 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61  Writer->data.pDa
2f2ca 74 61 2b 69 44 61 74 61 2b 6e 2c 20 70 54 65 72  ta+iData+n, pTer
2f2cb 6d 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20 72 65  m, nTerm);..  re
2f2cc 74 75 72 6e 20 6c 65 61 66 57 72 69 74 65 72 49  turn leafWriterI
2f2cd 6e 74 65 72 6e 61 6c 46 6c 75 73 68 28 76 2c 20  nternalFlush(v, 
2f2ce 70 57 72 69 74 65 72 2c 20 69 44 61 74 61 2c 20  pWriter, iData, 
2f2cf 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44  pWriter->data.nD
2f2d0 61 74 61 2d 69 44 61 74 61 29 3b 0a 7d 0a 0a 2f  ata-iData);.}../
2f2d1 2a 20 50 75 73 68 20 70 54 65 72 6d 5b 6e 54 65  * Push pTerm[nTe
2f2d2 72 6d 5d 20 61 6c 6f 6e 67 20 77 69 74 68 20 74  rm] along with t
2f2d3 68 65 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20  he doclist data 
2f2d4 74 6f 20 74 68 65 20 6c 65 61 66 20 6c 61 79 65  to the leaf laye
2f2d5 72 20 6f 66 0a 2a 2a 20 25 5f 73 65 67 6d 65 6e  r of.** %_segmen
2f2d6 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ts..*/.static in
2f2d7 74 20 6c 65 61 66 57 72 69 74 65 72 53 74 65 70  t leafWriterStep
2f2d8 4d 65 72 67 65 28 66 75 6c 6c 74 65 78 74 5f 76  Merge(fulltext_v
2f2d9 74 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69 74  tab *v, LeafWrit
2f2da 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20  er *pWriter,.   
2f2db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f2dc 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
2f2dd 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69  t char *pTerm, i
2f2de 6e 74 20 6e 54 65 72 6d 2c 0a 20 20 20 20 20 20  nt nTerm,.      
2f2df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f2e0 20 20 20 20 20 20 20 20 20 44 4c 52 65 61 64 65           DLReade
2f2e1 72 20 2a 70 52 65 61 64 65 72 73 2c 20 69 6e 74  r *pReaders, int
2f2e2 20 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 63 68   nReaders){.  ch
2f2e3 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 2b  ar c[VARINT_MAX+
2f2e4 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69  VARINT_MAX];.  i
2f2e5 6e 74 20 69 54 65 72 6d 44 61 74 61 20 3d 20 70  nt iTermData = p
2f2e6 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61  Writer->data.nDa
2f2e7 74 61 2c 20 69 44 6f 63 6c 69 73 74 44 61 74 61  ta, iDoclistData
2f2e8 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 44 61 74 61  ;.  int i, nData
2f2e9 2c 20 6e 2c 20 6e 41 63 74 75 61 6c 44 61 74 61  , n, nActualData
2f2ea 2c 20 6e 41 63 74 75 61 6c 2c 20 72 63 2c 20 6e  , nActual, rc, n
2f2eb 54 65 72 6d 44 69 73 74 69 6e 63 74 3b 0a 0a 20  TermDistinct;.. 
2f2ec 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 4c 45   ASSERT_VALID_LE
2f2ed 41 46 5f 4e 4f 44 45 28 70 57 72 69 74 65 72 2d  AF_NODE(pWriter-
2f2ee 3e 64 61 74 61 2e 70 44 61 74 61 2c 20 70 57 72  >data.pData, pWr
2f2ef 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61  iter->data.nData
2f2f0 29 3b 0a 20 20 6e 54 65 72 6d 44 69 73 74 69 6e  );.  nTermDistin
2f2f1 63 74 20 3d 20 6c 65 61 66 57 72 69 74 65 72 45  ct = leafWriterE
2f2f2 6e 63 6f 64 65 54 65 72 6d 28 70 57 72 69 74 65  ncodeTerm(pWrite
2f2f3 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29  r, pTerm, nTerm)
2f2f4 3b 0a 0a 20 20 2f 2a 20 52 65 6d 65 6d 62 65 72  ;..  /* Remember
2f2f5 20 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 20 69   nTermDistinct i
2f2f6 66 20 6f 70 65 6e 69 6e 67 20 61 20 6e 65 77 20  f opening a new 
2f2f7 6e 6f 64 65 2e 20 2a 2f 0a 20 20 69 66 28 20 69  node. */.  if( i
2f2f8 54 65 72 6d 44 61 74 61 3d 3d 30 20 29 20 70 57  TermData==0 ) pW
2f2f9 72 69 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74  riter->nTermDist
2f2fa 69 6e 63 74 20 3d 20 6e 54 65 72 6d 44 69 73 74  inct = nTermDist
2f2fb 69 6e 63 74 3b 0a 0a 20 20 69 44 6f 63 6c 69 73  inct;..  iDoclis
2f2fc 74 44 61 74 61 20 3d 20 70 57 72 69 74 65 72 2d  tData = pWriter-
2f2fd 3e 64 61 74 61 2e 6e 44 61 74 61 3b 0a 0a 20 20  >data.nData;..  
2f2fe 2f 2a 20 45 73 74 69 6d 61 74 65 20 74 68 65 20  /* Estimate the 
2f2ff 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 6d 65  length of the me
2f300 72 67 65 64 20 64 6f 63 6c 69 73 74 20 73 6f 20  rged doclist so 
2f301 77 65 20 63 61 6e 20 6c 65 61 76 65 20 73 70 61  we can leave spa
2f302 63 65 0a 20 20 2a 2a 20 74 6f 20 65 6e 63 6f 64  ce.  ** to encod
2f303 65 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  e it..  */.  for
2f304 28 69 3d 30 2c 20 6e 44 61 74 61 3d 30 3b 20 69  (i=0, nData=0; i
2f305 3c 6e 52 65 61 64 65 72 73 3b 20 69 2b 2b 29 7b  <nReaders; i++){
2f306 0a 20 20 20 20 6e 44 61 74 61 20 2b 3d 20 64 6c  .    nData += dl
2f307 72 41 6c 6c 44 61 74 61 42 79 74 65 73 28 26 70  rAllDataBytes(&p
2f308 52 65 61 64 65 72 73 5b 69 5d 29 3b 0a 20 20 7d  Readers[i]);.  }
2f309 0a 20 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61  .  n = fts3PutVa
2f30a 72 69 6e 74 28 63 2c 20 6e 44 61 74 61 29 3b 0a  rint(c, nData);.
2f30b 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
2f30c 6e 64 28 26 70 57 72 69 74 65 72 2d 3e 64 61 74  nd(&pWriter->dat
2f30d 61 2c 20 63 2c 20 6e 29 3b 0a 0a 20 20 64 6f 63  a, c, n);..  doc
2f30e 4c 69 73 74 4d 65 72 67 65 28 26 70 57 72 69 74  ListMerge(&pWrit
2f30f 65 72 2d 3e 64 61 74 61 2c 20 70 52 65 61 64 65  er->data, pReade
2f310 72 73 2c 20 6e 52 65 61 64 65 72 73 29 3b 0a 20  rs, nReaders);. 
2f311 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f   ASSERT_VALID_DO
2f312 43 4c 49 53 54 28 44 4c 5f 44 45 46 41 55 4c 54  CLIST(DL_DEFAULT
2f313 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2f314 20 20 20 20 20 20 20 20 20 70 57 72 69 74 65 72           pWriter
2f315 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 6f  ->data.pData+iDo
2f316 63 6c 69 73 74 44 61 74 61 2b 6e 2c 0a 20 20 20  clistData+n,.   
2f317 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f318 20 20 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74      pWriter->dat
2f319 61 2e 6e 44 61 74 61 2d 69 44 6f 63 6c 69 73 74  a.nData-iDoclist
2f31a 44 61 74 61 2d 6e 2c 20 4e 55 4c 4c 29 3b 0a 0a  Data-n, NULL);..
2f31b 20 20 2f 2a 20 54 68 65 20 61 63 74 75 61 6c 20    /* The actual 
2f31c 61 6d 6f 75 6e 74 20 6f 66 20 64 6f 63 6c 69 73  amount of doclis
2f31d 74 20 64 61 74 61 20 61 74 20 74 68 69 73 20 70  t data at this p
2f31e 6f 69 6e 74 20 63 6f 75 6c 64 20 62 65 20 73 6d  oint could be sm
2f31f 61 6c 6c 65 72 0a 20 20 2a 2a 20 74 68 61 6e 20  aller.  ** than 
2f320 74 68 65 20 6c 65 6e 67 74 68 20 77 65 20 65 6e  the length we en
2f321 63 6f 64 65 64 2e 20 20 41 64 64 69 74 69 6f 6e  coded.  Addition
2f322 61 6c 6c 79 2c 20 74 68 65 20 73 70 61 63 65 20  ally, the space 
2f323 72 65 71 75 69 72 65 64 20 74 6f 0a 20 20 2a 2a  required to.  **
2f324 20 65 6e 63 6f 64 65 20 74 68 69 73 20 6c 65 6e   encode this len
2f325 67 74 68 20 63 6f 75 6c 64 20 62 65 20 73 6d 61  gth could be sma
2f326 6c 6c 65 72 2e 20 20 46 6f 72 20 73 6d 61 6c 6c  ller.  For small
2f327 20 64 6f 63 6c 69 73 74 73 2c 20 74 68 69 73 20   doclists, this 
2f328 69 73 0a 20 20 2a 2a 20 6e 6f 74 20 61 20 62 69  is.  ** not a bi
2f329 67 20 64 65 61 6c 2c 20 77 65 20 63 61 6e 20 6a  g deal, we can j
2f32a 75 73 74 20 75 73 65 20 6d 65 6d 6d 6f 76 65 28  ust use memmove(
2f32b 29 20 74 6f 20 61 64 6a 75 73 74 20 74 68 69 6e  ) to adjust thin
2f32c 67 73 2e 0a 20 20 2a 2f 0a 20 20 6e 41 63 74 75  gs..  */.  nActu
2f32d 61 6c 44 61 74 61 20 3d 20 70 57 72 69 74 65 72  alData = pWriter
2f32e 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2d 28 69 44  ->data.nData-(iD
2f32f 6f 63 6c 69 73 74 44 61 74 61 2b 6e 29 3b 0a 20  oclistData+n);. 
2f330 20 6e 41 63 74 75 61 6c 20 3d 20 66 74 73 33 50   nActual = fts3P
2f331 75 74 56 61 72 69 6e 74 28 63 2c 20 6e 41 63 74  utVarint(c, nAct
2f332 75 61 6c 44 61 74 61 29 3b 0a 20 20 61 73 73 65  ualData);.  asse
2f333 72 74 28 20 6e 41 63 74 75 61 6c 44 61 74 61 3c  rt( nActualData<
2f334 3d 6e 44 61 74 61 20 29 3b 0a 20 20 61 73 73 65  =nData );.  asse
2f335 72 74 28 20 6e 41 63 74 75 61 6c 3c 3d 6e 20 29  rt( nActual<=n )
2f336 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6e  ;..  /* If the n
2f337 65 77 20 64 6f 63 6c 69 73 74 20 69 73 20 62 69  ew doclist is bi
2f338 67 20 65 6e 6f 75 67 68 20 66 6f 72 20 66 6f 72  g enough for for
2f339 63 65 20 61 20 73 74 61 6e 64 61 6c 6f 6e 65 20  ce a standalone 
2f33a 6c 65 61 66 0a 20 20 2a 2a 20 6e 6f 64 65 2c 20  leaf.  ** node, 
2f33b 77 65 20 63 61 6e 20 69 6d 6d 65 64 69 61 74 65  we can immediate
2f33c 6c 79 20 66 6c 75 73 68 20 69 74 20 69 6e 6c 69  ly flush it inli
2f33d 6e 65 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67  ne without doing
2f33e 20 74 68 65 0a 20 20 2a 2a 20 6d 65 6d 6d 6f 76   the.  ** memmov
2f33f 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 2f 2a 20 54  e()..  */.  /* T
2f340 4f 44 4f 28 73 68 65 73 73 29 20 54 68 69 73 20  ODO(shess) This 
2f341 74 65 73 74 20 6d 61 74 63 68 65 73 20 6c 65 61  test matches lea
2f342 66 57 72 69 74 65 72 53 74 65 70 28 29 2c 20 77  fWriterStep(), w
2f343 68 69 63 68 20 64 6f 65 73 20 74 68 69 73 0a 20  hich does this. 
2f344 20 2a 2a 20 74 65 73 74 20 62 65 66 6f 72 65 20   ** test before 
2f345 69 74 20 6b 6e 6f 77 73 20 74 68 65 20 63 6f 73  it knows the cos
2f346 74 20 74 6f 20 76 61 72 69 6e 74 2d 65 6e 63 6f  t to varint-enco
2f347 64 65 20 74 68 65 20 74 65 72 6d 20 61 6e 64 0a  de the term and.
2f348 20 20 2a 2a 20 64 6f 63 6c 69 73 74 20 6c 65 6e    ** doclist len
2f349 67 74 68 73 2e 20 20 41 74 20 73 6f 6d 65 20 70  gths.  At some p
2f34a 6f 69 6e 74 2c 20 63 68 61 6e 67 65 20 74 6f 0a  oint, change to.
2f34b 20 20 2a 2a 20 70 57 72 69 74 65 72 2d 3e 64 61    ** pWriter->da
2f34c 74 61 2e 6e 44 61 74 61 2d 69 54 65 72 6d 44 61  ta.nData-iTermDa
2f34d 74 61 3e 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49  ta>STANDALONE_MI
2f34e 4e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 54  N..  */.  if( nT
2f34f 65 72 6d 2b 6e 41 63 74 75 61 6c 44 61 74 61 3e  erm+nActualData>
2f350 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 29  STANDALONE_MIN )
2f351 7b 0a 20 20 20 20 2f 2a 20 50 75 73 68 20 6c 65  {.    /* Push le
2f352 61 66 20 6e 6f 64 65 20 66 72 6f 6d 20 62 65 66  af node from bef
2f353 6f 72 65 20 74 68 69 73 20 74 65 72 6d 2e 20 2a  ore this term. *
2f354 2f 0a 20 20 20 20 69 66 28 20 69 54 65 72 6d 44  /.    if( iTermD
2f355 61 74 61 3e 30 20 29 7b 0a 20 20 20 20 20 20 72  ata>0 ){.      r
2f356 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72 49 6e  c = leafWriterIn
2f357 74 65 72 6e 61 6c 46 6c 75 73 68 28 76 2c 20 70  ternalFlush(v, p
2f358 57 72 69 74 65 72 2c 20 30 2c 20 69 54 65 72 6d  Writer, 0, iTerm
2f359 44 61 74 61 29 3b 0a 20 20 20 20 20 20 69 66 28  Data);.      if(
2f35a 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2f35b 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20   return rc;..   
2f35c 20 20 20 70 57 72 69 74 65 72 2d 3e 6e 54 65 72     pWriter->nTer
2f35d 6d 44 69 73 74 69 6e 63 74 20 3d 20 6e 54 65 72  mDistinct = nTer
2f35e 6d 44 69 73 74 69 6e 63 74 3b 0a 20 20 20 20 7d  mDistinct;.    }
2f35f 0a 0a 20 20 20 20 2f 2a 20 46 69 78 20 74 68 65  ..    /* Fix the
2f360 20 65 6e 63 6f 64 65 64 20 64 6f 63 6c 69 73 74   encoded doclist
2f361 20 6c 65 6e 67 74 68 2e 20 2a 2f 0a 20 20 20 20   length. */.    
2f362 69 44 6f 63 6c 69 73 74 44 61 74 61 20 2b 3d 20  iDoclistData += 
2f363 6e 20 2d 20 6e 41 63 74 75 61 6c 3b 0a 20 20 20  n - nActual;.   
2f364 20 6d 65 6d 63 70 79 28 70 57 72 69 74 65 72 2d   memcpy(pWriter-
2f365 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 6f 63  >data.pData+iDoc
2f366 6c 69 73 74 44 61 74 61 2c 20 63 2c 20 6e 41 63  listData, c, nAc
2f367 74 75 61 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 50  tual);..    /* P
2f368 75 73 68 20 74 68 65 20 73 74 61 6e 64 61 6c 6f  ush the standalo
2f369 6e 65 20 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f  ne leaf node. */
2f36a 0a 20 20 20 20 72 63 20 3d 20 6c 65 61 66 57 72  .    rc = leafWr
2f36b 69 74 65 72 49 6e 6c 69 6e 65 46 6c 75 73 68 28  iterInlineFlush(
2f36c 76 2c 20 70 57 72 69 74 65 72 2c 20 70 54 65 72  v, pWriter, pTer
2f36d 6d 2c 20 6e 54 65 72 6d 2c 20 69 44 6f 63 6c 69  m, nTerm, iDocli
2f36e 73 74 44 61 74 61 29 3b 0a 20 20 20 20 69 66 28  stData);.    if(
2f36f 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2f370 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20   return rc;..   
2f371 20 2f 2a 20 4c 65 61 76 65 20 74 68 65 20 6e 6f   /* Leave the no
2f372 64 65 20 65 6d 70 74 79 2e 20 2a 2f 0a 20 20 20  de empty. */.   
2f373 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74   dataBufferReset
2f374 28 26 70 57 72 69 74 65 72 2d 3e 64 61 74 61 29  (&pWriter->data)
2f375 3b 0a 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ;..    return rc
2f376 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74  ;.  }..  /* At t
2f377 68 69 73 20 70 6f 69 6e 74 2c 20 77 65 20 6b 6e  his point, we kn
2f378 6f 77 20 74 68 61 74 20 74 68 65 20 64 6f 63 6c  ow that the docl
2f379 69 73 74 20 77 61 73 20 73 6d 61 6c 6c 2c 20 73  ist was small, s
2f37a 6f 20 64 6f 20 74 68 65 0a 20 20 2a 2a 20 6d 65  o do the.  ** me
2f37b 6d 6d 6f 76 65 20 69 66 20 69 6e 64 69 63 61 74  mmove if indicat
2f37c 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e  ed..  */.  if( n
2f37d 41 63 74 75 61 6c 3c 6e 20 29 7b 0a 20 20 20 20  Actual<n ){.    
2f37e 6d 65 6d 6d 6f 76 65 28 70 57 72 69 74 65 72 2d  memmove(pWriter-
2f37f 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 6f 63  >data.pData+iDoc
2f380 6c 69 73 74 44 61 74 61 2b 6e 41 63 74 75 61 6c  listData+nActual
2f381 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 57  ,.            pW
2f382 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74  riter->data.pDat
2f383 61 2b 69 44 6f 63 6c 69 73 74 44 61 74 61 2b 6e  a+iDoclistData+n
2f384 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 57  ,.            pW
2f385 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
2f386 61 2d 28 69 44 6f 63 6c 69 73 74 44 61 74 61 2b  a-(iDoclistData+
2f387 6e 29 29 3b 0a 20 20 20 20 70 57 72 69 74 65 72  n));.    pWriter
2f388 2d 3e 64 61 74 61 2e 6e 44 61 74 61 20 2d 3d 20  ->data.nData -= 
2f389 6e 2d 6e 41 63 74 75 61 6c 3b 0a 20 20 7d 0a 0a  n-nActual;.  }..
2f38a 20 20 2f 2a 20 52 65 70 6c 61 63 65 20 77 72 69    /* Replace wri
2f38b 74 74 65 6e 20 6c 65 6e 67 74 68 20 77 69 74 68  tten length with
2f38c 20 61 63 74 75 61 6c 20 6c 65 6e 67 74 68 2e 20   actual length. 
2f38d 2a 2f 0a 20 20 6d 65 6d 63 70 79 28 70 57 72 69  */.  memcpy(pWri
2f38e 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b  ter->data.pData+
2f38f 69 44 6f 63 6c 69 73 74 44 61 74 61 2c 20 63 2c  iDoclistData, c,
2f390 20 6e 41 63 74 75 61 6c 29 3b 0a 0a 20 20 2f 2a   nActual);..  /*
2f391 20 49 66 20 74 68 65 20 6e 6f 64 65 20 69 73 20   If the node is 
2f392 74 6f 6f 20 6c 61 72 67 65 2c 20 62 72 65 61 6b  too large, break
2f393 20 74 68 69 6e 67 73 20 75 70 2e 20 2a 2f 0a 20   things up. */. 
2f394 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20   /* TODO(shess) 
2f395 54 68 69 73 20 74 65 73 74 20 6d 61 74 63 68 65  This test matche
2f396 73 20 6c 65 61 66 57 72 69 74 65 72 53 74 65 70  s leafWriterStep
2f397 28 29 2c 20 77 68 69 63 68 20 64 6f 65 73 20 74  (), which does t
2f398 68 69 73 0a 20 20 2a 2a 20 74 65 73 74 20 62 65  his.  ** test be
2f399 66 6f 72 65 20 69 74 20 6b 6e 6f 77 73 20 74 68  fore it knows th
2f39a 65 20 63 6f 73 74 20 74 6f 20 76 61 72 69 6e 74  e cost to varint
2f39b 2d 65 6e 63 6f 64 65 20 74 68 65 20 74 65 72 6d  -encode the term
2f39c 20 61 6e 64 0a 20 20 2a 2a 20 64 6f 63 6c 69 73   and.  ** doclis
2f39d 74 20 6c 65 6e 67 74 68 73 2e 20 20 41 74 20 73  t lengths.  At s
2f39e 6f 6d 65 20 70 6f 69 6e 74 2c 20 63 68 61 6e 67  ome point, chang
2f39f 65 20 74 6f 0a 20 20 2a 2a 20 70 57 72 69 74 65  e to.  ** pWrite
2f3a0 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3e 4c 45  r->data.nData>LE
2f3a1 41 46 5f 4d 41 58 2e 0a 20 20 2a 2f 0a 20 20 69  AF_MAX..  */.  i
2f3a2 66 28 20 69 54 65 72 6d 44 61 74 61 2b 6e 54 65  f( iTermData+nTe
2f3a3 72 6d 2b 6e 41 63 74 75 61 6c 44 61 74 61 3e 4c  rm+nActualData>L
2f3a4 45 41 46 5f 4d 41 58 20 29 7b 0a 20 20 20 20 2f  EAF_MAX ){.    /
2f3a5 2a 20 46 6c 75 73 68 20 6f 75 74 20 74 68 65 20  * Flush out the 
2f3a6 6c 65 61 64 69 6e 67 20 64 61 74 61 20 61 73 20  leading data as 
2f3a7 61 20 6e 6f 64 65 20 2a 2f 0a 20 20 20 20 72 63  a node */.    rc
2f3a8 20 3d 20 6c 65 61 66 57 72 69 74 65 72 49 6e 74   = leafWriterInt
2f3a9 65 72 6e 61 6c 46 6c 75 73 68 28 76 2c 20 70 57  ernalFlush(v, pW
2f3aa 72 69 74 65 72 2c 20 30 2c 20 69 54 65 72 6d 44  riter, 0, iTermD
2f3ab 61 74 61 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ata);.    if( rc
2f3ac 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
2f3ad 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 70 57  turn rc;..    pW
2f3ae 72 69 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74  riter->nTermDist
2f3af 69 6e 63 74 20 3d 20 6e 54 65 72 6d 44 69 73 74  inct = nTermDist
2f3b0 69 6e 63 74 3b 0a 0a 20 20 20 20 2f 2a 20 52 65  inct;..    /* Re
2f3b1 62 75 69 6c 64 20 68 65 61 64 65 72 20 75 73 69  build header usi
2f3b2 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  ng the current t
2f3b3 65 72 6d 20 2a 2f 0a 20 20 20 20 6e 20 3d 20 66  erm */.    n = f
2f3b4 74 73 33 50 75 74 56 61 72 69 6e 74 28 70 57 72  ts3PutVarint(pWr
2f3b5 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61  iter->data.pData
2f3b6 2c 20 30 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66  , 0);.    n += f
2f3b7 74 73 33 50 75 74 56 61 72 69 6e 74 28 70 57 72  ts3PutVarint(pWr
2f3b8 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61  iter->data.pData
2f3b9 2b 6e 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20 20  +n, nTerm);.    
2f3ba 6d 65 6d 63 70 79 28 70 57 72 69 74 65 72 2d 3e  memcpy(pWriter->
2f3bb 64 61 74 61 2e 70 44 61 74 61 2b 6e 2c 20 70 54  data.pData+n, pT
2f3bc 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20  erm, nTerm);.   
2f3bd 20 6e 20 2b 3d 20 6e 54 65 72 6d 3b 0a 0a 20 20   n += nTerm;..  
2f3be 20 20 2f 2a 20 54 68 65 72 65 20 73 68 6f 75 6c    /* There shoul
2f3bf 64 20 61 6c 77 61 79 73 20 62 65 20 72 6f 6f 6d  d always be room
2f3c0 2c 20 62 65 63 61 75 73 65 20 74 68 65 20 70 72  , because the pr
2f3c1 65 76 69 6f 75 73 20 65 6e 63 6f 64 69 6e 67 0a  evious encoding.
2f3c2 20 20 20 20 2a 2a 20 69 6e 63 6c 75 64 65 64 20      ** included 
2f3c3 61 6c 6c 20 64 61 74 61 20 6e 65 63 65 73 73 61  all data necessa
2f3c4 72 79 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20  ry to construct 
2f3c5 74 68 65 20 74 65 72 6d 2e 0a 20 20 20 20 2a 2f  the term..    */
2f3c6 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c 69  .    assert( n<i
2f3c7 44 6f 63 6c 69 73 74 44 61 74 61 20 29 3b 0a 20  DoclistData );. 
2f3c8 20 20 20 2f 2a 20 53 6f 20 6c 6f 6e 67 20 61 73     /* So long as
2f3c9 20 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20   STANDALONE_MIN 
2f3ca 69 73 20 68 61 6c 66 20 6f 72 20 6c 65 73 73 20  is half or less 
2f3cb 6f 66 20 4c 45 41 46 5f 4d 41 58 2c 20 74 68 65  of LEAF_MAX, the
2f3cc 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e  .    ** followin
2f3cd 67 20 6d 65 6d 63 70 79 28 29 20 69 73 20 73 61  g memcpy() is sa
2f3ce 66 65 20 28 61 73 20 6f 70 70 6f 73 65 64 20 74  fe (as opposed t
2f3cf 6f 20 6e 65 65 64 69 6e 67 20 61 20 6d 65 6d 6d  o needing a memm
2f3d0 6f 76 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ove)..    */.   
2f3d1 20 61 73 73 65 72 74 28 20 32 2a 53 54 41 4e 44   assert( 2*STAND
2f3d2 41 4c 4f 4e 45 5f 4d 49 4e 3c 3d 4c 45 41 46 5f  ALONE_MIN<=LEAF_
2f3d3 4d 41 58 20 29 3b 0a 20 20 20 20 61 73 73 65 72  MAX );.    asser
2f3d4 74 28 20 6e 2b 70 57 72 69 74 65 72 2d 3e 64 61  t( n+pWriter->da
2f3d5 74 61 2e 6e 44 61 74 61 2d 69 44 6f 63 6c 69 73  ta.nData-iDoclis
2f3d6 74 44 61 74 61 3c 69 44 6f 63 6c 69 73 74 44 61  tData<iDoclistDa
2f3d7 74 61 20 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  ta );.    memcpy
2f3d8 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70  (pWriter->data.p
2f3d9 44 61 74 61 2b 6e 2c 0a 20 20 20 20 20 20 20 20  Data+n,.        
2f3da 20 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61     pWriter->data
2f3db 2e 70 44 61 74 61 2b 69 44 6f 63 6c 69 73 74 44  .pData+iDoclistD
2f3dc 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
2f3dd 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44  pWriter->data.nD
2f3de 61 74 61 2d 69 44 6f 63 6c 69 73 74 44 61 74 61  ata-iDoclistData
2f3df 29 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  );.    pWriter->
2f3e0 64 61 74 61 2e 6e 44 61 74 61 20 2d 3d 20 69 44  data.nData -= iD
2f3e1 6f 63 6c 69 73 74 44 61 74 61 2d 6e 3b 0a 20 20  oclistData-n;.  
2f3e2 7d 0a 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44  }.  ASSERT_VALID
2f3e3 5f 4c 45 41 46 5f 4e 4f 44 45 28 70 57 72 69 74  _LEAF_NODE(pWrit
2f3e4 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2c 20  er->data.pData, 
2f3e5 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44  pWriter->data.nD
2f3e6 61 74 61 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  ata);..  return 
2f3e7 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
2f3e8 20 50 75 73 68 20 70 54 65 72 6d 5b 6e 54 65 72   Push pTerm[nTer
2f3e9 6d 5d 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68  m] along with th
2f3ea 65 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 74  e doclist data t
2f3eb 6f 20 74 68 65 20 6c 65 61 66 20 6c 61 79 65 72  o the leaf layer
2f3ec 20 6f 66 0a 2a 2a 20 25 5f 73 65 67 6d 65 6e 74   of.** %_segment
2f3ed 73 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68  s..*/./* TODO(sh
2f3ee 65 73 73 29 20 52 65 76 69 73 65 20 77 72 69 74  ess) Revise writ
2f3ef 65 5a 65 72 6f 53 65 67 6d 65 6e 74 28 29 20 73  eZeroSegment() s
2f3f0 6f 20 74 68 61 74 20 64 6f 63 6c 69 73 74 73 20  o that doclists 
2f3f1 61 72 65 0a 2a 2a 20 63 6f 6e 73 74 72 75 63 74  are.** construct
2f3f2 65 64 20 64 69 72 65 63 74 6c 79 20 69 6e 20 70  ed directly in p
2f3f3 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 0a 2a 2f  Writer->data..*/
2f3f4 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66  .static int leaf
2f3f5 57 72 69 74 65 72 53 74 65 70 28 66 75 6c 6c 74  WriterStep(fullt
2f3f6 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61  ext_vtab *v, Lea
2f3f7 66 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  fWriter *pWriter
2f3f8 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2f3f9 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
2f3fa 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69  t char *pTerm, i
2f3fb 6e 74 20 6e 54 65 72 6d 2c 0a 20 20 20 20 20 20  nt nTerm,.      
2f3fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f3fd 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2f3fe 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61  pData, int nData
2f3ff 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 44  ){.  int rc;.  D
2f400 4c 52 65 61 64 65 72 20 72 65 61 64 65 72 3b 0a  LReader reader;.
2f401 0a 20 20 64 6c 72 49 6e 69 74 28 26 72 65 61 64  .  dlrInit(&read
2f402 65 72 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20  er, DL_DEFAULT, 
2f403 70 44 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a 20  pData, nData);. 
2f404 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72   rc = leafWriter
2f405 53 74 65 70 4d 65 72 67 65 28 76 2c 20 70 57 72  StepMerge(v, pWr
2f406 69 74 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65  iter, pTerm, nTe
2f407 72 6d 2c 20 26 72 65 61 64 65 72 2c 20 31 29 3b  rm, &reader, 1);
2f408 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72  .  dlrDestroy(&r
2f409 65 61 64 65 72 29 3b 0a 0a 20 20 72 65 74 75 72  eader);..  retur
2f40a 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a  n rc;.}.../*****
2f40b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f40c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f40d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f40e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20  ***********/./* 
2f40f 4c 65 61 66 52 65 61 64 65 72 20 69 73 20 75 73  LeafReader is us
2f410 65 64 20 74 6f 20 69 74 65 72 61 74 65 20 6f 76  ed to iterate ov
2f411 65 72 20 61 6e 20 69 6e 64 69 76 69 64 75 61 6c  er an individual
2f412 20 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a 74   leaf node. */.t
2f413 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4c 65  ypedef struct Le
2f414 61 66 52 65 61 64 65 72 20 7b 0a 20 20 44 61 74  afReader {.  Dat
2f415 61 42 75 66 66 65 72 20 74 65 72 6d 3b 20 20 20  aBuffer term;   
2f416 20 20 20 20 20 20 20 2f 2a 20 63 6f 70 79 20 6f         /* copy o
2f417 66 20 63 75 72 72 65 6e 74 20 74 65 72 6d 2e 20  f current term. 
2f418 2a 2f 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  */..  const char
2f419 20 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 20   *pData;        
2f41a 2f 2a 20 64 61 74 61 20 66 6f 72 20 63 75 72 72  /* data for curr
2f41b 65 6e 74 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 69  ent term. */.  i
2f41c 6e 74 20 6e 44 61 74 61 3b 0a 7d 20 4c 65 61 66  nt nData;.} Leaf
2f41d 52 65 61 64 65 72 3b 0a 0a 73 74 61 74 69 63 20  Reader;..static 
2f41e 76 6f 69 64 20 6c 65 61 66 52 65 61 64 65 72 44  void leafReaderD
2f41f 65 73 74 72 6f 79 28 4c 65 61 66 52 65 61 64 65  estroy(LeafReade
2f420 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 64  r *pReader){.  d
2f421 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
2f422 28 26 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 29  (&pReader->term)
2f423 3b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 52 65  ;.  SCRAMBLE(pRe
2f424 61 64 65 72 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  ader);.}..static
2f425 20 69 6e 74 20 6c 65 61 66 52 65 61 64 65 72 41   int leafReaderA
2f426 74 45 6e 64 28 4c 65 61 66 52 65 61 64 65 72 20  tEnd(LeafReader 
2f427 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74  *pReader){.  ret
2f428 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 6e 44 61  urn pReader->nDa
2f429 74 61 3c 3d 30 3b 0a 7d 0a 0a 2f 2a 20 41 63 63  ta<=0;.}../* Acc
2f42a 65 73 73 20 74 68 65 20 63 75 72 72 65 6e 74 20  ess the current 
2f42b 74 65 72 6d 2e 20 2a 2f 0a 73 74 61 74 69 63 20  term. */.static 
2f42c 69 6e 74 20 6c 65 61 66 52 65 61 64 65 72 54 65  int leafReaderTe
2f42d 72 6d 42 79 74 65 73 28 4c 65 61 66 52 65 61 64  rmBytes(LeafRead
2f42e 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
2f42f 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
2f430 74 65 72 6d 2e 6e 44 61 74 61 3b 0a 7d 0a 73 74  term.nData;.}.st
2f431 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
2f432 2a 6c 65 61 66 52 65 61 64 65 72 54 65 72 6d 28  *leafReaderTerm(
2f433 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52 65 61  LeafReader *pRea
2f434 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20  der){.  assert( 
2f435 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44  pReader->term.nD
2f436 61 74 61 3e 30 20 29 3b 0a 20 20 72 65 74 75 72  ata>0 );.  retur
2f437 6e 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e  n pReader->term.
2f438 70 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 20 41 63 63  pData;.}../* Acc
2f439 65 73 73 20 74 68 65 20 64 6f 63 6c 69 73 74 20  ess the doclist 
2f43a 64 61 74 61 20 66 6f 72 20 74 68 65 20 63 75 72  data for the cur
2f43b 72 65 6e 74 20 74 65 72 6d 2e 20 2a 2f 0a 73 74  rent term. */.st
2f43c 61 74 69 63 20 69 6e 74 20 6c 65 61 66 52 65 61  atic int leafRea
2f43d 64 65 72 44 61 74 61 42 79 74 65 73 28 4c 65 61  derDataBytes(Lea
2f43e 66 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  fReader *pReader
2f43f 29 7b 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a  ){.  int nData;.
2f440 20 20 61 73 73 65 72 74 28 20 70 52 65 61 64 65    assert( pReade
2f441 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 3e 30 20  r->term.nData>0 
2f442 29 3b 0a 20 20 66 74 73 33 47 65 74 56 61 72 69  );.  fts3GetVari
2f443 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44  nt32(pReader->pD
2f444 61 74 61 2c 20 26 6e 44 61 74 61 29 3b 0a 20 20  ata, &nData);.  
2f445 72 65 74 75 72 6e 20 6e 44 61 74 61 3b 0a 7d 0a  return nData;.}.
2f446 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
2f447 72 20 2a 6c 65 61 66 52 65 61 64 65 72 44 61 74  r *leafReaderDat
2f448 61 28 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52  a(LeafReader *pR
2f449 65 61 64 65 72 29 7b 0a 20 20 69 6e 74 20 6e 2c  eader){.  int n,
2f44a 20 6e 44 61 74 61 3b 0a 20 20 61 73 73 65 72 74   nData;.  assert
2f44b 28 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e  ( pReader->term.
2f44c 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 6e 20 3d  nData>0 );.  n =
2f44d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
2f44e 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c  (pReader->pData,
2f44f 20 26 6e 44 61 74 61 29 3b 0a 20 20 72 65 74 75   &nData);.  retu
2f450 72 6e 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74  rn pReader->pDat
2f451 61 2b 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  a+n;.}..static v
2f452 6f 69 64 20 6c 65 61 66 52 65 61 64 65 72 49 6e  oid leafReaderIn
2f453 69 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  it(const char *p
2f454 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c  Data, int nData,
2f455 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2f456 20 20 20 20 20 20 20 20 20 20 20 20 4c 65 61 66              Leaf
2f457 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
2f458 7b 0a 20 20 69 6e 74 20 6e 54 65 72 6d 2c 20 6e  {.  int nTerm, n
2f459 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61  ;..  assert( nDa
2f45a 74 61 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74  ta>0 );.  assert
2f45b 28 20 70 44 61 74 61 5b 30 5d 3d 3d 27 5c 30 27  ( pData[0]=='\0'
2f45c 20 29 3b 0a 0a 20 20 43 4c 45 41 52 28 70 52 65   );..  CLEAR(pRe
2f45d 61 64 65 72 29 3b 0a 0a 20 20 2f 2a 20 52 65 61  ader);..  /* Rea
2f45e 64 20 74 68 65 20 66 69 72 73 74 20 74 65 72 6d  d the first term
2f45f 2c 20 73 6b 69 70 70 69 6e 67 20 74 68 65 20 68  , skipping the h
2f460 65 61 64 65 72 20 62 79 74 65 2e 20 2a 2f 0a 20  eader byte. */. 
2f461 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
2f462 6e 74 33 32 28 70 44 61 74 61 2b 31 2c 20 26 6e  nt32(pData+1, &n
2f463 54 65 72 6d 29 3b 0a 20 20 64 61 74 61 42 75 66  Term);.  dataBuf
2f464 66 65 72 49 6e 69 74 28 26 70 52 65 61 64 65 72  ferInit(&pReader
2f465 2d 3e 74 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a  ->term, nTerm);.
2f466 20 20 64 61 74 61 42 75 66 66 65 72 52 65 70 6c    dataBufferRepl
2f467 61 63 65 28 26 70 52 65 61 64 65 72 2d 3e 74 65  ace(&pReader->te
2f468 72 6d 2c 20 70 44 61 74 61 2b 31 2b 6e 2c 20 6e  rm, pData+1+n, n
2f469 54 65 72 6d 29 3b 0a 0a 20 20 2f 2a 20 50 6f 73  Term);..  /* Pos
2f46a 69 74 69 6f 6e 20 61 66 74 65 72 20 74 68 65 20  ition after the 
2f46b 66 69 72 73 74 20 74 65 72 6d 2e 20 2a 2f 0a 20  first term. */. 
2f46c 20 61 73 73 65 72 74 28 20 31 2b 6e 2b 6e 54 65   assert( 1+n+nTe
2f46d 72 6d 3c 6e 44 61 74 61 20 29 3b 0a 20 20 70 52  rm<nData );.  pR
2f46e 65 61 64 65 72 2d 3e 70 44 61 74 61 20 3d 20 70  eader->pData = p
2f46f 44 61 74 61 2b 31 2b 6e 2b 6e 54 65 72 6d 3b 0a  Data+1+n+nTerm;.
2f470 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61    pReader->nData
2f471 20 3d 20 6e 44 61 74 61 2d 31 2d 6e 2d 6e 54 65   = nData-1-n-nTe
2f472 72 6d 3b 0a 7d 0a 0a 2f 2a 20 53 74 65 70 20 74  rm;.}../* Step t
2f473 68 65 20 72 65 61 64 65 72 20 66 6f 72 77 61 72  he reader forwar
2f474 64 20 74 6f 20 74 68 65 20 6e 65 78 74 20 74 65  d to the next te
2f475 72 6d 2e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f  rm. */.static vo
2f476 69 64 20 6c 65 61 66 52 65 61 64 65 72 53 74 65  id leafReaderSte
2f477 70 28 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52  p(LeafReader *pR
2f478 65 61 64 65 72 29 7b 0a 20 20 69 6e 74 20 6e 2c  eader){.  int n,
2f479 20 6e 44 61 74 61 2c 20 6e 50 72 65 66 69 78 2c   nData, nPrefix,
2f47a 20 6e 53 75 66 66 69 78 3b 0a 20 20 61 73 73 65   nSuffix;.  asse
2f47b 72 74 28 20 21 6c 65 61 66 52 65 61 64 65 72 41  rt( !leafReaderA
2f47c 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b  tEnd(pReader) );
2f47d 0a 0a 20 20 2f 2a 20 53 6b 69 70 20 70 72 65 76  ..  /* Skip prev
2f47e 69 6f 75 73 20 65 6e 74 72 79 27 73 20 64 61 74  ious entry's dat
2f47f 61 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 6e 20  a block. */.  n 
2f480 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33  = fts3GetVarint3
2f481 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61  2(pReader->pData
2f482 2c 20 26 6e 44 61 74 61 29 3b 0a 20 20 61 73 73  , &nData);.  ass
2f483 65 72 74 28 20 6e 2b 6e 44 61 74 61 3c 3d 70 52  ert( n+nData<=pR
2f484 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a  eader->nData );.
2f485 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61    pReader->pData
2f486 20 2b 3d 20 6e 2b 6e 44 61 74 61 3b 0a 20 20 70   += n+nData;.  p
2f487 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 2d 3d  Reader->nData -=
2f488 20 6e 2b 6e 44 61 74 61 3b 0a 0a 20 20 69 66 28   n+nData;..  if(
2f489 20 21 6c 65 61 66 52 65 61 64 65 72 41 74 45 6e   !leafReaderAtEn
2f48a 64 28 70 52 65 61 64 65 72 29 20 29 7b 0a 20 20  d(pReader) ){.  
2f48b 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20 74    /* Construct t
2f48c 68 65 20 6e 65 77 20 74 65 72 6d 20 75 73 69 6e  he new term usin
2f48d 67 20 61 20 70 72 65 66 69 78 20 66 72 6f 6d 20  g a prefix from 
2f48e 74 68 65 20 6f 6c 64 20 74 65 72 6d 20 70 6c 75  the old term plu
2f48f 73 20 61 0a 20 20 20 20 2a 2a 20 73 75 66 66 69  s a.    ** suffi
2f490 78 20 66 72 6f 6d 20 74 68 65 20 6c 65 61 66 20  x from the leaf 
2f491 64 61 74 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  data..    */.   
2f492 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
2f493 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44  nt32(pReader->pD
2f494 61 74 61 2c 20 26 6e 50 72 65 66 69 78 29 3b 0a  ata, &nPrefix);.
2f495 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74      n += fts3Get
2f496 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65 72  Varint32(pReader
2f497 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 6e 53 75 66  ->pData+n, &nSuf
2f498 66 69 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74  fix);.    assert
2f499 28 20 6e 2b 6e 53 75 66 66 69 78 3c 70 52 65 61  ( n+nSuffix<pRea
2f49a 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20  der->nData );.  
2f49b 20 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e    pReader->term.
2f49c 6e 44 61 74 61 20 3d 20 6e 50 72 65 66 69 78 3b  nData = nPrefix;
2f49d 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41  .    dataBufferA
2f49e 70 70 65 6e 64 28 26 70 52 65 61 64 65 72 2d 3e  ppend(&pReader->
2f49f 74 65 72 6d 2c 20 70 52 65 61 64 65 72 2d 3e 70  term, pReader->p
2f4a0 44 61 74 61 2b 6e 2c 20 6e 53 75 66 66 69 78 29  Data+n, nSuffix)
2f4a1 3b 0a 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  ;..    pReader->
2f4a2 70 44 61 74 61 20 2b 3d 20 6e 2b 6e 53 75 66 66  pData += n+nSuff
2f4a3 69 78 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d  ix;.    pReader-
2f4a4 3e 6e 44 61 74 61 20 2d 3d 20 6e 2b 6e 53 75 66  >nData -= n+nSuf
2f4a5 66 69 78 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 73  fix;.  }.}../* s
2f4a6 74 72 63 6d 70 2d 73 74 79 6c 65 20 63 6f 6d 70  trcmp-style comp
2f4a7 61 72 69 73 6f 6e 20 6f 66 20 70 52 65 61 64 65  arison of pReade
2f4a8 72 27 73 20 63 75 72 72 65 6e 74 20 74 65 72 6d  r's current term
2f4a9 20 61 67 61 69 6e 73 74 20 70 54 65 72 6d 2e 0a   against pTerm..
2f4aa 2a 2a 20 49 66 20 69 73 50 72 65 66 69 78 2c 20  ** If isPrefix, 
2f4ab 65 71 75 61 6c 69 74 79 20 6d 65 61 6e 73 20 65  equality means e
2f4ac 71 75 61 6c 20 74 68 72 6f 75 67 68 20 6e 54 65  qual through nTe
2f4ad 72 6d 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61  rm bytes..*/.sta
2f4ae 74 69 63 20 69 6e 74 20 6c 65 61 66 52 65 61 64  tic int leafRead
2f4af 65 72 54 65 72 6d 43 6d 70 28 4c 65 61 66 52 65  erTermCmp(LeafRe
2f4b0 61 64 65 72 20 2a 70 52 65 61 64 65 72 2c 0a 20  ader *pReader,. 
2f4b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f4b2 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
2f4b3 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69  t char *pTerm, i
2f4b4 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73  nt nTerm, int is
2f4b5 50 72 65 66 69 78 29 7b 0a 20 20 69 6e 74 20 63  Prefix){.  int c
2f4b6 2c 20 6e 20 3d 20 70 52 65 61 64 65 72 2d 3e 74  , n = pReader->t
2f4b7 65 72 6d 2e 6e 44 61 74 61 3c 6e 54 65 72 6d 20  erm.nData<nTerm 
2f4b8 3f 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e  ? pReader->term.
2f4b9 6e 44 61 74 61 20 3a 20 6e 54 65 72 6d 3b 0a 20  nData : nTerm;. 
2f4ba 20 69 66 28 20 6e 3d 3d 30 20 29 7b 0a 20 20 20   if( n==0 ){.   
2f4bb 20 69 66 28 20 70 52 65 61 64 65 72 2d 3e 74 65   if( pReader->te
2f4bc 72 6d 2e 6e 44 61 74 61 3e 30 20 29 20 72 65 74  rm.nData>0 ) ret
2f4bd 75 72 6e 20 2d 31 3b 0a 20 20 20 20 69 66 28 6e  urn -1;.    if(n
2f4be 54 65 72 6d 3e 30 20 29 20 72 65 74 75 72 6e 20  Term>0 ) return 
2f4bf 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  1;.    return 0;
2f4c0 0a 20 20 7d 0a 0a 20 20 63 20 3d 20 6d 65 6d 63  .  }..  c = memc
2f4c1 6d 70 28 70 52 65 61 64 65 72 2d 3e 74 65 72 6d  mp(pReader->term
2f4c2 2e 70 44 61 74 61 2c 20 70 54 65 72 6d 2c 20 6e  .pData, pTerm, n
2f4c3 29 3b 0a 20 20 69 66 28 20 63 21 3d 30 20 29 20  );.  if( c!=0 ) 
2f4c4 72 65 74 75 72 6e 20 63 3b 0a 20 20 69 66 28 20  return c;.  if( 
2f4c5 69 73 50 72 65 66 69 78 20 26 26 20 6e 3d 3d 6e  isPrefix && n==n
2f4c6 54 65 72 6d 20 29 20 72 65 74 75 72 6e 20 30 3b  Term ) return 0;
2f4c7 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65  .  return pReade
2f4c8 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 20 2d 20  r->term.nData - 
2f4c9 6e 54 65 72 6d 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a  nTerm;.}.../****
2f4ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f4cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f4cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f4cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2f4ce 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 77 72   LeavesReader wr
2f4cf 61 70 73 20 4c 65 61 66 52 65 61 64 65 72 20 74  aps LeafReader t
2f4d0 6f 20 61 6c 6c 6f 77 20 69 74 65 72 61 74 69 6e  o allow iteratin
2f4d1 67 20 6f 76 65 72 20 74 68 65 20 65 6e 74 69 72  g over the entir
2f4d2 65 0a 2a 2a 20 6c 65 61 66 20 6c 61 79 65 72 20  e.** leaf layer 
2f4d3 6f 66 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a  of the tree..*/.
2f4d4 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4c  typedef struct L
2f4d5 65 61 76 65 73 52 65 61 64 65 72 20 7b 0a 20 20  eavesReader {.  
2f4d6 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20 20  int idx;        
2f4d7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
2f4d8 65 78 20 77 69 74 68 69 6e 20 74 68 65 20 73 65  ex within the se
2f4d9 67 6d 65 6e 74 2e 20 2a 2f 0a 0a 20 20 73 71 6c  gment. */..  sql
2f4da 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
2f4db 3b 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 6d  ;      /* Statem
2f4dc 65 6e 74 20 77 65 27 72 65 20 73 74 72 65 61 6d  ent we're stream
2f4dd 69 6e 67 20 6c 65 61 76 65 73 20 66 72 6f 6d 2e  ing leaves from.
2f4de 20 2a 2f 0a 20 20 69 6e 74 20 65 6f 66 3b 20 20   */.  int eof;  
2f4df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f4e0 2f 2a 20 77 65 27 76 65 20 73 65 65 6e 20 53 51  /* we've seen SQ
2f4e1 4c 49 54 45 5f 44 4f 4e 45 20 66 72 6f 6d 20 70  LITE_DONE from p
2f4e2 53 74 6d 74 2e 20 2a 2f 0a 0a 20 20 4c 65 61 66  Stmt. */..  Leaf
2f4e3 52 65 61 64 65 72 20 6c 65 61 66 52 65 61 64 65  Reader leafReade
2f4e4 72 3b 20 20 20 20 2f 2a 20 72 65 61 64 65 72 20  r;    /* reader 
2f4e5 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
2f4e6 6c 65 61 66 2e 20 2a 2f 0a 20 20 44 61 74 61 42  leaf. */.  DataB
2f4e7 75 66 66 65 72 20 72 6f 6f 74 44 61 74 61 3b 20  uffer rootData; 
2f4e8 20 20 20 20 20 2f 2a 20 72 6f 6f 74 20 64 61 74       /* root dat
2f4e9 61 20 66 6f 72 20 69 6e 6c 69 6e 65 2e 20 2a 2f  a for inline. */
2f4ea 0a 7d 20 4c 65 61 76 65 73 52 65 61 64 65 72 3b  .} LeavesReader;
2f4eb 0a 0a 2f 2a 20 41 63 63 65 73 73 20 74 68 65 20  ../* Access the 
2f4ec 63 75 72 72 65 6e 74 20 74 65 72 6d 2e 20 2a 2f  current term. */
2f4ed 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 76  .static int leav
2f4ee 65 73 52 65 61 64 65 72 54 65 72 6d 42 79 74 65  esReaderTermByte
2f4ef 73 28 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  s(LeavesReader *
2f4f0 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65  pReader){.  asse
2f4f1 72 74 28 20 21 70 52 65 61 64 65 72 2d 3e 65 6f  rt( !pReader->eo
2f4f2 66 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 6c 65  f );.  return le
2f4f3 61 66 52 65 61 64 65 72 54 65 72 6d 42 79 74 65  afReaderTermByte
2f4f4 73 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66  s(&pReader->leaf
2f4f5 52 65 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69  Reader);.}.stati
2f4f6 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 65  c const char *le
2f4f7 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 28 4c  avesReaderTerm(L
2f4f8 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 52 65  eavesReader *pRe
2f4f9 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28  ader){.  assert(
2f4fa 20 21 70 52 65 61 64 65 72 2d 3e 65 6f 66 20 29   !pReader->eof )
2f4fb 3b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 66 52  ;.  return leafR
2f4fc 65 61 64 65 72 54 65 72 6d 28 26 70 52 65 61 64  eaderTerm(&pRead
2f4fd 65 72 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b  er->leafReader);
2f4fe 0a 7d 0a 0a 2f 2a 20 41 63 63 65 73 73 20 74 68  .}../* Access th
2f4ff 65 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 66  e doclist data f
2f500 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  or the current t
2f501 65 72 6d 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69  erm. */.static i
2f502 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65 72 44  nt leavesReaderD
2f503 61 74 61 42 79 74 65 73 28 4c 65 61 76 65 73 52  ataBytes(LeavesR
2f504 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
2f505 0a 20 20 61 73 73 65 72 74 28 20 21 70 52 65 61  .  assert( !pRea
2f506 64 65 72 2d 3e 65 6f 66 20 29 3b 0a 20 20 72 65  der->eof );.  re
2f507 74 75 72 6e 20 6c 65 61 66 52 65 61 64 65 72 44  turn leafReaderD
2f508 61 74 61 42 79 74 65 73 28 26 70 52 65 61 64 65  ataBytes(&pReade
2f509 72 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a  r->leafReader);.
2f50a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  }.static const c
2f50b 68 61 72 20 2a 6c 65 61 76 65 73 52 65 61 64 65  har *leavesReade
2f50c 72 44 61 74 61 28 4c 65 61 76 65 73 52 65 61 64  rData(LeavesRead
2f50d 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
2f50e 61 73 73 65 72 74 28 20 21 70 52 65 61 64 65 72  assert( !pReader
2f50f 2d 3e 65 6f 66 20 29 3b 0a 20 20 72 65 74 75 72  ->eof );.  retur
2f510 6e 20 6c 65 61 66 52 65 61 64 65 72 44 61 74 61  n leafReaderData
2f511 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66 52  (&pReader->leafR
2f512 65 61 64 65 72 29 3b 0a 7d 0a 0a 73 74 61 74 69  eader);.}..stati
2f513 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64  c int leavesRead
2f514 65 72 41 74 45 6e 64 28 4c 65 61 76 65 73 52 65  erAtEnd(LeavesRe
2f515 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
2f516 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72    return pReader
2f517 2d 3e 65 6f 66 3b 0a 7d 0a 0a 2f 2a 20 6c 6f 61  ->eof;.}../* loa
2f518 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73 28 29  dSegmentLeaves()
2f519 20 6d 61 79 20 6e 6f 74 20 72 65 61 64 20 61 6c   may not read al
2f51a 6c 20 74 68 65 20 77 61 79 20 74 6f 20 53 51 4c  l the way to SQL
2f51b 49 54 45 5f 44 4f 4e 45 2c 20 74 68 75 73 0a 2a  ITE_DONE, thus.*
2f51c 2a 20 6c 65 61 76 69 6e 67 20 74 68 65 20 73 74  * leaving the st
2f51d 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 6f  atement handle o
2f51e 70 65 6e 2c 20 77 68 69 63 68 20 6c 6f 63 6b 73  pen, which locks
2f51f 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 2f   the table..*/./
2f520 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68  * TODO(shess) Th
2f521 69 73 20 22 73 6f 6c 75 74 69 6f 6e 22 20 69 73  is "solution" is
2f522 20 6e 6f 74 20 73 61 74 69 73 66 61 63 74 6f 72   not satisfactor
2f523 79 2e 20 20 52 65 61 6c 6c 79 2c 20 74 68 65 72  y.  Really, ther
2f524 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63  e.** should be c
2f525 68 65 63 6b 2d 69 6e 20 66 75 6e 63 74 69 6f 6e  heck-in function
2f526 20 66 6f 72 20 61 6c 6c 20 73 74 61 74 65 6d 65   for all stateme
2f527 6e 74 20 68 61 6e 64 6c 65 73 20 77 68 69 63 68  nt handles which
2f528 0a 2a 2a 20 61 72 72 61 6e 67 65 73 20 74 6f 20  .** arranges to 
2f529 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 72 65 73  call sqlite3_res
2f52a 65 74 28 29 2e 20 20 54 68 69 73 20 6d 6f 73 74  et().  This most
2f52b 20 6c 69 6b 65 6c 79 20 77 69 6c 6c 20 72 65 71   likely will req
2f52c 75 69 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 63 61  uire.** modifica
2f52d 74 69 6f 6e 20 74 6f 20 63 6f 6e 74 72 6f 6c 20  tion to control 
2f52e 66 6c 6f 77 20 61 6c 6c 20 6f 76 65 72 20 74 68  flow all over th
2f52f 65 20 70 6c 61 63 65 2c 20 74 68 6f 75 67 68 2c  e place, though,
2f530 20 73 6f 20 66 6f 72 20 6e 6f 77 0a 2a 2a 20 6a   so for now.** j
2f531 75 73 74 20 70 75 6e 74 2e 0a 2a 2a 0a 2a 2a 20  ust punt..**.** 
2f532 4e 6f 74 65 20 74 68 65 20 74 68 65 20 63 75 72  Note the the cur
2f533 72 65 6e 74 20 73 79 73 74 65 6d 20 61 73 73 75  rent system assu
2f534 6d 65 73 20 74 68 61 74 20 73 65 67 6d 65 6e 74  mes that segment
2f535 20 6d 65 72 67 65 73 20 77 69 6c 6c 20 72 75 6e   merges will run
2f536 20 74 6f 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f   to.** completio
2f537 6e 2c 20 77 68 69 63 68 20 69 73 20 77 68 79 20  n, which is why 
2f538 74 68 69 73 20 70 61 72 74 69 63 75 6c 61 72 20  this particular 
2f539 70 72 6f 62 61 62 6c 79 20 68 61 73 6e 27 74 20  probably hasn't 
2f53a 61 72 69 73 65 6e 20 69 6e 0a 2a 2a 20 74 68 69  arisen in.** thi
2f53b 73 20 63 61 73 65 2e 20 20 50 72 6f 62 61 62 6c  s case.  Probabl
2f53c 79 20 61 20 62 72 69 74 74 6c 65 20 61 73 73 75  y a brittle assu
2f53d 6d 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  mption..*/.stati
2f53e 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64  c int leavesRead
2f53f 65 72 52 65 73 65 74 28 4c 65 61 76 65 73 52 65  erReset(LeavesRe
2f540 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
2f541 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
2f542 5f 72 65 73 65 74 28 70 52 65 61 64 65 72 2d 3e  _reset(pReader->
2f543 70 53 74 6d 74 29 3b 0a 7d 0a 0a 73 74 61 74 69  pStmt);.}..stati
2f544 63 20 76 6f 69 64 20 6c 65 61 76 65 73 52 65 61  c void leavesRea
2f545 64 65 72 44 65 73 74 72 6f 79 28 4c 65 61 76 65  derDestroy(Leave
2f546 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  sReader *pReader
2f547 29 7b 0a 20 20 2f 2a 20 49 66 20 69 64 78 20 69  ){.  /* If idx i
2f548 73 20 2d 31 2c 20 74 68 61 74 20 6d 65 61 6e 73  s -1, that means
2f549 20 77 65 27 72 65 20 75 73 69 6e 67 20 61 20 6e   we're using a n
2f54a 6f 6e 2d 63 61 63 68 65 64 20 73 74 61 74 65 6d  on-cached statem
2f54b 65 6e 74 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20  ent.  ** handle 
2f54c 69 6e 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 28  in the optimize(
2f54d 29 20 63 61 73 65 2c 20 73 6f 20 77 65 20 6e 65  ) case, so we ne
2f54e 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20 69 74  ed to release it
2f54f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 52 65  ..  */.  if( pRe
2f550 61 64 65 72 2d 3e 70 53 74 6d 74 21 3d 4e 55 4c  ader->pStmt!=NUL
2f551 4c 20 26 26 20 70 52 65 61 64 65 72 2d 3e 69 64  L && pReader->id
2f552 78 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 73 71 6c  x==-1 ){.    sql
2f553 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52  ite3_finalize(pR
2f554 65 61 64 65 72 2d 3e 70 53 74 6d 74 29 3b 0a 20  eader->pStmt);. 
2f555 20 7d 0a 20 20 6c 65 61 66 52 65 61 64 65 72 44   }.  leafReaderD
2f556 65 73 74 72 6f 79 28 26 70 52 65 61 64 65 72 2d  estroy(&pReader-
2f557 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 20 20  >leafReader);.  
2f558 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
2f559 79 28 26 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74  y(&pReader->root
2f55a 44 61 74 61 29 3b 0a 20 20 53 43 52 41 4d 42 4c  Data);.  SCRAMBL
2f55b 45 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a 0a 2f  E(pReader);.}../
2f55c 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 70 52 65  * Initialize pRe
2f55d 61 64 65 72 20 77 69 74 68 20 74 68 65 20 67 69  ader with the gi
2f55e 76 65 6e 20 72 6f 6f 74 20 64 61 74 61 20 28 69  ven root data (i
2f55f 66 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 3d  f iStartBlockid=
2f560 3d 30 0a 2a 2a 20 74 68 65 20 6c 65 61 66 20 64  =0.** the leaf d
2f561 61 74 61 20 77 61 73 20 65 6e 74 69 72 65 6c 79  ata was entirely
2f562 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68   contained in th
2f563 65 20 72 6f 6f 74 29 2c 20 6f 72 20 66 72 6f 6d  e root), or from
2f564 20 74 68 65 0a 2a 2a 20 73 74 72 65 61 6d 20 6f   the.** stream o
2f565 66 20 62 6c 6f 63 6b 73 20 62 65 74 77 65 65 6e  f blocks between
2f566 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 20 61   iStartBlockid a
2f567 6e 64 20 69 45 6e 64 42 6c 6f 63 6b 69 64 2c 20  nd iEndBlockid, 
2f568 69 6e 63 6c 75 73 69 76 65 2e 0a 2a 2f 0a 73 74  inclusive..*/.st
2f569 61 74 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52  atic int leavesR
2f56a 65 61 64 65 72 49 6e 69 74 28 66 75 6c 6c 74 65  eaderInit(fullte
2f56b 78 74 5f 76 74 61 62 20 2a 76 2c 0a 20 20 20 20  xt_vtab *v,.    
2f56c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f56d 20 20 20 20 20 20 20 20 69 6e 74 20 69 64 78 2c          int idx,
2f56e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2f56f 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
2f570 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61 72 74  ite_int64 iStart
2f571 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20  Blockid,.       
2f572 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f573 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
2f574 34 20 69 45 6e 64 42 6c 6f 63 6b 69 64 2c 0a 20  4 iEndBlockid,. 
2f575 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f576 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
2f577 20 63 68 61 72 20 2a 70 52 6f 6f 74 44 61 74 61   char *pRootData
2f578 2c 20 69 6e 74 20 6e 52 6f 6f 74 44 61 74 61 2c  , int nRootData,
2f579 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2f57a 20 20 20 20 20 20 20 20 20 20 20 20 20 4c 65 61               Lea
2f57b 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64  vesReader *pRead
2f57c 65 72 29 7b 0a 20 20 43 4c 45 41 52 28 70 52 65  er){.  CLEAR(pRe
2f57d 61 64 65 72 29 3b 0a 20 20 70 52 65 61 64 65 72  ader);.  pReader
2f57e 2d 3e 69 64 78 20 3d 20 69 64 78 3b 0a 0a 20 20  ->idx = idx;..  
2f57f 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26  dataBufferInit(&
2f580 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61 74  pReader->rootDat
2f581 61 2c 20 30 29 3b 0a 20 20 69 66 28 20 69 53 74  a, 0);.  if( iSt
2f582 61 72 74 42 6c 6f 63 6b 69 64 3d 3d 30 20 29 7b  artBlockid==0 ){
2f583 0a 20 20 20 20 2f 2a 20 45 6e 74 69 72 65 20 6c  .    /* Entire l
2f584 65 61 66 20 6c 65 76 65 6c 20 66 69 74 20 69 6e  eaf level fit in
2f585 20 72 6f 6f 74 20 64 61 74 61 2e 20 2a 2f 0a 20   root data. */. 
2f586 20 20 20 64 61 74 61 42 75 66 66 65 72 52 65 70     dataBufferRep
2f587 6c 61 63 65 28 26 70 52 65 61 64 65 72 2d 3e 72  lace(&pReader->r
2f588 6f 6f 74 44 61 74 61 2c 20 70 52 6f 6f 74 44 61  ootData, pRootDa
2f589 74 61 2c 20 6e 52 6f 6f 74 44 61 74 61 29 3b 0a  ta, nRootData);.
2f58a 20 20 20 20 6c 65 61 66 52 65 61 64 65 72 49 6e      leafReaderIn
2f58b 69 74 28 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74  it(pReader->root
2f58c 44 61 74 61 2e 70 44 61 74 61 2c 20 70 52 65 61  Data.pData, pRea
2f58d 64 65 72 2d 3e 72 6f 6f 74 44 61 74 61 2e 6e 44  der->rootData.nD
2f58e 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
2f58f 20 20 20 20 20 20 20 20 26 70 52 65 61 64 65 72          &pReader
2f590 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 20  ->leafReader);. 
2f591 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
2f592 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 20  te3_stmt *s;.   
2f593 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65   int rc = sql_ge
2f594 74 5f 6c 65 61 66 5f 73 74 61 74 65 6d 65 6e 74  t_leaf_statement
2f595 28 76 2c 20 69 64 78 2c 20 26 73 29 3b 0a 20 20  (v, idx, &s);.  
2f596 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2f597 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
2f598 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ..    rc = sqlit
2f599 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c  e3_bind_int64(s,
2f59a 20 31 2c 20 69 53 74 61 72 74 42 6c 6f 63 6b 69   1, iStartBlocki
2f59b 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  d);.    if( rc!=
2f59c 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
2f59d 72 6e 20 72 63 3b 0a 0a 20 20 20 20 72 63 20 3d  rn rc;..    rc =
2f59e 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
2f59f 74 36 34 28 73 2c 20 32 2c 20 69 45 6e 64 42 6c  t64(s, 2, iEndBl
2f5a0 6f 63 6b 69 64 29 3b 0a 20 20 20 20 69 66 28 20  ockid);.    if( 
2f5a1 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2f5a2 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20  return rc;..    
2f5a3 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65  rc = sqlite3_ste
2f5a4 70 28 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63  p(s);.    if( rc
2f5a5 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
2f5a6 0a 20 20 20 20 20 20 70 52 65 61 64 65 72 2d 3e  .      pReader->
2f5a7 65 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 72  eof = 1;.      r
2f5a8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2f5a9 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
2f5aa 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20  c!=SQLITE_ROW ) 
2f5ab 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20  return rc;..    
2f5ac 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74 20 3d  pReader->pStmt =
2f5ad 20 73 3b 0a 20 20 20 20 6c 65 61 66 52 65 61 64   s;.    leafRead
2f5ae 65 72 49 6e 69 74 28 73 71 6c 69 74 65 33 5f 63  erInit(sqlite3_c
2f5af 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 52 65 61 64  olumn_blob(pRead
2f5b0 65 72 2d 3e 70 53 74 6d 74 2c 20 30 29 2c 0a 20  er->pStmt, 0),. 
2f5b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f5b2 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
2f5b3 5f 62 79 74 65 73 28 70 52 65 61 64 65 72 2d 3e  _bytes(pReader->
2f5b4 70 53 74 6d 74 2c 20 30 29 2c 0a 20 20 20 20 20  pStmt, 0),.     
2f5b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70                &p
2f5b6 52 65 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64  Reader->leafRead
2f5b7 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  er);.  }.  retur
2f5b8 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
2f5b9 2f 2a 20 53 74 65 70 20 74 68 65 20 63 75 72 72  /* Step the curr
2f5ba 65 6e 74 20 6c 65 61 66 20 66 6f 72 77 61 72 64  ent leaf forward
2f5bb 20 74 6f 20 74 68 65 20 6e 65 78 74 20 74 65 72   to the next ter
2f5bc 6d 2e 20 20 49 66 20 77 65 20 72 65 61 63 68 20  m.  If we reach 
2f5bd 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68  the.** end of th
2f5be 65 20 63 75 72 72 65 6e 74 20 6c 65 61 66 2c 20  e current leaf, 
2f5bf 73 74 65 70 20 66 6f 72 77 61 72 64 20 74 6f 20  step forward to 
2f5c0 74 68 65 20 6e 65 78 74 20 6c 65 61 66 20 62 6c  the next leaf bl
2f5c1 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ock..*/.static i
2f5c2 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65 72 53  nt leavesReaderS
2f5c3 74 65 70 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  tep(fulltext_vta
2f5c4 62 20 2a 76 2c 20 4c 65 61 76 65 73 52 65 61 64  b *v, LeavesRead
2f5c5 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
2f5c6 61 73 73 65 72 74 28 20 21 6c 65 61 76 65 73 52  assert( !leavesR
2f5c7 65 61 64 65 72 41 74 45 6e 64 28 70 52 65 61 64  eaderAtEnd(pRead
2f5c8 65 72 29 20 29 3b 0a 20 20 6c 65 61 66 52 65 61  er) );.  leafRea
2f5c9 64 65 72 53 74 65 70 28 26 70 52 65 61 64 65 72  derStep(&pReader
2f5ca 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 0a  ->leafReader);..
2f5cb 20 20 69 66 28 20 6c 65 61 66 52 65 61 64 65 72    if( leafReader
2f5cc 41 74 45 6e 64 28 26 70 52 65 61 64 65 72 2d 3e  AtEnd(&pReader->
2f5cd 6c 65 61 66 52 65 61 64 65 72 29 20 29 7b 0a 20  leafReader) ){. 
2f5ce 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 69     int rc;.    i
2f5cf 66 28 20 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74  f( pReader->root
2f5d0 44 61 74 61 2e 70 44 61 74 61 20 29 7b 0a 20 20  Data.pData ){.  
2f5d1 20 20 20 20 70 52 65 61 64 65 72 2d 3e 65 6f 66      pReader->eof
2f5d2 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75   = 1;.      retu
2f5d3 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
2f5d4 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c    }.    rc = sql
2f5d5 69 74 65 33 5f 73 74 65 70 28 70 52 65 61 64 65  ite3_step(pReade
2f5d6 72 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 69  r->pStmt);.    i
2f5d7 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f  f( rc!=SQLITE_RO
2f5d8 57 20 29 7b 0a 20 20 20 20 20 20 70 52 65 61 64  W ){.      pRead
2f5d9 65 72 2d 3e 65 6f 66 20 3d 20 31 3b 0a 20 20 20  er->eof = 1;.   
2f5da 20 20 20 72 65 74 75 72 6e 20 72 63 3d 3d 53 51     return rc==SQ
2f5db 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53 51 4c 49  LITE_DONE ? SQLI
2f5dc 54 45 5f 4f 4b 20 3a 20 72 63 3b 0a 20 20 20 20  TE_OK : rc;.    
2f5dd 7d 0a 20 20 20 20 6c 65 61 66 52 65 61 64 65 72  }.    leafReader
2f5de 44 65 73 74 72 6f 79 28 26 70 52 65 61 64 65 72  Destroy(&pReader
2f5df 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 20  ->leafReader);. 
2f5e0 20 20 20 6c 65 61 66 52 65 61 64 65 72 49 6e 69     leafReaderIni
2f5e1 74 28 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  t(sqlite3_column
2f5e2 5f 62 6c 6f 62 28 70 52 65 61 64 65 72 2d 3e 70  _blob(pReader->p
2f5e3 53 74 6d 74 2c 20 30 29 2c 0a 20 20 20 20 20 20  Stmt, 0),.      
2f5e4 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
2f5e5 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
2f5e6 73 28 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74  s(pReader->pStmt
2f5e7 2c 20 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20  , 0),.          
2f5e8 20 20 20 20 20 20 20 20 20 26 70 52 65 61 64 65           &pReade
2f5e9 72 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a  r->leafReader);.
2f5ea 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
2f5eb 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4f 72  ITE_OK;.}../* Or
2f5ec 64 65 72 20 4c 65 61 76 65 73 52 65 61 64 65 72  der LeavesReader
2f5ed 73 20 62 79 20 74 68 65 69 72 20 74 65 72 6d 2c  s by their term,
2f5ee 20 69 67 6e 6f 72 69 6e 67 20 69 64 78 2e 20 20   ignoring idx.  
2f5ef 52 65 61 64 65 72 73 20 61 74 20 65 6f 66 0a 2a  Readers at eof.*
2f5f0 2a 20 61 6c 77 61 79 73 20 73 6f 72 74 20 74 6f  * always sort to
2f5f1 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61   the end..*/.sta
2f5f2 74 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65  tic int leavesRe
2f5f3 61 64 65 72 54 65 72 6d 43 6d 70 28 4c 65 61 76  aderTermCmp(Leav
2f5f4 65 73 52 65 61 64 65 72 20 2a 6c 72 31 2c 20 4c  esReader *lr1, L
2f5f5 65 61 76 65 73 52 65 61 64 65 72 20 2a 6c 72 32  eavesReader *lr2
2f5f6 29 7b 0a 20 20 69 66 28 20 6c 65 61 76 65 73 52  ){.  if( leavesR
2f5f7 65 61 64 65 72 41 74 45 6e 64 28 6c 72 31 29 20  eaderAtEnd(lr1) 
2f5f8 29 7b 0a 20 20 20 20 69 66 28 20 6c 65 61 76 65  ){.    if( leave
2f5f9 73 52 65 61 64 65 72 41 74 45 6e 64 28 6c 72 32  sReaderAtEnd(lr2
2f5fa 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  ) ) return 0;.  
2f5fb 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
2f5fc 20 20 69 66 28 20 6c 65 61 76 65 73 52 65 61 64    if( leavesRead
2f5fd 65 72 41 74 45 6e 64 28 6c 72 32 29 20 29 20 72  erAtEnd(lr2) ) r
2f5fe 65 74 75 72 6e 20 2d 31 3b 0a 0a 20 20 72 65 74  eturn -1;..  ret
2f5ff 75 72 6e 20 6c 65 61 66 52 65 61 64 65 72 54 65  urn leafReaderTe
2f600 72 6d 43 6d 70 28 26 6c 72 31 2d 3e 6c 65 61 66  rmCmp(&lr1->leaf
2f601 52 65 61 64 65 72 2c 0a 20 20 20 20 20 20 20 20  Reader,.        
2f602 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f603 20 20 20 6c 65 61 76 65 73 52 65 61 64 65 72 54     leavesReaderT
2f604 65 72 6d 28 6c 72 32 29 2c 20 6c 65 61 76 65 73  erm(lr2), leaves
2f605 52 65 61 64 65 72 54 65 72 6d 42 79 74 65 73 28  ReaderTermBytes(
2f606 6c 72 32 29 2c 0a 20 20 20 20 20 20 20 20 20 20  lr2),.          
2f607 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f608 20 30 29 3b 0a 7d 0a 0a 2f 2a 20 53 69 6d 69 6c   0);.}../* Simil
2f609 61 72 20 74 6f 20 6c 65 61 76 65 73 52 65 61 64  ar to leavesRead
2f60a 65 72 54 65 72 6d 43 6d 70 28 29 2c 20 77 69 74  erTermCmp(), wit
2f60b 68 20 61 64 64 69 74 69 6f 6e 61 6c 20 6f 72 64  h additional ord
2f60c 65 72 69 6e 67 20 62 79 20 69 64 78 0a 2a 2a 20  ering by idx.** 
2f60d 73 6f 20 74 68 61 74 20 6f 6c 64 65 72 20 73 65  so that older se
2f60e 67 6d 65 6e 74 73 20 73 6f 72 74 20 62 65 66 6f  gments sort befo
2f60f 72 65 20 6e 65 77 65 72 20 73 65 67 6d 65 6e 74  re newer segment
2f610 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
2f611 20 6c 65 61 76 65 73 52 65 61 64 65 72 43 6d 70   leavesReaderCmp
2f612 28 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 6c  (LeavesReader *l
2f613 72 31 2c 20 4c 65 61 76 65 73 52 65 61 64 65 72  r1, LeavesReader
2f614 20 2a 6c 72 32 29 7b 0a 20 20 69 6e 74 20 63 20   *lr2){.  int c 
2f615 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65  = leavesReaderTe
2f616 72 6d 43 6d 70 28 6c 72 31 2c 20 6c 72 32 29 3b  rmCmp(lr1, lr2);
2f617 0a 20 20 69 66 28 20 63 21 3d 30 20 29 20 72 65  .  if( c!=0 ) re
2f618 74 75 72 6e 20 63 3b 0a 20 20 72 65 74 75 72 6e  turn c;.  return
2f619 20 6c 72 31 2d 3e 69 64 78 2d 6c 72 32 2d 3e 69   lr1->idx-lr2->i
2f61a 64 78 3b 0a 7d 0a 0a 2f 2a 20 41 73 73 75 6d 65  dx;.}../* Assume
2f61b 20 74 68 61 74 20 70 4c 72 5b 31 5d 2e 2e 70 4c   that pLr[1]..pL
2f61c 72 5b 6e 4c 72 5d 20 61 72 65 20 73 6f 72 74 65  r[nLr] are sorte
2f61d 64 2e 20 20 42 75 62 62 6c 65 20 70 4c 72 5b 30  d.  Bubble pLr[0
2f61e 5d 20 69 6e 74 6f 20 69 74 73 0a 2a 2a 20 73 6f  ] into its.** so
2f61f 72 74 65 64 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a  rted position..*
2f620 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65  /.static void le
2f621 61 76 65 73 52 65 61 64 65 72 52 65 6f 72 64 65  avesReaderReorde
2f622 72 28 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  r(LeavesReader *
2f623 70 4c 72 2c 20 69 6e 74 20 6e 4c 72 29 7b 0a 20  pLr, int nLr){. 
2f624 20 77 68 69 6c 65 28 20 6e 4c 72 3e 31 20 26 26   while( nLr>1 &&
2f625 20 6c 65 61 76 65 73 52 65 61 64 65 72 43 6d 70   leavesReaderCmp
2f626 28 70 4c 72 2c 20 70 4c 72 2b 31 29 3e 30 20 29  (pLr, pLr+1)>0 )
2f627 7b 0a 20 20 20 20 4c 65 61 76 65 73 52 65 61 64  {.    LeavesRead
2f628 65 72 20 74 6d 70 20 3d 20 70 4c 72 5b 30 5d 3b  er tmp = pLr[0];
2f629 0a 20 20 20 20 70 4c 72 5b 30 5d 20 3d 20 70 4c  .    pLr[0] = pL
2f62a 72 5b 31 5d 3b 0a 20 20 20 20 70 4c 72 5b 31 5d  r[1];.    pLr[1]
2f62b 20 3d 20 74 6d 70 3b 0a 20 20 20 20 6e 4c 72 2d   = tmp;.    nLr-
2f62c 2d 3b 0a 20 20 20 20 70 4c 72 2b 2b 3b 0a 20 20  -;.    pLr++;.  
2f62d 7d 0a 7d 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69  }.}../* Initiali
2f62e 7a 65 73 20 70 52 65 61 64 65 72 73 20 77 69 74  zes pReaders wit
2f62f 68 20 74 68 65 20 73 65 67 6d 65 6e 74 73 20 66  h the segments f
2f630 72 6f 6d 20 6c 65 76 65 6c 20 69 4c 65 76 65 6c  rom level iLevel
2f631 2c 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 74  , returning.** t
2f632 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 67  he number of seg
2f633 6d 65 6e 74 73 20 69 6e 20 2a 70 69 52 65 61 64  ments in *piRead
2f634 65 72 73 2e 20 20 4c 65 61 76 65 73 20 70 52 65  ers.  Leaves pRe
2f635 61 64 65 72 73 20 69 6e 20 73 6f 72 74 65 64 0a  aders in sorted.
2f636 2a 2a 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61  ** order..*/.sta
2f637 74 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65  tic int leavesRe
2f638 61 64 65 72 73 49 6e 69 74 28 66 75 6c 6c 74 65  adersInit(fullte
2f639 78 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20  xt_vtab *v, int 
2f63a 69 4c 65 76 65 6c 2c 0a 20 20 20 20 20 20 20 20  iLevel,.        
2f63b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f63c 20 20 20 20 20 4c 65 61 76 65 73 52 65 61 64 65       LeavesReade
2f63d 72 20 2a 70 52 65 61 64 65 72 73 2c 20 69 6e 74  r *pReaders, int
2f63e 20 2a 70 69 52 65 61 64 65 72 73 29 7b 0a 20 20   *piReaders){.  
2f63f 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b  sqlite3_stmt *s;
2f640 0a 20 20 69 6e 74 20 69 2c 20 72 63 20 3d 20 73  .  int i, rc = s
2f641 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
2f642 28 76 2c 20 53 45 47 44 49 52 5f 53 45 4c 45 43  (v, SEGDIR_SELEC
2f643 54 5f 4c 45 56 45 4c 5f 53 54 4d 54 2c 20 26 73  T_LEVEL_STMT, &s
2f644 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
2f645 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
2f646 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  rc;..  rc = sqli
2f647 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20  te3_bind_int(s, 
2f648 31 2c 20 69 4c 65 76 65 6c 29 3b 0a 20 20 69 66  1, iLevel);.  if
2f649 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2f64a 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
2f64b 69 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20  i = 0;.  while( 
2f64c 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74  (rc = sqlite3_st
2f64d 65 70 28 73 29 29 3d 3d 53 51 4c 49 54 45 5f 52  ep(s))==SQLITE_R
2f64e 4f 57 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OW ){.    sqlite
2f64f 5f 69 6e 74 36 34 20 69 53 74 61 72 74 20 3d 20  _int64 iStart = 
2f650 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
2f651 6e 74 36 34 28 73 2c 20 30 29 3b 0a 20 20 20 20  nt64(s, 0);.    
2f652 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45 6e  sqlite_int64 iEn
2f653 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  d = sqlite3_colu
2f654 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29 3b 0a  mn_int64(s, 1);.
2f655 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2f656 70 52 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c 69  pRootData = sqli
2f657 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
2f658 73 2c 20 32 29 3b 0a 20 20 20 20 69 6e 74 20 6e  s, 2);.    int n
2f659 52 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c 69 74  RootData = sqlit
2f65a 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
2f65b 73 2c 20 32 29 3b 0a 0a 20 20 20 20 61 73 73 65  s, 2);..    asse
2f65c 72 74 28 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e  rt( i<MERGE_COUN
2f65d 54 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 6c 65  T );.    rc = le
2f65e 61 76 65 73 52 65 61 64 65 72 49 6e 69 74 28 76  avesReaderInit(v
2f65f 2c 20 69 2c 20 69 53 74 61 72 74 2c 20 69 45 6e  , i, iStart, iEn
2f660 64 2c 20 70 52 6f 6f 74 44 61 74 61 2c 20 6e 52  d, pRootData, nR
2f661 6f 6f 74 44 61 74 61 2c 0a 20 20 20 20 20 20 20  ootData,.       
2f662 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f663 20 20 20 26 70 52 65 61 64 65 72 73 5b 69 5d 29     &pReaders[i])
2f664 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
2f665 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b  LITE_OK ) break;
2f666 0a 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 20  ..    i++;.  }. 
2f667 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2f668 44 4f 4e 45 20 29 7b 0a 20 20 20 20 77 68 69 6c  DONE ){.    whil
2f669 65 28 20 69 2d 2d 3e 30 20 29 7b 0a 20 20 20 20  e( i-->0 ){.    
2f66a 20 20 6c 65 61 76 65 73 52 65 61 64 65 72 44 65    leavesReaderDe
2f66b 73 74 72 6f 79 28 26 70 52 65 61 64 65 72 73 5b  stroy(&pReaders[
2f66c 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  i]);.    }.    r
2f66d 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
2f66e 20 2a 70 69 52 65 61 64 65 72 73 20 3d 20 69 3b   *piReaders = i;
2f66f 0a 0a 20 20 2f 2a 20 4c 65 61 76 65 20 6f 75 72  ..  /* Leave our
2f670 20 72 65 73 75 6c 74 73 20 73 6f 72 74 65 64 20   results sorted 
2f671 62 79 20 74 65 72 6d 2c 20 74 68 65 6e 20 61 67  by term, then ag
2f672 65 2e 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69  e. */.  while( i
2f673 2d 2d 20 29 7b 0a 20 20 20 20 6c 65 61 76 65 73  -- ){.    leaves
2f674 52 65 61 64 65 72 52 65 6f 72 64 65 72 28 70 52  ReaderReorder(pR
2f675 65 61 64 65 72 73 2b 69 2c 20 2a 70 69 52 65 61  eaders+i, *piRea
2f676 64 65 72 73 2d 69 29 3b 0a 20 20 7d 0a 20 20 72  ders-i);.  }.  r
2f677 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2f678 0a 7d 0a 0a 2f 2a 20 4d 65 72 67 65 20 64 6f 63  .}../* Merge doc
2f679 6c 69 73 74 73 20 66 72 6f 6d 20 70 52 65 61 64  lists from pRead
2f67a 65 72 73 5b 6e 52 65 61 64 65 72 73 5d 20 69 6e  ers[nReaders] in
2f67b 74 6f 20 61 20 73 69 6e 67 6c 65 20 64 6f 63 6c  to a single docl
2f67c 69 73 74 2c 20 77 68 69 63 68 0a 2a 2a 20 69 73  ist, which.** is
2f67d 20 77 72 69 74 74 65 6e 20 74 6f 20 70 57 72 69   written to pWri
2f67e 74 65 72 2e 20 20 41 73 73 75 6d 65 73 20 70 52  ter.  Assumes pR
2f67f 65 61 64 65 72 73 20 69 73 20 6f 72 64 65 72 65  eaders is ordere
2f680 64 20 6f 6c 64 65 73 74 20 74 6f 0a 2a 2a 20 6e  d oldest to.** n
2f681 65 77 65 73 74 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44  ewest..*/./* TOD
2f682 4f 28 73 68 65 73 73 29 20 43 6f 6e 73 69 64 65  O(shess) Conside
2f683 72 20 70 75 74 74 69 6e 67 20 74 68 69 73 20 69  r putting this i
2f684 6e 6c 69 6e 65 20 69 6e 20 73 65 67 6d 65 6e 74  nline in segment
2f685 4d 65 72 67 65 28 29 2e 20 2a 2f 0a 73 74 61 74  Merge(). */.stat
2f686 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61  ic int leavesRea
2f687 64 65 72 73 4d 65 72 67 65 28 66 75 6c 6c 74 65  dersMerge(fullte
2f688 78 74 5f 76 74 61 62 20 2a 76 2c 0a 20 20 20 20  xt_vtab *v,.    
2f689 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f68a 20 20 20 20 20 20 20 20 20 20 4c 65 61 76 65 73            Leaves
2f68b 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 73  Reader *pReaders
2f68c 2c 20 69 6e 74 20 6e 52 65 61 64 65 72 73 2c 0a  , int nReaders,.
2f68d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f68e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4c 65                Le
2f68f 61 66 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  afWriter *pWrite
2f690 72 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 64  r){.  DLReader d
2f691 6c 52 65 61 64 65 72 73 5b 4d 45 52 47 45 5f 43  lReaders[MERGE_C
2f692 4f 55 4e 54 5d 3b 0a 20 20 63 6f 6e 73 74 20 63  OUNT];.  const c
2f693 68 61 72 20 2a 70 54 65 72 6d 20 3d 20 6c 65 61  har *pTerm = lea
2f694 76 65 73 52 65 61 64 65 72 54 65 72 6d 28 70 52  vesReaderTerm(pR
2f695 65 61 64 65 72 73 29 3b 0a 20 20 69 6e 74 20 69  eaders);.  int i
2f696 2c 20 6e 54 65 72 6d 20 3d 20 6c 65 61 76 65 73  , nTerm = leaves
2f697 52 65 61 64 65 72 54 65 72 6d 42 79 74 65 73 28  ReaderTermBytes(
2f698 70 52 65 61 64 65 72 73 29 3b 0a 0a 20 20 61 73  pReaders);..  as
2f699 73 65 72 74 28 20 6e 52 65 61 64 65 72 73 3c 3d  sert( nReaders<=
2f69a 4d 45 52 47 45 5f 43 4f 55 4e 54 20 29 3b 0a 0a  MERGE_COUNT );..
2f69b 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65    for(i=0; i<nRe
2f69c 61 64 65 72 73 3b 20 69 2b 2b 29 7b 0a 20 20 20  aders; i++){.   
2f69d 20 64 6c 72 49 6e 69 74 28 26 64 6c 52 65 61 64   dlrInit(&dlRead
2f69e 65 72 73 5b 69 5d 2c 20 44 4c 5f 44 45 46 41 55  ers[i], DL_DEFAU
2f69f 4c 54 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  LT,.            
2f6a0 6c 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61  leavesReaderData
2f6a1 28 70 52 65 61 64 65 72 73 2b 69 29 2c 0a 20 20  (pReaders+i),.  
2f6a2 20 20 20 20 20 20 20 20 20 20 6c 65 61 76 65 73            leaves
2f6a3 52 65 61 64 65 72 44 61 74 61 42 79 74 65 73 28  ReaderDataBytes(
2f6a4 70 52 65 61 64 65 72 73 2b 69 29 29 3b 0a 20 20  pReaders+i));.  
2f6a5 7d 0a 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 66  }..  return leaf
2f6a6 57 72 69 74 65 72 53 74 65 70 4d 65 72 67 65 28  WriterStepMerge(
2f6a7 76 2c 20 70 57 72 69 74 65 72 2c 20 70 54 65 72  v, pWriter, pTer
2f6a8 6d 2c 20 6e 54 65 72 6d 2c 20 64 6c 52 65 61 64  m, nTerm, dlRead
2f6a9 65 72 73 2c 20 6e 52 65 61 64 65 72 73 29 3b 0a  ers, nReaders);.
2f6aa 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65  }../* Forward re
2f6ab 66 20 64 75 65 20 74 6f 20 6d 75 74 75 61 6c 20  f due to mutual 
2f6ac 72 65 63 75 72 73 69 6f 6e 20 77 69 74 68 20 73  recursion with s
2f6ad 65 67 64 69 72 4e 65 78 74 49 6e 64 65 78 28 29  egdirNextIndex()
2f6ae 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  . */.static int 
2f6af 73 65 67 6d 65 6e 74 4d 65 72 67 65 28 66 75 6c  segmentMerge(ful
2f6b0 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69  ltext_vtab *v, i
2f6b1 6e 74 20 69 4c 65 76 65 6c 29 3b 0a 0a 2f 2a 20  nt iLevel);../* 
2f6b2 50 75 74 20 74 68 65 20 6e 65 78 74 20 61 76 61  Put the next ava
2f6b3 69 6c 61 62 6c 65 20 69 6e 64 65 78 20 61 74 20  ilable index at 
2f6b4 69 4c 65 76 65 6c 20 69 6e 74 6f 20 2a 70 69 64  iLevel into *pid
2f6b5 78 2e 20 20 49 66 20 69 4c 65 76 65 6c 0a 2a 2a  x.  If iLevel.**
2f6b6 20 61 6c 72 65 61 64 79 20 68 61 73 20 4d 45 52   already has MER
2f6b7 47 45 5f 43 4f 55 4e 54 20 73 65 67 6d 65 6e 74  GE_COUNT segment
2f6b8 73 2c 20 74 68 65 79 20 61 72 65 20 6d 65 72 67  s, they are merg
2f6b9 65 64 20 74 6f 20 61 20 68 69 67 68 65 72 0a 2a  ed to a higher.*
2f6ba 2a 20 6c 65 76 65 6c 20 74 6f 20 6d 61 6b 65 20  * level to make 
2f6bb 72 6f 6f 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  room..*/.static 
2f6bc 69 6e 74 20 73 65 67 64 69 72 4e 65 78 74 49 6e  int segdirNextIn
2f6bd 64 65 78 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  dex(fulltext_vta
2f6be 62 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c  b *v, int iLevel
2f6bf 2c 20 69 6e 74 20 2a 70 69 64 78 29 7b 0a 20 20  , int *pidx){.  
2f6c0 69 6e 74 20 72 63 20 3d 20 73 65 67 64 69 72 5f  int rc = segdir_
2f6c1 6d 61 78 5f 69 6e 64 65 78 28 76 2c 20 69 4c 65  max_index(v, iLe
2f6c2 76 65 6c 2c 20 70 69 64 78 29 3b 0a 20 20 69 66  vel, pidx);.  if
2f6c3 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
2f6c4 45 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  E ){            
2f6c5 20 20 2f 2a 20 4e 6f 20 73 65 67 6d 65 6e 74 73    /* No segments
2f6c6 20 61 74 20 69 4c 65 76 65 6c 2e 20 2a 2f 0a 20   at iLevel. */. 
2f6c7 20 20 20 2a 70 69 64 78 20 3d 20 30 3b 0a 20 20     *pidx = 0;.  
2f6c8 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51  }else if( rc==SQ
2f6c9 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
2f6ca 69 66 28 20 2a 70 69 64 78 3d 3d 28 4d 45 52 47  if( *pidx==(MERG
2f6cb 45 5f 43 4f 55 4e 54 2d 31 29 20 29 7b 0a 20 20  E_COUNT-1) ){.  
2f6cc 20 20 20 20 72 63 20 3d 20 73 65 67 6d 65 6e 74      rc = segment
2f6cd 4d 65 72 67 65 28 76 2c 20 69 4c 65 76 65 6c 29  Merge(v, iLevel)
2f6ce 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
2f6cf 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
2f6d0 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 2a 70 69  rn rc;.      *pi
2f6d1 64 78 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  dx = 0;.    }els
2f6d2 65 7b 0a 20 20 20 20 20 20 28 2a 70 69 64 78 29  e{.      (*pidx)
2f6d3 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  ++;.    }.  }els
2f6d4 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  e{.    return rc
2f6d5 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
2f6d6 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20  QLITE_OK;.}../* 
2f6d7 4d 65 72 67 65 20 4d 45 52 47 45 5f 43 4f 55 4e  Merge MERGE_COUN
2f6d8 54 20 73 65 67 6d 65 6e 74 73 20 61 74 20 69 4c  T segments at iL
2f6d9 65 76 65 6c 20 69 6e 74 6f 20 61 20 6e 65 77 20  evel into a new 
2f6da 73 65 67 6d 65 6e 74 20 61 74 0a 2a 2a 20 69 4c  segment at.** iL
2f6db 65 76 65 6c 2b 31 2e 20 20 49 66 20 69 4c 65 76  evel+1.  If iLev
2f6dc 65 6c 2b 31 20 69 73 20 61 6c 72 65 61 64 79 20  el+1 is already 
2f6dd 66 75 6c 6c 20 6f 66 20 73 65 67 6d 65 6e 74 73  full of segments
2f6de 2c 20 74 68 6f 73 65 20 77 69 6c 6c 20 62 65 0a  , those will be.
2f6df 2a 2a 20 6d 65 72 67 65 64 20 74 6f 20 6d 61 6b  ** merged to mak
2f6e0 65 20 72 6f 6f 6d 2e 0a 2a 2f 0a 73 74 61 74 69  e room..*/.stati
2f6e1 63 20 69 6e 74 20 73 65 67 6d 65 6e 74 4d 65 72  c int segmentMer
2f6e2 67 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  ge(fulltext_vtab
2f6e3 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 29   *v, int iLevel)
2f6e4 7b 0a 20 20 4c 65 61 66 57 72 69 74 65 72 20 77  {.  LeafWriter w
2f6e5 72 69 74 65 72 3b 0a 20 20 4c 65 61 76 65 73 52  riter;.  LeavesR
2f6e6 65 61 64 65 72 20 6c 72 73 5b 4d 45 52 47 45 5f  eader lrs[MERGE_
2f6e7 43 4f 55 4e 54 5d 3b 0a 20 20 69 6e 74 20 69 2c  COUNT];.  int i,
2f6e8 20 72 63 2c 20 69 64 78 20 3d 20 30 3b 0a 0a 20   rc, idx = 0;.. 
2f6e9 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68   /* Determine th
2f6ea 65 20 6e 65 78 74 20 61 76 61 69 6c 61 62 6c 65  e next available
2f6eb 20 73 65 67 6d 65 6e 74 20 69 6e 64 65 78 20 61   segment index a
2f6ec 74 20 74 68 65 20 6e 65 78 74 20 6c 65 76 65 6c  t the next level
2f6ed 2c 0a 20 20 2a 2a 20 6d 65 72 67 69 6e 67 20 61  ,.  ** merging a
2f6ee 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 20 20 2a  s necessary..  *
2f6ef 2f 0a 20 20 72 63 20 3d 20 73 65 67 64 69 72 4e  /.  rc = segdirN
2f6f0 65 78 74 49 6e 64 65 78 28 76 2c 20 69 4c 65 76  extIndex(v, iLev
2f6f1 65 6c 2b 31 2c 20 26 69 64 78 29 3b 0a 20 20 69  el+1, &idx);.  i
2f6f2 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2f6f3 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
2f6f4 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20   /* TODO(shess) 
2f6f5 54 68 69 73 20 61 73 73 75 6d 65 73 20 74 68 61  This assumes tha
2f6f6 74 20 77 65 27 6c 6c 20 61 6c 77 61 79 73 20 73  t we'll always s
2f6f7 65 65 20 65 78 61 63 74 6c 79 0a 20 20 2a 2a 20  ee exactly.  ** 
2f6f8 4d 45 52 47 45 5f 43 4f 55 4e 54 20 73 65 67 6d  MERGE_COUNT segm
2f6f9 65 6e 74 73 20 74 6f 20 6d 65 72 67 65 20 61 74  ents to merge at
2f6fa 20 61 20 67 69 76 65 6e 20 6c 65 76 65 6c 2e 20   a given level. 
2f6fb 20 54 68 61 74 20 77 69 6c 6c 20 62 65 0a 20 20   That will be.  
2f6fc 2a 2a 20 62 72 6f 6b 65 6e 20 69 66 20 77 65 20  ** broken if we 
2f6fd 61 6c 6c 6f 77 20 74 68 65 20 64 65 76 65 6c 6f  allow the develo
2f6fe 70 65 72 20 74 6f 20 72 65 71 75 65 73 74 20 70  per to request p
2f6ff 72 65 65 6d 70 74 69 76 65 20 6f 72 0a 20 20 2a  reemptive or.  *
2f700 2a 20 64 65 66 65 72 72 65 64 20 6d 65 72 67 69  * deferred mergi
2f701 6e 67 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65  ng..  */.  memse
2f702 74 28 26 6c 72 73 2c 20 27 5c 30 27 2c 20 73 69  t(&lrs, '\0', si
2f703 7a 65 6f 66 28 6c 72 73 29 29 3b 0a 20 20 72 63  zeof(lrs));.  rc
2f704 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 73   = leavesReaders
2f705 49 6e 69 74 28 76 2c 20 69 4c 65 76 65 6c 2c 20  Init(v, iLevel, 
2f706 6c 72 73 2c 20 26 69 29 3b 0a 20 20 69 66 28 20  lrs, &i);.  if( 
2f707 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2f708 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 61 73 73  return rc;.  ass
2f709 65 72 74 28 20 69 3d 3d 4d 45 52 47 45 5f 43 4f  ert( i==MERGE_CO
2f70a 55 4e 54 20 29 3b 0a 0a 20 20 6c 65 61 66 57 72  UNT );..  leafWr
2f70b 69 74 65 72 49 6e 69 74 28 69 4c 65 76 65 6c 2b  iterInit(iLevel+
2f70c 31 2c 20 69 64 78 2c 20 26 77 72 69 74 65 72 29  1, idx, &writer)
2f70d 3b 0a 0a 20 20 2f 2a 20 53 69 6e 63 65 20 6c 65  ;..  /* Since le
2f70e 61 76 65 73 52 65 61 64 65 72 52 65 6f 72 64 65  avesReaderReorde
2f70f 72 28 29 20 70 75 73 68 65 73 20 72 65 61 64 65  r() pushes reade
2f710 72 73 20 61 74 20 65 6f 66 20 74 6f 20 74 68 65  rs at eof to the
2f711 20 65 6e 64 2c 0a 20 20 2a 2a 20 77 68 65 6e 20   end,.  ** when 
2f712 74 68 65 20 66 69 72 73 74 20 72 65 61 64 65 72  the first reader
2f713 20 69 73 20 65 6d 70 74 79 2c 20 61 6c 6c 20 77   is empty, all w
2f714 69 6c 6c 20 62 65 20 65 6d 70 74 79 2e 0a 20 20  ill be empty..  
2f715 2a 2f 0a 20 20 77 68 69 6c 65 28 20 21 6c 65 61  */.  while( !lea
2f716 76 65 73 52 65 61 64 65 72 41 74 45 6e 64 28 6c  vesReaderAtEnd(l
2f717 72 73 29 20 29 7b 0a 20 20 20 20 2f 2a 20 46 69  rs) ){.    /* Fi
2f718 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e  gure out how man
2f719 79 20 72 65 61 64 65 72 73 20 73 68 61 72 65 20  y readers share 
2f71a 74 68 65 69 72 20 6e 65 78 74 20 74 65 72 6d 2e  their next term.
2f71b 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b   */.    for(i=1;
2f71c 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 20 26   i<MERGE_COUNT &
2f71d 26 20 21 6c 65 61 76 65 73 52 65 61 64 65 72 41  & !leavesReaderA
2f71e 74 45 6e 64 28 6c 72 73 2b 69 29 3b 20 69 2b 2b  tEnd(lrs+i); i++
2f71f 29 7b 0a 20 20 20 20 20 20 69 66 28 20 30 21 3d  ){.      if( 0!=
2f720 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d  leavesReaderTerm
2f721 43 6d 70 28 6c 72 73 2c 20 6c 72 73 2b 69 29 20  Cmp(lrs, lrs+i) 
2f722 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a  ) break;.    }..
2f723 20 20 20 20 72 63 20 3d 20 6c 65 61 76 65 73 52      rc = leavesR
2f724 65 61 64 65 72 73 4d 65 72 67 65 28 76 2c 20 6c  eadersMerge(v, l
2f725 72 73 2c 20 69 2c 20 26 77 72 69 74 65 72 29 3b  rs, i, &writer);
2f726 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
2f727 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72  ITE_OK ) goto er
2f728 72 3b 0a 0a 20 20 20 20 2f 2a 20 53 74 65 70 20  r;..    /* Step 
2f729 66 6f 72 77 61 72 64 20 74 68 6f 73 65 20 74 68  forward those th
2f72a 61 74 20 77 65 72 65 20 6d 65 72 67 65 64 2e 20  at were merged. 
2f72b 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 69 2d  */.    while( i-
2f72c 2d 3e 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ->0 ){.      rc 
2f72d 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 53 74  = leavesReaderSt
2f72e 65 70 28 76 2c 20 6c 72 73 2b 69 29 3b 0a 20 20  ep(v, lrs+i);.  
2f72f 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
2f730 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72  TE_OK ) goto err
2f731 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 52 65 6f 72  ;..      /* Reor
2f732 64 65 72 20 62 79 20 74 65 72 6d 2c 20 74 68 65  der by term, the
2f733 6e 20 62 79 20 61 67 65 2e 20 2a 2f 0a 20 20 20  n by age. */.   
2f734 20 20 20 6c 65 61 76 65 73 52 65 61 64 65 72 52     leavesReaderR
2f735 65 6f 72 64 65 72 28 6c 72 73 2b 69 2c 20 4d 45  eorder(lrs+i, ME
2f736 52 47 45 5f 43 4f 55 4e 54 2d 69 29 3b 0a 20 20  RGE_COUNT-i);.  
2f737 20 20 7d 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69    }.  }..  for(i
2f738 3d 30 3b 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e  =0; i<MERGE_COUN
2f739 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6c 65 61  T; i++){.    lea
2f73a 76 65 73 52 65 61 64 65 72 44 65 73 74 72 6f 79  vesReaderDestroy
2f73b 28 26 6c 72 73 5b 69 5d 29 3b 0a 20 20 7d 0a 0a  (&lrs[i]);.  }..
2f73c 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65    rc = leafWrite
2f73d 72 46 69 6e 61 6c 69 7a 65 28 76 2c 20 26 77 72  rFinalize(v, &wr
2f73e 69 74 65 72 29 3b 0a 20 20 6c 65 61 66 57 72 69  iter);.  leafWri
2f73f 74 65 72 44 65 73 74 72 6f 79 28 26 77 72 69 74  terDestroy(&writ
2f740 65 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  er);.  if( rc!=S
2f741 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
2f742 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65  n rc;..  /* Dele
2f743 74 65 20 74 68 65 20 6d 65 72 67 65 64 20 73 65  te the merged se
2f744 67 6d 65 6e 74 20 64 61 74 61 2e 20 2a 2f 0a 20  gment data. */. 
2f745 20 72 65 74 75 72 6e 20 73 65 67 64 69 72 5f 64   return segdir_d
2f746 65 6c 65 74 65 28 76 2c 20 69 4c 65 76 65 6c 29  elete(v, iLevel)
2f747 3b 0a 0a 20 65 72 72 3a 0a 20 20 66 6f 72 28 69  ;.. err:.  for(i
2f748 3d 30 3b 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e  =0; i<MERGE_COUN
2f749 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6c 65 61  T; i++){.    lea
2f74a 76 65 73 52 65 61 64 65 72 44 65 73 74 72 6f 79  vesReaderDestroy
2f74b 28 26 6c 72 73 5b 69 5d 29 3b 0a 20 20 7d 0a 20  (&lrs[i]);.  }. 
2f74c 20 6c 65 61 66 57 72 69 74 65 72 44 65 73 74 72   leafWriterDestr
2f74d 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 20 20 72  oy(&writer);.  r
2f74e 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
2f74f 41 63 63 75 6d 75 6c 61 74 65 20 74 68 65 20 75  Accumulate the u
2f750 6e 69 6f 6e 20 6f 66 20 2a 61 63 63 20 61 6e 64  nion of *acc and
2f751 20 2a 70 44 61 74 61 20 69 6e 74 6f 20 2a 61 63   *pData into *ac
2f752 63 2e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  c. */.static voi
2f753 64 20 64 6f 63 4c 69 73 74 41 63 63 75 6d 75 6c  d docListAccumul
2f754 61 74 65 55 6e 69 6f 6e 28 44 61 74 61 42 75 66  ateUnion(DataBuf
2f755 66 65 72 20 2a 61 63 63 2c 0a 20 20 20 20 20 20  fer *acc,.      
2f756 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f757 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
2f758 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
2f759 69 6e 74 20 6e 44 61 74 61 29 20 7b 0a 20 20 44  int nData) {.  D
2f75a 61 74 61 42 75 66 66 65 72 20 74 6d 70 20 3d 20  ataBuffer tmp = 
2f75b 2a 61 63 63 3b 0a 20 20 64 61 74 61 42 75 66 66  *acc;.  dataBuff
2f75c 65 72 49 6e 69 74 28 61 63 63 2c 20 74 6d 70 2e  erInit(acc, tmp.
2f75d 6e 44 61 74 61 2b 6e 44 61 74 61 29 3b 0a 20 20  nData+nData);.  
2f75e 64 6f 63 4c 69 73 74 55 6e 69 6f 6e 28 74 6d 70  docListUnion(tmp
2f75f 2e 70 44 61 74 61 2c 20 74 6d 70 2e 6e 44 61 74  .pData, tmp.nDat
2f760 61 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c  a, pData, nData,
2f761 20 61 63 63 29 3b 0a 20 20 64 61 74 61 42 75 66   acc);.  dataBuf
2f762 66 65 72 44 65 73 74 72 6f 79 28 26 74 6d 70 29  ferDestroy(&tmp)
2f763 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  ;.}../* TODO(she
2f764 73 73 29 20 49 74 20 6d 69 67 68 74 20 62 65 20  ss) It might be 
2f765 69 6e 74 65 72 65 73 74 69 6e 67 20 74 6f 20 65  interesting to e
2f766 78 70 6c 6f 72 65 20 64 69 66 66 65 72 65 6e 74  xplore different
2f767 20 6d 65 72 67 65 0a 2a 2a 20 73 74 72 61 74 65   merge.** strate
2f768 67 69 65 73 2c 20 68 65 72 65 2e 20 20 46 6f 72  gies, here.  For
2f769 20 69 6e 73 74 61 6e 63 65 2c 20 73 69 6e 63 65   instance, since
2f76a 20 74 68 69 73 20 69 73 20 61 20 73 6f 72 74 65   this is a sorte
2f76b 64 20 6d 65 72 67 65 2c 20 77 65 0a 2a 2a 20 63  d merge, we.** c
2f76c 6f 75 6c 64 20 65 61 73 69 6c 79 20 6d 65 72 67  ould easily merg
2f76d 65 20 6d 61 6e 79 20 64 6f 63 6c 69 73 74 73 20  e many doclists 
2f76e 69 6e 20 70 61 72 61 6c 6c 65 6c 2e 20 20 57 69  in parallel.  Wi
2f76f 74 68 20 73 6f 6d 65 0a 2a 2a 20 63 6f 6d 70 72  th some.** compr
2f770 65 68 65 6e 73 69 6f 6e 20 6f 66 20 74 68 65 20  ehension of the 
2f771 73 74 6f 72 61 67 65 20 66 6f 72 6d 61 74 2c 20  storage format, 
2f772 77 65 20 63 6f 75 6c 64 20 6d 65 72 67 65 20 61  we could merge a
2f773 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a 20 64 6f 63  ll of the.** doc
2f774 6c 69 73 74 73 20 77 69 74 68 69 6e 20 61 20 6c  lists within a l
2f775 65 61 66 20 6e 6f 64 65 20 64 69 72 65 63 74 6c  eaf node directl
2f776 79 20 66 72 6f 6d 20 74 68 65 20 6c 65 61 66 20  y from the leaf 
2f777 6e 6f 64 65 27 73 20 73 74 6f 72 61 67 65 2e 0a  node's storage..
2f778 2a 2a 20 49 74 20 6d 61 79 20 62 65 20 77 6f 72  ** It may be wor
2f779 74 68 77 68 69 6c 65 20 74 6f 20 6d 65 72 67 65  thwhile to merge
2f77a 20 73 6d 61 6c 6c 65 72 20 64 6f 63 6c 69 73 74   smaller doclist
2f77b 73 20 62 65 66 6f 72 65 20 6c 61 72 67 65 72 0a  s before larger.
2f77c 2a 2a 20 64 6f 63 6c 69 73 74 73 2c 20 73 69 6e  ** doclists, sin
2f77d 63 65 20 74 68 65 79 20 63 61 6e 20 62 65 20 74  ce they can be t
2f77e 72 61 76 65 72 73 65 64 20 6d 6f 72 65 20 71 75  raversed more qu
2f77f 69 63 6b 6c 79 20 2d 20 62 75 74 20 74 68 65 0a  ickly - but the.
2f780 2a 2a 20 72 65 73 75 6c 74 73 20 6d 61 79 20 68  ** results may h
2f781 61 76 65 20 6c 65 73 73 20 6f 76 65 72 6c 61 70  ave less overlap
2f782 2c 20 6d 61 6b 69 6e 67 20 74 68 65 6d 20 6d 6f  , making them mo
2f783 72 65 20 65 78 70 65 6e 73 69 76 65 20 69 6e 20  re expensive in 
2f784 61 0a 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 77  a.** different w
2f785 61 79 2e 0a 2a 2f 0a 0a 2f 2a 20 53 63 61 6e 20  ay..*/../* Scan 
2f786 70 52 65 61 64 65 72 20 66 6f 72 20 70 54 65 72  pReader for pTer
2f787 6d 2f 6e 54 65 72 6d 2c 20 61 6e 64 20 6d 65 72  m/nTerm, and mer
2f788 67 65 20 74 68 65 20 74 65 72 6d 27 73 20 64 6f  ge the term's do
2f789 63 6c 69 73 74 20 6f 76 65 72 0a 2a 2a 20 2a 6f  clist over.** *o
2f78a 75 74 20 28 61 6e 79 20 64 6f 63 6c 69 73 74 73  ut (any doclists
2f78b 20 77 69 74 68 20 64 75 70 6c 69 63 61 74 65 20   with duplicate 
2f78c 64 6f 63 69 64 73 20 6f 76 65 72 77 72 69 74 65  docids overwrite
2f78d 20 74 68 6f 73 65 20 69 6e 20 2a 6f 75 74 29 2e   those in *out).
2f78e 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e  .** Internal fun
2f78f 63 74 69 6f 6e 20 66 6f 72 20 6c 6f 61 64 53 65  ction for loadSe
2f790 67 6d 65 6e 74 4c 65 61 66 28 29 2e 0a 2a 2f 0a  gmentLeaf()..*/.
2f791 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 61 64 53  static int loadS
2f792 65 67 6d 65 6e 74 4c 65 61 76 65 73 49 6e 74 28  egmentLeavesInt(
2f793 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
2f794 2c 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  , LeavesReader *
2f795 70 52 65 61 64 65 72 2c 0a 20 20 20 20 20 20 20  pReader,.       
2f796 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f797 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
2f798 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20  har *pTerm, int 
2f799 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65  nTerm, int isPre
2f79a 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20  fix,.           
2f79b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f79c 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72 20       DataBuffer 
2f79d 2a 6f 75 74 29 7b 0a 20 20 2f 2a 20 64 6f 63 6c  *out){.  /* docl
2f79e 69 73 74 20 64 61 74 61 20 69 73 20 61 63 63 75  ist data is accu
2f79f 6d 75 6c 61 74 65 64 20 69 6e 74 6f 20 70 42 75  mulated into pBu
2f7a0 66 66 65 72 73 20 73 69 6d 69 6c 61 72 20 74 6f  ffers similar to
2f7a1 20 68 6f 77 20 6f 6e 65 20 64 6f 65 73 0a 20 20   how one does.  
2f7a2 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 20 69 6e 20  ** increment in 
2f7a3 62 69 6e 61 72 79 20 61 72 69 74 68 6d 65 74 69  binary arithmeti
2f7a4 63 2e 20 20 49 66 20 69 6e 64 65 78 20 30 20 69  c.  If index 0 i
2f7a5 73 20 65 6d 70 74 79 2c 20 74 68 65 20 64 61 74  s empty, the dat
2f7a6 61 20 69 73 0a 20 20 2a 2a 20 73 74 6f 72 65 64  a is.  ** stored
2f7a7 20 74 68 65 72 65 2e 20 20 49 66 20 74 68 65 72   there.  If ther
2f7a8 65 20 69 73 20 64 61 74 61 20 74 68 65 72 65 2c  e is data there,
2f7a9 20 69 74 20 69 73 20 6d 65 72 67 65 64 20 61 6e   it is merged an
2f7aa 64 20 74 68 65 0a 20 20 2a 2a 20 72 65 73 75 6c  d the.  ** resul
2f7ab 74 73 20 63 61 72 72 69 65 64 20 69 6e 74 6f 20  ts carried into 
2f7ac 70 6f 73 69 74 69 6f 6e 20 31 2c 20 77 69 74 68  position 1, with
2f7ad 20 66 75 72 74 68 65 72 20 6d 65 72 67 65 2d 61   further merge-a
2f7ae 6e 64 2d 63 61 72 72 79 0a 20 20 2a 2a 20 75 6e  nd-carry.  ** un
2f7af 74 69 6c 20 61 6e 20 65 6d 70 74 79 20 70 6f 73  til an empty pos
2f7b0 69 74 69 6f 6e 20 69 73 20 66 6f 75 6e 64 2e 0a  ition is found..
2f7b1 20 20 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65    */.  DataBuffe
2f7b2 72 20 2a 70 42 75 66 66 65 72 73 20 3d 20 4e 55  r *pBuffers = NU
2f7b3 4c 4c 3b 0a 20 20 69 6e 74 20 6e 42 75 66 66 65  LL;.  int nBuffe
2f7b4 72 73 20 3d 20 30 2c 20 6e 4d 61 78 42 75 66 66  rs = 0, nMaxBuff
2f7b5 65 72 73 20 3d 20 30 2c 20 72 63 3b 0a 0a 20 20  ers = 0, rc;..  
2f7b6 61 73 73 65 72 74 28 20 6e 54 65 72 6d 3e 30 20  assert( nTerm>0 
2f7b7 29 3b 0a 0a 20 20 66 6f 72 28 72 63 3d 53 51 4c  );..  for(rc=SQL
2f7b8 49 54 45 5f 4f 4b 3b 20 72 63 3d 3d 53 51 4c 49  ITE_OK; rc==SQLI
2f7b9 54 45 5f 4f 4b 20 26 26 20 21 6c 65 61 76 65 73  TE_OK && !leaves
2f7ba 52 65 61 64 65 72 41 74 45 6e 64 28 70 52 65 61  ReaderAtEnd(pRea
2f7bb 64 65 72 29 3b 0a 20 20 20 20 20 20 72 63 3d 6c  der);.      rc=l
2f7bc 65 61 76 65 73 52 65 61 64 65 72 53 74 65 70 28  eavesReaderStep(
2f7bd 76 2c 20 70 52 65 61 64 65 72 29 29 7b 0a 20 20  v, pReader)){.  
2f7be 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29    /* TODO(shess)
2f7bf 20 52 65 61 6c 6c 79 20 77 61 6e 74 20 6c 65 61   Really want lea
2f7c0 76 65 73 52 65 61 64 65 72 54 65 72 6d 43 6d 70  vesReaderTermCmp
2f7c1 28 29 2c 20 62 75 74 20 74 68 61 74 20 6e 61 6d  (), but that nam
2f7c2 65 20 69 73 0a 20 20 20 20 2a 2a 20 61 6c 72 65  e is.    ** alre
2f7c3 61 64 79 20 74 61 6b 65 6e 20 74 6f 20 63 6f 6d  ady taken to com
2f7c4 70 61 72 65 20 74 68 65 20 74 65 72 6d 73 20 6f  pare the terms o
2f7c5 66 20 74 77 6f 20 4c 65 61 76 65 73 52 65 61 64  f two LeavesRead
2f7c6 65 72 73 2e 20 20 54 68 69 6e 6b 0a 20 20 20 20  ers.  Think.    
2f7c7 2a 2a 20 6f 6e 20 61 20 62 65 74 74 65 72 20 6e  ** on a better n
2f7c8 61 6d 65 2e 20 20 5b 4d 65 61 6e 77 68 69 6c 65  ame.  [Meanwhile
2f7c9 2c 20 62 72 65 61 6b 20 65 6e 63 61 70 73 75 6c  , break encapsul
2f7ca 61 74 69 6f 6e 20 72 61 74 68 65 72 20 74 68 61  ation rather tha
2f7cb 6e 0a 20 20 20 20 2a 2a 20 75 73 65 20 61 20 63  n.    ** use a c
2f7cc 6f 6e 66 75 73 69 6e 67 20 6e 61 6d 65 2e 5d 0a  onfusing name.].
2f7cd 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63      */.    int c
2f7ce 20 3d 20 6c 65 61 66 52 65 61 64 65 72 54 65 72   = leafReaderTer
2f7cf 6d 43 6d 70 28 26 70 52 65 61 64 65 72 2d 3e 6c  mCmp(&pReader->l
2f7d0 65 61 66 52 65 61 64 65 72 2c 20 70 54 65 72 6d  eafReader, pTerm
2f7d1 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69  , nTerm, isPrefi
2f7d2 78 29 3b 0a 20 20 20 20 69 66 28 20 63 3e 30 20  x);.    if( c>0 
2f7d3 29 20 62 72 65 61 6b 3b 20 20 20 20 20 20 2f 2a  ) break;      /*
2f7d4 20 50 61 73 74 20 61 6e 79 20 70 6f 73 73 69 62   Past any possib
2f7d5 6c 65 20 6d 61 74 63 68 65 73 2e 20 2a 2f 0a 20  le matches. */. 
2f7d6 20 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20     if( c==0 ){. 
2f7d7 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
2f7d8 2a 70 44 61 74 61 20 3d 20 6c 65 61 76 65 73 52  *pData = leavesR
2f7d9 65 61 64 65 72 44 61 74 61 28 70 52 65 61 64 65  eaderData(pReade
2f7da 72 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 42  r);.      int iB
2f7db 75 66 66 65 72 2c 20 6e 44 61 74 61 20 3d 20 6c  uffer, nData = l
2f7dc 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61 42  eavesReaderDataB
2f7dd 79 74 65 73 28 70 52 65 61 64 65 72 29 3b 0a 0a  ytes(pReader);..
2f7de 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 20 74 68        /* Find th
2f7df 65 20 66 69 72 73 74 20 65 6d 70 74 79 20 62 75  e first empty bu
2f7e0 66 66 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 66  ffer. */.      f
2f7e1 6f 72 28 69 42 75 66 66 65 72 3d 30 3b 20 69 42  or(iBuffer=0; iB
2f7e2 75 66 66 65 72 3c 6e 42 75 66 66 65 72 73 3b 20  uffer<nBuffers; 
2f7e3 2b 2b 69 42 75 66 66 65 72 29 7b 0a 20 20 20 20  ++iBuffer){.    
2f7e4 20 20 20 20 69 66 28 20 30 3d 3d 70 42 75 66 66      if( 0==pBuff
2f7e5 65 72 73 5b 69 42 75 66 66 65 72 5d 2e 6e 44 61  ers[iBuffer].nDa
2f7e6 74 61 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  ta ) break;.    
2f7e7 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 75    }..      /* Ou
2f7e8 74 20 6f 66 20 62 75 66 66 65 72 73 2c 20 61 64  t of buffers, ad
2f7e9 64 20 61 6e 20 65 6d 70 74 79 20 6f 6e 65 2e 20  d an empty one. 
2f7ea 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 42 75  */.      if( iBu
2f7eb 66 66 65 72 3d 3d 6e 42 75 66 66 65 72 73 20 29  ffer==nBuffers )
2f7ec 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 42  {.        if( nB
2f7ed 75 66 66 65 72 73 3d 3d 6e 4d 61 78 42 75 66 66  uffers==nMaxBuff
2f7ee 65 72 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ers ){.         
2f7ef 20 44 61 74 61 42 75 66 66 65 72 20 2a 70 3b 0a   DataBuffer *p;.
2f7f0 20 20 20 20 20 20 20 20 20 20 6e 4d 61 78 42 75            nMaxBu
2f7f1 66 66 65 72 73 20 2b 3d 20 32 30 3b 0a 0a 20 20  ffers += 20;..  
2f7f2 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6e 75 61          /* Manua
2f7f3 6c 20 72 65 61 6c 6c 6f 63 20 73 6f 20 77 65 20  l realloc so we 
2f7f4 63 61 6e 20 68 61 6e 64 6c 65 20 4e 55 4c 4c 20  can handle NULL 
2f7f5 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 20 2a  appropriately. *
2f7f6 2f 0a 20 20 20 20 20 20 20 20 20 20 70 20 3d 20  /.          p = 
2f7f7 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
2f7f8 4d 61 78 42 75 66 66 65 72 73 2a 73 69 7a 65 6f  MaxBuffers*sizeo
2f7f9 66 28 2a 70 42 75 66 66 65 72 73 29 29 3b 0a 20  f(*pBuffers));. 
2f7fa 20 20 20 20 20 20 20 20 20 69 66 28 20 70 3d 3d           if( p==
2f7fb 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20  NULL ){.        
2f7fc 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
2f7fd 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20  NOMEM;.         
2f7fe 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2f7ff 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20      }..         
2f800 20 69 66 28 20 6e 42 75 66 66 65 72 73 3e 30 20   if( nBuffers>0 
2f801 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  ){.            a
2f802 73 73 65 72 74 28 70 42 75 66 66 65 72 73 21 3d  ssert(pBuffers!=
2f803 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 20 20 20  NULL);.         
2f804 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 42 75     memcpy(p, pBu
2f805 66 66 65 72 73 2c 20 6e 42 75 66 66 65 72 73 2a  ffers, nBuffers*
2f806 73 69 7a 65 6f 66 28 2a 70 42 75 66 66 65 72 73  sizeof(*pBuffers
2f807 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ));.            
2f808 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 75  sqlite3_free(pBu
2f809 66 66 65 72 73 29 3b 0a 20 20 20 20 20 20 20 20  ffers);.        
2f80a 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 42    }.          pB
2f80b 75 66 66 65 72 73 20 3d 20 70 3b 0a 20 20 20 20  uffers = p;.    
2f80c 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 61      }.        da
2f80d 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 28 70  taBufferInit(&(p
2f80e 42 75 66 66 65 72 73 5b 6e 42 75 66 66 65 72 73  Buffers[nBuffers
2f80f 5d 29 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  ]), 0);.        
2f810 6e 42 75 66 66 65 72 73 2b 2b 3b 0a 20 20 20 20  nBuffers++;.    
2f811 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 41 74    }..      /* At
2f812 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 6d 75 73   this point, mus
2f813 74 20 68 61 76 65 20 61 6e 20 65 6d 70 74 79 20  t have an empty 
2f814 61 74 20 69 42 75 66 66 65 72 2e 20 2a 2f 0a 20  at iBuffer. */. 
2f815 20 20 20 20 20 61 73 73 65 72 74 28 69 42 75 66       assert(iBuf
2f816 66 65 72 3c 6e 42 75 66 66 65 72 73 20 26 26 20  fer<nBuffers && 
2f817 70 42 75 66 66 65 72 73 5b 69 42 75 66 66 65 72  pBuffers[iBuffer
2f818 5d 2e 6e 44 61 74 61 3d 3d 30 29 3b 0a 0a 20 20  ].nData==0);..  
2f819 20 20 20 20 2f 2a 20 49 66 20 65 6d 70 74 79 20      /* If empty 
2f81a 77 61 73 20 66 69 72 73 74 20 62 75 66 66 65 72  was first buffer
2f81b 2c 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 6d 65  , no need for me
2f81c 72 67 65 20 6c 6f 67 69 63 2e 20 2a 2f 0a 20 20  rge logic. */.  
2f81d 20 20 20 20 69 66 28 20 69 42 75 66 66 65 72 3d      if( iBuffer=
2f81e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 64 61  =0 ){.        da
2f81f 74 61 42 75 66 66 65 72 52 65 70 6c 61 63 65 28  taBufferReplace(
2f820 26 28 70 42 75 66 66 65 72 73 5b 30 5d 29 2c 20  &(pBuffers[0]), 
2f821 70 44 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a 20  pData, nData);. 
2f822 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2f823 20 20 20 20 2f 2a 20 70 41 63 63 20 69 73 20 74      /* pAcc is t
2f824 68 65 20 65 6d 70 74 79 20 62 75 66 66 65 72 20  he empty buffer 
2f825 74 68 65 20 6d 65 72 67 65 64 20 64 61 74 61 20  the merged data 
2f826 77 69 6c 6c 20 65 6e 64 20 75 70 20 69 6e 2e 20  will end up in. 
2f827 2a 2f 0a 20 20 20 20 20 20 20 20 44 61 74 61 42  */.        DataB
2f828 75 66 66 65 72 20 2a 70 41 63 63 20 3d 20 26 28  uffer *pAcc = &(
2f829 70 42 75 66 66 65 72 73 5b 69 42 75 66 66 65 72  pBuffers[iBuffer
2f82a 5d 29 3b 0a 20 20 20 20 20 20 20 20 44 61 74 61  ]);.        Data
2f82b 42 75 66 66 65 72 20 2a 70 20 3d 20 26 28 70 42  Buffer *p = &(pB
2f82c 75 66 66 65 72 73 5b 30 5d 29 3b 0a 0a 20 20 20  uffers[0]);..   
2f82d 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 70       /* Handle p
2f82e 6f 73 69 74 69 6f 6e 20 30 20 73 70 65 63 69 61  osition 0 specia
2f82f 6c 6c 79 20 74 6f 20 61 76 6f 69 64 20 6e 65 65  lly to avoid nee
2f830 64 20 74 6f 20 70 72 69 6d 65 20 70 41 63 63 0a  d to prime pAcc.
2f831 20 20 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20          ** with 
2f832 70 44 61 74 61 2f 6e 44 61 74 61 2e 0a 20 20 20  pData/nData..   
2f833 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
2f834 64 61 74 61 42 75 66 66 65 72 53 77 61 70 28 70  dataBufferSwap(p
2f835 2c 20 70 41 63 63 29 3b 0a 20 20 20 20 20 20 20  , pAcc);.       
2f836 20 64 6f 63 4c 69 73 74 41 63 63 75 6d 75 6c 61   docListAccumula
2f837 74 65 55 6e 69 6f 6e 28 70 41 63 63 2c 20 70 44  teUnion(pAcc, pD
2f838 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a 0a 20 20  ata, nData);..  
2f839 20 20 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c        /* Accumul
2f83a 61 74 65 20 72 65 6d 61 69 6e 69 6e 67 20 64 6f  ate remaining do
2f83b 63 6c 69 73 74 73 20 69 6e 74 6f 20 70 41 63 63  clists into pAcc
2f83c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72  . */.        for
2f83d 28 2b 2b 70 3b 20 70 3c 70 41 63 63 3b 20 2b 2b  (++p; p<pAcc; ++
2f83e 70 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 6f  p){.          do
2f83f 63 4c 69 73 74 41 63 63 75 6d 75 6c 61 74 65 55  cListAccumulateU
2f840 6e 69 6f 6e 28 70 41 63 63 2c 20 70 2d 3e 70 44  nion(pAcc, p->pD
2f841 61 74 61 2c 20 70 2d 3e 6e 44 61 74 61 29 3b 0a  ata, p->nData);.
2f842 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 61  .          /* da
2f843 74 61 42 75 66 66 65 72 52 65 73 65 74 28 29 20  taBufferReset() 
2f844 63 6f 75 6c 64 20 61 6c 6c 6f 77 20 61 20 6c 61  could allow a la
2f845 72 67 65 20 64 6f 63 6c 69 73 74 20 74 6f 20 62  rge doclist to b
2f846 6c 6f 77 20 75 70 0a 20 20 20 20 20 20 20 20 20  low up.         
2f847 20 2a 2a 20 6f 75 72 20 6d 65 6d 6f 72 79 20 72   ** our memory r
2f848 65 71 75 69 72 65 6d 65 6e 74 73 2e 0a 20 20 20  equirements..   
2f849 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
2f84a 20 20 20 20 69 66 28 20 70 2d 3e 6e 43 61 70 61      if( p->nCapa
2f84b 63 69 74 79 3c 31 30 32 34 20 29 7b 0a 20 20 20  city<1024 ){.   
2f84c 20 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66           dataBuf
2f84d 66 65 72 52 65 73 65 74 28 70 29 3b 0a 20 20 20  ferReset(p);.   
2f84e 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
2f84f 20 20 20 20 20 20 20 20 20 20 64 61 74 61 42 75            dataBu
2f850 66 66 65 72 44 65 73 74 72 6f 79 28 70 29 3b 0a  fferDestroy(p);.
2f851 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61              data
2f852 42 75 66 66 65 72 49 6e 69 74 28 70 2c 20 30 29  BufferInit(p, 0)
2f853 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
2f854 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2f855 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
2f856 55 6e 69 6f 6e 20 61 6c 6c 20 74 68 65 20 64 6f  Union all the do
2f857 63 6c 69 73 74 73 20 74 6f 67 65 74 68 65 72 20  clists together 
2f858 69 6e 74 6f 20 2a 6f 75 74 2e 20 2a 2f 0a 20 20  into *out. */.  
2f859 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 57  /* TODO(shess) W
2f85a 68 61 74 20 69 66 20 2a 6f 75 74 20 69 73 20 62  hat if *out is b
2f85b 69 67 3f 20 20 53 69 67 68 2e 20 2a 2f 0a 20 20  ig?  Sigh. */.  
2f85c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
2f85d 4b 20 26 26 20 6e 42 75 66 66 65 72 73 3e 30 20  K && nBuffers>0 
2f85e 29 7b 0a 20 20 20 20 69 6e 74 20 69 42 75 66 66  ){.    int iBuff
2f85f 65 72 3b 0a 20 20 20 20 66 6f 72 28 69 42 75 66  er;.    for(iBuf
2f860 66 65 72 3d 30 3b 20 69 42 75 66 66 65 72 3c 6e  fer=0; iBuffer<n
2f861 42 75 66 66 65 72 73 3b 20 2b 2b 69 42 75 66 66  Buffers; ++iBuff
2f862 65 72 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  er){.      if( p
2f863 42 75 66 66 65 72 73 5b 69 42 75 66 66 65 72 5d  Buffers[iBuffer]
2f864 2e 6e 44 61 74 61 3e 30 20 29 7b 0a 20 20 20 20  .nData>0 ){.    
2f865 20 20 20 20 69 66 28 20 6f 75 74 2d 3e 6e 44 61      if( out->nDa
2f866 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ta==0 ){.       
2f867 20 20 20 64 61 74 61 42 75 66 66 65 72 53 77 61     dataBufferSwa
2f868 70 28 6f 75 74 2c 20 26 28 70 42 75 66 66 65 72  p(out, &(pBuffer
2f869 73 5b 69 42 75 66 66 65 72 5d 29 29 3b 0a 20 20  s[iBuffer]));.  
2f86a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2f86b 20 20 20 20 20 20 20 64 6f 63 4c 69 73 74 41 63         docListAc
2f86c 63 75 6d 75 6c 61 74 65 55 6e 69 6f 6e 28 6f 75  cumulateUnion(ou
2f86d 74 2c 20 70 42 75 66 66 65 72 73 5b 69 42 75 66  t, pBuffers[iBuf
2f86e 66 65 72 5d 2e 70 44 61 74 61 2c 0a 20 20 20 20  fer].pData,.    
2f86f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f870 20 20 20 20 20 20 20 20 20 20 20 20 20 70 42 75               pBu
2f871 66 66 65 72 73 5b 69 42 75 66 66 65 72 5d 2e 6e  ffers[iBuffer].n
2f872 44 61 74 61 29 3b 0a 20 20 20 20 20 20 20 20 7d  Data);.        }
2f873 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2f874 20 7d 0a 0a 20 20 77 68 69 6c 65 28 20 6e 42 75   }..  while( nBu
2f875 66 66 65 72 73 2d 2d 20 29 7b 0a 20 20 20 20 64  ffers-- ){.    d
2f876 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
2f877 28 26 28 70 42 75 66 66 65 72 73 5b 6e 42 75 66  (&(pBuffers[nBuf
2f878 66 65 72 73 5d 29 29 3b 0a 20 20 7d 0a 20 20 69  fers]));.  }.  i
2f879 66 28 20 70 42 75 66 66 65 72 73 21 3d 4e 55 4c  f( pBuffers!=NUL
2f87a 4c 20 29 20 73 71 6c 69 74 65 33 5f 66 72 65 65  L ) sqlite3_free
2f87b 28 70 42 75 66 66 65 72 73 29 3b 0a 0a 20 20 72  (pBuffers);..  r
2f87c 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
2f87d 43 61 6c 6c 20 6c 6f 61 64 53 65 67 6d 65 6e 74  Call loadSegment
2f87e 4c 65 61 76 65 73 49 6e 74 28 29 20 77 69 74 68  LeavesInt() with
2f87f 20 70 44 61 74 61 2f 6e 44 61 74 61 20 61 73 20   pData/nData as 
2f880 69 6e 70 75 74 2e 20 2a 2f 0a 73 74 61 74 69 63  input. */.static
2f881 20 69 6e 74 20 6c 6f 61 64 53 65 67 6d 65 6e 74   int loadSegment
2f882 4c 65 61 66 28 66 75 6c 6c 74 65 78 74 5f 76 74  Leaf(fulltext_vt
2f883 61 62 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61  ab *v, const cha
2f884 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44  r *pData, int nD
2f885 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
2f886 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f887 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72  const char *pTer
2f888 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e  m, int nTerm, in
2f889 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20 20 20  t isPrefix,.    
2f88a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f88b 20 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65         DataBuffe
2f88c 72 20 2a 6f 75 74 29 7b 0a 20 20 4c 65 61 76 65  r *out){.  Leave
2f88d 73 52 65 61 64 65 72 20 72 65 61 64 65 72 3b 0a  sReader reader;.
2f88e 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73    int rc;..  ass
2f88f 65 72 74 28 20 6e 44 61 74 61 3e 31 20 29 3b 0a  ert( nData>1 );.
2f890 20 20 61 73 73 65 72 74 28 20 2a 70 44 61 74 61    assert( *pData
2f891 3d 3d 27 5c 30 27 20 29 3b 0a 20 20 72 63 20 3d  =='\0' );.  rc =
2f892 20 6c 65 61 76 65 73 52 65 61 64 65 72 49 6e 69   leavesReaderIni
2f893 74 28 76 2c 20 30 2c 20 30 2c 20 30 2c 20 70 44  t(v, 0, 0, 0, pD
2f894 61 74 61 2c 20 6e 44 61 74 61 2c 20 26 72 65 61  ata, nData, &rea
2f895 64 65 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  der);.  if( rc!=
2f896 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
2f897 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 6c  rn rc;..  rc = l
2f898 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73  oadSegmentLeaves
2f899 49 6e 74 28 76 2c 20 26 72 65 61 64 65 72 2c 20  Int(v, &reader, 
2f89a 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73  pTerm, nTerm, is
2f89b 50 72 65 66 69 78 2c 20 6f 75 74 29 3b 0a 20 20  Prefix, out);.  
2f89c 6c 65 61 76 65 73 52 65 61 64 65 72 52 65 73 65  leavesReaderRese
2f89d 74 28 26 72 65 61 64 65 72 29 3b 0a 20 20 6c 65  t(&reader);.  le
2f89e 61 76 65 73 52 65 61 64 65 72 44 65 73 74 72 6f  avesReaderDestro
2f89f 79 28 26 72 65 61 64 65 72 29 3b 0a 20 20 72 65  y(&reader);.  re
2f8a0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 43  turn rc;.}../* C
2f8a1 61 6c 6c 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c  all loadSegmentL
2f8a2 65 61 76 65 73 49 6e 74 28 29 20 77 69 74 68 20  eavesInt() with 
2f8a3 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 73 20 66  the leaf nodes f
2f8a4 72 6f 6d 20 69 53 74 61 72 74 4c 65 61 66 20 74  rom iStartLeaf t
2f8a5 6f 0a 2a 2a 20 69 45 6e 64 4c 65 61 66 20 28 69  o.** iEndLeaf (i
2f8a6 6e 63 6c 75 73 69 76 65 29 20 61 73 20 69 6e 70  nclusive) as inp
2f8a7 75 74 2c 20 61 6e 64 20 6d 65 72 67 65 20 74 68  ut, and merge th
2f8a8 65 20 72 65 73 75 6c 74 69 6e 67 20 64 6f 63 6c  e resulting docl
2f8a9 69 73 74 20 69 6e 74 6f 0a 2a 2a 20 6f 75 74 2e  ist into.** out.
2f8aa 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
2f8ab 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73  oadSegmentLeaves
2f8ac 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
2f8ad 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  v,.             
2f8ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f8af 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 53 74  sqlite_int64 iSt
2f8b0 61 72 74 4c 65 61 66 2c 20 73 71 6c 69 74 65 5f  artLeaf, sqlite_
2f8b1 69 6e 74 36 34 20 69 45 6e 64 4c 65 61 66 2c 0a  int64 iEndLeaf,.
2f8b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f8b3 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
2f8b4 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20  st char *pTerm, 
2f8b5 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69  int nTerm, int i
2f8b6 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20  sPrefix,.       
2f8b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f8b8 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72        DataBuffer
2f8b9 20 2a 6f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63   *out){.  int rc
2f8ba 3b 0a 20 20 4c 65 61 76 65 73 52 65 61 64 65 72  ;.  LeavesReader
2f8bb 20 72 65 61 64 65 72 3b 0a 0a 20 20 61 73 73 65   reader;..  asse
2f8bc 72 74 28 20 69 53 74 61 72 74 4c 65 61 66 3c 3d  rt( iStartLeaf<=
2f8bd 69 45 6e 64 4c 65 61 66 20 29 3b 0a 20 20 72 63  iEndLeaf );.  rc
2f8be 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 49   = leavesReaderI
2f8bf 6e 69 74 28 76 2c 20 30 2c 20 69 53 74 61 72 74  nit(v, 0, iStart
2f8c0 4c 65 61 66 2c 20 69 45 6e 64 4c 65 61 66 2c 20  Leaf, iEndLeaf, 
2f8c1 4e 55 4c 4c 2c 20 30 2c 20 26 72 65 61 64 65 72  NULL, 0, &reader
2f8c2 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
2f8c3 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
2f8c4 72 63 3b 0a 0a 20 20 72 63 20 3d 20 6c 6f 61 64  rc;..  rc = load
2f8c5 53 65 67 6d 65 6e 74 4c 65 61 76 65 73 49 6e 74  SegmentLeavesInt
2f8c6 28 76 2c 20 26 72 65 61 64 65 72 2c 20 70 54 65  (v, &reader, pTe
2f8c7 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65  rm, nTerm, isPre
2f8c8 66 69 78 2c 20 6f 75 74 29 3b 0a 20 20 6c 65 61  fix, out);.  lea
2f8c9 76 65 73 52 65 61 64 65 72 52 65 73 65 74 28 26  vesReaderReset(&
2f8ca 72 65 61 64 65 72 29 3b 0a 20 20 6c 65 61 76 65  reader);.  leave
2f8cb 73 52 65 61 64 65 72 44 65 73 74 72 6f 79 28 26  sReaderDestroy(&
2f8cc 72 65 61 64 65 72 29 3b 0a 20 20 72 65 74 75 72  reader);.  retur
2f8cd 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 61 6b 69  n rc;.}../* Taki
2f8ce 6e 67 20 70 44 61 74 61 2f 6e 44 61 74 61 20 61  ng pData/nData a
2f8cf 73 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f  s an interior no
2f8d0 64 65 2c 20 66 69 6e 64 20 74 68 65 20 73 65 71  de, find the seq
2f8d1 75 65 6e 63 65 20 6f 66 20 63 68 69 6c 64 0a 2a  uence of child.*
2f8d2 2a 20 6e 6f 64 65 73 20 77 68 69 63 68 20 63 6f  * nodes which co
2f8d3 75 6c 64 20 69 6e 63 6c 75 64 65 20 70 54 65 72  uld include pTer
2f8d4 6d 2f 6e 54 65 72 6d 2f 69 73 50 72 65 66 69 78  m/nTerm/isPrefix
2f8d5 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  .  Note that the
2f8d6 0a 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  .** interior nod
2f8d7 65 20 74 65 72 6d 73 20 6c 6f 67 69 63 61 6c 6c  e terms logicall
2f8d8 79 20 63 6f 6d 65 20 62 65 74 77 65 65 6e 20 74  y come between t
2f8d9 68 65 20 62 6c 6f 63 6b 73 2c 20 73 6f 20 74 68  he blocks, so th
2f8da 65 72 65 20 69 73 0a 2a 2a 20 6f 6e 65 20 6d 6f  ere is.** one mo
2f8db 72 65 20 62 6c 6f 63 6b 69 64 20 74 68 61 6e 20  re blockid than 
2f8dc 74 68 65 72 65 20 61 72 65 20 74 65 72 6d 73 20  there are terms 
2f8dd 28 74 68 61 74 20 62 6c 6f 63 6b 20 63 6f 6e 74  (that block cont
2f8de 61 69 6e 73 20 74 65 72 6d 73 20 3e 3d 0a 2a 2a  ains terms >=.**
2f8df 20 74 68 65 20 6c 61 73 74 20 69 6e 74 65 72 69   the last interi
2f8e0 6f 72 2d 6e 6f 64 65 20 74 65 72 6d 29 2e 0a 2a  or-node term)..*
2f8e1 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  /./* TODO(shess)
2f8e2 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 63 6f 64   The calling cod
2f8e3 65 20 6d 61 79 20 61 6c 72 65 61 64 79 20 6b 6e  e may already kn
2f8e4 6f 77 20 74 68 61 74 20 74 68 65 20 65 6e 64 20  ow that the end 
2f8e5 63 68 69 6c 64 20 69 73 0a 2a 2a 20 6e 6f 74 20  child is.** not 
2f8e6 77 6f 72 74 68 20 63 61 6c 63 75 6c 61 74 69 6e  worth calculatin
2f8e7 67 2c 20 62 65 63 61 75 73 65 20 74 68 65 20 65  g, because the e
2f8e8 6e 64 20 6d 61 79 20 62 65 20 69 6e 20 61 20 6c  nd may be in a l
2f8e9 61 74 65 72 20 73 69 62 6c 69 6e 67 0a 2a 2a 20  ater sibling.** 
2f8ea 6e 6f 64 65 2e 20 20 43 6f 6e 73 69 64 65 72 20  node.  Consider 
2f8eb 77 68 65 74 68 65 72 20 62 72 65 61 6b 69 6e 67  whether breaking
2f8ec 20 73 79 6d 6d 65 74 72 79 20 69 73 20 77 6f 72   symmetry is wor
2f8ed 74 68 77 68 69 6c 65 2e 20 20 49 20 73 75 73 70  thwhile.  I susp
2f8ee 65 63 74 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74  ect.** it is not
2f8ef 20 77 6f 72 74 68 77 68 69 6c 65 2e 0a 2a 2f 0a   worthwhile..*/.
2f8f0 73 74 61 74 69 63 20 76 6f 69 64 20 67 65 74 43  static void getC
2f8f1 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e  hildrenContainin
2f8f2 67 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44  g(const char *pD
2f8f3 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a  ata, int nData,.
2f8f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f8f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f8f6 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54    const char *pT
2f8f7 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20  erm, int nTerm, 
2f8f8 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20  int isPrefix,.  
2f8f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f8fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f8fb 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69  sqlite_int64 *pi
2f8fc 53 74 61 72 74 43 68 69 6c 64 2c 0a 20 20 20 20  StartChild,.    
2f8fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f8fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
2f8ff 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69 45 6e  lite_int64 *piEn
2f900 64 43 68 69 6c 64 29 7b 0a 20 20 49 6e 74 65 72  dChild){.  Inter
2f901 69 6f 72 52 65 61 64 65 72 20 72 65 61 64 65 72  iorReader reader
2f902 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61  ;..  assert( nDa
2f903 74 61 3e 31 20 29 3b 0a 20 20 61 73 73 65 72 74  ta>1 );.  assert
2f904 28 20 2a 70 44 61 74 61 21 3d 27 5c 30 27 20 29  ( *pData!='\0' )
2f905 3b 0a 20 20 69 6e 74 65 72 69 6f 72 52 65 61 64  ;.  interiorRead
2f906 65 72 49 6e 69 74 28 70 44 61 74 61 2c 20 6e 44  erInit(pData, nD
2f907 61 74 61 2c 20 26 72 65 61 64 65 72 29 3b 0a 0a  ata, &reader);..
2f908 20 20 2f 2a 20 53 63 61 6e 20 66 6f 72 20 74 68    /* Scan for th
2f909 65 20 66 69 72 73 74 20 63 68 69 6c 64 20 77 68  e first child wh
2f90a 69 63 68 20 63 6f 75 6c 64 20 63 6f 6e 74 61 69  ich could contai
2f90b 6e 20 70 54 65 72 6d 2f 6e 54 65 72 6d 2e 20 2a  n pTerm/nTerm. *
2f90c 2f 0a 20 20 77 68 69 6c 65 28 20 21 69 6e 74 65  /.  while( !inte
2f90d 72 69 6f 72 52 65 61 64 65 72 41 74 45 6e 64 28  riorReaderAtEnd(
2f90e 26 72 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20  &reader) ){.    
2f90f 69 66 28 20 69 6e 74 65 72 69 6f 72 52 65 61 64  if( interiorRead
2f910 65 72 54 65 72 6d 43 6d 70 28 26 72 65 61 64 65  erTermCmp(&reade
2f911 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c  r, pTerm, nTerm,
2f912 20 30 29 3e 30 20 29 20 62 72 65 61 6b 3b 0a 20   0)>0 ) break;. 
2f913 20 20 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65     interiorReade
2f914 72 53 74 65 70 28 26 72 65 61 64 65 72 29 3b 0a  rStep(&reader);.
2f915 20 20 7d 0a 20 20 2a 70 69 53 74 61 72 74 43 68    }.  *piStartCh
2f916 69 6c 64 20 3d 20 69 6e 74 65 72 69 6f 72 52 65  ild = interiorRe
2f917 61 64 65 72 43 75 72 72 65 6e 74 42 6c 6f 63 6b  aderCurrentBlock
2f918 69 64 28 26 72 65 61 64 65 72 29 3b 0a 0a 20 20  id(&reader);..  
2f919 2f 2a 20 4b 65 65 70 20 73 63 61 6e 6e 69 6e 67  /* Keep scanning
2f91a 20 74 6f 20 66 69 6e 64 20 61 20 74 65 72 6d 20   to find a term 
2f91b 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 75 72  greater than our
2f91c 20 74 65 72 6d 2c 20 75 73 69 6e 67 20 70 72 65   term, using pre
2f91d 66 69 78 0a 20 20 2a 2a 20 63 6f 6d 70 61 72 69  fix.  ** compari
2f91e 73 6f 6e 20 69 66 20 69 6e 64 69 63 61 74 65 64  son if indicated
2f91f 2e 20 20 49 66 20 69 73 50 72 65 66 69 78 20 69  .  If isPrefix i
2f920 73 20 66 61 6c 73 65 2c 20 74 68 69 73 20 77 69  s false, this wi
2f921 6c 6c 20 62 65 20 74 68 65 0a 20 20 2a 2a 20 73  ll be the.  ** s
2f922 61 6d 65 20 62 6c 6f 63 6b 69 64 20 61 73 20 74  ame blockid as t
2f923 68 65 20 73 74 61 72 74 69 6e 67 20 62 6c 6f 63  he starting bloc
2f924 6b 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28  k..  */.  while(
2f925 20 21 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72   !interiorReader
2f926 41 74 45 6e 64 28 26 72 65 61 64 65 72 29 20 29  AtEnd(&reader) )
2f927 7b 0a 20 20 20 20 69 66 28 20 69 6e 74 65 72 69  {.    if( interi
2f928 6f 72 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28  orReaderTermCmp(
2f929 26 72 65 61 64 65 72 2c 20 70 54 65 72 6d 2c 20  &reader, pTerm, 
2f92a 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78 29  nTerm, isPrefix)
2f92b 3e 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  >0 ) break;.    
2f92c 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 53 74  interiorReaderSt
2f92d 65 70 28 26 72 65 61 64 65 72 29 3b 0a 20 20 7d  ep(&reader);.  }
2f92e 0a 20 20 2a 70 69 45 6e 64 43 68 69 6c 64 20 3d  .  *piEndChild =
2f92f 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 43   interiorReaderC
2f930 75 72 72 65 6e 74 42 6c 6f 63 6b 69 64 28 26 72  urrentBlockid(&r
2f931 65 61 64 65 72 29 3b 0a 0a 20 20 69 6e 74 65 72  eader);..  inter
2f932 69 6f 72 52 65 61 64 65 72 44 65 73 74 72 6f 79  iorReaderDestroy
2f933 28 26 72 65 61 64 65 72 29 3b 0a 0a 20 20 2f 2a  (&reader);..  /*
2f934 20 43 68 69 6c 64 72 65 6e 20 6d 75 73 74 20 61   Children must a
2f935 73 63 65 6e 64 2c 20 61 6e 64 20 69 66 20 21 70  scend, and if !p
2f936 72 65 66 69 78 2c 20 62 6f 74 68 20 6d 75 73 74  refix, both must
2f937 20 62 65 20 74 68 65 20 73 61 6d 65 2e 20 2a 2f   be the same. */
2f938 0a 20 20 61 73 73 65 72 74 28 20 2a 70 69 45 6e  .  assert( *piEn
2f939 64 43 68 69 6c 64 3e 3d 2a 70 69 53 74 61 72 74  dChild>=*piStart
2f93a 43 68 69 6c 64 20 29 3b 0a 20 20 61 73 73 65 72  Child );.  asser
2f93b 74 28 20 69 73 50 72 65 66 69 78 20 7c 7c 20 2a  t( isPrefix || *
2f93c 70 69 53 74 61 72 74 43 68 69 6c 64 3d 3d 2a 70  piStartChild==*p
2f93d 69 45 6e 64 43 68 69 6c 64 20 29 3b 0a 7d 0a 0a  iEndChild );.}..
2f93e 2f 2a 20 52 65 61 64 20 62 6c 6f 63 6b 20 61 74  /* Read block at
2f93f 20 69 42 6c 6f 63 6b 69 64 20 61 6e 64 20 70 61   iBlockid and pa
2f940 73 73 20 69 74 20 77 69 74 68 20 6f 74 68 65 72  ss it with other
2f941 20 70 61 72 61 6d 73 20 74 6f 0a 2a 2a 20 67 65   params to.** ge
2f942 74 43 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e  tChildrenContain
2f943 69 6e 67 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  ing()..*/.static
2f944 20 69 6e 74 20 6c 6f 61 64 41 6e 64 47 65 74 43   int loadAndGetC
2f945 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e  hildrenContainin
2f946 67 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74  g(.  fulltext_vt
2f947 61 62 20 2a 76 2c 0a 20 20 73 71 6c 69 74 65 5f  ab *v,.  sqlite_
2f948 69 6e 74 36 34 20 69 42 6c 6f 63 6b 69 64 2c 0a  int64 iBlockid,.
2f949 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54    const char *pT
2f94a 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20  erm, int nTerm, 
2f94b 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20  int isPrefix,.  
2f94c 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69  sqlite_int64 *pi
2f94d 53 74 61 72 74 43 68 69 6c 64 2c 20 73 71 6c 69  StartChild, sqli
2f94e 74 65 5f 69 6e 74 36 34 20 2a 70 69 45 6e 64 43  te_int64 *piEndC
2f94f 68 69 6c 64 0a 29 7b 0a 20 20 73 71 6c 69 74 65  hild.){.  sqlite
2f950 33 5f 73 74 6d 74 20 2a 73 20 3d 20 4e 55 4c 4c  3_stmt *s = NULL
2f951 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61  ;.  int rc;..  a
2f952 73 73 65 72 74 28 20 69 42 6c 6f 63 6b 69 64 21  ssert( iBlockid!
2f953 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
2f954 70 54 65 72 6d 21 3d 4e 55 4c 4c 20 29 3b 0a 20  pTerm!=NULL );. 
2f955 20 61 73 73 65 72 74 28 20 6e 54 65 72 6d 21 3d   assert( nTerm!=
2f956 30 20 29 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  0 );        /* T
2f957 4f 44 4f 28 73 68 65 73 73 29 20 57 68 79 20 6e  ODO(shess) Why n
2f958 6f 74 20 61 6c 6c 6f 77 20 74 68 69 73 3f 20 2a  ot allow this? *
2f959 2f 0a 20 20 61 73 73 65 72 74 28 20 70 69 53 74  /.  assert( piSt
2f95a 61 72 74 43 68 69 6c 64 21 3d 4e 55 4c 4c 20 29  artChild!=NULL )
2f95b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 69 45 6e  ;.  assert( piEn
2f95c 64 43 68 69 6c 64 21 3d 4e 55 4c 4c 20 29 3b 0a  dChild!=NULL );.
2f95d 0a 20 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f  .  rc = sql_get_
2f95e 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 42 4c 4f  statement(v, BLO
2f95f 43 4b 5f 53 45 4c 45 43 54 5f 53 54 4d 54 2c 20  CK_SELECT_STMT, 
2f960 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  &s);.  if( rc!=S
2f961 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
2f962 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
2f963 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
2f964 28 73 2c 20 31 2c 20 69 42 6c 6f 63 6b 69 64 29  (s, 1, iBlockid)
2f965 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
2f966 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
2f967 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  c;..  rc = sqlit
2f968 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66  e3_step(s);.  if
2f969 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
2f96a 45 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  E ) return SQLIT
2f96b 45 5f 45 52 52 4f 52 3b 0a 20 20 69 66 28 20 72  E_ERROR;.  if( r
2f96c 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20  c!=SQLITE_ROW ) 
2f96d 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 67 65  return rc;..  ge
2f96e 74 43 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e  tChildrenContain
2f96f 69 6e 67 28 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ing(sqlite3_colu
2f970 6d 6e 5f 62 6c 6f 62 28 73 2c 20 30 29 2c 20 73  mn_blob(s, 0), s
2f971 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
2f972 74 65 73 28 73 2c 20 30 29 2c 0a 20 20 20 20 20  tes(s, 0),.     
2f973 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f974 20 20 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c     pTerm, nTerm,
2f975 20 69 73 50 72 65 66 69 78 2c 20 70 69 53 74 61   isPrefix, piSta
2f976 72 74 43 68 69 6c 64 2c 20 70 69 45 6e 64 43 68  rtChild, piEndCh
2f977 69 6c 64 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 65  ild);..  /* We e
2f978 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72  xpect only one r
2f979 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65 78 65  ow.  We must exe
2f97a 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73 71 6c  cute another sql
2f97b 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20 20 2a  ite3_step().   *
2f97c 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65   to complete the
2f97d 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65   iteration; othe
2f97e 72 77 69 73 65 20 74 68 65 20 74 61 62 6c 65 20  rwise the table 
2f97f 77 69 6c 6c 20 72 65 6d 61 69 6e 0a 20 20 20 2a  will remain.   *
2f980 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63   locked. */.  rc
2f981 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
2f982 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  s);.  if( rc==SQ
2f983 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72  LITE_ROW ) retur
2f984 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
2f985 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2f986 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 72  _DONE ) return r
2f987 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  c;..  return SQL
2f988 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 72  ITE_OK;.}../* Tr
2f989 61 76 65 72 73 65 20 74 68 65 20 74 72 65 65 20  averse the tree 
2f98a 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 70  represented by p
2f98b 44 61 74 61 5b 6e 44 61 74 61 5d 20 6c 6f 6f 6b  Data[nData] look
2f98c 69 6e 67 20 66 6f 72 0a 2a 2a 20 70 54 65 72 6d  ing for.** pTerm
2f98d 5b 6e 54 65 72 6d 5d 2c 20 70 6c 61 63 69 6e 67  [nTerm], placing
2f98e 20 69 74 73 20 64 6f 63 6c 69 73 74 20 69 6e 74   its doclist int
2f98f 6f 20 2a 6f 75 74 2e 20 20 54 68 69 73 20 69 73  o *out.  This is
2f990 20 69 6e 74 65 72 6e 61 6c 20 74 6f 0a 2a 2a 20   internal to.** 
2f991 6c 6f 61 64 53 65 67 6d 65 6e 74 28 29 20 74 6f  loadSegment() to
2f992 20 6d 61 6b 65 20 65 72 72 6f 72 2d 68 61 6e 64   make error-hand
2f993 6c 69 6e 67 20 63 6c 65 61 6e 65 72 2e 0a 2a 2f  ling cleaner..*/
2f994 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 61 64  .static int load
2f995 53 65 67 6d 65 6e 74 49 6e 74 28 66 75 6c 6c 74  SegmentInt(fullt
2f996 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 63 6f 6e  ext_vtab *v, con
2f997 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
2f998 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20 20  int nData,.     
2f999 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f99a 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
2f99b 34 20 69 4c 65 61 76 65 73 45 6e 64 2c 0a 20 20  4 iLeavesEnd,.  
2f99c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f99d 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
2f99e 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e  ar *pTerm, int n
2f99f 54 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66  Term, int isPref
2f9a0 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ix,.            
2f9a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 44 61                Da
2f9a2 74 61 42 75 66 66 65 72 20 2a 6f 75 74 29 7b 0a  taBuffer *out){.
2f9a3 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73    /* Special cas
2f9a4 65 20 77 68 65 72 65 20 72 6f 6f 74 20 69 73 20  e where root is 
2f9a5 61 20 6c 65 61 66 2e 20 2a 2f 0a 20 20 69 66 28  a leaf. */.  if(
2f9a6 20 2a 70 44 61 74 61 3d 3d 27 5c 30 27 20 29 7b   *pData=='\0' ){
2f9a7 0a 20 20 20 20 72 65 74 75 72 6e 20 6c 6f 61 64  .    return load
2f9a8 53 65 67 6d 65 6e 74 4c 65 61 66 28 76 2c 20 70  SegmentLeaf(v, p
2f9a9 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 70 54 65  Data, nData, pTe
2f9aa 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65  rm, nTerm, isPre
2f9ab 66 69 78 2c 20 6f 75 74 29 3b 0a 20 20 7d 65 6c  fix, out);.  }el
2f9ac 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a  se{.    int rc;.
2f9ad 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
2f9ae 20 69 53 74 61 72 74 43 68 69 6c 64 2c 20 69 45   iStartChild, iE
2f9af 6e 64 43 68 69 6c 64 3b 0a 0a 20 20 20 20 2f 2a  ndChild;..    /*
2f9b0 20 50 72 6f 63 65 73 73 20 70 44 61 74 61 20 61   Process pData a
2f9b1 73 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f  s an interior no
2f9b2 64 65 2c 20 74 68 65 6e 20 6c 6f 6f 70 20 64 6f  de, then loop do
2f9b3 77 6e 20 74 68 65 20 74 72 65 65 0a 20 20 20 20  wn the tree.    
2f9b4 2a 2a 20 75 6e 74 69 6c 20 77 65 20 66 69 6e 64  ** until we find
2f9b5 20 74 68 65 20 73 65 74 20 6f 66 20 6c 65 61 66   the set of leaf
2f9b6 20 6e 6f 64 65 73 20 74 6f 20 73 63 61 6e 20 66   nodes to scan f
2f9b7 6f 72 20 74 68 65 20 74 65 72 6d 2e 0a 20 20 20  or the term..   
2f9b8 20 2a 2f 0a 20 20 20 20 67 65 74 43 68 69 6c 64   */.    getChild
2f9b9 72 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 70 44  renContaining(pD
2f9ba 61 74 61 2c 20 6e 44 61 74 61 2c 20 70 54 65 72  ata, nData, pTer
2f9bb 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66  m, nTerm, isPref
2f9bc 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ix,.            
2f9bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 69                &i
2f9be 53 74 61 72 74 43 68 69 6c 64 2c 20 26 69 45 6e  StartChild, &iEn
2f9bf 64 43 68 69 6c 64 29 3b 0a 20 20 20 20 77 68 69  dChild);.    whi
2f9c0 6c 65 28 20 69 53 74 61 72 74 43 68 69 6c 64 3e  le( iStartChild>
2f9c1 69 4c 65 61 76 65 73 45 6e 64 20 29 7b 0a 20 20  iLeavesEnd ){.  
2f9c2 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
2f9c3 20 69 4e 65 78 74 53 74 61 72 74 2c 20 69 4e 65   iNextStart, iNe
2f9c4 78 74 45 6e 64 3b 0a 20 20 20 20 20 20 72 63 20  xtEnd;.      rc 
2f9c5 3d 20 6c 6f 61 64 41 6e 64 47 65 74 43 68 69 6c  = loadAndGetChil
2f9c6 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 76  drenContaining(v
2f9c7 2c 20 69 53 74 61 72 74 43 68 69 6c 64 2c 20 70  , iStartChild, p
2f9c8 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50  Term, nTerm, isP
2f9c9 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20  refix,.         
2f9ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f9cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
2f9cc 69 4e 65 78 74 53 74 61 72 74 2c 20 26 69 4e 65  iNextStart, &iNe
2f9cd 78 74 45 6e 64 29 3b 0a 20 20 20 20 20 20 69 66  xtEnd);.      if
2f9ce 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2f9cf 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
2f9d0 20 20 20 20 2f 2a 20 49 66 20 77 65 27 76 65 20      /* If we've 
2f9d1 62 72 61 6e 63 68 65 64 2c 20 66 6f 6c 6c 6f 77  branched, follow
2f9d2 20 74 68 65 20 65 6e 64 20 62 72 61 6e 63 68 2c   the end branch,
2f9d3 20 74 6f 6f 2e 20 2a 2f 0a 20 20 20 20 20 20 69   too. */.      i
2f9d4 66 28 20 69 53 74 61 72 74 43 68 69 6c 64 21 3d  f( iStartChild!=
2f9d5 69 45 6e 64 43 68 69 6c 64 20 29 7b 0a 20 20 20  iEndChild ){.   
2f9d6 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
2f9d7 34 20 69 44 75 6d 6d 79 3b 0a 20 20 20 20 20 20  4 iDummy;.      
2f9d8 20 20 72 63 20 3d 20 6c 6f 61 64 41 6e 64 47 65    rc = loadAndGe
2f9d9 74 43 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e  tChildrenContain
2f9da 69 6e 67 28 76 2c 20 69 45 6e 64 43 68 69 6c 64  ing(v, iEndChild
2f9db 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20  , pTerm, nTerm, 
2f9dc 69 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20  isPrefix,.      
2f9dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f9de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f9df 20 20 20 20 26 69 44 75 6d 6d 79 2c 20 26 69 4e      &iDummy, &iN
2f9e0 65 78 74 45 6e 64 29 3b 0a 20 20 20 20 20 20 20  extEnd);.       
2f9e1 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2f9e2 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
2f9e3 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61        }..      a
2f9e4 73 73 65 72 74 28 20 69 4e 65 78 74 53 74 61 72  ssert( iNextStar
2f9e5 74 3c 3d 69 4e 65 78 74 45 6e 64 20 29 3b 0a 20  t<=iNextEnd );. 
2f9e6 20 20 20 20 20 69 53 74 61 72 74 43 68 69 6c 64       iStartChild
2f9e7 20 3d 20 69 4e 65 78 74 53 74 61 72 74 3b 0a 20   = iNextStart;. 
2f9e8 20 20 20 20 20 69 45 6e 64 43 68 69 6c 64 20 3d       iEndChild =
2f9e9 20 69 4e 65 78 74 45 6e 64 3b 0a 20 20 20 20 7d   iNextEnd;.    }
2f9ea 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 53 74  .    assert( iSt
2f9eb 61 72 74 43 68 69 6c 64 3c 3d 69 4c 65 61 76 65  artChild<=iLeave
2f9ec 73 45 6e 64 20 29 3b 0a 20 20 20 20 61 73 73 65  sEnd );.    asse
2f9ed 72 74 28 20 69 45 6e 64 43 68 69 6c 64 3c 3d 69  rt( iEndChild<=i
2f9ee 4c 65 61 76 65 73 45 6e 64 20 29 3b 0a 0a 20 20  LeavesEnd );..  
2f9ef 20 20 2f 2a 20 53 63 61 6e 20 74 68 72 6f 75 67    /* Scan throug
2f9f0 68 20 74 68 65 20 6c 65 61 66 20 73 65 67 6d 65  h the leaf segme
2f9f1 6e 74 73 20 66 6f 72 20 64 6f 63 6c 69 73 74 73  nts for doclists
2f9f2 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  . */.    return 
2f9f3 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65  loadSegmentLeave
2f9f4 73 28 76 2c 20 69 53 74 61 72 74 43 68 69 6c 64  s(v, iStartChild
2f9f5 2c 20 69 45 6e 64 43 68 69 6c 64 2c 0a 20 20 20  , iEndChild,.   
2f9f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f9f7 20 20 20 20 20 20 20 20 20 20 70 54 65 72 6d 2c            pTerm,
2f9f8 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78   nTerm, isPrefix
2f9f9 2c 20 6f 75 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  , out);.  }.}../
2f9fa 2a 20 43 61 6c 6c 20 6c 6f 61 64 53 65 67 6d 65  * Call loadSegme
2f9fb 6e 74 49 6e 74 28 29 20 74 6f 20 63 6f 6c 6c 65  ntInt() to colle
2f9fc 63 74 20 74 68 65 20 64 6f 63 6c 69 73 74 20 66  ct the doclist f
2f9fd 6f 72 20 70 54 65 72 6d 2f 6e 54 65 72 6d 2c 20  or pTerm/nTerm, 
2f9fe 74 68 65 6e 0a 2a 2a 20 6d 65 72 67 65 20 69 74  then.** merge it
2f9ff 73 20 64 6f 63 6c 69 73 74 20 6f 76 65 72 20 2a  s doclist over *
2fa00 6f 75 74 20 28 61 6e 79 20 64 75 70 6c 69 63 61  out (any duplica
2fa01 74 65 20 64 6f 63 6c 69 73 74 73 20 72 65 61 64  te doclists read
2fa02 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 73 65 67   from the.** seg
2fa03 6d 65 6e 74 20 72 6f 6f 74 65 64 20 61 74 20 70  ment rooted at p
2fa04 44 61 74 61 20 77 69 6c 6c 20 6f 76 65 72 77 72  Data will overwr
2fa05 69 74 65 20 74 68 6f 73 65 20 69 6e 20 2a 6f 75  ite those in *ou
2fa06 74 29 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73  t)..*/./* TODO(s
2fa07 68 65 73 73 29 20 43 6f 6e 73 69 64 65 72 20 63  hess) Consider c
2fa08 68 61 6e 67 69 6e 67 20 74 68 69 73 20 74 6f 20  hanging this to 
2fa09 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 64 65  determine the de
2fa0a 70 74 68 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 65  pth of the.** le
2fa0b 61 76 65 73 20 75 73 69 6e 67 20 65 69 74 68 65  aves using eithe
2fa0c 72 20 74 68 65 20 66 69 72 73 74 20 63 68 61 72  r the first char
2fa0d 61 63 74 65 72 73 20 6f 66 20 69 6e 74 65 72 69  acters of interi
2fa0e 6f 72 20 6e 6f 64 65 73 20 28 77 68 65 6e 0a 2a  or nodes (when.*
2fa0f 2a 20 3d 3d 31 2c 20 77 65 27 72 65 20 6f 6e 65  * ==1, we're one
2fa10 20 6c 65 76 65 6c 20 61 62 6f 76 65 20 74 68 65   level above the
2fa11 20 6c 65 61 76 65 73 29 2c 20 6f 72 20 74 68 65   leaves), or the
2fa12 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65 72   first character
2fa13 20 6f 66 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20   of.** the root 
2fa14 28 77 68 69 63 68 20 77 69 6c 6c 20 64 65 73 63  (which will desc
2fa15 72 69 62 65 20 74 68 65 20 68 65 69 67 68 74 20  ribe the height 
2fa16 6f 66 20 74 68 65 20 74 72 65 65 20 64 69 72 65  of the tree dire
2fa17 63 74 6c 79 29 2e 0a 2a 2a 20 45 69 74 68 65 72  ctly)..** Either
2fa18 20 66 65 65 6c 73 20 73 6f 6d 65 77 68 61 74 20   feels somewhat 
2fa19 74 72 69 63 6b 79 20 74 6f 20 6d 65 2e 0a 2a 2f  tricky to me..*/
2fa1a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
2fa1b 54 68 65 20 63 75 72 72 65 6e 74 20 6d 65 72 67  The current merg
2fa1c 65 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62  e is likely to b
2fa1d 65 20 73 6c 6f 77 20 66 6f 72 20 6c 61 72 67 65  e slow for large
2fa1e 0a 2a 2a 20 64 6f 63 6c 69 73 74 73 20 28 74 68  .** doclists (th
2fa1f 6f 75 67 68 20 69 74 20 73 68 6f 75 6c 64 20 70  ough it should p
2fa20 72 6f 63 65 73 73 20 66 72 6f 6d 20 6e 65 77 65  rocess from newe
2fa21 73 74 2f 73 6d 61 6c 6c 65 73 74 20 74 6f 0a 2a  st/smallest to.*
2fa22 2a 20 6f 6c 64 65 73 74 2f 6c 61 72 67 65 73 74  * oldest/largest
2fa23 2c 20 73 6f 20 69 74 20 6d 61 79 20 6e 6f 74 20  , so it may not 
2fa24 62 65 20 74 68 61 74 20 62 61 64 29 2e 20 20 49  be that bad).  I
2fa25 74 20 6d 69 67 68 74 20 62 65 20 75 73 65 66 75  t might be usefu
2fa26 6c 20 74 6f 0a 2a 2a 20 6d 6f 64 69 66 79 20 74  l to.** modify t
2fa27 68 69 6e 67 73 20 74 6f 20 61 6c 6c 6f 77 20 66  hings to allow f
2fa28 6f 72 20 4e 2d 77 61 79 20 6d 65 72 67 69 6e 67  or N-way merging
2fa29 2e 20 20 54 68 69 73 20 63 6f 75 6c 64 20 65 69  .  This could ei
2fa2a 74 68 65 72 20 62 65 0a 2a 2a 20 77 69 74 68 69  ther be.** withi
2fa2b 6e 20 61 20 73 65 67 6d 65 6e 74 2c 20 77 69 74  n a segment, wit
2fa2c 68 20 70 61 69 72 77 69 73 65 20 6d 65 72 67 65  h pairwise merge
2fa2d 73 20 61 63 72 6f 73 73 20 73 65 67 6d 65 6e 74  s across segment
2fa2e 73 2c 20 6f 72 20 61 63 72 6f 73 73 0a 2a 2a 20  s, or across.** 
2fa2f 61 6c 6c 20 73 65 67 6d 65 6e 74 73 20 61 74 20  all segments at 
2fa30 6f 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  once..*/.static 
2fa31 69 6e 74 20 6c 6f 61 64 53 65 67 6d 65 6e 74 28  int loadSegment(
2fa32 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
2fa33 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44  , const char *pD
2fa34 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a  ata, int nData,.
2fa35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa36 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e         sqlite_in
2fa37 74 36 34 20 69 4c 65 61 76 65 73 45 6e 64 2c 0a  t64 iLeavesEnd,.
2fa38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa39 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
2fa3a 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54  r *pTerm, int nT
2fa3b 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69  erm, int isPrefi
2fa3c 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  x,.             
2fa3d 20 20 20 20 20 20 20 20 20 20 44 61 74 61 42 75            DataBu
2fa3e 66 66 65 72 20 2a 6f 75 74 29 7b 0a 20 20 44 61  ffer *out){.  Da
2fa3f 74 61 42 75 66 66 65 72 20 72 65 73 75 6c 74 3b  taBuffer result;
2fa40 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73  .  int rc;..  as
2fa41 73 65 72 74 28 20 6e 44 61 74 61 3e 31 20 29 3b  sert( nData>1 );
2fa42 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f 64 65  ..  /* This code
2fa43 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65   should never be
2fa44 20 63 61 6c 6c 65 64 20 77 69 74 68 20 62 75 66   called with buf
2fa45 66 65 72 65 64 20 75 70 64 61 74 65 73 2e 20 2a  fered updates. *
2fa46 2f 0a 20 20 61 73 73 65 72 74 28 20 76 2d 3e 6e  /.  assert( v->n
2fa47 50 65 6e 64 69 6e 67 44 61 74 61 3c 30 20 29 3b  PendingData<0 );
2fa48 0a 0a 20 20 64 61 74 61 42 75 66 66 65 72 49 6e  ..  dataBufferIn
2fa49 69 74 28 26 72 65 73 75 6c 74 2c 20 30 29 3b 0a  it(&result, 0);.
2fa4a 20 20 72 63 20 3d 20 6c 6f 61 64 53 65 67 6d 65    rc = loadSegme
2fa4b 6e 74 49 6e 74 28 76 2c 20 70 44 61 74 61 2c 20  ntInt(v, pData, 
2fa4c 6e 44 61 74 61 2c 20 69 4c 65 61 76 65 73 45 6e  nData, iLeavesEn
2fa4d 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
2fa4e 20 20 20 20 20 20 20 20 20 70 54 65 72 6d 2c 20           pTerm, 
2fa4f 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c  nTerm, isPrefix,
2fa50 20 26 72 65 73 75 6c 74 29 3b 0a 20 20 69 66 28   &result);.  if(
2fa51 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
2fa52 26 20 72 65 73 75 6c 74 2e 6e 44 61 74 61 3e 30  & result.nData>0
2fa53 20 29 7b 0a 20 20 20 20 69 66 28 20 6f 75 74 2d   ){.    if( out-
2fa54 3e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20  >nData==0 ){.   
2fa55 20 20 20 44 61 74 61 42 75 66 66 65 72 20 74 6d     DataBuffer tm
2fa56 70 20 3d 20 2a 6f 75 74 3b 0a 20 20 20 20 20 20  p = *out;.      
2fa57 2a 6f 75 74 20 3d 20 72 65 73 75 6c 74 3b 0a 20  *out = result;. 
2fa58 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 74 6d       result = tm
2fa59 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  p;.    }else{.  
2fa5a 20 20 20 20 44 61 74 61 42 75 66 66 65 72 20 6d      DataBuffer m
2fa5b 65 72 67 65 64 3b 0a 20 20 20 20 20 20 44 4c 52  erged;.      DLR
2fa5c 65 61 64 65 72 20 72 65 61 64 65 72 73 5b 32 5d  eader readers[2]
2fa5d 3b 0a 0a 20 20 20 20 20 20 64 6c 72 49 6e 69 74  ;..      dlrInit
2fa5e 28 26 72 65 61 64 65 72 73 5b 30 5d 2c 20 44 4c  (&readers[0], DL
2fa5f 5f 44 45 46 41 55 4c 54 2c 20 6f 75 74 2d 3e 70  _DEFAULT, out->p
2fa60 44 61 74 61 2c 20 6f 75 74 2d 3e 6e 44 61 74 61  Data, out->nData
2fa61 29 3b 0a 20 20 20 20 20 20 64 6c 72 49 6e 69 74  );.      dlrInit
2fa62 28 26 72 65 61 64 65 72 73 5b 31 5d 2c 20 44 4c  (&readers[1], DL
2fa63 5f 44 45 46 41 55 4c 54 2c 20 72 65 73 75 6c 74  _DEFAULT, result
2fa64 2e 70 44 61 74 61 2c 20 72 65 73 75 6c 74 2e 6e  .pData, result.n
2fa65 44 61 74 61 29 3b 0a 20 20 20 20 20 20 64 61 74  Data);.      dat
2fa66 61 42 75 66 66 65 72 49 6e 69 74 28 26 6d 65 72  aBufferInit(&mer
2fa67 67 65 64 2c 20 6f 75 74 2d 3e 6e 44 61 74 61 2b  ged, out->nData+
2fa68 72 65 73 75 6c 74 2e 6e 44 61 74 61 29 3b 0a 20  result.nData);. 
2fa69 20 20 20 20 20 64 6f 63 4c 69 73 74 4d 65 72 67       docListMerg
2fa6a 65 28 26 6d 65 72 67 65 64 2c 20 72 65 61 64 65  e(&merged, reade
2fa6b 72 73 2c 20 32 29 3b 0a 20 20 20 20 20 20 64 61  rs, 2);.      da
2fa6c 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
2fa6d 6f 75 74 29 3b 0a 20 20 20 20 20 20 2a 6f 75 74  out);.      *out
2fa6e 20 3d 20 6d 65 72 67 65 64 3b 0a 20 20 20 20 20   = merged;.     
2fa6f 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72 65 61   dlrDestroy(&rea
2fa70 64 65 72 73 5b 30 5d 29 3b 0a 20 20 20 20 20 20  ders[0]);.      
2fa71 64 6c 72 44 65 73 74 72 6f 79 28 26 72 65 61 64  dlrDestroy(&read
2fa72 65 72 73 5b 31 5d 29 3b 0a 20 20 20 20 7d 0a 20  ers[1]);.    }. 
2fa73 20 7d 0a 20 20 64 61 74 61 42 75 66 66 65 72 44   }.  dataBufferD
2fa74 65 73 74 72 6f 79 28 26 72 65 73 75 6c 74 29 3b  estroy(&result);
2fa75 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2fa76 0a 2f 2a 20 53 63 61 6e 20 74 68 65 20 64 61 74  ./* Scan the dat
2fa77 61 62 61 73 65 20 61 6e 64 20 6d 65 72 67 65 20  abase and merge 
2fa78 74 6f 67 65 74 68 65 72 20 74 68 65 20 70 6f 73  together the pos
2fa79 74 69 6e 67 20 6c 69 73 74 73 20 66 6f 72 20 74  ting lists for t
2fa7a 68 65 20 74 65 72 6d 0a 2a 2a 20 69 6e 74 6f 20  he term.** into 
2fa7b 2a 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  *out..*/.static 
2fa7c 69 6e 74 20 74 65 72 6d 53 65 6c 65 63 74 28 66  int termSelect(f
2fa7d 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
2fa7e 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 0a 20 20   int iColumn,.  
2fa7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa80 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2fa81 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d  pTerm, int nTerm
2fa82 2c 20 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a  , int isPrefix,.
2fa83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa84 20 20 20 20 20 20 44 6f 63 4c 69 73 74 54 79 70        DocListTyp
2fa85 65 20 69 54 79 70 65 2c 20 44 61 74 61 42 75 66  e iType, DataBuf
2fa86 66 65 72 20 2a 6f 75 74 29 7b 0a 20 20 44 61 74  fer *out){.  Dat
2fa87 61 42 75 66 66 65 72 20 64 6f 63 6c 69 73 74 3b  aBuffer doclist;
2fa88 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
2fa89 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73  *s;.  int rc = s
2fa8a 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
2fa8b 28 76 2c 20 53 45 47 44 49 52 5f 53 45 4c 45 43  (v, SEGDIR_SELEC
2fa8c 54 5f 41 4c 4c 5f 53 54 4d 54 2c 20 26 73 29 3b  T_ALL_STMT, &s);
2fa8d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
2fa8e 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
2fa8f 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f 64  ;..  /* This cod
2fa90 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62  e should never b
2fa91 65 20 63 61 6c 6c 65 64 20 77 69 74 68 20 62 75  e called with bu
2fa92 66 66 65 72 65 64 20 75 70 64 61 74 65 73 2e 20  ffered updates. 
2fa93 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 76 2d 3e  */.  assert( v->
2fa94 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c 30 20 29  nPendingData<0 )
2fa95 3b 0a 0a 20 20 64 61 74 61 42 75 66 66 65 72 49  ;..  dataBufferI
2fa96 6e 69 74 28 26 64 6f 63 6c 69 73 74 2c 20 30 29  nit(&doclist, 0)
2fa97 3b 0a 0a 20 20 2f 2a 20 54 72 61 76 65 72 73 65  ;..  /* Traverse
2fa98 20 74 68 65 20 73 65 67 6d 65 6e 74 73 20 66 72   the segments fr
2fa99 6f 6d 20 6f 6c 64 65 73 74 20 74 6f 20 6e 65 77  om oldest to new
2fa9a 65 73 74 20 73 6f 20 74 68 61 74 20 6e 65 77 65  est so that newe
2fa9b 72 20 64 6f 63 6c 69 73 74 0a 20 20 2a 2a 20 65  r doclist.  ** e
2fa9c 6c 65 6d 65 6e 74 73 20 66 6f 72 20 67 69 76 65  lements for give
2fa9d 6e 20 64 6f 63 69 64 73 20 6f 76 65 72 77 72 69  n docids overwri
2fa9e 74 65 20 6f 6c 64 65 72 20 65 6c 65 6d 65 6e 74  te older element
2fa9f 73 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28  s..  */.  while(
2faa0 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73   (rc = sqlite3_s
2faa1 74 65 70 28 73 29 29 3d 3d 53 51 4c 49 54 45 5f  tep(s))==SQLITE_
2faa2 52 4f 57 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  ROW ){.    const
2faa3 20 63 68 61 72 20 2a 70 44 61 74 61 20 3d 20 73   char *pData = s
2faa4 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
2faa5 6f 62 28 73 2c 20 32 29 3b 0a 20 20 20 20 63 6f  ob(s, 2);.    co
2faa6 6e 73 74 20 69 6e 74 20 6e 44 61 74 61 20 3d 20  nst int nData = 
2faa7 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
2faa8 79 74 65 73 28 73 2c 20 32 29 3b 0a 20 20 20 20  ytes(s, 2);.    
2faa9 63 6f 6e 73 74 20 73 71 6c 69 74 65 5f 69 6e 74  const sqlite_int
2faaa 36 34 20 69 4c 65 61 76 65 73 45 6e 64 20 3d 20  64 iLeavesEnd = 
2faab 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
2faac 6e 74 36 34 28 73 2c 20 31 29 3b 0a 20 20 20 20  nt64(s, 1);.    
2faad 72 63 20 3d 20 6c 6f 61 64 53 65 67 6d 65 6e 74  rc = loadSegment
2faae 28 76 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61  (v, pData, nData
2faaf 2c 20 69 4c 65 61 76 65 73 45 6e 64 2c 20 70 54  , iLeavesEnd, pT
2fab0 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72  erm, nTerm, isPr
2fab1 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20  efix,.          
2fab2 20 20 20 20 20 20 20 20 20 20 20 26 64 6f 63 6c             &docl
2fab3 69 73 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ist);.    if( rc
2fab4 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
2fab5 74 6f 20 65 72 72 3b 0a 20 20 7d 0a 20 20 69 66  to err;.  }.  if
2fab6 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
2fab7 45 20 29 7b 0a 20 20 20 20 69 66 28 20 64 6f 63  E ){.    if( doc
2fab8 6c 69 73 74 2e 6e 44 61 74 61 21 3d 30 20 29 7b  list.nData!=0 ){
2fab9 0a 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73  .      /* TODO(s
2faba 68 65 73 73 29 20 54 68 65 20 6f 6c 64 20 74 65  hess) The old te
2fabb 72 6d 5f 73 65 6c 65 63 74 5f 61 6c 6c 28 29 20  rm_select_all() 
2fabc 63 6f 64 65 20 61 70 70 6c 69 65 64 20 74 68 65  code applied the
2fabd 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 2a 2a   column.      **
2fabe 20 72 65 73 74 72 69 63 74 20 61 73 20 77 65 20   restrict as we 
2fabf 6d 65 72 67 65 64 20 73 65 67 6d 65 6e 74 73 2c  merged segments,
2fac0 20 6c 65 61 64 69 6e 67 20 74 6f 20 73 6d 61 6c   leading to smal
2fac1 6c 65 72 20 62 75 66 66 65 72 73 2e 0a 20 20 20  ler buffers..   
2fac2 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 70 72     ** This is pr
2fac3 6f 62 61 62 6c 79 20 77 6f 72 74 68 77 68 69 6c  obably worthwhil
2fac4 65 20 74 6f 20 62 72 69 6e 67 20 62 61 63 6b 2c  e to bring back,
2fac5 20 6f 6e 63 65 20 74 68 65 20 6e 65 77 20 73 74   once the new st
2fac6 6f 72 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 73  orage.      ** s
2fac7 79 73 74 65 6d 20 69 73 20 63 68 65 63 6b 65 64  ystem is checked
2fac8 20 69 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20   in..      */.  
2fac9 20 20 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3d      if( iColumn=
2faca 3d 76 2d 3e 6e 43 6f 6c 75 6d 6e 29 20 69 43 6f  =v->nColumn) iCo
2facb 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20  lumn = -1;.     
2facc 20 64 6f 63 4c 69 73 74 54 72 69 6d 28 44 4c 5f   docListTrim(DL_
2facd 44 45 46 41 55 4c 54 2c 20 64 6f 63 6c 69 73 74  DEFAULT, doclist
2face 2e 70 44 61 74 61 2c 20 64 6f 63 6c 69 73 74 2e  .pData, doclist.
2facf 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20  nData,.         
2fad0 20 20 20 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e           iColumn
2fad1 2c 20 69 54 79 70 65 2c 20 6f 75 74 29 3b 0a 20  , iType, out);. 
2fad2 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 53 51     }.    rc = SQ
2fad3 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 65  LITE_OK;.  }.. e
2fad4 72 72 3a 0a 20 20 64 61 74 61 42 75 66 66 65 72  rr:.  dataBuffer
2fad5 44 65 73 74 72 6f 79 28 26 64 6f 63 6c 69 73 74  Destroy(&doclist
2fad6 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
2fad7 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
2fad8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fad9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fada 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fadb 2a 2a 2a 2a 2f 0a 2f 2a 20 55 73 65 64 20 74 6f  ****/./* Used to
2fadc 20 68 6f 6c 64 20 68 61 73 68 74 61 62 6c 65 20   hold hashtable 
2fadd 64 61 74 61 20 66 6f 72 20 73 6f 72 74 69 6e 67  data for sorting
2fade 2e 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  . */.typedef str
2fadf 75 63 74 20 54 65 72 6d 44 61 74 61 20 7b 0a 20  uct TermData {. 
2fae0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65   const char *pTe
2fae1 72 6d 3b 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b  rm;.  int nTerm;
2fae2 0a 20 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a  .  DLCollector *
2fae3 70 43 6f 6c 6c 65 63 74 6f 72 3b 0a 7d 20 54 65  pCollector;.} Te
2fae4 72 6d 44 61 74 61 3b 0a 0a 2f 2a 20 4f 72 64 65  rmData;../* Orde
2fae5 72 73 20 54 65 72 6d 44 61 74 61 20 65 6c 65 6d  rs TermData elem
2fae6 65 6e 74 73 20 69 6e 20 73 74 72 63 6d 70 20 66  ents in strcmp f
2fae7 61 73 68 69 6f 6e 20 28 20 3c 30 20 66 6f 72 20  ashion ( <0 for 
2fae8 6c 65 73 73 2d 74 68 61 6e 2c 20 30 0a 2a 2a 20  less-than, 0.** 
2fae9 66 6f 72 20 65 71 75 61 6c 2c 20 3e 30 20 66 6f  for equal, >0 fo
2faea 72 20 67 72 65 61 74 65 72 2d 74 68 61 6e 29 2e  r greater-than).
2faeb 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 74  .*/.static int t
2faec 65 72 6d 44 61 74 61 43 6d 70 28 63 6f 6e 73 74  ermDataCmp(const
2faed 20 76 6f 69 64 20 2a 61 76 2c 20 63 6f 6e 73 74   void *av, const
2faee 20 76 6f 69 64 20 2a 62 76 29 7b 0a 20 20 63 6f   void *bv){.  co
2faef 6e 73 74 20 54 65 72 6d 44 61 74 61 20 2a 61 20  nst TermData *a 
2faf0 3d 20 28 63 6f 6e 73 74 20 54 65 72 6d 44 61 74  = (const TermDat
2faf1 61 20 2a 29 61 76 3b 0a 20 20 63 6f 6e 73 74 20  a *)av;.  const 
2faf2 54 65 72 6d 44 61 74 61 20 2a 62 20 3d 20 28 63  TermData *b = (c
2faf3 6f 6e 73 74 20 54 65 72 6d 44 61 74 61 20 2a 29  onst TermData *)
2faf4 62 76 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 61 2d  bv;.  int n = a-
2faf5 3e 6e 54 65 72 6d 3c 62 2d 3e 6e 54 65 72 6d 20  >nTerm<b->nTerm 
2faf6 3f 20 61 2d 3e 6e 54 65 72 6d 20 3a 20 62 2d 3e  ? a->nTerm : b->
2faf7 6e 54 65 72 6d 3b 0a 20 20 69 6e 74 20 63 20 3d  nTerm;.  int c =
2faf8 20 6d 65 6d 63 6d 70 28 61 2d 3e 70 54 65 72 6d   memcmp(a->pTerm
2faf9 2c 20 62 2d 3e 70 54 65 72 6d 2c 20 6e 29 3b 0a  , b->pTerm, n);.
2fafa 20 20 69 66 28 20 63 21 3d 30 20 29 20 72 65 74    if( c!=0 ) ret
2fafb 75 72 6e 20 63 3b 0a 20 20 72 65 74 75 72 6e 20  urn c;.  return 
2fafc 61 2d 3e 6e 54 65 72 6d 2d 62 2d 3e 6e 54 65 72  a->nTerm-b->nTer
2fafd 6d 3b 0a 7d 0a 0a 2f 2a 20 4f 72 64 65 72 20 70  m;.}../* Order p
2fafe 54 65 72 6d 73 20 64 61 74 61 20 62 79 20 74 65  Terms data by te
2faff 72 6d 2c 20 74 68 65 6e 20 77 72 69 74 65 20 61  rm, then write a
2fb00 20 6e 65 77 20 6c 65 76 65 6c 20 30 20 73 65 67   new level 0 seg
2fb01 6d 65 6e 74 20 75 73 69 6e 67 0a 2a 2a 20 4c 65  ment using.** Le
2fb02 61 66 57 72 69 74 65 72 2e 0a 2a 2f 0a 73 74 61  afWriter..*/.sta
2fb03 74 69 63 20 69 6e 74 20 77 72 69 74 65 5a 65 72  tic int writeZer
2fb04 6f 53 65 67 6d 65 6e 74 28 66 75 6c 6c 74 65 78  oSegment(fulltex
2fb05 74 5f 76 74 61 62 20 2a 76 2c 20 66 74 73 33 48  t_vtab *v, fts3H
2fb06 61 73 68 20 2a 70 54 65 72 6d 73 29 7b 0a 20 20  ash *pTerms){.  
2fb07 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 3b  fts3HashElem *e;
2fb08 0a 20 20 69 6e 74 20 69 64 78 2c 20 72 63 2c 20  .  int idx, rc, 
2fb09 69 2c 20 6e 3b 0a 20 20 54 65 72 6d 44 61 74 61  i, n;.  TermData
2fb0a 20 2a 70 44 61 74 61 3b 0a 20 20 4c 65 61 66 57   *pData;.  LeafW
2fb0b 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a 20 20  riter writer;.  
2fb0c 44 61 74 61 42 75 66 66 65 72 20 64 6c 3b 0a 0a  DataBuffer dl;..
2fb0d 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 74    /* Determine t
2fb0e 68 65 20 6e 65 78 74 20 69 6e 64 65 78 20 61 74  he next index at
2fb0f 20 6c 65 76 65 6c 20 30 2c 20 6d 65 72 67 69 6e   level 0, mergin
2fb10 67 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 20  g as necessary. 
2fb11 2a 2f 0a 20 20 72 63 20 3d 20 73 65 67 64 69 72  */.  rc = segdir
2fb12 4e 65 78 74 49 6e 64 65 78 28 76 2c 20 30 2c 20  NextIndex(v, 0, 
2fb13 26 69 64 78 29 3b 0a 20 20 69 66 28 20 72 63 21  &idx);.  if( rc!
2fb14 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
2fb15 75 72 6e 20 72 63 3b 0a 0a 20 20 6e 20 3d 20 66  urn rc;..  n = f
2fb16 74 73 33 48 61 73 68 43 6f 75 6e 74 28 70 54 65  ts3HashCount(pTe
2fb17 72 6d 73 29 3b 0a 20 20 70 44 61 74 61 20 3d 20  rms);.  pData = 
2fb18 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
2fb19 2a 73 69 7a 65 6f 66 28 54 65 72 6d 44 61 74 61  *sizeof(TermData
2fb1a 29 29 3b 0a 0a 20 20 66 6f 72 28 69 20 3d 20 30  ));..  for(i = 0
2fb1b 2c 20 65 20 3d 20 66 74 73 33 48 61 73 68 46 69  , e = fts3HashFi
2fb1c 72 73 74 28 70 54 65 72 6d 73 29 3b 20 65 3b 20  rst(pTerms); e; 
2fb1d 69 2b 2b 2c 20 65 20 3d 20 66 74 73 33 48 61 73  i++, e = fts3Has
2fb1e 68 4e 65 78 74 28 65 29 29 7b 0a 20 20 20 20 61  hNext(e)){.    a
2fb1f 73 73 65 72 74 28 20 69 3c 6e 20 29 3b 0a 20 20  ssert( i<n );.  
2fb20 20 20 70 44 61 74 61 5b 69 5d 2e 70 54 65 72 6d    pData[i].pTerm
2fb21 20 3d 20 66 74 73 33 48 61 73 68 4b 65 79 28 65   = fts3HashKey(e
2fb22 29 3b 0a 20 20 20 20 70 44 61 74 61 5b 69 5d 2e  );.    pData[i].
2fb23 6e 54 65 72 6d 20 3d 20 66 74 73 33 48 61 73 68  nTerm = fts3Hash
2fb24 4b 65 79 73 69 7a 65 28 65 29 3b 0a 20 20 20 20  Keysize(e);.    
2fb25 70 44 61 74 61 5b 69 5d 2e 70 43 6f 6c 6c 65 63  pData[i].pCollec
2fb26 74 6f 72 20 3d 20 66 74 73 33 48 61 73 68 44 61  tor = fts3HashDa
2fb27 74 61 28 65 29 3b 0a 20 20 7d 0a 20 20 61 73 73  ta(e);.  }.  ass
2fb28 65 72 74 28 20 69 3d 3d 6e 20 29 3b 0a 0a 20 20  ert( i==n );..  
2fb29 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 53  /* TODO(shess) S
2fb2a 68 6f 75 6c 64 20 77 65 20 61 6c 6c 6f 77 20 75  hould we allow u
2fb2b 73 65 72 2d 64 65 66 69 6e 65 64 20 63 6f 6c 6c  ser-defined coll
2fb2c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 2c  ation sequences,
2fb2d 0a 20 20 2a 2a 20 68 65 72 65 3f 20 20 49 20 74  .  ** here?  I t
2fb2e 68 69 6e 6b 20 77 65 20 6f 6e 6c 79 20 6e 65 65  hink we only nee
2fb2f 64 20 74 68 61 74 20 6f 6e 63 65 20 77 65 20 73  d that once we s
2fb30 75 70 70 6f 72 74 20 70 72 65 66 69 78 20 73 65  upport prefix se
2fb31 61 72 63 68 65 73 2e 0a 20 20 2a 2f 0a 20 20 69  arches..  */.  i
2fb32 66 28 20 6e 3e 31 20 29 20 71 73 6f 72 74 28 70  f( n>1 ) qsort(p
2fb33 44 61 74 61 2c 20 6e 2c 20 73 69 7a 65 6f 66 28  Data, n, sizeof(
2fb34 2a 70 44 61 74 61 29 2c 20 74 65 72 6d 44 61 74  *pData), termDat
2fb35 61 43 6d 70 29 3b 0a 0a 20 20 2f 2a 20 54 4f 44  aCmp);..  /* TOD
2fb36 4f 28 73 68 65 73 73 29 20 52 65 66 61 63 74 6f  O(shess) Refacto
2fb37 72 20 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e  r so that we can
2fb38 20 77 72 69 74 65 20 64 69 72 65 63 74 6c 79 20   write directly 
2fb39 74 6f 20 74 68 65 20 73 65 67 6d 65 6e 74 0a 20  to the segment. 
2fb3a 20 2a 2a 20 44 61 74 61 42 75 66 66 65 72 2c 20   ** DataBuffer, 
2fb3b 61 73 20 68 61 70 70 65 6e 73 20 66 6f 72 20 73  as happens for s
2fb3c 65 67 6d 65 6e 74 20 6d 65 72 67 65 73 2e 0a 20  egment merges.. 
2fb3d 20 2a 2f 0a 20 20 6c 65 61 66 57 72 69 74 65 72   */.  leafWriter
2fb3e 49 6e 69 74 28 30 2c 20 69 64 78 2c 20 26 77 72  Init(0, idx, &wr
2fb3f 69 74 65 72 29 3b 0a 20 20 64 61 74 61 42 75 66  iter);.  dataBuf
2fb40 66 65 72 49 6e 69 74 28 26 64 6c 2c 20 30 29 3b  ferInit(&dl, 0);
2fb41 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b  .  for(i=0; i<n;
2fb42 20 69 2b 2b 29 7b 0a 20 20 20 20 64 61 74 61 42   i++){.    dataB
2fb43 75 66 66 65 72 52 65 73 65 74 28 26 64 6c 29 3b  ufferReset(&dl);
2fb44 0a 20 20 20 20 64 6c 63 41 64 64 44 6f 63 6c 69  .    dlcAddDocli
2fb45 73 74 28 70 44 61 74 61 5b 69 5d 2e 70 43 6f 6c  st(pData[i].pCol
2fb46 6c 65 63 74 6f 72 2c 20 26 64 6c 29 3b 0a 20 20  lector, &dl);.  
2fb47 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65    rc = leafWrite
2fb48 72 53 74 65 70 28 76 2c 20 26 77 72 69 74 65 72  rStep(v, &writer
2fb49 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2fb4a 20 20 20 20 20 20 20 20 20 20 70 44 61 74 61 5b            pData[
2fb4b 69 5d 2e 70 54 65 72 6d 2c 20 70 44 61 74 61 5b  i].pTerm, pData[
2fb4c 69 5d 2e 6e 54 65 72 6d 2c 20 64 6c 2e 70 44 61  i].nTerm, dl.pDa
2fb4d 74 61 2c 20 64 6c 2e 6e 44 61 74 61 29 3b 0a 20  ta, dl.nData);. 
2fb4e 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
2fb4f 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b  E_OK ) goto err;
2fb50 0a 20 20 7d 0a 20 20 72 63 20 3d 20 6c 65 61 66  .  }.  rc = leaf
2fb51 57 72 69 74 65 72 46 69 6e 61 6c 69 7a 65 28 76  WriterFinalize(v
2fb52 2c 20 26 77 72 69 74 65 72 29 3b 0a 0a 20 65 72  , &writer);.. er
2fb53 72 3a 0a 20 20 64 61 74 61 42 75 66 66 65 72 44  r:.  dataBufferD
2fb54 65 73 74 72 6f 79 28 26 64 6c 29 3b 0a 20 20 73  estroy(&dl);.  s
2fb55 71 6c 69 74 65 33 5f 66 72 65 65 28 70 44 61 74  qlite3_free(pDat
2fb56 61 29 3b 0a 20 20 6c 65 61 66 57 72 69 74 65 72  a);.  leafWriter
2fb57 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29  Destroy(&writer)
2fb58 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
2fb59 0a 0a 2f 2a 20 49 66 20 70 65 6e 64 69 6e 67 54  ../* If pendingT
2fb5a 65 72 6d 73 20 68 61 73 20 64 61 74 61 2c 20 66  erms has data, f
2fb5b 72 65 65 20 69 74 2e 20 2a 2f 0a 73 74 61 74 69  ree it. */.stati
2fb5c 63 20 69 6e 74 20 63 6c 65 61 72 50 65 6e 64 69  c int clearPendi
2fb5d 6e 67 54 65 72 6d 73 28 66 75 6c 6c 74 65 78 74  ngTerms(fulltext
2fb5e 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20 69 66 28  _vtab *v){.  if(
2fb5f 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61   v->nPendingData
2fb60 3e 3d 30 20 29 7b 0a 20 20 20 20 66 74 73 33 48  >=0 ){.    fts3H
2fb61 61 73 68 45 6c 65 6d 20 2a 65 3b 0a 20 20 20 20  ashElem *e;.    
2fb62 66 6f 72 28 65 3d 66 74 73 33 48 61 73 68 46 69  for(e=fts3HashFi
2fb63 72 73 74 28 26 76 2d 3e 70 65 6e 64 69 6e 67 54  rst(&v->pendingT
2fb64 65 72 6d 73 29 3b 20 65 3b 20 65 3d 66 74 73 33  erms); e; e=fts3
2fb65 48 61 73 68 4e 65 78 74 28 65 29 29 7b 0a 20 20  HashNext(e)){.  
2fb66 20 20 20 20 64 6c 63 44 65 6c 65 74 65 28 66 74      dlcDelete(ft
2fb67 73 33 48 61 73 68 44 61 74 61 28 65 29 29 3b 0a  s3HashData(e));.
2fb68 20 20 20 20 7d 0a 20 20 20 20 66 74 73 33 48 61      }.    fts3Ha
2fb69 73 68 43 6c 65 61 72 28 26 76 2d 3e 70 65 6e 64  shClear(&v->pend
2fb6a 69 6e 67 54 65 72 6d 73 29 3b 0a 20 20 20 20 76  ingTerms);.    v
2fb6b 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 20 3d  ->nPendingData =
2fb6c 20 2d 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   -1;.  }.  retur
2fb6d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
2fb6e 2f 2a 20 49 66 20 70 65 6e 64 69 6e 67 54 65 72  /* If pendingTer
2fb6f 6d 73 20 68 61 73 20 64 61 74 61 2c 20 66 6c 75  ms has data, flu
2fb70 73 68 20 69 74 20 74 6f 20 61 20 6c 65 76 65 6c  sh it to a level
2fb71 2d 7a 65 72 6f 20 73 65 67 6d 65 6e 74 2c 20 61  -zero segment, a
2fb72 6e 64 0a 2a 2a 20 66 72 65 65 20 69 74 2e 0a 2a  nd.** free it..*
2fb73 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 75  /.static int flu
2fb74 73 68 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 66  shPendingTerms(f
2fb75 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29  ulltext_vtab *v)
2fb76 7b 0a 20 20 69 66 28 20 76 2d 3e 6e 50 65 6e 64  {.  if( v->nPend
2fb77 69 6e 67 44 61 74 61 3e 3d 30 20 29 7b 0a 20 20  ingData>=0 ){.  
2fb78 20 20 69 6e 74 20 72 63 20 3d 20 77 72 69 74 65    int rc = write
2fb79 5a 65 72 6f 53 65 67 6d 65 6e 74 28 76 2c 20 26  ZeroSegment(v, &
2fb7a 76 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 29  v->pendingTerms)
2fb7b 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
2fb7c 4c 49 54 45 5f 4f 4b 20 29 20 63 6c 65 61 72 50  LITE_OK ) clearP
2fb7d 65 6e 64 69 6e 67 54 65 72 6d 73 28 76 29 3b 0a  endingTerms(v);.
2fb7e 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
2fb7f 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
2fb80 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 49 66 20  TE_OK;.}../* If 
2fb81 70 65 6e 64 69 6e 67 54 65 72 6d 73 20 69 73 20  pendingTerms is 
2fb82 22 74 6f 6f 20 62 69 67 22 2c 20 6f 72 20 64 6f  "too big", or do
2fb83 63 69 64 20 69 73 20 6f 75 74 20 6f 66 20 6f 72  cid is out of or
2fb84 64 65 72 2c 20 66 6c 75 73 68 20 69 74 2e 0a 2a  der, flush it..*
2fb85 2a 20 52 65 67 61 72 64 6c 65 73 73 2c 20 62 65  * Regardless, be
2fb86 20 63 65 72 74 61 69 6e 20 74 68 61 74 20 70 65   certain that pe
2fb87 6e 64 69 6e 67 54 65 72 6d 73 20 69 73 20 69 6e  ndingTerms is in
2fb88 69 74 69 61 6c 69 7a 65 64 20 66 6f 72 20 75 73  itialized for us
2fb89 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
2fb8a 20 69 6e 69 74 50 65 6e 64 69 6e 67 54 65 72 6d   initPendingTerm
2fb8b 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  s(fulltext_vtab 
2fb8c 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  *v, sqlite_int64
2fb8d 20 69 44 6f 63 69 64 29 7b 0a 20 20 2f 2a 20 54   iDocid){.  /* T
2fb8e 4f 44 4f 28 73 68 65 73 73 29 20 45 78 70 6c 6f  ODO(shess) Explo
2fb8f 72 65 20 77 68 65 74 68 65 72 20 70 61 72 74 69  re whether parti
2fb90 61 6c 6c 79 20 66 6c 75 73 68 69 6e 67 20 74 68  ally flushing th
2fb91 65 20 62 75 66 66 65 72 20 6f 6e 0a 20 20 2a 2a  e buffer on.  **
2fb92 20 66 6f 72 63 65 64 2d 66 6c 75 73 68 20 77 6f   forced-flush wo
2fb93 75 6c 64 20 70 72 6f 76 69 64 65 20 62 65 74 74  uld provide bett
2fb94 65 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20  er performance. 
2fb95 20 49 20 73 75 73 70 65 63 74 20 74 68 61 74 20   I suspect that 
2fb96 69 66 0a 20 20 2a 2a 20 77 65 20 6f 72 64 65 72  if.  ** we order
2fb97 65 64 20 74 68 65 20 64 6f 63 6c 69 73 74 73 20  ed the doclists 
2fb98 62 79 20 73 69 7a 65 20 61 6e 64 20 66 6c 75 73  by size and flus
2fb99 68 65 64 20 74 68 65 20 6c 61 72 67 65 73 74 20  hed the largest 
2fb9a 75 6e 74 69 6c 20 74 68 65 0a 20 20 2a 2a 20 62  until the.  ** b
2fb9b 75 66 66 65 72 20 77 61 73 20 68 61 6c 66 20 65  uffer was half e
2fb9c 6d 70 74 79 2c 20 74 68 61 74 20 77 6f 75 6c 64  mpty, that would
2fb9d 20 6c 65 74 20 74 68 65 20 6c 65 73 73 20 66 72   let the less fr
2fb9e 65 71 75 65 6e 74 20 74 65 72 6d 73 0a 20 20 2a  equent terms.  *
2fb9f 2a 20 67 65 6e 65 72 61 74 65 20 6c 6f 6e 67 65  * generate longe
2fba0 72 20 64 6f 63 6c 69 73 74 73 2e 0a 20 20 2a 2f  r doclists..  */
2fba1 0a 20 20 69 66 28 20 69 44 6f 63 69 64 3c 3d 76  .  if( iDocid<=v
2fba2 2d 3e 69 50 72 65 76 44 6f 63 69 64 20 7c 7c 20  ->iPrevDocid || 
2fba3 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3e  v->nPendingData>
2fba4 6b 50 65 6e 64 69 6e 67 54 68 72 65 73 68 6f 6c  kPendingThreshol
2fba5 64 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20  d ){.    int rc 
2fba6 3d 20 66 6c 75 73 68 50 65 6e 64 69 6e 67 54 65  = flushPendingTe
2fba7 72 6d 73 28 76 29 3b 0a 20 20 20 20 69 66 28 20  rms(v);.    if( 
2fba8 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2fba9 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
2fbaa 20 69 66 28 20 76 2d 3e 6e 50 65 6e 64 69 6e 67   if( v->nPending
2fbab 44 61 74 61 3c 30 20 29 7b 0a 20 20 20 20 66 74  Data<0 ){.    ft
2fbac 73 33 48 61 73 68 49 6e 69 74 28 26 76 2d 3e 70  s3HashInit(&v->p
2fbad 65 6e 64 69 6e 67 54 65 72 6d 73 2c 20 46 54 53  endingTerms, FTS
2fbae 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 2c 20 31  3_HASH_STRING, 1
2fbaf 29 3b 0a 20 20 20 20 76 2d 3e 6e 50 65 6e 64 69  );.    v->nPendi
2fbb0 6e 67 44 61 74 61 20 3d 20 30 3b 0a 20 20 7d 0a  ngData = 0;.  }.
2fbb1 20 20 76 2d 3e 69 50 72 65 76 44 6f 63 69 64 20    v->iPrevDocid 
2fbb2 3d 20 69 44 6f 63 69 64 3b 0a 20 20 72 65 74 75  = iDocid;.  retu
2fbb3 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
2fbb4 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ./* This functio
2fbb5 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  n implements the
2fbb6 20 78 55 70 64 61 74 65 20 63 61 6c 6c 62 61 63   xUpdate callbac
2fbb7 6b 3b 20 69 74 20 69 73 20 74 68 65 20 74 6f 70  k; it is the top
2fbb8 2d 6c 65 76 65 6c 20 65 6e 74 72 79 0a 20 2a 20  -level entry. * 
2fbb9 70 6f 69 6e 74 20 66 6f 72 20 69 6e 73 65 72 74  point for insert
2fbba 69 6e 67 2c 20 64 65 6c 65 74 69 6e 67 20 6f 72  ing, deleting or
2fbbb 20 75 70 64 61 74 69 6e 67 20 61 20 72 6f 77 20   updating a row 
2fbbc 69 6e 20 61 20 66 75 6c 6c 2d 74 65 78 74 20 74  in a full-text t
2fbbd 61 62 6c 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20  able. */.static 
2fbbe 69 6e 74 20 66 75 6c 6c 74 65 78 74 55 70 64 61  int fulltextUpda
2fbbf 74 65 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  te(sqlite3_vtab 
2fbc0 2a 70 56 74 61 62 2c 20 69 6e 74 20 6e 41 72 67  *pVtab, int nArg
2fbc1 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
2fbc2 2a 2a 70 70 41 72 67 2c 0a 20 20 20 20 20 20 20  **ppArg,.       
2fbc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fbc4 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
2fbc5 2a 70 52 6f 77 69 64 29 7b 0a 20 20 66 75 6c 6c  *pRowid){.  full
2fbc6 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d 20 28  text_vtab *v = (
2fbc7 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29  fulltext_vtab *)
2fbc8 20 70 56 74 61 62 3b 0a 20 20 69 6e 74 20 72 63   pVtab;.  int rc
2fbc9 3b 0a 0a 20 20 46 54 53 54 52 41 43 45 28 28 22  ;..  FTSTRACE(("
2fbca 46 54 53 33 20 55 70 64 61 74 65 20 25 70 5c 6e  FTS3 Update %p\n
2fbcb 22 2c 20 70 56 74 61 62 29 29 3b 0a 0a 20 20 69  ", pVtab));..  i
2fbcc 66 28 20 6e 41 72 67 3c 32 20 29 7b 0a 20 20 20  f( nArg<2 ){.   
2fbcd 20 72 63 20 3d 20 69 6e 64 65 78 5f 64 65 6c 65   rc = index_dele
2fbce 74 65 28 76 2c 20 73 71 6c 69 74 65 33 5f 76 61  te(v, sqlite3_va
2fbcf 6c 75 65 5f 69 6e 74 36 34 28 70 70 41 72 67 5b  lue_int64(ppArg[
2fbd0 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63  0]));.    if( rc
2fbd1 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
2fbd2 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 6a 75       /* If we ju
2fbd3 73 74 20 64 65 6c 65 74 65 64 20 74 68 65 20 6c  st deleted the l
2fbd4 61 73 74 20 72 6f 77 20 69 6e 20 74 68 65 20 74  ast row in the t
2fbd5 61 62 6c 65 2c 20 63 6c 65 61 72 20 6f 75 74 20  able, clear out 
2fbd6 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 69 6e 64  the.      ** ind
2fbd7 65 78 20 64 61 74 61 2e 0a 20 20 20 20 20 20 2a  ex data..      *
2fbd8 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 63 6f 6e  /.      rc = con
2fbd9 74 65 6e 74 5f 65 78 69 73 74 73 28 76 29 3b 0a  tent_exists(v);.
2fbda 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
2fbdb 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
2fbdc 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
2fbdd 4f 4b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  OK;.      }else 
2fbde 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  if( rc==SQLITE_D
2fbdf 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ONE ){.        /
2fbe0 2a 20 43 6c 65 61 72 20 74 68 65 20 70 65 6e 64  * Clear the pend
2fbe1 69 6e 67 20 74 65 72 6d 73 20 73 6f 20 77 65 20  ing terms so we 
2fbe2 64 6f 6e 27 74 20 66 6c 75 73 68 20 61 20 75 73  don't flush a us
2fbe3 65 6c 65 73 73 20 6c 65 76 65 6c 2d 30 0a 20 20  eless level-0.  
2fbe4 20 20 20 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74        ** segment
2fbe5 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61   when the transa
2fbe6 63 74 69 6f 6e 20 63 6c 6f 73 65 73 2e 0a 20 20  ction closes..  
2fbe7 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
2fbe8 20 72 63 20 3d 20 63 6c 65 61 72 50 65 6e 64 69   rc = clearPendi
2fbe9 6e 67 54 65 72 6d 73 28 76 29 3b 0a 20 20 20 20  ngTerms(v);.    
2fbea 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
2fbeb 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
2fbec 20 20 20 72 63 20 3d 20 73 65 67 64 69 72 5f 64     rc = segdir_d
2fbed 65 6c 65 74 65 5f 61 6c 6c 28 76 29 3b 0a 20 20  elete_all(v);.  
2fbee 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2fbef 20 20 20 20 7d 0a 20 20 7d 20 65 6c 73 65 20 69      }.  } else i
2fbf0 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f( sqlite3_value
2fbf1 5f 74 79 70 65 28 70 70 41 72 67 5b 30 5d 29 20  _type(ppArg[0]) 
2fbf2 21 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29  != SQLITE_NULL )
2fbf3 7b 0a 20 20 20 20 2f 2a 20 41 6e 20 75 70 64 61  {.    /* An upda
2fbf4 74 65 3a 0a 20 20 20 20 20 2a 20 70 70 41 72 67  te:.     * ppArg
2fbf5 5b 30 5d 20 3d 20 6f 6c 64 20 72 6f 77 69 64 0a  [0] = old rowid.
2fbf6 20 20 20 20 20 2a 20 70 70 41 72 67 5b 31 5d 20       * ppArg[1] 
2fbf7 3d 20 6e 65 77 20 72 6f 77 69 64 0a 20 20 20 20  = new rowid.    
2fbf8 20 2a 20 70 70 41 72 67 5b 32 2e 2e 32 2b 76 2d   * ppArg[2..2+v-
2fbf9 3e 6e 43 6f 6c 75 6d 6e 2d 31 5d 20 3d 20 76 61  >nColumn-1] = va
2fbfa 6c 75 65 73 0a 20 20 20 20 20 2a 20 70 70 41 72  lues.     * ppAr
2fbfb 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20  g[2+v->nColumn] 
2fbfc 3d 20 76 61 6c 75 65 20 66 6f 72 20 6d 61 67 69  = value for magi
2fbfd 63 20 63 6f 6c 75 6d 6e 20 28 77 65 20 69 67 6e  c column (we ign
2fbfe 6f 72 65 20 74 68 69 73 29 0a 20 20 20 20 20 2a  ore this).     *
2fbff 20 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c   ppArg[2+v->nCol
2fc00 75 6d 6e 2b 31 5d 20 3d 20 76 61 6c 75 65 20 66  umn+1] = value f
2fc01 6f 72 20 64 6f 63 69 64 0a 20 20 20 20 20 2a 2f  or docid.     */
2fc02 0a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  .    sqlite_int6
2fc03 34 20 72 6f 77 69 64 20 3d 20 73 71 6c 69 74 65  4 rowid = sqlite
2fc04 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 70 70  3_value_int64(pp
2fc05 41 72 67 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28  Arg[0]);.    if(
2fc06 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
2fc07 79 70 65 28 70 70 41 72 67 5b 31 5d 29 20 21 3d  ype(ppArg[1]) !=
2fc08 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20   SQLITE_INTEGER 
2fc09 7c 7c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ||.        sqlit
2fc0a 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 70  e3_value_int64(p
2fc0b 70 41 72 67 5b 31 5d 29 20 21 3d 20 72 6f 77 69  pArg[1]) != rowi
2fc0c 64 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  d ){.      rc = 
2fc0d 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 20 20 2f  SQLITE_ERROR;  /
2fc0e 2a 20 77 65 20 64 6f 6e 27 74 20 61 6c 6c 6f 77  * we don't allow
2fc0f 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 72 6f   changing the ro
2fc10 77 69 64 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65  wid */.    }else
2fc11 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
2fc12 75 65 5f 74 79 70 65 28 70 70 41 72 67 5b 32 2b  ue_type(ppArg[2+
2fc13 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 29 20 21  v->nColumn+1]) !
2fc14 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  = SQLITE_INTEGER
2fc15 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20   ||.            
2fc16 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f    sqlite3_value_
2fc17 69 6e 74 36 34 28 70 70 41 72 67 5b 32 2b 76 2d  int64(ppArg[2+v-
2fc18 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 29 20 21 3d 20  >nColumn+1]) != 
2fc19 72 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 72  rowid ){.      r
2fc1a 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
2fc1b 3b 20 20 2f 2a 20 77 65 20 64 6f 6e 27 74 20 61  ;  /* we don't a
2fc1c 6c 6c 6f 77 20 63 68 61 6e 67 69 6e 67 20 74 68  llow changing th
2fc1d 65 20 64 6f 63 69 64 20 2a 2f 0a 20 20 20 20 7d  e docid */.    }
2fc1e 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65  else{.      asse
2fc1f 72 74 28 20 6e 41 72 67 3d 3d 32 2b 76 2d 3e 6e  rt( nArg==2+v->n
2fc20 43 6f 6c 75 6d 6e 2b 32 29 3b 0a 20 20 20 20 20  Column+2);.     
2fc21 20 72 63 20 3d 20 69 6e 64 65 78 5f 75 70 64 61   rc = index_upda
2fc22 74 65 28 76 2c 20 72 6f 77 69 64 2c 20 26 70 70  te(v, rowid, &pp
2fc23 41 72 67 5b 32 5d 29 3b 0a 20 20 20 20 7d 0a 20  Arg[2]);.    }. 
2fc24 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a   } else {.    /*
2fc25 20 41 6e 20 69 6e 73 65 72 74 3a 0a 20 20 20 20   An insert:.    
2fc26 20 2a 20 70 70 41 72 67 5b 31 5d 20 3d 20 72 65   * ppArg[1] = re
2fc27 71 75 65 73 74 65 64 20 72 6f 77 69 64 0a 20 20  quested rowid.  
2fc28 20 20 20 2a 20 70 70 41 72 67 5b 32 2e 2e 32 2b     * ppArg[2..2+
2fc29 76 2d 3e 6e 43 6f 6c 75 6d 6e 2d 31 5d 20 3d 20  v->nColumn-1] = 
2fc2a 76 61 6c 75 65 73 0a 20 20 20 20 20 2a 20 70 70  values.     * pp
2fc2b 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e  Arg[2+v->nColumn
2fc2c 5d 20 3d 20 76 61 6c 75 65 20 66 6f 72 20 6d 61  ] = value for ma
2fc2d 67 69 63 20 63 6f 6c 75 6d 6e 20 28 77 65 20 69  gic column (we i
2fc2e 67 6e 6f 72 65 20 74 68 69 73 29 0a 20 20 20 20  gnore this).    
2fc2f 20 2a 20 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43   * ppArg[2+v->nC
2fc30 6f 6c 75 6d 6e 2b 31 5d 20 3d 20 76 61 6c 75 65  olumn+1] = value
2fc31 20 66 6f 72 20 64 6f 63 69 64 0a 20 20 20 20 20   for docid.     
2fc32 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  */.    sqlite3_v
2fc33 61 6c 75 65 20 2a 70 52 65 71 75 65 73 74 44 6f  alue *pRequestDo
2fc34 63 69 64 20 3d 20 70 70 41 72 67 5b 32 2b 76 2d  cid = ppArg[2+v-
2fc35 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 3b 0a 20 20 20  >nColumn+1];.   
2fc36 20 61 73 73 65 72 74 28 20 6e 41 72 67 3d 3d 32   assert( nArg==2
2fc37 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32 29 3b 0a  +v->nColumn+2);.
2fc38 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e      if( SQLITE_N
2fc39 55 4c 4c 20 21 3d 20 73 71 6c 69 74 65 33 5f 76  ULL != sqlite3_v
2fc3a 61 6c 75 65 5f 74 79 70 65 28 70 52 65 71 75 65  alue_type(pReque
2fc3b 73 74 44 6f 63 69 64 29 20 26 26 0a 20 20 20 20  stDocid) &&.    
2fc3c 20 20 20 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20      SQLITE_NULL 
2fc3d 21 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  != sqlite3_value
2fc3e 5f 74 79 70 65 28 70 70 41 72 67 5b 31 5d 29 20  _type(ppArg[1]) 
2fc3f 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f  ){.      /* TODO
2fc40 28 73 68 65 73 73 29 20 43 6f 6e 73 69 64 65 72  (shess) Consider
2fc41 20 61 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 74   allowing this t
2fc42 6f 20 77 6f 72 6b 20 69 66 20 74 68 65 20 76 61  o work if the va
2fc43 6c 75 65 73 20 61 72 65 0a 20 20 20 20 20 20 2a  lues are.      *
2fc44 2a 20 69 64 65 6e 74 69 63 61 6c 2e 20 20 49 27  * identical.  I'
2fc45 6d 20 69 6e 63 6c 69 6e 65 64 20 74 6f 20 64 69  m inclined to di
2fc46 73 63 6f 75 72 61 67 65 20 74 68 61 74 20 75 73  scourage that us
2fc47 61 67 65 2c 20 74 68 6f 75 67 68 2c 0a 20 20 20  age, though,.   
2fc48 20 20 20 2a 2a 20 67 69 76 65 6e 20 74 68 61 74     ** given that
2fc49 20 62 6f 74 68 20 72 6f 77 69 64 20 61 6e 64 20   both rowid and 
2fc4a 64 6f 63 69 64 20 61 72 65 20 73 70 65 63 69 61  docid are specia
2fc4b 6c 20 63 6f 6c 75 6d 6e 73 2e 20 20 42 65 74 74  l columns.  Bett
2fc4c 65 72 0a 20 20 20 20 20 20 2a 2a 20 77 6f 75 6c  er.      ** woul
2fc4d 64 20 62 65 20 74 6f 20 64 65 66 69 6e 65 20 6f  d be to define o
2fc4e 6e 65 20 6f 72 20 74 68 65 20 6f 74 68 65 72 20  ne or the other 
2fc4f 61 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 77  as the default w
2fc50 69 6e 6e 65 72 2c 0a 20 20 20 20 20 20 2a 2a 20  inner,.      ** 
2fc51 62 75 74 20 73 68 6f 75 6c 64 20 69 74 20 62 65  but should it be
2fc52 20 66 74 73 33 2d 63 65 6e 74 72 69 63 20 28 64   fts3-centric (d
2fc53 6f 63 69 64 29 20 6f 72 20 53 51 4c 69 74 65 2d  ocid) or SQLite-
2fc54 63 65 6e 74 72 69 63 0a 20 20 20 20 20 20 2a 2a  centric.      **
2fc55 20 28 72 6f 77 69 64 29 3f 0a 20 20 20 20 20 20   (rowid)?.      
2fc56 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  */.      rc = SQ
2fc57 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
2fc58 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28  }else{.      if(
2fc59 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 3d 3d 20   SQLITE_NULL == 
2fc5a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
2fc5b 70 65 28 70 52 65 71 75 65 73 74 44 6f 63 69 64  pe(pRequestDocid
2fc5c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 52 65  ) ){.        pRe
2fc5d 71 75 65 73 74 44 6f 63 69 64 20 3d 20 70 70 41  questDocid = ppA
2fc5e 72 67 5b 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20  rg[1];.      }. 
2fc5f 20 20 20 20 20 72 63 20 3d 20 69 6e 64 65 78 5f       rc = index_
2fc60 69 6e 73 65 72 74 28 76 2c 20 70 52 65 71 75 65  insert(v, pReque
2fc61 73 74 44 6f 63 69 64 2c 20 26 70 70 41 72 67 5b  stDocid, &ppArg[
2fc62 32 5d 2c 20 70 52 6f 77 69 64 29 3b 0a 20 20 20  2], pRowid);.   
2fc63 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
2fc64 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69   rc;.}..static i
2fc65 6e 74 20 66 75 6c 6c 74 65 78 74 53 79 6e 63 28  nt fulltextSync(
2fc66 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
2fc67 74 61 62 29 7b 0a 20 20 46 54 53 54 52 41 43 45  tab){.  FTSTRACE
2fc68 28 28 22 46 54 53 33 20 78 53 79 6e 63 28 29 5c  (("FTS3 xSync()\
2fc69 6e 22 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 66  n"));.  return f
2fc6a 6c 75 73 68 50 65 6e 64 69 6e 67 54 65 72 6d 73  lushPendingTerms
2fc6b 28 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  ((fulltext_vtab 
2fc6c 2a 29 70 56 74 61 62 29 3b 0a 7d 0a 0a 73 74 61  *)pVtab);.}..sta
2fc6d 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74  tic int fulltext
2fc6e 42 65 67 69 6e 28 73 71 6c 69 74 65 33 5f 76 74  Begin(sqlite3_vt
2fc6f 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 66 75  ab *pVtab){.  fu
2fc70 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d  lltext_vtab *v =
2fc71 20 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20   (fulltext_vtab 
2fc72 2a 29 20 70 56 74 61 62 3b 0a 20 20 46 54 53 54  *) pVtab;.  FTST
2fc73 52 41 43 45 28 28 22 46 54 53 33 20 78 42 65 67  RACE(("FTS3 xBeg
2fc74 69 6e 28 29 5c 6e 22 29 29 3b 0a 0a 20 20 2f 2a  in()\n"));..  /*
2fc75 20 41 6e 79 20 62 75 66 66 65 72 65 64 20 75 70   Any buffered up
2fc76 64 61 74 65 73 20 73 68 6f 75 6c 64 20 68 61 76  dates should hav
2fc77 65 20 62 65 65 6e 20 63 6c 65 61 72 65 64 20 62  e been cleared b
2fc78 79 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a 20  y the previous. 
2fc79 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e   ** transaction.
2fc7a 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
2fc7b 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c  v->nPendingData<
2fc7c 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 6c  0 );.  return cl
2fc7d 65 61 72 50 65 6e 64 69 6e 67 54 65 72 6d 73 28  earPendingTerms(
2fc7e 76 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  v);.}..static in
2fc7f 74 20 66 75 6c 6c 74 65 78 74 43 6f 6d 6d 69 74  t fulltextCommit
2fc80 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
2fc81 56 74 61 62 29 7b 0a 20 20 66 75 6c 6c 74 65 78  Vtab){.  fulltex
2fc82 74 5f 76 74 61 62 20 2a 76 20 3d 20 28 66 75 6c  t_vtab *v = (ful
2fc83 6c 74 65 78 74 5f 76 74 61 62 20 2a 29 20 70 56  ltext_vtab *) pV
2fc84 74 61 62 3b 0a 20 20 46 54 53 54 52 41 43 45 28  tab;.  FTSTRACE(
2fc85 28 22 46 54 53 33 20 78 43 6f 6d 6d 69 74 28 29  ("FTS3 xCommit()
2fc86 5c 6e 22 29 29 3b 0a 0a 20 20 2f 2a 20 42 75 66  \n"));..  /* Buf
2fc87 66 65 72 65 64 20 75 70 64 61 74 65 73 20 73 68  fered updates sh
2fc88 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 63  ould have been c
2fc89 6c 65 61 72 65 64 20 62 79 20 66 75 6c 6c 74 65  leared by fullte
2fc8a 78 74 53 79 6e 63 28 29 2e 20 2a 2f 0a 20 20 61  xtSync(). */.  a
2fc8b 73 73 65 72 74 28 20 76 2d 3e 6e 50 65 6e 64 69  ssert( v->nPendi
2fc8c 6e 67 44 61 74 61 3c 30 20 29 3b 0a 20 20 72 65  ngData<0 );.  re
2fc8d 74 75 72 6e 20 63 6c 65 61 72 50 65 6e 64 69 6e  turn clearPendin
2fc8e 67 54 65 72 6d 73 28 76 29 3b 0a 7d 0a 0a 73 74  gTerms(v);.}..st
2fc8f 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78  atic int fulltex
2fc90 74 52 6f 6c 6c 62 61 63 6b 28 73 71 6c 69 74 65  tRollback(sqlite
2fc91 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a  3_vtab *pVtab){.
2fc92 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53    FTSTRACE(("FTS
2fc93 33 20 78 52 6f 6c 6c 62 61 63 6b 28 29 5c 6e 22  3 xRollback()\n"
2fc94 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 6c 65  ));.  return cle
2fc95 61 72 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 28  arPendingTerms((
2fc96 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29  fulltext_vtab *)
2fc97 70 56 74 61 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pVtab);.}../*.**
2fc98 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
2fc99 6f 66 20 74 68 65 20 73 6e 69 70 70 65 74 28 29  of the snippet()
2fc9a 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 46 54   function for FT
2fc9b 53 33 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  S3.*/.static voi
2fc9c 64 20 73 6e 69 70 70 65 74 46 75 6e 63 28 0a 20  d snippetFunc(. 
2fc9d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
2fc9e 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20 20 69 6e   *pContext,.  in
2fc9f 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
2fca0 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
2fca1 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72  {.  fulltext_cur
2fca2 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20  sor *pCursor;.  
2fca3 69 66 28 20 61 72 67 63 3c 31 20 29 20 72 65 74  if( argc<1 ) ret
2fca4 75 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  urn;.  if( sqlit
2fca5 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
2fca6 67 76 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42  gv[0])!=SQLITE_B
2fca7 4c 4f 42 20 7c 7c 0a 20 20 20 20 20 20 73 71 6c  LOB ||.      sql
2fca8 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
2fca9 28 61 72 67 76 5b 30 5d 29 21 3d 73 69 7a 65 6f  (argv[0])!=sizeo
2fcaa 66 28 70 43 75 72 73 6f 72 29 20 29 7b 0a 20 20  f(pCursor) ){.  
2fcab 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
2fcac 5f 65 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c  _error(pContext,
2fcad 20 22 69 6c 6c 65 67 61 6c 20 66 69 72 73 74 20   "illegal first 
2fcae 61 72 67 75 6d 65 6e 74 20 74 6f 20 68 74 6d 6c  argument to html
2fcaf 5f 73 6e 69 70 70 65 74 22 2c 2d 31 29 3b 0a 20  _snippet",-1);. 
2fcb0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73   }else{.    cons
2fcb1 74 20 63 68 61 72 20 2a 7a 53 74 61 72 74 20 3d  t char *zStart =
2fcb2 20 22 3c 62 3e 22 3b 0a 20 20 20 20 63 6f 6e 73   "<b>";.    cons
2fcb3 74 20 63 68 61 72 20 2a 7a 45 6e 64 20 3d 20 22  t char *zEnd = "
2fcb4 3c 2f 62 3e 22 3b 0a 20 20 20 20 63 6f 6e 73 74  </b>";.    const
2fcb5 20 63 68 61 72 20 2a 7a 45 6c 6c 69 70 73 69 73   char *zEllipsis
2fcb6 20 3d 20 22 3c 62 3e 2e 2e 2e 3c 2f 62 3e 22 3b   = "<b>...</b>";
2fcb7 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 43 75  .    memcpy(&pCu
2fcb8 72 73 6f 72 2c 20 73 71 6c 69 74 65 33 5f 76 61  rsor, sqlite3_va
2fcb9 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d  lue_blob(argv[0]
2fcba 29 2c 20 73 69 7a 65 6f 66 28 70 43 75 72 73 6f  ), sizeof(pCurso
2fcbb 72 29 29 3b 0a 20 20 20 20 69 66 28 20 61 72 67  r));.    if( arg
2fcbc 63 3e 3d 32 20 29 7b 0a 20 20 20 20 20 20 7a 53  c>=2 ){.      zS
2fcbd 74 61 72 74 20 3d 20 28 63 6f 6e 73 74 20 63 68  tart = (const ch
2fcbe 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar*)sqlite3_valu
2fcbf 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b  e_text(argv[1]);
2fcc0 0a 20 20 20 20 20 20 69 66 28 20 61 72 67 63 3e  .      if( argc>
2fcc1 3d 33 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 45  =3 ){.        zE
2fcc2 6e 64 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  nd = (const char
2fcc3 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
2fcc4 74 65 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20  text(argv[2]);. 
2fcc5 20 20 20 20 20 20 20 69 66 28 20 61 72 67 63 3e         if( argc>
2fcc6 3d 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =4 ){.          
2fcc7 7a 45 6c 6c 69 70 73 69 73 20 3d 20 28 63 6f 6e  zEllipsis = (con
2fcc8 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33  st char*)sqlite3
2fcc9 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
2fcca 5b 33 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  [3]);.        }.
2fccb 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2fccc 20 20 73 6e 69 70 70 65 74 41 6c 6c 4f 66 66 73    snippetAllOffs
2fccd 65 74 73 28 70 43 75 72 73 6f 72 29 3b 0a 20 20  ets(pCursor);.  
2fcce 20 20 73 6e 69 70 70 65 74 54 65 78 74 28 70 43    snippetText(pC
2fccf 75 72 73 6f 72 2c 20 7a 53 74 61 72 74 2c 20 7a  ursor, zStart, z
2fcd0 45 6e 64 2c 20 7a 45 6c 6c 69 70 73 69 73 29 3b  End, zEllipsis);
2fcd1 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
2fcd2 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74 65 78  ult_text(pContex
2fcd3 74 2c 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70  t, pCursor->snip
2fcd4 70 65 74 2e 7a 53 6e 69 70 70 65 74 2c 0a 20 20  pet.zSnippet,.  
2fcd5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fcd6 20 20 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 73        pCursor->s
2fcd7 6e 69 70 70 65 74 2e 6e 53 6e 69 70 70 65 74 2c  nippet.nSnippet,
2fcd8 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
2fcd9 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d  .  }.}../*.** Im
2fcda 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
2fcdb 74 68 65 20 6f 66 66 73 65 74 73 28 29 20 66 75  the offsets() fu
2fcdc 6e 63 74 69 6f 6e 20 66 6f 72 20 46 54 53 33 0a  nction for FTS3.
2fcdd 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
2fcde 6e 69 70 70 65 74 4f 66 66 73 65 74 73 46 75 6e  nippetOffsetsFun
2fcdf 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
2fce0 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a  text *pContext,.
2fce1 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
2fce2 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
2fce3 67 76 0a 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74  gv.){.  fulltext
2fce4 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
2fce5 3b 0a 20 20 69 66 28 20 61 72 67 63 3c 31 20 29  ;.  if( argc<1 )
2fce6 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73   return;.  if( s
2fce7 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
2fce8 65 28 61 72 67 76 5b 30 5d 29 21 3d 53 51 4c 49  e(argv[0])!=SQLI
2fce9 54 45 5f 42 4c 4f 42 20 7c 7c 0a 20 20 20 20 20  TE_BLOB ||.     
2fcea 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
2fceb 79 74 65 73 28 61 72 67 76 5b 30 5d 29 21 3d 73  ytes(argv[0])!=s
2fcec 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 20 29  izeof(pCursor) )
2fced 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
2fcee 73 75 6c 74 5f 65 72 72 6f 72 28 70 43 6f 6e 74  sult_error(pCont
2fcef 65 78 74 2c 20 22 69 6c 6c 65 67 61 6c 20 66 69  ext, "illegal fi
2fcf0 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
2fcf1 6f 66 66 73 65 74 73 22 2c 2d 31 29 3b 0a 20 20  offsets",-1);.  
2fcf2 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 63 70  }else{.    memcp
2fcf3 79 28 26 70 43 75 72 73 6f 72 2c 20 73 71 6c 69  y(&pCursor, sqli
2fcf4 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61  te3_value_blob(a
2fcf5 72 67 76 5b 30 5d 29 2c 20 73 69 7a 65 6f 66 28  rgv[0]), sizeof(
2fcf6 70 43 75 72 73 6f 72 29 29 3b 0a 20 20 20 20 73  pCursor));.    s
2fcf7 6e 69 70 70 65 74 41 6c 6c 4f 66 66 73 65 74 73  nippetAllOffsets
2fcf8 28 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 73  (pCursor);.    s
2fcf9 6e 69 70 70 65 74 4f 66 66 73 65 74 54 65 78 74  nippetOffsetText
2fcfa 28 26 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70  (&pCursor->snipp
2fcfb 65 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  et);.    sqlite3
2fcfc 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43 6f  _result_text(pCo
2fcfd 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20  ntext,.         
2fcfe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
2fcff 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e  Cursor->snippet.
2fd00 7a 4f 66 66 73 65 74 2c 20 70 43 75 72 73 6f 72  zOffset, pCursor
2fd01 2d 3e 73 6e 69 70 70 65 74 2e 6e 4f 66 66 73 65  ->snippet.nOffse
2fd02 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
2fd03 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
2fd04 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 7d  E_STATIC);.  }.}
2fd05 0a 0a 2f 2a 20 4f 70 74 4c 65 61 76 65 73 52 65  ../* OptLeavesRe
2fd06 61 64 65 72 20 69 73 20 6e 65 61 72 6c 79 20 69  ader is nearly i
2fd07 64 65 6e 74 69 63 61 6c 20 74 6f 20 4c 65 61 76  dentical to Leav
2fd08 65 73 52 65 61 64 65 72 2c 20 65 78 63 65 70 74  esReader, except
2fd09 20 74 68 61 74 0a 2a 2a 20 77 68 65 72 65 20 4c   that.** where L
2fd0a 65 61 76 65 73 52 65 61 64 65 72 20 69 73 20 67  eavesReader is g
2fd0b 65 61 72 65 64 20 74 6f 77 61 72 64 73 20 74 68  eared towards th
2fd0c 65 20 6d 65 72 67 69 6e 67 20 6f 66 20 63 6f 6d  e merging of com
2fd0d 70 6c 65 74 65 0a 2a 2a 20 73 65 67 6d 65 6e 74  plete.** segment
2fd0e 20 6c 65 76 65 6c 73 20 28 77 69 74 68 20 65 78   levels (with ex
2fd0f 61 63 74 6c 79 20 4d 45 52 47 45 5f 43 4f 55 4e  actly MERGE_COUN
2fd10 54 20 73 65 67 6d 65 6e 74 73 29 2c 20 4f 70 74  T segments), Opt
2fd11 4c 65 61 76 65 73 52 65 61 64 65 72 0a 2a 2a 20  LeavesReader.** 
2fd12 69 73 20 67 65 61 72 65 64 20 74 6f 77 61 72 64  is geared toward
2fd13 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  s implementation
2fd14 20 6f 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 65   of the optimize
2fd15 28 29 20 66 75 6e 63 74 69 6f 6e 2c 20 61 6e 64  () function, and
2fd16 0a 2a 2a 20 63 61 6e 20 6d 65 72 67 65 20 61 6c  .** can merge al
2fd17 6c 20 73 65 67 6d 65 6e 74 73 20 73 69 6d 75 6c  l segments simul
2fd18 74 61 6e 65 6f 75 73 6c 79 2e 20 20 54 68 69 73  taneously.  This
2fd19 20 76 65 72 73 69 6f 6e 20 6d 61 79 20 62 65 0a   version may be.
2fd1a 2a 2a 20 73 6f 6d 65 77 68 61 74 20 6c 65 73 73  ** somewhat less
2fd1b 20 65 66 66 69 63 69 65 6e 74 20 74 68 61 6e 20   efficient than 
2fd1c 4c 65 61 76 65 73 52 65 61 64 65 72 20 62 65 63  LeavesReader bec
2fd1d 61 75 73 65 20 69 74 20 6d 65 72 67 65 73 20 69  ause it merges i
2fd1e 6e 74 6f 20 61 6e 0a 2a 2a 20 61 63 63 75 6d 75  nto an.** accumu
2fd1f 6c 61 74 6f 72 20 72 61 74 68 65 72 20 74 68 61  lator rather tha
2fd20 6e 20 64 6f 69 6e 67 20 61 6e 20 4e 2d 77 61 79  n doing an N-way
2fd21 20 6d 65 72 67 65 2c 20 62 75 74 20 73 69 6e 63   merge, but sinc
2fd22 65 20 73 65 67 6d 65 6e 74 0a 2a 2a 20 73 69 7a  e segment.** siz
2fd23 65 20 67 72 6f 77 73 20 65 78 70 6f 6e 65 6e 74  e grows exponent
2fd24 69 61 6c 6c 79 20 28 73 6f 20 73 65 67 6d 65 6e  ially (so segmen
2fd25 74 20 63 6f 75 6e 74 20 6c 6f 67 72 69 74 68 6d  t count logrithm
2fd26 69 63 61 6c 6c 79 29 20 74 68 69 73 20 69 73 0a  ically) this is.
2fd27 2a 2a 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74 20  ** probably not 
2fd28 61 6e 20 69 6d 6d 65 64 69 61 74 65 20 70 72 6f  an immediate pro
2fd29 62 6c 65 6d 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f  blem..*/./* TODO
2fd2a 28 73 68 65 73 73 29 3a 20 50 72 6f 76 65 20 74  (shess): Prove t
2fd2b 68 61 74 20 61 73 73 65 72 74 69 6f 6e 2c 20 6f  hat assertion, o
2fd2c 72 20 65 78 74 65 6e 64 20 74 68 65 20 6d 65 72  r extend the mer
2fd2d 67 65 20 63 6f 64 65 20 74 6f 0a 2a 2a 20 6d 65  ge code to.** me
2fd2e 72 67 65 20 74 72 65 65 20 66 61 73 68 69 6f 6e  rge tree fashion
2fd2f 20 28 6c 69 6b 65 20 74 68 65 20 70 72 65 66 69   (like the prefi
2fd30 78 2d 73 65 61 72 63 68 69 6e 67 20 63 6f 64 65  x-searching code
2fd31 20 64 6f 65 73 29 2e 0a 2a 2f 0a 2f 2a 20 54 4f   does)..*/./* TO
2fd32 44 4f 28 73 68 65 73 73 29 3a 20 4f 70 74 4c 65  DO(shess): OptLe
2fd33 61 76 65 73 52 65 61 64 65 72 20 61 6e 64 20 4c  avesReader and L
2fd34 65 61 76 65 73 52 65 61 64 65 72 20 63 6f 75 6c  eavesReader coul
2fd35 64 20 70 72 6f 62 61 62 6c 79 20 62 65 0a 2a 2a  d probably be.**
2fd36 20 6d 65 72 67 65 64 20 77 69 74 68 20 6c 69 74   merged with lit
2fd37 74 6c 65 20 6f 72 20 6e 6f 20 6c 6f 73 73 20 6f  tle or no loss o
2fd38 66 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 66 6f  f performance fo
2fd39 72 20 4c 65 61 76 65 73 52 65 61 64 65 72 2e 20  r LeavesReader. 
2fd3a 20 54 68 65 0a 2a 2a 20 6d 65 72 67 65 64 20 63   The.** merged c
2fd3b 6f 64 65 20 77 6f 75 6c 64 20 6e 65 65 64 20 74  ode would need t
2fd3c 6f 20 68 61 6e 64 6c 65 20 3e 4d 45 52 47 45 5f  o handle >MERGE_
2fd3d 43 4f 55 4e 54 20 73 65 67 6d 65 6e 74 73 2c 20  COUNT segments, 
2fd3e 61 6e 64 20 77 6f 75 6c 64 0a 2a 2a 20 61 6c 73  and would.** als
2fd3f 6f 20 6e 65 65 64 20 74 6f 20 62 65 20 61 62 6c  o need to be abl
2fd40 65 20 74 6f 20 6f 70 74 69 6f 6e 61 6c 6c 79 20  e to optionally 
2fd41 6f 70 74 69 6d 69 7a 65 20 61 77 61 79 20 64 65  optimize away de
2fd42 6c 65 74 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65  letes..*/.typede
2fd43 66 20 73 74 72 75 63 74 20 4f 70 74 4c 65 61 76  f struct OptLeav
2fd44 65 73 52 65 61 64 65 72 20 7b 0a 20 20 2f 2a 20  esReader {.  /* 
2fd45 53 65 67 6d 65 6e 74 20 6e 75 6d 62 65 72 2c 20  Segment number, 
2fd46 74 6f 20 6f 72 64 65 72 20 72 65 61 64 65 72 73  to order readers
2fd47 20 62 79 20 61 67 65 2e 20 2a 2f 0a 20 20 69 6e   by age. */.  in
2fd48 74 20 73 65 67 6d 65 6e 74 3b 0a 20 20 4c 65 61  t segment;.  Lea
2fd49 76 65 73 52 65 61 64 65 72 20 72 65 61 64 65 72  vesReader reader
2fd4a 3b 0a 7d 20 4f 70 74 4c 65 61 76 65 73 52 65 61  ;.} OptLeavesRea
2fd4b 64 65 72 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74  der;..static int
2fd4c 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   optLeavesReader
2fd4d 41 74 45 6e 64 28 4f 70 74 4c 65 61 76 65 73 52  AtEnd(OptLeavesR
2fd4e 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
2fd4f 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 76 65 73  .  return leaves
2fd50 52 65 61 64 65 72 41 74 45 6e 64 28 26 70 52 65  ReaderAtEnd(&pRe
2fd51 61 64 65 72 2d 3e 72 65 61 64 65 72 29 3b 0a 7d  ader->reader);.}
2fd52 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 74 4c  .static int optL
2fd53 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 42  eavesReaderTermB
2fd54 79 74 65 73 28 4f 70 74 4c 65 61 76 65 73 52 65  ytes(OptLeavesRe
2fd55 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
2fd56 20 20 72 65 74 75 72 6e 20 6c 65 61 76 65 73 52    return leavesR
2fd57 65 61 64 65 72 54 65 72 6d 42 79 74 65 73 28 26  eaderTermBytes(&
2fd58 70 52 65 61 64 65 72 2d 3e 72 65 61 64 65 72 29  pReader->reader)
2fd59 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.}.static const
2fd5a 20 63 68 61 72 20 2a 6f 70 74 4c 65 61 76 65 73   char *optLeaves
2fd5b 52 65 61 64 65 72 44 61 74 61 28 4f 70 74 4c 65  ReaderData(OptLe
2fd5c 61 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61  avesReader *pRea
2fd5d 64 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c  der){.  return l
2fd5e 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61 28  eavesReaderData(
2fd5f 26 70 52 65 61 64 65 72 2d 3e 72 65 61 64 65 72  &pReader->reader
2fd60 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  );.}.static int 
2fd61 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 44  optLeavesReaderD
2fd62 61 74 61 42 79 74 65 73 28 4f 70 74 4c 65 61 76  ataBytes(OptLeav
2fd63 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  esReader *pReade
2fd64 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61  r){.  return lea
2fd65 76 65 73 52 65 61 64 65 72 44 61 74 61 42 79 74  vesReaderDataByt
2fd66 65 73 28 26 70 52 65 61 64 65 72 2d 3e 72 65 61  es(&pReader->rea
2fd67 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 63  der);.}.static c
2fd68 6f 6e 73 74 20 63 68 61 72 20 2a 6f 70 74 4c 65  onst char *optLe
2fd69 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 28 4f  avesReaderTerm(O
2fd6a 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  ptLeavesReader *
2fd6b 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75  pReader){.  retu
2fd6c 72 6e 20 6c 65 61 76 65 73 52 65 61 64 65 72 54  rn leavesReaderT
2fd6d 65 72 6d 28 26 70 52 65 61 64 65 72 2d 3e 72 65  erm(&pReader->re
2fd6e 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20  ader);.}.static 
2fd6f 69 6e 74 20 6f 70 74 4c 65 61 76 65 73 52 65 61  int optLeavesRea
2fd70 64 65 72 53 74 65 70 28 66 75 6c 6c 74 65 78 74  derStep(fulltext
2fd71 5f 76 74 61 62 20 2a 76 2c 20 4f 70 74 4c 65 61  _vtab *v, OptLea
2fd72 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64  vesReader *pRead
2fd73 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c 65  er){.  return le
2fd74 61 76 65 73 52 65 61 64 65 72 53 74 65 70 28 76  avesReaderStep(v
2fd75 2c 20 26 70 52 65 61 64 65 72 2d 3e 72 65 61 64  , &pReader->read
2fd76 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e  er);.}.static in
2fd77 74 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65  t optLeavesReade
2fd78 72 54 65 72 6d 43 6d 70 28 4f 70 74 4c 65 61 76  rTermCmp(OptLeav
2fd79 65 73 52 65 61 64 65 72 20 2a 6c 72 31 2c 20 4f  esReader *lr1, O
2fd7a 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  ptLeavesReader *
2fd7b 6c 72 32 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c  lr2){.  return l
2fd7c 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 43  eavesReaderTermC
2fd7d 6d 70 28 26 6c 72 31 2d 3e 72 65 61 64 65 72 2c  mp(&lr1->reader,
2fd7e 20 26 6c 72 32 2d 3e 72 65 61 64 65 72 29 3b 0a   &lr2->reader);.
2fd7f 7d 0a 2f 2a 20 4f 72 64 65 72 20 62 79 20 74 65  }./* Order by te
2fd80 72 6d 20 61 73 63 65 6e 64 69 6e 67 2c 20 73 65  rm ascending, se
2fd81 67 6d 65 6e 74 20 61 73 63 65 6e 64 69 6e 67 20  gment ascending 
2fd82 28 6f 6c 64 65 73 74 20 74 6f 20 6e 65 77 65 73  (oldest to newes
2fd83 74 29 2c 20 77 69 74 68 0a 2a 2a 20 65 78 68 61  t), with.** exha
2fd84 75 73 74 65 64 20 72 65 61 64 65 72 73 20 74 6f  usted readers to
2fd85 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61   the end..*/.sta
2fd86 74 69 63 20 69 6e 74 20 6f 70 74 4c 65 61 76 65  tic int optLeave
2fd87 73 52 65 61 64 65 72 43 6d 70 28 4f 70 74 4c 65  sReaderCmp(OptLe
2fd88 61 76 65 73 52 65 61 64 65 72 20 2a 6c 72 31 2c  avesReader *lr1,
2fd89 20 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   OptLeavesReader
2fd8a 20 2a 6c 72 32 29 7b 0a 20 20 69 6e 74 20 63 20   *lr2){.  int c 
2fd8b 3d 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65  = optLeavesReade
2fd8c 72 54 65 72 6d 43 6d 70 28 6c 72 31 2c 20 6c 72  rTermCmp(lr1, lr
2fd8d 32 29 3b 0a 20 20 69 66 28 20 63 21 3d 30 20 29  2);.  if( c!=0 )
2fd8e 20 72 65 74 75 72 6e 20 63 3b 0a 20 20 72 65 74   return c;.  ret
2fd8f 75 72 6e 20 6c 72 31 2d 3e 73 65 67 6d 65 6e 74  urn lr1->segment
2fd90 2d 6c 72 32 2d 3e 73 65 67 6d 65 6e 74 3b 0a 7d  -lr2->segment;.}
2fd91 0a 2f 2a 20 42 75 62 62 6c 65 20 70 4c 72 5b 30  ./* Bubble pLr[0
2fd92 5d 20 74 6f 20 61 70 70 72 6f 70 72 69 61 74 65  ] to appropriate
2fd93 20 70 6c 61 63 65 20 69 6e 20 70 4c 72 5b 31 2e   place in pLr[1.
2fd94 2e 6e 4c 72 2d 31 5d 2e 20 20 41 73 73 75 6d 65  .nLr-1].  Assume
2fd95 73 20 74 68 61 74 0a 2a 2a 20 70 4c 72 5b 31 2e  s that.** pLr[1.
2fd96 2e 6e 4c 72 2d 31 5d 20 69 73 20 61 6c 72 65 61  .nLr-1] is alrea
2fd97 64 79 20 73 6f 72 74 65 64 2e 0a 2a 2f 0a 73 74  dy sorted..*/.st
2fd98 61 74 69 63 20 76 6f 69 64 20 6f 70 74 4c 65 61  atic void optLea
2fd99 76 65 73 52 65 61 64 65 72 52 65 6f 72 64 65 72  vesReaderReorder
2fd9a 28 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72  (OptLeavesReader
2fd9b 20 2a 70 4c 72 2c 20 69 6e 74 20 6e 4c 72 29 7b   *pLr, int nLr){
2fd9c 0a 20 20 77 68 69 6c 65 28 20 6e 4c 72 3e 31 20  .  while( nLr>1 
2fd9d 26 26 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64  && optLeavesRead
2fd9e 65 72 43 6d 70 28 70 4c 72 2c 20 70 4c 72 2b 31  erCmp(pLr, pLr+1
2fd9f 29 3e 30 20 29 7b 0a 20 20 20 20 4f 70 74 4c 65  )>0 ){.    OptLe
2fda0 61 76 65 73 52 65 61 64 65 72 20 74 6d 70 20 3d  avesReader tmp =
2fda1 20 70 4c 72 5b 30 5d 3b 0a 20 20 20 20 70 4c 72   pLr[0];.    pLr
2fda2 5b 30 5d 20 3d 20 70 4c 72 5b 31 5d 3b 0a 20 20  [0] = pLr[1];.  
2fda3 20 20 70 4c 72 5b 31 5d 20 3d 20 74 6d 70 3b 0a    pLr[1] = tmp;.
2fda4 20 20 20 20 6e 4c 72 2d 2d 3b 0a 20 20 20 20 70      nLr--;.    p
2fda5 4c 72 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20  Lr++;.  }.}../* 
2fda6 6f 70 74 69 6d 69 7a 65 28 29 20 68 65 6c 70 65  optimize() helpe
2fda7 72 20 66 75 6e 63 74 69 6f 6e 2e 20 20 50 75 74  r function.  Put
2fda8 20 74 68 65 20 72 65 61 64 65 72 73 20 69 6e 20   the readers in 
2fda9 6f 72 64 65 72 20 61 6e 64 20 69 74 65 72 61 74  order and iterat
2fdaa 65 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65  e.** through the
2fdab 6d 2c 20 6d 65 72 67 69 6e 67 20 64 6f 63 6c 69  m, merging docli
2fdac 73 74 73 20 66 6f 72 20 6d 61 74 63 68 69 6e 67  sts for matching
2fdad 20 74 65 72 6d 73 20 69 6e 74 6f 20 70 57 72 69   terms into pWri
2fdae 74 65 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 73 20  ter..** Returns 
2fdaf 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63  SQLITE_OK on suc
2fdb0 63 65 73 73 2c 20 6f 72 20 74 68 65 20 53 51 4c  cess, or the SQL
2fdb1 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 77  ite error code w
2fdb2 68 69 63 68 0a 2a 2a 20 70 72 65 76 65 6e 74 65  hich.** prevente
2fdb3 64 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 74  d success..*/.st
2fdb4 61 74 69 63 20 69 6e 74 20 6f 70 74 69 6d 69 7a  atic int optimiz
2fdb5 65 49 6e 74 65 72 6e 61 6c 28 66 75 6c 6c 74 65  eInternal(fullte
2fdb6 78 74 5f 76 74 61 62 20 2a 76 2c 0a 20 20 20 20  xt_vtab *v,.    
2fdb7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fdb8 20 20 20 20 20 20 20 20 4f 70 74 4c 65 61 76 65          OptLeave
2fdb9 73 52 65 61 64 65 72 20 2a 72 65 61 64 65 72 73  sReader *readers
2fdba 2c 20 69 6e 74 20 6e 52 65 61 64 65 72 73 2c 0a  , int nReaders,.
2fdbb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fdbc 20 20 20 20 20 20 20 20 20 20 20 20 4c 65 61 66              Leaf
2fdbd 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 29  Writer *pWriter)
2fdbe 7b 0a 20 20 69 6e 74 20 69 2c 20 72 63 20 3d 20  {.  int i, rc = 
2fdbf 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 44 61 74  SQLITE_OK;.  Dat
2fdc0 61 42 75 66 66 65 72 20 64 6f 63 6c 69 73 74 2c  aBuffer doclist,
2fdc1 20 6d 65 72 67 65 64 2c 20 74 6d 70 3b 0a 0a 20   merged, tmp;.. 
2fdc2 20 2f 2a 20 4f 72 64 65 72 20 74 68 65 20 72 65   /* Order the re
2fdc3 61 64 65 72 73 2e 20 2a 2f 0a 20 20 69 20 3d 20  aders. */.  i = 
2fdc4 6e 52 65 61 64 65 72 73 3b 0a 20 20 77 68 69 6c  nReaders;.  whil
2fdc5 65 28 20 69 2d 2d 20 3e 20 30 20 29 7b 0a 20 20  e( i-- > 0 ){.  
2fdc6 20 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65    optLeavesReade
2fdc7 72 52 65 6f 72 64 65 72 28 26 72 65 61 64 65 72  rReorder(&reader
2fdc8 73 5b 69 5d 2c 20 6e 52 65 61 64 65 72 73 2d 69  s[i], nReaders-i
2fdc9 29 3b 0a 20 20 7d 0a 0a 20 20 64 61 74 61 42 75  );.  }..  dataBu
2fdca 66 66 65 72 49 6e 69 74 28 26 64 6f 63 6c 69 73  fferInit(&doclis
2fdcb 74 2c 20 4c 45 41 46 5f 4d 41 58 29 3b 0a 20 20  t, LEAF_MAX);.  
2fdcc 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26  dataBufferInit(&
2fdcd 6d 65 72 67 65 64 2c 20 4c 45 41 46 5f 4d 41 58  merged, LEAF_MAX
2fdce 29 3b 0a 0a 20 20 2f 2a 20 45 78 68 61 75 73 74  );..  /* Exhaust
2fdcf 65 64 20 72 65 61 64 65 72 73 20 62 75 62 62 6c  ed readers bubbl
2fdd0 65 20 74 6f 20 74 68 65 20 65 6e 64 2c 20 73 6f  e to the end, so
2fdd1 20 77 68 65 6e 20 74 68 65 20 66 69 72 73 74 20   when the first 
2fdd2 72 65 61 64 65 72 20 69 73 0a 20 20 2a 2a 20 61  reader is.  ** a
2fdd3 74 20 65 6f 66 2c 20 61 6c 6c 20 61 72 65 20 61  t eof, all are a
2fdd4 74 20 65 6f 66 2e 0a 20 20 2a 2f 0a 20 20 77 68  t eof..  */.  wh
2fdd5 69 6c 65 28 20 21 6f 70 74 4c 65 61 76 65 73 52  ile( !optLeavesR
2fdd6 65 61 64 65 72 41 74 45 6e 64 28 26 72 65 61 64  eaderAtEnd(&read
2fdd7 65 72 73 5b 30 5d 29 20 29 7b 0a 0a 20 20 20 20  ers[0]) ){..    
2fdd8 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f  /* Figure out ho
2fdd9 77 20 6d 61 6e 79 20 72 65 61 64 65 72 73 20 73  w many readers s
2fdda 68 61 72 65 20 74 68 65 20 6e 65 78 74 20 74 65  hare the next te
2fddb 72 6d 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69  rm. */.    for(i
2fddc 3d 31 3b 20 69 3c 6e 52 65 61 64 65 72 73 20 26  =1; i<nReaders &
2fddd 26 20 21 6f 70 74 4c 65 61 76 65 73 52 65 61 64  & !optLeavesRead
2fdde 65 72 41 74 45 6e 64 28 26 72 65 61 64 65 72 73  erAtEnd(&readers
2fddf 5b 69 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  [i]); i++){.    
2fde0 20 20 69 66 28 20 30 21 3d 6f 70 74 4c 65 61 76    if( 0!=optLeav
2fde1 65 73 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28  esReaderTermCmp(
2fde2 26 72 65 61 64 65 72 73 5b 30 5d 2c 20 26 72 65  &readers[0], &re
2fde3 61 64 65 72 73 5b 69 5d 29 20 29 20 62 72 65 61  aders[i]) ) brea
2fde4 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  k;.    }..    /*
2fde5 20 53 70 65 63 69 61 6c 2d 63 61 73 65 20 66 6f   Special-case fo
2fde6 72 20 6e 6f 20 6d 65 72 67 65 2e 20 2a 2f 0a 20  r no merge. */. 
2fde7 20 20 20 69 66 28 20 69 3d 3d 31 20 29 7b 0a 20     if( i==1 ){. 
2fde8 20 20 20 20 20 2f 2a 20 54 72 69 6d 20 64 65 6c       /* Trim del
2fde9 65 74 69 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20  etions from the 
2fdea 64 6f 63 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 20  doclist. */.    
2fdeb 20 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65    dataBufferRese
2fdec 74 28 26 6d 65 72 67 65 64 29 3b 0a 20 20 20 20  t(&merged);.    
2fded 20 20 64 6f 63 4c 69 73 74 54 72 69 6d 28 44 4c    docListTrim(DL
2fdee 5f 44 45 46 41 55 4c 54 2c 0a 20 20 20 20 20 20  _DEFAULT,.      
2fdef 20 20 20 20 20 20 20 20 20 20 20 20 6f 70 74 4c              optL
2fdf0 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61 28  eavesReaderData(
2fdf1 26 72 65 61 64 65 72 73 5b 30 5d 29 2c 0a 20 20  &readers[0]),.  
2fdf2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fdf3 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 44  optLeavesReaderD
2fdf4 61 74 61 42 79 74 65 73 28 26 72 65 61 64 65 72  ataBytes(&reader
2fdf5 73 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20 20 20  s[0]),.         
2fdf6 20 20 20 20 20 20 20 20 20 2d 31 2c 20 44 4c 5f           -1, DL_
2fdf7 44 45 46 41 55 4c 54 2c 20 26 6d 65 72 67 65 64  DEFAULT, &merged
2fdf8 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
2fdf9 20 20 20 20 44 4c 52 65 61 64 65 72 20 64 6c 52      DLReader dlR
2fdfa 65 61 64 65 72 73 5b 4d 45 52 47 45 5f 43 4f 55  eaders[MERGE_COU
2fdfb 4e 54 5d 3b 0a 20 20 20 20 20 20 69 6e 74 20 69  NT];.      int i
2fdfc 52 65 61 64 65 72 2c 20 6e 52 65 61 64 65 72 73  Reader, nReaders
2fdfd 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 50 72 69 6d  ;..      /* Prim
2fdfe 65 20 74 68 65 20 70 69 70 65 6c 69 6e 65 20 77  e the pipeline w
2fdff 69 74 68 20 74 68 65 20 66 69 72 73 74 20 72 65  ith the first re
2fe00 61 64 65 72 27 73 20 64 6f 63 6c 69 73 74 2e 20  ader's doclist. 
2fe01 20 41 66 74 65 72 0a 20 20 20 20 20 20 2a 2a 20   After.      ** 
2fe02 6f 6e 65 20 70 61 73 73 20 69 6e 64 65 78 20 30  one pass index 0
2fe03 20 77 69 6c 6c 20 72 65 66 65 72 65 6e 63 65 20   will reference 
2fe04 74 68 65 20 61 63 63 75 6d 75 6c 61 74 65 64 20  the accumulated 
2fe05 64 6f 63 6c 69 73 74 2e 0a 20 20 20 20 20 20 2a  doclist..      *
2fe06 2f 0a 20 20 20 20 20 20 64 6c 72 49 6e 69 74 28  /.      dlrInit(
2fe07 26 64 6c 52 65 61 64 65 72 73 5b 30 5d 2c 20 44  &dlReaders[0], D
2fe08 4c 5f 44 45 46 41 55 4c 54 2c 0a 20 20 20 20 20  L_DEFAULT,.     
2fe09 20 20 20 20 20 20 20 20 20 6f 70 74 4c 65 61 76           optLeav
2fe0a 65 73 52 65 61 64 65 72 44 61 74 61 28 26 72 65  esReaderData(&re
2fe0b 61 64 65 72 73 5b 30 5d 29 2c 0a 20 20 20 20 20  aders[0]),.     
2fe0c 20 20 20 20 20 20 20 20 20 6f 70 74 4c 65 61 76           optLeav
2fe0d 65 73 52 65 61 64 65 72 44 61 74 61 42 79 74 65  esReaderDataByte
2fe0e 73 28 26 72 65 61 64 65 72 73 5b 30 5d 29 29 3b  s(&readers[0]));
2fe0f 0a 20 20 20 20 20 20 69 52 65 61 64 65 72 20 3d  .      iReader =
2fe10 20 31 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72   1;..      asser
2fe11 74 28 20 69 52 65 61 64 65 72 3c 69 20 29 3b 20  t( iReader<i ); 
2fe12 20 2f 2a 20 4d 75 73 74 20 65 78 65 63 75 74 65   /* Must execute
2fe13 20 74 68 65 20 6c 6f 6f 70 20 61 74 20 6c 65 61   the loop at lea
2fe14 73 74 20 6f 6e 63 65 2e 20 2a 2f 0a 20 20 20 20  st once. */.    
2fe15 20 20 77 68 69 6c 65 28 20 69 52 65 61 64 65 72    while( iReader
2fe16 3c 69 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  <i ){.        /*
2fe17 20 4d 65 72 67 65 20 31 36 20 69 6e 70 75 74 73   Merge 16 inputs
2fe18 20 70 65 72 20 70 61 73 73 2e 20 2a 2f 0a 20 20   per pass. */.  
2fe19 20 20 20 20 20 20 66 6f 72 28 20 6e 52 65 61 64        for( nRead
2fe1a 65 72 73 3d 31 3b 20 69 52 65 61 64 65 72 3c 69  ers=1; iReader<i
2fe1b 20 26 26 20 6e 52 65 61 64 65 72 73 3c 4d 45 52   && nReaders<MER
2fe1c 47 45 5f 43 4f 55 4e 54 3b 0a 20 20 20 20 20 20  GE_COUNT;.      
2fe1d 20 20 20 20 20 20 20 69 52 65 61 64 65 72 2b 2b         iReader++
2fe1e 2c 20 6e 52 65 61 64 65 72 73 2b 2b 20 29 7b 0a  , nReaders++ ){.
2fe1f 20 20 20 20 20 20 20 20 20 20 64 6c 72 49 6e 69            dlrIni
2fe20 74 28 26 64 6c 52 65 61 64 65 72 73 5b 6e 52 65  t(&dlReaders[nRe
2fe21 61 64 65 72 73 5d 2c 20 44 4c 5f 44 45 46 41 55  aders], DL_DEFAU
2fe22 4c 54 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  LT,.            
2fe23 20 20 20 20 20 20 6f 70 74 4c 65 61 76 65 73 52        optLeavesR
2fe24 65 61 64 65 72 44 61 74 61 28 26 72 65 61 64 65  eaderData(&reade
2fe25 72 73 5b 69 52 65 61 64 65 72 5d 29 2c 0a 20 20  rs[iReader]),.  
2fe26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fe27 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 44  optLeavesReaderD
2fe28 61 74 61 42 79 74 65 73 28 26 72 65 61 64 65 72  ataBytes(&reader
2fe29 73 5b 69 52 65 61 64 65 72 5d 29 29 3b 0a 20 20  s[iReader]));.  
2fe2a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20        }..       
2fe2b 20 2f 2a 20 4d 65 72 67 65 20 64 6f 63 6c 69 73   /* Merge doclis
2fe2c 74 73 20 61 6e 64 20 73 77 61 70 20 72 65 73 75  ts and swap resu
2fe2d 6c 74 20 69 6e 74 6f 20 61 63 63 75 6d 75 6c 61  lt into accumula
2fe2e 74 6f 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  tor. */.        
2fe2f 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74 28  dataBufferReset(
2fe30 26 6d 65 72 67 65 64 29 3b 0a 20 20 20 20 20 20  &merged);.      
2fe31 20 20 64 6f 63 4c 69 73 74 4d 65 72 67 65 28 26    docListMerge(&
2fe32 6d 65 72 67 65 64 2c 20 64 6c 52 65 61 64 65 72  merged, dlReader
2fe33 73 2c 20 6e 52 65 61 64 65 72 73 29 3b 0a 20 20  s, nReaders);.  
2fe34 20 20 20 20 20 20 74 6d 70 20 3d 20 6d 65 72 67        tmp = merg
2fe35 65 64 3b 0a 20 20 20 20 20 20 20 20 6d 65 72 67  ed;.        merg
2fe36 65 64 20 3d 20 64 6f 63 6c 69 73 74 3b 0a 20 20  ed = doclist;.  
2fe37 20 20 20 20 20 20 64 6f 63 6c 69 73 74 20 3d 20        doclist = 
2fe38 74 6d 70 3b 0a 0a 20 20 20 20 20 20 20 20 77 68  tmp;..        wh
2fe39 69 6c 65 28 20 6e 52 65 61 64 65 72 73 2d 2d 20  ile( nReaders-- 
2fe3a 3e 20 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  > 0 ){.         
2fe3b 20 64 6c 72 44 65 73 74 72 6f 79 28 26 64 6c 52   dlrDestroy(&dlR
2fe3c 65 61 64 65 72 73 5b 6e 52 65 61 64 65 72 73 5d  eaders[nReaders]
2fe3d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  );.        }..  
2fe3e 20 20 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c        /* Accumul
2fe3f 61 74 65 64 20 64 6f 63 6c 69 73 74 20 74 6f 20  ated doclist to 
2fe40 72 65 61 64 65 72 20 30 20 66 6f 72 20 6e 65 78  reader 0 for nex
2fe41 74 20 70 61 73 73 2e 20 2a 2f 0a 20 20 20 20 20  t pass. */.     
2fe42 20 20 20 64 6c 72 49 6e 69 74 28 26 64 6c 52 65     dlrInit(&dlRe
2fe43 61 64 65 72 73 5b 30 5d 2c 20 44 4c 5f 44 45 46  aders[0], DL_DEF
2fe44 41 55 4c 54 2c 20 64 6f 63 6c 69 73 74 2e 70 44  AULT, doclist.pD
2fe45 61 74 61 2c 20 64 6f 63 6c 69 73 74 2e 6e 44 61  ata, doclist.nDa
2fe46 74 61 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  ta);.      }..  
2fe47 20 20 20 20 2f 2a 20 44 65 73 74 72 6f 79 20 72      /* Destroy r
2fe48 65 61 64 65 72 20 74 68 61 74 20 77 61 73 20 6c  eader that was l
2fe49 65 66 74 20 69 6e 20 74 68 65 20 70 69 70 65 6c  eft in the pipel
2fe4a 69 6e 65 2e 20 2a 2f 0a 20 20 20 20 20 20 64 6c  ine. */.      dl
2fe4b 72 44 65 73 74 72 6f 79 28 26 64 6c 52 65 61 64  rDestroy(&dlRead
2fe4c 65 72 73 5b 30 5d 29 3b 0a 0a 20 20 20 20 20 20  ers[0]);..      
2fe4d 2f 2a 20 54 72 69 6d 20 64 65 6c 65 74 69 6f 6e  /* Trim deletion
2fe4e 73 20 66 72 6f 6d 20 74 68 65 20 64 6f 63 6c 69  s from the docli
2fe4f 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20 64 61 74  st. */.      dat
2fe50 61 42 75 66 66 65 72 52 65 73 65 74 28 26 6d 65  aBufferReset(&me
2fe51 72 67 65 64 29 3b 0a 20 20 20 20 20 20 64 6f 63  rged);.      doc
2fe52 4c 69 73 74 54 72 69 6d 28 44 4c 5f 44 45 46 41  ListTrim(DL_DEFA
2fe53 55 4c 54 2c 20 64 6f 63 6c 69 73 74 2e 70 44 61  ULT, doclist.pDa
2fe54 74 61 2c 20 64 6f 63 6c 69 73 74 2e 6e 44 61 74  ta, doclist.nDat
2fe55 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  a,.             
2fe56 20 20 20 20 20 2d 31 2c 20 44 4c 5f 44 45 46 41       -1, DL_DEFA
2fe57 55 4c 54 2c 20 26 6d 65 72 67 65 64 29 3b 0a 20  ULT, &merged);. 
2fe58 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f 6e 6c     }..    /* Onl
2fe59 79 20 70 61 73 73 20 64 6f 63 6c 69 73 74 73 20  y pass doclists 
2fe5a 77 69 74 68 20 68 69 74 73 20 28 73 6b 69 70 20  with hits (skip 
2fe5b 69 66 20 61 6c 6c 20 68 69 74 73 20 64 65 6c 65  if all hits dele
2fe5c 74 65 64 29 2e 20 2a 2f 0a 20 20 20 20 69 66 28  ted). */.    if(
2fe5d 20 6d 65 72 67 65 64 2e 6e 44 61 74 61 3e 30 20   merged.nData>0 
2fe5e 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 65  ){.      rc = le
2fe5f 61 66 57 72 69 74 65 72 53 74 65 70 28 76 2c 20  afWriterStep(v, 
2fe60 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20  pWriter,.       
2fe61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fe62 20 20 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64     optLeavesRead
2fe63 65 72 54 65 72 6d 28 26 72 65 61 64 65 72 73 5b  erTerm(&readers[
2fe64 30 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  0]),.           
2fe65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f                 o
2fe66 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 54 65  ptLeavesReaderTe
2fe67 72 6d 42 79 74 65 73 28 26 72 65 61 64 65 72 73  rmBytes(&readers
2fe68 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20  [0]),.          
2fe69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fe6a 6d 65 72 67 65 64 2e 70 44 61 74 61 2c 20 6d 65  merged.pData, me
2fe6b 72 67 65 64 2e 6e 44 61 74 61 29 3b 0a 20 20 20  rged.nData);.   
2fe6c 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
2fe6d 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b  E_OK ) goto err;
2fe6e 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53  .    }..    /* S
2fe6f 74 65 70 20 6d 65 72 67 65 64 20 72 65 61 64 65  tep merged reade
2fe70 72 73 20 74 6f 20 6e 65 78 74 20 74 65 72 6d 20  rs to next term 
2fe71 61 6e 64 20 72 65 6f 72 64 65 72 2e 20 2a 2f 0a  and reorder. */.
2fe72 20 20 20 20 77 68 69 6c 65 28 20 69 2d 2d 20 3e      while( i-- >
2fe73 20 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d   0 ){.      rc =
2fe74 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   optLeavesReader
2fe75 53 74 65 70 28 76 2c 20 26 72 65 61 64 65 72 73  Step(v, &readers
2fe76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  [i]);.      if( 
2fe77 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2fe78 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20 20 20 20  goto err;..     
2fe79 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   optLeavesReader
2fe7a 52 65 6f 72 64 65 72 28 26 72 65 61 64 65 72 73  Reorder(&readers
2fe7b 5b 69 5d 2c 20 6e 52 65 61 64 65 72 73 2d 69 29  [i], nReaders-i)
2fe7c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 65 72  ;.    }.  }.. er
2fe7d 72 3a 0a 20 20 64 61 74 61 42 75 66 66 65 72 44  r:.  dataBufferD
2fe7e 65 73 74 72 6f 79 28 26 64 6f 63 6c 69 73 74 29  estroy(&doclist)
2fe7f 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65  ;.  dataBufferDe
2fe80 73 74 72 6f 79 28 26 6d 65 72 67 65 64 29 3b 0a  stroy(&merged);.
2fe81 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
2fe82 2f 2a 20 49 6d 70 6c 65 6d 65 6e 74 20 6f 70 74  /* Implement opt
2fe83 69 6d 69 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e  imize() function
2fe84 20 66 6f 72 20 46 54 53 33 2e 20 20 6f 70 74 69   for FTS3.  opti
2fe85 6d 69 7a 65 28 74 29 20 6d 65 72 67 65 73 20 61  mize(t) merges a
2fe86 6c 6c 0a 2a 2a 20 73 65 67 6d 65 6e 74 73 20 69  ll.** segments i
2fe87 6e 20 74 68 65 20 66 74 73 20 69 6e 64 65 78 20  n the fts index 
2fe88 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 65  into a single se
2fe89 67 6d 65 6e 74 2e 20 20 27 74 27 20 69 73 20 74  gment.  't' is t
2fe8a 68 65 20 6d 61 67 69 63 0a 2a 2a 20 74 61 62 6c  he magic.** tabl
2fe8b 65 2d 6e 61 6d 65 64 20 63 6f 6c 75 6d 6e 2e 0a  e-named column..
2fe8c 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f  */.static void o
2fe8d 70 74 69 6d 69 7a 65 46 75 6e 63 28 73 71 6c 69  ptimizeFunc(sqli
2fe8e 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f  te3_context *pCo
2fe8f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20  ntext,.         
2fe90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fe91 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
2fe92 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b  3_value **argv){
2fe93 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  .  fulltext_curs
2fe94 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20 69  or *pCursor;.  i
2fe95 66 28 20 61 72 67 63 3e 31 20 29 7b 0a 20 20 20  f( argc>1 ){.   
2fe96 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2fe97 65 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20  error(pContext, 
2fe98 22 65 78 63 65 73 73 20 61 72 67 75 6d 65 6e 74  "excess argument
2fe99 73 20 74 6f 20 6f 70 74 69 6d 69 7a 65 28 29 22  s to optimize()"
2fe9a 2c 2d 31 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ,-1);.  }else if
2fe9b 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
2fe9c 74 79 70 65 28 61 72 67 76 5b 30 5d 29 21 3d 53  type(argv[0])!=S
2fe9d 51 4c 49 54 45 5f 42 4c 4f 42 20 7c 7c 0a 20 20  QLITE_BLOB ||.  
2fe9e 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2fe9f 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
2fea0 67 76 5b 30 5d 29 21 3d 73 69 7a 65 6f 66 28 70  gv[0])!=sizeof(p
2fea1 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 73  Cursor) ){.    s
2fea2 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
2fea3 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 69  ror(pContext, "i
2fea4 6c 6c 65 67 61 6c 20 66 69 72 73 74 20 61 72 67  llegal first arg
2fea5 75 6d 65 6e 74 20 74 6f 20 6f 70 74 69 6d 69 7a  ument to optimiz
2fea6 65 22 2c 2d 31 29 3b 0a 20 20 7d 65 6c 73 65 7b  e",-1);.  }else{
2fea7 0a 20 20 20 20 66 75 6c 6c 74 65 78 74 5f 76 74  .    fulltext_vt
2fea8 61 62 20 2a 76 3b 0a 20 20 20 20 69 6e 74 20 69  ab *v;.    int i
2fea9 2c 20 72 63 2c 20 69 4d 61 78 4c 65 76 65 6c 3b  , rc, iMaxLevel;
2feaa 0a 20 20 20 20 4f 70 74 4c 65 61 76 65 73 52 65  .    OptLeavesRe
2feab 61 64 65 72 20 2a 72 65 61 64 65 72 73 3b 0a 20  ader *readers;. 
2feac 20 20 20 69 6e 74 20 6e 52 65 61 64 65 72 73 3b     int nReaders;
2fead 0a 20 20 20 20 4c 65 61 66 57 72 69 74 65 72 20  .    LeafWriter 
2feae 77 72 69 74 65 72 3b 0a 20 20 20 20 73 71 6c 69  writer;.    sqli
2feaf 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 0a 20 20  te3_stmt *s;..  
2feb0 20 20 6d 65 6d 63 70 79 28 26 70 43 75 72 73 6f    memcpy(&pCurso
2feb1 72 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  r, sqlite3_value
2feb2 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 2c 20  _blob(argv[0]), 
2feb3 73 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 29  sizeof(pCursor))
2feb4 3b 0a 20 20 20 20 76 20 3d 20 63 75 72 73 6f 72  ;.    v = cursor
2feb5 5f 76 74 61 62 28 70 43 75 72 73 6f 72 29 3b 0a  _vtab(pCursor);.
2feb6 0a 20 20 20 20 2f 2a 20 46 6c 75 73 68 20 61 6e  .    /* Flush an
2feb7 79 20 62 75 66 66 65 72 65 64 20 75 70 64 61 74  y buffered updat
2feb8 65 73 20 62 65 66 6f 72 65 20 6f 70 74 69 6d 69  es before optimi
2feb9 7a 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 63 20  zing. */.    rc 
2feba 3d 20 66 6c 75 73 68 50 65 6e 64 69 6e 67 54 65  = flushPendingTe
2febb 72 6d 73 28 76 29 3b 0a 20 20 20 20 69 66 28 20  rms(v);.    if( 
2febc 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2febd 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20 20 20 72  goto err;..    r
2febe 63 20 3d 20 73 65 67 64 69 72 5f 63 6f 75 6e 74  c = segdir_count
2febf 28 76 2c 20 26 6e 52 65 61 64 65 72 73 2c 20 26  (v, &nReaders, &
2fec0 69 4d 61 78 4c 65 76 65 6c 29 3b 0a 20 20 20 20  iMaxLevel);.    
2fec1 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2fec2 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 20 20  K ) goto err;.  
2fec3 20 20 69 66 28 20 6e 52 65 61 64 65 72 73 3d 3d    if( nReaders==
2fec4 30 20 7c 7c 20 6e 52 65 61 64 65 72 73 3d 3d 31  0 || nReaders==1
2fec5 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2fec6 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43  3_result_text(pC
2fec7 6f 6e 74 65 78 74 2c 20 22 49 6e 64 65 78 20 61  ontext, "Index a
2fec8 6c 72 65 61 64 79 20 6f 70 74 69 6d 61 6c 22 2c  lready optimal",
2fec9 20 2d 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20   -1,.           
2feca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
2fecb 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
2fecc 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
2fecd 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   }..    rc = sql
2fece 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
2fecf 2c 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f  , SEGDIR_SELECT_
2fed0 41 4c 4c 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20  ALL_STMT, &s);. 
2fed1 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
2fed2 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b  E_OK ) goto err;
2fed3 0a 0a 20 20 20 20 72 65 61 64 65 72 73 20 3d 20  ..    readers = 
2fed4 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
2fed5 52 65 61 64 65 72 73 2a 73 69 7a 65 6f 66 28 72  Readers*sizeof(r
2fed6 65 61 64 65 72 73 5b 30 5d 29 29 3b 0a 20 20 20  eaders[0]));.   
2fed7 20 69 66 28 20 72 65 61 64 65 72 73 3d 3d 4e 55   if( readers==NU
2fed8 4c 4c 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a  LL ) goto err;..
2fed9 20 20 20 20 2f 2a 20 4e 6f 74 65 20 74 68 61 74      /* Note that
2feda 20 74 68 65 72 65 20 77 69 6c 6c 20 61 6c 72 65   there will alre
2fedb 61 64 79 20 62 65 20 61 20 73 65 67 6d 65 6e 74  ady be a segment
2fedc 20 61 74 20 74 68 69 73 20 70 6f 73 69 74 69 6f   at this positio
2fedd 6e 0a 20 20 20 20 2a 2a 20 75 6e 74 69 6c 20 77  n.    ** until w
2fede 65 20 63 61 6c 6c 20 73 65 67 64 69 72 5f 64 65  e call segdir_de
2fedf 6c 65 74 65 28 29 20 6f 6e 20 69 4d 61 78 4c 65  lete() on iMaxLe
2fee0 76 65 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  vel..    */.    
2fee1 6c 65 61 66 57 72 69 74 65 72 49 6e 69 74 28 69  leafWriterInit(i
2fee2 4d 61 78 4c 65 76 65 6c 2c 20 30 2c 20 26 77 72  MaxLevel, 0, &wr
2fee3 69 74 65 72 29 3b 0a 0a 20 20 20 20 69 20 3d 20  iter);..    i = 
2fee4 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 72  0;.    while( (r
2fee5 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
2fee6 28 73 29 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  (s))==SQLITE_ROW
2fee7 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2fee8 5f 69 6e 74 36 34 20 69 53 74 61 72 74 20 3d 20  _int64 iStart = 
2fee9 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
2feea 6e 74 36 34 28 73 2c 20 30 29 3b 0a 20 20 20 20  nt64(s, 0);.    
2feeb 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
2feec 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  End = sqlite3_co
2feed 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29  lumn_int64(s, 1)
2feee 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  ;.      const ch
2feef 61 72 20 2a 70 52 6f 6f 74 44 61 74 61 20 3d 20  ar *pRootData = 
2fef0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
2fef1 6c 6f 62 28 73 2c 20 32 29 3b 0a 20 20 20 20 20  lob(s, 2);.     
2fef2 20 69 6e 74 20 6e 52 6f 6f 74 44 61 74 61 20 3d   int nRootData =
2fef3 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
2fef4 62 79 74 65 73 28 73 2c 20 32 29 3b 0a 0a 20 20  bytes(s, 2);..  
2fef5 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e 52      assert( i<nR
2fef6 65 61 64 65 72 73 20 29 3b 0a 20 20 20 20 20 20  eaders );.      
2fef7 72 63 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65  rc = leavesReade
2fef8 72 49 6e 69 74 28 76 2c 20 2d 31 2c 20 69 53 74  rInit(v, -1, iSt
2fef9 61 72 74 2c 20 69 45 6e 64 2c 20 70 52 6f 6f 74  art, iEnd, pRoot
2fefa 44 61 74 61 2c 20 6e 52 6f 6f 74 44 61 74 61 2c  Data, nRootData,
2fefb 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2fefc 20 20 20 20 20 20 20 20 20 20 20 20 20 26 72 65               &re
2fefd 61 64 65 72 73 5b 69 5d 2e 72 65 61 64 65 72 29  aders[i].reader)
2fefe 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
2feff 53 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65 61  SQLITE_OK ) brea
2ff00 6b 3b 0a 0a 20 20 20 20 20 20 72 65 61 64 65 72  k;..      reader
2ff01 73 5b 69 5d 2e 73 65 67 6d 65 6e 74 20 3d 20 69  s[i].segment = i
2ff02 3b 0a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20  ;.      i++;.   
2ff03 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65   }..    /* If we
2ff04 20 6d 61 6e 61 67 65 64 20 74 6f 20 73 75 63 63   managed to succ
2ff05 65 73 66 75 6c 6c 79 20 72 65 61 64 20 74 68 65  esfully read the
2ff06 6d 20 61 6c 6c 2c 20 6f 70 74 69 6d 69 7a 65 20  m all, optimize 
2ff07 74 68 65 6d 2e 20 2a 2f 0a 20 20 20 20 69 66 28  them. */.    if(
2ff08 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc==SQLITE_DONE
2ff09 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
2ff0a 28 20 69 3d 3d 6e 52 65 61 64 65 72 73 20 29 3b  ( i==nReaders );
2ff0b 0a 20 20 20 20 20 20 72 63 20 3d 20 6f 70 74 69  .      rc = opti
2ff0c 6d 69 7a 65 49 6e 74 65 72 6e 61 6c 28 76 2c 20  mizeInternal(v, 
2ff0d 72 65 61 64 65 72 73 2c 20 6e 52 65 61 64 65 72  readers, nReader
2ff0e 73 2c 20 26 77 72 69 74 65 72 29 3b 0a 20 20 20  s, &writer);.   
2ff0f 20 7d 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 69   }..    while( i
2ff10 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 20 20  -- > 0 ){.      
2ff11 6c 65 61 76 65 73 52 65 61 64 65 72 44 65 73 74  leavesReaderDest
2ff12 72 6f 79 28 26 72 65 61 64 65 72 73 5b 69 5d 2e  roy(&readers[i].
2ff13 72 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a 20  reader);.    }. 
2ff14 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
2ff15 72 65 61 64 65 72 73 29 3b 0a 0a 20 20 20 20 2f  readers);..    /
2ff16 2a 20 49 66 20 77 65 27 76 65 20 73 75 63 63 65  * If we've succe
2ff17 73 73 66 75 6c 6c 79 20 67 6f 74 74 65 6e 20 74  ssfully gotten t
2ff18 6f 20 68 65 72 65 2c 20 64 65 6c 65 74 65 20 74  o here, delete t
2ff19 68 65 20 6f 6c 64 20 73 65 67 6d 65 6e 74 73 0a  he old segments.
2ff1a 20 20 20 20 2a 2a 20 61 6e 64 20 66 6c 75 73 68      ** and flush
2ff1b 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20 73 74   the interior st
2ff1c 72 75 63 74 75 72 65 20 6f 66 20 74 68 65 20 6e  ructure of the n
2ff1d 65 77 20 73 65 67 6d 65 6e 74 2e 0a 20 20 20 20  ew segment..    
2ff1e 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  */.    if( rc==S
2ff1f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
2ff20 20 20 66 6f 72 28 20 69 3d 30 3b 20 69 3c 3d 69    for( i=0; i<=i
2ff21 4d 61 78 4c 65 76 65 6c 3b 20 69 2b 2b 20 29 7b  MaxLevel; i++ ){
2ff22 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 65  .        rc = se
2ff23 67 64 69 72 5f 64 65 6c 65 74 65 28 76 2c 20 69  gdir_delete(v, i
2ff24 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
2ff25 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 62  c!=SQLITE_OK ) b
2ff26 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  reak;.      }.. 
2ff27 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
2ff28 49 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 6c 65  ITE_OK ) rc = le
2ff29 61 66 57 72 69 74 65 72 46 69 6e 61 6c 69 7a 65  afWriterFinalize
2ff2a 28 76 2c 20 26 77 72 69 74 65 72 29 3b 0a 20 20  (v, &writer);.  
2ff2b 20 20 7d 0a 0a 20 20 20 20 6c 65 61 66 57 72 69    }..    leafWri
2ff2c 74 65 72 44 65 73 74 72 6f 79 28 26 77 72 69 74  terDestroy(&writ
2ff2d 65 72 29 3b 0a 0a 20 20 20 20 69 66 28 20 72 63  er);..    if( rc
2ff2e 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
2ff2f 74 6f 20 65 72 72 3b 0a 0a 20 20 20 20 73 71 6c  to err;..    sql
2ff30 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
2ff31 28 70 43 6f 6e 74 65 78 74 2c 20 22 49 6e 64 65  (pContext, "Inde
2ff32 78 20 6f 70 74 69 6d 69 7a 65 64 22 2c 20 2d 31  x optimized", -1
2ff33 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
2ff34 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 0a 20  ;.    return;.. 
2ff35 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73     /* TODO(shess
2ff36 29 3a 20 45 72 72 6f 72 2d 68 61 6e 64 6c 69 6e  ): Error-handlin
2ff37 67 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6d  g needs to be im
2ff38 70 72 6f 76 65 64 20 61 6c 6f 6e 67 20 74 68 65  proved along the
2ff39 0a 20 20 20 20 2a 2a 20 6c 69 6e 65 73 20 6f 66  .    ** lines of
2ff3a 20 74 68 65 20 64 75 6d 70 5f 20 66 75 6e 63 74   the dump_ funct
2ff3b 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f 0a 20 65 72  ions..    */. er
2ff3c 72 3a 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 63  r:.    {.      c
2ff3d 68 61 72 20 62 75 66 5b 35 31 32 5d 3b 0a 20 20  har buf[512];.  
2ff3e 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
2ff3f 69 6e 74 66 28 73 69 7a 65 6f 66 28 62 75 66 29  intf(sizeof(buf)
2ff40 2c 20 62 75 66 2c 20 22 45 72 72 6f 72 20 69 6e  , buf, "Error in
2ff41 20 6f 70 74 69 6d 69 7a 65 3a 20 25 73 22 2c 0a   optimize: %s",.
2ff42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ff43 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 65         sqlite3_e
2ff44 72 72 6d 73 67 28 73 71 6c 69 74 65 33 5f 63 6f  rrmsg(sqlite3_co
2ff45 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
2ff46 70 43 6f 6e 74 65 78 74 29 29 29 3b 0a 20 20 20  pContext)));.   
2ff47 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
2ff48 74 5f 65 72 72 6f 72 28 70 43 6f 6e 74 65 78 74  t_error(pContext
2ff49 2c 20 62 75 66 2c 20 2d 31 29 3b 0a 20 20 20 20  , buf, -1);.    
2ff4a 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64 65 66 20  }.  }.}..#ifdef 
2ff4b 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 20 47  SQLITE_TEST./* G
2ff4c 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72  enerate an error
2ff4d 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3c 70   of the form "<p
2ff4e 72 65 66 69 78 3e 3a 20 3c 6d 73 67 3e 22 2e 20  refix>: <msg>". 
2ff4f 20 49 66 20 6d 73 67 20 69 73 20 4e 55 4c 4c 2c   If msg is NULL,
2ff50 0a 2a 2a 20 70 75 6c 6c 20 74 68 65 20 65 72 72  .** pull the err
2ff51 6f 72 20 66 72 6f 6d 20 74 68 65 20 63 6f 6e 74  or from the cont
2ff52 65 78 74 27 73 20 64 62 20 68 61 6e 64 6c 65 2e  ext's db handle.
2ff53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2ff54 67 65 6e 65 72 61 74 65 45 72 72 6f 72 28 73 71  generateError(sq
2ff55 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
2ff56 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20  Context,.       
2ff57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ff58 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
2ff59 72 65 66 69 78 2c 20 63 6f 6e 73 74 20 63 68 61  refix, const cha
2ff5a 72 20 2a 6d 73 67 29 7b 0a 20 20 63 68 61 72 20  r *msg){.  char 
2ff5b 62 75 66 5b 35 31 32 5d 3b 0a 20 20 69 66 28 20  buf[512];.  if( 
2ff5c 6d 73 67 3d 3d 4e 55 4c 4c 20 29 20 6d 73 67 20  msg==NULL ) msg 
2ff5d 3d 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  = sqlite3_errmsg
2ff5e 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2ff5f 5f 64 62 5f 68 61 6e 64 6c 65 28 70 43 6f 6e 74  _db_handle(pCont
2ff60 65 78 74 29 29 3b 0a 20 20 73 71 6c 69 74 65 33  ext));.  sqlite3
2ff61 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66  _snprintf(sizeof
2ff62 28 62 75 66 29 2c 20 62 75 66 2c 20 22 25 73 3a  (buf), buf, "%s:
2ff63 20 25 73 22 2c 20 70 72 65 66 69 78 2c 20 6d 73   %s", prefix, ms
2ff64 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  g);.  sqlite3_re
2ff65 73 75 6c 74 5f 65 72 72 6f 72 28 70 43 6f 6e 74  sult_error(pCont
2ff66 65 78 74 2c 20 62 75 66 2c 20 2d 31 29 3b 0a 7d  ext, buf, -1);.}
2ff67 0a 0a 2f 2a 20 48 65 6c 70 65 72 20 66 75 6e 63  ../* Helper func
2ff68 74 69 6f 6e 20 74 6f 20 63 6f 6c 6c 65 63 74 20  tion to collect 
2ff69 74 68 65 20 73 65 74 20 6f 66 20 74 65 72 6d 73  the set of terms
2ff6a 20 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74 20   in the segment 
2ff6b 69 6e 74 6f 0a 2a 2a 20 70 54 65 72 6d 73 2e 20  into.** pTerms. 
2ff6c 20 54 68 65 20 73 65 67 6d 65 6e 74 20 69 73 20   The segment is 
2ff6d 64 65 66 69 6e 65 64 20 62 79 20 74 68 65 20 6c  defined by the l
2ff6e 65 61 66 20 6e 6f 64 65 73 20 62 65 74 77 65 65  eaf nodes betwee
2ff6f 6e 0a 2a 2a 20 69 53 74 61 72 74 42 6c 6f 63 6b  n.** iStartBlock
2ff70 69 64 20 61 6e 64 20 69 45 6e 64 42 6c 6f 63 6b  id and iEndBlock
2ff71 69 64 2c 20 69 6e 63 6c 75 73 69 76 65 2c 20 6f  id, inclusive, o
2ff72 72 20 62 79 20 74 68 65 20 63 6f 6e 74 65 6e 74  r by the content
2ff73 73 20 6f 66 0a 2a 2a 20 70 52 6f 6f 74 44 61 74  s of.** pRootDat
2ff74 61 20 69 66 20 69 53 74 61 72 74 42 6c 6f 63 6b  a if iStartBlock
2ff75 69 64 20 69 73 20 30 20 28 69 6e 20 77 68 69 63  id is 0 (in whic
2ff76 68 20 63 61 73 65 20 74 68 65 20 65 6e 74 69 72  h case the entir
2ff77 65 20 73 65 67 6d 65 6e 74 0a 2a 2a 20 66 69 74  e segment.** fit
2ff78 20 69 6e 20 61 20 6c 65 61 66 29 2e 0a 2a 2f 0a   in a leaf)..*/.
2ff79 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6c 6c 65  static int colle
2ff7a 63 74 53 65 67 6d 65 6e 74 54 65 72 6d 73 28 66  ctSegmentTerms(f
2ff7b 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
2ff7c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73   sqlite3_stmt *s
2ff7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2ff7e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ff7f 20 66 74 73 33 48 61 73 68 20 2a 70 54 65 72 6d   fts3Hash *pTerm
2ff80 73 29 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69  s){.  const sqli
2ff81 74 65 5f 69 6e 74 36 34 20 69 53 74 61 72 74 42  te_int64 iStartB
2ff82 6c 6f 63 6b 69 64 20 3d 20 73 71 6c 69 74 65 33  lockid = sqlite3
2ff83 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c  _column_int64(s,
2ff84 20 30 29 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c   0);.  const sql
2ff85 69 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c  ite_int64 iEndBl
2ff86 6f 63 6b 69 64 20 3d 20 73 71 6c 69 74 65 33 5f  ockid = sqlite3_
2ff87 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20  column_int64(s, 
2ff88 31 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  1);.  const char
2ff89 20 2a 70 52 6f 6f 74 44 61 74 61 20 3d 20 73 71   *pRootData = sq
2ff8a 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
2ff8b 62 28 73 2c 20 32 29 3b 0a 20 20 63 6f 6e 73 74  b(s, 2);.  const
2ff8c 20 69 6e 74 20 6e 52 6f 6f 74 44 61 74 61 20 3d   int nRootData =
2ff8d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
2ff8e 62 79 74 65 73 28 73 2c 20 32 29 3b 0a 20 20 4c  bytes(s, 2);.  L
2ff8f 65 61 76 65 73 52 65 61 64 65 72 20 72 65 61 64  eavesReader read
2ff90 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 6c  er;.  int rc = l
2ff91 65 61 76 65 73 52 65 61 64 65 72 49 6e 69 74 28  eavesReaderInit(
2ff92 76 2c 20 30 2c 20 69 53 74 61 72 74 42 6c 6f 63  v, 0, iStartBloc
2ff93 6b 69 64 2c 20 69 45 6e 64 42 6c 6f 63 6b 69 64  kid, iEndBlockid
2ff94 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2ff95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 52                pR
2ff96 6f 6f 74 44 61 74 61 2c 20 6e 52 6f 6f 74 44 61  ootData, nRootDa
2ff97 74 61 2c 20 26 72 65 61 64 65 72 29 3b 0a 20 20  ta, &reader);.  
2ff98 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2ff99 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
2ff9a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c    while( rc==SQL
2ff9b 49 54 45 5f 4f 4b 20 26 26 20 21 6c 65 61 76 65  ITE_OK && !leave
2ff9c 73 52 65 61 64 65 72 41 74 45 6e 64 28 26 72 65  sReaderAtEnd(&re
2ff9d 61 64 65 72 29 20 29 7b 0a 20 20 20 20 63 6f 6e  ader) ){.    con
2ff9e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 20 3d  st char *pTerm =
2ff9f 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72   leavesReaderTer
2ffa0 6d 28 26 72 65 61 64 65 72 29 3b 0a 20 20 20 20  m(&reader);.    
2ffa1 63 6f 6e 73 74 20 69 6e 74 20 6e 54 65 72 6d 20  const int nTerm 
2ffa2 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65  = leavesReaderTe
2ffa3 72 6d 42 79 74 65 73 28 26 72 65 61 64 65 72 29  rmBytes(&reader)
2ffa4 3b 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64 56  ;.    void *oldV
2ffa5 61 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 46 74  alue = sqlite3Ft
2ffa6 73 33 48 61 73 68 46 69 6e 64 28 70 54 65 72 6d  s3HashFind(pTerm
2ffa7 73 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29  s, pTerm, nTerm)
2ffa8 3b 0a 20 20 20 20 76 6f 69 64 20 2a 6e 65 77 56  ;.    void *newV
2ffa9 61 6c 75 65 20 3d 20 28 76 6f 69 64 20 2a 29 28  alue = (void *)(
2ffaa 28 63 68 61 72 20 2a 29 6f 6c 64 56 61 6c 75 65  (char *)oldValue
2ffab 2b 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 72 6f  +1);..    /* Fro
2ffac 6d 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 62 65  m the comment be
2ffad 66 6f 72 65 20 73 71 6c 69 74 65 33 46 74 73 33  fore sqlite3Fts3
2ffae 48 61 73 68 49 6e 73 65 72 74 20 69 6e 20 66 74  HashInsert in ft
2ffaf 73 33 5f 68 61 73 68 2e 63 2c 0a 20 20 20 20 2a  s3_hash.c,.    *
2ffb0 2a 20 74 68 65 20 64 61 74 61 20 76 61 6c 75 65  * the data value
2ffb1 20 70 61 73 73 65 64 20 69 73 20 72 65 74 75 72   passed is retur
2ffb2 6e 65 64 20 69 6e 20 63 61 73 65 20 6f 66 20 6d  ned in case of m
2ffb3 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 2e 0a 20  alloc failure.. 
2ffb4 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 65     */.    if( ne
2ffb5 77 56 61 6c 75 65 3d 3d 73 71 6c 69 74 65 33 46  wValue==sqlite3F
2ffb6 74 73 33 48 61 73 68 49 6e 73 65 72 74 28 70 54  ts3HashInsert(pT
2ffb7 65 72 6d 73 2c 20 70 54 65 72 6d 2c 20 6e 54 65  erms, pTerm, nTe
2ffb8 72 6d 2c 20 6e 65 77 56 61 6c 75 65 29 20 29 7b  rm, newValue) ){
2ffb9 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
2ffba 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65  TE_NOMEM;.    }e
2ffbb 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
2ffbc 6c 65 61 76 65 73 52 65 61 64 65 72 53 74 65 70  leavesReaderStep
2ffbd 28 76 2c 20 26 72 65 61 64 65 72 29 3b 0a 20 20  (v, &reader);.  
2ffbe 20 20 7d 0a 20 20 7d 0a 0a 20 20 6c 65 61 76 65    }.  }..  leave
2ffbf 73 52 65 61 64 65 72 44 65 73 74 72 6f 79 28 26  sReaderDestroy(&
2ffc0 72 65 61 64 65 72 29 3b 0a 20 20 72 65 74 75 72  reader);.  retur
2ffc1 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 48 65 6c 70  n rc;.}../* Help
2ffc2 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62  er function to b
2ffc3 75 69 6c 64 20 74 68 65 20 72 65 73 75 6c 74 20  uild the result 
2ffc4 73 74 72 69 6e 67 20 66 6f 72 20 64 75 6d 70 5f  string for dump_
2ffc5 74 65 72 6d 73 28 29 2e 20 2a 2f 0a 73 74 61 74  terms(). */.stat
2ffc6 69 63 20 69 6e 74 20 67 65 6e 65 72 61 74 65 54  ic int generateT
2ffc7 65 72 6d 73 52 65 73 75 6c 74 28 73 71 6c 69 74  ermsResult(sqlit
2ffc8 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e  e3_context *pCon
2ffc9 74 65 78 74 2c 20 66 74 73 33 48 61 73 68 20 2a  text, fts3Hash *
2ffca 70 54 65 72 6d 73 29 7b 0a 20 20 69 6e 74 20 69  pTerms){.  int i
2ffcb 54 65 72 6d 2c 20 6e 54 65 72 6d 73 2c 20 6e 52  Term, nTerms, nR
2ffcc 65 73 75 6c 74 42 79 74 65 73 2c 20 69 42 79 74  esultBytes, iByt
2ffcd 65 3b 0a 20 20 63 68 61 72 20 2a 72 65 73 75 6c  e;.  char *resul
2ffce 74 3b 0a 20 20 54 65 72 6d 44 61 74 61 20 2a 70  t;.  TermData *p
2ffcf 44 61 74 61 3b 0a 20 20 66 74 73 33 48 61 73 68  Data;.  fts3Hash
2ffd0 45 6c 65 6d 20 2a 65 3b 0a 0a 20 20 2f 2a 20 49  Elem *e;..  /* I
2ffd1 74 65 72 61 74 65 20 70 54 65 72 6d 73 20 74 6f  terate pTerms to
2ffd2 20 67 65 6e 65 72 61 74 65 20 61 6e 20 61 72 72   generate an arr
2ffd3 61 79 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 70  ay of terms in p
2ffd4 44 61 74 61 20 66 6f 72 0a 20 20 2a 2a 20 73 6f  Data for.  ** so
2ffd5 72 74 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 6e 54  rting..  */.  nT
2ffd6 65 72 6d 73 20 3d 20 66 74 73 33 48 61 73 68 43  erms = fts3HashC
2ffd7 6f 75 6e 74 28 70 54 65 72 6d 73 29 3b 0a 20 20  ount(pTerms);.  
2ffd8 61 73 73 65 72 74 28 20 6e 54 65 72 6d 73 3e 30  assert( nTerms>0
2ffd9 20 29 3b 0a 20 20 70 44 61 74 61 20 3d 20 73 71   );.  pData = sq
2ffda 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 54 65  lite3_malloc(nTe
2ffdb 72 6d 73 2a 73 69 7a 65 6f 66 28 54 65 72 6d 44  rms*sizeof(TermD
2ffdc 61 74 61 29 29 3b 0a 20 20 69 66 28 20 70 44 61  ata));.  if( pDa
2ffdd 74 61 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72  ta==NULL ) retur
2ffde 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
2ffdf 0a 20 20 6e 52 65 73 75 6c 74 42 79 74 65 73 20  .  nResultBytes 
2ffe0 3d 20 30 3b 0a 20 20 66 6f 72 28 69 54 65 72 6d  = 0;.  for(iTerm
2ffe1 20 3d 20 30 2c 20 65 20 3d 20 66 74 73 33 48 61   = 0, e = fts3Ha
2ffe2 73 68 46 69 72 73 74 28 70 54 65 72 6d 73 29 3b  shFirst(pTerms);
2ffe3 20 65 3b 20 69 54 65 72 6d 2b 2b 2c 20 65 20 3d   e; iTerm++, e =
2ffe4 20 66 74 73 33 48 61 73 68 4e 65 78 74 28 65 29   fts3HashNext(e)
2ffe5 29 7b 0a 20 20 20 20 6e 52 65 73 75 6c 74 42 79  ){.    nResultBy
2ffe6 74 65 73 20 2b 3d 20 66 74 73 33 48 61 73 68 4b  tes += fts3HashK
2ffe7 65 79 73 69 7a 65 28 65 29 2b 31 3b 20 20 20 2f  eysize(e)+1;   /
2ffe8 2a 20 54 65 72 6d 20 70 6c 75 73 20 74 72 61 69  * Term plus trai
2ffe9 6c 69 6e 67 20 73 70 61 63 65 20 2a 2f 0a 20 20  ling space */.  
2ffea 20 20 61 73 73 65 72 74 28 20 69 54 65 72 6d 3c    assert( iTerm<
2ffeb 6e 54 65 72 6d 73 20 29 3b 0a 20 20 20 20 70 44  nTerms );.    pD
2ffec 61 74 61 5b 69 54 65 72 6d 5d 2e 70 54 65 72 6d  ata[iTerm].pTerm
2ffed 20 3d 20 66 74 73 33 48 61 73 68 4b 65 79 28 65   = fts3HashKey(e
2ffee 29 3b 0a 20 20 20 20 70 44 61 74 61 5b 69 54 65  );.    pData[iTe
2ffef 72 6d 5d 2e 6e 54 65 72 6d 20 3d 20 66 74 73 33  rm].nTerm = fts3
2fff0 48 61 73 68 4b 65 79 73 69 7a 65 28 65 29 3b 0a  HashKeysize(e);.
2fff1 20 20 20 20 70 44 61 74 61 5b 69 54 65 72 6d 5d      pData[iTerm]
2fff2 2e 70 43 6f 6c 6c 65 63 74 6f 72 20 3d 20 66 74  .pCollector = ft
2fff3 73 33 48 61 73 68 44 61 74 61 28 65 29 3b 20 20  s3HashData(e);  
2fff4 2f 2a 20 75 6e 75 73 65 64 20 2a 2f 0a 20 20 7d  /* unused */.  }
2fff5 0a 20 20 61 73 73 65 72 74 28 20 69 54 65 72 6d  .  assert( iTerm
2fff6 3d 3d 6e 54 65 72 6d 73 20 29 3b 0a 0a 20 20 61  ==nTerms );..  a
2fff7 73 73 65 72 74 28 20 6e 52 65 73 75 6c 74 42 79  ssert( nResultBy
2fff8 74 65 73 3e 30 20 29 3b 20 20 20 2f 2a 20 6e 54  tes>0 );   /* nT
2fff9 65 72 6d 73 3e 30 2c 20 6e 52 65 73 75 6c 74 73  erms>0, nResults
2fffa 42 79 74 65 73 20 6d 75 73 74 20 62 65 2c 20 74  Bytes must be, t
2fffb 6f 6f 2e 20 2a 2f 0a 20 20 72 65 73 75 6c 74 20  oo. */.  result 
2fffc 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
2fffd 28 6e 52 65 73 75 6c 74 42 79 74 65 73 29 3b 0a  (nResultBytes);.
2fffe 20 20 69 66 28 20 72 65 73 75 6c 74 3d 3d 4e 55    if( result==NU
2ffff 4c 4c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  LL ){.    sqlite
30000 33 5f 66 72 65 65 28 70 44 61 74 61 29 3b 0a 20  3_free(pData);. 
30001 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
30002 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69  _NOMEM;.  }..  i
30003 66 28 20 6e 54 65 72 6d 73 3e 31 20 29 20 71 73  f( nTerms>1 ) qs
30004 6f 72 74 28 70 44 61 74 61 2c 20 6e 54 65 72 6d  ort(pData, nTerm
30005 73 2c 20 73 69 7a 65 6f 66 28 2a 70 44 61 74 61  s, sizeof(*pData
30006 29 2c 20 74 65 72 6d 44 61 74 61 43 6d 70 29 3b  ), termDataCmp);
30007 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20  ..  /* Read the 
30008 74 65 72 6d 73 20 69 6e 20 6f 72 64 65 72 20 74  terms in order t
30009 6f 20 62 75 69 6c 64 20 74 68 65 20 72 65 73 75  o build the resu
3000a 6c 74 2e 20 2a 2f 0a 20 20 69 42 79 74 65 20 3d  lt. */.  iByte =
3000b 20 30 3b 0a 20 20 66 6f 72 28 69 54 65 72 6d 3d   0;.  for(iTerm=
3000c 30 3b 20 69 54 65 72 6d 3c 6e 54 65 72 6d 73 3b  0; iTerm<nTerms;
3000d 20 2b 2b 69 54 65 72 6d 29 7b 0a 20 20 20 20 6d   ++iTerm){.    m
3000e 65 6d 63 70 79 28 72 65 73 75 6c 74 2b 69 42 79  emcpy(result+iBy
3000f 74 65 2c 20 70 44 61 74 61 5b 69 54 65 72 6d 5d  te, pData[iTerm]
30010 2e 70 54 65 72 6d 2c 20 70 44 61 74 61 5b 69 54  .pTerm, pData[iT
30011 65 72 6d 5d 2e 6e 54 65 72 6d 29 3b 0a 20 20 20  erm].nTerm);.   
30012 20 69 42 79 74 65 20 2b 3d 20 70 44 61 74 61 5b   iByte += pData[
30013 69 54 65 72 6d 5d 2e 6e 54 65 72 6d 3b 0a 20 20  iTerm].nTerm;.  
30014 20 20 72 65 73 75 6c 74 5b 69 42 79 74 65 2b 2b    result[iByte++
30015 5d 20 3d 20 27 20 27 3b 0a 20 20 7d 0a 20 20 61  ] = ' ';.  }.  a
30016 73 73 65 72 74 28 20 69 42 79 74 65 3d 3d 6e 52  ssert( iByte==nR
30017 65 73 75 6c 74 42 79 74 65 73 20 29 3b 0a 20 20  esultBytes );.  
30018 61 73 73 65 72 74 28 20 72 65 73 75 6c 74 5b 6e  assert( result[n
30019 52 65 73 75 6c 74 42 79 74 65 73 2d 31 5d 3d 3d  ResultBytes-1]==
3001a 27 20 27 20 29 3b 0a 20 20 72 65 73 75 6c 74 5b  ' ' );.  result[
3001b 6e 52 65 73 75 6c 74 42 79 74 65 73 2d 31 5d 20  nResultBytes-1] 
3001c 3d 20 27 5c 30 27 3b 0a 0a 20 20 2f 2a 20 50 61  = '\0';..  /* Pa
3001d 73 73 65 73 20 61 77 61 79 20 6f 77 6e 65 72 73  sses away owners
3001e 68 69 70 20 6f 66 20 72 65 73 75 6c 74 2e 20 2a  hip of result. *
3001f 2f 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  /.  sqlite3_resu
30020 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74 65 78 74  lt_text(pContext
30021 2c 20 72 65 73 75 6c 74 2c 20 6e 52 65 73 75 6c  , result, nResul
30022 74 42 79 74 65 73 2d 31 2c 20 73 71 6c 69 74 65  tBytes-1, sqlite
30023 33 5f 66 72 65 65 29 3b 0a 20 20 73 71 6c 69 74  3_free);.  sqlit
30024 65 33 5f 66 72 65 65 28 70 44 61 74 61 29 3b 0a  e3_free(pData);.
30025 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
30026 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 49 6d 70 6c 65 6d  OK;.}../* Implem
30027 65 6e 74 73 20 64 75 6d 70 5f 74 65 72 6d 73 28  ents dump_terms(
30028 29 20 66 6f 72 20 75 73 65 20 69 6e 20 69 6e 73  ) for use in ins
30029 70 65 63 74 69 6e 67 20 74 68 65 20 66 74 73 33  pecting the fts3
3002a 20 69 6e 64 65 78 20 66 72 6f 6d 0a 2a 2a 20 74   index from.** t
3002b 65 73 74 73 2e 20 20 54 45 58 54 20 72 65 73 75  ests.  TEXT resu
3002c 6c 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  lt containing th
3002d 65 20 6f 72 64 65 72 65 64 20 6c 69 73 74 20 6f  e ordered list o
3002e 66 20 74 65 72 6d 73 20 6a 6f 69 6e 65 64 20 62  f terms joined b
3002f 79 0a 2a 2a 20 73 70 61 63 65 73 2e 20 20 64 75  y.** spaces.  du
30030 6d 70 5f 74 65 72 6d 73 28 74 2c 20 6c 65 76 65  mp_terms(t, leve
30031 6c 2c 20 69 64 78 29 20 64 75 6d 70 73 20 74 68  l, idx) dumps th
30032 65 20 74 65 72 6d 73 20 66 6f 72 20 74 68 65 20  e terms for the 
30033 73 65 67 6d 65 6e 74 0a 2a 2a 20 73 70 65 63 69  segment.** speci
30034 66 69 65 64 20 62 79 20 6c 65 76 65 6c 2c 20 69  fied by level, i
30035 64 78 20 28 69 6e 20 25 5f 73 65 67 64 69 72 29  dx (in %_segdir)
30036 2c 20 77 68 69 6c 65 20 64 75 6d 70 5f 74 65 72  , while dump_ter
30037 6d 73 28 74 29 20 64 75 6d 70 73 0a 2a 2a 20 61  ms(t) dumps.** a
30038 6c 6c 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20  ll terms in the 
30039 69 6e 64 65 78 2e 20 20 49 6e 20 62 6f 74 68 20  index.  In both 
3003a 63 61 73 65 73 20 74 20 69 73 20 74 68 65 20 66  cases t is the f
3003b 74 73 20 74 61 62 6c 65 27 73 20 6d 61 67 69 63  ts table's magic
3003c 0a 2a 2a 20 74 61 62 6c 65 2d 6e 61 6d 65 64 20  .** table-named 
3003d 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69  column..*/.stati
3003e 63 20 76 6f 69 64 20 64 75 6d 70 54 65 72 6d 73  c void dumpTerms
3003f 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
30040 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78  context *pContex
30041 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 73  t,.  int argc, s
30042 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
30043 72 67 76 0a 29 7b 0a 20 20 66 75 6c 6c 74 65 78  rgv.){.  fulltex
30044 74 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  t_cursor *pCurso
30045 72 3b 0a 20 20 69 66 28 20 61 72 67 63 21 3d 33  r;.  if( argc!=3
30046 20 26 26 20 61 72 67 63 21 3d 31 20 29 7b 0a 20   && argc!=1 ){. 
30047 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72     generateError
30048 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70  (pContext, "dump
30049 5f 74 65 72 6d 73 22 2c 20 22 69 6e 63 6f 72 72  _terms", "incorr
3004a 65 63 74 20 61 72 67 75 6d 65 6e 74 73 22 29 3b  ect arguments");
3004b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c  .  }else if( sql
3004c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
3004d 61 72 67 76 5b 30 5d 29 21 3d 53 51 4c 49 54 45  argv[0])!=SQLITE
3004e 5f 42 4c 4f 42 20 7c 7c 0a 20 20 20 20 20 20 20  _BLOB ||.       
3004f 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c       sqlite3_val
30050 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
30051 29 21 3d 73 69 7a 65 6f 66 28 70 43 75 72 73 6f  )!=sizeof(pCurso
30052 72 29 20 29 7b 0a 20 20 20 20 67 65 6e 65 72 61  r) ){.    genera
30053 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74  teError(pContext
30054 2c 20 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c 20  , "dump_terms", 
30055 22 69 6c 6c 65 67 61 6c 20 66 69 72 73 74 20 61  "illegal first a
30056 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 65 6c  rgument");.  }el
30057 73 65 7b 0a 20 20 20 20 66 75 6c 6c 74 65 78 74  se{.    fulltext
30058 5f 76 74 61 62 20 2a 76 3b 0a 20 20 20 20 66 74  _vtab *v;.    ft
30059 73 33 48 61 73 68 20 74 65 72 6d 73 3b 0a 20 20  s3Hash terms;.  
3005a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
3005b 73 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e  s = NULL;.    in
3005c 74 20 72 63 3b 0a 0a 20 20 20 20 6d 65 6d 63 70  t rc;..    memcp
3005d 79 28 26 70 43 75 72 73 6f 72 2c 20 73 71 6c 69  y(&pCursor, sqli
3005e 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61  te3_value_blob(a
3005f 72 67 76 5b 30 5d 29 2c 20 73 69 7a 65 6f 66 28  rgv[0]), sizeof(
30060 70 43 75 72 73 6f 72 29 29 3b 0a 20 20 20 20 76  pCursor));.    v
30061 20 3d 20 63 75 72 73 6f 72 5f 76 74 61 62 28 70   = cursor_vtab(p
30062 43 75 72 73 6f 72 29 3b 0a 0a 20 20 20 20 2f 2a  Cursor);..    /*
30063 20 49 66 20 70 61 73 73 65 64 20 6f 6e 6c 79 20   If passed only 
30064 74 68 65 20 63 75 72 73 6f 72 20 63 6f 6c 75 6d  the cursor colum
30065 6e 2c 20 67 65 74 20 61 6c 6c 20 73 65 67 6d 65  n, get all segme
30066 6e 74 73 2e 20 20 4f 74 68 65 72 77 69 73 65 0a  nts.  Otherwise.
30067 20 20 20 20 2a 2a 20 67 65 74 20 74 68 65 20 73      ** get the s
30068 65 67 6d 65 6e 74 20 64 65 73 63 72 69 62 65 64  egment described
30069 20 62 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   by the followin
3006a 67 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e  g two arguments.
3006b 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
3006c 61 72 67 63 3d 3d 31 20 29 7b 0a 20 20 20 20 20  argc==1 ){.     
3006d 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74   rc = sql_get_st
3006e 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49  atement(v, SEGDI
3006f 52 5f 53 45 4c 45 43 54 5f 41 4c 4c 5f 53 54 4d  R_SELECT_ALL_STM
30070 54 2c 20 26 73 29 3b 0a 20 20 20 20 7d 65 6c 73  T, &s);.    }els
30071 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  e{.      rc = sq
30072 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28  l_get_statement(
30073 76 2c 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54  v, SEGDIR_SELECT
30074 5f 53 45 47 4d 45 4e 54 5f 53 54 4d 54 2c 20 26  _SEGMENT_STMT, &
30075 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  s);.      if( rc
30076 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
30077 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
30078 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20  te3_bind_int(s, 
30079 31 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  1, sqlite3_value
3007a 5f 69 6e 74 28 61 72 67 76 5b 31 5d 29 29 3b 0a  _int(argv[1]));.
3007b 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
3007c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
3007d 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
3007e 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20  te3_bind_int(s, 
3007f 32 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  2, sqlite3_value
30080 5f 69 6e 74 28 61 72 67 76 5b 32 5d 29 29 3b 0a  _int(argv[2]));.
30081 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
30082 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  }.    }..    if(
30083 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
30084 7b 0a 20 20 20 20 20 20 67 65 6e 65 72 61 74 65  {.      generate
30085 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20  Error(pContext, 
30086 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c 20 4e 55  "dump_terms", NU
30087 4c 4c 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  LL);.      retur
30088 6e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  n;.    }..    /*
30089 20 43 6f 6c 6c 65 63 74 20 74 68 65 20 74 65 72   Collect the ter
3008a 6d 73 20 66 6f 72 20 65 61 63 68 20 73 65 67 6d  ms for each segm
3008b 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69  ent. */.    sqli
3008c 74 65 33 46 74 73 33 48 61 73 68 49 6e 69 74 28  te3Fts3HashInit(
3008d 26 74 65 72 6d 73 2c 20 46 54 53 33 5f 48 41 53  &terms, FTS3_HAS
3008e 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0a 20 20  H_STRING, 1);.  
3008f 20 20 77 68 69 6c 65 28 20 28 72 63 20 3d 20 73    while( (rc = s
30090 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 29 3d  qlite3_step(s))=
30091 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
30092 20 20 20 20 20 72 63 20 3d 20 63 6f 6c 6c 65 63       rc = collec
30093 74 53 65 67 6d 65 6e 74 54 65 72 6d 73 28 76 2c  tSegmentTerms(v,
30094 20 73 2c 20 26 74 65 72 6d 73 29 3b 0a 20 20 20   s, &terms);.   
30095 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
30096 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b 0a 20 20  E_OK ) break;.  
30097 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 21    }..    if( rc!
30098 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a  =SQLITE_DONE ){.
30099 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
3009a 73 65 74 28 73 29 3b 0a 20 20 20 20 20 20 67 65  set(s);.      ge
3009b 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e  nerateError(pCon
3009c 74 65 78 74 2c 20 22 64 75 6d 70 5f 74 65 72 6d  text, "dump_term
3009d 73 22 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 7d  s", NULL);.    }
3009e 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 6f 6e 73  else{.      cons
3009f 74 20 69 6e 74 20 6e 54 65 72 6d 73 20 3d 20 66  t int nTerms = f
300a0 74 73 33 48 61 73 68 43 6f 75 6e 74 28 26 74 65  ts3HashCount(&te
300a1 72 6d 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20  rms);.      if( 
300a2 6e 54 65 72 6d 73 3e 30 20 29 7b 0a 20 20 20 20  nTerms>0 ){.    
300a3 20 20 20 20 72 63 20 3d 20 67 65 6e 65 72 61 74      rc = generat
300a4 65 54 65 72 6d 73 52 65 73 75 6c 74 28 70 43 6f  eTermsResult(pCo
300a5 6e 74 65 78 74 2c 20 26 74 65 72 6d 73 29 3b 0a  ntext, &terms);.
300a6 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
300a7 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a  SQLITE_NOMEM ){.
300a8 20 20 20 20 20 20 20 20 20 20 67 65 6e 65 72 61            genera
300a9 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74  teError(pContext
300aa 2c 20 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c 20  , "dump_terms", 
300ab 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29  "out of memory")
300ac 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
300ad 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
300ae 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
300af 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   );.        }.  
300b0 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 61 72      }else if( ar
300b1 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20 20 20 20  gc==3 ){.       
300b2 20 2f 2a 20 54 68 65 20 73 70 65 63 69 66 69 63   /* The specific
300b3 20 73 65 67 6d 65 6e 74 20 61 73 6b 65 64 20 66   segment asked f
300b4 6f 72 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  or could not be 
300b5 66 6f 75 6e 64 2e 20 2a 2f 0a 20 20 20 20 20 20  found. */.      
300b6 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72 28    generateError(
300b7 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f  pContext, "dump_
300b8 74 65 72 6d 73 22 2c 20 22 73 65 67 6d 65 6e 74  terms", "segment
300b9 20 6e 6f 74 20 66 6f 75 6e 64 22 29 3b 0a 20 20   not found");.  
300ba 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
300bb 20 20 20 2f 2a 20 4e 6f 20 73 65 67 6d 65 6e 74     /* No segment
300bc 73 20 66 6f 75 6e 64 2e 20 2a 2f 0a 20 20 20 20  s found. */.    
300bd 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73      /* TODO(shes
300be 73 29 3a 20 49 74 20 73 68 6f 75 6c 64 20 62 65  s): It should be
300bf 20 69 6d 70 6f 73 73 69 62 6c 65 20 74 6f 20 72   impossible to r
300c0 65 61 63 68 20 74 68 69 73 2e 20 20 54 68 69 73  each this.  This
300c1 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 61 73 65  .        ** case
300c2 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e   can only happen
300c3 20 66 6f 72 20 61 6e 20 65 6d 70 74 79 20 74 61   for an empty ta
300c4 62 6c 65 2c 20 69 6e 20 77 68 69 63 68 20 63 61  ble, in which ca
300c5 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 53 51  se.        ** SQ
300c6 4c 69 74 65 20 68 61 73 20 6e 6f 20 72 6f 77 73  Lite has no rows
300c7 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 66 75   to call this fu
300c8 6e 63 74 69 6f 6e 20 6f 6e 2e 0a 20 20 20 20 20  nction on..     
300c9 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71     */.        sq
300ca 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
300cb 6c 28 70 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20  l(pContext);.   
300cc 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
300cd 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 43 6c  qlite3Fts3HashCl
300ce 65 61 72 28 26 74 65 72 6d 73 29 3b 0a 20 20 7d  ear(&terms);.  }
300cf 0a 7d 0a 0a 2f 2a 20 45 78 70 61 6e 64 20 74 68  .}../* Expand th
300d0 65 20 44 4c 5f 44 45 46 41 55 4c 54 20 64 6f 63  e DL_DEFAULT doc
300d1 6c 69 73 74 20 69 6e 20 70 44 61 74 61 20 69 6e  list in pData in
300d2 74 6f 20 61 20 74 65 78 74 20 72 65 73 75 6c 74  to a text result
300d3 20 69 6e 0a 2a 2a 20 70 43 6f 6e 74 65 78 74 2e   in.** pContext.
300d4 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
300d5 63 72 65 61 74 65 44 6f 63 6c 69 73 74 52 65 73  createDoclistRes
300d6 75 6c 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ult(sqlite3_cont
300d7 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20  ext *pContext,. 
300d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
300d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
300da 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61  onst char *pData
300db 2c 20 69 6e 74 20 6e 44 61 74 61 29 7b 0a 20 20  , int nData){.  
300dc 44 61 74 61 42 75 66 66 65 72 20 64 75 6d 70 3b  DataBuffer dump;
300dd 0a 20 20 44 4c 52 65 61 64 65 72 20 64 6c 52 65  .  DLReader dlRe
300de 61 64 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 28  ader;..  assert(
300df 20 70 44 61 74 61 21 3d 4e 55 4c 4c 20 26 26 20   pData!=NULL && 
300e0 6e 44 61 74 61 3e 30 20 29 3b 0a 0a 20 20 64 61  nData>0 );..  da
300e1 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 64 75  taBufferInit(&du
300e2 6d 70 2c 20 30 29 3b 0a 20 20 64 6c 72 49 6e 69  mp, 0);.  dlrIni
300e3 74 28 26 64 6c 52 65 61 64 65 72 2c 20 44 4c 5f  t(&dlReader, DL_
300e4 44 45 46 41 55 4c 54 2c 20 70 44 61 74 61 2c 20  DEFAULT, pData, 
300e5 6e 44 61 74 61 29 3b 0a 20 20 66 6f 72 28 20 3b  nData);.  for( ;
300e6 20 21 64 6c 72 41 74 45 6e 64 28 26 64 6c 52 65   !dlrAtEnd(&dlRe
300e7 61 64 65 72 29 3b 20 64 6c 72 53 74 65 70 28 26  ader); dlrStep(&
300e8 64 6c 52 65 61 64 65 72 29 20 29 7b 0a 20 20 20  dlReader) ){.   
300e9 20 63 68 61 72 20 62 75 66 5b 32 35 36 5d 3b 0a   char buf[256];.
300ea 20 20 20 20 50 4c 52 65 61 64 65 72 20 70 6c 52      PLReader plR
300eb 65 61 64 65 72 3b 0a 0a 20 20 20 20 70 6c 72 49  eader;..    plrI
300ec 6e 69 74 28 26 70 6c 52 65 61 64 65 72 2c 20 26  nit(&plReader, &
300ed 64 6c 52 65 61 64 65 72 29 3b 0a 20 20 20 20 69  dlReader);.    i
300ee 66 28 20 44 4c 5f 44 45 46 41 55 4c 54 3d 3d 44  f( DL_DEFAULT==D
300ef 4c 5f 44 4f 43 49 44 53 20 7c 7c 20 70 6c 72 41  L_DOCIDS || plrA
300f0 74 45 6e 64 28 26 70 6c 52 65 61 64 65 72 29 20  tEnd(&plReader) 
300f1 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
300f2 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66  _snprintf(sizeof
300f3 28 62 75 66 29 2c 20 62 75 66 2c 20 22 5b 25 6c  (buf), buf, "[%l
300f4 6c 64 5d 20 22 2c 20 64 6c 72 44 6f 63 69 64 28  ld] ", dlrDocid(
300f5 26 64 6c 52 65 61 64 65 72 29 29 3b 0a 20 20 20  &dlReader));.   
300f6 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70     dataBufferApp
300f7 65 6e 64 28 26 64 75 6d 70 2c 20 62 75 66 2c 20  end(&dump, buf, 
300f8 73 74 72 6c 65 6e 28 62 75 66 29 29 3b 0a 20 20  strlen(buf));.  
300f9 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
300fa 6e 74 20 69 43 6f 6c 75 6d 6e 20 3d 20 70 6c 72  nt iColumn = plr
300fb 43 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64 65 72  Column(&plReader
300fc 29 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
300fd 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
300fe 66 28 62 75 66 29 2c 20 62 75 66 2c 20 22 5b 25  f(buf), buf, "[%
300ff 6c 6c 64 20 25 64 5b 22 2c 0a 20 20 20 20 20 20  lld %d[",.      
30100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30101 20 64 6c 72 44 6f 63 69 64 28 26 64 6c 52 65 61   dlrDocid(&dlRea
30102 64 65 72 29 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a  der), iColumn);.
30103 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72        dataBuffer
30104 41 70 70 65 6e 64 28 26 64 75 6d 70 2c 20 62 75  Append(&dump, bu
30105 66 2c 20 73 74 72 6c 65 6e 28 62 75 66 29 29 3b  f, strlen(buf));
30106 0a 0a 20 20 20 20 20 20 66 6f 72 28 20 3b 20 21  ..      for( ; !
30107 70 6c 72 41 74 45 6e 64 28 26 70 6c 52 65 61 64  plrAtEnd(&plRead
30108 65 72 29 3b 20 70 6c 72 53 74 65 70 28 26 70 6c  er); plrStep(&pl
30109 52 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20 20  Reader) ){.     
3010a 20 20 20 69 66 28 20 70 6c 72 43 6f 6c 75 6d 6e     if( plrColumn
3010b 28 26 70 6c 52 65 61 64 65 72 29 21 3d 69 43 6f  (&plReader)!=iCo
3010c 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20 20  lumn ){.        
3010d 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 70 6c 72 43    iColumn = plrC
3010e 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64 65 72 29  olumn(&plReader)
3010f 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
30110 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
30111 65 6f 66 28 62 75 66 29 2c 20 62 75 66 2c 20 22  eof(buf), buf, "
30112 5d 20 25 64 5b 22 2c 20 69 43 6f 6c 75 6d 6e 29  ] %d[", iColumn)
30113 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  ;.          asse
30114 72 74 28 20 64 75 6d 70 2e 6e 44 61 74 61 3e 30  rt( dump.nData>0
30115 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 64 75   );.          du
30116 6d 70 2e 6e 44 61 74 61 2d 2d 3b 20 20 20 20 20  mp.nData--;     
30117 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30118 2f 2a 20 4f 76 65 72 77 72 69 74 65 20 74 72 61  /* Overwrite tra
30119 69 6c 69 6e 67 20 73 70 61 63 65 2e 20 2a 2f 0a  iling space. */.
3011a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
3011b 28 20 64 75 6d 70 2e 70 44 61 74 61 5b 64 75 6d  ( dump.pData[dum
3011c 70 2e 6e 44 61 74 61 5d 3d 3d 27 20 27 29 3b 0a  p.nData]==' ');.
3011d 20 20 20 20 20 20 20 20 20 20 64 61 74 61 42 75            dataBu
3011e 66 66 65 72 41 70 70 65 6e 64 28 26 64 75 6d 70  fferAppend(&dump
3011f 2c 20 62 75 66 2c 20 73 74 72 6c 65 6e 28 62 75  , buf, strlen(bu
30120 66 29 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  f));.        }. 
30121 20 20 20 20 20 20 20 69 66 28 20 44 4c 5f 44 45         if( DL_DE
30122 46 41 55 4c 54 3d 3d 44 4c 5f 50 4f 53 49 54 49  FAULT==DL_POSITI
30123 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29 7b 0a 20  ONS_OFFSETS ){. 
30124 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
30125 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66  _snprintf(sizeof
30126 28 62 75 66 29 2c 20 62 75 66 2c 20 22 25 64 2c  (buf), buf, "%d,
30127 25 64 2c 25 64 20 22 2c 0a 20 20 20 20 20 20 20  %d,%d ",.       
30128 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30129 20 20 20 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28      plrPosition(
3012a 26 70 6c 52 65 61 64 65 72 29 2c 0a 20 20 20 20  &plReader),.    
3012b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3012c 20 20 20 20 20 20 20 70 6c 72 53 74 61 72 74 4f         plrStartO
3012d 66 66 73 65 74 28 26 70 6c 52 65 61 64 65 72 29  ffset(&plReader)
3012e 2c 20 70 6c 72 45 6e 64 4f 66 66 73 65 74 28 26  , plrEndOffset(&
3012f 70 6c 52 65 61 64 65 72 29 29 3b 0a 20 20 20 20  plReader));.    
30130 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 44 4c      }else if( DL
30131 5f 44 45 46 41 55 4c 54 3d 3d 44 4c 5f 50 4f 53  _DEFAULT==DL_POS
30132 49 54 49 4f 4e 53 20 29 7b 0a 20 20 20 20 20 20  ITIONS ){.      
30133 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
30134 69 6e 74 66 28 73 69 7a 65 6f 66 28 62 75 66 29  intf(sizeof(buf)
30135 2c 20 62 75 66 2c 20 22 25 64 20 22 2c 20 70 6c  , buf, "%d ", pl
30136 72 50 6f 73 69 74 69 6f 6e 28 26 70 6c 52 65 61  rPosition(&plRea
30137 64 65 72 29 29 3b 0a 20 20 20 20 20 20 20 20 7d  der));.        }
30138 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
30139 61 73 73 65 72 74 28 20 4e 55 4c 4c 3d 3d 22 55  assert( NULL=="U
3013a 6e 68 61 6e 64 6c 65 64 20 44 4c 5f 44 45 46 41  nhandled DL_DEFA
3013b 55 4c 54 20 76 61 6c 75 65 22 29 3b 0a 20 20 20  ULT value");.   
3013c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64       }.        d
3013d 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28  ataBufferAppend(
3013e 26 64 75 6d 70 2c 20 62 75 66 2c 20 73 74 72 6c  &dump, buf, strl
3013f 65 6e 28 62 75 66 29 29 3b 0a 20 20 20 20 20 20  en(buf));.      
30140 7d 0a 20 20 20 20 20 20 70 6c 72 44 65 73 74 72  }.      plrDestr
30141 6f 79 28 26 70 6c 52 65 61 64 65 72 29 3b 0a 0a  oy(&plReader);..
30142 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 75        assert( du
30143 6d 70 2e 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20  mp.nData>0 );.  
30144 20 20 20 20 64 75 6d 70 2e 6e 44 61 74 61 2d 2d      dump.nData--
30145 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
30146 20 20 20 20 20 20 2f 2a 20 4f 76 65 72 77 72 69        /* Overwri
30147 74 65 20 74 72 61 69 6c 69 6e 67 20 73 70 61 63  te trailing spac
30148 65 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65  e. */.      asse
30149 72 74 28 20 64 75 6d 70 2e 70 44 61 74 61 5b 64  rt( dump.pData[d
3014a 75 6d 70 2e 6e 44 61 74 61 5d 3d 3d 27 20 27 29  ump.nData]==' ')
3014b 3b 0a 20 20 20 20 20 20 64 61 74 61 42 75 66 66  ;.      dataBuff
3014c 65 72 41 70 70 65 6e 64 28 26 64 75 6d 70 2c 20  erAppend(&dump, 
3014d 22 5d 5d 20 22 2c 20 33 29 3b 0a 20 20 20 20 7d  "]] ", 3);.    }
3014e 0a 20 20 7d 0a 20 20 64 6c 72 44 65 73 74 72 6f  .  }.  dlrDestro
3014f 79 28 26 64 6c 52 65 61 64 65 72 29 3b 0a 0a 20  y(&dlReader);.. 
30150 20 61 73 73 65 72 74 28 20 64 75 6d 70 2e 6e 44   assert( dump.nD
30151 61 74 61 3e 30 20 29 3b 0a 20 20 64 75 6d 70 2e  ata>0 );.  dump.
30152 6e 44 61 74 61 2d 2d 3b 20 20 20 20 20 20 20 20  nData--;        
30153 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
30154 4f 76 65 72 77 72 69 74 65 20 74 72 61 69 6c 69  Overwrite traili
30155 6e 67 20 73 70 61 63 65 2e 20 2a 2f 0a 20 20 61  ng space. */.  a
30156 73 73 65 72 74 28 20 64 75 6d 70 2e 70 44 61 74  ssert( dump.pDat
30157 61 5b 64 75 6d 70 2e 6e 44 61 74 61 5d 3d 3d 27  a[dump.nData]=='
30158 20 27 29 3b 0a 20 20 64 75 6d 70 2e 70 44 61 74   ');.  dump.pDat
30159 61 5b 64 75 6d 70 2e 6e 44 61 74 61 5d 20 3d 20  a[dump.nData] = 
3015a 27 5c 30 27 3b 0a 20 20 61 73 73 65 72 74 28 20  '\0';.  assert( 
3015b 64 75 6d 70 2e 6e 44 61 74 61 3e 30 20 29 3b 0a  dump.nData>0 );.
3015c 0a 20 20 2f 2a 20 50 61 73 73 65 73 20 6f 77 6e  .  /* Passes own
3015d 65 72 73 68 69 70 20 6f 66 20 64 75 6d 70 27 73  ership of dump's
3015e 20 62 75 66 66 65 72 20 74 6f 20 70 43 6f 6e 74   buffer to pCont
3015f 65 78 74 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ext. */.  sqlite
30160 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43  3_result_text(pC
30161 6f 6e 74 65 78 74 2c 20 64 75 6d 70 2e 70 44 61  ontext, dump.pDa
30162 74 61 2c 20 64 75 6d 70 2e 6e 44 61 74 61 2c 20  ta, dump.nData, 
30163 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20  sqlite3_free);. 
30164 20 64 75 6d 70 2e 70 44 61 74 61 20 3d 20 4e 55   dump.pData = NU
30165 4c 4c 3b 0a 20 20 64 75 6d 70 2e 6e 44 61 74 61  LL;.  dump.nData
30166 20 3d 20 64 75 6d 70 2e 6e 43 61 70 61 63 69 74   = dump.nCapacit
30167 79 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 20 49 6d 70  y = 0;.}../* Imp
30168 6c 65 6d 65 6e 74 73 20 64 75 6d 70 5f 64 6f 63  lements dump_doc
30169 6c 69 73 74 28 29 20 66 6f 72 20 75 73 65 20 69  list() for use i
3016a 6e 20 69 6e 73 70 65 63 74 69 6e 67 20 74 68 65  n inspecting the
3016b 20 66 74 73 33 20 69 6e 64 65 78 20 66 72 6f 6d   fts3 index from
3016c 0a 2a 2a 20 74 65 73 74 73 2e 20 20 54 45 58 54  .** tests.  TEXT
3016d 20 72 65 73 75 6c 74 20 63 6f 6e 74 61 69 6e 69   result containi
3016e 6e 67 20 61 20 73 74 72 69 6e 67 20 72 65 70 72  ng a string repr
3016f 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  esentation of th
30170 65 0a 2a 2a 20 64 6f 63 6c 69 73 74 20 66 6f 72  e.** doclist for
30171 20 74 68 65 20 69 6e 64 69 63 61 74 65 64 20 74   the indicated t
30172 65 72 6d 2e 20 20 64 75 6d 70 5f 64 6f 63 6c 69  erm.  dump_docli
30173 73 74 28 74 2c 20 74 65 72 6d 2c 20 6c 65 76 65  st(t, term, leve
30174 6c 2c 20 69 64 78 29 0a 2a 2a 20 64 75 6d 70 73  l, idx).** dumps
30175 20 74 68 65 20 64 6f 63 6c 69 73 74 20 66 6f 72   the doclist for
30176 20 74 65 72 6d 20 66 72 6f 6d 20 74 68 65 20 73   term from the s
30177 65 67 6d 65 6e 74 20 73 70 65 63 69 66 69 65 64  egment specified
30178 20 62 79 20 6c 65 76 65 6c 2c 20 69 64 78 0a 2a   by level, idx.*
30179 2a 20 28 69 6e 20 25 5f 73 65 67 64 69 72 29 2c  * (in %_segdir),
3017a 20 77 68 69 6c 65 20 64 75 6d 70 5f 64 6f 63 6c   while dump_docl
3017b 69 73 74 28 74 2c 20 74 65 72 6d 29 20 64 75 6d  ist(t, term) dum
3017c 70 73 20 74 68 65 20 6c 6f 67 69 63 61 6c 0a 2a  ps the logical.*
3017d 2a 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 74 68  * doclist for th
3017e 65 20 74 65 72 6d 20 61 63 72 6f 73 73 20 61 6c  e term across al
3017f 6c 20 73 65 67 6d 65 6e 74 73 2e 20 20 54 68 65  l segments.  The
30180 20 70 65 72 2d 73 65 67 6d 65 6e 74 20 64 6f 63   per-segment doc
30181 6c 69 73 74 0a 2a 2a 20 63 61 6e 20 63 6f 6e 74  list.** can cont
30182 61 69 6e 20 64 65 6c 65 74 69 6f 6e 73 2c 20 77  ain deletions, w
30183 68 69 6c 65 20 74 68 65 20 66 75 6c 6c 2d 69 6e  hile the full-in
30184 64 65 78 20 64 6f 63 6c 69 73 74 20 77 69 6c 6c  dex doclist will
30185 20 6e 6f 74 0a 2a 2a 20 28 64 65 6c 65 74 69 6f   not.** (deletio
30186 6e 73 20 61 72 65 20 6f 6d 69 74 74 65 64 29 2e  ns are omitted).
30187 0a 2a 2a 0a 2a 2a 20 52 65 73 75 6c 74 20 66 6f  .**.** Result fo
30188 72 6d 61 74 73 20 64 69 66 66 65 72 20 77 69 74  rmats differ wit
30189 68 20 74 68 65 20 73 65 74 74 69 6e 67 20 6f 66  h the setting of
3018a 20 44 4c 5f 44 45 46 41 55 4c 54 53 2e 20 20 45   DL_DEFAULTS.  E
3018b 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 44  xamples:.**.** D
3018c 4c 5f 44 4f 43 49 44 53 3a 20 5b 31 5d 20 5b 33  L_DOCIDS: [1] [3
3018d 5d 20 5b 37 5d 0a 2a 2a 20 44 4c 5f 50 4f 53 49  ] [7].** DL_POSI
3018e 54 49 4f 4e 53 3a 20 5b 31 20 30 5b 30 20 34 5d  TIONS: [1 0[0 4]
3018f 20 31 5b 31 37 5d 5d 20 5b 33 20 31 5b 35 5d 5d   1[17]] [3 1[5]]
30190 0a 2a 2a 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53  .** DL_POSITIONS
30191 5f 4f 46 46 53 45 54 53 3a 20 5b 31 20 30 5b 30  _OFFSETS: [1 0[0
30192 2c 30 2c 33 20 34 2c 32 33 2c 32 36 5d 20 31 5b  ,0,3 4,23,26] 1[
30193 31 37 2c 31 30 32 2c 31 30 35 5d 5d 20 5b 33 20  17,102,105]] [3 
30194 31 5b 35 2c 32 30 2c 32 33 5d 5d 0a 2a 2a 0a 2a  1[5,20,23]].**.*
30195 2a 20 49 6e 20 65 61 63 68 20 63 61 73 65 20 74  * In each case t
30196 68 65 20 6e 75 6d 62 65 72 20 61 66 74 65 72 20  he number after 
30197 74 68 65 20 6f 75 74 65 72 20 27 5b 27 20 69 73  the outer '[' is
30198 20 74 68 65 20 64 6f 63 69 64 2e 20 20 49 6e 20   the docid.  In 
30199 74 68 65 0a 2a 2a 20 6c 61 74 74 65 72 20 74 77  the.** latter tw
3019a 6f 20 63 61 73 65 73 2c 20 74 68 65 20 6e 75 6d  o cases, the num
3019b 62 65 72 20 62 65 66 6f 72 65 20 74 68 65 20 69  ber before the i
3019c 6e 6e 65 72 20 27 5b 27 20 69 73 20 74 68 65 20  nner '[' is the 
3019d 63 6f 6c 75 6d 6e 0a 2a 2a 20 61 73 73 6f 63 69  column.** associ
3019e 61 74 65 64 20 77 69 74 68 20 74 68 65 20 76 61  ated with the va
3019f 6c 75 65 73 20 77 69 74 68 69 6e 2e 20 20 46 6f  lues within.  Fo
301a0 72 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 74  r DL_POSITIONS t
301a1 68 65 20 6e 75 6d 62 65 72 73 0a 2a 2a 20 77 69  he numbers.** wi
301a2 74 68 69 6e 20 61 72 65 20 74 68 65 20 70 6f 73  thin are the pos
301a3 69 74 69 6f 6e 73 2c 20 66 6f 72 20 44 4c 5f 50  itions, for DL_P
301a4 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53  OSITIONS_OFFSETS
301a5 20 74 68 65 79 20 61 72 65 20 74 68 65 0a 2a 2a   they are the.**
301a6 20 70 6f 73 69 74 69 6f 6e 2c 20 74 68 65 20 73   position, the s
301a7 74 61 72 74 20 6f 66 66 73 65 74 2c 20 61 6e 64  tart offset, and
301a8 20 74 68 65 20 65 6e 64 20 6f 66 66 73 65 74 2e   the end offset.
301a9 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
301aa 64 75 6d 70 44 6f 63 6c 69 73 74 46 75 6e 63 28  dumpDoclistFunc(
301ab 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
301ac 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20 20  xt *pContext,.  
301ad 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
301ae 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
301af 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72  {.  fulltext_cur
301b0 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20  sor *pCursor;.  
301b1 69 66 28 20 61 72 67 63 21 3d 32 20 26 26 20 61  if( argc!=2 && a
301b2 72 67 63 21 3d 34 20 29 7b 0a 20 20 20 20 67 65  rgc!=4 ){.    ge
301b3 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e  nerateError(pCon
301b4 74 65 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63 6c  text, "dump_docl
301b5 69 73 74 22 2c 20 22 69 6e 63 6f 72 72 65 63 74  ist", "incorrect
301b6 20 61 72 67 75 6d 65 6e 74 73 22 29 3b 0a 20 20   arguments");.  
301b7 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65  }else if( sqlite
301b8 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
301b9 76 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42 4c  v[0])!=SQLITE_BL
301ba 4f 42 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20  OB ||.          
301bb 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f    sqlite3_value_
301bc 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 21 3d  bytes(argv[0])!=
301bd 73 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 20  sizeof(pCursor) 
301be 29 7b 0a 20 20 20 20 67 65 6e 65 72 61 74 65 45  ){.    generateE
301bf 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22  rror(pContext, "
301c0 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22 2c 20 22  dump_doclist", "
301c1 69 6c 6c 65 67 61 6c 20 66 69 72 73 74 20 61 72  illegal first ar
301c2 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 65 6c 73  gument");.  }els
301c3 65 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61  e if( sqlite3_va
301c4 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d  lue_text(argv[1]
301c5 29 3d 3d 4e 55 4c 4c 20 7c 7c 0a 20 20 20 20 20  )==NULL ||.     
301c6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76         sqlite3_v
301c7 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31  alue_text(argv[1
301c8 5d 29 5b 30 5d 3d 3d 27 5c 30 27 20 29 7b 0a 20  ])[0]=='\0' ){. 
301c9 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72     generateError
301ca 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70  (pContext, "dump
301cb 5f 64 6f 63 6c 69 73 74 22 2c 20 22 65 6d 70 74  _doclist", "empt
301cc 79 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  y second argumen
301cd 74 22 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  t");.  }else{.  
301ce 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54    const char *pT
301cf 65 72 6d 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  erm = (const cha
301d0 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  r *)sqlite3_valu
301d1 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b  e_text(argv[1]);
301d2 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e  .    const int n
301d3 54 65 72 6d 20 3d 20 73 74 72 6c 65 6e 28 70 54  Term = strlen(pT
301d4 65 72 6d 29 3b 0a 20 20 20 20 66 75 6c 6c 74 65  erm);.    fullte
301d5 78 74 5f 76 74 61 62 20 2a 76 3b 0a 20 20 20 20  xt_vtab *v;.    
301d6 69 6e 74 20 72 63 3b 0a 20 20 20 20 44 61 74 61  int rc;.    Data
301d7 42 75 66 66 65 72 20 64 6f 63 6c 69 73 74 3b 0a  Buffer doclist;.
301d8 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 43 75  .    memcpy(&pCu
301d9 72 73 6f 72 2c 20 73 71 6c 69 74 65 33 5f 76 61  rsor, sqlite3_va
301da 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d  lue_blob(argv[0]
301db 29 2c 20 73 69 7a 65 6f 66 28 70 43 75 72 73 6f  ), sizeof(pCurso
301dc 72 29 29 3b 0a 20 20 20 20 76 20 3d 20 63 75 72  r));.    v = cur
301dd 73 6f 72 5f 76 74 61 62 28 70 43 75 72 73 6f 72  sor_vtab(pCursor
301de 29 3b 0a 0a 20 20 20 20 64 61 74 61 42 75 66 66  );..    dataBuff
301df 65 72 49 6e 69 74 28 26 64 6f 63 6c 69 73 74 2c  erInit(&doclist,
301e0 20 30 29 3b 0a 0a 20 20 20 20 2f 2a 20 74 65 72   0);..    /* ter
301e1 6d 53 65 6c 65 63 74 28 29 20 79 69 65 6c 64 73  mSelect() yields
301e2 20 74 68 65 20 73 61 6d 65 20 6c 6f 67 69 63 61   the same logica
301e3 6c 20 64 6f 63 6c 69 73 74 20 74 68 61 74 20 71  l doclist that q
301e4 75 65 72 69 65 73 20 61 72 65 0a 20 20 20 20 2a  ueries are.    *
301e5 2a 20 72 75 6e 20 61 67 61 69 6e 73 74 2e 0a 20  * run against.. 
301e6 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 61 72     */.    if( ar
301e7 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20 72  gc==2 ){.      r
301e8 63 20 3d 20 74 65 72 6d 53 65 6c 65 63 74 28 76  c = termSelect(v
301e9 2c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 70 54  , v->nColumn, pT
301ea 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 30 2c 20 44  erm, nTerm, 0, D
301eb 4c 5f 44 45 46 41 55 4c 54 2c 20 26 64 6f 63 6c  L_DEFAULT, &docl
301ec 69 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ist);.    }else{
301ed 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
301ee 74 6d 74 20 2a 73 20 3d 20 4e 55 4c 4c 3b 0a 0a  tmt *s = NULL;..
301ef 20 20 20 20 20 20 2f 2a 20 47 65 74 20 6f 75 72        /* Get our
301f0 20 73 70 65 63 69 66 69 63 20 73 65 67 6d 65 6e   specific segmen
301f1 74 27 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  t's information.
301f2 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73   */.      rc = s
301f3 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
301f4 28 76 2c 20 53 45 47 44 49 52 5f 53 45 4c 45 43  (v, SEGDIR_SELEC
301f5 54 5f 53 45 47 4d 45 4e 54 5f 53 54 4d 54 2c 20  T_SEGMENT_STMT, 
301f6 26 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  &s);.      if( r
301f7 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
301f8 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
301f9 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c  ite3_bind_int(s,
301fa 20 31 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   1, sqlite3_valu
301fb 65 5f 69 6e 74 28 61 72 67 76 5b 32 5d 29 29 3b  e_int(argv[2]));
301fc 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
301fd 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
301fe 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
301ff 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c  ite3_bind_int(s,
30200 20 32 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   2, sqlite3_valu
30201 65 5f 69 6e 74 28 61 72 67 76 5b 33 5d 29 29 3b  e_int(argv[3]));
30202 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
30203 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 72 63   }..      if( rc
30204 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
30205 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
30206 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 0a 20 20  te3_step(s);..  
30207 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
30208 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20  LITE_DONE ){.   
30209 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65         dataBuffe
3020a 72 44 65 73 74 72 6f 79 28 26 64 6f 63 6c 69 73  rDestroy(&doclis
3020b 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 65  t);.          ge
3020c 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e  nerateError(pCon
3020d 74 65 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63 6c  text, "dump_docl
3020e 69 73 74 22 2c 20 22 73 65 67 6d 65 6e 74 20 6e  ist", "segment n
3020f 6f 74 20 66 6f 75 6e 64 22 29 3b 0a 20 20 20 20  ot found");.    
30210 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
30211 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20        }..       
30212 20 2f 2a 20 46 6f 75 6e 64 20 61 20 73 65 67 6d   /* Found a segm
30213 65 6e 74 2c 20 6c 6f 61 64 20 69 74 20 69 6e 74  ent, load it int
30214 6f 20 64 6f 63 6c 69 73 74 2e 20 2a 2f 0a 20 20  o doclist. */.  
30215 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
30216 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
30217 20 20 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69        const sqli
30218 74 65 5f 69 6e 74 36 34 20 69 4c 65 61 76 65 73  te_int64 iLeaves
30219 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  End = sqlite3_co
3021a 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29  lumn_int64(s, 1)
3021b 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73  ;.          cons
3021c 74 20 63 68 61 72 20 2a 70 44 61 74 61 20 3d 20  t char *pData = 
3021d 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
3021e 6c 6f 62 28 73 2c 20 32 29 3b 0a 20 20 20 20 20  lob(s, 2);.     
3021f 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e       const int n
30220 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 5f 63  Data = sqlite3_c
30221 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 2c 20 32  olumn_bytes(s, 2
30222 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  );..          /*
30223 20 6c 6f 61 64 53 65 67 6d 65 6e 74 28 29 20 69   loadSegment() i
30224 73 20 75 73 65 64 20 62 79 20 74 65 72 6d 53 65  s used by termSe
30225 6c 65 63 74 28 29 20 74 6f 20 6c 6f 61 64 20 65  lect() to load e
30226 61 63 68 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ach.          **
30227 20 73 65 67 6d 65 6e 74 27 73 20 64 61 74 61 2e   segment's data.
30228 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  .          */.  
30229 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 61          rc = loa
3022a 64 53 65 67 6d 65 6e 74 28 76 2c 20 70 44 61 74  dSegment(v, pDat
3022b 61 2c 20 6e 44 61 74 61 2c 20 69 4c 65 61 76 65  a, nData, iLeave
3022c 73 45 6e 64 2c 20 70 54 65 72 6d 2c 20 6e 54 65  sEnd, pTerm, nTe
3022d 72 6d 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20  rm, 0,.         
3022e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3022f 20 20 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20 20    &doclist);.   
30230 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
30231 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
30232 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
30233 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 0a 20  ite3_step(s);.. 
30234 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68             /* Sh
30235 6f 75 6c 64 20 6e 6f 74 20 68 61 76 65 20 6d 6f  ould not have mo
30236 72 65 20 74 68 61 6e 20 6f 6e 65 20 6d 61 74 63  re than one matc
30237 68 69 6e 67 20 73 65 67 6d 65 6e 74 2e 20 2a 2f  hing segment. */
30238 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
30239 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc!=SQLITE_DONE
3023a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
3023b 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28    sqlite3_reset(
3023c 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  s);.            
3023d 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74    dataBufferDest
3023e 72 6f 79 28 26 64 6f 63 6c 69 73 74 29 3b 0a 20  roy(&doclist);. 
3023f 20 20 20 20 20 20 20 20 20 20 20 20 20 67 65 6e               gen
30240 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74  erateError(pCont
30241 65 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69  ext, "dump_docli
30242 73 74 22 2c 20 22 69 6e 76 61 6c 69 64 20 73 65  st", "invalid se
30243 67 64 69 72 22 29 3b 0a 20 20 20 20 20 20 20 20  gdir");.        
30244 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
30245 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
30246 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
30247 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20  ITE_OK;.        
30248 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
30249 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 73 71 6c      }..      sql
3024a 69 74 65 33 5f 72 65 73 65 74 28 73 29 3b 0a 20  ite3_reset(s);. 
3024b 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63     }..    if( rc
3024c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
3024d 20 20 20 20 20 69 66 28 20 64 6f 63 6c 69 73 74       if( doclist
3024e 2e 6e 44 61 74 61 3e 30 20 29 7b 0a 20 20 20 20  .nData>0 ){.    
3024f 20 20 20 20 63 72 65 61 74 65 44 6f 63 6c 69 73      createDoclis
30250 74 52 65 73 75 6c 74 28 70 43 6f 6e 74 65 78 74  tResult(pContext
30251 2c 20 64 6f 63 6c 69 73 74 2e 70 44 61 74 61 2c  , doclist.pData,
30252 20 64 6f 63 6c 69 73 74 2e 6e 44 61 74 61 29 3b   doclist.nData);
30253 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
30254 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68        /* TODO(sh
30255 65 73 73 29 3a 20 54 68 69 73 20 63 61 6e 20 68  ess): This can h
30256 61 70 70 65 6e 20 69 66 20 74 68 65 20 74 65 72  appen if the ter
30257 6d 20 69 73 20 6e 6f 74 20 70 72 65 73 65 6e 74  m is not present
30258 2c 20 6f 72 0a 20 20 20 20 20 20 20 20 2a 2a 20  , or.        ** 
30259 69 66 20 61 6c 6c 20 69 6e 73 74 61 6e 63 65 73  if all instances
3025a 20 6f 66 20 74 68 65 20 74 65 72 6d 20 68 61 76   of the term hav
3025b 65 20 62 65 65 6e 20 64 65 6c 65 74 65 64 20 61  e been deleted a
3025c 6e 64 20 74 68 69 73 20 69 73 0a 20 20 20 20 20  nd this is.     
3025d 20 20 20 2a 2a 20 61 6e 20 61 6c 6c 2d 69 6e 64     ** an all-ind
3025e 65 78 20 64 75 6d 70 2e 20 20 49 74 20 6d 61 79  ex dump.  It may
3025f 20 62 65 20 69 6e 74 65 72 65 73 74 69 6e 67 20   be interesting 
30260 74 6f 20 64 69 73 74 69 6e 67 75 69 73 68 0a 20  to distinguish. 
30261 20 20 20 20 20 20 20 2a 2a 20 74 68 65 73 65 20         ** these 
30262 63 61 73 65 73 2e 0a 20 20 20 20 20 20 20 20 2a  cases..        *
30263 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  /.        sqlite
30264 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43  3_result_text(pC
30265 6f 6e 74 65 78 74 2c 20 22 22 2c 20 30 2c 20 53  ontext, "", 0, S
30266 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
30267 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
30268 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
30269 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 2f  NOMEM ){.      /
3026a 2a 20 48 61 6e 64 6c 65 20 6f 75 74 2d 6f 66 2d  * Handle out-of-
3026b 6d 65 6d 6f 72 79 20 63 61 73 65 73 20 73 70 65  memory cases spe
3026c 63 69 61 6c 6c 79 20 62 65 63 61 75 73 65 20 69  cially because i
3026d 66 20 74 68 65 79 20 61 72 65 0a 20 20 20 20 20  f they are.     
3026e 20 2a 2a 20 67 65 6e 65 72 61 74 65 64 20 69 6e   ** generated in
3026f 20 66 74 73 33 20 63 6f 64 65 20 74 68 65 79 20   fts3 code they 
30270 6d 61 79 20 6e 6f 74 20 62 65 20 72 65 66 6c 65  may not be refle
30271 63 74 65 64 20 69 6e 20 74 68 65 20 64 62 0a 20  cted in the db. 
30272 20 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 2e 0a       ** handle..
30273 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 2f        */.      /
30274 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 3a 20 48  * TODO(shess): H
30275 61 6e 64 6c 65 20 74 68 69 73 20 6d 6f 72 65 20  andle this more 
30276 63 6f 6d 70 72 65 68 65 6e 73 69 76 65 6c 79 2e  comprehensively.
30277 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  .      ** sqlite
30278 33 45 72 72 53 74 72 28 29 20 68 61 73 20 77 68  3ErrStr() has wh
30279 61 74 20 49 20 6e 65 65 64 2c 20 62 75 74 20 69  at I need, but i
3027a 73 20 69 6e 74 65 72 6e 61 6c 2e 0a 20 20 20 20  s internal..    
3027b 20 20 2a 2f 0a 20 20 20 20 20 20 67 65 6e 65 72    */.      gener
3027c 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78  ateError(pContex
3027d 74 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74  t, "dump_doclist
3027e 22 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72  ", "out of memor
3027f 79 22 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  y");.    }else{.
30280 20 20 20 20 20 20 67 65 6e 65 72 61 74 65 45 72        generateEr
30281 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64  ror(pContext, "d
30282 75 6d 70 5f 64 6f 63 6c 69 73 74 22 2c 20 4e 55  ump_doclist", NU
30283 4c 4c 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  LL);.    }..    
30284 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
30285 79 28 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20 7d  y(&doclist);.  }
30286 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
30287 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d   This routine im
30288 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 78 46 69  plements the xFi
30289 6e 64 46 75 6e 63 74 69 6f 6e 20 6d 65 74 68 6f  ndFunction metho
3028a 64 20 66 6f 72 20 74 68 65 20 46 54 53 33 0a 2a  d for the FTS3.*
3028b 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  * virtual table.
3028c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
3028d 75 6c 6c 74 65 78 74 46 69 6e 64 46 75 6e 63 74  ulltextFindFunct
3028e 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  ion(.  sqlite3_v
3028f 74 61 62 20 2a 70 56 74 61 62 2c 0a 20 20 69 6e  tab *pVtab,.  in
30290 74 20 6e 41 72 67 2c 0a 20 20 63 6f 6e 73 74 20  t nArg,.  const 
30291 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 76  char *zName,.  v
30292 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29 28 73  oid (**pxFunc)(s
30293 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
30294 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
30295 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 2a 2a 70  e**),.  void **p
30296 70 41 72 67 0a 29 7b 0a 20 20 69 66 28 20 73 74  pArg.){.  if( st
30297 72 63 6d 70 28 7a 4e 61 6d 65 2c 22 73 6e 69 70  rcmp(zName,"snip
30298 70 65 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  pet")==0 ){.    
30299 2a 70 78 46 75 6e 63 20 3d 20 73 6e 69 70 70 65  *pxFunc = snippe
3029a 74 46 75 6e 63 3b 0a 20 20 20 20 72 65 74 75 72  tFunc;.    retur
3029b 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  n 1;.  }else if(
3029c 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 22 6f   strcmp(zName,"o
3029d 66 66 73 65 74 73 22 29 3d 3d 30 20 29 7b 0a 20  ffsets")==0 ){. 
3029e 20 20 20 2a 70 78 46 75 6e 63 20 3d 20 73 6e 69     *pxFunc = sni
3029f 70 70 65 74 4f 66 66 73 65 74 73 46 75 6e 63 3b  ppetOffsetsFunc;
302a0 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
302a1 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d   }else if( strcm
302a2 70 28 7a 4e 61 6d 65 2c 22 6f 70 74 69 6d 69 7a  p(zName,"optimiz
302a3 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70  e")==0 ){.    *p
302a4 78 46 75 6e 63 20 3d 20 6f 70 74 69 6d 69 7a 65  xFunc = optimize
302a5 46 75 6e 63 3b 0a 20 20 20 20 72 65 74 75 72 6e  Func;.    return
302a6 20 31 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54   1;.#ifdef SQLIT
302a7 45 5f 54 45 53 54 0a 20 20 20 20 2f 2a 20 4e 4f  E_TEST.    /* NO
302a8 54 45 28 73 68 65 73 73 29 3a 20 54 68 65 73 65  TE(shess): These
302a9 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 70   functions are p
302aa 72 65 73 65 6e 74 20 6f 6e 6c 79 20 66 6f 72 20  resent only for 
302ab 74 65 73 74 69 6e 67 0a 20 20 20 20 2a 2a 20 70  testing.    ** p
302ac 75 72 70 6f 73 65 73 2e 20 20 4e 6f 20 70 61 72  urposes.  No par
302ad 74 69 63 75 6c 61 72 20 65 66 66 6f 72 74 20 69  ticular effort i
302ae 73 20 6d 61 64 65 20 74 6f 20 6f 70 74 69 6d 69  s made to optimi
302af 7a 65 20 74 68 65 69 72 0a 20 20 20 20 2a 2a 20  ze their.    ** 
302b0 65 78 65 63 75 74 69 6f 6e 20 6f 72 20 68 6f 77  execution or how
302b1 20 74 68 65 79 20 62 75 69 6c 64 20 74 68 65 69   they build thei
302b2 72 20 72 65 73 75 6c 74 73 2e 0a 20 20 20 20 2a  r results..    *
302b3 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74  /.  }else if( st
302b4 72 63 6d 70 28 7a 4e 61 6d 65 2c 22 64 75 6d 70  rcmp(zName,"dump
302b5 5f 74 65 72 6d 73 22 29 3d 3d 30 20 29 7b 0a 20  _terms")==0 ){. 
302b6 20 20 20 2f 2a 20 66 70 72 69 6e 74 66 28 73 74     /* fprintf(st
302b7 64 65 72 72 2c 20 22 46 6f 75 6e 64 20 64 75 6d  derr, "Found dum
302b8 70 5f 74 65 72 6d 73 5c 6e 22 29 3b 20 2a 2f 0a  p_terms\n"); */.
302b9 20 20 20 20 2a 70 78 46 75 6e 63 20 3d 20 64 75      *pxFunc = du
302ba 6d 70 54 65 72 6d 73 46 75 6e 63 3b 0a 20 20 20  mpTermsFunc;.   
302bb 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c   return 1;.  }el
302bc 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a 4e  se if( strcmp(zN
302bd 61 6d 65 2c 22 64 75 6d 70 5f 64 6f 63 6c 69 73  ame,"dump_doclis
302be 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  t")==0 ){.    /*
302bf 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
302c0 20 22 46 6f 75 6e 64 20 64 75 6d 70 5f 64 6f 63   "Found dump_doc
302c1 6c 69 73 74 5c 6e 22 29 3b 20 2a 2f 0a 20 20 20  list\n"); */.   
302c2 20 2a 70 78 46 75 6e 63 20 3d 20 64 75 6d 70 44   *pxFunc = dumpD
302c3 6f 63 6c 69 73 74 46 75 6e 63 3b 0a 20 20 20 20  oclistFunc;.    
302c4 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66  return 1;.#endif
302c5 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
302c6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6e 61 6d 65  .}../*.** Rename
302c7 20 61 6e 20 66 74 73 33 20 74 61 62 6c 65 2e 0a   an fts3 table..
302c8 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75  */.static int fu
302c9 6c 6c 74 65 78 74 52 65 6e 61 6d 65 28 0a 20 20  lltextRename(.  
302ca 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
302cb 74 61 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  tab,.  const cha
302cc 72 20 2a 7a 4e 61 6d 65 0a 29 7b 0a 20 20 66 75  r *zName.){.  fu
302cd 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 70 20 3d  lltext_vtab *p =
302ce 20 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20   (fulltext_vtab 
302cf 2a 29 70 56 74 61 62 3b 0a 20 20 69 6e 74 20 72  *)pVtab;.  int r
302d0 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
302d1 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d  ;.  char *zSql =
302d2 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
302d3 28 0a 20 20 20 20 22 41 4c 54 45 52 20 54 41 42  (.    "ALTER TAB
302d4 4c 45 20 25 51 2e 27 25 71 5f 63 6f 6e 74 65 6e  LE %Q.'%q_conten
302d5 74 27 20 20 52 45 4e 41 4d 45 20 54 4f 20 27 25  t'  RENAME TO '%
302d6 71 5f 63 6f 6e 74 65 6e 74 27 3b 22 0a 20 20 20  q_content';".   
302d7 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 25 51   "ALTER TABLE %Q
302d8 2e 27 25 71 5f 73 65 67 6d 65 6e 74 73 27 20 52  .'%q_segments' R
302d9 45 4e 41 4d 45 20 54 4f 20 27 25 71 5f 73 65 67  ENAME TO '%q_seg
302da 6d 65 6e 74 73 27 3b 22 0a 20 20 20 20 22 41 4c  ments';".    "AL
302db 54 45 52 20 54 41 42 4c 45 20 25 51 2e 27 25 71  TER TABLE %Q.'%q
302dc 5f 73 65 67 64 69 72 27 20 20 20 52 45 4e 41 4d  _segdir'   RENAM
302dd 45 20 54 4f 20 27 25 71 5f 73 65 67 64 69 72 27  E TO '%q_segdir'
302de 3b 22 0a 20 20 20 20 2c 20 70 2d 3e 7a 44 62 2c  ;".    , p->zDb,
302df 20 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65   p->zName, zName
302e0 20 0a 20 20 20 20 2c 20 70 2d 3e 7a 44 62 2c 20   .    , p->zDb, 
302e1 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 20  p->zName, zName 
302e2 0a 20 20 20 20 2c 20 70 2d 3e 7a 44 62 2c 20 70  .    , p->zDb, p
302e3 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 0a 20  ->zName, zName. 
302e4 20 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 20 29   );.  if( zSql )
302e5 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
302e6 65 33 5f 65 78 65 63 28 70 2d 3e 64 62 2c 20 7a  e3_exec(p->db, z
302e7 53 71 6c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  Sql, 0, 0, 0);. 
302e8 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
302e9 7a 53 71 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74  zSql);.  }.  ret
302ea 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69  urn rc;.}..stati
302eb 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
302ec 6d 6f 64 75 6c 65 20 66 74 73 33 4d 6f 64 75 6c  module fts3Modul
302ed 65 20 3d 20 7b 0a 20 20 2f 2a 20 69 56 65 72 73  e = {.  /* iVers
302ee 69 6f 6e 20 20 20 20 20 20 2a 2f 20 30 2c 0a 20  ion      */ 0,. 
302ef 20 2f 2a 20 78 43 72 65 61 74 65 20 20 20 20 20   /* xCreate     
302f0 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 43 72 65    */ fulltextCre
302f1 61 74 65 2c 0a 20 20 2f 2a 20 78 43 6f 6e 6e 65  ate,.  /* xConne
302f2 63 74 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74  ct      */ fullt
302f3 65 78 74 43 6f 6e 6e 65 63 74 2c 0a 20 20 2f 2a  extConnect,.  /*
302f4 20 78 42 65 73 74 49 6e 64 65 78 20 20 20 20 2a   xBestIndex    *
302f5 2f 20 66 75 6c 6c 74 65 78 74 42 65 73 74 49 6e  / fulltextBestIn
302f6 64 65 78 2c 0a 20 20 2f 2a 20 78 44 69 73 63 6f  dex,.  /* xDisco
302f7 6e 6e 65 63 74 20 20 20 2a 2f 20 66 75 6c 6c 74  nnect   */ fullt
302f8 65 78 74 44 69 73 63 6f 6e 6e 65 63 74 2c 0a 20  extDisconnect,. 
302f9 20 2f 2a 20 78 44 65 73 74 72 6f 79 20 20 20 20   /* xDestroy    
302fa 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 44 65 73    */ fulltextDes
302fb 74 72 6f 79 2c 0a 20 20 2f 2a 20 78 4f 70 65 6e  troy,.  /* xOpen
302fc 20 20 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c           */ full
302fd 74 65 78 74 4f 70 65 6e 2c 0a 20 20 2f 2a 20 78  textOpen,.  /* x
302fe 43 6c 6f 73 65 20 20 20 20 20 20 20 20 2a 2f 20  Close        */ 
302ff 66 75 6c 6c 74 65 78 74 43 6c 6f 73 65 2c 0a 20  fulltextClose,. 
30300 20 2f 2a 20 78 46 69 6c 74 65 72 20 20 20 20 20   /* xFilter     
30301 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 46 69 6c    */ fulltextFil
30302 74 65 72 2c 0a 20 20 2f 2a 20 78 4e 65 78 74 20  ter,.  /* xNext 
30303 20 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74          */ fullt
30304 65 78 74 4e 65 78 74 2c 0a 20 20 2f 2a 20 78 45  extNext,.  /* xE
30305 6f 66 20 20 20 20 20 20 20 20 20 20 2a 2f 20 66  of          */ f
30306 75 6c 6c 74 65 78 74 45 6f 66 2c 0a 20 20 2f 2a  ulltextEof,.  /*
30307 20 78 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20 2a   xColumn       *
30308 2f 20 66 75 6c 6c 74 65 78 74 43 6f 6c 75 6d 6e  / fulltextColumn
30309 2c 0a 20 20 2f 2a 20 78 52 6f 77 69 64 20 20 20  ,.  /* xRowid   
3030a 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74       */ fulltext
3030b 52 6f 77 69 64 2c 0a 20 20 2f 2a 20 78 55 70 64  Rowid,.  /* xUpd
3030c 61 74 65 20 20 20 20 20 20 20 2a 2f 20 66 75 6c  ate       */ ful
3030d 6c 74 65 78 74 55 70 64 61 74 65 2c 0a 20 20 2f  ltextUpdate,.  /
3030e 2a 20 78 42 65 67 69 6e 20 20 20 20 20 20 20 20  * xBegin        
3030f 2a 2f 20 66 75 6c 6c 74 65 78 74 42 65 67 69 6e  */ fulltextBegin
30310 2c 0a 20 20 2f 2a 20 78 53 79 6e 63 20 20 20 20  ,.  /* xSync    
30311 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74       */ fulltext
30312 53 79 6e 63 2c 0a 20 20 2f 2a 20 78 43 6f 6d 6d  Sync,.  /* xComm
30313 69 74 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c  it       */ full
30314 74 65 78 74 43 6f 6d 6d 69 74 2c 0a 20 20 2f 2a  textCommit,.  /*
30315 20 78 52 6f 6c 6c 62 61 63 6b 20 20 20 20 20 2a   xRollback     *
30316 2f 20 66 75 6c 6c 74 65 78 74 52 6f 6c 6c 62 61  / fulltextRollba
30317 63 6b 2c 0a 20 20 2f 2a 20 78 46 69 6e 64 46 75  ck,.  /* xFindFu
30318 6e 63 74 69 6f 6e 20 2a 2f 20 66 75 6c 6c 74 65  nction */ fullte
30319 78 74 46 69 6e 64 46 75 6e 63 74 69 6f 6e 2c 0a  xtFindFunction,.
3031a 20 20 2f 2a 20 78 52 65 6e 61 6d 65 20 2a 2f 20    /* xRename */ 
3031b 20 20 20 20 20 20 66 75 6c 6c 74 65 78 74 52 65        fulltextRe
3031c 6e 61 6d 65 2c 0a 7d 3b 0a 0a 73 74 61 74 69 63  name,.};..static
3031d 20 76 6f 69 64 20 68 61 73 68 44 65 73 74 72 6f   void hashDestro
3031e 79 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 66 74  y(void *p){.  ft
3031f 73 33 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20  s3Hash *pHash = 
30320 28 66 74 73 33 48 61 73 68 20 2a 29 70 3b 0a 20  (fts3Hash *)p;. 
30321 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
30322 43 6c 65 61 72 28 70 48 61 73 68 29 3b 0a 20 20  Clear(pHash);.  
30323 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48 61  sqlite3_free(pHa
30324 73 68 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  sh);.}../*.** Th
30325 65 20 66 74 73 33 20 62 75 69 6c 74 2d 69 6e 20  e fts3 built-in 
30326 74 6f 6b 65 6e 69 7a 65 72 73 20 2d 20 22 73 69  tokenizers - "si
30327 6d 70 6c 65 22 20 61 6e 64 20 22 70 6f 72 74 65  mple" and "porte
30328 72 22 20 2d 20 61 72 65 20 69 6d 70 6c 65 6d 65  r" - are impleme
30329 6e 74 65 64 0a 2a 2a 20 69 6e 20 66 69 6c 65 73  nted.** in files
3032a 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 31   fts3_tokenizer1
3032b 2e 63 20 61 6e 64 20 66 74 73 33 5f 70 6f 72 74  .c and fts3_port
3032c 65 72 2e 63 20 72 65 73 70 65 63 74 69 76 65 6c  er.c respectivel
3032d 79 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  y. The following
3032e 0a 2a 2a 20 74 77 6f 20 66 6f 72 77 61 72 64 20  .** two forward 
3032f 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 61 72 65  declarations are
30330 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 73 20 64   for functions d
30331 65 63 6c 61 72 65 64 20 69 6e 20 74 68 65 73 65  eclared in these
30332 20 66 69 6c 65 73 0a 2a 2a 20 75 73 65 64 20 74   files.** used t
30333 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 20 72  o retrieve the r
30334 65 73 70 65 63 74 69 76 65 20 69 6d 70 6c 65 6d  espective implem
30335 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  entations..**.**
30336 20 43 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33   Calling sqlite3
30337 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69  Fts3SimpleTokeni
30338 7a 65 72 4d 6f 64 75 6c 65 28 29 20 73 65 74 73  zerModule() sets
30339 20 74 68 65 20 76 61 6c 75 65 20 70 6f 69 6e 74   the value point
3033a 65 64 0a 2a 2a 20 74 6f 20 62 79 20 74 68 65 20  ed.** to by the 
3033b 61 72 67 75 6d 65 6e 74 20 74 6f 20 70 6f 69 6e  argument to poin
3033c 74 20 61 20 74 68 65 20 22 73 69 6d 70 6c 65 22  t a the "simple"
3033d 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6d 70 6c 65   tokenizer imple
3033e 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 46 75  mentation..** Fu
3033f 6e 63 74 69 6f 6e 20 2e 2e 2e 50 6f 72 74 65 72  nction ...Porter
30340 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28  TokenizerModule(
30341 29 20 73 65 74 73 20 2a 70 4d 6f 64 75 6c 65 20  ) sets *pModule 
30342 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a  to point to the.
30343 2a 2a 20 70 6f 72 74 65 72 20 74 6f 6b 65 6e 69  ** porter tokeni
30344 7a 65 72 2f 73 74 65 6d 6d 65 72 20 69 6d 70 6c  zer/stemmer impl
30345 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 53  ementation..*/.S
30346 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
30347 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 53 69  id sqlite3Fts3Si
30348 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64  mpleTokenizerMod
30349 75 6c 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65  ule(sqlite3_toke
3034a 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e  nizer_module con
3034b 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a 53  st**ppModule);.S
3034c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
3034d 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 50 6f  id sqlite3Fts3Po
3034e 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64  rterTokenizerMod
3034f 75 6c 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65  ule(sqlite3_toke
30350 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e  nizer_module con
30351 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a 53  st**ppModule);.S
30352 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
30353 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 49 63  id sqlite3Fts3Ic
30354 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  uTokenizerModule
30355 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a  (sqlite3_tokeniz
30356 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a  er_module const*
30357 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a 0a 53 51 4c  *ppModule);..SQL
30358 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
30359 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 48  sqlite3Fts3InitH
3035a 61 73 68 54 61 62 6c 65 28 73 71 6c 69 74 65 33  ashTable(sqlite3
3035b 20 2a 2c 20 66 74 73 33 48 61 73 68 20 2a 2c 20   *, fts3Hash *, 
3035c 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 0a  const char *);..
3035d 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 73 65  /*.** Initialise
3035e 20 74 68 65 20 66 74 73 33 20 65 78 74 65 6e 73   the fts3 extens
3035f 69 6f 6e 2e 20 49 66 20 74 68 69 73 20 65 78 74  ion. If this ext
30360 65 6e 73 69 6f 6e 20 69 73 20 62 75 69 6c 74 20  ension is built 
30361 61 73 20 70 61 72 74 0a 2a 2a 20 6f 66 20 74 68  as part.** of th
30362 65 20 73 71 6c 69 74 65 20 6c 69 62 72 61 72 79  e sqlite library
30363 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63  , then this func
30364 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 64  tion is called d
30365 69 72 65 63 74 6c 79 20 62 79 0a 2a 2a 20 53 51  irectly by.** SQ
30366 4c 69 74 65 2e 20 49 66 20 66 74 73 33 20 69 73  Lite. If fts3 is
30367 20 62 75 69 6c 74 20 61 73 20 61 20 64 79 6e 61   built as a dyna
30368 6d 69 63 61 6c 6c 79 20 6c 6f 61 64 61 62 6c 65  mically loadable
30369 20 65 78 74 65 6e 73 69 6f 6e 2c 20 74 68 69 73   extension, this
3036a 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  .** function is 
3036b 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 73 71  called by the sq
3036c 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f  lite3_extension_
3036d 69 6e 69 74 28 29 20 65 6e 74 72 79 20 70 6f 69  init() entry poi
3036e 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nt..*/.SQLITE_PR
3036f 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
30370 33 46 74 73 33 49 6e 69 74 28 73 71 6c 69 74 65  3Fts3Init(sqlite
30371 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 72 63  3 *db){.  int rc
30372 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
30373 66 74 73 33 48 61 73 68 20 2a 70 48 61 73 68 20  fts3Hash *pHash 
30374 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c  = 0;.  const sql
30375 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
30376 6f 64 75 6c 65 20 2a 70 53 69 6d 70 6c 65 20 3d  odule *pSimple =
30377 20 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69   0;.  const sqli
30378 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
30379 64 75 6c 65 20 2a 70 50 6f 72 74 65 72 20 3d 20  dule *pPorter = 
3037a 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  0;.  const sqlit
3037b 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
3037c 75 6c 65 20 2a 70 49 63 75 20 3d 20 30 3b 0a 0a  ule *pIcu = 0;..
3037d 20 20 73 71 6c 69 74 65 33 46 74 73 33 53 69 6d    sqlite3Fts3Sim
3037e 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75  pleTokenizerModu
3037f 6c 65 28 26 70 53 69 6d 70 6c 65 29 3b 0a 20 20  le(&pSimple);.  
30380 73 71 6c 69 74 65 33 46 74 73 33 50 6f 72 74 65  sqlite3Fts3Porte
30381 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  rTokenizerModule
30382 28 26 70 50 6f 72 74 65 72 29 3b 0a 23 69 66 64  (&pPorter);.#ifd
30383 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
30384 5f 49 43 55 0a 20 20 73 71 6c 69 74 65 33 46 74  _ICU.  sqlite3Ft
30385 73 33 49 63 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f  s3IcuTokenizerMo
30386 64 75 6c 65 28 26 70 49 63 75 29 3b 0a 23 65 6e  dule(&pIcu);.#en
30387 64 69 66 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61  dif..  /* Alloca
30388 74 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 73  te and initialis
30389 65 20 74 68 65 20 68 61 73 68 2d 74 61 62 6c 65  e the hash-table
3038a 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74   used to store t
3038b 6f 6b 65 6e 69 7a 65 72 73 2e 20 2a 2f 0a 20 20  okenizers. */.  
3038c 70 48 61 73 68 20 3d 20 73 71 6c 69 74 65 33 5f  pHash = sqlite3_
3038d 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 66 74  malloc(sizeof(ft
3038e 73 33 48 61 73 68 29 29 3b 0a 20 20 69 66 28 20  s3Hash));.  if( 
3038f 21 70 48 61 73 68 20 29 7b 0a 20 20 20 20 72 63  !pHash ){.    rc
30390 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
30391 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
30392 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 69  lite3Fts3HashIni
30393 74 28 70 48 61 73 68 2c 20 46 54 53 33 5f 48 41  t(pHash, FTS3_HA
30394 53 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0a 20  SH_STRING, 1);. 
30395 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 74 68   }..  /* Load th
30396 65 20 62 75 69 6c 74 2d 69 6e 20 74 6f 6b 65 6e  e built-in token
30397 69 7a 65 72 73 20 69 6e 74 6f 20 74 68 65 20 68  izers into the h
30398 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  ash table */.  i
30399 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
3039a 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   ){.    if( sqli
3039b 74 65 33 46 74 73 33 48 61 73 68 49 6e 73 65 72  te3Fts3HashInser
3039c 74 28 70 48 61 73 68 2c 20 22 73 69 6d 70 6c 65  t(pHash, "simple
3039d 22 2c 20 37 2c 20 28 76 6f 69 64 20 2a 29 70 53  ", 7, (void *)pS
3039e 69 6d 70 6c 65 29 0a 20 20 20 20 20 7c 7c 20 73  imple).     || s
3039f 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e  qlite3Fts3HashIn
303a0 73 65 72 74 28 70 48 61 73 68 2c 20 22 70 6f 72  sert(pHash, "por
303a1 74 65 72 22 2c 20 37 2c 20 28 76 6f 69 64 20 2a  ter", 7, (void *
303a2 29 70 50 6f 72 74 65 72 29 20 0a 20 20 20 20 20  )pPorter) .     
303a3 7c 7c 20 28 70 49 63 75 20 26 26 20 73 71 6c 69  || (pIcu && sqli
303a4 74 65 33 46 74 73 33 48 61 73 68 49 6e 73 65 72  te3Fts3HashInser
303a5 74 28 70 48 61 73 68 2c 20 22 69 63 75 22 2c 20  t(pHash, "icu", 
303a6 34 2c 20 28 76 6f 69 64 20 2a 29 70 49 63 75 29  4, (void *)pIcu)
303a7 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72  ).    ){.      r
303a8 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
303a9 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
303aa 2a 20 43 72 65 61 74 65 20 74 68 65 20 76 69 72  * Create the vir
303ab 74 75 61 6c 20 74 61 62 6c 65 20 77 72 61 70 70  tual table wrapp
303ac 65 72 20 61 72 6f 75 6e 64 20 74 68 65 20 68 61  er around the ha
303ad 73 68 2d 74 61 62 6c 65 20 61 6e 64 20 6f 76 65  sh-table and ove
303ae 72 6c 6f 61 64 20 0a 20 20 2a 2a 20 74 68 65 20  rload .  ** the 
303af 74 77 6f 20 73 63 61 6c 61 72 20 66 75 6e 63 74  two scalar funct
303b0 69 6f 6e 73 2e 20 49 66 20 74 68 69 73 20 69 73  ions. If this is
303b1 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72 65 67   successful, reg
303b2 69 73 74 65 72 20 74 68 65 0a 20 20 2a 2a 20 6d  ister the.  ** m
303b3 6f 64 75 6c 65 20 77 69 74 68 20 73 71 6c 69 74  odule with sqlit
303b4 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 53 51  e..  */.  if( SQ
303b5 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20 0a 20 20 20  LITE_OK==rc .   
303b6 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72  && SQLITE_OK==(r
303b7 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 49  c = sqlite3Fts3I
303b8 6e 69 74 48 61 73 68 54 61 62 6c 65 28 64 62 2c  nitHashTable(db,
303b9 20 70 48 61 73 68 2c 20 22 66 74 73 33 5f 74 6f   pHash, "fts3_to
303ba 6b 65 6e 69 7a 65 72 22 29 29 0a 20 20 20 26 26  kenizer")).   &&
303bb 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20   SQLITE_OK==(rc 
303bc 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f  = sqlite3_overlo
303bd 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20  ad_function(db, 
303be 22 73 6e 69 70 70 65 74 22 2c 20 2d 31 29 29 0a  "snippet", -1)).
303bf 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d     && SQLITE_OK=
303c0 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f  =(rc = sqlite3_o
303c1 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e  verload_function
303c2 28 64 62 2c 20 22 6f 66 66 73 65 74 73 22 2c 20  (db, "offsets", 
303c3 2d 31 29 29 0a 20 20 20 26 26 20 53 51 4c 49 54  -1)).   && SQLIT
303c4 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK==(rc = sqli
303c5 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e  te3_overload_fun
303c6 63 74 69 6f 6e 28 64 62 2c 20 22 6f 70 74 69 6d  ction(db, "optim
303c7 69 7a 65 22 2c 20 2d 31 29 29 0a 23 69 66 64 65  ize", -1)).#ifde
303c8 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
303c9 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28   && SQLITE_OK==(
303ca 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65  rc = sqlite3_ove
303cb 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64  rload_function(d
303cc 62 2c 20 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c  b, "dump_terms",
303cd 20 2d 31 29 29 0a 20 20 20 26 26 20 53 51 4c 49   -1)).   && SQLI
303ce 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c  TE_OK==(rc = sql
303cf 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75  ite3_overload_fu
303d0 6e 63 74 69 6f 6e 28 64 62 2c 20 22 64 75 6d 70  nction(db, "dump
303d1 5f 64 6f 63 6c 69 73 74 22 2c 20 2d 31 29 29 0a  _doclist", -1)).
303d2 23 65 6e 64 69 66 0a 20 20 29 7b 0a 20 20 20 20  #endif.  ){.    
303d3 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 63  return sqlite3_c
303d4 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28  reate_module_v2(
303d5 0a 20 20 20 20 20 20 20 20 64 62 2c 20 22 66 74  .        db, "ft
303d6 73 33 22 2c 20 26 66 74 73 33 4d 6f 64 75 6c 65  s3", &fts3Module
303d7 2c 20 28 76 6f 69 64 20 2a 29 70 48 61 73 68 2c  , (void *)pHash,
303d8 20 68 61 73 68 44 65 73 74 72 6f 79 0a 20 20 20   hashDestroy.   
303d9 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6e   );.  }..  /* An
303da 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72   error has occur
303db 65 64 2e 20 44 65 6c 65 74 65 20 74 68 65 20 68  ed. Delete the h
303dc 61 73 68 20 74 61 62 6c 65 20 61 6e 64 20 72 65  ash table and re
303dd 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63  turn the error c
303de 6f 64 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  ode. */.  assert
303df 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
303e0 29 3b 0a 20 20 69 66 28 20 70 48 61 73 68 20 29  );.  if( pHash )
303e1 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73  {.    sqlite3Fts
303e2 33 48 61 73 68 43 6c 65 61 72 28 70 48 61 73 68  3HashClear(pHash
303e3 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
303e4 72 65 65 28 70 48 61 73 68 29 3b 0a 20 20 7d 0a  ree(pHash);.  }.
303e5 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
303e6 23 69 66 20 21 53 51 4c 49 54 45 5f 43 4f 52 45  #if !SQLITE_CORE
303e7 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
303e8 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f  sqlite3_extensio
303e9 6e 5f 69 6e 69 74 28 0a 20 20 73 71 6c 69 74 65  n_init(.  sqlite
303ea 33 20 2a 64 62 2c 20 0a 20 20 63 68 61 72 20 2a  3 *db, .  char *
303eb 2a 70 7a 45 72 72 4d 73 67 2c 0a 20 20 63 6f 6e  *pzErrMsg,.  con
303ec 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  st sqlite3_api_r
303ed 6f 75 74 69 6e 65 73 20 2a 70 41 70 69 0a 29 7b  outines *pApi.){
303ee 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53  .  SQLITE_EXTENS
303ef 49 4f 4e 5f 49 4e 49 54 32 28 70 41 70 69 29 0a  ION_INIT2(pApi).
303f0 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
303f1 46 74 73 33 49 6e 69 74 28 64 62 29 3b 0a 7d 0a  Fts3Init(db);.}.
303f2 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f  #endif..#endif /
303f3 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
303f4 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
303f5 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
303f6 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _FTS3) */../****
303f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
303f8 66 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f fts3.c *******
303f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
303fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
303fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
303fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
303fd 20 66 69 6c 65 20 66 74 73 33 5f 68 61 73 68 2e   file fts3_hash.
303fe 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
303ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
30401 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
30402 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  22.**.** The aut
30403 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
30404 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
30405 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
30406 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
30407 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
30408 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
30409 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
3040a 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
3040b 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
3040c 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
3040d 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
3040e 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
3040f 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
30410 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
30411 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
30412 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
30413 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
30414 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30415 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30416 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30417 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30418 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65  *.** This is the
30419 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
3041a 6f 66 20 67 65 6e 65 72 69 63 20 68 61 73 68 2d  of generic hash-
3041b 74 61 62 6c 65 73 20 75 73 65 64 20 69 6e 20 53  tables used in S
3041c 51 4c 69 74 65 2e 0a 2a 2a 20 57 65 27 76 65 20  QLite..** We've 
3041d 6d 6f 64 69 66 69 65 64 20 69 74 20 73 6c 69 67  modified it slig
3041e 68 74 6c 79 20 74 6f 20 73 65 72 76 65 20 61 73  htly to serve as
3041f 20 61 20 73 74 61 6e 64 61 6c 6f 6e 65 20 68 61   a standalone ha
30420 73 68 20 74 61 62 6c 65 0a 2a 2a 20 69 6d 70 6c  sh table.** impl
30421 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 74  ementation for t
30422 68 65 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64  he full-text ind
30423 65 78 69 6e 67 20 6d 6f 64 75 6c 65 2e 0a 2a 2f  exing module..*/
30424 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65  ../*.** The code
30425 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73   in this file is
30426 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69   only compiled i
30427 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54  f:.**.**     * T
30428 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69  he FTS3 module i
30429 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20 61 73  s being built as
3042a 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a   an extension.**
3042b 20 20 20 20 20 20 20 28 69 6e 20 77 68 69 63 68         (in which
3042c 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 52   case SQLITE_COR
3042d 45 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64  E is not defined
3042e 29 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ), or.**.**     
3042f 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c  * The FTS3 modul
30430 65 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74  e is being built
30431 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 65 20 6f   into the core o
30432 66 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 69 74  f.**       SQLit
30433 65 20 28 69 6e 20 77 68 69 63 68 20 63 61 73 65  e (in which case
30434 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46   SQLITE_ENABLE_F
30435 54 53 33 20 69 73 20 64 65 66 69 6e 65 64 29 2e  TS3 is defined).
30436 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .*/.#if !defined
30437 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c  (SQLITE_CORE) ||
30438 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
30439 45 4e 41 42 4c 45 5f 46 54 53 33 29 0a 0a 0a 0a  ENABLE_FTS3)....
3043a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 61 6e 64  /*.** Malloc and
3043b 20 46 72 65 65 20 66 75 6e 63 74 69 6f 6e 73 0a   Free functions.
3043c 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  */.static void *
3043d 66 74 73 33 48 61 73 68 4d 61 6c 6c 6f 63 28 69  fts3HashMalloc(i
3043e 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70  nt n){.  void *p
3043f 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
30440 63 28 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b  c(n);.  if( p ){
30441 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30  .    memset(p, 0
30442 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , n);.  }.  retu
30443 72 6e 20 70 3b 0a 7d 0a 73 74 61 74 69 63 20 76  rn p;.}.static v
30444 6f 69 64 20 66 74 73 33 48 61 73 68 46 72 65 65  oid fts3HashFree
30445 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 73 71 6c  (void *p){.  sql
30446 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a  ite3_free(p);.}.
30447 0a 2f 2a 20 54 75 72 6e 20 62 75 6c 6b 20 6d 65  ./* Turn bulk me
30448 6d 6f 72 79 20 69 6e 74 6f 20 61 20 68 61 73 68  mory into a hash
30449 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 62 79   table object by
3044a 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68   initializing th
3044b 65 0a 2a 2a 20 66 69 65 6c 64 73 20 6f 66 20 74  e.** fields of t
3044c 68 65 20 48 61 73 68 20 73 74 72 75 63 74 75 72  he Hash structur
3044d 65 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e 65 77 22 20  e..**.** "pNew" 
3044e 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
3044f 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 74  the hash table t
30450 68 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e 69  hat is to be ini
30451 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 6b 65 79  tialized..** key
30452 43 6c 61 73 73 20 69 73 20 6f 6e 65 20 6f 66 20  Class is one of 
30453 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 0a 2a  the constants .*
30454 2a 20 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41  * FTS3_HASH_BINA
30455 52 59 20 6f 72 20 46 54 53 33 5f 48 41 53 48 5f  RY or FTS3_HASH_
30456 53 54 52 49 4e 47 2e 20 20 54 68 65 20 76 61 6c  STRING.  The val
30457 75 65 20 6f 66 20 6b 65 79 43 6c 61 73 73 20 0a  ue of keyClass .
30458 2a 2a 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68  ** determines wh
30459 61 74 20 6b 69 6e 64 20 6f 66 20 6b 65 79 20 74  at kind of key t
3045a 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 77 69  he hash table wi
3045b 6c 6c 20 75 73 65 2e 20 20 22 63 6f 70 79 4b 65  ll use.  "copyKe
3045c 79 22 20 69 73 0a 2a 2a 20 74 72 75 65 20 69 66  y" is.** true if
3045d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
3045e 73 68 6f 75 6c 64 20 6d 61 6b 65 20 69 74 73 20  should make its 
3045f 6f 77 6e 20 70 72 69 76 61 74 65 20 63 6f 70 79  own private copy
30460 20 6f 66 20 6b 65 79 73 20 61 6e 64 0a 2a 2a 20   of keys and.** 
30461 66 61 6c 73 65 20 69 66 20 69 74 20 73 68 6f 75  false if it shou
30462 6c 64 20 6a 75 73 74 20 75 73 65 20 74 68 65 20  ld just use the 
30463 73 75 70 70 6c 69 65 64 20 70 6f 69 6e 74 65 72  supplied pointer
30464 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
30465 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
30466 46 74 73 33 48 61 73 68 49 6e 69 74 28 66 74 73  Fts3HashInit(fts
30467 33 48 61 73 68 20 2a 70 4e 65 77 2c 20 69 6e 74  3Hash *pNew, int
30468 20 6b 65 79 43 6c 61 73 73 2c 20 69 6e 74 20 63   keyClass, int c
30469 6f 70 79 4b 65 79 29 7b 0a 20 20 61 73 73 65 72  opyKey){.  asser
3046a 74 28 20 70 4e 65 77 21 3d 30 20 29 3b 0a 20 20  t( pNew!=0 );.  
3046b 61 73 73 65 72 74 28 20 6b 65 79 43 6c 61 73 73  assert( keyClass
3046c 3e 3d 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49  >=FTS3_HASH_STRI
3046d 4e 47 20 26 26 20 6b 65 79 43 6c 61 73 73 3c 3d  NG && keyClass<=
3046e 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41 52 59  FTS3_HASH_BINARY
3046f 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 6b 65 79 43   );.  pNew->keyC
30470 6c 61 73 73 20 3d 20 6b 65 79 43 6c 61 73 73 3b  lass = keyClass;
30471 0a 20 20 70 4e 65 77 2d 3e 63 6f 70 79 4b 65 79  .  pNew->copyKey
30472 20 3d 20 63 6f 70 79 4b 65 79 3b 0a 20 20 70 4e   = copyKey;.  pN
30473 65 77 2d 3e 66 69 72 73 74 20 3d 20 30 3b 0a 20  ew->first = 0;. 
30474 20 70 4e 65 77 2d 3e 63 6f 75 6e 74 20 3d 20 30   pNew->count = 0
30475 3b 0a 20 20 70 4e 65 77 2d 3e 68 74 73 69 7a 65  ;.  pNew->htsize
30476 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 68 74   = 0;.  pNew->ht
30477 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f   = 0;.}../* Remo
30478 76 65 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 66  ve all entries f
30479 72 6f 6d 20 61 20 68 61 73 68 20 74 61 62 6c 65  rom a hash table
3047a 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d  .  Reclaim all m
3047b 65 6d 6f 72 79 2e 0a 2a 2a 20 43 61 6c 6c 20 74  emory..** Call t
3047c 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 64  his routine to d
3047d 65 6c 65 74 65 20 61 20 68 61 73 68 20 74 61 62  elete a hash tab
3047e 6c 65 20 6f 72 20 74 6f 20 72 65 73 65 74 20 61  le or to reset a
3047f 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2a 20 74   hash table.** t
30480 6f 20 74 68 65 20 65 6d 70 74 79 20 73 74 61 74  o the empty stat
30481 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
30482 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
30483 33 46 74 73 33 48 61 73 68 43 6c 65 61 72 28 66  3Fts3HashClear(f
30484 74 73 33 48 61 73 68 20 2a 70 48 29 7b 0a 20 20  ts3Hash *pH){.  
30485 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 6c  fts3HashElem *el
30486 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 46  em;         /* F
30487 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
30488 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20  all elements of 
30489 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20  the table */..  
3048a 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b  assert( pH!=0 );
3048b 0a 20 20 65 6c 65 6d 20 3d 20 70 48 2d 3e 66 69  .  elem = pH->fi
3048c 72 73 74 3b 0a 20 20 70 48 2d 3e 66 69 72 73 74  rst;.  pH->first
3048d 20 3d 20 30 3b 0a 20 20 66 74 73 33 48 61 73 68   = 0;.  fts3Hash
3048e 46 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20 20  Free(pH->ht);.  
3048f 70 48 2d 3e 68 74 20 3d 20 30 3b 0a 20 20 70 48  pH->ht = 0;.  pH
30490 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20  ->htsize = 0;.  
30491 77 68 69 6c 65 28 20 65 6c 65 6d 20 29 7b 0a 20  while( elem ){. 
30492 20 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20     fts3HashElem 
30493 2a 6e 65 78 74 5f 65 6c 65 6d 20 3d 20 65 6c 65  *next_elem = ele
30494 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 69 66 28  m->next;.    if(
30495 20 70 48 2d 3e 63 6f 70 79 4b 65 79 20 26 26 20   pH->copyKey && 
30496 65 6c 65 6d 2d 3e 70 4b 65 79 20 29 7b 0a 20 20  elem->pKey ){.  
30497 20 20 20 20 66 74 73 33 48 61 73 68 46 72 65 65      fts3HashFree
30498 28 65 6c 65 6d 2d 3e 70 4b 65 79 29 3b 0a 20 20  (elem->pKey);.  
30499 20 20 7d 0a 20 20 20 20 66 74 73 33 48 61 73 68    }.    fts3Hash
3049a 46 72 65 65 28 65 6c 65 6d 29 3b 0a 20 20 20 20  Free(elem);.    
3049b 65 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d  elem = next_elem
3049c 3b 0a 20 20 7d 0a 20 20 70 48 2d 3e 63 6f 75 6e  ;.  }.  pH->coun
3049d 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  t = 0;.}../*.** 
3049e 48 61 73 68 20 61 6e 64 20 63 6f 6d 70 61 72 69  Hash and compari
3049f 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 77 68  son functions wh
304a0 65 6e 20 74 68 65 20 6d 6f 64 65 20 69 73 20 46  en the mode is F
304a1 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 0a  TS3_HASH_STRING.
304a2 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
304a3 73 33 53 74 72 48 61 73 68 28 63 6f 6e 73 74 20  s3StrHash(const 
304a4 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20  void *pKey, int 
304a5 6e 4b 65 79 29 7b 0a 20 20 63 6f 6e 73 74 20 63  nKey){.  const c
304a6 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 20  har *z = (const 
304a7 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a 20 20 69  char *)pKey;.  i
304a8 6e 74 20 68 20 3d 20 30 3b 0a 20 20 69 66 28 20  nt h = 0;.  if( 
304a9 6e 4b 65 79 3c 3d 30 20 29 20 6e 4b 65 79 20 3d  nKey<=0 ) nKey =
304aa 20 28 69 6e 74 29 20 73 74 72 6c 65 6e 28 7a 29   (int) strlen(z)
304ab 3b 0a 20 20 77 68 69 6c 65 28 20 6e 4b 65 79 20  ;.  while( nKey 
304ac 3e 20 30 20 20 29 7b 0a 20 20 20 20 68 20 3d 20  > 0  ){.    h = 
304ad 28 68 3c 3c 33 29 20 5e 20 68 20 5e 20 2a 7a 2b  (h<<3) ^ h ^ *z+
304ae 2b 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d 3b 0a 20  +;.    nKey--;. 
304af 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 20 26 20   }.  return h & 
304b0 30 78 37 66 66 66 66 66 66 66 3b 0a 7d 0a 73 74  0x7fffffff;.}.st
304b1 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 74 72  atic int fts3Str
304b2 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 76 6f  Compare(const vo
304b3 69 64 20 2a 70 4b 65 79 31 2c 20 69 6e 74 20 6e  id *pKey1, int n
304b4 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  1, const void *p
304b5 4b 65 79 32 2c 20 69 6e 74 20 6e 32 29 7b 0a 20  Key2, int n2){. 
304b6 20 69 66 28 20 6e 31 21 3d 6e 32 20 29 20 72 65   if( n1!=n2 ) re
304b7 74 75 72 6e 20 31 3b 0a 20 20 72 65 74 75 72 6e  turn 1;.  return
304b8 20 73 74 72 6e 63 6d 70 28 28 63 6f 6e 73 74 20   strncmp((const 
304b9 63 68 61 72 2a 29 70 4b 65 79 31 2c 28 63 6f 6e  char*)pKey1,(con
304ba 73 74 20 63 68 61 72 2a 29 70 4b 65 79 32 2c 6e  st char*)pKey2,n
304bb 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 61 73  1);.}../*.** Has
304bc 68 20 61 6e 64 20 63 6f 6d 70 61 72 69 73 6f 6e  h and comparison
304bd 20 66 75 6e 63 74 69 6f 6e 73 20 77 68 65 6e 20   functions when 
304be 74 68 65 20 6d 6f 64 65 20 69 73 20 46 54 53 33  the mode is FTS3
304bf 5f 48 41 53 48 5f 42 49 4e 41 52 59 0a 2a 2f 0a  _HASH_BINARY.*/.
304c0 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 42  static int fts3B
304c1 69 6e 48 61 73 68 28 63 6f 6e 73 74 20 76 6f 69  inHash(const voi
304c2 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65  d *pKey, int nKe
304c3 79 29 7b 0a 20 20 69 6e 74 20 68 20 3d 20 30 3b  y){.  int h = 0;
304c4 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
304c5 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
304c6 29 70 4b 65 79 3b 0a 20 20 77 68 69 6c 65 28 20  )pKey;.  while( 
304c7 6e 4b 65 79 2d 2d 20 3e 20 30 20 29 7b 0a 20 20  nKey-- > 0 ){.  
304c8 20 20 68 20 3d 20 28 68 3c 3c 33 29 20 5e 20 68    h = (h<<3) ^ h
304c9 20 5e 20 2a 28 7a 2b 2b 29 3b 0a 20 20 7d 0a 20   ^ *(z++);.  }. 
304ca 20 72 65 74 75 72 6e 20 68 20 26 20 30 78 37 66   return h & 0x7f
304cb 66 66 66 66 66 66 3b 0a 7d 0a 73 74 61 74 69 63  ffffff;.}.static
304cc 20 69 6e 74 20 66 74 73 33 42 69 6e 43 6f 6d 70   int fts3BinComp
304cd 61 72 65 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a  are(const void *
304ce 70 4b 65 79 31 2c 20 69 6e 74 20 6e 31 2c 20 63  pKey1, int n1, c
304cf 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32  onst void *pKey2
304d0 2c 20 69 6e 74 20 6e 32 29 7b 0a 20 20 69 66 28  , int n2){.  if(
304d1 20 6e 31 21 3d 6e 32 20 29 20 72 65 74 75 72 6e   n1!=n2 ) return
304d2 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 6d 65 6d   1;.  return mem
304d3 63 6d 70 28 70 4b 65 79 31 2c 70 4b 65 79 32 2c  cmp(pKey1,pKey2,
304d4 6e 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  n1);.}../*.** Re
304d5 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
304d6 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  o the appropriat
304d7 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e 20  e hash function 
304d8 67 69 76 65 6e 20 74 68 65 20 6b 65 79 20 63 6c  given the key cl
304d9 61 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43  ass..**.** The C
304da 20 73 79 6e 74 61 78 20 69 6e 20 74 68 69 73 20   syntax in this 
304db 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74  function definit
304dc 69 6f 6e 20 6d 61 79 20 62 65 20 75 6e 66 61 6d  ion may be unfam
304dd 69 6c 61 72 20 74 6f 20 73 6f 6d 65 20 0a 2a 2a  ilar to some .**
304de 20 70 72 6f 67 72 61 6d 6d 65 72 73 2c 20 73 6f   programmers, so
304df 20 77 65 20 70 72 6f 76 69 64 65 20 74 68 65 20   we provide the 
304e0 66 6f 6c 6c 6f 77 69 6e 67 20 61 64 64 69 74 69  following additi
304e1 6f 6e 61 6c 20 65 78 70 6c 61 6e 61 74 69 6f 6e  onal explanation
304e2 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65  :.**.** The name
304e3 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
304e4 20 69 73 20 22 66 74 73 48 61 73 68 46 75 6e 63   is "ftsHashFunc
304e5 74 69 6f 6e 22 2e 20 20 54 68 65 20 66 75 6e 63  tion".  The func
304e6 74 69 6f 6e 20 74 61 6b 65 73 20 61 0a 2a 2a 20  tion takes a.** 
304e7 73 69 6e 67 6c 65 20 70 61 72 61 6d 65 74 65 72  single parameter
304e8 20 22 6b 65 79 43 6c 61 73 73 22 2e 20 20 54 68   "keyClass".  Th
304e9 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f  e return value o
304ea 66 20 66 74 73 48 61 73 68 46 75 6e 63 74 69 6f  f ftsHashFunctio
304eb 6e 28 29 0a 2a 2a 20 69 73 20 61 20 70 6f 69 6e  n().** is a poin
304ec 74 65 72 20 74 6f 20 61 6e 6f 74 68 65 72 20 66  ter to another f
304ed 75 6e 63 74 69 6f 6e 2e 20 20 53 70 65 63 69 66  unction.  Specif
304ee 69 63 61 6c 6c 79 2c 20 74 68 65 20 72 65 74 75  ically, the retu
304ef 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 66  rn value.** of f
304f0 74 73 48 61 73 68 46 75 6e 63 74 69 6f 6e 28 29  tsHashFunction()
304f1 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
304f2 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74   a function that
304f3 20 74 61 6b 65 73 20 74 77 6f 20 70 61 72 61 6d   takes two param
304f4 65 74 65 72 73 0a 2a 2a 20 77 69 74 68 20 74 79  eters.** with ty
304f5 70 65 73 20 22 63 6f 6e 73 74 20 76 6f 69 64 2a  pes "const void*
304f6 22 20 61 6e 64 20 22 69 6e 74 22 20 61 6e 64 20  " and "int" and 
304f7 72 65 74 75 72 6e 73 20 61 6e 20 22 69 6e 74 22  returns an "int"
304f8 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
304f9 28 2a 66 74 73 48 61 73 68 46 75 6e 63 74 69 6f  (*ftsHashFunctio
304fa 6e 28 69 6e 74 20 6b 65 79 43 6c 61 73 73 29 29  n(int keyClass))
304fb 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  (const void*,int
304fc 29 7b 0a 20 20 69 66 28 20 6b 65 79 43 6c 61 73  ){.  if( keyClas
304fd 73 3d 3d 46 54 53 33 5f 48 41 53 48 5f 53 54 52  s==FTS3_HASH_STR
304fe 49 4e 47 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ING ){.    retur
304ff 6e 20 26 66 74 73 33 53 74 72 48 61 73 68 3b 0a  n &fts3StrHash;.
30500 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
30501 65 72 74 28 20 6b 65 79 43 6c 61 73 73 3d 3d 46  ert( keyClass==F
30502 54 53 33 5f 48 41 53 48 5f 42 49 4e 41 52 59 20  TS3_HASH_BINARY 
30503 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 66  );.    return &f
30504 74 73 33 42 69 6e 48 61 73 68 3b 0a 20 20 7d 0a  ts3BinHash;.  }.
30505 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
30506 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
30507 20 61 70 70 72 6f 70 72 69 61 74 65 20 68 61 73   appropriate has
30508 68 20 66 75 6e 63 74 69 6f 6e 20 67 69 76 65 6e  h function given
30509 20 74 68 65 20 6b 65 79 20 63 6c 61 73 73 2e 0a   the key class..
3050a 2a 2a 0a 2a 2a 20 46 6f 72 20 68 65 6c 70 20 69  **.** For help i
3050b 6e 20 69 6e 74 65 72 70 72 65 74 65 64 20 74 68  n interpreted th
3050c 65 20 6f 62 73 63 75 72 65 20 43 20 63 6f 64 65  e obscure C code
3050d 20 69 6e 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   in the function
3050e 20 64 65 66 69 6e 69 74 69 6f 6e 2c 0a 2a 2a 20   definition,.** 
3050f 73 65 65 20 74 68 65 20 68 65 61 64 65 72 20 63  see the header c
30510 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68 65 20 70 72  omment on the pr
30511 65 76 69 6f 75 73 20 66 75 6e 63 74 69 6f 6e 2e  evious function.
30512 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 28  .*/.static int (
30513 2a 66 74 73 43 6f 6d 70 61 72 65 46 75 6e 63 74  *ftsCompareFunct
30514 69 6f 6e 28 69 6e 74 20 6b 65 79 43 6c 61 73 73  ion(int keyClass
30515 29 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  ))(const void*,i
30516 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  nt,const void*,i
30517 6e 74 29 7b 0a 20 20 69 66 28 20 6b 65 79 43 6c  nt){.  if( keyCl
30518 61 73 73 3d 3d 46 54 53 33 5f 48 41 53 48 5f 53  ass==FTS3_HASH_S
30519 54 52 49 4e 47 20 29 7b 0a 20 20 20 20 72 65 74  TRING ){.    ret
3051a 75 72 6e 20 26 66 74 73 33 53 74 72 43 6f 6d 70  urn &fts3StrComp
3051b 61 72 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  are;.  }else{.  
3051c 20 20 61 73 73 65 72 74 28 20 6b 65 79 43 6c 61    assert( keyCla
3051d 73 73 3d 3d 46 54 53 33 5f 48 41 53 48 5f 42 49  ss==FTS3_HASH_BI
3051e 4e 41 52 59 20 29 3b 0a 20 20 20 20 72 65 74 75  NARY );.    retu
3051f 72 6e 20 26 66 74 73 33 42 69 6e 43 6f 6d 70 61  rn &fts3BinCompa
30520 72 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 4c 69  re;.  }.}../* Li
30521 6e 6b 20 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e  nk an element in
30522 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  to the hash tabl
30523 65 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  e.*/.static void
30524 20 66 74 73 33 48 61 73 68 49 6e 73 65 72 74 45   fts3HashInsertE
30525 6c 65 6d 65 6e 74 28 0a 20 20 66 74 73 33 48 61  lement(.  fts3Ha
30526 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20 20  sh *pH,         
30527 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65     /* The comple
30528 74 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f  te hash table */
30529 0a 20 20 73 74 72 75 63 74 20 5f 66 74 73 33 68  .  struct _fts3h
3052a 74 20 2a 70 45 6e 74 72 79 2c 20 20 2f 2a 20 54  t *pEntry,  /* T
3052b 68 65 20 65 6e 74 72 79 20 69 6e 74 6f 20 77 68  he entry into wh
3052c 69 63 68 20 70 4e 65 77 20 69 73 20 69 6e 73 65  ich pNew is inse
3052d 72 74 65 64 20 2a 2f 0a 20 20 66 74 73 33 48 61  rted */.  fts3Ha
3052e 73 68 45 6c 65 6d 20 2a 70 4e 65 77 20 20 20 20  shElem *pNew    
3052f 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e     /* The elemen
30530 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64  t to be inserted
30531 20 2a 2f 0a 29 7b 0a 20 20 66 74 73 33 48 61 73   */.){.  fts3Has
30532 68 45 6c 65 6d 20 2a 70 48 65 61 64 3b 20 20 20  hElem *pHead;   
30533 20 20 2f 2a 20 46 69 72 73 74 20 65 6c 65 6d 65    /* First eleme
30534 6e 74 20 61 6c 72 65 61 64 79 20 69 6e 20 70 45  nt already in pE
30535 6e 74 72 79 20 2a 2f 0a 20 20 70 48 65 61 64 20  ntry */.  pHead 
30536 3d 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3b  = pEntry->chain;
30537 0a 20 20 69 66 28 20 70 48 65 61 64 20 29 7b 0a  .  if( pHead ){.
30538 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78 74 20 3d      pNew->next =
30539 20 70 48 65 61 64 3b 0a 20 20 20 20 70 4e 65 77   pHead;.    pNew
3053a 2d 3e 70 72 65 76 20 3d 20 70 48 65 61 64 2d 3e  ->prev = pHead->
3053b 70 72 65 76 3b 0a 20 20 20 20 69 66 28 20 70 48  prev;.    if( pH
3053c 65 61 64 2d 3e 70 72 65 76 20 29 7b 20 70 48 65  ead->prev ){ pHe
3053d 61 64 2d 3e 70 72 65 76 2d 3e 6e 65 78 74 20 3d  ad->prev->next =
3053e 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 65 6c 73   pNew; }.    els
3053f 65 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  e             { 
30540 70 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65 77  pH->first = pNew
30541 3b 20 7d 0a 20 20 20 20 70 48 65 61 64 2d 3e 70  ; }.    pHead->p
30542 72 65 76 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 65  rev = pNew;.  }e
30543 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e  lse{.    pNew->n
30544 65 78 74 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b  ext = pH->first;
30545 0a 20 20 20 20 69 66 28 20 70 48 2d 3e 66 69 72  .    if( pH->fir
30546 73 74 20 29 7b 20 70 48 2d 3e 66 69 72 73 74 2d  st ){ pH->first-
30547 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 20 7d 0a  >prev = pNew; }.
30548 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d      pNew->prev =
30549 20 30 3b 0a 20 20 20 20 70 48 2d 3e 66 69 72 73   0;.    pH->firs
3054a 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20  t = pNew;.  }.  
3054b 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2b 2b 3b  pEntry->count++;
3054c 0a 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e  .  pEntry->chain
3054d 20 3d 20 70 4e 65 77 3b 0a 7d 0a 0a 0a 2f 2a 20   = pNew;.}.../* 
3054e 52 65 73 69 7a 65 20 74 68 65 20 68 61 73 68 20  Resize the hash 
3054f 74 61 62 6c 65 20 73 6f 20 74 68 61 74 20 69 74  table so that it
30550 20 63 61 6e 74 61 69 6e 73 20 22 6e 65 77 5f 73   cantains "new_s
30551 69 7a 65 22 20 62 75 63 6b 65 74 73 2e 0a 2a 2a  ize" buckets..**
30552 20 22 6e 65 77 5f 73 69 7a 65 22 20 6d 75 73 74   "new_size" must
30553 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32   be a power of 2
30554 2e 20 20 54 68 65 20 68 61 73 68 20 74 61 62 6c  .  The hash tabl
30555 65 20 6d 69 67 68 74 20 66 61 69 6c 20 0a 2a 2a  e might fail .**
30556 20 74 6f 20 72 65 73 69 7a 65 20 69 66 20 73 71   to resize if sq
30557 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 66 61 69  liteMalloc() fai
30558 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ls..*/.static vo
30559 69 64 20 66 74 73 33 52 65 68 61 73 68 28 66 74  id fts3Rehash(ft
3055a 73 33 48 61 73 68 20 2a 70 48 2c 20 69 6e 74 20  s3Hash *pH, int 
3055b 6e 65 77 5f 73 69 7a 65 29 7b 0a 20 20 73 74 72  new_size){.  str
3055c 75 63 74 20 5f 66 74 73 33 68 74 20 2a 6e 65 77  uct _fts3ht *new
3055d 5f 68 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  _ht;          /*
3055e 20 54 68 65 20 6e 65 77 20 68 61 73 68 20 74 61   The new hash ta
3055f 62 6c 65 20 2a 2f 0a 20 20 66 74 73 33 48 61 73  ble */.  fts3Has
30560 68 45 6c 65 6d 20 2a 65 6c 65 6d 2c 20 2a 6e 65  hElem *elem, *ne
30561 78 74 5f 65 6c 65 6d 3b 20 20 2f 2a 20 46 6f 72  xt_elem;  /* For
30562 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65 78   looping over ex
30563 69 73 74 69 6e 67 20 65 6c 65 6d 65 6e 74 73 20  isting elements 
30564 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 48 61 73 68  */.  int (*xHash
30565 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  )(const void*,in
30566 74 29 3b 20 20 20 2f 2a 20 54 68 65 20 68 61 73  t);   /* The has
30567 68 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20  h function */.. 
30568 20 61 73 73 65 72 74 28 20 28 6e 65 77 5f 73 69   assert( (new_si
30569 7a 65 20 26 20 28 6e 65 77 5f 73 69 7a 65 2d 31  ze & (new_size-1
3056a 29 29 3d 3d 30 20 29 3b 0a 20 20 6e 65 77 5f 68  ))==0 );.  new_h
3056b 74 20 3d 20 28 73 74 72 75 63 74 20 5f 66 74 73  t = (struct _fts
3056c 33 68 74 20 2a 29 66 74 73 33 48 61 73 68 4d 61  3ht *)fts3HashMa
3056d 6c 6c 6f 63 28 20 6e 65 77 5f 73 69 7a 65 2a 73  lloc( new_size*s
3056e 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 66 74  izeof(struct _ft
3056f 73 33 68 74 29 20 29 3b 0a 20 20 69 66 28 20 6e  s3ht) );.  if( n
30570 65 77 5f 68 74 3d 3d 30 20 29 20 72 65 74 75 72  ew_ht==0 ) retur
30571 6e 3b 0a 20 20 66 74 73 33 48 61 73 68 46 72 65  n;.  fts3HashFre
30572 65 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d  e(pH->ht);.  pH-
30573 3e 68 74 20 3d 20 6e 65 77 5f 68 74 3b 0a 20 20  >ht = new_ht;.  
30574 70 48 2d 3e 68 74 73 69 7a 65 20 3d 20 6e 65 77  pH->htsize = new
30575 5f 73 69 7a 65 3b 0a 20 20 78 48 61 73 68 20 3d  _size;.  xHash =
30576 20 66 74 73 48 61 73 68 46 75 6e 63 74 69 6f 6e   ftsHashFunction
30577 28 70 48 2d 3e 6b 65 79 43 6c 61 73 73 29 3b 0a  (pH->keyClass);.
30578 20 20 66 6f 72 28 65 6c 65 6d 3d 70 48 2d 3e 66    for(elem=pH->f
30579 69 72 73 74 2c 20 70 48 2d 3e 66 69 72 73 74 3d  irst, pH->first=
3057a 30 3b 20 65 6c 65 6d 3b 20 65 6c 65 6d 20 3d 20  0; elem; elem = 
3057b 6e 65 78 74 5f 65 6c 65 6d 29 7b 0a 20 20 20 20  next_elem){.    
3057c 69 6e 74 20 68 20 3d 20 28 2a 78 48 61 73 68 29  int h = (*xHash)
3057d 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20 65 6c 65  (elem->pKey, ele
3057e 6d 2d 3e 6e 4b 65 79 29 20 26 20 28 6e 65 77 5f  m->nKey) & (new_
3057f 73 69 7a 65 2d 31 29 3b 0a 20 20 20 20 6e 65 78  size-1);.    nex
30580 74 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e  t_elem = elem->n
30581 65 78 74 3b 0a 20 20 20 20 66 74 73 33 48 61 73  ext;.    fts3Has
30582 68 49 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70  hInsertElement(p
30583 48 2c 20 26 6e 65 77 5f 68 74 5b 68 5d 2c 20 65  H, &new_ht[h], e
30584 6c 65 6d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20  lem);.  }.}../* 
30585 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 28 66  This function (f
30586 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 20  or internal use 
30587 6f 6e 6c 79 29 20 6c 6f 63 61 74 65 73 20 61 6e  only) locates an
30588 20 65 6c 65 6d 65 6e 74 20 69 6e 20 61 6e 0a 2a   element in an.*
30589 2a 20 68 61 73 68 20 74 61 62 6c 65 20 74 68 61  * hash table tha
3058a 74 20 6d 61 74 63 68 65 73 20 74 68 65 20 67 69  t matches the gi
3058b 76 65 6e 20 6b 65 79 2e 20 20 54 68 65 20 68 61  ven key.  The ha
3058c 73 68 20 66 6f 72 20 74 68 69 73 20 6b 65 79 20  sh for this key 
3058d 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62  has.** already b
3058e 65 65 6e 20 63 6f 6d 70 75 74 65 64 20 61 6e 64  een computed and
3058f 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68   is passed as th
30590 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 2e  e 4th parameter.
30591 0a 2a 2f 0a 73 74 61 74 69 63 20 66 74 73 33 48  .*/.static fts3H
30592 61 73 68 45 6c 65 6d 20 2a 66 74 73 33 46 69 6e  ashElem *fts3Fin
30593 64 45 6c 65 6d 65 6e 74 42 79 48 61 73 68 28 0a  dElementByHash(.
30594 20 20 63 6f 6e 73 74 20 66 74 73 33 48 61 73 68    const fts3Hash
30595 20 2a 70 48 2c 20 2f 2a 20 54 68 65 20 70 48 20   *pH, /* The pH 
30596 74 6f 20 62 65 20 73 65 61 72 63 68 65 64 20 2a  to be searched *
30597 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
30598 70 4b 65 79 2c 20 20 20 2f 2a 20 54 68 65 20 6b  pKey,   /* The k
30599 65 79 20 77 65 20 61 72 65 20 73 65 61 72 63 68  ey we are search
3059a 69 6e 67 20 66 6f 72 20 2a 2f 0a 20 20 69 6e 74  ing for */.  int
3059b 20 6e 4b 65 79 2c 0a 20 20 69 6e 74 20 68 20 20   nKey,.  int h  
3059c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3059d 54 68 65 20 68 61 73 68 20 66 6f 72 20 74 68 69  The hash for thi
3059e 73 20 6b 65 79 2e 20 2a 2f 0a 29 7b 0a 20 20 66  s key. */.){.  f
3059f 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65  ts3HashElem *ele
305a0 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  m;            /*
305a1 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68   Used to loop th
305a2 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c  ru the element l
305a3 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 75  ist */.  int cou
305a4 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nt;             
305a5 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
305a6 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 6c 65  r of elements le
305a7 66 74 20 74 6f 20 74 65 73 74 20 2a 2f 0a 20 20  ft to test */.  
305a8 69 6e 74 20 28 2a 78 43 6f 6d 70 61 72 65 29 28  int (*xCompare)(
305a9 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
305aa 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29  const void*,int)
305ab 3b 20 20 2f 2a 20 63 6f 6d 70 61 72 69 73 6f 6e  ;  /* comparison
305ac 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20   function */..  
305ad 69 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20  if( pH->ht ){.  
305ae 20 20 73 74 72 75 63 74 20 5f 66 74 73 33 68 74    struct _fts3ht
305af 20 2a 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e   *pEntry = &pH->
305b0 68 74 5b 68 5d 3b 0a 20 20 20 20 65 6c 65 6d 20  ht[h];.    elem 
305b1 3d 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3b  = pEntry->chain;
305b2 0a 20 20 20 20 63 6f 75 6e 74 20 3d 20 70 45 6e  .    count = pEn
305b3 74 72 79 2d 3e 63 6f 75 6e 74 3b 0a 20 20 20 20  try->count;.    
305b4 78 43 6f 6d 70 61 72 65 20 3d 20 66 74 73 43 6f  xCompare = ftsCo
305b5 6d 70 61 72 65 46 75 6e 63 74 69 6f 6e 28 70 48  mpareFunction(pH
305b6 2d 3e 6b 65 79 43 6c 61 73 73 29 3b 0a 20 20 20  ->keyClass);.   
305b7 20 77 68 69 6c 65 28 20 63 6f 75 6e 74 2d 2d 20   while( count-- 
305b8 26 26 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 20  && elem ){.     
305b9 20 69 66 28 20 28 2a 78 43 6f 6d 70 61 72 65 29   if( (*xCompare)
305ba 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 65 6c 65 6d  (elem->pKey,elem
305bb 2d 3e 6e 4b 65 79 2c 70 4b 65 79 2c 6e 4b 65 79  ->nKey,pKey,nKey
305bc 29 3d 3d 30 20 29 7b 20 0a 20 20 20 20 20 20 20  )==0 ){ .       
305bd 20 72 65 74 75 72 6e 20 65 6c 65 6d 3b 0a 20 20   return elem;.  
305be 20 20 20 20 7d 0a 20 20 20 20 20 20 65 6c 65 6d      }.      elem
305bf 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20   = elem->next;. 
305c0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
305c1 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76  n 0;.}../* Remov
305c2 65 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79  e a single entry
305c3 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74   from the hash t
305c4 61 62 6c 65 20 67 69 76 65 6e 20 61 20 70 6f 69  able given a poi
305c5 6e 74 65 72 20 74 6f 20 74 68 61 74 0a 2a 2a 20  nter to that.** 
305c6 65 6c 65 6d 65 6e 74 20 61 6e 64 20 61 20 68 61  element and a ha
305c7 73 68 20 6f 6e 20 74 68 65 20 65 6c 65 6d 65 6e  sh on the elemen
305c8 74 27 73 20 6b 65 79 2e 0a 2a 2f 0a 73 74 61 74  t's key..*/.stat
305c9 69 63 20 76 6f 69 64 20 66 74 73 33 52 65 6d 6f  ic void fts3Remo
305ca 76 65 45 6c 65 6d 65 6e 74 42 79 48 61 73 68 28  veElementByHash(
305cb 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70 48 2c  .  fts3Hash *pH,
305cc 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
305cd 70 48 20 63 6f 6e 74 61 69 6e 69 6e 67 20 22 65  pH containing "e
305ce 6c 65 6d 22 20 2a 2f 0a 20 20 66 74 73 33 48 61  lem" */.  fts3Ha
305cf 73 68 45 6c 65 6d 2a 20 65 6c 65 6d 2c 20 20 20  shElem* elem,   
305d0 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74  /* The element t
305d1 6f 20 62 65 20 72 65 6d 6f 76 65 64 20 66 72 6f  o be removed fro
305d2 6d 20 74 68 65 20 70 48 20 2a 2f 0a 20 20 69 6e  m the pH */.  in
305d3 74 20 68 20 20 20 20 20 20 20 20 20 20 20 20 20  t h             
305d4 20 20 20 20 2f 2a 20 48 61 73 68 20 76 61 6c 75      /* Hash valu
305d5 65 20 66 6f 72 20 74 68 65 20 65 6c 65 6d 65 6e  e for the elemen
305d6 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74  t */.){.  struct
305d7 20 5f 66 74 73 33 68 74 20 2a 70 45 6e 74 72 79   _fts3ht *pEntry
305d8 3b 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e 70 72  ;.  if( elem->pr
305d9 65 76 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e  ev ){.    elem->
305da 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20 65 6c 65  prev->next = ele
305db 6d 2d 3e 6e 65 78 74 3b 20 0a 20 20 7d 65 6c 73  m->next; .  }els
305dc 65 7b 0a 20 20 20 20 70 48 2d 3e 66 69 72 73 74  e{.    pH->first
305dd 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20   = elem->next;. 
305de 20 7d 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e 6e   }.  if( elem->n
305df 65 78 74 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d  ext ){.    elem-
305e0 3e 6e 65 78 74 2d 3e 70 72 65 76 20 3d 20 65 6c  >next->prev = el
305e1 65 6d 2d 3e 70 72 65 76 3b 0a 20 20 7d 0a 20 20  em->prev;.  }.  
305e2 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74  pEntry = &pH->ht
305e3 5b 68 5d 3b 0a 20 20 69 66 28 20 70 45 6e 74 72  [h];.  if( pEntr
305e4 79 2d 3e 63 68 61 69 6e 3d 3d 65 6c 65 6d 20 29  y->chain==elem )
305e5 7b 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 68  {.    pEntry->ch
305e6 61 69 6e 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74  ain = elem->next
305e7 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 2d 3e  ;.  }.  pEntry->
305e8 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 28 20 70  count--;.  if( p
305e9 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3c 3d 30 20  Entry->count<=0 
305ea 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e 63  ){.    pEntry->c
305eb 68 61 69 6e 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  hain = 0;.  }.  
305ec 69 66 28 20 70 48 2d 3e 63 6f 70 79 4b 65 79 20  if( pH->copyKey 
305ed 26 26 20 65 6c 65 6d 2d 3e 70 4b 65 79 20 29 7b  && elem->pKey ){
305ee 0a 20 20 20 20 66 74 73 33 48 61 73 68 46 72 65  .    fts3HashFre
305ef 65 28 65 6c 65 6d 2d 3e 70 4b 65 79 29 3b 0a 20  e(elem->pKey);. 
305f0 20 7d 0a 20 20 66 74 73 33 48 61 73 68 46 72 65   }.  fts3HashFre
305f1 65 28 20 65 6c 65 6d 20 29 3b 0a 20 20 70 48 2d  e( elem );.  pH-
305f2 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 28 20  >count--;.  if( 
305f3 70 48 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a  pH->count<=0 ){.
305f4 20 20 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e      assert( pH->
305f5 66 69 72 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20  first==0 );.    
305f6 61 73 73 65 72 74 28 20 70 48 2d 3e 63 6f 75 6e  assert( pH->coun
305f7 74 3d 3d 30 20 29 3b 0a 20 20 20 20 66 74 73 33  t==0 );.    fts3
305f8 48 61 73 68 43 6c 65 61 72 28 70 48 29 3b 0a 20  HashClear(pH);. 
305f9 20 7d 0a 7d 0a 0a 2f 2a 20 41 74 74 65 6d 70 74   }.}../* Attempt
305fa 20 74 6f 20 6c 6f 63 61 74 65 20 61 6e 20 65 6c   to locate an el
305fb 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 68 61 73  ement of the has
305fc 68 20 74 61 62 6c 65 20 70 48 20 77 69 74 68 20  h table pH with 
305fd 61 20 6b 65 79 0a 2a 2a 20 74 68 61 74 20 6d 61  a key.** that ma
305fe 74 63 68 65 73 20 70 4b 65 79 2c 6e 4b 65 79 2e  tches pKey,nKey.
305ff 20 20 52 65 74 75 72 6e 20 74 68 65 20 64 61 74    Return the dat
30600 61 20 66 6f 72 20 74 68 69 73 20 65 6c 65 6d 65  a for this eleme
30601 6e 74 20 69 66 20 69 74 20 69 73 0a 2a 2a 20 66  nt if it is.** f
30602 6f 75 6e 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66  ound, or NULL if
30603 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74   there is no mat
30604 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ch..*/.SQLITE_PR
30605 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
30606 74 65 33 46 74 73 33 48 61 73 68 46 69 6e 64 28  te3Fts3HashFind(
30607 63 6f 6e 73 74 20 66 74 73 33 48 61 73 68 20 2a  const fts3Hash *
30608 70 48 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  pH, const void *
30609 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b  pKey, int nKey){
3060a 0a 20 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20  .  int h;       
3060b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 68            /* A h
3060c 61 73 68 20 6f 6e 20 6b 65 79 20 2a 2f 0a 20 20  ash on key */.  
3060d 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 6c  fts3HashElem *el
3060e 65 6d 3b 20 20 20 20 2f 2a 20 54 68 65 20 65 6c  em;    /* The el
3060f 65 6d 65 6e 74 20 74 68 61 74 20 6d 61 74 63 68  ement that match
30610 65 73 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20  es key */.  int 
30611 28 2a 78 48 61 73 68 29 28 63 6f 6e 73 74 20 76  (*xHash)(const v
30612 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 54  oid*,int);  /* T
30613 68 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e  he hash function
30614 20 2a 2f 0a 0a 20 20 69 66 28 20 70 48 3d 3d 30   */..  if( pH==0
30615 20 7c 7c 20 70 48 2d 3e 68 74 3d 3d 30 20 29 20   || pH->ht==0 ) 
30616 72 65 74 75 72 6e 20 30 3b 0a 20 20 78 48 61 73  return 0;.  xHas
30617 68 20 3d 20 66 74 73 48 61 73 68 46 75 6e 63 74  h = ftsHashFunct
30618 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c 61 73 73  ion(pH->keyClass
30619 29 3b 0a 20 20 61 73 73 65 72 74 28 20 78 48 61  );.  assert( xHa
3061a 73 68 21 3d 30 20 29 3b 0a 20 20 68 20 3d 20 28  sh!=0 );.  h = (
3061b 2a 78 48 61 73 68 29 28 70 4b 65 79 2c 6e 4b 65  *xHash)(pKey,nKe
3061c 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70  y);.  assert( (p
3061d 48 2d 3e 68 74 73 69 7a 65 20 26 20 28 70 48 2d  H->htsize & (pH-
3061e 3e 68 74 73 69 7a 65 2d 31 29 29 3d 3d 30 20 29  >htsize-1))==0 )
3061f 3b 0a 20 20 65 6c 65 6d 20 3d 20 66 74 73 33 46  ;.  elem = fts3F
30620 69 6e 64 45 6c 65 6d 65 6e 74 42 79 48 61 73 68  indElementByHash
30621 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c 20 68  (pH,pKey,nKey, h
30622 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31   & (pH->htsize-1
30623 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 65 6c 65  ));.  return ele
30624 6d 20 3f 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3a  m ? elem->data :
30625 20 30 3b 0a 7d 0a 0a 2f 2a 20 49 6e 73 65 72 74   0;.}../* Insert
30626 20 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f   an element into
30627 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
30628 70 48 2e 20 20 54 68 65 20 6b 65 79 20 69 73 20  pH.  The key is 
30629 70 4b 65 79 2c 6e 4b 65 79 0a 2a 2a 20 61 6e 64  pKey,nKey.** and
3062a 20 74 68 65 20 64 61 74 61 20 69 73 20 22 64 61   the data is "da
3062b 74 61 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f  ta"..**.** If no
3062c 20 65 6c 65 6d 65 6e 74 20 65 78 69 73 74 73 20   element exists 
3062d 77 69 74 68 20 61 20 6d 61 74 63 68 69 6e 67 20  with a matching 
3062e 6b 65 79 2c 20 74 68 65 6e 20 61 20 6e 65 77 0a  key, then a new.
3062f 2a 2a 20 65 6c 65 6d 65 6e 74 20 69 73 20 63 72  ** element is cr
30630 65 61 74 65 64 2e 20 20 41 20 63 6f 70 79 20 6f  eated.  A copy o
30631 66 20 74 68 65 20 6b 65 79 20 69 73 20 6d 61 64  f the key is mad
30632 65 20 69 66 20 74 68 65 20 63 6f 70 79 4b 65 79  e if the copyKey
30633 0a 2a 2a 20 66 6c 61 67 20 69 73 20 73 65 74 2e  .** flag is set.
30634 20 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e    NULL is return
30635 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f  ed..**.** If ano
30636 74 68 65 72 20 65 6c 65 6d 65 6e 74 20 61 6c 72  ther element alr
30637 65 61 64 79 20 65 78 69 73 74 73 20 77 69 74 68  eady exists with
30638 20 74 68 65 20 73 61 6d 65 20 6b 65 79 2c 20 74   the same key, t
30639 68 65 6e 20 74 68 65 0a 2a 2a 20 6e 65 77 20 64  hen the.** new d
3063a 61 74 61 20 72 65 70 6c 61 63 65 73 20 74 68 65  ata replaces the
3063b 20 6f 6c 64 20 64 61 74 61 20 61 6e 64 20 74 68   old data and th
3063c 65 20 6f 6c 64 20 64 61 74 61 20 69 73 20 72 65  e old data is re
3063d 74 75 72 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 6b  turned..** The k
3063e 65 79 20 69 73 20 6e 6f 74 20 63 6f 70 69 65 64  ey is not copied
3063f 20 69 6e 20 74 68 69 73 20 69 6e 73 74 61 6e 63   in this instanc
30640 65 2e 20 20 49 66 20 61 20 6d 61 6c 6c 6f 63 20  e.  If a malloc 
30641 66 61 69 6c 73 2c 20 74 68 65 6e 0a 2a 2a 20 74  fails, then.** t
30642 68 65 20 6e 65 77 20 64 61 74 61 20 69 73 20 72  he new data is r
30643 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20  eturned and the 
30644 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 75 6e  hash table is un
30645 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  changed..**.** I
30646 66 20 74 68 65 20 22 64 61 74 61 22 20 70 61 72  f the "data" par
30647 61 6d 65 74 65 72 20 74 6f 20 74 68 69 73 20 66  ameter to this f
30648 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c  unction is NULL,
30649 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 65 6c 65   then the.** ele
3064a 6d 65 6e 74 20 63 6f 72 72 65 73 70 6f 6e 64 69  ment correspondi
3064b 6e 67 20 74 6f 20 22 6b 65 79 22 20 69 73 20 72  ng to "key" is r
3064c 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20  emoved from the 
3064d 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53  hash table..*/.S
3064e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
3064f 69 64 20 2a 73 71 6c 69 74 65 33 46 74 73 33 48  id *sqlite3Fts3H
30650 61 73 68 49 6e 73 65 72 74 28 0a 20 20 66 74 73  ashInsert(.  fts
30651 33 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20 20  3Hash *pH,      
30652 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 74 61    /* The hash ta
30653 62 6c 65 20 74 6f 20 69 6e 73 65 72 74 20 69 6e  ble to insert in
30654 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  to */.  const vo
30655 69 64 20 2a 70 4b 65 79 2c 20 20 20 20 2f 2a 20  id *pKey,    /* 
30656 54 68 65 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74  The key */.  int
30657 20 6e 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20   nKey,          
30658 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
30659 79 74 65 73 20 69 6e 20 74 68 65 20 6b 65 79 20  ytes in the key 
3065a 2a 2f 0a 20 20 76 6f 69 64 20 2a 64 61 74 61 20  */.  void *data 
3065b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
3065c 20 64 61 74 61 20 2a 2f 0a 29 7b 0a 20 20 69 6e   data */.){.  in
3065d 74 20 68 72 61 77 3b 20 20 20 20 20 20 20 20 20  t hraw;         
3065e 20 20 20 20 20 20 20 20 2f 2a 20 52 61 77 20 68          /* Raw h
3065f 61 73 68 20 76 61 6c 75 65 20 6f 66 20 74 68 65  ash value of the
30660 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 68 3b   key */.  int h;
30661 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30662 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73 68 20      /* the hash 
30663 6f 66 20 74 68 65 20 6b 65 79 20 6d 6f 64 75 6c  of the key modul
30664 6f 20 68 61 73 68 20 74 61 62 6c 65 20 73 69 7a  o hash table siz
30665 65 20 2a 2f 0a 20 20 66 74 73 33 48 61 73 68 45  e */.  fts3HashE
30666 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20  lem *elem;      
30667 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70   /* Used to loop
30668 20 74 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e   thru the elemen
30669 74 20 6c 69 73 74 20 2a 2f 0a 20 20 66 74 73 33  t list */.  fts3
3066a 48 61 73 68 45 6c 65 6d 20 2a 6e 65 77 5f 65 6c  HashElem *new_el
3066b 65 6d 3b 20 20 20 2f 2a 20 4e 65 77 20 65 6c 65  em;   /* New ele
3066c 6d 65 6e 74 20 61 64 64 65 64 20 74 6f 20 74 68  ment added to th
3066d 65 20 70 48 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  e pH */.  int (*
3066e 78 48 61 73 68 29 28 63 6f 6e 73 74 20 76 6f 69  xHash)(const voi
3066f 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 54 68 65  d*,int);  /* The
30670 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e 20 2a   hash function *
30671 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 48 21  /..  assert( pH!
30672 3d 30 20 29 3b 0a 20 20 78 48 61 73 68 20 3d 20  =0 );.  xHash = 
30673 66 74 73 48 61 73 68 46 75 6e 63 74 69 6f 6e 28  ftsHashFunction(
30674 70 48 2d 3e 6b 65 79 43 6c 61 73 73 29 3b 0a 20  pH->keyClass);. 
30675 20 61 73 73 65 72 74 28 20 78 48 61 73 68 21 3d   assert( xHash!=
30676 30 20 29 3b 0a 20 20 68 72 61 77 20 3d 20 28 2a  0 );.  hraw = (*
30677 78 48 61 73 68 29 28 70 4b 65 79 2c 20 6e 4b 65  xHash)(pKey, nKe
30678 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70  y);.  assert( (p
30679 48 2d 3e 68 74 73 69 7a 65 20 26 20 28 70 48 2d  H->htsize & (pH-
3067a 3e 68 74 73 69 7a 65 2d 31 29 29 3d 3d 30 20 29  >htsize-1))==0 )
3067b 3b 0a 20 20 68 20 3d 20 68 72 61 77 20 26 20 28  ;.  h = hraw & (
3067c 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29 3b 0a 20  pH->htsize-1);. 
3067d 20 65 6c 65 6d 20 3d 20 66 74 73 33 46 69 6e 64   elem = fts3Find
3067e 45 6c 65 6d 65 6e 74 42 79 48 61 73 68 28 70 48  ElementByHash(pH
3067f 2c 70 4b 65 79 2c 6e 4b 65 79 2c 68 29 3b 0a 20  ,pKey,nKey,h);. 
30680 20 69 66 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20   if( elem ){.   
30681 20 76 6f 69 64 20 2a 6f 6c 64 5f 64 61 74 61 20   void *old_data 
30682 3d 20 65 6c 65 6d 2d 3e 64 61 74 61 3b 0a 20 20  = elem->data;.  
30683 20 20 69 66 28 20 64 61 74 61 3d 3d 30 20 29 7b    if( data==0 ){
30684 0a 20 20 20 20 20 20 66 74 73 33 52 65 6d 6f 76  .      fts3Remov
30685 65 45 6c 65 6d 65 6e 74 42 79 48 61 73 68 28 70  eElementByHash(p
30686 48 2c 65 6c 65 6d 2c 68 29 3b 0a 20 20 20 20 7d  H,elem,h);.    }
30687 65 6c 73 65 7b 0a 20 20 20 20 20 20 65 6c 65 6d  else{.      elem
30688 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 3b 0a 20  ->data = data;. 
30689 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
3068a 6f 6c 64 5f 64 61 74 61 3b 0a 20 20 7d 0a 20 20  old_data;.  }.  
3068b 69 66 28 20 64 61 74 61 3d 3d 30 20 29 20 72 65  if( data==0 ) re
3068c 74 75 72 6e 20 30 3b 0a 20 20 6e 65 77 5f 65 6c  turn 0;.  new_el
3068d 65 6d 20 3d 20 28 66 74 73 33 48 61 73 68 45 6c  em = (fts3HashEl
3068e 65 6d 2a 29 66 74 73 33 48 61 73 68 4d 61 6c 6c  em*)fts3HashMall
3068f 6f 63 28 20 73 69 7a 65 6f 66 28 66 74 73 33 48  oc( sizeof(fts3H
30690 61 73 68 45 6c 65 6d 29 20 29 3b 0a 20 20 69 66  ashElem) );.  if
30691 28 20 6e 65 77 5f 65 6c 65 6d 3d 3d 30 20 29 20  ( new_elem==0 ) 
30692 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 69  return data;.  i
30693 66 28 20 70 48 2d 3e 63 6f 70 79 4b 65 79 20 26  f( pH->copyKey &
30694 26 20 70 4b 65 79 21 3d 30 20 29 7b 0a 20 20 20  & pKey!=0 ){.   
30695 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20   new_elem->pKey 
30696 3d 20 66 74 73 33 48 61 73 68 4d 61 6c 6c 6f 63  = fts3HashMalloc
30697 28 20 6e 4b 65 79 20 29 3b 0a 20 20 20 20 69 66  ( nKey );.    if
30698 28 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79  ( new_elem->pKey
30699 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 74 73  ==0 ){.      fts
3069a 33 48 61 73 68 46 72 65 65 28 6e 65 77 5f 65 6c  3HashFree(new_el
3069b 65 6d 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  em);.      retur
3069c 6e 20 64 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20  n data;.    }.  
3069d 20 20 6d 65 6d 63 70 79 28 28 76 6f 69 64 2a 29    memcpy((void*)
3069e 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20  new_elem->pKey, 
3069f 70 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 7d  pKey, nKey);.  }
306a0 65 6c 73 65 7b 0a 20 20 20 20 6e 65 77 5f 65 6c  else{.    new_el
306a1 65 6d 2d 3e 70 4b 65 79 20 3d 20 28 76 6f 69 64  em->pKey = (void
306a2 2a 29 70 4b 65 79 3b 0a 20 20 7d 0a 20 20 6e 65  *)pKey;.  }.  ne
306a3 77 5f 65 6c 65 6d 2d 3e 6e 4b 65 79 20 3d 20 6e  w_elem->nKey = n
306a4 4b 65 79 3b 0a 20 20 70 48 2d 3e 63 6f 75 6e 74  Key;.  pH->count
306a5 2b 2b 3b 0a 20 20 69 66 28 20 70 48 2d 3e 68 74  ++;.  if( pH->ht
306a6 73 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 66  size==0 ){.    f
306a7 74 73 33 52 65 68 61 73 68 28 70 48 2c 38 29 3b  ts3Rehash(pH,8);
306a8 0a 20 20 20 20 69 66 28 20 70 48 2d 3e 68 74 73  .    if( pH->hts
306a9 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ize==0 ){.      
306aa 70 48 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20  pH->count = 0;. 
306ab 20 20 20 20 20 66 74 73 33 48 61 73 68 46 72 65       fts3HashFre
306ac 65 28 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 20  e(new_elem);.   
306ad 20 20 20 72 65 74 75 72 6e 20 64 61 74 61 3b 0a     return data;.
306ae 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
306af 70 48 2d 3e 63 6f 75 6e 74 20 3e 20 70 48 2d 3e  pH->count > pH->
306b0 68 74 73 69 7a 65 20 29 7b 0a 20 20 20 20 66 74  htsize ){.    ft
306b1 73 33 52 65 68 61 73 68 28 70 48 2c 70 48 2d 3e  s3Rehash(pH,pH->
306b2 68 74 73 69 7a 65 2a 32 29 3b 0a 20 20 7d 0a 20  htsize*2);.  }. 
306b3 20 61 73 73 65 72 74 28 20 70 48 2d 3e 68 74 73   assert( pH->hts
306b4 69 7a 65 3e 30 20 29 3b 0a 20 20 61 73 73 65 72  ize>0 );.  asser
306b5 74 28 20 28 70 48 2d 3e 68 74 73 69 7a 65 20 26  t( (pH->htsize &
306b6 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29   (pH->htsize-1))
306b7 3d 3d 30 20 29 3b 0a 20 20 68 20 3d 20 68 72 61  ==0 );.  h = hra
306b8 77 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d  w & (pH->htsize-
306b9 31 29 3b 0a 20 20 66 74 73 33 48 61 73 68 49 6e  1);.  fts3HashIn
306ba 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20  sertElement(pH, 
306bb 26 70 48 2d 3e 68 74 5b 68 5d 2c 20 6e 65 77 5f  &pH->ht[h], new_
306bc 65 6c 65 6d 29 3b 0a 20 20 6e 65 77 5f 65 6c 65  elem);.  new_ele
306bd 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 3b 0a  m->data = data;.
306be 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 23    return 0;.}..#
306bf 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
306c0 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c  d(SQLITE_CORE) |
306c1 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
306c2 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f  _ENABLE_FTS3) */
306c3 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
306c4 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 68 61  * End of fts3_ha
306c5 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  sh.c ***********
306c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
306c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
306c8 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
306c9 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73  * Begin file fts
306ca 33 5f 70 6f 72 74 65 72 2e 63 20 2a 2a 2a 2a 2a  3_porter.c *****
306cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
306cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
306cd 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 53 65 70  /./*.** 2006 Sep
306ce 74 65 6d 62 65 72 20 33 30 0a 2a 2a 0a 2a 2a 20  tember 30.**.** 
306cf 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
306d0 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
306d1 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
306d2 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
306d3 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
306d4 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
306d5 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
306d6 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
306d7 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
306d8 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
306d9 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
306da 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
306db 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
306dc 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
306dd 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
306de 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
306df 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
306e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
306e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
306e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
306e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
306e4 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 49 6d 70 6c  ********.** Impl
306e5 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
306e6 65 20 66 75 6c 6c 2d 74 65 78 74 2d 73 65 61 72  e full-text-sear
306e7 63 68 20 74 6f 6b 65 6e 69 7a 65 72 20 74 68 61  ch tokenizer tha
306e8 74 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20  t implements.** 
306e9 61 20 50 6f 72 74 65 72 20 73 74 65 6d 6d 65 72  a Porter stemmer
306ea 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ..*/../*.** The 
306eb 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c  code in this fil
306ec 65 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c  e is only compil
306ed 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ed if:.**.**    
306ee 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75   * The FTS3 modu
306ef 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c  le is being buil
306f0 74 20 61 73 20 61 6e 20 65 78 74 65 6e 73 69 6f  t as an extensio
306f1 6e 0a 2a 2a 20 20 20 20 20 20 20 28 69 6e 20 77  n.**       (in w
306f2 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45  hich case SQLITE
306f3 5f 43 4f 52 45 20 69 73 20 6e 6f 74 20 64 65 66  _CORE is not def
306f4 69 6e 65 64 29 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20  ined), or.**.** 
306f5 20 20 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d      * The FTS3 m
306f6 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62  odule is being b
306f7 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20 63 6f  uilt into the co
306f8 72 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 53  re of.**       S
306f9 51 4c 69 74 65 20 28 69 6e 20 77 68 69 63 68 20  QLite (in which 
306fa 63 61 73 65 20 53 51 4c 49 54 45 5f 45 4e 41 42  case SQLITE_ENAB
306fb 4c 45 5f 46 54 53 33 20 69 73 20 64 65 66 69 6e  LE_FTS3 is defin
306fc 65 64 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66  ed)..*/.#if !def
306fd 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45  ined(SQLITE_CORE
306fe 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
306ff 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29  ITE_ENABLE_FTS3)
30700 0a 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c 61 73 73  ...../*.** Class
30701 20 64 65 72 69 76 65 64 20 66 72 6f 6d 20 73 71   derived from sq
30702 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 0a  lite3_tokenizer.
30703 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
30704 74 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a  t porter_tokeniz
30705 65 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74  er {.  sqlite3_t
30706 6f 6b 65 6e 69 7a 65 72 20 62 61 73 65 3b 20 20  okenizer base;  
30707 20 20 20 20 2f 2a 20 42 61 73 65 20 63 6c 61 73      /* Base clas
30708 73 20 2a 2f 0a 7d 20 70 6f 72 74 65 72 5f 74 6f  s */.} porter_to
30709 6b 65 6e 69 7a 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20  kenizer;../*.** 
3070a 43 6c 61 73 73 20 64 65 72 69 76 65 64 20 66 72  Class derived fr
3070b 6f 6d 20 73 71 6c 69 74 33 5f 74 6f 6b 65 6e 69  om sqlit3_tokeni
3070c 7a 65 72 5f 63 75 72 73 6f 72 0a 2a 2f 0a 74 79  zer_cursor.*/.ty
3070d 70 65 64 65 66 20 73 74 72 75 63 74 20 70 6f 72  pedef struct por
3070e 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  ter_tokenizer_cu
3070f 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33  rsor {.  sqlite3
30710 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
30711 72 20 62 61 73 65 3b 0a 20 20 63 6f 6e 73 74 20  r base;.  const 
30712 63 68 61 72 20 2a 7a 49 6e 70 75 74 3b 20 20 20  char *zInput;   
30713 20 20 20 20 20 20 20 2f 2a 20 69 6e 70 75 74 20         /* input 
30714 77 65 20 61 72 65 20 74 6f 6b 65 6e 69 7a 69 6e  we are tokenizin
30715 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 70 75  g */.  int nInpu
30716 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
30717 20 20 20 20 2f 2a 20 73 69 7a 65 20 6f 66 20 74      /* size of t
30718 68 65 20 69 6e 70 75 74 20 2a 2f 0a 20 20 69 6e  he input */.  in
30719 74 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20  t iOffset;      
3071a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 75             /* cu
3071b 72 72 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20 69  rrent position i
3071c 6e 20 7a 49 6e 70 75 74 20 2a 2f 0a 20 20 69 6e  n zInput */.  in
3071d 74 20 69 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20  t iToken;       
3071e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e             /* in
3071f 64 65 78 20 6f 66 20 6e 65 78 74 20 74 6f 6b 65  dex of next toke
30720 6e 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64  n to be returned
30721 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 6f 6b   */.  char *zTok
30722 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  en;             
30723 20 20 20 2f 2a 20 73 74 6f 72 61 67 65 20 66 6f     /* storage fo
30724 72 20 63 75 72 72 65 6e 74 20 74 6f 6b 65 6e 20  r current token 
30725 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 61  */.  int nAlloca
30726 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  ted;            
30727 20 20 2f 2a 20 73 70 61 63 65 20 61 6c 6c 6f 63    /* space alloc
30728 61 74 65 64 20 74 6f 20 7a 54 6f 6b 65 6e 20 62  ated to zToken b
30729 75 66 66 65 72 20 2a 2f 0a 7d 20 70 6f 72 74 65  uffer */.} porte
3072a 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  r_tokenizer_curs
3072b 6f 72 3b 0a 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  or;.../* Forward
3072c 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a   declaration */.
3072d 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c  static const sql
3072e 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
3072f 6f 64 75 6c 65 20 70 6f 72 74 65 72 54 6f 6b 65  odule porterToke
30730 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b 0a 0a 0a 2f  nizerModule;.../
30731 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  *.** Create a ne
30732 77 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 73 74  w tokenizer inst
30733 61 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ance..*/.static 
30734 69 6e 74 20 70 6f 72 74 65 72 43 72 65 61 74 65  int porterCreate
30735 28 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f  (.  int argc, co
30736 6e 73 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74  nst char * const
30737 20 2a 61 72 67 76 2c 0a 20 20 73 71 6c 69 74 65   *argv,.  sqlite
30738 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70 70  3_tokenizer **pp
30739 54 6f 6b 65 6e 69 7a 65 72 0a 29 7b 0a 20 20 70  Tokenizer.){.  p
3073a 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 20  orter_tokenizer 
3073b 2a 74 3b 0a 20 20 74 20 3d 20 28 70 6f 72 74 65  *t;.  t = (porte
3073c 72 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 20 73  r_tokenizer *) s
3073d 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
3073e 7a 65 6f 66 28 2a 74 29 29 3b 0a 20 20 69 66 28  zeof(*t));.  if(
3073f 20 74 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72   t==NULL ) retur
30740 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
30741 20 20 6d 65 6d 73 65 74 28 74 2c 20 30 2c 20 73    memset(t, 0, s
30742 69 7a 65 6f 66 28 2a 74 29 29 3b 0a 20 20 2a 70  izeof(*t));.  *p
30743 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 26 74 2d  pTokenizer = &t-
30744 3e 62 61 73 65 3b 0a 20 20 72 65 74 75 72 6e 20  >base;.  return 
30745 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
30746 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 74 6f  .** Destroy a to
30747 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69  kenizer.*/.stati
30748 63 20 69 6e 74 20 70 6f 72 74 65 72 44 65 73 74  c int porterDest
30749 72 6f 79 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65  roy(sqlite3_toke
3074a 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65  nizer *pTokenize
3074b 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  r){.  sqlite3_fr
3074c 65 65 28 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a  ee(pTokenizer);.
3074d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3074e 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65  OK;.}../*.** Pre
3074f 70 61 72 65 20 74 6f 20 62 65 67 69 6e 20 74 6f  pare to begin to
30750 6b 65 6e 69 7a 69 6e 67 20 61 20 70 61 72 74 69  kenizing a parti
30751 63 75 6c 61 72 20 73 74 72 69 6e 67 2e 20 20 54  cular string.  T
30752 68 65 20 69 6e 70 75 74 0a 2a 2a 20 73 74 72 69  he input.** stri
30753 6e 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a  ng to be tokeniz
30754 65 64 20 69 73 20 7a 49 6e 70 75 74 5b 30 2e 2e  ed is zInput[0..
30755 6e 49 6e 70 75 74 2d 31 5d 2e 20 20 41 20 63 75  nInput-1].  A cu
30756 72 73 6f 72 0a 2a 2a 20 75 73 65 64 20 74 6f 20  rsor.** used to 
30757 69 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 74 6f  incrementally to
30758 6b 65 6e 69 7a 65 20 74 68 69 73 20 73 74 72 69  kenize this stri
30759 6e 67 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  ng is returned i
3075a 6e 20 0a 2a 2a 20 2a 70 70 43 75 72 73 6f 72 2e  n .** *ppCursor.
3075b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
3075c 6f 72 74 65 72 4f 70 65 6e 28 0a 20 20 73 71 6c  orterOpen(.  sql
3075d 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
3075e 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20  pTokenizer,     
3075f 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e      /* The token
30760 69 7a 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  izer */.  const 
30761 63 68 61 72 20 2a 7a 49 6e 70 75 74 2c 20 69 6e  char *zInput, in
30762 74 20 6e 49 6e 70 75 74 2c 20 20 20 20 20 20 20  t nInput,       
30763 20 2f 2a 20 53 74 72 69 6e 67 20 74 6f 20 62 65   /* String to be
30764 20 74 6f 6b 65 6e 69 7a 65 64 20 2a 2f 0a 20 20   tokenized */.  
30765 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
30766 72 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72  r_cursor **ppCur
30767 73 6f 72 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54  sor    /* OUT: T
30768 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73  okenization curs
30769 6f 72 20 2a 2f 0a 29 7b 0a 20 20 70 6f 72 74 65  or */.){.  porte
3076a 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  r_tokenizer_curs
3076b 6f 72 20 2a 63 3b 0a 0a 20 20 63 20 3d 20 28 70  or *c;..  c = (p
3076c 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f  orter_tokenizer_
3076d 63 75 72 73 6f 72 20 2a 29 20 73 71 6c 69 74 65  cursor *) sqlite
3076e 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
3076f 2a 63 29 29 3b 0a 20 20 69 66 28 20 63 3d 3d 4e  *c));.  if( c==N
30770 55 4c 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ULL ) return SQL
30771 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a 20 20 63 2d  ITE_NOMEM;..  c-
30772 3e 7a 49 6e 70 75 74 20 3d 20 7a 49 6e 70 75 74  >zInput = zInput
30773 3b 0a 20 20 69 66 28 20 7a 49 6e 70 75 74 3d 3d  ;.  if( zInput==
30774 30 20 29 7b 0a 20 20 20 20 63 2d 3e 6e 49 6e 70  0 ){.    c->nInp
30775 75 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20  ut = 0;.  }else 
30776 69 66 28 20 6e 49 6e 70 75 74 3c 30 20 29 7b 0a  if( nInput<0 ){.
30777 20 20 20 20 63 2d 3e 6e 49 6e 70 75 74 20 3d 20      c->nInput = 
30778 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 49 6e 70  (int)strlen(zInp
30779 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ut);.  }else{.  
3077a 20 20 63 2d 3e 6e 49 6e 70 75 74 20 3d 20 6e 49    c->nInput = nI
3077b 6e 70 75 74 3b 0a 20 20 7d 0a 20 20 63 2d 3e 69  nput;.  }.  c->i
3077c 4f 66 66 73 65 74 20 3d 20 30 3b 20 20 20 20 20  Offset = 0;     
3077d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
3077e 74 61 72 74 20 74 6f 6b 65 6e 69 7a 69 6e 67 20  tart tokenizing 
3077f 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
30780 20 2a 2f 0a 20 20 63 2d 3e 69 54 6f 6b 65 6e 20   */.  c->iToken 
30781 3d 20 30 3b 0a 20 20 63 2d 3e 7a 54 6f 6b 65 6e  = 0;.  c->zToken
30782 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20 20 20   = NULL;        
30783 20 20 20 20 20 20 20 2f 2a 20 6e 6f 20 73 70 61         /* no spa
30784 63 65 20 61 6c 6c 6f 63 61 74 65 64 2c 20 79 65  ce allocated, ye
30785 74 2e 20 2a 2f 0a 20 20 63 2d 3e 6e 41 6c 6c 6f  t. */.  c->nAllo
30786 63 61 74 65 64 20 3d 20 30 3b 0a 0a 20 20 2a 70  cated = 0;..  *p
30787 70 43 75 72 73 6f 72 20 3d 20 26 63 2d 3e 62 61  pCursor = &c->ba
30788 73 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  se;.  return SQL
30789 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
3078a 20 43 6c 6f 73 65 20 61 20 74 6f 6b 65 6e 69 7a   Close a tokeniz
3078b 61 74 69 6f 6e 20 63 75 72 73 6f 72 20 70 72 65  ation cursor pre
3078c 76 69 6f 75 73 6c 79 20 6f 70 65 6e 65 64 20 62  viously opened b
3078d 79 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 70  y a call to.** p
3078e 6f 72 74 65 72 4f 70 65 6e 28 29 20 61 62 6f 76  orterOpen() abov
3078f 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
30790 20 70 6f 72 74 65 72 43 6c 6f 73 65 28 73 71 6c   porterClose(sql
30791 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
30792 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b  ursor *pCursor){
30793 0a 20 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69  .  porter_tokeni
30794 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20  zer_cursor *c = 
30795 28 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65  (porter_tokenize
30796 72 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72  r_cursor *) pCur
30797 73 6f 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  sor;.  sqlite3_f
30798 72 65 65 28 63 2d 3e 7a 54 6f 6b 65 6e 29 3b 0a  ree(c->zToken);.
30799 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63    sqlite3_free(c
3079a 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
3079b 54 45 5f 4f 4b 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 56  TE_OK;.}./*.** V
3079c 6f 77 65 6c 20 6f 72 20 63 6f 6e 73 6f 6e 61 6e  owel or consonan
3079d 74 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  t.*/.static cons
3079e 74 20 63 68 61 72 20 63 54 79 70 65 5b 5d 20 3d  t char cType[] =
3079f 20 7b 0a 20 20 20 30 2c 20 31 2c 20 31 2c 20 31   {.   0, 1, 1, 1
307a0 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 0, 1, 1, 1, 0,
307a1 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
307a2 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
307a3 2c 20 30 2c 0a 20 20 20 31 2c 20 31 2c 20 31 2c  , 0,.   1, 1, 1,
307a4 20 32 2c 20 31 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20   2, 1.};../*.** 
307a5 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 29 20 61 6e  isConsonant() an
307a6 64 20 69 73 56 6f 77 65 6c 28 29 20 64 65 74 65  d isVowel() dete
307a7 72 6d 69 6e 65 20 69 66 20 74 68 65 69 72 20 66  rmine if their f
307a8 69 72 73 74 20 63 68 61 72 61 63 74 65 72 20 69  irst character i
307a9 6e 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20  n.** the string 
307aa 74 68 65 79 20 70 6f 69 6e 74 20 74 6f 20 69 73  they point to is
307ab 20 61 20 63 6f 6e 73 6f 6e 61 6e 74 20 6f 72 20   a consonant or 
307ac 61 20 76 6f 77 65 6c 2c 20 61 63 63 6f 72 64 69  a vowel, accordi
307ad 6e 67 0a 2a 2a 20 74 6f 20 50 6f 72 74 65 72 20  ng.** to Porter 
307ae 72 75 6c 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 41 20  ruls.  .**.** A 
307af 63 6f 6e 73 6f 6e 61 74 65 20 69 73 20 61 6e 79  consonate is any
307b0 20 6c 65 74 74 65 72 20 6f 74 68 65 72 20 74 68   letter other th
307b1 61 6e 20 27 61 27 2c 20 27 65 27 2c 20 27 69 27  an 'a', 'e', 'i'
307b2 2c 20 27 6f 27 2c 20 6f 72 20 27 75 27 2e 0a 2a  , 'o', or 'u'..*
307b3 2a 20 27 59 27 20 69 73 20 61 20 63 6f 6e 73 6f  * 'Y' is a conso
307b4 6e 61 6e 74 20 75 6e 6c 65 73 73 20 69 74 20 66  nant unless it f
307b5 6f 6c 6c 6f 77 73 20 61 6e 6f 74 68 65 72 20 63  ollows another c
307b6 6f 6e 73 6f 6e 61 6e 74 2c 0a 2a 2a 20 69 6e 20  onsonant,.** in 
307b7 77 68 69 63 68 20 63 61 73 65 20 69 74 20 69 73  which case it is
307b8 20 61 20 76 6f 77 65 6c 2e 0a 2a 2a 0a 2a 2a 20   a vowel..**.** 
307b9 49 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  In these routine
307ba 2c 20 74 68 65 20 6c 65 74 74 65 72 73 20 61 72  , the letters ar
307bb 65 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64  e in reverse ord
307bc 65 72 2e 20 20 53 6f 20 74 68 65 20 27 79 27 20  er.  So the 'y' 
307bd 72 75 6c 65 0a 2a 2a 20 69 73 20 74 68 61 74 20  rule.** is that 
307be 27 79 27 20 69 73 20 61 20 63 6f 6e 73 6f 6e 61  'y' is a consona
307bf 6e 74 20 75 6e 6c 65 73 73 20 69 74 20 69 73 20  nt unless it is 
307c0 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 6e 6f 74  followed by anot
307c1 68 65 72 0a 2a 2a 20 63 6f 6e 73 6f 6e 65 6e 74  her.** consonent
307c2 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
307c3 69 73 56 6f 77 65 6c 28 63 6f 6e 73 74 20 63 68  isVowel(const ch
307c4 61 72 2a 29 3b 0a 73 74 61 74 69 63 20 69 6e 74  ar*);.static int
307c5 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 63 6f 6e   isConsonant(con
307c6 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69  st char *z){.  i
307c7 6e 74 20 6a 3b 0a 20 20 63 68 61 72 20 78 20 3d  nt j;.  char x =
307c8 20 2a 7a 3b 0a 20 20 69 66 28 20 78 3d 3d 30 20   *z;.  if( x==0 
307c9 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 61 73  ) return 0;.  as
307ca 73 65 72 74 28 20 78 3e 3d 27 61 27 20 26 26 20  sert( x>='a' && 
307cb 78 3c 3d 27 7a 27 20 29 3b 0a 20 20 6a 20 3d 20  x<='z' );.  j = 
307cc 63 54 79 70 65 5b 78 2d 27 61 27 5d 3b 0a 20 20  cType[x-'a'];.  
307cd 69 66 28 20 6a 3c 32 20 29 20 72 65 74 75 72 6e  if( j<2 ) return
307ce 20 6a 3b 0a 20 20 72 65 74 75 72 6e 20 7a 5b 31   j;.  return z[1
307cf 5d 3d 3d 30 20 7c 7c 20 69 73 56 6f 77 65 6c 28  ]==0 || isVowel(
307d0 7a 20 2b 20 31 29 3b 0a 7d 0a 73 74 61 74 69 63  z + 1);.}.static
307d1 20 69 6e 74 20 69 73 56 6f 77 65 6c 28 63 6f 6e   int isVowel(con
307d2 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69  st char *z){.  i
307d3 6e 74 20 6a 3b 0a 20 20 63 68 61 72 20 78 20 3d  nt j;.  char x =
307d4 20 2a 7a 3b 0a 20 20 69 66 28 20 78 3d 3d 30 20   *z;.  if( x==0 
307d5 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 61 73  ) return 0;.  as
307d6 73 65 72 74 28 20 78 3e 3d 27 61 27 20 26 26 20  sert( x>='a' && 
307d7 78 3c 3d 27 7a 27 20 29 3b 0a 20 20 6a 20 3d 20  x<='z' );.  j = 
307d8 63 54 79 70 65 5b 78 2d 27 61 27 5d 3b 0a 20 20  cType[x-'a'];.  
307d9 69 66 28 20 6a 3c 32 20 29 20 72 65 74 75 72 6e  if( j<2 ) return
307da 20 31 2d 6a 3b 0a 20 20 72 65 74 75 72 6e 20 69   1-j;.  return i
307db 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 20 2b 20 31  sConsonant(z + 1
307dc 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 65 74 20  );.}../*.** Let 
307dd 61 6e 79 20 73 65 71 75 65 6e 63 65 20 6f 66 20  any sequence of 
307de 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 76 6f 77 65  one or more vowe
307df 6c 73 20 62 65 20 72 65 70 72 65 73 65 6e 74 65  ls be represente
307e0 64 20 62 79 20 56 20 61 6e 64 20 6c 65 74 0a 2a  d by V and let.*
307e1 2a 20 43 20 62 65 20 73 65 71 75 65 6e 63 65 20  * C be sequence 
307e2 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 63  of one or more c
307e3 6f 6e 73 6f 6e 61 6e 74 73 2e 20 20 54 68 65 6e  onsonants.  Then
307e4 20 65 76 65 72 79 20 77 6f 72 64 20 63 61 6e 20   every word can 
307e5 62 65 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 65  be.** represente
307e6 64 20 61 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  d as:.**.**     
307e7 20 20 20 20 20 20 5b 43 5d 20 28 56 43 29 7b 6d        [C] (VC){m
307e8 7d 20 5b 56 5d 0a 2a 2a 0a 2a 2a 20 49 6e 20 70  } [V].**.** In p
307e9 72 6f 73 65 3a 20 20 41 20 77 6f 72 64 20 69 73  rose:  A word is
307ea 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 63 6f 6e   an optional con
307eb 73 6f 6e 61 6e 74 20 66 6f 6c 6c 6f 77 65 64 20  sonant followed 
307ec 62 79 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 76 6f  by zero or.** vo
307ed 77 65 6c 2d 63 6f 6e 73 6f 6e 61 6e 74 20 70 61  wel-consonant pa
307ee 69 72 73 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  irs followed by 
307ef 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 76 6f 77 65  an optional vowe
307f0 6c 2e 20 20 22 6d 22 20 69 73 20 74 68 65 0a 2a  l.  "m" is the.*
307f1 2a 20 6e 75 6d 62 65 72 20 6f 66 20 76 6f 77 65  * number of vowe
307f2 6c 20 63 6f 6e 73 6f 6e 61 6e 74 20 70 61 69 72  l consonant pair
307f3 73 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  s.  This routine
307f4 20 63 6f 6d 70 75 74 65 73 20 74 68 65 20 76 61   computes the va
307f5 6c 75 65 0a 2a 2a 20 6f 66 20 6d 20 66 6f 72 20  lue.** of m for 
307f6 74 68 65 20 66 69 72 73 74 20 69 20 62 79 74 65  the first i byte
307f7 73 20 6f 66 20 61 20 77 6f 72 64 2e 0a 2a 2a 0a  s of a word..**.
307f8 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  ** Return true i
307f9 66 20 74 68 65 20 6d 2d 76 61 6c 75 65 20 66 6f  f the m-value fo
307fa 72 20 7a 20 69 73 20 31 20 6f 72 20 6d 6f 72 65  r z is 1 or more
307fb 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
307fc 73 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 74 72 75  s,.** return tru
307fd 65 20 69 66 20 7a 20 63 6f 6e 74 61 69 6e 73 20  e if z contains 
307fe 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 76 6f 77  at least one vow
307ff 65 6c 20 74 68 61 74 20 69 73 20 66 6f 6c 6c 6f  el that is follo
30800 77 65 64 0a 2a 2a 20 62 79 20 61 20 63 6f 6e 73  wed.** by a cons
30801 6f 6e 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  onant..**.** In 
30802 74 68 69 73 20 72 6f 75 74 69 6e 65 20 7a 5b 5d  this routine z[]
30803 20 69 73 20 69 6e 20 72 65 76 65 72 73 65 20 6f   is in reverse o
30804 72 64 65 72 2e 20 20 53 6f 20 77 65 20 61 72 65  rder.  So we are
30805 20 72 65 61 6c 6c 79 20 6c 6f 6f 6b 69 6e 67 0a   really looking.
30806 2a 2a 20 66 6f 72 20 61 6e 20 69 6e 73 74 61 6e  ** for an instan
30807 63 65 20 6f 66 20 6f 66 20 61 20 63 6f 6e 73 6f  ce of of a conso
30808 6e 61 6e 74 20 66 6f 6c 6c 6f 77 65 64 20 62 79  nant followed by
30809 20 61 20 76 6f 77 65 6c 2e 0a 2a 2f 0a 73 74 61   a vowel..*/.sta
3080a 74 69 63 20 69 6e 74 20 6d 5f 67 74 5f 30 28 63  tic int m_gt_0(c
3080b 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20  onst char *z){. 
3080c 20 77 68 69 6c 65 28 20 69 73 56 6f 77 65 6c 28   while( isVowel(
3080d 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69  z) ){ z++; }.  i
3080e 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72  f( *z==0 ) retur
3080f 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73  n 0;.  while( is
30810 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20  Consonant(z) ){ 
30811 7a 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20  z++; }.  return 
30812 2a 7a 21 3d 30 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b  *z!=0;.}../* Lik
30813 65 20 6d 67 74 30 20 61 62 6f 76 65 20 65 78 63  e mgt0 above exc
30814 65 70 74 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69  ept we are looki
30815 6e 67 20 66 6f 72 20 61 20 76 61 6c 75 65 20 6f  ng for a value o
30816 66 20 6d 20 77 68 69 63 68 20 69 73 0a 2a 2a 20  f m which is.** 
30817 65 78 61 63 74 6c 79 20 31 0a 2a 2f 0a 73 74 61  exactly 1.*/.sta
30818 74 69 63 20 69 6e 74 20 6d 5f 65 71 5f 31 28 63  tic int m_eq_1(c
30819 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20  onst char *z){. 
3081a 20 77 68 69 6c 65 28 20 69 73 56 6f 77 65 6c 28   while( isVowel(
3081b 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69  z) ){ z++; }.  i
3081c 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72  f( *z==0 ) retur
3081d 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73  n 0;.  while( is
3081e 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20  Consonant(z) ){ 
3081f 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d  z++; }.  if( *z=
30820 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
30821 20 77 68 69 6c 65 28 20 69 73 56 6f 77 65 6c 28   while( isVowel(
30822 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69  z) ){ z++; }.  i
30823 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72  f( *z==0 ) retur
30824 6e 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 69 73  n 1;.  while( is
30825 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20  Consonant(z) ){ 
30826 7a 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20  z++; }.  return 
30827 2a 7a 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b  *z==0;.}../* Lik
30828 65 20 6d 67 74 30 20 61 62 6f 76 65 20 65 78 63  e mgt0 above exc
30829 65 70 74 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69  ept we are looki
3082a 6e 67 20 66 6f 72 20 61 20 76 61 6c 75 65 20 6f  ng for a value o
3082b 66 20 6d 3e 31 20 69 6e 73 74 65 61 64 0a 2a 2a  f m>1 instead.**
3082c 20 6f 72 20 6d 3e 30 0a 2a 2f 0a 73 74 61 74 69   or m>0.*/.stati
3082d 63 20 69 6e 74 20 6d 5f 67 74 5f 31 28 63 6f 6e  c int m_gt_1(con
3082e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 77  st char *z){.  w
3082f 68 69 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a 29  hile( isVowel(z)
30830 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28   ){ z++; }.  if(
30831 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20   *z==0 ) return 
30832 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 43 6f  0;.  while( isCo
30833 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b  nsonant(z) ){ z+
30834 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30  +; }.  if( *z==0
30835 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77   ) return 0;.  w
30836 68 69 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a 29  hile( isVowel(z)
30837 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28   ){ z++; }.  if(
30838 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20   *z==0 ) return 
30839 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 43 6f  0;.  while( isCo
3083a 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b  nsonant(z) ){ z+
3083b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a  +; }.  return *z
3083c 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  !=0;.}../*.** Re
3083d 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65  turn TRUE if the
3083e 72 65 20 69 73 20 61 20 76 6f 77 65 6c 20 61 6e  re is a vowel an
3083f 79 77 68 65 72 65 20 77 69 74 68 69 6e 20 7a 5b  ywhere within z[
30840 30 2e 2e 6e 2d 31 5d 0a 2a 2f 0a 73 74 61 74 69  0..n-1].*/.stati
30841 63 20 69 6e 74 20 68 61 73 56 6f 77 65 6c 28 63  c int hasVowel(c
30842 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20  onst char *z){. 
30843 20 77 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e   while( isConson
30844 61 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d  ant(z) ){ z++; }
30845 0a 20 20 72 65 74 75 72 6e 20 2a 7a 21 3d 30 3b  .  return *z!=0;
30846 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
30847 20 54 52 55 45 20 69 66 20 74 68 65 20 77 6f 72   TRUE if the wor
30848 64 20 65 6e 64 73 20 69 6e 20 61 20 64 6f 75 62  d ends in a doub
30849 6c 65 20 63 6f 6e 73 6f 6e 61 6e 74 2e 0a 2a 2a  le consonant..**
3084a 0a 2a 2a 20 54 68 65 20 74 65 78 74 20 69 73 20  .** The text is 
3084b 72 65 76 65 72 73 65 64 20 68 65 72 65 2e 20 53  reversed here. S
3084c 6f 20 77 65 20 61 72 65 20 72 65 61 6c 6c 79 20  o we are really 
3084d 6c 6f 6f 6b 69 6e 67 20 61 74 0a 2a 2a 20 74 68  looking at.** th
3084e 65 20 66 69 72 73 74 20 74 77 6f 20 63 68 61 72  e first two char
3084f 61 63 74 65 72 73 20 6f 66 20 7a 5b 5d 2e 0a 2a  acters of z[]..*
30850 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f 75  /.static int dou
30851 62 6c 65 43 6f 6e 73 6f 6e 61 6e 74 28 63 6f 6e  bleConsonant(con
30852 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 72  st char *z){.  r
30853 65 74 75 72 6e 20 69 73 43 6f 6e 73 6f 6e 61 6e  eturn isConsonan
30854 74 28 7a 29 20 26 26 20 7a 5b 30 5d 3d 3d 7a 5b  t(z) && z[0]==z[
30855 31 5d 20 26 26 20 69 73 43 6f 6e 73 6f 6e 61 6e  1] && isConsonan
30856 74 28 7a 2b 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  t(z+1);.}../*.**
30857 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
30858 74 68 65 20 77 6f 72 64 20 65 6e 64 73 20 77 69  the word ends wi
30859 74 68 20 74 68 72 65 65 20 6c 65 74 74 65 72 73  th three letters
3085a 20 77 68 69 63 68 0a 2a 2a 20 61 72 65 20 63 6f   which.** are co
3085b 6e 73 6f 6e 61 6e 74 2d 76 6f 77 65 6c 2d 63 6f  nsonant-vowel-co
3085c 6e 73 6f 6e 65 6e 74 20 61 6e 64 20 77 68 65 72  nsonent and wher
3085d 65 20 74 68 65 20 66 69 6e 61 6c 20 63 6f 6e 73  e the final cons
3085e 6f 6e 61 6e 74 0a 2a 2a 20 69 73 20 6e 6f 74 20  onant.** is not 
3085f 27 77 27 2c 20 27 78 27 2c 20 6f 72 20 27 79 27  'w', 'x', or 'y'
30860 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 77 6f 72 64  ..**.** The word
30861 20 69 73 20 72 65 76 65 72 73 65 64 20 68 65 72   is reversed her
30862 65 2e 20 20 53 6f 20 77 65 20 61 72 65 20 72 65  e.  So we are re
30863 61 6c 6c 79 20 63 68 65 63 6b 69 6e 67 20 74 68  ally checking th
30864 65 0a 2a 2a 20 66 69 72 73 74 20 74 68 72 65 65  e.** first three
30865 20 6c 65 74 74 65 72 73 20 61 6e 64 20 74 68 65   letters and the
30866 20 66 69 72 73 74 20 6f 6e 65 20 63 61 6e 6e 6f   first one canno
30867 74 20 62 65 20 69 6e 20 5b 77 78 79 5d 2e 0a 2a  t be in [wxy]..*
30868 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 74 61  /.static int sta
30869 72 5f 6f 68 28 63 6f 6e 73 74 20 63 68 61 72 20  r_oh(const char 
3086a 2a 7a 29 7b 0a 20 20 72 65 74 75 72 6e 0a 20 20  *z){.  return.  
3086b 20 20 7a 5b 30 5d 21 3d 30 20 26 26 20 69 73 43    z[0]!=0 && isC
3086c 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 26 26 0a 20  onsonant(z) &&. 
3086d 20 20 20 7a 5b 30 5d 21 3d 27 77 27 20 26 26 20     z[0]!='w' && 
3086e 7a 5b 30 5d 21 3d 27 78 27 20 26 26 20 7a 5b 30  z[0]!='x' && z[0
3086f 5d 21 3d 27 79 27 20 26 26 0a 20 20 20 20 7a 5b  ]!='y' &&.    z[
30870 31 5d 21 3d 30 20 26 26 20 69 73 56 6f 77 65 6c  1]!=0 && isVowel
30871 28 7a 2b 31 29 20 26 26 0a 20 20 20 20 7a 5b 32  (z+1) &&.    z[2
30872 5d 21 3d 30 20 26 26 20 69 73 43 6f 6e 73 6f 6e  ]!=0 && isConson
30873 61 6e 74 28 7a 2b 32 29 3b 0a 7d 0a 0a 2f 2a 0a  ant(z+2);.}../*.
30874 2a 2a 20 49 66 20 74 68 65 20 77 6f 72 64 20 65  ** If the word e
30875 6e 64 73 20 77 69 74 68 20 7a 46 72 6f 6d 20 61  nds with zFrom a
30876 6e 64 20 78 43 6f 6e 64 28 29 20 69 73 20 74 72  nd xCond() is tr
30877 75 65 20 66 6f 72 20 74 68 65 20 73 74 65 6d 0a  ue for the stem.
30878 2a 2a 20 6f 66 20 74 68 65 20 77 6f 72 64 20 74  ** of the word t
30879 68 61 74 20 70 72 65 63 65 65 64 73 20 74 68 65  hat preceeds the
3087a 20 7a 46 72 6f 6d 20 65 6e 64 69 6e 67 2c 20 74   zFrom ending, t
3087b 68 65 6e 20 63 68 61 6e 67 65 20 74 68 65 20 0a  hen change the .
3087c 2a 2a 20 65 6e 64 69 6e 67 20 74 6f 20 7a 54 6f  ** ending to zTo
3087d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75  ..**.** The inpu
3087e 74 20 77 6f 72 64 20 2a 70 7a 20 61 6e 64 20 7a  t word *pz and z
3087f 46 72 6f 6d 20 61 72 65 20 62 6f 74 68 20 69 6e  From are both in
30880 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 2e 20   reverse order. 
30881 20 7a 54 6f 0a 2a 2a 20 69 73 20 69 6e 20 6e 6f   zTo.** is in no
30882 72 6d 61 6c 20 6f 72 64 65 72 2e 20 0a 2a 2a 0a  rmal order. .**.
30883 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
30884 66 20 7a 46 72 6f 6d 20 6d 61 74 63 68 65 73 2e  f zFrom matches.
30885 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45 20 69    Return FALSE i
30886 66 20 7a 46 72 6f 6d 20 64 6f 65 73 20 6e 6f 74  f zFrom does not
30887 0a 2a 2a 20 6d 61 74 63 68 2e 20 20 4e 6f 74 20  .** match.  Not 
30888 74 68 61 74 20 54 52 55 45 20 69 73 20 72 65 74  that TRUE is ret
30889 75 72 6e 65 64 20 65 76 65 6e 20 69 66 20 78 43  urned even if xC
3088a 6f 6e 64 28 29 20 66 61 69 6c 73 20 61 6e 64 0a  ond() fails and.
3088b 2a 2a 20 6e 6f 20 73 75 62 73 74 69 74 75 74 69  ** no substituti
3088c 6f 6e 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74  on occurs..*/.st
3088d 61 74 69 63 20 69 6e 74 20 73 74 65 6d 28 0a 20  atic int stem(. 
3088e 20 63 68 61 72 20 2a 2a 70 7a 2c 20 20 20 20 20   char **pz,     
3088f 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 77          /* The w
30890 6f 72 64 20 62 65 69 6e 67 20 73 74 65 6d 6d 65  ord being stemme
30891 64 20 28 52 65 76 65 72 73 65 64 29 20 2a 2f 0a  d (Reversed) */.
30892 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
30893 72 6f 6d 2c 20 20 20 20 20 2f 2a 20 49 66 20 74  rom,     /* If t
30894 68 65 20 65 6e 64 69 6e 67 20 6d 61 74 63 68 65  he ending matche
30895 73 20 74 68 69 73 2e 2e 2e 20 28 52 65 76 65 72  s this... (Rever
30896 73 65 64 29 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  sed) */.  const 
30897 63 68 61 72 20 2a 7a 54 6f 2c 20 20 20 20 20 20  char *zTo,      
30898 20 2f 2a 20 2e 2e 2e 20 63 68 61 6e 67 65 20 74   /* ... change t
30899 68 65 20 65 6e 64 69 6e 67 20 74 6f 20 74 68 69  he ending to thi
3089a 73 20 28 6e 6f 74 20 72 65 76 65 72 73 65 64 29  s (not reversed)
3089b 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e   */.  int (*xCon
3089c 64 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 20  d)(const char*) 
3089d 20 20 2f 2a 20 43 6f 6e 64 69 74 69 6f 6e 20 74    /* Condition t
3089e 68 61 74 20 6d 75 73 74 20 62 65 20 74 72 75 65  hat must be true
3089f 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a   */.){.  char *z
308a0 20 3d 20 2a 70 7a 3b 0a 20 20 77 68 69 6c 65 28   = *pz;.  while(
308a1 20 2a 7a 46 72 6f 6d 20 26 26 20 2a 7a 46 72 6f   *zFrom && *zFro
308a2 6d 3d 3d 2a 7a 20 29 7b 20 7a 2b 2b 3b 20 7a 46  m==*z ){ z++; zF
308a3 72 6f 6d 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a  rom++; }.  if( *
308a4 7a 46 72 6f 6d 21 3d 30 20 29 20 72 65 74 75 72  zFrom!=0 ) retur
308a5 6e 20 30 3b 0a 20 20 69 66 28 20 78 43 6f 6e 64  n 0;.  if( xCond
308a6 20 26 26 20 21 78 43 6f 6e 64 28 7a 29 20 29 20   && !xCond(z) ) 
308a7 72 65 74 75 72 6e 20 31 3b 0a 20 20 77 68 69 6c  return 1;.  whil
308a8 65 28 20 2a 7a 54 6f 20 29 7b 0a 20 20 20 20 2a  e( *zTo ){.    *
308a9 28 2d 2d 7a 29 20 3d 20 2a 28 7a 54 6f 2b 2b 29  (--z) = *(zTo++)
308aa 3b 0a 20 20 7d 0a 20 20 2a 70 7a 20 3d 20 7a 3b  ;.  }.  *pz = z;
308ab 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
308ac 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68  /*.** This is th
308ad 65 20 66 61 6c 6c 62 61 63 6b 20 73 74 65 6d 6d  e fallback stemm
308ae 65 72 20 75 73 65 64 20 77 68 65 6e 20 74 68 65  er used when the
308af 20 70 6f 72 74 65 72 20 73 74 65 6d 6d 65 72 20   porter stemmer 
308b0 69 73 0a 2a 2a 20 69 6e 61 70 70 72 6f 70 72 69  is.** inappropri
308b1 61 74 65 2e 20 20 54 68 65 20 69 6e 70 75 74 20  ate.  The input 
308b2 77 6f 72 64 20 69 73 20 63 6f 70 69 65 64 20 69  word is copied i
308b3 6e 74 6f 20 74 68 65 20 6f 75 74 70 75 74 20 77  nto the output w
308b4 69 74 68 0a 2a 2a 20 55 53 2d 41 53 43 49 49 20  ith.** US-ASCII 
308b5 63 61 73 65 20 66 6f 6c 64 69 6e 67 2e 20 20 49  case folding.  I
308b6 66 20 74 68 65 20 69 6e 70 75 74 20 77 6f 72 64  f the input word
308b7 20 69 73 20 74 6f 6f 20 6c 6f 6e 67 20 28 6d 6f   is too long (mo
308b8 72 65 0a 2a 2a 20 74 68 61 6e 20 32 30 20 62 79  re.** than 20 by
308b9 74 65 73 20 69 66 20 69 74 20 63 6f 6e 74 61 69  tes if it contai
308ba 6e 73 20 6e 6f 20 64 69 67 69 74 73 20 6f 72 20  ns no digits or 
308bb 6d 6f 72 65 20 74 68 61 6e 20 36 20 62 79 74 65  more than 6 byte
308bc 73 20 69 66 0a 2a 2a 20 69 74 20 63 6f 6e 74 61  s if.** it conta
308bd 69 6e 73 20 64 69 67 69 74 73 29 20 74 68 65 6e  ins digits) then
308be 20 77 6f 72 64 20 69 73 20 74 72 75 6e 63 61 74   word is truncat
308bf 65 64 20 74 6f 20 32 30 20 6f 72 20 36 20 62 79  ed to 20 or 6 by
308c0 74 65 73 0a 2a 2a 20 62 79 20 74 61 6b 69 6e 67  tes.** by taking
308c1 20 31 30 20 6f 72 20 33 20 62 79 74 65 73 20 66   10 or 3 bytes f
308c2 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e  rom the beginnin
308c3 67 20 61 6e 64 20 65 6e 64 2e 0a 2a 2f 0a 73 74  g and end..*/.st
308c4 61 74 69 63 20 76 6f 69 64 20 63 6f 70 79 5f 73  atic void copy_s
308c5 74 65 6d 6d 65 72 28 63 6f 6e 73 74 20 63 68 61  temmer(const cha
308c6 72 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 49 6e 2c  r *zIn, int nIn,
308c7 20 63 68 61 72 20 2a 7a 4f 75 74 2c 20 69 6e 74   char *zOut, int
308c8 20 2a 70 6e 4f 75 74 29 7b 0a 20 20 69 6e 74 20   *pnOut){.  int 
308c9 69 2c 20 6d 78 2c 20 6a 3b 0a 20 20 69 6e 74 20  i, mx, j;.  int 
308ca 68 61 73 44 69 67 69 74 20 3d 20 30 3b 0a 20 20  hasDigit = 0;.  
308cb 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 49 6e 3b 20  for(i=0; i<nIn; 
308cc 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 63 20  i++){.    int c 
308cd 3d 20 7a 49 6e 5b 69 5d 3b 0a 20 20 20 20 69 66  = zIn[i];.    if
308ce 28 20 63 3e 3d 27 41 27 20 26 26 20 63 3c 3d 27  ( c>='A' && c<='
308cf 5a 27 20 29 7b 0a 20 20 20 20 20 20 7a 4f 75 74  Z' ){.      zOut
308d0 5b 69 5d 20 3d 20 63 20 2d 20 27 41 27 20 2b 20  [i] = c - 'A' + 
308d1 27 61 27 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  'a';.    }else{.
308d2 20 20 20 20 20 20 69 66 28 20 63 3e 3d 27 30 27        if( c>='0'
308d3 20 26 26 20 63 3c 3d 27 39 27 20 29 20 68 61 73   && c<='9' ) has
308d4 44 69 67 69 74 20 3d 20 31 3b 0a 20 20 20 20 20  Digit = 1;.     
308d5 20 7a 4f 75 74 5b 69 5d 20 3d 20 63 3b 0a 20 20   zOut[i] = c;.  
308d6 20 20 7d 0a 20 20 7d 0a 20 20 6d 78 20 3d 20 68    }.  }.  mx = h
308d7 61 73 44 69 67 69 74 20 3f 20 33 20 3a 20 31 30  asDigit ? 3 : 10
308d8 3b 0a 20 20 69 66 28 20 6e 49 6e 3e 6d 78 2a 32  ;.  if( nIn>mx*2
308d9 20 29 7b 0a 20 20 20 20 66 6f 72 28 6a 3d 6d 78   ){.    for(j=mx
308da 2c 20 69 3d 6e 49 6e 2d 6d 78 3b 20 69 3c 6e 49  , i=nIn-mx; i<nI
308db 6e 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20  n; i++, j++){.  
308dc 20 20 20 20 7a 4f 75 74 5b 6a 5d 20 3d 20 7a 4f      zOut[j] = zO
308dd 75 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20  ut[i];.    }.   
308de 20 69 20 3d 20 6a 3b 0a 20 20 7d 0a 20 20 7a 4f   i = j;.  }.  zO
308df 75 74 5b 69 5d 20 3d 20 30 3b 0a 20 20 2a 70 6e  ut[i] = 0;.  *pn
308e0 4f 75 74 20 3d 20 69 3b 0a 7d 0a 0a 0a 2f 2a 0a  Out = i;.}.../*.
308e1 2a 2a 20 53 74 65 6d 20 74 68 65 20 69 6e 70 75  ** Stem the inpu
308e2 74 20 77 6f 72 64 20 7a 49 6e 5b 30 2e 2e 6e 49  t word zIn[0..nI
308e3 6e 2d 31 5d 2e 20 20 53 74 6f 72 65 20 74 68 65  n-1].  Store the
308e4 20 6f 75 74 70 75 74 20 69 6e 20 7a 4f 75 74 2e   output in zOut.
308e5 0a 2a 2a 20 7a 4f 75 74 20 69 73 20 61 74 20 6c  .** zOut is at l
308e6 65 61 73 74 20 62 69 67 20 65 6e 6f 75 67 68 20  east big enough 
308e7 74 6f 20 68 6f 6c 64 20 6e 49 6e 20 62 79 74 65  to hold nIn byte
308e8 73 2e 20 20 57 72 69 74 65 20 74 68 65 20 61 63  s.  Write the ac
308e9 74 75 61 6c 0a 2a 2a 20 73 69 7a 65 20 6f 66 20  tual.** size of 
308ea 74 68 65 20 6f 75 74 70 75 74 20 77 6f 72 64 20  the output word 
308eb 28 65 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68  (exclusive of th
308ec 65 20 27 5c 30 27 20 74 65 72 6d 69 6e 61 74 6f  e '\0' terminato
308ed 72 29 20 69 6e 74 6f 20 2a 70 6e 4f 75 74 2e 0a  r) into *pnOut..
308ee 2a 2a 0a 2a 2a 20 41 6e 79 20 75 70 70 65 72 2d  **.** Any upper-
308ef 63 61 73 65 20 63 68 61 72 61 63 74 65 72 73 20  case characters 
308f0 69 6e 20 74 68 65 20 55 53 2d 41 53 43 49 49 20  in the US-ASCII 
308f1 63 68 61 72 61 63 74 65 72 20 73 65 74 20 28 5b  character set ([
308f2 41 2d 5a 5d 29 0a 2a 2a 20 61 72 65 20 63 6f 6e  A-Z]).** are con
308f3 76 65 72 74 65 64 20 74 6f 20 6c 6f 77 65 72 20  verted to lower 
308f4 63 61 73 65 2e 20 20 55 70 70 65 72 2d 63 61 73  case.  Upper-cas
308f5 65 20 55 54 46 20 63 68 61 72 61 63 74 65 72 73  e UTF characters
308f6 20 61 72 65 0a 2a 2a 20 75 6e 63 68 61 6e 67 65   are.** unchange
308f7 64 2e 0a 2a 2a 0a 2a 2a 20 57 6f 72 64 73 20 74  d..**.** Words t
308f8 68 61 74 20 61 72 65 20 6c 6f 6e 67 65 72 20 74  hat are longer t
308f9 68 61 6e 20 61 62 6f 75 74 20 32 30 20 62 79 74  han about 20 byt
308fa 65 73 20 61 72 65 20 73 74 65 6d 6d 65 64 20 62  es are stemmed b
308fb 79 20 72 65 74 61 69 6e 69 6e 67 0a 2a 2a 20 61  y retaining.** a
308fc 20 66 65 77 20 62 79 74 65 73 20 66 72 6f 6d 20   few bytes from 
308fd 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e  the beginning an
308fe 64 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  d the end of the
308ff 20 77 6f 72 64 2e 20 20 49 66 20 74 68 65 0a 2a   word.  If the.*
30900 2a 20 77 6f 72 64 20 63 6f 6e 74 61 69 6e 73 20  * word contains 
30901 64 69 67 69 74 73 2c 20 33 20 62 79 74 65 73 20  digits, 3 bytes 
30902 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74  are taken from t
30903 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64  he beginning and
30904 0a 2a 2a 20 33 20 62 79 74 65 73 20 66 72 6f 6d  .** 3 bytes from
30905 20 74 68 65 20 65 6e 64 2e 20 20 46 6f 72 20 6c   the end.  For l
30906 6f 6e 67 20 77 6f 72 64 73 20 77 69 74 68 6f 75  ong words withou
30907 74 20 64 69 67 69 74 73 2c 20 31 30 20 62 79 74  t digits, 10 byt
30908 65 73 0a 2a 2a 20 61 72 65 20 74 61 6b 65 6e 20  es.** are taken 
30909 66 72 6f 6d 20 65 61 63 68 20 65 6e 64 2e 20 20  from each end.  
3090a 55 53 2d 41 53 43 49 49 20 63 61 73 65 20 66 6f  US-ASCII case fo
3090b 6c 64 69 6e 67 20 73 74 69 6c 6c 20 61 70 70 6c  lding still appl
3090c 69 65 73 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74  ies..** .** If t
3090d 68 65 20 69 6e 70 75 74 20 77 6f 72 64 20 63 6f  he input word co
3090e 6e 74 61 69 6e 73 20 6e 6f 74 20 64 69 67 69 74  ntains not digit
3090f 73 20 62 75 74 20 64 6f 65 73 20 63 68 61 72 61  s but does chara
30910 63 74 65 72 73 20 6e 6f 74 20 0a 2a 2a 20 69 6e  cters not .** in
30911 20 5b 61 2d 7a 41 2d 5a 5d 20 74 68 65 6e 20 6e   [a-zA-Z] then n
30912 6f 20 73 74 65 6d 6d 69 6e 67 20 69 73 20 61 74  o stemming is at
30913 74 65 6d 70 74 65 64 20 61 6e 64 20 74 68 69 73  tempted and this
30914 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 20 0a 2a   routine just .*
30915 2a 20 63 6f 70 69 65 73 20 74 68 65 20 69 6e 70  * copies the inp
30916 75 74 20 69 6e 74 6f 20 74 68 65 20 69 6e 70 75  ut into the inpu
30917 74 20 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 75  t into the outpu
30918 74 20 77 69 74 68 20 55 53 2d 41 53 43 49 49 0a  t with US-ASCII.
30919 2a 2a 20 63 61 73 65 20 66 6f 6c 64 69 6e 67 2e  ** case folding.
3091a 0a 2a 2a 0a 2a 2a 20 53 74 65 6d 6d 69 6e 67 20  .**.** Stemming 
3091b 6e 65 76 65 72 20 69 6e 63 72 65 61 73 65 73 20  never increases 
3091c 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68  the length of th
3091d 65 20 77 6f 72 64 2e 20 20 53 6f 20 74 68 65 72  e word.  So ther
3091e 65 20 69 73 0a 2a 2a 20 6e 6f 20 63 68 61 6e 63  e is.** no chanc
3091f 65 20 6f 66 20 6f 76 65 72 66 6c 6f 77 69 6e 67  e of overflowing
30920 20 74 68 65 20 7a 4f 75 74 20 62 75 66 66 65 72   the zOut buffer
30921 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
30922 20 70 6f 72 74 65 72 5f 73 74 65 6d 6d 65 72 28   porter_stemmer(
30923 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c  const char *zIn,
30924 20 69 6e 74 20 6e 49 6e 2c 20 63 68 61 72 20 2a   int nIn, char *
30925 7a 4f 75 74 2c 20 69 6e 74 20 2a 70 6e 4f 75 74  zOut, int *pnOut
30926 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 63  ){.  int i, j, c
30927 3b 0a 20 20 63 68 61 72 20 7a 52 65 76 65 72 73  ;.  char zRevers
30928 65 5b 32 38 5d 3b 0a 20 20 63 68 61 72 20 2a 7a  e[28];.  char *z
30929 2c 20 2a 7a 32 3b 0a 20 20 69 66 28 20 6e 49 6e  , *z2;.  if( nIn
3092a 3c 33 20 7c 7c 20 6e 49 6e 3e 3d 73 69 7a 65 6f  <3 || nIn>=sizeo
3092b 66 28 7a 52 65 76 65 72 73 65 29 2d 37 20 29 7b  f(zReverse)-7 ){
3092c 0a 20 20 20 20 2f 2a 20 54 68 65 20 77 6f 72 64  .    /* The word
3092d 20 69 73 20 74 6f 6f 20 62 69 67 20 6f 72 20 74   is too big or t
3092e 6f 6f 20 73 6d 61 6c 6c 20 66 6f 72 20 74 68 65  oo small for the
3092f 20 70 6f 72 74 65 72 20 73 74 65 6d 6d 65 72 2e   porter stemmer.
30930 0a 20 20 20 20 2a 2a 20 46 61 6c 6c 62 61 63 6b  .    ** Fallback
30931 20 74 6f 20 74 68 65 20 63 6f 70 79 20 73 74 65   to the copy ste
30932 6d 6d 65 72 20 2a 2f 0a 20 20 20 20 63 6f 70 79  mmer */.    copy
30933 5f 73 74 65 6d 6d 65 72 28 7a 49 6e 2c 20 6e 49  _stemmer(zIn, nI
30934 6e 2c 20 7a 4f 75 74 2c 20 70 6e 4f 75 74 29 3b  n, zOut, pnOut);
30935 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
30936 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 73 69  .  for(i=0, j=si
30937 7a 65 6f 66 28 7a 52 65 76 65 72 73 65 29 2d 36  zeof(zReverse)-6
30938 3b 20 69 3c 6e 49 6e 3b 20 69 2b 2b 2c 20 6a 2d  ; i<nIn; i++, j-
30939 2d 29 7b 0a 20 20 20 20 63 20 3d 20 7a 49 6e 5b  -){.    c = zIn[
3093a 69 5d 3b 0a 20 20 20 20 69 66 28 20 63 3e 3d 27  i];.    if( c>='
3093b 41 27 20 26 26 20 63 3c 3d 27 5a 27 20 29 7b 0a  A' && c<='Z' ){.
3093c 20 20 20 20 20 20 7a 52 65 76 65 72 73 65 5b 6a        zReverse[j
3093d 5d 20 3d 20 63 20 2b 20 27 61 27 20 2d 20 27 41  ] = c + 'a' - 'A
3093e 27 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  ';.    }else if(
3093f 20 63 3e 3d 27 61 27 20 26 26 20 63 3c 3d 27 7a   c>='a' && c<='z
30940 27 20 29 7b 0a 20 20 20 20 20 20 7a 52 65 76 65  ' ){.      zReve
30941 72 73 65 5b 6a 5d 20 3d 20 63 3b 0a 20 20 20 20  rse[j] = c;.    
30942 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20  }else{.      /* 
30943 54 68 65 20 75 73 65 20 6f 66 20 61 20 63 68 61  The use of a cha
30944 72 61 63 74 65 72 20 6e 6f 74 20 69 6e 20 5b 61  racter not in [a
30945 2d 7a 41 2d 5a 5d 20 6d 65 61 6e 73 20 74 68 61  -zA-Z] means tha
30946 74 20 77 65 20 66 61 6c 6c 62 61 63 6b 0a 20 20  t we fallback.  
30947 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 63 6f      ** to the co
30948 70 79 20 73 74 65 6d 6d 65 72 20 2a 2f 0a 20 20  py stemmer */.  
30949 20 20 20 20 63 6f 70 79 5f 73 74 65 6d 6d 65 72      copy_stemmer
3094a 28 7a 49 6e 2c 20 6e 49 6e 2c 20 7a 4f 75 74 2c  (zIn, nIn, zOut,
3094b 20 70 6e 4f 75 74 29 3b 0a 20 20 20 20 20 20 72   pnOut);.      r
3094c 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d  eturn;.    }.  }
3094d 0a 20 20 6d 65 6d 73 65 74 28 26 7a 52 65 76 65  .  memset(&zReve
3094e 72 73 65 5b 73 69 7a 65 6f 66 28 7a 52 65 76 65  rse[sizeof(zReve
3094f 72 73 65 29 2d 35 5d 2c 20 30 2c 20 35 29 3b 0a  rse)-5], 0, 5);.
30950 20 20 7a 20 3d 20 26 7a 52 65 76 65 72 73 65 5b    z = &zReverse[
30951 6a 2b 31 5d 3b 0a 0a 0a 20 20 2f 2a 20 53 74 65  j+1];...  /* Ste
30952 70 20 31 61 20 2a 2f 0a 20 20 69 66 28 20 7a 5b  p 1a */.  if( z[
30953 30 5d 3d 3d 27 73 27 20 29 7b 0a 20 20 20 20 69  0]=='s' ){.    i
30954 66 28 0a 20 20 20 20 20 21 73 74 65 6d 28 26 7a  f(.     !stem(&z
30955 2c 20 22 73 65 73 73 22 2c 20 22 73 73 22 2c 20  , "sess", "ss", 
30956 30 29 20 26 26 0a 20 20 20 20 20 21 73 74 65 6d  0) &&.     !stem
30957 28 26 7a 2c 20 22 73 65 69 22 2c 20 22 69 22 2c  (&z, "sei", "i",
30958 20 30 29 20 20 26 26 0a 20 20 20 20 20 21 73 74   0)  &&.     !st
30959 65 6d 28 26 7a 2c 20 22 73 73 22 2c 20 22 73 73  em(&z, "ss", "ss
3095a 22 2c 20 30 29 0a 20 20 20 20 29 7b 0a 20 20 20  ", 0).    ){.   
3095b 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20     z++;.    }.  
3095c 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 31 62 20  }..  /* Step 1b 
3095d 2a 2f 20 20 0a 20 20 7a 32 20 3d 20 7a 3b 0a 20  */  .  z2 = z;. 
3095e 20 69 66 28 20 73 74 65 6d 28 26 7a 2c 20 22 64   if( stem(&z, "d
3095f 65 65 22 2c 20 22 65 65 22 2c 20 6d 5f 67 74 5f  ee", "ee", m_gt_
30960 30 29 20 29 7b 0a 20 20 20 20 2f 2a 20 44 6f 20  0) ){.    /* Do 
30961 6e 6f 74 68 69 6e 67 2e 20 20 54 68 65 20 77 6f  nothing.  The wo
30962 72 6b 20 77 61 73 20 61 6c 6c 20 69 6e 20 74 68  rk was all in th
30963 65 20 74 65 73 74 20 2a 2f 0a 20 20 7d 65 6c 73  e test */.  }els
30964 65 20 69 66 28 20 0a 20 20 20 20 20 28 73 74 65  e if( .     (ste
30965 6d 28 26 7a 2c 20 22 67 6e 69 22 2c 20 22 22 2c  m(&z, "gni", "",
30966 20 68 61 73 56 6f 77 65 6c 29 20 7c 7c 20 73 74   hasVowel) || st
30967 65 6d 28 26 7a 2c 20 22 64 65 22 2c 20 22 22 2c  em(&z, "de", "",
30968 20 68 61 73 56 6f 77 65 6c 29 29 0a 20 20 20 20   hasVowel)).    
30969 20 20 26 26 20 7a 21 3d 7a 32 0a 20 20 29 7b 0a    && z!=z2.  ){.
3096a 20 20 20 20 20 69 66 28 20 73 74 65 6d 28 26 7a       if( stem(&z
3096b 2c 20 22 74 61 22 2c 20 22 61 74 65 22 2c 20 30  , "ta", "ate", 0
3096c 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 73 74  ) ||.         st
3096d 65 6d 28 26 7a 2c 20 22 6c 62 22 2c 20 22 62 6c  em(&z, "lb", "bl
3096e 65 22 2c 20 30 29 20 7c 7c 0a 20 20 20 20 20 20  e", 0) ||.      
3096f 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 7a 69 22     stem(&z, "zi"
30970 2c 20 22 69 7a 65 22 2c 20 30 29 20 29 7b 0a 20  , "ize", 0) ){. 
30971 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68        /* Do noth
30972 69 6e 67 2e 20 20 54 68 65 20 77 6f 72 6b 20 77  ing.  The work w
30973 61 73 20 61 6c 6c 20 69 6e 20 74 68 65 20 74 65  as all in the te
30974 73 74 20 2a 2f 0a 20 20 20 20 20 7d 65 6c 73 65  st */.     }else
30975 20 69 66 28 20 64 6f 75 62 6c 65 43 6f 6e 73 6f   if( doubleConso
30976 6e 61 6e 74 28 7a 29 20 26 26 20 28 2a 7a 21 3d  nant(z) && (*z!=
30977 27 6c 27 20 26 26 20 2a 7a 21 3d 27 73 27 20 26  'l' && *z!='s' &
30978 26 20 2a 7a 21 3d 27 7a 27 29 20 29 7b 0a 20 20  & *z!='z') ){.  
30979 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 20 7d       z++;.     }
3097a 65 6c 73 65 20 69 66 28 20 6d 5f 65 71 5f 31 28  else if( m_eq_1(
3097b 7a 29 20 26 26 20 73 74 61 72 5f 6f 68 28 7a 29  z) && star_oh(z)
3097c 20 29 7b 0a 20 20 20 20 20 20 20 2a 28 2d 2d 7a   ){.       *(--z
3097d 29 20 3d 20 27 65 27 3b 0a 20 20 20 20 20 7d 0a  ) = 'e';.     }.
3097e 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 31    }..  /* Step 1
3097f 63 20 2a 2f 0a 20 20 69 66 28 20 7a 5b 30 5d 3d  c */.  if( z[0]=
30980 3d 27 79 27 20 26 26 20 68 61 73 56 6f 77 65 6c  ='y' && hasVowel
30981 28 7a 2b 31 29 20 29 7b 0a 20 20 20 20 7a 5b 30  (z+1) ){.    z[0
30982 5d 20 3d 20 27 69 27 3b 0a 20 20 7d 0a 0a 20 20  ] = 'i';.  }..  
30983 2f 2a 20 53 74 65 70 20 32 20 2a 2f 0a 20 20 73  /* Step 2 */.  s
30984 77 69 74 63 68 28 20 7a 5b 31 5d 20 29 7b 0a 20  witch( z[1] ){. 
30985 20 20 63 61 73 65 20 27 61 27 3a 0a 20 20 20 20    case 'a':.    
30986 20 73 74 65 6d 28 26 7a 2c 20 22 6c 61 6e 6f 69   stem(&z, "lanoi
30987 74 61 22 2c 20 22 61 74 65 22 2c 20 6d 5f 67 74  ta", "ate", m_gt
30988 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  _0) ||.     stem
30989 28 26 7a 2c 20 22 6c 61 6e 6f 69 74 22 2c 20 22  (&z, "lanoit", "
3098a 74 69 6f 6e 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a  tion", m_gt_0);.
3098b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
3098c 61 73 65 20 27 63 27 3a 0a 20 20 20 20 20 73 74  ase 'c':.     st
3098d 65 6d 28 26 7a 2c 20 22 69 63 6e 65 22 2c 20 22  em(&z, "icne", "
3098e 65 6e 63 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c  ence", m_gt_0) |
3098f 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  |.     stem(&z, 
30990 22 69 63 6e 61 22 2c 20 22 61 6e 63 65 22 2c 20  "icna", "ance", 
30991 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72  m_gt_0);.     br
30992 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 65 27  eak;.   case 'e'
30993 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  :.     stem(&z, 
30994 22 72 65 7a 69 22 2c 20 22 69 7a 65 22 2c 20 6d  "rezi", "ize", m
30995 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65  _gt_0);.     bre
30996 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 67 27 3a  ak;.   case 'g':
30997 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22  .     stem(&z, "
30998 69 67 6f 6c 22 2c 20 22 6c 6f 67 22 2c 20 6d 5f  igol", "log", m_
30999 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61  gt_0);.     brea
3099a 6b 3b 0a 20 20 20 63 61 73 65 20 27 6c 27 3a 0a  k;.   case 'l':.
3099b 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
3099c 6c 62 22 2c 20 22 62 6c 65 22 2c 20 6d 5f 67 74  lb", "ble", m_gt
3099d 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  _0) ||.     stem
3099e 28 26 7a 2c 20 22 69 6c 6c 61 22 2c 20 22 61 6c  (&z, "illa", "al
3099f 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20  ", m_gt_0) ||.  
309a0 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 6c 74     stem(&z, "ilt
309a1 6e 65 22 2c 20 22 65 6e 74 22 2c 20 6d 5f 67 74  ne", "ent", m_gt
309a2 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  _0) ||.     stem
309a3 28 26 7a 2c 20 22 69 6c 65 22 2c 20 22 65 22 2c  (&z, "ile", "e",
309a4 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20   m_gt_0) ||.    
309a5 20 73 74 65 6d 28 26 7a 2c 20 22 69 6c 73 75 6f   stem(&z, "ilsuo
309a6 22 2c 20 22 6f 75 73 22 2c 20 6d 5f 67 74 5f 30  ", "ous", m_gt_0
309a7 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  );.     break;. 
309a8 20 20 63 61 73 65 20 27 6f 27 3a 0a 20 20 20 20    case 'o':.    
309a9 20 73 74 65 6d 28 26 7a 2c 20 22 6e 6f 69 74 61   stem(&z, "noita
309aa 7a 69 22 2c 20 22 69 7a 65 22 2c 20 6d 5f 67 74  zi", "ize", m_gt
309ab 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  _0) ||.     stem
309ac 28 26 7a 2c 20 22 6e 6f 69 74 61 22 2c 20 22 61  (&z, "noita", "a
309ad 74 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  te", m_gt_0) ||.
309ae 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 72       stem(&z, "r
309af 6f 74 61 22 2c 20 22 61 74 65 22 2c 20 6d 5f 67  ota", "ate", m_g
309b0 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b  t_0);.     break
309b1 3b 0a 20 20 20 63 61 73 65 20 27 73 27 3a 0a 20  ;.   case 's':. 
309b2 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6d 73      stem(&z, "ms
309b3 69 6c 61 22 2c 20 22 61 6c 22 2c 20 6d 5f 67 74  ila", "al", m_gt
309b4 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  _0) ||.     stem
309b5 28 26 7a 2c 20 22 73 73 65 6e 65 76 69 22 2c 20  (&z, "ssenevi", 
309b6 22 69 76 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c  "ive", m_gt_0) |
309b7 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  |.     stem(&z, 
309b8 22 73 73 65 6e 6c 75 66 22 2c 20 22 66 75 6c 22  "ssenluf", "ful"
309b9 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20  , m_gt_0) ||.   
309ba 20 20 73 74 65 6d 28 26 7a 2c 20 22 73 73 65 6e    stem(&z, "ssen
309bb 73 75 6f 22 2c 20 22 6f 75 73 22 2c 20 6d 5f 67  suo", "ous", m_g
309bc 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b  t_0);.     break
309bd 3b 0a 20 20 20 63 61 73 65 20 27 74 27 3a 0a 20  ;.   case 't':. 
309be 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 74      stem(&z, "it
309bf 69 6c 61 22 2c 20 22 61 6c 22 2c 20 6d 5f 67 74  ila", "al", m_gt
309c0 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  _0) ||.     stem
309c1 28 26 7a 2c 20 22 69 74 69 76 69 22 2c 20 22 69  (&z, "itivi", "i
309c2 76 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  ve", m_gt_0) ||.
309c3 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
309c4 74 69 6c 69 62 22 2c 20 22 62 6c 65 22 2c 20 6d  tilib", "ble", m
309c5 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65  _gt_0);.     bre
309c6 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74  ak;.  }..  /* St
309c7 65 70 20 33 20 2a 2f 0a 20 20 73 77 69 74 63 68  ep 3 */.  switch
309c8 28 20 7a 5b 30 5d 20 29 7b 0a 20 20 20 63 61 73  ( z[0] ){.   cas
309c9 65 20 27 65 27 3a 0a 20 20 20 20 20 73 74 65 6d  e 'e':.     stem
309ca 28 26 7a 2c 20 22 65 74 61 63 69 22 2c 20 22 69  (&z, "etaci", "i
309cb 63 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20  c", m_gt_0) ||. 
309cc 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 65 76      stem(&z, "ev
309cd 69 74 61 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 30  ita", "", m_gt_0
309ce 29 20 20 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  )   ||.     stem
309cf 28 26 7a 2c 20 22 65 7a 69 6c 61 22 2c 20 22 61  (&z, "ezila", "a
309d0 6c 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20  l", m_gt_0);.   
309d1 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65    break;.   case
309d2 20 27 69 27 3a 0a 20 20 20 20 20 73 74 65 6d 28   'i':.     stem(
309d3 26 7a 2c 20 22 69 74 69 63 69 22 2c 20 22 69 63  &z, "itici", "ic
309d4 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20  ", m_gt_0);.    
309d5 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
309d6 27 6c 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  'l':.     stem(&
309d7 7a 2c 20 22 6c 61 63 69 22 2c 20 22 69 63 22 2c  z, "laci", "ic",
309d8 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20   m_gt_0) ||.    
309d9 20 73 74 65 6d 28 26 7a 2c 20 22 6c 75 66 22 2c   stem(&z, "luf",
309da 20 22 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20   "", m_gt_0);.  
309db 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73     break;.   cas
309dc 65 20 27 73 27 3a 0a 20 20 20 20 20 73 74 65 6d  e 's':.     stem
309dd 28 26 7a 2c 20 22 73 73 65 6e 22 2c 20 22 22 2c  (&z, "ssen", "",
309de 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62   m_gt_0);.     b
309df 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  reak;.  }..  /* 
309e0 53 74 65 70 20 34 20 2a 2f 0a 20 20 73 77 69 74  Step 4 */.  swit
309e1 63 68 28 20 7a 5b 31 5d 20 29 7b 0a 20 20 20 63  ch( z[1] ){.   c
309e2 61 73 65 20 27 61 27 3a 0a 20 20 20 20 20 69 66  ase 'a':.     if
309e3 28 20 7a 5b 30 5d 3d 3d 27 6c 27 20 26 26 20 6d  ( z[0]=='l' && m
309e4 5f 67 74 5f 31 28 7a 2b 32 29 20 29 7b 0a 20 20  _gt_1(z+2) ){.  
309e5 20 20 20 20 20 7a 20 2b 3d 20 32 3b 0a 20 20 20       z += 2;.   
309e6 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a    }.     break;.
309e7 20 20 20 63 61 73 65 20 27 63 27 3a 0a 20 20 20     case 'c':.   
309e8 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 65 27 20    if( z[0]=='e' 
309e9 26 26 20 7a 5b 32 5d 3d 3d 27 6e 27 20 26 26 20  && z[2]=='n' && 
309ea 28 7a 5b 33 5d 3d 3d 27 61 27 20 7c 7c 20 7a 5b  (z[3]=='a' || z[
309eb 33 5d 3d 3d 27 65 27 29 20 20 26 26 20 6d 5f 67  3]=='e')  && m_g
309ec 74 5f 31 28 7a 2b 34 29 20 20 29 7b 0a 20 20 20  t_1(z+4)  ){.   
309ed 20 20 20 20 7a 20 2b 3d 20 34 3b 0a 20 20 20 20      z += 4;.    
309ee 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20   }.     break;. 
309ef 20 20 63 61 73 65 20 27 65 27 3a 0a 20 20 20 20    case 'e':.    
309f0 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 72 27 20 26   if( z[0]=='r' &
309f1 26 20 6d 5f 67 74 5f 31 28 7a 2b 32 29 20 29 7b  & m_gt_1(z+2) ){
309f2 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 32 3b 0a  .       z += 2;.
309f3 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61       }.     brea
309f4 6b 3b 0a 20 20 20 63 61 73 65 20 27 69 27 3a 0a  k;.   case 'i':.
309f5 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27       if( z[0]=='
309f6 63 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 32  c' && m_gt_1(z+2
309f7 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d  ) ){.       z +=
309f8 20 32 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20   2;.     }.     
309f9 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27  break;.   case '
309fa 6c 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30  l':.     if( z[0
309fb 5d 3d 3d 27 65 27 20 26 26 20 7a 5b 32 5d 3d 3d  ]=='e' && z[2]==
309fc 27 62 27 20 26 26 20 28 7a 5b 33 5d 3d 3d 27 61  'b' && (z[3]=='a
309fd 27 20 7c 7c 20 7a 5b 33 5d 3d 3d 27 69 27 29 20  ' || z[3]=='i') 
309fe 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 34 29 20 29  && m_gt_1(z+4) )
309ff 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 34 3b  {.       z += 4;
30a00 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65  .     }.     bre
30a01 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 6e 27 3a  ak;.   case 'n':
30a02 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d  .     if( z[0]==
30a03 27 74 27 20 29 7b 0a 20 20 20 20 20 20 20 69 66  't' ){.       if
30a04 28 20 7a 5b 32 5d 3d 3d 27 61 27 20 29 7b 0a 20  ( z[2]=='a' ){. 
30a05 20 20 20 20 20 20 20 20 69 66 28 20 6d 5f 67 74          if( m_gt
30a06 5f 31 28 7a 2b 33 29 20 29 7b 0a 20 20 20 20 20  _1(z+3) ){.     
30a07 20 20 20 20 20 20 7a 20 2b 3d 20 33 3b 0a 20 20        z += 3;.  
30a08 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
30a09 7d 65 6c 73 65 20 69 66 28 20 7a 5b 32 5d 3d 3d  }else if( z[2]==
30a0a 27 65 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20  'e' ){.         
30a0b 73 74 65 6d 28 26 7a 2c 20 22 74 6e 65 6d 65 22  stem(&z, "tneme"
30a0c 2c 20 22 22 2c 20 6d 5f 67 74 5f 31 29 20 7c 7c  , "", m_gt_1) ||
30a0d 0a 20 20 20 20 20 20 20 20 20 73 74 65 6d 28 26  .         stem(&
30a0e 7a 2c 20 22 74 6e 65 6d 22 2c 20 22 22 2c 20 6d  z, "tnem", "", m
30a0f 5f 67 74 5f 31 29 20 7c 7c 0a 20 20 20 20 20 20  _gt_1) ||.      
30a10 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 74 6e 65     stem(&z, "tne
30a11 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31 29 3b 0a  ", "", m_gt_1);.
30a12 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 7d 0a         }.     }.
30a13 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
30a14 61 73 65 20 27 6f 27 3a 0a 20 20 20 20 20 69 66  ase 'o':.     if
30a15 28 20 7a 5b 30 5d 3d 3d 27 75 27 20 29 7b 0a 20  ( z[0]=='u' ){. 
30a16 20 20 20 20 20 20 69 66 28 20 6d 5f 67 74 5f 31        if( m_gt_1
30a17 28 7a 2b 32 29 20 29 7b 0a 20 20 20 20 20 20 20  (z+2) ){.       
30a18 20 20 7a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20    z += 2;.      
30a19 20 7d 0a 20 20 20 20 20 7d 65 6c 73 65 20 69 66   }.     }else if
30a1a 28 20 7a 5b 33 5d 3d 3d 27 73 27 20 7c 7c 20 7a  ( z[3]=='s' || z
30a1b 5b 33 5d 3d 3d 27 74 27 20 29 7b 0a 20 20 20 20  [3]=='t' ){.    
30a1c 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6e 6f 69     stem(&z, "noi
30a1d 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31 29 3b 0a  ", "", m_gt_1);.
30a1e 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61       }.     brea
30a1f 6b 3b 0a 20 20 20 63 61 73 65 20 27 73 27 3a 0a  k;.   case 's':.
30a20 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27       if( z[0]=='
30a21 6d 27 20 26 26 20 7a 5b 32 5d 3d 3d 27 69 27 20  m' && z[2]=='i' 
30a22 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 33 29 20 29  && m_gt_1(z+3) )
30a23 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 33 3b  {.       z += 3;
30a24 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65  .     }.     bre
30a25 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 74 27 3a  ak;.   case 't':
30a26 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22  .     stem(&z, "
30a27 65 74 61 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31  eta", "", m_gt_1
30a28 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
30a29 7a 2c 20 22 69 74 69 22 2c 20 22 22 2c 20 6d 5f  z, "iti", "", m_
30a2a 67 74 5f 31 29 3b 0a 20 20 20 20 20 62 72 65 61  gt_1);.     brea
30a2b 6b 3b 0a 20 20 20 63 61 73 65 20 27 75 27 3a 0a  k;.   case 'u':.
30a2c 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27       if( z[0]=='
30a2d 73 27 20 26 26 20 7a 5b 32 5d 3d 3d 27 6f 27 20  s' && z[2]=='o' 
30a2e 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 33 29 20 29  && m_gt_1(z+3) )
30a2f 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 33 3b  {.       z += 3;
30a30 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65  .     }.     bre
30a31 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 76 27 3a  ak;.   case 'v':
30a32 0a 20 20 20 63 61 73 65 20 27 7a 27 3a 0a 20 20  .   case 'z':.  
30a33 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 65 27     if( z[0]=='e'
30a34 20 26 26 20 7a 5b 32 5d 3d 3d 27 69 27 20 26 26   && z[2]=='i' &&
30a35 20 6d 5f 67 74 5f 31 28 7a 2b 33 29 20 29 7b 0a   m_gt_1(z+3) ){.
30a36 20 20 20 20 20 20 20 7a 20 2b 3d 20 33 3b 0a 20         z += 3;. 
30a37 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b      }.     break
30a38 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70  ;.  }..  /* Step
30a39 20 35 61 20 2a 2f 0a 20 20 69 66 28 20 7a 5b 30   5a */.  if( z[0
30a3a 5d 3d 3d 27 65 27 20 29 7b 0a 20 20 20 20 69 66  ]=='e' ){.    if
30a3b 28 20 6d 5f 67 74 5f 31 28 7a 2b 31 29 20 29 7b  ( m_gt_1(z+1) ){
30a3c 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20  .      z++;.    
30a3d 7d 65 6c 73 65 20 69 66 28 20 6d 5f 65 71 5f 31  }else if( m_eq_1
30a3e 28 7a 2b 31 29 20 26 26 20 21 73 74 61 72 5f 6f  (z+1) && !star_o
30a3f 68 28 7a 2b 31 29 20 29 7b 0a 20 20 20 20 20 20  h(z+1) ){.      
30a40 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  z++;.    }.  }..
30a41 20 20 2f 2a 20 53 74 65 70 20 35 62 20 2a 2f 0a    /* Step 5b */.
30a42 20 20 69 66 28 20 6d 5f 67 74 5f 31 28 7a 29 20    if( m_gt_1(z) 
30a43 26 26 20 7a 5b 30 5d 3d 3d 27 6c 27 20 26 26 20  && z[0]=='l' && 
30a44 7a 5b 31 5d 3d 3d 27 6c 27 20 29 7b 0a 20 20 20  z[1]=='l' ){.   
30a45 20 7a 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   z++;.  }..  /* 
30a46 7a 5b 5d 20 69 73 20 6e 6f 77 20 74 68 65 20 73  z[] is now the s
30a47 74 65 6d 6d 65 64 20 77 6f 72 64 20 69 6e 20 72  temmed word in r
30a48 65 76 65 72 73 65 20 6f 72 64 65 72 2e 20 20 46  everse order.  F
30a49 6c 69 70 20 69 74 20 62 61 63 6b 0a 20 20 2a 2a  lip it back.  **
30a4a 20 61 72 6f 75 6e 64 20 69 6e 74 6f 20 66 6f 72   around into for
30a4b 77 61 72 64 20 6f 72 64 65 72 20 61 6e 64 20 72  ward order and r
30a4c 65 74 75 72 6e 2e 0a 20 20 2a 2f 0a 20 20 2a 70  eturn..  */.  *p
30a4d 6e 4f 75 74 20 3d 20 69 20 3d 20 73 74 72 6c 65  nOut = i = strle
30a4e 6e 28 7a 29 3b 0a 20 20 7a 4f 75 74 5b 69 5d 20  n(z);.  zOut[i] 
30a4f 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a  = 0;.  while( *z
30a50 20 29 7b 0a 20 20 20 20 7a 4f 75 74 5b 2d 2d 69   ){.    zOut[--i
30a51 5d 20 3d 20 2a 28 7a 2b 2b 29 3b 0a 20 20 7d 0a  ] = *(z++);.  }.
30a52 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 72 61 63 74  }../*.** Charact
30a53 65 72 73 20 74 68 61 74 20 63 61 6e 20 62 65 20  ers that can be 
30a54 70 61 72 74 20 6f 66 20 61 20 74 6f 6b 65 6e 2e  part of a token.
30a55 20 20 57 65 20 61 73 73 75 6d 65 20 61 6e 79 20    We assume any 
30a56 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 77 68 6f  character.** who
30a57 73 65 20 76 61 6c 75 65 20 69 73 20 67 72 65 61  se value is grea
30a58 74 65 72 20 74 68 61 6e 20 30 78 38 30 20 28 61  ter than 0x80 (a
30a59 6e 79 20 55 54 46 20 63 68 61 72 61 63 74 65 72  ny UTF character
30a5a 29 20 63 61 6e 20 62 65 0a 2a 2a 20 70 61 72 74  ) can be.** part
30a5b 20 6f 66 20 61 20 74 6f 6b 65 6e 2e 20 20 49 6e   of a token.  In
30a5c 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 64 65   other words, de
30a5d 6c 69 6d 69 74 65 72 73 20 61 6c 6c 20 6d 75 73  limiters all mus
30a5e 74 20 68 61 76 65 0a 2a 2a 20 76 61 6c 75 65 73  t have.** values
30a5f 20 6f 66 20 30 78 37 66 20 6f 72 20 6c 6f 77 65   of 0x7f or lowe
30a60 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  r..*/.static con
30a61 73 74 20 63 68 61 72 20 70 6f 72 74 65 72 49 64  st char porterId
30a62 43 68 61 72 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30  Char[] = {./* x0
30a63 20 78 31 20 78 32 20 78 33 20 78 34 20 78 35 20   x1 x2 x3 x4 x5 
30a64 78 36 20 78 37 20 78 38 20 78 39 20 78 41 20 78  x6 x7 x8 x9 xA x
30a65 42 20 78 43 20 78 44 20 78 45 20 78 46 20 2a 2f  B xC xD xE xF */
30a66 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c  .    1, 1, 1, 1,
30a67 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
30a68 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 0, 0, 0, 0, 0
30a69 2c 20 30 2c 20 20 2f 2a 20 33 78 20 2a 2f 0a 20  , 0,  /* 3x */. 
30a6a 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31     0, 1, 1, 1, 1
30a6b 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
30a6c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
30a6d 31 2c 20 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20  1,  /* 4x */.   
30a6e 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
30a6f 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
30a70 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c  , 0, 0, 0, 0, 1,
30a71 20 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20 30    /* 5x */.    0
30a72 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
30a73 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
30a74 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20  1, 1, 1, 1, 1,  
30a75 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 20 31 2c 20  /* 6x */.    1, 
30a76 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
30a77 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
30a78 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a   0, 0, 0, 0,  /*
30a79 20 37 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e   7x */.};.#defin
30a7a 65 20 69 73 44 65 6c 69 6d 28 43 29 20 28 28 28  e isDelim(C) (((
30a7b 63 68 3d 43 29 26 30 78 38 30 29 3d 3d 30 20 26  ch=C)&0x80)==0 &
30a7c 26 20 28 63 68 3c 30 78 33 30 20 7c 7c 20 21 70  & (ch<0x30 || !p
30a7d 6f 72 74 65 72 49 64 43 68 61 72 5b 63 68 2d 30  orterIdChar[ch-0
30a7e 78 33 30 5d 29 29 0a 0a 2f 2a 0a 2a 2a 20 45 78  x30]))../*.** Ex
30a7f 74 72 61 63 74 20 74 68 65 20 6e 65 78 74 20 74  tract the next t
30a80 6f 6b 65 6e 20 66 72 6f 6d 20 61 20 74 6f 6b 65  oken from a toke
30a81 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72 2e  nization cursor.
30a82 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73    The cursor mus
30a83 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 6f  t.** have been o
30a84 70 65 6e 65 64 20 62 79 20 61 20 70 72 69 6f 72  pened by a prior
30a85 20 63 61 6c 6c 20 74 6f 20 70 6f 72 74 65 72 4f   call to porterO
30a86 70 65 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  pen()..*/.static
30a87 20 69 6e 74 20 70 6f 72 74 65 72 4e 65 78 74 28   int porterNext(
30a88 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
30a89 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75  izer_cursor *pCu
30a8a 72 73 6f 72 2c 20 20 2f 2a 20 43 75 72 73 6f 72  rsor,  /* Cursor
30a8b 20 72 65 74 75 72 6e 65 64 20 62 79 20 70 6f 72   returned by por
30a8c 74 65 72 4f 70 65 6e 20 2a 2f 0a 20 20 63 6f 6e  terOpen */.  con
30a8d 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 6f 6b 65  st char **pzToke
30a8e 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n,              
30a8f 20 2f 2a 20 4f 55 54 3a 20 2a 70 7a 54 6f 6b 65   /* OUT: *pzToke
30a90 6e 20 69 73 20 74 68 65 20 74 6f 6b 65 6e 20 74  n is the token t
30a91 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  ext */.  int *pn
30a92 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20  Bytes,          
30a93 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
30a94 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62  OUT: Number of b
30a95 79 74 65 73 20 69 6e 20 74 6f 6b 65 6e 20 2a 2f  ytes in token */
30a96 0a 20 20 69 6e 74 20 2a 70 69 53 74 61 72 74 4f  .  int *piStartO
30a97 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 20  ffset,          
30a98 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53         /* OUT: S
30a99 74 61 72 74 69 6e 67 20 6f 66 66 73 65 74 20 6f  tarting offset o
30a9a 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74  f token */.  int
30a9b 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 2c 20 20   *piEndOffset,  
30a9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30a9d 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 69 6e 67 20   /* OUT: Ending 
30a9e 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20  offset of token 
30a9f 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 50 6f 73 69  */.  int *piPosi
30aa0 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20  tion            
30aa1 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
30aa2 20 50 6f 73 69 74 69 6f 6e 20 69 6e 74 65 67 65   Position intege
30aa3 72 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b  r of token */.){
30aa4 0a 20 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69  .  porter_tokeni
30aa5 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20  zer_cursor *c = 
30aa6 28 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65  (porter_tokenize
30aa7 72 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72  r_cursor *) pCur
30aa8 73 6f 72 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  sor;.  const cha
30aa9 72 20 2a 7a 20 3d 20 63 2d 3e 7a 49 6e 70 75 74  r *z = c->zInput
30aaa 3b 0a 0a 20 20 77 68 69 6c 65 28 20 63 2d 3e 69  ;..  while( c->i
30aab 4f 66 66 73 65 74 3c 63 2d 3e 6e 49 6e 70 75 74  Offset<c->nInput
30aac 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 53 74 61   ){.    int iSta
30aad 72 74 4f 66 66 73 65 74 2c 20 63 68 3b 0a 0a 20  rtOffset, ch;.. 
30aae 20 20 20 2f 2a 20 53 63 61 6e 20 70 61 73 74 20     /* Scan past 
30aaf 64 65 6c 69 6d 69 74 65 72 20 63 68 61 72 61 63  delimiter charac
30ab0 74 65 72 73 20 2a 2f 0a 20 20 20 20 77 68 69 6c  ters */.    whil
30ab1 65 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d  e( c->iOffset<c-
30ab2 3e 6e 49 6e 70 75 74 20 26 26 20 69 73 44 65 6c  >nInput && isDel
30ab3 69 6d 28 7a 5b 63 2d 3e 69 4f 66 66 73 65 74 5d  im(z[c->iOffset]
30ab4 29 20 29 7b 0a 20 20 20 20 20 20 63 2d 3e 69 4f  ) ){.      c->iO
30ab5 66 66 73 65 74 2b 2b 3b 0a 20 20 20 20 7d 0a 0a  ffset++;.    }..
30ab6 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 6e 6f 6e      /* Count non
30ab7 2d 64 65 6c 69 6d 69 74 65 72 20 63 68 61 72 61  -delimiter chara
30ab8 63 74 65 72 73 2e 20 2a 2f 0a 20 20 20 20 69 53  cters. */.    iS
30ab9 74 61 72 74 4f 66 66 73 65 74 20 3d 20 63 2d 3e  tartOffset = c->
30aba 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 77 68 69  iOffset;.    whi
30abb 6c 65 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63  le( c->iOffset<c
30abc 2d 3e 6e 49 6e 70 75 74 20 26 26 20 21 69 73 44  ->nInput && !isD
30abd 65 6c 69 6d 28 7a 5b 63 2d 3e 69 4f 66 66 73 65  elim(z[c->iOffse
30abe 74 5d 29 20 29 7b 0a 20 20 20 20 20 20 63 2d 3e  t]) ){.      c->
30abf 69 4f 66 66 73 65 74 2b 2b 3b 0a 20 20 20 20 7d  iOffset++;.    }
30ac0 0a 0a 20 20 20 20 69 66 28 20 63 2d 3e 69 4f 66  ..    if( c->iOf
30ac1 66 73 65 74 3e 69 53 74 61 72 74 4f 66 66 73 65  fset>iStartOffse
30ac2 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e  t ){.      int n
30ac3 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74 2d 69 53   = c->iOffset-iS
30ac4 74 61 72 74 4f 66 66 73 65 74 3b 0a 20 20 20 20  tartOffset;.    
30ac5 20 20 69 66 28 20 6e 3e 63 2d 3e 6e 41 6c 6c 6f    if( n>c->nAllo
30ac6 63 61 74 65 64 20 29 7b 0a 20 20 20 20 20 20 20  cated ){.       
30ac7 20 63 2d 3e 6e 41 6c 6c 6f 63 61 74 65 64 20 3d   c->nAllocated =
30ac8 20 6e 2b 32 30 3b 0a 20 20 20 20 20 20 20 20 63   n+20;.        c
30ac9 2d 3e 7a 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74  ->zToken = sqlit
30aca 65 33 5f 72 65 61 6c 6c 6f 63 28 63 2d 3e 7a 54  e3_realloc(c->zT
30acb 6f 6b 65 6e 2c 20 63 2d 3e 6e 41 6c 6c 6f 63 61  oken, c->nAlloca
30acc 74 65 64 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ted);.        if
30acd 28 20 63 2d 3e 7a 54 6f 6b 65 6e 3d 3d 4e 55 4c  ( c->zToken==NUL
30ace 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  L ) return SQLIT
30acf 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d  E_NOMEM;.      }
30ad0 0a 20 20 20 20 20 20 70 6f 72 74 65 72 5f 73 74  .      porter_st
30ad1 65 6d 6d 65 72 28 26 7a 5b 69 53 74 61 72 74 4f  emmer(&z[iStartO
30ad2 66 66 73 65 74 5d 2c 20 6e 2c 20 63 2d 3e 7a 54  ffset], n, c->zT
30ad3 6f 6b 65 6e 2c 20 70 6e 42 79 74 65 73 29 3b 0a  oken, pnBytes);.
30ad4 20 20 20 20 20 20 2a 70 7a 54 6f 6b 65 6e 20 3d        *pzToken =
30ad5 20 63 2d 3e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20   c->zToken;.    
30ad6 20 20 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74    *piStartOffset
30ad7 20 3d 20 69 53 74 61 72 74 4f 66 66 73 65 74 3b   = iStartOffset;
30ad8 0a 20 20 20 20 20 20 2a 70 69 45 6e 64 4f 66 66  .      *piEndOff
30ad9 73 65 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74  set = c->iOffset
30ada 3b 0a 20 20 20 20 20 20 2a 70 69 50 6f 73 69 74  ;.      *piPosit
30adb 69 6f 6e 20 3d 20 63 2d 3e 69 54 6f 6b 65 6e 2b  ion = c->iToken+
30adc 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  +;.      return 
30add 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
30ade 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
30adf 4c 49 54 45 5f 44 4f 4e 45 3b 0a 7d 0a 0a 2f 2a  LITE_DONE;.}../*
30ae0 0a 2a 2a 20 54 68 65 20 73 65 74 20 6f 66 20 72  .** The set of r
30ae1 6f 75 74 69 6e 65 73 20 74 68 61 74 20 69 6d 70  outines that imp
30ae2 6c 65 6d 65 6e 74 20 74 68 65 20 70 6f 72 74 65  lement the porte
30ae3 72 2d 73 74 65 6d 6d 65 72 20 74 6f 6b 65 6e 69  r-stemmer tokeni
30ae4 7a 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f  zer.*/.static co
30ae5 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  nst sqlite3_toke
30ae6 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 70 6f 72  nizer_module por
30ae7 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75  terTokenizerModu
30ae8 6c 65 20 3d 20 7b 0a 20 20 30 2c 0a 20 20 70 6f  le = {.  0,.  po
30ae9 72 74 65 72 43 72 65 61 74 65 2c 0a 20 20 70 6f  rterCreate,.  po
30aea 72 74 65 72 44 65 73 74 72 6f 79 2c 0a 20 20 70  rterDestroy,.  p
30aeb 6f 72 74 65 72 4f 70 65 6e 2c 0a 20 20 70 6f 72  orterOpen,.  por
30aec 74 65 72 43 6c 6f 73 65 2c 0a 20 20 70 6f 72 74  terClose,.  port
30aed 65 72 4e 65 78 74 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a  erNext,.};../*.*
30aee 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77  * Allocate a new
30aef 20 70 6f 72 74 65 72 20 74 6f 6b 65 6e 69 7a 65   porter tokenize
30af0 72 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69  r.  Return a poi
30af1 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 0a  nter to the new.
30af2 2a 2a 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 20  ** tokenizer in 
30af3 2a 70 70 4d 6f 64 75 6c 65 0a 2a 2f 0a 53 51 4c  *ppModule.*/.SQL
30af4 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
30af5 20 73 71 6c 69 74 65 33 46 74 73 33 50 6f 72 74   sqlite3Fts3Port
30af6 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  erTokenizerModul
30af7 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  e(.  sqlite3_tok
30af8 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f  enizer_module co
30af9 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 0a 29 7b  nst**ppModule.){
30afa 0a 20 20 2a 70 70 4d 6f 64 75 6c 65 20 3d 20 26  .  *ppModule = &
30afb 70 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d  porterTokenizerM
30afc 6f 64 75 6c 65 3b 0a 7d 0a 0a 23 65 6e 64 69 66  odule;.}..#endif
30afd 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c   /* !defined(SQL
30afe 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66  ITE_CORE) || def
30aff 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
30b00 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a  LE_FTS3) */../**
30b01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
30b02 20 6f 66 20 66 74 73 33 5f 70 6f 72 74 65 72 2e   of fts3_porter.
30b03 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
30b04 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
30b06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
30b07 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 74 6f 6b  in file fts3_tok
30b08 65 6e 69 7a 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a  enizer.c *******
30b09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
30b0b 2a 2a 20 32 30 30 37 20 4a 75 6e 65 20 32 32 0a  ** 2007 June 22.
30b0c 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
30b0d 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
30b0e 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
30b0f 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
30b10 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
30b11 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
30b12 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
30b13 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
30b14 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
30b15 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
30b16 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
30b17 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
30b18 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
30b19 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
30b1a 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
30b1b 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
30b1c 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
30b1d 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
30b1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b1f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b22 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  ***.**.** This i
30b23 73 20 70 61 72 74 20 6f 66 20 61 6e 20 53 51 4c  s part of an SQL
30b24 69 74 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65  ite module imple
30b25 6d 65 6e 74 69 6e 67 20 66 75 6c 6c 2d 74 65 78  menting full-tex
30b26 74 20 73 65 61 72 63 68 2e 0a 2a 2a 20 54 68 69  t search..** Thi
30b27 73 20 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c  s particular fil
30b28 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  e implements the
30b29 20 67 65 6e 65 72 69 63 20 74 6f 6b 65 6e 69 7a   generic tokeniz
30b2a 65 72 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f  er interface..*/
30b2b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65  ../*.** The code
30b2c 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73   in this file is
30b2d 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69   only compiled i
30b2e 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54  f:.**.**     * T
30b2f 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69  he FTS3 module i
30b30 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20 61 73  s being built as
30b31 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a   an extension.**
30b32 20 20 20 20 20 20 20 28 69 6e 20 77 68 69 63 68         (in which
30b33 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 52   case SQLITE_COR
30b34 45 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64  E is not defined
30b35 29 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ), or.**.**     
30b36 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c  * The FTS3 modul
30b37 65 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74  e is being built
30b38 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 65 20 6f   into the core o
30b39 66 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 69 74  f.**       SQLit
30b3a 65 20 28 69 6e 20 77 68 69 63 68 20 63 61 73 65  e (in which case
30b3b 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46   SQLITE_ENABLE_F
30b3c 54 53 33 20 69 73 20 64 65 66 69 6e 65 64 29 2e  TS3 is defined).
30b3d 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .*/.#if !defined
30b3e 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c  (SQLITE_CORE) ||
30b3f 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
30b40 45 4e 41 42 4c 45 5f 46 54 53 33 29 0a 0a 23 69  ENABLE_FTS3)..#i
30b41 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 52  fndef SQLITE_COR
30b42 45 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e  E.  SQLITE_EXTEN
30b43 53 49 4f 4e 5f 49 4e 49 54 31 0a 23 65 6e 64 69  SION_INIT1.#endi
30b44 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  f.../*.** Implem
30b45 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
30b46 53 51 4c 20 73 63 61 6c 61 72 20 66 75 6e 63 74  SQL scalar funct
30b47 69 6f 6e 20 66 6f 72 20 61 63 63 65 73 73 69 6e  ion for accessin
30b48 67 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  g the underlying
30b49 20 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 2e   .** hash table.
30b4a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d   This function m
30b4b 61 79 20 62 65 20 63 61 6c 6c 65 64 20 61 73 20  ay be called as 
30b4c 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20  follows:.**.**  
30b4d 20 53 45 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f   SELECT <functio
30b4e 6e 2d 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d  n-name>(<key-nam
30b4f 65 3e 29 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54  e>);.**   SELECT
30b50 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e   <function-name>
30b51 28 3c 6b 65 79 2d 6e 61 6d 65 3e 2c 20 3c 70 6f  (<key-name>, <po
30b52 69 6e 74 65 72 3e 29 3b 0a 2a 2a 0a 2a 2a 20 77  inter>);.**.** w
30b53 68 65 72 65 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e  here <function-n
30b54 61 6d 65 3e 20 69 73 20 74 68 65 20 6e 61 6d 65  ame> is the name
30b55 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73   passed as the s
30b56 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a  econd argument.*
30b57 2a 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  * to the sqlite3
30b58 46 74 73 33 49 6e 69 74 48 61 73 68 54 61 62 6c  Fts3InitHashTabl
30b59 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 28 65 2e  e() function (e.
30b5a 67 2e 20 27 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  g. 'fts3_tokeniz
30b5b 65 72 27 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  er')..**.** If t
30b5c 68 65 20 3c 70 6f 69 6e 74 65 72 3e 20 61 72 67  he <pointer> arg
30b5d 75 6d 65 6e 74 20 69 73 20 73 70 65 63 69 66 69  ument is specifi
30b5e 65 64 2c 20 69 74 20 6d 75 73 74 20 62 65 20 61  ed, it must be a
30b5f 20 62 6c 6f 62 20 76 61 6c 75 65 0a 2a 2a 20 63   blob value.** c
30b60 6f 6e 74 61 69 6e 69 6e 67 20 61 20 70 6f 69 6e  ontaining a poin
30b61 74 65 72 20 74 6f 20 62 65 20 73 74 6f 72 65 64  ter to be stored
30b62 20 61 73 20 74 68 65 20 68 61 73 68 20 64 61 74   as the hash dat
30b63 61 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a  a corresponding.
30b64 2a 2a 20 74 6f 20 74 68 65 20 73 74 72 69 6e 67  ** to the string
30b65 20 3c 6b 65 79 2d 6e 61 6d 65 3e 2e 20 49 66 20   <key-name>. If 
30b66 3c 70 6f 69 6e 74 65 72 3e 20 69 73 20 6e 6f 74  <pointer> is not
30b67 20 73 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e   specified, then
30b68 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 3c  .** the string <
30b69 6b 65 79 2d 6e 61 6d 65 3e 20 6d 75 73 74 20 61  key-name> must a
30b6a 6c 72 65 61 64 79 20 65 78 69 73 74 20 69 6e 20  lready exist in 
30b6b 74 68 65 20 68 61 73 20 74 61 62 6c 65 2e 20 4f  the has table. O
30b6c 74 68 65 72 77 69 73 65 2c 0a 2a 2a 20 61 6e 20  therwise,.** an 
30b6d 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65  error is returne
30b6e 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72  d..**.** Whether
30b6f 20 6f 72 20 6e 6f 74 20 74 68 65 20 3c 70 6f 69   or not the <poi
30b70 6e 74 65 72 3e 20 61 72 67 75 6d 65 6e 74 20 69  nter> argument i
30b71 73 20 73 70 65 63 69 66 69 65 64 2c 20 74 68 65  s specified, the
30b72 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 0a   value returned.
30b73 2a 2a 20 69 73 20 61 20 62 6c 6f 62 20 63 6f 6e  ** is a blob con
30b74 74 61 69 6e 69 6e 67 20 74 68 65 20 70 6f 69 6e  taining the poin
30b75 74 65 72 20 73 74 6f 72 65 64 20 61 73 20 74 68  ter stored as th
30b76 65 20 68 61 73 68 20 64 61 74 61 20 63 6f 72 72  e hash data corr
30b77 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20  esponding.** to 
30b78 73 74 72 69 6e 67 20 3c 6b 65 79 2d 6e 61 6d 65  string <key-name
30b79 3e 20 28 61 66 74 65 72 20 74 68 65 20 68 61 73  > (after the has
30b7a 68 2d 74 61 62 6c 65 20 69 73 20 75 70 64 61 74  h-table is updat
30b7b 65 64 2c 20 69 66 20 61 70 70 6c 69 63 61 62 6c  ed, if applicabl
30b7c 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  e)..*/.static vo
30b7d 69 64 20 73 63 61 6c 61 72 46 75 6e 63 28 0a 20  id scalarFunc(. 
30b7e 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
30b7f 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
30b80 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
30b81 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
30b82 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70 48 61  .  fts3Hash *pHa
30b83 73 68 3b 0a 20 20 76 6f 69 64 20 2a 70 50 74 72  sh;.  void *pPtr
30b84 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 6e   = 0;.  const un
30b85 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 4e 61  signed char *zNa
30b86 6d 65 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b  me;.  int nName;
30b87 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  ..  assert( argc
30b88 3d 3d 31 20 7c 7c 20 61 72 67 63 3d 3d 32 20 29  ==1 || argc==2 )
30b89 3b 0a 0a 20 20 70 48 61 73 68 20 3d 20 28 66 74  ;..  pHash = (ft
30b8a 73 33 48 61 73 68 20 2a 29 73 71 6c 69 74 65 33  s3Hash *)sqlite3
30b8b 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65  _user_data(conte
30b8c 78 74 29 3b 0a 0a 20 20 7a 4e 61 6d 65 20 3d 20  xt);..  zName = 
30b8d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
30b8e 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e  xt(argv[0]);.  n
30b8f 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f 76  Name = sqlite3_v
30b90 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
30b91 30 5d 29 2b 31 3b 0a 0a 20 20 69 66 28 20 61 72  0])+1;..  if( ar
30b92 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20 76 6f 69  gc==2 ){.    voi
30b93 64 20 2a 70 4f 6c 64 3b 0a 20 20 20 20 69 6e 74  d *pOld;.    int
30b94 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c   n = sqlite3_val
30b95 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 31 5d  ue_bytes(argv[1]
30b96 29 3b 0a 20 20 20 20 69 66 28 20 6e 21 3d 73 69  );.    if( n!=si
30b97 7a 65 6f 66 28 70 50 74 72 29 20 29 7b 0a 20 20  zeof(pPtr) ){.  
30b98 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
30b99 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74  lt_error(context
30b9a 2c 20 22 61 72 67 75 6d 65 6e 74 20 74 79 70 65  , "argument type
30b9b 20 6d 69 73 6d 61 74 63 68 22 2c 20 2d 31 29 3b   mismatch", -1);
30b9c 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
30b9d 20 20 20 7d 0a 20 20 20 20 70 50 74 72 20 3d 20     }.    pPtr = 
30b9e 2a 28 76 6f 69 64 20 2a 2a 29 73 71 6c 69 74 65  *(void **)sqlite
30b9f 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67  3_value_blob(arg
30ba0 76 5b 31 5d 29 3b 0a 20 20 20 20 70 4f 6c 64 20  v[1]);.    pOld 
30ba1 3d 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73  = sqlite3Fts3Has
30ba2 68 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 28  hInsert(pHash, (
30ba3 76 6f 69 64 20 2a 29 7a 4e 61 6d 65 2c 20 6e 4e  void *)zName, nN
30ba4 61 6d 65 2c 20 70 50 74 72 29 3b 0a 20 20 20 20  ame, pPtr);.    
30ba5 69 66 28 20 70 4f 6c 64 3d 3d 70 50 74 72 20 29  if( pOld==pPtr )
30ba6 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
30ba7 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e  result_error(con
30ba8 74 65 78 74 2c 20 22 6f 75 74 20 6f 66 20 6d 65  text, "out of me
30ba9 6d 6f 72 79 22 2c 20 2d 31 29 3b 0a 20 20 20 20  mory", -1);.    
30baa 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
30bab 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 74    }else{.    pPt
30bac 72 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 48  r = sqlite3Fts3H
30bad 61 73 68 46 69 6e 64 28 70 48 61 73 68 2c 20 7a  ashFind(pHash, z
30bae 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20  Name, nName);.  
30baf 20 20 69 66 28 20 21 70 50 74 72 20 29 7b 0a 20    if( !pPtr ){. 
30bb0 20 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72 20       char *zErr 
30bb1 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
30bb2 66 28 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e  f("unknown token
30bb3 69 7a 65 72 3a 20 25 73 22 2c 20 7a 4e 61 6d 65  izer: %s", zName
30bb4 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
30bb5 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f  _result_error(co
30bb6 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29  ntext, zErr, -1)
30bb7 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
30bb8 66 72 65 65 28 7a 45 72 72 29 3b 0a 20 20 20 20  free(zErr);.    
30bb9 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
30bba 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 72    }..  sqlite3_r
30bbb 65 73 75 6c 74 5f 62 6c 6f 62 28 63 6f 6e 74 65  esult_blob(conte
30bbc 78 74 2c 20 28 76 6f 69 64 20 2a 29 26 70 50 74  xt, (void *)&pPt
30bbd 72 2c 20 73 69 7a 65 6f 66 28 70 50 74 72 29 2c  r, sizeof(pPtr),
30bbe 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
30bbf 54 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51  T);.}..#ifdef SQ
30bc0 4c 49 54 45 5f 54 45 53 54 0a 0a 0a 2f 2a 0a 2a  LITE_TEST.../*.*
30bc1 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
30bc2 20 6f 66 20 61 20 73 70 65 63 69 61 6c 20 53 51   of a special SQ
30bc3 4c 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  L scalar functio
30bc4 6e 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74 6f  n for testing to
30bc5 6b 65 6e 69 7a 65 72 73 20 0a 2a 2a 20 64 65 73  kenizers .** des
30bc6 69 67 6e 65 64 20 74 6f 20 62 65 20 75 73 65 64  igned to be used
30bc7 20 69 6e 20 63 6f 6e 63 65 72 74 20 77 69 74 68   in concert with
30bc8 20 74 68 65 20 54 63 6c 20 74 65 73 74 69 6e 67   the Tcl testing
30bc9 20 66 72 61 6d 65 77 6f 72 6b 2e 20 54 68 69 73   framework. This
30bca 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73  .** function mus
30bcb 74 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74 68  t be called with
30bcc 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 3a 0a   two arguments:.
30bcd 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c  **.**   SELECT <
30bce 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c  function-name>(<
30bcf 6b 65 79 2d 6e 61 6d 65 3e 2c 20 3c 69 6e 70 75  key-name>, <inpu
30bd0 74 2d 73 74 72 69 6e 67 3e 29 3b 0a 2a 2a 20 20  t-string>);.**  
30bd1 20 53 45 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f   SELECT <functio
30bd2 6e 2d 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d  n-name>(<key-nam
30bd3 65 3e 2c 20 3c 70 6f 69 6e 74 65 72 3e 29 3b 0a  e>, <pointer>);.
30bd4 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 3c 66 75 6e  **.** where <fun
30bd5 63 74 69 6f 6e 2d 6e 61 6d 65 3e 20 69 73 20 74  ction-name> is t
30bd6 68 65 20 6e 61 6d 65 20 70 61 73 73 65 64 20 61  he name passed a
30bd7 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  s the second arg
30bd8 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20  ument.** to the 
30bd9 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 48  sqlite3Fts3InitH
30bda 61 73 68 54 61 62 6c 65 28 29 20 66 75 6e 63 74  ashTable() funct
30bdb 69 6f 6e 20 28 65 2e 67 2e 20 27 66 74 73 33 5f  ion (e.g. 'fts3_
30bdc 74 6f 6b 65 6e 69 7a 65 72 27 29 0a 2a 2a 20 63  tokenizer').** c
30bdd 6f 6e 63 61 74 65 6e 61 74 65 64 20 77 69 74 68  oncatenated with
30bde 20 74 68 65 20 73 74 72 69 6e 67 20 27 5f 74 65   the string '_te
30bdf 73 74 27 20 28 65 2e 67 2e 20 27 66 74 73 33 5f  st' (e.g. 'fts3_
30be0 74 6f 6b 65 6e 69 7a 65 72 5f 74 65 73 74 27 29  tokenizer_test')
30be1 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75  ..**.** The retu
30be2 72 6e 20 76 61 6c 75 65 20 69 73 20 61 20 73 74  rn value is a st
30be3 72 69 6e 67 20 74 68 61 74 20 6d 61 79 20 62 65  ring that may be
30be4 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20   interpreted as 
30be5 61 20 54 63 6c 0a 2a 2a 20 6c 69 73 74 2e 20 46  a Tcl.** list. F
30be6 6f 72 20 65 61 63 68 20 74 6f 6b 65 6e 20 69 6e  or each token in
30be7 20 74 68 65 20 3c 69 6e 70 75 74 2d 73 74 72 69   the <input-stri
30be8 6e 67 3e 2c 20 74 68 72 65 65 20 65 6c 65 6d 65  ng>, three eleme
30be9 6e 74 73 20 61 72 65 0a 2a 2a 20 61 64 64 65 64  nts are.** added
30bea 20 74 6f 20 74 68 65 20 72 65 74 75 72 6e 65 64   to the returned
30beb 20 6c 69 73 74 2e 20 54 68 65 20 66 69 72 73 74   list. The first
30bec 20 69 73 20 74 68 65 20 74 6f 6b 65 6e 20 70 6f   is the token po
30bed 73 69 74 69 6f 6e 2c 20 74 68 65 20 0a 2a 2a 20  sition, the .** 
30bee 73 65 63 6f 6e 64 20 69 73 20 74 68 65 20 74 6f  second is the to
30bef 6b 65 6e 20 74 65 78 74 20 28 66 6f 6c 64 65 64  ken text (folded
30bf0 2c 20 73 74 65 6d 6d 65 64 2c 20 65 74 63 2e 29  , stemmed, etc.)
30bf1 20 61 6e 64 20 74 68 65 20 74 68 69 72 64 20 69   and the third i
30bf2 73 20 74 68 65 0a 2a 2a 20 73 75 62 73 74 72 69  s the.** substri
30bf3 6e 67 20 6f 66 20 3c 69 6e 70 75 74 2d 73 74 72  ng of <input-str
30bf4 69 6e 67 3e 20 61 73 73 6f 63 69 61 74 65 64 20  ing> associated 
30bf5 77 69 74 68 20 74 68 65 20 74 6f 6b 65 6e 2e 20  with the token. 
30bf6 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 0a 2a 2a  For example, .**
30bf7 20 75 73 69 6e 67 20 74 68 65 20 62 75 69 6c 74   using the built
30bf8 2d 69 6e 20 22 73 69 6d 70 6c 65 22 20 74 6f 6b  -in "simple" tok
30bf9 65 6e 69 7a 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20  enizer:.**.**   
30bfa 53 45 4c 45 43 54 20 66 74 73 5f 74 6f 6b 65 6e  SELECT fts_token
30bfb 69 7a 65 72 5f 74 65 73 74 28 27 73 69 6d 70 6c  izer_test('simpl
30bfc 65 27 2c 20 27 49 20 64 6f 6e 27 74 20 73 65 65  e', 'I don't see
30bfd 20 68 6f 77 27 29 3b 0a 2a 2a 0a 2a 2a 20 77 69   how');.**.** wi
30bfe 6c 6c 20 72 65 74 75 72 6e 20 74 68 65 20 73 74  ll return the st
30bff 72 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 7b  ring:.**.**   "{
30c00 30 20 69 20 49 20 31 20 64 6f 6e 74 20 64 6f 6e  0 i I 1 dont don
30c01 27 74 20 32 20 73 65 65 20 73 65 65 20 33 20 68  't 2 see see 3 h
30c02 6f 77 20 68 6f 77 7d 22 0a 2a 2a 20 20 20 0a 2a  ow how}".**   .*
30c03 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 65  /.static void te
30c04 73 74 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  stFunc(.  sqlite
30c05 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
30c06 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
30c07 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
30c08 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 66 74 73 33  **argv.){.  fts3
30c09 48 61 73 68 20 2a 70 48 61 73 68 3b 0a 20 20 73  Hash *pHash;.  s
30c0a 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
30c0b 5f 6d 6f 64 75 6c 65 20 2a 70 3b 0a 20 20 73 71  _module *p;.  sq
30c0c 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
30c0d 2a 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 30 3b  *pTokenizer = 0;
30c0e 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
30c0f 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 73  izer_cursor *pCs
30c10 72 20 3d 20 30 3b 0a 0a 20 20 63 6f 6e 73 74 20  r = 0;..  const 
30c11 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a  char *zErr = 0;.
30c12 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
30c13 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d  Name;.  int nNam
30c14 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  e;.  const char 
30c15 2a 7a 49 6e 70 75 74 3b 0a 20 20 69 6e 74 20 6e  *zInput;.  int n
30c16 49 6e 70 75 74 3b 0a 0a 20 20 63 6f 6e 73 74 20  Input;..  const 
30c17 63 68 61 72 20 2a 7a 41 72 67 20 3d 20 30 3b 0a  char *zArg = 0;.
30c18 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
30c19 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74 20 6e 54 6f  Token;.  int nTo
30c1a 6b 65 6e 3b 0a 20 20 69 6e 74 20 69 53 74 61 72  ken;.  int iStar
30c1b 74 3b 0a 20 20 69 6e 74 20 69 45 6e 64 3b 0a 20  t;.  int iEnd;. 
30c1c 20 69 6e 74 20 69 50 6f 73 3b 0a 0a 20 20 54 63   int iPos;..  Tc
30c1d 6c 5f 4f 62 6a 20 2a 70 52 65 74 3b 0a 0a 20 20  l_Obj *pRet;..  
30c1e 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 32 20  assert( argc==2 
30c1f 7c 7c 20 61 72 67 63 3d 3d 33 20 29 3b 0a 0a 20  || argc==3 );.. 
30c20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   nName = sqlite3
30c21 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
30c22 76 5b 30 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d  v[0]);.  zName =
30c23 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
30c24 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
30c25 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 49  t(argv[0]);.  nI
30c26 6e 70 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 76  nput = sqlite3_v
30c27 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
30c28 61 72 67 63 2d 31 5d 29 3b 0a 20 20 7a 49 6e 70  argc-1]);.  zInp
30c29 75 74 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  ut = (const char
30c2a 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
30c2b 5f 74 65 78 74 28 61 72 67 76 5b 61 72 67 63 2d  _text(argv[argc-
30c2c 31 5d 29 3b 0a 0a 20 20 69 66 28 20 61 72 67 63  1]);..  if( argc
30c2d 3d 3d 33 20 29 7b 0a 20 20 20 20 7a 41 72 67 20  ==3 ){.    zArg 
30c2e 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
30c2f 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
30c30 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 7d  xt(argv[1]);.  }
30c31 0a 0a 20 20 70 48 61 73 68 20 3d 20 28 66 74 73  ..  pHash = (fts
30c32 33 48 61 73 68 20 2a 29 73 71 6c 69 74 65 33 5f  3Hash *)sqlite3_
30c33 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78  user_data(contex
30c34 74 29 3b 0a 20 20 70 20 3d 20 28 73 71 6c 69 74  t);.  p = (sqlit
30c35 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
30c36 75 6c 65 20 2a 29 73 71 6c 69 74 65 33 46 74 73  ule *)sqlite3Fts
30c37 33 48 61 73 68 46 69 6e 64 28 70 48 61 73 68 2c  3HashFind(pHash,
30c38 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29   zName, nName+1)
30c39 3b 0a 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20  ;..  if( !p ){. 
30c3a 20 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20     char *zErr = 
30c3b 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
30c3c 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a  "unknown tokeniz
30c3d 65 72 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b  er: %s", zName);
30c3e 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
30c3f 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78  ult_error(contex
30c40 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20  t, zErr, -1);.  
30c41 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
30c42 45 72 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  Err);.    return
30c43 3b 0a 20 20 7d 0a 0a 20 20 70 52 65 74 20 3d 20  ;.  }..  pRet = 
30c44 54 63 6c 5f 4e 65 77 4f 62 6a 28 29 3b 0a 20 20  Tcl_NewObj();.  
30c45 54 63 6c 5f 49 6e 63 72 52 65 66 43 6f 75 6e 74  Tcl_IncrRefCount
30c46 28 70 52 65 74 29 3b 0a 0a 20 20 69 66 28 20 53  (pRet);..  if( S
30c47 51 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78 43 72  QLITE_OK!=p->xCr
30c48 65 61 74 65 28 7a 41 72 67 20 3f 20 31 20 3a 20  eate(zArg ? 1 : 
30c49 30 2c 20 26 7a 41 72 67 2c 20 26 70 54 6f 6b 65  0, &zArg, &pToke
30c4a 6e 69 7a 65 72 29 20 29 7b 0a 20 20 20 20 7a 45  nizer) ){.    zE
30c4b 72 72 20 3d 20 22 65 72 72 6f 72 20 69 6e 20 78  rr = "error in x
30c4c 43 72 65 61 74 65 28 29 22 3b 0a 20 20 20 20 67  Create()";.    g
30c4d 6f 74 6f 20 66 69 6e 69 73 68 3b 0a 20 20 7d 0a  oto finish;.  }.
30c4e 20 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d    pTokenizer->pM
30c4f 6f 64 75 6c 65 20 3d 20 70 3b 0a 20 20 69 66 28  odule = p;.  if(
30c50 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78   SQLITE_OK!=p->x
30c51 4f 70 65 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c  Open(pTokenizer,
30c52 20 7a 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2c   zInput, nInput,
30c53 20 26 70 43 73 72 29 20 29 7b 0a 20 20 20 20 7a   &pCsr) ){.    z
30c54 45 72 72 20 3d 20 22 65 72 72 6f 72 20 69 6e 20  Err = "error in 
30c55 78 4f 70 65 6e 28 29 22 3b 0a 20 20 20 20 67 6f  xOpen()";.    go
30c56 74 6f 20 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 20  to finish;.  }. 
30c57 20 70 43 73 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65   pCsr->pTokenize
30c58 72 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a  r = pTokenizer;.
30c59 0a 20 20 77 68 69 6c 65 28 20 53 51 4c 49 54 45  .  while( SQLITE
30c5a 5f 4f 4b 3d 3d 70 2d 3e 78 4e 65 78 74 28 70 43  _OK==p->xNext(pC
30c5b 73 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26 6e 54  sr, &zToken, &nT
30c5c 6f 6b 65 6e 2c 20 26 69 53 74 61 72 74 2c 20 26  oken, &iStart, &
30c5d 69 45 6e 64 2c 20 26 69 50 6f 73 29 20 29 7b 0a  iEnd, &iPos) ){.
30c5e 20 20 20 20 54 63 6c 5f 4c 69 73 74 4f 62 6a 41      Tcl_ListObjA
30c5f 70 70 65 6e 64 45 6c 65 6d 65 6e 74 28 30 2c 20  ppendElement(0, 
30c60 70 52 65 74 2c 20 54 63 6c 5f 4e 65 77 49 6e 74  pRet, Tcl_NewInt
30c61 4f 62 6a 28 69 50 6f 73 29 29 3b 0a 20 20 20 20  Obj(iPos));.    
30c62 54 63 6c 5f 4c 69 73 74 4f 62 6a 41 70 70 65 6e  Tcl_ListObjAppen
30c63 64 45 6c 65 6d 65 6e 74 28 30 2c 20 70 52 65 74  dElement(0, pRet
30c64 2c 20 54 63 6c 5f 4e 65 77 53 74 72 69 6e 67 4f  , Tcl_NewStringO
30c65 62 6a 28 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65  bj(zToken, nToke
30c66 6e 29 29 3b 0a 20 20 20 20 7a 54 6f 6b 65 6e 20  n));.    zToken 
30c67 3d 20 26 7a 49 6e 70 75 74 5b 69 53 74 61 72 74  = &zInput[iStart
30c68 5d 3b 0a 20 20 20 20 6e 54 6f 6b 65 6e 20 3d 20  ];.    nToken = 
30c69 69 45 6e 64 2d 69 53 74 61 72 74 3b 0a 20 20 20  iEnd-iStart;.   
30c6a 20 54 63 6c 5f 4c 69 73 74 4f 62 6a 41 70 70 65   Tcl_ListObjAppe
30c6b 6e 64 45 6c 65 6d 65 6e 74 28 30 2c 20 70 52 65  ndElement(0, pRe
30c6c 74 2c 20 54 63 6c 5f 4e 65 77 53 74 72 69 6e 67  t, Tcl_NewString
30c6d 4f 62 6a 28 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b  Obj(zToken, nTok
30c6e 65 6e 29 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  en));.  }..  if(
30c6f 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78   SQLITE_OK!=p->x
30c70 43 6c 6f 73 65 28 70 43 73 72 29 20 29 7b 0a 20  Close(pCsr) ){. 
30c71 20 20 20 7a 45 72 72 20 3d 20 22 65 72 72 6f 72     zErr = "error
30c72 20 69 6e 20 78 43 6c 6f 73 65 28 29 22 3b 0a 20   in xClose()";. 
30c73 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 68 3b 0a     goto finish;.
30c74 20 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 54 45    }.  if( SQLITE
30c75 5f 4f 4b 21 3d 70 2d 3e 78 44 65 73 74 72 6f 79  _OK!=p->xDestroy
30c76 28 70 54 6f 6b 65 6e 69 7a 65 72 29 20 29 7b 0a  (pTokenizer) ){.
30c77 20 20 20 20 7a 45 72 72 20 3d 20 22 65 72 72 6f      zErr = "erro
30c78 72 20 69 6e 20 78 44 65 73 74 72 6f 79 28 29 22  r in xDestroy()"
30c79 3b 0a 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73  ;.    goto finis
30c7a 68 3b 0a 20 20 7d 0a 0a 66 69 6e 69 73 68 3a 0a  h;.  }..finish:.
30c7b 20 20 69 66 28 20 7a 45 72 72 20 29 7b 0a 20 20    if( zErr ){.  
30c7c 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
30c7d 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
30c7e 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20 7d 65 6c  zErr, -1);.  }el
30c7f 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  se{.    sqlite3_
30c80 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
30c81 65 78 74 2c 20 54 63 6c 5f 47 65 74 53 74 72 69  ext, Tcl_GetStri
30c82 6e 67 28 70 52 65 74 29 2c 20 2d 31 2c 20 53 51  ng(pRet), -1, SQ
30c83 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
30c84 0a 20 20 7d 0a 20 20 54 63 6c 5f 44 65 63 72 52  .  }.  Tcl_DecrR
30c85 65 66 43 6f 75 6e 74 28 70 52 65 74 29 3b 0a 7d  efCount(pRet);.}
30c86 0a 0a 73 74 61 74 69 63 0a 69 6e 74 20 72 65 67  ..static.int reg
30c87 69 73 74 65 72 54 6f 6b 65 6e 69 7a 65 72 28 0a  isterTokenizer(.
30c88 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a    sqlite3 *db, .
30c89 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a    char *zName, .
30c8a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
30c8b 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
30c8c 20 2a 70 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   *p.){.  int rc;
30c8d 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
30c8e 2a 70 53 74 6d 74 3b 0a 20 20 63 6f 6e 73 74 20  *pStmt;.  const 
30c8f 63 68 61 72 20 7a 53 71 6c 5b 5d 20 3d 20 22 53  char zSql[] = "S
30c90 45 4c 45 43 54 20 66 74 73 33 5f 74 6f 6b 65 6e  ELECT fts3_token
30c91 69 7a 65 72 28 3f 2c 20 3f 29 22 3b 0a 0a 20 20  izer(?, ?)";..  
30c92 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65  rc = sqlite3_pre
30c93 70 61 72 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c  pare_v2(db, zSql
30c94 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29  , -1, &pStmt, 0)
30c95 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
30c96 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74  TE_OK ){.    ret
30c97 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 73  urn rc;.  }..  s
30c98 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
30c99 28 70 53 74 6d 74 2c 20 31 2c 20 7a 4e 61 6d 65  (pStmt, 1, zName
30c9a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41  , -1, SQLITE_STA
30c9b 54 49 43 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  TIC);.  sqlite3_
30c9c 62 69 6e 64 5f 62 6c 6f 62 28 70 53 74 6d 74 2c  bind_blob(pStmt,
30c9d 20 32 2c 20 26 70 2c 20 73 69 7a 65 6f 66 28 70   2, &p, sizeof(p
30c9e 29 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ), SQLITE_STATIC
30c9f 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65  );.  sqlite3_ste
30ca0 70 28 70 53 74 6d 74 29 3b 0a 0a 20 20 72 65 74  p(pStmt);..  ret
30ca1 75 72 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  urn sqlite3_fina
30ca2 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a  lize(pStmt);.}..
30ca3 73 74 61 74 69 63 0a 69 6e 74 20 71 75 65 72 79  static.int query
30ca4 54 6f 6b 65 6e 69 7a 65 72 28 0a 20 20 73 71 6c  Tokenizer(.  sql
30ca5 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 63 68 61  ite3 *db, .  cha
30ca6 72 20 2a 7a 4e 61 6d 65 2c 20 20 0a 20 20 63 6f  r *zName,  .  co
30ca7 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  nst sqlite3_toke
30ca8 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 2a 70  nizer_module **p
30ca9 70 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  p.){.  int rc;. 
30caa 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
30cab 53 74 6d 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68  Stmt;.  const ch
30cac 61 72 20 7a 53 71 6c 5b 5d 20 3d 20 22 53 45 4c  ar zSql[] = "SEL
30cad 45 43 54 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  ECT fts3_tokeniz
30cae 65 72 28 3f 29 22 3b 0a 0a 20 20 2a 70 70 20 3d  er(?)";..  *pp =
30caf 20 30 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   0;.  rc = sqlit
30cb0 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62  e3_prepare_v2(db
30cb1 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74  , zSql, -1, &pSt
30cb2 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63  mt, 0);.  if( rc
30cb3 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
30cb4 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
30cb5 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  }..  sqlite3_bin
30cb6 64 5f 74 65 78 74 28 70 53 74 6d 74 2c 20 31 2c  d_text(pStmt, 1,
30cb7 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53 51 4c 49   zName, -1, SQLI
30cb8 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 69 66  TE_STATIC);.  if
30cb9 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71  ( SQLITE_ROW==sq
30cba 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74  lite3_step(pStmt
30cbb 29 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  ) ){.    if( sql
30cbc 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65  ite3_column_type
30cbd 28 70 53 74 6d 74 2c 20 30 29 3d 3d 53 51 4c 49  (pStmt, 0)==SQLI
30cbe 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 20  TE_BLOB ){.     
30cbf 20 6d 65 6d 63 70 79 28 70 70 2c 20 73 71 6c 69   memcpy(pp, sqli
30cc0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
30cc1 70 53 74 6d 74 2c 20 30 29 2c 20 73 69 7a 65 6f  pStmt, 0), sizeo
30cc2 66 28 2a 70 70 29 29 3b 0a 20 20 20 20 7d 0a 20  f(*pp));.    }. 
30cc3 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c   }..  return sql
30cc4 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53  ite3_finalize(pS
30cc5 74 6d 74 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f  tmt);.}..SQLITE_
30cc6 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
30cc7 69 74 65 33 46 74 73 33 53 69 6d 70 6c 65 54 6f  ite3Fts3SimpleTo
30cc8 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 73 71  kenizerModule(sq
30cc9 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
30cca 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70  module const**pp
30ccb 4d 6f 64 75 6c 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20  Module);../*.** 
30ccc 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
30ccd 66 20 74 68 65 20 73 63 61 6c 61 72 20 66 75 6e  f the scalar fun
30cce 63 74 69 6f 6e 20 66 74 73 33 5f 74 6f 6b 65 6e  ction fts3_token
30ccf 69 7a 65 72 5f 69 6e 74 65 72 6e 61 6c 5f 74 65  izer_internal_te
30cd0 73 74 28 29 2e 0a 2a 2a 20 54 68 69 73 20 66 75  st()..** This fu
30cd1 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 66  nction is used f
30cd2 6f 72 20 74 65 73 74 69 6e 67 20 6f 6e 6c 79 2c  or testing only,
30cd3 20 69 74 20 69 73 20 6e 6f 74 20 69 6e 63 6c 75   it is not inclu
30cd4 64 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 62 75  ded in the.** bu
30cd5 69 6c 64 20 75 6e 6c 65 73 73 20 53 51 4c 49 54  ild unless SQLIT
30cd6 45 5f 54 45 53 54 20 69 73 20 64 65 66 69 6e 65  E_TEST is define
30cd7 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 75 72  d..**.** The pur
30cd8 70 6f 73 65 20 6f 66 20 74 68 69 73 20 69 73 20  pose of this is 
30cd9 74 6f 20 74 65 73 74 20 74 68 61 74 20 74 68 65  to test that the
30cda 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 28   fts3_tokenizer(
30cdb 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 63 61  ) function.** ca
30cdc 6e 20 62 65 20 75 73 65 64 20 61 73 20 64 65 73  n be used as des
30cdd 69 67 6e 65 64 20 62 79 20 74 68 65 20 43 2d 63  igned by the C-c
30cde 6f 64 65 20 69 6e 20 74 68 65 20 71 75 65 72 79  ode in the query
30cdf 54 6f 6b 65 6e 69 7a 65 72 20 61 6e 64 0a 2a 2a  Tokenizer and.**
30ce0 20 72 65 67 69 73 74 65 72 54 6f 6b 65 6e 69 7a   registerTokeniz
30ce1 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 73 20 61  er() functions a
30ce2 62 6f 76 65 2e 20 54 68 65 73 65 20 74 77 6f 20  bove. These two 
30ce3 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 72 65  functions are re
30ce4 70 65 61 74 65 64 0a 2a 2a 20 69 6e 20 74 68 65  peated.** in the
30ce5 20 52 45 41 44 4d 45 2e 74 6f 6b 65 6e 69 7a 65   README.tokenize
30ce6 72 20 66 69 6c 65 20 61 73 20 61 6e 20 65 78 61  r file as an exa
30ce7 6d 70 6c 65 2c 20 73 6f 20 69 74 20 69 73 20 69  mple, so it is i
30ce8 6d 70 6f 72 74 61 6e 74 20 74 6f 0a 2a 2a 20 74  mportant to.** t
30ce9 65 73 74 20 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20  est them..**.** 
30cea 54 6f 20 72 75 6e 20 74 68 65 20 74 65 73 74 73  To run the tests
30ceb 2c 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 66  , evaluate the f
30cec 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 69 6e  ts3_tokenizer_in
30ced 74 65 72 6e 61 6c 5f 74 65 73 74 28 29 20 73 63  ternal_test() sc
30cee 61 6c 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  alar.** function
30cef 20 77 69 74 68 20 6e 6f 20 61 72 67 75 6d 65 6e   with no argumen
30cf0 74 73 2e 20 41 6e 20 61 73 73 65 72 74 28 29 20  ts. An assert() 
30cf1 77 69 6c 6c 20 66 61 69 6c 20 69 66 20 61 20 70  will fail if a p
30cf2 72 6f 62 6c 65 6d 20 69 73 0a 2a 2a 20 64 65 74  roblem is.** det
30cf3 65 63 74 65 64 2e 20 69 2e 65 2e 3a 0a 2a 2a 0a  ected. i.e.:.**.
30cf4 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 66 74  **     SELECT ft
30cf5 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 69 6e 74  s3_tokenizer_int
30cf6 65 72 6e 61 6c 5f 74 65 73 74 28 29 3b 0a 2a 2a  ernal_test();.**
30cf7 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
30cf8 69 6e 74 54 65 73 74 46 75 6e 63 28 0a 20 20 73  intTestFunc(.  s
30cf9 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
30cfa 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
30cfb 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
30cfc 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
30cfd 20 69 6e 74 20 72 63 3b 0a 20 20 63 6f 6e 73 74   int rc;.  const
30cfe 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
30cff 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 31 3b 0a 20  er_module *p1;. 
30d00 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74   const sqlite3_t
30d01 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
30d02 2a 70 32 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  *p2;.  sqlite3 *
30d03 64 62 20 3d 20 28 73 71 6c 69 74 65 33 20 2a 29  db = (sqlite3 *)
30d04 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74  sqlite3_user_dat
30d05 61 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 2f  a(context);..  /
30d06 2a 20 54 65 73 74 20 74 68 65 20 71 75 65 72 79  * Test the query
30d07 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73   function */.  s
30d08 71 6c 69 74 65 33 46 74 73 33 53 69 6d 70 6c 65  qlite3Fts3Simple
30d09 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28  TokenizerModule(
30d0a 26 70 31 29 3b 0a 20 20 72 63 20 3d 20 71 75 65  &p1);.  rc = que
30d0b 72 79 54 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 20  ryTokenizer(db, 
30d0c 22 73 69 6d 70 6c 65 22 2c 20 26 70 32 29 3b 0a  "simple", &p2);.
30d0d 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
30d0e 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 61 73 73  LITE_OK );.  ass
30d0f 65 72 74 28 20 70 31 3d 3d 70 32 20 29 3b 0a 20  ert( p1==p2 );. 
30d10 20 72 63 20 3d 20 71 75 65 72 79 54 6f 6b 65 6e   rc = queryToken
30d11 69 7a 65 72 28 64 62 2c 20 22 6e 6f 73 75 63 68  izer(db, "nosuch
30d12 74 6f 6b 65 6e 69 7a 65 72 22 2c 20 26 70 32 29  tokenizer", &p2)
30d13 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  ;.  assert( rc==
30d14 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 3b 0a  SQLITE_ERROR );.
30d15 20 20 61 73 73 65 72 74 28 20 70 32 3d 3d 30 20    assert( p2==0 
30d16 29 3b 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d  );.  assert( 0==
30d17 73 74 72 63 6d 70 28 73 71 6c 69 74 65 33 5f 65  strcmp(sqlite3_e
30d18 72 72 6d 73 67 28 64 62 29 2c 20 22 75 6e 6b 6e  rrmsg(db), "unkn
30d19 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a 20 6e  own tokenizer: n
30d1a 6f 73 75 63 68 74 6f 6b 65 6e 69 7a 65 72 22 29  osuchtokenizer")
30d1b 20 29 3b 0a 0a 20 20 2f 2a 20 54 65 73 74 20 74   );..  /* Test t
30d1c 68 65 20 73 74 6f 72 61 67 65 20 66 75 6e 63 74  he storage funct
30d1d 69 6f 6e 20 2a 2f 0a 20 20 72 63 20 3d 20 72 65  ion */.  rc = re
30d1e 67 69 73 74 65 72 54 6f 6b 65 6e 69 7a 65 72 28  gisterTokenizer(
30d1f 64 62 2c 20 22 6e 6f 73 75 63 68 74 6f 6b 65 6e  db, "nosuchtoken
30d20 69 7a 65 72 22 2c 20 70 31 29 3b 0a 20 20 61 73  izer", p1);.  as
30d21 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
30d22 5f 4f 4b 20 29 3b 0a 20 20 72 63 20 3d 20 71 75  _OK );.  rc = qu
30d23 65 72 79 54 6f 6b 65 6e 69 7a 65 72 28 64 62 2c  eryTokenizer(db,
30d24 20 22 6e 6f 73 75 63 68 74 6f 6b 65 6e 69 7a 65   "nosuchtokenize
30d25 72 22 2c 20 26 70 32 29 3b 0a 20 20 61 73 73 65  r", &p2);.  asse
30d26 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
30d27 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  K );.  assert( p
30d28 32 3d 3d 70 31 20 29 3b 0a 0a 20 20 73 71 6c 69  2==p1 );..  sqli
30d29 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
30d2a 63 6f 6e 74 65 78 74 2c 20 22 6f 6b 22 2c 20 2d  context, "ok", -
30d2b 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  1, SQLITE_STATIC
30d2c 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a  );.}..#endif../*
30d2d 0a 2a 2a 20 53 65 74 20 75 70 20 53 51 4c 20 6f  .** Set up SQL o
30d2e 62 6a 65 63 74 73 20 69 6e 20 64 61 74 61 62 61  bjects in databa
30d2f 73 65 20 64 62 20 75 73 65 64 20 74 6f 20 61 63  se db used to ac
30d30 63 65 73 73 20 74 68 65 20 63 6f 6e 74 65 6e 74  cess the content
30d31 73 20 6f 66 0a 2a 2a 20 74 68 65 20 68 61 73 68  s of.** the hash
30d32 20 74 61 62 6c 65 20 70 6f 69 6e 74 65 64 20 74   table pointed t
30d33 6f 20 62 79 20 61 72 67 75 6d 65 6e 74 20 70 48  o by argument pH
30d34 61 73 68 2e 20 54 68 65 20 68 61 73 68 20 74 61  ash. The hash ta
30d35 62 6c 65 20 6d 75 73 74 0a 2a 2a 20 62 65 65 6e  ble must.** been
30d36 20 69 6e 69 74 69 61 6c 69 73 65 64 20 74 6f 20   initialised to 
30d37 75 73 65 20 73 74 72 69 6e 67 20 6b 65 79 73 2c  use string keys,
30d38 20 61 6e 64 20 74 6f 20 74 61 6b 65 20 61 20 70   and to take a p
30d39 72 69 76 61 74 65 20 63 6f 70 79 20 0a 2a 2a 20  rivate copy .** 
30d3a 6f 66 20 74 68 65 20 6b 65 79 20 77 68 65 6e 20  of the key when 
30d3b 61 20 76 61 6c 75 65 20 69 73 20 69 6e 73 65 72  a value is inser
30d3c 74 65 64 2e 20 69 2e 65 2e 20 62 79 20 61 20 63  ted. i.e. by a c
30d3d 61 6c 6c 20 73 69 6d 69 6c 61 72 20 74 6f 3a 0a  all similar to:.
30d3e 2a 2a 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65 33  **.**    sqlite3
30d3f 46 74 73 33 48 61 73 68 49 6e 69 74 28 70 48 61  Fts3HashInit(pHa
30d40 73 68 2c 20 46 54 53 33 5f 48 41 53 48 5f 53 54  sh, FTS3_HASH_ST
30d41 52 49 4e 47 2c 20 31 29 3b 0a 2a 2a 0a 2a 2a 20  RING, 1);.**.** 
30d42 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 64  This function ad
30d43 64 73 20 61 20 73 63 61 6c 61 72 20 66 75 6e 63  ds a scalar func
30d44 74 69 6f 6e 20 28 73 65 65 20 68 65 61 64 65 72  tion (see header
30d45 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 0a 2a   comment above.*
30d46 2a 20 73 63 61 6c 61 72 46 75 6e 63 28 29 20 69  * scalarFunc() i
30d47 6e 20 74 68 69 73 20 66 69 6c 65 20 66 6f 72 20  n this file for 
30d48 64 65 74 61 69 6c 73 29 20 61 6e 64 2c 20 69 66  details) and, if
30d49 20 45 4e 41 42 4c 45 5f 54 41 42 4c 45 20 69 73   ENABLE_TABLE is
30d4a 0a 2a 2a 20 64 65 66 69 6e 65 64 20 61 74 20 63  .** defined at c
30d4b 6f 6d 70 69 6c 61 74 69 6f 6e 20 74 69 6d 65 2c  ompilation time,
30d4c 20 61 20 74 65 6d 70 6f 72 61 72 79 20 76 69 72   a temporary vir
30d4d 74 75 61 6c 20 74 61 62 6c 65 20 28 73 65 65 20  tual table (see 
30d4e 68 65 61 64 65 72 20 0a 2a 2a 20 63 6f 6d 6d 65  header .** comme
30d4f 6e 74 20 61 62 6f 76 65 20 73 74 72 75 63 74 20  nt above struct 
30d50 48 61 73 68 54 61 62 6c 65 56 74 61 62 29 20 74  HashTableVtab) t
30d51 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  o the database s
30d52 63 68 65 6d 61 2e 20 42 6f 74 68 20 0a 2a 2a 20  chema. Both .** 
30d53 70 72 6f 76 69 64 65 20 72 65 61 64 2f 77 72 69  provide read/wri
30d54 74 65 20 61 63 63 65 73 73 20 74 6f 20 74 68 65  te access to the
30d55 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 2a 70 48   contents of *pH
30d56 61 73 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74  ash..**.** The t
30d57 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 74 6f  hird argument to
30d58 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20   this function, 
30d59 7a 4e 61 6d 65 2c 20 69 73 20 75 73 65 64 20 61  zName, is used a
30d5a 73 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66  s the name.** of
30d5b 20 62 6f 74 68 20 74 68 65 20 73 63 61 6c 61 72   both the scalar
30d5c 20 61 6e 64 2c 20 69 66 20 63 72 65 61 74 65 64   and, if created
30d5d 2c 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  , the virtual ta
30d5e 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ble..*/.SQLITE_P
30d5f 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
30d60 65 33 46 74 73 33 49 6e 69 74 48 61 73 68 54 61  e3Fts3InitHashTa
30d61 62 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ble(.  sqlite3 *
30d62 64 62 2c 20 0a 20 20 66 74 73 33 48 61 73 68 20  db, .  fts3Hash 
30d63 2a 70 48 61 73 68 2c 20 0a 20 20 63 6f 6e 73 74  *pHash, .  const
30d64 20 63 68 61 72 20 2a 7a 4e 61 6d 65 0a 29 7b 0a   char *zName.){.
30d65 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
30d66 45 5f 4f 4b 3b 0a 20 20 76 6f 69 64 20 2a 70 20  E_OK;.  void *p 
30d67 3d 20 28 76 6f 69 64 20 2a 29 70 48 61 73 68 3b  = (void *)pHash;
30d68 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 61 6e 79  .  const int any
30d69 20 3d 20 53 51 4c 49 54 45 5f 41 4e 59 3b 0a 20   = SQLITE_ANY;. 
30d6a 20 63 68 61 72 20 2a 7a 54 65 73 74 20 3d 20 30   char *zTest = 0
30d6b 3b 0a 20 20 63 68 61 72 20 2a 7a 54 65 73 74 32  ;.  char *zTest2
30d6c 20 3d 20 30 3b 0a 0a 23 69 66 64 65 66 20 53 51   = 0;..#ifdef SQ
30d6d 4c 49 54 45 5f 54 45 53 54 0a 20 20 76 6f 69 64  LITE_TEST.  void
30d6e 20 2a 70 64 62 20 3d 20 28 76 6f 69 64 20 2a 29   *pdb = (void *)
30d6f 64 62 3b 0a 20 20 7a 54 65 73 74 20 3d 20 73 71  db;.  zTest = sq
30d70 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25  lite3_mprintf("%
30d71 73 5f 74 65 73 74 22 2c 20 7a 4e 61 6d 65 29 3b  s_test", zName);
30d72 0a 20 20 7a 54 65 73 74 32 20 3d 20 73 71 6c 69  .  zTest2 = sqli
30d73 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 5f  te3_mprintf("%s_
30d74 69 6e 74 65 72 6e 61 6c 5f 74 65 73 74 22 2c 20  internal_test", 
30d75 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 7a  zName);.  if( !z
30d76 54 65 73 74 20 7c 7c 20 21 7a 54 65 73 74 32 20  Test || !zTest2 
30d77 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
30d78 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 65  TE_NOMEM;.  }.#e
30d79 6e 64 69 66 0a 0a 20 20 69 66 28 20 72 63 21 3d  ndif..  if( rc!=
30d7a 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 20 7c 7c 20  SQLITE_OK.   || 
30d7b 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72  (rc = sqlite3_cr
30d7c 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62  eate_function(db
30d7d 2c 20 7a 4e 61 6d 65 2c 20 31 2c 20 61 6e 79 2c  , zName, 1, any,
30d7e 20 70 2c 20 73 63 61 6c 61 72 46 75 6e 63 2c 20   p, scalarFunc, 
30d7f 30 2c 20 30 29 29 0a 20 20 20 7c 7c 20 28 72 63  0, 0)).   || (rc
30d80 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74   = sqlite3_creat
30d81 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a  e_function(db, z
30d82 4e 61 6d 65 2c 20 32 2c 20 61 6e 79 2c 20 70 2c  Name, 2, any, p,
30d83 20 73 63 61 6c 61 72 46 75 6e 63 2c 20 30 2c 20   scalarFunc, 0, 
30d84 30 29 29 0a 23 69 66 64 65 66 20 53 51 4c 49 54  0)).#ifdef SQLIT
30d85 45 5f 54 45 53 54 0a 20 20 20 7c 7c 20 28 72 63  E_TEST.   || (rc
30d86 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74   = sqlite3_creat
30d87 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a  e_function(db, z
30d88 54 65 73 74 2c 20 32 2c 20 61 6e 79 2c 20 70 2c  Test, 2, any, p,
30d89 20 74 65 73 74 46 75 6e 63 2c 20 30 2c 20 30 29   testFunc, 0, 0)
30d8a 29 0a 20 20 20 7c 7c 20 28 72 63 20 3d 20 73 71  ).   || (rc = sq
30d8b 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
30d8c 63 74 69 6f 6e 28 64 62 2c 20 7a 54 65 73 74 2c  ction(db, zTest,
30d8d 20 33 2c 20 61 6e 79 2c 20 70 2c 20 74 65 73 74   3, any, p, test
30d8e 46 75 6e 63 2c 20 30 2c 20 30 29 29 0a 20 20 20  Func, 0, 0)).   
30d8f 7c 7c 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33  || (rc = sqlite3
30d90 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
30d91 28 64 62 2c 20 7a 54 65 73 74 32 2c 20 30 2c 20  (db, zTest2, 0, 
30d92 61 6e 79 2c 20 70 64 62 2c 20 69 6e 74 54 65 73  any, pdb, intTes
30d93 74 46 75 6e 63 2c 20 30 2c 20 30 29 29 0a 23 65  tFunc, 0, 0)).#e
30d94 6e 64 69 66 0a 20 20 29 3b 0a 0a 20 20 73 71 6c  ndif.  );..  sql
30d95 69 74 65 33 5f 66 72 65 65 28 7a 54 65 73 74 29  ite3_free(zTest)
30d96 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
30d97 28 7a 54 65 73 74 32 29 3b 0a 20 20 72 65 74 75  (zTest2);.  retu
30d98 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66  rn rc;.}..#endif
30d99 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c   /* !defined(SQL
30d9a 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66  ITE_CORE) || def
30d9b 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
30d9c 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a  LE_FTS3) */../**
30d9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
30d9e 20 6f 66 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a   of fts3_tokeniz
30d9f 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.c ***********
30da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30da1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
30da2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
30da3 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 74 6f 6b  in file fts3_tok
30da4 65 6e 69 7a 65 72 31 2e 63 20 2a 2a 2a 2a 2a 2a  enizer1.c ******
30da5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30da6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
30da7 2a 2a 20 32 30 30 36 20 4f 63 74 20 31 30 0a 2a  ** 2006 Oct 10.*
30da8 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
30da9 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
30daa 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
30dab 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
30dac 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
30dad 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
30dae 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
30daf 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
30db0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
30db1 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
30db2 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
30db3 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
30db4 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
30db5 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
30db6 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
30db7 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
30db8 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
30db9 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
30dba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30dbb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30dbc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30dbd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30dbe 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  **.**.** Impleme
30dbf 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 22  ntation of the "
30dc0 73 69 6d 70 6c 65 22 20 66 75 6c 6c 2d 74 65 78  simple" full-tex
30dc1 74 2d 73 65 61 72 63 68 20 74 6f 6b 65 6e 69 7a  t-search tokeniz
30dc2 65 72 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  er..*/../*.** Th
30dc3 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66  e code in this f
30dc4 69 6c 65 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70  ile is only comp
30dc5 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20  iled if:.**.**  
30dc6 20 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f     * The FTS3 mo
30dc7 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75  dule is being bu
30dc8 69 6c 74 20 61 73 20 61 6e 20 65 78 74 65 6e 73  ilt as an extens
30dc9 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 28 69 6e  ion.**       (in
30dca 20 77 68 69 63 68 20 63 61 73 65 20 53 51 4c 49   which case SQLI
30dcb 54 45 5f 43 4f 52 45 20 69 73 20 6e 6f 74 20 64  TE_CORE is not d
30dcc 65 66 69 6e 65 64 29 2c 20 6f 72 0a 2a 2a 0a 2a  efined), or.**.*
30dcd 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53 33  *     * The FTS3
30dce 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67   module is being
30dcf 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20   built into the 
30dd0 63 6f 72 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20  core of.**      
30dd1 20 53 51 4c 69 74 65 20 28 69 6e 20 77 68 69 63   SQLite (in whic
30dd2 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f 45 4e  h case SQLITE_EN
30dd3 41 42 4c 45 5f 46 54 53 33 20 69 73 20 64 65 66  ABLE_FTS3 is def
30dd4 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69 66 20 21 64  ined)..*/.#if !d
30dd5 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
30dd6 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
30dd7 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
30dd8 33 29 0a 0a 0a 0a 0a 74 79 70 65 64 65 66 20 73  3).....typedef s
30dd9 74 72 75 63 74 20 73 69 6d 70 6c 65 5f 74 6f 6b  truct simple_tok
30dda 65 6e 69 7a 65 72 20 7b 0a 20 20 73 71 6c 69 74  enizer {.  sqlit
30ddb 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 62 61 73  e3_tokenizer bas
30ddc 65 3b 0a 20 20 63 68 61 72 20 64 65 6c 69 6d 5b  e;.  char delim[
30ddd 31 32 38 5d 3b 20 20 20 20 20 20 20 20 20 20 20  128];           
30dde 20 20 2f 2a 20 66 6c 61 67 20 41 53 43 49 49 20    /* flag ASCII 
30ddf 64 65 6c 69 6d 69 74 65 72 73 20 2a 2f 0a 7d 20  delimiters */.} 
30de0 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72  simple_tokenizer
30de1 3b 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  ;..typedef struc
30de2 74 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a  t simple_tokeniz
30de3 65 72 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73 71  er_cursor {.  sq
30de4 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
30de5 63 75 72 73 6f 72 20 62 61 73 65 3b 0a 20 20 63  cursor base;.  c
30de6 6f 6e 73 74 20 63 68 61 72 20 2a 70 49 6e 70 75  onst char *pInpu
30de7 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69  t;          /* i
30de8 6e 70 75 74 20 77 65 20 61 72 65 20 74 6f 6b 65  nput we are toke
30de9 6e 69 7a 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  nizing */.  int 
30dea 6e 42 79 74 65 73 3b 20 20 20 20 20 20 20 20 20  nBytes;         
30deb 20 20 20 20 20 20 20 20 20 2f 2a 20 73 69 7a 65           /* size
30dec 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 2a 2f   of the input */
30ded 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20  .  int iOffset; 
30dee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30def 2f 2a 20 63 75 72 72 65 6e 74 20 70 6f 73 69 74  /* current posit
30df0 69 6f 6e 20 69 6e 20 70 49 6e 70 75 74 20 2a 2f  ion in pInput */
30df1 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e 3b 20 20  .  int iToken;  
30df2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30df3 2f 2a 20 69 6e 64 65 78 20 6f 66 20 6e 65 78 74  /* index of next
30df4 20 74 6f 6b 65 6e 20 74 6f 20 62 65 20 72 65 74   token to be ret
30df5 75 72 6e 65 64 20 2a 2f 0a 20 20 63 68 61 72 20  urned */.  char 
30df6 2a 70 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20  *pToken;        
30df7 20 20 20 20 20 20 20 20 2f 2a 20 73 74 6f 72 61          /* stora
30df8 67 65 20 66 6f 72 20 63 75 72 72 65 6e 74 20 74  ge for current t
30df9 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54  oken */.  int nT
30dfa 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65 64 3b 20 20  okenAllocated;  
30dfb 20 20 20 20 20 20 20 2f 2a 20 73 70 61 63 65 20         /* space 
30dfc 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 7a 54 6f  allocated to zTo
30dfd 6b 65 6e 20 62 75 66 66 65 72 20 2a 2f 0a 7d 20  ken buffer */.} 
30dfe 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72  simple_tokenizer
30dff 5f 63 75 72 73 6f 72 3b 0a 0a 0a 2f 2a 20 46 6f  _cursor;.../* Fo
30e00 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f  rward declaratio
30e01 6e 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  n */.static cons
30e02 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  t sqlite3_tokeni
30e03 7a 65 72 5f 6d 6f 64 75 6c 65 20 73 69 6d 70 6c  zer_module simpl
30e04 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  eTokenizerModule
30e05 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 73 69  ;..static int si
30e06 6d 70 6c 65 44 65 6c 69 6d 28 73 69 6d 70 6c 65  mpleDelim(simple
30e07 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 74 2c 20 75  _tokenizer *t, u
30e08 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 29 7b  nsigned char c){
30e09 0a 20 20 72 65 74 75 72 6e 20 63 3c 30 78 38 30  .  return c<0x80
30e0a 20 26 26 20 74 2d 3e 64 65 6c 69 6d 5b 63 5d 3b   && t->delim[c];
30e0b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  .}../*.** Create
30e0c 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a 65 72   a new tokenizer
30e0d 20 69 6e 73 74 61 6e 63 65 2e 0a 2a 2f 0a 73 74   instance..*/.st
30e0e 61 74 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 43  atic int simpleC
30e0f 72 65 61 74 65 28 0a 20 20 69 6e 74 20 61 72 67  reate(.  int arg
30e10 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20  c, const char * 
30e11 63 6f 6e 73 74 20 2a 61 72 67 76 2c 0a 20 20 73  const *argv,.  s
30e12 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
30e13 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 0a 29   **ppTokenizer.)
30e14 7b 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e  {.  simple_token
30e15 69 7a 65 72 20 2a 74 3b 0a 0a 20 20 74 20 3d 20  izer *t;..  t = 
30e16 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65  (simple_tokenize
30e17 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c  r *) sqlite3_mal
30e18 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 74 29 29 3b  loc(sizeof(*t));
30e19 0a 20 20 69 66 28 20 74 3d 3d 4e 55 4c 4c 20 29  .  if( t==NULL )
30e1a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
30e1b 4f 4d 45 4d 3b 0a 20 20 6d 65 6d 73 65 74 28 74  OMEM;.  memset(t
30e1c 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 74 29 29  , 0, sizeof(*t))
30e1d 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65  ;..  /* TODO(she
30e1e 73 73 29 20 44 65 6c 69 6d 69 74 65 72 73 20 6e  ss) Delimiters n
30e1f 65 65 64 20 74 6f 20 72 65 6d 61 69 6e 20 74 68  eed to remain th
30e20 65 20 73 61 6d 65 20 66 72 6f 6d 20 72 75 6e 20  e same from run 
30e21 74 6f 20 72 75 6e 2c 0a 20 20 2a 2a 20 65 6c 73  to run,.  ** els
30e22 65 20 77 65 20 6e 65 65 64 20 74 6f 20 72 65 69  e we need to rei
30e23 6e 64 65 78 2e 20 20 4f 6e 65 20 73 6f 6c 75 74  ndex.  One solut
30e24 69 6f 6e 20 77 6f 75 6c 64 20 62 65 20 61 20 6d  ion would be a m
30e25 65 74 61 2d 74 61 62 6c 65 20 74 6f 0a 20 20 2a  eta-table to.  *
30e26 2a 20 74 72 61 63 6b 20 73 75 63 68 20 69 6e 66  * track such inf
30e27 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65 20  ormation in the 
30e28 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 77  database, then w
30e29 65 27 64 20 6f 6e 6c 79 20 77 61 6e 74 20 74 68  e'd only want th
30e2a 69 73 0a 20 20 2a 2a 20 69 6e 66 6f 72 6d 61 74  is.  ** informat
30e2b 69 6f 6e 20 6f 6e 20 74 68 65 20 69 6e 69 74 69  ion on the initi
30e2c 61 6c 20 63 72 65 61 74 65 2e 0a 20 20 2a 2f 0a  al create..  */.
30e2d 20 20 69 66 28 20 61 72 67 63 3e 31 20 29 7b 0a    if( argc>1 ){.
30e2e 20 20 20 20 69 6e 74 20 69 2c 20 6e 20 3d 20 73      int i, n = s
30e2f 74 72 6c 65 6e 28 61 72 67 76 5b 31 5d 29 3b 0a  trlen(argv[1]);.
30e30 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
30e31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 75 6e  ; i++){.      un
30e32 73 69 67 6e 65 64 20 63 68 61 72 20 63 68 20 3d  signed char ch =
30e33 20 61 72 67 76 5b 31 5d 5b 69 5d 3b 0a 20 20 20   argv[1][i];.   
30e34 20 20 20 2f 2a 20 57 65 20 65 78 70 6c 69 63 69     /* We explici
30e35 74 6c 79 20 64 6f 6e 27 74 20 73 75 70 70 6f 72  tly don't suppor
30e36 74 20 55 54 46 2d 38 20 64 65 6c 69 6d 69 74 65  t UTF-8 delimite
30e37 72 73 20 66 6f 72 20 6e 6f 77 2e 20 2a 2f 0a 20  rs for now. */. 
30e38 20 20 20 20 20 69 66 28 20 63 68 3e 3d 30 78 38       if( ch>=0x8
30e39 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  0 ){.        sql
30e3a 69 74 65 33 5f 66 72 65 65 28 74 29 3b 0a 20 20  ite3_free(t);.  
30e3b 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
30e3c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
30e3d 20 7d 0a 20 20 20 20 20 20 74 2d 3e 64 65 6c 69   }.      t->deli
30e3e 6d 5b 63 68 5d 20 3d 20 31 3b 0a 20 20 20 20 7d  m[ch] = 1;.    }
30e3f 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20  .  } else {.    
30e40 2f 2a 20 4d 61 72 6b 20 6e 6f 6e 2d 61 6c 70 68  /* Mark non-alph
30e41 61 6e 75 6d 65 72 69 63 20 41 53 43 49 49 20 63  anumeric ASCII c
30e42 68 61 72 61 63 74 65 72 73 20 61 73 20 64 65 6c  haracters as del
30e43 69 6d 69 74 65 72 73 20 2a 2f 0a 20 20 20 20 69  imiters */.    i
30e44 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nt i;.    for(i=
30e45 31 3b 20 69 3c 30 78 38 30 3b 20 69 2b 2b 29 7b  1; i<0x80; i++){
30e46 0a 20 20 20 20 20 20 74 2d 3e 64 65 6c 69 6d 5b  .      t->delim[
30e47 69 5d 20 3d 20 21 69 73 61 6c 6e 75 6d 28 69 29  i] = !isalnum(i)
30e48 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a  ;.    }.  }..  *
30e49 70 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 26 74  ppTokenizer = &t
30e4a 2d 3e 62 61 73 65 3b 0a 20 20 72 65 74 75 72 6e  ->base;.  return
30e4b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
30e4c 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 74  *.** Destroy a t
30e4d 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74  okenizer.*/.stat
30e4e 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 44 65 73  ic int simpleDes
30e4f 74 72 6f 79 28 73 71 6c 69 74 65 33 5f 74 6f 6b  troy(sqlite3_tok
30e50 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a  enizer *pTokeniz
30e51 65 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66  er){.  sqlite3_f
30e52 72 65 65 28 70 54 6f 6b 65 6e 69 7a 65 72 29 3b  ree(pTokenizer);
30e53 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
30e54 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72  _OK;.}../*.** Pr
30e55 65 70 61 72 65 20 74 6f 20 62 65 67 69 6e 20 74  epare to begin t
30e56 6f 6b 65 6e 69 7a 69 6e 67 20 61 20 70 61 72 74  okenizing a part
30e57 69 63 75 6c 61 72 20 73 74 72 69 6e 67 2e 20 20  icular string.  
30e58 54 68 65 20 69 6e 70 75 74 0a 2a 2a 20 73 74 72  The input.** str
30e59 69 6e 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69  ing to be tokeni
30e5a 7a 65 64 20 69 73 20 70 49 6e 70 75 74 5b 30 2e  zed is pInput[0.
30e5b 2e 6e 42 79 74 65 73 2d 31 5d 2e 20 20 41 20 63  .nBytes-1].  A c
30e5c 75 72 73 6f 72 0a 2a 2a 20 75 73 65 64 20 74 6f  ursor.** used to
30e5d 20 69 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 74   incrementally t
30e5e 6f 6b 65 6e 69 7a 65 20 74 68 69 73 20 73 74 72  okenize this str
30e5f 69 6e 67 20 69 73 20 72 65 74 75 72 6e 65 64 20  ing is returned 
30e60 69 6e 20 0a 2a 2a 20 2a 70 70 43 75 72 73 6f 72  in .** *ppCursor
30e61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
30e62 73 69 6d 70 6c 65 4f 70 65 6e 28 0a 20 20 73 71  simpleOpen(.  sq
30e63 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
30e64 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20  *pTokenizer,    
30e65 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65       /* The toke
30e66 6e 69 7a 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74  nizer */.  const
30e67 20 63 68 61 72 20 2a 70 49 6e 70 75 74 2c 20 69   char *pInput, i
30e68 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20  nt nBytes,      
30e69 20 20 2f 2a 20 53 74 72 69 6e 67 20 74 6f 20 62    /* String to b
30e6a 65 20 74 6f 6b 65 6e 69 7a 65 64 20 2a 2f 0a 20  e tokenized */. 
30e6b 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
30e6c 65 72 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75  er_cursor **ppCu
30e6d 72 73 6f 72 20 20 20 20 2f 2a 20 4f 55 54 3a 20  rsor    /* OUT: 
30e6e 54 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72  Tokenization cur
30e6f 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 73 69 6d 70  sor */.){.  simp
30e70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  le_tokenizer_cur
30e71 73 6f 72 20 2a 63 3b 0a 0a 20 20 63 20 3d 20 28  sor *c;..  c = (
30e72 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72  simple_tokenizer
30e73 5f 63 75 72 73 6f 72 20 2a 29 20 73 71 6c 69 74  _cursor *) sqlit
30e74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66  e3_malloc(sizeof
30e75 28 2a 63 29 29 3b 0a 20 20 69 66 28 20 63 3d 3d  (*c));.  if( c==
30e76 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 20 53 51  NULL ) return SQ
30e77 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a 20 20 63  LITE_NOMEM;..  c
30e78 2d 3e 70 49 6e 70 75 74 20 3d 20 70 49 6e 70 75  ->pInput = pInpu
30e79 74 3b 0a 20 20 69 66 28 20 70 49 6e 70 75 74 3d  t;.  if( pInput=
30e7a 3d 30 20 29 7b 0a 20 20 20 20 63 2d 3e 6e 42 79  =0 ){.    c->nBy
30e7b 74 65 73 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  tes = 0;.  }else
30e7c 20 69 66 28 20 6e 42 79 74 65 73 3c 30 20 29 7b   if( nBytes<0 ){
30e7d 0a 20 20 20 20 63 2d 3e 6e 42 79 74 65 73 20 3d  .    c->nBytes =
30e7e 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 70 49 6e   (int)strlen(pIn
30e7f 70 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  put);.  }else{. 
30e80 20 20 20 63 2d 3e 6e 42 79 74 65 73 20 3d 20 6e     c->nBytes = n
30e81 42 79 74 65 73 3b 0a 20 20 7d 0a 20 20 63 2d 3e  Bytes;.  }.  c->
30e82 69 4f 66 66 73 65 74 20 3d 20 30 3b 20 20 20 20  iOffset = 0;    
30e83 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
30e84 73 74 61 72 74 20 74 6f 6b 65 6e 69 7a 69 6e 67  start tokenizing
30e85 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   at the beginnin
30e86 67 20 2a 2f 0a 20 20 63 2d 3e 69 54 6f 6b 65 6e  g */.  c->iToken
30e87 20 3d 20 30 3b 0a 20 20 63 2d 3e 70 54 6f 6b 65   = 0;.  c->pToke
30e88 6e 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20 20  n = NULL;       
30e89 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 20 73 70          /* no sp
30e8a 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 2c 20 79  ace allocated, y
30e8b 65 74 2e 20 2a 2f 0a 20 20 63 2d 3e 6e 54 6f 6b  et. */.  c->nTok
30e8c 65 6e 41 6c 6c 6f 63 61 74 65 64 20 3d 20 30 3b  enAllocated = 0;
30e8d 0a 0a 20 20 2a 70 70 43 75 72 73 6f 72 20 3d 20  ..  *ppCursor = 
30e8e 26 63 2d 3e 62 61 73 65 3b 0a 20 20 72 65 74 75  &c->base;.  retu
30e8f 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
30e90 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 74  ./*.** Close a t
30e91 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73  okenization curs
30e92 6f 72 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70  or previously op
30e93 65 6e 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74  ened by a call t
30e94 6f 0a 2a 2a 20 73 69 6d 70 6c 65 4f 70 65 6e 28  o.** simpleOpen(
30e95 29 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73 74 61 74  ) above..*/.stat
30e96 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 43 6c 6f  ic int simpleClo
30e97 73 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  se(sqlite3_token
30e98 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75  izer_cursor *pCu
30e99 72 73 6f 72 29 7b 0a 20 20 73 69 6d 70 6c 65 5f  rsor){.  simple_
30e9a 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
30e9b 20 2a 63 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f   *c = (simple_to
30e9c 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
30e9d 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 73 71 6c  ) pCursor;.  sql
30e9e 69 74 65 33 5f 66 72 65 65 28 63 2d 3e 70 54 6f  ite3_free(c->pTo
30e9f 6b 65 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ken);.  sqlite3_
30ea0 66 72 65 65 28 63 29 3b 0a 20 20 72 65 74 75 72  free(c);.  retur
30ea1 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
30ea2 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68  /*.** Extract th
30ea3 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f  e next token fro
30ea4 6d 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e  m a tokenization
30ea5 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 63 75   cursor.  The cu
30ea6 72 73 6f 72 20 6d 75 73 74 0a 2a 2a 20 68 61 76  rsor must.** hav
30ea7 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 62 79  e been opened by
30ea8 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f   a prior call to
30ea9 20 73 69 6d 70 6c 65 4f 70 65 6e 28 29 2e 0a 2a   simpleOpen()..*
30eaa 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 69 6d  /.static int sim
30eab 70 6c 65 4e 65 78 74 28 0a 20 20 73 71 6c 69 74  pleNext(.  sqlit
30eac 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  e3_tokenizer_cur
30ead 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 20 2f  sor *pCursor,  /
30eae 2a 20 43 75 72 73 6f 72 20 72 65 74 75 72 6e 65  * Cursor returne
30eaf 64 20 62 79 20 73 69 6d 70 6c 65 4f 70 65 6e 20  d by simpleOpen 
30eb0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
30eb1 2a 2a 70 70 54 6f 6b 65 6e 2c 20 20 20 20 20 20  **ppToken,      
30eb2 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
30eb3 20 2a 70 70 54 6f 6b 65 6e 20 69 73 20 74 68 65   *ppToken is the
30eb4 20 74 6f 6b 65 6e 20 74 65 78 74 20 2a 2f 0a 20   token text */. 
30eb5 20 69 6e 74 20 2a 70 6e 42 79 74 65 73 2c 20 20   int *pnBytes,  
30eb6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30eb7 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d       /* OUT: Num
30eb8 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
30eb9 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a  token */.  int *
30eba 70 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 20  piStartOffset,  
30ebb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
30ebc 2a 20 4f 55 54 3a 20 53 74 61 72 74 69 6e 67 20  * OUT: Starting 
30ebd 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20  offset of token 
30ebe 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 45 6e 64 4f  */.  int *piEndO
30ebf 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 20  ffset,          
30ec0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
30ec1 20 45 6e 64 69 6e 67 20 6f 66 66 73 65 74 20 6f   Ending offset o
30ec2 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74  f token */.  int
30ec3 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 20 20 20   *piPosition    
30ec4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30ec5 20 2f 2a 20 4f 55 54 3a 20 50 6f 73 69 74 69 6f   /* OUT: Positio
30ec6 6e 20 69 6e 74 65 67 65 72 20 6f 66 20 74 6f 6b  n integer of tok
30ec7 65 6e 20 2a 2f 0a 29 7b 0a 20 20 73 69 6d 70 6c  en */.){.  simpl
30ec8 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  e_tokenizer_curs
30ec9 6f 72 20 2a 63 20 3d 20 28 73 69 6d 70 6c 65 5f  or *c = (simple_
30eca 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
30ecb 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 73   *) pCursor;.  s
30ecc 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20  imple_tokenizer 
30ecd 2a 74 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f 6b  *t = (simple_tok
30ece 65 6e 69 7a 65 72 20 2a 29 20 70 43 75 72 73 6f  enizer *) pCurso
30ecf 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20  r->pTokenizer;. 
30ed0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
30ed1 70 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  p = (unsigned ch
30ed2 61 72 20 2a 29 63 2d 3e 70 49 6e 70 75 74 3b 0a  ar *)c->pInput;.
30ed3 0a 20 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66  .  while( c->iOf
30ed4 66 73 65 74 3c 63 2d 3e 6e 42 79 74 65 73 20 29  fset<c->nBytes )
30ed5 7b 0a 20 20 20 20 69 6e 74 20 69 53 74 61 72 74  {.    int iStart
30ed6 4f 66 66 73 65 74 3b 0a 0a 20 20 20 20 2f 2a 20  Offset;..    /* 
30ed7 53 63 61 6e 20 70 61 73 74 20 64 65 6c 69 6d 69  Scan past delimi
30ed8 74 65 72 20 63 68 61 72 61 63 74 65 72 73 20 2a  ter characters *
30ed9 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 63 2d 3e  /.    while( c->
30eda 69 4f 66 66 73 65 74 3c 63 2d 3e 6e 42 79 74 65  iOffset<c->nByte
30edb 73 20 26 26 20 73 69 6d 70 6c 65 44 65 6c 69 6d  s && simpleDelim
30edc 28 74 2c 20 70 5b 63 2d 3e 69 4f 66 66 73 65 74  (t, p[c->iOffset
30edd 5d 29 20 29 7b 0a 20 20 20 20 20 20 63 2d 3e 69  ]) ){.      c->i
30ede 4f 66 66 73 65 74 2b 2b 3b 0a 20 20 20 20 7d 0a  Offset++;.    }.
30edf 0a 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 6e 6f  .    /* Count no
30ee0 6e 2d 64 65 6c 69 6d 69 74 65 72 20 63 68 61 72  n-delimiter char
30ee1 61 63 74 65 72 73 2e 20 2a 2f 0a 20 20 20 20 69  acters. */.    i
30ee2 53 74 61 72 74 4f 66 66 73 65 74 20 3d 20 63 2d  StartOffset = c-
30ee3 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 77 68  >iOffset;.    wh
30ee4 69 6c 65 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c  ile( c->iOffset<
30ee5 63 2d 3e 6e 42 79 74 65 73 20 26 26 20 21 73 69  c->nBytes && !si
30ee6 6d 70 6c 65 44 65 6c 69 6d 28 74 2c 20 70 5b 63  mpleDelim(t, p[c
30ee7 2d 3e 69 4f 66 66 73 65 74 5d 29 20 29 7b 0a 20  ->iOffset]) ){. 
30ee8 20 20 20 20 20 63 2d 3e 69 4f 66 66 73 65 74 2b       c->iOffset+
30ee9 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  +;.    }..    if
30eea 28 20 63 2d 3e 69 4f 66 66 73 65 74 3e 69 53 74  ( c->iOffset>iSt
30eeb 61 72 74 4f 66 66 73 65 74 20 29 7b 0a 20 20 20  artOffset ){.   
30eec 20 20 20 69 6e 74 20 69 2c 20 6e 20 3d 20 63 2d     int i, n = c-
30eed 3e 69 4f 66 66 73 65 74 2d 69 53 74 61 72 74 4f  >iOffset-iStartO
30eee 66 66 73 65 74 3b 0a 20 20 20 20 20 20 69 66 28  ffset;.      if(
30eef 20 6e 3e 63 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f   n>c->nTokenAllo
30ef0 63 61 74 65 64 20 29 7b 0a 20 20 20 20 20 20 20  cated ){.       
30ef1 20 63 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61   c->nTokenAlloca
30ef2 74 65 64 20 3d 20 6e 2b 32 30 3b 0a 20 20 20 20  ted = n+20;.    
30ef3 20 20 20 20 63 2d 3e 70 54 6f 6b 65 6e 20 3d 20      c->pToken = 
30ef4 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
30ef5 63 2d 3e 70 54 6f 6b 65 6e 2c 20 63 2d 3e 6e 54  c->pToken, c->nT
30ef6 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65 64 29 3b 0a  okenAllocated);.
30ef7 20 20 20 20 20 20 20 20 69 66 28 20 63 2d 3e 70          if( c->p
30ef8 54 6f 6b 65 6e 3d 3d 4e 55 4c 4c 20 29 20 72 65  Token==NULL ) re
30ef9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
30efa 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  M;.      }.     
30efb 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69   for(i=0; i<n; i
30efc 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ++){.        /* 
30efd 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68 69 73  TODO(shess) This
30efe 20 6e 65 65 64 73 20 65 78 70 61 6e 73 69 6f 6e   needs expansion
30eff 20 74 6f 20 68 61 6e 64 6c 65 20 55 54 46 2d 38   to handle UTF-8
30f00 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 61 73 65  .        ** case
30f01 2d 69 6e 73 65 6e 73 69 74 69 76 69 74 79 2e 0a  -insensitivity..
30f02 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
30f03 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72     unsigned char
30f04 20 63 68 20 3d 20 70 5b 69 53 74 61 72 74 4f 66   ch = p[iStartOf
30f05 66 73 65 74 2b 69 5d 3b 0a 20 20 20 20 20 20 20  fset+i];.       
30f06 20 63 2d 3e 70 54 6f 6b 65 6e 5b 69 5d 20 3d 20   c->pToken[i] = 
30f07 63 68 3c 30 78 38 30 20 3f 20 74 6f 6c 6f 77 65  ch<0x80 ? tolowe
30f08 72 28 63 68 29 20 3a 20 63 68 3b 0a 20 20 20 20  r(ch) : ch;.    
30f09 20 20 7d 0a 20 20 20 20 20 20 2a 70 70 54 6f 6b    }.      *ppTok
30f0a 65 6e 20 3d 20 63 2d 3e 70 54 6f 6b 65 6e 3b 0a  en = c->pToken;.
30f0b 20 20 20 20 20 20 2a 70 6e 42 79 74 65 73 20 3d        *pnBytes =
30f0c 20 6e 3b 0a 20 20 20 20 20 20 2a 70 69 53 74 61   n;.      *piSta
30f0d 72 74 4f 66 66 73 65 74 20 3d 20 69 53 74 61 72  rtOffset = iStar
30f0e 74 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 2a  tOffset;.      *
30f0f 70 69 45 6e 64 4f 66 66 73 65 74 20 3d 20 63 2d  piEndOffset = c-
30f10 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20  >iOffset;.      
30f11 2a 70 69 50 6f 73 69 74 69 6f 6e 20 3d 20 63 2d  *piPosition = c-
30f12 3e 69 54 6f 6b 65 6e 2b 2b 3b 0a 0a 20 20 20 20  >iToken++;..    
30f13 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
30f14 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  OK;.    }.  }.  
30f15 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f  return SQLITE_DO
30f16 4e 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  NE;.}../*.** The
30f17 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73   set of routines
30f18 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
30f19 74 68 65 20 73 69 6d 70 6c 65 20 74 6f 6b 65 6e  the simple token
30f1a 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20 63  izer.*/.static c
30f1b 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b  onst sqlite3_tok
30f1c 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 73 69  enizer_module si
30f1d 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64  mpleTokenizerMod
30f1e 75 6c 65 20 3d 20 7b 0a 20 20 30 2c 0a 20 20 73  ule = {.  0,.  s
30f1f 69 6d 70 6c 65 43 72 65 61 74 65 2c 0a 20 20 73  impleCreate,.  s
30f20 69 6d 70 6c 65 44 65 73 74 72 6f 79 2c 0a 20 20  impleDestroy,.  
30f21 73 69 6d 70 6c 65 4f 70 65 6e 2c 0a 20 20 73 69  simpleOpen,.  si
30f22 6d 70 6c 65 43 6c 6f 73 65 2c 0a 20 20 73 69 6d  mpleClose,.  sim
30f23 70 6c 65 4e 65 78 74 2c 0a 7d 3b 0a 0a 2f 2a 0a  pleNext,.};../*.
30f24 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65  ** Allocate a ne
30f25 77 20 73 69 6d 70 6c 65 20 74 6f 6b 65 6e 69 7a  w simple tokeniz
30f26 65 72 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f  er.  Return a po
30f27 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77  inter to the new
30f28 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e  .** tokenizer in
30f29 20 2a 70 70 4d 6f 64 75 6c 65 0a 2a 2f 0a 53 51   *ppModule.*/.SQ
30f2a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
30f2b 64 20 73 71 6c 69 74 65 33 46 74 73 33 53 69 6d  d sqlite3Fts3Sim
30f2c 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75  pleTokenizerModu
30f2d 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  le(.  sqlite3_to
30f2e 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63  kenizer_module c
30f2f 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 0a 29  onst**ppModule.)
30f30 7b 0a 20 20 2a 70 70 4d 6f 64 75 6c 65 20 3d 20  {.  *ppModule = 
30f31 26 73 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72  &simpleTokenizer
30f32 4d 6f 64 75 6c 65 3b 0a 7d 0a 0a 23 65 6e 64 69  Module;.}..#endi
30f33 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51  f /* !defined(SQ
30f34 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65  LITE_CORE) || de
30f35 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
30f36 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a  BLE_FTS3) */../*
30f37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
30f38 64 20 6f 66 20 66 74 73 33 5f 74 6f 6b 65 6e 69  d of fts3_tokeni
30f39 7a 65 72 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  zer1.c *********
30f3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30f3b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
30f3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
30f3d 67 69 6e 20 66 69 6c 65 20 72 74 72 65 65 2e 63  gin file rtree.c
30f3e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
30f3f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
30f41 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
30f42 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
30f43 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
30f44 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
30f45 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
30f46 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
30f47 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
30f48 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
30f49 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
30f4a 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
30f4b 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
30f4c 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
30f4d 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
30f4e 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
30f4f 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
30f50 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
30f51 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
30f52 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
30f53 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
30f54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30f55 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30f56 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30f57 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30f58 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
30f59 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
30f5a 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  for implementati
30f5b 6f 6e 73 20 6f 66 20 74 68 65 20 72 2d 74 72 65  ons of the r-tre
30f5c 65 20 61 6e 64 20 72 2a 2d 74 72 65 65 0a 2a 2a  e and r*-tree.**
30f5d 20 61 6c 67 6f 72 69 74 68 6d 73 20 70 61 63 6b   algorithms pack
30f5e 61 67 65 64 20 61 73 20 61 6e 20 53 51 4c 69 74  aged as an SQLit
30f5f 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
30f60 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49  module..**.** $I
30f61 64 3a 20 72 74 72 65 65 2e 63 2c 76 20 31 2e 31  d: rtree.c,v 1.1
30f62 30 20 32 30 30 38 2f 31 30 2f 32 35 20 31 37 3a  0 2008/10/25 17:
30f63 31 30 3a 31 30 20 64 61 6e 69 65 6c 6b 31 39 37  10:10 danielk197
30f64 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 20  7 Exp $.*/..#if 
30f65 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
30f66 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64  CORE) || defined
30f67 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 52  (SQLITE_ENABLE_R
30f68 54 52 45 45 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  TREE)../*.** Thi
30f69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
30f6a 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  an implementatio
30f6b 6e 20 6f 66 20 61 20 63 6f 75 70 6c 65 20 6f 66  n of a couple of
30f6c 20 64 69 66 66 65 72 65 6e 74 20 76 61 72 69 61   different varia
30f6d 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 72 2d  nts.** of the r-
30f6e 74 72 65 65 20 61 6c 67 6f 72 69 74 68 6d 2e 20  tree algorithm. 
30f6f 53 65 65 20 74 68 65 20 52 45 41 44 4d 45 20 66  See the README f
30f70 69 6c 65 20 66 6f 72 20 66 75 72 74 68 65 72 20  ile for further 
30f71 64 65 74 61 69 6c 73 2e 20 54 68 65 20 0a 2a 2a  details. The .**
30f72 20 73 61 6d 65 20 64 61 74 61 2d 73 74 72 75 63   same data-struc
30f73 74 75 72 65 20 69 73 20 75 73 65 64 20 66 6f 72  ture is used for
30f74 20 61 6c 6c 2c 20 62 75 74 20 74 68 65 20 61 6c   all, but the al
30f75 67 6f 72 69 74 68 6d 73 20 66 6f 72 20 69 6e 73  gorithms for ins
30f76 65 72 74 20 61 6e 64 0a 2a 2a 20 64 65 6c 65 74  ert and.** delet
30f77 65 20 6f 70 65 72 61 74 69 6f 6e 73 20 76 61 72  e operations var
30f78 79 2e 20 54 68 65 20 76 61 72 69 61 6e 74 73 20  y. The variants 
30f79 75 73 65 64 20 61 72 65 20 73 65 6c 65 63 74 65  used are selecte
30f7a 64 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d  d at compile tim
30f7b 65 20 0a 2a 2a 20 62 79 20 64 65 66 69 6e 69 6e  e .** by definin
30f7c 67 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  g the following 
30f7d 73 79 6d 62 6f 6c 73 3a 0a 2a 2f 0a 0a 2f 2a 20  symbols:.*/../* 
30f7e 45 69 74 68 65 72 2c 20 62 6f 74 68 20 6f 72 20  Either, both or 
30f7f 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  none of the foll
30f80 6f 77 69 6e 67 20 6d 61 79 20 62 65 20 73 65 74  owing may be set
30f81 20 74 6f 20 61 63 74 69 76 61 74 65 20 0a 2a 2a   to activate .**
30f82 20 72 2a 74 72 65 65 20 76 61 72 69 61 6e 74 20   r*tree variant 
30f83 61 6c 67 6f 72 69 74 68 6d 73 2e 0a 2a 2f 0a 23  algorithms..*/.#
30f84 64 65 66 69 6e 65 20 56 41 52 49 41 4e 54 5f 52  define VARIANT_R
30f85 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53 45 53  STARTREE_CHOOSES
30f86 55 42 54 52 45 45 20 30 0a 23 64 65 66 69 6e 65  UBTREE 0.#define
30f87 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52   VARIANT_RSTARTR
30f88 45 45 5f 52 45 49 4e 53 45 52 54 20 20 20 20 20  EE_REINSERT     
30f89 20 31 0a 0a 2f 2a 20 0a 2a 2a 20 45 78 61 63 74   1../* .** Exact
30f8a 6c 79 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f  ly one of the fo
30f8b 6c 6c 6f 77 69 6e 67 20 6d 75 73 74 20 62 65 20  llowing must be 
30f8c 73 65 74 20 74 6f 20 31 2e 0a 2a 2f 0a 23 64 65  set to 1..*/.#de
30f8d 66 69 6e 65 20 56 41 52 49 41 4e 54 5f 47 55 54  fine VARIANT_GUT
30f8e 54 4d 41 4e 5f 51 55 41 44 52 41 54 49 43 5f 53  TMAN_QUADRATIC_S
30f8f 50 4c 49 54 20 30 0a 23 64 65 66 69 6e 65 20 56  PLIT 0.#define V
30f90 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c  ARIANT_GUTTMAN_L
30f91 49 4e 45 41 52 5f 53 50 4c 49 54 20 20 20 20 30  INEAR_SPLIT    0
30f92 0a 23 64 65 66 69 6e 65 20 56 41 52 49 41 4e 54  .#define VARIANT
30f93 5f 52 53 54 41 52 54 52 45 45 5f 53 50 4c 49 54  _RSTARTREE_SPLIT
30f94 20 20 20 20 20 20 20 20 20 31 0a 0a 23 64 65 66           1..#def
30f95 69 6e 65 20 56 41 52 49 41 4e 54 5f 47 55 54 54  ine VARIANT_GUTT
30f96 4d 41 4e 5f 53 50 4c 49 54 20 5c 0a 20 20 20 20  MAN_SPLIT \.    
30f97 20 20 20 20 28 56 41 52 49 41 4e 54 5f 47 55 54      (VARIANT_GUT
30f98 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49  TMAN_LINEAR_SPLI
30f99 54 7c 7c 56 41 52 49 41 4e 54 5f 47 55 54 54 4d  T||VARIANT_GUTTM
30f9a 41 4e 5f 51 55 41 44 52 41 54 49 43 5f 53 50 4c  AN_QUADRATIC_SPL
30f9b 49 54 29 0a 0a 23 69 66 20 56 41 52 49 41 4e 54  IT)..#if VARIANT
30f9c 5f 47 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54  _GUTTMAN_QUADRAT
30f9d 49 43 5f 53 50 4c 49 54 0a 20 20 23 64 65 66 69  IC_SPLIT.  #defi
30f9e 6e 65 20 50 69 63 6b 4e 65 78 74 20 51 75 61 64  ne PickNext Quad
30f9f 72 61 74 69 63 50 69 63 6b 4e 65 78 74 0a 20 20  raticPickNext.  
30fa0 23 64 65 66 69 6e 65 20 50 69 63 6b 53 65 65 64  #define PickSeed
30fa1 73 20 51 75 61 64 72 61 74 69 63 50 69 63 6b 53  s QuadraticPickS
30fa2 65 65 64 73 0a 20 20 23 64 65 66 69 6e 65 20 41  eeds.  #define A
30fa3 73 73 69 67 6e 43 65 6c 6c 73 20 73 70 6c 69 74  ssignCells split
30fa4 4e 6f 64 65 47 75 74 74 6d 61 6e 0a 23 65 6e 64  NodeGuttman.#end
30fa5 69 66 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47  if.#if VARIANT_G
30fa6 55 54 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53 50  UTTMAN_LINEAR_SP
30fa7 4c 49 54 0a 20 20 23 64 65 66 69 6e 65 20 50 69  LIT.  #define Pi
30fa8 63 6b 4e 65 78 74 20 4c 69 6e 65 61 72 50 69 63  ckNext LinearPic
30fa9 6b 4e 65 78 74 0a 20 20 23 64 65 66 69 6e 65 20  kNext.  #define 
30faa 50 69 63 6b 53 65 65 64 73 20 4c 69 6e 65 61 72  PickSeeds Linear
30fab 50 69 63 6b 53 65 65 64 73 0a 20 20 23 64 65 66  PickSeeds.  #def
30fac 69 6e 65 20 41 73 73 69 67 6e 43 65 6c 6c 73 20  ine AssignCells 
30fad 73 70 6c 69 74 4e 6f 64 65 47 75 74 74 6d 61 6e  splitNodeGuttman
30fae 0a 23 65 6e 64 69 66 0a 23 69 66 20 56 41 52 49  .#endif.#if VARI
30faf 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f 53 50  ANT_RSTARTREE_SP
30fb0 4c 49 54 0a 20 20 23 64 65 66 69 6e 65 20 41 73  LIT.  #define As
30fb1 73 69 67 6e 43 65 6c 6c 73 20 73 70 6c 69 74 4e  signCells splitN
30fb2 6f 64 65 53 74 61 72 74 72 65 65 0a 23 65 6e 64  odeStartree.#end
30fb3 69 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  if...#ifndef SQL
30fb4 49 54 45 5f 43 4f 52 45 0a 20 20 53 51 4c 49 54  ITE_CORE.  SQLIT
30fb5 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54  E_EXTENSION_INIT
30fb6 31 0a 23 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a  1.#else.#endif..
30fb7 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
30fb8 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 74 79 70  AMALGAMATION.typ
30fb9 65 64 65 66 20 73 71 6c 69 74 65 33 5f 69 6e 74  edef sqlite3_int
30fba 36 34 20 69 36 34 3b 0a 74 79 70 65 64 65 66 20  64 i64;.typedef 
30fbb 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 75 38  unsigned char u8
30fbc 3b 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e  ;.typedef unsign
30fbd 65 64 20 69 6e 74 20 75 33 32 3b 0a 23 65 6e 64  ed int u32;.#end
30fbe 69 66 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75  if..typedef stru
30fbf 63 74 20 52 74 72 65 65 20 52 74 72 65 65 3b 0a  ct Rtree Rtree;.
30fc0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 52  typedef struct R
30fc1 74 72 65 65 43 75 72 73 6f 72 20 52 74 72 65 65  treeCursor Rtree
30fc2 43 75 72 73 6f 72 3b 0a 74 79 70 65 64 65 66 20  Cursor;.typedef 
30fc3 73 74 72 75 63 74 20 52 74 72 65 65 4e 6f 64 65  struct RtreeNode
30fc4 20 52 74 72 65 65 4e 6f 64 65 3b 0a 74 79 70 65   RtreeNode;.type
30fc5 64 65 66 20 73 74 72 75 63 74 20 52 74 72 65 65  def struct Rtree
30fc6 43 65 6c 6c 20 52 74 72 65 65 43 65 6c 6c 3b 0a  Cell RtreeCell;.
30fc7 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 52  typedef struct R
30fc8 74 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 20 52  treeConstraint R
30fc9 74 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 3b 0a  treeConstraint;.
30fca 74 79 70 65 64 65 66 20 75 6e 69 6f 6e 20 52 74  typedef union Rt
30fcb 72 65 65 43 6f 6f 72 64 20 52 74 72 65 65 43 6f  reeCoord RtreeCo
30fcc 6f 72 64 3b 0a 0a 2f 2a 20 54 68 65 20 72 74 72  ord;../* The rtr
30fcd 65 65 20 6d 61 79 20 68 61 76 65 20 62 65 74 77  ee may have betw
30fce 65 65 6e 20 31 20 61 6e 64 20 52 54 52 45 45 5f  een 1 and RTREE_
30fcf 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 20 64  MAX_DIMENSIONS d
30fd0 69 6d 65 6e 73 69 6f 6e 73 2e 20 2a 2f 0a 23 64  imensions. */.#d
30fd1 65 66 69 6e 65 20 52 54 52 45 45 5f 4d 41 58 5f  efine RTREE_MAX_
30fd2 44 49 4d 45 4e 53 49 4f 4e 53 20 35 0a 0a 2f 2a  DIMENSIONS 5../*
30fd3 20 53 69 7a 65 20 6f 66 20 68 61 73 68 20 74 61   Size of hash ta
30fd4 62 6c 65 20 52 74 72 65 65 2e 61 48 61 73 68 2e  ble Rtree.aHash.
30fd5 20 54 68 69 73 20 68 61 73 68 20 74 61 62 6c 65   This hash table
30fd6 20 69 73 20 6e 6f 74 20 65 78 70 65 63 74 65 64   is not expected
30fd7 20 74 6f 0a 2a 2a 20 65 76 65 72 20 63 6f 6e 74   to.** ever cont
30fd8 61 69 6e 20 76 65 72 79 20 6d 61 6e 79 20 65 6e  ain very many en
30fd9 74 72 69 65 73 2c 20 73 6f 20 61 20 66 69 78 65  tries, so a fixe
30fda 64 20 6e 75 6d 62 65 72 20 6f 66 20 62 75 63 6b  d number of buck
30fdb 65 74 73 20 69 73 20 0a 2a 2a 20 75 73 65 64 2e  ets is .** used.
30fdc 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 48 41 53 48  .*/.#define HASH
30fdd 53 49 5a 45 20 31 32 38 0a 0a 2f 2a 20 0a 2a 2a  SIZE 128../* .**
30fde 20 41 6e 20 72 74 72 65 65 20 76 69 72 74 75 61   An rtree virtua
30fdf 6c 2d 74 61 62 6c 65 20 6f 62 6a 65 63 74 2e 0a  l-table object..
30fe0 2a 2f 0a 73 74 72 75 63 74 20 52 74 72 65 65 20  */.struct Rtree 
30fe1 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  {.  sqlite3_vtab
30fe2 20 62 61 73 65 3b 0a 20 20 73 71 6c 69 74 65 33   base;.  sqlite3
30fe3 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20   *db;           
30fe4 20 20 20 20 20 2f 2a 20 48 6f 73 74 20 64 61 74       /* Host dat
30fe5 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
30fe6 20 2a 2f 0a 20 20 69 6e 74 20 69 4e 6f 64 65 53   */.  int iNodeS
30fe7 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ize;            
30fe8 20 20 2f 2a 20 53 69 7a 65 20 69 6e 20 62 79 74    /* Size in byt
30fe9 65 73 20 6f 66 20 65 61 63 68 20 6e 6f 64 65 20  es of each node 
30fea 69 6e 20 74 68 65 20 6e 6f 64 65 20 74 61 62 6c  in the node tabl
30feb 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 69 6d 3b  e */.  int nDim;
30fec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30fed 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
30fee 64 69 6d 65 6e 73 69 6f 6e 73 20 2a 2f 0a 20 20  dimensions */.  
30fef 69 6e 74 20 6e 42 79 74 65 73 50 65 72 43 65 6c  int nBytesPerCel
30ff0 6c 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42  l;          /* B
30ff1 79 74 65 73 20 63 6f 6e 73 75 6d 65 64 20 70 65  ytes consumed pe
30ff2 72 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20  r cell */.  int 
30ff3 69 44 65 70 74 68 3b 20 20 20 20 20 20 20 20 20  iDepth;         
30ff4 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65          /* Curre
30ff5 6e 74 20 64 65 70 74 68 20 6f 66 20 74 68 65 20  nt depth of the 
30ff6 72 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65  r-tree structure
30ff7 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44 62 3b   */.  char *zDb;
30ff8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30ff9 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74    /* Name of dat
30ffa 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67  abase containing
30ffb 20 72 2d 74 72 65 65 20 74 61 62 6c 65 20 2a 2f   r-tree table */
30ffc 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20  .  char *zName; 
30ffd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
30ffe 2a 20 4e 61 6d 65 20 6f 66 20 72 2d 74 72 65 65  * Name of r-tree
30fff 20 74 61 62 6c 65 20 2a 2f 20 0a 20 20 52 74 72   table */ .  Rtr
31000 65 65 4e 6f 64 65 20 2a 61 48 61 73 68 5b 48 41  eeNode *aHash[HA
31001 53 48 53 49 5a 45 5d 3b 20 2f 2a 20 48 61 73 68  SHSIZE]; /* Hash
31002 20 74 61 62 6c 65 20 6f 66 20 69 6e 2d 6d 65 6d   table of in-mem
31003 6f 72 79 20 6e 6f 64 65 73 2e 20 2a 2f 20 0a 20  ory nodes. */ . 
31004 20 69 6e 74 20 6e 42 75 73 79 3b 20 20 20 20 20   int nBusy;     
31005 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
31006 43 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f  Current number o
31007 66 20 75 73 65 72 73 20 6f 66 20 74 68 69 73 20  f users of this 
31008 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 0a 20 20  structure */..  
31009 2f 2a 20 4c 69 73 74 20 6f 66 20 6e 6f 64 65 73  /* List of nodes
3100a 20 72 65 6d 6f 76 65 64 20 64 75 72 69 6e 67 20   removed during 
3100b 61 20 43 6f 6e 64 65 6e 73 65 54 72 65 65 20 6f  a CondenseTree o
3100c 70 65 72 61 74 69 6f 6e 2e 20 4c 69 73 74 20 69  peration. List i
3100d 73 0a 20 20 2a 2a 20 6c 69 6e 6b 65 64 20 74 6f  s.  ** linked to
3100e 67 65 74 68 65 72 20 76 69 61 20 74 68 65 20 70  gether via the p
3100f 6f 69 6e 74 65 72 20 6e 6f 72 6d 61 6c 6c 79 20  ointer normally 
31010 75 73 65 64 20 66 6f 72 20 68 61 73 68 20 63 68  used for hash ch
31011 61 69 6e 73 20 2d 0a 20 20 2a 2a 20 52 74 72 65  ains -.  ** Rtre
31012 65 4e 6f 64 65 2e 70 4e 65 78 74 2e 20 52 74 72  eNode.pNext. Rtr
31013 65 65 4e 6f 64 65 2e 69 4e 6f 64 65 20 73 74 6f  eeNode.iNode sto
31014 72 65 73 20 74 68 65 20 64 65 70 74 68 20 6f 66  res the depth of
31015 20 74 68 65 20 73 75 62 2d 74 72 65 65 20 0a 20   the sub-tree . 
31016 20 2a 2a 20 68 65 61 64 65 64 20 62 79 20 74 68   ** headed by th
31017 65 20 6e 6f 64 65 20 28 6c 65 61 66 20 6e 6f 64  e node (leaf nod
31018 65 73 20 68 61 76 65 20 52 74 72 65 65 4e 6f 64  es have RtreeNod
31019 65 2e 69 4e 6f 64 65 3d 3d 30 29 2e 0a 20 20 2a  e.iNode==0)..  *
3101a 2f 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  /.  RtreeNode *p
3101b 44 65 6c 65 74 65 64 3b 0a 20 20 69 6e 74 20 69  Deleted;.  int i
3101c 52 65 69 6e 73 65 72 74 48 65 69 67 68 74 3b 20  ReinsertHeight; 
3101d 20 20 20 20 20 20 20 2f 2a 20 48 65 69 67 68 74         /* Height
3101e 20 6f 66 20 73 75 62 2d 74 72 65 65 73 20 52 65   of sub-trees Re
3101f 69 6e 73 65 72 74 28 29 20 68 61 73 20 72 75 6e  insert() has run
31020 20 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 53 74 61   on */..  /* Sta
31021 74 65 6d 65 6e 74 73 20 74 6f 20 72 65 61 64 2f  tements to read/
31022 77 72 69 74 65 2f 64 65 6c 65 74 65 20 61 20 72  write/delete a r
31023 65 63 6f 72 64 20 66 72 6f 6d 20 78 78 78 5f 6e  ecord from xxx_n
31024 6f 64 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ode */.  sqlite3
31025 5f 73 74 6d 74 20 2a 70 52 65 61 64 4e 6f 64 65  _stmt *pReadNode
31026 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  ;.  sqlite3_stmt
31027 20 2a 70 57 72 69 74 65 4e 6f 64 65 3b 0a 20 20   *pWriteNode;.  
31028 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 44  sqlite3_stmt *pD
31029 65 6c 65 74 65 4e 6f 64 65 3b 0a 0a 20 20 2f 2a  eleteNode;..  /*
3102a 20 53 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 72   Statements to r
3102b 65 61 64 2f 77 72 69 74 65 2f 64 65 6c 65 74 65  ead/write/delete
3102c 20 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 78   a record from x
3102d 78 78 5f 72 6f 77 69 64 20 2a 2f 0a 20 20 73 71  xx_rowid */.  sq
3102e 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 52 65 61  lite3_stmt *pRea
3102f 64 52 6f 77 69 64 3b 0a 20 20 73 71 6c 69 74 65  dRowid;.  sqlite
31030 33 5f 73 74 6d 74 20 2a 70 57 72 69 74 65 52 6f  3_stmt *pWriteRo
31031 77 69 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  wid;.  sqlite3_s
31032 74 6d 74 20 2a 70 44 65 6c 65 74 65 52 6f 77 69  tmt *pDeleteRowi
31033 64 3b 0a 0a 20 20 2f 2a 20 53 74 61 74 65 6d 65  d;..  /* Stateme
31034 6e 74 73 20 74 6f 20 72 65 61 64 2f 77 72 69 74  nts to read/writ
31035 65 2f 64 65 6c 65 74 65 20 61 20 72 65 63 6f 72  e/delete a recor
31036 64 20 66 72 6f 6d 20 78 78 78 5f 70 61 72 65 6e  d from xxx_paren
31037 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  t */.  sqlite3_s
31038 74 6d 74 20 2a 70 52 65 61 64 50 61 72 65 6e 74  tmt *pReadParent
31039 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  ;.  sqlite3_stmt
3103a 20 2a 70 57 72 69 74 65 50 61 72 65 6e 74 3b 0a   *pWriteParent;.
3103b 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
3103c 70 44 65 6c 65 74 65 50 61 72 65 6e 74 3b 0a 0a  pDeleteParent;..
3103d 20 20 69 6e 74 20 65 43 6f 6f 72 64 54 79 70 65    int eCoordType
3103e 3b 0a 7d 3b 0a 0a 2f 2a 20 50 6f 73 73 69 62 6c  ;.};../* Possibl
3103f 65 20 76 61 6c 75 65 73 20 66 6f 72 20 65 43 6f  e values for eCo
31040 6f 72 64 54 79 70 65 3a 20 2a 2f 0a 23 64 65 66  ordType: */.#def
31041 69 6e 65 20 52 54 52 45 45 5f 43 4f 4f 52 44 5f  ine RTREE_COORD_
31042 52 45 41 4c 33 32 20 30 0a 23 64 65 66 69 6e 65  REAL32 0.#define
31043 20 52 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54   RTREE_COORD_INT
31044 33 32 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  32  1../*.** The
31045 20 6d 69 6e 69 6d 75 6d 20 6e 75 6d 62 65 72 20   minimum number 
31046 6f 66 20 63 65 6c 6c 73 20 61 6c 6c 6f 77 65 64  of cells allowed
31047 20 66 6f 72 20 61 20 6e 6f 64 65 20 69 73 20 61   for a node is a
31048 20 74 68 69 72 64 20 6f 66 20 74 68 65 20 0a 2a   third of the .*
31049 2a 20 6d 61 78 69 6d 75 6d 2e 20 49 6e 20 47 75  * maximum. In Gu
3104a 74 6d 61 6e 27 73 20 6e 6f 74 61 74 69 6f 6e 3a  tman's notation:
3104b 0a 2a 2a 0a 2a 2a 20 20 20 20 20 6d 20 3d 20 4d  .**.**     m = M
3104c 2f 33 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 52  /3.**.** If an R
3104d 2a 2d 74 72 65 65 20 22 52 65 69 6e 73 65 72 74  *-tree "Reinsert
3104e 22 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 72  " operation is r
3104f 65 71 75 69 72 65 64 2c 20 74 68 65 20 73 61 6d  equired, the sam
31050 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63  e number of.** c
31051 65 6c 6c 73 20 61 72 65 20 72 65 6d 6f 76 65 64  ells are removed
31052 20 66 72 6f 6d 20 74 68 65 20 6f 76 65 72 66 75   from the overfu
31053 6c 6c 20 6e 6f 64 65 20 61 6e 64 20 72 65 69 6e  ll node and rein
31054 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20  serted into the 
31055 74 72 65 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  tree..*/.#define
31056 20 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28   RTREE_MINCELLS(
31057 70 29 20 28 28 28 28 70 29 2d 3e 69 4e 6f 64 65  p) ((((p)->iNode
31058 53 69 7a 65 2d 34 29 2f 28 70 29 2d 3e 6e 42 79  Size-4)/(p)->nBy
31059 74 65 73 50 65 72 43 65 6c 6c 29 2f 33 29 0a 23  tesPerCell)/3).#
3105a 64 65 66 69 6e 65 20 52 54 52 45 45 5f 52 45 49  define RTREE_REI
3105b 4e 53 45 52 54 28 70 29 20 52 54 52 45 45 5f 4d  NSERT(p) RTREE_M
3105c 49 4e 43 45 4c 4c 53 28 70 29 0a 23 64 65 66 69  INCELLS(p).#defi
3105d 6e 65 20 52 54 52 45 45 5f 4d 41 58 43 45 4c 4c  ne RTREE_MAXCELL
3105e 53 20 35 31 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20  S 51../* .** An 
3105f 72 74 72 65 65 20 63 75 72 73 6f 72 20 6f 62 6a  rtree cursor obj
31060 65 63 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52  ect..*/.struct R
31061 74 72 65 65 43 75 72 73 6f 72 20 7b 0a 20 20 73  treeCursor {.  s
31062 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
31063 6f 72 20 62 61 73 65 3b 0a 20 20 52 74 72 65 65  or base;.  Rtree
31064 4e 6f 64 65 20 2a 70 4e 6f 64 65 3b 20 20 20 20  Node *pNode;    
31065 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
31066 4e 6f 64 65 20 63 75 72 73 6f 72 20 69 73 20 63  Node cursor is c
31067 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e  urrently pointin
31068 67 20 61 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43  g at */.  int iC
31069 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  ell;            
3106a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
3106b 6e 64 65 78 20 6f 66 20 63 75 72 72 65 6e 74 20  ndex of current 
3106c 63 65 6c 6c 20 69 6e 20 70 4e 6f 64 65 20 2a 2f  cell in pNode */
3106d 0a 20 20 69 6e 74 20 69 53 74 72 61 74 65 67 79  .  int iStrategy
3106e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3106f 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20       /* Copy of 
31070 69 64 78 4e 75 6d 20 73 65 61 72 63 68 20 70 61  idxNum search pa
31071 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 69 6e 74  rameter */.  int
31072 20 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20   nConstraint;   
31073 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
31074 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
31075 69 65 73 20 69 6e 20 61 43 6f 6e 73 74 72 61 69  ies in aConstrai
31076 6e 74 20 2a 2f 0a 20 20 52 74 72 65 65 43 6f 6e  nt */.  RtreeCon
31077 73 74 72 61 69 6e 74 20 2a 61 43 6f 6e 73 74 72  straint *aConstr
31078 61 69 6e 74 3b 20 20 20 20 20 2f 2a 20 53 65 61  aint;     /* Sea
31079 72 63 68 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e  rch constraints.
3107a 20 2a 2f 0a 7d 3b 0a 0a 75 6e 69 6f 6e 20 52 74   */.};..union Rt
3107b 72 65 65 43 6f 6f 72 64 20 7b 0a 20 20 66 6c 6f  reeCoord {.  flo
3107c 61 74 20 66 3b 0a 20 20 69 6e 74 20 69 3b 0a 7d  at f;.  int i;.}
3107d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67  ;../*.** The arg
3107e 75 6d 65 6e 74 20 69 73 20 61 6e 20 52 74 72 65  ument is an Rtre
3107f 65 43 6f 6f 72 64 2e 20 52 65 74 75 72 6e 20 74  eCoord. Return t
31080 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20  he value stored 
31081 77 69 74 68 69 6e 20 74 68 65 20 52 74 72 65 65  within the Rtree
31082 43 6f 6f 72 64 0a 2a 2a 20 66 6f 72 6d 61 74 74  Coord.** formatt
31083 65 64 20 61 73 20 61 20 64 6f 75 62 6c 65 2e 20  ed as a double. 
31084 54 68 69 73 20 6d 61 63 72 6f 20 61 73 73 75 6d  This macro assum
31085 65 73 20 74 68 61 74 20 6c 6f 63 61 6c 20 76 61  es that local va
31086 72 69 61 62 6c 65 20 70 52 74 72 65 65 20 70 6f  riable pRtree po
31087 69 6e 74 73 0a 2a 2a 20 74 6f 20 74 68 65 20 52  ints.** to the R
31088 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 61  tree structure a
31089 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
3108a 68 65 20 52 74 72 65 65 43 6f 6f 72 64 2e 0a 2a  he RtreeCoord..*
3108b 2f 0a 23 64 65 66 69 6e 65 20 44 43 4f 4f 52 44  /.#define DCOORD
3108c 28 63 6f 6f 72 64 29 20 28 20 20 20 20 20 20 20  (coord) (       
3108d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3108e 20 20 20 20 5c 0a 20 20 28 70 52 74 72 65 65 2d      \.  (pRtree-
3108f 3e 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52  >eCoordType==RTR
31090 45 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32 29  EE_COORD_REAL32)
31091 20 3f 20 20 20 20 20 20 5c 0a 20 20 20 20 28 28   ?      \.    ((
31092 64 6f 75 62 6c 65 29 63 6f 6f 72 64 2e 66 29 20  double)coord.f) 
31093 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  :               
31094 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
31095 20 20 28 28 64 6f 75 62 6c 65 29 63 6f 6f 72 64    ((double)coord
31096 2e 69 29 20 20 20 20 20 20 20 20 20 20 20 20 20  .i)             
31097 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31098 5c 0a 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 65 61  \.)../*.** A sea
31099 72 63 68 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a  rch constraint..
3109a 2a 2f 0a 73 74 72 75 63 74 20 52 74 72 65 65 43  */.struct RtreeC
3109b 6f 6e 73 74 72 61 69 6e 74 20 7b 0a 20 20 69 6e  onstraint {.  in
3109c 74 20 69 43 6f 6f 72 64 3b 20 20 20 20 20 20 20  t iCoord;       
3109d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3109e 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6e 73  /* Index of cons
3109f 74 72 61 69 6e 65 64 20 63 6f 6f 72 64 69 6e 61  trained coordina
310a0 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b 20  te */.  int op; 
310a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
310a2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e            /* Con
310a3 73 74 72 61 69 6e 69 6e 67 20 6f 70 65 72 61 74  straining operat
310a4 69 6f 6e 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20  ion */.  double 
310a5 72 56 61 6c 75 65 3b 20 20 20 20 20 20 20 20 20  rValue;         
310a6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
310a7 6e 73 74 72 61 69 6e 74 20 76 61 6c 75 65 2e 20  nstraint value. 
310a8 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 50 6f 73 73 69 62  */.};../* Possib
310a9 6c 65 20 76 61 6c 75 65 73 20 66 6f 72 20 52 74  le values for Rt
310aa 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 2e 6f 70  reeConstraint.op
310ab 20 2a 2f 0a 23 64 65 66 69 6e 65 20 52 54 52 45   */.#define RTRE
310ac 45 5f 45 51 20 30 78 34 31 0a 23 64 65 66 69 6e  E_EQ 0x41.#defin
310ad 65 20 52 54 52 45 45 5f 4c 45 20 30 78 34 32 0a  e RTREE_LE 0x42.
310ae 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f 4c 54  #define RTREE_LT
310af 20 30 78 34 33 0a 23 64 65 66 69 6e 65 20 52 54   0x43.#define RT
310b0 52 45 45 5f 47 45 20 30 78 34 34 0a 23 64 65 66  REE_GE 0x44.#def
310b1 69 6e 65 20 52 54 52 45 45 5f 47 54 20 30 78 34  ine RTREE_GT 0x4
310b2 35 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 72 74 72  5../* .** An rtr
310b3 65 65 20 73 74 72 75 63 74 75 72 65 20 6e 6f 64  ee structure nod
310b4 65 2e 0a 2a 2a 0a 2a 2a 20 44 61 74 61 20 66 6f  e..**.** Data fo
310b5 72 6d 61 74 20 28 52 74 72 65 65 4e 6f 64 65 2e  rmat (RtreeNode.
310b6 7a 44 61 74 61 29 3a 0a 2a 2a 0a 2a 2a 20 20 20  zData):.**.**   
310b7 31 2e 20 49 66 20 74 68 65 20 6e 6f 64 65 20 69  1. If the node i
310b8 73 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20  s the root node 
310b9 28 6e 6f 64 65 20 31 29 2c 20 74 68 65 6e 20 74  (node 1), then t
310ba 68 65 20 66 69 72 73 74 20 32 20 62 79 74 65 73  he first 2 bytes
310bb 0a 2a 2a 20 20 20 20 20 20 6f 66 20 74 68 65 20  .**      of the 
310bc 6e 6f 64 65 20 63 6f 6e 74 61 69 6e 20 74 68 65  node contain the
310bd 20 74 72 65 65 20 64 65 70 74 68 20 61 73 20 61   tree depth as a
310be 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65   big-endian inte
310bf 67 65 72 2e 0a 2a 2a 20 20 20 20 20 20 46 6f 72  ger..**      For
310c0 20 6e 6f 6e 2d 72 6f 6f 74 20 6e 6f 64 65 73 2c   non-root nodes,
310c1 20 74 68 65 20 66 69 72 73 74 20 32 20 62 79 74   the first 2 byt
310c2 65 73 20 61 72 65 20 6c 65 66 74 20 75 6e 75 73  es are left unus
310c3 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 54  ed..**.**   2. T
310c4 68 65 20 6e 65 78 74 20 32 20 62 79 74 65 73 20  he next 2 bytes 
310c5 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e 75 6d 62  contain the numb
310c6 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 63 75  er of entries cu
310c7 72 72 65 6e 74 6c 79 20 0a 2a 2a 20 20 20 20 20  rrently .**     
310c8 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 6e   stored in the n
310c9 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20  ode..**.**   3. 
310ca 54 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66  The remainder of
310cb 20 74 68 65 20 6e 6f 64 65 20 63 6f 6e 74 61 69   the node contai
310cc 6e 73 20 74 68 65 20 6e 6f 64 65 20 65 6e 74 72  ns the node entr
310cd 69 65 73 2e 20 45 61 63 68 20 65 6e 74 72 79 0a  ies. Each entry.
310ce 2a 2a 20 20 20 20 20 20 63 6f 6e 73 69 73 74 73  **      consists
310cf 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 38 2d 62   of a single 8-b
310d0 79 74 65 20 69 6e 74 65 67 65 72 20 66 6f 6c 6c  yte integer foll
310d1 6f 77 65 64 20 62 79 20 61 6e 20 65 76 65 6e 20  owed by an even 
310d2 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 20 20 20 6f  number.**      o
310d3 66 20 34 2d 62 79 74 65 20 63 6f 6f 72 64 69 6e  f 4-byte coordin
310d4 61 74 65 73 2e 20 46 6f 72 20 6c 65 61 66 20 6e  ates. For leaf n
310d5 6f 64 65 73 20 74 68 65 20 69 6e 74 65 67 65 72  odes the integer
310d6 20 69 73 20 74 68 65 20 72 6f 77 69 64 0a 2a 2a   is the rowid.**
310d7 20 20 20 20 20 20 6f 66 20 61 20 72 65 63 6f 72        of a recor
310d8 64 2e 20 46 6f 72 20 69 6e 74 65 72 6e 61 6c 20  d. For internal 
310d9 6e 6f 64 65 73 20 69 74 20 69 73 20 74 68 65 20  nodes it is the 
310da 6e 6f 64 65 20 6e 75 6d 62 65 72 20 6f 66 20 61  node number of a
310db 0a 2a 2a 20 20 20 20 20 20 63 68 69 6c 64 20 70  .**      child p
310dc 61 67 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52  age..*/.struct R
310dd 74 72 65 65 4e 6f 64 65 20 7b 0a 20 20 52 74 72  treeNode {.  Rtr
310de 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 3b  eeNode *pParent;
310df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
310e0 2a 20 50 61 72 65 6e 74 20 6e 6f 64 65 20 2a 2f  * Parent node */
310e1 0a 20 20 69 36 34 20 69 4e 6f 64 65 3b 0a 20 20  .  i64 iNode;.  
310e2 69 6e 74 20 6e 52 65 66 3b 0a 20 20 69 6e 74 20  int nRef;.  int 
310e3 69 73 44 69 72 74 79 3b 0a 20 20 75 38 20 2a 7a  isDirty;.  u8 *z
310e4 44 61 74 61 3b 0a 20 20 52 74 72 65 65 4e 6f 64  Data;.  RtreeNod
310e5 65 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20  e *pNext;       
310e6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78            /* Nex
310e7 74 20 6e 6f 64 65 20 69 6e 20 74 68 69 73 20 68  t node in this h
310e8 61 73 68 20 63 68 61 69 6e 20 2a 2f 0a 7d 3b 0a  ash chain */.};.
310e9 23 64 65 66 69 6e 65 20 4e 43 45 4c 4c 28 70 4e  #define NCELL(pN
310ea 6f 64 65 29 20 72 65 61 64 49 6e 74 31 36 28 26  ode) readInt16(&
310eb 28 70 4e 6f 64 65 29 2d 3e 7a 44 61 74 61 5b 32  (pNode)->zData[2
310ec 5d 29 0a 0a 2f 2a 20 0a 2a 2a 20 53 74 72 75 63  ])../* .** Struc
310ed 74 75 72 65 20 74 6f 20 73 74 6f 72 65 20 61 20  ture to store a 
310ee 64 65 73 65 72 69 61 6c 69 7a 65 64 20 72 74 72  deserialized rtr
310ef 65 65 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 73 74  ee record..*/.st
310f0 72 75 63 74 20 52 74 72 65 65 43 65 6c 6c 20 7b  ruct RtreeCell {
310f1 0a 20 20 69 36 34 20 69 52 6f 77 69 64 3b 0a 20  .  i64 iRowid;. 
310f2 20 52 74 72 65 65 43 6f 6f 72 64 20 61 43 6f 6f   RtreeCoord aCoo
310f3 72 64 5b 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d  rd[RTREE_MAX_DIM
310f4 45 4e 53 49 4f 4e 53 2a 32 5d 3b 0a 7d 3b 0a 0a  ENSIONS*2];.};..
310f5 23 64 65 66 69 6e 65 20 4d 41 58 28 78 2c 79 29  #define MAX(x,y)
310f6 20 28 28 78 29 20 3c 20 28 79 29 20 3f 20 28 79   ((x) < (y) ? (y
310f7 29 20 3a 20 28 78 29 29 0a 23 64 65 66 69 6e 65  ) : (x)).#define
310f8 20 4d 49 4e 28 78 2c 79 29 20 28 28 78 29 20 3e   MIN(x,y) ((x) >
310f9 20 28 79 29 20 3f 20 28 79 29 20 3a 20 28 78 29   (y) ? (y) : (x)
310fa 29 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f  )../*.** Functio
310fb 6e 73 20 74 6f 20 64 65 73 65 72 69 61 6c 69 7a  ns to deserializ
310fc 65 20 61 20 31 36 20 62 69 74 20 69 6e 74 65 67  e a 16 bit integ
310fd 65 72 2c 20 33 32 20 62 69 74 20 72 65 61 6c 20  er, 32 bit real 
310fe 6e 75 6d 62 65 72 20 61 6e 64 0a 2a 2a 20 36 34  number and.** 64
310ff 20 62 69 74 20 69 6e 74 65 67 65 72 2e 20 54 68   bit integer. Th
31100 65 20 64 65 73 65 72 69 61 6c 69 7a 65 64 20 76  e deserialized v
31101 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64  alue is returned
31102 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
31103 72 65 61 64 49 6e 74 31 36 28 75 38 20 2a 70 29  readInt16(u8 *p)
31104 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 5b 30 5d  {.  return (p[0]
31105 3c 3c 38 29 20 2b 20 70 5b 31 5d 3b 0a 7d 0a 73  <<8) + p[1];.}.s
31106 74 61 74 69 63 20 76 6f 69 64 20 72 65 61 64 43  tatic void readC
31107 6f 6f 72 64 28 75 38 20 2a 70 2c 20 52 74 72 65  oord(u8 *p, Rtre
31108 65 43 6f 6f 72 64 20 2a 70 43 6f 6f 72 64 29 7b  eCoord *pCoord){
31109 0a 20 20 75 33 32 20 69 20 3d 20 28 0a 20 20 20  .  u32 i = (.   
3110a 20 28 28 28 75 33 32 29 70 5b 30 5d 29 20 3c 3c   (((u32)p[0]) <<
3110b 20 32 34 29 20 2b 20 0a 20 20 20 20 28 28 28 75   24) + .    (((u
3110c 33 32 29 70 5b 31 5d 29 20 3c 3c 20 31 36 29 20  32)p[1]) << 16) 
3110d 2b 20 0a 20 20 20 20 28 28 28 75 33 32 29 70 5b  + .    (((u32)p[
3110e 32 5d 29 20 3c 3c 20 20 38 29 20 2b 20 0a 20 20  2]) <<  8) + .  
3110f 20 20 28 28 28 75 33 32 29 70 5b 33 5d 29 20 3c    (((u32)p[3]) <
31110 3c 20 20 30 29 0a 20 20 29 3b 0a 20 20 2a 28 75  <  0).  );.  *(u
31111 33 32 20 2a 29 70 43 6f 6f 72 64 20 3d 20 69 3b  32 *)pCoord = i;
31112 0a 7d 0a 73 74 61 74 69 63 20 69 36 34 20 72 65  .}.static i64 re
31113 61 64 49 6e 74 36 34 28 75 38 20 2a 70 29 7b 0a  adInt64(u8 *p){.
31114 20 20 72 65 74 75 72 6e 20 28 0a 20 20 20 20 28    return (.    (
31115 28 28 69 36 34 29 70 5b 30 5d 29 20 3c 3c 20 35  ((i64)p[0]) << 5
31116 36 29 20 2b 20 0a 20 20 20 20 28 28 28 69 36 34  6) + .    (((i64
31117 29 70 5b 31 5d 29 20 3c 3c 20 34 38 29 20 2b 20  )p[1]) << 48) + 
31118 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 32 5d  .    (((i64)p[2]
31119 29 20 3c 3c 20 34 30 29 20 2b 20 0a 20 20 20 20  ) << 40) + .    
3111a 28 28 28 69 36 34 29 70 5b 33 5d 29 20 3c 3c 20  (((i64)p[3]) << 
3111b 33 32 29 20 2b 20 0a 20 20 20 20 28 28 28 69 36  32) + .    (((i6
3111c 34 29 70 5b 34 5d 29 20 3c 3c 20 32 34 29 20 2b  4)p[4]) << 24) +
3111d 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 35   .    (((i64)p[5
3111e 5d 29 20 3c 3c 20 31 36 29 20 2b 20 0a 20 20 20  ]) << 16) + .   
3111f 20 28 28 28 69 36 34 29 70 5b 36 5d 29 20 3c 3c   (((i64)p[6]) <<
31120 20 20 38 29 20 2b 20 0a 20 20 20 20 28 28 28 69    8) + .    (((i
31121 36 34 29 70 5b 37 5d 29 20 3c 3c 20 20 30 29 0a  64)p[7]) <<  0).
31122 20 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 75    );.}../*.** Fu
31123 6e 63 74 69 6f 6e 73 20 74 6f 20 73 65 72 69 61  nctions to seria
31124 6c 69 7a 65 20 61 20 31 36 20 62 69 74 20 69 6e  lize a 16 bit in
31125 74 65 67 65 72 2c 20 33 32 20 62 69 74 20 72 65  teger, 32 bit re
31126 61 6c 20 6e 75 6d 62 65 72 20 61 6e 64 0a 2a 2a  al number and.**
31127 20 36 34 20 62 69 74 20 69 6e 74 65 67 65 72 2e   64 bit integer.
31128 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   The value retur
31129 6e 65 64 20 69 73 20 74 68 65 20 6e 75 6d 62 65  ned is the numbe
3112a 72 20 6f 66 20 62 79 74 65 73 20 77 72 69 74 74  r of bytes writt
3112b 65 6e 0a 2a 2a 20 74 6f 20 74 68 65 20 61 72 67  en.** to the arg
3112c 75 6d 65 6e 74 20 62 75 66 66 65 72 20 28 61 6c  ument buffer (al
3112d 77 61 79 73 20 32 2c 20 34 20 61 6e 64 20 38 20  ways 2, 4 and 8 
3112e 72 65 73 70 65 63 74 69 76 65 6c 79 29 2e 0a 2a  respectively)..*
3112f 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72 69  /.static int wri
31130 74 65 49 6e 74 31 36 28 75 38 20 2a 70 2c 20 69  teInt16(u8 *p, i
31131 6e 74 20 69 29 7b 0a 20 20 70 5b 30 5d 20 3d 20  nt i){.  p[0] = 
31132 28 69 3e 3e 20 38 29 26 30 78 46 46 3b 0a 20 20  (i>> 8)&0xFF;.  
31133 70 5b 31 5d 20 3d 20 28 69 3e 3e 20 30 29 26 30  p[1] = (i>> 0)&0
31134 78 46 46 3b 0a 20 20 72 65 74 75 72 6e 20 32 3b  xFF;.  return 2;
31135 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72  .}.static int wr
31136 69 74 65 43 6f 6f 72 64 28 75 38 20 2a 70 2c 20  iteCoord(u8 *p, 
31137 52 74 72 65 65 43 6f 6f 72 64 20 2a 70 43 6f 6f  RtreeCoord *pCoo
31138 72 64 29 7b 0a 20 20 75 33 32 20 69 3b 0a 20 20  rd){.  u32 i;.  
31139 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 52  assert( sizeof(R
3113a 74 72 65 65 43 6f 6f 72 64 29 3d 3d 34 20 29 3b  treeCoord)==4 );
3113b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f  .  assert( sizeo
3113c 66 28 75 33 32 29 3d 3d 34 20 29 3b 0a 20 20 69  f(u32)==4 );.  i
3113d 20 3d 20 2a 28 75 33 32 20 2a 29 70 43 6f 6f 72   = *(u32 *)pCoor
3113e 64 3b 0a 20 20 70 5b 30 5d 20 3d 20 28 69 3e 3e  d;.  p[0] = (i>>
3113f 32 34 29 26 30 78 46 46 3b 0a 20 20 70 5b 31 5d  24)&0xFF;.  p[1]
31140 20 3d 20 28 69 3e 3e 31 36 29 26 30 78 46 46 3b   = (i>>16)&0xFF;
31141 0a 20 20 70 5b 32 5d 20 3d 20 28 69 3e 3e 20 38  .  p[2] = (i>> 8
31142 29 26 30 78 46 46 3b 0a 20 20 70 5b 33 5d 20 3d  )&0xFF;.  p[3] =
31143 20 28 69 3e 3e 20 30 29 26 30 78 46 46 3b 0a 20   (i>> 0)&0xFF;. 
31144 20 72 65 74 75 72 6e 20 34 3b 0a 7d 0a 73 74 61   return 4;.}.sta
31145 74 69 63 20 69 6e 74 20 77 72 69 74 65 49 6e 74  tic int writeInt
31146 36 34 28 75 38 20 2a 70 2c 20 69 36 34 20 69 29  64(u8 *p, i64 i)
31147 7b 0a 20 20 70 5b 30 5d 20 3d 20 28 69 3e 3e 35  {.  p[0] = (i>>5
31148 36 29 26 30 78 46 46 3b 0a 20 20 70 5b 31 5d 20  6)&0xFF;.  p[1] 
31149 3d 20 28 69 3e 3e 34 38 29 26 30 78 46 46 3b 0a  = (i>>48)&0xFF;.
3114a 20 20 70 5b 32 5d 20 3d 20 28 69 3e 3e 34 30 29    p[2] = (i>>40)
3114b 26 30 78 46 46 3b 0a 20 20 70 5b 33 5d 20 3d 20  &0xFF;.  p[3] = 
3114c 28 69 3e 3e 33 32 29 26 30 78 46 46 3b 0a 20 20  (i>>32)&0xFF;.  
3114d 70 5b 34 5d 20 3d 20 28 69 3e 3e 32 34 29 26 30  p[4] = (i>>24)&0
3114e 78 46 46 3b 0a 20 20 70 5b 35 5d 20 3d 20 28 69  xFF;.  p[5] = (i
3114f 3e 3e 31 36 29 26 30 78 46 46 3b 0a 20 20 70 5b  >>16)&0xFF;.  p[
31150 36 5d 20 3d 20 28 69 3e 3e 20 38 29 26 30 78 46  6] = (i>> 8)&0xF
31151 46 3b 0a 20 20 70 5b 37 5d 20 3d 20 28 69 3e 3e  F;.  p[7] = (i>>
31152 20 30 29 26 30 78 46 46 3b 0a 20 20 72 65 74 75   0)&0xFF;.  retu
31153 72 6e 20 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  rn 8;.}../*.** I
31154 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66  ncrement the ref
31155 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 66 20  erence count of 
31156 6e 6f 64 65 20 70 2e 0a 2a 2f 0a 73 74 61 74 69  node p..*/.stati
31157 63 20 76 6f 69 64 20 6e 6f 64 65 52 65 66 65 72  c void nodeRefer
31158 65 6e 63 65 28 52 74 72 65 65 4e 6f 64 65 20 2a  ence(RtreeNode *
31159 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  p){.  if( p ){. 
3115a 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20     p->nRef++;.  
3115b 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72  }.}../*.** Clear
3115c 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
3115d 6e 6f 64 65 20 70 20 28 73 65 74 20 61 6c 6c 20  node p (set all 
3115e 62 79 74 65 73 20 74 6f 20 30 78 30 30 29 2e 0a  bytes to 0x00)..
3115f 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e  */.static void n
31160 6f 64 65 5a 65 72 6f 28 52 74 72 65 65 20 2a 70  odeZero(Rtree *p
31161 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
31162 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b   *p){.  if( p ){
31163 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 70 2d 3e  .    memset(&p->
31164 7a 44 61 74 61 5b 32 5d 2c 20 30 2c 20 70 52 74  zData[2], 0, pRt
31165 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 2d 32  ree->iNodeSize-2
31166 29 3b 0a 20 20 20 20 70 2d 3e 69 73 44 69 72 74  );.    p->isDirt
31167 79 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  y = 1;.  }.}../*
31168 0a 2a 2a 20 47 69 76 65 6e 20 61 20 6e 6f 64 65  .** Given a node
31169 20 6e 75 6d 62 65 72 20 69 4e 6f 64 65 2c 20 72   number iNode, r
3116a 65 74 75 72 6e 20 74 68 65 20 63 6f 72 72 65 73  eturn the corres
3116b 70 6f 6e 64 69 6e 67 20 6b 65 79 20 74 6f 20 75  ponding key to u
3116c 73 65 0a 2a 2a 20 69 6e 20 74 68 65 20 52 74 72  se.** in the Rtr
3116d 65 65 2e 61 48 61 73 68 20 74 61 62 6c 65 2e 0a  ee.aHash table..
3116e 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f  */.static int no
3116f 64 65 48 61 73 68 28 69 36 34 20 69 4e 6f 64 65  deHash(i64 iNode
31170 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 0a 20 20  ){.  return (.  
31171 20 20 28 69 4e 6f 64 65 3e 3e 35 36 29 20 5e 20    (iNode>>56) ^ 
31172 28 69 4e 6f 64 65 3e 3e 34 38 29 20 5e 20 28 69  (iNode>>48) ^ (i
31173 4e 6f 64 65 3e 3e 34 30 29 20 5e 20 28 69 4e 6f  Node>>40) ^ (iNo
31174 64 65 3e 3e 33 32 29 20 5e 20 0a 20 20 20 20 28  de>>32) ^ .    (
31175 69 4e 6f 64 65 3e 3e 32 34 29 20 5e 20 28 69 4e  iNode>>24) ^ (iN
31176 6f 64 65 3e 3e 31 36 29 20 5e 20 28 69 4e 6f 64  ode>>16) ^ (iNod
31177 65 3e 3e 20 38 29 20 5e 20 28 69 4e 6f 64 65 3e  e>> 8) ^ (iNode>
31178 3e 20 30 29 0a 20 20 29 20 25 20 48 41 53 48 53  > 0).  ) % HASHS
31179 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65  IZE;.}../*.** Se
3117a 61 72 63 68 20 74 68 65 20 6e 6f 64 65 20 68 61  arch the node ha
3117b 73 68 20 74 61 62 6c 65 20 66 6f 72 20 6e 6f 64  sh table for nod
3117c 65 20 69 4e 6f 64 65 2e 20 49 66 20 66 6f 75 6e  e iNode. If foun
3117d 64 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e  d, return a poin
3117e 74 65 72 0a 2a 2a 20 74 6f 20 69 74 2e 20 4f 74  ter.** to it. Ot
3117f 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e 20  herwise, return 
31180 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52 74 72  0..*/.static Rtr
31181 65 65 4e 6f 64 65 20 2a 6e 6f 64 65 48 61 73 68  eeNode *nodeHash
31182 4c 6f 6f 6b 75 70 28 52 74 72 65 65 20 2a 70 52  Lookup(Rtree *pR
31183 74 72 65 65 2c 20 69 36 34 20 69 4e 6f 64 65 29  tree, i64 iNode)
31184 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  {.  RtreeNode *p
31185 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4e 6f 64  ;.  assert( iNod
31186 65 21 3d 30 20 29 3b 0a 20 20 66 6f 72 28 70 3d  e!=0 );.  for(p=
31187 70 52 74 72 65 65 2d 3e 61 48 61 73 68 5b 6e 6f  pRtree->aHash[no
31188 64 65 48 61 73 68 28 69 4e 6f 64 65 29 5d 3b 20  deHash(iNode)]; 
31189 70 20 26 26 20 70 2d 3e 69 4e 6f 64 65 21 3d 69  p && p->iNode!=i
3118a 4e 6f 64 65 3b 20 70 3d 70 2d 3e 70 4e 65 78 74  Node; p=p->pNext
3118b 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d  );.  return p;.}
3118c 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 6e 6f 64 65  ../*.** Add node
3118d 20 70 4e 6f 64 65 20 74 6f 20 74 68 65 20 6e 6f   pNode to the no
3118e 64 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a  de hash table..*
3118f 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f  /.static void no
31190 64 65 48 61 73 68 49 6e 73 65 72 74 28 52 74 72  deHashInsert(Rtr
31191 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
31192 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a 20  eNode *pNode){. 
31193 20 69 66 28 20 70 4e 6f 64 65 20 29 7b 0a 20 20   if( pNode ){.  
31194 20 20 69 6e 74 20 69 48 61 73 68 3b 0a 20 20 20    int iHash;.   
31195 20 61 73 73 65 72 74 28 20 70 4e 6f 64 65 2d 3e   assert( pNode->
31196 70 4e 65 78 74 3d 3d 30 20 29 3b 0a 20 20 20 20  pNext==0 );.    
31197 69 48 61 73 68 20 3d 20 6e 6f 64 65 48 61 73 68  iHash = nodeHash
31198 28 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a  (pNode->iNode);.
31199 20 20 20 20 70 4e 6f 64 65 2d 3e 70 4e 65 78 74      pNode->pNext
3119a 20 3d 20 70 52 74 72 65 65 2d 3e 61 48 61 73 68   = pRtree->aHash
3119b 5b 69 48 61 73 68 5d 3b 0a 20 20 20 20 70 52 74  [iHash];.    pRt
3119c 72 65 65 2d 3e 61 48 61 73 68 5b 69 48 61 73 68  ree->aHash[iHash
3119d 5d 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 7d 0a 7d  ] = pNode;.  }.}
3119e 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 6e  ../*.** Remove n
3119f 6f 64 65 20 70 4e 6f 64 65 20 66 72 6f 6d 20 74  ode pNode from t
311a0 68 65 20 6e 6f 64 65 20 68 61 73 68 20 74 61 62  he node hash tab
311a1 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  le..*/.static vo
311a2 69 64 20 6e 6f 64 65 48 61 73 68 44 65 6c 65 74  id nodeHashDelet
311a3 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  e(Rtree *pRtree,
311a4 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
311a5 65 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  e){.  RtreeNode 
311a6 2a 2a 70 70 3b 0a 20 20 69 66 28 20 70 4e 6f 64  **pp;.  if( pNod
311a7 65 2d 3e 69 4e 6f 64 65 21 3d 30 20 29 7b 0a 20  e->iNode!=0 ){. 
311a8 20 20 20 70 70 20 3d 20 26 70 52 74 72 65 65 2d     pp = &pRtree-
311a9 3e 61 48 61 73 68 5b 6e 6f 64 65 48 61 73 68 28  >aHash[nodeHash(
311aa 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 5d 3b 0a  pNode->iNode)];.
311ab 20 20 20 20 66 6f 72 28 20 3b 20 28 2a 70 70 29      for( ; (*pp)
311ac 21 3d 70 4e 6f 64 65 3b 20 70 70 20 3d 20 26 28  !=pNode; pp = &(
311ad 2a 70 70 29 2d 3e 70 4e 65 78 74 29 7b 20 61 73  *pp)->pNext){ as
311ae 73 65 72 74 28 2a 70 70 29 3b 20 7d 0a 20 20 20  sert(*pp); }.   
311af 20 2a 70 70 20 3d 20 70 4e 6f 64 65 2d 3e 70 4e   *pp = pNode->pN
311b0 65 78 74 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e  ext;.    pNode->
311b1 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 7d 0a 7d  pNext = 0;.  }.}
311b2 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ../*.** Allocate
311b3 20 61 6e 64 20 72 65 74 75 72 6e 20 6e 65 77 20   and return new 
311b4 72 2d 74 72 65 65 20 6e 6f 64 65 2e 20 49 6e 69  r-tree node. Ini
311b5 74 69 61 6c 6c 79 2c 20 28 52 74 72 65 65 4e 6f  tially, (RtreeNo
311b6 64 65 2e 69 4e 6f 64 65 3d 3d 30 29 2c 0a 2a 2a  de.iNode==0),.**
311b7 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74   indicating that
311b8 20 6e 6f 64 65 20 68 61 73 20 6e 6f 74 20 79 65   node has not ye
311b9 74 20 62 65 65 6e 20 61 73 73 69 67 6e 65 64 20  t been assigned 
311ba 61 20 6e 6f 64 65 20 6e 75 6d 62 65 72 2e 20 49  a node number. I
311bb 74 20 69 73 0a 2a 2a 20 61 73 73 69 67 6e 65 64  t is.** assigned
311bc 20 61 20 6e 6f 64 65 20 6e 75 6d 62 65 72 20 77   a node number w
311bd 68 65 6e 20 6e 6f 64 65 57 72 69 74 65 28 29 20  hen nodeWrite() 
311be 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 77 72 69  is called to wri
311bf 74 65 20 74 68 65 0a 2a 2a 20 6e 6f 64 65 20 63  te the.** node c
311c0 6f 6e 74 65 6e 74 73 20 6f 75 74 20 74 6f 20 74  ontents out to t
311c1 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a  he database..*/.
311c2 73 74 61 74 69 63 20 52 74 72 65 65 4e 6f 64 65  static RtreeNode
311c3 20 2a 6e 6f 64 65 4e 65 77 28 52 74 72 65 65 20   *nodeNew(Rtree 
311c4 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f  *pRtree, RtreeNo
311c5 64 65 20 2a 70 50 61 72 65 6e 74 2c 20 69 6e 74  de *pParent, int
311c6 20 7a 65 72 6f 29 7b 0a 20 20 52 74 72 65 65 4e   zero){.  RtreeN
311c7 6f 64 65 20 2a 70 4e 6f 64 65 3b 0a 20 20 70 4e  ode *pNode;.  pN
311c8 6f 64 65 20 3d 20 28 52 74 72 65 65 4e 6f 64 65  ode = (RtreeNode
311c9 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   *)sqlite3_mallo
311ca 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65 4e 6f  c(sizeof(RtreeNo
311cb 64 65 29 20 2b 20 70 52 74 72 65 65 2d 3e 69 4e  de) + pRtree->iN
311cc 6f 64 65 53 69 7a 65 29 3b 0a 20 20 69 66 28 20  odeSize);.  if( 
311cd 70 4e 6f 64 65 20 29 7b 0a 20 20 20 20 6d 65 6d  pNode ){.    mem
311ce 73 65 74 28 70 4e 6f 64 65 2c 20 30 2c 20 73 69  set(pNode, 0, si
311cf 7a 65 6f 66 28 52 74 72 65 65 4e 6f 64 65 29 20  zeof(RtreeNode) 
311d0 2b 20 28 7a 65 72 6f 3f 70 52 74 72 65 65 2d 3e  + (zero?pRtree->
311d1 69 4e 6f 64 65 53 69 7a 65 3a 30 29 29 3b 0a 20  iNodeSize:0));. 
311d2 20 20 20 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 20     pNode->zData 
311d3 3d 20 28 75 38 20 2a 29 26 70 4e 6f 64 65 5b 31  = (u8 *)&pNode[1
311d4 5d 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 6e 52  ];.    pNode->nR
311d5 65 66 20 3d 20 31 3b 0a 20 20 20 20 70 4e 6f 64  ef = 1;.    pNod
311d6 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 50 61  e->pParent = pPa
311d7 72 65 6e 74 3b 0a 20 20 20 20 70 4e 6f 64 65 2d  rent;.    pNode-
311d8 3e 69 73 44 69 72 74 79 20 3d 20 31 3b 0a 20 20  >isDirty = 1;.  
311d9 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28    nodeReference(
311da 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a 20 20  pParent);.  }.  
311db 72 65 74 75 72 6e 20 70 4e 6f 64 65 3b 0a 7d 0a  return pNode;.}.
311dc 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 61 20  ./*.** Obtain a 
311dd 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 6e 20  reference to an 
311de 72 2d 74 72 65 65 20 6e 6f 64 65 2e 0a 2a 2f 0a  r-tree node..*/.
311df 73 74 61 74 69 63 20 69 6e 74 0a 6e 6f 64 65 41  static int.nodeA
311e0 63 71 75 69 72 65 28 0a 20 20 52 74 72 65 65 20  cquire(.  Rtree 
311e1 2a 70 52 74 72 65 65 2c 20 20 20 20 20 20 20 20  *pRtree,        
311e2 20 20 20 20 20 2f 2a 20 52 2d 74 72 65 65 20 73       /* R-tree s
311e3 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 36  tructure */.  i6
311e4 34 20 69 4e 6f 64 65 2c 20 20 20 20 20 20 20 20  4 iNode,        
311e5 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 64 65           /* Node
311e6 20 6e 75 6d 62 65 72 20 74 6f 20 6c 6f 61 64 20   number to load 
311e7 2a 2f 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  */.  RtreeNode *
311e8 70 50 61 72 65 6e 74 2c 20 20 20 20 20 20 20 20  pParent,        
311e9 2f 2a 20 45 69 74 68 65 72 20 74 68 65 20 70 61  /* Either the pa
311ea 72 65 6e 74 20 6e 6f 64 65 20 6f 72 20 4e 55 4c  rent node or NUL
311eb 4c 20 2a 2f 0a 20 20 52 74 72 65 65 4e 6f 64 65  L */.  RtreeNode
311ec 20 2a 2a 70 70 4e 6f 64 65 20 20 20 20 20 20 20   **ppNode       
311ed 20 20 2f 2a 20 4f 55 54 3a 20 41 63 71 75 69 72    /* OUT: Acquir
311ee 65 64 20 6e 6f 64 65 20 2a 2f 0a 29 7b 0a 20 20  ed node */.){.  
311ef 69 6e 74 20 72 63 3b 0a 20 20 52 74 72 65 65 4e  int rc;.  RtreeN
311f0 6f 64 65 20 2a 70 4e 6f 64 65 3b 0a 0a 20 20 2f  ode *pNode;..  /
311f1 2a 20 43 68 65 63 6b 20 69 66 20 74 68 65 20 72  * Check if the r
311f2 65 71 75 65 73 74 65 64 20 6e 6f 64 65 20 69 73  equested node is
311f3 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
311f4 68 61 73 68 20 74 61 62 6c 65 2e 20 49 66 20 73  hash table. If s
311f5 6f 2c 0a 20 20 2a 2a 20 69 6e 63 72 65 61 73 65  o,.  ** increase
311f6 20 69 74 73 20 72 65 66 65 72 65 6e 63 65 20 63   its reference c
311f7 6f 75 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 20  ount and return 
311f8 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  it..  */.  if( (
311f9 70 4e 6f 64 65 20 3d 20 6e 6f 64 65 48 61 73 68  pNode = nodeHash
311fa 4c 6f 6f 6b 75 70 28 70 52 74 72 65 65 2c 20 69  Lookup(pRtree, i
311fb 4e 6f 64 65 29 29 20 29 7b 0a 20 20 20 20 61 73  Node)) ){.    as
311fc 73 65 72 74 28 20 21 70 50 61 72 65 6e 74 20 7c  sert( !pParent |
311fd 7c 20 21 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e  | !pNode->pParen
311fe 74 20 7c 7c 20 70 4e 6f 64 65 2d 3e 70 50 61 72  t || pNode->pPar
311ff 65 6e 74 3d 3d 70 50 61 72 65 6e 74 20 29 3b 0a  ent==pParent );.
31200 20 20 20 20 69 66 28 20 70 50 61 72 65 6e 74 20      if( pParent 
31201 29 7b 0a 20 20 20 20 20 20 70 4e 6f 64 65 2d 3e  ){.      pNode->
31202 70 50 61 72 65 6e 74 20 3d 20 70 50 61 72 65 6e  pParent = pParen
31203 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 6f  t;.    }.    pNo
31204 64 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20  de->nRef++;.    
31205 2a 70 70 4e 6f 64 65 20 3d 20 70 4e 6f 64 65 3b  *ppNode = pNode;
31206 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
31207 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 70 4e  TE_OK;.  }..  pN
31208 6f 64 65 20 3d 20 28 52 74 72 65 65 4e 6f 64 65  ode = (RtreeNode
31209 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   *)sqlite3_mallo
3120a 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65 4e 6f  c(sizeof(RtreeNo
3120b 64 65 29 20 2b 20 70 52 74 72 65 65 2d 3e 69 4e  de) + pRtree->iN
3120c 6f 64 65 53 69 7a 65 29 3b 0a 20 20 69 66 28 20  odeSize);.  if( 
3120d 21 70 4e 6f 64 65 20 29 7b 0a 20 20 20 20 2a 70  !pNode ){.    *p
3120e 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 20 20 72  pNode = 0;.    r
3120f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
31210 45 4d 3b 0a 20 20 7d 0a 20 20 70 4e 6f 64 65 2d  EM;.  }.  pNode-
31211 3e 70 50 61 72 65 6e 74 20 3d 20 70 50 61 72 65  >pParent = pPare
31212 6e 74 3b 0a 20 20 70 4e 6f 64 65 2d 3e 7a 44 61  nt;.  pNode->zDa
31213 74 61 20 3d 20 28 75 38 20 2a 29 26 70 4e 6f 64  ta = (u8 *)&pNod
31214 65 5b 31 5d 3b 0a 20 20 70 4e 6f 64 65 2d 3e 6e  e[1];.  pNode->n
31215 52 65 66 20 3d 20 31 3b 0a 20 20 70 4e 6f 64 65  Ref = 1;.  pNode
31216 2d 3e 69 4e 6f 64 65 20 3d 20 69 4e 6f 64 65 3b  ->iNode = iNode;
31217 0a 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74  .  pNode->isDirt
31218 79 20 3d 20 30 3b 0a 20 20 70 4e 6f 64 65 2d 3e  y = 0;.  pNode->
31219 70 4e 65 78 74 20 3d 20 30 3b 0a 0a 20 20 73 71  pNext = 0;..  sq
3121a 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
3121b 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f  (pRtree->pReadNo
3121c 64 65 2c 20 31 2c 20 69 4e 6f 64 65 29 3b 0a 20  de, 1, iNode);. 
3121d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74   rc = sqlite3_st
3121e 65 70 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64  ep(pRtree->pRead
3121f 4e 6f 64 65 29 3b 0a 20 20 69 66 28 20 72 63 3d  Node);.  if( rc=
31220 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
31221 20 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 42 6c     const u8 *zBl
31222 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  ob = sqlite3_col
31223 75 6d 6e 5f 62 6c 6f 62 28 70 52 74 72 65 65 2d  umn_blob(pRtree-
31224 3e 70 52 65 61 64 4e 6f 64 65 2c 20 30 29 3b 0a  >pReadNode, 0);.
31225 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 6f 64 65      memcpy(pNode
31226 2d 3e 7a 44 61 74 61 2c 20 7a 42 6c 6f 62 2c 20  ->zData, zBlob, 
31227 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a  pRtree->iNodeSiz
31228 65 29 3b 0a 20 20 20 20 6e 6f 64 65 52 65 66 65  e);.    nodeRefe
31229 72 65 6e 63 65 28 70 50 61 72 65 6e 74 29 3b 0a  rence(pParent);.
3122a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
3122b 69 74 65 33 5f 66 72 65 65 28 70 4e 6f 64 65 29  ite3_free(pNode)
3122c 3b 0a 20 20 20 20 70 4e 6f 64 65 20 3d 20 30 3b  ;.    pNode = 0;
3122d 0a 20 20 7d 0a 0a 20 20 2a 70 70 4e 6f 64 65 20  .  }..  *ppNode 
3122e 3d 20 70 4e 6f 64 65 3b 0a 20 20 72 63 20 3d 20  = pNode;.  rc = 
3122f 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52  sqlite3_reset(pR
31230 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64 65 29  tree->pReadNode)
31231 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ;..  if( rc==SQL
31232 49 54 45 5f 4f 4b 20 26 26 20 69 4e 6f 64 65 3d  ITE_OK && iNode=
31233 3d 31 20 29 7b 0a 20 20 20 20 70 52 74 72 65 65  =1 ){.    pRtree
31234 2d 3e 69 44 65 70 74 68 20 3d 20 72 65 61 64 49  ->iDepth = readI
31235 6e 74 31 36 28 70 4e 6f 64 65 2d 3e 7a 44 61 74  nt16(pNode->zDat
31236 61 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  a);.  }..  asser
31237 74 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  t( (rc==SQLITE_O
31238 4b 20 26 26 20 70 4e 6f 64 65 29 20 7c 7c 20 28  K && pNode) || (
31239 70 4e 6f 64 65 3d 3d 30 20 26 26 20 72 63 21 3d  pNode==0 && rc!=
3123a 53 51 4c 49 54 45 5f 4f 4b 29 20 29 3b 0a 20 20  SQLITE_OK) );.  
3123b 6e 6f 64 65 48 61 73 68 49 6e 73 65 72 74 28 70  nodeHashInsert(p
3123c 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 0a  Rtree, pNode);..
3123d 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
3123e 2f 2a 0a 2a 2a 20 4f 76 65 72 77 72 69 74 65 20  /*.** Overwrite 
3123f 63 65 6c 6c 20 69 43 65 6c 6c 20 6f 66 20 6e 6f  cell iCell of no
31240 64 65 20 70 4e 6f 64 65 20 77 69 74 68 20 74 68  de pNode with th
31241 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 43  e contents of pC
31242 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ell..*/.static v
31243 6f 69 64 20 6e 6f 64 65 4f 76 65 72 77 72 69 74  oid nodeOverwrit
31244 65 43 65 6c 6c 28 0a 20 20 52 74 72 65 65 20 2a  eCell(.  Rtree *
31245 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65  pRtree, .  Rtree
31246 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 20 0a 20  Node *pNode,  . 
31247 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c   RtreeCell *pCel
31248 6c 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c 6c 0a  l, .  int iCell.
31249 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 75  ){.  int ii;.  u
3124a 38 20 2a 70 20 3d 20 26 70 4e 6f 64 65 2d 3e 7a  8 *p = &pNode->z
3124b 44 61 74 61 5b 34 20 2b 20 70 52 74 72 65 65 2d  Data[4 + pRtree-
3124c 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 2a 69  >nBytesPerCell*i
3124d 43 65 6c 6c 5d 3b 0a 20 20 70 20 2b 3d 20 77 72  Cell];.  p += wr
3124e 69 74 65 49 6e 74 36 34 28 70 2c 20 70 43 65 6c  iteInt64(p, pCel
3124f 6c 2d 3e 69 52 6f 77 69 64 29 3b 0a 20 20 66 6f  l->iRowid);.  fo
31250 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72  r(ii=0; ii<(pRtr
31251 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b  ee->nDim*2); ii+
31252 2b 29 7b 0a 20 20 20 20 70 20 2b 3d 20 77 72 69  +){.    p += wri
31253 74 65 43 6f 6f 72 64 28 70 2c 20 26 70 43 65 6c  teCoord(p, &pCel
31254 6c 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 29 3b 0a  l->aCoord[ii]);.
31255 20 20 7d 0a 20 20 70 4e 6f 64 65 2d 3e 69 73 44    }.  pNode->isD
31256 69 72 74 79 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a  irty = 1;.}../*.
31257 2a 2a 20 52 65 6d 6f 76 65 20 63 65 6c 6c 20 74  ** Remove cell t
31258 68 65 20 63 65 6c 6c 20 77 69 74 68 20 69 6e 64  he cell with ind
31259 65 78 20 69 43 65 6c 6c 20 66 72 6f 6d 20 6e 6f  ex iCell from no
3125a 64 65 20 70 4e 6f 64 65 2e 0a 2a 2f 0a 73 74 61  de pNode..*/.sta
3125b 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 44 65 6c  tic void nodeDel
3125c 65 74 65 43 65 6c 6c 28 52 74 72 65 65 20 2a 70  eteCell(Rtree *p
3125d 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
3125e 20 2a 70 4e 6f 64 65 2c 20 69 6e 74 20 69 43 65   *pNode, int iCe
3125f 6c 6c 29 7b 0a 20 20 75 38 20 2a 70 44 73 74 20  ll){.  u8 *pDst 
31260 3d 20 26 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b  = &pNode->zData[
31261 34 20 2b 20 70 52 74 72 65 65 2d 3e 6e 42 79 74  4 + pRtree->nByt
31262 65 73 50 65 72 43 65 6c 6c 2a 69 43 65 6c 6c 5d  esPerCell*iCell]
31263 3b 0a 20 20 75 38 20 2a 70 53 72 63 20 3d 20 26  ;.  u8 *pSrc = &
31264 70 44 73 74 5b 70 52 74 72 65 65 2d 3e 6e 42 79  pDst[pRtree->nBy
31265 74 65 73 50 65 72 43 65 6c 6c 5d 3b 0a 20 20 69  tesPerCell];.  i
31266 6e 74 20 6e 42 79 74 65 20 3d 20 28 4e 43 45 4c  nt nByte = (NCEL
31267 4c 28 70 4e 6f 64 65 29 20 2d 20 69 43 65 6c 6c  L(pNode) - iCell
31268 20 2d 20 31 29 20 2a 20 70 52 74 72 65 65 2d 3e   - 1) * pRtree->
31269 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 3b 0a 20  nBytesPerCell;. 
3126a 20 6d 65 6d 6d 6f 76 65 28 70 44 73 74 2c 20 70   memmove(pDst, p
3126b 53 72 63 2c 20 6e 42 79 74 65 29 3b 0a 20 20 77  Src, nByte);.  w
3126c 72 69 74 65 49 6e 74 31 36 28 26 70 4e 6f 64 65  riteInt16(&pNode
3126d 2d 3e 7a 44 61 74 61 5b 32 5d 2c 20 4e 43 45 4c  ->zData[2], NCEL
3126e 4c 28 70 4e 6f 64 65 29 2d 31 29 3b 0a 20 20 70  L(pNode)-1);.  p
3126f 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20  Node->isDirty = 
31270 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65  1;.}../*.** Inse
31271 72 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  rt the contents 
31272 6f 66 20 63 65 6c 6c 20 70 43 65 6c 6c 20 69 6e  of cell pCell in
31273 74 6f 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 49  to node pNode. I
31274 66 20 74 68 65 20 69 6e 73 65 72 74 0a 2a 2a 20  f the insert.** 
31275 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72  is successful, r
31276 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e  eturn SQLITE_OK.
31277 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  .**.** If there 
31278 69 73 20 6e 6f 74 20 65 6e 6f 75 67 68 20 66 72  is not enough fr
31279 65 65 20 73 70 61 63 65 20 69 6e 20 70 4e 6f 64  ee space in pNod
3127a 65 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  e, return SQLITE
3127b 5f 46 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63  _FULL..*/.static
3127c 20 69 6e 74 0a 6e 6f 64 65 49 6e 73 65 72 74 43   int.nodeInsertC
3127d 65 6c 6c 28 0a 20 20 52 74 72 65 65 20 2a 70 52  ell(.  Rtree *pR
3127e 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65 4e 6f  tree, .  RtreeNo
3127f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 52 74  de *pNode, .  Rt
31280 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 20 0a  reeCell *pCell .
31281 29 7b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 20  ){.  int nCell; 
31282 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31283 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75     /* Current nu
31284 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e  mber of cells in
31285 20 70 4e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20   pNode */.  int 
31286 6e 4d 61 78 43 65 6c 6c 3b 20 20 20 20 20 20 20  nMaxCell;       
31287 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
31288 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63  imum number of c
31289 65 6c 6c 73 20 66 6f 72 20 70 4e 6f 64 65 20 2a  ells for pNode *
3128a 2f 0a 0a 20 20 6e 4d 61 78 43 65 6c 6c 20 3d 20  /..  nMaxCell = 
3128b 28 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69  (pRtree->iNodeSi
3128c 7a 65 2d 34 29 2f 70 52 74 72 65 65 2d 3e 6e 42  ze-4)/pRtree->nB
3128d 79 74 65 73 50 65 72 43 65 6c 6c 3b 0a 20 20 6e  ytesPerCell;.  n
3128e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f  Cell = NCELL(pNo
3128f 64 65 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 6e  de);..  assert(n
31290 43 65 6c 6c 3c 3d 6e 4d 61 78 43 65 6c 6c 29 3b  Cell<=nMaxCell);
31291 0a 0a 20 20 69 66 28 20 6e 43 65 6c 6c 3c 6e 4d  ..  if( nCell<nM
31292 61 78 43 65 6c 6c 20 29 7b 0a 20 20 20 20 6e 6f  axCell ){.    no
31293 64 65 4f 76 65 72 77 72 69 74 65 43 65 6c 6c 28  deOverwriteCell(
31294 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70  pRtree, pNode, p
31295 43 65 6c 6c 2c 20 6e 43 65 6c 6c 29 3b 0a 20 20  Cell, nCell);.  
31296 20 20 77 72 69 74 65 49 6e 74 31 36 28 26 70 4e    writeInt16(&pN
31297 6f 64 65 2d 3e 7a 44 61 74 61 5b 32 5d 2c 20 6e  ode->zData[2], n
31298 43 65 6c 6c 2b 31 29 3b 0a 20 20 20 20 70 4e 6f  Cell+1);.    pNo
31299 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20 31 3b  de->isDirty = 1;
3129a 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 28  .  }..  return (
3129b 6e 43 65 6c 6c 3d 3d 6e 4d 61 78 43 65 6c 6c 29  nCell==nMaxCell)
3129c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  ;.}../*.** If th
3129d 65 20 6e 6f 64 65 20 69 73 20 64 69 72 74 79 2c  e node is dirty,
3129e 20 77 72 69 74 65 20 69 74 20 6f 75 74 20 74 6f   write it out to
3129f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a   the database..*
312a0 2f 0a 73 74 61 74 69 63 20 69 6e 74 0a 6e 6f 64  /.static int.nod
312a1 65 57 72 69 74 65 28 52 74 72 65 65 20 2a 70 52  eWrite(Rtree *pR
312a2 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20  tree, RtreeNode 
312a3 2a 70 4e 6f 64 65 29 7b 0a 20 20 69 6e 74 20 72  *pNode){.  int r
312a4 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
312a5 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 73 44 69   if( pNode->isDi
312a6 72 74 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  rty ){.    sqlit
312a7 65 33 5f 73 74 6d 74 20 2a 70 20 3d 20 70 52 74  e3_stmt *p = pRt
312a8 72 65 65 2d 3e 70 57 72 69 74 65 4e 6f 64 65 3b  ree->pWriteNode;
312a9 0a 20 20 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e  .    if( pNode->
312aa 69 4e 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 73  iNode ){.      s
312ab 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
312ac 34 28 70 2c 20 31 2c 20 70 4e 6f 64 65 2d 3e 69  4(p, 1, pNode->i
312ad 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Node);.    }else
312ae 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
312af 62 69 6e 64 5f 6e 75 6c 6c 28 70 2c 20 31 29 3b  bind_null(p, 1);
312b0 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
312b1 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 70 2c 20  e3_bind_blob(p, 
312b2 32 2c 20 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 2c  2, pNode->zData,
312b3 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69   pRtree->iNodeSi
312b4 7a 65 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  ze, SQLITE_STATI
312b5 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  C);.    sqlite3_
312b6 73 74 65 70 28 70 29 3b 0a 20 20 20 20 70 4e 6f  step(p);.    pNo
312b7 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20 30 3b  de->isDirty = 0;
312b8 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
312b9 33 5f 72 65 73 65 74 28 70 29 3b 0a 20 20 20 20  3_reset(p);.    
312ba 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  if( pNode->iNode
312bb 3d 3d 30 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  ==0 && rc==SQLIT
312bc 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 4e  E_OK ){.      pN
312bd 6f 64 65 2d 3e 69 4e 6f 64 65 20 3d 20 73 71 6c  ode->iNode = sql
312be 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74  ite3_last_insert
312bf 5f 72 6f 77 69 64 28 70 52 74 72 65 65 2d 3e 64  _rowid(pRtree->d
312c0 62 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 48 61  b);.      nodeHa
312c1 73 68 49 6e 73 65 72 74 28 70 52 74 72 65 65 2c  shInsert(pRtree,
312c2 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20   pNode);.    }. 
312c3 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
312c4 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65  }../*.** Release
312c5 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20   a reference to 
312c6 61 20 6e 6f 64 65 2e 20 49 66 20 74 68 65 20 6e  a node. If the n
312c7 6f 64 65 20 69 73 20 64 69 72 74 79 20 61 6e 64  ode is dirty and
312c8 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 0a 2a   the reference.*
312c9 2a 20 63 6f 75 6e 74 20 64 72 6f 70 73 20 74 6f  * count drops to
312ca 20 7a 65 72 6f 2c 20 74 68 65 20 6e 6f 64 65 20   zero, the node 
312cb 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20  data is written 
312cc 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  to the database.
312cd 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 0a 6e  .*/.static int.n
312ce 6f 64 65 52 65 6c 65 61 73 65 28 52 74 72 65 65  odeRelease(Rtree
312cf 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e   *pRtree, RtreeN
312d0 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 69  ode *pNode){.  i
312d1 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
312d2 4b 3b 0a 20 20 69 66 28 20 70 4e 6f 64 65 20 29  K;.  if( pNode )
312d3 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e  {.    assert( pN
312d4 6f 64 65 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20  ode->nRef>0 );. 
312d5 20 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 2d 2d     pNode->nRef--
312d6 3b 0a 20 20 20 20 69 66 28 20 70 4e 6f 64 65 2d  ;.    if( pNode-
312d7 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20  >nRef==0 ){.    
312d8 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f    if( pNode->iNo
312d9 64 65 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20  de==1 ){.       
312da 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 20   pRtree->iDepth 
312db 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  = -1;.      }.  
312dc 20 20 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 70      if( pNode->p
312dd 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 20 20  Parent ){.      
312de 20 20 72 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61    rc = nodeRelea
312df 73 65 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  se(pRtree, pNode
312e0 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20  ->pParent);.    
312e1 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63    }.      if( rc
312e2 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
312e3 20 20 20 20 20 20 20 72 63 20 3d 20 6e 6f 64 65         rc = node
312e4 57 72 69 74 65 28 70 52 74 72 65 65 2c 20 70 4e  Write(pRtree, pN
312e5 6f 64 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ode);.      }.  
312e6 20 20 20 20 6e 6f 64 65 48 61 73 68 44 65 6c 65      nodeHashDele
312e7 74 65 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  te(pRtree, pNode
312e8 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
312e9 5f 66 72 65 65 28 70 4e 6f 64 65 29 3b 0a 20 20  _free(pNode);.  
312ea 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
312eb 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
312ec 74 75 72 6e 20 74 68 65 20 36 34 2d 62 69 74 20  turn the 64-bit 
312ed 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 61 73  integer value as
312ee 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 63 65  sociated with ce
312ef 6c 6c 20 69 43 65 6c 6c 20 6f 66 0a 2a 2a 20 6e  ll iCell of.** n
312f0 6f 64 65 20 70 4e 6f 64 65 2e 20 49 66 20 70 4e  ode pNode. If pN
312f1 6f 64 65 20 69 73 20 61 20 6c 65 61 66 20 6e 6f  ode is a leaf no
312f2 64 65 2c 20 74 68 69 73 20 69 73 20 61 20 72 6f  de, this is a ro
312f3 77 69 64 2e 20 49 66 20 69 74 20 69 73 0a 2a 2a  wid. If it is.**
312f4 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64   an internal nod
312f5 65 2c 20 74 68 65 6e 20 74 68 65 20 36 34 2d 62  e, then the 64-b
312f6 69 74 20 69 6e 74 65 67 65 72 20 69 73 20 61 20  it integer is a 
312f7 63 68 69 6c 64 20 70 61 67 65 20 6e 75 6d 62 65  child page numbe
312f8 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36 34  r..*/.static i64
312f9 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 0a 20   nodeGetRowid(. 
312fa 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20   Rtree *pRtree, 
312fb 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e  .  RtreeNode *pN
312fc 6f 64 65 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c  ode, .  int iCel
312fd 6c 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69  l.){.  assert( i
312fe 43 65 6c 6c 3c 4e 43 45 4c 4c 28 70 4e 6f 64 65  Cell<NCELL(pNode
312ff 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65  ) );.  return re
31300 61 64 49 6e 74 36 34 28 26 70 4e 6f 64 65 2d 3e  adInt64(&pNode->
31301 7a 44 61 74 61 5b 34 20 2b 20 70 52 74 72 65 65  zData[4 + pRtree
31302 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 2a  ->nBytesPerCell*
31303 69 43 65 6c 6c 5d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  iCell]);.}../*.*
31304 2a 20 52 65 74 75 72 6e 20 63 6f 6f 72 64 69 6e  * Return coordin
31305 61 74 65 20 69 43 6f 6f 72 64 20 66 72 6f 6d 20  ate iCoord from 
31306 63 65 6c 6c 20 69 43 65 6c 6c 20 69 6e 20 6e 6f  cell iCell in no
31307 64 65 20 70 4e 6f 64 65 2e 0a 2a 2f 0a 73 74 61  de pNode..*/.sta
31308 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 47 65 74  tic void nodeGet
31309 43 6f 6f 72 64 28 0a 20 20 52 74 72 65 65 20 2a  Coord(.  Rtree *
3130a 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65  pRtree, .  Rtree
3130b 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20  Node *pNode, .  
3130c 69 6e 74 20 69 43 65 6c 6c 2c 0a 20 20 69 6e 74  int iCell,.  int
3130d 20 69 43 6f 6f 72 64 2c 0a 20 20 52 74 72 65 65   iCoord,.  Rtree
3130e 43 6f 6f 72 64 20 2a 70 43 6f 6f 72 64 20 20 20  Coord *pCoord   
3130f 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65          /* Space
31310 20 74 6f 20 77 72 69 74 65 20 72 65 73 75 6c 74   to write result
31311 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 72 65 61 64   to */.){.  read
31312 43 6f 6f 72 64 28 26 70 4e 6f 64 65 2d 3e 7a 44  Coord(&pNode->zD
31313 61 74 61 5b 31 32 20 2b 20 70 52 74 72 65 65 2d  ata[12 + pRtree-
31314 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 2a 69  >nBytesPerCell*i
31315 43 65 6c 6c 20 2b 20 34 2a 69 43 6f 6f 72 64 5d  Cell + 4*iCoord]
31316 2c 20 70 43 6f 6f 72 64 29 3b 0a 7d 0a 0a 2f 2a  , pCoord);.}../*
31317 0a 2a 2a 20 44 65 73 65 72 69 61 6c 69 7a 65 20  .** Deserialize 
31318 63 65 6c 6c 20 69 43 65 6c 6c 20 6f 66 20 6e 6f  cell iCell of no
31319 64 65 20 70 4e 6f 64 65 2e 20 50 6f 70 75 6c 61  de pNode. Popula
3131a 74 65 20 74 68 65 20 73 74 72 75 63 74 75 72 65  te the structure
3131b 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62   pointed.** to b
3131c 79 20 70 43 65 6c 6c 20 77 69 74 68 20 74 68 65  y pCell with the
3131d 20 72 65 73 75 6c 74 73 2e 0a 2a 2f 0a 73 74 61   results..*/.sta
3131e 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 47 65 74  tic void nodeGet
3131f 43 65 6c 6c 28 0a 20 20 52 74 72 65 65 20 2a 70  Cell(.  Rtree *p
31320 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65 4e  Rtree, .  RtreeN
31321 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 69  ode *pNode, .  i
31322 6e 74 20 69 43 65 6c 6c 2c 0a 20 20 52 74 72 65  nt iCell,.  Rtre
31323 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 0a 29 7b 0a  eCell *pCell.){.
31324 20 20 69 6e 74 20 69 69 3b 0a 20 20 70 43 65 6c    int ii;.  pCel
31325 6c 2d 3e 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65  l->iRowid = node
31326 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c  GetRowid(pRtree,
31327 20 70 4e 6f 64 65 2c 20 69 43 65 6c 6c 29 3b 0a   pNode, iCell);.
31328 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70    for(ii=0; ii<p
31329 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 3b 20 69  Rtree->nDim*2; i
3132a 69 2b 2b 29 7b 0a 20 20 20 20 6e 6f 64 65 47 65  i++){.    nodeGe
3132b 74 43 6f 6f 72 64 28 70 52 74 72 65 65 2c 20 70  tCoord(pRtree, p
3132c 4e 6f 64 65 2c 20 69 43 65 6c 6c 2c 20 69 69 2c  Node, iCell, ii,
3132d 20 26 70 43 65 6c 6c 2d 3e 61 43 6f 6f 72 64 5b   &pCell->aCoord[
3132e 69 69 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a  ii]);.  }.}.../*
3132f 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61   Forward declara
31330 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 66 75 6e  tion for the fun
31331 63 74 69 6f 6e 20 74 68 61 74 20 64 6f 65 73 20  ction that does 
31332 74 68 65 20 77 6f 72 6b 20 6f 66 0a 2a 2a 20 74  the work of.** t
31333 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
31334 20 6d 6f 64 75 6c 65 20 78 43 72 65 61 74 65 28   module xCreate(
31335 29 20 61 6e 64 20 78 43 6f 6e 6e 65 63 74 28 29  ) and xConnect()
31336 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 73 74 61   methods..*/.sta
31337 74 69 63 20 69 6e 74 20 72 74 72 65 65 49 6e 69  tic int rtreeIni
31338 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 2c 20  t(.  sqlite3 *, 
31339 76 6f 69 64 20 2a 2c 20 69 6e 74 2c 20 63 6f 6e  void *, int, con
3133a 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 2c  st char *const*,
3133b 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a   sqlite3_vtab **
3133c 2c 20 63 68 61 72 20 2a 2a 2c 20 69 6e 74 0a 29  , char **, int.)
3133d 3b 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20  ;../* .** Rtree 
3133e 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
3133f 64 75 6c 65 20 78 43 72 65 61 74 65 20 6d 65 74  dule xCreate met
31340 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  hod..*/.static i
31341 6e 74 20 72 74 72 65 65 43 72 65 61 74 65 28 0a  nt rtreeCreate(.
31342 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20    sqlite3 *db,. 
31343 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 69   void *pAux,.  i
31344 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63  nt argc, const c
31345 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c  har *const*argv,
31346 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
31347 2a 2a 70 70 56 74 61 62 2c 0a 20 20 63 68 61 72  **ppVtab,.  char
31348 20 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20 20 72 65   **pzErr.){.  re
31349 74 75 72 6e 20 72 74 72 65 65 49 6e 69 74 28 64  turn rtreeInit(d
3134a 62 2c 20 70 41 75 78 2c 20 61 72 67 63 2c 20 61  b, pAux, argc, a
3134b 72 67 76 2c 20 70 70 56 74 61 62 2c 20 70 7a 45  rgv, ppVtab, pzE
3134c 72 72 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a  rr, 1);.}../* .*
3134d 2a 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20  * Rtree virtual 
3134e 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 43 6f  table module xCo
3134f 6e 6e 65 63 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f  nnect method..*/
31350 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65  .static int rtre
31351 65 43 6f 6e 6e 65 63 74 28 0a 20 20 73 71 6c 69  eConnect(.  sqli
31352 74 65 33 20 2a 64 62 2c 0a 20 20 76 6f 69 64 20  te3 *db,.  void 
31353 2a 70 41 75 78 2c 0a 20 20 69 6e 74 20 61 72 67  *pAux,.  int arg
31354 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  c, const char *c
31355 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 73 71 6c  onst*argv,.  sql
31356 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 74  ite3_vtab **ppVt
31357 61 62 2c 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45  ab,.  char **pzE
31358 72 72 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 72  rr.){.  return r
31359 74 72 65 65 49 6e 69 74 28 64 62 2c 20 70 41 75  treeInit(db, pAu
3135a 78 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 70  x, argc, argv, p
3135b 70 56 74 61 62 2c 20 70 7a 45 72 72 2c 20 30 29  pVtab, pzErr, 0)
3135c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65  ;.}../*.** Incre
3135d 6d 65 6e 74 20 74 68 65 20 72 2d 74 72 65 65 20  ment the r-tree 
3135e 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 2e  reference count.
3135f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
31360 72 74 72 65 65 52 65 66 65 72 65 6e 63 65 28 52  rtreeReference(R
31361 74 72 65 65 20 2a 70 52 74 72 65 65 29 7b 0a 20  tree *pRtree){. 
31362 20 70 52 74 72 65 65 2d 3e 6e 42 75 73 79 2b 2b   pRtree->nBusy++
31363 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65  ;.}../*.** Decre
31364 6d 65 6e 74 20 74 68 65 20 72 2d 74 72 65 65 20  ment the r-tree 
31365 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 2e  reference count.
31366 20 57 68 65 6e 20 74 68 65 20 72 65 66 65 72 65   When the refere
31367 6e 63 65 20 63 6f 75 6e 74 20 72 65 61 63 68 65  nce count reache
31368 73 0a 2a 2a 20 7a 65 72 6f 20 74 68 65 20 73 74  s.** zero the st
31369 72 75 63 74 75 72 65 20 69 73 20 64 65 6c 65 74  ructure is delet
3136a 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
3136b 69 64 20 72 74 72 65 65 52 65 6c 65 61 73 65 28  id rtreeRelease(
3136c 52 74 72 65 65 20 2a 70 52 74 72 65 65 29 7b 0a  Rtree *pRtree){.
3136d 20 20 70 52 74 72 65 65 2d 3e 6e 42 75 73 79 2d    pRtree->nBusy-
3136e 2d 3b 0a 20 20 69 66 28 20 70 52 74 72 65 65 2d  -;.  if( pRtree-
3136f 3e 6e 42 75 73 79 3d 3d 30 20 29 7b 0a 20 20 20  >nBusy==0 ){.   
31370 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
31371 65 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 4e  e(pRtree->pReadN
31372 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ode);.    sqlite
31373 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72 65  3_finalize(pRtre
31374 65 2d 3e 70 57 72 69 74 65 4e 6f 64 65 29 3b 0a  e->pWriteNode);.
31375 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61      sqlite3_fina
31376 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70 44 65  lize(pRtree->pDe
31377 6c 65 74 65 4e 6f 64 65 29 3b 0a 20 20 20 20 73  leteNode);.    s
31378 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
31379 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77  pRtree->pReadRow
3137a 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  id);.    sqlite3
3137b 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65  _finalize(pRtree
3137c 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 29 3b 0a  ->pWriteRowid);.
3137d 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61      sqlite3_fina
3137e 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70 44 65  lize(pRtree->pDe
3137f 6c 65 74 65 52 6f 77 69 64 29 3b 0a 20 20 20 20  leteRowid);.    
31380 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
31381 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 50 61  (pRtree->pReadPa
31382 72 65 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  rent);.    sqlit
31383 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72  e3_finalize(pRtr
31384 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65 6e 74  ee->pWriteParent
31385 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
31386 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e  inalize(pRtree->
31387 70 44 65 6c 65 74 65 50 61 72 65 6e 74 29 3b 0a  pDeleteParent);.
31388 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
31389 28 70 52 74 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a  (pRtree);.  }.}.
3138a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69  ./* .** Rtree vi
3138b 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
3138c 6c 65 20 78 44 69 73 63 6f 6e 6e 65 63 74 20 6d  le xDisconnect m
3138d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ethod..*/.static
3138e 20 69 6e 74 20 72 74 72 65 65 44 69 73 63 6f 6e   int rtreeDiscon
3138f 6e 65 63 74 28 73 71 6c 69 74 65 33 5f 76 74 61  nect(sqlite3_vta
31390 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 72 74 72  b *pVtab){.  rtr
31391 65 65 52 65 6c 65 61 73 65 28 28 52 74 72 65 65  eeRelease((Rtree
31392 20 2a 29 70 56 74 61 62 29 3b 0a 20 20 72 65 74   *)pVtab);.  ret
31393 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
31394 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76  ../* .** Rtree v
31395 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
31396 75 6c 65 20 78 44 65 73 74 72 6f 79 20 6d 65 74  ule xDestroy met
31397 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  hod..*/.static i
31398 6e 74 20 72 74 72 65 65 44 65 73 74 72 6f 79 28  nt rtreeDestroy(
31399 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
3139a 74 61 62 29 7b 0a 20 20 52 74 72 65 65 20 2a 70  tab){.  Rtree *p
3139b 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a  Rtree = (Rtree *
3139c 29 70 56 74 61 62 3b 0a 20 20 69 6e 74 20 72 63  )pVtab;.  int rc
3139d 3b 0a 20 20 63 68 61 72 20 2a 7a 43 72 65 61 74  ;.  char *zCreat
3139e 65 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  e = sqlite3_mpri
3139f 6e 74 66 28 0a 20 20 20 20 22 44 52 4f 50 20 54  ntf(.    "DROP T
313a0 41 42 4c 45 20 27 25 71 27 2e 27 25 71 5f 6e 6f  ABLE '%q'.'%q_no
313a1 64 65 27 3b 22 0a 20 20 20 20 22 44 52 4f 50 20  de';".    "DROP 
313a2 54 41 42 4c 45 20 27 25 71 27 2e 27 25 71 5f 72  TABLE '%q'.'%q_r
313a3 6f 77 69 64 27 3b 22 0a 20 20 20 20 22 44 52 4f  owid';".    "DRO
313a4 50 20 54 41 42 4c 45 20 27 25 71 27 2e 27 25 71  P TABLE '%q'.'%q
313a5 5f 70 61 72 65 6e 74 27 3b 22 2c 0a 20 20 20 20  _parent';",.    
313a6 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74  pRtree->zDb, pRt
313a7 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 0a 20 20 20  ree->zName, .   
313a8 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52   pRtree->zDb, pR
313a9 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20  tree->zName,.   
313aa 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52   pRtree->zDb, pR
313ab 74 72 65 65 2d 3e 7a 4e 61 6d 65 0a 20 20 29 3b  tree->zName.  );
313ac 0a 20 20 69 66 28 20 21 7a 43 72 65 61 74 65 20  .  if( !zCreate 
313ad 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
313ae 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73  TE_NOMEM;.  }els
313af 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  e{.    rc = sqli
313b0 74 65 33 5f 65 78 65 63 28 70 52 74 72 65 65 2d  te3_exec(pRtree-
313b1 3e 64 62 2c 20 7a 43 72 65 61 74 65 2c 20 30 2c  >db, zCreate, 0,
313b2 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
313b3 74 65 33 5f 66 72 65 65 28 7a 43 72 65 61 74 65  te3_free(zCreate
313b4 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  );.  }.  if( rc=
313b5 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
313b6 20 20 72 74 72 65 65 52 65 6c 65 61 73 65 28 70    rtreeRelease(p
313b7 52 74 72 65 65 29 3b 0a 20 20 7d 0a 0a 20 20 72  Rtree);.  }..  r
313b8 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
313b9 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61  .** Rtree virtua
313ba 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78  l table module x
313bb 4f 70 65 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a  Open method..*/.
313bc 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65  static int rtree
313bd 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 74 61  Open(sqlite3_vta
313be 62 20 2a 70 56 54 61 62 2c 20 73 71 6c 69 74 65  b *pVTab, sqlite
313bf 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 2a  3_vtab_cursor **
313c0 70 70 43 75 72 73 6f 72 29 7b 0a 20 20 69 6e 74  ppCursor){.  int
313c1 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
313c2 45 4d 3b 0a 20 20 52 74 72 65 65 43 75 72 73 6f  EM;.  RtreeCurso
313c3 72 20 2a 70 43 73 72 3b 0a 0a 20 20 70 43 73 72  r *pCsr;..  pCsr
313c4 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72 20   = (RtreeCursor 
313c5 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
313c6 28 73 69 7a 65 6f 66 28 52 74 72 65 65 43 75 72  (sizeof(RtreeCur
313c7 73 6f 72 29 29 3b 0a 20 20 69 66 28 20 70 43 73  sor));.  if( pCs
313c8 72 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28  r ){.    memset(
313c9 70 43 73 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28  pCsr, 0, sizeof(
313ca 52 74 72 65 65 43 75 72 73 6f 72 29 29 3b 0a 20  RtreeCursor));. 
313cb 20 20 20 70 43 73 72 2d 3e 62 61 73 65 2e 70 56     pCsr->base.pV
313cc 74 61 62 20 3d 20 70 56 54 61 62 3b 0a 20 20 20  tab = pVTab;.   
313cd 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
313ce 0a 20 20 7d 0a 20 20 2a 70 70 43 75 72 73 6f 72  .  }.  *ppCursor
313cf 20 3d 20 28 73 71 6c 69 74 65 33 5f 76 74 61 62   = (sqlite3_vtab
313d0 5f 63 75 72 73 6f 72 20 2a 29 70 43 73 72 3b 0a  _cursor *)pCsr;.
313d1 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
313d2 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69  ./* .** Rtree vi
313d3 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
313d4 6c 65 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64  le xClose method
313d5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
313d6 72 74 72 65 65 43 6c 6f 73 65 28 73 71 6c 69 74  rtreeClose(sqlit
313d7 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a  e3_vtab_cursor *
313d8 63 75 72 29 7b 0a 20 20 52 74 72 65 65 20 2a 70  cur){.  Rtree *p
313d9 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a  Rtree = (Rtree *
313da 29 28 63 75 72 2d 3e 70 56 74 61 62 29 3b 0a 20  )(cur->pVtab);. 
313db 20 69 6e 74 20 72 63 3b 0a 20 20 52 74 72 65 65   int rc;.  Rtree
313dc 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28  Cursor *pCsr = (
313dd 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29 63 75  RtreeCursor *)cu
313de 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  r;.  sqlite3_fre
313df 65 28 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61  e(pCsr->aConstra
313e0 69 6e 74 29 3b 0a 20 20 72 63 20 3d 20 6e 6f 64  int);.  rc = nod
313e1 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
313e2 20 70 43 73 72 2d 3e 70 4e 6f 64 65 29 3b 0a 20   pCsr->pNode);. 
313e3 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43   sqlite3_free(pC
313e4 73 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  sr);.  return rc
313e5 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 74 72 65 65  ;.}../*.** Rtree
313e6 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
313e7 6f 64 75 6c 65 20 78 45 6f 66 20 6d 65 74 68 6f  odule xEof metho
313e8 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  d..**.** Return 
313e9 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68 65 20  non-zero if the 
313ea 63 75 72 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20  cursor does not 
313eb 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 20  currently point 
313ec 74 6f 20 61 20 76 61 6c 69 64 20 0a 2a 2a 20 72  to a valid .** r
313ed 65 63 6f 72 64 20 28 69 2e 65 20 69 66 20 74 68  ecord (i.e if th
313ee 65 20 73 63 61 6e 20 68 61 73 20 66 69 6e 69 73  e scan has finis
313ef 68 65 64 29 2c 20 6f 72 20 7a 65 72 6f 20 6f 74  hed), or zero ot
313f0 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74  herwise..*/.stat
313f1 69 63 20 69 6e 74 20 72 74 72 65 65 45 6f 66 28  ic int rtreeEof(
313f2 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
313f3 73 6f 72 20 2a 63 75 72 29 7b 0a 20 20 52 74 72  sor *cur){.  Rtr
313f4 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d  eeCursor *pCsr =
313f5 20 28 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29   (RtreeCursor *)
313f6 63 75 72 3b 0a 20 20 72 65 74 75 72 6e 20 28 70  cur;.  return (p
313f7 43 73 72 2d 3e 70 4e 6f 64 65 3d 3d 30 29 3b 0a  Csr->pNode==0);.
313f8 7d 0a 0a 2f 2a 20 0a 2a 2a 20 43 75 72 73 6f 72  }../* .** Cursor
313f9 20 70 43 75 72 73 6f 72 20 63 75 72 72 65 6e 74   pCursor current
313fa 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 63  ly points to a c
313fb 65 6c 6c 20 69 6e 20 61 20 6e 6f 6e 2d 6c 65 61  ell in a non-lea
313fc 66 20 70 61 67 65 2e 0a 2a 2a 20 52 65 74 75 72  f page..** Retur
313fd 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 73 75  n true if the su
313fe 62 2d 74 72 65 65 20 68 65 61 64 65 64 20 62 79  b-tree headed by
313ff 20 74 68 65 20 63 65 6c 6c 20 69 73 20 66 69 6c   the cell is fil
31400 74 65 72 65 64 0a 2a 2a 20 28 65 78 63 6c 75 64  tered.** (exclud
31401 65 64 29 20 62 79 20 74 68 65 20 63 6f 6e 73 74  ed) by the const
31402 72 61 69 6e 74 73 20 69 6e 20 74 68 65 20 70 43  raints in the pC
31403 75 72 73 6f 72 2d 3e 61 43 6f 6e 73 74 72 61 69  ursor->aConstrai
31404 6e 74 5b 5d 20 0a 2a 2a 20 61 72 72 61 79 2c 20  nt[] .** array, 
31405 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 72 77 69  or false otherwi
31406 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  se..*/.static in
31407 74 20 74 65 73 74 52 74 72 65 65 43 65 6c 6c 28  t testRtreeCell(
31408 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52  Rtree *pRtree, R
31409 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43 75 72  treeCursor *pCur
3140a 73 6f 72 29 7b 0a 20 20 52 74 72 65 65 43 65 6c  sor){.  RtreeCel
3140b 6c 20 63 65 6c 6c 3b 0a 20 20 69 6e 74 20 69 69  l cell;.  int ii
3140c 3b 0a 20 20 69 6e 74 20 62 52 65 73 20 3d 20 30  ;.  int bRes = 0
3140d 3b 0a 0a 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c  ;..  nodeGetCell
3140e 28 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72  (pRtree, pCursor
3140f 2d 3e 70 4e 6f 64 65 2c 20 70 43 75 72 73 6f 72  ->pNode, pCursor
31410 2d 3e 69 43 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b  ->iCell, &cell);
31411 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 62 52 65  .  for(ii=0; bRe
31412 73 3d 3d 30 20 26 26 20 69 69 3c 70 43 75 72 73  s==0 && ii<pCurs
31413 6f 72 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  or->nConstraint;
31414 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65   ii++){.    Rtre
31415 65 43 6f 6e 73 74 72 61 69 6e 74 20 2a 70 20 3d  eConstraint *p =
31416 20 26 70 43 75 72 73 6f 72 2d 3e 61 43 6f 6e 73   &pCursor->aCons
31417 74 72 61 69 6e 74 5b 69 69 5d 3b 0a 20 20 20 20  traint[ii];.    
31418 64 6f 75 62 6c 65 20 63 65 6c 6c 5f 6d 69 6e 20  double cell_min 
31419 3d 20 44 43 4f 4f 52 44 28 63 65 6c 6c 2e 61 43  = DCOORD(cell.aC
3141a 6f 6f 72 64 5b 28 70 2d 3e 69 43 6f 6f 72 64 3e  oord[(p->iCoord>
3141b 3e 31 29 2a 32 5d 29 3b 0a 20 20 20 20 64 6f 75  >1)*2]);.    dou
3141c 62 6c 65 20 63 65 6c 6c 5f 6d 61 78 20 3d 20 44  ble cell_max = D
3141d 43 4f 4f 52 44 28 63 65 6c 6c 2e 61 43 6f 6f 72  COORD(cell.aCoor
3141e 64 5b 28 70 2d 3e 69 43 6f 6f 72 64 3e 3e 31 29  d[(p->iCoord>>1)
3141f 2a 32 2b 31 5d 29 3b 0a 0a 20 20 20 20 61 73 73  *2+1]);..    ass
31420 65 72 74 28 70 2d 3e 6f 70 3d 3d 52 54 52 45 45  ert(p->op==RTREE
31421 5f 4c 45 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54  _LE || p->op==RT
31422 52 45 45 5f 4c 54 20 7c 7c 20 70 2d 3e 6f 70 3d  REE_LT || p->op=
31423 3d 52 54 52 45 45 5f 47 45 20 0a 20 20 20 20 20  =RTREE_GE .     
31424 20 20 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52     || p->op==RTR
31425 45 45 5f 47 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d  EE_GT || p->op==
31426 52 54 52 45 45 5f 45 51 0a 20 20 20 20 29 3b 0a  RTREE_EQ.    );.
31427 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 2d 3e  .    switch( p->
31428 6f 70 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65  op ){.      case
31429 20 52 54 52 45 45 5f 4c 45 3a 20 63 61 73 65 20   RTREE_LE: case 
3142a 52 54 52 45 45 5f 4c 54 3a 20 62 52 65 73 20 3d  RTREE_LT: bRes =
3142b 20 70 2d 3e 72 56 61 6c 75 65 3c 63 65 6c 6c 5f   p->rValue<cell_
3142c 6d 69 6e 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20  min; break;.    
3142d 20 20 63 61 73 65 20 52 54 52 45 45 5f 47 45 3a    case RTREE_GE:
3142e 20 63 61 73 65 20 52 54 52 45 45 5f 47 54 3a 20   case RTREE_GT: 
3142f 62 52 65 73 20 3d 20 70 2d 3e 72 56 61 6c 75 65  bRes = p->rValue
31430 3e 63 65 6c 6c 5f 6d 61 78 3b 20 62 72 65 61 6b  >cell_max; break
31431 3b 0a 20 20 20 20 20 20 63 61 73 65 20 52 54 52  ;.      case RTR
31432 45 45 5f 45 51 3a 20 0a 20 20 20 20 20 20 20 20  EE_EQ: .        
31433 62 52 65 73 20 3d 20 28 70 2d 3e 72 56 61 6c 75  bRes = (p->rValu
31434 65 3e 63 65 6c 6c 5f 6d 61 78 20 7c 7c 20 70 2d  e>cell_max || p-
31435 3e 72 56 61 6c 75 65 3c 63 65 6c 6c 5f 6d 69 6e  >rValue<cell_min
31436 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
31437 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72  ;.    }.  }..  r
31438 65 74 75 72 6e 20 62 52 65 73 3b 0a 7d 0a 0a 2f  eturn bRes;.}../
31439 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  * .** Return tru
3143a 65 20 69 66 20 74 68 65 20 63 65 6c 6c 20 74 68  e if the cell th
3143b 61 74 20 63 75 72 73 6f 72 20 70 43 75 72 73 6f  at cursor pCurso
3143c 72 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  r currently poin
3143d 74 73 20 74 6f 0a 2a 2a 20 77 6f 75 6c 64 20 62  ts to.** would b
3143e 65 20 66 69 6c 74 65 72 65 64 20 28 65 78 63 6c  e filtered (excl
3143f 75 64 65 64 29 20 62 79 20 74 68 65 20 63 6f 6e  uded) by the con
31440 73 74 72 61 69 6e 74 73 20 69 6e 20 74 68 65 20  straints in the 
31441 0a 2a 2a 20 70 43 75 72 73 6f 72 2d 3e 61 43 6f  .** pCursor->aCo
31442 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 61 79  nstraint[] array
31443 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 72  , or false other
31444 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  wise..**.** This
31445 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65   function assume
31446 73 20 74 68 61 74 20 74 68 65 20 63 65 6c 6c 20  s that the cell 
31447 69 73 20 70 61 72 74 20 6f 66 20 61 20 6c 65 61  is part of a lea
31448 66 20 6e 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69  f node..*/.stati
31449 63 20 69 6e 74 20 74 65 73 74 52 74 72 65 65 45  c int testRtreeE
3144a 6e 74 72 79 28 52 74 72 65 65 20 2a 70 52 74 72  ntry(Rtree *pRtr
3144b 65 65 2c 20 52 74 72 65 65 43 75 72 73 6f 72 20  ee, RtreeCursor 
3144c 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 52 74 72  *pCursor){.  Rtr
3144d 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 69  eeCell cell;.  i
3144e 6e 74 20 69 69 3b 0a 0a 20 20 6e 6f 64 65 47 65  nt ii;..  nodeGe
3144f 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 43  tCell(pRtree, pC
31450 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20 70 43  ursor->pNode, pC
31451 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 2c 20 26 63  ursor->iCell, &c
31452 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30  ell);.  for(ii=0
31453 3b 20 69 69 3c 70 43 75 72 73 6f 72 2d 3e 6e 43  ; ii<pCursor->nC
31454 6f 6e 73 74 72 61 69 6e 74 3b 20 69 69 2b 2b 29  onstraint; ii++)
31455 7b 0a 20 20 20 20 52 74 72 65 65 43 6f 6e 73 74  {.    RtreeConst
31456 72 61 69 6e 74 20 2a 70 20 3d 20 26 70 43 75 72  raint *p = &pCur
31457 73 6f 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  sor->aConstraint
31458 5b 69 69 5d 3b 0a 20 20 20 20 64 6f 75 62 6c 65  [ii];.    double
31459 20 63 6f 6f 72 64 20 3d 20 44 43 4f 4f 52 44 28   coord = DCOORD(
3145a 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 70 2d 3e 69  cell.aCoord[p->i
3145b 43 6f 6f 72 64 5d 29 3b 0a 20 20 20 20 69 6e 74  Coord]);.    int
3145c 20 72 65 73 3b 0a 20 20 20 20 61 73 73 65 72 74   res;.    assert
3145d 28 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 4c 45  (p->op==RTREE_LE
3145e 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45   || p->op==RTREE
3145f 5f 4c 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54  _LT || p->op==RT
31460 52 45 45 5f 47 45 20 0a 20 20 20 20 20 20 20 20  REE_GE .        
31461 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f  || p->op==RTREE_
31462 47 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52  GT || p->op==RTR
31463 45 45 5f 45 51 0a 20 20 20 20 29 3b 0a 20 20 20  EE_EQ.    );.   
31464 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29   switch( p->op )
31465 7b 0a 20 20 20 20 20 20 63 61 73 65 20 52 54 52  {.      case RTR
31466 45 45 5f 4c 45 3a 20 72 65 73 20 3d 20 28 63 6f  EE_LE: res = (co
31467 6f 72 64 3c 3d 70 2d 3e 72 56 61 6c 75 65 29 3b  ord<=p->rValue);
31468 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31469 73 65 20 52 54 52 45 45 5f 4c 54 3a 20 72 65 73  se RTREE_LT: res
3146a 20 3d 20 28 63 6f 6f 72 64 3c 70 2d 3e 72 56 61   = (coord<p->rVa
3146b 6c 75 65 29 3b 20 20 62 72 65 61 6b 3b 0a 20 20  lue);  break;.  
3146c 20 20 20 20 63 61 73 65 20 52 54 52 45 45 5f 47      case RTREE_G
3146d 45 3a 20 72 65 73 20 3d 20 28 63 6f 6f 72 64 3e  E: res = (coord>
3146e 3d 70 2d 3e 72 56 61 6c 75 65 29 3b 20 62 72 65  =p->rValue); bre
3146f 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 52  ak;.      case R
31470 54 52 45 45 5f 47 54 3a 20 72 65 73 20 3d 20 28  TREE_GT: res = (
31471 63 6f 6f 72 64 3e 70 2d 3e 72 56 61 6c 75 65 29  coord>p->rValue)
31472 3b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  ;  break;.      
31473 63 61 73 65 20 52 54 52 45 45 5f 45 51 3a 20 72  case RTREE_EQ: r
31474 65 73 20 3d 20 28 63 6f 6f 72 64 3d 3d 70 2d 3e  es = (coord==p->
31475 72 56 61 6c 75 65 29 3b 20 62 72 65 61 6b 3b 0a  rValue); break;.
31476 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 21      }..    if( !
31477 72 65 73 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  res ) return 1;.
31478 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 30 3b    }..  return 0;
31479 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 75 72 73 6f 72  .}../*.** Cursor
3147a 20 70 43 75 72 73 6f 72 20 63 75 72 72 65 6e 74   pCursor current
3147b 6c 79 20 70 6f 69 6e 74 73 20 61 74 20 61 20 6e  ly points at a n
3147c 6f 64 65 20 74 68 61 74 20 68 65 61 64 73 20 61  ode that heads a
3147d 20 73 75 62 2d 74 72 65 65 20 6f 66 0a 2a 2a 20   sub-tree of.** 
3147e 68 65 69 67 68 74 20 69 48 65 69 67 68 74 20 28  height iHeight (
3147f 69 66 20 69 48 65 69 67 68 74 3d 3d 30 2c 20 74  if iHeight==0, t
31480 68 65 6e 20 74 68 65 20 6e 6f 64 65 20 69 73 20  hen the node is 
31481 61 20 6c 65 61 66 29 2e 20 44 65 73 63 65 6e 64  a leaf). Descend
31482 0a 2a 2a 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  .** to point to 
31483 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 63 65  the left-most ce
31484 6c 6c 20 6f 66 20 74 68 65 20 73 75 62 2d 74 72  ll of the sub-tr
31485 65 65 20 74 68 61 74 20 6d 61 74 63 68 65 73 20  ee that matches 
31486 74 68 65 20 0a 2a 2a 20 63 6f 6e 66 69 67 75 72  the .** configur
31487 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a  ed constraints..
31488 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65  */.static int de
31489 73 63 65 6e 64 54 6f 43 65 6c 6c 28 0a 20 20 52  scendToCell(.  R
3148a 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20  tree *pRtree, . 
3148b 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43   RtreeCursor *pC
3148c 75 72 73 6f 72 2c 20 0a 20 20 69 6e 74 20 69 48  ursor, .  int iH
3148d 65 69 67 68 74 2c 0a 20 20 69 6e 74 20 2a 70 45  eight,.  int *pE
3148e 6f 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20  of              
3148f 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65 74 20 74     /* OUT: Set t
31490 6f 20 74 72 75 65 20 69 66 20 63 61 6e 6e 6f 74  o true if cannot
31491 20 64 65 73 63 65 6e 64 20 2a 2f 0a 29 7b 0a 20   descend */.){. 
31492 20 69 6e 74 20 69 73 45 6f 66 3b 0a 20 20 69 6e   int isEof;.  in
31493 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 69 3b 0a  t rc;.  int ii;.
31494 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 43 68    RtreeNode *pCh
31495 69 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69  ild;.  sqlite3_i
31496 6e 74 36 34 20 69 52 6f 77 69 64 3b 0a 0a 20 20  nt64 iRowid;..  
31497 52 74 72 65 65 4e 6f 64 65 20 2a 70 53 61 76 65  RtreeNode *pSave
31498 64 4e 6f 64 65 20 3d 20 70 43 75 72 73 6f 72 2d  dNode = pCursor-
31499 3e 70 4e 6f 64 65 3b 0a 20 20 69 6e 74 20 69 53  >pNode;.  int iS
3149a 61 76 65 64 43 65 6c 6c 20 3d 20 70 43 75 72 73  avedCell = pCurs
3149b 6f 72 2d 3e 69 43 65 6c 6c 3b 0a 0a 20 20 61 73  or->iCell;..  as
3149c 73 65 72 74 28 20 69 48 65 69 67 68 74 3e 3d 30  sert( iHeight>=0
3149d 20 29 3b 0a 0a 20 20 69 66 28 20 69 48 65 69 67   );..  if( iHeig
3149e 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 73 45  ht==0 ){.    isE
3149f 6f 66 20 3d 20 74 65 73 74 52 74 72 65 65 45 6e  of = testRtreeEn
314a0 74 72 79 28 70 52 74 72 65 65 2c 20 70 43 75 72  try(pRtree, pCur
314a1 73 6f 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  sor);.  }else{. 
314a2 20 20 20 69 73 45 6f 66 20 3d 20 74 65 73 74 52     isEof = testR
314a3 74 72 65 65 43 65 6c 6c 28 70 52 74 72 65 65 2c  treeCell(pRtree,
314a4 20 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 20   pCursor);.  }. 
314a5 20 69 66 28 20 69 73 45 6f 66 20 7c 7c 20 69 48   if( isEof || iH
314a6 65 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20  eight==0 ){.    
314a7 2a 70 45 6f 66 20 3d 20 69 73 45 6f 66 3b 0a 20  *pEof = isEof;. 
314a8 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
314a9 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 52 6f 77  _OK;.  }..  iRow
314aa 69 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69  id = nodeGetRowi
314ab 64 28 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f  d(pRtree, pCurso
314ac 72 2d 3e 70 4e 6f 64 65 2c 20 70 43 75 72 73 6f  r->pNode, pCurso
314ad 72 2d 3e 69 43 65 6c 6c 29 3b 0a 20 20 72 63 20  r->iCell);.  rc 
314ae 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52  = nodeAcquire(pR
314af 74 72 65 65 2c 20 69 52 6f 77 69 64 2c 20 70 43  tree, iRowid, pC
314b0 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20 26 70  ursor->pNode, &p
314b1 43 68 69 6c 64 29 3b 0a 20 20 69 66 28 20 72 63  Child);.  if( rc
314b2 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
314b3 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
314b4 7d 0a 0a 20 20 6e 6f 64 65 52 65 6c 65 61 73 65  }..  nodeRelease
314b5 28 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72  (pRtree, pCursor
314b6 2d 3e 70 4e 6f 64 65 29 3b 0a 20 20 70 43 75 72  ->pNode);.  pCur
314b7 73 6f 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 43 68  sor->pNode = pCh
314b8 69 6c 64 3b 0a 20 20 69 73 45 6f 66 20 3d 20 31  ild;.  isEof = 1
314b9 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 73  ;.  for(ii=0; is
314ba 45 6f 66 20 26 26 20 69 69 3c 4e 43 45 4c 4c 28  Eof && ii<NCELL(
314bb 70 43 68 69 6c 64 29 3b 20 69 69 2b 2b 29 7b 0a  pChild); ii++){.
314bc 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 69 43 65      pCursor->iCe
314bd 6c 6c 20 3d 20 69 69 3b 0a 20 20 20 20 72 63 20  ll = ii;.    rc 
314be 3d 20 64 65 73 63 65 6e 64 54 6f 43 65 6c 6c 28  = descendToCell(
314bf 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72 2c  pRtree, pCursor,
314c0 20 69 48 65 69 67 68 74 2d 31 2c 20 26 69 73 45   iHeight-1, &isE
314c1 6f 66 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  of);.    if( rc!
314c2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
314c3 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
314c4 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
314c5 69 73 45 6f 66 20 29 7b 0a 20 20 20 20 61 73 73  isEof ){.    ass
314c6 65 72 74 28 20 70 43 75 72 73 6f 72 2d 3e 70 4e  ert( pCursor->pN
314c7 6f 64 65 3d 3d 70 43 68 69 6c 64 20 29 3b 0a 20  ode==pChild );. 
314c8 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65     nodeReference
314c9 28 70 53 61 76 65 64 4e 6f 64 65 29 3b 0a 20 20  (pSavedNode);.  
314ca 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52    nodeRelease(pR
314cb 74 72 65 65 2c 20 70 43 68 69 6c 64 29 3b 0a 20  tree, pChild);. 
314cc 20 20 20 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64     pCursor->pNod
314cd 65 20 3d 20 70 53 61 76 65 64 4e 6f 64 65 3b 0a  e = pSavedNode;.
314ce 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 69 43 65      pCursor->iCe
314cf 6c 6c 20 3d 20 69 53 61 76 65 64 43 65 6c 6c 3b  ll = iSavedCell;
314d0 0a 20 20 7d 0a 0a 20 20 2a 70 45 6f 66 20 3d 20  .  }..  *pEof = 
314d1 69 73 45 6f 66 3b 0a 20 20 72 65 74 75 72 6e 20  isEof;.  return 
314d2 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
314d3 0a 2a 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 63  .** One of the c
314d4 65 6c 6c 73 20 69 6e 20 6e 6f 64 65 20 70 4e 6f  ells in node pNo
314d5 64 65 20 69 73 20 67 75 61 72 61 6e 74 65 65 64  de is guaranteed
314d6 20 74 6f 20 68 61 76 65 20 61 20 36 34 2d 62 69   to have a 64-bi
314d7 74 20 0a 2a 2a 20 69 6e 74 65 67 65 72 20 76 61  t .** integer va
314d8 6c 75 65 20 65 71 75 61 6c 20 74 6f 20 69 52 6f  lue equal to iRo
314d9 77 69 64 2e 20 52 65 74 75 72 6e 20 74 68 65 20  wid. Return the 
314da 69 6e 64 65 78 20 6f 66 20 74 68 69 73 20 63 65  index of this ce
314db 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ll..*/.static in
314dc 74 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64 65 78  t nodeRowidIndex
314dd 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20  (Rtree *pRtree, 
314de 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
314df 2c 20 69 36 34 20 69 52 6f 77 69 64 29 7b 0a 20  , i64 iRowid){. 
314e0 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72 28 69   int ii;.  for(i
314e1 69 3d 30 3b 20 6e 6f 64 65 47 65 74 52 6f 77 69  i=0; nodeGetRowi
314e2 64 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c  d(pRtree, pNode,
314e3 20 69 69 29 21 3d 69 52 6f 77 69 64 3b 20 69 69   ii)!=iRowid; ii
314e4 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  ++){.    assert(
314e5 20 69 69 3c 28 4e 43 45 4c 4c 28 70 4e 6f 64 65   ii<(NCELL(pNode
314e6 29 2d 31 29 20 29 3b 0a 20 20 7d 0a 20 20 72 65  )-1) );.  }.  re
314e7 74 75 72 6e 20 69 69 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn ii;.}../*.*
314e8 2a 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64  * Return the ind
314e9 65 78 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63  ex of the cell c
314ea 6f 6e 74 61 69 6e 69 6e 67 20 61 20 70 6f 69 6e  ontaining a poin
314eb 74 65 72 20 74 6f 20 6e 6f 64 65 20 70 4e 6f 64  ter to node pNod
314ec 65 0a 2a 2a 20 69 6e 20 69 74 73 20 70 61 72 65  e.** in its pare
314ed 6e 74 2e 20 49 66 20 70 4e 6f 64 65 20 69 73 20  nt. If pNode is 
314ee 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 2c 20 72  the root node, r
314ef 65 74 75 72 6e 20 2d 31 2e 0a 2a 2f 0a 73 74 61  eturn -1..*/.sta
314f0 74 69 63 20 69 6e 74 20 6e 6f 64 65 50 61 72 65  tic int nodePare
314f1 6e 74 49 6e 64 65 78 28 52 74 72 65 65 20 2a 70  ntIndex(Rtree *p
314f2 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
314f3 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 52 74 72 65   *pNode){.  Rtre
314f4 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 20 3d  eNode *pParent =
314f5 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3b   pNode->pParent;
314f6 0a 20 20 69 66 28 20 70 50 61 72 65 6e 74 20 29  .  if( pParent )
314f7 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 6f 64  {.    return nod
314f8 65 52 6f 77 69 64 49 6e 64 65 78 28 70 52 74 72  eRowidIndex(pRtr
314f9 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 70 4e 6f  ee, pParent, pNo
314fa 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 7d 0a  de->iNode);.  }.
314fb 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 7d 0a 0a    return -1;.}..
314fc 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69 72  /* .** Rtree vir
314fd 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c  tual table modul
314fe 65 20 78 4e 65 78 74 20 6d 65 74 68 6f 64 2e 0a  e xNext method..
314ff 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74  */.static int rt
31500 72 65 65 4e 65 78 74 28 73 71 6c 69 74 65 33 5f  reeNext(sqlite3_
31501 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74  vtab_cursor *pVt
31502 61 62 43 75 72 73 6f 72 29 7b 0a 20 20 52 74 72  abCursor){.  Rtr
31503 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74  ee *pRtree = (Rt
31504 72 65 65 20 2a 29 28 70 56 74 61 62 43 75 72 73  ree *)(pVtabCurs
31505 6f 72 2d 3e 70 56 74 61 62 29 3b 0a 20 20 52 74  or->pVtab);.  Rt
31506 72 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72 20  reeCursor *pCsr 
31507 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72 20 2a  = (RtreeCursor *
31508 29 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20  )pVtabCursor;.  
31509 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
3150a 4f 4b 3b 0a 0a 20 20 69 66 28 20 70 43 73 72 2d  OK;..  if( pCsr-
3150b 3e 69 53 74 72 61 74 65 67 79 3d 3d 31 20 29 7b  >iStrategy==1 ){
3150c 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 22 73 63  .    /* This "sc
3150d 61 6e 22 20 69 73 20 61 20 64 69 72 65 63 74 20  an" is a direct 
3150e 6c 6f 6f 6b 75 70 20 62 79 20 72 6f 77 69 64 2e  lookup by rowid.
3150f 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 78   There is no nex
31510 74 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 20 20  t entry. */.    
31511 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72  nodeRelease(pRtr
31512 65 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64 65 29  ee, pCsr->pNode)
31513 3b 0a 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64  ;.    pCsr->pNod
31514 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 65 6c  e = 0;.  }..  el
31515 73 65 20 69 66 28 20 70 43 73 72 2d 3e 70 4e 6f  se if( pCsr->pNo
31516 64 65 20 29 7b 0a 20 20 20 20 2f 2a 20 4d 6f 76  de ){.    /* Mov
31517 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 65 6e  e to the next en
31518 74 72 79 20 74 68 61 74 20 6d 61 74 63 68 65 73  try that matches
31519 20 74 68 65 20 63 6f 6e 66 69 67 75 72 65 64 20   the configured 
3151a 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 2a 2f 0a  constraints. */.
3151b 20 20 20 20 69 6e 74 20 69 48 65 69 67 68 74 20      int iHeight 
3151c 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  = 0;.    while( 
3151d 70 43 73 72 2d 3e 70 4e 6f 64 65 20 29 7b 0a 20  pCsr->pNode ){. 
3151e 20 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a       RtreeNode *
3151f 70 4e 6f 64 65 20 3d 20 70 43 73 72 2d 3e 70 4e  pNode = pCsr->pN
31520 6f 64 65 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e  ode;.      int n
31521 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f  Cell = NCELL(pNo
31522 64 65 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 70  de);.      for(p
31523 43 73 72 2d 3e 69 43 65 6c 6c 2b 2b 3b 20 70 43  Csr->iCell++; pC
31524 73 72 2d 3e 69 43 65 6c 6c 3c 6e 43 65 6c 6c 3b  sr->iCell<nCell;
31525 20 70 43 73 72 2d 3e 69 43 65 6c 6c 2b 2b 29 7b   pCsr->iCell++){
31526 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 73 45  .        int isE
31527 6f 66 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  of;.        rc =
31528 20 64 65 73 63 65 6e 64 54 6f 43 65 6c 6c 28 70   descendToCell(p
31529 52 74 72 65 65 2c 20 70 43 73 72 2c 20 69 48 65  Rtree, pCsr, iHe
3152a 69 67 68 74 2c 20 26 69 73 45 6f 66 29 3b 0a 20  ight, &isEof);. 
3152b 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
3152c 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 69 73 45  QLITE_OK || !isE
3152d 6f 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  of ){.          
3152e 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
3152f 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
31530 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20 3d     pCsr->pNode =
31531 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3b   pNode->pParent;
31532 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 69 43 65  .      pCsr->iCe
31533 6c 6c 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49  ll = nodeParentI
31534 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70 4e 6f  ndex(pRtree, pNo
31535 64 65 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 52  de);.      nodeR
31536 65 66 65 72 65 6e 63 65 28 70 43 73 72 2d 3e 70  eference(pCsr->p
31537 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 6e 6f 64  Node);.      nod
31538 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
31539 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 69   pNode);.      i
3153a 48 65 69 67 68 74 2b 2b 3b 0a 20 20 20 20 7d 0a  Height++;.    }.
3153b 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
3153c 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65  ;.}../* .** Rtre
3153d 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
3153e 6d 6f 64 75 6c 65 20 78 52 6f 77 69 64 20 6d 65  module xRowid me
3153f 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  thod..*/.static 
31540 69 6e 74 20 72 74 72 65 65 52 6f 77 69 64 28 73  int rtreeRowid(s
31541 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
31542 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 2c  or *pVtabCursor,
31543 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70   sqlite_int64 *p
31544 52 6f 77 69 64 29 7b 0a 20 20 52 74 72 65 65 20  Rowid){.  Rtree 
31545 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72 65 65  *pRtree = (Rtree
31546 20 2a 29 70 56 74 61 62 43 75 72 73 6f 72 2d 3e   *)pVtabCursor->
31547 70 56 74 61 62 3b 0a 20 20 52 74 72 65 65 43 75  pVtab;.  RtreeCu
31548 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 52 74  rsor *pCsr = (Rt
31549 72 65 65 43 75 72 73 6f 72 20 2a 29 70 56 74 61  reeCursor *)pVta
3154a 62 43 75 72 73 6f 72 3b 0a 0a 20 20 61 73 73 65  bCursor;..  asse
3154b 72 74 28 70 43 73 72 2d 3e 70 4e 6f 64 65 29 3b  rt(pCsr->pNode);
3154c 0a 20 20 2a 70 52 6f 77 69 64 20 3d 20 6e 6f 64  .  *pRowid = nod
3154d 65 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65  eGetRowid(pRtree
3154e 2c 20 70 43 73 72 2d 3e 70 4e 6f 64 65 2c 20 70  , pCsr->pNode, p
3154f 43 73 72 2d 3e 69 43 65 6c 6c 29 3b 0a 0a 20 20  Csr->iCell);..  
31550 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
31551 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65  ;.}../* .** Rtre
31552 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
31553 6d 6f 64 75 6c 65 20 78 43 6f 6c 75 6d 6e 20 6d  module xColumn m
31554 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ethod..*/.static
31555 20 69 6e 74 20 72 74 72 65 65 43 6f 6c 75 6d 6e   int rtreeColumn
31556 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75  (sqlite3_vtab_cu
31557 72 73 6f 72 20 2a 63 75 72 2c 20 73 71 6c 69 74  rsor *cur, sqlit
31558 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 74 78 2c  e3_context *ctx,
31559 20 69 6e 74 20 69 29 7b 0a 20 20 52 74 72 65 65   int i){.  Rtree
3155a 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72 65   *pRtree = (Rtre
3155b 65 20 2a 29 63 75 72 2d 3e 70 56 74 61 62 3b 0a  e *)cur->pVtab;.
3155c 20 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70    RtreeCursor *p
3155d 43 73 72 20 3d 20 28 52 74 72 65 65 43 75 72 73  Csr = (RtreeCurs
3155e 6f 72 20 2a 29 63 75 72 3b 0a 0a 20 20 69 66 28  or *)cur;..  if(
3155f 20 69 3d 3d 30 20 29 7b 0a 20 20 20 20 69 36 34   i==0 ){.    i64
31560 20 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65   iRowid = nodeGe
31561 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20 70  tRowid(pRtree, p
31562 43 73 72 2d 3e 70 4e 6f 64 65 2c 20 70 43 73 72  Csr->pNode, pCsr
31563 2d 3e 69 43 65 6c 6c 29 3b 0a 20 20 20 20 73 71  ->iCell);.    sq
31564 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
31565 36 34 28 63 74 78 2c 20 69 52 6f 77 69 64 29 3b  64(ctx, iRowid);
31566 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 52 74  .  }else{.    Rt
31567 72 65 65 43 6f 6f 72 64 20 63 3b 0a 20 20 20 20  reeCoord c;.    
31568 6e 6f 64 65 47 65 74 43 6f 6f 72 64 28 70 52 74  nodeGetCoord(pRt
31569 72 65 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64 65  ree, pCsr->pNode
3156a 2c 20 70 43 73 72 2d 3e 69 43 65 6c 6c 2c 20 69  , pCsr->iCell, i
3156b 2d 31 2c 20 26 63 29 3b 0a 20 20 20 20 69 66 28  -1, &c);.    if(
3156c 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54   pRtree->eCoordT
3156d 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f 4f 52 44  ype==RTREE_COORD
3156e 5f 52 45 41 4c 33 32 20 29 7b 0a 20 20 20 20 20  _REAL32 ){.     
3156f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
31570 64 6f 75 62 6c 65 28 63 74 78 2c 20 63 2e 66 29  double(ctx, c.f)
31571 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
31572 20 20 20 61 73 73 65 72 74 28 20 70 52 74 72 65     assert( pRtre
31573 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52  e->eCoordType==R
31574 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54 33 32  TREE_COORD_INT32
31575 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
31576 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 74 78  3_result_int(ctx
31577 2c 20 63 2e 69 29 3b 0a 20 20 20 20 7d 0a 20 20  , c.i);.    }.  
31578 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  }..  return SQLI
31579 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  TE_OK;.}../* .**
3157a 20 55 73 65 20 6e 6f 64 65 41 63 71 75 69 72 65   Use nodeAcquire
3157b 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65  () to obtain the
3157c 20 6c 65 61 66 20 6e 6f 64 65 20 63 6f 6e 74 61   leaf node conta
3157d 69 6e 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64  ining the record
3157e 20 77 69 74 68 20 0a 2a 2a 20 72 6f 77 69 64 20   with .** rowid 
3157f 69 52 6f 77 69 64 2e 20 49 66 20 73 75 63 63 65  iRowid. If succe
31580 73 73 66 75 6c 2c 20 73 65 74 20 2a 70 70 4c 65  ssful, set *ppLe
31581 61 66 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  af to point to t
31582 68 65 20 6e 6f 64 65 20 61 6e 64 0a 2a 2a 20 72  he node and.** r
31583 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e  eturn SQLITE_OK.
31584 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
31585 73 75 63 68 20 72 65 63 6f 72 64 20 69 6e 20 74  such record in t
31586 68 65 20 74 61 62 6c 65 2c 20 73 65 74 0a 2a 2a  he table, set.**
31587 20 2a 70 70 4c 65 61 66 20 74 6f 20 30 20 61 6e   *ppLeaf to 0 an
31588 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  d return SQLITE_
31589 4f 4b 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  OK. If an error 
3158a 6f 63 63 75 72 73 2c 20 73 65 74 20 2a 70 70 4c  occurs, set *ppL
3158b 65 61 66 0a 2a 2a 20 74 6f 20 7a 65 72 6f 20 61  eaf.** to zero a
3158c 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 53 51 4c  nd return an SQL
3158d 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a  ite error code..
3158e 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69  */.static int fi
3158f 6e 64 4c 65 61 66 4e 6f 64 65 28 52 74 72 65 65  ndLeafNode(Rtree
31590 20 2a 70 52 74 72 65 65 2c 20 69 36 34 20 69 52   *pRtree, i64 iR
31591 6f 77 69 64 2c 20 52 74 72 65 65 4e 6f 64 65 20  owid, RtreeNode 
31592 2a 2a 70 70 4c 65 61 66 29 7b 0a 20 20 69 6e 74  **ppLeaf){.  int
31593 20 72 63 3b 0a 20 20 2a 70 70 4c 65 61 66 20 3d   rc;.  *ppLeaf =
31594 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69   0;.  sqlite3_bi
31595 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d  nd_int64(pRtree-
31596 3e 70 52 65 61 64 52 6f 77 69 64 2c 20 31 2c 20  >pReadRowid, 1, 
31597 69 52 6f 77 69 64 29 3b 0a 20 20 69 66 28 20 73  iRowid);.  if( s
31598 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72  qlite3_step(pRtr
31599 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 3d  ee->pReadRowid)=
3159a 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
3159b 20 20 20 69 36 34 20 69 4e 6f 64 65 20 3d 20 73     i64 iNode = s
3159c 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
3159d 74 36 34 28 70 52 74 72 65 65 2d 3e 70 52 65 61  t64(pRtree->pRea
3159e 64 52 6f 77 69 64 2c 20 30 29 3b 0a 20 20 20 20  dRowid, 0);.    
3159f 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65  rc = nodeAcquire
315a0 28 70 52 74 72 65 65 2c 20 69 4e 6f 64 65 2c 20  (pRtree, iNode, 
315a1 30 2c 20 70 70 4c 65 61 66 29 3b 0a 20 20 20 20  0, ppLeaf);.    
315a2 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52  sqlite3_reset(pR
315a3 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64  tree->pReadRowid
315a4 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
315a5 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73  rc = sqlite3_res
315a6 65 74 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64  et(pRtree->pRead
315a7 52 6f 77 69 64 29 3b 0a 20 20 7d 0a 20 20 72 65  Rowid);.  }.  re
315a8 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 20  turn rc;.}.../* 
315a9 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61  .** Rtree virtua
315aa 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78  l table module x
315ab 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 2e 0a 2a  Filter method..*
315ac 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72  /.static int rtr
315ad 65 65 46 69 6c 74 65 72 28 0a 20 20 73 71 6c 69  eeFilter(.  sqli
315ae 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
315af 2a 70 56 74 61 62 43 75 72 73 6f 72 2c 20 0a 20  *pVtabCursor, . 
315b0 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f 6e   int idxNum, con
315b1 73 74 20 63 68 61 72 20 2a 69 64 78 53 74 72 2c  st char *idxStr,
315b2 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  .  int argc, sql
315b3 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
315b4 76 0a 29 7b 0a 20 20 52 74 72 65 65 20 2a 70 52  v.){.  Rtree *pR
315b5 74 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a 29  tree = (Rtree *)
315b6 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74  pVtabCursor->pVt
315b7 61 62 3b 0a 20 20 52 74 72 65 65 43 75 72 73 6f  ab;.  RtreeCurso
315b8 72 20 2a 70 43 73 72 20 3d 20 28 52 74 72 65 65  r *pCsr = (Rtree
315b9 43 75 72 73 6f 72 20 2a 29 70 56 74 61 62 43 75  Cursor *)pVtabCu
315ba 72 73 6f 72 3b 0a 0a 20 20 52 74 72 65 65 4e 6f  rsor;..  RtreeNo
315bb 64 65 20 2a 70 52 6f 6f 74 20 3d 20 30 3b 0a 20  de *pRoot = 0;. 
315bc 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 72   int ii;.  int r
315bd 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  c = SQLITE_OK;..
315be 20 20 72 74 72 65 65 52 65 66 65 72 65 6e 63 65    rtreeReference
315bf 28 70 52 74 72 65 65 29 3b 0a 0a 20 20 73 71 6c  (pRtree);..  sql
315c0 69 74 65 33 5f 66 72 65 65 28 70 43 73 72 2d 3e  ite3_free(pCsr->
315c1 61 43 6f 6e 73 74 72 61 69 6e 74 29 3b 0a 20 20  aConstraint);.  
315c2 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  pCsr->aConstrain
315c3 74 20 3d 20 30 3b 0a 20 20 70 43 73 72 2d 3e 69  t = 0;.  pCsr->i
315c4 53 74 72 61 74 65 67 79 20 3d 20 69 64 78 4e 75  Strategy = idxNu
315c5 6d 3b 0a 0a 20 20 69 66 28 20 69 64 78 4e 75 6d  m;..  if( idxNum
315c6 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 53 70  ==1 ){.    /* Sp
315c7 65 63 69 61 6c 20 63 61 73 65 20 2d 20 6c 6f 6f  ecial case - loo
315c8 6b 75 70 20 62 79 20 72 6f 77 69 64 2e 20 2a 2f  kup by rowid. */
315c9 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  .    RtreeNode *
315ca 70 4c 65 61 66 3b 20 20 20 20 20 20 20 20 2f 2a  pLeaf;        /*
315cb 20 4c 65 61 66 20 6f 6e 20 77 68 69 63 68 20 74   Leaf on which t
315cc 68 65 20 72 65 71 75 69 72 65 64 20 63 65 6c 6c  he required cell
315cd 20 72 65 73 69 64 65 73 20 2a 2f 0a 20 20 20 20   resides */.    
315ce 69 36 34 20 69 52 6f 77 69 64 20 3d 20 73 71 6c  i64 iRowid = sql
315cf 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34  ite3_value_int64
315d0 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 72  (argv[0]);.    r
315d1 63 20 3d 20 66 69 6e 64 4c 65 61 66 4e 6f 64 65  c = findLeafNode
315d2 28 70 52 74 72 65 65 2c 20 69 52 6f 77 69 64 2c  (pRtree, iRowid,
315d3 20 26 70 4c 65 61 66 29 3b 0a 20 20 20 20 70 43   &pLeaf);.    pC
315d4 73 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 4c 65 61  sr->pNode = pLea
315d5 66 3b 20 0a 20 20 20 20 69 66 28 20 70 4c 65 61  f; .    if( pLea
315d6 66 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f  f && rc==SQLITE_
315d7 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 73 72  OK ){.      pCsr
315d8 2d 3e 69 43 65 6c 6c 20 3d 20 6e 6f 64 65 52 6f  ->iCell = nodeRo
315d9 77 69 64 49 6e 64 65 78 28 70 52 74 72 65 65 2c  widIndex(pRtree,
315da 20 70 4c 65 61 66 2c 20 69 52 6f 77 69 64 29 3b   pLeaf, iRowid);
315db 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
315dc 20 20 20 20 2f 2a 20 4e 6f 72 6d 61 6c 20 63 61      /* Normal ca
315dd 73 65 20 2d 20 72 2d 74 72 65 65 20 73 63 61 6e  se - r-tree scan
315de 2e 20 53 65 74 20 75 70 20 74 68 65 20 52 74 72  . Set up the Rtr
315df 65 65 43 75 72 73 6f 72 2e 61 43 6f 6e 73 74 72  eeCursor.aConstr
315e0 61 69 6e 74 20 61 72 72 61 79 20 0a 20 20 20 20  aint array .    
315e1 2a 2a 20 77 69 74 68 20 74 68 65 20 63 6f 6e 66  ** with the conf
315e2 69 67 75 72 65 64 20 63 6f 6e 73 74 72 61 69 6e  igured constrain
315e3 74 73 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ts. .    */.    
315e4 69 66 28 20 61 72 67 63 3e 30 20 29 7b 0a 20 20  if( argc>0 ){.  
315e5 20 20 20 20 70 43 73 72 2d 3e 61 43 6f 6e 73 74      pCsr->aConst
315e6 72 61 69 6e 74 20 3d 20 73 71 6c 69 74 65 33 5f  raint = sqlite3_
315e7 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74  malloc(sizeof(Rt
315e8 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 29 2a 61  reeConstraint)*a
315e9 72 67 63 29 3b 0a 20 20 20 20 20 20 70 43 73 72  rgc);.      pCsr
315ea 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20  ->nConstraint = 
315eb 61 72 67 63 3b 0a 20 20 20 20 20 20 69 66 28 20  argc;.      if( 
315ec 21 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69  !pCsr->aConstrai
315ed 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  nt ){.        rc
315ee 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
315ef 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
315f0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 69        assert( (i
315f1 64 78 53 74 72 3d 3d 30 20 26 26 20 61 72 67 63  dxStr==0 && argc
315f2 3d 3d 30 29 20 7c 7c 20 73 74 72 6c 65 6e 28 69  ==0) || strlen(i
315f3 64 78 53 74 72 29 3d 3d 61 72 67 63 2a 32 20 29  dxStr)==argc*2 )
315f4 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 69  ;.        for(ii
315f5 3d 30 3b 20 69 69 3c 61 72 67 63 3b 20 69 69 2b  =0; ii<argc; ii+
315f6 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 52 74  +){.          Rt
315f7 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 20 2a 70  reeConstraint *p
315f8 20 3d 20 26 70 43 73 72 2d 3e 61 43 6f 6e 73 74   = &pCsr->aConst
315f9 72 61 69 6e 74 5b 69 69 5d 3b 0a 20 20 20 20 20  raint[ii];.     
315fa 20 20 20 20 20 70 2d 3e 6f 70 20 3d 20 69 64 78       p->op = idx
315fb 53 74 72 5b 69 69 2a 32 5d 3b 0a 20 20 20 20 20  Str[ii*2];.     
315fc 20 20 20 20 20 70 2d 3e 69 43 6f 6f 72 64 20 3d       p->iCoord =
315fd 20 69 64 78 53 74 72 5b 69 69 2a 32 2b 31 5d 2d   idxStr[ii*2+1]-
315fe 27 61 27 3b 0a 20 20 20 20 20 20 20 20 20 20 70  'a';.          p
315ff 2d 3e 72 56 61 6c 75 65 20 3d 20 73 71 6c 69 74  ->rValue = sqlit
31600 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28  e3_value_double(
31601 61 72 67 76 5b 69 69 5d 29 3b 0a 20 20 20 20 20  argv[ii]);.     
31602 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
31603 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 72 63   }.  .    if( rc
31604 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
31605 20 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65       pCsr->pNode
31606 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d   = 0;.      rc =
31607 20 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52 74   nodeAcquire(pRt
31608 72 65 65 2c 20 31 2c 20 30 2c 20 26 70 52 6f 6f  ree, 1, 0, &pRoo
31609 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  t);.    }.    if
3160a 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
3160b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 73 45  ){.      int isE
3160c 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 69 6e  of = 1;.      in
3160d 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28  t nCell = NCELL(
3160e 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 70 43  pRoot);.      pC
3160f 73 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 52 6f 6f  sr->pNode = pRoo
31610 74 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 43 73  t;.      for(pCs
31611 72 2d 3e 69 43 65 6c 6c 3d 30 3b 20 72 63 3d 3d  r->iCell=0; rc==
31612 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 43 73  SQLITE_OK && pCs
31613 72 2d 3e 69 43 65 6c 6c 3c 6e 43 65 6c 6c 3b 20  r->iCell<nCell; 
31614 70 43 73 72 2d 3e 69 43 65 6c 6c 2b 2b 29 7b 0a  pCsr->iCell++){.
31615 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
31616 70 43 73 72 2d 3e 70 4e 6f 64 65 3d 3d 70 52 6f  pCsr->pNode==pRo
31617 6f 74 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63  ot );.        rc
31618 20 3d 20 64 65 73 63 65 6e 64 54 6f 43 65 6c 6c   = descendToCell
31619 28 70 52 74 72 65 65 2c 20 70 43 73 72 2c 20 70  (pRtree, pCsr, p
3161a 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2c 20 26  Rtree->iDepth, &
3161b 69 73 45 6f 66 29 3b 0a 20 20 20 20 20 20 20 20  isEof);.        
3161c 69 66 28 20 21 69 73 45 6f 66 20 29 7b 0a 20 20  if( !isEof ){.  
3161d 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3161e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
3161f 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
31620 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 73 45 6f  QLITE_OK && isEo
31621 66 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  f ){.        ass
31622 65 72 74 28 20 70 43 73 72 2d 3e 70 4e 6f 64 65  ert( pCsr->pNode
31623 3d 3d 70 52 6f 6f 74 20 29 3b 0a 20 20 20 20 20  ==pRoot );.     
31624 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70     nodeRelease(p
31625 52 74 72 65 65 2c 20 70 52 6f 6f 74 29 3b 0a 20  Rtree, pRoot);. 
31626 20 20 20 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f         pCsr->pNo
31627 64 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  de = 0;.      }.
31628 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63        assert( rc
31629 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21  !=SQLITE_OK || !
3162a 70 43 73 72 2d 3e 70 4e 6f 64 65 20 7c 7c 20 70  pCsr->pNode || p
3162b 43 73 72 2d 3e 69 43 65 6c 6c 3c 4e 43 45 4c 4c  Csr->iCell<NCELL
3162c 28 70 43 73 72 2d 3e 70 4e 6f 64 65 29 20 29 3b  (pCsr->pNode) );
3162d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 74  .    }.  }..  rt
3162e 72 65 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  reeRelease(pRtre
3162f 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  e);.  return rc;
31630 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 74 72 65 65 20  .}../*.** Rtree 
31631 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
31632 64 75 6c 65 20 78 42 65 73 74 49 6e 64 65 78 20  dule xBestIndex 
31633 6d 65 74 68 6f 64 2e 20 54 68 65 72 65 20 61 72  method. There ar
31634 65 20 74 68 72 65 65 0a 2a 2a 20 74 61 62 6c 65  e three.** table
31635 20 73 63 61 6e 20 73 74 72 61 74 65 67 69 65 73   scan strategies
31636 20 74 6f 20 63 68 6f 6f 73 65 20 66 72 6f 6d 20   to choose from 
31637 28 69 6e 20 6f 72 64 65 72 20 66 72 6f 6d 20 6d  (in order from m
31638 6f 73 74 20 74 6f 20 0a 2a 2a 20 6c 65 61 73 74  ost to .** least
31639 20 64 65 73 69 72 61 62 6c 65 29 3a 0a 2a 2a 0a   desirable):.**.
3163a 2a 2a 20 20 20 69 64 78 4e 75 6d 20 20 20 20 20  **   idxNum     
3163b 69 64 78 53 74 72 20 20 20 20 20 20 20 20 53 74  idxStr        St
3163c 72 61 74 65 67 79 0a 2a 2a 20 20 20 2d 2d 2d 2d  rategy.**   ----
3163d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3163e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3163f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20  ------------.** 
31640 20 20 20 20 31 20 20 20 20 20 20 20 20 55 6e 75      1        Unu
31641 73 65 64 20 20 20 20 20 20 20 20 44 69 72 65 63  sed        Direc
31642 74 20 6c 6f 6f 6b 75 70 20 62 79 20 72 6f 77 69  t lookup by rowi
31643 64 2e 0a 2a 2a 20 20 20 20 20 32 20 20 20 20 20  d..**     2     
31644 20 20 20 53 65 65 20 62 65 6c 6f 77 20 20 20 20     See below    
31645 20 52 2d 74 72 65 65 20 71 75 65 72 79 2e 0a 2a   R-tree query..*
31646 2a 20 20 20 20 20 33 20 20 20 20 20 20 20 20 55  *     3        U
31647 6e 75 73 65 64 20 20 20 20 20 20 20 20 46 75 6c  nused        Ful
31648 6c 20 74 61 62 6c 65 20 73 63 61 6e 2e 0a 2a 2a  l table scan..**
31649 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
3164a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3164b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3164c 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 49 66 20 73 74 72  ---.**.** If str
3164d 61 74 65 67 79 20 31 20 6f 72 20 33 20 69 73 20  ategy 1 or 3 is 
3164e 75 73 65 64 2c 20 74 68 65 6e 20 69 64 78 53 74  used, then idxSt
3164f 72 20 69 73 20 6e 6f 74 20 6d 65 61 6e 69 6e 67  r is not meaning
31650 66 75 6c 2e 20 49 66 20 73 74 72 61 74 65 67 79  ful. If strategy
31651 0a 2a 2a 20 32 20 69 73 20 75 73 65 64 2c 20 69  .** 2 is used, i
31652 64 78 53 74 72 20 69 73 20 66 6f 72 6d 61 74 74  dxStr is formatt
31653 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 32 20  ed to contain 2 
31654 62 79 74 65 73 20 66 6f 72 20 65 61 63 68 20 0a  bytes for each .
31655 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 75 73  ** constraint us
31656 65 64 2e 20 54 68 65 20 66 69 72 73 74 20 74 77  ed. The first tw
31657 6f 20 62 79 74 65 73 20 6f 66 20 69 64 78 53 74  o bytes of idxSt
31658 72 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20  r correspond to 
31659 0a 2a 2a 20 74 68 65 20 63 6f 6e 73 74 72 61 69  .** the constrai
3165a 6e 74 20 69 6e 20 73 71 6c 69 74 65 33 5f 69 6e  nt in sqlite3_in
3165b 64 65 78 5f 69 6e 66 6f 2e 61 43 6f 6e 73 74 72  dex_info.aConstr
3165c 61 69 6e 74 55 73 61 67 65 5b 5d 20 77 69 74 68  aintUsage[] with
3165d 0a 2a 2a 20 28 61 72 67 76 49 6e 64 65 78 3d 3d  .** (argvIndex==
3165e 31 29 20 65 74 63 2e 0a 2a 2a 0a 2a 2a 20 54 68  1) etc..**.** Th
3165f 65 20 66 69 72 73 74 20 6f 66 20 65 61 63 68 20  e first of each 
31660 70 61 69 72 20 6f 66 20 62 79 74 65 73 20 69 6e  pair of bytes in
31661 20 69 64 78 53 74 72 20 69 64 65 6e 74 69 66 69   idxStr identifi
31662 65 73 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  es the constrain
31663 74 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 61 73  t.** operator as
31664 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
31665 20 20 4f 70 65 72 61 74 6f 72 20 20 20 20 42 79    Operator    By
31666 74 65 20 56 61 6c 75 65 0a 2a 2a 20 20 20 2d 2d  te Value.**   --
31667 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
31668 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 3d 20 20  ----.**      =  
31669 20 20 20 20 20 20 30 78 34 31 20 28 27 41 27 29        0x41 ('A')
3166a 0a 2a 2a 20 20 20 20 20 3c 3d 20 20 20 20 20 20  .**     <=      
3166b 20 20 30 78 34 32 20 28 27 42 27 29 0a 2a 2a 20    0x42 ('B').** 
3166c 20 20 20 20 20 3c 20 20 20 20 20 20 20 20 30 78       <        0x
3166d 34 33 20 28 27 43 27 29 0a 2a 2a 20 20 20 20 20  43 ('C').**     
3166e 3e 3d 20 20 20 20 20 20 20 20 30 78 34 34 20 28  >=        0x44 (
3166f 27 44 27 29 0a 2a 2a 20 20 20 20 20 20 3e 20 20  'D').**      >  
31670 20 20 20 20 20 20 30 78 34 35 20 28 27 45 27 29        0x45 ('E')
31671 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .**   ----------
31672 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a  ------------.**.
31673 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 6f 66  ** The second of
31674 20 65 61 63 68 20 70 61 69 72 20 6f 66 20 62 79   each pair of by
31675 74 65 73 20 69 64 65 6e 74 69 66 69 65 73 20 74  tes identifies t
31676 68 65 20 63 6f 6f 72 64 69 6e 61 74 65 20 63 6f  he coordinate co
31677 6c 75 6d 6e 0a 2a 2a 20 74 6f 20 77 68 69 63 68  lumn.** to which
31678 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20   the constraint 
31679 61 70 70 6c 69 65 73 2e 20 54 68 65 20 6c 65 66  applies. The lef
3167a 74 6d 6f 73 74 20 63 6f 6f 72 64 69 6e 61 74 65  tmost coordinate
3167b 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20 27 61   column.** is 'a
3167c 27 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 66 72  ', the second fr
3167d 6f 6d 20 74 68 65 20 6c 65 66 74 20 27 62 27 20  om the left 'b' 
3167e 65 74 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  etc..*/.static i
3167f 6e 74 20 72 74 72 65 65 42 65 73 74 49 6e 64 65  nt rtreeBestInde
31680 78 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  x(sqlite3_vtab *
31681 74 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64  tab, sqlite3_ind
31682 65 78 5f 69 6e 66 6f 20 2a 70 49 64 78 49 6e 66  ex_info *pIdxInf
31683 6f 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  o){.  int rc = S
31684 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20  QLITE_OK;.  int 
31685 69 69 2c 20 63 43 6f 6c 3b 0a 0a 20 20 69 6e 74  ii, cCol;..  int
31686 20 69 49 64 78 20 3d 20 30 3b 0a 20 20 63 68 61   iIdx = 0;.  cha
31687 72 20 7a 49 64 78 53 74 72 5b 52 54 52 45 45 5f  r zIdxStr[RTREE_
31688 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 2a 38  MAX_DIMENSIONS*8
31689 2b 31 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 49  +1];.  memset(zI
3168a 64 78 53 74 72 2c 20 30 2c 20 73 69 7a 65 6f 66  dxStr, 0, sizeof
3168b 28 7a 49 64 78 53 74 72 29 29 3b 0a 0a 20 20 61  (zIdxStr));..  a
3168c 73 73 65 72 74 28 20 70 49 64 78 49 6e 66 6f 2d  ssert( pIdxInfo-
3168d 3e 69 64 78 53 74 72 3d 3d 30 20 29 3b 0a 20 20  >idxStr==0 );.  
3168e 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 49 64  for(ii=0; ii<pId
3168f 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69  xInfo->nConstrai
31690 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 73  nt; ii++){.    s
31691 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
31692 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 2a  dex_constraint *
31693 70 20 3d 20 26 70 49 64 78 49 6e 66 6f 2d 3e 61  p = &pIdxInfo->a
31694 43 6f 6e 73 74 72 61 69 6e 74 5b 69 69 5d 3b 0a  Constraint[ii];.
31695 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 73 61 62  .    if( p->usab
31696 6c 65 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e  le && p->iColumn
31697 3d 3d 30 20 26 26 20 70 2d 3e 6f 70 3d 3d 53 51  ==0 && p->op==SQ
31698 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
31699 52 41 49 4e 54 5f 45 51 20 29 7b 0a 20 20 20 20  RAINT_EQ ){.    
3169a 20 20 2f 2a 20 57 65 20 68 61 76 65 20 61 6e 20    /* We have an 
3169b 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
3169c 69 6e 74 20 6f 6e 20 74 68 65 20 72 6f 77 69 64  int on the rowid
3169d 2e 20 55 73 65 20 73 74 72 61 74 65 67 79 20 31  . Use strategy 1
3169e 2e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a  . */.      int j
3169f 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d  j;.      for(jj=
316a0 30 3b 20 6a 6a 3c 69 69 3b 20 6a 6a 2b 2b 29 7b  0; jj<ii; jj++){
316a1 0a 20 20 20 20 20 20 20 20 70 49 64 78 49 6e 66  .        pIdxInf
316a2 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73  o->aConstraintUs
316a3 61 67 65 5b 6a 6a 5d 2e 61 72 67 76 49 6e 64 65  age[jj].argvInde
316a4 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70  x = 0;.        p
316a5 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72  IdxInfo->aConstr
316a6 61 69 6e 74 55 73 61 67 65 5b 6a 6a 5d 2e 6f 6d  aintUsage[jj].om
316a7 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  it = 0;.      }.
316a8 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e        pIdxInfo->
316a9 69 64 78 4e 75 6d 20 3d 20 31 3b 0a 20 20 20 20  idxNum = 1;.    
316aa 20 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e    pIdxInfo->aCon
316ab 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69 69 5d  straintUsage[ii]
316ac 2e 61 72 67 76 49 6e 64 65 78 20 3d 20 31 3b 0a  .argvIndex = 1;.
316ad 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e        pIdxInfo->
316ae 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
316af 5b 6a 6a 5d 2e 6f 6d 69 74 20 3d 20 31 3b 0a 0a  [jj].omit = 1;..
316b0 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 73 74        /* This st
316b1 72 61 74 65 67 79 20 69 6e 76 6f 6c 76 65 73 20  rategy involves 
316b2 61 20 74 77 6f 20 72 6f 77 69 64 20 6c 6f 6f 6b  a two rowid look
316b3 75 70 73 20 6f 6e 20 61 6e 20 42 2d 54 72 65 65  ups on an B-Tree
316b4 20 73 74 72 75 63 74 75 72 65 73 0a 20 20 20 20   structures.    
316b5 20 20 2a 2a 20 61 6e 64 20 74 68 65 6e 20 61 20    ** and then a 
316b6 6c 69 6e 65 61 72 20 73 65 61 72 63 68 20 6f 66  linear search of
316b7 20 61 6e 20 52 2d 54 72 65 65 20 6e 6f 64 65 2e   an R-Tree node.
316b8 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20   This should be 
316b9 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73 69 64  .      ** consid
316ba 65 72 65 64 20 61 6c 6d 6f 73 74 20 61 73 20 71  ered almost as q
316bb 75 69 63 6b 20 61 73 20 61 20 64 69 72 65 63 74  uick as a direct
316bc 20 72 6f 77 69 64 20 6c 6f 6f 6b 75 70 20 28 66   rowid lookup (f
316bd 6f 72 20 77 68 69 63 68 20 0a 20 20 20 20 20 20  or which .      
316be 2a 2a 20 73 71 6c 69 74 65 20 75 73 65 73 20 61  ** sqlite uses a
316bf 6e 20 69 6e 74 65 72 6e 61 6c 20 63 6f 73 74 20  n internal cost 
316c0 6f 66 20 30 2e 30 29 2e 0a 20 20 20 20 20 20 2a  of 0.0)..      *
316c1 2f 20 0a 20 20 20 20 20 20 70 49 64 78 49 6e 66  / .      pIdxInf
316c2 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74  o->estimatedCost
316c3 20 3d 20 31 30 2e 30 3b 0a 20 20 20 20 20 20 72   = 10.0;.      r
316c4 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
316c5 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
316c6 70 2d 3e 75 73 61 62 6c 65 20 26 26 20 70 2d 3e  p->usable && p->
316c7 69 43 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20  iColumn>0 ){.   
316c8 20 20 20 75 38 20 6f 70 20 3d 20 30 3b 0a 20 20     u8 op = 0;.  
316c9 20 20 20 20 73 77 69 74 63 68 28 20 70 2d 3e 6f      switch( p->o
316ca 70 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73  p ){.        cas
316cb 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  e SQLITE_INDEX_C
316cc 4f 4e 53 54 52 41 49 4e 54 5f 45 51 3a 20 6f 70  ONSTRAINT_EQ: op
316cd 20 3d 20 52 54 52 45 45 5f 45 51 3b 20 62 72 65   = RTREE_EQ; bre
316ce 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
316cf 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f   SQLITE_INDEX_CO
316d0 4e 53 54 52 41 49 4e 54 5f 47 54 3a 20 6f 70 20  NSTRAINT_GT: op 
316d1 3d 20 52 54 52 45 45 5f 47 54 3b 20 62 72 65 61  = RTREE_GT; brea
316d2 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k;.        case 
316d3 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
316d4 53 54 52 41 49 4e 54 5f 4c 45 3a 20 6f 70 20 3d  STRAINT_LE: op =
316d5 20 52 54 52 45 45 5f 4c 45 3b 20 62 72 65 61 6b   RTREE_LE; break
316d6 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 53  ;.        case S
316d7 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
316d8 54 52 41 49 4e 54 5f 4c 54 3a 20 6f 70 20 3d 20  TRAINT_LT: op = 
316d9 52 54 52 45 45 5f 4c 54 3b 20 62 72 65 61 6b 3b  RTREE_LT; break;
316da 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 53 51  .        case SQ
316db 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
316dc 52 41 49 4e 54 5f 47 45 3a 20 6f 70 20 3d 20 52  RAINT_GE: op = R
316dd 54 52 45 45 5f 47 45 3b 20 62 72 65 61 6b 3b 0a  TREE_GE; break;.
316de 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
316df 28 20 6f 70 20 29 7b 0a 20 20 20 20 20 20 20 20  ( op ){.        
316e0 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69  /* Make sure thi
316e1 73 20 70 61 72 74 69 63 75 6c 61 72 20 63 6f 6e  s particular con
316e2 73 74 72 61 69 6e 74 20 68 61 73 20 6e 6f 74 20  straint has not 
316e3 62 65 65 6e 20 75 73 65 64 20 62 65 66 6f 72 65  been used before
316e4 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 66 20  ..        ** If 
316e5 69 74 20 68 61 73 20 62 65 65 6e 20 75 73 65 64  it has been used
316e6 20 62 65 66 6f 72 65 2c 20 69 67 6e 6f 72 65 20   before, ignore 
316e7 69 74 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  it..        **. 
316e8 20 20 20 20 20 20 20 2a 2a 20 41 20 3c 3d 20 6f         ** A <= o
316e9 72 20 3c 20 63 61 6e 20 62 65 20 75 73 65 64 20  r < can be used 
316ea 69 66 20 74 68 65 72 65 20 69 73 20 61 20 70 72  if there is a pr
316eb 69 6f 72 20 3e 3d 20 6f 72 20 3e 2e 0a 20 20 20  ior >= or >..   
316ec 20 20 20 20 20 2a 2a 20 41 20 3e 3d 20 6f 72 20       ** A >= or 
316ed 3e 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 66  > can be used if
316ee 20 74 68 65 72 65 20 69 73 20 61 20 70 72 69 6f   there is a prio
316ef 72 20 3c 20 6f 72 20 3c 3d 2e 0a 20 20 20 20 20  r < or <=..     
316f0 20 20 20 2a 2a 20 41 20 3c 3d 20 6f 72 20 3c 20     ** A <= or < 
316f1 69 73 20 64 69 73 71 75 61 6c 69 66 69 65 64 20  is disqualified 
316f2 69 66 20 74 68 65 72 65 20 69 73 20 61 20 70 72  if there is a pr
316f3 69 6f 72 20 3c 3d 2c 20 3c 2c 20 6f 72 20 3d 3d  ior <=, <, or ==
316f4 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 41 20 3e  ..        ** A >
316f5 3d 20 6f 72 20 3e 20 69 73 20 64 69 73 71 75 61  = or > is disqua
316f6 6c 69 66 69 65 64 20 69 66 20 74 68 65 72 65 20  lified if there 
316f7 69 73 20 61 20 70 72 69 6f 72 20 3e 3d 2c 20 3e  is a prior >=, >
316f8 2c 20 6f 72 20 3d 3d 2e 0a 20 20 20 20 20 20 20  , or ==..       
316f9 20 2a 2a 20 41 20 3d 3d 20 69 73 20 64 69 73 71   ** A == is disq
316fa 75 61 6c 69 66 65 64 20 69 66 20 74 68 65 72 65  ualifed if there
316fb 20 69 73 20 61 6e 79 20 70 72 69 6f 72 20 63 6f   is any prior co
316fc 6e 73 74 72 61 69 6e 74 2e 0a 20 20 20 20 20 20  nstraint..      
316fd 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74    */.        int
316fe 20 6a 2c 20 6f 70 6d 73 6b 3b 0a 20 20 20 20 20   j, opmsk;.     
316ff 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
31700 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f  unsigned char co
31701 6d 70 61 74 69 62 6c 65 5b 5d 20 3d 20 7b 20 30  mpatible[] = { 0
31702 2c 20 30 2c 20 31 2c 20 31 2c 20 32 2c 20 32 20  , 0, 1, 1, 2, 2 
31703 7d 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  };.        asser
31704 74 28 20 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54  t( compatible[RT
31705 52 45 45 5f 45 51 20 26 20 37 5d 3d 3d 30 20 29  REE_EQ & 7]==0 )
31706 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
31707 28 20 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52  ( compatible[RTR
31708 45 45 5f 4c 54 20 26 20 37 5d 3d 3d 31 20 29 3b  EE_LT & 7]==1 );
31709 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
3170a 20 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52 45   compatible[RTRE
3170b 45 5f 4c 45 20 26 20 37 5d 3d 3d 31 20 29 3b 0a  E_LE & 7]==1 );.
3170c 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
3170d 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52 45 45  compatible[RTREE
3170e 5f 47 54 20 26 20 37 5d 3d 3d 32 20 29 3b 0a 20  _GT & 7]==2 );. 
3170f 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 63         assert( c
31710 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52 45 45 5f  ompatible[RTREE_
31711 47 45 20 26 20 37 5d 3d 3d 32 20 29 3b 0a 20 20  GE & 7]==2 );.  
31712 20 20 20 20 20 20 63 43 6f 6c 20 3d 20 70 2d 3e        cCol = p->
31713 69 43 6f 6c 75 6d 6e 20 2d 20 31 20 2b 20 27 61  iColumn - 1 + 'a
31714 27 3b 0a 20 20 20 20 20 20 20 20 6f 70 6d 73 6b  ';.        opmsk
31715 20 3d 20 63 6f 6d 70 61 74 69 62 6c 65 5b 6f 70   = compatible[op
31716 20 26 20 37 5d 3b 0a 20 20 20 20 20 20 20 20 66   & 7];.        f
31717 6f 72 28 6a 3d 30 3b 20 6a 3c 69 49 64 78 3b 20  or(j=0; j<iIdx; 
31718 6a 2b 3d 32 29 7b 0a 20 20 20 20 20 20 20 20 20  j+=2){.         
31719 20 69 66 28 20 7a 49 64 78 53 74 72 5b 6a 2b 31   if( zIdxStr[j+1
3171a 5d 3d 3d 63 43 6f 6c 20 26 26 20 28 63 6f 6d 70  ]==cCol && (comp
3171b 61 74 69 62 6c 65 5b 7a 49 64 78 53 74 72 5b 6a  atible[zIdxStr[j
3171c 5d 20 26 20 37 5d 20 26 20 6f 70 6d 73 6b 29 21  ] & 7] & opmsk)!
3171d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
3171e 20 20 6f 70 20 3d 20 30 3b 0a 20 20 20 20 20 20    op = 0;.      
3171f 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31720 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
31721 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
31722 20 69 66 28 20 6f 70 20 29 7b 0a 20 20 20 20 20   if( op ){.     
31723 20 20 20 61 73 73 65 72 74 28 20 69 49 64 78 3c     assert( iIdx<
31724 73 69 7a 65 6f 66 28 7a 49 64 78 53 74 72 29 2d  sizeof(zIdxStr)-
31725 31 20 29 3b 0a 20 20 20 20 20 20 20 20 7a 49 64  1 );.        zId
31726 78 53 74 72 5b 69 49 64 78 2b 2b 5d 20 3d 20 6f  xStr[iIdx++] = o
31727 70 3b 0a 20 20 20 20 20 20 20 20 7a 49 64 78 53  p;.        zIdxS
31728 74 72 5b 69 49 64 78 2b 2b 5d 20 3d 20 63 43 6f  tr[iIdx++] = cCo
31729 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 64 78 49  l;.        pIdxI
3172a 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  nfo->aConstraint
3172b 55 73 61 67 65 5b 69 69 5d 2e 61 72 67 76 49 6e  Usage[ii].argvIn
3172c 64 65 78 20 3d 20 28 69 49 64 78 2f 32 29 3b 0a  dex = (iIdx/2);.
3172d 20 20 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f          pIdxInfo
3172e 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61  ->aConstraintUsa
3172f 67 65 5b 69 69 5d 2e 6f 6d 69 74 20 3d 20 31 3b  ge[ii].omit = 1;
31730 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
31731 20 7d 0a 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e   }..  pIdxInfo->
31732 69 64 78 4e 75 6d 20 3d 20 32 3b 0a 20 20 70 49  idxNum = 2;.  pI
31733 64 78 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 72  dxInfo->needToFr
31734 65 65 49 64 78 53 74 72 20 3d 20 31 3b 0a 20 20  eeIdxStr = 1;.  
31735 69 66 28 20 69 49 64 78 3e 30 20 26 26 20 30 3d  if( iIdx>0 && 0=
31736 3d 28 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78 53  =(pIdxInfo->idxS
31737 74 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  tr = sqlite3_mpr
31738 69 6e 74 66 28 22 25 73 22 2c 20 7a 49 64 78 53  intf("%s", zIdxS
31739 74 72 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75  tr)) ){.    retu
3173a 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
3173b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69  .  }.  assert( i
3173c 49 64 78 3e 3d 30 20 29 3b 0a 20 20 70 49 64 78  Idx>=0 );.  pIdx
3173d 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43  Info->estimatedC
3173e 6f 73 74 20 3d 20 28 32 30 30 30 30 30 30 2e 30  ost = (2000000.0
3173f 20 2f 20 28 64 6f 75 62 6c 65 29 28 69 49 64 78   / (double)(iIdx
31740 20 2b 20 31 29 29 3b 0a 20 20 72 65 74 75 72 6e   + 1));.  return
31741 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
31742 74 75 72 6e 20 74 68 65 20 4e 2d 64 69 6d 65 6e  turn the N-dimen
31743 73 69 6f 6e 61 6c 20 76 6f 6c 75 6d 6e 20 6f 66  sional volumn of
31744 20 74 68 65 20 63 65 6c 6c 20 73 74 6f 72 65 64   the cell stored
31745 20 69 6e 20 2a 70 2e 0a 2a 2f 0a 73 74 61 74 69   in *p..*/.stati
31746 63 20 66 6c 6f 61 74 20 63 65 6c 6c 41 72 65 61  c float cellArea
31747 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20  (Rtree *pRtree, 
31748 52 74 72 65 65 43 65 6c 6c 20 2a 70 29 7b 0a 20  RtreeCell *p){. 
31749 20 66 6c 6f 61 74 20 61 72 65 61 20 3d 20 31 2e   float area = 1.
3174a 30 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 66  0;.  int ii;.  f
3174b 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74  or(ii=0; ii<(pRt
3174c 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69  ree->nDim*2); ii
3174d 2b 3d 32 29 7b 0a 20 20 20 20 61 72 65 61 20 3d  +=2){.    area =
3174e 20 61 72 65 61 20 2a 20 28 44 43 4f 4f 52 44 28   area * (DCOORD(
3174f 70 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 29  p->aCoord[ii+1])
31750 20 2d 20 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f   - DCOORD(p->aCo
31751 6f 72 64 5b 69 69 5d 29 29 3b 0a 20 20 7d 0a 20  ord[ii]));.  }. 
31752 20 72 65 74 75 72 6e 20 61 72 65 61 3b 0a 7d 0a   return area;.}.
31753 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
31754 65 20 6d 61 72 67 69 6e 20 6c 65 6e 67 74 68 20  e margin length 
31755 6f 66 20 63 65 6c 6c 20 70 2e 20 54 68 65 20 6d  of cell p. The m
31756 61 72 67 69 6e 20 6c 65 6e 67 74 68 20 69 73 20  argin length is 
31757 74 68 65 20 73 75 6d 0a 2a 2a 20 6f 66 20 74 68  the sum.** of th
31758 65 20 6f 62 6a 65 63 74 73 20 73 69 7a 65 20 69  e objects size i
31759 6e 20 65 61 63 68 20 64 69 6d 65 6e 73 69 6f 6e  n each dimension
3175a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 66 6c 6f 61  ..*/.static floa
3175b 74 20 63 65 6c 6c 4d 61 72 67 69 6e 28 52 74 72  t cellMargin(Rtr
3175c 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
3175d 65 43 65 6c 6c 20 2a 70 29 7b 0a 20 20 66 6c 6f  eCell *p){.  flo
3175e 61 74 20 6d 61 72 67 69 6e 20 3d 20 30 2e 30 3b  at margin = 0.0;
3175f 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72  .  int ii;.  for
31760 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72 65  (ii=0; ii<(pRtre
31761 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d  e->nDim*2); ii+=
31762 32 29 7b 0a 20 20 20 20 6d 61 72 67 69 6e 20 2b  2){.    margin +
31763 3d 20 28 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f  = (DCOORD(p->aCo
31764 6f 72 64 5b 69 69 2b 31 5d 29 20 2d 20 44 43 4f  ord[ii+1]) - DCO
31765 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64 5b 69 69  ORD(p->aCoord[ii
31766 5d 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ]));.  }.  retur
31767 6e 20 6d 61 72 67 69 6e 3b 0a 7d 0a 0a 2f 2a 0a  n margin;.}../*.
31768 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 75 6e 69  ** Store the uni
31769 6f 6e 20 6f 66 20 63 65 6c 6c 73 20 70 31 20 61  on of cells p1 a
3176a 6e 64 20 70 32 20 69 6e 20 70 31 2e 0a 2a 2f 0a  nd p2 in p1..*/.
3176b 73 74 61 74 69 63 20 76 6f 69 64 20 63 65 6c 6c  static void cell
3176c 55 6e 69 6f 6e 28 52 74 72 65 65 20 2a 70 52 74  Union(Rtree *pRt
3176d 72 65 65 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a  ree, RtreeCell *
3176e 70 31 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70  p1, RtreeCell *p
3176f 32 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20  2){.  int ii;.  
31770 69 66 28 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f  if( pRtree->eCoo
31771 72 64 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f  rdType==RTREE_CO
31772 4f 52 44 5f 52 45 41 4c 33 32 20 29 7b 0a 20 20  ORD_REAL32 ){.  
31773 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28    for(ii=0; ii<(
31774 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b  pRtree->nDim*2);
31775 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 70   ii+=2){.      p
31776 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 20  1->aCoord[ii].f 
31777 3d 20 4d 49 4e 28 70 31 2d 3e 61 43 6f 6f 72 64  = MIN(p1->aCoord
31778 5b 69 69 5d 2e 66 2c 20 70 32 2d 3e 61 43 6f 6f  [ii].f, p2->aCoo
31779 72 64 5b 69 69 5d 2e 66 29 3b 0a 20 20 20 20 20  rd[ii].f);.     
3177a 20 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31   p1->aCoord[ii+1
3177b 5d 2e 66 20 3d 20 4d 41 58 28 70 31 2d 3e 61 43  ].f = MAX(p1->aC
3177c 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66 2c 20 70 32  oord[ii+1].f, p2
3177d 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66  ->aCoord[ii+1].f
3177e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
3177f 7b 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20  {.    for(ii=0; 
31780 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d  ii<(pRtree->nDim
31781 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20  *2); ii+=2){.   
31782 20 20 20 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69     p1->aCoord[ii
31783 5d 2e 69 20 3d 20 4d 49 4e 28 70 31 2d 3e 61 43  ].i = MIN(p1->aC
31784 6f 6f 72 64 5b 69 69 5d 2e 69 2c 20 70 32 2d 3e  oord[ii].i, p2->
31785 61 43 6f 6f 72 64 5b 69 69 5d 2e 69 29 3b 0a 20  aCoord[ii].i);. 
31786 20 20 20 20 20 70 31 2d 3e 61 43 6f 6f 72 64 5b       p1->aCoord[
31787 69 69 2b 31 5d 2e 69 20 3d 20 4d 41 58 28 70 31  ii+1].i = MAX(p1
31788 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 69  ->aCoord[ii+1].i
31789 2c 20 70 32 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b  , p2->aCoord[ii+
3178a 31 5d 2e 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  1].i);.    }.  }
3178b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
3178c 20 74 72 75 65 20 69 66 20 74 68 65 20 61 72 65   true if the are
3178d 61 20 63 6f 76 65 72 65 64 20 62 79 20 70 32 20  a covered by p2 
3178e 69 73 20 61 20 73 75 62 73 65 74 20 6f 66 20 74  is a subset of t
3178f 68 65 20 61 72 65 61 20 63 6f 76 65 72 65 64 0a  he area covered.
31790 2a 2a 20 62 79 20 70 31 2e 20 46 61 6c 73 65 20  ** by p1. False 
31791 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74  otherwise..*/.st
31792 61 74 69 63 20 69 6e 74 20 63 65 6c 6c 43 6f 6e  atic int cellCon
31793 74 61 69 6e 73 28 52 74 72 65 65 20 2a 70 52 74  tains(Rtree *pRt
31794 72 65 65 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a  ree, RtreeCell *
31795 70 31 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70  p1, RtreeCell *p
31796 32 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20  2){.  int ii;.  
31797 69 6e 74 20 69 73 49 6e 74 20 3d 20 28 70 52 74  int isInt = (pRt
31798 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d  ree->eCoordType=
31799 3d 52 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54  =RTREE_COORD_INT
3179a 33 32 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b  32);.  for(ii=0;
3179b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69   ii<(pRtree->nDi
3179c 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20  m*2); ii+=2){.  
3179d 20 20 52 74 72 65 65 43 6f 6f 72 64 20 2a 61 31    RtreeCoord *a1
3179e 20 3d 20 26 70 31 2d 3e 61 43 6f 6f 72 64 5b 69   = &p1->aCoord[i
3179f 69 5d 3b 0a 20 20 20 20 52 74 72 65 65 43 6f 6f  i];.    RtreeCoo
317a0 72 64 20 2a 61 32 20 3d 20 26 70 32 2d 3e 61 43  rd *a2 = &p2->aC
317a1 6f 6f 72 64 5b 69 69 5d 3b 0a 20 20 20 20 69 66  oord[ii];.    if
317a2 28 20 28 21 69 73 49 6e 74 20 26 26 20 28 61 32  ( (!isInt && (a2
317a3 5b 30 5d 2e 66 3c 61 31 5b 30 5d 2e 66 20 7c 7c  [0].f<a1[0].f ||
317a4 20 61 32 5b 31 5d 2e 66 3e 61 31 5b 31 5d 2e 66   a2[1].f>a1[1].f
317a5 29 29 20 0a 20 20 20 20 20 7c 7c 20 28 20 69 73  )) .     || ( is
317a6 49 6e 74 20 26 26 20 28 61 32 5b 30 5d 2e 69 3c  Int && (a2[0].i<
317a7 61 31 5b 30 5d 2e 69 20 7c 7c 20 61 32 5b 31 5d  a1[0].i || a2[1]
317a8 2e 69 3e 61 31 5b 31 5d 2e 69 29 29 20 0a 20 20  .i>a1[1].i)) .  
317a9 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72    ){.      retur
317aa 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n 0;.    }.  }. 
317ab 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a   return 1;.}../*
317ac 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 61  .** Return the a
317ad 6d 6f 75 6e 74 20 63 65 6c 6c 20 70 20 77 6f 75  mount cell p wou
317ae 6c 64 20 67 72 6f 77 20 62 79 20 69 66 20 69 74  ld grow by if it
317af 20 77 65 72 65 20 75 6e 69 6f 6e 65 64 20 77 69   were unioned wi
317b0 74 68 20 70 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61  th pCell..*/.sta
317b1 74 69 63 20 66 6c 6f 61 74 20 63 65 6c 6c 47 72  tic float cellGr
317b2 6f 77 74 68 28 52 74 72 65 65 20 2a 70 52 74 72  owth(Rtree *pRtr
317b3 65 65 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70  ee, RtreeCell *p
317b4 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65  , RtreeCell *pCe
317b5 6c 6c 29 7b 0a 20 20 66 6c 6f 61 74 20 61 72 65  ll){.  float are
317b6 61 3b 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 63  a;.  RtreeCell c
317b7 65 6c 6c 3b 0a 20 20 6d 65 6d 63 70 79 28 26 63  ell;.  memcpy(&c
317b8 65 6c 6c 2c 20 70 2c 20 73 69 7a 65 6f 66 28 52  ell, p, sizeof(R
317b9 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 61 72  treeCell));.  ar
317ba 65 61 20 3d 20 63 65 6c 6c 41 72 65 61 28 70 52  ea = cellArea(pR
317bb 74 72 65 65 2c 20 26 63 65 6c 6c 29 3b 0a 20 20  tree, &cell);.  
317bc 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65  cellUnion(pRtree
317bd 2c 20 26 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 3b  , &cell, pCell);
317be 0a 20 20 72 65 74 75 72 6e 20 28 63 65 6c 6c 41  .  return (cellA
317bf 72 65 61 28 70 52 74 72 65 65 2c 20 26 63 65 6c  rea(pRtree, &cel
317c0 6c 29 2d 61 72 65 61 29 3b 0a 7d 0a 0a 23 69 66  l)-area);.}..#if
317c1 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52   VARIANT_RSTARTR
317c2 45 45 5f 43 48 4f 4f 53 45 53 55 42 54 52 45 45  EE_CHOOSESUBTREE
317c3 20 7c 7c 20 56 41 52 49 41 4e 54 5f 52 53 54 41   || VARIANT_RSTA
317c4 52 54 52 45 45 5f 53 50 4c 49 54 0a 73 74 61 74  RTREE_SPLIT.stat
317c5 69 63 20 66 6c 6f 61 74 20 63 65 6c 6c 4f 76 65  ic float cellOve
317c6 72 6c 61 70 28 0a 20 20 52 74 72 65 65 20 2a 70  rlap(.  Rtree *p
317c7 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65 43  Rtree, .  RtreeC
317c8 65 6c 6c 20 2a 70 2c 20 0a 20 20 52 74 72 65 65  ell *p, .  Rtree
317c9 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20  Cell *aCell, .  
317ca 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e  int nCell, .  in
317cb 74 20 69 45 78 63 6c 75 64 65 0a 29 7b 0a 20 20  t iExclude.){.  
317cc 69 6e 74 20 69 69 3b 0a 20 20 66 6c 6f 61 74 20  int ii;.  float 
317cd 6f 76 65 72 6c 61 70 20 3d 20 30 2e 30 3b 0a 20  overlap = 0.0;. 
317ce 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43   for(ii=0; ii<nC
317cf 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  ell; ii++){.    
317d0 69 66 28 20 69 69 21 3d 69 45 78 63 6c 75 64 65  if( ii!=iExclude
317d1 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 6a   ){.      int jj
317d2 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 6f 20  ;.      float o 
317d3 3d 20 31 2e 30 3b 0a 20 20 20 20 20 20 66 6f 72  = 1.0;.      for
317d4 28 6a 6a 3d 30 3b 20 6a 6a 3c 28 70 52 74 72 65  (jj=0; jj<(pRtre
317d5 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 6a 6a 2b 3d  e->nDim*2); jj+=
317d6 32 29 7b 0a 20 20 20 20 20 20 20 20 64 6f 75 62  2){.        doub
317d7 6c 65 20 78 31 3b 0a 20 20 20 20 20 20 20 20 64  le x1;.        d
317d8 6f 75 62 6c 65 20 78 32 3b 0a 0a 20 20 20 20 20  ouble x2;..     
317d9 20 20 20 78 31 20 3d 20 4d 41 58 28 44 43 4f 4f     x1 = MAX(DCOO
317da 52 44 28 70 2d 3e 61 43 6f 6f 72 64 5b 6a 6a 5d  RD(p->aCoord[jj]
317db 29 2c 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b  ), DCOORD(aCell[
317dc 69 69 5d 2e 61 43 6f 6f 72 64 5b 6a 6a 5d 29 29  ii].aCoord[jj]))
317dd 3b 0a 20 20 20 20 20 20 20 20 78 32 20 3d 20 4d  ;.        x2 = M
317de 49 4e 28 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f  IN(DCOORD(p->aCo
317df 6f 72 64 5b 6a 6a 2b 31 5d 29 2c 20 44 43 4f 4f  ord[jj+1]), DCOO
317e0 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f  RD(aCell[ii].aCo
317e1 6f 72 64 5b 6a 6a 2b 31 5d 29 29 3b 0a 0a 20 20  ord[jj+1]));..  
317e2 20 20 20 20 20 20 69 66 28 20 78 32 3c 78 31 20        if( x2<x1 
317e3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6f 20 3d  ){.          o =
317e4 20 30 2e 30 3b 0a 20 20 20 20 20 20 20 20 20 20   0.0;.          
317e5 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
317e6 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
317e7 6f 20 3d 20 6f 20 2a 20 28 78 32 2d 78 31 29 3b  o = o * (x2-x1);
317e8 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
317e9 20 7d 0a 20 20 20 20 20 20 6f 76 65 72 6c 61 70   }.      overlap
317ea 20 2b 3d 20 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d   += o;.    }.  }
317eb 0a 20 20 72 65 74 75 72 6e 20 6f 76 65 72 6c 61  .  return overla
317ec 70 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  p;.}.#endif..#if
317ed 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52   VARIANT_RSTARTR
317ee 45 45 5f 43 48 4f 4f 53 45 53 55 42 54 52 45 45  EE_CHOOSESUBTREE
317ef 0a 73 74 61 74 69 63 20 66 6c 6f 61 74 20 63 65  .static float ce
317f0 6c 6c 4f 76 65 72 6c 61 70 45 6e 6c 61 72 67 65  llOverlapEnlarge
317f1 6d 65 6e 74 28 0a 20 20 52 74 72 65 65 20 2a 70  ment(.  Rtree *p
317f2 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65 43  Rtree, .  RtreeC
317f3 65 6c 6c 20 2a 70 2c 20 0a 20 20 52 74 72 65 65  ell *p, .  Rtree
317f4 43 65 6c 6c 20 2a 70 49 6e 73 65 72 74 2c 20 0a  Cell *pInsert, .
317f5 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65    RtreeCell *aCe
317f6 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c  ll, .  int nCell
317f7 2c 20 0a 20 20 69 6e 74 20 69 45 78 63 6c 75 64  , .  int iExclud
317f8 65 0a 29 7b 0a 20 20 66 6c 6f 61 74 20 62 65 66  e.){.  float bef
317f9 6f 72 65 3b 0a 20 20 66 6c 6f 61 74 20 61 66 74  ore;.  float aft
317fa 65 72 3b 0a 20 20 62 65 66 6f 72 65 20 3d 20 63  er;.  before = c
317fb 65 6c 6c 4f 76 65 72 6c 61 70 28 70 52 74 72 65  ellOverlap(pRtre
317fc 65 2c 20 70 2c 20 61 43 65 6c 6c 2c 20 6e 43 65  e, p, aCell, nCe
317fd 6c 6c 2c 20 69 45 78 63 6c 75 64 65 29 3b 0a 20  ll, iExclude);. 
317fe 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65   cellUnion(pRtre
317ff 65 2c 20 70 2c 20 70 49 6e 73 65 72 74 29 3b 0a  e, p, pInsert);.
31800 20 20 61 66 74 65 72 20 3d 20 63 65 6c 6c 4f 76    after = cellOv
31801 65 72 6c 61 70 28 70 52 74 72 65 65 2c 20 70 2c  erlap(pRtree, p,
31802 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 69   aCell, nCell, i
31803 45 78 63 6c 75 64 65 29 3b 0a 20 20 72 65 74 75  Exclude);.  retu
31804 72 6e 20 61 66 74 65 72 2d 62 65 66 6f 72 65 3b  rn after-before;
31805 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  .}.#endif.../*.*
31806 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
31807 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 43  implements the C
31808 68 6f 6f 73 65 4c 65 61 66 20 61 6c 67 6f 72 69  hooseLeaf algori
31809 74 68 6d 20 66 72 6f 6d 20 47 75 74 6d 61 6e 5b  thm from Gutman[
3180a 38 34 5d 2e 0a 2a 2a 20 43 68 6f 6f 73 65 53 75  84]..** ChooseSu
3180b 62 54 72 65 65 20 69 6e 20 72 2a 74 72 65 65 20  bTree in r*tree 
3180c 74 65 72 6d 69 6e 6f 6c 6f 67 79 2e 0a 2a 2f 0a  terminology..*/.
3180d 73 74 61 74 69 63 20 69 6e 74 20 43 68 6f 6f 73  static int Choos
3180e 65 4c 65 61 66 28 0a 20 20 52 74 72 65 65 20 2a  eLeaf(.  Rtree *
3180f 70 52 74 72 65 65 2c 20 20 20 20 20 20 20 20 20  pRtree,         
31810 20 20 20 20 20 20 2f 2a 20 52 74 72 65 65 20 74        /* Rtree t
31811 61 62 6c 65 20 2a 2f 0a 20 20 52 74 72 65 65 43  able */.  RtreeC
31812 65 6c 6c 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20  ell *pCell,     
31813 20 20 20 20 20 20 20 2f 2a 20 43 65 6c 6c 20 74         /* Cell t
31814 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 72 74  o insert into rt
31815 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 48 65  ree */.  int iHe
31816 69 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 20  ight,           
31817 20 20 20 20 20 20 2f 2a 20 48 65 69 67 68 74 20        /* Height 
31818 6f 66 20 73 75 62 2d 74 72 65 65 20 72 6f 6f 74  of sub-tree root
31819 65 64 20 61 74 20 70 43 65 6c 6c 20 2a 2f 0a 20  ed at pCell */. 
3181a 20 52 74 72 65 65 4e 6f 64 65 20 2a 2a 70 70 4c   RtreeNode **ppL
3181b 65 61 66 20 20 20 20 20 20 20 20 20 20 20 2f 2a  eaf           /*
3181c 20 4f 55 54 3a 20 53 65 6c 65 63 74 65 64 20 6c   OUT: Selected l
3181d 65 61 66 20 70 61 67 65 20 2a 2f 0a 29 7b 0a 20  eaf page */.){. 
3181e 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 69   int rc;.  int i
3181f 69 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  i;.  RtreeNode *
31820 70 4e 6f 64 65 3b 0a 20 20 72 63 20 3d 20 6e 6f  pNode;.  rc = no
31821 64 65 41 63 71 75 69 72 65 28 70 52 74 72 65 65  deAcquire(pRtree
31822 2c 20 31 2c 20 30 2c 20 26 70 4e 6f 64 65 29 3b  , 1, 0, &pNode);
31823 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 72 63  ..  for(ii=0; rc
31824 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
31825 69 3c 28 70 52 74 72 65 65 2d 3e 69 44 65 70 74  i<(pRtree->iDept
31826 68 2d 69 48 65 69 67 68 74 29 3b 20 69 69 2b 2b  h-iHeight); ii++
31827 29 7b 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c  ){.    int iCell
31828 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e  ;.    sqlite3_in
31829 74 36 34 20 69 42 65 73 74 3b 0a 0a 20 20 20 20  t64 iBest;..    
3182a 66 6c 6f 61 74 20 66 4d 69 6e 47 72 6f 77 74 68  float fMinGrowth
3182b 3b 0a 20 20 20 20 66 6c 6f 61 74 20 66 4d 69 6e  ;.    float fMin
3182c 41 72 65 61 3b 0a 20 20 20 20 66 6c 6f 61 74 20  Area;.    float 
3182d 66 4d 69 6e 4f 76 65 72 6c 61 70 3b 0a 0a 20 20  fMinOverlap;..  
3182e 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43    int nCell = NC
3182f 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20  ELL(pNode);.    
31830 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a  RtreeCell cell;.
31831 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70      RtreeNode *p
31832 43 68 69 6c 64 3b 0a 0a 20 20 20 20 52 74 72 65  Child;..    Rtre
31833 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 20 3d 20 30  eCell *aCell = 0
31834 3b 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 52  ;..#if VARIANT_R
31835 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53 45 53  STARTREE_CHOOSES
31836 55 42 54 52 45 45 0a 20 20 20 20 69 66 28 20 69  UBTREE.    if( i
31837 69 3d 3d 28 70 52 74 72 65 65 2d 3e 69 44 65 70  i==(pRtree->iDep
31838 74 68 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 69  th-1) ){.      i
31839 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20 61 43 65  nt jj;.      aCe
3183a 6c 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  ll = sqlite3_mal
3183b 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65  loc(sizeof(Rtree
3183c 43 65 6c 6c 29 2a 6e 43 65 6c 6c 29 3b 0a 20 20  Cell)*nCell);.  
3183d 20 20 20 20 69 66 28 20 21 61 43 65 6c 6c 20 29      if( !aCell )
3183e 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  {.        rc = S
3183f 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
31840 20 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65       nodeRelease
31841 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b  (pRtree, pNode);
31842 0a 20 20 20 20 20 20 20 20 70 4e 6f 64 65 20 3d  .        pNode =
31843 20 30 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74   0;.        cont
31844 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  inue;.      }.  
31845 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a      for(jj=0; jj
31846 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b 0a 20  <nCell; jj++){. 
31847 20 20 20 20 20 20 20 6e 6f 64 65 47 65 74 43 65         nodeGetCe
31848 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ll(pRtree, pNode
31849 2c 20 6a 6a 2c 20 26 61 43 65 6c 6c 5b 6a 6a 5d  , jj, &aCell[jj]
3184a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
3184b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20  .#endif..    /* 
3184c 53 65 6c 65 63 74 20 74 68 65 20 63 68 69 6c 64  Select the child
3184d 20 6e 6f 64 65 20 77 68 69 63 68 20 77 69 6c 6c   node which will
3184e 20 62 65 20 65 6e 6c 61 72 67 65 64 20 74 68 65   be enlarged the
3184f 20 6c 65 61 73 74 20 69 66 20 70 43 65 6c 6c 0a   least if pCell.
31850 20 20 20 20 2a 2a 20 69 73 20 69 6e 73 65 72 74      ** is insert
31851 65 64 20 69 6e 74 6f 20 69 74 2e 20 52 65 73 6f  ed into it. Reso
31852 6c 76 65 20 74 69 65 73 20 62 79 20 63 68 6f 6f  lve ties by choo
31853 73 69 6e 67 20 74 68 65 20 65 6e 74 72 79 20 77  sing the entry w
31854 69 74 68 0a 20 20 20 20 2a 2a 20 74 68 65 20 73  ith.    ** the s
31855 6d 61 6c 6c 65 73 74 20 61 72 65 61 2e 0a 20 20  mallest area..  
31856 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 43 65    */.    for(iCe
31857 6c 6c 3d 30 3b 20 69 43 65 6c 6c 3c 6e 43 65 6c  ll=0; iCell<nCel
31858 6c 3b 20 69 43 65 6c 6c 2b 2b 29 7b 0a 20 20 20  l; iCell++){.   
31859 20 20 20 66 6c 6f 61 74 20 67 72 6f 77 74 68 3b     float growth;
3185a 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 61 72 65  .      float are
3185b 61 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 6f  a;.      float o
3185c 76 65 72 6c 61 70 20 3d 20 30 2e 30 3b 0a 20 20  verlap = 0.0;.  
3185d 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28      nodeGetCell(
3185e 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69  pRtree, pNode, i
3185f 43 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b 0a 20 20  Cell, &cell);.  
31860 20 20 20 20 67 72 6f 77 74 68 20 3d 20 63 65 6c      growth = cel
31861 6c 47 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20  lGrowth(pRtree, 
31862 26 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 3b 0a 20  &cell, pCell);. 
31863 20 20 20 20 20 61 72 65 61 20 3d 20 63 65 6c 6c       area = cell
31864 41 72 65 61 28 70 52 74 72 65 65 2c 20 26 63 65  Area(pRtree, &ce
31865 6c 6c 29 3b 0a 23 69 66 20 56 41 52 49 41 4e 54  ll);.#if VARIANT
31866 5f 52 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53  _RSTARTREE_CHOOS
31867 45 53 55 42 54 52 45 45 0a 20 20 20 20 20 20 69  ESUBTREE.      i
31868 66 28 20 69 69 3d 3d 28 70 52 74 72 65 65 2d 3e  f( ii==(pRtree->
31869 69 44 65 70 74 68 2d 31 29 20 29 7b 0a 20 20 20  iDepth-1) ){.   
3186a 20 20 20 20 20 6f 76 65 72 6c 61 70 20 3d 20 63       overlap = c
3186b 65 6c 6c 4f 76 65 72 6c 61 70 45 6e 6c 61 72 67  ellOverlapEnlarg
3186c 65 6d 65 6e 74 28 70 52 74 72 65 65 2c 26 63 65  ement(pRtree,&ce
3186d 6c 6c 2c 70 43 65 6c 6c 2c 61 43 65 6c 6c 2c 6e  ll,pCell,aCell,n
3186e 43 65 6c 6c 2c 69 43 65 6c 6c 29 3b 0a 20 20 20  Cell,iCell);.   
3186f 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
31870 20 20 69 66 28 20 28 69 43 65 6c 6c 3d 3d 30 29    if( (iCell==0)
31871 20 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f 76 65   .       || (ove
31872 72 6c 61 70 3c 66 4d 69 6e 4f 76 65 72 6c 61 70  rlap<fMinOverlap
31873 29 20 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f 76  ) .       || (ov
31874 65 72 6c 61 70 3d 3d 66 4d 69 6e 4f 76 65 72 6c  erlap==fMinOverl
31875 61 70 20 26 26 20 67 72 6f 77 74 68 3c 66 4d 69  ap && growth<fMi
31876 6e 47 72 6f 77 74 68 29 0a 20 20 20 20 20 20 20  nGrowth).       
31877 7c 7c 20 28 6f 76 65 72 6c 61 70 3d 3d 66 4d 69  || (overlap==fMi
31878 6e 4f 76 65 72 6c 61 70 20 26 26 20 67 72 6f 77  nOverlap && grow
31879 74 68 3d 3d 66 4d 69 6e 47 72 6f 77 74 68 20 26  th==fMinGrowth &
3187a 26 20 61 72 65 61 3c 66 4d 69 6e 41 72 65 61 29  & area<fMinArea)
3187b 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20  .      ){.      
3187c 20 20 66 4d 69 6e 4f 76 65 72 6c 61 70 20 3d 20    fMinOverlap = 
3187d 6f 76 65 72 6c 61 70 3b 0a 20 20 20 20 20 20 20  overlap;.       
3187e 20 66 4d 69 6e 47 72 6f 77 74 68 20 3d 20 67 72   fMinGrowth = gr
3187f 6f 77 74 68 3b 0a 20 20 20 20 20 20 20 20 66 4d  owth;.        fM
31880 69 6e 41 72 65 61 20 3d 20 61 72 65 61 3b 0a 20  inArea = area;. 
31881 20 20 20 20 20 20 20 69 42 65 73 74 20 3d 20 63         iBest = c
31882 65 6c 6c 2e 69 52 6f 77 69 64 3b 0a 20 20 20 20  ell.iRowid;.    
31883 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73    }.    }..    s
31884 71 6c 69 74 65 33 5f 66 72 65 65 28 61 43 65 6c  qlite3_free(aCel
31885 6c 29 3b 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64  l);.    rc = nod
31886 65 41 63 71 75 69 72 65 28 70 52 74 72 65 65 2c  eAcquire(pRtree,
31887 20 69 42 65 73 74 2c 20 70 4e 6f 64 65 2c 20 26   iBest, pNode, &
31888 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 6e 6f 64  pChild);.    nod
31889 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
3188a 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 70 4e 6f   pNode);.    pNo
3188b 64 65 20 3d 20 70 43 68 69 6c 64 3b 0a 20 20 7d  de = pChild;.  }
3188c 0a 0a 20 20 2a 70 70 4c 65 61 66 20 3d 20 70 4e  ..  *ppLeaf = pN
3188d 6f 64 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ode;.  return rc
3188e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 65 6c  ;.}../*.** A cel
3188f 6c 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20  l with the same 
31890 63 6f 6e 74 65 6e 74 20 61 73 20 70 43 65 6c 6c  content as pCell
31891 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 69   has just been i
31892 6e 73 65 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20  nserted into.** 
31893 74 68 65 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 20  the node pNode. 
31894 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 75 70  This function up
31895 64 61 74 65 73 20 74 68 65 20 62 6f 75 6e 64 69  dates the boundi
31896 6e 67 20 62 6f 78 20 63 65 6c 6c 73 20 69 6e 0a  ng box cells in.
31897 2a 2a 20 61 6c 6c 20 61 6e 63 65 73 74 6f 72 20  ** all ancestor 
31898 65 6c 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61  elements..*/.sta
31899 74 69 63 20 76 6f 69 64 20 41 64 6a 75 73 74 54  tic void AdjustT
3189a 72 65 65 28 0a 20 20 52 74 72 65 65 20 2a 70 52  ree(.  Rtree *pR
3189b 74 72 65 65 2c 20 20 20 20 20 20 20 20 20 20 20  tree,           
3189c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 74 72 65           /* Rtre
3189d 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 52 74 72  e table */.  Rtr
3189e 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 20  eeNode *pNode,  
3189f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
318a0 2a 20 41 64 6a 75 73 74 20 61 6e 63 65 73 74 72  * Adjust ancestr
318a1 79 20 6f 66 20 74 68 69 73 20 6e 6f 64 65 2e 20  y of this node. 
318a2 2a 2f 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  */.  RtreeCell *
318a3 70 43 65 6c 6c 20 20 20 20 20 20 20 20 20 20 20  pCell           
318a4 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63         /* This c
318a5 65 6c 6c 20 77 61 73 20 6a 75 73 74 20 69 6e 73  ell was just ins
318a6 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 52 74  erted */.){.  Rt
318a7 72 65 65 4e 6f 64 65 20 2a 70 20 3d 20 70 4e 6f  reeNode *p = pNo
318a8 64 65 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e  de;.  while( p->
318a9 70 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 52  pParent ){.    R
318aa 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20  treeCell cell;. 
318ab 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50     RtreeNode *pP
318ac 61 72 65 6e 74 20 3d 20 70 2d 3e 70 50 61 72 65  arent = p->pPare
318ad 6e 74 3b 0a 20 20 20 20 69 6e 74 20 69 43 65 6c  nt;.    int iCel
318ae 6c 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e  l = nodeParentIn
318af 64 65 78 28 70 52 74 72 65 65 2c 20 70 29 3b 0a  dex(pRtree, p);.
318b0 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c  .    nodeGetCell
318b1 28 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74  (pRtree, pParent
318b2 2c 20 69 43 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b  , iCell, &cell);
318b3 0a 20 20 20 20 69 66 28 20 21 63 65 6c 6c 43 6f  .    if( !cellCo
318b4 6e 74 61 69 6e 73 28 70 52 74 72 65 65 2c 20 26  ntains(pRtree, &
318b5 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 20 29 7b 0a  cell, pCell) ){.
318b6 20 20 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28        cellUnion(
318b7 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20 70  pRtree, &cell, p
318b8 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 6e 6f 64  Cell);.      nod
318b9 65 4f 76 65 72 77 72 69 74 65 43 65 6c 6c 28 70  eOverwriteCell(p
318ba 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c 20  Rtree, pParent, 
318bb 26 63 65 6c 6c 2c 20 69 43 65 6c 6c 29 3b 0a 20  &cell, iCell);. 
318bc 20 20 20 7d 0a 20 0a 20 20 20 20 70 20 3d 20 70     }. .    p = p
318bd 50 61 72 65 6e 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f  Parent;.  }.}../
318be 2a 0a 2a 2a 20 57 72 69 74 65 20 6d 61 70 70 69  *.** Write mappi
318bf 6e 67 20 28 69 52 6f 77 69 64 2d 3e 69 4e 6f 64  ng (iRowid->iNod
318c0 65 29 20 74 6f 20 74 68 65 20 3c 72 74 72 65 65  e) to the <rtree
318c1 3e 5f 72 6f 77 69 64 20 74 61 62 6c 65 2e 0a 2a  >_rowid table..*
318c2 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 6f 77  /.static int row
318c3 69 64 57 72 69 74 65 28 52 74 72 65 65 20 2a 70  idWrite(Rtree *p
318c4 52 74 72 65 65 2c 20 73 71 6c 69 74 65 33 5f 69  Rtree, sqlite3_i
318c5 6e 74 36 34 20 69 52 6f 77 69 64 2c 20 73 71 6c  nt64 iRowid, sql
318c6 69 74 65 33 5f 69 6e 74 36 34 20 69 4e 6f 64 65  ite3_int64 iNode
318c7 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  ){.  sqlite3_bin
318c8 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e  d_int64(pRtree->
318c9 70 57 72 69 74 65 52 6f 77 69 64 2c 20 31 2c 20  pWriteRowid, 1, 
318ca 69 52 6f 77 69 64 29 3b 0a 20 20 73 71 6c 69 74  iRowid);.  sqlit
318cb 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52  e3_bind_int64(pR
318cc 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69  tree->pWriteRowi
318cd 64 2c 20 32 2c 20 69 4e 6f 64 65 29 3b 0a 20 20  d, 2, iNode);.  
318ce 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74  sqlite3_step(pRt
318cf 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64  ree->pWriteRowid
318d0 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  );.  return sqli
318d1 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65 65  te3_reset(pRtree
318d2 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 29 3b 0a  ->pWriteRowid);.
318d3 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d  }../*.** Write m
318d4 61 70 70 69 6e 67 20 28 69 4e 6f 64 65 2d 3e 69  apping (iNode->i
318d5 50 61 72 29 20 74 6f 20 74 68 65 20 3c 72 74 72  Par) to the <rtr
318d6 65 65 3e 5f 70 61 72 65 6e 74 20 74 61 62 6c 65  ee>_parent table
318d7 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
318d8 70 61 72 65 6e 74 57 72 69 74 65 28 52 74 72 65  parentWrite(Rtre
318d9 65 20 2a 70 52 74 72 65 65 2c 20 73 71 6c 69 74  e *pRtree, sqlit
318da 65 33 5f 69 6e 74 36 34 20 69 4e 6f 64 65 2c 20  e3_int64 iNode, 
318db 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 50  sqlite3_int64 iP
318dc 61 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62  ar){.  sqlite3_b
318dd 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65  ind_int64(pRtree
318de 2d 3e 70 57 72 69 74 65 50 61 72 65 6e 74 2c 20  ->pWriteParent, 
318df 31 2c 20 69 4e 6f 64 65 29 3b 0a 20 20 73 71 6c  1, iNode);.  sql
318e0 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
318e1 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 50 61  pRtree->pWritePa
318e2 72 65 6e 74 2c 20 32 2c 20 69 50 61 72 29 3b 0a  rent, 2, iPar);.
318e3 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70    sqlite3_step(p
318e4 52 74 72 65 65 2d 3e 70 57 72 69 74 65 50 61 72  Rtree->pWritePar
318e5 65 6e 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 73  ent);.  return s
318e6 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74  qlite3_reset(pRt
318e7 72 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65 6e  ree->pWriteParen
318e8 74 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  t);.}..static in
318e9 74 20 72 74 72 65 65 49 6e 73 65 72 74 43 65 6c  t rtreeInsertCel
318ea 6c 28 52 74 72 65 65 20 2a 2c 20 52 74 72 65 65  l(Rtree *, Rtree
318eb 4e 6f 64 65 20 2a 2c 20 52 74 72 65 65 43 65 6c  Node *, RtreeCel
318ec 6c 20 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69 66 20  l *, int);..#if 
318ed 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f  VARIANT_GUTTMAN_
318ee 4c 49 4e 45 41 52 5f 53 50 4c 49 54 0a 2f 2a 0a  LINEAR_SPLIT./*.
318ef 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
318f0 6e 20 6f 66 20 74 68 65 20 6c 69 6e 65 61 72 20  n of the linear 
318f1 76 61 72 69 61 6e 74 20 6f 66 20 74 68 65 20 50  variant of the P
318f2 69 63 6b 4e 65 78 74 28 29 20 66 75 6e 63 74 69  ickNext() functi
318f3 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d  on from.** Guttm
318f4 61 6e 5b 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69  an[84]..*/.stati
318f5 63 20 52 74 72 65 65 43 65 6c 6c 20 2a 4c 69 6e  c RtreeCell *Lin
318f6 65 61 72 50 69 63 6b 4e 65 78 74 28 0a 20 20 52  earPickNext(.  R
318f7 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20 20  tree *pRtree,.  
318f8 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c  RtreeCell *aCell
318f9 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20  , .  int nCell, 
318fa 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 4c  .  RtreeCell *pL
318fb 65 66 74 42 6f 78 2c 20 0a 20 20 52 74 72 65 65  eftBox, .  Rtree
318fc 43 65 6c 6c 20 2a 70 52 69 67 68 74 42 6f 78 2c  Cell *pRightBox,
318fd 0a 20 20 69 6e 74 20 2a 61 69 55 73 65 64 0a 29  .  int *aiUsed.)
318fe 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f  {.  int ii;.  fo
318ff 72 28 69 69 3d 30 3b 20 61 69 55 73 65 64 5b 69  r(ii=0; aiUsed[i
31900 69 5d 3b 20 69 69 2b 2b 29 3b 0a 20 20 61 69 55  i]; ii++);.  aiU
31901 73 65 64 5b 69 69 5d 20 3d 20 31 3b 0a 20 20 72  sed[ii] = 1;.  r
31902 65 74 75 72 6e 20 26 61 43 65 6c 6c 5b 69 69 5d  eturn &aCell[ii]
31903 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
31904 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
31905 20 6c 69 6e 65 61 72 20 76 61 72 69 61 6e 74 20   linear variant 
31906 6f 66 20 74 68 65 20 50 69 63 6b 53 65 65 64 73  of the PickSeeds
31907 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d  () function from
31908 0a 2a 2a 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e  .** Guttman[84].
31909 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
3190a 4c 69 6e 65 61 72 50 69 63 6b 53 65 65 64 73 28  LinearPickSeeds(
3190b 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
3190c 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61  ,.  RtreeCell *a
3190d 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65  Cell, .  int nCe
3190e 6c 6c 2c 20 0a 20 20 69 6e 74 20 2a 70 69 4c 65  ll, .  int *piLe
3190f 66 74 53 65 65 64 2c 20 0a 20 20 69 6e 74 20 2a  ftSeed, .  int *
31910 70 69 52 69 67 68 74 53 65 65 64 0a 29 7b 0a 20  piRightSeed.){. 
31911 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69 4c   int i;.  int iL
31912 65 66 74 53 65 65 64 20 3d 20 30 3b 0a 20 20 69  eftSeed = 0;.  i
31913 6e 74 20 69 52 69 67 68 74 53 65 65 64 20 3d 20  nt iRightSeed = 
31914 31 3b 0a 20 20 66 6c 6f 61 74 20 6d 61 78 4e 6f  1;.  float maxNo
31915 72 6d 61 6c 49 6e 6e 65 72 57 69 64 74 68 20 3d  rmalInnerWidth =
31916 20 30 2e 30 3b 0a 0a 20 20 2f 2a 20 50 69 63 6b   0.0;..  /* Pick
31917 20 74 77 6f 20 22 73 65 65 64 22 20 63 65 6c 6c   two "seed" cell
31918 73 20 66 72 6f 6d 20 74 68 65 20 61 72 72 61 79  s from the array
31919 20 6f 66 20 63 65 6c 6c 73 2e 20 54 68 65 20 61   of cells. The a
3191a 6c 67 6f 72 69 74 68 6d 20 75 73 65 64 0a 20 20  lgorithm used.  
3191b 2a 2a 20 68 65 72 65 20 69 73 20 74 68 65 20 4c  ** here is the L
3191c 69 6e 65 61 72 50 69 63 6b 53 65 65 64 73 20 61  inearPickSeeds a
3191d 6c 67 6f 72 69 74 68 6d 20 66 72 6f 6d 20 47 75  lgorithm from Gu
3191e 74 6d 61 6e 5b 31 39 38 34 5d 2e 20 54 68 65 20  tman[1984]. The 
3191f 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73 20 6f 66  .  ** indices of
31920 20 74 68 65 20 74 77 6f 20 73 65 65 64 20 63 65   the two seed ce
31921 6c 6c 73 20 69 6e 20 74 68 65 20 61 72 72 61 79  lls in the array
31922 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 6c   are stored in l
31923 6f 63 61 6c 0a 20 20 2a 2a 20 76 61 72 69 61 62  ocal.  ** variab
31924 6c 65 73 20 69 4c 65 66 74 53 65 65 6b 20 61 6e  les iLeftSeek an
31925 64 20 69 52 69 67 68 74 53 65 65 64 2e 0a 20 20  d iRightSeed..  
31926 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  */.  for(i=0; i<
31927 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 2b  pRtree->nDim; i+
31928 2b 29 7b 0a 20 20 20 20 66 6c 6f 61 74 20 78 31  +){.    float x1
31929 20 3d 20 61 43 65 6c 6c 5b 30 5d 2e 61 43 6f 6f   = aCell[0].aCoo
3192a 72 64 5b 69 2a 32 5d 3b 0a 20 20 20 20 66 6c 6f  rd[i*2];.    flo
3192b 61 74 20 78 32 20 3d 20 61 43 65 6c 6c 5b 30 5d  at x2 = aCell[0]
3192c 2e 61 43 6f 6f 72 64 5b 69 2a 32 2b 31 5d 3b 0a  .aCoord[i*2+1];.
3192d 20 20 20 20 66 6c 6f 61 74 20 78 33 20 3d 20 78      float x3 = x
3192e 31 3b 0a 20 20 20 20 66 6c 6f 61 74 20 78 34 20  1;.    float x4 
3192f 3d 20 78 32 3b 0a 20 20 20 20 69 6e 74 20 6a 6a  = x2;.    int jj
31930 3b 0a 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c  ;..    int iCell
31931 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e  Left = 0;.    in
31932 74 20 69 43 65 6c 6c 52 69 67 68 74 20 3d 20 30  t iCellRight = 0
31933 3b 0a 0a 20 20 20 20 66 6f 72 28 6a 6a 3d 31 3b  ;..    for(jj=1;
31934 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b 29   jj<nCell; jj++)
31935 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 6c 65  {.      float le
31936 66 74 20 3d 20 61 43 65 6c 6c 5b 6a 6a 5d 2e 61  ft = aCell[jj].a
31937 43 6f 6f 72 64 5b 69 2a 32 5d 3b 0a 20 20 20 20  Coord[i*2];.    
31938 20 20 66 6c 6f 61 74 20 72 69 67 68 74 20 3d 20    float right = 
31939 61 43 65 6c 6c 5b 6a 6a 5d 2e 61 43 6f 6f 72 64  aCell[jj].aCoord
3193a 5b 69 2a 32 2b 31 5d 3b 0a 0a 20 20 20 20 20 20  [i*2+1];..      
3193b 69 66 28 20 6c 65 66 74 3c 78 31 20 29 20 78 31  if( left<x1 ) x1
3193c 20 3d 20 6c 65 66 74 3b 0a 20 20 20 20 20 20 69   = left;.      i
3193d 66 28 20 72 69 67 68 74 3e 78 34 20 29 20 78 34  f( right>x4 ) x4
3193e 20 3d 20 72 69 67 68 74 3b 0a 20 20 20 20 20 20   = right;.      
3193f 69 66 28 20 6c 65 66 74 3e 78 33 20 29 7b 0a 20  if( left>x3 ){. 
31940 20 20 20 20 20 20 20 78 33 20 3d 20 6c 65 66 74         x3 = left
31941 3b 0a 20 20 20 20 20 20 20 20 69 43 65 6c 6c 52  ;.        iCellR
31942 69 67 68 74 20 3d 20 6a 6a 3b 0a 20 20 20 20 20  ight = jj;.     
31943 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 69 67   }.      if( rig
31944 68 74 3c 78 32 20 29 7b 0a 20 20 20 20 20 20 20  ht<x2 ){.       
31945 20 78 32 20 3d 20 72 69 67 68 74 3b 0a 20 20 20   x2 = right;.   
31946 20 20 20 20 20 69 43 65 6c 6c 4c 65 66 74 20 3d       iCellLeft =
31947 20 6a 6a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   jj;.      }.   
31948 20 7d 0a 0a 20 20 20 20 69 66 28 20 78 34 21 3d   }..    if( x4!=
31949 78 31 20 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61  x1 ){.      floa
3194a 74 20 6e 6f 72 6d 61 6c 77 69 64 74 68 20 3d 20  t normalwidth = 
3194b 28 78 33 20 2d 20 78 32 29 20 2f 20 28 78 34 20  (x3 - x2) / (x4 
3194c 2d 20 78 31 29 3b 0a 20 20 20 20 20 20 69 66 28  - x1);.      if(
3194d 20 6e 6f 72 6d 61 6c 77 69 64 74 68 3e 6d 61 78   normalwidth>max
3194e 4e 6f 72 6d 61 6c 49 6e 6e 65 72 57 69 64 74 68  NormalInnerWidth
3194f 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4c 65 66   ){.        iLef
31950 74 53 65 65 64 20 3d 20 69 43 65 6c 6c 4c 65 66  tSeed = iCellLef
31951 74 3b 0a 20 20 20 20 20 20 20 20 69 52 69 67 68  t;.        iRigh
31952 74 53 65 65 64 20 3d 20 69 43 65 6c 6c 52 69 67  tSeed = iCellRig
31953 68 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ht;.      }.    
31954 7d 0a 20 20 7d 0a 0a 20 20 2a 70 69 4c 65 66 74  }.  }..  *piLeft
31955 53 65 65 64 20 3d 20 69 4c 65 66 74 53 65 65 64  Seed = iLeftSeed
31956 3b 0a 20 20 2a 70 69 52 69 67 68 74 53 65 65 64  ;.  *piRightSeed
31957 20 3d 20 69 52 69 67 68 74 53 65 65 64 3b 0a 7d   = iRightSeed;.}
31958 0a 23 65 6e 64 69 66 20 2f 2a 20 56 41 52 49 41  .#endif /* VARIA
31959 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e 45 41  NT_GUTTMAN_LINEA
3195a 52 5f 53 50 4c 49 54 20 2a 2f 0a 0a 23 69 66 20  R_SPLIT */..#if 
3195b 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f  VARIANT_GUTTMAN_
3195c 51 55 41 44 52 41 54 49 43 5f 53 50 4c 49 54 0a  QUADRATIC_SPLIT.
3195d 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
3195e 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 61 64  tion of the quad
3195f 72 61 74 69 63 20 76 61 72 69 61 6e 74 20 6f 66  ratic variant of
31960 20 74 68 65 20 50 69 63 6b 4e 65 78 74 28 29 20   the PickNext() 
31961 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a  function from.**
31962 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2f   Guttman[84]..*/
31963 0a 73 74 61 74 69 63 20 52 74 72 65 65 43 65 6c  .static RtreeCel
31964 6c 20 2a 51 75 61 64 72 61 74 69 63 50 69 63 6b  l *QuadraticPick
31965 4e 65 78 74 28 0a 20 20 52 74 72 65 65 20 2a 70  Next(.  Rtree *p
31966 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65 43 65  Rtree,.  RtreeCe
31967 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e  ll *aCell, .  in
31968 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 52 74 72 65  t nCell, .  Rtre
31969 65 43 65 6c 6c 20 2a 70 4c 65 66 74 42 6f 78 2c  eCell *pLeftBox,
3196a 20 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70   .  RtreeCell *p
3196b 52 69 67 68 74 42 6f 78 2c 0a 20 20 69 6e 74 20  RightBox,.  int 
3196c 2a 61 69 55 73 65 64 0a 29 7b 0a 20 20 23 64 65  *aiUsed.){.  #de
3196d 66 69 6e 65 20 46 41 42 53 28 61 29 20 28 28 61  fine FABS(a) ((a
3196e 29 3c 30 2e 30 3f 2d 31 2e 30 2a 28 61 29 3a 28  )<0.0?-1.0*(a):(
3196f 61 29 29 0a 0a 20 20 69 6e 74 20 69 53 65 6c 65  a))..  int iSele
31970 63 74 20 3d 20 2d 31 3b 0a 20 20 66 6c 6f 61 74  ct = -1;.  float
31971 20 66 44 69 66 66 3b 0a 20 20 69 6e 74 20 69 69   fDiff;.  int ii
31972 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ;.  for(ii=0; ii
31973 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20  <nCell; ii++){. 
31974 20 20 20 69 66 28 20 61 69 55 73 65 64 5b 69 69     if( aiUsed[ii
31975 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 6c  ]==0 ){.      fl
31976 6f 61 74 20 6c 65 66 74 20 3d 20 63 65 6c 6c 47  oat left = cellG
31977 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20 70 4c  rowth(pRtree, pL
31978 65 66 74 42 6f 78 2c 20 26 61 43 65 6c 6c 5b 69  eftBox, &aCell[i
31979 69 5d 29 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74  i]);.      float
3197a 20 72 69 67 68 74 20 3d 20 63 65 6c 6c 47 72 6f   right = cellGro
3197b 77 74 68 28 70 52 74 72 65 65 2c 20 70 4c 65 66  wth(pRtree, pLef
3197c 74 42 6f 78 2c 20 26 61 43 65 6c 6c 5b 69 69 5d  tBox, &aCell[ii]
3197d 29 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 64  );.      float d
3197e 69 66 66 20 3d 20 46 41 42 53 28 72 69 67 68 74  iff = FABS(right
3197f 2d 6c 65 66 74 29 3b 0a 20 20 20 20 20 20 69 66  -left);.      if
31980 28 20 69 53 65 6c 65 63 74 3c 30 20 7c 7c 20 64  ( iSelect<0 || d
31981 69 66 66 3e 66 44 69 66 66 20 29 7b 0a 20 20 20  iff>fDiff ){.   
31982 20 20 20 20 20 66 44 69 66 66 20 3d 20 64 69 66       fDiff = dif
31983 66 3b 0a 20 20 20 20 20 20 20 20 69 53 65 6c 65  f;.        iSele
31984 63 74 20 3d 20 69 69 3b 0a 20 20 20 20 20 20 7d  ct = ii;.      }
31985 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 69 55  .    }.  }.  aiU
31986 73 65 64 5b 69 53 65 6c 65 63 74 5d 20 3d 20 31  sed[iSelect] = 1
31987 3b 0a 20 20 72 65 74 75 72 6e 20 26 61 43 65 6c  ;.  return &aCel
31988 6c 5b 69 53 65 6c 65 63 74 5d 3b 0a 7d 0a 0a 2f  l[iSelect];.}../
31989 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
3198a 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 61 64 72  ion of the quadr
3198b 61 74 69 63 20 76 61 72 69 61 6e 74 20 6f 66 20  atic variant of 
3198c 74 68 65 20 50 69 63 6b 53 65 65 64 73 28 29 20  the PickSeeds() 
3198d 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a  function from.**
3198e 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2f   Guttman[84]..*/
3198f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 51 75 61  .static void Qua
31990 64 72 61 74 69 63 50 69 63 6b 53 65 65 64 73 28  draticPickSeeds(
31991 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
31992 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61  ,.  RtreeCell *a
31993 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65  Cell, .  int nCe
31994 6c 6c 2c 20 0a 20 20 69 6e 74 20 2a 70 69 4c 65  ll, .  int *piLe
31995 66 74 53 65 65 64 2c 20 0a 20 20 69 6e 74 20 2a  ftSeed, .  int *
31996 70 69 52 69 67 68 74 53 65 65 64 0a 29 7b 0a 20  piRightSeed.){. 
31997 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 6a   int ii;.  int j
31998 6a 3b 0a 0a 20 20 69 6e 74 20 69 4c 65 66 74 53  j;..  int iLeftS
31999 65 65 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69  eed = 0;.  int i
3199a 52 69 67 68 74 53 65 65 64 20 3d 20 31 3b 0a 20  RightSeed = 1;. 
3199b 20 66 6c 6f 61 74 20 66 57 61 73 74 65 20 3d 20   float fWaste = 
3199c 30 2e 30 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30  0.0;..  for(ii=0
3199d 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b  ; ii<nCell; ii++
3199e 29 7b 0a 20 20 20 20 66 6f 72 28 6a 6a 3d 69 69  ){.    for(jj=ii
3199f 2b 31 3b 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a  +1; jj<nCell; jj
319a0 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74  ++){.      float
319a1 20 72 69 67 68 74 20 3d 20 63 65 6c 6c 41 72 65   right = cellAre
319a2 61 28 70 52 74 72 65 65 2c 20 26 61 43 65 6c 6c  a(pRtree, &aCell
319a3 5b 6a 6a 5d 29 3b 0a 20 20 20 20 20 20 66 6c 6f  [jj]);.      flo
319a4 61 74 20 67 72 6f 77 74 68 20 3d 20 63 65 6c 6c  at growth = cell
319a5 47 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20 26  Growth(pRtree, &
319a6 61 43 65 6c 6c 5b 69 69 5d 2c 20 26 61 43 65 6c  aCell[ii], &aCel
319a7 6c 5b 6a 6a 5d 29 3b 0a 20 20 20 20 20 20 66 6c  l[jj]);.      fl
319a8 6f 61 74 20 77 61 73 74 65 20 3d 20 67 72 6f 77  oat waste = grow
319a9 74 68 20 2d 20 72 69 67 68 74 3b 0a 0a 20 20 20  th - right;..   
319aa 20 20 20 69 66 28 20 77 61 73 74 65 3e 66 57 61     if( waste>fWa
319ab 73 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ste ){.        i
319ac 4c 65 66 74 53 65 65 64 20 3d 20 69 69 3b 0a 20  LeftSeed = ii;. 
319ad 20 20 20 20 20 20 20 69 52 69 67 68 74 53 65 65         iRightSee
319ae 64 20 3d 20 6a 6a 3b 0a 20 20 20 20 20 20 20 20  d = jj;.        
319af 66 57 61 73 74 65 20 3d 20 77 61 73 74 65 3b 0a  fWaste = waste;.
319b0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
319b1 7d 0a 0a 20 20 2a 70 69 4c 65 66 74 53 65 65 64  }..  *piLeftSeed
319b2 20 3d 20 69 4c 65 66 74 53 65 65 64 3b 0a 20 20   = iLeftSeed;.  
319b3 2a 70 69 52 69 67 68 74 53 65 65 64 20 3d 20 69  *piRightSeed = i
319b4 52 69 67 68 74 53 65 65 64 3b 0a 7d 0a 23 65 6e  RightSeed;.}.#en
319b5 64 69 66 20 2f 2a 20 56 41 52 49 41 4e 54 5f 47  dif /* VARIANT_G
319b6 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54 49 43  UTTMAN_QUADRATIC
319b7 5f 53 50 4c 49 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  _SPLIT */../*.**
319b8 20 41 72 67 75 6d 65 6e 74 73 20 61 49 64 78 2c   Arguments aIdx,
319b9 20 61 44 69 73 74 61 6e 63 65 20 61 6e 64 20 61   aDistance and a
319ba 53 70 61 72 65 20 61 6c 6c 20 70 6f 69 6e 74 20  Spare all point 
319bb 74 6f 20 61 72 72 61 79 73 20 6f 66 20 73 69 7a  to arrays of siz
319bc 65 0a 2a 2a 20 6e 49 64 78 2e 20 54 68 65 20 61  e.** nIdx. The a
319bd 49 64 78 20 61 72 72 61 79 20 63 6f 6e 74 61 69  Idx array contai
319be 6e 73 20 74 68 65 20 73 65 74 20 6f 66 20 69 6e  ns the set of in
319bf 74 65 67 65 72 73 20 66 72 6f 6d 20 30 20 74 6f  tegers from 0 to
319c0 20 0a 2a 2a 20 28 6e 49 64 78 2d 31 29 20 69 6e   .** (nIdx-1) in
319c1 20 6e 6f 20 70 61 72 74 69 63 75 6c 61 72 20 6f   no particular o
319c2 72 64 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74  rder. This funct
319c3 69 6f 6e 20 73 6f 72 74 73 20 74 68 65 20 76 61  ion sorts the va
319c4 6c 75 65 73 0a 2a 2a 20 69 6e 20 61 49 64 78 20  lues.** in aIdx 
319c5 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65  according to the
319c6 20 69 6e 64 65 78 65 64 20 76 61 6c 75 65 73 20   indexed values 
319c7 69 6e 20 61 44 69 73 74 61 6e 63 65 2e 20 46 6f  in aDistance. Fo
319c8 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 61 73  r.** example, as
319c9 73 75 6d 69 6e 67 20 74 68 65 20 69 6e 70 75 74  suming the input
319ca 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 49 64 78 20  s:.**.**   aIdx 
319cb 20 20 20 20 20 3d 20 7b 20 30 2c 20 20 20 31 2c       = { 0,   1,
319cc 20 20 20 32 2c 20 20 20 33 20 7d 0a 2a 2a 20 20     2,   3 }.**  
319cd 20 61 44 69 73 74 61 6e 63 65 20 3d 20 7b 20 35   aDistance = { 5
319ce 2e 30 2c 20 32 2e 30 2c 20 37 2e 30 2c 20 36 2e  .0, 2.0, 7.0, 6.
319cf 30 20 7d 0a 2a 2a 0a 2a 2a 20 74 68 69 73 20 66  0 }.**.** this f
319d0 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 74 68 65  unction sets the
319d1 20 61 49 64 78 20 61 72 72 61 79 20 74 6f 20 63   aIdx array to c
319d2 6f 6e 74 61 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20  ontain:.**.**   
319d3 61 49 64 78 20 20 20 20 20 20 3d 20 7b 20 30 2c  aIdx      = { 0,
319d4 20 20 20 31 2c 20 20 20 32 2c 20 20 20 33 20 7d     1,   2,   3 }
319d5 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 53 70 61 72  .**.** The aSpar
319d6 65 20 61 72 72 61 79 20 69 73 20 75 73 65 64 20  e array is used 
319d7 61 73 20 74 65 6d 70 6f 72 61 72 79 20 77 6f 72  as temporary wor
319d8 6b 69 6e 67 20 73 70 61 63 65 20 62 79 20 74 68  king space by th
319d9 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 20 61 6c 67  e.** sorting alg
319da 6f 72 69 74 68 6d 2e 0a 2a 2f 0a 73 74 61 74 69  orithm..*/.stati
319db 63 20 76 6f 69 64 20 53 6f 72 74 42 79 44 69 73  c void SortByDis
319dc 74 61 6e 63 65 28 0a 20 20 69 6e 74 20 2a 61 49  tance(.  int *aI
319dd 64 78 2c 20 0a 20 20 69 6e 74 20 6e 49 64 78 2c  dx, .  int nIdx,
319de 20 0a 20 20 66 6c 6f 61 74 20 2a 61 44 69 73 74   .  float *aDist
319df 61 6e 63 65 2c 20 0a 20 20 69 6e 74 20 2a 61 53  ance, .  int *aS
319e0 70 61 72 65 0a 29 7b 0a 20 20 69 66 28 20 6e 49  pare.){.  if( nI
319e1 64 78 3e 31 20 29 7b 0a 20 20 20 20 69 6e 74 20  dx>1 ){.    int 
319e2 69 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20 20 69  iLeft = 0;.    i
319e3 6e 74 20 69 52 69 67 68 74 20 3d 20 30 3b 0a 0a  nt iRight = 0;..
319e4 20 20 20 20 69 6e 74 20 6e 4c 65 66 74 20 3d 20      int nLeft = 
319e5 6e 49 64 78 2f 32 3b 0a 20 20 20 20 69 6e 74 20  nIdx/2;.    int 
319e6 6e 52 69 67 68 74 20 3d 20 6e 49 64 78 2d 6e 4c  nRight = nIdx-nL
319e7 65 66 74 3b 0a 20 20 20 20 69 6e 74 20 2a 61 4c  eft;.    int *aL
319e8 65 66 74 20 3d 20 61 49 64 78 3b 0a 20 20 20 20  eft = aIdx;.    
319e9 69 6e 74 20 2a 61 52 69 67 68 74 20 3d 20 26 61  int *aRight = &a
319ea 49 64 78 5b 6e 4c 65 66 74 5d 3b 0a 0a 20 20 20  Idx[nLeft];..   
319eb 20 53 6f 72 74 42 79 44 69 73 74 61 6e 63 65 28   SortByDistance(
319ec 61 4c 65 66 74 2c 20 6e 4c 65 66 74 2c 20 61 44  aLeft, nLeft, aD
319ed 69 73 74 61 6e 63 65 2c 20 61 53 70 61 72 65 29  istance, aSpare)
319ee 3b 0a 20 20 20 20 53 6f 72 74 42 79 44 69 73 74  ;.    SortByDist
319ef 61 6e 63 65 28 61 52 69 67 68 74 2c 20 6e 52 69  ance(aRight, nRi
319f0 67 68 74 2c 20 61 44 69 73 74 61 6e 63 65 2c 20  ght, aDistance, 
319f1 61 53 70 61 72 65 29 3b 0a 0a 20 20 20 20 6d 65  aSpare);..    me
319f2 6d 63 70 79 28 61 53 70 61 72 65 2c 20 61 4c 65  mcpy(aSpare, aLe
319f3 66 74 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a  ft, sizeof(int)*
319f4 6e 4c 65 66 74 29 3b 0a 20 20 20 20 61 4c 65 66  nLeft);.    aLef
319f5 74 20 3d 20 61 53 70 61 72 65 3b 0a 0a 20 20 20  t = aSpare;..   
319f6 20 77 68 69 6c 65 28 20 69 4c 65 66 74 3c 6e 4c   while( iLeft<nL
319f7 65 66 74 20 7c 7c 20 69 52 69 67 68 74 3c 6e 52  eft || iRight<nR
319f8 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 69 66  ight ){.      if
319f9 28 20 69 4c 65 66 74 3d 3d 6e 4c 65 66 74 20 29  ( iLeft==nLeft )
319fa 7b 0a 20 20 20 20 20 20 20 20 61 49 64 78 5b 69  {.        aIdx[i
319fb 4c 65 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61  Left+iRight] = a
319fc 52 69 67 68 74 5b 69 52 69 67 68 74 5d 3b 0a 20  Right[iRight];. 
319fd 20 20 20 20 20 20 20 69 52 69 67 68 74 2b 2b 3b         iRight++;
319fe 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
319ff 20 69 52 69 67 68 74 3d 3d 6e 52 69 67 68 74 20   iRight==nRight 
31a00 29 7b 0a 20 20 20 20 20 20 20 20 61 49 64 78 5b  ){.        aIdx[
31a01 69 4c 65 66 74 2b 69 52 69 67 68 74 5d 20 3d 20  iLeft+iRight] = 
31a02 61 4c 65 66 74 5b 69 4c 65 66 74 5d 3b 0a 20 20  aLeft[iLeft];.  
31a03 20 20 20 20 20 20 69 4c 65 66 74 2b 2b 3b 0a 20        iLeft++;. 
31a04 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
31a05 20 20 20 20 66 6c 6f 61 74 20 66 4c 65 66 74 20      float fLeft 
31a06 3d 20 61 44 69 73 74 61 6e 63 65 5b 61 4c 65 66  = aDistance[aLef
31a07 74 5b 69 4c 65 66 74 5d 5d 3b 0a 20 20 20 20 20  t[iLeft]];.     
31a08 20 20 20 66 6c 6f 61 74 20 66 52 69 67 68 74 20     float fRight 
31a09 3d 20 61 44 69 73 74 61 6e 63 65 5b 61 52 69 67  = aDistance[aRig
31a0a 68 74 5b 69 52 69 67 68 74 5d 5d 3b 0a 20 20 20  ht[iRight]];.   
31a0b 20 20 20 20 20 69 66 28 20 66 4c 65 66 74 3c 66       if( fLeft<f
31a0c 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 20  Right ){.       
31a0d 20 20 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52     aIdx[iLeft+iR
31a0e 69 67 68 74 5d 20 3d 20 61 4c 65 66 74 5b 69 4c  ight] = aLeft[iL
31a0f 65 66 74 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  eft];.          
31a10 69 4c 65 66 74 2b 2b 3b 0a 20 20 20 20 20 20 20  iLeft++;.       
31a11 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
31a12 20 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69    aIdx[iLeft+iRi
31a13 67 68 74 5d 20 3d 20 61 52 69 67 68 74 5b 69 52  ght] = aRight[iR
31a14 69 67 68 74 5d 3b 0a 20 20 20 20 20 20 20 20 20  ight];.         
31a15 20 69 52 69 67 68 74 2b 2b 3b 0a 20 20 20 20 20   iRight++;.     
31a16 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
31a17 20 7d 0a 0a 23 69 66 20 30 0a 20 20 20 20 2f 2a   }..#if 0.    /*
31a18 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20   Check that the 
31a19 73 6f 72 74 20 77 6f 72 6b 65 64 20 2a 2f 0a 20  sort worked */. 
31a1a 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a     {.      int j
31a1b 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d  j;.      for(jj=
31a1c 31 3b 20 6a 6a 3c 6e 49 64 78 3b 20 6a 6a 2b 2b  1; jj<nIdx; jj++
31a1d 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74  ){.        float
31a1e 20 6c 65 66 74 20 3d 20 61 44 69 73 74 61 6e 63   left = aDistanc
31a1f 65 5b 61 49 64 78 5b 6a 6a 2d 31 5d 5d 3b 0a 20  e[aIdx[jj-1]];. 
31a20 20 20 20 20 20 20 20 66 6c 6f 61 74 20 72 69 67         float rig
31a21 68 74 20 3d 20 61 44 69 73 74 61 6e 63 65 5b 61  ht = aDistance[a
31a22 49 64 78 5b 6a 6a 5d 5d 3b 0a 20 20 20 20 20 20  Idx[jj]];.      
31a23 20 20 61 73 73 65 72 74 28 20 6c 65 66 74 3c 3d    assert( left<=
31a24 72 69 67 68 74 20 29 3b 0a 20 20 20 20 20 20 7d  right );.      }
31a25 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
31a26 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d  }.}../*.** Argum
31a27 65 6e 74 73 20 61 49 64 78 2c 20 61 43 65 6c 6c  ents aIdx, aCell
31a28 20 61 6e 64 20 61 53 70 61 72 65 20 61 6c 6c 20   and aSpare all 
31a29 70 6f 69 6e 74 20 74 6f 20 61 72 72 61 79 73 20  point to arrays 
31a2a 6f 66 20 73 69 7a 65 0a 2a 2a 20 6e 49 64 78 2e  of size.** nIdx.
31a2b 20 54 68 65 20 61 49 64 78 20 61 72 72 61 79 20   The aIdx array 
31a2c 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73 65 74  contains the set
31a2d 20 6f 66 20 69 6e 74 65 67 65 72 73 20 66 72 6f   of integers fro
31a2e 6d 20 30 20 74 6f 20 0a 2a 2a 20 28 6e 49 64 78  m 0 to .** (nIdx
31a2f 2d 31 29 20 69 6e 20 6e 6f 20 70 61 72 74 69 63  -1) in no partic
31a30 75 6c 61 72 20 6f 72 64 65 72 2e 20 54 68 69 73  ular order. This
31a31 20 66 75 6e 63 74 69 6f 6e 20 73 6f 72 74 73 20   function sorts 
31a32 74 68 65 20 76 61 6c 75 65 73 0a 2a 2a 20 69 6e  the values.** in
31a33 20 61 49 64 78 20 61 63 63 6f 72 64 69 6e 67 20   aIdx according 
31a34 74 6f 20 64 69 6d 65 6e 73 69 6f 6e 20 69 44 69  to dimension iDi
31a35 6d 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 69  m of the cells i
31a36 6e 20 61 43 65 6c 6c 2e 20 54 68 65 0a 2a 2a 20  n aCell. The.** 
31a37 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66  minimum value of
31a38 20 64 69 6d 65 6e 73 69 6f 6e 20 69 44 69 6d 20   dimension iDim 
31a39 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 66 69  is considered fi
31a3a 72 73 74 2c 20 74 68 65 0a 2a 2a 20 6d 61 78 69  rst, the.** maxi
31a3b 6d 75 6d 20 75 73 65 64 20 74 6f 20 62 72 65 61  mum used to brea
31a3c 6b 20 74 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  k ties..**.** Th
31a3d 65 20 61 53 70 61 72 65 20 61 72 72 61 79 20 69  e aSpare array i
31a3e 73 20 75 73 65 64 20 61 73 20 74 65 6d 70 6f 72  s used as tempor
31a3f 61 72 79 20 77 6f 72 6b 69 6e 67 20 73 70 61 63  ary working spac
31a40 65 20 62 79 20 74 68 65 0a 2a 2a 20 73 6f 72 74  e by the.** sort
31a41 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 2a  ing algorithm..*
31a42 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 53 6f  /.static void So
31a43 72 74 42 79 44 69 6d 65 6e 73 69 6f 6e 28 0a 20  rtByDimension(. 
31a44 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a   Rtree *pRtree,.
31a45 20 20 69 6e 74 20 2a 61 49 64 78 2c 20 0a 20 20    int *aIdx, .  
31a46 69 6e 74 20 6e 49 64 78 2c 20 0a 20 20 69 6e 74  int nIdx, .  int
31a47 20 69 44 69 6d 2c 20 0a 20 20 52 74 72 65 65 43   iDim, .  RtreeC
31a48 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69  ell *aCell, .  i
31a49 6e 74 20 2a 61 53 70 61 72 65 0a 29 7b 0a 20 20  nt *aSpare.){.  
31a4a 69 66 28 20 6e 49 64 78 3e 31 20 29 7b 0a 0a 20  if( nIdx>1 ){.. 
31a4b 20 20 20 69 6e 74 20 69 4c 65 66 74 20 3d 20 30     int iLeft = 0
31a4c 3b 0a 20 20 20 20 69 6e 74 20 69 52 69 67 68 74  ;.    int iRight
31a4d 20 3d 20 30 3b 0a 0a 20 20 20 20 69 6e 74 20 6e   = 0;..    int n
31a4e 4c 65 66 74 20 3d 20 6e 49 64 78 2f 32 3b 0a 20  Left = nIdx/2;. 
31a4f 20 20 20 69 6e 74 20 6e 52 69 67 68 74 20 3d 20     int nRight = 
31a50 6e 49 64 78 2d 6e 4c 65 66 74 3b 0a 20 20 20 20  nIdx-nLeft;.    
31a51 69 6e 74 20 2a 61 4c 65 66 74 20 3d 20 61 49 64  int *aLeft = aId
31a52 78 3b 0a 20 20 20 20 69 6e 74 20 2a 61 52 69 67  x;.    int *aRig
31a53 68 74 20 3d 20 26 61 49 64 78 5b 6e 4c 65 66 74  ht = &aIdx[nLeft
31a54 5d 3b 0a 0a 20 20 20 20 53 6f 72 74 42 79 44 69  ];..    SortByDi
31a55 6d 65 6e 73 69 6f 6e 28 70 52 74 72 65 65 2c 20  mension(pRtree, 
31a56 61 4c 65 66 74 2c 20 6e 4c 65 66 74 2c 20 69 44  aLeft, nLeft, iD
31a57 69 6d 2c 20 61 43 65 6c 6c 2c 20 61 53 70 61 72  im, aCell, aSpar
31a58 65 29 3b 0a 20 20 20 20 53 6f 72 74 42 79 44 69  e);.    SortByDi
31a59 6d 65 6e 73 69 6f 6e 28 70 52 74 72 65 65 2c 20  mension(pRtree, 
31a5a 61 52 69 67 68 74 2c 20 6e 52 69 67 68 74 2c 20  aRight, nRight, 
31a5b 69 44 69 6d 2c 20 61 43 65 6c 6c 2c 20 61 53 70  iDim, aCell, aSp
31a5c 61 72 65 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70  are);..    memcp
31a5d 79 28 61 53 70 61 72 65 2c 20 61 4c 65 66 74 2c  y(aSpare, aLeft,
31a5e 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 4c 65   sizeof(int)*nLe
31a5f 66 74 29 3b 0a 20 20 20 20 61 4c 65 66 74 20 3d  ft);.    aLeft =
31a60 20 61 53 70 61 72 65 3b 0a 20 20 20 20 77 68 69   aSpare;.    whi
31a61 6c 65 28 20 69 4c 65 66 74 3c 6e 4c 65 66 74 20  le( iLeft<nLeft 
31a62 7c 7c 20 69 52 69 67 68 74 3c 6e 52 69 67 68 74  || iRight<nRight
31a63 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65   ){.      double
31a64 20 78 6c 65 66 74 31 20 3d 20 44 43 4f 4f 52 44   xleft1 = DCOORD
31a65 28 61 43 65 6c 6c 5b 61 4c 65 66 74 5b 69 4c 65  (aCell[aLeft[iLe
31a66 66 74 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d  ft]].aCoord[iDim
31a67 2a 32 5d 29 3b 0a 20 20 20 20 20 20 64 6f 75 62  *2]);.      doub
31a68 6c 65 20 78 6c 65 66 74 32 20 3d 20 44 43 4f 4f  le xleft2 = DCOO
31a69 52 44 28 61 43 65 6c 6c 5b 61 4c 65 66 74 5b 69  RD(aCell[aLeft[i
31a6a 4c 65 66 74 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44  Left]].aCoord[iD
31a6b 69 6d 2a 32 2b 31 5d 29 3b 0a 20 20 20 20 20 20  im*2+1]);.      
31a6c 64 6f 75 62 6c 65 20 78 72 69 67 68 74 31 20 3d  double xright1 =
31a6d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 61 52   DCOORD(aCell[aR
31a6e 69 67 68 74 5b 69 52 69 67 68 74 5d 5d 2e 61 43  ight[iRight]].aC
31a6f 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 29 3b 0a 20  oord[iDim*2]);. 
31a70 20 20 20 20 20 64 6f 75 62 6c 65 20 78 72 69 67       double xrig
31a71 68 74 32 20 3d 20 44 43 4f 4f 52 44 28 61 43 65  ht2 = DCOORD(aCe
31a72 6c 6c 5b 61 52 69 67 68 74 5b 69 52 69 67 68 74  ll[aRight[iRight
31a73 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32  ]].aCoord[iDim*2
31a74 2b 31 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  +1]);.      if( 
31a75 28 69 4c 65 66 74 21 3d 6e 4c 65 66 74 29 20 26  (iLeft!=nLeft) &
31a76 26 20 28 28 69 52 69 67 68 74 3d 3d 6e 52 69 67  & ((iRight==nRig
31a77 68 74 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 78  ht).       || (x
31a78 6c 65 66 74 31 3c 78 72 69 67 68 74 31 29 0a 20  left1<xright1). 
31a79 20 20 20 20 20 20 7c 7c 20 28 78 6c 65 66 74 31        || (xleft1
31a7a 3d 3d 78 72 69 67 68 74 31 20 26 26 20 78 6c 65  ==xright1 && xle
31a7b 66 74 32 3c 78 72 69 67 68 74 32 29 0a 20 20 20  ft2<xright2).   
31a7c 20 20 20 29 29 7b 0a 20 20 20 20 20 20 20 20 61     )){.        a
31a7d 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67 68 74  Idx[iLeft+iRight
31a7e 5d 20 3d 20 61 4c 65 66 74 5b 69 4c 65 66 74 5d  ] = aLeft[iLeft]
31a7f 3b 0a 20 20 20 20 20 20 20 20 69 4c 65 66 74 2b  ;.        iLeft+
31a80 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  +;.      }else{.
31a81 20 20 20 20 20 20 20 20 61 49 64 78 5b 69 4c 65          aIdx[iLe
31a82 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61 52 69  ft+iRight] = aRi
31a83 67 68 74 5b 69 52 69 67 68 74 5d 3b 0a 20 20 20  ght[iRight];.   
31a84 20 20 20 20 20 69 52 69 67 68 74 2b 2b 3b 0a 20       iRight++;. 
31a85 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69       }.    }..#i
31a86 66 20 30 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b  f 0.    /* Check
31a87 20 74 68 61 74 20 74 68 65 20 73 6f 72 74 20 77   that the sort w
31a88 6f 72 6b 65 64 20 2a 2f 0a 20 20 20 20 7b 0a 20  orked */.    {. 
31a89 20 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20       int jj;.   
31a8a 20 20 20 66 6f 72 28 6a 6a 3d 31 3b 20 6a 6a 3c     for(jj=1; jj<
31a8b 6e 49 64 78 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20  nIdx; jj++){.   
31a8c 20 20 20 20 20 66 6c 6f 61 74 20 78 6c 65 66 74       float xleft
31a8d 31 20 3d 20 61 43 65 6c 6c 5b 61 49 64 78 5b 6a  1 = aCell[aIdx[j
31a8e 6a 2d 31 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69  j-1]].aCoord[iDi
31a8f 6d 2a 32 5d 3b 0a 20 20 20 20 20 20 20 20 66 6c  m*2];.        fl
31a90 6f 61 74 20 78 6c 65 66 74 32 20 3d 20 61 43 65  oat xleft2 = aCe
31a91 6c 6c 5b 61 49 64 78 5b 6a 6a 2d 31 5d 5d 2e 61  ll[aIdx[jj-1]].a
31a92 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 3b  Coord[iDim*2+1];
31a93 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74 20 78  .        float x
31a94 72 69 67 68 74 31 20 3d 20 61 43 65 6c 6c 5b 61  right1 = aCell[a
31a95 49 64 78 5b 6a 6a 5d 5d 2e 61 43 6f 6f 72 64 5b  Idx[jj]].aCoord[
31a96 69 44 69 6d 2a 32 5d 3b 0a 20 20 20 20 20 20 20  iDim*2];.       
31a97 20 66 6c 6f 61 74 20 78 72 69 67 68 74 32 20 3d   float xright2 =
31a98 20 61 43 65 6c 6c 5b 61 49 64 78 5b 6a 6a 5d 5d   aCell[aIdx[jj]]
31a99 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31  .aCoord[iDim*2+1
31a9a 5d 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ];.        asser
31a9b 74 28 20 78 6c 65 66 74 31 3c 3d 78 72 69 67 68  t( xleft1<=xrigh
31a9c 74 31 20 26 26 20 28 78 6c 65 66 74 31 3c 78 72  t1 && (xleft1<xr
31a9d 69 67 68 74 31 20 7c 7c 20 78 6c 65 66 74 32 3c  ight1 || xleft2<
31a9e 3d 78 72 69 67 68 74 32 29 20 29 3b 0a 20 20 20  =xright2) );.   
31a9f 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69     }.    }.#endi
31aa0 66 0a 20 20 7d 0a 7d 0a 0a 23 69 66 20 56 41 52  f.  }.}..#if VAR
31aa1 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f 53  IANT_RSTARTREE_S
31aa2 50 4c 49 54 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  PLIT./*.** Imple
31aa3 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
31aa4 20 52 2a 2d 74 72 65 65 20 76 61 72 69 61 6e 74   R*-tree variant
31aa5 20 6f 66 20 53 70 6c 69 74 4e 6f 64 65 20 66 72   of SplitNode fr
31aa6 6f 6d 20 42 65 63 6b 6d 61 6e 5b 31 39 39 30 5d  om Beckman[1990]
31aa7 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
31aa8 73 70 6c 69 74 4e 6f 64 65 53 74 61 72 74 72 65  splitNodeStartre
31aa9 65 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  e(.  Rtree *pRtr
31aaa 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  ee,.  RtreeCell 
31aab 2a 61 43 65 6c 6c 2c 0a 20 20 69 6e 74 20 6e 43  *aCell,.  int nC
31aac 65 6c 6c 2c 0a 20 20 52 74 72 65 65 4e 6f 64 65  ell,.  RtreeNode
31aad 20 2a 70 4c 65 66 74 2c 0a 20 20 52 74 72 65 65   *pLeft,.  Rtree
31aae 4e 6f 64 65 20 2a 70 52 69 67 68 74 2c 0a 20 20  Node *pRight,.  
31aaf 52 74 72 65 65 43 65 6c 6c 20 2a 70 42 62 6f 78  RtreeCell *pBbox
31ab0 4c 65 66 74 2c 0a 20 20 52 74 72 65 65 43 65 6c  Left,.  RtreeCel
31ab1 6c 20 2a 70 42 62 6f 78 52 69 67 68 74 0a 29 7b  l *pBboxRight.){
31ab2 0a 20 20 69 6e 74 20 2a 2a 61 61 53 6f 72 74 65  .  int **aaSorte
31ab3 64 3b 0a 20 20 69 6e 74 20 2a 61 53 70 61 72 65  d;.  int *aSpare
31ab4 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 0a 20 20 69  ;.  int ii;..  i
31ab5 6e 74 20 69 42 65 73 74 44 69 6d 3b 0a 20 20 69  nt iBestDim;.  i
31ab6 6e 74 20 69 42 65 73 74 53 70 6c 69 74 3b 0a 20  nt iBestSplit;. 
31ab7 20 66 6c 6f 61 74 20 66 42 65 73 74 4d 61 72 67   float fBestMarg
31ab8 69 6e 3b 0a 0a 20 20 69 6e 74 20 6e 42 79 74 65  in;..  int nByte
31ab9 20 3d 20 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d   = (pRtree->nDim
31aba 2b 31 29 2a 28 73 69 7a 65 6f 66 28 69 6e 74 2a  +1)*(sizeof(int*
31abb 29 2b 6e 43 65 6c 6c 2a 73 69 7a 65 6f 66 28 69  )+nCell*sizeof(i
31abc 6e 74 29 29 3b 0a 0a 20 20 61 61 53 6f 72 74 65  nt));..  aaSorte
31abd 64 20 3d 20 28 69 6e 74 20 2a 2a 29 73 71 6c 69  d = (int **)sqli
31abe 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74 65  te3_malloc(nByte
31abf 29 3b 0a 20 20 69 66 28 20 21 61 61 53 6f 72 74  );.  if( !aaSort
31ac0 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ed ){.    return
31ac1 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
31ac2 20 7d 0a 0a 20 20 61 53 70 61 72 65 20 3d 20 26   }..  aSpare = &
31ac3 28 28 69 6e 74 20 2a 29 26 61 61 53 6f 72 74 65  ((int *)&aaSorte
31ac4 64 5b 70 52 74 72 65 65 2d 3e 6e 44 69 6d 5d 29  d[pRtree->nDim])
31ac5 5b 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 6e 43  [pRtree->nDim*nC
31ac6 65 6c 6c 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 61  ell];.  memset(a
31ac7 61 53 6f 72 74 65 64 2c 20 30 2c 20 6e 42 79 74  aSorted, 0, nByt
31ac8 65 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  e);.  for(ii=0; 
31ac9 69 69 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b  ii<pRtree->nDim;
31aca 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20   ii++){.    int 
31acb 6a 6a 3b 0a 20 20 20 20 61 61 53 6f 72 74 65 64  jj;.    aaSorted
31acc 5b 69 69 5d 20 3d 20 26 28 28 69 6e 74 20 2a 29  [ii] = &((int *)
31acd 26 61 61 53 6f 72 74 65 64 5b 70 52 74 72 65 65  &aaSorted[pRtree
31ace 2d 3e 6e 44 69 6d 5d 29 5b 69 69 2a 6e 43 65 6c  ->nDim])[ii*nCel
31acf 6c 5d 3b 0a 20 20 20 20 66 6f 72 28 6a 6a 3d 30  l];.    for(jj=0
31ad0 3b 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b  ; jj<nCell; jj++
31ad1 29 7b 0a 20 20 20 20 20 20 61 61 53 6f 72 74 65  ){.      aaSorte
31ad2 64 5b 69 69 5d 5b 6a 6a 5d 20 3d 20 6a 6a 3b 0a  d[ii][jj] = jj;.
31ad3 20 20 20 20 7d 0a 20 20 20 20 53 6f 72 74 42 79      }.    SortBy
31ad4 44 69 6d 65 6e 73 69 6f 6e 28 70 52 74 72 65 65  Dimension(pRtree
31ad5 2c 20 61 61 53 6f 72 74 65 64 5b 69 69 5d 2c 20  , aaSorted[ii], 
31ad6 6e 43 65 6c 6c 2c 20 69 69 2c 20 61 43 65 6c 6c  nCell, ii, aCell
31ad7 2c 20 61 53 70 61 72 65 29 3b 0a 20 20 7d 0a 0a  , aSpare);.  }..
31ad8 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70    for(ii=0; ii<p
31ad9 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 69 2b  Rtree->nDim; ii+
31ada 2b 29 7b 0a 20 20 20 20 66 6c 6f 61 74 20 6d 61  +){.    float ma
31adb 72 67 69 6e 20 3d 20 30 2e 30 3b 0a 20 20 20 20  rgin = 0.0;.    
31adc 66 6c 6f 61 74 20 66 42 65 73 74 4f 76 65 72 6c  float fBestOverl
31add 61 70 3b 0a 20 20 20 20 66 6c 6f 61 74 20 66 42  ap;.    float fB
31ade 65 73 74 41 72 65 61 3b 0a 20 20 20 20 69 6e 74  estArea;.    int
31adf 20 69 42 65 73 74 4c 65 66 74 3b 0a 20 20 20 20   iBestLeft;.    
31ae0 69 6e 74 20 6e 4c 65 66 74 3b 0a 0a 20 20 20 20  int nLeft;..    
31ae1 66 6f 72 28 0a 20 20 20 20 20 20 6e 4c 65 66 74  for(.      nLeft
31ae2 3d 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28  =RTREE_MINCELLS(
31ae3 70 52 74 72 65 65 29 3b 20 0a 20 20 20 20 20 20  pRtree); .      
31ae4 6e 4c 65 66 74 3c 3d 28 6e 43 65 6c 6c 2d 52 54  nLeft<=(nCell-RT
31ae5 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74  REE_MINCELLS(pRt
31ae6 72 65 65 29 29 3b 20 0a 20 20 20 20 20 20 6e 4c  ree)); .      nL
31ae7 65 66 74 2b 2b 0a 20 20 20 20 29 7b 0a 20 20 20  eft++.    ){.   
31ae8 20 20 20 52 74 72 65 65 43 65 6c 6c 20 6c 65 66     RtreeCell lef
31ae9 74 3b 0a 20 20 20 20 20 20 52 74 72 65 65 43 65  t;.      RtreeCe
31aea 6c 6c 20 72 69 67 68 74 3b 0a 20 20 20 20 20 20  ll right;.      
31aeb 69 6e 74 20 6b 6b 3b 0a 20 20 20 20 20 20 66 6c  int kk;.      fl
31aec 6f 61 74 20 6f 76 65 72 6c 61 70 3b 0a 20 20 20  oat overlap;.   
31aed 20 20 20 66 6c 6f 61 74 20 61 72 65 61 3b 0a 0a     float area;..
31aee 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 6c 65        memcpy(&le
31aef 66 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72  ft, &aCell[aaSor
31af0 74 65 64 5b 69 69 5d 5b 30 5d 5d 2c 20 73 69 7a  ted[ii][0]], siz
31af1 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29 3b  eof(RtreeCell));
31af2 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 72  .      memcpy(&r
31af3 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53  ight, &aCell[aaS
31af4 6f 72 74 65 64 5b 69 69 5d 5b 6e 43 65 6c 6c 2d  orted[ii][nCell-
31af5 31 5d 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65  1]], sizeof(Rtre
31af6 65 43 65 6c 6c 29 29 3b 0a 20 20 20 20 20 20 66  eCell));.      f
31af7 6f 72 28 6b 6b 3d 31 3b 20 6b 6b 3c 28 6e 43 65  or(kk=1; kk<(nCe
31af8 6c 6c 2d 31 29 3b 20 6b 6b 2b 2b 29 7b 0a 20 20  ll-1); kk++){.  
31af9 20 20 20 20 20 20 69 66 28 20 6b 6b 3c 6e 4c 65        if( kk<nLe
31afa 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ft ){.          
31afb 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65  cellUnion(pRtree
31afc 2c 20 26 6c 65 66 74 2c 20 26 61 43 65 6c 6c 5b  , &left, &aCell[
31afd 61 61 53 6f 72 74 65 64 5b 69 69 5d 5b 6b 6b 5d  aaSorted[ii][kk]
31afe 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ]);.        }els
31aff 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63 65 6c  e{.          cel
31b00 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 26  lUnion(pRtree, &
31b01 72 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b 61 61  right, &aCell[aa
31b02 53 6f 72 74 65 64 5b 69 69 5d 5b 6b 6b 5d 5d 29  Sorted[ii][kk]])
31b03 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
31b04 20 20 7d 0a 20 20 20 20 20 20 6d 61 72 67 69 6e    }.      margin
31b05 20 2b 3d 20 63 65 6c 6c 4d 61 72 67 69 6e 28 70   += cellMargin(p
31b06 52 74 72 65 65 2c 20 26 6c 65 66 74 29 3b 0a 20  Rtree, &left);. 
31b07 20 20 20 20 20 6d 61 72 67 69 6e 20 2b 3d 20 63       margin += c
31b08 65 6c 6c 4d 61 72 67 69 6e 28 70 52 74 72 65 65  ellMargin(pRtree
31b09 2c 20 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20  , &right);.     
31b0a 20 6f 76 65 72 6c 61 70 20 3d 20 63 65 6c 6c 4f   overlap = cellO
31b0b 76 65 72 6c 61 70 28 70 52 74 72 65 65 2c 20 26  verlap(pRtree, &
31b0c 6c 65 66 74 2c 20 26 72 69 67 68 74 2c 20 31 2c  left, &right, 1,
31b0d 20 2d 31 29 3b 0a 20 20 20 20 20 20 61 72 65 61   -1);.      area
31b0e 20 3d 20 63 65 6c 6c 41 72 65 61 28 70 52 74 72   = cellArea(pRtr
31b0f 65 65 2c 20 26 6c 65 66 74 29 20 2b 20 63 65 6c  ee, &left) + cel
31b10 6c 41 72 65 61 28 70 52 74 72 65 65 2c 20 26 72  lArea(pRtree, &r
31b11 69 67 68 74 29 3b 0a 20 20 20 20 20 20 69 66 28  ight);.      if(
31b12 20 28 6e 4c 65 66 74 3d 3d 52 54 52 45 45 5f 4d   (nLeft==RTREE_M
31b13 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65 29 29  INCELLS(pRtree))
31b14 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f 76 65 72  .       || (over
31b15 6c 61 70 3c 66 42 65 73 74 4f 76 65 72 6c 61 70  lap<fBestOverlap
31b16 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f 76 65  ).       || (ove
31b17 72 6c 61 70 3d 3d 66 42 65 73 74 4f 76 65 72 6c  rlap==fBestOverl
31b18 61 70 20 26 26 20 61 72 65 61 3c 66 42 65 73 74  ap && area<fBest
31b19 41 72 65 61 29 0a 20 20 20 20 20 20 29 7b 0a 20  Area).      ){. 
31b1a 20 20 20 20 20 20 20 69 42 65 73 74 4c 65 66 74         iBestLeft
31b1b 20 3d 20 6e 4c 65 66 74 3b 0a 20 20 20 20 20 20   = nLeft;.      
31b1c 20 20 66 42 65 73 74 4f 76 65 72 6c 61 70 20 3d    fBestOverlap =
31b1d 20 6f 76 65 72 6c 61 70 3b 0a 20 20 20 20 20 20   overlap;.      
31b1e 20 20 66 42 65 73 74 41 72 65 61 20 3d 20 61 72    fBestArea = ar
31b1f 65 61 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ea;.      }.    
31b20 7d 0a 0a 20 20 20 20 69 66 28 20 69 69 3d 3d 30  }..    if( ii==0
31b21 20 7c 7c 20 6d 61 72 67 69 6e 3c 66 42 65 73 74   || margin<fBest
31b22 4d 61 72 67 69 6e 20 29 7b 0a 20 20 20 20 20 20  Margin ){.      
31b23 69 42 65 73 74 44 69 6d 20 3d 20 69 69 3b 0a 20  iBestDim = ii;. 
31b24 20 20 20 20 20 66 42 65 73 74 4d 61 72 67 69 6e       fBestMargin
31b25 20 3d 20 6d 61 72 67 69 6e 3b 0a 20 20 20 20 20   = margin;.     
31b26 20 69 42 65 73 74 53 70 6c 69 74 20 3d 20 69 42   iBestSplit = iB
31b27 65 73 74 4c 65 66 74 3b 0a 20 20 20 20 7d 0a 20  estLeft;.    }. 
31b28 20 7d 0a 0a 20 20 6d 65 6d 63 70 79 28 70 42 62   }..  memcpy(pBb
31b29 6f 78 4c 65 66 74 2c 20 26 61 43 65 6c 6c 5b 61  oxLeft, &aCell[a
31b2a 61 53 6f 72 74 65 64 5b 69 42 65 73 74 44 69 6d  aSorted[iBestDim
31b2b 5d 5b 30 5d 5d 2c 20 73 69 7a 65 6f 66 28 52 74  ][0]], sizeof(Rt
31b2c 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 6d 65 6d  reeCell));.  mem
31b2d 63 70 79 28 70 42 62 6f 78 52 69 67 68 74 2c 20  cpy(pBboxRight, 
31b2e 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b  &aCell[aaSorted[
31b2f 69 42 65 73 74 44 69 6d 5d 5b 69 42 65 73 74 53  iBestDim][iBestS
31b30 70 6c 69 74 5d 5d 2c 20 73 69 7a 65 6f 66 28 52  plit]], sizeof(R
31b31 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 66 6f  treeCell));.  fo
31b32 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c  r(ii=0; ii<nCell
31b33 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52 74 72  ; ii++){.    Rtr
31b34 65 65 4e 6f 64 65 20 2a 70 54 61 72 67 65 74 20  eeNode *pTarget 
31b35 3d 20 28 69 69 3c 69 42 65 73 74 53 70 6c 69 74  = (ii<iBestSplit
31b36 29 3f 70 4c 65 66 74 3a 70 52 69 67 68 74 3b 0a  )?pLeft:pRight;.
31b37 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70      RtreeCell *p
31b38 42 62 6f 78 20 3d 20 28 69 69 3c 69 42 65 73 74  Bbox = (ii<iBest
31b39 53 70 6c 69 74 29 3f 70 42 62 6f 78 4c 65 66 74  Split)?pBboxLeft
31b3a 3a 70 42 62 6f 78 52 69 67 68 74 3b 0a 20 20 20  :pBboxRight;.   
31b3b 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c   RtreeCell *pCel
31b3c 6c 20 3d 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72  l = &aCell[aaSor
31b3d 74 65 64 5b 69 42 65 73 74 44 69 6d 5d 5b 69 69  ted[iBestDim][ii
31b3e 5d 5d 3b 0a 20 20 20 20 6e 6f 64 65 49 6e 73 65  ]];.    nodeInse
31b3f 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  rtCell(pRtree, p
31b40 54 61 72 67 65 74 2c 20 70 43 65 6c 6c 29 3b 0a  Target, pCell);.
31b41 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52      cellUnion(pR
31b42 74 72 65 65 2c 20 70 42 62 6f 78 2c 20 70 43 65  tree, pBbox, pCe
31b43 6c 6c 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69  ll);.  }..  sqli
31b44 74 65 33 5f 66 72 65 65 28 61 61 53 6f 72 74 65  te3_free(aaSorte
31b45 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  d);.  return SQL
31b46 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66  ITE_OK;.}.#endif
31b47 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47 55  ..#if VARIANT_GU
31b48 54 54 4d 41 4e 5f 53 50 4c 49 54 0a 2f 2a 0a 2a  TTMAN_SPLIT./*.*
31b49 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
31b4a 20 6f 66 20 74 68 65 20 72 65 67 75 6c 61 72 20   of the regular 
31b4b 52 2d 74 72 65 65 20 53 70 6c 69 74 4e 6f 64 65  R-tree SplitNode
31b4c 20 66 72 6f 6d 20 47 75 74 74 6d 61 6e 5b 31 39   from Guttman[19
31b4d 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  84]..*/.static i
31b4e 6e 74 20 73 70 6c 69 74 4e 6f 64 65 47 75 74 74  nt splitNodeGutt
31b4f 6d 61 6e 28 0a 20 20 52 74 72 65 65 20 2a 70 52  man(.  Rtree *pR
31b50 74 72 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c  tree,.  RtreeCel
31b51 6c 20 2a 61 43 65 6c 6c 2c 0a 20 20 69 6e 74 20  l *aCell,.  int 
31b52 6e 43 65 6c 6c 2c 0a 20 20 52 74 72 65 65 4e 6f  nCell,.  RtreeNo
31b53 64 65 20 2a 70 4c 65 66 74 2c 0a 20 20 52 74 72  de *pLeft,.  Rtr
31b54 65 65 4e 6f 64 65 20 2a 70 52 69 67 68 74 2c 0a  eeNode *pRight,.
31b55 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 42 62    RtreeCell *pBb
31b56 6f 78 4c 65 66 74 2c 0a 20 20 52 74 72 65 65 43  oxLeft,.  RtreeC
31b57 65 6c 6c 20 2a 70 42 62 6f 78 52 69 67 68 74 0a  ell *pBboxRight.
31b58 29 7b 0a 20 20 69 6e 74 20 69 4c 65 66 74 53 65  ){.  int iLeftSe
31b59 65 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 52  ed = 0;.  int iR
31b5a 69 67 68 74 53 65 65 64 20 3d 20 31 3b 0a 20 20  ightSeed = 1;.  
31b5b 69 6e 74 20 2a 61 69 55 73 65 64 3b 0a 20 20 69  int *aiUsed;.  i
31b5c 6e 74 20 69 3b 0a 0a 20 20 61 69 55 73 65 64 20  nt i;..  aiUsed 
31b5d 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
31b5e 28 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 43 65  (sizeof(int)*nCe
31b5f 6c 6c 29 3b 0a 20 20 6d 65 6d 73 65 74 28 61 69  ll);.  memset(ai
31b60 55 73 65 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Used, 0, sizeof(
31b61 69 6e 74 29 2a 6e 43 65 6c 6c 29 3b 0a 0a 20 20  int)*nCell);..  
31b62 50 69 63 6b 53 65 65 64 73 28 70 52 74 72 65 65  PickSeeds(pRtree
31b63 2c 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c 20  , aCell, nCell, 
31b64 26 69 4c 65 66 74 53 65 65 64 2c 20 26 69 52 69  &iLeftSeed, &iRi
31b65 67 68 74 53 65 65 64 29 3b 0a 0a 20 20 6d 65 6d  ghtSeed);..  mem
31b66 63 70 79 28 70 42 62 6f 78 4c 65 66 74 2c 20 26  cpy(pBboxLeft, &
31b67 61 43 65 6c 6c 5b 69 4c 65 66 74 53 65 65 64 5d  aCell[iLeftSeed]
31b68 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65 43 65  , sizeof(RtreeCe
31b69 6c 6c 29 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70  ll));.  memcpy(p
31b6a 42 62 6f 78 52 69 67 68 74 2c 20 26 61 43 65 6c  BboxRight, &aCel
31b6b 6c 5b 69 52 69 67 68 74 53 65 65 64 5d 2c 20 73  l[iRightSeed], s
31b6c 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29  izeof(RtreeCell)
31b6d 29 3b 0a 20 20 6e 6f 64 65 49 6e 73 65 72 74 43  );.  nodeInsertC
31b6e 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4c 65 66  ell(pRtree, pLef
31b6f 74 2c 20 26 61 43 65 6c 6c 5b 69 4c 65 66 74 53  t, &aCell[iLeftS
31b70 65 65 64 5d 29 3b 0a 20 20 6e 6f 64 65 49 6e 73  eed]);.  nodeIns
31b71 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  ertCell(pRtree, 
31b72 70 52 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b 69  pRight, &aCell[i
31b73 52 69 67 68 74 53 65 65 64 5d 29 3b 0a 20 20 61  RightSeed]);.  a
31b74 69 55 73 65 64 5b 69 4c 65 66 74 53 65 65 64 5d  iUsed[iLeftSeed]
31b75 20 3d 20 31 3b 0a 20 20 61 69 55 73 65 64 5b 69   = 1;.  aiUsed[i
31b76 52 69 67 68 74 53 65 65 64 5d 20 3d 20 31 3b 0a  RightSeed] = 1;.
31b77 0a 20 20 66 6f 72 28 69 3d 6e 43 65 6c 6c 2d 32  .  for(i=nCell-2
31b78 3b 20 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20  ; i>0; i--){.   
31b79 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 4e 65 78   RtreeCell *pNex
31b7a 74 3b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 50  t;.    pNext = P
31b7b 69 63 6b 4e 65 78 74 28 70 52 74 72 65 65 2c 20  ickNext(pRtree, 
31b7c 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 70 42  aCell, nCell, pB
31b7d 62 6f 78 4c 65 66 74 2c 20 70 42 62 6f 78 52 69  boxLeft, pBboxRi
31b7e 67 68 74 2c 20 61 69 55 73 65 64 29 3b 0a 20 20  ght, aiUsed);.  
31b7f 20 20 66 6c 6f 61 74 20 64 69 66 66 20 3d 20 20    float diff =  
31b80 0a 20 20 20 20 20 20 63 65 6c 6c 47 72 6f 77 74  .      cellGrowt
31b81 68 28 70 52 74 72 65 65 2c 20 70 42 62 6f 78 4c  h(pRtree, pBboxL
31b82 65 66 74 2c 20 70 4e 65 78 74 29 20 2d 20 0a 20  eft, pNext) - . 
31b83 20 20 20 20 20 63 65 6c 6c 47 72 6f 77 74 68 28       cellGrowth(
31b84 70 52 74 72 65 65 2c 20 70 42 62 6f 78 52 69 67  pRtree, pBboxRig
31b85 68 74 2c 20 70 4e 65 78 74 29 0a 20 20 20 20 3b  ht, pNext).    ;
31b86 0a 20 20 20 20 69 66 28 20 28 52 54 52 45 45 5f  .    if( (RTREE_
31b87 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65 29  MINCELLS(pRtree)
31b88 2d 4e 43 45 4c 4c 28 70 52 69 67 68 74 29 3d 3d  -NCELL(pRight)==
31b89 69 29 0a 20 20 20 20 20 7c 7c 20 28 64 69 66 66  i).     || (diff
31b8a 3e 30 2e 30 20 26 26 20 28 52 54 52 45 45 5f 4d  >0.0 && (RTREE_M
31b8b 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65 29 2d  INCELLS(pRtree)-
31b8c 4e 43 45 4c 4c 28 70 4c 65 66 74 29 21 3d 69 29  NCELL(pLeft)!=i)
31b8d 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 6e  ).    ){.      n
31b8e 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52  odeInsertCell(pR
31b8f 74 72 65 65 2c 20 70 52 69 67 68 74 2c 20 70 4e  tree, pRight, pN
31b90 65 78 74 29 3b 0a 20 20 20 20 20 20 63 65 6c 6c  ext);.      cell
31b91 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 70 42  Union(pRtree, pB
31b92 62 6f 78 52 69 67 68 74 2c 20 70 4e 65 78 74 29  boxRight, pNext)
31b93 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
31b94 20 20 20 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c     nodeInsertCel
31b95 6c 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 2c  l(pRtree, pLeft,
31b96 20 70 4e 65 78 74 29 3b 0a 20 20 20 20 20 20 63   pNext);.      c
31b97 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c  ellUnion(pRtree,
31b98 20 70 42 62 6f 78 4c 65 66 74 2c 20 70 4e 65 78   pBboxLeft, pNex
31b99 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  t);.    }.  }.. 
31b9a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 69   sqlite3_free(ai
31b9b 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  Used);.  return 
31b9c 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e  SQLITE_OK;.}.#en
31b9d 64 69 66 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  dif..static int 
31b9e 75 70 64 61 74 65 4d 61 70 70 69 6e 67 28 0a 20  updateMapping(. 
31b9f 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20   Rtree *pRtree, 
31ba0 0a 20 20 69 36 34 20 69 52 6f 77 69 64 2c 20 0a  .  i64 iRowid, .
31ba1 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f    RtreeNode *pNo
31ba2 64 65 2c 20 0a 20 20 69 6e 74 20 69 48 65 69 67  de, .  int iHeig
31ba3 68 74 0a 29 7b 0a 20 20 69 6e 74 20 28 2a 78 53  ht.){.  int (*xS
31ba4 65 74 4d 61 70 70 69 6e 67 29 28 52 74 72 65 65  etMapping)(Rtree
31ba5 20 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36   *, sqlite3_int6
31ba6 34 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  4, sqlite3_int64
31ba7 29 3b 0a 20 20 78 53 65 74 4d 61 70 70 69 6e 67  );.  xSetMapping
31ba8 20 3d 20 28 28 69 48 65 69 67 68 74 3d 3d 30 29   = ((iHeight==0)
31ba9 3f 72 6f 77 69 64 57 72 69 74 65 3a 70 61 72 65  ?rowidWrite:pare
31baa 6e 74 57 72 69 74 65 29 3b 0a 20 20 69 66 28 20  ntWrite);.  if( 
31bab 69 48 65 69 67 68 74 3e 30 20 29 7b 0a 20 20 20  iHeight>0 ){.   
31bac 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 43 68 69   RtreeNode *pChi
31bad 6c 64 20 3d 20 6e 6f 64 65 48 61 73 68 4c 6f 6f  ld = nodeHashLoo
31bae 6b 75 70 28 70 52 74 72 65 65 2c 20 69 52 6f 77  kup(pRtree, iRow
31baf 69 64 29 3b 0a 20 20 20 20 69 66 28 20 70 43 68  id);.    if( pCh
31bb0 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 6e 6f 64  ild ){.      nod
31bb1 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
31bb2 20 70 43 68 69 6c 64 2d 3e 70 50 61 72 65 6e 74   pChild->pParent
31bb3 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65 66  );.      nodeRef
31bb4 65 72 65 6e 63 65 28 70 4e 6f 64 65 29 3b 0a 20  erence(pNode);. 
31bb5 20 20 20 20 20 70 43 68 69 6c 64 2d 3e 70 50 61       pChild->pPa
31bb6 72 65 6e 74 20 3d 20 70 4e 6f 64 65 3b 0a 20 20  rent = pNode;.  
31bb7 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
31bb8 20 78 53 65 74 4d 61 70 70 69 6e 67 28 70 52 74   xSetMapping(pRt
31bb9 72 65 65 2c 20 69 52 6f 77 69 64 2c 20 70 4e 6f  ree, iRowid, pNo
31bba 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 7d 0a 0a 73  de->iNode);.}..s
31bbb 74 61 74 69 63 20 69 6e 74 20 53 70 6c 69 74 4e  tatic int SplitN
31bbc 6f 64 65 28 0a 20 20 52 74 72 65 65 20 2a 70 52  ode(.  Rtree *pR
31bbd 74 72 65 65 2c 0a 20 20 52 74 72 65 65 4e 6f 64  tree,.  RtreeNod
31bbe 65 20 2a 70 4e 6f 64 65 2c 0a 20 20 52 74 72 65  e *pNode,.  Rtre
31bbf 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 2c 0a 20 20  eCell *pCell,.  
31bc0 69 6e 74 20 69 48 65 69 67 68 74 0a 29 7b 0a 20  int iHeight.){. 
31bc1 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 65   int i;.  int ne
31bc2 77 43 65 6c 6c 49 73 52 69 67 68 74 20 3d 20 30  wCellIsRight = 0
31bc3 3b 0a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ;..  int rc = SQ
31bc4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e  LITE_OK;.  int n
31bc5 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f  Cell = NCELL(pNo
31bc6 64 65 29 3b 0a 20 20 52 74 72 65 65 43 65 6c 6c  de);.  RtreeCell
31bc7 20 2a 61 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 2a   *aCell;.  int *
31bc8 61 69 55 73 65 64 3b 0a 0a 20 20 52 74 72 65 65  aiUsed;..  Rtree
31bc9 4e 6f 64 65 20 2a 70 4c 65 66 74 20 3d 20 30 3b  Node *pLeft = 0;
31bca 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 52  .  RtreeNode *pR
31bcb 69 67 68 74 20 3d 20 30 3b 0a 0a 20 20 52 74 72  ight = 0;..  Rtr
31bcc 65 65 43 65 6c 6c 20 6c 65 66 74 62 62 6f 78 3b  eeCell leftbbox;
31bcd 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 72 69 67  .  RtreeCell rig
31bce 68 74 62 62 6f 78 3b 0a 0a 20 20 2f 2a 20 41 6c  htbbox;..  /* Al
31bcf 6c 6f 63 61 74 65 20 61 6e 20 61 72 72 61 79 20  locate an array 
31bd0 61 6e 64 20 70 6f 70 75 6c 61 74 65 20 69 74 20  and populate it 
31bd1 77 69 74 68 20 61 20 63 6f 70 79 20 6f 66 20 70  with a copy of p
31bd2 43 65 6c 6c 20 61 6e 64 20 0a 20 20 2a 2a 20 61  Cell and .  ** a
31bd3 6c 6c 20 63 65 6c 6c 73 20 66 72 6f 6d 20 6e 6f  ll cells from no
31bd4 64 65 20 70 4c 65 66 74 2e 20 54 68 65 6e 20 7a  de pLeft. Then z
31bd5 65 72 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  ero the original
31bd6 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 61 43   node..  */.  aC
31bd7 65 6c 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  ell = sqlite3_ma
31bd8 6c 6c 6f 63 28 28 73 69 7a 65 6f 66 28 52 74 72  lloc((sizeof(Rtr
31bd9 65 65 43 65 6c 6c 29 2b 73 69 7a 65 6f 66 28 69  eeCell)+sizeof(i
31bda 6e 74 29 29 2a 28 6e 43 65 6c 6c 2b 31 29 29 3b  nt))*(nCell+1));
31bdb 0a 20 20 69 66 28 20 21 61 43 65 6c 6c 20 29 7b  .  if( !aCell ){
31bdc 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
31bdd 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f  _NOMEM;.    goto
31bde 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a   splitnode_out;.
31bdf 20 20 7d 0a 20 20 61 69 55 73 65 64 20 3d 20 28    }.  aiUsed = (
31be0 69 6e 74 20 2a 29 26 61 43 65 6c 6c 5b 6e 43 65  int *)&aCell[nCe
31be1 6c 6c 2b 31 5d 3b 0a 20 20 6d 65 6d 73 65 74 28  ll+1];.  memset(
31be2 61 69 55 73 65 64 2c 20 30 2c 20 73 69 7a 65 6f  aiUsed, 0, sizeo
31be3 66 28 69 6e 74 29 2a 28 6e 43 65 6c 6c 2b 31 29  f(int)*(nCell+1)
31be4 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
31be5 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  nCell; i++){.   
31be6 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74   nodeGetCell(pRt
31be7 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 2c 20 26  ree, pNode, i, &
31be8 61 43 65 6c 6c 5b 69 5d 29 3b 0a 20 20 7d 0a 20  aCell[i]);.  }. 
31be9 20 6e 6f 64 65 5a 65 72 6f 28 70 52 74 72 65 65   nodeZero(pRtree
31bea 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 6d 65 6d 63  , pNode);.  memc
31beb 70 79 28 26 61 43 65 6c 6c 5b 6e 43 65 6c 6c 5d  py(&aCell[nCell]
31bec 2c 20 70 43 65 6c 6c 2c 20 73 69 7a 65 6f 66 28  , pCell, sizeof(
31bed 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 6e  RtreeCell));.  n
31bee 43 65 6c 6c 2b 2b 3b 0a 0a 20 20 69 66 28 20 70  Cell++;..  if( p
31bef 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29  Node->iNode==1 )
31bf0 7b 0a 20 20 20 20 70 52 69 67 68 74 20 3d 20 6e  {.    pRight = n
31bf1 6f 64 65 4e 65 77 28 70 52 74 72 65 65 2c 20 70  odeNew(pRtree, p
31bf2 4e 6f 64 65 2c 20 31 29 3b 0a 20 20 20 20 70 4c  Node, 1);.    pL
31bf3 65 66 74 20 3d 20 6e 6f 64 65 4e 65 77 28 70 52  eft = nodeNew(pR
31bf4 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 31 29 3b  tree, pNode, 1);
31bf5 0a 20 20 20 20 70 52 74 72 65 65 2d 3e 69 44 65  .    pRtree->iDe
31bf6 70 74 68 2b 2b 3b 0a 20 20 20 20 70 4e 6f 64 65  pth++;.    pNode
31bf7 2d 3e 69 73 44 69 72 74 79 20 3d 20 31 3b 0a 20  ->isDirty = 1;. 
31bf8 20 20 20 77 72 69 74 65 49 6e 74 31 36 28 70 4e     writeInt16(pN
31bf9 6f 64 65 2d 3e 7a 44 61 74 61 2c 20 70 52 74 72  ode->zData, pRtr
31bfa 65 65 2d 3e 69 44 65 70 74 68 29 3b 0a 20 20 7d  ee->iDepth);.  }
31bfb 65 6c 73 65 7b 0a 20 20 20 20 70 4c 65 66 74 20  else{.    pLeft 
31bfc 3d 20 70 4e 6f 64 65 3b 0a 20 20 20 20 70 52 69  = pNode;.    pRi
31bfd 67 68 74 20 3d 20 6e 6f 64 65 4e 65 77 28 70 52  ght = nodeNew(pR
31bfe 74 72 65 65 2c 20 70 4c 65 66 74 2d 3e 70 50 61  tree, pLeft->pPa
31bff 72 65 6e 74 2c 20 31 29 3b 0a 20 20 20 20 6e 6f  rent, 1);.    no
31c00 64 65 52 65 66 65 72 65 6e 63 65 28 70 4c 65 66  deReference(pLef
31c01 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21  t);.  }..  if( !
31c02 70 4c 65 66 74 20 7c 7c 20 21 70 52 69 67 68 74  pLeft || !pRight
31c03 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
31c04 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67  ITE_NOMEM;.    g
31c05 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75  oto splitnode_ou
31c06 74 3b 0a 20 20 7d 0a 0a 20 20 6d 65 6d 73 65 74  t;.  }..  memset
31c07 28 70 4c 65 66 74 2d 3e 7a 44 61 74 61 2c 20 30  (pLeft->zData, 0
31c08 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53  , pRtree->iNodeS
31c09 69 7a 65 29 3b 0a 20 20 6d 65 6d 73 65 74 28 70  ize);.  memset(p
31c0a 52 69 67 68 74 2d 3e 7a 44 61 74 61 2c 20 30 2c  Right->zData, 0,
31c0b 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69   pRtree->iNodeSi
31c0c 7a 65 29 3b 0a 0a 20 20 72 63 20 3d 20 41 73 73  ze);..  rc = Ass
31c0d 69 67 6e 43 65 6c 6c 73 28 70 52 74 72 65 65 2c  ignCells(pRtree,
31c0e 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 70   aCell, nCell, p
31c0f 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 26 6c  Left, pRight, &l
31c10 65 66 74 62 62 6f 78 2c 20 26 72 69 67 68 74 62  eftbbox, &rightb
31c11 62 6f 78 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  box);.  if( rc!=
31c12 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
31c13 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f   goto splitnode_
31c14 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45  out;.  }..  /* E
31c15 6e 73 75 72 65 20 62 6f 74 68 20 63 68 69 6c 64  nsure both child
31c16 20 6e 6f 64 65 73 20 68 61 76 65 20 6e 6f 64 65   nodes have node
31c17 20 6e 75 6d 62 65 72 73 20 61 73 73 69 67 6e 65   numbers assigne
31c18 64 20 74 6f 20 74 68 65 6d 2e 20 2a 2f 0a 20 20  d to them. */.  
31c19 69 66 28 20 28 30 3d 3d 70 52 69 67 68 74 2d 3e  if( (0==pRight->
31c1a 69 4e 6f 64 65 20 26 26 20 53 51 4c 49 54 45 5f  iNode && SQLITE_
31c1b 4f 4b 21 3d 28 72 63 20 3d 20 6e 6f 64 65 57 72  OK!=(rc = nodeWr
31c1c 69 74 65 28 70 52 74 72 65 65 2c 20 70 52 69 67  ite(pRtree, pRig
31c1d 68 74 29 29 29 0a 20 20 20 7c 7c 20 28 30 3d 3d  ht))).   || (0==
31c1e 70 4c 65 66 74 2d 3e 69 4e 6f 64 65 20 26 26 20  pLeft->iNode && 
31c1f 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
31c20 20 6e 6f 64 65 57 72 69 74 65 28 70 52 74 72 65   nodeWrite(pRtre
31c21 65 2c 20 70 4c 65 66 74 29 29 29 0a 20 20 29 7b  e, pLeft))).  ){
31c22 0a 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e  .    goto splitn
31c23 6f 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20  ode_out;.  }..  
31c24 72 69 67 68 74 62 62 6f 78 2e 69 52 6f 77 69 64  rightbbox.iRowid
31c25 20 3d 20 70 52 69 67 68 74 2d 3e 69 4e 6f 64 65   = pRight->iNode
31c26 3b 0a 20 20 6c 65 66 74 62 62 6f 78 2e 69 52 6f  ;.  leftbbox.iRo
31c27 77 69 64 20 3d 20 70 4c 65 66 74 2d 3e 69 4e 6f  wid = pLeft->iNo
31c28 64 65 3b 0a 0a 20 20 69 66 28 20 70 4e 6f 64 65  de;..  if( pNode
31c29 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a 20 20  ->iNode==1 ){.  
31c2a 20 20 72 63 20 3d 20 72 74 72 65 65 49 6e 73 65    rc = rtreeInse
31c2b 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  rtCell(pRtree, p
31c2c 4c 65 66 74 2d 3e 70 50 61 72 65 6e 74 2c 20 26  Left->pParent, &
31c2d 6c 65 66 74 62 62 6f 78 2c 20 69 48 65 69 67 68  leftbbox, iHeigh
31c2e 74 2b 31 29 3b 0a 20 20 20 20 69 66 28 20 72 63  t+1);.    if( rc
31c2f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
31c30 20 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e       goto splitn
31c31 6f 64 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  ode_out;.    }. 
31c32 20 7d 65 6c 73 65 7b 0a 20 20 20 20 52 74 72 65   }else{.    Rtre
31c33 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 20 3d  eNode *pParent =
31c34 20 70 4c 65 66 74 2d 3e 70 50 61 72 65 6e 74 3b   pLeft->pParent;
31c35 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 20 3d  .    int iCell =
31c36 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64 65 78   nodeParentIndex
31c37 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 29 3b  (pRtree, pLeft);
31c38 0a 20 20 20 20 6e 6f 64 65 4f 76 65 72 77 72 69  .    nodeOverwri
31c39 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  teCell(pRtree, p
31c3a 50 61 72 65 6e 74 2c 20 26 6c 65 66 74 62 62 6f  Parent, &leftbbo
31c3b 78 2c 20 69 43 65 6c 6c 29 3b 0a 20 20 20 20 41  x, iCell);.    A
31c3c 64 6a 75 73 74 54 72 65 65 28 70 52 74 72 65 65  djustTree(pRtree
31c3d 2c 20 70 50 61 72 65 6e 74 2c 20 26 6c 65 66 74  , pParent, &left
31c3e 62 62 6f 78 29 3b 0a 20 20 7d 0a 20 20 69 66 28  bbox);.  }.  if(
31c3f 20 28 72 63 20 3d 20 72 74 72 65 65 49 6e 73 65   (rc = rtreeInse
31c40 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  rtCell(pRtree, p
31c41 52 69 67 68 74 2d 3e 70 50 61 72 65 6e 74 2c 20  Right->pParent, 
31c42 26 72 69 67 68 74 62 62 6f 78 2c 20 69 48 65 69  &rightbbox, iHei
31c43 67 68 74 2b 31 29 29 20 29 7b 0a 20 20 20 20 67  ght+1)) ){.    g
31c44 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75  oto splitnode_ou
31c45 74 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69 3d  t;.  }..  for(i=
31c46 30 3b 20 69 3c 4e 43 45 4c 4c 28 70 52 69 67 68  0; i<NCELL(pRigh
31c47 74 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 36  t); i++){.    i6
31c48 34 20 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65 47  4 iRowid = nodeG
31c49 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20  etRowid(pRtree, 
31c4a 70 52 69 67 68 74 2c 20 69 29 3b 0a 20 20 20 20  pRight, i);.    
31c4b 72 63 20 3d 20 75 70 64 61 74 65 4d 61 70 70 69  rc = updateMappi
31c4c 6e 67 28 70 52 74 72 65 65 2c 20 69 52 6f 77 69  ng(pRtree, iRowi
31c4d 64 2c 20 70 52 69 67 68 74 2c 20 69 48 65 69 67  d, pRight, iHeig
31c4e 68 74 29 3b 0a 20 20 20 20 69 66 28 20 69 52 6f  ht);.    if( iRo
31c4f 77 69 64 3d 3d 70 43 65 6c 6c 2d 3e 69 52 6f 77  wid==pCell->iRow
31c50 69 64 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 43  id ){.      newC
31c51 65 6c 6c 49 73 52 69 67 68 74 20 3d 20 31 3b 0a  ellIsRight = 1;.
31c52 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
31c53 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
31c54 20 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e       goto splitn
31c55 6f 64 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  ode_out;.    }. 
31c56 20 7d 0a 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e   }.  if( pNode->
31c57 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a 20 20 20 20  iNode==1 ){.    
31c58 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 45 4c 4c  for(i=0; i<NCELL
31c59 28 70 4c 65 66 74 29 3b 20 69 2b 2b 29 7b 0a 20  (pLeft); i++){. 
31c5a 20 20 20 20 20 69 36 34 20 69 52 6f 77 69 64 20       i64 iRowid 
31c5b 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70  = nodeGetRowid(p
31c5c 52 74 72 65 65 2c 20 70 4c 65 66 74 2c 20 69 29  Rtree, pLeft, i)
31c5d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 75 70 64  ;.      rc = upd
31c5e 61 74 65 4d 61 70 70 69 6e 67 28 70 52 74 72 65  ateMapping(pRtre
31c5f 65 2c 20 69 52 6f 77 69 64 2c 20 70 4c 65 66 74  e, iRowid, pLeft
31c60 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20 20 20  , iHeight);.    
31c61 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
31c62 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67  _OK ){.        g
31c63 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75  oto splitnode_ou
31c64 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  t;.      }.    }
31c65 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 65 77  .  }else if( new
31c66 43 65 6c 6c 49 73 52 69 67 68 74 3d 3d 30 20 29  CellIsRight==0 )
31c67 7b 0a 20 20 20 20 72 63 20 3d 20 75 70 64 61 74  {.    rc = updat
31c68 65 4d 61 70 70 69 6e 67 28 70 52 74 72 65 65 2c  eMapping(pRtree,
31c69 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 2c 20   pCell->iRowid, 
31c6a 70 4c 65 66 74 2c 20 69 48 65 69 67 68 74 29 3b  pLeft, iHeight);
31c6b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d  .  }..  if( rc==
31c6c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
31c6d 20 72 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73   rc = nodeReleas
31c6e 65 28 70 52 74 72 65 65 2c 20 70 52 69 67 68 74  e(pRtree, pRight
31c6f 29 3b 0a 20 20 20 20 70 52 69 67 68 74 20 3d 20  );.    pRight = 
31c70 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  0;.  }.  if( rc=
31c71 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
31c72 20 20 72 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61    rc = nodeRelea
31c73 73 65 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74  se(pRtree, pLeft
31c74 29 3b 0a 20 20 20 20 70 4c 65 66 74 20 3d 20 30  );.    pLeft = 0
31c75 3b 0a 20 20 7d 0a 0a 73 70 6c 69 74 6e 6f 64 65  ;.  }..splitnode
31c76 5f 6f 75 74 3a 0a 20 20 6e 6f 64 65 52 65 6c 65  _out:.  nodeRele
31c77 61 73 65 28 70 52 74 72 65 65 2c 20 70 52 69 67  ase(pRtree, pRig
31c78 68 74 29 3b 0a 20 20 6e 6f 64 65 52 65 6c 65 61  ht);.  nodeRelea
31c79 73 65 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74  se(pRtree, pLeft
31c7a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
31c7b 65 28 61 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75  e(aCell);.  retu
31c7c 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63  rn rc;.}..static
31c7d 20 69 6e 74 20 66 69 78 4c 65 61 66 50 61 72 65   int fixLeafPare
31c7e 6e 74 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  nt(Rtree *pRtree
31c7f 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65  , RtreeNode *pLe
31c80 61 66 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  af){.  int rc = 
31c81 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
31c82 20 70 4c 65 61 66 2d 3e 69 4e 6f 64 65 21 3d 31   pLeaf->iNode!=1
31c83 20 26 26 20 70 4c 65 61 66 2d 3e 70 50 61 72 65   && pLeaf->pPare
31c84 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  nt==0 ){.    sql
31c85 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
31c86 70 52 74 72 65 65 2d 3e 70 52 65 61 64 50 61 72  pRtree->pReadPar
31c87 65 6e 74 2c 20 31 2c 20 70 4c 65 61 66 2d 3e 69  ent, 1, pLeaf->i
31c88 4e 6f 64 65 29 3b 0a 20 20 20 20 69 66 28 20 73  Node);.    if( s
31c89 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72  qlite3_step(pRtr
31c8a 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e 74 29  ee->pReadParent)
31c8b 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  ==SQLITE_ROW ){.
31c8c 20 20 20 20 20 20 69 36 34 20 69 4e 6f 64 65 20        i64 iNode 
31c8d 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
31c8e 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70  _int64(pRtree->p
31c8f 52 65 61 64 50 61 72 65 6e 74 2c 20 30 29 3b 0a  ReadParent, 0);.
31c90 20 20 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 41        rc = nodeA
31c91 63 71 75 69 72 65 28 70 52 74 72 65 65 2c 20 69  cquire(pRtree, i
31c92 4e 6f 64 65 2c 20 30 2c 20 26 70 4c 65 61 66 2d  Node, 0, &pLeaf-
31c93 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 7d  >pParent);.    }
31c94 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
31c95 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
31c96 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
31c97 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70  _reset(pRtree->p
31c98 52 65 61 64 50 61 72 65 6e 74 29 3b 0a 20 20 20  ReadParent);.   
31c99 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
31c9a 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  OK ){.      rc =
31c9b 20 66 69 78 4c 65 61 66 50 61 72 65 6e 74 28 70   fixLeafParent(p
31c9c 52 74 72 65 65 2c 20 70 4c 65 61 66 2d 3e 70 50  Rtree, pLeaf->pP
31c9d 61 72 65 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20  arent);.    }.  
31c9e 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
31c9f 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 6c  ..static int del
31ca0 65 74 65 43 65 6c 6c 28 52 74 72 65 65 20 2a 2c  eteCell(Rtree *,
31ca1 20 52 74 72 65 65 4e 6f 64 65 20 2a 2c 20 69 6e   RtreeNode *, in
31ca2 74 2c 20 69 6e 74 29 3b 0a 0a 73 74 61 74 69 63  t, int);..static
31ca3 20 69 6e 74 20 72 65 6d 6f 76 65 4e 6f 64 65 28   int removeNode(
31ca4 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52  Rtree *pRtree, R
31ca5 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c  treeNode *pNode,
31ca6 20 69 6e 74 20 69 48 65 69 67 68 74 29 7b 0a 20   int iHeight){. 
31ca7 20 69 6e 74 20 72 63 3b 0a 20 20 52 74 72 65 65   int rc;.  Rtree
31ca8 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 3b 0a 20  Node *pParent;. 
31ca9 20 69 6e 74 20 69 43 65 6c 6c 3b 0a 0a 20 20 61   int iCell;..  a
31caa 73 73 65 72 74 28 20 70 4e 6f 64 65 2d 3e 6e 52  ssert( pNode->nR
31cab 65 66 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 52  ef==1 );..  /* R
31cac 65 6d 6f 76 65 20 74 68 65 20 65 6e 74 72 79 20  emove the entry 
31cad 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 63 65  in the parent ce
31cae 6c 6c 2e 20 2a 2f 0a 20 20 69 43 65 6c 6c 20 3d  ll. */.  iCell =
31caf 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64 65 78   nodeParentIndex
31cb0 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b  (pRtree, pNode);
31cb1 0a 20 20 70 50 61 72 65 6e 74 20 3d 20 70 4e 6f  .  pParent = pNo
31cb2 64 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 70  de->pParent;.  p
31cb3 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20  Node->pParent = 
31cb4 30 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  0;.  if( SQLITE_
31cb5 4f 4b 21 3d 28 72 63 20 3d 20 64 65 6c 65 74 65  OK!=(rc = delete
31cb6 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 50 61  Cell(pRtree, pPa
31cb7 72 65 6e 74 2c 20 69 43 65 6c 6c 2c 20 69 48 65  rent, iCell, iHe
31cb8 69 67 68 74 2b 31 29 29 20 0a 20 20 20 7c 7c 20  ight+1)) .   || 
31cb9 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
31cba 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74   nodeRelease(pRt
31cbb 72 65 65 2c 20 70 50 61 72 65 6e 74 29 29 0a 20  ree, pParent)). 
31cbc 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
31cbd 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 6d  c;.  }..  /* Rem
31cbe 6f 76 65 20 74 68 65 20 78 78 78 5f 6e 6f 64 65  ove the xxx_node
31cbf 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 73 71 6c   entry. */.  sql
31cc0 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
31cc1 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 4e  pRtree->pDeleteN
31cc2 6f 64 65 2c 20 31 2c 20 70 4e 6f 64 65 2d 3e 69  ode, 1, pNode->i
31cc3 4e 6f 64 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Node);.  sqlite3
31cc4 5f 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70 44  _step(pRtree->pD
31cc5 65 6c 65 74 65 4e 6f 64 65 29 3b 0a 20 20 69 66  eleteNode);.  if
31cc6 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  ( SQLITE_OK!=(rc
31cc7 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74   = sqlite3_reset
31cc8 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65  (pRtree->pDelete
31cc9 4e 6f 64 65 29 29 20 29 7b 0a 20 20 20 20 72 65  Node)) ){.    re
31cca 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
31ccb 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 78 78  /* Remove the xx
31ccc 78 5f 70 61 72 65 6e 74 20 65 6e 74 72 79 2e 20  x_parent entry. 
31ccd 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  */.  sqlite3_bin
31cce 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e  d_int64(pRtree->
31ccf 70 44 65 6c 65 74 65 50 61 72 65 6e 74 2c 20 31  pDeleteParent, 1
31cd0 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b  , pNode->iNode);
31cd1 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  .  sqlite3_step(
31cd2 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 50  pRtree->pDeleteP
31cd3 61 72 65 6e 74 29 3b 0a 20 20 69 66 28 20 53 51  arent);.  if( SQ
31cd4 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73  LITE_OK!=(rc = s
31cd5 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74  qlite3_reset(pRt
31cd6 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61 72 65  ree->pDeletePare
31cd7 6e 74 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75  nt)) ){.    retu
31cd8 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 0a 20 20  rn rc;.  }.  .  
31cd9 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 6e 6f  /* Remove the no
31cda 64 65 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d 6d  de from the in-m
31cdb 65 6d 6f 72 79 20 68 61 73 68 20 74 61 62 6c 65  emory hash table
31cdc 20 61 6e 64 20 6c 69 6e 6b 20 69 74 20 69 6e 74   and link it int
31cdd 6f 0a 20 20 2a 2a 20 74 68 65 20 52 74 72 65 65  o.  ** the Rtree
31cde 2e 70 44 65 6c 65 74 65 64 20 6c 69 73 74 2e 20  .pDeleted list. 
31cdf 49 74 73 20 63 6f 6e 74 65 6e 74 73 20 77 69 6c  Its contents wil
31ce0 6c 20 62 65 20 72 65 2d 69 6e 73 65 72 74 65 64  l be re-inserted
31ce1 20 6c 61 74 65 72 20 6f 6e 2e 0a 20 20 2a 2f 0a   later on..  */.
31ce2 20 20 6e 6f 64 65 48 61 73 68 44 65 6c 65 74 65    nodeHashDelete
31ce3 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b  (pRtree, pNode);
31ce4 0a 20 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20  .  pNode->iNode 
31ce5 3d 20 69 48 65 69 67 68 74 3b 0a 20 20 70 4e 6f  = iHeight;.  pNo
31ce6 64 65 2d 3e 70 4e 65 78 74 20 3d 20 70 52 74 72  de->pNext = pRtr
31ce7 65 65 2d 3e 70 44 65 6c 65 74 65 64 3b 0a 20 20  ee->pDeleted;.  
31ce8 70 4e 6f 64 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20  pNode->nRef++;. 
31ce9 20 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65   pRtree->pDelete
31cea 64 20 3d 20 70 4e 6f 64 65 3b 0a 0a 20 20 72 65  d = pNode;..  re
31ceb 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
31cec 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  }..static void f
31ced 69 78 42 6f 75 6e 64 69 6e 67 42 6f 78 28 52 74  ixBoundingBox(Rt
31cee 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
31cef 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a  eeNode *pNode){.
31cf0 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61    RtreeNode *pPa
31cf1 72 65 6e 74 20 3d 20 70 4e 6f 64 65 2d 3e 70 50  rent = pNode->pP
31cf2 61 72 65 6e 74 3b 0a 20 20 69 66 28 20 70 50 61  arent;.  if( pPa
31cf3 72 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 20  rent ){.    int 
31cf4 69 69 3b 20 0a 20 20 20 20 69 6e 74 20 6e 43 65  ii; .    int nCe
31cf5 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65  ll = NCELL(pNode
31cf6 29 3b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c  );.    RtreeCell
31cf7 20 62 6f 78 3b 20 20 20 20 20 20 20 20 20 20 20   box;           
31cf8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31cf9 20 2f 2a 20 42 6f 75 6e 64 69 6e 67 20 62 6f 78   /* Bounding box
31cfa 20 66 6f 72 20 70 4e 6f 64 65 20 2a 2f 0a 20 20   for pNode */.  
31cfb 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52    nodeGetCell(pR
31cfc 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 30 2c 20  tree, pNode, 0, 
31cfd 26 62 6f 78 29 3b 0a 20 20 20 20 66 6f 72 28 69  &box);.    for(i
31cfe 69 3d 31 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69  i=1; ii<nCell; i
31cff 69 2b 2b 29 7b 0a 20 20 20 20 20 20 52 74 72 65  i++){.      Rtre
31d00 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20 20  eCell cell;.    
31d01 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52    nodeGetCell(pR
31d02 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 69 2c  tree, pNode, ii,
31d03 20 26 63 65 6c 6c 29 3b 0a 20 20 20 20 20 20 63   &cell);.      c
31d04 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c  ellUnion(pRtree,
31d05 20 26 62 6f 78 2c 20 26 63 65 6c 6c 29 3b 0a 20   &box, &cell);. 
31d06 20 20 20 7d 0a 20 20 20 20 62 6f 78 2e 69 52 6f     }.    box.iRo
31d07 77 69 64 20 3d 20 70 4e 6f 64 65 2d 3e 69 4e 6f  wid = pNode->iNo
31d08 64 65 3b 0a 20 20 20 20 69 69 20 3d 20 6e 6f 64  de;.    ii = nod
31d09 65 50 61 72 65 6e 74 49 6e 64 65 78 28 70 52 74  eParentIndex(pRt
31d0a 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20  ree, pNode);.   
31d0b 20 6e 6f 64 65 4f 76 65 72 77 72 69 74 65 43 65   nodeOverwriteCe
31d0c 6c 6c 28 70 52 74 72 65 65 2c 20 70 50 61 72 65  ll(pRtree, pPare
31d0d 6e 74 2c 20 26 62 6f 78 2c 20 69 69 29 3b 0a 20  nt, &box, ii);. 
31d0e 20 20 20 66 69 78 42 6f 75 6e 64 69 6e 67 42 6f     fixBoundingBo
31d0f 78 28 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e  x(pRtree, pParen
31d10 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  t);.  }.}../*.**
31d11 20 44 65 6c 65 74 65 20 74 68 65 20 63 65 6c 6c   Delete the cell
31d12 20 61 74 20 69 6e 64 65 78 20 69 43 65 6c 6c 20   at index iCell 
31d13 6f 66 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 41  of node pNode. A
31d14 66 74 65 72 20 72 65 6d 6f 76 69 6e 67 20 74 68  fter removing th
31d15 65 0a 2a 2a 20 63 65 6c 6c 2c 20 61 64 6a 75 73  e.** cell, adjus
31d16 74 20 74 68 65 20 72 2d 74 72 65 65 20 64 61 74  t the r-tree dat
31d17 61 20 73 74 72 75 63 74 75 72 65 20 69 66 20 72  a structure if r
31d18 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 73 74 61 74  equired..*/.stat
31d19 69 63 20 69 6e 74 20 64 65 6c 65 74 65 43 65 6c  ic int deleteCel
31d1a 6c 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  l(Rtree *pRtree,
31d1b 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
31d1c 65 2c 20 69 6e 74 20 69 43 65 6c 6c 2c 20 69 6e  e, int iCell, in
31d1d 74 20 69 48 65 69 67 68 74 29 7b 0a 20 20 69 6e  t iHeight){.  in
31d1e 74 20 72 63 3b 0a 0a 20 20 69 66 28 20 53 51 4c  t rc;..  if( SQL
31d1f 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 66 69  ITE_OK!=(rc = fi
31d20 78 4c 65 61 66 50 61 72 65 6e 74 28 70 52 74 72  xLeafParent(pRtr
31d21 65 65 2c 20 70 4e 6f 64 65 29 29 20 29 7b 0a 20  ee, pNode)) ){. 
31d22 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
31d23 7d 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74  }..  /* Remove t
31d24 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 74 68 65  he cell from the
31d25 20 6e 6f 64 65 2e 20 54 68 69 73 20 63 61 6c 6c   node. This call
31d26 20 6a 75 73 74 20 6d 6f 76 65 73 20 62 79 74 65   just moves byte
31d27 73 20 61 72 6f 75 6e 64 0a 20 20 2a 2a 20 74 68  s around.  ** th
31d28 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 6e 6f 64 65  e in-memory node
31d29 20 69 6d 61 67 65 2c 20 73 6f 20 69 74 20 63 61   image, so it ca
31d2a 6e 6e 6f 74 20 66 61 69 6c 2e 0a 20 20 2a 2f 0a  nnot fail..  */.
31d2b 20 20 6e 6f 64 65 44 65 6c 65 74 65 43 65 6c 6c    nodeDeleteCell
31d2c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20  (pRtree, pNode, 
31d2d 69 43 65 6c 6c 29 3b 0a 0a 20 20 2f 2a 20 49 66  iCell);..  /* If
31d2e 20 74 68 65 20 6e 6f 64 65 20 69 73 20 6e 6f 74   the node is not
31d2f 20 74 68 65 20 74 72 65 65 20 72 6f 6f 74 20 61   the tree root a
31d30 6e 64 20 6e 6f 77 20 68 61 73 20 6c 65 73 73 20  nd now has less 
31d31 74 68 61 6e 20 74 68 65 20 6d 69 6e 69 6d 75 6d  than the minimum
31d32 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  .  ** number of 
31d33 63 65 6c 6c 73 2c 20 72 65 6d 6f 76 65 20 69 74  cells, remove it
31d34 20 66 72 6f 6d 20 74 68 65 20 74 72 65 65 2e 20   from the tree. 
31d35 4f 74 68 65 72 77 69 73 65 2c 20 75 70 64 61 74  Otherwise, updat
31d36 65 20 74 68 65 0a 20 20 2a 2a 20 63 65 6c 6c 20  e the.  ** cell 
31d37 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 6e 6f  in the parent no
31d38 64 65 20 73 6f 20 74 68 61 74 20 69 74 20 74 69  de so that it ti
31d39 67 68 74 6c 79 20 63 6f 6e 74 61 69 6e 73 20 74  ghtly contains t
31d3a 68 65 20 75 70 64 61 74 65 64 0a 20 20 2a 2a 20  he updated.  ** 
31d3b 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  node..  */.  if(
31d3c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 21 3d 31   pNode->iNode!=1
31d3d 20 29 7b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64   ){.    RtreeNod
31d3e 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 70 4e 6f  e *pParent = pNo
31d3f 64 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 20  de->pParent;.   
31d40 20 69 66 28 20 28 70 50 61 72 65 6e 74 2d 3e 69   if( (pParent->i
31d41 4e 6f 64 65 21 3d 31 20 7c 7c 20 4e 43 45 4c 4c  Node!=1 || NCELL
31d42 28 70 50 61 72 65 6e 74 29 21 3d 31 29 20 0a 20  (pParent)!=1) . 
31d43 20 20 20 20 26 26 20 28 4e 43 45 4c 4c 28 70 4e      && (NCELL(pN
31d44 6f 64 65 29 3c 52 54 52 45 45 5f 4d 49 4e 43 45  ode)<RTREE_MINCE
31d45 4c 4c 53 28 70 52 74 72 65 65 29 29 0a 20 20 20  LLS(pRtree)).   
31d46 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 72   ){.      rc = r
31d47 65 6d 6f 76 65 4e 6f 64 65 28 70 52 74 72 65 65  emoveNode(pRtree
31d48 2c 20 70 4e 6f 64 65 2c 20 69 48 65 69 67 68 74  , pNode, iHeight
31d49 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
31d4a 20 20 20 20 66 69 78 42 6f 75 6e 64 69 6e 67 42      fixBoundingB
31d4b 6f 78 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ox(pRtree, pNode
31d4c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
31d4d 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74  return rc;.}..st
31d4e 61 74 69 63 20 69 6e 74 20 52 65 69 6e 73 65 72  atic int Reinser
31d4f 74 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  t(.  Rtree *pRtr
31d50 65 65 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64 65  ee, .  RtreeNode
31d51 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 52 74 72 65   *pNode, .  Rtre
31d52 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 2c 20 0a 20  eCell *pCell, . 
31d53 20 69 6e 74 20 69 48 65 69 67 68 74 0a 29 7b 0a   int iHeight.){.
31d54 20 20 69 6e 74 20 2a 61 4f 72 64 65 72 3b 0a 20    int *aOrder;. 
31d55 20 69 6e 74 20 2a 61 53 70 61 72 65 3b 0a 20 20   int *aSpare;.  
31d56 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c  RtreeCell *aCell
31d57 3b 0a 20 20 66 6c 6f 61 74 20 2a 61 44 69 73 74  ;.  float *aDist
31d58 61 6e 63 65 3b 0a 20 20 69 6e 74 20 6e 43 65 6c  ance;.  int nCel
31d59 6c 3b 0a 20 20 66 6c 6f 61 74 20 61 43 65 6e 74  l;.  float aCent
31d5a 65 72 43 6f 6f 72 64 5b 52 54 52 45 45 5f 4d 41  erCoord[RTREE_MA
31d5b 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 5d 3b 0a 20  X_DIMENSIONS];. 
31d5c 20 69 6e 74 20 69 44 69 6d 3b 0a 20 20 69 6e 74   int iDim;.  int
31d5d 20 69 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20   ii;.  int rc = 
31d5e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 6d 65  SQLITE_OK;..  me
31d5f 6d 73 65 74 28 61 43 65 6e 74 65 72 43 6f 6f 72  mset(aCenterCoor
31d60 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 6c 6f  d, 0, sizeof(flo
31d61 61 74 29 2a 52 54 52 45 45 5f 4d 41 58 5f 44 49  at)*RTREE_MAX_DI
31d62 4d 45 4e 53 49 4f 4e 53 29 3b 0a 0a 20 20 6e 43  MENSIONS);..  nC
31d63 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64  ell = NCELL(pNod
31d64 65 29 2b 31 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  e)+1;..  /* Allo
31d65 63 61 74 65 20 74 68 65 20 62 75 66 66 65 72 73  cate the buffers
31d66 20 75 73 65 64 20 62 79 20 74 68 69 73 20 6f 70   used by this op
31d67 65 72 61 74 69 6f 6e 2e 20 54 68 65 20 61 6c 6c  eration. The all
31d68 6f 63 61 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20  ocation is.  ** 
31d69 72 65 6c 69 6e 71 75 69 73 68 65 64 20 62 65 66  relinquished bef
31d6a 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ore this functio
31d6b 6e 20 72 65 74 75 72 6e 73 2e 0a 20 20 2a 2f 0a  n returns..  */.
31d6c 20 20 61 43 65 6c 6c 20 3d 20 28 52 74 72 65 65    aCell = (Rtree
31d6d 43 65 6c 6c 20 2a 29 73 71 6c 69 74 65 33 5f 6d  Cell *)sqlite3_m
31d6e 61 6c 6c 6f 63 28 6e 43 65 6c 6c 20 2a 20 28 0a  alloc(nCell * (.
31d6f 20 20 20 20 73 69 7a 65 6f 66 28 52 74 72 65 65      sizeof(Rtree
31d70 43 65 6c 6c 29 20 2b 20 20 20 20 20 20 20 20 20  Cell) +         
31d71 2f 2a 20 61 43 65 6c 6c 20 61 72 72 61 79 20 2a  /* aCell array *
31d72 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 69 6e 74  /.    sizeof(int
31d73 29 20 20 20 20 20 20 20 2b 20 20 20 20 20 20 20  )       +       
31d74 20 20 2f 2a 20 61 4f 72 64 65 72 20 61 72 72 61    /* aOrder arra
31d75 79 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28  y */.    sizeof(
31d76 69 6e 74 29 20 20 20 20 20 20 20 2b 20 20 20 20  int)       +    
31d77 20 20 20 20 20 2f 2a 20 61 53 70 61 72 65 20 61       /* aSpare a
31d78 72 72 61 79 20 2a 2f 0a 20 20 20 20 73 69 7a 65  rray */.    size
31d79 6f 66 28 66 6c 6f 61 74 29 20 20 20 20 20 20 20  of(float)       
31d7a 20 20 20 20 20 20 20 20 2f 2a 20 61 44 69 73 74          /* aDist
31d7b 61 6e 63 65 20 61 72 72 61 79 20 2a 2f 0a 20 20  ance array */.  
31d7c 29 29 3b 0a 20 20 69 66 28 20 21 61 43 65 6c 6c  ));.  if( !aCell
31d7d 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
31d7e 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
31d7f 0a 20 20 61 4f 72 64 65 72 20 20 20 20 3d 20 28  .  aOrder    = (
31d80 69 6e 74 20 2a 29 26 61 43 65 6c 6c 5b 6e 43 65  int *)&aCell[nCe
31d81 6c 6c 5d 3b 0a 20 20 61 53 70 61 72 65 20 20 20  ll];.  aSpare   
31d82 20 3d 20 28 69 6e 74 20 2a 29 26 61 4f 72 64 65   = (int *)&aOrde
31d83 72 5b 6e 43 65 6c 6c 5d 3b 0a 20 20 61 44 69 73  r[nCell];.  aDis
31d84 74 61 6e 63 65 20 3d 20 28 66 6c 6f 61 74 20 2a  tance = (float *
31d85 29 26 61 53 70 61 72 65 5b 6e 43 65 6c 6c 5d 3b  )&aSpare[nCell];
31d86 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ..  for(ii=0; ii
31d87 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20  <nCell; ii++){. 
31d88 20 20 20 69 66 28 20 69 69 3d 3d 28 6e 43 65 6c     if( ii==(nCel
31d89 6c 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 6d 65  l-1) ){.      me
31d8a 6d 63 70 79 28 26 61 43 65 6c 6c 5b 69 69 5d 2c  mcpy(&aCell[ii],
31d8b 20 70 43 65 6c 6c 2c 20 73 69 7a 65 6f 66 28 52   pCell, sizeof(R
31d8c 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 20 20  treeCell));.    
31d8d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 6f 64  }else{.      nod
31d8e 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  eGetCell(pRtree,
31d8f 20 70 4e 6f 64 65 2c 20 69 69 2c 20 26 61 43 65   pNode, ii, &aCe
31d90 6c 6c 5b 69 69 5d 29 3b 0a 20 20 20 20 7d 0a 20  ll[ii]);.    }. 
31d91 20 20 20 61 4f 72 64 65 72 5b 69 69 5d 20 3d 20     aOrder[ii] = 
31d92 69 69 3b 0a 20 20 20 20 66 6f 72 28 69 44 69 6d  ii;.    for(iDim
31d93 3d 30 3b 20 69 44 69 6d 3c 70 52 74 72 65 65 2d  =0; iDim<pRtree-
31d94 3e 6e 44 69 6d 3b 20 69 44 69 6d 2b 2b 29 7b 0a  >nDim; iDim++){.
31d95 20 20 20 20 20 20 61 43 65 6e 74 65 72 43 6f 6f        aCenterCoo
31d96 72 64 5b 69 44 69 6d 5d 20 2b 3d 20 44 43 4f 4f  rd[iDim] += DCOO
31d97 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f  RD(aCell[ii].aCo
31d98 6f 72 64 5b 69 44 69 6d 2a 32 5d 29 3b 0a 20 20  ord[iDim*2]);.  
31d99 20 20 20 20 61 43 65 6e 74 65 72 43 6f 6f 72 64      aCenterCoord
31d9a 5b 69 44 69 6d 5d 20 2b 3d 20 44 43 4f 4f 52 44  [iDim] += DCOORD
31d9b 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72  (aCell[ii].aCoor
31d9c 64 5b 69 44 69 6d 2a 32 2b 31 5d 29 3b 0a 20 20  d[iDim*2+1]);.  
31d9d 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 44    }.  }.  for(iD
31d9e 69 6d 3d 30 3b 20 69 44 69 6d 3c 70 52 74 72 65  im=0; iDim<pRtre
31d9f 65 2d 3e 6e 44 69 6d 3b 20 69 44 69 6d 2b 2b 29  e->nDim; iDim++)
31da0 7b 0a 20 20 20 20 61 43 65 6e 74 65 72 43 6f 6f  {.    aCenterCoo
31da1 72 64 5b 69 44 69 6d 5d 20 3d 20 61 43 65 6e 74  rd[iDim] = aCent
31da2 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 2f 28 28  erCoord[iDim]/((
31da3 66 6c 6f 61 74 29 6e 43 65 6c 6c 2a 32 2e 30 29  float)nCell*2.0)
31da4 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69 69 3d  ;.  }..  for(ii=
31da5 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b  0; ii<nCell; ii+
31da6 2b 29 7b 0a 20 20 20 20 61 44 69 73 74 61 6e 63  +){.    aDistanc
31da7 65 5b 69 69 5d 20 3d 20 30 2e 30 3b 0a 20 20 20  e[ii] = 0.0;.   
31da8 20 66 6f 72 28 69 44 69 6d 3d 30 3b 20 69 44 69   for(iDim=0; iDi
31da9 6d 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20  m<pRtree->nDim; 
31daa 69 44 69 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 66  iDim++){.      f
31dab 6c 6f 61 74 20 63 6f 6f 72 64 20 3d 20 44 43 4f  loat coord = DCO
31dac 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43  ORD(aCell[ii].aC
31dad 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 29 20  oord[iDim*2+1]) 
31dae 2d 20 0a 20 20 20 20 20 20 20 20 20 20 44 43 4f  - .          DCO
31daf 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43  ORD(aCell[ii].aC
31db0 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 29 3b 0a 20  oord[iDim*2]);. 
31db1 20 20 20 20 20 61 44 69 73 74 61 6e 63 65 5b 69       aDistance[i
31db2 69 5d 20 2b 3d 20 28 63 6f 6f 72 64 2d 61 43 65  i] += (coord-aCe
31db3 6e 74 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 29  nterCoord[iDim])
31db4 2a 28 63 6f 6f 72 64 2d 61 43 65 6e 74 65 72 43  *(coord-aCenterC
31db5 6f 6f 72 64 5b 69 44 69 6d 5d 29 3b 0a 20 20 20  oord[iDim]);.   
31db6 20 7d 0a 20 20 7d 0a 0a 20 20 53 6f 72 74 42 79   }.  }..  SortBy
31db7 44 69 73 74 61 6e 63 65 28 61 4f 72 64 65 72 2c  Distance(aOrder,
31db8 20 6e 43 65 6c 6c 2c 20 61 44 69 73 74 61 6e 63   nCell, aDistanc
31db9 65 2c 20 61 53 70 61 72 65 29 3b 0a 20 20 6e 6f  e, aSpare);.  no
31dba 64 65 5a 65 72 6f 28 70 52 74 72 65 65 2c 20 70  deZero(pRtree, p
31dbb 4e 6f 64 65 29 3b 0a 0a 20 20 66 6f 72 28 69 69  Node);..  for(ii
31dbc 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
31dbd 4b 20 26 26 20 69 69 3c 28 6e 43 65 6c 6c 2d 28  K && ii<(nCell-(
31dbe 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70  RTREE_MINCELLS(p
31dbf 52 74 72 65 65 29 2b 31 29 29 3b 20 69 69 2b 2b  Rtree)+1)); ii++
31dc0 29 7b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c  ){.    RtreeCell
31dc1 20 2a 70 20 3d 20 26 61 43 65 6c 6c 5b 61 4f 72   *p = &aCell[aOr
31dc2 64 65 72 5b 69 69 5d 5d 3b 0a 20 20 20 20 6e 6f  der[ii]];.    no
31dc3 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74  deInsertCell(pRt
31dc4 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 29 3b 0a  ree, pNode, p);.
31dc5 20 20 20 20 69 66 28 20 70 2d 3e 69 52 6f 77 69      if( p->iRowi
31dc6 64 3d 3d 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64  d==pCell->iRowid
31dc7 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 48   ){.      if( iH
31dc8 65 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20  eight==0 ){.    
31dc9 20 20 20 20 72 63 20 3d 20 72 6f 77 69 64 57 72      rc = rowidWr
31dca 69 74 65 28 70 52 74 72 65 65 2c 20 70 2d 3e 69  ite(pRtree, p->i
31dcb 52 6f 77 69 64 2c 20 70 4e 6f 64 65 2d 3e 69 4e  Rowid, pNode->iN
31dcc 6f 64 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ode);.      }els
31dcd 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  e{.        rc = 
31dce 70 61 72 65 6e 74 57 72 69 74 65 28 70 52 74 72  parentWrite(pRtr
31dcf 65 65 2c 20 70 2d 3e 69 52 6f 77 69 64 2c 20 70  ee, p->iRowid, p
31dd0 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20  Node->iNode);.  
31dd1 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
31dd2 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
31dd3 5f 4f 4b 20 29 7b 0a 20 20 20 20 66 69 78 42 6f  _OK ){.    fixBo
31dd4 75 6e 64 69 6e 67 42 6f 78 28 70 52 74 72 65 65  undingBox(pRtree
31dd5 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 7d 0a 20 20  , pNode);.  }.  
31dd6 66 6f 72 28 3b 20 72 63 3d 3d 53 51 4c 49 54 45  for(; rc==SQLITE
31dd7 5f 4f 4b 20 26 26 20 69 69 3c 6e 43 65 6c 6c 3b  _OK && ii<nCell;
31dd8 20 69 69 2b 2b 29 7b 0a 20 20 20 20 2f 2a 20 46   ii++){.    /* F
31dd9 69 6e 64 20 61 20 6e 6f 64 65 20 74 6f 20 73 74  ind a node to st
31dda 6f 72 65 20 74 68 69 73 20 63 65 6c 6c 20 69 6e  ore this cell in
31ddb 2e 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 63  . pNode->iNode c
31ddc 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e  urrently contain
31ddd 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 68 65 69  s.    ** the hei
31dde 67 68 74 20 6f 66 20 74 68 65 20 73 75 62 2d 74  ght of the sub-t
31ddf 72 65 65 20 68 65 61 64 65 64 20 62 79 20 74 68  ree headed by th
31de0 65 20 63 65 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20  e cell..    */. 
31de1 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 49     RtreeNode *pI
31de2 6e 73 65 72 74 3b 0a 20 20 20 20 52 74 72 65 65  nsert;.    Rtree
31de3 43 65 6c 6c 20 2a 70 20 3d 20 26 61 43 65 6c 6c  Cell *p = &aCell
31de4 5b 61 4f 72 64 65 72 5b 69 69 5d 5d 3b 0a 20 20  [aOrder[ii]];.  
31de5 20 20 72 63 20 3d 20 43 68 6f 6f 73 65 4c 65 61    rc = ChooseLea
31de6 66 28 70 52 74 72 65 65 2c 20 70 2c 20 69 48 65  f(pRtree, p, iHe
31de7 69 67 68 74 2c 20 26 70 49 6e 73 65 72 74 29 3b  ight, &pInsert);
31de8 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
31de9 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
31dea 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 20 20 72  int rc2;.      r
31deb 63 20 3d 20 72 74 72 65 65 49 6e 73 65 72 74 43  c = rtreeInsertC
31dec 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 49 6e 73  ell(pRtree, pIns
31ded 65 72 74 2c 20 70 2c 20 69 48 65 69 67 68 74 29  ert, p, iHeight)
31dee 3b 0a 20 20 20 20 20 20 72 63 32 20 3d 20 6e 6f  ;.      rc2 = no
31def 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  deRelease(pRtree
31df0 2c 20 70 49 6e 73 65 72 74 29 3b 0a 20 20 20 20  , pInsert);.    
31df1 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
31df2 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
31df3 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 20 20 7d  c = rc2;.      }
31df4 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71  .    }.  }..  sq
31df5 6c 69 74 65 33 5f 66 72 65 65 28 61 43 65 6c 6c  lite3_free(aCell
31df6 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
31df7 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20  }../*.** Insert 
31df8 63 65 6c 6c 20 70 43 65 6c 6c 20 69 6e 74 6f 20  cell pCell into 
31df9 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 4e 6f 64 65  node pNode. Node
31dfa 20 70 4e 6f 64 65 20 69 73 20 74 68 65 20 68 65   pNode is the he
31dfb 61 64 20 6f 66 20 61 20 0a 2a 2a 20 73 75 62 74  ad of a .** subt
31dfc 72 65 65 20 69 48 65 69 67 68 74 20 68 69 67 68  ree iHeight high
31dfd 20 28 6c 65 61 66 20 6e 6f 64 65 73 20 68 61 76   (leaf nodes hav
31dfe 65 20 69 48 65 69 67 68 74 3d 3d 30 29 2e 0a 2a  e iHeight==0)..*
31dff 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72  /.static int rtr
31e00 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 0a 20 20  eeInsertCell(.  
31e01 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20  Rtree *pRtree,. 
31e02 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
31e03 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  e,.  RtreeCell *
31e04 70 43 65 6c 6c 2c 0a 20 20 69 6e 74 20 69 48 65  pCell,.  int iHe
31e05 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74 20 72 63  ight.){.  int rc
31e06 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
31e07 69 66 28 20 69 48 65 69 67 68 74 3e 30 20 29 7b  if( iHeight>0 ){
31e08 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  .    RtreeNode *
31e09 70 43 68 69 6c 64 20 3d 20 6e 6f 64 65 48 61 73  pChild = nodeHas
31e0a 68 4c 6f 6f 6b 75 70 28 70 52 74 72 65 65 2c 20  hLookup(pRtree, 
31e0b 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 29 3b 0a  pCell->iRowid);.
31e0c 20 20 20 20 69 66 28 20 70 43 68 69 6c 64 20 29      if( pChild )
31e0d 7b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65 6c 65  {.      nodeRele
31e0e 61 73 65 28 70 52 74 72 65 65 2c 20 70 43 68 69  ase(pRtree, pChi
31e0f 6c 64 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20  ld->pParent);.  
31e10 20 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63      nodeReferenc
31e11 65 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20  e(pNode);.      
31e12 70 43 68 69 6c 64 2d 3e 70 50 61 72 65 6e 74 20  pChild->pParent 
31e13 3d 20 70 4e 6f 64 65 3b 0a 20 20 20 20 7d 0a 20  = pNode;.    }. 
31e14 20 7d 0a 20 20 69 66 28 20 6e 6f 64 65 49 6e 73   }.  if( nodeIns
31e15 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  ertCell(pRtree, 
31e16 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 29 20 29 7b  pNode, pCell) ){
31e17 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54  .#if VARIANT_RST
31e18 41 52 54 52 45 45 5f 52 45 49 4e 53 45 52 54 0a  ARTREE_REINSERT.
31e19 20 20 20 20 69 66 28 20 69 48 65 69 67 68 74 3c      if( iHeight<
31e1a 3d 70 52 74 72 65 65 2d 3e 69 52 65 69 6e 73 65  =pRtree->iReinse
31e1b 72 74 48 65 69 67 68 74 20 7c 7c 20 70 4e 6f 64  rtHeight || pNod
31e1c 65 2d 3e 69 4e 6f 64 65 3d 3d 31 29 7b 0a 20 20  e->iNode==1){.  
31e1d 20 20 20 20 72 63 20 3d 20 53 70 6c 69 74 4e 6f      rc = SplitNo
31e1e 64 65 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  de(pRtree, pNode
31e1f 2c 20 70 43 65 6c 6c 2c 20 69 48 65 69 67 68 74  , pCell, iHeight
31e20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
31e21 20 20 20 20 70 52 74 72 65 65 2d 3e 69 52 65 69      pRtree->iRei
31e22 6e 73 65 72 74 48 65 69 67 68 74 20 3d 20 69 48  nsertHeight = iH
31e23 65 69 67 68 74 3b 0a 20 20 20 20 20 20 72 63 20  eight;.      rc 
31e24 3d 20 52 65 69 6e 73 65 72 74 28 70 52 74 72 65  = Reinsert(pRtre
31e25 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 2c  e, pNode, pCell,
31e26 20 69 48 65 69 67 68 74 29 3b 0a 20 20 20 20 7d   iHeight);.    }
31e27 0a 23 65 6c 73 65 0a 20 20 20 20 72 63 20 3d 20  .#else.    rc = 
31e28 53 70 6c 69 74 4e 6f 64 65 28 70 52 74 72 65 65  SplitNode(pRtree
31e29 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 2c 20  , pNode, pCell, 
31e2a 69 48 65 69 67 68 74 29 3b 0a 23 65 6e 64 69 66  iHeight);.#endif
31e2b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 41 64  .  }else{.    Ad
31e2c 6a 75 73 74 54 72 65 65 28 70 52 74 72 65 65 2c  justTree(pRtree,
31e2d 20 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 29 3b 0a   pNode, pCell);.
31e2e 20 20 20 20 69 66 28 20 69 48 65 69 67 68 74 3d      if( iHeight=
31e2f 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  =0 ){.      rc =
31e30 20 72 6f 77 69 64 57 72 69 74 65 28 70 52 74 72   rowidWrite(pRtr
31e31 65 65 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69  ee, pCell->iRowi
31e32 64 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29  d, pNode->iNode)
31e33 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
31e34 20 20 20 72 63 20 3d 20 70 61 72 65 6e 74 57 72     rc = parentWr
31e35 69 74 65 28 70 52 74 72 65 65 2c 20 70 43 65 6c  ite(pRtree, pCel
31e36 6c 2d 3e 69 52 6f 77 69 64 2c 20 70 4e 6f 64 65  l->iRowid, pNode
31e37 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 0a  ->iNode);.    }.
31e38 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
31e39 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .}..static int r
31e3a 65 69 6e 73 65 72 74 4e 6f 64 65 43 6f 6e 74 65  einsertNodeConte
31e3b 6e 74 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  nt(Rtree *pRtree
31e3c 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f  , RtreeNode *pNo
31e3d 64 65 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  de){.  int ii;. 
31e3e 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
31e3f 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c  _OK;.  int nCell
31e40 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 3b   = NCELL(pNode);
31e41 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 72 63  ..  for(ii=0; rc
31e42 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
31e43 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a  i<nCell; ii++){.
31e44 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70      RtreeNode *p
31e45 49 6e 73 65 72 74 3b 0a 20 20 20 20 52 74 72 65  Insert;.    Rtre
31e46 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20 20  eCell cell;.    
31e47 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72  nodeGetCell(pRtr
31e48 65 65 2c 20 70 4e 6f 64 65 2c 20 69 69 2c 20 26  ee, pNode, ii, &
31e49 63 65 6c 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 46  cell);..    /* F
31e4a 69 6e 64 20 61 20 6e 6f 64 65 20 74 6f 20 73 74  ind a node to st
31e4b 6f 72 65 20 74 68 69 73 20 63 65 6c 6c 20 69 6e  ore this cell in
31e4c 2e 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 63  . pNode->iNode c
31e4d 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e  urrently contain
31e4e 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 68 65 69  s.    ** the hei
31e4f 67 68 74 20 6f 66 20 74 68 65 20 73 75 62 2d 74  ght of the sub-t
31e50 72 65 65 20 68 65 61 64 65 64 20 62 79 20 74 68  ree headed by th
31e51 65 20 63 65 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20  e cell..    */. 
31e52 20 20 20 72 63 20 3d 20 43 68 6f 6f 73 65 4c 65     rc = ChooseLe
31e53 61 66 28 70 52 74 72 65 65 2c 20 26 63 65 6c 6c  af(pRtree, &cell
31e54 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 2c 20  , pNode->iNode, 
31e55 26 70 49 6e 73 65 72 74 29 3b 0a 20 20 20 20 69  &pInsert);.    i
31e56 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
31e57 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63   ){.      int rc
31e58 32 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 74  2;.      rc = rt
31e59 72 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52  reeInsertCell(pR
31e5a 74 72 65 65 2c 20 70 49 6e 73 65 72 74 2c 20 26  tree, pInsert, &
31e5b 63 65 6c 6c 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f  cell, pNode->iNo
31e5c 64 65 29 3b 0a 20 20 20 20 20 20 72 63 32 20 3d  de);.      rc2 =
31e5d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74   nodeRelease(pRt
31e5e 72 65 65 2c 20 70 49 6e 73 65 72 74 29 3b 0a 20  ree, pInsert);. 
31e5f 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
31e60 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
31e61 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20    rc = rc2;.    
31e62 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
31e63 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
31e64 0a 2a 2a 20 53 65 6c 65 63 74 20 61 20 63 75 72  .** Select a cur
31e65 72 65 6e 74 6c 79 20 75 6e 75 73 65 64 20 72 6f  rently unused ro
31e66 77 69 64 20 66 6f 72 20 61 20 6e 65 77 20 72 2d  wid for a new r-
31e67 74 72 65 65 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a  tree record..*/.
31e68 73 74 61 74 69 63 20 69 6e 74 20 6e 65 77 52 6f  static int newRo
31e69 77 69 64 28 52 74 72 65 65 20 2a 70 52 74 72 65  wid(Rtree *pRtre
31e6a 65 2c 20 69 36 34 20 2a 70 69 52 6f 77 69 64 29  e, i64 *piRowid)
31e6b 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71  {.  int rc;.  sq
31e6c 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28  lite3_bind_null(
31e6d 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f  pRtree->pWriteRo
31e6e 77 69 64 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74  wid, 1);.  sqlit
31e6f 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70 52 74  e3_bind_null(pRt
31e70 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64  ree->pWriteRowid
31e71 2c 20 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  , 2);.  sqlite3_
31e72 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70 57 72  step(pRtree->pWr
31e73 69 74 65 52 6f 77 69 64 29 3b 0a 20 20 72 63 20  iteRowid);.  rc 
31e74 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  = sqlite3_reset(
31e75 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f  pRtree->pWriteRo
31e76 77 69 64 29 3b 0a 20 20 2a 70 69 52 6f 77 69 64  wid);.  *piRowid
31e77 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f   = sqlite3_last_
31e78 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 70 52 74  insert_rowid(pRt
31e79 72 65 65 2d 3e 64 62 29 3b 0a 20 20 72 65 74 75  ree->db);.  retu
31e7a 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65  rn rc;.}..#ifnde
31e7b 66 20 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20  f NDEBUG.static 
31e7c 69 6e 74 20 68 61 73 68 49 73 45 6d 70 74 79 28  int hashIsEmpty(
31e7d 52 74 72 65 65 20 2a 70 52 74 72 65 65 29 7b 0a  Rtree *pRtree){.
31e7e 20 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72 28    int ii;.  for(
31e7f 69 69 3d 30 3b 20 69 69 3c 48 41 53 48 53 49 5a  ii=0; ii<HASHSIZ
31e80 45 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 61 73  E; ii++){.    as
31e81 73 65 72 74 28 20 21 70 52 74 72 65 65 2d 3e 61  sert( !pRtree->a
31e82 48 61 73 68 5b 69 69 5d 20 29 3b 0a 20 20 7d 0a  Hash[ii] );.  }.
31e83 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65    return 1;.}.#e
31e84 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
31e85 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64 20 66  xUpdate method f
31e86 6f 72 20 72 74 72 65 65 20 6d 6f 64 75 6c 65 20  or rtree module 
31e87 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a  virtual tables..
31e88 2a 2f 0a 69 6e 74 20 72 74 72 65 65 55 70 64 61  */.int rtreeUpda
31e89 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 74  te(.  sqlite3_vt
31e8a 61 62 20 2a 70 56 74 61 62 2c 20 0a 20 20 69 6e  ab *pVtab, .  in
31e8b 74 20 6e 44 61 74 61 2c 20 0a 20 20 73 71 6c 69  t nData, .  sqli
31e8c 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 7a 44 61  te3_value **azDa
31e8d 74 61 2c 20 0a 20 20 73 71 6c 69 74 65 5f 69 6e  ta, .  sqlite_in
31e8e 74 36 34 20 2a 70 52 6f 77 69 64 0a 29 7b 0a 20  t64 *pRowid.){. 
31e8f 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 20 3d   Rtree *pRtree =
31e90 20 28 52 74 72 65 65 20 2a 29 70 56 74 61 62 3b   (Rtree *)pVtab;
31e91 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
31e92 54 45 5f 4f 4b 3b 0a 0a 20 20 72 74 72 65 65 52  TE_OK;..  rtreeR
31e93 65 66 65 72 65 6e 63 65 28 70 52 74 72 65 65 29  eference(pRtree)
31e94 3b 0a 0a 20 20 61 73 73 65 72 74 28 6e 44 61 74  ;..  assert(nDat
31e95 61 3e 3d 31 29 3b 0a 20 20 61 73 73 65 72 74 28  a>=1);.  assert(
31e96 68 61 73 68 49 73 45 6d 70 74 79 28 70 52 74 72  hashIsEmpty(pRtr
31e97 65 65 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61  ee));..  /* If a
31e98 7a 44 61 74 61 5b 30 5d 20 69 73 20 6e 6f 74 20  zData[0] is not 
31e99 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 76 61 6c 75  an SQL NULL valu
31e9a 65 2c 20 69 74 20 69 73 20 74 68 65 20 72 6f 77  e, it is the row
31e9b 69 64 20 6f 66 20 61 0a 20 20 2a 2a 20 72 65 63  id of a.  ** rec
31e9c 6f 72 64 20 74 6f 20 64 65 6c 65 74 65 20 66 72  ord to delete fr
31e9d 6f 6d 20 74 68 65 20 72 2d 74 72 65 65 20 74 61  om the r-tree ta
31e9e 62 6c 65 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ble. The followi
31e9f 6e 67 20 62 6c 6f 63 6b 20 64 6f 65 73 0a 20 20  ng block does.  
31ea0 2a 2a 20 6a 75 73 74 20 74 68 61 74 2e 0a 20 20  ** just that..  
31ea1 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
31ea2 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 7a 44 61  _value_type(azDa
31ea3 74 61 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 4e  ta[0])!=SQLITE_N
31ea4 55 4c 4c 20 29 7b 0a 20 20 20 20 69 36 34 20 69  ULL ){.    i64 i
31ea5 44 65 6c 65 74 65 3b 20 20 20 20 20 20 20 20 20  Delete;         
31ea6 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f         /* The ro
31ea7 77 69 64 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f  wid to delete */
31ea8 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  .    RtreeNode *
31ea9 70 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20  pLeaf;          
31eaa 20 2f 2a 20 4c 65 61 66 20 6e 6f 64 65 20 63 6f   /* Leaf node co
31eab 6e 74 61 69 6e 69 6e 67 20 72 65 63 6f 72 64 20  ntaining record 
31eac 69 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20 69  iDelete */.    i
31ead 6e 74 20 69 43 65 6c 6c 3b 20 20 20 20 20 20 20  nt iCell;       
31eae 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
31eaf 64 65 78 20 6f 66 20 69 44 65 6c 65 74 65 20 63  dex of iDelete c
31eb0 65 6c 6c 20 69 6e 20 70 4c 65 61 66 20 2a 2f 0a  ell in pLeaf */.
31eb1 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70      RtreeNode *p
31eb2 52 6f 6f 74 3b 0a 0a 20 20 20 20 2f 2a 20 4f 62  Root;..    /* Ob
31eb3 74 61 69 6e 20 61 20 72 65 66 65 72 65 6e 63 65  tain a reference
31eb4 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64   to the root nod
31eb5 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 73 65 20  e to initialise 
31eb6 52 74 72 65 65 2e 69 44 65 70 74 68 20 2a 2f 0a  Rtree.iDepth */.
31eb7 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71      rc = nodeAcq
31eb8 75 69 72 65 28 70 52 74 72 65 65 2c 20 31 2c 20  uire(pRtree, 1, 
31eb9 30 2c 20 26 70 52 6f 6f 74 29 3b 0a 0a 20 20 20  0, &pRoot);..   
31eba 20 2f 2a 20 4f 62 74 61 69 6e 20 61 20 72 65 66   /* Obtain a ref
31ebb 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 6c 65  erence to the le
31ebc 61 66 20 6e 6f 64 65 20 74 68 61 74 20 63 6f 6e  af node that con
31ebd 74 61 69 6e 73 20 74 68 65 20 65 6e 74 72 79 20  tains the entry 
31ebe 0a 20 20 20 20 2a 2a 20 61 62 6f 75 74 20 74 6f  .    ** about to
31ebf 20 62 65 20 64 65 6c 65 74 65 64 2e 20 0a 20 20   be deleted. .  
31ec0 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d    */.    if( rc=
31ec1 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
31ec2 20 20 20 20 69 44 65 6c 65 74 65 20 3d 20 73 71      iDelete = sq
31ec3 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36  lite3_value_int6
31ec4 34 28 61 7a 44 61 74 61 5b 30 5d 29 3b 0a 20 20  4(azData[0]);.  
31ec5 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c 65 61      rc = findLea
31ec6 66 4e 6f 64 65 28 70 52 74 72 65 65 2c 20 69 44  fNode(pRtree, iD
31ec7 65 6c 65 74 65 2c 20 26 70 4c 65 61 66 29 3b 0a  elete, &pLeaf);.
31ec8 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65      }..    /* De
31ec9 6c 65 74 65 20 74 68 65 20 63 65 6c 6c 20 69 6e  lete the cell in
31eca 20 71 75 65 73 74 69 6f 6e 20 66 72 6f 6d 20 74   question from t
31ecb 68 65 20 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f  he leaf node. */
31ecc 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
31ecd 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
31ece 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 20 20 69  int rc2;.      i
31ecf 43 65 6c 6c 20 3d 20 6e 6f 64 65 52 6f 77 69 64  Cell = nodeRowid
31ed0 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70 4c  Index(pRtree, pL
31ed1 65 61 66 2c 20 69 44 65 6c 65 74 65 29 3b 0a 20  eaf, iDelete);. 
31ed2 20 20 20 20 20 72 63 20 3d 20 64 65 6c 65 74 65       rc = delete
31ed3 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4c 65  Cell(pRtree, pLe
31ed4 61 66 2c 20 69 43 65 6c 6c 2c 20 30 29 3b 0a 20  af, iCell, 0);. 
31ed5 20 20 20 20 20 72 63 32 20 3d 20 6e 6f 64 65 52       rc2 = nodeR
31ed6 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70  elease(pRtree, p
31ed7 4c 65 61 66 29 3b 0a 20 20 20 20 20 20 69 66 28  Leaf);.      if(
31ed8 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
31ed9 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72  {.        rc = r
31eda 63 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  c2;.      }.    
31edb 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74 65  }..    /* Delete
31edc 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
31edd 6e 67 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  ng entry in the 
31ede 3c 72 74 72 65 65 3e 5f 72 6f 77 69 64 20 74 61  <rtree>_rowid ta
31edf 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ble. */.    if( 
31ee0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
31ee1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62  .      sqlite3_b
31ee2 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65  ind_int64(pRtree
31ee3 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69 64 2c 20  ->pDeleteRowid, 
31ee4 31 2c 20 69 44 65 6c 65 74 65 29 3b 0a 20 20 20  1, iDelete);.   
31ee5 20 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28     sqlite3_step(
31ee6 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 52  pRtree->pDeleteR
31ee7 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 72 63 20  owid);.      rc 
31ee8 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  = sqlite3_reset(
31ee9 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 52  pRtree->pDeleteR
31eea 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  owid);.    }..  
31eeb 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 74 68    /* Check if th
31eec 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6e 6f 77 20  e root node now 
31eed 68 61 73 20 65 78 61 63 74 6c 79 20 6f 6e 65 20  has exactly one 
31eee 63 68 69 6c 64 2e 20 49 66 20 73 6f 2c 20 72 65  child. If so, re
31eef 6d 6f 76 65 0a 20 20 20 20 2a 2a 20 69 74 2c 20  move.    ** it, 
31ef0 73 63 68 65 64 75 6c 65 20 74 68 65 20 63 6f 6e  schedule the con
31ef1 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 68 69  tents of the chi
31ef2 6c 64 20 66 6f 72 20 72 65 69 6e 73 65 72 74 69  ld for reinserti
31ef3 6f 6e 20 61 6e 64 20 0a 20 20 20 20 2a 2a 20 72  on and .    ** r
31ef4 65 64 75 63 65 20 74 68 65 20 74 72 65 65 20 68  educe the tree h
31ef5 65 69 67 68 74 20 62 79 20 6f 6e 65 2e 0a 20 20  eight by one..  
31ef6 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73    **.    ** This
31ef7 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74   is equivalent t
31ef8 6f 20 63 6f 70 79 69 6e 67 20 74 68 65 20 63 6f  o copying the co
31ef9 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 68  ntents of the ch
31efa 69 6c 64 20 69 6e 74 6f 0a 20 20 20 20 2a 2a 20  ild into.    ** 
31efb 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 28 74  the root node (t
31efc 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61  he operation tha
31efd 74 20 47 75 74 6d 61 6e 27 73 20 70 61 70 65 72  t Gutman's paper
31efe 20 73 61 79 73 20 74 6f 20 70 65 72 66 6f 72 6d   says to perform
31eff 20 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 69 73   .    ** in this
31f00 20 73 63 65 6e 61 72 69 6f 29 2e 0a 20 20 20 20   scenario)..    
31f01 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  */.    if( rc==S
31f02 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 52 74 72  QLITE_OK && pRtr
31f03 65 65 2d 3e 69 44 65 70 74 68 3e 30 20 29 7b 0a  ee->iDepth>0 ){.
31f04 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
31f05 4c 49 54 45 5f 4f 4b 20 26 26 20 4e 43 45 4c 4c  LITE_OK && NCELL
31f06 28 70 52 6f 6f 74 29 3d 3d 31 20 29 7b 0a 20 20  (pRoot)==1 ){.  
31f07 20 20 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20        RtreeNode 
31f08 2a 70 43 68 69 6c 64 3b 0a 20 20 20 20 20 20 20  *pChild;.       
31f09 20 69 36 34 20 69 43 68 69 6c 64 20 3d 20 6e 6f   i64 iChild = no
31f0a 64 65 47 65 74 52 6f 77 69 64 28 70 52 74 72 65  deGetRowid(pRtre
31f0b 65 2c 20 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20  e, pRoot, 0);.  
31f0c 20 20 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 41        rc = nodeA
31f0d 63 71 75 69 72 65 28 70 52 74 72 65 65 2c 20 69  cquire(pRtree, i
31f0e 43 68 69 6c 64 2c 20 70 52 6f 6f 74 2c 20 26 70  Child, pRoot, &p
31f0f 43 68 69 6c 64 29 3b 0a 20 20 20 20 20 20 20 20  Child);.        
31f10 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
31f11 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  K ){.          r
31f12 63 20 3d 20 72 65 6d 6f 76 65 4e 6f 64 65 28 70  c = removeNode(p
31f13 52 74 72 65 65 2c 20 70 43 68 69 6c 64 2c 20 70  Rtree, pChild, p
31f14 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d 31 29  Rtree->iDepth-1)
31f15 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
31f16 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
31f17 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
31f18 20 20 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74     pRtree->iDept
31f19 68 2d 2d 3b 0a 20 20 20 20 20 20 20 20 20 20 77  h--;.          w
31f1a 72 69 74 65 49 6e 74 31 36 28 70 52 6f 6f 74 2d  riteInt16(pRoot-
31f1b 3e 7a 44 61 74 61 2c 20 70 52 74 72 65 65 2d 3e  >zData, pRtree->
31f1c 69 44 65 70 74 68 29 3b 0a 20 20 20 20 20 20 20  iDepth);.       
31f1d 20 20 20 70 52 6f 6f 74 2d 3e 69 73 44 69 72 74     pRoot->isDirt
31f1e 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  y = 1;.        }
31f1f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
31f20 20 20 20 20 2f 2a 20 52 65 2d 69 6e 73 65 72 74      /* Re-insert
31f21 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
31f22 20 61 6e 79 20 75 6e 64 65 72 66 75 6c 6c 20 6e   any underfull n
31f23 6f 64 65 73 20 72 65 6d 6f 76 65 64 20 66 72 6f  odes removed fro
31f24 6d 20 74 68 65 20 74 72 65 65 2e 20 2a 2f 0a 20  m the tree. */. 
31f25 20 20 20 66 6f 72 28 70 4c 65 61 66 3d 70 52 74     for(pLeaf=pRt
31f26 72 65 65 2d 3e 70 44 65 6c 65 74 65 64 3b 20 70  ree->pDeleted; p
31f27 4c 65 61 66 3b 20 70 4c 65 61 66 3d 70 52 74 72  Leaf; pLeaf=pRtr
31f28 65 65 2d 3e 70 44 65 6c 65 74 65 64 29 7b 0a 20  ee->pDeleted){. 
31f29 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
31f2a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
31f2b 20 20 72 63 20 3d 20 72 65 69 6e 73 65 72 74 4e    rc = reinsertN
31f2c 6f 64 65 43 6f 6e 74 65 6e 74 28 70 52 74 72 65  odeContent(pRtre
31f2d 65 2c 20 70 4c 65 61 66 29 3b 0a 20 20 20 20 20  e, pLeaf);.     
31f2e 20 7d 0a 20 20 20 20 20 20 70 52 74 72 65 65 2d   }.      pRtree-
31f2f 3e 70 44 65 6c 65 74 65 64 20 3d 20 70 4c 65 61  >pDeleted = pLea
31f30 66 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  f->pNext;.      
31f31 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4c 65  sqlite3_free(pLe
31f32 61 66 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  af);.    }..    
31f33 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 72  /* Release the r
31f34 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20  eference to the 
31f35 72 6f 6f 74 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20  root node. */.  
31f36 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
31f37 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20  _OK ){.      rc 
31f38 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52  = nodeRelease(pR
31f39 74 72 65 65 2c 20 70 52 6f 6f 74 29 3b 0a 20 20  tree, pRoot);.  
31f3a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e    }else{.      n
31f3b 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
31f3c 65 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20 7d  e, pRoot);.    }
31f3d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68  .  }..  /* If th
31f3e 65 20 61 7a 44 61 74 61 5b 5d 20 61 72 72 61 79  e azData[] array
31f3f 20 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74   contains more t
31f40 68 61 6e 20 6f 6e 65 20 65 6c 65 6d 65 6e 74 2c  han one element,
31f41 20 65 6c 65 6d 65 6e 74 73 0a 20 20 2a 2a 20 28   elements.  ** (
31f42 61 7a 44 61 74 61 5b 32 5d 2e 2e 61 7a 44 61 74  azData[2]..azDat
31f43 61 5b 61 72 67 63 2d 31 5d 29 20 63 6f 6e 74 61  a[argc-1]) conta
31f44 69 6e 20 61 20 6e 65 77 20 72 65 63 6f 72 64 20  in a new record 
31f45 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 0a 20  to insert into. 
31f46 20 2a 2a 20 74 68 65 20 72 2d 74 72 65 65 20 73   ** the r-tree s
31f47 74 72 75 63 74 75 72 65 2e 0a 20 20 2a 2f 0a 20  tructure..  */. 
31f48 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
31f49 4f 4b 20 26 26 20 6e 44 61 74 61 3e 31 20 29 7b  OK && nData>1 ){
31f4a 0a 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 61  .    /* Insert a
31f4b 20 6e 65 77 20 72 65 63 6f 72 64 20 69 6e 74 6f   new record into
31f4c 20 74 68 65 20 72 2d 74 72 65 65 20 2a 2f 0a 20   the r-tree */. 
31f4d 20 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c     RtreeCell cel
31f4e 6c 3b 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a 20  l;.    int ii;. 
31f4f 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c     RtreeNode *pL
31f50 65 61 66 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f 70  eaf;..    /* Pop
31f51 75 6c 61 74 65 20 74 68 65 20 63 65 6c 6c 2e 61  ulate the cell.a
31f52 43 6f 6f 72 64 5b 5d 20 61 72 72 61 79 2e 20 54  Coord[] array. T
31f53 68 65 20 66 69 72 73 74 20 63 6f 6f 72 64 69 6e  he first coordin
31f54 61 74 65 20 69 73 20 61 7a 44 61 74 61 5b 33 5d  ate is azData[3]
31f55 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  . */.    assert(
31f56 20 6e 44 61 74 61 3d 3d 28 70 52 74 72 65 65 2d   nData==(pRtree-
31f57 3e 6e 44 69 6d 2a 32 20 2b 20 33 29 20 29 3b 0a  >nDim*2 + 3) );.
31f58 20 20 20 20 69 66 28 20 70 52 74 72 65 65 2d 3e      if( pRtree->
31f59 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52 45  eCoordType==RTRE
31f5a 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32 20 29  E_COORD_REAL32 )
31f5b 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30  {.      for(ii=0
31f5c 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44  ; ii<(pRtree->nD
31f5d 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20  im*2); ii+=2){. 
31f5e 20 20 20 20 20 20 20 63 65 6c 6c 2e 61 43 6f 6f         cell.aCoo
31f5f 72 64 5b 69 69 5d 2e 66 20 3d 20 28 66 6c 6f 61  rd[ii].f = (floa
31f60 74 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  t)sqlite3_value_
31f61 64 6f 75 62 6c 65 28 61 7a 44 61 74 61 5b 69 69  double(azData[ii
31f62 2b 33 5d 29 3b 0a 20 20 20 20 20 20 20 20 63 65  +3]);.        ce
31f63 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e  ll.aCoord[ii+1].
31f64 66 20 3d 20 28 66 6c 6f 61 74 29 73 71 6c 69 74  f = (float)sqlit
31f65 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28  e3_value_double(
31f66 61 7a 44 61 74 61 5b 69 69 2b 34 5d 29 3b 0a 20  azData[ii+4]);. 
31f67 20 20 20 20 20 20 20 69 66 28 20 63 65 6c 6c 2e         if( cell.
31f68 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 3e 63 65 6c  aCoord[ii].f>cel
31f69 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66  l.aCoord[ii+1].f
31f6a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
31f6b 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52   = SQLITE_CONSTR
31f6c 41 49 4e 54 3b 0a 20 20 20 20 20 20 20 20 20 20  AINT;.          
31f6d 67 6f 74 6f 20 63 6f 6e 73 74 72 61 69 6e 74 3b  goto constraint;
31f6e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
31f6f 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
31f70 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69      for(ii=0; ii
31f71 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  <(pRtree->nDim*2
31f72 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 20  ); ii+=2){.     
31f73 20 20 20 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69     cell.aCoord[i
31f74 69 5d 2e 69 20 3d 20 73 71 6c 69 74 65 33 5f 76  i].i = sqlite3_v
31f75 61 6c 75 65 5f 69 6e 74 28 61 7a 44 61 74 61 5b  alue_int(azData[
31f76 69 69 2b 33 5d 29 3b 0a 20 20 20 20 20 20 20 20  ii+3]);.        
31f77 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b 31  cell.aCoord[ii+1
31f78 5d 2e 69 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  ].i = sqlite3_va
31f79 6c 75 65 5f 69 6e 74 28 61 7a 44 61 74 61 5b 69  lue_int(azData[i
31f7a 69 2b 34 5d 29 3b 0a 20 20 20 20 20 20 20 20 69  i+4]);.        i
31f7b 66 28 20 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69  f( cell.aCoord[i
31f7c 69 5d 2e 69 3e 63 65 6c 6c 2e 61 43 6f 6f 72 64  i].i>cell.aCoord
31f7d 5b 69 69 2b 31 5d 2e 69 20 29 7b 0a 20 20 20 20  [ii+1].i ){.    
31f7e 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
31f7f 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20  E_CONSTRAINT;.  
31f80 20 20 20 20 20 20 20 20 67 6f 74 6f 20 63 6f 6e          goto con
31f81 73 74 72 61 69 6e 74 3b 0a 20 20 20 20 20 20 20  straint;.       
31f82 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
31f83 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20  ..    /* Figure 
31f84 6f 75 74 20 74 68 65 20 72 6f 77 69 64 20 6f 66  out the rowid of
31f85 20 74 68 65 20 6e 65 77 20 72 6f 77 2e 20 2a 2f   the new row. */
31f86 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
31f87 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 7a 44 61  _value_type(azDa
31f88 74 61 5b 32 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e  ta[2])==SQLITE_N
31f89 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ULL ){.      rc 
31f8a 3d 20 6e 65 77 52 6f 77 69 64 28 70 52 74 72 65  = newRowid(pRtre
31f8b 65 2c 20 26 63 65 6c 6c 2e 69 52 6f 77 69 64 29  e, &cell.iRowid)
31f8c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
31f8d 20 20 20 63 65 6c 6c 2e 69 52 6f 77 69 64 20 3d     cell.iRowid =
31f8e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
31f8f 6e 74 36 34 28 61 7a 44 61 74 61 5b 32 5d 29 3b  nt64(azData[2]);
31f90 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62  .      sqlite3_b
31f91 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65  ind_int64(pRtree
31f92 2d 3e 70 52 65 61 64 52 6f 77 69 64 2c 20 31 2c  ->pReadRowid, 1,
31f93 20 63 65 6c 6c 2e 69 52 6f 77 69 64 29 3b 0a 20   cell.iRowid);. 
31f94 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f       if( SQLITE_
31f95 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65  ROW==sqlite3_ste
31f96 70 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52  p(pRtree->pReadR
31f97 6f 77 69 64 29 20 29 7b 0a 20 20 20 20 20 20 20  owid) ){.       
31f98 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
31f99 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69  Rtree->pReadRowi
31f9a 64 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  d);.        rc =
31f9b 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49   SQLITE_CONSTRAI
31f9c 4e 54 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f  NT;.        goto
31f9d 20 63 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20 20   constraint;.   
31f9e 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20     }.      rc = 
31f9f 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52  sqlite3_reset(pR
31fa0 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64  tree->pReadRowid
31fa1 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  );.    }..    if
31fa2 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
31fa3 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 43 68  ){.      rc = Ch
31fa4 6f 6f 73 65 4c 65 61 66 28 70 52 74 72 65 65 2c  ooseLeaf(pRtree,
31fa5 20 26 63 65 6c 6c 2c 20 30 2c 20 26 70 4c 65 61   &cell, 0, &pLea
31fa6 66 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  f);.    }.    if
31fa7 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
31fa8 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32  ){.      int rc2
31fa9 3b 0a 20 20 20 20 20 20 70 52 74 72 65 65 2d 3e  ;.      pRtree->
31faa 69 52 65 69 6e 73 65 72 74 48 65 69 67 68 74 20  iReinsertHeight 
31fab 3d 20 2d 31 3b 0a 20 20 20 20 20 20 72 63 20 3d  = -1;.      rc =
31fac 20 72 74 72 65 65 49 6e 73 65 72 74 43 65 6c 6c   rtreeInsertCell
31fad 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66 2c 20  (pRtree, pLeaf, 
31fae 26 63 65 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 20  &cell, 0);.     
31faf 20 72 63 32 20 3d 20 6e 6f 64 65 52 65 6c 65 61   rc2 = nodeRelea
31fb0 73 65 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66  se(pRtree, pLeaf
31fb1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
31fb2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
31fb3 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a        rc = rc2;.
31fb4 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
31fb5 7d 0a 0a 63 6f 6e 73 74 72 61 69 6e 74 3a 0a 20  }..constraint:. 
31fb6 20 72 74 72 65 65 52 65 6c 65 61 73 65 28 70 52   rtreeRelease(pR
31fb7 74 72 65 65 29 3b 0a 20 20 72 65 74 75 72 6e 20  tree);.  return 
31fb8 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  rc;.}../*.** The
31fb9 20 78 52 65 6e 61 6d 65 20 6d 65 74 68 6f 64 20   xRename method 
31fba 66 6f 72 20 72 74 72 65 65 20 6d 6f 64 75 6c 65  for rtree module
31fbb 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2e   virtual tables.
31fbc 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
31fbd 74 72 65 65 52 65 6e 61 6d 65 28 73 71 6c 69 74  treeRename(sqlit
31fbe 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 2c 20  e3_vtab *pVtab, 
31fbf 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 65 77  const char *zNew
31fc0 4e 61 6d 65 29 7b 0a 20 20 52 74 72 65 65 20 2a  Name){.  Rtree *
31fc1 70 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20  pRtree = (Rtree 
31fc2 2a 29 70 56 74 61 62 3b 0a 20 20 69 6e 74 20 72  *)pVtab;.  int r
31fc3 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
31fc4 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d  ;.  char *zSql =
31fc5 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
31fc6 28 0a 20 20 20 20 22 41 4c 54 45 52 20 54 41 42  (.    "ALTER TAB
31fc7 4c 45 20 25 51 2e 27 25 71 5f 6e 6f 64 65 27 20  LE %Q.'%q_node' 
31fc8 20 20 52 45 4e 41 4d 45 20 54 4f 20 5c 22 25 77    RENAME TO \"%w
31fc9 5f 6e 6f 64 65 5c 22 3b 22 0a 20 20 20 20 22 41  _node\";".    "A
31fca 4c 54 45 52 20 54 41 42 4c 45 20 25 51 2e 27 25  LTER TABLE %Q.'%
31fcb 71 5f 70 61 72 65 6e 74 27 20 52 45 4e 41 4d 45  q_parent' RENAME
31fcc 20 54 4f 20 5c 22 25 77 5f 70 61 72 65 6e 74 5c   TO \"%w_parent\
31fcd 22 3b 22 0a 20 20 20 20 22 41 4c 54 45 52 20 54  ";".    "ALTER T
31fce 41 42 4c 45 20 25 51 2e 27 25 71 5f 72 6f 77 69  ABLE %Q.'%q_rowi
31fcf 64 27 20 20 52 45 4e 41 4d 45 20 54 4f 20 5c 22  d'  RENAME TO \"
31fd0 25 77 5f 72 6f 77 69 64 5c 22 3b 22 0a 20 20 20  %w_rowid\";".   
31fd1 20 2c 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20   , pRtree->zDb, 
31fd2 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 7a  pRtree->zName, z
31fd3 4e 65 77 4e 61 6d 65 20 0a 20 20 20 20 2c 20 70  NewName .    , p
31fd4 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72  Rtree->zDb, pRtr
31fd5 65 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 65 77 4e  ee->zName, zNewN
31fd6 61 6d 65 20 0a 20 20 20 20 2c 20 70 52 74 72 65  ame .    , pRtre
31fd7 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65 65 2d 3e  e->zDb, pRtree->
31fd8 7a 4e 61 6d 65 2c 20 7a 4e 65 77 4e 61 6d 65 0a  zName, zNewName.
31fd9 20 20 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 20    );.  if( zSql 
31fda 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
31fdb 74 65 33 5f 65 78 65 63 28 70 52 74 72 65 65 2d  te3_exec(pRtree-
31fdc 3e 64 62 2c 20 7a 53 71 6c 2c 20 30 2c 20 30 2c  >db, zSql, 0, 0,
31fdd 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
31fde 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20 7d  _free(zSql);.  }
31fdf 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
31fe0 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f  .static sqlite3_
31fe1 6d 6f 64 75 6c 65 20 72 74 72 65 65 4d 6f 64 75  module rtreeModu
31fe2 6c 65 20 3d 20 7b 0a 20 20 30 2c 20 20 20 20 20  le = {.  0,     
31fe3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31fe4 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20      /* iVersion 
31fe5 2a 2f 0a 20 20 72 74 72 65 65 43 72 65 61 74 65  */.  rtreeCreate
31fe6 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
31fe7 20 2f 2a 20 78 43 72 65 61 74 65 20 2d 20 63 72   /* xCreate - cr
31fe8 65 61 74 65 20 61 20 74 61 62 6c 65 20 2a 2f 0a  eate a table */.
31fe9 20 20 72 74 72 65 65 43 6f 6e 6e 65 63 74 2c 20    rtreeConnect, 
31fea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
31feb 20 78 43 6f 6e 6e 65 63 74 20 2d 20 63 6f 6e 6e   xConnect - conn
31fec 65 63 74 20 74 6f 20 61 6e 20 65 78 69 73 74 69  ect to an existi
31fed 6e 67 20 74 61 62 6c 65 20 2a 2f 0a 20 20 72 74  ng table */.  rt
31fee 72 65 65 42 65 73 74 49 6e 64 65 78 2c 20 20 20  reeBestIndex,   
31fef 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 42 65            /* xBe
31ff0 73 74 49 6e 64 65 78 20 2d 20 44 65 74 65 72 6d  stIndex - Determ
31ff1 69 6e 65 20 73 65 61 72 63 68 20 73 74 72 61 74  ine search strat
31ff2 65 67 79 20 2a 2f 0a 20 20 72 74 72 65 65 44 69  egy */.  rtreeDi
31ff3 73 63 6f 6e 6e 65 63 74 2c 20 20 20 20 20 20 20  sconnect,       
31ff4 20 20 20 20 20 2f 2a 20 78 44 69 73 63 6f 6e 6e       /* xDisconn
31ff5 65 63 74 20 2d 20 44 69 73 63 6f 6e 6e 65 63 74  ect - Disconnect
31ff6 20 66 72 6f 6d 20 61 20 74 61 62 6c 65 20 2a 2f   from a table */
31ff7 0a 20 20 72 74 72 65 65 44 65 73 74 72 6f 79 2c  .  rtreeDestroy,
31ff8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
31ff9 2a 20 78 44 65 73 74 72 6f 79 20 2d 20 44 72 6f  * xDestroy - Dro
31ffa 70 20 61 20 74 61 62 6c 65 20 2a 2f 0a 20 20 72  p a table */.  r
31ffb 74 72 65 65 4f 70 65 6e 2c 20 20 20 20 20 20 20  treeOpen,       
31ffc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f             /* xO
31ffd 70 65 6e 20 2d 20 6f 70 65 6e 20 61 20 63 75 72  pen - open a cur
31ffe 73 6f 72 20 2a 2f 0a 20 20 72 74 72 65 65 43 6c  sor */.  rtreeCl
31fff 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ose,            
32000 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2d       /* xClose -
32001 20 63 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 20   close a cursor 
32002 2a 2f 0a 20 20 72 74 72 65 65 46 69 6c 74 65 72  */.  rtreeFilter
32003 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
32004 20 2f 2a 20 78 46 69 6c 74 65 72 20 2d 20 63 6f   /* xFilter - co
32005 6e 66 69 67 75 72 65 20 73 63 61 6e 20 63 6f 6e  nfigure scan con
32006 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 72 74  straints */.  rt
32007 72 65 65 4e 65 78 74 2c 20 20 20 20 20 20 20 20  reeNext,        
32008 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4e 65            /* xNe
32009 78 74 20 2d 20 61 64 76 61 6e 63 65 20 61 20 63  xt - advance a c
3200a 75 72 73 6f 72 20 2a 2f 0a 20 20 72 74 72 65 65  ursor */.  rtree
3200b 45 6f 66 2c 20 20 20 20 20 20 20 20 20 20 20 20  Eof,            
3200c 20 20 20 20 20 20 20 2f 2a 20 78 45 6f 66 20 2a         /* xEof *
3200d 2f 0a 20 20 72 74 72 65 65 43 6f 6c 75 6d 6e 2c  /.  rtreeColumn,
3200e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3200f 2f 2a 20 78 43 6f 6c 75 6d 6e 20 2d 20 72 65 61  /* xColumn - rea
32010 64 20 64 61 74 61 20 2a 2f 0a 20 20 72 74 72 65  d data */.  rtre
32011 65 52 6f 77 69 64 2c 20 20 20 20 20 20 20 20 20  eRowid,         
32012 20 20 20 20 20 20 20 20 2f 2a 20 78 52 6f 77 69          /* xRowi
32013 64 20 2d 20 72 65 61 64 20 64 61 74 61 20 2a 2f  d - read data */
32014 0a 20 20 72 74 72 65 65 55 70 64 61 74 65 2c 20  .  rtreeUpdate, 
32015 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
32016 2a 20 78 55 70 64 61 74 65 20 2d 20 77 72 69 74  * xUpdate - writ
32017 65 20 64 61 74 61 20 2a 2f 0a 20 20 30 2c 20 20  e data */.  0,  
32018 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32019 20 20 20 20 20 20 20 20 2f 2a 20 78 42 65 67 69          /* xBegi
3201a 6e 20 2d 20 62 65 67 69 6e 20 74 72 61 6e 73 61  n - begin transa
3201b 63 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20 20 20  ction */.  0,   
3201c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3201d 20 20 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20         /* xSync 
3201e 2d 20 73 79 6e 63 20 74 72 61 6e 73 61 63 74 69  - sync transacti
3201f 6f 6e 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20  on */.  0,      
32020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32021 20 20 20 20 2f 2a 20 78 43 6f 6d 6d 69 74 20 2d      /* xCommit -
32022 20 63 6f 6d 6d 69 74 20 74 72 61 6e 73 61 63 74   commit transact
32023 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20  ion */.  0,     
32024 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32025 20 20 20 20 20 2f 2a 20 78 52 6f 6c 6c 62 61 63       /* xRollbac
32026 6b 20 2d 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61  k - rollback tra
32027 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c  nsaction */.  0,
32028 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32029 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69            /* xFi
3202a 6e 64 46 75 6e 63 74 69 6f 6e 20 2d 20 66 75 6e  ndFunction - fun
3202b 63 74 69 6f 6e 20 6f 76 65 72 6c 6f 61 64 69 6e  ction overloadin
3202c 67 20 2a 2f 0a 20 20 72 74 72 65 65 52 65 6e 61  g */.  rtreeRena
3202d 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  me              
3202e 20 20 20 2f 2a 20 78 52 65 6e 61 6d 65 20 2d 20     /* xRename - 
3202f 72 65 6e 61 6d 65 20 74 68 65 20 74 61 62 6c 65  rename the table
32030 20 2a 2f 0a 7d 3b 0a 0a 73 74 61 74 69 63 20 69   */.};..static i
32031 6e 74 20 72 74 72 65 65 53 71 6c 49 6e 69 74 28  nt rtreeSqlInit(
32032 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
32033 2c 20 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  , .  sqlite3 *db
32034 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
32035 2a 7a 44 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63  *zDb, .  const c
32036 68 61 72 20 2a 7a 50 72 65 66 69 78 2c 20 0a 20  har *zPrefix, . 
32037 20 69 6e 74 20 69 73 43 72 65 61 74 65 0a 29 7b   int isCreate.){
32038 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
32039 54 45 5f 4f 4b 3b 0a 0a 20 20 23 64 65 66 69 6e  TE_OK;..  #defin
3203a 65 20 4e 5f 53 54 41 54 45 4d 45 4e 54 20 39 0a  e N_STATEMENT 9.
3203b 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
3203c 68 61 72 20 2a 61 7a 53 71 6c 5b 4e 5f 53 54 41  har *azSql[N_STA
3203d 54 45 4d 45 4e 54 5d 20 3d 20 7b 0a 20 20 20 20  TEMENT] = {.    
3203e 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72 69 74  /* Read and writ
3203f 65 20 74 68 65 20 78 78 78 5f 6e 6f 64 65 20 74  e the xxx_node t
32040 61 62 6c 65 20 2a 2f 0a 20 20 20 20 22 53 45 4c  able */.    "SEL
32041 45 43 54 20 64 61 74 61 20 46 52 4f 4d 20 27 25  ECT data FROM '%
32042 71 27 2e 27 25 71 5f 6e 6f 64 65 27 20 57 48 45  q'.'%q_node' WHE
32043 52 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31 22 2c  RE nodeno = :1",
32044 0a 20 20 20 20 22 49 4e 53 45 52 54 20 4f 52 20  .    "INSERT OR 
32045 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 27 25 71  REPLACE INTO '%q
32046 27 2e 27 25 71 5f 6e 6f 64 65 27 20 56 41 4c 55  '.'%q_node' VALU
32047 45 53 28 3a 31 2c 20 3a 32 29 22 2c 0a 20 20 20  ES(:1, :2)",.   
32048 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 27 25   "DELETE FROM '%
32049 71 27 2e 27 25 71 5f 6e 6f 64 65 27 20 57 48 45  q'.'%q_node' WHE
3204a 52 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31 22 2c  RE nodeno = :1",
3204b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 61 6e  ..    /* Read an
3204c 64 20 77 72 69 74 65 20 74 68 65 20 78 78 78 5f  d write the xxx_
3204d 72 6f 77 69 64 20 74 61 62 6c 65 20 2a 2f 0a 20  rowid table */. 
3204e 20 20 20 22 53 45 4c 45 43 54 20 6e 6f 64 65 6e     "SELECT noden
3204f 6f 20 46 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f  o FROM '%q'.'%q_
32050 72 6f 77 69 64 27 20 57 48 45 52 45 20 72 6f 77  rowid' WHERE row
32051 69 64 20 3d 20 3a 31 22 2c 0a 20 20 20 20 22 49  id = :1",.    "I
32052 4e 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43 45  NSERT OR REPLACE
32053 20 49 4e 54 4f 20 27 25 71 27 2e 27 25 71 5f 72   INTO '%q'.'%q_r
32054 6f 77 69 64 27 20 56 41 4c 55 45 53 28 3a 31 2c  owid' VALUES(:1,
32055 20 3a 32 29 22 2c 0a 20 20 20 20 22 44 45 4c 45   :2)",.    "DELE
32056 54 45 20 46 52 4f 4d 20 27 25 71 27 2e 27 25 71  TE FROM '%q'.'%q
32057 5f 72 6f 77 69 64 27 20 57 48 45 52 45 20 72 6f  _rowid' WHERE ro
32058 77 69 64 20 3d 20 3a 31 22 2c 0a 0a 20 20 20 20  wid = :1",..    
32059 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72 69 74  /* Read and writ
3205a 65 20 74 68 65 20 78 78 78 5f 70 61 72 65 6e 74  e the xxx_parent
3205b 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 22 53   table */.    "S
3205c 45 4c 45 43 54 20 70 61 72 65 6e 74 6e 6f 64 65  ELECT parentnode
3205d 20 46 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f 70   FROM '%q'.'%q_p
3205e 61 72 65 6e 74 27 20 57 48 45 52 45 20 6e 6f 64  arent' WHERE nod
3205f 65 6e 6f 20 3d 20 3a 31 22 2c 0a 20 20 20 20 22  eno = :1",.    "
32060 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43  INSERT OR REPLAC
32061 45 20 49 4e 54 4f 20 27 25 71 27 2e 27 25 71 5f  E INTO '%q'.'%q_
32062 70 61 72 65 6e 74 27 20 56 41 4c 55 45 53 28 3a  parent' VALUES(:
32063 31 2c 20 3a 32 29 22 2c 0a 20 20 20 20 22 44 45  1, :2)",.    "DE
32064 4c 45 54 45 20 46 52 4f 4d 20 27 25 71 27 2e 27  LETE FROM '%q'.'
32065 25 71 5f 70 61 72 65 6e 74 27 20 57 48 45 52 45  %q_parent' WHERE
32066 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31 22 0a 20 20   nodeno = :1".  
32067 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  };.  sqlite3_stm
32068 74 20 2a 2a 61 70 70 53 74 6d 74 5b 4e 5f 53 54  t **appStmt[N_ST
32069 41 54 45 4d 45 4e 54 5d 3b 0a 20 20 69 6e 74 20  ATEMENT];.  int 
3206a 69 3b 0a 0a 20 20 70 52 74 72 65 65 2d 3e 64 62  i;..  pRtree->db
3206b 20 3d 20 64 62 3b 0a 0a 20 20 69 66 28 20 69 73   = db;..  if( is
3206c 43 72 65 61 74 65 20 29 7b 0a 20 20 20 20 63 68  Create ){.    ch
3206d 61 72 20 2a 7a 43 72 65 61 74 65 20 3d 20 73 71  ar *zCreate = sq
3206e 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0a 22  lite3_mprintf(."
3206f 43 52 45 41 54 45 20 54 41 42 4c 45 20 5c 22 25  CREATE TABLE \"%
32070 77 5c 22 2e 5c 22 25 77 5f 6e 6f 64 65 5c 22 28  w\".\"%w_node\"(
32071 6e 6f 64 65 6e 6f 20 49 4e 54 45 47 45 52 20 50  nodeno INTEGER P
32072 52 49 4d 41 52 59 20 4b 45 59 2c 20 64 61 74 61  RIMARY KEY, data
32073 20 42 4c 4f 42 29 3b 22 0a 22 43 52 45 41 54 45   BLOB);"."CREATE
32074 20 54 41 42 4c 45 20 5c 22 25 77 5c 22 2e 5c 22   TABLE \"%w\".\"
32075 25 77 5f 72 6f 77 69 64 5c 22 28 72 6f 77 69 64  %w_rowid\"(rowid
32076 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
32077 20 4b 45 59 2c 20 6e 6f 64 65 6e 6f 20 49 4e 54   KEY, nodeno INT
32078 45 47 45 52 29 3b 22 0a 22 43 52 45 41 54 45 20  EGER);"."CREATE 
32079 54 41 42 4c 45 20 5c 22 25 77 5c 22 2e 5c 22 25  TABLE \"%w\".\"%
3207a 77 5f 70 61 72 65 6e 74 5c 22 28 6e 6f 64 65 6e  w_parent\"(noden
3207b 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  o INTEGER PRIMAR
3207c 59 20 4b 45 59 2c 20 70 61 72 65 6e 74 6e 6f 64  Y KEY, parentnod
3207d 65 20 49 4e 54 45 47 45 52 29 3b 22 0a 22 49 4e  e INTEGER);"."IN
3207e 53 45 52 54 20 49 4e 54 4f 20 27 25 71 27 2e 27  SERT INTO '%q'.'
3207f 25 71 5f 6e 6f 64 65 27 20 56 41 4c 55 45 53 28  %q_node' VALUES(
32080 31 2c 20 7a 65 72 6f 62 6c 6f 62 28 25 64 29 29  1, zeroblob(%d))
32081 22 2c 0a 20 20 20 20 20 20 7a 44 62 2c 20 7a 50  ",.      zDb, zP
32082 72 65 66 69 78 2c 20 7a 44 62 2c 20 7a 50 72 65  refix, zDb, zPre
32083 66 69 78 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69  fix, zDb, zPrefi
32084 78 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69 78 2c  x, zDb, zPrefix,
32085 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69   pRtree->iNodeSi
32086 7a 65 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66  ze.    );.    if
32087 28 20 21 7a 43 72 65 61 74 65 20 29 7b 0a 20 20  ( !zCreate ){.  
32088 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
32089 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20  E_NOMEM;.    }. 
3208a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
3208b 65 78 65 63 28 64 62 2c 20 7a 43 72 65 61 74 65  exec(db, zCreate
3208c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  , 0, 0, 0);.    
3208d 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 72  sqlite3_free(zCr
3208e 65 61 74 65 29 3b 0a 20 20 20 20 69 66 28 20 72  eate);.    if( r
3208f 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
32090 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
32091 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 70  .    }.  }..  ap
32092 70 53 74 6d 74 5b 30 5d 20 3d 20 26 70 52 74 72  pStmt[0] = &pRtr
32093 65 65 2d 3e 70 52 65 61 64 4e 6f 64 65 3b 0a 20  ee->pReadNode;. 
32094 20 61 70 70 53 74 6d 74 5b 31 5d 20 3d 20 26 70   appStmt[1] = &p
32095 52 74 72 65 65 2d 3e 70 57 72 69 74 65 4e 6f 64  Rtree->pWriteNod
32096 65 3b 0a 20 20 61 70 70 53 74 6d 74 5b 32 5d 20  e;.  appStmt[2] 
32097 3d 20 26 70 52 74 72 65 65 2d 3e 70 44 65 6c 65  = &pRtree->pDele
32098 74 65 4e 6f 64 65 3b 0a 20 20 61 70 70 53 74 6d  teNode;.  appStm
32099 74 5b 33 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e  t[3] = &pRtree->
3209a 70 52 65 61 64 52 6f 77 69 64 3b 0a 20 20 61 70  pReadRowid;.  ap
3209b 70 53 74 6d 74 5b 34 5d 20 3d 20 26 70 52 74 72  pStmt[4] = &pRtr
3209c 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 3b  ee->pWriteRowid;
3209d 0a 20 20 61 70 70 53 74 6d 74 5b 35 5d 20 3d 20  .  appStmt[5] = 
3209e 26 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65  &pRtree->pDelete
3209f 52 6f 77 69 64 3b 0a 20 20 61 70 70 53 74 6d 74  Rowid;.  appStmt
320a0 5b 36 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e 70  [6] = &pRtree->p
320a1 52 65 61 64 50 61 72 65 6e 74 3b 0a 20 20 61 70  ReadParent;.  ap
320a2 70 53 74 6d 74 5b 37 5d 20 3d 20 26 70 52 74 72  pStmt[7] = &pRtr
320a3 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65 6e 74  ee->pWriteParent
320a4 3b 0a 20 20 61 70 70 53 74 6d 74 5b 38 5d 20 3d  ;.  appStmt[8] =
320a5 20 26 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74   &pRtree->pDelet
320a6 65 50 61 72 65 6e 74 3b 0a 0a 20 20 66 6f 72 28  eParent;..  for(
320a7 69 3d 30 3b 20 69 3c 4e 5f 53 54 41 54 45 4d 45  i=0; i<N_STATEME
320a8 4e 54 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45  NT && rc==SQLITE
320a9 5f 4f 4b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63  _OK; i++){.    c
320aa 68 61 72 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69  har *zSql = sqli
320ab 74 65 33 5f 6d 70 72 69 6e 74 66 28 61 7a 53 71  te3_mprintf(azSq
320ac 6c 5b 69 5d 2c 20 7a 44 62 2c 20 7a 50 72 65 66  l[i], zDb, zPref
320ad 69 78 29 3b 0a 20 20 20 20 69 66 28 20 7a 53 71  ix);.    if( zSq
320ae 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  l ){.      rc = 
320af 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
320b0 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c  v2(db, zSql, -1,
320b1 20 61 70 70 53 74 6d 74 5b 69 5d 2c 20 30 29 3b   appStmt[i], 0);
320b2 20 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20   .    }else{.   
320b3 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
320b4 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  OMEM;.    }.    
320b5 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 71  sqlite3_free(zSq
320b6 6c 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  l);.  }..  retur
320b7 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
320b8 68 69 73 20 72 6f 75 74 69 6e 65 20 71 75 65 72  his routine quer
320b9 69 65 73 20 64 61 74 61 62 61 73 65 20 68 61 6e  ies database han
320ba 64 6c 65 20 64 62 20 66 6f 72 20 74 68 65 20 70  dle db for the p
320bb 61 67 65 2d 73 69 7a 65 20 75 73 65 64 20 62 79  age-size used by
320bc 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 7a 44 62  .** database zDb
320bd 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c  . If successful,
320be 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 69   the page-size i
320bf 6e 20 62 79 74 65 73 20 69 73 20 77 72 69 74 74  n bytes is writt
320c0 65 6e 20 74 6f 0a 2a 2a 20 2a 70 69 50 61 67 65  en to.** *piPage
320c1 53 69 7a 65 20 61 6e 64 20 53 51 4c 49 54 45 5f  Size and SQLITE_
320c2 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68  OK returned. Oth
320c3 65 72 77 69 73 65 2c 20 61 6e 64 20 61 6e 20 53  erwise, and an S
320c4 51 4c 69 74 65 20 65 72 72 6f 72 20 0a 2a 2a 20  QLite error .** 
320c5 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
320c6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
320c7 67 65 74 50 61 67 65 53 69 7a 65 28 73 71 6c 69  getPageSize(sqli
320c8 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63  te3 *db, const c
320c9 68 61 72 20 2a 7a 44 62 2c 20 69 6e 74 20 2a 70  har *zDb, int *p
320ca 69 50 61 67 65 53 69 7a 65 29 7b 0a 20 20 69 6e  iPageSize){.  in
320cb 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  t rc = SQLITE_NO
320cc 4d 45 4d 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71  MEM;.  char *zSq
320cd 6c 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  l;.  sqlite3_stm
320ce 74 20 2a 70 53 74 6d 74 20 3d 20 30 3b 0a 0a 20  t *pStmt = 0;.. 
320cf 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f   zSql = sqlite3_
320d0 6d 70 72 69 6e 74 66 28 22 50 52 41 47 4d 41 20  mprintf("PRAGMA 
320d1 25 51 2e 70 61 67 65 5f 73 69 7a 65 22 2c 20 7a  %Q.page_size", z
320d2 44 62 29 3b 0a 20 20 69 66 28 20 21 7a 53 71 6c  Db);.  if( !zSql
320d3 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
320d4 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
320d5 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
320d6 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c 20  _prepare_v2(db, 
320d7 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74  zSql, -1, &pStmt
320d8 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  , 0);.  sqlite3_
320d9 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20 69 66  free(zSql);.  if
320da 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
320db 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
320dc 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 53 51 4c  ;.  }..  if( SQL
320dd 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33  ITE_ROW==sqlite3
320de 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29 7b 0a  _step(pStmt) ){.
320df 20 20 20 20 2a 70 69 50 61 67 65 53 69 7a 65 20      *piPageSize 
320e0 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
320e1 5f 69 6e 74 28 70 53 74 6d 74 2c 20 30 29 3b 0a  _int(pStmt, 0);.
320e2 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c    }.  return sql
320e3 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53  ite3_finalize(pS
320e4 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20  tmt);.}../* .** 
320e5 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
320e6 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
320e7 69 6f 6e 20 6f 66 20 62 6f 74 68 20 74 68 65 20  ion of both the 
320e8 78 43 6f 6e 6e 65 63 74 20 61 6e 64 20 78 43 72  xConnect and xCr
320e9 65 61 74 65 0a 2a 2a 20 6d 65 74 68 6f 64 73 20  eate.** methods 
320ea 6f 66 20 74 68 65 20 72 2d 74 72 65 65 20 76 69  of the r-tree vi
320eb 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a  rtual table..**.
320ec 2a 2a 20 20 20 61 72 67 76 5b 30 5d 20 20 20 2d  **   argv[0]   -
320ed 3e 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 0a 2a 2a  > module name.**
320ee 20 20 20 61 72 67 76 5b 31 5d 20 20 20 2d 3e 20     argv[1]   -> 
320ef 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a  database name.**
320f0 20 20 20 61 72 67 76 5b 32 5d 20 20 20 2d 3e 20     argv[2]   -> 
320f1 74 61 62 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20 20  table name.**   
320f2 61 72 67 76 5b 2e 2e 2e 5d 20 2d 3e 20 63 6f 6c  argv[...] -> col
320f3 75 6d 6e 20 6e 61 6d 65 73 2e 2e 2e 0a 2a 2f 0a  umn names....*/.
320f4 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65  static int rtree
320f5 49 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33 20  Init(.  sqlite3 
320f6 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
320f7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
320f8 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
320f9 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41  on */.  void *pA
320fa 75 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ux,             
320fb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
320fc 6e 65 20 6f 66 20 74 68 65 20 52 54 52 45 45 5f  ne of the RTREE_
320fd 43 4f 4f 52 44 5f 2a 20 63 6f 6e 73 74 61 6e 74  COORD_* constant
320fe 73 20 2a 2f 0a 20 20 69 6e 74 20 61 72 67 63 2c  s */.  int argc,
320ff 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e   const char *con
32100 73 74 2a 61 72 67 76 2c 20 20 20 2f 2a 20 50 61  st*argv,   /* Pa
32101 72 61 6d 65 74 65 72 73 20 74 6f 20 43 52 45 41  rameters to CREA
32102 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  TE TABLE stateme
32103 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  nt */.  sqlite3_
32104 76 74 61 62 20 2a 2a 70 70 56 74 61 62 2c 20 20  vtab **ppVtab,  
32105 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
32106 55 54 3a 20 4e 65 77 20 76 69 72 74 75 61 6c 20  UT: New virtual 
32107 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20  table */.  char 
32108 2a 2a 70 7a 45 72 72 2c 20 20 20 20 20 20 20 20  **pzErr,        
32109 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3210a 2a 20 4f 55 54 3a 20 45 72 72 6f 72 20 6d 65 73  * OUT: Error mes
3210b 73 61 67 65 2c 20 69 66 20 61 6e 79 20 2a 2f 0a  sage, if any */.
3210c 20 20 69 6e 74 20 69 73 43 72 65 61 74 65 20 20    int isCreate  
3210d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3210e 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f        /* True fo
3210f 72 20 78 43 72 65 61 74 65 2c 20 66 61 6c 73 65  r xCreate, false
32110 20 66 6f 72 20 78 43 6f 6e 6e 65 63 74 20 2a 2f   for xConnect */
32111 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
32112 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20  QLITE_OK;.  int 
32113 69 50 61 67 65 53 69 7a 65 20 3d 20 30 3b 0a 20  iPageSize = 0;. 
32114 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 3b 0a   Rtree *pRtree;.
32115 20 20 69 6e 74 20 6e 44 62 3b 20 20 20 20 20 20    int nDb;      
32116 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
32117 68 20 6f 66 20 73 74 72 69 6e 67 20 61 72 67 76  h of string argv
32118 5b 31 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61  [1] */.  int nNa
32119 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  me;            /
3211a 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 74 72 69  * Length of stri
3211b 6e 67 20 61 72 67 76 5b 32 5d 20 2a 2f 0a 20 20  ng argv[2] */.  
3211c 69 6e 74 20 65 43 6f 6f 72 64 54 79 70 65 20 3d  int eCoordType =
3211d 20 28 69 6e 74 29 70 41 75 78 3b 0a 0a 20 20 63   (int)pAux;..  c
3211e 6f 6e 73 74 20 63 68 61 72 20 2a 61 45 72 72 4d  onst char *aErrM
3211f 73 67 5b 5d 20 3d 20 7b 0a 20 20 20 20 30 2c 20  sg[] = {.    0, 
32120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32121 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32122 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32123 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 22     /* 0 */.    "
32124 57 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f 66 20  Wrong number of 
32125 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 61 6e 20 72  columns for an r
32126 74 72 65 65 20 74 61 62 6c 65 22 2c 20 20 20 20  tree table",    
32127 20 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20       /* 1 */.   
32128 20 22 54 6f 6f 20 66 65 77 20 63 6f 6c 75 6d 6e   "Too few column
32129 73 20 66 6f 72 20 61 6e 20 72 74 72 65 65 20 74  s for an rtree t
3212a 61 62 6c 65 22 2c 20 20 20 20 20 20 20 20 20 20  able",          
3212b 20 20 20 20 20 20 20 2f 2a 20 32 20 2a 2f 0a 20         /* 2 */. 
3212c 20 20 20 22 54 6f 6f 20 6d 61 6e 79 20 63 6f 6c     "Too many col
3212d 75 6d 6e 73 20 66 6f 72 20 61 6e 20 72 74 72 65  umns for an rtre
3212e 65 20 74 61 62 6c 65 22 20 20 20 20 20 20 20 20  e table"        
3212f 20 20 20 20 20 20 20 20 20 2f 2a 20 33 20 2a 2f           /* 3 */
32130 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 69 45 72  .  };..  int iEr
32131 72 20 3d 20 28 61 72 67 63 3c 36 29 20 3f 20 32  r = (argc<6) ? 2
32132 20 3a 20 61 72 67 63 3e 28 52 54 52 45 45 5f 4d   : argc>(RTREE_M
32133 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 2a 32 2b  AX_DIMENSIONS*2+
32134 34 29 20 3f 20 33 20 3a 20 61 72 67 63 25 32 3b  4) ? 3 : argc%2;
32135 0a 20 20 69 66 28 20 61 45 72 72 4d 73 67 5b 69  .  if( aErrMsg[i
32136 45 72 72 5d 20 29 7b 0a 20 20 20 20 2a 70 7a 45  Err] ){.    *pzE
32137 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  rr = sqlite3_mpr
32138 69 6e 74 66 28 22 25 73 22 2c 20 61 45 72 72 4d  intf("%s", aErrM
32139 73 67 5b 69 45 72 72 5d 29 3b 0a 20 20 20 20 72  sg[iErr]);.    r
3213a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
3213b 4f 52 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20  OR;.  }..  rc = 
3213c 67 65 74 50 61 67 65 53 69 7a 65 28 64 62 2c 20  getPageSize(db, 
3213d 61 72 67 76 5b 31 5d 2c 20 26 69 50 61 67 65 53  argv[1], &iPageS
3213e 69 7a 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  ize);.  if( rc!=
3213f 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
32140 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
32141 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74  .  /* Allocate t
32142 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  he sqlite3_vtab 
32143 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 6e  structure */.  n
32144 44 62 20 3d 20 73 74 72 6c 65 6e 28 61 72 67 76  Db = strlen(argv
32145 5b 31 5d 29 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20  [1]);.  nName = 
32146 73 74 72 6c 65 6e 28 61 72 67 76 5b 32 5d 29 3b  strlen(argv[2]);
32147 0a 20 20 70 52 74 72 65 65 20 3d 20 28 52 74 72  .  pRtree = (Rtr
32148 65 65 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  ee *)sqlite3_mal
32149 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65  loc(sizeof(Rtree
3214a 29 2b 6e 44 62 2b 6e 4e 61 6d 65 2b 32 29 3b 0a  )+nDb+nName+2);.
3214b 20 20 69 66 28 20 21 70 52 74 72 65 65 20 29 7b    if( !pRtree ){
3214c 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
3214d 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
3214e 6d 65 6d 73 65 74 28 70 52 74 72 65 65 2c 20 30  memset(pRtree, 0
3214f 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65 29 2b  , sizeof(Rtree)+
32150 6e 44 62 2b 6e 4e 61 6d 65 2b 32 29 3b 0a 20 20  nDb+nName+2);.  
32151 70 52 74 72 65 65 2d 3e 6e 42 75 73 79 20 3d 20  pRtree->nBusy = 
32152 31 3b 0a 20 20 70 52 74 72 65 65 2d 3e 62 61 73  1;.  pRtree->bas
32153 65 2e 70 4d 6f 64 75 6c 65 20 3d 20 26 72 74 72  e.pModule = &rtr
32154 65 65 4d 6f 64 75 6c 65 3b 0a 20 20 70 52 74 72  eeModule;.  pRtr
32155 65 65 2d 3e 7a 44 62 20 3d 20 28 63 68 61 72 20  ee->zDb = (char 
32156 2a 29 26 70 52 74 72 65 65 5b 31 5d 3b 0a 20 20  *)&pRtree[1];.  
32157 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 20 3d 20  pRtree->zName = 
32158 26 70 52 74 72 65 65 2d 3e 7a 44 62 5b 6e 44 62  &pRtree->zDb[nDb
32159 2b 31 5d 3b 0a 20 20 70 52 74 72 65 65 2d 3e 6e  +1];.  pRtree->n
3215a 44 69 6d 20 3d 20 28 61 72 67 63 2d 34 29 2f 32  Dim = (argc-4)/2
3215b 3b 0a 20 20 70 52 74 72 65 65 2d 3e 6e 42 79 74  ;.  pRtree->nByt
3215c 65 73 50 65 72 43 65 6c 6c 20 3d 20 38 20 2b 20  esPerCell = 8 + 
3215d 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 34 2a 32  pRtree->nDim*4*2
3215e 3b 0a 20 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f  ;.  pRtree->eCoo
3215f 72 64 54 79 70 65 20 3d 20 65 43 6f 6f 72 64 54  rdType = eCoordT
32160 79 70 65 3b 0a 20 20 6d 65 6d 63 70 79 28 70 52  ype;.  memcpy(pR
32161 74 72 65 65 2d 3e 7a 44 62 2c 20 61 72 67 76 5b  tree->zDb, argv[
32162 31 5d 2c 20 6e 44 62 29 3b 0a 20 20 6d 65 6d 63  1], nDb);.  memc
32163 70 79 28 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65  py(pRtree->zName
32164 2c 20 61 72 67 76 5b 32 5d 2c 20 6e 4e 61 6d 65  , argv[2], nName
32165 29 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20  );..  /* Figure 
32166 6f 75 74 20 74 68 65 20 6e 6f 64 65 20 73 69 7a  out the node siz
32167 65 20 74 6f 20 75 73 65 2e 20 42 79 20 64 65 66  e to use. By def
32168 61 75 6c 74 2c 20 75 73 65 20 36 34 20 62 79 74  ault, use 64 byt
32169 65 73 20 6c 65 73 73 20 74 68 61 6e 0a 20 20 2a  es less than.  *
3216a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70  * the database p
3216b 61 67 65 2d 73 69 7a 65 2e 20 54 68 69 73 20 65  age-size. This e
3216c 6e 73 75 72 65 73 20 74 68 61 74 20 65 61 63 68  nsures that each
3216d 20 6e 6f 64 65 20 69 73 20 73 74 6f 72 65 64 20   node is stored 
3216e 6f 6e 0a 20 20 2a 2a 20 61 20 73 69 6e 67 6c 65  on.  ** a single
3216f 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 0a   database page..
32170 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65    **.  ** If the
32171 20 64 61 74 61 62 61 73 64 20 70 61 67 65 2d 73   databasd page-s
32172 69 7a 65 20 69 73 20 73 6f 20 6c 61 72 67 65 20  ize is so large 
32173 74 68 61 74 20 6d 6f 72 65 20 74 68 61 6e 20 52  that more than R
32174 54 52 45 45 5f 4d 41 58 43 45 4c 4c 53 0a 20 20  TREE_MAXCELLS.  
32175 2a 2a 20 65 6e 74 72 69 65 73 20 77 6f 75 6c 64  ** entries would
32176 20 66 69 74 20 69 6e 20 61 20 73 69 6e 67 6c 65   fit in a single
32177 20 6e 6f 64 65 2c 20 75 73 65 20 61 20 73 6d 61   node, use a sma
32178 6c 6c 65 72 20 6e 6f 64 65 2d 73 69 7a 65 2e 0a  ller node-size..
32179 20 20 2a 2f 0a 20 20 70 52 74 72 65 65 2d 3e 69    */.  pRtree->i
3217a 4e 6f 64 65 53 69 7a 65 20 3d 20 69 50 61 67 65  NodeSize = iPage
3217b 53 69 7a 65 2d 36 34 3b 0a 20 20 69 66 28 20 28  Size-64;.  if( (
3217c 34 2b 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73  4+pRtree->nBytes
3217d 50 65 72 43 65 6c 6c 2a 52 54 52 45 45 5f 4d 41  PerCell*RTREE_MA
3217e 58 43 45 4c 4c 53 29 3c 70 52 74 72 65 65 2d 3e  XCELLS)<pRtree->
3217f 69 4e 6f 64 65 53 69 7a 65 20 29 7b 0a 20 20 20  iNodeSize ){.   
32180 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69   pRtree->iNodeSi
32181 7a 65 20 3d 20 34 2b 70 52 74 72 65 65 2d 3e 6e  ze = 4+pRtree->n
32182 42 79 74 65 73 50 65 72 43 65 6c 6c 2a 52 54 52  BytesPerCell*RTR
32183 45 45 5f 4d 41 58 43 45 4c 4c 53 3b 0a 20 20 7d  EE_MAXCELLS;.  }
32184 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 2f 43 6f  ..  /* Create/Co
32185 6e 6e 65 63 74 20 74 6f 20 74 68 65 20 75 6e 64  nnect to the und
32186 65 72 6c 79 69 6e 67 20 72 65 6c 61 74 69 6f 6e  erlying relation
32187 61 6c 20 64 61 74 61 62 61 73 65 20 73 63 68 65  al database sche
32188 6d 61 2e 20 49 66 0a 20 20 2a 2a 20 74 68 61 74  ma. If.  ** that
32189 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20   is successful, 
3218a 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 64 65 63  call sqlite3_dec
3218b 6c 61 72 65 5f 76 74 61 62 28 29 20 74 6f 20 63  lare_vtab() to c
3218c 6f 6e 66 69 67 75 72 65 0a 20 20 2a 2a 20 74 68  onfigure.  ** th
3218d 65 20 72 2d 74 72 65 65 20 74 61 62 6c 65 20 73  e r-tree table s
3218e 63 68 65 6d 61 2e 0a 20 20 2a 2f 0a 20 20 69 66  chema..  */.  if
3218f 28 20 28 72 63 20 3d 20 72 74 72 65 65 53 71 6c  ( (rc = rtreeSql
32190 49 6e 69 74 28 70 52 74 72 65 65 2c 20 64 62 2c  Init(pRtree, db,
32191 20 61 72 67 76 5b 31 5d 2c 20 61 72 67 76 5b 32   argv[1], argv[2
32192 5d 2c 20 69 73 43 72 65 61 74 65 29 29 20 29 7b  ], isCreate)) ){
32193 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71  .    *pzErr = sq
32194 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25  lite3_mprintf("%
32195 73 22 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  s", sqlite3_errm
32196 73 67 28 64 62 29 29 3b 0a 20 20 7d 65 6c 73 65  sg(db));.  }else
32197 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 71 6c  {.    char *zSql
32198 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
32199 74 66 28 22 43 52 45 41 54 45 20 54 41 42 4c 45  tf("CREATE TABLE
3219a 20 78 28 25 73 22 2c 20 61 72 67 76 5b 33 5d 29   x(%s", argv[3])
3219b 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 6d 70  ;.    char *zTmp
3219c 3b 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20  ;.    int ii;.  
3219d 20 20 66 6f 72 28 69 69 3d 34 3b 20 7a 53 71 6c    for(ii=4; zSql
3219e 20 26 26 20 69 69 3c 61 72 67 63 3b 20 69 69 2b   && ii<argc; ii+
3219f 2b 29 7b 0a 20 20 20 20 20 20 7a 54 6d 70 20 3d  +){.      zTmp =
321a0 20 7a 53 71 6c 3b 0a 20 20 20 20 20 20 7a 53 71   zSql;.      zSq
321a1 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  l = sqlite3_mpri
321a2 6e 74 66 28 22 25 73 2c 20 25 73 22 2c 20 7a 54  ntf("%s, %s", zT
321a3 6d 70 2c 20 61 72 67 76 5b 69 69 5d 29 3b 0a 20  mp, argv[ii]);. 
321a4 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
321a5 65 28 7a 54 6d 70 29 3b 0a 20 20 20 20 7d 0a 20  e(zTmp);.    }. 
321a6 20 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20     if( zSql ){. 
321a7 20 20 20 20 20 7a 54 6d 70 20 3d 20 7a 53 71 6c       zTmp = zSql
321a8 3b 0a 20 20 20 20 20 20 7a 53 71 6c 20 3d 20 73  ;.      zSql = s
321a9 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
321aa 25 73 29 3b 22 2c 20 7a 54 6d 70 29 3b 0a 20 20  %s);", zTmp);.  
321ab 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
321ac 28 7a 54 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20  (zTmp);.    }.  
321ad 20 20 69 66 28 20 21 7a 53 71 6c 20 7c 7c 20 73    if( !zSql || s
321ae 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76  qlite3_declare_v
321af 74 61 62 28 64 62 2c 20 7a 53 71 6c 29 20 29 7b  tab(db, zSql) ){
321b0 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
321b1 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
321b2 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
321b3 28 7a 53 71 6c 29 3b 0a 20 20 7d 0a 0a 20 20 69  (zSql);.  }..  i
321b4 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
321b5 20 29 7b 0a 20 20 20 20 2a 70 70 56 74 61 62 20   ){.    *ppVtab 
321b6 3d 20 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  = (sqlite3_vtab 
321b7 2a 29 70 52 74 72 65 65 3b 0a 20 20 7d 65 6c 73  *)pRtree;.  }els
321b8 65 7b 0a 20 20 20 20 72 74 72 65 65 52 65 6c 65  e{.    rtreeRele
321b9 61 73 65 28 70 52 74 72 65 65 29 3b 0a 20 20 7d  ase(pRtree);.  }
321ba 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
321bb 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
321bc 74 61 74 69 6f 6e 20 6f 66 20 61 20 73 63 61 6c  tation of a scal
321bd 61 72 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74  ar function that
321be 20 64 65 63 6f 64 65 73 20 72 2d 74 72 65 65 20   decodes r-tree 
321bf 6e 6f 64 65 73 20 74 6f 0a 2a 2a 20 68 75 6d 61  nodes to.** huma
321c0 6e 20 72 65 61 64 61 62 6c 65 20 73 74 72 69 6e  n readable strin
321c1 67 73 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20  gs. This can be 
321c2 75 73 65 64 20 66 6f 72 20 64 65 62 75 67 67 69  used for debuggi
321c3 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 2e  ng and analysis.
321c4 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 63 61 6c 61  .**.** The scala
321c5 72 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 65 73  r function takes
321c6 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2c 20   two arguments, 
321c7 61 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 63  a blob of data c
321c8 6f 6e 74 61 69 6e 69 6e 67 0a 2a 2a 20 61 6e 20  ontaining.** an 
321c9 72 2d 74 72 65 65 20 6e 6f 64 65 2c 20 61 6e 64  r-tree node, and
321ca 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64   the number of d
321cb 69 6d 65 6e 73 69 6f 6e 73 20 74 68 65 20 72 2d  imensions the r-
321cc 74 72 65 65 20 69 6e 64 65 78 65 73 2e 0a 2a 2a  tree indexes..**
321cd 20 46 6f 72 20 61 20 74 77 6f 2d 64 69 6d 65 6e   For a two-dimen
321ce 73 69 6f 6e 61 6c 20 72 2d 74 72 65 65 20 73 74  sional r-tree st
321cf 72 75 63 74 75 72 65 20 63 61 6c 6c 65 64 20 22  ructure called "
321d0 72 74 22 2c 20 74 6f 20 64 65 73 65 72 69 61 6c  rt", to deserial
321d1 69 7a 65 0a 2a 2a 20 61 6c 6c 20 6e 6f 64 65 73  ize.** all nodes
321d2 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6c 69  , a statement li
321d3 6b 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45  ke:.**.**   SELE
321d4 43 54 20 72 74 72 65 65 6e 6f 64 65 28 32 2c 20  CT rtreenode(2, 
321d5 64 61 74 61 29 20 46 52 4f 4d 20 72 74 5f 6e 6f  data) FROM rt_no
321d6 64 65 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 75  de;.**.** The hu
321d7 6d 61 6e 20 72 65 61 64 61 62 6c 65 20 73 74 72  man readable str
321d8 69 6e 67 20 74 61 6b 65 73 20 74 68 65 20 66 6f  ing takes the fo
321d9 72 6d 20 6f 66 20 61 20 54 63 6c 20 6c 69 73 74  rm of a Tcl list
321da 20 77 69 74 68 20 6f 6e 65 0a 2a 2a 20 65 6e 74   with one.** ent
321db 72 79 20 66 6f 72 20 65 61 63 68 20 63 65 6c 6c  ry for each cell
321dc 20 69 6e 20 74 68 65 20 72 2d 74 72 65 65 20 6e   in the r-tree n
321dd 6f 64 65 2e 20 45 61 63 68 20 65 6e 74 72 79 20  ode. Each entry 
321de 69 73 20 69 74 73 65 6c 66 20 61 0a 2a 2a 20 6c  is itself a.** l
321df 69 73 74 2c 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ist, containing 
321e0 74 68 65 20 38 2d 62 79 74 65 20 72 6f 77 69 64  the 8-byte rowid
321e1 2f 70 61 67 65 6e 6f 20 66 6f 6c 6c 6f 77 65 64  /pageno followed
321e2 20 62 79 20 74 68 65 20 0a 2a 2a 20 3c 6e 75 6d   by the .** <num
321e3 2d 64 69 6d 65 6e 73 69 6f 6e 3e 2a 32 20 63 6f  -dimension>*2 co
321e4 6f 72 64 69 6e 61 74 65 73 2e 0a 2a 2f 0a 73 74  ordinates..*/.st
321e5 61 74 69 63 20 76 6f 69 64 20 72 74 72 65 65 6e  atic void rtreen
321e6 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ode(sqlite3_cont
321e7 65 78 74 20 2a 63 74 78 2c 20 69 6e 74 20 6e 41  ext *ctx, int nA
321e8 72 67 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  rg, sqlite3_valu
321e9 65 20 2a 2a 61 70 41 72 67 29 7b 0a 20 20 63 68  e **apArg){.  ch
321ea 61 72 20 2a 7a 54 65 78 74 20 3d 20 30 3b 0a 20  ar *zText = 0;. 
321eb 20 52 74 72 65 65 4e 6f 64 65 20 6e 6f 64 65 3b   RtreeNode node;
321ec 0a 20 20 52 74 72 65 65 20 74 72 65 65 3b 0a 20  .  Rtree tree;. 
321ed 20 69 6e 74 20 69 69 3b 0a 0a 20 20 6d 65 6d 73   int ii;..  mems
321ee 65 74 28 26 6e 6f 64 65 2c 20 30 2c 20 73 69 7a  et(&node, 0, siz
321ef 65 6f 66 28 52 74 72 65 65 4e 6f 64 65 29 29 3b  eof(RtreeNode));
321f0 0a 20 20 6d 65 6d 73 65 74 28 26 74 72 65 65 2c  .  memset(&tree,
321f1 20 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65   0, sizeof(Rtree
321f2 29 29 3b 0a 20 20 74 72 65 65 2e 6e 44 69 6d 20  ));.  tree.nDim 
321f3 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
321f4 69 6e 74 28 61 70 41 72 67 5b 30 5d 29 3b 0a 20  int(apArg[0]);. 
321f5 20 74 72 65 65 2e 6e 42 79 74 65 73 50 65 72 43   tree.nBytesPerC
321f6 65 6c 6c 20 3d 20 38 20 2b 20 38 20 2a 20 74 72  ell = 8 + 8 * tr
321f7 65 65 2e 6e 44 69 6d 3b 0a 20 20 6e 6f 64 65 2e  ee.nDim;.  node.
321f8 7a 44 61 74 61 20 3d 20 28 75 38 20 2a 29 73 71  zData = (u8 *)sq
321f9 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
321fa 28 61 70 41 72 67 5b 31 5d 29 3b 0a 0a 20 20 66  (apArg[1]);..  f
321fb 6f 72 28 69 69 3d 30 3b 20 69 69 3c 4e 43 45 4c  or(ii=0; ii<NCEL
321fc 4c 28 26 6e 6f 64 65 29 3b 20 69 69 2b 2b 29 7b  L(&node); ii++){
321fd 0a 20 20 20 20 63 68 61 72 20 7a 43 65 6c 6c 5b  .    char zCell[
321fe 35 31 32 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 43  512];.    int nC
321ff 65 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 52 74 72  ell = 0;.    Rtr
32200 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20  eeCell cell;.   
32201 20 69 6e 74 20 6a 6a 3b 0a 0a 20 20 20 20 6e 6f   int jj;..    no
32202 64 65 47 65 74 43 65 6c 6c 28 26 74 72 65 65 2c  deGetCell(&tree,
32203 20 26 6e 6f 64 65 2c 20 69 69 2c 20 26 63 65 6c   &node, ii, &cel
32204 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  l);.    sqlite3_
32205 73 6e 70 72 69 6e 74 66 28 35 31 32 2d 6e 43 65  snprintf(512-nCe
32206 6c 6c 2c 26 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d  ll,&zCell[nCell]
32207 2c 22 25 64 22 2c 20 63 65 6c 6c 2e 69 52 6f 77  ,"%d", cell.iRow
32208 69 64 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d  id);.    nCell =
32209 20 73 74 72 6c 65 6e 28 7a 43 65 6c 6c 29 3b 0a   strlen(zCell);.
3220a 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a      for(jj=0; jj
3220b 3c 74 72 65 65 2e 6e 44 69 6d 2a 32 3b 20 6a 6a  <tree.nDim*2; jj
3220c 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
3220d 65 33 5f 73 6e 70 72 69 6e 74 66 28 35 31 32 2d  e3_snprintf(512-
3220e 6e 43 65 6c 6c 2c 26 7a 43 65 6c 6c 5b 6e 43 65  nCell,&zCell[nCe
3220f 6c 6c 5d 2c 22 20 25 66 22 2c 28 64 6f 75 62 6c  ll]," %f",(doubl
32210 65 29 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 6a 6a  e)cell.aCoord[jj
32211 5d 2e 66 29 3b 0a 20 20 20 20 20 20 6e 43 65 6c  ].f);.      nCel
32212 6c 20 3d 20 73 74 72 6c 65 6e 28 7a 43 65 6c 6c  l = strlen(zCell
32213 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  );.    }..    if
32214 28 20 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 20  ( zText ){.     
32215 20 63 68 61 72 20 2a 7a 54 65 78 74 4e 65 77 20   char *zTextNew 
32216 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
32217 66 28 22 25 73 20 7b 25 73 7d 22 2c 20 7a 54 65  f("%s {%s}", zTe
32218 78 74 2c 20 7a 43 65 6c 6c 29 3b 0a 20 20 20 20  xt, zCell);.    
32219 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
3221a 54 65 78 74 29 3b 0a 20 20 20 20 20 20 7a 54 65  Text);.      zTe
3221b 78 74 20 3d 20 7a 54 65 78 74 4e 65 77 3b 0a 20  xt = zTextNew;. 
3221c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
3221d 7a 54 65 78 74 20 3d 20 73 71 6c 69 74 65 33 5f  zText = sqlite3_
3221e 6d 70 72 69 6e 74 66 28 22 7b 25 73 7d 22 2c 20  mprintf("{%s}", 
3221f 7a 43 65 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20  zCell);.    }.  
32220 7d 0a 20 20 0a 20 20 73 71 6c 69 74 65 33 5f 72  }.  .  sqlite3_r
32221 65 73 75 6c 74 5f 74 65 78 74 28 63 74 78 2c 20  esult_text(ctx, 
32222 7a 54 65 78 74 2c 20 2d 31 2c 20 73 71 6c 69 74  zText, -1, sqlit
32223 65 33 5f 66 72 65 65 29 3b 0a 7d 0a 0a 73 74 61  e3_free);.}..sta
32224 74 69 63 20 76 6f 69 64 20 72 74 72 65 65 64 65  tic void rtreede
32225 70 74 68 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  pth(sqlite3_cont
32226 65 78 74 20 2a 63 74 78 2c 20 69 6e 74 20 6e 41  ext *ctx, int nA
32227 72 67 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  rg, sqlite3_valu
32228 65 20 2a 2a 61 70 41 72 67 29 7b 0a 20 20 69 66  e **apArg){.  if
32229 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
3222a 74 79 70 65 28 61 70 41 72 67 5b 30 5d 29 21 3d  type(apArg[0])!=
3222b 53 51 4c 49 54 45 5f 42 4c 4f 42 20 0a 20 20 20  SQLITE_BLOB .   
3222c 7c 7c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  || sqlite3_value
3222d 5f 62 79 74 65 73 28 61 70 41 72 67 5b 30 5d 29  _bytes(apArg[0])
3222e 3c 32 0a 20 20 29 7b 0a 20 20 20 20 73 71 6c 69  <2.  ){.    sqli
3222f 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
32230 28 63 74 78 2c 20 22 49 6e 76 61 6c 69 64 20 61  (ctx, "Invalid a
32231 72 67 75 6d 65 6e 74 20 74 6f 20 72 74 72 65 65  rgument to rtree
32232 64 65 70 74 68 28 29 22 2c 20 2d 31 29 3b 20 0a  depth()", -1); .
32233 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 38 20    }else{.    u8 
32234 2a 7a 42 6c 6f 62 20 3d 20 28 75 38 20 2a 29 73  *zBlob = (u8 *)s
32235 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
32236 62 28 61 70 41 72 67 5b 30 5d 29 3b 0a 20 20 20  b(apArg[0]);.   
32237 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
32238 69 6e 74 28 63 74 78 2c 20 72 65 61 64 49 6e 74  int(ctx, readInt
32239 31 36 28 7a 42 6c 6f 62 29 29 3b 0a 20 20 7d 0a  16(zBlob));.  }.
3223a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65  }../*.** Registe
3223b 72 20 74 68 65 20 72 2d 74 72 65 65 20 6d 6f 64  r the r-tree mod
3223c 75 6c 65 20 77 69 74 68 20 64 61 74 61 62 61 73  ule with databas
3223d 65 20 68 61 6e 64 6c 65 20 64 62 2e 20 54 68 69  e handle db. Thi
3223e 73 20 63 72 65 61 74 65 73 20 74 68 65 0a 2a 2a  s creates the.**
3223f 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
32240 6f 64 75 6c 65 20 22 72 74 72 65 65 22 20 61 6e  odule "rtree" an
32241 64 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 2f  d the debugging/
32242 61 6e 61 6c 79 73 69 73 20 73 63 61 6c 61 72 20  analysis scalar 
32243 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 22 72 74  .** function "rt
32244 72 65 65 6e 6f 64 65 22 2e 0a 2a 2f 0a 53 51 4c  reenode"..*/.SQL
32245 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
32246 73 71 6c 69 74 65 33 52 74 72 65 65 49 6e 69 74  sqlite3RtreeInit
32247 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
32248 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
32249 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d  _OK;..  if( rc==
3224a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
3224b 20 69 6e 74 20 75 74 66 38 20 3d 20 53 51 4c 49   int utf8 = SQLI
3224c 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 72 63 20  TE_UTF8;.    rc 
3224d 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  = sqlite3_create
3224e 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 72  _function(db, "r
3224f 74 72 65 65 6e 6f 64 65 22 2c 20 32 2c 20 75 74  treenode", 2, ut
32250 66 38 2c 20 30 2c 20 72 74 72 65 65 6e 6f 64 65  f8, 0, rtreenode
32251 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69  , 0, 0);.  }.  i
32252 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
32253 20 29 7b 0a 20 20 20 20 69 6e 74 20 75 74 66 38   ){.    int utf8
32254 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a   = SQLITE_UTF8;.
32255 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
32256 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
32257 28 64 62 2c 20 22 72 74 72 65 65 64 65 70 74 68  (db, "rtreedepth
32258 22 2c 20 31 2c 20 75 74 66 38 2c 20 30 2c 72 74  ", 1, utf8, 0,rt
32259 72 65 65 64 65 70 74 68 2c 20 30 2c 20 30 29 3b  reedepth, 0, 0);
3225a 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
3225b 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
3225c 76 6f 69 64 20 2a 63 20 3d 20 28 76 6f 69 64 20  void *c = (void 
3225d 2a 29 52 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45  *)RTREE_COORD_RE
3225e 41 4c 33 32 3b 0a 20 20 20 20 72 63 20 3d 20 73  AL32;.    rc = s
3225f 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f  qlite3_create_mo
32260 64 75 6c 65 5f 76 32 28 64 62 2c 20 22 72 74 72  dule_v2(db, "rtr
32261 65 65 22 2c 20 26 72 74 72 65 65 4d 6f 64 75 6c  ee", &rtreeModul
32262 65 2c 20 63 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  e, c, 0);.  }.  
32263 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
32264 4b 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 63  K ){.    void *c
32265 20 3d 20 28 76 6f 69 64 20 2a 29 52 54 52 45 45   = (void *)RTREE
32266 5f 43 4f 4f 52 44 5f 49 4e 54 33 32 3b 0a 20 20  _COORD_INT32;.  
32267 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63    rc = sqlite3_c
32268 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28  reate_module_v2(
32269 64 62 2c 20 22 72 74 72 65 65 5f 69 33 32 22 2c  db, "rtree_i32",
3226a 20 26 72 74 72 65 65 4d 6f 64 75 6c 65 2c 20 63   &rtreeModule, c
3226b 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  , 0);.  }..  ret
3226c 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21  urn rc;.}..#if !
3226d 53 51 4c 49 54 45 5f 43 4f 52 45 0a 53 51 4c 49  SQLITE_CORE.SQLI
3226e 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
3226f 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69  e3_extension_ini
32270 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  t(.  sqlite3 *db
32271 2c 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72  ,.  char **pzErr
32272 4d 73 67 2c 0a 20 20 63 6f 6e 73 74 20 73 71 6c  Msg,.  const sql
32273 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65  ite3_api_routine
32274 73 20 2a 70 41 70 69 0a 29 7b 0a 20 20 53 51 4c  s *pApi.){.  SQL
32275 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e  ITE_EXTENSION_IN
32276 49 54 32 28 70 41 70 69 29 0a 20 20 72 65 74 75  IT2(pApi).  retu
32277 72 6e 20 73 71 6c 69 74 65 33 52 74 72 65 65 49  rn sqlite3RtreeI
32278 6e 69 74 28 64 62 29 3b 0a 7d 0a 23 65 6e 64 69  nit(db);.}.#endi
32279 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a  f..#endif../****
3227a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
3227b 66 20 72 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a  f rtree.c ******
3227c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3227d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3227e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
3227f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
32280 20 66 69 6c 65 20 69 63 75 2e 63 20 2a 2a 2a 2a   file icu.c ****
32281 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32282 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32283 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
32284 20 32 30 30 37 20 4d 61 79 20 36 0a 2a 2a 0a 2a   2007 May 6.**.*
32285 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
32286 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
32287 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
32288 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
32289 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
3228a 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
3228b 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
3228c 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
3228d 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
3228e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
3228f 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
32290 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
32291 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
32292 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
32293 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
32294 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
32295 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
32296 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
32297 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32298 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32299 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3229a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49  **********.** $I
3229b 64 3a 20 69 63 75 2e 63 2c 76 20 31 2e 37 20 32  d: icu.c,v 1.7 2
3229c 30 30 37 2f 31 32 2f 31 33 20 32 31 3a 35 34 3a  007/12/13 21:54:
3229d 31 31 20 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a  11 drh Exp $.**.
3229e 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70  ** This file imp
3229f 6c 65 6d 65 6e 74 73 20 61 6e 20 69 6e 74 65 67  lements an integ
322a0 72 61 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 74  ration between t
322a1 68 65 20 49 43 55 20 6c 69 62 72 61 72 79 20 0a  he ICU library .
322a2 2a 2a 20 28 22 49 6e 74 65 72 6e 61 74 69 6f 6e  ** ("Internation
322a3 61 6c 20 43 6f 6d 70 6f 6e 65 6e 74 73 20 66 6f  al Components fo
322a4 72 20 55 6e 69 63 6f 64 65 22 2c 20 61 6e 20 6f  r Unicode", an o
322a5 70 65 6e 2d 73 6f 75 72 63 65 20 6c 69 62 72 61  pen-source libra
322a6 72 79 20 0a 2a 2a 20 66 6f 72 20 68 61 6e 64 6c  ry .** for handl
322a7 69 6e 67 20 75 6e 69 63 6f 64 65 20 64 61 74 61  ing unicode data
322a8 29 20 61 6e 64 20 53 51 4c 69 74 65 2e 20 54 68  ) and SQLite. Th
322a9 65 20 69 6e 74 65 67 72 61 74 69 6f 6e 20 75 73  e integration us
322aa 65 73 20 0a 2a 2a 20 49 43 55 20 74 6f 20 70 72  es .** ICU to pr
322ab 6f 76 69 64 65 20 74 68 65 20 66 6f 6c 6c 6f 77  ovide the follow
322ac 69 6e 67 20 74 6f 20 53 51 4c 69 74 65 3a 0a 2a  ing to SQLite:.*
322ad 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 6d 70 6c  *.**   * An impl
322ae 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
322af 65 20 53 51 4c 20 72 65 67 65 78 70 28 29 20 66  e SQL regexp() f
322b0 75 6e 63 74 69 6f 6e 20 28 61 6e 64 20 68 65 6e  unction (and hen
322b1 63 65 20 52 45 47 45 58 50 0a 2a 2a 20 20 20 20  ce REGEXP.**    
322b2 20 6f 70 65 72 61 74 6f 72 29 20 75 73 69 6e 67   operator) using
322b3 20 74 68 65 20 49 43 55 20 75 72 65 67 65 78 5f   the ICU uregex_
322b4 58 58 28 29 20 41 50 49 73 2e 0a 2a 2a 0a 2a 2a  XX() APIs..**.**
322b5 20 20 20 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74     * Implementat
322b6 69 6f 6e 73 20 6f 66 20 74 68 65 20 53 51 4c 20  ions of the SQL 
322b7 73 63 61 6c 61 72 20 75 70 70 65 72 28 29 20 61  scalar upper() a
322b8 6e 64 20 6c 6f 77 65 72 28 29 20 66 75 6e 63 74  nd lower() funct
322b9 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 66 6f 72 20  ions.**     for 
322ba 63 61 73 65 20 6d 61 70 70 69 6e 67 2e 0a 2a 2a  case mapping..**
322bb 0a 2a 2a 20 20 20 2a 20 49 6e 74 65 67 72 61 74  .**   * Integrat
322bc 69 6f 6e 20 6f 66 20 49 43 55 20 61 6e 64 20 53  ion of ICU and S
322bd 51 4c 69 74 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  QLite collation 
322be 73 65 71 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20  seqences..**.** 
322bf 20 20 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74    * An implement
322c0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c 49 4b  ation of the LIK
322c1 45 20 6f 70 65 72 61 74 6f 72 20 74 68 61 74 20  E operator that 
322c2 75 73 65 73 20 49 43 55 20 74 6f 20 0a 2a 2a 20  uses ICU to .** 
322c3 20 20 20 20 70 72 6f 76 69 64 65 20 63 61 73 65      provide case
322c4 2d 69 6e 64 65 70 65 6e 64 65 6e 74 20 6d 61 74  -independent mat
322c5 63 68 69 6e 67 2e 0a 2a 2f 0a 0a 23 69 66 20 21  ching..*/..#if !
322c6 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43  defined(SQLITE_C
322c7 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  ORE) || defined(
322c8 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43  SQLITE_ENABLE_IC
322c9 55 29 0a 0a 2f 2a 20 49 6e 63 6c 75 64 65 20 49  U)../* Include I
322ca 43 55 20 68 65 61 64 65 72 73 20 2a 2f 0a 23 69  CU headers */.#i
322cb 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f  nclude <unicode/
322cc 75 74 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c 75  utypes.h>.#inclu
322cd 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75 72 65 67  de <unicode/ureg
322ce 65 78 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c  ex.h>.#include <
322cf 75 6e 69 63 6f 64 65 2f 75 73 74 72 69 6e 67 2e  unicode/ustring.
322d0 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69  h>.#include <uni
322d1 63 6f 64 65 2f 75 63 6f 6c 2e 68 3e 0a 0a 0a 23  code/ucol.h>...#
322d2 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f  ifndef SQLITE_CO
322d3 52 45 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45  RE.  SQLITE_EXTE
322d4 4e 53 49 4f 4e 5f 49 4e 49 54 31 0a 23 65 6c 73  NSION_INIT1.#els
322d5 65 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  e.#endif../*.** 
322d6 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 28  Maximum length (
322d7 69 6e 20 62 79 74 65 73 29 20 6f 66 20 74 68 65  in bytes) of the
322d8 20 70 61 74 74 65 72 6e 20 69 6e 20 61 20 4c 49   pattern in a LI
322d9 4b 45 20 6f 72 20 47 4c 4f 42 0a 2a 2a 20 6f 70  KE or GLOB.** op
322da 65 72 61 74 6f 72 2e 0a 2a 2f 0a 23 69 66 6e 64  erator..*/.#ifnd
322db 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49  ef SQLITE_MAX_LI
322dc 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54  KE_PATTERN_LENGT
322dd 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  H.# define SQLIT
322de 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45  E_MAX_LIKE_PATTE
322df 52 4e 5f 4c 45 4e 47 54 48 20 35 30 30 30 30 0a  RN_LENGTH 50000.
322e0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 56 65  #endif../*.** Ve
322e1 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33  rsion of sqlite3
322e2 5f 66 72 65 65 28 29 20 74 68 61 74 20 69 73 20  _free() that is 
322e3 61 6c 77 61 79 73 20 61 20 66 75 6e 63 74 69 6f  always a functio
322e4 6e 2c 20 6e 65 76 65 72 20 61 20 6d 61 63 72 6f  n, never a macro
322e5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
322e6 20 78 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b   xFree(void *p){
322e7 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
322e8 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d  p);.}../*.** Com
322e9 70 61 72 65 20 74 77 6f 20 55 54 46 2d 38 20 73  pare two UTF-8 s
322ea 74 72 69 6e 67 73 20 66 6f 72 20 65 71 75 61 6c  trings for equal
322eb 69 74 79 20 77 68 65 72 65 20 74 68 65 20 66 69  ity where the fi
322ec 72 73 74 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a  rst string is.**
322ed 20 61 20 22 4c 49 4b 45 22 20 65 78 70 72 65 73   a "LIKE" expres
322ee 73 69 6f 6e 2e 20 52 65 74 75 72 6e 20 74 72 75  sion. Return tru
322ef 65 20 28 31 29 20 69 66 20 74 68 65 79 20 61 72  e (1) if they ar
322f0 65 20 74 68 65 20 73 61 6d 65 20 61 6e 64 20 0a  e the same and .
322f1 2a 2a 20 66 61 6c 73 65 20 28 30 29 20 69 66 20  ** false (0) if 
322f2 74 68 65 79 20 61 72 65 20 64 69 66 66 65 72 65  they are differe
322f3 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  nt..*/.static in
322f4 74 20 69 63 75 4c 69 6b 65 43 6f 6d 70 61 72 65  t icuLikeCompare
322f5 28 0a 20 20 63 6f 6e 73 74 20 75 69 6e 74 38 5f  (.  const uint8_
322f6 74 20 2a 7a 50 61 74 74 65 72 6e 2c 20 20 20 2f  t *zPattern,   /
322f7 2a 20 4c 49 4b 45 20 70 61 74 74 65 72 6e 20 2a  * LIKE pattern *
322f8 2f 0a 20 20 63 6f 6e 73 74 20 75 69 6e 74 38 5f  /.  const uint8_
322f9 74 20 2a 7a 53 74 72 69 6e 67 2c 20 20 20 20 2f  t *zString,    /
322fa 2a 20 54 68 65 20 55 54 46 2d 38 20 73 74 72 69  * The UTF-8 stri
322fb 6e 67 20 74 6f 20 63 6f 6d 70 61 72 65 20 61 67  ng to compare ag
322fc 61 69 6e 73 74 20 2a 2f 0a 20 20 63 6f 6e 73 74  ainst */.  const
322fd 20 55 43 68 61 72 33 32 20 75 45 73 63 20 20 20   UChar32 uEsc   
322fe 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 73 63        /* The esc
322ff 61 70 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f  ape character */
32300 0a 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  .){.  static con
32301 73 74 20 69 6e 74 20 4d 41 54 43 48 5f 4f 4e 45  st int MATCH_ONE
32302 20 3d 20 28 55 43 68 61 72 33 32 29 27 5f 27 3b   = (UChar32)'_';
32303 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
32304 69 6e 74 20 4d 41 54 43 48 5f 41 4c 4c 20 3d 20  int MATCH_ALL = 
32305 28 55 43 68 61 72 33 32 29 27 25 27 3b 0a 0a 20  (UChar32)'%';.. 
32306 20 69 6e 74 20 69 50 61 74 74 65 72 6e 20 3d 20   int iPattern = 
32307 30 3b 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72  0;       /* Curr
32308 65 6e 74 20 62 79 74 65 20 69 6e 64 65 78 20 69  ent byte index i
32309 6e 20 7a 50 61 74 74 65 72 6e 20 2a 2f 0a 20 20  n zPattern */.  
3230a 69 6e 74 20 69 53 74 72 69 6e 67 20 3d 20 30 3b  int iString = 0;
3230b 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65          /* Curre
3230c 6e 74 20 62 79 74 65 20 69 6e 64 65 78 20 69 6e  nt byte index in
3230d 20 7a 53 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 69   zString */..  i
3230e 6e 74 20 70 72 65 76 45 73 63 61 70 65 20 3d 20  nt prevEscape = 
3230f 30 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  0;     /* True i
32310 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63  f the previous c
32311 68 61 72 61 63 74 65 72 20 77 61 73 20 75 45 73  haracter was uEs
32312 63 20 2a 2f 0a 0a 20 20 77 68 69 6c 65 28 20 7a  c */..  while( z
32313 50 61 74 74 65 72 6e 5b 69 50 61 74 74 65 72 6e  Pattern[iPattern
32314 5d 21 3d 30 20 29 7b 0a 0a 20 20 20 20 2f 2a 20  ]!=0 ){..    /* 
32315 52 65 61 64 20 28 61 6e 64 20 63 6f 6e 73 75 6d  Read (and consum
32316 65 29 20 74 68 65 20 6e 65 78 74 20 63 68 61 72  e) the next char
32317 61 63 74 65 72 20 66 72 6f 6d 20 74 68 65 20 69  acter from the i
32318 6e 70 75 74 20 70 61 74 74 65 72 6e 2e 20 2a 2f  nput pattern. */
32319 0a 20 20 20 20 55 43 68 61 72 33 32 20 75 50 61  .    UChar32 uPa
3231a 74 74 65 72 6e 3b 0a 20 20 20 20 55 38 5f 4e 45  ttern;.    U8_NE
3231b 58 54 5f 55 4e 53 41 46 45 28 7a 50 61 74 74 65  XT_UNSAFE(zPatte
3231c 72 6e 2c 20 69 50 61 74 74 65 72 6e 2c 20 75 50  rn, iPattern, uP
3231d 61 74 74 65 72 6e 29 3b 0a 20 20 20 20 61 73 73  attern);.    ass
3231e 65 72 74 28 75 50 61 74 74 65 72 6e 21 3d 30 29  ert(uPattern!=0)
3231f 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20  ;..    /* There 
32320 61 72 65 20 6e 6f 77 20 34 20 70 6f 73 73 69 62  are now 4 possib
32321 69 6c 69 74 69 65 73 3a 0a 20 20 20 20 2a 2a 0a  ilities:.    **.
32322 20 20 20 20 2a 2a 20 20 20 20 20 31 2e 20 75 50      **     1. uP
32323 61 74 74 65 72 6e 20 69 73 20 61 6e 20 75 6e 65  attern is an une
32324 73 63 61 70 65 64 20 6d 61 74 63 68 2d 61 6c 6c  scaped match-all
32325 20 63 68 61 72 61 63 74 65 72 20 22 25 22 2c 0a   character "%",.
32326 20 20 20 20 2a 2a 20 20 20 20 20 32 2e 20 75 50      **     2. uP
32327 61 74 74 65 72 6e 20 69 73 20 61 6e 20 75 6e 65  attern is an une
32328 73 63 61 70 65 64 20 6d 61 74 63 68 2d 6f 6e 65  scaped match-one
32329 20 63 68 61 72 61 63 74 65 72 20 22 5f 22 2c 0a   character "_",.
3232a 20 20 20 20 2a 2a 20 20 20 20 20 33 2e 20 75 50      **     3. uP
3232b 61 74 74 65 72 6e 20 69 73 20 61 6e 20 75 6e 65  attern is an une
3232c 73 63 61 70 65 64 20 65 73 63 61 70 65 20 63 68  scaped escape ch
3232d 61 72 61 63 74 65 72 2c 20 6f 72 0a 20 20 20 20  aracter, or.    
3232e 2a 2a 20 20 20 20 20 34 2e 20 75 50 61 74 74 65  **     4. uPatte
3232f 72 6e 20 69 73 20 74 6f 20 62 65 20 68 61 6e 64  rn is to be hand
32330 6c 65 64 20 61 73 20 61 6e 20 6f 72 64 69 6e 61  led as an ordina
32331 72 79 20 63 68 61 72 61 63 74 65 72 0a 20 20 20  ry character.   
32332 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 72 65   */.    if( !pre
32333 76 45 73 63 61 70 65 20 26 26 20 75 50 61 74 74  vEscape && uPatt
32334 65 72 6e 3d 3d 4d 41 54 43 48 5f 41 4c 4c 20 29  ern==MATCH_ALL )
32335 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20  {.      /* Case 
32336 31 2e 20 2a 2f 0a 20 20 20 20 20 20 75 69 6e 74  1. */.      uint
32337 38 5f 74 20 63 3b 0a 0a 20 20 20 20 20 20 2f 2a  8_t c;..      /*
32338 20 53 6b 69 70 20 61 6e 79 20 4d 41 54 43 48 5f   Skip any MATCH_
32339 41 4c 4c 20 6f 72 20 4d 41 54 43 48 5f 4f 4e 45  ALL or MATCH_ONE
3233a 20 63 68 61 72 61 63 74 65 72 73 20 74 68 61 74   characters that
3233b 20 66 6f 6c 6c 6f 77 20 61 0a 20 20 20 20 20 20   follow a.      
3233c 2a 2a 20 4d 41 54 43 48 5f 41 4c 4c 2e 20 46 6f  ** MATCH_ALL. Fo
3233d 72 20 65 61 63 68 20 4d 41 54 43 48 5f 4f 4e 45  r each MATCH_ONE
3233e 2c 20 73 6b 69 70 20 6f 6e 65 20 63 68 61 72 61  , skip one chara
3233f 63 74 65 72 20 69 6e 20 74 68 65 20 0a 20 20 20  cter in the .   
32340 20 20 20 2a 2a 20 74 65 73 74 20 73 74 72 69 6e     ** test strin
32341 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  g..      */.    
32342 20 20 77 68 69 6c 65 28 20 28 63 3d 7a 50 61 74    while( (c=zPat
32343 74 65 72 6e 5b 69 50 61 74 74 65 72 6e 5d 29 20  tern[iPattern]) 
32344 3d 3d 20 4d 41 54 43 48 5f 41 4c 4c 20 7c 7c 20  == MATCH_ALL || 
32345 63 20 3d 3d 20 4d 41 54 43 48 5f 4f 4e 45 20 29  c == MATCH_ONE )
32346 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 3d  {.        if( c=
32347 3d 4d 41 54 43 48 5f 4f 4e 45 20 29 7b 0a 20 20  =MATCH_ONE ){.  
32348 20 20 20 20 20 20 20 20 69 66 28 20 7a 53 74 72          if( zStr
32349 69 6e 67 5b 69 53 74 72 69 6e 67 5d 3d 3d 30 20  ing[iString]==0 
3234a 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
3234b 20 20 20 20 20 20 55 38 5f 46 57 44 5f 31 5f 55        U8_FWD_1_U
3234c 4e 53 41 46 45 28 7a 53 74 72 69 6e 67 2c 20 69  NSAFE(zString, i
3234d 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 20  String);.       
3234e 20 7d 0a 20 20 20 20 20 20 20 20 69 50 61 74 74   }.        iPatt
3234f 65 72 6e 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 0a  ern++;.      }..
32350 20 20 20 20 20 20 69 66 28 20 7a 50 61 74 74 65        if( zPatte
32351 72 6e 5b 69 50 61 74 74 65 72 6e 5d 3d 3d 30 20  rn[iPattern]==0 
32352 29 20 72 65 74 75 72 6e 20 31 3b 0a 0a 20 20 20  ) return 1;..   
32353 20 20 20 77 68 69 6c 65 28 20 7a 53 74 72 69 6e     while( zStrin
32354 67 5b 69 53 74 72 69 6e 67 5d 20 29 7b 0a 20 20  g[iString] ){.  
32355 20 20 20 20 20 20 69 66 28 20 69 63 75 4c 69 6b        if( icuLik
32356 65 43 6f 6d 70 61 72 65 28 26 7a 50 61 74 74 65  eCompare(&zPatte
32357 72 6e 5b 69 50 61 74 74 65 72 6e 5d 2c 20 26 7a  rn[iPattern], &z
32358 53 74 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d 2c  String[iString],
32359 20 75 45 73 63 29 20 29 7b 0a 20 20 20 20 20 20   uEsc) ){.      
3235a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
3235b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
3235c 55 38 5f 46 57 44 5f 31 5f 55 4e 53 41 46 45 28  U8_FWD_1_UNSAFE(
3235d 7a 53 74 72 69 6e 67 2c 20 69 53 74 72 69 6e 67  zString, iString
3235e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
3235f 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 20 20   return 0;..    
32360 7d 65 6c 73 65 20 69 66 28 20 21 70 72 65 76 45  }else if( !prevE
32361 73 63 61 70 65 20 26 26 20 75 50 61 74 74 65 72  scape && uPatter
32362 6e 3d 3d 4d 41 54 43 48 5f 4f 4e 45 20 29 7b 0a  n==MATCH_ONE ){.
32363 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20 32 2e        /* Case 2.
32364 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 7a 53   */.      if( zS
32365 74 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d 3d 3d  tring[iString]==
32366 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
32367 20 20 20 20 55 38 5f 46 57 44 5f 31 5f 55 4e 53      U8_FWD_1_UNS
32368 41 46 45 28 7a 53 74 72 69 6e 67 2c 20 69 53 74  AFE(zString, iSt
32369 72 69 6e 67 29 3b 0a 0a 20 20 20 20 7d 65 6c 73  ring);..    }els
3236a 65 20 69 66 28 20 21 70 72 65 76 45 73 63 61 70  e if( !prevEscap
3236b 65 20 26 26 20 75 50 61 74 74 65 72 6e 3d 3d 75  e && uPattern==u
3236c 45 73 63 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43  Esc){.      /* C
3236d 61 73 65 20 33 2e 20 2a 2f 0a 20 20 20 20 20 20  ase 3. */.      
3236e 70 72 65 76 45 73 63 61 70 65 20 3d 20 31 3b 0a  prevEscape = 1;.
3236f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
32370 20 20 2f 2a 20 43 61 73 65 20 34 2e 20 2a 2f 0a    /* Case 4. */.
32371 20 20 20 20 20 20 55 43 68 61 72 33 32 20 75 53        UChar32 uS
32372 74 72 69 6e 67 3b 0a 20 20 20 20 20 20 55 38 5f  tring;.      U8_
32373 4e 45 58 54 5f 55 4e 53 41 46 45 28 7a 53 74 72  NEXT_UNSAFE(zStr
32374 69 6e 67 2c 20 69 53 74 72 69 6e 67 2c 20 75 53  ing, iString, uS
32375 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 75 53  tring);.      uS
32376 74 72 69 6e 67 20 3d 20 75 5f 66 6f 6c 64 43 61  tring = u_foldCa
32377 73 65 28 75 53 74 72 69 6e 67 2c 20 55 5f 46 4f  se(uString, U_FO
32378 4c 44 5f 43 41 53 45 5f 44 45 46 41 55 4c 54 29  LD_CASE_DEFAULT)
32379 3b 0a 20 20 20 20 20 20 75 50 61 74 74 65 72 6e  ;.      uPattern
3237a 20 3d 20 75 5f 66 6f 6c 64 43 61 73 65 28 75 50   = u_foldCase(uP
3237b 61 74 74 65 72 6e 2c 20 55 5f 46 4f 4c 44 5f 43  attern, U_FOLD_C
3237c 41 53 45 5f 44 45 46 41 55 4c 54 29 3b 0a 20 20  ASE_DEFAULT);.  
3237d 20 20 20 20 69 66 28 20 75 53 74 72 69 6e 67 21      if( uString!
3237e 3d 75 50 61 74 74 65 72 6e 20 29 7b 0a 20 20 20  =uPattern ){.   
3237f 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
32380 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 72 65       }.      pre
32381 76 45 73 63 61 70 65 20 3d 20 30 3b 0a 20 20 20  vEscape = 0;.   
32382 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
32383 20 7a 53 74 72 69 6e 67 5b 69 53 74 72 69 6e 67   zString[iString
32384 5d 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ]==0;.}../*.** I
32385 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
32386 20 74 68 65 20 6c 69 6b 65 28 29 20 53 51 4c 20   the like() SQL 
32387 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 20  function.  This 
32388 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65  function impleme
32389 6e 74 73 0a 2a 2a 20 74 68 65 20 62 75 69 6c 64  nts.** the build
3238a 2d 69 6e 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f  -in LIKE operato
3238b 72 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72  r.  The first ar
3238c 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 75  gument to the fu
3238d 6e 63 74 69 6f 6e 20 69 73 20 74 68 65 0a 2a 2a  nction is the.**
3238e 20 70 61 74 74 65 72 6e 20 61 6e 64 20 74 68 65   pattern and the
3238f 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
32390 20 69 73 20 74 68 65 20 73 74 72 69 6e 67 2e 20   is the string. 
32391 20 53 6f 2c 20 74 68 65 20 53 51 4c 20 73 74 61   So, the SQL sta
32392 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20  tements:.**.**  
32393 20 20 20 20 20 41 20 4c 49 4b 45 20 42 0a 2a 2a       A LIKE B.**
32394 0a 2a 2a 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74  .** is implement
32395 65 64 20 61 73 20 6c 69 6b 65 28 42 2c 20 41 29  ed as like(B, A)
32396 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6e  . If there is an
32397 20 65 73 63 61 70 65 20 63 68 61 72 61 63 74 65   escape characte
32398 72 20 45 2c 20 0a 2a 2a 0a 2a 2a 20 20 20 20 20  r E, .**.**     
32399 20 20 41 20 4c 49 4b 45 20 42 20 45 53 43 41 50    A LIKE B ESCAP
3239a 45 20 45 0a 2a 2a 0a 2a 2a 20 69 73 20 6d 61 70  E E.**.** is map
3239b 70 65 64 20 74 6f 20 6c 69 6b 65 28 42 2c 20 41  ped to like(B, A
3239c 2c 20 45 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  , E)..*/.static 
3239d 76 6f 69 64 20 69 63 75 4c 69 6b 65 46 75 6e 63  void icuLikeFunc
3239e 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
3239f 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20  ext *context, . 
323a0 20 69 6e 74 20 61 72 67 63 2c 20 0a 20 20 73 71   int argc, .  sq
323a1 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
323a2 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e  gv.){.  const un
323a3 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 41 20  signed char *zA 
323a4 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
323a5 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20  text(argv[0]);. 
323a6 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
323a7 63 68 61 72 20 2a 7a 42 20 3d 20 73 71 6c 69 74  char *zB = sqlit
323a8 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
323a9 67 76 5b 31 5d 29 3b 0a 20 20 55 43 68 61 72 33  gv[1]);.  UChar3
323aa 32 20 75 45 73 63 20 3d 20 30 3b 0a 0a 20 20 2f  2 uEsc = 0;..  /
323ab 2a 20 4c 69 6d 69 74 20 74 68 65 20 6c 65 6e 67  * Limit the leng
323ac 74 68 20 6f 66 20 74 68 65 20 4c 49 4b 45 20 6f  th of the LIKE o
323ad 72 20 47 4c 4f 42 20 70 61 74 74 65 72 6e 20 74  r GLOB pattern t
323ae 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d 73  o avoid problems
323af 0a 20 20 2a 2a 20 6f 66 20 64 65 65 70 20 72 65  .  ** of deep re
323b0 63 75 72 73 69 6f 6e 20 61 6e 64 20 4e 2a 4e 20  cursion and N*N 
323b1 62 65 68 61 76 69 6f 72 20 69 6e 20 70 61 74 74  behavior in patt
323b2 65 72 6e 43 6f 6d 70 61 72 65 28 29 2e 0a 20 20  ernCompare()..  
323b3 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
323b4 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
323b5 76 5b 30 5d 29 3e 53 51 4c 49 54 45 5f 4d 41 58  v[0])>SQLITE_MAX
323b6 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45  _LIKE_PATTERN_LE
323b7 4e 47 54 48 20 29 7b 0a 20 20 20 20 73 71 6c 69  NGTH ){.    sqli
323b8 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
323b9 28 63 6f 6e 74 65 78 74 2c 20 22 4c 49 4b 45 20  (context, "LIKE 
323ba 6f 72 20 47 4c 4f 42 20 70 61 74 74 65 72 6e 20  or GLOB pattern 
323bb 74 6f 6f 20 63 6f 6d 70 6c 65 78 22 2c 20 2d 31  too complex", -1
323bc 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
323bd 20 7d 0a 0a 0a 20 20 69 66 28 20 61 72 67 63 3d   }...  if( argc=
323be 3d 33 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  =3 ){.    /* The
323bf 20 65 73 63 61 70 65 20 63 68 61 72 61 63 74 65   escape characte
323c0 72 20 73 74 72 69 6e 67 20 6d 75 73 74 20 63 6f  r string must co
323c1 6e 73 69 73 74 20 6f 66 20 61 20 73 69 6e 67 6c  nsist of a singl
323c2 65 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65  e UTF-8 characte
323c3 72 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 77  r..    ** Otherw
323c4 69 73 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 65  ise, return an e
323c5 72 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  rror..    */.   
323c6 20 69 6e 74 20 6e 45 3d 20 73 71 6c 69 74 65 33   int nE= sqlite3
323c7 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
323c8 76 5b 32 5d 29 3b 0a 20 20 20 20 63 6f 6e 73 74  v[2]);.    const
323c9 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
323ca 7a 45 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  zE = sqlite3_val
323cb 75 65 5f 74 65 78 74 28 61 72 67 76 5b 32 5d 29  ue_text(argv[2])
323cc 3b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b  ;.    int i = 0;
323cd 0a 20 20 20 20 69 66 28 20 7a 45 3d 3d 30 20 29  .    if( zE==0 )
323ce 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 55 38 5f   return;.    U8_
323cf 4e 45 58 54 28 7a 45 2c 20 69 2c 20 6e 45 2c 20  NEXT(zE, i, nE, 
323d0 75 45 73 63 29 3b 0a 20 20 20 20 69 66 28 20 69  uEsc);.    if( i
323d1 21 3d 6e 45 29 7b 0a 20 20 20 20 20 20 73 71 6c  !=nE){.      sql
323d2 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
323d3 72 28 63 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20  r(context, .    
323d4 20 20 20 20 20 20 22 45 53 43 41 50 45 20 65 78        "ESCAPE ex
323d5 70 72 65 73 73 69 6f 6e 20 6d 75 73 74 20 62 65  pression must be
323d6 20 61 20 73 69 6e 67 6c 65 20 63 68 61 72 61 63   a single charac
323d7 74 65 72 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20  ter", -1);.     
323d8 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
323d9 20 7d 0a 0a 20 20 69 66 28 20 7a 41 20 26 26 20   }..  if( zA && 
323da 7a 42 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  zB ){.    sqlite
323db 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e  3_result_int(con
323dc 74 65 78 74 2c 20 69 63 75 4c 69 6b 65 43 6f 6d  text, icuLikeCom
323dd 70 61 72 65 28 7a 41 2c 20 7a 42 2c 20 75 45 73  pare(zA, zB, uEs
323de 63 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  c));.  }.}../*.*
323df 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
323e0 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61  is called when a
323e1 6e 20 49 43 55 20 66 75 6e 63 74 69 6f 6e 20 63  n ICU function c
323e2 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69  alled from withi
323e3 6e 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65  n.** the impleme
323e4 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51  ntation of an SQ
323e5 4c 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  L scalar functio
323e6 6e 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72  n returns an err
323e7 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 63  or..**.** The sc
323e8 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 63 6f  alar function co
323e9 6e 74 65 78 74 20 70 61 73 73 65 64 20 61 73 20  ntext passed as 
323ea 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
323eb 6e 74 20 69 73 20 0a 2a 2a 20 6c 6f 61 64 65 64  nt is .** loaded
323ec 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d   with an error m
323ed 65 73 73 61 67 65 20 62 61 73 65 64 20 6f 6e 20  essage based on 
323ee 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77  the following tw
323ef 6f 20 61 72 67 73 2e 0a 2a 2f 0a 73 74 61 74 69  o args..*/.stati
323f0 63 20 76 6f 69 64 20 69 63 75 46 75 6e 63 74 69  c void icuFuncti
323f1 6f 6e 45 72 72 6f 72 28 0a 20 20 73 71 6c 69 74  onError(.  sqlit
323f2 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
323f3 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74  ,       /* SQLit
323f4 65 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  e scalar functio
323f5 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63  n context */.  c
323f6 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
323f7 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ,           /* N
323f8 61 6d 65 20 6f 66 20 49 43 55 20 66 75 6e 63 74  ame of ICU funct
323f9 69 6f 6e 20 74 68 61 74 20 66 61 69 6c 65 64 20  ion that failed 
323fa 2a 2f 0a 20 20 55 45 72 72 6f 72 43 6f 64 65 20  */.  UErrorCode 
323fb 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
323fc 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20    /* Error code 
323fd 72 65 74 75 72 6e 65 64 20 62 79 20 49 43 55 20  returned by ICU 
323fe 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20  function */.){. 
323ff 20 63 68 61 72 20 7a 42 75 66 5b 31 32 38 5d 3b   char zBuf[128];
32400 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  .  sqlite3_snpri
32401 6e 74 66 28 31 32 38 2c 20 7a 42 75 66 2c 20 22  ntf(128, zBuf, "
32402 49 43 55 20 65 72 72 6f 72 3a 20 25 73 28 29 3a  ICU error: %s():
32403 20 25 73 22 2c 20 7a 4e 61 6d 65 2c 20 75 5f 65   %s", zName, u_e
32404 72 72 6f 72 4e 61 6d 65 28 65 29 29 3b 0a 20 20  rrorName(e));.  
32405 7a 42 75 66 5b 31 32 37 5d 20 3d 20 27 5c 30 27  zBuf[127] = '\0'
32406 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ;.  sqlite3_resu
32407 6c 74 5f 65 72 72 6f 72 28 70 43 74 78 2c 20 7a  lt_error(pCtx, z
32408 42 75 66 2c 20 2d 31 29 3b 0a 7d 0a 0a 2f 2a 0a  Buf, -1);.}../*.
32409 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 64  ** Function to d
3240a 65 6c 65 74 65 20 63 6f 6d 70 69 6c 65 64 20 72  elete compiled r
3240b 65 67 65 78 70 20 6f 62 6a 65 63 74 73 2e 20 52  egexp objects. R
3240c 65 67 69 73 74 65 72 65 64 20 61 73 0a 2a 2a 20  egistered as.** 
3240d 61 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e  a destructor fun
3240e 63 74 69 6f 6e 20 77 69 74 68 20 73 71 6c 69 74  ction with sqlit
3240f 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29  e3_set_auxdata()
32410 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
32411 20 69 63 75 52 65 67 65 78 70 44 65 6c 65 74 65   icuRegexpDelete
32412 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 55 52 65  (void *p){.  URe
32413 67 75 6c 61 72 45 78 70 72 65 73 73 69 6f 6e 20  gularExpression 
32414 2a 70 45 78 70 72 20 3d 20 28 55 52 65 67 75 6c  *pExpr = (URegul
32415 61 72 45 78 70 72 65 73 73 69 6f 6e 20 2a 29 70  arExpression *)p
32416 3b 0a 20 20 75 72 65 67 65 78 5f 63 6c 6f 73 65  ;.  uregex_close
32417 28 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  (pExpr);.}../*.*
32418 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
32419 20 6f 66 20 53 51 4c 69 74 65 20 52 45 47 45 58   of SQLite REGEX
3241a 50 20 6f 70 65 72 61 74 6f 72 2e 20 54 68 69 73  P operator. This
3241b 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
3241c 20 74 61 6b 65 73 0a 2a 2a 20 74 77 6f 20 61 72   takes.** two ar
3241d 67 75 6d 65 6e 74 73 2e 20 54 68 65 20 66 69 72  guments. The fir
3241e 73 74 20 69 73 20 61 20 72 65 67 75 6c 61 72 20  st is a regular 
3241f 65 78 70 72 65 73 73 69 6f 6e 20 70 61 74 74 65  expression patte
32420 72 6e 20 74 6f 20 63 6f 6d 70 69 6c 65 0a 2a 2a  rn to compile.**
32421 20 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 61   the second is a
32422 20 73 74 72 69 6e 67 20 74 6f 20 6d 61 74 63 68   string to match
32423 20 61 67 61 69 6e 73 74 20 74 68 61 74 20 70 61   against that pa
32424 74 74 65 72 6e 2e 20 49 66 20 65 69 74 68 65 72  ttern. If either
32425 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73   .** argument is
32426 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2c 20 74 68   an SQL NULL, th
32427 65 6e 20 4e 55 4c 4c 20 49 73 20 72 65 74 75 72  en NULL Is retur
32428 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ned. Otherwise, 
32429 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73  the result.** is
3242a 20 31 20 69 66 20 74 68 65 20 73 74 72 69 6e 67   1 if the string
3242b 20 6d 61 74 63 68 65 73 20 74 68 65 20 70 61 74   matches the pat
3242c 74 65 72 6e 2c 20 6f 72 20 30 20 6f 74 68 65 72  tern, or 0 other
3242d 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69  wise..**.** SQLi
3242e 74 65 20 6d 61 70 73 20 74 68 65 20 72 65 67 65  te maps the rege
3242f 78 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f  xp() function to
32430 20 74 68 65 20 72 65 67 65 78 70 28 29 20 6f 70   the regexp() op
32431 65 72 61 74 6f 72 20 73 75 63 68 0a 2a 2a 20 74  erator such.** t
32432 68 61 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  hat the followin
32433 67 20 74 77 6f 20 61 72 65 20 65 71 75 69 76 61  g two are equiva
32434 6c 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  lent:.**.**     
32435 7a 53 74 72 69 6e 67 20 52 45 47 45 58 50 20 7a  zString REGEXP z
32436 50 61 74 74 65 72 6e 0a 2a 2a 20 20 20 20 20 72  Pattern.**     r
32437 65 67 65 78 70 28 7a 50 61 74 74 65 72 6e 2c 20  egexp(zPattern, 
32438 7a 53 74 72 69 6e 67 29 0a 2a 2a 0a 2a 2a 20 55  zString).**.** U
32439 73 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ses the followin
3243a 67 20 49 43 55 20 72 65 67 65 78 70 20 41 50 49  g ICU regexp API
3243b 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 75 72 65  s:.**.**     ure
3243c 67 65 78 5f 6f 70 65 6e 28 29 0a 2a 2a 20 20 20  gex_open().**   
3243d 20 20 75 72 65 67 65 78 5f 6d 61 74 63 68 65 73    uregex_matches
3243e 28 29 0a 2a 2a 20 20 20 20 20 75 72 65 67 65 78  ().**     uregex
3243f 5f 63 6c 6f 73 65 28 29 0a 2a 2f 0a 73 74 61 74  _close().*/.stat
32440 69 63 20 76 6f 69 64 20 69 63 75 52 65 67 65 78  ic void icuRegex
32441 70 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f  pFunc(sqlite3_co
32442 6e 74 65 78 74 20 2a 70 2c 20 69 6e 74 20 6e 41  ntext *p, int nA
32443 72 67 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  rg, sqlite3_valu
32444 65 20 2a 2a 61 70 41 72 67 29 7b 0a 20 20 55 45  e **apArg){.  UE
32445 72 72 6f 72 43 6f 64 65 20 73 74 61 74 75 73 20  rrorCode status 
32446 3d 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b 0a  = U_ZERO_ERROR;.
32447 20 20 55 52 65 67 75 6c 61 72 45 78 70 72 65 73    URegularExpres
32448 73 69 6f 6e 20 2a 70 45 78 70 72 3b 0a 20 20 55  sion *pExpr;.  U
32449 42 6f 6f 6c 20 72 65 73 3b 0a 20 20 63 6f 6e 73  Bool res;.  cons
3244a 74 20 55 43 68 61 72 20 2a 7a 53 74 72 69 6e 67  t UChar *zString
3244b 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
3244c 5f 74 65 78 74 31 36 28 61 70 41 72 67 5b 31 5d  _text16(apArg[1]
3244d 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
3244e 6c 65 66 74 20 68 61 6e 64 20 73 69 64 65 20 6f  left hand side o
3244f 66 20 74 68 65 20 72 65 67 65 78 70 20 6f 70 65  f the regexp ope
32450 72 61 74 6f 72 20 69 73 20 4e 55 4c 4c 2c 20 0a  rator is NULL, .
32451 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 72 65    ** then the re
32452 73 75 6c 74 20 69 73 20 61 6c 73 6f 20 4e 55 4c  sult is also NUL
32453 4c 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  L. .  */.  if( !
32454 7a 53 74 72 69 6e 67 20 29 7b 0a 20 20 20 20 72  zString ){.    r
32455 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 70 45  eturn;.  }..  pE
32456 78 70 72 20 3d 20 73 71 6c 69 74 65 33 5f 67 65  xpr = sqlite3_ge
32457 74 5f 61 75 78 64 61 74 61 28 70 2c 20 30 29 3b  t_auxdata(p, 0);
32458 0a 20 20 69 66 28 20 21 70 45 78 70 72 20 29 7b  .  if( !pExpr ){
32459 0a 20 20 20 20 63 6f 6e 73 74 20 55 43 68 61 72  .    const UChar
3245a 20 2a 7a 50 61 74 74 65 72 6e 20 3d 20 73 71 6c   *zPattern = sql
3245b 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
3245c 36 28 61 70 41 72 67 5b 30 5d 29 3b 0a 20 20 20  6(apArg[0]);.   
3245d 20 69 66 28 20 21 7a 50 61 74 74 65 72 6e 20 29   if( !zPattern )
3245e 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  {.      return;.
3245f 20 20 20 20 7d 0a 20 20 20 20 70 45 78 70 72 20      }.    pExpr 
32460 3d 20 75 72 65 67 65 78 5f 6f 70 65 6e 28 7a 50  = uregex_open(zP
32461 61 74 74 65 72 6e 2c 20 2d 31 2c 20 30 2c 20 30  attern, -1, 0, 0
32462 2c 20 26 73 74 61 74 75 73 29 3b 0a 0a 20 20 20  , &status);..   
32463 20 69 66 28 20 55 5f 53 55 43 43 45 53 53 28 73   if( U_SUCCESS(s
32464 74 61 74 75 73 29 20 29 7b 0a 20 20 20 20 20 20  tatus) ){.      
32465 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64  sqlite3_set_auxd
32466 61 74 61 28 70 2c 20 30 2c 20 70 45 78 70 72 2c  ata(p, 0, pExpr,
32467 20 69 63 75 52 65 67 65 78 70 44 65 6c 65 74 65   icuRegexpDelete
32468 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
32469 20 20 20 20 61 73 73 65 72 74 28 21 70 45 78 70      assert(!pExp
3246a 72 29 3b 0a 20 20 20 20 20 20 69 63 75 46 75 6e  r);.      icuFun
3246b 63 74 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75  ctionError(p, "u
3246c 72 65 67 65 78 5f 6f 70 65 6e 22 2c 20 73 74 61  regex_open", sta
3246d 74 75 73 29 3b 0a 20 20 20 20 20 20 72 65 74 75  tus);.      retu
3246e 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  rn;.    }.  }.. 
3246f 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68   /* Configure th
32470 65 20 74 65 78 74 20 74 68 61 74 20 74 68 65 20  e text that the 
32471 72 65 67 75 6c 61 72 20 65 78 70 72 65 73 73 69  regular expressi
32472 6f 6e 20 6f 70 65 72 61 74 65 73 20 6f 6e 2e 20  on operates on. 
32473 2a 2f 0a 20 20 75 72 65 67 65 78 5f 73 65 74 54  */.  uregex_setT
32474 65 78 74 28 70 45 78 70 72 2c 20 7a 53 74 72 69  ext(pExpr, zStri
32475 6e 67 2c 20 2d 31 2c 20 26 73 74 61 74 75 73 29  ng, -1, &status)
32476 3b 0a 20 20 69 66 28 20 21 55 5f 53 55 43 43 45  ;.  if( !U_SUCCE
32477 53 53 28 73 74 61 74 75 73 29 20 29 7b 0a 20 20  SS(status) ){.  
32478 20 20 69 63 75 46 75 6e 63 74 69 6f 6e 45 72 72    icuFunctionErr
32479 6f 72 28 70 2c 20 22 75 72 65 67 65 78 5f 73 65  or(p, "uregex_se
3247a 74 54 65 78 74 22 2c 20 73 74 61 74 75 73 29 3b  tText", status);
3247b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
3247c 0a 0a 20 20 2f 2a 20 41 74 74 65 6d 70 74 20 74  ..  /* Attempt t
3247d 68 65 20 6d 61 74 63 68 20 2a 2f 0a 20 20 72 65  he match */.  re
3247e 73 20 3d 20 75 72 65 67 65 78 5f 6d 61 74 63 68  s = uregex_match
3247f 65 73 28 70 45 78 70 72 2c 20 30 2c 20 26 73 74  es(pExpr, 0, &st
32480 61 74 75 73 29 3b 0a 20 20 69 66 28 20 21 55 5f  atus);.  if( !U_
32481 53 55 43 43 45 53 53 28 73 74 61 74 75 73 29 20  SUCCESS(status) 
32482 29 7b 0a 20 20 20 20 69 63 75 46 75 6e 63 74 69  ){.    icuFuncti
32483 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 72 65 67  onError(p, "ureg
32484 65 78 5f 6d 61 74 63 68 65 73 22 2c 20 73 74 61  ex_matches", sta
32485 74 75 73 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  tus);.    return
32486 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20  ;.  }..  /* Set 
32487 74 68 65 20 74 65 78 74 20 74 68 61 74 20 74 68  the text that th
32488 65 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 73  e regular expres
32489 73 69 6f 6e 20 6f 70 65 72 61 74 65 73 20 6f 6e  sion operates on
3248a 20 74 6f 20 61 20 4e 55 4c 4c 0a 20 20 2a 2a 20   to a NULL.  ** 
3248b 70 6f 69 6e 74 65 72 2e 20 54 68 69 73 20 69 73  pointer. This is
3248c 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6e 65 63 65   not really nece
3248d 73 73 61 72 79 2c 20 62 75 74 20 69 74 20 69 73  ssary, but it is
3248e 20 74 69 64 69 65 72 20 74 68 61 6e 20 0a 20 20   tidier than .  
3248f 2a 2a 20 6c 65 61 76 69 6e 67 20 74 68 65 20 72  ** leaving the r
32490 65 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f  egular expressio
32491 6e 20 6f 62 6a 65 63 74 20 63 6f 6e 66 69 67 75  n object configu
32492 72 65 64 20 77 69 74 68 20 61 6e 20 69 6e 76 61  red with an inva
32493 6c 69 64 0a 20 20 2a 2a 20 70 6f 69 6e 74 65 72  lid.  ** pointer
32494 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63   after this func
32495 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0a 20 20  tion returns..  
32496 2a 2f 0a 20 20 75 72 65 67 65 78 5f 73 65 74 54  */.  uregex_setT
32497 65 78 74 28 70 45 78 70 72 2c 20 30 2c 20 30 2c  ext(pExpr, 0, 0,
32498 20 26 73 74 61 74 75 73 29 3b 0a 0a 20 20 2f 2a   &status);..  /*
32499 20 52 65 74 75 72 6e 20 31 20 6f 72 20 30 2e 20   Return 1 or 0. 
3249a 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  */.  sqlite3_res
3249b 75 6c 74 5f 69 6e 74 28 70 2c 20 72 65 73 20 3f  ult_int(p, res ?
3249c 20 31 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a   1 : 0);.}../*.*
3249d 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
3249e 73 20 6f 66 20 73 63 61 6c 61 72 20 66 75 6e 63  s of scalar func
3249f 74 69 6f 6e 73 20 66 6f 72 20 63 61 73 65 20 6d  tions for case m
324a0 61 70 70 69 6e 67 20 2d 20 75 70 70 65 72 28 29  apping - upper()
324a1 20 61 6e 64 20 0a 2a 2a 20 6c 6f 77 65 72 28 29   and .** lower()
324a2 2e 20 46 75 6e 63 74 69 6f 6e 20 75 70 70 65 72  . Function upper
324a3 28 29 20 63 6f 6e 76 65 72 74 73 20 69 74 73 20  () converts its 
324a4 69 6e 70 75 74 20 74 6f 20 75 70 70 65 72 2d 63  input to upper-c
324a5 61 73 65 20 28 41 42 43 29 2e 0a 2a 2a 20 46 75  ase (ABC)..** Fu
324a6 6e 63 74 69 6f 6e 20 6c 6f 77 65 72 28 29 20 63  nction lower() c
324a7 6f 6e 76 65 72 74 73 20 74 6f 20 6c 6f 77 65 72  onverts to lower
324a8 2d 63 61 73 65 20 28 61 62 63 29 2e 0a 2a 2a 0a  -case (abc)..**.
324a9 2a 2a 20 49 43 55 20 70 72 6f 76 69 64 65 73 20  ** ICU provides 
324aa 74 77 6f 20 74 79 70 65 73 20 6f 66 20 63 61 73  two types of cas
324ab 65 20 6d 61 70 70 69 6e 67 2c 20 22 67 65 6e 65  e mapping, "gene
324ac 72 61 6c 22 20 63 61 73 65 20 6d 61 70 70 69 6e  ral" case mappin
324ad 67 20 61 6e 64 0a 2a 2a 20 22 6c 61 6e 67 75 61  g and.** "langua
324ae 67 65 20 73 70 65 63 69 66 69 63 22 2e 20 52 65  ge specific". Re
324af 66 65 72 20 74 6f 20 49 43 55 20 64 6f 63 75 6d  fer to ICU docum
324b0 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 74 68 65  entation for the
324b1 20 64 69 66 66 65 72 65 6e 63 65 73 0a 2a 2a 20   differences.** 
324b2 62 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f 2e  between the two.
324b3 0a 2a 2a 0a 2a 2a 20 54 6f 20 75 74 69 6c 69 73  .**.** To utilis
324b4 65 20 22 67 65 6e 65 72 61 6c 22 20 63 61 73 65  e "general" case
324b5 20 6d 61 70 70 69 6e 67 2c 20 74 68 65 20 75 70   mapping, the up
324b6 70 65 72 28 29 20 6f 72 20 6c 6f 77 65 72 28 29  per() or lower()
324b7 20 73 63 61 6c 61 72 20 0a 2a 2a 20 66 75 6e 63   scalar .** func
324b8 74 69 6f 6e 73 20 61 72 65 20 69 6e 76 6f 6b 65  tions are invoke
324b9 64 20 77 69 74 68 20 6f 6e 65 20 61 72 67 75 6d  d with one argum
324ba 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 75  ent:.**.**     u
324bb 70 70 65 72 28 27 41 42 43 27 29 20 2d 3e 20 27  pper('ABC') -> '
324bc 61 62 63 27 0a 2a 2a 20 20 20 20 20 6c 6f 77 65  abc'.**     lowe
324bd 72 28 27 61 62 63 27 29 20 2d 3e 20 27 41 42 43  r('abc') -> 'ABC
324be 27 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 63 63 65 73  '.**.** To acces
324bf 73 20 49 43 55 20 22 6c 61 6e 67 75 61 67 65 20  s ICU "language 
324c0 73 70 65 63 69 66 69 63 22 20 63 61 73 65 20 6d  specific" case m
324c1 61 70 70 69 6e 67 2c 20 75 70 70 65 72 28 29 20  apping, upper() 
324c2 6f 72 20 6c 6f 77 65 72 28 29 0a 2a 2a 20 73 68  or lower().** sh
324c3 6f 75 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 20  ould be invoked 
324c4 77 69 74 68 20 74 77 6f 20 61 72 67 75 6d 65 6e  with two argumen
324c5 74 73 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 61  ts. The second a
324c6 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e  rgument is the n
324c7 61 6d 65 0a 2a 2a 20 6f 66 20 74 68 65 20 6c 6f  ame.** of the lo
324c8 63 61 6c 65 20 74 6f 20 75 73 65 2e 20 50 61 73  cale to use. Pas
324c9 73 69 6e 67 20 61 6e 20 65 6d 70 74 79 20 73 74  sing an empty st
324ca 72 69 6e 67 20 28 22 22 29 20 6f 72 20 53 51 4c  ring ("") or SQL
324cb 20 4e 55 4c 4c 20 76 61 6c 75 65 0a 2a 2a 20 61   NULL value.** a
324cc 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  s the second arg
324cd 75 6d 65 6e 74 20 69 73 20 74 68 65 20 73 61 6d  ument is the sam
324ce 65 20 61 73 20 69 6e 76 6f 6b 69 6e 67 20 74 68  e as invoking th
324cf 65 20 31 20 61 72 67 75 6d 65 6e 74 20 76 65 72  e 1 argument ver
324d0 73 69 6f 6e 0a 2a 2a 20 6f 66 20 75 70 70 65 72  sion.** of upper
324d1 28 29 20 6f 72 20 6c 6f 77 65 72 28 29 2e 0a 2a  () or lower()..*
324d2 2a 0a 2a 2a 20 20 20 20 20 6c 6f 77 65 72 28 27  *.**     lower('
324d3 49 27 2c 20 27 65 6e 5f 75 73 27 29 20 2d 3e 20  I', 'en_us') -> 
324d4 27 69 27 0a 2a 2a 20 20 20 20 20 6c 6f 77 65 72  'i'.**     lower
324d5 28 27 49 27 2c 20 27 74 72 5f 74 72 27 29 20 2d  ('I', 'tr_tr') -
324d6 3e 20 27 c4 b1 27 20 28 73 6d 61 6c 6c 20 64 6f  > '..' (small do
324d7 74 6c 65 73 73 20 69 29 0a 2a 2a 0a 2a 2a 20 68  tless i).**.** h
324d8 74 74 70 3a 2f 2f 77 77 77 2e 69 63 75 2d 70 72  ttp://www.icu-pr
324d9 6f 6a 65 63 74 2e 6f 72 67 2f 75 73 65 72 67 75  oject.org/usergu
324da 69 64 65 2f 70 6f 73 69 78 2e 68 74 6d 6c 23 63  ide/posix.html#c
324db 61 73 65 5f 6d 61 70 70 69 6e 67 73 0a 2a 2f 0a  ase_mappings.*/.
324dc 73 74 61 74 69 63 20 76 6f 69 64 20 69 63 75 43  static void icuC
324dd 61 73 65 46 75 6e 63 31 36 28 73 71 6c 69 74 65  aseFunc16(sqlite
324de 33 5f 63 6f 6e 74 65 78 74 20 2a 70 2c 20 69 6e  3_context *p, in
324df 74 20 6e 41 72 67 2c 20 73 71 6c 69 74 65 33 5f  t nArg, sqlite3_
324e0 76 61 6c 75 65 20 2a 2a 61 70 41 72 67 29 7b 0a  value **apArg){.
324e1 20 20 63 6f 6e 73 74 20 55 43 68 61 72 20 2a 7a    const UChar *z
324e2 49 6e 70 75 74 3b 0a 20 20 55 43 68 61 72 20 2a  Input;.  UChar *
324e3 7a 4f 75 74 70 75 74 3b 0a 20 20 69 6e 74 20 6e  zOutput;.  int n
324e4 49 6e 70 75 74 3b 0a 20 20 69 6e 74 20 6e 4f 75  Input;.  int nOu
324e5 74 70 75 74 3b 0a 0a 20 20 55 45 72 72 6f 72 43  tput;..  UErrorC
324e6 6f 64 65 20 73 74 61 74 75 73 20 3d 20 55 5f 5a  ode status = U_Z
324e7 45 52 4f 5f 45 52 52 4f 52 3b 0a 20 20 63 6f 6e  ERO_ERROR;.  con
324e8 73 74 20 63 68 61 72 20 2a 7a 4c 6f 63 61 6c 65  st char *zLocale
324e9 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28   = 0;..  assert(
324ea 6e 41 72 67 3d 3d 31 20 7c 7c 20 6e 41 72 67 3d  nArg==1 || nArg=
324eb 3d 32 29 3b 0a 20 20 69 66 28 20 6e 41 72 67 3d  =2);.  if( nArg=
324ec 3d 32 20 29 7b 0a 20 20 20 20 7a 4c 6f 63 61 6c  =2 ){.    zLocal
324ed 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  e = (const char 
324ee 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
324ef 74 65 78 74 28 61 70 41 72 67 5b 31 5d 29 3b 0a  text(apArg[1]);.
324f0 20 20 7d 0a 0a 20 20 7a 49 6e 70 75 74 20 3d 20    }..  zInput = 
324f1 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
324f2 78 74 31 36 28 61 70 41 72 67 5b 30 5d 29 3b 0a  xt16(apArg[0]);.
324f3 20 20 69 66 28 20 21 7a 49 6e 70 75 74 20 29 7b    if( !zInput ){
324f4 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
324f5 0a 20 20 6e 49 6e 70 75 74 20 3d 20 73 71 6c 69  .  nInput = sqli
324f6 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 31  te3_value_bytes1
324f7 36 28 61 70 41 72 67 5b 30 5d 29 3b 0a 0a 20 20  6(apArg[0]);..  
324f8 6e 4f 75 74 70 75 74 20 3d 20 6e 49 6e 70 75 74  nOutput = nInput
324f9 20 2a 20 32 20 2b 20 32 3b 0a 20 20 7a 4f 75 74   * 2 + 2;.  zOut
324fa 70 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  put = sqlite3_ma
324fb 6c 6c 6f 63 28 6e 4f 75 74 70 75 74 29 3b 0a 20  lloc(nOutput);. 
324fc 20 69 66 28 20 21 7a 4f 75 74 70 75 74 20 29 7b   if( !zOutput ){
324fd 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
324fe 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  ..  if( sqlite3_
324ff 75 73 65 72 5f 64 61 74 61 28 70 29 20 29 7b 0a  user_data(p) ){.
32500 20 20 20 20 75 5f 73 74 72 54 6f 55 70 70 65 72      u_strToUpper
32501 28 7a 4f 75 74 70 75 74 2c 20 6e 4f 75 74 70 75  (zOutput, nOutpu
32502 74 2f 32 2c 20 7a 49 6e 70 75 74 2c 20 6e 49 6e  t/2, zInput, nIn
32503 70 75 74 2f 32 2c 20 7a 4c 6f 63 61 6c 65 2c 20  put/2, zLocale, 
32504 26 73 74 61 74 75 73 29 3b 0a 20 20 7d 65 6c 73  &status);.  }els
32505 65 7b 0a 20 20 20 20 75 5f 73 74 72 54 6f 4c 6f  e{.    u_strToLo
32506 77 65 72 28 7a 4f 75 74 70 75 74 2c 20 6e 4f 75  wer(zOutput, nOu
32507 74 70 75 74 2f 32 2c 20 7a 49 6e 70 75 74 2c 20  tput/2, zInput, 
32508 6e 49 6e 70 75 74 2f 32 2c 20 7a 4c 6f 63 61 6c  nInput/2, zLocal
32509 65 2c 20 26 73 74 61 74 75 73 29 3b 0a 20 20 7d  e, &status);.  }
3250a 0a 0a 20 20 69 66 28 20 21 55 5f 53 55 43 43 45  ..  if( !U_SUCCE
3250b 53 53 28 73 74 61 74 75 73 29 20 29 7b 0a 20 20  SS(status) ){.  
3250c 20 20 69 63 75 46 75 6e 63 74 69 6f 6e 45 72 72    icuFunctionErr
3250d 6f 72 28 70 2c 20 22 75 5f 73 74 72 54 6f 4c 6f  or(p, "u_strToLo
3250e 77 65 72 28 29 2f 75 5f 73 74 72 54 6f 55 70 70  wer()/u_strToUpp
3250f 65 72 22 2c 20 73 74 61 74 75 73 29 3b 0a 20 20  er", status);.  
32510 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
32511 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
32512 74 65 78 74 31 36 28 70 2c 20 7a 4f 75 74 70 75  text16(p, zOutpu
32513 74 2c 20 2d 31 2c 20 78 46 72 65 65 29 3b 0a 7d  t, -1, xFree);.}
32514 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6c 6c 61 74 69 6f  ../*.** Collatio
32515 6e 20 73 65 71 75 65 6e 63 65 20 64 65 73 74 72  n sequence destr
32516 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 2e 20  uctor function. 
32517 54 68 65 20 70 43 74 78 20 61 72 67 75 6d 65 6e  The pCtx argumen
32518 74 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 61  t points to.** a
32519 20 55 43 6f 6c 6c 61 74 6f 72 20 73 74 72 75 63   UCollator struc
3251a 74 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20  ture previously 
3251b 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20  allocated using 
3251c 75 63 6f 6c 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a  ucol_open()..*/.
3251d 73 74 61 74 69 63 20 76 6f 69 64 20 69 63 75 43  static void icuC
3251e 6f 6c 6c 61 74 69 6f 6e 44 65 6c 28 76 6f 69 64  ollationDel(void
3251f 20 2a 70 43 74 78 29 7b 0a 20 20 55 43 6f 6c 6c   *pCtx){.  UColl
32520 61 74 6f 72 20 2a 70 20 3d 20 28 55 43 6f 6c 6c  ator *p = (UColl
32521 61 74 6f 72 20 2a 29 70 43 74 78 3b 0a 20 20 75  ator *)pCtx;.  u
32522 63 6f 6c 5f 63 6c 6f 73 65 28 70 29 3b 0a 7d 0a  col_close(p);.}.
32523 0a 2f 2a 0a 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e  ./*.** Collation
32524 20 73 65 71 75 65 6e 63 65 20 63 6f 6d 70 61 72   sequence compar
32525 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 2e 20 54  ison function. T
32526 68 65 20 70 43 74 78 20 61 72 67 75 6d 65 6e 74  he pCtx argument
32527 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 61 20   points to.** a 
32528 55 43 6f 6c 6c 61 74 6f 72 20 73 74 72 75 63 74  UCollator struct
32529 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61  ure previously a
3252a 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20 75  llocated using u
3252b 63 6f 6c 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a 73  col_open()..*/.s
3252c 74 61 74 69 63 20 69 6e 74 20 69 63 75 43 6f 6c  tatic int icuCol
3252d 6c 61 74 69 6f 6e 43 6f 6c 6c 28 0a 20 20 76 6f  lationColl(.  vo
3252e 69 64 20 2a 70 43 74 78 2c 0a 20 20 69 6e 74 20  id *pCtx,.  int 
3252f 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e 73 74 20 76  nLeft,.  const v
32530 6f 69 64 20 2a 7a 4c 65 66 74 2c 0a 20 20 69 6e  oid *zLeft,.  in
32531 74 20 6e 52 69 67 68 74 2c 0a 20 20 63 6f 6e 73  t nRight,.  cons
32532 74 20 76 6f 69 64 20 2a 7a 52 69 67 68 74 0a 29  t void *zRight.)
32533 7b 0a 20 20 55 43 6f 6c 6c 61 74 69 6f 6e 52 65  {.  UCollationRe
32534 73 75 6c 74 20 72 65 73 3b 0a 20 20 55 43 6f 6c  sult res;.  UCol
32535 6c 61 74 6f 72 20 2a 70 20 3d 20 28 55 43 6f 6c  lator *p = (UCol
32536 6c 61 74 6f 72 20 2a 29 70 43 74 78 3b 0a 20 20  lator *)pCtx;.  
32537 72 65 73 20 3d 20 75 63 6f 6c 5f 73 74 72 63 6f  res = ucol_strco
32538 6c 6c 28 70 2c 20 28 55 43 68 61 72 20 2a 29 7a  ll(p, (UChar *)z
32539 4c 65 66 74 2c 20 6e 4c 65 66 74 2f 32 2c 20 28  Left, nLeft/2, (
3253a 55 43 68 61 72 20 2a 29 7a 52 69 67 68 74 2c 20  UChar *)zRight, 
3253b 6e 52 69 67 68 74 2f 32 29 3b 0a 20 20 73 77 69  nRight/2);.  swi
3253c 74 63 68 28 20 72 65 73 20 29 7b 0a 20 20 20 20  tch( res ){.    
3253d 63 61 73 65 20 55 43 4f 4c 5f 4c 45 53 53 3a 20  case UCOL_LESS: 
3253e 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20     return -1;.  
3253f 20 20 63 61 73 65 20 55 43 4f 4c 5f 47 52 45 41    case UCOL_GREA
32540 54 45 52 3a 20 72 65 74 75 72 6e 20 2b 31 3b 0a  TER: return +1;.
32541 20 20 20 20 63 61 73 65 20 55 43 4f 4c 5f 45 51      case UCOL_EQ
32542 55 41 4c 3a 20 20 20 72 65 74 75 72 6e 20 30 3b  UAL:   return 0;
32543 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 21 22  .  }.  assert(!"
32544 55 6e 65 78 70 65 63 74 65 64 20 72 65 74 75 72  Unexpected retur
32545 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 75 63 6f  n value from uco
32546 6c 5f 73 74 72 63 6f 6c 6c 28 29 22 29 3b 0a 20  l_strcoll()");. 
32547 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
32548 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
32549 6f 6e 20 6f 66 20 74 68 65 20 73 63 61 6c 61 72  on of the scalar
3254a 20 66 75 6e 63 74 69 6f 6e 20 69 63 75 5f 6c 6f   function icu_lo
3254b 61 64 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 2e 0a  ad_collation()..
3254c 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 63 61 6c 61  **.** This scala
3254d 72 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  r function is us
3254e 65 64 20 74 6f 20 61 64 64 20 49 43 55 20 63 6f  ed to add ICU co
3254f 6c 6c 61 74 69 6f 6e 20 62 61 73 65 64 20 63 6f  llation based co
32550 6c 6c 61 74 69 6f 6e 20 0a 2a 2a 20 74 79 70 65  llation .** type
32551 73 20 74 6f 20 61 6e 20 53 51 4c 69 74 65 20 64  s to an SQLite d
32552 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
32553 6f 6e 2e 20 49 74 20 69 73 20 69 6e 74 65 6e 64  on. It is intend
32554 65 64 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 0a  ed to be called.
32555 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  ** as follows:.*
32556 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20  *.**     SELECT 
32557 69 63 75 5f 6c 6f 61 64 5f 63 6f 6c 6c 61 74 69  icu_load_collati
32558 6f 6e 28 3c 6c 6f 63 61 6c 65 3e 2c 20 3c 63 6f  on(<locale>, <co
32559 6c 6c 61 74 69 6f 6e 2d 6e 61 6d 65 3e 29 3b 0a  llation-name>);.
3255a 2a 2a 0a 2a 2a 20 57 68 65 72 65 20 3c 6c 6f 63  **.** Where <loc
3255b 61 6c 65 3e 20 69 73 20 61 20 73 74 72 69 6e 67  ale> is a string
3255c 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6e 20 49   containing an I
3255d 43 55 20 6c 6f 63 61 6c 65 20 69 64 65 6e 74 69  CU locale identi
3255e 66 69 65 72 20 28 69 2e 65 2e 0a 2a 2a 20 22 65  fier (i.e..** "e
3255f 6e 5f 41 55 22 2c 20 22 74 72 5f 54 52 22 20 65  n_AU", "tr_TR" e
32560 74 63 2e 29 20 61 6e 64 20 3c 63 6f 6c 6c 61 74  tc.) and <collat
32561 69 6f 6e 2d 6e 61 6d 65 3e 20 69 73 20 74 68 65  ion-name> is the
32562 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20   name of the.** 
32563 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
32564 63 65 20 74 6f 20 63 72 65 61 74 65 2e 0a 2a 2f  ce to create..*/
32565 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 63 75  .static void icu
32566 4c 6f 61 64 43 6f 6c 6c 61 74 69 6f 6e 28 0a 20  LoadCollation(. 
32567 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
32568 20 2a 70 2c 20 0a 20 20 69 6e 74 20 6e 41 72 67   *p, .  int nArg
32569 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  , .  sqlite3_val
3256a 75 65 20 2a 2a 61 70 41 72 67 0a 29 7b 0a 20 20  ue **apArg.){.  
3256b 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 28 73  sqlite3 *db = (s
3256c 71 6c 69 74 65 33 20 2a 29 73 71 6c 69 74 65 33  qlite3 *)sqlite3
3256d 5f 75 73 65 72 5f 64 61 74 61 28 70 29 3b 0a 20  _user_data(p);. 
3256e 20 55 45 72 72 6f 72 43 6f 64 65 20 73 74 61 74   UErrorCode stat
3256f 75 73 20 3d 20 55 5f 5a 45 52 4f 5f 45 52 52 4f  us = U_ZERO_ERRO
32570 52 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  R;.  const char 
32571 2a 7a 4c 6f 63 61 6c 65 3b 20 20 20 20 20 20 2f  *zLocale;      /
32572 2a 20 4c 6f 63 61 6c 65 20 69 64 65 6e 74 69 66  * Locale identif
32573 69 65 72 20 2d 20 28 65 67 2e 20 22 6a 70 5f 4a  ier - (eg. "jp_J
32574 50 22 29 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  P") */.  const c
32575 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
32576 20 20 20 2f 2a 20 53 51 4c 20 43 6f 6c 6c 61 74     /* SQL Collat
32577 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d  ion sequence nam
32578 65 20 28 65 67 2e 20 22 6a 61 70 61 6e 65 73 65  e (eg. "japanese
32579 22 29 20 2a 2f 0a 20 20 55 43 6f 6c 6c 61 74 6f  ") */.  UCollato
3257a 72 20 2a 70 55 43 6f 6c 6c 61 74 6f 72 3b 20 20  r *pUCollator;  
3257b 20 20 2f 2a 20 49 43 55 20 6c 69 62 72 61 72 79    /* ICU library
3257c 20 63 6f 6c 6c 61 74 69 6f 6e 20 6f 62 6a 65 63   collation objec
3257d 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  t */.  int rc;  
3257e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3257f 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
32580 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 72 65  from sqlite3_cre
32581 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 78 28  ate_collation_x(
32582 29 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 6e  ) */..  assert(n
32583 41 72 67 3d 3d 32 29 3b 0a 20 20 7a 4c 6f 63 61  Arg==2);.  zLoca
32584 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  le = (const char
32585 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
32586 5f 74 65 78 74 28 61 70 41 72 67 5b 30 5d 29 3b  _text(apArg[0]);
32587 0a 20 20 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73  .  zName = (cons
32588 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
32589 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 70 41 72  _value_text(apAr
3258a 67 5b 31 5d 29 3b 0a 0a 20 20 69 66 28 20 21 7a  g[1]);..  if( !z
3258b 4c 6f 63 61 6c 65 20 7c 7c 20 21 7a 4e 61 6d 65  Locale || !zName
3258c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
3258d 20 20 7d 0a 0a 20 20 70 55 43 6f 6c 6c 61 74 6f    }..  pUCollato
3258e 72 20 3d 20 75 63 6f 6c 5f 6f 70 65 6e 28 7a 4c  r = ucol_open(zL
3258f 6f 63 61 6c 65 2c 20 26 73 74 61 74 75 73 29 3b  ocale, &status);
32590 0a 20 20 69 66 28 20 21 55 5f 53 55 43 43 45 53  .  if( !U_SUCCES
32591 53 28 73 74 61 74 75 73 29 20 29 7b 0a 20 20 20  S(status) ){.   
32592 20 69 63 75 46 75 6e 63 74 69 6f 6e 45 72 72 6f   icuFunctionErro
32593 72 28 70 2c 20 22 75 63 6f 6c 5f 6f 70 65 6e 22  r(p, "ucol_open"
32594 2c 20 73 74 61 74 75 73 29 3b 0a 20 20 20 20 72  , status);.    r
32595 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73  eturn;.  }.  ass
32596 65 72 74 28 70 29 3b 0a 0a 20 20 72 63 20 3d 20  ert(p);..  rc = 
32597 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
32598 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 64 62 2c 20  ollation_v2(db, 
32599 7a 4e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 55 54  zName, SQLITE_UT
3259a 46 31 36 2c 20 28 76 6f 69 64 20 2a 29 70 55 43  F16, (void *)pUC
3259b 6f 6c 6c 61 74 6f 72 2c 20 0a 20 20 20 20 20 20  ollator, .      
3259c 69 63 75 43 6f 6c 6c 61 74 69 6f 6e 43 6f 6c 6c  icuCollationColl
3259d 2c 20 69 63 75 43 6f 6c 6c 61 74 69 6f 6e 44 65  , icuCollationDe
3259e 6c 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63 21  l.  );.  if( rc!
3259f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
325a0 20 20 75 63 6f 6c 5f 63 6c 6f 73 65 28 70 55 43    ucol_close(pUC
325a1 6f 6c 6c 61 74 6f 72 29 3b 0a 20 20 20 20 73 71  ollator);.    sq
325a2 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
325a3 6f 72 28 70 2c 20 22 45 72 72 6f 72 20 72 65 67  or(p, "Error reg
325a4 69 73 74 65 72 69 6e 67 20 63 6f 6c 6c 61 74 69  istering collati
325a5 6f 6e 20 66 75 6e 63 74 69 6f 6e 22 2c 20 2d 31  on function", -1
325a6 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
325a7 52 65 67 69 73 74 65 72 20 74 68 65 20 49 43 55  Register the ICU
325a8 20 65 78 74 65 6e 73 69 6f 6e 20 66 75 6e 63 74   extension funct
325a9 69 6f 6e 73 20 77 69 74 68 20 64 61 74 61 62 61  ions with databa
325aa 73 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45  se db..*/.SQLITE
325ab 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
325ac 69 74 65 33 49 63 75 49 6e 69 74 28 73 71 6c 69  ite3IcuInit(sqli
325ad 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 74 72 75  te3 *db){.  stru
325ae 63 74 20 49 63 75 53 63 61 6c 61 72 20 7b 0a 20  ct IcuScalar {. 
325af 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
325b0 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  Name;           
325b1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
325b2 46 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f  Function name */
325b3 0a 20 20 20 20 69 6e 74 20 6e 41 72 67 3b 20 20  .    int nArg;  
325b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
325b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
325b6 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  * Number of argu
325b7 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 69 6e 74  ments */.    int
325b8 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20   enc;           
325b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
325ba 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6d 61         /* Optima
325bb 6c 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20  l text encoding 
325bc 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 43 6f  */.    void *pCo
325bd 6e 74 65 78 74 3b 20 20 20 20 20 20 20 20 20 20  ntext;          
325be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
325bf 20 2f 2a 20 73 71 6c 69 74 65 33 5f 75 73 65 72   /* sqlite3_user
325c0 5f 64 61 74 61 28 29 20 63 6f 6e 74 65 78 74 20  _data() context 
325c1 2a 2f 0a 20 20 20 20 76 6f 69 64 20 28 2a 78 46  */.    void (*xF
325c2 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  unc)(sqlite3_con
325c3 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
325c4 33 5f 76 61 6c 75 65 2a 2a 29 3b 0a 20 20 7d 20  3_value**);.  } 
325c5 73 63 61 6c 61 72 73 5b 5d 20 3d 20 7b 0a 20 20  scalars[] = {.  
325c6 20 20 7b 22 72 65 67 65 78 70 22 2c 2d 31 2c 20    {"regexp",-1, 
325c7 53 51 4c 49 54 45 5f 41 4e 59 2c 20 20 20 20 20  SQLITE_ANY,     
325c8 20 20 20 20 20 30 2c 20 69 63 75 52 65 67 65 78       0, icuRegex
325c9 70 46 75 6e 63 7d 2c 0a 0a 20 20 20 20 7b 22 6c  pFunc},..    {"l
325ca 6f 77 65 72 22 2c 20 20 31 2c 20 53 51 4c 49 54  ower",  1, SQLIT
325cb 45 5f 55 54 46 31 36 2c 20 20 20 20 20 20 20 20  E_UTF16,        
325cc 30 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36  0, icuCaseFunc16
325cd 7d 2c 0a 20 20 20 20 7b 22 6c 6f 77 65 72 22 2c  },.    {"lower",
325ce 20 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 31    2, SQLITE_UTF1
325cf 36 2c 20 20 20 20 20 20 20 20 30 2c 20 69 63 75  6,        0, icu
325d0 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20  CaseFunc16},.   
325d1 20 7b 22 75 70 70 65 72 22 2c 20 20 31 2c 20 53   {"upper",  1, S
325d2 51 4c 49 54 45 5f 55 54 46 31 36 2c 20 28 76 6f  QLITE_UTF16, (vo
325d3 69 64 2a 29 31 2c 20 69 63 75 43 61 73 65 46 75  id*)1, icuCaseFu
325d4 6e 63 31 36 7d 2c 0a 20 20 20 20 7b 22 75 70 70  nc16},.    {"upp
325d5 65 72 22 2c 20 20 32 2c 20 53 51 4c 49 54 45 5f  er",  2, SQLITE_
325d6 55 54 46 31 36 2c 20 28 76 6f 69 64 2a 29 31 2c  UTF16, (void*)1,
325d7 20 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d 2c   icuCaseFunc16},
325d8 0a 0a 20 20 20 20 7b 22 6c 6f 77 65 72 22 2c 20  ..    {"lower", 
325d9 20 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   1, SQLITE_UTF8,
325da 20 20 20 20 20 20 20 20 20 30 2c 20 69 63 75 43           0, icuC
325db 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20  aseFunc16},.    
325dc 7b 22 6c 6f 77 65 72 22 2c 20 20 32 2c 20 53 51  {"lower",  2, SQ
325dd 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 20 20  LITE_UTF8,      
325de 20 20 20 30 2c 20 69 63 75 43 61 73 65 46 75 6e     0, icuCaseFun
325df 63 31 36 7d 2c 0a 20 20 20 20 7b 22 75 70 70 65  c16},.    {"uppe
325e0 72 22 2c 20 20 31 2c 20 53 51 4c 49 54 45 5f 55  r",  1, SQLITE_U
325e1 54 46 38 2c 20 20 28 76 6f 69 64 2a 29 31 2c 20  TF8,  (void*)1, 
325e2 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a  icuCaseFunc16},.
325e3 20 20 20 20 7b 22 75 70 70 65 72 22 2c 20 20 32      {"upper",  2
325e4 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20  , SQLITE_UTF8,  
325e5 28 76 6f 69 64 2a 29 31 2c 20 69 63 75 43 61 73  (void*)1, icuCas
325e6 65 46 75 6e 63 31 36 7d 2c 0a 0a 20 20 20 20 7b  eFunc16},..    {
325e7 22 6c 69 6b 65 22 2c 20 20 20 32 2c 20 53 51 4c  "like",   2, SQL
325e8 49 54 45 5f 55 54 46 38 2c 20 20 20 20 20 20 20  ITE_UTF8,       
325e9 20 20 30 2c 20 69 63 75 4c 69 6b 65 46 75 6e 63    0, icuLikeFunc
325ea 7d 2c 0a 20 20 20 20 7b 22 6c 69 6b 65 22 2c 20  },.    {"like", 
325eb 20 20 33 2c 20 53 51 4c 49 54 45 5f 55 54 46 38    3, SQLITE_UTF8
325ec 2c 20 20 20 20 20 20 20 20 20 30 2c 20 69 63 75  ,         0, icu
325ed 4c 69 6b 65 46 75 6e 63 7d 2c 0a 0a 20 20 20 20  LikeFunc},..    
325ee 7b 22 69 63 75 5f 6c 6f 61 64 5f 63 6f 6c 6c 61  {"icu_load_colla
325ef 74 69 6f 6e 22 2c 20 20 32 2c 20 53 51 4c 49 54  tion",  2, SQLIT
325f0 45 5f 55 54 46 38 2c 20 28 76 6f 69 64 2a 29 64  E_UTF8, (void*)d
325f1 62 2c 20 69 63 75 4c 6f 61 64 43 6f 6c 6c 61 74  b, icuLoadCollat
325f2 69 6f 6e 7d 2c 0a 20 20 7d 3b 0a 0a 20 20 69 6e  ion},.  };..  in
325f3 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
325f4 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 66 6f  ;.  int i;..  fo
325f5 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54  r(i=0; rc==SQLIT
325f6 45 5f 4f 4b 20 26 26 20 69 3c 28 73 69 7a 65 6f  E_OK && i<(sizeo
325f7 66 28 73 63 61 6c 61 72 73 29 2f 73 69 7a 65 6f  f(scalars)/sizeo
325f8 66 28 73 74 72 75 63 74 20 49 63 75 53 63 61 6c  f(struct IcuScal
325f9 61 72 29 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ar)); i++){.    
325fa 73 74 72 75 63 74 20 49 63 75 53 63 61 6c 61 72  struct IcuScalar
325fb 20 2a 70 20 3d 20 26 73 63 61 6c 61 72 73 5b 69   *p = &scalars[i
325fc 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ];.    rc = sqli
325fd 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
325fe 69 6f 6e 28 0a 20 20 20 20 20 20 20 20 64 62 2c  ion(.        db,
325ff 20 70 2d 3e 7a 4e 61 6d 65 2c 20 70 2d 3e 6e 41   p->zName, p->nA
32600 72 67 2c 20 70 2d 3e 65 6e 63 2c 20 70 2d 3e 70  rg, p->enc, p->p
32601 43 6f 6e 74 65 78 74 2c 20 70 2d 3e 78 46 75 6e  Context, p->xFun
32602 63 2c 20 30 2c 20 30 0a 20 20 20 20 29 3b 0a 20  c, 0, 0.    );. 
32603 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
32604 0a 7d 0a 0a 23 69 66 20 21 53 51 4c 49 54 45 5f  .}..#if !SQLITE_
32605 43 4f 52 45 0a 53 51 4c 49 54 45 5f 41 50 49 20  CORE.SQLITE_API 
32606 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65  int sqlite3_exte
32607 6e 73 69 6f 6e 5f 69 6e 69 74 28 0a 20 20 73 71  nsion_init(.  sq
32608 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 63 68  lite3 *db, .  ch
32609 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c 0a 20  ar **pzErrMsg,. 
3260a 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61   const sqlite3_a
3260b 70 69 5f 72 6f 75 74 69 6e 65 73 20 2a 70 41 70  pi_routines *pAp
3260c 69 0a 29 7b 0a 20 20 53 51 4c 49 54 45 5f 45 58  i.){.  SQLITE_EX
3260d 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 32 28 70 41  TENSION_INIT2(pA
3260e 70 69 29 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  pi).  return sql
3260f 69 74 65 33 49 63 75 49 6e 69 74 28 64 62 29 3b  ite3IcuInit(db);
32610 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69  .}.#endif..#endi
32611 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f../************
32612 2a 2a 20 45 6e 64 20 6f 66 20 69 63 75 2e 63 20  ** End of icu.c 
32613 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32614 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32615 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32616 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
32617 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74  ** Begin file ft
32618 73 33 5f 69 63 75 2e 63 20 2a 2a 2a 2a 2a 2a 2a  s3_icu.c *******
32619 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3261a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3261b 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4a 75  */./*.** 2007 Ju
3261c 6e 65 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ne 22.**.** The 
3261d 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
3261e 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
3261f 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
32620 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
32621 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
32622 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
32623 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
32624 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
32625 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
32626 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
32627 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
32628 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
32629 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
3262a 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
3262b 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
3262c 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
3262d 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
3262e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3262f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32631 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32632 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
32633 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 74  e implements a t
32634 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 66 74 73  okenizer for fts
32635 33 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 49  3 based on the I
32636 43 55 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 20 0a  CU library..** .
32637 2a 2a 20 24 49 64 3a 20 66 74 73 33 5f 69 63 75  ** $Id: fts3_icu
32638 2e 63 2c 76 20 31 2e 33 20 32 30 30 38 2f 30 39  .c,v 1.3 2008/09
32639 2f 30 31 20 31 38 3a 33 34 3a 32 30 20 64 61 6e  /01 18:34:20 dan
3263a 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a  ielk1977 Exp $.*
3263b 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  /..#if !defined(
3263c 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
3263d 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
3263e 4e 41 42 4c 45 5f 46 54 53 33 29 0a 23 69 66 64  NABLE_FTS3).#ifd
3263f 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
32640 5f 49 43 55 0a 0a 0a 23 69 6e 63 6c 75 64 65 20  _ICU...#include 
32641 3c 75 6e 69 63 6f 64 65 2f 75 62 72 6b 2e 68 3e  <unicode/ubrk.h>
32642 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f  .#include <unico
32643 64 65 2f 75 74 66 31 36 2e 68 3e 0a 0a 74 79 70  de/utf16.h>..typ
32644 65 64 65 66 20 73 74 72 75 63 74 20 49 63 75 54  edef struct IcuT
32645 6f 6b 65 6e 69 7a 65 72 20 49 63 75 54 6f 6b 65  okenizer IcuToke
32646 6e 69 7a 65 72 3b 0a 74 79 70 65 64 65 66 20 73  nizer;.typedef s
32647 74 72 75 63 74 20 49 63 75 43 75 72 73 6f 72 20  truct IcuCursor 
32648 49 63 75 43 75 72 73 6f 72 3b 0a 0a 73 74 72 75  IcuCursor;..stru
32649 63 74 20 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20  ct IcuTokenizer 
3264a 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  {.  sqlite3_toke
3264b 6e 69 7a 65 72 20 62 61 73 65 3b 0a 20 20 63 68  nizer base;.  ch
3264c 61 72 20 2a 7a 4c 6f 63 61 6c 65 3b 0a 7d 3b 0a  ar *zLocale;.};.
3264d 0a 73 74 72 75 63 74 20 49 63 75 43 75 72 73 6f  .struct IcuCurso
3264e 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  r {.  sqlite3_to
3264f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 62  kenizer_cursor b
32650 61 73 65 3b 0a 0a 20 20 55 42 72 65 61 6b 49 74  ase;..  UBreakIt
32651 65 72 61 74 6f 72 20 2a 70 49 74 65 72 3b 20 20  erator *pIter;  
32652 20 20 20 20 2f 2a 20 49 43 55 20 62 72 65 61 6b      /* ICU break
32653 2d 69 74 65 72 61 74 6f 72 20 6f 62 6a 65 63 74  -iterator object
32654 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 72 3b   */.  int nChar;
32655 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32656 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 55    /* Number of U
32657 43 68 61 72 20 65 6c 65 6d 65 6e 74 73 20 69 6e  Char elements in
32658 20 70 49 6e 70 75 74 20 2a 2f 0a 20 20 55 43 68   pInput */.  UCh
32659 61 72 20 2a 61 43 68 61 72 3b 20 20 20 20 20 20  ar *aChar;      
3265a 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79           /* Copy
3265b 20 6f 66 20 69 6e 70 75 74 20 75 73 69 6e 67 20   of input using 
3265c 75 74 66 2d 31 36 20 65 6e 63 6f 64 69 6e 67 20  utf-16 encoding 
3265d 2a 2f 0a 20 20 69 6e 74 20 2a 61 4f 66 66 73 65  */.  int *aOffse
3265e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
3265f 20 2f 2a 20 4f 66 66 73 65 74 73 20 6f 66 20 65   /* Offsets of e
32660 61 63 68 20 63 68 61 72 61 63 74 65 72 20 69 6e  ach character in
32661 20 75 74 66 2d 38 20 69 6e 70 75 74 20 2a 2f 0a   utf-8 input */.
32662 0a 20 20 69 6e 74 20 6e 42 75 66 66 65 72 3b 0a  .  int nBuffer;.
32663 20 20 63 68 61 72 20 2a 7a 42 75 66 66 65 72 3b    char *zBuffer;
32664 0a 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e 3b 0a  ..  int iToken;.
32665 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  };../*.** Create
32666 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a 65 72   a new tokenizer
32667 20 69 6e 73 74 61 6e 63 65 2e 0a 2a 2f 0a 73 74   instance..*/.st
32668 61 74 69 63 20 69 6e 74 20 69 63 75 43 72 65 61  atic int icuCrea
32669 74 65 28 0a 20 20 69 6e 74 20 61 72 67 63 2c 20  te(.  int argc, 
3266a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3266b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
3266c 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
3266d 69 6e 20 61 72 67 76 5b 5d 20 2a 2f 0a 20 20 63  in argv[] */.  c
3266e 6f 6e 73 74 20 63 68 61 72 20 2a 20 63 6f 6e 73  onst char * cons
3266f 74 20 2a 61 72 67 76 2c 20 20 20 20 20 20 20 20  t *argv,        
32670 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72      /* Tokenizer
32671 20 63 72 65 61 74 69 6f 6e 20 61 72 67 75 6d 65   creation argume
32672 6e 74 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  nts */.  sqlite3
32673 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70 70 54  _tokenizer **ppT
32674 6f 6b 65 6e 69 7a 65 72 20 20 20 20 20 20 2f 2a  okenizer      /*
32675 20 4f 55 54 3a 20 43 72 65 61 74 65 64 20 74 6f   OUT: Created to
32676 6b 65 6e 69 7a 65 72 20 2a 2f 0a 29 7b 0a 20 20  kenizer */.){.  
32677 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 70 3b  IcuTokenizer *p;
32678 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 0a 20  .  int n = 0;.. 
32679 20 69 66 28 20 61 72 67 63 3e 30 20 29 7b 0a 20   if( argc>0 ){. 
3267a 20 20 20 6e 20 3d 20 73 74 72 6c 65 6e 28 61 72     n = strlen(ar
3267b 67 76 5b 30 5d 29 2b 31 3b 0a 20 20 7d 0a 20 20  gv[0])+1;.  }.  
3267c 70 20 3d 20 28 49 63 75 54 6f 6b 65 6e 69 7a 65  p = (IcuTokenize
3267d 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  r *)sqlite3_mall
3267e 6f 63 28 73 69 7a 65 6f 66 28 49 63 75 54 6f 6b  oc(sizeof(IcuTok
3267f 65 6e 69 7a 65 72 29 2b 6e 29 3b 0a 20 20 69 66  enizer)+n);.  if
32680 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 75  ( !p ){.    retu
32681 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
32682 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 2c  .  }.  memset(p,
32683 20 30 2c 20 73 69 7a 65 6f 66 28 49 63 75 54 6f   0, sizeof(IcuTo
32684 6b 65 6e 69 7a 65 72 29 29 3b 0a 0a 20 20 69 66  kenizer));..  if
32685 28 20 6e 20 29 7b 0a 20 20 20 20 70 2d 3e 7a 4c  ( n ){.    p->zL
32686 6f 63 61 6c 65 20 3d 20 28 63 68 61 72 20 2a 29  ocale = (char *)
32687 26 70 5b 31 5d 3b 0a 20 20 20 20 6d 65 6d 63 70  &p[1];.    memcp
32688 79 28 70 2d 3e 7a 4c 6f 63 61 6c 65 2c 20 61 72  y(p->zLocale, ar
32689 67 76 5b 30 5d 2c 20 6e 29 3b 0a 20 20 7d 0a 0a  gv[0], n);.  }..
3268a 20 20 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20 3d    *ppTokenizer =
3268b 20 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69   (sqlite3_tokeni
3268c 7a 65 72 20 2a 29 70 3b 0a 0a 20 20 72 65 74 75  zer *)p;..  retu
3268d 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
3268e 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61  ./*.** Destroy a
3268f 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74   tokenizer.*/.st
32690 61 74 69 63 20 69 6e 74 20 69 63 75 44 65 73 74  atic int icuDest
32691 72 6f 79 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65  roy(sqlite3_toke
32692 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65  nizer *pTokenize
32693 72 29 7b 0a 20 20 49 63 75 54 6f 6b 65 6e 69 7a  r){.  IcuTokeniz
32694 65 72 20 2a 70 20 3d 20 28 49 63 75 54 6f 6b 65  er *p = (IcuToke
32695 6e 69 7a 65 72 20 2a 29 70 54 6f 6b 65 6e 69 7a  nizer *)pTokeniz
32696 65 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  er;.  sqlite3_fr
32697 65 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ee(p);.  return 
32698 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
32699 0a 2a 2a 20 50 72 65 70 61 72 65 20 74 6f 20 62  .** Prepare to b
3269a 65 67 69 6e 20 74 6f 6b 65 6e 69 7a 69 6e 67 20  egin tokenizing 
3269b 61 20 70 61 72 74 69 63 75 6c 61 72 20 73 74 72  a particular str
3269c 69 6e 67 2e 20 20 54 68 65 20 69 6e 70 75 74 0a  ing.  The input.
3269d 2a 2a 20 73 74 72 69 6e 67 20 74 6f 20 62 65 20  ** string to be 
3269e 74 6f 6b 65 6e 69 7a 65 64 20 69 73 20 70 49 6e  tokenized is pIn
3269f 70 75 74 5b 30 2e 2e 6e 42 79 74 65 73 2d 31 5d  put[0..nBytes-1]
326a0 2e 20 20 41 20 63 75 72 73 6f 72 0a 2a 2a 20 75  .  A cursor.** u
326a1 73 65 64 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74  sed to increment
326a2 61 6c 6c 79 20 74 6f 6b 65 6e 69 7a 65 20 74 68  ally tokenize th
326a3 69 73 20 73 74 72 69 6e 67 20 69 73 20 72 65 74  is string is ret
326a4 75 72 6e 65 64 20 69 6e 20 0a 2a 2a 20 2a 70 70  urned in .** *pp
326a5 43 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69  Cursor..*/.stati
326a6 63 20 69 6e 74 20 69 63 75 4f 70 65 6e 28 0a 20  c int icuOpen(. 
326a7 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
326a8 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c 20  er *pTokenizer, 
326a9 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
326aa 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 63 6f  okenizer */.  co
326ab 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74  nst char *zInput
326ac 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
326ad 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 73 74       /* Input st
326ae 72 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 49  ring */.  int nI
326af 6e 70 75 74 2c 20 20 20 20 20 20 20 20 20 20 20  nput,           
326b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
326b1 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 49   /* Length of zI
326b2 6e 70 75 74 20 69 6e 20 62 79 74 65 73 20 2a 2f  nput in bytes */
326b3 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
326b4 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 2a 70 70  izer_cursor **pp
326b5 43 75 72 73 6f 72 20 20 20 20 2f 2a 20 4f 55 54  Cursor    /* OUT
326b6 3a 20 54 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63  : Tokenization c
326b7 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 49 63  ursor */.){.  Ic
326b8 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 70 20 3d 20  uTokenizer *p = 
326b9 28 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 29  (IcuTokenizer *)
326ba 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 49 63  pTokenizer;.  Ic
326bb 75 43 75 72 73 6f 72 20 2a 70 43 73 72 3b 0a 0a  uCursor *pCsr;..
326bc 20 20 63 6f 6e 73 74 20 69 6e 74 33 32 5f 74 20    const int32_t 
326bd 6f 70 74 20 3d 20 55 5f 46 4f 4c 44 5f 43 41 53  opt = U_FOLD_CAS
326be 45 5f 44 45 46 41 55 4c 54 3b 0a 20 20 55 45 72  E_DEFAULT;.  UEr
326bf 72 6f 72 43 6f 64 65 20 73 74 61 74 75 73 20 3d  rorCode status =
326c0 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b 0a 20   U_ZERO_ERROR;. 
326c1 20 69 6e 74 20 6e 43 68 61 72 3b 0a 0a 20 20 55   int nChar;..  U
326c2 43 68 61 72 33 32 20 63 3b 0a 20 20 69 6e 74 20  Char32 c;.  int 
326c3 69 49 6e 70 75 74 20 3d 20 30 3b 0a 20 20 69 6e  iInput = 0;.  in
326c4 74 20 69 4f 75 74 20 3d 20 30 3b 0a 0a 20 20 2a  t iOut = 0;..  *
326c5 70 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 0a 20  ppCursor = 0;.. 
326c6 20 69 66 28 20 6e 49 6e 70 75 74 3c 30 20 29 7b   if( nInput<0 ){
326c7 0a 20 20 20 20 6e 49 6e 70 75 74 20 3d 20 73 74  .    nInput = st
326c8 72 6c 65 6e 28 7a 49 6e 70 75 74 29 3b 0a 20 20  rlen(zInput);.  
326c9 7d 0a 20 20 6e 43 68 61 72 20 3d 20 6e 49 6e 70  }.  nChar = nInp
326ca 75 74 2b 31 3b 0a 20 20 70 43 73 72 20 3d 20 28  ut+1;.  pCsr = (
326cb 49 63 75 43 75 72 73 6f 72 20 2a 29 73 71 6c 69  IcuCursor *)sqli
326cc 74 65 33 5f 6d 61 6c 6c 6f 63 28 0a 20 20 20 20  te3_malloc(.    
326cd 20 20 73 69 7a 65 6f 66 28 49 63 75 43 75 72 73    sizeof(IcuCurs
326ce 6f 72 29 20 2b 20 20 20 20 20 20 20 20 20 20 20  or) +           
326cf 20 20 20 20 20 2f 2a 20 49 63 75 43 75 72 73 6f       /* IcuCurso
326d0 72 20 2a 2f 0a 20 20 20 20 20 20 6e 43 68 61 72  r */.      nChar
326d1 20 2a 20 73 69 7a 65 6f 66 28 55 43 68 61 72 29   * sizeof(UChar)
326d2 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a   +            /*
326d3 20 49 63 75 43 75 72 73 6f 72 2e 61 43 68 61 72   IcuCursor.aChar
326d4 5b 5d 20 2a 2f 0a 20 20 20 20 20 20 28 6e 43 68  [] */.      (nCh
326d5 61 72 2b 31 29 20 2a 20 73 69 7a 65 6f 66 28 69  ar+1) * sizeof(i
326d6 6e 74 29 20 20 20 20 20 20 20 20 20 20 20 20 2f  nt)            /
326d7 2a 20 49 63 75 43 75 72 73 6f 72 2e 61 4f 66 66  * IcuCursor.aOff
326d8 73 65 74 5b 5d 20 2a 2f 0a 20 20 29 3b 0a 20 20  set[] */.  );.  
326d9 69 66 28 20 21 70 43 73 72 20 29 7b 0a 20 20 20  if( !pCsr ){.   
326da 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
326db 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73  OMEM;.  }.  mems
326dc 65 74 28 70 43 73 72 2c 20 30 2c 20 73 69 7a 65  et(pCsr, 0, size
326dd 6f 66 28 49 63 75 43 75 72 73 6f 72 29 29 3b 0a  of(IcuCursor));.
326de 20 20 70 43 73 72 2d 3e 61 43 68 61 72 20 3d 20    pCsr->aChar = 
326df 28 55 43 68 61 72 20 2a 29 26 70 43 73 72 5b 31  (UChar *)&pCsr[1
326e0 5d 3b 0a 20 20 70 43 73 72 2d 3e 61 4f 66 66 73  ];.  pCsr->aOffs
326e1 65 74 20 3d 20 28 69 6e 74 20 2a 29 26 70 43 73  et = (int *)&pCs
326e2 72 2d 3e 61 43 68 61 72 5b 6e 43 68 61 72 5d 3b  r->aChar[nChar];
326e3 0a 0a 20 20 70 43 73 72 2d 3e 61 4f 66 66 73 65  ..  pCsr->aOffse
326e4 74 5b 69 4f 75 74 5d 20 3d 20 69 49 6e 70 75 74  t[iOut] = iInput
326e5 3b 0a 20 20 55 38 5f 4e 45 58 54 28 7a 49 6e 70  ;.  U8_NEXT(zInp
326e6 75 74 2c 20 69 49 6e 70 75 74 2c 20 6e 49 6e 70  ut, iInput, nInp
326e7 75 74 2c 20 63 29 3b 20 0a 20 20 77 68 69 6c 65  ut, c); .  while
326e8 28 20 63 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74  ( c>0 ){.    int
326e9 20 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20   isError = 0;.  
326ea 20 20 63 20 3d 20 75 5f 66 6f 6c 64 43 61 73 65    c = u_foldCase
326eb 28 63 2c 20 6f 70 74 29 3b 0a 20 20 20 20 55 31  (c, opt);.    U1
326ec 36 5f 41 50 50 45 4e 44 28 70 43 73 72 2d 3e 61  6_APPEND(pCsr->a
326ed 43 68 61 72 2c 20 69 4f 75 74 2c 20 6e 43 68 61  Char, iOut, nCha
326ee 72 2c 20 63 2c 20 69 73 45 72 72 6f 72 29 3b 0a  r, c, isError);.
326ef 20 20 20 20 69 66 28 20 69 73 45 72 72 6f 72 20      if( isError 
326f0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
326f1 5f 66 72 65 65 28 70 43 73 72 29 3b 0a 20 20 20  _free(pCsr);.   
326f2 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
326f3 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20  _ERROR;.    }.  
326f4 20 20 70 43 73 72 2d 3e 61 4f 66 66 73 65 74 5b    pCsr->aOffset[
326f5 69 4f 75 74 5d 20 3d 20 69 49 6e 70 75 74 3b 0a  iOut] = iInput;.
326f6 0a 20 20 20 20 69 66 28 20 69 49 6e 70 75 74 3c  .    if( iInput<
326f7 6e 49 6e 70 75 74 20 29 7b 0a 20 20 20 20 20 20  nInput ){.      
326f8 55 38 5f 4e 45 58 54 28 7a 49 6e 70 75 74 2c 20  U8_NEXT(zInput, 
326f9 69 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2c 20  iInput, nInput, 
326fa 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  c);.    }else{. 
326fb 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 20 20 20       c = 0;.    
326fc 7d 0a 20 20 7d 0a 0a 20 20 70 43 73 72 2d 3e 70  }.  }..  pCsr->p
326fd 49 74 65 72 20 3d 20 75 62 72 6b 5f 6f 70 65 6e  Iter = ubrk_open
326fe 28 55 42 52 4b 5f 57 4f 52 44 2c 20 70 2d 3e 7a  (UBRK_WORD, p->z
326ff 4c 6f 63 61 6c 65 2c 20 70 43 73 72 2d 3e 61 43  Locale, pCsr->aC
32700 68 61 72 2c 20 69 4f 75 74 2c 20 26 73 74 61 74  har, iOut, &stat
32701 75 73 29 3b 0a 20 20 69 66 28 20 21 55 5f 53 55  us);.  if( !U_SU
32702 43 43 45 53 53 28 73 74 61 74 75 73 29 20 29 7b  CCESS(status) ){
32703 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
32704 65 28 70 43 73 72 29 3b 0a 20 20 20 20 72 65 74  e(pCsr);.    ret
32705 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
32706 3b 0a 20 20 7d 0a 20 20 70 43 73 72 2d 3e 6e 43  ;.  }.  pCsr->nC
32707 68 61 72 20 3d 20 69 4f 75 74 3b 0a 0a 20 20 75  har = iOut;..  u
32708 62 72 6b 5f 66 69 72 73 74 28 70 43 73 72 2d 3e  brk_first(pCsr->
32709 70 49 74 65 72 29 3b 0a 20 20 2a 70 70 43 75 72  pIter);.  *ppCur
3270a 73 6f 72 20 3d 20 28 73 71 6c 69 74 65 33 5f 74  sor = (sqlite3_t
3270b 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
3270c 2a 29 70 43 73 72 3b 0a 20 20 72 65 74 75 72 6e  *)pCsr;.  return
3270d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
3270e 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 74 6f 6b  *.** Close a tok
3270f 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72  enization cursor
32710 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e   previously open
32711 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20  ed by a call to 
32712 69 63 75 4f 70 65 6e 28 29 2e 0a 2a 2f 0a 73 74  icuOpen()..*/.st
32713 61 74 69 63 20 69 6e 74 20 69 63 75 43 6c 6f 73  atic int icuClos
32714 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  e(sqlite3_tokeni
32715 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  zer_cursor *pCur
32716 73 6f 72 29 7b 0a 20 20 49 63 75 43 75 72 73 6f  sor){.  IcuCurso
32717 72 20 2a 70 43 73 72 20 3d 20 28 49 63 75 43 75  r *pCsr = (IcuCu
32718 72 73 6f 72 20 2a 29 70 43 75 72 73 6f 72 3b 0a  rsor *)pCursor;.
32719 20 20 75 62 72 6b 5f 63 6c 6f 73 65 28 70 43 73    ubrk_close(pCs
3271a 72 2d 3e 70 49 74 65 72 29 3b 0a 20 20 73 71 6c  r->pIter);.  sql
3271b 69 74 65 33 5f 66 72 65 65 28 70 43 73 72 2d 3e  ite3_free(pCsr->
3271c 7a 42 75 66 66 65 72 29 3b 0a 20 20 73 71 6c 69  zBuffer);.  sqli
3271d 74 65 33 5f 66 72 65 65 28 70 43 73 72 29 3b 0a  te3_free(pCsr);.
3271e 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3271f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74  OK;.}../*.** Ext
32720 72 61 63 74 20 74 68 65 20 6e 65 78 74 20 74 6f  ract the next to
32721 6b 65 6e 20 66 72 6f 6d 20 61 20 74 6f 6b 65 6e  ken from a token
32722 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72 2e 0a  ization cursor..
32723 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 63  */.static int ic
32724 75 4e 65 78 74 28 0a 20 20 73 71 6c 69 74 65 33  uNext(.  sqlite3
32725 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
32726 72 20 2a 70 43 75 72 73 6f 72 2c 20 20 2f 2a 20  r *pCursor,  /* 
32727 43 75 72 73 6f 72 20 72 65 74 75 72 6e 65 64 20  Cursor returned 
32728 62 79 20 73 69 6d 70 6c 65 4f 70 65 6e 20 2a 2f  by simpleOpen */
32729 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  .  const char **
3272a 70 70 54 6f 6b 65 6e 2c 20 20 20 20 20 20 20 20  ppToken,        
3272b 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 2a         /* OUT: *
3272c 70 70 54 6f 6b 65 6e 20 69 73 20 74 68 65 20 74  ppToken is the t
3272d 6f 6b 65 6e 20 74 65 78 74 20 2a 2f 0a 20 20 69  oken text */.  i
3272e 6e 74 20 2a 70 6e 42 79 74 65 73 2c 20 20 20 20  nt *pnBytes,    
3272f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32730 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65     /* OUT: Numbe
32731 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 6f  r of bytes in to
32732 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69  ken */.  int *pi
32733 53 74 61 72 74 4f 66 66 73 65 74 2c 20 20 20 20  StartOffset,    
32734 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
32735 4f 55 54 3a 20 53 74 61 72 74 69 6e 67 20 6f 66  OUT: Starting of
32736 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f  fset of token */
32737 0a 20 20 69 6e 74 20 2a 70 69 45 6e 64 4f 66 66  .  int *piEndOff
32738 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  set,            
32739 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45         /* OUT: E
3273a 6e 64 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20  nding offset of 
3273b 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a  token */.  int *
3273c 70 69 50 6f 73 69 74 69 6f 6e 20 20 20 20 20 20  piPosition      
3273d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3273e 2a 20 4f 55 54 3a 20 50 6f 73 69 74 69 6f 6e 20  * OUT: Position 
3273f 69 6e 74 65 67 65 72 20 6f 66 20 74 6f 6b 65 6e  integer of token
32740 20 2a 2f 0a 29 7b 0a 20 20 49 63 75 43 75 72 73   */.){.  IcuCurs
32741 6f 72 20 2a 70 43 73 72 20 3d 20 28 49 63 75 43  or *pCsr = (IcuC
32742 75 72 73 6f 72 20 2a 29 70 43 75 72 73 6f 72 3b  ursor *)pCursor;
32743 0a 0a 20 20 69 6e 74 20 69 53 74 61 72 74 20 3d  ..  int iStart =
32744 20 30 3b 0a 20 20 69 6e 74 20 69 45 6e 64 20 3d   0;.  int iEnd =
32745 20 30 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20   0;.  int nByte 
32746 3d 20 30 3b 0a 0a 20 20 77 68 69 6c 65 28 20 69  = 0;..  while( i
32747 53 74 61 72 74 3d 3d 69 45 6e 64 20 29 7b 0a 20  Start==iEnd ){. 
32748 20 20 20 55 43 68 61 72 33 32 20 63 3b 0a 0a 20     UChar32 c;.. 
32749 20 20 20 69 53 74 61 72 74 20 3d 20 75 62 72 6b     iStart = ubrk
3274a 5f 63 75 72 72 65 6e 74 28 70 43 73 72 2d 3e 70  _current(pCsr->p
3274b 49 74 65 72 29 3b 0a 20 20 20 20 69 45 6e 64 20  Iter);.    iEnd 
3274c 3d 20 75 62 72 6b 5f 6e 65 78 74 28 70 43 73 72  = ubrk_next(pCsr
3274d 2d 3e 70 49 74 65 72 29 3b 0a 20 20 20 20 69 66  ->pIter);.    if
3274e 28 20 69 45 6e 64 3d 3d 55 42 52 4b 5f 44 4f 4e  ( iEnd==UBRK_DON
3274f 45 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  E ){.      retur
32750 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  n SQLITE_DONE;. 
32751 20 20 20 7d 0a 0a 20 20 20 20 77 68 69 6c 65 28     }..    while(
32752 20 69 53 74 61 72 74 3c 69 45 6e 64 20 29 7b 0a   iStart<iEnd ){.
32753 20 20 20 20 20 20 69 6e 74 20 69 57 68 69 74 65        int iWhite
32754 20 3d 20 69 53 74 61 72 74 3b 0a 20 20 20 20 20   = iStart;.     
32755 20 55 38 5f 4e 45 58 54 28 70 43 73 72 2d 3e 61   U8_NEXT(pCsr->a
32756 43 68 61 72 2c 20 69 57 68 69 74 65 2c 20 70 43  Char, iWhite, pC
32757 73 72 2d 3e 6e 43 68 61 72 2c 20 63 29 3b 0a 20  sr->nChar, c);. 
32758 20 20 20 20 20 69 66 28 20 75 5f 69 73 73 70 61       if( u_isspa
32759 63 65 28 63 29 20 29 7b 0a 20 20 20 20 20 20 20  ce(c) ){.       
3275a 20 69 53 74 61 72 74 20 3d 20 69 57 68 69 74 65   iStart = iWhite
3275b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
3275c 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3275d 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
3275e 61 73 73 65 72 74 28 69 53 74 61 72 74 3c 3d 69  assert(iStart<=i
3275f 45 6e 64 29 3b 0a 20 20 7d 0a 0a 20 20 64 6f 20  End);.  }..  do 
32760 7b 0a 20 20 20 20 55 45 72 72 6f 72 43 6f 64 65  {.    UErrorCode
32761 20 73 74 61 74 75 73 20 3d 20 55 5f 5a 45 52 4f   status = U_ZERO
32762 5f 45 52 52 4f 52 3b 0a 20 20 20 20 69 66 28 20  _ERROR;.    if( 
32763 6e 42 79 74 65 20 29 7b 0a 20 20 20 20 20 20 63  nByte ){.      c
32764 68 61 72 20 2a 7a 4e 65 77 20 3d 20 73 71 6c 69  har *zNew = sqli
32765 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 43 73 72  te3_realloc(pCsr
32766 2d 3e 7a 42 75 66 66 65 72 2c 20 6e 42 79 74 65  ->zBuffer, nByte
32767 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 7a 4e  );.      if( !zN
32768 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  ew ){.        re
32769 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
3276a 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  M;.      }.     
3276b 20 70 43 73 72 2d 3e 7a 42 75 66 66 65 72 20 3d   pCsr->zBuffer =
3276c 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20 70 43 73   zNew;.      pCs
3276d 72 2d 3e 6e 42 75 66 66 65 72 20 3d 20 6e 42 79  r->nBuffer = nBy
3276e 74 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 75  te;.    }..    u
3276f 5f 73 74 72 54 6f 55 54 46 38 28 0a 20 20 20 20  _strToUTF8(.    
32770 20 20 20 20 70 43 73 72 2d 3e 7a 42 75 66 66 65      pCsr->zBuffe
32771 72 2c 20 70 43 73 72 2d 3e 6e 42 75 66 66 65 72  r, pCsr->nBuffer
32772 2c 20 26 6e 42 79 74 65 2c 20 20 20 20 2f 2a 20  , &nByte,    /* 
32773 4f 75 74 70 75 74 20 76 61 72 73 20 2a 2f 0a 20  Output vars */. 
32774 20 20 20 20 20 20 20 26 70 43 73 72 2d 3e 61 43         &pCsr->aC
32775 68 61 72 5b 69 53 74 61 72 74 5d 2c 20 69 45 6e  har[iStart], iEn
32776 64 2d 69 53 74 61 72 74 2c 20 20 20 20 20 20 20  d-iStart,       
32777 2f 2a 20 49 6e 70 75 74 20 76 61 72 73 20 2a 2f  /* Input vars */
32778 0a 20 20 20 20 20 20 20 20 26 73 74 61 74 75 73  .        &status
32779 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3277a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3277b 20 20 2f 2a 20 4f 75 74 70 75 74 20 73 75 63 63    /* Output succ
3277c 65 73 73 2f 66 61 69 6c 75 72 65 20 2a 2f 0a 20  ess/failure */. 
3277d 20 20 20 29 3b 0a 20 20 7d 20 77 68 69 6c 65 28     );.  } while(
3277e 20 6e 42 79 74 65 3e 70 43 73 72 2d 3e 6e 42 75   nByte>pCsr->nBu
3277f 66 66 65 72 20 29 3b 0a 0a 20 20 2a 70 70 54 6f  ffer );..  *ppTo
32780 6b 65 6e 20 3d 20 70 43 73 72 2d 3e 7a 42 75 66  ken = pCsr->zBuf
32781 66 65 72 3b 0a 20 20 2a 70 6e 42 79 74 65 73 20  fer;.  *pnBytes 
32782 3d 20 6e 42 79 74 65 3b 0a 20 20 2a 70 69 53 74  = nByte;.  *piSt
32783 61 72 74 4f 66 66 73 65 74 20 3d 20 70 43 73 72  artOffset = pCsr
32784 2d 3e 61 4f 66 66 73 65 74 5b 69 53 74 61 72 74  ->aOffset[iStart
32785 5d 3b 0a 20 20 2a 70 69 45 6e 64 4f 66 66 73 65  ];.  *piEndOffse
32786 74 20 3d 20 70 43 73 72 2d 3e 61 4f 66 66 73 65  t = pCsr->aOffse
32787 74 5b 69 45 6e 64 5d 3b 0a 20 20 2a 70 69 50 6f  t[iEnd];.  *piPo
32788 73 69 74 69 6f 6e 20 3d 20 70 43 73 72 2d 3e 69  sition = pCsr->i
32789 54 6f 6b 65 6e 2b 2b 3b 0a 0a 20 20 72 65 74 75  Token++;..  retu
3278a 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
3278b 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 74 20 6f  ./*.** The set o
3278c 66 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20  f routines that 
3278d 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 73 69  implement the si
3278e 6d 70 6c 65 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a  mple tokenizer.*
3278f 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73  /.static const s
32790 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
32791 5f 6d 6f 64 75 6c 65 20 69 63 75 54 6f 6b 65 6e  _module icuToken
32792 69 7a 65 72 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20  izerModule = {. 
32793 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
32794 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
32795 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 69   iVersion */.  i
32796 63 75 43 72 65 61 74 65 2c 20 20 20 20 20 20 20  cuCreate,       
32797 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
32798 43 72 65 61 74 65 20 20 2a 2f 0a 20 20 69 63 75  Create  */.  icu
32799 44 65 73 74 72 6f 79 2c 20 20 20 20 20 20 20 20  Destroy,        
3279a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 72            /* xCr
3279b 65 61 74 65 20 20 2a 2f 0a 20 20 69 63 75 4f 70  eate  */.  icuOp
3279c 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  en,             
3279d 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e          /* xOpen
3279e 20 20 20 20 2a 2f 0a 20 20 69 63 75 43 6c 6f 73      */.  icuClos
3279f 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
327a0 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20        /* xClose 
327a1 20 20 2a 2f 0a 20 20 69 63 75 4e 65 78 74 2c 20    */.  icuNext, 
327a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
327a3 20 20 20 20 2f 2a 20 78 4e 65 78 74 20 20 20 20      /* xNext    
327a4 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  */.};../*.** Set
327a5 20 2a 70 70 4d 6f 64 75 6c 65 20 74 6f 20 70 6f   *ppModule to po
327a6 69 6e 74 20 61 74 20 74 68 65 20 69 6d 70 6c 65  int at the imple
327a7 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
327a8 20 49 43 55 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a   ICU tokenizer..
327a9 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
327aa 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74  E void sqlite3Ft
327ab 73 33 49 63 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f  s3IcuTokenizerMo
327ac 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  dule(.  sqlite3_
327ad 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
327ae 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65   const**ppModule
327af 0a 29 7b 0a 20 20 2a 70 70 4d 6f 64 75 6c 65 20  .){.  *ppModule 
327b0 3d 20 26 69 63 75 54 6f 6b 65 6e 69 7a 65 72 4d  = &icuTokenizerM
327b1 6f 64 75 6c 65 3b 0a 7d 0a 0a 23 65 6e 64 69 66  odule;.}..#endif
327b2 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49   /* defined(SQLI
327b3 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55 29 20 2a  TE_ENABLE_ICU) *
327b4 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  /.#endif /* !def
327b5 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45  ined(SQLITE_CORE
327b6 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
327b7 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29  ITE_ENABLE_FTS3)
327b8 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
327b9 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33  **** End of fts3
327ba 5f 69 63 75 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  _icu.c *********
327bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
327bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
327bd 2a 2a 2a 2f 0a                                   ***/.